Hex Artifact Content
Not logged in

Artifact 8fe0e0114d470db29d246c609567b507febd9197:

File src/sqlite3.c part of check-in [b4ec5750c6] - Update the built-in SQLite to version 3.6.14. by drh on 2009-05-07 00:47:20.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 31 34 2e 20 20  ersion 3.6.14.  
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 35 33 33 20 6c 69 6e 65  rst.** 5533 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 35 2d 30 37 20 30 30 3a 33   2009-05-07 00:3
04c0: 36 3a 31 31 20 55 54 43 2e 0a 2a 2f 0a 23 64 65  6:11 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 36 38 20 32 30 30 39 2f 30 35 2f 30 34 20  .868 2009/05/04 
0790: 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65 6c 6b  11:42:30 danielk
07a0: 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69  1977 Exp $.*/.#i
07b0: 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 49 4e 54  fndef _SQLITEINT
07c0: 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
07d0: 49 54 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  ITEINT_H_../*.**
07e0: 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63 6f 6e   Include the con
07f0: 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61 64 65  figuration heade
0800: 72 20 6f 75 74 70 75 74 20 62 79 20 27 63 6f 6e  r output by 'con
0810: 66 69 67 75 72 65 27 20 69 66 20 77 65 27 72 65  figure' if we're
0820: 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 61 75   using the.** au
0830: 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62 75 69  toconf-based bui
0840: 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 48 41  ld.*/.#ifdef _HA
0850: 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  VE_SQLITE_CONFIG
0860: 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e  _H.#include "con
0870: 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a 0a 2f  fig.h".#endif../
0880: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
0890: 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c 69 6d  nclude sqliteLim
08a0: 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  it.h in the midd
08b0: 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
08c0: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  h ***********/./
08d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
08e0: 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65  egin file sqlite
08f0: 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  Limit.h ********
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0910: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
0920: 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 37 0a  *.** 2007 May 7.
0930: 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
0940: 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
0950: 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
0960: 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
0970: 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
0980: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
0990: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
09a0: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
09b0: 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
09c0: 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
09d0: 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
09e0: 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
09f0: 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
0a00: 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
0a10: 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
0a20: 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
0a30: 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
0a40: 2e 0a 2a 2a 0a 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 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
0a90: 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  * .** This file 
0aa0: 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75 73 20  defines various 
0ab0: 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74 20 53  limits of what S
0ac0: 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63 65 73  QLite can proces
0ad0: 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  s..**.** @(#) $I
0ae0: 64 3a 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68  d: sqliteLimit.h
0af0: 2c 76 20 31 2e 31 30 20 32 30 30 39 2f 30 31 2f  ,v 1.10 2009/01/
0b00: 31 30 20 31 36 3a 31 35 3a 30 39 20 64 61 6e 69  10 16:15:09 dani
0b10: 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
0b20: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ../*.** The maxi
0b30: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20  mum length of a 
0b40: 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 69 6e 20  TEXT or BLOB in 
0b50: 62 79 74 65 73 2e 20 20 20 54 68 69 73 20 61 6c  bytes.   This al
0b60: 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 20 74 68 65  so.** limits the
0b70: 20 73 69 7a 65 20 6f 66 20 61 20 72 6f 77 20 69   size of a row i
0b80: 6e 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  n a table or ind
0b90: 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61  ex..**.** The ha
0ba0: 72 64 20 6c 69 6d 69 74 20 69 73 20 74 68 65 20  rd limit is the 
0bb0: 61 62 69 6c 69 74 79 20 6f 66 20 61 20 33 32 2d  ability of a 32-
0bc0: 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
0bd0: 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 6e 74 20 74  er.** to count t
0be0: 68 65 20 73 69 7a 65 3a 20 32 5e 33 31 2d 31 20  he size: 2^31-1 
0bf0: 6f 72 20 32 31 34 37 34 38 33 36 34 37 2e 0a 2a  or 2147483647..*
0c00: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
0c10: 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20 64 65  _MAX_LENGTH.# de
0c20: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
0c30: 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 30 30  LENGTH 100000000
0c40: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
0c50: 54 68 69 73 20 69 73 20 74 68 65 20 6d 61 78 69  This is the maxi
0c60: 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  mum number of.**
0c70: 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73  .**    * Columns
0c80: 20 69 6e 20 61 20 74 61 62 6c 65 0a 2a 2a 20 20   in a table.**  
0c90: 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61    * Columns in a
0ca0: 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 2a 20  n index.**    * 
0cb0: 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 76 69 65  Columns in a vie
0cc0: 77 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20  w.**    * Terms 
0cd0: 69 6e 20 74 68 65 20 53 45 54 20 63 6c 61 75 73  in the SET claus
0ce0: 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45 20 73  e of an UPDATE s
0cf0: 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a  tatement.**    *
0d00: 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 72 65   Terms in the re
0d10: 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45  sult set of a SE
0d20: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a  LECT statement.*
0d30: 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20  *    * Terms in 
0d40: 74 68 65 20 47 52 4f 55 50 20 42 59 20 6f 72 20  the GROUP BY or 
0d50: 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73  ORDER BY clauses
0d60: 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
0d70: 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 20 20 2a 20  tement..**    * 
0d80: 54 65 72 6d 73 20 69 6e 20 74 68 65 20 56 41 4c  Terms in the VAL
0d90: 55 45 53 20 63 6c 61 75 73 65 20 6f 66 20 61 6e  UES clause of an
0da0: 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
0db0: 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 72 64  t.**.** The hard
0dc0: 20 75 70 70 65 72 20 6c 69 6d 69 74 20 68 65 72   upper limit her
0dd0: 65 20 69 73 20 33 32 36 37 36 2e 20 20 4d 6f 73  e is 32676.  Mos
0de0: 74 20 64 61 74 61 62 61 73 65 20 70 65 6f 70 6c  t database peopl
0df0: 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 6c 6c 20 79  e will.** tell y
0e00: 6f 75 20 74 68 61 74 20 69 6e 20 61 20 77 65 6c  ou that in a wel
0e10: 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 61 74  l-normalized dat
0e20: 61 62 61 73 65 2c 20 79 6f 75 20 75 73 75 61 6c  abase, you usual
0e30: 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74  ly should.** not
0e40: 20 68 61 76 65 20 6d 6f 72 65 20 74 68 61 6e 20   have more than 
0e50: 61 20 64 6f 7a 65 6e 20 6f 72 20 73 6f 20 63 6f  a dozen or so co
0e60: 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 20 74 61 62  lumns in any tab
0e70: 6c 65 2e 20 20 41 6e 64 20 69 66 0a 2a 2a 20 74  le.  And if.** t
0e80: 68 61 74 20 69 73 20 74 68 65 20 63 61 73 65 2c  hat is the case,
0e90: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69   there is no poi
0ea0: 6e 74 20 69 6e 20 68 61 76 69 6e 67 20 6d 6f 72  nt in having mor
0eb0: 65 20 74 68 61 6e 20 61 20 66 65 77 0a 2a 2a 20  e than a few.** 
0ec0: 64 6f 7a 65 6e 20 76 61 6c 75 65 73 20 69 6e 20  dozen values in 
0ed0: 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72  any of the other
0ee0: 20 73 69 74 75 61 74 69 6f 6e 73 20 64 65 73 63   situations desc
0ef0: 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a  ribed above..*/.
0f00: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
0f10: 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 64 65 66 69  AX_COLUMN.# defi
0f20: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f  ne SQLITE_MAX_CO
0f30: 4c 55 4d 4e 20 32 30 30 30 0a 23 65 6e 64 69 66  LUMN 2000.#endif
0f40: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ../*.** The maxi
0f50: 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 20  mum length of a 
0f60: 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65  single SQL state
0f70: 6d 65 6e 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a  ment in bytes..*
0f80: 2a 0a 2a 2a 20 49 74 20 75 73 65 64 20 74 6f 20  *.** It used to 
0f90: 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
0fa0: 20 73 65 74 74 69 6e 67 20 74 68 69 73 20 76 61   setting this va
0fb0: 6c 75 65 20 74 6f 20 7a 65 72 6f 20 77 6f 75 6c  lue to zero woul
0fc0: 64 0a 2a 2a 20 74 75 72 6e 20 74 68 65 20 6c 69  d.** turn the li
0fd0: 6d 69 74 20 6f 66 66 2e 20 20 54 68 61 74 20 69  mit off.  That i
0fe0: 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 65  s no longer true
0ff0: 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  .  It is not pos
1000: 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 74 75 72 6e  sible.** to turn
1010: 20 74 68 69 73 20 6c 69 6d 69 74 20 6f 66 66 2e   this limit off.
1020: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1030: 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
1040: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  H.# define SQLIT
1050: 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48  E_MAX_SQL_LENGTH
1060: 20 31 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64   1000000000.#end
1070: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
1080: 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 61  ximum depth of a
1090: 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
10a0: 65 2e 20 54 68 69 73 20 69 73 20 6c 69 6d 69 74  e. This is limit
10b0: 65 64 20 74 6f 20 0a 2a 2a 20 73 6f 6d 65 20 65  ed to .** some e
10c0: 78 74 65 6e 74 20 62 79 20 53 51 4c 49 54 45 5f  xtent by SQLITE_
10d0: 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2e 20  MAX_SQL_LENGTH. 
10e0: 42 75 74 20 73 6f 6d 65 74 69 6d 65 20 79 6f 75  But sometime you
10f0: 20 6d 69 67 68 74 20 0a 2a 2a 20 77 61 6e 74 20   might .** want 
1100: 74 6f 20 70 6c 61 63 65 20 6d 6f 72 65 20 73 65  to place more se
1110: 76 65 72 65 20 6c 69 6d 69 74 73 20 6f 6e 20 74  vere limits on t
1120: 68 65 20 63 6f 6d 70 6c 65 78 69 74 79 20 6f 66  he complexity of
1130: 20 61 6e 20 0a 2a 2a 20 65 78 70 72 65 73 73 69   an .** expressi
1140: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 6c 75  on..**.** A valu
1150: 65 20 6f 66 20 30 20 75 73 65 64 20 74 6f 20 6d  e of 0 used to m
1160: 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69 6d  ean that the lim
1170: 69 74 20 77 61 73 20 6e 6f 74 20 65 6e 66 6f 72  it was not enfor
1180: 63 65 64 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74  ced..** But that
1190: 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72   is no longer tr
11a0: 75 65 2e 20 20 54 68 65 20 6c 69 6d 69 74 20 69  ue.  The limit i
11b0: 73 20 6e 6f 77 20 73 74 72 69 63 74 6c 79 20 65  s now strictly e
11c0: 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 74 20 61 6c  nforced.** at al
11d0: 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 6e  l times..*/.#ifn
11e0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45  def SQLITE_MAX_E
11f0: 58 50 52 5f 44 45 50 54 48 0a 23 20 64 65 66 69  XPR_DEPTH.# defi
1200: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  ne SQLITE_MAX_EX
1210: 50 52 5f 44 45 50 54 48 20 31 30 30 30 0a 23 65  PR_DEPTH 1000.#e
1220: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1230: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1240: 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d  f terms in a com
1250: 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61  pound SELECT sta
1260: 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 63  tement..** The c
1270: 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 66 6f  ode generator fo
1280: 72 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  r compound SELEC
1290: 54 20 73 74 61 74 65 6d 65 6e 74 73 20 64 6f 65  T statements doe
12a0: 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 65 6c 20 6f  s one.** level o
12b0: 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 20  f recursion for 
12c0: 65 61 63 68 20 74 65 72 6d 2e 20 20 41 20 73 74  each term.  A st
12d0: 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 63 61 6e  ack overflow can
12e0: 20 72 65 73 75 6c 74 0a 2a 2a 20 69 66 20 74 68   result.** if th
12f0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  e number of term
1300: 73 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2e 20  s is too large. 
1310: 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 6f   In practice, mo
1320: 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 76 65 72 20  st SQL.** never 
1330: 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 33 20  has more than 3 
1340: 6f 72 20 34 20 74 65 72 6d 73 2e 20 20 55 73 65  or 4 terms.  Use
1350: 20 61 20 76 61 6c 75 65 20 6f 66 20 30 20 74 6f   a value of 0 to
1360: 20 64 69 73 61 62 6c 65 0a 2a 2a 20 61 6e 79 20   disable.** any 
1370: 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d  limit on the num
1380: 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
1390: 61 20 63 6f 6d 70 6f 75 6e 74 20 53 45 4c 45 43  a compount SELEC
13a0: 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  T..*/.#ifndef SQ
13b0: 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e  LITE_MAX_COMPOUN
13c0: 44 5f 53 45 4c 45 43 54 0a 23 20 64 65 66 69 6e  D_SELECT.# defin
13d0: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d  e SQLITE_MAX_COM
13e0: 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 35 30 30  POUND_SELECT 500
13f0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1400: 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1410: 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 69 6e 20  r of opcodes in 
1420: 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a  a VDBE program..
1430: 2a 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79  ** Not currently
1440: 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2f 0a 23 69   enforced..*/.#i
1450: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
1460: 5f 56 44 42 45 5f 4f 50 0a 23 20 64 65 66 69 6e  _VDBE_OP.# defin
1470: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42  e SQLITE_MAX_VDB
1480: 45 5f 4f 50 20 32 35 30 30 30 0a 23 65 6e 64 69  E_OP 25000.#endi
1490: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  f../*.** The max
14a0: 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61  imum number of a
14b0: 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 6e 20 53  rguments to an S
14c0: 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  QL function..*/.
14d0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
14e0: 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 0a  AX_FUNCTION_ARG.
14f0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1500: 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
1510: 20 31 32 37 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   127.#endif../*.
1520: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1530: 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f  umber of in-memo
1540: 72 79 20 70 61 67 65 73 20 74 6f 20 75 73 65 20  ry pages to use 
1550: 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  for the main dat
1560: 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 61  abase.** table a
1570: 6e 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  nd for temporary
1580: 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 53 51   tables.  The SQ
1590: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
15a0: 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 69 66 6e 64  HE_SIZE.*/.#ifnd
15b0: 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ef SQLITE_DEFAUL
15c0: 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64  T_CACHE_SIZE.# d
15d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
15e0: 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 20  AULT_CACHE_SIZE 
15f0: 20 32 30 30 30 0a 23 65 6e 64 69 66 0a 23 69 66   2000.#endif.#if
1600: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
1610: 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53  ULT_TEMP_CACHE_S
1620: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  IZE.# define SQL
1630: 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50  ITE_DEFAULT_TEMP
1640: 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 35 30 30  _CACHE_SIZE  500
1650: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1660: 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1670: 72 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61  r of attached da
1680: 74 61 62 61 73 65 73 2e 20 20 54 68 69 73 20 6d  tabases.  This m
1690: 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 30  ust be between 0
16a0: 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 20 54 68 65  .** and 30.  The
16b0: 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20   upper bound on 
16c0: 33 30 20 69 73 20 62 65 63 61 75 73 65 20 61 20  30 is because a 
16d0: 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 62  32-bit integer b
16e0: 69 74 6d 61 70 0a 2a 2a 20 69 73 20 75 73 65 64  itmap.** is used
16f0: 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 74   internally to t
1700: 72 61 63 6b 20 61 74 74 61 63 68 65 64 20 64 61  rack attached da
1710: 74 61 62 61 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e  tabases..*/.#ifn
1720: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41  def SQLITE_MAX_A
1730: 54 54 41 43 48 45 44 0a 23 20 64 65 66 69 6e 65  TTACHED.# define
1740: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41   SQLITE_MAX_ATTA
1750: 43 48 45 44 20 31 30 0a 23 65 6e 64 69 66 0a 0a  CHED 10.#endif..
1760: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1770: 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 20 3f 6e  um value of a ?n
1780: 6e 6e 20 77 69 6c 64 63 61 72 64 20 74 68 61 74  nn wildcard that
1790: 20 74 68 65 20 70 61 72 73 65 72 20 77 69 6c 6c   the parser will
17a0: 20 61 63 63 65 70 74 2e 0a 2a 2f 0a 23 69 66 6e   accept..*/.#ifn
17b0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  def SQLITE_MAX_V
17c0: 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 0a 23  ARIABLE_NUMBER.#
17d0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
17e0: 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  AX_VARIABLE_NUMB
17f0: 45 52 20 39 39 39 0a 23 65 6e 64 69 66 0a 0a 2f  ER 999.#endif../
1800: 2a 20 4d 61 78 69 6d 75 6d 20 70 61 67 65 20 73  * Maximum page s
1810: 69 7a 65 2e 20 20 54 68 65 20 75 70 70 65 72 20  ize.  The upper 
1820: 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 20 76 61  bound on this va
1830: 6c 75 65 20 69 73 20 33 32 37 36 38 2e 20 20 54  lue is 32768.  T
1840: 68 69 73 20 61 20 6c 69 6d 69 74 0a 2a 2a 20 69  his a limit.** i
1850: 6d 70 6f 73 65 64 20 62 79 20 74 68 65 20 6e 65  mposed by the ne
1860: 63 65 73 73 69 74 79 20 6f 66 20 73 74 6f 72 69  cessity of stori
1870: 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ng the value in 
1880: 61 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  a 2-byte unsigne
1890: 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 6e 64  d integer.** and
18a0: 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74   the fact that t
18b0: 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73  he page size mus
18c0: 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20  t be a power of 
18d0: 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73  2..**.** If this
18e0: 20 6c 69 6d 69 74 20 69 73 20 63 68 61 6e 67 65   limit is change
18f0: 64 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 70  d, then the comp
1900: 69 6c 65 64 20 6c 69 62 72 61 72 79 20 69 73 20  iled library is 
1910: 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 69  technically.** i
1920: 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68  ncompatible with
1930: 20 61 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 61   an SQLite libra
1940: 72 79 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  ry compiled with
1950: 20 61 20 64 69 66 66 65 72 65 6e 74 20 6c 69 6d   a different lim
1960: 69 74 2e 20 49 66 0a 2a 2a 20 61 20 70 72 6f 63  it. If.** a proc
1970: 65 73 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e  ess operating on
1980: 20 61 20 64 61 74 61 62 61 73 65 20 77 69 74 68   a database with
1990: 20 61 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20   a page-size of 
19a0: 36 35 35 33 36 20 62 79 74 65 73 20 0a 2a 2a 20  65536 bytes .** 
19b0: 63 72 61 73 68 65 73 2c 20 74 68 65 6e 20 61 6e  crashes, then an
19c0: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 53 51 4c   instance of SQL
19d0: 69 74 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  ite compiled wit
19e0: 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61  h the default pa
19f0: 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 6c 69 6d 69  ge-size .** limi
1a00: 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  t will not be ab
1a10: 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  le to rollback t
1a20: 68 65 20 61 62 6f 72 74 65 64 20 74 72 61 6e 73  he aborted trans
1a30: 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 63 6f 75  action. This cou
1a40: 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 6f 20 64 61  ld.** lead to da
1a50: 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
1a60: 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  n..*/.#ifndef SQ
1a70: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
1a80: 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ZE.# define SQLI
1a90: 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
1aa0: 20 33 32 37 36 38 0a 23 65 6e 64 69 66 0a 0a 0a   32768.#endif...
1ab0: 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  /*.** The defaul
1ac0: 74 20 73 69 7a 65 20 6f 66 20 61 20 64 61 74 61  t size of a data
1ad0: 62 61 73 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69  base page..*/.#i
1ae0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
1af0: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23  AULT_PAGE_SIZE.#
1b00: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
1b10: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1b20: 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 23 69 66   1024.#endif.#if
1b30: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
1b40: 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c 49 54 45  PAGE_SIZE>SQLITE
1b50: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23  _MAX_PAGE_SIZE.#
1b60: 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45   undef SQLITE_DE
1b70: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a  FAULT_PAGE_SIZE.
1b80: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1b90: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
1ba0: 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  E SQLITE_MAX_PAG
1bb0: 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 2f  E_SIZE.#endif../
1bc0: 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 69 6c 79 2c  *.** Ordinarily,
1bd0: 20 69 66 20 6e 6f 20 76 61 6c 75 65 20 69 73 20   if no value is 
1be0: 65 78 70 6c 69 63 69 74 6c 79 20 70 72 6f 76 69  explicitly provi
1bf0: 64 65 64 2c 20 53 51 4c 69 74 65 20 63 72 65 61  ded, SQLite crea
1c00: 74 65 73 20 64 61 74 61 62 61 73 65 73 0a 2a 2a  tes databases.**
1c10: 20 77 69 74 68 20 70 61 67 65 20 73 69 7a 65 20   with page size 
1c20: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
1c30: 41 47 45 5f 53 49 5a 45 2e 20 48 6f 77 65 76 65  AGE_SIZE. Howeve
1c40: 72 2c 20 62 61 73 65 64 20 6f 6e 20 63 65 72 74  r, based on cert
1c50: 61 69 6e 0a 2a 2a 20 64 65 76 69 63 65 20 63 68  ain.** device ch
1c60: 61 72 61 63 74 65 72 69 73 74 69 63 73 20 28 73  aracteristics (s
1c70: 65 63 74 6f 72 2d 73 69 7a 65 20 61 6e 64 20 61  ector-size and a
1c80: 74 6f 6d 69 63 20 77 72 69 74 65 28 29 20 73 75  tomic write() su
1c90: 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 51 4c 69 74  pport),.** SQLit
1ca0: 65 20 6d 61 79 20 63 68 6f 6f 73 65 20 61 20 6c  e may choose a l
1cb0: 61 72 67 65 72 20 76 61 6c 75 65 2e 20 54 68 69  arger value. Thi
1cc0: 73 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 74 68  s constant is th
1cd0: 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 0a  e maximum value.
1ce0: 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 63  ** SQLite will c
1cf0: 68 6f 6f 73 65 20 6f 6e 20 69 74 73 20 6f 77 6e  hoose on its own
1d00: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1d10: 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f  ITE_MAX_DEFAULT_
1d20: 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69  PAGE_SIZE.# defi
1d30: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45  ne SQLITE_MAX_DE
1d40: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20  FAULT_PAGE_SIZE 
1d50: 38 31 39 32 0a 23 65 6e 64 69 66 0a 23 69 66 20  8192.#endif.#if 
1d60: 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55  SQLITE_MAX_DEFAU
1d70: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 4c  LT_PAGE_SIZE>SQL
1d80: 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
1d90: 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45  E.# undef SQLITE
1da0: 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
1db0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  E_SIZE.# define 
1dc0: 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55  SQLITE_MAX_DEFAU
1dd0: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 53 51 4c  LT_PAGE_SIZE SQL
1de0: 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
1df0: 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  E.#endif.../*.**
1e00: 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   Maximum number 
1e10: 6f 66 20 70 61 67 65 73 20 69 6e 20 6f 6e 65 20  of pages in one 
1e20: 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1e30: 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 72 65 61  *.** This is rea
1e40: 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 64 65 66  lly just the def
1e50: 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
1e60: 68 65 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e  he max_page_coun
1e70: 74 20 70 72 61 67 6d 61 2e 0a 2a 2a 20 54 68 69  t pragma..** Thi
1e80: 73 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6c  s value can be l
1e90: 6f 77 65 72 65 64 20 28 6f 72 20 72 61 69 73 65  owered (or raise
1ea0: 64 29 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75  d) at run-time u
1eb0: 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a 2a 2a  sing that the.**
1ec0: 20 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 20   max_page_count 
1ed0: 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65  macro..*/.#ifnde
1ee0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  f SQLITE_MAX_PAG
1ef0: 45 5f 43 4f 55 4e 54 0a 23 20 64 65 66 69 6e 65  E_COUNT.# define
1f00: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
1f10: 5f 43 4f 55 4e 54 20 31 30 37 33 37 34 31 38 32  _COUNT 107374182
1f20: 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  3.#endif../*.** 
1f30: 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28  Maximum length (
1f40: 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74 68 65  in bytes) of the
1f50: 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49   pattern in a LI
1f60: 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70  KE or GLOB.** op
1f70: 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64  erator..*/.#ifnd
1f80: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  ef SQLITE_MAX_LI
1f90: 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
1fa0: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  H.# define SQLIT
1fb0: 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45  E_MAX_LIKE_PATTE
1fc0: 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a  RN_LENGTH 50000.
1fd0: 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
1fe0: 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73  ******* End of s
1ff0: 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a  qliteLimit.h ***
2000: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2020: 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2030: 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
2040: 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
2050: 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
2060: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
2070: 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73 61  ******/../* Disa
2080: 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61 72  ble nuisance war
2090: 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e 64  nings on Borland
20a0: 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23 69   compilers */.#i
20b0: 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c  f defined(__BORL
20c0: 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61 20  ANDC__).#pragma 
20d0: 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e 72  warn -rch /* unr
20e0: 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a 2f  eachable code */
20f0: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63  .#pragma warn -c
2100: 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20  cc /* Condition 
2110: 69 73 20 61 6c 77 61 79 73 20 74 72 75 65 20 6f  is always true o
2120: 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61 67  r false */.#prag
2130: 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a 20  ma warn -aus /* 
2140: 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65 20 69  Assigned value i
2150: 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a  s never used */.
2160: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 73  #pragma warn -cs
2170: 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20 73  u /* Comparing s
2180: 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 6e  igned and unsign
2190: 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61  ed */.#pragma wa
21a0: 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 70 69  rn -spa /* Suspi
21b0: 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 61 72  cious pointer ar
21c0: 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 6e 64  ithmetic */.#end
21d0: 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 66 6f  if../* Needed fo
21e0: 72 20 76 61 72 69 6f 75 73 20 64 65 66 69 6e 69  r various defini
21f0: 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66 6e  tions... */.#ifn
2200: 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a  def _GNU_SOURCE.
2210: 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f 53 4f  # define _GNU_SO
2220: 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  URCE.#endif../*.
2230: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e 64  ** Include stand
2240: 61 72 64 20 68 65 61 64 65 72 20 66 69 6c 65 73  ard header files
2250: 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a 2f   as necessary.*/
2260: 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54 44  .#ifdef HAVE_STD
2270: 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20 3c  INT_H.#include <
2280: 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69 66  stdint.h>.#endif
2290: 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e 54  .#ifdef HAVE_INT
22a0: 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64 65  TYPES_H.#include
22b0: 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23 65   <inttypes.h>.#e
22c0: 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 20 54 68 69 73  ndif../*. * This
22d0: 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20 74   macro is used t
22e0: 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 75 67  o "hide" some ug
22f0: 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 69 6e  liness in castin
2300: 67 20 61 6e 20 69 6e 74 0a 20 2a 20 76 61 6c 75  g an int. * valu
2310: 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c 75 65  e to a ptr value
2320: 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 43 20   under the MSVC 
2330: 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 72 2e  64-bit compiler.
2340: 20 20 20 43 61 73 74 69 6e 67 0a 20 2a 20 6e 6f     Casting. * no
2350: 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 73 20  n 64-bit values 
2360: 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 65 73  to ptr types res
2370: 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 64 22  ults in a "hard"
2380: 20 65 72 72 6f 72 20 77 69 74 68 20 0a 20 2a 20   error with . * 
2390: 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20  the MSVC 64-bit 
23a0: 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 20 74  compiler which t
23b0: 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 6f 20  his attempts to 
23c0: 61 76 6f 69 64 2e 20 20 0a 20 2a 0a 20 2a 20 41  avoid.  . *. * A
23d0: 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c 65 72   simple compiler
23e0: 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 74 69   pragma or casti
23f0: 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 75 6c  ng sequence coul
2400: 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a 20  d not be found. 
2410: 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 69  * to correct thi
2420: 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 74 69  s in all situati
2430: 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d 61 63  ons, so this mac
2440: 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 63 65  ro was introduce
2450: 64 2e 0a 20 2a 0a 20 2a 20 49 74 20 63 6f 75 6c  d.. *. * It coul
2460: 64 20 62 65 20 61 72 67 75 65 64 20 74 68 61 74  d be argued that
2470: 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 74 79   the intptr_t ty
2480: 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 65 64  pe could be used
2490: 20 69 6e 20 74 68 69 73 0a 20 2a 20 63 61 73 65   in this. * case
24a0: 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 65 20  , but that type 
24b0: 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
24c0: 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72   on all compiler
24d0: 73 2c 20 6f 72 20 0a 20 2a 20 72 65 71 75 69 72  s, or . * requir
24e0: 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20  es the #include 
24f0: 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 61 64  of specific head
2500: 65 72 73 20 77 68 69 63 68 20 64 69 66 66 65 72  ers which differ
2510: 73 20 62 65 74 77 65 65 6e 0a 20 2a 20 70 6c 61  s between. * pla
2520: 74 66 6f 72 6d 73 2e 0a 20 2a 2f 0a 23 64 65 66  tforms.. */.#def
2530: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  ine SQLITE_INT_T
2540: 4f 5f 50 54 52 28 58 29 20 20 20 28 28 76 6f 69  O_PTR(X)   ((voi
2550: 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b 58  d*)&((char*)0)[X
2560: 5d 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  ]).#define SQLIT
2570: 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 20  E_PTR_TO_INT(X) 
2580: 20 20 28 28 69 6e 74 29 28 28 28 63 68 61 72 2a    ((int)(((char*
2590: 29 58 29 2d 28 63 68 61 72 2a 29 30 29 29 0a 0a  )X)-(char*)0))..
25a0: 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66  /*.** These #def
25b0: 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62  ines should enab
25c0: 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70  le >2GB file sup
25d0: 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 69 66  port on POSIX if
25e0: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69   the.** underlyi
25f0: 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ng operating sys
2600: 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e  tem supports it.
2610: 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b    If the OS lack
2620: 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20  s.** large file 
2630: 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 20 74  support, or if t
2640: 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f 77 73  he OS is windows
2650: 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62  , these should b
2660: 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  e no-ops..**.** 
2670: 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 20 54  Ticket #2739:  T
2680: 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  he _LARGEFILE_SO
2690: 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 74 20  URCE macro must 
26a0: 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 61 6e  appear before an
26b0: 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 6e 63  y.** system #inc
26c0: 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c 20 74  ludes.  Hence, t
26d0: 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  his block of cod
26e0: 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 76 65  e must be the ve
26f0: 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f 64 65  ry first.** code
2700: 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 20 66   in all source f
2710: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67  iles..**.** Larg
2720: 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63  e file support c
2730: 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75  an be disabled u
2740: 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54  sing the -DSQLIT
2750: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77  E_DISABLE_LFS sw
2760: 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63  itch.** on the c
2770: 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20  ompiler command 
2780: 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e  line.  This is n
2790: 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20  ecessary if you 
27a0: 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a  are compiling.**
27b0: 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63   on a recent mac
27c0: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 48 61  hine (ex: Red Ha
27d0: 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77  t 7.2) but you w
27e0: 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f  ant your code to
27f0: 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f   work.** on an o
2800: 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78  lder machine (ex
2810: 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 2e 20  : Red Hat 6.0). 
2820: 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20   If you compile 
2830: 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 0a 2a  on Red Hat 7.2.*
2840: 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f  * without this o
2850: 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e  ption, LFS is en
2860: 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64  able.  But LFS d
2870: 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e  oes not exist in
2880: 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69   the kernel.** i
2890: 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c 20 73  n Red Hat 6.0, s
28a0: 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74  o the code won't
28b0: 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66   work.  Hence, f
28c0: 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72  or maximum binar
28d0: 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79  y.** portability
28e0: 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74   you should omit
28f0: 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 6d 69   LFS..**.** Simi
2900: 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f 72 20  lar is true for 
2910: 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 20 69  Mac OS X.  LFS i
2920: 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64  s only supported
2930: 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 20 61   on Mac OS X 9 a
2940: 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69 66  nd later..*/.#if
2950: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41  ndef SQLITE_DISA
2960: 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65  BLE_LFS.# define
2970: 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20   _LARGE_FILE    
2980: 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46     1.# ifndef _F
2990: 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a  ILE_OFFSET_BITS.
29a0: 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45  #   define _FILE
29b0: 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a  _OFFSET_BITS 64.
29c0: 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
29d0: 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52   _LARGEFILE_SOUR
29e0: 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  CE 1.#endif.../*
29f0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54  .** The SQLITE_T
2a00: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20  HREADSAFE macro 
2a10: 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20  must be defined 
2a20: 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31  as either 0 or 1
2a30: 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69  ..** Older versi
2a40: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73  ons of SQLite us
2a50: 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54  ed an optional T
2a60: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e  HREADSAFE macro.
2a70: 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74  .** We support t
2a80: 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a  hat for legacy.*
2a90: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
2aa0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2ab0: 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  ).#if defined(TH
2ac0: 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69  READSAFE).# defi
2ad0: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ne SQLITE_THREAD
2ae0: 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a  SAFE THREADSAFE.
2af0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
2b00: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2b10: 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66   1.#endif.#endif
2b20: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
2b30: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
2b40: 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20  ATUS macro must 
2b50: 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69  be defined as ei
2b60: 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20  ther 0 or 1..** 
2b70: 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  It determines wh
2b80: 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
2b90: 20 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65   features relate
2ba0: 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  d to .** SQLITE_
2bb0: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53  CONFIG_MEMSTATUS
2bc0: 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62   are available b
2bd0: 79 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74  y default or not
2be0: 2e 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e  . This value can
2bf0: 0a 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65  .** be overridde
2c00: 6e 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69  n at runtime usi
2c10: 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  ng the sqlite3_c
2c20: 6f 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a  onfig() API..*/.
2c30: 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2c40: 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53  ITE_DEFAULT_MEMS
2c50: 54 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20  TATUS).# define 
2c60: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d  SQLITE_DEFAULT_M
2c70: 45 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69  EMSTATUS 1.#endi
2c80: 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79  f../*.** Exactly
2c90: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
2ca0: 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73  owing macros mus
2cb0: 74 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20  t be defined in 
2cc0: 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63  order to.** spec
2cd0: 69 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79  ify which memory
2ce0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
2cf0: 79 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a  ystem to use..**
2d00: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53  .**     SQLITE_S
2d10: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20  YSTEM_MALLOC    
2d20: 20 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72        // Use nor
2d30: 6d 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f  mal system mallo
2d40: 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54  c().**     SQLIT
2d50: 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20  E_MEMDEBUG      
2d60: 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75           // Debu
2d70: 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66  gging version of
2d80: 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29   system malloc()
2d90: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d  .**     SQLITE_M
2da0: 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20  EMORY_SIZE      
2db0: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
2dc0: 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a  l allocator #1.*
2dd0: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41  *     SQLITE_MMA
2de0: 50 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20  P_HEAP_SIZE     
2df0: 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20      // internal 
2e00: 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72  mmap() allocator
2e10: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50  .**     SQLITE_P
2e20: 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20  OW2_MEMORY_SIZE 
2e30: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
2e40: 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61  l power-of-two a
2e50: 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49  llocator.**.** I
2e60: 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62  f none of the ab
2e70: 6f 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c  ove are defined,
2e80: 20 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45   then set SQLITE
2e90: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61  _SYSTEM_MALLOC a
2ea0: 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  s.** the default
2eb0: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
2ec0: 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d  (SQLITE_SYSTEM_M
2ed0: 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53  ALLOC)+defined(S
2ee0: 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b  QLITE_MEMDEBUG)+
2ef0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
2f00: 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45  LITE_MEMORY_SIZE
2f10: 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  )+defined(SQLITE
2f20: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29  _MMAP_HEAP_SIZE)
2f30: 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53  +\.    defined(S
2f40: 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52  QLITE_POW2_MEMOR
2f50: 59 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f  Y_SIZE)>1.# erro
2f60: 72 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f  r "At most one o
2f70: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2f80: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e  compile-time con
2f90: 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
2fa0: 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20  ns\. is allows: 
2fb0: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
2fc0: 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d  LLOC, SQLITE_MEM
2fd0: 44 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45  DEBUG, SQLITE_ME
2fe0: 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c  MORY_SIZE,\. SQL
2ff0: 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49  ITE_MMAP_HEAP_SI
3000: 5a 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f  ZE, SQLITE_POW2_
3010: 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e  MEMORY_SIZE".#en
3020: 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28  dif.#if defined(
3030: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3040: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51  LLOC)+defined(SQ
3050: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c  LITE_MEMDEBUG)+\
3060: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
3070: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29  ITE_MEMORY_SIZE)
3080: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  +defined(SQLITE_
3090: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b  MMAP_HEAP_SIZE)+
30a0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
30b0: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59  LITE_POW2_MEMORY
30c0: 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69  _SIZE)==0.# defi
30d0: 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ne SQLITE_SYSTEM
30e0: 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66  _MALLOC 1.#endif
30f0: 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ../*.** If SQLIT
3100: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
3110: 4d 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c  MIT is not zero,
3120: 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65   then try to kee
3130: 70 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f  p the.** sizes o
3140: 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  f memory allocat
3150: 69 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20  ions below this 
3160: 76 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73  value where poss
3170: 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  ible..*/.#if !de
3180: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c  fined(SQLITE_MAL
3190: 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a  LOC_SOFT_LIMIT).
31a0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
31b0: 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49  MALLOC_SOFT_LIMI
31c0: 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f  T 1024.#endif../
31d0: 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20  *.** We need to 
31e0: 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f  define _XOPEN_SO
31f0: 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20  URCE as follows 
3200: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62  in order to enab
3210: 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20  le.** recursive 
3220: 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20  mutexes on most 
3230: 55 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42  Unix systems.  B
3240: 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64  ut Mac OS X is d
3250: 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65  ifferent..** The
3260: 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64   _XOPEN_SOURCE d
3270: 65 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f  efine causes pro
3280: 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53  blems for Mac OS
3290: 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a   X we are told,.
32a0: 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74  ** so it is omit
32b0: 74 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20  ted there.  See 
32c0: 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a  ticket #2673..**
32d0: 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61  .** Later we lea
32e0: 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53  rn that _XOPEN_S
32f0: 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20  OURCE is poorly 
3300: 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a  or incorrectly.*
3310: 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e  * implemented on
3320: 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20   some systems.  
3330: 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69  So we avoid defi
3340: 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a  ning it at all.*
3350: 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61  * if it is alrea
3360: 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66  dy defined or if
3370: 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20   it is unneeded 
3380: 62 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a  because we are.*
3390: 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68  * not doing a th
33a0: 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20  readsafe build. 
33b0: 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a   Ticket #2681..*
33c0: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69  *.** See also ti
33d0: 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23  cket #2741..*/.#
33e0: 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50  if !defined(_XOP
33f0: 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64  EN_SOURCE) && !d
3400: 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f  efined(__DARWIN_
3410: 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f  _) && !defined(_
3420: 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
3430: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23  ITE_THREADSAFE.#
3440: 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f    define _XOPEN_
3450: 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e  SOURCE 500  /* N
3460: 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20  eeded to enable 
3470: 70 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76  pthread recursiv
3480: 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e  e mutexes */.#en
3490: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54  dif../*.** The T
34a0: 43 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f  CL headers are o
34b0: 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20  nly needed when 
34c0: 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43  compiling the TC
34d0: 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23  L bindings..*/.#
34e0: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
34f0: 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65  E_TCL) || define
3500: 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75  d(TCLSH).# inclu
3510: 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69  de <tcl.h>.#endi
3520: 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65  f../*.** Many pe
3530: 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67  ople are failing
3540: 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47   to set -DNDEBUG
3550: 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e  =1 when compilin
3560: 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74  g SQLite..** Set
3570: 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65  ting NDEBUG make
3580: 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c  s the code small
3590: 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65  er and run faste
35a0: 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f  r.  So the follo
35b0: 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72  wing.** lines ar
35c0: 65 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d  e added to autom
35d0: 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45  atically set NDE
35e0: 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d  BUG unless the -
35f0: 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a  DSQLITE_DEBUG=1.
3600: 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74  ** option is set
3610: 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62  .  Thus NDEBUG b
3620: 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e  ecomes an opt-in
3630: 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20   rather than an 
3640: 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75  opt-out.** featu
3650: 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  re..*/.#if !defi
3660: 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21  ned(NDEBUG) && !
3670: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
3680: 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20  EBUG) .# define 
3690: 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a  NDEBUG 1.#endif.
36a0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63  ./*.** The testc
36b0: 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75  ase() macro is u
36c0: 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f  sed to aid in co
36d0: 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20  verage testing. 
36e0: 20 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20   When .** doing 
36f0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
3700: 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20  , the condition 
3710: 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d  inside the argum
3720: 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61  ent to.** testca
3730: 73 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61  se() must be eva
3740: 6c 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65  luated both true
3750: 20 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72   and false in or
3760: 64 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75  der to.** get fu
3770: 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61  ll branch covera
3780: 67 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73  ge.  The testcas
3790: 65 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73  e() macro is ins
37a0: 65 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70  erted.** to help
37b0: 20 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65   ensure adequate
37c0: 20 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69   test coverage i
37d0: 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73  n places where s
37e0: 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69  imple.** conditi
37f0: 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65  on/decision cove
3800: 72 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61  rage is inadequa
3810: 74 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  te.  For example
3820: 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20  , testcase().** 
3830: 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d  can be used to m
3840: 61 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72  ake sure boundar
3850: 79 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73  y values are tes
3860: 74 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74  ted.  For.** bit
3870: 6d 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74  mask tests, test
3880: 63 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73  case() can be us
3890: 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
38a0: 65 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73  each bit.** is s
38b0: 69 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75  ignificant and u
38c0: 73 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63  sed at least onc
38d0: 65 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74  e.  On switch st
38e0: 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72  atements.** wher
38f0: 65 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73  e multiple cases
3900: 20 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20   go to the same 
3910: 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74  block of code, t
3920: 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e  estcase().** can
3930: 20 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c   insure that all
3940: 20 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75   cases are evalu
3950: 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64  ated..**.*/.#ifd
3960: 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41  ef SQLITE_COVERA
3970: 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  GE_TEST.SQLITE_P
3980: 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
3990: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e  lite3Coverage(in
39a0: 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73  t);.# define tes
39b0: 74 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20  tcase(X)  if( X 
39c0: 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61  ){ sqlite3Covera
39d0: 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a  ge(__LINE__); }.
39e0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74  #else.# define t
39f0: 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69  estcase(X).#endi
3a00: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53  f../*.** The TES
3a10: 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75  TONLY macro is u
3a20: 73 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76  sed to enclose v
3a30: 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74  ariable declarat
3a40: 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72  ions or.** other
3a50: 20 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68   bits of code th
3a60: 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f  at are needed to
3a70: 20 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67   support the arg
3a80: 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  uments.** within
3a90: 20 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20   testcase() and 
3aa0: 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e  assert() macros.
3ab0: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
3ac0: 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  (NDEBUG) || defi
3ad0: 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52  ned(SQLITE_COVER
3ae0: 41 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69  AGE_TEST).# defi
3af0: 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20  ne TESTONLY(X)  
3b00: 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  X.#else.# define
3b10: 20 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e   TESTONLY(X).#en
3b20: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74  dif../*.** Somet
3b30: 69 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73  imes we need a s
3b40: 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63  mall amount of c
3b50: 6f 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61  ode such as a va
3b60: 72 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a  riable initializ
3b70: 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75  ation.** to setu
3b80: 70 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73  p for a later as
3b90: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
3ba0: 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e  .  We do not wan
3bb0: 74 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a  t this code to.*
3bc0: 2a 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73  * appear when as
3bd0: 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c  sert() is disabl
3be0: 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ed.  The followi
3bf0: 6e 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72  ng macro is ther
3c00: 65 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f  efore.** used to
3c10: 20 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65   contain that se
3c20: 74 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22  tup code.  The "
3c30: 56 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61  VVA" acronym sta
3c40: 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69  nds for.** "Veri
3c50: 66 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61  fication, Valida
3c60: 74 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64  tion, and Accred
3c70: 69 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74  itation".  In ot
3c80: 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a  her words, the.*
3c90: 2a 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56  * code within VV
3ca0: 41 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e  A_ONLY() will on
3cb0: 6c 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65  ly run during ve
3cc0: 72 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65  rification proce
3cd0: 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  sses..*/.#ifndef
3ce0: 20 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65   NDEBUG.# define
3cf0: 20 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a   VVA_ONLY(X)  X.
3d00: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56  #else.# define V
3d10: 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69  VA_ONLY(X).#endi
3d20: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57  f../*.** The ALW
3d30: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61  AYS and NEVER ma
3d40: 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f  cros surround bo
3d50: 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  olean expression
3d60: 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20  s which .** are 
3d70: 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61  intended to alwa
3d80: 79 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61  ys be true or fa
3d90: 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c  lse, respectivel
3da0: 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72  y.  Such.** expr
3db0: 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65  essions could be
3dc0: 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68   omitted from th
3dd0: 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c  e code completel
3de0: 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20  y.  But they.** 
3df0: 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  are included in 
3e00: 61 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f  a few cases in o
3e10: 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20  rder to enhance 
3e20: 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a  the resilience.*
3e30: 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75  * of SQLite to u
3e40: 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69  nexpected behavi
3e50: 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65  or - to make the
3e60: 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c   code "self-heal
3e70: 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74  ing".** or "duct
3e80: 69 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e  ile" rather than
3e90: 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22   being "brittle"
3ea0: 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74   and crashing at
3eb0: 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69   the first.** hi
3ec0: 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20  nt of unplanned 
3ed0: 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
3ee0: 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
3ef0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52  ALWAYS and NEVER
3f00: 20 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64   are added for d
3f10: 65 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a  efensive code..*
3f20: 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20  *.** When doing 
3f30: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
3f40: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45   ALWAYS and NEVE
3f50: 52 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64  R are hard-coded
3f60: 20 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61   to.** be true a
3f70: 6e 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74  nd false so that
3f80: 20 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65   the unreachable
3f90: 20 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69   code then speci
3fa0: 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62  fy will.** not b
3fb0: 65 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74  e counted as unt
3fc0: 65 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23  ested code..*/.#
3fd0: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
3fe0: 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29  E_COVERAGE_TEST)
3ff0: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53  .# define ALWAYS
4000: 28 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64  (X)      (1).# d
4010: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20  efine NEVER(X)  
4020: 20 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21       (0).#elif !
4030: 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a  defined(NDEBUG).
4040: 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4050: 20 69 6e 74 20 73 71 6c 69 74 65 33 41 73 73 65   int sqlite3Asse
4060: 72 74 28 76 6f 69 64 29 3b 0a 23 20 64 65 66 69  rt(void);.# defi
4070: 6e 65 20 41 4c 57 41 59 53 28 58 29 20 20 20 20  ne ALWAYS(X)    
4080: 20 20 28 28 58 29 3f 31 3a 73 71 6c 69 74 65 33    ((X)?1:sqlite3
4090: 41 73 73 65 72 74 28 29 29 0a 23 20 64 65 66 69  Assert()).# defi
40a0: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20  ne NEVER(X)     
40b0: 20 20 28 28 58 29 3f 73 71 6c 69 74 65 33 41 73    ((X)?sqlite3As
40c0: 73 65 72 74 28 29 3a 30 29 0a 23 65 6c 73 65 0a  sert():0).#else.
40d0: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28  # define ALWAYS(
40e0: 58 29 20 20 20 20 20 20 28 58 29 0a 23 20 64 65  X)      (X).# de
40f0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20  fine NEVER(X)   
4100: 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66 0a 0a      (X).#endif..
4110: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20  /*.** The macro 
4120: 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 20  unlikely() is a 
4130: 68 69 6e 74 20 74 68 61 74 20 73 75 72 72 6f 75  hint that surrou
4140: 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a 2a  nds a boolean.**
4150: 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
4160: 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c 73   is usually fals
4170: 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79  e.  Macro likely
4180: 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a 20  () surrounds.** 
4190: 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73  a boolean expres
41a0: 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75  sion that is usu
41b0: 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43 20  ally true.  GCC 
41c0: 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 73  is able to.** us
41d0: 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 74 6f  e these hints to
41e0: 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 65 72   generate better
41f0: 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65 73   code, sometimes
4200: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
4210: 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30 0a  (__GNUC__) && 0.
4220: 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28  # define likely(
4230: 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e 5f  X)    __builtin_
4240: 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a 23 20  expect((X),1).# 
4250: 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28  define unlikely(
4260: 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78  X)  __builtin_ex
4270: 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65 6c 73  pect((X),0).#els
4280: 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c  e.# define likel
4290: 79 28 58 29 20 20 20 20 21 21 28 58 29 0a 23 20  y(X)    !!(X).# 
42a0: 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28  define unlikely(
42b0: 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64 69 66  X)  !!(X).#endif
42c0: 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
42d0: 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65  * Include sqlite
42e0: 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  3.h in the middl
42f0: 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
4300: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4310: 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4320: 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c  * Begin file sql
4330: 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ite3.h *********
4340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4360: 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
4370: 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
4380: 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
4390: 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
43a0: 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
43b0: 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
43c0: 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
43d0: 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
43e0: 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
43f0: 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
4400: 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
4410: 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
4420: 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
4430: 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
4440: 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
4450: 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
4460: 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
4470: 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
4480: 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
4490: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
44d0: 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
44e0: 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66   header file def
44f0: 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  ines the interfa
4500: 63 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 69  ce that the SQLi
4510: 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70 72  te library.** pr
4520: 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e 74  esents to client
4530: 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20 61   programs.  If a
4540: 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74 72   C-function, str
4550: 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 70 65  ucture, datatype
4560: 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e 74  ,.** or constant
4570: 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65 73   definition does
4580: 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 74   not appear in t
4590: 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 69  his file, then i
45a0: 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 75  t is.** not a pu
45b0: 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20 53  blished API of S
45c0: 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65 63  QLite, is subjec
45d0: 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68  t to change with
45e0: 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20 61  out.** notice, a
45f0: 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  nd should not be
4600: 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 70   referenced by p
4610: 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73 65  rograms that use
4620: 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53   SQLite..**.** S
4630: 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69 6e  ome of the defin
4640: 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20  itions that are 
4650: 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72 65  in this file are
4660: 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22 65   marked as.** "e
4670: 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20 45  xperimental".  E
4680: 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65  xperimental inte
4690: 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d 61  rfaces are norma
46a0: 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74 75  lly new.** featu
46b0: 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64 64  res recently add
46c0: 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20 57  ed to SQLite.  W
46d0: 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69 70  e do not anticip
46e0: 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74  ate changes.** t
46f0: 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 69  o experimental i
4700: 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72 65  nterfaces but re
4710: 73 65 72 76 65 20 74 6f 20 6d 61 6b 65 20 6d 69  serve to make mi
4720: 6e 6f 72 20 63 68 61 6e 67 65 73 20 69 66 0a 2a  nor changes if.*
4730: 2a 20 65 78 70 65 72 69 65 6e 63 65 20 66 72 6f  * experience fro
4740: 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20 77 69  m use "in the wi
4750: 6c 64 22 20 73 75 67 67 65 73 74 20 73 75 63 68  ld" suggest such
4760: 20 63 68 61 6e 67 65 73 20 61 72 65 20 70 72 75   changes are pru
4770: 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  dent..**.** The 
4780: 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67 75  official C-langu
4790: 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65 6e 74  age API document
47a0: 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74 65  ation for SQLite
47b0: 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66   is derived.** f
47c0: 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20  rom comments in 
47d0: 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68 69 73  this file.  This
47e0: 20 66 69 6c 65 20 69 73 20 74 68 65 20 61 75 74   file is the aut
47f0: 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75 72 63  horitative sourc
4800: 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c 69  e.** on how SQLi
4810: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  te interfaces ar
4820: 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f 70 65  e suppose to ope
4830: 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rate..**.** The 
4840: 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 69 6c  name of this fil
4850: 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67 75 72  e under configur
4860: 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74  ation management
4870: 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e 69 6e   is "sqlite.h.in
4880: 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69  "..** The makefi
4890: 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20 6d 69  le makes some mi
48a0: 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74  nor changes to t
48b0: 68 69 73 20 66 69 6c 65 20 28 73 75 63 68 20 61  his file (such a
48c0: 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a 20 74  s inserting.** t
48d0: 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  he version numbe
48e0: 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73 20 69  r) and changes i
48f0: 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71 6c 69  ts name to "sqli
4900: 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70 61 72  te3.h" as.** par
4910: 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64 20 70  t of the build p
4920: 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28  rocess..**.** @(
4930: 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65 2e 68  #) $Id: sqlite.h
4940: 2e 69 6e 2c 76 20 31 2e 34 34 37 20 32 30 30 39  .in,v 1.447 2009
4950: 2f 30 34 2f 33 30 20 31 35 3a 35 39 3a 35 36 20  /04/30 15:59:56 
4960: 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  drh Exp $.*/.#if
4970: 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 5f 48 5f  ndef _SQLITE3_H_
4980: 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
4990: 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 20 3c 73  3_H_.#include <s
49a0: 74 64 61 72 67 2e 68 3e 20 20 20 20 20 2f 2a 20  tdarg.h>     /* 
49b0: 4e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64  Needed for the d
49c0: 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 76 61 5f  efinition of va_
49d0: 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d  list */../*.** M
49e0: 61 6b 65 20 73 75 72 65 20 77 65 20 63 61 6e 20  ake sure we can 
49f0: 63 61 6c 6c 20 74 68 69 73 20 73 74 75 66 66 20  call this stuff 
4a00: 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 66  from C++..*/.#if
4a10: 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a   0.extern "C" {.
4a20: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41  #endif.../*.** A
4a30: 64 64 20 74 68 65 20 61 62 69 6c 69 74 79 20 74  dd the ability t
4a40: 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78 74 65  o override 'exte
4a50: 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  rn'.*/.#ifndef S
4a60: 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a 23 20 64  QLITE_EXTERN.# d
4a70: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54  efine SQLITE_EXT
4a80: 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 6e 64 69  ERN extern.#endi
4a90: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6e  f../*.** These n
4aa0: 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 72 65 20  o-op macros are 
4ab0: 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f 66  used in front of
4ac0: 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f 20 6d   interfaces to m
4ad0: 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 6e 74  ark those.** int
4ae0: 65 72 66 61 63 65 73 20 61 73 20 65 69 74 68 65  erfaces as eithe
4af0: 72 20 64 65 70 72 65 63 61 74 65 64 20 6f 72 20  r deprecated or 
4b00: 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 4e  experimental.  N
4b10: 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  ew applications.
4b20: 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73  ** should not us
4b30: 65 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74  e deprecated int
4b40: 72 66 61 63 65 73 20 2d 20 74 68 65 79 20 61 72  rfaces - they ar
4b50: 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62 61  e support for ba
4b60: 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61  ckwards.** compa
4b70: 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 20 20  tibility only.  
4b80: 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 69 74  Application writ
4b90: 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 61 77  ers should be aw
4ba0: 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 70 65  are that.** expe
4bb0: 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61  rimental interfa
4bc0: 63 65 73 20 61 72 65 20 73 75 62 6a 65 63 74 20  ces are subject 
4bd0: 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 6f 69  to change in poi
4be0: 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a  nt releases..**.
4bf0: 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20  ** These macros 
4c00: 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20  used to resolve 
4c10: 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e 64 73  to various kinds
4c20: 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d 61 67   of compiler mag
4c30: 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 6c 64  ic that.** would
4c40: 20 67 65 6e 65 72 61 74 65 20 77 61 72 6e 69 6e   generate warnin
4c50: 67 20 6d 65 73 73 61 67 65 73 20 77 68 65 6e 20  g messages when 
4c60: 74 68 65 79 20 77 65 72 65 20 75 73 65 64 2e 20  they were used. 
4c70: 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 6f 6d   But that.** com
4c80: 70 69 6c 65 72 20 6d 61 67 69 63 20 65 6e 64 65  piler magic ende
4c90: 64 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67 20  d up generating 
4ca0: 73 75 63 68 20 61 20 66 6c 75 72 72 79 20 6f 66  such a flurry of
4cb0: 20 62 75 67 20 72 65 70 6f 72 74 73 0a 2a 2a 20   bug reports.** 
4cc0: 74 68 61 74 20 77 65 20 68 61 76 65 20 74 61 6b  that we have tak
4cd0: 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 61 6e  en it all out an
4ce0: 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f 20 75  d gone back to u
4cf0: 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a 20 6e  sing simple.** n
4d00: 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23  oop macros..*/.#
4d10: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
4d20: 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
4d30: 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45   SQLITE_EXPERIME
4d40: 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 75  NTAL../*.** Ensu
4d50: 72 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c 73  re these symbols
4d60: 20 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e 65   were not define
4d70: 64 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69 6f  d by some previo
4d80: 75 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a  us header file..
4d90: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
4da0: 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 65 66  _VERSION.# undef
4db0: 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 0a   SQLITE_VERSION.
4dc0: 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
4dd0: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d  LITE_VERSION_NUM
4de0: 42 45 52 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  BER.# undef SQLI
4df0: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45  TE_VERSION_NUMBE
4e00: 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  R.#endif../*.** 
4e10: 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c  CAPI3REF: Compil
4e20: 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56  e-Time Library V
4e30: 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b  ersion Numbers {
4e40: 48 31 30 30 31 30 7d 20 3c 53 36 30 31 30 30 3e  H10010} <S60100>
4e50: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  .**.** The SQLIT
4e60: 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 53 51  E_VERSION and SQ
4e70: 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d  LITE_VERSION_NUM
4e80: 42 45 52 20 23 64 65 66 69 6e 65 73 20 69 6e 0a  BER #defines in.
4e90: 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 2e 68  ** the sqlite3.h
4ea0: 20 66 69 6c 65 20 73 70 65 63 69 66 79 20 74 68   file specify th
4eb0: 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c  e version of SQL
4ec0: 69 74 65 20 77 69 74 68 20 77 68 69 63 68 0a 2a  ite with which.*
4ed0: 2a 20 74 68 61 74 20 68 65 61 64 65 72 20 66 69  * that header fi
4ee0: 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64  le is associated
4ef0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 65 72  ..**.** The "ver
4f00: 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 65 20  sion" of SQLite 
4f10: 69 73 20 61 20 73 74 72 69 6e 67 20 6f 66 20 74  is a string of t
4f20: 68 65 20 66 6f 72 6d 20 22 58 2e 59 2e 5a 22 2e  he form "X.Y.Z".
4f30: 0a 2a 2a 20 54 68 65 20 70 68 72 61 73 65 20 22  .** The phrase "
4f40: 61 6c 70 68 61 22 20 6f 72 20 22 62 65 74 61 22  alpha" or "beta"
4f50: 20 6d 69 67 68 74 20 62 65 20 61 70 70 65 6e 64   might be append
4f60: 65 64 20 61 66 74 65 72 20 74 68 65 20 5a 2e 0a  ed after the Z..
4f70: 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 65 20 69  ** The X value i
4f80: 73 20 6d 61 6a 6f 72 20 76 65 72 73 69 6f 6e 20  s major version 
4f90: 6e 75 6d 62 65 72 20 61 6c 77 61 79 73 20 33 20  number always 3 
4fa0: 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54  in SQLite3..** T
4fb0: 68 65 20 58 20 76 61 6c 75 65 20 6f 6e 6c 79 20  he X value only 
4fc0: 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63  changes when bac
4fd0: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
4fe0: 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65  lity is.** broke
4ff0: 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20  n and we intend 
5000: 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62  to never break b
5010: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
5020: 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 59  bility..** The Y
5030: 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69   value is the mi
5040: 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  nor version numb
5050: 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e  er and only chan
5060: 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72  ges when.** ther
5070: 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74  e are major feat
5080: 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73  ure enhancements
5090: 20 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72   that are forwar
50a0: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a  ds compatible.**
50b0: 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72   but not backwar
50c0: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a  ds compatible..*
50d0: 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 20 69 73  * The Z value is
50e0: 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d   the release num
50f0: 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65  ber and is incre
5100: 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65  mented with.** e
5110: 61 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20  ach release but 
5120: 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30  resets back to 0
5130: 20 77 68 65 6e 65 76 65 72 20 59 20 69 73 20 69   whenever Y is i
5140: 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a  ncremented..**.*
5150: 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
5160: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28  ite3_libversion(
5170: 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
5180: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
5190: 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  r()]..**.** Requ
51a0: 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 30 31  irements: [H1001
51b0: 31 5d 20 5b 48 31 30 30 31 34 5d 0a 2a 2f 0a 23  1] [H10014].*/.#
51c0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 45  define SQLITE_VE
51d0: 52 53 49 4f 4e 20 20 20 20 20 20 20 20 20 22 33  RSION         "3
51e0: 2e 36 2e 31 34 22 0a 23 64 65 66 69 6e 65 20 53  .6.14".#define S
51f0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
5200: 4d 42 45 52 20 20 33 30 30 36 30 31 34 0a 0a 2f  MBER  3006014../
5210: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
5220: 75 6e 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20  un-Time Library 
5230: 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20  Version Numbers 
5240: 7b 48 31 30 30 32 30 7d 20 3c 53 36 30 31 30 30  {H10020} <S60100
5250: 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73  >.** KEYWORDS: s
5260: 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 0a 2a  qlite3_version.*
5270: 2a 0a 2a 2a 20 54 68 65 73 65 20 66 65 61 74 75  *.** These featu
5280: 72 65 73 20 70 72 6f 76 69 64 65 20 74 68 65 20  res provide the 
5290: 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  same information
52a0: 20 61 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f   as the [SQLITE_
52b0: 56 45 52 53 49 4f 4e 5d 0a 2a 2a 20 61 6e 64 20  VERSION].** and 
52c0: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f  [SQLITE_VERSION_
52d0: 4e 55 4d 42 45 52 5d 20 23 64 65 66 69 6e 65 73  NUMBER] #defines
52e0: 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2c 20   in the header, 
52f0: 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 74  but are associat
5300: 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6c  ed.** with the l
5310: 69 62 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f  ibrary instead o
5320: 66 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c  f the header fil
5330: 65 2e 20 20 43 61 75 74 69 6f 75 73 20 70 72 6f  e.  Cautious pro
5340: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 0a 2a  grammers might.*
5350: 2a 20 69 6e 63 6c 75 64 65 20 61 20 63 68 65 63  * include a chec
5360: 6b 20 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69  k in their appli
5370: 63 61 74 69 6f 6e 20 74 6f 20 76 65 72 69 66 79  cation to verify
5380: 20 74 68 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33   that.** sqlite3
5390: 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
53a0: 65 72 28 29 20 61 6c 77 61 79 73 20 72 65 74 75  er() always retu
53b0: 72 6e 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a  rns the value.**
53c0: 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e   [SQLITE_VERSION
53d0: 5f 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20  _NUMBER]..**.** 
53e0: 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  The sqlite3_libv
53f0: 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f  ersion() functio
5400: 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61  n returns the sa
5410: 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  me information a
5420: 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73  s is.** in the s
5430: 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d  qlite3_version[]
5440: 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74   string constant
5450: 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  .  The function 
5460: 69 73 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66  is provided.** f
5470: 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c 73 20 73  or use in DLLs s
5480: 69 6e 63 65 20 44 4c 4c 20 75 73 65 72 73 20 75  ince DLL users u
5490: 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 68 61  sually do not ha
54a0: 76 65 20 64 69 72 65 63 74 20 61 63 63 65 73 73  ve direct access
54b0: 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f   to string.** co
54c0: 6e 73 74 61 6e 74 73 20 77 69 74 68 69 6e 20 74  nstants within t
54d0: 68 65 20 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65  he DLL..**.** Re
54e0: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30  quirements: [H10
54f0: 30 32 31 5d 20 5b 48 31 30 30 32 32 5d 20 5b 48  021] [H10022] [H
5500: 31 30 30 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  10023].*/.SQLITE
5510: 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
5520: 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b  sqlite3_version[
5530: 5d 20 3d 20 53 51 4c 49 54 45 5f 56 45 52 53 49  ] = SQLITE_VERSI
5540: 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  ON;.SQLITE_API c
5550: 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
5560: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f  e3_libversion(vo
5570: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
5580: 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  int sqlite3_libv
5590: 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f  ersion_number(vo
55a0: 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  id);../*.** CAPI
55b0: 33 52 45 46 3a 20 54 65 73 74 20 54 6f 20 53 65  3REF: Test To Se
55c0: 65 20 49 66 20 54 68 65 20 4c 69 62 72 61 72 79  e If The Library
55d0: 20 49 73 20 54 68 72 65 61 64 73 61 66 65 20 7b   Is Threadsafe {
55e0: 48 31 30 31 30 30 7d 20 3c 53 36 30 31 30 30 3e  H10100} <S60100>
55f0: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 61  .**.** SQLite ca
5600: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  n be compiled wi
5610: 74 68 20 6f 72 20 77 69 74 68 6f 75 74 20 6d 75  th or without mu
5620: 74 65 78 65 73 2e 20 20 57 68 65 6e 0a 2a 2a 20  texes.  When.** 
5630: 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 45  the [SQLITE_THRE
5640: 41 44 53 41 46 45 5d 20 43 20 70 72 65 70 72 6f  ADSAFE] C prepro
5650: 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 31 20 6f  cessor macro 1 o
5660: 72 20 32 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20  r 2, mutexes.** 
5670: 61 72 65 20 65 6e 61 62 6c 65 64 20 61 6e 64 20  are enabled and 
5680: 53 51 4c 69 74 65 20 69 73 20 74 68 72 65 61 64  SQLite is thread
5690: 73 61 66 65 2e 20 20 57 68 65 6e 20 74 68 65 0a  safe.  When the.
56a0: 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41  ** [SQLITE_THREA
56b0: 44 53 41 46 45 5d 20 6d 61 63 72 6f 20 69 73 20  DSAFE] macro is 
56c0: 30 2c 20 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78  0, .** the mutex
56d0: 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64 2e 20  es are omitted. 
56e0: 20 57 69 74 68 6f 75 74 20 74 68 65 20 6d 75 74   Without the mut
56f0: 65 78 65 73 2c 20 69 74 20 69 73 20 6e 6f 74 20  exes, it is not 
5700: 73 61 66 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53  safe.** to use S
5710: 51 4c 69 74 65 20 63 6f 6e 63 75 72 72 65 6e 74  QLite concurrent
5720: 6c 79 20 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61  ly from more tha
5730: 6e 20 6f 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a  n one thread..**
5740: 0a 2a 2a 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74  .** Enabling mut
5750: 65 78 65 73 20 69 6e 63 75 72 73 20 61 20 6d 65  exes incurs a me
5760: 61 73 75 72 61 62 6c 65 20 70 65 72 66 6f 72 6d  asurable perform
5770: 61 6e 63 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a  ance penalty..**
5780: 20 53 6f 20 69 66 20 73 70 65 65 64 20 69 73 20   So if speed is 
5790: 6f 66 20 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74  of utmost import
57a0: 61 6e 63 65 2c 20 69 74 20 6d 61 6b 65 73 20 73  ance, it makes s
57b0: 65 6e 73 65 20 74 6f 20 64 69 73 61 62 6c 65 0a  ense to disable.
57c0: 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 2e 20  ** the mutexes. 
57d0: 20 42 75 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d   But for maximum
57e0: 20 73 61 66 65 74 79 2c 20 6d 75 74 65 78 65 73   safety, mutexes
57f0: 20 73 68 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c   should be enabl
5800: 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  ed..** The defau
5810: 6c 74 20 62 65 68 61 76 69 6f 72 20 69 73 20 66  lt behavior is f
5820: 6f 72 20 6d 75 74 65 78 65 73 20 74 6f 20 62 65  or mutexes to be
5830: 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20   enabled..**.** 
5840: 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 63  This interface c
5850: 61 6e 20 62 65 20 75 73 65 64 20 62 79 20 61 20  an be used by a 
5860: 70 72 6f 67 72 61 6d 20 74 6f 20 6d 61 6b 65 20  program to make 
5870: 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a  sure that the.**
5880: 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69   version of SQLi
5890: 74 65 20 74 68 61 74 20 69 74 20 69 73 20 6c 69  te that it is li
58a0: 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 20 77 61  nking against wa
58b0: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a  s compiled with.
58c0: 2a 2a 20 74 68 65 20 64 65 73 69 72 65 64 20 73  ** the desired s
58d0: 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 5b 53  etting of the [S
58e0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
58f0: 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54  ] macro..**.** T
5900: 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 6f 6e  his interface on
5910: 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e 20 74 68  ly reports on th
5920: 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6d  e compile-time m
5930: 75 74 65 78 20 73 65 74 74 69 6e 67 0a 2a 2a 20  utex setting.** 
5940: 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54  of the [SQLITE_T
5950: 48 52 45 41 44 53 41 46 45 5d 20 66 6c 61 67 2e  HREADSAFE] flag.
5960: 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63    If SQLite is c
5970: 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20  ompiled with.** 
5980: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
5990: 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 78 65 73  E=1 then mutexes
59a0: 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62 79 20   are enabled by 
59b0: 64 65 66 61 75 6c 74 20 62 75 74 0a 2a 2a 20 63  default but.** c
59c0: 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f 72 20 70  an be fully or p
59d0: 61 72 74 69 61 6c 6c 79 20 64 69 73 61 62 6c 65  artially disable
59e0: 64 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74  d using a call t
59f0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
5a00: 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 74 68 65  g()].** with the
5a10: 20 76 65 72 62 73 20 5b 53 51 4c 49 54 45 5f 43   verbs [SQLITE_C
5a20: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
5a30: 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  AD], [SQLITE_CON
5a40: 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d  FIG_MULTITHREAD]
5a50: 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ,.** or [SQLITE_
5a60: 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2e 20 20  CONFIG_MUTEX].  
5a70: 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
5a80: 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
5a90: 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e 6c 79 20  n shows.** only 
5aa0: 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6d 70  the default comp
5ab0: 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69 6e 67  ile-time setting
5ac0: 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e 2d 74 69  , not any run-ti
5ad0: 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 6f  me changes.** to
5ae0: 20 74 68 61 74 20 73 65 74 74 69 6e 67 2e 0a 2a   that setting..*
5af0: 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68  *.** See the [th
5b00: 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f  reading mode] do
5b10: 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  cumentation for 
5b20: 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
5b30: 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65  mation..**.** Re
5b40: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30  quirements: [H10
5b50: 31 30 31 5d 20 5b 48 31 30 31 30 32 5d 0a 2a 2f  101] [H10102].*/
5b60: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
5b70: 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61  sqlite3_threadsa
5b80: 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a  fe(void);../*.**
5b90: 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62   CAPI3REF: Datab
5ba0: 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 48  ase Connection H
5bb0: 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 7d 20 3c  andle {H12000} <
5bc0: 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S40200>.** KEYWO
5bd0: 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65 20 63  RDS: {database c
5be0: 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61 74 61  onnection} {data
5bf0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
5c00: 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f 70 65  }.**.** Each ope
5c10: 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  n SQLite databas
5c20: 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  e is represented
5c30: 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   by a pointer to
5c40: 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a   an instance of.
5c50: 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20 73 74  ** the opaque st
5c60: 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 73  ructure named "s
5c70: 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69 73 20  qlite3".  It is 
5c80: 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e 6b 20  useful to think 
5c90: 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a 2a 2a  of an sqlite3.**
5ca0: 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e 20 6f   pointer as an o
5cb0: 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73 71 6c  bject.  The [sql
5cc0: 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73  ite3_open()], [s
5cd0: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
5ce0: 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  , and.** [sqlite
5cf0: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74  3_open_v2()] int
5d00: 65 72 66 61 63 65 73 20 61 72 65 20 69 74 73 20  erfaces are its 
5d10: 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20 61 6e  constructors, an
5d20: 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  d [sqlite3_close
5d30: 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 20 64 65  ()].** is its de
5d40: 73 74 72 75 63 74 6f 72 2e 20 20 54 68 65 72 65  structor.  There
5d50: 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 65 72 20   are many other 
5d60: 69 6e 74 65 72 66 61 63 65 73 20 28 73 75 63 68  interfaces (such
5d70: 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   as.** [sqlite3_
5d80: 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b  prepare_v2()], [
5d90: 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
5da0: 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e 64 0a  unction()], and.
5db0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  ** [sqlite3_busy
5dc0: 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f 20 6e  _timeout()] to n
5dd0: 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 20 74  ame but three) t
5de0: 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 73 20  hat are methods 
5df0: 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  on an.** sqlite3
5e00: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65   object..*/.type
5e10: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
5e20: 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f 2a 0a  e3 sqlite3;../*.
5e30: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 34 2d  ** CAPI3REF: 64-
5e40: 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 70 65  Bit Integer Type
5e50: 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 31 30 31  s {H10200} <S101
5e60: 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  10>.** KEYWORDS:
5e70: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71   sqlite_int64 sq
5e80: 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a 2a 0a 2a  lite_uint64.**.*
5e90: 2a 20 42 65 63 61 75 73 65 20 74 68 65 72 65 20  * Because there 
5ea0: 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c 61 74  is no cross-plat
5eb0: 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70 65 63  form way to spec
5ec0: 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74 65 67  ify 64-bit integ
5ed0: 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 51 4c 69  er types.** SQLi
5ee0: 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79 70 65  te includes type
5ef0: 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69 74 20  defs for 64-bit 
5f00: 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67  signed and unsig
5f10: 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a  ned integers..**
5f20: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
5f30: 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65  int64 and sqlite
5f40: 33 5f 75 69 6e 74 36 34 20 61 72 65 20 74 68 65  3_uint64 are the
5f50: 20 70 72 65 66 65 72 72 65 64 20 74 79 70 65 20   preferred type 
5f60: 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 20  definitions..** 
5f70: 54 68 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  The sqlite_int64
5f80: 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 69 6e 74   and sqlite_uint
5f90: 36 34 20 74 79 70 65 73 20 61 72 65 20 73 75 70  64 types are sup
5fa0: 70 6f 72 74 65 64 20 66 6f 72 20 62 61 63 6b 77  ported for backw
5fb0: 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62  ards.** compatib
5fc0: 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a  ility only..**.*
5fd0: 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
5fe0: 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 32 30 32  [H10201] [H10202
5ff0: 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ].*/.#ifdef SQLI
6000: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20 20  TE_INT64_TYPE.  
6010: 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f 49  typedef SQLITE_I
6020: 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65  NT64_TYPE sqlite
6030: 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65  _int64;.  typede
6040: 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49 54  f unsigned SQLIT
6050: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c  E_INT64_TYPE sql
6060: 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 69  ite_uint64;.#eli
6070: 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56  f defined(_MSC_V
6080: 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  ER) || defined(_
6090: 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20 74  _BORLANDC__).  t
60a0: 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20 73  ypedef __int64 s
60b0: 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74  qlite_int64;.  t
60c0: 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20  ypedef unsigned 
60d0: 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75  __int64 sqlite_u
60e0: 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20 74  int64;.#else.  t
60f0: 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67  ypedef long long
6100: 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74 36   int sqlite_int6
6110: 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73  4;.  typedef uns
6120: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
6130: 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  int sqlite_uint6
6140: 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65  4;.#endif.typede
6150: 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73  f sqlite_int64 s
6160: 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74 79  qlite3_int64;.ty
6170: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e  pedef sqlite_uin
6180: 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e 74  t64 sqlite3_uint
6190: 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f  64;../*.** If co
61a0: 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72  mpiling for a pr
61b0: 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63  ocessor that lac
61c0: 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  ks floating poin
61d0: 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75  t support,.** su
61e0: 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72  bstitute integer
61f0: 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f   for floating-po
6200: 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  int..*/.#ifdef S
6210: 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
6220: 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69  ING_POINT.# defi
6230: 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  ne double sqlite
6240: 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a 0a  3_int64.#endif..
6250: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
6260: 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 62 61  Closing A Databa
6270: 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48  se Connection {H
6280: 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 3e 3c  12010} <S30100><
6290: 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S40200>.**.** Th
62a0: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
62b0: 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  e destructor for
62c0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f   the [sqlite3] o
62d0: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 70 70  bject..**.** App
62e0: 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64  lications should
62f0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
6300: 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61  ze | finalize] a
6310: 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ll [prepared sta
6320: 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 20  tements].** and 
6330: 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c  [sqlite3_blob_cl
6340: 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c  ose | close] all
6350: 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d 20   [BLOB handles] 
6360: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
6370: 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d  ** the [sqlite3]
6380: 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74 6f   object prior to
6390: 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 63   attempting to c
63a0: 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74 2e  lose the object.
63b0: 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
63c0: 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d 20 69 6e  _next_stmt()] in
63d0: 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75  terface can be u
63e0: 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 61 6c  sed to locate al
63f0: 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73  l.** [prepared s
6400: 74 61 74 65 6d 65 6e 74 73 5d 20 61 73 73 6f 63  tatements] assoc
6410: 69 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61  iated with a [da
6420: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
6430: 6e 5d 20 69 66 20 64 65 73 69 72 65 64 2e 0a 2a  n] if desired..*
6440: 2a 20 54 79 70 69 63 61 6c 20 63 6f 64 65 20 6d  * Typical code m
6450: 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74  ight look like t
6460: 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63  his:.**.** <bloc
6470: 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20  kquote><pre>.** 
6480: 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
6490: 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c 65 28 20 28  tmt;.** while( (
64a0: 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 5f  pStmt = sqlite3_
64b0: 6e 65 78 74 5f 73 74 6d 74 28 64 62 2c 20 30 29  next_stmt(db, 0)
64c0: 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 6e 62 73 70  )!=0 ){.** &nbsp
64d0: 3b 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  ;   sqlite3_fina
64e0: 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 2a 2a 20  lize(pStmt);.** 
64f0: 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  }.** </pre></blo
6500: 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 49  ckquote>.**.** I
6510: 66 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  f [sqlite3_close
6520: 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 77  ()] is invoked w
6530: 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63 74 69  hile a transacti
6540: 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a 2a 20 74  on is open,.** t
6550: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
6560: 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
6570: 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
6580: 2a 2a 20 54 68 65 20 43 20 70 61 72 61 6d 65 74  ** The C paramet
6590: 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  er to [sqlite3_c
65a0: 6c 6f 73 65 28 43 29 5d 20 6d 75 73 74 20 62 65  lose(C)] must be
65b0: 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 0a 2a   either a NULL.*
65c0: 2a 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 6e 20  * pointer or an 
65d0: 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74  [sqlite3] object
65e0: 20 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 6e 65   pointer obtaine
65f0: 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74  d.** from [sqlit
6600: 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c  e3_open()], [sql
6610: 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20  ite3_open16()], 
6620: 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f  or.** [sqlite3_o
6630: 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 6e  pen_v2()], and n
6640: 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c  ot previously cl
6650: 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  osed..**.** Requ
6660: 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
6670: 32 30 31 31 5d 20 5b 48 31 32 30 31 32 5d 20 5b  2011] [H12012] [
6680: 48 31 32 30 31 33 5d 20 5b 48 31 32 30 31 34 5d  H12013] [H12014]
6690: 20 5b 48 31 32 30 31 35 5d 20 5b 48 31 32 30 31   [H12015] [H1201
66a0: 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  9].*/.SQLITE_API
66b0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 6f   int sqlite3_clo
66c0: 73 65 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 0a  se(sqlite3 *);..
66d0: 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 66  /*.** The type f
66e0: 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75  or a callback fu
66f0: 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20  nction..** This 
6700: 69 73 20 6c 65 67 61 63 79 20 61 6e 64 20 64 65  is legacy and de
6710: 70 72 65 63 61 74 65 64 2e 20 20 49 74 20 69 73  precated.  It is
6720: 20 69 6e 63 6c 75 64 65 64 20 66 6f 72 20 68 69   included for hi
6730: 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 63 6f 6d 70  storical.** comp
6740: 61 74 69 62 69 6c 69 74 79 20 61 6e 64 20 69 73  atibility and is
6750: 20 6e 6f 74 20 64 6f 63 75 6d 65 6e 74 65 64 2e   not documented.
6760: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 69 6e 74 20  .*/.typedef int 
6770: 28 2a 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61  (*sqlite3_callba
6780: 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68  ck)(void*,int,ch
6790: 61 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b 0a 0a  ar**, char**);..
67a0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
67b0: 4f 6e 65 2d 53 74 65 70 20 51 75 65 72 79 20 45  One-Step Query E
67c0: 78 65 63 75 74 69 6f 6e 20 49 6e 74 65 72 66 61  xecution Interfa
67d0: 63 65 20 7b 48 31 32 31 30 30 7d 20 3c 53 31 30  ce {H12100} <S10
67e0: 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  000>.**.** The s
67f0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e  qlite3_exec() in
6800: 74 65 72 66 61 63 65 20 69 73 20 61 20 63 6f 6e  terface is a con
6810: 76 65 6e 69 65 6e 74 20 77 61 79 20 6f 66 20 72  venient way of r
6820: 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f 72 20 6d 6f  unning one or mo
6830: 72 65 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d  re.** SQL statem
6840: 65 6e 74 73 20 77 69 74 68 6f 75 74 20 68 61 76  ents without hav
6850: 69 6e 67 20 74 6f 20 77 72 69 74 65 20 61 20 6c  ing to write a l
6860: 6f 74 20 6f 66 20 43 20 63 6f 64 65 2e 20 20 54  ot of C code.  T
6870: 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  he UTF-8 encoded
6880: 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  .** SQL statemen
6890: 74 73 20 61 72 65 20 70 61 73 73 65 64 20 69 6e  ts are passed in
68a0: 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   as the second p
68b0: 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
68c0: 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a 20 54  te3_exec()..** T
68d0: 68 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  he statements ar
68e0: 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e 65 20  e evaluated one 
68f0: 62 79 20 6f 6e 65 20 75 6e 74 69 6c 20 65 69 74  by one until eit
6900: 68 65 72 20 61 6e 20 65 72 72 6f 72 20 6f 72 0a  her an error or.
6910: 2a 2a 20 61 6e 20 69 6e 74 65 72 72 75 70 74 20  ** an interrupt 
6920: 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20  is encountered, 
6930: 6f 72 20 75 6e 74 69 6c 20 74 68 65 79 20 61 72  or until they ar
6940: 65 20 61 6c 6c 20 64 6f 6e 65 2e 20 20 54 68 65  e all done.  The
6950: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 0a 2a   3rd parameter.*
6960: 2a 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c  * is an optional
6970: 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 69   callback that i
6980: 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66  s invoked once f
6990: 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20 61  or each row of a
69a0: 6e 79 20 71 75 65 72 79 0a 2a 2a 20 72 65 73 75  ny query.** resu
69b0: 6c 74 73 20 70 72 6f 64 75 63 65 64 20 62 79 20  lts produced by 
69c0: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
69d0: 74 73 2e 20 20 54 68 65 20 35 74 68 20 70 61 72  ts.  The 5th par
69e0: 61 6d 65 74 65 72 20 74 65 6c 6c 73 20 77 68 65  ameter tells whe
69f0: 72 65 0a 2a 2a 20 74 6f 20 77 72 69 74 65 20 61  re.** to write a
6a00: 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ny error message
6a10: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72  s..**.** The err
6a20: 6f 72 20 6d 65 73 73 61 67 65 20 70 61 73 73 65  or message passe
6a30: 64 20 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74  d back through t
6a40: 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72  he 5th parameter
6a50: 20 69 73 20 68 65 6c 64 0a 2a 2a 20 69 6e 20 6d   is held.** in m
6a60: 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
6a70: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
6a80: 6c 6f 63 28 29 5d 2e 20 20 54 6f 20 61 76 6f 69  loc()].  To avoi
6a90: 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2c  d a memory leak,
6aa0: 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  .** the calling 
6ab0: 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75  application shou
6ac0: 6c 64 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33  ld call [sqlite3
6ad0: 5f 66 72 65 65 28 29 5d 20 6f 6e 20 61 6e 79 20  _free()] on any 
6ae0: 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65  error.** message
6af0: 20 72 65 74 75 72 6e 65 64 20 74 68 72 6f 75 67   returned throug
6b00: 68 20 74 68 65 20 35 74 68 20 70 61 72 61 6d 65  h the 5th parame
6b10: 74 65 72 20 77 68 65 6e 20 69 74 20 68 61 73 20  ter when it has 
6b20: 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 0a 2a  finished using.*
6b30: 2a 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  * the error mess
6b40: 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  age..**.** If th
6b50: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
6b60: 69 6e 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  in the 2nd param
6b70: 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 6f 72 20  eter is NULL or 
6b80: 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 0a  an empty string.
6b90: 2a 2a 20 6f 72 20 61 20 73 74 72 69 6e 67 20 63  ** or a string c
6ba0: 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e 6c 79 20 77  ontaining only w
6bb0: 68 69 74 65 73 70 61 63 65 20 61 6e 64 20 63 6f  hitespace and co
6bc0: 6d 6d 65 6e 74 73 2c 20 74 68 65 6e 20 6e 6f 20  mments, then no 
6bd0: 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
6be0: 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20  s are evaluated 
6bf0: 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
6c00: 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2e   is not changed.
6c10: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
6c20: 65 33 5f 65 78 65 63 28 29 20 69 6e 74 65 72 66  e3_exec() interf
6c30: 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  ace is implement
6c40: 65 64 20 69 6e 20 74 65 72 6d 73 20 6f 66 0a 2a  ed in terms of.*
6c50: 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
6c60: 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74  re_v2()], [sqlit
6c70: 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64 20  e3_step()], and 
6c80: 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
6c90: 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  e()]..** The sql
6ca0: 69 74 65 33 5f 65 78 65 63 28 29 20 72 6f 75 74  ite3_exec() rout
6cb0: 69 6e 65 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67  ine does nothing
6cc0: 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
6cd0: 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 20   that cannot be 
6ce0: 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b 73 71 6c 69  done.** by [sqli
6cf0: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
6d00: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ], [sqlite3_step
6d10: 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
6d20: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a  3_finalize()]..*
6d30: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70  *.** The first p
6d40: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
6d50: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6d 75 73  ite3_exec()] mus
6d60: 74 20 62 65 20 61 6e 20 76 61 6c 69 64 20 61 6e  t be an valid an
6d70: 64 20 6f 70 65 6e 0a 2a 2a 20 5b 64 61 74 61 62  d open.** [datab
6d80: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
6d90: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 61 74 61 62  .**.** The datab
6da0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  ase connection m
6db0: 75 73 74 20 6e 6f 74 20 62 65 20 63 6c 6f 73 65  ust not be close
6dc0: 64 20 77 68 69 6c 65 0a 2a 2a 20 5b 73 71 6c 69  d while.** [sqli
6dd0: 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72  te3_exec()] is r
6de0: 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  unning..**.** Th
6df0: 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
6e00: 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 5b 73  on should use [s
6e10: 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 74  qlite3_free()] t
6e20: 6f 20 66 72 65 65 0a 2a 2a 20 74 68 65 20 6d 65  o free.** the me
6e30: 6d 6f 72 79 20 74 68 61 74 20 2a 65 72 72 6d 73  mory that *errms
6e40: 67 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  g is left pointi
6e50: 6e 67 20 61 74 20 6f 6e 63 65 20 74 68 65 20 65  ng at once the e
6e60: 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20  rror.** message 
6e70: 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65  is no longer nee
6e80: 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53  ded..**.** The S
6e90: 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78  QL statement tex
6ea0: 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 61 72  t in the 2nd par
6eb0: 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
6ec0: 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20 6d 75  e3_exec()].** mu
6ed0: 73 74 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e  st remain unchan
6ee0: 67 65 64 20 77 68 69 6c 65 20 5b 73 71 6c 69 74  ged while [sqlit
6ef0: 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72 75  e3_exec()] is ru
6f00: 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  nning..**.** Req
6f10: 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
6f20: 31 32 31 30 31 5d 20 5b 48 31 32 31 30 32 5d 20  12101] [H12102] 
6f30: 5b 48 31 32 31 30 34 5d 20 5b 48 31 32 31 30 35  [H12104] [H12105
6f40: 5d 20 5b 48 31 32 31 30 37 5d 20 5b 48 31 32 31  ] [H12107] [H121
6f50: 31 30 5d 20 5b 48 31 32 31 31 33 5d 20 5b 48 31  10] [H12113] [H1
6f60: 32 31 31 36 5d 0a 2a 2a 20 5b 48 31 32 31 31 39  2116].** [H12119
6f70: 5d 20 5b 48 31 32 31 32 32 5d 20 5b 48 31 32 31  ] [H12122] [H121
6f80: 32 35 5d 20 5b 48 31 32 31 33 31 5d 20 5b 48 31  25] [H12131] [H1
6f90: 32 31 33 34 5d 20 5b 48 31 32 31 33 37 5d 20 5b  2134] [H12137] [
6fa0: 48 31 32 31 33 38 5d 0a 2a 2f 0a 53 51 4c 49 54  H12138].*/.SQLIT
6fb0: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
6fc0: 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65  3_exec(.  sqlite
6fd0: 33 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  3*,             
6fe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ff0: 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20       /* An open 
7000: 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f  database */.  co
7010: 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c 20 20  nst char *sql,  
7020: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7030: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20           /* SQL 
7040: 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20  to be evaluated 
7050: 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 61 6c 6c 62  */.  int (*callb
7060: 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  ack)(void*,int,c
7070: 68 61 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c 20 20  har**,char**),  
7080: 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63  /* Callback func
7090: 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
70a0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
70b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70c0: 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 67 75       /* 1st argu
70d0: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61 63 6b  ment to callback
70e0: 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 65 72 72   */.  char **err
70f0: 6d 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20  msg             
7100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7110: 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72   /* Error msg wr
7120: 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b  itten here */.);
7130: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
7140: 3a 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b  : Result Codes {
7150: 48 31 30 32 31 30 7d 20 3c 53 31 30 37 30 30 3e  H10210} <S10700>
7160: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51  .** KEYWORDS: SQ
7170: 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f 72 20 63  LITE_OK {error c
7180: 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63 6f 64 65  ode} {error code
7190: 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  s}.** KEYWORDS: 
71a0: 7b 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 72  {result code} {r
71b0: 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a  esult codes}.**.
71c0: 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 65 20 66  ** Many SQLite f
71d0: 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
71e0: 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c  an integer resul
71f0: 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20  t code from the 
7200: 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 68 65 72  set shown.** her
7210: 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e  e in order to in
7220: 64 69 63 61 74 65 73 20 73 75 63 63 65 73 73 20  dicates success 
7230: 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a  or failure..**.*
7240: 2a 20 4e 65 77 20 65 72 72 6f 72 20 63 6f 64 65  * New error code
7250: 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  s may be added i
7260: 6e 20 66 75 74 75 72 65 20 76 65 72 73 69 6f 6e  n future version
7270: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s of SQLite..**.
7280: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51  ** See also: [SQ
7290: 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20  LITE_IOERR_READ 
72a0: 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  | extended resul
72b0: 74 20 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64 65 66  t codes].*/.#def
72c0: 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20  ine SQLITE_OK   
72d0: 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 20 53          0   /* S
72e0: 75 63 63 65 73 73 66 75 6c 20 72 65 73 75 6c 74  uccessful result
72f0: 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 6e 67   */./* beginning
7300: 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20  -of-error-codes 
7310: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7320: 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20 20 31  E_ERROR        1
7330: 20 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f 72 20     /* SQL error 
7340: 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62  or missing datab
7350: 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ase */.#define S
7360: 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20  QLITE_INTERNAL  
7370: 20 20 20 32 20 20 20 2f 2a 20 49 6e 74 65 72 6e     2   /* Intern
7380: 61 6c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69  al logic error i
7390: 6e 20 53 51 4c 69 74 65 20 2a 2f 0a 23 64 65 66  n SQLite */.#def
73a0: 69 6e 65 20 53 51 4c 49 54 45 5f 50 45 52 4d 20  ine SQLITE_PERM 
73b0: 20 20 20 20 20 20 20 20 33 20 20 20 2f 2a 20 41          3   /* A
73c0: 63 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e  ccess permission
73d0: 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69   denied */.#defi
73e0: 6e 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20  ne SQLITE_ABORT 
73f0: 20 20 20 20 20 20 20 34 20 20 20 2f 2a 20 43 61         4   /* Ca
7400: 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 72  llback routine r
7410: 65 71 75 65 73 74 65 64 20 61 6e 20 61 62 6f 72  equested an abor
7420: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  t */.#define SQL
7430: 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20 20  ITE_BUSY        
7440: 20 35 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61   5   /* The data
7450: 62 61 73 65 20 66 69 6c 65 20 69 73 20 6c 6f 63  base file is loc
7460: 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ked */.#define S
7470: 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20  QLITE_LOCKED    
7480: 20 20 20 36 20 20 20 2f 2a 20 41 20 74 61 62 6c     6   /* A tabl
7490: 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
74a0: 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23  e is locked */.#
74b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f  define SQLITE_NO
74c0: 4d 45 4d 20 20 20 20 20 20 20 20 37 20 20 20 2f  MEM        7   /
74d0: 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  * A malloc() fai
74e0: 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  led */.#define S
74f0: 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20  QLITE_READONLY  
7500: 20 20 20 38 20 20 20 2f 2a 20 41 74 74 65 6d 70     8   /* Attemp
7510: 74 20 74 6f 20 77 72 69 74 65 20 61 20 72 65 61  t to write a rea
7520: 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a  donly database *
7530: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7540: 5f 49 4e 54 45 52 52 55 50 54 20 20 20 20 39 20  _INTERRUPT    9 
7550: 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 74    /* Operation t
7560: 65 72 6d 69 6e 61 74 65 64 20 62 79 20 73 71 6c  erminated by sql
7570: 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
7580: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7590: 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20 31 30  E_IOERR       10
75a0: 20 20 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 64 20     /* Some kind 
75b0: 6f 66 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f  of disk I/O erro
75c0: 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 23 64  r occurred */.#d
75d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52  efine SQLITE_COR
75e0: 52 55 50 54 20 20 20 20 20 31 31 20 20 20 2f 2a  RUPT     11   /*
75f0: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 64 69   The database di
7600: 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66  sk image is malf
7610: 6f 72 6d 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  ormed */.#define
7620: 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44   SQLITE_NOTFOUND
7630: 20 20 20 20 31 32 20 20 20 2f 2a 20 4e 4f 54 20      12   /* NOT 
7640: 55 53 45 44 2e 20 54 61 62 6c 65 20 6f 72 20 72  USED. Table or r
7650: 65 63 6f 72 64 20 6e 6f 74 20 66 6f 75 6e 64 20  ecord not found 
7660: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7670: 45 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 31 33  E_FULL        13
7680: 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6f 6e 20     /* Insertion 
7690: 66 61 69 6c 65 64 20 62 65 63 61 75 73 65 20 64  failed because d
76a0: 61 74 61 62 61 73 65 20 69 73 20 66 75 6c 6c 20  atabase is full 
76b0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
76c0: 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20 31 34  E_CANTOPEN    14
76d0: 20 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f 20     /* Unable to 
76e0: 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  open the databas
76f0: 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e  e file */.#defin
7700: 65 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f  e SQLITE_PROTOCO
7710: 4c 20 20 20 20 31 35 20 20 20 2f 2a 20 4e 4f 54  L    15   /* NOT
7720: 20 55 53 45 44 2e 20 44 61 74 61 62 61 73 65 20   USED. Database 
7730: 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 65 72  lock protocol er
7740: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ror */.#define S
7750: 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20  QLITE_EMPTY     
7760: 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61 62 61    16   /* Databa
7770: 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23  se is empty */.#
7780: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 43  define SQLITE_SC
7790: 48 45 4d 41 20 20 20 20 20 20 31 37 20 20 20 2f  HEMA      17   /
77a0: 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 73  * The database s
77b0: 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20 2a 2f  chema changed */
77c0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
77d0: 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38 20 20  TOOBIG      18  
77e0: 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c   /* String or BL
77f0: 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a 65 20  OB exceeds size 
7800: 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 6e 65  limit */.#define
7810: 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
7820: 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62 6f 72  NT  19   /* Abor
7830: 74 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 72 61  t due to constra
7840: 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f  int violation */
7850: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7860: 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 20 20  MISMATCH    20  
7870: 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 6d 69   /* Data type mi
7880: 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 69 6e  smatch */.#defin
7890: 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20  e SQLITE_MISUSE 
78a0: 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c 69 62       21   /* Lib
78b0: 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f 72 72  rary used incorr
78c0: 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65  ectly */.#define
78d0: 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20   SQLITE_NOLFS   
78e0: 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73 65 73      22   /* Uses
78f0: 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e 6f 74   OS features not
7900: 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 68 6f   supported on ho
7910: 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  st */.#define SQ
7920: 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20 20  LITE_AUTH       
7930: 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 72 69   23   /* Authori
7940: 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 2a 2f  zation denied */
7950: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7960: 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34 20 20  FORMAT      24  
7970: 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 64 61   /* Auxiliary da
7980: 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72  tabase format er
7990: 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ror */.#define S
79a0: 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20  QLITE_RANGE     
79b0: 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 70 61    25   /* 2nd pa
79c0: 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
79d0: 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 20 72  e3_bind out of r
79e0: 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ange */.#define 
79f0: 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20 20  SQLITE_NOTADB   
7a00: 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 65 20     26   /* File 
7a10: 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73 20 6e  opened that is n
7a20: 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 66 69  ot a database fi
7a30: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  le */.#define SQ
7a40: 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20 20 20  LITE_ROW        
7a50: 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 65 33   100  /* sqlite3
7a60: 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e 6f 74  _step() has anot
7a70: 68 65 72 20 72 6f 77 20 72 65 61 64 79 20 2a 2f  her row ready */
7a80: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7a90: 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30 31 20  DONE        101 
7aa0: 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70   /* sqlite3_step
7ab0: 28 29 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  () has finished 
7ac0: 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20  executing */./* 
7ad0: 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64  end-of-error-cod
7ae0: 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  es */../*.** CAP
7af0: 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65 64 20  I3REF: Extended 
7b00: 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31  Result Codes {H1
7b10: 30 32 32 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a  0220} <S10700>.*
7b20: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 78 74  * KEYWORDS: {ext
7b30: 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65  ended error code
7b40: 7d 20 7b 65 78 74 65 6e 64 65 64 20 65 72 72 6f  } {extended erro
7b50: 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 59 57  r codes}.** KEYW
7b60: 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20  ORDS: {extended 
7b70: 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b 65 78  result code} {ex
7b80: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
7b90: 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74  des}.**.** In it
7ba0: 73 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67  s default config
7bb0: 75 72 61 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20  uration, SQLite 
7bc0: 41 50 49 20 72 6f 75 74 69 6e 65 73 20 72 65 74  API routines ret
7bd0: 75 72 6e 20 6f 6e 65 20 6f 66 20 32 36 20 69 6e  urn one of 26 in
7be0: 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  teger.** [SQLITE
7bf0: 5f 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 6f 64  _OK | result cod
7c00: 65 73 5d 2e 20 20 48 6f 77 65 76 65 72 2c 20 65  es].  However, e
7c10: 78 70 65 72 69 65 6e 63 65 20 68 61 73 20 73 68  xperience has sh
7c20: 6f 77 6e 20 74 68 61 74 20 6d 61 6e 79 20 6f 66  own that many of
7c30: 0a 2a 2a 20 74 68 65 73 65 20 72 65 73 75 6c 74  .** these result
7c40: 20 63 6f 64 65 73 20 61 72 65 20 74 6f 6f 20 63   codes are too c
7c50: 6f 61 72 73 65 2d 67 72 61 69 6e 65 64 2e 20 20  oarse-grained.  
7c60: 54 68 65 79 20 64 6f 20 6e 6f 74 20 70 72 6f 76  They do not prov
7c70: 69 64 65 20 61 73 0a 2a 2a 20 6d 75 63 68 20 69  ide as.** much i
7c80: 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
7c90: 20 70 72 6f 62 6c 65 6d 73 20 61 73 20 70 72 6f   problems as pro
7ca0: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 6c  grammers might l
7cb0: 69 6b 65 2e 20 20 49 6e 20 61 6e 20 65 66 66 6f  ike.  In an effo
7cc0: 72 74 20 74 6f 0a 2a 2a 20 61 64 64 72 65 73 73  rt to.** address
7cd0: 20 74 68 69 73 2c 20 6e 65 77 65 72 20 76 65 72   this, newer ver
7ce0: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
7cf0: 28 76 65 72 73 69 6f 6e 20 33 2e 33 2e 38 20 61  (version 3.3.8 a
7d00: 6e 64 20 6c 61 74 65 72 29 20 69 6e 63 6c 75 64  nd later) includ
7d10: 65 0a 2a 2a 20 73 75 70 70 6f 72 74 20 66 6f 72  e.** support for
7d20: 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 65 73 75   additional resu
7d30: 6c 74 20 63 6f 64 65 73 20 74 68 61 74 20 70 72  lt codes that pr
7d40: 6f 76 69 64 65 20 6d 6f 72 65 20 64 65 74 61 69  ovide more detai
7d50: 6c 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  led information.
7d60: 2a 2a 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e  ** about errors.
7d70: 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 65   The extended re
7d80: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 65  sult codes are e
7d90: 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c  nabled or disabl
7da0: 65 64 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 20 64  ed.** on a per d
7db0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
7dc0: 6f 6e 20 62 61 73 69 73 20 75 73 69 6e 67 20 74  on basis using t
7dd0: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65  he.** [sqlite3_e
7de0: 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63  xtended_result_c
7df0: 6f 64 65 73 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a  odes()] API..**.
7e00: 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61  ** Some of the a
7e10: 76 61 69 6c 61 62 6c 65 20 65 78 74 65 6e 64 65  vailable extende
7e20: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61  d result codes a
7e30: 72 65 20 6c 69 73 74 65 64 20 68 65 72 65 2e 0a  re listed here..
7e40: 2a 2a 20 4f 6e 65 20 6d 61 79 20 65 78 70 65 63  ** One may expec
7e50: 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
7e60: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
7e70: 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 65 78  codes will be ex
7e80: 70 61 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 69 6d  pand.** over tim
7e90: 65 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61  e.  Software tha
7ea0: 74 20 75 73 65 73 20 65 78 74 65 6e 64 65 64 20  t uses extended 
7eb0: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 73 68 6f  result codes sho
7ec0: 75 6c 64 20 65 78 70 65 63 74 0a 2a 2a 20 74 6f  uld expect.** to
7ed0: 20 73 65 65 20 6e 65 77 20 72 65 73 75 6c 74 20   see new result 
7ee0: 63 6f 64 65 73 20 69 6e 20 66 75 74 75 72 65 20  codes in future 
7ef0: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
7f00: 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  te..**.** The SQ
7f10: 4c 49 54 45 5f 4f 4b 20 72 65 73 75 6c 74 20 63  LITE_OK result c
7f20: 6f 64 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  ode will never b
7f30: 65 20 65 78 74 65 6e 64 65 64 2e 20 20 49 74 20  e extended.  It 
7f40: 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62  will always.** b
7f50: 65 20 65 78 61 63 74 6c 79 20 7a 65 72 6f 2e 0a  e exactly zero..
7f60: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7f70: 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 20 20 20  E_IOERR_READ    
7f80: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
7f90: 45 5f 49 4f 45 52 52 20 7c 20 28 31 3c 3c 38 29  E_IOERR | (1<<8)
7fa0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
7fb0: 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
7fc0: 44 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45  D        (SQLITE
7fd0: 5f 49 4f 45 52 52 20 7c 20 28 32 3c 3c 38 29 29  _IOERR | (2<<8))
7fe0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7ff0: 49 4f 45 52 52 5f 57 52 49 54 45 20 20 20 20 20  IOERR_WRITE     
8000: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8010: 49 4f 45 52 52 20 7c 20 28 33 3c 3c 38 29 29 0a  IOERR | (3<<8)).
8020: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8030: 4f 45 52 52 5f 46 53 59 4e 43 20 20 20 20 20 20  OERR_FSYNC      
8040: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
8050: 4f 45 52 52 20 7c 20 28 34 3c 3c 38 29 29 0a 23  OERR | (4<<8)).#
8060: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
8070: 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 20 20 20  ERR_DIR_FSYNC   
8080: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
8090: 45 52 52 20 7c 20 28 35 3c 3c 38 29 29 0a 23 64  ERR | (5<<8)).#d
80a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
80b0: 52 52 5f 54 52 55 4e 43 41 54 45 20 20 20 20 20  RR_TRUNCATE     
80c0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
80d0: 52 52 20 7c 20 28 36 3c 3c 38 29 29 0a 23 64 65  RR | (6<<8)).#de
80e0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52  fine SQLITE_IOER
80f0: 52 5f 46 53 54 41 54 20 20 20 20 20 20 20 20 20  R_FSTAT         
8100: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52      (SQLITE_IOER
8110: 52 20 7c 20 28 37 3c 3c 38 29 29 0a 23 64 65 66  R | (7<<8)).#def
8120: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8130: 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 20  _UNLOCK         
8140: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8150: 20 7c 20 28 38 3c 3c 38 29 29 0a 23 64 65 66 69   | (8<<8)).#defi
8160: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
8170: 52 44 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20  RDLOCK          
8180: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
8190: 7c 20 28 39 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (9<<8)).#defin
81a0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  e SQLITE_IOERR_D
81b0: 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20  ELETE           
81c0: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
81d0: 20 28 31 30 3c 3c 38 29 29 0a 23 64 65 66 69 6e   (10<<8)).#defin
81e0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42  e SQLITE_IOERR_B
81f0: 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 20 20 20  LOCKED          
8200: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8210: 20 28 31 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e   (11<<8)).#defin
8220: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  e SQLITE_IOERR_N
8230: 4f 4d 45 4d 20 20 20 20 20 20 20 20 20 20 20 20  OMEM            
8240: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8250: 20 28 31 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e   (12<<8)).#defin
8260: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41  e SQLITE_IOERR_A
8270: 43 43 45 53 53 20 20 20 20 20 20 20 20 20 20 20  CCESS           
8280: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8290: 20 28 31 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e   (13<<8)).#defin
82a0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  e SQLITE_IOERR_C
82b0: 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b  HECKRESERVEDLOCK
82c0: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
82d0: 20 28 31 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e   (14<<8)).#defin
82e0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  e SQLITE_IOERR_L
82f0: 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 20  OCK             
8300: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8310: 20 28 31 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e   (15<<8)).#defin
8320: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  e SQLITE_IOERR_C
8330: 4c 4f 53 45 20 20 20 20 20 20 20 20 20 20 20 20  LOSE            
8340: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8350: 20 28 31 36 3c 3c 38 29 29 0a 23 64 65 66 69 6e   (16<<8)).#defin
8360: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  e SQLITE_IOERR_D
8370: 49 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20  IR_CLOSE        
8380: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8390: 20 28 31 37 3c 3c 38 29 29 0a 23 64 65 66 69 6e   (17<<8)).#defin
83a0: 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f  e SQLITE_LOCKED_
83b0: 53 48 41 52 45 44 43 41 43 48 45 20 20 20 20 20  SHAREDCACHE     
83c0: 20 28 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20   (SQLITE_LOCKED 
83d0: 7c 20 28 31 3c 3c 38 29 20 29 0a 0a 2f 2a 0a 2a  | (1<<8) )../*.*
83e0: 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67  * CAPI3REF: Flag
83f0: 73 20 46 6f 72 20 46 69 6c 65 20 4f 70 65 6e 20  s For File Open 
8400: 4f 70 65 72 61 74 69 6f 6e 73 20 7b 48 31 30 32  Operations {H102
8410: 33 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 31  30} <H11120> <H1
8420: 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  2700>.**.** Thes
8430: 65 20 62 69 74 20 76 61 6c 75 65 73 20 61 72 65  e bit values are
8440: 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73   intended for us
8450: 65 20 69 6e 20 74 68 65 0a 2a 2a 20 33 72 64 20  e in the.** 3rd 
8460: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
8470: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
8480: 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61  2()] interface a
8490: 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 20 34 74 68  nd.** in the 4th
84a0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
84b0: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f  e xOpen method o
84c0: 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  f the.** [sqlite
84d0: 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a  3_vfs] object..*
84e0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
84f0: 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 20  _OPEN_READONLY  
8500: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30         0x0000000
8510: 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
8520: 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
8530: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30         0x0000000
8540: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
8550: 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20 20 20  _OPEN_CREATE    
8560: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30         0x0000000
8570: 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  4.#define SQLITE
8580: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
8590: 4f 53 45 20 20 20 20 30 78 30 30 30 30 30 30 30  OSE    0x0000000
85a0: 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  8.#define SQLITE
85b0: 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20  _OPEN_EXCLUSIVE 
85c0: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30 31         0x0000001
85d0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
85e0: 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20  _OPEN_MAIN_DB   
85f0: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 31 30         0x0000010
8600: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
8610: 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 20 20  _OPEN_TEMP_DB   
8620: 20 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30         0x0000020
8630: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
8640: 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f  _OPEN_TRANSIENT_
8650: 44 42 20 20 20 20 20 30 78 30 30 30 30 30 34 30  DB     0x0000040
8660: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
8670: 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e  _OPEN_MAIN_JOURN
8680: 41 4c 20 20 20 20 20 30 78 30 30 30 30 30 38 30  AL     0x0000080
8690: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
86a0: 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e  _OPEN_TEMP_JOURN
86b0: 41 4c 20 20 20 20 20 30 78 30 30 30 30 31 30 30  AL     0x0000100
86c0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
86d0: 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c  _OPEN_SUBJOURNAL
86e0: 20 20 20 20 20 20 20 30 78 30 30 30 30 32 30 30         0x0000200
86f0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
8700: 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55  _OPEN_MASTER_JOU
8710: 52 4e 41 4c 20 20 20 30 78 30 30 30 30 34 30 30  RNAL   0x0000400
8720: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
8730: 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 20 20  _OPEN_NOMUTEX   
8740: 20 20 20 20 20 20 20 30 78 30 30 30 30 38 30 30         0x0000800
8750: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
8760: 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20  _OPEN_FULLMUTEX 
8770: 20 20 20 20 20 20 20 30 78 30 30 30 31 30 30 30         0x0001000
8780: 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  0../*.** CAPI3RE
8790: 46 3a 20 44 65 76 69 63 65 20 43 68 61 72 61 63  F: Device Charac
87a0: 74 65 72 69 73 74 69 63 73 20 7b 48 31 30 32 34  teristics {H1024
87b0: 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 2a 0a 2a  0} <H11120>.**.*
87c0: 2a 20 54 68 65 20 78 44 65 76 69 63 65 43 61 70  * The xDeviceCap
87d0: 61 62 69 6c 69 74 69 65 73 20 6d 65 74 68 6f 64  abilities method
87e0: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
87f0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20  _io_methods].** 
8800: 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 73 20 61  object returns a
8810: 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
8820: 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 74  is a vector of t
8830: 68 65 20 74 68 65 73 65 0a 2a 2a 20 62 69 74 20  he these.** bit 
8840: 76 61 6c 75 65 73 20 65 78 70 72 65 73 73 69 6e  values expressin
8850: 67 20 49 2f 4f 20 63 68 61 72 61 63 74 65 72 69  g I/O characteri
8860: 73 74 69 63 73 20 6f 66 20 74 68 65 20 6d 61 73  stics of the mas
8870: 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20 64 65 76  s storage.** dev
8880: 69 63 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ice that holds t
8890: 68 65 20 66 69 6c 65 20 74 68 61 74 20 74 68 65  he file that the
88a0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
88b0: 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65 72 73 20  hods].** refers 
88c0: 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  to..**.** The SQ
88d0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
88e0: 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73  C property means
88f0: 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73   that all writes
8900: 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20   of.** any size 
8910: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65  are atomic.  The
8920: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
8930: 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a  OMICnnn values.*
8940: 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74  * mean that writ
8950: 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61  es of blocks tha
8960: 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20  t are nnn bytes 
8970: 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61  in size and.** a
8980: 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e  re aligned to an
8990: 20 61 64 64 72 65 73 73 20 77 68 69 63 68 20 69   address which i
89a0: 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c  s an integer mul
89b0: 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20  tiple of.** nnn 
89c0: 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65  are atomic.  The
89d0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41   SQLITE_IOCAP_SA
89e0: 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20  FE_APPEND value 
89f0: 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68  means.** that wh
8a00: 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 65 6e  en data is appen
8a10: 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74  ded to a file, t
8a20: 68 65 20 64 61 74 61 20 69 73 20 61 70 70 65 6e  he data is appen
8a30: 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65  ded.** first the
8a40: 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  n the size of th
8a50: 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64  e file is extend
8a60: 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74  ed, never the ot
8a70: 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e  her.** way aroun
8a80: 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49  d.  The SQLITE_I
8a90: 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20  OCAP_SEQUENTIAL 
8aa0: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74  property means t
8ab0: 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  hat.** informati
8ac0: 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  on is written to
8ad0: 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d   disk in the sam
8ae0: 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73  e order as calls
8af0: 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e  .** to xWrite().
8b00: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
8b10: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20  TE_IOCAP_ATOMIC 
8b20: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
8b30: 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  001.#define SQLI
8b40: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35  TE_IOCAP_ATOMIC5
8b50: 31 32 20 20 20 20 20 20 20 30 78 30 30 30 30 30  12       0x00000
8b60: 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  002.#define SQLI
8b70: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31  TE_IOCAP_ATOMIC1
8b80: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  K        0x00000
8b90: 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  004.#define SQLI
8ba0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32  TE_IOCAP_ATOMIC2
8bb0: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  K        0x00000
8bc0: 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  008.#define SQLI
8bd0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34  TE_IOCAP_ATOMIC4
8be0: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  K        0x00000
8bf0: 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  010.#define SQLI
8c00: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38  TE_IOCAP_ATOMIC8
8c10: 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  K        0x00000
8c20: 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  020.#define SQLI
8c30: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31  TE_IOCAP_ATOMIC1
8c40: 36 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30  6K       0x00000
8c50: 30 34 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  040.#define SQLI
8c60: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33  TE_IOCAP_ATOMIC3
8c70: 32 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30  2K       0x00000
8c80: 30 38 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  080.#define SQLI
8c90: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36  TE_IOCAP_ATOMIC6
8ca0: 34 4b 20 20 20 20 20 20 20 30 78 30 30 30 30 30  4K       0x00000
8cb0: 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  100.#define SQLI
8cc0: 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50  TE_IOCAP_SAFE_AP
8cd0: 50 45 4e 44 20 20 20 20 20 30 78 30 30 30 30 30  PEND     0x00000
8ce0: 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  200.#define SQLI
8cf0: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
8d00: 49 41 4c 20 20 20 20 20 20 30 78 30 30 30 30 30  IAL      0x00000
8d10: 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  400../*.** CAPI3
8d20: 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e  REF: File Lockin
8d30: 67 20 4c 65 76 65 6c 73 20 7b 48 31 30 32 35 30  g Levels {H10250
8d40: 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 31 31 33  } <H11120> <H113
8d50: 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  10>.**.** SQLite
8d60: 20 75 73 65 73 20 6f 6e 65 20 6f 66 20 74 68 65   uses one of the
8d70: 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  se integer value
8d80: 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a  s as the second.
8d90: 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63  ** argument to c
8da0: 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73 20 74 6f  alls it makes to
8db0: 20 74 68 65 20 78 4c 6f 63 6b 28 29 20 61 6e 64   the xLock() and
8dc0: 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65 74 68 6f   xUnlock() metho
8dd0: 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b 73 71 6c  ds.** of an [sql
8de0: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d  ite3_io_methods]
8df0: 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66   object..*/.#def
8e00: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  ine SQLITE_LOCK_
8e10: 4e 4f 4e 45 20 20 20 20 20 20 20 20 20 20 30 0a  NONE          0.
8e20: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
8e30: 4f 43 4b 5f 53 48 41 52 45 44 20 20 20 20 20 20  OCK_SHARED      
8e40: 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    1.#define SQLI
8e50: 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44  TE_LOCK_RESERVED
8e60: 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20        2.#define 
8e70: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44  SQLITE_LOCK_PEND
8e80: 49 4e 47 20 20 20 20 20 20 20 33 0a 23 64 65 66  ING       3.#def
8e90: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  ine SQLITE_LOCK_
8ea0: 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 34 0a  EXCLUSIVE     4.
8eb0: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
8ec0: 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f 6e   Synchronization
8ed0: 20 54 79 70 65 20 46 6c 61 67 73 20 7b 48 31 30   Type Flags {H10
8ee0: 32 36 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 2a  260} <H11120>.**
8ef0: 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20  .** When SQLite 
8f00: 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 53 79 6e  invokes the xSyn
8f10: 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e  c() method of an
8f20: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  .** [sqlite3_io_
8f30: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
8f40: 69 74 20 75 73 65 73 20 61 20 63 6f 6d 62 69 6e  it uses a combin
8f50: 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 73  ation of.** thes
8f60: 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  e integer values
8f70: 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
8f80: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57  rgument..**.** W
8f90: 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 53  hen the SQLITE_S
8fa0: 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 66 6c 61  YNC_DATAONLY fla
8fb0: 67 20 69 73 20 75 73 65 64 2c 20 69 74 20 6d 65  g is used, it me
8fc0: 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ans that the.** 
8fd0: 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e 20 6f  sync operation o
8fe0: 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 66 6c 75  nly needs to flu
8ff0: 73 68 20 64 61 74 61 20 74 6f 20 6d 61 73 73 20  sh data to mass 
9000: 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f 64 65 0a  storage.  Inode.
9010: 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e  ** information n
9020: 65 65 64 20 6e 6f 74 20 62 65 20 66 6c 75 73 68  eed not be flush
9030: 65 64 2e 20 49 66 20 74 68 65 20 6c 6f 77 65 72  ed. If the lower
9040: 20 66 6f 75 72 20 62 69 74 73 20 6f 66 20 74 68   four bits of th
9050: 65 20 66 6c 61 67 0a 2a 2a 20 65 71 75 61 6c 20  e flag.** equal 
9060: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
9070: 41 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  AL, that means t
9080: 6f 20 75 73 65 20 6e 6f 72 6d 61 6c 20 66 73 79  o use normal fsy
9090: 6e 63 28 29 20 73 65 6d 61 6e 74 69 63 73 2e 0a  nc() semantics..
90a0: 2a 2a 20 49 66 20 74 68 65 20 6c 6f 77 65 72 20  ** If the lower 
90b0: 66 6f 75 72 20 62 69 74 73 20 65 71 75 61 6c 20  four bits equal 
90c0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
90d0: 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 2a 2a 20  , that means.** 
90e0: 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 20 58 20  to use Mac OS X 
90f0: 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 20 69  style fullsync i
9100: 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e 63 28  nstead of fsync(
9110: 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  )..*/.#define SQ
9120: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
9130: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 32 0a          0x00002.
9140: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
9150: 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20 20 20  YNC_FULL        
9160: 20 20 30 78 30 30 30 30 33 0a 23 64 65 66 69 6e    0x00003.#defin
9170: 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41  e SQLITE_SYNC_DA
9180: 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78 30 30  TAONLY      0x00
9190: 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  010../*.** CAPI3
91a0: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63  REF: OS Interfac
91b0: 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61 6e 64  e Open File Hand
91c0: 6c 65 20 7b 48 31 31 31 31 30 7d 20 3c 53 32 30  le {H11110} <S20
91d0: 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 73  110>.**.** An [s
91e0: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a  qlite3_file] obj
91f0: 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 61  ect represents a
9200: 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e 20 74  n open file in t
9210: 68 65 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 66 61  he OS.** interfa
9220: 63 65 20 6c 61 79 65 72 2e 20 20 49 6e 64 69 76  ce layer.  Indiv
9230: 69 64 75 61 6c 20 4f 53 20 69 6e 74 65 72 66 61  idual OS interfa
9240: 63 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ce implementatio
9250: 6e 73 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20  ns will.** want 
9260: 74 6f 20 73 75 62 63 6c 61 73 73 20 74 68 69 73  to subclass this
9270: 20 6f 62 6a 65 63 74 20 62 79 20 61 70 70 65 6e   object by appen
9280: 64 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c 20  ding additional 
9290: 66 69 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68  fields.** for th
92a0: 65 69 72 20 6f 77 6e 20 75 73 65 2e 20 20 54 68  eir own use.  Th
92b0: 65 20 70 4d 65 74 68 6f 64 73 20 65 6e 74 72 79  e pMethods entry
92c0: 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
92d0: 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   an.** [sqlite3_
92e0: 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65  io_methods] obje
92f0: 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  ct that defines 
9300: 6d 65 74 68 6f 64 73 20 66 6f 72 20 70 65 72 66  methods for perf
9310: 6f 72 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70  orming.** I/O op
9320: 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20  erations on the 
9330: 6f 70 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79  open file..*/.ty
9340: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
9350: 69 74 65 33 5f 66 69 6c 65 20 73 71 6c 69 74 65  ite3_file sqlite
9360: 33 5f 66 69 6c 65 3b 0a 73 74 72 75 63 74 20 73  3_file;.struct s
9370: 71 6c 69 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20  qlite3_file {.  
9380: 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c  const struct sql
9390: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
93a0: 2a 70 4d 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d  *pMethods;  /* M
93b0: 65 74 68 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70  ethods for an op
93c0: 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f  en file */.};../
93d0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
93e0: 53 20 49 6e 74 65 72 66 61 63 65 20 46 69 6c 65  S Interface File
93f0: 20 56 69 72 74 75 61 6c 20 4d 65 74 68 6f 64 73   Virtual Methods
9400: 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 32 30 7d   Object {H11120}
9410: 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20   <S20110>.**.** 
9420: 45 76 65 72 79 20 66 69 6c 65 20 6f 70 65 6e 65  Every file opene
9430: 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65  d by the [sqlite
9440: 33 5f 76 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74  3_vfs] xOpen met
9450: 68 6f 64 20 70 6f 70 75 6c 61 74 65 73 20 61 6e  hod populates an
9460: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  .** [sqlite3_fil
9470: 65 5d 20 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d  e] object (or, m
9480: 6f 72 65 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20  ore commonly, a 
9490: 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a  subclass of the.
94a0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65  ** [sqlite3_file
94b0: 5d 20 6f 62 6a 65 63 74 29 20 77 69 74 68 20 61  ] object) with a
94c0: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
94d0: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
94e0: 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20  object..** This 
94f0: 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74  object defines t
9500: 68 65 20 6d 65 74 68 6f 64 73 20 75 73 65 64 20  he methods used 
9510: 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f  to perform vario
9520: 75 73 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a  us operations.**
9530: 20 61 67 61 69 6e 73 74 20 74 68 65 20 6f 70 65   against the ope
9540: 6e 20 66 69 6c 65 20 72 65 70 72 65 73 65 6e 74  n file represent
9550: 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74  ed by the [sqlit
9560: 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e  e3_file] object.
9570: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  .**.** The flags
9580: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79   argument to xSy
9590: 6e 63 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66  nc may be one of
95a0: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f   [SQLITE_SYNC_NO
95b0: 52 4d 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c  RMAL] or.** [SQL
95c0: 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20  ITE_SYNC_FULL]. 
95d0: 20 54 68 65 20 66 69 72 73 74 20 63 68 6f 69 63   The first choic
95e0: 65 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20  e is the normal 
95f0: 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 65 20  fsync()..** The 
9600: 73 65 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73  second choice is
9610: 20 61 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c   a Mac OS X styl
9620: 65 20 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65  e fullsync.  The
9630: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41   [SQLITE_SYNC_DA
9640: 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20  TAONLY].** flag 
9650: 6d 61 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74  may be ORed in t
9660: 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20  o indicate that 
9670: 6f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66  only the data of
9680: 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64   the file.** and
9690: 20 6e 6f 74 20 69 74 73 20 69 6e 6f 64 65 20 6e   not its inode n
96a0: 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 65  eeds to be synce
96b0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74  d..**.** The int
96c0: 65 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 78  eger values to x
96d0: 4c 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f  Lock() and xUnlo
96e0: 63 6b 28 29 20 61 72 65 20 6f 6e 65 20 6f 66 0a  ck() are one of.
96f0: 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
9700: 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e  [SQLITE_LOCK_NON
9710: 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  E],.** <li> [SQL
9720: 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d  ITE_LOCK_SHARED]
9730: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ,.** <li> [SQLIT
9740: 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d  E_LOCK_RESERVED]
9750: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ,.** <li> [SQLIT
9760: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c  E_LOCK_PENDING],
9770: 20 6f 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c   or.** <li> [SQL
9780: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49  ITE_LOCK_EXCLUSI
9790: 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  VE]..** </ul>.**
97a0: 20 78 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61 73   xLock() increas
97b0: 65 73 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55 6e  es the lock. xUn
97c0: 6c 6f 63 6b 28 29 20 64 65 63 72 65 61 73 65 73  lock() decreases
97d0: 20 74 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68   the lock..** Th
97e0: 65 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  e xCheckReserved
97f0: 4c 6f 63 6b 28 29 20 6d 65 74 68 6f 64 20 63 68  Lock() method ch
9800: 65 63 6b 73 20 77 68 65 74 68 65 72 20 61 6e 79  ecks whether any
9810: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
9820: 74 69 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 72 20  tion,.** either 
9830: 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
9840: 6f 72 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72  or in some other
9850: 20 70 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c   process, is hol
9860: 64 69 6e 67 20 61 20 52 45 53 45 52 56 45 44 2c  ding a RESERVED,
9870: 0a 2a 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20  .** PENDING, or 
9880: 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
9890: 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20  n the file.  It 
98a0: 72 65 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20  returns true.** 
98b0: 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65  if such a lock e
98c0: 78 69 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20  xists and false 
98d0: 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a  otherwise..**.**
98e0: 20 54 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   The xFileContro
98f0: 6c 28 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20  l() method is a 
9900: 67 65 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63  generic interfac
9910: 65 20 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75  e that allows cu
9920: 73 74 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c  stom.** VFS impl
9930: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64  ementations to d
9940: 69 72 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20  irectly control 
9950: 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69  an open file usi
9960: 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  ng the.** [sqlit
9970: 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
9980: 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54  )] interface.  T
9990: 68 65 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61  he second "op" a
99a0: 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 0a 2a 2a  rgument is an.**
99b0: 20 69 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e   integer opcode.
99c0: 20 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75    The third argu
99d0: 6d 65 6e 74 20 69 73 20 61 20 67 65 6e 65 72 69  ment is a generi
99e0: 63 20 70 6f 69 6e 74 65 72 20 69 6e 74 65 6e 64  c pointer intend
99f0: 65 64 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74  ed to.** point t
9a00: 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68  o a structure th
9a10: 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61  at may contain a
9a20: 72 67 75 6d 65 6e 74 73 20 6f 72 20 73 70 61 63  rguments or spac
9a30: 65 20 69 6e 20 77 68 69 63 68 20 74 6f 0a 2a 2a  e in which to.**
9a40: 20 77 72 69 74 65 20 72 65 74 75 72 6e 20 76 61   write return va
9a50: 6c 75 65 73 2e 20 20 50 6f 74 65 6e 74 69 61 6c  lues.  Potential
9a60: 20 75 73 65 73 20 66 6f 72 20 78 46 69 6c 65 43   uses for xFileC
9a70: 6f 6e 74 72 6f 6c 28 29 20 6d 69 67 68 74 20 62  ontrol() might b
9a80: 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74  e.** functions t
9a90: 6f 20 65 6e 61 62 6c 65 20 62 6c 6f 63 6b 69 6e  o enable blockin
9aa0: 67 20 6c 6f 63 6b 73 20 77 69 74 68 20 74 69 6d  g locks with tim
9ab0: 65 6f 75 74 73 2c 20 74 6f 20 63 68 61 6e 67 65  eouts, to change
9ac0: 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20   the.** locking 
9ad0: 73 74 72 61 74 65 67 79 20 28 66 6f 72 20 65 78  strategy (for ex
9ae0: 61 6d 70 6c 65 20 74 6f 20 75 73 65 20 64 6f 74  ample to use dot
9af0: 2d 66 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 74 6f  -file locks), to
9b00: 20 69 6e 71 75 69 72 65 0a 2a 2a 20 61 62 6f 75   inquire.** abou
9b10: 74 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20  t the status of 
9b20: 61 20 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 62 72  a lock, or to br
9b30: 65 61 6b 20 73 74 61 6c 65 20 6c 6f 63 6b 73 2e  eak stale locks.
9b40: 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20    The SQLite.** 
9b50: 63 6f 72 65 20 72 65 73 65 72 76 65 73 20 61 6c  core reserves al
9b60: 6c 20 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74  l opcodes less t
9b70: 68 61 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20  han 100 for its 
9b80: 6f 77 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 5b 53  own use..** A [S
9b90: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b  QLITE_FCNTL_LOCK
9ba0: 53 54 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20  STATE | list of 
9bb0: 6f 70 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68  opcodes] less th
9bc0: 61 6e 20 31 30 30 20 69 73 20 61 76 61 69 6c 61  an 100 is availa
9bd0: 62 6c 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74  ble..** Applicat
9be0: 69 6f 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65  ions that define
9bf0: 20 61 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43   a custom xFileC
9c00: 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68  ontrol method sh
9c10: 6f 75 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73  ould use opcodes
9c20: 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
9c30: 20 31 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f   100 to avoid co
9c40: 6e 66 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  nflicts..**.** T
9c50: 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29  he xSectorSize()
9c60: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
9c70: 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
9c80: 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65  of the.** device
9c90: 20 74 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20   that underlies 
9ca0: 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73  the file.  The s
9cb0: 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68  ector size is th
9cc0: 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69  e.** minimum wri
9cd0: 74 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 70  te that can be p
9ce0: 65 72 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74  erformed without
9cf0: 20 64 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f   disturbing.** o
9d00: 74 68 65 72 20 62 79 74 65 73 20 69 6e 20 74 68  ther bytes in th
9d10: 65 20 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65  e file.  The xDe
9d20: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
9d30: 69 63 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  ics().** method 
9d40: 72 65 74 75 72 6e 73 20 61 20 62 69 74 20 76 65  returns a bit ve
9d50: 63 74 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20  ctor describing 
9d60: 62 65 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65  behaviors of the
9d70: 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64  .** underlying d
9d80: 65 76 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  evice:.**.** <ul
9d90: 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  >.** <li> [SQLIT
9da0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a  E_IOCAP_ATOMIC].
9db0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
9dc0: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d  IOCAP_ATOMIC512]
9dd0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9de0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d  _IOCAP_ATOMIC1K]
9df0: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9e00: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d  _IOCAP_ATOMIC2K]
9e10: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9e20: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d  _IOCAP_ATOMIC4K]
9e30: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9e40: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d  _IOCAP_ATOMIC8K]
9e50: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
9e60: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b  _IOCAP_ATOMIC16K
9e70: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ].** <li> [SQLIT
9e80: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32  E_IOCAP_ATOMIC32
9e90: 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  K].** <li> [SQLI
9ea0: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36  TE_IOCAP_ATOMIC6
9eb0: 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  4K].** <li> [SQL
9ec0: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
9ed0: 50 50 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b  PPEND].** <li> [
9ee0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
9ef0: 55 45 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c  UENTIAL].** </ul
9f00: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  >.**.** The SQLI
9f10: 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20  TE_IOCAP_ATOMIC 
9f20: 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74  property means t
9f30: 68 61 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f  hat all writes o
9f40: 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72  f.** any size ar
9f50: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53  e atomic.  The S
9f60: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
9f70: 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20  ICnnn values.** 
9f80: 6d 65 61 6e 20 74 68 61 74 20 77 72 69 74 65 73  mean that writes
9f90: 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20   of blocks that 
9fa0: 61 72 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e  are nnn bytes in
9fb0: 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65   size and.** are
9fc0: 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61   aligned to an a
9fd0: 64 64 72 65 73 73 20 77 68 69 63 68 20 69 73 20  ddress which is 
9fe0: 61 6e 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69  an integer multi
9ff0: 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72  ple of.** nnn ar
a000: 65 20 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53  e atomic.  The S
a010: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45  QLITE_IOCAP_SAFE
a020: 5f 41 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65  _APPEND value me
a030: 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e  ans.** that when
a040: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65   data is appende
a050: 64 20 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65  d to a file, the
a060: 20 64 61 74 61 20 69 73 20 61 70 70 65 6e 64 65   data is appende
a070: 64 0a 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20  d.** first then 
a080: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
a090: 66 69 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64  file is extended
a0a0: 2c 20 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65  , never the othe
a0b0: 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e  r.** way around.
a0c0: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43    The SQLITE_IOC
a0d0: 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72  AP_SEQUENTIAL pr
a0e0: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61  operty means tha
a0f0: 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  t.** information
a100: 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64   is written to d
a110: 69 73 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20  isk in the same 
a120: 6f 72 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a  order as calls.*
a130: 2a 20 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a  * to xWrite()..*
a140: 2a 0a 2a 2a 20 49 66 20 78 52 65 61 64 28 29 20  *.** If xRead() 
a150: 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49  returns SQLITE_I
a160: 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20  OERR_SHORT_READ 
a170: 69 74 20 6d 75 73 74 20 61 6c 73 6f 20 66 69 6c  it must also fil
a180: 6c 0a 2a 2a 20 69 6e 20 74 68 65 20 75 6e 72 65  l.** in the unre
a190: 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74  ad portions of t
a1a0: 68 65 20 62 75 66 66 65 72 20 77 69 74 68 20 7a  he buffer with z
a1b0: 65 72 6f 73 2e 20 20 41 20 56 46 53 20 74 68 61  eros.  A VFS tha
a1c0: 74 0a 2a 2a 20 66 61 69 6c 73 20 74 6f 20 7a 65  t.** fails to ze
a1d0: 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65  ro-fill short re
a1e0: 61 64 73 20 6d 69 67 68 74 20 73 65 65 6d 20 74  ads might seem t
a1f0: 6f 20 77 6f 72 6b 2e 20 20 48 6f 77 65 76 65 72  o work.  However
a200: 2c 0a 2a 2a 20 66 61 69 6c 75 72 65 20 74 6f 20  ,.** failure to 
a210: 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20  zero-fill short 
a220: 72 65 61 64 73 20 77 69 6c 6c 20 65 76 65 6e 74  reads will event
a230: 75 61 6c 6c 79 20 6c 65 61 64 20 74 6f 0a 2a 2a  ually lead to.**
a240: 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
a250: 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
a260: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
a270: 69 6f 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74  io_methods sqlit
a280: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73  e3_io_methods;.s
a290: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f  truct sqlite3_io
a2a0: 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74  _methods {.  int
a2b0: 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74   iVersion;.  int
a2c0: 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74   (*xClose)(sqlit
a2d0: 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74  e3_file*);.  int
a2e0: 20 28 2a 78 52 65 61 64 29 28 73 71 6c 69 74 65   (*xRead)(sqlite
a2f0: 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20  3_file*, void*, 
a300: 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65  int iAmt, sqlite
a310: 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a  3_int64 iOfst);.
a320: 20 20 69 6e 74 20 28 2a 78 57 72 69 74 65 29 28    int (*xWrite)(
a330: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63  sqlite3_file*, c
a340: 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20  onst void*, int 
a350: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e  iAmt, sqlite3_in
a360: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e  t64 iOfst);.  in
a370: 74 20 28 2a 78 54 72 75 6e 63 61 74 65 29 28 73  t (*xTruncate)(s
a380: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71  qlite3_file*, sq
a390: 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 69 7a 65  lite3_int64 size
a3a0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63  );.  int (*xSync
a3b0: 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  )(sqlite3_file*,
a3c0: 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20 69   int flags);.  i
a3d0: 6e 74 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28  nt (*xFileSize)(
a3e0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73  sqlite3_file*, s
a3f0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53  qlite3_int64 *pS
a400: 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c  ize);.  int (*xL
a410: 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock)(sqlite3_fil
a420: 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20  e*, int);.  int 
a430: 28 2a 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69 74  (*xUnlock)(sqlit
a440: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
a450: 20 20 69 6e 74 20 28 2a 78 43 68 65 63 6b 52 65    int (*xCheckRe
a460: 73 65 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c 69  servedLock)(sqli
a470: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 2a  te3_file*, int *
a480: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20  pResOut);.  int 
a490: 28 2a 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 29 28  (*xFileControl)(
a4a0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
a4b0: 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
a4c0: 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63  g);.  int (*xSec
a4d0: 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33  torSize)(sqlite3
a4e0: 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28  _file*);.  int (
a4f0: 2a 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65  *xDeviceCharacte
a500: 72 69 73 74 69 63 73 29 28 73 71 6c 69 74 65 33  ristics)(sqlite3
a510: 5f 66 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 41 64  _file*);.  /* Ad
a520: 64 69 74 69 6f 6e 61 6c 20 6d 65 74 68 6f 64 73  ditional methods
a530: 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e   may be added in
a540: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
a550: 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.};../*.** CA
a560: 50 49 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64  PI3REF: Standard
a570: 20 46 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70   File Control Op
a580: 63 6f 64 65 73 20 7b 48 31 31 33 31 30 7d 20 3c  codes {H11310} <
a590: 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30800>.**.** Th
a5a0: 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  ese integer cons
a5b0: 74 61 6e 74 73 20 61 72 65 20 6f 70 63 6f 64 65  tants are opcode
a5c0: 73 20 66 6f 72 20 74 68 65 20 78 46 69 6c 65 43  s for the xFileC
a5d0: 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a  ontrol method.**
a5e0: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
a5f0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  _io_methods] obj
a600: 65 63 74 20 61 6e 64 20 66 6f 72 20 74 68 65 20  ect and for the 
a610: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f  [sqlite3_file_co
a620: 6e 74 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65  ntrol()].** inte
a630: 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rface..**.** The
a640: 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c   [SQLITE_FCNTL_L
a650: 4f 43 4b 53 54 41 54 45 5d 20 6f 70 63 6f 64 65  OCKSTATE] opcode
a660: 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62   is used for deb
a670: 75 67 67 69 6e 67 2e 20 20 54 68 69 73 0a 2a 2a  ugging.  This.**
a680: 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20 74   opcode causes t
a690: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  he xFileControl 
a6a0: 6d 65 74 68 6f 64 20 74 6f 20 77 72 69 74 65 20  method to write 
a6b0: 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
a6c0: 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b  e of.** the lock
a6d0: 20 28 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45   (one of [SQLITE
a6e0: 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51  _LOCK_NONE], [SQ
a6f0: 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44  LITE_LOCK_SHARED
a700: 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f  ],.** [SQLITE_LO
a710: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 20 5b 53  CK_RESERVED], [S
a720: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49  QLITE_LOCK_PENDI
a730: 4e 47 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  NG], or [SQLITE_
a740: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 29  LOCK_EXCLUSIVE])
a750: 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65  .** into an inte
a760: 67 65 72 20 74 68 61 74 20 74 68 65 20 70 41 72  ger that the pAr
a770: 67 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74  g argument point
a780: 73 20 74 6f 2e 20 54 68 69 73 20 63 61 70 61 62  s to. This capab
a790: 69 6c 69 74 79 0a 2a 2a 20 69 73 20 75 73 65 64  ility.** is used
a7a0: 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 20   during testing 
a7b0: 61 6e 64 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74  and only needs t
a7c0: 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 20 77  o be supported w
a7d0: 68 65 6e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  hen SQLITE_TEST.
a7e0: 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  ** is defined..*
a7f0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
a800: 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
a810: 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
a820: 65 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43  e SQLITE_GET_LOC
a830: 4b 50 52 4f 58 59 46 49 4c 45 20 20 20 20 20 20  KPROXYFILE      
a840: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
a850: 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
a860: 4c 45 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e  LE      3.#defin
a870: 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52  e SQLITE_LAST_ER
a880: 52 4e 4f 20 20 20 20 20 20 20 20 20 20 20 20 20  RNO             
a890: 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  4../*.** CAPI3RE
a8a0: 46 3a 20 4d 75 74 65 78 20 48 61 6e 64 6c 65 20  F: Mutex Handle 
a8b0: 7b 48 31 37 31 31 30 7d 20 3c 53 32 30 31 33 30  {H17110} <S20130
a8c0: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65  >.**.** The mute
a8d0: 78 20 6d 6f 64 75 6c 65 20 77 69 74 68 69 6e 20  x module within 
a8e0: 53 51 4c 69 74 65 20 64 65 66 69 6e 65 73 20 5b  SQLite defines [
a8f0: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 74  sqlite3_mutex] t
a900: 6f 20 62 65 20 61 6e 0a 2a 2a 20 61 62 73 74 72  o be an.** abstr
a910: 61 63 74 20 74 79 70 65 20 66 6f 72 20 61 20 6d  act type for a m
a920: 75 74 65 78 20 6f 62 6a 65 63 74 2e 20 20 54 68  utex object.  Th
a930: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6e 65  e SQLite core ne
a940: 76 65 72 20 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20  ver looks.** at 
a950: 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
a960: 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  resentation of a
a970: 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  n [sqlite3_mutex
a980: 5d 2e 20 20 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64  ].  It only.** d
a990: 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65  eals with pointe
a9a0: 72 73 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  rs to the [sqlit
a9b0: 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74  e3_mutex] object
a9c0: 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 65 78 65 73 20  ..**.** Mutexes 
a9d0: 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e  are created usin
a9e0: 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  g [sqlite3_mutex
a9f0: 5f 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79  _alloc()]..*/.ty
aa00: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
aa10: 69 74 65 33 5f 6d 75 74 65 78 20 73 71 6c 69 74  ite3_mutex sqlit
aa20: 65 33 5f 6d 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a  e3_mutex;../*.**
aa30: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e   CAPI3REF: OS In
aa40: 74 65 72 66 61 63 65 20 4f 62 6a 65 63 74 20 7b  terface Object {
aa50: 48 31 31 31 34 30 7d 20 3c 53 32 30 31 30 30 3e  H11140} <S20100>
aa60: 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  .**.** An instan
aa70: 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ce of the sqlite
aa80: 33 5f 76 66 73 20 6f 62 6a 65 63 74 20 64 65 66  3_vfs object def
aa90: 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  ines the interfa
aaa0: 63 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68  ce between.** th
aab0: 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 61 6e  e SQLite core an
aac0: 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  d the underlying
aad0: 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
aae0: 6d 2e 20 20 54 68 65 20 22 76 66 73 22 0a 2a 2a  m.  The "vfs".**
aaf0: 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   in the name of 
ab00: 74 68 65 20 6f 62 6a 65 63 74 20 73 74 61 6e 64  the object stand
ab10: 73 20 66 6f 72 20 22 76 69 72 74 75 61 6c 20 66  s for "virtual f
ab20: 69 6c 65 20 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a  ile system"..**.
ab30: 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
ab40: 74 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65  the iVersion fie
ab50: 6c 64 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20  ld is initially 
ab60: 31 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72  1 but may be lar
ab70: 67 65 72 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65  ger in.** future
ab80: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
ab90: 69 74 65 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ite.  Additional
aba0: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61   fields may be a
abb0: 70 70 65 6e 64 65 64 20 74 6f 20 74 68 69 73 0a  ppended to this.
abc0: 2a 2a 20 6f 62 6a 65 63 74 20 77 68 65 6e 20 74  ** object when t
abd0: 68 65 20 69 56 65 72 73 69 6f 6e 20 76 61 6c 75  he iVersion valu
abe0: 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 2e 20  e is increased. 
abf0: 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73   Note that the s
ac00: 74 72 75 63 74 75 72 65 0a 2a 2a 20 6f 66 20 74  tructure.** of t
ac10: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f  he sqlite3_vfs o
ac20: 62 6a 65 63 74 20 63 68 61 6e 67 65 73 20 69 6e  bject changes in
ac30: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
ac40: 20 62 65 74 77 65 65 6e 0a 2a 2a 20 53 51 4c 69   between.** SQLi
ac50: 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 39  te version 3.5.9
ac60: 20 61 6e 64 20 33 2e 36 2e 30 20 61 6e 64 20 79   and 3.6.0 and y
ac70: 65 74 20 74 68 65 20 69 56 65 72 73 69 6f 6e 20  et the iVersion 
ac80: 66 69 65 6c 64 20 77 61 73 20 6e 6f 74 0a 2a 2a  field was not.**
ac90: 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a   modified..**.**
aca0: 20 54 68 65 20 73 7a 4f 73 46 69 6c 65 20 66 69   The szOsFile fi
acb0: 65 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20  eld is the size 
acc0: 6f 66 20 74 68 65 20 73 75 62 63 6c 61 73 73 65  of the subclasse
acd0: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d  d [sqlite3_file]
ace0: 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 75 73  .** structure us
acf0: 65 64 20 62 79 20 74 68 69 73 20 56 46 53 2e 20  ed by this VFS. 
ad00: 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 74   mxPathname is t
ad10: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
ad20: 68 20 6f 66 0a 2a 2a 20 61 20 70 61 74 68 6e 61  h of.** a pathna
ad30: 6d 65 20 69 6e 20 74 68 69 73 20 56 46 53 2e 0a  me in this VFS..
ad40: 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 65 64  **.** Registered
ad50: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a   sqlite3_vfs obj
ad60: 65 63 74 73 20 61 72 65 20 6b 65 70 74 20 6f 6e  ects are kept on
ad70: 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 66   a linked list f
ad80: 6f 72 6d 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  ormed by.** the 
ad90: 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e 20 20  pNext pointer.  
ada0: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  The [sqlite3_vfs
adb0: 5f 72 65 67 69 73 74 65 72 28 29 5d 0a 2a 2a 20  _register()].** 
adc0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  and [sqlite3_vfs
add0: 5f 75 6e 72 65 67 69 73 74 65 72 28 29 5d 20 69  _unregister()] i
ade0: 6e 74 65 72 66 61 63 65 73 20 6d 61 6e 61 67 65  nterfaces manage
adf0: 20 74 68 69 73 20 6c 69 73 74 0a 2a 2a 20 69 6e   this list.** in
ae00: 20 61 20 74 68 72 65 61 64 2d 73 61 66 65 20 77   a thread-safe w
ae10: 61 79 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65  ay.  The [sqlite
ae20: 33 5f 76 66 73 5f 66 69 6e 64 28 29 5d 20 69 6e  3_vfs_find()] in
ae30: 74 65 72 66 61 63 65 0a 2a 2a 20 73 65 61 72 63  terface.** searc
ae40: 68 65 73 20 74 68 65 20 6c 69 73 74 2e 20 20 4e  hes the list.  N
ae50: 65 69 74 68 65 72 20 74 68 65 20 61 70 70 6c 69  either the appli
ae60: 63 61 74 69 6f 6e 20 63 6f 64 65 20 6e 6f 72 20  cation code nor 
ae70: 74 68 65 20 56 46 53 0a 2a 2a 20 69 6d 70 6c 65  the VFS.** imple
ae80: 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64  mentation should
ae90: 20 75 73 65 20 74 68 65 20 70 4e 65 78 74 20 70   use the pNext p
aea0: 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ointer..**.** Th
aeb0: 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 69 73  e pNext field is
aec0: 20 74 68 65 20 6f 6e 6c 79 20 66 69 65 6c 64 20   the only field 
aed0: 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  in the sqlite3_v
aee0: 66 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  fs.** structure 
aef0: 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c  that SQLite will
af00: 20 65 76 65 72 20 6d 6f 64 69 66 79 2e 20 20 53   ever modify.  S
af10: 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  QLite will only 
af20: 61 63 63 65 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64  access.** or mod
af30: 69 66 79 20 74 68 69 73 20 66 69 65 6c 64 20 77  ify this field w
af40: 68 69 6c 65 20 68 6f 6c 64 69 6e 67 20 61 20 70  hile holding a p
af50: 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 69 63  articular static
af60: 20 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 61   mutex..** The a
af70: 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c  pplication shoul
af80: 64 20 6e 65 76 65 72 20 6d 6f 64 69 66 79 20 61  d never modify a
af90: 6e 79 74 68 69 6e 67 20 77 69 74 68 69 6e 20 74  nything within t
afa0: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a  he sqlite3_vfs.*
afb0: 2a 20 6f 62 6a 65 63 74 20 6f 6e 63 65 20 74 68  * object once th
afc0: 65 20 6f 62 6a 65 63 74 20 68 61 73 20 62 65 65  e object has bee
afd0: 6e 20 72 65 67 69 73 74 65 72 65 64 2e 0a 2a 2a  n registered..**
afe0: 0a 2a 2a 20 54 68 65 20 7a 4e 61 6d 65 20 66 69  .** The zName fi
aff0: 65 6c 64 20 68 6f 6c 64 73 20 74 68 65 20 6e 61  eld holds the na
b000: 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20 6d 6f  me of the VFS mo
b010: 64 75 6c 65 2e 20 20 54 68 65 20 6e 61 6d 65 20  dule.  The name 
b020: 6d 75 73 74 0a 2a 2a 20 62 65 20 75 6e 69 71 75  must.** be uniqu
b030: 65 20 61 63 72 6f 73 73 20 61 6c 6c 20 56 46 53  e across all VFS
b040: 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20   modules..**.** 
b050: 53 51 4c 69 74 65 20 77 69 6c 6c 20 67 75 61 72  SQLite will guar
b060: 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 7a  antee that the z
b070: 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74  Filename paramet
b080: 65 72 20 74 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69  er to xOpen.** i
b090: 73 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 20  s either a NULL 
b0a0: 70 6f 69 6e 74 65 72 20 6f 72 20 73 74 72 69 6e  pointer or strin
b0b0: 67 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  g obtained.** fr
b0c0: 6f 6d 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65  om xFullPathname
b0d0: 28 29 2e 20 20 53 51 4c 69 74 65 20 66 75 72 74  ().  SQLite furt
b0e0: 68 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74  her guarantees t
b0f0: 68 61 74 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e  hat.** the strin
b100: 67 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64 20  g will be valid 
b110: 61 6e 64 20 75 6e 63 68 61 6e 67 65 64 20 75 6e  and unchanged un
b120: 74 69 6c 20 78 43 6c 6f 73 65 28 29 20 69 73 0a  til xClose() is.
b130: 2a 2a 20 63 61 6c 6c 65 64 2e 20 42 65 63 61 75  ** called. Becau
b140: 73 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f  se of the previo
b150: 75 73 20 73 65 6e 74 65 6e 73 65 2c 0a 2a 2a 20  us sentense,.** 
b160: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  the [sqlite3_fil
b170: 65 5d 20 63 61 6e 20 73 61 66 65 6c 79 20 73 74  e] can safely st
b180: 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ore a pointer to
b190: 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65   the.** filename
b1a0: 20 69 66 20 69 74 20 6e 65 65 64 73 20 74 6f 20   if it needs to 
b1b0: 72 65 6d 65 6d 62 65 72 20 74 68 65 20 66 69 6c  remember the fil
b1c0: 65 6e 61 6d 65 20 66 6f 72 20 73 6f 6d 65 20 72  ename for some r
b1d0: 65 61 73 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65  eason..** If the
b1e0: 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d   zFilename param
b1f0: 65 74 65 72 20 69 73 20 78 4f 70 65 6e 20 69 73  eter is xOpen is
b200: 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
b210: 74 68 65 6e 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75  then xOpen.** mu
b220: 73 74 20 69 6e 76 69 74 65 20 69 74 73 20 6f 77  st invite its ow
b230: 6e 20 74 65 6d 70 6f 72 61 72 79 20 6e 61 6d 65  n temporary name
b240: 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 20   for the file.  
b250: 57 68 65 6e 65 76 65 72 20 74 68 65 20 0a 2a 2a  Whenever the .**
b260: 20 78 46 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d   xFilename param
b270: 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 69 74 20  eter is NULL it 
b280: 77 69 6c 6c 20 61 6c 73 6f 20 62 65 20 74 68 65  will also be the
b290: 20 63 61 73 65 20 74 68 61 74 20 74 68 65 0a 2a   case that the.*
b2a0: 2a 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  * flags paramete
b2b0: 72 20 77 69 6c 6c 20 69 6e 63 6c 75 64 65 20 5b  r will include [
b2c0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
b2d0: 54 45 4f 4e 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a  TEONCLOSE]..**.*
b2e0: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75  * The flags argu
b2f0: 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 29 20  ment to xOpen() 
b300: 69 6e 63 6c 75 64 65 73 20 61 6c 6c 20 62 69 74  includes all bit
b310: 73 20 73 65 74 20 69 6e 0a 2a 2a 20 74 68 65 20  s set in.** the 
b320: 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74  flags argument t
b330: 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  o [sqlite3_open_
b340: 76 32 28 29 5d 2e 20 20 4f 72 20 69 66 20 5b 73  v2()].  Or if [s
b350: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a  qlite3_open()].*
b360: 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70  * or [sqlite3_op
b370: 65 6e 31 36 28 29 5d 20 69 73 20 75 73 65 64 2c  en16()] is used,
b380: 20 74 68 65 6e 20 66 6c 61 67 73 20 69 6e 63 6c   then flags incl
b390: 75 64 65 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a  udes at least.**
b3a0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   [SQLITE_OPEN_RE
b3b0: 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49  ADWRITE] | [SQLI
b3c0: 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 2e  TE_OPEN_CREATE].
b3d0: 20 0a 2a 2a 20 49 66 20 78 4f 70 65 6e 28 29 20   .** If xOpen() 
b3e0: 6f 70 65 6e 73 20 61 20 66 69 6c 65 20 72 65 61  opens a file rea
b3f0: 64 2d 6f 6e 6c 79 20 74 68 65 6e 20 69 74 20 73  d-only then it s
b400: 65 74 73 20 2a 70 4f 75 74 46 6c 61 67 73 20 74  ets *pOutFlags t
b410: 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 5b 53 51  o.** include [SQ
b420: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
b430: 4c 59 5d 2e 20 20 4f 74 68 65 72 20 62 69 74 73  LY].  Other bits
b440: 20 69 6e 20 2a 70 4f 75 74 46 6c 61 67 73 20 6d   in *pOutFlags m
b450: 61 79 20 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  ay be set..**.**
b460: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73   SQLite will als
b470: 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 65  o add one of the
b480: 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73   following flags
b490: 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 28 29 0a   to the xOpen().
b4a0: 2a 2a 20 63 61 6c 6c 2c 20 64 65 70 65 6e 64 69  ** call, dependi
b4b0: 6e 67 20 6f 6e 20 74 68 65 20 6f 62 6a 65 63 74  ng on the object
b4c0: 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a   being opened:.*
b4d0: 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
b4e0: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >  [SQLITE_OPEN_
b4f0: 4d 41 49 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e  MAIN_DB].** <li>
b500: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d    [SQLITE_OPEN_M
b510: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20  AIN_JOURNAL].** 
b520: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
b530: 45 4e 5f 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c  EN_TEMP_DB].** <
b540: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45  li>  [SQLITE_OPE
b550: 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a  N_TEMP_JOURNAL].
b560: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45  ** <li>  [SQLITE
b570: 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f  _OPEN_TRANSIENT_
b580: 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51  DB].** <li>  [SQ
b590: 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55  LITE_OPEN_SUBJOU
b5a0: 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b  RNAL].** <li>  [
b5b0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
b5c0: 45 52 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c  ER_JOURNAL].** <
b5d0: 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  /ul>.**.** The f
b5e0: 69 6c 65 20 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e  ile I/O implemen
b5f0: 74 61 74 69 6f 6e 20 63 61 6e 20 75 73 65 20 74  tation can use t
b600: 68 65 20 6f 62 6a 65 63 74 20 74 79 70 65 20 66  he object type f
b610: 6c 61 67 73 20 74 6f 0a 2a 2a 20 63 68 61 6e 67  lags to.** chang
b620: 65 20 74 68 65 20 77 61 79 20 69 74 20 64 65 61  e the way it dea
b630: 6c 73 20 77 69 74 68 20 66 69 6c 65 73 2e 20 20  ls with files.  
b640: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20  For example, an 
b650: 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 74  application.** t
b660: 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 63 61 72  hat does not car
b670: 65 20 61 62 6f 75 74 20 63 72 61 73 68 20 72 65  e about crash re
b680: 63 6f 76 65 72 79 20 6f 72 20 72 6f 6c 6c 62 61  covery or rollba
b690: 63 6b 20 6d 69 67 68 74 20 6d 61 6b 65 0a 2a 2a  ck might make.**
b6a0: 20 74 68 65 20 6f 70 65 6e 20 6f 66 20 61 20 6a   the open of a j
b6b0: 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 20 6e 6f  ournal file a no
b6c0: 2d 6f 70 2e 20 20 57 72 69 74 65 73 20 74 6f 20  -op.  Writes to 
b6d0: 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 6f 75  this journal wou
b6e0: 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 6e 6f  ld.** also be no
b6f0: 2d 6f 70 73 2c 20 61 6e 64 20 61 6e 79 20 61 74  -ops, and any at
b700: 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68  tempt to read th
b710: 65 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 20  e journal would 
b720: 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
b730: 5f 49 4f 45 52 52 2e 20 20 4f 72 20 74 68 65 20  _IOERR.  Or the 
b740: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d  implementation m
b750: 69 67 68 74 20 72 65 63 6f 67 6e 69 7a 65 20 74  ight recognize t
b760: 68 61 74 20 61 20 64 61 74 61 62 61 73 65 0a 2a  hat a database.*
b770: 2a 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64  * file will be d
b780: 6f 69 6e 67 20 70 61 67 65 2d 61 6c 69 67 6e 65  oing page-aligne
b790: 64 20 73 65 63 74 6f 72 20 72 65 61 64 73 20 61  d sector reads a
b7a0: 6e 64 20 77 72 69 74 65 73 20 69 6e 20 61 20 72  nd writes in a r
b7b0: 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 64 65 72 20 61  andom.** order a
b7c0: 6e 64 20 73 65 74 20 75 70 20 69 74 73 20 49 2f  nd set up its I/
b7d0: 4f 20 73 75 62 73 79 73 74 65 6d 20 61 63 63 6f  O subsystem acco
b7e0: 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53  rdingly..**.** S
b7f0: 51 4c 69 74 65 20 6d 69 67 68 74 20 61 6c 73 6f  QLite might also
b800: 20 61 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20   add one of the 
b810: 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20  following flags 
b820: 74 6f 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74  to the xOpen met
b830: 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  hod:.**.** <ul>.
b840: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
b850: 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
b860: 53 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  SE].** <li> [SQL
b870: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
b880: 56 45 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  VE].** </ul>.**.
b890: 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f  ** The [SQLITE_O
b8a0: 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
b8b0: 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68  E] flag means th
b8c0: 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  e file should be
b8d0: 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e  .** deleted when
b8e0: 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 20 20   it is closed.  
b8f0: 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  The [SQLITE_OPEN
b900: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a  _DELETEONCLOSE].
b910: 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 66  ** will be set f
b920: 6f 72 20 54 45 4d 50 20 20 64 61 74 61 62 61 73  or TEMP  databas
b930: 65 73 2c 20 6a 6f 75 72 6e 61 6c 73 20 61 6e 64  es, journals and
b940: 20 66 6f 72 20 73 75 62 6a 6f 75 72 6e 61 6c 73   for subjournals
b950: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c  ..**.** The [SQL
b960: 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
b970: 56 45 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74  VE] flag means t
b980: 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  he file should b
b990: 65 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20  e opened.** for 
b9a0: 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73  exclusive access
b9b0: 2e 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 20  .  This flag is 
b9c0: 73 65 74 20 66 6f 72 20 61 6c 6c 20 66 69 6c 65  set for all file
b9d0: 73 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20  s except.** for 
b9e0: 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
b9f0: 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 74  e file..**.** At
ba00: 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c 65 20   least szOsFile 
ba10: 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
ba20: 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  are allocated by
ba30: 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20 68 6f   SQLite.** to ho
ba40: 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74 65 33  ld the  [sqlite3
ba50: 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75 72 65  _file] structure
ba60: 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 74   passed as the t
ba70: 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  hird.** argument
ba80: 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 65 20   to xOpen.  The 
ba90: 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64 6f 65  xOpen method doe
baa0: 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a 2a 2a  s not have to.**
bab0: 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 74   allocate the st
bac0: 72 75 63 74 75 72 65 3b 20 69 74 20 73 68 6f 75  ructure; it shou
bad0: 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69 74 20  ld just fill it 
bae0: 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c  in..**.** The fl
baf0: 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ags argument to 
bb00: 78 41 63 63 65 73 73 28 29 20 6d 61 79 20 62 65  xAccess() may be
bb10: 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f   [SQLITE_ACCESS_
bb20: 45 58 49 53 54 53 5d 0a 2a 2a 20 74 6f 20 74 65  EXISTS].** to te
bb30: 73 74 20 66 6f 72 20 74 68 65 20 65 78 69 73 74  st for the exist
bb40: 65 6e 63 65 20 6f 66 20 61 20 66 69 6c 65 2c 20  ence of a file, 
bb50: 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53  or [SQLITE_ACCES
bb60: 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74 6f 0a  S_READWRITE] to.
bb70: 2a 2a 20 74 65 73 74 20 77 68 65 74 68 65 72 20  ** test whether 
bb80: 61 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62  a file is readab
bb90: 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2c  le and writable,
bba0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45   or [SQLITE_ACCE
bbb0: 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74 6f 20 74  SS_READ].** to t
bbc0: 65 73 74 20 77 68 65 74 68 65 72 20 61 20 66 69  est whether a fi
bbd0: 6c 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 72  le is at least r
bbe0: 65 61 64 61 62 6c 65 2e 20 20 20 54 68 65 20 66  eadable.   The f
bbf0: 69 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a 2a 20  ile can be a.** 
bc00: 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a  directory..**.**
bc10: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 77   SQLite will alw
bc20: 61 79 73 20 61 6c 6c 6f 63 61 74 65 20 61 74 20  ays allocate at 
bc30: 6c 65 61 73 74 20 6d 78 50 61 74 68 6e 61 6d 65  least mxPathname
bc40: 2b 31 20 62 79 74 65 73 20 66 6f 72 20 74 68 65  +1 bytes for the
bc50: 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65  .** output buffe
bc60: 72 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2e  r xFullPathname.
bc70: 20 20 54 68 65 20 65 78 61 63 74 20 73 69 7a 65    The exact size
bc80: 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62   of the output b
bc90: 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f  uffer.** is also
bca0: 20 70 61 73 73 65 64 20 61 73 20 61 20 70 61 72   passed as a par
bcb0: 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20 20  ameter to both  
bcc0: 6d 65 74 68 6f 64 73 2e 20 49 66 20 74 68 65 20  methods. If the 
bcd0: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a  output buffer.**
bce0: 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65 6e   is not large en
bcf0: 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 5f 43 41  ough, [SQLITE_CA
bd00: 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c 64 20 62  NTOPEN] should b
bd10: 65 20 72 65 74 75 72 6e 65 64 2e 20 53 69 6e 63  e returned. Sinc
bd20: 65 20 74 68 69 73 20 69 73 0a 2a 2a 20 68 61 6e  e this is.** han
bd30: 64 6c 65 64 20 61 73 20 61 20 66 61 74 61 6c 20  dled as a fatal 
bd40: 65 72 72 6f 72 20 62 79 20 53 51 4c 69 74 65 2c  error by SQLite,
bd50: 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74   vfs implementat
bd60: 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 6e 64 65  ions should ende
bd70: 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72 65 76 65  avor.** to preve
bd80: 6e 74 20 74 68 69 73 20 62 79 20 73 65 74 74 69  nt this by setti
bd90: 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 20 74 6f  ng mxPathname to
bda0: 20 61 20 73 75 66 66 69 63 69 65 6e 74 6c 79 20   a sufficiently 
bdb0: 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a  large value..**.
bdc0: 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65  ** The xRandomne
bdd0: 73 73 28 29 2c 20 78 53 6c 65 65 70 28 29 2c 20  ss(), xSleep(), 
bde0: 61 6e 64 20 78 43 75 72 72 65 6e 74 54 69 6d 65  and xCurrentTime
bdf0: 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  () interfaces.**
be00: 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 6c   are not strictl
be10: 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20  y a part of the 
be20: 66 69 6c 65 73 79 73 74 65 6d 2c 20 62 75 74 20  filesystem, but 
be30: 74 68 65 79 20 61 72 65 0a 2a 2a 20 69 6e 63 6c  they are.** incl
be40: 75 64 65 64 20 69 6e 20 74 68 65 20 56 46 53 20  uded in the VFS 
be50: 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 63 6f  structure for co
be60: 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a 2a 20 54  mpleteness..** T
be70: 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 28 29  he xRandomness()
be80: 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70   function attemp
be90: 74 73 20 74 6f 20 72 65 74 75 72 6e 20 6e 42 79  ts to return nBy
bea0: 74 65 73 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20  tes bytes.** of 
beb0: 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20 72 61 6e  good-quality ran
bec0: 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 4f 75  domness into zOu
bed0: 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  t.  The return v
bee0: 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 61  alue is.** the a
bef0: 63 74 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  ctual number of 
bf00: 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e  bytes of randomn
bf10: 65 73 73 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2a  ess obtained..**
bf20: 20 54 68 65 20 78 53 6c 65 65 70 28 29 20 6d 65   The xSleep() me
bf30: 74 68 6f 64 20 63 61 75 73 65 73 20 74 68 65 20  thod causes the 
bf40: 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 74  calling thread t
bf50: 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0a 2a  o sleep for at.*
bf60: 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d 62  * least the numb
bf70: 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e  er of microsecon
bf80: 64 73 20 67 69 76 65 6e 2e 20 20 54 68 65 20 78  ds given.  The x
bf90: 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0a 2a 2a  CurrentTime().**
bfa0: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
bfb0: 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d  a Julian Day Num
bfc0: 62 65 72 20 66 6f 72 20 74 68 65 20 63 75 72 72  ber for the curr
bfd0: 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 6d  ent date and tim
bfe0: 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 70 65 64 65 66  e..**.*/.typedef
bff0: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
c000: 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 3b  vfs sqlite3_vfs;
c010: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
c020: 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72  vfs {.  int iVer
c030: 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  sion;           
c040: 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65   /* Structure ve
c050: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  rsion number */.
c060: 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b 20    int szOsFile; 
c070: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
c080: 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 64  ze of subclassed
c090: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2f   sqlite3_file */
c0a0: 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 6d  .  int mxPathnam
c0b0: 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  e;          /* M
c0c0: 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 68  aximum file path
c0d0: 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  name length */. 
c0e0: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e   sqlite3_vfs *pN
c0f0: 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
c100: 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53  t registered VFS
c110: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
c120: 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f   *zName;       /
c130: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 76  * Name of this v
c140: 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74  irtual file syst
c150: 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  em */.  void *pA
c160: 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  ppData;         
c170: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
c180: 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 69  pplication-speci
c190: 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  fic data */.  in
c1a0: 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74  t (*xOpen)(sqlit
c1b0: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
c1c0: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c 69  har *zName, sqli
c1d0: 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 20  te3_file*,.     
c1e0: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c            int fl
c1f0: 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 6c  ags, int *pOutFl
c200: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  ags);.  int (*xD
c210: 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f 76  elete)(sqlite3_v
c220: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
c230: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e 63  *zName, int sync
c240: 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 41  Dir);.  int (*xA
c250: 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f 76  ccess)(sqlite3_v
c260: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
c270: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67  *zName, int flag
c280: 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  s, int *pResOut)
c290: 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c 50  ;.  int (*xFullP
c2a0: 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  athname)(sqlite3
c2b0: 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61  _vfs*, const cha
c2c0: 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4f  r *zName, int nO
c2d0: 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b  ut, char *zOut);
c2e0: 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f 70  .  void *(*xDlOp
c2f0: 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  en)(sqlite3_vfs*
c300: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
c310: 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f 69 64  ilename);.  void
c320: 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71 6c   (*xDlError)(sql
c330: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e  ite3_vfs*, int n
c340: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 72  Byte, char *zErr
c350: 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 28  Msg);.  void (*(
c360: 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65 33  *xDlSym)(sqlite3
c370: 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f 6e  _vfs*,void*, con
c380: 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c  st char *zSymbol
c390: 29 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64  ))(void);.  void
c3a0: 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73 71 6c   (*xDlClose)(sql
c3b0: 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69 64 2a  ite3_vfs*, void*
c3c0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61 6e 64  );.  int (*xRand
c3d0: 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65 33 5f  omness)(sqlite3_
c3e0: 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c  vfs*, int nByte,
c3f0: 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20 20   char *zOut);.  
c400: 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28 73 71  int (*xSleep)(sq
c410: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20  lite3_vfs*, int 
c420: 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b 0a 20  microseconds);. 
c430: 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e 74 54   int (*xCurrentT
c440: 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ime)(sqlite3_vfs
c450: 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20 20 69  *, double*);.  i
c460: 6e 74 20 28 2a 78 47 65 74 4c 61 73 74 45 72 72  nt (*xGetLastErr
c470: 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  or)(sqlite3_vfs*
c480: 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a  , int, char *);.
c490: 20 20 2f 2a 20 4e 65 77 20 66 69 65 6c 64 73 20    /* New fields 
c4a0: 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64 20  may be appended 
c4b0: 69 6e 20 66 69 67 75 72 65 20 76 65 72 73 69 6f  in figure versio
c4c0: 6e 73 2e 20 20 54 68 65 20 69 56 65 72 73 69 6f  ns.  The iVersio
c4d0: 6e 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c  n.  ** value wil
c4e0: 6c 20 69 6e 63 72 65 6d 65 6e 74 20 77 68 65 6e  l increment when
c4f0: 65 76 65 72 20 74 68 69 73 20 68 61 70 70 65 6e  ever this happen
c500: 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  s. */.};../*.** 
c510: 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67 73 20  CAPI3REF: Flags 
c520: 66 6f 72 20 74 68 65 20 78 41 63 63 65 73 73 20  for the xAccess 
c530: 56 46 53 20 6d 65 74 68 6f 64 20 7b 48 31 31 31  VFS method {H111
c540: 39 30 7d 20 3c 48 31 31 31 34 30 3e 0a 2a 2a 0a  90} <H11140>.**.
c550: 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72  ** These integer
c560: 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e 20 62   constants can b
c570: 65 20 75 73 65 64 20 61 73 20 74 68 65 20 74 68  e used as the th
c580: 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ird parameter to
c590: 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73 73 20  .** the xAccess 
c5a0: 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73 71  method of an [sq
c5b0: 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
c5c0: 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79 20 64  t. {END}  They d
c5d0: 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 61 74  etermine.** what
c5e0: 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69 73 73   kind of permiss
c5f0: 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65 73 73  ions the xAccess
c600: 20 6d 65 74 68 6f 64 20 69 73 20 6c 6f 6f 6b 69   method is looki
c610: 6e 67 20 66 6f 72 2e 0a 2a 2a 20 57 69 74 68 20  ng for..** With 
c620: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
c630: 49 53 54 53 2c 20 74 68 65 20 78 41 63 63 65 73  ISTS, the xAcces
c640: 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 73 69 6d 70  s method.** simp
c650: 6c 79 20 63 68 65 63 6b 73 20 77 68 65 74 68 65  ly checks whethe
c660: 72 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74  r the file exist
c670: 73 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54  s..** With SQLIT
c680: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
c690: 54 45 2c 20 74 68 65 20 78 41 63 63 65 73 73 20  TE, the xAccess 
c6a0: 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73  method.** checks
c6b0: 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c   whether the fil
c6c0: 65 20 69 73 20 62 6f 74 68 20 72 65 61 64 61 62  e is both readab
c6d0: 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2e  le and writable.
c6e0: 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f  .** With SQLITE_
c6f0: 41 43 43 45 53 53 5f 52 45 41 44 2c 20 74 68 65  ACCESS_READ, the
c700: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a   xAccess method.
c710: 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65  ** checks whethe
c720: 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65  r the file is re
c730: 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69  adable..*/.#defi
c740: 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  ne SQLITE_ACCESS
c750: 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64 65  _EXISTS    0.#de
c760: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45  fine SQLITE_ACCE
c770: 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 0a 23  SS_READWRITE 1.#
c780: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43  define SQLITE_AC
c790: 43 45 53 53 5f 52 45 41 44 20 20 20 20 20 20 32  CESS_READ      2
c7a0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
c7b0: 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 65  : Initialize The
c7c0: 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 20   SQLite Library 
c7d0: 7b 48 31 30 31 33 30 7d 20 3c 53 32 30 30 30 30  {H10130} <S20000
c7e0: 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20  ><S30100>.**.** 
c7f0: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  The sqlite3_init
c800: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65  ialize() routine
c810: 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65   initializes the
c820: 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72 61  .** SQLite libra
c830: 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  ry.  The sqlite3
c840: 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f 75 74  _shutdown() rout
c850: 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74  ine.** deallocat
c860: 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73  es any resources
c870: 20 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f 63   that were alloc
c880: 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ated by sqlite3_
c890: 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a  initialize()..**
c8a0: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71  .** A call to sq
c8b0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
c8c0: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74  () is an "effect
c8d0: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20  ive" call if it 
c8e0: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  is.** the first 
c8f0: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  time sqlite3_ini
c900: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
c910: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20  oked during the 
c920: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74  lifetime of.** t
c930: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69  he process, or i
c940: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73  f it is the firs
c950: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69  t time sqlite3_i
c960: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
c970: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77  nvoked.** follow
c980: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
c990: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
c9a0: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63  .  Only an effec
c9b0: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20  tive call.** of 
c9c0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
c9d0: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e  ze() does any in
c9e0: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41  itialization.  A
c9f0: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a  ll other calls.*
ca00: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  * are harmless n
ca10: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63  o-ops..**.** A c
ca20: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  all to sqlite3_s
ca30: 68 75 74 64 6f 77 6e 28 29 20 69 73 20 61 6e 20  hutdown() is an 
ca40: 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c  "effective" call
ca50: 20 69 66 20 69 74 20 69 73 20 74 68 65 20 66 69   if it is the fi
ca60: 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73  rst.** call to s
ca70: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
ca80: 29 20 73 69 6e 63 65 20 74 68 65 20 6c 61 73 74  ) since the last
ca90: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
caa0: 69 7a 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20  ize().  Only.** 
cab0: 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 6c  an effective cal
cac0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  l to sqlite3_shu
cad0: 74 64 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e 79  tdown() does any
cae0: 20 64 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f   deinitializatio
caf0: 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20  n..** All other 
cb00: 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
cb10: 5f 73 68 75 74 64 6f 77 6e 28 29 20 61 72 65 20  _shutdown() are 
cb20: 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e  harmless no-ops.
cb30: 0a 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68  .**.** Among oth
cb40: 65 72 20 74 68 69 6e 67 73 2c 20 73 71 6c 69 74  er things, sqlit
cb50: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
cb60: 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a 20  shall invoke.** 
cb70: 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
cb80: 29 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 73  ).  Similarly, s
cb90: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
cba0: 29 0a 2a 2a 20 73 68 61 6c 6c 20 69 6e 76 6f 6b  ).** shall invok
cbb0: 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  e sqlite3_os_end
cbc0: 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ()..**.** The sq
cbd0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
cbe0: 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  () routine retur
cbf0: 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f  ns [SQLITE_OK] o
cc00: 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 49 66  n success..** If
cc10: 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e   for some reason
cc20: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  , sqlite3_initia
cc30: 6c 69 7a 65 28 29 20 69 73 20 75 6e 61 62 6c 65  lize() is unable
cc40: 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a   to initialize.*
cc50: 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 28 70  * the library (p
cc60: 65 72 68 61 70 73 20 69 74 20 69 73 20 75 6e 61  erhaps it is una
cc70: 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
cc80: 61 20 6e 65 65 64 65 64 20 72 65 73 6f 75 72 63  a needed resourc
cc90: 65 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 20 6d  e such.** as a m
cca0: 75 74 65 78 29 20 69 74 20 72 65 74 75 72 6e 73  utex) it returns
ccb0: 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   an [error code]
ccc0: 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c   other than [SQL
ccd0: 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 54  ITE_OK]..**.** T
cce0: 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  he sqlite3_initi
ccf0: 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 20  alize() routine 
cd00: 69 73 20 63 61 6c 6c 65 64 20 69 6e 74 65 72 6e  is called intern
cd10: 61 6c 6c 79 20 62 79 20 6d 61 6e 79 20 6f 74 68  ally by many oth
cd20: 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74  er.** SQLite int
cd30: 65 72 66 61 63 65 73 20 73 6f 20 74 68 61 74 20  erfaces so that 
cd40: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 75  an application u
cd50: 73 75 61 6c 6c 79 20 64 6f 65 73 20 6e 6f 74 20  sually does not 
cd60: 6e 65 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b  need to.** invok
cd70: 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  e sqlite3_initia
cd80: 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 2e  lize() directly.
cd90: 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 5b    For example, [
cda0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a  sqlite3_open()].
cdb0: 2a 2a 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33  ** calls sqlite3
cdc0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 6f  _initialize() so
cdd0: 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
cde0: 61 72 79 20 77 69 6c 6c 20 62 65 20 61 75 74 6f  ary will be auto
cdf0: 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 69 6e 69  matically.** ini
ce00: 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 5b 73  tialized when [s
ce10: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 69  qlite3_open()] i
ce20: 73 20 63 61 6c 6c 65 64 20 69 66 20 69 74 20 68  s called if it h
ce30: 61 73 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61  as not be initia
ce40: 6c 69 7a 65 64 0a 2a 2a 20 61 6c 72 65 61 64 79  lized.** already
ce50: 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 53  .  However, if S
ce60: 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
ce70: 64 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c 49  d with the [SQLI
ce80: 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
ce90: 5d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  ].** compile-tim
cea0: 65 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 20 74  e option, then t
ceb0: 68 65 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c  he automatic cal
cec0: 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e  ls to sqlite3_in
ced0: 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 61 72  itialize().** ar
cee0: 65 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 74 68  e omitted and th
cef0: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  e application mu
cf00: 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f  st call sqlite3_
cf10: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 72  initialize() dir
cf20: 65 63 74 6c 79 0a 2a 2a 20 70 72 69 6f 72 20 74  ectly.** prior t
cf30: 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 65  o using any othe
cf40: 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  r SQLite interfa
cf50: 63 65 2e 20 20 46 6f 72 20 6d 61 78 69 6d 75 6d  ce.  For maximum
cf60: 20 70 6f 72 74 61 62 69 6c 69 74 79 2c 0a 2a 2a   portability,.**
cf70: 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64   it is recommend
cf80: 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74  ed that applicat
cf90: 69 6f 6e 73 20 61 6c 77 61 79 73 20 69 6e 76 6f  ions always invo
cfa0: 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ke sqlite3_initi
cfb0: 61 6c 69 7a 65 28 29 0a 2a 2a 20 64 69 72 65 63  alize().** direc
cfc0: 74 6c 79 20 70 72 69 6f 72 20 74 6f 20 75 73 69  tly prior to usi
cfd0: 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c  ng any other SQL
cfe0: 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20  ite interface.  
cff0: 46 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 0a  Future releases.
d000: 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79  ** of SQLite may
d010: 20 72 65 71 75 69 72 65 20 74 68 69 73 2e 20 20   require this.  
d020: 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
d030: 74 68 65 20 62 65 68 61 76 69 6f 72 20 65 78 68  the behavior exh
d040: 69 62 69 74 65 64 0a 2a 2a 20 77 68 65 6e 20 53  ibited.** when S
d050: 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
d060: 64 20 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 4f  d with [SQLITE_O
d070: 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20 6d 69  MIT_AUTOINIT] mi
d080: 67 68 74 20 62 65 63 6f 6d 65 20 74 68 65 0a 2a  ght become the.*
d090: 2a 20 64 65 66 61 75 6c 74 20 62 65 68 61 76 69  * default behavi
d0a0: 6f 72 20 69 6e 20 73 6f 6d 65 20 66 75 74 75 72  or in some futur
d0b0: 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c  e release of SQL
d0c0: 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ite..**.** The s
d0d0: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
d0e0: 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6f 70   routine does op
d0f0: 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73  erating-system s
d100: 70 65 63 69 66 69 63 0a 2a 2a 20 69 6e 69 74 69  pecific.** initi
d110: 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 65  alization of the
d120: 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
d130: 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73    The sqlite3_os
d140: 5f 65 6e 64 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  _end().** routin
d150: 65 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66  e undoes the eff
d160: 65 63 74 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f  ect of sqlite3_o
d170: 73 5f 69 6e 69 74 28 29 2e 20 20 54 79 70 69 63  s_init().  Typic
d180: 61 6c 20 74 61 73 6b 73 0a 2a 2a 20 70 65 72 66  al tasks.** perf
d190: 6f 72 6d 65 64 20 62 79 20 74 68 65 73 65 20 72  ormed by these r
d1a0: 6f 75 74 69 6e 65 73 20 69 6e 63 6c 75 64 65 20  outines include 
d1b0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 64 65  allocation or de
d1c0: 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66  allocation.** of
d1d0: 20 73 74 61 74 69 63 20 72 65 73 6f 75 72 63 65   static resource
d1e0: 73 2c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  s, initializatio
d1f0: 6e 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69  n of global vari
d200: 61 62 6c 65 73 2c 0a 2a 2a 20 73 65 74 74 69 6e  ables,.** settin
d210: 67 20 75 70 20 61 20 64 65 66 61 75 6c 74 20 5b  g up a default [
d220: 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6d 6f 64  sqlite3_vfs] mod
d230: 75 6c 65 2c 20 6f 72 20 73 65 74 74 69 6e 67 20  ule, or setting 
d240: 75 70 0a 2a 2a 20 61 20 64 65 66 61 75 6c 74 20  up.** a default 
d250: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 75 73  configuration us
d260: 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  ing [sqlite3_con
d270: 66 69 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  fig()]..**.** Th
d280: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68  e application sh
d290: 6f 75 6c 64 20 6e 65 76 65 72 20 69 6e 76 6f 6b  ould never invok
d2a0: 65 20 65 69 74 68 65 72 20 73 71 6c 69 74 65 33  e either sqlite3
d2b0: 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 6f 72  _os_init().** or
d2c0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
d2d0: 29 20 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65  ) directly.  The
d2e0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f   application sho
d2f0: 75 6c 64 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 0a  uld only invoke.
d300: 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ** sqlite3_initi
d310: 61 6c 69 7a 65 28 29 20 61 6e 64 20 73 71 6c 69  alize() and sqli
d320: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20  te3_shutdown(). 
d330: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f   The sqlite3_os_
d340: 69 6e 69 74 28 29 0a 2a 2a 20 69 6e 74 65 72 66  init().** interf
d350: 61 63 65 20 69 73 20 63 61 6c 6c 65 64 20 61 75  ace is called au
d360: 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 73  tomatically by s
d370: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
d380: 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74  e() and.** sqlit
d390: 65 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73 20 63  e3_os_end() is c
d3a0: 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 33  alled by sqlite3
d3b0: 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 41 70  _shutdown().  Ap
d3c0: 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 69 6d 70  propriate.** imp
d3d0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72  lementations for
d3e0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
d3f0: 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f  () and sqlite3_o
d400: 73 5f 65 6e 64 28 29 0a 2a 2a 20 61 72 65 20 62  s_end().** are b
d410: 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65  uilt into SQLite
d420: 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70   when it is comp
d430: 69 6c 65 64 20 66 6f 72 20 75 6e 69 78 2c 20 77  iled for unix, w
d440: 69 6e 64 6f 77 73 2c 20 6f 72 20 6f 73 2f 32 2e  indows, or os/2.
d450: 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 74 20 66  .** When built f
d460: 6f 72 20 6f 74 68 65 72 20 70 6c 61 74 66 6f 72  or other platfor
d470: 6d 73 20 28 75 73 69 6e 67 20 74 68 65 20 5b 53  ms (using the [S
d480: 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31  QLITE_OS_OTHER=1
d490: 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a  ] compile-time.*
d4a0: 2a 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 70  * option) the ap
d4b0: 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73  plication must s
d4c0: 75 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c 65  upply a suitable
d4d0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
d4e0: 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f  for.** sqlite3_o
d4f0: 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c  s_init() and sql
d500: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20  ite3_os_end().  
d510: 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73  An application-s
d520: 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65  upplied.** imple
d530: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c  mentation of sql
d540: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f  ite3_os_init() o
d550: 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  r sqlite3_os_end
d560: 28 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 72  ().** must retur
d570: 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e  n [SQLITE_OK] on
d580: 20 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f 6d   success and som
d590: 65 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 63  e other [error c
d5a0: 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69  ode] upon.** fai
d5b0: 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lure..*/.SQLITE_
d5c0: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
d5d0: 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29  initialize(void)
d5e0: 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
d5f0: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
d600: 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  n(void);.SQLITE_
d610: 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
d620: 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53  os_init(void);.S
d630: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
d640: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69  lite3_os_end(voi
d650: 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  d);../*.** CAPI3
d660: 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67  REF: Configuring
d670: 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72   The SQLite Libr
d680: 61 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53 32  ary {H14100} <S2
d690: 30 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a  0000><S30200>.**
d6a0: 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
d6b0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
d6c0: 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61  config() interfa
d6d0: 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61  ce is used to ma
d6e0: 6b 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67  ke global config
d6f0: 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67  uration.** chang
d700: 65 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e 20  es to SQLite in 
d710: 6f 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53 51  order to tune SQ
d720: 4c 69 74 65 20 74 6f 20 74 68 65 20 73 70 65 63  Lite to the spec
d730: 69 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a  ific needs of.**
d740: 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
d750: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 63  .  The default c
d760: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73 20  onfiguration is 
d770: 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20  recommended for 
d780: 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74  most.** applicat
d790: 69 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69 73  ions and so this
d7a0: 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61   routine is usua
d7b0: 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61 72  lly not necessar
d7c0: 79 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f  y.  It is.** pro
d7d0: 76 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74  vided to support
d7e0: 20 72 61 72 65 20 61 70 70 6c 69 63 61 74 69 6f   rare applicatio
d7f0: 6e 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c 20  ns with unusual 
d800: 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  needs..**.** The
d810: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
d820: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e  ) interface is n
d830: 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20  ot threadsafe.  
d840: 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a  The application.
d850: 2a 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20 74  ** must insure t
d860: 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c  hat no other SQL
d870: 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61  ite interfaces a
d880: 72 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74  re invoked by ot
d890: 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20 77  her.** threads w
d8a0: 68 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e  hile sqlite3_con
d8b0: 66 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e 67  fig() is running
d8c0: 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20  .  Furthermore, 
d8d0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
d8e0: 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  .** may only be 
d8f0: 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f  invoked prior to
d900: 20 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61 6c   library initial
d910: 69 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a  ization using.**
d920: 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61   [sqlite3_initia
d930: 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72  lize()] or after
d940: 20 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73 71   shutdown by [sq
d950: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
d960: 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65  ]..** Note, howe
d970: 76 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74 65  ver, that sqlite
d980: 33 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20 62  3_config() can b
d990: 65 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74  e called as part
d9a0: 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65   of the.** imple
d9b0: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20  mentation of an 
d9c0: 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
d9d0: 6e 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f  ned [sqlite3_os_
d9e0: 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54  init()]..**.** T
d9f0: 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
da00: 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e  t to sqlite3_con
da10: 66 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74 65  fig() is an inte
da20: 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43  ger.** [SQLITE_C
da30: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
da40: 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69  AD | configurati
da50: 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20  on option] that 
da60: 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68  determines.** wh
da70: 61 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20 53  at property of S
da80: 51 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20 63  QLite is to be c
da90: 6f 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62 73  onfigured.  Subs
daa0: 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73  equent arguments
dab0: 0a 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64 69  .** vary dependi
dac0: 6e 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54  ng on the [SQLIT
dad0: 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54  E_CONFIG_SINGLET
dae0: 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72  HREAD | configur
daf0: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a  ation option].**
db00: 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72   in the first ar
db10: 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68  gument..**.** Wh
db20: 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74 69  en a configurati
db30: 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74  on option is set
db40: 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  , sqlite3_config
db50: 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  () returns [SQLI
db60: 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68  TE_OK]..** If th
db70: 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e  e option is unkn
db80: 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69 73  own or SQLite is
db90: 20 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20 74   unable to set t
dba0: 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65  he option.** the
dbb0: 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  n this routine r
dbc0: 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72  eturns a non-zer
dbd0: 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a  o [error code]..
dbe0: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
dbf0: 74 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20  ts:.** [H14103] 
dc00: 5b 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32 30  [H14106] [H14120
dc10: 5d 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34 31  ] [H14123] [H141
dc20: 32 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48 31  26] [H14129] [H1
dc30: 34 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a  4132] [H14135].*
dc40: 2a 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34 31  * [H14138] [H141
dc50: 34 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48 31  41] [H14144] [H1
dc60: 34 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b  4147] [H14150] [
dc70: 48 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36 5d  H14153] [H14156]
dc80: 20 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31   [H14159].** [H1
dc90: 34 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b  4162] [H14165] [
dca0: 48 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54  H14168].*/.SQLIT
dcb0: 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50  E_API SQLITE_EXP
dcc0: 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71  ERIMENTAL int sq
dcd0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74  lite3_config(int
dce0: 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  , ...);../*.** C
dcf0: 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75  API3REF: Configu
dd00: 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  re database conn
dd10: 65 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30 30  ections  {H14200
dd20: 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58  } <S20000>.** EX
dd30: 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
dd40: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 5f   The sqlite3_db_
dd50: 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61  config() interfa
dd60: 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61  ce is used to ma
dd70: 6b 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ke configuration
dd80: 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 61  .** changes to a
dd90: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
dda0: 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74  ction].  The int
ddb0: 65 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c 61  erface is simila
ddc0: 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  r to.** [sqlite3
ddd0: 5f 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65 70  _config()] excep
dde0: 74 20 74 68 61 74 20 74 68 65 20 63 68 61 6e 67  t that the chang
ddf0: 65 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73 69  es apply to a si
de00: 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73  ngle.** [databas
de10: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73  e connection] (s
de20: 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
de30: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29 2e  first argument).
de40: 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33    The.** sqlite3
de50: 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74  _db_config() int
de60: 65 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20  erface can only 
de70: 62 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61 74  be used immediat
de80: 65 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68 65  ely after.** the
de90: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
dea0: 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 20  tion is created 
deb0: 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f  using [sqlite3_o
dec0: 70 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  pen()],.** [sqli
ded0: 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f  te3_open16()], o
dee0: 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  r [sqlite3_open_
def0: 76 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  v2()].  .**.** T
df00: 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
df10: 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64 62  nt to sqlite3_db
df20: 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29  _config(D,V,...)
df30: 20 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66    is the.** conf
df40: 69 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20 2d  iguration verb -
df50: 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65   an integer code
df60: 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 20   that indicates 
df70: 77 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f  what.** aspect o
df80: 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  f the [database 
df90: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62  connection] is b
dfa0: 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 2e  eing configured.
dfb0: 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f  .** The only cho
dfc0: 69 63 65 20 66 6f 72 20 74 68 69 73 20 76 61 6c  ice for this val
dfd0: 75 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42  ue is [SQLITE_DB
dfe0: 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
dff0: 5d 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 20  ]..** New verbs 
e000: 61 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65  are likely to be
e010: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
e020: 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c   releases of SQL
e030: 69 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e  ite..** Addition
e040: 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65 70  al arguments dep
e050: 65 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62 2e  end on the verb.
e060: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
e070: 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d  nts:.** [H14203]
e080: 20 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32 30   [H14206] [H1420
e090: 39 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31 34  9] [H14212] [H14
e0a0: 32 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  215].*/.SQLITE_A
e0b0: 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49  PI SQLITE_EXPERI
e0c0: 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74  MENTAL int sqlit
e0d0: 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c  e3_db_config(sql
e0e0: 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e  ite3*, int op, .
e0f0: 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ..);../*.** CAPI
e100: 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c  3REF: Memory All
e110: 6f 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73  ocation Routines
e120: 20 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32   {H10155} <S2012
e130: 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
e140: 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  AL.**.** An inst
e150: 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
e160: 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  ect defines the 
e170: 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65  interface betwee
e180: 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20  n SQLite.** and 
e190: 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
e1a0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74   allocation rout
e1b0: 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ines..**.** This
e1c0: 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 20   object is used 
e1d0: 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63  in only one plac
e1e0: 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20  e in the SQLite 
e1f0: 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20  interface..** A 
e200: 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e  pointer to an in
e210: 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
e220: 62 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 67  bject is the arg
e230: 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c  ument to.** [sql
e240: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77  ite3_config()] w
e250: 68 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72  hen the configur
e260: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a  ation option is.
e270: 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
e280: 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 42 79 20 63  G_MALLOC].  By c
e290: 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61  reating an insta
e2a0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
e2b0: 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e  ct.** and passin
e2c0: 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  g it to [sqlite3
e2d0: 5f 63 6f 6e 66 69 67 28 29 5d 20 64 75 72 69 6e  _config()] durin
e2e0: 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c  g configuration,
e2f0: 20 61 6e 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69   an.** applicati
e300: 6f 6e 20 63 61 6e 20 73 70 65 63 69 66 79 20 61  on can specify a
e310: 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 65  n alternative me
e320: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
e330: 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 66 6f 72  subsystem.** for
e340: 20 53 51 4c 69 74 65 20 74 6f 20 75 73 65 20 66   SQLite to use f
e350: 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79  or all of its dy
e360: 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65  namic memory nee
e370: 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  ds..**.** Note t
e380: 68 61 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73  hat SQLite comes
e390: 20 77 69 74 68 20 61 20 62 75 69 6c 74 2d 69 6e   with a built-in
e3a0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
e3b0: 72 20 74 68 61 74 20 69 73 0a 2a 2a 20 70 65 72  r that is.** per
e3c0: 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 20  fectly adequate 
e3d0: 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 6c  for the overwhel
e3e0: 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f 66  ming majority of
e3f0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   applications.**
e400: 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 6f   and that this o
e410: 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 73  bject is only us
e420: 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 6d  eful to a tiny m
e430: 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69  inority of appli
e440: 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 20  cations.** with 
e450: 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d 6f  specialized memo
e460: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65  ry allocation re
e470: 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 69  quirements.  Thi
e480: 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 61  s object is.** a
e490: 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 20  lso used during 
e4a0: 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 74  testing of SQLit
e4b0: 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70  e in order to sp
e4c0: 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61  ecify an alterna
e4d0: 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61  tive.** memory a
e4e0: 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 69  llocator that si
e4f0: 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 6f  mulates memory o
e500: 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e  ut-of-memory con
e510: 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72  ditions in.** or
e520: 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68  der to verify th
e530: 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 65  at SQLite recove
e540: 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 72  rs gracefully fr
e550: 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 69  om such.** condi
e560: 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tions..**.** The
e570: 20 78 4d 61 6c 6c 6f 63 2c 20 78 46 72 65 65 2c   xMalloc, xFree,
e580: 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 20 6d 65   and xRealloc me
e590: 74 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20  thods must work 
e5a0: 6c 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c  like the.** mall
e5b0: 6f 63 28 29 2c 20 66 72 65 65 28 29 2c 20 61 6e  oc(), free(), an
e5c0: 64 20 72 65 61 6c 6c 6f 63 28 29 20 66 75 6e 63  d realloc() func
e5d0: 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73  tions from the s
e5e0: 74 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 2e  tandard library.
e5f0: 0a 2a 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f  .**.** xSize sho
e600: 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 61  uld return the a
e610: 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66  llocated size of
e620: 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
e630: 74 69 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73  tion.** previous
e640: 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ly obtained from
e650: 20 78 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61   xMalloc or xRea
e660: 6c 6c 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63  lloc.  The alloc
e670: 61 74 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20  ated size.** is 
e680: 61 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20  always at least 
e690: 61 73 20 62 69 67 20 61 73 20 74 68 65 20 72 65  as big as the re
e6a0: 71 75 65 73 74 65 64 20 73 69 7a 65 20 62 75 74  quested size but
e6b0: 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a   may be larger..
e6c0: 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64  **.** The xRound
e6d0: 75 70 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e  up method return
e6e0: 73 20 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20  s what would be 
e6f0: 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69  the allocated si
e700: 7a 65 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72  ze of.** a memor
e710: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76  y allocation giv
e720: 65 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  en a particular 
e730: 72 65 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20  requested size. 
e740: 20 4d 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20   Most memory.** 
e750: 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64  allocators round
e760: 20 75 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   up memory alloc
e770: 61 74 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20  ations at least 
e780: 74 6f 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74  to the next mult
e790: 69 70 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53  iple.** of 8.  S
e7a0: 6f 6d 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72  ome allocators r
e7b0: 6f 75 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72  ound up to a lar
e7c0: 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20  ger multiple or 
e7d0: 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e  to a power of 2.
e7e0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74  .**.** The xInit
e7f0: 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 6c 69   method initiali
e800: 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  zes the memory a
e810: 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f 72 20  llocator.  (For 
e820: 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d  example,.** it m
e830: 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 6e  ight allocate an
e840: 79 20 72 65 71 75 69 72 65 20 6d 75 74 65 78 65  y require mutexe
e850: 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a 65 20  s or initialize 
e860: 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 2a 2a  internal data.**
e870: 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 54 68   structures.  Th
e880: 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68  e xShutdown meth
e890: 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69  od is invoked (i
e8a0: 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a 2a 2a  ndirectly) by.**
e8b0: 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f   [sqlite3_shutdo
e8c0: 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 6c 64  wn()] and should
e8d0: 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20   deallocate any 
e8e0: 72 65 73 6f 75 72 63 65 73 20 61 63 71 75 69 72  resources acquir
e8f0: 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 2e 20  ed.** by xInit. 
e900: 20 54 68 65 20 70 41 70 70 44 61 74 61 20 70 6f   The pAppData po
e910: 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73  inter is used as
e920: 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 6d 65   the only parame
e930: 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 74 20  ter to.** xInit 
e940: 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e 0a 2a  and xShutdown..*
e950: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
e960: 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
e970: 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 6d  hods sqlite3_mem
e980: 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74  _methods;.struct
e990: 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
e9a0: 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 28  hods {.  void *(
e9b0: 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 20  *xMalloc)(int); 
e9c0: 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
e9d0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e  y allocation fun
e9e0: 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ction */.  void 
e9f0: 28 2a 78 46 72 65 65 29 28 76 6f 69 64 2a 29 3b  (*xFree)(void*);
ea00: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65            /* Fre
ea10: 65 20 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61  e a prior alloca
ea20: 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
ea30: 28 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 64  (*xRealloc)(void
ea40: 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 69  *,int);  /* Resi
ea50: 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  ze an allocation
ea60: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 7a   */.  int (*xSiz
ea70: 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  e)(void*);      
ea80: 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74       /* Return t
ea90: 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 6c  he size of an al
eaa0: 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  location */.  in
eab0: 74 20 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 6e  t (*xRoundup)(in
eac0: 74 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t);          /* 
ead0: 52 6f 75 6e 64 20 75 70 20 72 65 71 75 65 73 74  Round up request
eae0: 20 73 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 74   size to allocat
eaf0: 69 6f 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e  ion size */.  in
eb00: 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a  t (*xInit)(void*
eb10: 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  );           /* 
eb20: 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
eb30: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
eb40: 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 75  */.  void (*xShu
eb50: 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 20  tdown)(void*);  
eb60: 20 20 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 6c      /* Deinitial
eb70: 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
eb80: 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f  llocator */.  vo
eb90: 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 20  id *pAppData;   
eba0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ebb0: 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e 69  Argument to xIni
ebc0: 74 28 29 20 61 6e 64 20 78 53 68 75 74 64 6f 77  t() and xShutdow
ebd0: 6e 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  n() */.};../*.**
ebe0: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69   CAPI3REF: Confi
ebf0: 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73  guration Options
ec00: 20 7b 48 31 30 31 36 30 7d 20 3c 53 32 30 30 30   {H10160} <S2000
ec10: 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
ec20: 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  AL.**.** These c
ec30: 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65  onstants are the
ec40: 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67   available integ
ec50: 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  er configuration
ec60: 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a   options that.**
ec70: 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61   can be passed a
ec80: 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
ec90: 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c  ment to the [sql
eca0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69  ite3_config()] i
ecb0: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
ecc0: 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  New configuratio
ecd0: 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 65  n options may be
ece0: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
ecf0: 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c   releases of SQL
ed00: 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67  ite..** Existing
ed10: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
ed20: 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20  ptions might be 
ed30: 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41  discontinued.  A
ed40: 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73  pplications.** s
ed50: 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20  hould check the 
ed60: 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  return code from
ed70: 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
ed80: 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ()] to make sure
ed90: 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c   that.** the cal
eda0: 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b  l worked.  The [
edb0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
edc0: 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c  ] interface will
edd0: 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e   return a.** non
ede0: 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64  -zero [error cod
edf0: 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69  e] if a disconti
ee00: 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72  nued or unsuppor
ee10: 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ted configuratio
ee20: 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69  n option.** is i
ee30: 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64  nvoked..**.** <d
ee40: 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  l>.** <dt>SQLITE
ee50: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
ee60: 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  READ</dt>.** <dd
ee70: 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72  >There are no ar
ee80: 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
ee90: 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70  option.  This op
eea0: 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a  tion disables.**
eeb0: 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 6e   all mutexing an
eec0: 64 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 6e  d puts SQLite in
eed0: 74 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 20  to a mode where 
eee0: 69 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75  it can only be u
eef0: 73 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e 67  sed.** by a sing
ef00: 6c 65 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e 0a  le thread.</dd>.
ef10: 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
ef20: 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52  _CONFIG_MULTITHR
ef30: 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  EAD</dt>.** <dd>
ef40: 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67  There are no arg
ef50: 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f  uments to this o
ef60: 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74  ption.  This opt
ef70: 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20  ion disables.** 
ef80: 6d 75 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 74  mutexing on [dat
ef90: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
efa0: 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20  ] and [prepared 
efb0: 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 63  statement] objec
efc0: 74 73 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  ts..** The appli
efd0: 63 61 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e  cation is respon
efe0: 73 69 62 6c 65 20 66 6f 72 20 73 65 72 69 61 6c  sible for serial
eff0: 69 7a 69 6e 67 20 61 63 63 65 73 73 20 74 6f 0a  izing access to.
f000: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
f010: 6e 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70  nections] and [p
f020: 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
f030: 74 73 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 20  ts].  But other 
f040: 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65  mutexes.** are e
f050: 6e 61 62 6c 65 64 20 73 6f 20 74 68 61 74 20 53  nabled so that S
f060: 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 73 61  QLite will be sa
f070: 66 65 20 74 6f 20 75 73 65 20 69 6e 20 61 20 6d  fe to use in a m
f080: 75 6c 74 69 2d 74 68 72 65 61 64 65 64 0a 2a 2a  ulti-threaded.**
f090: 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 20   environment as 
f0a0: 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 74  long as no two t
f0b0: 68 72 65 61 64 73 20 61 74 74 65 6d 70 74 20 74  hreads attempt t
f0c0: 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 2a  o use the same.*
f0d0: 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
f0e0: 65 63 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 73  ection] at the s
f0f0: 61 6d 65 20 74 69 6d 65 2e 20 20 53 65 65 20 74  ame time.  See t
f100: 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f  he [threading mo
f110: 64 65 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61  de].** documenta
f120: 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f  tion for additio
f130: 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
f140: 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
f150: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45  SQLITE_CONFIG_SE
f160: 52 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a  RIALIZED</dt>.**
f170: 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 6e   <dd>There are n
f180: 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  o arguments to t
f190: 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69  his option.  Thi
f1a0: 73 20 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 73  s option enables
f1b0: 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 20  .** all mutexes 
f1c0: 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 72 65  including the re
f1d0: 63 75 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 78  cursive.** mutex
f1e0: 65 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20  es on [database 
f1f0: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20  connection] and 
f200: 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
f210: 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a  ent] objects..**
f220: 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 77   In this mode (w
f230: 68 69 63 68 20 69 73 20 74 68 65 20 64 65 66 61  hich is the defa
f240: 75 6c 74 20 77 68 65 6e 20 53 51 4c 69 74 65 20  ult when SQLite 
f250: 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
f260: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 45  .** [SQLITE_THRE
f270: 41 44 53 41 46 45 3d 31 5d 29 20 74 68 65 20 53  ADSAFE=1]) the S
f280: 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69  QLite library wi
f290: 6c 6c 20 69 74 73 65 6c 66 20 73 65 72 69 61 6c  ll itself serial
f2a0: 69 7a 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f  ize access.** to
f2b0: 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
f2c0: 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65  ctions] and [pre
f2d0: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
f2e0: 5d 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a  ] so that the.**
f2f0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 20   application is 
f300: 66 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 20  free to use the 
f310: 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63  same [database c
f320: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 68  onnection] or th
f330: 65 0a 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 61  e.** same [prepa
f340: 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69  red statement] i
f350: 6e 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  n different thre
f360: 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 20  ads at the same 
f370: 74 69 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 65  time..** See the
f380: 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
f390: 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  ] documentation 
f3a0: 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
f3b0: 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e  nformation.</dd>
f3c0: 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
f3d0: 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c  E_CONFIG_MALLOC<
f3e0: 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
f3f0: 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
f400: 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
f410: 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
f420: 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
f430: 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c  ance of the [sql
f440: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
f450: 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  ] structure.  Th
f460: 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69  e argument speci
f470: 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74  fies.** alternat
f480: 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  ive low-level me
f490: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
f4a0: 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75  routines to be u
f4b0: 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a  sed in place of.
f4c0: 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ** the memory al
f4d0: 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  location routine
f4e0: 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c  s built into SQL
f4f0: 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ite.</dd>.**.** 
f500: 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
f510: 47 5f 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e  G_GETMALLOC</dt>
f520: 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
f530: 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
f540: 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
f550: 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
f560: 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
f570: 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
f580: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74  _mem_methods] st
f590: 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b 73  ructure.  The [s
f5a0: 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
f5b0: 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ds].** structure
f5c0: 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
f5d0: 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 65  the currently de
f5e0: 66 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c  fined memory all
f5f0: 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
f600: 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e  ..** This option
f610: 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
f620: 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66  overload the def
f630: 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ault memory allo
f640: 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e  cation.** routin
f650: 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65  es with a wrappe
f660: 72 20 74 68 61 74 20 73 69 6d 75 6c 61 74 69 6f  r that simulatio
f670: 6e 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ns memory alloca
f680: 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 0a  tion failure or.
f690: 2a 2a 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 79  ** tracks memory
f6a0: 20 75 73 61 67 65 2c 20 66 6f 72 20 65 78 61 6d   usage, for exam
f6b0: 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ple.</dd>.**.** 
f6c0: 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
f6d0: 47 5f 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 3e  G_MEMSTATUS</dt>
f6e0: 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
f6f0: 69 6f 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c 65  ion takes single
f700: 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70   argument of typ
f710: 65 20 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 74  e int, interpret
f720: 65 64 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c  ed as a .** bool
f730: 65 61 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 6c  ean, which enabl
f740: 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74  es or disables t
f750: 68 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66  he collection of
f760: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f770: 6f 6e 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 63  on .** statistic
f780: 73 2e 20 57 68 65 6e 20 64 69 73 61 62 6c 65 64  s. When disabled
f790: 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
f7a0: 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
f7b0: 73 20 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e  s become .** non
f7c0: 2d 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a  -operational:.**
f7d0: 20 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69     <ul>.**   <li
f7e0: 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  > [sqlite3_memor
f7f0: 79 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 3c  y_used()].**   <
f800: 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  li> [sqlite3_mem
f810: 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 5d  ory_highwater()]
f820: 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69  .**   <li> [sqli
f830: 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69  te3_soft_heap_li
f840: 6d 69 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e  mit()].**   <li>
f850: 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
f860: 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a  ()].**   </ul>.*
f870: 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  * </dd>.**.** <d
f880: 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
f890: 53 43 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20  SCRATCH</dt>.** 
f8a0: 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
f8b0: 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74  specifies a stat
f8c0: 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72  ic memory buffer
f8d0: 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e   that SQLite can
f8e0: 20 75 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 61   use for.** scra
f8f0: 74 63 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 65  tch memory.  The
f900: 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72 67  re are three arg
f910: 75 6d 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e 74  uments:  A point
f920: 65 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79  er to the memory
f930: 2c 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66  , the.** size of
f940: 20 65 61 63 68 20 73 63 72 61 74 63 68 20 62 75   each scratch bu
f950: 66 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 74  ffer (sz), and t
f960: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 75 66  he number of buf
f970: 66 65 72 73 20 28 4e 29 2e 20 20 54 68 65 20 73  fers (N).  The s
f980: 7a 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d 75  z.** argument mu
f990: 73 74 20 62 65 20 61 20 6d 75 6c 74 69 70 6c 65  st be a multiple
f9a0: 20 6f 66 20 31 36 2e 20 54 68 65 20 73 7a 20 70   of 16. The sz p
f9b0: 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
f9c0: 62 65 20 61 20 66 65 77 20 62 79 74 65 73 0a 2a  be a few bytes.*
f9d0: 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  * larger than th
f9e0: 65 20 61 63 74 75 61 6c 20 73 63 72 61 74 63 68  e actual scratch
f9f0: 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20   space required 
fa00: 64 75 65 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65  due internal ove
fa10: 72 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69  rhead..** The fi
fa20: 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  rst.** argument 
fa30: 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f 20  should point to 
fa40: 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66  an allocation of
fa50: 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 62   at least sz*N b
fa60: 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a  ytes of memory..
fa70: 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  ** SQLite will u
fa80: 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20  se no more than 
fa90: 6f 6e 65 20 73 63 72 61 74 63 68 20 62 75 66 66  one scratch buff
faa0: 65 72 20 61 74 20 6f 6e 63 65 20 70 65 72 20 74  er at once per t
fab0: 68 72 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73  hread, so.** N s
fac0: 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
fad0: 74 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 78  the expected max
fae0: 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74  imum number of t
faf0: 68 72 65 61 64 73 2e 20 20 54 68 65 20 73 7a 0a  hreads.  The sz.
fb00: 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f  ** parameter sho
fb10: 75 6c 64 20 62 65 20 36 20 74 69 6d 65 73 20 74  uld be 6 times t
fb20: 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c  he size of the l
fb30: 61 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20  argest database 
fb40: 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 63  page size..** Sc
fb50: 72 61 74 63 68 20 62 75 66 66 65 72 73 20 61 72  ratch buffers ar
fb60: 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f  e used as part o
fb70: 66 20 74 68 65 20 62 74 72 65 65 20 62 61 6c 61  f the btree bala
fb80: 6e 63 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  nce operation.  
fb90: 49 66 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 20  If.** The btree 
fba0: 62 61 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 61  balancer needs a
fbb0: 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79  dditional memory
fbc0: 20 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20   beyond what is 
fbd0: 70 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 73  provided by.** s
fbe0: 63 72 61 74 63 68 20 62 75 66 66 65 72 73 20 6f  cratch buffers o
fbf0: 72 20 69 66 20 6e 6f 20 73 63 72 61 74 63 68 20  r if no scratch 
fc00: 62 75 66 66 65 72 20 73 70 61 63 65 20 69 73 20  buffer space is 
fc10: 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20  specified, then 
fc20: 53 51 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 74  SQLite.** goes t
fc30: 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
fc40: 63 28 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74  c()] to obtain t
fc50: 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65  he memory it nee
fc60: 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ds.</dd>.**.** <
fc70: 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
fc80: 5f 50 41 47 45 43 41 43 48 45 3c 2f 64 74 3e 0a  _PAGECACHE</dt>.
fc90: 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69  ** <dd>This opti
fca0: 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73  on specifies a s
fcb0: 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66  tatic memory buf
fcc0: 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20  fer that SQLite 
fcd0: 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74  can use for.** t
fce0: 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
fcf0: 20 63 61 63 68 65 20 77 69 74 68 20 74 68 65 20   cache with the 
fd00: 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63  default page cac
fd10: 68 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e  he implemenation
fd20: 2e 20 20 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 66  .  .** This conf
fd30: 69 67 75 72 61 74 69 6f 6e 20 73 68 6f 75 6c 64  iguration should
fd40: 20 6e 6f 74 20 62 65 20 75 73 65 64 20 69 66 20   not be used if 
fd50: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  an application-d
fd60: 65 66 69 6e 65 20 70 61 67 65 0a 2a 2a 20 63 61  efine page.** ca
fd70: 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
fd80: 6f 6e 20 69 73 20 6c 6f 61 64 65 64 20 75 73 69  on is loaded usi
fd90: 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f  ng the SQLITE_CO
fda0: 4e 46 49 47 5f 50 43 41 43 48 45 20 6f 70 74 69  NFIG_PCACHE opti
fdb0: 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  on..** There are
fdc0: 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73   three arguments
fdd0: 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 3a   to this option:
fde0: 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
fdf0: 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 74 68 65  e.** memory, the
fe00: 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61   size of each pa
fe10: 67 65 20 62 75 66 66 65 72 20 28 73 7a 29 2c 20  ge buffer (sz), 
fe20: 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  and the number o
fe30: 66 20 70 61 67 65 73 20 28 4e 29 2e 0a 2a 2a 20  f pages (N)..** 
fe40: 54 68 65 20 73 7a 20 61 72 67 75 6d 65 6e 74 20  The sz argument 
fe50: 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20  must be a power 
fe60: 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35  of two between 5
fe70: 31 32 20 61 6e 64 20 33 32 37 36 38 2e 20 20 54  12 and 32768.  T
fe80: 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
fe90: 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e  ment should poin
fea0: 74 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69  t to an allocati
feb0: 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73  on of at least s
fec0: 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  z*N bytes of mem
fed0: 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77  ory..** SQLite w
fee0: 69 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f  ill use the memo
fef0: 72 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74  ry provided by t
ff00: 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
ff10: 74 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73  t to satisfy its
ff20: 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73  .** memory needs
ff30: 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e   for the first N
ff40: 20 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61   pages that it a
ff50: 64 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49  dds to cache.  I
ff60: 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  f additional.** 
ff70: 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  page cache memor
ff80: 79 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f  y is needed beyo
ff90: 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69  nd what is provi
ffa0: 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69  ded by this opti
ffb0: 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69  on, then.** SQLi
ffc0: 74 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69  te goes to [sqli
ffd0: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f  te3_malloc()] fo
ffe0: 72 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c  r the additional
fff0: 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a   storage space..
10000 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** The implement
10010 61 74 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20  ation might use 
10020 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
10030 68 65 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20  he N buffers to 
10040 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20  hold .** memory 
10050 61 63 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72  accounting infor
10060 6d 61 74 69 6f 6e 2e 20 3c 2f 64 64 3e 0a 2a 2a  mation. </dd>.**
10070 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
10080 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a  ONFIG_HEAP</dt>.
10090 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69  ** <dd>This opti
100a0 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73  on specifies a s
100b0 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66  tatic memory buf
100c0 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20  fer that SQLite 
100d0 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20  will use.** for 
100e0 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d  all of its dynam
100f0 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ic memory alloca
10100 74 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e  tion needs beyon
10110 64 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65 64  d those provided
10120 0a 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c 49  .** for by [SQLI
10130 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43  TE_CONFIG_SCRATC
10140 48 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43  H] and [SQLITE_C
10150 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d  ONFIG_PAGECACHE]
10160 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74  ..** There are t
10170 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20  hree arguments: 
10180 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
10190 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 6e 75 6d   memory, the num
101a0 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20  ber of.** bytes 
101b0 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 75  in the memory bu
101c0 66 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d 69  ffer, and the mi
101d0 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e  nimum allocation
101e0 20 73 69 7a 65 2e 20 20 49 66 0a 2a 2a 20 74 68   size.  If.** th
101f0 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20  e first pointer 
10200 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e  (the memory poin
10210 74 65 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68  ter) is NULL, th
10220 65 6e 20 53 51 4c 69 74 65 20 72 65 76 65 72 74  en SQLite revert
10230 73 0a 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74  s.** to using it
10240 73 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79  s default memory
10250 20 61 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20   allocator (the 
10260 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20  system malloc() 
10270 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c  implementation),
10280 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20  .** undoing any 
10290 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e  prior invocation
102a0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46   of [SQLITE_CONF
102b0 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20  IG_MALLOC].  If 
102c0 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f  the.** memory po
102d0 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c  inter is not NUL
102e0 4c 20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51  L and either [SQ
102f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
10300 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49  YS3] or.** [SQLI
10310 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
10320 35 5d 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  5] are defined, 
10330 74 68 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61  then the alterna
10340 74 69 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  tive memory.** a
10350 6c 6c 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61  llocator is enga
10360 67 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c  ged to handle al
10370 6c 20 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d  l of SQLites mem
10380 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e  ory allocation n
10390 65 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  eeds.</dd>.**.**
103a0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
103b0 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a  IG_MUTEX</dt>.**
103c0 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
103d0 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20   takes a single 
103e0 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69  argument which i
103f0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
10400 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  n.** instance of
10410 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75   the [sqlite3_mu
10420 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72  tex_methods] str
10430 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 67  ucture.  The arg
10440 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 0a  ument specifies.
10450 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c  ** alternative l
10460 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 72  ow-level mutex r
10470 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 75 73  outines to be us
10480 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a 2a 20 74  ed in place.** t
10490 68 65 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65  he mutex routine
104a0 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c  s built into SQL
104b0 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ite.</dd>.**.** 
104c0 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
104d0 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 74 3e 0a  G_GETMUTEX</dt>.
104e0 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69  ** <dd>This opti
104f0 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c  on takes a singl
10500 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68  e argument which
10510 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
10520 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20   an.** instance 
10530 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
10540 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 73  mutex_methods] s
10550 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 2a  tructure.  The.*
10560 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  * [sqlite3_mutex
10570 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 74 72  _methods].** str
10580 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c 65 64  ucture is filled
10590 20 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e   with the curren
105a0 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 75 74 65  tly defined mute
105b0 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54  x routines..** T
105c0 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62  his option can b
105d0 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f  e used to overlo
105e0 61 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d  ad the default m
105f0 75 74 65 78 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  utex allocation.
10600 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68  ** routines with
10610 20 61 20 77 72 61 70 70 65 72 20 75 73 65 64 20   a wrapper used 
10620 74 6f 20 74 72 61 63 6b 20 6d 75 74 65 78 20 75  to track mutex u
10630 73 61 67 65 20 66 6f 72 20 70 65 72 66 6f 72 6d  sage for perform
10640 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e  ance.** profilin
10650 67 20 6f 72 20 74 65 73 74 69 6e 67 2c 20 66 6f  g or testing, fo
10660 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a  r example.</dd>.
10670 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
10680 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  _CONFIG_LOOKASID
10690 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  E</dt>.** <dd>Th
106a0 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20  is option takes 
106b0 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 68  two arguments th
106c0 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  at determine the
106d0 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f   default.** memo
106e0 72 79 20 61 6c 6c 63 61 74 69 6f 6e 20 6c 6f 6f  ry allcation loo
106f0 6b 61 73 69 64 65 20 6f 70 74 69 6d 69 7a 61 74  kaside optimizat
10700 69 6f 6e 2e 20 20 54 68 65 20 66 69 72 73 74 20  ion.  The first 
10710 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a  argument is the.
10720 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20  ** size of each 
10730 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72  lookaside buffer
10740 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 73 65   slot and the se
10750 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d 62  cond is the numb
10760 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73 20 61  er of.** slots a
10770 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68  llocated to each
10780 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
10790 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  tion.</dd>.**.**
107a0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
107b0 49 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a  IG_PCACHE</dt>.*
107c0 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
107d0 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
107e0 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
107f0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a  is a pointer to.
10800 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70  ** an [sqlite3_p
10810 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f  cache_methods] o
10820 62 6a 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a  bject.  This obj
10830 65 63 74 20 73 70 65 63 69 66 69 65 73 20 74 68  ect specifies th
10840 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74  e interface.** t
10850 6f 20 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20  o a custom page 
10860 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  cache implementa
10870 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61  tion.  SQLite ma
10880 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68  kes a copy of th
10890 65 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20  e.** object and 
108a0 75 73 65 73 20 69 74 20 66 6f 72 20 70 61 67 65  uses it for page
108b0 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c   cache memory al
108c0 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a  locations.</dd>.
108d0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
108e0 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48  _CONFIG_GETPCACH
108f0 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  E</dt>.** <dd>Th
10900 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20  is option takes 
10910 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  a single argumen
10920 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69  t which is a poi
10930 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73  nter to an.** [s
10940 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
10950 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20  thods] object.  
10960 53 51 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66  SQLite copies of
10970 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
10980 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65  page cache imple
10990 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74  mentation into t
109a0 68 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e  hat object.</dd>
109b0 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a  .**.** </dl>.*/.
109c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
109d0 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
109e0 41 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f  AD  1  /* nil */
109f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
10a00 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45  CONFIG_MULTITHRE
10a10 41 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a  AD   2  /* nil *
10a20 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
10a30 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a  _CONFIG_SERIALIZ
10a40 45 44 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20  ED    3  /* nil 
10a50 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
10a60 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20  E_CONFIG_MALLOC 
10a70 20 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c         4  /* sql
10a80 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
10a90 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  * */.#define SQL
10aa0 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41  ITE_CONFIG_GETMA
10ab0 4c 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73  LLOC     5  /* s
10ac0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
10ad0 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ds* */.#define S
10ae0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52  QLITE_CONFIG_SCR
10af0 41 54 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a  ATCH       6  /*
10b00 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20   void*, int sz, 
10b10 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65  int N */.#define
10b20 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
10b30 41 47 45 43 41 43 48 45 20 20 20 20 20 37 20 20  AGECACHE     7  
10b40 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a  /* void*, int sz
10b50 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69  , int N */.#defi
10b60 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
10b70 5f 48 45 41 50 20 20 20 20 20 20 20 20 20 20 38  _HEAP          8
10b80 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20    /* void*, int 
10b90 6e 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a  nByte, int min *
10ba0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
10bb0 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55  _CONFIG_MEMSTATU
10bc0 53 20 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c  S     9  /* bool
10bd0 65 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ean */.#define S
10be0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54  QLITE_CONFIG_MUT
10bf0 45 58 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a  EX        10  /*
10c00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
10c10 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69  ethods* */.#defi
10c20 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
10c30 5f 47 45 54 4d 55 54 45 58 20 20 20 20 20 31 31  _GETMUTEX     11
10c40 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74    /* sqlite3_mut
10c50 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f  ex_methods* */./
10c60 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c  * previously SQL
10c70 49 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b  ITE_CONFIG_CHUNK
10c80 41 4c 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69  ALLOC 12 which i
10c90 73 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f  s now unused. */
10ca0 20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45   .#define SQLITE
10cb0 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  _CONFIG_LOOKASID
10cc0 45 20 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20  E    13  /* int 
10cd0 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  int */.#define S
10ce0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41  QLITE_CONFIG_PCA
10cf0 43 48 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a  CHE       14  /*
10d00 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
10d10 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66  methods* */.#def
10d20 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ine SQLITE_CONFI
10d30 47 5f 47 45 54 50 43 41 43 48 45 20 20 20 20 31  G_GETPCACHE    1
10d40 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63  5  /* sqlite3_pc
10d50 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f  ache_methods* */
10d60 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
10d70 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  : Configuration 
10d80 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d  Options {H10170}
10d90 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50   <S20000>.** EXP
10da0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
10db0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
10dc0 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c  are the availabl
10dd0 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67  e integer config
10de0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
10df0 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70  that.** can be p
10e00 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
10e10 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
10e20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f  the [sqlite3_db_
10e30 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66  config()] interf
10e40 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63  ace..**.** New c
10e50 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
10e60 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65  ions may be adde
10e70 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  d in future rele
10e80 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
10e90 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66  ** Existing conf
10ea0 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
10eb0 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f  s might be disco
10ec0 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63  ntinued.  Applic
10ed0 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64  ations.** should
10ee0 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72   check the retur
10ef0 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c  n code from [sql
10f00 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29  ite3_db_config()
10f10 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  ] to make sure t
10f20 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20  hat.** the call 
10f30 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71  worked.  The [sq
10f40 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28  lite3_db_config(
10f50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c  )] interface wil
10f60 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f  l return a.** no
10f70 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f  n-zero [error co
10f80 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74  de] if a discont
10f90 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f  inued or unsuppo
10fa0 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69  rted configurati
10fb0 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20  on option.** is 
10fc0 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c  invoked..**.** <
10fd0 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  dl>.** <dt>SQLIT
10fe0 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  E_DBCONFIG_LOOKA
10ff0 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SIDE</dt>.** <dd
11000 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b  >This option tak
11010 65 73 20 74 68 72 65 65 20 61 64 64 69 74 69 6f  es three additio
11020 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68  nal arguments th
11030 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  at determine the
11040 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20   .** [lookaside 
11050 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
11060 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  ] configuration 
11070 66 6f 72 20 74 68 65 20 5b 64 61 74 61 62 61 73  for the [databas
11080 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a  e connection]..*
11090 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
110a0 6d 65 6e 74 20 28 74 68 65 20 74 68 69 72 64 20  ment (the third 
110b0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
110c0 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28  lite3_db_config(
110d0 29 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74  )] is a.** point
110e0 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 62  er to a memory b
110f0 75 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72  uffer to use for
11100 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72   lookaside memor
11110 79 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a  y.  The first.**
11120 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65   argument may be
11130 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63   NULL in which c
11140 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  ase SQLite will 
11150 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 6c 6f 6f  allocate the loo
11160 6b 61 73 69 64 65 0a 2a 2a 20 62 75 66 66 65 72  kaside.** buffer
11170 20 69 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73   itself using [s
11180 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
11190 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  .  The second ar
111a0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a  gument is the.**
111b0 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f   size of each lo
111c0 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73  okaside buffer s
111d0 6c 6f 74 20 61 6e 64 20 74 68 65 20 74 68 69 72  lot and the thir
111e0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
111f0 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73  e number of.** s
11200 6c 6f 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20  lots.  The size 
11210 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69 6e  of the buffer in
11220 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
11230 65 6e 74 20 6d 75 73 74 20 62 65 20 67 72 65 61  ent must be grea
11240 74 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65  ter than.** or e
11250 71 75 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64  qual to the prod
11260 75 63 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e  uct of the secon
11270 64 20 61 6e 64 20 74 68 69 72 64 20 61 72 67 75  d and third argu
11280 6d 65 6e 74 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ments.</dd>.**.*
11290 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69  * </dl>.*/.#defi
112a0 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46  ne SQLITE_DBCONF
112b0 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20  IG_LOOKASIDE    
112c0 31 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69  1001  /* void* i
112d0 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  nt int */.../*.*
112e0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62  * CAPI3REF: Enab
112f0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78  le Or Disable Ex
11300 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f  tended Result Co
11310 64 65 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31  des {H12200} <S1
11320 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0700>.**.** The 
11330 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
11340 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20  _result_codes() 
11350 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20  routine enables 
11360 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a  or disables the.
11370 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73  ** [extended res
11380 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75  ult codes] featu
11390 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68  re of SQLite. Th
113a0 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  e extended resul
113b0 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64  t.** codes are d
113c0 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  isabled by defau
113d0 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  lt for historica
113e0 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  l compatibility 
113f0 63 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a  considerations..
11400 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
11410 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20  ts:.** [H12201] 
11420 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49  [H12202].*/.SQLI
11430 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
11440 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
11450 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33  lt_codes(sqlite3
11460 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a  *, int onoff);..
11470 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
11480 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69  Last Insert Rowi
11490 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37  d {H12220} <S107
114a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65  00>.**.** Each e
114b0 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74  ntry in an SQLit
114c0 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e  e table has a un
114d0 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e  ique 64-bit sign
114e0 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65  ed.** integer ke
114f0 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f  y called the [RO
11500 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20  WID | "rowid"]. 
11510 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77  The rowid is alw
11520 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  ays available.**
11530 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65   as an undeclare
11540 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52  d column named R
11550 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52  OWID, OID, or _R
11560 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73  OWID_ as long as
11570 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20   those.** names 
11580 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65  are not also use
11590 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20  d by explicitly 
115a0 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73  declared columns
115b0 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  . If.** the tabl
115c0 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f  e has a column o
115d0 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20  f type [INTEGER 
115e0 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65  PRIMARY KEY] the
115f0 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a  n that column.**
11600 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61   is another alia
11610 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e  s for the rowid.
11620 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
11630 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
11640 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d  [rowid] of the m
11650 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75  ost recent.** su
11660 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54  ccessful [INSERT
11670 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  ] into the datab
11680 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61  ase from the [da
11690 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
116a0 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72  n].** in the fir
116b0 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  st argument.  If
116c0 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b   no successful [
116d0 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65  INSERT]s.** have
116e0 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f   ever occurred o
116f0 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20  n that database 
11700 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f  connection, zero
11710 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
11720 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52  .** If an [INSER
11730 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  T] occurs within
11740 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e   a trigger, then
11750 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20   the [rowid] of 
11760 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20  the inserted.** 
11770 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20  row is returned 
11780 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
11790 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74  as long as the t
117a0 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e  rigger is runnin
117b0 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74  g..** But once t
117c0 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69  he trigger termi
117d0 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65  nates, the value
117e0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
117f0 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76  s routine.** rev
11800 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74  erts to the last
11810 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20   value inserted 
11820 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67  before the trigg
11830 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  er fired..**.** 
11840 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74  An [INSERT] that
11850 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20   fails due to a 
11860 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
11870 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a  tion is not a.**
11880 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53   successful [INS
11890 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ERT] and does no
118a0 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  t change the val
118b0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ue returned by t
118c0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20  his.** routine. 
118d0 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20   Thus INSERT OR 
118e0 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20  FAIL, INSERT OR 
118f0 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f  IGNORE, INSERT O
11900 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61  R ROLLBACK,.** a
11910 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f  nd INSERT OR ABO
11920 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67  RT make no chang
11930 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e  es to the return
11940 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a   value of this.*
11950 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74  * routine when t
11960 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66  heir insertion f
11970 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45  ails.  When INSE
11980 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a  RT OR REPLACE.**
11990 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f   encounters a co
119a0 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
119b0 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20  on, it does not 
119c0 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e  fail.  The.** IN
119d0 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74  SERT continues t
119e0 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74  o completion aft
119f0 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73  er deleting rows
11a00 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20   that caused.** 
11a10 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70  the constraint p
11a20 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54  roblem so INSERT
11a30 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c   OR REPLACE will
11a40 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a   always change.*
11a50 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * the return val
11a60 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72  ue of this inter
11a70 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  face..**.** For 
11a80 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
11a90 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e  this routine, an
11aa0 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e   [INSERT] is con
11ab0 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65  sidered to.** be
11ac0 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e   successful even
11ad0 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71   if it is subseq
11ae0 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61  uently rolled ba
11af0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ck..**.** Requir
11b00 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
11b10 32 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a  21] [H12223].**.
11b20 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65  ** If a separate
11b30 20 74 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73   thread performs
11b40 20 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20   a new [INSERT] 
11b50 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64  on the same.** d
11b60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
11b70 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71  on while the [sq
11b80 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
11b90 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75  t_rowid()].** fu
11ba0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
11bb0 67 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67  g and thus chang
11bc0 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65  es the last inse
11bd0 72 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74  rt [rowid],.** t
11be0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
11bf0 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74  turned by [sqlit
11c00 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
11c10 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e  owid()] is.** un
11c20 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20  predictable and 
11c30 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20  might not equal 
11c40 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f  either the old o
11c50 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73  r the new.** las
11c60 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d  t insert [rowid]
11c70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
11c80 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
11c90 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
11ca0 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a  t_rowid(sqlite3*
11cb0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
11cc0 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75  EF: Count The Nu
11cd0 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64  mber Of Rows Mod
11ce0 69 66 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c  ified {H12240} <
11cf0 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10600>.**.** Th
11d00 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
11d10 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
11d20 66 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20  f database rows 
11d30 74 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65  that were change
11d40 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64  d.** or inserted
11d50 20 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74   or deleted by t
11d60 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
11d70 20 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73   completed SQL s
11d80 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74  tatement.** on t
11d90 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
11da0 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69  nection] specifi
11db0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20  ed by the first 
11dc0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e  parameter..** On
11dd0 6c 79 20 63 68 61 6e 67 65 73 20 74 68 61 74 20  ly changes that 
11de0 61 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65  are directly spe
11df0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 5b 49  cified by the [I
11e00 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d  NSERT], [UPDATE]
11e10 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d  ,.** or [DELETE]
11e20 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63   statement are c
11e30 6f 75 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61  ounted.  Auxilia
11e40 72 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65  ry changes cause
11e50 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73  d by.** triggers
11e60 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64   are not counted
11e70 2e 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74  . Use the [sqlit
11e80 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
11e90 28 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ()] function.** 
11ea0 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61  to find the tota
11eb0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  l number of chan
11ec0 67 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68  ges including ch
11ed0 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20  anges caused by 
11ee0 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  triggers..**.** 
11ef0 43 68 61 6e 67 65 73 20 74 6f 20 61 20 76 69 65  Changes to a vie
11f00 77 20 74 68 61 74 20 61 72 65 20 73 69 6d 75 6c  w that are simul
11f10 61 74 65 64 20 62 79 20 61 6e 20 5b 49 4e 53 54  ated by an [INST
11f20 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 0a  EAD OF trigger].
11f30 2a 2a 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74  ** are not count
11f40 65 64 2e 20 20 4f 6e 6c 79 20 72 65 61 6c 20 74  ed.  Only real t
11f50 61 62 6c 65 20 63 68 61 6e 67 65 73 20 61 72 65  able changes are
11f60 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   counted..**.** 
11f70 41 20 22 72 6f 77 20 63 68 61 6e 67 65 22 20 69  A "row change" i
11f80 73 20 61 20 63 68 61 6e 67 65 20 74 6f 20 61 20  s a change to a 
11f90 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20  single row of a 
11fa0 73 69 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20  single table.** 
11fb0 63 61 75 73 65 64 20 62 79 20 61 6e 20 49 4e 53  caused by an INS
11fc0 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20  ERT, DELETE, or 
11fd0 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
11fe0 2e 20 20 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20  .  Rows that.** 
11ff0 61 72 65 20 63 68 61 6e 67 65 64 20 61 73 20 73  are changed as s
12000 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 20 5b  ide effects of [
12010 52 45 50 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61  REPLACE] constra
12020 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a  int resolution,.
12030 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f  ** rollback, ABO
12040 52 54 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 5b  RT processing, [
12050 44 52 4f 50 20 54 41 42 4c 45 5d 2c 20 6f 72 20  DROP TABLE], or 
12060 62 79 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20  by any other.** 
12070 6d 65 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f  mechanisms do no
12080 74 20 63 6f 75 6e 74 20 61 73 20 64 69 72 65 63  t count as direc
12090 74 20 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a  t row changes..*
120a0 2a 0a 2a 2a 20 41 20 22 74 72 69 67 67 65 72 20  *.** A "trigger 
120b0 63 6f 6e 74 65 78 74 22 20 69 73 20 61 20 73 63  context" is a sc
120c0 6f 70 65 20 6f 66 20 65 78 65 63 75 74 69 6f 6e  ope of execution
120d0 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 6e 64   that begins and
120e0 0a 2a 2a 20 65 6e 64 73 20 77 69 74 68 20 74 68  .** ends with th
120f0 65 20 73 63 72 69 70 74 20 6f 66 20 61 20 5b 43  e script of a [C
12100 52 45 41 54 45 20 54 52 49 47 47 45 52 20 7c 20  REATE TRIGGER | 
12110 74 72 69 67 67 65 72 5d 2e 20 0a 2a 2a 20 4d 6f  trigger]. .** Mo
12120 73 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  st SQL statement
12130 73 20 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74  s are.** evaluat
12140 65 64 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e  ed outside of an
12150 79 20 74 72 69 67 67 65 72 2e 20 20 54 68 69 73  y trigger.  This
12160 20 69 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76   is the "top lev
12170 65 6c 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63  el".** trigger c
12180 6f 6e 74 65 78 74 2e 20 20 49 66 20 61 20 74 72  ontext.  If a tr
12190 69 67 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d  igger fires from
121a0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20   the top level, 
121b0 61 0a 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72  a.** new trigger
121c0 20 63 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65   context is ente
121d0 72 65 64 20 66 6f 72 20 74 68 65 20 64 75 72 61  red for the dura
121e0 74 69 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65  tion of that one
121f0 0a 2a 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75  .** trigger.  Su
12200 62 74 72 69 67 67 65 72 73 20 63 72 65 61 74 65  btriggers create
12210 20 73 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72   subcontexts for
12220 20 74 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e   their duration.
12230 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b  .**.** Calling [
12240 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
12250 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  or [sqlite3_step
12260 28 29 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20  ()] recursively 
12270 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61  does.** not crea
12280 74 65 20 61 20 6e 65 77 20 74 72 69 67 67 65 72  te a new trigger
12290 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20   context..**.** 
122a0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
122b0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
122c0 20 6f 66 20 64 69 72 65 63 74 20 72 6f 77 20 63   of direct row c
122d0 68 61 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a  hanges in the.**
122e0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53   most recent INS
122f0 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
12300 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
12310 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
12320 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74  .** trigger cont
12330 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c  ext..**.** Thus,
12340 20 77 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f   when called fro
12350 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c  m the top level,
12360 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
12370 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75  eturns the.** nu
12380 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20  mber of changes 
12390 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  in the most rece
123a0 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  nt INSERT, UPDAT
123b0 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20  E, or DELETE.** 
123c0 74 68 61 74 20 61 6c 73 6f 20 6f 63 63 75 72 72  that also occurr
123d0 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65  ed at the top le
123e0 76 65 6c 2e 20 20 57 69 74 68 69 6e 20 74 68 65  vel.  Within the
123f0 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67   body of a trigg
12400 65 72 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74  er,.** the sqlit
12410 65 33 5f 63 68 61 6e 67 65 73 28 29 20 69 6e 74  e3_changes() int
12420 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 63 61  erface can be ca
12430 6c 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65  lled to find the
12440 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68   number of.** ch
12450 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73  anges in the mos
12460 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c  t recently compl
12470 65 74 65 64 20 49 4e 53 45 52 54 2c 20 55 50 44  eted INSERT, UPD
12480 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a  ATE, or DELETE.*
12490 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  * statement with
124a0 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74  in the body of t
124b0 68 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 2e  he same trigger.
124c0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65  .** However, the
124d0 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64   number returned
124e0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
124f0 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75  e changes.** cau
12500 73 65 64 20 62 79 20 73 75 62 74 72 69 67 67 65  sed by subtrigge
12510 72 73 20 73 69 6e 63 65 20 74 68 6f 73 65 20 68  rs since those h
12520 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f  ave their own co
12530 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  ntext..**.** See
12540 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74   also the [sqlit
12550 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
12560 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e  ()] interface an
12570 64 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f  d the.** [count_
12580 63 68 61 6e 67 65 73 20 70 72 61 67 6d 61 5d 2e  changes pragma].
12590 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
125a0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d  nts:.** [H12241]
125b0 20 5b 48 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20   [H12243].**.** 
125c0 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68  If a separate th
125d0 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67  read makes chang
125e0 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64  es on the same d
125f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
12600 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c  on.** while [sql
12610 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20  ite3_changes()] 
12620 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20  is running then 
12630 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
12640 65 64 0a 2a 2a 20 69 73 20 75 6e 70 72 65 64 69  ed.** is unpredi
12650 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d  ctable and not m
12660 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51  eaningful..*/.SQ
12670 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
12680 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c  ite3_changes(sql
12690 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
126a0 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e  API3REF: Total N
126b0 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f  umber Of Rows Mo
126c0 64 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20  dified {H12260} 
126d0 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10600>.**.** T
126e0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
126f0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
12700 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63  of row changes c
12710 61 75 73 65 64 20 62 79 20 5b 49 4e 53 45 52 54  aused by [INSERT
12720 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f  ],.** [UPDATE] o
12730 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65  r [DELETE] state
12740 6d 65 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20  ments since the 
12750 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
12760 74 69 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64  tion] was opened
12770 2e 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 20 69  ..** The count i
12780 6e 63 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e  ncludes all chan
12790 67 65 73 20 66 72 6f 6d 20 61 6c 6c 20 0a 2a 2a  ges from all .**
127a0 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 52   [CREATE TRIGGER
127b0 20 7c 20 74 72 69 67 67 65 72 5d 20 63 6f 6e 74   | trigger] cont
127c0 65 78 74 73 2e 20 20 48 6f 77 65 76 65 72 2c 0a  exts.  However,.
127d0 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 6f 65  ** the count doe
127e0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68  s not include ch
127f0 61 6e 67 65 73 20 75 73 65 64 20 74 6f 20 69 6d  anges used to im
12800 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45  plement [REPLACE
12810 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a  ] constraints,.*
12820 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f  * do rollbacks o
12830 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69  r ABORT processi
12840 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 42  ng, or [DROP TAB
12850 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20  LE] processing. 
12860 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f   The.** count do
12870 65 73 20 6e 6f 74 20 72 6f 77 73 20 6f 66 20 76  es not rows of v
12880 69 65 77 73 20 74 68 61 74 20 66 69 72 65 20 61  iews that fire a
12890 6e 20 5b 49 4e 53 54 45 41 44 20 4f 46 20 74 72  n [INSTEAD OF tr
128a0 69 67 67 65 72 5d 2c 20 74 68 6f 75 67 68 20 69  igger], though i
128b0 66 0a 2a 2a 20 74 68 65 20 49 4e 53 54 45 41 44  f.** the INSTEAD
128c0 20 4f 46 20 74 72 69 67 67 65 72 20 6d 61 6b 65   OF trigger make
128d0 73 20 63 68 61 6e 67 65 73 20 6f 66 20 69 74 73  s changes of its
128e0 20 6f 77 6e 2c 20 74 68 6f 73 65 20 63 68 61 6e   own, those chan
128f0 67 65 73 20 61 72 65 0a 2a 2a 20 63 6f 75 6e 74  ges are.** count
12900 65 64 2e 0a 2a 2a 20 54 68 65 20 63 68 61 6e 67  ed..** The chang
12910 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 61  es are counted a
12920 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73 74  s soon as the st
12930 61 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 6b  atement that mak
12940 65 73 20 74 68 65 6d 20 69 73 0a 2a 2a 20 63 6f  es them is.** co
12950 6d 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68  mpleted (when th
12960 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  e statement hand
12970 6c 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20  le is passed to 
12980 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
12990 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ] or.** [sqlite3
129a0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a  _finalize()])..*
129b0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68  *.** See also th
129c0 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  e [sqlite3_chang
129d0 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  es()] interface 
129e0 61 6e 64 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e  and the.** [coun
129f0 74 5f 63 68 61 6e 67 65 73 20 70 72 61 67 6d 61  t_changes pragma
12a00 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
12a10 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 36  ments:.** [H1226
12a20 31 5d 20 5b 48 31 32 32 36 33 5d 0a 2a 2a 0a 2a  1] [H12263].**.*
12a30 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20  * If a separate 
12a40 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61  thread makes cha
12a50 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65  nges on the same
12a60 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
12a70 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73  tion.** while [s
12a80 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
12a90 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69  nges()] is runni
12aa0 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  ng then the valu
12ab0 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 73  e.** returned is
12ac0 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61   unpredictable a
12ad0 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75  nd not meaningfu
12ae0 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  l..*/.SQLITE_API
12af0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f 74   int sqlite3_tot
12b00 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74  al_changes(sqlit
12b10 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  e3*);../*.** CAP
12b20 49 33 52 45 46 3a 20 49 6e 74 65 72 72 75 70 74  I3REF: Interrupt
12b30 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67 20   A Long-Running 
12b40 51 75 65 72 79 20 7b 48 31 32 32 37 30 7d 20 3c  Query {H12270} <
12b50 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30500>.**.** Th
12b60 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73  is function caus
12b70 65 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 64  es any pending d
12b80 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f  atabase operatio
12b90 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a 2a  n to abort and.*
12ba0 2a 20 72 65 74 75 72 6e 20 61 74 20 69 74 73 20  * return at its 
12bb0 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75  earliest opportu
12bc0 6e 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74 69  nity. This routi
12bd0 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79 0a  ne is typically.
12be0 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73  ** called in res
12bf0 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72 20  ponse to a user 
12c00 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20 70  action such as p
12c10 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c 22  ressing "Cancel"
12c20 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77 68  .** or Ctrl-C wh
12c30 65 72 65 20 74 68 65 20 75 73 65 72 20 77 61 6e  ere the user wan
12c40 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79 20  ts a long query 
12c50 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61 6c  operation to hal
12c60 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79  t.** immediately
12c70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73 61  ..**.** It is sa
12c80 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  fe to call this 
12c90 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20 74  routine from a t
12ca0 68 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20  hread different 
12cb0 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72 65  from the.** thre
12cc0 61 64 20 74 68 61 74 20 69 73 20 63 75 72 72 65  ad that is curre
12cd0 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68 65  ntly running the
12ce0 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74   database operat
12cf0 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a 2a 20  ion.  But it.** 
12d00 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 63  is not safe to c
12d10 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
12d20 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 73   with a [databas
12d30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68  e connection] th
12d40 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65 64 20  at.** is closed 
12d50 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 65 20 62  or might close b
12d60 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 69 6e  efore sqlite3_in
12d70 74 65 72 72 75 70 74 28 29 20 72 65 74 75 72 6e  terrupt() return
12d80 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 53  s..**.** If an S
12d90 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  QL operation is 
12da0 76 65 72 79 20 6e 65 61 72 6c 79 20 66 69 6e 69  very nearly fini
12db0 73 68 65 64 20 61 74 20 74 68 65 20 74 69 6d 65  shed at the time
12dc0 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33   when.** sqlite3
12dd0 5f 69 6e 74 65 72 72 75 70 74 28 29 20 69 73 20  _interrupt() is 
12de0 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69 74 20  called, then it 
12df0 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20 61  might not have a
12e00 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a 2a 2a  n opportunity.**
12e10 20 74 6f 20 62 65 20 69 6e 74 65 72 72 75 70 74   to be interrupt
12e20 65 64 20 61 6e 64 20 6d 69 67 68 74 20 63 6f 6e  ed and might con
12e30 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c 65 74  tinue to complet
12e40 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 53 51  ion..**.** An SQ
12e50 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74  L operation that
12e60 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 20   is interrupted 
12e70 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c  will return [SQL
12e80 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e 0a  ITE_INTERRUPT]..
12e90 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 72 72  ** If the interr
12ea0 75 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61 74  upted SQL operat
12eb0 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54  ion is an INSERT
12ec0 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c  , UPDATE, or DEL
12ed0 45 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20 69  ETE.** that is i
12ee0 6e 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63 69  nside an explici
12ef0 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  t transaction, t
12f00 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 74  hen the entire t
12f10 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69  ransaction.** wi
12f20 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  ll be rolled bac
12f30 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e  k automatically.
12f40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
12f50 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20  e3_interrupt(D) 
12f60 63 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65 63  call is in effec
12f70 74 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72 72  t until all curr
12f80 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a 2a  ently running.**
12f90 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
12fa0 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  on [database con
12fb0 6e 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70 6c  nection] D compl
12fc0 65 74 65 2e 20 20 41 6e 79 20 6e 65 77 20 53 51  ete.  Any new SQ
12fd0 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  L statements.** 
12fe0 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64  that are started
12ff0 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74   after the sqlit
13000 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63  e3_interrupt() c
13010 61 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20 74  all and before t
13020 68 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 73  he .** running s
13030 74 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68 65  tatements reache
13040 73 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65 72  s zero are inter
13050 72 75 70 74 65 64 20 61 73 20 69 66 20 74 68 65  rupted as if the
13060 79 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72 75  y had been.** ru
13070 6e 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20 74  nning prior to t
13080 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  he sqlite3_inter
13090 72 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 4e 65  rupt() call.  Ne
130a0 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  w SQL statements
130b0 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61  .** that are sta
130c0 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20 72  rted after the r
130d0 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  unning statement
130e0 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a   count reaches z
130f0 65 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 65  ero are.** not e
13100 66 66 65 63 74 65 64 20 62 79 20 74 68 65 20 73  ffected by the s
13110 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
13120 28 29 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f  ()..** A call to
13130 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
13140 70 74 28 44 29 20 74 68 61 74 20 6f 63 63 75 72  pt(D) that occur
13150 73 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65  s when there are
13160 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 53   no running.** S
13170 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73  QL statements is
13180 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 68 61 73   a no-op and has
13190 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53 51   no effect on SQ
131a0 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  L statements.** 
131b0 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64  that are started
131c0 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74   after the sqlit
131d0 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63  e3_interrupt() c
131e0 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a  all returns..**.
131f0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
13200 0a 2a 2a 20 5b 48 31 32 32 37 31 5d 20 5b 48 31  .** [H12271] [H1
13210 32 32 37 32 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74  2272].**.** If t
13220 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
13230 65 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77 68  ection closes wh
13240 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74  ile [sqlite3_int
13250 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 69 73 20  errupt()].** is 
13260 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 62 61 64  running then bad
13270 20 74 68 69 6e 67 73 20 77 69 6c 6c 20 6c 69 6b   things will lik
13280 65 6c 79 20 68 61 70 70 65 6e 2e 0a 2a 2f 0a 53  ely happen..*/.S
13290 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
132a0 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
132b0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
132c0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 74  ** CAPI3REF: Det
132d0 65 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51 4c  ermine If An SQL
132e0 20 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43 6f   Statement Is Co
132f0 6d 70 6c 65 74 65 20 7b 48 31 30 35 31 30 7d 20  mplete {H10510} 
13300 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S70200>.**.** T
13310 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
13320 65 20 75 73 65 66 75 6c 20 64 75 72 69 6e 67 20  e useful during 
13330 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e 70  command-line inp
13340 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ut to determine 
13350 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  if the.** curren
13360 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78 74  tly entered text
13370 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20 61   seems to form a
13380 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74   complete SQL st
13390 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69 66  atement or.** if
133a0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70 75   additional inpu
133b0 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f  t is needed befo
133c0 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20 74  re sending the t
133d0 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c 69  ext into.** SQLi
133e0 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e 20  te for parsing. 
133f0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
13400 72 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  return 1 if the 
13410 69 6e 70 75 74 20 73 74 72 69 6e 67 0a 2a 2a 20  input string.** 
13420 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 61 20  appears to be a 
13430 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74 61  complete SQL sta
13440 74 65 6d 65 6e 74 2e 20 20 41 20 73 74 61 74 65  tement.  A state
13450 6d 65 6e 74 20 69 73 20 6a 75 64 67 65 64 20 74  ment is judged t
13460 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65  o be.** complete
13470 20 69 66 20 69 74 20 65 6e 64 73 20 77 69 74 68   if it ends with
13480 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 6f 6b   a semicolon tok
13490 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 20  en and is not a 
134a0 70 72 65 66 69 78 20 6f 66 20 61 0a 2a 2a 20 77  prefix of a.** w
134b0 65 6c 6c 2d 66 6f 72 6d 65 64 20 43 52 45 41 54  ell-formed CREAT
134c0 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d  E TRIGGER statem
134d0 65 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e 73  ent.  Semicolons
134e0 20 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 64   that are embedd
134f0 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 72  ed within.** str
13500 69 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 20  ing literals or 
13510 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65  quoted identifie
13520 72 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65  r names or comme
13530 6e 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 69  nts are not.** i
13540 6e 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e  ndependent token
13550 73 20 28 74 68 65 79 20 61 72 65 20 70 61 72 74  s (they are part
13560 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e   of the token in
13570 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65 0a   which they are.
13580 2a 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64  ** embedded) and
13590 20 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75   thus do not cou
135a0 6e 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e  nt as a statemen
135b0 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 57  t terminator.  W
135c0 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 61 6e 64  hitespace.** and
135d0 20 63 6f 6d 6d 65 6e 74 73 20 74 68 61 74 20 66   comments that f
135e0 6f 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c 20  ollow the final 
135f0 73 65 6d 69 63 6f 6c 6f 6e 20 61 72 65 20 69 67  semicolon are ig
13600 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  nored..**.** The
13610 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  se routines retu
13620 72 6e 20 30 20 69 66 20 74 68 65 20 73 74 61 74  rn 0 if the stat
13630 65 6d 65 6e 74 20 69 73 20 69 6e 63 6f 6d 70 6c  ement is incompl
13640 65 74 65 2e 20 20 49 66 20 61 0a 2a 2a 20 6d 65  ete.  If a.** me
13650 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
13660 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c 49  fails, then SQLI
13670 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75  TE_NOMEM is retu
13680 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  rned..**.** Thes
13690 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e 6f  e routines do no
136a0 74 20 70 61 72 73 65 20 74 68 65 20 53 51 4c 20  t parse the SQL 
136b0 73 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73 0a  statements thus.
136c0 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74 65  ** will not dete
136d0 63 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c 79  ct syntactically
136e0 20 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e 0a   incorrect SQL..
136f0 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20  **.** If SQLite 
13700 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69  has not been ini
13710 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 5b  tialized using [
13720 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
13730 7a 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a 20  ze()] prior .** 
13740 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69  to invoking sqli
13750 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29  te3_complete16()
13760 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e   then sqlite3_in
13770 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e  itialize() is in
13780 76 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74  voked.** automat
13790 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65  ically by sqlite
137a0 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e 20  3_complete16(). 
137b0 20 49 66 20 74 68 61 74 20 69 6e 69 74 69 61 6c   If that initial
137c0 69 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a  ization fails,.*
137d0 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72  * then the retur
137e0 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c  n value from sql
137f0 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
13800 29 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a 65  ) will be non-ze
13810 72 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 73  ro.** regardless
13820 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e   of whether or n
13830 6f 74 20 74 68 65 20 69 6e 70 75 74 20 53 51 4c  ot the input SQL
13840 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a   is complete..**
13850 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
13860 3a 20 5b 48 31 30 35 31 31 5d 20 5b 48 31 30 35  : [H10511] [H105
13870 31 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  12].**.** The in
13880 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  put to [sqlite3_
13890 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73 74  complete()] must
138a0 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69   be a zero-termi
138b0 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 38 20 73  nated.** UTF-8 s
138c0 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tring..**.** The
138d0 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74   input to [sqlit
138e0 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d  e3_complete16()]
138f0 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d   must be a zero-
13900 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54  terminated.** UT
13910 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 6e  F-16 string in n
13920 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
13930 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
13940 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  int sqlite3_comp
13950 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20  lete(const char 
13960 2a 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  *sql);.SQLITE_AP
13970 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
13980 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20 76  mplete16(const v
13990 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a  oid *sql);../*.*
139a0 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69  * CAPI3REF: Regi
139b0 73 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b 20  ster A Callback 
139c0 54 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45  To Handle SQLITE
139d0 5f 42 55 53 59 20 45 72 72 6f 72 73 20 7b 48 31  _BUSY Errors {H1
139e0 32 33 31 30 7d 20 3c 53 34 30 34 30 30 3e 0a 2a  2310} <S40400>.*
139f0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
13a00 65 20 73 65 74 73 20 61 20 63 61 6c 6c 62 61 63  e sets a callbac
13a10 6b 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  k function that 
13a20 6d 69 67 68 74 20 62 65 20 69 6e 76 6f 6b 65 64  might be invoked
13a30 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 61 6e 20   whenever.** an 
13a40 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
13a50 74 6f 20 6f 70 65 6e 20 61 20 64 61 74 61 62 61  to open a databa
13a60 73 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 6e  se table that an
13a70 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
13a80 6f 72 20 70 72 6f 63 65 73 73 20 68 61 73 20 6c  or process has l
13a90 6f 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ocked..**.** If 
13aa0 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
13ab0 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  k is NULL, then 
13ac0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72  [SQLITE_BUSY] or
13ad0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
13ae0 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 69 73 20 72 65  LOCKED].** is re
13af0 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65  turned immediate
13b00 6c 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74 65  ly upon encounte
13b10 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20 49  ring the lock. I
13b20 66 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62  f the busy callb
13b30 61 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55  ack.** is not NU
13b40 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c  LL, then the cal
13b50 6c 62 61 63 6b 20 77 69 6c 6c 20 62 65 20 69 6e  lback will be in
13b60 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61  voked with two a
13b70 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  rguments..**.** 
13b80 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
13b90 6e 74 20 74 6f 20 74 68 65 20 68 61 6e 64 6c 65  nt to the handle
13ba0 72 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  r is a copy of t
13bb0 68 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72  he void* pointer
13bc0 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 68 65   which.** is the
13bd0 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
13be0 74 6f 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  to sqlite3_busy_
13bf0 68 61 6e 64 6c 65 72 28 29 2e 20 20 54 68 65 20  handler().  The 
13c00 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
13c10 74 6f 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c 65  to.** the handle
13c20 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68  r callback is th
13c30 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  e number of time
13c40 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79 20  s that the busy 
13c50 68 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20 62  handler has.** b
13c60 65 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20  een invoked for 
13c70 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76 65  this locking eve
13c80 6e 74 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 62  nt.  If the.** b
13c90 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  usy callback ret
13ca0 75 72 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f 20  urns 0, then no 
13cb0 61 64 64 69 74 69 6f 6e 61 6c 20 61 74 74 65 6d  additional attem
13cc0 70 74 73 20 61 72 65 20 6d 61 64 65 20 74 6f 0a  pts are made to.
13cd0 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 64 61  ** access the da
13ce0 74 61 62 61 73 65 20 61 6e 64 20 5b 53 51 4c 49  tabase and [SQLI
13cf0 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c  TE_BUSY] or [SQL
13d00 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
13d10 44 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  D] is returned..
13d20 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61  ** If the callba
13d30 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ck returns non-z
13d40 65 72 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 65  ero, then anothe
13d50 72 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20  r attempt.** is 
13d60 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 65  made to open the
13d70 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 72 65   database for re
13d80 61 64 69 6e 67 20 61 6e 64 20 74 68 65 20 63 79  ading and the cy
13d90 63 6c 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a 0a  cle repeats..**.
13da0 2a 2a 20 54 68 65 20 70 72 65 73 65 6e 63 65 20  ** The presence 
13db0 6f 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65  of a busy handle
13dc0 72 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61  r does not guara
13dd0 6e 74 65 65 20 74 68 61 74 20 69 74 20 77 69 6c  ntee that it wil
13de0 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20  l be invoked.** 
13df0 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f  when there is lo
13e00 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20 49  ck contention. I
13e10 66 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d 69  f SQLite determi
13e20 6e 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69 6e  nes that invokin
13e30 67 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68 61  g the busy.** ha
13e40 6e 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 75  ndler could resu
13e50 6c 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b  lt in a deadlock
13e60 2c 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68 65  , it will go ahe
13e70 61 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53  ad and return [S
13e80 51 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20 6f  QLITE_BUSY].** o
13e90 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  r [SQLITE_IOERR_
13ea0 42 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61 64  BLOCKED] instead
13eb0 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65   of invoking the
13ec0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a   busy handler..*
13ed0 2a 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63 65  * Consider a sce
13ee0 6e 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65 20  nario where one 
13ef0 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69  process is holdi
13f00 6e 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 74  ng a read lock t
13f10 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72 79  hat.** it is try
13f20 69 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  ing to promote t
13f30 6f 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  o a reserved loc
13f40 6b 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f 6e  k and.** a secon
13f50 64 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c  d process is hol
13f60 64 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20  ding a reserved 
13f70 6c 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73 20  lock that it is 
13f80 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72 6f  trying.** to pro
13f90 6d 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c 75  mote to an exclu
13fa0 73 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65 20  sive lock.  The 
13fb0 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 63 61  first process ca
13fc0 6e 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a 20  nnot proceed.** 
13fd0 62 65 63 61 75 73 65 20 69 74 20 69 73 20 62 6c  because it is bl
13fe0 6f 63 6b 65 64 20 62 79 20 74 68 65 20 73 65 63  ocked by the sec
13ff0 6f 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f  ond and the seco
14000 6e 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f  nd process canno
14010 74 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65 63  t.** proceed bec
14020 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b  ause it is block
14030 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 2e  ed by the first.
14040 20 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65 73    If both proces
14050 73 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  ses.** invoke th
14060 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2c  e busy handlers,
14070 20 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d 61   neither will ma
14080 6b 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73 2e  ke any progress.
14090 20 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a 20    Therefore,.** 
140a0 53 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20 5b  SQLite returns [
140b0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f 72  SQLITE_BUSY] for
140c0 20 74 68 65 20 66 69 72 73 74 20 70 72 6f 63 65   the first proce
140d0 73 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74 20  ss, hoping that 
140e0 74 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e 64  this.** will ind
140f0 75 63 65 20 74 68 65 20 66 69 72 73 74 20 70 72  uce the first pr
14100 6f 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73 65  ocess to release
14110 20 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20 61   its read lock a
14120 6e 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65 20  nd allow.** the 
14130 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 74  second process t
14140 6f 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a 2a  o proceed..**.**
14150 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 73   The default bus
14160 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55  y callback is NU
14170 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53  LL..**.** The [S
14180 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 6f  QLITE_BUSY] erro
14190 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 74  r is converted t
141a0 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o [SQLITE_IOERR_
141b0 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65 6e  BLOCKED].** when
141c0 20 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74 68   SQLite is in th
141d0 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c 61  e middle of a la
141e0 72 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  rge transaction 
141f0 77 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a 2a  where all the.**
14200 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e 6f   changes will no
14210 74 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20 69  t fit into the i
14220 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20  n-memory cache. 
14230 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20   SQLite will.** 
14240 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 52  already hold a R
14250 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20  ESERVED lock on 
14260 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
14270 65 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73 0a  e, but it needs.
14280 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68  ** to promote th
14290 69 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c 55  is lock to EXCLU
142a0 53 49 56 45 20 73 6f 20 74 68 61 74 20 69 74 20  SIVE so that it 
142b0 63 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65 0a  can spill cache.
142c0 2a 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74 68  ** pages into th
142d0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
142e0 77 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f 20  without harm to 
142f0 63 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65  concurrent.** re
14300 61 64 65 72 73 2e 20 20 49 66 20 69 74 20 69 73  aders.  If it is
14310 20 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f 6d 6f   unable to promo
14320 74 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 68 65  te the lock, the
14330 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a  n the in-memory.
14340 2a 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 62 65  ** cache will be
14350 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f   left in an inco
14360 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 20 61  nsistent state a
14370 6e 64 20 73 6f 20 74 68 65 20 65 72 72 6f 72 0a  nd so the error.
14380 2a 2a 20 63 6f 64 65 20 69 73 20 70 72 6f 6d 6f  ** code is promo
14390 74 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65 6c  ted from the rel
143a0 61 74 69 76 65 6c 79 20 62 65 6e 69 67 6e 20 5b  atively benign [
143b0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 6f 0a  SQLITE_BUSY] to.
143c0 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 76 65  ** the more seve
143d0 72 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  re [SQLITE_IOERR
143e0 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 54 68 69 73  _BLOCKED].  This
143f0 20 65 72 72 6f 72 20 63 6f 64 65 20 70 72 6f 6d   error code prom
14400 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 65 73 20  otion.** forces 
14410 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c  an automatic rol
14420 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 68 61  lback of the cha
14430 6e 67 65 73 2e 20 20 53 65 65 20 74 68 65 0a 2a  nges.  See the.*
14440 2a 20 3c 61 20 68 72 65 66 3d 22 2f 63 76 73 74  * <a href="/cvst
14450 72 61 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72 75  rac/wiki?p=Corru
14460 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75  ptionFollowingBu
14470 73 79 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f 72  syError">.** Cor
14480 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67  ruptionFollowing
14490 42 75 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77 69  BusyError</a> wi
144a0 6b 69 20 70 61 67 65 20 66 6f 72 20 61 20 64 69  ki page for a di
144b0 73 63 75 73 73 69 6f 6e 20 6f 66 20 77 68 79 0a  scussion of why.
144c0 2a 2a 20 74 68 69 73 20 69 73 20 69 6d 70 6f 72  ** this is impor
144d0 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  tant..**.** Ther
144e0 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61 20  e can only be a 
144f0 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e 64  single busy hand
14500 6c 65 72 20 64 65 66 69 6e 65 64 20 66 6f 72 20  ler defined for 
14510 65 61 63 68 0a 2a 2a 20 5b 64 61 74 61 62 61 73  each.** [databas
14520 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20  e connection].  
14530 53 65 74 74 69 6e 67 20 61 20 6e 65 77 20 62 75  Setting a new bu
14540 73 79 20 68 61 6e 64 6c 65 72 20 63 6c 65 61 72  sy handler clear
14550 73 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69 6f 75  s any.** previou
14560 73 6c 79 20 73 65 74 20 68 61 6e 64 6c 65 72 2e  sly set handler.
14570 20 20 4e 6f 74 65 20 74 68 61 74 20 63 61 6c 6c    Note that call
14580 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75 73  ing [sqlite3_bus
14590 79 5f 74 69 6d 65 6f 75 74 28 29 5d 0a 2a 2a 20  y_timeout()].** 
145a0 77 69 6c 6c 20 61 6c 73 6f 20 73 65 74 20 6f 72  will also set or
145b0 20 63 6c 65 61 72 20 74 68 65 20 62 75 73 79 20   clear the busy 
145c0 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  handler..**.** T
145d0 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
145e0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 74 61 6b 65   should not take
145f0 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 77 68 69   any actions whi
14600 63 68 20 6d 6f 64 69 66 79 20 74 68 65 0a 2a 2a  ch modify the.**
14610 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14620 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65  tion that invoke
14630 64 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  d the busy handl
14640 65 72 2e 20 20 41 6e 79 20 73 75 63 68 20 61 63  er.  Any such ac
14650 74 69 6f 6e 73 0a 2a 2a 20 72 65 73 75 6c 74 20  tions.** result 
14660 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68  in undefined beh
14670 61 76 69 6f 72 2e 0a 2a 2a 20 0a 2a 2a 20 52 65  avior..** .** Re
14680 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
14690 48 31 32 33 31 31 5d 20 5b 48 31 32 33 31 32 5d  H12311] [H12312]
146a0 20 5b 48 31 32 33 31 34 5d 20 5b 48 31 32 33 31   [H12314] [H1231
146b0 36 5d 20 5b 48 31 32 33 31 38 5d 0a 2a 2a 0a 2a  6] [H12318].**.*
146c0 2a 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65 72  * A busy handler
146d0 20 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65 20   must not close 
146e0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
146f0 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 5b 70  nection.** or [p
14700 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
14710 74 5d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20  t] that invoked 
14720 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
14730 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
14740 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
14750 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33  _handler(sqlite3
14760 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  *, int(*)(void*,
14770 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f  int), void*);../
14780 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53  *.** CAPI3REF: S
14790 65 74 20 41 20 42 75 73 79 20 54 69 6d 65 6f 75  et A Busy Timeou
147a0 74 20 7b 48 31 32 33 34 30 7d 20 3c 53 34 30 34  t {H12340} <S404
147b0 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  10>.**.** This r
147c0 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 5b 73  outine sets a [s
147d0 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
147e0 6c 65 72 20 7c 20 62 75 73 79 20 68 61 6e 64 6c  ler | busy handl
147f0 65 72 5d 20 74 68 61 74 20 73 6c 65 65 70 73 0a  er] that sleeps.
14800 2a 2a 20 66 6f 72 20 61 20 73 70 65 63 69 66 69  ** for a specifi
14810 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  ed amount of tim
14820 65 20 77 68 65 6e 20 61 20 74 61 62 6c 65 20 69  e when a table i
14830 73 20 6c 6f 63 6b 65 64 2e 20 20 54 68 65 20 68  s locked.  The h
14840 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c 6c 20 73  andler.** will s
14850 6c 65 65 70 20 6d 75 6c 74 69 70 6c 65 20 74 69  leep multiple ti
14860 6d 65 73 20 75 6e 74 69 6c 20 61 74 20 6c 65 61  mes until at lea
14870 73 74 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 63  st "ms" millisec
14880 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e 67  onds of sleeping
14890 0a 2a 2a 20 68 61 76 65 20 61 63 63 75 6d 75 6c  .** have accumul
148a0 61 74 65 64 2e 20 7b 48 31 32 33 34 33 7d 20 41  ated. {H12343} A
148b0 66 74 65 72 20 22 6d 73 22 20 6d 69 6c 6c 69 73  fter "ms" millis
148c0 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69  econds of sleepi
148d0 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c  ng,.** the handl
148e0 65 72 20 72 65 74 75 72 6e 73 20 30 20 77 68 69  er returns 0 whi
148f0 63 68 20 63 61 75 73 65 73 20 5b 73 71 6c 69 74  ch causes [sqlit
14900 65 33 5f 73 74 65 70 28 29 5d 20 74 6f 20 72 65  e3_step()] to re
14910 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  turn.** [SQLITE_
14920 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45  BUSY] or [SQLITE
14930 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e  _IOERR_BLOCKED].
14940 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74  .**.** Calling t
14950 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68  his routine with
14960 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c 65 73   an argument les
14970 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
14980 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75 72 6e 73  to zero.** turns
14990 20 6f 66 66 20 61 6c 6c 20 62 75 73 79 20 68 61   off all busy ha
149a0 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ndlers..**.** Th
149b0 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20  ere can only be 
149c0 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61  a single busy ha
149d0 6e 64 6c 65 72 20 66 6f 72 20 61 20 70 61 72 74  ndler for a part
149e0 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 62  icular.** [datab
149f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
14a00 61 6e 79 20 61 6e 79 20 67 69 76 65 6e 20 6d 6f  any any given mo
14a10 6d 65 6e 74 2e 20 20 49 66 20 61 6e 6f 74 68 65  ment.  If anothe
14a20 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a  r busy handler.*
14a30 2a 20 77 61 73 20 64 65 66 69 6e 65 64 20 20 28  * was defined  (
14a40 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62  using [sqlite3_b
14a50 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 29 20  usy_handler()]) 
14a60 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67  prior to calling
14a70 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
14a80 2c 20 74 68 61 74 20 6f 74 68 65 72 20 62 75 73  , that other bus
14a90 79 20 68 61 6e 64 6c 65 72 20 69 73 20 63 6c 65  y handler is cle
14aa0 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ared..**.** Requ
14ab0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
14ac0 32 33 34 31 5d 20 5b 48 31 32 33 34 33 5d 20 5b  2341] [H12343] [
14ad0 48 31 32 33 34 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H12344].*/.SQLIT
14ae0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
14af0 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 73  3_busy_timeout(s
14b00 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73 29  qlite3*, int ms)
14b10 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
14b20 46 3a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 52  F: Convenience R
14b30 6f 75 74 69 6e 65 73 20 46 6f 72 20 52 75 6e 6e  outines For Runn
14b40 69 6e 67 20 51 75 65 72 69 65 73 20 7b 48 31 32  ing Queries {H12
14b50 33 37 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a  370} <S10000>.**
14b60 0a 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20  .** Definition: 
14b70 41 20 3c 62 3e 72 65 73 75 6c 74 20 74 61 62 6c  A <b>result tabl
14b80 65 3c 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79 20  e</b> is memory 
14b90 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63  data structure c
14ba0 72 65 61 74 65 64 20 62 79 20 74 68 65 0a 2a 2a  reated by the.**
14bb0 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   [sqlite3_get_ta
14bc0 62 6c 65 28 29 5d 20 69 6e 74 65 72 66 61 63 65  ble()] interface
14bd0 2e 20 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c  .  A result tabl
14be0 65 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a  e records the.**
14bf0 20 63 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 20   complete query 
14c00 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e 65  results from one
14c10 20 6f 72 20 6d 6f 72 65 20 71 75 65 72 69 65 73   or more queries
14c20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c  ..**.** The tabl
14c30 65 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 68  e conceptually h
14c40 61 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 72  as a number of r
14c50 6f 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 2e  ows and columns.
14c60 20 20 42 75 74 0a 2a 2a 20 74 68 65 73 65 20 6e    But.** these n
14c70 75 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20 70  umbers are not p
14c80 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c  art of the resul
14c90 74 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e 20  t table itself. 
14ca0 20 54 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65 72   These.** number
14cb0 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 73  s are obtained s
14cc0 65 70 61 72 61 74 65 6c 79 2e 20 20 4c 65 74 20  eparately.  Let 
14cd0 4e 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20  N be the number 
14ce0 6f 66 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20 4d  of rows.** and M
14cf0 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   be the number o
14d00 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a  f columns..**.**
14d10 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20   A result table 
14d20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  is an array of p
14d30 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d  ointers to zero-
14d40 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
14d50 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68 65   strings..** The
14d60 72 65 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20 65  re are (N+1)*M e
14d70 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61  lements in the a
14d80 72 72 61 79 2e 20 20 54 68 65 20 66 69 72 73 74  rray.  The first
14d90 20 4d 20 70 6f 69 6e 74 65 72 73 20 70 6f 69 6e   M pointers poin
14da0 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65 72  t.** to zero-ter
14db0 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20  minated strings 
14dc0 74 68 61 74 20 20 63 6f 6e 74 61 69 6e 20 74 68  that  contain th
14dd0 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63  e names of the c
14de0 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20 72  olumns..** The r
14df0 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 73  emaining entries
14e00 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71 75   all point to qu
14e10 65 72 79 20 72 65 73 75 6c 74 73 2e 20 20 4e 55  ery results.  NU
14e20 4c 4c 20 76 61 6c 75 65 73 20 72 65 73 75 6c 74  LL values result
14e30 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69 6e  .** in NULL poin
14e40 74 65 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65 72  ters.  All other
14e50 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 20 74   values are in t
14e60 68 65 69 72 20 55 54 46 2d 38 20 7a 65 72 6f 2d  heir UTF-8 zero-
14e70 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74  terminated.** st
14e80 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74  ring representat
14e90 69 6f 6e 20 61 73 20 72 65 74 75 72 6e 65 64 20  ion as returned 
14ea0 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  by [sqlite3_colu
14eb0 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a  mn_text()]..**.*
14ec0 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65  * A result table
14ed0 20 6d 69 67 68 74 20 63 6f 6e 73 69 73 74 20 6f   might consist o
14ee0 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d 65  f one or more me
14ef0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
14f00 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73  ..** It is not s
14f10 61 66 65 20 74 6f 20 70 61 73 73 20 61 20 72 65  afe to pass a re
14f20 73 75 6c 74 20 74 61 62 6c 65 20 64 69 72 65 63  sult table direc
14f30 74 6c 79 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  tly to [sqlite3_
14f40 66 72 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72 65  free()]..** A re
14f50 73 75 6c 74 20 74 61 62 6c 65 20 73 68 6f 75 6c  sult table shoul
14f60 64 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64  d be deallocated
14f70 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
14f80 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a 2a  free_table()]..*
14f90 2a 0a 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d 70  *.** As an examp
14fa0 6c 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  le of the result
14fb0 20 74 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20 73   table format, s
14fc0 75 70 70 6f 73 65 20 61 20 71 75 65 72 79 20 72  uppose a query r
14fd0 65 73 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20 66  esult.** is as f
14fe0 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62  ollows:.**.** <b
14ff0 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
15000 2a 2a 20 20 20 20 20 20 20 20 4e 61 6d 65 20 20  **        Name  
15010 20 20 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20 20        | Age.**  
15020 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d        ----------
15030 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
15040 20 20 20 20 20 20 20 20 41 6c 69 63 65 20 20 20          Alice   
15050 20 20 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20 20      | 43.**     
15060 20 20 20 42 6f 62 20 20 20 20 20 20 20 20 20 7c     Bob         |
15070 20 32 38 0a 2a 2a 20 20 20 20 20 20 20 20 43 69   28.**        Ci
15080 6e 64 79 20 20 20 20 20 20 20 7c 20 32 31 0a 2a  ndy       | 21.*
15090 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
150a0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  uote>.**.** Ther
150b0 65 20 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d 6e  e are two column
150c0 20 28 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72 65   (M==2) and thre
150d0 65 20 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20 20  e rows (N==3).  
150e0 54 68 75 73 20 74 68 65 0a 2a 2a 20 72 65 73 75  Thus the.** resu
150f0 6c 74 20 74 61 62 6c 65 20 68 61 73 20 38 20 65  lt table has 8 e
15100 6e 74 72 69 65 73 2e 20 20 53 75 70 70 6f 73 65  ntries.  Suppose
15110 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c   the result tabl
15120 65 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69  e is stored.** i
15130 6e 20 61 6e 20 61 72 72 61 79 20 6e 61 6d 65 73  n an array names
15140 20 61 7a 52 65 73 75 6c 74 2e 20 20 54 68 65 6e   azResult.  Then
15150 20 61 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73 20   azResult holds 
15160 74 68 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a 2a  this content:.**
15170 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
15180 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20  <pre>.**        
15190 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 30 5d 20  azResult&#91;0] 
151a0 3d 20 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20 20  = "Name";.**    
151b0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
151c0 3b 31 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a 20  ;1] = "Age";.** 
151d0 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
151e0 23 39 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65 22  #91;2] = "Alice"
151f0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65  ;.**        azRe
15200 73 75 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22 34  sult&#91;3] = "4
15210 33 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a  3";.**        az
15220 52 65 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d 20  Result&#91;4] = 
15230 22 42 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20 20  "Bob";.**       
15240 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 35 5d   azResult&#91;5]
15250 20 3d 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20 20   = "28";.**     
15260 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b     azResult&#91;
15270 36 5d 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a 2a  6] = "Cindy";.**
15280 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
15290 26 23 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b 0a  &#91;7] = "21";.
152a0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b  ** </pre></block
152b0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  quote>.**.** The
152c0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
152d0 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 65 76  le() function ev
152e0 61 6c 75 61 74 65 73 20 6f 6e 65 20 6f 72 20 6d  aluates one or m
152f0 6f 72 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e  ore.** semicolon
15300 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c 20 73  -separated SQL s
15310 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65  tatements in the
15320 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
15330 20 55 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e 67   UTF-8.** string
15340 20 6f 66 20 69 74 73 20 32 6e 64 20 70 61 72 61   of its 2nd para
15350 6d 65 74 65 72 2e 20 20 49 74 20 72 65 74 75 72  meter.  It retur
15360 6e 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c  ns a result tabl
15370 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69 6e  e to the.** poin
15380 74 65 72 20 67 69 76 65 6e 20 69 6e 20 69 74 73  ter given in its
15390 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 2e 0a   3rd parameter..
153a0 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20  **.** After the 
153b0 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
153c0 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73   has finished us
153d0 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2c 20  ing the result, 
153e0 69 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61 73  it should.** pas
153f0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  s the pointer to
15400 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c   the result tabl
15410 65 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65  e to sqlite3_fre
15420 65 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64  e_table() in ord
15430 65 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73 65  er to.** release
15440 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74   the memory that
15450 20 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20 20   was malloced.  
15460 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77  Because of the w
15470 61 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  ay the.** [sqlit
15480 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61 70  e3_malloc()] hap
15490 70 65 6e 73 20 77 69 74 68 69 6e 20 73 71 6c 69  pens within sqli
154a0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 2c  te3_get_table(),
154b0 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20   the calling.** 
154c0 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f  function must no
154d0 74 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b 73  t try to call [s
154e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 64  qlite3_free()] d
154f0 69 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a 2a  irectly.  Only.*
15500 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  * [sqlite3_free_
15510 74 61 62 6c 65 28 29 5d 20 69 73 20 61 62 6c 65  table()] is able
15520 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20   to release the 
15530 6d 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79 20  memory properly 
15540 61 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a 2a  and safely..**.*
15550 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65  * The sqlite3_ge
15560 74 5f 74 61 62 6c 65 28 29 20 69 6e 74 65 72 66  t_table() interf
15570 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  ace is implement
15580 65 64 20 61 73 20 61 20 77 72 61 70 70 65 72 20  ed as a wrapper 
15590 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74  around.** [sqlit
155a0 65 33 5f 65 78 65 63 28 29 5d 2e 20 20 54 68 65  e3_exec()].  The
155b0 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
155c0 6c 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  le() routine doe
155d0 73 20 6e 6f 74 20 68 61 76 65 20 61 63 63 65 73  s not have acces
155e0 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74 65  s.** to any inte
155f0 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74  rnal data struct
15600 75 72 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20  ures of SQLite. 
15610 20 49 74 20 75 73 65 73 20 6f 6e 6c 79 20 74 68   It uses only th
15620 65 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74 65  e public.** inte
15630 72 66 61 63 65 20 64 65 66 69 6e 65 64 20 68 65  rface defined he
15640 72 65 2e 20 20 41 73 20 61 20 63 6f 6e 73 65 71  re.  As a conseq
15650 75 65 6e 63 65 2c 20 65 72 72 6f 72 73 20 74 68  uence, errors th
15660 61 74 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a  at occur in the.
15670 2a 2a 20 77 72 61 70 70 65 72 20 6c 61 79 65 72  ** wrapper layer
15680 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20   outside of the 
15690 69 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74 65  internal [sqlite
156a0 33 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c 20 61  3_exec()] call a
156b0 72 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65 63  re not.** reflec
156c0 74 65 64 20 69 6e 20 73 75 62 73 65 71 75 65 6e  ted in subsequen
156d0 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  t calls to [sqli
156e0 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 6f  te3_errcode()] o
156f0 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  r [sqlite3_errms
15700 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  g()]..**.** Requ
15710 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
15720 32 33 37 31 5d 20 5b 48 31 32 33 37 33 5d 20 5b  2371] [H12373] [
15730 48 31 32 33 37 34 5d 20 5b 48 31 32 33 37 36 5d  H12374] [H12376]
15740 20 5b 48 31 32 33 37 39 5d 20 5b 48 31 32 33 38   [H12379] [H1238
15750 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  2].*/.SQLITE_API
15760 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74   int sqlite3_get
15770 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65  _table(.  sqlite
15780 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
15790 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62  /* An open datab
157a0 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ase */.  const c
157b0 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 2f  har *zSql,     /
157c0 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c  * SQL to be eval
157d0 75 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  uated */.  char 
157e0 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20  ***pazResult,   
157f0 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 74   /* Results of t
15800 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e  he query */.  in
15810 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20  t *pnRow,       
15820 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
15830 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69   result rows wri
15840 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69  tten here */.  i
15850 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20  nt *pnColumn,   
15860 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
15870 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
15880 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
15890 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d  .  char **pzErrm
158a0 73 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f  sg       /* Erro
158b0 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65  r msg written he
158c0 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  re */.);.SQLITE_
158d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
158e0 5f 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72  _free_table(char
158f0 20 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a   **result);../*.
15900 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72  ** CAPI3REF: For
15910 6d 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72  matted String Pr
15920 69 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73  inting Functions
15930 20 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 30   {H17400} <S7000
15940 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a  0><S20000>.**.**
15950 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
15960 61 72 65 20 77 6f 72 6b 61 6c 69 6b 65 73 20 6f  are workalikes o
15970 66 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22  f the "printf()"
15980 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74   family of funct
15990 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ions.** from the
159a0 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72   standard C libr
159b0 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ary..**.** The s
159c0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
159d0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70   and sqlite3_vmp
159e0 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73  rintf() routines
159f0 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20   write their.** 
15a00 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d  results into mem
15a10 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
15a20 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  m [sqlite3_mallo
15a30 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72  c()]..** The str
15a40 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79  ings returned by
15a50 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69   these two routi
15a60 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  nes should be.**
15a70 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71   released by [sq
15a80 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20  lite3_free()].  
15a90 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65  Both routines re
15aa0 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70  turn a.** NULL p
15ab0 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74  ointer if [sqlit
15ac0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20  e3_malloc()] is 
15ad0 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
15ae0 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d  te enough.** mem
15af0 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
15b00 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67  resulting string
15b10 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74  ..**.** In sqlit
15b20 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f  e3_snprintf() ro
15b30 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72  utine is similar
15b40 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22   to "snprintf()"
15b50 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61   from.** the sta
15b60 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e  ndard C library.
15b70 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20    The result is 
15b80 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
15b90 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c  .** buffer suppl
15ba0 69 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  ied as the secon
15bb0 64 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73  d parameter whos
15bc0 65 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20  e size is given 
15bd0 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  by.** the first 
15be0 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 65 20  parameter. Note 
15bf0 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f  that the order o
15c00 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74  f the.** first t
15c10 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69 73  wo parameters is
15c20 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20 73   reversed from s
15c30 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69 73  nprintf().  This
15c40 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f 72   is an.** histor
15c50 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74 68  ical accident th
15c60 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69 78  at cannot be fix
15c70 65 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b  ed without break
15c80 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73  ing.** backwards
15c90 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20   compatibility. 
15ca0 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20   Note also that 
15cb0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
15cc0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
15cd0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62  pointer to its b
15ce0 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66  uffer instead of
15cf0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
15d00 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 63 74  * characters act
15d10 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e  ually written in
15d20 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20 20  to the buffer.  
15d30 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a 2a  We admit that.**
15d40 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
15d50 68 61 72 61 63 74 65 72 73 20 77 72 69 74 74 65  haracters writte
15d60 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f 72  n would be a mor
15d70 65 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e 0a  e useful return.
15d80 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65 20  ** value but we 
15d90 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  cannot change th
15da0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
15db0 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72   of sqlite3_snpr
15dc0 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77 69  intf().** now wi
15dd0 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63  thout breaking c
15de0 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a  ompatibility..**
15df0 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20 74  .** As long as t
15e00 68 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 69  he buffer size i
15e10 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a  s greater than z
15e20 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e 70  ero, sqlite3_snp
15e30 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72 61  rintf().** guara
15e40 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 20 62  ntees that the b
15e50 75 66 66 65 72 20 69 73 20 61 6c 77 61 79 73 20  uffer is always 
15e60 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e  zero-terminated.
15e70 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 70    The first.** p
15e80 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73 20  arameter "n" is 
15e90 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20 6f  the total size o
15ea0 66 20 74 68 65 20 62 75 66 66 65 72 2c 20 69 6e  f the buffer, in
15eb0 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66 6f  cluding space fo
15ec0 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74 65  r.** the zero te
15ed0 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68  rminator.  So th
15ee0 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e 67  e longest string
15ef0 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d   that can be com
15f00 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74 74  pletely.** writt
15f10 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63  en will be n-1 c
15f20 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  haracters..**.**
15f30 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
15f40 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f  all implement so
15f50 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f  me additional fo
15f60 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69  rmatting.** opti
15f70 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65  ons that are use
15f80 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63  ful for construc
15f90 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65  ting SQL stateme
15fa0 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74  nts..** All of t
15fb0 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28  he usual printf(
15fc0 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74  ) formatting opt
15fd0 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20  ions apply.  In 
15fe0 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a  addition, there.
15ff0 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20  ** is are "%q", 
16000 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f  "%Q", and "%z" o
16010 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ptions..**.** Th
16020 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 6b  e %q option work
16030 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 61  s like %s in tha
16040 74 20 69 74 20 73 75 62 73 74 69 74 75 74 65 73  t it substitutes
16050 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74   a null-terminat
16060 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f  ed.** string fro
16070 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c  m the argument l
16080 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73  ist.  But %q als
16090 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20  o doubles every 
160a0 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a  '\'' character..
160b0 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65  ** %q is designe
160c0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
160d0 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61   a string litera
160e0 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20  l.  By doubling 
160f0 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 68 61  each '\''.** cha
16100 72 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65  racter it escape
16110 73 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72  s that character
16120 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74   and allows it t
16130 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e  o be inserted in
16140 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  to.** the string
16150 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d  ..**.** For exam
16160 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20  ple, assume the 
16170 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65 20  string variable 
16180 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 74  zText contains t
16190 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ext as follows:.
161a0 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
161b0 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72  e><pre>.**  char
161c0 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20   *zText = "It's 
161d0 61 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a  a happy day!";.*
161e0 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
161f0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20  uote>.**.** One 
16200 63 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78  can use this tex
16210 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74  t in an SQL stat
16220 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73  ement as follows
16230 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
16240 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68  ote><pre>.**  ch
16250 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74  ar *zSQL = sqlit
16260 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45  e3_mprintf("INSE
16270 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41  RT INTO table VA
16280 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65  LUES('%q')", zTe
16290 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33  xt);.**  sqlite3
162a0 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20  _exec(db, zSQL, 
162b0 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71  0, 0, 0);.**  sq
162c0 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29  lite3_free(zSQL)
162d0 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ;.** </pre></blo
162e0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42  ckquote>.**.** B
162f0 65 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f  ecause the %q fo
16300 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75  rmat string is u
16310 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68  sed, the '\'' ch
16320 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74  aracter in zText
16330 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61  .** is escaped a
16340 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72  nd the SQL gener
16350 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f  ated is as follo
16360 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ws:.**.** <block
16370 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
16380 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c  INSERT INTO tabl
16390 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73  e1 VALUES('It''s
163a0 20 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a   a happy day!').
163b0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b  ** </pre></block
163c0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  quote>.**.** Thi
163d0 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48  s is correct.  H
163e0 61 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e  ad we used %s in
163f0 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65  stead of %q, the
16400 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a   generated SQL.*
16410 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f  * would have loo
16420 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  ked like this:.*
16430 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
16440 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52  ><pre>.**  INSER
16450 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41  T INTO table1 VA
16460 4c 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70  LUES('It's a hap
16470 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f  py day!');.** </
16480 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
16490 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63  >.**.** This sec
164a0 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61  ond example is a
164b0 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72  n SQL syntax err
164c0 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61  or.  As a genera
164d0 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c  l rule you shoul
164e0 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20  d.** always use 
164f0 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73  %q instead of %s
16500 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20   when inserting 
16510 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69  text into a stri
16520 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a  ng literal..**.*
16530 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20  * The %Q option 
16540 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78  works like %q ex
16550 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64  cept it also add
16560 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20  s single quotes 
16570 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75  around.** the ou
16580 74 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74  tside of the tot
16590 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 64 69  al string.  Addi
165a0 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65  tionally, if the
165b0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68   parameter in th
165c0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6c 69  e.** argument li
165d0 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  st is a NULL poi
165e0 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74 69 74  nter, %Q substit
165f0 75 74 65 73 20 74 68 65 20 74 65 78 74 20 22 4e  utes the text "N
16600 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a 2a 2a  ULL" (without.**
16610 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 29 20   single quotes) 
16620 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20  in place of the 
16630 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f 2c 20  %Q option.  So, 
16640 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65  for example, one
16650 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a   could say:.**.*
16660 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
16670 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53  re>.**  char *zS
16680 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  QL = sqlite3_mpr
16690 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54  intf("INSERT INT
166a0 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 25  O table VALUES(%
166b0 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20  Q)", zText);.** 
166c0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
166d0 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29  , zSQL, 0, 0, 0)
166e0 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.**  sqlite3_fr
166f0 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70  ee(zSQL);.** </p
16700 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
16710 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
16720 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e 64 65  above will rende
16730 72 20 61 20 63 6f 72 72 65 63 74 20 53 51 4c 20  r a correct SQL 
16740 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65  statement in the
16750 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 62 6c   zSQL.** variabl
16760 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 7a 54  e even if the zT
16770 65 78 74 20 76 61 72 69 61 62 6c 65 20 69 73 20  ext variable is 
16780 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
16790 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 20 66  **.** The "%z" f
167a0 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e  ormatting option
167b0 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c   works exactly l
167c0 69 6b 65 20 22 25 73 22 20 77 69 74 68 20 74 68  ike "%s" with th
167d0 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 74 68  e.** addition th
167e0 61 74 20 61 66 74 65 72 20 74 68 65 20 73 74 72  at after the str
167f0 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72 65 61  ing has been rea
16800 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69 6e 74  d and copied int
16810 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c  o.** the result,
16820 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
16830 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74  ] is called on t
16840 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e  he input string.
16850 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 52 65 71   {END}.**.** Req
16860 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
16870 31 37 34 30 33 5d 20 5b 48 31 37 34 30 36 5d 20  17403] [H17406] 
16880 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a 53 51 4c 49  [H17407].*/.SQLI
16890 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
168a0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e  ite3_mprintf(con
168b0 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53  st char*,...);.S
168c0 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
168d0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66  sqlite3_vmprintf
168e0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61  (const char*, va
168f0 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 41  _list);.SQLITE_A
16900 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
16910 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c 63 68  _snprintf(int,ch
16920 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
16930 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   ...);../*.** CA
16940 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41  PI3REF: Memory A
16950 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73 79 73  llocation Subsys
16960 74 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c 53 32  tem {H17300} <S2
16970 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0000>.**.** The 
16980 53 51 4c 69 74 65 20 63 6f 72 65 20 20 75 73 65  SQLite core  use
16990 73 20 74 68 65 73 65 20 74 68 72 65 65 20 72 6f  s these three ro
169a0 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6f  utines for all o
169b0 66 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69 6e 74  f its own.** int
169c0 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  ernal memory all
169d0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 22  ocation needs. "
169e0 43 6f 72 65 22 20 69 6e 20 74 68 65 20 70 72 65  Core" in the pre
169f0 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 0a 2a  vious sentence.*
16a00 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75  * does not inclu
16a10 64 65 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73  de operating-sys
16a20 74 65 6d 20 73 70 65 63 69 66 69 63 20 56 46 53  tem specific VFS
16a30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
16a40 20 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f 77 73    The.** Windows
16a50 20 56 46 53 20 75 73 65 73 20 6e 61 74 69 76 65   VFS uses native
16a60 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72   malloc() and fr
16a70 65 65 28 29 20 66 6f 72 20 73 6f 6d 65 20 6f 70  ee() for some op
16a80 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  erations..**.** 
16a90 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  The sqlite3_mall
16aa0 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74  oc() routine ret
16ab0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
16ac0 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20  o a block.** of 
16ad0 6d 65 6d 6f 72 79 20 61 74 20 6c 65 61 73 74 20  memory at least 
16ae0 4e 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74  N bytes in lengt
16af0 68 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  h, where N is th
16b00 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20  e parameter..** 
16b10 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  If sqlite3_mallo
16b20 63 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  c() is unable to
16b30 20 6f 62 74 61 69 6e 20 73 75 66 66 69 63 69 65   obtain sufficie
16b40 6e 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d 6f 72  nt free.** memor
16b50 79 2c 20 69 74 20 72 65 74 75 72 6e 73 20 61 20  y, it returns a 
16b60 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 49  NULL pointer.  I
16b70 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  f the parameter 
16b80 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  N to.** sqlite3_
16b90 6d 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f  malloc() is zero
16ba0 20 6f 72 20 6e 65 67 61 74 69 76 65 20 74 68 65   or negative the
16bb0 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  n sqlite3_malloc
16bc0 28 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20  () returns.** a 
16bd0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
16be0 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69  .** Calling sqli
16bf0 74 65 33 5f 66 72 65 65 28 29 20 77 69 74 68 20  te3_free() with 
16c00 61 20 70 6f 69 6e 74 65 72 20 70 72 65 76 69 6f  a pointer previo
16c10 75 73 6c 79 20 72 65 74 75 72 6e 65 64 0a 2a 2a  usly returned.**
16c20 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   by sqlite3_mall
16c30 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  oc() or sqlite3_
16c40 72 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 61 73  realloc() releas
16c50 65 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 73  es that memory s
16c60 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d 69 67  o.** that it mig
16c70 68 74 20 62 65 20 72 65 75 73 65 64 2e 20 20 54  ht be reused.  T
16c80 68 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  he sqlite3_free(
16c90 29 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20  ) routine is.** 
16ca0 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 63 61  a no-op if is ca
16cb0 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c  lled with a NULL
16cc0 20 70 6f 69 6e 74 65 72 2e 20 20 50 61 73 73 69   pointer.  Passi
16cd0 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ng a NULL pointe
16ce0 72 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  r.** to sqlite3_
16cf0 66 72 65 65 28 29 20 69 73 20 68 61 72 6d 6c 65  free() is harmle
16d00 73 73 2e 20 20 41 66 74 65 72 20 62 65 69 6e 67  ss.  After being
16d10 20 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79 0a 2a   freed, memory.*
16d20 2a 20 73 68 6f 75 6c 64 20 6e 65 69 74 68 65 72  * should neither
16d30 20 62 65 20 72 65 61 64 20 6e 6f 72 20 77 72 69   be read nor wri
16d40 74 74 65 6e 2e 20 20 45 76 65 6e 20 72 65 61 64  tten.  Even read
16d50 69 6e 67 20 70 72 65 76 69 6f 75 73 6c 79 20 66  ing previously f
16d60 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6d  reed.** memory m
16d70 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61  ight result in a
16d80 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61   segmentation fa
16d90 75 6c 74 20 6f 72 20 6f 74 68 65 72 20 73 65 76  ult or other sev
16da0 65 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 4d 65  ere error..** Me
16db0 6d 6f 72 79 20 63 6f 72 72 75 70 74 69 6f 6e 2c  mory corruption,
16dc0 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20   a segmentation 
16dd0 66 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65 72 20  fault, or other 
16de0 73 65 76 65 72 65 20 65 72 72 6f 72 0a 2a 2a 20  severe error.** 
16df0 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 66 20  might result if 
16e00 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69  sqlite3_free() i
16e10 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20  s called with a 
16e20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  non-NULL pointer
16e30 20 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e 6f 74   that.** was not
16e40 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
16e50 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
16e60 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  or sqlite3_reall
16e70 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oc()..**.** The 
16e80 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
16e90 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65  ) interface atte
16ea0 6d 70 74 73 20 74 6f 20 72 65 73 69 7a 65 20 61  mpts to resize a
16eb0 0a 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f 72 79  .** prior memory
16ec0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62   allocation to b
16ed0 65 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74  e at least N byt
16ee0 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74  es, where N is t
16ef0 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72  he.** second par
16f00 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6d 65 6d  ameter.  The mem
16f10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74  ory allocation t
16f20 6f 20 62 65 20 72 65 73 69 7a 65 64 20 69 73 20  o be resized is 
16f30 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72  the first.** par
16f40 61 6d 65 74 65 72 2e 20 20 49 66 20 74 68 65 20  ameter.  If the 
16f50 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
16f60 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  to sqlite3_reall
16f70 6f 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e 55 4c  oc().** is a NUL
16f80 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 69  L pointer then i
16f90 74 73 20 62 65 68 61 76 69 6f 72 20 69 73 20 69  ts behavior is i
16fa0 64 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 6c 6c  dentical to call
16fb0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ing.** sqlite3_m
16fc0 61 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 20 4e  alloc(N) where N
16fd0 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   is the second p
16fe0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
16ff0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a  te3_realloc()..*
17000 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20  * If the second 
17010 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
17020 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69  ite3_realloc() i
17030 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67  s zero or.** neg
17040 61 74 69 76 65 20 74 68 65 6e 20 74 68 65 20 62  ative then the b
17050 65 68 61 76 69 6f 72 20 69 73 20 65 78 61 63 74  ehavior is exact
17060 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 63  ly the same as c
17070 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65  alling.** sqlite
17080 33 5f 66 72 65 65 28 50 29 20 77 68 65 72 65 20  3_free(P) where 
17090 50 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70  P is the first p
170a0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
170b0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a  te3_realloc()..*
170c0 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  * sqlite3_reallo
170d0 63 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f  c() returns a po
170e0 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72  inter to a memor
170f0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  y allocation.** 
17100 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79  of at least N by
17110 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 20 4e  tes in size or N
17120 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69 65 6e  ULL if sufficien
17130 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e 61 76  t memory is unav
17140 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 4d  ailable..** If M
17150 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   is the size of 
17160 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61  the prior alloca
17170 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e 28 4e  tion, then min(N
17180 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20  ,M) bytes.** of 
17190 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61  the prior alloca
171a0 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 64 20  tion are copied 
171b0 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  into the beginni
171c0 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72 65 74  ng of buffer ret
171d0 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69  urned.** by sqli
171e0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 61 6e  te3_realloc() an
171f0 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  d the prior allo
17200 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e  cation is freed.
17210 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 72  .** If sqlite3_r
17220 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73  ealloc() returns
17230 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20   NULL, then the 
17240 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
17250 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65 65 64  .** is not freed
17260 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f  ..**.** The memo
17270 72 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ry returned by s
17280 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
17290 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  and sqlite3_real
172a0 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c 77 61  loc().** is alwa
172b0 79 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 74  ys aligned to at
172c0 20 6c 65 61 73 74 20 61 6e 20 38 20 62 79 74 65   least an 8 byte
172d0 20 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e 44 7d   boundary. {END}
172e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  .**.** The defau
172f0 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  lt implementatio
17300 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
17310 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
17320 73 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74 68 65  stem uses.** the
17330 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61 6c 6c   malloc(), reall
17340 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20  oc() and free() 
17350 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20  provided by the 
17360 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61  standard C libra
17370 72 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32 7d 20  ry..** {H17382} 
17380 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69  However, if SQLi
17390 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77  te is compiled w
173a0 69 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54  ith the.** SQLIT
173b0 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d 3c 69  E_MEMORY_SIZE=<i
173c0 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65 70 72  >NNN</i> C prepr
173d0 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 28 77  ocessor macro (w
173e0 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a  here <i>NNN</i>.
173f0 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  ** is an integer
17400 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 63  ), then SQLite c
17410 72 65 61 74 65 20 61 20 73 74 61 74 69 63 20 61  reate a static a
17420 72 72 61 79 20 6f 66 20 61 74 20 6c 65 61 73 74  rray of at least
17430 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 62  .** <i>NNN</i> b
17440 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64  ytes in size and
17450 20 75 73 65 73 20 74 68 61 74 20 61 72 72 61 79   uses that array
17460 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20   for all of its 
17470 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d 6f 72  dynamic.** memor
17480 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65  y allocation nee
17490 64 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64 69 74  ds. {END}  Addit
174a0 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  ional memory all
174b0 6f 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73 0a 2a  ocator options.*
174c0 2a 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  * may be added i
174d0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
174e0 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51 4c 69  s..**.** In SQLi
174f0 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30  te version 3.5.0
17500 20 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74 20 77   and 3.5.1, it w
17510 61 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64  as possible to d
17520 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c  efine.** the SQL
17530 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f  ITE_OMIT_MEMORY_
17540 41 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69 63 68  ALLOCATION which
17550 20 77 6f 75 6c 64 20 63 61 75 73 65 20 74 68 65   would cause the
17560 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69 6d 70   built-in.** imp
17570 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
17580 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f  hese routines to
17590 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68   be omitted.  Th
175a0 61 74 20 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a  at capability.**
175b0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72   is no longer pr
175c0 6f 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20 62 75  ovided.  Only bu
175d0 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c  ilt-in memory al
175e0 6c 6f 63 61 74 6f 72 73 20 63 61 6e 20 62 65 20  locators can be 
175f0 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  used..**.** The 
17600 57 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74 65 72  Windows OS inter
17610 66 61 63 65 20 6c 61 79 65 72 20 63 61 6c 6c 73  face layer calls
17620 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 6d  .** the system m
17630 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65  alloc() and free
17640 28 29 20 64 69 72 65 63 74 6c 79 20 77 68 65 6e  () directly when
17650 20 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a 20 66   converting.** f
17660 69 6c 65 6e 61 6d 65 73 20 62 65 74 77 65 65 6e  ilenames between
17670 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64   the UTF-8 encod
17680 69 6e 67 20 75 73 65 64 20 62 79 20 53 51 4c 69  ing used by SQLi
17690 74 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74 65 76  te.** and whatev
176a0 65 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e 63 6f  er filename enco
176b0 64 69 6e 67 20 69 73 20 75 73 65 64 20 62 79 20  ding is used by 
176c0 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 57  the particular W
176d0 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 61 6c  indows.** instal
176e0 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20  lation.  Memory 
176f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
17700 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20  s are detected, 
17710 62 75 74 0a 2a 2a 20 74 68 65 79 20 61 72 65 20  but.** they are 
17720 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73  reported back as
17730 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45   [SQLITE_CANTOPE
17740 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  N] or.** [SQLITE
17750 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74  _IOERR] rather t
17760 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  han [SQLITE_NOME
17770 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  M]..**.** Requir
17780 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33  ements:.** [H173
17790 30 33 5d 20 5b 48 31 37 33 30 34 5d 20 5b 48 31  03] [H17304] [H1
177a0 37 33 30 35 5d 20 5b 48 31 37 33 30 36 5d 20 5b  7305] [H17306] [
177b0 48 31 37 33 31 30 5d 20 5b 48 31 37 33 31 32 5d  H17310] [H17312]
177c0 20 5b 48 31 37 33 31 35 5d 20 5b 48 31 37 33 31   [H17315] [H1731
177d0 38 5d 0a 2a 2a 20 5b 48 31 37 33 32 31 5d 20 5b  8].** [H17321] [
177e0 48 31 37 33 32 32 5d 20 5b 48 31 37 33 32 33 5d  H17322] [H17323]
177f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74  .**.** The point
17800 65 72 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  er arguments to 
17810 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
17820 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65   and [sqlite3_re
17830 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d 75 73 74  alloc()].** must
17840 20 62 65 20 65 69 74 68 65 72 20 4e 55 4c 4c 20   be either NULL 
17850 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 65 72 73  or else pointers
17860 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61   obtained from a
17870 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76 6f 63 61   prior.** invoca
17880 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33  tion of [sqlite3
17890 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73  _malloc()] or [s
178a0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
178b0 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 6e  ] that have.** n
178c0 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65 6c 65  ot yet been rele
178d0 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ased..**.** The 
178e0 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
178f0 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77 72 69   not read or wri
17900 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66 0a 2a  te any part of.*
17910 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  * a block of mem
17920 6f 72 79 20 61 66 74 65 72 20 69 74 20 68 61 73  ory after it has
17930 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75   been released u
17940 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  sing.** [sqlite3
17950 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b 73 71 6c  _free()] or [sql
17960 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 2e  ite3_realloc()].
17970 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
17980 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c  oid *sqlite3_mal
17990 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  loc(int);.SQLITE
179a0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
179b0 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 2a  e3_realloc(void*
179c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  , int);.SQLITE_A
179d0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
179e0 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a 2f 2a  free(void*);../*
179f0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65  .** CAPI3REF: Me
17a00 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72 20 53  mory Allocator S
17a10 74 61 74 69 73 74 69 63 73 20 7b 48 31 37 33 37  tatistics {H1737
17a20 30 7d 20 3c 53 33 30 32 31 30 3e 0a 2a 2a 0a 2a  0} <S30210>.**.*
17a30 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69 64 65  * SQLite provide
17a40 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e 74 65  s these two inte
17a50 72 66 61 63 65 73 20 66 6f 72 20 72 65 70 6f 72  rfaces for repor
17a60 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74 61 74  ting on the stat
17a70 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71  us.** of the [sq
17a80 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c  lite3_malloc()],
17a90 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
17aa0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
17ab0 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 72 6f  realloc()].** ro
17ac0 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20 66 6f  utines, which fo
17ad0 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  rm the built-in 
17ae0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
17af0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2a 0a  n subsystem..**.
17b00 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
17b10 0a 2a 2a 20 5b 48 31 37 33 37 31 5d 20 5b 48 31  .** [H17371] [H1
17b20 37 33 37 33 5d 20 5b 48 31 37 33 37 34 5d 20 5b  7373] [H17374] [
17b30 48 31 37 33 37 35 5d 0a 2a 2f 0a 53 51 4c 49 54  H17375].*/.SQLIT
17b40 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e  E_API sqlite3_in
17b50 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  t64 sqlite3_memo
17b60 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a 53  ry_used(void);.S
17b70 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
17b80 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
17b90 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72  memory_highwater
17ba0 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 3b  (int resetFlag);
17bb0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
17bc0 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d 20  : Pseudo-Random 
17bd0 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 6f 72  Number Generator
17be0 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30 30 30   {H17390} <S2000
17bf0 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  0>.**.** SQLite 
17c00 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 68 2d  contains a high-
17c10 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f 2d 72  quality pseudo-r
17c20 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
17c30 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 75 73  erator (PRNG) us
17c40 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 74 20  ed to.** select 
17c50 72 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20 7c 20  random [ROWID | 
17c60 52 4f 57 49 44 73 5d 20 77 68 65 6e 20 69 6e 73  ROWIDs] when ins
17c70 65 72 74 69 6e 67 20 6e 65 77 20 72 65 63 6f 72  erting new recor
17c80 64 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20  ds into a table 
17c90 74 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20  that.** already 
17ca0 75 73 65 73 20 74 68 65 20 6c 61 72 67 65 73 74  uses the largest
17cb0 20 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57 49 44   possible [ROWID
17cc0 5d 2e 20 20 54 68 65 20 50 52 4e 47 20 69 73 20  ].  The PRNG is 
17cd0 61 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a 2a 2a  also used for.**
17ce0 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 72 61   the build-in ra
17cf0 6e 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e 64 6f  ndom() and rando
17d00 6d 62 6c 6f 62 28 29 20 53 51 4c 20 66 75 6e 63  mblob() SQL func
17d10 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 6e 74  tions.  This int
17d20 65 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a 2a 2a  erface allows.**
17d30 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f   applications to
17d40 20 61 63 63 65 73 73 20 74 68 65 20 73 61 6d 65   access the same
17d50 20 50 52 4e 47 20 66 6f 72 20 6f 74 68 65 72 20   PRNG for other 
17d60 70 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a 2a 20  purposes..**.** 
17d70 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72  A call to this r
17d80 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 4e 20  outine stores N 
17d90 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e  bytes of randomn
17da0 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65 72 20  ess into buffer 
17db0 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  P..**.** The fir
17dc0 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75  st time this rou
17dd0 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 20  tine is invoked 
17de0 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e 61 6c  (either internal
17df0 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68 65 20  ly or by.** the 
17e00 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74 68 65  application) the
17e10 20 50 52 4e 47 20 69 73 20 73 65 65 64 65 64 20   PRNG is seeded 
17e20 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65 73 73  using randomness
17e30 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
17e40 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73  m the xRandomnes
17e50 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  s method of the 
17e60 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33  default [sqlite3
17e70 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  _vfs] object..**
17e80 20 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65   On all subseque
17e90 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2c 20  nt invocations, 
17ea0 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f  the pseudo-rando
17eb0 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72 61 74  mness is generat
17ec0 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c 79  ed.** internally
17ed0 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72 65 63   and without rec
17ee0 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b 73 71  ourse to the [sq
17ef0 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61 6e 64  lite3_vfs] xRand
17f00 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68 6f 64  omness.** method
17f10 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
17f20 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 39 32  ents:.** [H17392
17f30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
17f40 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 61 6e  void sqlite3_ran
17f50 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76  domness(int N, v
17f60 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20  oid *P);../*.** 
17f70 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c  CAPI3REF: Compil
17f80 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69 7a 61  e-Time Authoriza
17f90 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b  tion Callbacks {
17fa0 48 31 32 35 30 30 7d 20 3c 53 37 30 31 30 30 3e  H12500} <S70100>
17fb0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
17fc0 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 20  ine registers a 
17fd0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
17fe0 61 63 6b 20 77 69 74 68 20 61 20 70 61 72 74 69  ack with a parti
17ff0 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 62 61  cular.** [databa
18000 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2c 20  se connection], 
18010 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68 65 20  supplied in the 
18020 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a  first argument..
18030 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65  ** The authorize
18040 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  r callback is in
18050 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 73 74 61  voked as SQL sta
18060 74 65 6d 65 6e 74 73 20 61 72 65 20 62 65 69 6e  tements are bein
18070 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 62 79  g compiled.** by
18080 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
18090 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69  e()] or its vari
180a0 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 5f 70 72  ants [sqlite3_pr
180b0 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a 2a 2a 20  epare_v2()],.** 
180c0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
180d0 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  16()] and [sqlit
180e0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
180f0 29 5d 2e 20 20 41 74 20 76 61 72 69 6f 75 73 0a  )].  At various.
18100 2a 2a 20 70 6f 69 6e 74 73 20 64 75 72 69 6e 67  ** points during
18110 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e   the compilation
18120 20 70 72 6f 63 65 73 73 2c 20 61 73 20 6c 6f 67   process, as log
18130 69 63 20 69 73 20 62 65 69 6e 67 20 63 72 65 61  ic is being crea
18140 74 65 64 0a 2a 2a 20 74 6f 20 70 65 72 66 6f 72  ted.** to perfor
18150 6d 20 76 61 72 69 6f 75 73 20 61 63 74 69 6f 6e  m various action
18160 73 2c 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  s, the authorize
18170 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  r callback is in
18180 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 73 65 65 20  voked to.** see 
18190 69 66 20 74 68 6f 73 65 20 61 63 74 69 6f 6e 73  if those actions
181a0 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 20 20 54   are allowed.  T
181b0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
181c0 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 0a 2a 2a  llback should.**
181d0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
181e0 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77 20 74 68 65  OK] to allow the
181f0 20 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49 54 45   action, [SQLITE
18200 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69 73 61  _IGNORE] to disa
18210 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70 65 63  llow the.** spec
18220 69 66 69 63 20 61 63 74 69 6f 6e 20 62 75 74 20  ific action but 
18230 61 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20 73 74  allow the SQL st
18240 61 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e 74 69  atement to conti
18250 6e 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d  nue to be.** com
18260 70 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c 49 54  piled, or [SQLIT
18270 45 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75 73 65  E_DENY] to cause
18280 20 74 68 65 20 65 6e 74 69 72 65 20 53 51 4c 20   the entire SQL 
18290 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a  statement to be.
182a0 2a 2a 20 72 65 6a 65 63 74 65 64 20 77 69 74 68  ** rejected with
182b0 20 61 6e 20 65 72 72 6f 72 2e 20 20 49 66 20 74   an error.  If t
182c0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
182d0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a  llback returns.*
182e0 2a 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65  * any value othe
182f0 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 49  r than [SQLITE_I
18300 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f  GNORE], [SQLITE_
18310 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  OK], or [SQLITE_
18320 44 45 4e 59 5d 0a 2a 2a 20 74 68 65 6e 20 74 68  DENY].** then th
18330 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  e [sqlite3_prepa
18340 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69  re_v2()] or equi
18350 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74  valent call that
18360 20 74 72 69 67 67 65 72 65 64 0a 2a 2a 20 74 68   triggered.** th
18370 65 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c  e authorizer wil
18380 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65  l fail with an e
18390 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a  rror message..**
183a0 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 61 6c  .** When the cal
183b0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53  lback returns [S
183c0 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 68 61 74 20  QLITE_OK], that 
183d0 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74  means the operat
183e0 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  ion.** requested
183f0 20 69 73 20 6f 6b 2e 20 20 57 68 65 6e 20 74 68   is ok.  When th
18400 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
18410 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d  ns [SQLITE_DENY]
18420 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  , the.** [sqlite
18430 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
18440 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61  or equivalent ca
18450 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65  ll that triggere
18460 64 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72 69  d the.** authori
18470 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 69  zer will fail wi
18480 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
18490 61 67 65 20 65 78 70 6c 61 69 6e 69 6e 67 20 74  age explaining t
184a0 68 61 74 0a 2a 2a 20 61 63 63 65 73 73 20 69 73  hat.** access is
184b0 20 64 65 6e 69 65 64 2e 20 0a 2a 2a 0a 2a 2a 20   denied. .**.** 
184c0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
184d0 74 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f  ter to the autho
184e0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
184f0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
18500 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d 65 74  third.** paramet
18510 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  er to the sqlite
18520 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
18530 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 54 68  () interface. Th
18540 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
18550 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c  er.** to the cal
18560 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 65  lback is an inte
18570 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59  ger [SQLITE_COPY
18580 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65 5d 20   | action code] 
18590 74 68 61 74 20 73 70 65 63 69 66 69 65 73 0a 2a  that specifies.*
185a0 2a 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72  * the particular
185b0 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75   action to be au
185c0 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 20 74 68  thorized. The th
185d0 69 72 64 20 74 68 72 6f 75 67 68 20 73 69 78 74  ird through sixt
185e0 68 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  h parameters.** 
185f0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
18600 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  are zero-termina
18610 74 65 64 20 73 74 72 69 6e 67 73 20 74 68 61 74  ted strings that
18620 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 6f   contain additio
18630 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c 73 20 61  nal.** details a
18640 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20  bout the action 
18650 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64  to be authorized
18660 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61  ..**.** If the a
18670 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b 53  ction code is [S
18680 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 61  QLITE_READ].** a
18690 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  nd the callback 
186a0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
186b0 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65  IGNORE] then the
186c0 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
186d0 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d 65  atement] stateme
186e0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  nt is constructe
186f0 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 0a  d to substitute.
18700 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20  ** a NULL value 
18710 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20  in place of the 
18720 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
18730 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20  t would have.** 
18740 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51  been read if [SQ
18750 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65  LITE_OK] had bee
18760 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  n returned.  The
18770 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
18780 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 62  .** return can b
18790 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 61  e used to deny a
187a0 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 72  n untrusted user
187b0 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 76   access to indiv
187c0 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e 73  idual.** columns
187d0 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20   of a table..** 
187e0 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f  If the action co
187f0 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 45  de is [SQLITE_DE
18800 4c 45 54 45 5d 20 61 6e 64 20 74 68 65 20 63 61  LETE] and the ca
18810 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a  llback returns.*
18820 2a 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  * [SQLITE_IGNORE
18830 5d 20 74 68 65 6e 20 74 68 65 20 5b 44 45 4c 45  ] then the [DELE
18840 54 45 5d 20 6f 70 65 72 61 74 69 6f 6e 20 70 72  TE] operation pr
18850 6f 63 65 65 64 73 20 62 75 74 20 74 68 65 0a 2a  oceeds but the.*
18860 2a 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74 69  * [truncate opti
18870 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 20 64 69 73  mization] is dis
18880 61 62 6c 65 64 20 61 6e 64 20 61 6c 6c 20 72 6f  abled and all ro
18890 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 69  ws are deleted i
188a0 6e 64 69 76 69 64 75 61 6c 6c 79 2e 0a 2a 2a 0a  ndividually..**.
188b0 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72  ** An authorizer
188c0 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 5b 73   is used when [s
188d0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c  qlite3_prepare |
188e0 20 70 72 65 70 61 72 69 6e 67 5d 0a 2a 2a 20 53   preparing].** S
188f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 66 72  QL statements fr
18900 6f 6d 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20  om an untrusted 
18910 73 6f 75 72 63 65 2c 20 74 6f 20 65 6e 73 75 72  source, to ensur
18920 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73  e that the SQL s
18930 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 64 6f 20  tatements.** do 
18940 6e 6f 74 20 74 72 79 20 74 6f 20 61 63 63 65 73  not try to acces
18950 73 20 64 61 74 61 20 74 68 65 79 20 61 72 65 20  s data they are 
18960 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73  not allowed to s
18970 65 65 2c 20 6f 72 20 74 68 61 74 20 74 68 65 79  ee, or that they
18980 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74 72 79 20 74   do not.** try t
18990 6f 20 65 78 65 63 75 74 65 20 6d 61 6c 69 63 69  o execute malici
189a0 6f 75 73 20 73 74 61 74 65 6d 65 6e 74 73 20 74  ous statements t
189b0 68 61 74 20 64 61 6d 61 67 65 20 74 68 65 20 64  hat damage the d
189c0 61 74 61 62 61 73 65 2e 20 20 46 6f 72 0a 2a 2a  atabase.  For.**
189d0 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70   example, an app
189e0 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 61 6c 6c  lication may all
189f0 6f 77 20 61 20 75 73 65 72 20 74 6f 20 65 6e 74  ow a user to ent
18a00 65 72 20 61 72 62 69 74 72 61 72 79 0a 2a 2a 20  er arbitrary.** 
18a10 53 51 4c 20 71 75 65 72 69 65 73 20 66 6f 72 20  SQL queries for 
18a20 65 76 61 6c 75 61 74 69 6f 6e 20 62 79 20 61 20  evaluation by a 
18a30 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 20 74  database.  But t
18a40 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  he application d
18a50 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20  oes.** not want 
18a60 74 68 65 20 75 73 65 72 20 74 6f 20 62 65 20 61  the user to be a
18a70 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61 72 62 69  ble to make arbi
18a80 74 72 61 72 79 20 63 68 61 6e 67 65 73 20 74 6f  trary changes to
18a90 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
18aa0 2e 20 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72  .  An authorizer
18ab0 20 63 6f 75 6c 64 20 74 68 65 6e 20 62 65 20 70   could then be p
18ac0 75 74 20 69 6e 20 70 6c 61 63 65 20 77 68 69 6c  ut in place whil
18ad0 65 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 65 6e  e the.** user-en
18ae0 74 65 72 65 64 20 53 51 4c 20 69 73 20 62 65 69  tered SQL is bei
18af0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ng [sqlite3_prep
18b00 61 72 65 20 7c 20 70 72 65 70 61 72 65 64 5d 20  are | prepared] 
18b10 74 68 61 74 0a 2a 2a 20 64 69 73 61 6c 6c 6f 77  that.** disallow
18b20 73 20 65 76 65 72 79 74 68 69 6e 67 20 65 78 63  s everything exc
18b30 65 70 74 20 5b 53 45 4c 45 43 54 5d 20 73 74 61  ept [SELECT] sta
18b40 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 41  tements..**.** A
18b50 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
18b60 20 6e 65 65 64 20 74 6f 20 70 72 6f 63 65 73 73   need to process
18b70 20 53 51 4c 20 66 72 6f 6d 20 75 6e 74 72 75 73   SQL from untrus
18b80 74 65 64 20 73 6f 75 72 63 65 73 0a 2a 2a 20 6d  ted sources.** m
18b90 69 67 68 74 20 61 6c 73 6f 20 63 6f 6e 73 69 64  ight also consid
18ba0 65 72 20 6c 6f 77 65 72 69 6e 67 20 72 65 73 6f  er lowering reso
18bb0 75 72 63 65 20 6c 69 6d 69 74 73 20 75 73 69 6e  urce limits usin
18bc0 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  g [sqlite3_limit
18bd0 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c 69 6d 69 74  ()].** and limit
18be0 69 6e 67 20 64 61 74 61 62 61 73 65 20 73 69 7a  ing database siz
18bf0 65 20 75 73 69 6e 67 20 74 68 65 20 5b 6d 61 78  e using the [max
18c00 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52  _page_count] [PR
18c10 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20 61 64 64 69  AGMA].** in addi
18c20 74 69 6f 6e 20 74 6f 20 75 73 69 6e 67 20 61 6e  tion to using an
18c30 20 61 75 74 68 6f 72 69 7a 65 72 2e 0a 2a 2a 0a   authorizer..**.
18c40 2a 2a 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  ** Only a single
18c50 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6e 20   authorizer can 
18c60 62 65 20 69 6e 20 70 6c 61 63 65 20 6f 6e 20 61  be in place on a
18c70 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
18c80 74 69 6f 6e 0a 2a 2a 20 61 74 20 61 20 74 69 6d  tion.** at a tim
18c90 65 2e 20 20 45 61 63 68 20 63 61 6c 6c 20 74 6f  e.  Each call to
18ca0 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
18cb0 68 6f 72 69 7a 65 72 20 6f 76 65 72 72 69 64 65  horizer override
18cc0 73 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75  s the.** previou
18cd0 73 20 63 61 6c 6c 2e 20 20 44 69 73 61 62 6c 65  s call.  Disable
18ce0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
18cf0 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20 61 20  by installing a 
18d00 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a  NULL callback..*
18d10 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72  * The authorizer
18d20 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20   is disabled by 
18d30 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54  default..**.** T
18d40 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
18d50 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f 74 20  llback must not 
18d60 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74  do anything that
18d70 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20   will modify.** 
18d80 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
18d90 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76  nection that inv
18da0 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f 72 69  oked the authori
18db0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  zer callback..**
18dc0 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69   Note that [sqli
18dd0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
18de0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73  ] and [sqlite3_s
18df0 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69  tep()] both modi
18e00 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61  fy their.** data
18e10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
18e20 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67   for the meaning
18e30 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20   of "modify" in 
18e40 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a  this paragraph..
18e50 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b 73 71 6c 69  **.** When [sqli
18e60 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
18e70 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 65  ] is used to pre
18e80 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 6e 74  pare a statement
18e90 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65  , the.** stateme
18ea0 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65 70 72  nt might be repr
18eb0 65 70 61 72 65 64 20 64 75 72 69 6e 67 20 5b 73  epared during [s
18ec0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 64  qlite3_step()] d
18ed0 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73 63 68 65  ue to a .** sche
18ee0 6d 61 20 63 68 61 6e 67 65 2e 20 20 48 65 6e 63  ma change.  Henc
18ef0 65 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  e, the applicati
18f00 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73 75 72 65  on should ensure
18f10 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f 72   that the.** cor
18f20 72 65 63 74 20 61 75 74 68 6f 72 69 7a 65 72 20  rect authorizer 
18f30 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61 69 6e 73  callback remains
18f40 20 69 6e 20 70 6c 61 63 65 20 64 75 72 69 6e 67   in place during
18f50 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74   the [sqlite3_st
18f60 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  ep()]..**.** Not
18f70 65 20 74 68 61 74 20 74 68 65 20 61 75 74 68 6f  e that the autho
18f80 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
18f90 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 64  s invoked only d
18fa0 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65  uring.** [sqlite
18fb0 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20  3_prepare()] or 
18fc0 69 74 73 20 76 61 72 69 61 6e 74 73 2e 20 20 41  its variants.  A
18fd0 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 69 73 20  uthorization is 
18fe0 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64  not.** performed
18ff0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e   during statemen
19000 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 69 6e 20  t evaluation in 
19010 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
19020 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 61 73 20 73  , unless.** as s
19030 74 61 74 65 64 20 69 6e 20 74 68 65 20 70 72 65  tated in the pre
19040 76 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 2c  vious paragraph,
19050 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
19060 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 71 6c 69 74  invokes.** sqlit
19070 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20  e3_prepare_v2() 
19080 74 6f 20 72 65 70 72 65 70 61 72 65 20 61 20 73  to reprepare a s
19090 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 61  tatement after a
190a0 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a   schema change..
190b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
190c0 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 30 31 5d 20  ts:.** [H12501] 
190d0 5b 48 31 32 35 30 32 5d 20 5b 48 31 32 35 30 33  [H12502] [H12503
190e0 5d 20 5b 48 31 32 35 30 34 5d 20 5b 48 31 32 35  ] [H12504] [H125
190f0 30 35 5d 20 5b 48 31 32 35 30 36 5d 20 5b 48 31  05] [H12506] [H1
19100 32 35 30 37 5d 20 5b 48 31 32 35 31 30 5d 0a 2a  2507] [H12510].*
19110 2a 20 5b 48 31 32 35 31 31 5d 20 5b 48 31 32 35  * [H12511] [H125
19120 31 32 5d 20 5b 48 31 32 35 32 30 5d 20 5b 48 31  12] [H12520] [H1
19130 32 35 32 31 5d 20 5b 48 31 32 35 32 32 5d 0a 2a  2521] [H12522].*
19140 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
19150 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
19160 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  horizer(.  sqlit
19170 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78 41 75  e3*,.  int (*xAu
19180 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  th)(void*,int,co
19190 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
191a0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
191b0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 0a  *,const char*),.
191c0 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74    void *pUserDat
191d0 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  a.);../*.** CAPI
191e0 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a 65 72  3REF: Authorizer
191f0 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20 7b 48   Return Codes {H
19200 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30 3e 0a  12590} <H12500>.
19210 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  **.** The [sqlit
19220 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
19230 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63  r | authorizer c
19240 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
19250 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 72 6e  ] must.** return
19260 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 5f   either [SQLITE_
19270 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68  OK] or one of th
19280 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74  ese two constant
19290 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f  s in order.** to
192a0 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 20 77   signal SQLite w
192b0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
192c0 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72 6d  e action is perm
192d0 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 65 0a  itted.  See the.
192e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  ** [sqlite3_set_
192f0 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74  authorizer | aut
19300 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 6e 74  horizer document
19310 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 69 74  ation] for addit
19320 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ional.** informa
19330 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tion..*/.#define
19340 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 20 31   SQLITE_DENY   1
19350 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 65 20     /* Abort the 
19360 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 69  SQL statement wi
19370 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 23  th an error */.#
19380 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 47  define SQLITE_IG
19390 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f 6e 27  NORE 2   /* Don'
193a0 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 2c 20  t allow access, 
193b0 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 72 61  but don't genera
193c0 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 0a  te an error */..
193d0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
193e0 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 69 6f  Authorizer Actio
193f0 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35 30 7d  n Codes {H12550}
19400 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20   <H12500>.**.** 
19410 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74  The [sqlite3_set
19420 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20 69  _authorizer()] i
19430 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65  nterface registe
19440 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75  rs a callback fu
19450 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 69  nction.** that i
19460 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 75 74  s invoked to aut
19470 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e 20 53  horize certain S
19480 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 63 74  QL statement act
19490 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 73 65  ions.  The.** se
194a0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74  cond parameter t
194b0 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  o the callback i
194c0 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64  s an integer cod
194d0 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65 73  e that specifies
194e0 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f 6e 20  .** what action 
194f0 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69  is being authori
19500 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 65 20  zed.  These are 
19510 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 74 69  the integer acti
19520 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a 2a 2a  on codes that.**
19530 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
19540 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 65 20  callback may be 
19550 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  passed..**.** Th
19560 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20  ese action code 
19570 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 20 77  values signify w
19580 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 65 72  hat kind of oper
19590 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 0a 2a  ation is to be.*
195a0 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 54  * authorized.  T
195b0 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20 70  he 3rd and 4th p
195c0 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 65  arameters to the
195d0 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a 2a   authorization.*
195e0 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  * callback funct
195f0 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 72 61  ion will be para
19600 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20 64  meters or NULL d
19610 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63  epending on whic
19620 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 63 6f  h of these.** co
19630 64 65 73 20 69 73 20 75 73 65 64 20 61 73 20 74  des is used as t
19640 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
19650 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 70 61  ter.  The 5th pa
19660 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 0a 2a  rameter to the.*
19670 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  * authorizer cal
19680 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 61 6d  lback is the nam
19690 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
196a0 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70  e ("main", "temp
196b0 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66 20 61  ",.** etc.) if a
196c0 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68 65 20  pplicable.  The 
196d0 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  6th parameter to
196e0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
196f0 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 74  callback.** is t
19700 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69  he name of the i
19710 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67 67 65  nner-most trigge
19720 72 20 6f 72 20 76 69 65 77 20 74 68 61 74 20 69  r or view that i
19730 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
19740 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73 73 20  r.** the access 
19750 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c 4c 20  attempt or NULL 
19760 69 66 20 74 68 69 73 20 61 63 63 65 73 73 20 61  if this access a
19770 74 74 65 6d 70 74 20 69 73 20 64 69 72 65 63 74  ttempt is direct
19780 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70 2d 6c  ly from.** top-l
19790 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e 0a 2a  evel SQL code..*
197a0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
197b0 73 3a 0a 2a 2a 20 5b 48 31 32 35 35 31 5d 20 5b  s:.** [H12551] [
197c0 48 31 32 35 35 32 5d 20 5b 48 31 32 35 35 33 5d  H12552] [H12553]
197d0 20 5b 48 31 32 35 35 34 5d 0a 2a 2f 0a 2f 2a 2a   [H12554].*/./**
197e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
197f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19800 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64 20 2a 2a  ********* 3rd **
19810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74 68 20 2a  ********** 4th *
19820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66  **********/.#def
19830 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54  ine SQLITE_CREAT
19840 45 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  E_INDEX         
19850 20 31 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61   1   /* Index Na
19860 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61  me      Table Na
19870 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
19880 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
19890 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  _TABLE          
198a0 32 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  2   /* Table Nam
198b0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
198c0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
198d0 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
198e0 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 33  TEMP_INDEX     3
198f0 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65     /* Index Name
19900 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65        Table Name
19910 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
19920 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
19930 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 34 20  EMP_TABLE     4 
19940 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
19950 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
19960 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
19970 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
19980 4d 50 5f 54 52 49 47 47 45 52 20 20 20 35 20 20  MP_TRIGGER   5  
19990 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65   /* Trigger Name
199a0 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20      Table Name  
199b0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
199c0 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
199d0 50 5f 56 49 45 57 20 20 20 20 20 20 36 20 20 20  P_VIEW      6   
199e0 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20  /* View Name    
199f0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
19a00 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
19a10 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47  LITE_CREATE_TRIG
19a20 47 45 52 20 20 20 20 20 20 20 20 37 20 20 20 2f  GER        7   /
19a30 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20  * Trigger Name  
19a40 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
19a50 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
19a60 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 20  ITE_CREATE_VIEW 
19a70 20 20 20 20 20 20 20 20 20 20 38 20 20 20 2f 2a            8   /*
19a80 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20   View Name      
19a90 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
19aa0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
19ab0 54 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20  TE_DELETE       
19ac0 20 20 20 20 20 20 20 20 20 39 20 20 20 2f 2a 20           9   /* 
19ad0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
19ae0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
19af0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
19b00 45 5f 44 52 4f 50 5f 49 4e 44 45 58 20 20 20 20  E_DROP_INDEX    
19b10 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 49         10   /* I
19b20 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54  ndex Name      T
19b30 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
19b40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
19b50 5f 44 52 4f 50 5f 54 41 42 4c 45 20 20 20 20 20  _DROP_TABLE     
19b60 20 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 61        11   /* Ta
19b70 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55  ble Name      NU
19b80 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
19b90 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
19ba0 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 20  DROP_TEMP_INDEX 
19bb0 20 20 20 20 20 31 32 20 20 20 2f 2a 20 49 6e 64       12   /* Ind
19bc0 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62  ex Name      Tab
19bd0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  le Name      */.
19be0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
19bf0 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20  ROP_TEMP_TABLE  
19c00 20 20 20 20 31 33 20 20 20 2f 2a 20 54 61 62 6c      13   /* Tabl
19c10 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c  e Name      NULL
19c20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
19c30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
19c40 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20  OP_TEMP_TRIGGER 
19c50 20 20 20 31 34 20 20 20 2f 2a 20 54 72 69 67 67     14   /* Trigg
19c60 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65  er Name    Table
19c70 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64   Name      */.#d
19c80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
19c90 50 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20  P_TEMP_VIEW     
19ca0 20 20 31 35 20 20 20 2f 2a 20 56 69 65 77 20 4e    15   /* View N
19cb0 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20  ame       NULL  
19cc0 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
19cd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
19ce0 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20  _TRIGGER        
19cf0 20 31 36 20 20 20 2f 2a 20 54 72 69 67 67 65 72   16   /* Trigger
19d00 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e   Name    Table N
19d10 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
19d20 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
19d30 56 49 45 57 20 20 20 20 20 20 20 20 20 20 20 20  VIEW            
19d40 31 37 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d  17   /* View Nam
19d50 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  e       NULL    
19d60 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
19d70 6e 65 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  ne SQLITE_INSERT
19d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
19d90 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  8   /* Table Nam
19da0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
19db0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
19dc0 65 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 20  e SQLITE_PRAGMA 
19dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39                19
19de0 20 20 20 2f 2a 20 50 72 61 67 6d 61 20 4e 61 6d     /* Pragma Nam
19df0 65 20 20 20 20 20 31 73 74 20 61 72 67 20 6f 72  e     1st arg or
19e00 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
19e10 20 53 51 4c 49 54 45 5f 52 45 41 44 20 20 20 20   SQLITE_READ    
19e20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 20               20 
19e30 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
19e40 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65       Column Name
19e50 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
19e60 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 20 20 20  SQLITE_SELECT   
19e70 20 20 20 20 20 20 20 20 20 20 20 20 32 31 20 20              21  
19e80 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20   /* NULL        
19e90 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
19ea0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
19eb0 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f  QLITE_TRANSACTIO
19ec0 4e 20 20 20 20 20 20 20 20 20 20 32 32 20 20 20  N          22   
19ed0 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20  /* Operation    
19ee0 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
19ef0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
19f00 4c 49 54 45 5f 55 50 44 41 54 45 20 20 20 20 20  LITE_UPDATE     
19f10 20 20 20 20 20 20 20 20 20 20 32 33 20 20 20 2f            23   /
19f20 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
19f30 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20    Column Name   
19f40 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
19f50 49 54 45 5f 41 54 54 41 43 48 20 20 20 20 20 20  ITE_ATTACH      
19f60 20 20 20 20 20 20 20 20 20 32 34 20 20 20 2f 2a           24   /*
19f70 20 46 69 6c 65 6e 61 6d 65 20 20 20 20 20 20 20   Filename       
19f80 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
19f90 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
19fa0 54 45 5f 44 45 54 41 43 48 20 20 20 20 20 20 20  TE_DETACH       
19fb0 20 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 20          25   /* 
19fc0 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20  Database Name   
19fd0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
19fe0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
19ff0 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 20 20  E_ALTER_TABLE   
1a000 20 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20 44         26   /* D
1a010 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 54  atabase Name   T
1a020 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
1a030 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1a040 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20  _REINDEX        
1a050 20 20 20 20 20 20 32 37 20 20 20 2f 2a 20 49 6e        27   /* In
1a060 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 4e 55  dex Name      NU
1a070 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1a080 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1a090 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20  ANALYZE         
1a0a0 20 20 20 20 20 32 38 20 20 20 2f 2a 20 54 61 62       28   /* Tab
1a0b0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c  le Name      NUL
1a0c0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1a0d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1a0e0 52 45 41 54 45 5f 56 54 41 42 4c 45 20 20 20 20  REATE_VTABLE    
1a0f0 20 20 20 20 32 39 20 20 20 2f 2a 20 54 61 62 6c      29   /* Tabl
1a100 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75  e Name      Modu
1a110 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23  le Name     */.#
1a120 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
1a130 4f 50 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20  OP_VTABLE       
1a140 20 20 20 33 30 20 20 20 2f 2a 20 54 61 62 6c 65     30   /* Table
1a150 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c   Name      Modul
1a160 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64  e Name     */.#d
1a170 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
1a180 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20  CTION           
1a190 20 20 33 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20    31   /* NULL  
1a1a0 20 20 20 20 20 20 20 20 20 20 46 75 6e 63 74 69            Functi
1a1b0 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a 23 64 65  on Name   */.#de
1a1c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 41 56 45  fine SQLITE_SAVE
1a1d0 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20  POINT           
1a1e0 20 33 32 20 20 20 2f 2a 20 4f 70 65 72 61 74 69   32   /* Operati
1a1f0 6f 6e 20 20 20 20 20 20 20 53 61 76 65 70 6f 69  on       Savepoi
1a200 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a 23 64 65 66  nt Name  */.#def
1a210 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 50 59 20  ine SQLITE_COPY 
1a220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a230 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65   0   /* No longe
1a240 72 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  r used */../*.**
1a250 20 43 41 50 49 33 52 45 46 3a 20 54 72 61 63 69   CAPI3REF: Traci
1a260 6e 67 20 41 6e 64 20 50 72 6f 66 69 6c 69 6e 67  ng And Profiling
1a270 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 32 32   Functions {H122
1a280 38 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 20  80} <S60400>.** 
1a290 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
1a2a0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
1a2b0 73 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62  s register callb
1a2c0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  ack functions th
1a2d0 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  at can be used f
1a2e0 6f 72 0a 2a 2a 20 74 72 61 63 69 6e 67 20 61 6e  or.** tracing an
1a2f0 64 20 70 72 6f 66 69 6c 69 6e 67 20 74 68 65 20  d profiling the 
1a300 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 53 51 4c  execution of SQL
1a310 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
1a320 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20  ** The callback 
1a330 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65  function registe
1a340 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74  red by sqlite3_t
1a350 72 61 63 65 28 29 20 69 73 20 69 6e 76 6f 6b 65  race() is invoke
1a360 64 20 61 74 0a 2a 2a 20 76 61 72 69 6f 75 73 20  d at.** various 
1a370 74 69 6d 65 73 20 77 68 65 6e 20 61 6e 20 53 51  times when an SQ
1a380 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62  L statement is b
1a390 65 69 6e 67 20 72 75 6e 20 62 79 20 5b 73 71 6c  eing run by [sql
1a3a0 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a  ite3_step()]..**
1a3b0 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65   The callback re
1a3c0 74 75 72 6e 73 20 61 20 55 54 46 2d 38 20 72 65  turns a UTF-8 re
1a3d0 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 53  ndering of the S
1a3e0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78  QL statement tex
1a3f0 74 0a 2a 2a 20 61 73 20 74 68 65 20 73 74 61 74  t.** as the stat
1a400 65 6d 65 6e 74 20 66 69 72 73 74 20 62 65 67 69  ement first begi
1a410 6e 73 20 65 78 65 63 75 74 69 6e 67 2e 20 20 41  ns executing.  A
1a420 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61  dditional callba
1a430 63 6b 73 20 6f 63 63 75 72 0a 2a 2a 20 61 73 20  cks occur.** as 
1a440 65 61 63 68 20 74 72 69 67 67 65 72 65 64 20 73  each triggered s
1a450 75 62 70 72 6f 67 72 61 6d 20 69 73 20 65 6e 74  ubprogram is ent
1a460 65 72 65 64 2e 20 20 54 68 65 20 63 61 6c 6c 62  ered.  The callb
1a470 61 63 6b 73 20 66 6f 72 20 74 72 69 67 67 65 72  acks for trigger
1a480 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20 55  s.** contain a U
1a490 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d 65 6e 74  TF-8 SQL comment
1a4a0 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73   that identifies
1a4b0 20 74 68 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a   the trigger..**
1a4c0 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  .** The callback
1a4d0 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74   function regist
1a4e0 65 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ered by sqlite3_
1a4f0 70 72 6f 66 69 6c 65 28 29 20 69 73 20 69 6e 76  profile() is inv
1a500 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 61 63 68 20  oked.** as each 
1a510 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69  SQL statement fi
1a520 6e 69 73 68 65 73 2e 20 20 54 68 65 20 70 72 6f  nishes.  The pro
1a530 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f  file callback co
1a540 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 6f 72  ntains.** the or
1a550 69 67 69 6e 61 6c 20 73 74 61 74 65 6d 65 6e 74  iginal statement
1a560 20 74 65 78 74 20 61 6e 64 20 61 6e 20 65 73 74   text and an est
1a570 69 6d 61 74 65 20 6f 66 20 77 61 6c 6c 2d 63 6c  imate of wall-cl
1a580 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20 6f 66 20 68  ock time.** of h
1a590 6f 77 20 6c 6f 6e 67 20 74 68 61 74 20 73 74 61  ow long that sta
1a5a0 74 65 6d 65 6e 74 20 74 6f 6f 6b 20 74 6f 20 72  tement took to r
1a5b0 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  un..**.** Requir
1a5c0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
1a5d0 38 31 5d 20 5b 48 31 32 32 38 32 5d 20 5b 48 31  81] [H12282] [H1
1a5e0 32 32 38 33 5d 20 5b 48 31 32 32 38 34 5d 20 5b  2283] [H12284] [
1a5f0 48 31 32 32 38 35 5d 20 5b 48 31 32 32 38 37 5d  H12285] [H12287]
1a600 20 5b 48 31 32 32 38 38 5d 20 5b 48 31 32 32 38   [H12288] [H1228
1a610 39 5d 0a 2a 2a 20 5b 48 31 32 32 39 30 5d 0a 2a  9].** [H12290].*
1a620 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
1a630 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
1a640 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 74   void *sqlite3_t
1a650 72 61 63 65 28 73 71 6c 69 74 65 33 2a 2c 20 76  race(sqlite3*, v
1a660 6f 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f 69  oid(*xTrace)(voi
1a670 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c  d*,const char*),
1a680 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
1a690 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
1a6a0 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73 71  IMENTAL void *sq
1a6b0 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 73 71  lite3_profile(sq
1a6c0 6c 69 74 65 33 2a 2c 0a 20 20 20 76 6f 69 64 28  lite3*,.   void(
1a6d0 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a  *xProfile)(void*
1a6e0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c  ,const char*,sql
1a6f0 69 74 65 33 5f 75 69 6e 74 36 34 29 2c 20 76 6f  ite3_uint64), vo
1a700 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id*);../*.** CAP
1a710 49 33 52 45 46 3a 20 51 75 65 72 79 20 50 72 6f  I3REF: Query Pro
1a720 67 72 65 73 73 20 43 61 6c 6c 62 61 63 6b 73 20  gress Callbacks 
1a730 7b 48 31 32 39 31 30 7d 20 3c 53 36 30 34 30 30  {H12910} <S60400
1a740 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  >.**.** This rou
1a750 74 69 6e 65 20 63 6f 6e 66 69 67 75 72 65 73 20  tine configures 
1a760 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  a callback funct
1a770 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a 20 70 72 6f  ion - the.** pro
1a780 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2d  gress callback -
1a790 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64   that is invoked
1a7a0 20 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 75   periodically du
1a7b0 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a 20 72 75 6e  ring long.** run
1a7c0 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 5b 73  ning calls to [s
1a7d0 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 2c 20  qlite3_exec()], 
1a7e0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1a7f0 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   and.** [sqlite3
1a800 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 2e 20 20  _get_table()].  
1a810 41 6e 20 65 78 61 6d 70 6c 65 20 75 73 65 20 66  An example use f
1a820 6f 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72  or this.** inter
1a830 66 61 63 65 20 69 73 20 74 6f 20 6b 65 65 70 20  face is to keep 
1a840 61 20 47 55 49 20 75 70 64 61 74 65 64 20 64 75  a GUI updated du
1a850 72 69 6e 67 20 61 20 6c 61 72 67 65 20 71 75 65  ring a large que
1a860 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ry..**.** If the
1a870 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
1a880 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ck returns non-z
1a890 65 72 6f 2c 20 74 68 65 20 6f 70 65 72 61 74 69  ero, the operati
1a8a0 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74 65 72 72 75  on is.** interru
1a8b0 70 74 65 64 2e 20 20 54 68 69 73 20 66 65 61 74  pted.  This feat
1a8c0 75 72 65 20 63 61 6e 20 62 65 20 75 73 65 64 20  ure can be used 
1a8d0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 0a 2a  to implement a.*
1a8e0 2a 20 22 43 61 6e 63 65 6c 22 20 62 75 74 74 6f  * "Cancel" butto
1a8f0 6e 20 6f 6e 20 61 20 47 55 49 20 70 72 6f 67 72  n on a GUI progr
1a900 65 73 73 20 64 69 61 6c 6f 67 20 62 6f 78 2e 0a  ess dialog box..
1a910 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 67 72 65  **.** The progre
1a920 73 73 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20  ss handler must 
1a930 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20  not do anything 
1a940 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79  that will modify
1a950 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
1a960 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
1a970 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 70 72 6f   invoked the pro
1a980 67 72 65 73 73 20 68 61 6e 64 6c 65 72 2e 0a 2a  gress handler..*
1a990 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c  * Note that [sql
1a9a0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
1a9b0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
1a9c0 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64  step()] both mod
1a9d0 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74  ify their.** dat
1a9e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1a9f0 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e  s for the meanin
1aa00 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e  g of "modify" in
1aa10 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e   this paragraph.
1aa20 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
1aa30 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 31 31 5d  nts:.** [H12911]
1aa40 20 5b 48 31 32 39 31 32 5d 20 5b 48 31 32 39 31   [H12912] [H1291
1aa50 33 5d 20 5b 48 31 32 39 31 34 5d 20 5b 48 31 32  3] [H12914] [H12
1aa60 39 31 35 5d 20 5b 48 31 32 39 31 36 5d 20 5b 48  915] [H12916] [H
1aa70 31 32 39 31 37 5d 20 5b 48 31 32 39 31 38 5d 0a  12917] [H12918].
1aa80 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  **.*/.SQLITE_API
1aa90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 70 72   void sqlite3_pr
1aaa0 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 28 73  ogress_handler(s
1aab0 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 69 6e  qlite3*, int, in
1aac0 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69  t(*)(void*), voi
1aad0 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  d*);../*.** CAPI
1aae0 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67 20 41 20  3REF: Opening A 
1aaf0 4e 65 77 20 44 61 74 61 62 61 73 65 20 43 6f 6e  New Database Con
1ab00 6e 65 63 74 69 6f 6e 20 7b 48 31 32 37 30 30 7d  nection {H12700}
1ab10 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S40200>.**.** 
1ab20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f  These routines o
1ab30 70 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 61  pen an SQLite da
1ab40 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 6f 73  tabase file whos
1ab50 65 20 6e 61 6d 65 20 69 73 20 67 69 76 65 6e 20  e name is given 
1ab60 62 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e 61  by the.** filena
1ab70 6d 65 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65  me argument. The
1ab80 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65   filename argume
1ab90 6e 74 20 69 73 20 69 6e 74 65 72 70 72 65 74 65  nt is interprete
1aba0 64 20 61 73 20 55 54 46 2d 38 20 66 6f 72 0a 2a  d as UTF-8 for.*
1abb0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
1abc0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65   and sqlite3_ope
1abd0 6e 5f 76 32 28 29 20 61 6e 64 20 61 73 20 55 54  n_v2() and as UT
1abe0 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69  F-16 in the nati
1abf0 76 65 20 62 79 74 65 0a 2a 2a 20 6f 72 64 65 72  ve byte.** order
1ac00 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65   for sqlite3_ope
1ac10 6e 31 36 28 29 2e 20 41 20 5b 64 61 74 61 62 61  n16(). A [databa
1ac20 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68  se connection] h
1ac30 61 6e 64 6c 65 20 69 73 20 75 73 75 61 6c 6c 79  andle is usually
1ac40 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 20  .** returned in 
1ac50 2a 70 70 44 62 2c 20 65 76 65 6e 20 69 66 20 61  *ppDb, even if a
1ac60 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20  n error occurs. 
1ac70 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74   The only except
1ac80 69 6f 6e 20 69 73 20 74 68 61 74 0a 2a 2a 20 69  ion is that.** i
1ac90 66 20 53 51 4c 69 74 65 20 69 73 20 75 6e 61 62  f SQLite is unab
1aca0 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d  le to allocate m
1acb0 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68  emory to hold th
1acc0 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65  e [sqlite3] obje
1acd0 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 77 69  ct,.** a NULL wi
1ace0 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  ll be written in
1acf0 74 6f 20 2a 70 70 44 62 20 69 6e 73 74 65 61 64  to *ppDb instead
1ad00 20 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   of a pointer to
1ad10 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 0a 2a   the [sqlite3].*
1ad20 2a 20 6f 62 6a 65 63 74 2e 20 49 66 20 74 68 65  * object. If the
1ad30 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
1ad40 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63 72 65 61  ned (and/or crea
1ad50 74 65 64 29 20 73 75 63 63 65 73 73 66 75 6c 6c  ted) successfull
1ad60 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53 51 4c 49  y, then.** [SQLI
1ad70 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e  TE_OK] is return
1ad80 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 61  ed.  Otherwise a
1ad90 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  n [error code] i
1ada0 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  s returned.  The
1adb0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  .** [sqlite3_err
1adc0 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  msg()] or [sqlit
1add0 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 72  e3_errmsg16()] r
1ade0 6f 75 74 69 6e 65 73 20 63 61 6e 20 62 65 20 75  outines can be u
1adf0 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 0a 2a 2a  sed to obtain.**
1ae00 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67   an English lang
1ae10 75 61 67 65 20 64 65 73 63 72 69 70 74 69 6f 6e  uage description
1ae20 20 6f 66 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a   of the error..*
1ae30 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  *.** The default
1ae40 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68   encoding for th
1ae50 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20  e database will 
1ae60 62 65 20 55 54 46 2d 38 20 69 66 0a 2a 2a 20 73  be UTF-8 if.** s
1ae70 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72  qlite3_open() or
1ae80 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
1ae90 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64  () is called and
1aea0 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e 20 74 68  .** UTF-16 in th
1aeb0 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  e native byte or
1aec0 64 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f 6f  der if sqlite3_o
1aed0 70 65 6e 31 36 28 29 20 69 73 20 75 73 65 64 2e  pen16() is used.
1aee0 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f  .**.** Whether o
1aef0 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f  r not an error o
1af00 63 63 75 72 73 20 77 68 65 6e 20 69 74 20 69 73  ccurs when it is
1af10 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f 75 72 63   opened, resourc
1af20 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  es.** associated
1af30 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 61 62   with the [datab
1af40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
1af50 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64 20 62 65  handle should be
1af60 20 72 65 6c 65 61 73 65 64 20 62 79 0a 2a 2a 20   released by.** 
1af70 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 5b 73  passing it to [s
1af80 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20  qlite3_close()] 
1af90 77 68 65 6e 20 69 74 20 69 73 20 6e 6f 20 6c 6f  when it is no lo
1afa0 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e 0a 2a  nger required..*
1afb0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
1afc0 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e 74 65 72  _open_v2() inter
1afd0 66 61 63 65 20 77 6f 72 6b 73 20 6c 69 6b 65 20  face works like 
1afe0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 0a 2a  sqlite3_open().*
1aff0 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  * except that it
1b000 20 61 63 63 65 70 74 73 20 74 77 6f 20 61 64 64   accepts two add
1b010 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65  itional paramete
1b020 72 73 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  rs for additiona
1b030 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 6f 76 65  l control.** ove
1b040 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 62 61  r the new databa
1b050 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20  se connection.  
1b060 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  The flags parame
1b070 74 65 72 20 63 61 6e 20 74 61 6b 65 20 6f 6e 65  ter can take one
1b080 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f   of.** the follo
1b090 77 69 6e 67 20 74 68 72 65 65 20 76 61 6c 75 65  wing three value
1b0a0 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 63 6f  s, optionally co
1b0b0 6d 62 69 6e 65 64 20 77 69 74 68 20 74 68 65 20  mbined with the 
1b0c0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  .** [SQLITE_OPEN
1b0d0 5f 4e 4f 4d 55 54 45 58 5d 20 6f 72 20 5b 53 51  _NOMUTEX] or [SQ
1b0e0 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55  LITE_OPEN_FULLMU
1b0f0 54 45 58 5d 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a  TEX] flags:.**.*
1b100 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 5b 53  * <dl>.** <dt>[S
1b110 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
1b120 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NLY]</dt>.** <dd
1b130 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73  >The database is
1b140 20 6f 70 65 6e 65 64 20 69 6e 20 72 65 61 64 2d   opened in read-
1b150 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49 66 20 74  only mode.  If t
1b160 68 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73  he database does
1b170 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20   not.** already 
1b180 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20  exist, an error 
1b190 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64  is returned.</dd
1b1a0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c  >.**.** <dt>[SQL
1b1b0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
1b1c0 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TE]</dt>.** <dd>
1b1d0 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  The database is 
1b1e0 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69  opened for readi
1b1f0 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20 69  ng and writing i
1b200 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72 20 72  f possible, or r
1b210 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 20 69  eading.** only i
1b220 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 77 72  f the file is wr
1b230 69 74 65 20 70 72 6f 74 65 63 74 65 64 20 62 79  ite protected by
1b240 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
1b250 79 73 74 65 6d 2e 20 20 49 6e 20 65 69 74 68 65  ystem.  In eithe
1b260 72 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 64 61  r.** case the da
1b270 74 61 62 61 73 65 20 6d 75 73 74 20 61 6c 72 65  tabase must alre
1b280 61 64 79 20 65 78 69 73 74 2c 20 6f 74 68 65 72  ady exist, other
1b290 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 69 73  wise an error is
1b2a0 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a   returned.</dd>.
1b2b0 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54  **.** <dt>[SQLIT
1b2c0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
1b2d0 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  ] | [SQLITE_OPEN
1b2e0 5f 43 52 45 41 54 45 5d 3c 2f 64 74 3e 0a 2a 2a  _CREATE]</dt>.**
1b2f0 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73   <dd>The databas
1b300 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20  e is opened for 
1b310 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74  reading and writ
1b320 69 6e 67 2c 20 61 6e 64 20 69 73 20 63 72 65 61  ing, and is crea
1b330 74 65 73 20 69 74 20 69 66 0a 2a 2a 20 69 74 20  tes it if.** it 
1b340 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
1b350 20 65 78 69 73 74 2e 20 54 68 69 73 20 69 73 20   exist. This is 
1b360 74 68 65 20 62 65 68 61 76 69 6f 72 20 74 68 61  the behavior tha
1b370 74 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64  t is always used
1b380 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   for.** sqlite3_
1b390 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74  open() and sqlit
1b3a0 65 33 5f 6f 70 65 6e 31 36 28 29 2e 3c 2f 64 64  e3_open16().</dd
1b3b0 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a  >.** </dl>.**.**
1b3c0 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61   If the 3rd para
1b3d0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
1b3e0 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 6e 6f  _open_v2() is no
1b3f0 74 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20  t one of the.** 
1b400 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f  combinations sho
1b410 77 6e 20 61 62 6f 76 65 20 6f 72 20 6f 6e 65 20  wn above or one 
1b420 6f 66 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69  of the combinati
1b430 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 20  ons shown above 
1b440 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20 77 69 74 68  combined.** with
1b450 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45   the [SQLITE_OPE
1b460 4e 5f 4e 4f 4d 55 54 45 58 5d 20 6f 72 20 5b 53  N_NOMUTEX] or [S
1b470 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d  QLITE_OPEN_FULLM
1b480 55 54 45 58 5d 20 66 6c 61 67 73 2c 0a 2a 2a 20  UTEX] flags,.** 
1b490 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f  then the behavio
1b4a0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  r is undefined..
1b4b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 53 51  **.** If the [SQ
1b4c0 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45  LITE_OPEN_NOMUTE
1b4d0 58 5d 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  X] flag is set, 
1b4e0 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  then the databas
1b4f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
1b500 6f 70 65 6e 73 20 69 6e 20 74 68 65 20 6d 75 6c  opens in the mul
1b510 74 69 2d 74 68 72 65 61 64 20 5b 74 68 72 65 61  ti-thread [threa
1b520 64 69 6e 67 20 6d 6f 64 65 5d 20 61 73 20 6c 6f  ding mode] as lo
1b530 6e 67 20 61 73 20 74 68 65 20 73 69 6e 67 6c 65  ng as the single
1b540 2d 74 68 72 65 61 64 0a 2a 2a 20 6d 6f 64 65 20  -thread.** mode 
1b550 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 74  has not been set
1b560 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
1b570 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 20   or start-time. 
1b580 20 49 66 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49   If the.** [SQLI
1b590 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45  TE_OPEN_FULLMUTE
1b5a0 58 5d 20 66 6c 61 67 20 69 73 20 73 65 74 20 74  X] flag is set t
1b5b0 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
1b5c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e   connection open
1b5d0 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65 72 69  s.** in the seri
1b5e0 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e  alized [threadin
1b5f0 67 20 6d 6f 64 65 5d 20 75 6e 6c 65 73 73 20 73  g mode] unless s
1b600 69 6e 67 6c 65 2d 74 68 72 65 61 64 20 77 61 73  ingle-thread was
1b610 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73  .** previously s
1b620 65 6c 65 63 74 65 64 20 61 74 20 63 6f 6d 70 69  elected at compi
1b630 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74  le-time or start
1b640 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  -time..**.** If 
1b650 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  the filename is 
1b660 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e  ":memory:", then
1b670 20 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70   a private, temp
1b680 6f 72 61 72 79 20 69 6e 2d 6d 65 6d 6f 72 79 20  orary in-memory 
1b690 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 63  database.** is c
1b6a0 72 65 61 74 65 64 20 66 6f 72 20 74 68 65 20 63  reated for the c
1b6b0 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73  onnection.  This
1b6c0 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
1b6d0 61 73 65 20 77 69 6c 6c 20 76 61 6e 69 73 68 20  ase will vanish 
1b6e0 77 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61  when.** the data
1b6f0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1b700 69 73 20 63 6c 6f 73 65 64 2e 20 20 46 75 74 75  is closed.  Futu
1b710 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  re versions of S
1b720 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20 6d  QLite might.** m
1b730 61 6b 65 20 75 73 65 20 6f 66 20 61 64 64 69 74  ake use of addit
1b740 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c 20 66 69  ional special fi
1b750 6c 65 6e 61 6d 65 73 20 74 68 61 74 20 62 65 67  lenames that beg
1b760 69 6e 20 77 69 74 68 20 74 68 65 20 22 3a 22 20  in with the ":" 
1b770 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 74  character..** It
1b780 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20   is recommended 
1b790 74 68 61 74 20 77 68 65 6e 20 61 20 64 61 74 61  that when a data
1b7a0 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 61 63  base filename ac
1b7b0 74 75 61 6c 6c 79 20 64 6f 65 73 20 62 65 67 69  tually does begi
1b7c0 6e 20 77 69 74 68 0a 2a 2a 20 61 20 22 3a 22 20  n with.** a ":" 
1b7d0 63 68 61 72 61 63 74 65 72 20 79 6f 75 20 73 68  character you sh
1b7e0 6f 75 6c 64 20 70 72 65 66 69 78 20 74 68 65 20  ould prefix the 
1b7f0 66 69 6c 65 6e 61 6d 65 20 77 69 74 68 20 61 20  filename with a 
1b800 70 61 74 68 6e 61 6d 65 20 73 75 63 68 20 61 73  pathname such as
1b810 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 61 76 6f 69  .** "./" to avoi
1b820 64 20 61 6d 62 69 67 75 69 74 79 2e 0a 2a 2a 0a  d ambiguity..**.
1b830 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61  ** If the filena
1b840 6d 65 20 69 73 20 61 6e 20 65 6d 70 74 79 20 73  me is an empty s
1b850 74 72 69 6e 67 2c 20 74 68 65 6e 20 61 20 70 72  tring, then a pr
1b860 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79  ivate, temporary
1b870 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64 61 74 61  .** on-disk data
1b880 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63 72 65  base will be cre
1b890 61 74 65 64 2e 20 20 54 68 69 73 20 70 72 69 76  ated.  This priv
1b8a0 61 74 65 20 64 61 74 61 62 61 73 65 20 77 69 6c  ate database wil
1b8b0 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 69  l be.** automati
1b8c0 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 61 73  cally deleted as
1b8d0 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 64 61 74   soon as the dat
1b8e0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1b8f0 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a   is closed..**.*
1b900 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72  * The fourth par
1b910 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
1b920 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 74  3_open_v2() is t
1b930 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a  he name of the.*
1b940 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  * [sqlite3_vfs] 
1b950 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 66 69  object that defi
1b960 6e 65 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e  nes the operatin
1b970 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61  g system interfa
1b980 63 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e  ce that.** the n
1b990 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ew database conn
1b9a0 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73  ection should us
1b9b0 65 2e 20 20 49 66 20 74 68 65 20 66 6f 75 72 74  e.  If the fourt
1b9c0 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a  h parameter is.*
1b9d0 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  * a NULL pointer
1b9e0 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75 6c   then the defaul
1b9f0 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  t [sqlite3_vfs] 
1ba00 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 2e 0a  object is used..
1ba10 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65 20 74 6f  **.** <b>Note to
1ba20 20 57 69 6e 64 6f 77 73 20 75 73 65 72 73 3a 3c   Windows users:<
1ba30 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f 64 69 6e  /b>  The encodin
1ba40 67 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 66  g used for the f
1ba50 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74  ilename argument
1ba60 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f  .** of sqlite3_o
1ba70 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65  pen() and sqlite
1ba80 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d 75 73 74  3_open_v2() must
1ba90 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f 74 20 77   be UTF-8, not w
1baa0 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f 64 65 70  hatever.** codep
1bab0 61 67 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79  age is currently
1bac0 20 64 65 66 69 6e 65 64 2e 20 20 46 69 6c 65 6e   defined.  Filen
1bad0 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ames containing 
1bae0 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 0a 2a 2a  international.**
1baf0 20 63 68 61 72 61 63 74 65 72 73 20 6d 75 73 74   characters must
1bb00 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f   be converted to
1bb10 20 55 54 46 2d 38 20 70 72 69 6f 72 20 74 6f 20   UTF-8 prior to 
1bb20 70 61 73 73 69 6e 67 20 74 68 65 6d 20 69 6e 74  passing them int
1bb30 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  o.** sqlite3_ope
1bb40 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f  n() or sqlite3_o
1bb50 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a 2a 2a 20  pen_v2()..**.** 
1bb60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
1bb70 20 5b 48 31 32 37 30 31 5d 20 5b 48 31 32 37 30   [H12701] [H1270
1bb80 32 5d 20 5b 48 31 32 37 30 33 5d 20 5b 48 31 32  2] [H12703] [H12
1bb90 37 30 34 5d 20 5b 48 31 32 37 30 36 5d 20 5b 48  704] [H12706] [H
1bba0 31 32 37 30 37 5d 20 5b 48 31 32 37 30 39 5d 20  12707] [H12709] 
1bbb0 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 5b 48 31 32  [H12711].** [H12
1bbc0 37 31 32 5d 20 5b 48 31 32 37 31 33 5d 20 5b 48  712] [H12713] [H
1bbd0 31 32 37 31 34 5d 20 5b 48 31 32 37 31 37 5d 20  12714] [H12717] 
1bbe0 5b 48 31 32 37 31 39 5d 20 5b 48 31 32 37 32 31  [H12719] [H12721
1bbf0 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2f 0a 53 51  ] [H12723].*/.SQ
1bc00 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1bc10 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e  ite3_open(.  con
1bc20 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d  st char *filenam
1bc30 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  e,   /* Database
1bc40 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38   filename (UTF-8
1bc50 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  ) */.  sqlite3 *
1bc60 2a 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f  *ppDb          /
1bc70 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62  * OUT: SQLite db
1bc80 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51   handle */.);.SQ
1bc90 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1bca0 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a 20 20 63  ite3_open16(.  c
1bcb0 6f 6e 73 74 20 76 6f 69 64 20 2a 66 69 6c 65 6e  onst void *filen
1bcc0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61  ame,   /* Databa
1bcd0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46  se filename (UTF
1bce0 2d 31 36 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65  -16) */.  sqlite
1bcf0 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 20  3 **ppDb        
1bd00 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65    /* OUT: SQLite
1bd10 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b   db handle */.);
1bd20 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1bd30 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
1bd40 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66  .  const char *f
1bd50 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61  ilename,   /* Da
1bd60 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
1bd70 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c  (UTF-8) */.  sql
1bd80 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20  ite3 **ppDb,    
1bd90 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c       /* OUT: SQL
1bda0 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f  ite db handle */
1bdb0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
1bdc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
1bdd0 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ags */.  const c
1bde0 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20  har *zVfs       
1bdf0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53 20   /* Name of VFS 
1be00 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 2a 2f  module to use */
1be10 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
1be20 52 45 46 3a 20 45 72 72 6f 72 20 43 6f 64 65 73  REF: Error Codes
1be30 20 41 6e 64 20 4d 65 73 73 61 67 65 73 20 7b 48   And Messages {H
1be40 31 32 38 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a  12800} <S60200>.
1be50 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
1be60 33 5f 65 72 72 63 6f 64 65 28 29 20 69 6e 74 65  3_errcode() inte
1be70 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68  rface returns th
1be80 65 20 6e 75 6d 65 72 69 63 20 5b 72 65 73 75 6c  e numeric [resul
1be90 74 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a 20 5b 65  t code] or.** [e
1bea0 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
1beb0 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f 73  ode] for the mos
1bec0 74 20 72 65 63 65 6e 74 20 66 61 69 6c 65 64 20  t recent failed 
1bed0 73 71 6c 69 74 65 33 5f 2a 20 41 50 49 20 63 61  sqlite3_* API ca
1bee0 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  ll.** associated
1bef0 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 73   with a [databas
1bf00 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 49  e connection]. I
1bf10 66 20 61 20 70 72 69 6f 72 20 41 50 49 20 63 61  f a prior API ca
1bf20 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a 20 62 75 74  ll failed.** but
1bf30 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
1bf40 20 41 50 49 20 63 61 6c 6c 20 73 75 63 63 65 65   API call succee
1bf50 64 65 64 2c 20 74 68 65 20 72 65 74 75 72 6e 20  ded, the return 
1bf60 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 73 71  value from.** sq
1bf70 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20  lite3_errcode() 
1bf80 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 54  is undefined.  T
1bf90 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  he sqlite3_exten
1bfa0 64 65 64 5f 65 72 72 63 6f 64 65 28 29 0a 2a 2a  ded_errcode().**
1bfb0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 68   interface is th
1bfc0 65 20 73 61 6d 65 20 65 78 63 65 70 74 20 74 68  e same except th
1bfd0 61 74 20 69 74 20 61 6c 77 61 79 73 20 72 65 74  at it always ret
1bfe0 75 72 6e 73 20 74 68 65 20 0a 2a 2a 20 5b 65 78  urns the .** [ex
1bff0 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
1c000 64 65 5d 20 65 76 65 6e 20 77 68 65 6e 20 65 78  de] even when ex
1c010 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
1c020 64 65 73 20 61 72 65 0a 2a 2a 20 64 69 73 61 62  des are.** disab
1c030 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  led..**.** The s
1c040 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20  qlite3_errmsg() 
1c050 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  and sqlite3_errm
1c060 73 67 31 36 28 29 20 72 65 74 75 72 6e 20 45 6e  sg16() return En
1c070 67 6c 69 73 68 2d 6c 61 6e 67 75 61 67 65 0a 2a  glish-language.*
1c080 2a 20 74 65 78 74 20 74 68 61 74 20 64 65 73 63  * text that desc
1c090 72 69 62 65 73 20 74 68 65 20 65 72 72 6f 72 2c  ribes the error,
1c0a0 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d 38   as either UTF-8
1c0b0 20 6f 72 20 55 54 46 2d 31 36 20 72 65 73 70 65   or UTF-16 respe
1c0c0 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f  ctively..** Memo
1c0d0 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 65  ry to hold the e
1c0e0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72  rror message str
1c0f0 69 6e 67 20 69 73 20 6d 61 6e 61 67 65 64 20 69  ing is managed i
1c100 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2a 20 54 68  nternally..** Th
1c110 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 6f  e application do
1c120 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 77  es not need to w
1c130 6f 72 72 79 20 61 62 6f 75 74 20 66 72 65 65 69  orry about freei
1c140 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a  ng the result..*
1c150 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 65  * However, the e
1c160 72 72 6f 72 20 73 74 72 69 6e 67 20 6d 69 67 68  rror string migh
1c170 74 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e  t be overwritten
1c180 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 20   or deallocated 
1c190 62 79 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74  by.** subsequent
1c1a0 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68 65 72 20   calls to other 
1c1b0 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
1c1c0 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a   functions..**.*
1c1d0 2a 20 57 68 65 6e 20 74 68 65 20 73 65 72 69 61  * When the seria
1c1e0 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67  lized [threading
1c1f0 20 6d 6f 64 65 5d 20 69 73 20 69 6e 20 75 73 65   mode] is in use
1c200 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 68  , it might be th
1c210 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 61  e.** case that a
1c220 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 6f 63   second error oc
1c230 63 75 72 73 20 6f 6e 20 61 20 73 65 70 61 72 61  curs on a separa
1c240 74 65 20 74 68 72 65 61 64 20 69 6e 20 62 65 74  te thread in bet
1c250 77 65 65 6e 0a 2a 2a 20 74 68 65 20 74 69 6d 65  ween.** the time
1c260 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 65 72   of the first er
1c270 72 6f 72 20 61 6e 64 20 74 68 65 20 63 61 6c 6c  ror and the call
1c280 20 74 6f 20 74 68 65 73 65 20 69 6e 74 65 72 66   to these interf
1c290 61 63 65 73 2e 0a 2a 2a 20 57 68 65 6e 20 74 68  aces..** When th
1c2a0 61 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20  at happens, the 
1c2b0 73 65 63 6f 6e 64 20 65 72 72 6f 72 20 77 69 6c  second error wil
1c2c0 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 73 69  l be reported si
1c2d0 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 69 6e 74  nce these.** int
1c2e0 65 72 66 61 63 65 73 20 61 6c 77 61 79 73 20 72  erfaces always r
1c2f0 65 70 6f 72 74 20 74 68 65 20 6d 6f 73 74 20 72  eport the most r
1c300 65 63 65 6e 74 20 72 65 73 75 6c 74 2e 20 20 54  ecent result.  T
1c310 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 68 69 73 2c  o avoid.** this,
1c320 20 65 61 63 68 20 74 68 72 65 61 64 20 63 61 6e   each thread can
1c330 20 6f 62 74 61 69 6e 20 65 78 63 6c 75 73 69 76   obtain exclusiv
1c340 65 20 75 73 65 20 6f 66 20 74 68 65 20 5b 64 61  e use of the [da
1c350 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1c360 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 6e 76 6f 6b  n] D.** by invok
1c370 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  ing [sqlite3_mut
1c380 65 78 5f 65 6e 74 65 72 5d 28 5b 73 71 6c 69 74  ex_enter]([sqlit
1c390 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 29 29  e3_db_mutex](D))
1c3a0 20 62 65 66 6f 72 65 20 62 65 67 69 6e 6e 69 6e   before beginnin
1c3b0 67 0a 2a 2a 20 74 6f 20 75 73 65 20 44 20 61 6e  g.** to use D an
1c3c0 64 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69  d invoking [sqli
1c3d0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 5d  te3_mutex_leave]
1c3e0 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74  ([sqlite3_db_mut
1c3f0 65 78 5d 28 44 29 29 20 61 66 74 65 72 0a 2a 2a  ex](D)) after.**
1c400 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f 20 74 68   all calls to th
1c410 65 20 69 6e 74 65 72 66 61 63 65 73 20 6c 69 73  e interfaces lis
1c420 74 65 64 20 68 65 72 65 20 61 72 65 20 63 6f 6d  ted here are com
1c430 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  pleted..**.** If
1c440 20 61 6e 20 69 6e 74 65 72 66 61 63 65 20 66 61   an interface fa
1c450 69 6c 73 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ils with SQLITE_
1c460 4d 49 53 55 53 45 2c 20 74 68 61 74 20 6d 65 61  MISUSE, that mea
1c470 6e 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  ns the interface
1c480 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b 65 64 20  .** was invoked 
1c490 69 6e 63 6f 72 72 65 63 74 6c 79 20 62 79 20 74  incorrectly by t
1c4a0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20  he application. 
1c4b0 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74   In that case, t
1c4c0 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  he.** error code
1c4d0 20 61 6e 64 20 6d 65 73 73 61 67 65 20 6d 61 79   and message may
1c4e0 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 73   or may not be s
1c4f0 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  et..**.** Requir
1c500 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 38  ements:.** [H128
1c510 30 31 5d 20 5b 48 31 32 38 30 32 5d 20 5b 48 31  01] [H12802] [H1
1c520 32 38 30 33 5d 20 5b 48 31 32 38 30 37 5d 20 5b  2803] [H12807] [
1c530 48 31 32 38 30 38 5d 20 5b 48 31 32 38 30 39 5d  H12808] [H12809]
1c540 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1c550 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nt sqlite3_errco
1c560 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  de(sqlite3 *db);
1c570 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1c580 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
1c590 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33  _errcode(sqlite3
1c5a0 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50   *db);.SQLITE_AP
1c5b0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
1c5c0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c  lite3_errmsg(sql
1c5d0 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  ite3*);.SQLITE_A
1c5e0 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1c5f0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28  qlite3_errmsg16(
1c600 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
1c610 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20  * CAPI3REF: SQL 
1c620 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74  Statement Object
1c630 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30 31   {H13000} <H1301
1c640 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
1c650 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  {prepared statem
1c660 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20 73  ent} {prepared s
1c670 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a  tatements}.**.**
1c680 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
1c690 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72  this object repr
1c6a0 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20  esents a single 
1c6b0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  SQL statement..*
1c6c0 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73  * This object is
1c6d0 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e   variously known
1c6e0 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64 20   as a "prepared 
1c6f0 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61 0a  statement" or a.
1c700 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51 4c  ** "compiled SQL
1c710 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 73   statement" or s
1c720 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61 74  imply as a "stat
1c730 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68  ement"..**.** Th
1c740 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61 74  e life of a stat
1c750 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f 65  ement object goe
1c760 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65  s something like
1c770 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c   this:.**.** <ol
1c780 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74 65  >.** <li> Create
1c790 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e   the object usin
1c7a0 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  g [sqlite3_prepa
1c7b0 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72 65  re_v2()] or a re
1c7c0 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66 75  lated.**      fu
1c7d0 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20  nction..** <li> 
1c7e0 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 5b  Bind values to [
1c7f0 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 5d  host parameters]
1c800 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
1c810 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20  e3_bind_*().**  
1c820 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e 0a      interfaces..
1c830 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65 20  ** <li> Run the 
1c840 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b  SQL by calling [
1c850 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1c860 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65  one or more time
1c870 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65 74  s..** <li> Reset
1c880 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 75   the statement u
1c890 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65  sing [sqlite3_re
1c8a0 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20 62  set()] then go b
1c8b0 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 73  ack.**      to s
1c8c0 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73 20  tep 2.  Do this 
1c8d0 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69 6d  zero or more tim
1c8e0 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73 74  es..** <li> Dest
1c8f0 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20 75  roy the object u
1c900 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69  sing [sqlite3_fi
1c910 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f  nalize()]..** </
1c920 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 20  ol>.**.** Refer 
1c930 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  to documentation
1c940 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6d   on individual m
1c950 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f 72  ethods above for
1c960 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69   additional.** i
1c970 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  nformation..*/.t
1c980 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
1c990 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69 74  lite3_stmt sqlit
1c9a0 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20  e3_stmt;../*.** 
1c9b0 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74 69  CAPI3REF: Run-ti
1c9c0 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37 36  me Limits {H1276
1c9d0 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a  0} <S20600>.**.*
1c9e0 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
1c9f0 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a 65   allows the size
1ca00 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e 73   of various cons
1ca10 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69 6d  tructs to be lim
1ca20 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e  ited.** on a con
1ca30 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e 65  nection by conne
1ca40 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54 68  ction basis.  Th
1ca50 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
1ca60 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61 74  r is the.** [dat
1ca70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1ca80 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69 73  ] whose limit is
1ca90 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71 75   to be set or qu
1caa0 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73  eried.  The.** s
1cab0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
1cac0 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b 6c  is one of the [l
1cad0 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 5d  imit categories]
1cae0 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a 2a   that define a.*
1caf0 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73 74  * class of const
1cb00 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a 65  ructs to be size
1cb10 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20 74   limited.  The t
1cb20 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 69  hird parameter i
1cb30 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d  s the.** new lim
1cb40 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 73  it for that cons
1cb50 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e 63  truct.  The func
1cb60 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
1cb70 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a   old limit..**.*
1cb80 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69 6d  * If the new lim
1cb90 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76 65  it is a negative
1cba0 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69 6d   number, the lim
1cbb0 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  it is unchanged.
1cbc0 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d 69  .** For the limi
1cbd0 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53 51  t category of SQ
1cbe0 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20 74  LITE_LIMIT_XYZ t
1cbf0 68 65 72 65 20 69 73 20 61 20 0a 2a 2a 20 5b 6c  here is a .** [l
1cc00 69 6d 69 74 73 20 7c 20 68 61 72 64 20 75 70 70  imits | hard upp
1cc10 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a 20 73 65 74  er bound].** set
1cc20 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d 74 69   by a compile-ti
1cc30 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  me C preprocesso
1cc40 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20 0a 2a  r macro named .*
1cc50 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 53 51 4c 49  * [limits | SQLI
1cc60 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e 0a 2a 2a 20  TE_MAX_XYZ]..** 
1cc70 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f 22 20 69  (The "_LIMIT_" i
1cc80 6e 20 74 68 65 20 6e 61 6d 65 20 69 73 20 63 68  n the name is ch
1cc90 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41 58 5f 22  anged to "_MAX_"
1cca0 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74 73 20 74  .).** Attempts t
1ccb0 6f 20 69 6e 63 72 65 61 73 65 20 61 20 6c 69 6d  o increase a lim
1ccc0 69 74 20 61 62 6f 76 65 20 69 74 73 20 68 61 72  it above its har
1ccd0 64 20 75 70 70 65 72 20 62 6f 75 6e 64 20 61 72  d upper bound ar
1cce0 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79 20 74 72  e.** silently tr
1ccf0 75 6e 63 61 74 65 64 20 74 6f 20 74 68 65 20 68  uncated to the h
1cd00 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 2e  ard upper limit.
1cd10 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69 6d 65 20  .**.** Run time 
1cd20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e 74 65 6e  limits are inten
1cd30 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20 61  ded for use in a
1cd40 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
1cd50 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f 74 68 20   manage.** both 
1cd60 74 68 65 69 72 20 6f 77 6e 20 69 6e 74 65 72 6e  their own intern
1cd70 61 6c 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  al database and 
1cd80 61 6c 73 6f 20 64 61 74 61 62 61 73 65 73 20 74  also databases t
1cd90 68 61 74 20 61 72 65 20 63 6f 6e 74 72 6f 6c 6c  hat are controll
1cda0 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72 75 73 74  ed.** by untrust
1cdb0 65 64 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72  ed external sour
1cdc0 63 65 73 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65  ces.  An example
1cdd0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 69 67   application mig
1cde0 68 74 20 62 65 20 61 0a 2a 2a 20 77 65 62 20 62  ht be a.** web b
1cdf0 72 6f 77 73 65 72 20 74 68 61 74 20 68 61 73 20  rowser that has 
1ce00 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61 73 65  its own database
1ce10 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 68 69  s for storing hi
1ce20 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73 65 70  story and.** sep
1ce30 61 72 61 74 65 20 64 61 74 61 62 61 73 65 73 20  arate databases 
1ce40 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 4a 61  controlled by Ja
1ce50 76 61 53 63 72 69 70 74 20 61 70 70 6c 69 63 61  vaScript applica
1ce60 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64 65 64  tions downloaded
1ce70 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e 74 65  .** off the Inte
1ce80 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74 65 72  rnet.  The inter
1ce90 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20 63 61  nal databases ca
1cea0 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65 0a 2a  n be given the.*
1ceb0 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75 6c 74  * large, default
1cec0 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61 62 61   limits.  Databa
1ced0 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79 20 65  ses managed by e
1cee0 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 20  xternal sources 
1cef0 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65 6e 20  can.** be given 
1cf00 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c 69 6d  much smaller lim
1cf10 69 74 73 20 64 65 73 69 67 6e 65 64 20 74 6f 20  its designed to 
1cf20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69 61 6c  prevent a denial
1cf30 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a 20 61   of service.** a
1cf40 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f 70 65  ttack.  Develope
1cf50 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20 77 61  rs might also wa
1cf60 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20 5b 73  nt to use the [s
1cf70 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
1cf80 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e 74 65  rizer()].** inte
1cf90 72 66 61 63 65 20 74 6f 20 66 75 72 74 68 65 72  rface to further
1cfa0 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75 73 74   control untrust
1cfb0 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73 69 7a  ed SQL.  The siz
1cfc0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1cfd0 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20  e.** created by 
1cfe0 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73 63 72  an untrusted scr
1cff0 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e 74 61  ipt can be conta
1d000 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a  ined using the.*
1d010 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e  * [max_page_coun
1d020 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a 2a 0a  t] [PRAGMA]..**.
1d030 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d 65 20  ** New run-time 
1d040 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73  limit categories
1d050 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e   may be added in
1d060 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
1d070 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
1d080 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37 36 32  ents:.** [H12762
1d090 5d 20 5b 48 31 32 37 36 36 5d 20 5b 48 31 32 37  ] [H12766] [H127
1d0a0 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  69].*/.SQLITE_AP
1d0b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69  I int sqlite3_li
1d0c0 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  mit(sqlite3*, in
1d0d0 74 20 69 64 2c 20 69 6e 74 20 6e 65 77 56 61 6c  t id, int newVal
1d0e0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
1d0f0 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 6d  EF: Run-Time Lim
1d100 69 74 20 43 61 74 65 67 6f 72 69 65 73 20 7b 48  it Categories {H
1d110 31 32 37 39 30 7d 20 3c 48 31 32 37 36 30 3e 0a  12790} <H12760>.
1d120 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 6c 69  ** KEYWORDS: {li
1d130 6d 69 74 20 63 61 74 65 67 6f 72 79 7d 20 7b 6c  mit category} {l
1d140 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73 7d  imit categories}
1d150 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  .**.** These con
1d160 73 74 61 6e 74 73 20 64 65 66 69 6e 65 20 76 61  stants define va
1d170 72 69 6f 75 73 20 70 65 72 66 6f 72 6d 61 6e 63  rious performanc
1d180 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74 68 61 74  e limits.** that
1d190 20 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20   can be lowered 
1d1a0 61 74 20 72 75 6e 2d 74 69 6d 65 20 75 73 69 6e  at run-time usin
1d1b0 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  g [sqlite3_limit
1d1c0 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 79 6e 6f  ()]..** The syno
1d1d0 70 73 69 73 20 6f 66 20 74 68 65 20 6d 65 61 6e  psis of the mean
1d1e0 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69  ings of the vari
1d1f0 6f 75 73 20 6c 69 6d 69 74 73 20 69 73 20 73 68  ous limits is sh
1d200 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a 20 41 64  own below..** Ad
1d210 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
1d220 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c  tion is availabl
1d230 65 20 61 74 20 5b 6c 69 6d 69 74 73 20 7c 20 4c  e at [limits | L
1d240 69 6d 69 74 73 20 69 6e 20 53 51 4c 69 74 65 5d  imits in SQLite]
1d250 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20  ..**.** <dl>.** 
1d260 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1d270 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20  _LENGTH</dt>.** 
1d280 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
1d290 73 69 7a 65 20 6f 66 20 61 6e 79 20 73 74 72 69  size of any stri
1d2a0 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72 20 74 61  ng or BLOB or ta
1d2b0 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a 2a 2a 0a  ble row.<dd>.**.
1d2c0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
1d2d0 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 2f  MIT_SQL_LENGTH</
1d2e0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d  dt>.** <dd>The m
1d2f0 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
1d300 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
1d310 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  t.</dd>.**.** <d
1d320 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  t>SQLITE_LIMIT_C
1d330 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  OLUMN</dt>.** <d
1d340 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  d>The maximum nu
1d350 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1d360 69 6e 20 61 20 74 61 62 6c 65 20 64 65 66 69 6e  in a table defin
1d370 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74 68 65 0a  ition or in the.
1d380 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  ** result set of
1d390 20 61 20 5b 53 45 4c 45 43 54 5d 20 6f 72 20 74   a [SELECT] or t
1d3a0 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1d3b0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1d3c0 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 72 20 69  an index.** or i
1d3d0 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72  n an ORDER BY or
1d3e0 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1d3f0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1d400 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58  >SQLITE_LIMIT_EX
1d410 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a  PR_DEPTH</dt>.**
1d420 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d   <dd>The maximum
1d430 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61   depth of the pa
1d440 72 73 65 20 74 72 65 65 20 6f 6e 20 61 6e 79 20  rse tree on any 
1d450 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e  expression.</dd>
1d460 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
1d470 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  E_LIMIT_COMPOUND
1d480 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a 2a 2a 20  _SELECT</dt>.** 
1d490 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
1d4a0 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  number of terms 
1d4b0 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  in a compound SE
1d4c0 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 3c  LECT statement.<
1d4d0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
1d4e0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45  QLITE_LIMIT_VDBE
1d4f0 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  _OP</dt>.** <dd>
1d500 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1d510 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f  er of instructio
1d520 6e 73 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20  ns in a virtual 
1d530 6d 61 63 68 69 6e 65 20 70 72 6f 67 72 61 6d 0a  machine program.
1d540 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  ** used to imple
1d550 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73 74 61 74  ment an SQL stat
1d560 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ement.</dd>.**.*
1d570 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  * <dt>SQLITE_LIM
1d580 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c  IT_FUNCTION_ARG<
1d590 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
1d5a0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1d5b0 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 61  f arguments on a
1d5c0 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a   function.</dd>.
1d5d0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
1d5e0 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 3c  _LIMIT_ATTACHED<
1d5f0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
1d600 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1d610 66 20 5b 41 54 54 41 43 48 20 7c 20 61 74 74 61  f [ATTACH | atta
1d620 63 68 65 64 20 64 61 74 61 62 61 73 65 73 5d 2e  ched databases].
1d630 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
1d640 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b  SQLITE_LIMIT_LIK
1d650 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
1d660 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
1d670 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   maximum length 
1d680 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20 61  of the pattern a
1d690 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b  rgument to the [
1d6a0 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 5b 47 4c 4f  LIKE] or.** [GLO
1d6b0 42 5d 20 6f 70 65 72 61 74 6f 72 73 2e 3c 2f 64  B] operators.</d
1d6c0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
1d6d0 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42  ITE_LIMIT_VARIAB
1d6e0 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74 3e 0a 2a  LE_NUMBER</dt>.*
1d6f0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1d700 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 72 69  m number of vari
1d710 61 62 6c 65 73 20 69 6e 20 61 6e 20 53 51 4c 20  ables in an SQL 
1d720 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63  statement that c
1d730 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e 64 2e 3c  an.** be bound.<
1d740 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f  /dd>.** </dl>.*/
1d750 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1d760 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20 20 20 20  LIMIT_LENGTH    
1d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d780 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
1d790 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54  _LIMIT_SQL_LENGT
1d7a0 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
1d7b0 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
1d7c0 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 20 20  E_LIMIT_COLUMN  
1d7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d7e0 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
1d7f0 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45  TE_LIMIT_EXPR_DE
1d800 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20  PTH             
1d810 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
1d820 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55  ITE_LIMIT_COMPOU
1d830 4e 44 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20  ND_SELECT       
1d840 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51      4.#define SQ
1d850 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f  LITE_LIMIT_VDBE_
1d860 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OP              
1d870 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53       5.#define S
1d880 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43  QLITE_LIMIT_FUNC
1d890 54 49 4f 4e 5f 41 52 47 20 20 20 20 20 20 20 20  TION_ARG        
1d8a0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
1d8b0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
1d8c0 41 43 48 45 44 20 20 20 20 20 20 20 20 20 20 20  ACHED           
1d8d0 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65         7.#define
1d8e0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49   SQLITE_LIMIT_LI
1d8f0 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
1d900 48 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e  H       8.#defin
1d910 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  e SQLITE_LIMIT_V
1d920 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20  ARIABLE_NUMBER  
1d930 20 20 20 20 20 20 20 20 20 39 0a 0a 2f 2a 0a 2a           9../*.*
1d940 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70  * CAPI3REF: Comp
1d950 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20 53 74 61  iling An SQL Sta
1d960 74 65 6d 65 6e 74 20 7b 48 31 33 30 31 30 7d 20  tement {H13010} 
1d970 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b 45 59 57  <S10000>.** KEYW
1d980 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74 61 74 65  ORDS: {SQL state
1d990 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 7d 0a 2a  ment compiler}.*
1d9a0 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75 74 65 20  *.** To execute 
1d9b0 61 6e 20 53 51 4c 20 71 75 65 72 79 2c 20 69 74  an SQL query, it
1d9c0 20 6d 75 73 74 20 66 69 72 73 74 20 62 65 20 63   must first be c
1d9d0 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20 61 20 62  ompiled into a b
1d9e0 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70 72 6f 67  yte-code.** prog
1d9f0 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f 66  ram using one of
1da00 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e   these routines.
1da10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
1da20 20 61 72 67 75 6d 65 6e 74 2c 20 22 64 62 22 2c   argument, "db",
1da30 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65 20   is a [database 
1da40 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 74 61  connection] obta
1da50 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 70  ined from a.** p
1da60 72 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20  rior successful 
1da70 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
1da80 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74  _open()], [sqlit
1da90 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f 72  e3_open_v2()] or
1daa0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  .** [sqlite3_ope
1dab0 6e 31 36 28 29 5d 2e 20 20 54 68 65 20 64 61 74  n16()].  The dat
1dac0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1dad0 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20 62   must not have b
1dae0 65 65 6e 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a  een closed..**.*
1daf0 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
1db00 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20 69  ument, "zSql", i
1db10 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  s the statement 
1db20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c 20  to be compiled, 
1db30 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 69  encoded.** as ei
1db40 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54  ther UTF-8 or UT
1db50 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69 74  F-16.  The sqlit
1db60 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e 64  e3_prepare() and
1db70 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1db80 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61  _v2().** interfa
1db90 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20 61  ces use UTF-8, a
1dba0 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nd sqlite3_prepa
1dbb0 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69 74  re16() and sqlit
1dbc0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
1dbd0 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 2e  ).** use UTF-16.
1dbe0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42  .**.** If the nB
1dbf0 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  yte argument is 
1dc00 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20  less than zero, 
1dc10 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65 61  then zSql is rea
1dc20 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20 66  d up to the.** f
1dc30 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e  irst zero termin
1dc40 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20 69  ator. If nByte i
1dc50 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20  s non-negative, 
1dc60 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6d  then it is the m
1dc70 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65 72  aximum.** number
1dc80 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64 20   of  bytes read 
1dc90 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65 6e  from zSql.  When
1dca0 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65   nByte is non-ne
1dcb0 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20 7a  gative, the.** z
1dcc0 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73 20  Sql string ends 
1dcd0 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66 69  at either the fi
1dce0 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27 5c  rst '\000' or '\
1dcf0 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65 72  u0000' character
1dd00 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74 65   or.** the nByte
1dd10 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68 65  -th byte, whiche
1dd20 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e  ver comes first.
1dd30 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 6b   If the caller k
1dd40 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68 65  nows.** that the
1dd50 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e 67   supplied string
1dd60 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74   is nul-terminat
1dd70 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69  ed, then there i
1dd80 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65 72  s a small.** per
1dd90 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74 61  formance advanta
1dda0 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64 20  ge to be gained 
1ddb0 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e 42  by passing an nB
1ddc0 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74 68  yte parameter th
1ddd0 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74  at.** is equal t
1dde0 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
1ddf0 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e 70  bytes in the inp
1de00 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e 63  ut string <i>inc
1de10 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74 68  luding</i>.** th
1de20 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  e nul-terminator
1de30 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   bytes..**.** If
1de40 20 70 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20 4e   pzTail is not N
1de50 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69 6c  ULL then *pzTail
1de60 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e   is made to poin
1de70 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62  t to the first b
1de80 79 74 65 0a 2a 2a 20 70 61 73 74 20 74 68 65 20  yte.** past the 
1de90 65 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73 74  end of the first
1dea0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69   SQL statement i
1deb0 6e 20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20 72  n zSql.  These r
1dec0 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a 20  outines only.** 
1ded0 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 69 72 73  compile the firs
1dee0 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a  t statement in z
1def0 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c 20  Sql, so *pzTail 
1df00 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
1df10 20 74 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d 61   to.** what rema
1df20 69 6e 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a  ins uncompiled..
1df30 2a 2a 0a 2a 2a 20 2a 70 70 53 74 6d 74 20 69 73  **.** *ppStmt is
1df40 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74   left pointing t
1df50 6f 20 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70 72  o a compiled [pr
1df60 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1df70 5d 20 74 68 61 74 20 63 61 6e 20 62 65 0a 2a 2a  ] that can be.**
1df80 20 65 78 65 63 75 74 65 64 20 75 73 69 6e 67 20   executed using 
1df90 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1dfa0 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 61  .  If there is a
1dfb0 6e 20 65 72 72 6f 72 2c 20 2a 70 70 53 74 6d 74  n error, *ppStmt
1dfc0 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55   is set.** to NU
1dfd0 4c 4c 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75  LL.  If the inpu
1dfe0 74 20 74 65 78 74 20 63 6f 6e 74 61 69 6e 73 20  t text contains 
1dff0 6e 6f 20 53 51 4c 20 28 69 66 20 74 68 65 20 69  no SQL (if the i
1e000 6e 70 75 74 20 69 73 20 61 6e 20 65 6d 70 74 79  nput is an empty
1e010 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 72 20 61 20  .** string or a 
1e020 63 6f 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a 70  comment) then *p
1e030 70 53 74 6d 74 20 69 73 20 73 65 74 20 74 6f 20  pStmt is set to 
1e040 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65 20 63 61 6c  NULL..** The cal
1e050 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69  ling procedure i
1e060 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
1e070 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63  r deleting the c
1e080 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73  ompiled.** SQL s
1e090 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b  tatement using [
1e0a0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
1e0b0 28 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73  ()] after it has
1e0c0 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69   finished with i
1e0d0 74 2e 0a 2a 2a 20 70 70 53 74 6d 74 20 6d 61 79  t..** ppStmt may
1e0e0 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a   not be NULL..**
1e0f0 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20  .** On success, 
1e100 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72  [SQLITE_OK] is r
1e110 65 74 75 72 6e 65 64 2c 20 6f 74 68 65 72 77 69  eturned, otherwi
1e120 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64  se an [error cod
1e130 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  e] is returned..
1e140 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
1e150 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20 61  3_prepare_v2() a
1e160 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nd sqlite3_prepa
1e170 72 65 31 36 5f 76 32 28 29 20 69 6e 74 65 72 66  re16_v2() interf
1e180 61 63 65 73 20 61 72 65 0a 2a 2a 20 72 65 63 6f  aces are.** reco
1e190 6d 6d 65 6e 64 65 64 20 66 6f 72 20 61 6c 6c 20  mmended for all 
1e1a0 6e 65 77 20 70 72 6f 67 72 61 6d 73 2e 20 54 68  new programs. Th
1e1b0 65 20 74 77 6f 20 6f 6c 64 65 72 20 69 6e 74 65  e two older inte
1e1c0 72 66 61 63 65 73 20 61 72 65 20 72 65 74 61 69  rfaces are retai
1e1d0 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 61 63 6b 77  ned.** for backw
1e1e0 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
1e1f0 74 79 2c 20 62 75 74 20 74 68 65 69 72 20 75 73  ty, but their us
1e200 65 20 69 73 20 64 69 73 63 6f 75 72 61 67 65 64  e is discouraged
1e210 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 22 76 32 22  ..** In the "v2"
1e220 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65   interfaces, the
1e230 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1e240 65 6e 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 72  ent.** that is r
1e250 65 74 75 72 6e 65 64 20 28 74 68 65 20 5b 73 71  eturned (the [sq
1e260 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65  lite3_stmt] obje
1e270 63 74 29 20 63 6f 6e 74 61 69 6e 73 20 61 20 63  ct) contains a c
1e280 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 72  opy of the.** or
1e290 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 2e  iginal SQL text.
1e2a0 20 54 68 69 73 20 63 61 75 73 65 73 20 74 68 65   This causes the
1e2b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1e2c0 5d 20 69 6e 74 65 72 66 61 63 65 20 74 6f 0a 2a  ] interface to.*
1e2d0 2a 20 62 65 68 61 76 65 20 61 20 64 69 66 66 65  * behave a diffe
1e2e0 72 65 6e 74 6c 79 20 69 6e 20 74 77 6f 20 77 61  rently in two wa
1e2f0 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a  ys:.**.** <ol>.*
1e300 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 20 74 68 65  * <li>.** If the
1e310 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1e320 20 63 68 61 6e 67 65 73 2c 20 69 6e 73 74 65 61   changes, instea
1e330 64 20 6f 66 20 72 65 74 75 72 6e 69 6e 67 20 5b  d of returning [
1e340 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 61  SQLITE_SCHEMA] a
1e350 73 20 69 74 0a 2a 2a 20 61 6c 77 61 79 73 20 75  s it.** always u
1e360 73 65 64 20 74 6f 20 64 6f 2c 20 5b 73 71 6c 69  sed to do, [sqli
1e370 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c  te3_step()] will
1e380 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72   automatically r
1e390 65 63 6f 6d 70 69 6c 65 20 74 68 65 20 53 51 4c  ecompile the SQL
1e3a0 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  .** statement an
1e3b0 64 20 74 72 79 20 74 6f 20 72 75 6e 20 69 74 20  d try to run it 
1e3c0 61 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20 73  again.  If the s
1e3d0 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65  chema has change
1e3e0 64 20 69 6e 0a 2a 2a 20 61 20 77 61 79 20 74 68  d in.** a way th
1e3f0 61 74 20 6d 61 6b 65 73 20 74 68 65 20 73 74 61  at makes the sta
1e400 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72  tement no longer
1e410 20 76 61 6c 69 64 2c 20 5b 73 71 6c 69 74 65 33   valid, [sqlite3
1e420 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 73 74  _step()] will st
1e430 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53  ill.** return [S
1e440 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2e 20 20  QLITE_SCHEMA].  
1e450 42 75 74 20 75 6e 6c 69 6b 65 20 74 68 65 20 6c  But unlike the l
1e460 65 67 61 63 79 20 62 65 68 61 76 69 6f 72 2c 20  egacy behavior, 
1e470 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20  [SQLITE_SCHEMA] 
1e480 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 66 61 74 61  is.** now a fata
1e490 6c 20 65 72 72 6f 72 2e 20 20 43 61 6c 6c 69 6e  l error.  Callin
1e4a0 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  g [sqlite3_prepa
1e4b0 72 65 5f 76 32 28 29 5d 20 61 67 61 69 6e 20 77  re_v2()] again w
1e4c0 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 20 74 68 65  ill not make the
1e4d0 0a 2a 2a 20 65 72 72 6f 72 20 67 6f 20 61 77 61  .** error go awa
1e4e0 79 2e 20 20 4e 6f 74 65 3a 20 75 73 65 20 5b 73  y.  Note: use [s
1e4f0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d  qlite3_errmsg()]
1e500 20 74 6f 20 66 69 6e 64 20 74 68 65 20 74 65 78   to find the tex
1e510 74 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 73  t.** of the pars
1e520 69 6e 67 20 65 72 72 6f 72 20 74 68 61 74 20 72  ing error that r
1e530 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 5b 53 51  esults in an [SQ
1e540 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 72 65 74  LITE_SCHEMA] ret
1e550 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a  urn..** </li>.**
1e560 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 57 68 65 6e  .** <li>.** When
1e570 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1e580 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  , [sqlite3_step(
1e590 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 6f  )] will return o
1e5a0 6e 65 20 6f 66 20 74 68 65 20 64 65 74 61 69 6c  ne of the detail
1e5b0 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64  ed.** [error cod
1e5c0 65 73 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64  es] or [extended
1e5d0 20 65 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20 20   error codes].  
1e5e0 54 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76  The legacy behav
1e5f0 69 6f 72 20 77 61 73 20 74 68 61 74 0a 2a 2a 20  ior was that.** 
1e600 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1e610 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74 75   would only retu
1e620 72 6e 20 61 20 67 65 6e 65 72 69 63 20 5b 53 51  rn a generic [SQ
1e630 4c 49 54 45 5f 45 52 52 4f 52 5d 20 72 65 73 75  LITE_ERROR] resu
1e640 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 79  lt code.** and y
1e650 6f 75 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f  ou would have to
1e660 20 6d 61 6b 65 20 61 20 73 65 63 6f 6e 64 20 63   make a second c
1e670 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
1e680 72 65 73 65 74 28 29 5d 20 69 6e 20 6f 72 64 65  reset()] in orde
1e690 72 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65  r.** to find the
1e6a0 20 75 6e 64 65 72 6c 79 69 6e 67 20 63 61 75 73   underlying caus
1e6b0 65 20 6f 66 20 74 68 65 20 70 72 6f 62 6c 65 6d  e of the problem
1e6c0 2e 20 57 69 74 68 20 74 68 65 20 22 76 32 22 20  . With the "v2" 
1e6d0 70 72 65 70 61 72 65 0a 2a 2a 20 69 6e 74 65 72  prepare.** inter
1e6e0 66 61 63 65 73 2c 20 74 68 65 20 75 6e 64 65 72  faces, the under
1e6f0 6c 79 69 6e 67 20 72 65 61 73 6f 6e 20 66 6f 72  lying reason for
1e700 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 72 65   the error is re
1e710 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65  turned immediate
1e720 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20  ly..** </li>.** 
1e730 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  </ol>.**.** Requ
1e740 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
1e750 33 30 31 31 5d 20 5b 48 31 33 30 31 32 5d 20 5b  3011] [H13012] [
1e760 48 31 33 30 31 33 5d 20 5b 48 31 33 30 31 34 5d  H13013] [H13014]
1e770 20 5b 48 31 33 30 31 35 5d 20 5b 48 31 33 30 31   [H13015] [H1301
1e780 36 5d 20 5b 48 31 33 30 31 39 5d 20 5b 48 31 33  6] [H13019] [H13
1e790 30 32 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  021].**.*/.SQLIT
1e7a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1e7b0 33 5f 70 72 65 70 61 72 65 28 0a 20 20 73 71 6c  3_prepare(.  sql
1e7c0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
1e7d0 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1e7e0 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
1e7f0 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
1e800 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74       /* SQL stat
1e810 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63  ement, UTF-8 enc
1e820 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  oded */.  int nB
1e830 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  yte,            
1e840 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e    /* Maximum len
1e850 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
1e860 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ytes. */.  sqlit
1e870 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
1e880 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65  ,  /* OUT: State
1e890 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  ment handle */. 
1e8a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
1e8b0 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a  Tail     /* OUT:
1e8c0 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73   Pointer to unus
1e8d0 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53  ed portion of zS
1e8e0 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  ql */.);.SQLITE_
1e8f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1e900 70 72 65 70 61 72 65 5f 76 32 28 0a 20 20 73 71  prepare_v2(.  sq
1e910 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
1e920 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
1e930 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  e handle */.  co
1e940 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
1e950 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61        /* SQL sta
1e960 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e  tement, UTF-8 en
1e970 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  coded */.  int n
1e980 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  Byte,           
1e990 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65     /* Maximum le
1e9a0 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
1e9b0 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
1e9c0 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
1e9d0 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74  t,  /* OUT: Stat
1e9e0 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a  ement handle */.
1e9f0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
1ea00 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54  zTail     /* OUT
1ea10 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75  : Pointer to unu
1ea20 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a  sed portion of z
1ea30 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  Sql */.);.SQLITE
1ea40 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1ea50 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20 73 71  _prepare16(.  sq
1ea60 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
1ea70 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
1ea80 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  e handle */.  co
1ea90 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20  nst void *zSql, 
1eaa0 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61        /* SQL sta
1eab0 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65  tement, UTF-16 e
1eac0 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ncoded */.  int 
1ead0 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20  nByte,          
1eae0 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
1eaf0 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
1eb00 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
1eb10 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
1eb20 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61  mt,  /* OUT: Sta
1eb30 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f  tement handle */
1eb40 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a  .  const void **
1eb50 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55  pzTail     /* OU
1eb60 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e  T: Pointer to un
1eb70 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20  used portion of 
1eb80 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54  zSql */.);.SQLIT
1eb90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1eba0 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 0a  3_prepare16_v2(.
1ebb0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
1ebc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1ebd0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
1ebe0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53    const void *zS
1ebf0 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ql,       /* SQL
1ec00 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d   statement, UTF-
1ec10 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  16 encoded */.  
1ec20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20  int nByte,      
1ec30 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
1ec40 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71  um length of zSq
1ec50 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
1ec60 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
1ec70 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a  ppStmt,  /* OUT:
1ec80 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   Statement handl
1ec90 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e */.  const voi
1eca0 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f  d **pzTail     /
1ecb0 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74  * OUT: Pointer t
1ecc0 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e  o unused portion
1ecd0 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 0a   of zSql */.);..
1ece0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1ecf0 52 65 74 72 69 65 76 69 6e 67 20 53 74 61 74 65  Retrieving State
1ed00 6d 65 6e 74 20 53 51 4c 20 7b 48 31 33 31 30 30  ment SQL {H13100
1ed10 7d 20 3c 48 31 33 30 30 30 3e 0a 2a 2a 0a 2a 2a  } <H13000>.**.**
1ed20 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
1ed30 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72  can be used to r
1ed40 65 74 72 69 65 76 65 20 61 20 73 61 76 65 64 20  etrieve a saved 
1ed50 63 6f 70 79 20 6f 66 20 74 68 65 20 6f 72 69 67  copy of the orig
1ed60 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20 74 65 78 74  inal.** SQL text
1ed70 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
1ed80 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
1ed90 65 6d 65 6e 74 5d 20 69 66 20 74 68 61 74 20 73  ement] if that s
1eda0 74 61 74 65 6d 65 6e 74 20 77 61 73 0a 2a 2a 20  tatement was.** 
1edb0 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 65  compiled using e
1edc0 69 74 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70  ither [sqlite3_p
1edd0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
1ede0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1edf0 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  16_v2()]..**.** 
1ee00 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
1ee10 20 5b 48 31 33 31 30 31 5d 20 5b 48 31 33 31 30   [H13101] [H1310
1ee20 32 5d 20 5b 48 31 33 31 30 33 5d 0a 2a 2f 0a 53  2] [H13103].*/.S
1ee30 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1ee40 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71  char *sqlite3_sq
1ee50 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  l(sqlite3_stmt *
1ee60 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
1ee70 41 50 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 63  API3REF: Dynamic
1ee80 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65  ally Typed Value
1ee90 20 4f 62 6a 65 63 74 20 7b 48 31 35 30 30 30 7d   Object {H15000}
1eea0 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59   <S20200>.** KEY
1eeb0 57 4f 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 65  WORDS: {protecte
1eec0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d  d sqlite3_value}
1eed0 20 7b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71   {unprotected sq
1eee0 6c 69 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a  lite3_value}.**.
1eef0 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74  ** SQLite uses t
1ef00 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  he sqlite3_value
1ef10 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 70 72 65   object to repre
1ef20 73 65 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a  sent all values.
1ef30 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 73  ** that can be s
1ef40 74 6f 72 65 64 20 69 6e 20 61 20 64 61 74 61 62  tored in a datab
1ef50 61 73 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 74  ase table. SQLit
1ef60 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20 74  e uses dynamic t
1ef70 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65  yping.** for the
1ef80 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65   values it store
1ef90 73 2e 20 56 61 6c 75 65 73 20 73 74 6f 72 65 64  s. Values stored
1efa0 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   in sqlite3_valu
1efb0 65 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e  e objects.** can
1efc0 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c   be integers, fl
1efd0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
1efe0 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c  ues, strings, BL
1eff0 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a  OBs, or NULL..**
1f000 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76  .** An sqlite3_v
1f010 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20  alue object may 
1f020 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74 65  be either "prote
1f030 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74  cted" or "unprot
1f040 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20  ected"..** Some 
1f050 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69  interfaces requi
1f060 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20 73  re a protected s
1f070 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f  qlite3_value.  O
1f080 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a  ther interfaces.
1f090 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65  ** will accept e
1f0a0 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74 65  ither a protecte
1f0b0 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63  d or an unprotec
1f0c0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
1f0d0 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65  e..** Every inte
1f0e0 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65 70  rface that accep
1f0f0 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ts sqlite3_value
1f100 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69   arguments speci
1f110 66 69 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 20  fies.** whether 
1f120 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69 72  or not it requir
1f130 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20 73  es a protected s
1f140 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a  qlite3_value..**
1f150 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22 70  .** The terms "p
1f160 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22 75  rotected" and "u
1f170 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66 65  nprotected" refe
1f180 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72 20  r to whether or 
1f190 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69  not.** a mutex i
1f1a0 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 72  s held.  A inter
1f1b0 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c  nal mutex is hel
1f1c0 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74 65  d for a protecte
1f1d0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  d.** sqlite3_val
1f1e0 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f  ue object but no
1f1f0 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66   mutex is held f
1f200 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65  or an unprotecte
1f210 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c  d.** sqlite3_val
1f220 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 53  ue object.  If S
1f230 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
1f240 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74  d to be single-t
1f250 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 68  hreaded.** (with
1f260 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53   [SQLITE_THREADS
1f270 41 46 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20  AFE=0] and with 
1f280 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73  [sqlite3_threads
1f290 61 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67  afe()] returning
1f2a0 20 30 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c   0).** or if SQL
1f2b0 69 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e  ite is run in on
1f2c0 65 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74  e of reduced mut
1f2d0 65 78 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51  ex modes .** [SQ
1f2e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
1f2f0 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51  LETHREAD] or [SQ
1f300 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
1f310 49 54 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e  ITHREAD].** then
1f320 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 73   there is no dis
1f330 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e  tinction between
1f340 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75   protected and u
1f350 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71  nprotected.** sq
1f360 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
1f370 63 74 73 20 61 6e 64 20 74 68 65 79 20 63 61 6e  cts and they can
1f380 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63 68   be used interch
1f390 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 76  angeably.  Howev
1f3a0 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d  er,.** for maxim
1f3b0 75 6d 20 63 6f 64 65 20 70 6f 72 74 61 62 69 6c  um code portabil
1f3c0 69 74 79 20 69 74 20 69 73 20 72 65 63 6f 6d 6d  ity it is recomm
1f3d0 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69  ended that appli
1f3e0 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c  cations.** still
1f3f0 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e   make the distin
1f400 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 65  ction between be
1f410 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20  tween protected 
1f420 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a  and unprotected.
1f430 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
1f440 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68   objects even wh
1f450 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20  en not strictly 
1f460 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
1f470 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
1f480 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61  e objects that a
1f490 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61 72  re passed as par
1f4a0 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65  ameters into the
1f4b0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
1f4c0 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 69  on of [applicati
1f4d0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66  on-defined SQL f
1f4e0 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 72  unctions] are pr
1f4f0 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20  otected..** The 
1f500 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
1f510 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79  ject returned by
1f520 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  .** [sqlite3_col
1f530 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20  umn_value()] is 
1f540 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20  unprotected..** 
1f550 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  Unprotected sqli
1f560 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
1f570 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73  s may only be us
1f580 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69  ed with.** [sqli
1f590 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
1f5a0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
1f5b0 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a  _bind_value()]..
1f5c0 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
1f5d0 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c  value_blob | sql
1f5e0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
1f5f0 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20  )] family of.** 
1f600 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75 69  interfaces requi
1f610 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c  re protected sql
1f620 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
1f630 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ts..*/.typedef s
1f640 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65  truct Mem sqlite
1f650 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20  3_value;../*.** 
1f660 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 46 75  CAPI3REF: SQL Fu
1f670 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f  nction Context O
1f680 62 6a 65 63 74 20 7b 48 31 36 30 30 31 7d 20 3c  bject {H16001} <
1f690 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20200>.**.** Th
1f6a0 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69  e context in whi
1f6b0 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  ch an SQL functi
1f6c0 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20 73  on executes is s
1f6d0 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73  tored in an.** s
1f6e0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f  qlite3_context o
1f6f0 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65  bject.  A pointe
1f700 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f  r to an sqlite3_
1f710 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a  context object.*
1f720 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72 73  * is always firs
1f730 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  t parameter to [
1f740 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
1f750 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ned SQL function
1f760 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  s]..** The appli
1f770 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
1f780 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  QL function impl
1f790 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20  ementation will 
1f7a0 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69  pass this.** poi
1f7b0 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e 74  nter through int
1f7c0 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69  o calls to [sqli
1f7d0 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c  te3_result_int |
1f7e0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 28   sqlite3_result(
1f7f0 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  )],.** [sqlite3_
1f800 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
1f810 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75  t()], [sqlite3_u
1f820 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20  ser_data()],.** 
1f830 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  [sqlite3_context
1f840 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b  _db_handle()], [
1f850 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
1f860 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f  ata()],.** and/o
1f870 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  r [sqlite3_set_a
1f880 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79  uxdata()]..*/.ty
1f890 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
1f8a0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c  ite3_context sql
1f8b0 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f  ite3_context;../
1f8c0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 42  *.** CAPI3REF: B
1f8d0 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54 6f  inding Values To
1f8e0 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d   Prepared Statem
1f8f0 65 6e 74 73 20 7b 48 31 33 35 30 30 7d 20 3c 53  ents {H13500} <S
1f900 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  70300>.** KEYWOR
1f910 44 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65  DS: {host parame
1f920 74 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d  ter} {host param
1f930 65 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72  eters} {host par
1f940 61 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20  ameter name}.** 
1f950 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70  KEYWORDS: {SQL p
1f960 61 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70  arameter} {SQL p
1f970 61 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61  arameters} {para
1f980 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a  meter binding}.*
1f990 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c 20  *.** In the SQL 
1f9a0 73 74 72 69 6e 67 73 20 69 6e 70 75 74 20 74 6f  strings input to
1f9b0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
1f9c0 65 5f 76 32 28 29 5d 20 61 6e 64 20 69 74 73 20  e_v2()] and its 
1f9d0 76 61 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74  variants,.** lit
1f9e0 65 72 61 6c 73 20 6d 61 79 20 62 65 20 72 65 70  erals may be rep
1f9f0 6c 61 63 65 64 20 62 79 20 61 20 5b 70 61 72 61  laced by a [para
1fa00 6d 65 74 65 72 5d 20 69 6e 20 6f 6e 65 20 6f 66  meter] in one of
1fa10 20 74 68 65 73 65 20 66 6f 72 6d 73 3a 0a 2a 2a   these forms:.**
1fa20 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
1fa30 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e    ?.** <li>  ?NN
1fa40 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a  N.** <li>  :VVV.
1fa50 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a  ** <li>  @VVV.**
1fa60 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c   <li>  $VVV.** <
1fa70 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  /ul>.**.** In th
1fa80 65 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 6d  e parameter form
1fa90 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 4e 4e  s shown above NN
1faa0 4e 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  N is an integer 
1fab0 6c 69 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20  literal,.** and 
1fac0 56 56 56 20 69 73 20 61 6e 20 61 6c 70 68 61 2d  VVV is an alpha-
1fad0 6e 75 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65  numeric paramete
1fae0 72 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 6c 75  r name. The valu
1faf0 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70  es of these.** p
1fb00 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20  arameters (also 
1fb10 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72  called "host par
1fb20 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72  ameter names" or
1fb30 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73   "SQL parameters
1fb40 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74  ").** can be set
1fb50 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
1fb60 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74  e3_bind_*() rout
1fb70 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65 72  ines defined her
1fb80 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  e..**.** The fir
1fb90 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
1fba0 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  he sqlite3_bind_
1fbb0 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20  *() routines is 
1fbc0 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e  always.** a poin
1fbd0 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69  ter to the [sqli
1fbe0 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74  te3_stmt] object
1fbf0 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a   returned from.*
1fc00 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
1fc10 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20  re_v2()] or its 
1fc20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  variants..**.** 
1fc30 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
1fc40 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78  ent is the index
1fc50 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 72 61   of the SQL para
1fc60 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e  meter to be set.
1fc70 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74  .** The leftmost
1fc80 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68   SQL parameter h
1fc90 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31  as an index of 1
1fca0 2e 20 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65  .  When the same
1fcb0 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61   named.** SQL pa
1fcc0 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 20  rameter is used 
1fcd0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20  more than once, 
1fce0 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65  second and subse
1fcf0 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65  quent.** occurre
1fd00 6e 63 65 73 20 68 61 76 65 20 74 68 65 20 73 61  nces have the sa
1fd10 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68 65 20  me index as the 
1fd20 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65  first occurrence
1fd30 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66  ..** The index f
1fd40 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74  or named paramet
1fd50 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65  ers can be looke
1fd60 64 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a  d up using the.*
1fd70 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  * [sqlite3_bind_
1fd80 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28  parameter_index(
1fd90 29 5d 20 41 50 49 20 69 66 20 64 65 73 69 72 65  )] API if desire
1fda0 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a  d.  The index.**
1fdb0 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61   for "?NNN" para
1fdc0 6d 65 74 65 72 73 20 69 73 20 74 68 65 20 76 61  meters is the va
1fdd0 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54  lue of NNN..** T
1fde0 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73  he NNN value mus
1fdf0 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61  t be between 1 a
1fe00 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  nd the [sqlite3_
1fe10 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61  limit()].** para
1fe20 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49  meter [SQLITE_LI
1fe30 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
1fe40 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61  BER] (default va
1fe50 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a  lue: 999)..**.**
1fe60 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
1fe70 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75 65  ent is the value
1fe80 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20   to bind to the 
1fe90 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a  parameter..**.**
1fea0 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e   In those routin
1feb0 65 73 20 74 68 61 74 20 68 61 76 65 20 61 20 66  es that have a f
1fec0 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20  ourth argument, 
1fed0 69 74 73 20 76 61 6c 75 65 20 69 73 20 74 68 65  its value is the
1fee0 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  .** number of by
1fef0 74 65 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d  tes in the param
1ff00 65 74 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65  eter.  To be cle
1ff10 61 72 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73  ar: the value is
1ff20 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
1ff30 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69  f <u>bytes</u> i
1ff40 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74  n the value, not
1ff50 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
1ff60 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66  haracters..** If
1ff70 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   the fourth para
1ff80 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 76  meter is negativ
1ff90 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  e, the length of
1ffa0 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a   the string is.*
1ffb0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
1ffc0 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 65 20  bytes up to the 
1ffd0 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69  first zero termi
1ffe0 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  nator..**.** The
1fff0 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20   fifth argument 
20000 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  to sqlite3_bind_
20010 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f  blob(), sqlite3_
20020 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64  bind_text(), and
20030 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  .** sqlite3_bind
20040 5f 74 65 78 74 31 36 28 29 20 69 73 20 61 20 64  _text16() is a d
20050 65 73 74 72 75 63 74 6f 72 20 75 73 65 64 20 74  estructor used t
20060 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65  o dispose of the
20070 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69   BLOB or.** stri
20080 6e 67 20 61 66 74 65 72 20 53 51 4c 69 74 65 20  ng after SQLite 
20090 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74  has finished wit
200a0 68 20 69 74 2e 20 49 66 20 74 68 65 20 66 69 66  h it. If the fif
200b0 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a  th argument is.*
200c0 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 61  * the special va
200d0 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54  lue [SQLITE_STAT
200e0 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  IC], then SQLite
200f0 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
20100 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  e.** information
20110 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75   is in static, u
20120 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 61  nmanaged space a
20130 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  nd does not need
20140 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a   to be freed..**
20150 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72   If the fifth ar
20160 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76  gument has the v
20170 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41  alue [SQLITE_TRA
20180 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a  NSIENT], then.**
20190 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74   SQLite makes it
201a0 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f  s own private co
201b0 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 20 69  py of the data i
201c0 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f  mmediately, befo
201d0 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  re.** the sqlite
201e0 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69  3_bind_*() routi
201f0 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a  ne returns..**.*
20200 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69  * The sqlite3_bi
20210 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f  nd_zeroblob() ro
20220 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c  utine binds a BL
20230 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74  OB of length N t
20240 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64  hat.** is filled
20250 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41   with zeroes.  A
20260 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61   zeroblob uses a
20270 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66   fixed amount of
20280 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74   memory.** (just
20290 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68   an integer to h
202a0 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 20 77 68  old its size) wh
202b0 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67 20  ile it is being 
202c0 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65  processed..** Ze
202d0 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65  roblobs are inte
202e0 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20 61 73  nded to serve as
202f0 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f   placeholders fo
20300 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a  r BLOBs whose.**
20310 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65   content is late
20320 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a  r written using.
20330 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ** [sqlite3_blob
20340 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e  _open | incremen
20350 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f  tal BLOB I/O] ro
20360 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67  utines..** A neg
20370 61 74 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20  ative value for 
20380 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73  the zeroblob res
20390 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c  ults in a zero-l
203a0 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a  ength BLOB..**.*
203b0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69  * The sqlite3_bi
203c0 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  nd_*() routines 
203d0 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61  must be called a
203e0 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  fter.** [sqlite3
203f0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28  _prepare_v2()] (
20400 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
20410 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  ) or [sqlite3_re
20420 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65  set()] and.** be
20430 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74  fore [sqlite3_st
20440 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e  ep()]..** Bindin
20450 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72  gs are not clear
20460 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74  ed by the [sqlit
20470 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74  e3_reset()] rout
20480 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20  ine..** Unbound 
20490 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 69  parameters are i
204a0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 4e 55  nterpreted as NU
204b0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  LL..**.** These 
204c0 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
204d0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73  [SQLITE_OK] on s
204e0 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72  uccess or an err
204f0 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e  or code if.** an
20500 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ything goes wron
20510 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47  g.  [SQLITE_RANG
20520 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  E] is returned i
20530 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a  f the parameter.
20540 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20  ** index is out 
20550 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49  of range.  [SQLI
20560 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74  TE_NOMEM] is ret
20570 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28  urned if malloc(
20580 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c  ) fails..** [SQL
20590 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68  ITE_MISUSE] migh
205a0 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66  t be returned if
205b0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
205c0 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a  are called on a.
205d0 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ** virtual machi
205e0 6e 65 20 74 68 61 74 20 69 73 20 74 68 65 20 77  ne that is the w
205f0 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68  rong state or wh
20600 69 63 68 20 68 61 73 20 61 6c 72 65 61 64 79 20  ich has already 
20610 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a  been finalized..
20620 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20  ** Detection of 
20630 6d 69 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69  misuse is unreli
20640 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69  able.  Applicati
20650 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64  ons should not d
20660 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49  epend.** on SQLI
20670 54 45 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e  TE_MISUSE return
20680 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  s.  SQLITE_MISUS
20690 45 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  E is intended to
206a0 20 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61   indicate a.** a
206b0 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20   logic error in 
206c0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e  the application.
206d0 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e    Future version
206e0 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68  s of SQLite migh
206f0 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65  t.** panic rathe
20700 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51  r than return SQ
20710 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a  LITE_MISUSE..**.
20720 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
20730 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
20740 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a  eter_count()],.*
20750 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  * [sqlite3_bind_
20760 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29  parameter_name()
20770 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
20780 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
20790 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52  ndex()]..**.** R
207a0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
207b0 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 35 30 39  [H13506] [H13509
207c0 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 31 33 35  ] [H13512] [H135
207d0 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 5b 48 31  15] [H13518] [H1
207e0 33 35 32 31 5d 20 5b 48 31 33 35 32 34 5d 20 5b  3521] [H13524] [
207f0 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 31 33 35  H13527].** [H135
20800 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 5b 48 31  30] [H13533] [H1
20810 33 35 33 36 5d 20 5b 48 31 33 35 33 39 5d 20 5b  3536] [H13539] [
20820 48 31 33 35 34 32 5d 20 5b 48 31 33 35 34 35 5d  H13542] [H13545]
20830 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 33 35 35   [H13548] [H1355
20840 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  1].**.*/.SQLITE_
20850 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
20860 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65  bind_blob(sqlite
20870 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
20880 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e  nst void*, int n
20890 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
208a0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
208b0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  t sqlite3_bind_d
208c0 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 74  ouble(sqlite3_st
208d0 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65  mt*, int, double
208e0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
208f0 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  t sqlite3_bind_i
20900 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt(sqlite3_stmt*
20910 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
20920 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
20930 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
20940 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
20950 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  t, sqlite3_int64
20960 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
20970 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e  t sqlite3_bind_n
20980 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ull(sqlite3_stmt
20990 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
209a0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
209b0 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74 65  bind_text(sqlite
209c0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f  3_stmt*, int, co
209d0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e  nst char*, int n
209e0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
209f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
20a00 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74  t sqlite3_bind_t
20a10 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ext16(sqlite3_st
20a20 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  mt*, int, const 
20a30 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  void*, int, void
20a40 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
20a50 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
20a60 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73  te3_bind_value(s
20a70 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
20a80 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  t, const sqlite3
20a90 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
20aa0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
20ab0 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73  _bind_zeroblob(s
20ac0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
20ad0 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a  t, int n);../*.*
20ae0 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62  * CAPI3REF: Numb
20af0 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d 65  er Of SQL Parame
20b00 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c 53  ters {H13600} <S
20b10 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  70300>.**.** Thi
20b20 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
20b30 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74 68   used to find th
20b40 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c  e number of [SQL
20b50 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20   parameters].** 
20b60 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  in a [prepared s
20b70 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20  tatement].  SQL 
20b80 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74  parameters are t
20b90 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20  okens of the.** 
20ba0 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22  form "?", "?NNN"
20bb0 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41 22  , ":AAA", "$AAA"
20bc0 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61 74  , or "@AAA" that
20bd0 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61   serve as.** pla
20be0 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76 61  ceholders for va
20bf0 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b 73  lues that are [s
20c00 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
20c10 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20   | bound].** to 
20c20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61  the parameters a
20c30 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a  t a later time..
20c40 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
20c50 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74 75  ne actually retu
20c60 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66  rns the index of
20c70 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72 69   the largest (ri
20c80 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61  ghtmost).** para
20c90 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66  meter. For all f
20ca0 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e  orms except ?NNN
20cb0 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72 72  , this will corr
20cc0 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a  espond to the.**
20cd0 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71 75   number of uniqu
20ce0 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20 49  e parameters.  I
20cf0 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20  f parameters of 
20d00 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65  the ?NNN are use
20d10 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79 20  d,.** there may 
20d20 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20 6c  be gaps in the l
20d30 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ist..**.** See a
20d40 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69  lso: [sqlite3_bi
20d50 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f  nd_blob|sqlite3_
20d60 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  bind()],.** [sql
20d70 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
20d80 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64  ter_name()], and
20d90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
20da0 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
20db0 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  x()]..**.** Requ
20dc0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
20dd0 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3601].*/.SQLITE_
20de0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
20df0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
20e00 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  ount(sqlite3_stm
20e10 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
20e20 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20  3REF: Name Of A 
20e30 48 6f 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b  Host Parameter {
20e40 48 31 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e  H13620} <S70300>
20e50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
20e60 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ine returns a po
20e70 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d  inter to the nam
20e80 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a  e of the n-th.**
20e90 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d   [SQL parameter]
20ea0 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   in a [prepared 
20eb0 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53  statement]..** S
20ec0 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66  QL parameters of
20ed0 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22   the form "?NNN"
20ee0 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40   or ":AAA" or "@
20ef0 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a  AAA" or "$AAA".*
20f00 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68  * have a name wh
20f10 69 63 68 20 69 73 20 74 68 65 20 73 74 72 69 6e  ich is the strin
20f20 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41  g "?NNN" or ":AA
20f30 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20  A" or "@AAA" or 
20f40 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63  "$AAA".** respec
20f50 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74  tively..** In ot
20f60 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69  her words, the i
20f70 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24  nitial ":" or "$
20f80 22 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a  " or "@" or "?".
20f90 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61  ** is included a
20fa0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61  s part of the na
20fb0 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  me..** Parameter
20fc0 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f  s of the form "?
20fd0 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c  " without a foll
20fe0 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61  owing integer ha
20ff0 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e  ve no name.** an
21000 64 20 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72  d are also refer
21010 72 65 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79  red to as "anony
21020 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 72 73 22  mous parameters"
21030 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
21040 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  t host parameter
21050 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
21060 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a   1, not 0..**.**
21070 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20   If the value n 
21080 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  is out of range 
21090 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70  or if the n-th p
210a0 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e  arameter is.** n
210b0 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55  ameless, then NU
210c0 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  LL is returned. 
210d0 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   The returned st
210e0 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79  ring is.** alway
210f0 73 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64  s in UTF-8 encod
21100 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65 20  ing even if the 
21110 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20  named parameter 
21120 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c  was.** originall
21130 79 20 73 70 65 63 69 66 69 65 64 20 61 73 20 55  y specified as U
21140 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65  TF-16 in [sqlite
21150 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f  3_prepare16()] o
21160 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  r.** [sqlite3_pr
21170 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a  epare16_v2()]..*
21180 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
21190 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
211a0 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29  b|sqlite3_bind()
211b0 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  ],.** [sqlite3_b
211c0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
211d0 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b  unt()], and.** [
211e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
211f0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e  ameter_index()].
21200 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
21210 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 32 31 5d  nts:.** [H13621]
21220 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
21230 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
21240 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
21250 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  r_name(sqlite3_s
21260 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a  tmt*, int);../*.
21270 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e 64  ** CAPI3REF: Ind
21280 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65 74 65  ex Of A Paramete
21290 72 20 57 69 74 68 20 41 20 47 69 76 65 6e 20 4e  r With A Given N
212a0 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 3c 53 37  ame {H13640} <S7
212b0 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  0300>.**.** Retu
212c0 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
212d0 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  an SQL parameter
212e0 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e   given its name.
212f0 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 78 20 76    The.** index v
21300 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
21310 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73   suitable for us
21320 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 0a  e as the second.
21330 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  ** parameter to 
21340 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
21350 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28  ob|sqlite3_bind(
21360 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a 2a 20 69  )].  A zero.** i
21370 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 6e 6f  s returned if no
21380 20 6d 61 74 63 68 69 6e 67 20 70 61 72 61 6d 65   matching parame
21390 74 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 20 54  ter is found.  T
213a0 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  he parameter.** 
213b0 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 69 76  name must be giv
213c0 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 65 6e  en in UTF-8 even
213d0 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   if the original
213e0 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 61   statement.** wa
213f0 73 20 70 72 65 70 61 72 65 64 20 66 72 6f 6d 20  s prepared from 
21400 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 69 6e  UTF-16 text usin
21410 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  g [sqlite3_prepa
21420 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a  re16_v2()]..**.*
21430 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
21440 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73  ite3_bind_blob|s
21450 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a  qlite3_bind()],.
21460 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
21470 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
21480 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  ()], and.** [sql
21490 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
214a0 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a  ter_index()]..**
214b0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
214c0 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d 0a 2a 2f  :.** [H13641].*/
214d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
214e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
214f0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c  ameter_index(sql
21500 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73  ite3_stmt*, cons
21510 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a  t char *zName);.
21520 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
21530 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64 69   Reset All Bindi
21540 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72 65  ngs On A Prepare
21550 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33  d Statement {H13
21560 36 36 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a  660} <S70300>.**
21570 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 74 6f 20  .** Contrary to 
21580 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 6f 66  the intuition of
21590 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 33 5f   many, [sqlite3_
215a0 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20 6e 6f  reset()] does no
215b0 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 20 5b  t reset.** the [
215c0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
215d0 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 6e  b | bindings] on
215e0 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
215f0 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 73 65 20  tement]..** Use 
21600 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
21610 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20 70  reset all host p
21620 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c  arameters to NUL
21630 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  L..**.** Require
21640 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 36  ments:.** [H1366
21650 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  1].*/.SQLITE_API
21660 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65   int sqlite3_cle
21670 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69  ar_bindings(sqli
21680 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a  te3_stmt*);../*.
21690 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d  ** CAPI3REF: Num
216a0 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49  ber Of Columns I
216b0 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20 7b  n A Result Set {
216c0 48 31 33 37 31 30 7d 20 3c 53 31 30 37 30 30 3e  H13710} <S10700>
216d0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
216e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
216f0 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  mns in the resul
21700 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 20 62  t set returned b
21710 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72  y the.** [prepar
21720 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 54  ed statement]. T
21730 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
21740 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 20 69  rns 0 if pStmt i
21750 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 74  s an SQL.** stat
21760 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73 20  ement that does 
21770 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61 20  not return data 
21780 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e 20  (for example an 
21790 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a  [UPDATE])..**.**
217a0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
217b0 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f 0a 53 51  * [H13711].*/.SQ
217c0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
217d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
217e0 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
217f0 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
21800 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20  API3REF: Column 
21810 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c  Names In A Resul
21820 74 20 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c  t Set {H13720} <
21830 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10700>.**.** Th
21840 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74  ese routines ret
21850 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73  urn the name ass
21860 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69  igned to a parti
21870 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  cular column.** 
21880 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
21890 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20  t of a [SELECT] 
218a0 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
218b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
218c0 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61  ame().** interfa
218d0 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
218e0 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74  nter to a zero-t
218f0 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
21900 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71  string.** and sq
21910 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
21920 65 31 36 28 29 20 72 65 74 75 72 6e 73 20 61 20  e16() returns a 
21930 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72  pointer to a zer
21940 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20  o-terminated.** 
21950 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20  UTF-16 string.  
21960 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
21970 74 65 72 20 69 73 20 74 68 65 20 5b 70 72 65 70  ter is the [prep
21980 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a  ared statement].
21990 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ** that implemen
219a0 74 73 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20  ts the [SELECT] 
219b0 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73  statement. The s
219c0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
219d0 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e  is the.** column
219e0 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65   number.  The le
219f0 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73  ftmost column is
21a00 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a   number 0..**.**
21a10 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   The returned st
21a20 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20  ring pointer is 
21a30 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68  valid until eith
21a40 65 72 20 74 68 65 20 5b 70 72 65 70 61 72 65 64  er the [prepared
21a50 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69   statement].** i
21a60 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b  s destroyed by [
21a70 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
21a80 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65  ()] or until the
21a90 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a   next call to.**
21aa0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
21ab0 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65  name() or sqlite
21ac0 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28  3_column_name16(
21ad0 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f  ) on the same co
21ae0 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  lumn..**.** If s
21af0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
21b00 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65  fails during the
21b10 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65   processing of e
21b20 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a  ither routine.**
21b30 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75   (for example du
21b40 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f  ring a conversio
21b50 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20  n from UTF-8 to 
21b60 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a  UTF-16) then a.*
21b70 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69  * NULL pointer i
21b80 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
21b90 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  * The name of a 
21ba0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73  result column is
21bb0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
21bc0 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f  e "AS" clause fo
21bd0 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e  r.** that column
21be0 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  , if there is an
21bf0 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20   AS clause.  If 
21c00 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63  there is no AS c
21c10 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68  lause.** then th
21c20 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f  e name of the co
21c30 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66  lumn is unspecif
21c40 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e  ied and may chan
21c50 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72  ge from.** one r
21c60 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65  elease of SQLite
21c70 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a   to the next..**
21c80 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
21c90 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d 20 5b 48  :.** [H13721] [H
21ca0 31 33 37 32 33 5d 20 5b 48 31 33 37 32 34 5d 20  13723] [H13724] 
21cb0 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 37 32 36  [H13725] [H13726
21cc0 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 0a 53 51  ] [H13727].*/.SQ
21cd0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
21ce0 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
21cf0 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  umn_name(sqlite3
21d00 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a  _stmt*, int N);.
21d10 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
21d20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
21d30 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c  olumn_name16(sql
21d40 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
21d50 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  N);../*.** CAPI3
21d60 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20 44  REF: Source Of D
21d70 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20 52  ata In A Query R
21d80 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20 3c  esult {H13740} <
21d90 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10700>.**.** Th
21da0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72 6f  ese routines pro
21db0 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f 20  vide a means to 
21dc0 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 63  determine what c
21dd0 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a 2a  olumn of what.**
21de0 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68 20   table in which 
21df0 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75 6c  database a resul
21e00 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20  t of a [SELECT] 
21e10 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20  statement comes 
21e20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d  from..** The nam
21e30 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
21e40 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63 6f  e or table or co
21e50 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74 75  lumn can be retu
21e60 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68 65  rned as.** eithe
21e70 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54 46  r a UTF-8 or UTF
21e80 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65  -16 string.  The
21e90 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75 74   _database_ rout
21ea0 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20 74  ines return.** t
21eb0 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
21ec0 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72 6f  , the _table_ ro
21ed0 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68  utines return th
21ee0 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61 6e  e table name, an
21ef0 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 5f  d.** the origin_
21f00 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
21f10 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
21f20 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  ..** The returne
21f30 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c 69  d string is vali
21f40 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72 65  d until the [pre
21f50 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
21f60 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a   is destroyed.**
21f70 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
21f80 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75  finalize()] or u
21f90 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69 6e  ntil the same in
21fa0 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71  formation is req
21fb0 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e 20  uested.** again 
21fc0 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 65  in a different e
21fd0 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  ncoding..**.** T
21fe0 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e 65  he names returne
21ff0 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69 6e  d are the origin
22000 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e 61  al un-aliased na
22010 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  mes of the.** da
22020 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20 61  tabase, table, a
22030 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a  nd column..**.**
22040 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
22050 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  ent to the follo
22060 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61 20  wing calls is a 
22070 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
22080 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 66  ent]..** These f
22090 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
220a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
220b0 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  t the Nth column
220c0 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20   returned by.** 
220d0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 77  the statement, w
220e0 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65  here N is the se
220f0 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72  cond function ar
22100 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  gument..**.** If
22110 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
22120 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
22130 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e 20  statement is an 
22140 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a 2a  expression or.**
22150 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69 73   subquery and is
22160 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61   not a column va
22170 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66  lue, then all of
22180 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
22190 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e   return.** NULL.
221a0 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20    These routine 
221b0 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 72  might also retur
221c0 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f  n NULL if a memo
221d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
221e0 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20  ror.** occurs.  
221f0 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79 20  Otherwise, they 
22200 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  return the name 
22210 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64 20  of the attached 
22220 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 0a  database, table.
22230 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68  ** and column th
22240 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74 20  at query result 
22250 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72 61  column was extra
22260 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a  cted from..**.**
22270 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74 68   As with all oth
22280 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c 20  er SQLite APIs, 
22290 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64 20  those postfixed 
222a0 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72 6e  with "16" return
222b0 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  .** UTF-16 encod
222c0 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65 20  ed strings, the 
222d0 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  other functions 
222e0 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b 45  return UTF-8. {E
222f0 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ND}.**.** These 
22300 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61 76  APIs are only av
22310 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c  ailable if the l
22320 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69  ibrary was compi
22330 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20  led with the.** 
22340 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43  [SQLITE_ENABLE_C
22350 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20  OLUMN_METADATA] 
22360 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73  C-preprocessor s
22370 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a  ymbol defined..*
22380 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a 2a  *.** {A13751}.**
22390 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   If two or more 
223a0 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65  threads call one
223b0 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 73   or more of thes
223c0 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69 6e  e routines again
223d0 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70  st the same.** p
223e0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
223f0 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 20  t and column at 
22400 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68  the same time th
22410 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  en the results a
22420 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e  re.** undefined.
22430 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
22440 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 34 31 5d  nts:.** [H13741]
22450 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 33 37 34   [H13742] [H1374
22460 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 48 31 33  3] [H13744] [H13
22470 37 34 35 5d 20 5b 48 31 33 37 34 36 5d 20 5b 48  745] [H13746] [H
22480 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 66 20  13748].**.** If 
22490 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 65  two or more thre
224a0 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20  ads call one or 
224b0 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  more.** [sqlite3
224c0 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
224d0 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 6d  _name | column m
224e0 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61 63  etadata interfac
224f0 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73  es].** for the s
22500 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  ame [prepared st
22510 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 65 73  atement] and res
22520 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 74  ult column.** at
22530 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74   the same time t
22540 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20  hen the results 
22550 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a  are undefined..*
22560 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
22570 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
22580 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
22590 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
225a0 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  mt*,int);.SQLITE
225b0 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
225c0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
225d0 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28  database_name16(
225e0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
225f0 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  t);.SQLITE_API c
22600 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
22610 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
22620 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
22630 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
22640 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
22650 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
22660 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69  able_name16(sqli
22670 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
22680 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
22690 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
226a0 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
226b0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
226c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
226d0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
226e0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
226f0 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  in_name16(sqlite
22700 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f  3_stmt*,int);../
22710 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
22720 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70 65  eclared Datatype
22730 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73 75   Of A Query Resu
22740 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c 53 31 30  lt {H13760} <S10
22750 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  700>.**.** The f
22760 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69  irst parameter i
22770 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  s a [prepared st
22780 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20  atement]..** If 
22790 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69  this statement i
227a0 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61  s a [SELECT] sta
227b0 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 4e  tement and the N
227c0 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
227d0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65 73  .** returned res
227e0 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20  ult set of that 
227f0 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 74 61  [SELECT] is a ta
22800 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20  ble column (not 
22810 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  an.** expression
22820 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74 68   or subquery) th
22830 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64 20  en the declared 
22840 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62 6c  type of the tabl
22850 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72  e.** column is r
22860 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65  eturned.  If the
22870 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   Nth column of t
22880 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 73  he result set is
22890 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f   an.** expressio
228a0 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20 74  n or subquery, t
228b0 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  hen a NULL point
228c0 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
228d0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
228e0 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73  string is always
228f0 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 20   UTF-8 encoded. 
22900 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  {END}.**.** For 
22910 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74  example, given t
22920 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
22930 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45  ma:.**.** CREATE
22940 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41 52   TABLE t1(c1 VAR
22950 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64  IANT);.**.** and
22960 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
22970 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63  tatement to be c
22980 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53  ompiled:.**.** S
22990 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63 31  ELECT c1 + 1, c1
229a0 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20   FROM t1;.**.** 
229b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 75  this routine wou
229c0 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 74  ld return the st
229d0 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20 66  ring "VARIANT" f
229e0 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 72 65  or the second re
229f0 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 28  sult.** column (
22a00 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c  i==1), and a NUL
22a10 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68  L pointer for th
22a20 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 63  e first result c
22a30 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 0a 2a 2a  olumn (i==0)..**
22a40 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20  .** SQLite uses 
22a50 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69 6d 65  dynamic run-time
22a60 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a 75 73   typing.  So jus
22a70 74 20 62 65 63 61 75 73 65 20 61 20 63 6f 6c 75  t because a colu
22a80 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61 72 65  mn.** is declare
22a90 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 70  d to contain a p
22aa0 61 72 74 69 63 75 6c 61 72 20 74 79 70 65 20 64  articular type d
22ab0 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61  oes not mean tha
22ac0 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 73 74  t the.** data st
22ad0 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63 6f 6c  ored in that col
22ae0 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20 64 65  umn is of the de
22af0 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20 53 51  clared type.  SQ
22b00 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72 6f 6e  Lite is.** stron
22b10 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74 20 74  gly typed, but t
22b20 68 65 20 74 79 70 69 6e 67 20 69 73 20 64 79 6e  he typing is dyn
22b30 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69 63 2e  amic not static.
22b40 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61 73 73    Type.** is ass
22b50 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 6e 64  ociated with ind
22b60 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73 2c 20  ividual values, 
22b70 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63 6f 6e  not with the con
22b80 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65 64 20  tainers.** used 
22b90 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20 76 61  to hold those va
22ba0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  lues..**.** Requ
22bb0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
22bc0 33 37 36 31 5d 20 5b 48 31 33 37 36 32 5d 20 5b  3761] [H13762] [
22bd0 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54  H13763].*/.SQLIT
22be0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
22bf0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
22c00 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65  _decltype(sqlite
22c10 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51  3_stmt*,int);.SQ
22c20 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
22c30 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
22c40 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73  umn_decltype16(s
22c50 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
22c60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
22c70 45 46 3a 20 45 76 61 6c 75 61 74 65 20 41 6e 20  EF: Evaluate An 
22c80 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48  SQL Statement {H
22c90 31 33 32 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a  13200} <S10000>.
22ca0 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 5b 70  **.** After a [p
22cb0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
22cc0 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 65 70  t] has been prep
22cd0 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 68 65  ared using eithe
22ce0 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  r.** [sqlite3_pr
22cf0 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b  epare_v2()] or [
22d00 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
22d10 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f  6_v2()] or one o
22d20 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20  f the legacy.** 
22d30 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69  interfaces [sqli
22d40 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f  te3_prepare()] o
22d50 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
22d60 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 66 75  re16()], this fu
22d70 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62  nction.** must b
22d80 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20  e called one or 
22d90 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 65 76  more times to ev
22da0 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 74 65  aluate the state
22db0 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
22dc0 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 62  details of the b
22dd0 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 73  ehavior of the s
22de0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e  qlite3_step() in
22df0 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a 2a  terface depend.*
22e00 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68 65  * on whether the
22e10 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70   statement was p
22e20 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74 68  repared using th
22e30 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e 74  e newer "v2" int
22e40 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 74  erface.** [sqlit
22e50 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
22e60 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72   and [sqlite3_pr
22e70 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f 72  epare16_v2()] or
22e80 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61 63   the older legac
22e90 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 5b  y.** interface [
22ea0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
22eb0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
22ec0 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20 54  prepare16()].  T
22ed0 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a  he use of the.**
22ee0 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72 66   new "v2" interf
22ef0 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64  ace is recommend
22f00 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c 69  ed for new appli
22f10 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65 20  cations but the 
22f20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66  legacy.** interf
22f30 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75  ace will continu
22f40 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 65  e to be supporte
22f50 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  d..**.** In the 
22f60 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65  legacy interface
22f70 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
22f80 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65  ue will be eithe
22f90 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c  r [SQLITE_BUSY],
22fa0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45  .** [SQLITE_DONE
22fb0 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c  ], [SQLITE_ROW],
22fc0 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c   [SQLITE_ERROR],
22fd0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55   or [SQLITE_MISU
22fe0 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65  SE]..** With the
22ff0 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 2c   "v2" interface,
23000 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68 65   any of the othe
23010 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 73 5d  r [result codes]
23020 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64   or.** [extended
23030 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 6d   result codes] m
23040 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64  ight be returned
23050 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20   as well..**.** 
23060 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6d 65  [SQLITE_BUSY] me
23070 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 61 74  ans that the dat
23080 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77 61 73  abase engine was
23090 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71 75 69   unable to acqui
230a0 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  re the.** databa
230b0 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65 65 64  se locks it need
230c0 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62 2e  s to do its job.
230d0 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65    If the stateme
230e0 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49 54 5d  nt is a [COMMIT]
230f0 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 6f 75  .** or occurs ou
23100 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 70 6c  tside of an expl
23110 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  icit transaction
23120 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e 20 72  , then you can r
23130 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74 61 74  etry the.** stat
23140 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 73  ement.  If the s
23150 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20  tatement is not 
23160 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64 20 6f  a [COMMIT] and o
23170 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 0a 2a  ccurs within a.*
23180 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73  * explicit trans
23190 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f 75 20  action then you 
231a0 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 6b 20  should rollback 
231b0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
231c0 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e  before.** contin
231d0 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c  uing..**.** [SQL
231e0 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 20  ITE_DONE] means 
231f0 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65  that the stateme
23200 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  nt has finished 
23210 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73 75 63  executing.** suc
23220 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71 6c 69  cessfully.  sqli
23230 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c  te3_step() shoul
23240 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  d not be called 
23250 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20 76 69  again on this vi
23260 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
23270 20 77 69 74 68 6f 75 74 20 66 69 72 73 74 20 63   without first c
23280 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
23290 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65 73 65  reset()] to rese
232a0 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  t the virtual.**
232b0 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20 74 6f   machine back to
232c0 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 74 61   its initial sta
232d0 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  te..**.** If the
232e0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62   SQL statement b
232f0 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 72 65  eing executed re
23300 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61 2c 20  turns any data, 
23310 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57  then [SQLITE_ROW
23320 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  ].** is returned
23330 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e 65 77   each time a new
23340 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20   row of data is 
23350 72 65 61 64 79 20 66 6f 72 20 70 72 6f 63 65 73  ready for proces
23360 73 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20 63  sing by the.** c
23370 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75 65  aller. The value
23380 73 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65  s may be accesse
23390 64 20 75 73 69 6e 67 20 74 68 65 20 5b 63 6f 6c  d using the [col
233a0 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74  umn access funct
233b0 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74 65  ions]..** sqlite
233c0 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c 6c  3_step() is call
233d0 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 74 72  ed again to retr
233e0 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 72 6f  ieve the next ro
233f0 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a  w of data..**.**
23400 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
23410 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72 75 6e  means that a run
23420 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73 75 63  -time error (suc
23430 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61 69 6e  h as a constrain
23440 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 29 20  t.** violation) 
23450 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 73  has occurred.  s
23460 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 68  qlite3_step() sh
23470 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c  ould not be call
23480 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a 20 74  ed again on.** t
23490 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e 66 6f  he VM. More info
234a0 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 66  rmation may be f
234b0 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e 67 20  ound by calling 
234c0 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  [sqlite3_errmsg(
234d0 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 20  )]..** With the 
234e0 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 65  legacy interface
234f0 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69 66 69  , a more specifi
23500 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28 66 6f  c error code (fo
23510 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 5b 53  r example,.** [S
23520 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d  QLITE_INTERRUPT]
23530 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  , [SQLITE_SCHEMA
23540 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 52 52 55  ], [SQLITE_CORRU
23550 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74  PT], and so fort
23560 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 62 74  h).** can be obt
23570 61 69 6e 65 64 20 62 79 20 63 61 6c 6c 69 6e 67  ained by calling
23580 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
23590 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b 70 72  )] on the.** [pr
235a0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
235b0 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 32 22 20  ].  In the "v2" 
235c0 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 74 68  interface,.** th
235d0 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20  e more specific 
235e0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
235f0 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 20  turned directly 
23600 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  by sqlite3_step(
23610 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45  )..**.** [SQLITE
23620 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e 73 20 74  _MISUSE] means t
23630 68 61 74 20 74 68 65 20 74 68 69 73 20 72 6f 75  hat the this rou
23640 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 20  tine was called 
23650 69 6e 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  inappropriately.
23660 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 74 20 77  .** Perhaps it w
23670 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b  as called on a [
23680 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
23690 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a 2a 20  nt] that has.** 
236a0 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b 73 71  already been [sq
236b0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c  lite3_finalize |
236c0 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72 20 6f   finalized] or o
236d0 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64 0a 2a  n one that had.*
236e0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 74  * previously ret
236f0 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 45 52  urned [SQLITE_ER
23700 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ROR] or [SQLITE_
23710 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20 63 6f  DONE].  Or it co
23720 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 20 63 61  uld.** be the ca
23730 73 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65  se that the same
23740 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
23750 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 75 73  tion is being us
23760 65 64 20 62 79 20 74 77 6f 20 6f 72 0a 2a 2a 20  ed by two or.** 
23770 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61 74 20  more threads at 
23780 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 6e 74 20  the same moment 
23790 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c  in time..**.** <
237a0 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 61 63  b>Goofy Interfac
237b0 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e 20  e Alert:</b> In 
237c0 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72  the legacy inter
237d0 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 74 65  face, the sqlite
237e0 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 20  3_step().** API 
237f0 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61  always returns a
23800 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20 63   generic error c
23810 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52  ode, [SQLITE_ERR
23820 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  OR], following a
23830 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 68 65  ny.** error othe
23840 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 42  r than [SQLITE_B
23850 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45  USY] and [SQLITE
23860 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 20 6d  _MISUSE].  You m
23870 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71 6c  ust call.** [sql
23880 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72  ite3_reset()] or
23890 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
238a0 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 20 74  ze()] in order t
238b0 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 74 68  o find one of th
238c0 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 5b 65  e.** specific [e
238d0 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 61 74  rror codes] that
238e0 20 62 65 74 74 65 72 20 64 65 73 63 72 69 62 65   better describe
238f0 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 20  s the error..** 
23900 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 74 68  We admit that th
23910 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 64 65  is is a goofy de
23920 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f 62 6c  sign.  The probl
23930 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 78 65  em has been fixe
23940 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 22 76  d.** with the "v
23950 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49  2" interface.  I
23960 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 61 6c  f you prepare al
23970 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 73 74  l of your SQL st
23980 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 69 6e  atements.** usin
23990 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74 65  g either [sqlite
239a0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
239b0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  or [sqlite3_prep
239c0 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e 73 74  are16_v2()] inst
239d0 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 65  ead.** of the le
239e0 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70 72  gacy [sqlite3_pr
239f0 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73 71  epare()] and [sq
23a00 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28  lite3_prepare16(
23a10 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0a 2a  )] interfaces,.*
23a20 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65 20  * then the more 
23a30 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20  specific [error 
23a40 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75 72  codes] are retur
23a50 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20  ned directly.** 
23a60 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  by sqlite3_step(
23a70 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 74  ).  The use of t
23a80 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63  he "v2" interfac
23a90 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  e is recommended
23aa0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
23ab0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 32 30 32  ents:.** [H13202
23ac0 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 31 35 33  ] [H15304] [H153
23ad0 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 5b 48 31  06] [H15308] [H1
23ae0 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  5310].*/.SQLITE_
23af0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
23b00 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d  step(sqlite3_stm
23b10 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
23b20 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20  3REF: Number of 
23b30 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65 73  columns in a res
23b40 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30 7d  ult set {H13770}
23b50 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
23b60 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  Returns the numb
23b70 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 20  er of values in 
23b80 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
23b90 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
23ba0 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  t..**.** Require
23bb0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 37  ments:.** [H1377
23bc0 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 2f 0a 53  1] [H13772].*/.S
23bd0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
23be0 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
23bf0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
23c00 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
23c10 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d 65 6e  PI3REF: Fundamen
23c20 74 61 6c 20 44 61 74 61 74 79 70 65 73 20 7b 48  tal Datatypes {H
23c30 31 30 32 36 35 7d 20 3c 53 31 30 31 31 30 3e 3c  10265} <S10110><
23c40 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S10120>.** KEYWO
23c50 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 58 54  RDS: SQLITE_TEXT
23c60 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 36 7d 20  .**.** {H10266} 
23c70 45 76 65 72 79 20 76 61 6c 75 65 20 69 6e 20 53  Every value in S
23c80 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 20 6f 66  QLite has one of
23c90 20 66 69 76 65 20 66 75 6e 64 61 6d 65 6e 74 61   five fundamenta
23ca0 6c 20 64 61 74 61 74 79 70 65 73 3a 0a 2a 2a 0a  l datatypes:.**.
23cb0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
23cc0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
23cd0 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e 20 36 34  teger.** <li> 64
23ce0 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 61 74 69  -bit IEEE floati
23cf0 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 0a  ng point number.
23d00 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e 67 0a 2a  ** <li> string.*
23d10 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a 2a 20 3c  * <li> BLOB.** <
23d20 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c  li> NULL.** </ul
23d30 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  > {END}.**.** Th
23d40 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  ese constants ar
23d50 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61 63 68  e codes for each
23d60 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65 73 2e   of those types.
23d70 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
23d80 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 58 54   the SQLITE_TEXT
23d90 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20 61 6c   constant was al
23da0 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  so used in SQLit
23db0 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a 20 66  e version 2.** f
23dc0 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20  or a completely 
23dd0 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e 69 6e  different meanin
23de0 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68 61  g.  Software tha
23df0 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20  t links against 
23e00 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65 20 76  both.** SQLite v
23e10 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 51 4c  ersion 2 and SQL
23e20 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 73 68  ite version 3 sh
23e30 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 45 33  ould use SQLITE3
23e40 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a 20 53 51  _TEXT, not.** SQ
23e50 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23 64  LITE_TEXT..*/.#d
23e60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
23e70 45 47 45 52 20 20 31 0a 23 64 65 66 69 6e 65 20  EGER  1.#define 
23e80 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20 20 20  SQLITE_FLOAT    
23e90 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
23ea0 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 64 65 66  _BLOB     4.#def
23eb0 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ine SQLITE_NULL 
23ec0 20 20 20 20 35 0a 23 69 66 64 65 66 20 53 51 4c      5.#ifdef SQL
23ed0 49 54 45 5f 54 45 58 54 0a 23 20 75 6e 64 65 66  ITE_TEXT.# undef
23ee0 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 65 6c   SQLITE_TEXT.#el
23ef0 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
23f00 54 45 5f 54 45 58 54 20 20 20 20 20 33 0a 23 65  TE_TEXT     3.#e
23f10 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53 51 4c  ndif.#define SQL
23f20 49 54 45 33 5f 54 45 58 54 20 20 20 20 20 33 0a  ITE3_TEXT     3.
23f30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
23f40 20 52 65 73 75 6c 74 20 56 61 6c 75 65 73 20 46   Result Values F
23f50 72 6f 6d 20 41 20 51 75 65 72 79 20 7b 48 31 33  rom A Query {H13
23f60 38 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a  800} <S10700>.**
23f70 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6c 75   KEYWORDS: {colu
23f80 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69  mn access functi
23f90 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ons}.**.** These
23fa0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d 20 74   routines form t
23fb0 68 65 20 22 72 65 73 75 6c 74 20 73 65 74 20 71  he "result set q
23fc0 75 65 72 79 22 20 69 6e 74 65 72 66 61 63 65 2e  uery" interface.
23fd0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
23fe0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e 66  tines return inf
23ff0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
24000 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f   single column o
24010 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a  f the current.**
24020 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20 61   result row of a
24030 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76 65 72   query.  In ever
24040 79 20 63 61 73 65 20 74 68 65 20 66 69 72 73 74  y case the first
24050 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
24060 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ointer.** to the
24070 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
24080 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62 65  ment] that is be
24090 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28 74  ing evaluated (t
240a0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  he [sqlite3_stmt
240b0 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 72  *].** that was r
240c0 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71  eturned from [sq
240d0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
240e0 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74  ()] or one of it
240f0 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a 20 61  s variants).** a
24100 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  nd the second ar
24110 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e  gument is the in
24120 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  dex of the colum
24130 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f  n for which info
24140 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c  rmation.** shoul
24150 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 20  d be returned.  
24160 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c  The leftmost col
24170 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c  umn of the resul
24180 74 20 73 65 74 20 68 61 73 20 74 68 65 20 69 6e  t set has the in
24190 64 65 78 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  dex 0..**.** If 
241a0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
241b0 74 20 64 6f 65 73 20 6e 6f 74 20 63 75 72 72 65  t does not curre
241c0 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 20 61 20  ntly point to a 
241d0 76 61 6c 69 64 20 72 6f 77 2c 20 6f 72 20 69 66  valid row, or if
241e0 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69   the.** column i
241f0 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20 72  ndex is out of r
24200 61 6e 67 65 2c 20 74 68 65 20 72 65 73 75 6c 74  ange, the result
24210 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
24220 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
24230 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c   may only be cal
24240 6c 65 64 20 77 68 65 6e 20 74 68 65 20 6d 6f 73  led when the mos
24250 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f  t recent call to
24260 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  .** [sqlite3_ste
24270 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65  p()] has returne
24280 64 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61  d [SQLITE_ROW] a
24290 6e 64 20 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73  nd neither.** [s
242a0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
242b0 6e 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  nor [sqlite3_fin
242c0 61 6c 69 7a 65 28 29 5d 20 68 61 76 65 20 62 65  alize()] have be
242d0 65 6e 20 63 61 6c 6c 65 64 20 73 75 62 73 65 71  en called subseq
242e0 75 65 6e 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e  uently..** If an
242f0 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  y of these routi
24300 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 61  nes are called a
24310 66 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  fter [sqlite3_re
24320 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71  set()] or.** [sq
24330 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
24340 5d 20 6f 72 20 61 66 74 65 72 20 5b 73 71 6c 69  ] or after [sqli
24350 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20  te3_step()] has 
24360 72 65 74 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65  returned.** some
24370 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
24380 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74   [SQLITE_ROW], t
24390 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75  he results are u
243a0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20  ndefined..** If 
243b0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
243c0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73   or [sqlite3_res
243d0 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  et()] or [sqlite
243e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a  3_finalize()].**
243f0 20 61 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d   are called from
24400 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72   a different thr
24410 65 61 64 20 77 68 69 6c 65 20 61 6e 79 20 6f 66  ead while any of
24420 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a   these routines.
24430 2a 2a 20 61 72 65 20 70 65 6e 64 69 6e 67 2c 20  ** are pending, 
24440 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  then the results
24450 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a   are undefined..
24460 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
24470 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20  3_column_type() 
24480 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
24490 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49  the.** [SQLITE_I
244a0 4e 54 45 47 45 52 20 7c 20 64 61 74 61 74 79 70  NTEGER | datatyp
244b0 65 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20  e code] for the 
244c0 69 6e 69 74 69 61 6c 20 64 61 74 61 20 74 79 70  initial data typ
244d0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75  e.** of the resu
244e0 6c 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20  lt column.  The 
244f0 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
24500 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45  s one of [SQLITE
24510 5f 49 4e 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53  _INTEGER],.** [S
24520 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53  QLITE_FLOAT], [S
24530 51 4c 49 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51  QLITE_TEXT], [SQ
24540 4c 49 54 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b  LITE_BLOB], or [
24550 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54  SQLITE_NULL].  T
24560 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75  he value.** retu
24570 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
24580 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73  column_type() is
24590 20 6f 6e 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c   only meaningful
245a0 20 69 66 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63   if no type.** c
245b0 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20  onversions have 
245c0 6f 63 63 75 72 72 65 64 20 61 73 20 64 65 73 63  occurred as desc
245d0 72 69 62 65 64 20 62 65 6c 6f 77 2e 20 20 41 66  ribed below.  Af
245e0 74 65 72 20 61 20 74 79 70 65 20 63 6f 6e 76 65  ter a type conve
245f0 72 73 69 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61  rsion,.** the va
24600 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
24610 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
24620 79 70 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e  ype() is undefin
24630 65 64 2e 20 20 46 75 74 75 72 65 0a 2a 2a 20 76  ed.  Future.** v
24640 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
24650 65 20 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65  e may change the
24660 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c   behavior of sql
24670 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
24680 28 29 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  ().** following 
24690 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f  a type conversio
246a0 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
246b0 72 65 73 75 6c 74 20 69 73 20 61 20 42 4c 4f 42  result is a BLOB
246c0 20 6f 72 20 55 54 46 2d 38 20 73 74 72 69 6e 67   or UTF-8 string
246d0 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65   then the sqlite
246e0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
246f0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75  .** routine retu
24700 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
24710 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20  f bytes in that 
24720 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a  BLOB or string..
24730 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74  ** If the result
24740 20 69 73 20 61 20 55 54 46 2d 31 36 20 73 74 72   is a UTF-16 str
24750 69 6e 67 2c 20 74 68 65 6e 20 73 71 6c 69 74 65  ing, then sqlite
24760 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
24770 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65   converts.** the
24780 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38   string to UTF-8
24790 20 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e   and then return
247a0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
247b0 62 79 74 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65  bytes..** If the
247c0 20 72 65 73 75 6c 74 20 69 73 20 61 20 6e 75 6d   result is a num
247d0 65 72 69 63 20 76 61 6c 75 65 20 74 68 65 6e 20  eric value then 
247e0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
247f0 79 74 65 73 28 29 20 75 73 65 73 0a 2a 2a 20 5b  ytes() uses.** [
24800 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
24810 28 29 5d 20 74 6f 20 63 6f 6e 76 65 72 74 20 74  ()] to convert t
24820 68 61 74 20 76 61 6c 75 65 20 74 6f 20 61 20 55  hat value to a U
24830 54 46 2d 38 20 73 74 72 69 6e 67 20 61 6e 64 20  TF-8 string and 
24840 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e  returns.** the n
24850 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
24860 6e 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a  n that string..*
24870 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  * The value retu
24880 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e  rned does not in
24890 63 6c 75 64 65 20 74 68 65 20 7a 65 72 6f 20 74  clude the zero t
248a0 65 72 6d 69 6e 61 74 6f 72 20 61 74 20 74 68 65  erminator at the
248b0 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73   end.** of the s
248c0 74 72 69 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72  tring.  For clar
248d0 69 74 79 3a 20 74 68 65 20 76 61 6c 75 65 20 72  ity: the value r
248e0 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 6e  eturned is the n
248f0 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65  umber of.** byte
24900 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2c  s in the string,
24910 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20   not the number 
24920 6f 66 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a  of characters..*
24930 2a 0a 2a 2a 20 53 74 72 69 6e 67 73 20 72 65 74  *.** Strings ret
24940 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
24950 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61  _column_text() a
24960 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nd sqlite3_colum
24970 6e 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 65  n_text16(),.** e
24980 76 65 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67  ven empty string
24990 73 2c 20 61 72 65 20 61 6c 77 61 79 73 20 7a 65  s, are always ze
249a0 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 20  ro terminated.  
249b0 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  The return.** va
249c0 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  lue from sqlite3
249d0 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66  _column_blob() f
249e0 6f 72 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68  or a zero-length
249f0 20 42 4c 4f 42 20 69 73 20 61 6e 20 61 72 62 69   BLOB is an arbi
24a00 74 72 61 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72  trary.** pointer
24a10 2c 20 70 6f 73 73 69 62 6c 79 20 65 76 65 6e 20  , possibly even 
24a20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
24a30 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
24a40 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
24a50 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69  () routine is si
24a60 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33  milar to sqlite3
24a70 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a  _column_bytes().
24a80 2a 2a 20 62 75 74 20 6c 65 61 76 65 73 20 74 68  ** but leaves th
24a90 65 20 72 65 73 75 6c 74 20 69 6e 20 55 54 46 2d  e result in UTF-
24aa0 31 36 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74  16 in native byt
24ab0 65 20 6f 72 64 65 72 20 69 6e 73 74 65 61 64 20  e order instead 
24ac0 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65  of UTF-8..** The
24ad0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
24ae0 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64   is not included
24af0 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a   in this count..
24b00 2a 2a 0a 2a 2a 20 54 68 65 20 6f 62 6a 65 63 74  **.** The object
24b10 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
24b20 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
24b30 75 65 28 29 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b  ue()] is an.** [
24b40 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  unprotected sqli
24b50 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
24b60 74 2e 20 20 41 6e 20 75 6e 70 72 6f 74 65 63 74  t.  An unprotect
24b70 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
24b80 20 6f 62 6a 65 63 74 0a 2a 2a 20 6d 61 79 20 6f   object.** may o
24b90 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68  nly be used with
24ba0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76   [sqlite3_bind_v
24bb0 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  alue()] and [sql
24bc0 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
24bd0 65 28 29 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20  e()]..** If the 
24be0 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c  [unprotected sql
24bf0 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
24c00 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  ct returned by.*
24c10 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  * [sqlite3_colum
24c20 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 75 73  n_value()] is us
24c30 65 64 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20  ed in any other 
24c40 77 61 79 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63  way, including c
24c50 61 6c 6c 73 0a 2a 2a 20 74 6f 20 72 6f 75 74 69  alls.** to routi
24c60 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65  nes like [sqlite
24c70 33 5f 76 61 6c 75 65 5f 69 6e 74 28 29 5d 2c 20  3_value_int()], 
24c80 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
24c90 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73  ext()],.** or [s
24ca0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
24cb0 65 73 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20  es()], then the 
24cc0 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
24cd0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  fined..**.** The
24ce0 73 65 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65  se routines atte
24cf0 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 20 74  mpt to convert t
24d00 68 65 20 76 61 6c 75 65 20 77 68 65 72 65 20 61  he value where a
24d10 70 70 72 6f 70 72 69 61 74 65 2e 20 20 46 6f 72  ppropriate.  For
24d20 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  .** example, if 
24d30 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
24d40 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 46  resentation is F
24d50 4c 4f 41 54 20 61 6e 64 20 61 20 74 65 78 74 20  LOAT and a text 
24d60 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 72 65 71  result.** is req
24d70 75 65 73 74 65 64 2c 20 5b 73 71 6c 69 74 65 33  uested, [sqlite3
24d80 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 69 73 20  _snprintf()] is 
24d90 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20  used internally 
24da0 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 0a 2a  to perform the.*
24db0 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 75 74  * conversion aut
24dc0 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65  omatically.  The
24dd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65   following table
24de0 20 64 65 74 61 69 6c 73 20 74 68 65 20 63 6f 6e   details the con
24df0 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74  versions.** that
24e00 20 61 72 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a   are applied:.**
24e10 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
24e20 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65  .** <table borde
24e30 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74  r="1">.** <tr><t
24e40 68 3e 20 49 6e 74 65 72 6e 61 6c 3c 62 72 3e 54  h> Internal<br>T
24e50 79 70 65 20 3c 74 68 3e 20 52 65 71 75 65 73 74  ype <th> Request
24e60 65 64 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20  ed<br>Type <th> 
24e70 20 43 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a   Conversion.**.*
24e80 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c  * <tr><td>  NULL
24e90 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52      <td> INTEGER
24ea0 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69     <td> Result i
24eb0 73 20 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  s 0.** <tr><td> 
24ec0 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 46   NULL    <td>  F
24ed0 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 52 65 73  LOAT    <td> Res
24ee0 75 6c 74 20 69 73 20 30 2e 30 0a 2a 2a 20 3c 74  ult is 0.0.** <t
24ef0 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20  r><td>  NULL    
24f00 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c  <td>   TEXT    <
24f10 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 4e 55  td> Result is NU
24f20 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74  LL pointer.** <t
24f30 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20  r><td>  NULL    
24f40 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c  <td>   BLOB    <
24f50 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 4e 55  td> Result is NU
24f60 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74  LL pointer.** <t
24f70 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20  r><td> INTEGER  
24f80 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c  <td>  FLOAT    <
24f90 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d  td> Convert from
24fa0 20 69 6e 74 65 67 65 72 20 74 6f 20 66 6c 6f 61   integer to floa
24fb0 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e  t.** <tr><td> IN
24fc0 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 54 45  TEGER  <td>   TE
24fd0 58 54 20 20 20 20 3c 74 64 3e 20 41 53 43 49 49  XT    <td> ASCII
24fe0 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68   rendering of th
24ff0 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72  e integer.** <tr
25000 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c  ><td> INTEGER  <
25010 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74  td>   BLOB    <t
25020 64 3e 20 53 61 6d 65 20 61 73 20 49 4e 54 45 47  d> Same as INTEG
25030 45 52 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e  ER->TEXT.** <tr>
25040 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74  <td>  FLOAT   <t
25050 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64  d> INTEGER   <td
25060 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 66  > Convert from f
25070 6c 6f 61 74 20 74 6f 20 69 6e 74 65 67 65 72 0a  loat to integer.
25080 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f  ** <tr><td>  FLO
25090 41 54 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54  AT   <td>   TEXT
250a0 20 20 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72      <td> ASCII r
250b0 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20  endering of the 
250c0 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64  float.** <tr><td
250d0 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20  >  FLOAT   <td> 
250e0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53    BLOB    <td> S
250f0 61 6d 65 20 61 73 20 46 4c 4f 41 54 2d 3e 54 45  ame as FLOAT->TE
25100 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  XT.** <tr><td>  
25110 54 45 58 54 20 20 20 20 3c 74 64 3e 20 49 4e 54  TEXT    <td> INT
25120 45 47 45 52 20 20 20 3c 74 64 3e 20 55 73 65 20  EGER   <td> Use 
25130 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74  atoi().** <tr><t
25140 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  d>  TEXT    <td>
25150 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20    FLOAT    <td> 
25160 55 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74  Use atof().** <t
25170 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20  r><td>  TEXT    
25180 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c  <td>   BLOB    <
25190 74 64 3e 20 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a  td> No change.**
251a0 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20   <tr><td>  BLOB 
251b0 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20     <td> INTEGER 
251c0 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74    <td> Convert t
251d0 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73 65 20  o TEXT then use 
251e0 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74  atoi().** <tr><t
251f0 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e  d>  BLOB    <td>
25200 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20    FLOAT    <td> 
25210 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20  Convert to TEXT 
25220 74 68 65 6e 20 75 73 65 20 61 74 6f 66 28 29 0a  then use atof().
25230 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f  ** <tr><td>  BLO
25240 42 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54  B    <td>   TEXT
25250 20 20 20 20 3c 74 64 3e 20 41 64 64 20 61 20 7a      <td> Add a z
25260 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69  ero terminator i
25270 66 20 6e 65 65 64 65 64 0a 2a 2a 20 3c 2f 74 61  f needed.** </ta
25280 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71  ble>.** </blockq
25290 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  uote>.**.** The 
252a0 74 61 62 6c 65 20 61 62 6f 76 65 20 6d 61 6b 65  table above make
252b0 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 73  s reference to s
252c0 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72  tandard C librar
252d0 79 20 66 75 6e 63 74 69 6f 6e 73 20 61 74 6f 69  y functions atoi
252e0 28 29 0a 2a 2a 20 61 6e 64 20 61 74 6f 66 28 29  ().** and atof()
252f0 2e 20 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e  .  SQLite does n
25300 6f 74 20 72 65 61 6c 6c 79 20 75 73 65 20 74 68  ot really use th
25310 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  ese functions.  
25320 49 74 20 68 61 73 20 69 74 73 0a 2a 2a 20 6f 77  It has its.** ow
25330 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74  n equivalent int
25340 65 72 6e 61 6c 20 72 6f 75 74 69 6e 65 73 2e 20  ernal routines. 
25350 20 54 68 65 20 61 74 6f 69 28 29 20 61 6e 64 20   The atoi() and 
25360 61 74 6f 66 28 29 20 6e 61 6d 65 73 20 61 72 65  atof() names are
25370 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74 68 65 20  .** used in the 
25380 74 61 62 6c 65 20 66 6f 72 20 62 72 65 76 69 74  table for brevit
25390 79 20 61 6e 64 20 62 65 63 61 75 73 65 20 74 68  y and because th
253a0 65 79 20 61 72 65 20 66 61 6d 69 6c 69 61 72 20  ey are familiar 
253b0 74 6f 20 6d 6f 73 74 0a 2a 2a 20 43 20 70 72 6f  to most.** C pro
253c0 67 72 61 6d 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  grammers..**.** 
253d0 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 74  Note that when t
253e0 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  ype conversions 
253f0 6f 63 63 75 72 2c 20 70 6f 69 6e 74 65 72 73 20  occur, pointers 
25400 72 65 74 75 72 6e 65 64 20 62 79 20 70 72 69 6f  returned by prio
25410 72 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71  r.** calls to sq
25420 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
25430 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c  b(), sqlite3_col
25440 75 6d 6e 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f  umn_text(), and/
25450 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  or.** sqlite3_co
25460 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 6d 61  lumn_text16() ma
25470 79 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64  y be invalidated
25480 2e 0a 2a 2a 20 54 79 70 65 20 63 6f 6e 76 65 72  ..** Type conver
25490 73 69 6f 6e 73 20 61 6e 64 20 70 6f 69 6e 74 65  sions and pointe
254a0 72 20 69 6e 76 61 6c 69 64 61 74 69 6f 6e 73 20  r invalidations 
254b0 6d 69 67 68 74 20 6f 63 63 75 72 0a 2a 2a 20 69  might occur.** i
254c0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
254d0 63 61 73 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  cases:.**.** <ul
254e0 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e  >.** <li> The in
254f0 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73  itial content is
25500 20 61 20 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69   a BLOB and sqli
25510 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
25520 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c  ) or.**      sql
25530 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
25540 31 36 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20  16() is called. 
25550 20 41 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   A zero-terminat
25560 6f 72 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20  or might.**     
25570 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64 65   need to be adde
25580 64 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 2e  d to the string.
25590 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68  </li>.** <li> Th
255a0 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e  e initial conten
255b0 74 20 69 73 20 55 54 46 2d 38 20 74 65 78 74 20  t is UTF-8 text 
255c0 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  and sqlite3_colu
255d0 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 6f 72 0a  mn_bytes16() or.
255e0 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  **      sqlite3_
255f0 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20  column_text16() 
25600 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20  is called.  The 
25610 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20  content must be 
25620 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 20  converted.**    
25630 20 20 74 6f 20 55 54 46 2d 31 36 2e 3c 2f 6c 69    to UTF-16.</li
25640 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e  >.** <li> The in
25650 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73  itial content is
25660 20 55 54 46 2d 31 36 20 74 65 78 74 20 61 6e 64   UTF-16 text and
25670 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25680 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20  bytes() or.**   
25690 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
256a0 6e 5f 74 65 78 74 28 29 20 69 73 20 63 61 6c 6c  n_text() is call
256b0 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74  ed.  The content
256c0 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74   must be convert
256d0 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54  ed.**      to UT
256e0 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75  F-8.</li>.** </u
256f0 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73  l>.**.** Convers
25700 69 6f 6e 73 20 62 65 74 77 65 65 6e 20 55 54 46  ions between UTF
25710 2d 31 36 62 65 20 61 6e 64 20 55 54 46 2d 31 36  -16be and UTF-16
25720 6c 65 20 61 72 65 20 61 6c 77 61 79 73 20 64 6f  le are always do
25730 6e 65 20 69 6e 20 70 6c 61 63 65 20 61 6e 64 20  ne in place and 
25740 64 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69  do.** not invali
25750 64 61 74 65 20 61 20 70 72 69 6f 72 20 70 6f 69  date a prior poi
25760 6e 74 65 72 2c 20 74 68 6f 75 67 68 20 6f 66 20  nter, though of 
25770 63 6f 75 72 73 65 20 74 68 65 20 63 6f 6e 74 65  course the conte
25780 6e 74 20 6f 66 20 74 68 65 20 62 75 66 66 65 72  nt of the buffer
25790 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 69  .** that the pri
257a0 6f 72 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74  or pointer point
257b0 73 20 74 6f 20 77 69 6c 6c 20 68 61 76 65 20 62  s to will have b
257c0 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f  een modified.  O
257d0 74 68 65 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66  ther kinds.** of
257e0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 72 65 20   conversion are 
257f0 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20 77 68  done in place wh
25800 65 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  en it is possibl
25810 65 2c 20 62 75 74 20 73 6f 6d 65 74 69 6d 65 73  e, but sometimes
25820 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 6e 6f 74   they.** are not
25830 20 70 6f 73 73 69 62 6c 65 20 61 6e 64 20 69 6e   possible and in
25840 20 74 68 6f 73 65 20 63 61 73 65 73 20 70 72 69   those cases pri
25850 6f 72 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20  or pointers are 
25860 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a  invalidated..**.
25870 2a 2a 20 54 68 65 20 73 61 66 65 73 74 20 61 6e  ** The safest an
25880 64 20 65 61 73 69 65 73 74 20 74 6f 20 72 65 6d  d easiest to rem
25890 65 6d 62 65 72 20 70 6f 6c 69 63 79 20 69 73 20  ember policy is 
258a0 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 73 65 20  to invoke these 
258b0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f  routines.** in o
258c0 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
258d0 69 6e 67 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20  ing ways:.**.** 
258e0 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c  <ul>.**  <li>sql
258f0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
25900 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73  () followed by s
25910 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
25920 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c  tes()</li>.**  <
25930 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  li>sqlite3_colum
25940 6e 5f 62 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65  n_blob() followe
25950 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d by sqlite3_col
25960 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e  umn_bytes()</li>
25970 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33  .**  <li>sqlite3
25980 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29  _column_text16()
25990 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c   followed by sql
259a0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
259b0 73 31 36 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f  s16()</li>.** </
259c0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68  ul>.**.** In oth
259d0 65 72 20 77 6f 72 64 73 2c 20 79 6f 75 20 73 68  er words, you sh
259e0 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65  ould call sqlite
259f0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c  3_column_text(),
25a00 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  .** sqlite3_colu
25a10 6d 6e 5f 62 6c 6f 62 28 29 2c 20 6f 72 20 73 71  mn_blob(), or sq
25a20 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
25a30 74 31 36 28 29 20 66 69 72 73 74 20 74 6f 20 66  t16() first to f
25a40 6f 72 63 65 20 74 68 65 20 72 65 73 75 6c 74 0a  orce the result.
25a50 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 65 73 69  ** into the desi
25a60 72 65 64 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e  red format, then
25a70 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f   invoke sqlite3_
25a80 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f  column_bytes() o
25a90 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c  r.** sqlite3_col
25aa0 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 74 6f  umn_bytes16() to
25ab0 20 66 69 6e 64 20 74 68 65 20 73 69 7a 65 20 6f   find the size o
25ac0 66 20 74 68 65 20 72 65 73 75 6c 74 2e 20 20 44  f the result.  D
25ad0 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 0a  o not mix calls.
25ae0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ** to sqlite3_co
25af0 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 20 73  lumn_text() or s
25b00 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
25b10 6f 62 28 29 20 77 69 74 68 20 63 61 6c 6c 73 20  ob() with calls 
25b20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  to.** sqlite3_co
25b30 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 2c 20  lumn_bytes16(), 
25b40 61 6e 64 20 64 6f 20 6e 6f 74 20 6d 69 78 20 63  and do not mix c
25b50 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  alls to sqlite3_
25b60 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a  column_text16().
25b70 2a 2a 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f  ** with calls to
25b80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25b90 62 79 74 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  bytes()..**.** T
25ba0 68 65 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75  he pointers retu
25bb0 72 6e 65 64 20 61 72 65 20 76 61 6c 69 64 20 75  rned are valid u
25bc0 6e 74 69 6c 20 61 20 74 79 70 65 20 63 6f 6e 76  ntil a type conv
25bd0 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 20 61 73  ersion occurs as
25be0 0a 2a 2a 20 64 65 73 63 72 69 62 65 64 20 61 62  .** described ab
25bf0 6f 76 65 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73  ove, or until [s
25c00 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f  qlite3_step()] o
25c10 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  r [sqlite3_reset
25c20 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  ()] or.** [sqlit
25c30 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69  e3_finalize()] i
25c40 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d  s called.  The m
25c50 65 6d 6f 72 79 20 73 70 61 63 65 20 75 73 65 64  emory space used
25c60 20 74 6f 20 68 6f 6c 64 20 73 74 72 69 6e 67 73   to hold strings
25c70 0a 2a 2a 20 61 6e 64 20 42 4c 4f 42 73 20 69 73  .** and BLOBs is
25c80 20 66 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63   freed automatic
25c90 61 6c 6c 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74  ally.  Do <b>not
25ca0 3c 2f 62 3e 20 70 61 73 73 20 74 68 65 20 70 6f  </b> pass the po
25cb0 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 0a  inters returned.
25cc0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ** [sqlite3_colu
25cd0 6d 6e 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c  mn_blob()], [sql
25ce0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
25cf0 28 29 5d 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a  ()], etc. into.*
25d00 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  * [sqlite3_free(
25d10 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d  )]..**.** If a m
25d20 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
25d30 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
25d40 72 69 6e 67 20 74 68 65 20 65 76 61 6c 75 61 74  ring the evaluat
25d50 69 6f 6e 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66  ion of any.** of
25d60 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c   these routines,
25d70 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65   a default value
25d80 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
25d90 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
25da0 0a 2a 2a 20 69 73 20 65 69 74 68 65 72 20 74 68  .** is either th
25db0 65 20 69 6e 74 65 67 65 72 20 30 2c 20 74 68 65  e integer 0, the
25dc0 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
25dd0 6e 75 6d 62 65 72 20 30 2e 30 2c 20 6f 72 20 61  number 0.0, or a
25de0 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72   NULL.** pointer
25df0 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 61  .  Subsequent ca
25e00 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
25e10 65 72 72 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20  errcode()] will 
25e20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54  return.** [SQLIT
25e30 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20  E_NOMEM]..**.** 
25e40 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
25e50 20 5b 48 31 33 38 30 33 5d 20 5b 48 31 33 38 30   [H13803] [H1380
25e60 36 5d 20 5b 48 31 33 38 30 39 5d 20 5b 48 31 33  6] [H13809] [H13
25e70 38 31 32 5d 20 5b 48 31 33 38 31 35 5d 20 5b 48  812] [H13815] [H
25e80 31 33 38 31 38 5d 20 5b 48 31 33 38 32 31 5d 20  13818] [H13821] 
25e90 5b 48 31 33 38 32 34 5d 0a 2a 2a 20 5b 48 31 33  [H13824].** [H13
25ea0 38 32 37 5d 20 5b 48 31 33 38 33 30 5d 0a 2a 2f  827] [H13830].*/
25eb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
25ec0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
25ed0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69  column_blob(sqli
25ee0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
25ef0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  Col);.SQLITE_API
25f00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
25f10 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65  umn_bytes(sqlite
25f20 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
25f30 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
25f40 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
25f50 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65  n_bytes16(sqlite
25f60 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
25f70 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64  l);.SQLITE_API d
25f80 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f  ouble sqlite3_co
25f90 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69  lumn_double(sqli
25fa0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
25fb0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  Col);.SQLITE_API
25fc0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
25fd0 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f  umn_int(sqlite3_
25fe0 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
25ff0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  ;.SQLITE_API sql
26000 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74  ite3_int64 sqlit
26010 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
26020 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
26030 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
26040 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67  _API const unsig
26050 6e 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ned char *sqlite
26060 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71  3_column_text(sq
26070 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
26080 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
26090 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
260a0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
260b0 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  xt16(sqlite3_stm
260c0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
260d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
260e0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
260f0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
26100 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
26110 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76  TE_API sqlite3_v
26120 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f  alue *sqlite3_co
26130 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74  lumn_value(sqlit
26140 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
26150 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ol);../*.** CAPI
26160 33 52 45 46 3a 20 44 65 73 74 72 6f 79 20 41 20  3REF: Destroy A 
26170 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65  Prepared Stateme
26180 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 30  nt Object {H1330
26190 30 7d 20 3c 53 37 30 33 30 30 3e 3c 53 33 30 31  0} <S70300><S301
261a0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
261b0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
261c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
261d0 6c 65 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20  led to delete a 
261e0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
261f0 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ent]..** If the 
26200 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 65 78  statement was ex
26210 65 63 75 74 65 64 20 73 75 63 63 65 73 73 66 75  ecuted successfu
26220 6c 6c 79 20 6f 72 20 6e 6f 74 20 65 78 65 63 75  lly or not execu
26230 74 65 64 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e  ted at all, then
26240 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
26250 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 65 78   returned. If ex
26260 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ecution of the s
26270 74 61 74 65 6d 65 6e 74 20 66 61 69 6c 65 64 20  tatement failed 
26280 74 68 65 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f  then an.** [erro
26290 72 20 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65  r code] or [exte
262a0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d  nded error code]
262b0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
262c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
262d0 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61   can be called a
262e0 74 20 61 6e 79 20 70 6f 69 6e 74 20 64 75 72 69  t any point duri
262f0 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e  ng the execution
26300 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70   of the.** [prep
26310 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
26320 20 20 49 66 20 74 68 65 20 76 69 72 74 75 61 6c    If the virtual
26330 20 6d 61 63 68 69 6e 65 20 68 61 73 20 6e 6f 74   machine has not
26340 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78  .** completed ex
26350 65 63 75 74 69 6f 6e 20 77 68 65 6e 20 74 68 69  ecution when thi
26360 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
26370 6c 65 64 2c 20 74 68 61 74 20 69 73 20 6c 69 6b  led, that is lik
26380 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e  e.** encounterin
26390 67 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e  g an error or an
263a0 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72   [sqlite3_interr
263b0 75 70 74 20 7c 20 69 6e 74 65 72 72 75 70 74 5d  upt | interrupt]
263c0 2e 0a 2a 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20  ..** Incomplete 
263d0 75 70 64 61 74 65 73 20 6d 61 79 20 62 65 20 72  updates may be r
263e0 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74  olled back and t
263f0 72 61 6e 73 61 63 74 69 6f 6e 73 20 63 61 6e 63  ransactions canc
26400 65 6c 65 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69  eled,.** dependi
26410 6e 67 20 6f 6e 20 74 68 65 20 63 69 72 63 75 6d  ng on the circum
26420 73 74 61 6e 63 65 73 2c 20 61 6e 64 20 74 68 65  stances, and the
26430 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d  .** [error code]
26440 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 62   returned will b
26450 65 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d  e [SQLITE_ABORT]
26460 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
26470 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 31 33 30 32  ents:.** [H11302
26480 5d 20 5b 48 31 31 33 30 34 5d 0a 2a 2f 0a 53 51  ] [H11304].*/.SQ
26490 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
264a0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71  ite3_finalize(sq
264b0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
264c0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
264d0 52 45 46 3a 20 52 65 73 65 74 20 41 20 50 72 65  REF: Reset A Pre
264e0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
264f0 4f 62 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20  Object {H13330} 
26500 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S70300>.**.** T
26510 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  he sqlite3_reset
26520 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
26530 61 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61  alled to reset a
26540 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
26550 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20  ment].** object 
26560 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74  back to its init
26570 69 61 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79  ial state, ready
26580 20 74 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74   to be re-execut
26590 65 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73  ed..** Any SQL s
265a0 74 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c  tatement variabl
265b0 65 73 20 74 68 61 74 20 68 61 64 20 76 61 6c 75  es that had valu
265c0 65 73 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d  es bound to them
265d0 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73   using.** the [s
265e0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
265f0 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   | sqlite3_bind_
26600 2a 28 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20  *() API] retain 
26610 74 68 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a  their values..**
26620 20 55 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c   Use [sqlite3_cl
26630 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20  ear_bindings()] 
26640 74 6f 20 72 65 73 65 74 20 74 68 65 20 62 69 6e  to reset the bin
26650 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  dings..**.** {H1
26660 31 33 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74  1332} The [sqlit
26670 65 33 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74  e3_reset(S)] int
26680 65 72 66 61 63 65 20 72 65 73 65 74 73 20 74 68  erface resets th
26690 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
266a0 65 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20  ement] S.**     
266b0 20 20 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65       back to the
266c0 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74   beginning of it
266d0 73 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a  s program..**.**
266e0 20 7b 48 31 31 33 33 34 7d 20 49 66 20 74 68 65   {H11334} If the
266f0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c   most recent cal
26700 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
26710 65 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a  ep(S)] for the.*
26720 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70  *          [prep
26730 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
26740 53 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49  S returned [SQLI
26750 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49  TE_ROW] or [SQLI
26760 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20  TE_DONE],.**    
26770 20 20 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c        or if [sql
26780 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61  ite3_step(S)] ha
26790 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62  s never before b
267a0 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c  een called on S,
267b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
267c0 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  n [sqlite3_reset
267d0 28 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51  (S)] returns [SQ
267e0 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20  LITE_OK]..**.** 
267f0 7b 48 31 31 33 33 36 7d 20 49 66 20 74 68 65 20  {H11336} If the 
26800 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c  most recent call
26810 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   to [sqlite3_ste
26820 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a  p(S)] for the.**
26830 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
26840 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
26850 20 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72   indicated an er
26860 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20  ror, then.**    
26870 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72        [sqlite3_r
26880 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73  eset(S)] returns
26890 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
268a0 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a  [error code]..**
268b0 0a 2a 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65  .** {H11338} The
268c0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
268d0 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f  S)] interface do
268e0 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  es not change th
268f0 65 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20  e values.**     
26900 20 20 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c       of any [sql
26910 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62  ite3_bind_blob|b
26920 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20  indings] on the 
26930 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
26940 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54  ent] S..*/.SQLIT
26950 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
26960 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f  3_reset(sqlite3_
26970 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f  stmt *pStmt);../
26980 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
26990 72 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e  reate Or Redefin
269a0 65 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20  e SQL Functions 
269b0 7b 48 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30  {H16100} <S20200
269c0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
269d0 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f  function creatio
269e0 6e 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b  n routines}.** K
269f0 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63  EYWORDS: {applic
26a00 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51  ation-defined SQ
26a10 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b  L function}.** K
26a20 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63  EYWORDS: {applic
26a30 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51  ation-defined SQ
26a40 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a  L functions}.**.
26a50 2a 2a 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e  ** These two fun
26a60 63 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69  ctions (collecti
26a70 76 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66  vely known as "f
26a80 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e  unction creation
26a90 20 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61   routines").** a
26aa0 72 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53  re used to add S
26ab0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20  QL functions or 
26ac0 61 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f  aggregates or to
26ad0 20 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65   redefine the be
26ae0 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69  havior.** of exi
26af0 73 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69  sting SQL functi
26b00 6f 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65  ons or aggregate
26b10 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66  s.  The only dif
26b20 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
26b30 74 68 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68  the.** two is th
26b40 61 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  at the second pa
26b50 72 61 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d  rameter, the nam
26b60 65 20 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72  e of the (scalar
26b70 29 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a  ) function or.**
26b80 20 61 67 67 72 65 67 61 74 65 2c 20 69 73 20 65   aggregate, is e
26b90 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20  ncoded in UTF-8 
26ba0 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61  for sqlite3_crea
26bb0 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e  te_function() an
26bc0 64 20 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20  d UTF-16.** for 
26bd0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
26be0 75 6e 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a  unction16()..**.
26bf0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72  ** The first par
26c00 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64  ameter is the [d
26c10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
26c20 6f 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65  on] to which the
26c30 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e   SQL.** function
26c40 20 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e   is to be added.
26c50 20 20 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72    If a single pr
26c60 6f 67 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20  ogram uses more 
26c70 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  than one databas
26c80 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e.** connection 
26c90 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e  internally, then
26ca0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d   SQL functions m
26cb0 75 73 74 20 62 65 20 61 64 64 65 64 20 69 6e 64  ust be added ind
26cc0 69 76 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20  ividually to.** 
26cd0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f  each database co
26ce0 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nnection..**.** 
26cf0 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
26d00 65 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65  eter is the name
26d10 20 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63   of the SQL func
26d20 74 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74  tion to be creat
26d30 65 64 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e  ed or.** redefin
26d40 65 64 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20  ed.  The length 
26d50 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c  of the name is l
26d60 69 6d 69 74 65 64 20 74 6f 20 32 35 35 20 62 79  imited to 255 by
26d70 74 65 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f  tes, exclusive o
26d80 66 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65  f.** the zero-te
26d90 72 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20  rminator.  Note 
26da0 74 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65  that the name le
26db0 6e 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e  ngth limit is in
26dc0 20 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63   bytes, not.** c
26dd0 68 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20  haracters.  Any 
26de0 61 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74  attempt to creat
26df0 65 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  e a function wit
26e00 68 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a  h a longer name.
26e10 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  ** will result i
26e20 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  n [SQLITE_ERROR]
26e30 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e   being returned.
26e40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
26e50 20 70 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67   parameter (nArg
26e60 29 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62  ).** is the numb
26e70 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
26e80 74 68 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e  that the SQL fun
26e90 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72  ction or.** aggr
26ea0 65 67 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20  egate takes. If 
26eb0 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69  this parameter i
26ec0 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  s negative, then
26ed0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
26ee0 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74  n or.** aggregat
26ef0 65 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e  e may take any n
26f00 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
26f10 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ts..**.** The fo
26f20 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20  urth parameter, 
26f30 65 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66  eTextRep, specif
26f40 69 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c  ies what.** [SQL
26f50 49 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20  ITE_UTF8 | text 
26f60 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53  encoding] this S
26f70 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66  QL function pref
26f80 65 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70  ers for.** its p
26f90 61 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20  arameters.  Any 
26fa0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  SQL function imp
26fb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75  lementation shou
26fc0 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f  ld be able to wo
26fd0 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20  rk.** work with 
26fe0 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c  UTF-8, UTF-16le,
26ff0 20 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42   or UTF-16be.  B
27000 75 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e  ut some implemen
27010 74 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a  tations may be.*
27020 2a 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74  * more efficient
27030 20 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69   with one encodi
27040 6e 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e  ng than another.
27050 20 20 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20    It is allowed 
27060 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c  to.** invoke sql
27070 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
27080 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65  tion() or sqlite
27090 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
270a0 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a  n16() multiple.*
270b0 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65  * times with the
270c0 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62   same function b
270d0 75 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e  ut with differen
270e0 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78  t values of eTex
270f0 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75  tRep..** When mu
27100 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  ltiple implement
27110 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61  ations of the sa
27120 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20  me function are 
27130 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74  available, SQLit
27140 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74  e.** will pick t
27150 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f  he one that invo
27160 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61  lves the least a
27170 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f  mount of data co
27180 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20  nversion..** If 
27190 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20  there is only a 
271a0 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  single implement
271b0 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73  ation which does
271c0 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74   not care what t
271d0 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20  ext.** encoding 
271e0 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68  is used, then th
271f0 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  e fourth argumen
27200 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c  t should be [SQL
27210 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20  ITE_ANY]..**.** 
27220 54 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65  The fifth parame
27230 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72  ter is an arbitr
27240 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68  ary pointer.  Th
27250 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
27260 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74   of the.** funct
27270 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63  ion can gain acc
27280 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e  ess to this poin
27290 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ter using [sqlit
272a0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e  e3_user_data()].
272b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e  .**.** The seven
272c0 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e  th, eighth and n
272d0 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c  inth parameters,
272e0 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e   xFunc, xStep an
272f0 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a  d xFinal, are.**
27300 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c   pointers to C-l
27310 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e  anguage function
27320 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
27330 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
27340 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74  n or.** aggregat
27350 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20  e. A scalar SQL 
27360 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65  function require
27370 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  s an implementat
27380 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63  ion of the xFunc
27390 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c  .** callback onl
273a0 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73  y, NULL pointers
273b0 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65   should be passe
273c0 64 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61  d as the xStep a
273d0 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72  nd xFinal.** par
273e0 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72  ameters. An aggr
273f0 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69  egate SQL functi
27400 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69  on requires an i
27410 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
27420 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46   xStep.** and xF
27430 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68  inal and NULL sh
27440 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66  ould be passed f
27450 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c  or xFunc. To del
27460 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  ete an existing.
27470 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ** SQL function 
27480 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61  or aggregate, pa
27490 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20  ss NULL for all 
274a0 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63  three function c
274b0 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  allbacks..**.** 
274c0 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20  It is permitted 
274d0 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74  to register mult
274e0 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  iple implementat
274f0 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
27500 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69  .** functions wi
27510 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
27520 20 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72   but with either
27530 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65   differing numbe
27540 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e  rs of.** argumen
27550 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20  ts or differing 
27560 70 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  preferred text e
27570 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74  ncodings.  SQLit
27580 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68  e will use.** th
27590 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
275a0 20 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61   most closely ma
275b0 74 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e  tches the way in
275c0 20 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51   which the.** SQ
275d0 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  L function is us
275e0 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20  ed.  A function 
275f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
27600 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69  ith a non-negati
27610 76 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d  ve.** nArg param
27620 65 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72  eter is a better
27630 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75   match than a fu
27640 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
27650 61 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20  ation with.** a 
27660 6e 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20  negative nArg.  
27670 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65  A function where
27680 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74   the preferred t
27690 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  ext encoding.** 
276a0 6d 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61  matches the data
276b0 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73  base encoding is
276c0 20 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74   a better.** mat
276d0 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69  ch than a functi
276e0 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63  on where the enc
276f0 6f 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65  oding is differe
27700 6e 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74  nt.  .** A funct
27710 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e  ion where the en
27720 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63  coding differenc
27730 65 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46  e is between UTF
27740 31 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65  16le and UTF16be
27750 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20  .** is a closer 
27760 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e  match than a fun
27770 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20  ction where the 
27780 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65  encoding differe
27790 6e 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65  nce is.** betwee
277a0 6e 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36  n UTF8 and UTF16
277b0 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e  ..**.** Built-in
277c0 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62   functions may b
277d0 65 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20  e overloaded by 
277e0 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  new application-
277f0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
27800 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  s..** The first 
27810 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
27820 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  ned function wit
27830 68 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f  h a given name o
27840 76 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20  verrides all.** 
27850 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
27860 6e 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b  ns in the same [
27870 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
27880 69 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61  ion] with the sa
27890 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73  me name..** Subs
278a0 65 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69  equent applicati
278b0 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
278c0 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
278d0 20 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72   name only overr
278e0 69 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70  ide .** prior ap
278f0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
27900 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  d functions that
27910 20 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61   are an exact ma
27920 74 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e  tch for the.** n
27930 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74  umber of paramet
27940 65 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65  ers and preferre
27950 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a  d encoding..**.*
27960 2a 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  * An application
27970 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
27980 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74  n is permitted t
27990 6f 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20  o call other.** 
279a0 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
279b0 73 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63  s.  However, suc
279c0 68 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74  h calls must not
279d0 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61  .** close the da
279e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
279f0 6e 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f  n nor finalize o
27a00 72 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70  r reset the prep
27a10 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ared.** statemen
27a20 74 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66  t in which the f
27a30 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69  unction is runni
27a40 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ng..**.** Requir
27a50 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31  ements:.** [H161
27a60 30 33 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31  03] [H16106] [H1
27a70 36 31 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b  6109] [H16112] [
27a80 48 31 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d  H16118] [H16121]
27a90 20 5b 48 31 36 31 32 34 5d 20 5b 48 31 36 31 32   [H16124] [H1612
27aa0 37 5d 0a 2a 2a 20 5b 48 31 36 31 33 30 5d 20 5b  7].** [H16130] [
27ab0 48 31 36 31 33 33 5d 20 5b 48 31 36 31 33 36 5d  H16133] [H16136]
27ac0 20 5b 48 31 36 31 33 39 5d 20 5b 48 31 36 31 34   [H16139] [H1614
27ad0 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  2].*/.SQLITE_API
27ae0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
27af0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  ate_function(.  
27b00 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
27b10 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63  onst char *zFunc
27b20 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
27b30 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78  nArg,.  int eTex
27b40 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41  tRep,.  void *pA
27b50 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  pp,.  void (*xFu
27b60 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
27b70 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
27b80 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
27b90 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
27ba0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
27bb0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
27bc0 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
27bd0 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
27be0 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  xt*).);.SQLITE_A
27bf0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
27c00 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
27c10 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
27c20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
27c30 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20  FunctionName,.  
27c40 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20  int nArg,.  int 
27c50 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64  eTextRep,.  void
27c60 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28   *pApp,.  void (
27c70 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
27c80 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
27c90 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20  ite3_value**),. 
27ca0 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
27cb0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
27cc0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
27cd0 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  e**),.  void (*x
27ce0 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
27cf0 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a  ontext*).);../*.
27d00 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 78  ** CAPI3REF: Tex
27d10 74 20 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30  t Encodings {H10
27d20 32 36 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48  267} <S50200> <H
27d30 31 36 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  16100>.**.** The
27d40 73 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69  se constant defi
27d50 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73  ne integer codes
27d60 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20   that represent 
27d70 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74  the various.** t
27d80 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75  ext encodings su
27d90 70 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74  pported by SQLit
27da0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e..*/.#define SQ
27db0 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20  LITE_UTF8       
27dc0 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
27dd0 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20  LITE_UTF16LE    
27de0 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
27df0 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20  LITE_UTF16BE    
27e00 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51      3.#define SQ
27e10 4c 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20  LITE_UTF16      
27e20 20 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20      4    /* Use 
27e30 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
27e40 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  r */.#define SQL
27e50 49 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20  ITE_ANY         
27e60 20 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74     5    /* sqlit
27e70 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
27e80 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  on only */.#defi
27e90 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  ne SQLITE_UTF16_
27ea0 41 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a  ALIGNED  8    /*
27eb0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
27ec0 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a  collation only *
27ed0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
27ee0 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75  F: Deprecated Fu
27ef0 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45  nctions.** DEPRE
27f00 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73  CATED.**.** Thes
27f10 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
27f20 5b 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49  [deprecated].  I
27f30 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74  n order to maint
27f40 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73  ain.** backwards
27f50 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77   compatibility w
27f60 69 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20  ith older code, 
27f70 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
27f80 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20  continue .** to 
27f90 62 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48  be supported.  H
27fa0 6f 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c  owever, new appl
27fb0 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20  ications should 
27fc0 61 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65  avoid.** the use
27fd0 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69   of these functi
27fe0 6f 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e  ons.  To help en
27ff0 63 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74  courage people t
28000 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67  o avoid.** using
28010 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
28020 2c 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69  , we are not goi
28030 6e 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77  ng to tell you w
28040 68 61 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a  hat they do..*/.
28050 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28060 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53  MIT_DEPRECATED.S
28070 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
28080 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20  _DEPRECATED int 
28090 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
280a0 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  e_count(sqlite3_
280b0 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  context*);.SQLIT
280c0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
280d0 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
280e0 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69  te3_expired(sqli
280f0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49  te3_stmt*);.SQLI
28100 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45  TE_API SQLITE_DE
28110 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c  PRECATED int sql
28120 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
28130 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73  ndings(sqlite3_s
28140 74 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74  tmt*, sqlite3_st
28150 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  mt*);.SQLITE_API
28160 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
28170 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  ED int sqlite3_g
28180 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f  lobal_recover(vo
28190 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
281a0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
281b0 44 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74  D void sqlite3_t
281c0 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f  hread_cleanup(vo
281d0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
281e0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
281f0 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65  D int sqlite3_me
28200 6d 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28  mory_alarm(void(
28210 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  *)(void*,sqlite3
28220 5f 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64  _int64,int),void
28230 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  *,sqlite3_int64)
28240 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
28250 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e  CAPI3REF: Obtain
28260 69 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e  ing SQL Function
28270 20 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65   Parameter Value
28280 73 20 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32  s {H15100} <S202
28290 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d  00>.**.** The C-
282a0 6c 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65  language impleme
282b0 6e 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66  ntation of SQL f
282c0 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67  unctions and agg
282d0 72 65 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20  regates uses.** 
282e0 74 68 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65  this set of inte
282f0 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74  rface routines t
28300 6f 20 61 63 63 65 73 73 20 74 68 65 20 70 61 72  o access the par
28310 61 6d 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e  ameter values on
28320 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  .** the function
28330 20 6f 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a   or aggregate..*
28340 2a 0a 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28  *.** The xFunc (
28350 66 6f 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74  for scalar funct
28360 69 6f 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28  ions) or xStep (
28370 66 6f 72 20 61 67 67 72 65 67 61 74 65 73 29 20  for aggregates) 
28380 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f  parameters.** to
28390 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
283a0 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64  _function()] and
283b0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
283c0 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a  _function16()].*
283d0 2a 20 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63  * define callbac
283e0 6b 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ks that implemen
283f0 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  t the SQL functi
28400 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74  ons and aggregat
28410 65 73 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70  es..** The 4th p
28420 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73  arameter to thes
28430 65 20 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61  e callbacks is a
28440 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74  n array of point
28450 65 72 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65  ers to.** [prote
28460 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
28470 75 65 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68  ue] objects.  Th
28480 65 72 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69  ere is one [sqli
28490 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
284a0 74 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61  t for.** each pa
284b0 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53  rameter to the S
284c0 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
284d0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
284e0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72   used to.** extr
284f0 61 63 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20  act values from 
28500 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  the [sqlite3_val
28510 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a  ue] objects..**.
28520 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
28530 73 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68  s work only with
28540 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69   [protected sqli
28550 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
28560 74 73 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d  ts..** Any attem
28570 70 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20  pt to use these 
28580 72 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b  routines on an [
28590 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  unprotected sqli
285a0 74 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62  te3_value].** ob
285b0 6a 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20  ject results in 
285c0 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69  undefined behavi
285d0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  or..**.** These 
285e0 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75  routines work ju
285f0 73 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72  st like the corr
28600 65 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d  esponding [colum
28610 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f  n access functio
28620 6e 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68  ns].** except th
28630 61 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e  at  these routin
28640 65 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65  es take a single
28650 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69   [protected sqli
28660 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
28670 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73  t.** pointer ins
28680 74 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74  tead of a [sqlit
28690 65 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65  e3_stmt*] pointe
286a0 72 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72  r and an integer
286b0 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a   column number..
286c0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
286d0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29  3_value_text16()
286e0 20 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61   interface extra
286f0 63 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72  cts a UTF-16 str
28700 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61  ing.** in the na
28710 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20  tive byte-order 
28720 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68  of the host mach
28730 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c  ine.  The.** sql
28740 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
28750 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65  6be() and sqlite
28760 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
28770 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  () interfaces.**
28780 20 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 20   extract UTF-16 
28790 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65  strings as big-e
287a0 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65  ndian and little
287b0 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69  -endian respecti
287c0 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  vely..**.** The 
287d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
287e0 6d 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74  meric_type() int
287f0 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20  erface attempts 
28800 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65  to apply.** nume
28810 72 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20  ric affinity to 
28820 74 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73  the value.  This
28830 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61   means that an a
28840 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64  ttempt is.** mad
28850 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65  e to convert the
28860 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74   value to an int
28870 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67  eger or floating
28880 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73   point.  If.** s
28890 75 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e  uch a conversion
288a0 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74   is possible wit
288b0 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66  hout loss of inf
288c0 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68  ormation (in oth
288d0 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20  er.** words, if 
288e0 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73  the value is a s
288f0 74 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73  tring that looks
28900 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a   like a number).
28910 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76  ** then the conv
28920 65 72 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72  ersion is perfor
28930 6d 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20  med.  Otherwise 
28940 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63  no conversion oc
28950 63 75 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51  curs..** The [SQ
28960 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64  LITE_INTEGER | d
28970 61 74 61 74 79 70 65 5d 20 61 66 74 65 72 20 63  atatype] after c
28980 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74  onversion is ret
28990 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65  urned..**.** Ple
289a0 61 73 65 20 70 61 79 20 70 61 72 74 69 63 75 6c  ase pay particul
289b0 61 72 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20  ar attention to 
289c0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68  the fact that th
289d0 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e  e pointer return
289e0 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69  ed.** from [sqli
289f0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29  te3_value_blob()
28a00 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ], [sqlite3_valu
28a10 65 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a  e_text()], or.**
28a20 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
28a30 74 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65  text16()] can be
28a40 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20   invalidated by 
28a50 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  a subsequent cal
28a60 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  l to.** [sqlite3
28a70 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c  _value_bytes()],
28a80 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
28a90 62 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c  bytes16()], [sql
28aa0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
28ab0 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74  )],.** or [sqlit
28ac0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
28ad0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  )]..**.** These 
28ae0 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65  routines must be
28af0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
28b00 20 73 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a   same thread as.
28b10 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  ** the SQL funct
28b20 69 6f 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65  ion that supplie
28b30 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  d the [sqlite3_v
28b40 61 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72  alue*] parameter
28b50 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  s..**.** Require
28b60 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 35 31 30  ments:.** [H1510
28b70 33 5d 20 5b 48 31 35 31 30 36 5d 20 5b 48 31 35  3] [H15106] [H15
28b80 31 30 39 5d 20 5b 48 31 35 31 31 32 5d 20 5b 48  109] [H15112] [H
28b90 31 35 31 31 35 5d 20 5b 48 31 35 31 31 38 5d 20  15115] [H15118] 
28ba0 5b 48 31 35 31 32 31 5d 20 5b 48 31 35 31 32 34  [H15121] [H15124
28bb0 5d 0a 2a 2a 20 5b 48 31 35 31 32 37 5d 20 5b 48  ].** [H15127] [H
28bc0 31 35 31 33 30 5d 20 5b 48 31 35 31 33 33 5d 20  15130] [H15133] 
28bd0 5b 48 31 35 31 33 36 5d 0a 2a 2f 0a 53 51 4c 49  [H15136].*/.SQLI
28be0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
28bf0 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  d *sqlite3_value
28c00 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61  _blob(sqlite3_va
28c10 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
28c20 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
28c30 6c 75 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65  lue_bytes(sqlite
28c40 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
28c50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
28c60 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28  3_value_bytes16(
28c70 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28c80 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62  .SQLITE_API doub
28c90 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le sqlite3_value
28ca0 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
28cb0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
28cc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
28cd0 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65  value_int(sqlite
28ce0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
28cf0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e  E_API sqlite3_in
28d00 74 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t64 sqlite3_valu
28d10 65 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f  e_int64(sqlite3_
28d20 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
28d30 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  API const unsign
28d40 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  ed char *sqlite3
28d50 5f 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69  _value_text(sqli
28d60 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
28d70 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
28d80 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  id *sqlite3_valu
28d90 65 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33  e_text16(sqlite3
28da0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
28db0 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
28dc0 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  *sqlite3_value_t
28dd0 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f  ext16le(sqlite3_
28de0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
28df0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
28e00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
28e10 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76  xt16be(sqlite3_v
28e20 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
28e30 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76  PI int sqlite3_v
28e40 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65  alue_type(sqlite
28e50 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
28e60 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
28e70 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
28e80 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  type(sqlite3_val
28e90 75 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ue*);../*.** CAP
28ea0 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 20 41 67  I3REF: Obtain Ag
28eb0 67 72 65 67 61 74 65 20 46 75 6e 63 74 69 6f 6e  gregate Function
28ec0 20 43 6f 6e 74 65 78 74 20 7b 48 31 36 32 31 30   Context {H16210
28ed0 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20200>.**.**
28ee0 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
28ef0 69 6f 6e 20 6f 66 20 61 67 67 72 65 67 61 74 65  ion of aggregate
28f00 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 75   SQL functions u
28f10 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  se this routine 
28f20 74 6f 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61  to allocate.** a
28f30 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 73   structure for s
28f40 74 6f 72 69 6e 67 20 74 68 65 69 72 20 73 74 61  toring their sta
28f50 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  te..**.** The fi
28f60 72 73 74 20 74 69 6d 65 20 74 68 65 20 73 71 6c  rst time the sql
28f70 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
28f80 6f 6e 74 65 78 74 28 29 20 72 6f 75 74 69 6e 65  ontext() routine
28f90 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61   is called for a
28fa0 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 61  .** particular a
28fb0 67 67 72 65 67 61 74 65 2c 20 53 51 4c 69 74 65  ggregate, SQLite
28fc0 20 61 6c 6c 6f 63 61 74 65 73 20 6e 42 79 74 65   allocates nByte
28fd0 73 20 6f 66 20 6d 65 6d 6f 72 79 2c 20 7a 65 72  s of memory, zer
28fe0 6f 65 73 20 6f 75 74 20 74 68 61 74 0a 2a 2a 20  oes out that.** 
28ff0 6d 65 6d 6f 72 79 2c 20 61 6e 64 20 72 65 74 75  memory, and retu
29000 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
29010 20 69 74 2e 20 4f 6e 20 73 65 63 6f 6e 64 20 61   it. On second a
29020 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  nd subsequent ca
29030 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  lls to.** sqlite
29040 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
29050 65 78 74 28 29 20 66 6f 72 20 74 68 65 20 73 61  ext() for the sa
29060 6d 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  me aggregate fun
29070 63 74 69 6f 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20  ction index,.** 
29080 74 68 65 20 73 61 6d 65 20 62 75 66 66 65 72 20  the same buffer 
29090 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65  is returned. The
290a0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
290b0 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65  of the aggregate
290c0 20 63 61 6e 20 75 73 65 0a 2a 2a 20 74 68 65 20   can use.** the 
290d0 72 65 74 75 72 6e 65 64 20 62 75 66 66 65 72 20  returned buffer 
290e0 74 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 64 61  to accumulate da
290f0 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  ta..**.** SQLite
29100 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66   automatically f
29110 72 65 65 73 20 74 68 65 20 61 6c 6c 6f 63 61 74  rees the allocat
29120 65 64 20 62 75 66 66 65 72 20 77 68 65 6e 20 74  ed buffer when t
29130 68 65 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20  he aggregate.** 
29140 71 75 65 72 79 20 63 6f 6e 63 6c 75 64 65 73 2e  query concludes.
29150 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
29160 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c   parameter shoul
29170 64 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74  d be a copy of t
29180 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  he.** [sqlite3_c
29190 6f 6e 74 65 78 74 20 7c 20 53 51 4c 20 66 75 6e  ontext | SQL fun
291a0 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 5d 20 74  ction context] t
291b0 68 61 74 20 69 73 20 74 68 65 20 66 69 72 73 74  hat is the first
291c0 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f   parameter.** to
291d0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f   the callback ro
291e0 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65  utine that imple
291f0 6d 65 6e 74 73 20 74 68 65 20 61 67 67 72 65 67  ments the aggreg
29200 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  ate function..**
29210 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
29220 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
29230 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68  from the same th
29240 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a  read in which.**
29250 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 53   the aggregate S
29260 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72  QL function is r
29270 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65  unning..**.** Re
29280 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
29290 48 31 36 32 31 31 5d 20 5b 48 31 36 32 31 33 5d  H16211] [H16213]
292a0 20 5b 48 31 36 32 31 35 5d 20 5b 48 31 36 32 31   [H16215] [H1621
292b0 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  7].*/.SQLITE_API
292c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61   void *sqlite3_a
292d0 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
292e0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
292f0 2a 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a  *, int nBytes);.
29300 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
29310 20 55 73 65 72 20 44 61 74 61 20 46 6f 72 20 46   User Data For F
29320 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 32 34 30  unctions {H16240
29330 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20200>.**.**
29340 20 54 68 65 20 73 71 6c 69 74 65 33 5f 75 73 65   The sqlite3_use
29350 72 5f 64 61 74 61 28 29 20 69 6e 74 65 72 66 61  r_data() interfa
29360 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70  ce returns a cop
29370 79 20 6f 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e  y of.** the poin
29380 74 65 72 20 74 68 61 74 20 77 61 73 20 74 68 65  ter that was the
29390 20 70 55 73 65 72 44 61 74 61 20 70 61 72 61 6d   pUserData param
293a0 65 74 65 72 20 28 74 68 65 20 35 74 68 20 70 61  eter (the 5th pa
293b0 72 61 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74  rameter).** of t
293c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  he [sqlite3_crea
293d0 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a  te_function()].*
293e0 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  * and [sqlite3_c
293f0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
29400 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ()] routines tha
29410 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20  t originally.** 
29420 72 65 67 69 73 74 65 72 65 64 20 74 68 65 20 61  registered the a
29430 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e  pplication defin
29440 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e  ed function. {EN
29450 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  D}.**.** This ro
29460 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61  utine must be ca
29470 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61  lled from the sa
29480 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69  me thread in whi
29490 63 68 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63  ch.** the applic
294a0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
294b0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
294c0 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  g..**.** Require
294d0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 34  ments:.** [H1624
294e0 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  3].*/.SQLITE_API
294f0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75   void *sqlite3_u
29500 73 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33  ser_data(sqlite3
29510 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a  _context*);../*.
29520 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74  ** CAPI3REF: Dat
29530 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e  abase Connection
29540 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b   For Functions {
29550 48 31 36 32 35 30 7d 20 3c 53 36 30 36 30 30 3e  H16250} <S60600>
29560 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
29570 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  he sqlite3_conte
29580 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 20 69  xt_db_handle() i
29590 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
295a0 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68   a copy of.** th
295b0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  e pointer to the
295c0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
295d0 63 74 69 6f 6e 5d 20 28 74 68 65 20 31 73 74 20  ction] (the 1st 
295e0 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20 6f 66  parameter).** of
295f0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72   the [sqlite3_cr
29600 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d  eate_function()]
29610 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  .** and [sqlite3
29620 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
29630 31 36 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74  16()] routines t
29640 68 61 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a  hat originally.*
29650 2a 20 72 65 67 69 73 74 65 72 65 64 20 74 68 65  * registered the
29660 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66   application def
29670 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ined function..*
29680 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
29690 73 3a 0a 2a 2a 20 5b 48 31 36 32 35 33 5d 0a 2a  s:.** [H16253].*
296a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
296b0 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f  ite3 *sqlite3_co
296c0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
296d0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
296e0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
296f0 45 46 3a 20 46 75 6e 63 74 69 6f 6e 20 41 75 78  EF: Function Aux
29700 69 6c 69 61 72 79 20 44 61 74 61 20 7b 48 31 36  iliary Data {H16
29710 32 37 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a  270} <S20200>.**
29720 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
29730 67 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20  g two functions 
29740 6d 61 79 20 62 65 20 75 73 65 64 20 62 79 20 73  may be used by s
29750 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69  calar SQL functi
29760 6f 6e 73 20 74 6f 0a 2a 2a 20 61 73 73 6f 63 69  ons to.** associ
29770 61 74 65 20 6d 65 74 61 64 61 74 61 20 77 69 74  ate metadata wit
29780 68 20 61 72 67 75 6d 65 6e 74 20 76 61 6c 75 65  h argument value
29790 73 2e 20 49 66 20 74 68 65 20 73 61 6d 65 20 76  s. If the same v
297a0 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 74  alue is passed t
297b0 6f 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 69 6e  o.** multiple in
297c0 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  vocations of the
297d0 20 73 61 6d 65 20 53 51 4c 20 66 75 6e 63 74 69   same SQL functi
297e0 6f 6e 20 64 75 72 69 6e 67 20 71 75 65 72 79 20  on during query 
297f0 65 78 65 63 75 74 69 6f 6e 2c 20 75 6e 64 65 72  execution, under
29800 0a 2a 2a 20 73 6f 6d 65 20 63 69 72 63 75 6d 73  .** some circums
29810 74 61 6e 63 65 73 20 74 68 65 20 61 73 73 6f 63  tances the assoc
29820 69 61 74 65 64 20 6d 65 74 61 64 61 74 61 20 6d  iated metadata m
29830 61 79 20 62 65 20 70 72 65 73 65 72 76 65 64 2e  ay be preserved.
29840 20 54 68 69 73 20 6d 61 79 0a 2a 2a 20 62 65 20   This may.** be 
29850 75 73 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c  used, for exampl
29860 65 2c 20 74 6f 20 61 64 64 20 61 20 72 65 67 75  e, to add a regu
29870 6c 61 72 2d 65 78 70 72 65 73 73 69 6f 6e 20 6d  lar-expression m
29880 61 74 63 68 69 6e 67 20 73 63 61 6c 61 72 0a 2a  atching scalar.*
29890 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  * function. The 
298a0 63 6f 6d 70 69 6c 65 64 20 76 65 72 73 69 6f 6e  compiled version
298b0 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61 72 20   of the regular 
298c0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 74  expression is st
298d0 6f 72 65 64 20 61 73 0a 2a 2a 20 6d 65 74 61 64  ored as.** metad
298e0 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
298f0 69 74 68 20 74 68 65 20 53 51 4c 20 76 61 6c 75  ith the SQL valu
29900 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
29910 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69  regular expressi
29920 6f 6e 0a 2a 2a 20 70 61 74 74 65 72 6e 2e 20 20  on.** pattern.  
29930 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67  The compiled reg
29940 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20  ular expression 
29950 63 61 6e 20 62 65 20 72 65 75 73 65 64 20 6f 6e  can be reused on
29960 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e 76   multiple.** inv
29970 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ocations of the 
29980 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 73 6f  same function so
29990 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e   that the origin
299a0 61 6c 20 70 61 74 74 65 72 6e 20 73 74 72 69 6e  al pattern strin
299b0 67 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6e 65  g.** does not ne
299c0 65 64 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69  ed to be recompi
299d0 6c 65 64 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f  led on each invo
299e0 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cation..**.** Th
299f0 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  e sqlite3_get_au
29a00 78 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 63  xdata() interfac
29a10 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
29a20 74 65 72 20 74 6f 20 74 68 65 20 6d 65 74 61 64  ter to the metad
29a30 61 74 61 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  ata.** associate
29a40 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  d by the sqlite3
29a50 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 66  _set_auxdata() f
29a60 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
29a70 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   Nth argument.**
29a80 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 61 70   value to the ap
29a90 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
29aa0 64 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 6e  d function. If n
29ab0 6f 20 6d 65 74 61 64 61 74 61 20 68 61 73 20 62  o metadata has b
29ac0 65 65 6e 20 65 76 65 72 0a 2a 2a 20 62 65 65 6e  een ever.** been
29ad0 20 73 65 74 20 66 6f 72 20 74 68 65 20 4e 74 68   set for the Nth
29ae0 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65   argument of the
29af0 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 69 66   function, or if
29b00 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
29b10 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 70  ng.** function p
29b20 61 72 61 6d 65 74 65 72 20 68 61 73 20 63 68 61  arameter has cha
29b30 6e 67 65 64 20 73 69 6e 63 65 20 74 68 65 20 6d  nged since the m
29b40 65 74 61 2d 64 61 74 61 20 77 61 73 20 73 65 74  eta-data was set
29b50 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65  ,.** then sqlite
29b60 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20  3_get_auxdata() 
29b70 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70  returns a NULL p
29b80 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ointer..**.** Th
29b90 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  e sqlite3_set_au
29ba0 78 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 63  xdata() interfac
29bb0 65 20 73 61 76 65 73 20 74 68 65 20 6d 65 74 61  e saves the meta
29bc0 64 61 74 61 0a 2a 2a 20 70 6f 69 6e 74 65 64 20  data.** pointed 
29bd0 74 6f 20 62 79 20 69 74 73 20 33 72 64 20 70 61  to by its 3rd pa
29be0 72 61 6d 65 74 65 72 20 61 73 20 74 68 65 20 6d  rameter as the m
29bf0 65 74 61 64 61 74 61 20 66 6f 72 20 74 68 65 20  etadata for the 
29c00 4e 2d 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  N-th.** argument
29c10 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   of the applicat
29c20 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
29c30 74 69 6f 6e 2e 20 20 53 75 62 73 65 71 75 65 6e  tion.  Subsequen
29c40 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71  t.** calls to sq
29c50 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
29c60 61 28 29 20 6d 69 67 68 74 20 72 65 74 75 72 6e  a() might return
29c70 20 74 68 69 73 20 64 61 74 61 2c 20 69 66 20 69   this data, if i
29c80 74 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 62 65 65  t has.** not bee
29c90 6e 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 20  n destroyed..** 
29ca0 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c  If it is not NUL
29cb0 4c 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69  L, SQLite will i
29cc0 6e 76 6f 6b 65 20 74 68 65 20 64 65 73 74 72 75  nvoke the destru
29cd0 63 74 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ctor.** function
29ce0 20 67 69 76 65 6e 20 62 79 20 74 68 65 20 34 74   given by the 4t
29cf0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  h parameter to s
29d00 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
29d10 74 61 28 29 20 6f 6e 0a 2a 2a 20 74 68 65 20 6d  ta() on.** the m
29d20 65 74 61 64 61 74 61 20 77 68 65 6e 20 74 68 65  etadata when the
29d30 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 66   corresponding f
29d40 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65  unction paramete
29d50 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 6f 72 20  r changes.** or 
29d60 77 68 65 6e 20 74 68 65 20 53 51 4c 20 73 74 61  when the SQL sta
29d70 74 65 6d 65 6e 74 20 63 6f 6d 70 6c 65 74 65 73  tement completes
29d80 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65  , whichever come
29d90 73 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 53  s first..**.** S
29da0 51 4c 69 74 65 20 69 73 20 66 72 65 65 20 74 6f  QLite is free to
29db0 20 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75   call the destru
29dc0 63 74 6f 72 20 61 6e 64 20 64 72 6f 70 20 6d 65  ctor and drop me
29dd0 74 61 64 61 74 61 20 6f 6e 20 61 6e 79 0a 2a 2a  tadata on any.**
29de0 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 61 6e   parameter of an
29df0 79 20 66 75 6e 63 74 69 6f 6e 20 61 74 20 61 6e  y function at an
29e00 79 20 74 69 6d 65 2e 20 20 54 68 65 20 6f 6e 6c  y time.  The onl
29e10 79 20 67 75 61 72 61 6e 74 65 65 20 69 73 20 74  y guarantee is t
29e20 68 61 74 0a 2a 2a 20 74 68 65 20 64 65 73 74 72  hat.** the destr
29e30 75 63 74 6f 72 20 77 69 6c 6c 20 62 65 20 63 61  uctor will be ca
29e40 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  lled before the 
29e50 6d 65 74 61 64 61 74 61 20 69 73 20 64 72 6f 70  metadata is drop
29e60 70 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72  ped..**.** In pr
29e70 61 63 74 69 63 65 2c 20 6d 65 74 61 64 61 74 61  actice, metadata
29e80 20 69 73 20 70 72 65 73 65 72 76 65 64 20 62 65   is preserved be
29e90 74 77 65 65 6e 20 66 75 6e 63 74 69 6f 6e 20 63  tween function c
29ea0 61 6c 6c 73 20 66 6f 72 0a 2a 2a 20 65 78 70 72  alls for.** expr
29eb0 65 73 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65  essions that are
29ec0 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d   constant at com
29ed0 70 69 6c 65 20 74 69 6d 65 2e 20 54 68 69 73 20  pile time. This 
29ee0 69 6e 63 6c 75 64 65 73 20 6c 69 74 65 72 61 6c  includes literal
29ef0 0a 2a 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 53  .** values and S
29f00 51 4c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a  QL variables..**
29f10 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
29f20 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  es must be calle
29f30 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20  d from the same 
29f40 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a  thread in which.
29f50 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  ** the SQL funct
29f60 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a  ion is running..
29f70 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
29f80 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 37 32 5d 20  ts:.** [H16272] 
29f90 5b 48 31 36 32 37 34 5d 20 5b 48 31 36 32 37 36  [H16274] [H16276
29fa0 5d 20 5b 48 31 36 32 37 37 5d 20 5b 48 31 36 32  ] [H16277] [H162
29fb0 37 38 5d 20 5b 48 31 36 32 37 39 5d 0a 2a 2f 0a  78] [H16279].*/.
29fc0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
29fd0 2a 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78  *sqlite3_get_aux
29fe0 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e  data(sqlite3_con
29ff0 74 65 78 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53  text*, int N);.S
2a000 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2a010 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
2a020 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
2a030 78 74 2a 2c 20 69 6e 74 20 4e 2c 20 76 6f 69 64  xt*, int N, void
2a040 2a 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64  *, void (*)(void
2a050 2a 29 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  *));.../*.** CAP
2a060 49 33 52 45 46 3a 20 43 6f 6e 73 74 61 6e 74 73  I3REF: Constants
2a070 20 44 65 66 69 6e 69 6e 67 20 53 70 65 63 69 61   Defining Specia
2a080 6c 20 44 65 73 74 72 75 63 74 6f 72 20 42 65 68  l Destructor Beh
2a090 61 76 69 6f 72 20 7b 48 31 30 32 38 30 7d 20 3c  avior {H10280} <
2a0a0 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30100>.**.** Th
2a0b0 65 73 65 20 61 72 65 20 73 70 65 63 69 61 6c 20  ese are special 
2a0c0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 64  values for the d
2a0d0 65 73 74 72 75 63 74 6f 72 20 74 68 61 74 20 69  estructor that i
2a0e0 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 74  s passed in as t
2a0f0 68 65 0a 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75  he.** final argu
2a100 6d 65 6e 74 20 74 6f 20 72 6f 75 74 69 6e 65 73  ment to routines
2a110 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72   like [sqlite3_r
2a120 65 73 75 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20  esult_blob()].  
2a130 49 66 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  If the destructo
2a140 72 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73  r.** argument is
2a150 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20   SQLITE_STATIC, 
2a160 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
2a170 65 20 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65  e content pointe
2a180 72 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a  r is constant.**
2a190 20 61 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20   and will never 
2a1a0 63 68 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73  change.  It does
2a1b0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
2a1c0 64 65 73 74 72 6f 79 65 64 2e 20 20 54 68 65 0a  destroyed.  The.
2a1d0 2a 2a 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  ** SQLITE_TRANSI
2a1e0 45 4e 54 20 76 61 6c 75 65 20 6d 65 61 6e 73 20  ENT value means 
2a1f0 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
2a200 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 68 61   will likely cha
2a210 6e 67 65 20 69 6e 0a 2a 2a 20 74 68 65 20 6e 65  nge in.** the ne
2a220 61 72 20 66 75 74 75 72 65 20 61 6e 64 20 74 68  ar future and th
2a230 61 74 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64  at SQLite should
2a240 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72   make its own pr
2a250 69 76 61 74 65 20 63 6f 70 79 20 6f 66 0a 2a 2a  ivate copy of.**
2a260 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 66   the content bef
2a270 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
2a280 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 64 65 66  *.** The typedef
2a290 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f   is necessary to
2a2a0 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 70 72 6f   work around pro
2a2b0 62 6c 65 6d 73 20 69 6e 20 63 65 72 74 61 69 6e  blems in certain
2a2c0 0a 2a 2a 20 43 2b 2b 20 63 6f 6d 70 69 6c 65 72  .** C++ compiler
2a2d0 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23  s.  See ticket #
2a2e0 32 31 39 31 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  2191..*/.typedef
2a2f0 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 5f   void (*sqlite3_
2a300 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29  destructor_type)
2a310 28 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65  (void*);.#define
2a320 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 20   SQLITE_STATIC  
2a330 20 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65      ((sqlite3_de
2a340 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 30 29  structor_type)0)
2a350 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
2a360 54 52 41 4e 53 49 45 4e 54 20 20 20 28 28 73 71  TRANSIENT   ((sq
2a370 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72  lite3_destructor
2a380 5f 74 79 70 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a  _type)-1)../*.**
2a390 20 43 41 50 49 33 52 45 46 3a 20 53 65 74 74 69   CAPI3REF: Setti
2a3a0 6e 67 20 54 68 65 20 52 65 73 75 6c 74 20 4f 66  ng The Result Of
2a3b0 20 41 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e   An SQL Function
2a3c0 20 7b 48 31 36 34 30 30 7d 20 3c 53 32 30 32 30   {H16400} <S2020
2a3d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
2a3e0 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
2a3f0 20 62 79 20 74 68 65 20 78 46 75 6e 63 20 6f 72   by the xFunc or
2a400 20 78 46 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b   xFinal callback
2a410 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d  s that.** implem
2a420 65 6e 74 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ent SQL function
2a430 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73  s and aggregates
2a440 2e 20 20 53 65 65 0a 2a 2a 20 5b 73 71 6c 69 74  .  See.** [sqlit
2a450 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2a460 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  on()] and [sqlit
2a470 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
2a480 6f 6e 31 36 28 29 5d 0a 2a 2a 20 66 6f 72 20 61  on16()].** for a
2a490 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2a4a0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
2a4b0 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 77 6f 72  se functions wor
2a4c0 6b 20 76 65 72 79 20 6d 75 63 68 20 6c 69 6b 65  k very much like
2a4d0 20 74 68 65 20 5b 70 61 72 61 6d 65 74 65 72 20   the [parameter 
2a4e0 62 69 6e 64 69 6e 67 5d 20 66 61 6d 69 6c 79 20  binding] family 
2a4f0 6f 66 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  of.** functions 
2a500 75 73 65 64 20 74 6f 20 62 69 6e 64 20 76 61 6c  used to bind val
2a510 75 65 73 20 74 6f 20 68 6f 73 74 20 70 61 72 61  ues to host para
2a520 6d 65 74 65 72 73 20 69 6e 20 70 72 65 70 61 72  meters in prepar
2a530 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  ed statements..*
2a540 2a 20 52 65 66 65 72 20 74 6f 20 74 68 65 20 5b  * Refer to the [
2a550 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 64  SQL parameter] d
2a560 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  ocumentation for
2a570 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
2a580 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  rmation..**.** T
2a590 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2a5a0 74 5f 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61  t_blob() interfa
2a5b0 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75  ce sets the resu
2a5c0 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70  lt from.** an ap
2a5d0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2a5e0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
2a5f0 20 74 68 65 20 42 4c 4f 42 20 77 68 6f 73 65 20   the BLOB whose 
2a600 63 6f 6e 74 65 6e 74 20 69 73 20 70 6f 69 6e 74  content is point
2a610 65 64 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20  ed.** to by the 
2a620 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
2a630 20 61 6e 64 20 77 68 69 63 68 20 69 73 20 4e 20   and which is N 
2a640 62 79 74 65 73 20 6c 6f 6e 67 20 77 68 65 72 65  bytes long where
2a650 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 69   N is the.** thi
2a660 72 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  rd parameter..**
2a670 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2a680 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28  result_zeroblob(
2a690 29 20 69 6e 74 65 72 66 61 63 65 73 20 73 65 74  ) interfaces set
2a6a0 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a   the result of.*
2a6b0 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
2a6c0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
2a6d0 6f 6e 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20  on to be a BLOB 
2a6e0 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a  containing all z
2a6f0 65 72 6f 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64  ero.** bytes and
2a700 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65   N bytes in size
2a710 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
2a720 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 32 6e   value of the 2n
2a730 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a  d parameter..**.
2a740 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2a750 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 29 20 69  esult_double() i
2a760 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68  nterface sets th
2a770 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a  e result from.**
2a780 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   an application-
2a790 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2a7a0 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e   to be a floatin
2a7b0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 73 70  g point value sp
2a7c0 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 69 74  ecified.** by it
2a7d0 73 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a  s 2nd argument..
2a7e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2a7f0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
2a800 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73   and sqlite3_res
2a810 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 66 75  ult_error16() fu
2a820 6e 63 74 69 6f 6e 73 0a 2a 2a 20 63 61 75 73 65  nctions.** cause
2a830 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   the implemented
2a840 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 6f   SQL function to
2a850 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74   throw an except
2a860 69 6f 6e 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 75  ion..** SQLite u
2a870 73 65 73 20 74 68 65 20 73 74 72 69 6e 67 20 70  ses the string p
2a880 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65  ointed to by the
2a890 0a 2a 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65  .** 2nd paramete
2a8a0 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 72 65 73  r of sqlite3_res
2a8b0 75 6c 74 5f 65 72 72 6f 72 28 29 20 6f 72 20 73  ult_error() or s
2a8c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2a8d0 72 6f 72 31 36 28 29 0a 2a 2a 20 61 73 20 74 68  ror16().** as th
2a8e0 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72  e text of an err
2a8f0 6f 72 20 6d 65 73 73 61 67 65 2e 20 20 53 51 4c  or message.  SQL
2a900 69 74 65 20 69 6e 74 65 72 70 72 65 74 73 20 74  ite interprets t
2a910 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73  he error.** mess
2a920 61 67 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20  age string from 
2a930 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2a940 72 72 6f 72 28 29 20 61 73 20 55 54 46 2d 38 2e  rror() as UTF-8.
2a950 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72   SQLite.** inter
2a960 70 72 65 74 73 20 74 68 65 20 73 74 72 69 6e 67  prets the string
2a970 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65   from sqlite3_re
2a980 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 61  sult_error16() a
2a990 73 20 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69  s UTF-16 in nati
2a9a0 76 65 0a 2a 2a 20 62 79 74 65 20 6f 72 64 65 72  ve.** byte order
2a9b0 2e 20 20 49 66 20 74 68 65 20 74 68 69 72 64 20  .  If the third 
2a9c0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
2a9d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2a9e0 72 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65  r().** or sqlite
2a9f0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
2aa00 28 29 20 69 73 20 6e 65 67 61 74 69 76 65 20 74  () is negative t
2aa10 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73  hen SQLite takes
2aa20 20 61 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a   as the error.**
2aa30 20 6d 65 73 73 61 67 65 20 61 6c 6c 20 74 65 78   message all tex
2aa40 74 20 75 70 20 74 68 72 6f 75 67 68 20 74 68 65  t up through the
2aa50 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72   first zero char
2aa60 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65  acter..** If the
2aa70 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
2aa80 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75   to sqlite3_resu
2aa90 6c 74 5f 65 72 72 6f 72 28 29 20 6f 72 0a 2a 2a  lt_error() or.**
2aaa0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2aab0 65 72 72 6f 72 31 36 28 29 20 69 73 20 6e 6f 6e  error16() is non
2aac0 2d 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53  -negative then S
2aad0 51 4c 69 74 65 20 74 61 6b 65 73 20 74 68 61 74  QLite takes that
2aae0 20 6d 61 6e 79 0a 2a 2a 20 62 79 74 65 73 20 28   many.** bytes (
2aaf0 6e 6f 74 20 63 68 61 72 61 63 74 65 72 73 29 20  not characters) 
2ab00 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72  from the 2nd par
2ab10 61 6d 65 74 65 72 20 61 73 20 74 68 65 20 65 72  ameter as the er
2ab20 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 20  ror message..** 
2ab30 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2ab40 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73  lt_error() and s
2ab50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2ab60 72 6f 72 31 36 28 29 0a 2a 2a 20 72 6f 75 74 69  ror16().** routi
2ab70 6e 65 73 20 6d 61 6b 65 20 61 20 70 72 69 76 61  nes make a priva
2ab80 74 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65  te copy of the e
2ab90 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78  rror message tex
2aba0 74 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 79  t before.** they
2abb0 20 72 65 74 75 72 6e 2e 20 20 48 65 6e 63 65 2c   return.  Hence,
2abc0 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   the calling fun
2abd0 63 74 69 6f 6e 20 63 61 6e 20 64 65 61 6c 6c 6f  ction can deallo
2abe0 63 61 74 65 20 6f 72 0a 2a 2a 20 6d 6f 64 69 66  cate or.** modif
2abf0 79 20 74 68 65 20 74 65 78 74 20 61 66 74 65 72  y the text after
2ac00 20 74 68 65 79 20 72 65 74 75 72 6e 20 77 69 74   they return wit
2ac10 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 54 68  hout harm..** Th
2ac20 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2ac30 5f 65 72 72 6f 72 5f 63 6f 64 65 28 29 20 66 75  _error_code() fu
2ac40 6e 63 74 69 6f 6e 20 63 68 61 6e 67 65 73 20 74  nction changes t
2ac50 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a  he error code.**
2ac60 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c   returned by SQL
2ac70 69 74 65 20 61 73 20 61 20 72 65 73 75 6c 74 20  ite as a result 
2ac80 6f 66 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 61  of an error in a
2ac90 20 66 75 6e 63 74 69 6f 6e 2e 20 20 42 79 20 64   function.  By d
2aca0 65 66 61 75 6c 74 2c 0a 2a 2a 20 74 68 65 20 65  efault,.** the e
2acb0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 53 51 4c  rror code is SQL
2acc0 49 54 45 5f 45 52 52 4f 52 2e 20 20 41 20 73 75  ITE_ERROR.  A su
2acd0 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f  bsequent call to
2ace0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2acf0 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71  error().** or sq
2ad00 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2ad10 6f 72 31 36 28 29 20 72 65 73 65 74 73 20 74 68  or16() resets th
2ad20 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20  e error code to 
2ad30 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a  SQLITE_ERROR..**
2ad40 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2ad50 72 65 73 75 6c 74 5f 74 6f 6f 62 69 67 28 29 20  result_toobig() 
2ad60 69 6e 74 65 72 66 61 63 65 20 63 61 75 73 65 73  interface causes
2ad70 20 53 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77   SQLite to throw
2ad80 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64   an error.** ind
2ad90 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 20 73  icating that a s
2ada0 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 69 73  tring or BLOB is
2adb0 20 74 6f 20 6c 6f 6e 67 20 74 6f 20 72 65 70 72   to long to repr
2adc0 65 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  esent..**.** The
2add0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ade0 6e 6f 6d 65 6d 28 29 20 69 6e 74 65 72 66 61 63  nomem() interfac
2adf0 65 20 63 61 75 73 65 73 20 53 51 4c 69 74 65 20  e causes SQLite 
2ae00 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f  to throw an erro
2ae10 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20  r.** indicating 
2ae20 74 68 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c  that a memory al
2ae30 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2e  location failed.
2ae40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2ae50 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 29 20  e3_result_int() 
2ae60 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74  interface sets t
2ae70 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
2ae80 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63  ** of the applic
2ae90 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2aea0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65  nction to be the
2aeb0 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69   32-bit signed i
2aec0 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20  nteger.** value 
2aed0 67 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64  given in the 2nd
2aee0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68   argument..** Th
2aef0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2af00 5f 69 6e 74 36 34 28 29 20 69 6e 74 65 72 66 61  _int64() interfa
2af10 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 75  ce sets the retu
2af20 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74  rn value.** of t
2af30 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
2af40 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
2af50 74 6f 20 62 65 20 74 68 65 20 36 34 2d 62 69 74  to be the 64-bit
2af60 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a   signed integer.
2af70 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e 20 69  ** value given i
2af80 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75 6d 65  n the 2nd argume
2af90 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  nt..**.** The sq
2afa0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
2afb0 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  l() interface se
2afc0 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ts the return va
2afd0 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70  lue.** of the ap
2afe0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2aff0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
2b000 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   NULL..**.** The
2b010 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2b020 74 65 78 74 28 29 2c 20 73 71 6c 69 74 65 33 5f  text(), sqlite3_
2b030 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 29 2c  result_text16(),
2b040 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75  .** sqlite3_resu
2b050 6c 74 5f 74 65 78 74 31 36 6c 65 28 29 2c 20 61  lt_text16le(), a
2b060 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  nd sqlite3_resul
2b070 74 5f 74 65 78 74 31 36 62 65 28 29 20 69 6e 74  t_text16be() int
2b080 65 72 66 61 63 65 73 0a 2a 2a 20 73 65 74 20 74  erfaces.** set t
2b090 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
2b0a0 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  of the applicati
2b0b0 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
2b0c0 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 61 20 74  ion to be.** a t
2b0d0 65 78 74 20 73 74 72 69 6e 67 20 77 68 69 63 68  ext string which
2b0e0 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20   is represented 
2b0f0 61 73 20 55 54 46 2d 38 2c 20 55 54 46 2d 31 36  as UTF-8, UTF-16
2b100 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
2b110 65 72 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69  er,.** UTF-16 li
2b120 74 74 6c 65 20 65 6e 64 69 61 6e 2c 20 6f 72 20  ttle endian, or 
2b130 55 54 46 2d 31 36 20 62 69 67 20 65 6e 64 69 61  UTF-16 big endia
2b140 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
2b150 0a 2a 2a 20 53 51 4c 69 74 65 20 74 61 6b 65 73  .** SQLite takes
2b160 20 74 68 65 20 74 65 78 74 20 72 65 73 75 6c 74   the text result
2b170 20 66 72 6f 6d 20 74 68 65 20 61 70 70 6c 69 63   from the applic
2b180 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68  ation from.** th
2b190 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
2b1a0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  of the sqlite3_r
2b1b0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65  esult_text* inte
2b1c0 72 66 61 63 65 73 2e 0a 2a 2a 20 49 66 20 74 68  rfaces..** If th
2b1d0 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
2b1e0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  to the sqlite3_r
2b1f0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65  esult_text* inte
2b200 72 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 65 67  rfaces.** is neg
2b210 61 74 69 76 65 2c 20 74 68 65 6e 20 53 51 4c 69  ative, then SQLi
2b220 74 65 20 74 61 6b 65 73 20 72 65 73 75 6c 74 20  te takes result 
2b230 74 65 78 74 20 66 72 6f 6d 20 74 68 65 20 32 6e  text from the 2n
2b240 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74  d parameter.** t
2b250 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73 74  hrough the first
2b260 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e   zero character.
2b270 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 70  .** If the 3rd p
2b280 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
2b290 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2b2a0 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a  ext* interfaces.
2b2b0 2a 2a 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69  ** is non-negati
2b2c0 76 65 2c 20 74 68 65 6e 20 61 73 20 6d 61 6e 79  ve, then as many
2b2d0 20 62 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72   bytes (not char
2b2e0 61 63 74 65 72 73 29 20 6f 66 20 74 68 65 20 74  acters) of the t
2b2f0 65 78 74 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74  ext.** pointed t
2b300 6f 20 62 79 20 74 68 65 20 32 6e 64 20 70 61 72  o by the 2nd par
2b310 61 6d 65 74 65 72 20 61 72 65 20 74 61 6b 65 6e  ameter are taken
2b320 20 61 73 20 74 68 65 20 61 70 70 6c 69 63 61 74   as the applicat
2b330 69 6f 6e 2d 64 65 66 69 6e 65 64 0a 2a 2a 20 66  ion-defined.** f
2b340 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a  unction result..
2b350 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61  ** If the 4th pa
2b360 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73  rameter to the s
2b370 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2b380 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a  xt* interfaces.*
2b390 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73  * or sqlite3_res
2b3a0 75 6c 74 5f 62 6c 6f 62 20 69 73 20 61 20 6e 6f  ult_blob is a no
2b3b0 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  n-NULL pointer, 
2b3c0 74 68 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c  then SQLite call
2b3d0 73 20 74 68 61 74 0a 2a 2a 20 66 75 6e 63 74 69  s that.** functi
2b3e0 6f 6e 20 61 73 20 74 68 65 20 64 65 73 74 72 75  on as the destru
2b3f0 63 74 6f 72 20 6f 6e 20 74 68 65 20 74 65 78 74  ctor on the text
2b400 20 6f 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20   or BLOB result 
2b410 77 68 65 6e 20 69 74 20 68 61 73 0a 2a 2a 20 66  when it has.** f
2b420 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68  inished using th
2b430 61 74 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66  at result..** If
2b440 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74   the 4th paramet
2b450 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  er to the sqlite
2b460 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69  3_result_text* i
2b470 6e 74 65 72 66 61 63 65 73 20 6f 72 0a 2a 2a 20  nterfaces or.** 
2b480 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
2b490 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65 63 69  lob is the speci
2b4a0 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49  al constant SQLI
2b4b0 54 45 5f 53 54 41 54 49 43 2c 20 74 68 65 6e 20  TE_STATIC, then 
2b4c0 53 51 4c 69 74 65 0a 2a 2a 20 61 73 73 75 6d 65  SQLite.** assume
2b4d0 73 20 74 68 61 74 20 74 68 65 20 74 65 78 74 20  s that the text 
2b4e0 6f 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 69  or BLOB result i
2b4f0 73 20 69 6e 20 63 6f 6e 73 74 61 6e 74 20 73 70  s in constant sp
2b500 61 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74  ace and does not
2b510 0a 2a 2a 20 63 6f 70 79 20 74 68 65 20 69 74 20  .** copy the it 
2b520 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75  or call a destru
2b530 63 74 6f 72 20 77 68 65 6e 20 69 74 20 68 61 73  ctor when it has
2b540 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20   finished using 
2b550 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a 2a 20  that result..** 
2b560 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61 6d  If the 4th param
2b570 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69  eter to the sqli
2b580 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a  te3_result_text*
2b590 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f   interfaces.** o
2b5a0 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  r sqlite3_result
2b5b0 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65  _blob is the spe
2b5c0 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51  cial constant SQ
2b5d0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 0a 2a  LITE_TRANSIENT.*
2b5e0 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61  * then SQLite ma
2b5f0 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68  kes a copy of th
2b600 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 73 70  e result into sp
2b610 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ace obtained fro
2b620 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74  m.** from [sqlit
2b630 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 65 66  e3_malloc()] bef
2b640 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73 2e 0a  ore it returns..
2b650 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2b660 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29  3_result_value()
2b670 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20   interface sets 
2b680 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
2b690 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2b6a0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2b6b0 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79 20 74  n to be a copy t
2b6c0 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74  he.** [unprotect
2b6d0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2b6e0 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69 66 69  ] object specifi
2b6f0 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 70 61  ed by the 2nd pa
2b700 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a 2a 2a  rameter.  The.**
2b710 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2b720 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61 63  value() interfac
2b730 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f  e makes a copy o
2b740 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  f the [sqlite3_v
2b750 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68 61 74  alue].** so that
2b760 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   the [sqlite3_va
2b770 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64 20 69  lue] specified i
2b780 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  n the parameter 
2b790 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a  may change or.**
2b7a0 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20   be deallocated 
2b7b0 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 72 65  after sqlite3_re
2b7c0 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 65 74  sult_value() ret
2b7d0 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68 61 72  urns without har
2b7e0 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65 63 74  m..** A [protect
2b7f0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2b800 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 6c 77  ] object may alw
2b810 61 79 73 20 62 65 20 75 73 65 64 20 77 68 65 72  ays be used wher
2b820 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65  e an.** [unprote
2b830 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2b840 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20 72 65  ue] object is re
2b850 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74 68 65  quired, so eithe
2b860 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b 73 71  r.** kind of [sq
2b870 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
2b880 65 63 74 20 63 61 6e 20 62 65 20 75 73 65 64 20  ect can be used 
2b890 77 69 74 68 20 74 68 69 73 20 69 6e 74 65 72 66  with this interf
2b8a0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ace..**.** If th
2b8b0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
2b8c0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
2b8d0 68 69 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e  hin the differen
2b8e0 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68 61 6e  t thread.** than
2b8f0 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61 69 6e   the one contain
2b900 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63 61 74  ing the applicat
2b910 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2b920 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 69 76  tion that receiv
2b930 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74  ed.** the [sqlit
2b940 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f 69 6e  e3_context] poin
2b950 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c 74 73  ter, the results
2b960 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a   are undefined..
2b970 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
2b980 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33 5d 20  ts:.** [H16403] 
2b990 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34 30 39  [H16406] [H16409
2b9a0 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31 36 34  ] [H16412] [H164
2b9b0 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b 48 31  15] [H16418] [H1
2b9c0 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d 0a 2a  6421] [H16424].*
2b9d0 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31 36 34  * [H16427] [H164
2b9e0 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b 48 31  30] [H16433] [H1
2b9f0 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d 20 5b  6436] [H16439] [
2ba00 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34 35 5d  H16442] [H16445]
2ba10 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b 48 31   [H16448].** [H1
2ba20 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d 20 5b  6451] [H16454] [
2ba30 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36 30 5d  H16457] [H16460]
2ba40 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53 51 4c   [H16463].*/.SQL
2ba50 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2ba60 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
2ba70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2ba80 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
2ba90 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
2baa0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
2bab0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2bac0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69  sult_double(sqli
2bad0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64 6f  te3_context*, do
2bae0 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50  uble);.SQLITE_AP
2baf0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2bb00 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69  esult_error(sqli
2bb10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
2bb20 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  nst char*, int);
2bb30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2bb40 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2bb50 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f  error16(sqlite3_
2bb60 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  context*, const 
2bb70 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  void*, int);.SQL
2bb80 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2bb90 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2bba0 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33  r_toobig(sqlite3
2bbb0 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  _context*);.SQLI
2bbc0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2bbd0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2bbe0 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63  _nomem(sqlite3_c
2bbf0 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45  ontext*);.SQLITE
2bc00 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2bc10 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
2bc20 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ode(sqlite3_cont
2bc30 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ext*, int);.SQLI
2bc40 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2bc50 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73  te3_result_int(s
2bc60 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2bc70 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50   int);.SQLITE_AP
2bc80 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2bc90 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69  esult_int64(sqli
2bca0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71  te3_context*, sq
2bcb0 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51  lite3_int64);.SQ
2bcc0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2bcd0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
2bce0 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  l(sqlite3_contex
2bcf0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  t*);.SQLITE_API 
2bd00 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2bd10 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65 33  ult_text(sqlite3
2bd20 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
2bd30 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f 69   char*, int, voi
2bd40 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
2bd50 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2bd60 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2bd70 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  t16(sqlite3_cont
2bd80 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ext*, const void
2bd90 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28  *, int, void(*)(
2bda0 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  void*));.SQLITE_
2bdb0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2bdc0 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
2bdd0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2bde0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
2bdf0 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
2be00 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  *));.SQLITE_API 
2be10 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2be20 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71 6c  ult_text16be(sql
2be30 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
2be40 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c  onst void*, int,
2be50 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
2be60 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2be70 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2be80 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f  value(sqlite3_co
2be90 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f  ntext*, sqlite3_
2bea0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2beb0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2bec0 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
2bed0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2bee0 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a  *, int n);../*.*
2bef0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66 69  * CAPI3REF: Defi
2bf00 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e 67  ne New Collating
2bf10 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36 36   Sequences {H166
2bf20 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a  00} <S20300>.**.
2bf30 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f  ** These functio
2bf40 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  ns are used to a
2bf50 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e  dd new collation
2bf60 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74 68   sequences to th
2bf70 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
2bf80 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69  onnection] speci
2bf90 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72 73  fied as the firs
2bfa0 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  t argument..**.*
2bfb0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
2bfc0 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20  e new collation 
2bfd0 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65 63  sequence is spec
2bfe0 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d 38  ified as a UTF-8
2bff0 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 73   string.** for s
2c000 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2c010 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73 71  llation() and sq
2c020 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2c030 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20 61  lation_v2().** a
2c040 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72 69  nd a UTF-16 stri
2c050 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63  ng for sqlite3_c
2c060 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
2c070 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73 65  6(). In all case
2c080 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69 73  s.** the name is
2c090 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
2c0a0 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61  econd function a
2c0b0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
2c0c0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
2c0d0 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20  t may be one of 
2c0e0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b 53  the constants [S
2c0f0 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a 20  QLITE_UTF8],.** 
2c100 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d  [SQLITE_UTF16LE]
2c110 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46  , or [SQLITE_UTF
2c120 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e  16BE], indicatin
2c130 67 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d  g that the user-
2c140 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74  supplied.** rout
2c150 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62  ine expects to b
2c160 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72  e passed pointer
2c170 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63  s to strings enc
2c180 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38  oded using UTF-8
2c190 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74  ,.** UTF-16 litt
2c1a0 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54  le-endian, or UT
2c1b0 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c  F-16 big-endian,
2c1c0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54   respectively. T
2c1d0 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75  he.** third argu
2c1e0 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20  ment might also 
2c1f0 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  be [SQLITE_UTF16
2c200 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  ] to indicate th
2c210 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65 0a 2a  at the routine.*
2c220 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 65  * expects pointe
2c230 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31 36 20  rs to be UTF-16 
2c240 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20 6e  strings in the n
2c250 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
2c260 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72 67 75  , or the.** argu
2c270 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53 51 4c  ment can be [SQL
2c280 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45  ITE_UTF16_ALIGNE
2c290 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74 68 65  D] if the.** the
2c2a0 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73   routine expects
2c2b0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36 2d   pointers to 16-
2c2c0 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65 64  bit word aligned
2c2d0 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20 55   strings.** of U
2c2e0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74  TF-16 in the nat
2c2f0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e 0a  ive byte order..
2c300 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  **.** A pointer 
2c310 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 70 70  to the user supp
2c320 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d 75 73  lied routine mus
2c330 74 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74  t be passed as t
2c340 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 67 75  he fifth.** argu
2c350 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 73 20  ment.  If it is 
2c360 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 74 68  NULL, this is th
2c370 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 74 69  e same as deleti
2c380 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ng the collation
2c390 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 73 6f  .** sequence (so
2c3a0 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e   that SQLite can
2c3b0 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e 79 6d  not call it anym
2c3c0 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 74 69  ore)..** Each ti
2c3d0 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  me the applicati
2c3e0 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 6e 63  on supplied func
2c3f0 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 2c  tion is invoked,
2c400 20 69 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a   it is passed.**
2c410 20 61 73 20 69 74 73 20 66 69 72 73 74 20 70 61   as its first pa
2c420 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 20 6f  rameter a copy o
2c430 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 73 73  f the void* pass
2c440 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74 68  ed as the fourth
2c450 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
2c460 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2c470 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 73 71  ollation() or sq
2c480 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2c490 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a  lation16()..**.*
2c4a0 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20  * The remaining 
2c4b0 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
2c4c0 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70   application-sup
2c4d0 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 61 72  plied routine ar
2c4e0 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c 0a 2a  e two strings,.*
2c4f0 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 6e 74  * each represent
2c500 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 68 2c  ed by a (length,
2c510 20 64 61 74 61 29 20 70 61 69 72 20 61 6e 64 20   data) pair and 
2c520 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 65  encoded in the e
2c530 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 74 20  ncoding.** that 
2c540 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74 68  was passed as th
2c550 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
2c560 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74   when the collat
2c570 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 61 73  ion sequence was
2c580 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 2e 20  .** registered. 
2c590 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 6c 69  {END}  The appli
2c5a0 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 63  cation defined c
2c5b0 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  ollation routine
2c5c0 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72   should.** retur
2c5d0 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f  n negative, zero
2c5e0 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20   or positive if 
2c5f0 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
2c600 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 0a 2a   is less than,.*
2c610 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  * equal to, or g
2c620 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
2c630 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e 20 69  second string. i
2c640 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d 20 53  .e. (STRING1 - S
2c650 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a 20 54  TRING2)..**.** T
2c660 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  he sqlite3_creat
2c670 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29  e_collation_v2()
2c680 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69   works like sqli
2c690 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2c6a0 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 70 74  tion().** except
2c6b0 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61   that it takes a
2c6c0 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74  n extra argument
2c6d0 20 77 68 69 63 68 20 69 73 20 61 20 64 65 73 74   which is a dest
2c6e0 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 74 68  ructor for.** th
2c6f0 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 54 68  e collation.  Th
2c700 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 73 20  e destructor is 
2c710 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
2c720 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a 2a 20  collation is.** 
2c730 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 69 73  destroyed and is
2c740 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 20 6f   passed a copy o
2c750 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72  f the fourth par
2c760 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 6f 69  ameter void* poi
2c770 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 73  nter.** of the s
2c780 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2c790 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a 2a 2a  llation_v2()..**
2c7a0 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 65 20   Collations are 
2c7b0 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e 20 74  destroyed when t
2c7c0 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 64 64  hey are overridd
2c7d0 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 6c 6c  en by later call
2c7e0 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c  s to the.** coll
2c7f0 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 66  ation creation f
2c800 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 65 6e  unctions or when
2c810 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
2c820 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 63 6c  onnection] is cl
2c830 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 5b 73  osed.** using [s
2c840 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e  qlite3_close()].
2c850 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
2c860 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61    [sqlite3_colla
2c870 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d 20 61  tion_needed()] a
2c880 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  nd [sqlite3_coll
2c890 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 29  ation_needed16()
2c8a0 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
2c8b0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 36 30  ments:.** [H1660
2c8c0 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48 31 36  3] [H16604] [H16
2c8d0 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20 5b 48  606] [H16609] [H
2c8e0 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35 5d 20  16612] [H16615] 
2c8f0 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36 32 31  [H16618] [H16621
2c900 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20 5b 48  ].** [H16624] [H
2c910 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30 5d 0a  16627] [H16630].
2c920 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2c930 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
2c940 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71  _collation(.  sq
2c950 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74  lite3*, .  const
2c960 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
2c970 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
2c980 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a    void*,.  int(*
2c990 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
2c9a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
2c9b0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
2c9c0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
2c9d0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
2c9e0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a  e_collation_v2(.
2c9f0 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63    sqlite3*, .  c
2ca00 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2ca10 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  , .  int eTextRe
2ca20 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69  p, .  void*,.  i
2ca30 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
2ca40 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
2ca50 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
2ca60 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44  id*),.  void(*xD
2ca70 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a 29  estroy)(void*).)
2ca80 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
2ca90 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2caa0 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20 73  collation16(.  s
2cab0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73  qlite3*, .  cons
2cac0 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20  t void *zName,. 
2cad0 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a   int eTextRep, .
2cae0 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a    void*,.  int(*
2caf0 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
2cb00 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
2cb10 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
2cb20 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
2cb30 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 4e  REF: Collation N
2cb40 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73 20  eeded Callbacks 
2cb50 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30 30  {H16700} <S20300
2cb60 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64  >.**.** To avoid
2cb70 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69 73   having to regis
2cb80 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69 6f  ter all collatio
2cb90 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66 6f  n sequences befo
2cba0 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a  re a database.**
2cbb0 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61 20   can be used, a 
2cbc0 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b 20  single callback 
2cbd0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
2cbe0 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20  registered with 
2cbf0 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  the.** [database
2cc00 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f 20   connection] to 
2cc10 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65 76  be called whenev
2cc20 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 20  er an undefined 
2cc30 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71  collation.** seq
2cc40 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72 65  uence is require
2cc50 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
2cc60 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67 69  function is regi
2cc70 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68 65  stered using the
2cc80 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
2cc90 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49 2c  on_needed() API,
2cca0 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 70  .** then it is p
2ccb0 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73 20  assed the names 
2ccc0 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c  of undefined col
2ccd0 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
2cce0 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20 65   as strings.** e
2ccf0 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e  ncoded in UTF-8.
2cd00 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71 6c   {H16703} If sql
2cd10 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
2cd20 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73 65  eeded16() is use
2cd30 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20  d,.** the names 
2cd40 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55 54  are passed as UT
2cd50 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65 20  F-16 in machine 
2cd60 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
2cd70 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  r..** A call to 
2cd80 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20  either function 
2cd90 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78 69  replaces any exi
2cda0 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e 0a  sting callback..
2cdb0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  **.** When the c
2cdc0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
2cdd0 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61 72  ed, the first ar
2cde0 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69 73  gument passed is
2cdf0 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74 68   a copy.** of th
2ce00 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2ce10 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t to sqlite3_col
2ce20 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20  lation_needed() 
2ce30 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  or.** sqlite3_co
2ce40 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
2ce50 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ().  The second 
2ce60 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2ce70 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
2ce80 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68 69  ection.  The thi
2ce90 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  rd argument is o
2cea0 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55 54  ne of [SQLITE_UT
2ceb0 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54 46  F8], [SQLITE_UTF
2cec0 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51  16BE],.** or [SQ
2ced0 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20 69  LITE_UTF16LE], i
2cee0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d 6f  ndicating the mo
2cef0 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f 72  st desirable for
2cf00 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  m of the collati
2cf10 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 66  on.** sequence f
2cf20 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 64  unction required
2cf30 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70 61  .  The fourth pa
2cf40 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e  rameter is the n
2cf50 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65  ame of the.** re
2cf60 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  quired collation
2cf70 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a   sequence..**.**
2cf80 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   The callback fu
2cf90 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65  nction should re
2cfa0 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69 72  gister the desir
2cfb0 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73 69  ed collation usi
2cfc0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ng.** [sqlite3_c
2cfd0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28  reate_collation(
2cfe0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  )], [sqlite3_cre
2cff0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28  ate_collation16(
2d000 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  )], or.** [sqlit
2d010 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2d020 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a  ion_v2()]..**.**
2d030 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
2d040 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31 36 37  * [H16702] [H167
2d050 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 2f 0a  04] [H16706].*/.
2d060 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2d070 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2d080 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74  _needed(.  sqlit
2d090 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 20 0a  e3*, .  void*, .
2d0a0 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c    void(*)(void*,
2d0b0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65  sqlite3*,int eTe
2d0c0 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72  xtRep,const char
2d0d0 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  *).);.SQLITE_API
2d0e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
2d0f0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28  lation_needed16(
2d100 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20  .  sqlite3*, .  
2d110 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 2a 29  void*,.  void(*)
2d120 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c  (void*,sqlite3*,
2d130 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e  int eTextRep,con
2d140 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a  st void*).);../*
2d150 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 65 20  .** Specify the 
2d160 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 72 79  key for an encry
2d170 70 74 65 64 20 64 61 74 61 62 61 73 65 2e 20 20  pted database.  
2d180 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  This routine sho
2d190 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64  uld be.** called
2d1a0 20 72 69 67 68 74 20 61 66 74 65 72 20 73 71 6c   right after sql
2d1b0 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2a 0a  ite3_open()..**.
2d1c0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69  ** The code to i
2d1d0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50  mplement this AP
2d1e0 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  I is not availab
2d1f0 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63  le in the public
2d200 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53   release.** of S
2d210 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  QLite..*/.SQLITE
2d220 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2d230 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20  _key(.  sqlite3 
2d240 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2d250 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2d260 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64  se to be rekeyed
2d270 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
2d280 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
2d290 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20       /* The key 
2d2a0 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  */.);../*.** Cha
2d2b0 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e 20 61  nge the key on a
2d2c0 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 2e  n open database.
2d2d0 20 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74    If the current
2d2e0 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74   database is not
2d2f0 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c 20 74  .** encrypted, t
2d300 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
2d310 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20 49 66   encrypt it.  If
2d320 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e 65 77   pNew==0 or nNew
2d330 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 74 61  ==0, the.** data
2d340 62 61 73 65 20 69 73 20 64 65 63 72 79 70 74 65  base is decrypte
2d350 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  d..**.** The cod
2d360 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  e to implement t
2d370 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 20 61  his API is not a
2d380 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20  vailable in the 
2d390 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a  public release.*
2d3a0 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  * of SQLite..*/.
2d3b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2d3c0 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a 20 20  qlite3_rekey(.  
2d3d0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2d3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2d3f0 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 65  * Database to be
2d400 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f   rekeyed */.  co
2d410 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
2d420 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20  int nKey     /* 
2d430 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f 0a 29  The new key */.)
2d440 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2d450 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 63 75  F: Suspend Execu
2d460 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f 72 74  tion For A Short
2d470 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d 20 3c   Time {H10530} <
2d480 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S40410>.**.** Th
2d490 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28  e sqlite3_sleep(
2d4a0 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65  ) function cause
2d4b0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  s the current th
2d4c0 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e 64 20  read to suspend 
2d4d0 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 6f 72  execution.** for
2d4e0 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 6d 62   at least a numb
2d4f0 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e  er of millisecon
2d500 64 73 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  ds specified in 
2d510 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  its parameter..*
2d520 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72  *.** If the oper
2d530 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 6f 65  ating system doe
2d540 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 6c  s not support sl
2d550 65 65 70 20 72 65 71 75 65 73 74 73 20 77 69 74  eep requests wit
2d560 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f 6e 64  h.** millisecond
2d570 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e   time resolution
2d580 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d 65 20  , then the time 
2d590 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 64 20  will be rounded 
2d5a0 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 61  up to.** the nea
2d5b0 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54 68 65  rest second. The
2d5c0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69   number of milli
2d5d0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
2d5e0 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71   actually.** req
2d5f0 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20  uested from the 
2d600 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
2d610 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
2d620 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 6c 65  .** SQLite imple
2d630 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74 65 72  ments this inter
2d640 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e 67 20  face by calling 
2d650 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a 2a 20  the xSleep().** 
2d660 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65  method of the de
2d670 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76  fault [sqlite3_v
2d680 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  fs] object..**.*
2d690 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
2d6a0 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35 33 36  [H10533] [H10536
2d6b0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
2d6c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c 65 65  int sqlite3_slee
2d6d0 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  p(int);../*.** C
2d6e0 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66  API3REF: Name Of
2d6f0 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f 6c 64   The Folder Hold
2d700 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 46 69  ing Temporary Fi
2d710 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c 53 32  les {H10310} <S2
2d720 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  0000>.**.** If t
2d730 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  his global varia
2d740 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f 20 70  ble is made to p
2d750 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67  oint to a string
2d760 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65   which is.** the
2d770 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c 64 65   name of a folde
2d780 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 63 74  r (a.k.a. direct
2d790 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c 20 74  ory), then all t
2d7a0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 0a 2a  emporary files.*
2d7b0 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 51 4c  * created by SQL
2d7c0 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c 61 63  ite will be plac
2d7d0 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 65 63  ed in that direc
2d7e0 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 20 76  tory.  If this v
2d7f0 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20  ariable.** is a 
2d800 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68  NULL pointer, th
2d810 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 6f 72  en SQLite perfor
2d820 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f 72 20  ms a search for 
2d830 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a  an appropriate.*
2d840 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  * temporary file
2d850 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a   directory..**.*
2d860 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65  * It is not safe
2d870 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69   to read or modi
2d880 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  fy this variable
2d890 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e   in more than on
2d8a0 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74 20 61  e.** thread at a
2d8b0 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20 6e 6f   time.  It is no
2d8c0 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 6f  t safe to read o
2d8d0 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76 61  r modify this va
2d8e0 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61 20 5b  riable.** if a [
2d8f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2d900 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 75 73  ion] is being us
2d910 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ed at the same t
2d920 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72 61 74  ime in a separat
2d930 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 20  e.** thread..** 
2d940 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  It is intended t
2d950 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62 6c  hat this variabl
2d960 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a  e be set once.**
2d970 20 61 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63   as part of proc
2d980 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ess initializati
2d990 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e  on and before an
2d9a0 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  y SQLite interfa
2d9b0 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68  ce.** routines h
2d9c0 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  ave been called 
2d9d0 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 76 61  and that this va
2d9e0 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20 75 6e  riable remain un
2d9f0 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65 72 65  changed.** there
2da00 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  after..**.** The
2da10 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   [temp_store_dir
2da20 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 6d  ectory pragma] m
2da30 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73 20 76  ay modify this v
2da40 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 75 73  ariable and caus
2da50 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 6e 74  e.** it to point
2da60 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   to memory obtai
2da70 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ned from [sqlite
2da80 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75 72 74  3_malloc].  Furt
2da90 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 65 20  hermore,.** the 
2daa0 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65  [temp_store_dire
2dab0 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 61 6c  ctory pragma] al
2dac0 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 68 61  ways assumes tha
2dad0 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a 2a 20  t any string.** 
2dae0 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 62  that this variab
2daf0 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20  le points to is 
2db00 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  held in memory o
2db10 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a 2a 2a  btained from .**
2db20 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
2db30 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 6d 61  ] and the pragma
2db40 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 6f 20   may attempt to 
2db50 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f 72 79  free that memory
2db60 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  .** using [sqlit
2db70 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 65 6e  e3_free]..** Hen
2db80 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 72 69  ce, if this vari
2db90 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 65 64  able is modified
2dba0 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 68 65   directly, eithe
2dbb0 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a 2a  r it should be.*
2dbc0 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 20 6d  * made NULL or m
2dbd0 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ade to point to 
2dbe0 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
2dbf0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61  from [sqlite3_ma
2dc00 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c 73 65  lloc].** or else
2dc10 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 20   the use of the 
2dc20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65  [temp_store_dire
2dc30 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 73 68  ctory pragma] sh
2dc40 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 64 2e  ould be avoided.
2dc50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
2dc60 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d  har *sqlite3_tem
2dc70 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a 2f 2a  p_directory;../*
2dc80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65  .** CAPI3REF: Te
2dc90 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f 6d 6d  st For Auto-Comm
2dca0 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33 30 7d  it Mode {H12930}
2dcb0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b 45 59   <S60200>.** KEY
2dcc0 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f 6d 6d  WORDS: {autocomm
2dcd0 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54  it mode}.**.** T
2dce0 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  he sqlite3_get_a
2dcf0 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e 74 65  utocommit() inte
2dd00 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 6e 6f  rface returns no
2dd10 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a 65 72  n-zero or.** zer
2dd20 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 64  o if the given d
2dd30 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2dd40 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f 74 20  on is or is not 
2dd50 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  in autocommit mo
2dd60 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74 69 76  de,.** respectiv
2dd70 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74  ely.  Autocommit
2dd80 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 20 64   mode is on by d
2dd90 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74 6f 63  efault..** Autoc
2dda0 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 64 69  ommit mode is di
2ddb0 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42 45 47  sabled by a [BEG
2ddc0 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  IN] statement..*
2ddd0 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64  * Autocommit mod
2dde0 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65 64 20  e is re-enabled 
2ddf0 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72  by a [COMMIT] or
2de00 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a   [ROLLBACK]..**.
2de10 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20 6b 69  ** If certain ki
2de20 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 6f 63  nds of errors oc
2de30 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65 6d 65  cur on a stateme
2de40 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75 6c 74  nt within a mult
2de50 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74  i-statement.** t
2de60 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72 72 6f  ransaction (erro
2de70 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b 53 51  rs including [SQ
2de80 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53 51 4c  LITE_FULL], [SQL
2de90 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a 20 5b  ITE_IOERR],.** [
2dea0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b  SQLITE_NOMEM], [
2deb0 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 61 6e  SQLITE_BUSY], an
2dec0 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  d [SQLITE_INTERR
2ded0 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65 0a 2a  UPT]) then the.*
2dee0 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69  * transaction mi
2def0 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  ght be rolled ba
2df00 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ck automatically
2df10 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20  .  The only way 
2df20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74 20 77  to.** find out w
2df30 68 65 74 68 65 72 20 53 51 4c 69 74 65 20 61 75  hether SQLite au
2df40 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c  tomatically roll
2df50 65 64 20 62 61 63 6b 20 74 68 65 20 74 72 61 6e  ed back the tran
2df60 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a 2a 2a  saction after.**
2df70 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 6f 20   an error is to 
2df80 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  use this functio
2df90 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74  n..**.** If anot
2dfa0 68 65 72 20 74 68 72 65 61 64 20 63 68 61 6e 67  her thread chang
2dfb0 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d 6d 69  es the autocommi
2dfc0 74 20 73 74 61 74 75 73 20 6f 66 20 74 68 65 20  t status of the 
2dfd0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
2dfe0 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69  ection while thi
2dff0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e  s routine is run
2e000 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72  ning, then the r
2e010 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69  eturn value.** i
2e020 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  s undefined..**.
2e030 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2e040 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32 39 33   [H12931] [H1293
2e050 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48 31 32  2] [H12933] [H12
2e060 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  934].*/.SQLITE_A
2e070 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  PI int sqlite3_g
2e080 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73 71  et_autocommit(sq
2e090 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  lite3*);../*.** 
2e0a0 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 20 54  CAPI3REF: Find T
2e0b0 68 65 20 44 61 74 61 62 61 73 65 20 48 61 6e 64  he Database Hand
2e0c0 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 65 64  le Of A Prepared
2e0d0 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 31   Statement {H131
2e0e0 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a  20} <S60600>.**.
2e0f0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64  ** The sqlite3_d
2e100 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 66 61  b_handle interfa
2e110 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 5b  ce returns the [
2e120 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2e130 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 74  ion] handle.** t
2e140 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 70 61  o which a [prepa
2e150 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 62  red statement] b
2e160 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b 64 61  elongs.  The [da
2e170 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2e180 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62  n].** returned b
2e190 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e  y sqlite3_db_han
2e1a0 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65 20  dle is the same 
2e1b0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
2e1c0 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 74  tion] that was t
2e1d0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
2e1e0 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 71 6c  t.** to the [sql
2e1f0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
2e200 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74 73 20  )] call (or its 
2e210 76 61 72 69 61 6e 74 73 29 20 74 68 61 74 20 77  variants) that w
2e220 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 72  as used to.** cr
2e230 65 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65  eate the stateme
2e240 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  nt in the first 
2e250 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  place..**.** Req
2e260 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33 31  uirements: [H131
2e270 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  23].*/.SQLITE_AP
2e280 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  I sqlite3 *sqlit
2e290 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c  e3_db_handle(sql
2e2a0 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a  ite3_stmt*);../*
2e2b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69  .** CAPI3REF: Fi
2e2c0 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 65 70  nd the next prep
2e2d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 7b  ared statement {
2e2e0 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 30 3e  H13140} <S60600>
2e2f0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
2e300 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
2e310 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
2e320 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 73 74  ext [prepared st
2e330 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 0a 2a  atement] after.*
2e340 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 61 74  * pStmt associat
2e350 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 74  ed with the [dat
2e360 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2e370 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74  ] pDb.  If pStmt
2e380 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 65 6e   is NULL.** then
2e390 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
2e3a0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2e3b0 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 70  r to the first p
2e3c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2e3d0 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  t.** associated 
2e3e0 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
2e3f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62  e connection pDb
2e400 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 72 65  .  If no prepare
2e410 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73  d statement.** s
2e420 61 74 69 73 66 69 65 73 20 74 68 65 20 63 6f 6e  atisfies the con
2e430 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 73 20  ditions of this 
2e440 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 74 75  routine, it retu
2e450 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  rns NULL..**.** 
2e460 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  The [database co
2e470 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e 74 65  nnection] pointe
2e480 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20 74 6f  r D in a call to
2e490 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78  .** [sqlite3_nex
2e4a0 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d 75 73  t_stmt(D,S)] mus
2e4b0 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 6f 70  t refer to an op
2e4c0 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  en database.** c
2e4d0 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e  onnection and in
2e4e0 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75 73 74   particular must
2e4f0 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20 70   not be a NULL p
2e500 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65  ointer..**.** Re
2e510 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 33  quirements: [H13
2e520 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20 5b 48  143] [H13146] [H
2e530 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32 5d 0a  13149] [H13152].
2e540 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
2e550 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69  lite3_stmt *sqli
2e560 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71  te3_next_stmt(sq
2e570 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 6c 69  lite3 *pDb, sqli
2e580 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
2e590 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2e5a0 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 52 6f  F: Commit And Ro
2e5b0 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 61 74  llback Notificat
2e5c0 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48  ion Callbacks {H
2e5d0 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30 3e 0a  12950} <S60400>.
2e5e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2e5f0 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 20  3_commit_hook() 
2e600 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
2e610 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a  ers a callback.*
2e620 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  * function to be
2e630 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65   invoked wheneve
2e640 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  r a transaction 
2e650 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a  is committed..**
2e660 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65   Any callback se
2e670 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20  t by a previous 
2e680 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
2e690 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a  commit_hook().**
2e6a0 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61   for the same da
2e6b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2e6c0 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e  n is overridden.
2e6d0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2e6e0 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20  rollback_hook() 
2e6f0 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
2e700 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a  ers a callback.*
2e710 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  * function to be
2e720 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65   invoked wheneve
2e730 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  r a transaction 
2e740 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a  is committed..**
2e750 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65   Any callback se
2e760 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20  t by a previous 
2e770 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
2e780 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a  commit_hook().**
2e790 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61   for the same da
2e7a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2e7b0 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e  n is overridden.
2e7c0 0a 2a 2a 20 54 68 65 20 70 41 72 67 20 61 72 67  .** The pArg arg
2e7d0 75 6d 65 6e 74 20 69 73 20 70 61 73 73 65 64 20  ument is passed 
2e7e0 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 63  through to the c
2e7f0 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 66 20 74  allback..** If t
2e800 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 61  he callback on a
2e810 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 66 75 6e   commit hook fun
2e820 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 6e 6f  ction returns no
2e830 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20  n-zero,.** then 
2e840 74 68 65 20 63 6f 6d 6d 69 74 20 69 73 20 63 6f  the commit is co
2e850 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20 72  nverted into a r
2e860 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49  ollback..**.** I
2e870 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74 69  f another functi
2e880 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  on was previousl
2e890 79 20 72 65 67 69 73 74 65 72 65 64 2c 20 69 74  y registered, it
2e8a0 73 0a 2a 2a 20 70 41 72 67 20 76 61 6c 75 65 20  s.** pArg value 
2e8b0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 74  is returned.  Ot
2e8c0 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 73 20  herwise NULL is 
2e8d0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
2e8e0 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 6d 70  The callback imp
2e8f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74  lementation must
2e900 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67   not do anything
2e910 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66   that will modif
2e920 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  y.** the databas
2e930 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
2e940 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 63 61  t invoked the ca
2e950 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 20 61 63 74  llback.  Any act
2e960 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66  ions.** to modif
2e970 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  y the database c
2e980 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 62  onnection must b
2e990 65 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  e deferred until
2e9a0 20 61 66 74 65 72 20 74 68 65 0a 2a 2a 20 63 6f   after the.** co
2e9b0 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68 65 20  mpletion of the 
2e9c0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
2e9d0 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67   call that trigg
2e9e0 65 72 65 64 20 74 68 65 20 63 6f 6d 6d 69 74 0a  ered the commit.
2e9f0 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 68  ** or rollback h
2ea00 6f 6f 6b 20 69 6e 20 74 68 65 20 66 69 72 73 74  ook in the first
2ea10 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e 6f 74 65 20   place..** Note 
2ea20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72  that [sqlite3_pr
2ea30 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
2ea40 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
2ea50 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65   both modify the
2ea60 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  ir.** database c
2ea70 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74  onnections for t
2ea80 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d  he meaning of "m
2ea90 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70  odify" in this p
2eaa0 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20  aragraph..**.** 
2eab0 52 65 67 69 73 74 65 72 69 6e 67 20 61 20 4e 55  Registering a NU
2eac0 4c 4c 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61  LL function disa
2ead0 62 6c 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63  bles the callbac
2eae0 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  k..**.** For the
2eaf0 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69   purposes of thi
2eb00 73 20 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63  s API, a transac
2eb10 74 69 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20  tion is said to 
2eb20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c  have been.** rol
2eb30 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65  led back if an e
2eb40 78 70 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41 43  xplicit "ROLLBAC
2eb50 4b 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  K" statement is 
2eb60 65 78 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20  executed, or.** 
2eb70 61 6e 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73  an error or cons
2eb80 74 72 61 69 6e 74 20 63 61 75 73 65 73 20 61 6e  traint causes an
2eb90 20 69 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61   implicit rollba
2eba0 63 6b 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20  ck to occur..** 
2ebb0 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c  The rollback cal
2ebc0 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76  lback is not inv
2ebd0 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73 61  oked if a transa
2ebe0 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f  ction is.** auto
2ebf0 6d 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64  matically rolled
2ec00 20 62 61 63 6b 20 62 65 63 61 75 73 65 20 74 68   back because th
2ec10 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2ec20 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e  ction is closed.
2ec30 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b  .** The rollback
2ec40 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74   callback is not
2ec50 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72   invoked if a tr
2ec60 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  ansaction is.** 
2ec70 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61  rolled back beca
2ec80 75 73 65 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c  use a commit cal
2ec90 6c 62 61 63 6b 20 72 65 74 75 72 6e 65 64 20 6e  lback returned n
2eca0 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64  on-zero..** <tod
2ecb0 6f 3e 20 43 68 65 63 6b 20 6f 6e 20 74 68 69 73  o> Check on this
2ecc0 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 52   </todo>.**.** R
2ecd0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
2ece0 5b 48 31 32 39 35 31 5d 20 5b 48 31 32 39 35 32  [H12951] [H12952
2ecf0 5d 20 5b 48 31 32 39 35 33 5d 20 5b 48 31 32 39  ] [H12953] [H129
2ed00 35 34 5d 20 5b 48 31 32 39 35 35 5d 0a 2a 2a 20  54] [H12955].** 
2ed10 5b 48 31 32 39 36 31 5d 20 5b 48 31 32 39 36 32  [H12961] [H12962
2ed20 5d 20 5b 48 31 32 39 36 33 5d 20 5b 48 31 32 39  ] [H12963] [H129
2ed30 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  64].*/.SQLITE_AP
2ed40 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
2ed50 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 73 71 6c 69  commit_hook(sqli
2ed60 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69  te3*, int(*)(voi
2ed70 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  d*), void*);.SQL
2ed80 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
2ed90 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68  lite3_rollback_h
2eda0 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f  ook(sqlite3*, vo
2edb0 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76  id(*)(void *), v
2edc0 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid*);../*.** CA
2edd0 50 49 33 52 45 46 3a 20 44 61 74 61 20 43 68 61  PI3REF: Data Cha
2ede0 6e 67 65 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e  nge Notification
2edf0 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39   Callbacks {H129
2ee00 37 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a  70} <S60400>.**.
2ee10 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 75  ** The sqlite3_u
2ee20 70 64 61 74 65 5f 68 6f 6f 6b 28 29 20 69 6e 74  pdate_hook() int
2ee30 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73  erface registers
2ee40 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63   a callback func
2ee50 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65  tion.** with the
2ee60 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
2ee70 63 74 69 6f 6e 5d 20 69 64 65 6e 74 69 66 69 65  ction] identifie
2ee80 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 61  d by the first a
2ee90 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 62 65  rgument.** to be
2eea0 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65   invoked wheneve
2eeb0 72 20 61 20 72 6f 77 20 69 73 20 75 70 64 61 74  r a row is updat
2eec0 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20  ed, inserted or 
2eed0 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 41 6e 79 20  deleted..** Any 
2eee0 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 20  callback set by 
2eef0 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20  a previous call 
2ef00 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
2ef10 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65  .** for the same
2ef20 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
2ef30 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64  tion is overridd
2ef40 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  en..**.** The se
2ef50 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
2ef60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2ef70 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  e function to in
2ef80 76 6f 6b 65 20 77 68 65 6e 20 61 0a 2a 2a 20 72  voke when a.** r
2ef90 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20 69  ow is updated, i
2efa0 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74  nserted or delet
2efb0 65 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  ed..** The first
2efc0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
2efd0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63   callback is a c
2efe0 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 72 64  opy of the third
2eff0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
2f000 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68  sqlite3_update_h
2f010 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65  ook()..** The se
2f020 63 6f 6e 64 20 63 61 6c 6c 62 61 63 6b 20 61 72  cond callback ar
2f030 67 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66  gument is one of
2f040 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 5d   [SQLITE_INSERT]
2f050 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  , [SQLITE_DELETE
2f060 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45  ],.** or [SQLITE
2f070 5f 55 50 44 41 54 45 5d 2c 20 64 65 70 65 6e 64  _UPDATE], depend
2f080 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 65 72 61  ing on the opera
2f090 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65 64  tion that caused
2f0a0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a   the callback.**
2f0b0 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 2e 0a   to be invoked..
2f0c0 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 6e 64  ** The third and
2f0d0 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74   fourth argument
2f0e0 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  s to the callbac
2f0f0 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65  k contain pointe
2f100 72 73 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74  rs to the.** dat
2f110 61 62 61 73 65 20 61 6e 64 20 74 61 62 6c 65 20  abase and table 
2f120 6e 61 6d 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  name containing 
2f130 74 68 65 20 61 66 66 65 63 74 65 64 20 72 6f 77  the affected row
2f140 2e 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20 63  ..** The final c
2f150 61 6c 6c 62 61 63 6b 20 70 61 72 61 6d 65 74 65  allback paramete
2f160 72 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 5d  r is the [rowid]
2f170 20 6f 66 20 74 68 65 20 72 6f 77 2e 0a 2a 2a 20   of the row..** 
2f180 49 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 61  In the case of a
2f190 6e 20 75 70 64 61 74 65 2c 20 74 68 69 73 20 69  n update, this i
2f1a0 73 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 61 66  s the [rowid] af
2f1b0 74 65 72 20 74 68 65 20 75 70 64 61 74 65 20 74  ter the update t
2f1c0 61 6b 65 73 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a  akes place..**.*
2f1d0 2a 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f  * The update hoo
2f1e0 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64  k is not invoked
2f1f0 20 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c 20 73   when internal s
2f200 79 73 74 65 6d 20 74 61 62 6c 65 73 20 61 72 65  ystem tables are
2f210 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 28 69 2e  .** modified (i.
2f220 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  e. sqlite_master
2f230 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 65 71 75   and sqlite_sequ
2f240 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ence)..**.** The
2f250 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d 70   update hook imp
2f260 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74  lementation must
2f270 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67   not do anything
2f280 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66   that will modif
2f290 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  y.** the databas
2f2a0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
2f2b0 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75 70  t invoked the up
2f2c0 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79 20  date hook.  Any 
2f2d0 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f  actions.** to mo
2f2e0 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73  dify the databas
2f2f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73  e connection mus
2f300 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e  t be deferred un
2f310 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a  til after the.**
2f320 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74   completion of t
2f330 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  he [sqlite3_step
2f340 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72  ()] call that tr
2f350 69 67 67 65 72 65 64 20 74 68 65 20 75 70 64 61  iggered the upda
2f360 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74 65  te hook..** Note
2f370 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70   that [sqlite3_p
2f380 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64  repare_v2()] and
2f390 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
2f3a0 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68  ] both modify th
2f3b0 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  eir.** database 
2f3c0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20  connections for 
2f3d0 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22  the meaning of "
2f3e0 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20  modify" in this 
2f3f0 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a  paragraph..**.**
2f400 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63   If another func
2f410 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75  tion was previou
2f420 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20  sly registered, 
2f430 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a 2a  its pArg value.*
2f440 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  * is returned.  
2f450 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69  Otherwise NULL i
2f460 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
2f470 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
2f480 2a 2a 20 5b 48 31 32 39 37 31 5d 20 5b 48 31 32  ** [H12971] [H12
2f490 39 37 33 5d 20 5b 48 31 32 39 37 35 5d 20 5b 48  973] [H12975] [H
2f4a0 31 32 39 37 37 5d 20 5b 48 31 32 39 37 39 5d 20  12977] [H12979] 
2f4b0 5b 48 31 32 39 38 31 5d 20 5b 48 31 32 39 38 33  [H12981] [H12983
2f4c0 5d 20 5b 48 31 32 39 38 36 5d 0a 2a 2f 0a 53 51  ] [H12986].*/.SQ
2f4d0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
2f4e0 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
2f4f0 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  ok(.  sqlite3*, 
2f500 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20  .  void(*)(void 
2f510 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73  *,int ,char cons
2f520 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a  t *,char const *
2f530 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c  ,sqlite3_int64),
2f540 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a  .  void*.);../*.
2f550 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61  ** CAPI3REF: Ena
2f560 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 53  ble Or Disable S
2f570 68 61 72 65 64 20 50 61 67 65 72 20 43 61 63 68  hared Pager Cach
2f580 65 20 7b 48 31 30 33 33 30 7d 20 3c 53 33 30 39  e {H10330} <S309
2f590 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  00>.** KEYWORDS:
2f5a0 20 7b 73 68 61 72 65 64 20 63 61 63 68 65 7d 20   {shared cache} 
2f5b0 7b 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f  {shared cache mo
2f5c0 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  de}.**.** This r
2f5d0 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f  outine enables o
2f5e0 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 73  r disables the s
2f5f0 68 61 72 69 6e 67 20 6f 66 20 74 68 65 20 64 61  haring of the da
2f600 74 61 62 61 73 65 20 63 61 63 68 65 0a 2a 2a 20  tabase cache.** 
2f610 61 6e 64 20 73 63 68 65 6d 61 20 64 61 74 61 20  and schema data 
2f620 73 74 72 75 63 74 75 72 65 73 20 62 65 74 77 65  structures betwe
2f630 65 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  en [database con
2f640 6e 65 63 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63  nection | connec
2f650 74 69 6f 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65  tions].** to the
2f660 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20   same database. 
2f670 53 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c  Sharing is enabl
2f680 65 64 20 69 66 20 74 68 65 20 61 72 67 75 6d 65  ed if the argume
2f690 6e 74 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e  nt is true.** an
2f6a0 64 20 64 69 73 61 62 6c 65 64 20 69 66 20 74 68  d disabled if th
2f6b0 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61  e argument is fa
2f6c0 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65  lse..**.** Cache
2f6d0 20 73 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62   sharing is enab
2f6e0 6c 65 64 20 61 6e 64 20 64 69 73 61 62 6c 65 64  led and disabled
2f6f0 20 66 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 70   for an entire p
2f700 72 6f 63 65 73 73 2e 0a 2a 2a 20 54 68 69 73 20  rocess..** This 
2f710 69 73 20 61 20 63 68 61 6e 67 65 20 61 73 20 6f  is a change as o
2f720 66 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  f SQLite version
2f730 20 33 2e 35 2e 30 2e 20 49 6e 20 70 72 69 6f 72   3.5.0. In prior
2f740 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
2f750 69 74 65 2c 0a 2a 2a 20 73 68 61 72 69 6e 67 20  ite,.** sharing 
2f760 77 61 73 20 65 6e 61 62 6c 65 64 20 6f 72 20 64  was enabled or d
2f770 69 73 61 62 6c 65 64 20 66 6f 72 20 65 61 63 68  isabled for each
2f780 20 74 68 72 65 61 64 20 73 65 70 61 72 61 74 65   thread separate
2f790 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  ly..**.** The ca
2f7a0 63 68 65 20 73 68 61 72 69 6e 67 20 6d 6f 64 65  che sharing mode
2f7b0 20 73 65 74 20 62 79 20 74 68 69 73 20 69 6e 74   set by this int
2f7c0 65 72 66 61 63 65 20 65 66 66 65 63 74 73 20 61  erface effects a
2f7d0 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  ll subsequent.**
2f7e0 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74   calls to [sqlit
2f7f0 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c  e3_open()], [sql
2f800 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c  ite3_open_v2()],
2f810 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6f 70   and [sqlite3_op
2f820 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20 45 78 69 73  en16()]..** Exis
2f830 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f  ting database co
2f840 6e 6e 65 63 74 69 6f 6e 73 20 63 6f 6e 74 69 6e  nnections contin
2f850 75 65 20 75 73 65 20 74 68 65 20 73 68 61 72 69  ue use the shari
2f860 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74 68 61 74 20  ng mode.** that 
2f870 77 61 73 20 69 6e 20 65 66 66 65 63 74 20 61 74  was in effect at
2f880 20 74 68 65 20 74 69 6d 65 20 74 68 65 79 20 77   the time they w
2f890 65 72 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 0a 2a  ere opened..**.*
2f8a0 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73  * Virtual tables
2f8b0 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 20   cannot be used 
2f8c0 77 69 74 68 20 61 20 73 68 61 72 65 64 20 63 61  with a shared ca
2f8d0 63 68 65 2e 20 20 57 68 65 6e 20 73 68 61 72 65  che.  When share
2f8e0 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 65 6e  d.** cache is en
2f8f0 61 62 6c 65 64 2c 20 74 68 65 20 5b 73 71 6c 69  abled, the [sqli
2f900 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
2f910 65 28 29 5d 20 41 50 49 20 75 73 65 64 20 74 6f  e()] API used to
2f920 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 76 69 72   register.** vir
2f930 74 75 61 6c 20 74 61 62 6c 65 73 20 77 69 6c 6c  tual tables will
2f940 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 61   always return a
2f950 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  n error..**.** T
2f960 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
2f970 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  rns [SQLITE_OK] 
2f980 69 66 20 73 68 61 72 65 64 20 63 61 63 68 65 20  if shared cache 
2f990 77 61 73 20 65 6e 61 62 6c 65 64 20 6f 72 20 64  was enabled or d
2f9a0 69 73 61 62 6c 65 64 0a 2a 2a 20 73 75 63 63 65  isabled.** succe
2f9b0 73 73 66 75 6c 6c 79 2e 20 20 41 6e 20 5b 65 72  ssfully.  An [er
2f9c0 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
2f9d0 75 72 6e 65 64 20 6f 74 68 65 72 77 69 73 65 2e  urned otherwise.
2f9e0 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 20 63 61  .**.** Shared ca
2f9f0 63 68 65 20 69 73 20 64 69 73 61 62 6c 65 64 20  che is disabled 
2fa00 62 79 20 64 65 66 61 75 6c 74 2e 20 42 75 74 20  by default. But 
2fa10 74 68 69 73 20 6d 69 67 68 74 20 63 68 61 6e 67  this might chang
2fa20 65 20 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 72  e in.** future r
2fa30 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74  eleases of SQLit
2fa40 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  e.  Applications
2fa50 20 74 68 61 74 20 63 61 72 65 20 61 62 6f 75 74   that care about
2fa60 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65   shared.** cache
2fa70 20 73 65 74 74 69 6e 67 20 73 68 6f 75 6c 64 20   setting should 
2fa80 73 65 74 20 69 74 20 65 78 70 6c 69 63 69 74 6c  set it explicitl
2fa90 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73  y..**.** See Als
2faa0 6f 3a 20 20 5b 53 51 4c 69 74 65 20 53 68 61 72  o:  [SQLite Shar
2fab0 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d 0a 2a  ed-Cache Mode].*
2fac0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2fad0 73 3a 20 5b 48 31 30 33 33 31 5d 20 5b 48 31 30  s: [H10331] [H10
2fae0 33 33 36 5d 20 5b 48 31 30 33 33 37 5d 20 5b 48  336] [H10337] [H
2faf0 31 30 33 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45  10339].*/.SQLITE
2fb00 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2fb10 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
2fb20 61 63 68 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  ache(int);../*.*
2fb30 2a 20 43 41 50 49 33 52 45 46 3a 20 41 74 74 65  * CAPI3REF: Atte
2fb40 6d 70 74 20 54 6f 20 46 72 65 65 20 48 65 61 70  mpt To Free Heap
2fb50 20 4d 65 6d 6f 72 79 20 7b 48 31 37 33 34 30 7d   Memory {H17340}
2fb60 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20   <S30220>.**.** 
2fb70 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  The sqlite3_rele
2fb80 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74  ase_memory() int
2fb90 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20  erface attempts 
2fba0 74 6f 20 66 72 65 65 20 4e 20 62 79 74 65 73 0a  to free N bytes.
2fbb0 2a 2a 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72  ** of heap memor
2fbc0 79 20 62 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e  y by deallocatin
2fbd0 67 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20  g non-essential 
2fbe0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2fbf0 6e 73 0a 2a 2a 20 68 65 6c 64 20 62 79 20 74 68  ns.** held by th
2fc00 65 20 64 61 74 61 62 61 73 65 20 6c 69 62 72 61  e database libra
2fc10 72 79 2e 20 7b 45 4e 44 7d 20 20 4d 65 6d 6f 72  ry. {END}  Memor
2fc20 79 20 75 73 65 64 20 74 6f 20 63 61 63 68 65 20  y used to cache 
2fc30 64 61 74 61 62 61 73 65 0a 2a 2a 20 70 61 67 65  database.** page
2fc40 73 20 74 6f 20 69 6d 70 72 6f 76 65 20 70 65 72  s to improve per
2fc50 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6e 20 65  formance is an e
2fc60 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73  xample of non-es
2fc70 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 2e 0a  sential memory..
2fc80 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  ** sqlite3_relea
2fc90 73 65 5f 6d 65 6d 6f 72 79 28 29 20 72 65 74 75  se_memory() retu
2fca0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
2fcb0 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79  f bytes actually
2fcc0 20 66 72 65 65 64 2c 0a 2a 2a 20 77 68 69 63 68   freed,.** which
2fcd0 20 6d 69 67 68 74 20 62 65 20 6d 6f 72 65 20 6f   might be more o
2fce0 72 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  r less than the 
2fcf0 61 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65 64  amount requested
2fd00 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
2fd10 65 6e 74 73 3a 20 5b 48 31 37 33 34 31 5d 20 5b  ents: [H17341] [
2fd20 48 31 37 33 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54  H17342].*/.SQLIT
2fd30 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2fd40 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
2fd50 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  (int);../*.** CA
2fd60 50 49 33 52 45 46 3a 20 49 6d 70 6f 73 65 20 41  PI3REF: Impose A
2fd70 20 4c 69 6d 69 74 20 4f 6e 20 48 65 61 70 20 53   Limit On Heap S
2fd80 69 7a 65 20 7b 48 31 37 33 35 30 7d 20 3c 53 33  ize {H17350} <S3
2fd90 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0220>.**.** The 
2fda0 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61  sqlite3_soft_hea
2fdb0 70 5f 6c 69 6d 69 74 28 29 20 69 6e 74 65 72 66  p_limit() interf
2fdc0 61 63 65 20 70 6c 61 63 65 73 20 61 20 22 73 6f  ace places a "so
2fdd0 66 74 22 20 6c 69 6d 69 74 0a 2a 2a 20 6f 6e 20  ft" limit.** on 
2fde0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65  the amount of he
2fdf0 61 70 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d  ap memory that m
2fe00 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20  ay be allocated 
2fe10 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 49 66  by SQLite..** If
2fe20 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c   an internal all
2fe30 6f 63 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65  ocation is reque
2fe40 73 74 65 64 20 74 68 61 74 20 77 6f 75 6c 64 20  sted that would 
2fe50 65 78 63 65 65 64 20 74 68 65 0a 2a 2a 20 73 6f  exceed the.** so
2fe60 66 74 20 68 65 61 70 20 6c 69 6d 69 74 2c 20 5b  ft heap limit, [
2fe70 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
2fe80 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 69 6e 76  memory()] is inv
2fe90 6f 6b 65 64 20 6f 6e 65 20 6f 72 0a 2a 2a 20 6d  oked one or.** m
2fea0 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 66 72 65  ore times to fre
2feb0 65 20 75 70 20 73 6f 6d 65 20 73 70 61 63 65 20  e up some space 
2fec0 62 65 66 6f 72 65 20 74 68 65 20 61 6c 6c 6f 63  before the alloc
2fed0 61 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d  ation is perform
2fee0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69  ed..**.** The li
2fef0 6d 69 74 20 69 73 20 63 61 6c 6c 65 64 20 22 73  mit is called "s
2ff00 6f 66 74 22 2c 20 62 65 63 61 75 73 65 20 69 66  oft", because if
2ff10 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73   [sqlite3_releas
2ff20 65 5f 6d 65 6d 6f 72 79 28 29 5d 0a 2a 2a 20 63  e_memory()].** c
2ff30 61 6e 6e 6f 74 20 66 72 65 65 20 73 75 66 66 69  annot free suffi
2ff40 63 69 65 6e 74 20 6d 65 6d 6f 72 79 20 74 6f 20  cient memory to 
2ff50 70 72 65 76 65 6e 74 20 74 68 65 20 6c 69 6d 69  prevent the limi
2ff60 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 65 78 63  t from being exc
2ff70 65 65 64 65 64 2c 0a 2a 2a 20 74 68 65 20 6d 65  eeded,.** the me
2ff80 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65  mory is allocate
2ff90 64 20 61 6e 79 77 61 79 20 61 6e 64 20 74 68 65  d anyway and the
2ffa0 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69   current operati
2ffb0 6f 6e 20 70 72 6f 63 65 65 64 73 2e 0a 2a 2a 0a  on proceeds..**.
2ffc0 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72  ** A negative or
2ffd0 20 7a 65 72 6f 20 76 61 6c 75 65 20 66 6f 72 20   zero value for 
2ffe0 4e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  N means that the
2fff0 72 65 20 69 73 20 6e 6f 20 73 6f 66 74 20 68 65  re is no soft he
30000 61 70 20 6c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20  ap limit and.** 
30010 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65  [sqlite3_release
30020 5f 6d 65 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20  _memory()] will 
30030 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77  only be called w
30040 68 65 6e 20 6d 65 6d 6f 72 79 20 69 73 20 65 78  hen memory is ex
30050 68 61 75 73 74 65 64 2e 0a 2a 2a 20 54 68 65 20  hausted..** The 
30060 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f  default value fo
30070 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20  r the soft heap 
30080 6c 69 6d 69 74 20 69 73 20 7a 65 72 6f 2e 0a 2a  limit is zero..*
30090 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65  *.** SQLite make
300a0 73 20 61 20 62 65 73 74 20 65 66 66 6f 72 74 20  s a best effort 
300b0 74 6f 20 68 6f 6e 6f 72 20 74 68 65 20 73 6f 66  to honor the sof
300c0 74 20 68 65 61 70 20 6c 69 6d 69 74 2e 0a 2a 2a  t heap limit..**
300d0 20 42 75 74 20 69 66 20 74 68 65 20 73 6f 66 74   But if the soft
300e0 20 68 65 61 70 20 6c 69 6d 69 74 20 63 61 6e 6e   heap limit cann
300f0 6f 74 20 62 65 20 68 6f 6e 6f 72 65 64 2c 20 65  ot be honored, e
30100 78 65 63 75 74 69 6f 6e 20 77 69 6c 6c 0a 2a 2a  xecution will.**
30110 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 6f 75   continue withou
30120 74 20 65 72 72 6f 72 20 6f 72 20 6e 6f 74 69 66  t error or notif
30130 69 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69  ication.  This i
30140 73 20 77 68 79 20 74 68 65 20 6c 69 6d 69 74 20  s why the limit 
30150 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 20 22  is.** called a "
30160 73 6f 66 74 22 20 6c 69 6d 69 74 2e 20 20 49 74  soft" limit.  It
30170 20 69 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c   is advisory onl
30180 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74  y..**.** Prior t
30190 6f 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  o SQLite version
301a0 20 33 2e 35 2e 30 2c 20 74 68 69 73 20 72 6f 75   3.5.0, this rou
301b0 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6e 73 74 72  tine only constr
301c0 61 69 6e 65 64 20 74 68 65 20 6d 65 6d 6f 72 79  ained the memory
301d0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  .** allocated by
301e0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64   a single thread
301f0 20 2d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65   - the same thre
30200 61 64 20 69 6e 20 77 68 69 63 68 20 74 68 69 73  ad in which this
30210 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 75 6e 73   routine.** runs
30220 2e 20 20 42 65 67 69 6e 6e 69 6e 67 20 77 69 74  .  Beginning wit
30230 68 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  h SQLite version
30240 20 33 2e 35 2e 30 2c 20 74 68 65 20 73 6f 66 74   3.5.0, the soft
30250 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 0a 2a   heap limit is.*
30260 2a 20 61 70 70 6c 69 65 64 20 74 6f 20 61 6c 6c  * applied to all
30270 20 74 68 72 65 61 64 73 2e 20 54 68 65 20 76 61   threads. The va
30280 6c 75 65 20 73 70 65 63 69 66 69 65 64 20 66 6f  lue specified fo
30290 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20  r the soft heap 
302a0 6c 69 6d 69 74 0a 2a 2a 20 69 73 20 61 6e 20 75  limit.** is an u
302b0 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68  pper bound on th
302c0 65 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61  e total memory a
302d0 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f 72 20 61 6c  llocation for al
302e0 6c 20 74 68 72 65 61 64 73 2e 20 49 6e 0a 2a 2a  l threads. In.**
302f0 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 20 74   version 3.5.0 t
30300 68 65 72 65 20 69 73 20 6e 6f 20 6d 65 63 68 61  here is no mecha
30310 6e 69 73 6d 20 66 6f 72 20 6c 69 6d 69 74 69 6e  nism for limitin
30320 67 20 74 68 65 20 68 65 61 70 20 75 73 61 67 65  g the heap usage
30330 20 66 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75   for.** individu
30340 61 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a  al threads..**.*
30350 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
30360 2a 2a 20 5b 48 31 36 33 35 31 5d 20 5b 48 31 36  ** [H16351] [H16
30370 33 35 32 5d 20 5b 48 31 36 33 35 33 5d 20 5b 48  352] [H16353] [H
30380 31 36 33 35 34 5d 20 5b 48 31 36 33 35 35 5d 20  16354] [H16355] 
30390 5b 48 31 36 33 35 38 5d 0a 2a 2f 0a 53 51 4c 49  [H16358].*/.SQLI
303a0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
303b0 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69  te3_soft_heap_li
303c0 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  mit(int);../*.**
303d0 20 43 41 50 49 33 52 45 46 3a 20 45 78 74 72 61   CAPI3REF: Extra
303e0 63 74 20 4d 65 74 61 64 61 74 61 20 41 62 6f 75  ct Metadata Abou
303f0 74 20 41 20 43 6f 6c 75 6d 6e 20 4f 66 20 41 20  t A Column Of A 
30400 54 61 62 6c 65 20 7b 48 31 32 38 35 30 7d 20 3c  Table {H12850} <
30410 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S60300>.**.** Th
30420 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
30430 6e 73 20 6d 65 74 61 64 61 74 61 20 61 62 6f 75  ns metadata abou
30440 74 20 61 20 73 70 65 63 69 66 69 63 20 63 6f 6c  t a specific col
30450 75 6d 6e 20 6f 66 20 61 20 73 70 65 63 69 66 69  umn of a specifi
30460 63 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 61  c.** database ta
30470 62 6c 65 20 61 63 63 65 73 73 69 62 6c 65 20 75  ble accessible u
30480 73 69 6e 67 20 74 68 65 20 5b 64 61 74 61 62 61  sing the [databa
30490 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68  se connection] h
304a0 61 6e 64 6c 65 0a 2a 2a 20 70 61 73 73 65 64 20  andle.** passed 
304b0 61 73 20 74 68 65 20 66 69 72 73 74 20 66 75 6e  as the first fun
304c0 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a  ction argument..
304d0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  **.** The column
304e0 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
304f0 79 20 74 68 65 20 73 65 63 6f 6e 64 2c 20 74 68  y the second, th
30500 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70  ird and fourth p
30510 61 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20  arameters to.** 
30520 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54  this function. T
30530 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
30540 74 65 72 20 69 73 20 65 69 74 68 65 72 20 74 68  ter is either th
30550 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  e name of the da
30560 74 61 62 61 73 65 0a 2a 2a 20 28 69 2e 65 2e 20  tabase.** (i.e. 
30570 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f  "main", "temp" o
30580 72 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  r an attached da
30590 74 61 62 61 73 65 29 20 63 6f 6e 74 61 69 6e 69  tabase) containi
305a0 6e 67 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ng the specified
305b0 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c  .** table or NUL
305c0 4c 2e 20 49 66 20 69 74 20 69 73 20 4e 55 4c 4c  L. If it is NULL
305d0 2c 20 74 68 65 6e 20 61 6c 6c 20 61 74 74 61 63  , then all attac
305e0 68 65 64 20 64 61 74 61 62 61 73 65 73 20 61 72  hed databases ar
305f0 65 20 73 65 61 72 63 68 65 64 0a 2a 2a 20 66 6f  e searched.** fo
30600 72 20 74 68 65 20 74 61 62 6c 65 20 75 73 69 6e  r the table usin
30610 67 20 74 68 65 20 73 61 6d 65 20 61 6c 67 6f 72  g the same algor
30620 69 74 68 6d 20 75 73 65 64 20 62 79 20 74 68 65  ithm used by the
30630 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
30640 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 75   to.** resolve u
30650 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65  nqualified table
30660 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a   references..**.
30670 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 6e 64  ** The third and
30680 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
30690 72 73 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  rs to this funct
306a0 69 6f 6e 20 61 72 65 20 74 68 65 20 74 61 62 6c  ion are the tabl
306b0 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  e and column.** 
306c0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 65 73 69  name of the desi
306d0 72 65 64 20 63 6f 6c 75 6d 6e 2c 20 72 65 73 70  red column, resp
306e0 65 63 74 69 76 65 6c 79 2e 20 4e 65 69 74 68 65  ectively. Neithe
306f0 72 20 6f 66 20 74 68 65 73 65 20 70 61 72 61 6d  r of these param
30700 65 74 65 72 73 0a 2a 2a 20 6d 61 79 20 62 65 20  eters.** may be 
30710 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4d 65 74 61  NULL..**.** Meta
30720 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64  data is returned
30730 20 62 79 20 77 72 69 74 69 6e 67 20 74 6f 20 74   by writing to t
30740 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
30750 6f 6e 73 20 70 61 73 73 65 64 20 61 73 20 74 68  ons passed as th
30760 65 20 35 74 68 0a 2a 2a 20 61 6e 64 20 73 75 62  e 5th.** and sub
30770 73 65 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65  sequent paramete
30780 72 73 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  rs to this funct
30790 69 6f 6e 2e 20 41 6e 79 20 6f 66 20 74 68 65 73  ion. Any of thes
307a0 65 20 61 72 67 75 6d 65 6e 74 73 20 6d 61 79 20  e arguments may 
307b0 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20 69 6e 20 77  be.** NULL, in w
307c0 68 69 63 68 20 63 61 73 65 20 74 68 65 20 63 6f  hich case the co
307d0 72 72 65 73 70 6f 6e 64 69 6e 67 20 65 6c 65 6d  rresponding elem
307e0 65 6e 74 20 6f 66 20 6d 65 74 61 64 61 74 61 20  ent of metadata 
307f0 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a  is omitted..**.*
30800 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  * <blockquote>.*
30810 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d  * <table border=
30820 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e  "1">.** <tr><th>
30830 20 50 61 72 61 6d 65 74 65 72 20 3c 74 68 3e 20   Parameter <th> 
30840 4f 75 74 70 75 74 3c 62 72 3e 54 79 70 65 20 3c  Output<br>Type <
30850 74 68 3e 20 20 44 65 73 63 72 69 70 74 69 6f 6e  th>  Description
30860 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  .**.** <tr><td> 
30870 35 74 68 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63  5th <td> const c
30880 68 61 72 2a 20 3c 74 64 3e 20 44 61 74 61 20 74  har* <td> Data t
30890 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  ype.** <tr><td> 
308a0 36 74 68 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63  6th <td> const c
308b0 68 61 72 2a 20 3c 74 64 3e 20 4e 61 6d 65 20 6f  har* <td> Name o
308c0 66 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  f default collat
308d0 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20  ion sequence.** 
308e0 3c 74 72 3e 3c 74 64 3e 20 37 74 68 20 3c 74 64  <tr><td> 7th <td
308f0 3e 20 69 6e 74 20 20 20 20 20 20 20 20 20 3c 74  > int         <t
30900 64 3e 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  d> True if colum
30910 6e 20 68 61 73 20 61 20 4e 4f 54 20 4e 55 4c 4c  n has a NOT NULL
30920 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 3c   constraint.** <
30930 74 72 3e 3c 74 64 3e 20 38 74 68 20 3c 74 64 3e  tr><td> 8th <td>
30940 20 69 6e 74 20 20 20 20 20 20 20 20 20 3c 74 64   int         <td
30950 3e 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e  > True if column
30960 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   is part of the 
30970 50 52 49 4d 41 52 59 20 4b 45 59 0a 2a 2a 20 3c  PRIMARY KEY.** <
30980 74 72 3e 3c 74 64 3e 20 39 74 68 20 3c 74 64 3e  tr><td> 9th <td>
30990 20 69 6e 74 20 20 20 20 20 20 20 20 20 3c 74 64   int         <td
309a0 3e 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e  > True if column
309b0 20 69 73 20 5b 41 55 54 4f 49 4e 43 52 45 4d 45   is [AUTOINCREME
309c0 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a  NT].** </table>.
309d0 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  ** </blockquote>
309e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72  .**.** The memor
309f0 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  y pointed to by 
30a00 74 68 65 20 63 68 61 72 61 63 74 65 72 20 70 6f  the character po
30a10 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 64 20  inters returned 
30a20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 65 63 6c 61  for the.** decla
30a30 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 20  ration type and 
30a40 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
30a50 63 65 20 69 73 20 76 61 6c 69 64 20 6f 6e 6c 79  ce is valid only
30a60 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a   until the next.
30a70 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 53  ** call to any S
30a80 51 4c 69 74 65 20 41 50 49 20 66 75 6e 63 74 69  QLite API functi
30a90 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  on..**.** If the
30aa0 20 73 70 65 63 69 66 69 65 64 20 74 61 62 6c 65   specified table
30ab0 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 76   is actually a v
30ac0 69 65 77 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63  iew, an [error c
30ad0 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
30ae0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73  ..**.** If the s
30af0 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20  pecified column 
30b00 69 73 20 22 72 6f 77 69 64 22 2c 20 22 6f 69 64  is "rowid", "oid
30b10 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 20 61  " or "_rowid_" a
30b20 6e 64 20 61 6e 0a 2a 2a 20 5b 49 4e 54 45 47 45  nd an.** [INTEGE
30b30 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63  R PRIMARY KEY] c
30b40 6f 6c 75 6d 6e 20 68 61 73 20 62 65 65 6e 20 65  olumn has been e
30b50 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
30b60 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74  ed, then the out
30b70 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  put.** parameter
30b80 73 20 61 72 65 20 73 65 74 20 66 6f 72 20 74 68  s are set for th
30b90 65 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63  e explicitly dec
30ba0 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66  lared column. If
30bb0 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20   there is no.** 
30bc0 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61  explicitly decla
30bd0 72 65 64 20 5b 49 4e 54 45 47 45 52 20 50 52 49  red [INTEGER PRI
30be0 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e  MARY KEY] column
30bf0 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75  , then the outpu
30c00 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20  t.** parameters 
30c10 61 72 65 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f  are set as follo
30c20 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a  ws:.**.** <pre>.
30c30 2a 2a 20 20 20 20 20 64 61 74 61 20 74 79 70 65  **     data type
30c40 3a 20 22 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20  : "INTEGER".**  
30c50 20 20 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71     collation seq
30c60 75 65 6e 63 65 3a 20 22 42 49 4e 41 52 59 22 0a  uence: "BINARY".
30c70 2a 2a 20 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a  **     not null:
30c80 20 30 0a 2a 2a 20 20 20 20 20 70 72 69 6d 61 72   0.**     primar
30c90 79 20 6b 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20  y key: 1.**     
30ca0 61 75 74 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20  auto increment: 
30cb0 30 0a 2a 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a  0.** </pre>.**.*
30cc0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
30cd0 6d 61 79 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20  may load one or 
30ce0 6d 6f 72 65 20 73 63 68 65 6d 61 73 20 66 72 6f  more schemas fro
30cf0 6d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  m database files
30d00 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  . If an.** error
30d10 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74   occurs during t
30d20 68 69 73 20 70 72 6f 63 65 73 73 2c 20 6f 72 20  his process, or 
30d30 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  if the requested
30d40 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e   table or column
30d50 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  .** cannot be fo
30d60 75 6e 64 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63  und, an [error c
30d70 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
30d80 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   and an error me
30d90 73 73 61 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e  ssage left.** in
30da0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
30db0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62  onnection] (to b
30dc0 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69 6e  e retrieved usin
30dd0 67 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  g sqlite3_errmsg
30de0 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ())..**.** This 
30df0 41 50 49 20 69 73 20 6f 6e 6c 79 20 61 76 61 69  API is only avai
30e00 6c 61 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62  lable if the lib
30e10 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65  rary was compile
30e20 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53  d with the.** [S
30e30 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
30e40 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d  UMN_METADATA] C-
30e50 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d  preprocessor sym
30e60 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  bol defined..*/.
30e70 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
30e80 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c  qlite3_table_col
30e90 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20  umn_metadata(.  
30ea0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
30eb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
30ec0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
30ed0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
30ee0 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20   *zDbName,      
30ef0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61    /* Database na
30f00 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  me or NULL */.  
30f10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
30f20 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54  leName,     /* T
30f30 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63  able name */.  c
30f40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75  onst char *zColu
30f50 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f  mnName,    /* Co
30f60 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63  lumn name */.  c
30f70 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61  har const **pzDa
30f80 74 61 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55  taType,    /* OU
30f90 54 50 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64  TPUT: Declared d
30fa0 61 74 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68  ata type */.  ch
30fb0 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c  ar const **pzCol
30fc0 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54  lSeq,     /* OUT
30fd0 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73  PUT: Collation s
30fe0 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a  equence name */.
30ff0 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c    int *pNotNull,
31000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
31010 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66   OUTPUT: True if
31020 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
31030 61 69 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20  aint exists */. 
31040 20 69 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65   int *pPrimaryKe
31050 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
31060 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20  OUTPUT: True if 
31070 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50  column part of P
31080 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74  K */.  int *pAut
31090 6f 69 6e 63 20 20 20 20 20 20 20 20 20 20 20 20  oinc            
310a0 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
310b0 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20  ue if column is 
310c0 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a  auto-increment *
310d0 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.);../*.** CAPI
310e0 33 52 45 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78  3REF: Load An Ex
310f0 74 65 6e 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d  tension {H12600}
31100 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20500>.**.** 
31110 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 6c  This interface l
31120 6f 61 64 73 20 61 6e 20 53 51 4c 69 74 65 20 65  oads an SQLite e
31130 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79  xtension library
31140 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20   from the named 
31150 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  file..**.** {H12
31160 36 30 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33  601} The sqlite3
31170 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
31180 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65  ) interface atte
31190 6d 70 74 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a  mpts to load an.
311a0 2a 2a 20 20 20 20 20 20 20 20 20 20 53 51 4c 69  **          SQLi
311b0 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62  te extension lib
311c0 72 61 72 79 20 63 6f 6e 74 61 69 6e 65 64 20 69  rary contained i
311d0 6e 20 74 68 65 20 66 69 6c 65 20 7a 46 69 6c 65  n the file zFile
311e0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d  ..**.** {H12602}
311f0 20 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74   The entry point
31200 20 69 73 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a   is zProc..**.**
31210 20 7b 48 31 32 36 30 33 7d 20 7a 50 72 6f 63 20   {H12603} zProc 
31220 6d 61 79 20 62 65 20 30 2c 20 69 6e 20 77 68 69  may be 0, in whi
31230 63 68 20 63 61 73 65 20 74 68 65 20 6e 61 6d 65  ch case the name
31240 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 70 6f   of the entry po
31250 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  int.**          
31260 64 65 66 61 75 6c 74 73 20 74 6f 20 22 73 71 6c  defaults to "sql
31270 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
31280 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  nit"..**.** {H12
31290 36 30 34 7d 20 54 68 65 20 73 71 6c 69 74 65 33  604} The sqlite3
312a0 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
312b0 29 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c  ) interface shal
312c0 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20  l return.**     
312d0 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d       [SQLITE_OK]
312e0 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20   on success and 
312f0 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69  [SQLITE_ERROR] i
31300 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73  f something goes
31310 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48   wrong..**.** {H
31320 31 32 36 30 35 7d 20 49 66 20 61 6e 20 65 72 72  12605} If an err
31330 6f 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a  or occurs and pz
31340 45 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c  ErrMsg is not 0,
31350 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20   then the.**    
31360 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c        [sqlite3_l
31370 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d  oad_extension()]
31380 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c   interface shall
31390 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20   attempt to.**  
313a0 20 20 20 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a          fill *pz
313b0 45 72 72 4d 73 67 20 77 69 74 68 20 65 72 72 6f  ErrMsg with erro
313c0 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 73  r message text s
313d0 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a  tored in memory.
313e0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 74 61  **          obta
313f0 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74  ined from [sqlit
31400 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45  e3_malloc()]. {E
31410 4e 44 7d 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  ND}  The calling
31420 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20   function.**    
31430 20 20 20 20 20 20 73 68 6f 75 6c 64 20 66 72 65        should fre
31440 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 62 79  e this memory by
31450 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65   calling [sqlite
31460 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  3_free()]..**.**
31470 20 7b 48 31 32 36 30 36 7d 20 45 78 74 65 6e 73   {H12606} Extens
31480 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74  ion loading must
31490 20 62 65 20 65 6e 61 62 6c 65 64 20 75 73 69 6e   be enabled usin
314a0 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  g.**          [s
314b0 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f  qlite3_enable_lo
314c0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20  ad_extension()] 
314d0 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67  prior to calling
314e0 20 74 68 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20   this API,.**   
314f0 20 20 20 20 20 20 20 6f 74 68 65 72 77 69 73 65         otherwise
31500 20 61 6e 20 65 72 72 6f 72 20 77 69 6c 6c 20 62   an error will b
31510 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  e returned..*/.S
31520 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
31530 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
31540 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  sion(.  sqlite3 
31550 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  *db,          /*
31560 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73   Load the extens
31570 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61  ion into this da
31580 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
31590 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
315a0 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20  r *zFile,    /* 
315b0 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72  Name of the shar
315c0 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61  ed library conta
315d0 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20  ining extension 
315e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
315f0 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e  *zProc,    /* En
31600 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 65 72 69  try point.  Deri
31610 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c 65 20 69  ved from zFile i
31620 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  f 0 */.  char **
31630 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f  pzErrMsg       /
31640 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73  * Put error mess
31650 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20  age here if not 
31660 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  0 */.);../*.** C
31670 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20  API3REF: Enable 
31680 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e  Or Disable Exten
31690 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31  sion Loading {H1
316a0 32 36 32 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a  2620} <S20500>.*
316b0 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f 74 20 74  *.** So as not t
316c0 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79 20  o open security 
316d0 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 61  holes in older a
316e0 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
316f0 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61 72   are.** unprepar
31700 65 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20  ed to deal with 
31710 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
31720 67 2c 20 61 6e 64 20 61 73 20 61 20 6d 65 61 6e  g, and as a mean
31730 73 20 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a 2a  s of disabling.*
31740 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  * extension load
31750 69 6e 67 20 77 68 69 6c 65 20 65 76 61 6c 75 61  ing while evalua
31760 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 65 72 65  ting user-entere
31770 64 20 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c 6f  d SQL, the follo
31780 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20 70  wing API.** is p
31790 72 6f 76 69 64 65 64 20 74 6f 20 74 75 72 6e 20  rovided to turn 
317a0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61  the [sqlite3_loa
317b0 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 6d  d_extension()] m
317c0 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 20  echanism on and 
317d0 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e  off..**.** Exten
317e0 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20  sion loading is 
317f0 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e 20  off by default. 
31800 53 65 65 20 74 69 63 6b 65 74 20 23 31 38 36 33  See ticket #1863
31810 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 31 7d  ..**.** {H12621}
31820 20 43 61 6c 6c 20 74 68 65 20 73 71 6c 69 74 65   Call the sqlite
31830 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78  3_enable_load_ex
31840 74 65 6e 73 69 6f 6e 28 29 20 72 6f 75 74 69 6e  tension() routin
31850 65 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 31 0a  e with onoff==1.
31860 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 74  **          to t
31870 75 72 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  urn extension lo
31880 61 64 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61 6c  ading on and cal
31890 6c 20 69 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d  l it with onoff=
318a0 3d 30 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 20 20  =0 to turn.**   
318b0 20 20 20 20 20 20 20 69 74 20 62 61 63 6b 20 6f         it back o
318c0 66 66 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20  ff again..**.** 
318d0 7b 48 31 32 36 32 32 7d 20 45 78 74 65 6e 73 69  {H12622} Extensi
318e0 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66  on loading is of
318f0 66 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2f  f by default..*/
31900 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
31910 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c  sqlite3_enable_l
31920 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71  oad_extension(sq
31930 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f  lite3 *db, int o
31940 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  noff);../*.** CA
31950 50 49 33 52 45 46 3a 20 41 75 74 6f 6d 61 74 69  PI3REF: Automati
31960 63 61 6c 6c 79 20 4c 6f 61 64 20 41 6e 20 45 78  cally Load An Ex
31970 74 65 6e 73 69 6f 6e 73 20 7b 48 31 32 36 34 30  tensions {H12640
31980 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a  } <S20500>.**.**
31990 20 54 68 69 73 20 41 50 49 20 63 61 6e 20 62 65   This API can be
319a0 20 69 6e 76 6f 6b 65 64 20 61 74 20 70 72 6f 67   invoked at prog
319b0 72 61 6d 20 73 74 61 72 74 75 70 20 69 6e 20 6f  ram startup in o
319c0 72 64 65 72 20 74 6f 20 72 65 67 69 73 74 65 72  rder to register
319d0 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  .** one or more 
319e0 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65  statically linke
319f0 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 68 61  d extensions tha
31a00 74 20 77 69 6c 6c 20 62 65 20 61 76 61 69 6c 61  t will be availa
31a10 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e 65  ble.** to all ne
31a20 77 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  w [database conn
31a30 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d 0a  ections]. {END}.
31a40 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
31a50 6e 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e  ne stores a poin
31a60 74 65 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e  ter to the exten
31a70 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79  sion in an array
31a80 20 74 68 61 74 20 69 73 0a 2a 2a 20 6f 62 74 61   that is.** obta
31a90 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74  ined from [sqlit
31aa0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 49  e3_malloc()].  I
31ab0 66 20 79 6f 75 20 72 75 6e 20 61 20 6d 65 6d 6f  f you run a memo
31ac0 72 79 20 6c 65 61 6b 20 63 68 65 63 6b 65 72 0a  ry leak checker.
31ad0 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 72 6f 67 72  ** on your progr
31ae0 61 6d 20 61 6e 64 20 69 74 20 72 65 70 6f 72 74  am and it report
31af0 73 20 61 20 6c 65 61 6b 20 62 65 63 61 75 73 65  s a leak because
31b00 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 2c 20   of this array, 
31b10 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69 74  invoke.** [sqlit
31b20 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78  e3_reset_auto_ex
31b30 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72  tension()] prior
31b40 20 74 6f 20 73 68 75 74 64 6f 77 6e 20 74 6f 20   to shutdown to 
31b50 66 72 65 65 20 74 68 65 20 6d 65 6d 6f 72 79 2e  free the memory.
31b60 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 31 7d 20  .**.** {H12641} 
31b70 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
31b80 67 69 73 74 65 72 73 20 61 6e 20 65 78 74 65 6e  gisters an exten
31b90 73 69 6f 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74  sion entry point
31ba0 20 74 68 61 74 20 69 73 0a 2a 2a 20 20 20 20 20   that is.**     
31bb0 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c       automatical
31bc0 6c 79 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65  ly invoked whene
31bd0 76 65 72 20 61 20 6e 65 77 20 5b 64 61 74 61 62  ver a new [datab
31be0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a  ase connection].
31bf0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 6f  **          is o
31c00 70 65 6e 65 64 20 75 73 69 6e 67 20 5b 73 71 6c  pened using [sql
31c10 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73  ite3_open()], [s
31c20 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
31c30 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  ,.**          or
31c40 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
31c50 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  2()]..**.** {H12
31c60 36 34 32 7d 20 44 75 70 6c 69 63 61 74 65 20 65  642} Duplicate e
31c70 78 74 65 6e 73 69 6f 6e 73 20 61 72 65 20 64 65  xtensions are de
31c80 74 65 63 74 65 64 20 73 6f 20 63 61 6c 6c 69 6e  tected so callin
31c90 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  g this routine.*
31ca0 2a 20 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69  *          multi
31cb0 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 20 74  ple times with t
31cc0 68 65 20 73 61 6d 65 20 65 78 74 65 6e 73 69 6f  he same extensio
31cd0 6e 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 0a 2a  n is harmless..*
31ce0 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 7d 20 54 68  *.** {H12643} Th
31cf0 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65  is routine store
31d00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
31d10 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20  he extension in 
31d20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 20 20 20 20  an array.**     
31d30 20 20 20 20 20 74 68 61 74 20 69 73 20 6f 62 74       that is obt
31d40 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
31d50 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a  te3_malloc()]..*
31d60 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 7d 20 41 75  *.** {H12644} Au
31d70 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
31d80 6e 73 20 61 70 70 6c 79 20 61 63 72 6f 73 73 20  ns apply across 
31d90 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a  all threads..*/.
31da0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
31db0 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65  qlite3_auto_exte
31dc0 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78 45 6e  nsion(void (*xEn
31dd0 74 72 79 50 6f 69 6e 74 29 28 76 6f 69 64 29 29  tryPoint)(void))
31de0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
31df0 46 3a 20 52 65 73 65 74 20 41 75 74 6f 6d 61 74  F: Reset Automat
31e00 69 63 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61  ic Extension Loa
31e10 64 69 6e 67 20 7b 48 31 32 36 36 30 7d 20 3c 53  ding {H12660} <S
31e20 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  20500>.**.** Thi
31e30 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62  s function disab
31e40 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f 75 73  les all previous
31e50 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 61 75  ly registered au
31e60 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 78 74 65 6e  tomatic.** exten
31e70 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d 20 20 49 74  sions. {END}  It
31e80 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66 65   undoes the effe
31e90 63 74 20 6f 66 20 61 6c 6c 20 70 72 69 6f 72 0a  ct of all prior.
31ea0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 75 74 6f  ** [sqlite3_auto
31eb0 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 63 61  _extension()] ca
31ec0 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  lls..**.** {H126
31ed0 36 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 6f  61} This functio
31ee0 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70  n disables all p
31ef0 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74  reviously regist
31f00 65 72 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  ered.**         
31f10 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e   automatic exten
31f20 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  sions..**.** {H1
31f30 32 36 36 32 7d 20 54 68 69 73 20 66 75 6e 63 74  2662} This funct
31f40 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 75 74  ion disables aut
31f50 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e  omatic extension
31f60 73 20 69 6e 20 61 6c 6c 20 74 68 72 65 61 64 73  s in all threads
31f70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
31f80 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
31f90 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f  et_auto_extensio
31fa0 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 2a  n(void);../*.***
31fb0 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  *** EXPERIMENTAL
31fc0 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68   - subject to ch
31fd0 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74  ange without not
31fe0 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ice ************
31ff0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74  **.**.** The int
32000 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69  erface to the vi
32010 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68  rtual-table mech
32020 61 6e 69 73 6d 20 69 73 20 63 75 72 72 65 6e 74  anism is current
32030 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a  ly considered.**
32040 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e   to be experimen
32050 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66  tal.  The interf
32060 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65  ace might change
32070 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65   in incompatible
32080 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69   ways..** If thi
32090 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66  s is a problem f
320a0 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75  or you, do not u
320b0 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  se the interface
320c0 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a   at this time..*
320d0 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69  *.** When the vi
320e0 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68  rtual-table mech
320f0 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73  anism stabilizes
32100 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72  , we will declar
32110 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61  e the.** interfa
32120 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72  ce fixed, suppor
32130 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c  t it indefinitel
32140 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68  y, and remove th
32150 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a 0a  is comment..*/..
32160 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 73  /*.** Structures
32170 20 75 73 65 64 20 62 79 20 74 68 65 20 76 69 72   used by the vir
32180 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65 72  tual table inter
32190 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20  face.*/.typedef 
321a0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76  struct sqlite3_v
321b0 74 61 62 20 73 71 6c 69 74 65 33 5f 76 74 61 62  tab sqlite3_vtab
321c0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
321d0 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
321e0 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  nfo sqlite3_inde
321f0 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20  x_info;.typedef 
32200 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76  struct sqlite3_v
32210 74 61 62 5f 63 75 72 73 6f 72 20 73 71 6c 69 74  tab_cursor sqlit
32220 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 3b 0a  e3_vtab_cursor;.
32230 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
32240 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73 71  qlite3_module sq
32250 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b 0a 0a 2f  lite3_module;../
32260 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56  *.** CAPI3REF: V
32270 69 72 74 75 61 6c 20 54 61 62 6c 65 20 4f 62 6a  irtual Table Obj
32280 65 63 74 20 7b 48 31 38 30 30 30 7d 20 3c 53 32  ect {H18000} <S2
32290 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0400>.** KEYWORD
322a0 53 3a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  S: sqlite3_modul
322b0 65 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e {virtual table
322c0 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 45 58 50 45   module}.** EXPE
322d0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
322e0 68 69 73 20 73 74 72 75 63 74 75 72 65 2c 20 73  his structure, s
322f0 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20  ometimes called 
32300 61 20 61 20 22 76 69 72 74 75 61 6c 20 74 61 62  a a "virtual tab
32310 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 0a 2a 2a 20  le module", .** 
32320 64 65 66 69 6e 65 73 20 74 68 65 20 69 6d 70 6c  defines the impl
32330 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20  ementation of a 
32340 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 5d  [virtual tables]
32350 2e 20 20 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  .  .** This stru
32360 63 74 75 72 65 20 63 6f 6e 73 69 73 74 73 20 6d  cture consists m
32370 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68 6f 64 73  ostly of methods
32380 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 2e   for the module.
32390 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c  .**.** A virtual
323a0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 69 73   table module is
323b0 20 63 72 65 61 74 65 64 20 62 79 20 66 69 6c 6c   created by fill
323c0 69 6e 67 20 69 6e 20 61 20 70 65 72 73 69 73 74  ing in a persist
323d0 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20  ent.** instance 
323e0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
323f0 65 20 61 6e 64 20 70 61 73 73 69 6e 67 20 61 20  e and passing a 
32400 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20  pointer to that 
32410 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 74 6f 20 5b  instance.** to [
32420 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
32430 6f 64 75 6c 65 28 29 5d 20 6f 72 20 5b 73 71 6c  odule()] or [sql
32440 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
32450 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a 20 54 68 65  le_v2()]..** The
32460 20 72 65 67 69 73 74 72 61 74 69 6f 6e 20 72 65   registration re
32470 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69  mains valid unti
32480 6c 20 69 74 20 69 73 20 72 65 70 6c 61 63 65 64  l it is replaced
32490 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 0a   by a different.
324a0 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 20 75 6e 74  ** module or unt
324b0 69 6c 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  il the [database
324c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 63 6c 6f   connection] clo
324d0 73 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e  ses.  The conten
324e0 74 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72  t.** of this str
324f0 75 63 74 75 72 65 20 6d 75 73 74 20 6e 6f 74 20  ucture must not 
32500 63 68 61 6e 67 65 20 77 68 69 6c 65 20 69 74 20  change while it 
32510 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69  is registered wi
32520 74 68 0a 2a 2a 20 61 6e 79 20 64 61 74 61 62 61  th.** any databa
32530 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
32540 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
32550 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 6e 74 20  _module {.  int 
32560 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20  iVersion;.  int 
32570 28 2a 78 43 72 65 61 74 65 29 28 73 71 6c 69 74  (*xCreate)(sqlit
32580 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c  e3*, void *pAux,
32590 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
325a0 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
325b0 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76  char *const*argv
325c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
325d0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a   sqlite3_vtab **
325e0 70 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b  ppVTab, char**);
325f0 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 6e 65 63  .  int (*xConnec
32600 74 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69  t)(sqlite3*, voi
32610 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20  d *pAux,.       
32620 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 63          int argc
32630 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  , const char *co
32640 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20  nst*argv,.      
32650 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
32660 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20  _vtab **ppVTab, 
32670 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28  char**);.  int (
32680 2a 78 42 65 73 74 49 6e 64 65 78 29 28 73 71 6c  *xBestIndex)(sql
32690 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
326a0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  , sqlite3_index_
326b0 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  info*);.  int (*
326c0 78 44 69 73 63 6f 6e 6e 65 63 74 29 28 73 71 6c  xDisconnect)(sql
326d0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
326e0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 74  );.  int (*xDest
326f0 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 76 74 61  roy)(sqlite3_vta
32700 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74  b *pVTab);.  int
32710 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74 65   (*xOpen)(sqlite
32720 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73  3_vtab *pVTab, s
32730 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
32740 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 3b 0a  or **ppCursor);.
32750 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28    int (*xClose)(
32760 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
32770 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  sor*);.  int (*x
32780 46 69 6c 74 65 72 29 28 73 71 6c 69 74 65 33 5f  Filter)(sqlite3_
32790 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 69 6e  vtab_cursor*, in
327a0 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20  t idxNum, const 
327b0 63 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20 20  char *idxStr,.  
327c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
327d0 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
327e0 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 3b 0a 20  value **argv);. 
327f0 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73 71   int (*xNext)(sq
32800 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
32810 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 45 6f  r*);.  int (*xEo
32820 66 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  f)(sqlite3_vtab_
32830 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20  cursor*);.  int 
32840 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 6c 69 74  (*xColumn)(sqlit
32850 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c  e3_vtab_cursor*,
32860 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
32870 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  *, int);.  int (
32880 2a 78 52 6f 77 69 64 29 28 73 71 6c 69 74 65 33  *xRowid)(sqlite3
32890 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 73  _vtab_cursor*, s
328a0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 52  qlite3_int64 *pR
328b0 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78  owid);.  int (*x
328c0 55 70 64 61 74 65 29 28 73 71 6c 69 74 65 33 5f  Update)(sqlite3_
328d0 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20 73 71 6c  vtab *, int, sql
328e0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 2c 20 73  ite3_value **, s
328f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 29 3b  qlite3_int64 *);
32900 0a 20 20 69 6e 74 20 28 2a 78 42 65 67 69 6e 29  .  int (*xBegin)
32910 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
32920 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
32930 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 76 74  Sync)(sqlite3_vt
32940 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e  ab *pVTab);.  in
32950 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28 73 71 6c  t (*xCommit)(sql
32960 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
32970 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 6c 6c  );.  int (*xRoll
32980 62 61 63 6b 29 28 73 71 6c 69 74 65 33 5f 76 74  back)(sqlite3_vt
32990 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e  ab *pVTab);.  in
329a0 74 20 28 2a 78 46 69 6e 64 46 75 6e 63 74 69 6f  t (*xFindFunctio
329b0 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  n)(sqlite3_vtab 
329c0 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67  *pVtab, int nArg
329d0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
329e0 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ame,.           
329f0 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64              void
32a00 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c 69   (**pxFunc)(sqli
32a10 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
32a20 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
32a30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
32a40 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a            void *
32a50 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28  *ppArg);.  int (
32a60 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c 69 74 65  *xRename)(sqlite
32a70 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 63  3_vtab *pVtab, c
32a80 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 29  onst char *zNew)
32a90 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ;.};../*.** CAPI
32aa0 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61  3REF: Virtual Ta
32ab0 62 6c 65 20 49 6e 64 65 78 69 6e 67 20 49 6e 66  ble Indexing Inf
32ac0 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38 31 30 30  ormation {H18100
32ad0 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45  } <S20400>.** KE
32ae0 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f  YWORDS: sqlite3_
32af0 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 45 58  index_info.** EX
32b00 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
32b10 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   The sqlite3_ind
32b20 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
32b30 65 20 61 6e 64 20 69 74 73 20 73 75 62 73 74 72  e and its substr
32b40 75 63 74 75 72 65 73 20 69 73 20 75 73 65 64 20  uctures is used 
32b50 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e 66 6f 72  to.** pass infor
32b60 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61 6e 64 20  mation into and 
32b70 72 65 63 65 69 76 65 20 74 68 65 20 72 65 70 6c  receive the repl
32b80 79 20 66 72 6f 6d 20 74 68 65 20 5b 78 42 65 73  y from the [xBes
32b90 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d 65 74 68 6f  tIndex].** metho
32ba0 64 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20  d of a [virtual 
32bb0 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20  table module].  
32bc0 54 68 65 20 66 69 65 6c 64 73 20 75 6e 64 65 72  The fields under
32bd0 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65 20   **Inputs** are 
32be0 74 68 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74 6f  the.** inputs to
32bf0 20 78 42 65 73 74 49 6e 64 65 78 20 61 6e 64 20   xBestIndex and 
32c00 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20  are read-only.  
32c10 78 42 65 73 74 49 6e 64 65 78 20 69 6e 73 65 72  xBestIndex inser
32c20 74 73 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74  ts its.** result
32c30 73 20 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75 74  s into the **Out
32c40 70 75 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a 2a  puts** fields..*
32c50 2a 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72  *.** The aConstr
32c60 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 72 65 63  aint[] array rec
32c70 6f 72 64 73 20 57 48 45 52 45 20 63 6c 61 75 73  ords WHERE claus
32c80 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 66  e constraints of
32c90 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a   the form:.**.**
32ca0 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e 20 4f 50 20   <pre>column OP 
32cb0 65 78 70 72 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a  expr</pre>.**.**
32cc0 20 77 68 65 72 65 20 4f 50 20 69 73 20 3d 2c 20   where OP is =, 
32cd0 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67 74  &lt;, &lt;=, &gt
32ce0 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e 20 20 54 68  ;, or &gt;=.  Th
32cf0 65 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65  e particular ope
32d00 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73 74 6f 72  rator is.** stor
32d10 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e  ed in aConstrain
32d20 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 20 69 6e 64  t[].op.  The ind
32d30 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ex of the column
32d40 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a   is stored in.**
32d50 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 69   aConstraint[].i
32d60 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f 6e 73 74 72  Column.  aConstr
32d70 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73  aint[].usable is
32d80 20 54 52 55 45 20 69 66 20 74 68 65 0a 2a 2a 20   TRUE if the.** 
32d90 65 78 70 72 20 6f 6e 20 74 68 65 20 72 69 67 68  expr on the righ
32da0 74 2d 68 61 6e 64 20 73 69 64 65 20 63 61 6e 20  t-hand side can 
32db0 62 65 20 65 76 61 6c 75 61 74 65 64 20 28 61 6e  be evaluated (an
32dc0 64 20 74 68 75 73 20 74 68 65 20 63 6f 6e 73 74  d thus the const
32dd0 72 61 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61 62  raint.** is usab
32de0 6c 65 29 20 61 6e 64 20 66 61 6c 73 65 20 69 66  le) and false if
32df0 20 69 74 20 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a   it cannot..**.*
32e00 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20  * The optimizer 
32e10 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e  automatically in
32e20 76 65 72 74 73 20 74 65 72 6d 73 20 6f 66 20 74  verts terms of t
32e30 68 65 20 66 6f 72 6d 20 22 65 78 70 72 20 4f 50  he form "expr OP
32e40 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e 64 20   column".** and 
32e50 6d 61 6b 65 73 20 6f 74 68 65 72 20 73 69 6d 70  makes other simp
32e60 6c 69 66 69 63 61 74 69 6f 6e 73 20 74 6f 20 74  lifications to t
32e70 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
32e80 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f  in an attempt to
32e90 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61 6e 79 20  .** get as many 
32ea0 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
32eb0 6d 73 20 69 6e 74 6f 20 74 68 65 20 66 6f 72 6d  ms into the form
32ec0 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 61 73 20   shown above as 
32ed0 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 54 68 65  possible..** The
32ee0 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61   aConstraint[] a
32ef0 72 72 61 79 20 6f 6e 6c 79 20 72 65 70 6f 72 74  rray only report
32f00 73 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  s WHERE clause t
32f10 65 72 6d 73 20 69 6e 20 74 68 65 20 63 6f 72 72  erms in the corr
32f20 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 74 68 61 74  ect.** form that
32f30 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 61   refer to the pa
32f40 72 74 69 63 75 6c 61 72 20 76 69 72 74 75 61 6c  rticular virtual
32f50 20 74 61 62 6c 65 20 62 65 69 6e 67 20 71 75 65   table being que
32f60 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f  ried..**.** Info
32f70 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
32f80 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
32f90 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  e is stored in a
32fa0 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a 2a 20 45 61  OrderBy[]..** Ea
32fb0 63 68 20 74 65 72 6d 20 6f 66 20 61 4f 72 64 65  ch term of aOrde
32fc0 72 42 79 20 72 65 63 6f 72 64 73 20 61 20 63 6f  rBy records a co
32fd0 6c 75 6d 6e 20 6f 66 20 74 68 65 20 4f 52 44 45  lumn of the ORDE
32fe0 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  R BY clause..**.
32ff0 2a 2a 20 54 68 65 20 5b 78 42 65 73 74 49 6e 64  ** The [xBestInd
33000 65 78 5d 20 6d 65 74 68 6f 64 20 6d 75 73 74 20  ex] method must 
33010 66 69 6c 6c 20 61 43 6f 6e 73 74 72 61 69 6e 74  fill aConstraint
33020 55 73 61 67 65 5b 5d 20 77 69 74 68 20 69 6e 66  Usage[] with inf
33030 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75  ormation.** abou
33040 74 20 77 68 61 74 20 70 61 72 61 6d 65 74 65 72  t what parameter
33050 73 20 74 6f 20 70 61 73 73 20 74 6f 20 78 46 69  s to pass to xFi
33060 6c 74 65 72 2e 20 20 49 66 20 61 72 67 76 49 6e  lter.  If argvIn
33070 64 65 78 3e 30 20 74 68 65 6e 0a 2a 2a 20 74 68  dex>0 then.** th
33080 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
33090 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70  e of the corresp
330a0 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 74 72 61 69  onding aConstrai
330b0 6e 74 5b 5d 20 69 73 20 65 76 61 6c 75 61 74 65  nt[] is evaluate
330c0 64 0a 2a 2a 20 61 6e 64 20 62 65 63 6f 6d 65 73  d.** and becomes
330d0 20 74 68 65 20 61 72 67 76 49 6e 64 65 78 2d 74   the argvIndex-t
330e0 68 20 65 6e 74 72 79 20 69 6e 20 61 72 67 76 2e  h entry in argv.
330f0 20 20 49 66 20 61 43 6f 6e 73 74 72 61 69 6e 74    If aConstraint
33100 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20  Usage[].omit.** 
33110 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
33120 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  e constraint is 
33130 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 66 75  assumed to be fu
33140 6c 6c 79 20 68 61 6e 64 6c 65 64 20 62 79 20 74  lly handled by t
33150 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61  he.** virtual ta
33160 62 6c 65 20 61 6e 64 20 69 73 20 6e 6f 74 20 63  ble and is not c
33170 68 65 63 6b 65 64 20 61 67 61 69 6e 20 62 79 20  hecked again by 
33180 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  SQLite..**.** Th
33190 65 20 69 64 78 4e 75 6d 20 61 6e 64 20 69 64 78  e idxNum and idx
331a0 50 74 72 20 76 61 6c 75 65 73 20 61 72 65 20 72  Ptr values are r
331b0 65 63 6f 72 64 65 64 20 61 6e 64 20 70 61 73 73  ecorded and pass
331c0 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b  ed into the.** [
331d0 78 46 69 6c 74 65 72 5d 20 6d 65 74 68 6f 64 2e  xFilter] method.
331e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  .** [sqlite3_fre
331f0 65 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20  e()] is used to 
33200 66 72 65 65 20 69 64 78 50 74 72 20 69 66 20 61  free idxPtr if a
33210 6e 64 20 6f 6e 6c 79 20 69 66 66 0a 2a 2a 20 6e  nd only iff.** n
33220 65 65 64 54 6f 46 72 65 65 49 64 78 50 74 72 20  eedToFreeIdxPtr 
33230 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  is true..**.** T
33240 68 65 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  he orderByConsum
33250 65 64 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 75  ed means that ou
33260 74 70 75 74 20 66 72 6f 6d 20 5b 78 46 69 6c 74  tput from [xFilt
33270 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 77 69 6c 6c  er]/[xNext] will
33280 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65   occur in.** the
33290 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 74   correct order t
332a0 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f 52  o satisfy the OR
332b0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73 6f  DER BY clause so
332c0 20 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61 74   that no separat
332d0 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74 65  e.** sorting ste
332e0 70 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a  p is required..*
332f0 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74  *.** The estimat
33300 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73 20  edCost value is 
33310 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74  an estimate of t
33320 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67  he cost of doing
33330 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c   the.** particul
33340 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75  ar lookup.  A fu
33350 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61 62  ll scan of a tab
33360 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69 65  le with N entrie
33370 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a  s should have.**
33380 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41   a cost of N.  A
33390 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20 6f   binary search o
333a0 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20 65  f a table of N e
333b0 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61  ntries should ha
333c0 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20  ve a.** cost of 
333d0 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f  approximately lo
333e0 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  g(N)..*/.struct 
333f0 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
33400 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 75 74 73  fo {.  /* Inputs
33410 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74   */.  int nConst
33420 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  raint;          
33430 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
33440 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73 74 72  tries in aConstr
33450 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72 75 63 74  aint */.  struct
33460 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
33470 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 20 20 20  onstraint {.    
33480 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
33490 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
334a0 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d 68 61 6e  lumn on left-han
334b0 64 20 73 69 64 65 20 6f 66 20 63 6f 6e 73 74 72  d side of constr
334c0 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 75 6e 73  aint */.     uns
334d0 69 67 6e 65 64 20 63 68 61 72 20 6f 70 3b 20 20  igned char op;  
334e0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72         /* Constr
334f0 61 69 6e 74 20 6f 70 65 72 61 74 6f 72 20 2a 2f  aint operator */
33500 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  .     unsigned c
33510 68 61 72 20 75 73 61 62 6c 65 3b 20 20 20 20 20  har usable;     
33520 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
33530 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75 73  constraint is us
33540 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 69 6e 74  able */.     int
33550 20 69 54 65 72 6d 4f 66 66 73 65 74 3b 20 20 20   iTermOffset;   
33560 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 69         /* Used i
33570 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78 42 65 73  nternally - xBes
33580 74 49 6e 64 65 78 20 73 68 6f 75 6c 64 20 69 67  tIndex should ig
33590 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f  nore */.  } *aCo
335a0 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20  nstraint;       
335b0 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6f 66       /* Table of
335c0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f   WHERE clause co
335d0 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69  nstraints */.  i
335e0 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20  nt nOrderBy;    
335f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
33600 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
33610 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
33620 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  use */.  struct 
33630 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72  sqlite3_index_or
33640 64 65 72 62 79 20 7b 0a 20 20 20 20 20 69 6e 74  derby {.     int
33650 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   iColumn;       
33660 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
33670 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20   number */.     
33680 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 65  unsigned char de
33690 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  sc;       /* Tru
336a0 65 20 66 6f 72 20 44 45 53 43 2e 20 20 46 61 6c  e for DESC.  Fal
336b0 73 65 20 66 6f 72 20 41 53 43 2e 20 2a 2f 0a 20  se for ASC. */. 
336c0 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b 20 20 20   } *aOrderBy;   
336d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
336e0 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
336f0 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75 74 70 75  se */.  /* Outpu
33700 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  ts */.  struct s
33710 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
33720 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 7b 0a  straint_usage {.
33730 20 20 20 20 69 6e 74 20 61 72 67 76 49 6e 64 65      int argvInde
33740 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
33750 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72 61 69 6e  if >0, constrain
33760 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 72 67  t is part of arg
33770 76 20 74 6f 20 78 46 69 6c 74 65 72 20 2a 2f 0a  v to xFilter */.
33780 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
33790 72 20 6f 6d 69 74 3b 20 20 20 20 20 20 2f 2a 20  r omit;      /* 
337a0 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61 20 74 65  Do not code a te
337b0 73 74 20 66 6f 72 20 74 68 69 73 20 63 6f 6e 73  st for this cons
337c0 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d 20 2a 61  traint */.  } *a
337d0 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b  ConstraintUsage;
337e0 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20 20  .  int idxNum;  
337f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33800 20 4e 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20   Number used to 
33810 69 64 65 6e 74 69 66 79 20 74 68 65 20 69 6e 64  identify the ind
33820 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 2a 69 64  ex */.  char *id
33830 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20  xStr;           
33840 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c 20 70 6f     /* String, po
33850 73 73 69 62 6c 79 20 6f 62 74 61 69 6e 65 64 20  ssibly obtained 
33860 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
33870 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 65  loc */.  int nee
33880 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 20 20  dToFreeIdxStr;  
33890 20 20 20 20 2f 2a 20 46 72 65 65 20 69 64 78 53      /* Free idxS
338a0 74 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  tr using sqlite3
338b0 5f 66 72 65 65 28 29 20 69 66 20 74 72 75 65 20  _free() if true 
338c0 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42 79  */.  int orderBy
338d0 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 20 20 20  Consumed;       
338e0 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 70 75  /* True if outpu
338f0 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 72 64  t is already ord
33900 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  ered */.  double
33910 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b 20   estimatedCost; 
33920 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65       /* Estimate
33930 64 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20  d cost of using 
33940 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b  this index */.};
33950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
33960 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
33970 5f 45 51 20 20 20 20 32 0a 23 64 65 66 69 6e 65  _EQ    2.#define
33980 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
33990 4e 53 54 52 41 49 4e 54 5f 47 54 20 20 20 20 34  NSTRAINT_GT    4
339a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
339b0 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
339c0 5f 4c 45 20 20 20 20 38 0a 23 64 65 66 69 6e 65  _LE    8.#define
339d0 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
339e0 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20 20 20 31  NSTRAINT_LT    1
339f0 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  6.#define SQLITE
33a00 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
33a10 54 5f 47 45 20 20 20 20 33 32 0a 23 64 65 66 69  T_GE    32.#defi
33a20 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ne SQLITE_INDEX_
33a30 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48  CONSTRAINT_MATCH
33a40 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33   64../*.** CAPI3
33a50 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 20  REF: Register A 
33a60 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49 6d  Virtual Table Im
33a70 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 7b 48 31  plementation {H1
33a80 38 32 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a  8200} <S20400>.*
33a90 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
33aa0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
33ab0 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 67  e is used to reg
33ac0 69 73 74 65 72 20 61 20 6e 65 77 20 5b 76 69 72  ister a new [vir
33ad0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
33ae0 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 4d 6f 64 75  e] name..** Modu
33af0 6c 65 20 6e 61 6d 65 73 20 6d 75 73 74 20 62 65  le names must be
33b00 20 72 65 67 69 73 74 65 72 65 64 20 62 65 66 6f   registered befo
33b10 72 65 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20 61  re.** creating a
33b20 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20 74 61   new [virtual ta
33b30 62 6c 65 5d 20 75 73 69 6e 67 20 74 68 65 20 6d  ble] using the m
33b40 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65  odule, or before
33b50 20 75 73 69 6e 67 20 61 0a 2a 2a 20 70 72 65 65   using a.** pree
33b60 78 69 73 74 69 6e 67 20 5b 76 69 72 74 75 61 6c  xisting [virtual
33b70 20 74 61 62 6c 65 5d 20 66 6f 72 20 74 68 65 20   table] for the 
33b80 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  module..**.** Th
33b90 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69 73  e module name is
33ba0 20 72 65 67 69 73 74 65 72 65 64 20 6f 6e 20 74   registered on t
33bb0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
33bc0 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69  nection] specifi
33bd0 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 66 69 72  ed.** by the fir
33be0 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54  st parameter.  T
33bf0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d  he name of the m
33c00 6f 64 75 6c 65 20 69 73 20 67 69 76 65 6e 20 62  odule is given b
33c10 79 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64  y the .** second
33c20 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65   parameter.  The
33c30 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
33c40 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
33c50 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  .** the implemen
33c60 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 76  tation of the [v
33c70 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
33c80 75 6c 65 5d 2e 20 20 20 54 68 65 20 66 6f 75 72  ule].   The four
33c90 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  th.** parameter 
33ca0 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20  is an arbitrary 
33cb0 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69 6e  client data poin
33cc0 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73 73  ter that is pass
33cd0 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 6e  ed through.** in
33ce0 74 6f 20 74 68 65 20 5b 78 43 72 65 61 74 65 5d  to the [xCreate]
33cf0 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d 20   and [xConnect] 
33d00 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 76  methods of the v
33d10 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
33d20 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61 20 6e 65  ule.** when a ne
33d30 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  w virtual table 
33d40 69 73 20 62 65 20 62 65 69 6e 67 20 63 72 65 61  is be being crea
33d50 74 65 64 20 6f 72 20 72 65 69 6e 69 74 69 61 6c  ted or reinitial
33d60 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ized..**.** This
33d70 20 69 6e 74 65 72 66 61 63 65 20 68 61 73 20 65   interface has e
33d80 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20  xactly the same 
33d90 65 66 66 65 63 74 20 61 73 20 63 61 6c 6c 69 6e  effect as callin
33da0 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72  g.** [sqlite3_cr
33db0 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 29  eate_module_v2()
33dc0 5d 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 63 6c  ] with a NULL cl
33dd0 69 65 6e 74 20 64 61 74 61 20 64 65 73 74 72 75  ient data destru
33de0 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ctor..*/.SQLITE_
33df0 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
33e00 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69  IMENTAL int sqli
33e10 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
33e20 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
33e30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
33e40 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63  /* SQLite connec
33e50 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72  tion to register
33e60 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a   module with */.
33e70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
33e80 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ame,         /* 
33e90 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75  Name of the modu
33ea0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71  le */.  const sq
33eb0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 2c  lite3_module *p,
33ec0 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f     /* Methods fo
33ed0 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  r the module */.
33ee0 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74 44    void *pClientD
33ef0 61 74 61 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ata          /* 
33f00 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20  Client data for 
33f10 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74  xCreate/xConnect
33f20 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.);../*.** CA
33f30 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65 72  PI3REF: Register
33f40 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65   A Virtual Table
33f50 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
33f60 7b 48 31 38 32 31 30 7d 20 3c 53 32 30 34 30 30  {H18210} <S20400
33f70 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
33f80 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  L.**.** This rou
33f90 74 69 6e 65 20 69 73 20 69 64 65 6e 74 69 63 61  tine is identica
33fa0 6c 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  l to the [sqlite
33fb0 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28  3_create_module(
33fc0 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a 2a 20 65 78  )] method,.** ex
33fd0 63 65 70 74 20 74 68 61 74 20 69 74 20 68 61 73  cept that it has
33fe0 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d 65   an extra parame
33ff0 74 65 72 20 74 6f 20 73 70 65 63 69 66 79 20 0a  ter to specify .
34000 2a 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  ** a destructor 
34010 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65  function for the
34020 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69   client data poi
34030 6e 74 65 72 2e 20 20 53 51 4c 69 74 65 20 77 69  nter.  SQLite wi
34040 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65  ll.** invoke the
34050 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
34060 74 69 6f 6e 20 28 69 66 20 69 74 20 69 73 20 6e  tion (if it is n
34070 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e 20 53 51  ot NULL) when SQ
34080 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65  Lite.** no longe
34090 72 20 6e 65 65 64 73 20 74 68 65 20 70 43 6c 69  r needs the pCli
340a0 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 65 72 2e  entData pointer.
340b0 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49    .*/.SQLITE_API
340c0 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45   SQLITE_EXPERIME
340d0 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33  NTAL int sqlite3
340e0 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  _create_module_v
340f0 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  2(.  sqlite3 *db
34100 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
34110 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63  /* SQLite connec
34120 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72  tion to register
34130 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a   module with */.
34140 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
34150 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ame,         /* 
34160 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75  Name of the modu
34170 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71  le */.  const sq
34180 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 2c  lite3_module *p,
34190 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f     /* Methods fo
341a0 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  r the module */.
341b0 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74 44    void *pClientD
341c0 61 74 61 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ata,         /* 
341d0 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20  Client data for 
341e0 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74  xCreate/xConnect
341f0 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65 73   */.  void(*xDes
34200 74 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20 20  troy)(void*)    
34210 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72   /* Module destr
34220 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a  uctor function *
34230 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.);../*.** CAPI
34240 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61  3REF: Virtual Ta
34250 62 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62 6a  ble Instance Obj
34260 65 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53 32  ect {H18010} <S2
34270 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0400>.** KEYWORD
34280 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 0a  S: sqlite3_vtab.
34290 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
342a0 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b 76 69 72  **.** Every [vir
342b0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
342c0 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  e] implementatio
342d0 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61 73  n uses a subclas
342e0 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  s.** of the foll
342f0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
34300 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61  to describe a pa
34310 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63  rticular instanc
34320 65 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 76 69 72  e.** of the [vir
34330 74 75 61 6c 20 74 61 62 6c 65 5d 2e 20 20 45 61  tual table].  Ea
34340 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 6c 6c  ch subclass will
34350 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 64 20  .** be tailored 
34360 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63 20  to the specific 
34370 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d 6f 64  needs of the mod
34380 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
34390 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f  on..** The purpo
343a0 73 65 20 6f 66 20 74 68 69 73 20 73 75 70 65 72  se of this super
343b0 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65 66 69  class is to defi
343c0 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65 6c 64  ne certain field
343d0 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63 6f  s that are.** co
343e0 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f 64 75  mmon to all modu
343f0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
34400 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61  ns..**.** Virtua
34410 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f 64 73  l tables methods
34420 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72 72 6f   can set an erro
34430 72 20 6d 65 73 73 61 67 65 20 62 79 20 61 73 73  r message by ass
34440 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74 72 69  igning a.** stri
34450 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ng obtained from
34460 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74   [sqlite3_mprint
34470 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73 67 2e  f()] to zErrMsg.
34480 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73 68 6f    The method sho
34490 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61 72 65  uld.** take care
344a0 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f 72 20   that any prior 
344b0 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 64 20  string is freed 
344c0 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71  by a call to [sq
344d0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a 2a 2a  lite3_free()].**
344e0 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 67 6e   prior to assign
344f0 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 6e 67  ing a new string
34500 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 41 66   to zErrMsg.  Af
34510 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d 65  ter the error me
34520 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 6c 69  ssage.** is deli
34530 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 65 20  vered up to the 
34540 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 74 69  client applicati
34550 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 20 77  on, the string w
34560 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63  ill be automatic
34570 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 62 79  ally.** freed by
34580 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20   sqlite3_free() 
34590 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 67 20  and the zErrMsg 
345a0 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 7a 65  field will be ze
345b0 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  roed..*/.struct 
345c0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 7b 0a 20  sqlite3_vtab {. 
345d0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
345e0 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20  odule *pModule; 
345f0 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 66   /* The module f
34600 6f 72 20 74 68 69 73 20 76 69 72 74 75 61 6c 20  or this virtual 
34610 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
34620 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
34630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
34640 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f  ed internally */
34650 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
34660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
34670 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
34680 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  age from sqlite3
34690 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 20 20  _mprintf() */.  
346a0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
346b0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
346c0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
346d0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
346e0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ields */.};../*.
346f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72  ** CAPI3REF: Vir
34700 74 75 61 6c 20 54 61 62 6c 65 20 43 75 72 73 6f  tual Table Curso
34710 72 20 4f 62 6a 65 63 74 20 20 7b 48 31 38 30 32  r Object  {H1802
34720 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b  0} <S20400>.** K
34730 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33  EYWORDS: sqlite3
34740 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 76 69  _vtab_cursor {vi
34750 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73  rtual table curs
34760 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  or}.** EXPERIMEN
34770 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  TAL.**.** Every 
34780 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d  [virtual table m
34790 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74  odule] implement
347a0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62  ation uses a sub
347b0 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20  class of the.** 
347c0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
347d0 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20  ure to describe 
347e0 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69  cursors that poi
347f0 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b  nt into the.** [
34800 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 61  virtual table] a
34810 6e 64 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74  nd are used.** t
34820 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  o loop through t
34830 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
34840 2e 20 20 43 75 72 73 6f 72 73 20 61 72 65 20 63  .  Cursors are c
34850 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 65  reated using the
34860 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64  .** [sqlite3_mod
34870 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78 4f 70 65  ule.xOpen | xOpe
34880 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  n] method of the
34890 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61 72 65 20   module and are 
348a0 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 62 79 20  destroyed.** by 
348b0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64  the [sqlite3_mod
348c0 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20 78 43 6c  ule.xClose | xCl
348d0 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20 20 43 75  ose] method.  Cu
348e0 73 73 6f 72 73 20 61 72 65 20 75 73 65 64 0a 2a  ssors are used.*
348f0 2a 20 62 79 20 74 68 65 20 5b 78 46 69 6c 74 65  * by the [xFilte
34900 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20 5b 78 45  r], [xNext], [xE
34910 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e 5d 2c 20  of], [xColumn], 
34920 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20 6d 65 74  and [xRowid] met
34930 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 6d  hods.** of the m
34940 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 6d 6f 64  odule.  Each mod
34950 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
34960 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e 65 0a 2a  on will define.*
34970 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
34980 20 61 20 63 75 72 73 6f 72 20 73 74 72 75 63 74   a cursor struct
34990 75 72 65 20 74 6f 20 73 75 69 74 20 69 74 73 20  ure to suit its 
349a0 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  own needs..**.**
349b0 20 54 68 69 73 20 73 75 70 65 72 63 6c 61 73 73   This superclass
349c0 20 65 78 69 73 74 73 20 69 6e 20 6f 72 64 65 72   exists in order
349d0 20 74 6f 20 64 65 66 69 6e 65 20 66 69 65 6c 64   to define field
349e0 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20  s of the cursor 
349f0 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 6f 6d 6d  that.** are comm
34a00 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c 65 6d  on to all implem
34a10 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  entations..*/.st
34a20 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61  ruct sqlite3_vta
34a30 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  b_cursor {.  sql
34a40 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
34a50 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61  ;      /* Virtua
34a60 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20  l table of this 
34a70 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56  cursor */.  /* V
34a80 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
34a90 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c  lementations wil
34aa0 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20  l typically add 
34ab0 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  additional field
34ac0 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  s */.};../*.** C
34ad0 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65  API3REF: Declare
34ae0 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41   The Schema Of A
34af0 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b   Virtual Table {
34b00 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e  H18280} <S20400>
34b10 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
34b20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 43 72 65  .**.** The [xCre
34b30 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65  ate] and [xConne
34b40 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 61  ct] methods of a
34b50 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62  .** [virtual tab
34b60 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 6c 6c 20  le module] call 
34b70 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 0a 2a  this interface.*
34b80 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 74 68 65  * to declare the
34b90 20 66 6f 72 6d 61 74 20 28 74 68 65 20 6e 61 6d   format (the nam
34ba0 65 73 20 61 6e 64 20 64 61 74 61 74 79 70 65 73  es and datatypes
34bb0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 29   of the columns)
34bc0 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75   of.** the virtu
34bd0 61 6c 20 74 61 62 6c 65 73 20 74 68 65 79 20 69  al tables they i
34be0 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  mplement..*/.SQL
34bf0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
34c00 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20  XPERIMENTAL int 
34c10 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
34c20 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c 20 63  vtab(sqlite3*, c
34c30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 51 4c 29  onst char *zSQL)
34c40 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
34c50 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75  F: Overload A Fu
34c60 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72  nction For A Vir
34c70 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 33  tual Table {H183
34c80 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20  00} <S20400>.** 
34c90 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
34ca0 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  ** Virtual table
34cb0 73 20 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c  s can provide al
34cc0 74 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d  ternative implem
34cd0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e  entations of fun
34ce0 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20  ctions.** using 
34cf0 74 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 74 69  the [xFindFuncti
34d00 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  on] method of th
34d10 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e [virtual table
34d20 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a 20 42   module].  .** B
34d30 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f  ut global versio
34d40 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75 6e 63  ns of those func
34d50 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20 65 78  tions.** must ex
34d60 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ist in order to 
34d70 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e 0a 2a  be overloaded..*
34d80 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61  *.** This API ma
34d90 6b 65 73 20 73 75 72 65 20 61 20 67 6c 6f 62 61  kes sure a globa
34da0 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 20 66  l version of a f
34db0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 70  unction with a p
34dc0 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 6e 61 6d  articular.** nam
34dd0 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20  e and number of 
34de0 70 61 72 61 6d 65 74 65 72 73 20 65 78 69 73 74  parameters exist
34df0 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 68 20 66  s.  If no such f
34e00 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 0a 2a  unction exists.*
34e10 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 41 50  * before this AP
34e20 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 20 6e  I is called, a n
34e30 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  ew function is c
34e40 72 65 61 74 65 64 2e 20 20 54 68 65 20 69 6d 70  reated.  The imp
34e50 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f  lementation.** o
34e60 66 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74 69  f the new functi
34e70 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 73 65 73  on always causes
34e80 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f   an exception to
34e90 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 53 6f 0a   be thrown.  So.
34ea0 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74  ** the new funct
34eb0 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f 64 20  ion is not good 
34ec0 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 62 79 20  for anything by 
34ed0 69 74 73 65 6c 66 2e 20 20 49 74 73 20 6f 6e 6c  itself.  Its onl
34ee0 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69 73 20  y.** purpose is 
34ef0 74 6f 20 62 65 20 61 20 70 6c 61 63 65 68 6f 6c  to be a placehol
34f00 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  der function tha
34f10 74 20 63 61 6e 20 62 65 20 6f 76 65 72 6c 6f 61  t can be overloa
34f20 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b 76 69 72  ded.** by a [vir
34f30 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a 2f 0a  tual table]..*/.
34f40 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
34f50 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69  E_EXPERIMENTAL i
34f60 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  nt sqlite3_overl
34f70 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c  oad_function(sql
34f80 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
34f90 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e  r *zFuncName, in
34fa0 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t nArg);../*.** 
34fb0 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  The interface to
34fc0 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62   the virtual-tab
34fd0 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66  le mechanism def
34fe0 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b  ined above (back
34ff0 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d   up.** to a comm
35000 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73  ent remarkably s
35010 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f  imilar to this o
35020 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ne) is currently
35030 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74   considered.** t
35040 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61  o be experimenta
35050 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63  l.  The interfac
35060 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  e might change i
35070 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77  n incompatible w
35080 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  ays..** If this 
35090 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72  is a problem for
350a0 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65   you, do not use
350b0 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 61   the interface a
350c0 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a  t this time..**.
350d0 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 72 74  ** When the virt
350e0 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e  ual-table mechan
350f0 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20  ism stabilizes, 
35100 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20  we will declare 
35110 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  the.** interface
35120 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20   fixed, support 
35130 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c  it indefinitely,
35140 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73   and remove this
35150 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a   comment..**.***
35160 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  *** EXPERIMENTAL
35170 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68   - subject to ch
35180 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74  ange without not
35190 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ice ************
351a0 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  **.*/../*.** CAP
351b0 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20  I3REF: A Handle 
351c0 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20  To An Open BLOB 
351d0 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30  {H17800} <S30230
351e0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
351f0 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c  BLOB handle} {BL
35200 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a  OB handles}.**.*
35210 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
35220 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70   this object rep
35230 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20  resents an open 
35240 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a  BLOB on which.**
35250 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
35260 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61  pen | incrementa
35270 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20  l BLOB I/O] can 
35280 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a  be performed..**
35290 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73   Objects of this
352a0 20 74 79 70 65 20 61 72 65 20 63 72 65 61 74 65   type are create
352b0 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c  d by [sqlite3_bl
352c0 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e  ob_open()].** an
352d0 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b  d destroyed by [
352e0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
352f0 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73  se()]..** The [s
35300 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64  qlite3_blob_read
35310 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
35320 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69  _blob_write()] i
35330 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e  nterfaces.** can
35340 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 64   be used to read
35350 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20   or write small 
35360 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74  subsections of t
35370 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20  he BLOB..** The 
35380 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
35390 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tes()] interface
353a0 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a   returns the siz
353b0 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e  e of the BLOB in
353c0 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64   bytes..*/.typed
353d0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
353e0 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62  3_blob sqlite3_b
353f0 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lob;../*.** CAPI
35400 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f  3REF: Open A BLO
35410 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61  B For Incrementa
35420 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c  l I/O {H17810} <
35430 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30230>.**.** Th
35440 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70  is interfaces op
35450 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64  ens a [BLOB hand
35460 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20  le | handle] to 
35470 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64  the BLOB located
35480 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c  .** in row iRow,
35490 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c   column zColumn,
354a0 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e   table zTable in
354b0 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a   database zDb;.*
354c0 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * in other words
354d0 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20  , the same BLOB 
354e0 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65  that would be se
354f0 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a  lected by:.**.**
35500 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45   <pre>.**     SE
35510 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f  LECT zColumn FRO
35520 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45  M zDb.zTable WHE
35530 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f  RE [rowid] = iRo
35540 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e  w;.** </pre> {EN
35550 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  D}.**.** If the 
35560 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
35570 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
35580 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65   the BLOB is ope
35590 6e 65 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a 20  ned for read.** 
355a0 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73 73  and write access
355b0 2e 20 49 66 20 69 74 20 69 73 20 7a 65 72 6f 2c  . If it is zero,
355c0 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70 65   the BLOB is ope
355d0 6e 65 64 20 66 6f 72 20 72 65 61 64 20 61 63 63  ned for read acc
355e0 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ess..**.** Note 
355f0 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73  that the databas
35600 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68  e name is not th
35610 65 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20  e filename that 
35620 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20  contains.** the 
35630 64 61 74 61 62 61 73 65 20 62 75 74 20 72 61 74  database but rat
35640 68 65 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63  her the symbolic
35650 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
35660 61 62 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73  abase that.** is
35670 20 61 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74   assigned when t
35680 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63  he database is c
35690 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b  onnected using [
356a0 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20  ATTACH]..** For 
356b0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
356c0 65 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61  e file, the data
356d0 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61  base name is "ma
356e0 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50  in"..** For TEMP
356f0 20 74 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74   tables, the dat
35700 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74  abase name is "t
35710 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73  emp"..**.** On s
35720 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f  uccess, [SQLITE_
35730 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  OK] is returned 
35740 61 6e 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f  and the new [BLO
35750 42 20 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69  B handle] is wri
35760 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c  tten.** to *ppBl
35770 6f 62 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e  ob. Otherwise an
35780 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
35790 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e   returned and an
357a0 79 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 0a  y value written.
357b0 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 20 73 68  ** to *ppBlob sh
357c0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64  ould not be used
357d0 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   by the caller..
357e0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
357f0 20 73 65 74 73 20 74 68 65 20 5b 64 61 74 61 62   sets the [datab
35800 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
35810 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d  error code and m
35820 65 73 73 61 67 65 0a 2a 2a 20 61 63 63 65 73 73  essage.** access
35830 69 62 6c 65 20 76 69 61 20 5b 73 71 6c 69 74 65  ible via [sqlite
35840 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 61 6e 64  3_errcode()] and
35850 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
35860 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ()]..**.** If th
35870 65 20 72 6f 77 20 74 68 61 74 20 61 20 42 4c 4f  e row that a BLO
35880 42 20 68 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20  B handle points 
35890 74 6f 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62  to is modified b
358a0 79 20 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d  y an.** [UPDATE]
358b0 2c 20 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62  , [DELETE], or b
358c0 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20  y [ON CONFLICT] 
358d0 73 69 64 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20  side-effects.** 
358e0 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 68 61  then the BLOB ha
358f0 6e 64 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 61  ndle is marked a
35900 73 20 22 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20  s "expired"..** 
35910 54 68 69 73 20 69 73 20 74 72 75 65 20 69 66 20  This is true if 
35920 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  any column of th
35930 65 20 72 6f 77 20 69 73 20 63 68 61 6e 67 65 64  e row is changed
35940 2c 20 65 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a  , even a column.
35950 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68  ** other than th
35960 65 20 6f 6e 65 20 74 68 65 20 42 4c 4f 42 20 68  e one the BLOB h
35970 61 6e 64 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e  andle is open on
35980 2e 0a 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73  ..** Calls to [s
35990 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64  qlite3_blob_read
359a0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
359b0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66  _blob_write()] f
359c0 6f 72 0a 2a 2a 20 61 20 65 78 70 69 72 65 64 20  or.** a expired 
359d0 42 4c 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c  BLOB handle fail
359e0 20 77 69 74 68 20 61 6e 20 72 65 74 75 72 6e 20   with an return 
359f0 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f  code of [SQLITE_
35a00 41 42 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67  ABORT]..** Chang
35a10 65 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  es written into 
35a20 61 20 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20  a BLOB prior to 
35a30 74 68 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e  the BLOB expirin
35a40 67 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c  g are not.** rol
35a50 6c 62 61 63 6b 20 62 79 20 74 68 65 20 65 78 70  lback by the exp
35a60 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 42  iration of the B
35a70 4c 4f 42 2e 20 20 53 75 63 68 20 63 68 61 6e 67  LOB.  Such chang
35a80 65 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c  es will eventual
35a90 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20  ly.** commit if 
35aa0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
35ab0 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d  continues to com
35ac0 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52  pletion..**.** R
35ad0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
35ae0 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 38 31 34  [H17813] [H17814
35af0 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 31 37 38  ] [H17816] [H178
35b00 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48 31  19] [H17821] [H1
35b10 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  7824].*/.SQLITE_
35b20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
35b30 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c  blob_open(.  sql
35b40 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63  ite3*,.  const c
35b50 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73  har *zDb,.  cons
35b60 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a  t char *zTable,.
35b70 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
35b80 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 33  olumn,.  sqlite3
35b90 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 69  _int64 iRow,.  i
35ba0 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69  nt flags,.  sqli
35bb0 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f  te3_blob **ppBlo
35bc0 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  b.);../*.** CAPI
35bd0 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 4c  3REF: Close A BL
35be0 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 33  OB Handle {H1783
35bf0 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a  0} <S30230>.**.*
35c00 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 6e  * Closes an open
35c10 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a   [BLOB handle]..
35c20 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 20  **.** Closing a 
35c30 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 65  BLOB shall cause
35c40 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
35c50 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d  nsaction to comm
35c60 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 61  it.** if there a
35c70 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f 42  re no other BLOB
35c80 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 72  s, no pending pr
35c90 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
35ca0 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61  s, and the.** da
35cb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
35cc0 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f 6d  n is in [autocom
35cd0 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66  mit mode]..** If
35ce0 20 61 6e 79 20 77 72 69 74 65 73 20 77 65 72 65   any writes were
35cf0 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42 4c 4f   made to the BLO
35d00 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20 62 65  B, they might be
35d10 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 0a 2a   held in cache.*
35d20 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f 73  * until the clos
35d30 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 74  e operation if t
35d40 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 20 7b 45  hey will fit. {E
35d50 4e 44 7d 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e  ND}.**.** Closin
35d60 67 20 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e  g the BLOB often
35d70 20 66 6f 72 63 65 73 20 74 68 65 20 63 68 61 6e   forces the chan
35d80 67 65 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69  ges.** out to di
35d90 73 6b 20 61 6e 64 20 73 6f 20 69 66 20 61 6e 79  sk and so if any
35da0 20 49 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 75   I/O errors occu
35db0 72 2c 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b  r, they will lik
35dc0 65 6c 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20  ely occur.** at 
35dd0 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 74 68  the time when th
35de0 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64  e BLOB is closed
35df0 2e 20 20 7b 48 31 37 38 33 33 7d 20 41 6e 79 20  .  {H17833} Any 
35e00 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75  errors that occu
35e10 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73  r during.** clos
35e20 69 6e 67 20 61 72 65 20 72 65 70 6f 72 74 65 64  ing are reported
35e30 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72   as a non-zero r
35e40 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a  eturn value..**.
35e50 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69 73 20 63  ** The BLOB is c
35e60 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f  losed unconditio
35e70 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 20  nally.  Even if 
35e80 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
35e90 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72  urns.** an error
35ea0 20 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20   code, the BLOB 
35eb0 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e  is still closed.
35ec0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
35ed0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d  nts:.** [H17833]
35ee0 20 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33   [H17836] [H1783
35ef0 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  9].*/.SQLITE_API
35f00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
35f10 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  b_close(sqlite3_
35f20 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  blob *);../*.** 
35f30 43 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e  CAPI3REF: Return
35f40 20 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20   The Size Of An 
35f50 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34  Open BLOB {H1784
35f60 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a  0} <S30230>.**.*
35f70 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 69  * Returns the si
35f80 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
35f90 68 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62  he BLOB accessib
35fa0 6c 65 20 76 69 61 20 74 68 65 20 6f 70 65 6e 0a  le via the open.
35fb0 2a 2a 20 5b 5d 42 4c 4f 42 20 68 61 6e 64 6c 65  ** []BLOB handle
35fc0 5d 20 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72  ] in its only ar
35fd0 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65  gument..**.** Re
35fe0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
35ff0 48 31 37 38 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54  H17843].*/.SQLIT
36000 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
36010 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c  3_blob_bytes(sql
36020 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f  ite3_blob *);../
36030 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
36040 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20 41 20  ead Data From A 
36050 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c  BLOB Incremental
36060 6c 79 20 7b 48 31 37 38 35 30 7d 20 3c 53 33 30  ly {H17850} <S30
36070 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  230>.**.** This 
36080 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
36090 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 66 72   to read data fr
360a0 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42  om an open [BLOB
360b0 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20 61 0a   handle] into a.
360c0 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69  ** caller-suppli
360d0 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74  ed buffer. N byt
360e0 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63  es of data are c
360f0 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66 66 65  opied into buffe
36100 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  r Z.** from the 
36110 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74  open BLOB, start
36120 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f  ing at offset iO
36130 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ffset..**.** If 
36140 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69  offset iOffset i
36150 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79  s less than N by
36160 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64  tes from the end
36170 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a   of the BLOB,.**
36180 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20   [SQLITE_ERROR] 
36190 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
361a0 6e 6f 20 64 61 74 61 20 69 73 20 72 65 61 64 2e  no data is read.
361b0 20 20 49 66 20 4e 20 6f 72 20 69 4f 66 66 73 65    If N or iOffse
361c0 74 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61  t is.** less tha
361d0 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c 49 54 45 5f  n zero, [SQLITE_
361e0 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e  ERROR] is return
361f0 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69  ed and no data i
36200 73 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e  s read..**.** An
36210 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   attempt to read
36220 20 66 72 6f 6d 20 61 6e 20 65 78 70 69 72 65 64   from an expired
36230 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66   [BLOB handle] f
36240 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20  ails with an.** 
36250 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53  error code of [S
36260 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a  QLITE_ABORT]..**
36270 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20  .** On success, 
36280 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
36290 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77  urned..** Otherw
362a0 69 73 65 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63  ise, an [error c
362b0 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65  ode] or an [exte
362c0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d  nded error code]
362d0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
362e0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
362f0 3a 0a 2a 2a 20 5b 48 31 37 38 35 33 5d 20 5b 48  :.** [H17853] [H
36300 31 37 38 35 36 5d 20 5b 48 31 37 38 35 39 5d 20  17856] [H17859] 
36310 5b 48 31 37 38 36 32 5d 20 5b 48 31 37 38 36 33  [H17862] [H17863
36320 5d 20 5b 48 31 37 38 36 35 5d 20 5b 48 31 37 38  ] [H17865] [H178
36330 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  68].*/.SQLITE_AP
36340 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
36350 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f  ob_read(sqlite3_
36360 62 6c 6f 62 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c  blob *, void *Z,
36370 20 69 6e 74 20 4e 2c 20 69 6e 74 20 69 4f 66 66   int N, int iOff
36380 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  set);../*.** CAP
36390 49 33 52 45 46 3a 20 57 72 69 74 65 20 44 61 74  I3REF: Write Dat
363a0 61 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20 49 6e  a Into A BLOB In
363b0 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37  crementally {H17
363c0 38 37 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a  870} <S30230>.**
363d0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
363e0 6e 20 69 73 20 75 73 65 64 20 74 6f 20 77 72 69  n is used to wri
363f0 74 65 20 64 61 74 61 20 69 6e 74 6f 20 61 6e 20  te data into an 
36400 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c  open [BLOB handl
36410 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a 20 63 61 6c  e] from a.** cal
36420 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62 75 66  ler-supplied buf
36430 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f 66 20  fer. N bytes of 
36440 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20  data are copied 
36450 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20  from the buffer 
36460 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6f 70  Z.** into the op
36470 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e  en BLOB, startin
36480 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66  g at offset iOff
36490 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  set..**.** If th
364a0 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20  e [BLOB handle] 
364b0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
364c0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 77 61 73  rst argument was
364d0 20 6e 6f 74 20 6f 70 65 6e 65 64 20 66 6f 72 0a   not opened for.
364e0 2a 2a 20 77 72 69 74 69 6e 67 20 28 74 68 65 20  ** writing (the 
364f0 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
36500 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  to [sqlite3_blob
36510 5f 6f 70 65 6e 28 29 5d 20 77 61 73 20 7a 65 72  _open()] was zer
36520 6f 29 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  o),.** this func
36530 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51  tion returns [SQ
36540 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a  LITE_READONLY]..
36550 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
36560 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64  ion may only mod
36570 69 66 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ify the contents
36580 20 6f 66 20 74 68 65 20 42 4c 4f 42 3b 20 69 74   of the BLOB; it
36590 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69   is.** not possi
365a0 62 6c 65 20 74 6f 20 69 6e 63 72 65 61 73 65 20  ble to increase 
365b0 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 4c  the size of a BL
365c0 4f 42 20 75 73 69 6e 67 20 74 68 69 73 20 41 50  OB using this AP
365d0 49 2e 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 20  I..** If offset 
365e0 69 4f 66 66 73 65 74 20 69 73 20 6c 65 73 73 20  iOffset is less 
365f0 74 68 61 6e 20 4e 20 62 79 74 65 73 20 66 72 6f  than N bytes fro
36600 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  m the end of the
36610 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54   BLOB,.** [SQLIT
36620 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75  E_ERROR] is retu
36630 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61  rned and no data
36640 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49 66   is written.  If
36650 20 4e 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74 68   N is.** less th
36660 61 6e 20 7a 65 72 6f 20 5b 53 51 4c 49 54 45 5f  an zero [SQLITE_
36670 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e  ERROR] is return
36680 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69  ed and no data i
36690 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a  s written..**.**
366a0 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 77   An attempt to w
366b0 72 69 74 65 20 74 6f 20 61 6e 20 65 78 70 69 72  rite to an expir
366c0 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  ed [BLOB handle]
366d0 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a   fails with an.*
366e0 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20  * error code of 
366f0 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 20  [SQLITE_ABORT]. 
36700 20 57 72 69 74 65 73 20 74 6f 20 74 68 65 20 42   Writes to the B
36710 4c 4f 42 20 74 68 61 74 20 6f 63 63 75 72 72 65  LOB that occurre
36720 64 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20  d.** before the 
36730 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78  [BLOB handle] ex
36740 70 69 72 65 64 20 61 72 65 20 6e 6f 74 20 72 6f  pired are not ro
36750 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65  lled back by the
36760 0a 2a 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 6f  .** expiration o
36770 66 20 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 68  f the handle, th
36780 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74  ough of course t
36790 68 6f 73 65 20 63 68 61 6e 67 65 73 20 6d 69 67  hose changes mig
367a0 68 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20  ht.** have been 
367b0 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 74  overwritten by t
367c0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  he statement tha
367d0 74 20 65 78 70 69 72 65 64 20 74 68 65 20 42 4c  t expired the BL
367e0 4f 42 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20  OB handle.** or 
367f0 62 79 20 6f 74 68 65 72 20 69 6e 64 65 70 65 6e  by other indepen
36800 64 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 2e  dent statements.
36810 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73  .**.** On succes
36820 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  s, SQLITE_OK is 
36830 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68  returned..** Oth
36840 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65 72 72  erwise, an  [err
36850 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b  or code] or an [
36860 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
36870 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
36880 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
36890 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 37 33  ents:.** [H17873
368a0 5d 20 5b 48 31 37 38 37 34 5d 20 5b 48 31 37 38  ] [H17874] [H178
368b0 37 35 5d 20 5b 48 31 37 38 37 36 5d 20 5b 48 31  75] [H17876] [H1
368c0 37 38 37 37 5d 20 5b 48 31 37 38 37 39 5d 20 5b  7877] [H17879] [
368d0 48 31 37 38 38 32 5d 20 5b 48 31 37 38 38 35 5d  H17882] [H17885]
368e0 0a 2a 2a 20 5b 48 31 37 38 38 38 5d 0a 2a 2f 0a  .** [H17888].*/.
368f0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
36900 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
36910 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  e(sqlite3_blob *
36920 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c  , const void *z,
36930 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66   int n, int iOff
36940 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  set);../*.** CAP
36950 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 46  I3REF: Virtual F
36960 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65 63  ile System Objec
36970 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32 30  ts {H11200} <S20
36980 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72  100>.**.** A vir
36990 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d 20  tual filesystem 
369a0 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71 6c  (VFS) is an [sql
369b0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
369c0 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65 20  .** that SQLite 
369d0 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63 74  uses to interact
369e0 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e 64  .** with the und
369f0 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e  erlying operatin
36a00 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74 20  g system.  Most 
36a10 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63 6f  SQLite builds co
36a20 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69 6e  me with a.** sin
36a30 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53 20  gle default VFS 
36a40 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 69  that is appropri
36a50 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73 74  ate for the host
36a60 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e 65   computer..** Ne
36a70 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20 72  w VFSes can be r
36a80 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65 78  egistered and ex
36a90 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61 6e  isting VFSes can
36aa0 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65 64   be unregistered
36ab0 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ..** The followi
36ac0 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  ng interfaces ar
36ad0 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a 2a  e provided..**.*
36ae0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 66  * The sqlite3_vf
36af0 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66 61  s_find() interfa
36b00 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
36b10 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67 69  nter to a VFS gi
36b20 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a 2a  ven its name..**
36b30 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65 20   Names are case 
36b40 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e 61  sensitive..** Na
36b50 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65 72  mes are zero-ter
36b60 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
36b70 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65  rings..** If the
36b80 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20  re is no match, 
36b90 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69  a NULL pointer i
36ba0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49  s returned..** I
36bb0 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e 55  f zVfsName is NU
36bc0 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61  LL then the defa
36bd0 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75 72  ult VFS is retur
36be0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 56  ned..**.** New V
36bf0 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74 65  FSes are registe
36c00 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33  red with sqlite3
36c10 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 2e  _vfs_register().
36c20 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46 53  .** Each new VFS
36c30 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66   becomes the def
36c40 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65 20  ault VFS if the 
36c50 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69 73  makeDflt flag is
36c60 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61 6d   set..** The sam
36c70 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65 67  e VFS can be reg
36c80 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  istered multiple
36c90 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20 69   times without i
36ca0 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61 6b  njury..** To mak
36cb0 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56 46  e an existing VF
36cc0 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61 75  S into the defau
36cd0 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65 72  lt VFS, register
36ce0 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69 74   it again.** wit
36cf0 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66  h the makeDflt f
36d00 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77 6f  lag set.  If two
36d10 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65 73   different VFSes
36d20 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61 6d   with the.** sam
36d30 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69 73  e name are regis
36d40 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61 76  tered, the behav
36d50 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
36d60 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20 69  .  If a.** VFS i
36d70 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74  s registered wit
36d80 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69 73  h a name that is
36d90 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74   NULL or an empt
36da0 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68 65  y string,.** the
36db0 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  n the behavior i
36dc0 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  s undefined..**.
36dd0 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20  ** Unregister a 
36de0 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71 6c  VFS with the sql
36df0 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73  ite3_vfs_unregis
36e00 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e  ter() interface.
36e10 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61 75  .** If the defau
36e20 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67 69  lt VFS is unregi
36e30 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72 20  stered, another 
36e40 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61 73  VFS is chosen as
36e50 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e  .** the default.
36e60 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f 72    The choice for
36e70 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73 20   the new VFS is 
36e80 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  arbitrary..**.**
36e90 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
36ea0 2a 20 5b 48 31 31 32 30 33 5d 20 5b 48 31 31 32  * [H11203] [H112
36eb0 30 36 5d 20 5b 48 31 31 32 30 39 5d 20 5b 48 31  06] [H11209] [H1
36ec0 31 32 31 32 5d 20 5b 48 31 31 32 31 35 5d 20 5b  1212] [H11215] [
36ed0 48 31 31 32 31 38 5d 0a 2a 2f 0a 53 51 4c 49 54  H11218].*/.SQLIT
36ee0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66  E_API sqlite3_vf
36ef0 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66  s *sqlite3_vfs_f
36f00 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ind(const char *
36f10 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51 4c 49 54  zVfsName);.SQLIT
36f20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
36f30 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 73  3_vfs_register(s
36f40 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74  qlite3_vfs*, int
36f50 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53 51 4c 49   makeDflt);.SQLI
36f60 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
36f70 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
36f80 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b  r(sqlite3_vfs*);
36f90 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
36fa0 3a 20 4d 75 74 65 78 65 73 20 7b 48 31 37 30 30  : Mutexes {H1700
36fb0 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a  0} <S20000>.**.*
36fc0 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72  * The SQLite cor
36fd0 65 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75  e uses these rou
36fe0 74 69 6e 65 73 20 66 6f 72 20 74 68 72 65 61 64  tines for thread
36ff0 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61 74  .** synchronizat
37000 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 68 65 79  ion. Though they
37010 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f   are intended fo
37020 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
37030 65 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f 64  e by SQLite, cod
37040 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61  e that links aga
37050 69 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0a 2a  inst SQLite is.*
37060 2a 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 75  * permitted to u
37070 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  se any of these 
37080 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
37090 54 68 65 20 53 51 4c 69 74 65 20 73 6f 75 72 63  The SQLite sourc
370a0 65 20 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73 20  e code contains 
370b0 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65  multiple impleme
370c0 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74  ntations.** of t
370d0 68 65 73 65 20 6d 75 74 65 78 20 72 6f 75 74 69  hese mutex routi
370e0 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 6f 70 72  nes.  An appropr
370f0 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  iate implementat
37100 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63 74  ion.** is select
37110 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
37120 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
37130 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
37140 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
37150 6f 6e 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ons are availabl
37160 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20  e in the SQLite 
37170 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  core:.**.** <ul>
37180 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54  .** <li>   SQLIT
37190 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20 3c  E_MUTEX_OS2.** <
371a0 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  li>   SQLITE_MUT
371b0 45 58 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c 6c  EX_PTHREAD.** <l
371c0 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  i>   SQLITE_MUTE
371d0 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20  X_W32.** <li>   
371e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
371f0 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  P.** </ul>.**.**
37200 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45   The SQLITE_MUTE
37210 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e 74  X_NOOP implement
37220 61 74 69 6f 6e 20 69 73 20 61 20 73 65 74 20 6f  ation is a set o
37230 66 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 74 68  f routines.** th
37240 61 74 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c 20  at does no real 
37250 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 69 73 20 61  locking and is a
37260 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75  ppropriate for u
37270 73 65 20 69 6e 0a 2a 2a 20 61 20 73 69 6e 67 6c  se in.** a singl
37280 65 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69  e-threaded appli
37290 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 53 51 4c  cation.  The SQL
372a0 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 2c 0a 2a  ITE_MUTEX_OS2,.*
372b0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50  * SQLITE_MUTEX_P
372c0 54 48 52 45 41 44 2c 20 61 6e 64 20 53 51 4c 49  THREAD, and SQLI
372d0 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 69 6d 70  TE_MUTEX_W32 imp
372e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20  lementations.** 
372f0 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  are appropriate 
37300 66 6f 72 20 75 73 65 20 6f 6e 20 4f 53 2f 32 2c  for use on OS/2,
37310 20 55 6e 69 78 2c 20 61 6e 64 20 57 69 6e 64 6f   Unix, and Windo
37320 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c  ws..**.** If SQL
37330 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ite is compiled 
37340 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
37350 4d 55 54 45 58 5f 41 50 50 44 45 46 20 70 72 65  MUTEX_APPDEF pre
37360 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20 6d 61 63  processor.** mac
37370 72 6f 20 64 65 66 69 6e 65 64 20 28 77 69 74 68  ro defined (with
37380 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45 58   "-DSQLITE_MUTEX
37390 5f 41 50 50 44 45 46 3d 31 22 29 2c 20 74 68 65  _APPDEF=1"), the
373a0 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a 20 69 6d  n no mutex.** im
373b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
373c0 69 6e 63 6c 75 64 65 64 20 77 69 74 68 20 74 68  included with th
373d0 65 20 6c 69 62 72 61 72 79 2e 20 49 6e 20 74 68  e library. In th
373e0 69 73 20 63 61 73 65 20 74 68 65 0a 2a 2a 20 61  is case the.** a
373f0 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20  pplication must 
37400 73 75 70 70 6c 79 20 61 20 63 75 73 74 6f 6d 20  supply a custom 
37410 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
37420 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a  tion using the.*
37430 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
37440 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 20 6f  _MUTEX] option o
37450 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  f the sqlite3_co
37460 6e 66 69 67 28 29 20 66 75 6e 63 74 69 6f 6e 0a  nfig() function.
37470 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e  ** before callin
37480 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  g sqlite3_initia
37490 6c 69 7a 65 28 29 20 6f 72 20 61 6e 79 20 6f 74  lize() or any ot
374a0 68 65 72 20 70 75 62 6c 69 63 20 73 71 6c 69 74  her public sqlit
374b0 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  e3_.** function 
374c0 74 68 61 74 20 63 61 6c 6c 73 20 73 71 6c 69 74  that calls sqlit
374d0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e  e3_initialize().
374e0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 31 7d 20  .**.** {H17011} 
374f0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
37500 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
37510 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
37520 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
37530 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
37540 20 74 6f 20 69 74 2e 20 7b 48 31 37 30 31 32 7d   to it. {H17012}
37550 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e   If it returns N
37560 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e  ULL.** that mean
37570 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63  s that a mutex c
37580 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f  ould not be allo
37590 63 61 74 65 64 2e 20 7b 48 31 37 30 31 33 7d 20  cated. {H17013} 
375a0 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75  SQLite.** will u
375b0 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20  nwind its stack 
375c0 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72  and return an er
375d0 72 6f 72 2e 20 7b 48 31 37 30 31 34 7d 20 54 68  ror. {H17014} Th
375e0 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  e argument.** to
375f0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
37600 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66  lloc() is one of
37610 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63   these integer c
37620 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20  onstants:.**.** 
37630 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  <ul>.** <li>  SQ
37640 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a  LITE_MUTEX_FAST.
37650 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
37660 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a  MUTEX_RECURSIVE.
37670 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
37680 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
37690 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  TER.** <li>  SQL
376a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
376b0 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  _MEM.** <li>  SQ
376c0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
376d0 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20  C_MEM2.** <li>  
376e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
376f0 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e  TIC_PRNG.** <li>
37700 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
37710 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69  TATIC_LRU.** <li
37720 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
37730 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c  STATIC_LRU2.** <
37740 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  /ul>.**.** {H170
37750 31 35 7d 20 54 68 65 20 66 69 72 73 74 20 74 77  15} The first tw
37760 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73  o constants caus
37770 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
37780 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74  alloc() to creat
37790 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78  e.** a new mutex
377a0 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78  .  The new mutex
377b0 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68   is recursive wh
377c0 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  en SQLITE_MUTEX_
377d0 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20  RECURSIVE.** is 
377e0 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63  used but not nec
377f0 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e  essarily so when
37800 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
37810 53 54 20 69 73 20 75 73 65 64 2e 20 7b 45 4e 44  ST is used. {END
37820 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69  }.** The mutex i
37830 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f  mplementation do
37840 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
37850 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f  ake a distinctio
37860 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c  n.** between SQL
37870 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
37880 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d  IVE and SQLITE_M
37890 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20  UTEX_FAST if it 
378a0 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74  does.** not want
378b0 20 74 6f 2e 20 20 7b 48 31 37 30 31 36 7d 20 42   to.  {H17016} B
378c0 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  ut SQLite will o
378d0 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65  nly request a re
378e0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e  cursive mutex in
378f0 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
37900 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
37910 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 49 66 20 61  one.  {END} If a
37920 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75   faster non-recu
37930 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69  rsive mutex.** i
37940 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
37950 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68   available on th
37960 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c  e host platform,
37970 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
37980 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65  stem.** might re
37990 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65  turn such a mute
379a0 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f  x in response to
379b0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
379c0 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31  ST..**.** {H1701
379d0 37 7d 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c  7} The other all
379e0 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 20  owed parameters 
379f0 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
37a00 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65  _alloc() each re
37a10 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
37a20 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72  r to a static pr
37a30 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e  eexisting mutex.
37a40 20 7b 45 4e 44 7d 20 20 46 6f 75 72 20 73 74 61   {END}  Four sta
37a50 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a  tic mutexes are.
37a60 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63  ** used by the c
37a70 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f  urrent version o
37a80 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72  f SQLite.  Futur
37a90 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
37aa0 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20  Lite.** may add 
37ab0 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
37ac0 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74  c mutexes.  Stat
37ad0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66  ic mutexes are f
37ae0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
37af0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c  se by SQLite onl
37b00 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  y.  Applications
37b10 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
37b20 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a   mutexes should.
37b30 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20  ** use only the 
37b40 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20  dynamic mutexes 
37b50 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49  returned by SQLI
37b60 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72  TE_MUTEX_FAST or
37b70 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
37b80 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a  _RECURSIVE..**.*
37b90 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f 74 65 20  * {H17018} Note 
37ba0 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74  that if one of t
37bb0 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
37bc0 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c   parameters (SQL
37bd0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a  ITE_MUTEX_FAST.*
37be0 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45  * or SQLITE_MUTE
37bf0 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20  X_RECURSIVE) is 
37c00 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65  used then sqlite
37c10 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a  3_mutex_alloc().
37c20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66  ** returns a dif
37c30 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20  ferent mutex on 
37c40 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 7b 48 31  every call.  {H1
37c50 37 30 33 34 7d 20 42 75 74 20 66 6f 72 20 74 68  7034} But for th
37c60 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  e static.** mute
37c70 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d  x types, the sam
37c80 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72  e mutex is retur
37c90 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  ned on every cal
37ca0 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68  l that has.** th
37cb0 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62  e same type numb
37cc0 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31  er..**.** {H1701
37cd0 39 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  9} The sqlite3_m
37ce0 75 74 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74  utex_free() rout
37cf0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
37d00 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  a previously.** 
37d10 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69  allocated dynami
37d20 63 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 30  c mutex. {H17020
37d30 7d 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65  } SQLite is care
37d40 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ful to deallocat
37d50 65 20 65 76 65 72 79 0a 2a 2a 20 64 79 6e 61 6d  e every.** dynam
37d60 69 63 20 6d 75 74 65 78 20 74 68 61 74 20 69 74  ic mutex that it
37d70 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b 41 31 37   allocates. {A17
37d80 30 32 31 7d 20 54 68 65 20 64 79 6e 61 6d 69 63  021} The dynamic
37d90 20 6d 75 74 65 78 65 73 20 6d 75 73 74 20 6e 6f   mutexes must no
37da0 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 65 20 77  t be in.** use w
37db0 68 65 6e 20 74 68 65 79 20 61 72 65 20 64 65 61  hen they are dea
37dc0 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31 37 30 32  llocated. {A1702
37dd0 32 7d 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f  2} Attempting to
37de0 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 73 74   deallocate a st
37df0 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 72 65  atic.** mutex re
37e00 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e  sults in undefin
37e10 65 64 20 62 65 68 61 76 69 6f 72 2e 20 7b 48 31  ed behavior. {H1
37e20 37 30 32 33 7d 20 53 51 4c 69 74 65 20 6e 65 76  7023} SQLite nev
37e30 65 72 20 64 65 61 6c 6c 6f 63 61 74 65 73 0a 2a  er deallocates.*
37e40 2a 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78  * a static mutex
37e50 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  . {END}.**.** Th
37e60 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
37e70 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69  enter() and sqli
37e80 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
37e90 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
37ea0 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d  .** to enter a m
37eb0 75 74 65 78 2e 20 7b 48 31 37 30 32 34 7d 20 49  utex. {H17024} I
37ec0 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  f another thread
37ed0 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68   is already with
37ee0 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a  in the mutex,.**
37ef0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
37f00 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63  nter() will bloc
37f10 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  k and sqlite3_mu
37f20 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72  tex_try() will r
37f30 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
37f40 42 55 53 59 2e 20 7b 48 31 37 30 32 35 7d 20 20  BUSY. {H17025}  
37f50 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
37f60 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63  x_try() interfac
37f70 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  e returns [SQLIT
37f80 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75  E_OK].** upon su
37f90 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20  ccessful entry. 
37fa0 20 7b 48 31 37 30 32 36 7d 20 4d 75 74 65 78 65   {H17026} Mutexe
37fb0 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a  s created using.
37fc0 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
37fd0 52 45 43 55 52 53 49 56 45 20 63 61 6e 20 62 65  RECURSIVE can be
37fe0 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
37ff0 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
38000 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a 2a 20 7b  ame thread..** {
38010 48 31 37 30 32 37 7d 20 49 6e 20 73 75 63 68 20  H17027} In such 
38020 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75  cases the,.** mu
38030 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74  tex must be exit
38040 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62  ed an equal numb
38050 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f  er of times befo
38060 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  re another threa
38070 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20  d.** can enter. 
38080 20 7b 41 31 37 30 32 38 7d 20 49 66 20 74 68 65   {A17028} If the
38090 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
380a0 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
380b0 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66  other.** kind of
380c0 20 6d 75 74 65 78 20 6d 6f 72 65 20 74 68 61 6e   mutex more than
380d0 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76   once, the behav
380e0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
380f0 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d 20 53 51  ..** {H17029} SQ
38100 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
38110 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 20  exhibit.** such 
38120 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20  behavior in its 
38130 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78  own use of mutex
38140 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 73  es..**.** Some s
38150 79 73 74 65 6d 73 20 28 66 6f 72 20 65 78 61 6d  ystems (for exam
38160 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20 39 35 29  ple, Windows 95)
38170 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
38180 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a  the operation.**
38190 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
381a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
381b0 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73  y().  On those s
381c0 79 73 74 65 6d 73 2c 20 73 71 6c 69 74 65 33 5f  ystems, sqlite3_
381d0 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a 2a 20 77  mutex_try().** w
381e0 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72  ill always retur
381f0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  n SQLITE_BUSY.  
38200 7b 48 31 37 30 33 30 7d 20 54 68 65 20 53 51 4c  {H17030} The SQL
38210 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79 20 65 76  ite core only ev
38220 65 72 20 75 73 65 73 0a 2a 2a 20 73 71 6c 69 74  er uses.** sqlit
38230 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 61  e3_mutex_try() a
38240 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
38250 6e 20 73 6f 20 74 68 69 73 20 69 73 20 61 63 63  n so this is acc
38260 65 70 74 61 62 6c 65 20 62 65 68 61 76 69 6f 72  eptable behavior
38270 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 33 31 7d  ..**.** {H17031}
38280 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
38290 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
382a0 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
382b0 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
382c0 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
382d0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
382e0 61 64 2e 20 20 7b 41 31 37 30 33 32 7d 20 54 68  ad.  {A17032} Th
382f0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
38300 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
38310 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
38320 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
38330 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69   by the.** calli
38340 6e 67 20 74 68 72 65 61 64 20 6f 72 20 69 73 20  ng thread or is 
38350 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c  not currently al
38360 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31 37 30 33  located.  {H1703
38370 33 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a  3} SQLite will.*
38380 2a 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65  * never do eithe
38390 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49  r. {END}.**.** I
383a0 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  f the argument t
383b0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
383c0 65 6e 74 65 72 28 29 2c 20 73 71 6c 69 74 65 33  enter(), sqlite3
383d0 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c 20 6f 72  _mutex_try(), or
383e0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
383f0 78 5f 6c 65 61 76 65 28 29 20 69 73 20 61 20 4e  x_leave() is a N
38400 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65  ULL pointer, the
38410 6e 20 61 6c 6c 20 74 68 72 65 65 20 72 6f 75 74  n all three rout
38420 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76 65 20 61  ines.** behave a
38430 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  s no-ops..**.** 
38440 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
38450 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d  e3_mutex_held()]
38460 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75   and [sqlite3_mu
38470 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 2e 0a  tex_notheld()]..
38480 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
38490 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c  lite3_mutex *sql
384a0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
384b0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  (int);.SQLITE_AP
384c0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d  I void sqlite3_m
384d0 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65  utex_free(sqlite
384e0 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54  3_mutex*);.SQLIT
384f0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
38500 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
38510 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
38520 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
38530 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
38540 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
38550 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
38560 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
38570 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75  leave(sqlite3_mu
38580 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tex*);../*.** CA
38590 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 4d 65  PI3REF: Mutex Me
385a0 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31  thods Object {H1
385b0 37 31 32 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a  7120} <S20130>.*
385c0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
385d0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
385e0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
385f0 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c  re defines the l
38600 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  ow-level routine
38610 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 6c 6c  s.** used to all
38620 6f 63 61 74 65 20 61 6e 64 20 75 73 65 20 6d 75  ocate and use mu
38630 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55 73 75  texes..**.** Usu
38640 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 61 75 6c  ally, the defaul
38650 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  t mutex implemen
38660 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 64 65 64  tations provided
38670 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 0a 2a   by SQLite are.*
38680 2a 20 73 75 66 66 69 63 69 65 6e 74 2c 20 68 6f  * sufficient, ho
38690 77 65 76 65 72 20 74 68 65 20 75 73 65 72 20 68  wever the user h
386a0 61 73 20 74 68 65 20 6f 70 74 69 6f 6e 20 6f 66  as the option of
386b0 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 61 20   substituting a 
386c0 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c 65 6d  custom.** implem
386d0 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 73 70 65  entation for spe
386e0 63 69 61 6c 69 7a 65 64 20 64 65 70 6c 6f 79 6d  cialized deploym
386f0 65 6e 74 73 20 6f 72 20 73 79 73 74 65 6d 73 20  ents or systems 
38700 66 6f 72 20 77 68 69 63 68 20 53 51 4c 69 74 65  for which SQLite
38710 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f  .** does not pro
38720 76 69 64 65 20 61 20 73 75 69 74 61 62 6c 65 20  vide a suitable 
38730 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
38740 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68  In this case, th
38750 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 61 74 65  e user.** create
38760 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 73 20  s and populates 
38770 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
38780 68 69 73 20 73 74 72 75 63 74 75 72 65 20 74 6f  his structure to
38790 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 71 6c 69   pass.** to sqli
387a0 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 61 6c 6f  te3_config() alo
387b0 6e 67 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c  ng with the [SQL
387c0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
387d0 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41 64 64  ] option..** Add
387e0 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20 69 6e  itionally, an in
387f0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
38800 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20  tructure can be 
38810 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a 20 6f 75  used as an.** ou
38820 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 77 68  tput variable wh
38830 65 6e 20 71 75 65 72 79 69 6e 67 20 74 68 65 20  en querying the 
38840 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 63  system for the c
38850 75 72 72 65 6e 74 20 6d 75 74 65 78 0a 2a 2a 20  urrent mutex.** 
38860 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
38870 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54  using the [SQLIT
38880 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45  E_CONFIG_GETMUTE
38890 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  X] option..**.**
388a0 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20   The xMutexInit 
388b0 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62  method defined b
388c0 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
388d0 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a   is invoked as.*
388e0 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d  * part of system
388f0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
38900 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  by the sqlite3_i
38910 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 75 6e 63  nitialize() func
38920 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 30 30 31  tion..** {H17001
38930 7d 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74  } The xMutexInit
38940 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 62   routine shall b
38950 65 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69  e called by SQLi
38960 74 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  te once for each
38970 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 20 63 61  .** effective ca
38980 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69  ll to [sqlite3_i
38990 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a  nitialize()]..**
389a0 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 45 6e  .** The xMutexEn
389b0 64 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64  d method defined
389c0 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75   by this structu
389d0 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73  re is invoked as
389e0 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74  .** part of syst
389f0 65 6d 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74  em shutdown by t
38a00 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  he sqlite3_shutd
38a10 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  own() function. 
38a20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  The.** implement
38a30 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65  ation of this me
38a40 74 68 6f 64 20 69 73 20 65 78 70 65 63 74 65 64  thod is expected
38a50 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20   to release all 
38a60 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72  outstanding.** r
38a70 65 73 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65  esources obtaine
38a80 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d  d by the mutex m
38a90 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74  ethods implement
38aa0 61 74 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c  ation, especiall
38ab0 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69  y.** those obtai
38ac0 6e 65 64 20 62 79 20 74 68 65 20 78 4d 75 74 65  ned by the xMute
38ad0 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e 20 7b 48  xInit method. {H
38ae0 31 37 30 30 33 7d 20 54 68 65 20 78 4d 75 74 65  17003} The xMute
38af0 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66  xEnd().** interf
38b00 61 63 65 20 73 68 61 6c 6c 20 62 65 20 69 6e 76  ace shall be inv
38b10 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  oked once for ea
38b20 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ch call to [sqli
38b30 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e  te3_shutdown()].
38b40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69  .**.** The remai
38b50 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65 74 68 6f  ning seven metho
38b60 64 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68  ds defined by th
38b70 69 73 20 73 74 72 75 63 74 75 72 65 20 28 78 4d  is structure (xM
38b80 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d  utexAlloc,.** xM
38b90 75 74 65 78 46 72 65 65 2c 20 78 4d 75 74 65 78  utexFree, xMutex
38ba0 45 6e 74 65 72 2c 20 78 4d 75 74 65 78 54 72 79  Enter, xMutexTry
38bb0 2c 20 78 4d 75 74 65 78 4c 65 61 76 65 2c 20 78  , xMutexLeave, x
38bc0 4d 75 74 65 78 48 65 6c 64 20 61 6e 64 0a 2a 2a  MutexHeld and.**
38bd0 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 20   xMutexNotheld) 
38be0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f  implement the fo
38bf0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63  llowing interfac
38c00 65 73 20 28 72 65 73 70 65 63 74 69 76 65 6c 79  es (respectively
38c10 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  ):.**.** <ul>.**
38c20 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65     <li>  [sqlite
38c30 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d  3_mutex_alloc()]
38c40 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e   </li>.**   <li>
38c50 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78    [sqlite3_mutex
38c60 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  _free()] </li>.*
38c70 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74  *   <li>  [sqlit
38c80 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
38c90 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69  ] </li>.**   <li
38ca0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
38cb0 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  x_try()] </li>.*
38cc0 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74  *   <li>  [sqlit
38cd0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
38ce0 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69  ] </li>.**   <li
38cf0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
38d00 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a  x_held()] </li>.
38d10 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69  **   <li>  [sqli
38d20 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
38d30 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f  d()] </li>.** </
38d40 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e  ul>.**.** The on
38d50 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69 73  ly difference is
38d60 20 74 68 61 74 20 74 68 65 20 70 75 62 6c 69 63   that the public
38d70 20 73 71 6c 69 74 65 33 5f 58 58 58 20 66 75 6e   sqlite3_XXX fun
38d80 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65  ctions enumerate
38d90 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e  d.** above silen
38da0 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e 79 20 69  tly ignore any i
38db0 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20  nvocations that 
38dc0 70 61 73 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  pass a NULL poin
38dd0 74 65 72 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f  ter instead.** o
38de0 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20  f a valid mutex 
38df0 68 61 6e 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c  handle. The impl
38e00 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
38e10 68 65 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e  he methods defin
38e20 65 64 0a 2a 2a 20 62 79 20 74 68 69 73 20 73 74  ed.** by this st
38e30 72 75 63 74 75 72 65 20 61 72 65 20 6e 6f 74 20  ructure are not 
38e40 72 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64  required to hand
38e50 6c 65 20 74 68 69 73 20 63 61 73 65 2c 20 74 68  le this case, th
38e60 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20  e results.** of 
38e70 70 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70  passing a NULL p
38e80 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 20 6f  ointer instead o
38e90 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20  f a valid mutex 
38ea0 68 61 6e 64 6c 65 20 61 72 65 20 75 6e 64 65 66  handle are undef
38eb0 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74  ined.** (i.e. it
38ec0 20 69 73 20 61 63 63 65 70 74 61 62 6c 65 20 74   is acceptable t
38ed0 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 69 6d 70  o provide an imp
38ee0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74  lementation that
38ef0 20 73 65 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a   segfaults if.**
38f00 20 69 74 20 69 73 20 70 61 73 73 65 64 20 61 20   it is passed a 
38f10 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a  NULL pointer)..*
38f20 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
38f30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
38f40 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d  ethods sqlite3_m
38f50 75 74 65 78 5f 6d 65 74 68 6f 64 73 3b 0a 73 74  utex_methods;.st
38f60 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74  ruct sqlite3_mut
38f70 65 78 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69  ex_methods {.  i
38f80 6e 74 20 28 2a 78 4d 75 74 65 78 49 6e 69 74 29  nt (*xMutexInit)
38f90 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a  (void);.  int (*
38fa0 78 4d 75 74 65 78 45 6e 64 29 28 76 6f 69 64 29  xMutexEnd)(void)
38fb0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
38fc0 78 20 2a 28 2a 78 4d 75 74 65 78 41 6c 6c 6f 63  x *(*xMutexAlloc
38fd0 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28  )(int);.  void (
38fe0 2a 78 4d 75 74 65 78 46 72 65 65 29 28 73 71 6c  *xMutexFree)(sql
38ff0 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20  ite3_mutex *);. 
39000 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 45 6e   void (*xMutexEn
39010 74 65 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  ter)(sqlite3_mut
39020 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  ex *);.  int (*x
39030 4d 75 74 65 78 54 72 79 29 28 73 71 6c 69 74 65  MutexTry)(sqlite
39040 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f  3_mutex *);.  vo
39050 69 64 20 28 2a 78 4d 75 74 65 78 4c 65 61 76 65  id (*xMutexLeave
39060 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  )(sqlite3_mutex 
39070 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74  *);.  int (*xMut
39080 65 78 48 65 6c 64 29 28 73 71 6c 69 74 65 33 5f  exHeld)(sqlite3_
39090 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20  mutex *);.  int 
390a0 28 2a 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29  (*xMutexNotheld)
390b0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
390c0 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  );.};../*.** CAP
390d0 49 33 52 45 46 3a 20 4d 75 74 65 78 20 56 65 72  I3REF: Mutex Ver
390e0 69 66 69 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e  ification Routin
390f0 65 73 20 7b 48 31 37 30 38 30 7d 20 3c 53 32 30  es {H17080} <S20
39100 31 33 30 3e 20 3c 53 33 30 38 30 30 3e 0a 2a 2a  130> <S30800>.**
39110 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
39120 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64  mutex_held() and
39130 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
39140 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65  otheld() routine
39150 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65  s.** are intende
39160 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
39170 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
39180 65 6e 74 73 2e 20 7b 48 31 37 30 38 31 7d 20 54  ents. {H17081} T
39190 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a  he SQLite core.*
391a0 2a 20 6e 65 76 65 72 20 75 73 65 73 20 74 68 65  * never uses the
391b0 73 65 20 72 6f 75 74 69 6e 65 73 20 65 78 63 65  se routines exce
391c0 70 74 20 69 6e 73 69 64 65 20 61 6e 20 61 73 73  pt inside an ass
391d0 65 72 74 28 29 20 61 6e 64 20 61 70 70 6c 69 63  ert() and applic
391e0 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 64  ations.** are ad
391f0 76 69 73 65 64 20 74 6f 20 66 6f 6c 6c 6f 77 20  vised to follow 
39200 74 68 65 20 6c 65 61 64 20 6f 66 20 74 68 65 20  the lead of the 
39210 63 6f 72 65 2e 20 20 7b 48 31 37 30 38 32 7d 20  core.  {H17082} 
39220 54 68 65 20 63 6f 72 65 20 6f 6e 6c 79 0a 2a 2a  The core only.**
39230 20 70 72 6f 76 69 64 65 73 20 69 6d 70 6c 65 6d   provides implem
39240 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68  entations for th
39250 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 68 65  ese routines whe
39260 6e 20 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64  n it is compiled
39270 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c  .** with the SQL
39280 49 54 45 5f 44 45 42 55 47 20 66 6c 61 67 2e 20  ITE_DEBUG flag. 
39290 20 7b 41 31 37 30 38 37 7d 20 45 78 74 65 72 6e   {A17087} Extern
392a0 61 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  al mutex impleme
392b0 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  ntations.** are 
392c0 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f  only required to
392d0 20 70 72 6f 76 69 64 65 20 74 68 65 73 65 20 72   provide these r
392e0 6f 75 74 69 6e 65 73 20 69 66 20 53 51 4c 49 54  outines if SQLIT
392f0 45 5f 44 45 42 55 47 20 69 73 0a 2a 2a 20 64 65  E_DEBUG is.** de
39300 66 69 6e 65 64 20 61 6e 64 20 69 66 20 4e 44 45  fined and if NDE
39310 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  BUG is not defin
39320 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38  ed..**.** {H1708
39330 33 7d 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  3} These routine
39340 73 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20  s should return 
39350 74 72 75 65 20 69 66 20 74 68 65 20 6d 75 74 65  true if the mute
39360 78 20 69 6e 20 74 68 65 69 72 20 61 72 67 75 6d  x in their argum
39370 65 6e 74 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f  ent.** is held o
39380 72 20 6e 6f 74 20 68 65 6c 64 2c 20 72 65 73 70  r not held, resp
39390 65 63 74 69 76 65 6c 79 2c 20 62 79 20 74 68 65  ectively, by the
393a0 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 2e   calling thread.
393b0 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 30 38 34 7d 20  .**.** {X17084} 
393c0 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  The implementati
393d0 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  on is not requir
393e0 65 64 20 74 6f 20 70 72 6f 76 69 64 65 64 20 76  ed to provided v
393f0 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65  ersions of these
39400 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 74 68 61  .** routines tha
39410 74 20 61 63 74 75 61 6c 6c 79 20 77 6f 72 6b 2e  t actually work.
39420 20 49 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   If the implemen
39430 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
39440 70 72 6f 76 69 64 65 20 77 6f 72 6b 69 6e 67 0a  provide working.
39450 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  ** versions of t
39460 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 69  hese routines, i
39470 74 20 73 68 6f 75 6c 64 20 61 74 20 6c 65 61 73  t should at leas
39480 74 20 70 72 6f 76 69 64 65 20 73 74 75 62 73 20  t provide stubs 
39490 74 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 72  that always.** r
394a0 65 74 75 72 6e 20 74 72 75 65 20 73 6f 20 74 68  eturn true so th
394b0 61 74 20 6f 6e 65 20 64 6f 65 73 20 6e 6f 74 20  at one does not 
394c0 67 65 74 20 73 70 75 72 69 6f 75 73 20 61 73 73  get spurious ass
394d0 65 72 74 69 6f 6e 20 66 61 69 6c 75 72 65 73 2e  ertion failures.
394e0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 35 7d 20  .**.** {H17085} 
394f0 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  If the argument 
39500 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
39510 5f 68 65 6c 64 28 29 20 69 73 20 61 20 4e 55 4c  _held() is a NUL
39520 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 0a 2a  L pointer then.*
39530 2a 20 74 68 65 20 72 6f 75 74 69 6e 65 20 73 68  * the routine sh
39540 6f 75 6c 64 20 72 65 74 75 72 6e 20 31 2e 20 20  ould return 1.  
39550 7b 45 4e 44 7d 20 54 68 69 73 20 73 65 65 6d 73  {END} This seems
39560 20 63 6f 75 6e 74 65 72 2d 69 6e 74 75 69 74 69   counter-intuiti
39570 76 65 20 73 69 6e 63 65 0a 2a 2a 20 63 6c 65 61  ve since.** clea
39580 72 6c 79 20 74 68 65 20 6d 75 74 65 78 20 63 61  rly the mutex ca
39590 6e 6e 6f 74 20 62 65 20 68 65 6c 64 20 69 66 20  nnot be held if 
395a0 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  it does not exis
395b0 74 2e 20 20 42 75 74 20 74 68 65 0a 2a 2a 20 74  t.  But the.** t
395c0 68 65 20 72 65 61 73 6f 6e 20 74 68 65 20 6d 75  he reason the mu
395d0 74 65 78 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  tex does not exi
395e0 73 74 20 69 73 20 62 65 63 61 75 73 65 20 74 68  st is because th
395f0 65 20 62 75 69 6c 64 20 69 73 20 6e 6f 74 0a 2a  e build is not.*
39600 2a 20 75 73 69 6e 67 20 6d 75 74 65 78 65 73 2e  * using mutexes.
39610 20 20 41 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20    And we do not 
39620 77 61 6e 74 20 74 68 65 20 61 73 73 65 72 74 28  want the assert(
39630 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  ) containing the
39640 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  .** call to sqli
39650 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
39660 20 74 6f 20 66 61 69 6c 2c 20 73 6f 20 61 20 6e   to fail, so a n
39670 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 69  on-zero return i
39680 73 0a 2a 2a 20 74 68 65 20 61 70 70 72 6f 70 72  s.** the appropr
39690 69 61 74 65 20 74 68 69 6e 67 20 74 6f 20 64 6f  iate thing to do
396a0 2e 20 20 7b 48 31 37 30 38 36 7d 20 54 68 65 20  .  {H17086} The 
396b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
396c0 74 68 65 6c 64 28 29 0a 2a 2a 20 69 6e 74 65 72  theld().** inter
396d0 66 61 63 65 20 73 68 6f 75 6c 64 20 61 6c 73 6f  face should also
396e0 20 72 65 74 75 72 6e 20 31 20 77 68 65 6e 20 67   return 1 when g
396f0 69 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  iven a NULL poin
39700 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ter..*/.SQLITE_A
39710 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d  PI int sqlite3_m
39720 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65  utex_held(sqlite
39730 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54  3_mutex*);.SQLIT
39740 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
39750 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
39760 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
39770 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
39780 3a 20 4d 75 74 65 78 20 54 79 70 65 73 20 7b 48  : Mutex Types {H
39790 31 37 30 30 31 7d 20 3c 48 31 37 30 30 30 3e 0a  17001} <H17000>.
397a0 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  **.** The [sqlit
397b0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
397c0 5d 20 69 6e 74 65 72 66 61 63 65 20 74 61 6b 65  ] interface take
397d0 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  s a single argum
397e0 65 6e 74 0a 2a 2a 20 77 68 69 63 68 20 69 73 20  ent.** which is 
397f0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74  one of these int
39800 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 2e 0a  eger constants..
39810 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66  **.** The set of
39820 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   static mutexes 
39830 6d 61 79 20 63 68 61 6e 67 65 20 66 72 6f 6d 20  may change from 
39840 6f 6e 65 20 53 51 4c 69 74 65 20 72 65 6c 65 61  one SQLite relea
39850 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78  se to the.** nex
39860 74 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  t.  Applications
39870 20 74 68 61 74 20 6f 76 65 72 72 69 64 65 20 74   that override t
39880 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 75 74 65  he built-in mute
39890 78 20 6c 6f 67 69 63 20 6d 75 73 74 20 62 65 0a  x logic must be.
398a0 2a 2a 20 70 72 65 70 61 72 65 64 20 74 6f 20 61  ** prepared to a
398b0 63 63 6f 6d 6d 6f 64 61 74 65 20 61 64 64 69 74  ccommodate addit
398c0 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74  ional static mut
398d0 65 78 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  exes..*/.#define
398e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
398f0 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 30  ST             0
39900 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
39910 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
39920 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
39930 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
39940 41 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20 32  ATIC_MASTER    2
39950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
39960 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
39970 20 20 20 20 20 20 20 33 20 20 2f 2a 20 73 71 6c         3  /* sql
39980 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f  ite3_malloc() */
39990 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
399a0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
399b0 32 20 20 20 20 20 20 34 20 20 2f 2a 20 4e 4f 54  2      4  /* NOT
399c0 20 55 53 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65   USED */.#define
399d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
399e0 41 54 49 43 5f 4f 50 45 4e 20 20 20 20 20 20 34  ATIC_OPEN      4
399f0 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65    /* sqlite3Btre
39a00 65 4f 70 65 6e 28 29 20 2a 2f 0a 23 64 65 66 69  eOpen() */.#defi
39a10 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
39a20 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20  STATIC_PRNG     
39a30 20 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72   5  /* sqlite3_r
39a40 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66 69  andom() */.#defi
39a50 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ne SQLITE_MUTEX_
39a60 53 54 41 54 49 43 5f 4c 52 55 20 20 20 20 20 20  STATIC_LRU      
39a70 20 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20   6  /* lru page 
39a80 6c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  list */.#define 
39a90 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
39aa0 54 49 43 5f 4c 52 55 32 20 20 20 20 20 20 37 20  TIC_LRU2      7 
39ab0 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 73   /* lru page lis
39ac0 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t */../*.** CAPI
39ad0 33 52 45 46 3a 20 52 65 74 72 69 65 76 65 20 74  3REF: Retrieve t
39ae0 68 65 20 6d 75 74 65 78 20 66 6f 72 20 61 20 64  he mutex for a d
39af0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
39b00 6f 6e 20 7b 48 31 37 30 30 32 7d 20 3c 48 31 37  on {H17002} <H17
39b10 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  000>.**.** This 
39b20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
39b30 73 20 61 20 70 6f 69 6e 74 65 72 20 74 68 65 20  s a pointer the 
39b40 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20  [sqlite3_mutex] 
39b50 6f 62 6a 65 63 74 20 74 68 61 74 20 0a 2a 2a 20  object that .** 
39b60 73 65 72 69 61 6c 69 7a 65 73 20 61 63 63 65 73  serializes acces
39b70 73 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61  s to the [databa
39b80 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 67  se connection] g
39b90 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75  iven in the argu
39ba0 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 68 65  ment.** when the
39bb0 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
39bc0 5d 20 69 73 20 53 65 72 69 61 6c 69 7a 65 64 2e  ] is Serialized.
39bd0 0a 2a 2a 20 49 66 20 74 68 65 20 5b 74 68 72 65  .** If the [thre
39be0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53  ading mode] is S
39bf0 69 6e 67 6c 65 2d 74 68 72 65 61 64 20 6f 72 20  ingle-thread or 
39c00 4d 75 6c 74 69 2d 74 68 72 65 61 64 20 74 68 65  Multi-thread the
39c10 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  n this.** routin
39c20 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c  e returns a NULL
39c30 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c   pointer..*/.SQL
39c40 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
39c50 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64  mutex *sqlite3_d
39c60 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 2a  b_mutex(sqlite3*
39c70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
39c80 45 46 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43 6f  EF: Low-Level Co
39c90 6e 74 72 6f 6c 20 4f 66 20 44 61 74 61 62 61 73  ntrol Of Databas
39ca0 65 20 46 69 6c 65 73 20 7b 48 31 31 33 30 30 7d  e Files {H11300}
39cb0 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S30800>.**.** 
39cc0 7b 48 31 31 33 30 31 7d 20 54 68 65 20 5b 73 71  {H11301} The [sq
39cd0 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
39ce0 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  ol()] interface 
39cf0 6d 61 6b 65 73 20 61 20 64 69 72 65 63 74 20 63  makes a direct c
39d00 61 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46  all to the.** xF
39d10 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f  ileControl metho
39d20 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74  d for the [sqlit
39d30 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f  e3_io_methods] o
39d40 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64  bject associated
39d50 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 69  .** with a parti
39d60 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 69  cular database i
39d70 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
39d80 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
39d90 2e 20 7b 48 31 31 33 30 32 7d 20 54 68 65 0a 2a  . {H11302} The.*
39da0 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * name of the da
39db0 74 61 62 61 73 65 20 69 73 20 74 68 65 20 6e 61  tabase is the na
39dc0 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74  me assigned to t
39dd0 68 65 20 64 61 74 61 62 61 73 65 20 62 79 20 74  he database by t
39de0 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 6c  he.** <a href="l
39df0 61 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d 6c 22  ang_attach.html"
39e00 3e 41 54 54 41 43 48 3c 2f 61 3e 20 53 51 4c 20  >ATTACH</a> SQL 
39e10 63 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f 70 65  command that ope
39e20 6e 65 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  ned the.** datab
39e30 61 73 65 2e 20 7b 48 31 31 33 30 33 7d 20 54 6f  ase. {H11303} To
39e40 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6d 61 69   control the mai
39e50 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  n database file,
39e60 20 75 73 65 20 74 68 65 20 6e 61 6d 65 20 22 6d   use the name "m
39e70 61 69 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e 55 4c  ain".** or a NUL
39e80 4c 20 70 6f 69 6e 74 65 72 2e 20 7b 48 31 31 33  L pointer. {H113
39e90 30 34 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e  04} The third an
39ea0 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  d fourth paramet
39eb0 65 72 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74  ers to this rout
39ec0 69 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73 73 65  ine.** are passe
39ed0 64 20 64 69 72 65 63 74 6c 79 20 74 68 72 6f 75  d directly throu
39ee0 67 68 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64  gh to the second
39ef0 20 61 6e 64 20 74 68 69 72 64 20 70 61 72 61 6d   and third param
39f00 65 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68 65 20  eters of.** the 
39f10 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74  xFileControl met
39f20 68 6f 64 2e 20 20 7b 48 31 31 33 30 35 7d 20 54  hod.  {H11305} T
39f30 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
39f40 6f 66 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74  of the xFileCont
39f50 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65  rol.** method be
39f60 63 6f 6d 65 73 20 74 68 65 20 72 65 74 75 72 6e  comes the return
39f70 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72   value of this r
39f80 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48  outine..**.** {H
39f90 31 31 33 30 36 7d 20 49 66 20 74 68 65 20 73 65  11306} If the se
39fa0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28  cond parameter (
39fb0 7a 44 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e 6f  zDbName) does no
39fc0 74 20 6d 61 74 63 68 20 74 68 65 20 6e 61 6d 65  t match the name
39fd0 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e 20   of any.** open 
39fe0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
39ff0 68 65 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  hen SQLITE_ERROR
3a000 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 7b 48   is returned. {H
3a010 31 31 33 30 37 7d 20 54 68 69 73 20 65 72 72 6f  11307} This erro
3a020 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 6e 6f 74  r.** code is not
3a030 20 72 65 6d 65 6d 62 65 72 65 64 20 61 6e 64 20   remembered and 
3a040 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63 61  will not be reca
3a050 6c 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  lled by [sqlite3
3a060 5f 65 72 72 63 6f 64 65 28 29 5d 0a 2a 2a 20 6f  _errcode()].** o
3a070 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  r [sqlite3_errms
3a080 67 28 29 5d 2e 20 7b 41 31 31 33 30 38 7d 20 54  g()]. {A11308} T
3a090 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 78 46  he underlying xF
3a0a0 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f  ileControl metho
3a0b0 64 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20  d might.** also 
3a0c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
3a0d0 52 4f 52 2e 20 20 7b 41 31 31 33 30 39 7d 20 54  ROR.  {A11309} T
3a0e0 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74  here is no way t
3a0f0 6f 20 64 69 73 74 69 6e 67 75 69 73 68 20 62 65  o distinguish be
3a100 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e 63 6f  tween.** an inco
3a110 72 72 65 63 74 20 7a 44 62 4e 61 6d 65 20 61 6e  rrect zDbName an
3a120 64 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  d an SQLITE_ERRO
3a130 52 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68  R return from th
3a140 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20  e underlying.** 
3a150 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74  xFileControl met
3a160 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  hod. {END}.**.**
3a170 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49   See also: [SQLI
3a180 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41  TE_FCNTL_LOCKSTA
3a190 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  TE].*/.SQLITE_AP
3a1a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69  I int sqlite3_fi
3a1b0 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  le_control(sqlit
3a1c0 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  e3*, const char 
3a1d0 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70  *zDbName, int op
3a1e0 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  , void*);../*.**
3a1f0 20 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 69   CAPI3REF: Testi
3a200 6e 67 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31  ng Interface {H1
3a210 31 34 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a  1400} <S30800>.*
3a220 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
3a230 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20  _test_control() 
3a240 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65  interface is use
3a250 64 20 74 6f 20 72 65 61 64 20 6f 75 74 20 69 6e  d to read out in
3a260 74 65 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 65 20  ternal.** state 
3a270 6f 66 20 53 51 4c 69 74 65 20 61 6e 64 20 74 6f  of SQLite and to
3a280 20 69 6e 6a 65 63 74 20 66 61 75 6c 74 73 20 69   inject faults i
3a290 6e 74 6f 20 53 51 4c 69 74 65 20 66 6f 72 20 74  nto SQLite for t
3a2a0 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73  esting.** purpos
3a2b0 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 70  es.  The first p
3a2c0 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 6f  arameter is an o
3a2d0 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 74 68  peration code th
3a2e0 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a  at determines.**
3a2f0 20 74 68 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61   the number, mea
3a300 6e 69 6e 67 2c 20 61 6e 64 20 6f 70 65 72 61 74  ning, and operat
3a310 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 75 62 73 65  ion of all subse
3a320 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73  quent parameters
3a330 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  ..**.** This int
3a340 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 66 6f  erface is not fo
3a350 72 20 75 73 65 20 62 79 20 61 70 70 6c 69 63 61  r use by applica
3a360 74 69 6f 6e 73 2e 20 20 49 74 20 65 78 69 73 74  tions.  It exist
3a370 73 20 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 20  s solely.** for 
3a380 76 65 72 69 66 79 69 6e 67 20 74 68 65 20 63 6f  verifying the co
3a390 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
3a3a0 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  of the SQLite li
3a3b0 62 72 61 72 79 2e 20 20 44 65 70 65 6e 64 69 6e  brary.  Dependin
3a3c0 67 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65 20  g.** on how the 
3a3d0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69  SQLite library i
3a3e0 73 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69 73  s compiled, this
3a3f0 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74   interface might
3a400 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a   not exist..**.*
3a410 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66  * The details of
3a420 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 63   the operation c
3a430 6f 64 65 73 2c 20 74 68 65 69 72 20 6d 65 61 6e  odes, their mean
3a440 69 6e 67 73 2c 20 74 68 65 20 70 61 72 61 6d 65  ings, the parame
3a450 74 65 72 73 0a 2a 2a 20 74 68 65 79 20 74 61 6b  ters.** they tak
3a460 65 2c 20 61 6e 64 20 77 68 61 74 20 74 68 65 79  e, and what they
3a470 20 64 6f 20 61 72 65 20 61 6c 6c 20 73 75 62 6a   do are all subj
3a480 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69  ect to change wi
3a490 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 0a 2a 2a  thout notice..**
3a4a0 20 55 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 20   Unlike most of 
3a4b0 74 68 65 20 53 51 4c 69 74 65 20 41 50 49 2c 20  the SQLite API, 
3a4c0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
3a4d0 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 20   not guaranteed 
3a4e0 74 6f 0a 2a 2a 20 6f 70 65 72 61 74 65 20 63 6f  to.** operate co
3a4f0 6e 73 69 73 74 65 6e 74 6c 79 20 66 72 6f 6d 20  nsistently from 
3a500 6f 6e 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74  one release to t
3a510 68 65 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49  he next..*/.SQLI
3a520 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3a530 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
3a540 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f  int op, ...);../
3a550 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54  *.** CAPI3REF: T
3a560 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65  esting Interface
3a570 20 4f 70 65 72 61 74 69 6f 6e 20 43 6f 64 65 73   Operation Codes
3a580 20 7b 48 31 31 34 31 30 7d 20 3c 48 31 31 34 30   {H11410} <H1140
3a590 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  0>.**.** These c
3a5a0 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65  onstants are the
3a5b0 20 76 61 6c 69 64 20 6f 70 65 72 61 74 69 6f 6e   valid operation
3a5c0 20 63 6f 64 65 20 70 61 72 61 6d 65 74 65 72 73   code parameters
3a5d0 20 75 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20   used.** as the 
3a5e0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
3a5f0 6f 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  o [sqlite3_test_
3a600 63 6f 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a  control()]..**.*
3a610 2a 20 54 68 65 73 65 20 70 61 72 61 6d 65 74 65  * These paramete
3a620 72 73 20 61 6e 64 20 74 68 65 69 72 20 6d 65 61  rs and their mea
3a630 6e 69 6e 67 73 20 61 72 65 20 73 75 62 6a 65 63  nings are subjec
3a640 74 20 74 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 77  t to change.** w
3a650 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20 20  ithout notice.  
3a660 54 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 65  These values are
3a670 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72   for testing pur
3a680 70 6f 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20 41  poses only..** A
3a690 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75  pplications shou
3a6a0 6c 64 20 6e 6f 74 20 75 73 65 20 61 6e 79 20 6f  ld not use any o
3a6b0 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65  f these paramete
3a6c0 72 73 20 6f 72 20 74 68 65 0a 2a 2a 20 5b 73 71  rs or the.** [sq
3a6d0 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
3a6e0 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  ol()] interface.
3a6f0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
3a700 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47  TE_TESTCTRL_PRNG
3a710 5f 53 41 56 45 20 20 20 20 20 20 20 20 20 20 20  _SAVE           
3a720 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53       5.#define S
3a730 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50  QLITE_TESTCTRL_P
3a740 52 4e 47 5f 52 45 53 54 4f 52 45 20 20 20 20 20  RNG_RESTORE     
3a750 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e          6.#defin
3a760 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
3a770 4c 5f 50 52 4e 47 5f 52 45 53 45 54 20 20 20 20  L_PRNG_RESET    
3a780 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65             7.#de
3a790 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54  fine SQLITE_TEST
3a7a0 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54  CTRL_BITVEC_TEST
3a7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a                8.
3a7c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
3a7d0 45 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 4e  ESTCTRL_FAULT_IN
3a7e0 53 54 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20  STALL           
3a7f0 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   9.#define SQLIT
3a800 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47  E_TESTCTRL_BENIG
3a810 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20  N_MALLOC_HOOKS  
3a820 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 53 51     10.#define SQ
3a830 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45  LITE_TESTCTRL_PE
3a840 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20  NDING_BYTE      
3a850 20 20 20 20 20 20 31 31 0a 0a 2f 2a 0a 2a 2a 20        11../*.** 
3a860 43 41 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65  CAPI3REF: SQLite
3a870 20 52 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20   Runtime Status 
3a880 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30  {H17200} <S60200
3a890 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
3a8a0 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  L.**.** This int
3a8b0 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74  erface is used t
3a8c0 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e 74 69  o retrieve runti
3a8d0 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d  me status inform
3a8e0 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74  ation.** about t
3a8f0 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f  he preformance o
3a900 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70  f SQLite, and op
3a910 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65  tionally to rese
3a920 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67  t various.** hig
3a930 68 77 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54  hwater marks.  T
3a940 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
3a950 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  t is an integer 
3a960 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20  code for.** the 
3a970 73 70 65 63 69 66 69 63 20 70 61 72 61 6d 65 74  specific paramet
3a980 65 72 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20  er to measure.  
3a990 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67  Recognized integ
3a9a0 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20  er codes.** are 
3a9b0 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c  of the form [SQL
3a9c0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
3a9d0 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f  Y_USED | SQLITE_
3a9e0 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20  STATUS_...]..** 
3a9f0 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  The current valu
3aa00 65 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74  e of the paramet
3aa10 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  er is returned i
3aa20 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a  nto *pCurrent..*
3aa30 2a 20 54 68 65 20 68 69 67 68 65 73 74 20 72 65  * The highest re
3aa40 63 6f 72 64 65 64 20 76 61 6c 75 65 20 69 73 20  corded value is 
3aa50 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69  returned in *pHi
3aa60 67 68 77 61 74 65 72 2e 20 20 49 66 20 74 68 65  ghwater.  If the
3aa70 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73  .** resetFlag is
3aa80 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
3aa90 68 69 67 68 65 73 74 20 72 65 63 6f 72 64 20 76  highest record v
3aaa0 61 6c 75 65 20 69 73 20 72 65 73 65 74 20 61 66  alue is reset af
3aab0 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74  ter.** *pHighwat
3aac0 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53  er is written. S
3aad0 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 64  ome parameters d
3aae0 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65  o not record the
3aaf0 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75   highest.** valu
3ab00 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61  e.  For those pa
3ab10 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68  rameters.** noth
3ab20 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69  ing is written i
3ab30 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20  nto *pHighwater 
3ab40 61 6e 64 20 74 68 65 20 72 65 73 65 74 46 6c 61  and the resetFla
3ab50 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a  g is ignored..**
3ab60 20 4f 74 68 65 72 20 70 61 72 61 6d 65 74 65 72   Other parameter
3ab70 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68  s record only th
3ab80 65 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b  e highwater mark
3ab90 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72   and not the cur
3aba0 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20  rent.** value.  
3abb0 46 6f 72 20 74 68 65 73 65 20 6c 61 74 74 65 72  For these latter
3abc0 20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68   parameters noth
3abd0 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69  ing is written i
3abe0 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a  nto *pCurrent..*
3abf0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
3ac00 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
3ac10 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61  _OK on success a
3ac20 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a  nd a non-zero.**
3ac30 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e   [error code] on
3ac40 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20   failure..**.** 
3ac50 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
3ac60 74 68 72 65 61 64 73 61 66 65 20 62 75 74 20 69  threadsafe but i
3ac70 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54  s not atomic.  T
3ac80 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 0a  his routine can.
3ac90 2a 2a 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20  ** called while 
3aca0 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 72  other threads ar
3acb0 65 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 61  e running the sa
3acc0 6d 65 20 6f 72 20 64 69 66 66 65 72 65 6e 74 20  me or different 
3acd0 53 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 66  SQLite.** interf
3ace0 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72 20 74  aces.  However t
3acf0 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  he values return
3ad00 65 64 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 20  ed in *pCurrent 
3ad10 61 6e 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74  and.** *pHighwat
3ad20 65 72 20 72 65 66 6c 65 63 74 20 74 68 65 20 73  er reflect the s
3ad30 74 61 74 75 73 20 6f 66 20 53 51 4c 69 74 65 20  tatus of SQLite 
3ad40 61 74 20 64 69 66 66 65 72 65 6e 74 20 70 6f 69  at different poi
3ad50 6e 74 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 61  nts in time.** a
3ad60 6e 64 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  nd it is possibl
3ad70 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74  e that another t
3ad80 68 72 65 61 64 20 6d 69 67 68 74 20 63 68 61 6e  hread might chan
3ad90 67 65 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  ge the parameter
3ada0 0a 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 74  .** in between t
3adb0 68 65 20 74 69 6d 65 73 20 77 68 65 6e 20 2a 70  he times when *p
3adc0 43 75 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 69  Current and *pHi
3add0 67 68 77 61 74 65 72 20 61 72 65 20 77 72 69 74  ghwater are writ
3ade0 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ten..**.** See a
3adf0 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 62  lso: [sqlite3_db
3ae00 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 51  _status()].*/.SQ
3ae10 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
3ae20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74  EXPERIMENTAL int
3ae30 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28   sqlite3_status(
3ae40 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75  int op, int *pCu
3ae50 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67  rrent, int *pHig
3ae60 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65  hwater, int rese
3ae70 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  tFlag);.../*.** 
3ae80 43 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 73  CAPI3REF: Status
3ae90 20 50 61 72 61 6d 65 74 65 72 73 20 7b 48 31 37   Parameters {H17
3aea0 32 35 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 2a  250} <H17200>.**
3aeb0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
3aec0 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65  .** These intege
3aed0 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69  r constants desi
3aee0 67 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72 75  gnate various ru
3aef0 6e 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70 61  n-time status pa
3af00 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74  rameters.** that
3af10 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64   can be returned
3af20 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61   by [sqlite3_sta
3af30 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64  tus()]..**.** <d
3af40 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  l>.** <dt>SQLITE
3af50 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
3af60 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  SED</dt>.** <dd>
3af70 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69  This parameter i
3af80 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d  s the current am
3af90 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63  ount of memory c
3afa0 68 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73  hecked out.** us
3afb0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  ing [sqlite3_mal
3afc0 6c 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64  loc()], either d
3afd0 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72  irectly or indir
3afe0 65 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66  ectly.  The.** f
3aff0 69 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63  igure includes c
3b000 61 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71  alls made to [sq
3b010 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20  lite3_malloc()] 
3b020 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  by the applicati
3b030 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e  on.** and intern
3b040 61 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20  al memory usage 
3b050 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  by the SQLite li
3b060 62 72 61 72 79 2e 20 20 53 63 72 61 74 63 68 20  brary.  Scratch 
3b070 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f  memory.** contro
3b080 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f  lled by [SQLITE_
3b090 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20  CONFIG_SCRATCH] 
3b0a0 61 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70 61  and auxiliary pa
3b0b0 67 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f  ge-cache.** memo
3b0c0 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79  ry controlled by
3b0d0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
3b0e0 50 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f  PAGECACHE] is no
3b0f0 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a  t included in.**
3b100 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e   this parameter.
3b110 20 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74    The amount ret
3b120 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d  urned is the sum
3b130 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
3b140 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72  on.** sizes as r
3b150 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 78  eported by the x
3b160 53 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b  Size method in [
3b170 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
3b180 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ods].</dd>.**.**
3b190 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54   <dt>SQLITE_STAT
3b1a0 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f  US_MALLOC_SIZE</
3b1b0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
3b1c0 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64  parameter record
3b1d0 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65  s the largest me
3b1e0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
3b1f0 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65  request.** hande
3b200 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  d to [sqlite3_ma
3b210 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69  lloc()] or [sqli
3b220 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28  te3_realloc()] (
3b230 6f 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 65  or their.** inte
3b240 72 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 73  rnal equivalents
3b250 29 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c  ).  Only the val
3b260 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74  ue returned in t
3b270 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65  he.** *pHighwate
3b280 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  r parameter to [
3b290 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29  sqlite3_status()
3b2a0 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74  ] is of interest
3b2b0 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .  .** The value
3b2c0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
3b2d0 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61  e *pCurrent para
3b2e0 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e  meter is undefin
3b2f0 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ed.</dd>.**.** <
3b300 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
3b310 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 3c  _PAGECACHE_USED<
3b320 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3b330 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72   parameter retur
3b340 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
3b350 20 70 61 67 65 73 20 75 73 65 64 20 6f 75 74 20   pages used out 
3b360 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 63  of the.** [pagec
3b370 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ache memory allo
3b380 63 61 74 6f 72 5d 20 74 68 61 74 20 77 61 73 20  cator] that was 
3b390 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67  configured using
3b3a0 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e   .** [SQLITE_CON
3b3b0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 20  FIG_PAGECACHE]. 
3b3c0 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 65   The.** value re
3b3d0 74 75 72 6e 65 64 20 69 73 20 69 6e 20 70 61 67  turned is in pag
3b3e0 65 73 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 73  es, not in bytes
3b3f0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
3b400 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  >SQLITE_STATUS_P
3b410 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f  AGECACHE_OVERFLO
3b420 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  W</dt>.** <dd>Th
3b430 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74  is parameter ret
3b440 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
3b450 6f 66 20 62 79 74 65 73 20 6f 66 20 70 61 67 65  of bytes of page
3b460 20 63 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61   cache.** alloca
3b470 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64  tion which could
3b480 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66 69   not be statisfi
3b490 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54  ed by the [SQLIT
3b4a0 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
3b4b0 48 45 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e  HE].** buffer an
3b4c0 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74  d where forced t
3b4d0 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73  o overflow to [s
3b4e0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
3b4f0 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e  .  The.** return
3b500 65 64 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65  ed value include
3b510 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68  s allocations th
3b520 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65  at overflowed be
3b530 63 61 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 68  cause they.** wh
3b540 65 72 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 74  ere too large (t
3b550 68 65 79 20 77 65 72 65 20 6c 61 72 67 65 72 20  hey were larger 
3b560 74 68 61 6e 20 74 68 65 20 22 73 7a 22 20 70 61  than the "sz" pa
3b570 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 53  rameter to.** [S
3b580 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
3b590 45 43 41 43 48 45 5d 29 20 61 6e 64 20 61 6c 6c  ECACHE]) and all
3b5a0 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76  ocations that ov
3b5b0 65 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65  erflowed because
3b5c0 0a 2a 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 73  .** no space was
3b5d0 20 6c 65 66 74 20 69 6e 20 74 68 65 20 70 61 67   left in the pag
3b5e0 65 20 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a 2a  e cache.</dd>.**
3b5f0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3b600 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
3b610 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SIZE</dt>.** <dd
3b620 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3b630 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67  records the larg
3b640 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  est memory alloc
3b650 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a  ation request.**
3b660 20 68 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 65   handed to [page
3b670 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  cache memory all
3b680 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74  ocator].  Only t
3b690 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
3b6a0 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69  d in the.** *pHi
3b6b0 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65  ghwater paramete
3b6c0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  r to [sqlite3_st
3b6d0 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e  atus()] is of in
3b6e0 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65  terest.  .** The
3b6f0 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69   value written i
3b700 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e  nto the *pCurren
3b710 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75  t parameter is u
3b720 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a  ndefined.</dd>.*
3b730 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3b740 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55  STATUS_SCRATCH_U
3b750 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  SED</dt>.** <dd>
3b760 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3b770 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
3b780 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  r of allocations
3b790 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65   used out of the
3b7a0 0a 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d  .** [scratch mem
3b7b0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63  ory allocator] c
3b7c0 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a  onfigured using.
3b7d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
3b7e0 47 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65  G_SCRATCH].  The
3b7f0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
3b800 69 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  is in allocation
3b810 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74  s, not.** in byt
3b820 65 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e  es.  Since a sin
3b830 67 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f  gle thread may o
3b840 6e 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72  nly have one scr
3b850 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  atch allocation.
3b860 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61  ** outstanding a
3b870 74 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72  t time, this par
3b880 61 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f  ameter also repo
3b890 72 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rts the number o
3b8a0 66 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69  f threads.** usi
3b8b0 6e 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  ng scratch memor
3b8c0 79 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  y at the same ti
3b8d0 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  me.</dd>.**.** <
3b8e0 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
3b8f0 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f  _SCRATCH_OVERFLO
3b900 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  W</dt>.** <dd>Th
3b910 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74  is parameter ret
3b920 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
3b930 6f 66 20 62 79 74 65 73 20 6f 66 20 73 63 72 61  of bytes of scra
3b940 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  tch memory.** al
3b950 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63  location which c
3b960 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74  ould not be stat
3b970 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53  isfied by the [S
3b980 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52  QLITE_CONFIG_SCR
3b990 41 54 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 20  ATCH].** buffer 
3b9a0 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65 64  and where forced
3b9b0 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20   to overflow to 
3b9c0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
3b9d0 29 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 73 0a  )].  The values.
3b9e0 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c  ** returned incl
3b9f0 75 64 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65  ude overflows be
3ba00 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65 73  cause the reques
3ba10 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77  ted allocation w
3ba20 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 72  as too.** larger
3ba30 20 28 74 68 61 74 20 69 73 2c 20 62 65 63 61 75   (that is, becau
3ba40 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64  se the requested
3ba50 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20   allocation was 
3ba60 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a  larger than the.
3ba70 2a 2a 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65  ** "sz" paramete
3ba80 72 20 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  r to [SQLITE_CON
3ba90 46 49 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e  FIG_SCRATCH]) an
3baa0 64 20 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72  d because no scr
3bab0 61 74 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 73  atch buffer.** s
3bac0 6c 6f 74 73 20 77 65 72 65 20 61 76 61 69 6c 61  lots were availa
3bad0 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a  ble..** </dd>.**
3bae0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3baf0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49  TATUS_SCRATCH_SI
3bb00 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ZE</dt>.** <dd>T
3bb10 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
3bb20 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73  cords the larges
3bb30 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  t memory allocat
3bb40 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68  ion request.** h
3bb50 61 6e 64 65 64 20 74 6f 20 5b 73 63 72 61 74 63  anded to [scratc
3bb60 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  h memory allocat
3bb70 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76  or].  Only the v
3bb80 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e  alue returned in
3bb90 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61   the.** *pHighwa
3bba0 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ter parameter to
3bbb0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
3bbc0 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65  ()] is of intere
3bbd0 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c  st.  .** The val
3bbe0 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  ue written into 
3bbf0 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61  the *pCurrent pa
3bc00 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66  rameter is undef
3bc10 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ined.</dd>.**.**
3bc20 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54   <dt>SQLITE_STAT
3bc30 55 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c  US_PARSER_STACK<
3bc40 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3bc50 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72   parameter recor
3bc60 64 73 20 74 68 65 20 64 65 65 70 65 73 74 20 70  ds the deepest p
3bc70 61 72 73 65 72 20 73 74 61 63 6b 2e 20 20 49 74  arser stack.  It
3bc80 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 6e   is only.** mean
3bc90 69 6e 67 66 75 6c 20 69 66 20 53 51 4c 69 74 65  ingful if SQLite
3bca0 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   is compiled wit
3bcb0 68 20 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 41  h [YYTRACKMAXSTA
3bcc0 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a 2a  CKDEPTH].</dd>.*
3bcd0 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65  * </dl>.**.** Ne
3bce0 77 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 74  w status paramet
3bcf0 65 72 73 20 6d 61 79 20 62 65 20 61 64 64 65 64  ers may be added
3bd00 20 66 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69   from time to ti
3bd10 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  me..*/.#define S
3bd20 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
3bd30 4f 52 59 5f 55 53 45 44 20 20 20 20 20 20 20 20  ORY_USED        
3bd40 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    0.#define SQLI
3bd50 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
3bd60 43 48 45 5f 55 53 45 44 20 20 20 20 20 20 20 31  CHE_USED       1
3bd70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3bd80 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
3bd90 5f 4f 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 64  _OVERFLOW   2.#d
3bda0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
3bdb0 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44  TUS_SCRATCH_USED
3bdc0 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69           3.#defi
3bdd0 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ne SQLITE_STATUS
3bde0 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f  _SCRATCH_OVERFLO
3bdf0 57 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20  W     4.#define 
3be00 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41  SQLITE_STATUS_MA
3be10 4c 4c 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20  LLOC_SIZE       
3be20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c     5.#define SQL
3be30 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45  ITE_STATUS_PARSE
3be40 52 5f 53 54 41 43 4b 20 20 20 20 20 20 20 20 20  R_STACK         
3be50 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  6.#define SQLITE
3be60 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
3be70 45 5f 53 49 5a 45 20 20 20 20 20 20 20 37 0a 23  E_SIZE       7.#
3be80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3be90 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a  ATUS_SCRATCH_SIZ
3bea0 45 20 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a 0a  E         8../*.
3beb0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74  ** CAPI3REF: Dat
3bec0 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e  abase Connection
3bed0 20 53 74 61 74 75 73 20 7b 48 31 37 35 30 30 7d   Status {H17500}
3bee0 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50   <S60200>.** EXP
3bef0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
3bf00 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69  This interface i
3bf10 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65  s used to retrie
3bf20 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75  ve runtime statu
3bf30 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a  s information .*
3bf40 2a 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65  * about a single
3bf50 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3bf60 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72  ction].  The fir
3bf70 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  st argument is t
3bf80 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  he.** database c
3bf90 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74  onnection object
3bfa0 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61   to be interroga
3bfb0 74 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  ted.  The second
3bfc0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20   argument.** is 
3bfd0 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f  the parameter to
3bfe0 20 69 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 43   interrogate.  C
3bff0 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e  urrently, the on
3c000 6c 79 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ly allowed value
3c010 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 65 63 6f  .** for the seco
3c020 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nd parameter is 
3c030 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53  [SQLITE_DBSTATUS
3c040 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d  _LOOKASIDE_USED]
3c050 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20  ..** Additional 
3c060 6f 70 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 6b  options will lik
3c070 65 6c 79 20 61 70 70 65 61 72 20 69 6e 20 66 75  ely appear in fu
3c080 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66  ture releases of
3c090 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54   SQLite..**.** T
3c0a0 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
3c0b0 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
3c0c0 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 77  d parameter is w
3c0d0 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75  ritten into *pCu
3c0e0 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67  r.** and the hig
3c0f0 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f  hest instantaneo
3c100 75 73 20 76 61 6c 75 65 20 69 73 20 77 72 69 74  us value is writ
3c110 74 65 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72  ten into *pHiwtr
3c120 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 73  .  If.** the res
3c130 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74  etFlg is true, t
3c140 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20  hen the highest 
3c150 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61  instantaneous va
3c160 6c 75 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 20  lue is.** reset 
3c170 62 61 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 65  back down to the
3c180 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 2e 0a   current value..
3c190 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
3c1a0 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28  [sqlite3_status(
3c1b0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
3c1c0 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e 0a  stmt_status()]..
3c1d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
3c1e0 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
3c1f0 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62  L int sqlite3_db
3c200 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 2a  _status(sqlite3*
3c210 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70  , int op, int *p
3c220 43 75 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 72  Cur, int *pHiwtr
3c230 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b  , int resetFlg);
3c240 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3c250 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74  : Status Paramet
3c260 65 72 73 20 66 6f 72 20 64 61 74 61 62 61 73 65  ers for database
3c270 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 31   connections {H1
3c280 37 35 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 2a  7520} <H17500>.*
3c290 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3c2a0 2a 0a 2a 2a 20 53 74 61 74 75 73 20 76 65 72 62  *.** Status verb
3c2b0 73 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 64  s for [sqlite3_d
3c2c0 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a  b_status()]..**.
3c2d0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53  ** <dl>.** <dt>S
3c2e0 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c  QLITE_DBSTATUS_L
3c2f0 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64  OOKASIDE_USED</d
3c300 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  t>.** <dd>This p
3c310 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73  arameter returns
3c320 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   the number of l
3c330 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20  ookaside memory 
3c340 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a  slots currently.
3c350 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c  ** checked out.<
3c360 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f  /dd>.** </dl>.*/
3c370 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3c380 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49  DBSTATUS_LOOKASI
3c390 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a  DE_USED     0...
3c3a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3c3b0 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65  Prepared Stateme
3c3c0 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 35 35  nt Status {H1755
3c3d0 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45  0} <S60200>.** E
3c3e0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
3c3f0 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 64 20  * Each prepared 
3c400 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61  statement mainta
3c410 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b  ins various.** [
3c420 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55  SQLITE_STMTSTATU
3c430 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72  S_SORT | counter
3c440 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 20  s] that measure 
3c450 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66  the number.** of
3c460 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 65   times it has pe
3c470 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63  rformed specific
3c480 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68   operations.  Th
3c490 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e  ese counters can
3c4a0 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d  .** be used to m
3c4b0 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f  onitor the perfo
3c4c0 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 72  rmance character
3c4d0 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 72  istics of the pr
3c4e0 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d  epared.** statem
3c4f0 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70  ents.  For examp
3c500 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65  le, if the numbe
3c510 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73  r of table steps
3c520 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 73   greatly exceeds
3c530 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
3c540 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 73  f table searches
3c550 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c   or result rows,
3c560 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64   that would tend
3c570 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20   to indicate.** 
3c580 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72 65  that the prepare
3c590 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75  d statement is u
3c5a0 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c  sing a full tabl
3c5b0 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68  e scan rather th
3c5c0 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20  an.** an index. 
3c5d0 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74   .**.** This int
3c5e0 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74  erface is used t
3c5f0 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 20 72  o retrieve and r
3c600 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c  eset counter val
3c610 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70  ues from.** a [p
3c620 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3c630 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61  t].  The first a
3c640 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70  rgument is the p
3c650 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3c660 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62  t.** object to b
3c670 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20  e interrogated. 
3c680 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
3c690 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e  ment.** is an in
3c6a0 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61  teger code for a
3c6b0 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54   specific [SQLIT
3c6c0 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52  E_STMTSTATUS_SOR
3c6d0 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20  T | counter].** 
3c6e0 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74  to be interrogat
3c6f0 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72  ed. .** The curr
3c700 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ent value of the
3c710 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74   requested count
3c720 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
3c730 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 74 46  ** If the resetF
3c740 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  lg is true, then
3c750 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20   the counter is 
3c760 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66  reset to zero af
3c770 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65  ter this.** inte
3c780 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72  rface call retur
3c790 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ns..**.** See al
3c7a0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61  so: [sqlite3_sta
3c7b0 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  tus()] and [sqli
3c7c0 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d  te3_db_status()]
3c7d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3c7e0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
3c7f0 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TAL int sqlite3_
3c800 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69  stmt_status(sqli
3c810 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f  te3_stmt*, int o
3c820 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b  p,int resetFlg);
3c830 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3c840 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74  : Status Paramet
3c850 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65 64  ers for prepared
3c860 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37   statements {H17
3c870 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a  570} <H17550>.**
3c880 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
3c890 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f  .** These prepro
3c8a0 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65  cessor macros de
3c8b0 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64  fine integer cod
3c8c0 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75  es that name cou
3c8d0 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61  nter.** values a
3c8e0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
3c8f0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74  he [sqlite3_stmt
3c900 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72  _status()] inter
3c910 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61  face..** The mea
3c920 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72  nings of the var
3c930 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72  ious counters ar
3c940 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  e as follows:.**
3c950 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e  .** <dl>.** <dt>
3c960 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55  SQLITE_STMTSTATU
3c970 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c  S_FULLSCAN_STEP<
3c980 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3c990 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
3c9a0 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c  f times that SQL
3c9b0 69 74 65 20 68 61 73 20 73 74 65 70 70 65 64 20  ite has stepped 
3c9c0 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20  forward in.** a 
3c9d0 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66  table as part of
3c9e0 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63   a full table sc
3c9f0 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65  an.  Large numbe
3ca00 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e  rs for this coun
3ca10 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63  ter.** may indic
3ca20 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65  ate opportunitie
3ca30 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  s for performanc
3ca40 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68  e improvement th
3ca50 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75  rough .** carefu
3ca60 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73  l use of indices
3ca70 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
3ca80 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54  >SQLITE_STMTSTAT
3ca90 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20  US_SORT</dt>.** 
3caa0 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 20  <dd>This is the 
3cab0 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f  number of sort o
3cac0 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68  perations that h
3cad0 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a  ave occurred..**
3cae0 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75   A non-zero valu
3caf0 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65  e in this counte
3cb00 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61  r may indicate a
3cb10 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f  n opportunity to
3cb20 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20  .** improvement 
3cb30 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f  performance thro
3cb40 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 20  ugh careful use 
3cb50 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e  of indices.</dd>
3cb60 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a  .**.** </dl>.*/.
3cb70 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3cb80 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43  TMTSTATUS_FULLSC
3cb90 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64  AN_STEP     1.#d
3cba0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d  efine SQLITE_STM
3cbb0 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20  TSTATUS_SORT    
3cbc0 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a            2../*.
3cbd0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75 73  ** CAPI3REF: Cus
3cbe0 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f  tom Page Cache O
3cbf0 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d  bject.** EXPERIM
3cc00 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ENTAL.**.** The 
3cc10 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74  sqlite3_pcache t
3cc20 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20  ype is opaque.  
3cc30 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  It is implemente
3cc40 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67  d by.** the plug
3cc50 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54  gable module.  T
3cc60 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68  he SQLite core h
3cc70 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20  as no knowledge 
3cc80 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f  of.** its size o
3cc90 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63  r internal struc
3cca0 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64  ture and never d
3ccb0 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a 2a  eals with the.**
3ccc0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
3ccd0 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62 79  object except by
3cce0 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 73   holding and pas
3ccf0 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a  sing pointers.**
3cd00 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a   to the object..
3cd10 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74  **.** See [sqlit
3cd20 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
3cd30 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  s] for additiona
3cd40 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
3cd50 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
3cd60 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
3cd70 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b 0a  sqlite3_pcache;.
3cd80 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3cd90 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66   Application Def
3cda0 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 65 2e  ined Page Cache.
3cdb0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
3cdc0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  .**.** The [sqli
3cdd0 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c  te3_config]([SQL
3cde0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
3cdf0 45 5d 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61  E], ...) interfa
3ce00 63 65 20 63 61 6e 0a 2a 2a 20 72 65 67 69 73 74  ce can.** regist
3ce10 65 72 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  er an alternativ
3ce20 65 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  e page cache imp
3ce30 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20 70  lementation by p
3ce40 61 73 73 69 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a  assing in an .**
3ce50 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
3ce60 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
3ce70 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72  methods structur
3ce80 65 2e 20 54 68 65 20 6d 61 6a 6f 72 69 74 79 20  e. The majority 
3ce90 6f 66 20 74 68 65 20 0a 2a 2a 20 68 65 61 70 20  of the .** heap 
3cea0 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 73  memory used by s
3ceb0 71 6c 69 74 65 20 69 73 20 75 73 65 64 20 62 79  qlite is used by
3cec0 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
3ced0 74 6f 20 63 61 63 68 65 20 64 61 74 61 20 72 65  to cache data re
3cee0 61 64 20 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20  ad .** from, or 
3cef0 72 65 61 64 79 20 74 6f 20 62 65 20 77 72 69 74  ready to be writ
3cf00 74 65 6e 20 74 6f 2c 20 74 68 65 20 64 61 74 61  ten to, the data
3cf10 62 61 73 65 20 66 69 6c 65 2e 20 42 79 20 69 6d  base file. By im
3cf20 70 6c 65 6d 65 6e 74 69 6e 67 20 61 20 0a 2a 2a  plementing a .**
3cf30 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63   custom page cac
3cf40 68 65 20 75 73 69 6e 67 20 74 68 69 73 20 41 50  he using this AP
3cf50 49 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  I, an applicatio
3cf60 6e 20 63 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f  n can control mo
3cf70 72 65 20 0a 2a 2a 20 70 72 65 63 69 73 65 6c 79  re .** precisely
3cf80 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d   the amount of m
3cf90 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 62  emory consumed b
3cfa0 79 20 73 71 6c 69 74 65 2c 20 74 68 65 20 77 61  y sqlite, the wa
3cfb0 79 20 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20 73  y in which .** s
3cfc0 61 69 64 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c  aid memory is al
3cfd0 6c 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c 65  located and rele
3cfe0 61 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70 6f  ased, and the po
3cff0 6c 69 63 69 65 73 20 75 73 65 64 20 74 6f 20 0a  licies used to .
3d000 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78 61  ** determine exa
3d010 63 74 6c 79 20 77 68 69 63 68 20 70 61 72 74 73  ctly which parts
3d020 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66   of a database f
3d030 69 6c 65 20 61 72 65 20 63 61 63 68 65 64 20 61  ile are cached a
3d040 6e 64 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c  nd for .** how l
3d050 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ong..**.** The c
3d060 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 73  ontents of the s
3d070 74 72 75 63 74 75 72 65 20 61 72 65 20 63 6f 70  tructure are cop
3d080 69 65 64 20 74 6f 20 61 6e 20 69 6e 74 65 72 6e  ied to an intern
3d090 61 6c 20 62 75 66 66 65 72 20 62 79 20 73 71 6c  al buffer by sql
3d0a0 69 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68  ite.** within th
3d0b0 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  e call to [sqlit
3d0c0 65 33 5f 63 6f 6e 66 69 67 5d 2e 0a 2a 2a 0a 2a  e3_config]..**.*
3d0d0 2a 20 54 68 65 20 78 49 6e 69 74 28 29 20 6d 65  * The xInit() me
3d0e0 74 68 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 6f  thod is called o
3d0f0 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c  nce for each cal
3d100 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e  l to [sqlite3_in
3d110 69 74 69 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 28  itialize()].** (
3d120 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63  usually only onc
3d130 65 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66  e during the lif
3d140 65 74 69 6d 65 20 6f 66 20 74 68 65 20 70 72 6f  etime of the pro
3d150 63 65 73 73 29 2e 20 49 74 20 69 73 20 70 61 73  cess). It is pas
3d160 73 65 64 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66  sed.** a copy of
3d170 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
3d180 63 68 65 5f 6d 65 74 68 6f 64 73 2e 70 41 72 67  che_methods.pArg
3d190 20 76 61 6c 75 65 2e 20 49 74 20 63 61 6e 20 62   value. It can b
3d1a0 65 20 75 73 65 64 20 74 6f 20 73 65 74 0a 2a 2a  e used to set.**
3d1b0 20 75 70 20 67 6c 6f 62 61 6c 20 73 74 72 75 63   up global struc
3d1c0 74 75 72 65 73 20 61 6e 64 20 6d 75 74 65 78 65  tures and mutexe
3d1d0 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68  s required by th
3d1e0 65 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61  e custom page ca
3d1f0 63 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  che .** implemen
3d200 74 61 74 69 6f 6e 2e 20 54 68 65 20 78 53 68 75  tation. The xShu
3d210 74 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69  tdown() method i
3d220 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  s called from wi
3d230 74 68 69 6e 20 0a 2a 2a 20 5b 73 71 6c 69 74 65  thin .** [sqlite
3d240 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2c 20 69  3_shutdown()], i
3d250 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
3d260 6e 20 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41  n invokes this A
3d270 50 49 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73  PI. It can be us
3d280 65 64 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20 75  ed.** to clean u
3d290 70 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e  p any outstandin
3d2a0 67 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f  g resources befo
3d2b0 72 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64  re process shutd
3d2c0 6f 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64  own, if required
3d2d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65  ..**.** The xCre
3d2e0 61 74 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20  ate() method is 
3d2f0 75 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63  used to construc
3d300 74 20 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e  t a new cache in
3d310 73 74 61 6e 63 65 2e 20 54 68 65 0a 2a 2a 20 66  stance. The.** f
3d320 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20  irst parameter, 
3d330 73 7a 50 61 67 65 2c 20 69 73 20 74 68 65 20 73  szPage, is the s
3d340 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
3d350 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 6d  the pages that m
3d360 75 73 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f 63 61  ust.** be alloca
3d370 74 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65  ted by the cache
3d380 2e 20 73 7a 50 61 67 65 20 77 69 6c 6c 20 6e 6f  . szPage will no
3d390 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20  t be a power of 
3d3a0 74 77 6f 2e 20 54 68 65 0a 2a 2a 20 73 65 63 6f  two. The.** seco
3d3b0 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 62 50 75  nd argument, bPu
3d3c0 72 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65  rgeable, is true
3d3d0 20 69 66 20 74 68 65 20 63 61 63 68 65 20 62 65   if the cache be
3d3e0 69 6e 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c  ing created will
3d3f0 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63  .** be used to c
3d400 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61  ache database pa
3d410 67 65 73 20 72 65 61 64 20 66 72 6f 6d 20 61 20  ges read from a 
3d420 66 69 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64  file stored on d
3d430 69 73 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65  isk, or.** false
3d440 20 69 66 20 69 74 20 69 73 20 75 73 65 64 20 66   if it is used f
3d450 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  or an in-memory 
3d460 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 61  database. The ca
3d470 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
3d480 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68  on.** does not h
3d490 61 76 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69  ave to do anythi
3d4a0 6e 67 20 73 70 65 63 69 61 6c 20 62 61 73 65 64  ng special based
3d4b0 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66   on the value of
3d4c0 20 62 50 75 72 67 65 61 62 6c 65 2c 0a 2a 2a 20   bPurgeable,.** 
3d4d0 69 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76  it is purely adv
3d4e0 69 73 6f 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  isory. .**.** Th
3d4f0 65 20 78 43 61 63 68 65 73 69 7a 65 28 29 20 6d  e xCachesize() m
3d500 65 74 68 6f 64 20 6d 61 79 20 62 65 20 63 61 6c  ethod may be cal
3d510 6c 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20  led at any time 
3d520 62 79 20 53 51 4c 69 74 65 20 74 6f 20 73 65 74  by SQLite to set
3d530 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65   the.** suggeste
3d540 64 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 2d  d maximum cache-
3d550 73 69 7a 65 20 28 6e 75 6d 62 65 72 20 6f 66 20  size (number of 
3d560 70 61 67 65 73 20 73 74 6f 72 65 64 20 62 79 29  pages stored by)
3d570 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 6e   the cache.** in
3d580 73 74 61 6e 63 65 20 70 61 73 73 65 64 20 61 73  stance passed as
3d590 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
3d5a0 65 6e 74 2e 20 54 68 69 73 20 69 73 20 74 68 65  ent. This is the
3d5b0 20 76 61 6c 75 65 20 63 6f 6e 66 69 67 75 72 65   value configure
3d5c0 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53  d using.** the S
3d5d0 51 4c 69 74 65 20 22 5b 50 52 41 47 4d 41 20 63  QLite "[PRAGMA c
3d5e0 61 63 68 65 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d  ache_size]" comm
3d5f0 61 6e 64 2e 20 41 73 20 77 69 74 68 20 74 68 65  and. As with the
3d600 20 62 50 75 72 67 65 61 62 6c 65 20 70 61 72 61   bPurgeable para
3d610 6d 65 74 65 72 2c 0a 2a 2a 20 74 68 65 20 69 6d  meter,.** the im
3d620 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
3d630 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
3d640 64 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63  do anything spec
3d650 69 61 6c 20 77 69 74 68 20 74 68 69 73 0a 2a 2a  ial with this.**
3d660 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 61 64   value, it is ad
3d670 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a  visory only..**.
3d680 2a 2a 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e  ** The xPagecoun
3d690 74 28 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c  t() method shoul
3d6a0 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  d return the num
3d6b0 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 75 72  ber of pages cur
3d6c0 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64  rently.** stored
3d6d0 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 73 75   in the cache su
3d6e0 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67  pplied as an arg
3d6f0 75 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  ument..** .** Th
3d700 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f  e xFetch() metho
3d710 64 20 69 73 20 75 73 65 64 20 74 6f 20 66 65 74  d is used to fet
3d720 63 68 20 61 20 70 61 67 65 20 61 6e 64 20 72 65  ch a page and re
3d730 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
3d740 6f 20 69 74 2e 20 0a 2a 2a 20 41 20 27 70 61 67  o it. .** A 'pag
3d750 65 27 2c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74  e', in this cont
3d760 65 78 74 2c 20 69 73 20 61 20 62 75 66 66 65 72  ext, is a buffer
3d770 20 6f 66 20 73 7a 50 61 67 65 20 62 79 74 65 73   of szPage bytes
3d780 20 61 6c 69 67 6e 65 64 20 61 74 20 61 6e 0a 2a   aligned at an.*
3d790 2a 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72  * 8-byte boundar
3d7a0 79 2e 20 54 68 65 20 70 61 67 65 20 74 6f 20 62  y. The page to b
3d7b0 65 20 66 65 74 63 68 65 64 20 69 73 20 64 65 74  e fetched is det
3d7c0 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b  ermined by the k
3d7d0 65 79 2e 20 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d  ey. The.** mimim
3d7e0 75 6d 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20  um key value is 
3d7f0 31 2e 20 41 66 74 65 72 20 69 74 20 68 61 73 20  1. After it has 
3d800 62 65 65 6e 20 72 65 74 72 69 65 76 65 64 20 75  been retrieved u
3d810 73 69 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65  sing xFetch, the
3d820 20 70 61 67 65 20 0a 2a 2a 20 69 73 20 63 6f 6e   page .** is con
3d830 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 70 69  sidered to be pi
3d840 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  nned..**.** If t
3d850 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67  he requested pag
3d860 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  e is already in 
3d870 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2c 20  the page cache, 
3d880 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  then a pointer t
3d890 6f 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 64 20  o.** the cached 
3d8a0 62 75 66 66 65 72 20 73 68 6f 75 6c 64 20 62 65  buffer should be
3d8b0 20 72 65 74 75 72 6e 65 64 20 77 69 74 68 20 69   returned with i
3d8c0 74 73 20 63 6f 6e 74 65 6e 74 73 20 69 6e 74 61  ts contents inta
3d8d0 63 74 2e 20 49 66 20 74 68 65 0a 2a 2a 20 70 61  ct. If the.** pa
3d8e0 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  ge is not alread
3d8f0 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20  y in the cache, 
3d900 74 68 65 6e 20 74 68 65 20 65 78 70 65 63 74 65  then the expecte
3d910 64 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 74  d behaviour of t
3d920 68 65 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 64  he.** cache is d
3d930 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65  etermined by the
3d940 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72   value of the cr
3d950 65 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74  eateFlag paramet
3d960 65 72 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20  er passed.** to 
3d970 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e  xFetch, accordin
3d980 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  g to the followi
3d990 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20  ng table:.**.** 
3d9a0 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20  <table border=1 
3d9b0 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d  width=85% align=
3d9c0 63 65 6e 74 65 72 3e 0a 2a 2a 20 20 20 3c 74 72  center>.**   <tr
3d9d0 3e 3c 74 68 3e 63 72 65 61 74 65 46 6c 61 67 3c  ><th>createFlag<
3d9e0 74 68 3e 45 78 70 65 63 74 65 64 20 42 65 68 61  th>Expected Beha
3d9f0 76 69 6f 75 72 0a 2a 2a 20 20 20 3c 74 72 3e 3c  viour.**   <tr><
3da00 74 64 3e 30 3c 74 64 3e 4e 55 4c 4c 20 73 68 6f  td>0<td>NULL sho
3da10 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e  uld be returned.
3da20 20 4e 6f 20 6e 65 77 20 63 61 63 68 65 20 65 6e   No new cache en
3da30 74 72 79 20 69 73 20 63 72 65 61 74 65 64 2e 0a  try is created..
3da40 2a 2a 20 20 20 3c 74 72 3e 3c 74 64 3e 31 3c 74  **   <tr><td>1<t
3da50 64 3e 49 66 20 63 72 65 61 74 65 46 6c 61 67 20  d>If createFlag 
3da60 69 73 20 73 65 74 20 74 6f 20 31 2c 20 74 68 69  is set to 1, thi
3da70 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  s indicates that
3da80 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
3da90 20 20 20 20 53 51 4c 69 74 65 20 69 73 20 68 6f      SQLite is ho
3daa0 6c 64 69 6e 67 20 70 69 6e 6e 65 64 20 70 61 67  lding pinned pag
3dab0 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  es that can be u
3dac0 6e 70 69 6e 6e 65 64 0a 2a 2a 20 20 20 20 20 20  npinned.**      
3dad0 20 20 20 20 20 20 20 20 20 20 62 79 20 77 72 69            by wri
3dae0 74 69 6e 67 20 74 68 65 69 72 20 63 6f 6e 74 65  ting their conte
3daf0 6e 74 73 20 74 6f 20 74 68 65 20 64 61 74 61 62  nts to the datab
3db00 61 73 65 20 66 69 6c 65 20 28 61 0a 2a 2a 20 20  ase file (a.**  
3db10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
3db20 6c 61 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69  latively expensi
3db30 76 65 20 6f 70 65 72 61 74 69 6f 6e 29 2e 20 49  ve operation). I
3db40 6e 20 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e  n this situation
3db50 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
3db60 20 20 20 20 20 20 20 63 61 63 68 65 20 69 6d 70         cache imp
3db70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20  lementation has 
3db80 74 77 6f 20 63 68 6f 69 63 65 73 3a 20 69 74 20  two choices: it 
3db90 63 61 6e 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2c  can return NULL,
3dba0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3dbb0 20 20 20 69 6e 20 77 68 69 63 68 20 63 61 73 65     in which case
3dbc0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 74 74   SQLite will att
3dbd0 65 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e  empt to unpin on
3dbe0 65 20 6f 72 20 6d 6f 72 65 20 0a 2a 2a 20 20 20  e or more .**   
3dbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
3dc00 65 73 20 62 65 66 6f 72 65 20 72 65 2d 72 65 71  es before re-req
3dc10 75 65 73 74 69 6e 67 20 74 68 65 20 73 61 6d 65  uesting the same
3dc20 20 70 61 67 65 2c 20 6f 72 20 69 74 20 63 61 6e   page, or it can
3dc30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
3dc40 20 20 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65     allocate a ne
3dc50 77 20 70 61 67 65 20 61 6e 64 20 72 65 74 75 72  w page and retur
3dc60 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  n a pointer to i
3dc70 74 2e 20 49 66 20 61 20 6e 65 77 0a 2a 2a 20 20  t. If a new.**  
3dc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
3dc90 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c  ge is allocated,
3dca0 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   then the first 
3dcb0 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 62 79  sizeof(void*) by
3dcc0 74 65 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20  tes of.**       
3dcd0 20 20 20 20 20 20 20 20 20 69 74 20 28 61 74 20           it (at 
3dce0 6c 65 61 73 74 29 20 6d 75 73 74 20 62 65 20 7a  least) must be z
3dcf0 65 72 6f 65 64 20 62 65 66 6f 72 65 20 69 74 20  eroed before it 
3dd00 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
3dd10 20 20 3c 74 72 3e 3c 74 64 3e 32 3c 74 64 3e 49    <tr><td>2<td>I
3dd20 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20  f createFlag is 
3dd30 73 65 74 20 74 6f 20 32 2c 20 74 68 65 6e 20 53  set to 2, then S
3dd40 51 4c 69 74 65 20 69 73 20 6e 6f 74 20 68 6f 6c  QLite is not hol
3dd50 64 69 6e 67 20 61 6e 79 0a 2a 2a 20 20 20 20 20  ding any.**     
3dd60 20 20 20 20 20 20 20 20 20 20 20 70 69 6e 6e 65             pinne
3dd70 64 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74  d pages associat
3dd80 65 64 20 77 69 74 68 20 74 68 65 20 73 70 65 63  ed with the spec
3dd90 69 66 69 63 20 63 61 63 68 65 20 70 61 73 73 65  ific cache passe
3dda0 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
3ddb0 20 20 20 20 61 73 20 74 68 65 20 66 69 72 73 74      as the first
3ddc0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 46 65   argument to xFe
3ddd0 74 63 68 28 29 20 74 68 61 74 20 63 61 6e 20 62  tch() that can b
3dde0 65 20 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65 0a  e unpinned. The.
3ddf0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3de00 20 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e    cache implemen
3de10 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 74  tation should at
3de20 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74  tempt to allocat
3de30 65 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20  e a new.**      
3de40 20 20 20 20 20 20 20 20 20 20 63 61 63 68 65 20            cache 
3de50 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e  entry and return
3de60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
3de70 2e 20 41 67 61 69 6e 2c 20 74 68 65 20 66 69 72  . Again, the fir
3de80 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  st.**           
3de90 20 20 20 20 20 73 69 7a 65 6f 66 28 76 6f 69 64       sizeof(void
3dea0 2a 29 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  *) bytes of the 
3deb0 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 7a  page should be z
3dec0 65 72 6f 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a  eroed before .**
3ded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3dee0 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  it is returned. 
3def0 49 66 20 74 68 65 20 78 46 65 74 63 68 28 29 20  If the xFetch() 
3df00 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 4e  method returns N
3df10 55 4c 4c 20 77 68 65 6e 20 0a 2a 2a 20 20 20 20  ULL when .**    
3df20 20 20 20 20 20 20 20 20 20 20 20 20 63 72 65 61              crea
3df30 74 65 46 6c 61 67 3d 3d 32 2c 20 53 51 4c 69 74  teFlag==2, SQLit
3df40 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 61  e assumes that a
3df50 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
3df60 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  on .**          
3df70 20 20 20 20 20 20 66 61 69 6c 65 64 20 61 6e 64        failed and
3df80 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
3df90 4e 4f 4d 45 4d 20 74 6f 20 74 68 65 20 75 73 65  NOMEM to the use
3dfa0 72 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a  r..** </table>.*
3dfb0 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73  *.** xUnpin() is
3dfc0 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74   called by SQLit
3dfd0 65 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72  e with a pointer
3dfe0 20 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20   to a currently 
3dff0 70 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61  pinned page.** a
3e000 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67  s its second arg
3e010 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68  ument. If the th
3e020 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64  ird parameter, d
3e030 69 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a  iscard, is non-z
3e040 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ero,.** then the
3e050 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20   page should be 
3e060 65 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65  evicted from the
3e070 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20   cache. In this 
3e080 63 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20  case SQLite .** 
3e090 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
3e0a0 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70   next time the p
3e0b0 61 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64  age is retrieved
3e0c0 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20   from the cache 
3e0d0 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65  using.** the xFe
3e0e0 74 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74  tch() method, it
3e0f0 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e   will be zeroed.
3e100 20 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20   If the discard 
3e110 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20  parameter is.** 
3e120 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70  zero, then the p
3e130 61 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65  age is considere
3e140 64 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64  d to be unpinned
3e150 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c  . The cache impl
3e160 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61  ementation.** ma
3e170 79 20 63 68 6f 6f 73 65 20 74 6f 20 72 65 63 6c  y choose to recl
3e180 61 69 6d 20 28 66 72 65 65 20 6f 72 20 72 65 63  aim (free or rec
3e190 79 63 6c 65 29 20 75 6e 70 69 6e 6e 65 64 20 70  ycle) unpinned p
3e1a0 61 67 65 73 20 61 74 20 61 6e 79 20 74 69 6d 65  ages at any time
3e1b0 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 61 73 73 75  ..** SQLite assu
3e1c0 6d 65 73 20 74 68 61 74 20 6e 65 78 74 20 74 69  mes that next ti
3e1d0 6d 65 20 74 68 65 20 70 61 67 65 20 69 73 20 72  me the page is r
3e1e0 65 74 72 69 65 76 65 64 20 66 72 6f 6d 20 74 68  etrieved from th
3e1f0 65 20 63 61 63 68 65 0a 2a 2a 20 69 74 20 77 69  e cache.** it wi
3e200 6c 6c 20 65 69 74 68 65 72 20 62 65 20 7a 65 72  ll either be zer
3e210 6f 65 64 2c 20 6f 72 20 63 6f 6e 74 61 69 6e 20  oed, or contain 
3e220 74 68 65 20 73 61 6d 65 20 64 61 74 61 20 74 68  the same data th
3e230 61 74 20 69 74 20 64 69 64 20 77 68 65 6e 20 69  at it did when i
3e240 74 0a 2a 2a 20 77 61 73 20 75 6e 70 69 6e 6e 65  t.** was unpinne
3e250 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63  d..**.** The cac
3e260 68 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  he is not requir
3e270 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 61 6e  ed to perform an
3e280 79 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  y reference coun
3e290 74 69 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0a  ting. A single .
3e2a0 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70 69  ** call to xUnpi
3e2b0 6e 28 29 20 75 6e 70 69 6e 73 20 74 68 65 20 70  n() unpins the p
3e2c0 61 67 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f  age regardless o
3e2d0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
3e2e0 70 72 69 6f 72 20 63 61 6c 6c 73 20 0a 2a 2a 20  prior calls .** 
3e2f0 74 6f 20 78 46 65 74 63 68 28 29 2e 0a 2a 2a 0a  to xFetch()..**.
3e300 2a 2a 20 54 68 65 20 78 52 65 6b 65 79 28 29 20  ** The xRekey() 
3e310 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74  method is used t
3e320 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6b 65 79  o change the key
3e330 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65   value associate
3e340 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 70 61  d with the.** pa
3e350 67 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ge passed as the
3e360 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
3e370 20 66 72 6f 6d 20 6f 6c 64 4b 65 79 20 74 6f 20   from oldKey to 
3e380 6e 65 77 4b 65 79 2e 20 49 66 20 74 68 65 20 63  newKey. If the c
3e390 61 63 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73  ache.** previous
3e3a0 6c 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65  ly contains an e
3e3b0 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  ntry associated 
3e3c0 77 69 74 68 20 6e 65 77 4b 65 79 2c 20 69 74 20  with newKey, it 
3e3d0 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 69 73  should be.** dis
3e3e0 63 61 72 64 65 64 2e 20 41 6e 79 20 70 72 69 6f  carded. Any prio
3e3f0 72 20 63 61 63 68 65 20 65 6e 74 72 79 20 61 73  r cache entry as
3e400 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65  sociated with ne
3e410 77 4b 65 79 20 69 73 20 67 75 61 72 61 6e 74 65  wKey is guarante
3e420 65 64 20 6e 6f 74 0a 2a 2a 20 74 6f 20 62 65 20  ed not.** to be 
3e430 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68  pinned..**.** Wh
3e440 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20  en SQLite calls 
3e450 74 68 65 20 78 54 72 75 6e 63 61 74 65 28 29 20  the xTruncate() 
3e460 6d 65 74 68 6f 64 2c 20 74 68 65 20 63 61 63 68  method, the cach
3e470 65 20 6d 75 73 74 20 64 69 73 63 61 72 64 20 61  e must discard a
3e480 6c 6c 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 63  ll.** existing c
3e490 61 63 68 65 20 65 6e 74 72 69 65 73 20 77 69 74  ache entries wit
3e4a0 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 28  h page numbers (
3e4b0 6b 65 79 73 29 20 67 72 65 61 74 65 72 20 74 68  keys) greater th
3e4c0 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74  an or equal.** t
3e4d0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  o the value of t
3e4e0 68 65 20 69 4c 69 6d 69 74 20 70 61 72 61 6d 65  he iLimit parame
3e4f0 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 78 54  ter passed to xT
3e500 72 75 6e 63 61 74 65 28 29 2e 20 49 66 20 61 6e  runcate(). If an
3e510 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61  y.** of these pa
3e520 67 65 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20  ges are pinned, 
3e530 74 68 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69  they are implici
3e540 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65  tly unpinned, me
3e550 61 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 74 68  aning that.** th
3e560 65 79 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79  ey can be safely
3e570 20 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a   discarded..**.*
3e580 2a 20 54 68 65 20 78 44 65 73 74 72 6f 79 28 29  * The xDestroy()
3e590 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20   method is used 
3e5a0 74 6f 20 64 65 6c 65 74 65 20 61 20 63 61 63 68  to delete a cach
3e5b0 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 78  e allocated by x
3e5c0 43 72 65 61 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c  Create()..** All
3e5d0 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63   resources assoc
3e5e0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73  iated with the s
3e5f0 70 65 63 69 66 69 65 64 20 63 61 63 68 65 20 73  pecified cache s
3e600 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 2e 20  hould be freed. 
3e610 41 66 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67  After.** calling
3e620 20 74 68 65 20 78 44 65 73 74 72 6f 79 28 29 20   the xDestroy() 
3e630 6d 65 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63  method, SQLite c
3e640 6f 6e 73 69 64 65 72 73 20 74 68 65 20 5b 73 71  onsiders the [sq
3e650 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 5d 0a 2a  lite3_pcache*].*
3e660 2a 20 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64  * handle invalid
3e670 2c 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75  , and will not u
3e680 73 65 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f  se it with any o
3e690 74 68 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61  ther sqlite3_pca
3e6a0 63 68 65 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 66  che_methods.** f
3e6b0 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70  unctions..*/.typ
3e6c0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
3e6d0 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
3e6e0 64 73 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  ds sqlite3_pcach
3e6f0 65 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63  e_methods;.struc
3e700 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t sqlite3_pcache
3e710 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69  _methods {.  voi
3e720 64 20 2a 70 41 72 67 3b 0a 20 20 69 6e 74 20 28  d *pArg;.  int (
3e730 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 0a  *xInit)(void*);.
3e740 20 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f    void (*xShutdo
3e750 77 6e 29 28 76 6f 69 64 2a 29 3b 0a 20 20 73 71  wn)(void*);.  sq
3e760 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 28 2a  lite3_pcache *(*
3e770 78 43 72 65 61 74 65 29 28 69 6e 74 20 73 7a 50  xCreate)(int szP
3e780 61 67 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61  age, int bPurgea
3e790 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  ble);.  void (*x
3e7a0 43 61 63 68 65 73 69 7a 65 29 28 73 71 6c 69 74  Cachesize)(sqlit
3e7b0 65 33 5f 70 63 61 63 68 65 2a 2c 20 69 6e 74 20  e3_pcache*, int 
3e7c0 6e 43 61 63 68 65 73 69 7a 65 29 3b 0a 20 20 69  nCachesize);.  i
3e7d0 6e 74 20 28 2a 78 50 61 67 65 63 6f 75 6e 74 29  nt (*xPagecount)
3e7e0 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
3e7f0 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 46 65  );.  void *(*xFe
3e800 74 63 68 29 28 73 71 6c 69 74 65 33 5f 70 63 61  tch)(sqlite3_pca
3e810 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6b  che*, unsigned k
3e820 65 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c  ey, int createFl
3e830 61 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55  ag);.  void (*xU
3e840 6e 70 69 6e 29 28 73 71 6c 69 74 65 33 5f 70 63  npin)(sqlite3_pc
3e850 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e  ache*, void*, in
3e860 74 20 64 69 73 63 61 72 64 29 3b 0a 20 20 76 6f  t discard);.  vo
3e870 69 64 20 28 2a 78 52 65 6b 65 79 29 28 73 71 6c  id (*xRekey)(sql
3e880 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f  ite3_pcache*, vo
3e890 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6f 6c  id*, unsigned ol
3e8a0 64 4b 65 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e  dKey, unsigned n
3e8b0 65 77 4b 65 79 29 3b 0a 20 20 76 6f 69 64 20 28  ewKey);.  void (
3e8c0 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69  *xTruncate)(sqli
3e8d0 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73  te3_pcache*, uns
3e8e0 69 67 6e 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20  igned iLimit);. 
3e8f0 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
3e900 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  )(sqlite3_pcache
3e910 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  *);.};../*.** CA
3e920 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42  PI3REF: Online B
3e930 61 63 6b 75 70 20 4f 62 6a 65 63 74 0a 2a 2a 20  ackup Object.** 
3e940 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
3e950 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62  ** The sqlite3_b
3e960 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 72 65 63  ackup object rec
3e970 6f 72 64 73 20 73 74 61 74 65 20 69 6e 66 6f 72  ords state infor
3e980 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6e 20  mation about an 
3e990 6f 6e 67 6f 69 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e  ongoing.** onlin
3e9a0 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  e backup operati
3e9b0 6f 6e 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  on.  The sqlite3
3e9c0 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 69  _backup object i
3e9d0 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20  s created by.** 
3e9e0 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
3e9f0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
3ea00 5d 20 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79  ] and is destroy
3ea10 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
3ea20 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b  ** [sqlite3_back
3ea30 75 70 5f 66 69 6e 69 73 68 28 29 5d 2e 0a 2a 2a  up_finish()]..**
3ea40 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55  .** See Also: [U
3ea50 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20  sing the SQLite 
3ea60 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50  Online Backup AP
3ea70 49 5d 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  I].*/.typedef st
3ea80 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 61 63  ruct sqlite3_bac
3ea90 6b 75 70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  kup sqlite3_back
3eaa0 75 70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  up;../*.** CAPI3
3eab0 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b  REF: Online Back
3eac0 75 70 20 41 50 49 2e 0a 2a 2a 20 45 58 50 45 52  up API..** EXPER
3ead0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
3eae0 69 73 20 41 50 49 20 69 73 20 75 73 65 64 20 74  is API is used t
3eaf0 6f 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 20  o overwrite the 
3eb00 63 6f 6e 74 65 6e 74 73 20 6f 66 20 6f 6e 65 20  contents of one 
3eb10 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74 68  database with th
3eb20 61 74 0a 2a 2a 20 6f 66 20 61 6e 6f 74 68 65 72  at.** of another
3eb30 2e 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 65  . It is useful e
3eb40 69 74 68 65 72 20 66 6f 72 20 63 72 65 61 74 69  ither for creati
3eb50 6e 67 20 62 61 63 6b 75 70 73 20 6f 66 20 64 61  ng backups of da
3eb60 74 61 62 61 73 65 73 20 6f 72 0a 2a 2a 20 66 6f  tabases or.** fo
3eb70 72 20 63 6f 70 79 69 6e 67 20 69 6e 2d 6d 65 6d  r copying in-mem
3eb80 6f 72 79 20 64 61 74 61 62 61 73 65 73 20 74 6f  ory databases to
3eb90 20 6f 72 20 66 72 6f 6d 20 70 65 72 73 69 73 74   or from persist
3eba0 65 6e 74 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a  ent files. .**.*
3ebb0 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69  * See Also: [Usi
3ebc0 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e  ng the SQLite On
3ebd0 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d  line Backup API]
3ebe0 0a 2a 2a 0a 2a 2a 20 45 78 63 6c 75 73 69 76 65  .**.** Exclusive
3ebf0 20 61 63 63 65 73 73 20 69 73 20 72 65 71 75 69   access is requi
3ec00 72 65 64 20 74 6f 20 74 68 65 20 64 65 73 74 69  red to the desti
3ec10 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
3ec20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 64 75 72 61  for the .** dura
3ec30 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 65 72  tion of the oper
3ec40 61 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 20 74  ation. However t
3ec50 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
3ec60 73 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 65  se is only.** re
3ec70 61 64 2d 6c 6f 63 6b 65 64 20 77 68 69 6c 65 20  ad-locked while 
3ec80 69 74 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62  it is actually b
3ec90 65 69 6e 67 20 72 65 61 64 2c 20 69 74 20 69 73  eing read, it is
3eca0 20 6e 6f 74 20 6c 6f 63 6b 65 64 0a 2a 2a 20 63   not locked.** c
3ecb0 6f 6e 74 69 6e 75 6f 75 73 6c 79 20 66 6f 72 20  ontinuously for 
3ecc0 74 68 65 20 65 6e 74 69 72 65 20 6f 70 65 72 61  the entire opera
3ecd0 74 69 6f 6e 2e 20 54 68 75 73 2c 20 74 68 65 20  tion. Thus, the 
3ece0 62 61 63 6b 75 70 20 6d 61 79 20 62 65 0a 2a 2a  backup may be.**
3ecf0 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 61 20   performed on a 
3ed00 6c 69 76 65 20 64 61 74 61 62 61 73 65 20 77 69  live database wi
3ed10 74 68 6f 75 74 20 70 72 65 76 65 6e 74 69 6e 67  thout preventing
3ed20 20 6f 74 68 65 72 20 75 73 65 72 73 20 66 72 6f   other users fro
3ed30 6d 0a 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20  m.** writing to 
3ed40 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72  the database for
3ed50 20 61 6e 20 65 78 74 65 6e 64 65 64 20 70 65 72   an extended per
3ed60 69 6f 64 20 6f 66 20 74 69 6d 65 2e 0a 2a 2a 20  iod of time..** 
3ed70 0a 2a 2a 20 54 6f 20 70 65 72 66 6f 72 6d 20 61  .** To perform a
3ed80 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
3ed90 6e 3a 20 0a 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a  n: .**   <ol>.**
3eda0 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69       <li><b>sqli
3edb0 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
3edc0 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20  )</b> is called 
3edd0 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69  once to initiali
3ede0 7a 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  ze the.**       
3edf0 20 20 62 61 63 6b 75 70 2c 20 0a 2a 2a 20 20 20    backup, .**   
3ee00 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33    <li><b>sqlite3
3ee10 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f  _backup_step()</
3ee20 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 65  b> is called one
3ee30 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74   or more times t
3ee40 6f 20 74 72 61 6e 73 66 65 72 20 0a 2a 2a 20 20  o transfer .**  
3ee50 20 20 20 20 20 20 20 74 68 65 20 64 61 74 61 20         the data 
3ee60 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20  between the two 
3ee70 64 61 74 61 62 61 73 65 73 2c 20 61 6e 64 20 66  databases, and f
3ee80 69 6e 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c  inally.**     <l
3ee90 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63  i><b>sqlite3_bac
3eea0 6b 75 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e  kup_finish()</b>
3eeb0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65   is called to re
3eec0 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72  lease all resour
3eed0 63 65 73 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ces .**         
3eee0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
3eef0 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
3ef00 74 69 6f 6e 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c  tion. .**   </ol
3ef10 3e 0a 2a 2a 20 54 68 65 72 65 20 73 68 6f 75 6c  >.** There shoul
3ef20 64 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65  d be exactly one
3ef30 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
3ef40 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
3ef50 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 73 75 63   for each.** suc
3ef60 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20  cessful call to 
3ef70 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
3ef80 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e  nit()..**.** <b>
3ef90 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
3efa0 6e 69 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20  nit()</b>.**.** 
3efb0 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 61 72  The first two ar
3efc0 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74  guments passed t
3efd0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  o [sqlite3_backu
3efe0 70 5f 69 6e 69 74 28 29 5d 20 61 72 65 20 74 68  p_init()] are th
3eff0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 68 61  e database.** ha
3f000 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20  ndle associated 
3f010 77 69 74 68 20 74 68 65 20 64 65 73 74 69 6e 61  with the destina
3f020 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 61 6e  tion database an
3f030 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  d the database n
3f040 61 6d 65 20 0a 2a 2a 20 75 73 65 64 20 74 6f 20  ame .** used to 
3f050 61 74 74 61 63 68 20 74 68 65 20 64 65 73 74 69  attach the desti
3f060 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
3f070 74 6f 20 74 68 65 20 68 61 6e 64 6c 65 2e 20 54  to the handle. T
3f080 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
3f090 0a 2a 2a 20 69 73 20 22 6d 61 69 6e 22 20 66 6f  .** is "main" fo
3f0a0 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
3f0b0 61 73 65 2c 20 22 74 65 6d 70 22 20 66 6f 72 20  ase, "temp" for 
3f0c0 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
3f0d0 74 61 62 61 73 65 2c 20 6f 72 0a 2a 2a 20 74 68  tabase, or.** th
3f0e0 65 20 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64  e name specified
3f0f0 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
3f100 5b 41 54 54 41 43 48 5d 20 73 74 61 74 65 6d 65  [ATTACH] stateme
3f110 6e 74 20 69 66 20 74 68 65 20 64 65 73 74 69 6e  nt if the destin
3f120 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 6e 20 61  ation is.** an a
3f130 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
3f140 2e 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20  . The third and 
3f150 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73  fourth arguments
3f160 20 70 61 73 73 65 64 20 74 6f 20 0a 2a 2a 20 73   passed to .** s
3f170 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e  qlite3_backup_in
3f180 69 74 28 29 20 69 64 65 6e 74 69 66 79 20 74 68  it() identify th
3f190 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
3f1a0 65 63 74 69 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 64  ection].** and d
3f1b0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 75 73 65  atabase name use
3f1c0 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74  d.** to access t
3f1d0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
3f1e0 73 65 2e 20 54 68 65 20 76 61 6c 75 65 73 20 70  se. The values p
3f1f0 61 73 73 65 64 20 66 6f 72 20 74 68 65 20 73 6f  assed for the so
3f200 75 72 63 65 20 61 6e 64 20 0a 2a 2a 20 64 65 73  urce and .** des
3f210 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61  tination [databa
3f220 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70  se connection] p
3f230 61 72 61 6d 65 74 65 72 73 20 6d 75 73 74 20 6e  arameters must n
3f240 6f 74 20 62 65 20 74 68 65 20 73 61 6d 65 2e 0a  ot be the same..
3f250 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
3f260 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20  r occurs within 
3f270 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
3f280 6e 69 74 28 29 2c 20 74 68 65 6e 20 4e 55 4c 4c  nit(), then NULL
3f290 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20   is returned.** 
3f2a0 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
3f2b0 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73  e and error mess
3f2c0 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  age written into
3f2d0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
3f2e0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 0a 2a 2a 20 70  onnection] .** p
3f2f0 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
3f300 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65  st argument. The
3f310 79 20 6d 61 79 20 62 65 20 72 65 74 72 69 65 76  y may be retriev
3f320 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  ed using the.** 
3f330 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65  [sqlite3_errcode
3f340 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 65 72  ()], [sqlite3_er
3f350 72 6d 73 67 28 29 5d 2c 20 61 6e 64 20 5b 73 71  rmsg()], and [sq
3f360 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29  lite3_errmsg16()
3f370 5d 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20  ] functions..** 
3f380 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 73 75  Otherwise, if su
3f390 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e  ccessful, a poin
3f3a0 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74  ter to an [sqlit
3f3b0 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63  e3_backup] objec
3f3c0 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  t is.** returned
3f3d0 2e 20 54 68 69 73 20 70 6f 69 6e 74 65 72 20 6d  . This pointer m
3f3e0 61 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20  ay be used with 
3f3f0 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
3f400 75 70 5f 73 74 65 70 28 29 20 61 6e 64 0a 2a 2a  up_step() and.**
3f410 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
3f420 66 69 6e 69 73 68 28 29 20 66 75 6e 63 74 69 6f  finish() functio
3f430 6e 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68  ns to perform th
3f440 65 20 73 70 65 63 69 66 69 65 64 20 62 61 63 6b  e specified back
3f450 75 70 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e  up .** operation
3f460 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74  ..**.** <b>sqlit
3f470 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
3f480 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74  </b>.**.** Funct
3f490 69 6f 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63  ion [sqlite3_bac
3f4a0 6b 75 70 5f 73 74 65 70 28 29 5d 20 69 73 20 75  kup_step()] is u
3f4b0 73 65 64 20 74 6f 20 63 6f 70 79 20 75 70 20 74  sed to copy up t
3f4c0 6f 20 6e 50 61 67 65 20 70 61 67 65 73 20 62 65  o nPage pages be
3f4d0 74 77 65 65 6e 20 0a 2a 2a 20 74 68 65 20 73 6f  tween .** the so
3f4e0 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61  urce and destina
3f4f0 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 2c 20  tion databases, 
3f500 77 68 65 72 65 20 6e 50 61 67 65 20 69 73 20 74  where nPage is t
3f510 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
3f520 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  .** second param
3f530 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 73  eter passed to s
3f540 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
3f550 65 70 28 29 2e 20 49 66 20 6e 50 61 67 65 20 69  ep(). If nPage i
3f560 73 20 61 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20  s a negative.** 
3f570 76 61 6c 75 65 2c 20 61 6c 6c 20 72 65 6d 61 69  value, all remai
3f580 6e 69 6e 67 20 73 6f 75 72 63 65 20 70 61 67 65  ning source page
3f590 73 20 61 72 65 20 63 6f 70 69 65 64 2e 20 49 66  s are copied. If
3f5a0 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 61   the required pa
3f5b0 67 65 73 20 61 72 65 20 0a 2a 2a 20 73 75 63 63  ges are .** succ
3f5c0 65 73 66 75 6c 6c 79 20 63 6f 70 69 65 64 2c 20  esfully copied, 
3f5d0 62 75 74 20 74 68 65 72 65 20 61 72 65 20 73 74  but there are st
3f5e0 69 6c 6c 20 6d 6f 72 65 20 70 61 67 65 73 20 74  ill more pages t
3f5f0 6f 20 63 6f 70 79 20 62 65 66 6f 72 65 20 74 68  o copy before th
3f600 65 20 0a 2a 2a 20 62 61 63 6b 75 70 20 69 73 20  e .** backup is 
3f610 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20 72 65 74  complete, it ret
3f620 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  urns [SQLITE_OK]
3f630 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63  . If no error oc
3f640 63 75 72 65 64 20 61 6e 64 20 74 68 65 72 65 20  cured and there 
3f650 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20  .** are no more 
3f660 70 61 67 65 73 20 74 6f 20 63 6f 70 79 2c 20 74  pages to copy, t
3f670 68 65 6e 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45  hen [SQLITE_DONE
3f680 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  ] is returned. I
3f690 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f  f an error .** o
3f6a0 63 63 75 72 73 2c 20 74 68 65 6e 20 61 6e 20 53  ccurs, then an S
3f6b0 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
3f6c0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 41 73   is returned. As
3f6d0 20 77 65 6c 6c 20 61 73 20 5b 53 51 4c 49 54 45   well as [SQLITE
3f6e0 5f 4f 4b 5d 20 61 6e 64 0a 2a 2a 20 5b 53 51 4c  _OK] and.** [SQL
3f6f0 49 54 45 5f 44 4f 4e 45 5d 2c 20 61 20 63 61 6c  ITE_DONE], a cal
3f700 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
3f710 6b 75 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72  kup_step() may r
3f720 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45  eturn [SQLITE_RE
3f730 41 44 4f 4e 4c 59 5d 2c 0a 2a 2a 20 5b 53 51 4c  ADONLY],.** [SQL
3f740 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c  ITE_NOMEM], [SQL
3f750 49 54 45 5f 42 55 53 59 5d 2c 20 5b 53 51 4c 49  ITE_BUSY], [SQLI
3f760 54 45 5f 4c 4f 43 4b 45 44 5d 2c 20 6f 72 20 61  TE_LOCKED], or a
3f770 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45  n.** [SQLITE_IOE
3f780 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49  RR_ACCESS | SQLI
3f790 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 78  TE_IOERR_XXX] ex
3f7a0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
3f7b0 65 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c  e..**.** As well
3f7c0 20 61 73 20 74 68 65 20 63 61 73 65 20 77 68 65   as the case whe
3f7d0 72 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  re the destinati
3f7e0 6f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  on database file
3f7f0 20 77 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 0a   was opened for.
3f800 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 61 63 63  ** read-only acc
3f810 65 73 73 2c 20 73 71 6c 69 74 65 33 5f 62 61 63  ess, sqlite3_bac
3f820 6b 75 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72  kup_step() may r
3f830 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45  eturn [SQLITE_RE
3f840 41 44 4f 4e 4c 59 5d 20 69 66 0a 2a 2a 20 74 68  ADONLY] if.** th
3f850 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73  e destination is
3f860 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
3f870 74 61 62 61 73 65 20 77 69 74 68 20 61 20 64 69  tabase with a di
3f880 66 66 65 72 65 6e 74 20 70 61 67 65 20 73 69 7a  fferent page siz
3f890 65 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 6f  e.** from the so
3f8a0 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 0a 2a  urce database..*
3f8b0 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f  *.** If sqlite3_
3f8c0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61  backup_step() ca
3f8d0 6e 6e 6f 74 20 6f 62 74 61 69 6e 20 61 20 72 65  nnot obtain a re
3f8e0 71 75 69 72 65 64 20 66 69 6c 65 2d 73 79 73 74  quired file-syst
3f8f0 65 6d 20 6c 6f 63 6b 2c 20 74 68 65 6e 0a 2a 2a  em lock, then.**
3f900 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75   the [sqlite3_bu
3f910 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73  sy_handler | bus
3f920 79 2d 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69  y-handler functi
3f930 6f 6e 5d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65  on].** is invoke
3f940 64 20 28 69 66 20 6f 6e 65 20 69 73 20 73 70 65  d (if one is spe
3f950 63 69 66 69 65 64 29 2e 20 49 66 20 74 68 65 20  cified). If the 
3f960 0a 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72  .** busy-handler
3f970 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
3f980 6f 20 62 65 66 6f 72 65 20 74 68 65 20 6c 6f 63  o before the loc
3f990 6b 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  k is available, 
3f9a0 74 68 65 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  then .** [SQLITE
3f9b0 5f 42 55 53 59 5d 20 69 73 20 72 65 74 75 72 6e  _BUSY] is return
3f9c0 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
3f9d0 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
3f9e0 68 65 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71  he call to.** sq
3f9f0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
3fa00 70 28 29 20 63 61 6e 20 62 65 20 72 65 74 72 69  p() can be retri
3fa10 65 64 20 6c 61 74 65 72 2e 20 49 66 20 74 68 65  ed later. If the
3fa20 20 73 6f 75 72 63 65 0a 2a 2a 20 5b 64 61 74 61   source.** [data
3fa30 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
3fa40 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 75 73 65  .** is being use
3fa50 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68  d to write to th
3fa60 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
3fa70 65 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 62  e when sqlite3_b
3fa80 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20  ackup_step().** 
3fa90 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  is called, then 
3faa0 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20  [SQLITE_LOCKED] 
3fab0 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65  is returned imme
3fac0 64 69 61 74 65 6c 79 2e 20 41 67 61 69 6e 2c 20  diately. Again, 
3fad0 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20  in this.** case 
3fae0 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  the call to sqli
3faf0 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
3fb00 29 20 63 61 6e 20 62 65 20 72 65 74 72 69 65 64  ) can be retried
3fb10 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 0a 2a 2a   later on. If.**
3fb20 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41   [SQLITE_IOERR_A
3fb30 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49  CCESS | SQLITE_I
3fb40 4f 45 52 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49  OERR_XXX], [SQLI
3fb50 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a  TE_NOMEM], or.**
3fb60 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c   [SQLITE_READONL
3fb70 59 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  Y] is returned, 
3fb80 74 68 65 6e 20 0a 2a 2a 20 74 68 65 72 65 20 69  then .** there i
3fb90 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65  s no point in re
3fba0 74 72 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20  trying the call 
3fbb0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
3fbc0 70 5f 73 74 65 70 28 29 2e 20 54 68 65 73 65 20  p_step(). These 
3fbd0 0a 2a 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63  .** errors are c
3fbe0 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e  onsidered fatal.
3fbf0 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
3fc00 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  he application m
3fc10 75 73 74 20 61 63 63 65 70 74 20 0a 2a 2a 20 74  ust accept .** t
3fc20 68 61 74 20 74 68 65 20 62 61 63 6b 75 70 20 6f  hat the backup o
3fc30 70 65 72 61 74 69 6f 6e 20 68 61 73 20 66 61 69  peration has fai
3fc40 6c 65 64 20 61 6e 64 20 70 61 73 73 20 74 68 65  led and pass the
3fc50 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
3fc60 6e 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 74 6f 20  n handle .** to 
3fc70 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
3fc80 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 72  up_finish() to r
3fc90 65 6c 65 61 73 65 20 61 73 73 6f 63 69 61 74 65  elease associate
3fca0 64 20 72 65 73 6f 75 72 63 65 73 2e 0a 2a 2a 0a  d resources..**.
3fcb0 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ** Following the
3fcc0 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73   first call to s
3fcd0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
3fce0 65 70 28 29 2c 20 61 6e 20 65 78 63 6c 75 73 69  ep(), an exclusi
3fcf0 76 65 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62  ve lock is.** ob
3fd00 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 65  tained on the de
3fd10 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c 65 2e 20  stination file. 
3fd20 49 74 20 69 73 20 6e 6f 74 20 72 65 6c 65 61 73  It is not releas
3fd30 65 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20  ed until either 
3fd40 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
3fd50 75 70 5f 66 69 6e 69 73 68 28 29 20 69 73 20 63  up_finish() is c
3fd60 61 6c 6c 65 64 20 6f 72 20 74 68 65 20 62 61 63  alled or the bac
3fd70 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  kup operation is
3fd80 20 63 6f 6d 70 6c 65 74 65 20 0a 2a 2a 20 61 6e   complete .** an
3fd90 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  d sqlite3_backup
3fda0 5f 73 74 65 70 28 29 20 72 65 74 75 72 6e 73 20  _step() returns 
3fdb0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 41  [SQLITE_DONE]. A
3fdc0 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 65 61 63  dditionally, eac
3fdd0 68 20 74 69 6d 65 20 0a 2a 2a 20 61 20 63 61 6c  h time .** a cal
3fde0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
3fdf0 6b 75 70 5f 73 74 65 70 28 29 20 69 73 20 6d 61  kup_step() is ma
3fe00 64 65 20 61 20 5b 73 68 61 72 65 64 20 6c 6f 63  de a [shared loc
3fe10 6b 5d 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f  k] is obtained o
3fe20 6e 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20  n.** the source 
3fe30 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54  database file. T
3fe40 68 69 73 20 6c 6f 63 6b 20 69 73 20 72 65 6c 65  his lock is rele
3fe50 61 73 65 64 20 62 65 66 6f 72 65 20 74 68 65 0a  ased before the.
3fe60 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
3fe70 70 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 72 65  p_step() call re
3fe80 74 75 72 6e 73 2e 20 42 65 63 61 75 73 65 20 74  turns. Because t
3fe90 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
3fea0 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 63  se is not.** loc
3feb0 6b 65 64 20 62 65 74 77 65 65 6e 20 63 61 6c 6c  ked between call
3fec0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  s to sqlite3_bac
3fed0 6b 75 70 5f 73 74 65 70 28 29 2c 20 69 74 20 6d  kup_step(), it m
3fee0 61 79 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6d  ay be modified m
3fef0 69 64 2d 77 61 79 0a 2a 2a 20 74 68 72 6f 75 67  id-way.** throug
3ff00 68 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f  h the backup pro
3ff10 63 65 64 75 72 65 2e 20 49 66 20 74 68 65 20 73  cedure. If the s
3ff20 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69  ource database i
3ff30 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e  s modified by an
3ff40 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 70 72 6f  .** external pro
3ff50 63 65 73 73 20 6f 72 20 76 69 61 20 61 20 64 61  cess or via a da
3ff60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3ff70 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65  n other than the
3ff80 20 6f 6e 65 20 62 65 69 6e 67 0a 2a 2a 20 75 73   one being.** us
3ff90 65 64 20 62 79 20 74 68 65 20 62 61 63 6b 75 70  ed by the backup
3ffa0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e   operation, then
3ffb0 20 74 68 65 20 62 61 63 6b 75 70 20 77 69 6c 6c   the backup will
3ffc0 20 62 65 20 74 72 61 6e 73 70 61 72 65 6e 74 6c   be transparentl
3ffd0 79 0a 2a 2a 20 72 65 73 74 61 72 74 65 64 20 62  y.** restarted b
3ffe0 79 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  y the next call 
3fff0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
40000 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74 68 65  p_step(). If the
40010 20 73 6f 75 72 63 65 20 0a 2a 2a 20 64 61 74 61   source .** data
40020 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64  base is modified
40030 20 62 79 20 74 68 65 20 75 73 69 6e 67 20 74 68   by the using th
40040 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
40050 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73  connection as is
40060 20 75 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20   used.** by the 
40070 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
40080 2c 20 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75  , then the backu
40090 70 20 64 61 74 61 62 61 73 65 20 69 73 20 74 72  p database is tr
400a0 61 6e 73 70 61 72 65 6e 74 6c 79 20 0a 2a 2a 20  ansparently .** 
400b0 75 70 64 61 74 65 64 20 61 74 20 74 68 65 20 73  updated at the s
400c0 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  ame time..**.** 
400d0 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  <b>sqlite3_backu
400e0 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 0a 2a  p_finish()</b>.*
400f0 2a 0a 2a 2a 20 4f 6e 63 65 20 73 71 6c 69 74 65  *.** Once sqlite
40100 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
40110 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51  has returned [SQ
40120 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20 77  LITE_DONE], or w
40130 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 70 70 6c  hen the .** appl
40140 69 63 61 74 69 6f 6e 20 77 69 73 68 65 73 20 74  ication wishes t
40150 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 62 61  o abandon the ba
40160 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20  ckup operation, 
40170 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63  the [sqlite3_bac
40180 6b 75 70 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 73  kup].** object s
40190 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20  hould be passed 
401a0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
401b0 70 5f 66 69 6e 69 73 68 28 29 2e 20 54 68 69 73  p_finish(). This
401c0 20 72 65 6c 65 61 73 65 73 20 61 6c 6c 0a 2a 2a   releases all.**
401d0 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63   resources assoc
401e0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62  iated with the b
401f0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e  ackup operation.
40200 20 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   If sqlite3_back
40210 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20 68 61 73  up_step().** has
40220 20 6e 6f 74 20 79 65 74 20 72 65 74 75 72 6e 65   not yet returne
40230 64 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c  d [SQLITE_DONE],
40240 20 74 68 65 6e 20 61 6e 79 20 61 63 74 69 76 65   then any active
40250 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
40260 6f 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 65 73  on on the.** des
40270 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
40280 65 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  e is rolled back
40290 2e 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  . The [sqlite3_b
402a0 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73  ackup] object is
402b0 20 69 6e 76 61 6c 69 64 0a 2a 2a 20 61 6e 64 20   invalid.** and 
402c0 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20  may not be used 
402d0 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c  following a call
402e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
402f0 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a  up_finish()..**.
40300 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  ** The value ret
40310 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
40320 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 20 69  _backup_finish i
40330 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66  s [SQLITE_OK] if
40340 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63   no error.** occ
40350 75 72 72 65 64 2c 20 72 65 67 61 72 64 6c 65 73  urred, regardles
40360 73 20 6f 72 20 77 68 65 74 68 65 72 20 6f 72 20  s or whether or 
40370 6e 6f 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  not sqlite3_back
40380 75 70 5f 73 74 65 70 28 29 20 77 61 73 20 63 61  up_step() was ca
40390 6c 6c 65 64 0a 2a 2a 20 61 20 73 75 66 66 69 63  lled.** a suffic
403a0 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 74  ient number of t
403b0 69 6d 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 65  imes to complete
403c0 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72   the backup oper
403d0 61 74 69 6f 6e 2e 20 4f 72 2c 20 69 66 0a 2a 2a  ation. Or, if.**
403e0 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72   an out-of-memor
403f0 79 20 63 6f 6e 64 69 74 69 6f 6e 20 6f 72 20 49  y condition or I
40400 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20  O error occured 
40410 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f  during a call to
40420 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
40430 75 70 5f 73 74 65 70 28 29 20 74 68 65 6e 20 5b  up_step() then [
40440 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 6f 72  SQLITE_NOMEM] or
40450 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49   an.** [SQLITE_I
40460 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51  OERR_ACCESS | SQ
40470 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20  LITE_IOERR_XXX] 
40480 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73  error code.** is
40490 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68   returned. In th
404a0 69 73 20 63 61 73 65 20 74 68 65 20 65 72 72 6f  is case the erro
404b0 72 20 63 6f 64 65 20 61 6e 64 20 61 6e 20 65 72  r code and an er
404c0 72 6f 72 20 6d 65 73 73 61 67 65 20 61 72 65 0a  ror message are.
404d0 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  ** written to th
404e0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64  e destination [d
404f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
40500 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 74  on]..**.** A ret
40510 75 72 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42  urn of [SQLITE_B
40520 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  USY] or [SQLITE_
40530 4c 4f 43 4b 45 44 5d 20 66 72 6f 6d 20 73 71 6c  LOCKED] from sql
40540 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
40550 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70  () is.** not a p
40560 65 72 6d 61 6e 65 6e 74 20 65 72 72 6f 72 20 61  ermanent error a
40570 6e 64 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65  nd does not affe
40580 63 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ct the return va
40590 6c 75 65 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65  lue of.** sqlite
405a0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
405b0 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69  )..**.** <b>sqli
405c0 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69  te3_backup_remai
405d0 6e 69 6e 67 28 29 2c 20 73 71 6c 69 74 65 33 5f  ning(), sqlite3_
405e0 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
405f0 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 45 61 63  ()</b>.**.** Eac
40600 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  h call to sqlite
40610 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
40620 73 65 74 73 20 74 77 6f 20 76 61 6c 75 65 73 20  sets two values 
40630 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c  stored internall
40640 79 0a 2a 2a 20 62 79 20 61 6e 20 5b 73 71 6c 69  y.** by an [sqli
40650 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65  te3_backup] obje
40660 63 74 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  ct. The number o
40670 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f  f pages still to
40680 20 62 65 20 62 61 63 6b 65 64 0a 2a 2a 20 75 70   be backed.** up
40690 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 71  , which may be q
406a0 75 65 72 69 65 64 20 62 79 20 73 71 6c 69 74 65  ueried by sqlite
406b0 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69  3_backup_remaini
406c0 6e 67 28 29 2c 20 61 6e 64 20 74 68 65 20 74 6f  ng(), and the to
406d0 74 61 6c 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  tal.** number of
406e0 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f   pages in the so
406f0 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69  urce database fi
40700 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65  le, which may be
40710 20 71 75 65 72 69 65 64 20 62 79 0a 2a 2a 20 73   queried by.** s
40720 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61  qlite3_backup_pa
40730 67 65 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a  gecount()..**.**
40740 20 54 68 65 20 76 61 6c 75 65 73 20 72 65 74 75   The values retu
40750 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 66 75  rned by these fu
40760 6e 63 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79  nctions are only
40770 20 75 70 64 61 74 65 64 20 62 79 0a 2a 2a 20 73   updated by.** s
40780 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
40790 65 70 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75  ep(). If the sou
407a0 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
407b0 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e 67 20  modified during 
407c0 61 20 62 61 63 6b 75 70 0a 2a 2a 20 6f 70 65 72  a backup.** oper
407d0 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20  ation, then the 
407e0 76 61 6c 75 65 73 20 61 72 65 20 6e 6f 74 20 75  values are not u
407f0 70 64 61 74 65 64 20 74 6f 20 61 63 63 6f 75 6e  pdated to accoun
40800 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72 61 0a  t for any extra.
40810 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65  ** pages that ne
40820 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64  ed to be updated
40830 20 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20   or the size of 
40840 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
40850 61 73 65 20 66 69 6c 65 0a 2a 2a 20 63 68 61 6e  ase file.** chan
40860 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43  ging..**.** <b>C
40870 6f 6e 63 75 72 72 65 6e 74 20 55 73 61 67 65 20  oncurrent Usage 
40880 6f 66 20 44 61 74 61 62 61 73 65 20 48 61 6e 64  of Database Hand
40890 6c 65 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68  les</b>.**.** Th
408a0 65 20 73 6f 75 72 63 65 20 5b 64 61 74 61 62 61  e source [databa
408b0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6d  se connection] m
408c0 61 79 20 62 65 20 75 73 65 64 20 62 79 20 74 68  ay be used by th
408d0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 6f  e application fo
408e0 72 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f  r other.** purpo
408f0 73 65 73 20 77 68 69 6c 65 20 61 20 62 61 63 6b  ses while a back
40900 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  up operation is 
40910 75 6e 64 65 72 77 61 79 20 6f 72 20 62 65 69 6e  underway or bein
40920 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a  g initialized..*
40930 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63  * If SQLite is c
40940 6f 6d 70 69 6c 65 64 20 61 6e 64 20 63 6f 6e 66  ompiled and conf
40950 69 67 75 72 65 64 20 74 6f 20 73 75 70 70 6f 72  igured to suppor
40960 74 20 74 68 72 65 61 64 73 61 66 65 20 64 61 74  t threadsafe dat
40970 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
40980 69 6f 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73  ions, then the s
40990 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 63  ource database c
409a0 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 62 65  onnection may be
409b0 20 75 73 65 64 20 63 6f 6e 63 75 72 72 65 6e 74   used concurrent
409c0 6c 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69  ly.** from withi
409d0 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e  n other threads.
409e0 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  .**.** However, 
409f0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
40a00 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74  must guarantee t
40a10 68 61 74 20 74 68 65 20 64 65 73 74 69 6e 61 74  hat the destinat
40a20 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ion database.** 
40a30 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
40a40 65 20 69 73 20 6e 6f 74 20 70 61 73 73 65 64 20  e is not passed 
40a50 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 41 50 49  to any other API
40a60 20 28 62 79 20 61 6e 79 20 74 68 72 65 61 64 29   (by any thread)
40a70 20 61 66 74 65 72 20 0a 2a 2a 20 73 71 6c 69 74   after .** sqlit
40a80 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
40a90 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 62   is called and b
40aa0 65 66 6f 72 65 20 74 68 65 20 63 6f 72 72 65 73  efore the corres
40ab0 70 6f 6e 64 69 6e 67 20 63 61 6c 6c 20 74 6f 0a  ponding call to.
40ac0 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
40ad0 70 5f 66 69 6e 69 73 68 28 29 2e 20 55 6e 66 6f  p_finish(). Unfo
40ae0 72 74 75 6e 61 74 65 6c 79 20 53 51 4c 69 74 65  rtunately SQLite
40af0 20 64 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e   does not curren
40b00 74 6c 79 20 63 68 65 63 6b 0a 2a 2a 20 66 6f 72  tly check.** for
40b10 20 74 68 69 73 2c 20 69 66 20 74 68 65 20 61 70   this, if the ap
40b20 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 75  plication does u
40b30 73 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  se the destinati
40b40 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  on [database con
40b50 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 66 6f 72 20  nection].** for 
40b60 73 6f 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f  some other purpo
40b70 73 65 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b  se during a back
40b80 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68  up operation, th
40b90 69 6e 67 73 20 6d 61 79 20 61 70 70 65 61 72 20  ings may appear 
40ba0 74 6f 0a 2a 2a 20 77 6f 72 6b 20 63 6f 72 72 65  to.** work corre
40bb0 63 74 6c 79 20 62 75 74 20 69 6e 20 66 61 63 74  ctly but in fact
40bc0 20 62 65 20 73 75 62 74 6c 79 20 6d 61 6c 66 75   be subtly malfu
40bd0 6e 63 74 69 6f 6e 69 6e 67 2e 20 20 55 73 65 20  nctioning.  Use 
40be0 6f 66 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e  of the.** destin
40bf0 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 63  ation database c
40c00 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20  onnection while 
40c10 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70  a backup is in p
40c20 72 6f 67 72 65 73 73 20 6d 69 67 68 74 0a 2a 2a  rogress might.**
40c30 20 61 6c 73 6f 20 63 61 75 73 65 20 61 20 6d 75   also cause a mu
40c40 74 65 78 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a  tex deadlock..**
40c50 0a 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72 65 2c  .** Furthermore,
40c60 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 5b   if running in [
40c70 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64  shared cache mod
40c80 65 5d 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74  e], the applicat
40c90 69 6f 6e 20 6d 75 73 74 0a 2a 2a 20 67 75 61 72  ion must.** guar
40ca0 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 73  antee that the s
40cb0 68 61 72 65 64 20 63 61 63 68 65 20 75 73 65 64  hared cache used
40cc0 20 62 79 20 74 68 65 20 64 65 73 74 69 6e 61 74   by the destinat
40cd0 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ion database.** 
40ce0 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20  is not accessed 
40cf0 77 68 69 6c 65 20 74 68 65 20 62 61 63 6b 75 70  while the backup
40d00 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 49 6e 20   is running. In 
40d10 70 72 61 63 74 69 63 65 20 74 68 69 73 20 6d 65  practice this me
40d20 61 6e 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ans.** that the 
40d30 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
40d40 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
40d50 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
40d60 66 69 6c 65 20 62 65 69 6e 67 20 0a 2a 2a 20 62  file being .** b
40d70 61 63 6b 65 64 20 75 70 20 74 6f 20 69 73 20 6e  acked up to is n
40d80 6f 74 20 61 63 63 65 73 73 65 64 20 62 79 20 61  ot accessed by a
40d90 6e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69  ny connection wi
40da0 74 68 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73  thin the process
40db0 2c 0a 2a 2a 20 6e 6f 74 20 6a 75 73 74 20 74 68  ,.** not just th
40dc0 65 20 73 70 65 63 69 66 69 63 20 63 6f 6e 6e 65  e specific conne
40dd0 63 74 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70  ction that was p
40de0 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
40df0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a  _backup_init()..
40e00 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  **.** The [sqlit
40e10 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63  e3_backup] objec
40e20 74 20 69 74 73 65 6c 66 20 69 73 20 70 61 72 74  t itself is part
40e30 69 61 6c 6c 79 20 74 68 72 65 61 64 73 61 66 65  ially threadsafe
40e40 2e 20 4d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74  . Multiple .** t
40e50 68 72 65 61 64 73 20 6d 61 79 20 73 61 66 65 6c  hreads may safel
40e60 79 20 6d 61 6b 65 20 6d 75 6c 74 69 70 6c 65 20  y make multiple 
40e70 63 6f 6e 63 75 72 72 65 6e 74 20 63 61 6c 6c 73  concurrent calls
40e80 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
40e90 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2a 20 48 6f  up_step()..** Ho
40ea0 77 65 76 65 72 2c 20 74 68 65 20 73 71 6c 69 74  wever, the sqlit
40eb0 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  e3_backup_remain
40ec0 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ing() and sqlite
40ed0 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75  3_backup_pagecou
40ee0 6e 74 28 29 0a 2a 2a 20 41 50 49 73 20 61 72 65  nt().** APIs are
40ef0 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 73 70   not strictly sp
40f00 65 61 6b 69 6e 67 20 74 68 72 65 61 64 73 61 66  eaking threadsaf
40f10 65 2e 20 49 66 20 74 68 65 79 20 61 72 65 20 69  e. If they are i
40f20 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 0a 2a 2a  nvoked at the.**
40f30 20 73 61 6d 65 20 74 69 6d 65 20 61 73 20 61 6e   same time as an
40f40 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
40f50 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33  invoking sqlite3
40f60 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69  _backup_step() i
40f70 74 20 69 73 0a 2a 2a 20 70 6f 73 73 69 62 6c 65  t is.** possible
40f80 20 74 68 61 74 20 74 68 65 79 20 72 65 74 75 72   that they retur
40f90 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 73  n invalid values
40fa0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
40fb0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
40fc0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
40fd0 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit(.  sqlite3 *
40fe0 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 20  pDest,          
40ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
41000 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   Destination dat
41010 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
41020 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
41030 65 73 74 4e 61 6d 65 2c 20 20 20 20 20 20 20 20  estName,        
41040 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74           /* Dest
41050 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
41060 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71 6c 69 74   name */.  sqlit
41070 65 33 20 2a 70 53 6f 75 72 63 65 2c 20 20 20 20  e3 *pSource,    
41080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
41090 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61    /* Source data
410a0 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
410b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 6f   const char *zSo
410c0 75 72 63 65 4e 61 6d 65 20 20 20 20 20 20 20 20  urceName        
410d0 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63          /* Sourc
410e0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
410f0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  */.);.SQLITE_API
41100 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63   int sqlite3_bac
41110 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33  kup_step(sqlite3
41120 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20  _backup *p, int 
41130 6e 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f 41  nPage);.SQLITE_A
41140 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
41150 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c  ackup_finish(sql
41160 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b  ite3_backup *p);
41170 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
41180 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72  sqlite3_backup_r
41190 65 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33  emaining(sqlite3
411a0 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c  _backup *p);.SQL
411b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
411c0 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63  te3_backup_pagec
411d0 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63  ount(sqlite3_bac
411e0 6b 75 70 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20  kup *p);../*.** 
411f0 43 41 50 49 33 52 45 46 3a 20 55 6e 6c 6f 63 6b  CAPI3REF: Unlock
41200 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 0a 2a 2a   Notification.**
41210 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
41220 0a 2a 2a 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67  .** When running
41230 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63 68 65   in shared-cache
41240 20 6d 6f 64 65 2c 20 61 20 64 61 74 61 62 61 73   mode, a databas
41250 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20  e operation may 
41260 66 61 69 6c 20 77 69 74 68 0a 2a 2a 20 61 6e 20  fail with.** an 
41270 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20  [SQLITE_LOCKED] 
41280 65 72 72 6f 72 20 69 66 20 74 68 65 20 72 65 71  error if the req
41290 75 69 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74  uired locks on t
412a0 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  he shared-cache 
412b0 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  or.** individual
412c0 20 74 61 62 6c 65 73 20 77 69 74 68 69 6e 20 74   tables within t
412d0 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  he shared-cache 
412e0 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e  cannot be obtain
412f0 65 64 2e 20 53 65 65 0a 2a 2a 20 5b 53 51 4c 69  ed. See.** [SQLi
41300 74 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20  te Shared-Cache 
41310 4d 6f 64 65 5d 20 66 6f 72 20 61 20 64 65 73 63  Mode] for a desc
41320 72 69 70 74 69 6f 6e 20 6f 66 20 73 68 61 72 65  ription of share
41330 64 2d 63 61 63 68 65 20 6c 6f 63 6b 69 6e 67 2e  d-cache locking.
41340 20 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61   .** This API ma
41350 79 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 67  y be used to reg
41360 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b  ister a callback
41370 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c   that SQLite wil
41380 6c 20 69 6e 76 6f 6b 65 20 0a 2a 2a 20 77 68 65  l invoke .** whe
41390 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
413a0 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69   currently holdi
413b0 6e 67 20 74 68 65 20 72 65 71 75 69 72 65 64 20  ng the required 
413c0 6c 6f 63 6b 20 72 65 6c 69 6e 71 75 69 73 68 65  lock relinquishe
413d0 73 20 69 74 2e 0a 2a 2a 20 54 68 69 73 20 41 50  s it..** This AP
413e0 49 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  I is only availa
413f0 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61  ble if the libra
41400 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20  ry was compiled 
41410 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c  with the.** [SQL
41420 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43  ITE_ENABLE_UNLOC
41430 4b 5f 4e 4f 54 49 46 59 5d 20 43 2d 70 72 65 70  K_NOTIFY] C-prep
41440 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20  rocessor symbol 
41450 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  defined..**.** S
41460 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20  ee Also: [Using 
41470 74 68 65 20 53 51 4c 69 74 65 20 55 6e 6c 6f 63  the SQLite Unloc
41480 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 46  k Notification F
41490 65 61 74 75 72 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53  eature]..**.** S
414a0 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b  hared-cache lock
414b0 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20 77  s are released w
414c0 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 63  hen a database c
414d0 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 63 6c 75  onnection conclu
414e0 64 65 73 0a 2a 2a 20 69 74 73 20 63 75 72 72 65  des.** its curre
414f0 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  nt transaction, 
41500 65 69 74 68 65 72 20 62 79 20 63 6f 6d 6d 69 74  either by commit
41510 74 69 6e 67 20 69 74 20 6f 72 20 72 6f 6c 6c 69  ting it or rolli
41520 6e 67 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a  ng it back. .**.
41530 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 6e 65 63  ** When a connec
41540 74 69 6f 6e 20 28 6b 6e 6f 77 6e 20 61 73 20 74  tion (known as t
41550 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  he blocked conne
41560 63 74 69 6f 6e 29 20 66 61 69 6c 73 20 74 6f 20  ction) fails to 
41570 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 73 68 61 72  obtain a.** shar
41580 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b 20 61 6e  ed-cache lock an
41590 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  d SQLITE_LOCKED 
415a0 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
415b0 68 65 20 63 61 6c 6c 65 72 2c 20 74 68 65 0a 2a  he caller, the.*
415c0 2a 20 69 64 65 6e 74 69 74 79 20 6f 66 20 74 68  * identity of th
415d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
415e0 63 74 69 6f 6e 20 28 74 68 65 20 62 6c 6f 63 6b  ction (the block
415f0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20  ing connection) 
41600 74 68 61 74 0a 2a 2a 20 68 61 73 20 6c 6f 63 6b  that.** has lock
41610 65 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20  ed the required 
41620 72 65 73 6f 75 72 63 65 20 69 73 20 73 74 6f 72  resource is stor
41630 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 41  ed internally. A
41640 66 74 65 72 20 61 6e 20 0a 2a 2a 20 61 70 70 6c  fter an .** appl
41650 69 63 61 74 69 6f 6e 20 72 65 63 65 69 76 65 73  ication receives
41660 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   an SQLITE_LOCKE
41670 44 20 65 72 72 6f 72 2c 20 69 74 20 6d 61 79 20  D error, it may 
41680 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69  call the.** sqli
41690 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
416a0 79 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20  y() method with 
416b0 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  the blocked conn
416c0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 61 73  ection handle as
416d0 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61   .** the first a
416e0 72 67 75 6d 65 6e 74 20 74 6f 20 72 65 67 69 73  rgument to regis
416f0 74 65 72 20 66 6f 72 20 61 20 63 61 6c 6c 62 61  ter for a callba
41700 63 6b 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ck that will be 
41710 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20  invoked.** when 
41720 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e  the blocking con
41730 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74  nections current
41740 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
41750 63 6f 6e 63 6c 75 64 65 64 2e 20 54 68 65 0a 2a  concluded. The.*
41760 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  * callback is in
41770 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69  voked from withi
41780 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  n the [sqlite3_s
41790 74 65 70 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  tep] or [sqlite3
417a0 5f 63 6c 6f 73 65 5d 0a 2a 2a 20 63 61 6c 6c 20  _close].** call 
417b0 74 68 61 74 20 63 6f 6e 63 6c 75 64 65 73 20 74  that concludes t
417c0 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  he blocking conn
417d0 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74  ections transact
417e0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71  ion..**.** If sq
417f0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
41800 69 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ify() is called 
41810 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61  in a multi-threa
41820 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c  ded application,
41830 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 63  .** there is a c
41840 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 62  hance that the b
41850 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69  locking connecti
41860 6f 6e 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72  on will have alr
41870 65 61 64 79 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65  eady.** conclude
41880 64 20 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f  d its transactio
41890 6e 20 62 79 20 74 68 65 20 74 69 6d 65 20 73 71  n by the time sq
418a0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
418b0 69 66 79 28 29 20 69 73 20 69 6e 76 6f 6b 65 64  ify() is invoked
418c0 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 68 61 70  ..** If this hap
418d0 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73  pens, then the s
418e0 70 65 63 69 66 69 65 64 20 63 61 6c 6c 62 61 63  pecified callbac
418f0 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d  k is invoked imm
41900 65 64 69 61 74 65 6c 79 2c 0a 2a 2a 20 66 72 6f  ediately,.** fro
41910 6d 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c  m within the cal
41920 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e 6c  l to sqlite3_unl
41930 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 2e 0a 2a 2a  ock_notify()..**
41940 0a 2a 2a 20 49 66 20 74 68 65 20 62 6c 6f 63 6b  .** If the block
41950 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  ed connection is
41960 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f   attempting to o
41970 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f  btain a write-lo
41980 63 6b 20 6f 6e 20 61 0a 2a 2a 20 73 68 61 72 65  ck on a.** share
41990 64 2d 63 61 63 68 65 20 74 61 62 6c 65 2c 20 61  d-cache table, a
419a0 6e 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  nd more than one
419b0 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
419c0 6e 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64  n currently hold
419d0 73 0a 2a 2a 20 61 20 72 65 61 64 2d 6c 6f 63 6b  s.** a read-lock
419e0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62   on the same tab
419f0 6c 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20  le, then SQLite 
41a00 61 72 62 69 74 72 61 72 69 6c 79 20 73 65 6c 65  arbitrarily sele
41a10 63 74 73 20 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74  cts one of .** t
41a20 68 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  he other connect
41a30 69 6f 6e 73 20 74 6f 20 75 73 65 20 61 73 20 74  ions to use as t
41a40 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  he blocking conn
41a50 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ection..**.** Th
41a60 65 72 65 20 6d 61 79 20 62 65 20 61 74 20 6d 6f  ere may be at mo
41a70 73 74 20 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  st one unlock-no
41a80 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 72 65  tify callback re
41a90 67 69 73 74 65 72 65 64 20 62 79 20 61 20 0a 2a  gistered by a .*
41aa0 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  * blocked connec
41ab0 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33  tion. If sqlite3
41ac0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
41ad0 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
41ae0 74 68 65 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63  the.** blocked c
41af0 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64  onnection alread
41b00 79 20 68 61 73 20 61 20 72 65 67 69 73 74 65 72  y has a register
41b10 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  ed unlock-notify
41b20 20 63 61 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68   callback,.** th
41b30 65 6e 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62  en the new callb
41b40 61 63 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65  ack replaces the
41b50 20 6f 6c 64 2e 20 49 66 20 73 71 6c 69 74 65 33   old. If sqlite3
41b60 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
41b70 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69   is.** called wi
41b80 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  th a NULL pointe
41b90 72 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20  r as its second 
41ba0 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 61  argument, then a
41bb0 6e 79 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 75  ny existing.** u
41bc0 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
41bd0 6c 62 61 63 6b 20 69 73 20 63 61 6e 63 65 6c 6c  lback is cancell
41be0 65 64 2e 20 54 68 65 20 62 6c 6f 63 6b 65 64 20  ed. The blocked 
41bf0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 0a 2a 2a 20  connections .** 
41c00 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
41c10 6c 6c 62 61 63 6b 20 6d 61 79 20 61 6c 73 6f 20  llback may also 
41c20 62 65 20 63 61 6e 63 65 6c 65 64 20 62 79 20 63  be canceled by c
41c30 6c 6f 73 69 6e 67 20 74 68 65 20 62 6c 6f 63 6b  losing the block
41c40 65 64 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  ed.** connection
41c50 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
41c60 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  close()]..**.** 
41c70 54 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  The unlock-notif
41c80 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f  y callback is no
41c90 74 20 72 65 65 6e 74 72 61 6e 74 2e 20 49 66 20  t reentrant. If 
41ca0 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69  an application i
41cb0 6e 76 6f 6b 65 73 0a 2a 2a 20 61 6e 79 20 73 71  nvokes.** any sq
41cc0 6c 69 74 65 33 5f 78 78 78 20 41 50 49 20 66 75  lite3_xxx API fu
41cd0 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 77 69 74  nctions from wit
41ce0 68 69 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f  hin an unlock-no
41cf0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c 20 61  tify callback, a
41d00 0a 2a 2a 20 63 72 61 73 68 20 6f 72 20 64 65 61  .** crash or dea
41d10 64 6c 6f 63 6b 20 6d 61 79 20 62 65 20 74 68 65  dlock may be the
41d20 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55   result..**.** U
41d30 6e 6c 65 73 73 20 64 65 61 64 6c 6f 63 6b 20 69  nless deadlock i
41d40 73 20 64 65 74 65 63 74 65 64 20 28 73 65 65 20  s detected (see 
41d50 62 65 6c 6f 77 29 2c 20 73 71 6c 69 74 65 33 5f  below), sqlite3_
41d60 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20  unlock_notify() 
41d70 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e  always.** return
41d80 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a  s SQLITE_OK..**.
41d90 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61 63 6b 20 49  ** <b>Callback I
41da0 6e 76 6f 63 61 74 69 6f 6e 20 44 65 74 61 69 6c  nvocation Detail
41db0 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  s</b>.**.** When
41dc0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
41dd0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65  y callback is re
41de0 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 61 70  gistered, the ap
41df0 70 6c 69 63 61 74 69 6f 6e 20 70 72 6f 76 69 64  plication provid
41e00 65 73 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20  es a .** single 
41e10 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 20 74 68  void* pointer th
41e20 61 74 20 69 73 20 70 61 73 73 65 64 20 74 6f 20  at is passed to 
41e30 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 68 65  the callback whe
41e40 6e 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e  n it is invoked.
41e50 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65  .** However, the
41e60 20 73 69 67 6e 61 74 75 72 65 20 6f 66 20 74 68   signature of th
41e70 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
41e80 69 6f 6e 20 61 6c 6c 6f 77 73 20 53 51 4c 69 74  ion allows SQLit
41e90 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 69 74 20  e to pass.** it 
41ea0 61 6e 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64  an array of void
41eb0 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  * context pointe
41ec0 72 73 2e 20 54 68 65 20 66 69 72 73 74 20 61 72  rs. The first ar
41ed0 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f  gument passed to
41ee0 0a 2a 2a 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f  .** an unlock-no
41ef0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  tify callback is
41f00 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
41f10 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20   array of void* 
41f20 70 6f 69 6e 74 65 72 73 2c 0a 2a 2a 20 61 6e 64  pointers,.** and
41f30 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74   the second is t
41f40 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
41f50 72 69 65 73 20 69 6e 20 74 68 65 20 61 72 72 61  ries in the arra
41f60 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  y..**.** When a 
41f70 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74  blocking connect
41f80 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  ions transaction
41f90 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2c 20 74   is concluded, t
41fa0 68 65 72 65 20 6d 61 79 20 62 65 0a 2a 2a 20 6d  here may be.** m
41fb0 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 62 6c 6f  ore than one blo
41fc0 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  cked connection 
41fd0 74 68 61 74 20 68 61 73 20 72 65 67 69 73 74 65  that has registe
41fe0 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63  red for an unloc
41ff0 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c  k-notify.** call
42000 62 61 63 6b 2e 20 49 66 20 74 77 6f 20 6f 72 20  back. If two or 
42010 6d 6f 72 65 20 73 75 63 68 20 62 6c 6f 63 6b 65  more such blocke
42020 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 61  d connections ha
42030 76 65 20 73 70 65 63 69 66 69 65 64 20 74 68 65  ve specified the
42040 0a 2a 2a 20 73 61 6d 65 20 63 61 6c 6c 62 61 63  .** same callbac
42050 6b 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e  k function, then
42060 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f   instead of invo
42070 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63  king the callbac
42080 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75  k function.** mu
42090 6c 74 69 70 6c 65 20 74 69 6d 65 73 2c 20 69 74  ltiple times, it
420a0 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65   is invoked once
420b0 20 77 69 74 68 20 74 68 65 20 73 65 74 20 6f 66   with the set of
420c0 20 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70   void* context p
420d0 6f 69 6e 74 65 72 73 0a 2a 2a 20 73 70 65 63 69  ointers.** speci
420e0 66 69 65 64 20 62 79 20 74 68 65 20 62 6c 6f 63  fied by the bloc
420f0 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ked connections 
42100 62 75 6e 64 6c 65 64 20 74 6f 67 65 74 68 65 72  bundled together
42110 20 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 2e 0a   into an array..
42120 2a 2a 20 54 68 69 73 20 67 69 76 65 73 20 74 68  ** This gives th
42130 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 61 6e  e application an
42140 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20   opportunity to 
42150 70 72 69 6f 72 69 74 69 7a 65 20 61 6e 79 20 61  prioritize any a
42160 63 74 69 6f 6e 73 20 0a 2a 2a 20 72 65 6c 61 74  ctions .** relat
42170 65 64 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66  ed to the set of
42180 20 75 6e 62 6c 6f 63 6b 65 64 20 64 61 74 61 62   unblocked datab
42190 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
421a0 0a 2a 2a 0a 2a 2a 20 3c 62 3e 44 65 61 64 6c 6f  .**.** <b>Deadlo
421b0 63 6b 20 44 65 74 65 63 74 69 6f 6e 3c 2f 62 3e  ck Detection</b>
421c0 0a 2a 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20  .**.** Assuming 
421d0 74 68 61 74 20 61 66 74 65 72 20 72 65 67 69 73  that after regis
421e0 74 65 72 69 6e 67 20 66 6f 72 20 61 6e 20 75 6e  tering for an un
421f0 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
42200 62 61 63 6b 20 61 20 0a 2a 2a 20 64 61 74 61 62  back a .** datab
42210 61 73 65 20 77 61 69 74 73 20 66 6f 72 20 74 68  ase waits for th
42220 65 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65  e callback to be
42230 20 69 73 73 75 65 64 20 62 65 66 6f 72 65 20 74   issued before t
42240 61 6b 69 6e 67 20 61 6e 79 20 66 75 72 74 68 65  aking any furthe
42250 72 0a 2a 2a 20 61 63 74 69 6f 6e 20 28 61 20 72  r.** action (a r
42260 65 61 73 6f 6e 61 62 6c 65 20 61 73 73 75 6d 70  easonable assump
42270 74 69 6f 6e 29 2c 20 74 68 65 6e 20 75 73 69 6e  tion), then usin
42280 67 20 74 68 69 73 20 41 50 49 20 6d 61 79 20 63  g this API may c
42290 61 75 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 6c  ause the.** appl
422a0 69 63 61 74 69 6f 6e 20 74 6f 20 64 65 61 64 6c  ication to deadl
422b0 6f 63 6b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ock. For example
422c0 2c 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  , if connection 
422d0 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72  X is waiting for
422e0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 59  .** connection Y
422f0 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  's transaction t
42300 6f 20 62 65 20 63 6f 6e 63 6c 75 64 65 64 2c 20  o be concluded, 
42310 61 6e 64 20 73 69 6d 69 6c 61 72 6c 79 20 63 6f  and similarly co
42320 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 59 20 69 73  nnection.** Y is
42330 20 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e   waiting on conn
42340 65 63 74 69 6f 6e 20 58 27 73 20 74 72 61 6e 73  ection X's trans
42350 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 6e 65 69  action, then nei
42360 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  ther connection.
42370 2a 2a 20 77 69 6c 6c 20 70 72 6f 63 65 65 64 20  ** will proceed 
42380 61 6e 64 20 74 68 65 20 73 79 73 74 65 6d 20 6d  and the system m
42390 61 79 20 72 65 6d 61 69 6e 20 64 65 61 64 6c 6f  ay remain deadlo
423a0 63 6b 65 64 20 69 6e 64 65 66 69 6e 69 74 65 6c  cked indefinitel
423b0 79 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69  y..**.** To avoi
423c0 64 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f 2c  d this scenario,
423d0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c   the sqlite3_unl
423e0 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 70 65 72  ock_notify() per
423f0 66 6f 72 6d 73 20 64 65 61 64 6c 6f 63 6b 0a 2a  forms deadlock.*
42400 2a 20 64 65 74 65 63 74 69 6f 6e 2e 20 49 66 20  * detection. If 
42410 61 20 67 69 76 65 6e 20 63 61 6c 6c 20 74 6f 20  a given call to 
42420 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
42430 6f 74 69 66 79 28 29 20 77 6f 75 6c 64 20 70 75  otify() would pu
42440 74 20 74 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20  t the.** system 
42450 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20  in a deadlocked 
42460 73 74 61 74 65 2c 20 74 68 65 6e 20 53 51 4c 49  state, then SQLI
42470 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74  TE_LOCKED is ret
42480 75 72 6e 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20  urned and no.** 
42490 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
424a0 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69 73 74  llback is regist
424b0 65 72 65 64 2e 20 54 68 65 20 73 79 73 74 65 6d  ered. The system
424c0 20 69 73 20 73 61 69 64 20 74 6f 20 62 65 20 69   is said to be i
424d0 6e 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 65  n.** a deadlocke
424e0 64 20 73 74 61 74 65 20 69 66 20 63 6f 6e 6e 65  d state if conne
424f0 63 74 69 6f 6e 20 41 20 68 61 73 20 72 65 67 69  ction A has regi
42500 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e  stered for an un
42510 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63  lock-notify.** c
42520 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63  allback on the c
42530 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e  onclusion of con
42540 6e 65 63 74 69 6f 6e 20 42 27 73 20 74 72 61 6e  nection B's tran
42550 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 63 6f 6e  saction, and con
42560 6e 65 63 74 69 6f 6e 0a 2a 2a 20 42 20 68 61 73  nection.** B has
42570 20 69 74 73 65 6c 66 20 72 65 67 69 73 74 65 72   itself register
42580 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b  ed for an unlock
42590 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
425a0 20 77 68 65 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e   when connection
425b0 0a 2a 2a 20 41 27 73 20 74 72 61 6e 73 61 63 74  .** A's transact
425c0 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64  ion is concluded
425d0 2e 20 49 6e 64 69 72 65 63 74 20 64 65 61 64 6c  . Indirect deadl
425e0 6f 63 6b 20 69 73 20 61 6c 73 6f 20 64 65 74 65  ock is also dete
425f0 63 74 65 64 2c 20 73 6f 0a 2a 2a 20 74 68 65 20  cted, so.** the 
42600 73 79 73 74 65 6d 20 69 73 20 61 6c 73 6f 20 63  system is also c
42610 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20  onsidered to be 
42620 64 65 61 64 6c 6f 63 6b 65 64 20 69 66 20 63 6f  deadlocked if co
42630 6e 6e 65 63 74 69 6f 6e 20 42 20 68 61 73 0a 2a  nnection B has.*
42640 2a 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  * registered for
42650 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
42660 79 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68  y callback on th
42670 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20  e conclusion of 
42680 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 43 27  connection.** C'
42690 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 77  s transaction, w
426a0 68 65 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  here connection 
426b0 43 20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e 20  C is waiting on 
426c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 2e 20 41 6e  connection A. An
426d0 79 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 6c  y.** number of l
426e0 65 76 65 6c 73 20 6f 66 20 69 6e 64 69 72 65 63  evels of indirec
426f0 74 69 6f 6e 20 61 72 65 20 61 6c 6c 6f 77 65 64  tion are allowed
42700 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 54 68 65 20 22  ..**.** <b>The "
42710 44 52 4f 50 20 54 41 42 4c 45 22 20 45 78 63 65  DROP TABLE" Exce
42720 70 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20  ption</b>.**.** 
42730 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b  When a call to [
42740 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
42750 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4c  returns SQLITE_L
42760 4f 43 4b 45 44 2c 20 69 74 20 69 73 20 61 6c 6d  OCKED, it is alm
42770 6f 73 74 20 0a 2a 2a 20 61 6c 77 61 79 73 20 61  ost .** always a
42780 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 63 61  ppropriate to ca
42790 6c 6c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  ll sqlite3_unloc
427a0 6b 5f 6e 6f 74 69 66 79 28 29 2e 20 54 68 65 72  k_notify(). Ther
427b0 65 20 69 73 20 68 6f 77 65 76 65 72 2c 0a 2a 2a  e is however,.**
427c0 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 2e 20   one exception. 
427d0 57 68 65 6e 20 65 78 65 63 75 74 69 6e 67 20 61  When executing a
427e0 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 6f 72   "DROP TABLE" or
427f0 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20 73 74   "DROP INDEX" st
42800 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 53 51 4c 69  atement,.** SQLi
42810 74 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65  te checks if the
42820 72 65 20 61 72 65 20 61 6e 79 20 63 75 72 72 65  re are any curre
42830 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 53  ntly executing S
42840 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
42850 0a 2a 2a 20 74 68 61 74 20 62 65 6c 6f 6e 67 20  .** that belong 
42860 74 6f 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e  to the same conn
42870 65 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65  ection. If there
42880 20 61 72 65 2c 20 53 51 4c 49 54 45 5f 4c 4f 43   are, SQLITE_LOC
42890 4b 45 44 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  KED is.** return
428a0 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
428b0 20 74 68 65 72 65 20 69 73 20 6e 6f 20 22 62 6c   there is no "bl
428c0 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
428d0 6e 22 2c 20 73 6f 20 69 6e 76 6f 6b 69 6e 67 0a  n", so invoking.
428e0 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  ** sqlite3_unloc
428f0 6b 5f 6e 6f 74 69 66 79 28 29 20 72 65 73 75 6c  k_notify() resul
42900 74 73 20 69 6e 20 74 68 65 20 75 6e 6c 6f 63 6b  ts in the unlock
42910 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
42920 20 62 65 69 6e 67 0a 2a 2a 20 69 6e 76 6f 6b 65   being.** invoke
42930 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49  d immediately. I
42940 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
42950 6e 20 74 68 65 6e 20 72 65 2d 61 74 74 65 6d 70  n then re-attemp
42960 74 73 20 74 68 65 20 22 44 52 4f 50 20 54 41 42  ts the "DROP TAB
42970 4c 45 22 0a 2a 2a 20 6f 72 20 22 44 52 4f 50 20  LE".** or "DROP 
42980 49 4e 44 45 58 22 20 71 75 65 72 79 2c 20 61 6e  INDEX" query, an
42990 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 20 6d   infinite loop m
429a0 69 67 68 74 20 62 65 20 74 68 65 20 72 65 73 75  ight be the resu
429b0 6c 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 77 61  lt..**.** One wa
429c0 79 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72  y around this pr
429d0 6f 62 6c 65 6d 20 69 73 20 74 6f 20 63 68 65 63  oblem is to chec
429e0 6b 20 74 68 65 20 65 78 74 65 6e 64 65 64 20 65  k the extended e
429f0 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e  rror code return
42a00 65 64 0a 2a 2a 20 62 79 20 61 6e 20 73 71 6c 69  ed.** by an sqli
42a10 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 2e  te3_step() call.
42a20 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 62   If there is a b
42a30 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69  locking connecti
42a40 6f 6e 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  on, then the.** 
42a50 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
42a60 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 53 51  ode is set to SQ
42a70 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52  LITE_LOCKED_SHAR
42a80 45 44 43 41 43 48 45 2e 20 4f 74 68 65 72 77 69  EDCACHE. Otherwi
42a90 73 65 2c 20 69 6e 0a 2a 2a 20 74 68 65 20 73 70  se, in.** the sp
42aa0 65 63 69 61 6c 20 22 44 52 4f 50 20 54 41 42 4c  ecial "DROP TABL
42ab0 45 2f 49 4e 44 45 58 22 20 63 61 73 65 2c 20 74  E/INDEX" case, t
42ac0 68 65 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f  he extended erro
42ad0 72 20 63 6f 64 65 20 69 73 20 6a 75 73 74 20 0a  r code is just .
42ae0 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  ** SQLITE_LOCKED
42af0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
42b00 69 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  int sqlite3_unlo
42b10 63 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c  ck_notify(.  sql
42b20 69 74 65 33 20 2a 70 42 6c 6f 63 6b 65 64 2c 20  ite3 *pBlocked, 
42b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42b40 20 20 20 20 20 20 20 20 20 2f 2a 20 57 61 69 74           /* Wait
42b50 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ing connection *
42b60 2f 0a 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69  /.  void (*xNoti
42b70 66 79 29 28 76 6f 69 64 20 2a 2a 61 70 41 72 67  fy)(void **apArg
42b80 2c 20 69 6e 74 20 6e 41 72 67 29 2c 20 20 20 20  , int nArg),    
42b90 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63  /* Callback func
42ba0 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 2a  tion to invoke *
42bb0 2f 0a 20 20 76 6f 69 64 20 2a 70 4e 6f 74 69 66  /.  void *pNotif
42bc0 79 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20  yArg            
42bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
42be0 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70  /* Argument to p
42bf0 61 73 73 20 74 6f 20 78 4e 6f 74 69 66 79 20 2a  ass to xNotify *
42c00 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f  /.);../*.** Undo
42c10 20 74 68 65 20 68 61 63 6b 20 74 68 61 74 20 63   the hack that c
42c20 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74 69 6e 67  onverts floating
42c30 20 70 6f 69 6e 74 20 74 79 70 65 73 20 74 6f 20   point types to 
42c40 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a 2a 20 62  integer for.** b
42c50 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63 65 73 73  uilds on process
42c60 6f 72 73 20 77 69 74 68 6f 75 74 20 66 6c 6f 61  ors without floa
42c70 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f  ting point suppo
42c80 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  rt..*/.#ifdef SQ
42c90 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
42ca0 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e 64 65 66  NG_POINT.# undef
42cb0 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 0a   double.#endif..
42cc0 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45 6e 64 20  #if 0.}  /* End 
42cd0 6f 66 20 74 68 65 20 27 65 78 74 65 72 6e 20 22  of the 'extern "
42ce0 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65 6e  C"' block */.#en
42cf0 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  dif.#endif../***
42d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
42d10 6f 66 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a  of sqlite3.h ***
42d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
42d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
42d60 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
42d70 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
42d80 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
42d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
42da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
42db0 75 64 65 20 68 61 73 68 2e 68 20 69 6e 20 74 68  ude hash.h in th
42dc0 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
42dd0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
42de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
42df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
42e00 6e 20 66 69 6c 65 20 68 61 73 68 2e 68 20 2a 2a  n file hash.h **
42e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
42e40 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
42e50 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
42e60 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
42e70 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
42e80 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
42e90 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
42ea0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
42eb0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
42ec0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
42ed0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
42ee0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
42ef0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
42f00 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
42f10 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
42f20 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
42f30 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
42f40 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
42f50 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
42f60 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
42f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
42fb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  **.** This is th
42fc0 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f  e header file fo
42fd0 72 20 74 68 65 20 67 65 6e 65 72 69 63 20 68 61  r the generic ha
42fe0 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  sh-table impleme
42ff0 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69  nation.** used i
43000 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  n SQLite..**.** 
43010 24 49 64 3a 20 68 61 73 68 2e 68 2c 76 20 31 2e  $Id: hash.h,v 1.
43020 31 35 20 32 30 30 39 2f 30 35 2f 30 32 20 31 33  15 2009/05/02 13
43030 3a 32 39 3a 33 38 20 64 72 68 20 45 78 70 20 24  :29:38 drh Exp $
43040 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c  .*/.#ifndef _SQL
43050 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66  ITE_HASH_H_.#def
43060 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41 53 48  ine _SQLITE_HASH
43070 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  _H_../* Forward 
43080 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20  declarations of 
43090 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74  structures. */.t
430a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48 61  ypedef struct Ha
430b0 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64 65 66  sh Hash;.typedef
430c0 20 73 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d   struct HashElem
430d0 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41   HashElem;../* A
430e0 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74   complete hash t
430f0 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61  able is an insta
43100 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
43110 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
43120 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73  ** The internals
43130 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
43140 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  re are intended 
43150 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20  to be opaque -- 
43160 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73  client.** code s
43170 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70  hould not attemp
43180 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d  t to access or m
43190 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c 64 73  odify the fields
431a0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
431b0 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20  re.** directly. 
431c0 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 74 72   Change this str
431d0 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75  ucture only by u
431e0 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65  sing the routine
431f0 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65  s below..** Howe
43200 76 65 72 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65  ver, some of the
43210 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e   "procedures" an
43220 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f  d "functions" fo
43230 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a  r modifying and.
43240 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 69  ** accessing thi
43250 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
43260 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73  really macros, s
43270 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c  o we can't reall
43280 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73  y make.** this s
43290 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e  tructure opaque.
432a0 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 6c 65 6d 65  .**.** All eleme
432b0 6e 74 73 20 6f 66 20 74 68 65 20 68 61 73 68 20  nts of the hash 
432c0 74 61 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 73  table are on a s
432d0 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e  ingle doubly-lin
432e0 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 48 61 73  ked list..** Has
432f0 68 2e 66 69 72 73 74 20 70 6f 69 6e 74 73 20 74  h.first points t
43300 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68  o the head of th
43310 69 73 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54  is list..**.** T
43320 68 65 72 65 20 61 72 65 20 48 61 73 68 2e 68 74  here are Hash.ht
43330 73 69 7a 65 20 62 75 63 6b 65 74 73 2e 20 20 45  size buckets.  E
43340 61 63 68 20 62 75 63 6b 65 74 20 70 6f 69 6e 74  ach bucket point
43350 73 20 74 6f 20 61 20 73 70 6f 74 20 69 6e 0a 2a  s to a spot in.*
43360 2a 20 74 68 65 20 67 6c 6f 62 61 6c 20 64 6f 75  * the global dou
43370 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  bly-linked list.
43380 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f    The contents o
43390 66 20 74 68 65 20 62 75 63 6b 65 74 20 61 72 65  f the bucket are
433a0 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20   the.** element 
433b0 70 6f 69 6e 74 65 64 20 74 6f 20 70 6c 75 73 20  pointed to plus 
433c0 74 68 65 20 6e 65 78 74 20 5f 68 74 2e 63 6f 75  the next _ht.cou
433d0 6e 74 2d 31 20 65 6c 65 6d 65 6e 74 73 20 69 6e  nt-1 elements in
433e0 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a   the list..**.**
433f0 20 48 61 73 68 2e 68 74 73 69 7a 65 20 61 6e 64   Hash.htsize and
43400 20 48 61 73 68 2e 68 74 20 6d 61 79 20 62 65 20   Hash.ht may be 
43410 7a 65 72 6f 2e 20 20 49 6e 20 74 68 61 74 20 63  zero.  In that c
43420 61 73 65 20 6c 6f 6f 6b 75 70 20 69 73 20 64 6f  ase lookup is do
43430 6e 65 0a 2a 2a 20 62 79 20 61 20 6c 69 6e 65 61  ne.** by a linea
43440 72 20 73 65 61 72 63 68 20 6f 66 20 74 68 65 20  r search of the 
43450 67 6c 6f 62 61 6c 20 6c 69 73 74 2e 20 20 46 6f  global list.  Fo
43460 72 20 73 6d 61 6c 6c 20 74 61 62 6c 65 73 2c 20  r small tables, 
43470 74 68 65 20 0a 2a 2a 20 48 61 73 68 2e 68 74 20  the .** Hash.ht 
43480 74 61 62 6c 65 20 69 73 20 6e 65 76 65 72 20 61  table is never a
43490 6c 6c 6f 63 61 74 65 64 20 62 65 63 61 75 73 65  llocated because
434a0 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65   if there are fe
434b0 77 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e  w elements.** in
434c0 20 74 68 65 20 74 61 62 6c 65 2c 20 69 74 20 69   the table, it i
434d0 73 20 66 61 73 74 65 72 20 74 6f 20 64 6f 20 61  s faster to do a
434e0 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20 74   linear search t
434f0 68 61 6e 20 74 6f 20 6d 61 6e 61 67 65 0a 2a 2a  han to manage.**
43500 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
43510 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 20  .*/.struct Hash 
43520 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
43530 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a   htsize;      /*
43540 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65   Number of bucke
43550 74 73 20 69 6e 20 74 68 65 20 68 61 73 68 20 74  ts in the hash t
43560 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  able */.  unsign
43570 65 64 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20  ed int count;   
43580 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
43590 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73   entries in this
435a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68   table */.  Hash
435b0 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20 20 20  Elem *first;    
435c0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72        /* The fir
435d0 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  st element of th
435e0 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 74 72  e array */.  str
435f0 75 63 74 20 5f 68 74 20 7b 20 20 20 20 20 20 20  uct _ht {       
43600 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61         /* the ha
43610 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  sh table */.    
43620 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
43630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
43640 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
43650 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20 2a  with this hash *
43660 2f 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a  /.    HashElem *
43670 63 68 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20  chain;          
43680 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66   /* Pointer to f
43690 69 72 73 74 20 65 6e 74 72 79 20 77 69 74 68 20  irst entry with 
436a0 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d  this hash */.  }
436b0 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63   *ht;.};../* Eac
436c0 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65  h element in the
436d0 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 61   hash table is a
436e0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
436f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20  e following .** 
43700 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20  structure.  All 
43710 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f  elements are sto
43720 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  red on a single 
43730 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69  doubly-linked li
43740 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c  st..**.** Again,
43750 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
43760 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  is intended to b
43770 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74  e opaque, but it
43780 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a   can't really.**
43790 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 61 75   be opaque becau
437a0 73 65 20 69 74 20 69 73 20 75 73 65 64 20 62 79  se it is used by
437b0 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75   macros..*/.stru
437c0 63 74 20 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20  ct HashElem {.  
437d0 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20  HashElem *next, 
437e0 2a 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20  *prev;       /* 
437f0 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75  Next and previou
43800 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  s elements in th
43810 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69  e table */.  voi
43820 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20  d *data;        
43830 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
43840 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
43850 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a  h this element *
43860 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
43870 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20  pKey; int nKey; 
43880 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74   /* Key associat
43890 65 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65  ed with this ele
438a0 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ment */.};../*.*
438b0 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65  * Access routine
438c0 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69  s.  To delete, i
438d0 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69  nsert a NULL poi
438e0 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nter..*/.SQLITE_
438f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
43900 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 61 73  ite3HashInit(Has
43910 68 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  h*);.SQLITE_PRIV
43920 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
43930 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68  3HashInsert(Hash
43940 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  *, const char *p
43950 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76  Key, int nKey, v
43960 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c  oid *pData);.SQL
43970 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
43980 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e   *sqlite3HashFin
43990 64 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63  d(const Hash*, c
439a0 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c  onst char *pKey,
439b0 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49   int nKey);.SQLI
439c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
439d0 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
439e0 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  (Hash*);../*.** 
439f0 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69  Macros for loopi
43a00 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d  ng over all elem
43a10 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68 20 74  ents of a hash t
43a20 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69 6f 6d  able.  The idiom
43a30 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73   is.** like this
43a40 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68  :.**.**   Hash h
43a50 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65 6d 20  ;.**   HashElem 
43a60 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20  *p;.**   ....** 
43a70 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61    for(p=sqliteHa
43a80 73 68 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20  shFirst(&h); p; 
43a90 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  p=sqliteHashNext
43aa0 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d  (p)){.**     Som
43ab0 65 53 74 72 75 63 74 75 72 65 20 2a 70 44 61 74  eStructure *pDat
43ac0 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61  a = sqliteHashDa
43ad0 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f  ta(p);.**     //
43ae0 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69   do something wi
43af0 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a  th pData.**   }.
43b00 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  */.#define sqlit
43b10 65 48 61 73 68 46 69 72 73 74 28 48 29 20 20 28  eHashFirst(H)  (
43b20 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64 65 66  (H)->first).#def
43b30 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4e 65  ine sqliteHashNe
43b40 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65  xt(E)   ((E)->ne
43b50 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  xt).#define sqli
43b60 74 65 48 61 73 68 44 61 74 61 28 45 29 20 20 20  teHashData(E)   
43b70 28 28 45 29 2d 3e 64 61 74 61 29 0a 2f 2a 20 23  ((E)->data)./* #
43b80 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
43b90 68 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d  hKey(E)    ((E)-
43ba0 3e 70 4b 65 79 29 20 2f 2f 20 4e 4f 54 20 55 53  >pKey) // NOT US
43bb0 45 44 20 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65  ED */./* #define
43bc0 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 73 69   sqliteHashKeysi
43bd0 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79  ze(E) ((E)->nKey
43be0 29 20 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a  )  // NOT USED *
43bf0 2f 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20  /../*.** Number 
43c00 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20  of entries in a 
43c10 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 2f 2a  hash table.*/./*
43c20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48   #define sqliteH
43c30 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48  ashCount(H)  ((H
43c40 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e 4f 54  )->count) // NOT
43c50 20 55 53 45 44 20 2a 2f 0a 0a 23 65 6e 64 69 66   USED */..#endif
43c60 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 48 41 53 48   /* _SQLITE_HASH
43c70 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
43c80 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
43c90 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
43ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43cc0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
43cd0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
43ce0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
43cf0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
43d00 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
43d10 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
43d20 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
43d30 70 61 72 73 65 2e 68 20 69 6e 20 74 68 65 20 6d  parse.h in the m
43d40 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
43d50 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
43d60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
43d70 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
43d80 6c 65 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a  le parse.h *****
43d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
43db0 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20  ******/.#define 
43dc0 54 4b 5f 53 45 4d 49 20 20 20 20 20 20 20 20 20  TK_SEMI         
43dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43de0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f     1.#define TK_
43df0 45 58 50 4c 41 49 4e 20 20 20 20 20 20 20 20 20  EXPLAIN         
43e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e10 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 51 55 45  2.#define TK_QUE
43e20 52 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RY              
43e30 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23               3.#
43e40 64 65 66 69 6e 65 20 54 4b 5f 50 4c 41 4e 20 20  define TK_PLAN  
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 20 20 20 34 0a 23 64 65 66            4.#def
43e70 69 6e 65 20 54 4b 5f 42 45 47 49 4e 20 20 20 20  ine TK_BEGIN    
43e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43e90 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65         5.#define
43ea0 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20   TK_TRANSACTION 
43eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ec0 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 54 4b      6.#define TK
43ed0 5f 44 45 46 45 52 52 45 44 20 20 20 20 20 20 20  _DEFERRED       
43ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43ef0 20 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d   7.#define TK_IM
43f00 4d 45 44 49 41 54 45 20 20 20 20 20 20 20 20 20  MEDIATE         
43f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a                8.
43f20 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55  #define TK_EXCLU
43f30 53 49 56 45 20 20 20 20 20 20 20 20 20 20 20 20  SIVE            
43f40 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65             9.#de
43f50 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54 20 20  fine TK_COMMIT  
43f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43f70 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e         10.#defin
43f80 65 20 54 4b 5f 45 4e 44 20 20 20 20 20 20 20 20  e TK_END        
43f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43fa0 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 54      11.#define T
43fb0 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 20  K_ROLLBACK      
43fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43fd0 20 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53   12.#define TK_S
43fe0 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20  AVEPOINT        
43ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
44000 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4c 45  .#define TK_RELE
44010 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ASE             
44020 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 64             14.#d
44030 65 66 69 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20  efine TK_TO     
44040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44050 20 20 20 20 20 20 20 20 31 35 0a 23 64 65 66 69          15.#defi
44060 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20 20 20  ne TK_TABLE     
44070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44080 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20       16.#define 
44090 54 4b 5f 43 52 45 41 54 45 20 20 20 20 20 20 20  TK_CREATE       
440a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440b0 20 20 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    17.#define TK_
440c0 49 46 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IF              
440d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
440e0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54  8.#define TK_NOT
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 20 20 31 39 0a 23              19.#
44110 64 65 66 69 6e 65 20 54 4b 5f 45 58 49 53 54 53  define TK_EXISTS
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 20 20 32 30 0a 23 64 65 66           20.#def
44140 69 6e 65 20 54 4b 5f 54 45 4d 50 20 20 20 20 20  ine TK_TEMP     
44150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44160 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65        21.#define
44170 20 54 4b 5f 4c 50 20 20 20 20 20 20 20 20 20 20   TK_LP          
44180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44190 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 54 4b     22.#define TK
441a0 5f 52 50 20 20 20 20 20 20 20 20 20 20 20 20 20  _RP             
441b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
441c0 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53  23.#define TK_AS
441d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
441e0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 34 0a               24.
441f0 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41  #define TK_COMMA
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 20 20 32 35 0a 23 64 65            25.#de
44220 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20 20 20  fine TK_ID      
44230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44240 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e         26.#defin
44250 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20 20 20  e TK_INDEXED    
44260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44270 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 54      27.#define T
44280 4b 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 20  K_ABORT         
44290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
442a0 20 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41   28.#define TK_A
442b0 46 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20  FTER            
442c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 39                29
442d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 41 4c  .#define TK_ANAL
442e0 59 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20  YZE             
442f0 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 64             30.#d
44300 65 66 69 6e 65 20 54 4b 5f 41 53 43 20 20 20 20  efine TK_ASC    
44310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44320 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 66 69          31.#defi
44330 6e 65 20 54 4b 5f 41 54 54 41 43 48 20 20 20 20  ne TK_ATTACH    
44340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44350 20 20 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20       32.#define 
44360 54 4b 5f 42 45 46 4f 52 45 20 20 20 20 20 20 20  TK_BEFORE       
44370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44380 20 20 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    33.#define TK_
44390 42 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  BY              
443a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
443b0 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53  4.#define TK_CAS
443c0 43 41 44 45 20 20 20 20 20 20 20 20 20 20 20 20  CADE            
443d0 20 20 20 20 20 20 20 20 20 20 20 20 33 35 0a 23              35.#
443e0 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 54 20 20  define TK_CAST  
443f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44400 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66           36.#def
44410 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20  ine TK_COLUMNKW 
44420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44430 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65        37.#define
44440 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20   TK_CONFLICT    
44450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44460 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20 54 4b     38.#define TK
44470 5f 44 41 54 41 42 41 53 45 20 20 20 20 20 20 20  _DATABASE       
44480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44490 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45  39.#define TK_DE
444a0 53 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SC              
444b0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a               40.
444c0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 54 41 43  #define TK_DETAC
444d0 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
444e0 20 20 20 20 20 20 20 20 20 20 34 31 0a 23 64 65            41.#de
444f0 66 69 6e 65 20 54 4b 5f 45 41 43 48 20 20 20 20  fine TK_EACH    
44500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44510 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e         42.#defin
44520 65 20 54 4b 5f 46 41 49 4c 20 20 20 20 20 20 20  e TK_FAIL       
44530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44540 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 54      43.#define T
44550 4b 5f 46 4f 52 20 20 20 20 20 20 20 20 20 20 20  K_FOR           
44560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44570 20 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49   44.#define TK_I
44580 47 4e 4f 52 45 20 20 20 20 20 20 20 20 20 20 20  GNORE           
44590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35                45
445a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54  .#define TK_INIT
445b0 49 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20  IALLY           
445c0 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64             46.#d
445d0 65 66 69 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44  efine TK_INSTEAD
445e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
445f0 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 69          47.#defi
44600 6e 65 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20  ne TK_LIKE_KW   
44610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44620 20 20 20 20 20 34 38 0a 23 64 65 66 69 6e 65 20       48.#define 
44630 54 4b 5f 4d 41 54 43 48 20 20 20 20 20 20 20 20  TK_MATCH        
44640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44650 20 20 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    49.#define TK_
44660 4b 45 59 20 20 20 20 20 20 20 20 20 20 20 20 20  KEY             
44670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
44680 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 20  0.#define TK_OF 
44690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
446a0 20 20 20 20 20 20 20 20 20 20 20 20 35 31 0a 23              51.#
446b0 64 65 66 69 6e 65 20 54 4b 5f 4f 46 46 53 45 54  define TK_OFFSET
446c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
446d0 20 20 20 20 20 20 20 20 20 35 32 0a 23 64 65 66           52.#def
446e0 69 6e 65 20 54 4b 5f 50 52 41 47 4d 41 20 20 20  ine TK_PRAGMA   
446f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44700 20 20 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65        53.#define
44710 20 54 4b 5f 52 41 49 53 45 20 20 20 20 20 20 20   TK_RAISE       
44720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44730 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 54 4b     54.#define TK
44740 5f 52 45 50 4c 41 43 45 20 20 20 20 20 20 20 20  _REPLACE        
44750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44760 35 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  55.#define TK_RE
44770 53 54 52 49 43 54 20 20 20 20 20 20 20 20 20 20  STRICT          
44780 20 20 20 20 20 20 20 20 20 20 20 20 20 35 36 0a               56.
44790 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 57 20 20  #define TK_ROW  
447a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
447b0 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 64 65            57.#de
447c0 66 69 6e 65 20 54 4b 5f 54 52 49 47 47 45 52 20  fine TK_TRIGGER 
447d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
447e0 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 69 6e         58.#defin
447f0 65 20 54 4b 5f 56 41 43 55 55 4d 20 20 20 20 20  e TK_VACUUM     
44800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44810 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 20 54      59.#define T
44820 4b 5f 56 49 45 57 20 20 20 20 20 20 20 20 20 20  K_VIEW          
44830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44840 20 36 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56   60.#define TK_V
44850 49 52 54 55 41 4c 20 20 20 20 20 20 20 20 20 20  IRTUAL          
44860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31                61
44870 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 49 4e  .#define TK_REIN
44880 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20  DEX             
44890 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23 64             62.#d
448a0 65 66 69 6e 65 20 54 4b 5f 52 45 4e 41 4d 45 20  efine TK_RENAME 
448b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
448c0 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 66 69          63.#defi
448d0 6e 65 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 20 20  ne TK_CTIME_KW  
448e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
448f0 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20       64.#define 
44900 54 4b 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20  TK_ANY          
44910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44920 20 20 36 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    65.#define TK_
44930 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OR              
44940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
44950 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 44  6.#define TK_AND
44960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44970 20 20 20 20 20 20 20 20 20 20 20 20 36 37 0a 23              67.#
44980 64 65 66 69 6e 65 20 54 4b 5f 49 53 20 20 20 20  define TK_IS    
44990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
449a0 20 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 66           68.#def
449b0 69 6e 65 20 54 4b 5f 42 45 54 57 45 45 4e 20 20  ine TK_BETWEEN  
449c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
449d0 20 20 20 20 20 20 36 39 0a 23 64 65 66 69 6e 65        69.#define
449e0 20 54 4b 5f 49 4e 20 20 20 20 20 20 20 20 20 20   TK_IN          
449f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a00 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20 54 4b     70.#define TK
44a10 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20  _ISNULL         
44a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a30 37 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f  71.#define TK_NO
44a40 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20  TNULL           
44a50 20 20 20 20 20 20 20 20 20 20 20 20 20 37 32 0a               72.
44a60 23 64 65 66 69 6e 65 20 54 4b 5f 4e 45 20 20 20  #define TK_NE   
44a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44a80 20 20 20 20 20 20 20 20 20 20 37 33 0a 23 64 65            73.#de
44a90 66 69 6e 65 20 54 4b 5f 45 51 20 20 20 20 20 20  fine TK_EQ      
44aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ab0 20 20 20 20 20 20 20 37 34 0a 23 64 65 66 69 6e         74.#defin
44ac0 65 20 54 4b 5f 47 54 20 20 20 20 20 20 20 20 20  e TK_GT         
44ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ae0 20 20 20 20 37 35 0a 23 64 65 66 69 6e 65 20 54      75.#define T
44af0 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  K_LE            
44b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b10 20 37 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c   76.#define TK_L
44b20 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
44b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37                77
44b40 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 45 20 20  .#define TK_GE  
44b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b60 20 20 20 20 20 20 20 20 20 20 20 37 38 0a 23 64             78.#d
44b70 65 66 69 6e 65 20 54 4b 5f 45 53 43 41 50 45 20  efine TK_ESCAPE 
44b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44b90 20 20 20 20 20 20 20 20 37 39 0a 23 64 65 66 69          79.#defi
44ba0 6e 65 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 20  ne TK_BITAND    
44bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44bc0 20 20 20 20 20 38 30 0a 23 64 65 66 69 6e 65 20       80.#define 
44bd0 54 4b 5f 42 49 54 4f 52 20 20 20 20 20 20 20 20  TK_BITOR        
44be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44bf0 20 20 38 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    81.#define TK_
44c00 4c 53 48 49 46 54 20 20 20 20 20 20 20 20 20 20  LSHIFT          
44c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
44c20 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 53 48  2.#define TK_RSH
44c30 49 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20  IFT             
44c40 20 20 20 20 20 20 20 20 20 20 20 20 38 33 0a 23              83.#
44c50 64 65 66 69 6e 65 20 54 4b 5f 50 4c 55 53 20 20  define TK_PLUS  
44c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44c70 20 20 20 20 20 20 20 20 20 38 34 0a 23 64 65 66           84.#def
44c80 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20  ine TK_MINUS    
44c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ca0 20 20 20 20 20 20 38 35 0a 23 64 65 66 69 6e 65        85.#define
44cb0 20 54 4b 5f 53 54 41 52 20 20 20 20 20 20 20 20   TK_STAR        
44cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44cd0 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 54 4b     86.#define TK
44ce0 5f 53 4c 41 53 48 20 20 20 20 20 20 20 20 20 20  _SLASH          
44cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44d00 38 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  87.#define TK_RE
44d10 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  M               
44d20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 0a               88.
44d30 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 43 41  #define TK_CONCA
44d40 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
44d50 20 20 20 20 20 20 20 20 20 20 38 39 0a 23 64 65            89.#de
44d60 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c 41 54 45 20  fine TK_COLLATE 
44d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44d80 20 20 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e         90.#defin
44d90 65 20 54 4b 5f 55 4d 49 4e 55 53 20 20 20 20 20  e TK_UMINUS     
44da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44db0 20 20 20 20 39 31 0a 23 64 65 66 69 6e 65 20 54      91.#define T
44dc0 4b 5f 55 50 4c 55 53 20 20 20 20 20 20 20 20 20  K_UPLUS         
44dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44de0 20 39 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42   92.#define TK_B
44df0 49 54 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20  ITNOT           
44e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 33                93
44e10 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 52 49  .#define TK_STRI
44e20 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NG              
44e30 20 20 20 20 20 20 20 20 20 20 20 39 34 0a 23 64             94.#d
44e40 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57  efine TK_JOIN_KW
44e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44e60 20 20 20 20 20 20 20 20 39 35 0a 23 64 65 66 69          95.#defi
44e70 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54  ne TK_CONSTRAINT
44e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44e90 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20       96.#define 
44ea0 54 4b 5f 44 45 46 41 55 4c 54 20 20 20 20 20 20  TK_DEFAULT      
44eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44ec0 20 20 39 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    97.#define TK_
44ed0 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
44ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
44ef0 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 49  8.#define TK_PRI
44f00 4d 41 52 59 20 20 20 20 20 20 20 20 20 20 20 20  MARY            
44f10 20 20 20 20 20 20 20 20 20 20 20 20 39 39 0a 23              99.#
44f20 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45  define TK_UNIQUE
44f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44f40 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65           100.#de
44f50 66 69 6e 65 20 54 4b 5f 43 48 45 43 4b 20 20 20  fine TK_CHECK   
44f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44f70 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66 69         101.#defi
44f80 6e 65 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53  ne TK_REFERENCES
44f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44fa0 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65       102.#define
44fb0 20 54 4b 5f 41 55 54 4f 49 4e 43 52 20 20 20 20   TK_AUTOINCR    
44fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44fd0 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 20 54     103.#define T
44fe0 4b 5f 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20  K_ON            
44ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45000 20 31 30 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f   104.#define TK_
45010 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20  DELETE          
45020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
45030 30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50  05.#define TK_UP
45040 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20  DATE            
45050 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 36               106
45060 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53 45  .#define TK_INSE
45070 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RT              
45080 20 20 20 20 20 20 20 20 20 20 20 31 30 37 0a 23             107.#
45090 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 20 20  define TK_SET   
450a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
450b0 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 65           108.#de
450c0 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 41 42  fine TK_DEFERRAB
450d0 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
450e0 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 66 69         109.#defi
450f0 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 20 20  ne TK_FOREIGN   
45100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45110 20 20 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65       110.#define
45120 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 20 20   TK_DROP        
45130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45140 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 54     111.#define T
45150 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20  K_UNION         
45160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45170 20 31 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   112.#define TK_
45180 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  ALL             
45190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
451a0 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58  13.#define TK_EX
451b0 43 45 50 54 20 20 20 20 20 20 20 20 20 20 20 20  CEPT            
451c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 34               114
451d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45  .#define TK_INTE
451e0 52 53 45 43 54 20 20 20 20 20 20 20 20 20 20 20  RSECT           
451f0 20 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23             115.#
45200 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 43 54  define TK_SELECT
45210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45220 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65           116.#de
45230 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e 43 54  fine TK_DISTINCT
45240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45250 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 66 69         117.#defi
45260 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 20 20  ne TK_DOT       
45270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45280 20 20 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65       118.#define
45290 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 20 20   TK_FROM        
452a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
452b0 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 54     119.#define T
452c0 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 20 20  K_JOIN          
452d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
452e0 20 31 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   120.#define TK_
452f0 55 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20  USING           
45300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
45310 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52  21.#define TK_OR
45320 44 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  DER             
45330 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 32               122
45340 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55  .#define TK_GROU
45350 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  P               
45360 20 20 20 20 20 20 20 20 20 20 20 31 32 33 0a 23             123.#
45370 64 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47  define TK_HAVING
45380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45390 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 64 65           124.#de
453a0 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20  fine TK_LIMIT   
453b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
453c0 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69         125.#defi
453d0 6e 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20  ne TK_WHERE     
453e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
453f0 20 20 20 20 20 31 32 36 0a 23 64 65 66 69 6e 65       126.#define
45400 20 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20   TK_INTO        
45410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45420 20 20 20 31 32 37 0a 23 64 65 66 69 6e 65 20 54     127.#define T
45430 4b 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20  K_VALUES        
45440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45450 20 31 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   128.#define TK_
45460 49 4e 54 45 47 45 52 20 20 20 20 20 20 20 20 20  INTEGER         
45470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
45480 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c  29.#define TK_FL
45490 4f 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20  OAT             
454a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 30               130
454b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42  .#define TK_BLOB
454c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
454d0 20 20 20 20 20 20 20 20 20 20 20 31 33 31 0a 23             131.#
454e0 64 65 66 69 6e 65 20 54 4b 5f 52 45 47 49 53 54  define TK_REGIST
454f0 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ER              
45500 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65           132.#de
45510 66 69 6e 65 20 54 4b 5f 56 41 52 49 41 42 4c 45  fine TK_VARIABLE
45520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45530 20 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69         133.#defi
45540 6e 65 20 54 4b 5f 43 41 53 45 20 20 20 20 20 20  ne TK_CASE      
45550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45560 20 20 20 20 20 31 33 34 0a 23 64 65 66 69 6e 65       134.#define
45570 20 54 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20   TK_WHEN        
45580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45590 20 20 20 31 33 35 0a 23 64 65 66 69 6e 65 20 54     135.#define T
455a0 4b 5f 54 48 45 4e 20 20 20 20 20 20 20 20 20 20  K_THEN          
455b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
455c0 20 31 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   136.#define TK_
455d0 45 4c 53 45 20 20 20 20 20 20 20 20 20 20 20 20  ELSE            
455e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
455f0 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  37.#define TK_IN
45600 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20  DEX             
45610 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 38               138
45620 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45  .#define TK_ALTE
45630 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
45640 20 20 20 20 20 20 20 20 20 20 20 31 33 39 0a 23             139.#
45650 64 65 66 69 6e 65 20 54 4b 5f 41 44 44 20 20 20  define TK_ADD   
45660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45670 20 20 20 20 20 20 20 20 20 31 34 30 0a 23 64 65           140.#de
45680 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20  fine TK_TO_TEXT 
45690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
456a0 20 20 20 20 20 20 20 31 34 31 0a 23 64 65 66 69         141.#defi
456b0 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20  ne TK_TO_BLOB   
456c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
456d0 20 20 20 20 20 31 34 32 0a 23 64 65 66 69 6e 65       142.#define
456e0 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20   TK_TO_NUMERIC  
456f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45700 20 20 20 31 34 33 0a 23 64 65 66 69 6e 65 20 54     143.#define T
45710 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20 20 20  K_TO_INT        
45720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45730 20 31 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f   144.#define TK_
45740 54 4f 5f 52 45 41 4c 20 20 20 20 20 20 20 20 20  TO_REAL         
45750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
45760 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e  45.#define TK_EN
45770 44 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20 20  D_OF_FILE       
45780 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 36               146
45790 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c 45  .#define TK_ILLE
457a0 47 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  GAL             
457b0 20 20 20 20 20 20 20 20 20 20 20 31 34 37 0a 23             147.#
457c0 64 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45 20  define TK_SPACE 
457d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
457e0 20 20 20 20 20 20 20 20 20 31 34 38 0a 23 64 65           148.#de
457f0 66 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45 44  fine TK_UNCLOSED
45800 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20  _STRING         
45810 20 20 20 20 20 20 20 31 34 39 0a 23 64 65 66 69         149.#defi
45820 6e 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20  ne TK_FUNCTION  
45830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45840 20 20 20 20 20 31 35 30 0a 23 64 65 66 69 6e 65       150.#define
45850 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20   TK_COLUMN      
45860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45870 20 20 20 31 35 31 0a 23 64 65 66 69 6e 65 20 54     151.#define T
45880 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20  K_AGG_FUNCTION  
45890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
458a0 20 31 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   152.#define TK_
458b0 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20  AGG_COLUMN      
458c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
458d0 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  53.#define TK_CO
458e0 4e 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20  NST_FUNC        
458f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 34               154
45900 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
45910 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e 68  * End of parse.h
45920 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
45930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45950 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
45960 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
45970 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
45980 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
45990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
459a0 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69  /.#include <stdi
459b0 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73  o.h>.#include <s
459c0 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64  tdlib.h>.#includ
459d0 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e  e <string.h>.#in
459e0 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e  clude <assert.h>
459f0 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 64 65  .#include <stdde
45a00 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63  f.h>../*.** If c
45a10 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70  ompiling for a p
45a20 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61  rocessor that la
45a30 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  cks floating poi
45a40 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73  nt support,.** s
45a50 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65  ubstitute intege
45a60 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70  r for floating-p
45a70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 65 66 20 53  oint.*/.#ifdef S
45a80 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
45a90 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69  ING_POINT.# defi
45aa0 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  ne double sqlite
45ab0 5f 69 6e 74 36 34 0a 23 20 64 65 66 69 6e 65 20  _int64.# define 
45ac0 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20  LONGDOUBLE_TYPE 
45ad0 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20 69  sqlite_int64.# i
45ae0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47  fndef SQLITE_BIG
45af0 5f 44 42 4c 0a 23 20 20 20 64 65 66 69 6e 65 20  _DBL.#   define 
45b00 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28  SQLITE_BIG_DBL (
45b10 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66  0x7fffffffffffff
45b20 66 66 29 0a 23 20 65 6e 64 69 66 0a 23 20 64 65  ff).# endif.# de
45b30 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fine SQLITE_OMIT
45b40 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 20  _DATETIME_FUNCS 
45b50 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  1.# define SQLIT
45b60 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 31 0a 23  E_OMIT_TRACE 1.#
45b70 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 49   undef SQLITE_MI
45b80 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54  XED_ENDIAN_64BIT
45b90 5f 46 4c 4f 41 54 0a 23 65 6e 64 69 66 0a 23 69  _FLOAT.#endif.#i
45ba0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47  fndef SQLITE_BIG
45bb0 5f 44 42 4c 0a 23 20 64 65 66 69 6e 65 20 53 51  _DBL.# define SQ
45bc0 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 31 65  LITE_BIG_DBL (1e
45bd0 39 39 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  99).#endif../*.*
45be0 2a 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73  * OMIT_TEMPDB is
45bf0 20 73 65 74 20 74 6f 20 31 20 69 66 20 53 51 4c   set to 1 if SQL
45c00 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 20  ITE_OMIT_TEMPDB 
45c10 69 73 20 64 65 66 69 6e 65 64 2c 20 6f 72 20 30  is defined, or 0
45c20 0a 2a 2a 20 61 66 74 65 72 77 61 72 64 2e 20 48  .** afterward. H
45c30 61 76 69 6e 67 20 74 68 69 73 20 6d 61 63 72 6f  aving this macro
45c40 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 63 61   allows us to ca
45c50 75 73 65 20 74 68 65 20 43 20 63 6f 6d 70 69 6c  use the C compil
45c60 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69 74 20 63  er .** to omit c
45c70 6f 64 65 20 75 73 65 64 20 62 79 20 54 45 4d 50  ode used by TEMP
45c80 20 74 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20   tables without 
45c90 6d 65 73 73 79 20 23 69 66 6e 64 65 66 20 73 74  messy #ifndef st
45ca0 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66  atements..*/.#if
45cb0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
45cc0 54 45 4d 50 44 42 0a 23 64 65 66 69 6e 65 20 4f  TEMPDB.#define O
45cd0 4d 49 54 5f 54 45 4d 50 44 42 20 31 0a 23 65 6c  MIT_TEMPDB 1.#el
45ce0 73 65 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f  se.#define OMIT_
45cf0 54 45 4d 50 44 42 20 30 0a 23 65 6e 64 69 66 0a  TEMPDB 0.#endif.
45d00 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f  ./*.** If the fo
45d10 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73  llowing macro is
45d20 20 73 65 74 20 74 6f 20 31 2c 20 74 68 65 6e 20   set to 1, then 
45d30 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20  NULL values are 
45d40 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 64 69  considered.** di
45d50 73 74 69 6e 63 74 20 77 68 65 6e 20 64 65 74 65  stinct when dete
45d60 72 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20  rmining whether 
45d70 6f 72 20 6e 6f 74 20 74 77 6f 20 65 6e 74 72 69  or not two entri
45d80 65 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 0a  es are the same.
45d90 2a 2a 20 69 6e 20 61 20 55 4e 49 51 55 45 20 69  ** in a UNIQUE i
45da0 6e 64 65 78 2e 20 20 54 68 69 73 20 69 73 20 74  ndex.  This is t
45db0 68 65 20 77 61 79 20 50 6f 73 74 67 72 65 53 51  he way PostgreSQ
45dc0 4c 2c 20 4f 72 61 63 6c 65 2c 20 44 42 32 2c 20  L, Oracle, DB2, 
45dd0 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54  MySQL,.** OCELOT
45de0 2c 20 61 6e 64 20 46 69 72 65 62 69 72 64 20 61  , and Firebird a
45df0 6c 6c 20 77 6f 72 6b 2e 20 20 54 68 65 20 53 51  ll work.  The SQ
45e00 4c 39 32 20 73 70 65 63 20 65 78 70 6c 69 63 69  L92 spec explici
45e10 74 6c 79 20 73 61 79 73 20 74 68 69 73 0a 2a 2a  tly says this.**
45e20 20 69 73 20 74 68 65 20 77 61 79 20 74 68 69 6e   is the way thin
45e30 67 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74  gs are suppose t
45e40 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  o work..**.** If
45e50 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
45e60 61 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 30  acro is set to 0
45e70 2c 20 74 68 65 20 4e 55 4c 4c 73 20 61 72 65 20  , the NULLs are 
45e80 69 6e 64 69 73 74 69 6e 63 74 20 66 6f 72 0a 2a  indistinct for.*
45e90 2a 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  * a UNIQUE index
45ea0 2e 20 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c  .  In this mode,
45eb0 20 79 6f 75 20 63 61 6e 20 6f 6e 6c 79 20 68 61   you can only ha
45ec0 76 65 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c  ve a single NULL
45ed0 20 65 6e 74 72 79 0a 2a 2a 20 66 6f 72 20 61 20   entry.** for a 
45ee0 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20  column declared 
45ef0 55 4e 49 51 55 45 2e 20 20 54 68 69 73 20 69 73  UNIQUE.  This is
45f00 20 74 68 65 20 77 61 79 20 49 6e 66 6f 72 6d 69   the way Informi
45f10 78 20 61 6e 64 20 53 51 4c 20 53 65 72 76 65 72  x and SQL Server
45f20 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65  .** work..*/.#de
45f30 66 69 6e 65 20 4e 55 4c 4c 5f 44 49 53 54 49 4e  fine NULL_DISTIN
45f40 43 54 5f 46 4f 52 5f 55 4e 49 51 55 45 20 31 0a  CT_FOR_UNIQUE 1.
45f50 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 66 69 6c 65  ./*.** The "file
45f60 20 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65 72 20   format" number 
45f70 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  is an integer th
45f80 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  at is incremente
45f90 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 74 68  d whenever.** th
45fa0 65 20 56 44 42 45 2d 6c 65 76 65 6c 20 66 69 6c  e VDBE-level fil
45fb0 65 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 73  e format changes
45fc0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
45fd0 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74   macros define t
45fe0 68 65 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c  he.** the defaul
45ff0 74 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f  t file format fo
46000 72 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20  r new databases 
46010 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  and the maximum 
46020 66 69 6c 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 74  file format.** t
46030 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  hat the library 
46040 63 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a 23 64 65  can read..*/.#de
46050 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
46060 46 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 69  FILE_FORMAT 4.#i
46070 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
46080 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  AULT_FILE_FORMAT
46090 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
460a0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f  _DEFAULT_FILE_FO
460b0 52 4d 41 54 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  RMAT 1.#endif../
460c0 2a 0a 2a 2a 20 50 72 6f 76 69 64 65 20 61 20 64  *.** Provide a d
460d0 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
460e0 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
460f0 52 45 20 69 6e 20 63 61 73 65 20 69 74 20 69 73  RE in case it is
46100 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a   not specified.*
46110 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64  * on the command
46120 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66  -line.*/.#ifndef
46130 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
46140 52 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  RE.# define SQLI
46150 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a  TE_TEMP_STORE 1.
46160 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43  #endif../*.** GC
46170 43 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e  C does not defin
46180 65 20 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29  e the offsetof()
46190 20 6d 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20   macro so we'll 
461a0 68 61 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a  have to do it.**
461b0 20 6f 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23   ourselves..*/.#
461c0 69 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a  ifndef offsetof.
461d0 23 64 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66  #define offsetof
461e0 28 53 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44  (STRUCTURE,FIELD
461f0 29 20 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29  ) ((int)((char*)
46200 26 28 28 53 54 52 55 43 54 55 52 45 2a 29 30 29  &((STRUCTURE*)0)
46210 2d 3e 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66  ->FIELD)).#endif
46220 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
46230 20 73 65 65 20 69 66 20 74 68 69 73 20 6d 61 63   see if this mac
46240 68 69 6e 65 20 75 73 65 73 20 45 42 43 44 49 43  hine uses EBCDIC
46250 2e 20 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65  .  (Yes, believe
46260 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74   it or.** not, t
46270 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d  here are still m
46280 61 63 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72  achines out ther
46290 65 20 74 68 61 74 20 75 73 65 20 45 42 43 44 49  e that use EBCDI
462a0 43 2e 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d  C.).*/.#if 'A' =
462b0 3d 20 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e  = '\301'.# defin
462c0 65 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20  e SQLITE_EBCDIC 
462d0 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  1.#else.# define
462e0 20 53 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a   SQLITE_ASCII 1.
462f0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
46300 74 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20  tegers of known 
46310 73 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79  sizes.  These ty
46320 70 65 64 65 66 73 20 6d 69 67 68 74 20 63 68 61  pedefs might cha
46330 6e 67 65 20 66 6f 72 20 61 72 63 68 69 74 65 63  nge for architec
46340 74 75 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74  tures.** where t
46350 68 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20  he sizes very.  
46360 50 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63  Preprocessor mac
46370 72 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ros are availabl
46380 65 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a  e so that the.**
46390 20 74 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f   types can be co
463a0 6e 76 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66  nveniently redef
463b0 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d  ined at compile-
463c0 74 79 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73  type.  Like this
463d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
463e0 63 63 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59  cc '-DUINTPTR_TY
463f0 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74  PE=long long int
46400 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ' ....*/.#ifndef
46410 20 55 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69   UINT32_TYPE.# i
46420 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32  fdef HAVE_UINT32
46430 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e  _T.#  define UIN
46440 54 33 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f  T32_TYPE uint32_
46450 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69  t.# else.#  defi
46460 6e 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75  ne UINT32_TYPE u
46470 6e 73 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e  nsigned int.# en
46480 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  dif.#endif.#ifnd
46490 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23  ef UINT16_TYPE.#
464a0 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54   ifdef HAVE_UINT
464b0 31 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55  16_T.#  define U
464c0 49 4e 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31  INT16_TYPE uint1
464d0 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65  6_t.# else.#  de
464e0 66 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45  fine UINT16_TYPE
464f0 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20   unsigned short 
46500 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64  int.# endif.#end
46510 69 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36  if.#ifndef INT16
46520 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41  _TYPE.# ifdef HA
46530 56 45 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65  VE_INT16_T.#  de
46540 66 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20  fine INT16_TYPE 
46550 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23  int16_t.# else.#
46560 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54    define INT16_T
46570 59 50 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20  YPE short int.# 
46580 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66  endif.#endif.#if
46590 6e 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a  ndef UINT8_TYPE.
465a0 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e  # ifdef HAVE_UIN
465b0 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55  T8_T.#  define U
465c0 49 4e 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f  INT8_TYPE uint8_
465d0 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69  t.# else.#  defi
465e0 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e  ne UINT8_TYPE un
465f0 73 69 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e  signed char.# en
46600 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  dif.#endif.#ifnd
46610 65 66 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69  ef INT8_TYPE.# i
46620 66 64 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54  fdef HAVE_INT8_T
46630 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f  .#  define INT8_
46640 54 59 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c  TYPE int8_t.# el
46650 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54  se.#  define INT
46660 38 5f 54 59 50 45 20 73 69 67 6e 65 64 20 63 68  8_TYPE signed ch
46670 61 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  ar.# endif.#endi
46680 66 0a 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f  f.#ifndef LONGDO
46690 55 42 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69  UBLE_TYPE.# defi
466a0 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59  ne LONGDOUBLE_TY
466b0 50 45 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23  PE long double.#
466c0 65 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71  endif.typedef sq
466d0 6c 69 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20  lite_int64 i64; 
466e0 20 20 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79           /* 8-by
466f0 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
46700 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c  r */.typedef sql
46710 69 74 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20  ite_uint64 u64; 
46720 20 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74          /* 8-byt
46730 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  e unsigned integ
46740 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49  er */.typedef UI
46750 4e 54 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20  NT32_TYPE u32;  
46760 20 20 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79           /* 4-by
46770 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  te unsigned inte
46780 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55  ger */.typedef U
46790 49 4e 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20  INT16_TYPE u16; 
467a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62            /* 2-b
467b0 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  yte unsigned int
467c0 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20  eger */.typedef 
467d0 49 4e 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20  INT16_TYPE i16; 
467e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d             /* 2-
467f0 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
46800 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55  ger */.typedef U
46810 49 4e 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20  INT8_TYPE u8;   
46820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62            /* 1-b
46830 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  yte unsigned int
46840 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20  eger */.typedef 
46850 49 4e 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20  INT8_TYPE i8;   
46860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d             /* 1-
46870 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
46880 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61  ger */../*.** Ma
46890 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e  cros to determin
468a0 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d 61  e whether the ma
468b0 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 20  chine is big or 
468c0 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a  little endian,.*
468d0 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 72  * evaluated at r
468e0 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65  untime..*/.#ifde
468f0 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
46900 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49  ATION.SQLITE_PRI
46910 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73  VATE const int s
46920 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23  qlite3one = 1;.#
46930 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56  else.SQLITE_PRIV
46940 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71  ATE const int sq
46950 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69 66  lite3one;.#endif
46960 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33 38  .#if defined(i38
46970 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  6) || defined(__
46980 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  i386__) || defin
46990 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20 20  ed(_M_IX86)\.   
469a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
469b0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 66            || def
469c0 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20 7c  ined(__x86_64) |
469d0 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f  | defined(__x86_
469e0 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53  64__).# define S
469f0 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
46a00 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53 51     0.# define SQ
46a10 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41  LITE_LITTLEENDIA
46a20 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  N 1.# define SQL
46a30 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
46a40 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a   SQLITE_UTF16LE.
46a50 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
46a60 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
46a70 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73     (*(char *)(&s
46a80 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a 23  qlite3one)==0).#
46a90 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c   define SQLITE_L
46aa0 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28 63  ITTLEENDIAN (*(c
46ab0 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 6f  har *)(&sqlite3o
46ac0 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e 65  ne)==1).# define
46ad0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
46ae0 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47 45  IVE (SQLITE_BIGE
46af0 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54 46  NDIAN?SQLITE_UTF
46b00 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46 31  16BE:SQLITE_UTF1
46b10 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  6LE).#endif../*.
46b20 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f 72  ** Constants for
46b30 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e 64   the largest and
46b40 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62   smallest possib
46b50 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  le 64-bit signed
46b60 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68   integers..** Th
46b70 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 64  ese macros are d
46b80 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20  esigned to work 
46b90 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f 74  correctly on bot
46ba0 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34 2d  h 32-bit and 64-
46bb0 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 73  bit.** compilers
46bc0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41 52  ..*/.#define LAR
46bd0 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78 66  GEST_INT64  (0xf
46be0 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29 30  fffffff|(((i64)0
46bf0 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 29  x7fffffff)<<32))
46c00 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 53  .#define SMALLES
46c10 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 2d  T_INT64 (((i64)-
46c20 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e 54  1) - LARGEST_INT
46c30 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75 6e  64)../* .** Roun
46c40 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 6f  d up a number to
46c50 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72   the next larger
46c60 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20   multiple of 8. 
46c70 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a   This is used.**
46c80 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 65   to force 8-byte
46c90 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 34   alignment on 64
46ca0 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 72  -bit architectur
46cb0 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  es..*/.#define R
46cc0 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28 28  OUND8(x)     (((
46cd0 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a  x)+7)&~7)../*.**
46ce0 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20 74   Round down to t
46cf0 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74 69  he nearest multi
46d00 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65 66  ple of 8.*/.#def
46d10 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28 78  ine ROUNDDOWN8(x
46d20 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a 2a  ) ((x)&~7)../*.*
46d30 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68  * Assert that th
46d40 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20 61  e pointer X is a
46d50 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62  ligned to an 8-b
46d60 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f  yte boundary..*/
46d70 0a 23 64 65 66 69 6e 65 20 45 49 47 48 54 5f 42  .#define EIGHT_B
46d80 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 29  YTE_ALIGNMENT(X)
46d90 20 20 20 28 28 28 28 63 68 61 72 2a 29 28 58 29     ((((char*)(X)
46da0 20 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29 3d   - (char*)0)&7)=
46db0 3d 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  =0)../*.** An in
46dc0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
46dd0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
46de0 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f  e is used to sto
46df0 72 65 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64  re the busy-hand
46e00 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20  ler.** callback 
46e10 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71 6c 69  for a given sqli
46e20 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a  te handle. .**.*
46e30 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62 75 73  * The sqlite.bus
46e40 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65 72 20  yHandler member 
46e50 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 73 74  of the sqlite st
46e60 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  ruct contains th
46e70 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c 62 61  e busy.** callba
46e80 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  ck for the datab
46e90 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61 63 68  ase handle. Each
46ea0 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20 76 69   pager opened vi
46eb0 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20  a the sqlite.** 
46ec0 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 65 64  handle is passed
46ed0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 73 71   a pointer to sq
46ee0 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72  lite.busyHandler
46ef0 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e 64 6c  . The busy-handl
46f00 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 69  er.** callback i
46f10 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 76 6f  s currently invo
46f20 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69  ked only from wi
46f30 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a 2a 2f  thin pager.c..*/
46f40 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
46f50 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75 73 79  BusyHandler Busy
46f60 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63 74 20  Handler;.struct 
46f70 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a 20 20  BusyHandler {.  
46f80 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 6f 69  int (*xFunc)(voi
46f90 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68  d *,int);  /* Th
46fa0 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  e busy callback 
46fb0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b  */.  void *pArg;
46fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46fd0 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20  /* First arg to 
46fe0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  busy callback */
46ff0 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20 20 20  .  int nBusy;   
47000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
47010 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74   Incremented wit
47020 68 20 65 61 63 68 20 62 75 73 79 20 63 61 6c 6c  h each busy call
47030 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61   */.};../*.** Na
47040 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  me of the master
47050 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e   database table.
47060 20 20 54 68 65 20 6d 61 73 74 65 72 20 64 61 74    The master dat
47070 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a 20 69  abase table.** i
47080 73 20 61 20 73 70 65 63 69 61 6c 20 74 61 62 6c  s a special tabl
47090 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65  e that holds the
470a0 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74 72 69   names and attri
470b0 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20  butes of all.** 
470c0 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e 64 20  user tables and 
470d0 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64 65 66  indices..*/.#def
470e0 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d 45 20  ine MASTER_NAME 
470f0 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f 6d 61        "sqlite_ma
47100 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20 54 45  ster".#define TE
47110 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20  MP_MASTER_NAME  
47120 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73  "sqlite_temp_mas
47130 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ter"../*.** The 
47140 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68 65  root-page of the
47150 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
47160 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69   table..*/.#defi
47170 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54 20 20  ne MASTER_ROOT  
47180 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68       1../*.** Th
47190 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 73 63  e name of the sc
471a0 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23  hema table..*/.#
471b0 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f 54 41  define SCHEMA_TA
471c0 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49 54 5f  BLE(x)  ((!OMIT_
471d0 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31 29 3f  TEMPDB)&&(x==1)?
471e0 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45  TEMP_MASTER_NAME
471f0 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f  :MASTER_NAME)../
47200 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69 65 6e  *.** A convenien
47210 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20 72 65  ce macro that re
47220 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
47230 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a   of elements in.
47240 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a  ** an array..*/.
47250 23 64 65 66 69 6e 65 20 41 72 72 61 79 53 69 7a  #define ArraySiz
47260 65 28 58 29 20 20 20 20 28 28 69 6e 74 29 28 73  e(X)    ((int)(s
47270 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65 6f 66 28  izeof(X)/sizeof(
47280 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54  X[0])))../*.** T
47290 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  he following val
472a0 75 65 20 61 73 20 61 20 64 65 73 74 72 75 63 74  ue as a destruct
472b0 6f 72 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20  or means to use 
472c0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 2e  sqlite3DbFree().
472d0 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69  .** This is an i
472e0 6e 74 65 72 6e 61 6c 20 65 78 74 65 6e 73 69 6f  nternal extensio
472f0 6e 20 74 6f 20 53 51 4c 49 54 45 5f 53 54 41 54  n to SQLITE_STAT
47300 49 43 20 61 6e 64 20 53 51 4c 49 54 45 5f 54 52  IC and SQLITE_TR
47310 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a 23 64 65 66  ANSIENT..*/.#def
47320 69 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  ine SQLITE_DYNAM
47330 49 43 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64  IC   ((sqlite3_d
47340 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 73  estructor_type)s
47350 71 6c 69 74 65 33 44 62 46 72 65 65 29 0a 0a 2f  qlite3DbFree)../
47360 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49 54 45  *.** When SQLITE
47370 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64 65 66  _OMIT_WSD is def
47380 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74  ined, it means t
47390 68 61 74 20 74 68 65 20 74 61 72 67 65 74 20 70  hat the target p
473a0 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0a 2a 2a 20  latform does.** 
473b0 6e 6f 74 20 73 75 70 70 6f 72 74 20 57 72 69 74  not support Writ
473c0 61 62 6c 65 20 53 74 61 74 69 63 20 44 61 74 61  able Static Data
473d0 20 28 57 53 44 29 20 73 75 63 68 20 61 73 20 67   (WSD) such as g
473e0 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61 74 69 63  lobal and static
473f0 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 20 41   variables..** A
47400 6c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 75 73  ll variables mus
47410 74 20 65 69 74 68 65 72 20 62 65 20 6f 6e 20 74  t either be on t
47420 68 65 20 73 74 61 63 6b 20 6f 72 20 64 79 6e 61  he stack or dyna
47430 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
47440 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 68 65  d from.** the he
47450 61 70 2e 20 20 57 68 65 6e 20 57 53 44 20 69 73  ap.  When WSD is
47460 20 75 6e 73 75 70 70 6f 72 74 65 64 2c 20 74 68   unsupported, th
47470 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61  e variable decla
47480 72 61 74 69 6f 6e 73 20 73 63 61 74 74 65 72 65  rations scattere
47490 64 0a 2a 2a 20 74 68 72 6f 75 67 68 6f 75 74 20  d.** throughout 
474a0 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20  the SQLite code 
474b0 6d 75 73 74 20 62 65 63 6f 6d 65 20 63 6f 6e 73  must become cons
474c0 74 61 6e 74 73 20 69 6e 73 74 65 61 64 2e 20 20  tants instead.  
474d0 54 68 65 20 53 51 4c 49 54 45 5f 57 53 44 0a 2a  The SQLITE_WSD.*
474e0 2a 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 20  * macro is used 
474f0 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 65  for this purpose
47500 2e 20 20 41 6e 64 20 69 6e 73 74 65 61 64 20 6f  .  And instead o
47510 66 20 72 65 66 65 72 65 6e 63 69 6e 67 20 74 68  f referencing th
47520 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 64 69  e variable.** di
47530 72 65 63 74 6c 79 2c 20 77 65 20 75 73 65 20 69  rectly, we use i
47540 74 73 20 63 6f 6e 73 74 61 6e 74 20 61 73 20 61  ts constant as a
47550 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74   key to lookup t
47560 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f  he run-time allo
47570 63 61 74 65 64 0a 2a 2a 20 62 75 66 66 65 72 20  cated.** buffer 
47580 74 68 61 74 20 68 6f 6c 64 73 20 72 65 61 6c 20  that holds real 
47590 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65 20 63  variable.  The c
475a0 6f 6e 73 74 61 6e 74 20 69 73 20 61 6c 73 6f 20  onstant is also 
475b0 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 0a  the initializer.
475c0 2a 2a 20 66 6f 72 20 74 68 65 20 72 75 6e 2d 74  ** for the run-t
475d0 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 75  ime allocated bu
475e0 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  ffer..**.** In t
475f0 68 65 20 75 73 75 61 6c 20 63 61 73 65 20 77 68  he usual case wh
47600 65 72 65 20 57 53 44 20 69 73 20 73 75 70 70 6f  ere WSD is suppo
47610 72 74 65 64 2c 20 74 68 65 20 53 51 4c 49 54 45  rted, the SQLITE
47620 5f 57 53 44 20 61 6e 64 20 47 4c 4f 42 41 4c 0a  _WSD and GLOBAL.
47630 2a 2a 20 6d 61 63 72 6f 73 20 62 65 63 6f 6d 65  ** macros become
47640 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 68 61 76 65   no-ops and have
47650 20 7a 65 72 6f 20 70 65 72 66 6f 72 6d 61 6e 63   zero performanc
47660 65 20 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23 69 66  e impact..*/.#if
47670 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
47680 57 53 44 0a 20 20 23 64 65 66 69 6e 65 20 53 51  WSD.  #define SQ
47690 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74 0a 20  LITE_WSD const. 
476a0 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28   #define GLOBAL(
476b0 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c 69 74  t,v) (*(t*)sqlit
476c0 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76 6f 69  e3_wsd_find((voi
476d0 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f 66 28  d*)&(v), sizeof(
476e0 76 29 29 29 0a 20 20 23 64 65 66 69 6e 65 20 73  v))).  #define s
476f0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
47700 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  ig GLOBAL(struct
47710 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 2c 20   Sqlite3Config, 
47720 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29 0a 53  sqlite3Config).S
47730 51 4c 49 54 45 5f 41 50 49 20 20 20 69 6e 74 20  QLITE_API   int 
47740 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69 74  sqlite3_wsd_init
47750 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a 29 3b 0a  (int N, int J);.
47760 53 51 4c 49 54 45 5f 41 50 49 20 20 20 76 6f 69  SQLITE_API   voi
47770 64 20 2a 73 71 6c 69 74 65 33 5f 77 73 64 5f 66  d *sqlite3_wsd_f
47780 69 6e 64 28 76 6f 69 64 20 2a 4b 2c 20 69 6e 74  ind(void *K, int
47790 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65   L);.#else.  #de
477a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20  fine SQLITE_WSD 
477b0 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41  .  #define GLOBA
477c0 4c 28 74 2c 76 29 20 76 0a 20 20 23 64 65 66 69  L(t,v) v.  #defi
477d0 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ne sqlite3Global
477e0 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f  Config sqlite3Co
477f0 6e 66 69 67 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  nfig.#endif../*.
47800 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
47810 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 64   macros are used
47820 20 74 6f 20 73 75 70 70 72 65 73 73 20 63 6f 6d   to suppress com
47830 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 61  piler warnings a
47840 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 69 74  nd to.** make it
47850 20 63 6c 65 61 72 20 74 6f 20 68 75 6d 61 6e 20   clear to human 
47860 72 65 61 64 65 72 73 20 77 68 65 6e 20 61 20 66  readers when a f
47870 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65  unction paramete
47880 72 20 69 73 20 64 65 6c 69 62 65 72 61 74 65 6c  r is deliberatel
47890 79 20 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 73 65  y .** left unuse
478a0 64 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64  d within the bod
478b0 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2e  y of a function.
478c0 20 54 68 69 73 20 75 73 75 61 6c 6c 79 20 68 61   This usually ha
478d0 70 70 65 6e 73 20 77 68 65 6e 0a 2a 2a 20 61 20  ppens when.** a 
478e0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
478f0 65 64 20 76 69 61 20 61 20 66 75 6e 63 74 69 6f  ed via a functio
47900 6e 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72 20 65  n pointer. For e
47910 78 61 6d 70 6c 65 20 74 68 65 20 0a 2a 2a 20 69  xample the .** i
47920 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
47930 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67 61 74   an SQL aggregat
47940 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63 6b 20  e step callback 
47950 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68 65 0a  may not use the.
47960 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64  ** parameter ind
47970 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75 6d 62  icating the numb
47980 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
47990 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 61 67  passed to the ag
479a0 67 72 65 67 61 74 65 2c 0a 2a 2a 20 69 66 20 69  gregate,.** if i
479b0 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 69  t knows that thi
479c0 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20 65 6c  s is enforced el
479d0 73 65 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 57  sewhere..**.** W
479e0 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70  hen a function p
479f0 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20  arameter is not 
47a00 75 73 65 64 20 61 74 20 61 6c 6c 20 77 69 74 68  used at all with
47a10 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61  in the body of a
47a20 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 69 74   function,.** it
47a30 20 69 73 20 67 65 6e 65 72 61 6c 6c 79 20 6e 61   is generally na
47a40 6d 65 64 20 22 4e 6f 74 55 73 65 64 22 20 6f 72  med "NotUsed" or
47a50 20 22 4e 6f 74 55 73 65 64 32 22 20 74 6f 20 6d   "NotUsed2" to m
47a60 61 6b 65 20 74 68 69 6e 67 73 20 65 76 65 6e 20  ake things even 
47a70 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 48 6f 77 65  clearer..** Howe
47a80 76 65 72 2c 20 74 68 65 73 65 20 6d 61 63 72 6f  ver, these macro
47a90 73 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 75 73  s may also be us
47aa0 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 77  ed to suppress w
47ab0 61 72 6e 69 6e 67 73 20 72 65 6c 61 74 65 64 20  arnings related 
47ac0 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73  to.** parameters
47ad0 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d 61 79   that may or may
47ae0 20 6e 6f 74 20 62 65 20 75 73 65 64 20 64 65 70   not be used dep
47af0 65 6e 64 69 6e 67 20 6f 6e 20 63 6f 6d 70 69 6c  ending on compil
47b00 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e 0a 2a  ation options..*
47b10 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68  * For example th
47b20 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f  ose parameters o
47b30 6e 6c 79 20 75 73 65 64 20 69 6e 20 61 73 73 65  nly used in asse
47b40 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
47b50 20 49 6e 20 74 68 65 73 65 0a 2a 2a 20 63 61 73   In these.** cas
47b60 65 73 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  es the parameter
47b70 73 20 61 72 65 20 6e 61 6d 65 64 20 61 73 20 70  s are named as p
47b80 65 72 20 74 68 65 20 75 73 75 61 6c 20 63 6f 6e  er the usual con
47b90 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65  ventions..*/.#de
47ba0 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52 41  fine UNUSED_PARA
47bb0 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64 29 28  METER(x) (void)(
47bc0 78 29 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45  x).#define UNUSE
47bd0 44 5f 50 41 52 41 4d 45 54 45 52 32 28 78 2c 79  D_PARAMETER2(x,y
47be0 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  ) UNUSED_PARAMET
47bf0 45 52 28 78 29 2c 55 4e 55 53 45 44 5f 50 41 52  ER(x),UNUSED_PAR
47c00 41 4d 45 54 45 52 28 79 29 0a 0a 2f 2a 0a 2a 2a  AMETER(y)../*.**
47c10 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e   Forward referen
47c20 63 65 73 20 74 6f 20 73 74 72 75 63 74 75 72 65  ces to structure
47c30 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  s.*/.typedef str
47c40 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67 67 49  uct AggInfo AggI
47c50 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72  nfo;.typedef str
47c60 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20  uct AuthContext 
47c70 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 74 79 70  AuthContext;.typ
47c80 65 64 65 66 20 73 74 72 75 63 74 20 42 69 74 76  edef struct Bitv
47c90 65 63 20 42 69 74 76 65 63 3b 0a 74 79 70 65 64  ec Bitvec;.typed
47ca0 65 66 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  ef struct RowSet
47cb0 20 52 6f 77 53 65 74 3b 0a 74 79 70 65 64 65 66   RowSet;.typedef
47cc0 20 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20   struct CollSeq 
47cd0 43 6f 6c 6c 53 65 71 3b 0a 74 79 70 65 64 65 66  CollSeq;.typedef
47ce0 20 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 43   struct Column C
47cf0 6f 6c 75 6d 6e 3b 0a 74 79 70 65 64 65 66 20 73  olumn;.typedef s
47d00 74 72 75 63 74 20 44 62 20 44 62 3b 0a 74 79 70  truct Db Db;.typ
47d10 65 64 65 66 20 73 74 72 75 63 74 20 53 63 68 65  edef struct Sche
47d20 6d 61 20 53 63 68 65 6d 61 3b 0a 74 79 70 65 64  ma Schema;.typed
47d30 65 66 20 73 74 72 75 63 74 20 45 78 70 72 20 45  ef struct Expr E
47d40 78 70 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  xpr;.typedef str
47d50 75 63 74 20 45 78 70 72 4c 69 73 74 20 45 78 70  uct ExprList Exp
47d60 72 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73  rList;.typedef s
47d70 74 72 75 63 74 20 46 4b 65 79 20 46 4b 65 79 3b  truct FKey FKey;
47d80 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
47d90 46 75 6e 63 44 65 66 20 46 75 6e 63 44 65 66 3b  FuncDef FuncDef;
47da0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
47db0 46 75 6e 63 44 65 66 48 61 73 68 20 46 75 6e 63  FuncDefHash Func
47dc0 44 65 66 48 61 73 68 3b 0a 74 79 70 65 64 65 66  DefHash;.typedef
47dd0 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 49   struct IdList I
47de0 64 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73  dList;.typedef s
47df0 74 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64 65  truct Index Inde
47e00 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  x;.typedef struc
47e10 74 20 4b 65 79 43 6c 61 73 73 20 4b 65 79 43 6c  t KeyClass KeyCl
47e20 61 73 73 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ass;.typedef str
47e30 75 63 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49  uct KeyInfo KeyI
47e40 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72  nfo;.typedef str
47e50 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f  uct Lookaside Lo
47e60 6f 6b 61 73 69 64 65 3b 0a 74 79 70 65 64 65 66  okaside;.typedef
47e70 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64   struct Lookasid
47e80 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 69 64 65 53  eSlot LookasideS
47e90 6c 6f 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  lot;.typedef str
47ea0 75 63 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c  uct Module Modul
47eb0 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
47ec0 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61  t NameContext Na
47ed0 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64  meContext;.typed
47ee0 65 66 20 73 74 72 75 63 74 20 50 61 72 73 65 20  ef struct Parse 
47ef0 50 61 72 73 65 3b 0a 74 79 70 65 64 65 66 20 73  Parse;.typedef s
47f00 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20  truct Savepoint 
47f10 53 61 76 65 70 6f 69 6e 74 3b 0a 74 79 70 65 64  Savepoint;.typed
47f20 65 66 20 73 74 72 75 63 74 20 53 65 6c 65 63 74  ef struct Select
47f30 20 53 65 6c 65 63 74 3b 0a 74 79 70 65 64 65 66   Select;.typedef
47f40 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 20   struct SrcList 
47f50 53 72 63 4c 69 73 74 3b 0a 74 79 70 65 64 65 66  SrcList;.typedef
47f60 20 73 74 72 75 63 74 20 53 74 72 41 63 63 75 6d   struct StrAccum
47f70 20 53 74 72 41 63 63 75 6d 3b 0a 74 79 70 65 64   StrAccum;.typed
47f80 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 20  ef struct Table 
47f90 54 61 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73  Table;.typedef s
47fa0 74 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20  truct TableLock 
47fb0 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74 79 70 65 64  TableLock;.typed
47fc0 65 66 20 73 74 72 75 63 74 20 54 6f 6b 65 6e 20  ef struct Token 
47fd0 54 6f 6b 65 6e 3b 0a 74 79 70 65 64 65 66 20 73  Token;.typedef s
47fe0 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74 61  truct TriggerSta
47ff0 63 6b 20 54 72 69 67 67 65 72 53 74 61 63 6b 3b  ck TriggerStack;
48000 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
48010 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67  TriggerStep Trig
48020 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66  gerStep;.typedef
48030 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20   struct Trigger 
48040 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66  Trigger;.typedef
48050 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64   struct Unpacked
48060 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52  Record UnpackedR
48070 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73  ecord;.typedef s
48080 74 72 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c  truct Walker Wal
48090 6b 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ker;.typedef str
480a0 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68  uct WherePlan Wh
480b0 65 72 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66  erePlan;.typedef
480c0 20 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66   struct WhereInf
480d0 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70  o WhereInfo;.typ
480e0 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
480f0 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65  eLevel WhereLeve
48100 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20  l;../*.** Defer 
48110 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20  sourcing vdbe.h 
48120 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69  and btree.h unti
48130 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 38 22  l after the "u8"
48140 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61   and .** "BusyHa
48150 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e  ndler" typedefs.
48160 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71   vdbe.h also req
48170 75 69 72 65 73 20 61 20 66 65 77 20 6f 66 20 74  uires a few of t
48180 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69  he opaque.** poi
48190 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e  nter types (i.e.
481a0 20 46 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65   FuncDef) define
481b0 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a  d above..*/./***
481c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
481d0 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74  ude btree.h in t
481e0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
481f0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
48200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
48210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
48220 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a  n file btree.h *
48230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
48260 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
48270 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
48280 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
48290 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
482a0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
482b0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
482c0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
482d0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
482e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
482f0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
48300 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
48310 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
48320 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
48330 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
48340 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
48350 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
48360 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
48370 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
48380 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
48390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
483a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
483b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
483c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
483d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
483e0 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74  r file defines t
483f0 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  he interface tha
48400 74 20 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54  t the sqlite B-T
48410 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73  ree file.** subs
48420 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d  ystem.  See comm
48430 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72  ents in the sour
48440 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65  ce code for a de
48450 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 69  tailed descripti
48460 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61  on.** of what ea
48470 63 68 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75  ch interface rou
48480 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a  tine does..**.**
48490 20 40 28 23 29 20 24 49 64 3a 20 62 74 72 65 65   @(#) $Id: btree
484a0 2e 68 2c 76 20 31 2e 31 31 34 20 32 30 30 39 2f  .h,v 1.114 2009/
484b0 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30 20 64  05/04 11:42:30 d
484c0 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
484d0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54 52  .*/.#ifndef _BTR
484e0 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42  EE_H_.#define _B
484f0 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f  TREE_H_../* TODO
48500 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74 69 6f  : This definitio
48510 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75 64  n is just includ
48520 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75  ed so other modu
48530 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a  les compile. It.
48540 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  ** needs to be r
48550 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65  evisited..*/.#de
48560 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54  fine SQLITE_N_BT
48570 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a  REE_META 10../*.
48580 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 73  ** If defined as
48590 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d   non-zero, auto-
485a0 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65  vacuum is enable
485b0 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f 74  d by default. Ot
485c0 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75  herwise.** it mu
485d0 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20  st be turned on 
485e0 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
485f0 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20  e using "PRAGMA 
48600 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22  auto_vacuum = 1"
48610 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
48620 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
48630 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e 65  VACUUM.  #define
48640 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
48650 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e  AUTOVACUUM 0.#en
48660 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54 52  dif..#define BTR
48670 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f  EE_AUTOVACUUM_NO
48680 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20 44  NE 0        /* D
48690 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61  o not do auto-va
486a0 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cuum */.#define 
486b0 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
486c0 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20 2f  _FULL 1        /
486d0 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76  * Do full auto-v
486e0 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65  acuum */.#define
486f0 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
48700 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20 20  M_INCR 2        
48710 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76  /* Incremental v
48720 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  acuum */../*.** 
48730 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
48740 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72  ions of structur
48750 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  e.*/.typedef str
48760 75 63 74 20 42 74 72 65 65 20 42 74 72 65 65 3b  uct Btree Btree;
48770 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
48780 42 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 6f  BtCursor BtCurso
48790 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
487a0 74 20 42 74 53 68 61 72 65 64 20 42 74 53 68 61  t BtShared BtSha
487b0 72 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72  red;.typedef str
487c0 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72  uct BtreeMutexAr
487d0 72 61 79 20 42 74 72 65 65 4d 75 74 65 78 41 72  ray BtreeMutexAr
487e0 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ray;../*.** This
487f0 20 73 74 72 75 63 74 75 72 65 20 72 65 63 6f 72   structure recor
48800 64 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42 74  ds all of the Bt
48810 72 65 65 73 20 74 68 61 74 20 6e 65 65 64 20 74  rees that need t
48820 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65  o hold.** a mute
48830 78 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74 65  x before we ente
48840 72 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  r sqlite3VdbeExe
48850 63 28 29 2e 20 20 54 68 65 20 42 74 72 65 65 73  c().  The Btrees
48860 20 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63   are.** are plac
48870 65 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20 69  ed in aBtree[] i
48880 6e 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72 65  n order of aBtre
48890 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74 20  e[]->pBt.  That 
488a0 77 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61  way,.** we can a
488b0 6c 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75  lways lock and u
488c0 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71  nlock them all q
488d0 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63  uickly..*/.struc
488e0 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  t BtreeMutexArra
488f0 79 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78  y {.  int nMutex
48900 3b 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72 65  ;.  Btree *aBtre
48910 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  e[SQLITE_MAX_ATT
48920 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53  ACHED+1];.};...S
48930 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
48940 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70  t sqlite3BtreeOp
48950 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  en(.  const char
48960 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f   *zFilename,   /
48970 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
48980 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20  se file to open 
48990 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
489a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
489b0 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61   Associated data
489c0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
489d0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 2c 20 20  */.  Btree **,  
489e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
489f0 20 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74 72   Return open Btr
48a00 65 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  ee* here */.  in
48a10 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
48a20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
48a30 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67  */.  int vfsFlag
48a40 73 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s             /*
48a50 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68   Flags passed th
48a60 72 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70 65  rough to VFS ope
48a70 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20  n */.);../* The 
48a80 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
48a90 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f  to sqlite3BtreeO
48aa0 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 62  pen can be the b
48ab0 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68 65  itwise or of the
48ac0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  .** following va
48ad0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45  lues..**.** NOTE
48ae0 3a 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20  :  These values 
48af0 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63  must match the c
48b00 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41 47  orresponding PAG
48b10 45 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a  ER_ values in.**
48b20 20 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65   pager.h..*/.#de
48b30 66 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f  fine BTREE_OMIT_
48b40 4a 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44  JOURNAL  1  /* D
48b50 6f 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61  o not use journa
48b60 6c 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20  l.  No argument 
48b70 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45  */.#define BTREE
48b80 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32  _NO_READLOCK   2
48b90 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f    /* Omit readlo
48ba0 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20  cks on readonly 
48bb0 66 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  files */.#define
48bc0 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20   BTREE_MEMORY   
48bd0 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65       4  /* In-me
48be0 6d 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72 67  mory DB.  No arg
48bf0 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  ument */.#define
48c00 20 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20   BTREE_READONLY 
48c10 20 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20       8  /* Open 
48c20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20  the database in 
48c30 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a  read-only mode *
48c40 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
48c50 52 45 41 44 57 52 49 54 45 20 20 20 20 31 36 20  READWRITE    16 
48c60 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74   /* Open for bot
48c70 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72  h reading and wr
48c80 69 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65  iting */.#define
48c90 20 42 54 52 45 45 5f 43 52 45 41 54 45 20 20 20   BTREE_CREATE   
48ca0 20 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61 74      32  /* Creat
48cb0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  e the database i
48cc0 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78  f it does not ex
48cd0 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50  ist */..SQLITE_P
48ce0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
48cf0 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 72  e3BtreeClose(Btr
48d00 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
48d10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
48d20 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
48d30 65 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53  e(Btree*,int);.S
48d40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
48d50 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
48d60 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72  tSafetyLevel(Btr
48d70 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  ee*,int,int);.SQ
48d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
48d90 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e   sqlite3BtreeSyn
48da0 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 2a  cDisabled(Btree*
48db0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
48dc0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
48dd0 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 74  eeSetPageSize(Bt
48de0 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ree*,int,int,int
48df0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
48e00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
48e10 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 42 74  eeGetPageSize(Bt
48e20 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
48e30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
48e40 33 42 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75  3BtreeMaxPageCou
48e50 6e 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a  nt(Btree*,int);.
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 47  nt sqlite3BtreeG
48e80 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 2a  etReserve(Btree*
48e90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
48ea0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
48eb0 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeSetAutoVacuum(
48ec0 42 74 72 65 65 20 2a 2c 20 69 6e 74 29 3b 0a 53  Btree *, int);.S
48ed0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
48ee0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
48ef0 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65  tAutoVacuum(Btre
48f00 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
48f10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
48f20 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28  BtreeBeginTrans(
48f30 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Btree*,int);.SQL
48f40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
48f50 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
48f60 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 65 65  itPhaseOne(Btree
48f70 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
48f80 4d 61 73 74 65 72 29 3b 0a 53 51 4c 49 54 45 5f  Master);.SQLITE_
48f90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
48fa0 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
48fb0 61 73 65 54 77 6f 28 42 74 72 65 65 2a 29 3b 0a  aseTwo(Btree*);.
48fc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
48fd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
48fe0 6f 6d 6d 69 74 28 42 74 72 65 65 2a 29 3b 0a 53  ommit(Btree*);.S
48ff0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
49000 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f  t sqlite3BtreeRo
49010 6c 6c 62 61 63 6b 28 42 74 72 65 65 2a 29 3b 0a  llback(Btree*);.
49020 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
49030 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42  nt sqlite3BtreeB
49040 65 67 69 6e 53 74 6d 74 28 42 74 72 65 65 2a 2c  eginStmt(Btree*,
49050 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
49060 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
49070 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65  BtreeCreateTable
49080 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a 2c 20 69  (Btree*, int*, i
49090 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51 4c 49 54  nt flags);.SQLIT
490a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
490b0 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
490c0 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  ans(Btree*);.SQL
490d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
490e0 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
490f0 52 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 2a  ReadTrans(Btree*
49100 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
49110 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
49120 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 74 72  eeIsInBackup(Btr
49130 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
49140 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
49150 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74  e3BtreeSchema(Bt
49160 72 65 65 20 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  ree *, int, void
49170 28 2a 29 28 76 6f 69 64 20 2a 29 29 3b 0a 53 51  (*)(void *));.SQ
49180 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
49190 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
491a0 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20  emaLocked(Btree 
491b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
491c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
491d0 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72  reeLockTable(Btr
491e0 65 65 20 2a 2c 20 69 6e 74 2c 20 75 38 29 3b 0a  ee *, int, u8);.
491f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
49200 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
49210 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 2a  avepoint(Btree *
49220 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51  , int, int);..SQ
49230 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
49240 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
49250 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
49260 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54  (Btree *);.SQLIT
49270 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
49280 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72  char *sqlite3Btr
49290 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65  eeGetJournalname
492a0 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54  (Btree *);.SQLIT
492b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
492c0 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69  lite3BtreeCopyFi
492d0 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65  le(Btree *, Btre
492e0 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  e *);..SQLITE_PR
492f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
49300 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d  3BtreeIncrVacuum
49310 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54  (Btree *);../* T
49320 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
49330 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  er to sqlite3Btr
49340 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 61  eeCreateTable ca
49350 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73 65  n be the bitwise
49360 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f   OR.** of the fo
49370 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a  llowing flags:.*
49380 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
49390 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 20  INTKEY     1    
493a0 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c  /* Table has onl
493b0 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  y 64-bit signed 
493c0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a  integer keys */.
493d0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45  #define BTREE_ZE
493e0 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a  RODATA   2    /*
493f0 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20   Table has keys 
49400 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a  only - no data *
49410 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
49420 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 20 20  LEAFDATA   4    
49430 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 69  /* Data stored i
49440 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20  n leaves only.  
49450 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a  Implies INTKEY *
49460 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
49470 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
49480 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65  eeDropTable(Btre
49490 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a  e*, int, int*);.
494a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
494b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
494c0 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a  learTable(Btree*
494d0 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51  , int, int*);.SQ
494e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
494f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
49500 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74  Meta(Btree*, int
49510 20 69 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75   idx, u32 *pValu
49520 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
49530 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
49540 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74  reeUpdateMeta(Bt
49550 72 65 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75  ree*, int idx, u
49560 33 32 20 76 61 6c 75 65 29 3b 0a 53 51 4c 49 54  32 value);.SQLIT
49570 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
49580 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41  qlite3BtreeTripA
49590 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65 2a  llCursors(Btree*
495a0 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
495b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
495c0 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a  te3BtreeCursor(.
495d0 20 20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20    Btree*,       
495e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
495f0 20 20 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20         /* BTree 
49600 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65  containing table
49610 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e   to open */.  in
49620 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20  t iTable,       
49630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49640 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72     /* Index of r
49650 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  oot page */.  in
49660 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20  t wrFlag,       
49670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49680 20 20 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74     /* 1 for writ
49690 69 6e 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64  ing.  0 for read
496a0 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63  -only */.  struc
496b0 74 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20  t KeyInfo*,     
496c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
496d0 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
496e0 74 20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e  t to compare fun
496f0 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72  ction */.  BtCur
49700 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20  sor *pCursor    
49710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49720 2f 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74  /* Space to writ
49730 65 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75  e cursor structu
49740 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  re */.);.SQLITE_
49750 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
49760 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69  te3BtreeCursorSi
49770 7a 65 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54  ze(void);..SQLIT
49780 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
49790 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43  lite3BtreeCloseC
497a0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29  ursor(BtCursor*)
497b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
497c0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
497d0 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72  eMoveto(.  BtCur
497e0 73 6f 72 2a 2c 0a 20 20 63 6f 6e 73 74 20 76 6f  sor*,.  const vo
497f0 69 64 20 2a 70 4b 65 79 2c 0a 20 20 69 36 34 20  id *pKey,.  i64 
49800 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73  nKey,.  int bias
49810 2c 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b  ,.  int *pRes.);
49820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
49830 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
49840 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a  MovetoUnpacked(.
49850 20 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55    BtCursor*,.  U
49860 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70  npackedRecord *p
49870 55 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74  UnKey,.  i64 int
49880 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c  Key,.  int bias,
49890 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a  .  int *pRes.);.
498a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
498b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
498c0 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74  ursorHasMoved(Bt
498d0 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a  Cursor*, int*);.
498e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
498f0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
49900 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29  elete(BtCursor*)
49910 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
49920 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
49930 65 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72  eInsert(BtCursor
49940 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  *, const void *p
49950 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20  Key, i64 nKey,. 
49960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49980 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61   const void *pDa
49990 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20  ta, int nData,. 
499a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
499b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
499c0 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20   int nZero, int 
499d0 62 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 65  bias, int seekRe
499e0 73 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sult);.SQLITE_PR
499f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
49a00 33 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75  3BtreeFirst(BtCu
49a10 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73  rsor*, int *pRes
49a20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
49a30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
49a40 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a  eeLast(BtCursor*
49a50 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51  , int *pRes);.SQ
49a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
49a70 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
49a80 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  t(BtCursor*, int
49a90 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f   *pRes);.SQLITE_
49aa0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
49ab0 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75  te3BtreeEof(BtCu
49ac0 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
49ad0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
49ae0 65 33 42 74 72 65 65 46 6c 61 67 73 28 42 74 43  e3BtreeFlags(BtC
49af0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ursor*);.SQLITE_
49b00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
49b10 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
49b20 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20  (BtCursor*, int 
49b30 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50  *pRes);.SQLITE_P
49b40 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
49b50 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42  e3BtreeKeySize(B
49b60 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70  tCursor*, i64 *p
49b70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Size);.SQLITE_PR
49b80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
49b90 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73  3BtreeKey(BtCurs
49ba0 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c  or*, u32 offset,
49bb0 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29   u32 amt, void*)
49bc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
49bd0 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
49be0 33 42 74 72 65 65 43 75 72 73 6f 72 44 62 28 63  3BtreeCursorDb(c
49bf0 6f 6e 73 74 20 42 74 43 75 72 73 6f 72 2a 29 3b  onst BtCursor*);
49c00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
49c10 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
49c20 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68  te3BtreeKeyFetch
49c30 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20  (BtCursor*, int 
49c40 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *pAmt);.SQLITE_P
49c50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69  RIVATE const voi
49c60 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44  d *sqlite3BtreeD
49c70 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f  ataFetch(BtCurso
49c80 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a  r*, int *pAmt);.
49c90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
49ca0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
49cb0 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72  ataSize(BtCursor
49cc0 2a 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a  *, u32 *pSize);.
49cd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
49ce0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
49cf0 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75  ata(BtCursor*, u
49d00 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61  32 offset, u32 a
49d10 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  mt, void*);.SQLI
49d20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
49d30 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
49d40 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72  achedRowid(BtCur
49d50 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  sor*, sqlite3_in
49d60 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t64);.SQLITE_PRI
49d70 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74  VATE sqlite3_int
49d80 36 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  64 sqlite3BtreeG
49d90 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74  etCachedRowid(Bt
49da0 43 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54  Cursor*);..SQLIT
49db0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
49dc0 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65  sqlite3BtreeInte
49dd0 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65  grityCheck(Btree
49de0 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69  *, int *aRoot, i
49df0 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69  nt nRoot, int, i
49e00 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
49e10 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65  VATE struct Page
49e20 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50  r *sqlite3BtreeP
49e30 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53  ager(Btree*);..S
49e40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
49e50 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75  t sqlite3BtreePu
49e60 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c  tData(BtCursor*,
49e70 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
49e80 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51   amt, void*);.SQ
49e90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
49ea0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61  d sqlite3BtreeCa
49eb0 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75  cheOverflow(BtCu
49ec0 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  rsor *);.SQLITE_
49ed0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
49ee0 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75  ite3BtreeClearCu
49ef0 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29  rsor(BtCursor *)
49f00 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
49f10 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e  E_OMIT_BTREECOUN
49f20 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
49f30 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
49f40 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20  eCount(BtCursor 
49f50 2a 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64 69  *, i64 *);.#endi
49f60 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
49f70 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49  _TEST.SQLITE_PRI
49f80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
49f90 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 28  BtreeCursorInfo(
49fa0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c  BtCursor*, int*,
49fb0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
49fc0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
49fd0 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 73  e3BtreeCursorLis
49fe0 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69  t(Btree*);.#endi
49ff0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61  f../*.** If we a
4a000 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 61  re not using sha
4a010 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e 20  red cache, then 
4a020 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
4a030 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78   to.** use mutex
4a040 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  es to access the
4a050 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74   BtShared struct
4a060 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74  ures.  So make t
4a070 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20  he.** Enter and 
4a080 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73  Leave procedures
4a090 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e   no-ops..*/.#ifn
4a0a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
4a0b0 53 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c  SHARED_CACHE.SQL
4a0c0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4a0d0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
4a0e0 6e 74 65 72 28 42 74 72 65 65 2a 29 3b 0a 53 51  nter(Btree*);.SQ
4a0f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4a100 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4a110 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33  EnterAll(sqlite3
4a120 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  *);.#else.# defi
4a130 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  ne sqlite3BtreeE
4a140 6e 74 65 72 28 58 29 20 0a 23 20 64 65 66 69 6e  nter(X) .# defin
4a150 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  e sqlite3BtreeEn
4a160 74 65 72 41 6c 6c 28 58 29 0a 23 65 6e 64 69 66  terAll(X).#endif
4a170 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
4a180 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
4a190 44 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c 49  D_CACHE) && SQLI
4a1a0 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53 51  TE_THREADSAFE.SQ
4a1b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4a1c0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4a1d0 4c 65 61 76 65 28 42 74 72 65 65 2a 29 3b 0a 53  Leave(Btree*);.S
4a1e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4a1f0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
4a200 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43  eEnterCursor(BtC
4a210 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ursor*);.SQLITE_
4a220 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
4a230 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
4a240 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a  Cursor(BtCursor*
4a250 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4a260 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
4a270 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71  BtreeLeaveAll(sq
4a280 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
4a290 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
4a2a0 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
4a2b0 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 65  ArrayEnter(Btree
4a2c0 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51  MutexArray*);.SQ
4a2d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4a2e0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4a2f0 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28  MutexArrayLeave(
4a300 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a  BtreeMutexArray*
4a310 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4a320 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
4a330 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49  BtreeMutexArrayI
4a340 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78  nsert(BtreeMutex
4a350 41 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29 3b  Array*, Btree*);
4a360 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
4a370 20 20 2f 2a 20 54 68 65 73 65 20 72 6f 75 74 69    /* These routi
4a380 6e 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 73  nes are used ins
4a390 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
4a3a0 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f  tements only. */
4a3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4a3c0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72    int sqlite3Btr
4a3d0 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72  eeHoldsMutex(Btr
4a3e0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4a3f0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
4a400 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
4a410 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a 29  utexes(sqlite3*)
4a420 3b 0a 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0a  ;.#endif.#else..
4a430 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
4a440 42 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 20  BtreeLeave(X).# 
4a450 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
4a460 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58  reeEnterCursor(X
4a470 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
4a480 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
4a490 6f 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  or(X).# define s
4a4a0 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
4a4b0 41 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20  All(X).# define 
4a4c0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
4a4d0 78 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23  xArrayEnter(X).#
4a4e0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4a4f0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
4a500 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20  ave(X).# define 
4a510 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
4a520 78 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59  xArrayInsert(X,Y
4a530 29 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  )..# define sqli
4a540 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
4a550 65 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e 65  ex(X) 1.# define
4a560 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
4a570 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20  dsAllMutexes(X) 
4a580 31 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69  1.#endif...#endi
4a590 66 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a  f /* _BTREE_H_ *
4a5a0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
4a5b0 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e  ** End of btree.
4a5c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4a5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a5f0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4a600 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
4a610 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
4a620 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
4a630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a640 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4a650 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e  ** Include vdbe.
4a660 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
4a670 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
4a680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a690 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4a6a0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
4a6b0 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.h ***********
4a6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a6e0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
4a6f0 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
4a700 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
4a710 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
4a720 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
4a730 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
4a740 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
4a750 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
4a760 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
4a770 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
4a780 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
4a790 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
4a7a0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
4a7b0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
4a7c0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
4a7d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
4a7e0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
4a7f0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
4a800 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
4a810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4a850 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61  *********.** Hea
4a860 64 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65  der file for the
4a870 20 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 73   Virtual DataBas
4a880 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a  e Engine (VDBE).
4a890 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
4a8a0 72 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e  r defines the in
4a8b0 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76  terface to the v
4a8c0 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20  irtual database 
4a8d0 65 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42  engine.** or VDB
4a8e0 45 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d 70  E.  The VDBE imp
4a8f0 6c 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72  lements an abstr
4a900 61 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74  act machine that
4a910 20 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c   runs a.** simpl
4a920 65 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63  e program to acc
4a930 65 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74  ess and modify t
4a940 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61  he underlying da
4a950 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  tabase..**.** $I
4a960 64 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 34  d: vdbe.h,v 1.14
4a970 31 20 32 30 30 39 2f 30 34 2f 31 30 20 30 30 3a  1 2009/04/10 00:
4a980 35 36 3a 32 39 20 64 72 68 20 45 78 70 20 24 0a  56:29 drh Exp $.
4a990 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49  */.#ifndef _SQLI
4a9a0 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69  TE_VDBE_H_.#defi
4a9b0 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f  ne _SQLITE_VDBE_
4a9c0 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67  H_../*.** A sing
4a9d0 6c 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70  le VDBE is an op
4a9e0 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e  aque structure n
4a9f0 61 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e  amed "Vdbe".  On
4aa00 6c 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69  ly routines.** i
4aa10 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c  n the source fil
4aa20 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61  e sqliteVdbe.c a
4aa30 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65  re allowed to se
4aa40 65 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a  e the insides.**
4aa50 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
4aa60 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  re..*/.typedef s
4aa70 74 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b  truct Vdbe Vdbe;
4aa80 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  ../*.** The name
4aa90 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s of the followi
4aaa0 6e 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65  ng types declare
4aab0 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61  d in vdbeInt.h a
4aac0 72 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66  re required.** f
4aad0 6f 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65  or the VdbeOp de
4aae0 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  finition..*/.typ
4aaf0 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
4ab00 46 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74  Func VdbeFunc;.t
4ab10 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65  ypedef struct Me
4ab20 6d 20 4d 65 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20  m Mem;../*.** A 
4ab30 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69  single instructi
4ab40 6f 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  on of the virtua
4ab50 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e  l machine has an
4ab60 20 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61   opcode.** and a
4ab70 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20  s many as three 
4ab80 6f 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69  operands.  The i
4ab90 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65  nstruction is re
4aba0 63 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20  corded.** as an 
4abb0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
4abc0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
4abd0 75 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56  ure:.*/.struct V
4abe0 64 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63  dbeOp {.  u8 opc
4abf0 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ode;          /*
4ac00 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20   What operation 
4ac10 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20  to perform */.  
4ac20 73 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79  signed char p4ty
4ac30 70 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68  pe; /* One of th
4ac40 65 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e  e P4_xxx constan
4ac50 74 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75  ts for p4 */.  u
4ac60 38 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20  8 opflags;      
4ac70 20 20 20 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e     /* Not curren
4ac80 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 75 38  tly used */.  u8
4ac90 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20   p5;            
4aca0 20 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d    /* Fifth param
4acb0 65 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67  eter is an unsig
4acc0 6e 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f  ned character */
4acd0 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20  .  int p1;      
4ace0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
4acf0 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74  operand */.  int
4ad00 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20   p2;            
4ad10 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d   /* Second param
4ad20 65 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20  eter (often the 
4ad30 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e  jump destination
4ad40 29 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20  ) */.  int p3;  
4ad50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
4ad60 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
4ad70 72 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20  r */.  union {  
4ad80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f             /* fo
4ad90 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  rth parameter */
4ada0 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
4adb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
4adc0 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20  nteger value if 
4add0 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32  p4type==P4_INT32
4ade0 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b   */.    void *p;
4adf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4ae00 2a 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65  * Generic pointe
4ae10 72 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  r */.    char *z
4ae20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4ae30 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61  /* Pointer to da
4ae40 74 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63  ta for string (c
4ae50 68 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73  har array) types
4ae60 20 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36   */.    i64 *pI6
4ae70 34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  4;             /
4ae80 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
4ae90 70 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a  pe is P4_INT64 *
4aea0 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52  /.    double *pR
4aeb0 65 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  eal;         /* 
4aec0 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65  Used when p4type
4aed0 20 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20   is P4_REAL */. 
4aee0 20 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e     FuncDef *pFun
4aef0 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  c;        /* Use
4af00 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4af10 20 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20   P4_FUNCDEF */. 
4af20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64     VdbeFunc *pVd
4af30 62 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65  beFunc;   /* Use
4af40 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4af50 20 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a   P4_VDBEFUNC */.
4af60 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
4af70 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ll;        /* Us
4af80 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4af90 73 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a  s P4_COLLSEQ */.
4afa0 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20      Mem *pMem;  
4afb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
4afc0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4afd0 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20  s P4_MEM */.    
4afe0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
4aff0 74 61 62 3b 20 20 20 2f 2a 20 55 73 65 64 20 77  tab;   /* Used w
4b000 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34  hen p4type is P4
4b010 5f 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79  _VTAB */.    Key
4b020 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
4b030 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
4b040 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45   p4type is P4_KE
4b050 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74  YINFO */.    int
4b060 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20   *ai;           
4b070 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
4b080 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e   p4type is P4_IN
4b090 54 41 52 52 41 59 20 2a 2f 0a 20 20 7d 20 70 34  TARRAY */.  } p4
4b0a0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
4b0b0 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a 43  DEBUG.  char *zC
4b0c0 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20  omment;         
4b0d0 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69   /* Comment to i
4b0e0 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c 69  mprove readabili
4b0f0 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  ty */.#endif.#if
4b100 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45  def VDBE_PROFILE
4b110 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20  .  int cnt;     
4b120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
4b130 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74  umber of times t
4b140 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
4b150 77 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f 0a  was executed */.
4b160 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 20    u64 cycles;   
4b170 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
4b180 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 65  tal time spent e
4b190 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e  xecuting this in
4b1a0 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e  struction */.#en
4b1b0 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73  dif.};.typedef s
4b1c0 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62  truct VdbeOp Vdb
4b1d0 65 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d  eOp;../*.** A sm
4b1e0 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66  aller version of
4b1f0 20 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72   VdbeOp used for
4b200 20 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69   the VdbeAddOpLi
4b210 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65  st() function be
4b220 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65  cause.** it take
4b230 73 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e  s up less space.
4b240 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f  .*/.struct VdbeO
4b250 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63  pList {.  u8 opc
4b260 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ode;          /*
4b270 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20   What operation 
4b280 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20  to perform */.  
4b290 73 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20  signed char p1; 
4b2a0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65      /* First ope
4b2b0 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 64  rand */.  signed
4b2c0 20 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f 2a   char p2;     /*
4b2d0 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   Second paramete
4b2e0 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d  r (often the jum
4b2f0 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a  p destination) *
4b300 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20  /.  signed char 
4b310 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 64  p3;     /* Third
4b320 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b   parameter */.};
4b330 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b340 56 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f  VdbeOpList VdbeO
4b350 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c  pList;../*.** Al
4b360 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20  lowed values of 
4b370 56 64 62 65 4f 70 2e 70 33 74 79 70 65 0a 2a 2f  VdbeOp.p3type.*/
4b380 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 55  .#define P4_NOTU
4b390 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68  SED    0   /* Th
4b3a0 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69  e P4 parameter i
4b3b0 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64  s not used */.#d
4b3c0 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 43  efine P4_DYNAMIC
4b3d0 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74    (-1)  /* Point
4b3e0 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f  er to a string o
4b3f0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
4b400 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23  iteMalloc() */.#
4b410 64 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 43  define P4_STATIC
4b420 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e     (-2)  /* Poin
4b430 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20  ter to a static 
4b440 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
4b450 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 2d  e P4_COLLSEQ  (-
4b460 34 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70  4)  /* P4 is a p
4b470 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c  ointer to a Coll
4b480 53 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f  Seq structure */
4b490 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e 43  .#define P4_FUNC
4b4a0 44 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 34  DEF  (-5)  /* P4
4b4b0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
4b4c0 20 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63   a FuncDef struc
4b4d0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4b4e0 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29  P4_KEYINFO  (-6)
4b4f0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
4b500 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66  nter to a KeyInf
4b510 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23  o structure */.#
4b520 64 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 55  define P4_VDBEFU
4b530 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 69  NC (-7)  /* P4 i
4b540 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
4b550 20 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74   VdbeFunc struct
4b560 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
4b570 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20  4_MEM      (-8) 
4b580 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
4b590 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20  ter to a Mem*   
4b5a0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
4b5b0 65 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 45  efine P4_TRANSIE
4b5c0 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 73  NT (-9) /* P4 is
4b5d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4b5e0 74 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67  transient string
4b5f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56   */.#define P4_V
4b600 54 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a  TAB     (-10) /*
4b610 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
4b620 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76   to an sqlite3_v
4b630 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f  tab structure */
4b640 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 49  .#define P4_MPRI
4b650 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 34  NTF  (-11) /* P4
4b660 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74   is a string obt
4b670 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
4b680 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a  e3_mprintf() */.
4b690 23 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c 20  #define P4_REAL 
4b6a0 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 20      (-12) /* P4 
4b6b0 69 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61  is a 64-bit floa
4b6c0 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
4b6d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49   */.#define P4_I
4b6e0 4e 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a  NT64    (-13) /*
4b6f0 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20   P4 is a 64-bit 
4b700 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
4b710 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54  /.#define P4_INT
4b720 33 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50  32    (-14) /* P
4b730 34 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69  4 is a 32-bit si
4b740 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
4b750 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52  #define P4_INTAR
4b760 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20  RAY (-15) /* P4 
4b770 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33  is a vector of 3
4b780 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a  2-bit integers *
4b790 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e  /../* When addin
4b7a0 67 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20  g a P4 argument 
4b7b0 75 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f  using P4_KEYINFO
4b7c0 2c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  , a copy of the 
4b7d0 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
4b7e0 65 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54  e.** is made.  T
4b7f0 68 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65  hat copy is free
4b800 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
4b810 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42  is finalized.  B
4b820 75 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67  ut if the.** arg
4b830 75 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49  ument is P4_KEYI
4b840 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65  NFO_HANDOFF, the
4b850 20 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74   passed in point
4b860 65 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 20  er is used.  It 
4b870 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72  still.** gets fr
4b880 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62  eed when the Vdb
4b890 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73  e is finalized s
4b8a0 6f 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c  o it still shoul
4b8b0 64 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  d be obtained.**
4b8c0 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73   from a single s
4b8d0 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
4b8e0 42 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d  But no copy is m
4b8f0 61 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c  ade and the call
4b900 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ing.** function 
4b910 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79  should *not* try
4b920 20 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79   to free the Key
4b930 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  Info..*/.#define
4b940 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
4b950 4f 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e  OFF (-16).#defin
4b960 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41  e P4_KEYINFO_STA
4b970 54 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a  TIC  (-17)../*.*
4b980 2a 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e  * The Vdbe.aColN
4b990 61 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69  ame array contai
4b9a0 6e 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74  ns 5n Mem struct
4b9b0 75 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73  ures, where n is
4b9c0 20 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20   the .** number 
4b9d0 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61  of columns of da
4b9e0 74 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ta returned by t
4b9f0 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  he statement..*/
4ba00 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
4ba10 5f 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66  _NAME     0.#def
4ba20 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c  ine COLNAME_DECL
4ba30 54 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43  TYPE 1.#define C
4ba40 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20  OLNAME_DATABASE 
4ba50 32 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  2.#define COLNAM
4ba60 45 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65  E_TABLE    3.#de
4ba70 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c  fine COLNAME_COL
4ba80 55 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53  UMN   4.#ifdef S
4ba90 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
4baa0 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64  UMN_METADATA.# d
4bab0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
4bac0 20 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a         5      /*
4bad0 20 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41   Number of COLNA
4bae0 4d 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a  ME_xxx symbols *
4baf0 2f 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20  /.#else.# ifdef 
4bb00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
4bb10 54 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20  TYPE.#   define 
4bb20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31  COLNAME_N      1
4bb30 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f        /* Store o
4bb40 6e 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a  nly the name */.
4bb50 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e  # else.#   defin
4bb60 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20  e COLNAME_N     
4bb70 20 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65   2      /* Store
4bb80 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65   the name and de
4bb90 63 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69  cltype */.# endi
4bba0 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
4bbb0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  The following ma
4bbc0 63 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72  cro converts a r
4bbd0 65 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 20  elative address 
4bbe0 69 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a  in the p2 field.
4bbf0 2a 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73  ** of a VdbeOp s
4bc00 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20  tructure into a 
4bc10 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20  negative number 
4bc20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69  so that .** sqli
4bc30 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
4bc40 28 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68  () knows that th
4bc50 65 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c  e address is rel
4bc60 61 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a  ative.  Calling.
4bc70 2a 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61  ** the macro aga
4bc80 69 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20  in restores the 
4bc90 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66  address..*/.#def
4bca0 69 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d 31  ine ADDR(X)  (-1
4bcb0 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  -(X))../*.** The
4bcc0 20 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20   makefile scans 
4bcd0 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63  the vdbe.c sourc
4bce0 65 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74  e file and creat
4bcf0 65 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e  es the "opcodes.
4bd00 68 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c  h".** header fil
4bd10 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 61  e that defines a
4bd20 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68   number for each
4bd30 20 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 20   opcode used by 
4bd40 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a  the VDBE..*/./**
4bd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
4bd60 6c 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69  lude opcodes.h i
4bd70 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
4bd80 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vdbe.h *********
4bd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
4bda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
4bdb0 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e  in file opcodes.
4bdc0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4bdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
4bdf0 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  Automatically ge
4be00 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74  nerated.  Do not
4be10 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20   edit */./* See 
4be20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77  the mkopcodeh.aw
4be30 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74  k script for det
4be40 61 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ails */.#define 
4be50 4f 50 5f 56 4e 65 78 74 20 20 20 20 20 20 20 20  OP_VNext        
4be60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4be70 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
4be80 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20  e OP_Affinity   
4be90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bea0 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66            2.#def
4beb0 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20  ine OP_Column   
4bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bed0 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64              3.#d
4bee0 65 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f 6f 6b  efine OP_SetCook
4bef0 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ie              
4bf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a                4.
4bf10 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20  #define OP_Seek 
4bf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bf40 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61  5.#define OP_Rea
4bf50 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
4bf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bf70 31 33 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73  130   /* same as
4bf80 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 2a 2f 0a   TK_FLOAT    */.
4bf90 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 71 75 65  #define OP_Seque
4bfa0 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20  nce             
4bfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bfc0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 61 76  6.#define OP_Sav
4bfd0 65 70 6f 69 6e 74 20 20 20 20 20 20 20 20 20 20  epoint          
4bfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4bff0 20 20 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47    7.#define OP_G
4c000 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
4c010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c020 20 20 20 37 38 20 20 20 2f 2a 20 73 61 6d 65 20     78   /* same 
4c030 61 73 20 54 4b 5f 47 45 20 20 20 20 20 20 20 2a  as TK_GE       *
4c040 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77  /.#define OP_Row
4c050 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 20 20  Key             
4c060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c070 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53    8.#define OP_S
4c080 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 20 20  Copy            
4c090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c0a0 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50      9.#define OP
4c0b0 5f 45 71 20 20 20 20 20 20 20 20 20 20 20 20 20  _Eq             
4c0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c0d0 20 20 20 20 20 37 34 20 20 20 2f 2a 20 73 61 6d       74   /* sam
4c0e0 65 20 61 73 20 54 4b 5f 45 51 20 20 20 20 20 20  e as TK_EQ      
4c0f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f   */.#define OP_O
4c100 70 65 6e 57 72 69 74 65 20 20 20 20 20 20 20 20  penWrite        
4c110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c120 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 4f 50     10.#define OP
4c130 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 20 20 20 20  _NotNull        
4c140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c150 20 20 20 20 20 37 32 20 20 20 2f 2a 20 73 61 6d       72   /* sam
4c160 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20  e as TK_NOTNULL 
4c170 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   */.#define OP_I
4c180 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f               
4c190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c1a0 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 4f 50     11.#define OP
4c1b0 5f 54 6f 49 6e 74 20 20 20 20 20 20 20 20 20 20  _ToInt          
4c1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c1d0 20 20 20 20 31 34 34 20 20 20 2f 2a 20 73 61 6d      144   /* sam
4c1e0 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20  e as TK_TO_INT  
4c1f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   */.#define OP_S
4c200 74 72 69 6e 67 38 20 20 20 20 20 20 20 20 20 20  tring8          
4c210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c220 20 20 20 39 34 20 20 20 2f 2a 20 73 61 6d 65 20     94   /* same 
4c230 61 73 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a  as TK_STRING   *
4c240 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c  /.#define OP_Col
4c250 6c 53 65 71 20 20 20 20 20 20 20 20 20 20 20 20  lSeq            
4c260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c270 20 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f   12.#define OP_O
4c280 70 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 20  penRead         
4c290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c2a0 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20 4f 50     13.#define OP
4c2b0 5f 45 78 70 69 72 65 20 20 20 20 20 20 20 20 20  _Expire         
4c2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c2d0 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20       14.#define 
4c2e0 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20  OP_AutoCommit   
4c2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c300 20 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e         15.#defin
4c310 65 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20  e OP_Gt         
4c320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c330 20 20 20 20 20 20 20 20 20 37 35 20 20 20 2f 2a           75   /*
4c340 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20   same as TK_GT  
4c350 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4c360 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20  OP_Pagecount    
4c370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c380 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e         16.#defin
4c390 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b  e OP_IntegrityCk
4c3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c3b0 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 66           17.#def
4c3c0 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 20 20  ine OP_Sort     
4c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c3e0 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23 64             18.#d
4c3f0 65 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20  efine OP_Copy   
4c400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c410 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a               20.
4c420 23 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 63 65  #define OP_Trace
4c430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
4c450 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e  1.#define OP_Fun
4c460 63 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  ction           
4c470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c480 20 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   22.#define OP_I
4c490 66 4e 65 67 20 20 20 20 20 20 20 20 20 20 20 20  fNeg            
4c4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c4b0 20 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50     23.#define OP
4c4c0 5f 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20  _And            
4c4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c4e0 20 20 20 20 20 36 37 20 20 20 2f 2a 20 73 61 6d       67   /* sam
4c4f0 65 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 20 20  e as TK_AND     
4c500 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   */.#define OP_S
4c510 75 62 74 72 61 63 74 20 20 20 20 20 20 20 20 20  ubtract         
4c520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c530 20 20 20 38 35 20 20 20 2f 2a 20 73 61 6d 65 20     85   /* same 
4c540 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 2a  as TK_MINUS    *
4c550 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f  /.#define OP_Noo
4c560 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
4c570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c580 20 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52   24.#define OP_R
4c590 65 74 75 72 6e 20 20 20 20 20 20 20 20 20 20 20  eturn           
4c5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c5b0 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 4f 50     25.#define OP
4c5c0 5f 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20 20  _Remainder      
4c5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c5e0 20 20 20 20 20 38 38 20 20 20 2f 2a 20 73 61 6d       88   /* sam
4c5f0 65 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20 20  e as TK_REM     
4c600 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e   */.#define OP_N
4c610 65 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 20  ewRowid         
4c620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c630 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50     26.#define OP
4c640 5f 4d 75 6c 74 69 70 6c 79 20 20 20 20 20 20 20  _Multiply       
4c650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c660 20 20 20 20 20 38 36 20 20 20 2f 2a 20 73 61 6d       86   /* sam
4c670 65 20 61 73 20 54 4b 5f 53 54 41 52 20 20 20 20  e as TK_STAR    
4c680 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56   */.#define OP_V
4c690 61 72 69 61 62 6c 65 20 20 20 20 20 20 20 20 20  ariable         
4c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c6b0 20 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 50     27.#define OP
4c6c0 5f 53 74 72 69 6e 67 20 20 20 20 20 20 20 20 20  _String         
4c6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c6e0 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20       28.#define 
4c6f0 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20  OP_RealAffinity 
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 32 39 0a 23 64 65 66 69 6e         29.#defin
4c720 65 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20 20 20  e OP_VRename    
4c730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c740 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66           30.#def
4c750 69 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65  ine OP_ParseSche
4c760 6d 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ma              
4c770 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64             31.#d
4c780 65 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20  efine OP_VOpen  
4c790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a               32.
4c7b0 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65  #define OP_Close
4c7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
4c7e0 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65  3.#define OP_Cre
4c7f0 61 74 65 49 6e 64 65 78 20 20 20 20 20 20 20 20  ateIndex        
4c800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c810 20 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49   34.#define OP_I
4c820 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 20  sUnique         
4c830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c840 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50     35.#define OP
4c850 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 20 20  _NotFound       
4c860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c870 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20       36.#define 
4c880 4f 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20  OP_Int64        
4c890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c8a0 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e         37.#defin
4c8b0 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20 20  e OP_MustBeInt  
4c8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c8d0 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 66           38.#def
4c8e0 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20 20 20 20  ine OP_Halt     
4c8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c900 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64             39.#d
4c910 65 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20  efine OP_Rowid  
4c920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c930 20 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a               40.
4c940 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54  #define OP_IdxLT
4c950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
4c970 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64  1.#define OP_Add
4c980 49 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  Imm             
4c990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c9a0 20 34 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   42.#define OP_S
4c9b0 74 61 74 65 6d 65 6e 74 20 20 20 20 20 20 20 20  tatement        
4c9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4c9d0 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 4f 50     43.#define OP
4c9e0 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 20  _RowData        
4c9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ca00 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20       44.#define 
4ca10 4f 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20 20  OP_MemMax       
4ca20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ca30 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e         45.#defin
4ca40 65 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20  e OP_Or         
4ca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ca60 20 20 20 20 20 20 20 20 20 36 36 20 20 20 2f 2a           66   /*
4ca70 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20   same as TK_OR  
4ca80 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4ca90 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20  OP_NotExists    
4caa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cab0 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e         46.#defin
4cac0 65 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20  e OP_Gosub      
4cad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cae0 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66           47.#def
4caf0 69 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20  ine OP_Divide   
4cb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb10 20 20 20 20 20 20 20 20 20 20 20 38 37 20 20 20             87   
4cb20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c  /* same as TK_SL
4cb30 41 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  ASH    */.#defin
4cb40 65 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20  e OP_Integer    
4cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb60 20 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66           48.#def
4cb70 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63  ine OP_ToNumeric
4cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb90 20 20 20 20 20 20 20 20 20 20 31 34 33 20 20 20            143   
4cba0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
4cbb0 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69  _NUMERIC*/.#defi
4cbc0 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20  ne OP_Prev      
4cbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cbe0 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65            49.#de
4cbf0 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 52 65  fine OP_RowSetRe
4cc00 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ad              
4cc10 20 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23              50.#
4cc20 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e 63 61 74  define OP_Concat
4cc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 39                89
4cc50 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4cc60 5f 43 4f 4e 43 41 54 20 20 20 2a 2f 0a 23 64 65  _CONCAT   */.#de
4cc70 66 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 41 64  fine OP_RowSetAd
4cc80 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
4cc90 20 20 20 20 20 20 20 20 20 20 20 20 35 31 0a 23              51.#
4cca0 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64  define OP_BitAnd
4ccb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ccc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30                80
4ccd0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4cce0 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65  _BITAND   */.#de
4ccf0 66 69 6e 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20  fine OP_VColumn 
4cd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cd10 20 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23              52.#
4cd20 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65  define OP_Create
4cd30 54 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  Table           
4cd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33                53
4cd50 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 61 73 74  .#define OP_Last
4cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cd80 35 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65  54.#define OP_Se
4cd90 65 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20 20  ekLe            
4cda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cdb0 20 20 35 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f    55.#define OP_
4cdc0 49 73 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  IsNull          
4cdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cde0 20 20 20 20 37 31 20 20 20 2f 2a 20 73 61 6d 65      71   /* same
4cdf0 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20   as TK_ISNULL   
4ce00 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e  */.#define OP_In
4ce10 63 72 56 61 63 75 75 6d 20 20 20 20 20 20 20 20  crVacuum        
4ce20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce30 20 20 35 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f    56.#define OP_
4ce40 49 64 78 52 6f 77 69 64 20 20 20 20 20 20 20 20  IdxRowid        
4ce50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce60 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20 4f      57.#define O
4ce70 50 5f 53 68 69 66 74 52 69 67 68 74 20 20 20 20  P_ShiftRight    
4ce80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce90 20 20 20 20 20 20 38 33 20 20 20 2f 2a 20 73 61        83   /* sa
4cea0 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 20  me as TK_RSHIFT 
4ceb0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4cec0 52 65 73 65 74 43 6f 75 6e 74 20 20 20 20 20 20  ResetCount      
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cee0 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f      58.#define O
4cef0 50 5f 43 6f 6e 74 65 78 74 50 75 73 68 20 20 20  P_ContextPush   
4cf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cf10 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65        59.#define
4cf20 20 4f 50 5f 59 69 65 6c 64 20 20 20 20 20 20 20   OP_Yield       
4cf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cf40 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69          60.#defi
4cf50 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65  ne OP_DropTrigge
4cf60 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
4cf70 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65            61.#de
4cf80 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e 64 65  fine OP_DropInde
4cf90 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
4cfa0 20 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23              62.#
4cfb0 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 47 45 20  define OP_IdxGE 
4cfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33                63
4cfe0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 44  .#define OP_IdxD
4cff0 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20  elete           
4d000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d010 36 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 61  64.#define OP_Va
4d020 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20 20  cuum            
4d030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d040 20 20 36 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f    65.#define OP_
4d050 49 66 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20  IfNot           
4d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d070 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20 4f      68.#define O
4d080 50 5f 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20  P_DropTable     
4d090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d0a0 20 20 20 20 20 20 36 39 0a 23 64 65 66 69 6e 65        69.#define
4d0b0 20 4f 50 5f 53 65 65 6b 4c 74 20 20 20 20 20 20   OP_SeekLt      
4d0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d0d0 20 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69          70.#defi
4d0e0 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  ne OP_MakeRecord
4d0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d100 20 20 20 20 20 20 20 20 20 20 37 39 0a 23 64 65            79.#de
4d110 66 69 6e 65 20 4f 50 5f 54 6f 42 6c 6f 62 20 20  fine OP_ToBlob  
4d120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d130 20 20 20 20 20 20 20 20 20 20 20 31 34 32 20 20             142  
4d140 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
4d150 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 64 65 66 69  O_BLOB  */.#defi
4d160 6e 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20  ne OP_ResultRow 
4d170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d180 20 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 65            90.#de
4d190 66 69 6e 65 20 4f 50 5f 44 65 6c 65 74 65 20 20  fine OP_Delete  
4d1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d1b0 20 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23              91.#
4d1c0 64 65 66 69 6e 65 20 4f 50 5f 41 67 67 46 69 6e  define OP_AggFin
4d1d0 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  al              
4d1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 32                92
4d1f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6d 70  .#define OP_Comp
4d200 61 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20  are             
4d210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d220 39 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68  95.#define OP_Sh
4d230 69 66 74 4c 65 66 74 20 20 20 20 20 20 20 20 20  iftLeft         
4d240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d250 20 20 38 32 20 20 20 2f 2a 20 73 61 6d 65 20 61    82   /* same a
4d260 73 20 54 4b 5f 4c 53 48 49 46 54 20 20 20 2a 2f  s TK_LSHIFT   */
4d270 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 74 6f  .#define OP_Goto
4d280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d2a0 39 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 61  96.#define OP_Ta
4d2b0 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20 20 20  bleLock         
4d2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d2d0 20 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f    97.#define OP_
4d2e0 43 6c 65 61 72 20 20 20 20 20 20 20 20 20 20 20  Clear           
4d2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d300 20 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f      98.#define O
4d310 50 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20 20  P_Le            
4d320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d330 20 20 20 20 20 20 37 36 20 20 20 2f 2a 20 73 61        76   /* sa
4d340 6d 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20 20  me as TK_LE     
4d350 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4d360 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20  VerifyCookie    
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d380 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 4f      99.#define O
4d390 50 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20  P_AggStep       
4d3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d3b0 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65       100.#define
4d3c0 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20   OP_ToText      
4d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d3e0 20 20 20 20 20 20 20 31 34 31 20 20 20 2f 2a 20         141   /* 
4d3f0 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45  same as TK_TO_TE
4d400 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  XT  */.#define O
4d410 50 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20  P_Not           
4d420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d430 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20 73 61        19   /* sa
4d440 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20  me as TK_NOT    
4d450 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4d460 54 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20  ToReal          
4d470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d480 20 20 20 31 34 35 20 20 20 2f 2a 20 73 61 6d 65     145   /* same
4d490 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20   as TK_TO_REAL  
4d4a0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65  */.#define OP_Se
4d4b0 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20 20 20 20 20  tNumColumns     
4d4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d4d0 20 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f   101.#define OP_
4d4e0 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20  Transaction     
4d4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d500 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f     102.#define O
4d510 50 5f 56 46 69 6c 74 65 72 20 20 20 20 20 20 20  P_VFilter       
4d520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d530 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65       103.#define
4d540 20 4f 50 5f 4e 65 20 20 20 20 20 20 20 20 20 20   OP_Ne          
4d550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d560 20 20 20 20 20 20 20 20 37 33 20 20 20 2f 2a 20          73   /* 
4d570 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20 20 20  same as TK_NE   
4d580 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
4d590 50 5f 56 44 65 73 74 72 6f 79 20 20 20 20 20 20  P_VDestroy      
4d5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d5b0 20 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65       104.#define
4d5c0 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 20 20   OP_ContextPop  
4d5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d5e0 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69         105.#defi
4d5f0 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20  ne OP_BitOr     
4d600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d610 20 20 20 20 20 20 20 20 20 20 38 31 20 20 20 2f            81   /
4d620 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
4d630 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  OR    */.#define
4d640 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 20 20   OP_Next        
4d650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d660 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69         106.#defi
4d670 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20 20 20  ne OP_Count     
4d680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d690 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65           107.#de
4d6a0 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 65 72  fine OP_IdxInser
4d6b0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4d6c0 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23             108.#
4d6d0 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20  define OP_Lt    
4d6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37                77
4d700 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4d710 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _LT       */.#de
4d720 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20  fine OP_SeekGe  
4d730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d740 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23             109.#
4d750 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74  define OP_Insert
4d760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d770 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30               110
4d780 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74  .#define OP_Dest
4d790 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20  roy             
4d7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4d7b0 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65  11.#define OP_Re
4d7c0 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20  adCookie        
4d7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d7e0 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f   112.#define OP_
4d7f0 52 6f 77 53 65 74 54 65 73 74 20 20 20 20 20 20  RowSetTest      
4d800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d810 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 4f     113.#define O
4d820 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 20  P_LoadAnalysis  
4d830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d840 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65       114.#define
4d850 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20 20 20   OP_Explain     
4d860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d870 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69         115.#defi
4d880 6e 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c  ne OP_HaltIfNull
4d890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d8a0 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65           116.#de
4d8b0 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75  fine OP_OpenPseu
4d8c0 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  do              
4d8d0 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23             117.#
4d8e0 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45 70  define OP_OpenEp
4d8f0 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20 20  hemeral         
4d900 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38               118
4d910 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 6c 6c  .#define OP_Null
4d920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4d940 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f  19.#define OP_Mo
4d950 76 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ve              
4d960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d970 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f   120.#define OP_
4d980 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  Blob            
4d990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d9a0 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 4f     121.#define O
4d9b0 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 20 20  P_Add           
4d9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d9d0 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 73 61        84   /* sa
4d9e0 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 20 20  me as TK_PLUS   
4d9f0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
4da00 52 65 77 69 6e 64 20 20 20 20 20 20 20 20 20 20  Rewind          
4da10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4da20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f     122.#define O
4da30 50 5f 53 65 65 6b 47 74 20 20 20 20 20 20 20 20  P_SeekGt        
4da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4da50 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65       123.#define
4da60 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 20 20 20   OP_VBegin      
4da70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4da80 20 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69         124.#defi
4da90 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20 20 20  ne OP_VUpdate   
4daa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dab0 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65           125.#de
4dac0 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f 20 20  fine OP_IfZero  
4dad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dae0 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a 23             126.#
4daf0 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4e 6f 74  define OP_BitNot
4db00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4db10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 33                93
4db20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
4db30 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a 23 64 65  _BITNOT   */.#de
4db40 66 69 6e 65 20 4f 50 5f 56 43 72 65 61 74 65 20  fine OP_VCreate 
4db50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4db60 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23             127.#
4db70 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 6e 64 20  define OP_Found 
4db80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4db90 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38               128
4dba0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 50 6f  .#define OP_IfPo
4dbb0 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
4dbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4dbd0 32 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75  29.#define OP_Nu
4dbe0 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20 20 20  llRow           
4dbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dc00 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f   131.#define OP_
4dc10 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20 20 20  Jump            
4dc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dc30 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 4f     132.#define O
4dc40 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 20 20  P_Permutation   
4dc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dc60 20 20 20 20 20 31 33 33 0a 0a 2f 2a 20 54 68 65       133../* The
4dc70 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64   following opcod
4dc80 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 76  e values are nev
4dc90 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69  er used */.#defi
4dca0 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33  ne OP_NotUsed_13
4dcb0 34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  4               
4dcc0 20 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65           134.#de
4dcd0 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
4dce0 31 33 35 20 20 20 20 20 20 20 20 20 20 20 20 20  135             
4dcf0 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23             135.#
4dd00 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65  define OP_NotUse
4dd10 64 5f 31 33 36 20 20 20 20 20 20 20 20 20 20 20  d_136           
4dd20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36               136
4dd30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
4dd40 73 65 64 5f 31 33 37 20 20 20 20 20 20 20 20 20  sed_137         
4dd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4dd60 33 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  37.#define OP_No
4dd70 74 55 73 65 64 5f 31 33 38 20 20 20 20 20 20 20  tUsed_138       
4dd80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4dd90 20 31 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   138.#define OP_
4dda0 4e 6f 74 55 73 65 64 5f 31 33 39 20 20 20 20 20  NotUsed_139     
4ddb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ddc0 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f     139.#define O
4ddd0 50 5f 4e 6f 74 55 73 65 64 5f 31 34 30 20 20 20  P_NotUsed_140   
4dde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ddf0 20 20 20 20 20 31 34 30 0a 0a 0a 2f 2a 20 50 72       140.../* Pr
4de00 6f 70 65 72 74 69 65 73 20 73 75 63 68 20 61 73  operties such as
4de10 20 22 6f 75 74 32 22 20 6f 72 20 22 6a 75 6d 70   "out2" or "jump
4de20 22 20 74 68 61 74 20 61 72 65 20 73 70 65 63 69  " that are speci
4de30 66 69 65 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65  fied in.** comme
4de40 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  nts following th
4de50 65 20 22 63 61 73 65 22 20 66 6f 72 20 65 61 63  e "case" for eac
4de60 68 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20  h opcode in the 
4de70 76 64 62 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e  vdbe.c.** are en
4de80 63 6f 64 65 64 20 69 6e 74 6f 20 62 69 74 76 65  coded into bitve
4de90 63 74 6f 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73  ctors as follows
4dea0 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  :.*/.#define OPF
4deb0 4c 47 5f 4a 55 4d 50 20 20 20 20 20 20 20 20 20  LG_JUMP         
4dec0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75     0x0001  /* ju
4ded0 6d 70 3a 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d  mp:  P2 holds jm
4dee0 70 20 74 61 72 67 65 74 20 2a 2f 0a 23 64 65 66  p target */.#def
4def0 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50  ine OPFLG_OUT2_P
4df00 52 45 52 45 4c 45 41 53 45 20 30 78 30 30 30 32  RERELEASE 0x0002
4df10 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
4df20 65 61 73 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65  ease: */.#define
4df30 20 4f 50 46 4c 47 5f 49 4e 31 20 20 20 20 20 20   OPFLG_IN1      
4df40 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f         0x0004  /
4df50 2a 20 69 6e 31 3a 20 20 20 50 31 20 69 73 20 61  * in1:   P1 is a
4df60 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69  n input */.#defi
4df70 6e 65 20 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20  ne OPFLG_IN2    
4df80 20 20 20 20 20 20 20 20 20 30 78 30 30 30 38 20           0x0008 
4df90 20 2f 2a 20 69 6e 32 3a 20 20 20 50 32 20 69 73   /* in2:   P2 is
4dfa0 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65   an input */.#de
4dfb0 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20  fine OPFLG_IN3  
4dfc0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 31             0x001
4dfd0 30 20 20 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20  0  /* in3:   P3 
4dfe0 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23  is an input */.#
4dff0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54  define OPFLG_OUT
4e000 33 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30  3            0x0
4e010 30 32 30 20 20 2f 2a 20 6f 75 74 33 3a 20 20 50  020  /* out3:  P
4e020 33 20 69 73 20 61 6e 20 6f 75 74 70 75 74 20 2a  3 is an output *
4e030 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
4e040 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f  INITIALIZER {\./
4e050 2a 20 20 20 30 20 2a 2f 20 30 78 30 30 2c 20 30  *   0 */ 0x00, 0
4e060 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x01, 0x00, 0x00,
4e070 20 30 78 31 30 2c 20 30 78 30 38 2c 20 30 78 30   0x10, 0x08, 0x0
4e080 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 20 38  2, 0x00,\./*   8
4e090 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 34 2c 20   */ 0x00, 0x04, 
4e0a0 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 30  0x00, 0x05, 0x00
4e0b0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
4e0c0 30 30 2c 5c 0a 2f 2a 20 20 31 36 20 2a 2f 20 30  00,\./*  16 */ 0
4e0d0 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 31 2c  x02, 0x00, 0x01,
4e0e0 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78 30   0x04, 0x04, 0x0
4e0f0 30 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 5c 0a  0, 0x00, 0x05,\.
4e100 2f 2a 20 20 32 34 20 2a 2f 20 30 78 30 30 2c 20  /*  24 */ 0x00, 
4e110 30 78 30 34 2c 20 30 78 30 32 2c 20 30 78 30 30  0x04, 0x02, 0x00
4e120 2c 20 30 78 30 32 2c 20 30 78 30 34 2c 20 30 78  , 0x02, 0x04, 0x
4e130 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 33  00, 0x00,\./*  3
4e140 32 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c  2 */ 0x00, 0x00,
4e150 20 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 31   0x02, 0x11, 0x1
4e160 31 2c 20 30 78 30 32 2c 20 30 78 30 35 2c 20 30  1, 0x02, 0x05, 0
4e170 78 30 30 2c 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20  x00,\./*  40 */ 
4e180 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 30 34  0x02, 0x11, 0x04
4e190 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
4e1a0 30 63 2c 20 30 78 31 31 2c 20 30 78 30 31 2c 5c  0c, 0x11, 0x01,\
4e1b0 0a 2f 2a 20 20 34 38 20 2a 2f 20 30 78 30 32 2c  ./*  48 */ 0x02,
4e1c0 20 30 78 30 31 2c 20 30 78 32 31 2c 20 30 78 30   0x01, 0x21, 0x0
4e1d0 38 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30  8, 0x00, 0x02, 0
4e1e0 78 30 31 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 20  x01, 0x11,\./*  
4e1f0 35 36 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 32  56 */ 0x01, 0x02
4e200 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
4e210 30 34 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  04, 0x00, 0x00, 
4e220 30 78 31 31 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f  0x11,\./*  64 */
4e230 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 32   0x00, 0x00, 0x2
4e240 63 2c 20 30 78 32 63 2c 20 30 78 30 35 2c 20 30  c, 0x2c, 0x05, 0
4e250 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 35 2c  x00, 0x11, 0x05,
4e260 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20 30 78 30 35  \./*  72 */ 0x05
4e270 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78  , 0x15, 0x15, 0x
4e280 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20  15, 0x15, 0x15, 
4e290 30 78 31 35 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20  0x15, 0x00,\./* 
4e2a0 20 38 30 20 2a 2f 20 30 78 32 63 2c 20 30 78 32   80 */ 0x2c, 0x2
4e2b0 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  c, 0x2c, 0x2c, 0
4e2c0 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c  x2c, 0x2c, 0x2c,
4e2d0 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 38 38 20 2a   0x2c,\./*  88 *
4e2e0 2f 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78  / 0x2c, 0x2c, 0x
4e2f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
4e300 30 78 30 34 2c 20 30 78 30 32 2c 20 30 78 30 30  0x04, 0x02, 0x00
4e310 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f 20 30 78 30  ,\./*  96 */ 0x0
4e320 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  1, 0x00, 0x00, 0
4e330 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
4e340 20 30 78 30 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a   0x00, 0x01,\./*
4e350 20 31 30 34 20 2a 2f 20 30 78 30 30 2c 20 30 78   104 */ 0x00, 0x
4e360 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  00, 0x01, 0x02, 
4e370 30 78 30 38 2c 20 30 78 31 31 2c 20 30 78 30 30  0x08, 0x11, 0x00
4e380 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 31 32 20  , 0x02,\./* 112 
4e390 2a 2f 20 30 78 30 32 2c 20 30 78 31 35 2c 20 30  */ 0x02, 0x15, 0
4e3a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 31 30 2c  x00, 0x00, 0x10,
4e3b0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
4e3c0 32 2c 5c 0a 2f 2a 20 31 32 30 20 2a 2f 20 30 78  2,\./* 120 */ 0x
4e3d0 30 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 20  00, 0x02, 0x01, 
4e3e0 30 78 31 31 2c 20 30 78 30 30 2c 20 30 78 30 30  0x11, 0x00, 0x00
4e3f0 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 5c 0a 2f  , 0x05, 0x00,\./
4e400 2a 20 31 32 38 20 2a 2f 20 30 78 31 31 2c 20 30  * 128 */ 0x11, 0
4e410 78 30 35 2c 20 30 78 30 32 2c 20 30 78 30 30 2c  x05, 0x02, 0x00,
4e420 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30   0x01, 0x00, 0x0
4e430 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 33 36  0, 0x00,\./* 136
4e440 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20   */ 0x00, 0x00, 
4e450 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
4e460 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78  , 0x04, 0x04, 0x
4e470 30 34 2c 5c 0a 2f 2a 20 31 34 34 20 2a 2f 20 30  04,\./* 144 */ 0
4e480 78 30 34 2c 20 30 78 30 34 2c 7d 0a 0a 2f 2a 2a  x04, 0x04,}../**
4e490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
4e4a0 20 6f 66 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a   of opcodes.h **
4e4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
4e4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
4e4f0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
4e500 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62   left off in vdb
4e510 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
4e520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
4e530 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 65 73 20 66  .** Prototypes f
4e540 6f 72 20 74 68 65 20 56 44 42 45 20 69 6e 74 65  or the VDBE inte
4e550 72 66 61 63 65 2e 20 20 53 65 65 20 63 6f 6d 6d  rface.  See comm
4e560 65 6e 74 73 20 6f 6e 20 74 68 65 20 69 6d 70 6c  ents on the impl
4e570 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 6f  ementation.** fo
4e580 72 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20  r a description 
4e590 6f 66 20 77 68 61 74 20 65 61 63 68 20 6f 66 20  of what each of 
4e5a0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64  these routines d
4e5b0 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  oes..*/.SQLITE_P
4e5c0 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c  RIVATE Vdbe *sql
4e5d0 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 73  ite3VdbeCreate(s
4e5e0 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
4e5f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4e600 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 56  ite3VdbeAddOp0(V
4e610 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  dbe*,int);.SQLIT
4e620 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4e630 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
4e640 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  Vdbe*,int,int);.
4e650 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4e660 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
4e670 64 4f 70 32 28 56 64 62 65 2a 2c 69 6e 74 2c 69  dOp2(Vdbe*,int,i
4e680 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
4e690 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4e6a0 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64  te3VdbeAddOp3(Vd
4e6b0 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c  be*,int,int,int,
4e6c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4e6d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4e6e0 56 64 62 65 41 64 64 4f 70 34 28 56 64 62 65 2a  VdbeAddOp4(Vdbe*
4e6f0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
4e700 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34  ,const char *zP4
4e710 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
4e720 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4e730 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56  3VdbeAddOpList(V
4e740 64 62 65 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56  dbe*, int nOp, V
4e750 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20  dbeOpList const 
4e760 2a 61 4f 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52  *aOp);.SQLITE_PR
4e770 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4e780 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56  e3VdbeChangeP1(V
4e790 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20  dbe*, int addr, 
4e7a0 69 6e 74 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f  int P1);.SQLITE_
4e7b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4e7c0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
4e7d0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
4e7e0 2c 20 69 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54  , int P2);.SQLIT
4e7f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4e800 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
4e810 50 33 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  P3(Vdbe*, int ad
4e820 64 72 2c 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c  dr, int P3);.SQL
4e830 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4e840 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
4e850 67 65 50 35 28 56 64 62 65 2a 2c 20 75 38 20 50  geP5(Vdbe*, u8 P
4e860 35 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  5);.SQLITE_PRIVA
4e870 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
4e880 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65  dbeJumpHere(Vdbe
4e890 2a 2c 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51  *, int addr);.SQ
4e8a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4e8b0 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
4e8c0 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c  ngeToNoop(Vdbe*,
4e8d0 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e   int addr, int N
4e8e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4e8f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
4e900 62 65 43 68 61 6e 67 65 50 34 28 56 64 62 65 2a  beChangeP4(Vdbe*
4e910 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73  , int addr, cons
4e920 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74  t char *zP4, int
4e930 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   N);.SQLITE_PRIV
4e940 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4e950 56 64 62 65 55 73 65 73 42 74 72 65 65 28 56 64  VdbeUsesBtree(Vd
4e960 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  be*, int);.SQLIT
4e970 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70  E_PRIVATE VdbeOp
4e980 20 2a 73 71 6c 69 74 65 33 56 64 62 65 47 65 74   *sqlite3VdbeGet
4e990 4f 70 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a  Op(Vdbe*, int);.
4e9a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4e9b0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61  nt sqlite3VdbeMa
4e9c0 6b 65 4c 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a  keLabel(Vdbe*);.
4e9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4e9e0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
4e9f0 65 6c 65 74 65 28 56 64 62 65 2a 29 3b 0a 53 51  elete(Vdbe*);.SQ
4ea00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4ea10 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  d sqlite3VdbeMak
4ea20 65 52 65 61 64 79 28 56 64 62 65 2a 2c 69 6e 74  eReady(Vdbe*,int
4ea30 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
4ea40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4ea50 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e  t sqlite3VdbeFin
4ea60 61 6c 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51  alize(Vdbe*);.SQ
4ea70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4ea80 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73  d sqlite3VdbeRes
4ea90 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c  olveLabel(Vdbe*,
4eaa0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
4eab0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4eac0 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
4ead0 28 56 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20  (Vdbe*);.#ifdef 
4eae0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
4eaf0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4eb00 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54 72  id sqlite3VdbeTr
4eb10 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45 2a 29  ace(Vdbe*,FILE*)
4eb20 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
4eb30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4eb40 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65  ite3VdbeResetSte
4eb50 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29 3b 0a  pResult(Vdbe*);.
4eb60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4eb70 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  nt sqlite3VdbeRe
4eb80 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  set(Vdbe*);.SQLI
4eb90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4eba0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
4ebb0 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 6e 74 29  mCols(Vdbe*,int)
4ebc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4ebd0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
4ebe0 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62 65 2a  SetColName(Vdbe*
4ebf0 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e 73  , int, int, cons
4ec00 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 64 28 2a  t char *, void(*
4ec10 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
4ec20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4ec30 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
4ec40 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b 0a 53  hanges(Vdbe*);.S
4ec50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
4ec60 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64  lite3 *sqlite3Vd
4ec70 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53 51 4c  beDb(Vdbe*);.SQL
4ec80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4ec90 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53   sqlite3VdbeSetS
4eca0 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20  ql(Vdbe*, const 
4ecb0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20  char *z, int n, 
4ecc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4ecd0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4ece0 33 56 64 62 65 53 77 61 70 28 56 64 62 65 2a 2c  3VdbeSwap(Vdbe*,
4ecf0 56 64 62 65 2a 29 3b 0a 0a 23 69 66 64 65 66 20  Vdbe*);..#ifdef 
4ed00 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
4ed10 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
4ed20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4ed30 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  nt sqlite3VdbeRe
4ed40 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 29  leaseMemory(int)
4ed50 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
4ed60 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 64  PRIVATE Unpacked
4ed70 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 56  Record *sqlite3V
4ed80 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28  dbeRecordUnpack(
4ed90 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 2c 63 6f 6e  KeyInfo*,int,con
4eda0 73 74 20 76 6f 69 64 2a 2c 63 68 61 72 2a 2c 69  st void*,char*,i
4edb0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
4edc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4edd0 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b  VdbeDeleteUnpack
4ede0 65 64 52 65 63 6f 72 64 28 55 6e 70 61 63 6b 65  edRecord(Unpacke
4edf0 64 52 65 63 6f 72 64 2a 29 3b 0a 53 51 4c 49 54  dRecord*);.SQLIT
4ee00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4ee10 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
4ee20 6f 6d 70 61 72 65 28 69 6e 74 2c 63 6f 6e 73 74  ompare(int,const
4ee30 20 76 6f 69 64 2a 2c 55 6e 70 61 63 6b 65 64 52   void*,UnpackedR
4ee40 65 63 6f 72 64 2a 29 3b 0a 0a 0a 23 69 66 6e 64  ecord*);...#ifnd
4ee50 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45  ef NDEBUG.SQLITE
4ee60 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4ee70 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65  sqlite3VdbeComme
4ee80 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20  nt(Vdbe*, const 
4ee90 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64  char*, ...);.# d
4eea0 65 66 69 6e 65 20 56 64 62 65 43 6f 6d 6d 65 6e  efine VdbeCommen
4eeb0 74 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62  t(X)  sqlite3Vdb
4eec0 65 43 6f 6d 6d 65 6e 74 20 58 0a 53 51 4c 49 54  eComment X.SQLIT
4eed0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
4eee0 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70   sqlite3VdbeNoop
4eef0 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63  Comment(Vdbe*, c
4ef00 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
4ef10 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e  ;.# define VdbeN
4ef20 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73  oopComment(X)  s
4ef30 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f  qlite3VdbeNoopCo
4ef40 6d 6d 65 6e 74 20 58 0a 23 65 6c 73 65 0a 23 20  mment X.#else.# 
4ef50 64 65 66 69 6e 65 20 56 64 62 65 43 6f 6d 6d 65  define VdbeComme
4ef60 6e 74 28 58 29 0a 23 20 64 65 66 69 6e 65 20 56  nt(X).# define V
4ef70 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58  dbeNoopComment(X
4ef80 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ).#endif..#endif
4ef90 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
4efa0 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 68 20  * End of vdbe.h 
4efb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4efc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4efd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4efe0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4eff0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
4f000 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
4f010 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
4f020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f030 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4f040 2a 20 49 6e 63 6c 75 64 65 20 70 61 67 65 72 2e  * Include pager.
4f050 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
4f060 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
4f070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f080 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4f090 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67  * Begin file pag
4f0a0 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
4f0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f0d0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
4f0e0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
4f0f0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
4f100 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
4f110 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
4f120 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
4f130 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
4f140 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
4f150 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
4f160 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
4f170 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
4f180 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
4f190 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
4f1a0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
4f1b0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
4f1c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
4f1d0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
4f1e0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
4f1f0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
4f200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f240 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
4f250 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66   header file def
4f260 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  ines the interfa
4f270 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ce that the sqli
4f280 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a  te page cache.**
4f290 20 73 75 62 73 79 73 74 65 6d 2e 20 20 54 68 65   subsystem.  The
4f2a0 20 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73   page cache subs
4f2b0 79 73 74 65 6d 20 72 65 61 64 73 20 61 6e 64 20  ystem reads and 
4f2c0 77 72 69 74 65 73 20 61 20 66 69 6c 65 20 61 20  writes a file a 
4f2d0 70 61 67 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d  page.** at a tim
4f2e0 65 20 61 6e 64 20 70 72 6f 76 69 64 65 73 20 61  e and provides a
4f2f0 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 6c   journal for rol
4f300 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  lback..**.** @(#
4f310 29 20 24 49 64 3a 20 70 61 67 65 72 2e 68 2c 76  ) $Id: pager.h,v
4f320 20 31 2e 31 30 31 20 32 30 30 39 2f 30 34 2f 33   1.101 2009/04/3
4f330 30 20 30 39 3a 31 30 3a 33 38 20 64 61 6e 69 65  0 09:10:38 danie
4f340 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
4f350 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 5f  .#ifndef _PAGER_
4f360 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 45  H_.#define _PAGE
4f370 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61  R_H_../*.** Defa
4f380 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65  ult maximum size
4f390 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20   for persistent 
4f3a0 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41  journal files. A
4f3b0 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 61   negative .** va
4f3c0 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d  lue means no lim
4f3d0 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 6d  it. This value m
4f3e0 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  ay be overridden
4f3f0 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 73   using the .** s
4f400 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
4f410 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 50  alSizeLimit() AP
4f420 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 41  I. See also "PRA
4f430 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65  GMA journal_size
4f440 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 6e  _limit"..*/.#ifn
4f450 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
4f460 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f  LT_JOURNAL_SIZE_
4f470 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 20  LIMIT.  #define 
4f480 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a  SQLITE_DEFAULT_J
4f490 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49  OURNAL_SIZE_LIMI
4f4a0 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  T -1.#endif../*.
4f4b0 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64  ** The type used
4f4c0 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20   to represent a 
4f4d0 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68  page number.  Th
4f4e0 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20  e first page in 
4f4f0 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c  a file.** is cal
4f500 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69  led page 1.  0 i
4f510 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  s used to repres
4f520 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22  ent "not a page"
4f530 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 32  ..*/.typedef u32
4f540 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   Pgno;../*.** Ea
4f550 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20  ch open file is 
4f560 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70  managed by a sep
4f570 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f  arate instance o
4f580 66 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74  f the "Pager" st
4f590 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65  ructure..*/.type
4f5a0 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72  def struct Pager
4f5b0 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48   Pager;../*.** H
4f5c0 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70  andle type for p
4f5d0 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ages..*/.typedef
4f5e0 20 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62   struct PgHdr Db
4f5f0 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 67  Page;../*.** Pag
4f600 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d  e number PAGER_M
4f610 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20  J_PGNO is never 
4f620 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74  used in an SQLit
4f630 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69  e database (it i
4f640 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f  s.** reserved fo
4f650 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64  r working around
4f660 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78   a windows/posix
4f670 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79   incompatibility
4f680 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64  ). It is.** used
4f690 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
4f6a0 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20  to signify that 
4f6b0 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66  the remainder of
4f6c0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
4f6d0 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64  e .** is devoted
4f6e0 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61   to storing a ma
4f6f0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
4f700 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f  e - there are no
4f710 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a   more pages to.*
4f720 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65  * roll back. See
4f730 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75   comments for fu
4f740 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74  nction writeMast
4f750 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70  erJournal() in p
4f760 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 64  ager.c .** for d
4f770 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69  etails..*/.#defi
4f780 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  ne PAGER_MJ_PGNO
4f790 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e  (x) ((Pgno)((PEN
4f7a0 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e  DING_BYTE/((x)->
4f7b0 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a 0a  pageSize))+1))..
4f7c0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
4f7d0 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61  lues for the fla
4f7e0 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gs parameter to 
4f7f0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
4f800 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20  ()..**.** NOTE: 
4f810 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73  These values mus
4f820 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72  t match the corr
4f830 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f  esponding BTREE_
4f840 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65   values in btree
4f850 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  .h..*/.#define P
4f860 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41  AGER_OMIT_JOURNA
4f870 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20  L  0x0001    /* 
4f880 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c  Do not use a rol
4f890 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f  lback journal */
4f8a0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4e  .#define PAGER_N
4f8b0 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78 30  O_READLOCK   0x0
4f8c0 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20 72  002    /* Omit r
4f8d0 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64  eadlocks on read
4f8e0 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f  only files */../
4f8f0 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65  *.** Valid value
4f900 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64  s for the second
4f910 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
4f920 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67  ite3PagerLocking
4f930 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69  Mode()..*/.#defi
4f940 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  ne PAGER_LOCKING
4f950 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20  MODE_QUERY      
4f960 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  -1.#define PAGER
4f970 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
4f980 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 69  MAL      0.#defi
4f990 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  ne PAGER_LOCKING
4f9a0 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 20  MODE_EXCLUSIVE  
4f9b0 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20   1../*.** Valid 
4f9c0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73  values for the s
4f9d0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
4f9e0 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  o sqlite3PagerJo
4f9f0 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a  urnalMode()..*/.
4fa00 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f  #define PAGER_JO
4fa10 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20  URNALMODE_QUERY 
4fa20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20       -1.#define 
4fa30 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
4fa40 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 20  E_DELETE      0 
4fa50 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 64    /* Commit by d
4fa60 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20  eleting journal 
4fa70 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  file */.#define 
4fa80 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
4fa90 45 5f 50 45 52 53 49 53 54 20 20 20 20 20 31 20  E_PERSIST     1 
4faa0 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 7a    /* Commit by z
4fab0 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 68  eroing journal h
4fac0 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  eader */.#define
4fad0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
4fae0 44 45 5f 4f 46 46 20 20 20 20 20 20 20 20 20 32  DE_OFF         2
4faf0 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d     /* Journal om
4fb00 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 66 69  itted.  */.#defi
4fb10 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
4fb20 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 20 20  MODE_TRUNCATE   
4fb30 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62   3   /* Commit b
4fb40 79 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75  y truncating jou
4fb50 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  rnal */.#define 
4fb60 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
4fb70 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 34 20  E_MEMORY      4 
4fb80 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a    /* In-memory j
4fb90 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a  ournal file */..
4fba0 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e  /*.** The remain
4fbb0 64 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c 65  der of this file
4fbc0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 65   contains the de
4fbd0 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 68  clarations of th
4fbe0 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74  e functions.** t
4fbf0 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20  hat make up the 
4fc00 50 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 6d  Pager sub-system
4fc10 20 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 65   API. See source
4fc20 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66   code comments f
4fc30 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c 65  or .** a detaile
4fc40 64 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  d description of
4fc50 20 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a 2a   each routine..*
4fc60 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63  /../* Open and c
4fc70 6c 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e  lose a Pager con
4fc80 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 4c  nection. */ .SQL
4fc90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4fca0 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
4fcb0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
4fcc0 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65 72 2c  Pager **ppPager,
4fcd0 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e   const char*, in
4fce0 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  t,int,int);.SQLI
4fcf0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4fd00 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
4fd10 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b  (Pager *pPager);
4fd20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4fd30 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
4fd40 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50  ReadFileheader(P
4fd50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e 73 69  ager*, int, unsi
4fd60 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 0a 2f 2a  gned char*);../*
4fd70 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20   Functions used 
4fd80 74 6f 20 63 6f 6e 66 69 67 75 72 65 20 61 20 50  to configure a P
4fd90 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a  ager object. */.
4fda0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4fdb0 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
4fdc0 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 50  SetBusyhandler(P
4fdd0 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29 28 76 6f  ager*, int(*)(vo
4fde0 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a 29 3b 0a  id *), void *);.
4fdf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4fe00 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
4fe10 53 65 74 52 65 69 6e 69 74 65 72 28 50 61 67 65  SetReiniter(Page
4fe20 72 2a 2c 20 76 6f 69 64 28 2a 29 28 44 62 50 61  r*, void(*)(DbPa
4fe30 67 65 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ge*));.SQLITE_PR
4fe40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4fe50 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a  3PagerSetPagesiz
4fe60 65 28 50 61 67 65 72 2a 2c 20 75 31 36 2a 29 3b  e(Pager*, u16*);
4fe70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4fe80 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
4fe90 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67  MaxPageCount(Pag
4fea0 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  er*, int);.SQLIT
4feb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4fec0 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61  qlite3PagerSetCa
4fed0 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20  chesize(Pager*, 
4fee0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4fef0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4ff00 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c  3PagerSetSafetyL
4ff10 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74 2c  evel(Pager*,int,
4ff20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4ff30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4ff40 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
4ff50 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a  (Pager *, int);.
4ff60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4ff70 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  nt sqlite3PagerJ
4ff80 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72  ournalMode(Pager
4ff90 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
4ffa0 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c  _PRIVATE i64 sql
4ffb0 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
4ffc0 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72 20  SizeLimit(Pager 
4ffd0 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f  *, i64);.SQLITE_
4ffe0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
4fff0 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 65 33  backup **sqlite3
50000 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 50  PagerBackupPtr(P
50010 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63  ager*);../* Func
50020 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6f 62  tions used to ob
50030 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 61 73 65  tain and release
50040 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73   page references
50050 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  . */ .SQLITE_PRI
50060 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
50070 50 61 67 65 72 41 63 71 75 69 72 65 28 50 61 67  PagerAcquire(Pag
50080 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
50090 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 20 2a 2a   pgno, DbPage **
500a0 70 70 50 61 67 65 2c 20 69 6e 74 20 63 6c 72 46  ppPage, int clrF
500b0 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 20 73 71  lag);.#define sq
500c0 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 41 2c  lite3PagerGet(A,
500d0 42 2c 43 29 20 73 71 6c 69 74 65 33 50 61 67 65  B,C) sqlite3Page
500e0 72 41 63 71 75 69 72 65 28 41 2c 42 2c 43 2c 30  rAcquire(A,B,C,0
500f0 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
50100 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33   DbPage *sqlite3
50110 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65  PagerLookup(Page
50120 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20  r *pPager, Pgno 
50130 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pgno);.SQLITE_PR
50140 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
50150 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67  e3PagerRef(DbPag
50160 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
50170 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
50180 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61 67  PagerUnref(DbPag
50190 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 61 74 69  e*);../* Operati
501a0 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 65 66 65  ons on page refe
501b0 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 4c 49 54  rences. */.SQLIT
501c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
501d0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
501e0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
501f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
50200 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
50210 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  ite(DbPage*);.SQ
50220 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
50230 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76   sqlite3PagerMov
50240 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62 50  epage(Pager*,DbP
50250 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 0a  age*,Pgno,int);.
50260 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
50270 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  nt sqlite3PagerP
50280 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61  ageRefcount(DbPa
50290 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ge*);.SQLITE_PRI
502a0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
502b0 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 44  e3PagerGetData(D
502c0 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 54  bPage *); .SQLIT
502d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
502e0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
502f0 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b 20  xtra(DbPage *); 
50300 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75  ../* Functions u
50310 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20 70 61  sed to manage pa
50320 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ger transactions
50330 20 61 6e 64 20 73 61 76 65 70 6f 69 6e 74 73 2e   and savepoints.
50340 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
50350 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
50360 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67  gerPagecount(Pag
50370 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  er*, int*);.SQLI
50380 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
50390 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e  qlite3PagerBegin
503a0 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 65 78 46  (Pager*, int exF
503b0 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  lag, int);.SQLIT
503c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
503d0 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
503e0 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c  PhaseOne(Pager*,
503f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
50400 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  ter, int);.SQLIT
50410 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
50420 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50  lite3PagerSync(P
50430 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53  ager *pPager);.S
50440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
50450 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
50460 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67  mmitPhaseTwo(Pag
50470 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
50480 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
50490 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61  PagerRollback(Pa
504a0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
504b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
504c0 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f  3PagerOpenSavepo
504d0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  int(Pager *pPage
504e0 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54  r, int n);.SQLIT
504f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
50500 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f  lite3PagerSavepo
50510 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  int(Pager *pPage
50520 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69  r, int op, int i
50530 53 61 76 65 70 6f 69 6e 74 29 3b 0a 0a 2f 2a 20  Savepoint);../* 
50540 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  Functions used t
50550 6f 20 71 75 65 72 79 20 70 61 67 65 72 20 73 74  o query pager st
50560 61 74 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 72  ate and configur
50570 61 74 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45  ation. */.SQLITE
50580 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69  _PRIVATE u8 sqli
50590 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e  te3PagerIsreadon
505a0 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  ly(Pager*);.SQLI
505b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
505c0 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
505d0 75 6e 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  unt(Pager*);.SQL
505e0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
505f0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50  t char *sqlite3P
50600 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67  agerFilename(Pag
50610 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
50620 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74  VATE const sqlit
50630 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50  e3_vfs *sqlite3P
50640 61 67 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b  agerVfs(Pager*);
50650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
50660 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71  sqlite3_file *sq
50670 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50  lite3PagerFile(P
50680 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
50690 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
506a0 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a  r *sqlite3PagerJ
506b0 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72  ournalname(Pager
506c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
506d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
506e0 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a  gerNosync(Pager*
506f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
50700 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50  E void *sqlite3P
50710 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 50 61  agerTempSpace(Pa
50720 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
50730 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
50740 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61  3PagerIsMemdb(Pa
50750 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74  ger*);../* Funct
50760 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 74 72 75  ions used to tru
50770 6e 63 61 74 65 20 74 68 65 20 64 61 74 61 62 61  ncate the databa
50780 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49  se file. */.SQLI
50790 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
507a0 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e  sqlite3PagerTrun
507b0 63 61 74 65 49 6d 61 67 65 28 50 61 67 65 72 2a  cateImage(Pager*
507c0 2c 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 55 73 65 64  ,Pgno);../* Used
507d0 20 62 79 20 65 6e 63 72 79 70 74 69 6f 6e 20 65   by encryption e
507e0 78 74 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69  xtensions. */.#i
507f0 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
50800 43 4f 44 45 43 0a 53 51 4c 49 54 45 5f 50 52 49  CODEC.SQLITE_PRI
50810 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
50820 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 63  te3PagerSetCodec
50830 28 50 61 67 65 72 2a 2c 76 6f 69 64 2a 28 2a 29  (Pager*,void*(*)
50840 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e  (void*,void*,Pgn
50850 6f 2c 69 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a 23  o,int),void*);.#
50860 65 6e 64 69 66 0a 0a 2f 2a 20 46 75 6e 63 74 69  endif../* Functi
50870 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ons to support t
50880 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
50890 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65  ging. */.#if !de
508a0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
508b0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
508c0 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49  TEST).SQLITE_PRI
508d0 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69  VATE   Pgno sqli
508e0 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
508f0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  er(DbPage*);.SQL
50900 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
50910 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
50920 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65  writeable(DbPage
50930 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  *);.#endif.#ifde
50940 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
50950 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
50960 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  nt *sqlite3Pager
50970 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53  Stats(Pager*);.S
50980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
50990 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
509a0 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29  rRefdump(Pager*)
509b0 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65  ;.  void disable
509c0 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
509d0 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f  rors(void);.  vo
509e0 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  id enable_simula
509f0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f  ted_io_errors(vo
50a00 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  id);.#else.# def
50a10 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  ine disable_simu
50a20 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
50a30 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c  ).# define enabl
50a40 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
50a50 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a  rrors().#endif..
50a60 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52  #endif /* _PAGER
50a70 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
50a80 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
50a90 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ager.h *********
50aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50ac0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
50ad0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
50ae0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
50af0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
50b00 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
50b10 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
50b20 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
50b30 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20  pcache.h in the 
50b40 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
50b50 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
50b60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
50b70 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
50b80 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a  le pcache.h ****
50b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50bb0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
50bc0 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a  08 August 05.**.
50bd0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
50be0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
50bf0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
50c00 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
50c10 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
50c20 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
50c30 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
50c40 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
50c50 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
50c60 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
50c70 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
50c80 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
50c90 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
50ca0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
50cb0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
50cc0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
50cd0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
50ce0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
50cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
50d30 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
50d40 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  defines the inte
50d50 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73  rface that the s
50d60 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65  qlite page cache
50d70 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a  .** subsystem. .
50d80 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
50d90 70 63 61 63 68 65 2e 68 2c 76 20 31 2e 31 39 20  pcache.h,v 1.19 
50da0 32 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36  2009/01/20 17:06
50db0 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :27 danielk1977 
50dc0 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
50dd0 66 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79  f _PCACHE_H_..ty
50de0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48  pedef struct PgH
50df0 64 72 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65  dr PgHdr;.typede
50e00 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20  f struct PCache 
50e10 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45  PCache;../*.** E
50e20 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65  very page in the
50e30 20 63 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f   cache is contro
50e40 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61  lled by an insta
50e50 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
50e60 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  wing.** structur
50e70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48  e..*/.struct PgH
50e80 64 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61  dr {.  void *pDa
50e90 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
50ea0 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74        /* Content
50eb0 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f   of this page */
50ec0 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b  .  void *pExtra;
50ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ee0 20 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65    /* Extra conte
50ef0 6e 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70  nt */.  PgHdr *p
50f00 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20  Dirty;          
50f10 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69         /* Transi
50f20 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74  ent list of dirt
50f30 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e  y pages */.  Pgn
50f40 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20  o pgno;         
50f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
50f60 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
50f70 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61  his page */.  Pa
50f80 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20  ger *pPager;    
50f90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
50fa0 54 68 65 20 70 61 67 65 72 20 74 68 69 73 20 70  The pager this p
50fb0 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a  age is part of *
50fc0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
50fd0 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33  CHECK_PAGES.  u3
50fe0 32 20 70 61 67 65 48 61 73 68 3b 20 20 20 20 20  2 pageHash;     
50ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
51000 48 61 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e  Hash of page con
51010 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  tent */.#endif. 
51020 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20   u16 flags;     
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51040 2f 2a 20 50 47 48 44 52 20 66 6c 61 67 73 20 64  /* PGHDR flags d
51050 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a  efined below */.
51060 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  /************
51070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
510a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20  **********.  ** 
510b0 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61  Elements above a
510c0 72 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20  re public.  All 
510d0 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20  that follows is 
510e0 70 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 68  private to pcach
510f0 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f  e.c.  ** and sho
51100 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73  uld not be acces
51110 73 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64  sed by other mod
51120 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36  ules..  */.  i16
51130 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
51140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
51150 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f  umber of users o
51160 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  f this page */. 
51170 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b   PCache *pCache;
51180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51190 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f 77  /* Cache that ow
511a0 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  ns this page */.
511b0 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79  .  PgHdr *pDirty
511c0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
511d0 20 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e    /* Next elemen
511e0 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72  t in list of dir
511f0 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67  ty pages */.  Pg
51200 48 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b  Hdr *pDirtyPrev;
51210 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
51220 50 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74  Previous element
51230 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74   in list of dirt
51240 79 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f  y pages */.};../
51250 2a 20 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72  * Bit values for
51260 20 50 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a   PgHdr.flags */.
51270 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49  #define PGHDR_DI
51280 52 54 59 20 20 20 20 20 20 20 20 20 20 20 20 20  RTY             
51290 30 78 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68  0x002  /* Page h
512a0 61 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64  as changed */.#d
512b0 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44  efine PGHDR_NEED
512c0 5f 53 59 4e 43 20 20 20 20 20 20 20 20 20 30 78  _SYNC         0x
512d0 30 30 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68  004  /* Fsync th
512e0 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
512f0 61 6c 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20  al before.      
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51320 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73   ** writing this
51330 20 70 61 67 65 20 74 6f 20 74 68 65 20 64 61 74   page to the dat
51340 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65  abase */.#define
51350 20 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44   PGHDR_NEED_READ
51360 20 20 20 20 20 20 20 20 20 30 78 30 30 38 20 20           0x008  
51370 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e  /* Content is un
51380 72 65 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  read */.#define 
51390 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49  PGHDR_REUSE_UNLI
513a0 4b 45 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f  KELY    0x010  /
513b0 2a 20 41 20 68 69 6e 74 20 74 68 61 74 20 72 65  * A hint that re
513c0 75 73 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20  use is unlikely 
513d0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52  */.#define PGHDR
513e0 5f 44 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20  _DONT_WRITE     
513f0 20 20 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20     0x020  /* Do 
51400 6e 6f 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e  not write conten
51410 74 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a  t to disk */../*
51420 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
51430 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67  shutdown the pag
51440 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65  e cache subsyste
51450 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  m */.SQLITE_PRIV
51460 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
51470 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28  cacheInitialize(
51480 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
51490 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
514a0 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e  e3PcacheShutdown
514b0 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65  (void);../* Page
514c0 20 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61   cache buffer ma
514d0 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65  nagement:.** The
514e0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  se routines impl
514f0 65 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e  ement SQLITE_CON
51500 46 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a  FIG_PAGECACHE..*
51510 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
51520 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61   void sqlite3PCa
51530 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28 76  cheBufferSetup(v
51540 6f 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69  oid *, int sz, i
51550 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74  nt n);../* Creat
51560 65 20 61 20 6e 65 77 20 70 61 67 65 72 20 63 61  e a new pager ca
51570 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65  che..** Under me
51580 6d 6f 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76  mory stress, inv
51590 6f 6b 65 20 78 53 74 72 65 73 73 20 74 6f 20 74  oke xStress to t
515a0 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73  ry to make pages
515b0 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20   clean..** Only 
515c0 63 6c 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e  clean and unpinn
515d0 65 64 20 70 61 67 65 73 20 63 61 6e 20 62 65 20  ed pages can be 
515e0 72 65 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51  reclaimed..*/.SQ
515f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
51600 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f  d sqlite3PcacheO
51610 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67  pen(.  int szPag
51620 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
51630 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
51640 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20   every page */. 
51650 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20   int szExtra,   
51660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51670 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61  /* Extra space a
51680 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65  ssociated with e
51690 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ach page */.  in
516a0 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20  t bPurgeable,   
516b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
516c0 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
516d0 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
516e0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
516f0 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67  tress)(void*, Pg
51700 48 64 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74  Hdr*), /* Call t
51710 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61  o try to make pa
51720 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76  ges clean */.  v
51730 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20  oid *pStress,   
51740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
51750 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74   Argument to xSt
51760 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65  ress */.  PCache
51770 20 2a 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20   *pToInit       
51780 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61           /* Prea
51790 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66  llocated space f
517a0 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f  or the PCache */
517b0 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74  .);../* Modify t
517c0 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74  he page-size aft
517d0 65 72 20 74 68 65 20 63 61 63 68 65 20 68 61 73  er the cache has
517e0 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a   been created. *
517f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
51800 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
51810 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50  cheSetPageSize(P
51820 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a  Cache *, int);..
51830 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  /* Return the si
51840 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61  ze in bytes of a
51850 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20   PCache object. 
51860 20 55 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f   Used to preallo
51870 63 61 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20  cate.** storage 
51880 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  space..*/.SQLITE
51890 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
518a0 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76  ite3PcacheSize(v
518b0 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65  oid);../* One re
518c0 6c 65 61 73 65 20 70 65 72 20 73 75 63 63 65 73  lease per succes
518d0 73 66 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67  sful fetch.  Pag
518e0 65 20 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69  e is pinned unti
518f0 6c 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52  l released..** R
51900 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64  eference counted
51910 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
51920 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
51930 50 63 61 63 68 65 46 65 74 63 68 28 50 43 61 63  PcacheFetch(PCac
51940 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63  he*, Pgno, int c
51950 72 65 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72  reateFlag, PgHdr
51960 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
51970 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
51980 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67  PcacheRelease(Pg
51990 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  Hdr*);..SQLITE_P
519a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
519b0 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67  te3PcacheDrop(Pg
519c0 48 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f  Hdr*);         /
519d0 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72  * Remove page fr
519e0 6f 6d 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  om cache */.SQLI
519f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
51a00 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
51a10 65 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20  eDirty(PgHdr*); 
51a20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
51a30 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64  page is marked d
51a40 69 72 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  irty */.SQLITE_P
51a50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
51a60 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
51a70 61 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f  an(PgHdr*);    /
51a80 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20  * Mark a single 
51a90 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f  page as clean */
51aa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
51ab0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
51ac0 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68  heCleanAll(PCach
51ad0 65 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20  e*);    /* Mark 
51ae0 61 6c 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70  all dirty list p
51af0 61 67 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f  ages as clean */
51b00 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61  ../* Change a pa
51b10 67 65 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64  ge number.  Used
51b20 20 62 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e   by incr-vacuum.
51b30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
51b40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
51b50 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a  cacheMove(PgHdr*
51b60 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d  , Pgno);../* Rem
51b70 6f 76 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69  ove all pages wi
51b80 74 68 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65  th pgno>x.  Rese
51b90 74 20 74 68 65 20 63 61 63 68 65 20 69 66 20 78  t the cache if x
51ba0 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ==0 */.SQLITE_PR
51bb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
51bc0 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65  e3PcacheTruncate
51bd0 28 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78  (PCache*, Pgno x
51be0 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73  );../* Get a lis
51bf0 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70  t of all dirty p
51c00 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68  ages in the cach
51c10 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67  e, sorted by pag
51c20 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49  e number */.SQLI
51c30 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 72  TE_PRIVATE PgHdr
51c40 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 44   *sqlite3PcacheD
51c50 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a  irtyList(PCache*
51c60 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64  );../* Reset and
51c70 20 63 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65   close the cache
51c80 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54   object */.SQLIT
51c90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
51ca0 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73  qlite3PcacheClos
51cb0 65 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20  e(PCache*);../* 
51cc0 43 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d  Clear flags from
51cd0 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61   pages of the pa
51ce0 67 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  ge cache */.SQLI
51cf0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
51d00 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
51d10 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63  arSyncFlags(PCac
51d20 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61  he *);../* Disca
51d30 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rd the contents 
51d40 6f 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a  of the cache */.
51d50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
51d60 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
51d70 65 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b  eClear(PCache*);
51d80 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ../* Return the 
51d90 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
51da0 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
51db0 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53   references */.S
51dc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
51dd0 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  t sqlite3PcacheR
51de0 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29  efCount(PCache*)
51df0 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20  ;../* Increment 
51e00 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
51e10 75 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69  unt of an existi
51e20 6e 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54  ng page */.SQLIT
51e30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
51e40 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28  qlite3PcacheRef(
51e50 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45  PgHdr*);..SQLITE
51e60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
51e70 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65  ite3PcachePageRe
51e80 66 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a  fcount(PgHdr*);.
51e90 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74  ./* Return the t
51ea0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
51eb0 61 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74  ages stored in t
51ec0 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  he cache */.SQLI
51ed0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
51ee0 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65  qlite3PcachePage
51ef0 63 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a  count(PCache*);.
51f00 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
51f10 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 20 49 74  HECK_PAGES./* It
51f20 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c  erate through al
51f30 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75  l dirty pages cu
51f40 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69  rrently stored i
51f50 6e 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 69  n the cache. Thi
51f60 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69  s.** interface i
51f70 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
51f80 20 69 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b   if SQLITE_CHECK
51f90 5f 50 41 47 45 53 20 69 73 20 64 65 66 69 6e 65  _PAGES is define
51fa0 64 20 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c  d when the .** l
51fb0 69 62 72 61 72 79 20 69 73 20 62 75 69 6c 74 2e  ibrary is built.
51fc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
51fd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
51fe0 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74  cacheIterateDirt
51ff0 79 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  y(PCache *pCache
52000 2c 20 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28  , void (*xIter)(
52010 50 67 48 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69  PgHdr *));.#endi
52020 66 0a 0a 2f 2a 20 53 65 74 20 61 6e 64 20 67 65  f../* Set and ge
52030 74 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20  t the suggested 
52040 63 61 63 68 65 2d 73 69 7a 65 20 66 6f 72 20 74  cache-size for t
52050 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67  he specified pag
52060 65 72 2d 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20  er-cache..**.** 
52070 49 66 20 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78  If no global max
52080 69 6d 75 6d 20 69 73 20 63 6f 6e 66 69 67 75 72  imum is configur
52090 65 64 2c 20 74 68 65 6e 20 74 68 65 20 73 79 73  ed, then the sys
520a0 74 65 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20  tem attempts to 
520b0 6c 69 6d 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74  limit.** the tot
520c0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
520d0 65 73 20 63 61 63 68 65 64 20 62 79 20 70 75 72  es cached by pur
520e0 67 65 61 62 6c 65 20 70 61 67 65 72 2d 63 61 63  geable pager-cac
520f0 68 65 73 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a  hes to the sum.*
52100 2a 20 6f 66 20 74 68 65 20 73 75 67 67 65 73 74  * of the suggest
52110 65 64 20 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a  ed cache-sizes..
52120 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
52130 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
52140 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65  acheSetCachesize
52150 28 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b  (PCache *, int);
52160 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
52170 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
52180 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
52190 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65  acheGetCachesize
521a0 28 50 43 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64  (PCache *);.#end
521b0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
521c0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
521d0 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72  MANAGEMENT./* Tr
521e0 79 20 74 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f  y to return memo
521f0 72 79 20 75 73 65 64 20 62 79 20 74 68 65 20 70  ry used by the p
52200 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20  cache module to 
52210 74 68 65 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20  the main memory 
52220 68 65 61 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  heap */.SQLITE_P
52230 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
52240 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d  e3PcacheReleaseM
52250 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64  emory(int);.#end
52260 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
52270 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
52280 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
52290 65 33 50 63 61 63 68 65 53 74 61 74 73 28 69 6e  e3PcacheStats(in
522a0 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74  t*,int*,int*,int
522b0 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49  *);.#endif..SQLI
522c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
522d0 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74  sqlite3PCacheSet
522e0 44 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a  Default(void);..
522f0 23 65 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43 48  #endif /* _PCACH
52300 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  E_H_ */../******
52310 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
52320 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  pcache.h *******
52330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52350 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
52360 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
52370 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
52380 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49  t off in sqliteI
52390 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
523a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  *******/../*****
523b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
523c0 65 20 6f 73 2e 68 20 69 6e 20 74 68 65 20 6d 69  e os.h in the mi
523d0 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
523e0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
523f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
52400 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
52410 66 69 6c 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a  file os.h ******
52420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52440 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
52450 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
52460 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
52470 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
52480 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
52490 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
524a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
524b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
524c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
524d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
524e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
524f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
52500 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
52510 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
52520 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
52530 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
52540 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
52550 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
52560 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
52570 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
52580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
525a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
525b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
525c0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
525d0 20 68 65 61 64 65 72 20 66 69 6c 65 20 28 74 6f   header file (to
525e0 67 65 74 68 65 72 20 77 69 74 68 20 69 73 20 63  gether with is c
525f0 6f 6d 70 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63  ompanion C sourc
52600 65 2d 63 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22  e-code file.** "
52610 6f 73 2e 63 22 29 20 61 74 74 65 6d 70 74 20 74  os.c") attempt t
52620 6f 20 61 62 73 74 72 61 63 74 20 74 68 65 20 75  o abstract the u
52630 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
52640 69 6e 67 20 73 79 73 74 65 6d 20 73 6f 20 74 68  ing system so th
52650 61 74 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  at.** the SQLite
52660 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 77 6f   library will wo
52670 72 6b 20 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58  rk on both POSIX
52680 20 61 6e 64 20 77 69 6e 64 6f 77 73 20 73 79 73   and windows sys
52690 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tems..**.** This
526a0 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20   header file is 
526b0 23 69 6e 63 6c 75 64 65 2d 65 64 20 62 79 20 73  #include-ed by s
526c0 71 6c 69 74 65 49 6e 74 2e 68 20 61 6e 64 20 74  qliteInt.h and t
526d0 68 75 73 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62  hus ends up.** b
526e0 65 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 62 79  eing included by
526f0 20 65 76 65 72 79 20 73 6f 75 72 63 65 20 66 69   every source fi
52700 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f  le..**.** $Id: o
52710 73 2e 68 2c 76 20 31 2e 31 30 38 20 32 30 30 39  s.h,v 1.108 2009
52720 2f 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20  /02/05 16:31:46 
52730 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  drh Exp $.*/.#if
52740 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f  ndef _SQLITE_OS_
52750 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49  H_.#define _SQLI
52760 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  TE_OS_H_../*.** 
52770 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65  Figure out if we
52780 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
52790 68 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c  h Unix, Windows,
527a0 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a   or some other.*
527b0 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  * operating syst
527c0 65 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66  em.  After the f
527d0 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f  ollowing block o
527e0 66 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63  f preprocess mac
527f0 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53  ros,.** all of S
52800 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53  QLITE_OS_UNIX, S
52810 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51  QLITE_OS_WIN, SQ
52820 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64  LITE_OS_OS2, and
52830 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52   SQLITE_OS_OTHER
52840 20 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65   .** will define
52850 64 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72  d to either 1 or
52860 20 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20   0.  One of the 
52870 66 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20  four will be 1. 
52880 20 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74   The other .** t
52890 68 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a  hree will be 0..
528a0 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
528b0 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a  QLITE_OS_OTHER).
528c0 23 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  # if SQLITE_OS_O
528d0 54 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65  THER==1.#   unde
528e0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  f SQLITE_OS_UNIX
528f0 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  .#   define SQLI
52900 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20  TE_OS_UNIX 0.#  
52910 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53   undef SQLITE_OS
52920 5f 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20  _WIN.#   define 
52930 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a  SQLITE_OS_WIN 0.
52940 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45  #   undef SQLITE
52950 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69  _OS_OS2.#   defi
52960 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  ne SQLITE_OS_OS2
52970 20 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e   0.# else.#   un
52980 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54  def SQLITE_OS_OT
52990 48 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64  HER.# endif.#end
529a0 69 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  if.#if !defined(
529b0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20  SQLITE_OS_UNIX) 
529c0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
529d0 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64  TE_OS_OTHER).# d
529e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
529f0 4f 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66  OTHER 0.# ifndef
52a00 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23   SQLITE_OS_WIN.#
52a10 20 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57     if defined(_W
52a20 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64  IN32) || defined
52a30 28 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e  (WIN32) || defin
52a40 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c  ed(__CYGWIN__) |
52a50 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47  | defined(__MING
52a60 57 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  W32__) || define
52a70 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a  d(__BORLANDC__).
52a80 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  #     define SQL
52a90 49 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20  ITE_OS_WIN 1.#  
52aa0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
52ab0 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20  _OS_UNIX 0.#    
52ac0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
52ad0 53 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66  S_OS2 0.#   elif
52ae0 20 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f   defined(__EMX__
52af0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53  ) || defined(_OS
52b00 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53  2) || defined(OS
52b10 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f  2) || defined(_O
52b20 53 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  S2_) || defined(
52b30 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64  __OS2__).#     d
52b40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
52b50 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69  WIN 0.#     defi
52b60 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  ne SQLITE_OS_UNI
52b70 58 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65  X 0.#     define
52b80 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31   SQLITE_OS_OS2 1
52b90 0a 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20  .#   else.#     
52ba0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
52bb0 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66  _WIN 0.#     def
52bc0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e  ine SQLITE_OS_UN
52bd0 49 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e  IX 1.#     defin
52be0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20  e SQLITE_OS_OS2 
52bf0 30 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73  0.#  endif.# els
52c00 65 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49  e.#  define SQLI
52c10 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20  TE_OS_UNIX 0.#  
52c20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
52c30 5f 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23  _OS2 0.# endif.#
52c40 65 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51  else.# ifndef SQ
52c50 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64  LITE_OS_WIN.#  d
52c60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
52c70 57 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65  WIN 0.# endif.#e
52c80 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65  ndif../*.** Dete
52c90 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20  rmine if we are 
52ca0 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e  dealing with Win
52cb0 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68  dowsCE - which h
52cc0 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64  as a much.** red
52cd0 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66  uced API..*/.#if
52ce0 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f   defined(_WIN32_
52cf0 57 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51  WCE).# define SQ
52d00 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a  LITE_OS_WINCE 1.
52d10 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
52d20 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30  QLITE_OS_WINCE 0
52d30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
52d40 44 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d  Define the maxim
52d50 75 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d  um size of a tem
52d60 70 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a  porary filename.
52d70 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
52d80 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c  _WIN.# include <
52d90 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66  windows.h>.# def
52da0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e  ine SQLITE_TEMPN
52db0 41 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41  AME_SIZE (MAX_PA
52dc0 54 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c  TH+50).#elif SQL
52dd0 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20  ITE_OS_OS2.# if 
52de0 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c  (__GNUC__ > 3 ||
52df0 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26   __GNUC__ == 3 &
52e00 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f  & __GNUC_MINOR__
52e10 20 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65   >= 3) && define
52e20 64 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52  d(OS2_HIGH_MEMOR
52e30 59 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f  Y).#  include <o
52e40 73 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73  s2safe.h> /* has
52e50 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20   to be included 
52e60 62 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72  before os2.h for
52e70 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b   linking to work
52e80 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65   */.# endif.# de
52e90 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54  fine INCL_DOSDAT
52ea0 45 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49  ETIME.# define I
52eb0 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23  NCL_DOSFILEMGR.#
52ec0 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53   define INCL_DOS
52ed0 45 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20  ERRORS.# define 
52ee0 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64  INCL_DOSMISC.# d
52ef0 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52  efine INCL_DOSPR
52f00 4f 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49  OCESS.# define I
52f10 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52  NCL_DOSMODULEMGR
52f20 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
52f30 4f 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69  OSSEMAPHORES.# i
52f40 6e 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23  nclude <os2.h>.#
52f50 20 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e   include <uconv.
52f60 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  h>.# define SQLI
52f70 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45  TE_TEMPNAME_SIZE
52f80 20 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50   (CCHMAXPATHCOMP
52f90 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
52fa0 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45   SQLITE_TEMPNAME
52fb0 5f 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66  _SIZE 200.#endif
52fc0 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f  ../* If the SET_
52fd0 46 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69  FULLSYNC macro i
52fe0 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62  s not defined ab
52ff0 6f 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69  ove, then make i
53000 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a  t.** a no-op.*/.
53010 23 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c  #ifndef SET_FULL
53020 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45  SYNC.# define SE
53030 54 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a  T_FULLSYNC(x,y).
53040 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
53050 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f  e default size o
53060 66 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a  f a disk sector.
53070 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
53080 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
53090 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53  _SIZE.# define S
530a0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
530b0 43 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65  CTOR_SIZE 512.#e
530c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70  ndif../*.** Temp
530d0 6f 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20  orary files are 
530e0 6e 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77  named starting w
530f0 69 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20  ith this prefix 
53100 66 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72  followed by 16 r
53110 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75  andom.** alphanu
53120 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73  meric characters
53130 2c 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78  , and no file ex
53140 74 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72  tension. They ar
53150 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a  e stored in the.
53160 2a 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64  ** OS's standard
53170 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
53180 64 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61  directory, and a
53190 72 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72  re deleted prior
531a0 20 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20   to exit..** If 
531b0 73 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20  sqlite is being 
531c0 65 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74  embedded in anot
531d0 68 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75  her program, you
531e0 20 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61   may wish to cha
531f0 6e 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69  nge the.** prefi
53200 78 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75  x to reflect you
53210 72 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65  r program's name
53220 2c 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75  , so that if you
53230 72 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a  r program exits.
53240 2a 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20  ** prematurely, 
53250 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69  old temporary fi
53260 6c 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c  les can be easil
53270 79 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68  y identified. Th
53280 69 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a  is can be done.*
53290 2a 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45  * using -DSQLITE
532a0 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49  _TEMP_FILE_PREFI
532b0 58 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74  X=myprefix_ on t
532c0 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d  he compiler comm
532d0 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20  and line..**.** 
532e0 32 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65  2006-10-31:  The
532f0 20 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20   default prefix 
53300 75 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69  used to be "sqli
53310 74 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a  te_".  But then.
53320 2a 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65  ** Mcafee starte
53330 64 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69  d using SQLite i
53340 6e 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72  n their anti-vir
53350 75 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69  us product and i
53360 74 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74  t.** started put
53370 74 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20  ting files with 
53380 74 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d  the "sqlite" nam
53390 65 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70  e in the c:/temp
533a0 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73   folder..** This
533b0 20 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69   annoyed many wi
533c0 6e 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68  ndows users.  Th
533d0 6f 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20  ose users would 
533e0 74 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f  then do a .** Go
533f0 6f 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20  ogle search for 
53400 22 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74  "sqlite", find t
53410 68 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d  he telephone num
53420 62 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64  bers of the.** d
53430 65 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61  evelopers and ca
53440 6c 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20  ll to wake them 
53450 75 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20  up at night and 
53460 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72  complain..** For
53470 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68   this reason, th
53480 65 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70  e default name p
53490 72 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64  refix is changed
534a0 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20   to be "sqlite" 
534b0 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b  .** spelled back
534c0 77 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74  wards.  So the t
534d0 65 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74  emp files are st
534e0 69 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20  ill identified, 
534f0 62 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73  but.** anybody s
53500 6d 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66  mart enough to f
53510 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f  igure out the co
53520 64 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c  de is also likel
53530 79 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67  y smart.** enoug
53540 68 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63  h to know that c
53550 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c  alling the devel
53560 6f 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65  oper will not he
53570 6c 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66  lp get rid.** of
53580 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69   the file..*/.#i
53590 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d  fndef SQLITE_TEM
535a0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20  P_FILE_PREFIX.# 
535b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
535c0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22  MP_FILE_PREFIX "
535d0 65 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a  etilqs_".#endif.
535e0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
535f0 77 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20  wing values may 
53600 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  be passed as the
53610 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
53620 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73   to.** sqlite3Os
53630 4c 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69  Lock(). The vari
53640 6f 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69  ous locks exhibi
53650 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  t the following 
53660 73 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a  semantics:.**.**
53670 20 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20   SHARED:    Any 
53680 6e 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73  number of proces
53690 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53  ses may hold a S
536a0 48 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c  HARED lock simul
536b0 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45  taneously..** RE
536c0 53 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c  SERVED:  A singl
536d0 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f  e process may ho
536e0 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  ld a RESERVED lo
536f0 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a  ck on a file at.
53700 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e  **            an
53710 79 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72  y time. Other pr
53720 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64  ocesses may hold
53730 20 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20   and obtain new 
53740 53 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a  SHARED locks..**
53750 20 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69   PENDING:   A si
53760 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79  ngle process may
53770 20 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20   hold a PENDING 
53780 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61  lock on a file a
53790 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
537a0 61 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78  any one time. Ex
537b0 69 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f  isting SHARED lo
537c0 63 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c  cks may persist,
537d0 20 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20   but no new.**  
537e0 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44            SHARED
537f0 20 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62   locks may be ob
53800 74 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20  tained by other 
53810 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58  processes..** EX
53820 43 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c  CLUSIVE: An EXCL
53830 55 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c  USIVE lock precl
53840 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c  udes all other l
53850 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44  ocks..**.** PEND
53860 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74  ING_LOCK may not
53870 20 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63   be passed direc
53880 74 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73  tly to sqlite3Os
53890 4c 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c  Lock(). Instead,
538a0 20 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68   a.** process th
538b0 61 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45  at requests an E
538c0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61  XCLUSIVE lock ma
538d0 79 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69  y actually obtai
538e0 6e 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c  n a PENDING.** l
538f0 6f 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65  ock. This can be
53900 20 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20   upgraded to an 
53910 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62  EXCLUSIVE lock b
53920 79 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63  y a subsequent c
53930 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
53940 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64  3OsLock()..*/.#d
53950 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20  efine NO_LOCK   
53960 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
53970 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20  SHARED_LOCK     
53980 31 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56  1.#define RESERV
53990 45 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66  ED_LOCK   2.#def
539a0 69 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  ine PENDING_LOCK
539b0 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58      3.#define EX
539c0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a  CLUSIVE_LOCK  4.
539d0 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b  ./*.** File Lock
539e0 69 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73  ing Notes:  (Mos
539f0 74 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77  tly about window
53a00 73 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20  s but also some 
53a10 69 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a  info for Unix).*
53a20 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75  *.** We cannot u
53a30 73 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20  se LockFileEx() 
53a40 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28  or UnlockFileEx(
53a50 29 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45  ) on Win95/98/ME
53a60 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73   because.** thos
53a70 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
53a80 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20  not available.  
53a90 53 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c  So we use only L
53aa0 6f 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a  ockFile() and.**
53ab0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a   UnlockFile()..*
53ac0 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20  *.** LockFile() 
53ad0 70 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73  prevents not jus
53ae0 74 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c  t writing but al
53af0 73 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74  so reading by ot
53b00 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a  her processes..*
53b10 2a 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20  * A SHARED_LOCK 
53b20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c  is obtained by l
53b30 6f 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20  ocking a single 
53b40 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20  randomly-chosen 
53b50 0a 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20  .** byte out of 
53b60 61 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65  a specific range
53b70 20 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c   of bytes. The l
53b80 6f 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61  ock byte is obta
53b90 69 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64  ined at .** rand
53ba0 6f 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61  om so two separa
53bb0 74 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70  te readers can p
53bc0 72 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74  robably access t
53bd0 68 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a  he file at the .
53be0 2a 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e  ** same time, un
53bf0 6c 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e  less they are un
53c00 6c 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65  lucky and choose
53c10 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62   the same lock b
53c20 79 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55  yte..** An EXCLU
53c30 53 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  SIVE_LOCK is obt
53c40 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
53c50 20 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68   all bytes in th
53c60 65 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72  e range..** Ther
53c70 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e  e can only be on
53c80 65 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53  e writer.  A RES
53c90 45 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62  ERVED_LOCK is ob
53ca0 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e  tained by lockin
53cb0 67 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79  g.** a single by
53cc0 74 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74  te of the file t
53cd0 68 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65  hat is designate
53ce0 64 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65  d as the reserve
53cf0 64 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20  d lock byte..** 
53d00 41 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69  A PENDING_LOCK i
53d10 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f  s obtained by lo
53d20 63 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74  cking a designat
53d30 65 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e  ed byte differen
53d40 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45  t from.** the RE
53d50 53 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65  SERVED_LOCK byte
53d60 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54  ..**.** On WinNT
53d70 2f 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20  /2K/XP systems, 
53d80 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64  LockFileEx() and
53d90 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20   UnlockFileEx() 
53da0 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a  are available,.*
53db0 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65  * which means we
53dc0 20 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f   can use reader/
53dd0 77 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57  writer locks.  W
53de0 68 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65  hen reader/write
53df0 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75  r locks.** are u
53e00 73 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73  sed, the lock is
53e10 20 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73   placed on the s
53e20 61 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74  ame range of byt
53e30 65 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a  es that is used.
53e40 2a 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69  ** for probabili
53e50 73 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20  stic locking in 
53e60 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65  Win95/98/ME.  He
53e70 6e 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67  nce, the locking
53e80 20 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20   scheme.** will 
53e90 73 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d  support two or m
53ea0 6f 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72  ore Win95 reader
53eb0 73 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65  s or two or more
53ec0 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a   WinNT readers..
53ed0 2a 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20  ** But a single 
53ee0 57 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c  Win95 reader wil
53ef0 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57  l lock out all W
53f00 69 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64  inNT readers and
53f10 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e   a single.** Win
53f20 4e 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c  NT reader will l
53f30 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65  ock out all othe
53f40 72 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e  r Win95 readers.
53f50 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
53f60 77 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70  wing #defines sp
53f70 65 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20  ecify the range 
53f80 6f 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f  of bytes used fo
53f90 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48  r locking..** SH
53fa0 41 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65  ARED_SIZE is the
53fb0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
53fc0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
53fd0 65 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63  e pool from whic
53fe0 68 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79  h.** a random by
53ff0 74 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66  te is selected f
54000 6f 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  or a shared lock
54010 2e 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62  .  The pool of b
54020 79 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72  ytes for.** shar
54030 65 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20  ed locks begins 
54040 61 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e  at SHARED_FIRST.
54050 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65   .**.** The same
54060 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
54070 79 20 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61  y and.** byte ra
54080 6e 67 65 73 20 61 72 65 20 75 73 65 64 20 66 6f  nges are used fo
54090 72 20 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65  r Unix.  This le
540a0 61 76 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f  aves open the po
540b0 73 73 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69  ssiblity of havi
540c0 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e  ng.** clients on
540d0 20 77 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61   win95, winNT, a
540e0 6e 64 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b  nd unix all talk
540f0 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ing to the same 
54100 73 68 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61  shared file.** a
54110 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63  nd all locking c
54120 6f 72 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f  orrectly.  To do
54130 20 73 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72   so would requir
54140 65 20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72  e that samba (or
54150 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f   whatever.** too
54160 6c 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20  l is being used 
54170 66 6f 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67  for file sharing
54180 29 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63  ) implements loc
54190 6b 73 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74  ks correctly bet
541a0 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20  ween.** windows 
541b0 61 6e 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67  and unix.  I'm g
541c0 75 65 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e  uessing that isn
541d0 27 74 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70  't likely to hap
541e0 70 65 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75  pen, but by.** u
541f0 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f  sing the same lo
54200 63 6b 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61  cking range we a
54210 72 65 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e  re at least open
54220 20 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c   to the possibil
54230 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69  ity..**.** Locki
54240 6e 67 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73  ng in windows is
54250 20 6d 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72   manditory.  For
54260 20 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65   this reason, we
54270 20 63 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a   cannot store.**
54280 20 61 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20   actual data in 
54290 74 68 65 20 62 79 74 65 73 20 75 73 65 64 20 66  the bytes used f
542a0 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65  or locking.  The
542b0 20 70 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c   pager never all
542c0 6f 63 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61  ocates.** the pa
542d0 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20  ges involved in 
542e0 6c 6f 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72  locking therefor
542f0 65 2e 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20  e.  SHARED_SIZE 
54300 69 73 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a  is selected so.*
54310 2a 20 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73  * that all locks
54320 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73   will fit on a s
54330 69 6e 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20  ingle page even 
54340 61 74 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70  at the minimum p
54350 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e  age size..** PEN
54360 44 49 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65  DING_BYTE define
54370 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  s the beginning 
54380 6f 66 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42  of the locks.  B
54390 79 20 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e  y default PENDIN
543a0 47 5f 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74  G_BYTE.** is set
543b0 20 68 69 67 68 20 73 6f 20 74 68 61 74 20 77 65   high so that we
543c0 20 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61   don't have to a
543d0 6c 6c 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65  llocate an unuse
543e0 64 20 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a  d page except.**
543f0 20 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20   for very large 
54400 64 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20  databases.  But 
54410 6f 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20  one should test 
54420 74 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e  the page skippin
54430 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73  g logic .** by s
54440 65 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42  etting PENDING_B
54450 59 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e  YTE low and runn
54460 69 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72  ing the entire r
54470 65 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e  egression suite.
54480 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20  .**.** Changing 
54490 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e  the value of PEN
544a0 44 49 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74  DING_BYTE result
544b0 73 20 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e  s in a subtly in
544c0 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69  compatible.** fi
544d0 6c 65 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65  le format.  Depe
544e0 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20  nding on how it 
544f0 69 73 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20  is changed, you 
54500 6d 69 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65  might not notice
54510 0a 2a 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74  .** the incompat
54520 69 62 69 6c 69 74 79 20 72 69 67 68 74 20 61 77  ibility right aw
54530 61 79 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67  ay, even running
54540 20 61 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69   a full regressi
54550 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20  on test..** The 
54560 64 65 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e  default location
54570 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45   of PENDING_BYTE
54580 20 69 73 20 74 68 65 20 66 69 72 73 74 20 62 79   is the first by
54590 74 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31  te past the.** 1
545a0 47 42 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a  GB boundary..**.
545b0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49  */.#define PENDI
545c0 4e 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c  NG_BYTE      sql
545d0 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a  ite3PendingByte.
545e0 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44  #define RESERVED
545f0 5f 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49  _BYTE     (PENDI
54600 4e 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69  NG_BYTE+1).#defi
54610 6e 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20  ne SHARED_FIRST 
54620 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59       (PENDING_BY
54630 54 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48  TE+2).#define SH
54640 41 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20  ARED_SIZE       
54650 35 31 30 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63  510../* .** Func
54660 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73  tions for access
54670 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ing sqlite3_file
54680 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c   methods .*/.SQL
54690 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
546a0 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73  sqlite3OsClose(s
546b0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 53  qlite3_file*);.S
546c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
546d0 74 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  t sqlite3OsRead(
546e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76  sqlite3_file*, v
546f0 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69  oid*, int amt, i
54700 36 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49  64 offset);.SQLI
54710 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
54720 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71  qlite3OsWrite(sq
54730 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e  lite3_file*, con
54740 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d  st void*, int am
54750 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a  t, i64 offset);.
54760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
54770 6e 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e  nt sqlite3OsTrun
54780 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  cate(sqlite3_fil
54790 65 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 53  e*, i64 size);.S
547a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
547b0 74 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  t sqlite3OsSync(
547c0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
547d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
547e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
547f0 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  sFileSize(sqlite
54800 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53  3_file*, i64 *pS
54810 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ize);.SQLITE_PRI
54820 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
54830 4f 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  OsLock(sqlite3_f
54840 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ile*, int);.SQLI
54850 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
54860 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73  qlite3OsUnlock(s
54870 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
54880 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
54890 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
548a0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
548b0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
548c0 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
548d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
548e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46  E int sqlite3OsF
548f0 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  ileControl(sqlit
54900 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69  e3_file*,int,voi
54910 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c  d*);.#define SQL
54920 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43  ITE_FCNTL_DB_UNC
54930 48 41 4e 47 45 44 20 30 78 63 61 30 39 33 66 61  HANGED 0xca093fa
54940 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  0.SQLITE_PRIVATE
54950 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 65   int sqlite3OsSe
54960 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33  ctorSize(sqlite3
54970 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 53 51 4c 49  _file *id);.SQLI
54980 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
54990 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68  qlite3OsDeviceCh
549a0 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71  aracteristics(sq
549b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b  lite3_file *id);
549c0 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f  ../* .** Functio
549d0 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67  ns for accessing
549e0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74   sqlite3_vfs met
549f0 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hods .*/.SQLITE_
54a00 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
54a10 74 65 33 4f 73 4f 70 65 6e 28 73 71 6c 69 74 65  te3OsOpen(sqlite
54a20 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63  3_vfs *, const c
54a30 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66  har *, sqlite3_f
54a40 69 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a  ile*, int, int *
54a50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
54a60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44  E int sqlite3OsD
54a70 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66  elete(sqlite3_vf
54a80 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  s *, const char 
54a90 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
54aa0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
54ab0 74 65 33 4f 73 41 63 63 65 73 73 28 73 71 6c 69  te3OsAccess(sqli
54ac0 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74  te3_vfs *, const
54ad0 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e   char *, int, in
54ae0 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c  t *pResOut);.SQL
54af0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54b00 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74  sqlite3OsFullPat
54b10 68 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  hname(sqlite3_vf
54b20 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  s *, const char 
54b30 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b  *, int, char *);
54b40 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
54b50 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
54b60 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ION.SQLITE_PRIVA
54b70 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
54b80 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33  OsDlOpen(sqlite3
54b90 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68  _vfs *, const ch
54ba0 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar *);.SQLITE_PR
54bb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
54bc0 65 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69  e3OsDlError(sqli
54bd0 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20  te3_vfs *, int, 
54be0 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  char *);.SQLITE_
54bf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73  PRIVATE void (*s
54c00 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71  qlite3OsDlSym(sq
54c10 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f 69  lite3_vfs *, voi
54c20 64 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  d *, const char 
54c30 2a 29 29 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  *))(void);.SQLIT
54c40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
54c50 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28  qlite3OsDlClose(
54c60 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76  sqlite3_vfs *, v
54c70 6f 69 64 20 2a 29 3b 0a 23 65 6e 64 69 66 20 2f  oid *);.#endif /
54c80 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  * SQLITE_OMIT_LO
54c90 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a  AD_EXTENSION */.
54ca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
54cb0 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64  nt sqlite3OsRand
54cc0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76  omness(sqlite3_v
54cd0 66 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20  fs *, int, char 
54ce0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
54cf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
54d00 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66  Sleep(sqlite3_vf
54d10 73 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  s *, int);.SQLIT
54d20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
54d30 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
54d40 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  me(sqlite3_vfs *
54d50 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 0a 2f 2a 0a  , double*);../*.
54d60 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 66  ** Convenience f
54d70 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 6f 70 65  unctions for ope
54d80 6e 69 6e 67 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ning and closing
54d90 20 66 69 6c 65 73 20 75 73 69 6e 67 20 0a 2a 2a   files using .**
54da0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
54db0 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63  ) to obtain spac
54dc0 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2d 68  e for the file-h
54dd0 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e  andle structure.
54de0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
54df0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
54e00 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 71 6c 69 74  OpenMalloc(sqlit
54e10 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
54e20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f  char *, sqlite3_
54e30 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c 69 6e 74  file **, int,int
54e40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
54e50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
54e60 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65  CloseFree(sqlite
54e70 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 65 6e 64  3_file *);..#end
54e80 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 4f 53  if /* _SQLITE_OS
54e90 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
54ea0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
54eb0 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.h ************
54ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54ee0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54ef0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
54f00 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
54f10 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
54f20 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
54f30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54f40 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
54f50 6d 75 74 65 78 2e 68 20 69 6e 20 74 68 65 20 6d  mutex.h in the m
54f60 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
54f70 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
54f80 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54f90 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
54fa0 6c 65 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a  le mutex.h *****
54fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54fd0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
54fe0 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a  07 August 28.**.
54ff0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
55000 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
55010 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
55020 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
55030 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
55040 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
55050 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
55060 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
55070 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
55080 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
55090 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
550a0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
550b0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
550c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
550d0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
550e0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
550f0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
55100 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
55110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
55150 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
55160 61 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20  ains the common 
55170 68 65 61 64 65 72 20 66 6f 72 20 61 6c 6c 20 6d  header for all m
55180 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
55190 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  ions..** The sql
551a0 69 74 65 49 6e 74 2e 68 20 68 65 61 64 65 72 20  iteInt.h header 
551b0 23 69 6e 63 6c 75 64 65 73 20 74 68 69 73 20 66  #includes this f
551c0 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 69  ile so that it i
551d0 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74  s available.** t
551e0 6f 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 69 6c  o all source fil
551f0 65 73 2e 20 20 57 65 20 62 72 65 61 6b 20 69 74  es.  We break it
55200 20 6f 75 74 20 69 6e 20 61 6e 20 65 66 66 6f 72   out in an effor
55210 74 20 74 6f 20 6b 65 65 70 20 74 68 65 20 63 6f  t to keep the co
55220 64 65 0a 2a 2a 20 62 65 74 74 65 72 20 6f 72 67  de.** better org
55230 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f  anized..**.** NO
55240 54 45 3a 20 20 73 6f 75 72 63 65 20 66 69 6c 65  TE:  source file
55250 73 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 23  s should *not* #
55260 69 6e 63 6c 75 64 65 20 74 68 69 73 20 68 65 61  include this hea
55270 64 65 72 20 66 69 6c 65 20 64 69 72 65 63 74 6c  der file directl
55280 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 20 66 69 6c  y..** Source fil
55290 65 73 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75  es should #inclu
552a0 64 65 20 74 68 65 20 73 71 6c 69 74 65 49 6e 74  de the sqliteInt
552b0 2e 68 20 66 69 6c 65 20 61 6e 64 20 6c 65 74 20  .h file and let 
552c0 74 68 61 74 20 66 69 6c 65 0a 2a 2a 20 69 6e 63  that file.** inc
552d0 6c 75 64 65 20 74 68 69 73 20 6f 6e 65 20 69 6e  lude this one in
552e0 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20  directly..**.** 
552f0 24 49 64 3a 20 6d 75 74 65 78 2e 68 2c 76 20 31  $Id: mutex.h,v 1
55300 2e 39 20 32 30 30 38 2f 31 30 2f 30 37 20 31 35  .9 2008/10/07 15
55310 3a 32 35 3a 34 38 20 64 72 68 20 45 78 70 20 24  :25:48 drh Exp $
55320 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75  .*/.../*.** Figu
55330 72 65 20 6f 75 74 20 77 68 61 74 20 76 65 72 73  re out what vers
55340 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 64 65 20  ion of the code 
55350 74 6f 20 75 73 65 2e 20 20 54 68 65 20 63 68 6f  to use.  The cho
55360 69 63 65 73 20 61 72 65 0a 2a 2a 0a 2a 2a 20 20  ices are.**.**  
55370 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d   SQLITE_MUTEX_OM
55380 49 54 20 20 20 20 20 20 20 20 20 4e 6f 20 6d 75  IT         No mu
55390 74 65 78 20 6c 6f 67 69 63 2e 20 20 4e 6f 74 20  tex logic.  Not 
553a0 65 76 65 6e 20 73 74 75 62 73 2e 20 20 54 68 65  even stubs.  The
553b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
553c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
553d0 6d 75 74 65 78 65 73 20 69 6d 70 6c 65 6d 65 6e  mutexes implemen
553e0 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6f  tion cannot be o
553f0 76 65 72 72 69 64 64 65 6e 0a 2a 2a 20 20 20 20  verridden.**    
55400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55410 20 20 20 20 20 20 20 20 20 61 74 20 73 74 61 72           at star
55420 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  t-time..**.**   
55430 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
55440 50 20 20 20 20 20 20 20 20 20 46 6f 72 20 73 69  P         For si
55450 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70  ngle-threaded ap
55460 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 4e 6f 0a  plications.  No.
55470 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
55480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
55490 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20  utual exclusion 
554a0 69 73 20 70 72 6f 76 69 64 65 64 2e 20 20 42 75  is provided.  Bu
554b0 74 20 74 68 69 73 0a 2a 2a 20 20 20 20 20 20 20  t this.**       
554c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
554d0 20 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 61        implementa
554e0 74 69 6f 6e 20 63 61 6e 20 62 65 20 6f 76 65 72  tion can be over
554f0 72 69 64 64 65 6e 20 61 74 0a 2a 2a 20 20 20 20  ridden at.**    
55500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55510 20 20 20 20 20 20 20 20 20 73 74 61 72 74 2d 74           start-t
55520 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c  ime..**.**   SQL
55530 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
55540 44 53 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69  DS     For multi
55550 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63  -threaded applic
55560 61 74 69 6f 6e 73 20 6f 6e 20 55 6e 69 78 2e 0a  ations on Unix..
55570 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
55580 55 54 45 58 5f 57 33 32 20 20 20 20 20 20 20 20  UTEX_W32        
55590 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65    For multi-thre
555a0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
555b0 73 20 6f 6e 20 57 69 6e 33 32 2e 0a 2a 2a 0a 2a  s on Win32..**.*
555c0 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  *   SQLITE_MUTEX
555d0 5f 4f 53 32 20 20 20 20 20 20 20 20 20 20 46 6f  _OS2          Fo
555e0 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64  r multi-threaded
555f0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e   applications on
55600 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 66 20 21 53   OS/2..*/.#if !S
55610 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
55620 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
55630 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 23 65 6e 64  _MUTEX_OMIT.#end
55640 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  if.#if SQLITE_TH
55650 52 45 41 44 53 41 46 45 20 26 26 20 21 64 65 66  READSAFE && !def
55660 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45  ined(SQLITE_MUTE
55670 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 66 20 53 51  X_NOOP).#  if SQ
55680 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 20  LITE_OS_UNIX.#  
55690 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
556a0 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a 23  MUTEX_PTHREADS.#
556b0 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53    elif SQLITE_OS
556c0 5f 57 49 4e 0a 23 20 20 20 20 64 65 66 69 6e 65  _WIN.#    define
556d0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
556e0 32 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 45  2.#  elif SQLITE
556f0 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 20 64 65 66  _OS_OS2.#    def
55700 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
55710 5f 4f 53 32 0a 23 20 20 65 6c 73 65 0a 23 20 20  _OS2.#  else.#  
55720 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
55730 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 20 65 6e  MUTEX_NOOP.#  en
55740 64 69 66 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  dif.#endif..#ifd
55750 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
55760 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  OMIT./*.** If th
55770 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6d  is is a no-op im
55780 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 69 6d  plementation, im
55790 70 6c 65 6d 65 6e 74 20 65 76 65 72 79 74 68 69  plement everythi
557a0 6e 67 20 61 73 20 6d 61 63 72 6f 73 2e 0a 2a 2f  ng as macros..*/
557b0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
557c0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 58 29 20  _mutex_alloc(X) 
557d0 20 20 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74     ((sqlite3_mut
557e0 65 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73  ex*)8).#define s
557f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65  qlite3_mutex_fre
55800 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c  e(X).#define sql
55810 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
55820 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  (X).#define sqli
55830 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 58 29  te3_mutex_try(X)
55840 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a        SQLITE_OK.
55850 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
55860 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29 0a 23  mutex_leave(X).#
55870 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
55880 75 74 65 78 5f 68 65 6c 64 28 58 29 20 20 20 20  utex_held(X)    
55890 20 31 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74   1.#define sqlit
558a0 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
558b0 28 58 29 20 20 31 0a 23 64 65 66 69 6e 65 20 73  (X)  1.#define s
558c0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
558d0 28 58 29 20 20 20 20 20 20 28 28 73 71 6c 69 74  (X)      ((sqlit
558e0 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65  e3_mutex*)8).#de
558f0 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65  fine sqlite3Mute
55900 78 49 6e 69 74 28 29 20 20 20 20 20 20 20 20 53  xInit()        S
55910 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65  QLITE_OK.#define
55920 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64   sqlite3MutexEnd
55930 28 29 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  ().#endif /* def
55940 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
55950 5f 4d 55 54 45 58 29 20 2a 2f 0a 0a 2f 2a 2a 2a  _MUTEX) */../***
55960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
55970 6f 66 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a  of mutex.h *****
55980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
559a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
559b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
559c0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
559d0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
559e0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
559f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a  **********/.../*
55a00 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73  .** Each databas
55a10 65 20 66 69 6c 65 20 74 6f 20 62 65 20 61 63 63  e file to be acc
55a20 65 73 73 65 64 20 62 79 20 74 68 65 20 73 79 73  essed by the sys
55a30 74 65 6d 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  tem is an instan
55a40 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ce.** of the fol
55a50 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
55a60 2e 20 20 54 68 65 72 65 20 61 72 65 20 6e 6f 72  .  There are nor
55a70 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 20 74 68 65  mally two of the
55a80 73 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a  se structures.**
55a90 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61   in the sqlite.a
55aa0 44 62 5b 5d 20 61 72 72 61 79 2e 20 20 61 44 62  Db[] array.  aDb
55ab0 5b 30 5d 20 69 73 20 74 68 65 20 6d 61 69 6e 20  [0] is the main 
55ac0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
55ad0 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 69 73 20 74  d.** aDb[1] is t
55ae0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
55af0 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 65   used to hold te
55b00 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20  mporary tables. 
55b10 20 41 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64   Additional.** d
55b20 61 74 61 62 61 73 65 73 20 6d 61 79 20 62 65 20  atabases may be 
55b30 61 74 74 61 63 68 65 64 2e 0a 2a 2f 0a 73 74 72  attached..*/.str
55b40 75 63 74 20 44 62 20 7b 0a 20 20 63 68 61 72 20  uct Db {.  char 
55b50 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
55b60 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20  /* Name of this 
55b70 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 74  database */.  Bt
55b80 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20  ree *pBt;       
55b90 20 20 20 2f 2a 20 54 68 65 20 42 2a 54 72 65 65     /* The B*Tree
55ba0 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74   structure for t
55bb0 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c  his database fil
55bc0 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e  e */.  u8 inTran
55bd0 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30  s;          /* 0
55be0 3a 20 6e 6f 74 20 77 72 69 74 61 62 6c 65 2e 20  : not writable. 
55bf0 20 31 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 2e   1: Transaction.
55c00 20 20 32 3a 20 43 68 65 63 6b 70 6f 69 6e 74 20    2: Checkpoint 
55c10 2a 2f 0a 20 20 75 38 20 73 61 66 65 74 79 5f 6c  */.  u8 safety_l
55c20 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 48 6f 77  evel;     /* How
55c30 20 61 67 67 72 65 73 73 69 76 65 20 61 74 20 73   aggressive at s
55c40 79 6e 63 69 6e 67 20 64 61 74 61 20 74 6f 20 64  yncing data to d
55c50 69 73 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  isk */.  void *p
55c60 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Aux;            
55c70 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20     /* Auxiliary 
55c80 64 61 74 61 2e 20 20 55 73 75 61 6c 6c 79 20 4e  data.  Usually N
55c90 55 4c 4c 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ULL */.  void (*
55ca0 78 46 72 65 65 41 75 78 29 28 76 6f 69 64 2a 29  xFreeAux)(void*)
55cb0 3b 20 20 2f 2a 20 52 6f 75 74 69 6e 65 20 74 6f  ;  /* Routine to
55cc0 20 66 72 65 65 20 70 41 75 78 20 2a 2f 0a 20 20   free pAux */.  
55cd0 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b  Schema *pSchema;
55ce0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
55cf0 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 65  to database sche
55d00 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61  ma (possibly sha
55d10 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  red) */.};../*.*
55d20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
55d30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
55d40 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 20  tructure stores 
55d50 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  a database schem
55d60 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  a..**.** If ther
55d70 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c  e are no virtual
55d80 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72   tables configur
55d90 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d  ed in this schem
55da0 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61  a, the.** Schema
55db0 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 20  .db variable is 
55dc0 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74  set to NULL. Aft
55dd0 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69 72  er the first vir
55de0 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61  tual table.** ha
55df0 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74  s been added, it
55e00 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   is set to point
55e10 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
55e20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20   connection .** 
55e30 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74  used to create t
55e40 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f  he connection. O
55e50 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 61  nce a virtual ta
55e60 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  ble has been.** 
55e70 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 68  added to the Sch
55e80 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 6e  ema structure an
55e90 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20  d the Schema.db 
55ea0 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74  variable populat
55eb0 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61  ed, .** only tha
55ec0 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  t database conne
55ed0 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68  ction may use th
55ee0 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70  e Schema to prep
55ef0 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  are .** statemen
55f00 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63  ts..*/.struct Sc
55f10 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68  hema {.  int sch
55f20 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a  ema_cookie;   /*
55f30 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   Database schema
55f40 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20   version number 
55f50 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f  for this file */
55f60 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b  .  Hash tblHash;
55f70 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74          /* All t
55f80 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79  ables indexed by
55f90 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20   name */.  Hash 
55fa0 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 20  idxHash;        
55fb0 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69  /* All (named) i
55fc0 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 62  ndices indexed b
55fd0 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68  y name */.  Hash
55fe0 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 20   trigHash;      
55ff0 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73   /* All triggers
56000 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65   indexed by name
56010 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 65   */.  Table *pSe
56020 71 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 68  qTab;      /* Th
56030 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
56040 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20  e table used by 
56050 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f  AUTOINCREMENT */
56060 0a 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 6d 61  .  u8 file_forma
56070 74 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d  t;      /* Schem
56080 61 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e  a format version
56090 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a   for this file *
560a0 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20  /.  u8 enc;     
560b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74           /* Text
560c0 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62   encoding used b
560d0 79 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  y this database 
560e0 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20  */.  u16 flags; 
560f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
56100 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  gs associated wi
56110 74 68 20 74 68 69 73 20 73 63 68 65 6d 61 20 2a  th this schema *
56120 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 5f 73 69  /.  int cache_si
56130 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ze;      /* Numb
56140 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 75  er of pages to u
56150 73 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  se in the cache 
56160 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
56170 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
56180 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  BLE.  sqlite3 *d
56190 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 22 4f  b;         /* "O
561a0 77 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 6f 6e  wner" connection
561b0 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 61 62  . See comment ab
561c0 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  ove */.#endif.};
561d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61  ../*.** These ma
561e0 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64  cros can be used
561f0 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f   to test, set, o
56200 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20  r clear bits in 
56210 74 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 67 73  the .** Db.flags
56220 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69   field..*/.#defi
56230 6e 65 20 44 62 48 61 73 50 72 6f 70 65 72 74 79  ne DbHasProperty
56240 28 44 2c 49 2c 50 29 20 20 20 20 20 28 28 28 44  (D,I,P)     (((D
56250 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d  )->aDb[I].pSchem
56260 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28  a->flags&(P))==(
56270 50 29 29 0a 23 64 65 66 69 6e 65 20 44 62 48 61  P)).#define DbHa
56280 73 41 6e 79 50 72 6f 70 65 72 74 79 28 44 2c 49  sAnyProperty(D,I
56290 2c 50 29 20 20 28 28 28 44 29 2d 3e 61 44 62 5b  ,P)  (((D)->aDb[
562a0 49 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67  I].pSchema->flag
562b0 73 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69  s&(P))!=0).#defi
562c0 6e 65 20 44 62 53 65 74 50 72 6f 70 65 72 74 79  ne DbSetProperty
562d0 28 44 2c 49 2c 50 29 20 20 20 20 20 28 44 29 2d  (D,I,P)     (D)-
562e0 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[I].pSchema-
562f0 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66  >flags|=(P).#def
56300 69 6e 65 20 44 62 43 6c 65 61 72 50 72 6f 70 65  ine DbClearPrope
56310 72 74 79 28 44 2c 49 2c 50 29 20 20 20 28 44 29  rty(D,I,P)   (D)
56320 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61  ->aDb[I].pSchema
56330 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f  ->flags&=~(P)../
56340 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
56350 75 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e 66  ues for the DB.f
56360 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a  lags field..**.*
56370 2a 20 54 68 65 20 44 42 5f 53 63 68 65 6d 61 4c  * The DB_SchemaL
56380 6f 61 64 65 64 20 66 6c 61 67 20 69 73 20 73 65  oaded flag is se
56390 74 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61  t after the data
563a0 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20  base schema has 
563b0 62 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 6e 74  been.** read int
563c0 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20  o internal hash 
563d0 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 42  tables..**.** DB
563e0 5f 55 6e 72 65 73 65 74 56 69 65 77 73 20 6d 65  _UnresetViews me
563f0 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20  ans that one or 
56400 6d 6f 72 65 20 76 69 65 77 73 20 68 61 76 65 20  more views have 
56410 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 61  column names tha
56420 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 66  t.** have been f
56430 69 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20 74  illed out.  If t
56440 68 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  he schema change
56450 73 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 20  s, these column 
56460 6e 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a 20 63  names might.** c
56470 68 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74 68  hanges and so th
56480 65 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65 64  e view will need
56490 20 74 6f 20 62 65 20 72 65 73 65 74 2e 0a 2a 2f   to be reset..*/
564a0 0a 23 64 65 66 69 6e 65 20 44 42 5f 53 63 68 65  .#define DB_Sche
564b0 6d 61 4c 6f 61 64 65 64 20 20 20 20 30 78 30 30  maLoaded    0x00
564c0 30 31 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d  01  /* The schem
564d0 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65  a has been loade
564e0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f  d */.#define DB_
564f0 55 6e 72 65 73 65 74 56 69 65 77 73 20 20 20 20  UnresetViews    
56500 30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 65 20  0x0002  /* Some 
56510 76 69 65 77 73 20 68 61 76 65 20 64 65 66 69 6e  views have defin
56520 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ed column names 
56530 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 45 6d  */.#define DB_Em
56540 70 74 79 20 20 20 20 20 20 20 20 20 20 20 30 78  pty           0x
56550 30 30 30 34 20 20 2f 2a 20 54 68 65 20 66 69 6c  0004  /* The fil
56560 65 20 69 73 20 65 6d 70 74 79 20 28 6c 65 6e 67  e is empty (leng
56570 74 68 20 30 20 62 79 74 65 73 29 20 2a 2f 0a 0a  th 0 bytes) */..
56580 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  /*.** The number
56590 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 6b 69   of different ki
565a0 6e 64 73 20 6f 66 20 74 68 69 6e 67 73 20 74 68  nds of things th
565b0 61 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74 65  at can be limite
565c0 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73  d.** using the s
565d0 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 20 69  qlite3_limit() i
565e0 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65  nterface..*/.#de
565f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4c 49  fine SQLITE_N_LI
56600 4d 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 4d 49  MIT (SQLITE_LIMI
56610 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
56620 52 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b  R+1)../*.** Look
56630 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20  aside malloc is 
56640 61 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73  a set of fixed-s
56650 69 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74  ize buffers that
56660 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20   can be used.** 
56670 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61 6c 6c  to satisfy small
56680 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72   transient memor
56690 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
566a0 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74  uests for object
566b0 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  s.** associated 
566c0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
566d0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
566e0 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20  ction.  The use 
566f0 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20  of.** lookaside 
56700 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20  malloc provides 
56710 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65  a significant pe
56720 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63  rformance enhanc
56730 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78  ement.** (approx
56740 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e   10%) by avoidin
56750 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f  g numerous mallo
56760 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20  c/free requests 
56770 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a  while parsing.**
56780 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e   SQL statements.
56790 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61  .**.** The Looka
567a0 73 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68  side structure h
567b0 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69  olds configurati
567c0 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  on information a
567d0 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b  bout the.** look
567e0 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62  aside malloc sub
567f0 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76  system.  Each av
56800 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61  ailable memory a
56810 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20  llocation in.** 
56820 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75  the lookaside su
56830 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65  bsystem is store
56840 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d on a linked li
56850 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53  st of LookasideS
56860 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a  lot.** objects..
56870 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20  **.** Lookaside 
56880 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20  allocations are 
56890 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72  only allowed for
568a0 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72   objects that ar
568b0 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  e associated.** 
568c0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
568d0 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
568e0 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73  ction.  Hence, s
568f0 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f  chema informatio
56900 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73  n cannot.** be s
56910 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69  tored in lookasi
56920 64 65 20 62 65 63 61 75 73 65 20 69 6e 20 73 68  de because in sh
56930 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 20  ared cache mode 
56940 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f 72  the schema infor
56950 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61  mation.** is sha
56960 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20  red by multiple 
56970 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
56980 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f 72 65  ions.  Therefore
56990 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a  , while parsing.
569a0 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d  ** schema inform
569b0 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61  ation, the Looka
569c0 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c  side.bEnabled fl
569d0 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 73 6f  ag is cleared so
569e0 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69   that.** lookasi
569f0 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61  de allocations a
56a00 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f 20 63  re not used to c
56a10 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 63 68  onstruct the sch
56a20 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a  ema objects..*/.
56a30 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65  struct Lookaside
56a40 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 20 20   {.  u16 sz;    
56a50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
56a60 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62 75 66  Size of each buf
56a70 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
56a80 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b 20 20    u8 bEnabled;  
56a90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c            /* Fal
56aa0 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 6e 65  se to disable ne
56ab0 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f  w lookaside allo
56ac0 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20  cations */.  u8 
56ad0 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20  bMalloced;      
56ae0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
56af0 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 64 20  pStart obtained 
56b00 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
56b10 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  loc() */.  int n
56b20 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Out;            
56b30 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
56b40 62 75 66 66 65 72 73 20 63 75 72 72 65 6e 74 6c  buffers currentl
56b50 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f  y checked out */
56b60 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20  .  int mxOut;   
56b70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 69             /* Hi
56b80 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72  ghwater mark for
56b90 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61   nOut */.  Looka
56ba0 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b  sideSlot *pFree;
56bb0 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76     /* List of av
56bc0 61 69 6c 61 62 6c 65 20 62 75 66 66 65 72 73 20  ailable buffers 
56bd0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61 72  */.  void *pStar
56be0 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
56bf0 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 76  First byte of av
56c00 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73  ailable memory s
56c10 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  pace */.  void *
56c20 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20  pEnd;           
56c30 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
56c40 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76 61 69  past end of avai
56c50 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d  lable space */.}
56c60 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69  ;.struct Lookasi
56c70 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61  deSlot {.  Looka
56c80 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b  sideSlot *pNext;
56c90 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 75 66 66      /* Next buff
56ca0 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 6f  er in the list o
56cb0 66 20 66 72 65 65 20 62 75 66 66 65 72 73 20 2a  f free buffers *
56cc0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61  /.};../*.** A ha
56cd0 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e  sh table for fun
56ce0 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
56cf0 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61  s..**.** Hash ea
56d00 63 68 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  ch FuncDef struc
56d10 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66  ture into one of
56d20 20 74 68 65 20 46 75 6e 63 44 65 66 48 61 73 68   the FuncDefHash
56d30 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43  .a[] slots..** C
56d40 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e  ollisions are on
56d50 20 74 68 65 20 46 75 6e 63 44 65 66 2e 70 48 61   the FuncDef.pHa
56d60 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72  sh chain..*/.str
56d70 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 68 20  uct FuncDefHash 
56d80 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 5b 32  {.  FuncDef *a[2
56d90 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48 61 73  3];       /* Has
56da0 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63  h table for func
56db0 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  tions */.};../*.
56dc0 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73 65  ** Each database
56dd0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
56de0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
56df0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
56e00 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 61 73  * The sqlite.las
56e10 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73 20 74  tRowid records t
56e20 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72  he last insert r
56e30 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64 20 62  owid generated b
56e40 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73  y an.** insert s
56e50 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72  tatement.  Inser
56e60 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e  ts on views do n
56e70 6f 74 20 61 66 66 65 63 74 20 69 74 73 20 76 61  ot affect its va
56e80 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72  lue.  Each.** tr
56e90 69 67 67 65 72 20 68 61 73 20 69 74 73 20 6f 77  igger has its ow
56ea0 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68  n context, so th
56eb0 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63 61 6e  at lastRowid can
56ec0 20 62 65 20 75 70 64 61 74 65 64 20 69 6e 73 69   be updated insi
56ed0 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 20 61  de.** triggers a
56ee0 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20 70 72  s usual.  The pr
56ef0 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77 69 6c  evious value wil
56f00 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 6f 6e  l be restored on
56f10 63 65 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a  ce the trigger.*
56f20 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e 20 65  * exits.  Upon e
56f30 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f 72 65  ntering a before
56f40 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 20 74   or instead of t
56f50 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f 77 69  rigger, lastRowi
56f60 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65  d is no.** longe
56f70 72 20 28 73 69 6e 63 65 20 61 66 74 65 72 20 76  r (since after v
56f80 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72  ersion 2.8.12) r
56f90 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a  eset to -1..**.*
56fa0 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e 43 68  * The sqlite.nCh
56fb0 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f  ange does not co
56fc0 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69 74 68  unt changes with
56fd0 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e 64 20  in triggers and 
56fe0 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74  keeps no.** cont
56ff0 65 78 74 2e 20 20 49 74 20 69 73 20 72 65 73 65  ext.  It is rese
57000 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 73 71  t at start of sq
57010 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54  lite3_exec..** T
57020 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e  he sqlite.lsChan
57030 67 65 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  ge represents th
57040 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  e number of chan
57050 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20  ges made by the 
57060 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20  last.** insert, 
57070 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c 65 74  update, or delet
57080 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 74  e statement.  It
57090 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e   remains constan
570a0 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65  t throughout the
570b0 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 61 20  .** length of a 
570c0 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 69 73  statement and is
570d0 20 74 68 65 6e 20 75 70 64 61 74 65 64 20 62 79   then updated by
570e0 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20   OP_SetCounts.  
570f0 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f  It keeps a.** co
57100 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75 73 74  ntext stack just
57110 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 64 20   like lastRowid 
57120 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 75 6e  so that the coun
57130 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20  t of changes.** 
57140 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72  within a trigger
57150 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74   is not seen out
57160 73 69 64 65 20 74 68 65 20 74 72 69 67 67 65 72  side the trigger
57170 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20 76 69  .  Changes to vi
57180 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66  ews do not.** af
57190 66 65 63 74 20 74 68 65 20 76 61 6c 75 65 20 6f  fect the value o
571a0 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54  f lsChange..** T
571b0 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68 61 6e  he sqlite.csChan
571c0 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f  ge keeps track o
571d0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
571e0 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65 73 20  current changes 
571f0 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61  (since.** the la
57200 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e  st statement) an
57210 64 20 69 73 20 75 73 65 64 20 74 6f 20 75 70 64  d is used to upd
57220 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 68 61  ate sqlite_lsCha
57230 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  nge..**.** The m
57240 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 73 20  ember variables 
57250 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 2c 20  sqlite.errCode, 
57260 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 20 61  sqlite.zErrMsg a
57270 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73  nd sqlite.zErrMs
57280 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65  g16.** store the
57290 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72   most recent err
572a0 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 66 20  or code and, if 
572b0 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 72 69  applicable, stri
572c0 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72  ng. The.** inter
572d0 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c  nal function sql
572e0 69 74 65 33 45 72 72 6f 72 28 29 20 69 73 20 75  ite3Error() is u
572f0 73 65 64 20 74 6f 20 73 65 74 20 74 68 65 73 65  sed to set these
57300 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f   variables.** co
57310 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73  nsistently..*/.s
57320 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 7b 0a  truct sqlite3 {.
57330 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
57340 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Vfs;            
57350 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20  /* OS Interface 
57360 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20  */.  int nDb;   
57370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57380 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
57390 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65 6e 74  backends current
573a0 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44  ly in use */.  D
573b0 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20 20 20  b *aDb;         
573c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
573d0 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a  All backends */.
573e0 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20    int flags;    
573f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57400 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73  /* Miscellaneous
57410 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 6c 6f   flags. See belo
57420 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46  w */.  int openF
57430 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
57440 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61       /* Flags pa
57450 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f  ssed to sqlite3_
57460 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20  vfs.xOpen() */. 
57470 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20   int errCode;   
57480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
57490 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  * Most recent er
574a0 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 54 45  ror code (SQLITE
574b0 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72  _*) */.  int err
574c0 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Mask;           
574d0 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65 73 75         /* & resu
574e0 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20 74 68  lt codes with th
574f0 69 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  is before return
57500 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f  ing */.  u8 auto
57510 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 20 20  Commit;         
57520 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 75         /* The au
57530 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20  to-commit flag. 
57540 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 74 6f  */.  u8 temp_sto
57550 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  re;             
57560 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a     /* 1: file 2:
57570 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 61 75   memory 0: defau
57580 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f  lt */.  u8 mallo
57590 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 20 20  cFailed;        
575a0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
575b0 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 61 20   we have seen a 
575c0 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a  malloc failure *
575d0 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d  /.  u8 dfltLockM
575e0 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ode;            
575f0 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63    /* Default loc
57600 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74  king-mode for at
57610 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20  tached dbs */.  
57620 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f  u8 dfltJournalMo
57630 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  de;           /*
57640 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c   Default journal
57650 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 68   mode for attach
57660 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e  ed dbs */.  sign
57670 65 64 20 63 68 61 72 20 6e 65 78 74 41 75 74 6f  ed char nextAuto
57680 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 75 74  vac;      /* Aut
57690 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61 66 74  ovac setting aft
576a0 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e 3d 30  er VACUUM if >=0
576b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 50 61   */.  int nextPa
576c0 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20  gesize;         
576d0 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a 65 20      /* Pagesize 
576e0 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20  after VACUUM if 
576f0 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62  >0 */.  int nTab
57700 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
57710 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
57720 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
57730 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 43   database */.  C
57740 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 6f 6c  ollSeq *pDfltCol
57750 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l;           /* 
57760 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c  The default coll
57770 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 28  ating sequence (
57780 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 36 34  BINARY) */.  i64
57790 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20   lastRowid;     
577a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 4f             /* RO
577b0 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 63 65  WID of most rece
577c0 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65 20 61  nt insert (see a
577d0 62 6f 76 65 29 20 2a 2f 0a 20 20 69 36 34 20 70  bove) */.  i64 p
577e0 72 69 6f 72 4e 65 77 52 6f 77 69 64 3b 20 20 20  riorNewRowid;   
577f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
57800 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65 72 61   randomly genera
57810 74 65 64 20 52 4f 57 49 44 20 2a 2f 0a 20 20 75  ted ROWID */.  u
57820 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20  32 magic;       
57830 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
57840 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72  Magic number for
57850 20 64 65 74 65 63 74 20 6c 69 62 72 61 72 79 20   detect library 
57860 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  misuse */.  int 
57870 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20  nChange;        
57880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
57890 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
578a0 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
578b0 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c   */.  int nTotal
578c0 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20  Change;         
578d0 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74      /* Value ret
578e0 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
578f0 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
57900 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
57910 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20  tex *mutex;     
57920 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f      /* Connectio
57930 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74  n mutex */.  int
57940 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4e   aLimit[SQLITE_N
57950 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 4c 69  _LIMIT];   /* Li
57960 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  mits */.  struct
57970 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e 66 6f   sqlite3InitInfo
57980 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72   {      /* Infor
57990 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75 72 69  mation used duri
579a0 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ng initializatio
579b0 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62  n */.    int iDb
579c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
579d0 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 61 63       /* When bac
579e0 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69 74 69  k is being initi
579f0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e  alized */.    in
57a00 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20 20  t newTnum;      
57a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f            /* Roo
57a20 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 62  tpage of table b
57a30 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64  eing initialized
57a40 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 79 3b   */.    u8 busy;
57a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57a60 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 63      /* TRUE if c
57a70 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 61 6c  urrently initial
57a80 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d 20 69 6e 69  izing */.  } ini
57a90 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73  t;.  int nExtens
57aa0 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ion;            
57ab0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
57ac0 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e  loaded extension
57ad0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45  s */.  void **aE
57ae0 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20  xtension;       
57af0 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
57b00 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
57b10 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72  handles */.  str
57b20 75 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b  uct Vdbe *pVdbe;
57b30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69             /* Li
57b40 73 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72  st of active vir
57b50 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f  tual machines */
57b60 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62  .  int activeVdb
57b70 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  eCnt;           
57b80 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44   /* Number of VD
57b90 42 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78  BEs currently ex
57ba0 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  ecuting */.  int
57bb0 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20   writeVdbeCnt;  
57bc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
57bd0 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56  mber of active V
57be0 44 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72  DBEs that are wr
57bf0 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20  iting */.  void 
57c00 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c  (*xTrace)(void*,
57c10 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20  const char*);   
57c20 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75       /* Trace fu
57c30 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
57c40 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20   *pTraceArg;    
57c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57c60 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
57c70 74 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66  t to the trace f
57c80 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
57c90 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f  d (*xProfile)(vo
57ca0 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  id*,const char*,
57cb0 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c  u64);  /* Profil
57cc0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ing function */.
57cd0 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65    void *pProfile
57ce0 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
57cf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
57d00 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69  rgument to profi
57d10 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  le function */. 
57d20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72   void *pCommitAr
57d30 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
57d40 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
57d50 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63  o xCommitCallbac
57d60 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20  k() */   .  int 
57d70 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63  (*xCommitCallbac
57d80 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a  k)(void*);    /*
57d90 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72   Invoked at ever
57da0 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76  y commit. */.  v
57db0 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72  oid *pRollbackAr
57dc0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
57dd0 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
57de0 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
57df0 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64  k() */   .  void
57e00 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c   (*xRollbackCall
57e10 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a  back)(void*); /*
57e20 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72   Invoked at ever
57e30 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76  y commit. */.  v
57e40 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b  oid *pUpdateArg;
57e50 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74  .  void (*xUpdat
57e60 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  eCallback)(void*
57e70 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  ,int, const char
57e80 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71  *,const char*,sq
57e90 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76  lite_int64);.  v
57ea0 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64  oid(*xCollNeeded
57eb0 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
57ec0 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
57ed0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f  nst char*);.  vo
57ee0 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31  id(*xCollNeeded1
57ef0 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  6)(void*,sqlite3
57f00 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63  *,int eTextRep,c
57f10 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76  onst void*);.  v
57f20 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64  oid *pCollNeeded
57f30 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  Arg;.  sqlite3_v
57f40 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 20 20  alue *pErr;     
57f50 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
57f60 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
57f70 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  e */.  char *zEr
57f80 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20  rMsg;           
57f90 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
57fa0 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
57fb0 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  e (UTF-8 encoded
57fc0 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  ) */.  char *zEr
57fd0 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20 20 20  rMsg16;         
57fe0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
57ff0 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
58000 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  e (UTF-16 encode
58010 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a  d) */.  union {.
58020 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74      volatile int
58030 20 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20   isInterrupted; 
58040 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74  /* True if sqlit
58050 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73  e3_interrupt has
58060 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a   been called */.
58070 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73      double notUs
58080 65 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed1;            
58090 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d  /* Spacer */.  }
580a0 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65   u1;.  Lookaside
580b0 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20   lookaside;     
580c0 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64       /* Lookasid
580d0 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75  e malloc configu
580e0 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65  ration */.#ifnde
580f0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
58100 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e  THORIZATION.  in
58110 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a  t (*xAuth)(void*
58120 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
58130 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
58140 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
58150 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 20  har*);.         
58160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58170 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73 73         /* Access
58180 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
58190 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
581a0 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20 20  d *pAuthArg;    
581b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73             /* 1s
581c0 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
581d0 65 20 61 63 63 65 73 73 20 61 75 74 68 20 66 75  e access auth fu
581e0 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66  nction */.#endif
581f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
58200 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41  OMIT_PROGRESS_CA
58210 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78  LLBACK.  int (*x
58220 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20 2a  Progress)(void *
58230 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72  );     /* The pr
58240 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
58250 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 67  */.  void *pProg
58260 72 65 73 73 41 72 67 3b 20 20 20 20 20 20 20 20  ressArg;        
58270 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
58280 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63  o the progress c
58290 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  allback */.  int
582a0 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20   nProgressOps;  
582b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
582c0 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20  mber of opcodes 
582d0 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61 6c  for progress cal
582e0 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a  lback */.#endif.
582f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
58300 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
58310 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b  .  Hash aModule;
58320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58330 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79   /* populated by
58340 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
58350 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61  module() */.  Ta
58360 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20 20  ble *pVTab;     
58370 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76              /* v
58380 74 61 62 20 77 69 74 68 20 61 63 74 69 76 65 20  tab with active 
58390 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20 6d  Connect/Create m
583a0 65 74 68 6f 64 20 2a 2f 0a 20 20 73 71 6c 69 74  ethod */.  sqlit
583b0 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61 6e  e3_vtab **aVTran
583c0 73 3b 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74  s;       /* Virt
583d0 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20  ual tables with 
583e0 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  open transaction
583f0 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61  s */.  int nVTra
58400 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ns;             
58410 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65       /* Allocate
58420 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61 6e  d size of aVTran
58430 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 46 75  s */.#endif.  Fu
58440 6e 63 44 65 66 48 61 73 68 20 61 46 75 6e 63 3b  ncDefHash aFunc;
58450 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
58460 61 73 68 20 74 61 62 6c 65 20 6f 66 20 63 6f 6e  ash table of con
58470 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  nection function
58480 73 20 2a 2f 0a 20 20 48 61 73 68 20 61 43 6f 6c  s */.  Hash aCol
58490 6c 53 65 71 3b 20 20 20 20 20 20 20 20 20 20 20  lSeq;           
584a0 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 6f 6c 6c       /* All coll
584b0 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
584c0 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c 65 72  */.  BusyHandler
584d0 20 62 75 73 79 48 61 6e 64 6c 65 72 3b 20 20 20   busyHandler;   
584e0 20 20 20 2f 2a 20 42 75 73 79 20 63 61 6c 6c 62     /* Busy callb
584f0 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 62 75 73  ack */.  int bus
58500 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 20 20 20  yTimeout;       
58510 20 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 68         /* Busy h
58520 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 74 2c 20  andler timeout, 
58530 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20 44 62 20  in msec */.  Db 
58540 61 44 62 53 74 61 74 69 63 5b 32 5d 3b 20 20 20  aDbStatic[2];   
58550 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
58560 61 74 69 63 20 73 70 61 63 65 20 66 6f 72 20 74  atic space for t
58570 68 65 20 32 20 64 65 66 61 75 6c 74 20 62 61 63  he 2 default bac
58580 6b 65 6e 64 73 20 2a 2f 0a 23 69 66 64 65 66 20  kends */.#ifdef 
58590 53 51 4c 49 54 45 5f 53 53 45 0a 20 20 73 71 6c  SQLITE_SSE.  sql
585a0 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 65 74 63  ite3_stmt *pFetc
585b0 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  h;         /* Us
585c0 65 64 20 62 79 20 53 53 45 20 74 6f 20 66 65 74  ed by SSE to fet
585d0 63 68 20 73 74 6f 72 65 64 20 73 74 61 74 65 6d  ch stored statem
585e0 65 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  ents */.#endif. 
585f0 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76   Savepoint *pSav
58600 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f  epoint;        /
58610 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65  * List of active
58620 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20   savepoints */. 
58630 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b   int nSavepoint;
58640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
58650 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d  * Number of non-
58660 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65  transaction save
58670 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  points */.  int 
58680 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20  nStatement;     
58690 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
586a0 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73 74  ber of nested st
586b0 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74  atement-transact
586c0 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20 69 73  ions  */.  u8 is
586d0 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
586e0 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75 65  oint;    /* True
586f0 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73   if the outermos
58700 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61  t savepoint is a
58710 20 54 53 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53   TS */..#ifdef S
58720 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c  QLITE_ENABLE_UNL
58730 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f 2a 20  OCK_NOTIFY.  /* 
58740 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
58750 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c 6c 20  riables are all 
58760 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65  protected by the
58770 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 0a   STATIC_MASTER .
58780 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f 74 20    ** mutex, not 
58790 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78  by sqlite3.mutex
587a0 2e 20 54 68 65 79 20 61 72 65 20 75 73 65 64 20  . They are used 
587b0 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66  by code in notif
587c0 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  y.c. .  **.  ** 
587d0 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b 43 6f  When X.pUnlockCo
587e0 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61  nnection==Y, tha
587f0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 58 20 69  t means that X i
58800 73 20 77 61 69 74 69 6e 67 20 66 6f 72 20 59 20  s waiting for Y 
58810 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b 20 73  to.  ** unlock s
58820 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 70 72  o that it can pr
58830 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  oceed..  **.  **
58840 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 6b 69 6e   When X.pBlockin
58850 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20  gConnection==Y, 
58860 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
58870 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 58  something that X
58880 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 72 69 65   tried.  ** trie
58890 64 20 74 6f 20 64 6f 20 72 65 63 65 6e 74 6c 79  d to do recently
588a0 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 6e 20   failed with an 
588b0 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72  SQLITE_LOCKED er
588c0 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f 63 6b 73  ror due to locks
588d0 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 59 2e  .  ** held by Y.
588e0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  .  */.  sqlite3 
588f0 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  *pBlockingConnec
58900 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74  tion; /* Connect
58910 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65 64 20  ion that caused 
58920 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f  SQLITE_LOCKED */
58930 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55 6e 6c  .  sqlite3 *pUnl
58940 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20  ockConnection;  
58950 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e           /* Conn
58960 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63 68 20  ection to watch 
58970 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a 20 20  for unlock */.  
58980 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 72 67  void *pUnlockArg
58990 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
589a0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
589b0 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69  t to xUnlockNoti
589c0 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  fy */.  void (*x
589d0 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28 76 6f  UnlockNotify)(vo
589e0 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20 2f 2a  id **, int);  /*
589f0 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79 20 63   Unlock notify c
58a00 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 73 71 6c  allback */.  sql
58a10 69 74 65 33 20 2a 70 4e 65 78 74 42 6c 6f 63 6b  ite3 *pNextBlock
58a20 65 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  ed;        /* Ne
58a30 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 61 6c  xt in list of al
58a40 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  l blocked connec
58a50 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  tions */.#endif.
58a60 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72  };../*.** A macr
58a70 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68  o to discover th
58a80 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20  e encoding of a 
58a90 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 64 65  database..*/.#de
58aa0 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28 28 64  fine ENC(db) ((d
58ab0 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65  b)->aDb[0].pSche
58ac0 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20  ma->enc)../*.** 
58ad0 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
58ae0 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 2e 66  for the sqlite.f
58af0 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62 2e 66  lags and or Db.f
58b00 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a  lags fields..**.
58b10 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66 6c 61  ** On sqlite.fla
58b20 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 49  gs, the SQLITE_I
58b30 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d 65 61  nTrans value mea
58b40 6e 73 20 74 68 61 74 20 77 65 20 68 61 76 65 0a  ns that we have.
58b50 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20 42 45  ** executed a BE
58b60 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c 61 67  GIN.  On Db.flag
58b70 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e  s, SQLITE_InTran
58b80 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74 65 6d  s means a statem
58b90 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  ent.** transacti
58ba0 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f 6e 20  on is active on 
58bb0 74 68 61 74 20 70 61 72 74 69 63 75 6c 61 72 20  that particular 
58bc0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
58bd0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
58be0 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 20  _VdbeTrace      
58bf0 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 54  0x00000001  /* T
58c00 72 75 65 20 74 6f 20 74 72 61 63 65 20 56 44 42  rue to trace VDB
58c10 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23  E execution */.#
58c20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e  define SQLITE_In
58c30 54 72 61 6e 73 20 20 20 20 20 20 20 20 30 78 30  Trans        0x0
58c40 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72 75 65  0000008  /* True
58c50 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73 61 63   if in a transac
58c60 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tion */.#define 
58c70 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
58c80 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 31 30  nges  0x00000010
58c90 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 65 64    /* Uncommitted
58ca0 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 61 6e   Hash table chan
58cb0 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ges */.#define S
58cc0 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d  QLITE_FullColNam
58cd0 65 73 20 20 20 30 78 30 30 30 30 30 30 32 30 20  es   0x00000020 
58ce0 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 63 6f   /* Show full co
58cf0 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 53 45  lumn names on SE
58d00 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e 65 20  LECT */.#define 
58d10 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
58d20 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 34 30  ames  0x00000040
58d30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 74 20    /* Show short 
58d40 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f  columns names */
58d50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
58d60 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 20 30  CountRows      0
58d70 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 43 6f  x00000080  /* Co
58d80 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 65 64  unt rows changed
58d90 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f 0a 20   by INSERT, */. 
58da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 44 45           /*   DE
58dd0 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20  LETE, or UPDATE 
58de0 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20  and return */.  
58df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58e10 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 68 65          /*   the
58e20 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 20 63   count using a c
58e30 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 65 66  allback. */.#def
58e40 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43  ine SQLITE_NullC
58e50 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 30 30  allback   0x0000
58e60 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  0100  /* Invoke 
58e70 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 63  the callback onc
58e80 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 20 20  e if the */.    
58e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58eb0 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 75 6c        /*   resul
58ec0 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 20 2a  t set is empty *
58ed0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
58ee0 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 20 20  _SqlTrace       
58ef0 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 44  0x00000200  /* D
58f00 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c 20 61  ebug print SQL a
58f10 73 20 69 74 20 65 78 65 63 75 74 65 73 20 2a 2f  s it executes */
58f20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
58f30 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 20 30  VdbeListing    0
58f40 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 44 65  x00000400  /* De
58f50 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f 66 20  bug listings of 
58f60 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 2a 2f  VDBE programs */
58f70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
58f80 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 20 30  WriteSchema    0
58f90 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 4f 4b  x00000800  /* OK
58fa0 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c 49 54   to update SQLIT
58fb0 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64 65 66  E_MASTER */.#def
58fc0 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61  ine SQLITE_NoRea
58fd0 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30 30 30  dlock     0x0000
58fe0 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c 6f 63  1000  /* Readloc
58ff0 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77  ks are omitted w
59000 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20  hen .           
59010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
59030 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65 61 64  * accessing read
59040 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 73 20  -only databases 
59050 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
59060 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 20 20  E_IgnoreChecks  
59070 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20   0x00002000  /* 
59080 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65 20 63  Do not enforce c
59090 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73  heck constraints
590a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
590b0 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
590c0 65 64 20 30 78 30 30 30 30 34 30 30 30 20 2f 2a  ed 0x00004000 /*
590d0 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68   For shared-cach
590e0 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e  e mode */.#defin
590f0 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46  e SQLITE_LegacyF
59100 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30 38 30  ileFmt  0x000080
59110 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65  00  /* Create ne
59120 77 20 64 61 74 61 62 61 73 65 73 20 69 6e 20 66  w databases in f
59130 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65 66 69  ormat 1 */.#defi
59140 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53  ne SQLITE_FullFS
59150 79 6e 63 20 20 20 20 20 20 30 78 30 30 30 31 30  ync      0x00010
59160 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75 6c 6c  000  /* Use full
59170 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20 62 61   fsync on the ba
59180 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65  ckend */.#define
59190 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65   SQLITE_LoadExte
591a0 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30 30 30  nsion  0x0002000
591b0 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 6f 61  0  /* Enable loa
591c0 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a  d_extension */..
591d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
591e0 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20 30 78  ecoveryMode   0x
591f0 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49 67 6e  00040000  /* Ign
59200 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72 6f 72  ore schema error
59210 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
59220 49 54 45 5f 53 68 61 72 65 64 43 61 63 68 65 20  ITE_SharedCache 
59230 20 20 20 30 78 30 30 30 38 30 30 30 30 20 20 2f     0x00080000  /
59240 2a 20 43 61 63 68 65 20 73 68 61 72 69 6e 67 20  * Cache sharing 
59250 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 64  is enabled */.#d
59260 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 6d  efine SQLITE_Com
59270 6d 69 74 42 75 73 79 20 20 20 20 20 30 78 30 30  mitBusy     0x00
59280 32 30 30 30 30 30 20 20 2f 2a 20 49 6e 20 74 68  200000  /* In th
59290 65 20 70 72 6f 63 65 73 73 20 6f 66 20 63 6f 6d  e process of com
592a0 6d 69 74 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69  mitting */.#defi
592b0 6e 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73  ne SQLITE_Revers
592c0 65 4f 72 64 65 72 20 20 20 30 78 30 30 34 30 30  eOrder   0x00400
592d0 30 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20  000  /* Reverse 
592e0 75 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54  unordered SELECT
592f0 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73  s */../*.** Poss
59300 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20  ible values for 
59310 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63  the sqlite.magic
59320 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65 20 6e   field..** The n
59330 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69  umbers are obtai
59340 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e  ned at random an
59350 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63 69 61  d have no specia
59360 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68 65 72  l meaning, other
59370 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67 20 64  .** than being d
59380 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f 6e 65  istinct from one
59390 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23 64 65   another..*/.#de
593a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49  fine SQLITE_MAGI
593b0 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61 30 32  C_OPEN     0xa02
593c0 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61 62 61  9a697  /* Databa
593d0 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a 23 64  se is open */.#d
593e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
593f0 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78 39 66  IC_CLOSED   0x9f
59400 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74 61 62  3c2d33  /* Datab
59410 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 2a 2f  ase is closed */
59420 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
59430 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20 20 30  MAGIC_SICK     0
59440 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20 45 72  x4b771290  /* Er
59450 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69 6e 67  ror and awaiting
59460 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 69 6e   close */.#defin
59470 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  e SQLITE_MAGIC_B
59480 55 53 59 20 20 20 20 20 30 78 66 30 33 62 37 39  USY     0xf03b79
59490 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  06  /* Database 
594a0 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
594b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
594c0 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 20 20  TE_MAGIC_ERROR  
594d0 20 20 30 78 62 35 33 35 37 39 33 30 20 20 2f 2a    0xb5357930  /*
594e0 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53   An SQLITE_MISUS
594f0 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  E error occurred
59500 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20   */../*.** Each 
59510 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  SQL function is 
59520 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e  defined by an in
59530 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
59540 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
59550 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74 65 72  ture.  A pointer
59560 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
59570 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  re is stored in 
59580 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75 6e 63  the sqlite.aFunc
59590 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20  .** hash table. 
595a0 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 66   When multiple f
595b0 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 74 68  unctions have th
595c0 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 68 65  e same name, the
595d0 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 70   hash table.** p
595e0 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e 6b 65  oints to a linke
595f0 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20  d list of these 
59600 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73  structures..*/.s
59610 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 7b 0a  truct FuncDef {.
59620 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20 20 20    i16 nArg;     
59630 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
59640 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   of arguments.  
59650 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d 69 74  -1 means unlimit
59660 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72 65 66  ed */.  u8 iPref
59670 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Enc;         /* 
59680 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  Preferred text e
59690 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54 45 5f  ncoding (SQLITE_
596a0 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36 42 45  UTF8, 16LE, 16BE
596b0 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 3b  ) */.  u8 flags;
596c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
596d0 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  ome combination 
596e0 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 2a  of SQLITE_FUNC_*
596f0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65   */.  void *pUse
59700 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 55 73  rData;     /* Us
59710 65 72 20 64 61 74 61 20 70 61 72 61 6d 65 74 65  er data paramete
59720 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a  r */.  FuncDef *
59730 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e  pNext;      /* N
59740 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  ext function wit
59750 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20  h same name */. 
59760 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
59770 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
59780 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
59790 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c 61 72  e**); /* Regular
597a0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
597b0 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
597c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
597d0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
597e0 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61 74 65  *); /* Aggregate
597f0 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69 64 20   step */.  void 
59800 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 71 6c  (*xFinalize)(sql
59810 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 20  ite3_context*); 
59820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
59830 2a 20 41 67 67 72 65 67 61 74 65 20 66 69 6e 61  * Aggregate fina
59840 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  lizer */.  char 
59850 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
59860 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f 66 20 74  /* SQL name of t
59870 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a  he function. */.
59880 20 20 46 75 6e 63 44 65 66 20 2a 70 48 61 73 68    FuncDef *pHash
59890 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 77  ;      /* Next w
598a0 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20  ith a different 
598b0 6e 61 6d 65 20 62 75 74 20 74 68 65 20 73 61 6d  name but the sam
598c0 65 20 68 61 73 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  e hash */.};../*
598d0 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c  .** Possible val
598e0 75 65 73 20 66 6f 72 20 46 75 6e 63 44 65 66 2e  ues for FuncDef.
598f0 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65  flags.*/.#define
59900 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
59910 45 20 20 20 20 20 30 78 30 31 20 2f 2a 20 43 61  E     0x01 /* Ca
59920 6e 64 69 64 61 74 65 20 66 6f 72 20 74 68 65 20  ndidate for the 
59930 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f  LIKE optimizatio
59940 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
59950 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 20 20 20  ITE_FUNC_CASE   
59960 20 20 30 78 30 32 20 2f 2a 20 43 61 73 65 2d 73    0x02 /* Case-s
59970 65 6e 73 69 74 69 76 65 20 4c 49 4b 45 2d 74 79  ensitive LIKE-ty
59980 70 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23  pe function */.#
59990 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
599a0 4e 43 5f 45 50 48 45 4d 20 20 20 20 30 78 30 34  NC_EPHEM    0x04
599b0 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c 2e 20 20   /* Ephemeral.  
599c0 44 65 6c 65 74 65 20 77 69 74 68 20 56 44 42 45  Delete with VDBE
599d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
599e0 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
599f0 20 30 78 30 38 20 2f 2a 20 73 71 6c 69 74 65 33   0x08 /* sqlite3
59a00 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29  GetFuncCollSeq()
59a10 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64   might be called
59a20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
59a30 54 45 5f 46 55 4e 43 5f 50 52 49 56 41 54 45 20  TE_FUNC_PRIVATE 
59a40 20 30 78 31 30 20 2f 2a 20 41 6c 6c 6f 77 65 64   0x10 /* Allowed
59a50 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
59a60 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  e only */.#defin
59a70 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f  e SQLITE_FUNC_CO
59a80 55 4e 54 20 20 20 20 30 78 32 30 20 2f 2a 20 42  UNT    0x20 /* B
59a90 75 69 6c 74 2d 69 6e 20 63 6f 75 6e 74 28 2a 29  uilt-in count(*)
59aa0 20 61 67 67 72 65 67 61 74 65 20 2a 2f 0a 0a 2f   aggregate */../
59ab0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
59ac0 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f 73 2c  ng three macros,
59ad0 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c 49 4b   FUNCTION(), LIK
59ae0 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 47 52  EFUNC() and AGGR
59af0 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a 20 75  EGATE() are.** u
59b00 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  sed to create th
59b10 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 20 66  e initializers f
59b20 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 20 73  or the FuncDef s
59b30 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a  tructures..**.**
59b40 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d     FUNCTION(zNam
59b50 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62  e, nArg, iArg, b
59b60 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 20 20  NC, xFunc).**   
59b70 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65    Used to create
59b80 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69   a scalar functi
59b90 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  on definition of
59ba0 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d   a function zNam
59bb0 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c 65 6d  e .**     implem
59bc0 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e 63 74  ented by C funct
59bd0 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 20 61  ion xFunc that a
59be0 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75  ccepts nArg argu
59bf0 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 20 20  ments. The.**   
59c00 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61    value passed a
59c10 73 20 69 41 72 67 20 69 73 20 63 61 73 74 20 74  s iArg is cast t
59c20 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e 64 20  o a (void*) and 
59c30 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a  made available.*
59c40 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 73 65  *     as the use
59c50 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f  r-data (sqlite3_
59c60 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 6f 72  user_data()) for
59c70 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 49   the function. I
59c80 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 6d 65  f .**     argume
59c90 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 2c 20  nt bNC is true, 
59ca0 74 68 65 6e 20 74 68 65 20 46 75 6e 63 44 65 66  then the FuncDef
59cb0 2e 6e 65 65 64 43 6f 6c 6c 61 74 65 20 66 6c 61  .needCollate fla
59cc0 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20  g is set..**.** 
59cd0 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d    AGGREGATE(zNam
59ce0 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62  e, nArg, iArg, b
59cf0 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  NC, xStep, xFina
59d00 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74  l).**     Used t
59d10 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67 72  o create an aggr
59d20 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 64  egate function d
59d30 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65 6d  efinition implem
59d40 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 20 20 20  ented by.**     
59d50 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
59d60 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c  xStep and xFinal
59d70 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f 75 72  . The first four
59d80 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 20   parameters.**  
59d90 20 20 20 61 72 65 20 69 6e 74 65 72 70 72 65 74     are interpret
59da0 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 77  ed in the same w
59db0 61 79 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ay as the first 
59dc0 34 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a  4 parameters to.
59dd0 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  **     FUNCTION(
59de0 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46  )..**.**   LIKEF
59df0 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c  UNC(zName, nArg,
59e00 20 70 41 72 67 2c 20 66 6c 61 67 73 29 0a 2a 2a   pArg, flags).**
59e10 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65       Used to cre
59e20 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 6e  ate a scalar fun
59e30 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
59e40 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a   of a function z
59e50 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 74 68 61  Name .**     tha
59e60 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 61  t accepts nArg a
59e70 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 69 73 20  rguments and is 
59e80 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 61  implemented by a
59e90 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20 20   call to C .**  
59ea0 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65     function like
59eb0 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e 74 20 70  Func. Argument p
59ec0 41 72 67 20 69 73 20 63 61 73 74 20 74 6f 20 61  Arg is cast to a
59ed0 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 20 6d 61   (void *) and ma
59ee0 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 69 6c 61  de.**     availa
59ef0 62 6c 65 20 61 73 20 74 68 65 20 66 75 6e 63 74  ble as the funct
59f00 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 20 28 73  ion user-data (s
59f10 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
59f20 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20  ()). The.**     
59f30 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 20 76 61  FuncDef.flags va
59f40 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  riable is set to
59f50 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65   the value passe
59f60 64 20 61 73 20 74 68 65 20 66 6c 61 67 73 0a 2a  d as the flags.*
59f70 2a 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 2e  *     parameter.
59f80 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e 43  .*/.#define FUNC
59f90 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67  TION(zName, nArg
59fa0 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75  , iArg, bNC, xFu
59fb0 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53  nc) \.  {nArg, S
59fc0 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a  QLITE_UTF8, bNC*
59fd0 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f  8, SQLITE_INT_TO
59fe0 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78  _PTR(iArg), 0, x
59ff0 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61  Func, 0, 0, #zNa
5a000 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 53  me, 0}.#define S
5a010 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d  TR_FUNCTION(zNam
5a020 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 62  e, nArg, pArg, b
5a030 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b  NC, xFunc) \.  {
5a040 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
5a050 38 2c 20 62 4e 43 2a 38 2c 20 70 41 72 67 2c 20  8, bNC*8, pArg, 
5a060 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20  0, xFunc, 0, 0, 
5a070 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69  #zName, 0}.#defi
5a080 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d  ne LIKEFUNC(zNam
5a090 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 66 6c  e, nArg, arg, fl
5a0a0 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20  ags) \.  {nArg, 
5a0b0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 66 6c 61  SQLITE_UTF8, fla
5a0c0 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 72 67 2c  gs, (void *)arg,
5a0d0 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c   0, likeFunc, 0,
5a0e0 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23   0, #zName, 0}.#
5a0f0 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 45  define AGGREGATE
5a100 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72  (zName, nArg, ar
5a110 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 46  g, nc, xStep, xF
5a120 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 2c  inal) \.  {nArg,
5a130 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63   SQLITE_UTF8, nc
5a140 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  *8, SQLITE_INT_T
5a150 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 30  O_PTR(arg), 0, 0
5a160 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 23  , xStep,xFinal,#
5a170 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a 2a 20  zName,0}../*.** 
5a180 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 76 65  All current save
5a190 70 6f 69 6e 74 73 20 61 72 65 20 73 74 6f 72 65  points are store
5a1a0 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d in a linked li
5a1b0 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 0a 2a  st starting at.*
5a1c0 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70  * sqlite3.pSavep
5a1d0 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74 20  oint. The first 
5a1e0 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 6c  element in the l
5a1f0 69 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74 20  ist is the most 
5a200 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 65 6e  recently.** open
5a210 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53 61  ed savepoint. Sa
5a220 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64 64  vepoints are add
5a230 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20 62  ed to the list b
5a240 79 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 4f 50  y the vdbe.** OP
5a250 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72  _Savepoint instr
5a260 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63  uction..*/.struc
5a270 74 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20  t Savepoint {.  
5a280 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
5a290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2a0 20 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 6e 74      /* Savepoint
5a2b0 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 6d 69   name (nul-termi
5a2c0 6e 61 74 65 64 29 20 2a 2f 0a 20 20 53 61 76 65  nated) */.  Save
5a2d0 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20 20  point *pNext;   
5a2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2f0 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70 6f  /* Parent savepo
5a300 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a  int (if any) */.
5a310 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  };../*.** The fo
5a320 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65 64  llowing are used
5a330 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   as the second p
5a340 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
5a350 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c 0a  te3Savepoint(),.
5a360 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50 31  ** and as the P1
5a370 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
5a380 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e   OP_Savepoint in
5a390 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64  struction..*/.#d
5a3a0 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f  efine SAVEPOINT_
5a3b0 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64 65  BEGIN      0.#de
5a3c0 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52  fine SAVEPOINT_R
5a3d0 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65 66  ELEASE    1.#def
5a3e0 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  ine SAVEPOINT_RO
5a3f0 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a  LLBACK   2.../*.
5a400 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d  ** Each SQLite m
5a410 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74  odule (virtual t
5a420 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29  able definition)
5a430 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61   is defined by a
5a440 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  n.** instance of
5a450 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
5a460 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64  tructure, stored
5a470 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e   in the sqlite3.
5a480 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20  aModule.** hash 
5a490 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  table..*/.struct
5a4a0 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73   Module {.  cons
5a4b0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
5a4c0 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20   *pModule;      
5a4d0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69   /* Callback poi
5a4e0 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  nters */.  const
5a4f0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
5a500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a510 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74  /* Name passed t
5a520 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28  o create_module(
5a530 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75  ) */.  void *pAu
5a540 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
5a550 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
5a560 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 63 72  Aux passed to cr
5a570 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f  eate_module() */
5a580 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72  .  void (*xDestr
5a590 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20  oy)(void *);    
5a5a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c          /* Modul
5a5b0 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
5a5c0 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ction */.};../*.
5a5d0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ** information a
5a5e0 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e  bout each column
5a5f0 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65   of an SQL table
5a600 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69   is held in an i
5a610 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68  nstance.** of th
5a620 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  is structure..*/
5a630 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b  .struct Column {
5a640 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
5a650 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
5a660 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  his column */.  
5a670 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20  Expr *pDflt;    
5a680 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75   /* Default valu
5a690 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e  e of this column
5a6a0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 70   */.  char *zTyp
5a6b0 65 3b 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74  e;     /* Data t
5a6c0 79 70 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c  ype for this col
5a6d0 75 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  umn */.  char *z
5a6e0 43 6f 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c  Coll;     /* Col
5a6f0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e  lating sequence.
5a700 20 20 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74    If NULL, use t
5a710 68 65 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20  he default */.  
5a720 75 38 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20  u8 notNull;     
5a730 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72   /* True if ther
5a740 65 20 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20  e is a NOT NULL 
5a750 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  constraint */.  
5a760 75 38 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 20  u8 isPrimKey;   
5a770 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
5a780 20 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20   column is part 
5a790 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b  of the PRIMARY K
5a7a0 45 59 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66  EY */.  char aff
5a7b0 69 6e 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20  inity;   /* One 
5a7c0 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46  of the SQLITE_AF
5a7d0 46 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a  F_... values */.
5a7e0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
5a7f0 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
5a800 0a 20 20 75 38 20 69 73 48 69 64 64 65 6e 3b 20  .  u8 isHidden; 
5a810 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
5a820 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68  his column is 'h
5a830 69 64 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66  idden' */.#endif
5a840 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f  .};../*.** A "Co
5a850 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65  llating Sequence
5a860 22 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20  " is defined by 
5a870 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
5a880 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
5a890 73 74 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 65  structure. Conce
5a8a0 70 74 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61  ptually, a colla
5a8b0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f  ting sequence co
5a8c0 6e 73 69 73 74 73 20 6f 66 20 61 20 6e 61 6d 65  nsists of a name
5a8d0 20 61 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72   and.** a compar
5a8e0 69 73 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 61  ison routine tha
5a8f0 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f 72  t defines the or
5a900 64 65 72 20 6f 66 20 74 68 61 74 20 73 65 71 75  der of that sequ
5a910 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  ence..**.** Ther
5a920 65 20 6d 61 79 20 74 77 6f 20 73 65 70 61 72 61  e may two separa
5a930 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  te implementatio
5a940 6e 73 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74  ns of the collat
5a950 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e  ion function, on
5a960 65 0a 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73  e.** that proces
5a970 73 65 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d  ses text in UTF-
5a980 38 20 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c  8 encoding (Coll
5a990 53 65 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e  Seq.xCmp) and an
5a9a0 6f 74 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 72  other that.** pr
5a9b0 6f 63 65 73 73 65 73 20 74 65 78 74 20 65 6e 63  ocesses text enc
5a9c0 6f 64 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28  oded in UTF-16 (
5a9d0 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c  CollSeq.xCmp16),
5a9e0 20 75 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69   using the machi
5a9f0 6e 65 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 74  ne.** native byt
5aa00 65 20 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 20  e order. When a 
5aa10 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
5aa20 63 65 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53  ce is invoked, S
5aa30 51 4c 69 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a  QLite selects.**
5aa40 20 74 68 65 20 76 65 72 73 69 6f 6e 20 74 68 61   the version tha
5aa50 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 74  t will require t
5aa60 68 65 20 6c 65 61 73 74 20 65 78 70 65 6e 73 69  he least expensi
5aa70 76 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74  ve encoding.** t
5aa80 72 61 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20  ranslations, if 
5aa90 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43  any..**.** The C
5aaa0 6f 6c 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 6d  ollSeq.pUser mem
5aab0 62 65 72 20 76 61 72 69 61 62 6c 65 20 69 73 20  ber variable is 
5aac0 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d 65 74  an extra paramet
5aad0 65 72 20 74 68 61 74 20 70 61 73 73 65 64 20 69  er that passed i
5aae0 6e 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73  n.** as the firs
5aaf0 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
5ab00 65 20 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 73  e UTF-8 comparis
5ab10 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d  on function, xCm
5ab20 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55  p..** CollSeq.pU
5ab30 73 65 72 31 36 20 69 73 20 74 68 65 20 65 71 75  ser16 is the equ
5ab40 69 76 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20  ivalent for the 
5ab50 55 54 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f  UTF-16 compariso
5ab60 6e 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78  n function,.** x
5ab70 43 6d 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  Cmp16..**.** If 
5ab80 62 6f 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d  both CollSeq.xCm
5ab90 70 20 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43  p and CollSeq.xC
5aba0 6d 70 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69  mp16 are NULL, i
5abb0 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
5abc0 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  .** collating se
5abd0 71 75 65 6e 63 65 20 69 73 20 75 6e 64 65 66 69  quence is undefi
5abe0 6e 65 64 2e 20 20 49 6e 64 69 63 65 73 20 62 75  ned.  Indices bu
5abf0 69 6c 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69  ilt on an undefi
5ac00 6e 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67  ned.** collating
5ac10 20 73 65 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f   sequence may no
5ac20 74 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69  t be read or wri
5ac30 74 74 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  tten..*/.struct 
5ac40 43 6f 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 72  CollSeq {.  char
5ac50 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
5ac60 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
5ac70 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5ac80 6e 63 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64  nce, UTF-8 encod
5ac90 65 64 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20  ed */.  u8 enc; 
5aca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5acb0 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68   Text encoding h
5acc0 61 6e 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29  andled by xCmp()
5acd0 20 2a 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 20   */.  u8 type;  
5ace0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
5acf0 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ne of the SQLITE
5ad00 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73  _COLL_... values
5ad10 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64   below */.  void
5ad20 20 2a 70 55 73 65 72 3b 20 20 20 20 20 20 20 20   *pUser;        
5ad30 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d    /* First argum
5ad40 65 6e 74 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f  ent to xCmp() */
5ad50 0a 20 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 76  .  int (*xCmp)(v
5ad60 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20  oid*,int, const 
5ad70 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  void*, int, cons
5ad80 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  t void*);.  void
5ad90 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b   (*xDel)(void*);
5ada0 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20    /* Destructor 
5adb0 66 6f 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a  for pUser */.};.
5adc0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
5add0 61 6c 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71  alues of CollSeq
5ade0 2e 74 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e  .type:.*/.#defin
5adf0 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49  e SQLITE_COLL_BI
5ae00 4e 41 52 59 20 20 31 20 20 2f 2a 20 54 68 65 20  NARY  1  /* The 
5ae10 64 65 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29  default memcmp()
5ae20 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5ae30 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nce */.#define S
5ae40 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53  QLITE_COLL_NOCAS
5ae50 45 20 20 32 20 20 2f 2a 20 54 68 65 20 62 75 69  E  2  /* The bui
5ae60 6c 74 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c  lt-in NOCASE col
5ae70 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
5ae80 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5ae90 45 5f 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33  E_COLL_REVERSE 3
5aea0 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69    /* The built-i
5aeb0 6e 20 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 74  n REVERSE collat
5aec0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
5aed0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
5aee0 4f 4c 4c 5f 55 53 45 52 20 20 20 20 30 20 20 2f  OLL_USER    0  /
5aef0 2a 20 41 6e 79 20 6f 74 68 65 72 20 75 73 65 72  * Any other user
5af00 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69  -defined collati
5af10 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a  ng sequence */..
5af20 2f 2a 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64  /*.** A sort ord
5af30 65 72 20 63 61 6e 20 62 65 20 65 69 74 68 65 72  er can be either
5af40 20 41 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f   ASC or DESC..*/
5af50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5af60 53 4f 5f 41 53 43 20 20 20 20 20 20 20 30 20 20  SO_ASC       0  
5af70 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e  /* Sort in ascen
5af80 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64  ding order */.#d
5af90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f  efine SQLITE_SO_
5afa0 44 45 53 43 20 20 20 20 20 20 31 20 20 2f 2a 20  DESC      1  /* 
5afb0 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e  Sort in ascendin
5afc0 67 20 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a  g order */../*.*
5afd0 2a 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  * Column affinit
5afe0 79 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  y types..**.** T
5aff0 68 65 73 65 20 75 73 65 64 20 74 6f 20 68 61 76  hese used to hav
5b000 65 20 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20  e mnemonic name 
5b010 6c 69 6b 65 20 27 69 27 20 66 6f 72 20 53 51 4c  like 'i' for SQL
5b020 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20  ITE_AFF_INTEGER 
5b030 61 6e 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53  and.** 't' for S
5b040 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2e 20  QLITE_AFF_TEXT. 
5b050 20 42 75 74 20 77 65 20 63 61 6e 20 73 61 76 65   But we can save
5b060 20 61 20 6c 69 74 74 6c 65 20 73 70 61 63 65 20   a little space 
5b070 61 6e 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74  and improve.** t
5b080 68 65 20 73 70 65 65 64 20 61 20 6c 69 74 74 6c  he speed a littl
5b090 65 20 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 74  e by numbering t
5b0a0 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63  he values consec
5b0b0 75 74 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a  utively.  .**.**
5b0c0 20 42 75 74 20 72 61 74 68 65 72 20 74 68 61 6e   But rather than
5b0d0 20 73 74 61 72 74 20 77 69 74 68 20 30 20 6f 72   start with 0 or
5b0e0 20 31 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74   1, we begin wit
5b0f0 68 20 27 61 27 2e 20 20 54 68 61 74 20 77 61 79  h 'a'.  That way
5b100 2c 0a 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70  ,.** when multip
5b110 6c 65 20 61 66 66 69 6e 69 74 79 20 74 79 70 65  le affinity type
5b120 73 20 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74  s are concatenat
5b130 65 64 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67  ed into a string
5b140 20 61 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20   and.** used as 
5b150 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20  the P4 operand, 
5b160 74 68 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72  they will be mor
5b170 65 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a  e readable..**.*
5b180 2a 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74  * Note also that
5b190 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70   the numeric typ
5b1a0 65 73 20 61 72 65 20 67 72 6f 75 70 65 64 20 74  es are grouped t
5b1b0 6f 67 65 74 68 65 72 20 73 6f 20 74 68 61 74 20  ogether so that 
5b1c0 74 65 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61  testing.** for a
5b1d0 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 69 73   numeric type is
5b1e0 20 61 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72   a single compar
5b1f0 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ison..*/.#define
5b200 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
5b210 20 20 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65       'a'.#define
5b220 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
5b230 20 20 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65       'b'.#define
5b240 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45   SQLITE_AFF_NUME
5b250 52 49 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65  RIC  'c'.#define
5b260 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
5b270 47 45 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65  GER  'd'.#define
5b280 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
5b290 20 20 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e       'e'..#defin
5b2a0 65 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72  e sqlite3IsNumer
5b2b0 69 63 41 66 66 69 6e 69 74 79 28 58 29 20 20 28  icAffinity(X)  (
5b2c0 28 58 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f  (X)>=SQLITE_AFF_
5b2d0 4e 55 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20  NUMERIC)../*.** 
5b2e0 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d  The SQLITE_AFF_M
5b2f0 41 53 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73  ASK values masks
5b300 20 6f 66 66 20 74 68 65 20 73 69 67 6e 69 66 69   off the signifi
5b310 63 61 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a  cant bits of an.
5b320 2a 2a 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75  ** affinity valu
5b330 65 2e 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  e. .*/.#define S
5b340 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20  QLITE_AFF_MASK  
5b350 20 20 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41     0x67../*.** A
5b360 64 64 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61  dditional bit va
5b370 6c 75 65 73 20 74 68 61 74 20 63 61 6e 20 62 65  lues that can be
5b380 20 4f 52 65 64 20 77 69 74 68 20 61 6e 20 61 66   ORed with an af
5b390 66 69 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a  finity without.*
5b3a0 2a 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61  * changing the a
5b3b0 66 66 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66  ffinity..*/.#def
5b3c0 69 6e 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ine SQLITE_JUMPI
5b3d0 46 4e 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a  FNULL   0x08  /*
5b3e0 20 6a 75 6d 70 73 20 69 66 20 65 69 74 68 65 72   jumps if either
5b3f0 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
5b400 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5b410 54 45 5f 53 54 4f 52 45 50 32 20 20 20 20 20 20  TE_STOREP2      
5b420 30 78 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 72  0x10  /* Store r
5b430 65 73 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d  esult in reg[P2]
5b440 20 72 61 74 68 65 72 20 74 68 61 6e 20 6a 75 6d   rather than jum
5b450 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  p */../*.** Each
5b460 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20 72 65   SQL table is re
5b470 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d  presented in mem
5b480 6f 72 79 20 62 79 20 61 6e 20 69 6e 73 74 61 6e  ory by an instan
5b490 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c  ce of the.** fol
5b4a0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
5b4b0 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e  ..**.** Table.zN
5b4c0 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ame is the name 
5b4d0 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54  of the table.  T
5b4e0 68 65 20 63 61 73 65 20 6f 66 20 74 68 65 20 6f  he case of the o
5b4f0 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 54  riginal.** CREAT
5b500 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
5b510 74 20 69 73 20 73 74 6f 72 65 64 2c 20 62 75 74  t is stored, but
5b520 20 63 61 73 65 20 69 73 20 6e 6f 74 20 73 69 67   case is not sig
5b530 6e 69 66 69 63 61 6e 74 20 66 6f 72 0a 2a 2a 20  nificant for.** 
5b540 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a  comparisons..**.
5b550 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 73  ** Table.nCol is
5b560 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
5b570 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74  olumns in this t
5b580 61 62 6c 65 2e 20 20 54 61 62 6c 65 2e 61 43 6f  able.  Table.aCo
5b590 6c 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65  l is a.** pointe
5b5a0 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  r to an array of
5b5b0 20 43 6f 6c 75 6d 6e 20 73 74 72 75 63 74 75 72   Column structur
5b5c0 65 73 2c 20 6f 6e 65 20 66 6f 72 20 65 61 63 68  es, one for each
5b5d0 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49   column..**.** I
5b5e0 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  f the table has 
5b5f0 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
5b600 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 54 61 62  RY KEY, then Tab
5b610 6c 65 2e 69 50 4b 65 79 20 69 73 20 74 68 65 20  le.iPKey is the 
5b620 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20  index of.** the 
5b630 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74  column that is t
5b640 68 61 74 20 6b 65 79 2e 20 20 20 4f 74 68 65 72  hat key.   Other
5b650 77 69 73 65 20 54 61 62 6c 65 2e 69 50 4b 65 79  wise Table.iPKey
5b660 20 69 73 20 6e 65 67 61 74 69 76 65 2e 20 20 4e   is negative.  N
5b670 6f 74 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ote.** that the 
5b680 64 61 74 61 74 79 70 65 20 6f 66 20 74 68 65 20  datatype of the 
5b690 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74  PRIMARY KEY must
5b6a0 20 62 65 20 49 4e 54 45 47 45 52 20 66 6f 72 20   be INTEGER for 
5b6b0 74 68 69 73 20 66 69 65 6c 64 20 74 6f 0a 2a 2a  this field to.**
5b6c0 20 62 65 20 73 65 74 2e 20 20 41 6e 20 49 4e 54   be set.  An INT
5b6d0 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
5b6e0 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20   is used as the 
5b6f0 72 6f 77 69 64 20 66 6f 72 20 65 61 63 68 20 72  rowid for each r
5b700 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62  ow of.** the tab
5b710 6c 65 2e 20 20 49 66 20 61 20 74 61 62 6c 65 20  le.  If a table 
5b720 68 61 73 20 6e 6f 20 49 4e 54 45 47 45 52 20 50  has no INTEGER P
5b730 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
5b740 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 0a   a random rowid.
5b750 2a 2a 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  ** is generated 
5b760 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20  for each row of 
5b770 74 68 65 20 74 61 62 6c 65 2e 20 20 54 46 5f 48  the table.  TF_H
5b780 61 73 50 72 69 6d 61 72 79 4b 65 79 20 69 73 20  asPrimaryKey is 
5b790 73 65 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61  set if.** the ta
5b7a0 62 6c 65 20 68 61 73 20 61 6e 79 20 50 52 49 4d  ble has any PRIM
5b7b0 41 52 59 20 4b 45 59 2c 20 49 4e 54 45 47 45 52  ARY KEY, INTEGER
5b7c0 20 6f 72 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a   or otherwise..*
5b7d0 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20  *.** Table.tnum 
5b7e0 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  is the page numb
5b7f0 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20  er for the root 
5b800 42 54 72 65 65 20 70 61 67 65 20 6f 66 20 74 68  BTree page of th
5b810 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a  e table in the.*
5b820 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  * database file.
5b830 20 20 49 66 20 54 61 62 6c 65 2e 69 44 62 20 69    If Table.iDb i
5b840 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
5b850 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
5b860 65 20 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20  e backend.** in 
5b870 73 71 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30  sqlite.aDb[].  0
5b880 20 69 73 20 66 6f 72 20 74 68 65 20 6d 61 69 6e   is for the main
5b890 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 31 20   database and 1 
5b8a0 69 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20  is for the file 
5b8b0 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65  that.** holds te
5b8c0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61  mporary tables a
5b8d0 6e 64 20 69 6e 64 69 63 65 73 2e 20 20 49 66 20  nd indices.  If 
5b8e0 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 69 73 20  TF_Ephemeral is 
5b8f0 73 65 74 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  set.** then the 
5b900 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20  table is stored 
5b910 69 6e 20 61 20 66 69 6c 65 20 74 68 61 74 20 69  in a file that i
5b920 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
5b930 64 65 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20  deleted.** when 
5b940 74 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  the VDBE cursor 
5b950 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  to the table is 
5b960 63 6c 6f 73 65 64 2e 20 20 49 6e 20 74 68 69 73  closed.  In this
5b970 20 63 61 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d   case Table.tnum
5b980 20 0a 2a 2a 20 72 65 66 65 72 73 20 56 44 42 45   .** refers VDBE
5b990 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74   cursor number t
5b9a0 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 74 61  hat holds the ta
5b9b0 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f  ble open, not to
5b9c0 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67   the root.** pag
5b9d0 65 20 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e 73  e number.  Trans
5b9e0 69 65 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20  ient tables are 
5b9f0 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65  used to hold the
5ba00 20 72 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a   results of a.**
5ba10 20 73 75 62 2d 71 75 65 72 79 20 74 68 61 74 20   sub-query that 
5ba20 61 70 70 65 61 72 73 20 69 6e 73 74 65 61 64 20  appears instead 
5ba30 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20  of a real table 
5ba40 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  name in the FROM
5ba50 20 63 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61   clause .** of a
5ba60 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
5ba70 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62  t..*/.struct Tab
5ba80 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  le {.  sqlite3 *
5ba90 64 62 4d 65 6d 3b 20 20 20 20 20 20 2f 2a 20 44  dbMem;      /* D
5baa0 42 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 65  B connection use
5bab0 64 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20  d for lookaside 
5bac0 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 2a 2f 0a  allocations. */.
5bad0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
5bae0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
5baf0 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76  f the table or v
5bb00 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b  iew */.  int iPK
5bb10 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey;           /*
5bb20 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 76 65   If not negative
5bb30 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79  , use aCol[iPKey
5bb40 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79  ] as the primary
5bb50 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43   key */.  int nC
5bb60 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ol;            /
5bb70 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
5bb80 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c  mns in this tabl
5bb90 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61  e */.  Column *a
5bba0 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 49  Col;        /* I
5bbb0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
5bbc0 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   each column */.
5bbd0 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
5bbe0 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
5bbf0 66 20 53 51 4c 20 69 6e 64 65 78 65 73 20 6f 6e  f SQL indexes on
5bc00 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a   this table. */.
5bc10 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20    int tnum;     
5bc20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42         /* Root B
5bc30 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 74 68  Tree node for th
5bc40 69 73 20 74 61 62 6c 65 20 28 73 65 65 20 6e 6f  is table (see no
5bc50 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53  te above) */.  S
5bc60 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
5bc70 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20      /* NULL for 
5bc80 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20  tables.  Points 
5bc90 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 66  to definition if
5bca0 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 75 31   a view. */.  u1
5bcb0 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  6 nRef;         
5bcc0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5bcd0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73  pointers to this
5bce0 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 74   Table */.  u8 t
5bcf0 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  abFlags;        
5bd00 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a   /* Mask of TF_*
5bd10 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20   values */.  u8 
5bd20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20 20 20  keyConf;        
5bd30 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
5bd40 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71 75  in case of uniqu
5bd50 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f  eness conflict o
5bd60 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 46 4b 65  n iPKey */.  FKe
5bd70 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20  y *pFKey;       
5bd80 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74    /* Linked list
5bd90 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20   of all foreign 
5bda0 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74 61 62  keys in this tab
5bdb0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  le */.  char *zC
5bdc0 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 2f 2a 20  olAff;       /* 
5bdd0 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20  String defining 
5bde0 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
5bdf0 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23  each column */.#
5be00 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
5be10 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70 72 20  IT_CHECK.  Expr 
5be20 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20  *pCheck;        
5be30 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 61 6c  /* The AND of al
5be40 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  l CHECK constrai
5be50 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  nts */.#endif.#i
5be60 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
5be70 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 69  T_ALTERTABLE.  i
5be80 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b  nt addColOffset;
5be90 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
5bea0 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
5beb0 6d 74 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20  mt to add a new 
5bec0 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66  column */.#endif
5bed0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
5bee0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
5bef0 45 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  E.  Module *pMod
5bf00 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
5bf10 74 65 72 20 74 6f 20 74 68 65 20 69 6d 70 6c 65  ter to the imple
5bf20 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
5bf30 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71 6c   module */.  sql
5bf40 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
5bf50 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20  ; /* Pointer to 
5bf60 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e 73 74 61  the module insta
5bf70 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f  nce */.  int nMo
5bf80 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 2f 2a  duleArg;      /*
5bf90 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
5bfa0 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75  ents to the modu
5bfb0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
5bfc0 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20  zModuleArg;  /* 
5bfd0 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75  Text of all modu
5bfe0 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 73 20  le args. [0] is 
5bff0 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23  module name */.#
5c000 65 6e 64 69 66 0a 20 20 54 72 69 67 67 65 72 20  endif.  Trigger 
5c010 2a 70 54 72 69 67 67 65 72 3b 20 20 20 2f 2a 20  *pTrigger;   /* 
5c020 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73  List of triggers
5c030 20 73 74 6f 72 65 64 20 69 6e 20 70 53 63 68 65   stored in pSche
5c040 6d 61 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a  ma */.  Schema *
5c050 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20  pSchema;     /* 
5c060 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e 74  Schema that cont
5c070 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c 65 20  ains this table 
5c080 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 78  */.  Table *pNex
5c090 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e 65 78  tZombie;  /* Nex
5c0a0 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65 2e 70  t on the Parse.p
5c0b0 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74 20 2a  ZombieTab list *
5c0c0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  /.};../*.** Allo
5c0d0 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 54  wed values for T
5c0e0 61 62 65 2e 74 61 62 46 6c 61 67 73 2e 0a 2a 2f  abe.tabFlags..*/
5c0f0 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 65 61 64  .#define TF_Read
5c100 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 78 30 31  only        0x01
5c110 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79      /* Read-only
5c120 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f   system table */
5c130 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 70 68 65  .#define TF_Ephe
5c140 6d 65 72 61 6c 20 20 20 20 20 20 20 30 78 30 32  meral       0x02
5c150 20 20 20 20 2f 2a 20 41 6e 20 65 70 68 65 6d 65      /* An epheme
5c160 72 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65  ral table */.#de
5c170 66 69 6e 65 20 54 46 5f 48 61 73 50 72 69 6d 61  fine TF_HasPrima
5c180 72 79 4b 65 79 20 20 20 30 78 30 34 20 20 20 20  ryKey   0x04    
5c190 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 61 20 70  /* Table has a p
5c1a0 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 23 64  rimary key */.#d
5c1b0 65 66 69 6e 65 20 54 46 5f 41 75 74 6f 69 6e 63  efine TF_Autoinc
5c1c0 72 65 6d 65 6e 74 20 20 20 30 78 30 38 20 20 20  rement   0x08   
5c1d0 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 72 69 6d   /* Integer prim
5c1e0 61 72 79 20 6b 65 79 20 69 73 20 61 75 74 6f 69  ary key is autoi
5c1f0 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 64 65 66  ncrement */.#def
5c200 69 6e 65 20 54 46 5f 56 69 72 74 75 61 6c 20 20  ine TF_Virtual  
5c210 20 20 20 20 20 20 20 30 78 31 30 20 20 20 20 2f         0x10    /
5c220 2a 20 49 73 20 61 20 76 69 72 74 75 61 6c 20 74  * Is a virtual t
5c230 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  able */.#define 
5c240 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 74 61 20  TF_NeedMetadata 
5c250 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 61 43     0x20    /* aC
5c260 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e 64 20 61  ol[].zType and a
5c270 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73  Col[].pColl miss
5c280 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  ing */..../*.** 
5c290 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74  Test to see whet
5c2a0 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74 61 62  her or not a tab
5c2b0 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 20  le is a virtual 
5c2c0 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a  table.  This is.
5c2d0 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d 61 63  ** done as a mac
5c2e0 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20 77 69  ro so that it wi
5c2f0 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20  ll be optimized 
5c300 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75 61 6c  out when virtual
5c310 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 70 6f 72  .** table suppor
5c320 74 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f  t is omitted fro
5c330 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a  m the build..*/.
5c340 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
5c350 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
5c360 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72  .#  define IsVir
5c370 74 75 61 6c 28 58 29 20 20 20 20 20 20 28 28 28  tual(X)      (((
5c380 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  X)->tabFlags & T
5c390 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 29 0a 23  F_Virtual)!=0).#
5c3a0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65    define IsHidde
5c3b0 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d  nColumn(X) ((X)-
5c3c0 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 6c 73 65  >isHidden).#else
5c3d0 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72  .#  define IsVir
5c3e0 74 75 61 6c 28 58 29 20 20 20 20 20 20 30 0a 23  tual(X)      0.#
5c3f0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65    define IsHidde
5c400 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e  nColumn(X) 0.#en
5c410 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  dif../*.** Each 
5c420 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
5c430 74 72 61 69 6e 74 20 69 73 20 61 6e 20 69 6e 73  traint is an ins
5c440 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
5c450 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
5c460 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67  ..**.** A foreig
5c470 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63 69 61  n key is associa
5c480 74 65 64 20 77 69 74 68 20 74 77 6f 20 74 61 62  ted with two tab
5c490 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f 6d 22  les.  The "from"
5c4a0 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65   table is.** the
5c4b0 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74   table that cont
5c4c0 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 45 4e  ains the REFEREN
5c4d0 43 45 53 20 63 6c 61 75 73 65 20 74 68 61 74 20  CES clause that 
5c4e0 63 72 65 61 74 65 73 20 74 68 65 20 66 6f 72 65  creates the fore
5c4f0 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65  ign.** key.  The
5c500 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73 20 74   "to" table is t
5c510 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73  he table that is
5c520 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 52 45   named in the RE
5c530 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 2e  FERENCES clause.
5c540 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 69  .** Consider thi
5c550 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  s example:.**.**
5c560 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
5c570 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 20 20 20  E ex1(.**       
5c580 61 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  a INTEGER PRIMAR
5c590 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20  Y KEY,.**       
5c5a0 62 20 49 4e 54 45 47 45 52 20 43 4f 4e 53 54 52  b INTEGER CONSTR
5c5b0 41 49 4e 54 20 66 6b 31 20 52 45 46 45 52 45 4e  AINT fk1 REFEREN
5c5c0 43 45 53 20 65 78 32 28 78 29 0a 2a 2a 20 20 20  CES ex2(x).**   
5c5d0 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66    );.**.** For f
5c5e0 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 6b 31 22  oreign key "fk1"
5c5f0 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65  , the from-table
5c600 20 69 73 20 22 65 78 31 22 20 61 6e 64 20 74 68   is "ex1" and th
5c610 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 22 65  e to-table is "e
5c620 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  x2"..**.** Each 
5c630 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
5c640 65 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 69  e generates an i
5c650 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
5c660 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
5c670 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61  re.** which is a
5c680 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 66  ttached to the f
5c690 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 68 65 20  rom-table.  The 
5c6a0 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f  to-table need no
5c6b0 74 20 65 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20  t exist when.** 
5c6c0 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69  the from-table i
5c6d0 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20  s created.  The 
5c6e0 65 78 69 73 74 65 6e 63 65 20 6f 66 20 74 68 65  existence of the
5c6f0 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74   to-table is not
5c700 20 63 68 65 63 6b 65 64 2e 0a 2a 2f 0a 73 74 72   checked..*/.str
5c710 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54 61 62  uct FKey {.  Tab
5c720 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f  le *pFrom;     /
5c730 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74  * The table that
5c740 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 52 45   contains the RE
5c750 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20  FERENCES clause 
5c760 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74  */.  FKey *pNext
5c770 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66  From;  /* Next f
5c780 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46  oreign key in pF
5c790 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  rom */.  char *z
5c7a0 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  To;        /* Na
5c7b0 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74  me of table that
5c7c0 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20   the key points 
5c7d0 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  to */.  int nCol
5c7e0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
5c7f0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
5c800 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20  n this key */.  
5c810 75 38 20 69 73 44 65 66 65 72 72 65 64 3b 20 20  u8 isDeferred;  
5c820 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 6e    /* True if con
5c830 73 74 72 61 69 6e 74 20 63 68 65 63 6b 69 6e 67  straint checking
5c840 20 69 73 20 64 65 66 65 72 72 65 64 20 74 69 6c   is deferred til
5c850 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 75 38  l COMMIT */.  u8
5c860 20 75 70 64 61 74 65 43 6f 6e 66 3b 20 20 20 20   updateConf;    
5c870 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76  /* How to resolv
5c880 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74  e conflicts that
5c890 20 6f 63 63 75 72 20 6f 6e 20 55 50 44 41 54 45   occur on UPDATE
5c8a0 20 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74 65 43   */.  u8 deleteC
5c8b0 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77 20 74  onf;    /* How t
5c8c0 6f 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66 6c 69  o resolve confli
5c8d0 63 74 73 20 74 68 61 74 20 6f 63 63 75 72 20 6f  cts that occur o
5c8e0 6e 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38  n DELETE */.  u8
5c8f0 20 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20 20 20   insertConf;    
5c900 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f 6c 76  /* How to resolv
5c910 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 61 74  e conflicts that
5c920 20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45 52 54   occur on INSERT
5c930 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f   */.  struct sCo
5c940 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69  lMap {  /* Mappi
5c950 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  ng of columns in
5c960 20 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e   pFrom to column
5c970 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20  s in zTo */.    
5c980 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20  int iFrom;      
5c990 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
5c9a0 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a  olumn in pFrom *
5c9b0 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c  /.    char *zCol
5c9c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ;        /* Name
5c9d0 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54   of column in zT
5c9e0 6f 2e 20 20 49 66 20 30 20 75 73 65 20 50 52 49  o.  If 0 use PRI
5c9f0 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20  MARY KEY */.  } 
5ca00 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20  aCol[1];        
5ca10 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72  /* One entry for
5ca20 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f   each of nCol co
5ca30 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  lumn s */.};../*
5ca40 0a 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f  .** SQLite suppo
5ca50 72 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65  rts many differe
5ca60 6e 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c  nt ways to resol
5ca70 76 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a  ve a constraint.
5ca80 2a 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42  ** error.  ROLLB
5ca90 41 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d  ACK processing m
5caa0 65 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73  eans that a cons
5cab0 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
5cac0 0a 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f  .** causes the o
5cad0 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63  peration in proc
5cae0 65 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20  ess to fail and 
5caf0 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
5cb00 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74  transaction.** t
5cb10 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  o be rolled back
5cb20 2e 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73  .  ABORT process
5cb30 69 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70  ing means the op
5cb40 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65  eration in proce
5cb50 73 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20  ss.** fails and 
5cb60 61 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65  any prior change
5cb70 73 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20  s from that one 
5cb80 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61  operation are ba
5cb90 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74  cked out,.** but
5cba0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
5cbb0 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62   is not rolled b
5cbc0 61 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65  ack.  FAIL proce
5cbd0 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74  ssing means that
5cbe0 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f  .** the operatio
5cbf0 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74  n in progress st
5cc00 6f 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ops and returns 
5cc10 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20  an error code.  
5cc20 42 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61  But prior.** cha
5cc30 6e 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20  nges due to the 
5cc40 73 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61  same operation a
5cc50 72 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75  re not backed ou
5cc60 74 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63  t and no rollbac
5cc70 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47  k.** occurs.  IG
5cc80 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20  NORE means that 
5cc90 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72  the particular r
5cca0 6f 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74  ow that caused t
5ccb0 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a  he constraint.**
5ccc0 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e   error is not in
5ccd0 73 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65  serted or update
5cce0 64 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63  d.  Processing c
5ccf0 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20  ontinues and no 
5cd00 65 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75  error.** is retu
5cd10 72 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d  rned.  REPLACE m
5cd20 65 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69  eans that preexi
5cd30 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72  sting database r
5cd40 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a  ows that caused.
5cd50 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73  ** a UNIQUE cons
5cd60 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
5cd70 20 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20   are removed so 
5cd80 74 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73  that the new ins
5cd90 65 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65  ert or.** update
5cda0 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50   can proceed.  P
5cdb0 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e  rocessing contin
5cdc0 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72  ues and no error
5cdd0 20 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a   is reported..**
5cde0 0a 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45  .** RESTRICT, SE
5cdf0 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41  TNULL, and CASCA
5ce00 44 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79  DE actions apply
5ce10 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e   only to foreign
5ce20 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49   keys..** RESTRI
5ce30 43 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  CT is the same a
5ce40 73 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45  s ABORT for IMME
5ce50 44 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65  DIATE foreign ke
5ce60 79 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61  ys and the.** sa
5ce70 6d 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66  me as ROLLBACK f
5ce80 6f 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73  or DEFERRED keys
5ce90 2e 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73  .  SETNULL means
5cea0 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67   that the foreig
5ceb0 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20  n.** key is set 
5cec0 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44  to NULL.  CASCAD
5ced0 45 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44  E means that a D
5cee0 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20  ELETE or UPDATE 
5cef0 6f 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65  of the.** refere
5cf00 6e 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69  nced table row i
5cf10 73 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74  s propagated int
5cf20 6f 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68  o the row that h
5cf30 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65  olds the.** fore
5cf40 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20  ign key..** .** 
5cf50 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79  The following sy
5cf60 6d 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72  mbolic values ar
5cf70 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64  e used to record
5cf80 20 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f   which type.** o
5cf90 66 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65  f action to take
5cfa0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  ..*/.#define OE_
5cfb0 4e 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20  None     0   /* 
5cfc0 54 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73  There is no cons
5cfd0 74 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20  traint to check 
5cfe0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f  */.#define OE_Ro
5cff0 6c 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61  llback 1   /* Fa
5d000 69 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  il the operation
5d010 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68   and rollback th
5d020 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  e transaction */
5d030 0a 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72  .#define OE_Abor
5d040 74 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b  t    2   /* Back
5d050 20 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74   out changes but
5d060 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20   do no rollback 
5d070 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23  transaction */.#
5d080 64 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20  define OE_Fail  
5d090 20 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74     3   /* Stop t
5d0a0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74  he operation but
5d0b0 20 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72   leave all prior
5d0c0 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66   changes */.#def
5d0d0 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20  ine OE_Ignore   
5d0e0 34 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68  4   /* Ignore th
5d0f0 65 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20  e error. Do not 
5d100 64 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72  do the INSERT or
5d110 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69   UPDATE */.#defi
5d120 6e 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35  ne OE_Replace  5
5d130 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69     /* Delete exi
5d140 73 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68  sting record, th
5d150 65 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20  en do INSERT or 
5d160 55 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69  UPDATE */..#defi
5d170 6e 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36  ne OE_Restrict 6
5d180 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66     /* OE_Abort f
5d190 6f 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45  or IMMEDIATE, OE
5d1a0 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45  _Rollback for DE
5d1b0 46 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e  FERRED */.#defin
5d1c0 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20  e OE_SetNull  7 
5d1d0 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72    /* Set the for
5d1e0 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74  eign key value t
5d1f0 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e  o NULL */.#defin
5d200 65 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20  e OE_SetDflt  8 
5d210 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72    /* Set the for
5d220 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74  eign key value t
5d230 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f  o its default */
5d240 0a 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63  .#define OE_Casc
5d250 61 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63  ade  9   /* Casc
5d260 61 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20  ade the changes 
5d270 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44  */..#define OE_D
5d280 65 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44  efault  99  /* D
5d290 6f 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64  o whatever the d
5d2a0 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73  efault action is
5d2b0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69   */.../*.** An i
5d2c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
5d2d0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
5d2e0 72 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20  re is passed as 
5d2f0 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67  the first.** arg
5d300 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
5d310 56 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61  VdbeKeyCompare a
5d320 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  nd is used to co
5d330 6e 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f  ntrol the .** co
5d340 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20  mparison of the 
5d350 74 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a  two index keys..
5d360 2a 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66  */.struct KeyInf
5d370 6f 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  o {.  sqlite3 *d
5d380 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b;        /* The
5d390 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
5d3a0 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63  tion */.  u8 enc
5d3b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
5d3c0 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d   Text encoding -
5d3d0 20 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54   one of the TEXT
5d3e0 5f 55 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a  _Utf* values */.
5d3f0 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20    u16 nField;   
5d400 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
5d410 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43  of entries in aC
5d420 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61  oll[] */.  u8 *a
5d430 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f  SortOrder;     /
5d440 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20  * If defined an 
5d450 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73  aSortOrder[i] is
5d460 20 74 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43   true, sort DESC
5d470 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61   */.  CollSeq *a
5d480 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c  Coll[1];  /* Col
5d490 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
5d4a0 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66  for each term of
5d4b0 20 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a   the key */.};..
5d4c0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
5d4d0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
5d4e0 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  ng structure hol
5d4f0 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ds information a
5d500 62 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  bout a.** single
5d510 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68   index record th
5d520 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  at has already b
5d530 65 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69  een parsed out i
5d540 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a  nto individual.*
5d550 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  * values..**.** 
5d560 41 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f  A record is an o
5d570 62 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61  bject that conta
5d580 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ins one or more 
5d590 66 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a  fields of data..
5d5a0 2a 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75  ** Records are u
5d5b0 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
5d5c0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61   content of a ta
5d5d0 62 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73  ble row and to s
5d5e0 74 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20  tore.** the key 
5d5f0 6f 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20  of an index.  A 
5d600 62 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66  blob encoding of
5d610 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65   a record is cre
5d620 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f  ated by.** the O
5d630 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63  P_MakeRecord opc
5d640 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20  ode of the VDBE 
5d650 61 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62  and is disassemb
5d660 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50  led by the.** OP
5d670 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a  _Column opcode..
5d680 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  **.** This struc
5d690 74 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63  ture holds a rec
5d6a0 6f 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72  ord that has alr
5d6b0 65 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73  eady been disass
5d6c0 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69  embled.** into i
5d6d0 74 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66  ts constituent f
5d6e0 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  ields..*/.struct
5d6f0 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
5d700 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  {.  KeyInfo *pKe
5d710 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61  yInfo;  /* Colla
5d720 74 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72  tion and sort-or
5d730 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  der information 
5d740 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b  */.  u16 nField;
5d750 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5d760 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
5d770 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31   apMem[] */.  u1
5d780 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  6 flags;        
5d790 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74    /* Boolean set
5d7a0 74 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44  tings.  UNPACKED
5d7b0 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  _... below */.  
5d7c0 69 36 34 20 72 6f 77 69 64 3b 20 20 20 20 20 20  i64 rowid;      
5d7d0 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 55      /* Used by U
5d7e0 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53  NPACKED_PREFIX_S
5d7f0 45 41 52 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a  EARCH */.  Mem *
5d800 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  aMem;          /
5d810 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a  * Values */.};..
5d820 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
5d830 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 6b 65 64  lues of Unpacked
5d840 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a  Record.flags.*/.
5d850 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44  #define UNPACKED
5d860 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 20 20 30  _NEED_FREE     0
5d870 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79  x0001  /* Memory
5d880 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33   is from sqlite3
5d890 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66  Malloc() */.#def
5d8a0 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  ine UNPACKED_NEE
5d8b0 44 5f 44 45 53 54 52 4f 59 20 20 30 78 30 30 30  D_DESTROY  0x000
5d8c0 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73  2  /* apMem[]s s
5d8d0 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 64 65 73  hould all be des
5d8e0 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e  troyed */.#defin
5d8f0 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52  e UNPACKED_IGNOR
5d900 45 5f 52 4f 57 49 44 20 20 30 78 30 30 30 34 20  E_ROWID  0x0004 
5d910 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 61 69 6c   /* Ignore trail
5d920 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79  ing rowid on key
5d930 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50  1 */.#define UNP
5d940 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 20 20  ACKED_INCRKEY   
5d950 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 4d      0x0008  /* M
5d960 61 6b 65 20 74 68 69 73 20 6b 65 79 20 61 6e 20  ake this key an 
5d970 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a  epsilon larger *
5d980 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b  /.#define UNPACK
5d990 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20  ED_PREFIX_MATCH 
5d9a0 20 30 78 30 30 31 30 20 20 2f 2a 20 41 20 70 72   0x0010  /* A pr
5d9b0 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f  efix match is co
5d9c0 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23  nsidered OK */.#
5d9d0 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f  define UNPACKED_
5d9e0 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 30 78  PREFIX_SEARCH 0x
5d9f0 30 30 32 30 20 20 2f 2a 20 41 20 70 72 65 66 69  0020  /* A prefi
5da00 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69  x match is consi
5da10 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a  dered OK */../*.
5da20 2a 2a 20 45 61 63 68 20 53 51 4c 20 69 6e 64 65  ** Each SQL inde
5da30 78 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  x is represented
5da40 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e   in memory by an
5da50 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
5da60 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
5da70 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
5da80 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  he columns of th
5da90 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 72 65  e table that are
5daa0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61   to be indexed a
5dab0 72 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20  re described.** 
5dac0 62 79 20 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b  by the aiColumn[
5dad0 5d 20 66 69 65 6c 64 20 6f 66 20 74 68 69 73 20  ] field of this 
5dae0 73 74 72 75 63 74 75 72 65 2e 20 20 46 6f 72 20  structure.  For 
5daf0 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65  example, suppose
5db00 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 68 65 20  .** we have the 
5db10 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
5db20 61 6e 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a  and index:.**.**
5db30 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
5db40 45 20 45 78 31 28 63 31 20 69 6e 74 2c 20 63 32  E Ex1(c1 int, c2
5db50 20 69 6e 74 2c 20 63 33 20 74 65 78 74 29 3b 0a   int, c3 text);.
5db60 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 4e  **     CREATE IN
5db70 44 45 58 20 45 78 32 20 4f 4e 20 45 78 31 28 63  DEX Ex2 ON Ex1(c
5db80 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20  3,c1);.**.** In 
5db90 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
5dba0 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 20 45  ure describing E
5dbb0 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61  x1, nCol==3 beca
5dbc0 75 73 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a  use there are.**
5dbd0 20 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69   three columns i
5dbe0 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e  n the table.  In
5dbf0 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63   the Index struc
5dc00 74 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 0a  ture describing.
5dc10 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d  ** Ex2, nColumn=
5dc20 3d 32 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68  =2 since 2 of th
5dc30 65 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45  e 3 columns of E
5dc40 78 31 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0a  x1 are indexed..
5dc50 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
5dc60 61 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20  aiColumn is {2, 
5dc70 30 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d  0}.  aiColumn[0]
5dc80 3d 3d 32 20 62 65 63 61 75 73 65 20 74 68 65 20  ==2 because the 
5dc90 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  .** first column
5dca0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28   to be indexed (
5dcb0 63 33 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78  c3) has an index
5dcc0 20 6f 66 20 32 20 69 6e 20 45 78 31 2e 61 43 6f   of 2 in Ex1.aCo
5dcd0 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f  l[]..** The seco
5dce0 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20  nd column to be 
5dcf0 69 6e 64 65 78 65 64 20 28 63 31 29 20 68 61 73  indexed (c1) has
5dd00 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 69   an index of 0 i
5dd10 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c  n.** Ex1.aCol[],
5dd20 20 68 65 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c   hence Ex2.aiCol
5dd30 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a  umn[1]==0..**.**
5dd40 20 54 68 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72   The Index.onErr
5dd50 6f 72 20 66 69 65 6c 64 20 64 65 74 65 72 6d 69  or field determi
5dd60 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  nes whether or n
5dd70 6f 74 20 74 68 65 20 69 6e 64 65 78 65 64 20 63  ot the indexed c
5dd80 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62  olumns.** must b
5dd90 65 20 75 6e 69 71 75 65 20 61 6e 64 20 77 68 61  e unique and wha
5dda0 74 20 74 6f 20 64 6f 20 69 66 20 74 68 65 79 20  t to do if they 
5ddb0 61 72 65 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49  are not.  When I
5ddc0 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f  ndex.onError=OE_
5ddd0 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e  None,.** it mean
5dde0 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20  s this is not a 
5ddf0 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 20 4f  unique index.  O
5de00 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 61  therwise it is a
5de10 20 75 6e 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a   unique index.**
5de20 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f   and the value o
5de30 66 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20  f Index.onError 
5de40 69 6e 64 69 63 61 74 65 20 74 68 65 20 77 68 69  indicate the whi
5de50 63 68 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f  ch conflict reso
5de60 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72  lution .** algor
5de70 69 74 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77  ithm to employ w
5de80 68 65 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d  henever an attem
5de90 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e  pt is made to in
5dea0 73 65 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75  sert a non-uniqu
5deb0 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f  e.** element..*/
5dec0 0a 73 74 72 75 63 74 20 49 6e 64 65 78 20 7b 0a  .struct Index {.
5ded0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
5dee0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
5def0 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  is index */.  in
5df00 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f  t nColumn;     /
5df10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
5df20 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  mns in the table
5df30 20 75 73 65 64 20 62 79 20 74 68 69 73 20 69 6e   used by this in
5df40 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69  dex */.  int *ai
5df50 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69  Column;   /* Whi
5df60 63 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75  ch columns are u
5df70 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65  sed by this inde
5df80 78 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a  x.  1st is 0 */.
5df90 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f    unsigned *aiRo
5dfa0 77 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20  wEst; /* Result 
5dfb0 6f 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e  of ANALYZE: Est.
5dfc0 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62   rows selected b
5dfd0 79 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f  y each column */
5dfe0 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  .  Table *pTable
5dff0 3b 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74  ;   /* The SQL t
5e000 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78  able being index
5e010 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d  ed */.  int tnum
5e020 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65  ;        /* Page
5e030 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74   containing root
5e040 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 69   of this index i
5e050 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
5e060 2a 2f 0a 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b  */.  u8 onError;
5e070 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72        /* OE_Abor
5e080 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45  t, OE_Ignore, OE
5e090 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f  _Replace, or OE_
5e0a0 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 61 75 74  None */.  u8 aut
5e0b0 6f 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 54 72  oIndex;    /* Tr
5e0c0 75 65 20 69 66 20 69 73 20 61 75 74 6f 6d 61 74  ue if is automat
5e0d0 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 28  ically created (
5e0e0 65 78 3a 20 62 79 20 55 4e 49 51 55 45 29 20 2a  ex: by UNIQUE) *
5e0f0 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66  /.  char *zColAf
5e100 66 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64  f;   /* String d
5e110 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69  efining the affi
5e120 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c  nity of each col
5e130 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  umn */.  Index *
5e140 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54 68 65  pNext;    /* The
5e150 20 6e 65 78 74 20 69 6e 64 65 78 20 61 73 73 6f   next index asso
5e160 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
5e170 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  same table */.  
5e180 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b  Schema *pSchema;
5e190 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61   /* Schema conta
5e1a0 69 6e 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78  ining this index
5e1b0 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f   */.  u8 *aSortO
5e1c0 72 64 65 72 3b 20 20 2f 2a 20 41 72 72 61 79 20  rder;  /* Array 
5e1d0 6f 66 20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43  of size Index.nC
5e1e0 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53  olumn. True==DES
5e1f0 43 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f  C, False==ASC */
5e200 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c  .  char **azColl
5e210 3b 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20  ;   /* Array of 
5e220 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
5e230 63 65 20 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64  ce names for ind
5e240 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ex */.};../*.** 
5e250 45 61 63 68 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e  Each token comin
5e260 67 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 65 78  g out of the lex
5e270 65 72 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  er is an instanc
5e280 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72  e of.** this str
5e290 75 63 74 75 72 65 2e 20 20 54 6f 6b 65 6e 73 20  ucture.  Tokens 
5e2a0 61 72 65 20 61 6c 73 6f 20 75 73 65 64 20 61 73  are also used as
5e2b0 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72   part of an expr
5e2c0 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  ession..**.** No
5e2d0 74 65 20 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d 30  te if Token.z==0
5e2e0 20 74 68 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e 20   then Token.dyn 
5e2f0 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61 72 65 20  and Token.n are 
5e300 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a 2a 2a  undefined and.**
5e310 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72 61 6e   may contain ran
5e320 64 6f 6d 20 76 61 6c 75 65 73 2e 20 20 44 6f 20  dom values.  Do 
5e330 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 61 73 73  not make any ass
5e340 75 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74 20 54  umptions about T
5e350 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 61 6e 64 20  oken.dyn.** and 
5e360 54 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20 54 6f 6b  Token.n when Tok
5e370 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 75  en.z==0..*/.stru
5e380 63 74 20 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e  ct Token {.  con
5e390 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
5e3a0 20 2a 7a 3b 20 2f 2a 20 54 65 78 74 20 6f 66 20   *z; /* Text of 
5e3b0 74 68 65 20 74 6f 6b 65 6e 2e 20 20 4e 6f 74 20  the token.  Not 
5e3c0 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 21  NULL-terminated!
5e3d0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 64   */.  unsigned d
5e3e0 79 6e 20 20 20 20 3a 20 31 3b 20 20 20 20 2f 2a  yn    : 1;    /*
5e3f0 20 54 72 75 65 20 66 6f 72 20 6d 61 6c 6c 6f 63   True for malloc
5e400 65 64 20 6d 65 6d 6f 72 79 2c 20 66 61 6c 73 65  ed memory, false
5e410 20 66 6f 72 20 73 74 61 74 69 63 20 2a 2f 0a 20   for static */. 
5e420 20 75 6e 73 69 67 6e 65 64 20 71 75 6f 74 65 64   unsigned quoted
5e430 20 3a 20 31 3b 20 20 20 20 2f 2a 20 54 72 75 65   : 1;    /* True
5e440 20 69 66 20 74 6f 6b 65 6e 20 73 74 69 6c 6c 20   if token still 
5e450 68 61 73 20 69 74 73 20 71 75 6f 74 65 73 20 2a  has its quotes *
5e460 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6e 20 20  /.  unsigned n  
5e470 20 20 20 20 3a 20 33 30 3b 20 20 20 2f 2a 20 4e      : 30;   /* N
5e480 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
5e490 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f 6b 65  ers in this toke
5e4a0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  n */.};../*.** A
5e4b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
5e4c0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  is structure con
5e4d0 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tains informatio
5e4e0 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65  n needed to gene
5e4f0 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72  rate.** code for
5e500 20 61 20 53 45 4c 45 43 54 20 74 68 61 74 20 63   a SELECT that c
5e510 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74  ontains aggregat
5e520 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  e functions..**.
5e530 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d 3d 54  ** If Expr.op==T
5e540 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f 72 20  K_AGG_COLUMN or 
5e550 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
5e560 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67 49 6e  then Expr.pAggIn
5e570 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74  fo is a.** point
5e580 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63  er to this struc
5e590 74 75 72 65 2e 20 20 54 68 65 20 45 78 70 72 2e  ture.  The Expr.
5e5a0 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 69 73  iColumn field is
5e5b0 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a   the index in.**
5e5c0 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 5d 20   AggInfo.aCol[] 
5e5d0 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63  or AggInfo.aFunc
5e5e0 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f  [] of informatio
5e5f0 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65  n needed to gene
5e600 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72  rate.** code for
5e610 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a   that node..**.*
5e620 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70  * AggInfo.pGroup
5e630 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f 2e 61  By and AggInfo.a
5e640 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 6e 74  Func.pExpr point
5e650 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74 68 69   to fields withi
5e660 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61  n the.** origina
5e670 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75  l Select structu
5e680 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65  re that describe
5e690 73 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  s the SELECT sta
5e6a0 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 0a 2a  tement.  These.*
5e6b0 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f 74 20  * fields do not 
5e6c0 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64  need to be freed
5e6d0 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 74 69   when deallocati
5e6e0 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f 20 73  ng the AggInfo s
5e6f0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72  tructure..*/.str
5e700 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a 20 20  uct AggInfo {.  
5e710 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b 20 20  u8 directMode;  
5e720 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63          /* Direc
5e730 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f 64 65  t rendering mode
5e740 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 74 61   means take data
5e750 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20 20 20   directly.      
5e760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e770 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f 75 72      ** from sour
5e780 63 65 20 74 61 62 6c 65 73 20 72 61 74 68 65 72  ce tables rather
5e790 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 75 6d   than from accum
5e7a0 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 38 20  ulators */.  u8 
5e7b0 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 20 20  useSortingIdx;  
5e7c0 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 65 63       /* In direc
5e7d0 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 6e 63  t mode, referenc
5e7e0 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e  e the sorting in
5e7f0 64 65 78 20 72 61 74 68 65 72 0a 20 20 20 20 20  dex rather.     
5e800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e810 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65       ** than the
5e820 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
5e830 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 49 64  .  int sortingId
5e840 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  x;         /* Cu
5e850 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
5e860 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
5e870 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
5e880 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a  pGroupBy;     /*
5e890 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c   The group by cl
5e8a0 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  ause */.  int nS
5e8b0 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 20 20  ortingColumn;   
5e8c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
5e8d0 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f  olumns in the so
5e8e0 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20  rting index */. 
5e8f0 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
5e900 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f 72 20  col {    /* For 
5e910 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 65 64  each column used
5e920 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62 6c 65   in source table
5e930 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a  s */.    Table *
5e940 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
5e950 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 62 6c    /* Source tabl
5e960 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 61  e */.    int iTa
5e970 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ble;            
5e980 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
5e990 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  er of the source
5e9a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e   table */.    in
5e9b0 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
5e9c0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
5e9d0 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74   number within t
5e9e0 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
5e9f0 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74  */.    int iSort
5ea00 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  erColumn;       
5ea10 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
5ea20 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e 67 20   in the sorting 
5ea30 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74  index */.    int
5ea40 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   iMem;          
5ea50 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
5ea60 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 61 63  location that ac
5ea70 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61 74 6f  ts as accumulato
5ea80 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70  r */.    Expr *p
5ea90 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
5eaa0 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 6e 61    /* The origina
5eab0 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  l expression */.
5eac0 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 6e 74    } *aCol;.  int
5ead0 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
5eae0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5eaf0 66 20 75 73 65 64 20 65 6e 74 72 69 65 73 20 69  f used entries i
5eb00 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e  n aCol[] */.  in
5eb10 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 3b 20  t nColumnAlloc; 
5eb20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
5eb30 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74  of slots allocat
5eb40 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 2a 2f  ed for aCol[] */
5eb50 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 6c 61  .  int nAccumula
5eb60 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  tor;       /* Nu
5eb70 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
5eb80 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f 75 67  that show throug
5eb90 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e  h to the output.
5eba0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
5ebb0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 64             ** Ad
5ebc0 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73  ditional columns
5ebd0 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 20 61   are used only a
5ebe0 73 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a  s parameters to.
5ebf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ec00 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 67 67            ** agg
5ec10 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
5ec20 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67   */.  struct Agg
5ec30 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 2f 2a  Info_func {   /*
5ec40 20 46 6f 72 20 65 61 63 68 20 61 67 67 72 65 67   For each aggreg
5ec50 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ate function */.
5ec60 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b      Expr *pExpr;
5ec70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5ec80 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 6f 64  Expression encod
5ec90 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  ing the function
5eca0 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20   */.    FuncDef 
5ecb0 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20  *pFunc;         
5ecc0 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67 61 74   /* The aggregat
5ecd0 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  e function imple
5ece0 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  mentation */.   
5ecf0 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20   int iMem;      
5ed00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
5ed10 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61  ory location tha
5ed20 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75  t acts as accumu
5ed30 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74  lator */.    int
5ed40 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20   iDistinct;     
5ed50 20 20 20 20 20 20 2f 2a 20 45 70 68 65 6d 65 72        /* Ephemer
5ed60 61 6c 20 74 61 62 6c 65 20 75 73 65 64 20 74 6f  al table used to
5ed70 20 65 6e 66 6f 72 63 65 20 44 49 53 54 49 4e 43   enforce DISTINC
5ed80 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 6e 63 3b  T */.  } *aFunc;
5ed90 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b 20 20 20  .  int nFunc;   
5eda0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5edb0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
5edc0 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20  in aFunc[] */.  
5edd0 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f 63 3b 20  int nFuncAlloc; 
5ede0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5edf0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
5ee00 61 74 65 64 20 66 6f 72 20 61 46 75 6e 63 5b 5d  ated for aFunc[]
5ee10 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   */.};../*.** Ea
5ee20 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 78  ch node of an ex
5ee30 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20  pression in the 
5ee40 70 61 72 73 65 20 74 72 65 65 20 69 73 20 61 6e  parse tree is an
5ee50 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20   instance.** of 
5ee60 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a  this structure..
5ee70 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73  **.** Expr.op is
5ee80 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 65   the opcode. The
5ee90 20 69 6e 74 65 67 65 72 20 70 61 72 73 65 72 20   integer parser 
5eea0 74 6f 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 20  token codes are 
5eeb0 72 65 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 63  reused.** as opc
5eec0 6f 64 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65  odes here. For e
5eed0 78 61 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73  xample, the pars
5eee0 65 72 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45  er defines TK_GE
5eef0 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65   to be an intege
5ef00 72 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73  r.** code repres
5ef10 65 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20  enting the ">=" 
5ef20 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73  operator. This s
5ef30 61 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65  ame integer code
5ef40 20 69 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 6f   is reused.** to
5ef50 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 67   represent the g
5ef60 72 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65  reater-than-or-e
5ef70 71 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72  qual-to operator
5ef80 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   in the expressi
5ef90 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a  on.** tree..**.*
5efa0 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
5efb0 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c 69  ion is an SQL li
5efc0 74 65 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 45  teral (TK_INTEGE
5efd0 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b 5f  R, TK_FLOAT, TK_
5efe0 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b 5f  BLOB, .** or TK_
5eff0 53 54 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 78  STRING), then Ex
5f000 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e  pr.token contain
5f010 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68  s the text of th
5f020 65 20 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 49  e SQL literal. I
5f030 66 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73  f.** the express
5f040 69 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c  ion is a variabl
5f050 65 20 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 2c  e (TK_VARIABLE),
5f060 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e   then Expr.token
5f070 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a   contains the .*
5f080 2a 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2e  * variable name.
5f090 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65   Finally, if the
5f0a0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
5f0b0 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 28  n SQL function (
5f0c0 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a 2a  TK_FUNCTION),.**
5f0d0 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e   then Expr.token
5f0e0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61   contains the na
5f0f0 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  me of the functi
5f100 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70  on..**.** Expr.p
5f110 52 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70  Right and Expr.p
5f120 4c 65 66 74 20 61 72 65 20 74 68 65 20 6c 65 66  Left are the lef
5f130 74 20 61 6e 64 20 72 69 67 68 74 20 73 75 62 65  t and right sube
5f140 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 0a  xpressions of a.
5f150 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74  ** binary operat
5f160 6f 72 2e 20 45 69 74 68 65 72 20 6f 72 20 62 6f  or. Either or bo
5f170 74 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a  th may be NULL..
5f180 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c 69  **.** Expr.x.pLi
5f190 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  st is a list of 
5f1a0 61 72 67 75 6d 65 6e 74 73 20 69 66 20 74 68 65  arguments if the
5f1b0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
5f1c0 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 0a  n SQL function,.
5f1d0 2a 2a 20 61 20 43 41 53 45 20 65 78 70 72 65 73  ** a CASE expres
5f1e0 73 69 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78  sion or an IN ex
5f1f0 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20  pression of the 
5f200 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28  form "<lhs> IN (
5f210 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a  <y>, <z>...)"..*
5f220 2a 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74  * Expr.x.pSelect
5f230 20 69 73 20 75 73 65 64 20 69 66 20 74 68 65 20   is used if the 
5f240 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
5f250 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e  sub-select or an
5f260 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a 2a   expression of.*
5f270 2a 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73  * the form "<lhs
5f280 3e 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e  > IN (SELECT ...
5f290 29 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49  )". If the EP_xI
5f2a0 73 53 65 6c 65 63 74 20 62 69 74 20 69 73 20 73  sSelect bit is s
5f2b0 65 74 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 70  et in the.** Exp
5f2c0 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68  r.flags mask, th
5f2d0 65 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63  en Expr.x.pSelec
5f2e0 74 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65  t is valid. Othe
5f2f0 72 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c  rwise, Expr.x.pL
5f300 69 73 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 64  ist is .** valid
5f310 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65  ..**.** An expre
5f320 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72  ssion of the for
5f330 6d 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65  m ID or ID.ID re
5f340 66 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e  fers to a column
5f350 20 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20   in a table..** 
5f360 46 6f 72 20 73 75 63 68 20 65 78 70 72 65 73 73  For such express
5f370 69 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73  ions, Expr.op is
5f380 20 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d   set to TK_COLUM
5f390 4e 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c  N and Expr.iTabl
5f3a0 65 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65  e is.** the inte
5f3b0 67 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ger cursor numbe
5f3c0 72 20 6f 66 20 61 20 56 44 42 45 20 63 75 72 73  r of a VDBE curs
5f3d0 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  or pointing to t
5f3e0 68 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a  hat table and.**
5f3f0 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73   Expr.iColumn is
5f400 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62   the column numb
5f410 65 72 20 66 6f 72 20 74 68 65 20 73 70 65 63 69  er for the speci
5f420 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20  fic column.  If 
5f430 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  the.** expressio
5f440 6e 20 69 73 20 75 73 65 64 20 61 73 20 61 20 72  n is used as a r
5f450 65 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72  esult in an aggr
5f460 65 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68  egate SELECT, th
5f470 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20  en the.** value 
5f480 69 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69  is also stored i
5f490 6e 20 74 68 65 20 45 78 70 72 2e 69 41 67 67 20  n the Expr.iAgg 
5f4a0 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67  column in the ag
5f4b0 67 72 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a  gregate so that.
5f4c0 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63 63  ** it can be acc
5f4d0 65 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20  essed after all 
5f4e0 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20 63  aggregates are c
5f4f0 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  omputed..**.** I
5f500 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
5f510 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 76   is an unbound v
5f520 61 72 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 28  ariable marker (
5f530 61 20 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b 20  a question mark 
5f540 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 27 3f  .** character '?
5f550 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61  ' in the origina
5f560 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 65 20  l SQL) then the 
5f570 45 78 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c 64  Expr.iTable hold
5f580 73 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20  s the index .** 
5f590 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20  number for that 
5f5a0 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  variable..**.** 
5f5b0 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
5f5c0 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20  n is a subquery 
5f5d0 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d  then Expr.iColum
5f5e0 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67  n holds an integ
5f5f0 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e  er.** register n
5f600 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  umber containing
5f610 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
5f620 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 66  he subquery.  If
5f630 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79   the.** subquery
5f640 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 6e   gives a constan
5f650 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 69  t result, then i
5f660 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 66  Table is -1.  If
5f670 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a 2a   the subquery.**
5f680 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72 65   gives a differe
5f690 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 66  nt answer at dif
5f6a0 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 72  ferent times dur
5f6b0 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 72  ing statement pr
5f6c0 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 6e  ocessing.** then
5f6d0 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 61   iTable is the a
5f6e0 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 72  ddress of a subr
5f6f0 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d 70  outine that comp
5f700 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65 72  utes the subquer
5f710 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  y..**.** If the 
5f720 45 78 70 72 20 69 73 20 6f 66 20 74 79 70 65 20  Expr is of type 
5f730 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74  OP_Column, and t
5f740 68 65 20 74 61 62 6c 65 20 69 74 20 69 73 20 73  he table it is s
5f750 65 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a  electing from.**
5f760 20 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65   is a disk table
5f770 20 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20   or the "old.*" 
5f780 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68  pseudo-table, th
5f790 65 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74  en pTab points t
5f7a0 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70  o the.** corresp
5f7b0 6f 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66  onding table def
5f7c0 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41  inition..**.** A
5f7d0 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 3a  LLOCATION NOTES:
5f7e0 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a 65  .**.** Expr obje
5f7f0 63 74 73 20 63 61 6e 20 75 73 65 20 61 20 6c 6f  cts can use a lo
5f800 74 20 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 63  t of memory spac
5f810 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 73 63  e in database sc
5f820 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 6c  hema.  To.** hel
5f830 70 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20  p reduce memory 
5f840 72 65 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 6f  requirements, so
5f850 6d 65 74 69 6d 65 73 20 61 6e 20 45 78 70 72 20  metimes an Expr 
5f860 6f 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a 2a  object will be.*
5f870 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 20 41 6e  * truncated.  An
5f880 64 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20  d to reduce the 
5f890 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79  number of memory
5f8a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f   allocations, so
5f8b0 6d 65 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 6f  metimes.** two o
5f8c0 72 20 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65  r more Expr obje
5f8d0 63 74 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72  cts will be stor
5f8e0 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d  ed in a single m
5f8f0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
5f900 2c 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69  ,.** together wi
5f910 74 68 20 45 78 70 72 2e 74 6f 6b 65 6e 20 61 6e  th Expr.token an
5f920 64 2f 6f 72 20 45 78 70 72 2e 73 70 61 6e 20 73  d/or Expr.span s
5f930 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  trings..**.** If
5f940 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 2c   the EP_Reduced,
5f950 20 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 2c 20 61   EP_SpanToken, a
5f960 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20  nd EP_TokenOnly 
5f970 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 77 68  flags are set wh
5f980 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 6f 62  en.** an Expr ob
5f990 6a 65 63 74 20 69 73 20 74 72 75 6e 63 61 74 65  ject is truncate
5f9a0 64 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 64 75  d.  When EP_Redu
5f9b0 63 65 64 20 69 73 20 73 65 74 2c 20 74 68 65 6e  ced is set, then
5f9c0 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 69 6c   all.** the chil
5f9d0 64 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 69  d Expr objects i
5f9e0 6e 20 74 68 65 20 45 78 70 72 2e 70 4c 65 66 74  n the Expr.pLeft
5f9f0 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74   and Expr.pRight
5fa00 20 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 72 65   subtrees.** are
5fa10 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
5fa20 6e 20 74 68 65 20 73 61 6d 65 20 6d 65 6d 6f 72  n the same memor
5fa30 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e  y allocation.  N
5fa40 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68  ote, however, th
5fa50 61 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 72 65  at.** the subtre
5fa60 65 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 4c 69  es in Expr.x.pLi
5fa70 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 65  st or Expr.x.pSe
5fa80 6c 65 63 74 20 61 72 65 20 61 6c 77 61 79 73 20  lect are always 
5fa90 73 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 61 6c  separately.** al
5faa0 6c 6f 63 61 74 65 64 2c 20 72 65 67 61 72 64 6c  located, regardl
5fab0 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f  ess of whether o
5fac0 72 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 65 64  r not EP_Reduced
5fad0 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 72 75   is set..*/.stru
5fae0 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f  ct Expr {.  u8 o
5faf0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
5fb00 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20     /* Operation 
5fb10 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69  performed by thi
5fb20 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72  s node */.  char
5fb30 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20   affinity;      
5fb40 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69     /* The affini
5fb50 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ty of the column
5fb60 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63   or 0 if not a c
5fb70 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 56 56 41 5f 4f  olumn */.  VVA_O
5fb80 4e 4c 59 28 75 38 20 76 76 61 46 6c 61 67 73 3b  NLY(u8 vvaFlags;
5fb90 29 20 2f 2a 20 46 6c 61 67 73 20 75 73 65 64 20  ) /* Flags used 
5fba0 66 6f 72 20 56 56 26 41 20 6f 6e 6c 79 2e 20 20  for VV&A only.  
5fbb0 45 56 56 41 5f 2a 20 62 65 6c 6f 77 2e 20 2a 2f  EVVA_* below. */
5fbc0 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20  .  u16 flags;   
5fbd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72            /* Var
5fbe0 69 6f 75 73 20 66 6c 61 67 73 2e 20 20 45 50 5f  ious flags.  EP_
5fbf0 2a 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20  * See below */. 
5fc00 20 54 6f 6b 65 6e 20 74 6f 6b 65 6e 3b 20 20 20   Token token;   
5fc10 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70          /* An op
5fc20 65 72 61 6e 64 20 74 6f 6b 65 6e 20 2a 2f 0a 0a  erand token */..
5fc30 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f 54    /* If the EP_T
5fc40 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 73  okenOnly flag is
5fc50 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 70 72   set in the Expr
5fc60 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65  .flags mask, the
5fc70 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 20  n no.  ** space 
5fc80 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  is allocated for
5fc90 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c 6f   the fields belo
5fca0 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 6e  w this point. An
5fcb0 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a 2a   attempt to.  **
5fcc0 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 6c   access them wil
5fcd0 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65  l result in a se
5fce0 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 6e  gfault or malfun
5fcf0 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a 2a  ction. .  ******
5fd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
5fd40 0a 0a 20 20 54 6f 6b 65 6e 20 73 70 61 6e 3b 20  ..  Token span; 
5fd50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
5fd60 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74  mplete text of t
5fd70 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  he expression */
5fd80 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50  ..  /* If the EP
5fd90 5f 53 70 61 6e 54 6f 6b 65 6e 20 66 6c 61 67 20  _SpanToken flag 
5fda0 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78  is set in the Ex
5fdb0 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74  pr.flags mask, t
5fdc0 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63  hen no.  ** spac
5fdd0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  e is allocated f
5fde0 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65  or the fields be
5fdf0 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20  low this point. 
5fe00 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20  An attempt to.  
5fe10 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77  ** access them w
5fe20 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ill result in a 
5fe30 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66  segfault or malf
5fe40 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a  unction. .  ****
5fe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5fe90 2a 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c 65 66  */..  Expr *pLef
5fea0 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
5feb0 4c 65 66 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a  Left subnode */.
5fec0 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20    Expr *pRight; 
5fed0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68           /* Righ
5fee0 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 75  t subnode */.  u
5fef0 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 72 4c  nion {.    ExprL
5ff00 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20  ist *pList;     
5ff10 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 67 75  /* Function argu
5ff20 6d 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c 65 78  ments or in "<ex
5ff30 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69  pr> IN (<expr-li
5ff40 73 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 6c 65  st)" */.    Sele
5ff50 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20  ct *pSelect;    
5ff60 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 75 62   /* Used for sub
5ff70 2d 73 65 6c 65 63 74 73 20 61 6e 64 20 22 3c 65  -selects and "<e
5ff80 78 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 63 74  xpr> IN (<select
5ff90 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20  >)" */.  } x;.  
5ffa0 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
5ffb0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
5ffc0 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f 66 20  llation type of 
5ffd0 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20  the column or 0 
5ffe0 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  */..  /* If the 
5fff0 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20  EP_Reduced flag 
60000 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78  is set in the Ex
60010 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74  pr.flags mask, t
60020 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63  hen no.  ** spac
60030 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  e is allocated f
60040 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65  or the fields be
60050 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20  low this point. 
60060 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20  An attempt to.  
60070 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77  ** access them w
60080 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ill result in a 
60090 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66  segfault or malf
600a0 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a  unction..  *****
600b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
600f0 2f 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c  /..  int iTable,
60100 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57   iColumn;   /* W
60110 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  hen op==TK_COLUM
60120 4e 2c 20 74 68 65 6e 20 74 68 69 73 20 65 78 70  N, then this exp
60130 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20 74 68 65  r node means the
60140 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
60150 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 43 6f            ** iCo
60160 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64 20 6f 66  lumn-th field of
60170 20 74 68 65 20 69 54 61 62 6c 65 2d 74 68 20 74   the iTable-th t
60180 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67 67 49 6e  able. */.  AggIn
60190 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20  fo *pAggInfo;   
601a0 20 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b 5f    /* Used by TK_
601b0 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54  AGG_COLUMN and T
601c0 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a  K_AGG_FUNCTION *
601d0 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b 20 20 20  /.  int iAgg;   
601e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68             /* Wh
601f0 69 63 68 20 65 6e 74 72 79 20 69 6e 20 70 41 67  ich entry in pAg
60200 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 6f 72  gInfo->aCol[] or
60210 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20   ->aFunc[] */.  
60220 69 6e 74 20 69 52 69 67 68 74 4a 6f 69 6e 54 61  int iRightJoinTa
60230 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 45 50 5f  ble;   /* If EP_
60240 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 20 72 69  FromJoin, the ri
60250 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65  ght table of the
60260 20 6a 6f 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65   join */.  Table
60270 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
60280 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 20 54    /* Table for T
60290 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73  K_COLUMN express
602a0 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 51 4c  ions. */.#if SQL
602b0 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
602c0 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 69 67  TH>0.  int nHeig
602d0 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ht;           /*
602e0 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 20 74   Height of the t
602f0 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
60300 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64 69  is node */.#endi
60310 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  f.};../*.** The 
60320 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68  following are th
60330 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69  e meanings of bi
60340 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66  ts in the Expr.f
60350 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
60360 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d 4a 6f  define EP_FromJo
60370 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20  in   0x0001  /* 
60380 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 4f 4e  Originated in ON
60390 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
603a0 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23 64   of a join */.#d
603b0 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20 20 20  efine EP_Agg    
603c0 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 43      0x0002  /* C
603d0 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d  ontains one or m
603e0 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 66 75  ore aggregate fu
603f0 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69  nctions */.#defi
60400 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 20  ne EP_Resolved  
60410 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44 73 20   0x0004  /* IDs 
60420 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76  have been resolv
60430 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a 2f  ed to COLUMNs */
60440 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 72 6f  .#define EP_Erro
60450 72 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f  r      0x0008  /
60460 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e  * Expression con
60470 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
60480 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66  e errors */.#def
60490 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 74 20  ine EP_Distinct 
604a0 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 67 67    0x0010  /* Agg
604b0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
604c0 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 6b 65  with DISTINCT ke
604d0 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65  yword */.#define
604e0 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20 20 30   EP_VarSelect  0
604f0 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c 65 63  x0020  /* pSelec
60500 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65 64 2c  t is correlated,
60510 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 2a 2f   not constant */
60520 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 62 6c 51  .#define EP_DblQ
60530 75 6f 74 65 64 20 20 30 78 30 30 34 30 20 20 2f  uoted  0x0040  /
60540 2a 20 74 6f 6b 65 6e 2e 7a 20 77 61 73 20 6f 72  * token.z was or
60550 69 67 69 6e 61 6c 6c 79 20 69 6e 20 22 2e 2e 2e  iginally in "...
60560 22 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  " */.#define EP_
60570 49 6e 66 69 78 46 75 6e 63 20 20 30 78 30 30 38  InfixFunc  0x008
60580 30 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61  0  /* True for a
60590 6e 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e  n infix function
605a0 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 65 74  : LIKE, GLOB, et
605b0 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  c */.#define EP_
605c0 45 78 70 43 6f 6c 6c 61 74 65 20 30 78 30 31 30  ExpCollate 0x010
605d0 30 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20  0  /* Collating 
605e0 73 65 71 75 65 6e 63 65 20 73 70 65 63 69 66 69  sequence specifi
605f0 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 2a 2f  ed explicitly */
60600 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 6e 79 41  .#define EP_AnyA
60610 66 66 20 20 20 20 20 30 78 30 32 30 30 20 20 2f  ff     0x0200  /
60620 2a 20 43 61 6e 20 74 61 6b 65 20 61 20 63 61 63  * Can take a cac
60630 68 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e  hed column of an
60640 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 23 64  y affinity */.#d
60650 65 66 69 6e 65 20 45 50 5f 46 69 78 65 64 44 65  efine EP_FixedDe
60660 73 74 20 20 30 78 30 34 30 30 20 20 2f 2a 20 52  st  0x0400  /* R
60670 65 73 75 6c 74 20 6e 65 65 64 65 64 20 69 6e 20  esult needed in 
60680 61 20 73 70 65 63 69 66 69 63 20 72 65 67 69 73  a specific regis
60690 74 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ter */.#define E
606a0 50 5f 49 6e 74 56 61 6c 75 65 20 20 20 30 78 30  P_IntValue   0x0
606b0 38 30 30 20 20 2f 2a 20 49 6e 74 65 67 65 72 20  800  /* Integer 
606c0 76 61 6c 75 65 20 63 6f 6e 74 61 69 6e 65 64 20  value contained 
606d0 69 6e 20 69 54 61 62 6c 65 20 2a 2f 0a 23 64 65  in iTable */.#de
606e0 66 69 6e 65 20 45 50 5f 78 49 73 53 65 6c 65 63  fine EP_xIsSelec
606f0 74 20 20 30 78 31 30 30 30 20 20 2f 2a 20 78 2e  t  0x1000  /* x.
60700 70 53 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64  pSelect is valid
60710 20 28 6f 74 68 65 72 77 69 73 65 20 78 2e 70 4c   (otherwise x.pL
60720 69 73 74 20 69 73 29 20 2a 2f 0a 0a 23 64 65 66  ist is) */..#def
60730 69 6e 65 20 45 50 5f 52 65 64 75 63 65 64 20 20  ine EP_Reduced  
60740 20 20 30 78 32 30 30 30 20 20 2f 2a 20 45 78 70    0x2000  /* Exp
60750 72 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52  r struct is EXPR
60760 5f 52 45 44 55 43 45 44 53 49 5a 45 20 62 79 74  _REDUCEDSIZE byt
60770 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  es only */.#defi
60780 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20  ne EP_TokenOnly 
60790 20 30 78 34 30 30 30 20 20 2f 2a 20 45 78 70 72   0x4000  /* Expr
607a0 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52 5f   struct is EXPR_
607b0 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 62 79  TOKENONLYSIZE by
607c0 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66  tes only */.#def
607d0 69 6e 65 20 45 50 5f 53 70 61 6e 54 6f 6b 65 6e  ine EP_SpanToken
607e0 20 20 30 78 38 30 30 30 20 20 2f 2a 20 45 78 70    0x8000  /* Exp
607f0 72 20 73 69 7a 65 20 69 73 20 45 58 50 52 5f 53  r size is EXPR_S
60800 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 20 62 79 74  PANTOKENSIZE byt
60810 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  es */../*.** The
60820 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74   following are t
60830 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62  he meanings of b
60840 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e  its in the Expr.
60850 76 76 61 46 6c 61 67 73 20 66 69 65 6c 64 2e 0a  vvaFlags field..
60860 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74  ** This informat
60870 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ion is only used
60880 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20   when SQLite is 
60890 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a  compiled with.**
608a0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65   SQLITE_DEBUG de
608b0 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  fined..*/.#ifnde
608c0 66 20 4e 44 45 42 55 47 0a 23 64 65 66 69 6e 65  f NDEBUG.#define
608d0 20 45 56 56 41 5f 52 65 61 64 4f 6e 6c 79 54 6f   EVVA_ReadOnlyTo
608e0 6b 65 6e 20 20 30 78 30 31 20 20 2f 2a 20 45 78  ken  0x01  /* Ex
608f0 70 72 2e 74 6f 6b 65 6e 2e 7a 20 69 73 20 72 65  pr.token.z is re
60900 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69  ad-only */.#endi
60910 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d  f../*.** These m
60920 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65  acros can be use
60930 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20  d to test, set, 
60940 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e  or clear bits in
60950 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66 6c   the .** Expr.fl
60960 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64  ags field..*/.#d
60970 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72 6f  efine ExprHasPro
60980 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 28  perty(E,P)     (
60990 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 29  ((E)->flags&(P))
609a0 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 45  ==(P)).#define E
609b0 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
609c0 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e 66  y(E,P)  (((E)->f
609d0 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 64  lags&(P))!=0).#d
609e0 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72 6f  efine ExprSetPro
609f0 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20 28  perty(E,P)     (
60a00 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a 23  E)->flags|=(P).#
60a10 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61 72  define ExprClear
60a20 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20  Property(E,P)   
60a30 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 29  (E)->flags&=~(P)
60a40 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74  ../*.** Macros t
60a50 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
60a60 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
60a70 72 65 71 75 69 72 65 64 20 62 79 20 61 20 6e 6f  required by a no
60a80 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20 73 74  rmal Expr .** st
60a90 72 75 63 74 2c 20 61 6e 20 45 78 70 72 20 73 74  ruct, an Expr st
60aa0 72 75 63 74 20 77 69 74 68 20 74 68 65 20 45 50  ruct with the EP
60ab0 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 73 65  _Reduced flag se
60ac0 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 73 20  t in Expr.flags 
60ad0 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70 72 20  .** and an Expr 
60ae0 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20  struct with the 
60af0 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61  EP_TokenOnly fla
60b00 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  g set..*/.#defin
60b10 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 20  e EXPR_FULLSIZE 
60b20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
60b30 28 45 78 70 72 29 20 20 20 20 20 20 20 20 20 20  (Expr)          
60b40 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20 2a 2f   /* Full size */
60b50 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 52 45  .#define EXPR_RE
60b60 44 55 43 45 44 53 49 5a 45 20 20 20 20 20 20 20  DUCEDSIZE       
60b70 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 69   offsetof(Expr,i
60b80 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d 6d 6f  Table)  /* Commo
60b90 6e 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 23 64  n features */.#d
60ba0 65 66 69 6e 65 20 45 58 50 52 5f 53 50 41 4e 54  efine EXPR_SPANT
60bb0 4f 4b 45 4e 53 49 5a 45 20 20 20 20 20 20 6f 66  OKENSIZE      of
60bc0 66 73 65 74 6f 66 28 45 78 70 72 2c 70 4c 65 66  fsetof(Expr,pLef
60bd0 74 29 20 20 20 2f 2a 20 46 65 77 65 72 20 66 65  t)   /* Fewer fe
60be0 61 74 75 72 65 73 20 2a 2f 0a 23 64 65 66 69 6e  atures */.#defin
60bf0 65 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59  e EXPR_TOKENONLY
60c00 53 49 5a 45 20 20 20 20 20 20 6f 66 66 73 65 74  SIZE      offset
60c10 6f 66 28 45 78 70 72 2c 73 70 61 6e 29 20 20 20  of(Expr,span)   
60c20 20 2f 2a 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73   /* Smallest pos
60c30 73 69 62 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  sible */../*.** 
60c40 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20  Flags passed to 
60c50 74 68 65 20 73 71 6c 69 74 65 33 45 78 70 72 44  the sqlite3ExprD
60c60 75 70 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 53  up() function. S
60c70 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f  ee the header co
60c80 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f 76 65 20  mment .** above 
60c90 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29  sqlite3ExprDup()
60ca0 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f   for details..*/
60cb0 0a 23 64 65 66 69 6e 65 20 45 58 50 52 44 55 50  .#define EXPRDUP
60cc0 5f 52 45 44 55 43 45 20 20 20 20 20 20 20 20 20  _REDUCE         
60cd0 30 78 30 30 30 31 20 20 2f 2a 20 55 73 65 64 20  0x0001  /* Used 
60ce0 72 65 64 75 63 65 64 2d 73 69 7a 65 20 45 78 70  reduced-size Exp
60cf0 72 20 6e 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69  r nodes */.#defi
60d00 6e 65 20 45 58 50 52 44 55 50 5f 53 50 41 4e 20  ne EXPRDUP_SPAN 
60d10 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 32            0x0002
60d20 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79    /* Make a copy
60d30 20 6f 66 20 45 78 70 72 2e 73 70 61 6e 20 2a 2f   of Expr.span */
60d40 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f  ../*.** A list o
60d50 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20  f expressions.  
60d60 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20  Each expression 
60d70 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68  may optionally h
60d80 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20  ave a.** name.  
60d90 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d  An expr/name com
60da0 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 20  bination can be 
60db0 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20  used in several 
60dc0 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73  ways, such.** as
60dd0 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 78   the list of "ex
60de0 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 73  pr AS ID" fields
60df0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45   following a "SE
60e00 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 0a  LECT" or in the.
60e10 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 3d  ** list of "ID =
60e20 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e 20   expr" items in 
60e30 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c 69  an UPDATE.  A li
60e40 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
60e50 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65  s can.** also be
60e60 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72 67   used as the arg
60e70 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74  ument to a funct
60e80 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 61  ion, in which ca
60e90 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a  se the a.zName.*
60ea0 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 75  * field is not u
60eb0 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45  sed..*/.struct E
60ec0 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20  xprList {.  int 
60ed0 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  nExpr;          
60ee0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
60ef0 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74  expressions on t
60f00 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  he list */.  int
60f10 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20   nAlloc;        
60f20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
60f30 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74   entries allocat
60f40 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e  ed below */.  in
60f50 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 20  t iECursor;     
60f60 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72       /* VDBE Cur
60f70 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 77  sor associated w
60f80 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 73  ith this ExprLis
60f90 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78  t */.  struct Ex
60fa0 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20  prList_item {.  
60fb0 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20    Expr *pExpr;  
60fc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
60fd0 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69  list of expressi
60fe0 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  ons */.    char 
60ff0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
61000 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63    /* Token assoc
61010 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
61020 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
61030 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20    u8 sortOrder; 
61040 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f           /* 1 fo
61050 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20  r DESC or 0 for 
61060 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f  ASC */.    u8 do
61070 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ne;             
61080 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69    /* A flag to i
61090 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f  ndicate when pro
610a0 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73  cessing is finis
610b0 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69  hed */.    u16 i
610c0 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
610d0 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42    /* For ORDER B
610e0 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  Y, column number
610f0 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a   in result set *
61100 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61 73  /.    u16 iAlias
61110 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
61120 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65  Index into Parse
61130 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e  .aAlias[] for zN
61140 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20  ame */.  } *a;  
61150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
61160 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72  /* One entry for
61170 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
61180 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
61190 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
611a0 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20  s structure can 
611b0 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c 69  hold a simple li
611c0 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72  st of identifier
611d0 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 68  s,.** such as th
611e0 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 69  e list "a,b,c" i
611f0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
61200 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  statements:.**.*
61210 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  *      INSERT IN
61220 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c 55  TO t(a,b,c) VALU
61230 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20  ES ...;.**      
61240 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 78  CREATE INDEX idx
61250 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a   ON t(a,b,c);.**
61260 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49        CREATE TRI
61270 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 45  GGER trig BEFORE
61280 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c 62   UPDATE ON t(a,b
61290 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54  ,c) ...;.**.** T
612a0 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 20  he IdList.a.idx 
612b0 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 68  field is used wh
612c0 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 65  en the IdList re
612d0 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 73  presents the lis
612e0 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e  t of.** column n
612f0 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 62  ames after a tab
61300 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e  le name in an IN
61310 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  SERT statement. 
61320 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   In the statemen
61330 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45  t.**.**     INSE
61340 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29  RT INTO t(a,b,c)
61350 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61   ....**.** If "a
61360 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 6f  " is the k-th co
61370 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 74  lumn of table "t
61380 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e 61  ", then IdList.a
61390 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73  [0].idx==k..*/.s
613a0 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a 20  truct IdList {. 
613b0 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69   struct IdList_i
613c0 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a  tem {.    char *
613d0 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e  zName;      /* N
613e0 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e 74  ame of the ident
613f0 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  ifier */.    int
61400 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f   idx;          /
61410 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20  * Index in some 
61420 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20  Table.aCol[] of 
61430 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a  a column named z
61440 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a  Name */.  } *a;.
61450 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20    int nId;      
61460 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
61470 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 74  identifiers on t
61480 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  he list */.  int
61490 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a   nAlloc;      /*
614a0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
614b0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
614c0 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b   a[] below */.};
614d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d  ../*.** The bitm
614e0 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 66  ask datatype def
614f0 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 73  ined below is us
61500 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 6f  ed for various o
61510 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a  ptimizations..**
61520 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 69  .** Changing thi
61530 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 20  s from a 64-bit 
61540 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 65  to a 32-bit type
61550 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d 62   limits the numb
61560 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20  er of.** tables 
61570 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20  in a join to 32 
61580 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 20  instead of 64.  
61590 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 75  But it also redu
615a0 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20  ces the size.** 
615b0 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 62  of the library b
615c0 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 69  y 738 bytes on i
615d0 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  x86..*/.typedef 
615e0 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a  u64 Bitmask;../*
615f0 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  .** The number o
61600 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d  f bits in a Bitm
61610 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e  ask.  "BMS" mean
61620 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 22  s "BitMask Size"
61630 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53  ..*/.#define BMS
61640 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 28    ((int)(sizeof(
61650 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f 2a  Bitmask)*8))../*
61660 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
61670 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63  g structure desc
61680 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 63  ribes the FROM c
61690 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
616a0 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  T statement..** 
616b0 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 75  Each table or su
616c0 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52  bquery in the FR
616d0 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73  OM clause is a s
616e0 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 20  eparate element 
616f0 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 73  of.** the SrcLis
61700 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a  t.a[] array..**.
61710 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 69  ** With the addi
61720 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65  tion of multiple
61730 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
61740 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
61750 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 61   structure.** ca
61760 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74  n also be used t
61770 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72  o describe a par
61780 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 75  ticular table su
61790 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 20  ch as the table 
617a0 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66  that.** is modif
617b0 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54  ied by an INSERT
617c0 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44  , DELETE, or UPD
617d0 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ATE statement.  
617e0 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c 2c  In standard SQL,
617f0 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c 65  .** such a table
61800 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 6c   must be a simpl
61810 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 74  e name: ID.  But
61820 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 20   in SQLite, the 
61830 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77  table can.** now
61840 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 62   be identified b
61850 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 6d  y a database nam
61860 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 74  e, a dot, then t
61870 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49  he table name: I
61880 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  D.ID..**.** The 
61890 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 20  jointype starts 
618a0 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 20  out showing the 
618b0 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 65  join type betwee
618c0 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  n the current ta
618d0 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  ble.** and the n
618e0 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 65  ext table on the
618f0 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 73   list.  The pars
61900 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c 69  er builds the li
61910 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20  st this way..** 
61920 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c 69  But sqlite3SrcLi
61930 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
61940 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 74  ) later shifts t
61950 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20  he jointypes so 
61960 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69  that each.** joi
61970 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 20  ntype expresses 
61980 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e  the join between
61990 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74   the table and t
619a0 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 6c  he previous tabl
619b0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 63  e..*/.struct Src
619c0 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 72  List {.  i16 nSr
619d0 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  c;        /* Num
619e0 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f 72  ber of tables or
619f0 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74   subqueries in t
61a00 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
61a10 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20  /.  i16 nAlloc; 
61a20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
61a30 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
61a40 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77  ted in a[] below
61a50 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
61a60 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20  List_item {.    
61a70 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b  char *zDatabase;
61a80 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
61a90 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68  abase holding th
61aa0 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  is table */.    
61ab0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
61ac0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
61ad0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68   table */.    ch
61ae0 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20  ar *zAlias;     
61af0 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 20  /* The "B" part 
61b00 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 68  of a "A AS B" ph
61b10 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20  rase.  zName is 
61b20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 54  the "A" */.    T
61b30 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
61b40 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c 65   /* An SQL table
61b50 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
61b60 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53  o zName */.    S
61b70 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
61b80 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
61b90 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 70  tement used in p
61ba0 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20  lace of a table 
61bb0 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 69  name */.    u8 i
61bc0 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a  sPopulated;   /*
61bd0 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   Temporary table
61be0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
61bf0 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 6c   SELECT is popul
61c00 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a  ated */.    u8 j
61c10 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f 2a  ointype;      /*
61c20 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65   Type of join be
61c30 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 20  tween this able 
61c40 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73  and the previous
61c50 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49 6e   */.    u8 notIn
61c60 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72 75  dexed;    /* Tru
61c70 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  e if there is a 
61c80 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75  NOT INDEXED clau
61c90 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  se */.    int iC
61ca0 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54  ursor;      /* T
61cb0 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  he VDBE cursor n
61cc0 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63  umber used to ac
61cd0 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20  cess this table 
61ce0 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e  */.    Expr *pOn
61cf0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
61d00 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  ON clause of a j
61d10 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73  oin */.    IdLis
61d20 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20  t *pUsing;   /* 
61d30 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  The USING clause
61d40 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20   of a join */.  
61d50 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65    Bitmask colUse
61d60 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c  d;  /* Bit N (1<
61d70 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d  <N) set if colum
61d80 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20 75  n N of pTab is u
61d90 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  sed */.    char 
61da0 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20  *zIndex;     /* 
61db0 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d 20  Identifier from 
61dc0 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 6e  "INDEXED BY <zIn
61dd0 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f 0a  dex>" clause */.
61de0 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65      Index *pInde
61df0 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73  x;    /* Index s
61e00 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 70  tructure corresp
61e10 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 78  onding to zIndex
61e20 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d 20  , if any */.  } 
61e30 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20  a[1];           
61e40 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66    /* One entry f
61e50 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 69  or each identifi
61e60 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a  er on the list *
61e70 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d  /.};../*.** Perm
61e80 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 20  itted values of 
61e90 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f  the SrcList.a.jo
61ea0 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a  intype field.*/.
61eb0 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52  #define JT_INNER
61ec0 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 2f       0x0001    /
61ed0 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e  * Any kind of in
61ee0 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69  ner or cross joi
61ef0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f  n */.#define JT_
61f00 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 32  CROSS     0x0002
61f10 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 20      /* Explicit 
61f20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 53  use of the CROSS
61f30 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66   keyword */.#def
61f40 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20  ine JT_NATURAL  
61f50 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 72   0x0004    /* Tr
61f60 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 61  ue for a "natura
61f70 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69  l" join */.#defi
61f80 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 20  ne JT_LEFT      
61f90 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 66  0x0008    /* Lef
61fa0 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a  t outer join */.
61fb0 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 54  #define JT_RIGHT
61fc0 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 2f       0x0010    /
61fd0 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a 6f  * Right outer jo
61fe0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
61ff0 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 32  _OUTER     0x002
62000 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 54  0    /* The "OUT
62010 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 70  ER" keyword is p
62020 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  resent */.#defin
62030 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 30  e JT_ERROR     0
62040 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e  x0040    /* unkn
62050 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  own or unsupport
62060 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a  ed join type */.
62070 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 50  ../*.** A WhereP
62080 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73  lan object holds
62090 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
620a0 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c 6f  t describes a lo
620b0 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67 79  okup.** strategy
620c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a  ..**.** This obj
620d0 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ect is intended 
620e0 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75 74  to be opaque out
620f0 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65 72  side of the wher
62100 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 49  e.c module..** I
62110 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68 65  t is included he
62120 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74 20  re only so that 
62130 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77 69  that compiler wi
62140 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20  ll know how big 
62150 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 20  it.** is.  None 
62160 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69 6e  of the fields in
62170 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68 6f   this object sho
62180 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74 73  uld be used outs
62190 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77 68  ide of.** the wh
621a0 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a  ere.c module..**
621b0 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 75  .** Within the u
621c0 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e  nion, pIdx is on
621d0 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46  ly used when wsF
621e0 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 58  lags&WHERE_INDEX
621f0 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 70  ED is true..** p
62200 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65  Term is only use
62210 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57  d when wsFlags&W
62220 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73  HERE_MULTI_OR is
62230 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 61   true.  And pVta
62240 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20  bIdx.** is only 
62250 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67  used when wsFlag
62260 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  s&WHERE_VIRTUALT
62270 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20 49  ABLE is true.  I
62280 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a 2a  t is never the.*
62290 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72 65  * case that more
622a0 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65   than one of the
622b0 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73  se conditions is
622c0 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74   true..*/.struct
622d0 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20 75   WherePlan {.  u
622e0 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20 20  32 wsFlags;     
622f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
62300 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 74   WHERE_* flags t
62310 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65  hat describe the
62320 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 75   strategy */.  u
62330 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20  32 nEq;         
62340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
62350 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f   Number of == co
62360 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 75  nstraints */.  u
62370 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65 78  nion {.    Index
62380 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
62390 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
623a0 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49 4e  ex when WHERE_IN
623b0 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a 2f  DEXED is true */
623c0 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65 72  .    struct Wher
623d0 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20  eTerm *pTerm;   
623e0 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61      /* WHERE cla
623f0 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52 2d  use term for OR-
62400 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73 71  search */.    sq
62410 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
62420 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a 20   *pVtabIdx;  /* 
62430 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  Virtual table in
62440 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20  dex to use */.  
62450 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  } u;.};../*.** F
62460 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20 6c  or each nested l
62470 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20 63  oop in a WHERE c
62480 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74 61  lause implementa
62490 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65 49  tion, the WhereI
624a0 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  nfo.** structure
624b0 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67   contains a sing
624c0 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  le instance of t
624d0 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20  his structure.  
624e0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  This structure.*
624f0 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  * is intended to
62500 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65 20   be private the 
62510 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75  the where.c modu
62520 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f  le and should no
62530 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20 6f  t be.** access o
62540 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f 74  r modified by ot
62550 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a  her modules..**.
62560 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20  ** The pIdxInfo 
62570 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f  field is used to
62580 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 62   help pick the b
62590 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a 2a  est index on a.*
625a0 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  * virtual table.
625b0 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 70    The pIdxInfo p
625c0 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 20  ointer contains 
625d0 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f  indexing.** info
625e0 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  rmation for the 
625f0 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 68  i-th table in th
62600 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 65  e FROM clause be
62610 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 2e  fore reordering.
62620 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 78  .** All the pIdx
62630 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 72  Info pointers ar
62640 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 65  e freed by where
62650 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 68  InfoFree() in wh
62660 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f 74  ere.c..** All ot
62670 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  her information 
62680 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65 72  in the i-th Wher
62690 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66 6f  eLevel object fo
626a0 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65  r the i-th table
626b0 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20 63  .** after FROM c
626c0 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 0a  lause ordering..
626d0 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4c  */.struct WhereL
626e0 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50 6c  evel {.  WherePl
626f0 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20 2f  an plan;       /
62700 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72  * query plan for
62710 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f 66   this element of
62720 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
62730 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 4a   */.  int iLeftJ
62740 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  oin;        /* M
62750 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20  emory cell used 
62760 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 46  to implement LEF
62770 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f 0a  T OUTER JOIN */.
62780 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 20    int iTabCur;  
62790 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
627a0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20  DBE cursor used 
627b0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 61  to access the ta
627c0 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64  ble */.  int iId
627d0 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f  xCur;          /
627e0 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
627f0 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  r used to access
62800 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 61   pIdx */.  int a
62810 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 20  ddrBrk;         
62820 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
62830 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68   break out of th
62840 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  e loop */.  int 
62850 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 20  addrNxt;        
62860 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
62870 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 74  o start the next
62880 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20   IN combination 
62890 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e  */.  int addrCon
628a0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  t;         /* Ju
628b0 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69  mp here to conti
628c0 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78  nue with the nex
628d0 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a  t loop cycle */.
628e0 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 3b    int addrFirst;
628f0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
62900 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20   instruction of 
62910 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 20  interior of the 
62920 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46 72  loop */.  u8 iFr
62930 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  om;             
62940 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69  /* Which entry i
62950 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
62960 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70 35  e */.  u8 op, p5
62970 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
62980 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f 66  Opcode and P5 of
62990 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74   the opcode that
629a0 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a   ends the loop *
629b0 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b 20  /.  int p1, p2; 
629c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
629d0 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70 63  rands of the opc
629e0 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64 73  ode used to ends
629f0 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75   the loop */.  u
62a00 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20  nion {          
62a10 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
62a20 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64 73  ion that depends
62a30 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73   on plan.wsFlags
62a40 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 7b   */.    struct {
62a50 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20  .      int nIn; 
62a60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
62a70 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
62a80 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a  s in aInLoop[] *
62a90 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49  /.      struct I
62aa0 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 20  nLoop {.        
62ab0 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20  int iCur;       
62ac0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
62ad0 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 62  BE cursor used b
62ae0 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 74  y this IN operat
62af0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  or */.        in
62b00 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 20  t addrInTop;    
62b10 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74       /* Top of t
62b20 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20  he IN loop */.  
62b30 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20      } *aInLoop; 
62b40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66            /* Inf
62b50 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65  ormation about e
62b60 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f 70  ach nested IN op
62b70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 20  erator */.    } 
62b80 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  in;             
62b90 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
62ba0 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48   plan.wsFlags&WH
62bb0 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20  ERE_IN_ABLE */. 
62bc0 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20   } u;..  /* The 
62bd0 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20  following field 
62be0 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61  is really not pa
62bf0 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  rt of the curren
62c00 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20  t level.  But.  
62c10 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61  ** we need a pla
62c20 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 74  ce to cache virt
62c30 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20  ual table index 
62c40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
62c50 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61  each.  ** virtua
62c60 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  l table in the F
62c70 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74  ROM clause and t
62c80 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74  he WhereLevel st
62c90 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20  ructure is.  ** 
62ca0 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61  a convenient pla
62cb0 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69  ce since there i
62cc0 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c  s one WhereLevel
62cd0 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63   for each FROM c
62ce0 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65  lause.  ** eleme
62cf0 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  nt..  */.  sqlit
62d00 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
62d10 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64  IdxInfo;  /* Ind
62d20 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68  ex info for n-th
62d30 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
62d40 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73  .};../*.** Flags
62d50 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
62d60 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20   the wctrlFlags 
62d70 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c  parameter of sql
62d80 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
62d90 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72  .** and the Wher
62da0 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73  eInfo.wctrlFlags
62db0 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66   member..*/.#def
62dc0 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42  ine WHERE_ORDERB
62dd0 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30  Y_NORMAL   0x000
62de0 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64  0 /* No-op */.#d
62df0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
62e00 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30  RBY_MIN      0x0
62e10 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20  001 /* ORDER BY 
62e20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d  processing for m
62e30 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65  in() func */.#de
62e40 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52  fine WHERE_ORDER
62e50 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30  BY_MAX      0x00
62e60 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70  02 /* ORDER BY p
62e70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61  rocessing for ma
62e80 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66  x() func */.#def
62e90 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53  ine WHERE_ONEPAS
62ea0 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30  S_DESIRED  0x000
62eb0 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20  4 /* Want to do 
62ec0 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f  one-pass UPDATE/
62ed0 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e  DELETE */.#defin
62ee0 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  e WHERE_DUPLICAT
62ef0 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 20  ES_OK    0x0008 
62f00 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20  /* Ok to return 
62f10 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20  a row more than 
62f20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  once */.#define 
62f30 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20  WHERE_OMIT_OPEN 
62f40 20 20 20 20 20 20 20 30 78 30 30 31 30 20 2f 2a         0x0010 /*
62f50 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 72   Table cursor ar
62f60 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a  e already open *
62f70 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
62f80 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 20  OMIT_CLOSE      
62f90 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 74 20   0x0020 /* Omit 
62fa0 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20 26  close of table &
62fb0 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a   index cursors *
62fc0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
62fd0 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 20  FORCE_TABLE     
62fe0 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 6e 6f   0x0040 /* Do no
62ff0 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f  t use an index-o
63000 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f  nly search */../
63010 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 63  *.** The WHERE c
63020 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
63030 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 6f   routine has two
63040 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a 2a   halves.  The.**
63050 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 73   first part does
63060 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
63070 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e 64  e WHERE loop and
63080 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 68   the second.** h
63090 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 69  alf does the tai
630a0 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c  l of the WHERE l
630b0 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e 63  oop.  An instanc
630c0 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72  e of.** this str
630d0 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 6e  ucture is return
630e0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20  ed by the first 
630f0 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 0a  half and passed.
63100 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 6f  ** into the seco
63110 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 20  nd half to give 
63120 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 2e  some continuity.
63130 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65  .*/.struct Where
63140 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 2a  Info {.  Parse *
63150 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a  pParse;       /*
63160 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
63170 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
63180 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 77 63  text */.  u16 wc
63190 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 2f  trlFlags;      /
631a0 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61 6c  * Flags original
631b0 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  ly passed to sql
631c0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
631d0 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 61   */.  u8 okOnePa
631e0 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6b  ss;        /* Ok
631f0 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 73 73   to use one-pass
63200 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 55   algorithm for U
63210 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
63220 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
63230 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20  abList;         
63240 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
63250 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69  ables in the joi
63260 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b  n */.  int iTop;
63270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63280 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72        /* The ver
63290 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  y beginning of t
632a0 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f  he WHERE loop */
632b0 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65  .  int iContinue
632c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
632d0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
632e0 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
632f0 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20  next record */. 
63300 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20   int iBreak;    
63310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63320 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
63330 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
63340 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e   loop */.  int n
63350 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20  Level;          
63360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
63370 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f  ber of nested lo
63380 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 57  op */.  struct W
63390 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b  hereClause *pWC;
633a0 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70         /* Decomp
633b0 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57  osition of the W
633c0 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
633d0 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d   WhereLevel a[1]
633e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
633f0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
63400 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 6c  bout each nest l
63410 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a  oop in WHERE */.
63420 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65  };../*.** A Name
63430 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 20  Context defines 
63440 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69  a context in whi
63450 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 61  ch to resolve ta
63460 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a  ble and column.*
63470 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f  * names.  The co
63480 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f  ntext consists o
63490 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c  f a list of tabl
634a0 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 74  es (the pSrcList
634b0 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61  ) field and.** a
634c0 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65   list of named e
634d0 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73  xpression (pELis
634e0 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65  t).  The named e
634f0 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d  xpression list m
63500 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20  ay.** be NULL.  
63510 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70  The pSrc corresp
63520 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d  onds to the FROM
63530 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c   clause of a SEL
63540 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65  ECT or.** to the
63550 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 65   table being ope
63560 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 45  rated on by INSE
63570 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44  RT, UPDATE, or D
63580 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70  ELETE.  The.** p
63590 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64  EList correspond
635a0 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20  s to the result 
635b0 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20  set of a SELECT 
635c0 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a  and is NULL for.
635d0 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65  ** other stateme
635e0 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43  nts..**.** NameC
635f0 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 6e  ontexts can be n
63600 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 73  ested.  When res
63610 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68  olving names, th
63620 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a  e inner-most .**
63630 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 72   context is sear
63640 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 20  ched first.  If 
63650 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e  no match is foun
63660 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 65  d, the next oute
63670 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20  r.** context is 
63680 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 65  checked.  If the
63690 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d  re is still no m
636a0 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 63  atch, the next c
636b0 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 65  ontext.** is che
636c0 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f 63  cked.  This proc
636d0 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e  ess continues un
636e0 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 74  til either a mat
636f0 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f  ch is found.** o
63700 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61  r all contexts a
63710 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e 20  re check.  When 
63720 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  a match is found
63730 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 65  , the nRef membe
63740 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  r of.** the cont
63750 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ext containing t
63760 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 72  he match is incr
63770 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20  emented. .**.** 
63780 45 61 63 68 20 73 75 62 71 75 65 72 79 20 67 65  Each subquery ge
63790 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e  ts a new NameCon
637a0 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 74  text.  The pNext
637b0 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f   field points to
637c0 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74   the.** NameCont
637d0 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 6e  ext in the paren
637e0 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 74  t query.  Thus t
637f0 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 63  he process of sc
63800 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61  anning the.** Na
63810 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63  meContext list c
63820 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65  orresponds to se
63830 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20  arching through 
63840 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74  successively out
63850 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73  er.** subqueries
63860 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d   looking for a m
63870 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  atch..*/.struct 
63880 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20  NameContext {.  
63890 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
638a0 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
638b0 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  er */.  SrcList 
638c0 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20  *pSrcList;   /* 
638d0 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c  One or more tabl
638e0 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c  es used to resol
638f0 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78  ve names */.  Ex
63900 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
63910 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c     /* Optional l
63920 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70  ist of named exp
63930 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ressions */.  in
63940 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
63950 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
63960 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 62  names resolved b
63970 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 2a  y this context *
63980 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20  /.  int nErr;   
63990 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
639a0 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63  er of errors enc
639b0 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 72  ountered while r
639c0 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a  esolving names *
639d0 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b  /.  u8 allowAgg;
639e0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72           /* Aggr
639f0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
63a00 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a  allowed here */.
63a10 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 20    u8 hasAgg;    
63a20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
63a30 66 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65  f aggregates are
63a40 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73   seen */.  u8 is
63a50 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 20  Check;          
63a60 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f 6c  /* True if resol
63a70 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 20  ving names in a 
63a80 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74  CHECK constraint
63a90 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 68   */.  int nDepth
63aa0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65  ;          /* De
63ab0 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 20  pth of subquery 
63ac0 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72  recursion. 1 for
63ad0 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f   no recursion */
63ae0 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  .  AggInfo *pAgg
63af0 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72  Info;   /* Infor
63b00 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 67  mation about agg
63b10 72 65 67 61 74 65 73 20 61 74 20 74 68 69 73 20  regates at this 
63b20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43  level */.  NameC
63b30 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 20  ontext *pNext;  
63b40 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e 61  /* Next outer na
63b50 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c  me context.  NUL
63b60 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20  L for outermost 
63b70 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
63b80 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
63b90 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
63ba0 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  ure contains all
63bb0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
63bc0 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 61  needed to genera
63bd0 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69  te code for a si
63be0 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74  ngle SELECT stat
63bf0 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69  ement..**.** nLi
63c00 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d 31  mit is set to -1
63c10 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
63c20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 6e  LIMIT clause.  n
63c30 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f  Offset is set to
63c40 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20   0..** If there 
63c50 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  is a LIMIT claus
63c60 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 65  e, the parser se
63c70 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65  ts nLimit to the
63c80 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a   value of the.**
63c90 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73   limit and nOffs
63ca0 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  et to the value 
63cb0 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 6f  of the offset (o
63cc0 72 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20  r 0 if there is 
63cd0 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20  not.** offset). 
63ce0 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e   But later on, n
63cf0 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65  Limit and nOffse
63d00 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d  t become the mem
63d10 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a  ory locations.**
63d20 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 61   in the VDBE tha
63d30 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 6d  t record the lim
63d40 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 6f  it and offset co
63d50 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64  unters..**.** ad
63d60 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74  drOpenEphm[] ent
63d70 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65  ries contain the
63d80 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f   address of OP_O
63d90 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63  penEphemeral opc
63da0 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61  odes..** These a
63db0 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 65  ddresses must be
63dc0 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 20   stored so that 
63dd0 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 61  we can go back a
63de0 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68  nd fill in.** th
63df0 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64  e P4_KEYINFO and
63e00 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 6c   P2 parameters l
63e10 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 74  ater.  Neither t
63e20 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a  he KeyInfo nor.*
63e30 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
63e40 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 61  columns in P2 ca
63e50 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74  n be computed at
63e60 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a   the same time.*
63e70 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e  * as the OP_Open
63e80 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  Ephm instruction
63e90 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 73   is coded becaus
63ea0 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20  e not.** enough 
63eb0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
63ec0 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71  t the compound q
63ed0 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74  uery is known at
63ee0 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20   that point..** 
63ef0 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20  The KeyInfo for 
63f00 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20  addrOpenTran[0] 
63f10 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73  and [1] contains
63f20 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
63f30 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20  nces.** for the 
63f40 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65  result set.  The
63f50 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64   KeyInfo for add
63f60 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e  rOpenTran[2] con
63f70 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a  tains collating.
63f80 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72  ** sequences for
63f90 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
63fa0 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ause..*/.struct 
63fb0 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 4c  Select {.  ExprL
63fc0 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20  ist *pEList;    
63fd0 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 20    /* The fields 
63fe0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f  of the result */
63ff0 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20  .  u8 op;       
64000 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
64010 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b   of: TK_UNION TK
64020 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43  _ALL TK_INTERSEC
64030 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20  T TK_EXCEPT */. 
64040 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20   char affinity; 
64050 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52          /* MakeR
64060 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 20  ecord with this 
64070 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 54  affinity for SRT
64080 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 65  _Set */.  u16 se
64090 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  lFlags;         
640a0 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a   /* Various SF_*
640b0 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63   values */.  Src
640c0 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20  List *pSrc;     
640d0 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20      /* The FROM 
640e0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
640f0 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20   *pWhere;       
64100 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
64110 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
64120 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20  List *pGroupBy; 
64130 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20     /* The GROUP 
64140 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  BY clause */.  E
64150 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20  xpr *pHaving;   
64160 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56        /* The HAV
64170 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ING clause */.  
64180 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
64190 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  By;    /* The OR
641a0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
641b0 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f  .  Select *pPrio
641c0 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69  r;        /* Pri
641d0 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63  or select in a c
641e0 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73  ompound select s
641f0 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65  tatement */.  Se
64200 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20  lect *pNext;    
64210 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c       /* Next sel
64220 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20  ect to the left 
64230 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f  in a compound */
64240 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68  .  Select *pRigh
64250 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67  tmost;    /* Rig
64260 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69  ht-most select i
64270 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  n a compound sel
64280 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ect statement */
64290 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b  .  Expr *pLimit;
642a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d            /* LIM
642b0 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  IT expression. N
642c0 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
642d0 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ed. */.  Expr *p
642e0 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
642f0 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 73  /* OFFSET expres
64300 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73  sion. NULL means
64310 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20   not used. */.  
64320 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66  int iLimit, iOff
64330 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79  set;   /* Memory
64340 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69   registers holdi
64350 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 45  ng LIMIT & OFFSE
64360 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  T counters */.  
64370 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 6d  int addrOpenEphm
64380 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65  [3];   /* OP_Ope
64390 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 72  nEphem opcodes r
643a0 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 73  elated to this s
643b0 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  elect */.};../*.
643c0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
643d0 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c  s for Select.sel
643e0 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 22  Flags.  The "SF"
643f0 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 66   prefix stands f
64400 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c  or.** "Select Fl
64410 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ag"..*/.#define 
64420 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20  SF_Distinct     
64430 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75     0x0001  /* Ou
64440 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44  tput should be D
64450 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69  ISTINCT */.#defi
64460 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20  ne SF_Resolved  
64470 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a        0x0002  /*
64480 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 76   Identifiers hav
64490 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20  e been resolved 
644a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67  */.#define SF_Ag
644b0 67 72 65 67 61 74 65 20 20 20 20 20 20 20 30 78  gregate       0x
644c0 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e  0004  /* Contain
644d0 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  s aggregate func
644e0 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65  tions */.#define
644f0 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61   SF_UsesEphemera
64500 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55  l   0x0008  /* U
64510 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65  ses the OpenEphe
64520 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a  meral opcode */.
64530 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e  #define SF_Expan
64540 64 65 64 20 20 20 20 20 20 20 20 30 78 30 30 31  ded        0x001
64550 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c  0  /* sqlite3Sel
64560 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c  ectExpand() call
64570 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64  ed on this */.#d
64580 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65  efine SF_HasType
64590 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 20  Info     0x0020 
645a0 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72   /* FROM subquer
645b0 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d  ies have Table m
645c0 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a  etadata */.../*.
645d0 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f  ** The results o
645e0 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 62  f a select can b
645f0 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e  e distributed in
64600 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 20   several ways.  
64610 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 65  The.** "SRT" pre
64620 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 43  fix means "SELEC
64630 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e 0a  T Result Type"..
64640 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 55  */.#define SRT_U
64650 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 2f  nion        1  /
64660 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61  * Store result a
64670 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64  s keys in an ind
64680 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  ex */.#define SR
64690 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 32  T_Except       2
646a0 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 75    /* Remove resu
646b0 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20  lt from a UNION 
646c0 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65  index */.#define
646d0 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20   SRT_Exists     
646e0 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 20    3  /* Store 1 
646f0 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  if the result is
64700 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64   not empty */.#d
64710 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72  efine SRT_Discar
64720 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f 20  d      4  /* Do 
64730 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 73  not save the res
64740 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f  ults anywhere */
64750 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42  ../* The ORDER B
64760 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f  Y clause is igno
64770 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74  red for all of t
64780 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66  he above */.#def
64790 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64  ine IgnorableOrd
647a0 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 65  erby(X) ((X->eDe
647b0 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 64  st)<=SRT_Discard
647c0 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f  )..#define SRT_O
647d0 75 74 70 75 74 20 20 20 20 20 20 20 35 20 20 2f  utput       5  /
647e0 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f  * Output each ro
647f0 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23  w of result */.#
64800 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20  define SRT_Mem  
64810 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74          6  /* St
64820 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ore result in a 
64830 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23  memory cell */.#
64840 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20  define SRT_Set  
64850 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74          7  /* St
64860 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b  ore results as k
64870 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20  eys in an index 
64880 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54  */.#define SRT_T
64890 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f  able        8  /
648a0 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61  * Store result a
648b0 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61  s data with an a
648c0 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a  utomatic rowid *
648d0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70  /.#define SRT_Ep
648e0 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a  hemTab     9  /*
648f0 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e   Create transien
64900 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20  t tab and store 
64910 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a  like SRT_Table *
64920 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 6f  /.#define SRT_Co
64930 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f 2a  routine   10  /*
64940 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67   Generate a sing
64950 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74  le row of result
64960 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72   */../*.** A str
64970 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 63  ucture used to c
64980 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 68  ustomize the beh
64990 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33  avior of sqlite3
649a0 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a  Select(). See.**
649b0 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20   comments above 
649c0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20  sqlite3Select() 
649d0 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
649e0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
649f0 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 74  electDest Select
64a00 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 6c  Dest;.struct Sel
64a10 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65  ectDest {.  u8 e
64a20 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Dest;         /*
64a30 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20   How to dispose 
64a40 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a  of the results *
64a50 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 3b  /.  u8 affinity;
64a60 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74        /* Affinit
64a70 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 73  y used when eDes
64a80 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20  t==SRT_Set */.  
64a90 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 20  int iParm;      
64aa0 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 72    /* A parameter
64ab0 20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 65   used by the eDe
64ac0 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68  st disposal meth
64ad0 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d  od */.  int iMem
64ae0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73  ;         /* Bas
64af0 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65  e register where
64b00 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 69   results are wri
64b10 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  tten */.  int nM
64b20 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  em;         /* N
64b30 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65  umber of registe
64b40 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  rs allocated */.
64b50 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f  };../*.** Size o
64b60 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63  f the column cac
64b70 68 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  he.*/.#ifndef SQ
64b80 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 0a  LITE_N_COLCACHE.
64b90 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
64ba0 4e 5f 43 4f 4c 43 41 43 48 45 20 31 30 0a 23 65  N_COLCACHE 10.#e
64bb0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53  ndif../*.** An S
64bc0 51 4c 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  QL parser contex
64bd0 74 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68  t.  A copy of th
64be0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
64bf0 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a 2a  passed through.*
64c00 2a 20 74 68 65 20 70 61 72 73 65 72 20 61 6e 64  * the parser and
64c10 20 64 6f 77 6e 20 69 6e 74 6f 20 61 6c 6c 20 74   down into all t
64c20 68 65 20 70 61 72 73 65 72 20 61 63 74 69 6f 6e  he parser action
64c30 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65   routine in orde
64c40 72 20 74 6f 0a 2a 2a 20 63 61 72 72 79 20 61 72  r to.** carry ar
64c50 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ound information
64c60 20 74 68 61 74 20 69 73 20 67 6c 6f 62 61 6c 20   that is global 
64c70 74 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 61  to the entire pa
64c80 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  rse..**.** The s
64c90 74 72 75 63 74 75 72 65 20 69 73 20 64 69 76 69  tructure is divi
64ca0 64 65 64 20 69 6e 74 6f 20 74 77 6f 20 70 61 72  ded into two par
64cb0 74 73 2e 20 20 57 68 65 6e 20 74 68 65 20 70 61  ts.  When the pa
64cc0 72 73 65 72 20 61 6e 64 20 63 6f 64 65 0a 2a 2a  rser and code.**
64cd0 20 67 65 6e 65 72 61 74 65 20 63 61 6c 6c 20 74   generate call t
64ce0 68 65 6d 73 65 6c 76 65 73 20 72 65 63 75 72 73  hemselves recurs
64cf0 69 76 65 6c 79 2c 20 74 68 65 20 66 69 72 73 74  ively, the first
64d00 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 74 72   part of the str
64d10 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 63 6f 6e  ucture.** is con
64d20 73 74 61 6e 74 20 62 75 74 20 74 68 65 20 73 65  stant but the se
64d30 63 6f 6e 64 20 70 61 72 74 20 69 73 20 72 65 73  cond part is res
64d40 65 74 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  et at the beginn
64d50 69 6e 67 20 61 6e 64 20 65 6e 64 20 6f 66 0a 2a  ing and end of.*
64d60 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e  * each recursion
64d70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 54 61 62  ..**.** The nTab
64d80 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 54 61 62 6c  leLock and aTabl
64d90 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 73 20  eLock variables 
64da0 61 72 65 20 6f 6e 6c 79 20 75 73 65 64 20 69 66  are only used if
64db0 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
64dc0 65 20 0a 2a 2a 20 66 65 61 74 75 72 65 20 69 73  e .** feature is
64dd0 20 65 6e 61 62 6c 65 64 20 28 69 66 20 73 71 6c   enabled (if sql
64de0 69 74 65 33 54 73 64 28 29 2d 3e 75 73 65 53 68  ite3Tsd()->useSh
64df0 61 72 65 64 44 61 74 61 20 69 73 20 74 72 75 65  aredData is true
64e00 29 2e 20 54 68 65 79 20 61 72 65 0a 2a 2a 20 75  ). They are.** u
64e10 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
64e20 20 73 65 74 20 6f 66 20 74 61 62 6c 65 2d 6c 6f   set of table-lo
64e30 63 6b 73 20 72 65 71 75 69 72 65 64 20 62 79 20  cks required by 
64e40 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 62 65  the statement be
64e50 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e  ing.** compiled.
64e60 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   Function sqlite
64e70 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 69 73 20  3TableLock() is 
64e80 75 73 65 64 20 74 6f 20 61 64 64 20 65 6e 74 72  used to add entr
64e90 69 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 69  ies to the.** li
64ea0 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61  st..*/.struct Pa
64eb0 72 73 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse {.  sqlite3 
64ec0 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  *db;         /* 
64ed0 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  The main databas
64ee0 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
64ef0 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
64f00 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
64f10 63 6f 64 65 20 66 72 6f 6d 20 65 78 65 63 75 74  code from execut
64f20 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ion */.  char *z
64f30 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 2f 2a  ErrMsg;       /*
64f40 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   An error messag
64f50 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 56 64  e */.  Vdbe *pVd
64f60 62 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  be;         /* A
64f70 6e 20 65 6e 67 69 6e 65 20 66 6f 72 20 65 78 65  n engine for exe
64f80 63 75 74 69 6e 67 20 64 61 74 61 62 61 73 65 20  cuting database 
64f90 62 79 74 65 63 6f 64 65 20 2a 2f 0a 20 20 75 38  bytecode */.  u8
64fa0 20 63 6f 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20   colNamesSet;   
64fb0 20 20 20 2f 2a 20 54 52 55 45 20 61 66 74 65 72     /* TRUE after
64fc0 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68   OP_ColumnName h
64fd0 61 73 20 62 65 65 6e 20 69 73 73 75 65 64 20 74  as been issued t
64fe0 6f 20 70 56 64 62 65 20 2a 2f 0a 20 20 75 38 20  o pVdbe */.  u8 
64ff0 6e 61 6d 65 43 6c 61 73 68 3b 20 20 20 20 20 20  nameClash;      
65000 20 20 2f 2a 20 41 20 70 65 72 6d 61 6e 65 6e 74    /* A permanent
65010 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6c 61 73   table name clas
65020 68 65 73 20 77 69 74 68 20 74 65 6d 70 20 74 61  hes with temp ta
65030 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 38  ble name */.  u8
65040 20 63 68 65 63 6b 53 63 68 65 6d 61 3b 20 20 20   checkSchema;   
65050 20 20 20 2f 2a 20 43 61 75 73 65 73 20 73 63 68     /* Causes sch
65060 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b  ema cookie check
65070 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20   after an error 
65080 2a 2f 0a 20 20 75 38 20 6e 65 73 74 65 64 3b 20  */.  u8 nested; 
65090 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
650a0 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61  ber of nested ca
650b0 6c 6c 73 20 74 6f 20 74 68 65 20 70 61 72 73 65  lls to the parse
650c0 72 2f 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72  r/code generator
650d0 20 2a 2f 0a 20 20 75 38 20 70 61 72 73 65 45 72   */.  u8 parseEr
650e0 72 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 72  ror;       /* Tr
650f0 75 65 20 61 66 74 65 72 20 61 20 70 61 72 73 69  ue after a parsi
65100 6e 67 20 65 72 72 6f 72 2e 20 20 54 69 63 6b 65  ng error.  Ticke
65110 74 20 23 31 37 39 34 20 2a 2f 0a 20 20 75 38 20  t #1794 */.  u8 
65120 6e 54 65 6d 70 52 65 67 3b 20 20 20 20 20 20 20  nTempReg;       
65130 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
65140 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
65150 72 73 20 69 6e 20 61 54 65 6d 70 52 65 67 5b 5d  rs in aTempReg[]
65160 20 2a 2f 0a 20 20 75 38 20 6e 54 65 6d 70 49 6e   */.  u8 nTempIn
65170 55 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  Use;       /* Nu
65180 6d 62 65 72 20 6f 66 20 61 54 65 6d 70 52 65 67  mber of aTempReg
65190 5b 5d 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65  [] currently che
651a0 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 69 6e  cked out */.  in
651b0 74 20 61 54 65 6d 70 52 65 67 5b 38 5d 3b 20 20  t aTempReg[8];  
651c0 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 61 72     /* Holding ar
651d0 65 61 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  ea for temporary
651e0 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20 20   registers */.  
651f0 69 6e 74 20 6e 52 61 6e 67 65 52 65 67 3b 20 20  int nRangeReg;  
65200 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
65210 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 72 65  the temporary re
65220 67 69 73 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a  gister block */.
65230 20 20 69 6e 74 20 69 52 61 6e 67 65 52 65 67 3b    int iRangeReg;
65240 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
65250 72 65 67 69 73 74 65 72 20 69 6e 20 74 65 6d 70  register in temp
65260 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 62  orary register b
65270 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  lock */.  int nE
65280 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  rr;            /
65290 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  * Number of erro
652a0 72 73 20 73 65 65 6e 20 2a 2f 0a 20 20 69 6e 74  rs seen */.  int
652b0 20 6e 54 61 62 3b 20 20 20 20 20 20 20 20 20 20   nTab;          
652c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
652d0 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61  reviously alloca
652e0 74 65 64 20 56 44 42 45 20 63 75 72 73 6f 72 73  ted VDBE cursors
652f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20   */.  int nMem; 
65300 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
65310 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63  mber of memory c
65320 65 6c 6c 73 20 75 73 65 64 20 73 6f 20 66 61 72  ells used so far
65330 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 65 74 3b 20   */.  int nSet; 
65340 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
65350 6d 62 65 72 20 6f 66 20 73 65 74 73 20 75 73 65  mber of sets use
65360 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e  d so far */.  in
65370 74 20 63 6b 42 61 73 65 3b 20 20 20 20 20 20 20  t ckBase;       
65380 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73     /* Base regis
65390 74 65 72 20 6f 66 20 64 61 74 61 20 64 75 72 69  ter of data duri
653a0 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61  ng check constra
653b0 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ints */.  int iC
653c0 61 63 68 65 4c 65 76 65 6c 3b 20 20 20 20 20 2f  acheLevel;     /
653d0 2a 20 43 6f 6c 43 61 63 68 65 20 76 61 6c 69 64  * ColCache valid
653e0 20 77 68 65 6e 20 61 43 6f 6c 43 61 63 68 65 5b   when aColCache[
653f0 5d 2e 69 4c 65 76 65 6c 3c 3d 69 43 61 63 68 65  ].iLevel<=iCache
65400 4c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e 74 20 69  Level */.  int i
65410 43 61 63 68 65 43 6e 74 3b 20 20 20 20 20 20 20  CacheCnt;       
65420 2f 2a 20 43 6f 75 6e 74 65 72 20 75 73 65 64 20  /* Counter used 
65430 74 6f 20 67 65 6e 65 72 61 74 65 20 61 43 6f 6c  to generate aCol
65440 43 61 63 68 65 5b 5d 2e 6c 72 75 20 76 61 6c 75  Cache[].lru valu
65450 65 73 20 2a 2f 0a 20 20 75 38 20 6e 43 6f 6c 43  es */.  u8 nColC
65460 61 63 68 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  ache;        /* 
65470 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
65480 73 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20  s in the column 
65490 63 61 63 68 65 20 2a 2f 0a 20 20 75 38 20 69 43  cache */.  u8 iC
654a0 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20 20  olCache;        
654b0 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79 20 6f 66  /* Next entry of
654c0 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 72 65   the cache to re
654d0 70 6c 61 63 65 20 2a 2f 0a 20 20 73 74 72 75 63  place */.  struc
654e0 74 20 79 43 6f 6c 43 61 63 68 65 20 7b 0a 20 20  t yColCache {.  
654f0 20 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20    int iTable;   
65500 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
65510 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 2a   cursor number *
65520 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d  /.    int iColum
65530 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  n;          /* T
65540 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62  able column numb
65550 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 61 66 66  er */.    u8 aff
65560 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20  Change;         
65570 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
65580 72 65 67 69 73 74 65 72 20 68 61 73 20 68 61 64  register has had
65590 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61   an affinity cha
655a0 6e 67 65 20 2a 2f 0a 20 20 20 20 75 38 20 74 65  nge */.    u8 te
655b0 6d 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 20  mpReg;          
655c0 20 2f 2a 20 69 52 65 67 20 69 73 20 61 20 74 65   /* iReg is a te
655d0 6d 70 20 72 65 67 69 73 74 65 72 20 74 68 61 74  mp register that
655e0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72 65   needs to be fre
655f0 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c  ed */.    int iL
65600 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  evel;           
65610 2f 2a 20 4e 65 73 74 69 6e 67 20 6c 65 76 65 6c  /* Nesting level
65620 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 65 67   */.    int iReg
65630 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
65640 20 52 65 67 20 77 69 74 68 20 76 61 6c 75 65 20   Reg with value 
65650 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 2e 20  of this column. 
65660 30 20 6d 65 61 6e 73 20 6e 6f 6e 65 2e 20 2a 2f  0 means none. */
65670 0a 20 20 20 20 69 6e 74 20 6c 72 75 3b 20 20 20  .    int lru;   
65680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
65690 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 73 65  ast recently use
656a0 64 20 65 6e 74 72 79 20 68 61 73 20 74 68 65 20  d entry has the 
656b0 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20 2a  smallest value *
656c0 2f 0a 20 20 7d 20 61 43 6f 6c 43 61 63 68 65 5b  /.  } aColCache[
656d0 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
656e0 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20 66 6f 72 20  E];  /* One for 
656f0 65 61 63 68 20 63 6f 6c 75 6d 6e 20 63 61 63 68  each column cach
65700 65 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32  e entry */.  u32
65710 20 77 72 69 74 65 4d 61 73 6b 3b 20 20 20 20 20   writeMask;     
65720 20 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69    /* Start a wri
65730 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te transaction o
65740 6e 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65  n these database
65750 73 20 2a 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69  s */.  u32 cooki
65760 65 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42  eMask;      /* B
65770 69 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61  itmask of schema
65780 20 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61   verified databa
65790 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f  ses */.  int coo
657a0 6b 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a  kieGoto;      /*
657b0 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47   Address of OP_G
657c0 6f 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65  oto to cookie ve
657d0 72 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e  rifier subroutin
657e0 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69  e */.  int cooki
657f0 65 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41  eValue[SQLITE_MA
65800 58 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20  X_ATTACHED+2];  
65810 2f 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f  /* Values of coo
65820 6b 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a  kies to verify *
65830 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
65840 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
65850 48 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c  HE.  int nTableL
65860 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  ock;        /* N
65870 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69  umber of locks i
65880 6e 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a  n aTableLock */.
65890 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61    TableLock *aTa
658a0 62 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75  bleLock; /* Requ
658b0 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73  ired table locks
658c0 20 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68   for shared-cach
658d0 65 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66  e mode */.#endif
658e0 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b  .  int regRowid;
658f0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
65900 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69  ter holding rowi
65910 64 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c  d of CREATE TABL
65920 45 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74  E entry */.  int
65930 20 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20   regRoot;       
65940 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f    /* Register ho
65950 6c 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20  lding root page 
65960 6e 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f  number for new o
65970 62 6a 65 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20  bjects */..  /* 
65980 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e  Above is constan
65990 74 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73  t between recurs
659a0 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20  ions.  Below is 
659b0 72 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64  reset before and
659c0 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68   after.  ** each
659d0 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20   recursion */.. 
659e0 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20   int nVar;      
659f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
65a00 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73  of '?' variables
65a10 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c   seen in the SQL
65a20 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74   so far */.  int
65a30 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20   nVarExpr;      
65a40 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75    /* Number of u
65a50 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56  sed slots in apV
65a60 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e  arExpr[] */.  in
65a70 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b  t nVarExprAlloc;
65a80 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
65a90 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20  allocated slots 
65aa0 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a  in apVarExpr[] *
65ab0 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72  /.  Expr **apVar
65ac0 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e  Expr;    /* Poin
65ad0 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64  ters to :aaa and
65ae0 20 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20   $aaaa wildcard 
65af0 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20  expressions */. 
65b00 20 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20   int nAlias;    
65b10 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
65b20 6f 66 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c  of aliased resul
65b30 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f  t set columns */
65b40 0a 20 20 69 6e 74 20 6e 41 6c 69 61 73 41 6c 6c  .  int nAliasAll
65b50 6f 63 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  oc;     /* Numbe
65b60 72 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73  r of allocated s
65b70 6c 6f 74 73 20 66 6f 72 20 61 41 6c 69 61 73 5b  lots for aAlias[
65b80 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 41 6c 69  ] */.  int *aAli
65b90 61 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52  as;         /* R
65ba0 65 67 69 73 74 65 72 20 75 73 65 64 20 74 6f 20  egister used to 
65bb0 68 6f 6c 64 20 61 6c 69 61 73 65 64 20 72 65 73  hold aliased res
65bc0 75 6c 74 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c  ult */.  u8 expl
65bd0 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ain;          /*
65be0 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50   True if the EXP
65bf0 4c 41 49 4e 20 66 6c 61 67 20 69 73 20 66 6f 75  LAIN flag is fou
65c00 6e 64 20 6f 6e 20 74 68 65 20 71 75 65 72 79 20  nd on the query 
65c10 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 45 72 72 54  */.  Token sErrT
65c20 6f 6b 65 6e 3b 20 20 20 20 20 2f 2a 20 54 68 65  oken;     /* The
65c30 20 74 6f 6b 65 6e 20 61 74 20 77 68 69 63 68 20   token at which 
65c40 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 72  the error occurr
65c50 65 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4e  ed */.  Token sN
65c60 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20  ameToken;    /* 
65c70 54 6f 6b 65 6e 20 77 69 74 68 20 75 6e 71 75 61  Token with unqua
65c80 6c 69 66 69 65 64 20 73 63 68 65 6d 61 20 6f 62  lified schema ob
65c90 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 54  ject name */.  T
65ca0 6f 6b 65 6e 20 73 4c 61 73 74 54 6f 6b 65 6e 3b  oken sLastToken;
65cb0 20 20 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20      /* The last 
65cc0 74 6f 6b 65 6e 20 70 61 72 73 65 64 20 2a 2f 0a  token parsed */.
65cd0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
65ce0 71 6c 3b 20 20 20 20 2f 2a 20 41 6c 6c 20 53 51  ql;    /* All SQ
65cf0 4c 20 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73  L text */.  cons
65d00 74 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20  t char *zTail;  
65d10 20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74   /* All SQL text
65d20 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73   past the last s
65d30 65 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20  emicolon parsed 
65d40 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77  */.  Table *pNew
65d50 54 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74  Table;    /* A t
65d60 61 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74  able being const
65d70 72 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45  ructed by CREATE
65d80 20 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67   TABLE */.  Trig
65d90 67 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72  ger *pNewTrigger
65da0 3b 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72  ;     /* Trigger
65db0 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
65dc0 20 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49   by a CREATE TRI
65dd0 47 47 45 52 20 2a 2f 0a 20 20 54 72 69 67 67 65  GGER */.  Trigge
65de0 72 53 74 61 63 6b 20 2a 74 72 69 67 53 74 61 63  rStack *trigStac
65df0 6b 3b 20 20 2f 2a 20 54 72 69 67 67 65 72 20 61  k;  /* Trigger a
65e00 63 74 69 6f 6e 73 20 62 65 69 6e 67 20 63 6f 64  ctions being cod
65e10 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
65e20 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74  ar *zAuthContext
65e30 3b 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 72  ; /* The 6th par
65e40 61 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 41  ameter to db->xA
65e50 75 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f  uth callbacks */
65e60 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
65e70 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
65e80 45 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b 20  E.  Token sArg; 
65e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
65ea0 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20  * Complete text 
65eb0 6f 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 75  of a module argu
65ec0 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 63  ment */.  u8 dec
65ed0 6c 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 20  lareVtab;       
65ee0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
65ef0 69 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f 64  inside sqlite3_d
65f00 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f  eclare_vtab() */
65f10 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b  .  int nVtabLock
65f20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
65f30 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75   Number of virtu
65f40 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63  al tables to loc
65f50 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a 61  k */.  Table **a
65f60 70 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20  pVtabLock;      
65f70 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
65f80 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e  virtual tables n
65f90 65 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a  eeding locking *
65fa0 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e  /.#endif.  int n
65fb0 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  Height;         
65fc0 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
65fd0 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 20   tree height of 
65fe0 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65  current sub-sele
65ff0 63 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ct */.  Table *p
66000 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 20  ZombieTab;      
66010 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65  /* List of Table
66020 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65   objects to dele
66030 74 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 65  te after code ge
66040 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20  n */.};..#ifdef 
66050 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
66060 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 69  UALTABLE.  #defi
66070 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  ne IN_DECLARE_VT
66080 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 65  AB 0.#else.  #de
66090 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f  fine IN_DECLARE_
660a0 56 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64 65  VTAB (pParse->de
660b0 63 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64 69  clareVtab).#endi
660c0 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  f../*.** An inst
660d0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
660e0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
660f0 63 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20  can be declared 
66100 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75  on a stack and u
66110 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74  sed.** to save t
66120 68 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f  he Parse.zAuthCo
66130 6e 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74  ntext value so t
66140 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
66150 73 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f  stored later..*/
66160 0a 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74  .struct AuthCont
66170 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68  ext {.  const ch
66180 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74  ar *zAuthContext
66190 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65 64  ;   /* Put saved
661a0 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74   Parse.zAuthCont
661b0 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 61  ext here */.  Pa
661c0 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20  rse *pParse;    
661d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
661e0 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65   Parse structure
661f0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69   */.};../*.** Bi
66200 74 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f 72  tfield flags for
66210 20 50 35 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f   P5 value in OP_
66220 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 65  Insert and OP_De
66230 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lete.*/.#define 
66240 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20  OPFLAG_NCHANGE  
66250 20 20 31 20 20 20 20 2f 2a 20 53 65 74 20 74 6f    1    /* Set to
66260 20 75 70 64 61 74 65 20 64 62 2d 3e 6e 43 68 61   update db->nCha
66270 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  nge */.#define O
66280 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20  PFLAG_LASTROWID 
66290 20 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20   2    /* Set to 
662a0 75 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52  update db->lastR
662b0 6f 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  owid */.#define 
662c0 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20  OPFLAG_ISUPDATE 
662d0 20 20 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f    4    /* This O
662e0 50 5f 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73  P_Insert is an s
662f0 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65  ql UPDATE */.#de
66300 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45  fine OPFLAG_APPE
66310 4e 44 20 20 20 20 20 38 20 20 20 20 2f 2a 20 54  ND     8    /* T
66320 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f  his is likely to
66330 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f   be an append */
66340 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
66350 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 31 36  USESEEKRESULT 16
66360 20 20 20 20 2f 2a 20 54 72 79 20 74 6f 20 61 76      /* Try to av
66370 6f 69 64 20 61 20 73 65 65 6b 20 69 6e 20 42 74  oid a seek in Bt
66380 72 65 65 49 6e 73 65 72 74 28 29 20 2a 2f 0a 0a  reeInsert() */..
66390 2f 2a 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67  /*. * Each trigg
663a0 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68  er present in th
663b0 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
663c0 61 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61  a is stored as a
663d0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a  n instance of. *
663e0 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 2e   struct Trigger.
663f0 20 0a 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73   . *. * Pointers
66400 20 74 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66   to instances of
66410 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20   struct Trigger 
66420 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77  are stored in tw
66430 6f 20 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e  o ways.. * 1. In
66440 20 74 68 65 20 22 74 72 69 67 48 61 73 68 22 20   the "trigHash" 
66450 68 61 73 68 20 74 61 62 6c 65 20 28 70 61 72 74  hash table (part
66460 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a   of the sqlite3*
66470 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73   that represents
66480 20 74 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61   the . *    data
66490 62 61 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f  base). This allo
664a0 77 73 20 54 72 69 67 67 65 72 20 73 74 72 75 63  ws Trigger struc
664b0 74 75 72 65 73 20 74 6f 20 62 65 20 72 65 74 72  tures to be retr
664c0 69 65 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20  ieved by name.. 
664d0 2a 20 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72  * 2. All trigger
664e0 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
664f0 68 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  h a single table
66500 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c   form a linked l
66510 69 73 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20  ist, using the. 
66520 2a 20 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65  *    pNext membe
66530 72 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67  r of struct Trig
66540 67 65 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74  ger. A pointer t
66550 6f 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d  o the first elem
66560 65 6e 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20  ent of the. *   
66570 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20   linked list is 
66580 73 74 6f 72 65 64 20 61 73 20 74 68 65 20 22 70  stored as the "p
66590 54 72 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20  Trigger" member 
665a0 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  of the associate
665b0 64 0a 20 2a 20 20 20 20 73 74 72 75 63 74 20 54  d. *    struct T
665c0 61 62 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20  able.. *. * The 
665d0 22 73 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62  "step_list" memb
665e0 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  er points to the
665f0 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
66600 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a  f a linked list.
66610 20 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68   * containing th
66620 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  e SQL statements
66630 20 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68   specified as th
66640 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  e trigger progra
66650 6d 2e 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72  m.. */.struct Tr
66660 69 67 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a  igger {.  char *
66670 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  name;           
66680 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66    /* The name of
66690 20 74 68 65 20 74 72 69 67 67 65 72 20 20 20 20   the trigger    
666a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
666b0 20 20 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74      */.  char *t
666c0 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  able;           
666d0 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72   /* The table or
666e0 20 76 69 65 77 20 74 6f 20 77 68 69 63 68 20 74   view to which t
666f0 68 65 20 74 72 69 67 67 65 72 20 61 70 70 6c 69  he trigger appli
66700 65 73 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20  es */.  u8 op;  
66710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66720 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c  /* One of TK_DEL
66730 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20  ETE, TK_UPDATE, 
66740 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20  TK_INSERT       
66750 20 20 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b    */.  u8 tr_tm;
66760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
66770 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52  * One of TRIGGER
66780 5f 42 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52  _BEFORE, TRIGGER
66790 5f 41 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72  _AFTER */.  Expr
667a0 20 2a 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20   *pWhen;        
667b0 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20      /* The WHEN 
667c0 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 65 78  clause of the ex
667d0 70 72 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65  pression (may be
667e0 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69   NULL) */.  IdLi
667f0 73 74 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20  st *pColumns;   
66800 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
66810 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c  s an UPDATE OF <
66820 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69  column-list> tri
66830 67 67 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  gger,.          
66840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66850 20 20 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c     the <column-l
66860 69 73 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68  ist> is stored h
66870 65 72 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e  ere */.  Token n
66880 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20  ameToken;       
66890 20 2f 2a 20 54 6f 6b 65 6e 20 63 6f 6e 74 61 69   /* Token contai
668a0 6e 69 6e 67 20 7a 4e 61 6d 65 2e 20 55 73 65 20  ning zName. Use 
668b0 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 20 6f  during parsing o
668c0 6e 6c 79 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20  nly */.  Schema 
668d0 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20  *pSchema;       
668e0 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61   /* Schema conta
668f0 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67 65  ining the trigge
66900 72 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70  r */.  Schema *p
66910 54 61 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f  TabSchema;     /
66920 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e  * Schema contain
66930 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f  ing the table */
66940 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
66950 73 74 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69  step_list; /* Li
66960 6e 6b 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67  nk list of trigg
66970 65 72 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73  er program steps
66980 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a               */.
66990 20 20 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74    Trigger *pNext
669a0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  ;         /* Nex
669b0 74 20 74 72 69 67 67 65 72 20 61 73 73 6f 63 69  t trigger associ
669c0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 61  ated with the ta
669d0 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ble */.};../*.**
669e0 20 41 20 74 72 69 67 67 65 72 20 69 73 20 65 69   A trigger is ei
669f0 74 68 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72  ther a BEFORE or
66a00 20 61 6e 20 41 46 54 45 52 20 74 72 69 67 67 65   an AFTER trigge
66a10 72 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  r.  The followin
66a20 67 20 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64  g constants.** d
66a30 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20  etermine which. 
66a40 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
66a50 61 72 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69  are multiple tri
66a60 67 67 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74  ggers, you might
66a70 20 6f 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20   of some BEFORE 
66a80 61 6e 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a  and some AFTER..
66a90 2a 2a 20 49 6e 20 74 68 61 74 20 63 61 73 65 73  ** In that cases
66aa0 2c 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20  , the constants 
66ab0 62 65 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65  below can be ORe
66ac0 64 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23  d together..*/.#
66ad0 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42  define TRIGGER_B
66ae0 45 46 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65  EFORE  1.#define
66af0 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 20   TRIGGER_AFTER  
66b00 20 32 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73   2../*. * An ins
66b10 74 61 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20  tance of struct 
66b20 54 72 69 67 67 65 72 53 74 65 70 20 69 73 20 75  TriggerStep is u
66b30 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73  sed to store a s
66b40 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d  ingle SQL statem
66b50 65 6e 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61  ent. * that is a
66b60 20 70 61 72 74 20 6f 66 20 61 20 74 72 69 67 67   part of a trigg
66b70 65 72 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a  er-program. . *.
66b80 20 2a 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20   * Instances of 
66b90 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 74  struct TriggerSt
66ba0 65 70 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e  ep are stored in
66bb0 20 61 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64   a singly linked
66bc0 20 6c 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a   list (linked. *
66bd0 20 75 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78   using the "pNex
66be0 74 22 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72  t" member) refer
66bf0 65 6e 63 65 64 20 62 79 20 74 68 65 20 22 73 74  enced by the "st
66c00 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20  ep_list" member 
66c10 6f 66 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63  of the . * assoc
66c20 69 61 74 65 64 20 73 74 72 75 63 74 20 54 72 69  iated struct Tri
66c30 67 67 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54  gger instance. T
66c40 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
66c50 20 6f 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c   of the linked l
66c60 69 73 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69  ist is. * the fi
66c70 72 73 74 20 73 74 65 70 20 6f 66 20 74 68 65 20  rst step of the 
66c80 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e  trigger-program.
66c90 0a 20 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22  . * . * The "op"
66ca0 20 6d 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65   member indicate
66cb0 73 20 77 68 65 74 68 65 72 20 74 68 69 73 20 69  s whether this i
66cc0 73 20 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49  s a "DELETE", "I
66cd0 4e 53 45 52 54 22 2c 20 22 55 50 44 41 54 45 22  NSERT", "UPDATE"
66ce0 20 6f 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20   or. * "SELECT" 
66cf0 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d  statement. The m
66d00 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f  eanings of the o
66d10 74 68 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20  ther members is 
66d20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68  determined by th
66d30 65 20 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22  e . * value of "
66d40 6f 70 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  op" as follows:.
66d50 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f   *. * (op == TK_
66d60 49 4e 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e  INSERT). * orcon
66d70 66 20 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74  f    -> stores t
66d80 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61  he ON CONFLICT a
66d90 6c 67 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c  lgorithm. * pSel
66da0 65 63 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73  ect   -> If this
66db0 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e   is an INSERT IN
66dc0 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e  TO ... SELECT ..
66dd0 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  . statement, the
66de0 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  n. *            
66df0 20 20 74 68 69 73 20 73 74 6f 72 65 73 20 61 20    this stores a 
66e00 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53  pointer to the S
66e10 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
66e20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e   Otherwise NULL.
66e30 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e  . * target    ->
66e40 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67   A token holding
66e50 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
66e60 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74   table to insert
66e70 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c   into.. * pExprL
66e80 69 73 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69  ist -> If this i
66e90 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f  s an INSERT INTO
66ea0 20 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20   ... VALUES ... 
66eb0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a  statement, then.
66ec0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
66ed0 74 68 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75  this stores valu
66ee0 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  es to be inserte
66ef0 64 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c  d. Otherwise NUL
66f00 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20  L.. * pIdList   
66f10 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  -> If this is an
66f20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e   INSERT INTO ...
66f30 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e   (<column-names>
66f40 29 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a  ) VALUES ... . *
66f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
66f60 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68  atement, then th
66f70 69 73 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f  is stores the co
66f80 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65  lumn-names to be
66f90 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  . *             
66fa0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a   inserted into..
66fb0 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f   *. * (op == TK_
66fc0 44 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65  DELETE). * targe
66fd0 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20  t    -> A token 
66fe0 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d 65  holding the name
66ff0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
67000 20 64 65 6c 65 74 65 20 66 72 6f 6d 2e 0a 20 2a   delete from.. *
67010 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68   pWhere    -> Th
67020 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  e WHERE clause o
67030 66 20 74 68 65 20 44 45 4c 45 54 45 20 73 74 61  f the DELETE sta
67040 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73  tement if one is
67050 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20   specified.. *  
67060 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65              Othe
67070 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a  rwise NULL.. * .
67080 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44   * (op == TK_UPD
67090 41 54 45 29 0a 20 2a 20 74 61 72 67 65 74 20 20  ATE). * target  
670a0 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 6f 6c    -> A token hol
670b0 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 20 6f 66  ding the name of
670c0 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 75 70   the table to up
670d0 64 61 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a  date rows of.. *
670e0 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68   pWhere    -> Th
670f0 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  e WHERE clause o
67100 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
67110 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73  tement if one is
67120 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20   specified.. *  
67130 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65              Othe
67140 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70  rwise NULL.. * p
67150 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69  ExprList -> A li
67160 73 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  st of the column
67170 73 20 74 6f 20 75 70 64 61 74 65 20 61 6e 64 20  s to update and 
67180 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
67190 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 20 20 20  to update. *    
671a0 20 20 20 20 20 20 20 20 20 20 74 68 65 6d 20 74            them t
671b0 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 33 55 70  o. See sqlite3Up
671c0 64 61 74 65 28 29 20 64 6f 63 75 6d 65 6e 74 61  date() documenta
671d0 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67 65  tion of "pChange
671e0 73 22 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  s". *           
671f0 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20     argument.. * 
67200 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67  . */.struct Trig
67210 67 65 72 53 74 65 70 20 7b 0a 20 20 69 6e 74 20  gerStep {.  int 
67220 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
67230 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45   /* One of TK_DE
67240 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c  LETE, TK_UPDATE,
67250 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53   TK_INSERT, TK_S
67260 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 6f  ELECT */.  int o
67270 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20  rconf;          
67280 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65  /* OE_Rollback e
67290 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  tc. */.  Trigger
672a0 20 2a 70 54 72 69 67 3b 20 20 20 20 20 20 2f 2a   *pTrig;      /*
672b0 20 54 68 65 20 74 72 69 67 67 65 72 20 74 68 61   The trigger tha
672c0 74 20 74 68 69 73 20 73 74 65 70 20 69 73 20 61  t this step is a
672d0 20 70 61 72 74 20 6f 66 20 2a 2f 0a 0a 20 20 53   part of */..  S
672e0 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
672f0 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72      /* Valid for
67300 20 53 45 4c 45 43 54 20 61 6e 64 20 73 6f 6d 65   SELECT and some
67310 74 69 6d 65 73 20 0a 20 20 20 20 20 20 20 20 20  times .         
67320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67330 20 49 4e 53 45 52 54 20 73 74 65 70 73 20 28 77   INSERT steps (w
67340 68 65 6e 20 70 45 78 70 72 4c 69 73 74 20 3d 3d  hen pExprList ==
67350 20 30 29 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74   0) */.  Token t
67360 61 72 67 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  arget;        /*
67370 20 56 61 6c 69 64 20 66 6f 72 20 44 45 4c 45 54   Valid for DELET
67380 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52  E, UPDATE, INSER
67390 54 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70  T steps */.  Exp
673a0 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20  r *pWhere;      
673b0 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 44    /* Valid for D
673c0 45 4c 45 54 45 2c 20 55 50 44 41 54 45 20 73 74  ELETE, UPDATE st
673d0 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  eps */.  ExprLis
673e0 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20 2f 2a  t *pExprList; /*
673f0 20 56 61 6c 69 64 20 66 6f 72 20 55 50 44 41 54   Valid for UPDAT
67400 45 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64  E statements and
67410 20 73 6f 6d 65 74 69 6d 65 73 20 0a 20 20 20 20   sometimes .    
67420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67430 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 73 74         INSERT st
67440 65 70 73 20 28 77 68 65 6e 20 70 53 65 6c 65 63  eps (when pSelec
67450 74 20 3d 3d 20 30 29 20 20 20 20 20 20 20 20 20  t == 0)         
67460 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 49 64  */.  IdList *pId
67470 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 56 61 6c  List;     /* Val
67480 69 64 20 66 6f 72 20 49 4e 53 45 52 54 20 73 74  id for INSERT st
67490 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 20 2a 2f  atements only */
674a0 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
674b0 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20  pNext;  /* Next 
674c0 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74  in the link-list
674d0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
674e0 70 20 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61  p *pLast;  /* La
674f0 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69  st element in li
67500 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66  nk-list. Valid f
67510 6f 72 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79  or 1st elem only
67520 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 20 2a 20 41 6e   */.};../*. * An
67530 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 73 74 72   instance of str
67540 75 63 74 20 54 72 69 67 67 65 72 53 74 61 63 6b  uct TriggerStack
67550 20 73 74 6f 72 65 73 20 69 6e 66 6f 72 6d 61 74   stores informat
67560 69 6f 6e 20 72 65 71 75 69 72 65 64 20 64 75 72  ion required dur
67570 69 6e 67 20 63 6f 64 65 0a 20 2a 20 67 65 6e 65  ing code. * gene
67580 72 61 74 69 6f 6e 20 6f 66 20 61 20 73 69 6e 67  ration of a sing
67590 6c 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  le trigger progr
675a0 61 6d 2e 20 57 68 69 6c 65 20 74 68 65 20 74 72  am. While the tr
675b0 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 69 73  igger program is
675c0 20 62 65 69 6e 67 0a 20 2a 20 63 6f 64 65 64 2c   being. * coded,
675d0 20 69 74 73 20 61 73 73 6f 63 69 61 74 65 64 20   its associated 
675e0 54 72 69 67 67 65 72 53 74 61 63 6b 20 69 6e 73  TriggerStack ins
675f0 74 61 6e 63 65 20 69 73 20 70 6f 69 6e 74 65 64  tance is pointed
67600 20 74 6f 20 62 79 20 74 68 65 0a 20 2a 20 22 70   to by the. * "p
67610 54 72 69 67 67 65 72 53 74 61 63 6b 22 20 6d 65  TriggerStack" me
67620 6d 62 65 72 20 6f 66 20 74 68 65 20 50 61 72 73  mber of the Pars
67630 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 2a 0a  e structure.. *.
67640 20 2a 20 54 68 65 20 70 54 61 62 20 6d 65 6d 62   * The pTab memb
67650 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  er points to the
67660 20 74 61 62 6c 65 20 74 68 61 74 20 74 72 69 67   table that trig
67670 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 63  gers are being c
67680 6f 64 65 64 20 6f 6e 2e 20 54 68 65 20 0a 20 2a  oded on. The . *
67690 20 6e 65 77 49 64 78 20 6d 65 6d 62 65 72 20 63   newIdx member c
676a0 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6e 64 65  ontains the inde
676b0 78 20 6f 66 20 74 68 65 20 76 64 62 65 20 63 75  x of the vdbe cu
676c0 72 73 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73  rsor that points
676d0 20 61 74 20 74 68 65 20 74 65 6d 70 0a 20 2a 20   at the temp. * 
676e0 74 61 62 6c 65 20 74 68 61 74 20 73 74 6f 72 65  table that store
676f0 73 20 74 68 65 20 6e 65 77 2e 2a 20 72 65 66 65  s the new.* refe
67700 72 65 6e 63 65 73 2e 20 49 66 20 6e 65 77 2e 2a  rences. If new.*
67710 20 72 65 66 65 72 65 6e 63 65 73 20 61 72 65 20   references are 
67720 6e 6f 74 20 76 61 6c 69 64 0a 20 2a 20 66 6f 72  not valid. * for
67730 20 74 68 65 20 74 72 69 67 67 65 72 20 62 65 69   the trigger bei
67740 6e 67 20 63 6f 64 65 64 20 28 66 6f 72 20 65 78  ng coded (for ex
67750 61 6d 70 6c 65 20 61 6e 20 4f 4e 20 44 45 4c 45  ample an ON DELE
67760 54 45 20 74 72 69 67 67 65 72 29 2c 20 74 68 65  TE trigger), the
67770 6e 20 6e 65 77 49 64 78 0a 20 2a 20 69 73 20 73  n newIdx. * is s
67780 65 74 20 74 6f 20 2d 31 2e 20 54 68 65 20 6f 6c  et to -1. The ol
67790 64 49 64 78 20 6d 65 6d 62 65 72 20 69 73 20 61  dIdx member is a
677a0 6e 61 6c 6f 67 6f 75 73 20 74 6f 20 6e 65 77 49  nalogous to newI
677b0 64 78 2c 20 66 6f 72 20 6f 6c 64 2e 2a 20 72 65  dx, for old.* re
677c0 66 65 72 65 6e 63 65 73 2e 0a 20 2a 0a 20 2a 20  ferences.. *. * 
677d0 54 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  The ON CONFLICT 
677e0 70 6f 6c 69 63 79 20 74 6f 20 62 65 20 75 73 65  policy to be use
677f0 64 20 66 6f 72 20 74 68 65 20 74 72 69 67 67 65  d for the trigge
67800 72 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20  r program steps 
67810 69 73 20 73 74 6f 72 65 64 20 0a 20 2a 20 61 73  is stored . * as
67820 20 74 68 65 20 6f 72 63 6f 6e 66 20 6d 65 6d 62   the orconf memb
67830 65 72 2e 20 49 66 20 74 68 69 73 20 69 73 20 4f  er. If this is O
67840 45 5f 44 65 66 61 75 6c 74 2c 20 74 68 65 6e 20  E_Default, then 
67850 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  the ON CONFLICT 
67860 63 6c 61 75 73 65 20 0a 20 2a 20 73 70 65 63 69  clause . * speci
67870 66 69 65 64 20 66 6f 72 20 69 6e 64 69 76 69 64  fied for individ
67880 75 61 6c 20 74 72 69 67 67 65 72 73 20 73 74 65  ual triggers ste
67890 70 73 20 69 73 20 75 73 65 64 2e 0a 20 2a 0a 20  ps is used.. *. 
678a0 2a 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  * struct Trigger
678b0 53 74 61 63 6b 20 68 61 73 20 61 20 22 70 4e 65  Stack has a "pNe
678c0 78 74 22 20 6d 65 6d 62 65 72 2c 20 74 6f 20 61  xt" member, to a
678d0 6c 6c 6f 77 20 6c 69 6e 6b 65 64 20 6c 69 73 74  llow linked list
678e0 73 20 74 6f 20 62 65 0a 20 2a 20 63 6f 6e 73 74  s to be. * const
678f0 72 75 63 74 65 64 2e 20 57 68 65 6e 20 63 6f 64  ructed. When cod
67900 69 6e 67 20 6e 65 73 74 65 64 20 74 72 69 67 67  ing nested trigg
67910 65 72 73 20 28 74 72 69 67 67 65 72 73 20 66 69  ers (triggers fi
67920 72 65 64 20 62 79 20 6f 74 68 65 72 20 74 72 69  red by other tri
67930 67 67 65 72 73 29 0a 20 2a 20 65 61 63 68 20 6e  ggers). * each n
67940 65 73 74 65 64 20 74 72 69 67 67 65 72 20 73 74  ested trigger st
67950 6f 72 65 73 20 69 74 73 20 70 61 72 65 6e 74 20  ores its parent 
67960 74 72 69 67 67 65 72 27 73 20 54 72 69 67 67 65  trigger's Trigge
67970 72 53 74 61 63 6b 20 61 73 20 74 68 65 20 22 70  rStack as the "p
67980 4e 65 78 74 22 20 0a 20 2a 20 70 6f 69 6e 74 65  Next" . * pointe
67990 72 2e 20 4f 6e 63 65 20 74 68 65 20 6e 65 73 74  r. Once the nest
679a0 65 64 20 74 72 69 67 67 65 72 20 68 61 73 20 62  ed trigger has b
679b0 65 65 6e 20 63 6f 64 65 64 2c 20 74 68 65 20 70  een coded, the p
679c0 4e 65 78 74 20 76 61 6c 75 65 20 69 73 20 72 65  Next value is re
679d0 73 74 6f 72 65 64 0a 20 2a 20 74 6f 20 74 68 65  stored. * to the
679e0 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 20 6d   pTriggerStack m
679f0 65 6d 62 65 72 20 6f 66 20 74 68 65 20 50 61 72  ember of the Par
67a00 73 65 20 73 74 75 63 74 75 72 65 20 61 6e 64 20  se stucture and 
67a10 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 70 61  coding of the pa
67a20 72 65 6e 74 0a 20 2a 20 74 72 69 67 67 65 72 20  rent. * trigger 
67a30 63 6f 6e 74 69 6e 75 65 73 2e 0a 20 2a 0a 20 2a  continues.. *. *
67a40 20 42 65 66 6f 72 65 20 61 20 6e 65 73 74 65 64   Before a nested
67a50 20 74 72 69 67 67 65 72 20 69 73 20 63 6f 64 65   trigger is code
67a60 64 2c 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69  d, the linked li
67a70 73 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  st pointed to by
67a80 20 74 68 65 20 0a 20 2a 20 70 54 72 69 67 67 65   the . * pTrigge
67a90 72 53 74 61 63 6b 20 69 73 20 73 63 61 6e 6e 65  rStack is scanne
67aa0 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  d to ensure that
67ab0 20 74 68 65 20 74 72 69 67 67 65 72 20 69 73 20   the trigger is 
67ac0 6e 6f 74 20 61 62 6f 75 74 20 74 6f 20 62 65 20  not about to be 
67ad0 63 6f 64 65 64 0a 20 2a 20 72 65 63 75 72 73 69  coded. * recursi
67ae0 76 65 6c 79 2e 20 49 66 20 74 68 69 73 20 63 6f  vely. If this co
67af0 6e 64 69 74 69 6f 6e 20 69 73 20 64 65 74 65 63  ndition is detec
67b00 74 65 64 2c 20 74 68 65 20 6e 65 73 74 65 64 20  ted, the nested 
67b10 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 63  trigger is not c
67b20 6f 64 65 64 2e 0a 20 2a 2f 0a 73 74 72 75 63 74  oded.. */.struct
67b30 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 7b 0a   TriggerStack {.
67b40 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
67b50 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
67b60 74 68 61 74 20 74 72 69 67 67 65 72 73 20 61 72  that triggers ar
67b70 65 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e  e currently bein
67b80 67 20 63 6f 64 65 64 20 6f 6e 20 2a 2f 0a 20 20  g coded on */.  
67b90 69 6e 74 20 6e 65 77 49 64 78 3b 20 20 20 20 20  int newIdx;     
67ba0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
67bb0 20 76 64 62 65 20 63 75 72 73 6f 72 20 74 6f 20   vdbe cursor to 
67bc0 22 6e 65 77 22 20 74 65 6d 70 20 74 61 62 6c 65  "new" temp table
67bd0 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78   */.  int oldIdx
67be0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
67bf0 64 65 78 20 6f 66 20 76 64 62 65 20 63 75 72 73  dex of vdbe curs
67c00 6f 72 20 74 6f 20 22 6f 6c 64 22 20 74 65 6d 70  or to "old" temp
67c10 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 33 32 20   table */.  u32 
67c20 6e 65 77 43 6f 6c 4d 61 73 6b 3b 0a 20 20 75 33  newColMask;.  u3
67c30 32 20 6f 6c 64 43 6f 6c 4d 61 73 6b 3b 0a 20 20  2 oldColMask;.  
67c40 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20  int orconf;     
67c50 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
67c60 6f 72 63 6f 6e 66 20 70 6f 6c 69 63 79 20 2a 2f  orconf policy */
67c70 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 4a 75 6d  .  int ignoreJum
67c80 70 3b 20 20 20 20 20 20 2f 2a 20 77 68 65 72 65  p;      /* where
67c90 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20   to jump to for 
67ca0 61 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20  a RAISE(IGNORE) 
67cb0 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
67cc0 72 69 67 67 65 72 3b 20 20 20 2f 2a 20 54 68 65  rigger;   /* The
67cd0 20 74 72 69 67 67 65 72 20 63 75 72 72 65 6e 74   trigger current
67ce0 6c 79 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a  ly being coded *
67cf0 2f 0a 20 20 54 72 69 67 67 65 72 53 74 61 63 6b  /.  TriggerStack
67d00 20 2a 70 4e 65 78 74 3b 20 2f 2a 20 4e 65 78 74   *pNext; /* Next
67d10 20 74 72 69 67 67 65 72 20 64 6f 77 6e 20 6f 6e   trigger down on
67d20 20 74 68 65 20 74 72 69 67 67 65 72 20 73 74 61   the trigger sta
67d30 63 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ck */.};../*.** 
67d40 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  The following st
67d50 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
67d60 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65   information use
67d70 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 46  d by the sqliteF
67d80 69 78 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65  ix....** routine
67d90 73 20 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74  s as they walk t
67da0 68 65 20 70 61 72 73 65 20 74 72 65 65 20 74 6f  he parse tree to
67db0 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20 72   make database r
67dc0 65 66 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70  eferences.** exp
67dd0 6c 69 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65  licit.  .*/.type
67de0 64 65 66 20 73 74 72 75 63 74 20 44 62 46 69 78  def struct DbFix
67df0 65 72 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75  er DbFixer;.stru
67e00 63 74 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50  ct DbFixer {.  P
67e10 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
67e20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
67e30 67 20 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f  g context.  Erro
67e40 72 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74  r messages writt
67e50 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e  en here */.  con
67e60 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  st char *zDb;   
67e70 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c   /* Make sure al
67e80 6c 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f  l objects are co
67e90 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20  ntained in this 
67ea0 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f  database */.  co
67eb0 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b  nst char *zType;
67ec0 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65    /* Type of the
67ed0 20 63 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65   container - use
67ee0 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73  d for error mess
67ef0 61 67 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ages */.  const 
67f00 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a  Token *pName; /*
67f10 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e   Name of the con
67f20 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f  tainer - used fo
67f30 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  r error messages
67f40 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
67f50 20 6f 62 6a 65 63 74 65 64 20 75 73 65 64 20 74   objected used t
67f60 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65  o accumulate the
67f70 20 74 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e   text of a strin
67f80 67 20 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f  g where we.** do
67f90 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
67fa0 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68   know how big th
67fb0 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65  e string will be
67fc0 20 69 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a   in the end..*/.
67fd0 73 74 72 75 63 74 20 53 74 72 41 63 63 75 6d 20  struct StrAccum 
67fe0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
67ff0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69           /* Opti
68000 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 6f  onal database fo
68010 72 20 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61  r lookaside.  Ca
68020 6e 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  n be NULL */.  c
68030 68 61 72 20 2a 7a 42 61 73 65 3b 20 20 20 20 20  har *zBase;     
68040 20 20 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c      /* A base al
68050 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66  location.  Not f
68060 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20  rom malloc. */. 
68070 20 63 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20   char *zText;   
68080 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72        /* The str
68090 69 6e 67 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f  ing collected so
680a0 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e   far */.  int  n
680b0 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f  Char;          /
680c0 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  * Length of the 
680d0 73 74 72 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f  string so far */
680e0 0a 20 20 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20  .  int  nAlloc; 
680f0 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e          /* Amoun
68100 74 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63  t of space alloc
68110 61 74 65 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f  ated in zText */
68120 0a 20 20 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b  .  int  mxAlloc;
68130 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
68140 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e  um allowed strin
68150 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38  g length */.  u8
68160 20 20 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b     mallocFailed;
68170 20 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72     /* Becomes tr
68180 75 65 20 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79  ue if any memory
68190 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
681a0 73 20 2a 2f 0a 20 20 75 38 20 20 20 75 73 65 4d  s */.  u8   useM
681b0 61 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54  alloc;      /* T
681c0 72 75 65 20 69 66 20 7a 54 65 78 74 20 69 73 20  rue if zText is 
681d0 65 6e 6c 61 72 67 65 61 62 6c 65 20 75 73 69 6e  enlargeable usin
681e0 67 20 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75  g realloc */.  u
681f0 38 20 20 20 74 6f 6f 42 69 67 3b 20 20 20 20 20  8   tooBig;     
68200 20 20 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74      /* Becomes t
68210 72 75 65 20 69 66 20 73 74 72 69 6e 67 20 73 69  rue if string si
68220 7a 65 20 65 78 63 65 65 64 73 20 6c 69 6d 69 74  ze exceeds limit
68230 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  s */.};../*.** A
68240 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
68250 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
68260 65 64 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74  ed to communicat
68270 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  e information.**
68280 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69   from sqlite3Ini
68290 74 20 61 6e 64 20 4f 50 5f 50 61 72 73 65 53 63  t and OP_ParseSc
682a0 68 65 6d 61 20 69 6e 74 6f 20 74 68 65 20 73 71  hema into the sq
682b0 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63  lite3InitCallbac
682c0 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  k..*/.typedef st
682d0 72 75 63 74 20 7b 0a 20 20 73 71 6c 69 74 65 33  ruct {.  sqlite3
682e0 20 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20   *db;        /* 
682f0 54 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69  The database bei
68300 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  ng initialized *
68310 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
68320 20 20 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72          /* 0 for
68330 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20   main database. 
68340 20 31 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e   1 for TEMP, 2..
68350 20 66 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f   for ATTACHed */
68360 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  .  char **pzErrM
68370 73 67 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20  sg;    /* Error 
68380 6d 65 73 73 61 67 65 20 73 74 6f 72 65 64 20 68  message stored h
68390 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ere */.  int rc;
683a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
683b0 52 65 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72  Result code stor
683c0 65 64 20 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69  ed here */.} Ini
683d0 74 44 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74  tData;../*.** St
683e0 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69  ructure containi
683f0 6e 67 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67  ng global config
68400 75 72 61 74 69 6f 6e 20 64 61 74 61 20 66 6f 72  uration data for
68410 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
68420 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ary..**.** This 
68430 73 74 72 75 63 74 75 72 65 20 61 6c 73 6f 20 63  structure also c
68440 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 73 74 61  ontains some sta
68450 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  te information..
68460 2a 2f 0a 73 74 72 75 63 74 20 53 71 6c 69 74 65  */.struct Sqlite
68470 33 43 6f 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20  3Config {.  int 
68480 62 4d 65 6d 73 74 61 74 3b 20 20 20 20 20 20 20  bMemstat;       
68490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
684a0 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20   True to enable 
684b0 6d 65 6d 6f 72 79 20 73 74 61 74 75 73 20 2a 2f  memory status */
684c0 0a 20 20 69 6e 74 20 62 43 6f 72 65 4d 75 74 65  .  int bCoreMute
684d0 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
684e0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
684f0 65 6e 61 62 6c 65 20 63 6f 72 65 20 6d 75 74 65  enable core mute
68500 78 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46  xing */.  int bF
68510 75 6c 6c 4d 75 74 65 78 3b 20 20 20 20 20 20 20  ullMutex;       
68520 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
68530 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 66 75  rue to enable fu
68540 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20  ll mutexing */. 
68550 20 69 6e 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20   int mxStrlen;  
68560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68570 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74     /* Maximum st
68580 72 69 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  ring length */. 
68590 20 69 6e 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65   int szLookaside
685a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
685b0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f     /* Default lo
685c0 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73  okaside buffer s
685d0 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f  ize */.  int nLo
685e0 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20  okaside;        
685f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
68600 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20  fault lookaside 
68610 62 75 66 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a  buffer count */.
68620 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65    sqlite3_mem_me
68630 74 68 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20  thods m;        
68640 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c      /* Low-level
68650 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
68660 6f 6e 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a  on interface */.
68670 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
68680 6d 65 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20  methods mutex;  
68690 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c      /* Low-level
686a0 20 6d 75 74 65 78 20 69 6e 74 65 72 66 61 63 65   mutex interface
686b0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63   */.  sqlite3_pc
686c0 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 70 63 61  ache_methods pca
686d0 63 68 65 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c  che;    /* Low-l
686e0 65 76 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20  evel page-cache 
686f0 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76  interface */.  v
68700 6f 69 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20  oid *pHeap;     
68710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68720 20 2f 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65   /* Heap storage
68730 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20   space */.  int 
68740 6e 48 65 61 70 3b 20 20 20 20 20 20 20 20 20 20  nHeap;          
68750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
68760 20 53 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d   Size of pHeap[]
68770 20 2a 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c   */.  int mnReq,
68780 20 6d 78 52 65 71 3b 20 20 20 20 20 20 20 20 20   mxReq;         
68790 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61          /* Min a
687a0 6e 64 20 6d 61 78 20 68 65 61 70 20 72 65 71 75  nd max heap requ
687b0 65 73 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20  ests sizes */.  
687c0 76 6f 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20  void *pScratch; 
687d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
687e0 20 20 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d    /* Scratch mem
687f0 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53  ory */.  int szS
68800 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20  cratch;         
68810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
68820 7a 65 20 6f 66 20 65 61 63 68 20 73 63 72 61 74  ze of each scrat
68830 63 68 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69  ch buffer */.  i
68840 6e 74 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20  nt nScratch;    
68850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68860 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63   /* Number of sc
68870 72 61 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f  ratch buffers */
68880 0a 20 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20  .  void *pPage; 
68890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
688a0 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63       /* Page cac
688b0 68 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69  he memory */.  i
688c0 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20  nt szPage;      
688d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
688e0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68   /* Size of each
688f0 20 70 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d   page in pPage[]
68900 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b   */.  int nPage;
68910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68920 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
68930 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50  r of pages in pP
68940 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d  age[] */.  int m
68950 78 50 61 72 73 65 72 53 74 61 63 6b 3b 20 20 20  xParserStack;   
68960 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
68970 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66  maximum depth of
68980 20 74 68 65 20 70 61 72 73 65 72 20 73 74 61 63   the parser stac
68990 6b 20 2a 2f 0a 20 20 69 6e 74 20 73 68 61 72 65  k */.  int share
689a0 64 43 61 63 68 65 45 6e 61 62 6c 65 64 3b 20 20  dCacheEnabled;  
689b0 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65           /* true
689c0 20 69 66 20 73 68 61 72 65 64 2d 63 61 63 68 65   if shared-cache
689d0 20 6d 6f 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f   mode enabled */
689e0 0a 20 20 2f 2a 20 54 68 65 20 61 62 6f 76 65 20  .  /* The above 
689f0 6d 69 67 68 74 20 62 65 20 69 6e 69 74 69 61 6c  might be initial
68a00 69 7a 65 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f  ized to non-zero
68a10 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
68a20 20 6e 65 65 64 20 74 6f 20 61 6c 77 61 79 73 0a   need to always.
68a30 20 20 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62    ** initially b
68a40 65 20 7a 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e  e zero, however.
68a50 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74   */.  int isInit
68a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
68a70 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
68a80 61 66 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61  after initializa
68a90 74 69 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65  tion has finishe
68aa0 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f  d */.  int inPro
68ab0 67 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20  gress;          
68ac0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
68ad0 20 77 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a   while initializ
68ae0 61 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73  ation in progres
68af0 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 61 6c  s */.  int isMal
68b00 6c 6f 63 49 6e 69 74 3b 20 20 20 20 20 20 20 20  locInit;        
68b10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
68b20 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73   after malloc is
68b30 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
68b40 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
68b50 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 20  *pInitMutex;    
68b60 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65      /* Mutex use
68b70 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69  d by sqlite3_ini
68b80 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 69  tialize() */.  i
68b90 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 78  nt nRefInitMutex
68ba0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
68bb0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73   /* Number of us
68bc0 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74 65  ers of pInitMute
68bd0 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  x */.};../*.** C
68be0 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 70  ontext pointer p
68bf0 61 73 73 65 64 20 64 6f 77 6e 20 74 68 72 6f 75  assed down throu
68c00 67 68 20 74 68 65 20 74 72 65 65 2d 77 61 6c 6b  gh the tree-walk
68c10 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c 6b  ..*/.struct Walk
68c20 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 45 78  er {.  int (*xEx
68c30 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b  prCallback)(Walk
68c40 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 20 20 20  er*, Expr*);    
68c50 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72   /* Callback for
68c60 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a   expressions */.
68c70 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 63 74 43    int (*xSelectC
68c80 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a  allback)(Walker*
68c90 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 43  ,Select*);  /* C
68ca0 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 45 4c 45  allback for SELE
68cb0 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  CTs */.  Parse *
68cc0 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
68cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68ce0 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
68cf0 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 6f  text.  */.  unio
68d00 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  n {             
68d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68d20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 64        /* Extra d
68d30 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 61 63 6b  ata for callback
68d40 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74   */.    NameCont
68d50 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 20 20 20  ext *pNC;       
68d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68d70 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f 6e     /* Naming con
68d80 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  text */.    int 
68d90 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
68da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68db0 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
68dc0 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75  r value */.  } u
68dd0 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  ;.};../* Forward
68de0 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f   declarations */
68df0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
68e00 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  int sqlite3WalkE
68e10 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70  xpr(Walker*, Exp
68e20 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
68e30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
68e40 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b  alkExprList(Walk
68e50 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  er*, ExprList*);
68e60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
68e70 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  int sqlite3WalkS
68e80 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a 2c 20 53  elect(Walker*, S
68e90 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
68ea0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
68eb0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70  te3WalkSelectExp
68ec0 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63  r(Walker*, Selec
68ed0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
68ee0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
68ef0 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 61  alkSelectFrom(Wa
68f00 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b  lker*, Select*);
68f10 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63  ../*.** Return c
68f20 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72  ode from the par
68f30 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 6e 67 20  se-tree walking 
68f40 70 72 69 6d 69 74 69 76 65 73 20 61 6e 64 20 74  primitives and t
68f50 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  heir.** callback
68f60 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52  s..*/.#define WR
68f70 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 30 20  C_Continue    0 
68f80 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 64 6f    /* Continue do
68f90 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 72 65 6e  wn into children
68fa0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f   */.#define WRC_
68fb0 50 72 75 6e 65 20 20 20 20 20 20 20 31 20 20 20  Prune       1   
68fc0 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 72 65 6e  /* Omit children
68fd0 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20 77 61   but continue wa
68fe0 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 73 20 2a  lking siblings *
68ff0 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41 62  /.#define WRC_Ab
69000 6f 72 74 20 20 20 20 20 20 20 32 20 20 20 2f 2a  ort       2   /*
69010 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65   Abandon the tre
69020 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  e walk */../*.**
69030 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f   Assuming zIn po
69040 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73  ints to the firs
69050 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d  t byte of a UTF-
69060 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20  8 character,.** 
69070 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70  advance zIn to p
69080 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73  oint to the firs
69090 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65  t byte of the ne
690a0 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  xt UTF-8 charact
690b0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  er..*/.#define S
690c0 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
690d0 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20  zIn) {          
690e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
690f0 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29    if( (*(zIn++))
69100 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20  >=0xc0 ){       
69110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
69120 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69         \.    whi
69130 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30  le( (*zIn & 0xc0
69140 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b  )==0x80 ){ zIn++
69150 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20  ; }             
69160 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20  \.  }           
69170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
69180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
69190 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a           \.}../*
691a0 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43  .** The SQLITE_C
691b0 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72  ORRUPT_BKPT macr
691c0 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  o can be either 
691d0 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20  a constant (for 
691e0 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75  production.** bu
691f0 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74  ilds) or a funct
69200 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65  ion call (for de
69210 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74  bugging).  If it
69220 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63   is a function c
69230 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77  all,.** it allow
69240 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74  s the operator t
69250 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69  o set a breakpoi
69260 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20 77  nt at the spot w
69270 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  here database.**
69280 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66   corruption is f
69290 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a  irst detected..*
692a0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
692b0 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
692c0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
692d0 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b  e3Corrupt(void);
692e0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
692f0 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71  _CORRUPT_BKPT sq
69300 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23  lite3Corrupt().#
69310 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
69320 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
69330 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  T SQLITE_CORRUPT
69340 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
69350 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
69360 72 6f 73 20 6d 69 6d 69 63 20 74 68 65 20 73 74  ros mimic the st
69370 61 6e 64 61 72 64 20 6c 69 62 72 61 72 79 20 66  andard library f
69380 75 6e 63 74 69 6f 6e 73 20 74 6f 75 70 70 65 72  unctions toupper
69390 28 29 2c 0a 2a 2a 20 69 73 73 70 61 63 65 28 29  (),.** isspace()
693a0 2c 20 69 73 61 6c 6e 75 6d 28 29 2c 20 69 73 64  , isalnum(), isd
693b0 69 67 69 74 28 29 20 61 6e 64 20 69 73 78 64 69  igit() and isxdi
693c0 67 69 74 28 29 2c 20 72 65 73 70 65 63 74 69 76  git(), respectiv
693d0 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 73 71 6c 69  ely. The.** sqli
693e0 74 65 20 76 65 72 73 69 6f 6e 73 20 6f 6e 6c 79  te versions only
693f0 20 77 6f 72 6b 20 66 6f 72 20 41 53 43 49 49 20   work for ASCII 
69400 63 68 61 72 61 63 74 65 72 73 2c 20 72 65 67 61  characters, rega
69410 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c 65  rdless of locale
69420 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
69430 54 45 5f 41 53 43 49 49 0a 23 20 64 65 66 69 6e  TE_ASCII.# defin
69440 65 20 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72  e sqlite3Toupper
69450 28 78 29 20 20 28 28 78 29 26 7e 28 73 71 6c 69  (x)  ((x)&~(sqli
69460 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73  te3CtypeMap[(uns
69470 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26  igned char)(x)]&
69480 30 78 32 30 29 29 0a 23 20 64 65 66 69 6e 65 20  0x20)).# define 
69490 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 78  sqlite3Isspace(x
694a0 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70  )   (sqlite3Ctyp
694b0 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63  eMap[(unsigned c
694c0 68 61 72 29 28 78 29 5d 26 30 78 30 31 29 0a 23  har)(x)]&0x01).#
694d0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
694e0 73 61 6c 6e 75 6d 28 78 29 20 20 20 28 73 71 6c  salnum(x)   (sql
694f0 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e  ite3CtypeMap[(un
69500 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d  signed char)(x)]
69510 26 30 78 30 36 29 0a 23 20 64 65 66 69 6e 65 20  &0x06).# define 
69520 73 71 6c 69 74 65 33 49 73 61 6c 70 68 61 28 78  sqlite3Isalpha(x
69530 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70  )   (sqlite3Ctyp
69540 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63  eMap[(unsigned c
69550 68 61 72 29 28 78 29 5d 26 30 78 30 32 29 0a 23  har)(x)]&0x02).#
69560 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
69570 73 64 69 67 69 74 28 78 29 20 20 20 28 73 71 6c  sdigit(x)   (sql
69580 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e  ite3CtypeMap[(un
69590 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d  signed char)(x)]
695a0 26 30 78 30 34 29 0a 23 20 64 65 66 69 6e 65 20  &0x04).# define 
695b0 73 71 6c 69 74 65 33 49 73 78 64 69 67 69 74 28  sqlite3Isxdigit(
695c0 78 29 20 20 28 73 71 6c 69 74 65 33 43 74 79 70  x)  (sqlite3Ctyp
695d0 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63  eMap[(unsigned c
695e0 68 61 72 29 28 78 29 5d 26 30 78 30 38 29 0a 23  har)(x)]&0x08).#
695f0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54   define sqlite3T
69600 6f 6c 6f 77 65 72 28 78 29 20 20 20 28 73 71 6c  olower(x)   (sql
69610 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
69620 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  [(unsigned char)
69630 28 78 29 5d 29 0a 23 65 6c 73 65 0a 23 20 69 6e  (x)]).#else.# in
69640 63 6c 75 64 65 20 3c 63 74 79 70 65 2e 68 3e 0a  clude <ctype.h>.
69650 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
69660 54 6f 75 70 70 65 72 28 78 29 20 20 20 74 6f 75  Toupper(x)   tou
69670 70 70 65 72 28 28 75 6e 73 69 67 6e 65 64 20 63  pper((unsigned c
69680 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e  har)(x)).# defin
69690 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  e sqlite3Isspace
696a0 28 78 29 20 20 20 69 73 73 70 61 63 65 28 28 75  (x)   isspace((u
696b0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29  nsigned char)(x)
696c0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
696d0 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 69  e3Isalnum(x)   i
696e0 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 6e 65 64  salnum((unsigned
696f0 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66   char)(x)).# def
69700 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70  ine sqlite3Isalp
69710 68 61 28 78 29 20 20 20 69 73 61 6c 70 68 61 28  ha(x)   isalpha(
69720 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
69730 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  x)).# define sql
69740 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20  ite3Isdigit(x)  
69750 20 69 73 64 69 67 69 74 28 28 75 6e 73 69 67 6e   isdigit((unsign
69760 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
69770 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78  efine sqlite3Isx
69780 64 69 67 69 74 28 78 29 20 20 69 73 78 64 69 67  digit(x)  isxdig
69790 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  it((unsigned cha
697a0 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20  r)(x)).# define 
697b0 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78  sqlite3Tolower(x
697c0 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 75 6e 73  )   tolower((uns
697d0 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a  igned char)(x)).
697e0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
697f0 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20  ternal function 
69800 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51  prototypes.*/.SQ
69810 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
69820 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
69830 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f  const char *, co
69840 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nst char *);.SQL
69850 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
69860 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
69870 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f  const char *, co
69880 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29  nst char *, int)
69890 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
698a0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75   int sqlite3IsNu
698b0 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a  mber(const char*
698c0 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c  , int*, u8);.SQL
698d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
698e0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
698f0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a 53  const char*);..S
69900 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
69910 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49  t sqlite3MallocI
69920 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  nit(void);.SQLIT
69930 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
69940 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28  qlite3MallocEnd(
69950 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
69960 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
69970 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a  te3Malloc(int);.
69980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
69990 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c  oid *sqlite3Mall
699a0 6f 63 5a 65 72 6f 28 69 6e 74 29 3b 0a 53 51 4c  ocZero(int);.SQL
699b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
699c0 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f   *sqlite3DbMallo
699d0 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 2a 2c 20  cZero(sqlite3*, 
699e0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
699f0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
69a00 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71  e3DbMallocRaw(sq
69a10 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
69a20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
69a30 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 44  r *sqlite3DbStrD
69a40 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  up(sqlite3*,cons
69a50 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
69a60 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
69a70 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
69a80 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
69a90 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  har*, int);.SQLI
69aa0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
69ab0 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28  *sqlite3Realloc(
69ac0 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  void*, int);.SQL
69ad0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
69ae0 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c   *sqlite3DbReall
69af0 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33  ocOrFree(sqlite3
69b00 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 29   *, void *, int)
69b10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
69b20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
69b30 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20  Realloc(sqlite3 
69b40 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 6e 74 29 3b  *, void *, int);
69b50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
69b60 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72  void sqlite3DbFr
69b70 65 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69  ee(sqlite3*, voi
69b80 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
69b90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
69ba0 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 2a 29  allocSize(void*)
69bb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
69bc0 20 69 6e 74 20 73 71 6c 69 74 65 33 44 62 4d 61   int sqlite3DbMa
69bd0 6c 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74 65 33  llocSize(sqlite3
69be0 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54  *, void*);.SQLIT
69bf0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
69c00 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61  sqlite3ScratchMa
69c10 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
69c20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
69c30 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65  qlite3ScratchFre
69c40 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  e(void*);.SQLITE
69c50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
69c60 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63  qlite3PageMalloc
69c70 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.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 50 61 67 65 46 72 65 65 28 76 6f 69 64 2a  e3PageFree(void*
69ca0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
69cb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
69cc0 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64  mSetDefault(void
69cd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
69ce0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
69cf0 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28  nignMallocHooks(
69d00 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 2c 20  void (*)(void), 
69d10 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 29 29 3b  void (*)(void));
69d20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
69d30 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72  int sqlite3Memor
69d40 79 41 6c 61 72 6d 28 76 6f 69 64 20 28 2a 29 28  yAlarm(void (*)(
69d50 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69  void*, sqlite3_i
69d60 6e 74 36 34 2c 20 69 6e 74 29 2c 20 76 6f 69 64  nt64, int), void
69d70 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
69d80 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
69d90 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33  E_ENABLE_MEMSYS3
69da0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
69db0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
69dc0 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74  m_methods *sqlit
69dd0 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28  e3MemGetMemsys3(
69de0 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69  void);.#endif.#i
69df0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
69e00 4c 45 5f 4d 45 4d 53 59 53 35 0a 53 51 4c 49 54  LE_MEMSYS5.SQLIT
69e10 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
69e20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
69e30 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47  ods *sqlite3MemG
69e40 65 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 3b  etMemsys5(void);
69e50 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65  .#endif...#ifnde
69e60 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  f SQLITE_MUTEX_O
69e70 4d 49 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  MIT.SQLITE_PRIVA
69e80 54 45 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  TE   sqlite3_mut
69e90 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  ex_methods *sqli
69ea0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
69eb0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
69ec0 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 5f  IVATE   sqlite3_
69ed0 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 4d 75  mutex *sqlite3Mu
69ee0 74 65 78 41 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53  texAlloc(int);.S
69ef0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
69f00 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78  int sqlite3Mutex
69f10 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49  Init(void);.SQLI
69f20 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
69f30 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64   sqlite3MutexEnd
69f40 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a  (void);.#endif..
69f50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
69f60 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  nt sqlite3Status
69f70 56 61 6c 75 65 28 69 6e 74 29 3b 0a 53 51 4c 49  Value(int);.SQLI
69f80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
69f90 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
69fa0 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  (int, int);.SQLI
69fb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
69fc0 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74  sqlite3StatusSet
69fd0 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c  (int, int);..SQL
69fe0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
69ff0 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75  sqlite3IsNaN(dou
6a000 62 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  ble);..SQLITE_PR
6a010 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6a020 65 33 56 58 50 72 69 6e 74 66 28 53 74 72 41 63  e3VXPrintf(StrAc
6a030 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  cum*, int, const
6a040 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29   char*, va_list)
6a050 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6a060 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 50   char *sqlite3MP
6a070 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c 63  rintf(sqlite3*,c
6a080 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
6a090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6a0a0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d   char *sqlite3VM
6a0b0 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 2a 2c  Printf(sqlite3*,
6a0c0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f  const char*, va_
6a0d0 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  list);.SQLITE_PR
6a0e0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
6a0f0 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c 69  te3MAppendf(sqli
6a100 74 65 33 2a 2c 63 68 61 72 2a 2c 63 6f 6e 73 74  te3*,char*,const
6a110 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 69 66   char*,...);.#if
6a120 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
6a130 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64  TEST) || defined
6a140 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 53  (SQLITE_DEBUG).S
6a150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6a160 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 62 75  void sqlite3Debu
6a170 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  gPrintf(const ch
6a180 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 65 6e 64 69  ar*, ...);.#endi
6a190 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  f.#if defined(SQ
6a1a0 4c 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54  LITE_TEST).SQLIT
6a1b0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
6a1c0 20 2a 73 71 6c 69 74 65 33 54 65 73 74 54 65 78   *sqlite3TestTex
6a1d0 74 54 6f 50 74 72 28 63 6f 6e 73 74 20 63 68 61  tToPtr(const cha
6a1e0 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  r*);.#endif.SQLI
6a1f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6a200 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
6a210 28 63 68 61 72 20 2a 2a 2c 20 73 71 6c 69 74 65  (char **, sqlite
6a220 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  3*, const char*,
6a230 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52   ...);.SQLITE_PR
6a240 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6a250 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65  e3ErrorMsg(Parse
6a260 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
6a270 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
6a280 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6a290 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73  3ErrorClear(Pars
6a2a0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
6a2b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44  ATE int sqlite3D
6a2c0 65 71 75 6f 74 65 28 63 68 61 72 2a 29 3b 0a 53  equote(char*);.S
6a2d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6a2e0 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64  t sqlite3Keyword
6a2f0 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67  Code(const unsig
6a300 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b  ned char*, int);
6a310 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6a320 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61  int sqlite3RunPa
6a330 72 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f 6e  rser(Parse*, con
6a340 73 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20 2a  st char*, char *
6a350 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6a360 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
6a370 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 73  inishCoding(Pars
6a380 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
6a390 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
6a3a0 65 74 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a  etTempReg(Parse*
6a3b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6a3c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
6a3d0 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72  leaseTempReg(Par
6a3e0 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  se*,int);.SQLITE
6a3f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6a400 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
6a410 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Parse*,int);.SQ
6a420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6a430 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65  d sqlite3Release
6a440 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a  TempRange(Parse*
6a450 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
6a460 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
6a470 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69  sqlite3Expr(sqli
6a480 74 65 33 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a  te3*, int, Expr*
6a490 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54  , Expr*, const T
6a4a0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6a4b0 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
6a4c0 69 74 65 33 50 45 78 70 72 28 50 61 72 73 65 2a  ite3PExpr(Parse*
6a4d0 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45 78  , int, Expr*, Ex
6a4e0 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  pr*, const Token
6a4f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6a500 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
6a510 52 65 67 69 73 74 65 72 45 78 70 72 28 50 61 72  RegisterExpr(Par
6a520 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  se*,Token*);.SQL
6a530 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
6a540 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64   *sqlite3ExprAnd
6a550 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c  (sqlite3*,Expr*,
6a560 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
6a570 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6a580 69 74 65 33 45 78 70 72 53 70 61 6e 28 45 78 70  ite3ExprSpan(Exp
6a590 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  r*,Token*,Token*
6a5a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6a5b0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
6a5c0 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 73  xprFunction(Pars
6a5d0 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f  e*,ExprList*, To
6a5e0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6a5f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6a600 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e  e3ExprAssignVarN
6a610 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45 78  umber(Parse*, Ex
6a620 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
6a630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6a640 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69 74  3ExprClear(sqlit
6a650 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  e3*, Expr*);.SQL
6a660 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6a670 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
6a680 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70  te(sqlite3*, Exp
6a690 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6a6a0 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71  ATE ExprList *sq
6a6b0 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
6a6c0 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72 4c  end(Parse*,ExprL
6a6d0 69 73 74 2a 2c 45 78 70 72 2a 2c 54 6f 6b 65 6e  ist*,Expr*,Token
6a6e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6a6f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6a700 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71  xprListDelete(sq
6a710 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74  lite3*, ExprList
6a720 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6a730 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
6a740 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61  it(sqlite3*, cha
6a750 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r**);.SQLITE_PRI
6a760 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6a770 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69  InitCallback(voi
6a780 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c  d*, int, char**,
6a790 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45   char**);.SQLITE
6a7a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6a7b0 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 73  lite3Pragma(Pars
6a7c0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
6a7d0 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51  ,Token*,int);.SQ
6a7e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6a7f0 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e  d sqlite3ResetIn
6a800 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c  ternalSchema(sql
6a810 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ite3*, int);.SQL
6a820 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6a830 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72   sqlite3BeginPar
6a840 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a  se(Parse*,int);.
6a850 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6a860 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69  oid sqlite3Commi
6a870 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73  tInternalChanges
6a880 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
6a890 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65  TE_PRIVATE Table
6a8a0 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53   *sqlite3ResultS
6a8b0 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65  etOfSelect(Parse
6a8c0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  *,Select*);.SQLI
6a8d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6a8e0 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65  sqlite3OpenMaste
6a8f0 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20  rTable(Parse *, 
6a900 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6a910 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6a920 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 73  3StartTable(Pars
6a930 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
6a940 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
6a950 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6a960 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
6a970 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54  dColumn(Parse*,T
6a980 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6a990 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6a9a0 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61  te3AddNotNull(Pa
6a9b0 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
6a9c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6a9d0 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72  sqlite3AddPrimar
6a9e0 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70  yKey(Parse*, Exp
6a9f0 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74  rList*, int, int
6aa00 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6aa10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6aa20 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74  te3AddCheckConst
6aa30 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78  raint(Parse*, Ex
6aa40 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
6aa50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6aa60 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50  3AddColumnType(P
6aa70 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53  arse*,Token*);.S
6aa80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6aa90 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  id sqlite3AddDef
6aaa0 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a  aultValue(Parse*
6aab0 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ,Expr*);.SQLITE_
6aac0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6aad0 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79  ite3AddCollateTy
6aae0 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  pe(Parse*, Token
6aaf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6ab00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6ab10 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  ndTable(Parse*,T
6ab20 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c  oken*,Token*,Sel
6ab30 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  ect*);..SQLITE_P
6ab40 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73  RIVATE Bitvec *s
6ab50 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
6ab60 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f  te(u32);.SQLITE_
6ab70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6ab80 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69  te3BitvecTest(Bi
6ab90 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c  tvec*, u32);.SQL
6aba0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6abb0 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
6abc0 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a  (Bitvec*, u32);.
6abd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6abe0 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65  oid sqlite3Bitve
6abf0 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20  cClear(Bitvec*, 
6ac00 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u32);.SQLITE_PRI
6ac10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6ac20 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 42  3BitvecDestroy(B
6ac30 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f  itvec*);.SQLITE_
6ac40 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69  PRIVATE u32 sqli
6ac50 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 42 69  te3BitvecSize(Bi
6ac60 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tvec*);.SQLITE_P
6ac70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6ac80 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54  e3BitvecBuiltinT
6ac90 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a  est(int,int*);..
6aca0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 52  SQLITE_PRIVATE R
6acb0 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 52 6f  owSet *sqlite3Ro
6acc0 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 65 33  wSetInit(sqlite3
6acd0 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67 6e  *, void*, unsign
6ace0 65 64 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  ed int);.SQLITE_
6acf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6ad00 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28  ite3RowSetClear(
6ad10 52 6f 77 53 65 74 2a 29 3b 0a 53 51 4c 49 54 45  RowSet*);.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 52 6f 77 53 65 74 49 6e 73 65 72  lite3RowSetInser
6ad40 74 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 29 3b  t(RowSet*, i64);
6ad50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ad60 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65  int sqlite3RowSe
6ad70 74 54 65 73 74 28 52 6f 77 53 65 74 2a 2c 20 75  tTest(RowSet*, u
6ad80 38 20 69 42 61 74 63 68 2c 20 69 36 34 29 3b 0a  8 iBatch, i64);.
6ad90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6ada0 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  nt sqlite3RowSet
6adb0 4e 65 78 74 28 52 6f 77 53 65 74 2a 2c 20 69 36  Next(RowSet*, i6
6adc0 34 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  4*);..SQLITE_PRI
6add0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6ade0 33 43 72 65 61 74 65 56 69 65 77 28 50 61 72 73  3CreateView(Pars
6adf0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
6ae00 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c  ,Token*,Select*,
6ae10 69 6e 74 2c 69 6e 74 29 3b 0a 0a 23 69 66 20 21  int,int);..#if !
6ae20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
6ae30 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
6ae40 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
6ae50 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a  T_VIRTUALTABLE).
6ae60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6ae70 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77   int sqlite3View
6ae80 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50  GetColumnNames(P
6ae90 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23  arse*,Table*);.#
6aea0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
6aeb0 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
6aec0 6d 6e 4e 61 6d 65 73 28 41 2c 42 29 20 30 0a 23  mnNames(A,B) 0.#
6aed0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
6aee0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6aef0 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73  e3DropTable(Pars
6af00 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
6af10 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6af20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6af30 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
6af40 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Table*);.SQLITE_
6af50 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6af60 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 65  ite3Insert(Parse
6af70 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
6af80 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c  rList*, Select*,
6af90 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a   IdList*, int);.
6afa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6afb0 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61  oid *sqlite3Arra
6afc0 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65  yAllocate(sqlite
6afd0 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  3*,void*,int,int
6afe0 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29  ,int*,int*,int*)
6aff0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b000 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
6b010 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  IdListAppend(sql
6b020 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20  ite3*, IdList*, 
6b030 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6b040 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6b050 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49  te3IdListIndex(I
6b060 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61  dList*,const cha
6b070 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6b080 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
6b090 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
6b0a0 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63  ge(sqlite3*, Src
6b0b0 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  List*, int, int)
6b0c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b0d0 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
6b0e0 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73  3SrcListAppend(s
6b0f0 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74  qlite3*, SrcList
6b100 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
6b110 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6b120 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69  TE SrcList *sqli
6b130 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
6b140 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c  FromTerm(Parse*,
6b150 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e   SrcList*, Token
6b160 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20  *, Token*,.     
6b170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b190 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a   Token*, Select*
6b1a0 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a  , Expr*, IdList*
6b1b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6b1c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
6b1d0 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50  cListIndexedBy(P
6b1e0 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20  arse *, SrcList 
6b1f0 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c  *, Token *);.SQL
6b200 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6b210 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79  sqlite3IndexedBy
6b220 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20  Lookup(Parse *, 
6b230 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
6b240 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tem *);.SQLITE_P
6b250 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6b260 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
6b270 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a  oinType(SrcList*
6b280 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6b290 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
6b2a0 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f  cListAssignCurso
6b2b0 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  rs(Parse*, SrcLi
6b2c0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
6b2d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6b2e0 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71  3IdListDelete(sq
6b2f0 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29  lite3*, IdList*)
6b300 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6b310 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63   void sqlite3Src
6b320 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
6b330 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a  e3*, SrcList*);.
6b340 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6b350 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
6b360 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f  eIndex(Parse*,To
6b370 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c  ken*,Token*,SrcL
6b380 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  ist*,ExprList*,i
6b390 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20  nt,Token*,.     
6b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b3b0 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20     Token*, int, 
6b3c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6b3d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6b3e0 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65  3DropIndex(Parse
6b3f0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74  *, SrcList*, int
6b400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6b410 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c  E int sqlite3Sel
6b420 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65  ect(Parse*, Sele
6b430 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a  ct*, SelectDest*
6b440 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6b450 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
6b460 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65  3SelectNew(Parse
6b470 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c  *,ExprList*,SrcL
6b480 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c  ist*,Expr*,ExprL
6b490 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20  ist*,.          
6b4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
6b4b0 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  xpr*,ExprList*,i
6b4c0 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b  nt,Expr*,Expr*);
6b4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6b4e0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65  void sqlite3Sele
6b4f0 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  ctDelete(sqlite3
6b500 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
6b510 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
6b520 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  e *sqlite3SrcLis
6b530 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20  tLookup(Parse*, 
6b540 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
6b550 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6b560 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28  lite3IsReadOnly(
6b570 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
6b580 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6b590 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6b5a0 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65  3OpenTable(Parse
6b5b0 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74  *, int iCur, int
6b5c0 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e   iDb, Table*, in
6b5d0 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  t);.#if defined(
6b5e0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50  SQLITE_ENABLE_UP
6b5f0 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49  DATE_DELETE_LIMI
6b600 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  T) && !defined(S
6b610 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
6b620 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  ERY).SQLITE_PRIV
6b630 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
6b640 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73  3LimitWhere(Pars
6b650 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20  e *, SrcList *, 
6b660 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74  Expr *, ExprList
6b670 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72   *, Expr *, Expr
6b680 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e   *, char *);.#en
6b690 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
6b6a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
6b6b0 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a  eleteFrom(Parse*
6b6c0 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72  , SrcList*, Expr
6b6d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6b6e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
6b6f0 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72  pdate(Parse*, Sr
6b700 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74  cList*, ExprList
6b710 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a  *, Expr*, int);.
6b720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57  SQLITE_PRIVATE W
6b730 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65  hereInfo *sqlite
6b740 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73  3WhereBegin(Pars
6b750 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
6b760 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c  pr*, ExprList**,
6b770 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 52   u16);.SQLITE_PR
6b780 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6b790 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65  e3WhereEnd(Where
6b7a0 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Info*);.SQLITE_P
6b7b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6b7c0 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c  e3ExprCodeGetCol
6b7d0 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  umn(Parse*, Tabl
6b7e0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  e*, int, int, in
6b7f0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6b800 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6b810 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
6b820 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
6b830 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6b840 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6b850 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79  ite3ExprCodeCopy
6b860 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
6b870 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6b880 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6b890 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f  ite3ExprCacheSto
6b8a0 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  re(Parse*, int, 
6b8b0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
6b8c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6b8d0 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
6b8e0 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ush(Parse*);.SQL
6b8f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6b900 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
6b910 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74  ePop(Parse*, int
6b920 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6b930 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
6b940 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61  prCacheRemove(Pa
6b950 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
6b960 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6b970 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
6b980 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53  Clear(Parse*);.S
6b990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6b9a0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
6b9b0 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
6b9c0 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69  e(Parse*, int, i
6b9d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6b9e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6b9f0 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72  ExprHardCopy(Par
6ba00 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  se*,int,int);.SQ
6ba10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ba20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
6ba30 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
6ba40 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6ba50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6ba60 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72  ExprCodeTemp(Par
6ba70 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a  se*, Expr*, int*
6ba80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ba90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
6baa0 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73  rCodeTarget(Pars
6bab0 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b  e*, Expr*, int);
6bac0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6bad0 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
6bae0 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73  odeAndCache(Pars
6baf0 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b  e*, Expr*, int);
6bb00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6bb10 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
6bb20 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61  CodeConstants(Pa
6bb30 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  rse*, Expr*);.SQ
6bb40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6bb50 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
6bb60 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a 2c  ExprList(Parse*,
6bb70 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c   ExprList*, int,
6bb80 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6bb90 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6bba0 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72  e3ExprIfTrue(Par
6bbb0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c  se*, Expr*, int,
6bbc0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6bbd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6bbe0 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 61  e3ExprIfFalse(Pa
6bbf0 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
6bc00 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6bc10 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
6bc20 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73  lite3FindTable(s
6bc30 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
6bc40 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  ar*, const char*
6bc50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6bc60 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
6bc70 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73  LocateTable(Pars
6bc80 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 6f  e*,int isView,co
6bc90 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74  nst char*, const
6bca0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
6bcb0 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73  PRIVATE Index *s
6bcc0 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
6bcd0 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
6bce0 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
6bcf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6bd00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
6bd10 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61  nlinkAndDeleteTa
6bd20 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ble(sqlite3*,int
6bd30 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53  ,const char*);.S
6bd40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6bd50 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  id sqlite3Unlink
6bd60 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73  AndDeleteIndex(s
6bd70 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73  qlite3*,int,cons
6bd80 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
6bd90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6bda0 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73  lite3Vacuum(Pars
6bdb0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
6bdc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
6bdd0 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c  unVacuum(char**,
6bde0 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49   sqlite3*);.SQLI
6bdf0 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
6be00 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d  *sqlite3NameFrom
6be10 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20  Token(sqlite3*, 
6be20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6be30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6be40 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45  te3ExprCompare(E
6be50 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  xpr*, Expr*);.SQ
6be60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6be70 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61  d sqlite3ExprAna
6be80 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e  lyzeAggregates(N
6be90 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70  ameContext*, Exp
6bea0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6beb0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6bec0 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69  ExprAnalyzeAggLi
6bed0 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c  st(NameContext*,
6bee0 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  ExprList*);.SQLI
6bef0 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20  TE_PRIVATE Vdbe 
6bf00 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28  *sqlite3GetVdbe(
6bf10 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
6bf20 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
6bf30 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70  lite3CreateIdExp
6bf40 72 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74  r(Parse *, const
6bf50 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
6bf60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6bf70 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74  ite3PrngSaveStat
6bf80 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  e(void);.SQLITE_
6bf90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6bfa0 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53  ite3PrngRestoreS
6bfb0 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49  tate(void);.SQLI
6bfc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6bfd0 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74  sqlite3PrngReset
6bfe0 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c  State(void);.SQL
6bff0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6c000 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
6c010 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  All(sqlite3*);.S
6c020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6c030 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65  id sqlite3CodeVe
6c040 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65  rifySchema(Parse
6c050 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
6c060 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6c070 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63  ite3BeginTransac
6c080 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74  tion(Parse*, int
6c090 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c0a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
6c0b0 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28  mmitTransaction(
6c0c0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
6c0d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6c0e0 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e  ite3RollbackTran
6c0f0 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b  saction(Parse*);
6c100 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6c110 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65  void sqlite3Save
6c120 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e  point(Parse*, in
6c130 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  t, Token*);.SQLI
6c140 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6c150 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
6c160 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a  points(sqlite3 *
6c170 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c180 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
6c190 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72  rIsConstant(Expr
6c1a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6c1b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
6c1c0 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a  prIsConstantNotJ
6c1d0 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49  oin(Expr*);.SQLI
6c1e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6c1f0 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
6c200 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45  tantOrFunction(E
6c210 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6c220 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6c230 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45  3ExprIsInteger(E
6c240 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c  xpr*, int*);.SQL
6c250 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6c260 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63  sqlite3IsRowid(c
6c270 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
6c280 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6c290 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
6c2a0 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65 2a  RowDelete(Parse*
6c2b0 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69  , Table*, int, i
6c2c0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
6c2d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6c2e0 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77  lite3GenerateRow
6c2f0 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 73  IndexDelete(Pars
6c300 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
6c310 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   int*);.SQLITE_P
6c320 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6c330 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b  e3GenerateIndexK
6c340 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 78  ey(Parse*, Index
6c350 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
6c360 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c370 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65  E void sqlite3Ge
6c380 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  nerateConstraint
6c390 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54 61  Checks(Parse*,Ta
6c3a0 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20 20  ble*,int,int,.  
6c3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c3d0 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 2c     int*,int,int,
6c3e0 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53  int,int,int*);.S
6c3f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6c400 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65  id sqlite3Comple
6c410 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 61 72 73  teInsertion(Pars
6c420 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
6c430 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e 74 2c   int, int*, int,
6c440 20 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53   int,int,int);.S
6c450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6c460 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62  t sqlite3OpenTab
6c470 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61 72  leAndIndices(Par
6c480 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  se*, Table*, int
6c490 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6c4a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6c4b0 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
6c4c0 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69  ration(Parse*, i
6c4d0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
6c4e0 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
6c4f0 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 71  qlite3ExprDup(sq
6c500 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74  lite3*,Expr*,int
6c510 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c520 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 6f  E void sqlite3To
6c530 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 2a  kenCopy(sqlite3*
6c540 2c 54 6f 6b 65 6e 2a 2c 63 6f 6e 73 74 20 54 6f  ,Token*,const To
6c550 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6c560 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a  IVATE ExprList *
6c570 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
6c580 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72  up(sqlite3*,Expr
6c590 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  List*,int);.SQLI
6c5a0 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69  TE_PRIVATE SrcLi
6c5b0 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69  st *sqlite3SrcLi
6c5c0 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53  stDup(sqlite3*,S
6c5d0 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51  rcList*,int);.SQ
6c5e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c  LITE_PRIVATE IdL
6c5f0 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69  ist *sqlite3IdLi
6c600 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 49  stDup(sqlite3*,I
6c610 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  dList*);.SQLITE_
6c620 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a  PRIVATE Select *
6c630 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
6c640 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74  (sqlite3*,Select
6c650 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
6c660 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6c670 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74  te3FuncDefInsert
6c680 28 46 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46  (FuncDefHash*, F
6c690 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45  uncDef*);.SQLITE
6c6a0 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65 66  _PRIVATE FuncDef
6c6b0 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e   *sqlite3FindFun
6c6c0 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63  ction(sqlite3*,c
6c6d0 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69  onst char*,int,i
6c6e0 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49  nt,u8,int);.SQLI
6c6f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6c700 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42  sqlite3RegisterB
6c710 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28  uiltinFunctions(
6c720 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
6c730 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6c740 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61  qlite3RegisterDa
6c750 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28  teTimeFunctions(
6c760 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
6c770 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6c780 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c  e3RegisterGlobal
6c790 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b  Functions(void);
6c7a0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
6c7b0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
6c7c0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
6c7d0 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65  3SafetyOn(sqlite
6c7e0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
6c7f0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
6c800 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74  3SafetyOff(sqlit
6c810 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  e3*);.#else.# de
6c820 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65  fine sqlite3Safe
6c830 74 79 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69  tyOn(A) 0.# defi
6c840 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ne sqlite3Safety
6c850 4f 66 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a  Off(A) 0.#endif.
6c860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6c870 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
6c880 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a  CheckOk(sqlite3*
6c890 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6c8a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
6c8b0 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b  etyCheckSickOrOk
6c8c0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
6c8d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6c8e0 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
6c8f0 6b 69 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  kie(Parse*, int)
6c900 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  ;..#if !defined(
6c910 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
6c920 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
6c930 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
6c940 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  R).SQLITE_PRIVAT
6c950 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61  E void sqlite3Ma
6c960 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61  terializeView(Pa
6c970 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78  rse*, Table*, Ex
6c980 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69  pr*, int);.#endi
6c990 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
6c9a0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53  E_OMIT_TRIGGER.S
6c9b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6c9c0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
6c9d0 6e 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c  nTrigger(Parse*,
6c9e0 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69   Token*,Token*,i
6c9f0 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53  nt,int,IdList*,S
6ca00 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20  rcList*,.       
6ca10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ca20 20 20 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69      Expr*,int, i
6ca30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6ca40 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
6ca50 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28  e3FinishTrigger(
6ca60 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53  Parse*, TriggerS
6ca70 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  tep*, Token*);.S
6ca80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6ca90 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
6caa0 54 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20  Trigger(Parse*, 
6cab0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a  SrcList*, int);.
6cac0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
6cad0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
6cae0 70 54 72 69 67 67 65 72 50 74 72 28 50 61 72 73  pTriggerPtr(Pars
6caf0 65 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53  e*, Trigger*);.S
6cb00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6cb10 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33  Trigger *sqlite3
6cb20 54 72 69 67 67 65 72 73 45 78 69 73 74 28 50 61  TriggersExist(Pa
6cb30 72 73 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  rse *, Table*, i
6cb40 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69  nt, ExprList*, i
6cb50 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c 49  nt *pMask);.SQLI
6cb60 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
6cb70 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69  gger *sqlite3Tri
6cb80 67 67 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a  ggerList(Parse *
6cb90 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49  , Table *);.SQLI
6cba0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
6cbb0 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
6cbc0 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54  rigger(Parse*, T
6cbd0 72 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45  rigger *, int, E
6cbe0 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54  xprList*, int, T
6cbf0 61 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 20  able *,.        
6cc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cc10 20 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e      int, int, in
6cc20 74 2c 20 69 6e 74 2c 20 75 33 32 2a 2c 20 75 33  t, int, u32*, u3
6cc30 32 2a 29 3b 0a 20 20 76 6f 69 64 20 73 71 6c 69  2*);.  void sqli
6cc40 74 65 56 69 65 77 54 72 69 67 67 65 72 73 28 50  teViewTriggers(P
6cc50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45  arse*, Table*, E
6cc60 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c  xpr*, int, ExprL
6cc70 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
6cc80 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
6cc90 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65  ite3DeleteTrigge
6cca0 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 20  rStep(sqlite3*, 
6ccb0 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53  TriggerStep*);.S
6ccc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6ccd0 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c  TriggerStep *sql
6cce0 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63  ite3TriggerSelec
6ccf0 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 53  tStep(sqlite3*,S
6cd00 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
6cd10 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
6cd20 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
6cd30 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28  iggerInsertStep(
6cd40 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c  sqlite3*,Token*,
6cd50 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20   IdList*,.      
6cd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cd80 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65    ExprList*,Sele
6cd90 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ct*,int);.SQLITE
6cda0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
6cdb0 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
6cdc0 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70  riggerUpdateStep
6cdd0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a  (sqlite3*,Token*
6cde0 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 72  ,ExprList*, Expr
6cdf0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
6ce00 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
6ce10 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
6ce20 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28  iggerDeleteStep(
6ce30 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c  sqlite3*,Token*,
6ce40 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
6ce50 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
6ce60 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
6ce70 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72  ger(sqlite3*, Tr
6ce80 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  igger*);.SQLITE_
6ce90 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
6cea0 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
6ceb0 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c  eleteTrigger(sql
6cec0 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  ite3*,int,const 
6ced0 63 68 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23 20  char*);.#else.# 
6cee0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72  define sqlite3Tr
6cef0 69 67 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c  iggersExist(B,C,
6cf00 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e  D,E,F) 0.# defin
6cf10 65 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  e sqlite3DeleteT
6cf20 72 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65  rigger(A,B).# de
6cf30 66 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70  fine sqlite3Drop
6cf40 54 72 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a  TriggerPtr(A,B).
6cf50 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6cf60 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
6cf70 72 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20  rigger(A,B,C).# 
6cf80 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f  define sqlite3Co
6cf90 64 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42  deRowTrigger(A,B
6cfa0 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c 4a  ,C,D,E,F,G,H,I,J
6cfb0 2c 4b 2c 4c 29 20 30 0a 23 20 64 65 66 69 6e 65  ,K,L) 0.# define
6cfc0 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c   sqlite3TriggerL
6cfd0 69 73 74 28 58 2c 20 59 29 20 30 0a 23 65 6e 64  ist(X, Y) 0.#end
6cfe0 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  if..SQLITE_PRIVA
6cff0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f  TE int sqlite3Jo
6d000 69 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54  inType(Parse*, T
6d010 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  oken*, Token*, T
6d020 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
6d030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6d040 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
6d050 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72  Key(Parse*, Expr
6d060 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45  List*, Token*, E
6d070 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a  xprList*, int);.
6d080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6d090 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72  oid sqlite3Defer
6d0a0 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65  ForeignKey(Parse
6d0b0 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66  *, int);.#ifndef
6d0c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
6d0d0 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54  HORIZATION.SQLIT
6d0e0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
6d0f0 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64   sqlite3AuthRead
6d100 28 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63  (Parse*,Expr*,Sc
6d110 68 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b  hema*,SrcList*);
6d120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d130 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74    int sqlite3Aut
6d140 68 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e  hCheck(Parse*,in
6d150 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  t, const char*, 
6d160 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e  const char*, con
6d170 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
6d180 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
6d190 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
6d1a0 65 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20  extPush(Parse*, 
6d1b0 41 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f  AuthContext*, co
6d1c0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
6d1d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
6d1e0 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  d sqlite3AuthCon
6d1f0 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74  textPop(AuthCont
6d200 65 78 74 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64  ext*);.#else.# d
6d210 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74  efine sqlite3Aut
6d220 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23  hRead(a,b,c,d).#
6d230 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41   define sqlite3A
6d240 75 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64  uthCheck(a,b,c,d
6d250 2c 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b  ,e)    SQLITE_OK
6d260 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6d270 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68  3AuthContextPush
6d280 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e 65  (a,b,c).# define
6d290 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
6d2a0 65 78 74 50 6f 70 28 61 29 20 20 28 28 76 6f 69  extPop(a)  ((voi
6d2b0 64 29 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 51  d)(a)).#endif.SQ
6d2c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6d2d0 64 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28  d sqlite3Attach(
6d2e0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45  Parse*, Expr*, E
6d2f0 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  xpr*, Expr*);.SQ
6d300 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6d310 64 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28  d sqlite3Detach(
6d320 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a  Parse*, Expr*);.
6d330 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6d340 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
6d350 61 63 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c  actory(const sql
6d360 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
6d370 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
6d380 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
6d390 20 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74          int omit
6d3a0 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61  Journal, int nCa
6d3b0 63 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20  che, int flags, 
6d3c0 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29  Btree **ppBtree)
6d3d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6d3e0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49   int sqlite3FixI
6d3f0 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61  nit(DbFixer*, Pa
6d400 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  rse*, int, const
6d410 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f   char*, const To
6d420 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6d430 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6d440 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69  3FixSrcList(DbFi
6d450 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b  xer*, SrcList*);
6d460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d470 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65  int sqlite3FixSe
6d480 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53  lect(DbFixer*, S
6d490 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
6d4a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6d4b0 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78  te3FixExpr(DbFix
6d4c0 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  er*, Expr*);.SQL
6d4d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6d4e0 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69  sqlite3FixExprLi
6d4f0 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70  st(DbFixer*, Exp
6d500 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  rList*);.SQLITE_
6d510 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6d520 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
6d530 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67  p(DbFixer*, Trig
6d540 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54  gerStep*);.SQLIT
6d550 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d560 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20  lite3AtoF(const 
6d570 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a  char *z, double*
6d580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6d590 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
6d5a0 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72  Int32(const char
6d5b0 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54   *, int*);.SQLIT
6d5c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6d5d0 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74  lite3FitsIn64Bit
6d5e0 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  s(const char *, 
6d5f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6d600 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6d610 55 74 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e  Utf16ByteLen(con
6d620 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20  st void *pData, 
6d630 69 6e 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49  int nChar);.SQLI
6d640 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6d650 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
6d660 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  n(const char *pD
6d670 61 74 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b  ata, int nByte);
6d680 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6d690 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 52  int sqlite3Utf8R
6d6a0 65 61 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63  ead(const u8*, c
6d6b0 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a  onst u8**);../*.
6d6c0 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72  ** Routines to r
6d6d0 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 61  ead and write va
6d6e0 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
6d6f0 74 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 75  tegers.  These u
6d700 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66  sed to.** be def
6d710 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75  ined locally, bu
6d720 74 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65  t now we use the
6d730 20 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73   varint routines
6d740 20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a   in the util.c.*
6d750 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68  * file.  Code sh
6d760 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43  ould use the MAC
6d770 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20  RO forms below, 
6d780 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20  as the Varint32 
6d790 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  versions.** are 
6d7a0 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20  coded to assume 
6d7b0 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20  the single byte 
6d7c0 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20  case is already 
6d7d0 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a  handled (which .
6d7e0 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72  ** the MACRO for
6d7f0 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49  m does)..*/.SQLI
6d800 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6d810 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28  qlite3PutVarint(
6d820 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20  unsigned char*, 
6d830 75 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u64);.SQLITE_PRI
6d840 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6d850 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69  PutVarint32(unsi
6d860 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29  gned char*, u32)
6d870 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6d880 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61   u8 sqlite3GetVa
6d890 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67  rint(const unsig
6d8a0 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20  ned char *, u64 
6d8b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6d8c0 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74  TE u8 sqlite3Get
6d8d0 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75  Varint32(const u
6d8e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20  nsigned char *, 
6d8f0 75 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  u32 *);.SQLITE_P
6d900 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6d910 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20  e3VarintLen(u64 
6d920 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  v);../*.** The h
6d930 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72  eader of a recor
6d940 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20  d consists of a 
6d950 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c  sequence variabl
6d960 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
6d970 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65  s..** These inte
6d980 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20  gers are almost 
6d990 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64  always small and
6d9a0 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20   are encoded as 
6d9b0 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a  a single byte..*
6d9c0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
6d9d0 6d 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61  macros take adva
6d9e0 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20  ntage this fact 
6d9f0 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73  to provide a fas
6da00 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20  t encode.** and 
6da10 64 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e  decode of the in
6da20 74 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f  tegers in a reco
6da30 72 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 69  rd header.  It i
6da40 73 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 65  s faster for the
6da50 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20   common.** case 
6da60 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65  where the intege
6da70 72 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79  r is a single by
6da80 74 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74  te.  It is a lit
6da90 74 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20  tle slower when 
6daa0 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69  the.** integer i
6dab0 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79  s two or more by
6dac0 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c  tes.  But overal
6dad0 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a  l it is faster..
6dae0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
6daf0 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ing expressions 
6db00 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a  are equivalent:.
6db10 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71  **.**     x = sq
6db20 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
6db30 28 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20  ( A, &B );.**   
6db40 20 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74    x = sqlite3Put
6db50 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29  Varint32( A, B )
6db60 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20  ;.**.**     x = 
6db70 67 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20  getVarint32( A, 
6db80 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20  B );.**     x = 
6db90 70 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20  putVarint32( A, 
6dba0 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69  B );.**.*/.#defi
6dbb0 6e 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 41  ne getVarint32(A
6dbc0 2c 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c  ,B)  (u8)((*(A)<
6dbd0 28 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 29  (u8)0x80) ? ((B)
6dbe0 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20   = (u32)*(A)),1 
6dbf0 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  : sqlite3GetVari
6dc00 6e 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a  nt32((A), (u32 *
6dc10 29 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20  )&(B))).#define 
6dc20 70 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29  putVarint32(A,B)
6dc30 20 20 28 75 38 29 28 28 28 75 33 32 29 28 42 29    (u8)(((u32)(B)
6dc40 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a  <(u32)0x80) ? (*
6dc50 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  (A) = (unsigned 
6dc60 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71  char)(B)),1 : sq
6dc70 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32  lite3PutVarint32
6dc80 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66  ((A), (B))).#def
6dc90 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 20  ine getVarint   
6dca0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
6dcb0 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72  t.#define putVar
6dcc0 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 75  int    sqlite3Pu
6dcd0 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45  tVarint...SQLITE
6dce0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6dcf0 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
6dd00 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e  tyStr(Vdbe *, In
6dd10 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  dex *);.SQLITE_P
6dd20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6dd30 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79  te3TableAffinity
6dd40 53 74 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c  Str(Vdbe *, Tabl
6dd50 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
6dd60 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65  VATE char sqlite
6dd70 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
6dd80 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
6dd90 61 72 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45  ar aff2);.SQLITE
6dda0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6ddb0 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
6ddc0 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c  yOk(Expr *pExpr,
6ddd0 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69   char idx_affini
6dde0 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ty);.SQLITE_PRIV
6ddf0 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
6de00 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70  ExprAffinity(Exp
6de10 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54  r *pExpr);.SQLIT
6de20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6de30 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73  lite3Atoi64(cons
6de40 74 20 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a  t char*, i64*);.
6de50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6de60 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
6de70 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20  (sqlite3*, int, 
6de80 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29  const char*,...)
6de90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6dea0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65   void *sqlite3He
6deb0 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a  xToBlob(sqlite3*
6dec0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  , const char *z,
6ded0 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f   int n);.SQLITE_
6dee0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6def0 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 50  te3TwoPartName(P
6df00 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c  arse *, Token *,
6df10 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20   Token *, Token 
6df20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
6df30 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
6df40 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e  sqlite3ErrStr(in
6df50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6df60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65  TE int sqlite3Re
6df70 61 64 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a  adSchema(Parse *
6df80 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f  pParse);.SQLITE_
6df90 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
6dfa0 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c  *sqlite3FindColl
6dfb0 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 75 38 20  Seq(sqlite3*,u8 
6dfc0 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  enc, const char 
6dfd0 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  *,int,int);.SQLI
6dfe0 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
6dff0 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74  eq *sqlite3Locat
6e000 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  eCollSeq(Parse *
6e010 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
6e020 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e  ar *zName, int n
6e030 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Name);.SQLITE_PR
6e040 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
6e050 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
6e060 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
6e070 20 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53   Expr *pExpr);.S
6e080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
6e090 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53  pr *sqlite3ExprS
6e0a0 65 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50  etColl(Parse *pP
6e0b0 61 72 73 65 2c 20 45 78 70 72 20 2a 2c 20 54 6f  arse, Expr *, To
6e0c0 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ken *);.SQLITE_P
6e0d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6e0e0 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50  e3CheckCollSeq(P
6e0f0 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 20  arse *, CollSeq 
6e100 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e110 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68  TE int sqlite3Ch
6e120 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61  eckObjectName(Pa
6e130 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  rse *, const cha
6e140 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r *);.SQLITE_PRI
6e150 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6e160 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
6e170 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74 29 3b  sqlite3 *, int);
6e180 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6e190 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
6e1a0 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 73 71  ite3ValueText(sq
6e1b0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38  lite3_value*, u8
6e1c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e1d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c  E int sqlite3Val
6e1e0 75 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f  ueBytes(sqlite3_
6e1f0 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c  value*, u8);.SQL
6e200 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6e210 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
6e220 53 74 72 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  Str(sqlite3_valu
6e230 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76  e*, int, const v
6e240 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20 20 20  oid *,u8, .     
6e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e260 20 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a     void(*)(void*
6e270 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ));.SQLITE_PRIVA
6e280 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
6e290 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33  alueFree(sqlite3
6e2a0 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
6e2b0 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
6e2c0 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56  _value *sqlite3V
6e2d0 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20  alueNew(sqlite3 
6e2e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e2f0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
6e300 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33  Utf16to8(sqlite3
6e310 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c   *, const void*,
6e320 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6e330 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6e340 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73  3ValueFromExpr(s
6e350 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20 2a  qlite3 *, Expr *
6e360 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 65  , u8, u8, sqlite
6e370 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c  3_value **);.SQL
6e380 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6e390 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70   sqlite3ValueApp
6e3a0 6c 79 41 66 66 69 6e 69 74 79 28 73 71 6c 69 74  lyAffinity(sqlit
6e3b0 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c 20  e3_value *, u8, 
6e3c0 75 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  u8);.#ifndef SQL
6e3d0 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
6e3e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e3f0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
6e400 68 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72  har sqlite3Upper
6e410 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54  ToLower[];.SQLIT
6e420 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
6e430 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71  unsigned char sq
6e440 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 5d 3b  lite3CtypeMap[];
6e450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e460 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63  SQLITE_WSD struc
6e470 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20  t Sqlite3Config 
6e480 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 3b 0a 53  sqlite3Config;.S
6e490 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51  QLITE_PRIVATE SQ
6e4a0 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66  LITE_WSD FuncDef
6e4b0 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62  Hash sqlite3Glob
6e4c0 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 53 51 4c  alFunctions;.SQL
6e4d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6e4e0 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79  sqlite3PendingBy
6e4f0 74 65 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  te;.#endif.SQLIT
6e500 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6e510 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f  qlite3RootPageMo
6e520 76 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20 69 6e  ved(Db*, int, in
6e530 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6e540 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
6e550 65 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 54  eindex(Parse*, T
6e560 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  oken*, Token*);.
6e570 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6e580 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
6e590 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
6e5a0 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  3*);.SQLITE_PRIV
6e5b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6e5c0 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65  AlterRenameTable
6e5d0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
6e5e0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
6e5f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6e600 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63  qlite3GetToken(c
6e610 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
6e620 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51  ar *, int *);.SQ
6e630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6e640 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  d sqlite3NestedP
6e650 61 72 73 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e  arse(Parse*, con
6e660 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a  st char*, ...);.
6e670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6e680 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72  oid sqlite3Expir
6e690 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65  ePreparedStateme
6e6a0 6e 74 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  nts(sqlite3*);.S
6e6b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6e6c0 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75  id sqlite3CodeSu
6e6d0 62 73 65 6c 65 63 74 28 50 61 72 73 65 20 2a 2c  bselect(Parse *,
6e6e0 20 45 78 70 72 20 2a 2c 20 69 6e 74 2c 20 69 6e   Expr *, int, in
6e6f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6e700 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
6e710 65 6c 65 63 74 50 72 65 70 28 50 61 72 73 65 2a  electPrep(Parse*
6e720 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43  , Select*, NameC
6e730 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45  ontext*);.SQLITE
6e740 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6e750 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
6e760 61 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74  ames(NameContext
6e770 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
6e780 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6e790 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c  qlite3ResolveSel
6e7a0 65 63 74 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c  ectNames(Parse*,
6e7b0 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f   Select*, NameCo
6e7c0 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  ntext*);.SQLITE_
6e7d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6e7e0 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47  te3ResolveOrderG
6e7f0 72 6f 75 70 42 79 28 50 61 72 73 65 2a 2c 20 53  roupBy(Parse*, S
6e800 65 6c 65 63 74 2a 2c 20 45 78 70 72 4c 69 73 74  elect*, ExprList
6e810 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  *, const char*);
6e820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e830 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75  void sqlite3Colu
6e840 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 2a  mnDefault(Vdbe *
6e850 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 29 3b  , Table *, int);
6e860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e870 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
6e880 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e  rFinishAddColumn
6e890 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20  (Parse *, Token 
6e8a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6e8b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
6e8c0 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75  lterBeginAddColu
6e8d0 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c  mn(Parse *, SrcL
6e8e0 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ist *);.SQLITE_P
6e8f0 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
6e900 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65  sqlite3GetCollSe
6e910 71 28 73 71 6c 69 74 65 33 2a 2c 20 43 6f 6c 6c  q(sqlite3*, Coll
6e920 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  Seq *, const cha
6e930 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  r *, int);.SQLIT
6e940 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73  E_PRIVATE char s
6e950 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79  qlite3AffinityTy
6e960 70 65 28 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29  pe(const Token*)
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 41 6e 61   void sqlite3Ana
6e990 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b  lyze(Parse*, Tok
6e9a0 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  en*, Token*);.SQ
6e9b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6e9c0 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75   sqlite3InvokeBu
6e9d0 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61  syHandler(BusyHa
6e9e0 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ndler*);.SQLITE_
6e9f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6ea00 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65  te3FindDb(sqlite
6ea10 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  3*, Token*);.SQL
6ea20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6ea30 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d  sqlite3FindDbNam
6ea40 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e  e(sqlite3 *, con
6ea50 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49  st char *);.SQLI
6ea60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6ea70 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f  qlite3AnalysisLo
6ea80 61 64 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  ad(sqlite3*,int 
6ea90 69 44 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  iDB);.SQLITE_PRI
6eaa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6eab0 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49  3DefaultRowEst(I
6eac0 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ndex*);.SQLITE_P
6ead0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6eae0 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46  te3RegisterLikeF
6eaf0 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33  unctions(sqlite3
6eb00 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
6eb10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6eb20 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f  te3IsLikeFunctio
6eb30 6e 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  n(sqlite3*,Expr*
6eb40 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 3b 0a 53 51  ,int*,char*);.SQ
6eb50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6eb60 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d  d sqlite3Minimum
6eb70 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 72 73 65  FileFormat(Parse
6eb80 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
6eb90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6eba0 64 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46  d sqlite3SchemaF
6ebb0 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a 53 51 4c  ree(void *);.SQL
6ebc0 49 54 45 5f 50 52 49 56 41 54 45 20 53 63 68 65  ITE_PRIVATE Sche
6ebd0 6d 61 20 2a 73 71 6c 69 74 65 33 53 63 68 65 6d  ma *sqlite3Schem
6ebe0 61 47 65 74 28 73 71 6c 69 74 65 33 20 2a 2c 20  aGet(sqlite3 *, 
6ebf0 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Btree *);.SQLITE
6ec00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6ec10 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
6ec20 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53  x(sqlite3 *db, S
6ec30 63 68 65 6d 61 20 2a 29 3b 0a 53 51 4c 49 54 45  chema *);.SQLITE
6ec40 5f 50 52 49 56 41 54 45 20 4b 65 79 49 6e 66 6f  _PRIVATE KeyInfo
6ec50 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65   *sqlite3IndexKe
6ec60 79 69 6e 66 6f 28 50 61 72 73 65 20 2a 2c 20 49  yinfo(Parse *, I
6ec70 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  ndex *);.SQLITE_
6ec80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6ec90 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 73 71  te3CreateFunc(sq
6eca0 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63  lite3 *, const c
6ecb0 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  har *, int, int,
6ecc0 20 76 6f 69 64 20 2a 2c 20 0a 20 20 76 6f 69 64   void *, .  void
6ecd0 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e   (*)(sqlite3_con
6ece0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
6ecf0 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76  3_value **),.  v
6ed00 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f  oid (*)(sqlite3_
6ed10 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
6ed20 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 20  ite3_value **), 
6ed30 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33  void (*)(sqlite3
6ed40 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 53 51 4c  _context*));.SQL
6ed50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6ed60 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 73  sqlite3ApiExit(s
6ed70 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 29  qlite3 *db, int)
6ed80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ed90 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e   int sqlite3Open
6eda0 54 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72  TempDatabase(Par
6edb0 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  se *);..SQLITE_P
6edc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6edd0 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
6ede0 53 74 72 41 63 63 75 6d 2a 2c 20 63 68 61 72 2a  StrAccum*, char*
6edf0 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
6ee00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6ee10 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
6ee20 41 70 70 65 6e 64 28 53 74 72 41 63 63 75 6d 2a  Append(StrAccum*
6ee30 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
6ee40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ee50 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 53  E char *sqlite3S
6ee60 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 53 74  trAccumFinish(St
6ee70 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45  rAccum*);.SQLITE
6ee80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6ee90 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73  lite3StrAccumRes
6eea0 65 74 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53  et(StrAccum*);.S
6eeb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6eec0 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  id sqlite3Select
6eed0 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44  DestInit(SelectD
6eee0 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a  est*,int,int);..
6eef0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6ef00 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75  oid sqlite3Backu
6ef10 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33  pRestart(sqlite3
6ef20 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53 51 4c 49  _backup *);.SQLI
6ef30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6ef40 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64  sqlite3BackupUpd
6ef50 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ate(sqlite3_back
6ef60 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63 6f 6e 73  up *, Pgno, cons
6ef70 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t u8 *);../*.** 
6ef80 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  The interface to
6ef90 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72   the LEMON-gener
6efa0 61 74 65 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53  ated parser.*/.S
6efb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6efc0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65  id *sqlite3Parse
6efd0 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28  rAlloc(void*(*)(
6efe0 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45  size_t));.SQLITE
6eff0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6f000 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28  lite3ParserFree(
6f010 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76  void*, void(*)(v
6f020 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  oid*));.SQLITE_P
6f030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f040 74 65 33 50 61 72 73 65 72 28 76 6f 69 64 2a 2c  te3Parser(void*,
6f050 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72   int, Token, Par
6f060 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 59 59 54  se*);.#ifdef YYT
6f070 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54  RACKMAXSTACKDEPT
6f080 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  H.SQLITE_PRIVATE
6f090 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61     int sqlite3Pa
6f0a0 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f  rserStackPeak(vo
6f0b0 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51  id*);.#endif..SQ
6f0c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6f0d0 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64   sqlite3AutoLoad
6f0e0 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74  Extensions(sqlit
6f0f0 65 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  e3*);.#ifndef SQ
6f100 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
6f110 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f  XTENSION.SQLITE_
6f120 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
6f130 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e  qlite3CloseExten
6f140 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b  sions(sqlite3*);
6f150 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
6f160 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65  sqlite3CloseExte
6f170 6e 73 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69 66  nsions(X).#endif
6f180 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
6f190 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
6f1a0 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  HE.SQLITE_PRIVAT
6f1b0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
6f1c0 54 61 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65 20  TableLock(Parse 
6f1d0 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38 2c  *, int, int, u8,
6f1e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
6f1f0 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
6f200 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
6f210 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64  (v,w,x,y,z).#end
6f220 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
6f230 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
6f240 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
6f250 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67  te3Utf8To8(unsig
6f260 6e 65 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64  ned char*);.#end
6f270 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
6f280 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
6f290 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 73 71  BLE.#  define sq
6f2a0 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 58  lite3VtabClear(X
6f2b0 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69  ).#  define sqli
6f2c0 74 65 33 56 74 61 62 53 79 6e 63 28 58 2c 59 29  te3VtabSync(X,Y)
6f2d0 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 20 64 65   SQLITE_OK.#  de
6f2e0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
6f2f0 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20 64  Rollback(X).#  d
6f300 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61  efine sqlite3Vta
6f310 62 43 6f 6d 6d 69 74 28 58 29 0a 23 20 20 64 65  bCommit(X).#  de
6f320 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
6f330 49 6e 53 79 6e 63 28 64 62 29 20 30 0a 23 65 6c  InSync(db) 0.#el
6f340 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  se.SQLITE_PRIVAT
6f350 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  E    void sqlite
6f360 33 56 74 61 62 43 6c 65 61 72 28 54 61 62 6c 65  3VtabClear(Table
6f370 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f380 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65  TE    int sqlite
6f390 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74 65  3VtabSync(sqlite
6f3a0 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 29 3b  3 *db, char **);
6f3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f3c0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74     int sqlite3Vt
6f3d0 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74  abRollback(sqlit
6f3e0 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f  e3 *db);.SQLITE_
6f3f0 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73  PRIVATE    int s
6f400 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
6f410 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23  (sqlite3 *db);.#
6f420 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
6f430 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 28  VtabInSync(db) (
6f440 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e 30 20  (db)->nVTrans>0 
6f450 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61 6e 73  && (db)->aVTrans
6f460 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49  ==0).#endif.SQLI
6f470 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f480 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57  sqlite3VtabMakeW
6f490 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  ritable(Parse*,T
6f4a0 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
6f4b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f4c0 74 65 33 56 74 61 62 4c 6f 63 6b 28 73 71 6c 69  te3VtabLock(sqli
6f4d0 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53 51 4c 49  te3_vtab*);.SQLI
6f4e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f4f0 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
6f500 6b 28 73 71 6c 69 74 65 33 2a 2c 20 73 71 6c 69  k(sqlite3*, sqli
6f510 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53 51 4c 49  te3_vtab*);.SQLI
6f520 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f530 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e  sqlite3VtabBegin
6f540 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f  Parse(Parse*, To
6f550 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ken*, Token*, To
6f560 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6f570 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f580 65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73  e3VtabFinishPars
6f590 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a  e(Parse*, Token*
6f5a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f5b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
6f5c0 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a  abArgInit(Parse*
6f5d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f5e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
6f5f0 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72 73  abArgExtend(Pars
6f600 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  e*, Token*);.SQL
6f610 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6f620 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
6f630 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20  reate(sqlite3*, 
6f640 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  int, const char 
6f650 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c  *, char **);.SQL
6f660 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6f670 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
6f680 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54  onnect(Parse*, T
6f690 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
6f6a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6f6b0 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f  e3VtabCallDestro
6f6c0 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  y(sqlite3*, int,
6f6d0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
6f6e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6f6f0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  nt sqlite3VtabBe
6f700 67 69 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 73  gin(sqlite3 *, s
6f710 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a  qlite3_vtab *);.
6f720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46  SQLITE_PRIVATE F
6f730 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56  uncDef *sqlite3V
6f740 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74  tabOverloadFunct
6f750 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 2c 46 75  ion(sqlite3 *,Fu
6f760 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67  ncDef*, int nArg
6f770 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6f780 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6f790 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63  lite3InvalidFunc
6f7a0 74 69 6f 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tion(sqlite3_con
6f7b0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
6f7c0 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49  3_value**);.SQLI
6f7d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6f7e0 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69  qlite3TransferBi
6f7f0 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73  ndings(sqlite3_s
6f800 74 6d 74 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73  tmt *, sqlite3_s
6f810 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tmt *);.SQLITE_P
6f820 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6f830 65 33 52 65 70 72 65 70 61 72 65 28 56 64 62 65  e3Reprepare(Vdbe
6f840 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f850 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6f860 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67  xprListCheckLeng
6f870 74 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c  th(Parse*, ExprL
6f880 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  ist*, const char
6f890 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f8a0 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
6f8b0 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65  te3BinaryCompare
6f8c0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c  CollSeq(Parse *,
6f8d0 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20 2a 29   Expr *, Expr *)
6f8e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f8f0 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65 6d 70   int sqlite3Temp
6f900 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73  InMemory(const s
6f910 71 6c 69 74 65 33 2a 29 3b 0a 0a 0a 0a 2f 2a 0a  qlite3*);..../*.
6f920 2a 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 75  ** Available fau
6f930 6c 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 53  lt injectors.  S
6f940 68 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 65  hould be numbere
6f950 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  d beginning with
6f960 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53   0..*/.#define S
6f970 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43  QLITE_FAULTINJEC
6f980 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 30  TOR_MALLOC     0
6f990 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
6f9a0 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 4f  FAULTINJECTOR_CO
6f9b0 55 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a  UNT      1../*.*
6f9c0 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
6f9d0 74 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 66  to the code in f
6f9e0 61 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 20  ault.c used for 
6f9f0 69 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 6e  identifying "ben
6fa00 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66  ign".** malloc f
6fa10 61 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 73  ailures. This is
6fa20 20 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 66   only present if
6fa30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
6fa40 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 20  LTIN_TEST.** is 
6fa50 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  not defined..*/.
6fa60 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
6fa70 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
6fa80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fa90 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65    void sqlite3Be
6faa0 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
6fab0 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
6fac0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
6fad0 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
6fae0 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65  loc(void);.#else
6faf0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
6fb00 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
6fb10 6c 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 20  loc().  #define 
6fb20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
6fb30 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 0a  Malloc().#endif.
6fb40 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45  .#define IN_INDE
6fb50 58 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 20  X_ROWID         
6fb60 20 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49    1.#define IN_I
6fb70 4e 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 20  NDEX_EPH        
6fb80 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 49       2.#define I
6fb90 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 20  N_INDEX_INDEX   
6fba0 20 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 45          3.SQLITE
6fbb0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6fbc0 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28  ite3FindInIndex(
6fbd0 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c  Parse *, Expr *,
6fbe0 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 20   int*);..#ifdef 
6fbf0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
6fc00 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 54  OMIC_WRITE.SQLIT
6fc10 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
6fc20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70  sqlite3JournalOp
6fc30 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  en(sqlite3_vfs *
6fc40 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
6fc50 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c 20  sqlite3_file *, 
6fc60 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
6fc70 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
6fc80 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69  sqlite3JournalSi
6fc90 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ze(sqlite3_vfs *
6fca0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fcb0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  E   int sqlite3J
6fcc0 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 6c  ournalCreate(sql
6fcd0 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 65  ite3_file *);.#e
6fce0 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
6fcf0 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65  lite3JournalSize
6fd00 28 70 56 66 73 29 20 28 28 70 56 66 73 29 2d 3e  (pVfs) ((pVfs)->
6fd10 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 66  szOsFile).#endif
6fd20 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
6fd30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
6fd40 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69  JournalOpen(sqli
6fd50 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 4c  te3_file *);.SQL
6fd60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6fd70 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
6fd80 6c 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 4c  lSize(void);.SQL
6fd90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6fda0 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72  sqlite3IsMemJour
6fdb0 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  nal(sqlite3_file
6fdc0 20 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45   *);..#if SQLITE
6fdd0 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e  _MAX_EXPR_DEPTH>
6fde0 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  0.SQLITE_PRIVATE
6fdf0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45     void sqlite3E
6fe00 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72  xprSetHeight(Par
6fe10 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
6fe20 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   *p);.SQLITE_PRI
6fe30 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6fe40 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67  e3SelectExprHeig
6fe50 68 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 51  ht(Select *);.SQ
6fe60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
6fe70 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68  nt sqlite3ExprCh
6fe80 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 2a  eckHeight(Parse*
6fe90 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 20  , int);.#else.  
6fea0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45  #define sqlite3E
6feb0 78 70 72 53 65 74 48 65 69 67 68 74 28 78 2c 79  xprSetHeight(x,y
6fec0 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ).  #define sqli
6fed0 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69  te3SelectExprHei
6fee0 67 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 69  ght(x) 0.  #defi
6fef0 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 68  ne sqlite3ExprCh
6ff00 65 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a 23  eckHeight(x,y).#
6ff10 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
6ff20 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
6ff30 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20  3Get4byte(const 
6ff40 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u8*);.SQLITE_PRI
6ff50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6ff60 33 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 75  3Put4byte(u8*, u
6ff70 33 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  32);..#ifdef SQL
6ff80 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43  ITE_ENABLE_UNLOC
6ff90 4b 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 54 45 5f  K_NOTIFY.SQLITE_
6ffa0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
6ffb0 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
6ffc0 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20  Blocked(sqlite3 
6ffd0 2a 2c 20 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53  *, sqlite3 *);.S
6ffe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6fff0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e  void sqlite3Conn
70000 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73  ectionUnlocked(s
70010 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c  qlite3 *db);.SQL
70020 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
70030 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63  id sqlite3Connec
70040 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74  tionClosed(sqlit
70050 65 33 20 2a 64 62 29 3b 0a 23 65 6c 73 65 0a 20  e3 *db);.#else. 
70060 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
70070 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65  ConnectionBlocke
70080 64 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65  d(x,y).  #define
70090 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
700a0 6f 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 0a 20 20  onUnlocked(x).  
700b0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43  #define sqlite3C
700c0 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28  onnectionClosed(
700d0 78 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64  x).#endif...#ifd
700e0 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 23 69  ef SQLITE_SSE.#i
700f0 6e 63 6c 75 64 65 20 22 73 73 65 49 6e 74 2e 68  nclude "sseInt.h
70100 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ".#endif..#ifdef
70110 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51   SQLITE_DEBUG.SQ
70120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
70130 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
70140 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 63 68  rTrace(FILE*, ch
70150 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f  ar *);.#endif../
70160 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49  *.** If the SQLI
70170 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 41 43  TE_ENABLE IOTRAC
70180 45 20 65 78 69 73 74 73 20 74 68 65 6e 20 74 68  E exists then th
70190 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  e global variabl
701a0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f 54 72  e.** sqlite3IoTr
701b0 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ace is a pointer
701c0 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b   to a printf-lik
701d0 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74  e routine used t
701e0 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74  o.** print I/O t
701f0 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65 73 2e  racing messages.
70200 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49   .*/.#ifdef SQLI
70210 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43  TE_ENABLE_IOTRAC
70220 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41  E.# define IOTRA
70230 43 45 28 41 29 20 20 69 66 28 20 73 71 6c 69 74  CE(A)  if( sqlit
70240 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73 71 6c  e3IoTrace ){ sql
70250 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b 20 7d  ite3IoTrace A; }
70260 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70270 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
70280 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62  beIOTraceSql(Vdb
70290 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
702a0 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74  ATE void (*sqlit
702b0 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74  e3IoTrace)(const
702c0 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c   char*,...);.#el
702d0 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52  se.# define IOTR
702e0 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e 65 20  ACE(A).# define 
702f0 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61  sqlite3VdbeIOTra
70300 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a  ceSql(X).#endif.
70310 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
70320 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
70330 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
70340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70360 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
70370 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
70380 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a  ile global.c ***
70390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
703a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
703b0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
703c0 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a  008 June 13.**.*
703d0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
703e0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
703f0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
70400 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
70410 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
70420 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
70430 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
70440 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
70450 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
70460 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
70470 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
70480 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
70490 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
704a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
704b0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
704c0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
704d0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
704e0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
704f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
70520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
70530 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
70540 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20  ins definitions 
70550 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  of global variab
70560 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73  les and contants
70570 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 67 6c 6f  ..**.** $Id: glo
70580 62 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30  bal.c,v 1.12 200
70590 39 2f 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36  9/02/05 16:31:46
705a0 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
705b0 2f 2a 20 41 6e 20 61 72 72 61 79 20 74 6f 20 6d  /* An array to m
705c0 61 70 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 73  ap all upper-cas
705d0 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 74  e characters int
705e0 6f 20 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f  o their correspo
705f0 6e 64 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63  nding.** lower-c
70600 61 73 65 20 63 68 61 72 61 63 74 65 72 2e 20 0a  ase character. .
70610 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c  **.** SQLite onl
70620 79 20 63 6f 6e 73 69 64 65 72 73 20 55 53 2d 41  y considers US-A
70630 53 43 49 49 20 28 6f 72 20 45 42 43 44 49 43 29  SCII (or EBCDIC)
70640 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 57 65   characters.  We
70650 20 64 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c   do not.** handl
70660 65 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f  e case conversio
70670 6e 73 20 66 6f 72 20 74 68 65 20 55 54 46 20 63  ns for the UTF c
70680 68 61 72 61 63 74 65 72 20 73 65 74 20 73 69 6e  haracter set sin
70690 63 65 20 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a  ce the tables.**
706a0 20 69 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65   involved are ne
706b0 61 72 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62  arly as big or b
706c0 69 67 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74  igger than SQLit
706d0 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c  e itself..*/.SQL
706e0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
706f0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
70700 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
70710 77 65 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66  wer[] = {.#ifdef
70720 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20   SQLITE_ASCII.  
70730 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20      0,  1,  2,  
70740 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20  3,  4,  5,  6,  
70750 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31  7,  8,  9, 10, 1
70760 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31  1, 12, 13, 14, 1
70770 35 2c 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20  5, 16, 17,.     
70780 31 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20  18, 19, 20, 21, 
70790 32 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20  22, 23, 24, 25, 
707a0 32 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20  26, 27, 28, 29, 
707b0 33 30 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20  30, 31, 32, 33, 
707c0 33 34 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c  34, 35,.     36,
707d0 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c   37, 38, 39, 40,
707e0 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c   41, 42, 43, 44,
707f0 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c   45, 46, 47, 48,
70800 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c   49, 50, 51, 52,
70810 20 35 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35   53,.     54, 55
70820 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39  , 56, 57, 58, 59
70830 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33  , 60, 61, 62, 63
70840 2c 20 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39  , 64, 97, 98, 99
70850 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33  ,100,101,102,103
70860 2c 0a 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30  ,.    104,105,10
70870 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31  6,107,108,109,11
70880 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31  0,111,112,113,11
70890 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31  4,115,116,117,11
708a0 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20  8,119,120,121,. 
708b0 20 20 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20     122, 91, 92, 
708c0 39 33 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20  93, 94, 95, 96, 
708d0 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31  97, 98, 99,100,1
708e0 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31  01,102,103,104,1
708f0 30 35 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20  05,106,107,.    
70900 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c  108,109,110,111,
70910 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c  112,113,114,115,
70920 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c  116,117,118,119,
70930 31 32 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c  120,121,122,123,
70940 31 32 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36  124,125,.    126
70950 2c 31 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30  ,127,128,129,130
70960 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34  ,131,132,133,134
70970 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38  ,135,136,137,138
70980 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32  ,139,140,141,142
70990 2c 31 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34  ,143,.    144,14
709a0 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34  5,146,147,148,14
709b0 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35  9,150,151,152,15
709c0 33 2c 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35  3,154,155,156,15
709d0 37 2c 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36  7,158,159,160,16
709e0 31 2c 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31  1,.    162,163,1
709f0 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31  64,165,166,167,1
70a00 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31  68,169,170,171,1
70a10 37 32 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31  72,173,174,175,1
70a20 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a  76,177,178,179,.
70a30 20 20 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c      180,181,182,
70a40 31 38 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c  183,184,185,186,
70a50 31 38 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c  187,188,189,190,
70a60 31 39 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c  191,192,193,194,
70a70 31 39 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20  195,196,197,.   
70a80 20 31 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31   198,199,200,201
70a90 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35  ,202,203,204,205
70aa0 2c 32 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39  ,206,207,208,209
70ab0 2c 32 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33  ,210,211,212,213
70ac0 2c 32 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31  ,214,215,.    21
70ad0 36 2c 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32  6,217,218,219,22
70ae0 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32  0,221,222,223,22
70af0 34 2c 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32  4,225,226,227,22
70b00 38 2c 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33  8,229,230,231,23
70b10 32 2c 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32  2,233,.    234,2
70b20 33 35 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32  35,236,237,238,2
70b30 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32  39,240,241,242,2
70b40 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32  43,244,245,246,2
70b50 34 37 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32  47,248,249,250,2
70b60 35 31 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c  51,.    252,253,
70b70 32 35 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23  254,255.#endif.#
70b80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
70b90 44 49 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c  DIC.      0,  1,
70ba0 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c    2,  3,  4,  5,
70bb0 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c    6,  7,  8,  9,
70bc0 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c   10, 11, 12, 13,
70bd0 20 31 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a   14, 15, /* 0x *
70be0 2f 0a 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31  /.     16, 17, 1
70bf0 38 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32  8, 19, 20, 21, 2
70c00 32 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32  2, 23, 24, 25, 2
70c10 36 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33  6, 27, 28, 29, 3
70c20 30 2c 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a  0, 31, /* 1x */.
70c30 20 20 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c       32, 33, 34,
70c40 20 33 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c   35, 36, 37, 38,
70c50 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c   39, 40, 41, 42,
70c60 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c   43, 44, 45, 46,
70c70 20 34 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20   47, /* 2x */.  
70c80 20 20 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35     48, 49, 50, 5
70c90 31 2c 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35  1, 52, 53, 54, 5
70ca0 35 2c 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35  5, 56, 57, 58, 5
70cb0 39 2c 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36  9, 60, 61, 62, 6
70cc0 33 2c 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20  3, /* 3x */.    
70cd0 20 36 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c   64, 65, 66, 67,
70ce0 20 36 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c   68, 69, 70, 71,
70cf0 20 37 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c   72, 73, 74, 75,
70d00 20 37 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c   76, 77, 78, 79,
70d10 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38   /* 4x */.     8
70d20 30 2c 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38  0, 81, 82, 83, 8
70d30 34 2c 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38  4, 85, 86, 87, 8
70d40 38 2c 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39  8, 89, 90, 91, 9
70d50 32 2c 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f  2, 93, 94, 95, /
70d60 2a 20 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c  * 5x */.     96,
70d70 20 39 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c   97, 66, 67, 68,
70d80 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c   69, 70, 71, 72,
70d90 20 37 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c   73,106,107,108,
70da0 31 30 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20  109,110,111, /* 
70db0 36 78 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38  6x */.    112, 8
70dc0 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38  1, 82, 83, 84, 8
70dd0 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38  5, 86, 87, 88, 8
70de0 39 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32  9,122,123,124,12
70df0 35 2c 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78  5,126,127, /* 7x
70e00 20 2a 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c   */.    128,129,
70e10 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c  130,131,132,133,
70e20 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c  134,135,136,137,
70e30 31 33 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c  138,139,140,141,
70e40 31 34 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a  142,143, /* 8x *
70e50 2f 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34  /.    144,145,14
70e60 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35  6,147,148,149,15
70e70 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35  0,151,152,153,15
70e80 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35  4,155,156,157,15
70e90 36 2c 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a  6,159, /* 9x */.
70ea0 20 20 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c      160,161,162,
70eb0 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c  163,164,165,166,
70ec0 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c  167,168,169,170,
70ed0 31 37 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c  171,140,141,142,
70ee0 31 37 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20  175, /* Ax */.  
70ef0 20 20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37    176,177,178,17
70f00 39 2c 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38  9,180,181,182,18
70f10 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38  3,184,185,186,18
70f20 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39  7,188,189,190,19
70f30 31 2c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20  1, /* Bx */.    
70f40 31 39 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c  192,129,130,131,
70f50 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c  132,133,134,135,
70f60 31 33 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c  136,137,202,203,
70f70 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c  204,205,206,207,
70f80 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30   /* Cx */.    20
70f90 38 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34  8,145,146,147,14
70fa0 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35  8,149,150,151,15
70fb0 32 2c 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32  2,153,218,219,22
70fc0 30 2c 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f  0,221,222,223, /
70fd0 2a 20 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c  * Dx */.    224,
70fe0 32 32 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c  225,162,163,164,
70ff0 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c  165,166,167,168,
71000 31 36 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c  169,232,203,204,
71010 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20  205,206,207, /* 
71020 45 78 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34  Ex */.    239,24
71030 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34  0,241,242,243,24
71040 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34  4,245,246,247,24
71050 38 2c 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32  8,249,219,220,22
71060 31 2c 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78  1,222,255, /* Fx
71070 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
71080 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
71090 6e 67 20 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b  ng 256 byte look
710a0 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 64  up table is used
710b0 20 74 6f 20 73 75 70 70 6f 72 74 20 53 51 4c 69   to support SQLi
710c0 74 65 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20  tes built-in.** 
710d0 65 71 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 74  equivalents to t
710e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61  he following sta
710f0 6e 64 61 72 64 20 6c 69 62 72 61 72 79 20 66 75  ndard library fu
71100 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20  nctions:.**.**  
71110 20 69 73 73 70 61 63 65 28 29 20 20 20 20 20 20   isspace()      
71120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71130 20 20 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c    0x01.**   isal
71140 70 68 61 28 29 20 20 20 20 20 20 20 20 20 20 20  pha()           
71150 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
71160 32 0a 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29  2.**   isdigit()
71170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71180 20 20 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 20          0x04.** 
71190 20 20 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20    isalnum()     
711a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
711b0 20 20 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 78     0x06.**   isx
711c0 64 69 67 69 74 28 29 20 20 20 20 20 20 20 20 20  digit()         
711d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78                0x
711e0 30 38 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 28  08.**   toupper(
711f0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
71200 20 20 20 20 20 20 20 20 20 30 78 32 30 0a 2a 2a           0x20.**
71210 0a 2a 2a 20 42 69 74 20 30 78 32 30 20 69 73 20  .** Bit 0x20 is 
71220 73 65 74 20 69 66 20 74 68 65 20 6d 61 70 70 65  set if the mappe
71230 64 20 63 68 61 72 61 63 74 65 72 20 72 65 71 75  d character requ
71240 69 72 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e  ires translation
71250 20 74 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 73   to upper.** cas
71260 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63  e. i.e. if the c
71270 68 61 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f  haracter is a lo
71280 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 63  wer-case ASCII c
71290 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20  haracter..** If 
712a0 78 20 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73  x is a lower-cas
712b0 65 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65  e ASCII characte
712c0 72 2c 20 74 68 65 6e 20 69 74 73 20 75 70 70 65  r, then its uppe
712d0 72 2d 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e  r-case equivalen
712e0 74 0a 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 32  t.** is (x - 0x2
712f0 30 29 2e 20 54 68 65 72 65 66 6f 72 65 20 74 6f  0). Therefore to
71300 75 70 70 65 72 28 29 20 63 61 6e 20 62 65 20 69  upper() can be i
71310 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a  mplemented as:.*
71320 2a 0a 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d 61  *.**   (x & ~(ma
71330 70 5b 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a  p[x]&0x20)).**.*
71340 2a 20 53 74 61 6e 64 61 72 64 20 66 75 6e 63 74  * Standard funct
71350 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73  ion tolower() is
71360 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69   implemented usi
71370 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 55 70  ng the sqlite3Up
71380 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20  perToLower[].** 
71390 61 72 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29  array. tolower()
713a0 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 6f 66   is used more of
713b0 74 65 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 72  ten than toupper
713c0 28 29 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a  () by SQLite..**
713d0 0a 2a 2a 20 53 51 4c 69 74 65 27 73 20 76 65 72  .** SQLite's ver
713e0 73 69 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69  sions are identi
713f0 63 61 6c 20 74 6f 20 74 68 65 20 73 74 61 6e 64  cal to the stand
71400 61 72 64 20 76 65 72 73 69 6f 6e 73 20 61 73 73  ard versions ass
71410 75 6d 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c  uming a.** local
71420 65 20 6f 66 20 22 43 22 2e 20 54 68 65 79 20 61  e of "C". They a
71430 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  re implemented a
71440 73 20 6d 61 63 72 6f 73 20 69 6e 20 73 71 6c 69  s macros in sqli
71450 74 65 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64  teInt.h..*/.#ifd
71460 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a  ef SQLITE_ASCII.
71470 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
71480 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
71490 61 72 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d  ar sqlite3CtypeM
714a0 61 70 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78  ap[256] = {.  0x
714b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
714c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
714d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
714e0 2a 20 30 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e  * 00..07    ....
714f0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20  .... */.  0x00, 
71500 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31  0x01, 0x01, 0x01
71510 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78  , 0x01, 0x01, 0x
71520 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 38  00, 0x00,  /* 08
71530 2e 2e 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..0f    ........
71540 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30   */.  0x00, 0x00
71550 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71560 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71570 30 78 30 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37  0x00,  /* 10..17
71580 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
71590 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78    0x00, 0x00, 0x
715a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
715b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
715c0 2c 20 20 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20  ,  /* 18..1f    
715d0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
715e0 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  01, 0x00, 0x00, 
715f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71600 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
71610 2a 20 32 30 2e 2e 32 37 20 20 20 20 20 21 22 23  * 20..27     !"#
71620 24 25 26 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20  $%&' */.  0x00, 
71630 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71640 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71650 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38  00, 0x00,  /* 28
71660 2e 2e 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f  ..2f    ()*+,-./
71670 20 2a 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63   */.  0x0c, 0x0c
71680 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78  , 0x0c, 0x0c, 0x
71690 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20  0c, 0x0c, 0x0c, 
716a0 30 78 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37  0x0c,  /* 30..37
716b0 20 20 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0a      01234567 */.
716c0 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78    0x0c, 0x0c, 0x
716d0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
716e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
716f0 2c 20 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20  ,  /* 38..3f    
71700 38 39 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30  89:;<=>? */..  0
71710 78 30 30 2c 20 30 78 30 61 2c 20 30 78 30 61 2c  x00, 0x0a, 0x0a,
71720 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30   0x0a, 0x0a, 0x0
71730 61 2c 20 30 78 30 61 2c 20 30 78 30 32 2c 20 20  a, 0x0a, 0x02,  
71740 2f 2a 20 34 30 2e 2e 34 37 20 20 20 20 40 41 42  /* 40..47    @AB
71750 43 44 45 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c  CDEFG */.  0x02,
71760 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
71770 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
71780 78 30 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34  x02, 0x02,  /* 4
71790 38 2e 2e 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e  8..4f    HIJKLMN
717a0 4f 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30  O */.  0x02, 0x0
717b0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
717c0 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c  x02, 0x02, 0x02,
717d0 20 30 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35   0x02,  /* 50..5
717e0 37 20 20 20 20 50 51 52 53 54 55 56 57 20 2a 2f  7    PQRSTUVW */
717f0 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  .  0x02, 0x02, 0
71800 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x02, 0x00, 0x00,
71810 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71820 30 2c 20 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20  0,  /* 58..5f   
71830 20 58 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30   XYZ[\]^_ */.  0
71840 78 30 30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c  x00, 0x2a, 0x2a,
71850 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32   0x2a, 0x2a, 0x2
71860 61 2c 20 30 78 32 61 2c 20 30 78 32 32 2c 20 20  a, 0x2a, 0x22,  
71870 2f 2a 20 36 30 2e 2e 36 37 20 20 20 20 60 61 62  /* 60..67    `ab
71880 63 64 65 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c  cdefg */.  0x22,
71890 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
718a0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30  2, 0x22, 0x22, 0
718b0 78 32 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36  x22, 0x22,  /* 6
718c0 38 2e 2e 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e  8..6f    hijklmn
718d0 6f 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32  o */.  0x22, 0x2
718e0 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30  2, 0x22, 0x22, 0
718f0 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c  x22, 0x22, 0x22,
71900 20 30 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37   0x22,  /* 70..7
71910 37 20 20 20 20 70 71 72 73 74 75 76 77 20 2a 2f  7    pqrstuvw */
71920 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30  .  0x22, 0x22, 0
71930 78 32 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x22, 0x00, 0x00,
71940 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71950 30 2c 20 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20  0,  /* 78..7f   
71960 20 78 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20   xyz{|}~. */..  
71970 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71980 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71990 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
719a0 20 2f 2a 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e   /* 80..87    ..
719b0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
719c0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
719d0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
719e0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
719f0 38 38 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e  88..8f    ......
71a00 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
71a10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71a20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71a30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e  , 0x00,  /* 90..
71a40 39 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  97    ........ *
71a50 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
71a60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71a70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71a80 30 30 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20  00,  /* 98..9f  
71a90 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
71aa0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71ab0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71ac0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71ad0 20 2f 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e   /* a0..a7    ..
71ae0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
71af0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71b00 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71b10 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
71b20 61 38 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e  a8..af    ......
71b30 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
71b40 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
71b50 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71b60 2c 20 30 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e  , 0x00,  /* b0..
71b70 62 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  b7    ........ *
71b80 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
71b90 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
71ba0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
71bb0 30 30 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20  00,  /* b8..bf  
71bc0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20    ........ */.. 
71bd0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71be0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71bf0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71c00 20 20 2f 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e    /* c0..c7    .
71c10 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
71c20 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71c30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71c40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
71c50 20 63 38 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e   c8..cf    .....
71c60 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
71c70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71c80 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71c90 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e  0, 0x00,  /* d0.
71ca0 2e 64 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .d7    ........ 
71cb0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  */.  0x00, 0x00,
71cc0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71cd0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71ce0 78 30 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20  x00,  /* d8..df 
71cf0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
71d00 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71d10 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71d20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71d30 20 20 2f 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e    /* e0..e7    .
71d40 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
71d50 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71d60 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71d70 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
71d80 20 65 38 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e   e8..ef    .....
71d90 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30  ... */.  0x00, 0
71da0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
71db0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71dc0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e  0, 0x00,  /* f0.
71dd0 2e 66 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .f7    ........ 
71de0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  */.  0x00, 0x00,
71df0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
71e00 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
71e10 78 30 30 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20  x00   /* f8..ff 
71e20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d     ........ */.}
71e30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a  ;.#endif..../*.*
71e40 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
71e50 73 69 6e 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69  singleton contai
71e60 6e 73 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f  ns the global co
71e70 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a  nfiguration for.
71e80 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  ** the SQLite li
71e90 62 72 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  brary..*/.SQLITE
71ea0 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f  _PRIVATE SQLITE_
71eb0 57 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 74  WSD struct Sqlit
71ec0 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33  e3Config sqlite3
71ed0 43 6f 6e 66 69 67 20 3d 20 7b 0a 20 20 20 53 51  Config = {.   SQ
71ee0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d  LITE_DEFAULT_MEM
71ef0 53 54 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d  STATUS,  /* bMem
71f00 73 74 61 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20  stat */.   1,   
71f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71f20 20 20 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75        /* bCoreMu
71f30 74 65 78 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45  tex */.   SQLITE
71f40 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 31 2c 20  _THREADSAFE==1, 
71f50 20 20 20 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74       /* bFullMut
71f60 65 78 20 2a 2f 0a 20 20 20 30 78 37 66 66 66 66  ex */.   0x7ffff
71f70 66 66 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ffe,            
71f80 20 20 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20      /* mxStrlen 
71f90 2a 2f 0a 20 20 20 31 30 30 2c 20 20 20 20 20 20  */.   100,      
71fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71fb0 20 2f 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20   /* szLookaside 
71fc0 2a 2f 0a 20 20 20 35 30 30 2c 20 20 20 20 20 20  */.   500,      
71fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71fe0 20 2f 2a 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a   /* nLookaside *
71ff0 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c  /.   {0,0,0,0,0,
72000 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 20 20  0,0,0},         
72010 2f 2a 20 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c  /* m */.   {0,0,
72020 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20  0,0,0,0,0,0,0}, 
72030 20 20 20 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a        /* mutex *
72040 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c  /.   {0,0,0,0,0,
72050 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20  0,0,0,0,0,0},   
72060 2f 2a 20 70 63 61 63 68 65 20 2a 2f 0a 20 20 20  /* pcache */.   
72070 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20  (void*)0,       
72080 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48             /* pH
72090 65 61 70 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  eap */.   0,    
720a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
720b0 20 20 20 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f       /* nHeap */
720c0 0a 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20  .   0, 0,       
720d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
720e0 2a 20 6d 6e 48 65 61 70 2c 20 6d 78 48 65 61 70  * mnHeap, mxHeap
720f0 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c   */.   (void*)0,
72100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72110 20 20 2f 2a 20 70 53 63 72 61 74 63 68 20 2a 2f    /* pScratch */
72120 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
72130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
72140 2a 20 73 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20  * szScratch */. 
72150 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
72160 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
72170 6e 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 28  nScratch */.   (
72180 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20  void*)0,        
72190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61            /* pPa
721a0 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ge */.   0,     
721b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
721c0 20 20 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f      /* szPage */
721d0 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
721e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
721f0 2a 20 6e 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c  * nPage */.   0,
72200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72210 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61           /* mxPa
72220 72 73 65 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20  rserStack */.   
72230 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
72240 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68             /* sh
72250 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64  aredCacheEnabled
72260 20 2a 2f 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68   */.   /* All th
72270 65 20 72 65 73 74 20 6e 65 65 64 20 74 6f 20 61  e rest need to a
72280 6c 77 61 79 73 20 62 65 20 7a 65 72 6f 20 2a 2f  lways be zero */
72290 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
722a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
722b0 2a 20 69 73 49 6e 69 74 20 2a 2f 0a 20 20 20 30  * isInit */.   0
722c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
722d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 50            /* inP
722e0 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 20 30 2c  rogress */.   0,
722f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72300 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 4d 61           /* isMa
72310 6c 6c 6f 63 49 6e 69 74 20 2a 2f 0a 20 20 20 30  llocInit */.   0
72320 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
72330 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 49 6e            /* pIn
72340 69 74 4d 75 74 65 78 20 2a 2f 0a 20 20 20 30 2c  itMutex */.   0,
72350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72360 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 52 65 66           /* nRef
72370 49 6e 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a  InitMutex */.};.
72380 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 74 61 62  ../*.** Hash tab
72390 6c 65 20 66 6f 72 20 67 6c 6f 62 61 6c 20 66 75  le for global fu
723a0 6e 63 74 69 6f 6e 73 20 2d 20 66 75 6e 63 74 69  nctions - functi
723b0 6f 6e 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c  ons common to al
723c0 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  l.** database co
723d0 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 41 66 74 65  nnections.  Afte
723e0 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  r initialization
723f0 2c 20 74 68 69 73 20 74 61 62 6c 65 20 69 73 0a  , this table is.
72400 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f  ** read-only..*/
72410 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72420 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44  SQLITE_WSD FuncD
72430 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c  efHash sqlite3Gl
72440 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a  obalFunctions;..
72450 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  /*.** The value 
72460 6f 66 20 74 68 65 20 22 70 65 6e 64 69 6e 67 22  of the "pending"
72470 20 62 79 74 65 20 6d 75 73 74 20 62 65 20 30 78   byte must be 0x
72480 34 30 30 30 30 30 30 30 20 28 31 20 62 79 74 65  40000000 (1 byte
72490 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 2d 67   past the.** 1-g
724a0 69 62 61 62 79 74 65 20 62 6f 75 6e 64 61 72 79  ibabyte boundary
724b0 29 20 69 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c  ) in a compatibl
724c0 65 20 64 61 74 61 62 61 73 65 2e 20 20 53 51 4c  e database.  SQL
724d0 69 74 65 20 6e 65 76 65 72 20 75 73 65 73 0a 2a  ite never uses.*
724e0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70  * the database p
724f0 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  age that contain
72500 73 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  s the pending by
72510 74 65 2e 20 20 49 74 20 6e 65 76 65 72 20 61 74  te.  It never at
72520 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 72 65 61  tempts.** to rea
72530 64 20 6f 72 20 77 72 69 74 65 20 74 68 61 74 20  d or write that 
72540 70 61 67 65 2e 20 20 54 68 65 20 70 65 6e 64 69  page.  The pendi
72550 6e 67 20 62 79 74 65 20 70 61 67 65 20 69 73 20  ng byte page is 
72560 73 65 74 20 61 73 73 69 67 6e 0a 2a 2a 20 66 6f  set assign.** fo
72570 72 20 75 73 65 20 62 79 20 74 68 65 20 56 46 53  r use by the VFS
72580 20 6c 61 79 65 72 73 20 61 73 20 73 70 61 63 65   layers as space
72590 20 66 6f 72 20 6d 61 6e 61 67 69 6e 67 20 66 69   for managing fi
725a0 6c 65 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  le locks..**.** 
725b0 44 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20  During testing, 
725c0 69 74 20 69 73 20 6f 66 74 65 6e 20 64 65 73 69  it is often desi
725d0 72 61 62 6c 65 20 74 6f 20 6d 6f 76 65 20 74 68  rable to move th
725e0 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74  e pending byte t
725f0 6f 0a 2a 2a 20 61 20 64 69 66 66 65 72 65 6e 74  o.** a different
72600 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65   position in the
72610 20 66 69 6c 65 2e 20 20 54 68 69 73 20 61 6c 6c   file.  This all
72620 6f 77 73 20 63 6f 64 65 20 74 68 61 74 20 68 61  ows code that ha
72630 73 20 74 6f 0a 2a 2a 20 64 65 61 6c 20 77 69 74  s to.** deal wit
72640 68 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  h the pending by
72650 74 65 20 74 6f 20 72 75 6e 20 6f 6e 20 66 69 6c  te to run on fil
72660 65 73 20 74 68 61 74 20 61 72 65 20 6d 75 63 68  es that are much
72670 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e   smaller.** than
72680 20 31 20 47 69 42 2e 20 20 54 68 65 20 73 71 6c   1 GiB.  The sql
72690 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
726a0 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61  l() interface ca
726b0 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20  n be used to.** 
726c0 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 69 6e 67  move the pending
726d0 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50   byte..**.** IMP
726e0 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e  ORTANT:  Changin
726f0 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  g the pending by
72700 74 65 20 74 6f 20 61 6e 79 20 76 61 6c 75 65 20  te to any value 
72710 6f 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 30 78  other than.** 0x
72720 34 30 30 30 30 30 30 30 20 72 65 73 75 6c 74 73  40000000 results
72730 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69   in an incompati
72740 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ble database fil
72750 65 20 66 6f 72 6d 61 74 21 0a 2a 2a 20 43 68 61  e format!.** Cha
72760 6e 67 69 6e 67 20 74 68 65 20 70 65 6e 64 69 6e  nging the pendin
72770 67 20 62 79 74 65 20 64 75 72 69 6e 67 20 6f 70  g byte during op
72780 65 72 61 74 69 6e 67 20 72 65 73 75 6c 74 73 20  erating results 
72790 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20  in undefined.** 
727a0 61 6e 64 20 64 69 6c 65 74 65 72 69 6f 75 73 20  and dileterious 
727b0 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c  behavior..*/.SQL
727c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
727d0 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79  sqlite3PendingBy
727e0 74 65 20 3d 20 30 78 34 30 30 30 30 30 30 30 3b  te = 0x40000000;
727f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
72800 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e  * End of global.
72810 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
72820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72840 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
72850 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 74 61  * Begin file sta
72860 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tus.c **********
72870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72890 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e  /./*.** 2008 Jun
728a0 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  e 18.**.** The a
728b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
728c0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
728d0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
728e0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
728f0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
72900 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
72910 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
72920 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
72930 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
72940 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
72950 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
72960 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
72970 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
72980 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
72990 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
729a0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
729b0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
729c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
729f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
72a00 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  ***.**.** This m
72a10 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73  odule implements
72a20 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61   the sqlite3_sta
72a30 74 75 73 28 29 20 69 6e 74 65 72 66 61 63 65 20  tus() interface 
72a40 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66  and related.** f
72a50 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a  unctionality..**
72a60 0a 2a 2a 20 24 49 64 3a 20 73 74 61 74 75 73 2e  .** $Id: status.
72a70 63 2c 76 20 31 2e 39 20 32 30 30 38 2f 30 39 2f  c,v 1.9 2008/09/
72a80 30 32 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20  02 00:52:52 drh 
72a90 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
72aa0 56 61 72 69 61 62 6c 65 73 20 69 6e 20 77 68 69  Variables in whi
72ab0 63 68 20 74 6f 20 72 65 63 6f 72 64 20 73 74 61  ch to record sta
72ac0 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  tus information.
72ad0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
72ae0 63 74 20 73 71 6c 69 74 65 33 53 74 61 74 54 79  ct sqlite3StatTy
72af0 70 65 20 73 71 6c 69 74 65 33 53 74 61 74 54 79  pe sqlite3StatTy
72b00 70 65 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54  pe;.static SQLIT
72b10 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c  E_WSD struct sql
72b20 69 74 65 33 53 74 61 74 54 79 70 65 20 7b 0a 20  ite3StatType {. 
72b30 20 69 6e 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d   int nowValue[9]
72b40 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ;         /* Cur
72b50 72 65 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20  rent value */.  
72b60 69 6e 74 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20  int mxValue[9]; 
72b70 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
72b80 6d 75 6d 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73  mum value */.} s
72b90 71 6c 69 74 65 33 53 74 61 74 20 3d 20 7b 20 7b  qlite3Stat = { {
72ba0 30 2c 7d 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f  0,}, {0,} };.../
72bb0 2a 20 54 68 65 20 22 77 73 64 53 74 61 74 22 20  * The "wsdStat" 
72bc0 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c  macro will resol
72bd0 76 65 20 74 6f 20 74 68 65 20 73 74 61 74 75 73  ve to the status
72be0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
72bf0 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49  state vector.  I
72c00 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  f writable stati
72c10 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70  c data is unsupp
72c20 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72  orted on the tar
72c30 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20  get,.** we have 
72c40 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74  to locate the st
72c50 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75  ate vector at ru
72c60 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20  n-time.  In the 
72c70 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63  more common.** c
72c80 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62  ase where writab
72c90 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
72ca0 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64  s supported, wsd
72cb0 53 74 61 74 20 63 61 6e 20 72 65 66 65 72 20 64  Stat can refer d
72cc0 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68  irectly.** to th
72cd0 65 20 22 73 71 6c 69 74 65 33 53 74 61 74 22 20  e "sqlite3Stat" 
72ce0 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 63  state vector dec
72cf0 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a  lared above..*/.
72d00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
72d10 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20  IT_WSD.# define 
72d20 77 73 64 53 74 61 74 49 6e 69 74 20 20 73 71 6c  wsdStatInit  sql
72d30 69 74 65 33 53 74 61 74 54 79 70 65 20 2a 78 20  ite3StatType *x 
72d40 3d 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65  = &GLOBAL(sqlite
72d50 33 53 74 61 74 54 79 70 65 2c 73 71 6c 69 74 65  3StatType,sqlite
72d60 33 53 74 61 74 29 0a 23 20 64 65 66 69 6e 65 20  3Stat).# define 
72d70 77 73 64 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c  wsdStat x[0].#el
72d80 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53  se.# define wsdS
72d90 74 61 74 49 6e 69 74 0a 23 20 64 65 66 69 6e 65  tatInit.# define
72da0 20 77 73 64 53 74 61 74 20 73 71 6c 69 74 65 33   wsdStat sqlite3
72db0 53 74 61 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  Stat.#endif../*.
72dc0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75  ** Return the cu
72dd0 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 61  rrent value of a
72de0 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65   status paramete
72df0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
72e00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72e10 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 20  StatusValue(int 
72e20 6f 70 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e  op){.  wsdStatIn
72e30 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70  it;.  assert( op
72e40 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53  >=0 && op<ArrayS
72e50 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56  ize(wsdStat.nowV
72e60 61 6c 75 65 29 20 29 3b 0a 20 20 72 65 74 75 72  alue) );.  retur
72e70 6e 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  n wsdStat.nowVal
72e80 75 65 5b 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ue[op];.}../*.**
72e90 20 41 64 64 20 4e 20 74 6f 20 74 68 65 20 76 61   Add N to the va
72ea0 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20  lue of a status 
72eb0 72 65 63 6f 72 64 2e 20 20 49 74 20 69 73 20 61  record.  It is a
72ec0 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 0a  ssumed that the.
72ed0 2a 2a 20 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20  ** caller holds 
72ee0 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b  appropriate lock
72ef0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
72f00 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
72f10 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 20 6f  3StatusAdd(int o
72f20 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64  p, int N){.  wsd
72f30 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65  StatInit;.  asse
72f40 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c  rt( op>=0 && op<
72f50 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61  ArraySize(wsdSta
72f60 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20  t.nowValue) );. 
72f70 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
72f80 65 5b 6f 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66  e[op] += N;.  if
72f90 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  ( wsdStat.nowVal
72fa0 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d  ue[op]>wsdStat.m
72fb0 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20  xValue[op] ){.  
72fc0 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75    wsdStat.mxValu
72fd0 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e  e[op] = wsdStat.
72fe0 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20  nowValue[op];.  
72ff0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
73000 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74  he value of a st
73010 61 74 75 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51  atus to X..*/.SQ
73020 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
73030 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53  d sqlite3StatusS
73040 65 74 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58  et(int op, int X
73050 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74  ){.  wsdStatInit
73060 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d  ;.  assert( op>=
73070 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a  0 && op<ArraySiz
73080 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  e(wsdStat.nowVal
73090 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74  ue) );.  wsdStat
730a0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20  .nowValue[op] = 
730b0 58 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61 74  X;.  if( wsdStat
730c0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73  .nowValue[op]>ws
730d0 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70  dStat.mxValue[op
730e0 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74  ] ){.    wsdStat
730f0 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77  .mxValue[op] = w
73100 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
73110 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  op];.  }.}../*.*
73120 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69  * Query status i
73130 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nformation..**.*
73140 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
73150 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ation assumes th
73160 61 74 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72  at reading or wr
73170 69 74 69 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64  iting an aligned
73180 0a 2a 2a 20 33 32 2d 62 69 74 20 69 6e 74 65 67  .** 32-bit integ
73190 65 72 20 69 73 20 61 6e 20 61 74 6f 6d 69 63 20  er is an atomic 
731a0 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 20 74  operation.  If t
731b0 68 61 74 20 61 73 73 75 6d 70 74 69 6f 6e 20 69  hat assumption i
731c0 73 20 6e 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74  s not true,.** t
731d0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
731e0 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61   is not threadsa
731f0 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  fe..*/.SQLITE_AP
73200 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  I int sqlite3_st
73210 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74  atus(int op, int
73220 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20   *pCurrent, int 
73230 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74  *pHighwater, int
73240 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 77   resetFlag){.  w
73250 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 69 66  sdStatInit;.  if
73260 28 20 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72  ( op<0 || op>=Ar
73270 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e  raySize(wsdStat.
73280 6e 6f 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20  nowValue) ){.   
73290 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
732a0 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43  ISUSE;.  }.  *pC
732b0 75 72 72 65 6e 74 20 3d 20 77 73 64 53 74 61 74  urrent = wsdStat
732c0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20  .nowValue[op];. 
732d0 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 77   *pHighwater = w
732e0 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f  sdStat.mxValue[o
732f0 70 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46  p];.  if( resetF
73300 6c 61 67 20 29 7b 0a 20 20 20 20 77 73 64 53 74  lag ){.    wsdSt
73310 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d  at.mxValue[op] =
73320 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75   wsdStat.nowValu
73330 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74  e[op];.  }.  ret
73340 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
73350 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74  ../*.** Query st
73360 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  atus information
73370 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61   for a single da
73380 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
73390 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  n.*/.SQLITE_API 
733a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73  int sqlite3_db_s
733b0 74 61 74 75 73 28 0a 20 20 73 71 6c 69 74 65 33  tatus(.  sqlite3
733c0 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
733d0 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
733e0 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 6f 73 65 20  onnection whose 
733f0 73 74 61 74 75 73 20 69 73 20 64 65 73 69 72 65  status is desire
73400 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  d */.  int op,  
73410 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
73420 53 74 61 74 75 73 20 76 65 72 62 20 2a 2f 0a 20  Status verb */. 
73430 20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20   int *pCurrent, 
73440 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
73450 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 68 65  current value he
73460 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69  re */.  int *pHi
73470 67 68 77 61 74 65 72 2c 20 20 20 20 20 20 2f 2a  ghwater,      /*
73480 20 57 72 69 74 65 20 68 69 67 68 2d 77 61 74 65   Write high-wate
73490 72 20 6d 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20  r mark here */. 
734a0 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67 20 20   int resetFlag  
734b0 20 20 20 20 20 20 20 2f 2a 20 52 65 73 65 74 20         /* Reset 
734c0 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20  high-water mark 
734d0 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20  if true */.){.  
734e0 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20  switch( op ){.  
734f0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42    case SQLITE_DB
73500 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45  STATUS_LOOKASIDE
73510 5f 55 53 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a  _USED: {.      *
73520 70 43 75 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c  pCurrent = db->l
73530 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20  ookaside.nOut;. 
73540 20 20 20 20 20 2a 70 48 69 67 68 77 61 74 65 72       *pHighwater
73550 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
73560 2e 6d 78 4f 75 74 3b 0a 20 20 20 20 20 20 69 66  .mxOut;.      if
73570 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20  ( resetFlag ){. 
73580 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61         db->looka
73590 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d  side.mxOut = db-
735a0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b  >lookaside.nOut;
735b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
735c0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
735d0 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
735e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
735f0 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  RROR;.    }.  }.
73600 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
73610 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  OK;.}../********
73620 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74  ****** End of st
73630 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  atus.c *********
73640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73660 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
73670 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
73680 65 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e date.c *******
73690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
736a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
736b0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
736c0 33 20 4f 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a  3 October 31.**.
736d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
736e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
736f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
73700 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
73710 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
73720 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
73730 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
73740 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
73750 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
73760 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
73770 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
73780 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
73790 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
737a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
737b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
737c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
737d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
737e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
737f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
73820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
73830 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
73840 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
73850 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
73860 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a   date and time.*
73870 2a 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  * functions for 
73880 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20  SQLite.  .**.** 
73890 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e  There is only on
738a0 65 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f  e exported symbo
738b0 6c 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d  l in this file -
738c0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a   the function.**
738d0 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
738e0 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e  DateTimeFunction
738f0 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65  s() found at the
73900 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66   bottom of the f
73910 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65  ile..** All othe
73920 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20  r code has file 
73930 73 63 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  scope..**.** $Id
73940 3a 20 64 61 74 65 2e 63 2c 76 20 31 2e 31 30 37  : date.c,v 1.107
73950 20 32 30 30 39 2f 30 35 2f 30 33 20 32 30 3a 32   2009/05/03 20:2
73960 33 3a 35 33 20 64 72 68 20 45 78 70 20 24 0a 2a  3:53 drh Exp $.*
73970 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 63  *.** SQLite proc
73980 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 20  esses all times 
73990 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a 75 6c  and dates as Jul
739a0 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 2e  ian Day numbers.
739b0 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 61    The.** dates a
739c0 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73 74 6f  nd times are sto
739d0 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d 62 65  red as the numbe
739e0 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20  r of days since 
739f0 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e  noon.** in Green
73a00 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72  wich on November
73a10 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61   24, 4714 B.C. a
73a20 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
73a30 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c  Gregorian.** cal
73a40 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a  endar system. .*
73a50 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 20  *.** 1970-01-01 
73a60 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32  00:00:00 is JD 2
73a70 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30  440587.5.** 2000
73a80 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20  -01-01 00:00:00 
73a90 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e 35 0a  is JD 2451544.5.
73aa0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  **.** This imple
73ab0 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 73  mention requires
73ac0 20 79 65 61 72 73 20 74 6f 20 62 65 20 65 78 70   years to be exp
73ad0 72 65 73 73 65 64 20 61 73 20 61 20 34 2d 64 69  ressed as a 4-di
73ae0 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68  git number.** wh
73af0 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f  ich means that o
73b00 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77 65 65  nly dates betwee
73b10 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61 6e 64  n 0000-01-01 and
73b20 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a   9999-12-31 can.
73b30 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e 74 65  ** be represente
73b40 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 6a  d, even though j
73b50 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72  ulian day number
73b60 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77  s allow a much w
73b70 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66  ider.** range of
73b80 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   dates..**.** Th
73b90 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65  e Gregorian cale
73ba0 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73 20 75  ndar system is u
73bb0 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 65  sed for all date
73bc0 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20  s and times,.** 
73bd0 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61 74 20  even those that 
73be0 70 72 65 64 61 74 65 20 74 68 65 20 47 72 65 67  predate the Greg
73bf0 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20  orian calendar. 
73c00 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73 75 61   Historians usua
73c10 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a  lly.** use the J
73c20 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 66  ulian calendar f
73c30 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72 20 74  or dates prior t
73c40 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61 6e 64  o 1582-10-15 and
73c50 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74   for some.** dat
73c60 65 73 20 61 66 74 65 72 77 61 72 64 73 2c 20 64  es afterwards, d
73c70 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61  epending on loca
73c80 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66 20 74  le.  Beware of t
73c90 68 69 73 20 64 69 66 66 65 72 65 6e 63 65 2e 0a  his difference..
73ca0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72  **.** The conver
73cb0 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20  sion algorithms 
73cc0 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  are implemented 
73cd0 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70  based on descrip
73ce0 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20  tions.** in the 
73cf0 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a  following text:.
73d00 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20  **.**      Jean 
73d10 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 73  Meeus.**      As
73d20 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72  tronomical Algor
73d30 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 69  ithms, 2nd Editi
73d40 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 20  on, 1998.**     
73d50 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36 2d 36   ISBM 0-943396-6
73d60 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c  1-1.**      Will
73d70 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a  mann-Bell, Inc.*
73d80 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c  *      Richmond,
73d90 20 56 69 72 67 69 6e 69 61 20 28 55 53 41 29 0a   Virginia (USA).
73da0 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d  */.#include <tim
73db0 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51  e.h>..#ifndef SQ
73dc0 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49  LITE_OMIT_DATETI
73dd0 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20  ME_FUNCS../*.** 
73de0 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 6f 77  On recent Window
73df0 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 68 65  s platforms, the
73e00 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 20 66   localtime_s() f
73e10 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  unction is avail
73e20 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 74 20  able.** as part 
73e30 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 20 43  of the "Secure C
73e40 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 65 6e  RT". It is essen
73e50 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c 65 6e  tially equivalen
73e60 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69  t to .** localti
73e70 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 6c 65  me_r() available
73e80 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f 53 49   under most POSI
73e90 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63  X platforms, exc
73ea0 65 70 74 20 74 68 61 74 20 74 68 65 20 0a 2a 2a  ept that the .**
73eb0 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 70 61   order of the pa
73ec0 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65  rameters is reve
73ed0 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  rsed..**.** See 
73ee0 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72  http://msdn.micr
73ef0 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f  osoft.com/en-us/
73f00 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79 65  library/a442x3ye
73f10 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a 2a 2a  (VS.80).aspx..**
73f20 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 72 20  .** If the user 
73f30 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 74 65  has not indicate
73f40 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c 74 69  d to use localti
73f50 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 6c 74  me_r() or localt
73f60 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 61  ime_s().** alrea
73f70 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e  dy, check for an
73f80 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e 76 69   MSVC build envi
73f90 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 72 6f  ronment that pro
73fa0 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74  vides .** localt
73fb0 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 20  ime_s()..*/.#if 
73fc0 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f  !defined(HAVE_LO
73fd0 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 21 64  CALTIME_R) && !d
73fe0 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41  efined(HAVE_LOCA
73ff0 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a 20 20  LTIME_S) && \.  
74000 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f     defined(_MSC_
74010 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 64 28  VER) && defined(
74020 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f 44 45  _CRT_INSECURE_DE
74030 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 6e 65  PRECATE).#define
74040 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f   HAVE_LOCALTIME_
74050 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  S 1.#endif../*.*
74060 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 66 6f  * A structure fo
74070 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67  r holding a sing
74080 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  le date and time
74090 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
740a0 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 61 74  uct DateTime Dat
740b0 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44 61  eTime;.struct Da
740c0 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74  teTime {.  sqlit
740d0 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a  e3_int64 iJD; /*
740e0 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20   The julian day 
740f0 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 36 34  number times 864
74100 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59  00000 */.  int Y
74110 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a  , M, D;       /*
74120 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e   Year, month, an
74130 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68  d day */.  int h
74140 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  , m;          /*
74150 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74 65   Hour and minute
74160 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20  s */.  int tz;  
74170 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d            /* Tim
74180 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e 20  ezone offset in 
74190 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75  minutes */.  dou
741a0 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 20 20  ble s;          
741b0 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20  /* Seconds */.  
741c0 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b 20 20  char validYMD;  
741d0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69     /* True (1) i
741e0 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69  f Y,M,D are vali
741f0 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69  d */.  char vali
74200 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54 72 75  dHMS;     /* Tru
74210 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73 20 61  e (1) if h,m,s a
74220 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68  re valid */.  ch
74230 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 20 20  ar validJD;     
74240 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20   /* True (1) if 
74250 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a  iJD is valid */.
74260 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a 3b 20    char validTZ; 
74270 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29       /* True (1)
74280 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 64 20   if tz is valid 
74290 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f  */.};.../*.** Co
742a0 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e 74 6f  nvert zDate into
742b0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74   one or more int
742c0 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 6f 6e  egers.  Addition
742d0 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20  al arguments.** 
742e0 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 20 6f  come in groups o
742f0 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  f 5 as follows:.
74300 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20 20  **.**       N   
74310 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 64 69      number of di
74320 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e 74 65  gits in the inte
74330 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e  ger.**       min
74340 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c       minimum all
74350 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68  owed value of th
74360 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  e integer.**    
74370 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 69 6d     max     maxim
74380 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65  um allowed value
74390 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a   of the integer.
743a0 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 20 20  **       nextC  
743b0 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
743c0 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 65 67   after the integ
743d0 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 61 6c  er.**       pVal
743e0 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 72 69      where to wri
743f0 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 73 20  te the integers 
74400 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e  value..**.** Con
74410 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75  versions continu
74420 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68  e until one with
74430 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 6e 63   nextC==0 is enc
74440 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65  ountered..** The
74450 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
74460 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
74470 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e 76 65  successful conve
74480 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69  rsions..*/.stati
74490 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 73 28  c int getDigits(
744a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74  const char *zDat
744b0 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  e, ...){.  va_li
744c0 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c  st ap;.  int val
744d0 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74  ;.  int N;.  int
744e0 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b   min;.  int max;
744f0 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20  .  int nextC;.  
74500 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74  int *pVal;.  int
74510 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73   cnt = 0;.  va_s
74520 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 29 3b  tart(ap, zDate);
74530 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76  .  do{.    N = v
74540 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
74550 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67      min = va_arg
74560 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d  (ap, int);.    m
74570 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ax = va_arg(ap, 
74580 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 43 20  int);.    nextC 
74590 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
745a0 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 76 61  );.    pVal = va
745b0 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a  _arg(ap, int*);.
745c0 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20      val = 0;.   
745d0 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20   while( N-- ){. 
745e0 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
745f0 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29  3Isdigit(*zDate)
74600 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
74610 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a   end_getDigits;.
74620 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 61        }.      va
74630 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44  l = val*10 + *zD
74640 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  ate - '0';.     
74650 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a   zDate++;.    }.
74660 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20      if( val<min 
74670 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e  || val>max || (n
74680 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74 43  extC!=0 && nextC
74690 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20  !=*zDate) ){.   
746a0 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44     goto end_getD
746b0 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20  igits;.    }.   
746c0 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20   *pVal = val;.  
746d0 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63    zDate++;.    c
746e0 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20  nt++;.  }while( 
746f0 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74  nextC );.end_get
74700 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64  Digits:.  va_end
74710 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 63  (ap);.  return c
74720 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  nt;.}../*.** Rea
74730 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20  d text from z[] 
74740 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f  and convert into
74750 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
74760 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72  t number.  Retur
74770 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  n.** the number 
74780 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65 72  of digits conver
74790 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ted..*/.#define 
747a0 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65 33  getValue sqlite3
747b0 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73  AtoF../*.** Pars
747c0 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74  e a timezone ext
747d0 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e  ension on the en
747e0 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d 65  d of a date-time
747f0 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 69  ..** The extensi
74800 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  on is of the for
74810 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  m:.**.**        
74820 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a  (+/-)HH:MM.**.**
74830 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 6e   Or the "zulu" n
74840 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20  otation:.**.**  
74850 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66        Z.**.** If
74860 20 74 68 65 20 70 61 72 73 65 20 69 73 20 73 75   the parse is su
74870 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20  ccessful, write 
74880 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
74890 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e  nutes.** of chan
748a0 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20  ge in p->tz and 
748b0 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20  return 0.  If a 
748c0 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63  parser error occ
748d0 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e  urs,.** return n
748e0 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41  on-zero..**.** A
748f0 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 69   missing specifi
74900 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64  er is not consid
74910 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  ered an error..*
74920 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
74930 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74  seTimezone(const
74940 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61   char *zDate, Da
74950 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
74960 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74  t sgn = 0;.  int
74970 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74   nHr, nMn;.  int
74980 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c   c;.  while( sql
74990 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61  ite3Isspace(*zDa
749a0 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20  te) ){ zDate++; 
749b0 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20  }.  p->tz = 0;. 
749c0 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69   c = *zDate;.  i
749d0 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  f( c=='-' ){.   
749e0 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c   sgn = -1;.  }el
749f0 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b  se if( c=='+' ){
74a00 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20  .    sgn = +1;. 
74a10 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a   }else if( c=='Z
74a20 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20  ' || c=='z' ){. 
74a30 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
74a40 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a  goto zulu_time;.
74a50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
74a60 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20  urn c!=0;.  }.  
74a70 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67  zDate++;.  if( g
74a80 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20  etDigits(zDate, 
74a90 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26  2, 0, 14, ':', &
74aa0 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30  nHr, 2, 0, 59, 0
74ab0 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20  , &nMn)!=2 ){.  
74ac0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
74ad0 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20    zDate += 5;.  
74ae0 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e  p->tz = sgn*(nMn
74af0 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75   + nHr*60);.zulu
74b00 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20  _time:.  while( 
74b10 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a  sqlite3Isspace(*
74b20 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b  zDate) ){ zDate+
74b30 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
74b40 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Date!=0;.}../*.*
74b50 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 6f 66  * Parse times of
74b60 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20   the form HH:MM 
74b70 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48  or HH:MM:SS or H
74b80 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a  H:MM:SS.FFFF..**
74b90 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64   The HH, MM, and
74ba0 20 53 53 20 6d 75 73 74 20 65 61 63 68 20 62 65   SS must each be
74bb0 20 65 78 61 63 74 6c 79 20 32 20 64 69 67 69 74   exactly 2 digit
74bc0 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 63 74  s.  The.** fract
74bd0 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 46 46  ional seconds FF
74be0 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72  FF can be one or
74bf0 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a   more digits..**
74c00 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20  .** Return 1 if 
74c10 74 68 65 72 65 20 69 73 20 61 20 70 61 72 73 69  there is a parsi
74c20 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30 20 6f  ng error and 0 o
74c30 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74  n success..*/.st
74c40 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 48 68  atic int parseHh
74c50 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 72 20  MmSs(const char 
74c60 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65  *zDate, DateTime
74c70 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d   *p){.  int h, m
74c80 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73  , s;.  double ms
74c90 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 67 65   = 0.0;.  if( ge
74ca0 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32  tDigits(zDate, 2
74cb0 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68  , 0, 24, ':', &h
74cc0 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26  , 2, 0, 59, 0, &
74cd0 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74  m)!=2 ){.    ret
74ce0 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61  urn 1;.  }.  zDa
74cf0 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a  te += 5;.  if( *
74d00 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20  zDate==':' ){.  
74d10 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69    zDate++;.    i
74d20 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61  f( getDigits(zDa
74d30 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c  te, 2, 0, 59, 0,
74d40 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 20 20   &s)!=1 ){.     
74d50 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
74d60 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b  .    zDate += 2;
74d70 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74 65 3d  .    if( *zDate=
74d80 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65 33 49  ='.' && sqlite3I
74d90 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31 5d 29  sdigit(zDate[1])
74da0 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65   ){.      double
74db0 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20   rScale = 1.0;. 
74dc0 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20       zDate++;.  
74dd0 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
74de0 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 65  e3Isdigit(*zDate
74df0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 73 20  ) ){.        ms 
74e00 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61  = ms*10.0 + *zDa
74e10 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20  te - '0';.      
74e20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30    rScale *= 10.0
74e30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 65 2b  ;.        zDate+
74e40 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  +;.      }.     
74e50 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20   ms /= rScale;. 
74e60 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
74e70 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70    s = 0;.  }.  p
74e80 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  ->validJD = 0;. 
74e90 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31   p->validHMS = 1
74ea0 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20  ;.  p->h = h;.  
74eb0 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73  p->m = m;.  p->s
74ec0 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28   = s + ms;.  if(
74ed0 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a   parseTimezone(z
74ee0 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 75 72  Date, p) ) retur
74ef0 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54  n 1;.  p->validT
74f00 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31  Z = (p->tz!=0)?1
74f10 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  :0;.  return 0;.
74f20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
74f30 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44   from YYYY-MM-DD
74f40 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c   HH:MM:SS to jul
74f50 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c 77  ian day.  We alw
74f60 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68  ays assume.** th
74f70 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44  at the YYYY-MM-D
74f80 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74  D is according t
74f90 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20  o the Gregorian 
74fa0 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20  calendar..**.** 
74fb0 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75  Reference:  Meeu
74fc0 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61  s page 61.*/.sta
74fd0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65  tic void compute
74fe0 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  JD(DateTime *p){
74ff0 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20  .  int Y, M, D, 
75000 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20  A, B, X1, X2;.. 
75010 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20   if( p->validJD 
75020 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
75030 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20  p->validYMD ){. 
75040 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20     Y = p->Y;.   
75050 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44   M = p->M;.    D
75060 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65   = p->D;.  }else
75070 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b 20  {.    Y = 2000; 
75080 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 70   /* If no YMD sp
75090 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 20  ecified, assume 
750a0 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20  2000-Jan-01 */. 
750b0 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20     M = 1;.    D 
750c0 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d  = 1;.  }.  if( M
750d0 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a  <=2 ){.    Y--;.
750e0 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d      M += 12;.  }
750f0 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20  .  A = Y/100;.  
75100 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f 34  B = 2 - A + (A/4
75110 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 35 2a  );.  X1 = 36525*
75120 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 20 20  (Y+4716)/100;.  
75130 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31  X2 = 306001*(M+1
75140 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a  )/10000;.  p->iJ
75150 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  D = (sqlite3_int
75160 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b 20 44  64)((X1 + X2 + D
75170 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 29 20   + B - 1524.5 ) 
75180 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 70  * 86400000);.  p
75190 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20  ->validJD = 1;. 
751a0 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53   if( p->validHMS
751b0 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b   ){.    p->iJD +
751c0 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b  = p->h*3600000 +
751d0 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73   p->m*60000 + (s
751e0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 70 2d  qlite3_int64)(p-
751f0 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 69 66  >s*1000);.    if
75200 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a  ( p->validTZ ){.
75210 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20        p->iJD -= 
75220 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20  p->tz*60000;.   
75230 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d     p->validYMD =
75240 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c   0;.      p->val
75250 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20  idHMS = 0;.     
75260 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b   p->validTZ = 0;
75270 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
75280 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 73 20  .** Parse dates 
75290 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a  of the form.**.*
752a0 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44  *     YYYY-MM-DD
752b0 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a   HH:MM:SS.FFF.**
752c0 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20       YYYY-MM-DD 
752d0 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20  HH:MM:SS.**     
752e0 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d  YYYY-MM-DD HH:MM
752f0 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d  .**     YYYY-MM-
75300 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74  DD.**.** Write t
75310 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 74  he result into t
75320 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 75  he DateTime stru
75330 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e  cture and return
75340 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73   0.** on success
75350 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 69 6e   and 1 if the in
75360 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 6e 6f  put string is no
75370 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a  t a well-formed.
75380 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74  ** date..*/.stat
75390 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 79 79  ic int parseYyyy
753a0 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 72 20  MmDd(const char 
753b0 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65  *zDate, DateTime
753c0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d   *p){.  int Y, M
753d0 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28  , D, neg;..  if(
753e0 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29   zDate[0]=='-' )
753f0 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20  {.    zDate++;. 
75400 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65     neg = 1;.  }e
75410 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30  lse{.    neg = 0
75420 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 74 44  ;.  }.  if( getD
75430 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c  igits(zDate,4,0,
75440 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c  9999,'-',&Y,2,1,
75450 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31  12,'-',&M,2,1,31
75460 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 20 20  ,0,&D)!=3 ){.   
75470 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
75480 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20   zDate += 10;.  
75490 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
754a0 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c  space(*zDate) ||
754b0 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74   'T'==*(u8*)zDat
754c0 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a  e ){ zDate++; }.
754d0 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53    if( parseHhMmS
754e0 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29  s(zDate, p)==0 )
754f0 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74 20  {.    /* We got 
75500 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65  the time */.  }e
75510 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d  lse if( *zDate==
75520 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69  0 ){.    p->vali
75530 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  dHMS = 0;.  }els
75540 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  e{.    return 1;
75550 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a  .  }.  p->validJ
75560 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69  D = 0;.  p->vali
75570 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59  dYMD = 1;.  p->Y
75580 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b   = neg ? -Y : Y;
75590 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70  .  p->M = M;.  p
755a0 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20 70  ->D = D;.  if( p
755b0 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20  ->validTZ ){.   
755c0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
755d0 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
755e0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
755f0 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72 72  time to the curr
75600 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74 65  ent time reporte
75610 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a  d by the VFS.*/.
75620 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 44  static void setD
75630 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74  ateTimeToCurrent
75640 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
75650 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65 54   *context, DateT
75660 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c  ime *p){.  doubl
75670 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  e r;.  sqlite3 *
75680 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
75690 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
756a0 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74  ontext);.  sqlit
756b0 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28  e3OsCurrentTime(
756c0 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20  db->pVfs, &r);. 
756d0 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74   p->iJD = (sqlit
756e0 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30  e3_int64)(r*8640
756f0 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20  0000.0 + 0.5);. 
75700 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b   p->validJD = 1;
75710 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
75720 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20 67  t to parse the g
75730 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f  iven string into
75740 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75   a Julian Day Nu
75750 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a  mber.  Return.**
75760 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
75770 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rrors..**.** The
75780 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61   following are a
75790 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73 20  cceptable forms 
757a0 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73 74  for the input st
757b0 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ring:.**.**     
757c0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
757d0 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a  M:SS.FFF  +/-HH:
757e0 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44 2e  MM.**      DDDD.
757f0 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a  DD .**      now.
75800 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69 72  **.** In the fir
75810 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d  st form, the +/-
75820 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73 20  HH:MM is always 
75830 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66  optional.  The f
75840 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63  ractional.** sec
75850 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20 28  onds extension (
75860 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20 6f  the ".FFF") is o
75870 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65  ptional.  The se
75880 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a  conds portion.**
75890 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73 20   (":SS.FFF") is 
758a0 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65 61  option.  The yea
758b0 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20 62  r and date can b
758c0 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e  e omitted as lon
758d0 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73  g.** as there is
758e0 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e 20   a time string. 
758f0 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e 67   The time string
75900 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20   can be omitted 
75910 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68  as long.** as th
75920 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61 6e  ere is a year an
75930 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  d date..*/.stati
75940 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65 4f  c int parseDateO
75950 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65 33  rTime(.  sqlite3
75960 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
75970 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t, .  const char
75980 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74 65   *zDate, .  Date
75990 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74  Time *p.){.  int
759a0 20 69 73 52 65 61 6c 4e 75 6d 3b 20 20 20 20 2f   isRealNum;    /
759b0 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 73 71  * Return from sq
759c0 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 29 2e  lite3IsNumber().
759d0 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20    Not used */.  
759e0 69 66 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44  if( parseYyyyMmD
759f0 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b  d(zDate,p)==0 ){
75a00 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
75a10 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72 73 65   }else if( parse
75a20 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29  HhMmSs(zDate, p)
75a30 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
75a40 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 0;.  }else if(
75a50 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
75a60 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29  zDate,"now")==0)
75a70 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 69 6d  {.    setDateTim
75a80 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65  eToCurrent(conte
75a90 78 74 2c 20 70 29 3b 0a 20 20 20 20 72 65 74 75  xt, p);.    retu
75aa0 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 0;.  }else if
75ab0 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65  ( sqlite3IsNumbe
75ac0 72 28 7a 44 61 74 65 2c 20 26 69 73 52 65 61 6c  r(zDate, &isReal
75ad0 4e 75 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Num, SQLITE_UTF8
75ae0 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20  ) ){.    double 
75af0 72 3b 0a 20 20 20 20 67 65 74 56 61 6c 75 65 28  r;.    getValue(
75b00 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 20 20 20  zDate, &r);.    
75b10 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65  p->iJD = (sqlite
75b20 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30  3_int64)(r*86400
75b30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20  000.0 + 0.5);.  
75b40 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31    p->validJD = 1
75b50 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
75b60 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
75b70 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
75b80 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68   the Year, Month
75b90 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d 20 74  , and Day from t
75ba0 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  he julian day nu
75bb0 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mber..*/.static 
75bc0 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44 28  void computeYMD(
75bd0 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
75be0 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20  int Z, A, B, C, 
75bf0 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 28 20  D, E, X1;.  if( 
75c00 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 72 65  p->validYMD ) re
75c10 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e  turn;.  if( !p->
75c20 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 70  validJD ){.    p
75c30 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20  ->Y = 2000;.    
75c40 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d  p->M = 1;.    p-
75c50 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  >D = 1;.  }else{
75c60 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29 28 28  .    Z = (int)((
75c70 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30  p->iJD + 4320000
75c80 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20  0)/86400000);.  
75c90 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a 20 2d    A = (int)((Z -
75ca0 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33 36 35   1867216.25)/365
75cb0 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 20 3d 20  24.25);.    A = 
75cc0 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41 2f 34  Z + 1 + A - (A/4
75cd0 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b 20 31  );.    B = A + 1
75ce0 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28 69 6e  524;.    C = (in
75cf0 74 29 28 28 42 20 2d 20 31 32 32 2e 31 29 2f 33  t)((B - 122.1)/3
75d00 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 20 3d 20  65.25);.    D = 
75d10 28 33 36 35 32 35 2a 43 29 2f 31 30 30 3b 0a 20  (36525*C)/100;. 
75d20 20 20 20 45 20 3d 20 28 69 6e 74 29 28 28 42 2d     E = (int)((B-
75d30 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a 20 20 20  D)/30.6001);.   
75d40 20 58 31 20 3d 20 28 69 6e 74 29 28 33 30 2e 36   X1 = (int)(30.6
75d50 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 2d 3e 44  001*E);.    p->D
75d60 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20   = B - D - X1;. 
75d70 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f     p->M = E<14 ?
75d80 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20   E-1 : E-13;.   
75d90 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f   p->Y = p->M>2 ?
75da0 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20 2d 20   C - 4716 : C - 
75db0 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76  4715;.  }.  p->v
75dc0 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a  alidYMD = 1;.}..
75dd0 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
75de0 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20  e Hour, Minute, 
75df0 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d  and Seconds from
75e00 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20   the julian day 
75e10 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
75e20 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d  c void computeHM
75e30 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  S(DateTime *p){.
75e40 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 28 20 70    int s;.  if( p
75e50 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 72 65 74  ->validHMS ) ret
75e60 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44  urn;.  computeJD
75e70 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29  (p);.  s = (int)
75e80 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30  ((p->iJD + 43200
75e90 30 30 30 29 20 25 20 38 36 34 30 30 30 30 30 29  000) % 86400000)
75ea0 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30  ;.  p->s = s/100
75eb0 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29  0.0;.  s = (int)
75ec0 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20  p->s;.  p->s -= 
75ed0 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36  s;.  p->h = s/36
75ee0 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a  00;.  s -= p->h*
75ef0 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73  3600;.  p->m = s
75f00 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73  /60;.  p->s += s
75f10 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d   - p->m*60;.  p-
75f20 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d  >validHMS = 1;.}
75f30 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
75f40 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 4d 53  both YMD and HMS
75f50 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
75f60 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 44  computeYMD_HMS(D
75f70 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63  ateTime *p){.  c
75f80 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20  omputeYMD(p);.  
75f90 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d  computeHMS(p);.}
75fa0 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
75fb0 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e  e YMD and HMS an
75fc0 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74  d the TZ.*/.stat
75fd0 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59 4d 44  ic void clearYMD
75fe0 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 6d 65  _HMS_TZ(DateTime
75ff0 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64   *p){.  p->valid
76000 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  YMD = 0;.  p->va
76010 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d  lidHMS = 0;.  p-
76020 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a  >validTZ = 0;.}.
76030 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
76040 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f  OMIT_LOCALTIME./
76050 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65  *.** Compute the
76060 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e 20   difference (in 
76070 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a 2a 2a  milliseconds).**
76080 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69   between localti
76090 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e  me and UTC (a.k.
760a0 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74  a. GMT).** for t
760b0 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20 70 20  he time value p 
760c0 77 68 65 72 65 20 70 20 69 73 20 69 6e 20 55 54  where p is in UT
760d0 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c  C..*/.static sql
760e0 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c  ite3_int64 local
760f0 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74 65 54  timeOffset(DateT
76100 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 65 54  ime *p){.  DateT
76110 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65  ime x, y;.  time
76120 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a  _t t;.  x = *p;.
76130 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53    computeYMD_HMS
76140 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c  (&x);.  if( x.Y<
76150 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33  1971 || x.Y>=203
76160 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32  8 ){.    x.Y = 2
76170 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31  000;.    x.M = 1
76180 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20  ;.    x.D = 1;. 
76190 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20     x.h = 0;.    
761a0 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73  x.m = 0;.    x.s
761b0 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65   = 0.0;.  } else
761c0 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d 20 28   {.    int s = (
761d0 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b  int)(x.s + 0.5);
761e0 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20  .    x.s = s;.  
761f0 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20  }.  x.tz = 0;.  
76200 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  x.validJD = 0;. 
76210 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a   computeJD(&x);.
76220 20 20 74 20 3d 20 78 2e 69 4a 44 2f 31 30 30 30    t = x.iJD/1000
76230 20 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34   - 21086676*(i64
76240 29 31 30 30 30 30 3b 0a 23 69 66 64 65 66 20 48  )10000;.#ifdef H
76250 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a  AVE_LOCALTIME_R.
76260 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
76270 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f  m sLocal;.    lo
76280 63 61 6c 74 69 6d 65 5f 72 28 26 74 2c 20 26 73  caltime_r(&t, &s
76290 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 79 2e 59 20  Local);.    y.Y 
762a0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72  = sLocal.tm_year
762b0 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d   + 1900;.    y.M
762c0 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e   = sLocal.tm_mon
762d0 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20   + 1;.    y.D = 
762e0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a  sLocal.tm_mday;.
762f0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c      y.h = sLocal
76300 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e  .tm_hour;.    y.
76310 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69  m = sLocal.tm_mi
76320 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f  n;.    y.s = sLo
76330 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a  cal.tm_sec;.  }.
76340 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41  #elif defined(HA
76350 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 0a  VE_LOCALTIME_S).
76360 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
76370 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f  m sLocal;.    lo
76380 63 61 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61  caltime_s(&sLoca
76390 6c 2c 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20  l, &t);.    y.Y 
763a0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72  = sLocal.tm_year
763b0 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d   + 1900;.    y.M
763c0 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e   = sLocal.tm_mon
763d0 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20   + 1;.    y.D = 
763e0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a  sLocal.tm_mday;.
763f0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c      y.h = sLocal
76400 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e  .tm_hour;.    y.
76410 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69  m = sLocal.tm_mi
76420 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f  n;.    y.s = sLo
76430 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a  cal.tm_sec;.  }.
76440 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74  #else.  {.    st
76450 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20  ruct tm *pTm;.  
76460 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
76470 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74  enter(sqlite3Mut
76480 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
76490 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
764a0 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20  ER));.    pTm = 
764b0 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20  localtime(&t);. 
764c0 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d     y.Y = pTm->tm
764d0 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20  _year + 1900;.  
764e0 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f    y.M = pTm->tm_
764f0 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44  mon + 1;.    y.D
76500 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b   = pTm->tm_mday;
76510 0a 20 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e  .    y.h = pTm->
76520 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d  tm_hour;.    y.m
76530 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a   = pTm->tm_min;.
76540 20 20 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74      y.s = pTm->t
76550 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74  m_sec;.    sqlit
76560 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
76570 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
76580 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
76590 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20  ATIC_MASTER));. 
765a0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61   }.#endif.  y.va
765b0 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e  lidYMD = 1;.  y.
765c0 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20  validHMS = 1;.  
765d0 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  y.validJD = 0;. 
765e0 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a   y.validTZ = 0;.
765f0 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b    computeJD(&y);
76600 0a 20 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20  .  return y.iJD 
76610 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69  - x.iJD;.}.#endi
76620 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
76630 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f  _LOCALTIME */../
76640 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d  *.** Process a m
76650 6f 64 69 66 69 65 72 20 74 6f 20 61 20 64 61 74  odifier to a dat
76660 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54  e-time stamp.  T
76670 68 65 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65  he modifiers are
76680 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  .** as follows:.
76690 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61  **.**     NNN da
766a0 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f  ys.**     NNN ho
766b0 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d  urs.**     NNN m
766c0 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e  inutes.**     NN
766d0 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a  N.NNNN seconds.*
766e0 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73  *     NNN months
766f0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72  .**     NNN year
76700 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f  s.**     start o
76710 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73  f month.**     s
76720 74 61 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20  tart of year.** 
76730 20 20 20 20 73 74 61 72 74 20 6f 66 20 77 65 65      start of wee
76740 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f  k.**     start o
76750 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65  f day.**     wee
76760 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e  kday N.**     un
76770 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c  ixepoch.**     l
76780 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20  ocaltime.**     
76790 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  utc.**.** Return
767a0 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e   0 on success an
767b0 64 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20  d 1 if there is 
767c0 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f  any kind of erro
767d0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
767e0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63   parseModifier(c
767f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c  onst char *zMod,
76800 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20   DateTime *p){. 
76810 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69   int rc = 1;.  i
76820 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72  nt n;.  double r
76830 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75  ;.  char *z, zBu
76840 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75  f[30];.  z = zBu
76850 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c  f;.  for(n=0; n<
76860 41 72 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d  ArraySize(zBuf)-
76870 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b  1 && zMod[n]; n+
76880 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 28  +){.    z[n] = (
76890 63 68 61 72 29 73 71 6c 69 74 65 33 55 70 70 65  char)sqlite3Uppe
768a0 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f  rToLower[(u8)zMo
768b0 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e  d[n]];.  }.  z[n
768c0 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28  ] = 0;.  switch(
768d0 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66   z[0] ){.#ifndef
768e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43   SQLITE_OMIT_LOC
768f0 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20  ALTIME.    case 
76900 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  'l': {.      /* 
76910 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20     localtime.   
76920 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41     **.      ** A
76930 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72  ssuming the curr
76940 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69  ent time value i
76950 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d  s UTC (a.k.a. GM
76960 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a  T), shift it to.
76970 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f        ** show lo
76980 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20  cal time..      
76990 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72  */.      if( str
769a0 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d  cmp(z, "localtim
769b0 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  e")==0 ){.      
769c0 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
769d0 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b          p->iJD +
769e0 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65  = localtimeOffse
769f0 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c  t(p);.        cl
76a00 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
76a10 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
76a20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
76a30 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
76a40 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 75 27  dif.    case 'u'
76a50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20  : {.      /*.   
76a60 20 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f     **    unixepo
76a70 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  ch.      **.    
76a80 20 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63    ** Treat the c
76a90 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
76aa0 70 2d 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75  p->iJD as the nu
76ab0 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a  mber of.      **
76ac0 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31   seconds since 1
76ad0 39 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f  970.  Convert to
76ae0 20 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64   a real julian d
76af0 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20  ay number..     
76b00 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74   */.      if( st
76b10 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f  rcmp(z, "unixepo
76b20 63 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61  ch")==0 && p->va
76b30 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20  lidJD ){.       
76b40 20 70 2d 3e 69 4a 44 20 3d 20 28 70 2d 3e 69 4a   p->iJD = (p->iJ
76b50 44 20 2b 20 34 33 32 30 30 29 2f 38 36 34 30 30  D + 43200)/86400
76b60 20 2b 20 32 31 30 38 36 36 37 36 2a 28 69 36 34   + 21086676*(i64
76b70 29 31 30 30 30 30 30 30 30 3b 0a 20 20 20 20 20  )10000000;.     
76b80 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f     clearYMD_HMS_
76b90 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72  TZ(p);.        r
76ba0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23  c = 0;.      }.#
76bb0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
76bc0 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20  IT_LOCALTIME.   
76bd0 20 20 20 65 6c 73 65 20 69 66 28 20 73 74 72 63     else if( strc
76be0 6d 70 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20  mp(z, "utc")==0 
76bf0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
76c00 65 33 5f 69 6e 74 36 34 20 63 31 3b 0a 20 20 20  e3_int64 c1;.   
76c10 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70       computeJD(p
76c20 29 3b 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20  );.        c1 = 
76c30 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28  localtimeOffset(
76c40 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  p);.        p->i
76c50 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20  JD -= c1;.      
76c60 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54    clearYMD_HMS_T
76c70 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  Z(p);.        p-
76c80 3e 69 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63  >iJD += c1 - loc
76c90 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b  altimeOffset(p);
76ca0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
76cb0 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
76cc0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
76cd0 20 7d 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a   }.    case 'w':
76ce0 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20   {.      /*.    
76cf0 20 20 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20    **    weekday 
76d00 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  N.      **.     
76d10 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74   ** Move the dat
76d20 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69  e to the same ti
76d30 6d 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f  me on the next o
76d40 63 63 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20  ccurrence of.   
76d50 20 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20     ** weekday N 
76d60 77 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c  where 0==Sunday,
76d70 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20   1==Monday, and 
76d80 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68  so forth.  If th
76d90 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20  e.      ** date 
76da0 69 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68  is already on th
76db0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 77 65  e appropriate we
76dc0 65 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20 61  ekday, this is a
76dd0 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f   no-op..      */
76de0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63  .      if( strnc
76df0 6d 70 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22  mp(z, "weekday "
76e00 2c 20 38 29 3d 3d 30 20 26 26 20 67 65 74 56 61  , 8)==0 && getVa
76e10 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a  lue(&z[8],&r)>0.
76e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76e30 20 26 26 20 28 6e 3d 28 69 6e 74 29 72 29 3d 3d   && (n=(int)r)==
76e40 72 20 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37  r && n>=0 && r<7
76e50 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
76e60 74 65 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20  te3_int64 Z;.   
76e70 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
76e80 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
76e90 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
76ea0 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64          p->valid
76eb0 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  JD = 0;.        
76ec0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
76ed0 20 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69        Z = ((p->i
76ee0 4a 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f  JD + 129600000)/
76ef0 38 36 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20  86400000) % 7;. 
76f00 20 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29         if( Z>n )
76f10 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20   Z -= 7;.       
76f20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20   p->iJD += (n - 
76f30 5a 29 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20  Z)*86400000;.   
76f40 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d       clearYMD_HM
76f50 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20  S_TZ(p);.       
76f60 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d   rc = 0;.      }
76f70 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
76f80 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27    }.    case 's'
76f90 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20  : {.      /*.   
76fa0 20 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f     **    start o
76fb0 66 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a  f TTTTT.      **
76fc0 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74  .      ** Move t
76fd0 68 65 20 64 61 74 65 20 62 61 63 6b 77 61 72 64  he date backward
76fe0 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  s to the beginni
76ff0 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ng of the curren
77000 74 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20  t day,.      ** 
77010 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72  or month or year
77020 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
77030 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20   if( strncmp(z, 
77040 22 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21  "start of ", 9)!
77050 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
77060 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20    z += 9;.      
77070 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20  computeYMD(p);. 
77080 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53       p->validHMS
77090 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68   = 1;.      p->h
770a0 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20   = p->m = 0;.   
770b0 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20     p->s = 0.0;. 
770c0 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20       p->validTZ 
770d0 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  = 0;.      p->va
770e0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20  lidJD = 0;.     
770f0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d   if( strcmp(z,"m
77100 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20  onth")==0 ){.   
77110 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20       p->D = 1;. 
77120 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
77130 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73       }else if( s
77140 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d  trcmp(z,"year")=
77150 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  =0 ){.        co
77160 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20  mputeYMD(p);.   
77170 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20       p->M = 1;. 
77180 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b         p->D = 1;
77190 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
771a0 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
771b0 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29   strcmp(z,"day")
771c0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
771d0 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 0;.      }. 
771e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
771f0 7d 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a  }.    case '+':.
77200 20 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20      case '-':.  
77210 20 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20    case '0':.    
77220 63 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61  case '1':.    ca
77230 73 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65  se '2':.    case
77240 20 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '3':.    case '
77250 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27  4':.    case '5'
77260 3a 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a  :.    case '6':.
77270 20 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20      case '7':.  
77280 20 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20    case '8':.    
77290 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20  case '9': {.    
772a0 20 20 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65    double rRounde
772b0 72 3b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74  r;.      n = get
772c0 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20  Value(z, &r);.  
772d0 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31      assert( n>=1
772e0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b   );.      if( z[
772f0 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20  n]==':' ){.     
77300 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72     /* A modifier
77310 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c   of the form (+|
77320 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61  -)HH:MM:SS.FFF a
77330 64 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74  dds (or subtract
77340 73 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  s) the.        *
77350 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62  * specified numb
77360 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e  er of hours, min
77370 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61  utes, seconds, a
77380 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65  nd fractional se
77390 63 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a  conds.        **
773a0 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54   to the time.  T
773b0 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65  he ".FFF" may be
773c0 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22   omitted.  The "
773d0 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a  :SS.FFF" may be.
773e0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74          ** omitt
773f0 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ed..        */. 
77400 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
77410 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20  r *z2 = z;.     
77420 20 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a     DateTime tx;.
77430 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
77440 69 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20  int64 day;.     
77450 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
77460 73 64 69 67 69 74 28 2a 7a 32 29 20 29 20 7a 32  sdigit(*z2) ) z2
77470 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  ++;.        mems
77480 65 74 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f  et(&tx, 0, sizeo
77490 66 28 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20  f(tx));.        
774a0 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28  if( parseHhMmSs(
774b0 7a 32 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b  z2, &tx) ) break
774c0 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
774d0 65 4a 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20  eJD(&tx);.      
774e0 20 20 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30    tx.iJD -= 4320
774f0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61  0000;.        da
77500 79 20 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30  y = tx.iJD/86400
77510 30 30 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e  000;.        tx.
77520 69 4a 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30  iJD -= day*86400
77530 30 30 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  000;.        if(
77540 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e   z[0]=='-' ) tx.
77550 69 4a 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20  iJD = -tx.iJD;. 
77560 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
77570 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65  (p);.        cle
77580 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
77590 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
775a0 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20  += tx.iJD;.     
775b0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
775c0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
775d0 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a  }.      z += n;.
775e0 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
775f0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20  ite3Isspace(*z) 
77600 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d  ) z++;.      n =
77610 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
77620 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  (z);.      if( n
77630 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65  >10 || n<3 ) bre
77640 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  ak;.      if( z[
77650 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e  n-1]=='s' ){ z[n
77660 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a  -1] = 0; n--; }.
77670 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
77680 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30  p);.      rc = 0
77690 3b 0a 20 20 20 20 20 20 72 52 6f 75 6e 64 65 72  ;.      rRounder
776a0 20 3d 20 72 3c 30 20 3f 20 2d 30 2e 35 20 3a 20   = r<0 ? -0.5 : 
776b0 2b 30 2e 35 3b 0a 20 20 20 20 20 20 69 66 28 20  +0.5;.      if( 
776c0 6e 3d 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a  n==3 && strcmp(z
776d0 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20  ,"day")==0 ){.  
776e0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
776f0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
77700 72 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72  r*86400000.0 + r
77710 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20  Rounder);.      
77720 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26  }else if( n==4 &
77730 26 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72  & strcmp(z,"hour
77740 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
77750 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
77760 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36  te3_int64)(r*(86
77770 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b  400000.0/24.0) +
77780 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20   rRounder);.    
77790 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36    }else if( n==6
777a0 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69   && strcmp(z,"mi
777b0 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  nute")==0 ){.   
777c0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
777d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
777e0 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34  *(86400000.0/(24
777f0 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75  .0*60.0)) + rRou
77800 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
77810 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73  se if( n==6 && s
77820 74 72 63 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22  trcmp(z,"second"
77830 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
77840 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
77850 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34  e3_int64)(r*(864
77860 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30  00000.0/(24.0*60
77870 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75  .0*60.0)) + rRou
77880 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
77890 73 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73  se if( n==5 && s
778a0 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29  trcmp(z,"month")
778b0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
778c0 6e 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20  nt x, y;.       
778d0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
778e0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d  p);.        p->M
778f0 20 2b 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20   += (int)r;.    
77900 20 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f      x = p->M>0 ?
77910 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28   (p->M-1)/12 : (
77920 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20  p->M-12)/12;.   
77930 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a       p->Y += x;.
77940 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20          p->M -= 
77950 78 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d  x*12;.        p-
77960 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20  >validJD = 0;.  
77970 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
77980 70 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20  p);.        y = 
77990 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20  (int)r;.        
779a0 69 66 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20  if( y!=r ){.    
779b0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20        p->iJD += 
779c0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
779d0 28 72 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34  (r - y)*30.0*864
779e0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64  00000.0 + rRound
779f0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  er);.        }. 
77a00 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
77a10 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c  ==4 && strcmp(z,
77a20 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20  "year")==0 ){.  
77a30 20 20 20 20 20 20 69 6e 74 20 79 20 3d 20 28 69        int y = (i
77a40 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 63 6f  nt)r;.        co
77a50 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b  mputeYMD_HMS(p);
77a60 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d  .        p->Y +=
77a70 20 79 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76   y;.        p->v
77a80 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20  alidJD = 0;.    
77a90 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
77aa0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 21  ;.        if( y!
77ab0 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =r ){.          
77ac0 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
77ad0 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 79  e3_int64)((r - y
77ae0 29 2a 33 36 35 2e 30 2a 38 36 34 30 30 30 30 30  )*365.0*86400000
77af0 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  .0 + rRounder);.
77b00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
77b10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
77b20 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 1;.      }. 
77b30 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d       clearYMD_HM
77b40 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62  S_TZ(p);.      b
77b50 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
77b60 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
77b70 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
77b80 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
77b90 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20  ../*.** Process 
77ba0 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72  time function ar
77bb0 67 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30  guments.  argv[0
77bc0 5d 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65  ] is a date-time
77bd0 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b   stamp..** argv[
77be0 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67  1] and following
77bf0 20 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20   are modifiers. 
77c00 20 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20   Parse them all 
77c10 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65  and write.** the
77c20 20 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20   resulting time 
77c30 69 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d  into the DateTim
77c40 65 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20  e structure p.  
77c50 52 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73  Return 0.** on s
77c60 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20  uccess and 1 if 
77c70 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72  there are any er
77c80 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rors..**.** If t
77c90 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61  here are zero pa
77ca0 72 61 6d 65 74 65 72 73 20 28 69 66 20 65 76 65  rameters (if eve
77cb0 6e 20 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64  n argv[0] is und
77cc0 65 66 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20  efined).** then 
77cd0 61 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74  assume a default
77ce0 20 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20   value of "now" 
77cf0 66 6f 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a  for argv[0]..*/.
77d00 73 74 61 74 69 63 20 69 6e 74 20 69 73 44 61 74  static int isDat
77d10 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  e(.  sqlite3_con
77d20 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
77d30 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73    int argc, .  s
77d40 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
77d50 72 67 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65  rgv, .  DateTime
77d60 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *p.){.  int i;.
77d70 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
77d80 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20   char *z;.  int 
77d90 65 54 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28  eType;.  memset(
77da0 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29  p, 0, sizeof(*p)
77db0 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30  );.  if( argc==0
77dc0 20 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54   ){.    setDateT
77dd0 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e  imeToCurrent(con
77de0 74 65 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73  text, p);.  }els
77df0 65 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73  e if( (eType = s
77e00 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
77e10 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c  e(argv[0]))==SQL
77e20 49 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20  ITE_FLOAT.      
77e30 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
77e40 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e  eType==SQLITE_IN
77e50 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e  TEGER ){.    p->
77e60 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  iJD = (sqlite3_i
77e70 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61  nt64)(sqlite3_va
77e80 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
77e90 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b  0])*86400000.0 +
77ea0 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61   0.5);.    p->va
77eb0 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c  lidJD = 1;.  }el
77ec0 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  se{.    z = sqli
77ed0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
77ee0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[0]);.    if(
77ef0 20 21 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65   !z || parseDate
77f00 4f 72 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20  OrTime(context, 
77f10 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a  (char*)z, p) ){.
77f20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
77f30 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
77f40 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b  i=1; i<argc; i++
77f50 29 7b 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20  ){.    if( (z = 
77f60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
77f70 78 74 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20  xt(argv[i]))==0 
77f80 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72  || parseModifier
77f90 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b  ((char*)z, p) ){
77fa0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
77fb0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
77fc0 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
77fd0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
77fe0 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
77ff0 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 64 61  t the various da
78000 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63  te and time func
78010 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69  tions.** of SQLi
78020 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20  te..*/../*.**   
78030 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45   julianday( TIME
78040 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
78050 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
78060 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  urn the julian d
78070 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ay number of the
78080 20 64 61 74 65 20 73 70 65 63 69 66 69 65 64 20   date specified 
78090 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73  in the arguments
780a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
780b0 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20  juliandayFunc(. 
780c0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
780d0 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
780e0 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
780f0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
78100 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20  .  DateTime x;. 
78110 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74   if( isDate(cont
78120 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  ext, argc, argv,
78130 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63   &x)==0 ){.    c
78140 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20  omputeJD(&x);.  
78150 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
78160 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c  _double(context,
78170 20 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e   x.iJD/86400000.
78180 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  0);.  }.}../*.**
78190 20 20 20 20 64 61 74 65 74 69 6d 65 28 20 54 49      datetime( TI
781a0 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
781b0 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
781c0 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44  eturn YYYY-MM-DD
781d0 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61   HH:MM:SS.*/.sta
781e0 74 69 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d  tic void datetim
781f0 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
78200 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
78210 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
78220 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
78230 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54  *argv.){.  DateT
78240 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44  ime x;.  if( isD
78250 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  ate(context, arg
78260 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20  c, argv, &x)==0 
78270 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66  ){.    char zBuf
78280 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75  [100];.    compu
78290 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20  teYMD_HMS(&x);. 
782a0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
782b0 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29  ntf(sizeof(zBuf)
782c0 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30  , zBuf, "%04d-%0
782d0 32 64 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32  2d-%02d %02d:%02
782e0 64 3a 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20  d:%02d",.       
782f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 78 2e                x.
78300 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68  Y, x.M, x.D, x.h
78310 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73  , x.m, (int)(x.s
78320 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ));.    sqlite3_
78330 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
78340 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53  ext, zBuf, -1, S
78350 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
78360 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20  ;.  }.}../*.**  
78370 20 20 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49    time( TIMESTRI
78380 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e  NG, MOD, MOD, ..
78390 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  .).**.** Return 
783a0 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74  HH:MM:SS.*/.stat
783b0 69 63 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63  ic void timeFunc
783c0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
783d0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
783e0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
783f0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
78400 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
78410 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63  ;.  if( isDate(c
78420 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
78430 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20  gv, &x)==0 ){.  
78440 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
78450 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53  ;.    computeHMS
78460 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (&x);.    sqlite
78470 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
78480 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22  f(zBuf), zBuf, "
78490 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c  %02d:%02d:%02d",
784a0 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29   x.h, x.m, (int)
784b0 78 2e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  x.s);.    sqlite
784c0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
784d0 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c  ntext, zBuf, -1,
784e0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
784f0 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  T);.  }.}../*.**
78500 20 20 20 20 64 61 74 65 28 20 54 49 4d 45 53 54      date( TIMEST
78510 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
78520 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
78530 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a  n YYYY-MM-DD.*/.
78540 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 65  static void date
78550 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
78560 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
78570 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
78580 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
78590 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69  argv.){.  DateTi
785a0 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61  me x;.  if( isDa
785b0 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  te(context, argc
785c0 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29  , argv, &x)==0 )
785d0 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b  {.    char zBuf[
785e0 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74  100];.    comput
785f0 65 59 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71  eYMD(&x);.    sq
78600 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
78610 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
78620 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30  f, "%04d-%02d-%0
78630 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78  2d", x.Y, x.M, x
78640 2e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .D);.    sqlite3
78650 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
78660 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20  text, zBuf, -1, 
78670 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
78680 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
78690 20 20 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52     strftime( FOR
786a0 4d 41 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c  MAT, TIMESTRING,
786b0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a   MOD, MOD, ...).
786c0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73  **.** Return a s
786d0 74 72 69 6e 67 20 64 65 73 63 72 69 62 65 64 20  tring described 
786e0 62 79 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76  by FORMAT.  Conv
786f0 65 72 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f  ersions as follo
78700 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20  ws:.**.**   %d  
78710 64 61 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20  day of month.** 
78720 20 20 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f    %f  ** fractio
78730 6e 61 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e  nal seconds  SS.
78740 53 53 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75  SSS.**   %H  hou
78750 72 20 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20  r 00-24.**   %j 
78760 20 64 61 79 20 6f 66 20 79 65 61 72 20 30 30 30   day of year 000
78770 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a  -366.**   %J  **
78780 20 4a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62   Julian day numb
78790 65 72 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74  er.**   %m  mont
787a0 68 20 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20  h 01-12.**   %M 
787b0 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a   minute 00-59.**
787c0 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73     %s  seconds s
787d0 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a  ince 1970-01-01.
787e0 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73  **   %S  seconds
787f0 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20   00-59.**   %w  
78800 64 61 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20  day of week 0-6 
78810 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20   sunday==0.**   
78820 25 57 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72  %W  week of year
78830 20 30 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20   00-53.**   %Y  
78840 79 65 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a  year 0000-9999.*
78850 2a 20 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61  *   %%  %.*/.sta
78860 74 69 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d  tic void strftim
78870 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
78880 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
78890 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
788a0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
788b0 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54  *argv.){.  DateT
788c0 69 6d 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a  ime x;.  u64 n;.
788d0 20 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20    size_t i,j;.  
788e0 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74  char *z;.  sqlit
788f0 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20  e3 *db;.  const 
78900 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28 63 6f  char *zFmt = (co
78910 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65  nst char*)sqlite
78920 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
78930 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 20 7a 42  v[0]);.  char zB
78940 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 28 20 7a  uf[100];.  if( z
78950 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 61 74 65  Fmt==0 || isDate
78960 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2d 31  (context, argc-1
78970 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20 29 20  , argv+1, &x) ) 
78980 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 73  return;.  db = s
78990 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
789a0 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
789b0 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d  );.  for(i=0, n=
789c0 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c  1; zFmt[i]; i++,
789d0 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a   n++){.    if( z
789e0 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 7b 0a 20  Fmt[i]=='%' ){. 
789f0 20 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d       switch( zFm
78a00 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20  t[i+1] ){.      
78a10 20 20 63 61 73 65 20 27 64 27 3a 0a 20 20 20 20    case 'd':.    
78a20 20 20 20 20 63 61 73 65 20 27 48 27 3a 0a 20 20      case 'H':.  
78a30 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a 0a        case 'm':.
78a40 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d 27          case 'M'
78a50 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
78a60 53 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  S':.        case
78a70 20 27 57 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'W':.          
78a80 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 2f  n++;.          /
78a90 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20  * fall thru */. 
78aa0 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a         case 'w':
78ab0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 25  .        case '%
78ac0 27 3a 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  ':.          bre
78ad0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
78ae0 20 27 66 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'f':.          
78af0 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20  n += 8;.        
78b00 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
78b10 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 20 20 20   case 'j':.     
78b20 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20       n += 3;.   
78b30 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
78b40 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 0a        case 'Y':.
78b50 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38            n += 8
78b60 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
78b70 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
78b80 27 73 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  's':.        cas
78b90 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 20 20 20  e 'J':.         
78ba0 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 20 20 20   n += 50;.      
78bb0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
78bc0 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
78bd0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f        return;  /
78be0 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e  * ERROR.  return
78bf0 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20   a NULL */.     
78c00 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20   }.      i++;.  
78c10 20 20 7d 0a 20 20 7d 0a 20 20 74 65 73 74 63 61    }.  }.  testca
78c20 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42  se( n==sizeof(zB
78c30 75 66 29 2d 31 20 29 3b 0a 20 20 74 65 73 74 63  uf)-1 );.  testc
78c40 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a  ase( n==sizeof(z
78c50 42 75 66 29 20 29 3b 0a 20 20 74 65 73 74 63 61  Buf) );.  testca
78c60 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e  se( n==(u64)db->
78c70 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
78c80 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b  MIT_LENGTH]+1 );
78c90 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d  .  testcase( n==
78ca0 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b  (u64)db->aLimit[
78cb0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
78cc0 47 54 48 5d 20 29 3b 0a 20 20 69 66 28 20 6e 3c  GTH] );.  if( n<
78cd0 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 7b 0a  sizeof(zBuf) ){.
78ce0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
78cf0 7d 65 6c 73 65 20 69 66 28 20 6e 3e 28 75 36 34  }else if( n>(u64
78d00 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  )db->aLimit[SQLI
78d10 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
78d20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
78d30 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
78d40 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  big(context);.  
78d50 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73    return;.  }els
78d60 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  e{.    z = sqlit
78d70 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
78d80 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 20 20 69  , (int)n);.    i
78d90 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( z==0 ){.     
78da0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
78db0 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74  error_nomem(cont
78dc0 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ext);.      retu
78dd0 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
78de0 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20  computeJD(&x);. 
78df0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
78e00 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30  &x);.  for(i=j=0
78e10 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b  ; zFmt[i]; i++){
78e20 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d  .    if( zFmt[i]
78e30 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a  !='%' ){.      z
78e40 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b  [j++] = zFmt[i];
78e50 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
78e60 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69    i++;.      swi
78e70 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a  tch( zFmt[i] ){.
78e80 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27          case 'd'
78e90 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
78ea0 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30  ntf(3, &z[j],"%0
78eb0 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20  2d",x.D); j+=2; 
78ec0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
78ed0 61 73 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20  ase 'f': {.     
78ee0 20 20 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20       double s = 
78ef0 78 2e 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69  x.s;.          i
78f00 66 28 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20  f( s>59.999 ) s 
78f10 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20  = 59.999;.      
78f20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
78f30 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(7, &z[j],"%
78f40 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20  06.3f", s);.    
78f50 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74        j += sqlit
78f60 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d  e3Strlen30(&z[j]
78f70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
78f80 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
78f90 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 20        case 'H': 
78fa0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
78fb0 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64  f(3, &z[j],"%02d
78fc0 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72  ",x.h); j+=2; br
78fd0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
78fe0 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74  e 'W': /* Fall t
78ff0 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63  hru */.        c
79000 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20  ase 'j': {.     
79010 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20       int nDay;  
79020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
79030 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e  mber of days sin
79040 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79 65  ce 1st day of ye
79050 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ar */.          
79060 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a  DateTime y = x;.
79070 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69            y.vali
79080 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dJD = 0;.       
79090 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20     y.M = 1;.    
790a0 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20        y.D = 1;. 
790b0 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65           compute
790c0 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20  JD(&y);.        
790d0 20 20 6e 44 61 79 20 3d 20 28 69 6e 74 29 28 28    nDay = (int)((
790e0 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30  x.iJD-y.iJD+4320
790f0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b  0000)/86400000);
79100 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
79110 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20  Fmt[i]=='W' ){. 
79120 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 77             int w
79130 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79  d;   /* 0=Monday
79140 2c 20 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e  , 1=Tuesday, ...
79150 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20   6=Sunday */.   
79160 20 20 20 20 20 20 20 20 20 77 64 20 3d 20 28 69           wd = (i
79170 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30  nt)(((x.iJD+4320
79180 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 25  0000)/86400000)%
79190 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  7);.            
791a0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
791b0 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  (3, &z[j],"%02d"
791c0 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b  ,(nDay+7-wd)/7);
791d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b  .            j +
791e0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 2;.          }
791f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
79200 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
79210 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33  tf(4, &z[j],"%03
79220 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20  d",nDay+1);.    
79230 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a          j += 3;.
79240 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
79250 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
79260 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
79270 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20  ase 'J': {.     
79280 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
79290 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c  rintf(20, &z[j],
792a0 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 2f 38 36  "%.16g",x.iJD/86
792b0 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 20  400000.0);.     
792c0 20 20 20 20 20 6a 2b 3d 73 71 6c 69 74 65 33 53       j+=sqlite3S
792d0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a  trlen30(&z[j]);.
792e0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
792f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
79300 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71     case 'm':  sq
79310 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
79320 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78  , &z[j],"%02d",x
79330 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b  .M); j+=2; break
79340 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
79350 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  M':  sqlite3_snp
79360 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
79370 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32  %02d",x.m); j+=2
79380 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
79390 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20   case 's': {.   
793a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
793b0 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d  nprintf(30,&z[j]
793c0 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 20 20 20 20  ,"%lld",.       
793d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
793e0 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a 44 2f      (i64)(x.iJD/
793f0 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a  1000 - 21086676*
79400 28 69 36 34 29 31 30 30 30 30 29 29 3b 0a 20 20  (i64)10000));.  
79410 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c          j += sql
79420 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b  ite3Strlen30(&z[
79430 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  j]);.          b
79440 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
79450 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27          case 'S'
79460 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  :  sqlite3_snpri
79470 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32  ntf(3,&z[j],"%02
79480 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b  d",(int)x.s); j+
79490 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
794a0 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20     case 'w': {. 
794b0 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
794c0 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 4a 44  = (char)(((x.iJD
794d0 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30  +129600000)/8640
794e0 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 30 27  0000) % 7) + '0'
794f0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
79500 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
79510 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b       case 'Y': {
79520 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
79530 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a  e3_snprintf(5,&z
79540 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b  [j],"%04d",x.Y);
79550 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65   j+=sqlite3Strle
79560 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20  n30(&z[j]);.    
79570 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
79580 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
79590 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d  efault:   z[j++]
795a0 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20   = '%'; break;. 
795b0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
795c0 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73  .  z[j] = 0;.  s
795d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
795e0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d  xt(context, z, -
795f0 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
79600 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66           z==zBuf
79610 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49   ? SQLITE_TRANSI
79620 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e  ENT : SQLITE_DYN
79630 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  AMIC);.}../*.** 
79640 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a  current_time().*
79650 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
79660 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  on returns the s
79670 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d  ame value as tim
79680 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61  e('now')..*/.sta
79690 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75  tic void ctimeFu
796a0 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
796b0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
796c0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
796d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
796e0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55  *NotUsed2.){.  U
796f0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
79700 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
79710 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28  d2);.  timeFunc(
79720 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a  context, 0, 0);.
79730 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74  }../*.** current
79740 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68  _date().**.** Th
79750 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
79760 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c  rns the same val
79770 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27  ue as date('now'
79780 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
79790 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73  d cdateFunc(.  s
797a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
797b0 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
797c0 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
797d0 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
797e0 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  d2.){.  UNUSED_P
797f0 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
79800 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
79810 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74  dateFunc(context
79820 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , 0, 0);.}../*.*
79830 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74  * current_timest
79840 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73  amp().**.** This
79850 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
79860 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  s the same value
79870 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f   as datetime('no
79880 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  w')..*/.static v
79890 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75  oid ctimestampFu
798a0 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
798b0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
798c0 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
798d0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
798e0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55  *NotUsed2.){.  U
798f0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
79900 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
79910 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46  d2);.  datetimeF
79920 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20  unc(context, 0, 
79930 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  0);.}.#endif /* 
79940 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
79950 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55  OMIT_DATETIME_FU
79960 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20  NCS) */..#ifdef 
79970 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45  SQLITE_OMIT_DATE
79980 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a  TIME_FUNCS./*.**
79990 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   If the library 
799a0 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f  is compiled to o
799b0 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61  mit the full-sca
799c0 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65  le date and time
799d0 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f  .** handling (to
799e0 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62   get a smaller b
799f0 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c  inary), the foll
79a00 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65  owing minimal ve
79a10 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  rsion.** of the 
79a20 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e  functions curren
79a30 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e  t_time(), curren
79a40 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 75 72  t_date() and cur
79a50 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29  rent_timestamp()
79a60 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64  .** are included
79a70 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69   instead. This i
79a80 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c  s to support col
79a90 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  umn declarations
79aa0 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65   that.** include
79ab0 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e   "DEFAULT CURREN
79ac0 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a  T_TIME" etc..**.
79ad0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
79ae0 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72   uses the C-libr
79af0 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69  ary functions ti
79b00 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a  me(), gmtime().*
79b10 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29  * and strftime()
79b20 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72  . The format str
79b30 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73  ing to pass to s
79b40 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75 70  trftime() is sup
79b50 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20  plied.** as the 
79b60 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68  user-data for th
79b70 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  e function..*/.s
79b80 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 72 65  tatic void curre
79b90 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71  ntTimeFunc(.  sq
79ba0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
79bb0 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
79bc0 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
79bd0 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
79be0 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72  time_t t;.  char
79bf0 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61   *zFormat = (cha
79c00 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72  r *)sqlite3_user
79c10 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a  _data(context);.
79c20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
79c30 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68   double rT;.  ch
79c40 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20  ar zBuf[20];..  
79c50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
79c60 28 61 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44  (argc);.  UNUSED
79c70 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 76 29  _PARAMETER(argv)
79c80 3b 0a 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  ;..  db = sqlite
79c90 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
79ca0 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
79cb0 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
79cc0 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26  Time(db->pVfs, &
79cd0 72 54 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  rT);.#ifndef SQL
79ce0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
79cf0 47 5f 50 4f 49 4e 54 0a 20 20 74 20 3d 20 38 36  G_POINT.  t = 86
79d00 34 30 30 2e 30 2a 28 72 54 20 2d 20 32 34 34 30  400.0*(rT - 2440
79d10 35 38 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23 65  587.5) + 0.5;.#e
79d20 6c 73 65 0a 20 20 2f 2a 20 77 69 74 68 6f 75 74  lse.  /* without
79d30 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
79d40 73 75 70 70 6f 72 74 2c 20 72 54 20 77 69 6c 6c  support, rT will
79d50 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61   have.  ** alrea
79d60 64 79 20 6c 6f 73 74 20 66 72 61 63 74 69 6f 6e  dy lost fraction
79d70 61 6c 20 64 61 79 20 70 72 65 63 69 73 69 6f 6e  al day precision
79d80 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d 20 38 36 34  ..  */.  t = 864
79d90 30 30 20 2a 20 28 72 54 20 2d 20 32 34 34 30 35  00 * (rT - 24405
79da0 38 37 29 20 2d 20 34 33 32 30 30 3b 0a 23 65 6e  87) - 43200;.#en
79db0 64 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f  dif.#ifdef HAVE_
79dc0 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20  GMTIME_R.  {.   
79dd0 20 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b   struct tm sNow;
79de0 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74  .    gmtime_r(&t
79df0 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74  , &sNow);.    st
79e00 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c  rftime(zBuf, 20,
79e10 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29   zFormat, &sNow)
79e20 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a  ;.  }.#else.  {.
79e30 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70      struct tm *p
79e40 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  Tm;.    sqlite3_
79e50 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69  mutex_enter(sqli
79e60 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
79e70 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
79e80 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20  C_MASTER));.    
79e90 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29  pTm = gmtime(&t)
79ea0 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a  ;.    strftime(z
79eb0 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74  Buf, 20, zFormat
79ec0 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69  , pTm);.    sqli
79ed0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
79ee0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
79ef0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
79f00 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
79f10 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71    }.#endif..  sq
79f20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
79f30 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
79f40 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
79f50 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66  SIENT);.}.#endif
79f60 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
79f70 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
79f80 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76   all of the abov
79f90 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73  e C functions as
79fa0 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e   SQL.** function
79fb0 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20  s.  This should 
79fc0 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74  be the only rout
79fd0 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ine in this file
79fe0 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61   with.** externa
79ff0 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51  l linkage..*/.SQ
7a000 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
7a010 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
7a020 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f  rDateTimeFunctio
7a030 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ns(void){.  stat
7a040 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75  ic SQLITE_WSD Fu
7a050 6e 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46  ncDef aDateTimeF
7a060 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64  uncs[] = {.#ifnd
7a070 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
7a080 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20  ATETIME_FUNCS.  
7a090 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61    FUNCTION(julia
7a0a0 6e 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c  nday,        -1,
7a0b0 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79   0, 0, julianday
7a0c0 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43  Func ),.    FUNC
7a0d0 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20  TION(date,      
7a0e0 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c         -1, 0, 0,
7a0f0 20 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29   dateFunc      )
7a100 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74  ,.    FUNCTION(t
7a110 69 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ime,            
7a120 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46   -1, 0, 0, timeF
7a130 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20  unc      ),.    
7a140 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d  FUNCTION(datetim
7a150 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30  e,         -1, 0
7a160 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e  , 0, datetimeFun
7a170 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49  c  ),.    FUNCTI
7a180 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20  ON(strftime,    
7a190 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73       -1, 0, 0, s
7a1a0 74 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a  trftimeFunc  ),.
7a1b0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72      FUNCTION(cur
7a1c0 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20  rent_time,      
7a1d0 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75  0, 0, 0, ctimeFu
7a1e0 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55  nc     ),.    FU
7a1f0 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74  NCTION(current_t
7a200 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20  imestamp, 0, 0, 
7a210 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e  0, ctimestampFun
7a220 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  c),.    FUNCTION
7a230 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20  (current_date,  
7a240 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61      0, 0, 0, cda
7a250 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65  teFunc     ),.#e
7a260 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43  lse.    STR_FUNC
7a270 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d  TION(current_tim
7a280 65 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25  e,      0, "%H:%
7a290 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20  M:%S",          
7a2a0 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75  0, currentTimeFu
7a2b0 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e  nc),.    STR_FUN
7a2c0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69  CTION(current_ti
7a2d0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d  mestamp, 0, "%Y-
7a2e0 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20  %m-%d",         
7a2f0 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46   0, currentTimeF
7a300 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55  unc),.    STR_FU
7a310 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64  NCTION(current_d
7a320 61 74 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59  ate,      0, "%Y
7a330 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22  -%m-%d %H:%M:%S"
7a340 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65  , 0, currentTime
7a350 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20  Func),.#endif.  
7a360 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75  };.  int i;.  Fu
7a370 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
7a380 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44   = &GLOBAL(FuncD
7a390 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47  efHash, sqlite3G
7a3a0 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b  lobalFunctions);
7a3b0 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e  .  FuncDef *aFun
7a3c0 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47  c = (FuncDef*)&G
7a3d0 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61  LOBAL(FuncDef, a
7a3e0 44 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a  DateTimeFuncs);.
7a3f0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72  .  for(i=0; i<Ar
7a400 72 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d  raySize(aDateTim
7a410 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20  eFuncs); i++){. 
7a420 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65     sqlite3FuncDe
7a430 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26  fInsert(pHash, &
7a440 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d  aFunc[i]);.  }.}
7a450 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
7a460 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20  * End of date.c 
7a470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a4a0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
7a4b0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e  * Begin file os.
7a4c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
7a4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a4f0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76  /./*.** 2005 Nov
7a500 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54  ember 29.**.** T
7a510 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
7a520 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
7a530 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
7a540 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
7a550 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
7a560 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
7a570 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
7a580 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
7a590 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
7a5a0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
7a5b0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
7a5c0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
7a5d0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
7a5e0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
7a5f0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
7a600 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
7a610 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
7a620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7a660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
7a670 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
7a680 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61  tains OS interfa
7a690 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ce code that is 
7a6a0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a  common to all.**
7a6b0 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a   architectures..
7a6c0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 63 2c  **.** $Id: os.c,
7a6d0 76 20 31 2e 31 32 36 20 32 30 30 39 2f 30 33 2f  v 1.126 2009/03/
7a6e0 32 35 20 31 34 3a 32 34 3a 34 32 20 64 72 68 20  25 14:24:42 drh 
7a6f0 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66 69 6e 65  Exp $.*/.#define
7a700 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 20 31   _SQLITE_OS_C_ 1
7a710 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f  .#undef _SQLITE_
7a720 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OS_C_../*.** The
7a730 20 64 65 66 61 75 6c 74 20 53 51 4c 69 74 65 20   default SQLite 
7a740 73 71 6c 69 74 65 33 5f 76 66 73 20 69 6d 70 6c  sqlite3_vfs impl
7a750 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 20 6e  ementations do n
7a760 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d  ot allocate.** m
7a770 65 6d 6f 72 79 20 28 61 63 74 75 61 6c 6c 79 2c  emory (actually,
7a780 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c 6c 6f 63   os_unix.c alloc
7a790 61 74 65 73 20 61 20 73 6d 61 6c 6c 20 61 6d 6f  ates a small amo
7a7a0 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a  unt of memory.**
7a7b0 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 4f 73 4f   from within OsO
7a7c0 70 65 6e 28 29 29 2c 20 62 75 74 20 73 6f 6d 65  pen()), but some
7a7d0 20 74 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70   third-party imp
7a7e0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79  lementations may
7a7f0 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65 73 74 20  ..** So we test 
7a800 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20 61  the effects of a
7a810 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 69 6e   malloc() failin
7a820 67 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65  g and the sqlite
7a830 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66 75 6e 63  3OsXXX().** func
7a840 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e 67 20 53  tion returning S
7a850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
7a860 4d 20 75 73 69 6e 67 20 74 68 65 20 44 4f 5f 4f  M using the DO_O
7a870 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 6d 61  S_MALLOC_TEST ma
7a880 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  cro..**.** The f
7a890 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
7a8a0 6e 73 20 61 72 65 20 69 6e 73 74 72 75 6d 65 6e  ns are instrumen
7a8b0 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 28 29  ted for malloc()
7a8c0 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 74 65 73   failure .** tes
7a8d0 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ting:.**.**     
7a8e0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 0a  sqlite3OsOpen().
7a8f0 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
7a900 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 20 73 71  Read().**     sq
7a910 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 0a 2a  lite3OsWrite().*
7a920 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53  *     sqlite3OsS
7a930 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  ync().**     sql
7a940 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a  ite3OsLock().**.
7a950 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
7a960 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 28  QLITE_TEST) && (
7a970 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30  SQLITE_OS_WIN==0
7a980 29 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f  ).  #define DO_O
7a990 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 69 66  S_MALLOC_TEST if
7a9a0 20 28 31 29 20 7b 20 20 20 20 20 20 20 20 20 20   (1) {          
7a9b0 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54    \.    void *pT
7a9c0 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65  stAlloc = sqlite
7a9d0 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20  3Malloc(10);    
7a9e0 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70 54     \.    if (!pT
7a9f0 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e 20  stAlloc) return 
7aa00 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
7aa10 45 4d 3b 20 20 5c 0a 20 20 20 20 73 71 6c 69 74  EM;  \.    sqlit
7aa20 65 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c 6f  e3_free(pTstAllo
7aa30 63 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  c);             
7aa40 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c         \.  }.#el
7aa50 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f  se.  #define DO_
7aa60 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 0a 23  OS_MALLOC_TEST.#
7aa70 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
7aa80 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
7aa90 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65  nes are convenie
7aaa0 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f  nce wrappers aro
7aab0 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f  und methods.** o
7aac0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  f the sqlite3_fi
7aad0 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73  le object.  This
7aae0 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20   is mostly just 
7aaf0 73 79 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e  syntactic sugar.
7ab00 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20   All.** of this 
7ab10 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74  would be complet
7ab20 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66  ely automatic if
7ab30 20 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64   SQLite were cod
7ab40 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20  ed using.** C++ 
7ab50 69 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e  instead of plain
7ab60 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54   old C..*/.SQLIT
7ab70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7ab80 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c  lite3OsClose(sql
7ab90 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b  ite3_file *pId){
7aba0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
7abb0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64  TE_OK;.  if( pId
7abc0 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
7abd0 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74    rc = pId->pMet
7abe0 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64  hods->xClose(pId
7abf0 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74  );.    pId->pMet
7ac00 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  hods = 0;.  }.  
7ac10 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
7ac20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7ac30 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71  sqlite3OsRead(sq
7ac40 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
7ac50 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20  void *pBuf, int 
7ac60 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29  amt, i64 offset)
7ac70 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  {.  DO_OS_MALLOC
7ac80 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20  _TEST;.  return 
7ac90 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52  id->pMethods->xR
7aca0 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 61 6d  ead(id, pBuf, am
7acb0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51  t, offset);.}.SQ
7acc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
7acd0 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
7ace0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
7acf0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42  , const void *pB
7ad00 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34  uf, int amt, i64
7ad10 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f   offset){.  DO_O
7ad20 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20  S_MALLOC_TEST;. 
7ad30 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
7ad40 68 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64 2c  hods->xWrite(id,
7ad50 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73   pBuf, amt, offs
7ad60 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  et);.}.SQLITE_PR
7ad70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7ad80 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69  3OsTruncate(sqli
7ad90 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36  te3_file *id, i6
7ada0 34 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 72  4 size){.  retur
7adb0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
7adc0 78 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73 69  xTruncate(id, si
7add0 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ze);.}.SQLITE_PR
7ade0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7adf0 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  3OsSync(sqlite3_
7ae00 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c  file *id, int fl
7ae10 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  ags){.  DO_OS_MA
7ae20 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74  LLOC_TEST;.  ret
7ae30 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
7ae40 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67  ->xSync(id, flag
7ae50 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  s);.}.SQLITE_PRI
7ae60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7ae70 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74  OsFileSize(sqlit
7ae80 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34  e3_file *id, i64
7ae90 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f   *pSize){.  DO_O
7aea0 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20  S_MALLOC_TEST;. 
7aeb0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
7aec0 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28  hods->xFileSize(
7aed0 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51  id, pSize);.}.SQ
7aee0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
7aef0 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73   sqlite3OsLock(s
7af00 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
7af10 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a   int lockType){.
7af20 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
7af30 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64  EST;.  return id
7af40 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 4c 6f 63  ->pMethods->xLoc
7af50 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b  k(id, lockType);
7af60 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
7af70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55  E int sqlite3OsU
7af80 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
7af90 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
7afa0 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  Type){.  return 
7afb0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 55  id->pMethods->xU
7afc0 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79  nlock(id, lockTy
7afd0 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  pe);.}.SQLITE_PR
7afe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7aff0 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64  3OsCheckReserved
7b000 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
7b010 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
7b020 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  Out){.  DO_OS_MA
7b030 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74  LLOC_TEST;.  ret
7b040 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
7b050 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  ->xCheckReserved
7b060 4c 6f 63 6b 28 69 64 2c 20 70 52 65 73 4f 75 74  Lock(id, pResOut
7b070 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
7b080 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
7b090 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c  sFileControl(sql
7b0a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
7b0b0 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
7b0c0 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  g){.  return id-
7b0d0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65  >pMethods->xFile
7b0e0 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20  Control(id, op, 
7b0f0 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pArg);.}.SQLITE_
7b100 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7b110 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  te3OsSectorSize(
7b120 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
7b130 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74  ){.  int (*xSect
7b140 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f  orSize)(sqlite3_
7b150 66 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65  file*) = id->pMe
7b160 74 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53 69  thods->xSectorSi
7b170 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 78 53  ze;.  return (xS
7b180 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63  ectorSize ? xSec
7b190 74 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51  torSize(id) : SQ
7b1a0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
7b1b0 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c  TOR_SIZE);.}.SQL
7b1c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7b1d0 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43  sqlite3OsDeviceC
7b1e0 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73  haracteristics(s
7b1f0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
7b200 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  {.  return id->p
7b210 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76 69 63 65  Methods->xDevice
7b220 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
7b230 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  id);.}../*.** Th
7b240 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20  e next group of 
7b250 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e  routines are con
7b260 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72  venience wrapper
7b270 73 20 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20  s around the.** 
7b280 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a  VFS methods..*/.
7b290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
7b2a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e  nt sqlite3OsOpen
7b2b0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
7b2c0 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20  *pVfs, .  const 
7b2d0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20  char *zPath, .  
7b2e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
7b2f0 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67  ile, .  int flag
7b300 73 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67  s, .  int *pFlag
7b310 73 4f 75 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63  sOut.){.  int rc
7b320 3b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  ;.  DO_OS_MALLOC
7b330 5f 54 45 53 54 3b 0a 20 20 72 63 20 3d 20 70 56  _TEST;.  rc = pV
7b340 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c 20  fs->xOpen(pVfs, 
7b350 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c  zPath, pFile, fl
7b360 61 67 73 2c 20 70 46 6c 61 67 73 4f 75 74 29 3b  ags, pFlagsOut);
7b370 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
7b380 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c  QLITE_OK || pFil
7b390 65 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 29  e->pMethods==0 )
7b3a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
7b3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7b3c0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c  int sqlite3OsDel
7b3d0 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ete(sqlite3_vfs 
7b3e0 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61  *pVfs, const cha
7b3f0 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20 64 69  r *zPath, int di
7b400 72 53 79 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e  rSync){.  return
7b410 20 70 56 66 73 2d 3e 78 44 65 6c 65 74 65 28 70   pVfs->xDelete(p
7b420 56 66 73 2c 20 7a 50 61 74 68 2c 20 64 69 72 53  Vfs, zPath, dirS
7b430 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ync);.}.SQLITE_P
7b440 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
7b450 65 33 4f 73 41 63 63 65 73 73 28 0a 20 20 73 71  e3OsAccess(.  sq
7b460 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
7b470 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
7b480 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c  zPath, .  int fl
7b490 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65  ags, .  int *pRe
7b4a0 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f  sOut.){.  DO_OS_
7b4b0 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72  MALLOC_TEST;.  r
7b4c0 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63 63  eturn pVfs->xAcc
7b4d0 65 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68 2c  ess(pVfs, zPath,
7b4e0 20 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74 29   flags, pResOut)
7b4f0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
7b500 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
7b510 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20  FullPathname(.  
7b520 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
7b530 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  s, .  const char
7b540 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 20   *zPath, .  int 
7b550 6e 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 68 61  nPathOut, .  cha
7b560 72 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20  r *zPathOut.){. 
7b570 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46   return pVfs->xF
7b580 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73  ullPathname(pVfs
7b590 2c 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75  , zPath, nPathOu
7b5a0 74 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a  t, zPathOut);.}.
7b5b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
7b5c0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
7b5d0 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ON.SQLITE_PRIVAT
7b5e0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f  E void *sqlite3O
7b5f0 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  sDlOpen(sqlite3_
7b600 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
7b610 20 63 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20   char *zPath){. 
7b620 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44   return pVfs->xD
7b630 6c 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74  lOpen(pVfs, zPat
7b640 68 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  h);.}.SQLITE_PRI
7b650 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
7b660 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74  3OsDlError(sqlit
7b670 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
7b680 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a  t nByte, char *z
7b690 42 75 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d  BufOut){.  pVfs-
7b6a0 3e 78 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20  >xDlError(pVfs, 
7b6b0 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b  nByte, zBufOut);
7b6c0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
7b6d0 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33  E void (*sqlite3
7b6e0 4f 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f  OsDlSym(sqlite3_
7b6f0 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20  vfs *pVfs, void 
7b700 2a 70 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68  *pHdle, const ch
7b710 61 72 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29  ar *zSym))(void)
7b720 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
7b730 3e 78 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48  >xDlSym(pVfs, pH
7b740 64 6c 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51  dle, zSym);.}.SQ
7b750 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
7b760 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f  d sqlite3OsDlClo
7b770 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  se(sqlite3_vfs *
7b780 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e  pVfs, void *pHan
7b790 64 6c 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44  dle){.  pVfs->xD
7b7a0 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61  lClose(pVfs, pHa
7b7b0 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  ndle);.}.#endif 
7b7c0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  /* SQLITE_OMIT_L
7b7d0 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f  OAD_EXTENSION */
7b7e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7b7f0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e  int sqlite3OsRan
7b800 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f  domness(sqlite3_
7b810 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
7b820 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66  Byte, char *zBuf
7b830 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  Out){.  return p
7b840 56 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73  Vfs->xRandomness
7b850 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42  (pVfs, nByte, zB
7b860 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45  ufOut);.}.SQLITE
7b870 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
7b880 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69  ite3OsSleep(sqli
7b890 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
7b8a0 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65  nt nMicro){.  re
7b8b0 74 75 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65  turn pVfs->xSlee
7b8c0 70 28 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b  p(pVfs, nMicro);
7b8d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
7b8e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
7b8f0 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
7b900 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f  e3_vfs *pVfs, do
7b910 75 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b  uble *pTimeOut){
7b920 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e  .  return pVfs->
7b930 78 43 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66  xCurrentTime(pVf
7b940 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a  s, pTimeOut);.}.
7b950 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7b960 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  int sqlite3OsOpe
7b970 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74  nMalloc(.  sqlit
7b980 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20  e3_vfs *pVfs, . 
7b990 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
7b9a0 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66  le, .  sqlite3_f
7b9b0 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20  ile **ppFile, . 
7b9c0 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e   int flags,.  in
7b9d0 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a  t *pOutFlags.){.
7b9e0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
7b9f0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74  E_NOMEM;.  sqlit
7ba00 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a  e3_file *pFile;.
7ba10 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74    pFile = (sqlit
7ba20 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65  e3_file *)sqlite
7ba30 33 4d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a  3Malloc(pVfs->sz
7ba40 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70  OsFile);.  if( p
7ba50 46 69 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d  File ){.    rc =
7ba60 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
7ba70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c  Vfs, zFile, pFil
7ba80 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c  e, flags, pOutFl
7ba90 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ags);.    if( rc
7baa0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
7bab0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
7bac0 65 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65  e(pFile);.    }e
7bad0 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69  lse{.      *ppFi
7bae0 6c 65 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20  le = pFile;.    
7baf0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
7bb00 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  c;.}.SQLITE_PRIV
7bb10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
7bb20 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74  sCloseFree(sqlit
7bb30 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b  e3_file *pFile){
7bb40 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
7bb50 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28  TE_OK;.  assert(
7bb60 20 70 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d   pFile );.  rc =
7bb70 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
7bb80 70 46 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65  pFile);.  sqlite
7bb90 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20  3_free(pFile);. 
7bba0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
7bbb0 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66  *.** The list of
7bbc0 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20   all registered 
7bbd0 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  VFS implementati
7bbe0 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ons..*/.static s
7bbf0 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c  qlite3_vfs * SQL
7bc00 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20  ITE_WSD vfsList 
7bc10 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73  = 0;.#define vfs
7bc20 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69  List GLOBAL(sqli
7bc30 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69  te3_vfs *, vfsLi
7bc40 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74  st)../*.** Locat
7bc50 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e  e a VFS by name.
7bc60 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20    If no name is 
7bc70 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65  given, simply re
7bc80 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73  turn the.** firs
7bc90 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73  t VFS on the lis
7bca0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
7bcb0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71   sqlite3_vfs *sq
7bcc0 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63  lite3_vfs_find(c
7bcd0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29  onst char *zVfs)
7bce0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
7bcf0 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 53  *pVfs = 0;.#if S
7bd00 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
7bd10 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
7bd20 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a   *mutex;.#endif.
7bd30 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
7bd40 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69  MIT_AUTOINIT.  i
7bd50 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  nt rc = sqlite3_
7bd60 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20  initialize();.  
7bd70 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
7bd80 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  0;.#endif.#if SQ
7bd90 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
7bda0 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65    mutex = sqlite
7bdb0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
7bdc0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
7bdd0 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a  MASTER);.#endif.
7bde0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
7bdf0 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
7be00 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c 69  for(pVfs = vfsLi
7be10 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d 70  st; pVfs; pVfs=p
7be20 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Vfs->pNext){.   
7be30 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 62   if( zVfs==0 ) b
7be40 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 74  reak;.    if( st
7be50 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d  rcmp(zVfs, pVfs-
7be60 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65  >zName)==0 ) bre
7be70 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ak;.  }.  sqlite
7be80 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
7be90 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  tex);.  return p
7bea0 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  Vfs;.}../*.** Un
7beb0 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d 20  link a VFS from 
7bec0 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a  the linked list.
7bed0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76  */.static void v
7bee0 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 33  fsUnlink(sqlite3
7bef0 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 61  _vfs *pVfs){.  a
7bf00 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
7bf10 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65  utex_held(sqlite
7bf20 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
7bf30 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
7bf40 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 66  MASTER)) );.  if
7bf50 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 20  ( pVfs==0 ){.   
7bf60 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d   /* No-op */.  }
7bf70 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74  else if( vfsList
7bf80 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 66  ==pVfs ){.    vf
7bf90 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e  sList = pVfs->pN
7bfa0 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ext;.  }else if(
7bfb0 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 20   vfsList ){.    
7bfc0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d  sqlite3_vfs *p =
7bfd0 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68   vfsList;.    wh
7bfe0 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26  ile( p->pNext &&
7bff0 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20   p->pNext!=pVfs 
7c000 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e  ){.      p = p->
7c010 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  pNext;.    }.   
7c020 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70   if( p->pNext==p
7c030 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  Vfs ){.      p->
7c040 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e  pNext = pVfs->pN
7c050 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ext;.    }.  }.}
7c060 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
7c070 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 20   a VFS with the 
7c080 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68  system.  It is h
7c090 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73  armless to regis
7c0a0 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  ter the same.** 
7c0b0 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  VFS multiple tim
7c0c0 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53  es.  The new VFS
7c0d0 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66   becomes the def
7c0e0 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74  ault if makeDflt
7c0f0 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a   is.** true..*/.
7c100 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
7c110 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
7c120 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ter(sqlite3_vfs 
7c130 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44  *pVfs, int makeD
7c140 66 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  flt){.  sqlite3_
7c150 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30  mutex *mutex = 0
7c160 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
7c170 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20  _OMIT_AUTOINIT. 
7c180 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
7c190 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a  3_initialize();.
7c1a0 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
7c1b0 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 6d  n rc;.#endif.  m
7c1c0 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
7c1d0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
7c1e0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
7c1f0 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  TER);.  sqlite3_
7c200 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
7c210 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28  x);.  vfsUnlink(
7c220 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b  pVfs);.  if( mak
7c230 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74  eDflt || vfsList
7c240 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d  ==0 ){.    pVfs-
7c250 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74  >pNext = vfsList
7c260 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20  ;.    vfsList = 
7c270 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pVfs;.  }else{. 
7c280 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d     pVfs->pNext =
7c290 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b   vfsList->pNext;
7c2a0 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e  .    vfsList->pN
7c2b0 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a  ext = pVfs;.  }.
7c2c0 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 74    assert(vfsList
7c2d0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
7c2e0 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b  ex_leave(mutex);
7c2f0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
7c300 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  _OK;.}../*.** Un
7c310 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 73  register a VFS s
7c320 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20  o that it is no 
7c330 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c  longer accessibl
7c340 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
7c350 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73   int sqlite3_vfs
7c360 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69  _unregister(sqli
7c370 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a  te3_vfs *pVfs){.
7c380 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
7c390 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f  DSAFE.  sqlite3_
7c3a0 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73  mutex *mutex = s
7c3b0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
7c3c0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
7c3d0 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65  ATIC_MASTER);.#e
7c3e0 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ndif.  sqlite3_m
7c3f0 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
7c400 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70  );.  vfsUnlink(p
7c410 56 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Vfs);.  sqlite3_
7c420 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
7c430 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
7c440 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  ITE_OK;.}../****
7c450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
7c460 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  f os.c *********
7c470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
7c4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
7c4b0 20 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a   file fault.c **
7c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
7c4f0 20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a   2008 Jan 22.**.
7c500 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
7c510 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
7c520 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
7c530 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
7c540 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
7c550 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
7c560 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
7c570 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
7c580 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
7c590 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7c5a0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
7c5b0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
7c5c0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
7c5d0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7c5e0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
7c5f0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
7c600 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
7c610 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
7c620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
7c660 2a 20 24 49 64 3a 20 66 61 75 6c 74 2e 63 2c 76  * $Id: fault.c,v
7c670 20 31 2e 31 31 20 32 30 30 38 2f 30 39 2f 30 32   1.11 2008/09/02
7c680 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78   00:52:52 drh Ex
7c690 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  p $.*/../*.** Th
7c6a0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
7c6b0 20 63 6f 64 65 20 74 6f 20 73 75 70 70 6f 72 74   code to support
7c6c0 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20   the concept of 
7c6d0 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c  "benign" .** mal
7c6e0 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 28 77 68  loc failures (wh
7c6f0 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29  en the xMalloc()
7c700 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 20 6d   or xRealloc() m
7c710 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20  ethod of the.** 
7c720 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
7c730 6f 64 73 20 73 74 72 75 63 74 75 72 65 20 66 61  ods structure fa
7c740 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ils to allocate 
7c750 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  a block of memor
7c760 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 73  y.** and returns
7c770 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74   0). .**.** Most
7c780 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
7c790 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e   are non-benign.
7c7a0 20 41 66 74 65 72 20 74 68 65 79 20 6f 63 63 75   After they occu
7c7b0 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 61 62 61  r, SQLite.** aba
7c7c0 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 72 65 6e  ndons the curren
7c7d0 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20  t operation and 
7c7e0 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
7c7f0 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 0a 2a   code (usually.*
7c800 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20  * SQLITE_NOMEM) 
7c810 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48 6f 77  to the user. How
7c820 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73 20  ever, sometimes 
7c830 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 6e  a fault is not n
7c840 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 66 61  ecessarily.** fa
7c850 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  tal. For example
7c860 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61  , if a malloc fa
7c870 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69 7a 69  ils while resizi
7c880 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 2c  ng a hash table,
7c890 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 63 6f 6d   this .** is com
7c8a0 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65 72 61  pletely recovera
7c8b0 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20 6e 6f  ble simply by no
7c8c0 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74 20 74  t carrying out t
7c8d0 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65 20 0a  he resize. The .
7c8e0 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 77 69  ** hash table wi
7c8f0 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 66  ll continue to f
7c900 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79  unction normally
7c910 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 20 66  .  So a malloc f
7c920 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 72 69 6e  ailure .** durin
7c930 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 72  g a hash table r
7c940 65 73 69 7a 65 20 69 73 20 61 20 62 65 6e 69 67  esize is a benig
7c950 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69  n fault..*/...#i
7c960 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
7c970 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 0a  T_BUILTIN_TEST..
7c980 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61 72  /*.** Global var
7c990 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64  iables..*/.typed
7c9a0 65 66 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e  ef struct Benign
7c9b0 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e 69  MallocHooks Beni
7c9c0 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73  gnMallocHooks;.s
7c9d0 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
7c9e0 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61   struct BenignMa
7c9f0 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f  llocHooks {.  vo
7ca00 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69  id (*xBenignBegi
7ca10 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64  n)(void);.  void
7ca20 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76   (*xBenignEnd)(v
7ca30 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 65 33 48  oid);.} sqlite3H
7ca40 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d 3b  ooks = { 0, 0 };
7ca50 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 48 6f 6f  ../* The "wsdHoo
7ca60 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72  ks" macro will r
7ca70 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 70  esolve to the ap
7ca80 70 72 6f 70 72 69 61 74 65 20 42 65 6e 69 67 6e  propriate Benign
7ca90 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73  MallocHooks.** s
7caa0 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 77 72  tructure.  If wr
7cab0 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
7cac0 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65  ta is unsupporte
7cad0 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c  d on the target,
7cae0 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c  .** we have to l
7caf0 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20  ocate the state 
7cb00 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69  vector at run-ti
7cb10 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65  me.  In the more
7cb20 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20   common.** case 
7cb30 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73  where writable s
7cb40 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75  tatic data is su
7cb50 70 70 6f 72 74 65 64 2c 20 77 73 64 48 6f 6f 6b  pported, wsdHook
7cb60 73 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65  s can refer dire
7cb70 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22  ctly.** to the "
7cb80 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 20 73 74  sqlite3Hooks" st
7cb90 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61  ate vector decla
7cba0 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69  red above..*/.#i
7cbb0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
7cbc0 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73  _WSD.# define ws
7cbd0 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 20 42  dHooksInit \.  B
7cbe0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
7cbf0 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 42 65   *x = &GLOBAL(Be
7cc00 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c  nignMallocHooks,
7cc10 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 0a 23 20  sqlite3Hooks).# 
7cc20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20  define wsdHooks 
7cc30 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  x[0].#else.# def
7cc40 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74  ine wsdHooksInit
7cc50 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f  .# define wsdHoo
7cc60 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 0a  ks sqlite3Hooks.
7cc70 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52  #endif.../*.** R
7cc80 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f  egister hooks to
7cc90 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 6c 69 74   call when sqlit
7cca0 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
7ccb0 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c  loc() and.** sql
7ccc0 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
7ccd0 6c 6f 63 28 29 20 61 72 65 20 63 61 6c 6c 65 64  loc() are called
7cce0 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a  , respectively..
7ccf0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
7cd00 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
7cd10 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28  nignMallocHooks(
7cd20 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67  .  void (*xBenig
7cd30 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 2c 0a 20  nBegin)(void),. 
7cd40 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45   void (*xBenignE
7cd50 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a 20 20 77  nd)(void).){.  w
7cd60 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 77  sdHooksInit;.  w
7cd70 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42  sdHooks.xBenignB
7cd80 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 6e 42 65  egin = xBenignBe
7cd90 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e  gin;.  wsdHooks.
7cda0 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 78 42 65  xBenignEnd = xBe
7cdb0 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  nignEnd;.}../*.*
7cdc0 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65 33 45  * This (sqlite3E
7cdd0 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
7cde0 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53  ) is called by S
7cdf0 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20 69 6e  QLite code to in
7ce00 64 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 73  dicate that.** s
7ce10 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63  ubsequent malloc
7ce20 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 62 65   failures are be
7ce30 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74 6f 20  nign. A call to 
7ce40 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
7ce50 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e 64 69  Malloc().** indi
7ce60 63 61 74 65 73 20 74 68 61 74 20 73 75 62 73 65  cates that subse
7ce70 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69  quent malloc fai
7ce80 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65  lures are non-be
7ce90 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nign..*/.SQLITE_
7cea0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
7ceb0 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
7cec0 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77  alloc(void){.  w
7ced0 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69  sdHooksInit;.  i
7cee0 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e  f( wsdHooks.xBen
7cef0 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 20  ignBegin ){.    
7cf00 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e  wsdHooks.xBenign
7cf10 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53  Begin();.  }.}.S
7cf20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7cf30 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  id sqlite3EndBen
7cf40 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b  ignMalloc(void){
7cf50 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b  .  wsdHooksInit;
7cf60 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e  .  if( wsdHooks.
7cf70 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b 0a 20 20  xBenignEnd ){.  
7cf80 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69    wsdHooks.xBeni
7cf90 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a  gnEnd();.  }.}..
7cfa0 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 6e  #endif   /* #ifn
7cfb0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
7cfc0 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a  BUILTIN_TEST */.
7cfd0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
7cfe0 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 2e 63 20   End of fault.c 
7cff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
7d020 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
7d030 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 30   Begin file mem0
7d040 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
7d050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
7d070 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f  ./*.** 2008 Octo
7d080 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 28.**.** The
7d090 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
7d0a0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
7d0b0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
7d0c0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
7d0d0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
7d0e0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
7d0f0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
7d100 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
7d110 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
7d120 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
7d130 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
7d140 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
7d150 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
7d160 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
7d170 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
7d180 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
7d190 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
7d1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1e0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
7d1f0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61   file contains a
7d200 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 61 6c   no-op memory al
7d210 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73  location drivers
7d220 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0a 2a 2a   for use when.**
7d230 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c   SQLITE_ZERO_MAL
7d240 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64 2e 20  LOC is defined. 
7d250 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   The allocation 
7d260 64 72 69 76 65 72 73 20 69 6d 70 6c 65 6d 65 6e  drivers implemen
7d270 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 6c 77 61  ted.** here alwa
7d280 79 73 20 66 61 69 6c 2e 20 20 53 51 4c 69 74 65  ys fail.  SQLite
7d290 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 61 74   will not operat
7d2a0 65 20 77 69 74 68 20 74 68 65 73 65 20 64 72 69  e with these dri
7d2b0 76 65 72 73 2e 20 20 54 68 65 73 65 0a 2a 2a 20  vers.  These.** 
7d2c0 61 72 65 20 6d 65 72 65 6c 79 20 70 6c 61 63 65  are merely place
7d2d0 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 6c 20 64  holders.  Real d
7d2e0 72 69 76 65 72 73 20 6d 75 73 74 20 62 65 20 73  rivers must be s
7d2f0 75 62 73 74 69 74 75 74 65 64 20 75 73 69 6e 67  ubstituted using
7d300 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  .** sqlite3_conf
7d310 69 67 28 29 20 62 65 66 6f 72 65 20 53 51 4c 69  ig() before SQLi
7d320 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 74 65 2e  te will operate.
7d330 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 30  .**.** $Id: mem0
7d340 2e 63 2c 76 20 31 2e 31 20 32 30 30 38 2f 31 30  .c,v 1.1 2008/10
7d350 2f 32 38 20 31 38 3a 35 38 3a 32 30 20 64 72 68  /28 18:58:20 drh
7d360 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
7d370 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
7d380 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
7d390 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66  cator is the def
7d3a0 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20  ault.  It is.** 
7d3b0 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68  used when no oth
7d3c0 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  er memory alloca
7d3d0 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64  tor is specified
7d3e0 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74   using compile-t
7d3f0 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a  ime.** macros..*
7d400 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
7d410 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a  ZERO_MALLOC../*.
7d420 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e  ** No-op version
7d430 73 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  s of all memory 
7d440 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69  allocation routi
7d450 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nes.*/.static vo
7d460 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61  id *sqlite3MemMa
7d470 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b  lloc(int nByte){
7d480 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61   return 0; }.sta
7d490 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
7d4a0 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50  MemFree(void *pP
7d4b0 72 69 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d  rior){ return; }
7d4c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71  .static void *sq
7d4d0 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28  lite3MemRealloc(
7d4e0 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e  void *pPrior, in
7d4f0 74 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e  t nByte){ return
7d500 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74   0; }.static int
7d510 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28   sqlite3MemSize(
7d520 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72  void *pPrior){ r
7d530 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69  eturn 0; }.stati
7d540 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
7d550 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20  Roundup(int n){ 
7d560 72 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74  return n; }.stat
7d570 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
7d580 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55  mInit(void *NotU
7d590 73 65 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  sed){ return SQL
7d5a0 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
7d5b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
7d5c0 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e  Shutdown(void *N
7d5d0 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 3b  otUsed){ return;
7d5e0 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72   }../*.** This r
7d5f0 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e  outine is the on
7d600 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68  ly routine in th
7d610 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74  is file with ext
7d620 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a  ernal linkage..*
7d630 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68  *.** Populate th
7d640 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  e low-level memo
7d650 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75  ry allocation fu
7d660 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20  nction pointers 
7d670 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f  in.** sqlite3Glo
7d680 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68  balConfig.m with
7d690 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
7d6a0 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69   routines in thi
7d6b0 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  s file..*/.SQLIT
7d6c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
7d6d0 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61  qlite3MemSetDefa
7d6e0 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61  ult(void){.  sta
7d6f0 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
7d700 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65  3_mem_methods de
7d710 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b  faultMethods = {
7d720 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
7d730 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c  Malloc,.     sql
7d740 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20  ite3MemFree,.   
7d750 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c    sqlite3MemReal
7d760 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  loc,.     sqlite
7d770 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73  3MemSize,.     s
7d780 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70  qlite3MemRoundup
7d790 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
7d7a0 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69  mInit,.     sqli
7d7b0 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a  te3MemShutdown,.
7d7c0 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71       0.  };.  sq
7d7d0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c  lite3_config(SQL
7d7e0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f  ITE_CONFIG_MALLO
7d7f0 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f  C, &defaultMetho
7d800 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ds);.}..#endif /
7d810 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41  * SQLITE_ZERO_MA
7d820 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  LLOC */../******
7d830 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
7d840 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem0.c *********
7d850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d870 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
7d880 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
7d890 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a  ile mem1.c *****
7d8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d8c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
7d8d0 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a  007 August 14.**
7d8e0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
7d8f0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
7d900 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
7d910 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
7d920 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
7d930 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
7d940 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
7d950 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
7d960 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
7d970 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
7d980 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
7d990 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
7d9a0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
7d9b0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
7d9c0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
7d9d0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
7d9e0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
7d9f0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
7da00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7da10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7da20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7da30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
7da40 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
7da50 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20  tains low-level 
7da60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
7da70 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68  n drivers for wh
7da80 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  en.** SQLite wil
7da90 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61  l use the standa
7daa0 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c  rd C-library mal
7dab0 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65  loc/realloc/free
7dac0 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f   interface.** to
7dad0 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f   obtain the memo
7dae0 72 79 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a  ry it needs..**.
7daf0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
7db00 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61  tains implementa
7db10 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77  tions of the low
7db20 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
7db30 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74  location.** rout
7db40 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69  ines specified i
7db50 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65  n the sqlite3_me
7db60 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  m_methods object
7db70 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d  ..**.** $Id: mem
7db80 31 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 2f  1.c,v 1.30 2009/
7db90 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 33 20 64  03/23 04:33:33 d
7dba0 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
7dbb0 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .*/../*.** This 
7dbc0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
7dbd0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
7dbe0 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20  is the default. 
7dbf0 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77   It is.** used w
7dc00 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d  hen no other mem
7dc10 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ory allocator is
7dc20 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67   specified using
7dc30 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a   compile-time.**
7dc40 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64   macros..*/.#ifd
7dc50 65 66 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ef SQLITE_SYSTEM
7dc60 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c  _MALLOC../*.** L
7dc70 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75  ike malloc(), bu
7dc80 74 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73  t remember the s
7dc90 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
7dca0 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20 74 68 61 74  ation.** so that
7dcb0 20 77 65 20 63 61 6e 20 66 69 6e 64 20 69 74 20   we can find it 
7dcc0 6c 61 74 65 72 20 75 73 69 6e 67 20 73 71 6c 69  later using sqli
7dcd0 74 65 33 4d 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a  te3MemSize()..**
7dce0 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77  .** For this low
7dcf0 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20  -level routine, 
7dd00 77 65 20 61 72 65 20 67 75 61 72 61 6e 74 65 65  we are guarantee
7dd10 64 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62  d that nByte>0 b
7dd20 65 63 61 75 73 65 0a 2a 2a 20 63 61 73 65 73 20  ecause.** cases 
7dd30 6f 66 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c  of nByte<=0 will
7dd40 20 62 65 20 69 6e 74 65 72 63 65 70 74 65 64 20   be intercepted 
7dd50 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68 20 62  and dealt with b
7dd60 79 20 68 69 67 68 65 72 20 6c 65 76 65 6c 0a 2a  y higher level.*
7dd70 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73  * routines..*/.s
7dd80 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69  tatic void *sqli
7dd90 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74  te3MemMalloc(int
7dda0 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74   nByte){.  sqlit
7ddb0 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61  e3_int64 *p;.  a
7ddc0 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29  ssert( nByte>0 )
7ddd0 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e  ;.  nByte = ROUN
7dde0 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d  D8(nByte);.  p =
7ddf0 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2b 38   malloc( nByte+8
7de00 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   );.  if( p ){. 
7de10 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b     p[0] = nByte;
7de20 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20  .    p++;.  }.  
7de30 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 70  return (void *)p
7de40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20  ;.}../*.** Like 
7de50 66 72 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73  free() but works
7de60 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73   for allocations
7de70 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
7de80 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28  qlite3MemMalloc(
7de90 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d  ).** or sqlite3M
7dea0 65 6d 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a  emRealloc()..**.
7deb0 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d  ** For this low-
7dec0 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77  level routine, w
7ded0 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74  e already know t
7dee0 68 61 74 20 70 50 72 69 6f 72 21 3d 30 20 73 69  hat pPrior!=0 si
7def0 6e 63 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65  nce.** cases whe
7df00 72 65 20 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c  re pPrior==0 wil
7df10 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65  l have been inte
7df20 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74  cepted and dealt
7df30 20 77 69 74 68 0a 2a 2a 20 62 79 20 68 69 67 68   with.** by high
7df40 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  er-level routine
7df50 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
7df60 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65  d sqlite3MemFree
7df70 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a  (void *pPrior){.
7df80 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
7df90 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  *p = (sqlite3_in
7dfa0 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61  t64*)pPrior;.  a
7dfb0 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30  ssert( pPrior!=0
7dfc0 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 66 72 65   );.  p--;.  fre
7dfd0 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  e(p);.}../*.** L
7dfe0 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20  ike realloc().  
7dff0 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61  Resize an alloca
7e000 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20  tion previously 
7e010 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
7e020 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f   sqlite3MemMallo
7e030 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  c()..**.** For t
7e040 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e  his low-level in
7e050 74 65 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77  terface, we know
7e060 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d 30 2e   that pPrior!=0.
7e070 20 20 43 61 73 65 73 20 77 68 65 72 65 0a 2a 2a    Cases where.**
7e080 20 70 50 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65   pPrior==0 while
7e090 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72   have been inter
7e0a0 63 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72  cepted by higher
7e0b0 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 20 61  -level routine a
7e0c0 6e 64 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64  nd.** redirected
7e0d0 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69   to xMalloc.  Si
7e0e0 6d 69 6c 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77  milarly, we know
7e0f0 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65   that nByte>0 be
7e100 63 61 75 73 65 73 0a 2a 2a 20 63 61 73 65 73 20  causes.** cases 
7e110 77 68 65 72 65 20 6e 42 79 74 65 3c 3d 30 20 77  where nByte<=0 w
7e120 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ill have been in
7e130 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69 67  tercepted by hig
7e140 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75  her-level.** rou
7e150 74 69 6e 65 73 20 61 6e 64 20 72 65 64 69 72 65  tines and redire
7e160 63 74 65 64 20 74 6f 20 78 46 72 65 65 2e 0a 2a  cted to xFree..*
7e170 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73  /.static void *s
7e180 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
7e190 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69  (void *pPrior, i
7e1a0 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c  nt nByte){.  sql
7e1b0 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20  ite3_int64 *p = 
7e1c0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
7e1d0 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74  pPrior;.  assert
7e1e0 28 20 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e  ( pPrior!=0 && n
7e1f0 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74  Byte>0 );.  nByt
7e200 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65  e = ROUND8(nByte
7e210 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65  );.  p = (sqlite
7e220 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b  3_int64*)pPrior;
7e230 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 72 65  .  p--;.  p = re
7e240 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 74 65 2b 38  alloc(p, nByte+8
7e250 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   );.  if( p ){. 
7e260 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b     p[0] = nByte;
7e270 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20  .    p++;.  }.  
7e280 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b  return (void*)p;
7e290 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74  .}../*.** Report
7e2a0 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73   the allocated s
7e2b0 69 7a 65 20 6f 66 20 61 20 70 72 69 6f 72 20 72  ize of a prior r
7e2c0 65 74 75 72 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c  eturn from xMall
7e2d0 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 52 65 61 6c  oc().** or xReal
7e2e0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
7e2f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53   int sqlite3MemS
7e300 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ize(void *pPrior
7e310 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
7e320 36 34 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72  64 *p;.  if( pPr
7e330 69 6f 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ior==0 ) return 
7e340 30 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65  0;.  p = (sqlite
7e350 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b  3_int64*)pPrior;
7e360 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e  .  p--;.  return
7e370 20 28 69 6e 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f   (int)p[0];.}../
7e380 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20  *.** Round up a 
7e390 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20  request size to 
7e3a0 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61  the next valid a
7e3b0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a  llocation size..
7e3c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
7e3d0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28  lite3MemRoundup(
7e3e0 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e  int n){.  return
7e3f0 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f   ROUND8(n);.}../
7e400 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
7e410 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  this module..*/.
7e420 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
7e430 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a  e3MemInit(void *
7e440 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53  NotUsed){.  UNUS
7e450 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
7e460 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
7e470 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
7e480 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
7e490 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f   this module..*/
7e4a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
7e4b0 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28  ite3MemShutdown(
7e4c0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
7e4d0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
7e4e0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
7e4f0 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
7e500 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
7e510 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
7e520 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
7e530 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b  th external link
7e540 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c  age..**.** Popul
7e550 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65  ate the low-leve
7e560 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
7e570 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  ion function poi
7e580 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69  nters in.** sqli
7e590 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
7e5a0 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  m with pointers 
7e5b0 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  to the routines 
7e5c0 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f  in this file..*/
7e5d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7e5e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53  void sqlite3MemS
7e5f0 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b  etDefault(void){
7e600 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
7e610 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
7e620 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f  ods defaultMetho
7e630 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69  ds = {.     sqli
7e640 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20  te3MemMalloc,.  
7e650 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65     sqlite3MemFre
7e660 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  e,.     sqlite3M
7e670 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  emRealloc,.     
7e680 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a  sqlite3MemSize,.
7e690 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52       sqlite3MemR
7e6a0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c  oundup,.     sql
7e6b0 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20  ite3MemInit,.   
7e6c0 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74    sqlite3MemShut
7e6d0 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d  down,.     0.  }
7e6e0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ;.  sqlite3_conf
7e6f0 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
7e700 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c  _MALLOC, &defaul
7e710 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65  tMethods);.}..#e
7e720 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53  ndif /* SQLITE_S
7e730 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a  YSTEM_MALLOC */.
7e740 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
7e750 20 45 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a   End of mem1.c *
7e760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
7e790 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
7e7a0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32   Begin file mem2
7e7b0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
7e7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
7e7e0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
7e7f0 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 15.**.** The 
7e800 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
7e810 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
7e820 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
7e830 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
7e840 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
7e850 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
7e860 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
7e870 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
7e880 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
7e890 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
7e8a0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
7e8b0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
7e8c0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
7e8d0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
7e8e0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
7e8f0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
7e900 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
7e910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7e950 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
7e960 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f  file contains lo
7e970 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
7e980 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72  llocation driver
7e990 73 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51  s for when.** SQ
7e9a0 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68  Lite will use th
7e9b0 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62  e standard C-lib
7e9c0 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c  rary malloc/real
7e9d0 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61  loc/free interfa
7e9e0 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20  ce.** to obtain 
7e9f0 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65  the memory it ne
7ea00 65 64 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67  eds while adding
7ea10 20 6c 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f   lots of additio
7ea20 6e 61 6c 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a  nal debugging.**
7ea30 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
7ea40 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  each allocation 
7ea50 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70  in order to help
7ea60 20 64 65 74 65 63 74 20 61 6e 64 20 66 69 78 20   detect and fix 
7ea70 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65 61 6b 73 20  memory.** leaks 
7ea80 61 6e 64 20 6d 65 6d 6f 72 79 20 75 73 61 67 65  and memory usage
7ea90 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54   errors..**.** T
7eaa0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
7eab0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
7eac0 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76  s of the low-lev
7ead0 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
7eae0 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
7eaf0 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
7eb00 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  e sqlite3_mem_me
7eb10 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
7eb20 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 32 2e 63 2c  .** $Id: mem2.c,
7eb30 76 20 31 2e 34 35 20 32 30 30 39 2f 30 33 2f 32  v 1.45 2009/03/2
7eb40 33 20 30 34 3a 33 33 3a 33 33 20 64 61 6e 69 65  3 04:33:33 danie
7eb50 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
7eb60 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  ./*.** This vers
7eb70 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
7eb80 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75  y allocator is u
7eb90 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a  sed only if the.
7eba0 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  ** SQLITE_MEMDEB
7ebb0 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69  UG macro is defi
7ebc0 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ned.*/.#ifdef SQ
7ebd0 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f  LITE_MEMDEBUG../
7ebe0 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61  *.** The backtra
7ebf0 63 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  ce functionality
7ec00 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
7ec10 6c 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f  le with GLIBC.*/
7ec20 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f  .#ifdef __GLIBC_
7ec30 5f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62  _.  extern int b
7ec40 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c  acktrace(void**,
7ec50 69 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76  int);.  extern v
7ec60 6f 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79  oid backtrace_sy
7ec70 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f  mbols_fd(void*co
7ec80 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23  nst*,int,int);.#
7ec90 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61  else.# define ba
7eca0 63 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a 23  cktrace(A,B) 1.#
7ecb0 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63   define backtrac
7ecc0 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42  e_symbols_fd(A,B
7ecd0 2c 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,C).#endif../*.*
7ece0 2a 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c  * Each memory al
7ecf0 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c  location looks l
7ed00 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
7ed10 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
7ed20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ed30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ed40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ed50 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20  ---------.**  | 
7ed60 54 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61  Title |  backtra
7ed70 63 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d  ce pointers |  M
7ed80 65 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c  emBlockHdr |  al
7ed90 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47  location |  EndG
7eda0 75 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d  uard |.**  -----
7edb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7edc0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7edd0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ede0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7edf0 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70  ---.**.** The ap
7ee00 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73  plication code s
7ee10 65 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74  ees only a point
7ee20 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61  er to the alloca
7ee30 74 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a  tion.  We have.*
7ee40 2a 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f  * to back up fro
7ee50 6d 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  m the allocation
7ee60 20 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64   pointer to find
7ee70 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72   the MemBlockHdr
7ee80 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f  .  The.** MemBlo
7ee90 63 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74  ckHdr tells us t
7eea0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61  he size of the a
7eeb0 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68  llocation and th
7eec0 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62  e number of.** b
7eed0 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72  acktrace pointer
7eee0 73 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c 73  s.  There is als
7eef0 6f 20 61 20 67 75 61 72 64 20 77 6f 72 64 20 61  o a guard word a
7ef00 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
7ef10 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e  .** MemBlockHdr.
7ef20 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c  .*/.struct MemBl
7ef30 6f 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69  ockHdr {.  i64 i
7ef40 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
7ef50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
7ef60 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61  * Size of this a
7ef70 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73  llocation */.  s
7ef80 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
7ef90 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76  r *pNext, *pPrev
7efa0 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73  ;  /* Linked lis
7efb0 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64  t of all unfreed
7efc0 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61   memory */.  cha
7efd0 72 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20  r nBacktrace;   
7efe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7eff0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61   /* Number of ba
7f000 63 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73  cktraces on this
7f010 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72   alloc */.  char
7f020 20 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73   nBacktraceSlots
7f030 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
7f040 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63  /* Available bac
7f050 6b 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a  ktrace slots */.
7f060 20 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20    short nTitle; 
7f070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f080 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
7f090 66 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65  f title; include
7f0a0 73 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20  s '\0' */.  int 
7f0b0 69 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20  iForeGuard;     
7f0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f0d0 2f 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f  /* Guard word fo
7f0e0 72 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a  r sanity */.};..
7f0f0 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64  /*.** Guard word
7f100 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52  s.*/.#define FOR
7f110 45 47 55 41 52 44 20 30 78 38 30 46 35 45 31 35  EGUARD 0x80F5E15
7f120 33 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 55  3.#define REARGU
7f130 41 52 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a  ARD 0xE4676B53..
7f140 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20  /*.** Number of 
7f150 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72  malloc size incr
7f160 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e  ements to track.
7f170 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49  .*/.#define NCSI
7f180 5a 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20  ZE  1000../*.** 
7f190 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69  All of the stati
7f1a0 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64  c variables used
7f1b0 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   by this module 
7f1c0 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a  are collected.**
7f1d0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
7f1e0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22  tructure named "
7f1f0 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74  mem".  This is t
7f200 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
7f210 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
7f220 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
7f230 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
7f240 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
7f250 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
7f260 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
7f270 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
7f280 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
7f290 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20  atic struct {.  
7f2a0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78  .  /*.  ** Mutex
7f2b0 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65   to control acce
7f2c0 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79  ss to the memory
7f2d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
7f2e0 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71  ystem..  */.  sq
7f2f0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
7f300 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48  ex;..  /*.  ** H
7f310 65 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 20  ead and tail of 
7f320 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  a linked list of
7f330 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67   all outstanding
7f340 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a   allocations.  *
7f350 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  /.  struct MemBl
7f360 6f 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b 0a  ockHdr *pFirst;.
7f370 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
7f380 6b 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a  kHdr *pLast;.  .
7f390 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75    /*.  ** The nu
7f3a0 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f  mber of levels o
7f3b0 66 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20 73  f backtrace to s
7f3c0 61 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63  ave in new alloc
7f3d0 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  ations..  */.  i
7f3e0 6e 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20  nt nBacktrace;. 
7f3f0 20 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61   void (*xBacktra
7f400 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f  ce)(int, int, vo
7f410 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20  id **);..  /*.  
7f420 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f  ** Title text to
7f430 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74   insert in front
7f440 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20   of each block. 
7f450 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65   */.  int nTitle
7f460 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65  ;        /* Byte
7f470 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73  s of zTitle to s
7f480 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27  ave.  Includes '
7f490 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20  \0' and padding 
7f4a0 2a 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65  */.  char zTitle
7f4b0 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74  [100];  /* The t
7f4c0 69 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20  itle text */..  
7f4d0 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  /* .  ** sqlite3
7f4e0 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29  MallocDisallow()
7f4f0 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20   increments the 
7f500 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65  following counte
7f510 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d  r..  ** sqlite3M
7f520 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63  allocAllow() dec
7f530 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f  rements it..  */
7f540 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b  .  int disallow;
7f550 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77   /* Do not allow
7f560 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
7f570 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  on */..  /*.  **
7f580 20 47 61 74 68 65 72 20 73 74 61 74 69 73 74 69   Gather statisti
7f590 63 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 20  cs on the sizes 
7f5a0 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  of memory alloca
7f5b0 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c  tions..  ** nAll
7f5c0 6f 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 6d  oc[i] is the num
7f5d0 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f  ber of allocatio
7f5e0 6e 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69 2a  n attempts of i*
7f5f0 38 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69  8.  ** bytes.  i
7f600 3d 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20  ==NCSIZE is the 
7f610 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  number of alloca
7f620 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f  tion attempts fo
7f630 72 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72  r.  ** sizes mor
7f640 65 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20  e than NCSIZE*8 
7f650 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e  bytes..  */.  in
7f660 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d  t nAlloc[NCSIZE]
7f670 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20  ;      /* Total 
7f680 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  number of alloca
7f690 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  tions */.  int n
7f6a0 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b  Current[NCSIZE];
7f6b0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e      /* Current n
7f6c0 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
7f6d0 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  ions */.  int mx
7f6e0 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b  Current[NCSIZE];
7f6f0 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20     /* Highwater 
7f700 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e  mark for nCurren
7f710 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f  t */..} mem;.../
7f720 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f  *.** Adjust memo
7f730 72 79 20 75 73 61 67 65 20 73 74 61 74 69 73 74  ry usage statist
7f740 69 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ics.*/.static vo
7f750 69 64 20 61 64 6a 75 73 74 53 74 61 74 73 28 69  id adjustStats(i
7f760 6e 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e  nt iSize, int in
7f770 63 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20  crement){.  int 
7f780 69 20 3d 20 52 4f 55 4e 44 38 28 69 53 69 7a 65  i = ROUND8(iSize
7f790 29 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53  )/8;.  if( i>NCS
7f7a0 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d  IZE-1 ){.    i =
7f7b0 20 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d   NCSIZE - 1;.  }
7f7c0 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74  .  if( increment
7f7d0 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41  >0 ){.    mem.nA
7f7e0 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d  lloc[i]++;.    m
7f7f0 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b  em.nCurrent[i]++
7f800 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43  ;.    if( mem.nC
7f810 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78  urrent[i]>mem.mx
7f820 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20  Current[i] ){.  
7f830 20 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e      mem.mxCurren
7f840 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72  t[i] = mem.nCurr
7f850 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  ent[i];.    }.  
7f860 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e  }else{.    mem.n
7f870 43 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20  Current[i]--;.  
7f880 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43    assert( mem.nC
7f890 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a  urrent[i]>=0 );.
7f8a0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76    }.}../*.** Giv
7f8b0 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  en an allocation
7f8c0 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c  , find the MemBl
7f8d0 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20  ockHdr for that 
7f8e0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  allocation..**.*
7f8f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
7f900 68 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73  hecks the guards
7f910 20 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f   at either end o
7f920 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
7f930 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20   and.** if they 
7f940 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74  are incorrect it
7f950 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61   asserts..*/.sta
7f960 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  tic struct MemBl
7f970 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d  ockHdr *sqlite3M
7f980 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 76  emsysGetHeader(v
7f990 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e  oid *pAllocation
7f9a0 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  ){.  struct MemB
7f9b0 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e  lockHdr *p;.  in
7f9c0 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70  t *pInt;.  u8 *p
7f9d0 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72  U8;.  int nReser
7f9e0 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75  ve;..  p = (stru
7f9f0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29  ct MemBlockHdr*)
7fa00 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70  pAllocation;.  p
7fa10 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  --;.  assert( p-
7fa20 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 6e  >iForeGuard==(in
7fa30 74 29 46 4f 52 45 47 55 41 52 44 20 29 3b 0a 20  t)FOREGUARD );. 
7fa40 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 4e   nReserve = ROUN
7fa50 44 38 28 70 2d 3e 69 53 69 7a 65 29 3b 0a 20 20  D8(p->iSize);.  
7fa60 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70 41 6c  pInt = (int*)pAl
7fa70 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 55 38 20  location;.  pU8 
7fa80 3d 20 28 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69  = (u8*)pAllocati
7fa90 6f 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49  on;.  assert( pI
7faa0 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65  nt[nReserve/size
7fab0 6f 66 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52  of(int)]==(int)R
7fac0 45 41 52 47 55 41 52 44 20 29 3b 0a 20 20 2f 2a  EARGUARD );.  /*
7fad0 20 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e 79   This checks any
7fae0 20 6f 66 20 74 68 65 20 22 65 78 74 72 61 22 20   of the "extra" 
7faf0 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  bytes allocated 
7fb00 64 75 65 0a 20 20 2a 2a 20 74 6f 20 72 6f 75 6e  due.  ** to roun
7fb10 64 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38 20  ding up to an 8 
7fb20 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74 6f  byte boundary to
7fb30 20 65 6e 73 75 72 65 20 0a 20 20 2a 2a 20 74 68   ensure .  ** th
7fb40 65 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e 20  ey haven't been 
7fb50 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a  overwritten..  *
7fb60 2f 0a 20 20 77 68 69 6c 65 28 20 6e 52 65 73 65  /.  while( nRese
7fb70 72 76 65 2d 2d 20 3e 20 70 2d 3e 69 53 69 7a 65  rve-- > p->iSize
7fb80 20 29 20 61 73 73 65 72 74 28 20 70 55 38 5b 6e   ) assert( pU8[n
7fb90 52 65 73 65 72 76 65 5d 3d 3d 30 78 36 35 20 29  Reserve]==0x65 )
7fba0 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
7fbb0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
7fbc0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
7fbd0 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  s currently allo
7fbe0 63 61 74 65 64 20 61 74 20 61 64 64 72 65 73 73  cated at address
7fbf0 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   p..*/.static in
7fc00 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65  t sqlite3MemSize
7fc10 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 72  (void *p){.  str
7fc20 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
7fc30 2a 70 48 64 72 3b 0a 20 20 69 66 28 20 21 70 20  *pHdr;.  if( !p 
7fc40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
7fc50 0a 20 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 71  .  }.  pHdr = sq
7fc60 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65  lite3MemsysGetHe
7fc70 61 64 65 72 28 70 29 3b 0a 20 20 72 65 74 75 72  ader(p);.  retur
7fc80 6e 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d  n pHdr->iSize;.}
7fc90 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
7fca0 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
7fcb0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
7fcc0 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  em..*/.static in
7fcd0 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74  t sqlite3MemInit
7fce0 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b  (void *NotUsed){
7fcf0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
7fd00 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
7fd10 61 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 28  assert( (sizeof(
7fd20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
7fd30 64 72 29 26 37 29 20 3d 3d 20 30 20 29 3b 0a 20  dr)&7) == 0 );. 
7fd40 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f   if( !sqlite3Glo
7fd50 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
7fd60 61 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  at ){.    /* If 
7fd70 6d 65 6d 6f 72 79 20 73 74 61 74 75 73 20 69 73  memory status is
7fd80 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74   enabled, then t
7fd90 68 65 20 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 70  he malloc.c wrap
7fda0 70 65 72 20 77 69 6c 6c 20 61 6c 72 65 61 64 79  per will already
7fdb0 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 20 74 68 65  .    ** hold the
7fdc0 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65   STATIC_MEM mute
7fdd0 78 20 77 68 65 6e 20 74 68 65 20 72 6f 75 74 69  x when the routi
7fde0 6e 65 73 20 68 65 72 65 20 61 72 65 20 69 6e 76  nes here are inv
7fdf0 6f 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d  oked. */.    mem
7fe00 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33  .mutex = sqlite3
7fe10 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
7fe20 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
7fe30 45 4d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  EM);.  }.  retur
7fe40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
7fe50 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69  /*.** Deinitiali
7fe60 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
7fe70 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
7fe80 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  em..*/.static vo
7fe90 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75  id sqlite3MemShu
7fea0 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
7feb0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
7fec0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
7fed0 29 3b 0a 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d  );.  mem.mutex =
7fee0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75   0;.}../*.** Rou
7fef0 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20  nd up a request 
7ff00 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74  size to the next
7ff10 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f   valid allocatio
7ff20 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  n size..*/.stati
7ff30 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
7ff40 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a  Roundup(int n){.
7ff50 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28    return ROUND8(
7ff60 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  n);.}../*.** All
7ff70 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65  ocate nByte byte
7ff80 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  s of memory..*/.
7ff90 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c  static void *sql
7ffa0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e  ite3MemMalloc(in
7ffb0 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75  t nByte){.  stru
7ffc0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
7ffd0 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70  pHdr;.  void **p
7ffe0 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  Bt;.  char *z;. 
7fff0 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f   int *pInt;.  vo
80000 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74  id *p = 0;.  int
80010 20 74 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e   totalSize;.  in
80020 74 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71  t nReserve;.  sq
80030 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
80040 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  r(mem.mutex);.  
80050 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61  assert( mem.disa
80060 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65  llow==0 );.  nRe
80070 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 6e  serve = ROUND8(n
80080 42 79 74 65 29 3b 0a 20 20 74 6f 74 61 6c 53 69  Byte);.  totalSi
80090 7a 65 20 3d 20 6e 52 65 73 65 72 76 65 20 2b 20  ze = nReserve + 
800a0 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 20  sizeof(*pHdr) + 
800b0 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20  sizeof(int) +.  
800c0 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
800d0 2e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65  .nBacktrace*size
800e0 6f 66 28 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e  of(void*) + mem.
800f0 6e 54 69 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61  nTitle;.  p = ma
80100 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b  lloc(totalSize);
80110 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
80120 7a 20 3d 20 70 3b 0a 20 20 20 20 70 42 74 20 3d  z = p;.    pBt =
80130 20 28 76 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e   (void**)&z[mem.
80140 6e 54 69 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64  nTitle];.    pHd
80150 72 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42  r = (struct MemB
80160 6c 6f 63 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65  lockHdr*)&pBt[me
80170 6d 2e 6e 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20  m.nBacktrace];. 
80180 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d     pHdr->pNext =
80190 20 30 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50   0;.    pHdr->pP
801a0 72 65 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b  rev = mem.pLast;
801b0 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61  .    if( mem.pLa
801c0 73 74 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e  st ){.      mem.
801d0 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70  pLast->pNext = p
801e0 48 64 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Hdr;.    }else{.
801f0 20 20 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 74        mem.pFirst
80200 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20   = pHdr;.    }. 
80210 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70     mem.pLast = p
80220 48 64 72 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69  Hdr;.    pHdr->i
80230 46 6f 72 65 47 75 61 72 64 20 3d 20 46 4f 52 45  ForeGuard = FORE
80240 47 55 41 52 44 3b 0a 20 20 20 20 70 48 64 72 2d  GUARD;.    pHdr-
80250 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
80260 20 3d 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63   = mem.nBacktrac
80270 65 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 54 69  e;.    pHdr->nTi
80280 74 6c 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65  tle = mem.nTitle
80290 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42  ;.    if( mem.nB
802a0 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20  acktrace ){.    
802b0 20 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34 30    void *aAddr[40
802c0 5d 3b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e  ];.      pHdr->n
802d0 42 61 63 6b 74 72 61 63 65 20 3d 20 62 61 63 6b  Backtrace = back
802e0 74 72 61 63 65 28 61 41 64 64 72 2c 20 6d 65 6d  trace(aAddr, mem
802f0 2e 6e 42 61 63 6b 74 72 61 63 65 2b 31 29 2d 31  .nBacktrace+1)-1
80300 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  ;.      memcpy(p
80310 42 74 2c 20 26 61 41 64 64 72 5b 31 5d 2c 20 70  Bt, &aAddr[1], p
80320 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a  Hdr->nBacktrace*
80330 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a  sizeof(void*));.
80340 20 20 20 20 20 20 61 73 73 65 72 74 28 70 42 74        assert(pBt
80350 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [0]);.      if( 
80360 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 29  mem.xBacktrace )
80370 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78 42  {.        mem.xB
80380 61 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c 20  acktrace(nByte, 
80390 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
803a0 2d 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b 0a  -1, &aAddr[1]);.
803b0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
803c0 65 7b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e  e{.      pHdr->n
803d0 42 61 63 6b 74 72 61 63 65 20 3d 20 30 3b 0a 20  Backtrace = 0;. 
803e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d     }.    if( mem
803f0 2e 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20 20  .nTitle ){.     
80400 20 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e 7a   memcpy(z, mem.z
80410 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74 6c  Title, mem.nTitl
80420 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 48  e);.    }.    pH
80430 64 72 2d 3e 69 53 69 7a 65 20 3d 20 6e 42 79 74  dr->iSize = nByt
80440 65 3b 0a 20 20 20 20 61 64 6a 75 73 74 53 74 61  e;.    adjustSta
80450 74 73 28 6e 42 79 74 65 2c 20 2b 31 29 3b 0a 20  ts(nByte, +1);. 
80460 20 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29     pInt = (int*)
80470 26 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20 70 49  &pHdr[1];.    pI
80480 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65  nt[nReserve/size
80490 6f 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52 47  of(int)] = REARG
804a0 55 41 52 44 3b 0a 20 20 20 20 6d 65 6d 73 65 74  UARD;.    memset
804b0 28 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52 65  (pInt, 0x65, nRe
804c0 73 65 72 76 65 29 3b 0a 20 20 20 20 70 20 3d 20  serve);.    p = 
804d0 28 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 7d  (void*)pInt;.  }
804e0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
804f0 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
80500 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 0a  );.  return p; .
80510 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65  }../*.** Free me
80520 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mory..*/.static 
80530 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46  void sqlite3MemF
80540 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
80550 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  ){.  struct MemB
80560 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20  lockHdr *pHdr;. 
80570 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63   void **pBt;.  c
80580 68 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74  har *z;.  assert
80590 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
805a0 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 7c  onfig.bMemstat |
805b0 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 29  | mem.mutex!=0 )
805c0 3b 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74  ;.  pHdr = sqlit
805d0 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65  e3MemsysGetHeade
805e0 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 42 74  r(pPrior);.  pBt
805f0 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b   = (void**)pHdr;
80600 0a 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e  .  pBt -= pHdr->
80610 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b  nBacktraceSlots;
80620 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
80630 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78  _enter(mem.mutex
80640 29 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70  );.  if( pHdr->p
80650 50 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73 65  Prev ){.    asse
80660 72 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76 2d  rt( pHdr->pPrev-
80670 3e 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b 0a  >pNext==pHdr );.
80680 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 2d      pHdr->pPrev-
80690 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e 70  >pNext = pHdr->p
806a0 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Next;.  }else{. 
806b0 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70     assert( mem.p
806c0 46 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20  First==pHdr );. 
806d0 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20     mem.pFirst = 
806e0 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  pHdr->pNext;.  }
806f0 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e 65  .  if( pHdr->pNe
80700 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  xt ){.    assert
80710 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70  ( pHdr->pNext->p
80720 50 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20 20  Prev==pHdr );.  
80730 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70    pHdr->pNext->p
80740 50 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 72  Prev = pHdr->pPr
80750 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ev;.  }else{.   
80760 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c 61   assert( mem.pLa
80770 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20  st==pHdr );.    
80780 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72  mem.pLast = pHdr
80790 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 7a  ->pPrev;.  }.  z
807a0 20 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a 20   = (char*)pBt;. 
807b0 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69 74   z -= pHdr->nTit
807c0 6c 65 3b 0a 20 20 61 64 6a 75 73 74 53 74 61 74  le;.  adjustStat
807d0 73 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 2d  s(pHdr->iSize, -
807e0 31 29 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c 20  1);.  memset(z, 
807f0 30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f 69  0x2b, sizeof(voi
80800 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b 74  d*)*pHdr->nBackt
80810 72 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a 65  raceSlots + size
80820 6f 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20 20  of(*pHdr) +.    
80830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 48                pH
80840 64 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a 65  dr->iSize + size
80850 6f 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d 3e  of(int) + pHdr->
80860 6e 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65 28  nTitle);.  free(
80870 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  z);.  sqlite3_mu
80880 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75  tex_leave(mem.mu
80890 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  tex);  .}../*.**
808a0 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65   Change the size
808b0 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   of an existing 
808c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
808d0 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69  n..**.** For thi
808e0 73 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70 6c  s debugging impl
808f0 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20 2a  ementation, we *
80900 61 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20 63  always* make a c
80910 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c  opy of the.** al
80920 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61 20  location into a 
80930 6e 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65 6d  new place in mem
80940 6f 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77 61  ory.  In this wa
80950 79 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 69  y, if the .** hi
80960 67 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 20  gher level code 
80970 69 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72  is using pointer
80980 20 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f   to the old allo
80990 63 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a 2a  cation, it is .*
809a0 2a 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65  * much more like
809b0 6c 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 20  ly to break and 
809c0 77 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 65  we are much more
809d0 20 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 0a   liking to find.
809e0 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f  ** the error..*/
809f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71  .static void *sq
80a00 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28  lite3MemRealloc(
80a10 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e  void *pPrior, in
80a20 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75  t nByte){.  stru
80a30 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
80a40 70 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69 64 20  pOldHdr;.  void 
80a50 2a 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74 28  *pNew;.  assert(
80a60 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30   mem.disallow==0
80a70 20 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20   );.  pOldHdr = 
80a80 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74  sqlite3MemsysGet
80a90 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a  Header(pPrior);.
80aa0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
80ab0 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29  MemMalloc(nByte)
80ac0 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a  ;.  if( pNew ){.
80ad0 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c      memcpy(pNew,
80ae0 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70   pPrior, nByte<p
80af0 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20  OldHdr->iSize ? 
80b00 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d  nByte : pOldHdr-
80b10 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28  >iSize);.    if(
80b20 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e   nByte>pOldHdr->
80b30 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6d  iSize ){.      m
80b40 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70  emset(&((char*)p
80b50 4e 65 77 29 5b 70 4f 6c 64 48 64 72 2d 3e 69 53  New)[pOldHdr->iS
80b60 69 7a 65 5d 2c 20 30 78 32 62 2c 20 6e 42 79 74  ize], 0x2b, nByt
80b70 65 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69  e - pOldHdr->iSi
80b80 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ze);.    }.    s
80b90 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 70 50  qlite3MemFree(pP
80ba0 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  rior);.  }.  ret
80bb0 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
80bc0 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  ** Populate the 
80bd0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
80be0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63   allocation func
80bf0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e  tion pointers in
80c00 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  .** sqlite3Globa
80c10 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70  lConfig.m with p
80c20 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72  ointers to the r
80c30 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
80c40 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  file..*/.SQLITE_
80c50 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
80c60 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c  ite3MemSetDefaul
80c70 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  t(void){.  stati
80c80 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
80c90 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61  mem_methods defa
80ca0 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20  ultMethods = {. 
80cb0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61      sqlite3MemMa
80cc0 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74  lloc,.     sqlit
80cd0 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20  e3MemFree,.     
80ce0 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f  sqlite3MemReallo
80cf0 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  c,.     sqlite3M
80d00 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c  emSize,.     sql
80d10 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a  ite3MemRoundup,.
80d20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49       sqlite3MemI
80d30 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  nit,.     sqlite
80d40 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20  3MemShutdown,.  
80d50 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69     0.  };.  sqli
80d60 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
80d70 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c  E_CONFIG_MALLOC,
80d80 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73   &defaultMethods
80d90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
80da0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 61  the number of ba
80db0 63 6b 74 72 61 63 65 20 6c 65 76 65 6c 73 20 6b  cktrace levels k
80dc0 65 70 74 20 66 6f 72 20 65 61 63 68 20 61 6c 6c  ept for each all
80dd0 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61  ocation..** A va
80de0 6c 75 65 20 6f 66 20 7a 65 72 6f 20 74 75 72 6e  lue of zero turn
80df0 73 20 6f 66 66 20 62 61 63 6b 74 72 61 63 69 6e  s off backtracin
80e00 67 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 69  g.  The number i
80e10 73 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65 64  s always rounded
80e20 0a 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c 74  .** up to a mult
80e30 69 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53 51  iple of 2..*/.SQ
80e40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
80e50 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  d sqlite3Memdebu
80e60 67 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20 64  gBacktrace(int d
80e70 65 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65 70  epth){.  if( dep
80e80 74 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d 20  th<0 ){ depth = 
80e90 30 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74 68  0; }.  if( depth
80ea0 3e 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20 32  >20 ){ depth = 2
80eb0 30 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20 28  0; }.  depth = (
80ec0 64 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a 20  depth+1)&0xfe;. 
80ed0 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20   mem.nBacktrace 
80ee0 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c 49  = depth;.}..SQLI
80ef0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
80f00 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 42  sqlite3MemdebugB
80f10 61 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63 6b  acktraceCallback
80f20 28 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61  (void (*xBacktra
80f30 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f  ce)(int, int, vo
80f40 69 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e 78  id **)){.  mem.x
80f50 42 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61 63  Backtrace = xBac
80f60 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ktrace;.}../*.**
80f70 20 53 65 74 20 74 68 65 20 74 69 74 6c 65 20 73   Set the title s
80f80 74 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65 71  tring for subseq
80f90 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  uent allocations
80fa0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
80fb0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
80fc0 4d 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c 65  MemdebugSettitle
80fd0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 69  (const char *zTi
80fe0 74 6c 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  tle){.  unsigned
80ff0 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33   int n = sqlite3
81000 53 74 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65 29  Strlen30(zTitle)
81010 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 5f   + 1;.  sqlite3_
81020 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e  mutex_enter(mem.
81030 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 3e  mutex);.  if( n>
81040 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74  =sizeof(mem.zTit
81050 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f 66  le) ) n = sizeof
81060 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b 0a  (mem.zTitle)-1;.
81070 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54 69    memcpy(mem.zTi
81080 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29 3b  tle, zTitle, n);
81090 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b 6e 5d  .  mem.zTitle[n]
810a0 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 69 74   = 0;.  mem.nTit
810b0 6c 65 20 3d 20 52 4f 55 4e 44 38 28 6e 29 3b 0a  le = ROUND8(n);.
810c0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
810d0 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
810e0 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
810f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
81100 4d 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b 0a  MemdebugSync(){.
81110 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
81120 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66 6f  kHdr *pHdr;.  fo
81130 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73  r(pHdr=mem.pFirs
81140 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48  t; pHdr; pHdr=pH
81150 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dr->pNext){.    
81160 76 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76 6f  void **pBt = (vo
81170 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 70  id**)pHdr;.    p
81180 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63  Bt -= pHdr->nBac
81190 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20  ktraceSlots;.   
811a0 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28   mem.xBacktrace(
811b0 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48 64  pHdr->iSize, pHd
811c0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c  r->nBacktrace-1,
811d0 20 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a 7d   &pBt[1]);.  }.}
811e0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  ../*.** Open the
811f0 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20   file indicated 
81200 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20  and write a log 
81210 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d  of all unfreed m
81220 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61  emory .** alloca
81230 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20  tions into that 
81240 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  log..*/.SQLITE_P
81250 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
81260 74 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70 28  te3MemdebugDump(
81270 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
81280 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a  ename){.  FILE *
81290 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65  out;.  struct Me
812a0 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b  mBlockHdr *pHdr;
812b0 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20  .  void **pBt;. 
812c0 20 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d 20   int i;.  out = 
812d0 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c  fopen(zFilename,
812e0 20 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75 74   "w");.  if( out
812f0 3d 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  ==0 ){.    fprin
81300 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55  tf(stderr, "** U
81310 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20  nable to output 
81320 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74  memory debug out
81330 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e  put log: %s **\n
81340 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
81350 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65         zFilename
81360 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
81370 20 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65   }.  for(pHdr=me
81380 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20  m.pFirst; pHdr; 
81390 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74  pHdr=pHdr->pNext
813a0 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d  ){.    char *z =
813b0 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20 20   (char*)pHdr;.  
813c0 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61    z -= pHdr->nBa
813d0 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69 7a  cktraceSlots*siz
813e0 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48 64  eof(void*) + pHd
813f0 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 66  r->nTitle;.    f
81400 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a 2a  printf(out, "***
81410 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74 20  * %lld bytes at 
81420 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a 5c  %p from %s ****\
81430 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  n", .           
81440 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26 70   pHdr->iSize, &p
81450 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 54  Hdr[1], pHdr->nT
81460 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f 22  itle ? z : "???"
81470 29 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72 2d  );.    if( pHdr-
81480 3e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20  >nBacktrace ){. 
81490 20 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74 29       fflush(out)
814a0 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28 76  ;.      pBt = (v
814b0 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20  oid**)pHdr;.    
814c0 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e    pBt -= pHdr->n
814d0 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a  BacktraceSlots;.
814e0 20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65 5f        backtrace_
814f0 73 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c 20  symbols_fd(pBt, 
81500 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
81510 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b 0a  , fileno(out));.
81520 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
81530 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a  t, "\n");.    }.
81540 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75    }.  fprintf(ou
81550 74 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b  t, "COUNTS:\n");
81560 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43  .  for(i=0; i<NC
81570 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20  SIZE-1; i++){.  
81580 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63    if( mem.nAlloc
81590 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70 72  [i] ){.      fpr
815a0 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35  intf(out, "   %5
815b0 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30  d: %10d %10d %10
815c0 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20  d\n", .         
815d0 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c     i*8, mem.nAll
815e0 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72  oc[i], mem.nCurr
815f0 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75  ent[i], mem.mxCu
81600 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 7d  rrent[i]);.    }
81610 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e 6e  .  }.  if( mem.n
81620 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 20  Alloc[NCSIZE-1] 
81630 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  ){.    fprintf(o
81640 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31 30  ut, "   %5d: %10
81650 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 0a  d %10d %10d\n",.
81660 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 43 53               NCS
81670 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41 6c  IZE*8-8, mem.nAl
81680 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a 20  loc[NCSIZE-1],. 
81690 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e              mem.
816a0 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d  nCurrent[NCSIZE-
816b0 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e  1], mem.mxCurren
816c0 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20 20  t[NCSIZE-1]);.  
816d0 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b  }.  fclose(out);
816e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
816f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
81700 69 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d 4d  imes sqlite3MemM
81710 61 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65 6e  alloc() has been
81720 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49   called..*/.SQLI
81730 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
81740 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d 61  qlite3MemdebugMa
81750 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20 69  llocCount(){.  i
81760 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f 74  nt i;.  int nTot
81770 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  al = 0;.  for(i=
81780 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b 2b  0; i<NCSIZE; i++
81790 29 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b 3d  ){.    nTotal +=
817a0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b 0a   mem.nAlloc[i];.
817b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54 6f    }.  return nTo
817c0 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20  tal;.}...#endif 
817d0 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  /* SQLITE_MEMDEB
817e0 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  UG */../********
817f0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
81800 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m2.c ***********
81810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81830 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
81840 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
81850 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e mem3.c *******
81860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81880 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
81890 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a  7 October 14.**.
818a0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
818b0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
818c0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
818d0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
818e0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
818f0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
81900 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
81910 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
81920 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
81930 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
81940 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
81950 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
81960 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
81970 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
81980 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
81990 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
819a0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
819b0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
819c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
81a00 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
81a10 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
81a20 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
81a30 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c   a memory.** all
81a40 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
81a50 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c  m for use by SQL
81a60 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ite. .**.** This
81a70 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
81a80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
81a90 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74  n subsystem omit
81aa0 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20  s all.** use of 
81ab0 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53 51  malloc(). The SQ
81ac0 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 6c 69  Lite user suppli
81ad0 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65  es a block of me
81ae0 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63  mory.** before c
81af0 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  alling sqlite3_i
81b00 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d  nitialize() from
81b10 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f   which allocatio
81b20 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61  ns.** are made a
81b30 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  nd returned by t
81b40 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64  he xMalloc() and
81b50 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20   xRealloc() .** 
81b60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
81b70 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e   Once sqlite3_in
81b80 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62  itialize() has b
81b90 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74  een called,.** t
81ba0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  he amount of mem
81bb0 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f  ory available to
81bc0 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64   SQLite is fixed
81bd0 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62   and cannot.** b
81be0 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  e changed..**.**
81bf0 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
81c00 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
81c10 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
81c20 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20   is included.** 
81c30 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c  in the build onl
81c40 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  y if SQLITE_ENAB
81c50 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 65  LE_MEMSYS3 is de
81c60 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  fined..**.** $Id
81c70 3a 20 6d 65 6d 33 2e 63 2c 76 20 31 2e 32 35 20  : mem3.c,v 1.25 
81c80 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32  2008/11/19 16:52
81c90 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :44 danielk1977 
81ca0 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
81cb0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
81cc0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
81cd0 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 75 69  ator is only bui
81ce0 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72  lt into the libr
81cf0 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e  ary.** SQLITE_EN
81d00 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20  ABLE_MEMSYS3 is 
81d10 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e 69 6e  defined. Definin
81d20 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 64 6f  g this symbol do
81d30 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e 20 74  es not.** mean t
81d40 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  hat the library 
81d50 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d 6f 72  will use a memor
81d60 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 75 6c  y-pool by defaul
81d70 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a 2a 20  t, just that.** 
81d80 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e  it is available.
81d90 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 6c 6c   The mempool all
81da0 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 76 61  ocator is activa
81db0 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 0a 2a  ted by calling.*
81dc0 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  * sqlite3_config
81dd0 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ()..*/.#ifdef SQ
81de0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
81df0 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d  YS3../*.** Maxim
81e00 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33  um size (in Mem3
81e10 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73 6d  Blocks) of a "sm
81e20 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23  all" chunk..*/.#
81e30 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20  define MX_SMALL 
81e40 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65  10.../*.** Numbe
81e50 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68 61  r of freelist ha
81e60 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66  sh slots.*/.#def
81e70 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a 0a  ine N_HASH  61..
81e80 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61  /*.** A memory a
81e90 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f 20  llocation (also 
81ea0 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b 22  called a "chunk"
81eb0 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77  ) consists of tw
81ec0 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c  o or .** more bl
81ed0 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 68 20  ocks where each 
81ee0 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65 73  block is 8 bytes
81ef0 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 20 62  .  The first 8 b
81f00 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20 68  ytes are .** a h
81f10 65 61 64 65 72 20 74 68 61 74 20 69 73 20 6e 6f  eader that is no
81f20 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  t returned to th
81f30 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  e user..**.** A 
81f40 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 20  chunk is two or 
81f50 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 74  more blocks that
81f60 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63 6b   is either check
81f70 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72 65  ed out or.** fre
81f80 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62 6c  e.  The first bl
81f90 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20 75  ock has format u
81fa0 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69 7a  .hdr.  u.hdr.siz
81fb0 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20 74  e4x is 4 times t
81fc0 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68  he.** size of th
81fd0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20  e allocation in 
81fe0 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61 6c  blocks if the al
81ff0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
82000 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e 73  ..** The u.hdr.s
82010 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 20 74  ize4x&1 bit is t
82020 72 75 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b  rue if the chunk
82030 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20   is checked out 
82040 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20  and.** false if 
82050 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20  the chunk is on 
82060 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 54  the freelist.  T
82070 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  he u.hdr.size4x&
82080 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75 65  2 bit.** is true
82090 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
820a0 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
820b0 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65 20  d out and false 
820c0 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f  if the.** previo
820d0 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65 65  us chunk is free
820e0 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72 65  .  The u.hdr.pre
820f0 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20 74  vSize field is t
82100 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68  he size of.** th
82110 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b  e previous chunk
82120 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68   in blocks if th
82130 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b  e previous chunk
82140 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66 72   is on the.** fr
82150 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20 70  eelist. If the p
82160 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73  revious chunk is
82170 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68   checked out, th
82180 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65 76  en.** u.hdr.prev
82190 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72 74  Size can be part
821a0 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f 72   of the data for
821b0 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64 20   that chunk and 
821c0 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65  should.** not be
821d0 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
821e0 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65 6e  ..**.** We often
821f0 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 75 6e   identify a chun
82200 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 20 69  k by its index i
82210 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20  n mem3.aPool[]. 
82220 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69 73   When.** this is
82230 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e 6b   done, the chunk
82240 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f   index refers to
82250 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63   the second bloc
82260 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75 6e  k of.** the chun
82270 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c  k.  In this way,
82280 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b   the first chunk
82290 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
822a0 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69   1..** A chunk i
822b0 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 20  ndex of 0 means 
822c0 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 20  "no such chunk" 
822d0 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 69 76  and is the equiv
822e0 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e 55  alent.** of a NU
822f0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
82300 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f  * The second blo
82310 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b  ck of free chunk
82320 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  s is of the form
82330 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a 2a   u.list.  The.**
82340 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f 72 6d   two fields form
82350 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64   a double-linked
82360 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b 73 20   list of chunks 
82370 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a 65 73  of related sizes
82380 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f  ..** Pointers to
82390 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65   the head of the
823a0 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 65 64   list are stored
823b0 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c   in mem3.aiSmall
823c0 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c  [] .** for small
823d0 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 6d 65  er chunks and me
823e0 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f 72 20  m3.aiHash[] for 
823f0 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e 0a 2a  larger chunks..*
82400 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
82410 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 6e 6b  block of a chunk
82420 20 69 73 20 75 73 65 72 20 64 61 74 61 20 69 66   is user data if
82430 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 63 68   the chunk is ch
82440 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e 20 20  ecked .** out.  
82450 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 63 68  If a chunk is ch
82460 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 20 75  ecked out, the u
82470 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 78 74  ser data may ext
82480 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20  end into.** the 
82490 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 76  u.hdr.prevSize v
824a0 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  alue of the foll
824b0 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a  owing chunk..*/.
824c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d  typedef struct M
824d0 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f  em3Block Mem3Blo
824e0 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 33 42  ck;.struct Mem3B
824f0 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b  lock {.  union {
82500 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20  .    struct {.  
82510 20 20 20 20 75 33 32 20 70 72 65 76 53 69 7a 65      u32 prevSize
82520 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70  ;   /* Size of p
82530 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e  revious chunk in
82540 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65   Mem3Block eleme
82550 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 33 32  nts */.      u32
82560 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a 20   size4x;     /* 
82570 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 63  4x the size of c
82580 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e 20  urrent chunk in 
82590 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e  Mem3Block elemen
825a0 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 72 3b  ts */.    } hdr;
825b0 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20  .    struct {.  
825c0 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 20 20      u32 next;   
825d0 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
825e0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20  mem3.aPool[] of 
825f0 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20  next free chunk 
82600 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 72 65  */.      u32 pre
82610 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  v;       /* Inde
82620 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  x in mem3.aPool[
82630 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72  ] of previous fr
82640 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20  ee chunk */.    
82650 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b 0a 7d  } list;.  } u;.}
82660 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  ;../*.** All of 
82670 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61  the static varia
82680 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69  bles used by thi
82690 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c  s module are col
826a0 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61  lected.** into a
826b0 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72   single structur
826c0 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 2e 20  e named "mem3". 
826d0 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70   This is to keep
826e0 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76   the.** static v
826f0 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a  ariables organiz
82700 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65  ed and to reduce
82710 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75   namespace pollu
82720 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69  tion.** when thi
82730 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62  s module is comb
82740 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20  ined with other 
82750 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74  in the amalgamat
82760 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  ion..*/.static S
82770 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
82780 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a 20 20   Mem3Global {.  
82790 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61  /*.  ** Memory a
827a0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c  vailable for all
827b0 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 69  ocation. nPool i
827c0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
827d0 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 69 6e  e array.  ** (in
827e0 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 6f 69   Mem3Blocks) poi
827f0 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f 6f 6c  nted to by aPool
82800 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a 20 20   less 2..  */.  
82810 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 6d  u32 nPool;.  Mem
82820 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b 0a 0a  3Block *aPool;..
82830 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20 69    /*.  ** True i
82840 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 74  f we are evaluat
82850 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65  ing an out-of-me
82860 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 20  mory callback.. 
82870 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d 42   */.  int alarmB
82880 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  usy;.  .  /*.  *
82890 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72  * Mutex to contr
828a0 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ol access to the
828b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
828c0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20  on subsystem..  
828d0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
828e0 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20 20  ex *mutex;.  .  
828f0 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e 69  /*.  ** The mini
82900 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72  mum amount of fr
82910 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77 65  ee space that we
82920 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a 2f   have seen..  */
82930 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72 3b  .  u32 mnMaster;
82940 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61 73  ..  /*.  ** iMas
82950 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65 78  ter is the index
82960 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   of the master c
82970 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77 20  hunk.  Most new 
82980 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2a  allocations.  **
82990 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74 68   occur off of th
829a0 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61 73  is chunk.  szMas
829b0 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65 20  ter is the size 
829c0 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 0a  (in Mem3Blocks).
829d0 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72    ** of the curr
829e0 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d 61  ent master.  iMa
829f0 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68 65  ster is 0 if the
82a00 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65 72  re is not master
82a10 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65   chunk..  ** The
82a20 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69 73   master chunk is
82a30 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20 74   not in either t
82a40 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20 61  he aiHash[] or a
82a50 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a 20  iSmall[]..  */. 
82a60 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20 20   u32 iMaster;.  
82a70 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a 20  u32 szMaster;.. 
82a80 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20 6f   /*.  ** Array o
82a90 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65 20  f lists of free 
82aa0 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e 67  blocks according
82ab0 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73 69   to the block si
82ac0 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d 61  ze .  ** for sma
82ad0 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72 20  ller chunks, or 
82ae0 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62 6c  a hash on the bl
82af0 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61 72  ock size for lar
82b00 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e  ger.  ** chunks.
82b10 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53 6d  .  */.  u32 aiSm
82b20 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d 3b  all[MX_SMALL-1];
82b30 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20     /* For sizes 
82b40 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d 41  2 through MX_SMA
82b50 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a 2f  LL, inclusive */
82b60 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e 5f  .  u32 aiHash[N_
82b70 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a  HASH];        /*
82b80 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53 4d   For sizes MX_SM
82b90 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65 72  ALL+1 and larger
82ba0 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b 20 39   */.} mem3 = { 9
82bb0 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 65 66  7535575 };..#def
82bc0 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 4c 28  ine mem3 GLOBAL(
82bd0 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61  struct Mem3Globa
82be0 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a 20  l, mem3)../*.** 
82bf0 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  Unlink the chunk
82c00 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69   at mem3.aPool[i
82c10 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69  ] from list it i
82c20 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f  s currently.** o
82c30 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 74 68  n.  *pRoot is th
82c40 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 69 73  e list that i is
82c50 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f   a member of..*/
82c60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
82c70 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69  sys3UnlinkFromLi
82c80 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70  st(u32 i, u32 *p
82c90 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e 65 78  Root){.  u32 nex
82ca0 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t = mem3.aPool[i
82cb0 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20  ].u.list.next;. 
82cc0 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d 33   u32 prev = mem3
82cd0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
82ce0 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28  .prev;.  assert(
82cf0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
82d00 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
82d10 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d 3d 30  );.  if( prev==0
82d20 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 20 3d   ){.    *pRoot =
82d30 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a   next;.  }else{.
82d40 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70      mem3.aPool[p
82d50 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  rev].u.list.next
82d60 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69   = next;.  }.  i
82d70 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 20 6d  f( next ){.    m
82d80 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e  em3.aPool[next].
82d90 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 70 72  u.list.prev = pr
82da0 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 2e 61  ev;.  }.  mem3.a
82db0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[i].u.list.n
82dc0 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 33 2e  ext = 0;.  mem3.
82dd0 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
82de0 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  prev = 0;.}../*.
82df0 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68  ** Unlink the ch
82e00 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20 66  unk at index i f
82e10 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 65 72  rom .** whatever
82e20 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74   list is current
82e30 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a  ly a member of..
82e40 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
82e50 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 33 32  emsys3Unlink(u32
82e60 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c   i){.  u32 size,
82e70 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28   hash;.  assert(
82e80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
82e90 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
82ea0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
82eb0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
82ec0 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d  hdr.size4x & 1)=
82ed0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
82ee0 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d  i>=1 );.  size =
82ef0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
82f00 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b  .u.hdr.size4x/4;
82f10 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d  .  assert( size=
82f20 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69  =mem3.aPool[i+si
82f30 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  ze-1].u.hdr.prev
82f40 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Size );.  assert
82f50 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 69  ( size>=2 );.  i
82f60 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d  f( size <= MX_SM
82f70 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79  ALL ){.    memsy
82f80 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74  s3UnlinkFromList
82f90 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c  (i, &mem3.aiSmal
82fa0 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65  l[size-2]);.  }e
82fb0 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d 20  lse{.    hash = 
82fc0 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a 20  size % N_HASH;. 
82fd0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
82fe0 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d  FromList(i, &mem
82ff0 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b  3.aiHash[hash]);
83000 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69  .  }.}../*.** Li
83010 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20  nk the chunk at 
83020 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f  mem3.aPool[i] so
83030 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20   that is on the 
83040 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20 61  list rooted.** a
83050 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61  t *pRoot..*/.sta
83060 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
83070 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33 32  LinkIntoList(u32
83080 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b   i, u32 *pRoot){
83090 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
830a0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
830b0 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6d  m3.mutex) );.  m
830c0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c  em3.aPool[i].u.l
830d0 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 6f 6f  ist.next = *pRoo
830e0 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  t;.  mem3.aPool[
830f0 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d  i].u.list.prev =
83100 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f 6f 74   0;.  if( *pRoot
83110 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f   ){.    mem3.aPo
83120 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c 69 73  ol[*pRoot].u.lis
83130 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a  t.prev = i;.  }.
83140 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a 7d 0a    *pRoot = i;.}.
83150 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20  ./*.** Link the 
83160 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69  chunk at index i
83170 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68 65   into either the
83180 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
83190 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 73 74  small chunk list
831a0 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6c 61  , or into the la
831b0 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74  rge chunk hash t
831c0 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
831d0 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  void memsys3Link
831e0 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73  (u32 i){.  u32 s
831f0 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73  ize, hash;.  ass
83200 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
83210 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
83220 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
83230 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72   i>=1 );.  asser
83240 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
83250 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
83260 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 73 69   & 1)==0 );.  si
83270 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
83280 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
83290 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73  x/4;.  assert( s
832a0 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ize==mem3.aPool[
832b0 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e  i+size-1].u.hdr.
832c0 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73  prevSize );.  as
832d0 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b  sert( size>=2 );
832e0 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d  .  if( size <= M
832f0 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d  X_SMALL ){.    m
83300 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69  emsys3LinkIntoLi
83310 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d  st(i, &mem3.aiSm
83320 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20  all[size-2]);.  
83330 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20  }else{.    hash 
83340 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b  = size % N_HASH;
83350 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  .    memsys3Link
83360 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d  IntoList(i, &mem
83370 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b  3.aiHash[hash]);
83380 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
83390 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20   the STATIC_MEM 
833a0 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72  mutex is not alr
833b0 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69  eady held, obtai
833c0 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75  n it now. The mu
833d0 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65  tex.** will alre
833e0 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74  ady be held (obt
833f0 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e  ained by code in
83400 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a   malloc.c) if.**
83410 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
83420 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73  nfig.bMemStat is
83430 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   true..*/.static
83440 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 6e 74   void memsys3Ent
83450 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  er(void){.  if( 
83460 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
83470 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20  fig.bMemstat==0 
83480 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 3d 30  && mem3.mutex==0
83490 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d 75 74   ){.    mem3.mut
834a0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
834b0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
834c0 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b  TEX_STATIC_MEM);
834d0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
834e0 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e  utex_enter(mem3.
834f0 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63  mutex);.}.static
83500 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65 61   void memsys3Lea
83510 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ve(void){.  sqli
83520 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
83530 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a  mem3.mutex);.}..
83540 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 68 65  /*.** Called whe
83550 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20  n we are unable 
83560 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 61 6c  to satisfy an al
83570 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 79 74  location of nByt
83580 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
83590 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d  id memsys3OutOfM
835a0 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 65 29  emory(int nByte)
835b0 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e 61 6c  {.  if( !mem3.al
835c0 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 20 6d  armBusy ){.    m
835d0 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  em3.alarmBusy = 
835e0 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  1;.    assert( s
835f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
83600 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
83610 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
83620 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75  ex_leave(mem3.mu
83630 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
83640 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
83650 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c  (nByte);.    sql
83660 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
83670 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem3.mutex);.  
83680 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79    mem3.alarmBusy
83690 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a   = 0;.  }.}.../*
836a0 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 20 61  .** Chunk i is a
836b0 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 61 74   free chunk that
836c0 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b   has been unlink
836d0 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 73 20  ed.  Adjust its 
836e0 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d 65 74  .** size paramet
836f0 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d 6f 75  ers for check-ou
83700 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70  t and return a p
83710 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a 2a  ointer to the .*
83720 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e 20 6f  * user portion o
83730 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a 2f 0a  f the chunk..*/.
83740 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d  static void *mem
83750 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 33 32  sys3Checkout(u32
83760 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 29 7b   i, u32 nBlock){
83770 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 73 65  .  u32 x;.  asse
83780 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
83790 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
837a0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
837b0 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74  i>=1 );.  assert
837c0 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31  ( mem3.aPool[i-1
837d0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34  ].u.hdr.size4x/4
837e0 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73  ==nBlock );.  as
837f0 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c  sert( mem3.aPool
83800 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68  [i+nBlock-1].u.h
83810 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e 42 6c  dr.prevSize==nBl
83820 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d 65 6d  ock );.  x = mem
83830 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
83840 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d 65 6d  dr.size4x;.  mem
83850 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
83860 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f  dr.size4x = nBlo
83870 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 32 29  ck*4 | 1 | (x&2)
83880 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ;.  mem3.aPool[i
83890 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72  +nBlock-1].u.hdr
838a0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f  .prevSize = nBlo
838b0 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ck;.  mem3.aPool
838c0 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68  [i+nBlock-1].u.h
838d0 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a  dr.size4x |= 2;.
838e0 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e 61    return &mem3.a
838f0 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  Pool[i];.}../*.*
83900 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 65 20  * Carve a piece 
83910 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 20 6f  off of the end o
83920 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 73 74  f the mem3.iMast
83930 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 2a  er free chunk..*
83940 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
83950 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61 6c  er to the new al
83960 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c 20 69  location.  Or, i
83970 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75  f the master chu
83980 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72  nk.** is not lar
83990 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 75 72  ge enough, retur
839a0 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  n 0..*/.static v
839b0 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 6f 6d  oid *memsys3From
839c0 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c 6f 63  Master(u32 nBloc
839d0 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  k){.  assert( sq
839e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
839f0 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem3.mutex) );.
83a00 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 73    assert( mem3.s
83a10 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20  zMaster>=nBlock 
83a20 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 3e  );.  if( nBlock>
83a30 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31  =mem3.szMaster-1
83a40 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20 74   ){.    /* Use t
83a50 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72  he entire master
83a60 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 20   */.    void *p 
83a70 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75  = memsys3Checkou
83a80 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20  t(mem3.iMaster, 
83a90 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a  mem3.szMaster);.
83aa0 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72      mem3.iMaster
83ab0 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 73   = 0;.    mem3.s
83ac0 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20  zMaster = 0;.   
83ad0 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d   mem3.mnMaster =
83ae0 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   0;.    return p
83af0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
83b00 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 73 74  * Split the mast
83b10 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 75 72  er block.  Retur
83b20 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f 0a 20  n the tail. */. 
83b30 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 3b 0a     u32 newi, x;.
83b40 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d 33 2e      newi = mem3.
83b50 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 2e 73  iMaster + mem3.s
83b60 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f 63 6b  zMaster - nBlock
83b70 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 65  ;.    assert( ne
83b80 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 74 65  wi > mem3.iMaste
83b90 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d 33 2e  r+1 );.    mem3.
83ba0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
83bb0 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
83bc0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
83bd0 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20  ze = nBlock;.   
83be0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
83bf0 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
83c00 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
83c10 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 20  size4x |= 2;.   
83c20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69   mem3.aPool[newi
83c30 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
83c40 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b   = nBlock*4 + 1;
83c50 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74  .    mem3.szMast
83c60 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20  er -= nBlock;.  
83c70 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77    mem3.aPool[new
83c80 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  i-1].u.hdr.prevS
83c90 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  ize = mem3.szMas
83ca0 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d 65 6d  ter;.    x = mem
83cb0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
83cc0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
83cd0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 6d 65  ze4x & 2;.    me
83ce0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
83cf0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
83d00 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d  ize4x = mem3.szM
83d10 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20  aster*4 | x;.   
83d20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74   if( mem3.szMast
83d30 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74  er < mem3.mnMast
83d40 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33  er ){.      mem3
83d50 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33  .mnMaster = mem3
83d60 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d  .szMaster;.    }
83d70 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69  .    return (voi
83d80 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e  d*)&mem3.aPool[n
83d90 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ewi];.  }.}../*.
83da0 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 65  ** *pRoot is the
83db0 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 74 20   head of a list 
83dc0 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 6f  of free chunks o
83dd0 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 0a  f the same size.
83de0 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a 65 20  ** or same size 
83df0 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 72 20  hash.  In other 
83e00 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 69 73  words, *pRoot is
83e10 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 69 74   an entry in eit
83e20 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 53 6d  her.** mem3.aiSm
83e30 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e 61 69  all[] or mem3.ai
83e40 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20  Hash[].  .**.** 
83e50 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 61  This routine exa
83e60 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69 65  mines all entrie
83e70 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 6c  s on the given l
83e80 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a  ist and tries.**
83e90 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61 63   to coalesce eac
83ea0 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 61  h entries with a
83eb0 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68 75  djacent free chu
83ec0 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20  nks.  .**.** If 
83ed0 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b 20  it sees a chunk 
83ee0 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74  that is larger t
83ef0 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  han mem3.iMaster
83f00 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a 2a  , it replaces .*
83f10 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65  * the current me
83f20 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68 20  m3.iMaster with 
83f30 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 63  the new larger c
83f40 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 20  hunk.  In order 
83f50 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d 33  for.** this mem3
83f60 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63 65  .iMaster replace
83f70 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 68  ment to work, th
83f80 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6d  e master chunk m
83f90 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64  ust be.** linked
83fa0 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74   into the hash t
83fb0 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73 20  ables.  That is 
83fc0 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73  not the normal s
83fd0 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61 69  tate of.** affai
83fe0 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20  rs, of course.  
83ff0 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  The calling rout
84000 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74 68  ine must link th
84010 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75 6e  e master.** chun
84020 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69 6e  k before invokin
84030 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  g this routine, 
84040 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e 6b  then must unlink
84050 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 0a 2a   the (possibly.*
84060 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 74 65  * changed) maste
84070 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68 69  r chunk once thi
84080 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69  s routine has fi
84090 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  nished..*/.stati
840a0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d 65  c void memsys3Me
840b0 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 29 7b  rge(u32 *pRoot){
840c0 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 70 72  .  u32 iNext, pr
840d0 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b 0a  ev, size, i, x;.
840e0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
840f0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
84100 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  m3.mutex) );.  f
84110 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30  or(i=*pRoot; i>0
84120 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 20 20  ; i=iNext){.    
84130 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f  iNext = mem3.aPo
84140 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[i].u.list.nex
84150 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65  t;.    size = me
84160 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
84170 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20  hdr.size4x;.    
84180 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 31 29  assert( (size&1)
84190 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28  ==0 );.    if( (
841a0 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a 20 20  size&2)==0 ){.  
841b0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
841c0 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52 6f  kFromList(i, pRo
841d0 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ot);.      asser
841e0 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f  t( i > mem3.aPoo
841f0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  l[i-1].u.hdr.pre
84200 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 70  vSize );.      p
84210 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e 61  rev = i - mem3.a
84220 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
84230 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20 20  prevSize;.      
84240 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 74 20  if( prev==iNext 
84250 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74  ){.        iNext
84260 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72   = mem3.aPool[pr
84270 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b  ev].u.list.next;
84280 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
84290 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 72 65  emsys3Unlink(pre
842a0 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d  v);.      size =
842b0 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 70 72   i + size/4 - pr
842c0 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65  ev;.      x = me
842d0 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d  m3.aPool[prev-1]
842e0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20  .u.hdr.size4x & 
842f0 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  2;.      mem3.aP
84300 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 64  ool[prev-1].u.hd
84310 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a 65 2a  r.size4x = size*
84320 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d  4 | x;.      mem
84330 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 69 7a  3.aPool[prev+siz
84340 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  e-1].u.hdr.prevS
84350 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20  ize = size;.    
84360 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 70 72    memsys3Link(pr
84370 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 70  ev);.      i = p
84380 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rev;.    }else{.
84390 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 34 3b        size /= 4;
843a0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
843b0 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ize>mem3.szMaste
843c0 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  r ){.      mem3.
843d0 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 20 20  iMaster = i;.   
843e0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72     mem3.szMaster
843f0 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20   = size;.    }. 
84400 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
84410 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65  rn a block of me
84420 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74  mory of at least
84430 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e   nBytes in size.
84440 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  .** Return NULL 
84450 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  if unable..**.**
84460 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
84470 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
84480 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65  necessary mutexe
84490 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a  s, if any, are.*
844a0 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62  * already held b
844b0 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65  y the caller. He
844c0 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f  nce "Unsafe"..*/
844d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
844e0 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66  msys3MallocUnsaf
844f0 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  e(int nByte){.  
84500 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e 42 6c  u32 i;.  u32 nBl
84510 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 72 65  ock;.  u32 toFre
84520 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
84530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
84540 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem3.mutex) );.
84550 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
84560 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29  (Mem3Block)==8 )
84570 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 31  ;.  if( nByte<=1
84580 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20  2 ){.    nBlock 
84590 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 2;.  }else{.  
845a0 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74    nBlock = (nByt
845b0 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d 0a 20  e + 11)/8;.  }. 
845c0 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 6b 3e   assert( nBlock>
845d0 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 45 50  =2 );..  /* STEP
845e0 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f   1:.  ** Look fo
845f0 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68  r an entry of th
84600 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 69  e correct size i
84610 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 6d 61  n either the sma
84620 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 74 61  ll.  ** chunk ta
84630 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 6c 61  ble or in the la
84640 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74  rge chunk hash t
84650 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20  able.  This is. 
84660 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6d   ** successful m
84670 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d 65 20  ost of the time 
84680 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 20 6f  (about 9 times o
84690 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a 2f 0a  ut of 10)..  */.
846a0 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20    if( nBlock <= 
846b0 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20  MX_SMALL ){.    
846c0 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c  i = mem3.aiSmall
846d0 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 20 20  [nBlock-2];.    
846e0 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20  if( i>0 ){.     
846f0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
84700 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  omList(i, &mem3.
84710 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32  aiSmall[nBlock-2
84720 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ]);.      return
84730 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74   memsys3Checkout
84740 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20  (i, nBlock);.   
84750 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
84760 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c 6f 63  int hash = nBloc
84770 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20  k % N_HASH;.    
84780 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 61 73  for(i=mem3.aiHas
84790 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 69 3d  h[hash]; i>0; i=
847a0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
847b0 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20  list.next){.    
847c0 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c    if( mem3.aPool
847d0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
847e0 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 7b 0a  4x/4==nBlock ){.
847f0 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55          memsys3U
84800 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c  nlinkFromList(i,
84810 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61   &mem3.aiHash[ha
84820 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 72 65  sh]);.        re
84830 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63  turn memsys3Chec
84840 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b  kout(i, nBlock);
84850 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
84860 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 32 3a   }..  /* STEP 2:
84870 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61 74  .  ** Try to sat
84880 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74  isfy the allocat
84890 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61  ion by carving a
848a0 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68   piece off of th
848b0 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68  e end.  ** of th
848c0 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20  e master chunk. 
848d0 20 54 68 69 73 20 73 74 65 70 20 75 73 75 61 6c   This step usual
848e0 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 65 70  ly works if step
848f0 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20   1 fails..  */. 
84900 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74   if( mem3.szMast
84910 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20  er>=nBlock ){.  
84920 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33    return memsys3
84930 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 63  FromMaster(nBloc
84940 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 53  k);.  }...  /* S
84950 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 4c 6f  TEP 3:  .  ** Lo
84960 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65  op through the e
84970 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 6f 6f  ntire memory poo
84980 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 64 6a  l.  Coalesce adj
84990 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a 2a 20  acent free.  ** 
849a0 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d 70 75  chunks.  Recompu
849b0 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 63 68  te the master ch
849c0 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 67 65  unk as the large
849d0 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 20  st free chunk.. 
849e0 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 67 61   ** Then try aga
849f0 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  in to satisfy th
84a00 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20  e allocation by 
84a10 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 20  carving a piece 
84a20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  off.  ** of the 
84a30 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65  end of the maste
84a40 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73  r chunk.  This s
84a50 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 72 79  tep happens very
84a60 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77 65  .  ** rarely (we
84a70 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 20 66   hope!).  */.  f
84a80 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b  or(toFree=nBlock
84a90 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65 6d  *16; toFree<(mem
84aa0 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46  3.nPool*16); toF
84ab0 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 20 6d  ree *= 2){.    m
84ac0 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f 72  emsys3OutOfMemor
84ad0 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 20 69  y(toFree);.    i
84ae0 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20  f( mem3.iMaster 
84af0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  ){.      memsys3
84b00 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65  Link(mem3.iMaste
84b10 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69  r);.      mem3.i
84b20 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
84b30 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20    mem3.szMaster 
84b40 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  = 0;.    }.    f
84b50 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48  or(i=0; i<N_HASH
84b60 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65  ; i++){.      me
84b70 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33  msys3Merge(&mem3
84b80 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 20 20  .aiHash[i]);.   
84b90 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
84ba0 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b  i<MX_SMALL-1; i+
84bb0 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73  +){.      memsys
84bc0 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 53  3Merge(&mem3.aiS
84bd0 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  mall[i]);.    }.
84be0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d      if( mem3.szM
84bf0 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
84c00 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d  emsys3Unlink(mem
84c10 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  3.iMaster);.    
84c20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73    if( mem3.szMas
84c30 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20  ter>=nBlock ){. 
84c40 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65         return me
84c50 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28  msys3FromMaster(
84c60 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d  nBlock);.      }
84c70 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
84c80 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   If none of the 
84c90 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74 68  above worked, th
84ca0 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a 20  en we fail. */. 
84cb0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
84cc0 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73  .** Free an outs
84cd0 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61  tanding memory a
84ce0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
84cf0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
84d00 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
84d10 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65  necessary mutexe
84d20 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a  s, if any, are.*
84d30 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62  * already held b
84d40 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65  y the caller. He
84d50 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f  nce "Unsafe"..*/
84d60 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65  .void memsys3Fre
84d70 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 4f  eUnsafe(void *pO
84d80 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b  ld){.  Mem3Block
84d90 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b   *p = (Mem3Block
84da0 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 3b  *)pOld;.  int i;
84db0 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 3b 0a  .  u32 size, x;.
84dc0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
84dd0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
84de0 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
84df0 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 50 6f  sert( p>mem3.aPo
84e00 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e 61 50  ol && p<&mem3.aP
84e10 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20  ool[mem3.nPool] 
84e20 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d  );.  i = p - mem
84e30 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 72  3.aPool;.  asser
84e40 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
84e50 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
84e60 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 7a 65  &1)==1 );.  size
84e70 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
84e80 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
84e90 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 2b 73  4;.  assert( i+s
84ea0 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b  ize<=mem3.nPool+
84eb0 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  1 );.  mem3.aPoo
84ec0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
84ed0 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d  e4x &= ~1;.  mem
84ee0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31  3.aPool[i+size-1
84ef0 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
84f00 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e   = size;.  mem3.
84f10 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e  aPool[i+size-1].
84f20 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20  u.hdr.size4x &= 
84f30 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69 6e  ~2;.  memsys3Lin
84f40 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 20  k(i);..  /* Try 
84f50 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d 61  to expand the ma
84f60 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20 6e  ster using the n
84f70 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e 6b  ewly freed chunk
84f80 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 69   */.  if( mem3.i
84f90 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77 68  Master ){.    wh
84fa0 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c  ile( (mem3.aPool
84fb0 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d  [mem3.iMaster-1]
84fc0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29  .u.hdr.size4x&2)
84fd0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a  ==0 ){.      siz
84fe0 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  e = mem3.aPool[m
84ff0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
85000 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20  .hdr.prevSize;. 
85010 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65       mem3.iMaste
85020 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20  r -= size;.     
85030 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b   mem3.szMaster +
85040 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65  = size;.      me
85050 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33  msys3Unlink(mem3
85060 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  .iMaster);.     
85070 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
85080 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
85090 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32  u.hdr.size4x & 2
850a0 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ;.      mem3.aPo
850b0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d  ol[mem3.iMaster-
850c0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
850d0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a  = mem3.szMaster*
850e0 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d  4 | x;.      mem
850f0 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
85100 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
85110 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  er-1].u.hdr.prev
85120 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61  Size = mem3.szMa
85130 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ster;.    }.    
85140 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  x = mem3.aPool[m
85150 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
85160 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b  .hdr.size4x & 2;
85170 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d  .    while( (mem
85180 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
85190 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
851a0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
851b0 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20  4x&1)==0 ){.    
851c0 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28    memsys3Unlink(
851d0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
851e0 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  3.szMaster);.   
851f0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72     mem3.szMaster
85200 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d   += mem3.aPool[m
85210 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33  em3.iMaster+mem3
85220 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  .szMaster-1].u.h
85230 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 20  dr.size4x/4;.   
85240 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
85250 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  m3.iMaster-1].u.
85260 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d  hdr.size4x = mem
85270 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78  3.szMaster*4 | x
85280 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ;.      mem3.aPo
85290 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b  ol[mem3.iMaster+
852a0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d  mem3.szMaster-1]
852b0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
852c0 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
852d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
852e0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
852f0 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61  ize of an outsta
85300 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e  nding allocation
85310 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65  , in bytes.  The
85320 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65  .** size returne
85330 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79  d omits the 8-by
85340 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65  te header overhe
85350 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a  ad.  This only.*
85360 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e  * works for chun
85370 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72  ks that are curr
85380 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75  ently checked ou
85390 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
853a0 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f 69   memsys3Size(voi
853b0 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f  d *p){.  Mem3Blo
853c0 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 66  ck *pBlock;.  if
853d0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
853e0 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 28 4d  0;.  pBlock = (M
853f0 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61  em3Block*)p;.  a
85400 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b 5b 2d  ssert( (pBlock[-
85410 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  1].u.hdr.size4x&
85420 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  1)!=0 );.  retur
85430 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e  n (pBlock[-1].u.
85440 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 2a 32  hdr.size4x&~3)*2
85450 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   - 4;.}../*.** R
85460 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73  ound up a reques
85470 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65  t size to the ne
85480 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74  xt valid allocat
85490 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61  ion size..*/.sta
854a0 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 52  tic int memsys3R
854b0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20  oundup(int n){. 
854c0 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 20   if( n<=12 ){.  
854d0 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 20 7d    return 12;.  }
854e0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
854f0 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34   ((n+11)&~7) - 4
85500 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
85510 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f  llocate nBytes o
85520 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61  f memory..*/.sta
85530 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73  tic void *memsys
85540 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74  3Malloc(int nByt
85550 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  es){.  sqlite3_i
85560 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72  nt64 *p;.  asser
85570 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b 20 20  t( nBytes>0 );  
85580 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f          /* mallo
85590 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 74 20  c.c filters out 
855a0 30 20 62 79 74 65 20 72 65 71 75 65 73 74 73 20  0 byte requests 
855b0 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65  */.  memsys3Ente
855c0 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79  r();.  p = memsy
855d0 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e  s3MallocUnsafe(n
855e0 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 79 73  Bytes);.  memsys
855f0 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75  3Leave();.  retu
85600 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a  rn (void*)p; .}.
85610 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f  ./*.** Free memo
85620 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73  ry..*/.void mems
85630 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a 70 50  ys3Free(void *pP
85640 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28  rior){.  assert(
85650 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d 65 6d   pPrior );.  mem
85660 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 6d  sys3Enter();.  m
85670 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65  emsys3FreeUnsafe
85680 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73  (pPrior);.  mems
85690 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a 0a 2f  ys3Leave();.}../
856a0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
856b0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74  size of an exist
856c0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
856d0 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 2a 6d  ation.*/.void *m
856e0 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 76 6f  emsys3Realloc(vo
856f0 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20  id *pPrior, int 
85700 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e  nBytes){.  int n
85710 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a  Old;.  void *p;.
85720 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20    if( pPrior==0 
85730 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
85740 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79  lite3_malloc(nBy
85750 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  tes);.  }.  if( 
85760 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20  nBytes<=0 ){.   
85770 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
85780 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72  rior);.    retur
85790 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20  n 0;.  }.  nOld 
857a0 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 70 50  = memsys3Size(pP
857b0 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e 42 79  rior);.  if( nBy
857c0 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e 42 79  tes<=nOld && nBy
857d0 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 29 7b  tes>=nOld-128 ){
857e0 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 69  .    return pPri
857f0 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73  or;.  }.  memsys
85800 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20  3Enter();.  p = 
85810 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73  memsys3MallocUns
85820 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69  afe(nBytes);.  i
85830 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20  f( p ){.    if( 
85840 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a 20  nOld<nBytes ){. 
85850 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70       memcpy(p, p
85860 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20  Prior, nOld);.  
85870 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
85880 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c  emcpy(p, pPrior,
85890 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 7d 0a   nBytes);.    }.
858a0 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55      memsys3FreeU
858b0 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20  nsafe(pPrior);. 
858c0 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76   }.  memsys3Leav
858d0 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  e();.  return p;
858e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
858f0 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
85900 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
85910 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f 69 64  memsys3Init(void
85920 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
85930 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
85940 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 21  otUsed);.  if( !
85950 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
85960 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 20 20  fig.pHeap ){.   
85970 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
85980 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
85990 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20  Store a pointer 
859a0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 6c  to the memory bl
859b0 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73 74  ock in global st
859c0 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a 2f  ructure mem3. */
859d0 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
859e0 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20  f(Mem3Block)==8 
859f0 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20  );.  mem3.aPool 
85a00 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29 73  = (Mem3Block *)s
85a10 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
85a20 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 6d 33  ig.pHeap;.  mem3
85a30 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74 65  .nPool = (sqlite
85a40 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48  3GlobalConfig.nH
85a50 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65 6d  eap / sizeof(Mem
85a60 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a 0a 20  3Block)) - 2;.. 
85a70 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
85a80 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e  he master block.
85a90 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d 61 73   */.  mem3.szMas
85aa0 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c  ter = mem3.nPool
85ab0 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65  ;.  mem3.mnMaste
85ac0 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  r = mem3.szMaste
85ad0 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65  r;.  mem3.iMaste
85ae0 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e 61 50  r = 1;.  mem3.aP
85af0 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 69 7a  ool[0].u.hdr.siz
85b00 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a 4d 61  e4x = (mem3.szMa
85b10 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a 20 20  ster<<2) + 2;.  
85b20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
85b30 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 72 65  nPool].u.hdr.pre
85b40 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e 50 6f  vSize = mem3.nPo
85b50 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ol;.  mem3.aPool
85b60 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68  [mem3.nPool].u.h
85b70 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a 0a  dr.size4x = 1;..
85b80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
85b90 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69  OK;.}../*.** Dei
85ba0 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d  nitialize this m
85bb0 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  odule..*/.static
85bc0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68 75   void memsys3Shu
85bd0 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
85be0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
85bf0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
85c00 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
85c10 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  ../*.** Open the
85c20 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20   file indicated 
85c30 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20  and write a log 
85c40 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d  of all unfreed m
85c50 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61  emory .** alloca
85c60 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20  tions into that 
85c70 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  log..*/.SQLITE_P
85c80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
85c90 74 65 33 4d 65 6d 73 79 73 33 44 75 6d 70 28 63  te3Memsys3Dump(c
85ca0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
85cb0 6e 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51  name){.#ifdef SQ
85cc0 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c  LITE_DEBUG.  FIL
85cd0 45 20 2a 6f 75 74 3b 0a 20 20 75 33 32 20 69 2c  E *out;.  u32 i,
85ce0 20 6a 3b 0a 20 20 75 33 32 20 73 69 7a 65 3b 0a   j;.  u32 size;.
85cf0 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d    if( zFilename=
85d00 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b  =0 || zFilename[
85d10 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74  0]==0 ){.    out
85d20 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c   = stdout;.  }el
85d30 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f  se{.    out = fo
85d40 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22  pen(zFilename, "
85d50 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74  w");.    if( out
85d60 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72  ==0 ){.      fpr
85d70 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a  intf(stderr, "**
85d80 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75   Unable to outpu
85d90 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f  t memory debug o
85da0 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a  utput log: %s **
85db0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
85dc0 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65             zFile
85dd0 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74  name);.      ret
85de0 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
85df0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
85e00 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
85e10 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 20  "CHUNKS:\n");.  
85e20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33  for(i=1; i<=mem3
85e30 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f  .nPool; i+=size/
85e40 34 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d  4){.    size = m
85e50 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
85e60 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20  .hdr.size4x;.   
85e70 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31 20 29   if( size/4<=1 )
85e80 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
85e90 6f 75 74 2c 20 22 25 70 20 73 69 7a 65 20 65 72  out, "%p size er
85ea0 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50  ror\n", &mem3.aP
85eb0 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61  ool[i]);.      a
85ec0 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20  ssert( 0 );.    
85ed0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
85ee0 20 20 20 69 66 28 20 28 73 69 7a 65 26 31 29 3d     if( (size&1)=
85ef0 3d 30 20 26 26 20 6d 65 6d 33 2e 61 50 6f 6f 6c  =0 && mem3.aPool
85f00 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68  [i+size/4-1].u.h
85f10 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73 69 7a  dr.prevSize!=siz
85f20 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66 70 72  e/4 ){.      fpr
85f30 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61  intf(out, "%p ta
85f40 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e 6f 74  il size does not
85f50 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65 6d 33   match\n", &mem3
85f60 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20  .aPool[i]);.    
85f70 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20    assert( 0 );. 
85f80 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
85f90 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65 6d 33  }.    if( ((mem3
85fa0 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d  .aPool[i+size/4-
85fb0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  1].u.hdr.size4x&
85fc0 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31 29  2)>>1)!=(size&1)
85fd0 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
85fe0 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20  f(out, "%p tail 
85ff0 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69 73 20  checkout bit is 
86000 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d  incorrect\n", &m
86010 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20  em3.aPool[i]);. 
86020 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29       assert( 0 )
86030 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
86040 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a     }.    if( siz
86050 65 26 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72  e&1 ){.      fpr
86060 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36  intf(out, "%p %6
86070 64 20 62 79 74 65 73 20 63 68 65 63 6b 65 64 20  d bytes checked 
86080 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50  out\n", &mem3.aP
86090 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29  ool[i], (size/4)
860a0 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65  *8-8);.    }else
860b0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
860c0 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62 79 74  out, "%p %6d byt
860d0 65 73 20 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d  es free%s\n", &m
860e0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73  em3.aPool[i], (s
860f0 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20  ize/4)*8-8,.    
86100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 3d                i=
86110 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3f 20  =mem3.iMaster ? 
86120 22 20 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 3a 20  " **master**" : 
86130 22 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  "");.    }.  }. 
86140 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53   for(i=0; i<MX_S
86150 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20  MALL-1; i++){.  
86160 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 53 6d 61    if( mem3.aiSma
86170 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  ll[i]==0 ) conti
86180 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  nue;.    fprintf
86190 28 6f 75 74 2c 20 22 73 6d 61 6c 6c 28 25 32 64  (out, "small(%2d
861a0 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72  ):", i);.    for
861b0 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c  (j = mem3.aiSmal
861c0 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d  l[i]; j>0; j=mem
861d0 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73  3.aPool[j].u.lis
861e0 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66  t.next){.      f
861f0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70  printf(out, " %p
86200 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f  (%d)", &mem3.aPo
86210 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20  ol[j],.         
86220 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c       (mem3.aPool
86230 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [j-1].u.hdr.size
86240 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20  4x/4)*8-8);.    
86250 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  }.    fprintf(ou
86260 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20  t, "\n"); .  }. 
86270 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41   for(i=0; i<N_HA
86280 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  SH; i++){.    if
86290 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d  ( mem3.aiHash[i]
862a0 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
862b0 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
862c0 20 22 68 61 73 68 28 25 32 64 29 3a 22 2c 20 69   "hash(%2d):", i
862d0 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d  );.    for(j = m
862e0 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3b 20 6a  em3.aiHash[i]; j
862f0 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  >0; j=mem3.aPool
86300 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [j].u.list.next)
86310 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
86320 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20  out, " %p(%d)", 
86330 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a  &mem3.aPool[j],.
86340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d                (m
86350 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75  em3.aPool[j-1].u
86360 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38  .hdr.size4x/4)*8
86370 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  -8);.    }.    f
86380 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
86390 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74  ); .  }.  fprint
863a0 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72 3d 25  f(out, "master=%
863b0 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 74  d\n", mem3.iMast
863c0 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  er);.  fprintf(o
863d0 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64 5c  ut, "nowUsed=%d\
863e0 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38  n", mem3.nPool*8
863f0 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   - mem3.szMaster
86400 2a 38 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  *8);.  fprintf(o
86410 75 74 2c 20 22 6d 78 55 73 65 64 3d 25 64 5c 6e  ut, "mxUsed=%d\n
86420 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20  ", mem3.nPool*8 
86430 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a  - mem3.mnMaster*
86440 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  8);.  sqlite3_mu
86450 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d  tex_leave(mem3.m
86460 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 75 74  utex);.  if( out
86470 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 20 20  ==stdout ){.    
86480 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a  fflush(stdout);.
86490 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c    }else{.    fcl
864a0 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65  ose(out);.  }.#e
864b0 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  lse.  UNUSED_PAR
864c0 41 4d 45 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65  AMETER(zFilename
864d0 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  );.#endif.}../*.
864e0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
864f0 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74  is the only rout
86500 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ine in this file
86510 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 0a   with external .
86520 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a  ** linkage..**.*
86530 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c  * Populate the l
86540 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
86550 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
86560 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a  ion pointers in.
86570 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ** sqlite3Global
86580 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f  Config.m with po
86590 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f  inters to the ro
865a0 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66  utines in this f
865b0 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75  ile. The.** argu
865c0 6d 65 6e 74 73 20 73 70 65 63 69 66 79 20 74 68  ments specify th
865d0 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  e block of memor
865e0 79 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a  y to manage..**.
865f0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
86600 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62  is only called b
86610 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  y sqlite3_config
86620 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72  (), and therefor
86630 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 75  e.** is not requ
86640 69 72 65 64 20 74 6f 20 62 65 20 74 68 72 65 61  ired to be threa
86650 64 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f 74  dsafe (it is not
86660 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
86670 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74  VATE const sqlit
86680 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a  e3_mem_methods *
86690 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d  sqlite3MemGetMem
866a0 73 79 73 33 28 76 6f 69 64 29 7b 0a 20 20 73 74  sys3(void){.  st
866b0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
866c0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d  e3_mem_methods m
866d0 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d 20  empoolMethods = 
866e0 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 61  {.     memsys3Ma
866f0 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79  lloc,.     memsy
86700 73 33 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d  s3Free,.     mem
86710 73 79 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 20  sys3Realloc,.   
86720 20 20 6d 65 6d 73 79 73 33 53 69 7a 65 2c 0a 20    memsys3Size,. 
86730 20 20 20 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64      memsys3Round
86740 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33  up,.     memsys3
86750 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79  Init,.     memsy
86760 73 33 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20  s3Shutdown,.    
86770 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e   0.  };.  return
86780 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73   &mempoolMethods
86790 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
867a0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
867b0 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  SYS3 */../******
867c0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
867d0 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem3.c *********
867e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
867f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86800 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
86810 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
86820 69 6c 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a  ile mem5.c *****
86830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86850 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
86860 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a  007 October 14.*
86870 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
86880 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
86890 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
868a0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
868b0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
868c0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
868d0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
868e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
868f0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
86900 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
86910 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
86920 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
86930 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
86940 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
86950 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
86960 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
86970 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
86980 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
86990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
869a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
869b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
869c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
869d0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
869e0 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
869f0 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
86a00 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  nt a memory.** a
86a10 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
86a20 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53  tem for use by S
86a30 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  QLite. .**.** Th
86a40 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
86a50 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
86a60 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d  ion subsystem om
86a70 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f  its all.** use o
86a80 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20  f malloc(). The 
86a90 53 51 4c 69 74 65 20 75 73 65 72 20 73 75 70 70  SQLite user supp
86aa0 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20  lies a block of 
86ab0 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65  memory.** before
86ac0 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33   calling sqlite3
86ad0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72  _initialize() fr
86ae0 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74  om which allocat
86af0 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65  ions.** are made
86b00 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79   and returned by
86b10 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61   the xMalloc() a
86b20 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a  nd xRealloc() .*
86b30 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
86b40 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f  s. Once sqlite3_
86b50 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73  initialize() has
86b60 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a   been called,.**
86b70 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d   the amount of m
86b80 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20  emory available 
86b90 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69 78  to SQLite is fix
86ba0 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a  ed and cannot.**
86bb0 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a   be changed..**.
86bc0 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
86bd0 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
86be0 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
86bf0 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a  em is included.*
86c00 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f  * in the build o
86c10 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e  nly if SQLITE_EN
86c20 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 20  ABLE_MEMSYS5 is 
86c30 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24  defined..**.** $
86c40 49 64 3a 20 6d 65 6d 35 2e 63 2c 76 20 31 2e 31  Id: mem5.c,v 1.1
86c50 39 20 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a  9 2008/11/19 16:
86c60 35 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37  52:44 danielk197
86c70 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  7 Exp $.*/../*.*
86c80 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
86c90 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
86ca0 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 20 6f  ocator is used o
86cb0 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a 20 53 51 4c  nly when .** SQL
86cc0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
86cd0 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  S5 is defined..*
86ce0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
86cf0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a  ENABLE_MEMSYS5..
86d00 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20  /*.** A minimum 
86d10 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e  allocation is an
86d20 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
86d30 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
86d40 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20  ture..** Larger 
86d50 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20  allocations are 
86d60 61 6e 20 61 72 72 61 79 20 6f 66 20 74 68 65 73  an array of thes
86d70 65 20 73 74 72 75 63 74 75 72 65 73 20 77 68 65  e structures whe
86d80 72 65 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f  re the.** size o
86d90 66 20 74 68 65 20 61 72 72 61 79 20 69 73 20 61  f the array is a
86da0 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2f 0a   power of 2..*/.
86db0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d  typedef struct M
86dc0 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b  em5Link Mem5Link
86dd0 3b 0a 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e  ;.struct Mem5Lin
86de0 6b 20 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20  k {.  int next; 
86df0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
86e00 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e  f next free chun
86e10 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b  k */.  int prev;
86e20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
86e30 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72 65 65  of previous free
86e40 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   chunk */.};../*
86e50 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65  .** Maximum size
86e60 20 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69   of any allocati
86e70 6f 6e 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41  on is ((1<<LOGMA
86e80 58 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 29 2e 20  X)*mem5.nAtom). 
86e90 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 6e 41  Since.** mem5.nA
86ea0 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 74  tom is always at
86eb0 20 6c 65 61 73 74 20 38 2c 20 74 68 69 73 20 69   least 8, this i
86ec0 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 61 20 70  s not really a p
86ed0 72 61 63 74 69 63 61 6c 0a 2a 2a 20 6c 69 6d 69  ractical.** limi
86ee0 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  tation..*/.#defi
86ef0 6e 65 20 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f 2a  ne LOGMAX 30../*
86f00 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65 64 20 66  .** Masks used f
86f10 6f 72 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20  or mem5.aCtrl[] 
86f20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65  elements..*/.#de
86f30 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a  fine CTRL_LOGSIZ
86f40 45 20 20 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f  E  0x1f    /* Lo
86f50 67 32 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20  g2 Size of this 
86f60 62 6c 6f 63 6b 20 72 65 6c 61 74 69 76 65 20 74  block relative t
86f70 6f 20 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 23 64  o POW2_MIN */.#d
86f80 65 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20  efine CTRL_FREE 
86f90 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54      0x20    /* T
86fa0 72 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b  rue if not check
86fb0 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ed out */../*.**
86fc0 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74   All of the stat
86fd0 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65  ic variables use
86fe0 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65  d by this module
86ff0 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a   are collected.*
87000 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  * into a single 
87010 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20  structure named 
87020 22 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73  "mem5".  This is
87030 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20   to keep the.** 
87040 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73  static variables
87050 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74   organized and t
87060 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61  o reduce namespa
87070 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20  ce pollution.** 
87080 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65  when this module
87090 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74   is combined wit
870a0 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61  h other in the a
870b0 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  malgamation..*/.
870c0 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
870d0 44 20 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f  D struct Mem5Glo
870e0 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20  bal {.  /*.  ** 
870f0 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65  Memory available
87100 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a   for allocation.
87110 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 74 6f 6d    */.  int nAtom
87120 3b 20 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c  ;       /* Small
87130 65 73 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c  est possible all
87140 6f 63 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73  ocation in bytes
87150 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b   */.  int nBlock
87160 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ;      /* Number
87170 20 6f 66 20 6e 41 74 6f 6d 20 73 69 7a 65 64 20   of nAtom sized 
87180 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20  blocks in zPool 
87190 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 0a  */.  u8 *zPool;.
871a0 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74    .  /*.  ** Mut
871b0 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63  ex to control ac
871c0 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f  cess to the memo
871d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
871e0 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20  bsystem..  */.  
871f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
87200 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  utex;..  /*.  **
87210 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61   Performance sta
87220 74 69 73 74 69 63 73 0a 20 20 2a 2f 0a 20 20 75  tistics.  */.  u
87230 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20  64 nAlloc;      
87240 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
87250 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f 20 6d  er of calls to m
87260 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 36 34 20 74  alloc */.  u64 t
87270 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 2f  otalAlloc;     /
87280 2a 20 54 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 6d  * Total of all m
87290 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 2d 20 69 6e  alloc calls - in
872a0 63 6c 75 64 65 73 20 69 6e 74 65 72 6e 61 6c 20  cludes internal 
872b0 66 72 61 67 20 2a 2f 0a 20 20 75 36 34 20 74 6f  frag */.  u64 to
872c0 74 61 6c 45 78 63 65 73 73 3b 20 20 20 20 2f 2a  talExcess;    /*
872d0 20 54 6f 74 61 6c 20 69 6e 74 65 72 6e 61 6c 20   Total internal 
872e0 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  fragmentation */
872f0 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 4f 75  .  u32 currentOu
87300 74 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e  t;     /* Curren
87310 74 20 63 68 65 63 6b 6f 75 74 2c 20 69 6e 63 6c  t checkout, incl
87320 75 64 69 6e 67 20 69 6e 74 65 72 6e 61 6c 20 66  uding internal f
87330 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  ragmentation */.
87340 20 20 75 33 32 20 63 75 72 72 65 6e 74 43 6f 75    u32 currentCou
87350 6e 74 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e 74  nt;   /* Current
87360 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69   number of disti
87370 6e 63 74 20 63 68 65 63 6b 6f 75 74 73 20 2a 2f  nct checkouts */
87380 0a 20 20 75 33 32 20 6d 61 78 4f 75 74 3b 20 20  .  u32 maxOut;  
87390 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
873a0 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20  m instantaneous 
873b0 63 75 72 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 20  currentOut */.  
873c0 75 33 32 20 6d 61 78 43 6f 75 6e 74 3b 20 20 20  u32 maxCount;   
873d0 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69      /* Maximum i
873e0 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72  nstantaneous cur
873f0 72 65 6e 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 75  rentCount */.  u
87400 33 32 20 6d 61 78 52 65 71 75 65 73 74 3b 20 20  32 maxRequest;  
87410 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 61 6c     /* Largest al
87420 6c 6f 63 61 74 69 6f 6e 20 28 65 78 63 6c 75 73  location (exclus
87430 69 76 65 20 6f 66 20 69 6e 74 65 72 6e 61 6c 20  ive of internal 
87440 66 72 61 67 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a  frag) */.  .  /*
87450 0a 20 20 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66  .  ** Lists of f
87460 72 65 65 20 62 6c 6f 63 6b 73 20 6f 66 20 76 61  ree blocks of va
87470 72 69 6f 75 73 20 73 69 7a 65 73 2e 0a 20 20 2a  rious sizes..  *
87480 2f 0a 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69  /.  int aiFreeli
87490 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20  st[LOGMAX+1];.. 
874a0 20 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66   /*.  ** Space f
874b0 6f 72 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63  or tracking whic
874c0 68 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65  h blocks are che
874d0 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65  cked out and the
874e0 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61   size.  ** of ea
874f0 63 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62  ch block.  One b
87500 79 74 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20  yte per block.. 
87510 20 2a 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b   */.  u8 *aCtrl;
87520 0a 0a 7d 20 6d 65 6d 35 20 3d 20 7b 20 31 39 38  ..} mem5 = { 198
87530 30 34 31 36 37 20 7d 3b 0a 0a 23 64 65 66 69 6e  04167 };..#defin
87540 65 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74  e mem5 GLOBAL(st
87550 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c  ruct Mem5Global,
87560 20 6d 65 6d 35 29 0a 0a 23 64 65 66 69 6e 65 20   mem5)..#define 
87570 4d 45 4d 35 4c 49 4e 4b 28 69 64 78 29 20 28 28  MEM5LINK(idx) ((
87580 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d  Mem5Link *)(&mem
87590 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65  5.zPool[(idx)*me
875a0 6d 35 2e 6e 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a  m5.nAtom]))../*.
875b0 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68  ** Unlink the ch
875c0 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f  unk at mem5.aPoo
875d0 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69  l[i] from list i
875e0 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a  t is currently.*
875f0 2a 20 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64  * on.  It should
87600 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d   be found on mem
87610 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
87620 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69  gsize]..*/.stati
87630 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e  c void memsys5Un
87640 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20  link(int i, int 
87650 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74  iLogsize){.  int
87660 20 6e 65 78 74 2c 20 70 72 65 76 3b 0a 20 20 61   next, prev;.  a
87670 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
87680 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a  <mem5.nBlock );.
87690 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69    assert( iLogsi
876a0 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a  ze>=0 && iLogsiz
876b0 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61  e<=LOGMAX );.  a
876c0 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74  ssert( (mem5.aCt
876d0 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47  rl[i] & CTRL_LOG
876e0 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20  SIZE)==iLogsize 
876f0 29 3b 0a 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d  );..  next = MEM
87700 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a  5LINK(i)->next;.
87710 20 20 70 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e    prev = MEM5LIN
87720 4b 28 69 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66  K(i)->prev;.  if
87730 28 20 70 72 65 76 3c 30 20 29 7b 0a 20 20 20 20  ( prev<0 ){.    
87740 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
87750 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74  iLogsize] = next
87760 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d  ;.  }else{.    M
87770 45 4d 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e  EM5LINK(prev)->n
87780 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a  ext = next;.  }.
87790 20 20 69 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b    if( next>=0 ){
877a0 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65  .    MEM5LINK(ne
877b0 78 74 29 2d 3e 70 72 65 76 20 3d 20 70 72 65 76  xt)->prev = prev
877c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  ;.  }.}../*.** L
877d0 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74  ink the chunk at
877e0 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73   mem5.aPool[i] s
877f0 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65  o that is on the
87800 20 69 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65   iLogsize.** fre
87810 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  e list..*/.stati
87820 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69  c void memsys5Li
87830 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c  nk(int i, int iL
87840 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78  ogsize){.  int x
87850 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
87860 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
87870 65 6d 35 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em5.mutex) );.  
87880 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
87890 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b  i<mem5.nBlock );
878a0 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73  .  assert( iLogs
878b0 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69  ize>=0 && iLogsi
878c0 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20  ze<=LOGMAX );.  
878d0 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43  assert( (mem5.aC
878e0 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f  trl[i] & CTRL_LO
878f0 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65  GSIZE)==iLogsize
87900 20 29 3b 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c   );..  x = MEM5L
87910 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d  INK(i)->next = m
87920 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69  em5.aiFreelist[i
87930 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35  Logsize];.  MEM5
87940 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20  LINK(i)->prev = 
87950 2d 31 3b 0a 20 20 69 66 28 20 78 3e 3d 30 20 29  -1;.  if( x>=0 )
87960 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 78 3c  {.    assert( x<
87970 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20  mem5.nBlock );. 
87980 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e     MEM5LINK(x)->
87990 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20  prev = i;.  }.  
879a0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
879b0 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d  iLogsize] = i;.}
879c0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ../*.** If the S
879d0 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20  TATIC_MEM mutex 
879e0 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68  is not already h
879f0 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e  eld, obtain it n
87a00 6f 77 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a  ow. The mutex.**
87a10 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65   will already be
87a20 20 68 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 20   held (obtained 
87a30 62 79 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f  by code in mallo
87a40 63 2e 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74  c.c) if.** sqlit
87a50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
87a60 4d 65 6d 53 74 61 74 20 69 73 20 74 72 75 65 2e  MemStat is true.
87a70 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
87a80 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 76 6f 69  memsys5Enter(voi
87a90 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d){.  if( sqlite
87aa0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
87ab0 65 6d 73 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d  emstat==0 && mem
87ac0 35 2e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20  5.mutex==0 ){.  
87ad0 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73    mem5.mutex = s
87ae0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
87af0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
87b00 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20  ATIC_MEM);.  }. 
87b10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
87b20 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65 78 29  nter(mem5.mutex)
87b30 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
87b40 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 76 6f 69  memsys5Leave(voi
87b50 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d){.  sqlite3_mu
87b60 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35 2e 6d  tex_leave(mem5.m
87b70 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  utex);.}../*.** 
87b80 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
87b90 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e  of an outstandin
87ba0 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e  g allocation, in
87bb0 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20   bytes.  The.** 
87bc0 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d  size returned om
87bd0 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20 68  its the 8-byte h
87be0 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20  eader overhead. 
87bf0 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f   This only.** wo
87c00 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74  rks for chunks t
87c10 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c  hat are currentl
87c20 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a  y checked out..*
87c30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  /.static int mem
87c40 73 79 73 35 53 69 7a 65 28 76 6f 69 64 20 2a 70  sys5Size(void *p
87c50 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d  ){.  int iSize =
87c60 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   0;.  if( p ){. 
87c70 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75 38 20     int i = ((u8 
87c80 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f  *)p-mem5.zPool)/
87c90 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 20 20  mem5.nAtom;.    
87ca0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
87cb0 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b  i<mem5.nBlock );
87cc0 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d  .    iSize = mem
87cd0 35 2e 6e 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 20  5.nAtom * (1 << 
87ce0 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 43  (mem5.aCtrl[i]&C
87cf0 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a 20  TRL_LOGSIZE));. 
87d00 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 53 69 7a   }.  return iSiz
87d10 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  e;.}../*.** Find
87d20 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
87d30 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74   on the freelist
87d40 20 69 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c 69   iLogsize.  Unli
87d50 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72 79  nk that.** entry
87d60 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 73 20   and return its 
87d70 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 69  index. .*/.stati
87d80 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c  c int memsys5Unl
87d90 69 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c 6f  inkFirst(int iLo
87da0 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69 3b  gsize){.  int i;
87db0 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0a 0a  .  int iFirst;..
87dc0 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69    assert( iLogsi
87dd0 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a  ze>=0 && iLogsiz
87de0 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 69  e<=LOGMAX );.  i
87df0 20 3d 20 69 46 69 72 73 74 20 3d 20 6d 65 6d 35   = iFirst = mem5
87e00 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67  .aiFreelist[iLog
87e10 73 69 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74 28  size];.  assert(
87e20 20 69 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20 20   iFirst>=0 );.  
87e30 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20  while( i>0 ){.  
87e40 20 20 69 66 28 20 69 3c 69 46 69 72 73 74 20 29    if( i<iFirst )
87e50 20 69 46 69 72 73 74 20 3d 20 69 3b 0a 20 20 20   iFirst = i;.   
87e60 20 69 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29   i = MEM5LINK(i)
87e70 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65  ->next;.  }.  me
87e80 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72  msys5Unlink(iFir
87e90 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20  st, iLogsize);. 
87ea0 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b 0a   return iFirst;.
87eb0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
87ec0 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  a block of memor
87ed0 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42  y of at least nB
87ee0 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a  ytes in size..**
87ef0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
87f00 75 6e 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  unable..*/.stati
87f10 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d  c void *memsys5M
87f20 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74 20  allocUnsafe(int 
87f30 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 69 3b  nByte){.  int i;
87f40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
87f50 64 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e 61 50  dex of a mem5.aP
87f60 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0a 20 20  ool[] slot */.  
87f70 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20 20 20  int iBin;       
87f80 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 6d   /* Index into m
87f90 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 5d  em5.aiFreelist[]
87fa0 20 2a 2f 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53   */.  int iFullS
87fb0 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f  z;     /* Size o
87fc0 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75  f allocation rou
87fd0 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77 65 72  nded up to power
87fe0 20 6f 66 20 32 20 2a 2f 0a 20 20 69 6e 74 20 69   of 2 */.  int i
87ff0 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a 20 4c  Logsize;    /* L
88000 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a 2f 50  og2 of iFullSz/P
88010 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 2f 2a  OW2_MIN */..  /*
88020 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 74   Keep track of t
88030 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63  he maximum alloc
88040 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e 20 20  ation request.  
88050 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64  Even unfulfilled
88060 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20 61  .  ** requests a
88070 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 20  re counted */.  
88080 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e 6d  if( (u32)nByte>m
88090 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 29  em5.maxRequest )
880a0 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 65  {.    mem5.maxRe
880b0 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a 20  quest = nByte;. 
880c0 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 20 6e   }..  /* Round n
880d0 42 79 74 65 20 75 70 20 74 6f 20 74 68 65 20 6e  Byte up to the n
880e0 65 78 74 20 76 61 6c 69 64 20 70 6f 77 65 72 20  ext valid power 
880f0 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f 72 28  of two */.  for(
88100 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 6e 41 74  iFullSz=mem5.nAt
88110 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20  om, iLogsize=0; 
88120 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69  iFullSz<nByte; i
88130 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f  FullSz *= 2, iLo
88140 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a  gsize++){}..  /*
88150 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e   Make sure mem5.
88160 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73  aiFreelist[iLogs
88170 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74  ize] contains at
88180 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65 0a   least one free.
88190 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20    ** block.  If 
881a0 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74 20  not, then split 
881b0 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20 6e  a block of the n
881c0 65 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65 72  ext larger power
881d0 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 69 6e 20   of.  ** two in 
881e0 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 20  order to create 
881f0 61 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b  a new free block
88200 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73 69 7a   of size iLogsiz
88210 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 42  e..  */.  for(iB
88220 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d  in=iLogsize; mem
88230 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 42 69  5.aiFreelist[iBi
88240 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c 3d 4c 4f  n]<0 && iBin<=LO
88250 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d 0a  GMAX; iBin++){}.
88260 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f 47 4d 41    if( iBin>LOGMA
88270 58 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  X ) return 0;.  
88280 69 20 3d 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e  i = memsys5Unlin
88290 6b 46 69 72 73 74 28 69 42 69 6e 29 3b 0a 20 20  kFirst(iBin);.  
882a0 77 68 69 6c 65 28 20 69 42 69 6e 3e 69 4c 6f 67  while( iBin>iLog
882b0 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 6e 74 20  size ){.    int 
882c0 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 20 20 69 42  newSize;..    iB
882d0 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77 53 69 7a  in--;.    newSiz
882e0 65 20 3d 20 31 20 3c 3c 20 69 42 69 6e 3b 0a 20  e = 1 << iBin;. 
882f0 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 2b     mem5.aCtrl[i+
88300 6e 65 77 53 69 7a 65 5d 20 3d 20 43 54 52 4c 5f  newSize] = CTRL_
88310 46 52 45 45 20 7c 20 69 42 69 6e 3b 0a 20 20 20  FREE | iBin;.   
88320 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 2b 6e   memsys5Link(i+n
88330 65 77 53 69 7a 65 2c 20 69 42 69 6e 29 3b 0a 20  ewSize, iBin);. 
88340 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b   }.  mem5.aCtrl[
88350 69 5d 20 3d 20 69 4c 6f 67 73 69 7a 65 3b 0a 0a  i] = iLogsize;..
88360 20 20 2f 2a 20 55 70 64 61 74 65 20 61 6c 6c 6f    /* Update allo
88370 63 61 74 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  cator performanc
88380 65 20 73 74 61 74 69 73 74 69 63 73 2e 20 2a 2f  e statistics. */
88390 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b  .  mem5.nAlloc++
883a0 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c  ;.  mem5.totalAl
883b0 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a  loc += iFullSz;.
883c0 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65    mem5.totalExce
883d0 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a 20 2d 20  ss += iFullSz - 
883e0 6e 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 63 75  nByte;.  mem5.cu
883f0 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b 0a 20 20  rrentCount++;.  
88400 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20  mem5.currentOut 
88410 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 66  += iFullSz;.  if
88420 28 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 3c  ( mem5.maxCount<
88430 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e  mem5.currentCoun
88440 74 20 29 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e  t ) mem5.maxCoun
88450 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  t = mem5.current
88460 43 6f 75 6e 74 3b 0a 20 20 69 66 28 20 6d 65 6d  Count;.  if( mem
88470 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35 2e 63 75  5.maxOut<mem5.cu
88480 72 72 65 6e 74 4f 75 74 20 29 20 6d 65 6d 35 2e  rrentOut ) mem5.
88490 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 35 2e 63 75  maxOut = mem5.cu
884a0 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 20 2f 2a 20  rrentOut;..  /* 
884b0 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
884c0 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65   to the allocate
884d0 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20 72  d memory. */.  r
884e0 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65  eturn (void*)&me
884f0 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e  m5.zPool[i*mem5.
88500 6e 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  nAtom];.}../*.**
88510 20 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e   Free an outstan
88520 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ding memory allo
88530 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  cation..*/.stati
88540 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72  c void memsys5Fr
88550 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70  eeUnsafe(void *p
88560 4f 6c 64 29 7b 0a 20 20 75 33 32 20 73 69 7a 65  Old){.  u32 size
88570 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69 6e  , iLogsize;.  in
88580 74 20 69 42 6c 6f 63 6b 3b 20 20 20 20 20 20 20  t iBlock;       
88590 20 20 20 20 20 20 0a 0a 20 20 2f 2a 20 53 65 74        ..  /* Set
885a0 20 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69   iBlock to the i
885b0 6e 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f 63  ndex of the bloc
885c0 6b 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  k pointed to by 
885d0 70 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74 68  pOld in .  ** th
885e0 65 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35 2e  e array of mem5.
885f0 6e 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 6b  nAtom byte block
88600 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
88610 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a 2f  mem5.zPool..  */
88620 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 38  .  iBlock = ((u8
88630 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f   *)pOld-mem5.zPo
88640 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a  ol)/mem5.nAtom;.
88650 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
88660 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f 6c   the pointer pOl
88670 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61  d points to a va
88680 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62 6c  lid, non-free bl
88690 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ock. */.  assert
886a0 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20 69  ( iBlock>=0 && i
886b0 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f 63  Block<mem5.nBloc
886c0 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  k );.  assert( (
886d0 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e  (u8 *)pOld-mem5.
886e0 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 6e 41 74 6f  zPool)%mem5.nAto
886f0 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  m==0 );.  assert
88700 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42  ( (mem5.aCtrl[iB
88710 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46 52 45  lock] & CTRL_FRE
88720 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c 6f 67  E)==0 );..  iLog
88730 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43 74 72  size = mem5.aCtr
88740 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c  l[iBlock] & CTRL
88750 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 7a 65  _LOGSIZE;.  size
88760 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 3b 0a   = 1<<iLogsize;.
88770 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b    assert( iBlock
88780 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d 65 6d  +size-1<(u32)mem
88790 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20 6d  5.nBlock );..  m
887a0 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b  em5.aCtrl[iBlock
887b0 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a  ] |= CTRL_FREE;.
887c0 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c    mem5.aCtrl[iBl
887d0 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d 20 43  ock+size-1] |= C
887e0 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73 73 65  TRL_FREE;.  asse
887f0 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  rt( mem5.current
88800 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61 73 73  Count>0 );.  ass
88810 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e  ert( mem5.curren
88820 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65 6d 35  tOut>=(size*mem5
88830 2e 6e 41 74 6f 6d 29 20 29 3b 0a 20 20 6d 65 6d  .nAtom) );.  mem
88840 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2d 2d  5.currentCount--
88850 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  ;.  mem5.current
88860 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d 65 6d 35  Out -= size*mem5
88870 2e 6e 41 74 6f 6d 3b 0a 20 20 61 73 73 65 72 74  .nAtom;.  assert
88880 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75  ( mem5.currentOu
88890 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72  t>0 || mem5.curr
888a0 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20  entCount==0 );. 
888b0 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75   assert( mem5.cu
888c0 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 7c 7c 20  rrentCount>0 || 
888d0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3d  mem5.currentOut=
888e0 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43  =0 );..  mem5.aC
888f0 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54  trl[iBlock] = CT
88900 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69  RL_FREE | iLogsi
88910 7a 65 3b 0a 20 20 77 68 69 6c 65 28 20 69 4c 6f  ze;.  while( iLo
88920 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 20 29 7b 0a  gsize<LOGMAX ){.
88930 20 20 20 20 69 6e 74 20 69 42 75 64 64 79 3b 0a      int iBuddy;.
88940 20 20 20 20 69 66 28 20 28 69 42 6c 6f 63 6b 3e      if( (iBlock>
88950 3e 69 4c 6f 67 73 69 7a 65 29 20 26 20 31 20 29  >iLogsize) & 1 )
88960 7b 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d  {.      iBuddy =
88970 20 69 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a   iBlock - size;.
88980 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
88990 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b   iBuddy = iBlock
889a0 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20   + size;.    }. 
889b0 20 20 20 61 73 73 65 72 74 28 20 69 42 75 64 64     assert( iBudd
889c0 79 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  y>=0 );.    if( 
889d0 28 69 42 75 64 64 79 2b 28 31 3c 3c 69 4c 6f 67  (iBuddy+(1<<iLog
889e0 73 69 7a 65 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f  size))>mem5.nBlo
889f0 63 6b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ck ) break;.    
88a00 69 66 28 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69  if( mem5.aCtrl[i
88a10 42 75 64 64 79 5d 21 3d 28 43 54 52 4c 5f 46 52  Buddy]!=(CTRL_FR
88a20 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 29 20 29  EE | iLogsize) )
88a30 20 62 72 65 61 6b 3b 0a 20 20 20 20 6d 65 6d 73   break;.    mems
88a40 79 73 35 55 6e 6c 69 6e 6b 28 69 42 75 64 64 79  ys5Unlink(iBuddy
88a50 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 20  , iLogsize);.   
88a60 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b 0a 20 20 20   iLogsize++;.   
88a70 20 69 66 28 20 69 42 75 64 64 79 3c 69 42 6c 6f   if( iBuddy<iBlo
88a80 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35  ck ){.      mem5
88a90 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d  .aCtrl[iBuddy] =
88aa0 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f   CTRL_FREE | iLo
88ab0 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d  gsize;.      mem
88ac0 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
88ad0 3d 20 30 3b 0a 20 20 20 20 20 20 69 42 6c 6f 63  = 0;.      iBloc
88ae0 6b 20 3d 20 69 42 75 64 64 79 3b 0a 20 20 20 20  k = iBuddy;.    
88af0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
88b00 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
88b10 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c  = CTRL_FREE | iL
88b20 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65  ogsize;.      me
88b30 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d  m5.aCtrl[iBuddy]
88b40 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
88b50 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20  size *= 2;.  }. 
88b60 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 42 6c   memsys5Link(iBl
88b70 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a  ock, iLogsize);.
88b80 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
88b90 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f  e nBytes of memo
88ba0 72 79 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ry.*/.static voi
88bb0 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63  d *memsys5Malloc
88bc0 28 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20  (int nBytes){.  
88bd0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
88be0 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 42 79 74   = 0;.  if( nByt
88bf0 65 73 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73  es>0 ){.    mems
88c00 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 20 20  ys5Enter();.    
88c10 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f  p = memsys5Mallo
88c20 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b  cUnsafe(nBytes);
88c30 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76  .    memsys5Leav
88c40 65 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  e();.  }.  retur
88c50 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a  n (void*)p; .}..
88c60 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72  /*.** Free memor
88c70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  y..*/.static voi
88c80 64 20 6d 65 6d 73 79 73 35 46 72 65 65 28 76 6f  d memsys5Free(vo
88c90 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 69  id *pPrior){.  i
88ca0 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a  f( pPrior==0 ){.
88cb0 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20 72  assert(0);.    r
88cc0 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6d 65 6d  eturn;.  }.  mem
88cd0 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d  sys5Enter();.  m
88ce0 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65  emsys5FreeUnsafe
88cf0 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73  (pPrior);.  mems
88d00 79 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a  ys5Leave();  .}.
88d10 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
88d20 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
88d30 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
88d40 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69  ocation.*/.stati
88d50 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52  c void *memsys5R
88d60 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
88d70 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29  ior, int nBytes)
88d80 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20  {.  int nOld;.  
88d90 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 70  void *p;.  if( p
88da0 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20  Prior==0 ){.    
88db0 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 35 4d 61  return memsys5Ma
88dc0 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20  lloc(nBytes);.  
88dd0 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d  }.  if( nBytes<=
88de0 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35  0 ){.    memsys5
88df0 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Free(pPrior);.  
88e00 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
88e10 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 35    nOld = memsys5
88e20 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Size(pPrior);.  
88e30 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64  if( nBytes<=nOld
88e40 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
88e50 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d  Prior;.  }.  mem
88e60 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 70  sys5Enter();.  p
88e70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63   = memsys5Malloc
88e80 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a  Unsafe(nBytes);.
88e90 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d    if( p ){.    m
88ea0 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c  emcpy(p, pPrior,
88eb0 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 6d 65 6d 73   nOld);.    mems
88ec0 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 70 50  ys5FreeUnsafe(pP
88ed0 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d  rior);.  }.  mem
88ee0 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 72  sys5Leave();.  r
88ef0 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
88f00 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71  * Round up a req
88f10 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65  uest size to the
88f20 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f   next valid allo
88f30 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a  cation size..*/.
88f40 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
88f50 73 35 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29  s5Roundup(int n)
88f60 7b 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b  {.  int iFullSz;
88f70 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d  .  for(iFullSz=m
88f80 65 6d 35 2e 6e 41 74 6f 6d 3b 20 69 46 75 6c 6c  em5.nAtom; iFull
88f90 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d  Sz<n; iFullSz *=
88fa0 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46   2);.  return iF
88fb0 75 6c 6c 53 7a 3b 0a 7d 0a 0a 73 74 61 74 69 63  ullSz;.}..static
88fc0 20 69 6e 74 20 6d 65 6d 73 79 73 35 4c 6f 67 28   int memsys5Log(
88fd0 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 69  int iValue){.  i
88fe0 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 6f 72 28 69  nt iLog;.  for(i
88ff0 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 4c 6f 67 29  Log=0; (1<<iLog)
89000 3c 69 56 61 6c 75 65 3b 20 69 4c 6f 67 2b 2b 29  <iValue; iLog++)
89010 3b 0a 20 20 72 65 74 75 72 6e 20 69 4c 6f 67 3b  ;.  return iLog;
89020 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
89030 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
89040 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
89050 6d 65 6d 73 79 73 35 49 6e 69 74 28 76 6f 69 64  memsys5Init(void
89060 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 69 6e   *NotUsed){.  in
89070 74 20 69 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74  t ii;.  int nByt
89080 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  e = sqlite3Globa
89090 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 3b 0a 20  lConfig.nHeap;. 
890a0 20 75 38 20 2a 7a 42 79 74 65 20 3d 20 28 75 38   u8 *zByte = (u8
890b0 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   *)sqlite3Global
890c0 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20  Config.pHeap;.  
890d0 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20  int nMinLog;    
890e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
890f0 4c 6f 67 20 6f 66 20 6d 69 6e 69 6d 75 6d 20 61  Log of minimum a
89100 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69  llocation size i
89110 6e 20 62 79 74 65 73 2a 2f 0a 20 20 69 6e 74 20  n bytes*/.  int 
89120 69 4f 66 66 73 65 74 3b 0a 0a 20 20 55 4e 55 53  iOffset;..  UNUS
89130 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
89140 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 21 7a  Used);..  if( !z
89150 42 79 74 65 20 29 7b 0a 20 20 20 20 72 65 74 75  Byte ){.    retu
89160 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
89170 0a 20 20 7d 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20  .  }..  nMinLog 
89180 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c  = memsys5Log(sql
89190 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
891a0 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e  .mnReq);.  mem5.
891b0 6e 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 6e  nAtom = (1<<nMin
891c0 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 28  Log);.  while( (
891d0 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 4c  int)sizeof(Mem5L
891e0 69 6e 6b 29 3e 6d 65 6d 35 2e 6e 41 74 6f 6d 20  ink)>mem5.nAtom 
891f0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6e 41 74 6f  ){.    mem5.nAto
89200 6d 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20 3c  m = mem5.nAtom <
89210 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 35  < 1;.  }..  mem5
89220 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 65  .nBlock = (nByte
89230 20 2f 20 28 6d 65 6d 35 2e 6e 41 74 6f 6d 2b 73   / (mem5.nAtom+s
89240 69 7a 65 6f 66 28 75 38 29 29 29 3b 0a 20 20 6d  izeof(u8)));.  m
89250 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79 74  em5.zPool = zByt
89260 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 20  e;.  mem5.aCtrl 
89270 3d 20 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a 50  = (u8 *)&mem5.zP
89280 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 2a  ool[mem5.nBlock*
89290 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 0a 20 20  mem5.nAtom];..  
892a0 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c 4f  for(ii=0; ii<=LO
892b0 47 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  GMAX; ii++){.   
892c0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
892d0 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a  [ii] = -1;.  }..
892e0 20 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20    iOffset = 0;. 
892f0 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b 20   for(ii=LOGMAX; 
89300 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 20  ii>=0; ii--){.  
89310 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 28    int nAlloc = (
89320 31 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28 20  1<<ii);.    if( 
89330 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29  (iOffset+nAlloc)
89340 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 7b  <=mem5.nBlock ){
89350 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 72  .      mem5.aCtr
89360 6c 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69 20  l[iOffset] = ii 
89370 7c 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 20  | CTRL_FREE;.   
89380 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69     memsys5Link(i
89390 4f 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20 20  Offset, ii);.   
893a0 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e 41     iOffset += nA
893b0 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  lloc;.    }.    
893c0 61 73 73 65 72 74 28 28 69 4f 66 66 73 65 74 2b  assert((iOffset+
893d0 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 6c  nAlloc)>mem5.nBl
893e0 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  ock);.  }..  ret
893f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
89400 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61  ../*.** Deinitia
89410 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
89420 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
89430 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e   memsys5Shutdown
89440 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b  (void *NotUsed){
89450 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
89460 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
89470 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  return;.}../*.**
89480 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69   Open the file i
89490 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69  ndicated and wri
894a0 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20  te a log of all 
894b0 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a  unfreed memory .
894c0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69  ** allocations i
894d0 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f  nto that log..*/
894e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
894f0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73  void sqlite3Mems
89500 79 73 35 44 75 6d 70 28 63 6f 6e 73 74 20 63 68  ys5Dump(const ch
89510 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
89520 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
89530 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b  BUG.  FILE *out;
89540 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a  .  int i, j, n;.
89550 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0a 0a    int nMinLog;..
89560 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d    if( zFilename=
89570 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b  =0 || zFilename[
89580 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74  0]==0 ){.    out
89590 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c   = stdout;.  }el
895a0 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f  se{.    out = fo
895b0 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22  pen(zFilename, "
895c0 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74  w");.    if( out
895d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72  ==0 ){.      fpr
895e0 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a  intf(stderr, "**
895f0 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75   Unable to outpu
89600 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f  t memory debug o
89610 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a  utput log: %s **
89620 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
89630 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65             zFile
89640 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74  name);.      ret
89650 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
89660 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b   memsys5Enter();
89670 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d  .  nMinLog = mem
89680 73 79 73 35 4c 6f 67 28 6d 65 6d 35 2e 6e 41 74  sys5Log(mem5.nAt
89690 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  om);.  for(i=0; 
896a0 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 20 69 2b 6e  i<=LOGMAX && i+n
896b0 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b  MinLog<32; i++){
896c0 0a 20 20 20 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d  .    for(n=0, j=
896d0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
896e0 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45  i]; j>=0; j = ME
896f0 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c  M5LINK(j)->next,
89700 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 66 70 72 69   n++){}.    fpri
89710 6e 74 66 28 6f 75 74 2c 20 22 66 72 65 65 6c 69  ntf(out, "freeli
89720 73 74 20 69 74 65 6d 73 20 6f 66 20 73 69 7a 65  st items of size
89730 20 25 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d 35   %d: %d\n", mem5
89740 2e 6e 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 3b  .nAtom << i, n);
89750 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f  .  }.  fprintf(o
89760 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f 63  ut, "mem5.nAlloc
89770 20 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22         = %llu\n"
89780 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b 0a  , mem5.nAlloc);.
89790 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
897a0 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20  mem5.totalAlloc 
897b0 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d    = %llu\n", mem
897c0 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a 20  5.totalAlloc);. 
897d0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
897e0 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20  em5.totalExcess 
897f0 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35   = %llu\n", mem5
89800 2e 74 6f 74 61 6c 45 78 63 65 73 73 29 3b 0a 20  .totalExcess);. 
89810 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
89820 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 20  em5.currentOut  
89830 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63   = %u\n", mem5.c
89840 75 72 72 65 6e 74 4f 75 74 29 3b 0a 20 20 66 70  urrentOut);.  fp
89850 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35  rintf(out, "mem5
89860 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 3d 20  .currentCount = 
89870 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72  %u\n", mem5.curr
89880 65 6e 74 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72  entCount);.  fpr
89890 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e  intf(out, "mem5.
898a0 6d 61 78 4f 75 74 20 20 20 20 20 20 20 3d 20 25  maxOut       = %
898b0 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f 75  u\n", mem5.maxOu
898c0 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  t);.  fprintf(ou
898d0 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e  t, "mem5.maxCoun
898e0 74 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d  t     = %u\n", m
898f0 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a 20  em5.maxCount);. 
89900 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d   fprintf(out, "m
89910 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 20  em5.maxRequest  
89920 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d   = %u\n", mem5.m
89930 61 78 52 65 71 75 65 73 74 29 3b 0a 20 20 6d 65  axRequest);.  me
89940 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20  msys5Leave();.  
89950 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20  if( out==stdout 
89960 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 74  ){.    fflush(st
89970 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  dout);.  }else{.
89980 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b      fclose(out);
89990 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55  .  }.#else.  UNU
899a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 46  SED_PARAMETER(zF
899b0 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66  ilename);.#endif
899c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
899d0 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e  outine is the on
899e0 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68  ly routine in th
899f0 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74  is file with ext
89a00 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67  ernal .** linkag
89a10 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20  e. It returns a 
89a20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
89a30 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  tic sqlite3_mem_
89a40 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 63  methods.** struc
89a50 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  t populated with
89a60 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74   the memsys5 met
89a70 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hods..*/.SQLITE_
89a80 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71  PRIVATE const sq
89a90 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
89aa0 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74  s *sqlite3MemGet
89ab0 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a 20  Memsys5(void){. 
89ac0 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
89ad0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
89ae0 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73  s memsys5Methods
89af0 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73   = {.     memsys
89b00 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65  5Malloc,.     me
89b10 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 20  msys5Free,.     
89b20 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c 0a  memsys5Realloc,.
89b30 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a 65       memsys5Size
89b40 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 6f  ,.     memsys5Ro
89b50 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73  undup,.     mems
89b60 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65  ys5Init,.     me
89b70 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a 20  msys5Shutdown,. 
89b80 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74      0.  };.  ret
89b90 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 68  urn &memsys5Meth
89ba0 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ods;.}..#endif /
89bb0 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
89bc0 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a  MEMSYS5 */../***
89bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
89be0 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a  of mem5.c ******
89bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
89c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
89c30 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 2a  n file mutex.c *
89c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
89c70 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34  * 2007 August 14
89c80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
89c90 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
89ca0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
89cb0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
89cc0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
89cd0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
89ce0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
89cf0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
89d00 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
89d10 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
89d20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
89d30 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
89d40 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
89d50 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
89d60 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
89d70 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
89d80 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
89d90 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
89da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
89dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
89de0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
89df0 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
89e00 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
89e10 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a  ment mutexes..**
89e20 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
89e30 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74  ntains code that
89e40 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73   is common acros
89e50 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c  s all mutex impl
89e60 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a 2a  ementations...**
89e70 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 63  .** $Id: mutex.c
89e80 2c 76 20 31 2e 33 30 20 32 30 30 39 2f 30 32 2f  ,v 1.30 2009/02/
89e90 31 37 20 31 36 3a 32 39 3a 31 31 20 64 61 6e 69  17 16:29:11 dani
89ea0 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
89eb0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
89ec0 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a  _MUTEX_OMIT./*.*
89ed0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
89ee0 20 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 0a 2a   mutex system..*
89ef0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
89f00 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65   int sqlite3Mute
89f10 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20  xInit(void){ .  
89f20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
89f30 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  OK;.  if( sqlite
89f40 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
89f50 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  oreMutex ){.    
89f60 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62  if( !sqlite3Glob
89f70 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
89f80 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b 0a 20 20  MutexAlloc ){.  
89f90 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 78 4d      /* If the xM
89fa0 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74 68 6f 64  utexAlloc method
89fb0 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65   has not been se
89fc0 74 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65 72  t, then the user
89fd0 20 64 69 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a   did not.      *
89fe0 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d 75 74 65  * install a mute
89ff0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
8a000 20 76 69 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e   via sqlite3_con
8a010 66 69 67 28 29 20 70 72 69 6f 72 20 74 6f 20 0a  fig() prior to .
8a020 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33        ** sqlite3
8a030 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 62 65  _initialize() be
8a040 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54 68 69 73  ing called. This
8a050 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 70 6f   block copies po
8a060 69 6e 74 65 72 73 20 74 6f 0a 20 20 20 20 20 20  inters to.      
8a070 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 69  ** the default i
8a080 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e  mplementation in
8a090 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c  to the sqlite3Gl
8a0a0 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63  obalConfig struc
8a0b0 74 75 72 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  ture..      **. 
8a0c0 20 20 20 20 20 2a 2a 20 54 68 65 20 64 61 6e 67       ** The dang
8a0d0 65 72 20 69 73 20 74 68 61 74 20 61 6c 74 68 6f  er is that altho
8a0e0 75 67 68 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ugh sqlite3_conf
8a0f0 69 67 28 29 20 69 73 20 6e 6f 74 20 61 20 74 68  ig() is not a th
8a100 72 65 61 64 73 61 66 65 0a 20 20 20 20 20 20 2a  readsafe.      *
8a110 2a 20 41 50 49 2c 20 73 71 6c 69 74 65 33 5f 69  * API, sqlite3_i
8a120 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 2c 20  nitialize() is, 
8a130 61 6e 64 20 73 6f 20 6d 75 6c 74 69 70 6c 65 20  and so multiple 
8a140 74 68 72 65 61 64 73 20 6d 61 79 20 62 65 0a 20  threads may be. 
8a150 20 20 20 20 20 2a 2a 20 61 74 74 65 6d 70 74 69       ** attempti
8a160 6e 67 20 74 6f 20 72 75 6e 20 74 68 69 73 20 66  ng to run this f
8a170 75 6e 63 74 69 6f 6e 20 73 69 6d 75 6c 74 61 6e  unction simultan
8a180 65 6f 75 73 6c 79 2e 20 54 6f 20 67 75 61 72 64  eously. To guard
8a190 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20   write.      ** 
8a1a0 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 73 71  access to the sq
8a1b0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
8a1c0 67 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65  g structure, the
8a1d0 20 27 4d 41 53 54 45 52 27 20 73 74 61 74 69 63   'MASTER' static
8a1e0 20 6d 75 74 65 78 0a 20 20 20 20 20 20 2a 2a 20   mutex.      ** 
8a1f0 69 73 20 6f 62 74 61 69 6e 65 64 20 62 65 66 6f  is obtained befo
8a200 72 65 20 6d 6f 64 69 66 79 69 6e 67 20 69 74 2e  re modifying it.
8a210 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
8a220 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
8a230 74 68 6f 64 73 20 2a 70 20 3d 20 73 71 6c 69 74  thods *p = sqlit
8a240 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 29  e3DefaultMutex()
8a250 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
8a260 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 20 3d  mutex *pMaster =
8a270 20 30 3b 0a 20 20 0a 20 20 20 20 20 20 72 63 20   0;.  .      rc 
8a280 3d 20 70 2d 3e 78 4d 75 74 65 78 49 6e 69 74 28  = p->xMutexInit(
8a290 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
8a2a0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
8a2b0 20 20 20 20 20 20 70 4d 61 73 74 65 72 20 3d 20        pMaster = 
8a2c0 70 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 53  p->xMutexAlloc(S
8a2d0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
8a2e0 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20  IC_MASTER);.    
8a2f0 20 20 20 20 61 73 73 65 72 74 28 70 4d 61 73 74      assert(pMast
8a300 65 72 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  er);.        p->
8a310 78 4d 75 74 65 78 45 6e 74 65 72 28 70 4d 61 73  xMutexEnter(pMas
8a320 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 61 73  ter);.        as
8a330 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f  sert( sqlite3Glo
8a340 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
8a350 78 4d 75 74 65 78 41 6c 6c 6f 63 3d 3d 30 20 0a  xMutexAlloc==0 .
8a360 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
8a370 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
8a380 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
8a390 41 6c 6c 6f 63 3d 3d 70 2d 3e 78 4d 75 74 65 78  Alloc==p->xMutex
8a3a0 41 6c 6c 6f 63 0a 20 20 20 20 20 20 20 20 29 3b  Alloc.        );
8a3b0 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71  .        if( !sq
8a3c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
8a3d0 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c  g.mutex.xMutexAl
8a3e0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  loc ){.         
8a3f0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
8a400 6e 66 69 67 2e 6d 75 74 65 78 20 3d 20 2a 70 3b  nfig.mutex = *p;
8a410 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
8a420 20 20 20 70 2d 3e 78 4d 75 74 65 78 4c 65 61 76     p->xMutexLeav
8a430 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  e(pMaster);.    
8a440 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
8a450 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
8a460 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
8a470 74 65 78 2e 78 4d 75 74 65 78 49 6e 69 74 28 29  tex.xMutexInit()
8a480 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
8a490 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
8a4a0 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20  ** Shutdown the 
8a4b0 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 20 54 68  mutex system. Th
8a4c0 69 73 20 63 61 6c 6c 20 66 72 65 65 73 20 72 65  is call frees re
8a4d0 73 6f 75 72 63 65 73 20 61 6c 6c 6f 63 61 74 65  sources allocate
8a4e0 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d  d by.** sqlite3M
8a4f0 75 74 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53  utexInit()..*/.S
8a500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
8a510 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e  t sqlite3MutexEn
8a520 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72  d(void){.  int r
8a530 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
8a540 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
8a550 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
8a560 4d 75 74 65 78 45 6e 64 20 29 7b 0a 20 20 20 20  MutexEnd ){.    
8a570 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  rc = sqlite3Glob
8a580 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
8a590 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a  MutexEnd();.  }.
8a5a0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
8a5b0 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61  /*.** Retrieve a
8a5c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
8a5d0 61 74 69 63 20 6d 75 74 65 78 20 6f 72 20 61 6c  atic mutex or al
8a5e0 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 64 79 6e  locate a new dyn
8a5f0 61 6d 69 63 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  amic one..*/.SQL
8a600 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
8a610 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d  mutex *sqlite3_m
8a620 75 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69  utex_alloc(int i
8a630 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  d){.#ifndef SQLI
8a640 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
8a650 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
8a660 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
8a670 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
8a680 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47   return sqlite3G
8a690 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
8a6a0 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64  x.xMutexAlloc(id
8a6b0 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
8a6c0 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74  VATE sqlite3_mut
8a6d0 65 78 20 2a 73 71 6c 69 74 65 33 4d 75 74 65 78  ex *sqlite3Mutex
8a6e0 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20  Alloc(int id){. 
8a6f0 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f   if( !sqlite3Glo
8a700 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
8a710 75 74 65 78 20 29 7b 0a 20 20 20 20 72 65 74 75  utex ){.    retu
8a720 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
8a730 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
8a740 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
8a750 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a  texAlloc(id);.}.
8a760 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79  ./*.** Free a dy
8a770 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a  namic mutex..*/.
8a780 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
8a790 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72  sqlite3_mutex_fr
8a7a0 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ee(sqlite3_mutex
8a7b0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
8a7c0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
8a7d0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
8a7e0 4d 75 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20  MutexFree(p);.  
8a7f0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69  }.}../*.** Obtai
8a800 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49  n the mutex p. I
8a810 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72  f some other thr
8a820 65 61 64 20 61 6c 72 65 61 64 79 20 68 61 73 20  ead already has 
8a830 74 68 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b  the mutex, block
8a840 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e  .** until it can
8a850 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f   be obtained..*/
8a860 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
8a870 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
8a880 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74  nter(sqlite3_mut
8a890 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  ex *p){.  if( p 
8a8a0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
8a8b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
8a8c0 2e 78 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b  .xMutexEnter(p);
8a8d0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62  .  }.}../*.** Ob
8a8e0 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70  tain the mutex p
8a8f0 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
8a900 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
8a910 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  K. Otherwise, if
8a920 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65   another.** thre
8a930 61 64 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74  ad holds the mut
8a940 65 78 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74  ex and it cannot
8a950 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65   be obtained, re
8a960 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
8a970 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
8a980 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  int sqlite3_mute
8a990 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75  x_try(sqlite3_mu
8a9a0 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  tex *p){.  int r
8a9b0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
8a9c0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65   if( p ){.    re
8a9d0 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62  turn sqlite3Glob
8a9e0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
8a9f0 4d 75 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d  MutexTry(p);.  }
8aa00 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
8aa10 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
8aa20 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
8aa30 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
8aa40 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 20   mutex that was 
8aa50 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e  previously.** en
8aa60 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
8aa70 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62  e thread.  The b
8aa80 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
8aa90 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
8aaa0 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  x .** is not cur
8aab0 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20  rently entered. 
8aac0 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  If a NULL pointe
8aad0 72 20 69 73 20 70 61 73 73 65 64 20 61 73 20 61  r is passed as a
8aae0 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68  n argument.** th
8aaf0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
8ab00 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
8ab10 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
8ab20 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
8ab30 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
8ab40 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
8ab50 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
8ab60 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
8ab70 78 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d  xLeave(p);.  }.}
8ab80 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
8ab90 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
8aba0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
8abb0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
8abc0 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74  x_notheld() rout
8abd0 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e  ine are.** inten
8abe0 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69  ded for use insi
8abf0 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
8ac00 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ements..*/.SQLIT
8ac10 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
8ac20 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c  3_mutex_held(sql
8ac30 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
8ac40 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c    return p==0 ||
8ac50 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
8ac60 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
8ac70 78 48 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49  xHeld(p);.}.SQLI
8ac80 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
8ac90 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
8aca0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
8acb0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  p){.  return p==
8acc0 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  0 || sqlite3Glob
8acd0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
8ace0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b  MutexNotheld(p);
8acf0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  .}.#endif..#endi
8ad00 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
8ad10 5f 4d 55 54 45 58 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _MUTEX */../****
8ad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
8ad30 66 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a  f mutex.c ******
8ad40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ad50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
8ad70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
8ad80 20 66 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70   file mutex_noop
8ad90 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
8ada0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8adb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
8adc0 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37   2008 October 07
8add0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
8ade0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
8adf0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
8ae00 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
8ae10 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
8ae20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
8ae30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
8ae40 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
8ae50 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
8ae60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
8ae70 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
8ae80 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
8ae90 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
8aea0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
8aeb0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
8aec0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
8aed0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
8aee0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
8aef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8af00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8af10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8af20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
8af30 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
8af40 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
8af50 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
8af60 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a  ment mutexes..**
8af70 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  .** This impleme
8af80 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20  ntation in this 
8af90 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72  file does not pr
8afa0 6f 76 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c  ovide any mutual
8afb0 0a 2a 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e  .** exclusion an
8afc0 64 20 69 73 20 74 68 75 73 20 73 75 69 74 61 62  d is thus suitab
8afd0 6c 65 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20  le for use only 
8afe0 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  in applications.
8aff0 2a 2a 20 74 68 61 74 20 75 73 65 20 53 51 4c 69  ** that use SQLi
8b000 74 65 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74  te in a single t
8b010 68 72 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74  hread.  The rout
8b020 69 6e 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20  ines defined.** 
8b030 68 65 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68  here are place-h
8b040 6f 6c 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61  olders.  Applica
8b050 74 69 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69  tions can substi
8b060 74 75 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20  tute working.** 
8b070 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61  mutex routines a
8b080 74 20 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69  t start-time usi
8b090 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20  ng the.**.**    
8b0a0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
8b0b0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
8b0c0 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69  TEX,...).**.** i
8b0d0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
8b0e0 49 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  If compiled with
8b0f0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74   SQLITE_DEBUG, t
8b100 68 65 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  hen additional l
8b110 6f 67 69 63 20 69 73 20 69 6e 73 65 72 74 65 64  ogic is inserted
8b120 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72  .** that does er
8b130 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20  ror checking on 
8b140 6d 75 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20  mutexes to make 
8b150 73 75 72 65 20 74 68 65 79 20 61 72 65 20 62 65  sure they are be
8b160 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f  ing.** called co
8b170 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24  rrectly..**.** $
8b180 49 64 3a 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63  Id: mutex_noop.c
8b190 2c 76 20 31 2e 33 20 32 30 30 38 2f 31 32 2f 30  ,v 1.3 2008/12/0
8b1a0 35 20 31 37 3a 31 37 3a 30 38 20 64 72 68 20 45  5 17:17:08 drh E
8b1b0 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 64 65  xp $.*/...#if de
8b1c0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54  fined(SQLITE_MUT
8b1d0 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 66  EX_NOOP) && !def
8b1e0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
8b1f0 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 62 20 72 6f  G)./*.** Stub ro
8b200 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6d  utines for all m
8b210 75 74 65 78 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a  utex methods..**
8b220 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
8b230 73 20 70 72 6f 76 69 64 65 20 6e 6f 20 6d 75 74  s provide no mut
8b240 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20 6f 72  ual exclusion or
8b250 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 2e   error checking.
8b260 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
8b270 6f 6f 70 4d 75 74 65 78 48 65 6c 64 28 73 71 6c  oopMutexHeld(sql
8b280 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20  ite3_mutex *p){ 
8b290 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74  return 1; }.stat
8b2a0 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78  ic int noopMutex
8b2b0 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f  Notheld(sqlite3_
8b2c0 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72  mutex *p){ retur
8b2d0 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  n 1; }.static in
8b2e0 74 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 28  t noopMutexInit(
8b2f0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
8b300 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69  LITE_OK; }.stati
8b310 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 45  c int noopMutexE
8b320 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  nd(void){ return
8b330 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
8b340 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74  atic sqlite3_mut
8b350 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c  ex *noopMutexAll
8b360 6f 63 28 69 6e 74 20 69 64 29 7b 20 72 65 74 75  oc(int id){ retu
8b370 72 6e 20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  rn (sqlite3_mute
8b380 78 2a 29 38 3b 20 7d 0a 73 74 61 74 69 63 20 76  x*)8; }.static v
8b390 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65  oid noopMutexFre
8b3a0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
8b3b0 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73  *p){ return; }.s
8b3c0 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d  tatic void noopM
8b3d0 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65  utexEnter(sqlite
8b3e0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74  3_mutex *p){ ret
8b3f0 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  urn; }.static in
8b400 74 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 28 73  t noopMutexTry(s
8b410 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
8b420 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
8b430 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  OK; }.static voi
8b440 64 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65  d noopMutexLeave
8b450 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
8b460 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 53  p){ return; }..S
8b470 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
8b480 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
8b490 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61  ods *sqlite3Defa
8b4a0 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  ultMutex(void){.
8b4b0 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
8b4c0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
8b4d0 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 6e 6f  Mutex = {.    no
8b4e0 6f 70 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20  opMutexInit,.   
8b4f0 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0a 20   noopMutexEnd,. 
8b500 20 20 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f     noopMutexAllo
8b510 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  c,.    noopMutex
8b520 46 72 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75  Free,.    noopMu
8b530 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 6e 6f  texEnter,.    no
8b540 6f 70 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20  opMutexTry,.    
8b550 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 2c 0a  noopMutexLeave,.
8b560 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 48 65  .    noopMutexHe
8b570 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65  ld,.    noopMute
8b580 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20  xNotheld.  };.. 
8b590 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b   return &sMutex;
8b5a0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  .}.#endif /* def
8b5b0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45  ined(SQLITE_MUTE
8b5c0 58 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69  X_NOOP) && !defi
8b5d0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
8b5e0 29 20 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65  ) */..#if define
8b5f0 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e  d(SQLITE_MUTEX_N
8b600 4f 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28  OOP) && defined(
8b610 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a  SQLITE_DEBUG)./*
8b620 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c  .** In this impl
8b630 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 72 72 6f  ementation, erro
8b640 72 20 63 68 65 63 6b 69 6e 67 20 69 73 20 70 72  r checking is pr
8b650 6f 76 69 64 65 64 20 66 6f 72 20 74 65 73 74 69  ovided for testi
8b660 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62 75 67 67  ng.** and debugg
8b670 69 6e 67 20 70 75 72 70 6f 73 65 73 2e 20 20 54  ing purposes.  T
8b680 68 65 20 6d 75 74 65 78 65 73 20 73 74 69 6c 6c  he mutexes still
8b690 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20   do not provide 
8b6a0 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 6c 20 65 78  any.** mutual ex
8b6b0 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a  clusion..*/../*.
8b6c0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a  ** The mutex obj
8b6d0 65 63 74 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  ect.*/.struct sq
8b6e0 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20  lite3_mutex {.  
8b6f0 69 6e 74 20 69 64 3b 20 20 20 20 20 2f 2a 20 54  int id;     /* T
8b700 68 65 20 6d 75 74 65 78 20 74 79 70 65 20 2a 2f  he mutex type */
8b710 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 2f  .  int cnt;    /
8b720 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
8b730 69 65 73 20 77 69 74 68 6f 75 74 20 61 20 6d 61  ies without a ma
8b740 74 63 68 69 6e 67 20 6c 65 61 76 65 20 2a 2f 0a  tching leave */.
8b750 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  };../*.** The sq
8b760 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
8b770 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
8b780 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72  utex_notheld() r
8b790 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e  outine are.** in
8b7a0 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
8b7b0 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
8b7c0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74  tatements..*/.st
8b7d0 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75  atic int debugMu
8b7e0 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f  texHeld(sqlite3_
8b7f0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74  mutex *p){.  ret
8b800 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63  urn p==0 || p->c
8b810 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 69 63 20 69  nt>0;.}.static i
8b820 6e 74 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74  nt debugMutexNot
8b830 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
8b840 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ex *p){.  return
8b850 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3d   p==0 || p->cnt=
8b860 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  =0;.}../*.** Ini
8b870 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
8b880 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74  itialize the mut
8b890 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  ex subsystem..*/
8b8a0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75  .static int debu
8b8b0 67 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29  gMutexInit(void)
8b8c0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
8b8d0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74  OK; }.static int
8b8e0 20 64 65 62 75 67 4d 75 74 65 78 45 6e 64 28 76   debugMutexEnd(v
8b8f0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
8b900 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK; }../*.**
8b910 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
8b920 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69  ex_alloc() routi
8b930 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e  ne allocates a n
8b940 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20  ew.** mutex and 
8b950 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
8b960 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20  r to it.  If it 
8b970 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20  returns NULL.** 
8b980 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
8b990 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f  a mutex could no
8b9a0 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20  t be allocated. 
8b9b0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
8b9c0 65 33 5f 6d 75 74 65 78 20 2a 64 65 62 75 67 4d  e3_mutex *debugM
8b9d0 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64  utexAlloc(int id
8b9e0 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
8b9f0 74 65 33 5f 6d 75 74 65 78 20 61 53 74 61 74 69  te3_mutex aStati
8ba00 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f  c[6];.  sqlite3_
8ba10 6d 75 74 65 78 20 2a 70 4e 65 77 20 3d 20 30 3b  mutex *pNew = 0;
8ba20 0a 20 20 73 77 69 74 63 68 28 20 69 64 20 29 7b  .  switch( id ){
8ba30 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
8ba40 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20  _MUTEX_FAST:.   
8ba50 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54   case SQLITE_MUT
8ba60 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a  EX_RECURSIVE: {.
8ba70 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
8ba80 69 74 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f  ite3Malloc(sizeo
8ba90 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 20  f(*pNew));.     
8baa0 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
8bab0 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20       pNew->id = 
8bac0 69 64 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  id;.        pNew
8bad0 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  ->cnt = 0;.     
8bae0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
8baf0 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
8bb00 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t: {.      asser
8bb10 74 28 20 69 64 2d 32 20 3e 3d 20 30 20 29 3b 0a  t( id-2 >= 0 );.
8bb20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 64        assert( id
8bb30 2d 32 20 3c 20 28 69 6e 74 29 28 73 69 7a 65 6f  -2 < (int)(sizeo
8bb40 66 28 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f  f(aStatic)/sizeo
8bb50 66 28 61 53 74 61 74 69 63 5b 30 5d 29 29 20 29  f(aStatic[0])) )
8bb60 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26  ;.      pNew = &
8bb70 61 53 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20  aStatic[id-2];. 
8bb80 20 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20       pNew->id = 
8bb90 69 64 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  id;.      break;
8bba0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
8bbb0 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
8bbc0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
8bbd0 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72  deallocates a pr
8bbe0 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74  eviously allocat
8bbf0 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61  ed mutex..*/.sta
8bc00 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75  tic void debugMu
8bc10 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
8bc20 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
8bc30 65 72 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29  ert( p->cnt==0 )
8bc40 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
8bc50 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
8bc60 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  FAST || p->id==S
8bc70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
8bc80 52 53 49 56 45 20 29 3b 0a 20 20 73 71 6c 69 74  RSIVE );.  sqlit
8bc90 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f  e3_free(p);.}../
8bca0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
8bcb0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61  _mutex_enter() a
8bcc0 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
8bcd0 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20  _try() routines 
8bce0 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e  attempt.** to en
8bcf0 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66  ter a mutex.  If
8bd00 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20   another thread 
8bd10 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69  is already withi
8bd20 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20  n the mutex,.** 
8bd30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
8bd40 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b  ter() will block
8bd50 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
8bd60 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65  ex_try() will re
8bd70 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42  turn.** SQLITE_B
8bd80 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65  USY.  The sqlite
8bd90 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e  3_mutex_try() in
8bda0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
8bdb0 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f  SQLITE_OK.** upo
8bdc0 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74  n successful ent
8bdd0 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65  ry.  Mutexes cre
8bde0 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54  ated using SQLIT
8bdf0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
8be00 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65  E can.** be ente
8be10 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  red multiple tim
8be20 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  es by the same t
8be30 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20  hread.  In such 
8be40 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75  cases the,.** mu
8be50 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74  tex must be exit
8be60 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62  ed an equal numb
8be70 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f  er of times befo
8be80 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  re another threa
8be90 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20  d.** can enter. 
8bea0 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72   If the same thr
8beb0 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74  ead tries to ent
8bec0 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e  er any other kin
8bed0 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f  d of mutex.** mo
8bee0 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68  re than once, th
8bef0 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e  e behavior is un
8bf00 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  defined..*/.stat
8bf10 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74  ic void debugMut
8bf20 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f  exEnter(sqlite3_
8bf30 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
8bf40 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
8bf50 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
8bf60 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78  VE || debugMutex
8bf70 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
8bf80 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74  p->cnt++;.}.stat
8bf90 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
8bfa0 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  xTry(sqlite3_mut
8bfb0 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
8bfc0 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
8bfd0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
8bfe0 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74  || debugMutexNot
8bff0 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e  held(p) );.  p->
8c000 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20  cnt++;.  return 
8c010 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
8c020 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
8c030 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f  mutex_leave() ro
8c040 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75  utine exits a mu
8c050 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20  tex that was.** 
8c060 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
8c070 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
8c080 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61  hread.  The beha
8c090 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66  vior.** is undef
8c0a0 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
8c0b0 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  x is not current
8c0c0 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a  ly entered or.**
8c0d0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
8c0e0 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51  y allocated.  SQ
8c0f0 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
8c100 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74  do either..*/.st
8c110 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d  atic void debugM
8c120 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65  utexLeave(sqlite
8c130 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
8c140 73 73 65 72 74 28 20 64 65 62 75 67 4d 75 74 65  ssert( debugMute
8c150 78 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d  xHeld(p) );.  p-
8c160 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74  >cnt--;.  assert
8c170 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
8c180 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
8c190 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74  || debugMutexNot
8c1a0 68 65 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51  held(p) );.}..SQ
8c1b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
8c1c0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
8c1d0 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75  ds *sqlite3Defau
8c1e0 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20  ltMutex(void){. 
8c1f0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
8c200 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d  mutex_methods sM
8c210 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 64 65 62  utex = {.    deb
8c220 75 67 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20  ugMutexInit,.   
8c230 20 64 65 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a   debugMutexEnd,.
8c240 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 41 6c      debugMutexAl
8c250 6c 6f 63 2c 0a 20 20 20 20 64 65 62 75 67 4d 75  loc,.    debugMu
8c260 74 65 78 46 72 65 65 2c 0a 20 20 20 20 64 65 62  texFree,.    deb
8c270 75 67 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20  ugMutexEnter,.  
8c280 20 20 64 65 62 75 67 4d 75 74 65 78 54 72 79 2c    debugMutexTry,
8c290 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 4c  .    debugMutexL
8c2a0 65 61 76 65 2c 0a 0a 20 20 20 20 64 65 62 75 67  eave,..    debug
8c2b0 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 64  MutexHeld,.    d
8c2c0 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64  ebugMutexNotheld
8c2d0 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20  .  };..  return 
8c2e0 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69  &sMutex;.}.#endi
8c2f0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c  f /* defined(SQL
8c300 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20  ITE_MUTEX_NOOP) 
8c310 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
8c320 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a  E_DEBUG) */../**
8c330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
8c340 20 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63   of mutex_noop.c
8c350 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
8c360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
8c380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
8c390 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73  in file mutex_os
8c3a0 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  2.c ************
8c3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
8c3d0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32  ** 2007 August 2
8c3e0 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
8c3f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
8c400 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
8c410 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
8c420 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
8c430 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
8c440 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
8c450 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
8c460 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
8c470 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
8c480 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
8c490 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
8c4a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
8c4b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
8c4c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
8c4d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
8c4e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
8c4f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
8c500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c540 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
8c550 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
8c560 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
8c570 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f  ement mutexes fo
8c580 72 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64  r OS/2.**.** $Id
8c590 3a 20 6d 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20  : mutex_os2.c,v 
8c5a0 31 2e 31 31 20 32 30 30 38 2f 31 31 2f 32 32 20  1.11 2008/11/22 
8c5b0 31 39 3a 35 30 3a 35 34 20 70 77 65 69 6c 62 61  19:50:54 pweilba
8c5c0 63 68 65 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f  cher Exp $.*/../
8c5d0 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
8c5e0 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
8c5f0 6c 79 20 75 73 65 64 20 69 66 20 53 51 4c 49 54  ly used if SQLIT
8c600 45 5f 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64  E_MUTEX_OS2 is d
8c610 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74  efined..** See t
8c620 68 65 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20  he mutex.h file 
8c630 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
8c640 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  #ifdef SQLITE_MU
8c650 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a  TEX_OS2../******
8c660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c670 20 4f 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c   OS/2 Mutex Impl
8c680 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a  ementation *****
8c690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c6a0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  *.**.** This imp
8c6b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d  lementation of m
8c6c0 75 74 65 78 65 73 20 69 73 20 62 75 69 6c 74 20  utexes is built 
8c6d0 75 73 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41  using the OS/2 A
8c6e0 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  PI..*/../*.** Th
8c6f0 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a  e mutex object.*
8c700 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65  * Each recursive
8c710 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73   mutex is an ins
8c720 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
8c730 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
8c740 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
8c750 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d  te3_mutex {.  HM
8c760 54 58 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20  TX mutex;       
8c770 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c  /* Mutex control
8c780 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f  ling the lock */
8c790 0a 20 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20  .  int  id;     
8c7a0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79       /* Mutex ty
8c7b0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65  pe */.  int  nRe
8c7c0 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  f;        /* Num
8c7d0 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
8c7e0 73 20 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65  s */.  TID  owne
8c7f0 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65  r;       /* Thre
8c800 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  ad holding this 
8c810 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65  mutex */.};..#de
8c820 66 69 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49  fine OS2_MUTEX_I
8c830 4e 49 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30  NITIALIZER   0,0
8c840 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  ,0,0../*.** Init
8c850 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69  ialize and deini
8c860 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65  tialize the mute
8c870 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  x subsystem..*/.
8c880 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75  static int os2Mu
8c890 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72  texInit(void){ r
8c8a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
8c8b0 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73   }.static int os
8c8c0 32 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b  2MutexEnd(void){
8c8d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
8c8e0 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K; }../*.** The 
8c8f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
8c900 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c  loc() routine al
8c910 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
8c920 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72   mutex and retur
8c930 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
8c940 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72  it.  If it retur
8c950 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
8c960 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
8c970 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
8c980 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53  allocated. .** S
8c990 51 4c 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e  QLite will unwin
8c9a0 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20  d its stack and 
8c9b0 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
8c9c0 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a    The argument.*
8c9d0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
8c9e0 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
8c9f0 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
8ca00 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  er constants:.**
8ca10 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
8ca20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
8ca30 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20  AST             
8ca40 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c    0.** <li>  SQL
8ca50 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
8ca60 49 56 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a  IVE          1.*
8ca70 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
8ca80 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
8ca90 45 52 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69  ER      2.** <li
8caa0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
8cab0 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20  STATIC_MEM      
8cac0 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51     3.** <li>  SQ
8cad0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
8cae0 43 5f 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a  C_PRNG        4.
8caf0 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
8cb00 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e  he first two con
8cb10 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c  stants cause sql
8cb20 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
8cb30 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20  () to create.** 
8cb40 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68  a new mutex.  Th
8cb50 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72  e new mutex is r
8cb60 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51  ecursive when SQ
8cb70 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
8cb80 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20  SIVE.** is used 
8cb90 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72  but not necessar
8cba0 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49  ily so when SQLI
8cbb0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73  TE_MUTEX_FAST is
8cbc0 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75   used..** The mu
8cbd0 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
8cbe0 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
8cbf0 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69   to make a disti
8cc00 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65  nction.** betwee
8cc10 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
8cc20 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c  ECURSIVE and SQL
8cc30 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
8cc40 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  f it does.** not
8cc50 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53   want to.  But S
8cc60 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  QLite will only 
8cc70 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73  request a recurs
8cc80 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20  ive mutex in.** 
8cc90 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72  cases where it r
8cca0 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e  eally needs one.
8ccb0 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f    If a faster no
8ccc0 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  n-recursive mute
8ccd0 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  x.** implementat
8cce0 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ion is available
8ccf0 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61   on the host pla
8cd00 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78  tform, the mutex
8cd10 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69   subsystem.** mi
8cd20 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20  ght return such 
8cd30 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f  a mutex in respo
8cd40 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55  nse to SQLITE_MU
8cd50 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20  TEX_FAST..**.** 
8cd60 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65  The other allowe
8cd70 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  d parameters to 
8cd80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
8cd90 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72  loc() each retur
8cda0 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
8cdb0 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78  o a static preex
8cdc0 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54  isting mutex.  T
8cdd0 68 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 65  hree static mute
8cde0 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20  xes are.** used 
8cdf0 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  by the current v
8ce00 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  ersion of SQLite
8ce10 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f  .  Future versio
8ce20 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20  ns of SQLite.** 
8ce30 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e  may add addition
8ce40 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  al static mutexe
8ce50 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78  s.  Static mutex
8ce60 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72  es are for inter
8ce70 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51  nal.** use by SQ
8ce80 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c  Lite only.  Appl
8ce90 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73  ications that us
8cea0 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73  e SQLite mutexes
8ceb0 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f   should.** use o
8cec0 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20  nly the dynamic 
8ced0 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64  mutexes returned
8cee0 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   by SQLITE_MUTEX
8cef0 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49  _FAST or.** SQLI
8cf00 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
8cf10 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  VE..**.** Note t
8cf20 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68  hat if one of th
8cf30 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20  e dynamic mutex 
8cf40 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49  parameters (SQLI
8cf50 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a  TE_MUTEX_FAST.**
8cf60 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   or SQLITE_MUTEX
8cf70 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75  _RECURSIVE) is u
8cf80 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33  sed then sqlite3
8cf90 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a  _mutex_alloc().*
8cfa0 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66  * returns a diff
8cfb0 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65  erent mutex on e
8cfc0 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20  very call.  But 
8cfd0 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a  for the static.*
8cfe0 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74  * mutex types, t
8cff0 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73  he same mutex is
8d000 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65   returned on eve
8d010 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73  ry call that has
8d020 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70  .** the same typ
8d030 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
8d040 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tic sqlite3_mute
8d050 78 20 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63  x *os2MutexAlloc
8d060 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73  (int iType){.  s
8d070 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20  qlite3_mutex *p 
8d080 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68  = NULL;.  switch
8d090 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63  ( iType ){.    c
8d0a0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
8d0b0 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20  _FAST:.    case 
8d0c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
8d0d0 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20  URSIVE: {.      
8d0e0 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  p = sqlite3Mallo
8d0f0 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70  cZero( sizeof(*p
8d100 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ) );.      if( p
8d110 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
8d120 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20  d = iType;.     
8d130 20 20 20 69 66 28 20 44 6f 73 43 72 65 61 74 65     if( DosCreate
8d140 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d  MutexSem( 0, &p-
8d150 3e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45  >mutex, 0, FALSE
8d160 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29   ) != NO_ERROR )
8d170 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
8d180 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20  te3_free( p );. 
8d190 20 20 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c           p = NUL
8d1a0 4c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  L;.        }.   
8d1b0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
8d1c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
8d1d0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61  ult: {.      sta
8d1e0 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74  tic volatile int
8d1f0 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20   isInit = 0;.   
8d200 20 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65     static sqlite
8d210 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75  3_mutex staticMu
8d220 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20  texes[] = {.    
8d230 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f      { OS2_MUTEX_
8d240 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a  INITIALIZER, },.
8d250 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
8d260 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
8d270 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
8d280 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
8d290 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20  ZER, },.        
8d2a0 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54  { OS2_MUTEX_INIT
8d2b0 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20  IALIZER, },.    
8d2c0 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f      { OS2_MUTEX_
8d2d0 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a  INITIALIZER, },.
8d2e0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
8d2f0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
8d300 20 7d 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20   },.      };.   
8d310 20 20 20 69 66 20 28 20 21 69 73 49 6e 69 74 20     if ( !isInit 
8d320 29 7b 0a 20 20 20 20 20 20 20 20 41 50 49 52 45  ){.        APIRE
8d330 54 20 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54  T rc;.        PT
8d340 49 42 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20  IB ptib;.       
8d350 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20   PPIB ppib;.    
8d360 20 20 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a      HMTX mutex;.
8d370 20 20 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d          char nam
8d380 65 5b 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44  e[32];.        D
8d390 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
8d3a0 20 26 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b   &ptib, &ppib );
8d3b0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
8d3c0 5f 73 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f  _snprintf( sizeo
8d3d0 66 28 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22  f(name), name, "
8d3e0 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25  \\SEM32\\SQLITE%
8d3f0 30 34 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20  04x",.          
8d400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8d410 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20  ppib->pib_ulpid 
8d420 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  );.        while
8d430 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  ( !isInit ){.   
8d440 20 20 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30         mutex = 0
8d450 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
8d460 20 44 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53   DosCreateMutexS
8d470 65 6d 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78  em( name, &mutex
8d480 2c 20 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20  , 0, FALSE);.   
8d490 20 20 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d         if( rc ==
8d4a0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
8d4b0 20 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65           unsigne
8d4c0 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20  d int i;.       
8d4d0 20 20 20 20 20 69 66 28 20 21 69 73 49 6e 69 74       if( !isInit
8d4e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
8d4f0 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20    for( i = 0; i 
8d500 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d  < sizeof(staticM
8d510 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73  utexes)/sizeof(s
8d520 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29  taticMutexes[0])
8d530 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20  ; i++ ){.       
8d540 20 20 20 20 20 20 20 20 20 44 6f 73 43 72 65 61           DosCrea
8d550 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26  teMutexSem( 0, &
8d560 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d  staticMutexes[i]
8d570 2e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45  .mutex, 0, FALSE
8d580 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
8d590 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
8d5a0 20 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20    isInit = 1;.  
8d5b0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
8d5c0 20 20 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65          DosClose
8d5d0 4d 75 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20  MutexSem( mutex 
8d5e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
8d5f0 73 65 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52  se if( rc == ERR
8d600 4f 52 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d  OR_DUPLICATE_NAM
8d610 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  E ){.           
8d620 20 44 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a   DosSleep( 1 );.
8d630 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
8d640 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74  .            ret
8d650 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20  urn p;.         
8d660 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
8d670 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
8d680 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20  t( iType-2 >= 0 
8d690 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
8d6a0 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f   iType-2 < sizeo
8d6b0 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29  f(staticMutexes)
8d6c0 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75  /sizeof(staticMu
8d6d0 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20  texes[0]) );.   
8d6e0 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75     p = &staticMu
8d6f0 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a  texes[iType-2];.
8d700 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
8d710 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ype;.      break
8d720 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
8d730 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn p;.}.../*.*
8d740 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
8d750 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65  eallocates a pre
8d760 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
8d770 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69  d mutex..** SQLi
8d780 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f  te is careful to
8d790 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72   deallocate ever
8d7a0 79 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20  y mutex that it 
8d7b0 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74  allocates..*/.st
8d7c0 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74  atic void os2Mut
8d7d0 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  exFree(sqlite3_m
8d7e0 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20  utex *p){.  if( 
8d7f0 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
8d800 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
8d810 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
8d820 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
8d830 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e  UTEX_FAST || p->
8d840 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
8d850 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
8d860 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d  DosCloseMutexSem
8d870 28 20 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20  ( p->mutex );.  
8d880 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20  sqlite3_free( p 
8d890 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
8d8a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
8d8b0 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ter() and sqlite
8d8c0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
8d8d0 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a  utines attempt.*
8d8e0 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74  * to enter a mut
8d8f0 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20  ex.  If another 
8d900 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64  thread is alread
8d910 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74  y within the mut
8d920 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ex,.** sqlite3_m
8d930 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c  utex_enter() wil
8d940 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69  l block and sqli
8d950 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
8d960 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53  will return.** S
8d970 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65  QLITE_BUSY.  The
8d980 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
8d990 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ry() interface r
8d9a0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
8d9b0 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73  .** upon success
8d9c0 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65  ful entry.  Mute
8d9d0 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e  xes created usin
8d9e0 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  g SQLITE_MUTEX_R
8d9f0 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20  ECURSIVE can.** 
8da00 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69  be entered multi
8da10 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65  ple times by the
8da20 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49   same thread.  I
8da30 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65  n such cases the
8da40 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20  ,.** mutex must 
8da50 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75  be exited an equ
8da60 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  al number of tim
8da70 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65  es before anothe
8da80 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20  r thread.** can 
8da90 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73  enter.  If the s
8daa0 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73  ame thread tries
8dab0 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74   to enter any ot
8dac0 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65  her kind of mute
8dad0 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f  x.** more than o
8dae0 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f  nce, the behavio
8daf0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  r is undefined..
8db00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
8db10 73 32 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c  s2MutexEnter(sql
8db20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
8db30 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44    TID tid;.  PID
8db40 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e   holder1;.  ULON
8db50 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28  G holder2;.  if(
8db60 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   p==0 ) return;.
8db70 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
8db80 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
8db90 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75  CURSIVE || os2Mu
8dba0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
8dbb0 0a 20 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74  .  DosRequestMut
8dbc0 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20  exSem(p->mutex, 
8dbd0 53 45 4d 5f 49 4e 44 45 46 49 4e 49 54 45 5f 57  SEM_INDEFINITE_W
8dbe0 41 49 54 29 3b 0a 20 20 44 6f 73 51 75 65 72 79  AIT);.  DosQuery
8dbf0 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
8dc00 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69  x, &holder1, &ti
8dc10 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20  d, &holder2);.  
8dc20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a  p->owner = tid;.
8dc30 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73    p->nRef++;.}.s
8dc40 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74  tatic int os2Mut
8dc50 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75  exTry(sqlite3_mu
8dc60 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  tex *p){.  int r
8dc70 63 3b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20  c;.  TID tid;.  
8dc80 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55  PID holder1;.  U
8dc90 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20  LONG holder2;.  
8dca0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
8dcb0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  n SQLITE_OK;.  a
8dcc0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
8dcd0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
8dce0 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78  SIVE || os2Mutex
8dcf0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
8dd00 69 66 28 20 44 6f 73 52 65 71 75 65 73 74 4d 75  if( DosRequestMu
8dd10 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c  texSem(p->mutex,
8dd20 20 53 45 4d 5f 49 4d 4d 45 44 49 41 54 45 5f 52   SEM_IMMEDIATE_R
8dd30 45 54 55 52 4e 29 20 3d 3d 20 4e 4f 5f 45 52 52  ETURN) == NO_ERR
8dd40 4f 52 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65  OR) {.    DosQue
8dd50 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75  ryMutexSem(p->mu
8dd60 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26  tex, &holder1, &
8dd70 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a  tid, &holder2);.
8dd80 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74      p->owner = t
8dd90 69 64 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b  id;.    p->nRef+
8dda0 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  +;.    rc = SQLI
8ddb0 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 6c 73 65 20  TE_OK;.  } else 
8ddc0 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
8ddd0 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 72  E_BUSY;.  }..  r
8dde0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
8ddf0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
8de00 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75  utex_leave() rou
8de10 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74  tine exits a mut
8de20 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70  ex that was.** p
8de30 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65  reviously entere
8de40 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  d by the same th
8de50 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76  read.  The behav
8de60 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  ior.** is undefi
8de70 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78  ned if the mutex
8de80 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
8de90 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20  y entered or.** 
8dea0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
8deb0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c   allocated.  SQL
8dec0 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64  ite will never d
8ded0 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61  o either..*/.sta
8dee0 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65  tic void os2Mute
8def0 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d  xLeave(sqlite3_m
8df00 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20  utex *p){.  TID 
8df10 74 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65  tid;.  PID holde
8df20 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64  r1;.  ULONG hold
8df30 65 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  er2;.  if( p==0 
8df40 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
8df50 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b  rt( p->nRef>0 );
8df60 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78  .  DosQueryMutex
8df70 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68  Sem(p->mutex, &h
8df80 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68  older1, &tid, &h
8df90 6f 6c 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72  older2);.  asser
8dfa0 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64  t( p->owner==tid
8dfb0 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b   );.  p->nRef--;
8dfc0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
8dfd0 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d  ef==0 || p->id==
8dfe0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
8dff0 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52  URSIVE );.  DosR
8e000 65 6c 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70  eleaseMutexSem(p
8e010 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66  ->mutex);.}..#if
8e020 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
8e030 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
8e040 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
8e050 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
8e060 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74  x_notheld() rout
8e070 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e  ine are.** inten
8e080 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69  ded for use insi
8e090 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
8e0a0 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  ements..*/.stati
8e0b0 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 48 65  c int os2MutexHe
8e0c0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
8e0d0 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b   *p){.  TID tid;
8e0e0 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c  .  PID pid;.  UL
8e0f0 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50  ONG ulCount;.  P
8e100 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20  TIB ptib;.  if( 
8e110 70 21 3d 30 20 29 20 7b 0a 20 20 20 20 44 6f 73  p!=0 ) {.    Dos
8e120 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d  QueryMutexSem(p-
8e130 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74  >mutex, &pid, &t
8e140 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20  id, &ulCount);. 
8e150 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f   } else {.    Do
8e160 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26  sGetInfoBlocks(&
8e170 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20  ptib, NULL);.   
8e180 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62   tid = ptib->tib
8e190 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74  _ptib2->tib2_ult
8e1a0 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  id;.  }.  return
8e1b0 20 70 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65   p==0 || (p->nRe
8e1c0 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72  f!=0 && p->owner
8e1d0 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63  ==tid);.}.static
8e1e0 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74   int os2MutexNot
8e1f0 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
8e200 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69  ex *p){.  TID ti
8e210 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20  d;.  PID pid;.  
8e220 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20  ULONG ulCount;. 
8e230 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66   PTIB ptib;.  if
8e240 28 20 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20  ( p!= 0 ) {.    
8e250 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d  DosQueryMutexSem
8e260 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c  (p->mutex, &pid,
8e270 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29   &tid, &ulCount)
8e280 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
8e290 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
8e2a0 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a  s(&ptib, NULL);.
8e2b0 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e      tid = ptib->
8e2c0 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f  tib_ptib2->tib2_
8e2d0 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74  ultid;.  }.  ret
8e2e0 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e  urn p==0 || p->n
8e2f0 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e  Ref==0 || p->own
8e300 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69  er!=tid;.}.#endi
8e310 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
8e320 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  E sqlite3_mutex_
8e330 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
8e340 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69  DefaultMutex(voi
8e350 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c  d){.  static sql
8e360 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
8e370 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20  ds sMutex = {.  
8e380 20 20 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a    os2MutexInit,.
8e390 20 20 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c      os2MutexEnd,
8e3a0 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c  .    os2MutexAll
8e3b0 6f 63 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  oc,.    os2Mutex
8e3c0 46 72 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74  Free,.    os2Mut
8e3d0 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32  exEnter,.    os2
8e3e0 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73  MutexTry,.    os
8e3f0 32 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66  2MutexLeave,.#if
8e400 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
8e410 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c  .    os2MutexHel
8e420 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e  d,.    os2MutexN
8e430 6f 74 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20  otheld.#endif.  
8e440 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d  };..  return &sM
8e450 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  utex;.}.#endif /
8e460 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  * SQLITE_MUTEX_O
8e470 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  S2 */../********
8e480 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75  ****** End of mu
8e490 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a  tex_os2.c ******
8e4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e4c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
8e4d0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
8e4e0 65 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a  e mutex_unix.c *
8e4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e510 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
8e520 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a  7 August 28.**.*
8e530 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
8e540 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
8e550 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
8e560 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
8e570 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
8e580 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
8e590 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
8e5a0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
8e5b0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
8e5c0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
8e5d0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
8e5e0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
8e5f0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
8e600 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
8e610 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
8e620 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
8e630 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
8e640 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
8e650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8e680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
8e690 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
8e6a0 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
8e6b0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
8e6c0 6d 75 74 65 78 65 73 20 66 6f 72 20 70 74 68 72  mutexes for pthr
8e6d0 65 61 64 73 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  eads.**.** $Id: 
8e6e0 6d 75 74 65 78 5f 75 6e 69 78 2e 63 2c 76 20 31  mutex_unix.c,v 1
8e6f0 2e 31 36 20 32 30 30 38 2f 31 32 2f 30 38 20 31  .16 2008/12/08 1
8e700 38 3a 31 39 3a 31 38 20 64 72 68 20 45 78 70 20  8:19:18 drh Exp 
8e710 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
8e720 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
8e730 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  e is only used i
8e740 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69  f we are compili
8e750 6e 67 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a  ng threadsafe.**
8e760 20 75 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68   under unix with
8e770 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a   pthreads..**.**
8e780 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 20   Note that this 
8e790 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72  implementation r
8e7a0 65 71 75 69 72 65 73 20 61 20 76 65 72 73 69 6f  equires a versio
8e7b0 6e 20 6f 66 20 70 74 68 72 65 61 64 73 20 74 68  n of pthreads th
8e7c0 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72  at.** supports r
8e7d0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73  ecursive mutexes
8e7e0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
8e7f0 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44  TE_MUTEX_PTHREAD
8e800 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68  S..#include <pth
8e810 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20  read.h>.../*.** 
8e820 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d  Each recursive m
8e830 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61  utex is an insta
8e840 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
8e850 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
8e860 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
8e870 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72  3_mutex {.  pthr
8e880 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65  ead_mutex_t mute
8e890 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20  x;     /* Mutex 
8e8a0 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20  controlling the 
8e8b0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64  lock */.  int id
8e8c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
8e8d0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79       /* Mutex ty
8e8e0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  pe */.  int nRef
8e8f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
8e900 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
8e910 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70  entrances */.  p
8e920 74 68 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20  thread_t owner; 
8e930 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72            /* Thr
8e940 65 61 64 20 74 68 61 74 20 69 73 20 77 69 74 68  ead that is with
8e950 69 6e 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f  in this mutex */
8e960 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
8e970 45 42 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65  EBUG.  int trace
8e980 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
8e990 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61    /* True to tra
8e9a0 63 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65  ce changes */.#e
8e9b0 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53  ndif.};.#ifdef S
8e9c0 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66  QLITE_DEBUG.#def
8e9d0 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45  ine SQLITE3_MUTE
8e9e0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20  X_INITIALIZER { 
8e9f0 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e  PTHREAD_MUTEX_IN
8ea00 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c  ITIALIZER, 0, 0,
8ea10 20 28 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30   (pthread_t)0, 0
8ea20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   }.#else.#define
8ea30 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49   SQLITE3_MUTEX_I
8ea40 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48  NITIALIZER { PTH
8ea50 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49  READ_MUTEX_INITI
8ea60 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70  ALIZER, 0, 0, (p
8ea70 74 68 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e  thread_t)0 }.#en
8ea80 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  dif../*.** The s
8ea90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
8eaa0 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
8eab0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
8eac0 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
8ead0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
8eae0 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65  only inside asse
8eaf0 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
8eb00 20 20 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f    On some platfo
8eb10 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69  rms,.** there mi
8eb20 67 68 74 20 62 65 20 72 61 63 65 20 63 6f 6e 64  ght be race cond
8eb30 69 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20  itions that can 
8eb40 63 61 75 73 65 20 74 68 65 73 65 20 72 6f 75 74  cause these rout
8eb50 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76  ines to.** deliv
8eb60 65 72 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73  er incorrect res
8eb70 75 6c 74 73 2e 20 20 49 6e 20 70 61 72 74 69 63  ults.  In partic
8eb80 75 6c 61 72 2c 20 69 66 20 70 74 68 72 65 61 64  ular, if pthread
8eb90 5f 65 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e  _equal() is.** n
8eba0 6f 74 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65  ot an atomic ope
8ebb0 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  ration, then the
8ebc0 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 68  se routines migh
8ebd0 74 20 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e  t delivery.** in
8ebe0 63 6f 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e  correct results.
8ebf0 20 20 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f    On most platfo
8ec00 72 6d 73 2c 20 70 74 68 72 65 61 64 5f 65 71 75  rms, pthread_equ
8ec10 61 6c 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f  al() is a .** co
8ec20 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20  mparison of two 
8ec30 69 6e 74 65 67 65 72 73 20 61 6e 64 20 69 73 20  integers and is 
8ec40 74 68 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 63  therefore atomic
8ec50 2e 20 20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a  .  But we are.**
8ec60 20 74 6f 6c 64 20 74 68 61 74 20 48 50 55 58 20   told that HPUX 
8ec70 69 73 20 6e 6f 74 20 73 75 63 68 20 61 20 70 6c  is not such a pl
8ec80 61 74 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20  atform.  If so, 
8ec90 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69  then these routi
8eca0 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  nes.** will not 
8ecb0 61 6c 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72  always work corr
8ecc0 65 63 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a  ectly on HPUX..*
8ecd0 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c  *.** On those pl
8ece0 61 74 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74  atforms where pt
8ecf0 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73  hread_equal() is
8ed00 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c   not atomic, SQL
8ed10 69 74 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65  ite.** should be
8ed20 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f 75   compiled withou
8ed30 74 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47  t -DSQLITE_DEBUG
8ed40 20 61 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 42   and with -DNDEB
8ed50 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75  UG to.** make su
8ed60 72 65 20 6e 6f 20 61 73 73 65 72 74 28 29 20 73  re no assert() s
8ed70 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76  tatements are ev
8ed80 61 6c 75 61 74 65 64 20 61 6e 64 20 68 65 6e 63  aluated and henc
8ed90 65 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69  e these.** routi
8eda0 6e 65 73 20 61 72 65 20 6e 65 76 65 72 20 63 61  nes are never ca
8edb0 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  lled..*/.#if !de
8edc0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
8edd0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
8ede0 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e  DEBUG).static in
8edf0 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 48 65  t pthreadMutexHe
8ee00 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
8ee10 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28   *p){.  return (
8ee20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 74  p->nRef!=0 && pt
8ee30 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f  hread_equal(p->o
8ee40 77 6e 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65  wner, pthread_se
8ee50 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63  lf()));.}.static
8ee60 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65   int pthreadMute
8ee70 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
8ee80 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65  _mutex *p){.  re
8ee90 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  turn p->nRef==0 
8eea0 7c 7c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  || pthread_equal
8eeb0 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65  (p->owner, pthre
8eec0 61 64 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d  ad_self())==0;.}
8eed0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
8eee0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65  nitialize and de
8eef0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  initialize the m
8ef00 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a  utex subsystem..
8ef10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74  */.static int pt
8ef20 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 28 76  hreadMutexInit(v
8ef30 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
8ef40 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
8ef50 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65   int pthreadMute
8ef60 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75  xEnd(void){ retu
8ef70 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
8ef80 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
8ef90 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
8efa0 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
8efb0 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65  es a new.** mute
8efc0 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  x and returns a 
8efd0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
8efe0 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  If it returns NU
8eff0 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73  LL.** that means
8f000 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f   that a mutex co
8f010 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63  uld not be alloc
8f020 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a  ated.  SQLite.**
8f030 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73   will unwind its
8f040 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72   stack and retur
8f050 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65  n an error.  The
8f060 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
8f070 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
8f080 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20  loc() is one of 
8f090 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  these integer co
8f0a0 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c  nstants:.**.** <
8f0b0 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  ul>.** <li>  SQL
8f0c0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a  ITE_MUTEX_FAST.*
8f0d0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
8f0e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a  UTEX_RECURSIVE.*
8f0f0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
8f100 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
8f110 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  ER.** <li>  SQLI
8f120 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
8f130 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  MEM.** <li>  SQL
8f140 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
8f150 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _MEM2.** <li>  S
8f160 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
8f170 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20  IC_PRNG.** <li> 
8f180 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
8f190 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 2f 75 6c  ATIC_LRU.** </ul
8f1a0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  >.**.** The firs
8f1b0 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20  t two constants 
8f1c0 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75  cause sqlite3_mu
8f1d0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63  tex_alloc() to c
8f1e0 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d  reate.** a new m
8f1f0 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d  utex.  The new m
8f200 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76  utex is recursiv
8f210 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  e when SQLITE_MU
8f220 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
8f230 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74   is used but not
8f240 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20   necessarily so 
8f250 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
8f260 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a  X_FAST is used..
8f270 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70  ** The mutex imp
8f280 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73  lementation does
8f290 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b   not need to mak
8f2a0 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a  e a distinction.
8f2b0 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54  ** between SQLIT
8f2c0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
8f2d0 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54  E and SQLITE_MUT
8f2e0 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f  EX_FAST if it do
8f2f0 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74  es.** not want t
8f300 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77  o.  But SQLite w
8f310 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74  ill only request
8f320 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74   a recursive mut
8f330 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77  ex in.** cases w
8f340 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e  here it really n
8f350 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20  eeds one.  If a 
8f360 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72  faster non-recur
8f370 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d  sive mutex.** im
8f380 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
8f390 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65  available on the
8f3a0 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20   host platform, 
8f3b0 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
8f3c0 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  tem.** might ret
8f3d0 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78  urn such a mutex
8f3e0 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20   in response to 
8f3f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
8f400 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68  T..**.** The oth
8f410 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d  er allowed param
8f420 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33  eters to sqlite3
8f430 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65  _mutex_alloc() e
8f440 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ach return.** a 
8f450 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
8f460 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20  tic preexisting 
8f470 6d 75 74 65 78 2e 20 20 54 68 72 65 65 20 73 74  mutex.  Three st
8f480 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65  atic mutexes are
8f490 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20  .** used by the 
8f4a0 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20  current version 
8f4b0 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75  of SQLite.  Futu
8f4c0 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  re versions of S
8f4d0 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64  QLite.** may add
8f4e0 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74   additional stat
8f4f0 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61  ic mutexes.  Sta
8f500 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20  tic mutexes are 
8f510 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  for internal.** 
8f520 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e  use by SQLite on
8f530 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  ly.  Application
8f540 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74  s that use SQLit
8f550 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64  e mutexes should
8f560 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65  .** use only the
8f570 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73   dynamic mutexes
8f580 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c   returned by SQL
8f590 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f  ITE_MUTEX_FAST o
8f5a0 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  r.** SQLITE_MUTE
8f5b0 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a  X_RECURSIVE..**.
8f5c0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  ** Note that if 
8f5d0 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d  one of the dynam
8f5e0 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74  ic mutex paramet
8f5f0 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45  ers (SQLITE_MUTE
8f600 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c  X_FAST.** or SQL
8f610 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
8f620 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65  IVE) is used the
8f630 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  n sqlite3_mutex_
8f640 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72  alloc().** retur
8f650 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d  ns a different m
8f660 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61  utex on every ca
8f670 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65  ll.  But for the
8f680 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65   static .** mute
8f690 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d  x types, the sam
8f6a0 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72  e mutex is retur
8f6b0 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  ned on every cal
8f6c0 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68  l that has.** th
8f6d0 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62  e same type numb
8f6e0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  er..*/.static sq
8f6f0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68  lite3_mutex *pth
8f700 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69  readMutexAlloc(i
8f710 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 74 61  nt iType){.  sta
8f720 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tic sqlite3_mute
8f730 78 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b  x staticMutexes[
8f740 5d 20 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 45  ] = {.    SQLITE
8f750 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
8f760 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33  ZER,.    SQLITE3
8f770 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
8f780 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f  ER,.    SQLITE3_
8f790 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
8f7a0 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d  R,.    SQLITE3_M
8f7b0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
8f7c0 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55  ,.    SQLITE3_MU
8f7d0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
8f7e0 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54  .    SQLITE3_MUT
8f7f0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20  EX_INITIALIZER. 
8f800 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   };.  sqlite3_mu
8f810 74 65 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68  tex *p;.  switch
8f820 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63  ( iType ){.    c
8f830 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
8f840 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
8f850 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
8f860 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
8f870 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
8f880 66 28 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53  f( p ){.#ifdef S
8f890 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f  QLITE_HOMEGROWN_
8f8a0 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a  RECURSIVE_MUTEX.
8f8b0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65          /* If re
8f8c0 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20  cursive mutexes 
8f8d0 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
8f8e0 65 2c 20 77 65 20 77 69 6c 6c 20 68 61 76 65 20  e, we will have 
8f8f0 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75  to.        ** bu
8f900 69 6c 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65  ild our own.  Se
8f910 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20  e below. */.    
8f920 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
8f930 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78  x_init(&p->mutex
8f940 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  , 0);.#else.    
8f950 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 65 63      /* Use a rec
8f960 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 66 20  ursive mutex if 
8f970 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  it is available 
8f980 2a 2f 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  */.        pthre
8f990 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 72  ad_mutexattr_t r
8f9a0 65 63 75 72 73 69 76 65 41 74 74 72 3b 0a 20 20  ecursiveAttr;.  
8f9b0 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75        pthread_mu
8f9c0 74 65 78 61 74 74 72 5f 69 6e 69 74 28 26 72 65  texattr_init(&re
8f9d0 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20  cursiveAttr);.  
8f9e0 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75        pthread_mu
8f9f0 74 65 78 61 74 74 72 5f 73 65 74 74 79 70 65 28  texattr_settype(
8fa00 26 72 65 63 75 72 73 69 76 65 41 74 74 72 2c 20  &recursiveAttr, 
8fa10 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 45  PTHREAD_MUTEX_RE
8fa20 43 55 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20  CURSIVE);.      
8fa30 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
8fa40 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
8fa50 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b  &recursiveAttr);
8fa60 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
8fa70 5f 6d 75 74 65 78 61 74 74 72 5f 64 65 73 74 72  _mutexattr_destr
8fa80 6f 79 28 26 72 65 63 75 72 73 69 76 65 41 74 74  oy(&recursiveAtt
8fa90 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  r);.#endif.     
8faa0 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65     p->id = iType
8fab0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
8fac0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
8fad0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54   case SQLITE_MUT
8fae0 45 58 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20  EX_FAST: {.     
8faf0 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c   p = sqlite3Mall
8fb00 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a  ocZero( sizeof(*
8fb10 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  p) );.      if( 
8fb20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  p ){.        p->
8fb30 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20  id = iType;.    
8fb40 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
8fb50 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78  x_init(&p->mutex
8fb60 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
8fb70 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
8fb80 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
8fb90 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54        assert( iT
8fba0 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20  ype-2 >= 0 );.  
8fbb0 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
8fbc0 65 2d 32 20 3c 20 41 72 72 61 79 53 69 7a 65 28  e-2 < ArraySize(
8fbd0 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 20 29  staticMutexes) )
8fbe0 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61  ;.      p = &sta
8fbf0 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65  ticMutexes[iType
8fc00 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64  -2];.      p->id
8fc10 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
8fc20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
8fc30 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
8fc40 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
8fc50 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
8fc60 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  a previously.** 
8fc70 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e  allocated mutex.
8fc80 20 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65    SQLite is care
8fc90 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ful to deallocat
8fca0 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78  e every.** mutex
8fcb0 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74   that it allocat
8fcc0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
8fcd0 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 46  id pthreadMutexF
8fce0 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ree(sqlite3_mute
8fcf0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
8fd00 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20   p->nRef==0 );. 
8fd10 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d   assert( p->id==
8fd20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
8fd30 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  T || p->id==SQLI
8fd40 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
8fd50 56 45 20 29 3b 0a 20 20 70 74 68 72 65 61 64 5f  VE );.  pthread_
8fd60 6d 75 74 65 78 5f 64 65 73 74 72 6f 79 28 26 70  mutex_destroy(&p
8fd70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69  ->mutex);.  sqli
8fd80 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a  te3_free(p);.}..
8fd90 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
8fda0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20  3_mutex_enter() 
8fdb0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
8fdc0 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73  x_try() routines
8fdd0 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65   attempt.** to e
8fde0 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49  nter a mutex.  I
8fdf0 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  f another thread
8fe00 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68   is already with
8fe10 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a  in the mutex,.**
8fe20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
8fe30 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63  nter() will bloc
8fe40 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  k and sqlite3_mu
8fe50 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72  tex_try() will r
8fe60 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
8fe70 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74  BUSY.  The sqlit
8fe80 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69  e3_mutex_try() i
8fe90 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
8fea0 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70   SQLITE_OK.** up
8feb0 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e  on successful en
8fec0 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72  try.  Mutexes cr
8fed0 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49  eated using SQLI
8fee0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
8fef0 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74  VE can.** be ent
8ff00 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  ered multiple ti
8ff10 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 20  mes by the same 
8ff20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68  thread.  In such
8ff30 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d   cases the,.** m
8ff40 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69  utex must be exi
8ff50 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d  ted an equal num
8ff60 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66  ber of times bef
8ff70 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65  ore another thre
8ff80 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e  ad.** can enter.
8ff90 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 68    If the same th
8ffa0 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e  read tries to en
8ffb0 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69  ter any other ki
8ffc0 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d  nd of mutex.** m
8ffd0 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74  ore than once, t
8ffe0 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
8fff0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61  ndefined..*/.sta
90000 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 64  tic void pthread
90010 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74  MutexEnter(sqlit
90020 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
90030 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
90040 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
90050 52 53 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64  RSIVE || pthread
90060 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
90070 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
90080 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55  E_HOMEGROWN_RECU
90090 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a  RSIVE_MUTEX.  /*
900a0 20 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75   If recursive mu
900b0 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76  texes are not av
900c0 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65  ailable, then we
900d0 20 68 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20   have to grow.  
900e0 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69  ** our own.  Thi
900f0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
90100 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 70 74   assumes that pt
90110 68 72 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20  hread_equal().  
90120 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74  ** is atomic - t
90130 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65  hat it cannot be
90140 20 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74   deceived into t
90150 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a  hinking self.  *
90160 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61  * and p->owner a
90170 72 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f  re equal if p->o
90180 77 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65 74  wner changes bet
90190 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a  ween two values.
901a0 20 20 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f    ** that are no
901b0 74 20 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20  t equal to self 
901c0 77 68 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72  while the compar
901d0 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70  ison is taking p
901e0 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20  lace..  ** This 
901f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61  implementation a
90200 6c 73 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f  lso assumes a co
90210 68 65 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74  herent cache - t
90220 68 61 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61  hat .  ** separa
90230 74 65 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e  te processes can
90240 6e 6f 74 20 72 65 61 64 20 64 69 66 66 65 72 65  not read differe
90250 6e 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74  nt values from t
90260 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64  he same.  ** add
90270 72 65 73 73 20 61 74 20 74 68 65 20 73 61 6d 65  ress at the same
90280 20 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65   time.  If eithe
90290 72 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63  r of these two c
902a0 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61  onditions.  ** a
902b0 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e  re not met, then
902c0 20 74 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c   the mutexes wil
902d0 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c  l fail and probl
902e0 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e  ems will result.
902f0 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74  .  */.  {.    pt
90300 68 72 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70  hread_t self = p
90310 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20  thread_self();. 
90320 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30     if( p->nRef>0
90330 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61   && pthread_equa
90340 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66  l(p->owner, self
90350 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52  ) ){.      p->nR
90360 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ef++;.    }else{
90370 0a 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d  .      pthread_m
90380 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75  utex_lock(&p->mu
90390 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65  tex);.      asse
903a0 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  rt( p->nRef==0 )
903b0 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72  ;.      p->owner
903c0 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70   = self;.      p
903d0 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20  ->nRef = 1;.    
903e0 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a  }.  }.#else.  /*
903f0 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69   Use the built-i
90400 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  n recursive mute
90410 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20  xes if they are 
90420 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a  available..  */.
90430 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
90440 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b  lock(&p->mutex);
90450 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74  .  p->owner = pt
90460 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20  hread_self();.  
90470 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69  p->nRef++;.#endi
90480 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
90490 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e  _DEBUG.  if( p->
904a0 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69  trace ){.    pri
904b0 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78  ntf("enter mutex
904c0 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52   %p (%d) with nR
904d0 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e  ef=%d\n", p, p->
904e0 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b  trace, p->nRef);
904f0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 74  .  }.#endif.}.st
90500 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64  atic int pthread
90510 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33  MutexTry(sqlite3
90520 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e  _mutex *p){.  in
90530 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
90540 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
90550 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
90560 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74   pthreadMutexNot
90570 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64  held(p) );..#ifd
90580 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52  ef SQLITE_HOMEGR
90590 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55  OWN_RECURSIVE_MU
905a0 54 45 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75  TEX.  /* If recu
905b0 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72  rsive mutexes ar
905c0 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c  e not available,
905d0 20 74 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f   then we have to
905e0 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f   grow.  ** our o
905f0 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d  wn.  This implem
90600 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73  entation assumes
90610 20 74 68 61 74 20 70 74 68 72 65 61 64 5f 65 71   that pthread_eq
90620 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74  ual().  ** is at
90630 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 74 20 63  omic - that it c
90640 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65  annot be deceive
90650 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20  d into thinking 
90660 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d  self.  ** and p-
90670 3e 6f 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c  >owner are equal
90680 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61   if p->owner cha
90690 6e 67 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f  nges between two
906a0 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61   values.  ** tha
906b0 74 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20  t are not equal 
906c0 74 6f 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68  to self while th
906d0 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  e comparison is 
906e0 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20  taking place..  
906f0 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  ** This implemen
90700 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75  tation also assu
90710 6d 65 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63  mes a coherent c
90720 61 63 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a  ache - that .  *
90730 2a 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65  * separate proce
90740 73 73 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64  sses cannot read
90750 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65   different value
90760 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a  s from the same.
90770 20 20 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20    ** address at 
90780 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20  the same time.  
90790 49 66 20 65 69 74 68 65 72 20 6f 66 20 74 68 65  If either of the
907a0 73 65 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e  se two condition
907b0 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d  s.  ** are not m
907c0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74  et, then the mut
907d0 65 78 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61  exes will fail a
907e0 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c  nd problems will
907f0 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20   result..  */.  
90800 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 74 20  {.    pthread_t 
90810 73 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73  self = pthread_s
90820 65 6c 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70  elf();.    if( p
90830 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72  ->nRef>0 && pthr
90840 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e  ead_equal(p->own
90850 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20  er, self) ){.   
90860 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20     p->nRef++;.  
90870 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
90880 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  OK;.    }else if
90890 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  ( pthread_mutex_
908a0 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  trylock(&p->mute
908b0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  x)==0 ){.      a
908c0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
908d0 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77  0 );.      p->ow
908e0 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20  ner = self;.    
908f0 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20    p->nRef = 1;. 
90900 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
90910 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  _OK;.    }else{.
90920 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
90930 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20  E_BUSY;.    }.  
90940 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65  }.#else.  /* Use
90950 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65   the built-in re
90960 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20  cursive mutexes 
90970 69 66 20 74 68 65 79 20 61 72 65 20 61 76 61 69  if they are avai
90980 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  lable..  */.  if
90990 28 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f  ( pthread_mutex_
909a0 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  trylock(&p->mute
909b0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  x)==0 ){.    p->
909c0 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f  owner = pthread_
909d0 73 65 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e  self();.    p->n
909e0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
909f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
90a00 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  se{.    rc = SQL
90a10 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65  ITE_BUSY;.  }.#e
90a20 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
90a30 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20  ITE_DEBUG.  if( 
90a40 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
90a50 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20   p->trace ){.   
90a60 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d   printf("enter m
90a70 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74  utex %p (%d) wit
90a80 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c  h nRef=%d\n", p,
90a90 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52   p->trace, p->nR
90aa0 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ef);.  }.#endif.
90ab0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
90ac0 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
90ad0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20  3_mutex_leave() 
90ae0 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20  routine exits a 
90af0 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a  mutex that was.*
90b00 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74  * previously ent
90b10 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65  ered by the same
90b20 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65   thread.  The be
90b30 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64  havior.** is und
90b40 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75  efined if the mu
90b50 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65  tex is not curre
90b60 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a  ntly entered or.
90b70 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  ** is not curren
90b80 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20  tly allocated.  
90b90 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65  SQLite will neve
90ba0 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a  r do either..*/.
90bb0 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 72  static void pthr
90bc0 65 61 64 4d 75 74 65 78 4c 65 61 76 65 28 73 71  eadMutexLeave(sq
90bd0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
90be0 0a 20 20 61 73 73 65 72 74 28 20 70 74 68 72 65  .  assert( pthre
90bf0 61 64 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29  adMutexHeld(p) )
90c00 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  ;.  p->nRef--;. 
90c10 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
90c20 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51  ==0 || p->id==SQ
90c30 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
90c40 53 49 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 20  SIVE );..#ifdef 
90c50 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e  SQLITE_HOMEGROWN
90c60 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58  _RECURSIVE_MUTEX
90c70 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d  .  if( p->nRef==
90c80 30 20 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64  0 ){.    pthread
90c90 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70  _mutex_unlock(&p
90ca0 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 65  ->mutex);.  }.#e
90cb0 6c 73 65 0a 20 20 70 74 68 72 65 61 64 5f 6d 75  lse.  pthread_mu
90cc0 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d  tex_unlock(&p->m
90cd0 75 74 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  utex);.#endif..#
90ce0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
90cf0 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63  UG.  if( p->trac
90d00 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28  e ){.    printf(
90d10 22 6c 65 61 76 65 20 6d 75 74 65 78 20 25 70 20  "leave mutex %p 
90d20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25  (%d) with nRef=%
90d30 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63  d\n", p, p->trac
90d40 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d  e, p->nRef);.  }
90d50 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 54  .#endif.}..SQLIT
90d60 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
90d70 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
90d80 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d  *sqlite3DefaultM
90d90 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74  utex(void){.  st
90da0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74  atic sqlite3_mut
90db0 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65  ex_methods sMute
90dc0 78 20 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61  x = {.    pthrea
90dd0 64 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20  dMutexInit,.    
90de0 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c  pthreadMutexEnd,
90df0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65  .    pthreadMute
90e00 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72  xAlloc,.    pthr
90e10 65 61 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20  eadMutexFree,.  
90e20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e    pthreadMutexEn
90e30 74 65 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64  ter,.    pthread
90e40 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74  MutexTry,.    pt
90e50 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c  hreadMutexLeave,
90e60 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
90e70 45 42 55 47 0a 20 20 20 20 70 74 68 72 65 61 64  EBUG.    pthread
90e80 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70  MutexHeld,.    p
90e90 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65  threadMutexNothe
90ea0 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a  ld.#else.    0,.
90eb0 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d      0.#endif.  }
90ec0 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75  ;..  return &sMu
90ed0 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  tex;.}..#endif /
90ee0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50  * SQLITE_MUTEX_P
90ef0 54 48 52 45 41 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  THREAD */../****
90f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
90f10 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a  f mutex_unix.c *
90f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
90f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
90f60 20 66 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 2e   file mutex_w32.
90f70 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
90f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
90fa0 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34 0a   2007 August 14.
90fb0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
90fc0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
90fd0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
90fe0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
90ff0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
91000 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
91010 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
91020 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
91030 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
91040 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
91050 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
91060 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
91070 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
91080 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
91090 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
910a0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
910b0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
910c0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
910d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
91110 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
91120 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
91130 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
91140 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20  ent mutexes for 
91150 77 69 6e 33 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  win32.**.** $Id:
91160 20 6d 75 74 65 78 5f 77 33 32 2e 63 2c 76 20 31   mutex_w32.c,v 1
91170 2e 31 35 20 32 30 30 39 2f 30 31 2f 33 30 20 31  .15 2009/01/30 1
91180 36 3a 30 39 3a 32 33 20 73 68 61 6e 65 20 45 78  6:09:23 shane Ex
91190 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  p $.*/../*.** Th
911a0 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
911b0 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ile is only used
911c0 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69   if we are compi
911d0 6c 69 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64  ling multithread
911e0 65 64 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32  ed.** on a win32
911f0 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64   system..*/.#ifd
91200 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
91210 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  W32../*.** Each 
91220 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
91230 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
91240 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
91250 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
91260 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74  ruct sqlite3_mut
91270 65 78 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f  ex {.  CRITICAL_
91280 53 45 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20  SECTION mutex;  
91290 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72    /* Mutex contr
912a0 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  olling the lock 
912b0 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20  */.  int id;    
912c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
912d0 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f  /* Mutex type */
912e0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
912f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
91300 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72   Number of enter
91310 61 6e 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44  ances */.  DWORD
91320 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20   owner;         
91330 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20        /* Thread 
91340 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74  holding this mut
91350 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ex */.};../*.** 
91360 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e  Return true (non
91370 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65  -zero) if we are
91380 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57   running under W
91390 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e  inNT, Win2K, Win
913a0 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e  XP,.** or WinCE.
913b0 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28    Return false (
913c0 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c  zero) for Win95,
913d0 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45   Win98, or WinME
913e0 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20  ..**.** Here is 
913f0 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f  an interesting o
91400 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e  bservation:  Win
91410 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57  95, Win98, and W
91420 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65  inME lack.** the
91430 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50   LockFileEx() AP
91440 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73  I.  But we can s
91450 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20  till statically 
91460 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61  link against tha
91470 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67  t.** API as long
91480 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c   as we don't cal
91490 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67  l it win running
914a0 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41   Win95/98/ME.  A
914b0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73   call to.** this
914c0 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
914d0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66   to determine if
914e0 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e   the host is Win
914f0 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57  95/98/ME or.** W
91500 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68  inNT/2K/XP so th
91510 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20  at we will know 
91520 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77  whether or not w
91530 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c  e can safely cal
91540 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c  l.** the LockFil
91550 65 45 78 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a  eEx() API..**.**
91560 20 6d 75 74 65 78 49 73 4e 54 28 29 20 69 73 20   mutexIsNT() is 
91570 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 74 68  only used for th
91580 65 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63  e TryEnterCritic
91590 61 6c 53 65 63 74 69 6f 6e 28 29 20 41 50 49 20  alSection() API 
915a0 63 61 6c 6c 2c 0a 2a 2a 20 77 68 69 63 68 20 69  call,.** which i
915b0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
915c0 20 69 66 20 79 6f 75 72 20 61 70 70 6c 69 63 61   if your applica
915d0 74 69 6f 6e 20 77 61 73 20 63 6f 6d 70 69 6c 65  tion was compile
915e0 64 20 77 69 74 68 20 0a 2a 2a 20 5f 57 49 4e 33  d with .** _WIN3
915f0 32 5f 57 49 4e 4e 54 20 64 65 66 69 6e 65 64 20  2_WINNT defined 
91600 74 6f 20 61 20 76 61 6c 75 65 20 3e 3d 20 30 78  to a value >= 0x
91610 30 34 30 30 2e 20 20 43 75 72 72 65 6e 74 6c 79  0400.  Currently
91620 2c 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 63 61  , the only.** ca
91630 6c 6c 20 74 6f 20 54 72 79 45 6e 74 65 72 43 72  ll to TryEnterCr
91640 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20  iticalSection() 
91650 69 73 20 23 69 66 64 65 66 27 65 64 20 6f 75 74  is #ifdef'ed out
91660 2c 20 73 6f 20 23 69 66 64 65 66 20 0a 2a 2a 20  , so #ifdef .** 
91670 74 68 69 73 20 6f 75 74 20 61 73 20 77 65 6c 6c  this out as well
91680 2e 0a 2a 2f 0a 23 69 66 20 30 0a 23 69 66 20 53  ..*/.#if 0.#if S
91690 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23  QLITE_OS_WINCE.#
916a0 20 64 65 66 69 6e 65 20 6d 75 74 65 78 49 73 4e   define mutexIsN
916b0 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20  T()  (1).#else. 
916c0 20 73 74 61 74 69 63 20 69 6e 74 20 6d 75 74 65   static int mute
916d0 78 49 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20  xIsNT(void){.   
916e0 20 73 74 61 74 69 63 20 69 6e 74 20 6f 73 54 79   static int osTy
916f0 70 65 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  pe = 0;.    if( 
91700 6f 73 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20  osType==0 ){.   
91710 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f     OSVERSIONINFO
91720 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49   sInfo;.      sI
91730 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49  nfo.dwOSVersionI
91740 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66  nfoSize = sizeof
91750 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47  (sInfo);.      G
91760 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e  etVersionEx(&sIn
91770 66 6f 29 3b 0a 20 20 20 20 20 20 6f 73 54 79 70  fo);.      osTyp
91780 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74  e = sInfo.dwPlat
91790 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54  formId==VER_PLAT
917a0 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20  FORM_WIN32_NT ? 
917b0 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  2 : 1;.    }.   
917c0 20 72 65 74 75 72 6e 20 6f 73 54 79 70 65 3d 3d   return osType==
917d0 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  2;.  }.#endif /*
917e0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
917f0 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64   */.#endif..#ifd
91800 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
91810 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
91820 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61  3_mutex_held() a
91830 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
91840 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69  _notheld() routi
91850 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64  ne are.** intend
91860 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20  ed for use only 
91870 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
91880 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  statements..*/.s
91890 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74  tatic int winMut
918a0 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  exHeld(sqlite3_m
918b0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
918c0 72 6e 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26  rn p->nRef!=0 &&
918d0 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75   p->owner==GetCu
918e0 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b  rrentThreadId();
918f0 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  .}.static int wi
91900 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71  nMutexNotheld(sq
91910 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
91920 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65  .  return p->nRe
91930 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72  f==0 || p->owner
91940 21 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  !=GetCurrentThre
91950 61 64 49 64 28 29 3b 0a 7d 0a 23 65 6e 64 69 66  adId();.}.#endif
91960 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  .../*.** Initial
91970 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61  ize and deinitia
91980 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73  lize the mutex s
91990 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61  ubsystem..*/.sta
919a0 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78  tic int winMutex
919b0 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75  Init(void){ retu
919c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
919d0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75  static int winMu
919e0 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
919f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
91a00 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
91a10 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
91a20 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63  () routine alloc
91a30 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75  ates a new.** mu
91a40 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20  tex and returns 
91a50 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
91a60 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20    If it returns 
91a70 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61  NULL.** that mea
91a80 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20  ns that a mutex 
91a90 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c  could not be all
91aa0 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a  ocated.  SQLite.
91ab0 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69  ** will unwind i
91ac0 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74  ts stack and ret
91ad0 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54  urn an error.  T
91ae0 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  he argument.** t
91af0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
91b00 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f  alloc() is one o
91b10 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20  f these integer 
91b20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a  constants:.**.**
91b30 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53   <ul>.** <li>  S
91b40 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
91b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
91b60 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
91b70 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
91b80 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 3c            1.** <
91b90 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
91ba0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  X_STATIC_MASTER 
91bb0 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 20       2.** <li>  
91bc0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
91bd0 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 20  TIC_MEM         
91be0 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  3.** <li>  SQLIT
91bf0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50  E_MUTEX_STATIC_P
91c00 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a 20  RNG        4.** 
91c10 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  </ul>.**.** The 
91c20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61  first two consta
91c30 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65  nts cause sqlite
91c40 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
91c50 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e  to create.** a n
91c60 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e  ew mutex.  The n
91c70 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75  ew mutex is recu
91c80 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54  rsive when SQLIT
91c90 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
91ca0 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74  E.** is used but
91cb0 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79   not necessarily
91cc0 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f   so when SQLITE_
91cd0 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73  MUTEX_FAST is us
91ce0 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78  ed..** The mutex
91cf0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
91d00 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
91d10 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74   make a distinct
91d20 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53  ion.** between S
91d30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
91d40 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45  RSIVE and SQLITE
91d50 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69  _MUTEX_FAST if i
91d60 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61  t does.** not wa
91d70 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69  nt to.  But SQLi
91d80 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71  te will only req
91d90 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 65  uest a recursive
91da0 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73   mutex in.** cas
91db0 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 6c  es where it real
91dc0 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49  ly needs one.  I
91dd0 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72  f a faster non-r
91de0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a  ecursive mutex.*
91df0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
91e00 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e   is available on
91e10 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f   the host platfo
91e20 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75  rm, the mutex su
91e30 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74  bsystem.** might
91e40 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d   return such a m
91e50 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65  utex in response
91e60 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   to SQLITE_MUTEX
91e70 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  _FAST..**.** The
91e80 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70   other allowed p
91e90 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c  arameters to sql
91ea0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
91eb0 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a  () each return.*
91ec0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
91ed0 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 74   static preexist
91ee0 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 65  ing mutex.  Thre
91ef0 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  e static mutexes
91f00 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20   are.** used by 
91f10 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73  the current vers
91f20 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ion of SQLite.  
91f30 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
91f40 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79  of SQLite.** may
91f50 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
91f60 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20  static mutexes. 
91f70 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   Static mutexes 
91f80 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
91f90 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
91fa0 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61  e only.  Applica
91fb0 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53  tions that use S
91fc0 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68  QLite mutexes sh
91fd0 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79  ould.** use only
91fe0 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
91ff0 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  exes returned by
92000 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
92010 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  ST or.** SQLITE_
92020 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e  MUTEX_RECURSIVE.
92030 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
92040 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64   if one of the d
92050 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72  ynamic mutex par
92060 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f  ameters (SQLITE_
92070 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72  MUTEX_FAST.** or
92080 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
92090 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64  CURSIVE) is used
920a0 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75   then sqlite3_mu
920b0 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72  tex_alloc().** r
920c0 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65  eturns a differe
920d0 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72  nt mutex on ever
920e0 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72  y call.  But for
920f0 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20   the static .** 
92100 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
92110 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
92120 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
92130 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
92140 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
92150 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
92160 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
92170 2a 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 28 69  *winMutexAlloc(i
92180 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c  nt iType){.  sql
92190 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 0a  ite3_mutex *p;..
921a0 20 20 73 77 69 74 63 68 28 20 69 54 79 70 65 20    switch( iType 
921b0 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
921c0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20  TE_MUTEX_FAST:. 
921d0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
921e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20  UTEX_RECURSIVE: 
921f0 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69  {.      p = sqli
92200 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
92210 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20  izeof(*p) );.   
92220 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
92230 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70      p->id = iTyp
92240 65 3b 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69  e;.        Initi
92250 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63  alizeCriticalSec
92260 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b  tion(&p->mutex);
92270 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
92280 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
92290 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
922a0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
922b0 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65  mutex staticMute
922c0 78 65 73 5b 36 5d 3b 0a 20 20 20 20 20 20 73 74  xes[6];.      st
922d0 61 74 69 63 20 69 6e 74 20 69 73 49 6e 69 74 20  atic int isInit 
922e0 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  = 0;.      while
922f0 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  ( !isInit ){.   
92300 20 20 20 20 20 73 74 61 74 69 63 20 6c 6f 6e 67       static long
92310 20 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20   lock = 0;.     
92320 20 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b     if( Interlock
92330 65 64 49 6e 63 72 65 6d 65 6e 74 28 26 6c 6f 63  edIncrement(&loc
92340 6b 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  k)==1 ){.       
92350 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
92360 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
92370 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65  izeof(staticMute
92380 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74  xes)/sizeof(stat
92390 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 69  icMutexes[0]); i
923a0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
923b0 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69   InitializeCriti
923c0 63 61 6c 53 65 63 74 69 6f 6e 28 26 73 74 61 74  calSection(&stat
923d0 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74  icMutexes[i].mut
923e0 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ex);.          }
923f0 0a 20 20 20 20 20 20 20 20 20 20 69 73 49 6e 69  .          isIni
92400 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  t = 1;.        }
92410 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
92420 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 20 20  Sleep(1);.      
92430 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
92440 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d    assert( iType-
92450 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20  2 >= 0 );.      
92460 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20  assert( iType-2 
92470 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d  < sizeof(staticM
92480 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73  utexes)/sizeof(s
92490 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29  taticMutexes[0])
924a0 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73   );.      p = &s
924b0 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79  taticMutexes[iTy
924c0 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e  pe-2];.      p->
924d0 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20  id = iType;.    
924e0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
924f0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
92500 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
92510 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65  utine deallocate
92520 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  s a previously.*
92530 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65  * allocated mute
92540 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 61  x.  SQLite is ca
92550 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63  reful to dealloc
92560 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74  ate every.** mut
92570 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63  ex that it alloc
92580 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ates..*/.static 
92590 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 46 72 65  void winMutexFre
925a0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
925b0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
925c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
925d0 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73  >nRef==0 );.  as
925e0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
925f0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c  ITE_MUTEX_FAST |
92600 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  | p->id==SQLITE_
92610 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
92620 29 3b 0a 20 20 44 65 6c 65 74 65 43 72 69 74 69  );.  DeleteCriti
92630 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d  calSection(&p->m
92640 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  utex);.  sqlite3
92650 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  _free(p);.}../*.
92660 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
92670 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64  utex_enter() and
92680 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
92690 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74  ry() routines at
926a0 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65  tempt.** to ente
926b0 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61  r a mutex.  If a
926c0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73  nother thread is
926d0 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20   already within 
926e0 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71  the mutex,.** sq
926f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
92700 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61  r() will block a
92710 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
92720 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75  _try() will retu
92730 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53  rn.** SQLITE_BUS
92740 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  Y.  The sqlite3_
92750 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
92760 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51  rface returns SQ
92770 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20  LITE_OK.** upon 
92780 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79  successful entry
92790 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74  .  Mutexes creat
927a0 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f  ed using SQLITE_
927b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
927c0 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65  can.** be entere
927d0 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
927e0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72   by the same thr
927f0 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61  ead.  In such ca
92800 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65  ses the,.** mute
92810 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64  x must be exited
92820 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72   an equal number
92830 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65   of times before
92840 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a   another thread.
92850 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49  ** can enter.  I
92860 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  f the same threa
92870 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72  d tries to enter
92880 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20   any other kind 
92890 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65  of mutex.** more
928a0 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20   than once, the 
928b0 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
928c0 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  fined..*/.static
928d0 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 45 6e   void winMutexEn
928e0 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ter(sqlite3_mute
928f0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
92900 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
92910 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
92920 7c 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c  | winMutexNothel
92930 64 28 70 29 20 29 3b 0a 20 20 45 6e 74 65 72 43  d(p) );.  EnterC
92940 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26  riticalSection(&
92950 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e  p->mutex);.  p->
92960 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65  owner = GetCurre
92970 6e 74 54 68 72 65 61 64 49 64 28 29 3b 20 0a 20  ntThreadId(); . 
92980 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74   p->nRef++;.}.st
92990 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65  atic int winMute
929a0 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  xTry(sqlite3_mut
929b0 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  ex *p){.  int rc
929c0 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
929d0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
929e0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
929f0 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75  CURSIVE || winMu
92a00 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
92a10 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 73  .  /*.  ** The s
92a20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
92a30 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 76 65  () routine is ve
92a40 72 79 20 72 61 72 65 6c 79 20 75 73 65 64 2c 20  ry rarely used, 
92a50 61 6e 64 20 77 68 65 6e 20 69 74 0a 20 20 2a 2a  and when it.  **
92a60 20 69 73 20 75 73 65 64 20 69 74 20 69 73 20 6d   is used it is m
92a70 65 72 65 6c 79 20 61 6e 20 6f 70 74 69 6d 69 7a  erely an optimiz
92a80 61 74 69 6f 6e 2e 20 20 53 6f 20 69 74 20 69 73  ation.  So it is
92a90 20 4f 4b 20 66 6f 72 20 69 74 20 74 6f 20 61 6c   OK for it to al
92aa0 77 61 79 73 0a 20 20 2a 2a 20 66 61 69 6c 2e 20  ways.  ** fail. 
92ab0 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20   .  **.  ** The 
92ac0 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c  TryEnterCritical
92ad0 53 65 63 74 69 6f 6e 28 29 20 69 6e 74 65 72 66  Section() interf
92ae0 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69  ace is only avai
92af0 6c 61 62 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e 0a  lable on WinNT..
92b00 20 20 2a 2a 20 41 6e 64 20 73 6f 6d 65 20 77 69    ** And some wi
92b10 6e 64 6f 77 73 20 63 6f 6d 70 69 6c 65 72 73 20  ndows compilers 
92b20 63 6f 6d 70 6c 61 69 6e 20 69 66 20 79 6f 75 20  complain if you 
92b30 74 72 79 20 74 6f 20 75 73 65 20 69 74 20 77 69  try to use it wi
92b40 74 68 6f 75 74 0a 20 20 2a 2a 20 66 69 72 73 74  thout.  ** first
92b50 20 64 6f 69 6e 67 20 73 6f 6d 65 20 23 64 65 66   doing some #def
92b60 69 6e 65 73 20 74 68 61 74 20 70 72 65 76 65 6e  ines that preven
92b70 74 20 53 51 4c 69 74 65 20 66 72 6f 6d 20 62 75  t SQLite from bu
92b80 69 6c 64 69 6e 67 20 6f 6e 20 57 69 6e 39 38 2e  ilding on Win98.
92b90 0a 20 20 2a 2a 20 46 6f 72 20 74 68 61 74 20 72  .  ** For that r
92ba0 65 61 73 6f 6e 2c 20 77 65 20 77 69 6c 6c 20 6f  eason, we will o
92bb0 6d 69 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a  mit this optimiz
92bc0 61 74 69 6f 6e 20 66 6f 72 20 6e 6f 77 2e 20 20  ation for now.  
92bd0 53 65 65 0a 20 20 2a 2a 20 74 69 63 6b 65 74 20  See.  ** ticket 
92be0 23 32 36 38 35 2e 0a 20 20 2a 2f 0a 23 69 66 20  #2685..  */.#if 
92bf0 30 0a 20 20 69 66 28 20 6d 75 74 65 78 49 73 4e  0.  if( mutexIsN
92c00 54 28 29 20 26 26 20 54 72 79 45 6e 74 65 72 43  T() && TryEnterC
92c10 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26  riticalSection(&
92c20 70 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 20  p->mutex) ){.   
92c30 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43   p->owner = GetC
92c40 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29  urrentThreadId()
92c50 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  ;.    p->nRef++;
92c60 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
92c70 5f 4f 4b 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  _OK;.  }.#else. 
92c80 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
92c90 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72  R(p);.#endif.  r
92ca0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
92cb0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
92cc0 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75  utex_leave() rou
92cd0 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74  tine exits a mut
92ce0 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70  ex that was.** p
92cf0 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65  reviously entere
92d00 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  d by the same th
92d10 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76  read.  The behav
92d20 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  ior.** is undefi
92d30 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78  ned if the mutex
92d40 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
92d50 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20  y entered or.** 
92d60 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
92d70 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c   allocated.  SQL
92d80 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64  ite will never d
92d90 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61  o either..*/.sta
92da0 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65  tic void winMute
92db0 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d  xLeave(sqlite3_m
92dc0 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65  utex *p){.  asse
92dd0 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b  rt( p->nRef>0 );
92de0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77  .  assert( p->ow
92df0 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54  ner==GetCurrentT
92e00 68 72 65 61 64 49 64 28 29 20 29 3b 0a 20 20 70  hreadId() );.  p
92e10 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65  ->nRef--;.  asse
92e20 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c  rt( p->nRef==0 |
92e30 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  | p->id==SQLITE_
92e40 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
92e50 29 3b 0a 20 20 4c 65 61 76 65 43 72 69 74 69 63  );.  LeaveCritic
92e60 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
92e70 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f  tex);.}..SQLITE_
92e80 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
92e90 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73  mutex_methods *s
92ea0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74  qlite3DefaultMut
92eb0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ex(void){.  stat
92ec0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
92ed0 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20  _methods sMutex 
92ee0 3d 20 7b 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  = {.    winMutex
92ef0 49 6e 69 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74  Init,.    winMut
92f00 65 78 45 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75  exEnd,.    winMu
92f10 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69  texAlloc,.    wi
92f20 6e 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  nMutexFree,.    
92f30 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20  winMutexEnter,. 
92f40 20 20 20 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a     winMutexTry,.
92f50 20 20 20 20 77 69 6e 4d 75 74 65 78 4c 65 61 76      winMutexLeav
92f60 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  e,.#ifdef SQLITE
92f70 5f 44 45 42 55 47 0a 20 20 20 20 77 69 6e 4d 75  _DEBUG.    winMu
92f80 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e  texHeld,.    win
92f90 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6c  MutexNotheld.#el
92fa0 73 65 0a 20 20 20 20 30 2c 0a 20 20 20 20 30 0a  se.    0,.    0.
92fb0 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72  #endif.  };..  r
92fc0 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d  eturn &sMutex;.}
92fd0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
92fe0 45 5f 4d 55 54 45 58 5f 57 33 32 20 2a 2f 0a 0a  E_MUTEX_W32 */..
92ff0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
93000 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 77 33 32  End of mutex_w32
93010 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
93020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
93040 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
93050 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f  Begin file mallo
93060 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c.c ************
93070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
93090 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
930a0 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
930b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
930c0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
930d0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
930e0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
930f0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
93100 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
93110 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
93120 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
93130 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
93140 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
93150 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
93160 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
93170 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
93180 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
93190 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
931a0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
931b0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
931c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
931d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
931e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
931f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93200 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d  ******.**.** Mem
93210 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
93220 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68  unctions used th
93230 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e  roughout sqlite.
93240 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c  .**.** $Id: mall
93250 6f 63 2e 63 2c 76 20 31 2e 36 32 20 32 30 30 39  oc.c,v 1.62 2009
93260 2f 30 35 2f 30 33 20 32 30 3a 32 33 3a 35 34 20  /05/03 20:23:54 
93270 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
93280 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
93290 20 72 75 6e 73 20 77 68 65 6e 20 74 68 65 20 6d   runs when the m
932a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
932b0 73 65 65 73 20 74 68 61 74 20 74 68 65 0a 2a 2a  sees that the.**
932c0 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c   total memory al
932d0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 62 6f 75  location is abou
932e0 74 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 20  t to exceed the 
932f0 73 6f 66 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d  soft heap.** lim
93300 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  it..*/.static vo
93310 69 64 20 73 6f 66 74 48 65 61 70 4c 69 6d 69 74  id softHeapLimit
93320 45 6e 66 6f 72 63 65 72 28 0a 20 20 76 6f 69 64  Enforcer(.  void
93330 20 2a 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71   *NotUsed, .  sq
93340 6c 69 74 65 33 5f 69 6e 74 36 34 20 4e 6f 74 55  lite3_int64 NotU
93350 73 65 64 32 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f  sed2,.  int allo
93360 63 53 69 7a 65 0a 29 7b 0a 20 20 55 4e 55 53 45  cSize.){.  UNUSE
93370 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
93380 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
93390 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  .  sqlite3_relea
933a0 73 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c 6f 63 53  se_memory(allocS
933b0 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ize);.}../*.** S
933c0 65 74 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  et the soft heap
933d0 2d 73 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20  -size limit for 
933e0 74 68 65 20 6c 69 62 72 61 72 79 2e 20 50 61 73  the library. Pas
933f0 73 69 6e 67 20 61 20 7a 65 72 6f 20 6f 72 20 0a  sing a zero or .
93400 2a 2a 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75  ** negative valu
93410 65 20 69 6e 64 69 63 61 74 65 73 20 6e 6f 20 6c  e indicates no l
93420 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  imit..*/.SQLITE_
93430 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
93440 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
93450 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74  (int n){.  sqlit
93460 65 33 5f 75 69 6e 74 36 34 20 69 4c 69 6d 69 74  e3_uint64 iLimit
93470 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61 67 65 3b  ;.  int overage;
93480 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20  .  if( n<0 ){.  
93490 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20    iLimit = 0;.  
934a0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69  }else{.    iLimi
934b0 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c  t = n;.  }.  sql
934c0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
934d0 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e  );.  if( iLimit>
934e0 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
934f0 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 74  MemoryAlarm(soft
93500 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65  HeapLimitEnforce
93510 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a 20  r, 0, iLimit);. 
93520 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
93530 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 30  te3MemoryAlarm(0
93540 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 6f  , 0, 0);.  }.  o
93550 76 65 72 61 67 65 20 3d 20 28 69 6e 74 29 28 73  verage = (int)(s
93560 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
93570 65 64 28 29 20 2d 20 28 69 36 34 29 6e 29 3b 0a  ed() - (i64)n);.
93580 20 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30 20    if( overage>0 
93590 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
935a0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f 76  elease_memory(ov
935b0 65 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  erage);.  }.}../
935c0 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
935d0 72 65 6c 65 61 73 65 20 75 70 20 74 6f 20 6e 20  release up to n 
935e0 62 79 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 73  bytes of non-ess
935f0 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 75  ential memory cu
93600 72 72 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 20  rrently.** held 
93610 62 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 78  by SQLite. An ex
93620 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73  ample of non-ess
93630 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 73  ential memory is
93640 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 0a   memory used to.
93650 2a 2a 20 63 61 63 68 65 20 64 61 74 61 62 61 73  ** cache databas
93660 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65  e pages that are
93670 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 69   not currently i
93680 6e 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  n use..*/.SQLITE
93690 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
936a0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
936b0 69 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 53  int n){.#ifdef S
936c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
936d0 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 20  ORY_MANAGEMENT. 
936e0 20 69 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a 23   int nRet = 0;.#
936f0 69 66 20 30 0a 20 20 6e 52 65 74 20 2b 3d 20 73  if 0.  nRet += s
93700 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73  qlite3VdbeReleas
93710 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a 23 65 6e 64  eMemory(n);.#end
93720 69 66 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 6c  if.  nRet += sql
93730 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73  ite3PcacheReleas
93740 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 29 3b  eMemory(n-nRet);
93750 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74 3b 0a  .  return nRet;.
93760 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50  #else.  UNUSED_P
93770 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 72  ARAMETER(n);.  r
93780 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
93790 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
937a0 20 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69   State informati
937b0 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 20  on local to the 
937c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
937d0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  n subsystem..*/.
937e0 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
937f0 44 20 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f  D struct Mem0Glo
93800 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 65  bal {.  /* Numbe
93810 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20  r of free pages 
93820 66 6f 72 20 73 63 72 61 74 63 68 20 61 6e 64 20  for scratch and 
93830 70 61 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f 72  page-cache memor
93840 79 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 61  y */.  u32 nScra
93850 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 6e  tchFree;.  u32 n
93860 50 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 6c  PageFree;..  sql
93870 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
93880 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75  x;         /* Mu
93890 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69 7a 65  tex to serialize
938a0 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f 2a   access */..  /*
938b0 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d 20  .  ** The alarm 
938c0 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 73  callback and its
938d0 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 65   arguments.  The
938e0 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 6b   mem0.mutex lock
938f0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 65   will.  ** be he
93900 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 6c  ld while the cal
93910 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e 67  lback is running
93920 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c  .  Recursive cal
93930 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  ls into.  ** the
93940 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65   memory subsyste
93950 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 62  m are allowed, b
93960 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 61  ut no new callba
93970 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a  cks will be.  **
93980 20 69 73 73 75 65 64 2e 20 20 54 68 65 20 61 6c   issued.  The al
93990 61 72 6d 42 75 73 79 20 76 61 72 69 61 62 6c 65  armBusy variable
939a0 20 69 73 20 73 65 74 20 74 6f 20 70 72 65 76 65   is set to preve
939b0 6e 74 20 72 65 63 75 72 73 69 76 65 0a 20 20 2a  nt recursive.  *
939c0 2a 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a  * callbacks..  *
939d0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
939e0 34 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64  4 alarmThreshold
939f0 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d  ;.  void (*alarm
93a00 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c  Callback)(void*,
93a10 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69   sqlite3_int64,i
93a20 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61  nt);.  void *ala
93a30 72 6d 41 72 67 3b 0a 20 20 69 6e 74 20 61 6c 61  rmArg;.  int ala
93a40 72 6d 42 75 73 79 3b 0a 0a 20 20 2f 2a 0a 20 20  rmBusy;..  /*.  
93a50 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74  ** Pointers to t
93a60 68 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65  he end of sqlite
93a70 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53  3GlobalConfig.pS
93a80 63 72 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20  cratch and.  ** 
93a90 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
93aa0 66 69 67 2e 70 50 61 67 65 20 74 6f 20 61 20 62  fig.pPage to a b
93ab0 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74  lock of memory t
93ac0 68 61 74 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a  hat records.  **
93ad0 20 77 68 69 63 68 20 70 61 67 65 73 20 61 72 65   which pages are
93ae0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f   available..  */
93af0 0a 20 20 75 33 32 20 2a 61 53 63 72 61 74 63 68  .  u32 *aScratch
93b00 46 72 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 61  Free;.  u32 *aPa
93b10 67 65 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d  geFree;.} mem0 =
93b20 20 7b 20 36 32 35 36 30 39 35 35 2c 20 30 2c 20   { 62560955, 0, 
93b30 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
93b40 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 69 6e 65 20  , 0 };..#define 
93b50 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 73 74 72 75  mem0 GLOBAL(stru
93b60 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 2c 20 6d  ct Mem0Global, m
93b70 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  em0)../*.** Init
93b80 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
93b90 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
93ba0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  system..*/.SQLIT
93bb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
93bc0 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28  lite3MallocInit(
93bd0 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c  void){.  if( sql
93be0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
93bf0 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 7b  .m.xMalloc==0 ){
93c00 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53  .    sqlite3MemS
93c10 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d  etDefault();.  }
93c20 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c  .  memset(&mem0,
93c30 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29   0, sizeof(mem0)
93c40 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
93c50 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
93c60 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 6d  reMutex ){.    m
93c70 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69  em0.mutex = sqli
93c80 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
93c90 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
93ca0 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66  C_MEM);.  }.  if
93cb0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
93cc0 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 26  onfig.pScratch &
93cd0 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  & sqlite3GlobalC
93ce0 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3e  onfig.szScratch>
93cf0 3d 31 30 30 0a 20 20 20 20 20 20 26 26 20 73 71  =100.      && sq
93d00 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
93d10 67 2e 6e 53 63 72 61 74 63 68 3e 3d 30 20 29 7b  g.nScratch>=0 ){
93d20 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
93d30 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
93d40 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20  fig.szScratch = 
93d50 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74  ROUNDDOWN8(sqlit
93d60 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
93d70 7a 53 63 72 61 74 63 68 2d 34 29 3b 0a 20 20 20  zScratch-4);.   
93d80 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72   mem0.aScratchFr
93d90 65 65 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68  ee = (u32*)&((ch
93da0 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61  ar*)sqlite3Globa
93db0 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
93dc0 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
93dd0 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f 62      [sqlite3Glob
93de0 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
93df0 63 68 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ch*sqlite3Global
93e00 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 5d  Config.nScratch]
93e10 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
93e20 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  <sqlite3GlobalCo
93e30 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3b 20 69  nfig.nScratch; i
93e40 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 63 72 61 74  ++){ mem0.aScrat
93e50 63 68 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d  chFree[i] = i; }
93e60 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61 74  .    mem0.nScrat
93e70 63 68 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33  chFree = sqlite3
93e80 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63  GlobalConfig.nSc
93e90 72 61 74 63 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ratch;.  }else{.
93ea0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
93eb0 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
93ec0 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
93ed0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
93ee0 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 7d  Scratch = 0;.  }
93ef0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
93f00 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65  obalConfig.pPage
93f10 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
93f20 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3e 3d  lConfig.szPage>=
93f30 35 31 32 0a 20 20 20 20 20 20 26 26 20 73 71 6c  512.      && sql
93f40 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
93f50 2e 6e 50 61 67 65 3e 3d 31 20 29 7b 0a 20 20 20  .nPage>=1 ){.   
93f60 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20   int i;.    int 
93f70 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20 69 6e  overhead;.    in
93f80 74 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e  t sz = ROUNDDOWN
93f90 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  8(sqlite3GlobalC
93fa0 6f 6e 66 69 67 2e 73 7a 50 61 67 65 29 3b 0a 20  onfig.szPage);. 
93fb0 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74     int n = sqlit
93fc0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
93fd0 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72 68 65  Page;.    overhe
93fe0 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a 20 2d  ad = (4*n + sz -
93ff0 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71 6c 69   1)/sz;.    sqli
94000 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
94010 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68 65 61  nPage -= overhea
94020 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50 61 67  d;.    mem0.aPag
94030 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26 28  eFree = (u32*)&(
94040 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c  (char*)sqlite3Gl
94050 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65  obalConfig.pPage
94060 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
94070 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f 62      [sqlite3Glob
94080 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2a  alConfig.szPage*
94090 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
940a0 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20 20 20  fig.nPage];.    
940b0 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69 74  for(i=0; i<sqlit
940c0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
940d0 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30  Page; i++){ mem0
940e0 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20 3d 20  .aPageFree[i] = 
940f0 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 50  i; }.    mem0.nP
94100 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69 74 65  ageFree = sqlite
94110 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50  3GlobalConfig.nP
94120 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  age;.  }else{.  
94130 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
94140 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 30 3b  onfig.pPage = 0;
94150 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
94160 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 20  alConfig.szPage 
94170 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
94180 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  n sqlite3GlobalC
94190 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28 73 71  onfig.m.xInit(sq
941a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
941b0 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b 0a 7d  g.m.pAppData);.}
941c0 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61  ../*.** Deinitia
941d0 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  lize the memory 
941e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
941f0 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  stem..*/.SQLITE_
94200 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
94210 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f  ite3MallocEnd(vo
94220 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74  id){.  if( sqlit
94230 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
94240 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20  .xShutdown ){.  
94250 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
94260 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77  onfig.m.xShutdow
94270 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  n(sqlite3GlobalC
94280 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61  onfig.m.pAppData
94290 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  );.  }.  memset(
942a0 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66  &mem0, 0, sizeof
942b0 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (mem0));.}../*.*
942c0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f  * Return the amo
942d0 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 75  unt of memory cu
942e0 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20  rrently checked 
942f0 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  out..*/.SQLITE_A
94300 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  PI sqlite3_int64
94310 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f   sqlite3_memory_
94320 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e  used(void){.  in
94330 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74  t n, mx;.  sqlit
94340 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 20  e3_int64 res;.  
94350 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53  sqlite3_status(S
94360 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
94370 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d  ORY_USED, &n, &m
94380 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 3d 20 28  x, 0);.  res = (
94390 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6e 3b  sqlite3_int64)n;
943a0 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64    /* Work around
943b0 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20   bug in Borland 
943c0 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20  C. Ticket #3216 
943d0 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b  */.  return res;
943e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
943f0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f   the maximum amo
94400 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68  unt of memory th
94410 61 74 20 68 61 73 20 65 76 65 72 20 62 65 65 6e  at has ever been
94420 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 20  .** checked out 
94430 73 69 6e 63 65 20 65 69 74 68 65 72 20 74 68 65  since either the
94440 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
94450 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 6f 72  is process.** or
94460 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20   since the most 
94470 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a 2a 2f  recent reset..*/
94480 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
94490 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
944a0 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
944b0 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67  er(int resetFlag
944c0 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a  ){.  int n, mx;.
944d0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
944e0 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  res;.  sqlite3_s
944f0 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41  tatus(SQLITE_STA
94500 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c  TUS_MEMORY_USED,
94510 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 65 74 46   &n, &mx, resetF
94520 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d 20 28 73  lag);.  res = (s
94530 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6d 78 3b  qlite3_int64)mx;
94540 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 6e 64    /* Work around
94550 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e 64 20   bug in Borland 
94560 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 36 20  C. Ticket #3216 
94570 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b  */.  return res;
94580 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
94590 20 74 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62   the alarm callb
945a0 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ack.*/.SQLITE_PR
945b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
945c0 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 0a 20 20  3MemoryAlarm(.  
945d0 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29  void(*xCallback)
945e0 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c  (void *pArg, sql
945f0 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c  ite3_int64 used,
94600 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a  int N),.  void *
94610 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f  pArg,.  sqlite3_
94620 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64  int64 iThreshold
94630 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  .){.  sqlite3_mu
94640 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
94650 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e 61 6c  utex);.  mem0.al
94660 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43  armCallback = xC
94670 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 30 2e  allback;.  mem0.
94680 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67 3b  alarmArg = pArg;
94690 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72  .  mem0.alarmThr
946a0 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 65 73 68  eshold = iThresh
946b0 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  old;.  sqlite3_m
946c0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
946d0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
946e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
946f0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
94700 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
94710 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 20 65  .** Deprecated e
94720 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63  xternal interfac
94730 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f 72  e.  Internal/cor
94740 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a 2a 2a  e SQLite code.**
94750 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c   should call sql
94760 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 2e  ite3MemoryAlarm.
94770 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
94780 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  nt sqlite3_memor
94790 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28  y_alarm(.  void(
947a0 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  *xCallback)(void
947b0 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33 5f   *pArg, sqlite3_
947c0 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20 4e  int64 used,int N
947d0 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c  ),.  void *pArg,
947e0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
947f0 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20   iThreshold.){. 
94800 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d   return sqlite3M
94810 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 61 6c 6c  emoryAlarm(xCall
94820 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 54 68 72  back, pArg, iThr
94830 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 6e 64 69  eshold);.}.#endi
94840 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72  f../*.** Trigger
94850 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f 0a 73   the alarm .*/.s
94860 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
94870 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 69 6e  e3MallocAlarm(in
94880 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64  t nByte){.  void
94890 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f   (*xCallback)(vo
948a0 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36  id*,sqlite3_int6
948b0 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65  4,int);.  sqlite
948c0 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 64 3b  3_int64 nowUsed;
948d0 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20  .  void *pArg;. 
948e0 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43   if( mem0.alarmC
948f0 61 6c 6c 62 61 63 6b 3d 3d 30 20 7c 7c 20 6d 65  allback==0 || me
94900 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 20 29 20  m0.alarmBusy  ) 
94910 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 30 2e 61  return;.  mem0.a
94920 6c 61 72 6d 42 75 73 79 20 3d 20 31 3b 0a 20 20  larmBusy = 1;.  
94930 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d 65 6d 30  xCallback = mem0
94940 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a  .alarmCallback;.
94950 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 71 6c 69    nowUsed = sqli
94960 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53  te3StatusValue(S
94970 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
94980 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 70 41 72  ORY_USED);.  pAr
94990 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72  g = mem0.alarmAr
949a0 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  g;.  sqlite3_mut
949b0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
949c0 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c 62 61 63  tex);.  xCallbac
949d0 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 65 64 2c  k(pArg, nowUsed,
949e0 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 6c 69 74   nByte);.  sqlit
949f0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
94a00 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65  em0.mutex);.  me
94a10 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 30  m0.alarmBusy = 0
94a20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20  ;.}../*.** Do a 
94a30 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
94a40 6e 20 77 69 74 68 20 73 74 61 74 69 73 74 69 63  n with statistic
94a50 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e 20 20 41  s and alarms.  A
94a60 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20 6c 6f 63  ssume the.** loc
94a70 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c  k is already hel
94a80 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
94a90 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d   mallocWithAlarm
94aa0 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20 2a 2a 70  (int n, void **p
94ab0 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 3b  p){.  int nFull;
94ac0 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73  .  void *p;.  as
94ad0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
94ae0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d 75  tex_held(mem0.mu
94af0 74 65 78 29 20 29 3b 0a 20 20 6e 46 75 6c 6c 20  tex) );.  nFull 
94b00 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
94b10 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70  onfig.m.xRoundup
94b20 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74  (n);.  sqlite3St
94b30 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
94b40 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a  TATUS_MALLOC_SIZ
94b50 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20 6d 65 6d  E, n);.  if( mem
94b60 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 21  0.alarmCallback!
94b70 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 55  =0 ){.    int nU
94b80 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61  sed = sqlite3Sta
94b90 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f  tusValue(SQLITE_
94ba0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
94bb0 45 44 29 3b 0a 20 20 20 20 69 66 28 20 6e 55 73  ED);.    if( nUs
94bc0 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d 65 6d 30  ed+nFull >= mem0
94bd0 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20  .alarmThreshold 
94be0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
94bf0 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c  MallocAlarm(nFul
94c00 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  l);.    }.  }.  
94c10 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  p = sqlite3Globa
94c20 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f  lConfig.m.xMallo
94c30 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69 66 28 20  c(nFull);.  if( 
94c40 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61  p==0 && mem0.ala
94c50 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20  rmCallback ){.  
94c60 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41    sqlite3MallocA
94c70 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20  larm(nFull);.   
94c80 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62   p = sqlite3Glob
94c90 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c  alConfig.m.xMall
94ca0 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20  oc(nFull);.  }. 
94cb0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 46   if( p ){.    nF
94cc0 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  ull = sqlite3Mal
94cd0 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20  locSize(p);.    
94ce0 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
94cf0 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
94d00 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 46 75 6c  EMORY_USED, nFul
94d10 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20  l);.  }.  *pp = 
94d20 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e 46 75 6c  p;.  return nFul
94d30 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  l;.}../*.** Allo
94d40 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20 20 54 68  cate memory.  Th
94d50 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6c 69  is routine is li
94d60 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ke sqlite3_mallo
94d70 63 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20  c() except that 
94d80 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68  it.** assumes th
94d90 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74  e memory subsyst
94da0 65 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  em has already b
94db0 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e  een initialized.
94dc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
94dd0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
94de0 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20  Malloc(int n){. 
94df0 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20   void *p;.  if( 
94e00 6e 3c 3d 30 20 7c 7c 20 4e 45 56 45 52 28 6e 3e  n<=0 || NEVER(n>
94e10 3d 30 78 37 66 66 66 66 66 30 30 29 20 29 7b 0a  =0x7fffff00) ){.
94e20 20 20 20 20 2f 2a 20 54 68 65 20 4e 45 56 45 52      /* The NEVER
94e30 28 6e 3e 3d 30 78 37 66 66 66 66 66 30 30 29 20  (n>=0x7fffff00) 
94e40 74 65 72 6d 20 69 73 20 61 64 64 65 64 20 6f 75  term is added ou
94e50 74 20 6f 66 20 70 61 72 61 6e 6f 69 61 2e 20 20  t of paranoia.  
94e60 57 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 0a  We want to make.
94e70 20 20 20 20 2a 2a 20 61 62 73 6f 6c 75 74 65 6c      ** absolutel
94e80 79 20 73 75 72 65 20 74 68 61 74 20 74 68 65 72  y sure that ther
94e90 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 77 69 74  e is nothing wit
94ea0 68 69 6e 20 53 51 4c 69 74 65 20 74 68 61 74 20  hin SQLite that 
94eb0 63 61 6e 20 63 61 75 73 65 20 61 0a 20 20 20 20  can cause a.    
94ec0 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** memory alloca
94ed0 74 69 6f 6e 20 6f 66 20 61 20 6e 75 6d 62 65 72  tion of a number
94ee0 20 6f 66 20 62 79 74 65 73 20 77 68 69 63 68 20   of bytes which 
94ef0 69 73 20 6e 65 61 72 20 74 68 65 20 6d 61 78 69  is near the maxi
94f00 6d 75 6d 20 73 69 67 6e 65 64 0a 20 20 20 20 2a  mum signed.    *
94f10 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * integer value 
94f20 61 6e 64 20 74 68 75 73 20 63 61 75 73 65 20 61  and thus cause a
94f30 6e 20 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c  n integer overfl
94f40 6f 77 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65  ow inside of the
94f50 20 78 4d 61 6c 6c 6f 63 28 29 0a 20 20 20 20 2a   xMalloc().    *
94f60 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
94f70 2e 20 20 54 68 65 20 6e 3e 3d 30 78 37 66 66 66  .  The n>=0x7fff
94f80 66 66 30 30 20 67 69 76 65 73 20 75 73 20 32 35  ff00 gives us 25
94f90 35 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 72  5 bytes of headr
94fa0 6f 6f 6d 2e 20 20 54 68 65 0a 20 20 20 20 2a 2a  oom.  The.    **
94fb0 20 74 65 73 74 20 73 68 6f 75 6c 64 20 6e 65 76   test should nev
94fc0 65 72 20 62 65 20 74 72 75 65 20 62 65 63 61 75  er be true becau
94fd0 73 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  se SQLITE_MAX_LE
94fe0 4e 47 54 48 20 73 68 6f 75 6c 64 20 62 65 20 6d  NGTH should be m
94ff0 75 63 68 0a 20 20 20 20 2a 2a 20 6c 65 73 73 20  uch.    ** less 
95000 74 68 61 6e 20 30 78 37 66 66 66 66 66 30 30 20  than 0x7fffff00 
95010 61 6e 64 20 69 74 20 73 68 6f 75 6c 64 20 63 61  and it should ca
95020 74 63 68 20 6c 61 72 67 65 20 6d 65 6d 6f 72 79  tch large memory
95030 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 20   allocations.   
95040 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 79 20   ** before they 
95050 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74  reach this point
95060 2e 20 2a 2f 0a 20 20 20 20 70 20 3d 20 30 3b 0a  . */.    p = 0;.
95070 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
95080 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
95090 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20  bMemstat ){.    
950a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
950b0 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ter(mem0.mutex);
950c0 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 69 74 68 41  .    mallocWithA
950d0 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20  larm(n, &p);.   
950e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
950f0 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
95100 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
95110 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
95120 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63  Config.m.xMalloc
95130 28 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  (n);.  }.  retur
95140 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  n p;.}../*.** Th
95150 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
95160 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
95170 69 6f 6e 20 69 73 20 66 6f 72 20 75 73 65 20 62  ion is for use b
95180 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  y the applicatio
95190 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 6d 61 6b 65  n..** First make
951a0 20 73 75 72 65 20 74 68 65 20 6d 65 6d 6f 72 79   sure the memory
951b0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e   subsystem is in
951c0 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20  itialized, then 
951d0 64 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61  do the.** alloca
951e0 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
951f0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
95200 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b  3_malloc(int n){
95210 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
95220 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
95230 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  if( sqlite3_init
95240 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72  ialize() ) retur
95250 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  n 0;.#endif.  re
95260 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c  turn sqlite3Mall
95270 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  oc(n);.}../*.** 
95280 45 61 63 68 20 74 68 72 65 61 64 20 6d 61 79 20  Each thread may 
95290 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67  only have a sing
952a0 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61  le outstanding a
952b0 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a  llocation from.*
952c0 2a 20 78 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63  * xScratchMalloc
952d0 28 29 2e 20 20 57 65 20 76 65 72 69 66 79 20 74  ().  We verify t
952e0 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  his constraint i
952f0 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72  n the single-thr
95300 65 61 64 65 64 0a 2a 2a 20 63 61 73 65 20 62 79  eaded.** case by
95310 20 73 65 74 74 69 6e 67 20 73 63 72 61 74 63 68   setting scratch
95320 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31 20 77 68  AllocOut to 1 wh
95330 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  en an allocation
95340 0a 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 69  .** is outstandi
95350 6e 67 20 63 6c 65 61 72 69 6e 67 20 69 74 20 77  ng clearing it w
95360 68 65 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  hen the allocati
95370 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a 2f 0a  on is freed..*/.
95380 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
95390 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66  DSAFE==0 && !def
953a0 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 73 74 61  ined(NDEBUG).sta
953b0 74 69 63 20 69 6e 74 20 73 63 72 61 74 63 68 41  tic int scratchA
953c0 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23 65 6e  llocOut = 0;.#en
953d0 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  dif.../*.** Allo
953e0 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 68 61 74  cate memory that
953f0 20 69 73 20 74 6f 20 62 65 20 75 73 65 64 20 61   is to be used a
95400 6e 64 20 72 65 6c 65 61 73 65 64 20 72 69 67 68  nd released righ
95410 74 20 61 77 61 79 2e 0a 2a 2a 20 54 68 69 73 20  t away..** This 
95420 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c  routine is simil
95430 61 72 20 74 6f 20 61 6c 6c 6f 63 61 28 29 20 69  ar to alloca() i
95440 6e 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74  n that it is not
95450 20 69 6e 74 65 6e 64 65 64 0a 2a 2a 20 66 6f 72   intended.** for
95460 20 73 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72   situations wher
95470 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d 69 67  e the memory mig
95480 68 74 20 62 65 20 68 65 6c 64 20 6c 6f 6e 67 2d  ht be held long-
95490 74 65 72 6d 2e 20 20 54 68 69 73 0a 2a 2a 20 72  term.  This.** r
954a0 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 65 6e 64  outine is intend
954b0 65 64 20 74 6f 20 67 65 74 20 6d 65 6d 6f 72 79  ed to get memory
954c0 20 74 6f 20 6f 6c 64 20 6c 61 72 67 65 20 74 72   to old large tr
954d0 61 6e 73 69 65 6e 74 20 64 61 74 61 0a 2a 2a 20  ansient data.** 
954e0 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
954f0 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72 6d 61 6c  would not normal
95500 6c 79 20 66 69 74 20 6f 6e 20 74 68 65 20 73 74  ly fit on the st
95510 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a 20 65 6d 62  ack of an.** emb
95520 65 64 64 65 64 20 70 72 6f 63 65 73 73 6f 72 2e  edded processor.
95530 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
95540 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
95550 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e  ScratchMalloc(in
95560 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b  t n){.  void *p;
95570 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29  .  assert( n>0 )
95580 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  ;..#if SQLITE_TH
95590 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21  READSAFE==0 && !
955a0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a  defined(NDEBUG).
955b0 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74    /* Verify that
955c0 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e   no more than on
955d0 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61  e scratch alloca
955e0 74 69 6f 6e 20 70 65 72 20 74 68 72 65 61 64 0a  tion per thread.
955f0 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e 64    ** is outstand
95600 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e  ing at one time.
95610 20 20 28 54 68 69 73 20 69 73 20 6f 6e 6c 79 20    (This is only 
95620 63 68 65 63 6b 65 64 20 69 6e 20 74 68 65 0a 20  checked in the. 
95630 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 72 65 61   ** single-threa
95640 64 65 64 20 63 61 73 65 20 73 69 6e 63 65 20 63  ded case since c
95650 68 65 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d  hecking in the m
95660 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 63 61  ulti-threaded ca
95670 73 65 0a 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65  se.  ** would be
95680 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c   much more compl
95690 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20 61 73  icated.) */.  as
956a0 73 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c  sert( scratchAll
956b0 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a 23 65 6e 64  ocOut==0 );.#end
956c0 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  if..  if( sqlite
956d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
956e0 53 63 72 61 74 63 68 3c 6e 20 29 7b 0a 20 20 20  Scratch<n ){.   
956f0 20 67 6f 74 6f 20 73 63 72 61 74 63 68 5f 6f 76   goto scratch_ov
95700 65 72 66 6c 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b  erflow;.  }else{
95710 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d    .    sqlite3_m
95720 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e  utex_enter(mem0.
95730 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20  mutex);.    if( 
95740 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65  mem0.nScratchFre
95750 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  e==0 ){.      sq
95760 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
95770 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
95780 20 20 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63       goto scratc
95790 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20  h_overflow;.    
957a0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
957b0 20 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 6d 65   i;.      i = me
957c0 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b  m0.aScratchFree[
957d0 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46  --mem0.nScratchF
957e0 72 65 65 5d 3b 0a 20 20 20 20 20 20 69 20 2a 3d  ree];.      i *=
957f0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
95800 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3b 0a  nfig.szScratch;.
95810 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
95820 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
95830 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45  ATUS_SCRATCH_USE
95840 44 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  D, 1);.      sql
95850 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51  ite3StatusSet(SQ
95860 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
95870 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20  TCH_SIZE, n);.  
95880 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
95890 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
958a0 65 78 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 28  ex);.      p = (
958b0 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 73  void*)&((char*)s
958c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
958d0 69 67 2e 70 53 63 72 61 74 63 68 29 5b 69 5d 3b  ig.pScratch)[i];
958e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 20  .      assert(  
958f0 28 28 28 75 38 2a 29 70 20 2d 20 28 75 38 2a 29  (((u8*)p - (u8*)
95900 30 29 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20  0) & 7)==0 );.  
95910 20 20 7d 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49    }.  }.#if SQLI
95920 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30  TE_THREADSAFE==0
95930 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45   && !defined(NDE
95940 42 55 47 29 0a 20 20 73 63 72 61 74 63 68 41 6c  BUG).  scratchAl
95950 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23  locOut = p!=0;.#
95960 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20  endif..  return 
95970 70 3b 0a 0a 73 63 72 61 74 63 68 5f 6f 76 65 72  p;..scratch_over
95980 66 6c 6f 77 3a 0a 20 20 69 66 28 20 73 71 6c 69  flow:.  if( sqli
95990 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
959a0 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20  bMemstat ){.    
959b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
959c0 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ter(mem0.mutex);
959d0 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74  .    sqlite3Stat
959e0 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41  usSet(SQLITE_STA
959f0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45  TUS_SCRATCH_SIZE
95a00 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 3d 20 6d 61  , n);.    n = ma
95a10 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c  llocWithAlarm(n,
95a20 20 26 70 29 3b 0a 20 20 20 20 69 66 28 20 70 20   &p);.    if( p 
95a30 29 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41  ) sqlite3StatusA
95a40 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
95a50 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f  _SCRATCH_OVERFLO
95a60 57 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  W, n);.    sqlit
95a70 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
95a80 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65  em0.mutex);.  }e
95a90 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c  lse{.    p = sql
95aa0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
95ab0 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20  .m.xMalloc(n);. 
95ac0 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48   }.#if SQLITE_TH
95ad0 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21  READSAFE==0 && !
95ae0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a  defined(NDEBUG).
95af0 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75    scratchAllocOu
95b00 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66  t = p!=0;.#endif
95b10 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 20 20 20  .  return p;    
95b20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
95b30 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63  E void sqlite3Sc
95b40 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 20 2a  ratchFree(void *
95b50 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 0a  p){.  if( p ){..
95b60 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
95b70 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66  DSAFE==0 && !def
95b80 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 20  ined(NDEBUG).   
95b90 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20   /* Verify that 
95ba0 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  no more than one
95bb0 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 74   scratch allocat
95bc0 69 6f 6e 20 70 65 72 20 74 68 72 65 61 64 0a 20  ion per thread. 
95bd0 20 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 61 6e     ** is outstan
95be0 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d 65  ding at one time
95bf0 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e 6c 79  .  (This is only
95c00 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68 65 0a   checked in the.
95c10 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68      ** single-th
95c20 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e 63  readed case sinc
95c30 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74 68  e checking in th
95c40 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64  e multi-threaded
95c50 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 77 6f 75   case.    ** wou
95c60 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 20  ld be much more 
95c70 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f  complicated.) */
95c80 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 63 72  .    assert( scr
95c90 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 31 20  atchAllocOut==1 
95ca0 29 3b 0a 20 20 20 20 73 63 72 61 74 63 68 41 6c  );.    scratchAl
95cb0 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23 65 6e 64  locOut = 0;.#end
95cc0 69 66 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69  if..    if( sqli
95cd0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
95ce0 70 53 63 72 61 74 63 68 3d 3d 30 0a 20 20 20 20  pScratch==0.    
95cf0 20 20 20 20 20 20 20 7c 7c 20 70 3c 73 71 6c 69         || p<sqli
95d00 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
95d10 70 53 63 72 61 74 63 68 0a 20 20 20 20 20 20 20  pScratch.       
95d20 20 20 20 20 7c 7c 20 70 3e 3d 28 76 6f 69 64 2a      || p>=(void*
95d30 29 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72  )mem0.aScratchFr
95d40 65 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ee ){.      if( 
95d50 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
95d60 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a  fig.bMemstat ){.
95d70 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 69 7a          int iSiz
95d80 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  e = sqlite3Mallo
95d90 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20  cSize(p);.      
95da0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
95db0 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78  enter(mem0.mutex
95dc0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
95dd0 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49  e3StatusAdd(SQLI
95de0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
95df0 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69  H_OVERFLOW, -iSi
95e00 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ze);.        sql
95e10 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
95e20 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
95e30 52 59 5f 55 53 45 44 2c 20 2d 69 53 69 7a 65 29  RY_USED, -iSize)
95e40 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
95e50 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
95e60 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  xFree(p);.      
95e70 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
95e80 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
95e90 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
95ea0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
95eb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46  lobalConfig.m.xF
95ec0 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a  ree(p);.      }.
95ed0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
95ee0 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20   int i;.      i 
95ef0 3d 20 28 69 6e 74 29 28 28 75 38 2a 29 70 20 2d  = (int)((u8*)p -
95f00 20 28 75 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f   (u8*)sqlite3Glo
95f10 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
95f20 63 68 29 3b 0a 20 20 20 20 20 20 69 20 2f 3d 20  ch);.      i /= 
95f30 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
95f40 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3b 0a 20  fig.szScratch;. 
95f50 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d       assert( i>=
95f60 30 20 26 26 20 69 3c 73 71 6c 69 74 65 33 47 6c  0 && i<sqlite3Gl
95f70 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
95f80 74 63 68 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  tch );.      sql
95f90 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
95fa0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
95fb0 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 30      assert( mem0
95fc0 2e 6e 53 63 72 61 74 63 68 46 72 65 65 3c 28 75  .nScratchFree<(u
95fd0 33 32 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  32)sqlite3Global
95fe0 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20  Config.nScratch 
95ff0 29 3b 0a 20 20 20 20 20 20 6d 65 6d 30 2e 61 53  );.      mem0.aS
96000 63 72 61 74 63 68 46 72 65 65 5b 6d 65 6d 30 2e  cratchFree[mem0.
96010 6e 53 63 72 61 74 63 68 46 72 65 65 2b 2b 5d 20  nScratchFree++] 
96020 3d 20 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = i;.      sqlit
96030 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49  e3StatusAdd(SQLI
96040 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
96050 48 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20  H_USED, -1);.   
96060 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
96070 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
96080 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  x);.    }.  }.}.
96090 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 69 66 20 70  ./*.** TRUE if p
960a0 20 69 73 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20   is a lookaside 
960b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
960c0 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f 0a 23 69 66  n from db.*/.#if
960d0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
960e0 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 74 61 74 69  _LOOKASIDE.stati
960f0 63 20 69 6e 74 20 69 73 4c 6f 6f 6b 61 73 69 64  c int isLookasid
96100 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76  e(sqlite3 *db, v
96110 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  oid *p){.  retur
96120 6e 20 64 62 20 26 26 20 70 20 26 26 20 70 3e 3d  n db && p && p>=
96130 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53  db->lookaside.pS
96140 74 61 72 74 20 26 26 20 70 3c 64 62 2d 3e 6c 6f  tart && p<db->lo
96150 6f 6b 61 73 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a  okaside.pEnd;.}.
96160 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 69 73  #else.#define is
96170 4c 6f 6f 6b 61 73 69 64 65 28 41 2c 42 29 20 30  Lookaside(A,B) 0
96180 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
96190 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f  eturn the size o
961a0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
961b0 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79  ation previously
961c0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
961d0 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  * sqlite3Malloc(
961e0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ) or sqlite3_mal
961f0 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  loc()..*/.SQLITE
96200 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
96210 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76  ite3MallocSize(v
96220 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  oid *p){.  retur
96230 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  n sqlite3GlobalC
96240 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29  onfig.m.xSize(p)
96250 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
96260 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 62  TE int sqlite3Db
96270 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c 69 74  MallocSize(sqlit
96280 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29  e3 *db, void *p)
96290 7b 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d  {.  assert( db==
962a0 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
962b0 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
962c0 78 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30  x) );.  if( p==0
962d0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
962e0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73  ;.  }else if( is
962f0 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29  Lookaside(db, p)
96300 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64   ){.    return d
96310 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 3b  b->lookaside.sz;
96320 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
96330 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62  turn sqlite3Glob
96340 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65  alConfig.m.xSize
96350 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
96360 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 70 72  * Free memory pr
96370 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65  eviously obtaine
96380 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61  d from sqlite3Ma
96390 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  lloc()..*/.SQLIT
963a0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
963b0 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70 29  e3_free(void *p)
963c0 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
963d0 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c  eturn;.  if( sql
963e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
963f0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
96400 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
96410 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
96420 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
96430 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
96440 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
96450 2c 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  , -sqlite3Malloc
96460 53 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 73 71  Size(p));.    sq
96470 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
96480 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20  g.m.xFree(p);.  
96490 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
964a0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
964b0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
964c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
964d0 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
964e0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65    }.}../*.** Fre
964f0 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 69  e memory that mi
96500 67 68 74 20 62 65 20 61 73 73 6f 63 69 61 74 65  ght be associate
96510 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  d with a particu
96520 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  lar database.** 
96530 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  connection..*/.S
96540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
96550 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  id sqlite3DbFree
96560 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f  (sqlite3 *db, vo
96570 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  id *p){.  assert
96580 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ( db==0 || sqlit
96590 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
965a0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  ->mutex) );.  if
965b0 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62  ( isLookaside(db
965c0 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b  , p) ){.    Look
965d0 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 20  asideSlot *pBuf 
965e0 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74  = (LookasideSlot
965f0 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d 3e 70  *)p;.    pBuf->p
96600 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  Next = db->looka
96610 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 20  side.pFree;.    
96620 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46  db->lookaside.pF
96630 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 20 20  ree = pBuf;.    
96640 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f  db->lookaside.nO
96650 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ut--;.  }else{. 
96660 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
96670 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
96680 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65   Change the size
96690 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   of an existing 
966a0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
966b0 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n.*/.SQLITE_PRIV
966c0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
966d0 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70  3Realloc(void *p
966e0 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73 29  Old, int nBytes)
966f0 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e 4e  {.  int nOld, nN
96700 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77  ew;.  void *pNew
96710 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30 20  ;.  if( pOld==0 
96720 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
96730 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74  lite3Malloc(nByt
96740 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e  es);.  }.  if( n
96750 42 79 74 65 73 3c 3d 30 20 7c 7c 20 4e 45 56 45  Bytes<=0 || NEVE
96760 52 28 6e 42 79 74 65 73 3e 3d 30 78 37 66 66 66  R(nBytes>=0x7fff
96770 66 66 30 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20  ff00) ){.    /* 
96780 54 68 65 20 4e 45 56 45 52 28 2e 2e 2e 29 20 74  The NEVER(...) t
96790 65 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64  erm is explained
967a0 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20   in comments on 
967b0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20  sqlite3Malloc() 
967c0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  */.    sqlite3_f
967d0 72 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20 20 72  ree(pOld);.    r
967e0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
967f0 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Old = sqlite3Mal
96800 6c 6f 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a 20  locSize(pOld);. 
96810 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
96820 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61  alConfig.bMemsta
96830 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
96840 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
96850 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71  0.mutex);.    sq
96860 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53  lite3StatusSet(S
96870 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c  QLITE_STATUS_MAL
96880 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 79 74 65 73  LOC_SIZE, nBytes
96890 29 3b 0a 20 20 20 20 6e 4e 65 77 20 3d 20 73 71  );.    nNew = sq
968a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
968b0 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 42 79  g.m.xRoundup(nBy
968c0 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 6e 4f  tes);.    if( nO
968d0 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20  ld==nNew ){.    
968e0 20 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20    pNew = pOld;. 
968f0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
96900 69 66 28 20 73 71 6c 69 74 65 33 53 74 61 74 75  if( sqlite3Statu
96910 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54  sValue(SQLITE_ST
96920 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
96930 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 0a  )+nNew-nOld >= .
96940 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 30              mem0
96950 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20  .alarmThreshold 
96960 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
96970 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e  e3MallocAlarm(nN
96980 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 20 20  ew-nOld);.      
96990 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73  }.      pNew = s
969a0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
969b0 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f  ig.m.xRealloc(pO
969c0 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20  ld, nNew);.     
969d0 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20   if( pNew==0 && 
969e0 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
969f0 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ck ){.        sq
96a00 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d  lite3MallocAlarm
96a10 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20  (nBytes);.      
96a20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
96a30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
96a40 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e  Realloc(pOld, nN
96a50 65 77 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ew);.      }.   
96a60 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20     if( pNew ){. 
96a70 20 20 20 20 20 20 20 6e 4e 65 77 20 3d 20 73 71         nNew = sq
96a80 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
96a90 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20 73  pNew);.        s
96aa0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
96ab0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
96ac0 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 4e 65 77 2d  MORY_USED, nNew-
96ad0 6e 4f 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20  nOld);.      }. 
96ae0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
96af0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
96b00 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73  0.mutex);.  }els
96b10 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  e{.    pNew = sq
96b20 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
96b30 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c  g.m.xRealloc(pOl
96b40 64 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a  d, nBytes);.  }.
96b50 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
96b60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 62 6c  ../*.** The publ
96b70 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  ic interface to 
96b80 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e 20  sqlite3Realloc. 
96b90 20 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   Make sure that 
96ba0 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 75  the memory.** su
96bb0 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69  bsystem is initi
96bc0 61 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f 20  alized prior to 
96bd0 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 52  invoking sqliteR
96be0 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 54  ealloc..*/.SQLIT
96bf0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
96c00 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64  te3_realloc(void
96c10 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0a   *pOld, int n){.
96c20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
96c30 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69  MIT_AUTOINIT.  i
96c40 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  f( sqlite3_initi
96c50 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e  alize() ) return
96c60 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74   0;.#endif.  ret
96c70 75 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c 6c  urn sqlite3Reall
96c80 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a  oc(pOld, n);.}..
96c90 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
96ca0 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e  and zero memory.
96cb0 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/ .SQLITE_PRIV
96cc0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
96cd0 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20  3MallocZero(int 
96ce0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20  n){.  void *p = 
96cf0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29  sqlite3Malloc(n)
96d00 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
96d10 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29   memset(p, 0, n)
96d20 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
96d30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
96d40 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d  ate and zero mem
96d50 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c  ory.  If the all
96d60 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d  ocation fails, m
96d70 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f  ake.** the mallo
96d80 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20  cFailed flag in 
96d90 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  the connection p
96da0 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
96db0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
96dc0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
96dd0 65 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ero(sqlite3 *db,
96de0 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20   int n){.  void 
96df0 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  *p = sqlite3DbMa
96e00 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a  llocRaw(db, n);.
96e10 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d    if( p ){.    m
96e20 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a  emset(p, 0, n);.
96e30 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
96e40 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
96e50 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72  e and zero memor
96e60 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f 63  y.  If the alloc
96e70 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b  ation fails, mak
96e80 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46  e.** the mallocF
96e90 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68  ailed flag in th
96ea0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69  e connection poi
96eb0 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64  nter..**.** If d
96ec0 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c  b!=0 and db->mal
96ed0 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72 75  locFailed is tru
96ee0 65 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61 20  e (indicating a 
96ef0 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a 20  prior malloc.** 
96f00 66 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20 73  failure on the s
96f10 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
96f20 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61 6c  nection) then al
96f30 77 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a  ways return 0..*
96f40 2a 20 48 65 6e 63 65 20 66 6f 72 20 61 20 70 61  * Hence for a pa
96f50 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
96f60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 6e  e connection, on
96f70 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 74 73  ce malloc starts
96f80 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 74 20  .** failing, it 
96f90 66 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 6e 74  fails consistent
96fa0 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 46  ly until mallocF
96fb0 61 69 6c 65 64 20 69 73 20 72 65 73 65 74 2e 0a  ailed is reset..
96fc0 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6d  ** This is an im
96fd0 70 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 74 69  portant assumpti
96fe0 6f 6e 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d  on.  There are m
96ff0 61 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 74 68  any places in th
97000 65 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 20 64  e.** code that d
97010 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 68  o things like th
97020 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  is:.**.**       
97030 20 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a    int *a = (int*
97040 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  )sqlite3DbMalloc
97050 52 61 77 28 64 62 2c 20 31 30 30 29 3b 0a 2a 2a  Raw(db, 100);.**
97060 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a 62 20           int *b 
97070 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44  = (int*)sqlite3D
97080 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32  bMallocRaw(db, 2
97090 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20  00);.**         
970a0 69 66 28 20 62 20 29 20 61 5b 31 30 5d 20 3d 20  if( b ) a[10] = 
970b0 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65  9;.**.** In othe
970c0 72 20 77 6f 72 64 73 2c 20 69 66 20 61 20 73 75  r words, if a su
970d0 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20  bsequent malloc 
970e0 28 65 78 3a 20 22 62 22 29 20 77 6f 72 6b 65 64  (ex: "b") worked
970f0 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 0a  , it is assumed.
97100 2a 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 69 6f  ** that all prio
97110 72 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a 20 22  r mallocs (ex: "
97120 61 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f 2e 0a  a") worked too..
97130 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
97140 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44  E void *sqlite3D
97150 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74  bMallocRaw(sqlit
97160 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a  e3 *db, int n){.
97170 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73    void *p;.  ass
97180 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71  ert( db==0 || sq
97190 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
971a0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23  (db->mutex) );.#
971b0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
971c0 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 69  IT_LOOKASIDE.  i
971d0 66 28 20 64 62 20 29 7b 0a 20 20 20 20 4c 6f 6f  f( db ){.    Loo
971e0 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66  kasideSlot *pBuf
971f0 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
97200 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
97210 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
97220 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
97230 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
97240 65 64 20 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f  ed && n<=db->loo
97250 6b 61 73 69 64 65 2e 73 7a 0a 20 20 20 20 20 20  kaside.sz.      
97260 20 20 20 26 26 20 28 70 42 75 66 20 3d 20 64 62     && (pBuf = db
97270 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65  ->lookaside.pFre
97280 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 64  e)!=0 ){.      d
97290 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72  b->lookaside.pFr
972a0 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74  ee = pBuf->pNext
972b0 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  ;.      db->look
972c0 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 20  aside.nOut++;.  
972d0 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b      if( db->look
972e0 61 73 69 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e 6c  aside.nOut>db->l
972f0 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 29  ookaside.mxOut )
97300 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f  {.        db->lo
97310 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20  okaside.mxOut = 
97320 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f  db->lookaside.nO
97330 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
97340 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
97350 70 42 75 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  pBuf;.    }.  }.
97360 23 65 6c 73 65 0a 20 20 69 66 28 20 64 62 20 26  #else.  if( db &
97370 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  & db->mallocFail
97380 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
97390 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20   0;.  }.#endif. 
973a0 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c   p = sqlite3Mall
973b0 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 21 70 20  oc(n);.  if( !p 
973c0 26 26 20 64 62 20 29 7b 0a 20 20 20 20 64 62 2d  && db ){.    db-
973d0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
973e0 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
973f0 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69  p;.}../*.** Resi
97400 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20  ze the block of 
97410 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74  memory pointed t
97420 6f 20 62 79 20 70 20 74 6f 20 6e 20 62 79 74 65  o by p to n byte
97430 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73  s. If the.** res
97440 69 7a 65 20 66 61 69 6c 73 2c 20 73 65 74 20 74  ize fails, set t
97450 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
97460 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e  flag in the conn
97470 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a  ection object..*
97480 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
97490 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
974a0 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20  Realloc(sqlite3 
974b0 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e  *db, void *p, in
974c0 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e  t n){.  void *pN
974d0 65 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ew = 0;.  assert
974e0 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73  ( db!=0 );.  ass
974f0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
97500 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
97510 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  x) );.  if( db->
97520 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20  mallocFailed==0 
97530 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20  ){.    if( p==0 
97540 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
97550 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
97560 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d  aw(db, n);.    }
97570 0a 20 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61  .    if( isLooka
97580 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20  side(db, p) ){. 
97590 20 20 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e       if( n<=db->
975a0 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a  lookaside.sz ){.
975b0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70          return p
975c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
975d0 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
975e0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29  MallocRaw(db, n)
975f0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77  ;.      if( pNew
97600 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63   ){.        memc
97610 70 79 28 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e  py(pNew, p, db->
97620 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20  lookaside.sz);. 
97630 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
97640 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20  Free(db, p);.   
97650 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
97660 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
97670 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20  ite3_realloc(p, 
97680 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  n);.      if( !p
97690 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 64  New ){.        d
976a0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
976b0 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
976c0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
976d0 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  pNew;.}../*.** A
976e0 74 74 65 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f  ttempt to reallo
976f0 63 61 74 65 20 70 2e 20 20 49 66 20 74 68 65 20  cate p.  If the 
97700 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  reallocation fai
97710 6c 73 2c 20 74 68 65 6e 20 66 72 65 65 20 70 0a  ls, then free p.
97720 2a 2a 20 61 6e 64 20 73 65 74 20 74 68 65 20 6d  ** and set the m
97730 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
97740 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
97750 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a   connection..*/.
97760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
97770 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65  oid *sqlite3DbRe
97780 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c 69  allocOrFree(sqli
97790 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70  te3 *db, void *p
977a0 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  , int n){.  void
977b0 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d   *pNew;.  pNew =
977c0 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
977d0 63 28 64 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 69  c(db, p, n);.  i
977e0 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20  f( !pNew ){.    
977f0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
97800 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , p);.  }.  retu
97810 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
97820 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  * Make a copy of
97830 20 61 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d   a string in mem
97840 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
97850 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  m sqliteMalloc()
97860 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63  . These .** func
97870 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74  tions call sqlit
97880 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20 64 69  e3MallocRaw() di
97890 72 65 63 74 6c 79 20 69 6e 73 74 65 61 64 20 6f  rectly instead o
978a0 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  f sqliteMalloc()
978b0 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 62 65 63  . This.** is bec
978c0 61 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79  ause when memory
978d0 20 64 65 62 75 67 67 69 6e 67 20 69 73 20 74 75   debugging is tu
978e0 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 20 74  rned on, these t
978f0 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  wo functions are
97900 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 61 20   .** called via 
97910 6d 61 63 72 6f 73 20 74 68 61 74 20 72 65 63 6f  macros that reco
97920 72 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 66  rd the current f
97930 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d  ile and line num
97940 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68  ber in the.** Th
97950 72 65 61 64 44 61 74 61 20 73 74 72 75 63 74 75  readData structu
97960 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  re..*/.SQLITE_PR
97970 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
97980 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69  te3DbStrDup(sqli
97990 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
979a0 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20  har *z){.  char 
979b0 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 5f 74 20  *zNew;.  size_t 
979c0 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  n;.  if( z==0 ){
979d0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
979e0 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33   }.  n = sqlite3
979f0 53 74 72 6c 65 6e 33 30 28 7a 29 20 2b 20 31 3b  Strlen30(z) + 1;
97a00 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78  .  assert( (n&0x
97a10 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a  7fffffff)==n );.
97a20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    zNew = sqlite3
97a30 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
97a40 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28 20 7a  (int)n);.  if( z
97a50 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  New ){.    memcp
97a60 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20  y(zNew, z, n);. 
97a70 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77   }.  return zNew
97a80 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
97a90 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
97aa0 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65  DbStrNDup(sqlite
97ab0 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
97ac0 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *z, int n){.  
97ad0 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 66  char *zNew;.  if
97ae0 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ( z==0 ){.    re
97af0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73  turn 0;.  }.  as
97b00 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66  sert( (n&0x7ffff
97b10 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65  fff)==n );.  zNe
97b20 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
97b30 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31 29 3b  locRaw(db, n+1);
97b40 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20  .  if( zNew ){. 
97b50 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20     memcpy(zNew, 
97b60 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 5b  z, n);.    zNew[
97b70 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  n] = 0;.  }.  re
97b80 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn zNew;.}../*
97b90 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73 74 72  .** Create a str
97ba0 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a 46 72  ing from the zFr
97bb0 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 61 6e  omat argument an
97bc0 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20 74 68  d the va_list th
97bd0 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 20 53  at follows..** S
97be0 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67 20  tore the string 
97bf0 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  in memory obtain
97c00 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61  ed from sqliteMa
97c10 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20  lloc() and make 
97c20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20  *pz.** point to 
97c30 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a  that string..*/.
97c40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
97c50 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74  oid sqlite3SetSt
97c60 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a 2c 20  ring(char **pz, 
97c70 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
97c80 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
97c90 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
97ca0 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b  t ap;.  char *z;
97cb0 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ..  va_start(ap,
97cc0 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d   zFormat);.  z =
97cd0 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66   sqlite3VMPrintf
97ce0 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  (db, zFormat, ap
97cf0 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
97d00 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
97d10 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a 70 7a  (db, *pz);.  *pz
97d20 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   = z;.}.../*.** 
97d30 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75  This function mu
97d40 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66  st be called bef
97d50 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e 79 20  ore exiting any 
97d60 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69 2e  API function (i.
97d70 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67  e. .** returning
97d80 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20   control to the 
97d90 75 73 65 72 29 20 74 68 61 74 20 68 61 73 20 63  user) that has c
97da0 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f 6d 61  alled sqlite3_ma
97db0 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c 69 74  lloc or.** sqlit
97dc0 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a  e3_realloc..**.*
97dd0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76  * The returned v
97de0 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79  alue is normally
97df0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73   a copy of the s
97e00 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
97e10 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69  o this.** functi
97e20 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20  on. However, if 
97e30 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  a malloc() failu
97e40 72 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 20  re has occurred 
97e50 73 69 6e 63 65 20 74 68 65 20 70 72 65 76 69 6f  since the previo
97e60 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e  us.** invocation
97e70 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73   SQLITE_NOMEM is
97e80 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61   returned instea
97e90 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  d. .**.** If the
97ea0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c   first argument,
97eb0 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c   db, is not NULL
97ec0 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 29 20   and a malloc() 
97ed0 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
97ee0 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ed,.** then the 
97ef0 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 6f 72  connection error
97f00 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c 75 65  -code (the value
97f10 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
97f20 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 29 0a  ite3_errcode()).
97f30 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c  ** is set to SQL
97f40 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51  ITE_NOMEM..*/.SQ
97f50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
97f60 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
97f70 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 6e 74  sqlite3* db, int
97f80 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68   rc){.  /* If th
97f90 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73 20 6e  e db handle is n
97fa0 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65  ot NULL, then we
97fb0 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63   must hold the c
97fc0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
97fd0 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72 65  .  ** mutex here
97fe0 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20  . Otherwise the 
97ff0 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69 62  read (and possib
98000 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64 62 2d  le write) of db-
98010 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0a 20  >mallocFailed . 
98020 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20 61   ** is unsafe, a
98030 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74 6f  s is the call to
98040 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e   sqlite3Error().
98050 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
98060 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  !db || sqlite3_m
98070 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
98080 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62  tex) );.  if( db
98090 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46   && (db->mallocF
980a0 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 51 4c  ailed || rc==SQL
980b0 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29  ITE_IOERR_NOMEM)
980c0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
980d0 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
980e0 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64  NOMEM, 0);.    d
980f0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
98100 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  = 0;.    rc = SQ
98110 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
98120 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 28 64    return rc & (d
98130 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20  b ? db->errMask 
98140 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  : 0xff);.}../***
98150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
98160 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a  of malloc.c ****
98170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
981a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
981b0 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63 20  n file printf.c 
981c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
981d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
981e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
981f0 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22 20 63  * The "printf" c
98200 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ode that follows
98210 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20   dates from the 
98220 31 39 38 30 27 73 2e 20 20 49 74 20 69 73 20 69  1980's.  It is i
98230 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 20  n.** the public 
98240 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 69  domain.  The ori
98250 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61  ginal comments a
98260 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65  re included here
98270 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65   for.** complete
98280 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 20  ness.  They are 
98290 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65  very out-of-date
982a0 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 73   but might be us
982b0 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69  eful as.** an hi
982c0 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65 6e  storical referen
982d0 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65  ce.  Most of the
982e0 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20   "enhancements" 
982f0 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65 64  have been backed
98300 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74 20  .** out so that 
98310 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  the functionalit
98320 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61 6d  y is now the sam
98330 65 20 61 73 20 73 74 61 6e 64 61 72 64 20 70 72  e as standard pr
98340 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 24 49  intf()..**.** $I
98350 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20 31 2e  d: printf.c,v 1.
98360 31 30 33 20 32 30 30 39 2f 30 35 2f 30 34 20 32  103 2009/05/04 2
98370 30 3a 32 30 3a 31 36 20 64 72 68 20 45 78 70 20  0:20:16 drh Exp 
98380 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  $.**.***********
98390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
983a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
983b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
983c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
983d0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
983e0 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 61  ing modules is a
983f0 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c 61  n enhanced repla
98400 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 22  cement for the "
98410 70 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 69  printf" subrouti
98420 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20  nes.** found in 
98430 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c  the standard C l
98440 69 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f 6c  ibrary.  The fol
98450 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d 65  lowing enhanceme
98460 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 6f  nts are.** suppo
98470 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  rted:.**.**     
98480 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66   +  Additional f
98490 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 73  unctions.  The s
984a0 74 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 22  tandard set of "
984b0 70 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f 6e  printf" function
984c0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 63  s.**         inc
984d0 6c 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 70  ludes printf, fp
984e0 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c 20  rintf, sprintf, 
984f0 76 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e 74  vprintf, vfprint
98500 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  f, and.**       
98510 20 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 69    vsprintf.  Thi
98520 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 68  s module adds th
98530 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
98540 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
98550 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 6b  snprintf -- Work
98560 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c 20  s like sprintf, 
98570 62 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 61  but has an extra
98580 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20   argument.**    
98590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
985a0 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 74        which is t
985b0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62  he size of the b
985c0 75 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 6f  uffer written to
985d0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..**.**         
985e0 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d 20    *  mprintf -- 
985f0 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 69   Similar to spri
98600 6e 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 74  ntf.  Writes out
98610 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a  put to memory.**
98620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98630 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e            obtain
98640 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a  ed from malloc..
98650 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
98660 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 43  *  xprintf --  C
98670 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e 20  alls a function 
98680 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f 75  to dispose of ou
98690 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  tput..**.**     
986a0 20 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 66        *  nprintf
986b0 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c 20   --  No output, 
986c0 62 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 20  but returns the 
986d0 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
986e0 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ters.**         
986f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98700 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65   that would have
98710 20 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 20   been output by 
98720 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 20  printf..**.**   
98730 20 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d 20          *  A v- 
98740 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 6e  version (ex: vsn
98750 70 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 79  printf) of every
98760 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73   function is als
98770 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  o.**            
98780 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a 2a    supplied..**.*
98790 2a 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 20  *      +  A few 
987a0 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 68  extensions to th
987b0 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f 74  e formatting not
987c0 61 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f 72  ation are suppor
987d0 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ted:.**.**      
987e0 20 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 20       *  The "=" 
987f0 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 6f  flag (similar to
98800 20 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 65   "-") causes the
98810 20 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a 2a   output to be.**
98820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
98830 20 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 65   centered in the
98840 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 73   appropriately s
98850 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a  ized field..**.*
98860 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54  *           *  T
98870 68 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 70  he %b field outp
98880 75 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69  uts an integer i
98890 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 6f  n binary notatio
988a0 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  n..**.**        
988b0 20 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 65     *  The %c fie
988c0 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 61  ld now accepts a
988d0 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 65   precision.  The
988e0 20 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 75   character outpu
988f0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
98900 20 20 69 73 20 72 65 70 65 61 74 65 64 20 62 79    is repeated by
98910 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
98920 69 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 69  imes the precisi
98930 6f 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a 2a  on specifies..**
98940 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20  .**           * 
98950 20 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 6f   The %' field wo
98960 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 74  rks like %c, but
98970 20 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 68   takes as its ch
98980 61 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 20  aracter the.**  
98990 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74              next
989a0 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68   character of th
989b0 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c  e format string,
989c0 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20   instead of the 
989d0 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  next.**         
989e0 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 20       argument.  
989f0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 72  For example,  pr
98a00 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 20  intf("%.78'-")  
98a10 70 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 0a  prints 78 minus.
98a20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
98a30 73 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 20  signs, the same 
98a40 61 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38  as  printf("%.78
98a50 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 20  c",'-')..**.**  
98a60 20 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d 70      +  When comp
98a70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 6f  iled using GCC o
98a80 6e 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 20  n a SPARC, this 
98a90 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74  version of print
98aa0 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  f is.**         
98ab0 66 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 20  faster than the 
98ac0 6c 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 66  library printf f
98ad0 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a 2a  or SUN OS 4.1..*
98ae0 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c 6c  *.**      +  All
98af0 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 66   functions are f
98b00 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e 0a  ully reentrant..
98b10 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  **.*/../*.** Con
98b20 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 61  version types fa
98b30 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 20  ll into various 
98b40 63 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 65  categories as de
98b50 66 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  fined by the.** 
98b60 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 72  following enumer
98b70 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ation..*/.#defin
98b80 65 20 65 74 52 41 44 49 58 20 20 20 20 20 20 20  e etRADIX       
98b90 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 70  1 /* Integer typ
98ba0 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f 2c  es.  %d, %x, %o,
98bb0 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a 2f   and so forth */
98bc0 0a 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 54  .#define etFLOAT
98bd0 20 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f 61         2 /* Floa
98be0 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 20  ting point.  %f 
98bf0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 50  */.#define etEXP
98c00 20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 78           3 /* Ex
98c10 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 61  ponentional nota
98c20 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 20  tion. %e and %E 
98c30 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 4e  */.#define etGEN
98c40 45 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 6c  ERIC     4 /* Fl
98c50 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e 65  oating or expone
98c60 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e 67  ntial, depending
98c70 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 67   on exponent. %g
98c80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 49   */.#define etSI
98c90 5a 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 52  ZE        5 /* R
98ca0 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20  eturn number of 
98cb0 63 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 65  characters proce
98cc0 73 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e 20  ssed so far. %n 
98cd0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 52  */.#define etSTR
98ce0 49 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 74  ING      6 /* St
98cf0 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 65  rings. %s */.#de
98d00 66 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e 47  fine etDYNSTRING
98d10 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 61     7 /* Dynamica
98d20 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74  lly allocated st
98d30 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 65  rings. %z */.#de
98d40 66 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 20  fine etPERCENT  
98d50 20 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 20     8 /* Percent 
98d60 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 64  symbol. %% */.#d
98d70 65 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 20  efine etCHARX   
98d80 20 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 74      9 /* Charact
98d90 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 68  ers. %c */./* Th
98da0 65 20 72 65 73 74 20 61 72 65 20 65 78 74 65 6e  e rest are exten
98db0 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61  sions, not norma
98dc0 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 69  lly found in pri
98dd0 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  ntf() */.#define
98de0 20 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 30   etSQLESCAPE  10
98df0 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68   /* Strings with
98e00 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 20   '\'' doubled.  
98e10 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %q */.#define et
98e20 53 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f 2a  SQLESCAPE2 11 /*
98e30 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c   Strings with '\
98e40 27 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 65  '' doubled and e
98e50 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a 20  nclosed in '',. 
98e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
98e70 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 6f           NULL po
98e80 69 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 20  inters replaced 
98e90 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 51  by SQL NULL.  %Q
98ea0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 4f   */.#define etTO
98eb0 4b 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 61  KEN      12 /* a
98ec0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 6f   pointer to a To
98ed0 6b 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ken structure */
98ee0 0a 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c 49  .#define etSRCLI
98ef0 53 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 6f  ST    13 /* a po
98f00 69 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c 69  inter to a SrcLi
98f10 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  st */.#define et
98f20 50 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f 2a  POINTER    14 /*
98f30 20 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 69   The %p conversi
98f40 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  on */.#define et
98f50 53 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f 2a  SQLESCAPE3 15 /*
98f60 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 77   %w -> Strings w
98f70 69 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 64  ith '\"' doubled
98f80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f 52   */.#define etOR
98f90 44 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 25  DINAL    16 /* %
98fa0 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 33  r -> 1st, 2nd, 3
98fb0 72 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 45  rd, 4th, etc.  E
98fc0 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a 0a  nglish only */..
98fd0 23 64 65 66 69 6e 65 20 65 74 49 4e 56 41 4c 49  #define etINVALI
98fe0 44 20 20 20 20 20 30 20 2f 2a 20 41 6e 79 20 75  D     0 /* Any u
98ff0 6e 72 65 63 6f 67 6e 69 7a 65 64 20 63 6f 6e 76  nrecognized conv
99000 65 72 73 69 6f 6e 20 74 79 70 65 20 2a 2f 0a 0a  ersion type */..
99010 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 74  ./*.** An "etByt
99020 65 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 75  e" is an 8-bit u
99030 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a 2a  nsigned value..*
99040 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e  /.typedef unsign
99050 65 64 20 63 68 61 72 20 65 74 42 79 74 65 3b 0a  ed char etByte;.
99060 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 6c  ./*.** Each buil
99070 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63  tin conversion c
99080 68 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 68  haracter (ex: th
99090 65 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 69  e 'd' in "%d") i
990a0 73 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62  s described.** b
990b0 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
990c0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
990d0 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65  tructure.*/.type
990e0 64 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 6e  def struct et_in
990f0 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  fo {   /* Inform
99100 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68  ation about each
99110 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a 2f   format field */
99120 0a 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 3b  .  char fmttype;
99130 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
99140 68 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20  he format field 
99150 63 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a 20  code letter */. 
99160 20 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 20   etByte base;   
99170 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
99180 20 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 20   base for radix 
99190 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20  conversion */.  
991a0 65 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 20  etByte flags;   
991b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
991c0 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 5f  or more of FLAG_
991d0 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77   constants below
991e0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 70   */.  etByte typ
991f0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
99200 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 72  * Conversion par
99210 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 74  adigm */.  etByt
99220 65 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 20  e charset;      
99230 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
99240 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 20  to aDigits[] of 
99250 74 68 65 20 64 69 67 69 74 73 20 73 74 72 69 6e  the digits strin
99260 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 72  g */.  etByte pr
99270 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
99280 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61  /* Offset into a
99290 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 20  Prefix[] of the 
992a0 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a 2f  prefix string */
992b0 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a  .} et_info;../*.
992c0 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
992d0 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 6c  s for et_info.fl
992e0 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  ags.*/.#define F
992f0 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 20  LAG_SIGNED  1   
99300 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
99310 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 72   value to conver
99320 74 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a 23  t is signed */.#
99330 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 45  define FLAG_INTE
99340 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 75  RN  2     /* Tru
99350 65 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e 61  e if for interna
99360 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64  l use only */.#d
99370 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 4e  efine FLAG_STRIN
99380 47 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c 6f  G  4     /* Allo
99390 77 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 69  w infinity preci
993a0 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  sion */.../*.** 
993b0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  The following ta
993c0 62 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 20  ble is searched 
993d0 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 20  linearly, so it 
993e0 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 74  is good to put t
993f0 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 75  he.** most frequ
99400 65 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 65  ently used conve
99410 72 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 73  rsion types firs
99420 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  t..*/.static con
99430 73 74 20 63 68 61 72 20 61 44 69 67 69 74 73 5b  st char aDigits[
99440 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 41  ] = "0123456789A
99450 42 43 44 45 46 30 31 32 33 34 35 36 37 38 39 61  BCDEF0123456789a
99460 62 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 63  bcdef";.static c
99470 6f 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 69  onst char aPrefi
99480 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 30  x[] = "-x0\000X0
99490 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  ";.static const 
994a0 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b  et_info fmtinfo[
994b0 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c 20  ] = {.  {  'd', 
994c0 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20  10, 1, etRADIX, 
994d0 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20       0,  0 },.  
994e0 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 65  {  's',  0, 4, e
994f0 74 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c 20  tSTRING,     0, 
99500 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 20   0 },.  {  'g', 
99510 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43   0, 1, etGENERIC
99520 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20  ,    30, 0 },.  
99530 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 65  {  'z',  0, 4, e
99540 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c 20  tDYNSTRING,  0, 
99550 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 20   0 },.  {  'q', 
99560 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41   0, 4, etSQLESCA
99570 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20  PE,  0,  0 },.  
99580 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 65  {  'Q',  0, 4, e
99590 74 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c 20  tSQLESCAPE2, 0, 
995a0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 20   0 },.  {  'w', 
995b0 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41   0, 4, etSQLESCA
995c0 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20  PE3, 0,  0 },.  
995d0 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 65  {  'c',  0, 0, e
995e0 74 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c 20  tCHARX,      0, 
995f0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 20   0 },.  {  'o', 
99600 20 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20   8, 0, etRADIX, 
99610 20 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 20       0,  2 },.  
99620 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 65  {  'u', 10, 0, e
99630 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20  tRADIX,      0, 
99640 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 20   0 },.  {  'x', 
99650 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20  16, 0, etRADIX, 
99660 20 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 20       16, 1 },.  
99670 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 65  {  'X', 16, 0, e
99680 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20  tRADIX,      0, 
99690 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 51   4 },.#ifndef SQ
996a0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
996b0 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 66  NG_POINT.  {  'f
996c0 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f 41  ',  0, 1, etFLOA
996d0 54 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  T,      0,  0 },
996e0 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 31  .  {  'e',  0, 1
996f0 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20  , etEXP,        
99700 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 45  30, 0 },.  {  'E
99710 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c  ',  0, 1, etEXP,
99720 20 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d 2c          14, 0 },
99730 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 31  .  {  'G',  0, 1
99740 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20  , etGENERIC,    
99750 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a  14, 0 },.#endif.
99760 20 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 2c    {  'i', 10, 1,
99770 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30   etRADIX,      0
99780 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 27  ,  0 },.  {  'n'
99790 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 2c  ,  0, 0, etSIZE,
997a0 20 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a         0,  0 },.
997b0 20 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 2c    {  '%',  0, 0,
997c0 20 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 30   etPERCENT,    0
997d0 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 27  ,  0 },.  {  'p'
997e0 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e 54  , 16, 0, etPOINT
997f0 45 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c 0a  ER,    0,  1 },.
99800 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 74  ./* All the rest
99810 20 68 61 76 65 20 74 68 65 20 46 4c 41 47 5f 49   have the FLAG_I
99820 4e 54 45 52 4e 20 62 69 74 20 73 65 74 20 61 6e  NTERN bit set an
99830 64 20 61 72 65 20 74 68 75 73 20 66 6f 72 20 69  d are thus for i
99840 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 6f  nternal.** use o
99850 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 27 54 27 2c  nly */.  {  'T',
99860 20 20 30 2c 20 32 2c 20 65 74 54 4f 4b 45 4e 2c    0, 2, etTOKEN,
99870 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
99880 20 7b 20 20 27 53 27 2c 20 20 30 2c 20 32 2c 20   {  'S',  0, 2, 
99890 65 74 53 52 43 4c 49 53 54 2c 20 20 20 20 30 2c  etSRCLIST,    0,
998a0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 72 27 2c    0 },.  {  'r',
998b0 20 31 30 2c 20 33 2c 20 65 74 4f 52 44 49 4e 41   10, 3, etORDINA
998c0 4c 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 7d  L,    0,  0 },.}
998d0 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49  ;../*.** If SQLI
998e0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
998f0 5f 50 4f 49 4e 54 20 69 73 20 64 65 66 69 6e 65  _POINT is define
99900 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20  d, then none of 
99910 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  the floating poi
99920 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e  nt.** conversion
99930 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a  s will work..*/.
99940 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
99950 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
99960 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20  NT./*.** "*val" 
99970 69 73 20 61 20 64 6f 75 62 6c 65 20 73 75 63 68  is a double such
99980 20 74 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76 61   that 0.1 <= *va
99990 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 52 65 74 75  l < 10.0.** Retu
999a0 72 6e 20 74 68 65 20 61 73 63 69 69 20 63 6f 64  rn the ascii cod
999b0 65 20 66 6f 72 20 74 68 65 20 6c 65 61 64 69 6e  e for the leadin
999c0 67 20 64 69 67 69 74 20 6f 66 20 2a 76 61 6c 2c  g digit of *val,
999d0 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c   then.** multipl
999e0 79 20 22 2a 76 61 6c 22 20 62 79 20 31 30 2e 30  y "*val" by 10.0
999f0 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e   to renormalize.
99a00 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a  .**.** Example:.
99a10 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20  **     input:   
99a20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 34 31 35 39    *val = 3.14159
99a30 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20  .**     output: 
99a40 20 20 20 2a 76 61 6c 20 3d 20 31 2e 34 31 35 39     *val = 1.4159
99a50 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 72 65 74      function ret
99a60 75 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20  urn = '3'.**.** 
99a70 54 68 65 20 63 6f 75 6e 74 65 72 20 2a 63 6e 74  The counter *cnt
99a80 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
99a90 65 61 63 68 20 74 69 6d 65 2e 20 20 41 66 74 65  each time.  Afte
99aa0 72 20 63 6f 75 6e 74 65 72 20 65 78 63 65 65 64  r counter exceed
99ab0 73 0a 2a 2a 20 31 36 20 28 74 68 65 20 6e 75 6d  s.** 16 (the num
99ac0 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61  ber of significa
99ad0 6e 74 20 64 69 67 69 74 73 20 69 6e 20 61 20 36  nt digits in a 6
99ae0 34 2d 62 69 74 20 66 6c 6f 61 74 29 20 27 30 27  4-bit float) '0'
99af0 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65   is.** always re
99b00 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
99b10 63 20 63 68 61 72 20 65 74 5f 67 65 74 64 69 67  c char et_getdig
99b20 69 74 28 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59  it(LONGDOUBLE_TY
99b30 50 45 20 2a 76 61 6c 2c 20 69 6e 74 20 2a 63 6e  PE *val, int *cn
99b40 74 29 7b 0a 20 20 69 6e 74 20 64 69 67 69 74 3b  t){.  int digit;
99b50 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59  .  LONGDOUBLE_TY
99b60 50 45 20 64 3b 0a 20 20 69 66 28 20 28 2a 63 6e  PE d;.  if( (*cn
99b70 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20 72 65 74  t)++ >= 16 ) ret
99b80 75 72 6e 20 27 30 27 3b 0a 20 20 64 69 67 69 74  urn '0';.  digit
99b90 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b 0a 20 20   = (int)*val;.  
99ba0 64 20 3d 20 64 69 67 69 74 3b 0a 20 20 64 69 67  d = digit;.  dig
99bb0 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20 2a 76 61  it += '0';.  *va
99bc0 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64 29 2a 31  l = (*val - d)*1
99bd0 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 63  0.0;.  return (c
99be0 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a 23 65 6e  har)digit;.}.#en
99bf0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
99c00 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
99c10 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65  T */../*.** Appe
99c20 6e 64 20 4e 20 73 70 61 63 65 20 63 68 61 72 61  nd N space chara
99c30 63 74 65 72 73 20 74 6f 20 74 68 65 20 67 69 76  cters to the giv
99c40 65 6e 20 73 74 72 69 6e 67 20 62 75 66 66 65 72  en string buffer
99c50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
99c60 20 61 70 70 65 6e 64 53 70 61 63 65 28 53 74 72   appendSpace(Str
99c70 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 69  Accum *pAccum, i
99c80 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74 69 63 20  nt N){.  static 
99c90 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 70 61 63  const char zSpac
99ca0 65 73 5b 5d 20 3d 20 22 20 20 20 20 20 20 20 20  es[] = "        
99cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
99cc0 20 20 20 20 20 22 3b 0a 20 20 77 68 69 6c 65 28       ";.  while(
99cd0 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28   N>=(int)sizeof(
99ce0 7a 53 70 61 63 65 73 29 2d 31 20 29 7b 0a 20 20  zSpaces)-1 ){.  
99cf0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
99d00 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
99d10 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 6f 66 28  zSpaces, sizeof(
99d20 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a 20 20 20  zSpaces)-1);.   
99d30 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a 53 70   N -= sizeof(zSp
99d40 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 20 69  aces)-1;.  }.  i
99d50 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 73 71  f( N>0 ){.    sq
99d60 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
99d70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61  end(pAccum, zSpa
99d80 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d 0a 0a  ces, N);.  }.}..
99d90 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 6e 65  /*.** On machine
99da0 73 20 77 69 74 68 20 61 20 73 6d 61 6c 6c 20 73  s with a small s
99db0 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75 20 63  tack size, you c
99dc0 61 6e 20 72 65 64 65 66 69 6e 65 20 74 68 65 0a  an redefine the.
99dd0 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f  ** SQLITE_PRINT_
99de0 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 65 20 6c  BUF_SIZE to be l
99df0 65 73 73 20 74 68 61 6e 20 33 35 30 2e 20 20 42  ess than 350.  B
99e00 75 74 20 62 65 77 61 72 65 20 2d 20 66 6f 72 0a  ut beware - for.
99e10 2a 2a 20 73 6d 61 6c 6c 65 72 20 76 61 6c 75 65  ** smaller value
99e20 73 20 73 6f 6d 65 20 25 66 20 63 6f 6e 76 65 72  s some %f conver
99e30 73 69 6f 6e 73 20 6d 61 79 20 67 6f 20 69 6e 74  sions may go int
99e40 6f 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6c 6f  o an infinite lo
99e50 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  op..*/.#ifndef S
99e60 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f  QLITE_PRINT_BUF_
99e70 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
99e80 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53  LITE_PRINT_BUF_S
99e90 49 5a 45 20 33 35 30 0a 23 65 6e 64 69 66 0a 23  IZE 350.#endif.#
99ea0 64 65 66 69 6e 65 20 65 74 42 55 46 53 49 5a 45  define etBUFSIZE
99eb0 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55   SQLITE_PRINT_BU
99ec0 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69 7a 65 20  F_SIZE  /* Size 
99ed0 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  of the output bu
99ee0 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ffer */../*.** T
99ef0 68 65 20 72 6f 6f 74 20 70 72 6f 67 72 61 6d 2e  he root program.
99f00 20 20 41 6c 6c 20 76 61 72 69 61 74 69 6f 6e 73    All variations
99f10 20 63 61 6c 6c 20 74 68 69 73 20 63 6f 72 65 2e   call this core.
99f20 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a 0a 2a  .**.** INPUTS:.*
99f30 2a 20 20 20 66 75 6e 63 20 20 20 54 68 69 73 20  *   func   This 
99f40 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
99f50 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e  a function takin
99f60 67 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74  g three argument
99f70 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
99f80 31 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  1. A pointer to 
99f90 61 6e 79 74 68 69 6e 67 2e 20 20 53 61 6d 65 20  anything.  Same 
99fa0 61 73 20 74 68 65 20 22 61 72 67 22 20 70 61 72  as the "arg" par
99fb0 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 20 20 20  ameter..**      
99fc0 20 20 20 20 20 20 32 2e 20 41 20 70 6f 69 6e 74        2. A point
99fd0 65 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f  er to the list o
99fe0 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20  f characters to 
99ff0 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20  be output.**    
9a000 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65             (Note
9a010 2c 20 74 68 69 73 20 6c 69 73 74 20 69 73 20 4e  , this list is N
9a020 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74  OT null terminat
9a030 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  ed.).**         
9a040 20 20 20 33 2e 20 41 6e 20 69 6e 74 65 67 65 72     3. An integer
9a050 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   number of chara
9a060 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70  cters to be outp
9a070 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ut..**          
9a080 20 20 20 20 20 28 4e 6f 74 65 3a 20 54 68 69 73       (Note: This
9a090 20 6e 75 6d 62 65 72 20 6d 69 67 68 74 20 62 65   number might be
9a0a0 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20 20 20   zero.).**.**   
9a0b0 61 72 67 20 20 20 20 54 68 69 73 20 69 73 20 74  arg    This is t
9a0c0 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e  he pointer to an
9a0d0 79 74 68 69 6e 67 20 77 68 69 63 68 20 77 69 6c  ything which wil
9a0e0 6c 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74  l be passed as t
9a0f0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  he.**          f
9a100 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
9a110 20 22 66 75 6e 63 22 2e 20 20 55 73 65 20 69 74   "func".  Use it
9a120 20 66 6f 72 20 77 68 61 74 65 76 65 72 20 79 6f   for whatever yo
9a130 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  u like..**.**   
9a140 66 6d 74 20 20 20 20 54 68 69 73 20 69 73 20 74  fmt    This is t
9a150 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67  he format string
9a160 2c 20 61 73 20 69 6e 20 74 68 65 20 75 73 75 61  , as in the usua
9a170 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  l print..**.**  
9a180 20 61 70 20 20 20 20 20 54 68 69 73 20 69 73 20   ap     This is 
9a190 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6c  a pointer to a l
9a1a0 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ist of arguments
9a1b0 2e 20 20 53 61 6d 65 20 61 73 20 69 6e 0a 2a 2a  .  Same as in.**
9a1c0 20 20 20 20 20 20 20 20 20 20 76 66 70 72 69 6e            vfprin
9a1d0 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55 54 53  t..**.** OUTPUTS
9a1e0 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 54 68  :.**          Th
9a1f0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
9a200 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  s the total numb
9a210 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
9a220 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20   sent to.**     
9a230 20 20 20 20 20 74 68 65 20 66 75 6e 63 74 69 6f       the functio
9a240 6e 20 22 66 75 6e 63 22 2e 20 20 52 65 74 75 72  n "func".  Retur
9a250 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72 72 6f 72  ns -1 on a error
9a260 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
9a270 74 20 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77  t the order in w
9a280 68 69 63 68 20 61 75 74 6f 6d 61 74 69 63 20 76  hich automatic v
9a290 61 72 69 61 62 6c 65 73 20 61 72 65 20 64 65 63  ariables are dec
9a2a0 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a 2a 20 73  lared below.** s
9a2b0 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20 61 20 62  eems to make a b
9a2c0 69 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e  ig difference in
9a2d0 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 68 6f 77   determining how
9a2e0 20 66 61 73 74 20 74 68 69 73 20 62 65 61 73 74   fast this beast
9a2f0 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a 2a 2f  .** will run..*/
9a300 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9a310 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72  void sqlite3VXPr
9a320 69 6e 74 66 28 0a 20 20 53 74 72 41 63 63 75 6d  intf(.  StrAccum
9a330 20 2a 70 41 63 63 75 6d 2c 20 20 20 20 20 20 20   *pAccum,       
9a340 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63             /* Ac
9a350 63 75 6d 75 6c 61 74 65 20 72 65 73 75 6c 74 73  cumulate results
9a360 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 75   here */.  int u
9a370 73 65 45 78 74 65 6e 64 65 64 2c 20 20 20 20 20  seExtended,     
9a380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
9a390 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 64 20   Allow extended 
9a3a0 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f  %-conversions */
9a3b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66  .  const char *f
9a3c0 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
9a3d0 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 74 20        /* Format 
9a3e0 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f 6c  string */.  va_l
9a3f0 69 73 74 20 61 70 20 20 20 20 20 20 20 20 20 20  ist ap          
9a400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
9a410 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29  * arguments */.)
9a420 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 20  {.  int c;      
9a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
9a440 2a 20 4e 65 78 74 20 63 68 61 72 61 63 74 65 72  * Next character
9a450 20 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 20 73   in the format s
9a460 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20  tring */.  char 
9a470 2a 62 75 66 70 74 3b 20 20 20 20 20 20 20 20 20  *bufpt;         
9a480 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
9a490 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69   to the conversi
9a4a0 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69  on buffer */.  i
9a4b0 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 20 20  nt precision;   
9a4c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
9a4d0 63 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 75  cision of the cu
9a4e0 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20  rrent field */. 
9a4f0 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20 20   int length;    
9a500 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
9a510 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 69 65  ength of the fie
9a520 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b  ld */.  int idx;
9a530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9a540 20 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c 20     /* A general 
9a550 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f 75  purpose loop cou
9a560 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 77 69  nter */.  int wi
9a570 64 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  dth;            
9a580 20 20 20 20 20 2f 2a 20 57 69 64 74 68 20 6f 66       /* Width of
9a590 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65   the current fie
9a5a0 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  ld */.  etByte f
9a5b0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 3b  lag_leftjustify;
9a5c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2d     /* True if "-
9a5d0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
9a5e0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
9a5f0 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20 20 20  ag_plussign;    
9a600 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2b 22    /* True if "+"
9a610 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
9a620 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
9a630 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20 20 20  g_blanksign;    
9a640 20 2f 2a 20 54 72 75 65 20 69 66 20 22 20 22 20   /* True if " " 
9a650 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20  flag is present 
9a660 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
9a670 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 20  _alternateform; 
9a680 2f 2a 20 54 72 75 65 20 69 66 20 22 23 22 20 66  /* True if "#" f
9a690 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a  lag is present *
9a6a0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
9a6b0 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20 20 2f  altform2;      /
9a6c0 2a 20 54 72 75 65 20 69 66 20 22 21 22 20 66 6c  * True if "!" fl
9a6d0 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  ag is present */
9a6e0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 7a  .  etByte flag_z
9a6f0 65 72 6f 70 61 64 3b 20 20 20 20 20 20 20 2f 2a  eropad;       /*
9a700 20 54 72 75 65 20 69 66 20 66 69 65 6c 64 20 77   True if field w
9a710 69 64 74 68 20 63 6f 6e 73 74 61 6e 74 20 73 74  idth constant st
9a720 61 72 74 73 20 77 69 74 68 20 7a 65 72 6f 20 2a  arts with zero *
9a730 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
9a740 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20 20 20 2f  long;          /
9a750 2a 20 54 72 75 65 20 69 66 20 22 6c 22 20 66 6c  * True if "l" fl
9a760 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  ag is present */
9a770 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c  .  etByte flag_l
9a780 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20 2f 2a  onglong;      /*
9a790 20 54 72 75 65 20 69 66 20 74 68 65 20 22 6c 6c   True if the "ll
9a7a0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
9a7b0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 64 6f  t */.  etByte do
9a7c0 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ne;             
9a7d0 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d 69 6e    /* Loop termin
9a7e0 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a 20 20  ation flag */.  
9a7f0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 6c 6f  sqlite_uint64 lo
9a800 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20 56 61  ngvalue;   /* Va
9a810 6c 75 65 20 66 6f 72 20 69 6e 74 65 67 65 72 20  lue for integer 
9a820 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47 44  types */.  LONGD
9a830 4f 55 42 4c 45 5f 54 59 50 45 20 72 65 61 6c 76  OUBLE_TYPE realv
9a840 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65 20 66  alue; /* Value f
9a850 6f 72 20 72 65 61 6c 20 74 79 70 65 73 20 2a 2f  or real types */
9a860 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f  .  const et_info
9a870 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20 2f 2a   *infop;      /*
9a880 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
9a890 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 6f  appropriate info
9a8a0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
9a8b0 63 68 61 72 20 62 75 66 5b 65 74 42 55 46 53 49  char buf[etBUFSI
9a8c0 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f  ZE];       /* Co
9a8d0 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20  nversion buffer 
9a8e0 2a 2f 0a 20 20 63 68 61 72 20 70 72 65 66 69 78  */.  char prefix
9a8f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
9a900 2f 2a 20 50 72 65 66 69 78 20 63 68 61 72 61 63  /* Prefix charac
9a910 74 65 72 2e 20 20 22 2b 22 20 6f 72 20 22 2d 22  ter.  "+" or "-"
9a920 20 6f 72 20 22 20 22 20 6f 72 20 27 5c 30 27 2e   or " " or '\0'.
9a930 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 74 79   */.  etByte xty
9a940 70 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  pe = 0;         
9a950 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70   /* Conversion p
9a960 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 68 61  aradigm */.  cha
9a970 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 20 20 20  r *zExtra;      
9a980 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61          /* Extra
9a990 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72   memory used for
9a9a0 20 65 74 54 43 4c 45 53 43 41 50 45 20 63 6f 6e   etTCLESCAPE con
9a9b0 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69 66 6e  versions */.#ifn
9a9c0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
9a9d0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
9a9e0 20 69 6e 74 20 20 65 78 70 2c 20 65 32 3b 20 20   int  exp, e2;  
9a9f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65              /* e
9aa00 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61 6c 20  xponent of real 
9aa10 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64 6f 75  numbers */.  dou
9aa20 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20 20 20  ble rounder;    
9aa30 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
9aa40 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66 6c 6f  for rounding flo
9aa50 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
9aa60 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  es */.  etByte f
9aa70 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 20 20 20  lag_dp;         
9aa80 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 65     /* True if de
9aa90 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68 6f 75  cimal point shou
9aaa0 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f 0a 20  ld be shown */. 
9aab0 20 65 74 42 79 74 65 20 66 6c 61 67 5f 72 74 7a   etByte flag_rtz
9aac0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
9aad0 72 75 65 20 69 66 20 74 72 61 69 6c 69 6e 67 20  rue if trailing 
9aae0 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 62 65 20  zeros should be 
9aaf0 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65 74 42  removed */.  etB
9ab00 79 74 65 20 66 6c 61 67 5f 65 78 70 3b 20 20 20  yte flag_exp;   
9ab10 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
9ab20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 6c 61 79  to force display
9ab30 20 6f 66 20 74 68 65 20 65 78 70 6f 6e 65 6e 74   of the exponent
9ab40 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b 20 20   */.  int nsd;  
9ab50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ab60 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 69   /* Number of si
9ab70 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73  gnificant digits
9ab80 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23 65 6e   returned */.#en
9ab90 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20 3d 20  dif..  length = 
9aba0 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 30 3b 0a  0;.  bufpt = 0;.
9abb0 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66 6d 74    for(; (c=(*fmt
9abc0 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b 0a 20  ))!=0; ++fmt){. 
9abd0 20 20 20 69 66 28 20 63 21 3d 27 25 27 20 29 7b     if( c!='%' ){
9abe0 0a 20 20 20 20 20 20 69 6e 74 20 61 6d 74 3b 0a  .      int amt;.
9abf0 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 28 63        bufpt = (c
9ac00 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20 20 20  har *)fmt;.     
9ac10 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20 20 20   amt = 1;.      
9ac20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b 66 6d  while( (c=(*++fm
9ac30 74 29 29 21 3d 27 25 27 20 26 26 20 63 21 3d 30  t))!='%' && c!=0
9ac40 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20 20 20   ) amt++;.      
9ac50 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
9ac60 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75  ppend(pAccum, bu
9ac70 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20 20 20  fpt, amt);.     
9ac80 20 69 66 28 20 63 3d 3d 30 20 29 20 62 72 65 61   if( c==0 ) brea
9ac90 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
9aca0 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d 3d 30   (c=(*++fmt))==0
9acb0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
9acc0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
9acd0 70 41 63 63 75 6d 2c 20 22 25 22 2c 20 31 29 3b  pAccum, "%", 1);
9ace0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
9acf0 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20    }.    /* Find 
9ad00 6f 75 74 20 77 68 61 74 20 66 6c 61 67 73 20 61  out what flags a
9ad10 72 65 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  re present */.  
9ad20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69    flag_leftjusti
9ad30 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73 73 69  fy = flag_plussi
9ad40 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e 6b 73  gn = flag_blanks
9ad50 69 67 6e 20 3d 20 0a 20 20 20 20 20 66 6c 61 67  ign = .     flag
9ad60 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d  _alternateform =
9ad70 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d   flag_altform2 =
9ad80 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20   flag_zeropad = 
9ad90 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d 20 30 3b  0;.    done = 0;
9ada0 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 73  .    do{.      s
9adb0 77 69 74 63 68 28 20 63 20 29 7b 0a 20 20 20 20  witch( c ){.    
9adc0 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 20 20      case '-':   
9add0 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
9ade0 20 3d 20 31 3b 20 20 20 20 20 62 72 65 61 6b 3b   = 1;     break;
9adf0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 2b  .        case '+
9ae00 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69  ':   flag_plussi
9ae10 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62  gn = 1;        b
9ae20 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
9ae30 73 65 20 27 20 27 3a 20 20 20 66 6c 61 67 5f 62  se ' ':   flag_b
9ae40 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b 20 20 20  lanksign = 1;   
9ae50 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
9ae60 20 20 20 63 61 73 65 20 27 23 27 3a 20 20 20 66     case '#':   f
9ae70 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
9ae80 6d 20 3d 20 31 3b 20 20 20 62 72 65 61 6b 3b 0a  m = 1;   break;.
9ae90 20 20 20 20 20 20 20 20 63 61 73 65 20 27 21 27          case '!'
9aea0 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d  :   flag_altform
9aeb0 32 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 72  2 = 1;        br
9aec0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
9aed0 65 20 27 30 27 3a 20 20 20 66 6c 61 67 5f 7a 65  e '0':   flag_ze
9aee0 72 6f 70 61 64 20 3d 20 31 3b 20 20 20 20 20 20  ropad = 1;      
9aef0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
9af00 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 64 6f    default:    do
9af10 6e 65 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  ne = 1;         
9af20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
9af30 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 69 6c       }.    }whil
9af40 65 28 20 21 64 6f 6e 65 20 26 26 20 28 63 3d 28  e( !done && (c=(
9af50 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29 3b 0a 20  *++fmt))!=0 );. 
9af60 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 66 69     /* Get the fi
9af70 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a 20 20 20  eld width */.   
9af80 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20   width = 0;.    
9af90 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20  if( c=='*' ){.  
9afa0 20 20 20 20 77 69 64 74 68 20 3d 20 76 61 5f 61      width = va_a
9afb0 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
9afc0 20 20 69 66 28 20 77 69 64 74 68 3c 30 20 29 7b    if( width<0 ){
9afd0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 65  .        flag_le
9afe0 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 0a 20  ftjustify = 1;. 
9aff0 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 2d         width = -
9b000 77 69 64 74 68 3b 0a 20 20 20 20 20 20 7d 0a 20  width;.      }. 
9b010 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b       c = *++fmt;
9b020 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
9b030 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20    while( c>='0' 
9b040 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20  && c<='9' ){.   
9b050 20 20 20 20 20 77 69 64 74 68 20 3d 20 77 69 64       width = wid
9b060 74 68 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b  th*10 + c - '0';
9b070 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b  .        c = *++
9b080 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  fmt;.      }.   
9b090 20 7d 0a 20 20 20 20 69 66 28 20 77 69 64 74 68   }.    if( width
9b0a0 20 3e 20 65 74 42 55 46 53 49 5a 45 2d 31 30 20   > etBUFSIZE-10 
9b0b0 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d  ){.      width =
9b0c0 20 65 74 42 55 46 53 49 5a 45 2d 31 30 3b 0a 20   etBUFSIZE-10;. 
9b0d0 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20     }.    /* Get 
9b0e0 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f  the precision */
9b0f0 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2e 27 20  .    if( c=='.' 
9b100 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69  ){.      precisi
9b110 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20  on = 0;.      c 
9b120 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20  = *++fmt;.      
9b130 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20  if( c=='*' ){.  
9b140 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20        precision 
9b150 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29  = va_arg(ap,int)
9b160 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  ;.        if( pr
9b170 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63  ecision<0 ) prec
9b180 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69 73 69  ision = -precisi
9b190 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20  on;.        c = 
9b1a0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65  *++fmt;.      }e
9b1b0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77 68 69  lse{.        whi
9b1c0 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c  le( c>='0' && c<
9b1d0 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='9' ){.        
9b1e0 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72    precision = pr
9b1f0 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20 63 20 2d  ecision*10 + c -
9b200 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '0';.          
9b210 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
9b220 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
9b230 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
9b240 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31 3b 0a 20  recision = -1;. 
9b250 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20     }.    /* Get 
9b260 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74  the conversion t
9b270 79 70 65 20 6d 6f 64 69 66 69 65 72 20 2a 2f 0a  ype modifier */.
9b280 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29      if( c=='l' )
9b290 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e  {.      flag_lon
9b2a0 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 20 3d  g = 1;.      c =
9b2b0 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 69   *++fmt;.      i
9b2c0 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20  f( c=='l' ){.   
9b2d0 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f       flag_longlo
9b2e0 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ng = 1;.        
9b2f0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
9b300 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
9b310 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d   flag_longlong =
9b320 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
9b330 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61  }else{.      fla
9b340 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f 6c 6f  g_long = flag_lo
9b350 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20  nglong = 0;.    
9b360 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63 68 20 74  }.    /* Fetch t
9b370 68 65 20 69 6e 66 6f 20 65 6e 74 72 79 20 66 6f  he info entry fo
9b380 72 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20  r the field */. 
9b390 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69     infop = &fmti
9b3a0 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 78 74 79 70  nfo[0];.    xtyp
9b3b0 65 20 3d 20 65 74 49 4e 56 41 4c 49 44 3b 0a 20  e = etINVALID;. 
9b3c0 20 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 64     for(idx=0; id
9b3d0 78 3c 41 72 72 61 79 53 69 7a 65 28 66 6d 74 69  x<ArraySize(fmti
9b3e0 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 7b 0a 20 20  nfo); idx++){.  
9b3f0 20 20 20 20 69 66 28 20 63 3d 3d 66 6d 74 69 6e      if( c==fmtin
9b400 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 70 65 20  fo[idx].fmttype 
9b410 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 66 6f 70  ){.        infop
9b420 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 64 78 5d   = &fmtinfo[idx]
9b430 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 73  ;.        if( us
9b440 65 45 78 74 65 6e 64 65 64 20 7c 7c 20 28 69 6e  eExtended || (in
9b450 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41  fop->flags & FLA
9b460 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 29 7b 0a  G_INTERN)==0 ){.
9b470 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20            xtype 
9b480 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65 3b 0a 20  = infop->type;. 
9b490 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
9b4a0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
9b4b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9b4c0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
9b4d0 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 78 74 72  .    }.    zExtr
9b4e0 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 20 2f 2a 20  a = 0;...    /* 
9b4f0 4c 69 6d 69 74 20 74 68 65 20 70 72 65 63 69 73  Limit the precis
9b500 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 6f  ion to prevent o
9b510 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d  verflowing buf[]
9b520 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 69   during conversi
9b530 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 72  on */.    if( pr
9b540 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a  ecision>etBUFSIZ
9b550 45 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e  E-40 && (infop->
9b560 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54 52  flags & FLAG_STR
9b570 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ING)==0 ){.     
9b580 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42   precision = etB
9b590 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d  UFSIZE-40;.    }
9b5a0 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
9b5b0 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76  At this point, v
9b5c0 61 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69  ariables are ini
9b5d0 74 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c  tialized as foll
9b5e0 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ows:.    **.    
9b5f0 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e  **   flag_altern
9b600 61 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20  ateform         
9b610 20 54 52 55 45 20 69 66 20 61 20 27 23 27 20 69   TRUE if a '#' i
9b620 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a  s present..    *
9b630 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d  *   flag_altform
9b640 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2               
9b650 54 52 55 45 20 69 66 20 61 20 27 21 27 20 69 73  TRUE if a '!' is
9b660 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a   present..    **
9b670 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e     flag_plussign
9b680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
9b690 52 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 20  RUE if a '+' is 
9b6a0 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20  present..    ** 
9b6b0 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69    flag_leftjusti
9b6c0 66 79 20 20 20 20 20 20 20 20 20 20 20 20 54 52  fy            TR
9b6d0 55 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 70  UE if a '-' is p
9b6e0 72 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65  resent or if the
9b6f0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
9b700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b710 20 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 74        field widt
9b720 68 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a  h was negative..
9b730 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65      **   flag_ze
9b740 72 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 20  ropad           
9b750 20 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65       TRUE if the
9b760 20 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 74   width began wit
9b770 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c  h 0..    **   fl
9b780 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20  ag_long         
9b790 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69            TRUE i
9b7a0 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27  f the letter 'l'
9b7b0 20 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a   (ell) prefixed.
9b7c0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
9b7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b7e0 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73       the convers
9b7f0 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20  ion character.. 
9b800 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e     **   flag_lon
9b810 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20  glong           
9b820 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20      TRUE if the 
9b830 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c  letter 'll' (ell
9b840 20 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20   ell) prefixed. 
9b850 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
9b860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b870 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69      the conversi
9b880 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20  on character..  
9b890 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e    **   flag_blan
9b8a0 6b 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20  ksign           
9b8b0 20 20 20 54 52 55 45 20 69 66 20 61 20 27 20 27     TRUE if a ' '
9b8c0 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20   is present..   
9b8d0 20 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 20   **   width     
9b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b8f0 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20    The specified 
9b900 66 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68  field width.  Th
9b910 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20  is is.    **    
9b920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b930 20 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 79             alway
9b940 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20  s non-negative. 
9b950 20 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 66   Zero is the def
9b960 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70  ault..    **   p
9b970 72 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20  recision        
9b980 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73             The s
9b990 70 65 63 69 66 69 65 64 20 70 72 65 63 69 73 69  pecified precisi
9b9a0 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  on.  The default
9b9b0 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
9b9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9b9d0 20 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20        is -1..   
9b9e0 20 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 20   **   xtype     
9b9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ba00 20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74    The class of t
9ba10 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20  he conversion.. 
9ba20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20     **   infop   
9ba30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ba40 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74      Pointer to t
9ba50 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69  he appropriate i
9ba60 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20  nfo struct..    
9ba70 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 78  */.    switch( x
9ba80 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61  type ){.      ca
9ba90 73 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20  se etPOINTER:.  
9baa0 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c        flag_longl
9bab0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61  ong = sizeof(cha
9bac0 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29  r*)==sizeof(i64)
9bad0 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c  ;.        flag_l
9bae0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61  ong = sizeof(cha
9baf0 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67  r*)==sizeof(long
9bb00 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f   int);.        /
9bb10 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69  * Fall through i
9bb20 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
9bb30 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  e */.      case 
9bb40 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20  etORDINAL:.     
9bb50 20 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a 20   case etRADIX:. 
9bb60 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 70         if( infop
9bb70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53  ->flags & FLAG_S
9bb80 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 20  IGNED ){.       
9bb90 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20     i64 v;.      
9bba0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e      if( flag_lon
9bbb0 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20  glong ){.       
9bbc0 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28       v = va_arg(
9bbd0 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20 20  ap,i64);.       
9bbe0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61     }else if( fla
9bbf0 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20  g_long ){.      
9bc00 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67        v = va_arg
9bc10 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20  (ap,long int);. 
9bc20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
9bc30 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20              v = 
9bc40 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a  va_arg(ap,int);.
9bc50 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
9bc60 20 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b        if( v<0 ){
9bc70 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e  .            lon
9bc80 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20  gvalue = -v;.   
9bc90 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
9bca0 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20  = '-';.         
9bcb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
9bcc0 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20      longvalue = 
9bcd0 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  v;.            i
9bce0 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e  f( flag_plussign
9bcf0 20 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78   )        prefix
9bd00 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20   = '+';.        
9bd10 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61      else if( fla
9bd20 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70  g_blanksign )  p
9bd30 72 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20  refix = ' ';.   
9bd40 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20           else   
9bd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9bd60 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a      prefix = 0;.
9bd70 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
9bd80 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9bd90 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f       if( flag_lo
9bda0 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20  nglong ){.      
9bdb0 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
9bdc0 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34 29  = va_arg(ap,u64)
9bdd0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
9bde0 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20  e if( flag_long 
9bdf0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  ){.            l
9be00 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72  ongvalue = va_ar
9be10 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c 6f  g(ap,unsigned lo
9be20 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  ng int);.       
9be30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
9be40 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
9be50 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69  = va_arg(ap,unsi
9be60 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 20  gned int);.     
9be70 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
9be80 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20   prefix = 0;.   
9be90 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
9bea0 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20  f( longvalue==0 
9beb0 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65  ) flag_alternate
9bec0 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20  form = 0;.      
9bed0 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70    if( flag_zerop
9bee0 61 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c  ad && precision<
9bef0 77 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30  width-(prefix!=0
9bf00 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
9bf10 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68  recision = width
9bf20 2d 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20  -(prefix!=0);.  
9bf30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
9bf40 62 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 42  bufpt = &buf[etB
9bf50 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 20  UFSIZE-1];.     
9bf60 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74     if( xtype==et
9bf70 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20  ORDINAL ){.     
9bf80 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
9bf90 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20  t char zOrd[] = 
9bfa0 22 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20  "thstndrd";.    
9bfb0 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69        int x = (i
9bfc0 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20  nt)(longvalue % 
9bfd0 31 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  10);.          i
9bfe0 66 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67  f( x>=4 || (long
9bff0 76 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20  value/10)%10==1 
9c000 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78  ){.            x
9c010 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
9c020 7d 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b  }.          buf[
9c030 65 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 7a  etBUFSIZE-3] = z
9c040 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 20  Ord[x*2];.      
9c050 20 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a      buf[etBUFSIZ
9c060 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b  E-2] = zOrd[x*2+
9c070 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75  1];.          bu
9c080 66 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20  fpt -= 2;.      
9c090 20 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 20    }.        {.  
9c0a0 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72          register
9c0b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65   const char *cse
9c0c0 74 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72  t;      /* Use r
9c0d0 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65  egisters for spe
9c0e0 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ed */.          
9c0f0 72 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73  register int bas
9c100 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65  e;.          cse
9c110 74 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66  t = &aDigits[inf
9c120 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20  op->charset];.  
9c130 20 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69          base = i
9c140 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20  nfop->base;.    
9c150 20 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20        do{       
9c160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c180 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74      /* Convert t
9c190 6f 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20  o ascii */.     
9c1a0 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74         *(--bufpt
9c1b0 29 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c  ) = cset[longval
9c1c0 75 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20  ue%base];.      
9c1d0 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
9c1e0 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65  = longvalue/base
9c1f0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69  ;.          }whi
9c200 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20  le( longvalue>0 
9c210 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
9c220 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69       length = (i
9c230 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49  nt)(&buf[etBUFSI
9c240 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20  ZE-1]-bufpt);.  
9c250 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72        for(idx=pr
9c260 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20  ecision-length; 
9c270 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20  idx>0; idx--){. 
9c280 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66           *(--buf
9c290 70 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20  pt) = '0';      
9c2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c2b0 20 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70         /* Zero p
9c2c0 61 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a  ad */.        }.
9c2d0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66          if( pref
9c2e0 69 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20  ix ) *(--bufpt) 
9c2f0 3d 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20  = prefix;       
9c300 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73          /* Add s
9c310 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ign */.        i
9c320 66 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74  f( flag_alternat
9c330 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e  eform && infop->
9c340 70 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f  prefix ){      /
9c350 2a 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 78  * Add "0" or "0x
9c360 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63  " */.          c
9c370 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a  onst char *pre;.
9c380 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 78            char x
9c390 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20  ;.          pre 
9c3a0 3d 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70  = &aPrefix[infop
9c3b0 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20  ->prefix];.     
9c3c0 20 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a       for(; (x=(*
9c3d0 70 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29  pre))!=0; pre++)
9c3e0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b   *(--bufpt) = x;
9c3f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
9c400 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74     length = (int
9c410 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45  )(&buf[etBUFSIZE
9c420 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 20  -1]-bufpt);.    
9c430 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
9c440 20 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20   case etFLOAT:. 
9c450 20 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a       case etEXP:
9c460 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45  .      case etGE
9c470 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72  NERIC:.        r
9c480 65 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72  ealvalue = va_ar
9c490 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69  g(ap,double);.#i
9c4a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
9c4b0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
9c4c0 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65  .        if( pre
9c4d0 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69  cision<0 ) preci
9c4e0 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20  sion = 6;       
9c4f0 20 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74    /* Set default
9c500 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20   precision */.  
9c510 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73        if( precis
9c520 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d  ion>etBUFSIZE/2-
9c530 31 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d  10 ) precision =
9c540 20 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b   etBUFSIZE/2-10;
9c550 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61  .        if( rea
9c560 6c 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20  lvalue<0.0 ){.  
9c570 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75          realvalu
9c580 65 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a  e = -realvalue;.
9c590 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78            prefix
9c5a0 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20   = '-';.        
9c5b0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
9c5c0 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69   if( flag_plussi
9c5d0 67 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72  gn )          pr
9c5e0 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20  efix = '+';.    
9c5f0 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66        else if( f
9c600 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20  lag_blanksign ) 
9c610 20 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b     prefix = ' ';
9c620 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20  .          else 
9c630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9c640 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
9c650 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
9c660 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d        if( xtype=
9c670 3d 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72  =etGENERIC && pr
9c680 65 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63  ecision>0 ) prec
9c690 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20  ision--;.#if 0. 
9c6a0 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69         /* Roundi
9c6b0 6e 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53  ng works like BS
9c6c0 44 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74  D when the const
9c6d0 61 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73  ant 0.4999 is us
9c6e0 65 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20  ed.  Wierd! */. 
9c6f0 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70         for(idx=p
9c700 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65  recision, rounde
9c710 72 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b  r=0.4999; idx>0;
9c720 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a   idx--, rounder*
9c730 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20  =0.1);.#else.   
9c740 20 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73       /* It makes
9c750 20 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75   more sense to u
9c760 73 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20  se 0.5 */.      
9c770 20 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73    for(idx=precis
9c780 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35  ion, rounder=0.5
9c790 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20  ; idx>0; idx--, 
9c7a0 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a  rounder*=0.1){}.
9c7b0 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69  #endif.        i
9c7c0 66 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41  f( xtype==etFLOA
9c7d0 54 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d  T ) realvalue +=
9c7e0 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20   rounder;.      
9c7f0 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72    /* Normalize r
9c800 65 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68  ealvalue to with
9c810 69 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61  in 10.0 > realva
9c820 6c 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20  lue >= 1.0 */.  
9c830 20 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20        exp = 0;. 
9c840 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
9c850 65 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 29  e3IsNaN((double)
9c860 72 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20  realvalue) ){.  
9c870 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
9c880 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20  "NaN";.         
9c890 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20   length = 3;.   
9c8a0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
9c8b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
9c8c0 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e  if( realvalue>0.
9c8d0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77  0 ){.          w
9c8e0 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e  hile( realvalue>
9c8f0 3d 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35  =1e32 && exp<=35
9c900 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a  0 ){ realvalue *
9c910 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32  = 1e-32; exp+=32
9c920 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68  ; }.          wh
9c930 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d  ile( realvalue>=
9c940 31 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20  1e8 && exp<=350 
9c950 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
9c960 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a  1e-8; exp+=8; }.
9c970 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
9c980 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30   realvalue>=10.0
9c990 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20   && exp<=350 ){ 
9c9a0 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31  realvalue *= 0.1
9c9b0 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20  ; exp++; }.     
9c9c0 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c       while( real
9c9d0 76 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65  value<1e-8 ){ re
9c9e0 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20  alvalue *= 1e8; 
9c9f0 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20  exp-=8; }.      
9ca00 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76      while( realv
9ca10 61 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c  alue<1.0 ){ real
9ca20 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65  value *= 10.0; e
9ca30 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20  xp--; }.        
9ca40 20 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b    if( exp>350 ){
9ca50 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
9ca60 20 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a   prefix=='-' ){.
9ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75                bu
9ca80 66 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20  fpt = "-Inf";.  
9ca90 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20            }else 
9caa0 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20  if( prefix=='+' 
9cab0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
9cac0 20 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b   bufpt = "+Inf";
9cad0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
9cae0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
9caf0 20 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b    bufpt = "Inf";
9cb00 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
9cb10 20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74             lengt
9cb20 68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  h = sqlite3Strle
9cb30 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20  n30(bufpt);.    
9cb40 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
9cb50 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
9cb60 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66     }.        buf
9cb70 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20  pt = buf;.      
9cb80 20 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    /*.        ** 
9cb90 49 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70  If the field typ
9cba0 65 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20  e is etGENERIC, 
9cbb0 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20  then convert to 
9cbc0 65 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20  either etEXP.   
9cbd0 20 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f       ** or etFLO
9cbe0 41 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61  AT, as appropria
9cbf0 74 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  te..        */. 
9cc00 20 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20         flag_exp 
9cc10 3d 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a  = xtype==etEXP;.
9cc20 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70          if( xtyp
9cc30 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20  e!=etFLOAT ){.  
9cc40 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75          realvalu
9cc50 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20  e += rounder;.  
9cc60 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c          if( real
9cc70 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72  value>=10.0 ){ r
9cc80 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b  ealvalue *= 0.1;
9cc90 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20   exp++; }.      
9cca0 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
9ccb0 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43  xtype==etGENERIC
9ccc0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c   ){.          fl
9ccd0 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61  ag_rtz = !flag_a
9cce0 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20  lternateform;.  
9ccf0 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c          if( exp<
9cd00 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73  -4 || exp>precis
9cd10 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ion ){.         
9cd20 20 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50     xtype = etEXP
9cd30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
9cd40 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  e{.            p
9cd50 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69  recision = preci
9cd60 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20  sion - exp;.    
9cd70 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20          xtype = 
9cd80 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20  etFLOAT;.       
9cd90 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
9cda0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c  se{.          fl
9cdb0 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20  ag_rtz = 0;.    
9cdc0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
9cdd0 28 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29  ( xtype==etEXP )
9cde0 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d  {.          e2 =
9cdf0 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   0;.        }els
9ce00 65 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20  e{.          e2 
9ce10 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d  = exp;.        }
9ce20 0a 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30  .        nsd = 0
9ce30 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64  ;.        flag_d
9ce40 70 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30  p = (precision>0
9ce50 20 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c   ?1:0) | flag_al
9ce60 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c  ternateform | fl
9ce70 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20  ag_altform2;.   
9ce80 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e       /* The sign
9ce90 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65   in front of the
9cea0 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20   number */.     
9ceb0 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b     if( prefix ){
9cec0 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
9ced0 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a  pt++) = prefix;.
9cee0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9cef0 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f    /* Digits prio
9cf00 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c  r to the decimal
9cf10 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20   point */.      
9cf20 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20    if( e2<0 ){.  
9cf30 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
9cf40 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20  +) = '0';.      
9cf50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
9cf60 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20     for(; e2>=0; 
9cf70 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20  e2--){.         
9cf80 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
9cf90 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61  et_getdigit(&rea
9cfa0 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20  lvalue,&nsd);.  
9cfb0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9cfc0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54    }.        /* T
9cfd0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
9cfe0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
9cff0 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20  flag_dp ){.     
9d000 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
9d010 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d  = '.';.        }
9d020 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20  .        /* "0" 
9d030 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65  digits after the
9d040 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62   decimal point b
9d050 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69  ut before the fi
9d060 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  rst.        ** s
9d070 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74  ignificant digit
9d080 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a   of the number *
9d090 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32  /.        for(e2
9d0a0 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73  ++; e2<0; precis
9d0b0 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20  ion--, e2++){.  
9d0c0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
9d0d0 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20  precision>0 );. 
9d0e0 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
9d0f0 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20  ++) = '0';.     
9d100 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
9d110 53 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  Significant digi
9d120 74 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63  ts after the dec
9d130 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  imal point */.  
9d140 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72        while( (pr
9d150 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a  ecision--)>0 ){.
9d160 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
9d170 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67  t++) = et_getdig
9d180 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e  it(&realvalue,&n
9d190 73 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  sd);.        }. 
9d1a0 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65         /* Remove
9d1b0 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20   trailing zeros 
9d1c0 61 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e  and the "." if n
9d1d0 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20  o digits follow 
9d1e0 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20  the "." */.     
9d1f0 20 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20     if( flag_rtz 
9d200 26 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20  && flag_dp ){.  
9d210 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62          while( b
9d220 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20  ufpt[-1]=='0' ) 
9d230 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a  *(--bufpt) = 0;.
9d240 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
9d250 28 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20  ( bufpt>buf );. 
9d260 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66           if( buf
9d270 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20  pt[-1]=='.' ){. 
9d280 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66             if( f
9d290 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a  lag_altform2 ){.
9d2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28                *(
9d2b0 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a  bufpt++) = '0';.
9d2c0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
9d2d0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
9d2e0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b   *(--bufpt) = 0;
9d2f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
9d300 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
9d310 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
9d320 41 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73  Add the "eNNN" s
9d330 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20  uffix */.       
9d340 20 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c   if( flag_exp ||
9d350 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b   xtype==etEXP ){
9d360 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
9d370 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b  pt++) = aDigits[
9d380 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b  infop->charset];
9d390 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65  .          if( e
9d3a0 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  xp<0 ){.        
9d3b0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
9d3c0 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70   '-'; exp = -exp
9d3d0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
9d3e0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  e{.            *
9d3f0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b  (bufpt++) = '+';
9d400 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
9d410 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d         if( exp>=
9d420 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  100 ){.         
9d430 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
9d440 28 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29  (char)((exp/100)
9d450 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a  +'0');        /*
9d460 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a   100's digit */.
9d470 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20              exp 
9d480 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20  %= 100;.        
9d490 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28    }.          *(
9d4a0 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72  bufpt++) = (char
9d4b0 29 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20  )(exp/10+'0');  
9d4c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30             /* 10
9d4d0 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20  's digit */.    
9d4e0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
9d4f0 20 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30   = (char)(exp%10
9d500 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20  +'0');          
9d510 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20     /* 1's digit 
9d520 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  */.        }.   
9d530 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b       *bufpt = 0;
9d540 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ..        /* The
9d550 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65   converted numbe
9d560 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e  r is in buf[] an
9d570 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65  d zero terminate
9d580 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20  d. Output it..  
9d590 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68        ** Note th
9d5a0 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73  at the number is
9d5b0 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72   in the usual or
9d5c0 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65  der, not reverse
9d5d0 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20  d as with.      
9d5e0 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e    ** integer con
9d5f0 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20  versions. */.   
9d600 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69       length = (i
9d610 6e 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b 0a  nt)(bufpt-buf);.
9d620 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
9d630 62 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  buf;..        /*
9d640 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20   Special case:  
9d650 41 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f  Add leading zero
9d660 73 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65  s if the flag_ze
9d670 72 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20  ropad flag is.  
9d680 20 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64        ** set and
9d690 20 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74   we are not left
9d6a0 20 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20   justified */.  
9d6b0 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a        if( flag_z
9d6c0 65 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f  eropad && !flag_
9d6d0 6c 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c  leftjustify && l
9d6e0 65 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a  ength < width){.
9d6f0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b            int i;
9d700 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e  .          int n
9d710 50 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65  Pad = width - le
9d720 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20  ngth;.          
9d730 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d  for(i=width; i>=
9d740 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  nPad; i--){.    
9d750 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d          bufpt[i]
9d760 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d   = bufpt[i-nPad]
9d770 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
9d780 20 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66          i = pref
9d790 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20  ix!=0;.         
9d7a0 20 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29   while( nPad-- )
9d7b0 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30   bufpt[i++] = '0
9d7c0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  ';.          len
9d7d0 67 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20  gth = width;.   
9d7e0 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
9d7f0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
9d800 20 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a     case etSIZE:.
9d810 20 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67          *(va_arg
9d820 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63  (ap,int*)) = pAc
9d830 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20  cum->nChar;.    
9d840 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64      length = wid
9d850 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  th = 0;.        
9d860 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
9d870 65 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 20  e etPERCENT:.   
9d880 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 25       buf[0] = '%
9d890 27 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74  ';.        bufpt
9d8a0 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20   = buf;.        
9d8b0 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 20  length = 1;.    
9d8c0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
9d8d0 20 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20   case etCHARX:. 
9d8e0 20 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 72         c = va_ar
9d8f0 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20  g(ap,int);.     
9d900 20 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 61     buf[0] = (cha
9d910 72 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28  r)c;.        if(
9d920 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b   precision>=0 ){
9d930 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69  .          for(i
9d940 64 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 73  dx=1; idx<precis
9d950 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 5b  ion; idx++) buf[
9d960 69 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a  idx] = (char)c;.
9d970 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
9d980 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20   = precision;.  
9d990 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
9d9a0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31         length =1
9d9b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
9d9c0 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b      bufpt = buf;
9d9d0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
9d9e0 20 20 20 20 20 20 63 61 73 65 20 65 74 53 54 52        case etSTR
9d9f0 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ING:.      case 
9da00 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20  etDYNSTRING:.   
9da10 20 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f       bufpt = va_
9da20 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20  arg(ap,char*);. 
9da30 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74         if( bufpt
9da40 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
9da50 20 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20   bufpt = "";.   
9da60 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78       }else if( x
9da70 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e  type==etDYNSTRIN
9da80 47 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  G ){.          z
9da90 45 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20  Extra = bufpt;. 
9daa0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
9dab0 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d   if( precision>=
9dac0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  0 ){.          f
9dad0 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 6e  or(length=0; len
9dae0 67 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 26  gth<precision &&
9daf0 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b 20   bufpt[length]; 
9db00 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 20  length++){}.    
9db10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9db20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71       length = sq
9db30 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75  lite3Strlen30(bu
9db40 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  fpt);.        }.
9db50 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
9db60 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45       case etSQLE
9db70 53 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73  SCAPE:.      cas
9db80 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a  e etSQLESCAPE2:.
9db90 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c        case etSQL
9dba0 45 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20  ESCAPE3: {.     
9dbb0 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20     int i, j, n, 
9dbc0 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  isnull;.        
9dbd0 69 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20  int needQuote;. 
9dbe0 20 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a         char ch;.
9dbf0 20 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d          char q =
9dc00 20 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45   ((xtype==etSQLE
9dc10 53 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27  SCAPE3)?'"':'\''
9dc20 29 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68  );   /* Quote ch
9dc30 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20  aracter */.     
9dc40 20 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20     char *escarg 
9dc50 3d 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72  = va_arg(ap,char
9dc60 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75  *);.        isnu
9dc70 6c 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a  ll = escarg==0;.
9dc80 20 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75          if( isnu
9dc90 6c 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78  ll ) escarg = (x
9dca0 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50  type==etSQLESCAP
9dcb0 45 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28  E2 ? "NULL" : "(
9dcc0 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20  NULL)");.       
9dcd0 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d   for(i=n=0; (ch=
9dce0 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69  escarg[i])!=0; i
9dcf0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
9dd00 66 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b  f( ch==q )  n++;
9dd10 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
9dd20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21     needQuote = !
9dd30 69 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d  isnull && xtype=
9dd40 3d 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20  =etSQLESCAPE2;. 
9dd50 20 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20         n += i + 
9dd60 31 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b  1 + needQuote*2;
9dd70 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65  .        if( n>e
9dd80 74 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20  tBUFSIZE ){.    
9dd90 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45        bufpt = zE
9dda0 78 74 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61  xtra = sqlite3Ma
9ddb0 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20  lloc( n );.     
9ddc0 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d       if( bufpt==
9ddd0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
9dde0 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46   pAccum->mallocF
9ddf0 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
9de00 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
9de10 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
9de20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
9de30 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b      bufpt = buf;
9de40 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
9de50 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20     j = 0;.      
9de60 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20    if( needQuote 
9de70 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71  ) bufpt[j++] = q
9de80 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
9de90 30 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d  0; (ch=escarg[i]
9dea0 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
9deb0 20 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d        bufpt[j++]
9dec0 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20   = ch;.         
9ded0 20 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66   if( ch==q ) buf
9dee0 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20  pt[j++] = ch;.  
9def0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
9df00 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20  if( needQuote ) 
9df10 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a  bufpt[j++] = q;.
9df20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d          bufpt[j]
9df30 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65   = 0;.        le
9df40 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20  ngth = j;.      
9df50 20 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69    /* The precisi
9df60 6f 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e  on is ignored on
9df70 20 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20   %q and %Q */.  
9df80 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65        /* if( pre
9df90 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65  cision>=0 && pre
9dfa0 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20  cision<length ) 
9dfb0 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69  length = precisi
9dfc0 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62  on; */.        b
9dfd0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
9dfe0 20 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e      case etTOKEN
9dff0 3a 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65  : {.        Toke
9e000 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61  n *pToken = va_a
9e010 72 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  rg(ap, Token*);.
9e020 20 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b          if( pTok
9e030 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  en ){.          
9e040 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
9e050 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63  ppend(pAccum, (c
9e060 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65  onst char*)pToke
9e070 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29  n->z, pToken->n)
9e080 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
9e090 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64      length = wid
9e0a0 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  th = 0;.        
9e0b0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
9e0c0 20 20 20 20 20 63 61 73 65 20 65 74 53 52 43 4c       case etSRCL
9e0d0 49 53 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53  IST: {.        S
9e0e0 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76  rcList *pSrc = v
9e0f0 61 5f 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73  a_arg(ap, SrcLis
9e100 74 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  t*);.        int
9e110 20 6b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20   k = va_arg(ap, 
9e120 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74  int);.        st
9e130 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
9e140 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63  m *pItem = &pSrc
9e150 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20  ->a[k];.        
9e160 61 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20  assert( k>=0 && 
9e170 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a  k<pSrc->nSrc );.
9e180 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65          if( pIte
9e190 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a  m->zDatabase ){.
9e1a0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
9e1b0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
9e1c0 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a  pAccum, pItem->z
9e1d0 44 61 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20  Database, -1);. 
9e1e0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
9e1f0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
9e200 41 63 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a  Accum, ".", 1);.
9e210 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
9e220 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
9e230 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
9e240 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31  pItem->zName, -1
9e250 29 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74  );.        lengt
9e260 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20  h = width = 0;. 
9e270 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
9e280 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61      }.      defa
9e290 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 61  ult: {.        a
9e2a0 73 73 65 72 74 28 20 78 74 79 70 65 3d 3d 65 74  ssert( xtype==et
9e2b0 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20  INVALID );.     
9e2c0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
9e2d0 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73   }.    }/* End s
9e2e0 77 69 74 63 68 20 6f 76 65 72 20 74 68 65 20 66  witch over the f
9e2f0 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 20  ormat type */.  
9e300 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    /*.    ** The 
9e310 74 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76  text of the conv
9e320 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65  ersion is pointe
9e330 64 20 74 6f 20 62 79 20 22 62 75 66 70 74 22 20  d to by "bufpt" 
9e340 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 6c  and is.    ** "l
9e350 65 6e 67 74 68 22 20 63 68 61 72 61 63 74 65 72  ength" character
9e360 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65  s long.  The fie
9e370 6c 64 20 77 69 64 74 68 20 69 73 20 22 77 69 64  ld width is "wid
9e380 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a 20  th".  Do.    ** 
9e390 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20  the output..    
9e3a0 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 6c 61 67  */.    if( !flag
9e3b0 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a  _leftjustify ){.
9e3c0 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69        register i
9e3d0 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20  nt nspace;.     
9e3e0 20 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d   nspace = width-
9e3f0 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66  length;.      if
9e400 28 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20  ( nspace>0 ){.  
9e410 20 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63        appendSpac
9e420 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65  e(pAccum, nspace
9e430 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
9e440 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e  .    if( length>
9e450 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
9e460 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
9e470 28 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20  (pAccum, bufpt, 
9e480 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20  length);.    }. 
9e490 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74     if( flag_left
9e4a0 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20  justify ){.     
9e4b0 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73   register int ns
9e4c0 70 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61  pace;.      nspa
9e4d0 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74  ce = width-lengt
9e4e0 68 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70  h;.      if( nsp
9e4f0 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ace>0 ){.       
9e500 20 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 63   appendSpace(pAc
9e510 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20  cum, nspace);.  
9e520 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
9e530 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 20  if( zExtra ){.  
9e540 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
9e550 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a  (zExtra);.    }.
9e560 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f    }/* End for lo
9e570 6f 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d  op over the form
9e580 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f  at string */.} /
9e590 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f  * End of functio
9e5a0 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65  n */../*.** Appe
9e5b0 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 20 74 65  nd N bytes of te
9e5c0 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65  xt from z to the
9e5d0 20 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74   StrAccum object
9e5e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
9e5f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
9e600 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53  StrAccumAppend(S
9e610 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73  trAccum *p, cons
9e620 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e  t char *z, int N
9e630 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d  ){.  assert( z!=
9e640 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0a 20 20 69  0 || N==0 );.  i
9e650 66 28 20 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70  f( p->tooBig | p
9e660 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
9e670 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 70  {.    testcase(p
9e680 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 20 20 20 74  ->tooBig);.    t
9e690 65 73 74 63 61 73 65 28 70 2d 3e 6d 61 6c 6c 6f  estcase(p->mallo
9e6a0 63 46 61 69 6c 65 64 29 3b 0a 20 20 20 20 72 65  cFailed);.    re
9e6b0 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  turn;.  }.  if( 
9e6c0 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73  N<0 ){.    N = s
9e6d0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
9e6e0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d  );.  }.  if( N==
9e6f0 30 20 7c 7c 20 4e 45 56 45 52 28 7a 3d 3d 30 29  0 || NEVER(z==0)
9e700 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
9e710 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 43 68    }.  if( p->nCh
9e720 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f  ar+N >= p->nAllo
9e730 63 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  c ){.    char *z
9e740 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 21 70 2d  New;.    if( !p-
9e750 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20  >useMalloc ){.  
9e760 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20      p->tooBig = 
9e770 31 3b 0a 20 20 20 20 20 20 4e 20 3d 20 70 2d 3e  1;.      N = p->
9e780 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e 6e 43 68 61  nAlloc - p->nCha
9e790 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 69 66 28  r - 1;.      if(
9e7a0 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   N<=0 ){.       
9e7b0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
9e7c0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
9e7d0 20 20 69 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d    i64 szNew = p-
9e7e0 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 20 73 7a  >nChar;.      sz
9e7f0 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20  New += N + 1;.  
9e800 20 20 20 20 69 66 28 20 73 7a 4e 65 77 20 3e 20      if( szNew > 
9e810 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20  p->mxAlloc ){.  
9e820 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
9e830 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20  AccumReset(p);. 
9e840 20 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67         p->tooBig
9e850 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65   = 1;.        re
9e860 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73  turn;.      }els
9e870 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 41  e{.        p->nA
9e880 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65  lloc = (int)szNe
9e890 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  w;.      }.     
9e8a0 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   zNew = sqlite3D
9e8b0 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62  bMallocRaw(p->db
9e8c0 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  , p->nAlloc );. 
9e8d0 20 20 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b       if( zNew ){
9e8e0 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
9e8f0 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c 20  zNew, p->zText, 
9e900 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20  p->nChar);.     
9e910 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
9e920 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20  umReset(p);.    
9e930 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a      p->zText = z
9e940 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  New;.      }else
9e950 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c  {.        p->mal
9e960 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
9e970 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
9e980 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a  rAccumReset(p);.
9e990 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
9e9a0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
9e9b0 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a  }.  memcpy(&p->z
9e9c0 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 2c 20  Text[p->nChar], 
9e9d0 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 68 61  z, N);.  p->nCha
9e9e0 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  r += N;.}../*.**
9e9f0 20 46 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74   Finish off a st
9ea00 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73  ring by making s
9ea10 75 72 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74  ure it is zero-t
9ea20 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 52 65  erminated..** Re
9ea30 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
9ea40 6f 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20  o the resulting 
9ea50 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20  string.  Return 
9ea60 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65  a NULL.** pointe
9ea70 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66  r if any kind of
9ea80 20 65 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75   error was encou
9ea90 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ntered..*/.SQLIT
9eaa0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
9eab0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
9eac0 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d 20 2a  inish(StrAccum *
9ead0 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65  p){.  if( p->zTe
9eae0 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 54 65  xt ){.    p->zTe
9eaf0 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d 20 30  xt[p->nChar] = 0
9eb00 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65  ;.    if( p->use
9eb10 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a 54 65  Malloc && p->zTe
9eb20 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a  xt==p->zBase ){.
9eb30 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d        p->zText =
9eb40 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
9eb50 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 43  Raw(p->db, p->nC
9eb60 68 61 72 2b 31 20 29 3b 0a 20 20 20 20 20 20 69  har+1 );.      i
9eb70 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20  f( p->zText ){. 
9eb80 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d         memcpy(p-
9eb90 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65  >zText, p->zBase
9eba0 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0a 20  , p->nChar+1);. 
9ebb0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
9ebc0 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69      p->mallocFai
9ebd0 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  led = 1;.      }
9ebe0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
9ebf0 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a 7d 0a  urn p->zText;.}.
9ec00 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 61 6e 20  ./*.** Reset an 
9ec10 53 74 72 41 63 63 75 6d 20 73 74 72 69 6e 67 2e  StrAccum string.
9ec20 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 61    Reclaim all ma
9ec30 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a  lloced memory..*
9ec40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9ec50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
9ec60 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63  AccumReset(StrAc
9ec70 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  cum *p){.  if( p
9ec80 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 61 73  ->zText!=p->zBas
9ec90 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
9eca0 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  DbFree(p->db, p-
9ecb0 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a 20 20 70  >zText);.  }.  p
9ecc0 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 7d 0a 0a  ->zText = 0;.}..
9ecd0 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
9ece0 20 61 20 73 74 72 69 6e 67 20 61 63 63 75 6d 75   a string accumu
9ecf0 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lator.*/.SQLITE_
9ed00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
9ed10 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
9ed20 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 68  (StrAccum *p, ch
9ed30 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e  ar *zBase, int n
9ed40 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e  , int mx){.  p->
9ed50 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65  zText = p->zBase
9ed60 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 64   = zBase;.  p->d
9ed70 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 68 61  b = 0;.  p->nCha
9ed80 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c  r = 0;.  p->nAll
9ed90 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41  oc = n;.  p->mxA
9eda0 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e  lloc = mx;.  p->
9edb0 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20  useMalloc = 1;. 
9edc0 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a   p->tooBig = 0;.
9edd0 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65    p->mallocFaile
9ede0 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d = 0;.}../*.** 
9edf0 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
9ee00 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
9ee10 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20  sqliteMalloc(). 
9ee20 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61   Use the interna
9ee30 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f  l.** %-conversio
9ee40 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f  n extensions..*/
9ee50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9ee60 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50  char *sqlite3VMP
9ee70 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64  rintf(sqlite3 *d
9ee80 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
9ee90 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20  Format, va_list 
9eea0 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  ap){.  char *z;.
9eeb0 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c    char zBase[SQL
9eec0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
9eed0 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20  ZE];.  StrAccum 
9eee0 61 63 63 3b 0a 20 20 61 73 73 65 72 74 28 20 64  acc;.  assert( d
9eef0 62 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65  b!=0 );.  sqlite
9ef00 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61  3StrAccumInit(&a
9ef10 63 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f  cc, zBase, sizeo
9ef20 66 28 7a 42 61 73 65 29 2c 0a 20 20 20 20 20 20  f(zBase),.      
9ef30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ef40 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
9ef50 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 29  E_LIMIT_LENGTH])
9ef60 3b 0a 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b  ;.  acc.db = db;
9ef70 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  .  sqlite3VXPrin
9ef80 74 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72  tf(&acc, 1, zFor
9ef90 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20  mat, ap);.  z = 
9efa0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
9efb0 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69  inish(&acc);.  i
9efc0 66 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69  f( acc.mallocFai
9efd0 6c 65 64 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  led ){.    db->m
9efe0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
9eff0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b  .  }.  return z;
9f000 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  .}../*.** Print 
9f010 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
9f020 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
9f030 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74  Malloc().  Use t
9f040 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25  he internal.** %
9f050 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65  -conversion exte
9f060 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  nsions..*/.SQLIT
9f070 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
9f080 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73  sqlite3MPrintf(s
9f090 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
9f0a0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
9f0b0 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
9f0c0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a   ap;.  char *z;.
9f0d0 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
9f0e0 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73  Format);.  z = s
9f0f0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64  qlite3VMPrintf(d
9f100 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
9f110 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
9f120 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a   return z;.}../*
9f130 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65 33  .** Like sqlite3
9f140 4d 50 72 69 6e 74 66 28 29 2c 20 62 75 74 20 63  MPrintf(), but c
9f150 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65  all sqlite3DbFre
9f160 65 28 29 20 6f 6e 20 7a 53 74 72 20 61 66 74 65  e() on zStr afte
9f170 72 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20  r formatting.** 
9f180 74 68 65 20 73 74 72 69 6e 67 20 61 6e 64 20 62  the string and b
9f190 65 66 6f 72 65 20 72 65 74 75 72 6e 6e 69 6e 67  efore returnning
9f1a0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
9f1b0 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  is intended to b
9f1c0 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64  e used.** to mod
9f1d0 69 66 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20  ify an existing 
9f1e0 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 65 78 61  string.  For exa
9f1f0 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple:.**.**     
9f200 20 20 78 20 3d 20 73 71 6c 69 74 65 33 4d 50 72    x = sqlite3MPr
9f210 69 6e 74 66 28 64 62 2c 20 78 2c 20 22 70 72 65  intf(db, x, "pre
9f220 66 69 78 20 25 73 20 73 75 66 66 69 78 22 2c 20  fix %s suffix", 
9f230 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  x);.**.*/.SQLITE
9f240 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
9f250 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73  qlite3MAppendf(s
9f260 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72  qlite3 *db, char
9f270 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 74 20 63 68   *zStr, const ch
9f280 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
9f290 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
9f2a0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61  .  char *z;.  va
9f2b0 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
9f2c0 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74  at);.  z = sqlit
9f2d0 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a  e3VMPrintf(db, z
9f2e0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
9f2f0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c  a_end(ap);.  sql
9f300 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
9f310 53 74 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  Str);.  return z
9f320 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74  ;.}../*.** Print
9f330 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74   into memory obt
9f340 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
9f350 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d  e3_malloc().  Om
9f360 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  it the internal.
9f370 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20  ** %-conversion 
9f380 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
9f390 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
9f3a0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66  sqlite3_vmprintf
9f3b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f  (const char *zFo
9f3c0 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70  rmat, va_list ap
9f3d0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  ){.  char *z;.  
9f3e0 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54  char zBase[SQLIT
9f3f0 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45  E_PRINT_BUF_SIZE
9f400 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63  ];.  StrAccum ac
9f410 63 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  c;.#ifndef SQLIT
9f420 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
9f430 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
9f440 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74  itialize() ) ret
9f450 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 0;.#endif.  
9f460 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49  sqlite3StrAccumI
9f470 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c  nit(&acc, zBase,
9f480 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20   sizeof(zBase), 
9f490 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
9f4a0 48 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50  H);.  sqlite3VXP
9f4b0 72 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a  rintf(&acc, 0, z
9f4c0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a  Format, ap);.  z
9f4d0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63   = sqlite3StrAcc
9f4e0 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a  umFinish(&acc);.
9f4f0 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f    return z;.}../
9f500 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20  *.** Print into 
9f510 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
9f520 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
9f530 6c 6f 63 28 29 28 29 2e 20 20 4f 6d 69 74 20 74  loc()().  Omit t
9f540 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25  he internal.** %
9f550 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65  -conversion exte
9f560 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  nsions..*/.SQLIT
9f570 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
9f580 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73  te3_mprintf(cons
9f590 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
9f5a0 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
9f5b0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a   ap;.  char *z;.
9f5c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
9f5d0 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69  MIT_AUTOINIT.  i
9f5e0 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  f( sqlite3_initi
9f5f0 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e  alize() ) return
9f600 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 76 61 5f   0;.#endif.  va_
9f610 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
9f620 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65  t);.  z = sqlite
9f630 33 5f 76 6d 70 72 69 6e 74 66 28 7a 46 6f 72 6d  3_vmprintf(zForm
9f640 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
9f650 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  d(ap);.  return 
9f660 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69  z;.}../*.** sqli
9f670 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 77  te3_snprintf() w
9f680 6f 72 6b 73 20 6c 69 6b 65 20 73 6e 70 72 69 6e  orks like snprin
9f690 74 66 28 29 20 65 78 63 65 70 74 20 74 68 61 74  tf() except that
9f6a0 20 69 74 20 69 67 6e 6f 72 65 73 20 74 68 65 0a   it ignores the.
9f6b0 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c  ** current local
9f6c0 65 20 73 65 74 74 69 6e 67 73 2e 20 20 54 68 69  e settings.  Thi
9f6d0 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66  s is important f
9f6e0 6f 72 20 53 51 4c 69 74 65 20 62 65 63 61 75 73  or SQLite becaus
9f6f0 65 20 77 65 0a 2a 2a 20 61 72 65 20 6e 6f 74 20  e we.** are not 
9f700 61 62 6c 65 20 74 6f 20 75 73 65 20 61 20 22 2c  able to use a ",
9f710 22 20 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c  " as the decimal
9f720 20 70 6f 69 6e 74 20 69 6e 20 70 6c 61 63 65 20   point in place 
9f730 6f 66 20 22 2e 22 20 61 73 0a 2a 2a 20 73 70 65  of "." as.** spe
9f740 63 69 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c  cified by some l
9f750 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ocales..*/.SQLIT
9f760 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
9f770 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74  te3_snprintf(int
9f780 20 6e 2c 20 63 68 61 72 20 2a 7a 42 75 66 2c 20   n, char *zBuf, 
9f790 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
9f7a0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63 68 61  mat, ...){.  cha
9f7b0 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73 74 20  r *z;.  va_list 
9f7c0 61 70 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61  ap;.  StrAccum a
9f7d0 63 63 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d 30 20  cc;..  if( n<=0 
9f7e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 42  ){.    return zB
9f7f0 75 66 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  uf;.  }.  sqlite
9f800 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61  3StrAccumInit(&a
9f810 63 63 2c 20 7a 42 75 66 2c 20 6e 2c 20 30 29 3b  cc, zBuf, n, 0);
9f820 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63  .  acc.useMalloc
9f830 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74   = 0;.  va_start
9f840 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  (ap,zFormat);.  
9f850 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28  sqlite3VXPrintf(
9f860 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74  &acc, 0, zFormat
9f870 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
9f880 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74  ap);.  z = sqlit
9f890 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68  e3StrAccumFinish
9f8a0 28 26 61 63 63 29 3b 0a 20 20 72 65 74 75 72 6e  (&acc);.  return
9f8b0 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e   z;.}..#if defin
9f8c0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
9f8d0 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e  ./*.** A version
9f8e0 20 6f 66 20 70 72 69 6e 74 66 28 29 20 74 68 61   of printf() tha
9f8f0 74 20 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c  t understands %l
9f900 6c 64 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65  ld.  Used for de
9f910 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20  bugging..** The 
9f920 70 72 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69  printf() built i
9f930 6e 74 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e  nto some version
9f940 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65  s of windows doe
9f950 73 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64  s not understand
9f960 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67   %lld.** and seg
9f970 66 61 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69  faults if you gi
9f980 76 65 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e  ve it a long lon
9f990 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  g int..*/.SQLITE
9f9a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
9f9b0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
9f9c0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f  (const char *zFo
9f9d0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
9f9e0 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41  _list ap;.  StrA
9f9f0 63 63 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72  ccum acc;.  char
9fa00 20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71   zBuf[500];.  sq
9fa10 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
9fa20 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69  t(&acc, zBuf, si
9fa30 7a 65 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a  zeof(zBuf), 0);.
9fa40 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20    acc.useMalloc 
9fa50 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28  = 0;.  va_start(
9fa60 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73  ap,zFormat);.  s
9fa70 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26  qlite3VXPrintf(&
9fa80 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c  acc, 0, zFormat,
9fa90 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
9faa0 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72  p);.  sqlite3Str
9fab0 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63  AccumFinish(&acc
9fac0 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 73 74 64  );.  fprintf(std
9fad0 6f 75 74 2c 22 25 73 22 2c 20 7a 42 75 66 29 3b  out,"%s", zBuf);
9fae0 0a 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74  .  fflush(stdout
9faf0 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  );.}.#endif../**
9fb00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
9fb10 20 6f 66 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a   of printf.c ***
9fb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
9fb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
9fb60 69 6e 20 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63  in file random.c
9fb70 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
9fb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
9fba0 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
9fbb0 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
9fbc0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
9fbd0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
9fbe0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
9fbf0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
9fc00 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
9fc10 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
9fc20 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
9fc30 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
9fc40 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
9fc50 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
9fc60 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
9fc70 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
9fc80 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
9fc90 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
9fca0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
9fcb0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
9fcc0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
9fcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fd10 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
9fd20 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74   contains code t
9fd30 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 70 73  o implement a ps
9fd40 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62  eudo-random numb
9fd50 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72 20  er.** generator 
9fd60 28 50 52 4e 47 29 20 66 6f 72 20 53 51 4c 69 74  (PRNG) for SQLit
9fd70 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20  e..**.** Random 
9fd80 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64  numbers are used
9fd90 20 62 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20   by some of the 
9fda0 64 61 74 61 62 61 73 65 20 62 61 63 6b 65 6e 64  database backend
9fdb0 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f  s in order.** to
9fdc0 20 67 65 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d   generate random
9fdd0 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 66 6f   integer keys fo
9fde0 72 20 74 61 62 6c 65 73 20 6f 72 20 72 61 6e 64  r tables or rand
9fdf0 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2a  om filenames..**
9fe00 0a 2a 2a 20 24 49 64 3a 20 72 61 6e 64 6f 6d 2e  .** $Id: random.
9fe10 63 2c 76 20 31 2e 32 39 20 32 30 30 38 2f 31 32  c,v 1.29 2008/12
9fe20 2f 31 30 20 31 39 3a 32 36 3a 32 34 20 64 72 68  /10 19:26:24 drh
9fe30 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41   Exp $.*/.../* A
9fe40 6c 6c 20 74 68 72 65 61 64 73 20 73 68 61 72 65  ll threads share
9fe50 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d   a single random
9fe60 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f   number generato
9fe70 72 2e 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  r..** This struc
9fe80 74 75 72 65 20 69 73 20 74 68 65 20 63 75 72 72  ture is the curr
9fe90 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68 65  ent state of the
9fea0 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73   generator..*/.s
9feb0 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
9fec0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50   struct sqlite3P
9fed0 72 6e 67 54 79 70 65 20 7b 0a 20 20 75 6e 73 69  rngType {.  unsi
9fee0 67 6e 65 64 20 63 68 61 72 20 69 73 49 6e 69 74  gned char isInit
9fef0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ;          /* Tr
9ff00 75 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65  ue if initialize
9ff10 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  d */.  unsigned 
9ff20 63 68 61 72 20 69 2c 20 6a 3b 20 20 20 20 20 20  char i, j;      
9ff30 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76        /* State v
9ff40 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 6e  ariables */.  un
9ff50 73 69 67 6e 65 64 20 63 68 61 72 20 73 5b 32 35  signed char s[25
9ff60 36 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  6];          /* 
9ff70 53 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 20  State variables 
9ff80 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e 67  */.} sqlite3Prng
9ff90 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 73  ;../*.** Get a s
9ffa0 69 6e 67 6c 65 20 38 2d 62 69 74 20 72 61 6e 64  ingle 8-bit rand
9ffb0 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68  om value from th
9ffc0 65 20 52 43 34 20 50 52 4e 47 2e 20 20 54 68 65  e RC4 PRNG.  The
9ffd0 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 74 20 62   Mutex.** must b
9ffe0 65 20 68 65 6c 64 20 77 68 69 6c 65 20 65 78 65  e held while exe
9fff0 63 75 74 69 6e 67 20 74 68 69 73 20 72 6f 75 74  cuting this rout
a0000 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20 6e  ine..**.** Why n
a0010 6f 74 20 6a 75 73 74 20 75 73 65 20 61 20 6c 69  ot just use a li
a0020 62 72 61 72 79 20 72 61 6e 64 6f 6d 20 67 65 6e  brary random gen
a0030 65 72 61 74 6f 72 20 6c 69 6b 65 20 6c 72 61 6e  erator like lran
a0040 64 34 38 28 29 20 66 6f 72 20 74 68 69 73 3f 0a  d48() for this?.
a0050 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 4f  ** Because the O
a0060 50 5f 4e 65 77 52 6f 77 69 64 20 6f 70 63 6f 64  P_NewRowid opcod
a0070 65 20 69 6e 20 74 68 65 20 56 44 42 45 20 64 65  e in the VDBE de
a0080 70 65 6e 64 73 20 6f 6e 20 68 61 76 69 6e 67 20  pends on having 
a0090 61 20 76 65 72 79 0a 2a 2a 20 67 6f 6f 64 20 73  a very.** good s
a00a0 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20  ource of random 
a00b0 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 20 6c 72  numbers.  The lr
a00c0 61 6e 64 34 38 28 29 20 6c 69 62 72 61 72 79 20  and48() library 
a00d0 66 75 6e 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20  function may.** 
a00e0 77 65 6c 6c 20 62 65 20 67 6f 6f 64 20 65 6e 6f  well be good eno
a00f0 75 67 68 2e 20 20 42 75 74 20 6d 61 79 62 65 20  ugh.  But maybe 
a0100 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 62 65 20 6c  not.  Or maybe l
a0110 72 61 6e 64 34 38 28 29 20 68 61 73 20 73 6f 6d  rand48() has som
a0120 65 0a 2a 2a 20 73 75 62 74 6c 65 20 70 72 6f 62  e.** subtle prob
a0130 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 73 79 73  lems on some sys
a0140 74 65 6d 73 20 74 68 61 74 20 63 6f 75 6c 64 20  tems that could 
a0150 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 2e 20  cause problems. 
a0160 20 49 74 20 69 73 20 68 61 72 64 0a 2a 2a 20 74   It is hard.** t
a0170 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e 69  o know.  To mini
a0180 6d 69 7a 65 20 74 68 65 20 72 69 73 6b 20 6f 66  mize the risk of
a0190 20 70 72 6f 62 6c 65 6d 73 20 64 75 65 20 74 6f   problems due to
a01a0 20 62 61 64 20 6c 72 61 6e 64 34 38 28 29 0a 2a   bad lrand48().*
a01b0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
a01c0 73 2c 20 53 51 4c 69 74 65 20 75 73 65 73 20 74  s, SQLite uses t
a01d0 68 69 73 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  his random numbe
a01e0 72 20 67 65 6e 65 72 61 74 6f 72 20 62 61 73 65  r generator base
a01f0 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 77 68 69  d.** on RC4, whi
a0200 63 68 20 77 65 20 6b 6e 6f 77 20 77 6f 72 6b 73  ch we know works
a0210 20 76 65 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a   very well..**.*
a0220 2a 20 28 4c 61 74 65 72 29 3a 20 20 41 63 74 75  * (Later):  Actu
a0230 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  ally, OP_NewRowi
a0240 64 20 64 6f 65 73 20 6e 6f 74 20 64 65 70 65 6e  d does not depen
a0250 64 20 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75 72  d on a good sour
a0260 63 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d 6e  ce of.** randomn
a0270 65 73 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20 42  ess any more.  B
a0280 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65  ut we will leave
a0290 20 74 68 69 73 20 63 6f 64 65 20 69 6e 20 61 6c   this code in al
a02a0 6c 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a 73  l the same..*/.s
a02b0 74 61 74 69 63 20 75 38 20 72 61 6e 64 6f 6d 42  tatic u8 randomB
a02c0 79 74 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73  yte(void){.  uns
a02d0 69 67 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a 0a  igned char t;...
a02e0 20 20 2f 2a 20 54 68 65 20 22 77 73 64 50 72 6e    /* The "wsdPrn
a02f0 67 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65  g" macro will re
a0300 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 70 73 65  solve to the pse
a0310 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  udo-random numbe
a0320 72 20 67 65 6e 65 72 61 74 6f 72 0a 20 20 2a 2a  r generator.  **
a0330 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20   state vector.  
a0340 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74  If writable stat
a0350 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70  ic data is unsup
a0360 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61  ported on the ta
a0370 72 67 65 74 2c 0a 20 20 2a 2a 20 77 65 20 68 61  rget,.  ** we ha
a0380 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65  ve to locate the
a0390 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74   state vector at
a03a0 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74   run-time.  In t
a03b0 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 20  he more common. 
a03c0 20 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77   ** case where w
a03d0 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64  ritable static d
a03e0 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64  ata is supported
a03f0 2c 20 77 73 64 50 72 6e 67 20 63 61 6e 20 72 65  , wsdPrng can re
a0400 66 65 72 20 64 69 72 65 63 74 6c 79 0a 20 20 2a  fer directly.  *
a0410 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65  * to the "sqlite
a0420 33 50 72 6e 67 22 20 73 74 61 74 65 20 76 65 63  3Prng" state vec
a0430 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f  tor declared abo
a0440 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  ve..  */.#ifdef 
a0450 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a  SQLITE_OMIT_WSD.
a0460 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
a0470 50 72 6e 67 54 79 70 65 20 2a 70 20 3d 20 26 47  PrngType *p = &G
a0480 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c  LOBAL(struct sql
a0490 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71  ite3PrngType, sq
a04a0 6c 69 74 65 33 50 72 6e 67 29 3b 0a 23 20 64 65  lite3Prng);.# de
a04b0 66 69 6e 65 20 77 73 64 50 72 6e 67 20 70 5b 30  fine wsdPrng p[0
a04c0 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ].#else.# define
a04d0 20 77 73 64 50 72 6e 67 20 73 71 6c 69 74 65 33   wsdPrng sqlite3
a04e0 50 72 6e 67 0a 23 65 6e 64 69 66 0a 0a 0a 20 20  Prng.#endif...  
a04f0 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
a0500 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 72  e state of the r
a0510 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
a0520 65 72 61 74 6f 72 20 6f 6e 63 65 2c 0a 20 20 2a  erator once,.  *
a0530 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  * the first time
a0540 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
a0550 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 73 65   called.  The se
a0560 65 64 20 76 61 6c 75 65 20 64 6f 65 73 0a 20 20  ed value does.  
a0570 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 63  ** not need to c
a0580 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 20 6f 66 20  ontain a lot of 
a0590 72 61 6e 64 6f 6d 6e 65 73 73 20 73 69 6e 63 65  randomness since
a05a0 20 77 65 20 61 72 65 20 6e 6f 74 0a 20 20 2a 2a   we are not.  **
a05b0 20 74 72 79 69 6e 67 20 74 6f 20 64 6f 20 73 65   trying to do se
a05c0 63 75 72 65 20 65 6e 63 72 79 70 74 69 6f 6e 20  cure encryption 
a05d0 6f 72 20 61 6e 79 74 68 69 6e 67 20 6c 69 6b 65  or anything like
a05e0 20 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a 0a 20 20   that....  **.  
a05f0 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 6e 20 74 68  ** Nothing in th
a0600 69 73 20 66 69 6c 65 20 6f 72 20 61 6e 79 77 68  is file or anywh
a0610 65 72 65 20 65 6c 73 65 20 69 6e 20 53 51 4c 69  ere else in SQLi
a0620 74 65 20 64 6f 65 73 20 61 6e 79 20 6b 69 6e 64  te does any kind
a0630 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 72 79 70 74   of.  ** encrypt
a0640 69 6f 6e 2e 20 20 54 68 65 20 52 43 34 20 61 6c  ion.  The RC4 al
a0650 67 6f 72 69 74 68 6d 20 69 73 20 62 65 69 6e 67  gorithm is being
a0660 20 75 73 65 64 20 61 73 20 61 20 50 52 4e 47 20   used as a PRNG 
a0670 28 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 0a 20  (pseudo-random. 
a0680 20 2a 2a 20 6e 75 6d 62 65 72 20 67 65 6e 65 72   ** number gener
a0690 61 74 6f 72 29 20 6e 6f 74 20 61 73 20 61 6e 20  ator) not as an 
a06a0 65 6e 63 72 79 70 74 69 6f 6e 20 64 65 76 69 63  encryption devic
a06b0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 77  e..  */.  if( !w
a06c0 73 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 29 7b  sdPrng.isInit ){
a06d0 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
a06e0 63 68 61 72 20 6b 5b 32 35 36 5d 3b 0a 20 20 20  char k[256];.   
a06f0 20 77 73 64 50 72 6e 67 2e 6a 20 3d 20 30 3b 0a   wsdPrng.j = 0;.
a0700 20 20 20 20 77 73 64 50 72 6e 67 2e 69 20 3d 20      wsdPrng.i = 
a0710 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  0;.    sqlite3Os
a0720 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
a0730 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 2c 20  e3_vfs_find(0), 
a0740 32 35 36 2c 20 6b 29 3b 0a 20 20 20 20 66 6f 72  256, k);.    for
a0750 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b  (i=0; i<256; i++
a0760 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67  ){.      wsdPrng
a0770 2e 73 5b 69 5d 20 3d 20 28 75 38 29 69 3b 0a 20  .s[i] = (u8)i;. 
a0780 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30     }.    for(i=0
a0790 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20  ; i<256; i++){. 
a07a0 20 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b       wsdPrng.j +
a07b0 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 2b  = wsdPrng.s[i] +
a07c0 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74 20 3d   k[i];.      t =
a07d0 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72   wsdPrng.s[wsdPr
a07e0 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 77 73 64  ng.j];.      wsd
a07f0 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a  Prng.s[wsdPrng.j
a0800 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d  ] = wsdPrng.s[i]
a0810 3b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e  ;.      wsdPrng.
a0820 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20 7d 0a  s[i] = t;.    }.
a0830 20 20 20 20 77 73 64 50 72 6e 67 2e 69 73 49 6e      wsdPrng.isIn
a0840 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f  it = 1;.  }..  /
a0850 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 64 20 72  * Generate and r
a0860 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 61 6e  eturn single ran
a0870 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a 20 20  dom byte.  */.  
a0880 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a 20 20 74  wsdPrng.i++;.  t
a0890 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64   = wsdPrng.s[wsd
a08a0 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 64 50 72  Prng.i];.  wsdPr
a08b0 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 77 73 64  ng.j += t;.  wsd
a08c0 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69  Prng.s[wsdPrng.i
a08d0 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73  ] = wsdPrng.s[ws
a08e0 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 73 64 50  dPrng.j];.  wsdP
a08f0 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d  rng.s[wsdPrng.j]
a0900 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 77 73 64   = t;.  t += wsd
a0910 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69  Prng.s[wsdPrng.i
a0920 5d 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 50  ];.  return wsdP
a0930 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a 2f 2a 0a  rng.s[t];.}../*.
a0940 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72 61 6e 64  ** Return N rand
a0950 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 53 51 4c  om bytes..*/.SQL
a0960 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
a0970 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
a0980 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 70 42 75  int N, void *pBu
a0990 66 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  f){.  unsigned c
a09a0 68 61 72 20 2a 7a 42 75 66 20 3d 20 70 42 75 66  har *zBuf = pBuf
a09b0 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
a09c0 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65  EADSAFE.  sqlite
a09d0 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d  3_mutex *mutex =
a09e0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
a09f0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
a0a00 53 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a 23 65  STATIC_PRNG);.#e
a0a10 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ndif.  sqlite3_m
a0a20 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
a0a30 29 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20  );.  while( N-- 
a0a40 29 7b 0a 20 20 20 20 2a 28 7a 42 75 66 2b 2b 29  ){.    *(zBuf++)
a0a50 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65 28 29 3b   = randomByte();
a0a60 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
a0a70 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
a0a80 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  );.}..#ifndef SQ
a0a90 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
a0aa0 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 46 6f 72  N_TEST./*.** For
a0ab0 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65   testing purpose
a0ac0 73 2c 20 77 65 20 73 6f 6d 65 74 69 6d 65 73 20  s, we sometimes 
a0ad0 77 61 6e 74 20 74 6f 20 70 72 65 73 65 72 76 65  want to preserve
a0ae0 20 74 68 65 20 73 74 61 74 65 20 6f 66 0a 2a 2a   the state of.**
a0af0 20 50 52 4e 47 20 61 6e 64 20 72 65 73 74 6f 72   PRNG and restor
a0b00 65 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74  e the PRNG to it
a0b10 73 20 73 61 76 65 64 20 73 74 61 74 65 20 61 74  s saved state at
a0b20 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2c 20 6f   a later time, o
a0b30 72 0a 2a 2a 20 74 6f 20 72 65 73 65 74 20 74 68  r.** to reset th
a0b40 65 20 50 52 4e 47 20 74 6f 20 69 74 73 20 69 6e  e PRNG to its in
a0b50 69 74 69 61 6c 20 73 74 61 74 65 2e 20 20 54 68  itial state.  Th
a0b60 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 63 63  ese routines acc
a0b70 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 6f 73 65  omplish.** those
a0b80 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   tasks..**.** Th
a0b90 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  e sqlite3_test_c
a0ba0 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61  ontrol() interfa
a0bb0 63 65 20 63 61 6c 6c 73 20 74 68 65 73 65 20 72  ce calls these r
a0bc0 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 63 6f  outines to.** co
a0bd0 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e 47 2e 0a  ntrol the PRNG..
a0be0 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45  */.static SQLITE
a0bf0 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69  _WSD struct sqli
a0c00 74 65 33 50 72 6e 67 54 79 70 65 20 73 71 6c 69  te3PrngType sqli
a0c10 74 65 33 53 61 76 65 64 50 72 6e 67 3b 0a 53 51  te3SavedPrng;.SQ
a0c20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a0c30 64 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76  d sqlite3PrngSav
a0c40 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20  eState(void){.  
a0c50 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f  memcpy(.    &GLO
a0c60 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74  BAL(struct sqlit
a0c70 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69  e3PrngType, sqli
a0c80 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a 20  te3SavedPrng),. 
a0c90 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63     &GLOBAL(struc
a0ca0 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70  t sqlite3PrngTyp
a0cb0 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2c  e, sqlite3Prng),
a0cc0 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69  .    sizeof(sqli
a0cd0 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a  te3Prng).  );.}.
a0ce0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a0cf0 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52  oid sqlite3PrngR
a0d00 65 73 74 6f 72 65 53 74 61 74 65 28 76 6f 69 64  estoreState(void
a0d10 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20  ){.  memcpy(.   
a0d20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20   &GLOBAL(struct 
a0d30 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c  sqlite3PrngType,
a0d40 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20   sqlite3Prng),. 
a0d50 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63     &GLOBAL(struc
a0d60 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70  t sqlite3PrngTyp
a0d70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 65 64 50  e, sqlite3SavedP
a0d80 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66  rng),.    sizeof
a0d90 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20  (sqlite3Prng).  
a0da0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
a0db0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
a0dc0 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 76  PrngResetState(v
a0dd0 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 41 4c 28 73  oid){.  GLOBAL(s
a0de0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a0df0 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72  gType, sqlite3Pr
a0e00 6e 67 29 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  ng).isInit = 0;.
a0e10 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
a0e20 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
a0e30 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TEST */../******
a0e40 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
a0e50 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a  random.c *******
a0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0e80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
a0e90 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
a0ea0 69 6c 65 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a  ile utf.c ******
a0eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a0ed0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
a0ee0 30 30 34 20 41 70 72 69 6c 20 31 33 0a 2a 2a 0a  004 April 13.**.
a0ef0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
a0f00 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
a0f10 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
a0f20 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
a0f30 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
a0f40 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
a0f50 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
a0f60 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
a0f70 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
a0f80 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
a0f90 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
a0fa0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
a0fb0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
a0fc0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
a0fd0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
a0fe0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
a0ff0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
a1000 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
a1010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
a1050 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
a1060 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20  s routines used 
a1070 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 62 65 74  to translate bet
a1080 77 65 65 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a 20  ween UTF-8, .** 
a1090 55 54 46 2d 31 36 2c 20 55 54 46 2d 31 36 42 45  UTF-16, UTF-16BE
a10a0 2c 20 61 6e 64 20 55 54 46 2d 31 36 4c 45 2e 0a  , and UTF-16LE..
a10b0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 74 66 2e 63  **.** $Id: utf.c
a10c0 2c 76 20 31 2e 37 33 20 32 30 30 39 2f 30 34 2f  ,v 1.73 2009/04/
a10d0 30 31 20 31 38 3a 34 30 3a 33 32 20 64 72 68 20  01 18:40:32 drh 
a10e0 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  Exp $.**.** Note
a10f0 73 20 6f 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a  s on UTF-8:.**.*
a1100 2a 20 20 20 42 79 74 65 2d 30 20 20 20 20 42 79  *   Byte-0    By
a1110 74 65 2d 31 20 20 20 20 42 79 74 65 2d 32 20 20  te-1    Byte-2  
a1120 20 20 42 79 74 65 2d 33 20 20 20 20 56 61 6c 75    Byte-3    Valu
a1130 65 0a 2a 2a 20 20 30 78 78 78 78 78 78 78 20 20  e.**  0xxxxxxx  
a1140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
a1160 30 30 30 30 30 30 30 20 30 30 30 30 30 30 30 30  0000000 00000000
a1170 20 30 78 78 78 78 78 78 78 0a 2a 2a 20 20 31 31   0xxxxxxx.**  11
a1180 30 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78  0yyyyy  10xxxxxx
a1190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a11a0 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20         00000000 
a11b0 30 30 30 30 30 79 79 79 20 79 79 78 78 78 78 78  00000yyy yyxxxxx
a11c0 78 0a 2a 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20  x.**  1110zzzz  
a11d0 31 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78  10yyyyyy  10xxxx
a11e0 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 30  xx             0
a11f0 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79  0000000 zzzzyyyy
a1200 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31   yyxxxxxx.**  11
a1210 31 31 30 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a  110uuu  10uuzzzz
a1220 20 20 31 30 79 79 79 79 79 79 20 20 31 30 78 78    10yyyyyy  10xx
a1230 78 78 78 78 20 20 20 30 30 30 75 75 75 75 75 20  xxxx   000uuuuu 
a1240 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78  zzzzyyyy yyxxxxx
a1250 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73  x.**.**.** Notes
a1260 20 6f 6e 20 55 54 46 2d 31 36 3a 20 20 28 77 69   on UTF-16:  (wi
a1270 74 68 20 77 77 77 77 2b 31 3d 3d 75 75 75 75 75  th wwww+1==uuuuu
a1280 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72  ).**.**      Wor
a1290 64 2d 30 20 20 20 20 20 20 20 20 20 20 20 20 20  d-0             
a12a0 20 20 57 6f 72 64 2d 31 20 20 20 20 20 20 20 20    Word-1        
a12b0 20 20 56 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31    Value.**  1101
a12c0 31 30 77 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20  10ww wwzzzzyy   
a12d0 31 31 30 31 31 31 79 79 20 79 79 78 78 78 78 78  110111yy yyxxxxx
a12e0 78 20 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a  x    000uuuuu zz
a12f0 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a  zzyyyy yyxxxxxx.
a1300 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78  **  zzzzyyyy yyx
a1310 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20  xxxxx           
a1320 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30               000
a1330 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79  00000 zzzzyyyy y
a1340 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a  yxxxxxx.**.**.**
a1350 20 42 4f 4d 20 6f 72 20 42 79 74 65 20 4f 72 64   BOM or Byte Ord
a1360 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20  er Mark:.**     
a1370 30 78 66 66 20 30 78 66 65 20 20 20 6c 69 74 74  0xff 0xfe   litt
a1380 6c 65 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36  le-endian utf-16
a1390 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20   follows.**     
a13a0 30 78 66 65 20 30 78 66 66 20 20 20 62 69 67 2d  0xfe 0xff   big-
a13b0 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f  endian utf-16 fo
a13c0 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a  llows.**.*/./***
a13d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
a13e0 75 64 65 20 76 64 62 65 49 6e 74 2e 68 20 69 6e  ude vdbeInt.h in
a13f0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 75   the middle of u
a1400 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tf.c ***********
a1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
a1420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
a1430 6e 20 66 69 6c 65 20 76 64 62 65 49 6e 74 2e 68  n file vdbeInt.h
a1440 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
a1450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
a1470 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62 65 72  * 2003 September
a1480 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   6.**.** The aut
a1490 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
a14a0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
a14b0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
a14c0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
a14d0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
a14e0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
a14f0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
a1500 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
a1510 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
a1520 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
a1530 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
a1540 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
a1550 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
a1560 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
a1570 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
a1580 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
a1590 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
a15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a15c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a15d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a15e0 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
a15f0 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72   header file for
a1600 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
a1610 74 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20  t is private to 
a1620 74 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20 54 68  the.** VDBE.  Th
a1630 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  is information u
a1640 73 65 64 20 74 6f 20 61 6c 6c 20 62 65 20 61 74  sed to all be at
a1650 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
a1660 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f 75 72 63 65  single.** source
a1670 20 63 6f 64 65 20 66 69 6c 65 20 22 76 64 62 65   code file "vdbe
a1680 2e 63 22 2e 20 20 57 68 65 6e 20 74 68 61 74 20  .c".  When that 
a1690 66 69 6c 65 20 62 65 63 61 6d 65 20 74 6f 6f 20  file became too 
a16a0 62 69 67 20 28 6f 76 65 72 0a 2a 2a 20 36 30 30  big (over.** 600
a16b0 30 20 6c 69 6e 65 73 20 6c 6f 6e 67 29 20 69 74  0 lines long) it
a16c0 20 77 61 73 20 73 70 6c 69 74 20 75 70 20 69 6e   was split up in
a16d0 74 6f 20 73 65 76 65 72 61 6c 20 73 6d 61 6c 6c  to several small
a16e0 65 72 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20  er files and.** 
a16f0 74 68 69 73 20 68 65 61 64 65 72 20 69 6e 66 6f  this header info
a1700 72 6d 61 74 69 6f 6e 20 77 61 73 20 66 61 63 74  rmation was fact
a1710 6f 72 65 64 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20  ored out..**.** 
a1720 24 49 64 3a 20 76 64 62 65 49 6e 74 2e 68 2c 76  $Id: vdbeInt.h,v
a1730 20 31 2e 31 37 30 20 32 30 30 39 2f 30 35 2f 30   1.170 2009/05/0
a1740 34 20 31 31 3a 34 32 3a 33 30 20 64 61 6e 69 65  4 11:42:30 danie
a1750 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
a1760 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e 54  #ifndef _VDBEINT
a1770 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 44 42  _H_.#define _VDB
a1780 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 69  EINT_H_../*.** i
a1790 6e 74 54 6f 4b 65 79 28 29 20 61 6e 64 20 6b 65  ntToKey() and ke
a17a0 79 54 6f 49 6e 74 28 29 20 75 73 65 64 20 74 6f  yToInt() used to
a17b0 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 72   transform the r
a17c0 6f 77 69 64 2e 20 20 42 75 74 20 77 69 74 68 0a  owid.  But with.
a17d0 2a 2a 20 74 68 65 20 6c 61 74 65 73 74 20 76 65  ** the latest ve
a17e0 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 65  rsions of the de
a17f0 73 69 67 6e 20 74 68 65 79 20 61 72 65 20 6e 6f  sign they are no
a1800 2d 6f 70 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  -ops..*/.#define
a1810 20 6b 65 79 54 6f 49 6e 74 28 58 29 20 20 20 28   keyToInt(X)   (
a1820 58 29 0a 23 64 65 66 69 6e 65 20 69 6e 74 54 6f  X).#define intTo
a1830 4b 65 79 28 58 29 20 20 20 28 58 29 0a 0a 0a 2f  Key(X)   (X).../
a1840 2a 0a 2a 2a 20 53 51 4c 20 69 73 20 74 72 61 6e  *.** SQL is tran
a1850 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 73 65  slated into a se
a1860 71 75 65 6e 63 65 20 6f 66 20 69 6e 73 74 72 75  quence of instru
a1870 63 74 69 6f 6e 73 20 74 6f 20 62 65 0a 2a 2a 20  ctions to be.** 
a1880 65 78 65 63 75 74 65 64 20 62 79 20 61 20 76 69  executed by a vi
a1890 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20  rtual machine.  
a18a0 45 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e  Each instruction
a18b0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a   is an instance.
a18c0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
a18d0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
a18e0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
a18f0 20 56 64 62 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a   VdbeOp Op;../*.
a1900 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76 61 6c 75 65  ** Boolean value
a1910 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73  s.*/.typedef uns
a1920 69 67 6e 65 64 20 63 68 61 72 20 42 6f 6f 6c 3b  igned char Bool;
a1930 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72  ../*.** A cursor
a1940 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 69 6e   is a pointer in
a1950 74 6f 20 61 20 73 69 6e 67 6c 65 20 42 54 72 65  to a single BTre
a1960 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62  e within a datab
a1970 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65  ase file..** The
a1980 20 63 75 72 73 6f 72 20 63 61 6e 20 73 65 65 6b   cursor can seek
a1990 20 74 6f 20 61 20 42 54 72 65 65 20 65 6e 74 72   to a BTree entr
a19a0 79 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  y with a particu
a19b0 6c 61 72 20 6b 65 79 2c 20 6f 72 0a 2a 2a 20 6c  lar key, or.** l
a19c0 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 65 6e 74  oop over all ent
a19d0 72 69 65 73 20 6f 66 20 74 68 65 20 42 74 72 65  ries of the Btre
a19e0 65 2e 20 20 59 6f 75 20 63 61 6e 20 61 6c 73 6f  e.  You can also
a19f0 20 69 6e 73 65 72 74 20 6e 65 77 20 42 54 72 65   insert new BTre
a1a00 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f 72 20  e.** entries or 
a1a10 72 65 74 72 69 65 76 65 20 74 68 65 20 6b 65 79  retrieve the key
a1a20 20 6f 72 20 64 61 74 61 20 66 72 6f 6d 20 74 68   or data from th
a1a30 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65  e entry that the
a1a40 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 63 75   cursor.** is cu
a1a50 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
a1a60 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45 76 65 72   to..** .** Ever
a1a70 79 20 63 75 72 73 6f 72 20 74 68 61 74 20 74 68  y cursor that th
a1a80 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
a1a90 65 20 68 61 73 20 6f 70 65 6e 20 69 73 20 72 65  e has open is re
a1aa0 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e 0a  presented by an.
a1ab0 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
a1ac0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
a1ad0 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  ucture..**.** If
a1ae0 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e   the VdbeCursor.
a1af0 69 73 54 72 69 67 67 65 72 52 6f 77 20 66 6c 61  isTriggerRow fla
a1b00 67 20 69 73 20 73 65 74 20 69 74 20 6d 65 61 6e  g is set it mean
a1b10 73 20 74 68 61 74 20 74 68 69 73 20 63 75 72 73  s that this curs
a1b20 6f 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20  or is.** really 
a1b30 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 74 68 61  a single row tha
a1b40 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65  t represents the
a1b50 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75   NEW or OLD pseu
a1b60 64 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a 20 61  do-table of.** a
a1b70 20 72 6f 77 20 74 72 69 67 67 65 72 2e 20 20 54   row trigger.  T
a1b80 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  he data for the 
a1b90 72 6f 77 20 69 73 20 73 74 6f 72 65 64 20 69 6e  row is stored in
a1ba0 20 56 64 62 65 43 75 72 73 6f 72 2e 70 44 61 74   VdbeCursor.pDat
a1bb0 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 6f 77  a and.** the row
a1bc0 69 64 20 69 73 20 69 6e 20 56 64 62 65 43 75 72  id is in VdbeCur
a1bd0 73 6f 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72  sor.iKey..*/.str
a1be0 75 63 74 20 56 64 62 65 43 75 72 73 6f 72 20 7b  uct VdbeCursor {
a1bf0 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
a1c00 72 73 6f 72 3b 20 20 20 20 2f 2a 20 54 68 65 20  rsor;    /* The 
a1c10 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  cursor structure
a1c20 20 6f 66 20 74 68 65 20 62 61 63 6b 65 6e 64 20   of the backend 
a1c30 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
a1c40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
a1c50 64 65 78 20 6f 66 20 63 75 72 73 6f 72 20 64 61  dex of cursor da
a1c60 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44  tabase in db->aD
a1c70 62 5b 5d 20 28 6f 72 20 2d 31 29 20 2a 2f 0a 20  b[] (or -1) */. 
a1c80 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20   i64 lastRowid; 
a1c90 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72         /* Last r
a1ca0 6f 77 69 64 20 66 72 6f 6d 20 61 20 4e 65 78 74  owid from a Next
a1cb0 20 6f 72 20 4e 65 78 74 49 64 78 20 6f 70 65 72   or NextIdx oper
a1cc0 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 6f 6f 6c 20  ation */.  Bool 
a1cd0 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20 20  zeroed;         
a1ce0 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72 6f   /* True if zero
a1cf0 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64 79  ed out and ready
a1d00 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a 20 20   for reuse */.  
a1d10 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 6c 69  Bool rowidIsVali
a1d20 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  d;    /* True if
a1d30 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 76 61   lastRowid is va
a1d40 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74  lid */.  Bool at
a1d50 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 2f  First;         /
a1d60 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 69  * True if pointi
a1d70 6e 67 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72  ng to first entr
a1d80 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 65 52  y */.  Bool useR
a1d90 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f 2a 20  andomRowid;  /* 
a1da0 47 65 6e 65 72 61 74 65 20 6e 65 77 20 72 65 63  Generate new rec
a1db0 6f 72 64 20 6e 75 6d 62 65 72 73 20 73 65 6d 69  ord numbers semi
a1dc0 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42  -randomly */.  B
a1dd0 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 20 20  ool nullRow;    
a1de0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a1df0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f  pointing to a ro
a1e00 77 20 77 69 74 68 20 6e 6f 20 64 61 74 61 20 2a  w with no data *
a1e10 2f 0a 20 20 42 6f 6f 6c 20 70 73 65 75 64 6f 54  /.  Bool pseudoT
a1e20 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 68 69  able;     /* Thi
a1e30 73 20 69 73 20 61 20 4e 45 57 20 6f 72 20 4f 4c  s is a NEW or OL
a1e40 44 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20  D pseudo-tables 
a1e50 6f 66 20 61 20 74 72 69 67 67 65 72 20 2a 2f 0a  of a trigger */.
a1e60 20 20 42 6f 6f 6c 20 65 70 68 65 6d 50 73 65 75    Bool ephemPseu
a1e70 64 6f 54 61 62 6c 65 3b 0a 20 20 42 6f 6f 6c 20  doTable;.  Bool 
a1e80 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20  deferredMoveto; 
a1e90 20 2f 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71   /* A call to sq
a1ea0 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f  lite3BtreeMoveto
a1eb0 28 29 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a  () is needed */.
a1ec0 20 20 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20    Bool isTable; 
a1ed0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a1ee0 69 66 20 61 20 74 61 62 6c 65 20 72 65 71 75 69  if a table requi
a1ef0 72 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79  ring integer key
a1f00 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 49 6e  s */.  Bool isIn
a1f10 64 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  dex;         /* 
a1f20 54 72 75 65 20 69 66 20 61 6e 20 69 6e 64 65 78  True if an index
a1f30 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79 73   containing keys
a1f40 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20   only - no data 
a1f50 2a 2f 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54  */.  i64 movetoT
a1f60 61 72 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72  arget;     /* Ar
a1f70 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65  gument to the de
a1f80 66 65 72 72 65 64 20 73 71 6c 69 74 65 33 42 74  ferred sqlite3Bt
a1f90 72 65 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0a 20  reeMoveto() */. 
a1fa0 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20   Btree *pBt;    
a1fb0 20 20 20 20 20 20 20 2f 2a 20 53 65 70 61 72 61         /* Separa
a1fc0 74 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20  te file holding 
a1fd0 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
a1fe0 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20  */.  int nData; 
a1ff0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
a2000 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
a2010 20 70 44 61 74 61 20 2a 2f 0a 20 20 63 68 61 72   pData */.  char
a2020 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20   *pData;        
a2030 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 61 20    /* Data for a 
a2040 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64  NEW or OLD pseud
a2050 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 69 36 34  o-table */.  i64
a2060 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20   iKey;          
a2070 20 20 20 2f 2a 20 4b 65 79 20 66 6f 72 20 74 68     /* Key for th
a2080 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65  e NEW or OLD pse
a2090 75 64 6f 2d 74 61 62 6c 65 20 72 6f 77 20 2a 2f  udo-table row */
a20a0 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
a20b0 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f  Info;    /* Info
a20c0 20 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79   about index key
a20d0 73 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65  s needed by inde
a20e0 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69  x cursors */.  i
a20f0 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
a2100 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
a2110 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
a2120 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20  header */.  i64 
a2130 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 20  seqCount;       
a2140 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 6f    /* Sequence co
a2150 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74  unter */.  sqlit
a2160 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
a2170 70 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f 2a  pVtabCursor;  /*
a2180 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20   The cursor for 
a2190 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
a21a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  */.  const sqlit
a21b0 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
a21c0 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c  le;     /* Modul
a21d0 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 74  e for cursor pVt
a21e0 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f  abCursor */..  /
a21f0 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74  * Result of last
a2200 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
a2210 65 74 6f 28 29 20 64 6f 6e 65 20 62 79 20 61 6e  eto() done by an
a2220 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72   OP_NotExists or
a2230 20 0a 20 20 2a 2a 20 4f 50 5f 49 73 55 6e 69 71   .  ** OP_IsUniq
a2240 75 65 20 6f 70 63 6f 64 65 20 6f 6e 20 74 68 69  ue opcode on thi
a2250 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 69  s cursor. */.  i
a2260 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 0a 0a  nt seekResult;..
a2270 20 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f    /* Cached info
a2280 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
a2290 65 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65  e header for the
a22a0 20 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61   data record tha
a22b0 74 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f  t the.  ** curso
a22c0 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  r is currently p
a22d0 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c  ointing to.  Onl
a22e0 79 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65  y valid if cache
a22f0 56 61 6c 69 64 20 69 73 20 74 72 75 65 2e 0a 20  Valid is true.. 
a2300 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70   ** aRow might p
a2310 6f 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72  oint to (ephemer
a2320 61 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65  al) data for the
a2330 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72   current row, or
a2340 20 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62   it might.  ** b
a2350 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69  e NULL..  */.  i
a2360 6e 74 20 63 61 63 68 65 53 74 61 74 75 73 3b 20  nt cacheStatus; 
a2370 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73       /* Cache is
a2380 20 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d   valid if this m
a2390 61 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68  atches Vdbe.cach
a23a0 65 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61  eCtr */.  int pa
a23b0 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20  yloadSize;      
a23c0 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
a23d0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
a23e0 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20  record */.  u32 
a23f0 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20  *aType;         
a2400 20 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73    /* Type values
a2410 20 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73   for all entries
a2420 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
a2430 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74  /.  u32 *aOffset
a2440 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63  ;         /* Cac
a2450 68 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74  hed offsets to t
a2460 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68  he start of each
a2470 20 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f   columns data */
a2480 0a 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20  .  u8 *aRow;    
a2490 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
a24a0 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
a24b0 20 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20   row, if all on 
a24c0 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74  one page */.};.t
a24d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64  ypedef struct Vd
a24e0 62 65 43 75 72 73 6f 72 20 56 64 62 65 43 75 72  beCursor VdbeCur
a24f0 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61  sor;../*.** A va
a2500 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 72 73  lue for VdbeCurs
a2510 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74 68  or.cacheValid th
a2520 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61 63  at means the cac
a2530 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 76  he is always inv
a2540 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  alid..*/.#define
a2550 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a 0a   CACHE_STALE 0..
a2560 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c 79  /*.** Internally
a2570 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69 70  , the vdbe manip
a2580 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61 6c  ulates nearly al
a2590 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73 20  l SQL values as 
a25a0 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  Mem.** structure
a25b0 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72 75  s. Each Mem stru
a25c0 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75 6c  ct may cache mul
a25d0 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74 61  tiple representa
a25e0 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a 2a  tions (string,.*
a25f0 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29 20  * integer etc.) 
a2600 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  of the same valu
a2610 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 6e 64  e.  A value (and
a2620 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d 20 73   therefore Mem s
a2630 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 61 73  tructure).** has
a2640 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70   the following p
a2650 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a  roperties:.**.**
a2660 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 73 20   Each value has 
a2670 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 2e  a manifest type.
a2680 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 74 79   The manifest ty
a2690 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  pe of the value 
a26a0 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 20 4d  stored.** in a M
a26b0 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 65 74  em struct is ret
a26c0 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d 65 6d  urned by the Mem
a26d0 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 72 6f  Type(Mem*) macro
a26e0 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a 2a 2a  . The type is.**
a26f0 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e   one of SQLITE_N
a2700 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45  ULL, SQLITE_INTE
a2710 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 41 4c  GER, SQLITE_REAL
a2720 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 6f 72  , SQLITE_TEXT or
a2730 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f 42 2e  .** SQLITE_BLOB.
a2740 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 20 7b  .*/.struct Mem {
a2750 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69  .  union {.    i
a2760 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  64 i;           
a2770 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
a2780 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  lue. */.    int 
a2790 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20  nZero;          
a27a0 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62 69 74  /* Used when bit
a27b0 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73 65 74   MEM_Zero is set
a27c0 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20   in flags */.   
a27d0 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20   FuncDef *pDef; 
a27e0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c       /* Used onl
a27f0 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45  y when flags==ME
a2800 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 52 6f 77  M_Agg */.    Row
a2810 53 65 74 20 2a 70 52 6f 77 53 65 74 3b 20 20 20  Set *pRowSet;   
a2820 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68   /* Used only wh
a2830 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 52 6f  en flags==MEM_Ro
a2840 77 53 65 74 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20  wSet */.  } u;. 
a2850 20 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20   double r;      
a2860 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
a2870 75 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  ue */.  sqlite3 
a2880 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  *db;        /* T
a2890 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 64 61  he associated da
a28a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
a28b0 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20  n */.  char *z; 
a28c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
a28d0 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c  ring or BLOB val
a28e0 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  ue */.  int n;  
a28f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
a2900 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
a2910 65 72 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61  ers in string va
a2920 6c 75 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27  lue, excluding '
a2930 5c 30 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  \0' */.  u16 fla
a2940 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  gs;          /* 
a2950 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  Some combination
a2960 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45   of MEM_Null, ME
a2970 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20  M_Str, MEM_Dyn, 
a2980 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79  etc. */.  u8  ty
a2990 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  pe;           /*
a29a0 20 4f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e   One of SQLITE_N
a29b0 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54  ULL, SQLITE_TEXT
a29c0 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  , SQLITE_INTEGER
a29d0 2c 20 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65  , etc */.  u8  e
a29e0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nc;            /
a29f0 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  * SQLITE_UTF8, S
a2a00 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
a2a10 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f  QLITE_UTF16LE */
a2a20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
a2a30 76 6f 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20  void *);  /* If 
a2a40 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74  not null, call t
a2a50 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  his function to 
a2a60 64 65 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a  delete Mem.z */.
a2a70 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b    char *zMalloc;
a2a80 20 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63        /* Dynamic
a2a90 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65   buffer allocate
a2aa0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c  d by sqlite3_mal
a2ab0 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  loc() */.};../* 
a2ac0 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  One or more of t
a2ad0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61  he following fla
a2ae0 67 73 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e  gs are set to in
a2af0 64 69 63 61 74 65 20 74 68 65 20 76 61 6c 69 64  dicate the valid
a2b00 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61  OK.** representa
a2b10 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c  tions of the val
a2b20 75 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ue stored in the
a2b30 20 4d 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a   Mem struct..**.
a2b40 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75  ** If the MEM_Nu
a2b50 6c 6c 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  ll flag is set, 
a2b60 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69  then the value i
a2b70 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61  s an SQL NULL va
a2b80 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72  lue..** No other
a2b90 20 66 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65   flags may be se
a2ba0 74 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  t in this case..
a2bb0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d  **.** If the MEM
a2bc0 5f 53 74 72 20 66 6c 61 67 20 69 73 20 73 65 74  _Str flag is set
a2bd0 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e   then Mem.z poin
a2be0 74 73 20 61 74 20 61 20 73 74 72 69 6e 67 20 72  ts at a string r
a2bf0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a  epresentation..*
a2c00 2a 20 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69  * Usually this i
a2c10 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65  s encoded in the
a2c20 20 73 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e   same unicode en
a2c30 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61  coding as the ma
a2c40 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28  in.** database (
a2c50 73 65 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78  see below for ex
a2c60 63 65 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68  ceptions). If th
a2c70 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20  e MEM_Term flag 
a2c80 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20  is also.** set, 
a2c90 74 68 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20  then the string 
a2ca0 69 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65  is nul terminate
a2cb0 64 2e 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61  d. The MEM_Int a
a2cc0 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20  nd MEM_Real .** 
a2cd0 66 6c 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73  flags may coexis
a2ce0 74 20 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53  t with the MEM_S
a2cf0 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d  tr flag..**.** M
a2d00 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65  ultiple of these
a2d10 20 76 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65   values can appe
a2d20 61 72 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e  ar in Mem.flags.
a2d30 20 20 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a    But only one.*
a2d40 2a 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20  * at a time can 
a2d50 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79  appear in Mem.ty
a2d60 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  pe..*/.#define M
a2d70 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30  EM_Null      0x0
a2d80 30 30 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69  001   /* Value i
a2d90 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e  s NULL */.#defin
a2da0 65 20 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20  e MEM_Str       
a2db0 30 78 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75  0x0002   /* Valu
a2dc0 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f  e is a string */
a2dd0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74  .#define MEM_Int
a2de0 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 20         0x0004   
a2df0 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69  /* Value is an i
a2e00 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e  nteger */.#defin
a2e10 65 20 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20  e MEM_Real      
a2e20 30 78 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75  0x0008   /* Valu
a2e30 65 20 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62  e is a real numb
a2e40 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  er */.#define ME
a2e50 4d 5f 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30  M_Blob      0x00
a2e60 31 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  10   /* Value is
a2e70 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 64 65 66 69   a BLOB */.#defi
a2e80 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20  ne MEM_RowSet   
a2e90 20 30 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c   0x0020   /* Val
a2ea0 75 65 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f  ue is a RowSet o
a2eb0 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65  bject */.#define
a2ec0 20 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 20 20 30   MEM_TypeMask  0
a2ed0 78 30 30 66 66 20 20 20 2f 2a 20 4d 61 73 6b 20  x00ff   /* Mask 
a2ee0 6f 66 20 74 79 70 65 20 62 69 74 73 20 2a 2f 0a  of type bits */.
a2ef0 0a 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 6d  ./* Whenever Mem
a2f00 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69   contains a vali
a2f10 64 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  d string or blob
a2f20 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c   representation,
a2f30 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66   one of.** the f
a2f40 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d  ollowing flags m
a2f50 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 65  ust be set to de
a2f60 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f  termine the memo
a2f70 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a  ry management.**
a2f80 20 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e   policy for Mem.
a2f90 7a 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d  z.  The MEM_Term
a2fa0 20 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77   flag tells us w
a2fb0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
a2fc0 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c  e.** string is \
a2fd0 30 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65  000 or \u0000 te
a2fe0 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 66  rminated.*/.#def
a2ff0 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 20  ine MEM_Term    
a3000 20 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53 74    0x0200   /* St
a3010 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c 20  ring rep is nul 
a3020 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 64  terminated */.#d
a3030 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 20  efine MEM_Dyn   
a3040 20 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20      0x0400   /* 
a3050 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c  Need to call sql
a3060 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 6d  iteFree() on Mem
a3070 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  .z */.#define ME
a3080 4d 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 38  M_Static    0x08
a3090 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f  00   /* Mem.z po
a30a0 69 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 63  ints to a static
a30b0 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
a30c0 6e 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 20  ne MEM_Ephem    
a30d0 20 30 78 31 30 30 30 20 20 20 2f 2a 20 4d 65 6d   0x1000   /* Mem
a30e0 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  .z points to an 
a30f0 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67  ephemeral string
a3100 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
a3110 41 67 67 20 20 20 20 20 20 20 30 78 32 30 30 30  Agg       0x2000
a3120 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e     /* Mem.z poin
a3130 74 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 6e  ts to an agg fun
a3140 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f  ction context */
a3150 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72  .#define MEM_Zer
a3160 6f 20 20 20 20 20 20 30 78 34 30 30 30 20 20 20  o      0x4000   
a3170 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 6e  /* Mem.i contain
a3180 73 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 70  s count of 0s ap
a3190 70 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 2a  pended to blob *
a31a0 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
a31b0 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
a31c0 20 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 6f   #undef MEM_Zero
a31d0 0a 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a  .  #define MEM_Z
a31e0 65 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 69  ero 0x0000.#endi
a31f0 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  f.../*.** Clear 
a3200 61 6e 79 20 65 78 69 73 74 69 6e 67 20 74 79 70  any existing typ
a3210 65 20 66 6c 61 67 73 20 66 72 6f 6d 20 61 20 4d  e flags from a M
a3220 65 6d 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74  em and replace t
a3230 68 65 6d 20 77 69 74 68 20 66 0a 2a 2f 0a 23 64  hem with f.*/.#d
a3240 65 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65  efine MemSetType
a3250 46 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 20  Flag(p, f) \.   
a3260 28 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28  ((p)->flags = ((
a3270 70 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f  p)->flags&~(MEM_
a3280 54 79 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72  TypeMask|MEM_Zer
a3290 6f 29 29 7c 66 29 0a 0a 0a 2f 2a 20 41 20 56 64  o))|f).../* A Vd
a32a0 62 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61  beFunc is just a
a32b0 20 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65   FuncDef (define
a32c0 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  d in sqliteInt.h
a32d0 29 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a  ) that contains.
a32e0 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  ** additional in
a32f0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
a3300 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d  auxiliary inform
a3310 61 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61  ation bound to a
a3320 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74  rguments.** of t
a3330 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  he function.  Th
a3340 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d  is is used to im
a3350 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69  plement the sqli
a3360 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
a3370 29 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  ).** and sqlite3
a3380 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41  _set_auxdata() A
a3390 50 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61  PIs.  The "auxda
a33a0 74 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69  ta" is some auxi
a33b0 6c 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68  liary data.** th
a33c0 61 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69  at can be associ
a33d0 61 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73  ated with a cons
a33e0 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  tant argument to
a33f0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68   a function.  Th
a3400 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e  is.** allows fun
a3410 63 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22  ctions such as "
a3420 72 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69  regexp" to compi
a3430 6c 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e  le their constan
a3440 74 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70  t regular.** exp
a3450 72 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74  ression argument
a3460 20 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64   once and reused
a3470 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f   the compiled co
a3480 64 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a  de for multiple.
a3490 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a  ** invocations..
a34a0 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75  */.struct VdbeFu
a34b0 6e 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  nc {.  FuncDef *
a34c0 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20  pFunc;          
a34d0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69       /* The defi
a34e0 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75  nition of the fu
a34f0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nction */.  int 
a3500 6e 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20  nAux;           
a3510 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
a3520 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
a3530 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41  llocated for apA
a3540 75 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74  ux[] */.  struct
a3550 20 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76   AuxData {.    v
a3560 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20  oid *pAux;      
a3570 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a3580 41 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65  Aux data for the
a3590 20 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a   i-th argument *
a35a0 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65  /.    void (*xDe
a35b0 6c 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20  lete)(void *);  
a35c0 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f      /* Destructo
a35d0 72 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61  r for the aux da
a35e0 74 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b  ta */.  } apAux[
a35f0 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  1];             
a3600 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f        /* One slo
a3610 74 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74  t for each funct
a3620 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  ion argument */.
a3630 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63  };../*.** The "c
a3640 6f 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74  ontext" argument
a3650 20 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62   for a installab
a3660 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20  le function.  A 
a3670 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a  pointer to an.**
a3680 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
a3690 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74  s structure is t
a36a0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
a36b0 74 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65  t to the routine
a36c0 73 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d  s used.** implem
a36d0 65 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63  ent the SQL func
a36e0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tions..**.** The
a36f0 72 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20  re is a typedef 
a3700 66 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75  for this structu
a3710 72 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20  re in sqlite.h. 
a3720 20 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73   So all routines
a3730 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75  ,.** even the pu
a3740 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74  blic interface t
a3750 6f 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73  o SQLite, can us
a3760 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
a3770 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
a3780 2a 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20  * But this file 
a3790 69 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63  is the only plac
a37a0 65 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65  e where the inte
a37b0 72 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20  rnal details of 
a37c0 74 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72  this.** structur
a37d0 65 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a  e are known..**.
a37e0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
a37f0 65 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73  e is defined ins
a3800 69 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68  ide of vdbeInt.h
a3810 20 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73   because it uses
a3820 20 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a   substructures.*
a3830 2a 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72  * (Mem) which ar
a3840 65 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74  e only defined t
a3850 68 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  here..*/.struct 
a3860 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
a3870 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75  {.  FuncDef *pFu
a3880 6e 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  nc;       /* Poi
a3890 6e 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e  nter to function
a38a0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d   information.  M
a38b0 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a  UST BE FIRST */.
a38c0 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62    VdbeFunc *pVdb
a38d0 65 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c  eFunc;  /* Auxil
a38e0 61 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65  ary data, if cre
a38f0 61 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73  ated. */.  Mem s
a3900 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a3910 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76   /* The return v
a3920 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68  alue is stored h
a3930 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ere */.  Mem *pM
a3940 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
a3950 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73  * Memory cell us
a3960 65 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72  ed to store aggr
a3970 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f  egate context */
a3980 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b 20  .  int isError; 
a3990 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
a39a0 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20  r code returned 
a39b0 62 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  by the function.
a39c0 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70   */.  CollSeq *p
a39d0 43 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43  Coll;       /* C
a39e0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
a39f0 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
a3a00 20 53 65 74 20 73 74 72 75 63 74 75 72 65 20 69   Set structure i
a3a10 73 20 75 73 65 64 20 66 6f 72 20 71 75 69 63 6b  s used for quick
a3a20 20 74 65 73 74 69 6e 67 20 74 6f 20 73 65 65 20   testing to see 
a3a30 69 66 20 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73  if a value.** is
a3a40 20 70 61 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c   part of a small
a3a50 20 73 65 74 2e 20 20 53 65 74 73 20 61 72 65 20   set.  Sets are 
a3a60 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
a3a70 74 20 63 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74  t code like.** t
a3a80 68 69 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20  his:.**         
a3a90 20 20 20 78 2e 79 20 49 4e 20 28 27 68 69 27 2c     x.y IN ('hi',
a3aa0 27 68 6f 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a  'hoo','hum').*/.
a3ab0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
a3ac0 65 74 20 53 65 74 3b 0a 73 74 72 75 63 74 20 53  et Set;.struct S
a3ad0 65 74 20 7b 0a 20 20 48 61 73 68 20 68 61 73 68  et {.  Hash hash
a3ae0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
a3af0 20 41 20 73 65 74 20 69 73 20 6a 75 73 74 20 61   A set is just a
a3b00 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
a3b10 20 48 61 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b   HashElem *prev;
a3b20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69          /* Previ
a3b30 6f 75 73 6c 79 20 61 63 63 65 73 73 65 64 20 68  ously accessed h
a3b40 61 73 68 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b  ash elemen */.};
a3b50 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f 6e 74 65 78  ../*.** A Contex
a3b60 74 20 73 74 6f 72 65 73 20 74 68 65 20 6c 61 73  t stores the las
a3b70 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 2c 20  t insert rowid, 
a3b80 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65  the last stateme
a3b90 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2c  nt change count,
a3ba0 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 75 72 72  .** and the curr
a3bb0 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68  ent statement ch
a3bc0 61 6e 67 65 20 63 6f 75 6e 74 20 28 69 2e 65 2e  ange count (i.e.
a3bd0 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 6c   changes since l
a3be0 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 2e 0a  ast statement)..
a3bf0 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 6b  ** The current k
a3c00 65 79 6c 69 73 74 20 69 73 20 61 6c 73 6f 20 73  eylist is also s
a3c10 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e  tored in the con
a3c20 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 6d 65 6e 74  text..** Element
a3c30 73 20 6f 66 20 43 6f 6e 74 65 78 74 20 73 74 72  s of Context str
a3c40 75 63 74 75 72 65 20 74 79 70 65 20 6d 61 6b 65  ucture type make
a3c50 20 75 70 20 74 68 65 20 43 6f 6e 74 65 78 74 53   up the ContextS
a3c60 74 61 63 6b 2c 20 77 68 69 63 68 20 69 73 0a 2a  tack, which is.*
a3c70 2a 20 75 70 64 61 74 65 64 20 62 79 20 74 68 65  * updated by the
a3c80 20 43 6f 6e 74 65 78 74 50 75 73 68 20 61 6e 64   ContextPush and
a3c90 20 43 6f 6e 74 65 78 74 50 6f 70 20 6f 70 63 6f   ContextPop opco
a3ca0 64 65 73 20 28 75 73 65 64 20 62 79 20 74 72 69  des (used by tri
a3cb0 67 67 65 72 73 29 2e 0a 2a 2a 20 54 68 65 20 63  ggers)..** The c
a3cc0 6f 6e 74 65 78 74 20 69 73 20 70 75 73 68 65 64  ontext is pushed
a3cd0 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 69 6e   before executin
a3ce0 67 20 61 20 74 72 69 67 67 65 72 20 61 20 70 6f  g a trigger a po
a3cf0 70 70 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a  pped when the.**
a3d00 20 74 72 69 67 67 65 72 20 66 69 6e 69 73 68 65   trigger finishe
a3d10 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
a3d20 72 75 63 74 20 43 6f 6e 74 65 78 74 20 43 6f 6e  ruct Context Con
a3d30 74 65 78 74 3b 0a 73 74 72 75 63 74 20 43 6f 6e  text;.struct Con
a3d40 74 65 78 74 20 7b 0a 20 20 69 36 34 20 6c 61 73  text {.  i64 las
a3d50 74 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 4c 61  tRowid;    /* La
a3d60 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20  st insert rowid 
a3d70 28 73 71 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77  (sqlite3.lastRow
a3d80 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  id) */.  int nCh
a3d90 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a 20 53 74  ange;      /* St
a3da0 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 73 20  atement changes 
a3db0 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 73 29 20  (Vdbe.nChanges) 
a3dc0 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a      */.};../*.**
a3dd0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
a3de0 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
a3df0 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75 63  ine.  This struc
a3e00 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  ture contains th
a3e10 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74  e complete.** st
a3e20 61 74 65 20 6f 66 20 74 68 65 20 76 69 72 74 75  ate of the virtu
a3e30 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a  al machine..**.*
a3e40 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f 73  * The "sqlite3_s
a3e50 74 6d 74 22 20 73 74 72 75 63 74 75 72 65 20 70  tmt" structure p
a3e60 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20 72  ointer that is r
a3e70 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
a3e80 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20  e3_compile().** 
a3e90 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e  is really a poin
a3ea0 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
a3eb0 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
a3ec0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ture..**.** The 
a3ed0 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f  Vdbe.inVtabMetho
a3ee0 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  d variable is se
a3ef0 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f  t to non-zero fo
a3f00 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f  r the duration o
a3f10 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61 6c  f.** any virtual
a3f20 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e   table method in
a3f30 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20 62  vocations made b
a3f40 79 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  y the vdbe progr
a3f50 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65 74  am. It is.** set
a3f60 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74 72   to 2 for xDestr
a3f70 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20  oy method calls 
a3f80 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74  and 1 for all ot
a3f90 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68 69  her methods. Thi
a3fa0 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73  s.** variable is
a3fb0 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70 75   used for two pu
a3fc0 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77  rposes: to allow
a3fd0 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64   xDestroy method
a3fe0 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20  s to execute.** 
a3ff0 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74 61  "DROP TABLE" sta
a4000 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20 70  tements and to p
a4010 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74  revent some nast
a4020 79 20 73 69 64 65 20 65 66 66 65 63 74 73 20 6f  y side effects o
a4030 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  f.** malloc fail
a4040 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65 20  ure when SQLite 
a4050 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75 72  is invoked recur
a4060 73 69 76 65 6c 79 20 62 79 20 61 20 76 69 72 74  sively by a virt
a4070 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65  ual table .** me
a4080 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  thod function..*
a4090 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b 0a  /.struct Vdbe {.
a40a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
a40b0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f        /* The who
a40c0 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  le database */. 
a40d0 20 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e   Vdbe *pPrev,*pN
a40e0 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c  ext; /* Linked l
a40f0 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 69 74  ist of VDBEs wit
a4100 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 65 2e  h the same Vdbe.
a4110 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b  db */.  int nOp;
a4120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
a4130 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63  umber of instruc
a4140 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72 6f  tions in the pro
a4150 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  gram */.  int nO
a4160 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a  pAlloc;       /*
a4170 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   Number of slots
a4180 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61   allocated for a
a4190 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f  Op[] */.  Op *aO
a41a0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p;            /*
a41b0 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
a41c0 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
a41d0 6e 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  ne's program */.
a41e0 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20    int nLabel;   
a41f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
a4200 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 20 2a  of labels used *
a4210 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 41 6c  /.  int nLabelAl
a4220 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65  loc;    /* Numbe
a4230 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
a4240 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b 5d  ated in aLabel[]
a4250 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65   */.  int *aLabe
a4260 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61  l;        /* Spa
a4270 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6c  ce to hold the l
a4280 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a  abels */.  Mem *
a4290 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 2f  *apArg;        /
a42a0 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63  * Arguments to c
a42b0 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69  urrently executi
a42c0 6e 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  ng user function
a42d0 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e   */.  Mem *aColN
a42e0 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ame;      /* Col
a42f0 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65 74  umn names to ret
a4300 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75  urn */.  int nCu
a4310 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  rsor;        /* 
a4320 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20  Number of slots 
a4330 69 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20  in apCsr[] */.  
a4340 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43  VdbeCursor **apC
a4350 73 72 3b 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65  sr; /* One eleme
a4360 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72 61 79  nt of this array
a4370 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63   for each open c
a4380 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  ursor */.  int n
a4390 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Var;           /
a43a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
a43b0 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a 2f  ies in aVar[] */
a43c0 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20 20  .  Mem *aVar;   
a43d0 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73         /* Values
a43e0 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69   for the OP_Vari
a43f0 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a  able opcode. */.
a4400 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20    char **azVar; 
a4410 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
a4420 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20   variables */.  
a4430 69 6e 74 20 6f 6b 56 61 72 3b 20 20 20 20 20 20  int okVar;      
a4440 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
a4450 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e 20  zVar[] has been 
a4460 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
a4470 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20   u32 magic;     
a4480 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67 69           /* Magi
a4490 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 61 6e  c number for san
a44a0 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
a44b0 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20    int nMem;     
a44c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
a44d0 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f  ber of memory lo
a44e0 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c  cations currentl
a44f0 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20  y allocated */. 
a4500 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20   Mem *aMem;     
a4510 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
a4520 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
a4530 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 65 43   */.  int cacheC
a4540 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  tr;           /*
a4550 20 56 64 62 65 43 75 72 73 6f 72 20 72 6f 77 20   VdbeCursor row 
a4560 63 61 63 68 65 20 67 65 6e 65 72 61 74 69 6f 6e  cache generation
a4570 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
a4580 74 20 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f  t contextStackTo
a4590 70 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f  p;    /* Index o
a45a0 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e  f top element in
a45b0 20 74 68 65 20 63 6f 6e 74 65 78 74 20 73 74 61   the context sta
a45c0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6e 74  ck */.  int cont
a45d0 65 78 74 53 74 61 63 6b 44 65 70 74 68 3b 20 20  extStackDepth;  
a45e0 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  /* The size of t
a45f0 68 65 20 22 63 6f 6e 74 65 78 74 22 20 73 74 61  he "context" sta
a4600 63 6b 20 2a 2f 0a 20 20 43 6f 6e 74 65 78 74 20  ck */.  Context 
a4610 2a 63 6f 6e 74 65 78 74 53 74 61 63 6b 3b 20 20  *contextStack;  
a4620 2f 2a 20 53 74 61 63 6b 20 75 73 65 64 20 62 79  /* Stack used by
a4630 20 6f 70 63 6f 64 65 73 20 43 6f 6e 74 65 78 74   opcodes Context
a4640 50 75 73 68 20 26 20 43 6f 6e 74 65 78 74 50 6f  Push & ContextPo
a4650 70 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20  p*/.  int pc;   
a4660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a4670 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75   The program cou
a4680 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63  nter */.  int rc
a4690 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a46a0 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
a46b0 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 65 72  turn */.  int er
a46c0 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20  rorAction;      
a46d0 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63    /* Recovery ac
a46e0 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61  tion to do in ca
a46f0 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a  se of an error *
a4700 2f 0a 20 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75  /.  int nResColu
a4710 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  mn;         /* N
a4720 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
a4730 20 69 6e 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74   in one row of t
a4740 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
a4750 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65 73 43  .  char **azResC
a4760 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 56 61  olumn;     /* Va
a4770 6c 75 65 73 20 66 6f 72 20 6f 6e 65 20 72 6f 77  lues for one row
a4780 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 20 0a 20   of result */ . 
a4790 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20   char *zErrMsg; 
a47a0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
a47b0 72 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65  r message writte
a47c0 6e 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20  n here */.  Mem 
a47d0 2a 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20  *pResultSet;    
a47e0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
a47f0 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65  o an array of re
a4800 73 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 65 78  sults */.  u8 ex
a4810 70 6c 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20  plain;          
a4820 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 45 58     /* True if EX
a4830 50 4c 41 49 4e 20 70 72 65 73 65 6e 74 20 6f 6e  PLAIN present on
a4840 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 2a 2f 0a   SQL command */.
a4850 20 20 75 38 20 63 68 61 6e 67 65 43 6e 74 4f 6e    u8 changeCntOn
a4860 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
a4870 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20  e to update the 
a4880 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a  change-counter *
a4890 2f 0a 20 20 75 38 20 65 78 70 69 72 65 64 3b 20  /.  u8 expired; 
a48a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
a48b0 72 75 65 20 69 66 20 74 68 65 20 56 4d 20 6e 65  rue if the VM ne
a48c0 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70  eds to be recomp
a48d0 69 6c 65 64 20 2a 2f 0a 20 20 75 38 20 6d 69 6e  iled */.  u8 min
a48e0 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b  WriteFileFormat;
a48f0 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 66 69 6c    /* Minimum fil
a4900 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 77 72 69  e format for wri
a4910 74 61 62 6c 65 20 64 61 74 61 62 61 73 65 20 66  table database f
a4920 69 6c 65 73 20 2a 2f 0a 20 20 75 38 20 69 6e 56  iles */.  u8 inV
a4930 74 61 62 4d 65 74 68 6f 64 3b 20 20 20 20 20 20  tabMethod;      
a4940 20 20 2f 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74    /* See comment
a4950 73 20 61 62 6f 76 65 20 2a 2f 0a 20 20 75 38 20  s above */.  u8 
a4960 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b  usesStmtJournal;
a4970 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a4980 75 73 65 73 20 61 20 73 74 61 74 65 6d 65 6e 74  uses a statement
a4990 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38   journal */.  u8
a49a0 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20   readOnly;      
a49b0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
a49c0 72 20 72 65 61 64 2d 6f 6e 6c 79 20 73 74 61 74  r read-only stat
a49d0 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 75 38 20 69  ements */.  u8 i
a49e0 73 50 72 65 70 61 72 65 56 32 3b 20 20 20 20 20  sPrepareV2;     
a49f0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
a4a00 72 65 70 61 72 65 64 20 77 69 74 68 20 70 72 65  repared with pre
a4a10 70 61 72 65 5f 76 32 28 29 20 2a 2f 0a 20 20 69  pare_v2() */.  i
a4a20 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20  nt nChange;     
a4a30 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
a4a40 20 6f 66 20 64 62 20 63 68 61 6e 67 65 73 20 6d   of db changes m
a4a50 61 64 65 20 73 69 6e 63 65 20 6c 61 73 74 20 72  ade since last r
a4a60 65 73 65 74 20 2a 2f 0a 20 20 69 36 34 20 73 74  eset */.  i64 st
a4a70 61 72 74 54 69 6d 65 3b 20 20 20 20 20 20 20 20  artTime;        
a4a80 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 6e 20 71    /* Time when q
a4a90 75 65 72 79 20 73 74 61 72 74 65 64 20 2d 20 75  uery started - u
a4aa0 73 65 64 20 66 6f 72 20 70 72 6f 66 69 6c 69 6e  sed for profilin
a4ab0 67 20 2a 2f 0a 20 20 69 6e 74 20 62 74 72 65 65  g */.  int btree
a4ac0 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 2f  Mask;          /
a4ad0 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 64 62 2d  * Bitmask of db-
a4ae0 3e 61 44 62 5b 5d 20 65 6e 74 72 69 65 73 20 72  >aDb[] entries r
a4af0 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 42  eferenced */.  B
a4b00 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 61  treeMutexArray a
a4b10 4d 75 74 65 78 3b 20 2f 2a 20 41 6e 20 61 72 72  Mutex; /* An arr
a4b20 61 79 20 6f 66 20 42 74 72 65 65 20 75 73 65 64  ay of Btree used
a4b30 20 68 65 72 65 20 61 6e 64 20 6e 65 65 64 69 6e   here and needin
a4b40 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e 74  g locks */.  int
a4b50 20 61 43 6f 75 6e 74 65 72 5b 32 5d 3b 20 20 20   aCounter[2];   
a4b60 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 73       /* Counters
a4b70 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33   used by sqlite3
a4b80 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 29 20 2a  _stmt_status() *
a4b90 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20  /.  char *zSql; 
a4ba0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
a4bb0 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  t of the SQL sta
a4bc0 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65  tement that gene
a4bd0 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 20 20  rated this */.  
a4be0 76 6f 69 64 20 2a 70 46 72 65 65 3b 20 20 20 20  void *pFree;    
a4bf0 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20          /* Free 
a4c00 74 68 69 73 20 77 68 65 6e 20 64 65 6c 65 74 69  this when deleti
a4c10 6e 67 20 74 68 65 20 76 64 62 65 20 2a 2f 0a 23  ng the vdbe */.#
a4c20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
a4c30 55 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65  UG.  FILE *trace
a4c40 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ;          /* Wr
a4c50 69 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f 6e  ite an execution
a4c60 20 74 72 61 63 65 20 68 65 72 65 2c 20 69 66 20   trace here, if 
a4c70 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64  not NULL */.#end
a4c80 69 66 0a 20 20 69 6e 74 20 69 53 74 61 74 65 6d  if.  int iStatem
a4c90 65 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ent;         /* 
a4ca0 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72  Statement number
a4cb0 20 28 6f 72 20 30 20 69 66 20 68 61 73 20 6e 6f   (or 0 if has no
a4cc0 74 20 6f 70 65 6e 65 64 20 73 74 6d 74 29 20 2a  t opened stmt) *
a4cd0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
a4ce0 53 53 45 0a 20 20 69 6e 74 20 66 65 74 63 68 49  SSE.  int fetchI
a4cf0 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  d;          /* S
a4d00 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20  tatement number 
a4d10 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  used by sqlite3_
a4d20 66 65 74 63 68 5f 73 74 61 74 65 6d 65 6e 74 20  fetch_statement 
a4d30 2a 2f 0a 20 20 69 6e 74 20 6c 72 75 3b 20 20 20  */.  int lru;   
a4d40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
a4d50 75 6e 74 65 72 20 75 73 65 64 20 66 6f 72 20 4c  unter used for L
a4d60 52 55 20 63 61 63 68 65 20 72 65 70 6c 61 63 65  RU cache replace
a4d70 6d 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  ment */.#endif.#
a4d80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
a4d90 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
a4da0 45 4d 45 4e 54 0a 20 20 56 64 62 65 20 2a 70 4c  EMENT.  Vdbe *pL
a4db0 72 75 50 72 65 76 3b 0a 20 20 56 64 62 65 20 2a  ruPrev;.  Vdbe *
a4dc0 70 4c 72 75 4e 65 78 74 3b 0a 23 65 6e 64 69 66  pLruNext;.#endif
a4dd0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .};../*.** The f
a4de0 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c  ollowing are all
a4df0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20  owed values for 
a4e00 56 64 62 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64  Vdbe.magic.*/.#d
a4e10 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43  efine VDBE_MAGIC
a4e20 5f 49 4e 49 54 20 20 20 20 20 30 78 32 36 62 63  _INIT     0x26bc
a4e30 65 61 61 35 20 20 20 20 2f 2a 20 42 75 69 6c 64  eaa5    /* Build
a4e40 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72  ing a VDBE progr
a4e50 61 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44  am */.#define VD
a4e60 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 20 20 20  BE_MAGIC_RUN    
a4e70 20 20 30 78 62 64 66 32 30 64 61 33 20 20 20 20    0xbdf20da3    
a4e80 2f 2a 20 56 44 42 45 20 69 73 20 72 65 61 64 79  /* VDBE is ready
a4e90 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 23   to execute */.#
a4ea0 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49  define VDBE_MAGI
a4eb0 43 5f 48 41 4c 54 20 20 20 20 20 30 78 35 31 39  C_HALT     0x519
a4ec0 63 32 39 37 33 20 20 20 20 2f 2a 20 56 44 42 45  c2973    /* VDBE
a4ed0 20 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 20 65   has completed e
a4ee0 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66  xecution */.#def
a4ef0 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 44  ine VDBE_MAGIC_D
a4f00 45 41 44 20 20 20 20 20 30 78 62 36 30 36 63 33  EAD     0xb606c3
a4f10 63 38 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42  c8    /* The VDB
a4f20 45 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c  E has been deall
a4f30 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ocated */../*.**
a4f40 20 46 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74   Function protot
a4f50 79 70 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ypes.*/.SQLITE_P
a4f60 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a4f70 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f  te3VdbeFreeCurso
a4f80 72 28 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75  r(Vdbe *, VdbeCu
a4f90 72 73 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c  rsor*);.void sql
a4fa0 69 74 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28  iteVdbePopStack(
a4fb0 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  Vdbe*,int);.SQLI
a4fc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a4fd0 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
a4fe0 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f  Moveto(VdbeCurso
a4ff0 72 2a 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  r*);.#if defined
a5000 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c  (SQLITE_DEBUG) |
a5010 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50  | defined(VDBE_P
a5020 52 4f 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50  ROFILE).SQLITE_P
a5030 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a5040 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46  te3VdbePrintOp(F
a5050 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b  ILE*, int, Op*);
a5060 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
a5070 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a5080 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
a5090 4c 65 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54 45  Len(u32);.SQLITE
a50a0 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
a50b0 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
a50c0 70 65 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53  pe(Mem*, int);.S
a50d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
a50e0 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  t sqlite3VdbeSer
a50f0 69 61 6c 50 75 74 28 75 6e 73 69 67 6e 65 64 20  ialPut(unsigned 
a5100 63 68 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a  char*, int, Mem*
a5110 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
a5120 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a5130 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28  e3VdbeSerialGet(
a5140 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
a5150 68 61 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29  har*, u32, Mem*)
a5160 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a5170 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
a5180 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56  eDeleteAuxData(V
a5190 64 62 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a  dbeFunc*, int);.
a51a0 0a 69 6e 74 20 73 71 6c 69 74 65 32 42 74 72 65  .int sqlite2Btre
a51b0 65 4b 65 79 43 6f 6d 70 61 72 65 28 42 74 43 75  eKeyCompare(BtCu
a51c0 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f  rsor *, const vo
a51d0 69 64 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  id *, int, int, 
a51e0 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  int *);.SQLITE_P
a51f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a5200 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70  e3VdbeIdxKeyComp
a5210 61 72 65 28 56 64 62 65 43 75 72 73 6f 72 2a 2c  are(VdbeCursor*,
a5220 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c  UnpackedRecord*,
a5230 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
a5240 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a5250 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 42 74  3VdbeIdxRowid(Bt
a5260 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29  Cursor *, i64 *)
a5270 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a5280 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43   int sqlite3MemC
a5290 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d  ompare(const Mem
a52a0 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63  *, const Mem*, c
a52b0 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a  onst CollSeq*);.
a52c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
a52d0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  nt sqlite3VdbeEx
a52e0 65 63 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  ec(Vdbe*);.SQLIT
a52f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a5300 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 56 64  lite3VdbeList(Vd
a5310 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
a5320 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
a5330 56 64 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3b  VdbeHalt(Vdbe*);
a5340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a5350 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
a5360 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65  hangeEncoding(Me
a5370 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  m *, int);.SQLIT
a5380 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
a5390 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42  lite3VdbeMemTooB
a53a0 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ig(Mem*);.SQLITE
a53b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a53c0 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28  ite3VdbeMemCopy(
a53d0 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a  Mem*, const Mem*
a53e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
a53f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
a5400 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
a5410 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d  (Mem*, const Mem
a5420 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
a5430 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a5440 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
a5450 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c  Mem*, Mem*);.SQL
a5460 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a5470 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
a5480 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29  lTerminate(Mem*)
a5490 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
a54a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
a54b0 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20  MemSetStr(Mem*, 
a54c0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
a54d0 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f  , u8, void(*)(vo
a54e0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  id*));.SQLITE_PR
a54f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a5500 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
a5510 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51  4(Mem*, i64);.SQ
a5520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a5530 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
a5540 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20  SetDouble(Mem*, 
a5550 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f  double);.SQLITE_
a5560 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a5570 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
a5580 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ll(Mem*);.SQLITE
a5590 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a55a0 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
a55b0 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74  eroBlob(Mem*,int
a55c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
a55d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
a55e0 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d  beMemSetRowSet(M
a55f0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
a5600 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
a5610 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
a5620 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  able(Mem*);.SQLI
a5630 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a5640 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72  qlite3VdbeMemStr
a5650 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74  ingify(Mem*, int
a5660 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
a5670 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 62  E i64 sqlite3Vdb
a5680 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b  eIntValue(Mem*);
a5690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a56a0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
a56b0 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d  emIntegerify(Mem
a56c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
a56d0 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  TE double sqlite
a56e0 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 4d  3VdbeRealValue(M
a56f0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
a5700 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a5710 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69  3VdbeIntegerAffi
a5720 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  nity(Mem*);.SQLI
a5730 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a5740 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61  qlite3VdbeMemRea
a5750 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  lify(Mem*);.SQLI
a5760 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a5770 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d  qlite3VdbeMemNum
a5780 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c  erify(Mem*);.SQL
a5790 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
a57a0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
a57b0 6f 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f 72  omBtree(BtCursor
a57c0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65  *,int,int,int,Me
a57d0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
a57e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
a57f0 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d  VdbeMemRelease(M
a5800 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50  em *p);.SQLITE_P
a5810 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a5820 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
a5830 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70  eExternal(Mem *p
a5840 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
a5850 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
a5860 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d  eMemFinalize(Mem
a5870 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51  *, FuncDef*);.SQ
a5880 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
a5890 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
a58a0 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b  OpcodeName(int);
a58b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a58c0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f  int sqlite3VdbeO
a58d0 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79  pcodeHasProperty
a58e0 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  (int, int);.SQLI
a58f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
a5900 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
a5910 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74  w(Mem *pMem, int
a5920 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65   n, int preserve
a5930 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
a5940 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
a5950 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28  eCloseStatement(
a5960 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69  Vdbe *, int);.#i
a5970 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
a5980 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45  LE_MEMORY_MANAGE
a5990 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  MENT.SQLITE_PRIV
a59a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
a59b0 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 65 72  dbeReleaseBuffer
a59c0 73 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64  s(Vdbe *p);.#end
a59d0 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
a59e0 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
a59f0 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56  ACHE.SQLITE_PRIV
a5a00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
a5a10 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e  VdbeMutexArrayEn
a5a20 74 65 72 28 56 64 62 65 20 2a 70 29 3b 0a 23 65  ter(Vdbe *p);.#e
a5a30 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  lse.# define sql
a5a40 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72  ite3VdbeMutexArr
a5a50 61 79 45 6e 74 65 72 28 70 29 0a 23 65 6e 64 69  ayEnter(p).#endi
a5a60 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
a5a70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
a5a80 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65  eMemTranslate(Me
a5a90 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65 66 20  m*, u8);.#ifdef 
a5aa0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
a5ab0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
a5ac0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72  id sqlite3VdbePr
a5ad0 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53  intSql(Vdbe*);.S
a5ae0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
a5af0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
a5b00 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 4d  MemPrettyPrint(M
a5b10 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a  em *pMem, char *
a5b20 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a 53 51  zBuf);.#endif.SQ
a5b30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
a5b40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48   sqlite3VdbeMemH
a5b50 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d  andleBom(Mem *pM
a5b60 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  em);..#ifndef SQ
a5b70 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
a5b80 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  OB.SQLITE_PRIVAT
a5b90 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56  E   int sqlite3V
a5ba0 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
a5bb0 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73 65 0a 20  (Mem *);.#else. 
a5bc0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
a5bd0 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
a5be0 62 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23  b(x) SQLITE_OK.#
a5bf0 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
a5c00 20 21 64 65 66 69 6e 65 64 28 5f 56 44 42 45 49   !defined(_VDBEI
a5c10 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  NT_H_) */../****
a5c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
a5c30 66 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a  f vdbeInt.h ****
a5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
a5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
a5c80 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
a5c90 65 66 74 20 6f 66 66 20 69 6e 20 75 74 66 2e 63  eft off in utf.c
a5ca0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
a5cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e  *********/..#ifn
a5cc0 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47  def SQLITE_AMALG
a5cd0 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 54 68  AMATION./*.** Th
a5ce0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73  e following cons
a5cf0 74 61 6e 74 20 76 61 6c 75 65 20 69 73 20 75 73  tant value is us
a5d00 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45  ed by the SQLITE
a5d10 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e 64 0a 2a  _BIGENDIAN and.*
a5d20 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45  * SQLITE_LITTLEE
a5d30 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f  NDIAN macros..*/
a5d40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a5d50 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65  const int sqlite
a5d60 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66  3one = 1;.#endif
a5d70 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47   /* SQLITE_AMALG
a5d80 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a  AMATION */../*.*
a5d90 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70 20 74 61  * This lookup ta
a5da0 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 68  ble is used to h
a5db0 65 6c 70 20 64 65 63 6f 64 65 20 74 68 65 20 66  elp decode the f
a5dc0 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20  irst byte of.** 
a5dd0 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 55 54 46  a multi-byte UTF
a5de0 38 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a  8 character..*/.
a5df0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
a5e00 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74  igned char sqlit
a5e10 65 33 55 74 66 38 54 72 61 6e 73 31 5b 5d 20 3d  e3Utf8Trans1[] =
a5e20 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c   {.  0x00, 0x01,
a5e30 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30   0x02, 0x03, 0x0
a5e40 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30  4, 0x05, 0x06, 0
a5e50 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30  x07,.  0x08, 0x0
a5e60 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30  9, 0x0a, 0x0b, 0
a5e70 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c  x0c, 0x0d, 0x0e,
a5e80 20 30 78 30 66 2c 0a 20 20 30 78 31 30 2c 20 30   0x0f,.  0x10, 0
a5e90 78 31 31 2c 20 30 78 31 32 2c 20 30 78 31 33 2c  x11, 0x12, 0x13,
a5ea0 20 30 78 31 34 2c 20 30 78 31 35 2c 20 30 78 31   0x14, 0x15, 0x1
a5eb0 36 2c 20 30 78 31 37 2c 0a 20 20 30 78 31 38 2c  6, 0x17,.  0x18,
a5ec0 20 30 78 31 39 2c 20 30 78 31 61 2c 20 30 78 31   0x19, 0x1a, 0x1
a5ed0 62 2c 20 30 78 31 63 2c 20 30 78 31 64 2c 20 30  b, 0x1c, 0x1d, 0
a5ee0 78 31 65 2c 20 30 78 31 66 2c 0a 20 20 30 78 30  x1e, 0x1f,.  0x0
a5ef0 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30  0, 0x01, 0x02, 0
a5f00 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c  x03, 0x04, 0x05,
a5f10 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30   0x06, 0x07,.  0
a5f20 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c  x08, 0x09, 0x0a,
a5f30 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30   0x0b, 0x0c, 0x0
a5f40 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20  d, 0x0e, 0x0f,. 
a5f50 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
a5f60 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30  2, 0x03, 0x04, 0
a5f70 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c  x05, 0x06, 0x07,
a5f80 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  .  0x00, 0x01, 0
a5f90 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 30 2c  x02, 0x03, 0x00,
a5fa0 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30   0x01, 0x00, 0x0
a5fb0 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65 20  0,.};...#define 
a5fc0 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c  WRITE_UTF8(zOut,
a5fd0 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20   c) {           
a5fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a5ff0 0a 20 20 69 66 28 20 63 3c 30 78 30 30 30 38 30  .  if( c<0x00080
a6000 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
a6010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6020 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
a6030 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30  Out++ = (u8)(c&0
a6040 78 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  xFF);           
a6050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6060 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
a6070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6090 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65 6c            \.  el
a60a0 73 65 20 69 66 28 20 63 3c 30 78 30 30 38 30 30  se if( c<0x00800
a60b0 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
a60c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a60d0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
a60e0 20 3d 20 30 78 43 30 20 2b 20 28 75 38 29 28 28   = 0xC0 + (u8)((
a60f0 63 3e 3e 36 29 26 30 78 31 46 29 3b 20 20 20 20  c>>6)&0x1F);    
a6100 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a6110 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30    *zOut++ = 0x80
a6120 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46   + (u8)(c & 0x3F
a6130 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
a6140 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
a6150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a6180 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 31    else if( c<0x1
a6190 30 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20  0000 ){         
a61a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a61b0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
a61c0 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b 20 28 75  ut++ = 0xE0 + (u
a61d0 38 29 28 28 63 3e 3e 31 32 29 26 30 78 30 46 29  8)((c>>12)&0x0F)
a61e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a61f0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
a6200 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e  0x80 + (u8)((c>>
a6210 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  6) & 0x3F);     
a6220 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
a6230 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
a6240 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20  (u8)(c & 0x3F); 
a6250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6260 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20    \.  }else{    
a6270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6290 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a62a0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20   *zOut++ = 0xF0 
a62b0 2b 20 28 75 38 29 28 28 63 3e 3e 31 38 29 20 26  + (u8)((c>>18) &
a62c0 20 30 78 30 37 29 3b 20 20 20 20 20 20 20 20 20   0x07);         
a62d0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
a62e0 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28  + = 0x80 + (u8)(
a62f0 28 63 3e 3e 31 32 29 20 26 20 30 78 33 46 29 3b  (c>>12) & 0x3F);
a6300 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a6310 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
a6320 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20  0 + (u8)((c>>6) 
a6330 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
a6340 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
a6350 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38  t++ = 0x80 + (u8
a6360 29 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20  )(c & 0x3F);    
a6370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
a6380 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  .  }            
a6390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a63a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a63b0 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65          \.}..#de
a63c0 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36  fine WRITE_UTF16
a63d0 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20  LE(zOut, c) {   
a63e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a63f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6400 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46   \.  if( c<=0xFF
a6410 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  FF ){           
a6420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6440 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
a6450 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78  ut++ = (u8)(c&0x
a6460 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  00FF);          
a6470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6480 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a6490 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
a64a0 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29  )((c>>8)&0x00FF)
a64b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a64c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a64d0 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20     \.  }else{   
a64e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a64f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6510 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
a6520 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 28  zOut++ = (u8)(((
a6530 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b  c>>10)&0x003F) +
a6540 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e   (((c-0x10000)>>
a6550 31 30 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c  10)&0x00C0));  \
a6560 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
a6570 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 28  u8)(0x00D8 + (((
a6580 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26  c-0x10000)>>18)&
a6590 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20  0x03));         
a65a0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
a65b0 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 30  ++ = (u8)(c&0x00
a65c0 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  FF);            
a65d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a65e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a65f0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
a6600 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29  0x00DC + ((c>>8)
a6610 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20  &0x03));        
a6620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6630 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
a6640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6670 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66         \.}..#def
a6680 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31 36 42  ine WRITE_UTF16B
a6690 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20  E(zOut, c) {    
a66a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a66b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a66c0 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46 46  \.  if( c<=0xFFF
a66d0 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  F ){            
a66e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a66f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6700 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
a6710 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38  t++ = (u8)((c>>8
a6720 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  )&0x00FF);      
a6730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6740 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a6750 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
a6760 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  (c&0x00FF);     
a6770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6790 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20    \.  }else{    
a67a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a67b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a67c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a67d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
a67e0 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30  Out++ = (u8)(0x0
a67f0 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30  0D8 + (((c-0x100
a6800 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b  00)>>18)&0x03));
a6810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a6820 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
a6830 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30  8)(((c>>10)&0x00
a6840 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30  3F) + (((c-0x100
a6850 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29  00)>>10)&0x00C0)
a6860 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b  );  \.    *zOut+
a6870 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 43 20  + = (u8)(0x00DC 
a6880 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29  + ((c>>8)&0x03))
a6890 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a68a0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
a68b0 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63  *zOut++ = (u8)(c
a68c0 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20  &0x00FF);       
a68d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a68e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a68f0 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20  \.  }           
a6900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6930 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69        \.}..#defi
a6940 6e 65 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28  ne READ_UTF16LE(
a6950 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 20 20  zIn, c){        
a6960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6980 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b   \.  c = (*zIn++
a6990 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
a69a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a69b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a69c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 2b           \.  c +
a69d0 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b  = ((*zIn++)<<8);
a69e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a69f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6a10 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 38   \.  if( c>=0xD8
a6a20 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 29  00 && c<0xE000 )
a6a30 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
a6a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6a50 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69           \.    i
a6a60 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29  nt c2 = (*zIn++)
a6a70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a6a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6aa0 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 28 2a   \.    c2 += ((*
a6ab0 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20  zIn++)<<8);     
a6ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6ae0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63           \.    c
a6af0 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b   = (c2&0x03FF) +
a6b00 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30   ((c&0x003F)<<10
a6b10 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29  ) + (((c&0x03C0)
a6b20 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20  +0x0040)<<10);  
a6b30 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
a6b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6b70 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64           \.}..#d
a6b80 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36  efine READ_UTF16
a6b90 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20  BE(zIn, c){     
a6ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6bc0 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 28 2a 7a      \.  c = ((*z
a6bd0 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20  In++)<<8);      
a6be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c00 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a6c10 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20  c += (*zIn++);  
a6c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c50 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30      \.  if( c>=0
a6c60 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30  xD800 && c<0xE00
a6c70 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
a6c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c90 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a6ca0 20 20 69 6e 74 20 63 32 20 3d 20 28 28 2a 7a 49    int c2 = ((*zI
a6cb0 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20  n++)<<8);       
a6cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6ce0 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20      \.    c2 += 
a6cf0 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20  (*zIn++);       
a6d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6d20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
a6d30 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46    c = (c2&0x03FF
a6d40 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c  ) + ((c&0x003F)<
a6d50 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33  <10) + (((c&0x03
a6d60 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29  C0)+0x0040)<<10)
a6d70 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20  ;   \.  }       
a6d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6db0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
a6dc0 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65  ./*.** Translate
a6dd0 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20   a single UTF-8 
a6de0 63 68 61 72 61 63 74 65 72 2e 20 20 52 65 74 75  character.  Retu
a6df0 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 20 76  rn the unicode v
a6e00 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69  alue..**.** Duri
a6e10 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2c 20  ng translation, 
a6e20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20  assume that the 
a6e30 62 79 74 65 20 74 68 61 74 20 7a 54 65 72 6d 20  byte that zTerm 
a6e40 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 61 20 30  points.** is a 0
a6e50 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65  x00..**.** Write
a6e60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
a6e70 65 20 6e 65 78 74 20 75 6e 72 65 61 64 20 62 79  e next unread by
a6e80 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a 70 7a  te back into *pz
a6e90 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  Next..**.** Note
a6ea0 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55 54 46  s On Invalid UTF
a6eb0 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68  -8:.**.**  *  Th
a6ec0 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72  is routine never
a6ed0 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 69 74 20   allows a 7-bit 
a6ee0 63 68 61 72 61 63 74 65 72 20 28 30 78 30 30 20  character (0x00 
a6ef0 74 68 72 6f 75 67 68 20 30 78 37 66 29 20 74 6f  through 0x7f) to
a6f00 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e 63 6f 64  .**     be encod
a6f10 65 64 20 61 73 20 61 20 6d 75 6c 74 69 2d 62 79  ed as a multi-by
a6f20 74 65 20 63 68 61 72 61 63 74 65 72 2e 20 20 41  te character.  A
a6f30 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68  ny multi-byte ch
a6f40 61 72 61 63 74 65 72 20 74 68 61 74 0a 2a 2a 20  aracter that.** 
a6f50 20 20 20 20 61 74 74 65 6d 70 74 73 20 74 6f 20      attempts to 
a6f60 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 62  encode a value b
a6f70 65 74 77 65 65 6e 20 30 78 30 30 20 61 6e 64 20  etween 0x00 and 
a6f80 30 78 37 66 20 69 73 20 72 65 6e 64 65 72 65 64  0x7f is rendered
a6f90 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a   as 0xfffd..**.*
a6fa0 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69  *  *  This routi
a6fb0 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20  ne never allows 
a6fc0 61 20 55 54 46 31 36 20 73 75 72 72 6f 67 61 74  a UTF16 surrogat
a6fd0 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 65 6e  e value to be en
a6fe0 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 20 49 66  coded..**     If
a6ff0 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68   a multi-byte ch
a7000 61 72 61 63 74 65 72 20 61 74 74 65 6d 70 74 73  aracter attempts
a7010 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c   to encode a val
a7020 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 20 20  ue between.**   
a7030 20 20 30 78 64 38 30 30 20 61 6e 64 20 30 78 65    0xd800 and 0xe
a7040 30 30 30 20 74 68 65 6e 20 69 74 20 69 73 20 72  000 then it is r
a7050 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66  endered as 0xfff
a7060 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 42 79 74  d..**.**  *  Byt
a7070 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 65 20  es in the range 
a7080 6f 66 20 30 78 38 30 20 74 68 72 6f 75 67 68 20  of 0x80 through 
a7090 30 78 62 66 20 77 68 69 63 68 20 6f 63 63 75 72  0xbf which occur
a70a0 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   as the first.**
a70b0 20 20 20 20 20 62 79 74 65 20 6f 66 20 61 20 63       byte of a c
a70c0 68 61 72 61 63 74 65 72 20 61 72 65 20 69 6e 74  haracter are int
a70d0 65 72 70 72 65 74 65 64 20 61 73 20 73 69 6e 67  erpreted as sing
a70e0 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 74 65  le-byte characte
a70f0 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 20 72 65  rs.**     and re
a7100 6e 64 65 72 65 64 20 61 73 20 74 68 65 6d 73 65  ndered as themse
a7110 6c 76 65 73 20 65 76 65 6e 20 74 68 6f 75 67 68  lves even though
a7120 20 74 68 65 79 20 61 72 65 20 74 65 63 68 6e 69   they are techni
a7130 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 69 6e 76  cally.**     inv
a7140 61 6c 69 64 20 63 68 61 72 61 63 74 65 72 73 2e  alid characters.
a7150 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20  .**.**  *  This 
a7160 72 6f 75 74 69 6e 65 20 61 63 63 65 70 74 73 20  routine accepts 
a7170 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75 6d 62  an infinite numb
a7180 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20  er of different 
a7190 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 73 0a 2a  UTF8 encodings.*
a71a0 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 63 6f 64  *     for unicod
a71b0 65 20 76 61 6c 75 65 73 20 30 78 38 30 20 61 6e  e values 0x80 an
a71c0 64 20 67 72 65 61 74 65 72 2e 20 20 49 74 20 64  d greater.  It d
a71d0 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65  o not change ove
a71e0 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 20 20 20  r-length.**     
a71f0 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 30 78 66  encodings to 0xf
a7200 66 66 64 20 61 73 20 73 6f 6d 65 20 73 79 73 74  ffd as some syst
a7210 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 2e 0a 2a  ems recommend..*
a7220 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55  /.#define READ_U
a7230 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20  TF8(zIn, zTerm, 
a7240 63 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c)              
a7250 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
a7260 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 20 20   c = *(zIn++);  
a7270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7290 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
a72a0 28 20 63 3e 3d 30 78 63 30 20 29 7b 20 20 20 20  ( c>=0xc0 ){    
a72b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a72c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a72d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d         \.    c =
a72e0 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e   sqlite3Utf8Tran
a72f0 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20 20 20 20  s1[c-0xc0];     
a7300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7310 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c 65 28      \.    while(
a7320 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26 26 20 28   zIn!=zTerm && (
a7330 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 78  *zIn & 0xc0)==0x
a7340 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  80 ){           
a7350 20 5c 0a 20 20 20 20 20 20 63 20 3d 20 28 63 3c   \.      c = (c<
a7360 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 2a 28  <6) + (0x3f & *(
a7370 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20 20 20 20  zIn++));        
a7380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
a7390 20 20 20 20 7d 20 20 20 20 20 20 20 20 20 20 20      }           
a73a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a73b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a73c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
a73d0 20 69 66 28 20 63 3c 30 78 38 30 20 20 20 20 20   if( c<0x80     
a73e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a73f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7400 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20          \.      
a7410 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 38    || (c&0xFFFFF8
a7420 30 30 29 3d 3d 30 78 44 38 30 30 20 20 20 20 20  00)==0xD800     
a7430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7440 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c       \.        |
a7450 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45 29  | (c&0xFFFFFFFE)
a7460 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20 3d  ==0xFFFE ){  c =
a7470 20 30 78 46 46 46 44 3b 20 7d 20 20 20 20 20 20   0xFFFD; }      
a7480 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54 45 5f 50    \.  }.SQLITE_P
a7490 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
a74a0 65 33 55 74 66 38 52 65 61 64 28 0a 20 20 63 6f  e3Utf8Read(.  co
a74b0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
a74c0 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 20 2f 2a  r *zIn,       /*
a74d0 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 55   First byte of U
a74e0 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 20 2a  TF-8 character *
a74f0 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  /.  const unsign
a7500 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e 65 78 74  ed char **pzNext
a7510 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66 69 72      /* Write fir
a7520 73 74 20 62 79 74 65 20 70 61 73 74 20 55 54 46  st byte past UTF
a7530 2d 38 20 63 68 61 72 20 68 65 72 65 20 2a 2f 0a  -8 char here */.
a7540 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 0a 20 20 2f  ){.  int c;..  /
a7550 2a 20 53 61 6d 65 20 61 73 20 52 45 41 44 5f 55  * Same as READ_U
a7560 54 46 38 28 29 20 61 62 6f 76 65 20 62 75 74 20  TF8() above but 
a7570 77 69 74 68 6f 75 74 20 74 68 65 20 7a 54 65 72  without the zTer
a7580 6d 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a  m parameter..  *
a7590 2a 20 46 6f 72 20 74 68 69 73 20 72 6f 75 74 69  * For this routi
a75a0 6e 65 2c 20 77 65 20 61 73 73 75 6d 65 20 74 68  ne, we assume th
a75b0 65 20 55 54 46 38 20 73 74 72 69 6e 67 20 69 73  e UTF8 string is
a75c0 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65 72   always zero-ter
a75d0 6d 69 6e 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  minated..  */.  
a75e0 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 0a 20 20  c = *(zIn++);.  
a75f0 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 0a 20  if( c>=0xc0 ){. 
a7600 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74     c = sqlite3Ut
a7610 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d  f8Trans1[c-0xc0]
a7620 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 7a  ;.    while( (*z
a7630 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30  In & 0xc0)==0x80
a7640 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 28 63   ){.      c = (c
a7650 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 2a  <<6) + (0x3f & *
a7660 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 20 20 7d 0a  (zIn++));.    }.
a7670 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 0a 20      if( c<0x80. 
a7680 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46         || (c&0xF
a7690 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30  FFFF800)==0xD800
a76a0 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30  .        || (c&0
a76b0 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46  xFFFFFFFE)==0xFF
a76c0 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46 46  FE ){  c = 0xFFF
a76d0 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a 4e 65  D; }.  }.  *pzNe
a76e0 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 72 65 74 75  xt = zIn;.  retu
a76f0 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a 2a  rn c;.}...../*.*
a7700 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 4c 41  * If the TRANSLA
a7710 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f 20 69  TE_TRACE macro i
a7720 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 20 76  s defined, the v
a7730 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d 65 6d  alue of each Mem
a7740 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 20 6f   is.** printed o
a7750 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 65 20  n stderr on the 
a7760 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f 75 74  way into and out
a7770 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 4d   of sqlite3VdbeM
a7780 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e 0a 2a  emTranslate()..*
a7790 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 54 52  / ./* #define TR
a77a0 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 31 20  ANSLATE_TRACE 1 
a77b0 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
a77c0 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
a77d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
a77e0 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 65 20   transforms the 
a77f0 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 65 6e  internal text en
a7800 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 70  coding used by p
a7810 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 72 65  Mem to.** desire
a7820 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e 20 65  dEnc. It is an e
a7830 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 72 69  rror if the stri
a7840 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 6f 66  ng is already of
a7850 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20   the desired.** 
a7860 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 66 20  encoding, or if 
a7870 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 20 63  *pMem does not c
a7880 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e 67 20  ontain a string 
a7890 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  value..*/.SQLITE
a78a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a78b0 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73  ite3VdbeMemTrans
a78c0 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  late(Mem *pMem, 
a78d0 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a  u8 desiredEnc){.
a78e0 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20    int len;      
a78f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a7900 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   Maximum length 
a7910 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67  of output string
a7920 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75   in bytes */.  u
a7930 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f  nsigned char *zO
a7940 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
a7950 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62       /* Output b
a7960 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67  uffer */.  unsig
a7970 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20  ned char *zIn;  
a7980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7990 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 61 74   /* Input iterat
a79a0 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  or */.  unsigned
a79b0 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20   char *zTerm;   
a79c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a79d0 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 2a 2f   End of input */
a79e0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
a79f0 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
a7a00 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
a7a10 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20  ut iterator */. 
a7a20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 3b   unsigned int c;
a7a30 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ..  assert( pMem
a7a40 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
a7a50 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
a7a60 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
a7a70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ;.  assert( pMem
a7a80 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20  ->flags&MEM_Str 
a7a90 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
a7aa0 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45  m->enc!=desiredE
a7ab0 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  nc );.  assert( 
a7ac0 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b 0a  pMem->enc!=0 );.
a7ad0 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
a7ae0 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 65 66  n>=0 );..#if def
a7af0 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 5f 54  ined(TRANSLATE_T
a7b00 52 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 64  RACE) && defined
a7b10 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 20  (SQLITE_DEBUG). 
a7b20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66   {.    char zBuf
a7b30 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74  [100];.    sqlit
a7b40 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
a7b50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29  rint(pMem, zBuf)
a7b60 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 73 74  ;.    fprintf(st
a7b70 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 20 25  derr, "INPUT:  %
a7b80 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d  s\n", zBuf);.  }
a7b90 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
a7ba0 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e   the translation
a7bb0 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d   is between UTF-
a7bc0 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 62 69  16 little and bi
a7bd0 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e 20 0a  g endian, then .
a7be0 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 73    ** all that is
a7bf0 20 72 65 71 75 69 72 65 64 20 69 73 20 74 6f 20   required is to 
a7c00 73 77 61 70 20 74 68 65 20 62 79 74 65 20 6f 72  swap the byte or
a7c10 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 20 69  der. This case i
a7c20 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a 20 64  s handled.  ** d
a7c30 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20  ifferently from 
a7c40 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 2a 2f  the others..  */
a7c50 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63  .  if( pMem->enc
a7c60 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26  !=SQLITE_UTF8 &&
a7c70 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 51 4c   desiredEnc!=SQL
a7c80 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20  ITE_UTF8 ){.    
a7c90 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 6e 74  u8 temp;.    int
a7ca0 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71   rc;.    rc = sq
a7cb0 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
a7cc0 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b  Writeable(pMem);
a7cd0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
a7ce0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
a7cf0 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
a7d00 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
a7d10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a7d20 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
a7d30 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d   zIn = (u8*)pMem
a7d40 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d 20 3d  ->z;.    zTerm =
a7d50 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e 31   &zIn[pMem->n&~1
a7d60 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49  ];.    while( zI
a7d70 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20  n<zTerm ){.     
a7d80 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20 20   temp = *zIn;.  
a7d90 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49 6e      *zIn = *(zIn
a7da0 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e 2b 2b  +1);.      zIn++
a7db0 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b 20 3d  ;.      *zIn++ =
a7dc0 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20   temp;.    }.   
a7dd0 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73   pMem->enc = des
a7de0 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 6f 74  iredEnc;.    got
a7df0 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3b  o translate_out;
a7e00 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 6c  .  }..  /* Set l
a7e10 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  en to the maximu
a7e20 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  m number of byte
a7e30 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68  s required in th
a7e40 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e  e output buffer.
a7e50 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 72 65   */.  if( desire
a7e60 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
a7e70 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e  8 ){.    /* When
a7e80 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d   converting from
a7e90 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d 61 78   UTF-16, the max
a7ea0 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 73 75  imum growth resu
a7eb0 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20  lts from.    ** 
a7ec0 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 32 2d  translating a 2-
a7ed0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74  byte character t
a7ee0 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 2d 38  o a 4-byte UTF-8
a7ef0 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20   character..    
a7f00 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 74 65  ** A single byte
a7f10 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
a7f20 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 72 69   the output stri
a7f30 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65  ng.    ** nul-te
a7f40 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f  rminator..    */
a7f50 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d 20  .    pMem->n &= 
a7f60 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d  ~1;.    len = pM
a7f70 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a 20  em->n * 2 + 1;. 
a7f80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57   }else{.    /* W
a7f90 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66  hen converting f
a7fa0 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46  rom UTF-8 to UTF
a7fb0 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  -16 the maximum 
a7fc0 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 65 64  growth is caused
a7fd0 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 20 31  .    ** when a 1
a7fe0 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 61 72  -byte UTF-8 char
a7ff0 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 6c 61  acter is transla
a8000 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 79 74  ted into a 2-byt
a8010 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a 2a 20  e UTF-16.    ** 
a8020 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f 20 62  character. Two b
a8030 79 74 65 73 20 61 72 65 20 72 65 71 75 69 72 65  ytes are require
a8040 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20  d in the output 
a8050 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 0a 20  buffer for the. 
a8060 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e     ** nul-termin
a8070 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ator..    */.   
a8080 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a   len = pMem->n *
a8090 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f   2 + 2;.  }..  /
a80a0 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69  * Set zIn to poi
a80b0 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20  nt at the start 
a80c0 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 66  of the input buf
a80d0 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f  fer and zTerm to
a80e0 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 62 79   point 1.  ** by
a80f0 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 2e  te past the end.
a8100 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61  .  **.  ** Varia
a8110 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 74 20  ble zOut is set 
a8120 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
a8130 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c 20 73  output buffer, s
a8140 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a 20 20  pace obtained.  
a8150 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ** from sqlite3_
a8160 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20  malloc()..  */. 
a8170 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d   zIn = (u8*)pMem
a8180 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26  ->z;.  zTerm = &
a8190 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20  zIn[pMem->n];.  
a81a0 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 44 62  zOut = sqlite3Db
a81b0 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e  MallocRaw(pMem->
a81c0 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 28 20  db, len);.  if( 
a81d0 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 65 74  !zOut ){.    ret
a81e0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
a81f0 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74  ;.  }.  z = zOut
a8200 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65  ;..  if( pMem->e
a8210 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
a8220 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 69 72  ){.    if( desir
a8230 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
a8240 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f  F16LE ){.      /
a8250 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31  * UTF-8 -> UTF-1
a8260 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20  6 Little-endian 
a8270 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
a8280 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
a8290 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69       /* c = sqli
a82a0 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c  te3Utf8Read(zIn,
a82b0 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75   zTerm, (const u
a82c0 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20  8**)&zIn); */.  
a82d0 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28        READ_UTF8(
a82e0 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a  zIn, zTerm, c);.
a82f0 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
a8300 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16LE(z, c);.   
a8310 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
a8320 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 65        assert( de
a8330 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
a8340 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 20 20  _UTF16BE );.    
a8350 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54    /* UTF-8 -> UT
a8360 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20  F-16 Big-endian 
a8370 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
a8380 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
a8390 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69       /* c = sqli
a83a0 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c  te3Utf8Read(zIn,
a83b0 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75   zTerm, (const u
a83c0 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20  8**)&zIn); */.  
a83d0 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 38 28        READ_UTF8(
a83e0 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a  zIn, zTerm, c);.
a83f0 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
a8400 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20  F16BE(z, c);.   
a8410 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
a8420 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a  Mem->n = (int)(z
a8430 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2a 7a   - zOut);.    *z
a8440 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ++ = 0;.  }else{
a8450 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65 73  .    assert( des
a8460 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
a8470 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28 20  UTF8 );.    if( 
a8480 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  pMem->enc==SQLIT
a8490 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20  E_UTF16LE ){.   
a84a0 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 74     /* UTF-16 Lit
a84b0 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54  tle-endian -> UT
a84c0 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  F-8 */.      whi
a84d0 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b  le( zIn<zTerm ){
a84e0 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54  .        READ_UT
a84f0 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b 20 0a  F16LE(zIn, c); .
a8500 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
a8510 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  F8(z, c);.      
a8520 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
a8530 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69 67     /* UTF-16 Big
a8540 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38  -endian -> UTF-8
a8550 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
a8560 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
a8570 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36        READ_UTF16
a8580 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20 20  BE(zIn, c); .   
a8590 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 28       WRITE_UTF8(
a85a0 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20  z, c);.      }. 
a85b0 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e     }.    pMem->n
a85c0 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75   = (int)(z - zOu
a85d0 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30  t);.  }.  *z = 0
a85e0 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  ;.  assert( (pMe
a85f0 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45 6e 63  m->n+(desiredEnc
a8600 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a  ==SQLITE_UTF8?1:
a8610 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73  2))<=len );..  s
a8620 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
a8630 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d  ease(pMem);.  pM
a8640 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  em->flags &= ~(M
a8650 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79  EM_Static|MEM_Dy
a8660 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20  n|MEM_Ephem);.  
a8670 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69  pMem->enc = desi
a8680 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e  redEnc;.  pMem->
a8690 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65  flags |= (MEM_Te
a86a0 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 70  rm|MEM_Dyn);.  p
a86b0 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29  Mem->z = (char*)
a86c0 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d  zOut;.  pMem->zM
a86d0 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b  alloc = pMem->z;
a86e0 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a  ..translate_out:
a86f0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41  .#if defined(TRA
a8700 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26  NSLATE_TRACE) &&
a8710 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
a8720 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63  DEBUG).  {.    c
a8730 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20  har zBuf[100];. 
a8740 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
a8750 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65  mPrettyPrint(pMe
a8760 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70  m, zBuf);.    fp
a8770 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 4f  rintf(stderr, "O
a8780 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42  UTPUT: %s\n", zB
a8790 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  uf);.  }.#endif.
a87a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
a87b0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
a87c0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
a87d0 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72 64 65   for a byte-orde
a87e0 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20 62 65  r mark at the be
a87f0 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a  ginning of the .
a8800 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  ** UTF-16 string
a8810 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d   stored in *pMem
a8820 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72 65 73  . If one is pres
a8830 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d 6f 76  ent, it is remov
a8840 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e  ed and.** the en
a8850 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65  coding of the Me
a8860 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68 69 73  m adjusted. This
a8870 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f   routine does no
a8880 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65  t do any.** byte
a8890 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 6a 75  -swapping, it ju
a88a0 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20  st sets Mem.enc 
a88b0 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a  appropriately..*
a88c0 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74  *.** The allocat
a88d0 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64 79 6e  ion (static, dyn
a88e0 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64 20 65  amic etc.) and e
a88f0 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 4d  ncoding of the M
a8900 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61  em may be.** cha
a8910 6e 67 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  nged by this fun
a8920 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
a8930 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
a8940 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c  ite3VdbeMemHandl
a8950 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  eBom(Mem *pMem){
a8960 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
a8970 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20  TE_OK;.  u8 bom 
a8980 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
a8990 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 20  pMem->n>=0 );.  
a89a0 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b  if( pMem->n>1 ){
a89b0 0a 20 20 20 20 75 38 20 62 31 20 3d 20 2a 28 75  .    u8 b1 = *(u
a89c0 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20  8 *)pMem->z;.   
a89d0 20 75 38 20 62 32 20 3d 20 2a 28 28 28 75 38 20   u8 b2 = *(((u8 
a89e0 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b  *)pMem->z) + 1);
a89f0 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46  .    if( b1==0xF
a8a00 45 20 26 26 20 62 32 3d 3d 30 78 46 46 20 29 7b  E && b2==0xFF ){
a8a10 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c  .      bom = SQL
a8a20 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 20  ITE_UTF16BE;.   
a8a30 20 7d 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30   }.    if( b1==0
a8a40 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46 45 20  xFF && b2==0xFE 
a8a50 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53  ){.      bom = S
a8a60 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20  QLITE_UTF16LE;. 
a8a70 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66     }.  }.  .  if
a8a80 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72 63 20  ( bom ){.    rc 
a8a90 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
a8aa0 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d  MakeWriteable(pM
a8ab0 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  em);.    if( rc=
a8ac0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
a8ad0 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32      pMem->n -= 2
a8ae0 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28  ;.      memmove(
a8af0 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e  pMem->z, &pMem->
a8b00 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a  z[2], pMem->n);.
a8b10 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d        pMem->z[pM
a8b20 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20  em->n] = '\0';. 
a8b30 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65       pMem->z[pMe
a8b40 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a  m->n+1] = '\0';.
a8b50 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
a8b60 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20  s |= MEM_Term;. 
a8b70 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d       pMem->enc =
a8b80 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   bom;.    }.  }.
a8b90 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
a8ba0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
a8bb0 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
a8bc0 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46  *.** pZ is a UTF
a8bd0 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f  -8 encoded unico
a8be0 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e 42  de string. If nB
a8bf0 79 74 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e  yte is less than
a8c00 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e   zero,.** return
a8c10 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75   the number of u
a8c20 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72  nicode character
a8c30 73 20 69 6e 20 70 5a 20 75 70 20 74 6f 20 28 62  s in pZ up to (b
a8c40 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67  ut not including
a8c50 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30  ).** the first 0
a8c60 78 30 30 20 62 79 74 65 2e 20 49 66 20 6e 42 79  x00 byte. If nBy
a8c70 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 73 20 74  te is not less t
a8c80 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e  han zero, return
a8c90 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
a8ca0 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63  f unicode charac
a8cb0 74 65 72 73 20 69 6e 20 74 68 65 20 66 69 72 73  ters in the firs
a8cc0 74 20 6e 42 79 74 65 20 6f 66 20 70 5a 20 28 6f  t nByte of pZ (o
a8cd0 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20  r up to .** the 
a8ce0 66 69 72 73 74 20 30 78 30 30 2c 20 77 68 69 63  first 0x00, whic
a8cf0 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73  hever comes firs
a8d00 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  t)..*/.SQLITE_PR
a8d10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
a8d20 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e  3Utf8CharLen(con
a8d30 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e  st char *zIn, in
a8d40 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20  t nByte){.  int 
a8d50 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75  r = 0;.  const u
a8d60 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38  8 *z = (const u8
a8d70 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75  *)zIn;.  const u
a8d80 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20  8 *zTerm;.  if( 
a8d90 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20  nByte>=0 ){.    
a8da0 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65  zTerm = &z[nByte
a8db0 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
a8dc0 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75  zTerm = (const u
a8dd0 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61  8*)(-1);.  }.  a
a8de0 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20  ssert( z<=zTerm 
a8df0 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d  );.  while( *z!=
a8e00 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a  0 && z<zTerm ){.
a8e10 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
a8e20 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b  UTF8(z);.    r++
a8e30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
a8e40 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74 65 73  ;.}../* This tes
a8e50 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f  t function is no
a8e60 74 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64  t currently used
a8e70 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61 74 65   by the automate
a8e80 64 20 74 65 73 74 2d 73 75 69 74 65 2e 20 0a 2a  d test-suite. .*
a8e90 2a 20 48 65 6e 63 65 20 69 74 20 69 73 20 6f 6e  * Hence it is on
a8ea0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ly available in 
a8eb0 64 65 62 75 67 20 62 75 69 6c 64 73 2e 0a 2a 2f  debug builds..*/
a8ec0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
a8ed0 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66  ITE_TEST) && def
a8ee0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
a8ef0 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61  G)./*.** Transla
a8f00 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d  te UTF-8 to UTF-
a8f10 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 61  8..**.** This ha
a8f20 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20  s the effect of 
a8f30 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74  making sure that
a8f40 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 77   the string is w
a8f50 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 54  ell-formed.** UT
a8f60 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64 20 63  F-8.  Miscoded c
a8f70 68 61 72 61 63 74 65 72 73 20 61 72 65 20 72 65  haracters are re
a8f80 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  moved..**.** The
a8f90 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20   translation is 
a8fa0 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20 28 73  done in-place (s
a8fb0 69 6e 63 65 20 69 74 20 69 73 20 69 6d 70 6f 73  ince it is impos
a8fc0 73 69 62 6c 65 20 66 6f 72 20 74 68 65 0a 2a 2a  sible for the.**
a8fd0 20 63 6f 72 72 65 63 74 20 55 54 46 2d 38 20 65   correct UTF-8 e
a8fe0 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 6c 6f  ncoding to be lo
a8ff0 6e 67 65 72 20 74 68 61 6e 20 61 20 6d 61 6c 66  nger than a malf
a9000 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67 29 2e  ormed encoding).
a9010 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a9020 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
a9030 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63  f8To8(unsigned c
a9040 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73  har *zIn){.  uns
a9050 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74  igned char *zOut
a9060 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e   = zIn;.  unsign
a9070 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20  ed char *zStart 
a9080 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 63 3b 0a  = zIn;.  u32 c;.
a9090 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d  .  while( zIn[0]
a90a0 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69   ){.    c = sqli
a90b0 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 6e 2c  te3Utf8Read(zIn,
a90c0 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49   (const u8**)&zI
a90d0 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 21 3d 30  n);.    if( c!=0
a90e0 78 66 66 66 64 20 29 7b 0a 20 20 20 20 20 20 57  xfffd ){.      W
a90f0 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20  RITE_UTF8(zOut, 
a9100 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  c);.    }.  }.  
a9110 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65 74  *zOut = 0;.  ret
a9120 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74 20 2d  urn (int)(zOut -
a9130 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 65 6e 64   zStart);.}.#end
a9140 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
a9150 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
a9160 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54  .** Convert a UT
a9170 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 74  F-16 string in t
a9180 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69  he native encodi
a9190 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d 38 20  ng into a UTF-8 
a91a0 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72  string..** Memor
a91b0 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 55 54  y to hold the UT
a91c0 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20 6f 62  F-8 string is ob
a91d0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
a91e0 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d  te3_malloc and m
a91f0 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65 64 20  ust.** be freed 
a9200 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66  by the calling f
a9210 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e  unction..**.** N
a9220 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
a9230 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 61  if there is an a
a9240 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e  llocation error.
a9250 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a9260 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
a9270 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33  Utf16to8(sqlite3
a9280 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *db, const void
a9290 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b   *z, int nByte){
a92a0 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73  .  Mem m;.  mems
a92b0 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66  et(&m, 0, sizeof
a92c0 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64  (m));.  m.db = d
a92d0 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  b;.  sqlite3Vdbe
a92e0 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c  MemSetStr(&m, z,
a92f0 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45 5f 55   nByte, SQLITE_U
a9300 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49  TF16NATIVE, SQLI
a9310 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71  TE_STATIC);.  sq
a9320 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
a9330 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51 4c 49  ncoding(&m, SQLI
a9340 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28 20  TE_UTF8);.  if( 
a9350 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
a9360 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
a9370 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d  dbeMemRelease(&m
a9380 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a  );.    m.z = 0;.
a9390 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 6d    }.  assert( (m
a93a0 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72  .flags & MEM_Ter
a93b0 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c  m)!=0 || db->mal
a93c0 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61  locFailed );.  a
a93d0 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 73 20  ssert( (m.flags 
a93e0 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c  & MEM_Str)!=0 ||
a93f0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
a9400 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 6d  d );.  return (m
a9410 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  .flags & MEM_Dyn
a9420 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c  )!=0 ? m.z : sql
a9430 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
a9440 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   m.z);.}../*.** 
a9450 70 5a 20 69 73 20 61 20 55 54 46 2d 31 36 20 65  pZ is a UTF-16 e
a9460 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20 73  ncoded unicode s
a9470 74 72 69 6e 67 20 61 74 20 6c 65 61 73 74 20 6e  tring at least n
a9480 43 68 61 72 20 63 68 61 72 61 63 74 65 72 73 20  Char characters 
a9490 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  long..** Return 
a94a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
a94b0 74 65 73 20 69 6e 20 74 68 65 20 66 69 72 73 74  tes in the first
a94c0 20 6e 43 68 61 72 20 75 6e 69 63 6f 64 65 20 63   nChar unicode c
a94d0 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 69 6e 20  haracters.** in 
a94e0 70 5a 2e 20 20 6e 43 68 61 72 20 6d 75 73 74 20  pZ.  nChar must 
a94f0 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  be non-negative.
a9500 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a9510 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
a9520 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74  f16ByteLen(const
a9530 20 76 6f 69 64 20 2a 7a 49 6e 2c 20 69 6e 74 20   void *zIn, int 
a9540 6e 43 68 61 72 29 7b 0a 20 20 69 6e 74 20 63 3b  nChar){.  int c;
a9550 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
a9560 20 63 6f 6e 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b   const *z = zIn;
a9570 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20  .  int n = 0;.  
a9580 69 66 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36  if( SQLITE_UTF16
a9590 4e 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55  NATIVE==SQLITE_U
a95a0 54 46 31 36 42 45 20 29 7b 0a 20 20 20 20 2f 2a  TF16BE ){.    /*
a95b0 20 55 73 69 6e 67 20 61 6e 20 22 69 66 20 28 53   Using an "if (S
a95c0 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
a95d0 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  E==SQLITE_UTF16B
a95e0 45 29 22 20 63 6f 6e 73 74 72 75 63 74 20 68 65  E)" construct he
a95f0 72 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69 6e  re.    ** and in
a9600 20 6f 74 68 65 72 20 70 61 72 74 73 20 6f 66 20   other parts of 
a9610 74 68 69 73 20 66 69 6c 65 20 6d 65 61 6e 73 20  this file means 
a9620 74 68 61 74 20 61 74 20 6f 6e 65 20 62 72 61 6e  that at one bran
a9630 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e  ch will.    ** n
a9640 6f 74 20 62 65 20 63 6f 76 65 72 65 64 20 62 79  ot be covered by
a9650 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e   coverage testin
a9660 67 20 6f 6e 20 61 6e 79 20 73 69 6e 67 6c 65 20  g on any single 
a9670 68 6f 73 74 2e 20 42 75 74 20 63 6f 76 65 72 61  host. But covera
a9680 67 65 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  ge.    ** will b
a9690 65 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 74 68  e complete if th
a96a0 65 20 74 65 73 74 73 20 61 72 65 20 72 75 6e 20  e tests are run 
a96b0 6f 6e 20 62 6f 74 68 20 61 20 6c 69 74 74 6c 65  on both a little
a96c0 2d 65 6e 64 69 61 6e 20 61 6e 64 20 0a 20 20 20  -endian and .   
a96d0 20 2a 2a 20 62 69 67 2d 65 6e 64 69 61 6e 20 68   ** big-endian h
a96e0 6f 73 74 2e 20 42 65 63 61 75 73 65 20 62 6f 74  ost. Because bot
a96f0 68 20 74 68 65 20 55 54 46 31 36 4e 41 54 49 56  h the UTF16NATIV
a9700 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 55 54 46  E and SQLITE_UTF
a9710 31 36 42 45 0a 20 20 20 20 2a 2a 20 6d 61 63 72  16BE.    ** macr
a9720 6f 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20  os are constant 
a9730 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
a9740 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e  the compiler can
a9750 20 64 65 74 65 72 6d 69 6e 65 0a 20 20 20 20 2a   determine.    *
a9760 2a 20 77 68 69 63 68 20 62 72 61 6e 63 68 20 77  * which branch w
a9770 69 6c 6c 20 62 65 20 66 6f 6c 6c 6f 77 65 64 2e  ill be followed.
a9780 20 49 74 20 69 73 20 74 68 65 72 65 66 6f 72 65   It is therefore
a9790 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 6e 6f   assumed that no
a97a0 20 72 75 6e 74 69 6d 65 0a 20 20 20 20 2a 2a 20   runtime.    ** 
a97b0 70 65 6e 61 6c 74 79 20 69 73 20 70 61 69 64 20  penalty is paid 
a97c0 66 6f 72 20 74 68 69 73 20 22 69 66 22 20 73 74  for this "if" st
a97d0 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a  atement..    */.
a97e0 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68      while( n<nCh
a97f0 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44  ar ){.      READ
a9800 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a  _UTF16BE(z, c);.
a9810 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d        n++;.    }
a9820 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68  .  }else{.    wh
a9830 69 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a  ile( n<nChar ){.
a9840 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36        READ_UTF16
a9850 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  LE(z, c);.      
a9860 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n++;.    }.  }. 
a9870 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 2d   return (int)(z-
a9880 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63  (unsigned char c
a9890 6f 6e 73 74 20 2a 29 7a 49 6e 29 3b 0a 7d 0a 0a  onst *)zIn);.}..
a98a0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
a98b0 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54  TE_TEST)./*.** T
a98c0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
a98d0 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54  alled from the T
a98e0 43 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e  CL test function
a98f0 20 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66   "translate_self
a9900 74 65 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 65  test"..** It che
a9910 63 6b 73 20 74 68 61 74 20 74 68 65 20 70 72 69  cks that the pri
a9920 6d 69 74 69 76 65 73 20 66 6f 72 20 73 65 72 69  mitives for seri
a9930 61 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65  alizing and dese
a9940 72 69 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 61  rializing.** cha
a9950 72 61 63 74 65 72 73 20 69 6e 20 65 61 63 68 20  racters in each 
a9960 65 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e 76  encoding are inv
a9970 65 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f 74  erses of each ot
a9980 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  her..*/.SQLITE_P
a9990 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a99a0 74 65 33 55 74 66 53 65 6c 66 54 65 73 74 28 76  te3UtfSelfTest(v
a99b0 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  oid){.  unsigned
a99c0 20 69 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e 73   int i, t;.  uns
a99d0 69 67 6e 65 64 20 63 68 61 72 20 7a 42 75 66 5b  igned char zBuf[
a99e0 32 30 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  20];.  unsigned 
a99f0 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 6e  char *z;.  int n
a9a00 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  ;.  unsigned int
a9a10 20 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20   c;..  for(i=0; 
a9a20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b  i<0x00110000; i+
a9a30 2b 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66  +){.    z = zBuf
a9a40 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 38  ;.    WRITE_UTF8
a9a50 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20  (z, i);.    n = 
a9a60 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20  (int)(z-zBuf);. 
a9a70 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26     assert( n>0 &
a9a80 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b  & n<=4 );.    z[
a9a90 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20  0] = 0;.    z = 
a9aa0 7a 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71  zBuf;.    c = sq
a9ab0 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c  lite3Utf8Read(z,
a9ac0 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29   (const u8**)&z)
a9ad0 3b 0a 20 20 20 20 74 20 3d 20 69 3b 0a 20 20 20  ;.    t = i;.   
a9ae0 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26   if( i>=0xD800 &
a9af0 26 20 69 3c 3d 30 78 44 46 46 46 20 29 20 74 20  & i<=0xDFFF ) t 
a9b00 3d 20 30 78 46 46 46 44 3b 0a 20 20 20 20 69 66  = 0xFFFD;.    if
a9b10 28 20 28 69 26 30 78 46 46 46 46 46 46 46 45 29  ( (i&0xFFFFFFFE)
a9b20 3d 3d 30 78 46 46 46 45 20 29 20 74 20 3d 20 30  ==0xFFFE ) t = 0
a9b30 78 46 46 46 44 3b 0a 20 20 20 20 61 73 73 65 72  xFFFD;.    asser
a9b40 74 28 20 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61  t( c==t );.    a
a9b50 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d  ssert( (z-zBuf)=
a9b60 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  =n );.  }.  for(
a9b70 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30  i=0; i<0x0011000
a9b80 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  0; i++){.    if(
a9b90 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c   i>=0xD800 && i<
a9ba0 30 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75  0xE000 ) continu
a9bb0 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  e;.    z = zBuf;
a9bc0 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36  .    WRITE_UTF16
a9bd0 4c 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20  LE(z, i);.    n 
a9be0 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b  = (int)(z-zBuf);
a9bf0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30  .    assert( n>0
a9c00 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20   && n<=4 );.    
a9c10 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20  z[0] = 0;.    z 
a9c20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 41 44  = zBuf;.    READ
a9c30 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a  _UTF16LE(z, c);.
a9c40 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 69      assert( c==i
a9c50 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
a9c60 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20  (z-zBuf)==n );. 
a9c70 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
a9c80 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29  0x00110000; i++)
a9c90 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44  {.    if( i>=0xD
a9ca0 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20  800 && i<0xE000 
a9cb0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
a9cc0 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52  z = zBuf;.    WR
a9cd0 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 69  ITE_UTF16BE(z, i
a9ce0 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29  );.    n = (int)
a9cf0 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73  (z-zBuf);.    as
a9d00 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d  sert( n>0 && n<=
a9d10 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20  4 );.    z[0] = 
a9d20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  0;.    z = zBuf;
a9d30 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42  .    READ_UTF16B
a9d40 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73 73  E(z, c);.    ass
a9d50 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20  ert( c==i );.   
a9d60 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66   assert( (z-zBuf
a9d70 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65  )==n );.  }.}.#e
a9d80 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54  ndif /* SQLITE_T
a9d90 45 53 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  EST */.#endif /*
a9da0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
a9db0 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  16 */../********
a9dc0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74  ****** End of ut
a9dd0 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.c ************
a9de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9e00 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
a9e10 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
a9e20 65 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e util.c *******
a9e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9e50 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
a9e60 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
a9e70 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
a9e80 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
a9e90 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
a9ea0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
a9eb0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
a9ec0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
a9ed0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
a9ee0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
a9ef0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
a9f00 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
a9f10 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
a9f20 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
a9f30 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
a9f40 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
a9f50 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
a9f60 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
a9f70 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
a9f80 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
a9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
a9fd0 20 55 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f   Utility functio
a9fe0 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f  ns used througho
a9ff0 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a  ut sqlite..**.**
aa000 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
aa010 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f  ins functions fo
aa020 72 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d  r allocating mem
aa030 6f 72 79 2c 20 63 6f 6d 70 61 72 69 6e 67 0a 2a  ory, comparing.*
aa040 2a 20 73 74 72 69 6e 67 73 2c 20 61 6e 64 20 73  * strings, and s
aa050 74 75 66 66 20 6c 69 6b 65 20 74 68 61 74 2e 0a  tuff like that..
aa060 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 74 69 6c 2e  **.** $Id: util.
aa070 63 2c 76 20 31 2e 32 35 34 20 32 30 30 39 2f 30  c,v 1.254 2009/0
aa080 35 2f 30 36 20 31 39 3a 30 33 3a 31 34 20 64 72  5/06 19:03:14 dr
aa090 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 64 65  h Exp $.*/.#ifde
aa0a0 66 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53  f SQLITE_HAVE_IS
aa0b0 4e 41 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6d  NAN.# include <m
aa0c0 61 74 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f  ath.h>.#endif../
aa0d0 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 65  *.** Routine nee
aa0e0 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ded to support t
aa0f0 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d 61  he testcase() ma
aa100 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  cro..*/.#ifdef S
aa110 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54  QLITE_COVERAGE_T
aa120 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
aa130 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
aa140 6f 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b 0a  overage(int x){.
aa150 20 20 73 74 61 74 69 63 20 69 6e 74 20 64 75 6d    static int dum
aa160 6d 79 20 3d 20 30 3b 0a 20 20 64 75 6d 6d 79 20  my = 0;.  dummy 
aa170 2b 3d 20 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  += x;.}.#endif..
aa180 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65  /*.** Routine ne
aa190 65 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20  eded to support 
aa1a0 74 68 65 20 41 4c 57 41 59 53 28 29 20 61 6e 64  the ALWAYS() and
aa1b0 20 4e 45 56 45 52 28 29 20 6d 61 63 72 6f 73 2e   NEVER() macros.
aa1c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  .**.** The argum
aa1d0 65 6e 74 20 74 6f 20 41 4c 57 41 59 53 28 29 20  ent to ALWAYS() 
aa1e0 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
aa1f0 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 61 72   true and the ar
aa200 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 4e 45 56  gument.** to NEV
aa210 45 52 28 29 20 73 68 6f 75 6c 64 20 61 6c 77 61  ER() should alwa
aa220 79 73 20 62 65 20 66 61 6c 73 65 2e 20 20 49 66  ys be false.  If
aa230 20 65 69 74 68 65 72 20 69 73 20 6e 6f 74 20 74   either is not t
aa240 68 65 20 63 61 73 65 0a 2a 2a 20 74 68 65 6e 20  he case.** then 
aa250 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
aa260 63 61 6c 6c 65 64 20 69 6e 20 6f 72 64 65 72 20  called in order 
aa270 74 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f  to throw an erro
aa280 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
aa290 75 74 69 6e 65 20 6f 6e 6c 79 20 65 78 69 73 74  utine only exist
aa2a0 73 20 69 66 20 61 73 73 65 72 74 28 29 20 69 73  s if assert() is
aa2b0 20 6f 70 65 72 61 74 69 6f 6e 61 6c 2e 20 20 49   operational.  I
aa2c0 74 20 61 6c 77 61 79 73 0a 2a 2a 20 74 68 72 6f  t always.** thro
aa2d0 77 73 20 61 6e 20 61 73 73 65 72 74 20 6f 6e 20  ws an assert on 
aa2e0 69 74 73 20 66 69 72 73 74 20 69 6e 76 6f 63 61  its first invoca
aa2f0 74 69 6f 6e 2e 20 20 54 68 65 20 76 61 72 69 61  tion.  The varia
aa300 62 6c 65 20 68 61 73 20 61 20 6c 6f 6e 67 0a 2a  ble has a long.*
aa310 2a 20 6e 61 6d 65 20 74 6f 20 68 65 6c 70 20 74  * name to help t
aa320 68 65 20 61 73 73 65 72 74 28 29 20 6d 65 73 73  he assert() mess
aa330 61 67 65 20 62 65 20 6d 6f 72 65 20 72 65 61 64  age be more read
aa340 61 62 6c 65 2e 20 20 54 68 65 20 76 61 72 69 61  able.  The varia
aa350 62 6c 65 0a 2a 2a 20 69 73 20 75 73 65 64 20 74  ble.** is used t
aa360 6f 20 70 72 65 76 65 6e 74 20 61 20 74 6f 6f 2d  o prevent a too-
aa370 63 6c 65 76 65 72 20 6f 70 74 69 6d 69 7a 65 72  clever optimizer
aa380 20 66 72 6f 6d 20 6f 70 74 69 6d 69 7a 69 6e 67   from optimizing
aa390 20 6f 75 74 20 74 68 65 0a 2a 2a 20 65 6e 74 69   out the.** enti
aa3a0 72 65 20 63 61 6c 6c 2e 0a 2a 2f 0a 23 69 66 6e  re call..*/.#ifn
aa3b0 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54  def NDEBUG.SQLIT
aa3c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
aa3d0 6c 69 74 65 33 41 73 73 65 72 74 28 76 6f 69 64  lite3Assert(void
aa3e0 29 7b 0a 20 20 73 74 61 74 69 63 20 76 6f 6c 61  ){.  static vola
aa3f0 74 69 6c 65 20 69 6e 74 20 41 4c 57 41 59 53 5f  tile int ALWAYS_
aa400 77 61 73 5f 66 61 6c 73 65 5f 6f 72 5f 4e 45 56  was_false_or_NEV
aa410 45 52 5f 77 61 73 5f 74 72 75 65 20 3d 20 30 3b  ER_was_true = 0;
aa420 0a 20 20 61 73 73 65 72 74 28 20 41 4c 57 41 59  .  assert( ALWAY
aa430 53 5f 77 61 73 5f 66 61 6c 73 65 5f 6f 72 5f 4e  S_was_false_or_N
aa440 45 56 45 52 5f 77 61 73 5f 74 72 75 65 20 29 3b  EVER_was_true );
aa450 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20        /* Always 
aa460 66 61 69 6c 73 20 2a 2f 0a 20 20 72 65 74 75 72  fails */.  retur
aa470 6e 20 41 4c 57 41 59 53 5f 77 61 73 5f 66 61 6c  n ALWAYS_was_fal
aa480 73 65 5f 6f 72 5f 4e 45 56 45 52 5f 77 61 73 5f  se_or_NEVER_was_
aa490 74 72 75 65 2b 2b 3b 20 20 20 20 20 20 20 2f 2a  true++;       /*
aa4a0 20 4e 6f 74 20 52 65 61 63 68 65 64 20 2a 2f 0a   Not Reached */.
aa4b0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
aa4c0 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
aa4d0 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  he floating poin
aa4e0 74 20 76 61 6c 75 65 20 69 73 20 4e 6f 74 20 61  t value is Not a
aa4f0 20 4e 75 6d 62 65 72 20 28 4e 61 4e 29 2e 0a 2a   Number (NaN)..*
aa500 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 6d 61 74  *.** Use the mat
aa510 68 20 6c 69 62 72 61 72 79 20 69 73 6e 61 6e 28  h library isnan(
aa520 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f  ) function if co
aa530 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49  mpiled with SQLI
aa540 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a  TE_HAVE_ISNAN..*
aa550 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20  * Otherwise, we 
aa560 68 61 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70  have our own imp
aa570 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74  lementation that
aa580 20 77 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73   works on most s
aa590 79 73 74 65 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ystems..*/.SQLIT
aa5a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
aa5b0 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c  lite3IsNaN(doubl
aa5c0 65 20 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  e x){.  int rc; 
aa5d0 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72    /* The value r
aa5e0 65 74 75 72 6e 20 2a 2f 0a 23 69 66 20 21 64 65  eturn */.#if !de
aa5f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56  fined(SQLITE_HAV
aa600 45 5f 49 53 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20  E_ISNAN).  /*.  
aa610 2a 2a 20 53 79 73 74 65 6d 73 20 74 68 61 74 20  ** Systems that 
aa620 73 75 70 70 6f 72 74 20 74 68 65 20 69 73 6e 61  support the isna
aa630 6e 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63  n() library func
aa640 74 69 6f 6e 20 73 68 6f 75 6c 64 20 70 72 6f 62  tion should prob
aa650 61 62 6c 79 0a 20 20 2a 2a 20 6d 61 6b 65 20 75  ably.  ** make u
aa660 73 65 20 6f 66 20 69 74 20 62 79 20 63 6f 6d 70  se of it by comp
aa670 69 6c 69 6e 67 20 77 69 74 68 20 2d 44 53 51 4c  iling with -DSQL
aa680 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 20  ITE_HAVE_ISNAN. 
aa690 20 42 75 74 20 77 65 20 68 61 76 65 0a 20 20 2a   But we have.  *
aa6a0 2a 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 6e  * found that man
aa6b0 79 20 73 79 73 74 65 6d 73 20 64 6f 20 6e 6f 74  y systems do not
aa6c0 20 68 61 76 65 20 61 20 77 6f 72 6b 69 6e 67 20   have a working 
aa6d0 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e  isnan() function
aa6e0 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 69 6d   so.  ** this im
aa6f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
aa700 70 72 6f 76 69 64 65 64 20 61 73 20 61 6e 20 61  provided as an a
aa710 6c 74 65 72 6e 61 74 69 76 65 2e 0a 20 20 2a 2a  lternative..  **
aa720 0a 20 20 2a 2a 20 54 68 69 73 20 4e 61 4e 20 74  .  ** This NaN t
aa730 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61  est sometimes fa
aa740 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ils if compiled 
aa750 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61  on GCC with -ffa
aa760 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e  st-math..  ** On
aa770 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
aa780 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61   the use of -ffa
aa790 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69  st-math comes wi
aa7a0 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  th the following
aa7b0 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20  .  ** warning:. 
aa7c0 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68   **.  **      Th
aa7d0 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73  is option [-ffas
aa7e0 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e  t-math] should n
aa7f0 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f  ever be turned o
aa800 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20  n by any.  **   
aa810 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e     -O option sin
aa820 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74  ce it can result
aa830 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75   in incorrect ou
aa840 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d  tput for program
aa850 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63  s.  **      whic
aa860 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65  h depend on an e
aa870 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  xact implementat
aa880 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49  ion of IEEE or I
aa890 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75  SO .  **      ru
aa8a0 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f  les/specificatio
aa8b0 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63  ns for math func
aa8c0 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
aa8d0 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69   Under MSVC, thi
aa8e0 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66  s NaN test may f
aa8f0 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ail if compiled 
aa900 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d  with a floating-
aa910 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63  .  ** point prec
aa920 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72  ision mode other
aa930 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73   than /fp:precis
aa940 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44  e.  From the MSD
aa950 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74  N .  ** document
aa960 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ation:.  **.  **
aa970 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c        The compil
aa980 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65  er [with /fp:pre
aa990 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65  cise] will prope
aa9a0 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61  rly handle compa
aa9b0 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20  risons .  **    
aa9c0 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e    involving NaN.
aa9d0 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20   For example, x 
aa9e0 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74  != x evaluates t
aa9f0 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e  o true if x is N
aaa00 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e  aN .  **      ..
aaa10 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f  ..  */.#ifdef __
aaa20 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72  FAST_MATH__.# er
aaa30 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  ror SQLite will 
aaa40 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74  not work correct
aaa50 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61  ly with the -ffa
aaa60 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f  st-math option o
aaa70 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20  f GCC..#endif.  
aaa80 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20  volatile double 
aaa90 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c  y = x;.  volatil
aaaa0 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a  e double z = y;.
aaab0 20 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0a 23    rc = (y!=z);.#
aaac0 65 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69  else  /* if defi
aaad0 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f  ned(SQLITE_HAVE_
aaae0 49 53 4e 41 4e 29 20 2a 2f 0a 20 20 72 63 20 3d  ISNAN) */.  rc =
aaaf0 20 69 73 6e 61 6e 28 78 29 3b 0a 23 65 6e 64 69   isnan(x);.#endi
aab00 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 56 45  f /* SQLITE_HAVE
aab10 5f 49 53 4e 41 4e 20 2a 2f 0a 20 20 74 65 73 74  _ISNAN */.  test
aab20 63 61 73 65 28 20 72 63 20 29 3b 0a 20 20 72 65  case( rc );.  re
aab30 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
aab40 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69  * Compute a stri
aab50 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 69  ng length that i
aab60 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 61  s limited to wha
aab70 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20  t can be stored 
aab80 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 62  in.** lower 30 b
aab90 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 20  its of a 32-bit 
aaba0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a  signed integer..
aabb0 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
aabc0 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 65  returned will ne
aabd0 76 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 2e  ver be negative.
aabe0 20 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 76    Nor will it ev
aabf0 65 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a 2a  er be greater.**
aac00 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c   than the actual
aac10 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73   length of the s
aac20 74 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 79  tring.  For very
aac30 20 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 67   long strings (g
aac40 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 31  reater.** than 1
aac50 47 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 72  GiB) the value r
aac60 65 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 65  eturned might be
aac70 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74   less than the t
aac80 72 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74  rue string lengt
aac90 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  h..*/.SQLITE_PRI
aaca0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
aacb0 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63  Strlen30(const c
aacc0 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74  har *z){.  const
aacd0 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20   char *z2 = z;. 
aace0 20 77 68 69 6c 65 28 20 2a 7a 32 20 29 7b 20 7a   while( *z2 ){ z
aacf0 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  2++; }.  return 
aad00 30 78 33 66 66 66 66 66 66 66 20 26 20 28 69 6e  0x3fffffff & (in
aad10 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 7d 0a 0a 2f  t)(z2 - z);.}../
aad20 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 6f 73  *.** Set the mos
aad30 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63  t recent error c
aad40 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 73 74  ode and error st
aad50 72 69 6e 67 20 66 6f 72 20 74 68 65 20 73 71 6c  ring for the sql
aad60 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 22 64  ite.** handle "d
aad70 62 22 2e 20 54 68 65 20 65 72 72 6f 72 20 63 6f  b". The error co
aad80 64 65 20 69 73 20 73 65 74 20 74 6f 20 22 65 72  de is set to "er
aad90 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 49  r_code"..**.** I
aada0 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f it is not NULL
aadb0 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d 61 74  , string zFormat
aadc0 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 66   specifies the f
aadd0 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a 2a 20  ormat of the.** 
aade0 65 72 72 6f 72 20 73 74 72 69 6e 67 20 69 6e 20  error string in 
aadf0 74 68 65 20 73 74 79 6c 65 20 6f 66 20 74 68 65  the style of the
aae00 20 70 72 69 6e 74 66 20 66 75 6e 63 74 69 6f 6e   printf function
aae10 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  s: The following
aae20 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61 72 61  .** format chara
aae30 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65  cters are allowe
aae40 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73  d:.**.**      %s
aae50 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 73        Insert a s
aae60 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a  tring.**      %z
aae70 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74        A string t
aae80 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72  hat should be fr
aae90 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a  eed after use.**
aaea0 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e        %d      In
aaeb0 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a  sert an integer.
aaec0 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 20  **      %T      
aaed0 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a  Insert a token.*
aaee0 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20 49  *      %S      I
aaef0 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74 20  nsert the first 
aaf00 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63  element of a Src
aaf10 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 6d  List.**.** zForm
aaf20 61 74 20 61 6e 64 20 61 6e 79 20 73 74 72 69 6e  at and any strin
aaf30 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 66 6f  g tokens that fo
aaf40 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 73 73 75  llow it are assu
aaf50 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65 6e 63  med to be.** enc
aaf60 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a  oded in UTF-8..*
aaf70 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20 74 68  *.** To clear th
aaf80 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  e most recent er
aaf90 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 65 20 68  ror for sqlite h
aafa0 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 71 6c 69  andle "db", sqli
aafb0 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68 6f 75  te3Error.** shou
aafc0 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  ld be called wit
aafd0 68 20 65 72 72 5f 63 6f 64 65 20 73 65 74 20 74  h err_code set t
aafe0 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20  o SQLITE_OK and 
aaff0 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a 20 74  zFormat set.** t
ab000 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54  o NULL..*/.SQLIT
ab010 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
ab020 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71 6c 69  qlite3Error(sqli
ab030 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 72 72  te3 *db, int err
ab040 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63 68 61  _code, const cha
ab050 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
ab060 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 64  {.  if( db && (d
ab070 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62 2d 3e  b->pErr || (db->
ab080 70 45 72 72 20 3d 20 73 71 6c 69 74 65 33 56 61  pErr = sqlite3Va
ab090 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 30 29 20  lueNew(db))!=0) 
ab0a0 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72 43 6f  ){.    db->errCo
ab0b0 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b 0a 20  de = err_code;. 
ab0c0 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 74 20 29     if( zFormat )
ab0d0 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b  {.      char *z;
ab0e0 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74 20 61  .      va_list a
ab0f0 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 74 61 72  p;.      va_star
ab100 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
ab110 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
ab120 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  3VMPrintf(db, zF
ab130 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 20  ormat, ap);.    
ab140 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
ab150 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
ab160 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c  SetStr(db->pErr,
ab170 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 5f 55   -1, z, SQLITE_U
ab180 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41  TF8, SQLITE_DYNA
ab190 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  MIC);.    }else{
ab1a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
ab1b0 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45  lueSetStr(db->pE
ab1c0 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45  rr, 0, 0, SQLITE
ab1d0 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54  _UTF8, SQLITE_ST
ab1e0 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ATIC);.    }.  }
ab1f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e  .}../*.** Add an
ab200 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
ab210 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  o pParse->zErrMs
ab220 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20  g and increment 
ab230 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a  pParse->nErr..**
ab240 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
ab250 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 72 61 63  ormatting charac
ab260 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64  ters are allowed
ab270 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20  :.**.**      %s 
ab280 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 73 74       Insert a st
ab290 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20  ring.**      %z 
ab2a0 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 74 68       A string th
ab2b0 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65  at should be fre
ab2c0 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20  ed after use.** 
ab2d0 20 20 20 20 20 25 64 20 20 20 20 20 20 49 6e 73       %d      Ins
ab2e0 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a  ert an integer.*
ab2f0 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 20 49  *      %T      I
ab300 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a  nsert a token.**
ab310 20 20 20 20 20 20 25 53 20 20 20 20 20 20 49 6e        %S      In
ab320 73 65 72 74 20 74 68 65 20 66 69 72 73 74 20 65  sert the first e
ab330 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c  lement of a SrcL
ab340 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ist.**.** This f
ab350 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62  unction should b
ab360 65 20 75 73 65 64 20 74 6f 20 72 65 70 6f 72 74  e used to report
ab370 20 61 6e 79 20 65 72 72 6f 72 20 74 68 61 74 20   any error that 
ab380 6f 63 63 75 72 73 20 77 68 69 6c 73 74 0a 2a 2a  occurs whilst.**
ab390 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 53 51   compiling an SQ
ab3a0 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 69 2e 65  L statement (i.e
ab3b0 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33  . within sqlite3
ab3c0 5f 70 72 65 70 61 72 65 28 29 29 2e 20 54 68 65  _prepare()). The
ab3d0 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 20 74  .** last thing t
ab3e0 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  he sqlite3_prepa
ab3f0 72 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 64 6f  re() function do
ab400 65 73 20 69 73 20 63 6f 70 79 20 74 68 65 20 65  es is copy the e
ab410 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 20 62  rror.** stored b
ab420 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  y this function 
ab430 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
ab440 65 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 20 73  e handle using s
ab450 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a 2a  qlite3Error()..*
ab460 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74  * Function sqlit
ab470 65 33 45 72 72 6f 72 28 29 20 73 68 6f 75 6c 64  e3Error() should
ab480 20 62 65 20 75 73 65 64 20 64 75 72 69 6e 67 20   be used during 
ab490 73 74 61 74 65 6d 65 6e 74 20 65 78 65 63 75 74  statement execut
ab4a0 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 33 5f  ion.** (sqlite3_
ab4b0 73 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 2a 2f  step() etc.)..*/
ab4c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ab4d0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f  void sqlite3Erro
ab4e0 72 4d 73 67 28 50 61 72 73 65 20 2a 70 50 61 72  rMsg(Parse *pPar
ab4f0 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
ab500 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
ab510 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 73   va_list ap;.  s
ab520 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
ab530 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61 72 73  rse->db;.  pPars
ab540 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 74 65 73  e->nErr++;.  tes
ab550 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e 7a  tcase( pParse->z
ab560 45 72 72 4d 73 67 21 3d 30 20 29 3b 0a 20 20 73  ErrMsg!=0 );.  s
ab570 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
ab580 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
ab590 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  );.  va_start(ap
ab5a0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50  , zFormat);.  pP
ab5b0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
ab5c0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
ab5d0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
ab5e0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
ab5f0 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63    if( pParse->rc
ab600 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
ab610 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
ab620 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
ab630 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  }.}../*.** Clear
ab640 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
ab650 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66  ge in pParse, if
ab660 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   any.*/.SQLITE_P
ab670 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
ab680 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61  te3ErrorClear(Pa
ab690 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
ab6a0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50  sqlite3DbFree(pP
ab6b0 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65  arse->db, pParse
ab6c0 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50  ->zErrMsg);.  pP
ab6d0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
ab6e0 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  0;.  pParse->nEr
ab6f0 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r = 0;.}../*.** 
ab700 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73  Convert an SQL-s
ab710 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69  tyle quoted stri
ab720 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c  ng into a normal
ab730 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76   string by remov
ab740 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65  ing.** the quote
ab750 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68   characters.  Th
ab760 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
ab770 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20  done in-place.  
ab780 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20  If the.** input 
ab790 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77  does not begin w
ab7a0 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72  ith a quote char
ab7b0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73  acter, then this
ab7c0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61   routine.** is a
ab7d0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68   no-op..**.** Th
ab7e0 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d  e input string m
ab7f0 75 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d  ust be zero-term
ab800 69 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a  inated.  A new z
ab810 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0a 2a  ero-terminator.*
ab820 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  * is added to th
ab830 65 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e  e dequoted strin
ab840 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  g..**.** The ret
ab850 75 72 6e 20 76 61 6c 75 65 20 69 73 20 2d 31 20  urn value is -1 
ab860 69 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20  if no dequoting 
ab870 6f 63 63 75 72 73 20 6f 72 20 74 68 65 20 6c 65  occurs or the le
ab880 6e 67 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64  ngth of the.** d
ab890 65 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2c 20  equoted string, 
ab8a0 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
ab8b0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
ab8c0 2c 20 69 66 20 64 65 71 75 6f 74 69 6e 67 20 64  , if dequoting d
ab8d0 6f 65 73 0a 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a  oes.** occur..**
ab8e0 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34 3a  .** 2002-Feb-14:
ab8f0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
ab900 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65 6d   extended to rem
ab910 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73 74  ove MS-Access st
ab920 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73 20  yle.** brackets 
ab930 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65 6e  from around iden
ab940 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78 61  tifers.  For exa
ab950 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d 22  mple:  "[a-b-c]"
ab960 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62   becomes.** "a-b
ab970 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  -c"..*/.SQLITE_P
ab980 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ab990 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a  e3Dequote(char *
ab9a0 7a 29 7b 0a 20 20 63 68 61 72 20 71 75 6f 74 65  z){.  char quote
ab9b0 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
ab9c0 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
ab9d0 6e 20 2d 31 3b 0a 20 20 71 75 6f 74 65 20 3d 20  n -1;.  quote = 
ab9e0 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20  z[0];.  switch( 
ab9f0 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73  quote ){.    cas
aba00 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a  e '\'':  break;.
aba10 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20      case '"':   
aba20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
aba30 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20  '`':   break;   
aba40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
aba50 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74  For MySQL compat
aba60 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63  ibility */.    c
aba70 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65  ase '[':   quote
aba80 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20   = ']';  break; 
aba90 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65   /* For MS SqlSe
abaa0 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69  rver compatibili
abab0 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c  ty */.    defaul
abac0 74 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b  t:    return -1;
abad0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20  .  }.  for(i=1, 
abae0 6a 3d 30 3b 20 41 4c 57 41 59 53 28 7a 5b 69 5d  j=0; ALWAYS(z[i]
abaf0 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ); i++){.    if(
abb00 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a   z[i]==quote ){.
abb10 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d        if( z[i+1]
abb20 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20  ==quote ){.     
abb30 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74     z[j++] = quot
abb40 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a  e;.        i++;.
abb50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
abb60 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
abb70 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
abb80 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b       z[j++] = z[
abb90 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  i];.    }.  }.  
abba0 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75  z[j] = 0;.  retu
abbb0 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76  rn j;.}../* Conv
abbc0 65 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e  enient short-han
abbd0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70  d */.#define Upp
abbe0 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65  erToLower sqlite
abbf0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f  3UpperToLower../
abc00 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d  *.** Some system
abc10 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29  s have stricmp()
abc20 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73  .  Others have s
abc30 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65  trcasecmp().  Be
abc40 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69  cause.** there i
abc50 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79  s no consistency
abc60 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65  , we will define
abc70 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c   our own..*/.SQL
abc80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
abc90 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63  sqlite3StrICmp(c
abca0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
abcb0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
abcc0 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65  ight){.  registe
abcd0 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
abce0 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
abcf0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
abd00 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
abd10 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
abd20 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61  ght;.  while( *a
abd30 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f  !=0 && UpperToLo
abd40 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f  wer[*a]==UpperTo
abd50 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b  Lower[*b]){ a++;
abd60 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e   b++; }.  return
abd70 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
abd80 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ] - UpperToLower
abd90 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  [*b];.}.SQLITE_P
abda0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
abdb0 65 33 53 74 72 4e 49 43 6d 70 28 63 6f 6e 73 74  e3StrNICmp(const
abdc0 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f   char *zLeft, co
abdd0 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74  nst char *zRight
abde0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69  , int N){.  regi
abdf0 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 63 68  ster unsigned ch
abe00 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d  ar *a, *b;.  a =
abe10 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
abe20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28  *)zLeft;.  b = (
abe30 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
abe40 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c 65 28  zRight;.  while(
abe50 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d   N-- > 0 && *a!=
abe60 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f 77 65  0 && UpperToLowe
abe70 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f  r[*a]==UpperToLo
abe80 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62  wer[*b]){ a++; b
abe90 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e  ++; }.  return N
abea0 3c 30 20 3f 20 30 20 3a 20 55 70 70 65 72 54 6f  <0 ? 0 : UpperTo
abeb0 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65  Lower[*a] - Uppe
abec0 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a  rToLower[*b];.}.
abed0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
abee0 55 45 20 69 66 20 7a 20 69 73 20 61 20 70 75 72  UE if z is a pur
abef0 65 20 6e 75 6d 65 72 69 63 20 73 74 72 69 6e 67  e numeric string
abf00 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20  .  Return FALSE 
abf10 61 6e 64 20 6c 65 61 76 65 0a 2a 2a 20 2a 72 65  and leave.** *re
abf20 61 6c 6e 75 6d 20 75 6e 63 68 61 6e 67 65 64 20  alnum unchanged 
abf30 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 63 6f  if the string co
abf40 6e 74 61 69 6e 73 20 61 6e 79 20 63 68 61 72 61  ntains any chara
abf50 63 74 65 72 20 77 68 69 63 68 20 69 73 20 6e 6f  cter which is no
abf60 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 6e  t.** part of a n
abf70 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  umber..**.** If 
abf80 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 70 75  the string is pu
abf90 72 65 20 6e 75 6d 65 72 69 63 2c 20 73 65 74 20  re numeric, set 
abfa0 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 54 52 55 45  *realnum to TRUE
abfb0 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a   if the string.*
abfc0 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 27  * contains the '
abfd0 2e 27 20 63 68 61 72 61 63 74 65 72 20 6f 72 20  .' character or 
abfe0 61 6e 20 22 45 2b 30 30 30 22 20 73 74 79 6c 65  an "E+000" style
abff0 20 65 78 70 6f 6e 65 6e 74 69 61 74 69 6f 6e 20   exponentiation 
ac000 73 75 66 66 69 78 2e 0a 2a 2a 20 4f 74 68 65 72  suffix..** Other
ac010 77 69 73 65 20 73 65 74 20 2a 72 65 61 6c 6e 75  wise set *realnu
ac020 6d 20 74 6f 20 46 41 4c 53 45 2e 20 20 4e 6f 74  m to FALSE.  Not
ac030 65 20 74 68 61 74 20 6a 75 73 74 20 62 65 63 61  e that just beca
ac040 75 65 20 2a 72 65 61 6c 6e 75 6d 20 69 73 0a 2a  ue *realnum is.*
ac050 2a 20 66 61 6c 73 65 20 64 6f 65 73 20 6e 6f 74  * false does not
ac060 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6e   mean that the n
ac070 75 6d 62 65 72 20 63 61 6e 20 62 65 20 73 75 63  umber can be suc
ac080 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 76 65 72  cessfully conver
ac090 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 69  ted into.** an i
ac0a0 6e 74 65 67 65 72 20 2d 20 69 74 20 6d 69 67 68  nteger - it migh
ac0b0 74 20 62 65 20 74 6f 6f 20 62 69 67 2e 0a 2a 2a  t be too big..**
ac0c0 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20 73 74 72  .** An empty str
ac0d0 69 6e 67 20 69 73 20 63 6f 6e 73 69 64 65 72 65  ing is considere
ac0e0 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a  d non-numeric..*
ac0f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
ac100 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75   int sqlite3IsNu
ac110 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20  mber(const char 
ac120 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d  *z, int *realnum
ac130 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74  , u8 enc){.  int
ac140 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51   incr = (enc==SQ
ac150 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a  LITE_UTF8?1:2);.
ac160 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54    if( enc==SQLIT
ac170 45 5f 55 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b  E_UTF16BE ) z++;
ac180 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c  .  if( *z=='-' |
ac190 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d  | *z=='+' ) z +=
ac1a0 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 21 73 71   incr;.  if( !sq
ac1b0 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29  lite3Isdigit(*z)
ac1c0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
ac1d0 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63  ;.  }.  z += inc
ac1e0 72 3b 0a 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20  r;.  *realnum = 
ac1f0 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69  0;.  while( sqli
ac200 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29  te3Isdigit(*z) )
ac210 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20  { z += incr; }. 
ac220 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a   if( *z=='.' ){.
ac230 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20      z += incr;. 
ac240 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
ac250 73 64 69 67 69 74 28 2a 7a 29 20 29 20 72 65 74  sdigit(*z) ) ret
ac260 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  urn 0;.    while
ac270 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
ac280 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63  (*z) ){ z += inc
ac290 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c 6e 75  r; }.    *realnu
ac2a0 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  m = 1;.  }.  if(
ac2b0 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d   *z=='e' || *z==
ac2c0 27 45 27 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20  'E' ){.    z += 
ac2d0 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a  incr;.    if( *z
ac2e0 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27  =='+' || *z=='-'
ac2f0 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20   ) z += incr;.  
ac300 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
ac310 64 69 67 69 74 28 2a 7a 29 20 29 20 72 65 74 75  digit(*z) ) retu
ac320 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28  rn 0;.    while(
ac330 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
ac340 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72  *z) ){ z += incr
ac350 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d  ; }.    *realnum
ac360 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
ac370 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  rn *z==0;.}../*.
ac380 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 5b  ** The string z[
ac390 5d 20 69 73 20 61 6e 20 61 73 63 69 69 20 72 65  ] is an ascii re
ac3a0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
ac3b0 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a  a real number..*
ac3c0 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73 20 73  * Convert this s
ac3d0 74 72 69 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c  tring to a doubl
ac3e0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
ac3f0 75 74 69 6e 65 20 61 73 73 75 6d 65 73 20 74 68  utine assumes th
ac400 61 74 20 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73  at z[] really is
ac410 20 61 20 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e   a valid number.
ac420 20 20 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f    If it.** is no
ac430 74 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  t, the result is
ac440 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
ac450 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
ac460 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f  s used instead o
ac470 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 61 74  f the library at
ac480 6f 66 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65  of() function be
ac490 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62  cause.** the lib
ac4a0 72 61 72 79 20 61 74 6f 66 28 29 20 6d 69 67 68  rary atof() migh
ac4b0 74 20 77 61 6e 74 20 74 6f 20 75 73 65 20 22 2c  t want to use ",
ac4c0 22 20 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c  " as the decimal
ac4d0 20 70 6f 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a   point instead.*
ac4e0 2a 20 6f 66 20 22 2e 22 20 64 65 70 65 6e 64 69  * of "." dependi
ac4f0 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65  ng on how locale
ac500 20 69 73 20 73 65 74 2e 20 20 42 75 74 20 74 68   is set.  But th
ac510 61 74 20 77 6f 75 6c 64 20 63 61 75 73 65 20 70  at would cause p
ac520 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53  roblems.** for S
ac530 51 4c 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75  QL.  So this rou
ac540 74 69 6e 65 20 61 6c 77 61 79 73 20 75 73 65 73  tine always uses
ac550 20 22 2e 22 20 72 65 67 61 72 64 6c 65 73 73 20   "." regardless 
ac560 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51  of locale..*/.SQ
ac570 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac580 20 73 71 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e   sqlite3AtoF(con
ac590 73 74 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62  st char *z, doub
ac5a0 6c 65 20 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69  le *pResult){.#i
ac5b0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
ac5c0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
ac5d0 0a 20 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b  .  int sign = 1;
ac5e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
ac5f0 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 20 4c 4f 4e  Begin = z;.  LON
ac600 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 76 31 20  GDOUBLE_TYPE v1 
ac610 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20 6e 53 69  = 0.0;.  int nSi
ac620 67 6e 69 66 69 63 61 6e 74 20 3d 20 30 3b 0a 20  gnificant = 0;. 
ac630 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
ac640 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b  sspace(*z) ) z++
ac650 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20  ;.  if( *z=='-' 
ac660 29 7b 0a 20 20 20 20 73 69 67 6e 20 3d 20 2d 31  ){.    sign = -1
ac670 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c  ;.    z++;.  }el
ac680 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29  se if( *z=='+' )
ac690 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20  {.    z++;.  }. 
ac6a0 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30   while( z[0]=='0
ac6b0 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20  ' ){.    z++;.  
ac6c0 7d 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74  }.  while( sqlit
ac6d0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b  e3Isdigit(*z) ){
ac6e0 0a 20 20 20 20 76 31 20 3d 20 76 31 2a 31 30 2e  .    v1 = v1*10.
ac6f0 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a  0 + (*z - '0');.
ac700 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 6e 53 69      z++;.    nSi
ac710 67 6e 69 66 69 63 61 6e 74 2b 2b 3b 0a 20 20 7d  gnificant++;.  }
ac720 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20 29  .  if( *z=='.' )
ac730 7b 0a 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45  {.    LONGDOUBLE
ac740 5f 54 59 50 45 20 64 69 76 69 73 6f 72 20 3d 20  _TYPE divisor = 
ac750 31 2e 30 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20  1.0;.    z++;.  
ac760 20 20 69 66 28 20 6e 53 69 67 6e 69 66 69 63 61    if( nSignifica
ac770 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 77  nt==0 ){.      w
ac780 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20  hile( z[0]=='0' 
ac790 29 7b 0a 20 20 20 20 20 20 20 20 64 69 76 69 73  ){.        divis
ac7a0 6f 72 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20  or *= 10.0;.    
ac7b0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 7d      z++;.      }
ac7c0 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65  .    }.    while
ac7d0 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
ac7e0 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 69 66  (*z) ){.      if
ac7f0 28 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 3c 31  ( nSignificant<1
ac800 38 20 29 7b 0a 20 20 20 20 20 20 20 20 76 31 20  8 ){.        v1 
ac810 3d 20 76 31 2a 31 30 2e 30 20 2b 20 28 2a 7a 20  = v1*10.0 + (*z 
ac820 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20 20  - '0');.        
ac830 64 69 76 69 73 6f 72 20 2a 3d 20 31 30 2e 30 3b  divisor *= 10.0;
ac840 0a 20 20 20 20 20 20 20 20 6e 53 69 67 6e 69 66  .        nSignif
ac850 69 63 61 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d  icant++;.      }
ac860 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
ac870 7d 0a 20 20 20 20 76 31 20 2f 3d 20 64 69 76 69  }.    v1 /= divi
ac880 73 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a  sor;.  }.  if( *
ac890 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45  z=='e' || *z=='E
ac8a0 27 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 73 69  ' ){.    int esi
ac8b0 67 6e 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20  gn = 1;.    int 
ac8c0 65 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 4c 4f  eval = 0;.    LO
ac8d0 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 63  NGDOUBLE_TYPE sc
ac8e0 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 7a  ale = 1.0;.    z
ac8f0 2b 2b 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d  ++;.    if( *z==
ac900 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73 69  '-' ){.      esi
ac910 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a  gn = -1;.      z
ac920 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ++;.    }else if
ac930 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20  ( *z=='+' ){.   
ac940 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     z++;.    }.  
ac950 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
ac960 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20  Isdigit(*z) ){. 
ac970 20 20 20 20 20 65 76 61 6c 20 3d 20 65 76 61 6c       eval = eval
ac980 2a 31 30 20 2b 20 2a 7a 20 2d 20 27 30 27 3b 0a  *10 + *z - '0';.
ac990 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d        z++;.    }
ac9a0 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76 61 6c  .    while( eval
ac9b0 3e 3d 36 34 20 29 7b 20 73 63 61 6c 65 20 2a 3d  >=64 ){ scale *=
ac9c0 20 31 2e 30 65 2b 36 34 3b 20 65 76 61 6c 20 2d   1.0e+64; eval -
ac9d0 3d 20 36 34 3b 20 7d 0a 20 20 20 20 77 68 69 6c  = 64; }.    whil
ac9e0 65 28 20 65 76 61 6c 3e 3d 31 36 20 29 7b 20 73  e( eval>=16 ){ s
ac9f0 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 36 3b  cale *= 1.0e+16;
aca00 20 65 76 61 6c 20 2d 3d 20 31 36 3b 20 7d 0a 20   eval -= 16; }. 
aca10 20 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d     while( eval>=
aca20 34 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e  4 ){ scale *= 1.
aca30 30 65 2b 34 3b 20 65 76 61 6c 20 2d 3d 20 34 3b  0e+4; eval -= 4;
aca40 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 65 76   }.    while( ev
aca50 61 6c 3e 3d 31 20 29 7b 20 73 63 61 6c 65 20 2a  al>=1 ){ scale *
aca60 3d 20 31 2e 30 65 2b 31 3b 20 65 76 61 6c 20 2d  = 1.0e+1; eval -
aca70 3d 20 31 3b 20 7d 0a 20 20 20 20 69 66 28 20 65  = 1; }.    if( e
aca80 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20  sign<0 ){.      
aca90 76 31 20 2f 3d 20 73 63 61 6c 65 3b 0a 20 20 20  v1 /= scale;.   
acaa0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 31   }else{.      v1
acab0 20 2a 3d 20 73 63 61 6c 65 3b 0a 20 20 20 20 7d   *= scale;.    }
acac0 0a 20 20 7d 0a 20 20 2a 70 52 65 73 75 6c 74 20  .  }.  *pResult 
acad0 3d 20 28 64 6f 75 62 6c 65 29 28 73 69 67 6e 3c  = (double)(sign<
acae0 30 20 3f 20 2d 76 31 20 3a 20 76 31 29 3b 0a 20  0 ? -v1 : v1);. 
acaf0 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 20   return (int)(z 
acb00 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65  - zBegin);.#else
acb10 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
acb20 33 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75  3Atoi64(z, pResu
acb30 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  lt);.#endif /* S
acb40 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
acb50 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a  ING_POINT */.}..
acb60 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68  /*.** Compare th
acb70 65 20 31 39 2d 63 68 61 72 61 63 74 65 72 20 73  e 19-character s
acb80 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e  tring zNum again
acb90 73 74 20 74 68 65 20 74 65 78 74 20 72 65 70 72  st the text repr
acba0 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61  esentation.** va
acbb0 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33  lue 2^63:  92233
acbc0 37 32 30 33 36 38 35 34 37 37 35 38 30 38 2e 20  72036854775808. 
acbd0 20 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65   Return negative
acbe0 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74  , zero, or posit
acbf0 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69  ive.** if zNum i
acc00 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75  s less than, equ
acc10 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65  al to, or greate
acc20 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e  r than the strin
acc30 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20  g..**.** Unlike 
acc40 6d 65 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f  memcmp() this ro
acc50 75 74 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74  utine is guarant
acc60 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68  eed to return th
acc70 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20  e difference.** 
acc80 69 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  in the values of
acc90 20 74 68 65 20 6c 61 73 74 20 64 69 67 69 74 20   the last digit 
acca0 69 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66  if the only diff
accb0 65 72 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65  erence is in the
accc0 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20  .** last digit. 
accd0 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65   So, for example
acce0 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d  ,.**.**      com
accf0 70 61 72 65 32 70 6f 77 36 33 28 22 39 32 32 33  pare2pow63("9223
acd00 33 37 32 30 33 36 38 35 34 37 37 35 38 30 30 22  372036854775800"
acd10 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74  ).**.** will ret
acd20 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69  urn -8..*/.stati
acd30 63 20 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f  c int compare2po
acd40 77 36 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  w63(const char *
acd50 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a  zNum){.  int c;.
acd60 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75    c = memcmp(zNu
acd70 6d 2c 22 39 32 32 33 33 37 32 30 33 36 38 35 34  m,"9223372036854
acd80 37 37 35 38 30 22 2c 31 38 29 3b 0a 20 20 69 66  77580",18);.  if
acd90 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20  ( c==0 ){.    c 
acda0 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38 27  = zNum[18] - '8'
acdb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
acdc0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ;.}.../*.** Retu
acdd0 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d 20  rn TRUE if zNum 
acde0 69 73 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e  is a 64-bit sign
acdf0 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20 77  ed integer and w
ace00 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75  rite.** the valu
ace10 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  e of the integer
ace20 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49 66   into *pNum.  If
ace30 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e 20   zNum is not an 
ace40 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69 73  integer.** or is
ace50 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74   an integer that
ace60 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f   is too large to
ace70 20 62 65 20 65 78 70 72 65 73 73 65 64 20 77 69   be expressed wi
ace80 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20 74  th 64 bits,.** t
ace90 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65  hen return false
acea0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
aceb0 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f 72  s routine was or
acec0 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 6e  iginally written
aced0 20 69 74 20 64 65 61 6c 74 20 77 69 74 68 20 6f   it dealt with o
acee0 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e 75  nly.** 32-bit nu
acef0 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61 74 20  mbers.  At that 
acf00 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75 63  time, it was muc
acf10 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 68  h faster than th
acf20 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62 72  e.** atoi() libr
acf30 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 52  ary routine in R
acf40 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51  edHat 7.2..*/.SQ
acf50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
acf60 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 63   sqlite3Atoi64(c
acf70 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c  onst char *zNum,
acf80 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20 69   i64 *pNum){.  i
acf90 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20  64 v = 0;.  int 
acfa0 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b  neg;.  int i, c;
acfb0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
acfc0 53 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 28 20  Start;.  while( 
acfd0 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a  sqlite3Isspace(*
acfe0 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a  zNum) ) zNum++;.
acff0 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27    if( *zNum=='-'
ad000 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b   ){.    neg = 1;
ad010 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d  .    zNum++;.  }
ad020 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d  else if( *zNum==
ad030 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d  '+' ){.    neg =
ad040 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a   0;.    zNum++;.
ad050 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67    }else{.    neg
ad060 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 53 74 61   = 0;.  }.  zSta
ad070 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 77 68 69  rt = zNum;.  whi
ad080 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27  le( zNum[0]=='0'
ad090 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a   ){ zNum++; } /*
ad0a0 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 61 64 69   Skip over leadi
ad0b0 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 6b 65 74  ng zeros. Ticket
ad0c0 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 6f 72 28   #2454 */.  for(
ad0d0 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29  i=0; (c=zNum[i])
ad0e0 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 3b  >='0' && c<='9';
ad0f0 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76   i++){.    v = v
ad100 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20  *10 + c - '0';. 
ad110 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 6e 65 67   }.  *pNum = neg
ad120 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 69 66 28   ? -v : v;.  if(
ad130 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d 30 20 26   c!=0 || (i==0 &
ad140 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 6d 29 20  & zStart==zNum) 
ad150 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f  || i>19 ){.    /
ad160 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74 79 20  * zNum is empty 
ad170 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d  or contains non-
ad180 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f 72 20  numeric text or 
ad190 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a  is longer.    **
ad1a0 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20   than 19 digits 
ad1b0 28 74 68 75 73 20 67 75 61 72 61 6e 74 69 6e 67  (thus guaranting
ad1c0 20 74 68 61 74 20 69 74 20 69 73 20 74 6f 6f 20   that it is too 
ad1d0 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20 72 65  large) */.    re
ad1e0 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20  turn 0;.  }else 
ad1f0 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20  if( i<19 ){.    
ad200 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39 20  /* Less than 19 
ad210 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b 6e  digits, so we kn
ad220 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73 20  ow that it fits 
ad230 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20 20  in 64 bits */.  
ad240 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
ad250 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39 2d 64  lse{.    /* 19-d
ad260 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d 75 73  igit numbers mus
ad270 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72 20 74  t be no larger t
ad280 68 61 6e 20 39 32 32 33 33 37 32 30 33 36 38 35  han 922337203685
ad290 34 37 37 35 38 30 37 20 69 66 20 70 6f 73 69 74  4775807 if posit
ad2a0 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20 39 32  ive.    ** or 92
ad2b0 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30  2337203685477580
ad2c0 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20 20  8 if negative.  
ad2d0 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33 33 37  Note that 922337
ad2e0 32 30 33 36 38 35 34 36 36 35 38 30 38 0a 20 20  2036854665808.  
ad2f0 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20 2a 2f    ** is 2^63. */
ad300 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d 70  .    return comp
ad310 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c  are2pow63(zNum)<
ad320 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  neg;.  }.}../*.*
ad330 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 4e 75  * The string zNu
ad340 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  m represents an 
ad350 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
ad360 2e 20 20 54 68 65 72 65 20 6d 69 67 68 74 20 62  .  There might b
ad370 65 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20  e some other.** 
ad380 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 6c 6c  information foll
ad390 6f 77 69 6e 67 20 74 68 65 20 69 6e 74 65 67 65  owing the intege
ad3a0 72 20 74 6f 6f 2c 20 62 75 74 20 74 68 61 74 20  r too, but that 
ad3b0 70 61 72 74 20 69 73 20 69 67 6e 6f 72 65 64 2e  part is ignored.
ad3c0 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 67  .** If the integ
ad3d0 65 72 20 74 68 61 74 20 74 68 65 20 70 72 65 66  er that the pref
ad3e0 69 78 20 6f 66 20 7a 4e 75 6d 20 72 65 70 72 65  ix of zNum repre
ad3f0 73 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20 69  sents will fit i
ad400 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73 69  n a.** 64-bit si
ad410 67 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72 65  gned integer, re
ad420 74 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68 65  turn TRUE.  Othe
ad430 72 77 69 73 65 20 72 65 74 75 72 6e 20 46 41 4c  rwise return FAL
ad440 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  SE..**.** If the
ad450 20 6e 65 67 46 6c 61 67 20 70 61 72 61 6d 65 74   negFlag paramet
ad460 65 72 20 69 73 20 74 72 75 65 2c 20 74 68 61 74  er is true, that
ad470 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 4e 75 6d   means that zNum
ad480 20 72 65 61 6c 6c 79 20 72 65 70 72 65 73 65 6e   really represen
ad490 74 73 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76 65  ts.** a negative
ad4a0 20 6e 75 6d 62 65 72 2e 20 20 28 54 68 65 20 6c   number.  (The l
ad4b0 65 61 64 69 6e 67 20 22 2d 22 20 69 73 20 6f 6d  eading "-" is om
ad4c0 69 74 74 65 64 20 66 72 6f 6d 20 7a 4e 75 6d 2e  itted from zNum.
ad4d0 29 20 20 54 68 69 73 0a 2a 2a 20 70 61 72 61 6d  )  This.** param
ad4e0 65 74 65 72 20 69 73 20 6e 65 65 64 65 64 20 74  eter is needed t
ad4f0 6f 20 64 65 74 65 72 6d 69 6e 65 20 61 20 62 6f  o determine a bo
ad500 75 6e 64 61 72 79 20 63 61 73 65 2e 20 20 41 20  undary case.  A 
ad510 73 74 72 69 6e 67 0a 2a 2a 20 6f 66 20 22 39 32  string.** of "92
ad520 32 33 33 37 33 30 33 36 38 35 34 37 37 35 38 30  2337303685477580
ad530 38 22 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65  8" returns false
ad540 20 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 66   if negFlag is f
ad550 61 6c 73 65 20 6f 72 20 74 72 75 65 0a 2a 2a 20  alse or true.** 
ad560 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 74 72  if negFlag is tr
ad570 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 64 69 6e  ue..**.** Leadin
ad580 67 20 7a 65 72 6f 73 20 61 72 65 20 69 67 6e 6f  g zeros are igno
ad590 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
ad5a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ad5b0 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63  e3FitsIn64Bits(c
ad5c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c  onst char *zNum,
ad5d0 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 7b 0a 20   int negFlag){. 
ad5e0 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e 74   int i, c;.  int
ad5f0 20 6e 65 67 20 3d 20 30 3b 0a 0a 20 20 61 73 73   neg = 0;..  ass
ad600 65 72 74 28 20 7a 4e 75 6d 5b 30 5d 3e 3d 27 30  ert( zNum[0]>='0
ad610 27 20 26 26 20 7a 4e 75 6d 5b 30 5d 3c 3d 27 39  ' && zNum[0]<='9
ad620 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d 20 69 73 20  ' ); /* zNum is 
ad630 61 6e 20 75 6e 73 69 67 6e 65 64 20 6e 75 6d 62  an unsigned numb
ad640 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 65 67  er */..  if( neg
ad650 46 6c 61 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e  Flag ) neg = 1-n
ad660 65 67 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e  eg;.  while( *zN
ad670 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a  um=='0' ){.    z
ad680 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70  Num++;   /* Skip
ad690 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20   leading zeros. 
ad6a0 20 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f   Ticket #2454 */
ad6b0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
ad6c0 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30 27  (c=zNum[i])>='0'
ad6d0 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b 29   && c<='9'; i++)
ad6e0 7b 7d 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b  {}.  if( i<19 ){
ad6f0 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65  .    /* Guarante
ad700 65 64 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73  ed to fit if les
ad710 73 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73  s than 19 digits
ad720 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
ad730 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e  ;.  }else if( i>
ad740 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61  19 ){.    /* Gua
ad750 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f  ranteed to be to
ad760 6f 20 62 69 67 20 69 66 20 67 72 65 61 74 65 72  o big if greater
ad770 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20   than 19 digits 
ad780 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  */.    return 0;
ad790 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
ad7a0 20 43 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74   Compare against
ad7b0 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65   2^63. */.    re
ad7c0 74 75 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77  turn compare2pow
ad7d0 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20  63(zNum)<neg;.  
ad7e0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e  }.}../*.** If zN
ad7f0 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e  um represents an
ad800 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 77 69   integer that wi
ad810 6c 6c 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74  ll fit in 32-bit
ad820 73 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a  s, then set.** *
ad830 70 56 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69  pValue to that i
ad840 6e 74 65 67 65 72 20 61 6e 64 20 72 65 74 75 72  nteger and retur
ad850 6e 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69  n true.  Otherwi
ad860 73 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e  se return false.
ad870 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e  .**.** Any non-n
ad880 75 6d 65 72 69 63 20 63 68 61 72 61 63 74 65 72  umeric character
ad890 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67  s that following
ad8a0 20 7a 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65   zNum are ignore
ad8b0 64 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69  d..** This is di
ad8c0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c  fferent from sql
ad8d0 69 74 65 33 41 74 6f 69 36 34 28 29 20 77 68 69  ite3Atoi64() whi
ad8e0 63 68 20 72 65 71 75 69 72 65 73 20 74 68 65 0a  ch requires the.
ad8f0 2a 2a 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20  ** input number 
ad900 74 6f 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69  to be zero-termi
ad910 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nated..*/.SQLITE
ad920 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ad930 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e  ite3GetInt32(con
ad940 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69  st char *zNum, i
ad950 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73  nt *pValue){.  s
ad960 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20  qlite_int64 v = 
ad970 30 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20  0;.  int i, c;. 
ad980 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20   int neg = 0;.  
ad990 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27  if( zNum[0]=='-'
ad9a0 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b   ){.    neg = 1;
ad9b0 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d  .    zNum++;.  }
ad9c0 65 6c 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d  else if( zNum[0]
ad9d0 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75  =='+' ){.    zNu
ad9e0 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65  m++;.  }.  while
ad9f0 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29  ( zNum[0]=='0' )
ada00 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69   zNum++;.  for(i
ada10 3d 30 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d  =0; i<11 && (c =
ada20 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e   zNum[i] - '0')>
ada30 3d 30 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29  =0 && c<=9; i++)
ada40 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b  {.    v = v*10 +
ada50 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68   c;.  }..  /* Th
ada60 65 20 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61  e longest decima
ada70 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  l representation
ada80 20 6f 66 20 61 20 33 32 20 62 69 74 20 69 6e 74   of a 32 bit int
ada90 65 67 65 72 20 69 73 20 31 30 20 64 69 67 69 74  eger is 10 digit
adaa0 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  s:.  **.  **    
adab0 20 20 20 20 20 20 20 20 20 31 32 33 34 35 36 37           1234567
adac0 38 39 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33  890.  **     2^3
adad0 31 20 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a  1 -> 2147483648.
adae0 20 20 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20    */.  if( i>10 
adaf0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
adb00 0a 20 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67  .  }.  if( v-neg
adb10 3e 32 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20  >2147483647 ){. 
adb20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
adb30 0a 20 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20  .  if( neg ){.  
adb40 20 20 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20    v = -v;.  }.  
adb50 2a 70 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76  *pValue = (int)v
adb60 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
adb70 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61  ./*.** The varia
adb80 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
adb90 65 72 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61  er encoding is a
adba0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
adbb0 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20   KEY:.**        
adbc0 20 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20   A = 0xxxxxxx   
adbd0 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20   7 bits of data 
adbe0 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74  and one flag bit
adbf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20  .**         B = 
adc00 31 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69  1xxxxxxx    7 bi
adc10 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f  ts of data and o
adc20 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20  ne flag bit.**  
adc30 20 20 20 20 20 20 20 43 20 3d 20 78 78 78 78 78         C = xxxxx
adc40 78 78 78 20 20 20 20 38 20 62 69 74 73 20 6f 66  xxx    8 bits of
adc50 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62   data.**.**  7 b
adc60 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69  its - A.** 14 bi
adc70 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69  ts - BA.** 21 bi
adc80 74 73 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62  ts - BBA.** 28 b
adc90 69 74 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35  its - BBBA.** 35
adca0 20 62 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a   bits - BBBBA.**
adcb0 20 34 32 20 62 69 74 73 20 2d 20 42 42 42 42 42   42 bits - BBBBB
adcc0 41 0a 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42  A.** 49 bits - B
adcd0 42 42 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74  BBBBBA.** 56 bit
adce0 73 20 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20  s - BBBBBBBA.** 
adcf0 36 34 20 62 69 74 73 20 2d 20 42 42 42 42 42 42  64 bits - BBBBBB
add00 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72  BBC.*/../*.** Wr
add10 69 74 65 20 61 20 36 34 2d 62 69 74 20 76 61 72  ite a 64-bit var
add20 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
add30 65 67 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73  eger to memory s
add40 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e  tarting at p[0].
add50 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f  .** The length o
add60 66 20 64 61 74 61 20 77 72 69 74 65 20 77 69 6c  f data write wil
add70 6c 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61  l be between 1 a
add80 6e 64 20 39 20 62 79 74 65 73 2e 20 20 54 68 65  nd 9 bytes.  The
add90 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79   number.** of by
adda0 74 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72  tes written is r
addb0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41  eturned..**.** A
addc0 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
addd0 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74   integer consist
adde0 73 20 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37  s of the lower 7
addf0 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 62 79   bits of each by
ade00 74 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79  te.** for all by
ade10 74 65 73 20 74 68 61 74 20 68 61 76 65 20 74 68  tes that have th
ade20 65 20 38 74 68 20 62 69 74 20 73 65 74 20 61 6e  e 8th bit set an
ade30 64 20 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20  d one byte with 
ade40 74 68 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63  the 8th.** bit c
ade50 6c 65 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69  lear.  Except, i
ade60 66 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 20  f we get to the 
ade70 39 74 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f  9th byte, it sto
ade80 72 65 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20  res the full.** 
ade90 38 20 62 69 74 73 20 61 6e 64 20 69 73 20 74 68  8 bits and is th
adea0 65 20 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a  e last byte..*/.
adeb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
adec0 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  nt sqlite3PutVar
aded0 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61  int(unsigned cha
adee0 72 20 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20  r *p, u64 v){.  
adef0 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75  int i, j, n;.  u
adf00 38 20 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28  8 buf[10];.  if(
adf10 20 76 20 26 20 28 28 28 75 36 34 29 30 78 66 66   v & (((u64)0xff
adf20 30 30 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a  000000)<<32) ){.
adf30 20 20 20 20 70 5b 38 5d 20 3d 20 28 75 38 29 76      p[8] = (u8)v
adf40 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20  ;.    v >>= 8;. 
adf50 20 20 20 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30     for(i=7; i>=0
adf60 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b  ; i--){.      p[
adf70 69 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30  i] = (u8)((v & 0
adf80 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20  x7f) | 0x80);.  
adf90 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20      v >>= 7;.   
adfa0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 39 3b   }.    return 9;
adfb0 0a 20 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30  .  }    .  n = 0
adfc0 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b  ;.  do{.    buf[
adfd0 6e 2b 2b 5d 20 3d 20 28 75 38 29 28 28 76 20 26  n++] = (u8)((v &
adfe0 20 30 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a   0x7f) | 0x80);.
adff0 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d      v >>= 7;.  }
ae000 77 68 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20  while( v!=0 );. 
ae010 20 62 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b   buf[0] &= 0x7f;
ae020 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20  .  assert( n<=9 
ae030 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d  );.  for(i=0, j=
ae040 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20  n-1; j>=0; j--, 
ae050 69 2b 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d  i++){.    p[i] =
ae060 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72   buf[j];.  }.  r
ae070 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
ae080 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
ae090 73 20 61 20 66 61 73 74 65 72 20 76 65 72 73 69  s a faster versi
ae0a0 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 50 75 74  on of sqlite3Put
ae0b0 56 61 72 69 6e 74 28 29 20 74 68 61 74 20 6f 6e  Varint() that on
ae0c0 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20  ly.** works for 
ae0d0 33 32 2d 62 69 74 20 70 6f 73 69 74 69 76 65 20  32-bit positive 
ae0e0 69 6e 74 65 67 65 72 73 20 61 6e 64 20 77 68 69  integers and whi
ae0f0 63 68 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20  ch is optimized 
ae100 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f  for.** the commo
ae110 6e 20 63 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20  n case of small 
ae120 69 6e 74 65 67 65 72 73 2e 20 20 41 20 4d 41 43  integers.  A MAC
ae130 52 4f 20 76 65 72 73 69 6f 6e 2c 20 70 75 74 56  RO version, putV
ae140 61 72 69 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70  arint32,.** is p
ae150 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e  rovided which in
ae160 6c 69 6e 65 73 20 74 68 65 20 73 69 6e 67 6c 65  lines the single
ae170 2d 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c  -byte case.  All
ae180 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65   code should use
ae190 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 76 65  .** the MACRO ve
ae1a0 72 73 69 6f 6e 20 61 73 20 74 68 69 73 20 66 75  rsion as this fu
ae1b0 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  nction assumes t
ae1c0 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63  he single-byte c
ae1d0 61 73 65 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61  ase has.** alrea
ae1e0 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e  dy been handled.
ae1f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
ae200 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75  TE int sqlite3Pu
ae210 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e  tVarint32(unsign
ae220 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20  ed char *p, u32 
ae230 76 29 7b 0a 23 69 66 6e 64 65 66 20 70 75 74 56  v){.#ifndef putV
ae240 61 72 69 6e 74 33 32 0a 20 20 69 66 28 20 28 76  arint32.  if( (v
ae250 20 26 20 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a   & ~0x7f)==0 ){.
ae260 20 20 20 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20      p[0] = v;.  
ae270 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
ae280 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 76 20  #endif.  if( (v 
ae290 26 20 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b  & ~0x3fff)==0 ){
ae2a0 0a 20 20 20 20 70 5b 30 5d 20 3d 20 28 75 38 29  .    p[0] = (u8)
ae2b0 28 28 76 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b  ((v>>7) | 0x80);
ae2c0 0a 20 20 20 20 70 5b 31 5d 20 3d 20 28 75 38 29  .    p[1] = (u8)
ae2d0 28 76 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20  (v & 0x7f);.    
ae2e0 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20  return 2;.  }.  
ae2f0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 75  return sqlite3Pu
ae300 74 56 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d  tVarint(p, v);.}
ae310 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36  ../*.** Read a 6
ae320 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c  4-bit variable-l
ae330 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 66 72  ength integer fr
ae340 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69  om memory starti
ae350 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52  ng at p[0]..** R
ae360 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
ae370 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e 20   of bytes read. 
ae380 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74   The value is st
ae390 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53  ored in *v..*/.S
ae3a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38  QLITE_PRIVATE u8
ae3b0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
ae3c0 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  t(const unsigned
ae3d0 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76   char *p, u64 *v
ae3e0 29 7b 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a  ){.  u32 a,b,s;.
ae3f0 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20  .  a = *p;.  /* 
ae400 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29  a: p0 (unmasked)
ae410 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
ae420 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20  80)).  {.    *v 
ae430 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = a;.    return 
ae440 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20  1;.  }..  p++;. 
ae450 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a   b = *p;.  /* b:
ae460 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p1 (unmasked) *
ae470 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30  /.  if (!(b&0x80
ae480 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20  )).  {.    a &= 
ae490 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c  0x7f;.    a = a<
ae4a0 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a  <7;.    a |= b;.
ae4b0 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20      *v = a;.    
ae4c0 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20  return 2;.  }.. 
ae4d0 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31   p++;.  a = a<<1
ae4e0 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  a |= *p;.  
ae4f0 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70  /* a: p0<<14 | p
ae500 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  2 (unmasked) */.
ae510 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29    if (!(a&0x80))
ae520 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30  .  {.    a &= (0
ae530 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
ae540 0a 20 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a  .    b &= 0x7f;.
ae550 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20      b = b<<7;.  
ae560 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76    a |= b;.    *v
ae570 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   = a;.    return
ae580 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53   3;.  }..  /* CS
ae590 45 31 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f  E1 from below */
ae5a0 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31  .  a &= (0x7f<<1
ae5b0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b  4)|(0x7f);.  p++
ae5c0 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20  ;.  b = b<<14;. 
ae5d0 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62   b |= *p;.  /* b
ae5e0 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75  : p1<<14 | p3 (u
ae5f0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
ae600 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b   (!(b&0x80)).  {
ae610 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c  .    b &= (0x7f<
ae620 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20  <14)|(0x7f);.   
ae630 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75   /* moved CSE1 u
ae640 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d  p */.    /* a &=
ae650 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
ae660 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61  f); */.    a = a
ae670 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b  <<7;.    a |= b;
ae680 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20  .    *v = a;.   
ae690 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a   return 4;.  }..
ae6a0 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c    /* a: p0<<14 |
ae6b0 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a   p2 (masked) */.
ae6c0 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c    /* b: p1<<14 |
ae6d0 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p3 (unmasked) *
ae6e0 2f 0a 20 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66  /.  /* 1:save of
ae6f0 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31  f p0<<21 | p1<<1
ae700 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28  4 | p2<<7 | p3 (
ae710 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20  masked) */.  /* 
ae720 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f  moved CSE1 up */
ae730 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66  .  /* a &= (0x7f
ae740 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f  <<14)|(0x7f); */
ae750 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31  .  b &= (0x7f<<1
ae760 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d  4)|(0x7f);.  s =
ae770 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c   a;.  /* s: p0<<
ae780 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29  14 | p2 (masked)
ae790 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20   */..  p++;.  a 
ae7a0 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20  = a<<14;.  a |= 
ae7b0 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c  *p;.  /* a: p0<<
ae7c0 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34  28 | p2<<14 | p4
ae7d0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
ae7e0 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a   if (!(a&0x80)).
ae7f0 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61    {.    /* we ca
ae800 6e 20 73 6b 69 70 20 74 68 65 73 65 20 63 61 75  n skip these cau
ae810 73 65 20 74 68 65 79 20 77 65 72 65 20 28 65 66  se they were (ef
ae820 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20  fectively) done 
ae830 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e  above in calc'in
ae840 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20  g s */.    /* a 
ae850 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30  &= (0x7f<<28)|(0
ae860 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
ae870 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20   */.    /* b &= 
ae880 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
ae890 29 3b 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c  ); */.    b = b<
ae8a0 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a  <7;.    a |= b;.
ae8b0 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20      s = s>>18;. 
ae8c0 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29     *v = ((u64)s)
ae8d0 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65  <<32 | a;.    re
ae8e0 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 5;.  }..  /
ae8f0 2a 20 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c  * 2:save off p0<
ae900 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70  <21 | p1<<14 | p
ae910 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65  2<<7 | p3 (maske
ae920 64 29 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37  d) */.  s = s<<7
ae930 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a  ;.  s |= b;.  /*
ae940 20 73 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c   s: p0<<21 | p1<
ae950 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33  <14 | p2<<7 | p3
ae960 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20   (masked) */..  
ae970 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34  p++;.  b = b<<14
ae980 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  b |= *p;.  /
ae990 2a 20 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33  * b: p1<<28 | p3
ae9a0 3c 3c 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73  <<14 | p5 (unmas
ae9b0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
ae9c0 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
ae9d0 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20   /* we can skip 
ae9e0 74 68 69 73 20 63 61 75 73 65 20 69 74 20 77 61  this cause it wa
ae9f0 73 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20  s (effectively) 
aea00 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61  done above in ca
aea10 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20  lc'ing s */.    
aea20 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32  /* b &= (0x7f<<2
aea30 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
aea40 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26  x7f); */.    a &
aea50 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
aea60 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c  7f);.    a = a<<
aea70 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
aea80 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20     s = s>>18;.  
aea90 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c    *v = ((u64)s)<
aeaa0 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74  <32 | a;.    ret
aeab0 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b  urn 6;.  }..  p+
aeac0 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a  +;.  a = a<<14;.
aead0 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    a |= *p;.  /* 
aeae0 61 3a 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c  a: p2<<28 | p4<<
aeaf0 31 34 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65  14 | p6 (unmaske
aeb00 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26  d) */.  if (!(a&
aeb10 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61  0x80)).  {.    a
aeb20 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28   &= (0x1f<<28)|(
aeb30 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
aeb40 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66  ;.    b &= (0x7f
aeb50 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
aeb60 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
aeb70 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20  a |= b;.    s = 
aeb80 73 3e 3e 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20  s>>11;.    *v = 
aeb90 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61  ((u64)s)<<32 | a
aeba0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a  ;.    return 7;.
aebb0 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66    }..  /* CSE2 f
aebc0 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61  rom below */.  a
aebd0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
aebe0 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20  0x7f);.  p++;.  
aebf0 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c  b = b<<14;.  b |
aec00 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33  = *p;.  /* b: p3
aec10 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20  <<28 | p5<<14 | 
aec20 70 37 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p7 (unmasked) */
aec30 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29  .  if (!(b&0x80)
aec40 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28  ).  {.    b &= (
aec50 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c  0x1f<<28)|(0x7f<
aec60 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20  <14)|(0x7f);.   
aec70 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75   /* moved CSE2 u
aec80 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d  p */.    /* a &=
aec90 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
aeca0 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61  f); */.    a = a
aecb0 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b  <<7;.    a |= b;
aecc0 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20  .    s = s>>4;. 
aecd0 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29     *v = ((u64)s)
aece0 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65  <<32 | a;.    re
aecf0 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70  turn 8;.  }..  p
aed00 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b  ++;.  a = a<<15;
aed10 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
aed20 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c   a: p4<<29 | p6<
aed30 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b  <15 | p8 (unmask
aed40 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76  ed) */..  /* mov
aed50 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20  ed CSE2 up */.  
aed60 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32  /* a &= (0x7f<<2
aed70 39 29 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 30  9)|(0x7f<<15)|(0
aed80 78 66 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20  xff); */.  b &= 
aed90 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
aeda0 29 3b 0a 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20  );.  b = b<<8;. 
aedb0 20 61 20 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20   a |= b;..  s = 
aedc0 73 3c 3c 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34  s<<4;.  b = p[-4
aedd0 5d 3b 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0a  ];.  b &= 0x7f;.
aede0 20 20 62 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20    b = b>>3;.  s 
aedf0 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28  |= b;..  *v = ((
aee00 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a  u64)s)<<32 | a;.
aee10 0a 20 20 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a  .  return 9;.}..
aee20 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d  /*.** Read a 32-
aee30 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
aee40 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  gth integer from
aee50 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
aee60 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74   at p[0]..** Ret
aee70 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
aee80 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54  f bytes read.  T
aee90 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  he value is stor
aeea0 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a 20 41 20 4d  ed in *v..** A M
aeeb0 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 67 65  ACRO version, ge
aeec0 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 70 72  tVarint32, is pr
aeed0 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c  ovided which inl
aeee0 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 69 6e  ines the .** sin
aeef0 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 20  gle-byte case.  
aef00 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  All code should 
aef10 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 76 65  use the MACRO ve
aef20 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 69  rsion as .** thi
aef30 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
aef40 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79  es the single-by
aef50 74 65 20 63 61 73 65 20 68 61 73 20 61 6c 72 65  te case has alre
aef60 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 64  ady been handled
aef70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
aef80 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65  ATE u8 sqlite3Ge
aef90 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20  tVarint32(const 
aefa0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
aefb0 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33 32  , u32 *v){.  u32
aefc0 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 68 65 20   a,b;..  /* The 
aefd0 31 2d 62 79 74 65 20 63 61 73 65 2e 20 20 4f 76  1-byte case.  Ov
aefe0 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74 68 65  erwhelmingly the
aeff0 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20 48   most common.  H
af000 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0a 20 20  andled inline.  
af010 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56 61 72  ** by the getVar
af020 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a 2f 0a  in32() macro */.
af030 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61    a = *p;.  /* a
af040 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20  : p0 (unmasked) 
af050 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 74 56 61  */.#ifndef getVa
af060 72 69 6e 74 33 32 0a 20 20 69 66 20 28 21 28 61  rint32.  if (!(a
af070 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
af080 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 65  /* Values betwee
af090 6e 20 30 20 61 6e 64 20 31 32 37 20 2a 2f 0a 20  n 0 and 127 */. 
af0a0 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72     *v = a;.    r
af0b0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e  eturn 1;.  }.#en
af0c0 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 20 32 2d  dif..  /* The 2-
af0d0 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70  byte case */.  p
af0e0 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20  ++;.  b = *p;.  
af0f0 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b  /* b: p1 (unmask
af100 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62  ed) */.  if (!(b
af110 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
af120 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 65 65  /* Values betwee
af130 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38 33 20  n 128 and 16383 
af140 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 78 37 66  */.    a &= 0x7f
af150 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a  ;.    a = a<<7;.
af160 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a      *v = a | b;.
af170 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
af180 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 2d 62 79  }..  /* The 3-by
af190 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b 2b  te case */.  p++
af1a0 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20  ;.  a = a<<14;. 
af1b0 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61   a |= *p;.  /* a
af1c0 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 75  : p0<<14 | p2 (u
af1d0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
af1e0 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b   (!(a&0x80)).  {
af1f0 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62  .    /* Values b
af200 65 74 77 65 65 6e 20 31 36 33 38 34 20 61 6e 64  etween 16384 and
af210 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 20 20 20   2097151 */.    
af220 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
af230 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d  (0x7f);.    b &=
af240 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d 20 62   0x7f;.    b = b
af250 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20  <<7;.    *v = a 
af260 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  | b;.    return 
af270 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 33  3;.  }..  /* A 3
af280 32 2d 62 69 74 20 76 61 72 69 6e 74 20 69 73 20  2-bit varint is 
af290 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 73 69  used to store si
af2a0 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ze information i
af2b0 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a 2a 20 4f  n btrees..  ** O
af2c0 62 6a 65 63 74 73 20 61 72 65 20 72 61 72 65 6c  bjects are rarel
af2d0 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 32 4d  y larger than 2M
af2e0 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 20 33 2d  iB limit of a 3-
af2f0 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 20 20 2a  byte varint..  *
af300 2a 20 41 20 33 2d 62 79 74 65 20 76 61 72 69 6e  * A 3-byte varin
af310 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 2c  t is sufficient,
af320 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f   for example, to
af330 20 72 65 63 6f 72 64 20 74 68 65 20 73 69 7a 65   record the size
af340 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 34 38 35  .  ** of a 10485
af350 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 6f 72 20  69-byte BLOB or 
af360 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a  string..  **.  *
af370 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c 6c  * We only unroll
af380 20 74 68 65 20 66 69 72 73 74 20 31 2d 2c 20 32   the first 1-, 2
af390 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 65 20 63  -, and 3- byte c
af3a0 61 73 65 73 2e 20 20 54 68 65 20 76 65 72 79 0a  ases.  The very.
af3b0 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 67 65 72    ** rare larger
af3c0 20 63 61 73 65 73 20 63 61 6e 20 62 65 20 68 61   cases can be ha
af3d0 6e 64 6c 65 64 20 62 79 20 74 68 65 20 73 6c 6f  ndled by the slo
af3e0 77 65 72 20 36 34 2d 62 69 74 20 76 61 72 69 6e  wer 64-bit varin
af3f0 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a  t.  ** routine..
af400 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 7b 0a 20    */.#if 1.  {. 
af410 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20     u64 v64;.    
af420 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20  u8 n;..    p -= 
af430 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  2;.    n = sqlit
af440 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26  e3GetVarint(p, &
af450 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74  v64);.    assert
af460 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 20 29 3b  ( n>3 && n<=9 );
af470 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76  .    *v = (u32)v
af480 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e  64;.    return n
af490 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f  ;.  }..#else.  /
af4a0 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20  * For following 
af4b0 63 6f 64 65 20 28 6b 65 70 74 20 66 6f 72 20 68  code (kept for h
af4c0 69 73 74 6f 72 69 63 61 6c 20 72 65 63 6f 72 64  istorical record
af4d0 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20 61 6e 0a   only) shows an.
af4e0 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 6e 67 20 66    ** unrolling f
af4f0 6f 72 20 74 68 65 20 33 2d 20 61 6e 64 20 34 2d  or the 3- and 4-
af500 62 79 74 65 20 76 61 72 69 6e 74 20 63 61 73 65  byte varint case
af510 73 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73  s.  This code is
af520 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66  .  ** slightly f
af530 61 73 74 65 72 2c 20 62 75 74 20 69 74 20 69 73  aster, but it is
af540 20 61 6c 73 6f 20 6c 61 72 67 65 72 20 61 6e 64   also larger and
af550 20 6d 75 63 68 20 68 61 72 64 65 72 20 74 6f 20   much harder to 
af560 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 2b 2b  test..  */.  p++
af570 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20  ;.  b = b<<14;. 
af580 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62   b |= *p;.  /* b
af590 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75  : p1<<14 | p3 (u
af5a0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
af5b0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b   (!(b&0x80)).  {
af5c0 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62  .    /* Values b
af5d0 65 74 77 65 65 6e 20 32 30 39 37 31 35 32 20 61  etween 2097152 a
af5e0 6e 64 20 32 36 38 34 33 35 34 35 35 20 2a 2f 0a  nd 268435455 */.
af5f0 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c      b &= (0x7f<<
af600 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
af610 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
af620 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d 20  (0x7f);.    a = 
af630 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61  a<<7;.    *v = a
af640 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e   | b;.    return
af650 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a   4;.  }..  p++;.
af660 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61    a = a<<14;.  a
af670 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20   |= *p;.  /* a: 
af680 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20  p0<<28 | p2<<14 
af690 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p4 (unmasked) 
af6a0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  */.  if (!(a&0x8
af6b0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 57  0)).  {.    /* W
af6c0 61 6c 75 65 73 20 20 62 65 74 77 65 65 6e 20 32  alues  between 2
af6d0 36 38 34 33 35 34 35 36 20 61 6e 64 20 33 34 33  68435456 and 343
af6e0 35 39 37 33 38 33 36 37 20 2a 2f 0a 20 20 20 20  59738367 */.    
af6f0 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c  a &= (0x1f<<28)|
af700 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
af710 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 31  );.    b &= (0x1
af720 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34  f<<28)|(0x7f<<14
af730 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20  )|(0x7f);.    b 
af740 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d  = b<<7;.    *v =
af750 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75   a | b;.    retu
af760 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 5;.  }..  /* 
af770 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63  We can only reac
af780 68 20 74 68 69 73 20 70 6f 69 6e 74 20 77 68 65  h this point whe
af790 6e 20 72 65 61 64 69 6e 67 20 61 20 63 6f 72 72  n reading a corr
af7a0 75 70 74 20 64 61 74 61 62 61 73 65 0a 20 20 2a  upt database.  *
af7b0 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 68 61 74  * file.  In that
af7c0 20 63 61 73 65 20 77 65 20 61 72 65 20 6e 6f 74   case we are not
af7d0 20 69 6e 20 61 6e 79 20 68 75 72 72 79 2e 20 20   in any hurry.  
af7e0 55 73 65 20 74 68 65 20 28 72 65 6c 61 74 69 76  Use the (relativ
af7f0 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67  ely.  ** slow) g
af800 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 73  eneral-purpose s
af810 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
af820 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74  ) routine to ext
af830 72 61 63 74 20 74 68 65 0a 20 20 2a 2a 20 76 61  ract the.  ** va
af840 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20  lue. */.  {.    
af850 75 36 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20  u64 v64;.    u8 
af860 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a  n;..    p -= 4;.
af870 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47      n = sqlite3G
af880 65 74 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34  etVarint(p, &v64
af890 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
af8a0 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20  >5 && n<=9 );.  
af8b0 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b    *v = (u32)v64;
af8c0 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20  .    return n;. 
af8d0 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
af8e0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
af8f0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 68  mber of bytes th
af900 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 65  at will be neede
af910 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 67  d to store the g
af920 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69  iven.** 64-bit i
af930 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  nteger..*/.SQLIT
af940 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
af950 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75  lite3VarintLen(u
af960 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d  64 v){.  int i =
af970 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b   0;.  do{.    i+
af980 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a  +;.    v >>= 7;.
af990 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 26    }while( v!=0 &
af9a0 26 20 41 4c 57 41 59 53 28 69 3c 39 29 20 29 3b  & ALWAYS(i<9) );
af9b0 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a  .  return i;.}..
af9c0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77  ./*.** Read or w
af9d0 72 69 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65  rite a four-byte
af9e0 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
af9f0 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51  ger value..*/.SQ
afa00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
afa10 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
afa20 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20  (const u8 *p){. 
afa30 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32   return (p[0]<<2
afa40 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20  4) | (p[1]<<16) 
afa50 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b  | (p[2]<<8) | p[
afa60 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  3];.}.SQLITE_PRI
afa70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
afa80 33 50 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e  3Put4byte(unsign
afa90 65 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20  ed char *p, u32 
afaa0 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 75 38  v){.  p[0] = (u8
afab0 29 28 76 3e 3e 32 34 29 3b 0a 20 20 70 5b 31 5d  )(v>>24);.  p[1]
afac0 20 3d 20 28 75 38 29 28 76 3e 3e 31 36 29 3b 0a   = (u8)(v>>16);.
afad0 20 20 70 5b 32 5d 20 3d 20 28 75 38 29 28 76 3e    p[2] = (u8)(v>
afae0 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 75  >8);.  p[3] = (u
afaf0 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 21 64  8)v;.}....#if !d
afb00 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
afb10 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29  IT_BLOB_LITERAL)
afb20 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
afb30 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a  TE_HAS_CODEC)./*
afb40 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20  .** Translate a 
afb50 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 48  single byte of H
afb60 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67  ex into an integ
afb70 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  er..** This rout
afb80 69 6e 65 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  inen only works 
afb90 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73 20 61  if h really is a
afba0 20 76 61 6c 69 64 20 68 65 78 61 64 65 63 69 6d   valid hexadecim
afbb0 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 3a  al.** character:
afbc0 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46 0a 2a    0..9a..fA..F.*
afbd0 2f 0a 73 74 61 74 69 63 20 75 38 20 68 65 78 54  /.static u8 hexT
afbe0 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20 20 61  oInt(int h){.  a
afbf0 73 73 65 72 74 28 20 28 68 3e 3d 27 30 27 20 26  ssert( (h>='0' &
afc00 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20 28 68  & h<='9') ||  (h
afc10 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 27 29  >='a' && h<='f')
afc20 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26 26 20   ||  (h>='A' && 
afc30 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66 64 65  h<='F') );.#ifde
afc40 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 20  f SQLITE_ASCII. 
afc50 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e 3e 36   h += 9*(1&(h>>6
afc60 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  ));.#endif.#ifde
afc70 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a  f SQLITE_EBCDIC.
afc80 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28 68 3e    h += 9*(1&~(h>
afc90 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72  >4));.#endif.  r
afca0 65 74 75 72 6e 20 28 75 38 29 28 68 20 26 20 30  eturn (u8)(h & 0
afcb0 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  xf);.}.#endif /*
afcc0 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c   !SQLITE_OMIT_BL
afcd0 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51  OB_LITERAL || SQ
afce0 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a  LITE_HAS_CODEC *
afcf0 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
afd00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
afd10 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66  _LITERAL) || def
afd20 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f  ined(SQLITE_HAS_
afd30 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e  CODEC)./*.** Con
afd40 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69 74 65  vert a BLOB lite
afd50 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ral of the form 
afd60 22 78 27 68 68 68 68 68 68 27 22 20 69 6e 74 6f  "x'hhhhhh'" into
afd70 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a 20 76   its binary.** v
afd80 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20 61 20  alue.  Return a 
afd90 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62  pointer to its b
afda0 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20 53 70  inary value.  Sp
afdb0 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a  ace to hold the.
afdc0 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75 65 20  ** binary value 
afdd0 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65  has been obtaine
afde0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e  d from malloc an
afdf0 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20  d must be freed 
afe00 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e  by.** the callin
afe10 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51  g routine..*/.SQ
afe20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
afe30 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 6f 42  d *sqlite3HexToB
afe40 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  lob(sqlite3 *db,
afe50 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
afe60 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a  int n){.  char *
afe70 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 69 3b 0a  zBlob;.  int i;.
afe80 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 68 61 72  .  zBlob = (char
afe90 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   *)sqlite3DbMall
afea0 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 20 2b 20  ocRaw(db, n/2 + 
afeb0 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66 28  1);.  n--;.  if(
afec0 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 20 66 6f   zBlob ){.    fo
afed0 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32  r(i=0; i<n; i+=2
afee0 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 5b 69  ){.      zBlob[i
afef0 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 6e 74 28  /2] = (hexToInt(
aff00 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 65 78 54  z[i])<<4) | hexT
aff10 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a 20 20  oInt(z[i+1]);.  
aff20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 5b 69 2f    }.    zBlob[i/
aff30 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  2] = 0;.  }.  re
aff40 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a 23 65  turn zBlob;.}.#e
aff50 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
aff60 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
aff70 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f  L || SQLITE_HAS_
aff80 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  CODEC */.../*.**
aff90 20 43 68 61 6e 67 65 20 74 68 65 20 73 71 6c 69   Change the sqli
affa0 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d 20 53 51  te.magic from SQ
affb0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
affc0 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  to SQLITE_MAGIC_
affd0 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  BUSY..** Return 
affe0 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65  an error (non-ze
afff0 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 69 63  ro) if the magic
b0000 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f   was not SQLITE_
b0010 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68  MAGIC_OPEN.** wh
b0020 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
b0030 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a  is called..**.**
b0040 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
b0050 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 65 6e 74   called when ent
b0060 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 74 65 20  ering an SQLite 
b0070 41 50 49 2e 20 20 54 68 65 20 53 51 4c 49 54 45  API.  The SQLITE
b0080 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 76  _MAGIC_OPEN.** v
b0090 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 74  alue indicates t
b00a0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
b00b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73   connection pass
b00c0 65 64 20 69 6e 74 6f 20 74 68 65 20 41 50 49 20  ed into the API 
b00d0 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e 64 20 69  is.** open and i
b00e0 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 73 65 64  s not being used
b00f0 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68 72 65   by another thre
b0100 61 64 2e 20 20 42 79 20 63 68 61 6e 67 69 6e 67  ad.  By changing
b0110 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 74 6f   the value.** to
b0120 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
b0130 53 59 20 77 65 20 69 6e 64 69 63 61 74 65 20 74  SY we indicate t
b0140 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  hat the connecti
b0150 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e 0a 2a 2a  on is in use..**
b0160 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
b0170 66 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 63  f() below will c
b0180 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
b0190 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4d  back to SQLITE_M
b01a0 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65  AGIC_OPEN.** whe
b01b0 6e 20 74 68 65 20 41 50 49 20 65 78 69 74 73 2e  n the API exits.
b01c0 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75   .**.** This rou
b01d0 74 69 6e 65 20 69 73 20 61 20 61 74 74 65 6d 70  tine is a attemp
b01e0 74 20 74 6f 20 64 65 74 65 63 74 20 69 66 20 74  t to detect if t
b01f0 77 6f 20 74 68 72 65 61 64 73 20 75 73 65 20 74  wo threads use t
b0200 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 6c 69 74  he.** same sqlit
b0210 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 20 74 68  e* pointer at th
b0220 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 54 68  e same time.  Th
b0230 65 72 65 20 69 73 20 61 20 72 61 63 65 20 0a 2a  ere is a race .*
b0240 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 6f 20 69  * condition so i
b0250 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68  t is possible th
b0260 61 74 20 74 68 65 20 65 72 72 6f 72 20 69 73 20  at the error is 
b0270 6e 6f 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2a  not detected..**
b0280 20 42 75 74 20 75 73 75 61 6c 6c 79 20 74 68 65   But usually the
b0290 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c 20 62 65   problem will be
b02a0 20 73 65 65 6e 2e 20 20 54 68 65 20 72 65 73 75   seen.  The resu
b02b0 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e 0a 2a 2a  lt will be an.**
b02c0 20 65 72 72 6f 72 20 77 68 69 63 68 20 63 61 6e   error which can
b02d0 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 62 75   be used to debu
b02e0 67 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  g the applicatio
b02f0 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 75 73 69  n that is.** usi
b0300 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 6f 72 72  ng SQLite incorr
b0310 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 69 63  ectly..**.** Tic
b0320 6b 65 74 20 23 32 30 32 3a 20 20 49 66 20 64 62  ket #202:  If db
b0330 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f 74 20 61  ->magic is not a
b0340 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 61 6c 75   valid open valu
b0350 65 2c 20 74 61 6b 65 20 63 61 72 65 20 6e 6f 74  e, take care not
b0360 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  .** to modify th
b0370 65 20 64 62 20 73 74 72 75 63 74 75 72 65 20 61  e db structure a
b0380 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f 75 6c 64  t all.  It could
b0390 20 62 65 20 74 68 61 74 20 64 62 20 69 73 20 61   be that db is a
b03a0 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 6e 74 65   stale.** pointe
b03b0 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  r.  In other wor
b03c0 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20  ds, it could be 
b03d0 74 68 61 74 20 74 68 65 72 65 20 68 61 73 20 62  that there has b
b03e0 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a 2a 20 63  een a prior.** c
b03f0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  all to sqlite3_c
b0400 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 64 62 20  lose(db) and db 
b0410 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63  has been dealloc
b0420 61 74 65 64 2e 20 20 41 6e 64 20 77 65 20 64 6f  ated.  And we do
b0430 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20  .** not want to 
b0440 77 72 69 74 65 20 69 6e 74 6f 20 64 65 61 6c 6c  write into deall
b0450 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a  ocated memory..*
b0460 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
b0470 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
b0480 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b0490 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33  SafetyOn(sqlite3
b04a0 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d   *db){.  if( db-
b04b0 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d  >magic==SQLITE_M
b04c0 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a 20 20 20  AGIC_OPEN ){.   
b04d0 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
b04e0 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b 0a  ITE_MAGIC_BUSY;.
b04f0 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
b0500 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
b0510 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20  b->mutex) );.   
b0520 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
b0530 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 67 69 63  se if( db->magic
b0540 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  ==SQLITE_MAGIC_B
b0550 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  USY ){.    db->m
b0560 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
b0570 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64  GIC_ERROR;.    d
b0580 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
b0590 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  ted = 1;.  }.  r
b05a0 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
b05b0 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  f../*.** Change 
b05c0 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d 20 53  the magic from S
b05d0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
b05e0 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43   to SQLITE_MAGIC
b05f0 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75 72 6e  _OPEN..** Return
b0600 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a   an error (non-z
b0610 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 69  ero) if the magi
b0620 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 45  c was not SQLITE
b0630 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a 20 77  _MAGIC_BUSY.** w
b0640 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
b0650 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23   is called..*/.#
b0660 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
b0670 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
b0680 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
b0690 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 20 2a  etyOff(sqlite3 *
b06a0 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  db){.  if( db->m
b06b0 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47  agic==SQLITE_MAG
b06c0 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64  IC_BUSY ){.    d
b06d0 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
b06e0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20  E_MAGIC_OPEN;.  
b06f0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
b0700 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
b0710 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 72  >mutex) );.    r
b0720 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
b0730 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20  {.    db->magic 
b0740 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45  = SQLITE_MAGIC_E
b0750 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31  RROR;.    db->u1
b0760 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d  .isInterrupted =
b0770 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   1;.    return 1
b0780 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ;.  }.}.#endif..
b0790 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d  /*.** Check to m
b07a0 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65  ake sure we have
b07b0 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f 69 6e   a valid db poin
b07c0 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 74 20  ter.  This test 
b07d0 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 72  is not.** foolpr
b07e0 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65 73 20  oof but it does 
b07f0 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d 65 61  provide some mea
b0800 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63 74 69  sure of protecti
b0810 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 6d 69  on against.** mi
b0820 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e 74 65  suse of the inte
b0830 72 66 61 63 65 20 73 75 63 68 20 61 73 20 70 61  rface such as pa
b0840 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f 69 6e  ssing in db poin
b0850 74 65 72 73 20 74 68 61 74 20 61 72 65 0a 2a 2a  ters that are.**
b0860 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68 20 68   NULL or which h
b0870 61 76 65 20 62 65 65 6e 20 70 72 65 76 69 6f 75  ave been previou
b0880 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49 66 20  sly closed.  If 
b0890 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
b08a0 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 6d 65 61  urns.** 1 it mea
b08b0 6e 73 20 74 68 61 74 20 74 68 65 20 64 62 20 70  ns that the db p
b08c0 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 20  ointer is valid 
b08d0 61 6e 64 20 30 20 69 66 20 69 74 20 73 68 6f 75  and 0 if it shou
b08e0 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 64 65 72  ld not be.** der
b08f0 65 66 65 72 65 6e 63 65 64 20 66 6f 72 20 61 6e  eferenced for an
b0900 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 65 20 63  y reason.  The c
b0910 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
b0920 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 0a 2a 2a  should invoke.**
b0930 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69   SQLITE_MISUSE i
b0940 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a  mmediately..**.*
b0950 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  * sqlite3SafetyC
b0960 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 69 72 65  heckOk() require
b0970 73 20 74 68 61 74 20 74 68 65 20 64 62 20 70 6f  s that the db po
b0980 69 6e 74 65 72 20 62 65 20 76 61 6c 69 64 20 66  inter be valid f
b0990 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 71 6c 69  or.** use.  sqli
b09a0 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69  te3SafetyCheckSi
b09b0 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f 77 73 20  ckOrOk() allows 
b09c0 61 20 64 62 20 70 6f 69 6e 74 65 72 20 74 68 61  a db pointer tha
b09d0 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a 2a 20 6f  t failed to.** o
b09e0 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 61 6e 64  pen properly and
b09f0 20 69 73 20 6e 6f 74 20 66 69 74 20 66 6f 72 20   is not fit for 
b0a00 67 65 6e 65 72 61 6c 20 75 73 65 20 62 75 74 20  general use but 
b0a10 77 68 69 63 68 20 63 61 6e 20 62 65 0a 2a 2a 20  which can be.** 
b0a20 75 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d  used as an argum
b0a30 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 65  ent to sqlite3_e
b0a40 72 72 6d 73 67 28 29 20 6f 72 20 73 71 6c 69 74  rrmsg() or sqlit
b0a50 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 53  e3_close()..*/.S
b0a60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b0a70 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  t sqlite3SafetyC
b0a80 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 20 2a  heckOk(sqlite3 *
b0a90 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 69 63  db){.  u32 magic
b0aa0 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 29 20  ;.  if( db==0 ) 
b0ab0 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 61 67 69  return 0;.  magi
b0ac0 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20  c = db->magic;. 
b0ad0 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49   if( magic!=SQLI
b0ae0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 0a 23  TE_MAGIC_OPEN .#
b0af0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
b0b00 55 47 0a 20 20 20 20 20 26 26 20 6d 61 67 69 63  UG.     && magic
b0b10 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  !=SQLITE_MAGIC_B
b0b20 55 53 59 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a  USY.#endif.  ){.
b0b30 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
b0b40 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
b0b50 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54  n 1;.  }.}.SQLIT
b0b60 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b0b70 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
b0b80 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 65 33  SickOrOk(sqlite3
b0b90 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67   *db){.  u32 mag
b0ba0 69 63 3b 0a 20 20 6d 61 67 69 63 20 3d 20 64 62  ic;.  magic = db
b0bb0 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 6d  ->magic;.  if( m
b0bc0 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47  agic!=SQLITE_MAG
b0bd0 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 20 20  IC_SICK &&.     
b0be0 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d   magic!=SQLITE_M
b0bf0 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20  AGIC_OPEN &&.   
b0c00 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45     magic!=SQLITE
b0c10 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 72 65  _MAGIC_BUSY ) re
b0c20 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e  turn 0;.  return
b0c30 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   1;.}../********
b0c40 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74  ****** End of ut
b0c50 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  il.c ***********
b0c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0c80 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
b0c90 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
b0ca0 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e hash.c *******
b0cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0cd0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
b0ce0 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a  1 September 22.*
b0cf0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
b0d00 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
b0d10 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
b0d20 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
b0d30 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
b0d40 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
b0d50 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
b0d60 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
b0d70 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
b0d80 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
b0d90 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
b0da0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
b0db0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
b0dc0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
b0dd0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
b0de0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
b0df0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
b0e00 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
b0e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
b0e50 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70   This is the imp
b0e60 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67  lementation of g
b0e70 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c  eneric hash-tabl
b0e80 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 51  es.** used in SQ
b0e90 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  Lite..**.** $Id:
b0ea0 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 37 20 32   hash.c,v 1.37 2
b0eb0 30 30 39 2f 30 35 2f 30 32 20 31 33 3a 32 39 3a  009/05/02 13:29:
b0ec0 33 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  38 drh Exp $.*/.
b0ed0 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65  ./* Turn bulk me
b0ee0 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68  mory into a hash
b0ef0 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79   table object by
b0f00 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68   initializing th
b0f10 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74  e.** fields of t
b0f20 68 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72  he Hash structur
b0f30 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20  e..**.** "pNew" 
b0f40 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
b0f50 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74  the hash table t
b0f60 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69  hat is to be ini
b0f70 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c  tialized..*/.SQL
b0f80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b0f90 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
b0fa0 28 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a 20 20  (Hash *pNew){.  
b0fb0 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20  assert( pNew!=0 
b0fc0 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74  );.  pNew->first
b0fd0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f   = 0;.  pNew->co
b0fe0 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  unt = 0;.  pNew-
b0ff0 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70  >htsize = 0;.  p
b1000 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a  New->ht = 0;.}..
b1010 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e  /* Remove all en
b1020 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73  tries from a has
b1030 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69  h table.  Reclai
b1040 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  m all memory..**
b1050 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   Call this routi
b1060 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68  ne to delete a h
b1070 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20  ash table or to 
b1080 72 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62  reset a hash tab
b1090 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70  le.** to the emp
b10a0 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c  ty state..*/.SQL
b10b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b10c0 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
b10d0 72 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 48  r(Hash *pH){.  H
b10e0 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
b10f0 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
b1100 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65  oping over all e
b1110 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74  lements of the t
b1120 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  able */..  asser
b1130 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c  t( pH!=0 );.  el
b1140 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a  em = pH->first;.
b1150 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b    pH->first = 0;
b1160 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
b1170 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68  pH->ht);.  pH->h
b1180 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73  t = 0;.  pH->hts
b1190 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  ize = 0;.  while
b11a0 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61  ( elem ){.    Ha
b11b0 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65  shElem *next_ele
b11c0 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
b11d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
b11e0 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d  (elem);.    elem
b11f0 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20   = next_elem;.  
b1200 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20  }.  pH->count = 
b1210 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
b1220 68 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e  hashing function
b1230 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69  ..*/.static unsi
b1240 67 6e 65 64 20 69 6e 74 20 73 74 72 48 61 73 68  gned int strHash
b1250 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
b1260 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74  int nKey){.  int
b1270 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74   h = 0;.  assert
b1280 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 77  ( nKey>=0 );.  w
b1290 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 20  hile( nKey > 0  
b12a0 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33  ){.    h = (h<<3
b12b0 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33 55  ) ^ h ^ sqlite3U
b12c0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73  pperToLower[(uns
b12d0 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d  igned char)*z++]
b12e0 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20  ;.    nKey--;.  
b12f0 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a 7d 0a  }.  return h;.}.
b1300 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 65  ../* Link pNew e
b1310 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  lement into the 
b1320 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20  hash table pH.  
b1330 49 66 20 70 45 6e 74 72 79 21 3d 30 20 74 68 65  If pEntry!=0 the
b1340 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 74  n also.** insert
b1350 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 20 70   pNew into the p
b1360 45 6e 74 72 79 20 68 61 73 68 20 62 75 63 6b 65  Entry hash bucke
b1370 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
b1380 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28  d insertElement(
b1390 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20  .  Hash *pH,    
b13a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
b13b0 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74   complete hash t
b13c0 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  able */.  struct
b13d0 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 20   _ht *pEntry,   
b13e0 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69 6e   /* The entry in
b13f0 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69 73  to which pNew is
b1400 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 48   inserted */.  H
b1410 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20  ashElem *pNew   
b1420 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65        /* The ele
b1430 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72  ment to be inser
b1440 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68  ted */.){.  Hash
b1450 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 20  Elem *pHead;    
b1460 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d     /* First elem
b1470 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 70  ent already in p
b1480 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 20 70  Entry */.  if( p
b1490 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 48 65  Entry ){.    pHe
b14a0 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75  ad = pEntry->cou
b14b0 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61  nt ? pEntry->cha
b14c0 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 6e 74  in : 0;.    pEnt
b14d0 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20  ry->count++;.   
b14e0 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d   pEntry->chain =
b14f0 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a   pNew;.  }else{.
b1500 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b 0a 20      pHead = 0;. 
b1510 20 7d 0a 20 20 69 66 28 20 70 48 65 61 64 20 29   }.  if( pHead )
b1520 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74  {.    pNew->next
b1530 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e   = pHead;.    pN
b1540 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64  ew->prev = pHead
b1550 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20  ->prev;.    if( 
b1560 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70  pHead->prev ){ p
b1570 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74  Head->prev->next
b1580 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65   = pNew; }.    e
b1590 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
b15a0 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e  { pH->first = pN
b15b0 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d  ew; }.    pHead-
b15c0 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20  >prev = pNew;.  
b15d0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d  }else{.    pNew-
b15e0 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73  >next = pH->firs
b15f0 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66  t;.    if( pH->f
b1600 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73  irst ){ pH->firs
b1610 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20  t->prev = pNew; 
b1620 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76  }.    pNew->prev
b1630 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69   = 0;.    pH->fi
b1640 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  rst = pNew;.  }.
b1650 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68  }.../* Resize th
b1660 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20  e hash table so 
b1670 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73  that it cantains
b1680 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b   "new_size" buck
b1690 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68  ets..**.** The h
b16a0 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20  ash table might 
b16b0 66 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 20 69  fail to resize i
b16c0 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  f sqlite3_malloc
b16d0 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 69  () fails or.** i
b16e0 66 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 69  f the new size i
b16f0 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
b1700 65 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a  e prior size..**
b1710 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
b1720 74 68 65 20 72 65 73 69 7a 65 20 6f 63 63 75 72  the resize occur
b1730 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e  s and false if n
b1740 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ot..*/.static in
b1750 74 20 72 65 68 61 73 68 28 48 61 73 68 20 2a 70  t rehash(Hash *p
b1760 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  H, unsigned int 
b1770 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72  new_size){.  str
b1780 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b  uct _ht *new_ht;
b1790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
b17a0 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c  he new hash tabl
b17b0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
b17c0 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65  *elem, *next_ele
b17d0 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f  m;    /* For loo
b17e0 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69  ping over existi
b17f0 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a  ng elements */..
b1800 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  #if SQLITE_MALLO
b1810 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a 20  C_SOFT_LIMIT>0. 
b1820 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69   if( new_size*si
b1830 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29  zeof(struct _ht)
b1840 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53  >SQLITE_MALLOC_S
b1850 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20  OFT_LIMIT ){.   
b1860 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49   new_size = SQLI
b1870 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c  TE_MALLOC_SOFT_L
b1880 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75  IMIT/sizeof(stru
b1890 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69  ct _ht);.  }.  i
b18a0 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d  f( new_size==pH-
b18b0 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e  >htsize ) return
b18c0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a   0;.#endif..  /*
b18d0 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20 74   The inability t
b18e0 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63  o allocates spac
b18f0 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 68  e for a larger h
b1900 61 73 68 20 74 61 62 6c 65 20 69 73 0a 20 20 2a  ash table is.  *
b1910 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  * a performance 
b1920 68 69 74 20 62 75 74 20 69 74 20 69 73 20 6e 6f  hit but it is no
b1930 74 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e  t a fatal error.
b1940 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a 20 20    So mark the.  
b1950 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 73  ** allocation as
b1960 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a 2f 0a   a benign..  */.
b1970 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
b1980 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
b1990 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74  new_ht = (struct
b19a0 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 4d 61   _ht *)sqlite3Ma
b19b0 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73  lloc( new_size*s
b19c0 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74  izeof(struct _ht
b19d0 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e  ) );.  sqlite3En
b19e0 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
b19f0 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d  ..  if( new_ht==
b1a00 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
b1a10 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d  sqlite3_free(pH-
b1a20 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d  >ht);.  pH->ht =
b1a30 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68   new_ht;.  pH->h
b1a40 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65  tsize = new_size
b1a50 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
b1a60 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a  Size(new_ht)/siz
b1a70 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b  eof(struct _ht);
b1a80 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74  .  memset(new_ht
b1a90 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69  , 0, new_size*si
b1aa0 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29  zeof(struct _ht)
b1ab0 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48  );.  for(elem=pH
b1ac0 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72  ->first, pH->fir
b1ad0 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d  st=0; elem; elem
b1ae0 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20   = next_elem){. 
b1af0 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
b1b00 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d  h = strHash(elem
b1b10 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b  ->pKey, elem->nK
b1b20 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b 0a  ey) % new_size;.
b1b30 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20      next_elem = 
b1b40 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20  elem->next;.    
b1b50 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48  insertElement(pH
b1b60 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c  , &new_ht[h], el
b1b70 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  em);.  }.  retur
b1b80 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  n 1;.}../* This 
b1b90 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e  function (for in
b1ba0 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29  ternal use only)
b1bb0 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d   locates an elem
b1bc0 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73  ent in an.** has
b1bd0 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74  h table that mat
b1be0 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b  ches the given k
b1bf0 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f  ey.  The hash fo
b1c00 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a  r this key has.*
b1c10 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  * already been c
b1c20 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70  omputed and is p
b1c30 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68  assed as the 4th
b1c40 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73   parameter..*/.s
b1c50 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a  tatic HashElem *
b1c60 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e  findElementGiven
b1c70 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61  Hash(.  const Ha
b1c80 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54  sh *pH,     /* T
b1c90 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72  he pH to be sear
b1ca0 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ched */.  const 
b1cb0 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a  char *pKey,   /*
b1cc0 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20   The key we are 
b1cd0 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f  searching for */
b1ce0 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20  .  int nKey,    
b1cf0 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
b1d00 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e  in key (not coun
b1d10 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e  ting zero termin
b1d20 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 67  ator) */.  unsig
b1d30 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20 20 2f  ned int h      /
b1d40 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74  * The hash for t
b1d50 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20  his key. */.){. 
b1d60 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b   HashElem *elem;
b1d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1d80 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
b1d90 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
b1da0 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63   list */.  int c
b1db0 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
b1dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
b1dd0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
b1de0 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a  left to test */.
b1df0 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b  .  if( pH->ht ){
b1e00 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20  .    struct _ht 
b1e10 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68  *pEntry = &pH->h
b1e20 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d  t[h];.    elem =
b1e30 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a   pEntry->chain;.
b1e40 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74      count = pEnt
b1e50 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c  ry->count;.  }el
b1e60 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70  se{.    elem = p
b1e70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 6f  H->first;.    co
b1e80 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b  unt = pH->count;
b1e90 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 6f  .  }.  while( co
b1ea0 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28  unt-- && ALWAYS(
b1eb0 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 28  elem) ){.    if(
b1ec0 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65   elem->nKey==nKe
b1ed0 79 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  y && sqlite3StrN
b1ee0 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c  ICmp(elem->pKey,
b1ef0 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b  pKey,nKey)==0 ){
b1f00 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 65   .      return e
b1f10 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65  lem;.    }.    e
b1f20 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  lem = elem->next
b1f30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
b1f40 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61  ;.}../* Remove a
b1f50 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72   single entry fr
b1f60 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
b1f70 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65  e given a pointe
b1f80 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65  r to that.** ele
b1f90 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20  ment and a hash 
b1fa0 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73  on the element's
b1fb0 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   key..*/.static 
b1fc0 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65  void removeEleme
b1fd0 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48  ntGivenHash(.  H
b1fe0 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20  ash *pH,        
b1ff0 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61   /* The pH conta
b2000 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a  ining "elem" */.
b2010 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d    HashElem* elem
b2020 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65  ,   /* The eleme
b2030 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64  nt to be removed
b2040 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a   from the pH */.
b2050 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
b2060 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75      /* Hash valu
b2070 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e  e for the elemen
b2080 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74  t */.){.  struct
b2090 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20   _ht *pEntry;.  
b20a0 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29  if( elem->prev )
b20b0 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76  {.    elem->prev
b20c0 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e  ->next = elem->n
b20d0 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20  ext; .  }else{. 
b20e0 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65     pH->first = e
b20f0 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20  lem->next;.  }. 
b2100 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20   if( elem->next 
b2110 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78  ){.    elem->nex
b2120 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e  t->prev = elem->
b2130 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20  prev;.  }.  if( 
b2140 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 45  pH->ht ){.    pE
b2150 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68  ntry = &pH->ht[h
b2160 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 6e 74 72  ];.    if( pEntr
b2170 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29  y->chain==elem )
b2180 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 2d 3e  {.      pEntry->
b2190 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65  chain = elem->ne
b21a0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45  xt;.    }.    pE
b21b0 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20  ntry->count--;. 
b21c0 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 74 72     assert( pEntr
b21d0 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20  y->count>=0 );. 
b21e0 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
b21f0 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d  e( elem );.  pH-
b2200 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20  >count--;.  if( 
b2210 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a  pH->count<=0 ){.
b2220 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e      assert( pH->
b2230 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20  first==0 );.    
b2240 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e  assert( pH->coun
b2250 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69  t==0 );.    sqli
b2260 74 65 33 48 61 73 68 43 6c 65 61 72 28 70 48 29  te3HashClear(pH)
b2270 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65  ;.  }.}../* Atte
b2280 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e  mpt to locate an
b2290 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
b22a0 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69  hash table pH wi
b22b0 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74  th a key.** that
b22c0 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b   matches pKey,nK
b22d0 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  ey.  Return the 
b22e0 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c  data for this el
b22f0 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a  ement if it is.*
b2300 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c  * found, or NULL
b2310 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
b2320 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45  match..*/.SQLITE
b2330 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
b2340 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63  qlite3HashFind(c
b2350 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63  onst Hash *pH, c
b2360 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c  onst char *pKey,
b2370 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61   int nKey){.  Ha
b2380 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20  shElem *elem;   
b2390 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20   /* The element 
b23a0 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79  that matches key
b23b0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
b23c0 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61  nt h;    /* A ha
b23d0 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20  sh on key */..  
b23e0 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b  assert( pH!=0 );
b23f0 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 21  .  assert( pKey!
b2400 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
b2410 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28  nKey>=0 );.  if(
b2420 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 68   pH->ht ){.    h
b2430 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c   = strHash(pKey,
b2440 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73   nKey) % pH->hts
b2450 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ize;.  }else{.  
b2460 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65    h = 0;.  }.  e
b2470 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e  lem = findElemen
b2480 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c 20 70  tGivenHash(pH, p
b2490 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a 20  Key, nKey, h);. 
b24a0 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65   return elem ? e
b24b0 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d  lem->data : 0;.}
b24c0 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65  ../* Insert an e
b24d0 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  lement into the 
b24e0 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20  hash table pH.  
b24f0 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c  The key is pKey,
b2500 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  nKey.** and the 
b2510 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a  data is "data"..
b2520 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d  **.** If no elem
b2530 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20  ent exists with 
b2540 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20  a matching key, 
b2550 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c  then a new.** el
b2560 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64  ement is created
b2570 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74   and NULL is ret
b2580 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
b2590 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20  another element 
b25a0 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 77  already exists w
b25b0 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79  ith the same key
b25c0 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65  , then the.** ne
b25d0 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 20  w data replaces 
b25e0 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64  the old data and
b25f0 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73   the old data is
b2600 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68   returned..** Th
b2610 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70  e key is not cop
b2620 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74  ied in this inst
b2630 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c  ance.  If a mall
b2640 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a  oc fails, then.*
b2650 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69  * the new data i
b2660 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
b2670 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  he hash table is
b2680 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a   unchanged..**.*
b2690 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22 20  * If the "data" 
b26a0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69  parameter to thi
b26b0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55  s function is NU
b26c0 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  LL, then the.** 
b26d0 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f  element correspo
b26e0 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69  nding to "key" i
b26f0 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  s removed from t
b2700 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  he hash table..*
b2710 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b2720 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61   void *sqlite3Ha
b2730 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a 70  shInsert(Hash *p
b2740 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  H, const char *p
b2750 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76  Key, int nKey, v
b2760 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 6e  oid *data){.  un
b2770 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20  signed int h;   
b2780 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20      /* the hash 
b2790 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c  of the key modul
b27a0 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a  o hash table siz
b27b0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
b27c0 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20  *elem;       /* 
b27d0 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72  Used to loop thr
b27e0 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69  u the element li
b27f0 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  st */.  HashElem
b2800 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a   *new_elem;   /*
b2810 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64   New element add
b2820 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a  ed to the pH */.
b2830 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30  .  assert( pH!=0
b2840 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b   );.  assert( pK
b2850 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ey!=0 );.  asser
b2860 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20  t( nKey>=0 );.  
b2870 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29  if( pH->htsize )
b2880 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73  {.    h = strHas
b2890 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20  h(pKey, nKey) % 
b28a0 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65  pH->htsize;.  }e
b28b0 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a  lse{.    h = 0;.
b28c0 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e    }.  elem = fin
b28d0 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  dElementGivenHas
b28e0 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68  h(pH,pKey,nKey,h
b28f0 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b  );.  if( elem ){
b2900 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64  .    void *old_d
b2910 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61  ata = elem->data
b2920 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d  ;.    if( data==
b2930 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76  0 ){.      remov
b2940 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  eElementGivenHas
b2950 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20  h(pH,elem,h);.  
b2960 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65    }else{.      e
b2970 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61  lem->data = data
b2980 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b  ;.      elem->pK
b2990 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20  ey = pKey;.     
b29a0 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c   assert(nKey==el
b29b0 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 7d  em->nKey);.    }
b29c0 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f  .    return old_
b29d0 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20  data;.  }.  if( 
b29e0 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e  data==0 ) return
b29f0 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d   0;.  new_elem =
b2a00 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c 69   (HashElem*)sqli
b2a10 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  te3Malloc( sizeo
b2a20 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20  f(HashElem) );. 
b2a30 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30   if( new_elem==0
b2a40 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a   ) return data;.
b2a50 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79    new_elem->pKey
b2a60 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f 65   = pKey;.  new_e
b2a70 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79  lem->nKey = nKey
b2a80 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61  ;.  new_elem->da
b2a90 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 2d  ta = data;.  pH-
b2aa0 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20  >count++;.  if( 
b2ab0 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 26  pH->count>=10 &&
b2ac0 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a 70   pH->count > 2*p
b2ad0 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20  H->htsize ){.   
b2ae0 20 69 66 28 20 72 65 68 61 73 68 28 70 48 2c 20   if( rehash(pH, 
b2af0 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 26 26 20  pH->count*2) && 
b2b00 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20  pH->htsize ){.  
b2b10 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28      h = strHash(
b2b20 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48  pKey, nKey) % pH
b2b30 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d 0a  ->htsize;.    }.
b2b40 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74    }.  if( pH->ht
b2b50 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c   ){.    insertEl
b2b60 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68  ement(pH, &pH->h
b2b70 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b  t[h], new_elem);
b2b80 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
b2b90 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
b2ba0 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20  0, new_elem);.  
b2bb0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
b2bc0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b2bd0 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a   End of hash.c *
b2be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b2c10 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b2c20 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f   Begin file opco
b2c30 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  des.c **********
b2c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b2c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b2c60 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c  ./* Automaticall
b2c70 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f  y generated.  Do
b2c80 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20   not edit */./* 
b2c90 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  See the mkopcode
b2ca0 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72  c.awk script for
b2cb0 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66   details. */.#if
b2cc0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
b2cd0 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c  _OMIT_EXPLAIN) |
b2ce0 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  | !defined(NDEBU
b2cf0 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44  G) || defined(VD
b2d00 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64  BE_PROFILE) || d
b2d10 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
b2d20 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  BUG).SQLITE_PRIV
b2d30 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
b2d40 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d  sqlite3OpcodeNam
b2d50 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69  e(int i){. stati
b2d60 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  c const char *co
b2d70 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b  nst azName[] = {
b2d80 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20   "?",.     /*   
b2d90 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20 20  1 */ "VNext",.  
b2da0 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 41 66     /*   2 */ "Af
b2db0 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a  finity",.     /*
b2dc0 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22     3 */ "Column"
b2dd0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f  ,.     /*   4 */
b2de0 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20 20   "SetCookie",.  
b2df0 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 53 65     /*   5 */ "Se
b2e00 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36  ek",.     /*   6
b2e10 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 2c 0a   */ "Sequence",.
b2e20 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22       /*   7 */ "
b2e30 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 20 20 20  Savepoint",.    
b2e40 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 6f 77 4b   /*   8 */ "RowK
b2e50 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39  ey",.     /*   9
b2e60 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 20 20 20   */ "SCopy",.   
b2e70 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 4f 70 65    /*  10 */ "Ope
b2e80 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a  nWrite",.     /*
b2e90 20 20 31 31 20 2a 2f 20 22 49 66 22 2c 0a 20 20    11 */ "If",.  
b2ea0 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 43 6f     /*  12 */ "Co
b2eb0 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a 20  llSeq",.     /* 
b2ec0 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64   13 */ "OpenRead
b2ed0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 2a  ",.     /*  14 *
b2ee0 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 20 20  / "Expire",.    
b2ef0 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 75 74 6f   /*  15 */ "Auto
b2f00 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a  Commit",.     /*
b2f10 20 20 31 36 20 2a 2f 20 22 50 61 67 65 63 6f 75    16 */ "Pagecou
b2f20 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 37  nt",.     /*  17
b2f30 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43 6b   */ "IntegrityCk
b2f40 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 20 2a  ",.     /*  18 *
b2f50 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f  / "Sort",.     /
b2f60 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 2c 0a  *  19 */ "Not",.
b2f70 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20 22       /*  20 */ "
b2f80 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Copy",.     /*  
b2f90 32 31 20 2a 2f 20 22 54 72 61 63 65 22 2c 0a 20  21 */ "Trace",. 
b2fa0 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 46      /*  22 */ "F
b2fb0 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f  unction",.     /
b2fc0 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e 65 67 22  *  23 */ "IfNeg"
b2fd0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a 2f  ,.     /*  24 */
b2fe0 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 2f 2a   "Noop",.     /*
b2ff0 20 20 32 35 20 2a 2f 20 22 52 65 74 75 72 6e 22    25 */ "Return"
b3000 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f  ,.     /*  26 */
b3010 20 22 4e 65 77 52 6f 77 69 64 22 2c 0a 20 20 20   "NewRowid",.   
b3020 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 56 61 72    /*  27 */ "Var
b3030 69 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20  iable",.     /* 
b3040 20 32 38 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c   28 */ "String",
b3050 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a 2f 20  .     /*  29 */ 
b3060 22 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a  "RealAffinity",.
b3070 20 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22       /*  30 */ "
b3080 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f  VRename",.     /
b3090 2a 20 20 33 31 20 2a 2f 20 22 50 61 72 73 65 53  *  31 */ "ParseS
b30a0 63 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20  chema",.     /* 
b30b0 20 33 32 20 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a   32 */ "VOpen",.
b30c0 20 20 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22       /*  33 */ "
b30d0 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20  Close",.     /* 
b30e0 20 33 34 20 2a 2f 20 22 43 72 65 61 74 65 49 6e   34 */ "CreateIn
b30f0 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  dex",.     /*  3
b3100 35 20 2a 2f 20 22 49 73 55 6e 69 71 75 65 22 2c  5 */ "IsUnique",
b3110 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20  .     /*  36 */ 
b3120 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20  "NotFound",.    
b3130 20 2f 2a 20 20 33 37 20 2a 2f 20 22 49 6e 74 36   /*  37 */ "Int6
b3140 34 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20  4",.     /*  38 
b3150 2a 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a  */ "MustBeInt",.
b3160 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22       /*  39 */ "
b3170 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Halt",.     /*  
b3180 34 30 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20  40 */ "Rowid",. 
b3190 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22 49      /*  41 */ "I
b31a0 64 78 4c 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20  dxLT",.     /*  
b31b0 34 32 20 2a 2f 20 22 41 64 64 49 6d 6d 22 2c 0a  42 */ "AddImm",.
b31c0 20 20 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22       /*  43 */ "
b31d0 53 74 61 74 65 6d 65 6e 74 22 2c 0a 20 20 20 20  Statement",.    
b31e0 20 2f 2a 20 20 34 34 20 2a 2f 20 22 52 6f 77 44   /*  44 */ "RowD
b31f0 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  ata",.     /*  4
b3200 35 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20  5 */ "MemMax",. 
b3210 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4e      /*  46 */ "N
b3220 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20  otExists",.     
b3230 2f 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75 62  /*  47 */ "Gosub
b3240 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a  ",.     /*  48 *
b3250 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20  / "Integer",.   
b3260 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 50 72 65    /*  49 */ "Pre
b3270 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20  v",.     /*  50 
b3280 2a 2f 20 22 52 6f 77 53 65 74 52 65 61 64 22 2c  */ "RowSetRead",
b3290 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20  .     /*  51 */ 
b32a0 22 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20  "RowSetAdd",.   
b32b0 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 56 43 6f    /*  52 */ "VCo
b32c0 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20  lumn",.     /*  
b32d0 35 33 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62  53 */ "CreateTab
b32e0 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34  le",.     /*  54
b32f0 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 20   */ "Last",.    
b3300 20 2f 2a 20 20 35 35 20 2a 2f 20 22 53 65 65 6b   /*  55 */ "Seek
b3310 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36  Le",.     /*  56
b3320 20 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22   */ "IncrVacuum"
b3330 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f  ,.     /*  57 */
b3340 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20   "IdxRowid",.   
b3350 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 52 65 73    /*  58 */ "Res
b3360 65 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f  etCount",.     /
b3370 2a 20 20 35 39 20 2a 2f 20 22 43 6f 6e 74 65 78  *  59 */ "Contex
b3380 74 50 75 73 68 22 2c 0a 20 20 20 20 20 2f 2a 20  tPush",.     /* 
b3390 20 36 30 20 2a 2f 20 22 59 69 65 6c 64 22 2c 0a   60 */ "Yield",.
b33a0 20 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22       /*  61 */ "
b33b0 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20  DropTrigger",.  
b33c0 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 72     /*  62 */ "Dr
b33d0 6f 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f  opIndex",.     /
b33e0 2a 20 20 36 33 20 2a 2f 20 22 49 64 78 47 45 22  *  63 */ "IdxGE"
b33f0 2c 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f  ,.     /*  64 */
b3400 20 22 49 64 78 44 65 6c 65 74 65 22 2c 0a 20 20   "IdxDelete",.  
b3410 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22 56 61     /*  65 */ "Va
b3420 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20  cuum",.     /*  
b3430 36 36 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20  66 */ "Or",.    
b3440 20 2f 2a 20 20 36 37 20 2a 2f 20 22 41 6e 64 22   /*  67 */ "And"
b3450 2c 0a 20 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f  ,.     /*  68 */
b3460 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f   "IfNot",.     /
b3470 2a 20 20 36 39 20 2a 2f 20 22 44 72 6f 70 54 61  *  69 */ "DropTa
b3480 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  ble",.     /*  7
b3490 30 20 2a 2f 20 22 53 65 65 6b 4c 74 22 2c 0a 20  0 */ "SeekLt",. 
b34a0 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22 49      /*  71 */ "I
b34b0 73 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20  sNull",.     /* 
b34c0 20 37 32 20 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22   72 */ "NotNull"
b34d0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f  ,.     /*  73 */
b34e0 20 22 4e 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20   "Ne",.     /*  
b34f0 37 34 20 2a 2f 20 22 45 71 22 2c 0a 20 20 20 20  74 */ "Eq",.    
b3500 20 2f 2a 20 20 37 35 20 2a 2f 20 22 47 74 22 2c   /*  75 */ "Gt",
b3510 0a 20 20 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20  .     /*  76 */ 
b3520 22 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  "Le",.     /*  7
b3530 37 20 2a 2f 20 22 4c 74 22 2c 0a 20 20 20 20 20  7 */ "Lt",.     
b3540 2f 2a 20 20 37 38 20 2a 2f 20 22 47 65 22 2c 0a  /*  78 */ "Ge",.
b3550 20 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22       /*  79 */ "
b3560 4d 61 6b 65 52 65 63 6f 72 64 22 2c 0a 20 20 20  MakeRecord",.   
b3570 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 42 69 74    /*  80 */ "Bit
b3580 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  And",.     /*  8
b3590 31 20 2a 2f 20 22 42 69 74 4f 72 22 2c 0a 20 20  1 */ "BitOr",.  
b35a0 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20 22 53 68     /*  82 */ "Sh
b35b0 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 20 20 2f  iftLeft",.     /
b35c0 2a 20 20 38 33 20 2a 2f 20 22 53 68 69 66 74 52  *  83 */ "ShiftR
b35d0 69 67 68 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ight",.     /*  
b35e0 38 34 20 2a 2f 20 22 41 64 64 22 2c 0a 20 20 20  84 */ "Add",.   
b35f0 20 20 2f 2a 20 20 38 35 20 2a 2f 20 22 53 75 62    /*  85 */ "Sub
b3600 74 72 61 63 74 22 2c 0a 20 20 20 20 20 2f 2a 20  tract",.     /* 
b3610 20 38 36 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 79   86 */ "Multiply
b3620 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 37 20 2a  ",.     /*  87 *
b3630 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 20 20 20  / "Divide",.    
b3640 20 2f 2a 20 20 38 38 20 2a 2f 20 22 52 65 6d 61   /*  88 */ "Rema
b3650 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  inder",.     /* 
b3660 20 38 39 20 2a 2f 20 22 43 6f 6e 63 61 74 22 2c   89 */ "Concat",
b3670 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a 2f 20  .     /*  90 */ 
b3680 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 20 20  "ResultRow",.   
b3690 20 20 2f 2a 20 20 39 31 20 2a 2f 20 22 44 65 6c    /*  91 */ "Del
b36a0 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  ete",.     /*  9
b36b0 32 20 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22 2c  2 */ "AggFinal",
b36c0 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20  .     /*  93 */ 
b36d0 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f  "BitNot",.     /
b36e0 2a 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e 67  *  94 */ "String
b36f0 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 35 20  8",.     /*  95 
b3700 2a 2f 20 22 43 6f 6d 70 61 72 65 22 2c 0a 20 20  */ "Compare",.  
b3710 20 20 20 2f 2a 20 20 39 36 20 2a 2f 20 22 47 6f     /*  96 */ "Go
b3720 74 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37  to",.     /*  97
b3730 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 63 6b 22 2c   */ "TableLock",
b3740 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 2a 2f 20  .     /*  98 */ 
b3750 22 43 6c 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a  "Clear",.     /*
b3760 20 20 39 39 20 2a 2f 20 22 56 65 72 69 66 79 43    99 */ "VerifyC
b3770 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20  ookie",.     /* 
b3780 31 30 30 20 2a 2f 20 22 41 67 67 53 74 65 70 22  100 */ "AggStep"
b3790 2c 0a 20 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f  ,.     /* 101 */
b37a0 20 22 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 22   "SetNumColumns"
b37b0 2c 0a 20 20 20 20 20 2f 2a 20 31 30 32 20 2a 2f  ,.     /* 102 */
b37c0 20 22 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c 0a   "Transaction",.
b37d0 20 20 20 20 20 2f 2a 20 31 30 33 20 2a 2f 20 22       /* 103 */ "
b37e0 56 46 69 6c 74 65 72 22 2c 0a 20 20 20 20 20 2f  VFilter",.     /
b37f0 2a 20 31 30 34 20 2a 2f 20 22 56 44 65 73 74 72  * 104 */ "VDestr
b3800 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 35  oy",.     /* 105
b3810 20 2a 2f 20 22 43 6f 6e 74 65 78 74 50 6f 70 22   */ "ContextPop"
b3820 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20 2a 2f  ,.     /* 106 */
b3830 20 22 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a   "Next",.     /*
b3840 20 31 30 37 20 2a 2f 20 22 43 6f 75 6e 74 22 2c   107 */ "Count",
b3850 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20  .     /* 108 */ 
b3860 22 49 64 78 49 6e 73 65 72 74 22 2c 0a 20 20 20  "IdxInsert",.   
b3870 20 20 2f 2a 20 31 30 39 20 2a 2f 20 22 53 65 65    /* 109 */ "See
b3880 6b 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  kGe",.     /* 11
b3890 30 20 2a 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20  0 */ "Insert",. 
b38a0 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 44      /* 111 */ "D
b38b0 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a  estroy",.     /*
b38c0 20 31 31 32 20 2a 2f 20 22 52 65 61 64 43 6f 6f   112 */ "ReadCoo
b38d0 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  kie",.     /* 11
b38e0 33 20 2a 2f 20 22 52 6f 77 53 65 74 54 65 73 74  3 */ "RowSetTest
b38f0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 20 2a  ",.     /* 114 *
b3900 2f 20 22 4c 6f 61 64 41 6e 61 6c 79 73 69 73 22  / "LoadAnalysis"
b3910 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f  ,.     /* 115 */
b3920 20 22 45 78 70 6c 61 69 6e 22 2c 0a 20 20 20 20   "Explain",.    
b3930 20 2f 2a 20 31 31 36 20 2a 2f 20 22 48 61 6c 74   /* 116 */ "Halt
b3940 49 66 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a  IfNull",.     /*
b3950 20 31 31 37 20 2a 2f 20 22 4f 70 65 6e 50 73 65   117 */ "OpenPse
b3960 75 64 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  udo",.     /* 11
b3970 38 20 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65  8 */ "OpenEpheme
b3980 72 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  ral",.     /* 11
b3990 39 20 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20  9 */ "Null",.   
b39a0 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 4d 6f 76    /* 120 */ "Mov
b39b0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 31 20  e",.     /* 121 
b39c0 2a 2f 20 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20  */ "Blob",.     
b39d0 2f 2a 20 31 32 32 20 2a 2f 20 22 52 65 77 69 6e  /* 122 */ "Rewin
b39e0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20  d",.     /* 123 
b39f0 2a 2f 20 22 53 65 65 6b 47 74 22 2c 0a 20 20 20  */ "SeekGt",.   
b3a00 20 20 2f 2a 20 31 32 34 20 2a 2f 20 22 56 42 65    /* 124 */ "VBe
b3a10 67 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32  gin",.     /* 12
b3a20 35 20 2a 2f 20 22 56 55 70 64 61 74 65 22 2c 0a  5 */ "VUpdate",.
b3a30 20 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22       /* 126 */ "
b3a40 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a  IfZero",.     /*
b3a50 20 31 32 37 20 2a 2f 20 22 56 43 72 65 61 74 65   127 */ "VCreate
b3a60 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20 2a  ",.     /* 128 *
b3a70 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20  / "Found",.     
b3a80 2f 2a 20 31 32 39 20 2a 2f 20 22 49 66 50 6f 73  /* 129 */ "IfPos
b3a90 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 30 20 2a  ",.     /* 130 *
b3aa0 2f 20 22 52 65 61 6c 22 2c 0a 20 20 20 20 20 2f  / "Real",.     /
b3ab0 2a 20 31 33 31 20 2a 2f 20 22 4e 75 6c 6c 52 6f  * 131 */ "NullRo
b3ac0 77 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32 20  w",.     /* 132 
b3ad0 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 20 20 20  */ "Jump",.     
b3ae0 2f 2a 20 31 33 33 20 2a 2f 20 22 50 65 72 6d 75  /* 133 */ "Permu
b3af0 74 61 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a  tation",.     /*
b3b00 20 31 33 34 20 2a 2f 20 22 4e 6f 74 55 73 65 64   134 */ "NotUsed
b3b10 5f 31 33 34 22 2c 0a 20 20 20 20 20 2f 2a 20 31  _134",.     /* 1
b3b20 33 35 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31  35 */ "NotUsed_1
b3b30 33 35 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 36  35",.     /* 136
b3b40 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 36   */ "NotUsed_136
b3b50 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 37 20 2a  ",.     /* 137 *
b3b60 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 37 22 2c  / "NotUsed_137",
b3b70 0a 20 20 20 20 20 2f 2a 20 31 33 38 20 2a 2f 20  .     /* 138 */ 
b3b80 22 4e 6f 74 55 73 65 64 5f 31 33 38 22 2c 0a 20  "NotUsed_138",. 
b3b90 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20 22 4e      /* 139 */ "N
b3ba0 6f 74 55 73 65 64 5f 31 33 39 22 2c 0a 20 20 20  otUsed_139",.   
b3bb0 20 20 2f 2a 20 31 34 30 20 2a 2f 20 22 4e 6f 74    /* 140 */ "Not
b3bc0 55 73 65 64 5f 31 34 30 22 2c 0a 20 20 20 20 20  Used_140",.     
b3bd0 2f 2a 20 31 34 31 20 2a 2f 20 22 54 6f 54 65 78  /* 141 */ "ToTex
b3be0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 32 20  t",.     /* 142 
b3bf0 2a 2f 20 22 54 6f 42 6c 6f 62 22 2c 0a 20 20 20  */ "ToBlob",.   
b3c00 20 20 2f 2a 20 31 34 33 20 2a 2f 20 22 54 6f 4e    /* 143 */ "ToN
b3c10 75 6d 65 72 69 63 22 2c 0a 20 20 20 20 20 2f 2a  umeric",.     /*
b3c20 20 31 34 34 20 2a 2f 20 22 54 6f 49 6e 74 22 2c   144 */ "ToInt",
b3c30 0a 20 20 20 20 20 2f 2a 20 31 34 35 20 2a 2f 20  .     /* 145 */ 
b3c40 22 54 6f 52 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20  "ToReal",.  };. 
b3c50 20 72 65 74 75 72 6e 20 61 7a 4e 61 6d 65 5b 69   return azName[i
b3c60 5d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  ];.}.#endif../**
b3c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
b3c80 20 6f 66 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a   of opcodes.c **
b3c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
b3cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
b3cd0 69 6e 20 66 69 6c 65 20 6f 73 5f 6f 73 32 2e 63  in file os_os2.c
b3ce0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
b3cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
b3d10 2a 2a 20 32 30 30 36 20 46 65 62 20 31 34 0a 2a  ** 2006 Feb 14.*
b3d20 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
b3d30 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
b3d40 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
b3d50 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
b3d60 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
b3d70 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
b3d80 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
b3d90 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
b3da0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
b3db0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
b3dc0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
b3dd0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
b3de0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
b3df0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
b3e00 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
b3e10 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
b3e20 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
b3e30 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
b3e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3e80 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
b3e90 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
b3ea0 20 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69   that is specifi
b3eb0 63 20 74 6f 20 4f 53 2f 32 2e 0a 2a 2a 0a 2a 2a  c to OS/2..**.**
b3ec0 20 24 49 64 3a 20 6f 73 5f 6f 73 32 2e 63 2c 76   $Id: os_os2.c,v
b3ed0 20 31 2e 36 33 20 32 30 30 38 2f 31 32 2f 31 30   1.63 2008/12/10
b3ee0 20 31 39 3a 32 36 3a 32 34 20 64 72 68 20 45 78   19:26:24 drh Ex
b3ef0 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c  p $.*/...#if SQL
b3f00 49 54 45 5f 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a  ITE_OS_OS2../*.*
b3f10 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d  * A Note About M
b3f20 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e  emory Allocation
b3f30 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69  :.**.** This dri
b3f40 76 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28  ver uses malloc(
b3f50 29 2f 66 72 65 65 28 29 20 64 69 72 65 63 74 6c  )/free() directl
b3f60 79 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 6f  y rather than go
b3f70 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  ing through.** t
b3f80 68 65 20 53 51 4c 69 74 65 2d 77 72 61 70 70 65  he SQLite-wrappe
b3f90 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  rs sqlite3_mallo
b3fa0 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 65 65  c()/sqlite3_free
b3fb0 28 29 2e 20 20 54 68 6f 73 65 20 77 72 61 70 70  ().  Those wrapp
b3fc0 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 69 67  ers.** are desig
b3fd0 6e 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 20 65  ned for use on e
b3fe0 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 20  mbedded systems 
b3ff0 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 73 20  where memory is 
b4000 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61  scarce and.** ma
b4010 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 68 61  lloc failures ha
b4020 70 70 65 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e  ppen frequently.
b4030 20 20 4f 53 2f 32 20 64 6f 65 73 20 6e 6f 74 20    OS/2 does not 
b4040 74 79 70 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e  typically run on
b4050 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 73 79 73  .** embedded sys
b4060 74 65 6d 73 2c 20 61 6e 64 20 77 68 65 6e 20 69  tems, and when i
b4070 74 20 64 6f 65 73 20 74 68 65 20 64 65 76 65 6c  t does the devel
b4080 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68  opers normally h
b4090 61 76 65 20 62 69 67 67 65 72 0a 2a 2a 20 70 72  ave bigger.** pr
b40a0 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72 72 79 20  oblems to worry 
b40b0 61 62 6f 75 74 20 74 68 61 6e 20 72 75 6e 6e 69  about than runni
b40c0 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  ng out of memory
b40d0 2e 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e  .  So there is n
b40e0 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69  ot.** a compelli
b40f0 6e 67 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74  ng need to use t
b4100 68 65 20 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a  he wrappers..**.
b4110 2a 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20  ** But there is 
b4120 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f  a good reason to
b4130 20 6e 6f 74 20 75 73 65 20 74 68 65 20 77 72 61   not use the wra
b4140 70 70 65 72 73 2e 20 20 49 66 20 77 65 20 75 73  ppers.  If we us
b4150 65 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72  e the.** wrapper
b4160 73 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67  s then we will g
b4170 65 74 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c  et simulated mal
b4180 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 77  loc() failures w
b4190 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72  ithin this.** dr
b41a0 69 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20  iver.  And that 
b41b0 63 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73  causes all kinds
b41c0 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72   of problems for
b41d0 20 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0a   our tests.  We.
b41e0 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65  ** could enhance
b41f0 20 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20   SQLite to deal 
b4200 77 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d  with simulated m
b4210 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77  alloc failures w
b4220 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20  ithin.** the OS 
b4230 64 72 69 76 65 72 2c 20 62 75 74 20 74 68 65 20  driver, but the 
b4240 63 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69 74  code to deal wit
b4250 68 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65 20  h those failure 
b4260 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20  would not.** be 
b4270 65 78 65 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e  exercised on Lin
b4280 75 78 20 28 77 68 69 63 68 20 64 6f 65 73 20 6e  ux (which does n
b4290 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f  ot need to mallo
b42a0 63 28 29 20 69 6e 20 74 68 65 20 64 72 69 76 65  c() in the drive
b42b0 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20  r).** and so we 
b42c0 77 6f 75 6c 64 20 68 61 76 65 20 64 69 66 66 69  would have diffi
b42d0 63 75 6c 74 79 20 77 72 69 74 69 6e 67 20 63 6f  culty writing co
b42e0 76 65 72 61 67 65 20 74 65 73 74 73 20 66 6f 72  verage tests for
b42f0 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20   that.** code.  
b4300 42 65 74 74 65 72 20 74 6f 20 6c 65 61 76 65 20  Better to leave 
b4310 74 68 65 20 63 6f 64 65 20 6f 75 74 2c 20 77 65  the code out, we
b4320 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68   think..**.** Th
b4330 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20  e point of this 
b4340 64 69 73 63 75 73 73 69 6f 6e 20 69 73 20 61 73  discussion is as
b4350 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20   follows:  When 
b4360 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a  creating a new.*
b4370 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f 72 20 61  * OS layer for a
b4380 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
b4390 6d 2c 20 69 66 20 79 6f 75 20 75 73 65 20 74 68  m, if you use th
b43a0 69 73 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78  is file as an ex
b43b0 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20  ample,.** avoid 
b43c0 74 68 65 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f  the use of mallo
b43d0 63 28 29 2f 66 72 65 65 28 29 2e 20 20 54 68 6f  c()/free().  Tho
b43e0 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b  se routines work
b43f0 20 6f 6b 20 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64   ok on OS/2.** d
b4400 65 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20  esktops but not 
b4410 73 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64  so well in embed
b4420 64 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a  ded systems..*/.
b4430 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73  ./*.** Macros us
b4440 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
b4450 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
b4460 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a  o use threads..*
b4470 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
b4480 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29  LITE_THREADSAFE)
b4490 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41   && SQLITE_THREA
b44a0 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53  DSAFE.# define S
b44b0 51 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45 41 44  QLITE_OS2_THREAD
b44c0 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  S 1.#endif../*.*
b44d0 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74  * Include code t
b44e0 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f  hat is common to
b44f0 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65   all os_*.c file
b4500 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.*/./**********
b4510 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f  **** Include os_
b4520 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20  common.h in the 
b4530 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 6f 73 32  middle of os_os2
b4540 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
b4550 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
b4560 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
b4570 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
b4580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b45a0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
b45b0 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65  May 22.**.** The
b45c0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
b45d0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
b45e0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
b45f0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
b4600 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
b4610 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
b4620 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
b4630 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
b4640 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
b4650 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
b4660 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
b4670 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
b4680 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
b4690 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
b46a0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
b46b0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
b46c0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
b46d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b46e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b46f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
b4720 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
b4730 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61  ins macros and a
b4740 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63   little bit of c
b4750 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d  ode that is comm
b4760 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20  on to.** all of 
b4770 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65  the platform-spe
b4780 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f  cific files (os_
b4790 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63  *.c) and is #inc
b47a0 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65  luded into those
b47b0 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  .** files..**.**
b47c0 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c   This file shoul
b47d0 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62  d be #included b
b47e0 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c  y the os_*.c fil
b47f0 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20  es only.  It is 
b4800 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c  not a.** general
b4810 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20   purpose header 
b4820 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  file..**.** $Id:
b4830 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31   os_common.h,v 1
b4840 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 20 31  .38 2009/02/24 1
b4850 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31  8:40:50 danielk1
b4860 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  977 Exp $.*/.#if
b4870 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f  ndef _OS_COMMON_
b4880 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43  H_.#define _OS_C
b4890 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  OMMON_H_../*.** 
b48a0 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67  At least two bug
b48b0 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69  s have slipped i
b48c0 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61  n because we cha
b48d0 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f  nged the MEMORY_
b48e0 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74  DEBUG.** macro t
b48f0 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61  o SQLITE_DEBUG a
b4900 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61  nd some older ma
b4910 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74  kefiles have not
b4920 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a   yet made the.**
b4930 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f   switch.  The fo
b4940 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f  llowing code sho
b4950 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70  uld catch this p
b4960 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c  roblem at compil
b4970 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65  e-time..*/.#ifde
b4980 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23  f MEMORY_DEBUG.#
b4990 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f   error "The MEMO
b49a0 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69  RY_DEBUG macro i
b49b0 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65  s obsolete.  Use
b49c0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e   SQLITE_DEBUG in
b49d0 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a  stead.".#endif..
b49e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
b49f0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
b4a00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53  TE int sqlite3OS
b4a10 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69  Trace = 0;.#defi
b4a20 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20  ne OSTRACE1(X)  
b4a30 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
b4a40 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
b4a50 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
b4a60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
b4a70 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66  E2(X,Y)       if
b4a80 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
b4a90 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
b4aa0 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69  rintf(X,Y).#defi
b4ab0 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
b4ac0 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  Z)     if( sqlit
b4ad0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
b4ae0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
b4af0 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Y,Z).#define OS
b4b00 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20  TRACE4(X,Y,Z,A) 
b4b10 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
b4b20 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
b4b30 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
b4b40 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
b4b50 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69  CE5(X,Y,Z,A,B) i
b4b60 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
b4b70 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
b4b80 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
b4b90 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
b4ba0 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20  E6(X,Y,Z,A,B,C) 
b4bb0 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
b4bc0 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
b4bd0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
b4be0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
b4bf0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
b4c00 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69  A,B,C,D) \.    i
b4c10 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
b4c20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
b4c30 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
b4c40 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  ,D).#else.#defin
b4c50 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64  e OSTRACE1(X).#d
b4c60 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58  efine OSTRACE2(X
b4c70 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
b4c80 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66  ACE3(X,Y,Z).#def
b4c90 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59  ine OSTRACE4(X,Y
b4ca0 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Z,A).#define OS
b4cb0 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE5(X,Y,Z,A,B
b4cc0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
b4cd0 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  E6(X,Y,Z,A,B,C).
b4ce0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
b4cf0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a  (X,Y,Z,A,B,C,D).
b4d00 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
b4d10 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  cros for perform
b4d20 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e  ance tracing.  N
b4d30 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f  ormally turned o
b4d40 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a  ff.  Only works.
b4d50 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77  ** on i486 hardw
b4d60 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  are..*/.#ifdef S
b4d70 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43  QLITE_PERFORMANC
b4d80 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20  E_TRACE../* .** 
b4d90 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e  hwtime.h contain
b4da0 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c  s inline assembl
b4db0 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c  er code for impl
b4dc0 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67  ementing .** hig
b4dd0 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69  h-performance ti
b4de0 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  ming routines..*
b4df0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
b4e00 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65  * Include hwtime
b4e10 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
b4e20 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   of os_common.h 
b4e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4e40 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
b4e50 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74  * Begin file hwt
b4e60 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
b4e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4e90 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79  /./*.** 2008 May
b4ea0 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   27.**.** The au
b4eb0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
b4ec0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
b4ed0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
b4ee0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
b4ef0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
b4f00 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
b4f10 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
b4f20 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
b4f30 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
b4f40 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
b4f50 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
b4f60 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
b4f70 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
b4f80 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
b4f90 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
b4fa0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
b4fb0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
b4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b4ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5000 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
b5010 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
b5020 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65   inline asm code
b5030 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20   for retrieving 
b5040 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63  "high-performanc
b5050 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66  e".** counters f
b5060 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55  or x86 class CPU
b5070 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77  s..**.** $Id: hw
b5080 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30  time.h,v 1.3 200
b5090 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35  8/08/01 14:33:15
b50a0 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
b50b0 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f  #ifndef _HWTIME_
b50c0 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49  H_.#define _HWTI
b50d0 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ME_H_../*.** The
b50e0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
b50f0 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ne only works on
b5100 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28   pentium-class (
b5110 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73  or newer) proces
b5120 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73  sors..** It uses
b5130 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64   the RDTSC opcod
b5140 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79  e to read the cy
b5150 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20  cle count value 
b5160 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72  out of the.** pr
b5170 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75  ocessor and retu
b5180 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20  rns that value. 
b5190 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
b51a0 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a  d for high-res.*
b51b0 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a  * profiling..*/.
b51c0 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  #if (defined(__G
b51d0 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  NUC__) || define
b51e0 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20  d(_MSC_VER)) && 
b51f0 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64  \.      (defined
b5200 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
b5210 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
b5220 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29  efined(_M_IX86))
b5230 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28  ..  #if defined(
b5240 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69  __GNUC__)..  __i
b5250 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
b5260 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
b5270 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
b5280 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c  unsigned int lo,
b5290 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f   hi;.     __asm_
b52a0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
b52b0 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28  "rdtsc" : "=a" (
b52c0 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b  lo), "=d" (hi));
b52d0 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71  .     return (sq
b52e0 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c  lite_uint64)hi <
b52f0 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a  < 32 | lo;.  }..
b5300 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28    #elif defined(
b5310 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64  _MSC_VER)..  __d
b5320 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f  eclspec(naked) _
b5330 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75  _inline sqlite_u
b5340 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71  int64 __cdecl sq
b5350 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
b5360 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a  ){.     __asm {.
b5370 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20          rdtsc.  
b5380 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20        ret       
b5390 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61  ; return value a
b53a0 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d  t EDX:EAX.     }
b53b0 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a  .  }..  #endif..
b53c0 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f  #elif (defined(_
b53d0 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69  _GNUC__) && defi
b53e0 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29  ned(__x86_64__))
b53f0 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
b5400 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
b5410 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
b5420 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
b5430 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20   long val;.     
b5440 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
b5450 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
b5460 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20   "=A" (val));.  
b5470 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
b5480 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66    }. .#elif (def
b5490 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
b54a0 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f  & defined(__ppc_
b54b0 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
b54c0 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
b54d0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
b54e0 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
b54f0 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
b5500 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69  tval;.      unsi
b5510 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a  gned long junk;.
b5520 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
b5530 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c  volatile__ ("\n\
b5540 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20  .          1:   
b5550 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c     mftbu   %1\n\
b5560 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
b5570 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e     mftb    %L0\n
b5580 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
b5590 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e      mftbu   %0\n
b55a0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
b55b0 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25      cmpw    %0,%
b55c0 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
b55d0 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31         bne     1
b55e0 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b".             
b55f0 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74       : "=r" (ret
b5600 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b  val), "=r" (junk
b5610 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ));.      return
b5620 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65   retval;.  }..#e
b5630 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65  lse..  #error Ne
b5640 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ed implementatio
b5650 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69  n of sqlite3Hwti
b5660 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
b5670 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20  atform...  /*.  
b5680 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69  ** To compile wi
b5690 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69  thout implementi
b56a0 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ng sqlite3Hwtime
b56b0 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
b56c0 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63  form,.  ** you c
b56d0 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62  an remove the ab
b56e0 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75  ove #error and u
b56f0 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  se the following
b5700 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74  .  ** stub funct
b5710 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c  ion.  You will l
b5720 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f  ose timing suppo
b5730 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a  rt for many.  **
b5740 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e   of the debuggin
b5750 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74  g and testing ut
b5760 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20  ilities, but it 
b5770 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c  should at.  ** l
b5780 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64  east compile and
b5790 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54   run..  */.SQLIT
b57a0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
b57b0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
b57c0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72  3Hwtime(void){ r
b57d0 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75  eturn ((sqlite_u
b57e0 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e  int64)0); }..#en
b57f0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
b5800 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f  defined(_HWTIME_
b5810 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
b5820 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
b5830 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
b5840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b5860 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
b5870 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
b5880 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
b5890 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f   off in os_commo
b58a0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
b58b0 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20  ******/..static 
b58c0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
b58d0 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71  start;.static sq
b58e0 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c  lite_uint64 g_el
b58f0 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54  apsed;.#define T
b5900 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20  IMER_START      
b5910 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33   g_start=sqlite3
b5920 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65  Hwtime().#define
b5930 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20   TIMER_END      
b5940 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c     g_elapsed=sql
b5950 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73  ite3Hwtime()-g_s
b5960 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d  tart.#define TIM
b5970 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67  ER_ELAPSED     g
b5980 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23  _elapsed.#else.#
b5990 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41  define TIMER_STA
b59a0 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  RT.#define TIMER
b59b0 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d  _END.#define TIM
b59c0 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28  ER_ELAPSED     (
b59d0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
b59e0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
b59f0 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  If we compile wi
b5a00 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45  th the SQLITE_TE
b5a10 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68  ST macro set, th
b5a20 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  en the following
b5a30 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64   block.** of cod
b5a40 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74  e will give us t
b5a50 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  he ability to si
b5a60 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f  mulate a disk I/
b5a70 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a  O error.  This.*
b5a80 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  * is used for te
b5a90 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65  sting the I/O re
b5aa0 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f  covery logic..*/
b5ab0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
b5ac0 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
b5ad0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
b5ae0 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20  ror_hit = 0;    
b5af0 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
b5b00 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45   number of I/O E
b5b10 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f  rrors */.SQLITE_
b5b20 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
b5b30 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
b5b40 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
b5b50 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65  Number of non-be
b5b60 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53  nign errors */.S
b5b70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
b5b80 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
b5b90 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20  ending = 0;     
b5ba0 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e     /* Count down
b5bb0 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72   to first I/O er
b5bc0 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ror */.SQLITE_AP
b5bd0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
b5be0 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d  _error_persist =
b5bf0 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
b5c00 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73  ue if I/O errors
b5c10 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49   persist */.SQLI
b5c20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
b5c30 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
b5c40 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gn = 0;         
b5c50 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72  /* True if error
b5c60 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a  s are benign */.
b5c70 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
b5c80 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
b5c90 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c  pending = 0;.SQL
b5ca0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
b5cb0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30  te3_diskfull = 0
b5cc0 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ;.#define Simula
b5cd0 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
b5ce0 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  X) sqlite3_io_er
b5cf0 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23  ror_benign=(X).#
b5d00 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
b5d10 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a  OError(CODE)  \.
b5d20 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69    if( (sqlite3_i
b5d30 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20  o_error_persist 
b5d40 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  && sqlite3_io_er
b5d50 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20  ror_hit) \.     
b5d60 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f    || sqlite3_io_
b5d70 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20  error_pending-- 
b5d80 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20  == 1 )  \.      
b5d90 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f          { local_
b5da0 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d  ioerr(); CODE; }
b5db0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63  .static void loc
b5dc0 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f  al_ioerr(){.  IO
b5dd0 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22  TRACE(("IOERR\n"
b5de0 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  ));.  sqlite3_io
b5df0 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20  _error_hit++;.  
b5e00 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f  if( !sqlite3_io_
b5e10 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73  error_benign ) s
b5e20 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
b5e30 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65  hardhit++;.}.#de
b5e40 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
b5e50 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29  kfullError(CODE)
b5e60 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65   \.   if( sqlite
b5e70 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
b5e80 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28  ng ){ \.     if(
b5e90 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
b5ea0 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29  l_pending == 1 )
b5eb0 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c  { \.       local
b5ec0 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20  _ioerr(); \.    
b5ed0 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66     sqlite3_diskf
b5ee0 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  ull = 1; \.     
b5ef0 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
b5f00 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20  or_hit = 1; \.  
b5f10 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20       CODE; \.   
b5f20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20    }else{ \.     
b5f30 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75    sqlite3_diskfu
b5f40 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a  ll_pending--; \.
b5f50 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65       } \.   }.#e
b5f60 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  lse.#define Simu
b5f70 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
b5f80 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  n(X).#define Sim
b5f90 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a  ulateIOError(A).
b5fa0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
b5fb0 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29  DiskfullError(A)
b5fc0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
b5fd0 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65  hen testing, kee
b5fe0 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65  p a count of the
b5ff0 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20   number of open 
b6000 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  files..*/.#ifdef
b6010 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
b6020 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
b6030 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
b6040 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  unt = 0;.#define
b6050 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20   OpenCounter(X) 
b6060 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
b6070 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65  le_count+=(X).#e
b6080 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e  lse.#define Open
b6090 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69  Counter(X).#endi
b60a0 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
b60b0 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e  fined(_OS_COMMON
b60c0 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _H_) */../******
b60d0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
b60e0 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
b60f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6110 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
b6120 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
b6130 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
b6140 74 20 6f 66 66 20 69 6e 20 6f 73 5f 6f 73 32 2e  t off in os_os2.
b6150 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
b6160 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
b6170 54 68 65 20 6f 73 32 46 69 6c 65 20 73 74 72 75  The os2File stru
b6180 63 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73  cture is subclas
b6190 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c  s of sqlite3_fil
b61a0 65 20 73 70 65 63 69 66 69 63 20 66 6f 72 20 74  e specific for t
b61b0 68 65 20 4f 53 2f 32 0a 2a 2a 20 70 72 6f 74 61  he OS/2.** prota
b61c0 62 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f  bility layer..*/
b61d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
b61e0 6f 73 32 46 69 6c 65 20 6f 73 32 46 69 6c 65 3b  os2File os2File;
b61f0 0a 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20  .struct os2File 
b6200 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  {.  const sqlite
b6210 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
b6220 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79  ethod;  /* Alway
b6230 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72  s the first entr
b6240 79 20 2a 2f 0a 20 20 48 46 49 4c 45 20 68 3b 20  y */.  HFILE h; 
b6250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b6260 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61   /* Handle for a
b6270 63 63 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c  ccessing the fil
b6280 65 20 2a 2f 0a 20 20 63 68 61 72 2a 20 70 61 74  e */.  char* pat
b6290 68 54 6f 44 65 6c 3b 20 20 20 20 20 20 20 20 20  hToDel;         
b62a0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65   /* Name of file
b62b0 20 74 6f 20 64 65 6c 65 74 65 20 6f 6e 20 63 6c   to delete on cl
b62c0 6f 73 65 2c 20 4e 55 4c 4c 20 69 66 20 6e 6f 74  ose, NULL if not
b62d0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
b62e0 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20  har locktype;   
b62f0 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b 20  /* Type of lock 
b6300 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f  currently held o
b6310 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 7d  n this file */.}
b6320 3b 0a 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 5f  ;..#define LOCK_
b6330 54 49 4d 45 4f 55 54 20 31 30 4c 20 2f 2a 20 74  TIMEOUT 10L /* t
b6340 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  he default locki
b6350 6e 67 20 74 69 6d 65 6f 75 74 20 2a 2f 0a 0a 2f  ng timeout */../
b6360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b63a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
b63b0 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20   The next group 
b63c0 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  of routines impl
b63d0 65 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65  ement the I/O me
b63e0 74 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a  thods specified.
b63f0 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ** by the sqlite
b6400 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
b6410 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ect..***********
b6420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b6460 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  ***/../*.** Clos
b6470 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  e a file..*/.sta
b6480 74 69 63 20 69 6e 74 20 6f 73 32 43 6c 6f 73 65  tic int os2Close
b6490 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ( sqlite3_file *
b64a0 69 64 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72  id ){.  APIRET r
b64b0 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
b64c0 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 3b 0a  os2File *pFile;.
b64d0 20 20 69 66 28 20 69 64 20 26 26 20 28 70 46 69    if( id && (pFi
b64e0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
b64f0 64 29 20 21 3d 20 30 20 29 7b 0a 20 20 20 20 4f  d) != 0 ){.    O
b6500 53 54 52 41 43 45 32 28 20 22 43 4c 4f 53 45 20  STRACE2( "CLOSE 
b6510 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20  %d\n", pFile->h 
b6520 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 43  );.    rc = DosC
b6530 6c 6f 73 65 28 20 70 46 69 6c 65 2d 3e 68 20 29  lose( pFile->h )
b6540 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ;.    pFile->loc
b6550 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b  ktype = NO_LOCK;
b6560 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  .    if( pFile->
b6570 70 61 74 68 54 6f 44 65 6c 20 21 3d 20 4e 55 4c  pathToDel != NUL
b6580 4c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  L ){.      rc = 
b6590 44 6f 73 46 6f 72 63 65 44 65 6c 65 74 65 28 20  DosForceDelete( 
b65a0 28 50 53 5a 29 70 46 69 6c 65 2d 3e 70 61 74 68  (PSZ)pFile->path
b65b0 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 66  ToDel );.      f
b65c0 72 65 65 28 20 70 46 69 6c 65 2d 3e 70 61 74 68  ree( pFile->path
b65d0 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 70  ToDel );.      p
b65e0 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20  File->pathToDel 
b65f0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20  = NULL;.    }.  
b6600 20 20 69 64 20 3d 20 30 3b 0a 20 20 20 20 4f 70    id = 0;.    Op
b6610 65 6e 43 6f 75 6e 74 65 72 28 20 2d 31 20 29 3b  enCounter( -1 );
b6620 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
b6630 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20  c == NO_ERROR ? 
b6640 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
b6650 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_IOERR;.}../*.
b6660 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f  ** Read data fro
b6670 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20  m a file into a 
b6680 62 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20  buffer.  Return 
b6690 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c  SQLITE_OK if all
b66a0 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 72  .** bytes were r
b66b0 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ead successfully
b66c0 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52   and SQLITE_IOER
b66d0 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f  R if anything go
b66e0 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a  es.** wrong..*/.
b66f0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 52 65  static int os2Re
b6700 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  ad(.  sqlite3_fi
b6710 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20  le *id,         
b6720 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f        /* File to
b6730 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20   read from */.  
b6740 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20  void *pBuf,     
b6750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b6760 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74  /* Write content
b6770 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65   into this buffe
b6780 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20  r */.  int amt, 
b6790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b67a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
b67b0 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
b67c0 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  d */.  sqlite3_i
b67d0 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20  nt64 offset     
b67e0 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20         /* Begin 
b67f0 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20  reading at this 
b6800 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 55  offset */.){.  U
b6810 4c 4f 4e 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f  LONG fileLocatio
b6820 6e 20 3d 20 30 4c 3b 0a 20 20 55 4c 4f 4e 47 20  n = 0L;.  ULONG 
b6830 67 6f 74 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  got;.  os2File *
b6840 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
b6850 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20  *)id;.  assert( 
b6860 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c  id!=0 );.  Simul
b6870 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
b6880 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
b6890 52 45 41 44 20 29 3b 0a 20 20 4f 53 54 52 41 43  READ );.  OSTRAC
b68a0 45 33 28 20 22 52 45 41 44 20 25 64 20 6c 6f 63  E3( "READ %d loc
b68b0 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  k=%d\n", pFile->
b68c0 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
b68d0 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53  pe );.  if( DosS
b68e0 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d  etFilePtr(pFile-
b68f0 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45  >h, offset, FILE
b6900 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63  _BEGIN, &fileLoc
b6910 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52  ation) != NO_ERR
b6920 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
b6930 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20   SQLITE_IOERR;. 
b6940 20 7d 0a 20 20 69 66 28 20 44 6f 73 52 65 61 64   }.  if( DosRead
b6950 28 20 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66  ( pFile->h, pBuf
b6960 2c 20 61 6d 74 2c 20 26 67 6f 74 20 29 20 21 3d  , amt, &got ) !=
b6970 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
b6980 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
b6990 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20  OERR_READ;.  }. 
b69a0 20 69 66 28 20 67 6f 74 20 3d 3d 20 28 55 4c 4f   if( got == (ULO
b69b0 4e 47 29 61 6d 74 20 29 0a 20 20 20 20 72 65 74  NG)amt ).    ret
b69c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
b69d0 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 55   else {.    /* U
b69e0 6e 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f  nread portions o
b69f0 66 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66  f the input buff
b6a00 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  er must be zero-
b6a10 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65  filled */.    me
b6a20 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42  mset(&((char*)pB
b6a30 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74  uf)[got], 0, amt
b6a40 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72  -got);.    retur
b6a50 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  n SQLITE_IOERR_S
b6a60 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d  HORT_READ;.  }.}
b6a70 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
b6a80 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72  ta from a buffer
b6a90 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52   into a file.  R
b6aa0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
b6ab0 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  on success.** or
b6ac0 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f   some other erro
b6ad0 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72  r code on failur
b6ae0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
b6af0 20 6f 73 32 57 72 69 74 65 28 0a 20 20 73 71 6c   os2Write(.  sql
b6b00 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20  ite3_file *id,  
b6b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b6b20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e  File to write in
b6b30 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  to */.  const vo
b6b40 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20  id *pBuf,       
b6b50 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
b6b60 79 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74  ytes to be writt
b6b70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c  en */.  int amt,
b6b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b6b90 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
b6ba0 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72  r of bytes to wr
b6bb0 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ite */.  sqlite3
b6bc0 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20  _int64 offset   
b6bd0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
b6be0 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65  et into the file
b6bf0 20 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e   to begin writin
b6c00 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f  g at */.){.  ULO
b6c10 4e 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20  NG fileLocation 
b6c20 3d 20 30 4c 3b 0a 20 20 41 50 49 52 45 54 20 72  = 0L;.  APIRET r
b6c30 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
b6c40 55 4c 4f 4e 47 20 77 72 6f 74 65 3b 0a 20 20 6f  ULONG wrote;.  o
b6c50 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
b6c60 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
b6c70 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
b6c80 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
b6c90 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
b6ca0 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 29 3b  E_IOERR_WRITE );
b6cb0 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  .  SimulateDiskf
b6cc0 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e  ullError( return
b6cd0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a   SQLITE_FULL );.
b6ce0 20 20 4f 53 54 52 41 43 45 33 28 20 22 57 52 49    OSTRACE3( "WRI
b6cf0 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  TE %d lock=%d\n"
b6d00 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
b6d10 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
b6d20 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50   if( DosSetFileP
b6d30 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66  tr(pFile->h, off
b6d40 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c  set, FILE_BEGIN,
b6d50 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20   &fileLocation) 
b6d60 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  != NO_ERROR ){. 
b6d70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
b6d80 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 61 73  _IOERR;.  }.  as
b6d90 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20  sert( amt>0 );. 
b6da0 20 77 68 69 6c 65 28 20 61 6d 74 20 3e 20 30 20   while( amt > 0 
b6db0 26 26 0a 20 20 20 20 20 20 20 20 20 28 20 72 63  &&.         ( rc
b6dc0 20 3d 20 44 6f 73 57 72 69 74 65 28 20 70 46 69   = DosWrite( pFi
b6dd0 6c 65 2d 3e 68 2c 20 28 50 56 4f 49 44 29 70 42  le->h, (PVOID)pB
b6de0 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 20  uf, amt, &wrote 
b6df0 29 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  ) ) == NO_ERROR 
b6e00 26 26 0a 20 20 20 20 20 20 20 20 20 77 72 6f 74  &&.         wrot
b6e10 65 20 3e 20 30 0a 20 20 29 7b 0a 20 20 20 20 61  e > 0.  ){.    a
b6e20 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20  mt -= wrote;.   
b6e30 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a   pBuf = &((char*
b6e40 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20  )pBuf)[wrote];. 
b6e50 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 20 72   }..  return ( r
b6e60 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c  c != NO_ERROR ||
b6e70 20 61 6d 74 20 3e 20 28 69 6e 74 29 77 72 6f 74   amt > (int)wrot
b6e80 65 20 29 20 3f 20 53 51 4c 49 54 45 5f 46 55 4c  e ) ? SQLITE_FUL
b6e90 4c 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  L : SQLITE_OK;.}
b6ea0 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65  ../*.** Truncate
b6eb0 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f   an open file to
b6ec0 20 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a   a specified siz
b6ed0 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  e.*/.static int 
b6ee0 6f 73 32 54 72 75 6e 63 61 74 65 28 20 73 71 6c  os2Truncate( sql
b6ef0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
b6f00 36 34 20 6e 42 79 74 65 20 29 7b 0a 20 20 41 50  64 nByte ){.  AP
b6f10 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52  IRET rc = NO_ERR
b6f20 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70  OR;.  os2File *p
b6f30 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
b6f40 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  )id;.  OSTRACE3(
b6f50 20 22 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c   "TRUNCATE %d %l
b6f60 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  ld\n", pFile->h,
b6f70 20 6e 42 79 74 65 20 29 3b 0a 20 20 53 69 6d 75   nByte );.  Simu
b6f80 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
b6f90 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
b6fa0 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72  _TRUNCATE );.  r
b6fb0 63 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 53 69  c = DosSetFileSi
b6fc0 7a 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42  ze( pFile->h, nB
b6fd0 79 74 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  yte );.  return 
b6fe0 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f  rc == NO_ERROR ?
b6ff0 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c   SQLITE_OK : SQL
b7000 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
b7010 54 45 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  TE;.}..#ifdef SQ
b7020 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LITE_TEST./*.** 
b7030 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72  Count the number
b7040 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e   of fullsyncs an
b7050 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20  d normal syncs. 
b7060 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
b7070 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79   test.** that sy
b7080 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63  ncs and fullsync
b7090 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61  s are occuring a
b70a0 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65  t the right time
b70b0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
b70c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e   int sqlite3_syn
b70d0 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c  c_count = 0;.SQL
b70e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
b70f0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
b7100 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
b7110 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
b7120 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20  all writes to a 
b7130 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20  particular file 
b7140 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f  are committed to
b7150 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63   disk..*/.static
b7160 20 69 6e 74 20 6f 73 32 53 79 6e 63 28 20 73 71   int os2Sync( sq
b7170 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
b7180 69 6e 74 20 66 6c 61 67 73 20 29 7b 0a 20 20 6f  int flags ){.  o
b7190 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
b71a0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
b71b0 4f 53 54 52 41 43 45 33 28 20 22 53 59 4e 43 20  OSTRACE3( "SYNC 
b71c0 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70  %d lock=%d\n", p
b71d0 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
b71e0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 23 69 66 64  locktype );.#ifd
b71f0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
b7200 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
b7210 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 29 7b 0a  ITE_SYNC_FULL){.
b7220 20 20 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c      sqlite3_full
b7230 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20  sync_count++;.  
b7240 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63  }.  sqlite3_sync
b7250 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
b7260 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70  .  /* If we comp
b7270 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51  iled with the SQ
b7280 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61  LITE_NO_SYNC fla
b7290 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20  g, then syncing 
b72a0 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a  is a.  ** no-op.
b72b0 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
b72c0 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55  TE_NO_SYNC.  UNU
b72d0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 46  SED_PARAMETER(pF
b72e0 69 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ile);.  return S
b72f0 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a  QLITE_OK;.#else.
b7300 20 20 72 65 74 75 72 6e 20 44 6f 73 52 65 73 65    return DosRese
b7310 74 42 75 66 66 65 72 28 20 70 46 69 6c 65 2d 3e  tBuffer( pFile->
b7320 68 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  h ) == NO_ERROR 
b7330 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51  ? SQLITE_OK : SQ
b7340 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 23 65 6e 64  LITE_IOERR;.#end
b7350 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65  if.}../*.** Dete
b7360 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e  rmine the curren
b7370 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65  t size of a file
b7380 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61   in bytes.*/.sta
b7390 74 69 63 20 69 6e 74 20 6f 73 32 46 69 6c 65 53  tic int os2FileS
b73a0 69 7a 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c  ize( sqlite3_fil
b73b0 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69  e *id, sqlite3_i
b73c0 6e 74 36 34 20 2a 70 53 69 7a 65 20 29 7b 0a 20  nt64 *pSize ){. 
b73d0 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
b73e0 45 52 52 4f 52 3b 0a 20 20 46 49 4c 45 53 54 41  ERROR;.  FILESTA
b73f0 54 55 53 33 20 66 73 74 73 33 46 69 6c 65 49 6e  TUS3 fsts3FileIn
b7400 66 6f 3b 0a 20 20 6d 65 6d 73 65 74 28 26 66 73  fo;.  memset(&fs
b7410 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 30 2c 20  ts3FileInfo, 0, 
b7420 73 69 7a 65 6f 66 28 66 73 74 73 33 46 69 6c 65  sizeof(fsts3File
b7430 49 6e 66 6f 29 29 3b 0a 20 20 61 73 73 65 72 74  Info));.  assert
b7440 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d  ( id!=0 );.  Sim
b7450 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
b7460 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
b7470 52 5f 46 53 54 41 54 20 29 3b 0a 20 20 72 63 20  R_FSTAT );.  rc 
b7480 3d 20 44 6f 73 51 75 65 72 79 46 69 6c 65 49 6e  = DosQueryFileIn
b7490 66 6f 28 20 28 28 6f 73 32 46 69 6c 65 2a 29 69  fo( ((os2File*)i
b74a0 64 29 2d 3e 68 2c 20 46 49 4c 5f 53 54 41 4e 44  d)->h, FIL_STAND
b74b0 41 52 44 2c 20 26 66 73 74 73 33 46 69 6c 65 49  ARD, &fsts3FileI
b74c0 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45  nfo, sizeof(FILE
b74d0 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20 69 66  STATUS3) );.  if
b74e0 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52  ( rc == NO_ERROR
b74f0 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d   ){.    *pSize =
b7500 20 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2e 63   fsts3FileInfo.c
b7510 62 46 69 6c 65 3b 0a 20 20 20 20 72 65 74 75 72  bFile;.    retur
b7520 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
b7530 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
b7540 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53   SQLITE_IOERR_FS
b7550 54 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  TAT;.  }.}../*.*
b7560 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64  * Acquire a read
b7570 65 72 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74  er lock..*/.stat
b7580 69 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c 6f  ic int getReadLo
b7590 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ck( os2File *pFi
b75a0 6c 65 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b  le ){.  FILELOCK
b75b0 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20    LockArea,.    
b75c0 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72          UnlockAr
b75d0 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73  ea;.  APIRET res
b75e0 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b  ;.  memset(&Lock
b75f0 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
b7600 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65  LockArea));.  me
b7610 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
b7620 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
b7630 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b  ckArea));.  Lock
b7640 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53  Area.lOffset = S
b7650 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 4c  HARED_FIRST;.  L
b7660 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
b7670 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20   SHARED_SIZE;.  
b7680 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
b7690 65 74 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63  et = 0L;.  Unloc
b76a0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
b76b0 4c 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65  L;.  res = DosSe
b76c0 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c  tFileLocks( pFil
b76d0 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65  e->h, &UnlockAre
b76e0 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f  a, &LockArea, LO
b76f0 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29  CK_TIMEOUT, 1L )
b7700 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 47  ;.  OSTRACE3( "G
b7710 45 54 52 45 41 44 4c 4f 43 4b 20 25 64 20 72 65  ETREADLOCK %d re
b7720 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
b7730 68 2c 20 72 65 73 20 29 3b 0a 20 20 72 65 74 75  h, res );.  retu
b7740 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn res;.}../*.**
b7750 20 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b   Undo a readlock
b7760 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
b7770 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 20 6f  nlockReadLock( o
b7780 73 32 46 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20  s2File *id ){.  
b7790 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72  FILELOCK  LockAr
b77a0 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ea,.            
b77b0 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50  UnlockArea;.  AP
b77c0 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73  IRET res;.  mems
b77d0 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c  et(&LockArea, 0,
b77e0 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61   sizeof(LockArea
b77f0 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e  ));.  memset(&Un
b7800 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  lockArea, 0, siz
b7810 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29  eof(UnlockArea))
b7820 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66  ;.  LockArea.lOf
b7830 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 4c 6f 63  fset = 0L;.  Loc
b7840 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
b7850 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  L;.  UnlockArea.
b7860 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44  lOffset = SHARED
b7870 5f 46 49 52 53 54 3b 0a 20 20 55 6e 6c 6f 63 6b  _FIRST;.  Unlock
b7880 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48  Area.lRange = SH
b7890 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 72 65 73  ARED_SIZE;.  res
b78a0 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
b78b0 6b 73 28 20 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f  ks( id->h, &Unlo
b78c0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
b78d0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
b78e0 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45   1L );.  OSTRACE
b78f0 33 28 20 22 55 4e 4c 4f 43 4b 2d 52 45 41 44 4c  3( "UNLOCK-READL
b7900 4f 43 4b 20 66 69 6c 65 20 68 61 6e 64 6c 65 3d  OCK file handle=
b7910 25 64 20 72 65 73 3d 25 64 3f 5c 6e 22 2c 20 69  %d res=%d?\n", i
b7920 64 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 72  d->h, res );.  r
b7930 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a  eturn res;.}../*
b7940 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c  .** Lock the fil
b7950 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20  e with the lock 
b7960 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72  specified by par
b7970 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20  ameter locktype 
b7980 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20  - one.** of the 
b7990 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
b79a0 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f       (1) SHARED_
b79b0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20  LOCK.**     (2) 
b79c0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a  RESERVED_LOCK.**
b79d0 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47       (3) PENDING
b79e0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29  _LOCK.**     (4)
b79f0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a   EXCLUSIVE_LOCK.
b7a00 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20  **.** Sometimes 
b7a10 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20  when requesting 
b7a20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20  one lock state, 
b7a30 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20  additional lock 
b7a40 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e  states.** are in
b7a50 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65  serted in betwee
b7a60 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20  n.  The locking 
b7a70 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e  might fail on on
b7a80 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a  e of the later.*
b7a90 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65  * transitions le
b7aa0 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73  aving the lock s
b7ab0 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66  tate different f
b7ac0 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72  rom what it star
b7ad0 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c  ted but.** still
b7ae0 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f   short of its go
b7af0 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  al.  The followi
b7b00 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74  ng chart shows t
b7b10 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72  he allowed.** tr
b7b20 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68  ansitions and th
b7b30 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72  e inserted inter
b7b40 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a  mediate states:.
b7b50 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45  **.**    UNLOCKE
b7b60 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20  D -> SHARED.**  
b7b70 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45    SHARED -> RESE
b7b80 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  RVED.**    SHARE
b7b90 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
b7ba0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
b7bb0 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50    RESERVED -> (P
b7bc0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
b7bd0 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49  SIVE.**    PENDI
b7be0 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  NG -> EXCLUSIVE.
b7bf0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
b7c00 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63  ne will only inc
b7c10 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54  rease a lock.  T
b7c20 68 65 20 6f 73 32 55 6e 6c 6f 63 6b 28 29 20 72  he os2Unlock() r
b7c30 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73  outine.** erases
b7c40 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e   all locks at on
b7c50 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75  ce and returns u
b7c60 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  s immediately to
b7c70 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30   locking level 0
b7c80 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70  ..** It is not p
b7c90 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72  ossible to lower
b7ca0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
b7cb0 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61  el one step at a
b7cc0 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d   time.  You.** m
b7cd0 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20  ust go straight 
b7ce0 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  to locking level
b7cf0 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   0..*/.static in
b7d00 74 20 6f 73 32 4c 6f 63 6b 28 20 73 71 6c 69 74  t os2Lock( sqlit
b7d10 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
b7d20 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 69   locktype ){.  i
b7d30 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
b7d40 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75  K;       /* Retu
b7d50 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62  rn code from sub
b7d60 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 41 50  routines */.  AP
b7d70 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52  IRET res = NO_ER
b7d80 52 4f 52 3b 20 20 20 20 2f 2a 20 52 65 73 75 6c  ROR;    /* Resul
b7d90 74 20 6f 66 20 61 6e 20 4f 53 2f 32 20 6c 6f 63  t of an OS/2 loc
b7da0 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  k call */.  int 
b7db0 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20  newLocktype;    
b7dc0 20 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d     /* Set pFile-
b7dd0 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69  >locktype to thi
b7de0 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65  s value before e
b7df0 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  xiting */.  int 
b7e00 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
b7e10 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65   0;/* True if we
b7e20 20 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44   acquired a PEND
b7e30 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69  ING lock this ti
b7e40 6d 65 20 2a 2f 0a 20 20 46 49 4c 45 4c 4f 43 4b  me */.  FILELOCK
b7e50 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20    LockArea,.    
b7e60 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72          UnlockAr
b7e70 65 61 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70  ea;.  os2File *p
b7e80 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
b7e90 29 69 64 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c  )id;.  memset(&L
b7ea0 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
b7eb0 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20  of(LockArea));. 
b7ec0 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41   memset(&UnlockA
b7ed0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55  rea, 0, sizeof(U
b7ee0 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61  nlockArea));.  a
b7ef0 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20  ssert( pFile!=0 
b7f00 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22  );.  OSTRACE4( "
b7f10 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 25  LOCK %d %d was %
b7f20 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
b7f30 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d  locktype, pFile-
b7f40 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 0a 20 20  >locktype );..  
b7f50 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
b7f60 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66  lready a lock of
b7f70 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f   this type or mo
b7f80 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f  re restrictive o
b7f90 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 73 32 46 69  n the.  ** os2Fi
b7fa0 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20  le, do nothing. 
b7fb0 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e  Don't use the en
b7fc0 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74  d_lock: exit pat
b7fd0 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74  h, as.  ** sqlit
b7fe0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
b7ff0 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c   hasn't been cal
b8000 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20  led yet..  */.  
b8010 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
b8020 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype>=locktype ){
b8030 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22  .    OSTRACE3( "
b8040 4c 4f 43 4b 20 25 64 20 25 64 20 6f 6b 20 28 61  LOCK %d %d ok (a
b8050 6c 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c  lready held)\n",
b8060 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
b8070 79 70 65 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ype );.    retur
b8080 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
b8090 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
b80a0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71   the locking seq
b80b0 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74  uence is correct
b80c0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
b80d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21  pFile->locktype!
b80e0 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b  =NO_LOCK || lock
b80f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
b8100 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  K );.  assert( l
b8110 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47  ocktype!=PENDING
b8120 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
b8130 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53  t( locktype!=RES
b8140 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46  ERVED_LOCK || pF
b8150 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
b8160 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20  HARED_LOCK );.. 
b8170 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e   /* Lock the PEN
b8180 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69  DING_LOCK byte i
b8190 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71  f we need to acq
b81a0 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c  uire a PENDING l
b81b0 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48  ock or.  ** a SH
b81c0 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77  ARED lock.  If w
b81d0 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20  e are acquiring 
b81e0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74  a SHARED lock, t
b81f0 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f  he acquisition o
b8200 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49  f.  ** the PENDI
b8210 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20  NG_LOCK byte is 
b8220 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a  temporary..  */.
b8230 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
b8240 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b  pFile->locktype;
b8250 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
b8260 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a  cktype==NO_LOCK.
b8270 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79        || (lockty
b8280 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
b8290 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK && pFile->loc
b82a0 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f  ktype==RESERVED_
b82b0 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 4c  LOCK).  ){.    L
b82c0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
b82d0 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
b82e0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
b82f0 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e  nge = 1L;.    Un
b8300 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
b8310 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
b8320 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
b8330 4c 3b 0a 0a 20 20 20 20 2f 2a 20 77 61 69 74 20  L;..    /* wait 
b8340 6c 6f 6e 67 65 72 20 74 68 61 6e 20 4c 4f 43 4b  longer than LOCK
b8350 5f 54 49 4d 45 4f 55 54 20 68 65 72 65 20 6e 6f  _TIMEOUT here no
b8360 74 20 74 6f 20 68 61 76 65 20 74 6f 20 74 72 79  t to have to try
b8370 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
b8380 2a 2f 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  */.    res = Dos
b8390 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
b83a0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
b83b0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
b83c0 31 30 30 4c 2c 20 30 4c 20 29 3b 0a 20 20 20 20  100L, 0L );.    
b83d0 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52  if( res == NO_ER
b83e0 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ROR ){.      got
b83f0 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 31 3b  PendingLock = 1;
b8400 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28  .      OSTRACE3(
b8410 20 22 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e   "LOCK %d pendin
b8420 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e 20 73  g lock boolean s
b8430 65 74 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20  et.  res=%d\n", 
b8440 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
b8450 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
b8460 20 41 63 71 75 69 72 65 20 61 20 73 68 61 72 65   Acquire a share
b8470 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  d lock.  */.  if
b8480 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
b8490 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d  ED_LOCK && res =
b84a0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
b84b0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
b84c0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f  >locktype==NO_LO
b84d0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20  CK );.    res = 
b84e0 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
b84f0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20  e);.    if( res 
b8500 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
b8510 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65       newLocktype
b8520 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a   = SHARED_LOCK;.
b8530 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43      }.    OSTRAC
b8540 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71  E3( "LOCK %d acq
b8550 75 69 72 65 20 73 68 61 72 65 64 20 6c 6f 63 6b  uire shared lock
b8560 2e 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69  . res=%d\n", pFi
b8570 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20  le->h, res );.  
b8580 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
b8590 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a  a RESERVED lock.
b85a0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
b85b0 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype==RESERVED_LO
b85c0 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f  CK && res == NO_
b85d0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73  ERROR ){.    ass
b85e0 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ert( pFile->lock
b85f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
b8600 4b 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  K );.    LockAre
b8610 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45  a.lOffset = RESE
b8620 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c  RVED_BYTE;.    L
b8630 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
b8640 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41   1L;.    UnlockA
b8650 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
b8660 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
b8670 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
b8680 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69    res = DosSetFi
b8690 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e  leLocks( pFile->
b86a0 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
b86b0 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
b86c0 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20  TIMEOUT, 0L );. 
b86d0 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f     if( res == NO
b86e0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20  _ERROR ){.      
b86f0 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45  newLocktype = RE
b8700 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20  SERVED_LOCK;.   
b8710 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28   }.    OSTRACE3(
b8720 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72   "LOCK %d acquir
b8730 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e  e reserved lock.
b8740 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
b8750 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d  e->h, res );.  }
b8760 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
b8770 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20   PENDING lock.  
b8780 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
b8790 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
b87a0 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45  K && res == NO_E
b87b0 52 52 4f 52 20 29 7b 0a 20 20 20 20 6e 65 77 4c  RROR ){.    newL
b87c0 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e  ocktype = PENDIN
b87d0 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50  G_LOCK;.    gotP
b87e0 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a  endingLock = 0;.
b87f0 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 4c      OSTRACE2( "L
b8800 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 70  OCK %d acquire p
b8810 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 70 65 6e  ending lock. pen
b8820 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61  ding lock boolea
b8830 6e 20 75 6e 73 65 74 2e 5c 6e 22 2c 20 70 46 69  n unset.\n", pFi
b8840 6c 65 2d 3e 68 20 29 3b 0a 20 20 7d 0a 0a 20 20  le->h );.  }..  
b8850 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58  /* Acquire an EX
b8860 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a  CLUSIVE lock.  *
b8870 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  /.  if( locktype
b8880 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
b8890 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52   && res == NO_ER
b88a0 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ROR ){.    asser
b88b0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
b88c0 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe>=SHARED_LOCK 
b88d0 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c  );.    res = unl
b88e0 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  ockReadLock(pFil
b88f0 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32  e);.    OSTRACE2
b8900 28 20 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20  ( "unreadlock = 
b8910 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a 20 20  %d\n", res );.  
b8920 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
b8930 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  et = SHARED_FIRS
b8940 54 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  T;.    LockArea.
b8950 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f  lRange = SHARED_
b8960 53 49 5a 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  SIZE;.    Unlock
b8970 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30  Area.lOffset = 0
b8980 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
b8990 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
b89a0 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46     res = DosSetF
b89b0 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d  ileLocks( pFile-
b89c0 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  >h, &UnlockArea,
b89d0 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b   &LockArea, LOCK
b89e0 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a  _TIMEOUT, 0L );.
b89f0 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e      if( res == N
b8a00 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
b8a10 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45   newLocktype = E
b8a20 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20  XCLUSIVE_LOCK;. 
b8a30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
b8a40 4f 53 54 52 41 43 45 32 28 20 22 4f 53 2f 32 20  OSTRACE2( "OS/2 
b8a50 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c  error-code = %d\
b8a60 6e 22 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 20  n", res );.     
b8a70 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69   getReadLock(pFi
b8a80 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f  le);.    }.    O
b8a90 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25  STRACE3( "LOCK %
b8aa0 64 20 61 63 71 75 69 72 65 20 65 78 63 6c 75 73  d acquire exclus
b8ab0 69 76 65 20 6c 6f 63 6b 2e 20 20 72 65 73 3d 25  ive lock.  res=%
b8ac0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
b8ad0 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  res );.  }..  /*
b8ae0 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69   If we are holdi
b8af0 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ng a PENDING loc
b8b00 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20  k that ought to 
b8b10 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65  be released, the
b8b20 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69  n.  ** release i
b8b30 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  t now..  */.  if
b8b40 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b  ( gotPendingLock
b8b50 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   && locktype==SH
b8b60 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
b8b70 20 69 6e 74 20 72 3b 0a 20 20 20 20 4c 6f 63 6b   int r;.    Lock
b8b80 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30  Area.lOffset = 0
b8b90 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  L;.    LockArea.
b8ba0 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20  lRange = 0L;.   
b8bb0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
b8bc0 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59  set = PENDING_BY
b8bd0 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  TE;.    UnlockAr
b8be0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a  ea.lRange = 1L;.
b8bf0 20 20 20 20 72 20 3d 20 44 6f 73 53 65 74 46 69      r = DosSetFi
b8c00 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e  leLocks( pFile->
b8c10 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
b8c20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
b8c30 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20  TIMEOUT, 0L );. 
b8c40 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f     OSTRACE3( "LO
b8c50 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 69 6e 67 20  CK %d unlocking 
b8c60 70 65 6e 64 69 6e 67 2f 69 73 20 73 68 61 72 65  pending/is share
b8c70 64 2e 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c  d. r=%d\n", pFil
b8c80 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 0a  e->h, r );.  }..
b8c90 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
b8ca0 73 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63  state of the loc
b8cb0 6b 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68  k has held in th
b8cc0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
b8cd0 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75  r then.  ** retu
b8ce0 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  rn the appropria
b8cf0 74 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a  te result code..
b8d00 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 3d    */.  if( res =
b8d10 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
b8d20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
b8d30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f  ;.  }else{.    O
b8d40 53 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 46  STRACE4( "LOCK F
b8d50 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20  AILED %d trying 
b8d60 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25  for %d but got %
b8d70 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
b8d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f                lo
b8d90 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74  cktype, newLockt
b8da0 79 70 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ype );.    rc = 
b8db0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
b8dc0 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  .  pFile->lockty
b8dd0 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79 70 65  pe = newLocktype
b8de0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c  ;.  OSTRACE3( "L
b8df0 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 5c 6e 22  OCK %d now %d\n"
b8e00 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
b8e10 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
b8e20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
b8e30 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
b8e40 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
b8e50 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
b8e60 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
b8e70 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
b8e80 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
b8e90 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
b8ea0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
b8eb0 69 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a  is held, return.
b8ec0 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68  ** non-zero, oth
b8ed0 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a  erwise zero..*/.
b8ee0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 43 68  static int os2Ch
b8ef0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
b8f00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
b8f10 64 2c 20 69 6e 74 20 2a 70 4f 75 74 20 29 7b 0a  d, int *pOut ){.
b8f20 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 6f    int r = 0;.  o
b8f30 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
b8f40 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
b8f50 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30  assert( pFile!=0
b8f60 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   );.  if( pFile-
b8f70 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52  >locktype>=RESER
b8f80 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  VED_LOCK ){.    
b8f90 72 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41  r = 1;.    OSTRA
b8fa0 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f  CE3( "TEST WR-LO
b8fb0 43 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29  CK %d %d (local)
b8fc0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
b8fd0 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
b8fe0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
b8ff0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
b9000 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20     UnlockArea;. 
b9010 20 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e     APIRET rc = N
b9020 4f 5f 45 52 52 4f 52 3b 0a 20 20 20 20 6d 65 6d  O_ERROR;.    mem
b9030 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30  set(&LockArea, 0
b9040 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65  , sizeof(LockAre
b9050 61 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  a));.    memset(
b9060 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  &UnlockArea, 0, 
b9070 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65  sizeof(UnlockAre
b9080 61 29 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  a));.    LockAre
b9090 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45  a.lOffset = RESE
b90a0 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c  RVED_BYTE;.    L
b90b0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
b90c0 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41   1L;.    UnlockA
b90d0 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
b90e0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
b90f0 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
b9100 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69 6c    rc = DosSetFil
b9110 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
b9120 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
b9130 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
b9140 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20  IMEOUT, 0L );.  
b9150 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53    OSTRACE3( "TES
b9160 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 6c 6f 63  T WR-LOCK %d loc
b9170 6b 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20  k reserved byte 
b9180 72 63 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  rc=%d\n", pFile-
b9190 3e 68 2c 20 72 63 20 29 3b 0a 20 20 20 20 69 66  >h, rc );.    if
b91a0 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52  ( rc == NO_ERROR
b91b0 20 29 7b 0a 20 20 20 20 20 20 41 50 49 52 45 54   ){.      APIRET
b91c0 20 72 63 75 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b   rcu = NO_ERROR;
b91d0 20 2f 2a 20 72 65 74 75 72 6e 20 63 6f 64 65 20   /* return code 
b91e0 66 6f 72 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f  for unlocking */
b91f0 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  .      LockArea.
b9200 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
b9210 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
b9220 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20  nge = 0L;.      
b9230 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
b9240 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  et = RESERVED_BY
b9250 54 45 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b  TE;.      Unlock
b9260 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c  Area.lRange = 1L
b9270 3b 0a 20 20 20 20 20 20 72 63 75 20 3d 20 44 6f  ;.      rcu = Do
b9280 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
b9290 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
b92a0 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
b92b0 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
b92c0 4c 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41  L );.      OSTRA
b92d0 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f  CE3( "TEST WR-LO
b92e0 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 20 72 65 73  CK %d unlock res
b92f0 65 72 76 65 64 20 62 79 74 65 20 72 3d 25 64 5c  erved byte r=%d\
b9300 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63  n", pFile->h, rc
b9310 75 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  u );.    }.    r
b9320 20 3d 20 21 28 72 63 20 3d 3d 20 4e 4f 5f 45 52   = !(rc == NO_ER
b9330 52 4f 52 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  ROR);.    OSTRAC
b9340 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43  E3( "TEST WR-LOC
b9350 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29  K %d %d (remote)
b9360 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
b9370 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20   );.  }.  *pOut 
b9380 3d 20 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  = r;.  return SQ
b9390 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
b93a0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  * Lower the lock
b93b0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c  ing level on fil
b93c0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20  e descriptor id 
b93d0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
b93e0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
b93f0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
b9400 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
b9410 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
b9420 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
b9430 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
b9440 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
b9450 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
b9460 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
b9470 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
b9480 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
b9490 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e  p..**.** It is n
b94a0 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20  ot possible for 
b94b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
b94c0 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f  fail if the seco
b94d0 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69  nd argument.** i
b94e0 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74  s NO_LOCK.  If t
b94f0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
b9500 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43  nt is SHARED_LOC
b9510 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  K then this rout
b9520 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  ine.** might ret
b9530 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
b9540 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.*/.static int 
b9550 6f 73 32 55 6e 6c 6f 63 6b 28 20 73 71 6c 69 74  os2Unlock( sqlit
b9560 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
b9570 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 69   locktype ){.  i
b9580 6e 74 20 74 79 70 65 3b 0a 20 20 6f 73 32 46 69  nt type;.  os2Fi
b9590 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32  le *pFile = (os2
b95a0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 41 50 49 52  File*)id;.  APIR
b95b0 45 54 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  ET rc = SQLITE_O
b95c0 4b 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 20  K;.  APIRET res 
b95d0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49  = NO_ERROR;.  FI
b95e0 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61  LELOCK  LockArea
b95f0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e  ,.            Un
b9600 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6d 65 6d 73  lockArea;.  mems
b9610 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c  et(&LockArea, 0,
b9620 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61   sizeof(LockArea
b9630 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e  ));.  memset(&Un
b9640 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  lockArea, 0, siz
b9650 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29  eof(UnlockArea))
b9660 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ;.  assert( pFil
b9670 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  e!=0 );.  assert
b9680 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
b9690 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54  ED_LOCK );.  OST
b96a0 52 41 43 45 34 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE4( "UNLOCK %
b96b0 64 20 74 6f 20 25 64 20 77 61 73 20 25 64 5c 6e  d to %d was %d\n
b96c0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
b96d0 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f  ktype, pFile->lo
b96e0 63 6b 74 79 70 65 20 29 3b 0a 20 20 74 79 70 65  cktype );.  type
b96f0 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79   = pFile->lockty
b9700 70 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d  pe;.  if( type>=
b9710 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
b9720 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  {.    LockArea.l
b9730 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20  Offset = 0L;.   
b9740 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65   LockArea.lRange
b9750 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
b9760 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
b9770 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20  SHARED_FIRST;.  
b9780 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
b9790 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a  nge = SHARED_SIZ
b97a0 45 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  E;.    res = Dos
b97b0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
b97c0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
b97d0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
b97e0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
b97f0 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   );.    OSTRACE3
b9800 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 65 78 63  ( "UNLOCK %d exc
b9810 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 65 73 3d  lusive lock res=
b9820 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
b9830 20 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20   res );.    if( 
b9840 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
b9850 5f 4c 4f 43 4b 20 26 26 20 67 65 74 52 65 61 64  _LOCK && getRead
b9860 4c 6f 63 6b 28 70 46 69 6c 65 29 20 21 3d 20 4e  Lock(pFile) != N
b9870 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
b9880 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20   /* This should 
b9890 6e 65 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57  never happen.  W
b98a0 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  e should always 
b98b0 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20  be able to.     
b98c0 20 2a 2a 20 72 65 61 63 71 75 69 72 65 20 74 68   ** reacquire th
b98d0 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20  e read lock */. 
b98e0 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22       OSTRACE3( "
b98f0 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20  UNLOCK %d to %d 
b9900 67 65 74 52 65 61 64 4c 6f 63 6b 28 29 20 66 61  getReadLock() fa
b9910 69 6c 65 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  iled\n", pFile->
b9920 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  h, locktype );. 
b9930 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
b9940 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20  _IOERR_UNLOCK;. 
b9950 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74     }.  }.  if( t
b9960 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype>=RESERVED_LO
b9970 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72  CK ){.    LockAr
b9980 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
b9990 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  .    LockArea.lR
b99a0 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55  ange = 0L;.    U
b99b0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
b99c0 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
b99d0 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  E;.    UnlockAre
b99e0 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20  a.lRange = 1L;. 
b99f0 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46     res = DosSetF
b9a00 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d  ileLocks( pFile-
b9a10 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  >h, &UnlockArea,
b9a20 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b   &LockArea, LOCK
b9a30 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a  _TIMEOUT, 0L );.
b9a40 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55      OSTRACE3( "U
b9a50 4e 4c 4f 43 4b 20 25 64 20 72 65 73 65 72 76 65  NLOCK %d reserve
b9a60 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69  d res=%d\n", pFi
b9a70 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20  le->h, res );.  
b9a80 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  }.  if( locktype
b9a90 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70  ==NO_LOCK && typ
b9aa0 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e>=SHARED_LOCK )
b9ab0 7b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f  {.    res = unlo
b9ac0 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65  ckReadLock(pFile
b9ad0 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 35 28  );.    OSTRACE5(
b9ae0 20 22 55 4e 4c 4f 43 4b 20 25 64 20 69 73 20 25   "UNLOCK %d is %
b9af0 64 20 77 61 6e 74 20 25 64 20 72 65 73 3d 25 64  d want %d res=%d
b9b00 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 74  \n", pFile->h, t
b9b10 79 70 65 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 72  ype, locktype, r
b9b20 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  es );.  }.  if( 
b9b30 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type>=PENDING_LO
b9b40 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72  CK ){.    LockAr
b9b50 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
b9b60 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52  .    LockArea.lR
b9b70 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55  ange = 0L;.    U
b9b80 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
b9b90 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  t = PENDING_BYTE
b9ba0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
b9bb0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
b9bc0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69    res = DosSetFi
b9bd0 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e  leLocks( pFile->
b9be0 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
b9bf0 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
b9c00 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20  TIMEOUT, 0L );. 
b9c10 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e     OSTRACE3( "UN
b9c20 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e 67 20  LOCK %d pending 
b9c30 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  res=%d\n", pFile
b9c40 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a  ->h, res );.  }.
b9c50 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
b9c60 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
b9c70 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43  OSTRACE3( "UNLOC
b9c80 4b 20 25 64 20 6e 6f 77 20 25 64 5c 6e 22 2c 20  K %d now %d\n", 
b9c90 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d  pFile->h, pFile-
b9ca0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 72  >locktype );.  r
b9cb0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
b9cc0 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71  ** Control and q
b9cd0 75 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e  uery of the open
b9ce0 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f   file handle..*/
b9cf0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46  .static int os2F
b9d00 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  ileControl(sqlit
b9d10 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
b9d20 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29   op, void *pArg)
b9d30 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  {.  switch( op )
b9d40 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
b9d50 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
b9d60 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74  E: {.      *(int
b9d70 2a 29 70 41 72 67 20 3d 20 28 28 6f 73 32 46 69  *)pArg = ((os2Fi
b9d80 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70  le*)id)->locktyp
b9d90 65 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  e;.      OSTRACE
b9da0 33 28 20 22 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54  3( "FCNTL_LOCKST
b9db0 41 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e  ATE %d lock=%d\n
b9dc0 22 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64  ", ((os2File*)id
b9dd0 29 2d 3e 68 2c 20 28 28 6f 73 32 46 69 6c 65 2a  )->h, ((os2File*
b9de0 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  )id)->locktype )
b9df0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
b9e00 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
b9e10 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
b9e20 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a  ITE_ERROR;.}../*
b9e30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
b9e40 65 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79  ector size in by
b9e50 74 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72  tes of the under
b9e60 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69  lying block devi
b9e70 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70  ce for.** the sp
b9e80 65 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68  ecified file. Th
b9e90 69 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77  is is almost alw
b9ea0 61 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62  ays 512 bytes, b
b9eb0 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72  ut may be.** lar
b9ec0 67 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76  ger for some dev
b9ed0 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  ices..**.** SQLi
b9ee0 74 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20  te code assumes 
b9ef0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61  this function ca
b9f00 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c  nnot fail. It al
b9f10 73 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a  so assumes that.
b9f20 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20  ** if two files 
b9f30 61 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74  are created in t
b9f40 68 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73  he same file-sys
b9f50 74 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69  tem directory (i
b9f60 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73  .e..** a databas
b9f70 65 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61  e and its journa
b9f80 6c 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65  l file) that the
b9f90 20 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c   sector size wil
b9fa0 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65  l be the.** same
b9fb0 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74   for both..*/.st
b9fc0 61 74 69 63 20 69 6e 74 20 6f 73 32 53 65 63 74  atic int os2Sect
b9fd0 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66  orSize(sqlite3_f
b9fe0 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75  ile *id){.  retu
b9ff0 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  rn SQLITE_DEFAUL
ba000 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d  T_SECTOR_SIZE;.}
ba010 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
ba020 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63   vector of devic
ba030 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
ba040 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
ba050 20 6f 73 32 44 65 76 69 63 65 43 68 61 72 61 63   os2DeviceCharac
ba060 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65  teristics(sqlite
ba070 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72  3_file *id){.  r
ba080 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn 0;.}.../*.
ba090 2a 2a 20 43 68 61 72 61 63 74 65 72 20 73 65 74  ** Character set
ba0a0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65   conversion obje
ba0b0 63 74 73 20 75 73 65 64 20 62 79 20 63 6f 6e 76  cts used by conv
ba0c0 65 72 73 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e  ersion routines.
ba0d0 0a 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76  .*/.static Uconv
ba0e0 4f 62 6a 65 63 74 20 75 63 55 74 66 38 20 3d 20  Object ucUtf8 = 
ba0f0 4e 55 4c 4c 3b 20 2f 2a 20 63 6f 6e 76 65 72 74  NULL; /* convert
ba100 20 62 65 74 77 65 65 6e 20 55 54 46 2d 38 20 61   between UTF-8 a
ba110 6e 64 20 55 43 53 2d 32 20 2a 2f 0a 73 74 61 74  nd UCS-2 */.stat
ba120 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75  ic UconvObject u
ba130 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a  clCp = NULL;  /*
ba140 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e   convert between
ba150 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20   local codepage 
ba160 61 6e 64 20 55 43 53 2d 32 20 2a 2f 0a 0a 2f 2a  and UCS-2 */../*
ba170 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  .** Helper funct
ba180 69 6f 6e 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  ion to initializ
ba190 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  e the conversion
ba1a0 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e   objects from an
ba1b0 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73  d to UTF-8..*/.s
ba1c0 74 61 74 69 63 20 76 6f 69 64 20 69 6e 69 74 55  tatic void initU
ba1d0 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69  convObjects( voi
ba1e0 64 20 29 7b 0a 20 20 69 66 28 20 55 6e 69 43 72  d ){.  if( UniCr
ba1f0 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28  eateUconvObject(
ba200 20 55 54 46 5f 38 2c 20 26 75 63 55 74 66 38 20   UTF_8, &ucUtf8 
ba210 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53  ) != ULS_SUCCESS
ba220 20 29 0a 20 20 20 20 75 63 55 74 66 38 20 3d 20   ).    ucUtf8 = 
ba230 4e 55 4c 4c 3b 0a 20 20 69 66 20 28 20 55 6e 69  NULL;.  if ( Uni
ba240 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65 63  CreateUconvObjec
ba250 74 28 20 28 55 6e 69 43 68 61 72 20 2a 29 4c 22  t( (UniChar *)L"
ba260 40 70 61 74 68 3d 79 65 73 22 2c 20 26 75 63 6c  @path=yes", &ucl
ba270 43 70 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43  Cp ) != ULS_SUCC
ba280 45 53 53 20 29 0a 20 20 20 20 75 63 6c 43 70 20  ESS ).    uclCp 
ba290 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  = NULL;.}../*.**
ba2a0 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
ba2b0 20 74 6f 20 66 72 65 65 20 74 68 65 20 63 6f 6e   to free the con
ba2c0 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20  version objects 
ba2d0 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d  from and to UTF-
ba2e0 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  8..*/.static voi
ba2f0 64 20 66 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63  d freeUconvObjec
ba300 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66  ts( void ){.  if
ba310 20 28 20 75 63 55 74 66 38 20 29 0a 20 20 20 20   ( ucUtf8 ).    
ba320 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65  UniFreeUconvObje
ba330 63 74 28 20 75 63 55 74 66 38 20 29 3b 0a 20 20  ct( ucUtf8 );.  
ba340 69 66 20 28 20 75 63 6c 43 70 20 29 0a 20 20 20  if ( uclCp ).   
ba350 20 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a   UniFreeUconvObj
ba360 65 63 74 28 20 75 63 6c 43 70 20 29 3b 0a 20 20  ect( uclCp );.  
ba370 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20  ucUtf8 = NULL;. 
ba380 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d   uclCp = NULL;.}
ba390 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66  ../*.** Helper f
ba3a0 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65  unction to conve
ba3b0 72 74 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d  rt UTF-8 filenam
ba3c0 65 73 20 74 6f 20 6c 6f 63 61 6c 20 4f 53 2f 32  es to local OS/2
ba3d0 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68   codepage..** Th
ba3e0 65 20 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65  e two-step proce
ba3f0 73 73 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72  ss: first conver
ba400 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 55  t the incoming U
ba410 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 69  TF-8 string.** i
ba420 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68  nto UCS-2 and th
ba430 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f  en from UCS-2 to
ba440 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 64   the current cod
ba450 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 72 65  epage..** The re
ba460 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e  turned char poin
ba470 74 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72  ter has to be fr
ba480 65 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  eed..*/.static c
ba490 68 61 72 20 2a 63 6f 6e 76 65 72 74 55 74 66 38  har *convertUtf8
ba4a0 50 61 74 68 54 6f 43 70 28 20 63 6f 6e 73 74 20  PathToCp( const 
ba4b0 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e  char *in ){.  Un
ba4c0 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43  iChar tempPath[C
ba4d0 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68  CHMAXPATH];.  ch
ba4e0 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20  ar *out = (char 
ba4f0 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58  *)calloc( CCHMAX
ba500 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66  PATH, 1 );..  if
ba510 28 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74  ( !out ).    ret
ba520 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28  urn NULL;..  if(
ba530 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c   !ucUtf8 || !ucl
ba540 43 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f  Cp ).    initUco
ba550 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20  nvObjects();..  
ba560 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 74 72  /* determine str
ba570 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6e 76  ing for the conv
ba580 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20  ersion of UTF-8 
ba590 77 68 69 63 68 20 69 73 20 43 50 31 32 30 38 20  which is CP1208 
ba5a0 2a 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54  */.  if( UniStrT
ba5b0 6f 55 63 73 28 20 75 63 55 74 66 38 2c 20 74 65  oUcs( ucUtf8, te
ba5c0 6d 70 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29  mpPath, (char *)
ba5d0 69 6e 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29  in, CCHMAXPATH )
ba5e0 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20   != ULS_SUCCESS 
ba5f0 29 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74  ).    return out
ba600 3b 20 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69  ; /* if conversi
ba610 6f 6e 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e  on fails, return
ba620 20 74 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e   the empty strin
ba630 67 20 2a 2f 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65  g */..  /* conve
ba640 72 73 69 6f 6e 20 66 6f 72 20 63 75 72 72 65 6e  rsion for curren
ba650 74 20 63 6f 64 65 70 61 67 65 20 77 68 69 63 68  t codepage which
ba660 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72   can be used for
ba670 20 70 61 74 68 73 20 2a 2f 0a 20 20 55 6e 69 53   paths */.  UniS
ba680 74 72 46 72 6f 6d 55 63 73 28 20 75 63 6c 43 70  trFromUcs( uclCp
ba690 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c  , out, tempPath,
ba6a0 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a   CCHMAXPATH );..
ba6b0 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a    return out;.}.
ba6c0 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75  ./*.** Helper fu
ba6d0 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72  nction to conver
ba6e0 74 20 66 69 6c 65 6e 61 6d 65 73 20 66 72 6f 6d  t filenames from
ba6f0 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20   local codepage 
ba700 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65  to UTF-8..** The
ba710 20 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65 73   two-step proces
ba720 73 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72 74  s: first convert
ba730 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f   the incoming co
ba740 64 65 70 61 67 65 2d 73 70 65 63 69 66 69 63 0a  depage-specific.
ba750 2a 2a 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 55  ** string into U
ba760 43 53 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72  CS-2 and then fr
ba770 6f 6d 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20  om UCS-2 to the 
ba780 63 6f 64 65 70 61 67 65 20 6f 66 20 55 54 46 2d  codepage of UTF-
ba790 38 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  8..** The return
ba7a0 65 64 20 63 68 61 72 20 70 6f 69 6e 74 65 72 20  ed char pointer 
ba7b0 68 61 73 20 74 6f 20 62 65 20 66 72 65 65 64 2e  has to be freed.
ba7c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
ba7d0 74 69 6f 6e 20 69 73 20 6e 6f 6e 2d 73 74 61 74  tion is non-stat
ba7e0 69 63 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f  ic to be able to
ba7f0 20 75 73 65 20 74 68 69 73 20 69 6e 20 73 68 65   use this in she
ba800 6c 6c 2e 63 20 61 6e 64 0a 2a 2a 20 73 69 6d 69  ll.c and.** simi
ba810 6c 61 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  lar applications
ba820 20 74 68 61 74 20 74 61 6b 65 20 63 6f 6d 6d 61   that take comma
ba830 6e 64 20 6c 69 6e 65 20 61 72 67 75 6d 65 6e 74  nd line argument
ba840 73 2e 0a 2a 2f 0a 63 68 61 72 20 2a 63 6f 6e 76  s..*/.char *conv
ba850 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28  ertCpPathToUtf8(
ba860 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20   const char *in 
ba870 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d  ){.  UniChar tem
ba880 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48  pPath[CCHMAXPATH
ba890 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d  ];.  char *out =
ba8a0 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28   (char *)calloc(
ba8b0 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29   CCHMAXPATH, 1 )
ba8c0 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a  ;..  if( !out ).
ba8d0 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b      return NULL;
ba8e0 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20  ..  if( !ucUtf8 
ba8f0 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20  || !uclCp ).    
ba900 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73  initUconvObjects
ba910 28 29 3b 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 72  ();..  /* conver
ba920 73 69 6f 6e 20 66 6f 72 20 63 75 72 72 65 6e 74  sion for current
ba930 20 63 6f 64 65 70 61 67 65 20 77 68 69 63 68 20   codepage which 
ba940 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20  can be used for 
ba950 70 61 74 68 73 20 2a 2f 0a 20 20 69 66 28 20 55  paths */.  if( U
ba960 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 6c 43  niStrToUcs( uclC
ba970 70 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68  p, tempPath, (ch
ba980 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50  ar *)in, CCHMAXP
ba990 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43  ATH ) != ULS_SUC
ba9a0 43 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72  CESS ).    retur
ba9b0 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e  n out; /* if con
ba9c0 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72  version fails, r
ba9d0 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20  eturn the empty 
ba9e0 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20  string */..  /* 
ba9f0 64 65 74 65 72 6d 69 6e 65 20 73 74 72 69 6e 67  determine string
baa00 20 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 72 73   for the convers
baa10 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77 68 69  ion of UTF-8 whi
baa20 63 68 20 69 73 20 43 50 31 32 30 38 20 2a 2f 0a  ch is CP1208 */.
baa30 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28    UniStrFromUcs(
baa40 20 75 63 55 74 66 38 2c 20 6f 75 74 2c 20 74 65   ucUtf8, out, te
baa50 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41  mpPath, CCHMAXPA
baa60 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  TH );..  return 
baa70 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  out;.}../*.** Th
baa80 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65  is vector define
baa90 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64  s all the method
baaa0 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61  s that can opera
baab0 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69  te on an.** sqli
baac0 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 6f 73 32  te3_file for os2
baad0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
baae0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
baaf0 68 6f 64 73 20 6f 73 32 49 6f 4d 65 74 68 6f 64  hods os2IoMethod
bab00 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20   = {.  1,       
bab10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bab20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
bab30 20 20 6f 73 32 43 6c 6f 73 65 2c 0a 20 20 6f 73    os2Close,.  os
bab40 32 52 65 61 64 2c 0a 20 20 6f 73 32 57 72 69 74  2Read,.  os2Writ
bab50 65 2c 0a 20 20 6f 73 32 54 72 75 6e 63 61 74 65  e,.  os2Truncate
bab60 2c 0a 20 20 6f 73 32 53 79 6e 63 2c 0a 20 20 6f  ,.  os2Sync,.  o
bab70 73 32 46 69 6c 65 53 69 7a 65 2c 0a 20 20 6f 73  s2FileSize,.  os
bab80 32 4c 6f 63 6b 2c 0a 20 20 6f 73 32 55 6e 6c 6f  2Lock,.  os2Unlo
bab90 63 6b 2c 0a 20 20 6f 73 32 43 68 65 63 6b 52 65  ck,.  os2CheckRe
baba0 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 6f 73  servedLock,.  os
babb0 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20  2FileControl,.  
babc0 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20  os2SectorSize,. 
babd0 20 6f 73 32 44 65 76 69 63 65 43 68 61 72 61 63   os2DeviceCharac
babe0 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a  teristics.};../*
babf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65  **********.** He
bac40 72 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20  re ends the I/O 
bac50 6d 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72  methods that for
bac60 6d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f  m the sqlite3_io
bac70 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e  _methods object.
bac80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  .**.** The next 
bac90 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d  block of code im
baca0 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53  plements the VFS
bacb0 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a   methods..******
bacc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bacd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bace0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bacf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bad00 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  ******/../*.** C
bad10 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72  reate a temporar
bad20 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a  y file name in z
bad30 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20  Buf.  zBuf must 
bad40 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  be big enough to
bad50 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73  .** hold at pVfs
bad60 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61  ->mxPathname cha
bad70 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74  racters..*/.stat
bad80 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61  ic int getTempna
bad90 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  me(int nBuf, cha
bada0 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20 73 74 61  r *zBuf ){.  sta
badb0 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
badc0 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d  ed char zChars[]
badd0 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68   =.    "abcdefgh
bade0 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78  ijklmnopqrstuvwx
badf0 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47  yz".    "ABCDEFG
bae00 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57  HIJKLMNOPQRSTUVW
bae10 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35  XYZ".    "012345
bae20 36 37 38 39 22 3b 0a 20 20 69 6e 74 20 69 2c 20  6789";.  int i, 
bae30 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50  j;.  char zTempP
bae40 61 74 68 42 75 66 5b 33 5d 3b 0a 20 20 50 53 5a  athBuf[3];.  PSZ
bae50 20 7a 54 65 6d 70 50 61 74 68 20 3d 20 28 50 53   zTempPath = (PS
bae60 5a 29 26 7a 54 65 6d 70 50 61 74 68 42 75 66 3b  Z)&zTempPathBuf;
bae70 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74  .  if( sqlite3_t
bae80 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b  emp_directory ){
bae90 0a 20 20 20 20 7a 54 65 6d 70 50 61 74 68 20 3d  .    zTempPath =
baea0 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
baeb0 72 65 63 74 6f 72 79 3b 0a 20 20 7d 65 6c 73 65  rectory;.  }else
baec0 7b 0a 20 20 20 20 69 66 28 20 44 6f 73 53 63 61  {.    if( DosSca
baed0 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 45 4d 50  nEnv( (PSZ)"TEMP
baee0 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20  ", &zTempPath ) 
baef0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 44 6f 73  ){.      if( Dos
baf00 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 54  ScanEnv( (PSZ)"T
baf10 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20  MP", &zTempPath 
baf20 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ) ){.        if(
baf30 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53   DosScanEnv( (PS
baf40 5a 29 22 54 4d 50 44 49 52 22 2c 20 26 7a 54 65  Z)"TMPDIR", &zTe
baf50 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20  mpPath ) ){.    
baf60 20 20 20 20 20 20 20 55 4c 4f 4e 47 20 75 6c 44         ULONG ulD
baf70 72 69 76 65 4e 75 6d 20 3d 20 30 2c 20 75 6c 44  riveNum = 0, ulD
baf80 72 69 76 65 4d 61 70 20 3d 20 30 3b 0a 20 20 20  riveMap = 0;.   
baf90 20 20 20 20 20 20 20 20 44 6f 73 51 75 65 72 79          DosQuery
bafa0 43 75 72 72 65 6e 74 44 69 73 6b 28 20 26 75 6c  CurrentDisk( &ul
bafb0 44 72 69 76 65 4e 75 6d 2c 20 26 75 6c 44 72 69  DriveNum, &ulDri
bafc0 76 65 4d 61 70 20 29 3b 0a 20 20 20 20 20 20 20  veMap );.       
bafd0 20 20 20 20 73 70 72 69 6e 74 66 28 20 28 63 68      sprintf( (ch
bafe0 61 72 2a 29 7a 54 65 6d 70 50 61 74 68 2c 20 22  ar*)zTempPath, "
baff0 25 63 3a 22 2c 20 28 63 68 61 72 29 28 20 27 41  %c:", (char)( 'A
bb000 27 20 2b 20 75 6c 44 72 69 76 65 4e 75 6d 20 2d  ' + ulDriveNum -
bb010 20 31 20 29 20 29 3b 0a 20 20 20 20 20 20 20 20   1 ) );.        
bb020 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
bb030 20 20 7d 0a 20 20 2f 2a 20 53 74 72 69 70 20 6f    }.  /* Strip o
bb040 66 66 20 61 20 74 72 61 69 6c 69 6e 67 20 73 6c  ff a trailing sl
bb050 61 73 68 65 73 20 6f 72 20 62 61 63 6b 73 6c 61  ashes or backsla
bb060 73 68 65 73 2c 20 6f 74 68 65 72 77 69 73 65 20  shes, otherwise 
bb070 77 65 20 77 6f 75 6c 64 20 67 65 74 20 2a 0a 20  we would get *. 
bb080 20 20 2a 20 6d 75 6c 74 69 70 6c 65 20 28 62 61    * multiple (ba
bb090 63 6b 29 73 6c 61 73 68 65 73 20 77 68 69 63 68  ck)slashes which
bb0a0 20 63 61 75 73 65 73 20 44 6f 73 4f 70 65 6e 28   causes DosOpen(
bb0b0 29 20 74 6f 20 66 61 69 6c 2e 20 20 20 20 20 20  ) to fail.      
bb0c0 20 20 20 20 20 20 20 20 2a 0a 20 20 20 2a 20 54          *.   * T
bb0d0 72 61 69 6c 69 6e 67 20 73 70 61 63 65 73 20 61  railing spaces a
bb0e0 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 2c 20  re not allowed, 
bb0f0 65 69 74 68 65 72 2e 20 20 20 20 20 20 20 20 20  either.         
bb100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb110 20 20 20 2a 2f 0a 20 20 6a 20 3d 20 73 71 6c 69     */.  j = sqli
bb120 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d  te3Strlen30(zTem
bb130 70 50 61 74 68 29 3b 0a 20 20 77 68 69 6c 65 28  pPath);.  while(
bb140 20 6a 20 3e 20 30 20 26 26 20 28 20 7a 54 65 6d   j > 0 && ( zTem
bb150 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c  pPath[j-1] == '\
bb160 5c 27 20 7c 7c 20 7a 54 65 6d 70 50 61 74 68 5b  \' || zTempPath[
bb170 6a 2d 31 5d 20 3d 3d 20 27 2f 27 0a 20 20 20 20  j-1] == '/'.    
bb180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb190 7c 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31  || zTempPath[j-1
bb1a0 5d 20 3d 3d 20 27 20 27 20 29 20 29 7b 0a 20 20  ] == ' ' ) ){.  
bb1b0 20 20 6a 2d 2d 3b 0a 20 20 7d 0a 20 20 7a 54 65    j--;.  }.  zTe
bb1c0 6d 70 50 61 74 68 5b 6a 5d 20 3d 20 27 5c 30 27  mpPath[j] = '\0'
bb1d0 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
bb1e0 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20  _temp_directory 
bb1f0 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65  ){.    char *zTe
bb200 6d 70 50 61 74 68 55 54 46 20 3d 20 63 6f 6e 76  mpPathUTF = conv
bb210 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28  ertCpPathToUtf8(
bb220 20 7a 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20   zTempPath );.  
bb230 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
bb240 74 66 28 20 6e 42 75 66 2d 33 30 2c 20 7a 42 75  tf( nBuf-30, zBu
bb250 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  f,.             
bb260 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53           "%s\\"S
bb270 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f  QLITE_TEMP_FILE_
bb280 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74  PREFIX, zTempPat
bb290 68 55 54 46 20 29 3b 0a 20 20 20 20 66 72 65 65  hUTF );.    free
bb2a0 28 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29  ( zTempPathUTF )
bb2b0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
bb2c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
bb2d0 20 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a   nBuf-30, zBuf,.
bb2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb2f0 20 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49        "%s\\"SQLI
bb300 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
bb310 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 20 29  FIX, zTempPath )
bb320 3b 0a 20 20 7d 0a 20 20 6a 20 3d 20 73 71 6c 69  ;.  }.  j = sqli
bb330 74 65 33 53 74 72 6c 65 6e 33 30 28 20 7a 42 75  te3Strlen30( zBu
bb340 66 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  f );.  sqlite3_r
bb350 61 6e 64 6f 6d 6e 65 73 73 28 20 32 30 2c 20 26  andomness( 20, &
bb360 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20 20 66 6f 72  zBuf[j] );.  for
bb370 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 32 30 3b  ( i = 0; i < 20;
bb380 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a 20 20 20   i++, j++ ){.   
bb390 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72   zBuf[j] = (char
bb3a0 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67  )zChars[ ((unsig
bb3b0 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d  ned char)zBuf[j]
bb3c0 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73  )%(sizeof(zChars
bb3d0 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42  )-1) ];.  }.  zB
bb3e0 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54  uf[j] = 0;.  OST
bb3f0 52 41 43 45 32 28 20 22 54 45 4d 50 20 46 49 4c  RACE2( "TEMP FIL
bb400 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42  ENAME: %s\n", zB
bb410 75 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  uf );.  return S
bb420 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
bb430 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74  .** Turn a relat
bb440 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74  ive pathname int
bb450 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d  o a full pathnam
bb460 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75  e.  Write the fu
bb470 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69  ll.** pathname i
bb480 6e 74 6f 20 7a 46 75 6c 6c 5b 5d 2e 20 20 7a 46  nto zFull[].  zF
bb490 75 6c 6c 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74  ull[] will be at
bb4a0 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50   least pVfs->mxP
bb4b0 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73  athname.** bytes
bb4c0 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61   in size..*/.sta
bb4d0 74 69 63 20 69 6e 74 20 6f 73 32 46 75 6c 6c 50  tic int os2FullP
bb4e0 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74  athname(.  sqlit
bb4f0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
bb500 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
bb510 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20  r to vfs object 
bb520 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
bb530 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20  *zRelative,     
bb540 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c   /* Possibly rel
bb550 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68  ative input path
bb560 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c   */.  int nFull,
bb570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb580 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74    /* Size of out
bb590 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 79  put buffer in by
bb5a0 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  tes */.  char *z
bb5b0 46 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  Full            
bb5c0 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62       /* Output b
bb5d0 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 63 68  uffer */.){.  ch
bb5e0 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 43 70 20  ar *zRelativeCp 
bb5f0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74  = convertUtf8Pat
bb600 68 54 6f 43 70 28 20 7a 52 65 6c 61 74 69 76 65  hToCp( zRelative
bb610 20 29 3b 0a 20 20 63 68 61 72 20 7a 46 75 6c 6c   );.  char zFull
bb620 43 70 5b 43 43 48 4d 41 58 50 41 54 48 5d 20 3d  Cp[CCHMAXPATH] =
bb630 20 22 5c 30 22 3b 0a 20 20 63 68 61 72 20 2a 7a   "\0";.  char *z
bb640 46 75 6c 6c 55 54 46 3b 0a 20 20 41 50 49 52 45  FullUTF;.  APIRE
bb650 54 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50  T rc = DosQueryP
bb660 61 74 68 49 6e 66 6f 28 20 7a 52 65 6c 61 74 69  athInfo( zRelati
bb670 76 65 43 70 2c 20 46 49 4c 5f 51 55 45 52 59 46  veCp, FIL_QUERYF
bb680 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75 6c 6c 43 70  ULLNAME, zFullCp
bb690 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
bb6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bb6b0 20 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a    CCHMAXPATH );.
bb6c0 20 20 66 72 65 65 28 20 7a 52 65 6c 61 74 69 76    free( zRelativ
bb6d0 65 43 70 20 29 3b 0a 20 20 7a 46 75 6c 6c 55 54  eCp );.  zFullUT
bb6e0 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74  F = convertCpPat
bb6f0 68 54 6f 55 74 66 38 28 20 7a 46 75 6c 6c 43 70  hToUtf8( zFullCp
bb700 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e   );.  sqlite3_sn
bb710 70 72 69 6e 74 66 28 20 6e 46 75 6c 6c 2c 20 7a  printf( nFull, z
bb720 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54 46 20 29  Full, zFullUTF )
bb730 3b 0a 20 20 66 72 65 65 28 20 7a 46 75 6c 6c 55  ;.  free( zFullU
bb740 54 46 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  TF );.  return r
bb750 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20  c == NO_ERROR ? 
bb760 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
bb770 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 0a 2f 2a  TE_IOERR;.}.../*
bb780 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e  .** Open a file.
bb790 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
bb7a0 73 32 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  s2Open(.  sqlite
bb7b0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
bb7c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75          /* Not u
bb7d0 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  sed */.  const c
bb7e0 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
bb7f0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
bb800 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  f the file */.  
bb810 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
bb820 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
bb830 20 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 74   Write the SQLit
bb840 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 65  e file handle he
bb850 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  re */.  int flag
bb860 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
bb870 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f        /* Open mo
bb880 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e  de flags */.  in
bb890 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20  t *pOutFlags    
bb8a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
bb8b0 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61  tatus return fla
bb8c0 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 46 49 4c 45  gs */.){.  HFILE
bb8d0 20 68 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 46 69   h;.  ULONG ulFi
bb8e0 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46 49  leAttribute = FI
bb8f0 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 55 4c 4f  LE_NORMAL;.  ULO
bb900 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 3d  NG ulOpenFlags =
bb910 20 30 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70   0;.  ULONG ulOp
bb920 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 6f 73  enMode = 0;.  os
bb930 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
bb940 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 41  os2File*)id;.  A
bb950 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
bb960 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 41  ROR;.  ULONG ulA
bb970 63 74 69 6f 6e 3b 0a 20 20 63 68 61 72 20 2a 7a  ction;.  char *z
bb980 4e 61 6d 65 43 70 3b 0a 20 20 63 68 61 72 20 7a  NameCp;.  char z
bb990 54 6d 70 6e 61 6d 65 5b 43 43 48 4d 41 58 50 41  Tmpname[CCHMAXPA
bb9a0 54 48 2b 31 5d 3b 20 20 20 20 2f 2a 20 42 75 66  TH+1];    /* Buf
bb9b0 66 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65  fer to hold name
bb9c0 20 6f 66 20 74 65 6d 70 20 66 69 6c 65 20 2a 2f   of temp file */
bb9d0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65  ..  /* If the se
bb9e0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
bb9f0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
bba00 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65  s NULL, generate
bba10 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61   a .  ** tempora
bba20 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20  ry file name to 
bba30 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  use .  */.  if( 
bba40 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e  !zName ){.    in
bba50 74 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61  t rc = getTempna
bba60 6d 65 28 43 43 48 4d 41 58 50 41 54 48 2b 31 2c  me(CCHMAXPATH+1,
bba70 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20   zTmpname);.    
bba80 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
bba90 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
bbaa0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
bbab0 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65  zName = zTmpname
bbac0 3b 0a 20 20 7d 0a 0a 0a 20 20 6d 65 6d 73 65 74  ;.  }...  memset
bbad0 28 20 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65  ( pFile, 0, size
bbae0 6f 66 28 2a 70 46 69 6c 65 29 20 29 3b 0a 0a 20  of(*pFile) );.. 
bbaf0 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e   OSTRACE2( "OPEN
bbb00 20 77 61 6e 74 20 25 64 5c 6e 22 2c 20 66 6c 61   want %d\n", fla
bbb10 67 73 20 29 3b 0a 0a 20 20 69 66 28 20 66 6c 61  gs );..  if( fla
bbb20 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
bbb30 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20  _READWRITE ){.  
bbb40 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20    ulOpenMode |= 
bbb50 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44  OPEN_ACCESS_READ
bbb60 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41  WRITE;.    OSTRA
bbb70 43 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 2f  CE1( "OPEN read/
bbb80 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65  write\n" );.  }e
bbb90 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d  lse{.    ulOpenM
bbba0 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45  ode |= OPEN_ACCE
bbbb0 53 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20  SS_READONLY;.   
bbbc0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
bbbd0 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b   read only\n" );
bbbe0 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67  .  }..  if( flag
bbbf0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
bbc00 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 75 6c  CREATE ){.    ul
bbc10 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45  OpenFlags |= OPE
bbc20 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46  N_ACTION_OPEN_IF
bbc30 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41  _EXISTS | OPEN_A
bbc40 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49 46 5f  CTION_CREATE_IF_
bbc50 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  NEW;.    OSTRACE
bbc60 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 6e 65  1( "OPEN open ne
bbc70 77 2f 63 72 65 61 74 65 5c 6e 22 20 29 3b 0a 20  w/create\n" );. 
bbc80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70   }else{.    ulOp
bbc90 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e 5f  enFlags |= OPEN_
bbca0 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45  ACTION_OPEN_IF_E
bbcb0 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43 54  XISTS | OPEN_ACT
bbcc0 49 4f 4e 5f 46 41 49 4c 5f 49 46 5f 4e 45 57 3b  ION_FAIL_IF_NEW;
bbcd0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
bbce0 4f 50 45 4e 20 6f 70 65 6e 20 65 78 69 73 74 69  OPEN open existi
bbcf0 6e 67 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20  ng\n" );.  }..  
bbd00 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
bbd10 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
bbd20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64  ){.    ulOpenMod
bbd30 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f  e |= OPEN_SHARE_
bbd40 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20 20 4f 53  DENYNONE;.    OS
bbd50 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 68  TRACE1( "OPEN sh
bbd60 61 72 65 20 72 65 61 64 2f 77 72 69 74 65 5c 6e  are read/write\n
bbd70 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  " );.  }else{.  
bbd80 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20    ulOpenMode |= 
bbd90 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 57  OPEN_SHARE_DENYW
bbda0 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41 43  RITE;.    OSTRAC
bbdb0 45 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65 20  E1( "OPEN share 
bbdc0 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a  read only\n" );.
bbdd0 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73    }..  if( flags
bbde0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44   & SQLITE_OPEN_D
bbdf0 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a  ELETEONCLOSE ){.
bbe00 20 20 20 20 63 68 61 72 20 70 61 74 68 55 74 66      char pathUtf
bbe10 38 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 23  8[CCHMAXPATH];.#
bbe20 69 66 64 65 66 20 4e 44 45 42 55 47 20 2f 2a 20  ifdef NDEBUG /* 
bbe30 77 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20 77  when debugging w
bbe40 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 73  e want to make s
bbe50 75 72 65 20 69 74 20 69 73 20 64 65 6c 65 74 65  ure it is delete
bbe60 64 20 2a 2f 0a 20 20 20 20 75 6c 46 69 6c 65 41  d */.    ulFileA
bbe70 74 74 72 69 62 75 74 65 20 3d 20 46 49 4c 45 5f  ttribute = FILE_
bbe80 48 49 44 44 45 4e 3b 0a 23 65 6e 64 69 66 0a 20  HIDDEN;.#endif. 
bbe90 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61     os2FullPathna
bbea0 6d 65 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c  me( pVfs, zName,
bbeb0 20 43 43 48 4d 41 58 50 41 54 48 2c 20 70 61 74   CCHMAXPATH, pat
bbec0 68 55 74 66 38 20 29 3b 0a 20 20 20 20 70 46 69  hUtf8 );.    pFi
bbed0 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20  le->pathToDel = 
bbee0 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54  convertUtf8PathT
bbef0 6f 43 70 28 20 70 61 74 68 55 74 66 38 20 29 3b  oCp( pathUtf8 );
bbf00 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
bbf10 4f 50 45 4e 20 68 69 64 64 65 6e 2f 64 65 6c 65  OPEN hidden/dele
bbf20 74 65 20 6f 6e 20 63 6c 6f 73 65 20 66 69 6c 65  te on close file
bbf30 20 61 74 74 72 69 62 75 74 65 73 5c 6e 22 20 29   attributes\n" )
bbf40 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
bbf50 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20  File->pathToDel 
bbf60 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 4f 53 54 52  = NULL;.    OSTR
bbf70 41 43 45 31 28 20 22 4f 50 45 4e 20 6e 6f 72 6d  ACE1( "OPEN norm
bbf80 61 6c 20 66 69 6c 65 20 61 74 74 72 69 62 75 74  al file attribut
bbf90 65 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  e\n" );.  }..  /
bbfa0 2a 20 61 6c 77 61 79 73 20 6f 70 65 6e 20 69 6e  * always open in
bbfb0 20 72 61 6e 64 6f 6d 20 61 63 63 65 73 73 20 6d   random access m
bbfc0 6f 64 65 20 66 6f 72 20 70 6f 73 73 69 62 6c 79  ode for possibly
bbfd0 20 62 65 74 74 65 72 20 73 70 65 65 64 20 2a 2f   better speed */
bbfe0 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d  .  ulOpenMode |=
bbff0 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41 4e 44   OPEN_FLAGS_RAND
bc000 4f 4d 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65  OM;.  ulOpenMode
bc010 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 46   |= OPEN_FLAGS_F
bc020 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b 0a 20 20  AIL_ON_ERROR;.  
bc030 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50  ulOpenMode |= OP
bc040 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e 48 45 52  EN_FLAGS_NOINHER
bc050 49 54 3b 0a 0a 20 20 7a 4e 61 6d 65 43 70 20 3d  IT;..  zNameCp =
bc060 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68   convertUtf8Path
bc070 54 6f 43 70 28 20 7a 4e 61 6d 65 20 29 3b 0a 20  ToCp( zName );. 
bc080 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e 28 20 28   rc = DosOpen( (
bc090 50 53 5a 29 7a 4e 61 6d 65 43 70 2c 0a 20 20 20  PSZ)zNameCp,.   
bc0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 26 68 2c               &h,
bc0b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
bc0c0 20 26 75 6c 41 63 74 69 6f 6e 2c 0a 20 20 20 20   &ulAction,.    
bc0d0 20 20 20 20 20 20 20 20 20 20 20 20 30 4c 2c 0a              0L,.
bc0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc0f0 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c  ulFileAttribute,
bc100 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
bc110 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 0a 20 20   ulOpenFlags,.  
bc120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6c                ul
bc130 4f 70 65 6e 4d 6f 64 65 2c 0a 20 20 20 20 20 20  OpenMode,.      
bc140 20 20 20 20 20 20 20 20 20 20 28 50 45 41 4f 50            (PEAOP
bc150 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 66 72 65 65  2)NULL );.  free
bc160 28 20 7a 4e 61 6d 65 43 70 20 29 3b 0a 20 20 69  ( zNameCp );.  i
bc170 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f  f( rc != NO_ERRO
bc180 52 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45  R ){.    OSTRACE
bc190 37 28 20 22 4f 50 45 4e 20 49 6e 76 61 6c 69 64  7( "OPEN Invalid
bc1a0 20 68 61 6e 64 6c 65 20 72 63 3d 25 64 3a 20 7a   handle rc=%d: z
bc1b0 4e 61 6d 65 3d 25 73 2c 20 75 6c 41 63 74 69 6f  Name=%s, ulActio
bc1c0 6e 3d 25 23 6c 78 2c 20 75 6c 41 74 74 72 3d 25  n=%#lx, ulAttr=%
bc1d0 23 6c 78 2c 20 75 6c 46 6c 61 67 73 3d 25 23 6c  #lx, ulFlags=%#l
bc1e0 78 2c 20 75 6c 4d 6f 64 65 3d 25 23 6c 78 5c 6e  x, ulMode=%#lx\n
bc1f0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
bc200 20 72 63 2c 20 7a 4e 61 6d 65 2c 20 75 6c 41 63   rc, zName, ulAc
bc210 74 69 6f 6e 2c 20 75 6c 46 69 6c 65 41 74 74 72  tion, ulFileAttr
bc220 69 62 75 74 65 2c 20 75 6c 4f 70 65 6e 46 6c 61  ibute, ulOpenFla
bc230 67 73 2c 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 29  gs, ulOpenMode )
bc240 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d  ;.    if( pFile-
bc250 3e 70 61 74 68 54 6f 44 65 6c 20 29 0a 20 20 20  >pathToDel ).   
bc260 20 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d 3e     free( pFile->
bc270 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20  pathToDel );.   
bc280 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
bc290 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 66  l = NULL;.    if
bc2a0 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
bc2b0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
bc2c0 29 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  ){.      OSTRACE
bc2d0 32 28 20 22 4f 50 45 4e 20 25 64 20 49 6e 76 61  2( "OPEN %d Inva
bc2e0 6c 69 64 20 68 61 6e 64 6c 65 5c 6e 22 2c 20 28  lid handle\n", (
bc2f0 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f  (flags | SQLITE_
bc300 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26  OPEN_READONLY) &
bc310 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   ~SQLITE_OPEN_RE
bc320 41 44 57 52 49 54 45 29 20 29 3b 0a 20 20 20 20  ADWRITE) );.    
bc330 20 20 72 65 74 75 72 6e 20 6f 73 32 4f 70 65 6e    return os2Open
bc340 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 69  ( pVfs, zName, i
bc350 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
bc360 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 73           ((flags
bc370 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   | SQLITE_OPEN_R
bc380 45 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49  EADONLY) & ~SQLI
bc390 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
bc3a0 45 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  E),.            
bc3b0 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 46 6c            pOutFl
bc3c0 61 67 73 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ags );.    }else
bc3d0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
bc3e0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
bc3f0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
bc400 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20   pOutFlags ){.  
bc410 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66    *pOutFlags = f
bc420 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
bc430 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3f 20 53  EN_READWRITE ? S
bc440 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
bc450 52 49 54 45 20 3a 20 53 51 4c 49 54 45 5f 4f 50  RITE : SQLITE_OP
bc460 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d  EN_READONLY;.  }
bc470 0a 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68  ..  pFile->pMeth
bc480 6f 64 20 3d 20 26 6f 73 32 49 6f 4d 65 74 68 6f  od = &os2IoMetho
bc490 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20  d;.  pFile->h = 
bc4a0 68 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72  h;.  OpenCounter
bc4b0 28 2b 31 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  (+1);.  OSTRACE3
bc4c0 28 20 22 4f 50 45 4e 20 25 64 20 70 4f 75 74 46  ( "OPEN %d pOutF
bc4d0 6c 61 67 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c  lags=%d\n", pFil
bc4e0 65 2d 3e 68 2c 20 70 4f 75 74 46 6c 61 67 73 20  e->h, pOutFlags 
bc4f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
bc500 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
bc510 44 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 64  Delete the named
bc520 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
bc530 20 69 6e 74 20 6f 73 32 44 65 6c 65 74 65 28 0a   int os2Delete(.
bc540 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
bc550 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  Vfs,            
bc560 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20           /* Not 
bc570 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20  used on os2 */. 
bc580 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
bc590 6c 65 6e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  lename,         
bc5a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
bc5b0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74  of file to delet
bc5c0 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44  e */.  int syncD
bc5d0 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ir              
bc5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bc5f0 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32   Not used on os2
bc600 20 2a 2f 0a 29 7b 0a 20 20 41 50 49 52 45 54 20   */.){.  APIRET 
bc610 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20  rc = NO_ERROR;. 
bc620 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
bc630 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  Cp = convertUtf8
bc640 50 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e  PathToCp( zFilen
bc650 61 6d 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  ame );.  Simulat
bc660 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
bc670 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45   SQLITE_IOERR_DE
bc680 4c 45 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44  LETE );.  rc = D
bc690 6f 73 44 65 6c 65 74 65 28 20 28 50 53 5a 29 7a  osDelete( (PSZ)z
bc6a0 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20  FilenameCp );.  
bc6b0 66 72 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43  free( zFilenameC
bc6c0 70 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28  p );.  OSTRACE2(
bc6d0 20 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c   "DELETE \"%s\"\
bc6e0 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b  n", zFilename );
bc6f0 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20  .  return rc == 
bc700 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54  NO_ERROR ? SQLIT
bc710 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f  E_OK : SQLITE_IO
bc720 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f  ERR_DELETE;.}../
bc730 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65  *.** Check the e
bc740 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61  xistance and sta
bc750 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a  tus of a file..*
bc760 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
bc770 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65  Access(.  sqlite
bc780 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
bc790 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20      /* Not used 
bc7a0 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73  on os2 */.  cons
bc7b0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
bc7c0 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
bc7d0 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a   file to check *
bc7e0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
bc7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bc800 20 54 79 70 65 20 6f 66 20 74 65 73 74 20 74 6f   Type of test to
bc810 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66 69   make on this fi
bc820 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75  le */.  int *pOu
bc830 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
bc840 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 6c    /* Write resul
bc850 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ts here */.){.  
bc860 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 73  FILESTATUS3 fsts
bc870 33 43 6f 6e 66 69 67 49 6e 66 6f 3b 0a 20 20 41  3ConfigInfo;.  A
bc880 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
bc890 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69  ROR;.  char *zFi
bc8a0 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65  lenameCp = conve
bc8b0 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20  rtUtf8PathToCp( 
bc8c0 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 0a 20 20  zFilename );..  
bc8d0 6d 65 6d 73 65 74 28 20 26 66 73 74 73 33 43 6f  memset( &fsts3Co
bc8e0 6e 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a  nfigInfo, 0, siz
bc8f0 65 6f 66 28 66 73 74 73 33 43 6f 6e 66 69 67 49  eof(fsts3ConfigI
bc900 6e 66 6f 29 20 29 3b 0a 20 20 72 63 20 3d 20 44  nfo) );.  rc = D
bc910 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28  osQueryPathInfo(
bc920 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43   (PSZ)zFilenameC
bc930 70 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c  p, FIL_STANDARD,
bc940 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
bc950 20 20 20 20 20 20 20 20 20 20 26 66 73 74 73 33            &fsts3
bc960 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a 65  ConfigInfo, size
bc970 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20  of(FILESTATUS3) 
bc980 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65  );.  free( zFile
bc990 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52  nameCp );.  OSTR
bc9a0 41 43 45 34 28 20 22 41 43 43 45 53 53 20 66 73  ACE4( "ACCESS fs
bc9b0 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74  ts3ConfigInfo.at
bc9c0 74 72 46 69 6c 65 3d 25 64 20 66 6c 61 67 73 3d  trFile=%d flags=
bc9d0 25 64 20 72 63 3d 25 64 5c 6e 22 2c 0a 20 20 20  %d rc=%d\n",.   
bc9e0 20 20 20 20 20 20 20 20 20 66 73 74 73 33 43 6f           fsts3Co
bc9f0 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c  nfigInfo.attrFil
bca00 65 2c 20 66 6c 61 67 73 2c 20 72 63 20 29 3b 0a  e, flags, rc );.
bca10 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20    switch( flags 
bca20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
bca30 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a  TE_ACCESS_READ:.
bca40 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
bca50 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20  ACCESS_EXISTS:. 
bca60 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d 3d       rc = (rc ==
bca70 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20   NO_ERROR);.    
bca80 20 20 4f 53 54 52 41 43 45 33 28 20 22 41 43 43    OSTRACE3( "ACC
bca90 45 53 53 20 25 73 20 61 63 63 65 73 73 20 6f 66  ESS %s access of
bcaa0 20 72 65 61 64 20 61 6e 64 20 65 78 69 73 74 73   read and exists
bcab0 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c    rc=%d\n", zFil
bcac0 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20  ename, rc );.   
bcad0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
bcae0 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
bcaf0 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20  _READWRITE:.    
bcb00 20 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 4e 4f    rc = (rc == NO
bcb10 5f 45 52 52 4f 52 29 20 26 26 20 28 20 28 66 73  _ERROR) && ( (fs
bcb20 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74  ts3ConfigInfo.at
bcb30 74 72 46 69 6c 65 20 26 20 46 49 4c 45 5f 52 45  trFile & FILE_RE
bcb40 41 44 4f 4e 4c 59 29 20 3d 3d 20 30 20 29 3b 0a  ADONLY) == 0 );.
bcb50 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20        OSTRACE3( 
bcb60 22 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73  "ACCESS %s acces
bcb70 73 20 6f 66 20 72 65 61 64 2f 77 72 69 74 65 20  s of read/write 
bcb80 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65   rc=%d\n", zFile
bcb90 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 20  name, rc );.    
bcba0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66    break;.    def
bcbb0 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65  ault:.      asse
bcbc0 72 74 28 20 21 22 49 6e 76 61 6c 69 64 20 66 6c  rt( !"Invalid fl
bcbd0 61 67 73 20 61 72 67 75 6d 65 6e 74 22 20 29 3b  ags argument" );
bcbe0 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d 20 72  .  }.  *pOut = r
bcbf0 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  c;.  return SQLI
bcc00 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64  TE_OK;.}...#ifnd
bcc10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
bcc20 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a  OAD_EXTENSION./*
bcc30 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66  .** Interfaces f
bcc40 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61  or opening a sha
bcc50 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e  red library, fin
bcc60 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74  ding entry point
bcc70 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20  s.** within the 
bcc80 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
bcc90 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20  and closing the 
bcca0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a  shared library..
bccb0 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  */./*.** Interfa
bccc0 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
bccd0 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
bcce0 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
bccf0 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
bcd00 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
bcd10 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
bcd20 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
bcd30 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ary..*/.static v
bcd40 6f 69 64 20 2a 6f 73 32 44 6c 4f 70 65 6e 28 73  oid *os2DlOpen(s
bcd50 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
bcd60 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
bcd70 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 43 48 41  ilename){.  UCHA
bcd80 52 20 6c 6f 61 64 45 72 72 5b 32 35 36 5d 3b 0a  R loadErr[256];.
bcd90 20 20 48 4d 4f 44 55 4c 45 20 68 6d 6f 64 3b 0a    HMODULE hmod;.
bcda0 20 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 63    APIRET rc;.  c
bcdb0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70  har *zFilenameCp
bcdc0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61   = convertUtf8Pa
bcdd0 74 68 54 6f 43 70 28 7a 46 69 6c 65 6e 61 6d 65  thToCp(zFilename
bcde0 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4c 6f 61  );.  rc = DosLoa
bcdf0 64 4d 6f 64 75 6c 65 28 28 50 53 5a 29 6c 6f 61  dModule((PSZ)loa
bce00 64 45 72 72 2c 20 73 69 7a 65 6f 66 28 6c 6f 61  dErr, sizeof(loa
bce10 64 45 72 72 29 2c 20 7a 46 69 6c 65 6e 61 6d 65  dErr), zFilename
bce20 43 70 2c 20 26 68 6d 6f 64 29 3b 0a 20 20 66 72  Cp, &hmod);.  fr
bce30 65 65 28 7a 46 69 6c 65 6e 61 6d 65 43 70 29 3b  ee(zFilenameCp);
bce40 0a 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d 20  .  return rc != 
bce50 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 28  NO_ERROR ? 0 : (
bce60 76 6f 69 64 2a 29 68 6d 6f 64 3b 0a 7d 0a 2f 2a  void*)hmod;.}./*
bce70 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 20 73 69 6e 63  .** A no-op sinc
bce80 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  e the error code
bce90 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
bcea0 74 68 65 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c  the DosLoadModul
bceb0 65 20 63 61 6c 6c 2e 0a 2a 2a 20 6f 73 32 44 6c  e call..** os2Dl
bcec0 6f 70 65 6e 20 72 65 74 75 72 6e 73 20 7a 65 72  open returns zer
bced0 6f 20 69 66 20 44 6f 73 4c 6f 61 64 4d 6f 64 75  o if DosLoadModu
bcee0 6c 65 20 69 73 20 6e 6f 74 20 73 75 63 63 65 73  le is not succes
bcef0 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sful..*/.static 
bcf00 76 6f 69 64 20 6f 73 32 44 6c 45 72 72 6f 72 28  void os2DlError(
bcf10 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
bcf20 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
bcf30 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 2f 2a 20  r *zBufOut){./* 
bcf40 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 74 61 74 69  no-op */.}.stati
bcf50 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 53 79 6d  c void *os2DlSym
bcf60 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
bcf70 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c  fs, void *pHandl
bcf80 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
bcf90 53 79 6d 62 6f 6c 29 7b 0a 20 20 50 46 4e 20 70  Symbol){.  PFN p
bcfa0 66 6e 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b  fn;.  APIRET rc;
bcfb0 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79  .  rc = DosQuery
bcfc0 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c  ProcAddr((HMODUL
bcfd0 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 7a  E)pHandle, 0L, z
bcfe0 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20  Symbol, &pfn);. 
bcff0 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52   if( rc != NO_ER
bd000 52 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 69 66  ROR ){.    /* if
bd010 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 74 73 65   the symbol itse
bd020 6c 66 20 77 61 73 20 6e 6f 74 20 66 6f 75 6e 64  lf was not found
bd030 2c 20 73 65 61 72 63 68 20 61 67 61 69 6e 20 66  , search again f
bd040 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20  or the same.    
bd050 20 2a 20 73 79 6d 62 6f 6c 20 77 69 74 68 20 61   * symbol with a
bd060 6e 20 65 78 74 72 61 20 75 6e 64 65 72 73 63 6f  n extra undersco
bd070 72 65 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62  re, that might b
bd080 65 20 6e 65 65 64 65 64 20 64 65 70 65 6e 64 69  e needed dependi
bd090 6e 67 0a 20 20 20 20 20 2a 20 6f 6e 20 74 68 65  ng.     * on the
bd0a0 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e 74   calling convent
bd0b0 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  ion */.    char 
bd0c0 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d 20 3d 20  _zSymbol[256] = 
bd0d0 22 5f 22 3b 0a 20 20 20 20 73 74 72 6e 63 61 74  "_";.    strncat
bd0e0 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 79 6d 62  (_zSymbol, zSymb
bd0f0 6f 6c 2c 20 32 35 35 29 3b 0a 20 20 20 20 72 63  ol, 255);.    rc
bd100 20 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41   = DosQueryProcA
bd110 64 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61  ddr((HMODULE)pHa
bd120 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53 79 6d 62  ndle, 0L, _zSymb
bd130 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 7d 0a 20  ol, &pfn);.  }. 
bd140 20 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e 4f   return rc != NO
bd150 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 6f  _ERROR ? 0 : (vo
bd160 69 64 2a 29 70 66 6e 3b 0a 7d 0a 73 74 61 74 69  id*)pfn;.}.stati
bd170 63 20 76 6f 69 64 20 6f 73 32 44 6c 43 6c 6f 73  c void os2DlClos
bd180 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  e(sqlite3_vfs *p
bd190 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64  Vfs, void *pHand
bd1a0 6c 65 29 7b 0a 20 20 44 6f 73 46 72 65 65 4d 6f  le){.  DosFreeMo
bd1b0 64 75 6c 65 28 28 48 4d 4f 44 55 4c 45 29 70 48  dule((HMODULE)pH
bd1c0 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20  andle);.}.#else 
bd1d0 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49  /* if SQLITE_OMI
bd1e0 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
bd1f0 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a   is defined: */.
bd200 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 4f    #define os2DlO
bd210 70 65 6e 20 30 0a 20 20 23 64 65 66 69 6e 65 20  pen 0.  #define 
bd220 6f 73 32 44 6c 45 72 72 6f 72 20 30 0a 20 20 23  os2DlError 0.  #
bd230 64 65 66 69 6e 65 20 6f 73 32 44 6c 53 79 6d 20  define os2DlSym 
bd240 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44  0.  #define os2D
bd250 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a  lClose 0.#endif.
bd260 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70  ../*.** Write up
bd270 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f   to nBuf bytes o
bd280 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74  f randomness int
bd290 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69  o zBuf..*/.stati
bd2a0 63 20 69 6e 74 20 6f 73 32 52 61 6e 64 6f 6d 6e  c int os2Randomn
bd2b0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ess(sqlite3_vfs 
bd2c0 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c  *pVfs, int nBuf,
bd2d0 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20   char *zBuf ){. 
bd2e0 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 23 69 66 20   int n = 0;.#if 
bd2f0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
bd300 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 66 3b  EST).  n = nBuf;
bd310 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20  .  memset(zBuf, 
bd320 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 65 0a  0, nBuf);.#else.
bd330 20 20 69 6e 74 20 73 69 7a 65 6f 66 55 4c 6f 6e    int sizeofULon
bd340 67 20 3d 20 73 69 7a 65 6f 66 28 55 4c 4f 4e 47  g = sizeof(ULONG
bd350 29 3b 0a 20 20 69 66 28 20 28 69 6e 74 29 73 69  );.  if( (int)si
bd360 7a 65 6f 66 28 44 41 54 45 54 49 4d 45 29 20 3c  zeof(DATETIME) <
bd370 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20  = nBuf - n ){.  
bd380 20 20 44 41 54 45 54 49 4d 45 20 78 3b 0a 20 20    DATETIME x;.  
bd390 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65    DosGetDateTime
bd3a0 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  (&x);.    memcpy
bd3b0 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73  (&zBuf[n], &x, s
bd3c0 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e  izeof(x));.    n
bd3d0 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20   += sizeof(x);. 
bd3e0 20 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f 66   }..  if( sizeof
bd3f0 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20  ULong <= nBuf - 
bd400 6e 20 29 7b 0a 20 20 20 20 50 50 49 42 20 70 70  n ){.    PPIB pp
bd410 69 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e  ib;.    DosGetIn
bd420 66 6f 42 6c 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26  foBlocks(NULL, &
bd430 70 70 69 62 29 3b 0a 20 20 20 20 6d 65 6d 63 70  ppib);.    memcp
bd440 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 70 69  y(&zBuf[n], &ppi
bd450 62 2d 3e 70 69 62 5f 75 6c 70 69 64 2c 20 73 69  b->pib_ulpid, si
bd460 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20  zeofULong);.    
bd470 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  n += sizeofULong
bd480 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 69 7a  ;.  }..  if( siz
bd490 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66  eofULong <= nBuf
bd4a0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 50 54 49 42   - n ){.    PTIB
bd4b0 20 70 74 69 62 3b 0a 20 20 20 20 44 6f 73 47 65   ptib;.    DosGe
bd4c0 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69  tInfoBlocks(&pti
bd4d0 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 6d 65  b, NULL);.    me
bd4e0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
bd4f0 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d  ptib->tib_ptib2-
bd500 3e 74 69 62 32 5f 75 6c 74 69 64 2c 20 73 69 7a  >tib2_ultid, siz
bd510 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e  eofULong);.    n
bd520 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b   += sizeofULong;
bd530 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77 65  .  }..  /* if we
bd540 20 73 74 69 6c 6c 20 68 61 76 65 6e 27 74 20 66   still haven't f
bd550 69 6c 6c 65 64 20 74 68 65 20 62 75 66 66 65 72  illed the buffer
bd560 20 79 65 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69   yet the followi
bd570 6e 67 20 77 69 6c 6c 20 2a 2f 0a 20 20 2f 2a 20  ng will */.  /* 
bd580 67 72 61 62 20 65 76 65 72 79 74 68 69 6e 67 20  grab everything 
bd590 6f 6e 63 65 20 69 6e 73 74 65 61 64 20 6f 66 20  once instead of 
bd5a0 6d 61 6b 69 6e 67 20 73 65 76 65 72 61 6c 20 63  making several c
bd5b0 61 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c  alls for a singl
bd5c0 65 20 69 74 65 6d 20 2a 2f 0a 20 20 69 66 28 20  e item */.  if( 
bd5d0 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e  sizeofULong <= n
bd5e0 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 55  Buf - n ){.    U
bd5f0 4c 4f 4e 47 20 75 6c 53 79 73 49 6e 66 6f 5b 51  LONG ulSysInfo[Q
bd600 53 56 5f 4d 41 58 5d 3b 0a 20 20 20 20 44 6f 73  SV_MAX];.    Dos
bd610 51 75 65 72 79 53 79 73 49 6e 66 6f 28 31 4c 2c  QuerySysInfo(1L,
bd620 20 51 53 56 5f 4d 41 58 2c 20 75 6c 53 79 73 49   QSV_MAX, ulSysI
bd630 6e 66 6f 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  nfo, sizeofULong
bd640 20 2a 20 51 53 56 5f 4d 41 58 29 3b 0a 0a 20 20   * QSV_MAX);..  
bd650 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
bd660 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53  ], &ulSysInfo[QS
bd670 56 5f 4d 53 5f 43 4f 55 4e 54 20 2d 20 31 5d 2c  V_MS_COUNT - 1],
bd680 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20   sizeofULong);. 
bd690 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c     n += sizeofUL
bd6a0 6f 6e 67 3b 0a 0a 20 20 20 20 69 66 28 20 73 69  ong;..    if( si
bd6b0 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75  zeofULong <= nBu
bd6c0 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d  f - n ){.      m
bd6d0 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20  emcpy(&zBuf[n], 
bd6e0 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54  &ulSysInfo[QSV_T
bd6f0 49 4d 45 52 5f 49 4e 54 45 52 56 41 4c 20 2d 20  IMER_INTERVAL - 
bd700 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29  1], sizeofULong)
bd710 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ;.      n += siz
bd720 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a  eofULong;.    }.
bd730 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c      if( sizeofUL
bd740 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ong <= nBuf - n 
bd750 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
bd760 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73  &zBuf[n], &ulSys
bd770 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 4c 4f  Info[QSV_TIME_LO
bd780 57 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c  W - 1], sizeofUL
bd790 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d  ong);.      n +=
bd7a0 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20   sizeofULong;.  
bd7b0 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65    }.    if( size
bd7c0 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20  ofULong <= nBuf 
bd7d0 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  - n ){.      mem
bd7e0 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75  cpy(&zBuf[n], &u
bd7f0 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d  lSysInfo[QSV_TIM
bd800 45 5f 48 49 47 48 20 2d 20 31 5d 2c 20 73 69 7a  E_HIGH - 1], siz
bd810 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20  eofULong);.     
bd820 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
bd830 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  g;.    }.    if(
bd840 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
bd850 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
bd860 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
bd870 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53  ], &ulSysInfo[QS
bd880 56 5f 54 4f 54 41 56 41 49 4c 4d 45 4d 20 2d 20  V_TOTAVAILMEM - 
bd890 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29  1], sizeofULong)
bd8a0 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ;.      n += siz
bd8b0 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a  eofULong;.    }.
bd8c0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65    }.#endif..  re
bd8d0 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
bd8e0 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74   Sleep for a lit
bd8f0 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75  tle while.  Retu
bd900 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  rn the amount of
bd910 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20   time slept..** 
bd920 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  The argument is 
bd930 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
bd940 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61  croseconds we wa
bd950 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20  nt to sleep..** 
bd960 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
bd970 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
bd980 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f  f microseconds o
bd990 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79  f sleep actually
bd9a0 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 72  .** requested fr
bd9b0 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  om the underlyin
bd9c0 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  g operating syst
bd9d0 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69  em, a number whi
bd9e0 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67  ch.** might be g
bd9f0 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
bda00 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75  qual to the argu
bda10 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65  ment, but not le
bda20 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61  ss.** than the a
bda30 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  rgument..*/.stat
bda40 69 63 20 69 6e 74 20 6f 73 32 53 6c 65 65 70 28  ic int os2Sleep(
bda50 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
bda60 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  fs, int microsec
bda70 20 29 7b 0a 20 20 44 6f 73 53 6c 65 65 70 28 20   ){.  DosSleep( 
bda80 28 6d 69 63 72 6f 73 65 63 2f 31 30 30 30 29 20  (microsec/1000) 
bda90 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72  );.  return micr
bdaa0 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  osec;.}../*.** T
bdab0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72  he following var
bdac0 69 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f  iable, if set to
bdad0 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75   a non-zero valu
bdae0 65 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72  e, becomes the r
bdaf0 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65  esult.** returne
bdb00 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73  d from sqlite3Os
bdb10 43 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20  CurrentTime().  
bdb20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72  This is used for
bdb30 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66   testing..*/.#if
bdb40 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
bdb50 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
bdb60 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
bdb70 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  ime = 0;.#endif.
bdb80 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
bdb90 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e  current time (in
bdba0 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64   Universal Coord
bdbb0 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57  inated Time).  W
bdbc0 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72  rite the.** curr
bdbd0 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74  ent time and dat
bdbe0 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61  e as a Julian Da
bdbf0 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70  y number into *p
bdc00 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75  rNow and.** retu
bdc10 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20  rn 0.  Return 1 
bdc20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20  if the time and 
bdc30 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66  date cannot be f
bdc40 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32  ound..*/.int os2
bdc50 43 75 72 72 65 6e 74 54 69 6d 65 28 20 73 71 6c  CurrentTime( sql
bdc60 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
bdc70 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 20 29 7b  double *prNow ){
bdc80 0a 20 20 64 6f 75 62 6c 65 20 6e 6f 77 3b 0a 20  .  double now;. 
bdc90 20 53 48 4f 52 54 20 6d 69 6e 75 74 65 3b 20 2f   SHORT minute; /
bdca0 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 62  * needs to be ab
bdcb0 6c 65 20 74 6f 20 63 6f 70 65 20 77 69 74 68 20  le to cope with 
bdcc0 6e 65 67 61 74 69 76 65 20 74 69 6d 65 7a 6f 6e  negative timezon
bdcd0 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 55 53  e offset */.  US
bdce0 48 4f 52 54 20 73 65 63 6f 6e 64 2c 20 68 6f 75  HORT second, hou
bdcf0 72 2c 0a 20 20 20 20 20 20 20 20 20 64 61 79 2c  r,.         day,
bdd00 20 6d 6f 6e 74 68 2c 20 79 65 61 72 3b 0a 20 20   month, year;.  
bdd10 44 41 54 45 54 49 4d 45 20 64 74 3b 0a 20 20 44  DATETIME dt;.  D
bdd20 6f 73 47 65 74 44 61 74 65 54 69 6d 65 28 20 26  osGetDateTime( &
bdd30 64 74 20 29 3b 0a 20 20 73 65 63 6f 6e 64 20 3d  dt );.  second =
bdd40 20 28 55 53 48 4f 52 54 29 64 74 2e 73 65 63 6f   (USHORT)dt.seco
bdd50 6e 64 73 3b 0a 20 20 6d 69 6e 75 74 65 20 3d 20  nds;.  minute = 
bdd60 28 53 48 4f 52 54 29 64 74 2e 6d 69 6e 75 74 65  (SHORT)dt.minute
bdd70 73 20 2b 20 64 74 2e 74 69 6d 65 7a 6f 6e 65 3b  s + dt.timezone;
bdd80 0a 20 20 68 6f 75 72 20 3d 20 28 55 53 48 4f 52  .  hour = (USHOR
bdd90 54 29 64 74 2e 68 6f 75 72 73 3b 0a 20 20 64 61  T)dt.hours;.  da
bdda0 79 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 64  y = (USHORT)dt.d
bddb0 61 79 3b 0a 20 20 6d 6f 6e 74 68 20 3d 20 28 55  ay;.  month = (U
bddc0 53 48 4f 52 54 29 64 74 2e 6d 6f 6e 74 68 3b 0a  SHORT)dt.month;.
bddd0 20 20 79 65 61 72 20 3d 20 28 55 53 48 4f 52 54    year = (USHORT
bdde0 29 64 74 2e 79 65 61 72 3b 0a 0a 20 20 2f 2a 20  )dt.year;..  /* 
bddf0 43 61 6c 63 75 6c 61 74 69 6f 6e 73 20 66 72 6f  Calculations fro
bde00 6d 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74  m http://www.ast
bde10 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e  ro.keele.ac.uk/~
bde20 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a  rno/Astronomy/hj
bde30 64 2e 68 74 6d 6c 0a 20 20 20 20 20 68 74 74 70  d.html.     http
bde40 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65  ://www.astro.kee
bde50 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73  le.ac.uk/~rno/As
bde60 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2d 30 2e 31 2e  tronomy/hjd-0.1.
bde70 63 20 2a 2f 0a 20 20 2f 2a 20 43 61 6c 63 75 6c  c */.  /* Calcul
bde80 61 74 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 64  ate the Julian d
bde90 61 79 73 20 2a 2f 0a 20 20 6e 6f 77 20 3d 20 64  ays */.  now = d
bdea0 61 79 20 2d 20 33 32 30 37 36 20 2b 0a 20 20 20  ay - 32076 +.   
bdeb0 20 31 34 36 31 2a 28 79 65 61 72 20 2b 20 34 38   1461*(year + 48
bdec0 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34  00 + (month - 14
bded0 29 2f 31 32 29 2f 34 20 2b 0a 20 20 20 20 33 36  )/12)/4 +.    36
bdee0 37 2a 28 6d 6f 6e 74 68 20 2d 20 32 20 2d 20 28  7*(month - 2 - (
bdef0 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 2a 31  month - 14)/12*1
bdf00 32 29 2f 31 32 20 2d 0a 20 20 20 20 33 2a 28 28  2)/12 -.    3*((
bdf10 79 65 61 72 20 2b 20 34 39 30 30 20 2b 20 28 6d  year + 4900 + (m
bdf20 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 31  onth - 14)/12)/1
bdf30 30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 20 41 64 64  00)/4;..  /* Add
bdf40 20 74 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 20   the fractional 
bdf50 68 6f 75 72 73 2c 20 6d 69 6e 73 20 61 6e 64 20  hours, mins and 
bdf60 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 6e 6f 77  seconds */.  now
bdf70 20 2b 3d 20 28 68 6f 75 72 20 2b 20 31 32 2e 30   += (hour + 12.0
bdf80 29 2f 32 34 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d  )/24.0;.  now +=
bdf90 20 6d 69 6e 75 74 65 2f 31 34 34 30 2e 30 3b 0a   minute/1440.0;.
bdfa0 20 20 6e 6f 77 20 2b 3d 20 73 65 63 6f 6e 64 2f    now += second/
bdfb0 38 36 34 30 30 2e 30 3b 0a 20 20 2a 70 72 4e 6f  86400.0;.  *prNo
bdfc0 77 20 3d 20 6e 6f 77 3b 0a 23 69 66 64 65 66 20  w = now;.#ifdef 
bdfd0 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66  SQLITE_TEST.  if
bdfe0 28 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  ( sqlite3_curren
bdff0 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70  t_time ){.    *p
be000 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33 5f 63  rNow = sqlite3_c
be010 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30  urrent_time/8640
be020 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b  0.0 + 2440587.5;
be030 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
be040 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 61 74 69  turn 0;.}..stati
be050 63 20 69 6e 74 20 6f 73 32 47 65 74 4c 61 73 74  c int os2GetLast
be060 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
be070 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
be080 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
be090 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
be0a0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
be0b0 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65  and deinitialize
be0c0 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
be0d0 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e  ystem interface.
be0e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
be0f0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  nt sqlite3_os_in
be100 69 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  it(void){.  stat
be110 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f  ic sqlite3_vfs o
be120 73 32 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c  s2Vfs = {.    1,
be130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be140 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
be150 20 20 20 20 73 69 7a 65 6f 66 28 6f 73 32 46 69      sizeof(os2Fi
be160 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69  le),   /* szOsFi
be170 6c 65 20 2a 2f 0a 20 20 20 20 43 43 48 4d 41 58  le */.    CCHMAX
be180 50 41 54 48 2c 20 20 20 20 20 20 20 20 2f 2a 20  PATH,        /* 
be190 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20  mxPathname */.  
be1a0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
be1b0 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f       /* pNext */
be1c0 0a 20 20 20 20 22 6f 73 32 22 2c 20 20 20 20 20  .    "os2",     
be1d0 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65          /* zName
be1e0 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
be1f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41             /* pA
be200 70 70 44 61 74 61 20 2a 2f 0a 0a 20 20 20 20 6f  ppData */..    o
be210 73 32 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20  s2Open,         
be220 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20    /* xOpen */.  
be230 20 20 6f 73 32 44 65 6c 65 74 65 2c 20 20 20 20    os2Delete,    
be240 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20       /* xDelete 
be250 2a 2f 0a 20 20 20 20 6f 73 32 41 63 63 65 73 73  */.    os2Access
be260 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63  ,         /* xAc
be270 63 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 46  cess */.    os2F
be280 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f  ullPathname,   /
be290 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20  * xFullPathname 
be2a0 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 4f 70 65 6e  */.    os2DlOpen
be2b0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c  ,         /* xDl
be2c0 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44  Open */.    os2D
be2d0 6c 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f  lError,        /
be2e0 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20  * xDlError */.  
be2f0 20 20 6f 73 32 44 6c 53 79 6d 2c 20 20 20 20 20    os2DlSym,     
be300 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a       /* xDlSym *
be310 2f 0a 20 20 20 20 6f 73 32 44 6c 43 6c 6f 73 65  /.    os2DlClose
be320 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43  ,        /* xDlC
be330 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 6f 73 32 52  lose */.    os2R
be340 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f  andomness,     /
be350 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f  * xRandomness */
be360 0a 20 20 20 20 6f 73 32 53 6c 65 65 70 2c 20 20  .    os2Sleep,  
be370 20 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65          /* xSlee
be380 70 20 2a 2f 0a 20 20 20 20 6f 73 32 43 75 72 72  p */.    os2Curr
be390 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78  entTime,    /* x
be3a0 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20  CurrentTime */. 
be3b0 20 20 20 6f 73 32 47 65 74 4c 61 73 74 45 72 72     os2GetLastErr
be3c0 6f 72 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73  or    /* xGetLas
be3d0 74 45 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 20  tError */.  };. 
be3e0 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
be3f0 69 73 74 65 72 28 26 6f 73 32 56 66 73 2c 20 31  ister(&os2Vfs, 1
be400 29 3b 0a 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62  );.  initUconvOb
be410 6a 65 63 74 73 28 29 3b 0a 20 20 72 65 74 75 72  jects();.  retur
be420 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53  n SQLITE_OK;.}.S
be430 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
be440 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69  lite3_os_end(voi
be450 64 29 7b 0a 20 20 66 72 65 65 55 63 6f 6e 76 4f  d){.  freeUconvO
be460 62 6a 65 63 74 73 28 29 3b 0a 20 20 72 65 74 75  bjects();.  retu
be470 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
be480 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
be490 45 5f 4f 53 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a  E_OS_OS2 */../**
be4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
be4b0 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a   of os_os2.c ***
be4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
be4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
be500 69 6e 20 66 69 6c 65 20 6f 73 5f 75 6e 69 78 2e  in file os_unix.
be510 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
be520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
be540 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a  ** 2004 May 22.*
be550 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
be560 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
be570 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
be580 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
be590 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
be5a0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
be5b0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
be5c0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
be5d0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
be5e0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
be5f0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
be600 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
be610 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
be620 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
be630 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
be640 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
be650 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
be660 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
be670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be6b0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
be6c0 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
be6d0 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  VFS implementati
be6e0 6f 6e 20 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65  on for unix-like
be6f0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
be700 6d 73 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 4c 69  ms.** include Li
be710 6e 75 78 2c 20 4d 61 63 4f 53 58 2c 20 2a 42 53  nux, MacOSX, *BS
be720 44 2c 20 51 4e 58 2c 20 56 78 57 6f 72 6b 73 2c  D, QNX, VxWorks,
be730 20 41 49 58 2c 20 48 50 55 58 2c 20 61 6e 64 20   AIX, HPUX, and 
be740 6f 74 68 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  others..**.** Th
be750 65 72 65 20 61 72 65 20 61 63 74 75 61 6c 6c 79  ere are actually
be760 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 65   several differe
be770 6e 74 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74  nt VFS implement
be780 61 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20 66  ations in this f
be790 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 64 69 66 66  ile..** The diff
be7a0 65 72 65 6e 63 65 73 20 61 72 65 20 69 6e 20 74  erences are in t
be7b0 68 65 20 77 61 79 20 74 68 61 74 20 66 69 6c 65  he way that file
be7c0 20 6c 6f 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65   locking is done
be7d0 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a  .  The default.*
be7e0 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
be7f0 20 75 73 65 73 20 50 6f 73 69 78 20 41 64 76 69   uses Posix Advi
be800 73 6f 72 79 20 4c 6f 63 6b 73 2e 20 20 41 6c 74  sory Locks.  Alt
be810 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65  ernative impleme
be820 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 75 73 65 20  ntations.** use 
be830 66 6c 6f 63 6b 28 29 2c 20 64 6f 74 2d 66 69 6c  flock(), dot-fil
be840 65 73 2c 20 76 61 72 69 6f 75 73 20 70 72 6f 70  es, various prop
be850 72 69 65 74 61 72 79 20 6c 6f 63 6b 69 6e 67 20  rietary locking 
be860 73 63 68 65 6d 61 73 2c 20 6f 72 20 73 69 6d 70  schemas, or simp
be870 6c 79 0a 2a 2a 20 73 6b 69 70 20 6c 6f 63 6b 69  ly.** skip locki
be880 6e 67 20 61 6c 6c 20 74 6f 67 65 74 68 65 72 2e  ng all together.
be890 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 6f 75 72  .**.** This sour
be8a0 63 65 20 66 69 6c 65 20 69 73 20 6f 72 67 61 6e  ce file is organ
be8b0 69 7a 65 64 20 69 6e 74 6f 20 64 69 76 69 73 69  ized into divisi
be8c0 6f 6e 73 20 77 68 65 72 65 20 74 68 65 20 6c 6f  ons where the lo
be8d0 67 69 63 20 66 6f 72 20 76 61 72 69 6f 75 73 0a  gic for various.
be8e0 2a 2a 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20  ** subfunctions 
be8f0 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74  is contained wit
be900 68 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69  hin the appropri
be910 61 74 65 20 64 69 76 69 73 69 6f 6e 2e 20 20 50  ate division.  P
be920 4c 45 41 53 45 0a 2a 2a 20 4b 45 45 50 20 54 48  LEASE.** KEEP TH
be930 45 20 53 54 52 55 43 54 55 52 45 20 4f 46 20 54  E STRUCTURE OF T
be940 48 49 53 20 46 49 4c 45 20 49 4e 54 41 43 54 2e  HIS FILE INTACT.
be950 20 20 4e 65 77 20 63 6f 64 65 20 73 68 6f 75 6c    New code shoul
be960 64 20 62 65 20 70 6c 61 63 65 64 0a 2a 2a 20 69  d be placed.** i
be970 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 64 69  n the correct di
be980 76 69 73 69 6f 6e 20 61 6e 64 20 73 68 6f 75 6c  vision and shoul
be990 64 20 62 65 20 63 6c 65 61 72 6c 79 20 6c 61 62  d be clearly lab
be9a0 65 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eled..**.** The 
be9b0 6c 61 79 6f 75 74 20 6f 66 20 64 69 76 69 73 69  layout of divisi
be9c0 6f 6e 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  ons is as follow
be9d0 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 47 65  s:.**.**   *  Ge
be9e0 6e 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 64 65  neral-purpose de
be9f0 63 6c 61 72 61 74 69 6f 6e 73 20 61 6e 64 20 75  clarations and u
bea00 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73  tility functions
bea10 2e 0a 2a 2a 20 20 20 2a 20 20 55 6e 69 71 75 65  ..**   *  Unique
bea20 20 66 69 6c 65 20 49 44 20 6c 6f 67 69 63 20 75   file ID logic u
bea30 73 65 64 20 62 79 20 56 78 57 6f 72 6b 73 2e 0a  sed by VxWorks..
bea40 2a 2a 20 20 20 2a 20 20 56 61 72 69 6f 75 73 20  **   *  Various 
bea50 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76  locking primitiv
bea60 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
bea70 73 20 28 61 6c 6c 20 65 78 63 65 70 74 20 70 72  s (all except pr
bea80 6f 78 79 20 6c 6f 63 6b 69 6e 67 29 3a 0a 2a 2a  oxy locking):.**
bea90 20 20 20 20 20 20 2b 20 66 6f 72 20 50 6f 73 69        + for Posi
beaa0 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73  x Advisory Locks
beab0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e  .**      + for n
beac0 6f 2d 6f 70 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20  o-op locks.**   
bead0 20 20 20 2b 20 66 6f 72 20 64 6f 74 2d 66 69 6c     + for dot-fil
beae0 65 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20  e locks.**      
beaf0 2b 20 66 6f 72 20 66 6c 6f 63 6b 28 29 20 6c 6f  + for flock() lo
beb00 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 2b 20  cking.**      + 
beb10 66 6f 72 20 6e 61 6d 65 64 20 73 65 6d 61 70 68  for named semaph
beb20 6f 72 65 20 6c 6f 63 6b 73 20 28 56 78 57 6f 72  ore locks (VxWor
beb30 6b 73 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 20 20  ks only).**     
beb40 20 2b 20 66 6f 72 20 41 46 50 20 66 69 6c 65 73   + for AFP files
beb50 79 73 74 65 6d 20 6c 6f 63 6b 73 20 28 4d 61 63  ystem locks (Mac
beb60 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a  OSX only).**   *
beb70 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d    sqlite3_file m
beb80 65 74 68 6f 64 73 20 6e 6f 74 20 61 73 73 6f 63  ethods not assoc
beb90 69 61 74 65 64 20 77 69 74 68 20 6c 6f 63 6b 69  iated with locki
beba0 6e 67 2e 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69  ng..**   *  Defi
bebb0 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74  nitions of sqlit
bebc0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
bebd0 6a 65 63 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f  jects for all lo
bebe0 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65  cking.**      me
bebf0 74 68 6f 64 73 20 70 6c 75 73 20 22 66 69 6e 64  thods plus "find
bec00 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f  er" functions fo
bec10 72 20 65 61 63 68 20 6c 6f 63 6b 69 6e 67 20 6d  r each locking m
bec20 65 74 68 6f 64 2e 0a 2a 2a 20 20 20 2a 20 20 73  ethod..**   *  s
bec30 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f  qlite3_vfs metho
bec40 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  d implementation
bec50 73 2e 0a 2a 2a 20 20 20 2a 20 20 4c 6f 63 6b 69  s..**   *  Locki
bec60 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 66 6f  ng primitives fo
bec70 72 20 74 68 65 20 70 72 6f 78 79 20 75 62 65 72  r the proxy uber
bec80 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 2e  -locking-method.
bec90 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a   (MacOSX only).*
beca0 2a 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f  *   *  Definitio
becb0 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66  ns of sqlite3_vf
becc0 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c  s objects for al
becd0 6c 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64  l locking method
bece0 73 0a 2a 2a 20 20 20 20 20 20 70 6c 75 73 20 69  s.**      plus i
becf0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
bed00 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  f sqlite3_os_ini
bed10 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  t() and sqlite3_
bed20 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20  os_end()..**.** 
bed30 24 49 64 3a 20 6f 73 5f 75 6e 69 78 2e 63 2c 76  $Id: os_unix.c,v
bed40 20 31 2e 32 35 30 20 32 30 30 39 2f 30 34 2f 30   1.250 2009/04/0
bed50 37 20 30 35 3a 33 35 3a 30 34 20 63 68 77 20 45  7 05:35:04 chw E
bed60 78 70 20 24 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  xp $.*/.#if SQLI
bed70 54 45 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20  TE_OS_UNIX      
bed80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
bed90 66 69 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20  file is used on 
beda0 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a  unix only */../*
bedb0 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 76 61  .** There are va
bedc0 72 69 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 6f  rious methods fo
bedd0 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75  r file locking u
bede0 73 65 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 65  sed for concurre
bedf0 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a  ncy.** control:.
bee00 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58  **.**   1. POSIX
bee10 20 6c 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65   locking (the de
bee20 66 61 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20  fault),.**   2. 
bee30 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20  No locking,.**  
bee40 20 33 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63   3. Dot-file loc
bee50 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c  king,.**   4. fl
bee60 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a  ock() locking,.*
bee70 2a 20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69  *   5. AFP locki
bee80 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a  ng (OSX only),.*
bee90 2a 20 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53  *   6. Named POS
beea0 49 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56  IX semaphores (V
beeb0 58 57 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a  XWorks only),.**
beec0 20 20 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b     7. proxy lock
beed0 69 6e 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a  ing. (OSX only).
beee0 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20  **.** Styles 4, 
beef0 35 2c 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c  5, and 7 are onl
bef00 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 53  y available of S
bef10 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
bef20 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73  KING_STYLE.** is
bef30 20 64 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 20   defined to 1.  
bef40 54 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  The SQLITE_ENABL
bef50 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
bef60 61 6c 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 74  also enables aut
bef70 6f 6d 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74  omatic.** select
bef80 69 6f 6e 20 6f 66 20 74 68 65 20 61 70 70 72 6f  ion of the appro
bef90 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73  priate locking s
befa0 74 79 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 68  tyle based on th
befb0 65 20 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20  e filesystem.** 
befc0 77 68 65 72 65 20 74 68 65 20 64 61 74 61 62 61  where the databa
befd0 73 65 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 20  se is located.  
befe0 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
beff0 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c  (SQLITE_ENABLE_L
bf000 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20  OCKING_STYLE).# 
bf010 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   if defined(__AP
bf020 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69  PLE__).#    defi
bf030 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ne SQLITE_ENABLE
bf040 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31  _LOCKING_STYLE 1
bf050 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65  .#  else.#    de
bf060 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  fine SQLITE_ENAB
bf070 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
bf080 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64   0.#  endif.#end
bf090 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65  if../*.** Define
bf0a0 20 74 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20   the OS_VXWORKS 
bf0b0 70 72 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61  pre-processor ma
bf0c0 63 72 6f 20 74 6f 20 31 20 69 66 20 62 75 69 6c  cro to 1 if buil
bf0d0 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f  ding on .** vxwo
bf0e0 72 6b 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77  rks, or 0 otherw
bf0f0 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ise..*/.#ifndef 
bf100 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 66  OS_VXWORKS.#  if
bf110 20 64 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f   defined(__RTP__
bf120 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 52  ) || defined(_WR
bf130 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20 64  S_KERNEL).#    d
bf140 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53  efine OS_VXWORKS
bf150 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20   1.#  else.#    
bf160 64 65 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b  define OS_VXWORK
bf170 53 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e  S 0.#  endif.#en
bf180 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  dif../*.** These
bf190 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75 6c 64   #defines should
bf1a0 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 6c   enable >2GB fil
bf1b0 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73  e support on Pos
bf1c0 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64  ix if the.** und
bf1d0 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e  erlying operatin
bf1e0 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  g system support
bf1f0 73 20 69 74 2e 20 20 49 66 20 74 68 65 20 4f 53  s it.  If the OS
bf200 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20   lacks.** large 
bf210 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 68  file support, th
bf220 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f  ese should be no
bf230 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67  -ops..**.** Larg
bf240 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63  e file support c
bf250 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75  an be disabled u
bf260 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54  sing the -DSQLIT
bf270 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77  E_DISABLE_LFS sw
bf280 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63  itch.** on the c
bf290 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20  ompiler command 
bf2a0 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e  line.  This is n
bf2b0 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20  ecessary if you 
bf2c0 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a  are compiling.**
bf2d0 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63   on a recent mac
bf2e0 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74  hine (ex: RedHat
bf2f0 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77 61   7.2) but you wa
bf300 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20  nt your code to 
bf310 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c  work.** on an ol
bf320 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78 3a  der machine (ex:
bf330 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20 20 49   RedHat 6.0).  I
bf340 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e  f you compile on
bf350 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77   RedHat 7.2.** w
bf360 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69  ithout this opti
bf370 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c  on, LFS is enabl
bf380 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73  e.  But LFS does
bf390 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68   not exist in th
bf3a0 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52  e kernel.** in R
bf3b0 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 68  edHat 6.0, so th
bf3c0 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72  e code won't wor
bf3d0 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d  k.  Hence, for m
bf3e0 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a  aximum binary.**
bf3f0 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79 6f 75   portability you
bf400 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53   should omit LFS
bf410 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76  ..**.** The prev
bf420 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 20 77  ious paragraph w
bf430 61 73 20 77 72 69 74 74 65 6e 20 69 6e 20 32 30  as written in 20
bf440 30 35 2e 20 20 28 54 68 69 73 20 70 61 72 61 67  05.  (This parag
bf450 72 61 70 68 20 69 73 20 77 72 69 74 74 65 6e 0a  raph is written.
bf460 2a 2a 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38  ** on 2008-11-28
bf470 2e 29 20 54 68 65 73 65 20 64 61 79 73 2c 20 61  .) These days, a
bf480 6c 6c 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73  ll Linux kernels
bf490 20 73 75 70 70 6f 72 74 20 6c 61 72 67 65 20 66   support large f
bf4a0 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20  iles, so.** you 
bf4b0 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20  should probably 
bf4c0 6c 65 61 76 65 20 4c 46 53 20 65 6e 61 62 6c 65  leave LFS enable
bf4d0 64 2e 20 20 42 75 74 20 73 6f 6d 65 20 65 6d 62  d.  But some emb
bf4e0 65 64 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 20  edded platforms 
bf4f0 6d 69 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46  might.** lack LF
bf500 53 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  S in which case 
bf510 74 68 65 20 53 51 4c 49 54 45 5f 44 49 53 41 42  the SQLITE_DISAB
bf520 4c 45 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67  LE_LFS macro mig
bf530 68 74 20 73 74 69 6c 6c 20 62 65 20 75 73 65 66  ht still be usef
bf540 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ul..*/.#ifndef S
bf550 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46  QLITE_DISABLE_LF
bf560 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47  S.# define _LARG
bf570 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23  E_FILE       1.#
bf580 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46   ifndef _FILE_OF
bf590 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65  FSET_BITS.#   de
bf5a0 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45  fine _FILE_OFFSE
bf5b0 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69  T_BITS 64.# endi
bf5c0 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47  f.# define _LARG
bf5d0 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23  EFILE_SOURCE 1.#
bf5e0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61  endif../*.** sta
bf5f0 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69  ndard include fi
bf600 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65  les..*/.#include
bf610 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23   <sys/types.h>.#
bf620 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61  include <sys/sta
bf630 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66  t.h>.#include <f
bf640 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  cntl.h>.#include
bf650 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63   <unistd.h>.#inc
bf660 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68  lude <sys/time.h
bf670 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e  >.#include <errn
bf680 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45  o.h>..#if SQLITE
bf690 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
bf6a0 53 54 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20  STYLE.# include 
bf6b0 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20  <sys/ioctl.h>.# 
bf6c0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20  if OS_VXWORKS.# 
bf6d0 20 69 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70 68   include <semaph
bf6e0 6f 72 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64  ore.h>.#  includ
bf6f0 65 20 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65  e <limits.h>.# e
bf700 6c 73 65 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c  lse.#  include <
bf710 73 79 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69  sys/file.h>.#  i
bf720 6e 63 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61  nclude <sys/para
bf730 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20  m.h>.#  include 
bf740 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20  <sys/mount.h>.# 
bf750 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20  endif.#endif /* 
bf760 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
bf770 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a  CKING_STYLE */..
bf780 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20  /*.** If we are 
bf790 74 6f 20 62 65 20 74 68 72 65 61 64 2d 73 61 66  to be thread-saf
bf7a0 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70  e, include the p
bf7b0 74 68 72 65 61 64 73 20 68 65 61 64 65 72 20 61  threads header a
bf7c0 6e 64 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65  nd define.** the
bf7d0 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52   SQLITE_UNIX_THR
bf7e0 45 41 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23  EADS macro..*/.#
bf7f0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
bf800 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SAFE.# define SQ
bf810 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44  LITE_UNIX_THREAD
bf820 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  S 1.#endif../*.*
bf830 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69 73  * Default permis
bf840 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 74  sions when creat
bf850 69 6e 67 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a  ing a new file.*
bf860 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
bf870 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45  _DEFAULT_FILE_PE
bf880 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69  RMISSIONS.# defi
bf890 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
bf8a0 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f  T_FILE_PERMISSIO
bf8b0 4e 53 20 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a  NS 0644.#endif..
bf8c0 2f 2a 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20 70  /*. ** Default p
bf8d0 65 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20  ermissions when 
bf8e0 63 72 65 61 74 69 6e 67 20 61 75 74 6f 20 70 72  creating auto pr
bf8f0 6f 78 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e  oxy dir. */.#ifn
bf900 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
bf910 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d  LT_PROXYDIR_PERM
bf920 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65  ISSIONS.# define
bf930 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
bf940 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53  PROXYDIR_PERMISS
bf950 49 4f 4e 53 20 30 37 35 35 0a 23 65 6e 64 69 66  IONS 0755.#endif
bf960 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
bf970 73 75 70 70 6f 72 74 65 64 20 70 61 74 68 2d 6c  supported path-l
bf980 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ength..*/.#defin
bf990 65 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 35  e MAX_PATHNAME 5
bf9a0 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73  12../*.** Only s
bf9b0 65 74 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f  et the lastErrno
bf9c0 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 63 6f   if the error co
bf9d0 64 65 20 69 73 20 61 20 72 65 61 6c 20 65 72 72  de is a real err
bf9e0 6f 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61  or and not .** a
bf9f0 20 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 64   normal expected
bfa00 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20   return code of 
bfa10 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 53  SQLITE_BUSY or S
bfa20 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66  QLITE_OK.*/.#def
bfa30 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  ine IS_LOCK_ERRO
bfa40 52 28 78 29 20 20 28 28 78 20 21 3d 20 53 51 4c  R(x)  ((x != SQL
bfa50 49 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d  ITE_OK) && (x !=
bfa60 20 53 51 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a   SQLITE_BUSY))..
bfa70 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69 78 46  ./*.** The unixF
bfa80 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73  ile structure is
bfa90 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c   subclass of sql
bfaa0 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66  ite3_file specif
bfab0 69 63 20 74 6f 20 74 68 65 20 75 6e 69 78 0a 2a  ic to the unix.*
bfac0 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61  * VFS implementa
bfad0 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  tions..*/.typede
bfae0 66 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c  f struct unixFil
bfaf0 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73 74 72 75  e unixFile;.stru
bfb00 63 74 20 75 6e 69 78 46 69 6c 65 20 7b 0a 20 20  ct unixFile {.  
bfb10 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
bfb20 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f  ds const *pMetho
bfb30 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68  d;  /* Always th
bfb40 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f  e first entry */
bfb50 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70  .  struct unixOp
bfb60 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20  enCnt *pOpen;   
bfb70 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75      /* Info abou
bfb80 74 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27 73 20  t all open fd's 
bfb90 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f  on this inode */
bfba0 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  .  struct unixLo
bfbb0 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20  ckInfo *pLock;  
bfbc0 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75      /* Info abou
bfbd0 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 73 20  t locks on this 
bfbe0 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68  inode */.  int h
bfbf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
bfc00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
bfc10 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
bfc20 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66  or */.  int dirf
bfc30 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
bfc40 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
bfc50 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20   descriptor for 
bfc60 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 2a 2f  the directory */
bfc70 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
bfc80 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 20   locktype;      
bfc90 20 20 20 20 2f 2a 20 54 68 65 20 74 79 70 65 20      /* The type 
bfca0 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  of lock held on 
bfcb0 74 68 69 73 20 66 64 20 2a 2f 0a 20 20 69 6e 74  this fd */.  int
bfcc0 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 20 20   lastErrno;     
bfcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bfce0 20 54 68 65 20 75 6e 69 78 20 65 72 72 6e 6f 20   The unix errno 
bfcf0 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f  from the last I/
bfd00 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 76 6f 69  O error */.  voi
bfd10 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  d *lockingContex
bfd20 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
bfd30 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 73   Locking style s
bfd40 70 65 63 69 66 69 63 20 73 74 61 74 65 20 2a 2f  pecific state */
bfd50 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
bfd60 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
bfd70 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73  .  int openFlags
bfd80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
bfd90 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 67 73      /* The flags
bfda0 20 73 70 65 63 69 66 69 65 64 20 61 74 20 6f 70   specified at op
bfdb0 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  en() */.#endif.#
bfdc0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
bfdd0 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28  SAFE && defined(
bfde0 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68  __linux__).  pth
bfdf0 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20 20 20  read_t tid;     
bfe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bfe10 20 54 68 65 20 74 68 72 65 61 64 20 74 68 61 74   The thread that
bfe20 20 22 6f 77 6e 73 22 20 74 68 69 73 20 75 6e 69   "owns" this uni
bfe30 78 46 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 66 0a  xFile */.#endif.
bfe40 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
bfe50 20 69 6e 74 20 69 73 44 65 6c 65 74 65 3b 20 20   int isDelete;  
bfe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfe70 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63    /* Delete on c
bfe80 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a  lose if true */.
bfe90 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73    struct vxworks
bfea0 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 20 20  FileId *pId;    
bfeb0 20 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 69 6c     /* Unique fil
bfec0 65 20 49 44 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  e ID */.#endif.#
bfed0 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
bfee0 2f 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75  /* The next grou
bfef0 70 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 61  p of variables a
bff00 72 65 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b  re used to track
bff10 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
bff20 74 68 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  the.  ** transac
bff30 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 6e 20  tion counter in 
bff40 62 79 74 65 73 20 32 34 2d 32 37 20 6f 66 20 64  bytes 24-27 of d
bff50 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 61 72  atabase files ar
bff60 65 20 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 77  e updated.  ** w
bff70 68 65 6e 65 76 65 72 20 61 6e 79 20 70 61 72 74  henever any part
bff80 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
bff90 20 63 68 61 6e 67 65 73 2e 20 20 41 6e 20 61 73   changes.  An as
bffa0 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69  sertion fault wi
bffb0 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 72 20 69 66  ll.  ** occur if
bffc0 20 61 20 66 69 6c 65 20 69 73 20 75 70 64 61 74   a file is updat
bffd0 65 64 20 77 69 74 68 6f 75 74 20 61 6c 73 6f 20  ed without also 
bffe0 75 70 64 61 74 69 6e 67 20 74 68 65 20 74 72 61  updating the tra
bfff0 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f  nsaction.  ** co
c0000 75 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73  unter.  This tes
c0010 74 20 69 73 20 6d 61 64 65 20 74 6f 20 61 76 6f  t is made to avo
c0020 69 64 20 6e 65 77 20 70 72 6f 62 6c 65 6d 73 20  id new problems 
c0030 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65 0a 20  similar to the. 
c0040 20 2a 2a 20 6f 6e 65 20 64 65 73 63 72 69 62 65   ** one describe
c0050 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 35 38  d by ticket #358
c0060 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67  4. .  */.  unsig
c0070 6e 65 64 20 63 68 61 72 20 74 72 61 6e 73 43 6e  ned char transCn
c0080 74 72 43 68 6e 67 3b 20 20 20 2f 2a 20 54 72 75  trChng;   /* Tru
c0090 65 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63  e if the transac
c00a0 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 63 68 61  tion counter cha
c00b0 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  nged */.  unsign
c00c0 65 64 20 63 68 61 72 20 64 62 55 70 64 61 74 65  ed char dbUpdate
c00d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
c00e0 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20   if any part of 
c00f0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68  database file ch
c0100 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67  anged */.  unsig
c0110 6e 65 64 20 63 68 61 72 20 69 6e 4e 6f 72 6d 61  ned char inNorma
c0120 6c 57 72 69 74 65 3b 20 20 20 2f 2a 20 54 72 75  lWrite;   /* Tru
c0130 65 20 69 66 20 69 6e 20 61 20 6e 6f 72 6d 61 6c  e if in a normal
c0140 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e   write operation
c0150 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 72 75   */..  /* If tru
c0160 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65  e, that means we
c0170 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
c0180 68 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  h a database fil
c0190 65 20 74 68 61 74 20 68 61 73 0a 20 20 2a 2a 20  e that has.  ** 
c01a0 61 20 72 61 6e 67 65 20 6f 66 20 6c 6f 63 6b 69  a range of locki
c01b0 6e 67 20 62 79 74 65 73 20 66 72 6f 6d 20 50 45  ng bytes from PE
c01c0 4e 44 49 4e 47 5f 42 59 54 45 20 74 68 72 6f 75  NDING_BYTE throu
c01d0 67 68 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b  gh PENDING_BYTE+
c01e0 35 31 31 0a 20 20 2a 2a 20 77 68 69 63 68 20 73  511.  ** which s
c01f0 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72  hould never be r
c0200 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20  ead or written. 
c0210 20 41 73 73 65 72 74 73 28 29 20 77 69 6c 6c 20   Asserts() will 
c0220 76 65 72 69 66 79 20 74 68 69 73 20 2a 2f 0a 20  verify this */. 
c0230 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69   unsigned char i
c0240 73 4c 6f 63 6b 61 62 6c 65 3b 20 20 20 20 20 20  sLockable;      
c0250 2f 2a 20 54 72 75 65 20 69 66 20 66 69 6c 65 20  /* True if file 
c0260 6d 69 67 68 74 20 62 65 20 6c 6f 63 6b 65 64 20  might be locked 
c0270 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  */.#endif.#ifdef
c0280 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f   SQLITE_TEST.  /
c0290 2a 20 49 6e 20 74 65 73 74 20 6d 6f 64 65 2c 20  * In test mode, 
c02a0 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a  increase the siz
c02b0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
c02c0 75 72 65 20 61 20 62 69 74 20 73 6f 20 74 68 61  ure a bit so tha
c02d0 74 20 0a 20 20 2a 2a 20 69 74 20 69 73 20 6c 61  t .  ** it is la
c02e0 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 73 74  rger than the st
c02f0 72 75 63 74 20 43 72 61 73 68 46 69 6c 65 20 64  ruct CrashFile d
c0300 65 66 69 6e 65 64 20 69 6e 20 74 65 73 74 36 2e  efined in test6.
c0310 63 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20 61  c..  */.  char a
c0320 50 61 64 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e  Padding[32];.#en
c0330 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e  dif.};../*.** In
c0340 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20  clude code that 
c0350 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  is common to all
c0360 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f   os_*.c files.*/
c0370 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c0380 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d   Include os_comm
c0390 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  on.h in the midd
c03a0 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20  le of os_unix.c 
c03b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c03c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c03d0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63   Begin file os_c
c03e0 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
c03f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c0410 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20  ./*.** 2004 May 
c0420 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
c0430 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
c0440 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
c0450 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
c0460 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
c0470 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
c0480 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
c0490 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
c04a0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
c04b0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
c04c0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
c04d0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
c04e0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
c04f0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
c0500 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
c0510 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
c0520 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
c0530 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
c0540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0580 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
c0590 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
c05a0 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74  macros and a lit
c05b0 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20  tle bit of code 
c05c0 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
c05d0 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20  o.** all of the 
c05e0 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69  platform-specifi
c05f0 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29  c files (os_*.c)
c0600 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65   and is #include
c0610 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20  d into those.** 
c0620 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  files..**.** Thi
c0630 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65  s file should be
c0640 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68   #included by th
c0650 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f  e os_*.c files o
c0660 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20  nly.  It is not 
c0670 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72  a.** general pur
c0680 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65  pose header file
c0690 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f  ..**.** $Id: os_
c06a0 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20  common.h,v 1.38 
c06b0 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 30  2009/02/24 18:40
c06c0 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :50 danielk1977 
c06d0 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
c06e0 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23   _OS_COMMON_H_.#
c06f0 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f  define _OS_COMMO
c0700 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c  N_H_../*.** At l
c0710 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61  east two bugs ha
c0720 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65  ve slipped in be
c0730 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64  cause we changed
c0740 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55   the MEMORY_DEBU
c0750 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51  G.** macro to SQ
c0760 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73  LITE_DEBUG and s
c0770 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69  ome older makefi
c0780 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74  les have not yet
c0790 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69   made the.** swi
c07a0 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  tch.  The follow
c07b0 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  ing code should 
c07c0 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c  catch this probl
c07d0 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  em at compile-ti
c07e0 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45  me..*/.#ifdef ME
c07f0 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72  MORY_DEBUG.# err
c0800 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44  or "The MEMORY_D
c0810 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62  EBUG macro is ob
c0820 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c  solete.  Use SQL
c0830 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61  ITE_DEBUG instea
c0840 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  d.".#endif..#ifd
c0850 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
c0860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
c0870 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  nt sqlite3OSTrac
c0880 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f  e = 0;.#define O
c0890 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20  STRACE1(X)      
c08a0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
c08b0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
c08c0 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64  ebugPrintf(X).#d
c08d0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58  efine OSTRACE2(X
c08e0 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71  ,Y)       if( sq
c08f0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
c0900 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c0910 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f  f(X,Y).#define O
c0920 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20  STRACE3(X,Y,Z)  
c0930 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
c0940 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
c0950 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
c0960 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c0970 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66  E4(X,Y,Z,A)   if
c0980 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
c0990 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
c09a0 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23  rintf(X,Y,Z,A).#
c09b0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
c09c0 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73  X,Y,Z,A,B) if( s
c09d0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
c09e0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
c09f0 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64  tf(X,Y,Z,A,B).#d
c0a00 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58  efine OSTRACE6(X
c0a10 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20  ,Y,Z,A,B,C) \.  
c0a20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72    if(sqlite3OSTr
c0a30 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75  ace) sqlite3Debu
c0a40 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c  gPrintf(X,Y,Z,A,
c0a50 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  B,C).#define OST
c0a60 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE7(X,Y,Z,A,B,
c0a70 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71  C,D) \.    if(sq
c0a80 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71  lite3OSTrace) sq
c0a90 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
c0aa0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a  (X,Y,Z,A,B,C,D).
c0ab0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53  #else.#define OS
c0ac0 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e  TRACE1(X).#defin
c0ad0 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a  e OSTRACE2(X,Y).
c0ae0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33  #define OSTRACE3
c0af0 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
c0b00 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
c0b10 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c0b20 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64  E5(X,Y,Z,A,B).#d
c0b30 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58  efine OSTRACE6(X
c0b40 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66  ,Y,Z,A,B,C).#def
c0b50 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59  ine OSTRACE7(X,Y
c0b60 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64  ,Z,A,B,C,D).#end
c0b70 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  if../*.** Macros
c0b80 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65   for performance
c0b90 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61   tracing.  Norma
c0ba0 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20  lly turned off. 
c0bb0 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f   Only works.** o
c0bc0 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e  n i486 hardware.
c0bd0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
c0be0 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52  E_PERFORMANCE_TR
c0bf0 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69  ACE../* .** hwti
c0c00 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e  me.h contains in
c0c10 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63  line assembler c
c0c20 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e  ode for implemen
c0c30 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65  ting .** high-pe
c0c40 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67  rformance timing
c0c50 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a   routines..*/./*
c0c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
c0c70 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69  clude hwtime.h i
c0c80 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
c0c90 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
c0ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
c0cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
c0cc0 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e  gin file hwtime.
c0cd0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
c0ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
c0d00 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a  .** 2008 May 27.
c0d10 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
c0d20 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
c0d30 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
c0d40 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
c0d50 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
c0d60 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
c0d70 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
c0d80 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
c0d90 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
c0da0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
c0db0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
c0dc0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
c0dd0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
c0de0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
c0df0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
c0e00 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
c0e10 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
c0e20 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
c0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c0e70 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
c0e80 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c  ile contains inl
c0e90 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72  ine asm code for
c0ea0 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67   retrieving "hig
c0eb0 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a  h-performance".*
c0ec0 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78  * counters for x
c0ed0 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a  86 class CPUs..*
c0ee0 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65  *.** $Id: hwtime
c0ef0 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38  .h,v 1.3 2008/08
c0f00 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61  /01 14:33:15 sha
c0f10 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  ne Exp $.*/.#ifn
c0f20 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23  def _HWTIME_H_.#
c0f30 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48  define _HWTIME_H
c0f40 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  _../*.** The fol
c0f50 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f  lowing routine o
c0f60 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e  nly works on pen
c0f70 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e  tium-class (or n
c0f80 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73  ewer) processors
c0f90 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65  ..** It uses the
c0fa0 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f   RDTSC opcode to
c0fb0 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20   read the cycle 
c0fc0 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20  count value out 
c0fd0 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73  of the.** proces
c0fe0 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20  sor and returns 
c0ff0 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69  that value.  Thi
c1000 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f  s can be used fo
c1010 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72  r high-res.** pr
c1020 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20  ofiling..*/.#if 
c1030 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
c1040 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
c1050 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20  SC_VER)) && \.  
c1060 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38      (defined(i38
c1070 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  6) || defined(__
c1080 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  i386__) || defin
c1090 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20  ed(_M_IX86))..  
c10a0 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e  #if defined(__GN
c10b0 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  UC__)..  __inlin
c10c0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
c10d0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
c10e0 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69  void){.     unsi
c10f0 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b  gned int lo, hi;
c1100 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f  .     __asm__ __
c1110 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74  volatile__ ("rdt
c1120 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c  sc" : "=a" (lo),
c1130 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20   "=d" (hi));.   
c1140 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65    return (sqlite
c1150 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32  _uint64)hi << 32
c1160 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65   | lo;.  }..  #e
c1170 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43  lif defined(_MSC
c1180 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73  _VER)..  __decls
c1190 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c  pec(naked) __inl
c11a0 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  ine sqlite_uint6
c11b0 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65  4 __cdecl sqlite
c11c0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
c11d0 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20      __asm {.    
c11e0 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20      rdtsc.      
c11f0 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65    ret       ; re
c1200 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44  turn value at ED
c1210 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d  X:EAX.     }.  }
c1220 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69  ..  #endif..#eli
c1230 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  f (defined(__GNU
c1240 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28  C__) && defined(
c1250 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20  __x86_64__))..  
c1260 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
c1270 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
c1280 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
c1290 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
c12a0 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61  g val;.      __a
c12b0 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
c12c0 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41  _ ("rdtsc" : "=A
c12d0 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20  " (val));.      
c12e0 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a  return val;.  }.
c12f0 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64   .#elif (defined
c1300 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
c1310 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a  fined(__ppc__)).
c1320 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71  .  __inline__ sq
c1330 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
c1340 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
c1350 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
c1360 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c  long long retval
c1370 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  ;.      unsigned
c1380 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20   long junk;.    
c1390 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61    __asm__ __vola
c13a0 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20  tile__ ("\n\.   
c13b0 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d         1:      m
c13c0 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20  ftbu   %1\n\.   
c13d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
c13e0 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20  ftb    %L0\n\.  
c13f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1400 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20  mftbu   %0\n\.  
c1410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1420 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c  cmpw    %0,%1\n\
c1430 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c1440 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20     bne     1b". 
c1450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1460 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29   : "=r" (retval)
c1470 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a  , "=r" (junk));.
c1480 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74        return ret
c1490 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a  val;.  }..#else.
c14a0 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69  .  #error Need i
c14b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
c14c0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
c14d0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f   for your platfo
c14e0 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  rm...  /*.  ** T
c14f0 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75  o compile withou
c1500 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73  t implementing s
c1510 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66  qlite3Hwtime() f
c1520 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d  or your platform
c1530 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72  ,.  ** you can r
c1540 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20  emove the above 
c1550 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74  #error and use t
c1560 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a  he following.  *
c1570 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e  * stub function.
c1580 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20    You will lose 
c1590 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66  timing support f
c15a0 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20  or many.  ** of 
c15b0 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e  the debugging an
c15c0 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74  d testing utilit
c15d0 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75  ies, but it shou
c15e0 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74  ld at.  ** least
c15f0 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e   compile and run
c1600 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ..  */.SQLITE_PR
c1610 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75  IVATE   sqlite_u
c1620 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
c1630 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72  ime(void){ retur
c1640 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36  n ((sqlite_uint6
c1650 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a  4)0); }..#endif.
c1660 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
c1670 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20  ned(_HWTIME_H_) 
c1680 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
c1690 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d  *** End of hwtim
c16a0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
c16b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c16c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c16d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
c16e0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
c16f0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
c1700 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   in os_common.h 
c1710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1720 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69  **/..static sqli
c1730 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72  te_uint64 g_star
c1740 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  t;.static sqlite
c1750 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65  _uint64 g_elapse
c1760 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  d;.#define TIMER
c1770 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73  _START       g_s
c1780 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69  tart=sqlite3Hwti
c1790 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d  me().#define TIM
c17a0 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67  ER_END         g
c17b0 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33  _elapsed=sqlite3
c17c0 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74  Hwtime()-g_start
c17d0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
c17e0 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61  LAPSED     g_ela
c17f0 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69  psed.#else.#defi
c1800 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23  ne TIMER_START.#
c1810 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44  define TIMER_END
c1820 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
c1830 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c  LAPSED     ((sql
c1840 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65  ite_uint64)0).#e
c1850 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77  ndif../*.** If w
c1860 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74  e compile with t
c1870 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d  he SQLITE_TEST m
c1880 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74  acro set, then t
c1890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
c18a0 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69  ck.** of code wi
c18b0 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61  ll give us the a
c18c0 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61  bility to simula
c18d0 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72  te a disk I/O er
c18e0 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73  ror.  This.** is
c18f0 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
c1900 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65  g the I/O recove
c1910 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66  ry logic..*/.#if
c1920 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
c1930 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
c1940 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c1950 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  hit = 0;        
c1960 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
c1970 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72  ber of I/O Error
c1980 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  s */.SQLITE_API 
c1990 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
c19a0 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30  rror_hardhit = 0
c19b0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
c19c0 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e  er of non-benign
c19d0 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54   errors */.SQLIT
c19e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c19f0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
c1a00 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  ng = 0;        /
c1a10 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20  * Count down to 
c1a20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20  first I/O error 
c1a30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
c1a40 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
c1a50 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20  or_persist = 0; 
c1a60 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
c1a70 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72  f I/O errors per
c1a80 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  sist */.SQLITE_A
c1a90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
c1aa0 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d  o_error_benign =
c1ab0 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54   0;         /* T
c1ac0 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72  rue if errors ar
c1ad0 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49  e benign */.SQLI
c1ae0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c1af0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
c1b00 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f  ing = 0;.SQLITE_
c1b10 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c1b20 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64  diskfull = 0;.#d
c1b30 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
c1b40 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73  ErrorBenign(X) s
c1b50 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c1b60 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69  benign=(X).#defi
c1b70 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
c1b80 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66  or(CODE)  \.  if
c1b90 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  ( (sqlite3_io_er
c1ba0 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73  ror_persist && s
c1bb0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c1bc0 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c  hit) \.       ||
c1bd0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c1be0 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31  r_pending-- == 1
c1bf0 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20   )  \.          
c1c00 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72      { local_ioer
c1c10 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61  r(); CODE; }.sta
c1c20 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69  tic void local_i
c1c30 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43  oerr(){.  IOTRAC
c1c40 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a  E(("IOERR\n"));.
c1c50 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
c1c60 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20  or_hit++;.  if( 
c1c70 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f  !sqlite3_io_erro
c1c80 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74  r_benign ) sqlit
c1c90 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64  e3_io_error_hard
c1ca0 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65  hit++;.}.#define
c1cb0 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
c1cc0 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20  lError(CODE) \. 
c1cd0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69    if( sqlite3_di
c1ce0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29  skfull_pending )
c1cf0 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c  { \.     if( sql
c1d00 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
c1d10 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a  nding == 1 ){ \.
c1d20 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65         local_ioe
c1d30 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73  rr(); \.       s
c1d40 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20  qlite3_diskfull 
c1d50 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71  = 1; \.       sq
c1d60 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
c1d70 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20  it = 1; \.      
c1d80 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65   CODE; \.     }e
c1d90 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71  lse{ \.       sq
c1da0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
c1db0 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20  ending--; \.    
c1dc0 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a   } \.   }.#else.
c1dd0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
c1de0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29  IOErrorBenign(X)
c1df0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
c1e00 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66  eIOError(A).#def
c1e10 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ine SimulateDisk
c1e20 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e  fullError(A).#en
c1e30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  dif../*.** When 
c1e40 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20  testing, keep a 
c1e50 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d  count of the num
c1e60 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65  ber of open file
c1e70 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  s..*/.#ifdef SQL
c1e80 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
c1e90 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c1ea0 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20  open_file_count 
c1eb0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65  = 0;.#define Ope
c1ec0 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c  nCounter(X)  sql
c1ed0 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63  ite3_open_file_c
c1ee0 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a  ount+=(X).#else.
c1ef0 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e  #define OpenCoun
c1f00 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23  ter(X).#endif..#
c1f10 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
c1f20 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29  d(_OS_COMMON_H_)
c1f30 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
c1f40 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63  **** End of os_c
c1f50 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
c1f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1f80 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c1f90 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
c1fa0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
c1fb0 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a  f in os_unix.c *
c1fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1fd0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69  ***/../*.** Defi
c1fe0 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f  ne various macro
c1ff0 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 69  s that are missi
c2000 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73  ng from some sys
c2010 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tems..*/.#ifndef
c2020 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64   O_LARGEFILE.# d
c2030 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c  efine O_LARGEFIL
c2040 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  E 0.#endif.#ifde
c2050 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  f SQLITE_DISABLE
c2060 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c  _LFS.# undef O_L
c2070 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e  ARGEFILE.# defin
c2080 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a  e O_LARGEFILE 0.
c2090 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f  #endif.#ifndef O
c20a0 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69  _NOFOLLOW.# defi
c20b0 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a  ne O_NOFOLLOW 0.
c20c0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f  #endif.#ifndef O
c20d0 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65  _BINARY.# define
c20e0 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64   O_BINARY 0.#end
c20f0 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a  if../*.** The DJ
c2100 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76  GPP compiler env
c2110 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d  ironment looks m
c2120 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c  ostly like Unix,
c2130 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73   but it.** lacks
c2140 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73   the fcntl() sys
c2150 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65  tem call.  So re
c2160 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74  define fcntl() t
c2170 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a  o be something.*
c2180 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 75  * that always su
c2190 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65  cceeds.  This me
c21a0 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67  ans that locking
c21b0 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20   does not occur 
c21c0 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20  under.** DJGPP. 
c21d0 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 2d   But it is DOS -
c21e0 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 78   what did you ex
c21f0 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20  pect?.*/.#ifdef 
c2200 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69  __DJGPP__.# defi
c2210 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20  ne fcntl(A,B,C) 
c2220 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
c2230 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 63  The threadid mac
c2240 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74  ro resolves to t
c2250 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20  he thread-id or 
c2260 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a  to 0.  Used for.
c2270 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  ** testing and d
c2280 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a  ebugging only..*
c2290 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  /.#if SQLITE_THR
c22a0 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20  EADSAFE.#define 
c22b0 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61 64  threadid pthread
c22c0 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64  _self().#else.#d
c22d0 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 30  efine threadid 0
c22e0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
c22f0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73  Helper functions
c2300 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72   to obtain and r
c2310 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c  elinquish the gl
c2320 6f 62 61 6c 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73  obal mutex..*/.s
c2330 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 45  tatic void unixE
c2340 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29 7b  nterMutex(void){
c2350 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
c2360 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75  _enter(sqlite3Mu
c2370 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
c2380 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
c2390 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20  TER));.}.static 
c23a0 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 4d 75  void unixLeaveMu
c23b0 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c  tex(void){.  sql
c23c0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
c23d0 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
c23e0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
c23f0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b  STATIC_MASTER));
c2400 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .}...#ifdef SQLI
c2410 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48  TE_DEBUG./*.** H
c2420 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66  elper function f
c2430 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20  or printing out 
c2440 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f  trace informatio
c2450 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67  n from debugging
c2460 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68  .** binaries. Th
c2470 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  is returns the s
c2480 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74  tring represetat
c2490 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c  ion of the suppl
c24a0 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c  ied.** integer l
c24b0 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61  ock-type..*/.sta
c24c0 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
c24d0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74  locktypeName(int
c24e0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77   locktype){.  sw
c24f0 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29  itch( locktype )
c2500 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b  {.  case NO_LOCK
c2510 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b  : return "NONE";
c2520 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c  .  case SHARED_L
c2530 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41  OCK: return "SHA
c2540 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45 53  RED";.  case RES
c2550 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75  ERVED_LOCK: retu
c2560 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20  rn "RESERVED";. 
c2570 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f   case PENDING_LO
c2580 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44  CK: return "PEND
c2590 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58 43  ING";.  case EXC
c25a0 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74  LUSIVE_LOCK: ret
c25b0 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b  urn "EXCLUSIVE";
c25c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45  .  }.  return "E
c25d0 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a  RROR";.}.#endif.
c25e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c  .#ifdef SQLITE_L
c25f0 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20  OCK_TRACE./*.** 
c2600 50 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d  Print out inform
c2610 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20  ation about all 
c2620 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f  locking operatio
c2630 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ns..**.** This r
c2640 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66  outine is used f
c2650 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69  or troubleshooti
c2660 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74  ng locks on mult
c2670 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61  ithreaded.** pla
c2680 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20  tforms.  Enable 
c2690 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74  by compiling wit
c26a0 68 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c  h the -DSQLITE_L
c26b0 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d  OCK_TRACE.** com
c26c0 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e  mand-line option
c26d0 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72   on the compiler
c26e0 2e 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 20  .  This code is 
c26f0 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e  normally.** turn
c2700 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69  ed off..*/.stati
c2710 63 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28  c int lockTrace(
c2720 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20  int fd, int op, 
c2730 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29  struct flock *p)
c2740 7b 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d  {.  char *zOpNam
c2750 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74  e, *zType;.  int
c2760 20 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45   s;.  int savedE
c2770 72 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  rrno;.  if( op==
c2780 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a  F_GETLK ){.    z
c2790 4f 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22  OpName = "GETLK"
c27a0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70  ;.  }else if( op
c27b0 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20  ==F_SETLK ){.   
c27c0 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c   zOpName = "SETL
c27d0 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  K";.  }else{.   
c27e0 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f   s = fcntl(fd, o
c27f0 70 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  p, p);.    sqlit
c2800 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66  e3DebugPrintf("f
c2810 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20  cntl unknown %d 
c2820 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70  %d %d\n", fd, op
c2830 2c 20 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , s);.    return
c2840 20 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d   s;.  }.  if( p-
c2850 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b  >l_type==F_RDLCK
c2860 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20   ){.    zType = 
c2870 22 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65  "RDLCK";.  }else
c2880 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d   if( p->l_type==
c2890 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a  F_WRLCK ){.    z
c28a0 54 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a  Type = "WRLCK";.
c28b0 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c    }else if( p->l
c28c0 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29  _type==F_UNLCK )
c28d0 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55  {.    zType = "U
c28e0 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NLCK";.  }else{.
c28f0 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b      assert( 0 );
c2900 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
c2910 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b  ->l_whence==SEEK
c2920 5f 53 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63  _SET );.  s = fc
c2930 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a  ntl(fd, op, p);.
c2940 20 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65    savedErrno = e
c2950 72 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44  rrno;.  sqlite3D
c2960 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74  ebugPrintf("fcnt
c2970 6c 20 25 64 20 25 64 20 25 73 20 25 73 20 25 64  l %d %d %s %s %d
c2980 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20   %d %d %d\n",.  
c2990 20 20 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c     threadid, fd,
c29a0 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c   zOpName, zType,
c29b0 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74   (int)p->l_start
c29c0 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c  , (int)p->l_len,
c29d0 0a 20 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f  .     (int)p->l_
c29e0 70 69 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73  pid, s);.  if( s
c29f0 3d 3d 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f  ==(-1) && op==F_
c2a00 53 45 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74  SETLK && (p->l_t
c2a10 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20  ype==F_RDLCK || 
c2a20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c  p->l_type==F_WRL
c2a30 43 4b 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63  CK) ){.    struc
c2a40 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20  t flock l2;.    
c2a50 6c 32 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e  l2 = *p;.    fcn
c2a60 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20  tl(fd, F_GETLK, 
c2a70 26 6c 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32  &l2);.    if( l2
c2a80 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b  .l_type==F_RDLCK
c2a90 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20   ){.      zType 
c2aa0 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d  = "RDLCK";.    }
c2ab0 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79  else if( l2.l_ty
c2ac0 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20  pe==F_WRLCK ){. 
c2ad0 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52       zType = "WR
c2ae0 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20  LCK";.    }else 
c2af0 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46  if( l2.l_type==F
c2b00 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20  _UNLCK ){.      
c2b10 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b  zType = "UNLCK";
c2b20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
c2b30 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20    assert( 0 );. 
c2b40 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
c2b50 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e  DebugPrintf("fcn
c2b60 74 6c 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f  tl-failure-reaso
c2b70 6e 3a 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e  n: %s %d %d %d\n
c2b80 22 2c 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c  ",.       zType,
c2b90 20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74   (int)l2.l_start
c2ba0 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c  , (int)l2.l_len,
c2bb0 20 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b   (int)l2.l_pid);
c2bc0 0a 20 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73  .  }.  errno = s
c2bd0 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74  avedErrno;.  ret
c2be0 75 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65  urn s;.}.#define
c2bf0 20 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65   fcntl lockTrace
c2c00 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
c2c10 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a  E_LOCK_TRACE */.
c2c20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
c2c30 75 74 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 73  utine translates
c2c40 20 61 20 73 74 61 6e 64 61 72 64 20 50 4f 53 49   a standard POSI
c2c50 58 20 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e 74  X errno code int
c2c60 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 75  o something.** u
c2c70 73 65 66 75 6c 20 74 6f 20 74 68 65 20 63 6c 69  seful to the cli
c2c80 65 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69  ents of the sqli
c2c90 74 65 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  te3 functions.  
c2ca0 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 74  Specifically, it
c2cb0 20 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20   is.** intended 
c2cc0 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76  to translate a v
c2cd0 61 72 69 65 74 79 20 6f 66 20 22 74 72 79 20 61  ariety of "try a
c2ce0 67 61 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74  gain" errors int
c2cf0 6f 20 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a 2a  o SQLITE_BUSY.**
c2d00 20 61 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f   and a variety o
c2d10 66 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20  f "please close 
c2d20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
c2d30 74 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20  tor NOW" errors 
c2d40 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  into .** SQLITE_
c2d50 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 72  IOERR.** .** Err
c2d60 6f 72 73 20 64 75 72 69 6e 67 20 69 6e 69 74 69  ors during initi
c2d70 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f 63  alization of loc
c2d80 6b 73 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 74  ks, or file syst
c2d90 65 6d 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6c  em support for l
c2da0 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20  ocks,.** should 
c2db0 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 45  handle ENOLCK, E
c2dc0 4e 4f 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 55  NOTSUP, EOPNOTSU
c2dd0 50 50 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a  PP separately..*
c2de0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
c2df0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
c2e00 78 45 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 78  xError(int posix
c2e10 45 72 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 74  Error, int sqlit
c2e20 65 49 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 74  eIOErr) {.  swit
c2e30 63 68 20 28 70 6f 73 69 78 45 72 72 6f 72 29 20  ch (posixError) 
c2e40 7b 0a 20 20 63 61 73 65 20 30 3a 20 0a 20 20 20  {.  case 0: .   
c2e50 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c2e60 4b 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45  K;.    .  case E
c2e70 41 47 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 54  AGAIN:.  case ET
c2e80 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 20  IMEDOUT:.  case 
c2e90 45 42 55 53 59 3a 0a 20 20 63 61 73 65 20 45 49  EBUSY:.  case EI
c2ea0 4e 54 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f 4c  NTR:.  case ENOL
c2eb0 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 6e  CK:  .    /* ran
c2ec0 64 6f 6d 20 4e 46 53 20 72 65 74 72 79 20 65 72  dom NFS retry er
c2ed0 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 69  ror, unless duri
c2ee0 6e 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73  ng file system s
c2ef0 75 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69  upport .     * i
c2f00 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e  ntrospection, in
c2f10 20 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c   which it actual
c2f20 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74  ly means what it
c2f30 20 73 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 74   says */.    ret
c2f40 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
c2f50 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 43  .    .  case EAC
c2f60 43 45 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 43  CES: .    /* EAC
c2f70 43 45 53 20 69 73 20 6c 69 6b 65 20 45 41 47 41  CES is like EAGA
c2f80 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e  IN during lockin
c2f90 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 75  g operations, bu
c2fa0 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 20  t not any other 
c2fb0 74 69 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 28  time*/.    if( (
c2fc0 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53  sqliteIOErr == S
c2fd0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b  QLITE_IOERR_LOCK
c2fe0 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f  ) || ..(sqliteIO
c2ff0 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f  Err == SQLITE_IO
c3000 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 7c 7c 20 0a  ERR_UNLOCK) || .
c3010 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d  .(sqliteIOErr ==
c3020 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44   SQLITE_IOERR_RD
c3030 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73 71 6c 69 74  LOCK) ||..(sqlit
c3040 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45  eIOErr == SQLITE
c3050 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
c3060 52 56 45 44 4c 4f 43 4b 29 20 29 7b 0a 20 20 20  RVEDLOCK) ){.   
c3070 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
c3080 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20  _BUSY;.    }.   
c3090 20 2f 2a 20 65 6c 73 65 20 66 61 6c 6c 20 74 68   /* else fall th
c30a0 72 6f 75 67 68 20 2a 2f 0a 20 20 63 61 73 65 20  rough */.  case 
c30b0 45 50 45 52 4d 3a 20 0a 20 20 20 20 72 65 74 75  EPERM: .    retu
c30c0 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a  rn SQLITE_PERM;.
c30d0 20 20 20 20 0a 20 20 63 61 73 65 20 45 44 45 41      .  case EDEA
c30e0 44 4c 4b 3a 0a 20 20 20 20 72 65 74 75 72 6e 20  DLK:.    return 
c30f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f  SQLITE_IOERR_BLO
c3100 43 4b 45 44 3b 0a 20 20 20 20 0a 23 69 66 20 45  CKED;.    .#if E
c3110 4f 50 4e 4f 54 53 55 50 50 21 3d 45 4e 4f 54 53  OPNOTSUPP!=ENOTS
c3120 55 50 0a 20 20 63 61 73 65 20 45 4f 50 4e 4f 54  UP.  case EOPNOT
c3130 53 55 50 50 3a 20 0a 20 20 20 20 2f 2a 20 73 6f  SUPP: .    /* so
c3140 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 74 65 72  mething went ter
c3150 72 69 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c 65  ribly awry, unle
c3160 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73  ss during file s
c3170 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20  ystem support . 
c3180 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74      * introspect
c3190 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74  ion, in which it
c31a0 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20   actually means 
c31b0 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a  what it says */.
c31c0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 45 4e  #endif.#ifdef EN
c31d0 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4e 4f  OTSUP.  case ENO
c31e0 54 53 55 50 3a 20 0a 20 20 20 20 2f 2a 20 69 6e  TSUP: .    /* in
c31f0 76 61 6c 69 64 20 66 64 2c 20 75 6e 6c 65 73 73  valid fd, unless
c3200 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73   during file sys
c3210 74 65 6d 20 73 75 70 70 6f 72 74 20 69 6e 74 72  tem support intr
c3220 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68  ospection, in wh
c3230 69 63 68 20 0a 20 20 20 20 20 2a 20 69 74 20 61  ich .     * it a
c3240 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68  ctually means wh
c3250 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 65  at it says */.#e
c3260 6e 64 69 66 0a 20 20 63 61 73 65 20 45 49 4f 3a  ndif.  case EIO:
c3270 0a 20 20 63 61 73 65 20 45 42 41 44 46 3a 0a 20  .  case EBADF:. 
c3280 20 63 61 73 65 20 45 49 4e 56 41 4c 3a 0a 20 20   case EINVAL:.  
c3290 63 61 73 65 20 45 4e 4f 54 43 4f 4e 4e 3a 0a 20  case ENOTCONN:. 
c32a0 20 63 61 73 65 20 45 4e 4f 44 45 56 3a 0a 20 20   case ENODEV:.  
c32b0 63 61 73 65 20 45 4e 58 49 4f 3a 0a 20 20 63 61  case ENXIO:.  ca
c32c0 73 65 20 45 4e 4f 45 4e 54 3a 0a 20 20 63 61 73  se ENOENT:.  cas
c32d0 65 20 45 53 54 41 4c 45 3a 0a 20 20 63 61 73 65  e ESTALE:.  case
c32e0 20 45 4e 4f 53 59 53 3a 0a 20 20 20 20 2f 2a 20   ENOSYS:.    /* 
c32f0 74 68 65 73 65 20 73 68 6f 75 6c 64 20 66 6f 72  these should for
c3300 63 65 20 74 68 65 20 63 6c 69 65 6e 74 20 74 6f  ce the client to
c3310 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20   close the file 
c3320 61 6e 64 20 72 65 63 6f 6e 6e 65 63 74 20 2a 2f  and reconnect */
c3330 0a 20 20 20 20 0a 20 20 64 65 66 61 75 6c 74 3a  .    .  default:
c3340 20 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c   .    return sql
c3350 69 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d 0a  iteIOErr;.  }.}.
c3360 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
c3370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c33a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c33b0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
c33c0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69 71  ***** Begin Uniq
c33d0 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69  ue File ID Utili
c33e0 74 79 20 55 73 65 64 20 42 79 20 56 78 57 6f 72  ty Used By VxWor
c33f0 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ks *************
c3400 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73 74  **.**.** On most
c3410 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69   versions of uni
c3420 78 2c 20 77 65 20 63 61 6e 20 67 65 74 20 61 20  x, we can get a 
c3430 75 6e 69 71 75 65 20 49 44 20 66 6f 72 20 61 20  unique ID for a 
c3440 66 69 6c 65 20 62 79 20 63 6f 6e 63 61 74 65 6e  file by concaten
c3450 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65 76  ating.** the dev
c3460 69 63 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74  ice number and t
c3470 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e  he inode number.
c3480 20 20 42 75 74 20 74 68 69 73 20 64 6f 65 73 20    But this does 
c3490 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f  not work on VxWo
c34a0 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f 72  rks..** On VxWor
c34b0 6b 73 2c 20 61 20 75 6e 69 71 75 65 20 66 69 6c  ks, a unique fil
c34c0 65 20 69 64 20 6d 75 73 74 20 62 65 20 62 61 73  e id must be bas
c34d0 65 64 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e 69  ed on the canoni
c34e0 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a 2a  cal filename..**
c34f0 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  .** A pointer to
c3500 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
c3510 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
c3520 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 75  ructure can be u
c3530 73 65 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69 71  sed as a.** uniq
c3540 75 65 20 66 69 6c 65 20 49 44 20 69 6e 20 56 78  ue file ID in Vx
c3550 57 6f 72 6b 73 2e 20 20 45 61 63 68 20 69 6e 73  Works.  Each ins
c3560 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
c3570 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
c3580 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68  .** a copy of th
c3590 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65  e canonical file
c35a0 6e 61 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20  name.  There is 
c35b0 61 6c 73 6f 20 61 20 72 65 66 65 72 65 6e 63 65  also a reference
c35c0 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68 65   count.  .** The
c35d0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65   structure is re
c35e0 63 6c 61 69 6d 65 64 20 77 68 65 6e 20 74 68 65  claimed when the
c35f0 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74   number of point
c3600 65 72 73 20 74 6f 20 69 74 20 64 72 6f 70 73 20  ers to it drops 
c3610 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a  to.** zero..**.*
c3620 2a 20 54 68 65 72 65 20 61 72 65 20 6e 65 76 65  * There are neve
c3630 72 20 76 65 72 79 20 6d 61 6e 79 20 66 69 6c 65  r very many file
c3640 73 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 69  s open at one ti
c3650 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 61  me and lookups a
c3660 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72 66  re not.** a perf
c3670 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 6c  ormance-critical
c3680 20 70 61 74 68 2c 20 73 6f 20 69 74 20 69 73 20   path, so it is 
c3690 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 70 75  sufficient to pu
c36a0 74 20 74 68 65 73 65 0a 2a 2a 20 73 74 72 75 63  t these.** struc
c36b0 74 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b 65  tures on a linke
c36c0 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63  d list..*/.struc
c36d0 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
c36e0 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72  {.  struct vxwor
c36f0 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 78 74 3b  ksFileId *pNext;
c3700 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20 6c    /* Next in a l
c3710 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20  ist of them all 
c3720 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
c3730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3740 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
c3750 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
c3760 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  is one */.  int 
c3770 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  nName;          
c3780 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
c3790 67 74 68 20 6f 66 20 74 68 65 20 7a 43 61 6e 6f  gth of the zCano
c37a0 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72 69  nicalName[] stri
c37b0 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  ng */.  char *zC
c37c0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20 20  anonicalName;   
c37d0 20 20 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69 63        /* Canonic
c37e0 61 6c 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 7d  al filename */.}
c37f0 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  ;..#if OS_VXWORK
c3800 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e 69  S./* .** All uni
c3810 71 75 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 72  que filenames ar
c3820 65 20 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e 6b  e held on a link
c3830 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62  ed list headed b
c3840 79 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 62  y this.** variab
c3850 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  le:.*/.static st
c3860 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
c3870 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 4c  Id *vxworksFileL
c3880 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20  ist = 0;../*.** 
c3890 53 69 6d 70 6c 69 66 79 20 61 20 66 69 6c 65 6e  Simplify a filen
c38a0 61 6d 65 20 69 6e 74 6f 20 69 74 73 20 63 61 6e  ame into its can
c38b0 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20 62  onical form.** b
c38c0 79 20 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f 6c  y making the fol
c38d0 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 3a 0a  lowing changes:.
c38e0 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69 6e  **.**  * removin
c38f0 67 20 61 6e 79 20 74 72 61 69 6c 69 6e 67 20 61  g any trailing a
c3900 6e 64 20 64 75 70 6c 69 63 61 74 65 20 2f 0a 2a  nd duplicate /.*
c3910 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e 2f  *  * convert /./
c3920 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 20   into just /.** 
c3930 20 2a 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e 2e   * convert /A/..
c3940 2f 20 77 68 65 72 65 20 41 20 69 73 20 61 6e 79  / where A is any
c3950 20 73 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e 74   simple name int
c3960 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20 43  o just /.**.** C
c3970 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
c3980 69 6e 2d 70 6c 61 63 65 2e 20 20 52 65 74 75 72  in-place.  Retur
c3990 6e 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 20 6c  n the new name l
c39a0 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ength..**.** The
c39b0 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e 61   original filena
c39c0 6d 65 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e 2d  me is in z[0..n-
c39d0 31 5d 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  1].  Return the 
c39e0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61  number of.** cha
c39f0 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 73  racters in the s
c3a00 69 6d 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e 0a  implified name..
c3a10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 78  */.static int vx
c3a20 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d  worksSimplifyNam
c3a30 65 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  e(char *z, int n
c3a40 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ){.  int i, j;. 
c3a50 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a   while( n>1 && z
c3a60 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d  [n-1]=='/' ){ n-
c3a70 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 30  -; }.  for(i=j=0
c3a80 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<n; i++){.   
c3a90 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20 29   if( z[i]=='/' )
c3aa0 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b  {.      if( z[i+
c3ab0 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e  1]=='/' ) contin
c3ac0 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  ue;.      if( z[
c3ad0 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 32  i+1]=='.' && i+2
c3ae0 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2f  <n && z[i+2]=='/
c3af0 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b  ' ){.        i +
c3b00 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e  = 1;.        con
c3b10 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  tinue;.      }. 
c3b20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d       if( z[i+1]=
c3b30 3d 27 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26 26  ='.' && i+3<n &&
c3b40 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26 20   z[i+2]=='.' && 
c3b50 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20  z[i+3]=='/' ){. 
c3b60 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3e         while( j>
c3b70 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f 27  0 && z[j-1]!='/'
c3b80 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20   ){ j--; }.     
c3b90 20 20 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a 2d     if( j>0 ){ j-
c3ba0 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 20 2b  -; }.        i +
c3bb0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e  = 2;.        con
c3bc0 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20  tinue;.      }. 
c3bd0 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d 20     }.    z[j++] 
c3be0 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a 5b  = z[i];.  }.  z[
c3bf0 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  j] = 0;.  return
c3c00 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e   j;.}../*.** Fin
c3c10 64 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20  d a unique file 
c3c20 49 44 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  ID for the given
c3c30 20 61 62 73 6f 6c 75 74 65 20 70 61 74 68 6e 61   absolute pathna
c3c40 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61  me.  Return.** a
c3c50 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
c3c60 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62  vxworksFileId ob
c3c70 6a 65 63 74 2e 20 20 54 68 69 73 20 70 6f 69 6e  ject.  This poin
c3c80 74 65 72 20 69 73 20 74 68 65 20 75 6e 69 71 75  ter is the uniqu
c3c90 65 0a 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a 2a  e.** file ID..**
c3ca0 0a 2a 2a 20 54 68 65 20 6e 52 65 66 20 66 69 65  .** The nRef fie
c3cb0 6c 64 20 6f 66 20 74 68 65 20 76 78 77 6f 72 6b  ld of the vxwork
c3cc0 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69  sFileId object i
c3cd0 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 65  s incremented be
c3ce0 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65  fore.** the obje
c3cf0 63 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ct is returned. 
c3d00 20 41 20 6e 65 77 20 76 78 77 6f 72 6b 73 46 69   A new vxworksFi
c3d10 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20 63  leId object is c
c3d20 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61 64  reated.** and ad
c3d30 64 65 64 20 74 6f 20 74 68 65 20 67 6c 6f 62 61  ded to the globa
c3d40 6c 20 6c 69 73 74 20 69 66 20 6e 65 63 65 73 73  l list if necess
c3d50 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ary..**.** If a 
c3d60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
c3d70 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
c3d80 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a  return NULL..*/.
c3d90 73 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78  static struct vx
c3da0 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77  worksFileId *vxw
c3db0 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 63  orksFindFileId(c
c3dc0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 62 73 6f  onst char *zAbso
c3dd0 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74 72  luteName){.  str
c3de0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
c3df0 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20  d *pNew;        
c3e00 20 2f 2a 20 73 65 61 72 63 68 20 6b 65 79 20 61   /* search key a
c3e10 6e 64 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a  nd new file ID *
c3e20 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72  /.  struct vxwor
c3e30 6b 73 46 69 6c 65 49 64 20 2a 70 43 61 6e 64 69  ksFileId *pCandi
c3e40 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20 6c  date;   /* For l
c3e50 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73  ooping over exis
c3e60 74 69 6e 67 20 66 69 6c 65 20 49 44 73 20 2a 2f  ting file IDs */
c3e70 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20  .  int n;       
c3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3e90 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
c3ea0 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d   of zAbsoluteNam
c3eb0 65 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 61  e string */..  a
c3ec0 73 73 65 72 74 28 20 7a 41 62 73 6f 6c 75 74 65  ssert( zAbsolute
c3ed0 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0a  Name[0]=='/' );.
c3ee0 20 20 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65    n = (int)strle
c3ef0 6e 28 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29  n(zAbsoluteName)
c3f00 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
c3f10 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  e3_malloc( sizeo
c3f20 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31 29  f(*pNew) + (n+1)
c3f30 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d   );.  if( pNew==
c3f40 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
c3f50 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c  pNew->zCanonical
c3f60 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70  Name = (char*)&p
c3f70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79  New[1];.  memcpy
c3f80 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61  (pNew->zCanonica
c3f90 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74 65  lName, zAbsolute
c3fa0 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e 20  Name, n+1);.  n 
c3fb0 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66  = vxworksSimplif
c3fc0 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61 6e  yName(pNew->zCan
c3fd0 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b 0a  onicalName, n);.
c3fe0 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72  .  /* Search for
c3ff0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74   an existing ent
c4000 72 79 20 74 68 61 74 20 6d 61 74 63 68 69 6e 67  ry that matching
c4010 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 6e   the canonical n
c4020 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f 75  ame..  ** If fou
c4030 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68  nd, increment th
c4040 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
c4050 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70  t and return a p
c4060 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20 74  ointer to.  ** t
c4070 68 65 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65  he existing file
c4080 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 78   ID..  */.  unix
c4090 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
c40a0 66 6f 72 28 70 43 61 6e 64 69 64 61 74 65 3d 76  for(pCandidate=v
c40b0 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20  xworksFileList; 
c40c0 70 43 61 6e 64 69 64 61 74 65 3b 20 70 43 61 6e  pCandidate; pCan
c40d0 64 69 64 61 74 65 3d 70 43 61 6e 64 69 64 61 74  didate=pCandidat
c40e0 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  e->pNext){.    i
c40f0 66 28 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e  f( pCandidate->n
c4100 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26 26  Name==n .     &&
c4110 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69 64 61   memcmp(pCandida
c4120 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61  te->zCanonicalNa
c4130 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e  me, pNew->zCanon
c4140 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30 0a  icalName, n)==0.
c4150 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 73 71      ){.       sq
c4160 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29  lite3_free(pNew)
c4170 3b 0a 20 20 20 20 20 20 20 70 43 61 6e 64 69 64  ;.       pCandid
c4180 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20  ate->nRef++;.   
c4190 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74      unixLeaveMut
c41a0 65 78 28 29 3b 0a 20 20 20 20 20 20 20 72 65 74  ex();.       ret
c41b0 75 72 6e 20 70 43 61 6e 64 69 64 61 74 65 3b 0a  urn pCandidate;.
c41c0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
c41d0 4e 6f 20 6d 61 74 63 68 20 77 61 73 20 66 6f 75  No match was fou
c41e0 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61 6b  nd.  We will mak
c41f0 65 20 61 20 6e 65 77 20 66 69 6c 65 20 49 44 20  e a new file ID 
c4200 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20  */.  pNew->nRef 
c4210 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e 61  = 1;.  pNew->nNa
c4220 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d 3e  me = n;.  pNew->
c4230 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 46  pNext = vxworksF
c4240 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f 72  ileList;.  vxwor
c4250 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e 65  ksFileList = pNe
c4260 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  w;.  unixLeaveMu
c4270 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20  tex();.  return 
c4280 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  pNew;.}../*.** D
c4290 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66  ecrement the ref
c42a0 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20  erence count on 
c42b0 61 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  a vxworksFileId 
c42c0 6f 62 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a 2a  object.  Free.**
c42d0 20 74 68 65 20 6f 62 6a 65 63 74 20 77 68 65 6e   the object when
c42e0 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
c42f0 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72  ount reaches zer
c4300 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  o..*/.static voi
c4310 64 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65  d vxworksRelease
c4320 46 69 6c 65 49 64 28 73 74 72 75 63 74 20 76 78  FileId(struct vx
c4330 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64  worksFileId *pId
c4340 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  ){.  unixEnterMu
c4350 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28  tex();.  assert(
c4360 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a   pId->nRef>0 );.
c4370 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a 20    pId->nRef--;. 
c4380 20 69 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d 3d   if( pId->nRef==
c4390 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  0 ){.    struct 
c43a0 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 2a  vxworksFileId **
c43b0 70 70 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d 26  pp;.    for(pp=&
c43c0 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b  vxworksFileList;
c43d0 20 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 49 64   *pp && *pp!=pId
c43e0 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 29 2d 3e  ; pp = &((*pp)->
c43f0 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20 61 73  pNext)){}.    as
c4400 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 20 29  sert( *pp==pId )
c4410 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 49 64 2d  ;.    *pp = pId-
c4420 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69  >pNext;.    sqli
c4430 74 65 33 5f 66 72 65 65 28 70 49 64 29 3b 0a 20  te3_free(pId);. 
c4440 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75   }.  unixLeaveMu
c4450 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  tex();.}.#endif 
c4460 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f  /* OS_VXWORKS */
c4470 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c4480 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71 75 65 20  * End of Unique 
c4490 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79 20  File ID Utility 
c44a0 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73 20  Used By VxWorks 
c44b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c44c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
c44d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c44e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c44f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c4510 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
c4520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c4560 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
c4570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 50  ************** P
c4580 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f  osix Advisory Lo
c4590 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cking **********
c45a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c45b0 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 61  **.**.** POSIX a
c45c0 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 72  dvisory locks ar
c45d0 65 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 69  e broken by desi
c45e0 67 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 31 30  gn.  ANSI STD 10
c45f0 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20 73  03.1 (1996).** s
c4600 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 6c  ection 6.5.2.2 l
c4610 69 6e 65 73 20 34 38 33 20 74 68 72 6f 75 67 68  ines 483 through
c4620 20 34 39 30 20 73 70 65 63 69 66 79 20 74 68 61   490 specify tha
c4630 74 20 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73  t when a process
c4640 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c 65 61  .** sets or clea
c4650 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74 20  rs a lock, that 
c4660 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 72 69  operation overri
c4670 64 65 73 20 61 6e 79 20 70 72 69 6f 72 20 6c 6f  des any prior lo
c4680 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20 74 68  cks set.** by th
c4690 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20  e same process. 
c46a0 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 70   It does not exp
c46b0 6c 69 63 69 74 6c 79 20 73 61 79 20 73 6f 2c 20  licitly say so, 
c46c0 62 75 74 20 74 68 69 73 20 69 6d 70 6c 69 65 73  but this implies
c46d0 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f 76 65 72  .** that it over
c46e0 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 65 74 20  rides locks set 
c46f0 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63  by the same proc
c4700 65 73 73 20 75 73 69 6e 67 20 61 20 64 69 66 66  ess using a diff
c4710 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65  erent.** file de
c4720 73 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73 69  scriptor.  Consi
c4730 64 65 72 20 74 68 69 73 20 74 65 73 74 20 63 61  der this test ca
c4740 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  se:.**.**       
c4750 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e 28 22  int fd1 = open("
c4760 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57 52  ./file1", O_RDWR
c4770 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b  |O_CREAT, 0644);
c4780 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64  .**       int fd
c4790 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65  2 = open("./file
c47a0 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45  2", O_RDWR|O_CRE
c47b0 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a  AT, 0644);.**.**
c47c0 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31   Suppose ./file1
c47d0 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72 65   and ./file2 are
c47e0 20 72 65 61 6c 6c 79 20 74 68 65 20 73 61 6d 65   really the same
c47f0 20 66 69 6c 65 20 28 62 65 63 61 75 73 65 0a 2a   file (because.*
c4800 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 72 64 20  * one is a hard 
c4810 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b  or symbolic link
c4820 20 74 6f 20 74 68 65 20 6f 74 68 65 72 29 20 74   to the other) t
c4830 68 65 6e 20 69 66 20 79 6f 75 20 73 65 74 0a 2a  hen if you set.*
c4840 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  * an exclusive l
c4850 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e  ock on fd1, then
c4860 20 74 72 79 20 74 6f 20 67 65 74 20 61 6e 20 65   try to get an e
c4870 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a  xclusive lock.**
c4880 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f 72 6b   on fd2, it work
c4890 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 61 76 65  s.  I would have
c48a0 20 65 78 70 65 63 74 65 64 20 74 68 65 20 73 65   expected the se
c48b0 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20  cond lock to.** 
c48c0 66 61 69 6c 20 73 69 6e 63 65 20 74 68 65 72 65  fail since there
c48d0 20 77 61 73 20 61 6c 72 65 61 64 79 20 61 20 6c   was already a l
c48e0 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20  ock on the file 
c48f0 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42  due to fd1..** B
c4900 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63  ut not so.  Sinc
c4910 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d  e both locks cam
c4920 65 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20  e from the same 
c4930 70 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20  process, the.** 
c4940 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 73  second overrides
c4950 20 74 68 65 20 66 69 72 73 74 2c 20 65 76 65 6e   the first, even
c4960 20 74 68 6f 75 67 68 20 74 68 65 79 20 77 65 72   though they wer
c4970 65 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a  e on different.*
c4980 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * file descripto
c4990 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66  rs opened on dif
c49a0 66 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65  ferent file name
c49b0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65  s..**.** This me
c49c0 61 6e 73 20 74 68 61 74 20 77 65 20 63 61 6e 6e  ans that we cann
c49d0 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c 6f 63  ot use POSIX loc
c49e0 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a  ks to synchroniz
c49f0 65 20 66 69 6c 65 20 61 63 63 65 73 73 0a 2a 2a  e file access.**
c4a00 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e 67   among competing
c4a10 20 74 68 72 65 61 64 73 20 6f 66 20 74 68 65 20   threads of the 
c4a20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 50  same process.  P
c4a30 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c 20  OSIX locks will 
c4a40 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f 20  work fine.** to 
c4a50 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63 65  synchronize acce
c4a60 73 73 20 66 6f 72 20 74 68 72 65 61 64 73 20 69  ss for threads i
c4a70 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65  n separate proce
c4a80 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a 2a  sses, but not.**
c4a90 20 74 68 72 65 61 64 73 20 77 69 74 68 69 6e 20   threads within 
c4aa0 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
c4ab0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20  ..**.** To work 
c4ac0 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f 62 6c  around the probl
c4ad0 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 20 74  em, SQLite has t
c4ae0 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c 6f  o manage file lo
c4af0 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a  cks internally.*
c4b00 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20 57  * on its own.  W
c4b10 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 64 61  henever a new da
c4b20 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
c4b30 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66 69 6e  , we have to fin
c4b40 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69  d the.** specifi
c4b50 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 20 64  c inode of the d
c4b60 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 74 68  atabase file (th
c4b70 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74 65 72  e inode is deter
c4b80 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  mined by the.** 
c4b90 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f 69 6e  st_dev and st_in
c4ba0 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  o fields of the 
c4bb0 73 74 61 74 20 73 74 72 75 63 74 75 72 65 20 74  stat structure t
c4bc0 68 61 74 20 66 73 74 61 74 28 29 20 66 69 6c 6c  hat fstat() fill
c4bd0 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 65  s in).** and che
c4be0 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c 72  ck for locks alr
c4bf0 65 61 64 79 20 65 78 69 73 74 69 6e 67 20 6f 6e  eady existing on
c4c00 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 57 68   that inode.  Wh
c4c10 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20  en locks are.** 
c4c20 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d 6f 76  created or remov
c4c30 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6c  ed, we have to l
c4c40 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20 69  ook at our own i
c4c50 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f  nternal record o
c4c60 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 74  f the.** locks t
c4c70 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72  o see if another
c4c80 20 74 68 72 65 61 64 20 68 61 73 20 70 72 65 76   thread has prev
c4c90 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c 6f 63  iously set a loc
c4ca0 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a 2a  k on that same.*
c4cb0 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 28  * inode..**.** (
c4cc0 41 73 69 64 65 3a 20 54 68 65 20 75 73 65 20 6f  Aside: The use o
c4cd0 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20  f inode numbers 
c4ce0 61 73 20 75 6e 69 71 75 65 20 49 44 73 20 64 6f  as unique IDs do
c4cf0 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56  es not work on V
c4d00 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20 56  xWorks..** For V
c4d10 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 65 20  xWorks, we have 
c4d20 74 6f 20 75 73 65 20 74 68 65 20 61 6c 74 65 72  to use the alter
c4d30 6e 61 74 69 76 65 20 75 6e 69 71 75 65 20 49 44  native unique ID
c4d40 20 73 79 73 74 65 6d 20 62 61 73 65 64 20 6f 6e   system based on
c4d50 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69  .** canonical fi
c4d60 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c 65  lename and imple
c4d70 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 70 72  mented in the pr
c4d80 65 76 69 6f 75 73 20 64 69 76 69 73 69 6f 6e 2e  evious division.
c4d90 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ).**.** The sqli
c4da0 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75  te3_file structu
c4db0 72 65 20 66 6f 72 20 50 4f 53 49 58 20 69 73 20  re for POSIX is 
c4dc0 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61  no longer just a
c4dd0 6e 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a  n integer file.*
c4de0 2a 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 49  * descriptor.  I
c4df0 74 20 69 73 20 6e 6f 77 20 61 20 73 74 72 75 63  t is now a struc
c4e00 74 75 72 65 20 74 68 61 74 20 68 6f 6c 64 73 20  ture that holds 
c4e10 74 68 65 20 69 6e 74 65 67 65 72 20 66 69 6c 65  the integer file
c4e20 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 61  .** descriptor a
c4e30 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  nd a pointer to 
c4e40 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  a structure that
c4e50 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 69   describes the i
c4e60 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73  nternal.** locks
c4e70 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f   on the correspo
c4e80 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68  nding inode.  Th
c4e90 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69  ere is one locki
c4ea0 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ng structure.** 
c4eb0 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66  per inode, so if
c4ec0 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20   the same inode 
c4ed0 69 73 20 6f 70 65 6e 65 64 20 74 77 69 63 65 2c  is opened twice,
c4ee0 20 62 6f 74 68 20 75 6e 69 78 46 69 6c 65 20 73   both unixFile s
c4ef0 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69  tructures.** poi
c4f00 6e 74 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6c  nt to the same l
c4f10 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65  ocking structure
c4f20 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73  .  The locking s
c4f30 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 0a 2a  tructure keeps.*
c4f40 2a 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f  * a reference co
c4f50 75 6e 74 20 28 73 6f 20 77 65 20 77 69 6c 6c 20  unt (so we will 
c4f60 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64 65 6c  know when to del
c4f70 65 74 65 20 69 74 29 20 61 6e 64 20 61 20 22 63  ete it) and a "c
c4f80 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61  nt".** field tha
c4f90 74 20 74 65 6c 6c 73 20 75 73 20 69 74 73 20 69  t tells us its i
c4fa0 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61  nternal lock sta
c4fb0 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61  tus.  cnt==0 mea
c4fc0 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69  ns the.** file i
c4fd0 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74  s unlocked.  cnt
c4fe0 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65 20 66  ==-1 means the f
c4ff0 69 6c 65 20 68 61 73 20 61 6e 20 65 78 63 6c 75  ile has an exclu
c5000 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e  sive lock..** cn
c5010 74 3e 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20  t>0 means there 
c5020 61 72 65 20 63 6e 74 20 73 68 61 72 65 64 20 6c  are cnt shared l
c5030 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c 65  ocks on the file
c5040 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65  ..**.** Any atte
c5050 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75  mpt to lock or u
c5060 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 72  nlock a file fir
c5070 73 74 20 63 68 65 63 6b 73 20 74 68 65 20 6c 6f  st checks the lo
c5080 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75  cking.** structu
c5090 72 65 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 29  re.  The fcntl()
c50a0 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 20   system call is 
c50b0 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20  only invoked to 
c50c0 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20  set a .** POSIX 
c50d0 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 6e 74 65  lock if the inte
c50e0 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74  rnal lock struct
c50f0 75 72 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  ure transitions 
c5100 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63  between.** a loc
c5110 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63  ked and an unloc
c5120 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  ked state..**.**
c5130 20 42 75 74 20 77 61 69 74 3a 20 20 74 68 65 72   But wait:  ther
c5140 65 20 61 72 65 20 79 65 74 20 6d 6f 72 65 20 70  e are yet more p
c5150 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 50 4f 53  roblems with POS
c5160 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b  IX advisory lock
c5170 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20  s..**.** If you 
c5180 63 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73  close a file des
c5190 63 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69  criptor that poi
c51a0 6e 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68  nts to a file th
c51b0 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a  at has locks,.**
c51c0 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68   all locks on th
c51d0 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65  at file that are
c51e0 20 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63 75   owned by the cu
c51f0 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72  rrent process ar
c5200 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20  e.** released.  
c5210 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74  To work around t
c5220 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63  his problem, eac
c5230 68 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63  h unixFile struc
c5240 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  ture contains.**
c5250 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
c5260 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72   unixOpenCnt str
c5270 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 69  ucture.  There i
c5280 73 20 6f 6e 65 20 75 6e 69 78 4f 70 65 6e 43 6e  s one unixOpenCn
c5290 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70  t structure.** p
c52a0 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20 77  er open inode, w
c52b0 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20  hich means that 
c52c0 6d 75 6c 74 69 70 6c 65 20 75 6e 69 78 46 69 6c  multiple unixFil
c52d0 65 20 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20 61  e can point to a
c52e0 20 73 69 6e 67 6c 65 0a 2a 2a 20 75 6e 69 78 4f   single.** unixO
c52f0 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e  penCnt.  When an
c5300 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
c5310 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69   to close an uni
c5320 78 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20  xFile, if there 
c5330 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69  are.** other uni
c5340 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68  xFile open on th
c5350 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61  e same inode tha
c5360 74 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f  t are holding lo
c5370 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a  cks, the call.**
c5380 20 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20   to close() the 
c5390 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
c53a0 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69  is deferred unti
c53b0 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63  l all of the loc
c53c0 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65  ks clear..** The
c53d0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72   unixOpenCnt str
c53e0 75 63 74 75 72 65 20 6b 65 65 70 73 20 61 20 6c  ucture keeps a l
c53f0 69 73 74 20 6f 66 20 66 69 6c 65 20 64 65 73 63  ist of file desc
c5400 72 69 70 74 6f 72 73 20 74 68 61 74 20 6e 65 65  riptors that nee
c5410 64 20 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 65  d to.** be close
c5420 64 20 61 6e 64 20 74 68 61 74 20 6c 69 73 74 20  d and that list 
c5430 69 73 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 63  is walked (and c
c5440 6c 65 61 72 65 64 29 20 77 68 65 6e 20 74 68 65  leared) when the
c5450 20 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c   last lock.** cl
c5460 65 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 65 74 20  ears..**.** Yet 
c5470 61 6e 6f 74 68 65 72 20 70 72 6f 62 6c 65 6d 3a  another problem:
c5480 20 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 64    LinuxThreads d
c5490 6f 20 6e 6f 74 20 70 6c 61 79 20 77 65 6c 6c 20  o not play well 
c54a0 77 69 74 68 20 70 6f 73 69 78 20 6c 6f 63 6b 73  with posix locks
c54b0 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 6f 6c 64  ..**.** Many old
c54c0 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 6c  er versions of l
c54d0 69 6e 75 78 20 75 73 65 20 74 68 65 20 4c 69 6e  inux use the Lin
c54e0 75 78 54 68 72 65 61 64 73 20 6c 69 62 72 61 72  uxThreads librar
c54f0 79 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6e 6f  y which is.** no
c5500 74 20 70 6f 73 69 78 20 63 6f 6d 70 6c 69 61 6e  t posix complian
c5510 74 2e 20 20 55 6e 64 65 72 20 4c 69 6e 75 78 54  t.  Under LinuxT
c5520 68 72 65 61 64 73 2c 20 61 20 6c 6f 63 6b 20 63  hreads, a lock c
c5530 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64  reated by thread
c5540 0a 2a 2a 20 41 20 63 61 6e 6e 6f 74 20 62 65 20  .** A cannot be 
c5550 6d 6f 64 69 66 69 65 64 20 6f 72 20 6f 76 65 72  modified or over
c5560 72 69 64 64 65 6e 20 62 79 20 61 20 64 69 66 66  ridden by a diff
c5570 65 72 65 6e 74 20 74 68 72 65 61 64 20 42 2e 0a  erent thread B..
c5580 2a 2a 20 4f 6e 6c 79 20 74 68 72 65 61 64 20 41  ** Only thread A
c5590 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 20   can modify the 
c55a0 6c 6f 63 6b 2e 20 20 4c 6f 63 6b 69 6e 67 20 62  lock.  Locking b
c55b0 65 68 61 76 69 6f 72 20 69 73 20 63 6f 72 72 65  ehavior is corre
c55c0 63 74 0a 2a 2a 20 69 66 20 74 68 65 20 61 70 70  ct.** if the app
c55d0 6c 69 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65  liation uses the
c55e0 20 6e 65 77 65 72 20 4e 61 74 69 76 65 20 50 6f   newer Native Po
c55f0 73 69 78 20 54 68 72 65 61 64 20 4c 69 62 72 61  six Thread Libra
c5600 72 79 20 28 4e 50 54 4c 29 0a 2a 2a 20 6f 6e 20  ry (NPTL).** on 
c5610 6c 69 6e 75 78 20 2d 20 77 69 74 68 20 4e 50 54  linux - with NPT
c5620 4c 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64  L a lock created
c5630 20 62 79 20 74 68 72 65 61 64 20 41 20 63 61 6e   by thread A can
c5640 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 0a   override locks.
c5650 2a 2a 20 69 6e 20 74 68 72 65 61 64 20 42 2e 20  ** in thread B. 
c5660 20 42 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f   But there is no
c5670 20 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 20   way to know at 
c5680 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 69  compile-time whi
c5690 63 68 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67 20  ch.** threading 
c56a0 6c 69 62 72 61 72 79 20 69 73 20 62 65 69 6e 67  library is being
c56b0 20 75 73 65 64 2e 20 20 53 6f 20 74 68 65 72 65   used.  So there
c56c0 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e   is no way to kn
c56d0 6f 77 20 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65  ow at.** compile
c56e0 2d 74 69 6d 65 20 77 68 65 74 68 65 72 20 6f 72  -time whether or
c56f0 20 6e 6f 74 20 74 68 72 65 61 64 20 41 20 63 61   not thread A ca
c5700 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73  n override locks
c5710 20 6f 6e 20 74 68 72 65 61 64 20 42 2e 0a 2a 2a   on thread B..**
c5720 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 61   We have to do a
c5730 20 72 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b 20   run-time check 
c5740 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20  to discover the 
c5750 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 0a  behavior of the.
c5760 2a 2a 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65  ** current proce
c5770 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 79 73  ss..**.** On sys
c5780 74 65 6d 73 20 77 68 65 72 65 20 74 68 72 65 61  tems where threa
c5790 64 20 41 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  d A is unable to
c57a0 20 6d 6f 64 69 66 79 20 6c 6f 63 6b 73 20 63 72   modify locks cr
c57b0 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 72 65  eated by.** thre
c57c0 61 64 20 42 2c 20 77 65 20 68 61 76 65 20 74 6f  ad B, we have to
c57d0 20 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 77   keep track of w
c57e0 68 69 63 68 20 74 68 72 65 61 64 20 63 72 65 61  hich thread crea
c57f0 74 65 64 20 65 61 63 68 0a 2a 2a 20 6c 6f 63 6b  ted each.** lock
c5800 2e 20 20 48 65 6e 63 65 20 74 68 65 72 65 20 69  .  Hence there i
c5810 73 20 61 6e 20 65 78 74 72 61 20 66 69 65 6c 64  s an extra field
c5820 20 69 6e 20 74 68 65 20 6b 65 79 20 74 6f 20 74   in the key to t
c5830 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 0a  he unixLockInfo.
c5840 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  ** structure to 
c5850 72 65 63 6f 72 64 20 74 68 69 73 20 69 6e 66 6f  record this info
c5860 72 6d 61 74 69 6f 6e 2e 20 20 41 6e 64 20 6f 6e  rmation.  And on
c5870 20 74 68 6f 73 65 20 73 79 73 74 65 6d 73 20 69   those systems i
c5880 74 0a 2a 2a 20 69 73 20 69 6c 6c 65 67 61 6c 20  t.** is illegal 
c5890 74 6f 20 62 65 67 69 6e 20 61 20 74 72 61 6e 73  to begin a trans
c58a0 61 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 68  action in one th
c58b0 72 65 61 64 20 61 6e 64 20 66 69 6e 69 73 68 20  read and finish 
c58c0 69 74 0a 2a 2a 20 69 6e 20 61 6e 6f 74 68 65 72  it.** in another
c58d0 2e 20 20 46 6f 72 20 74 68 69 73 20 6c 61 74 74  .  For this latt
c58e0 65 72 20 72 65 73 74 72 69 63 74 69 6f 6e 2c 20  er restriction, 
c58f0 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b  there is no work
c5900 2d 61 72 6f 75 6e 64 2e 0a 2a 2a 20 49 74 20 69  -around..** It i
c5910 73 20 61 20 6c 69 6d 69 74 61 74 69 6f 6e 20 6f  s a limitation o
c5920 66 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a  f LinuxThreads..
c5930 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72  */../*.** Set or
c5940 20 63 68 65 63 6b 20 74 68 65 20 75 6e 69 78 46   check the unixF
c5950 69 6c 65 2e 74 69 64 20 66 69 65 6c 64 2e 20 20  ile.tid field.  
c5960 54 68 69 73 20 66 69 65 6c 64 20 69 73 20 73 65  This field is se
c5970 74 20 77 68 65 6e 20 61 6e 20 75 6e 69 78 46 69  t when an unixFi
c5980 6c 65 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 6f  le.** is first o
c5990 70 65 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62 73  pened.  All subs
c59a0 65 71 75 65 6e 74 20 75 73 65 73 20 6f 66 20 74  equent uses of t
c59b0 68 65 20 75 6e 69 78 46 69 6c 65 20 76 65 72 69  he unixFile veri
c59c0 66 79 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73  fy that the.** s
c59d0 61 6d 65 20 74 68 72 65 61 64 20 69 73 20 6f 70  ame thread is op
c59e0 65 72 61 74 69 6e 67 20 6f 6e 20 74 68 65 20 75  erating on the u
c59f0 6e 69 78 46 69 6c 65 2e 20 20 53 6f 6d 65 20 6f  nixFile.  Some o
c5a00 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73  perating systems
c5a10 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61 6c 6c 6f 77   do.** not allow
c5a20 20 6c 6f 63 6b 73 20 74 6f 20 62 65 20 6f 76 65   locks to be ove
c5a30 72 72 69 64 64 65 6e 20 62 79 20 6f 74 68 65 72  rridden by other
c5a40 20 74 68 72 65 61 64 73 20 61 6e 64 20 74 68 61   threads and tha
c5a50 74 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a  t restriction.**
c5a60 20 6d 65 61 6e 73 20 74 68 61 74 20 73 71 6c 69   means that sqli
c5a70 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 61  te3* database ha
c5a80 6e 64 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20  ndles cannot be 
c5a90 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74  moved from one t
c5aa0 68 72 65 61 64 0a 2a 2a 20 74 6f 20 61 6e 6f 74  hread.** to anot
c5ab0 68 65 72 20 77 68 69 6c 65 20 6c 6f 63 6b 73 20  her while locks 
c5ac0 61 72 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20  are held..**.** 
c5ad0 56 65 72 73 69 6f 6e 20 33 2e 33 2e 31 20 28 32  Version 3.3.1 (2
c5ae0 30 30 36 2d 30 31 2d 31 35 29 3a 20 20 75 6e 69  006-01-15):  uni
c5af0 78 46 69 6c 65 20 63 61 6e 20 62 65 20 6d 6f 76  xFile can be mov
c5b00 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65  ed from one thre
c5b10 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72  ad to.** another
c5b20 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 61   as long as we a
c5b30 72 65 20 72 75 6e 6e 69 6e 67 20 6f 6e 20 61 20  re running on a 
c5b40 73 79 73 74 65 6d 20 74 68 61 74 20 73 75 70 70  system that supp
c5b50 6f 72 74 73 20 74 68 72 65 61 64 73 0a 2a 2a 20  orts threads.** 
c5b60 6f 76 65 72 72 69 64 69 6e 67 20 65 61 63 68 20  overriding each 
c5b70 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 77 68  others locks (wh
c5b80 69 63 68 20 69 73 20 6e 6f 77 20 74 68 65 20 6d  ich is now the m
c5b90 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61 76  ost common behav
c5ba0 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e 6f  ior).** or if no
c5bb0 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e   locks are held.
c5bc0 20 20 42 75 74 20 74 68 65 20 75 6e 69 78 46 69    But the unixFi
c5bd0 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20 6e  le.pLock field n
c5be0 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72 65  eeds to be.** re
c5bf0 63 6f 6d 70 75 74 65 64 20 62 65 63 61 75 73 65  computed because
c5c00 20 69 74 73 20 6b 65 79 20 69 6e 63 6c 75 64 65   its key include
c5c10 73 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 2e  s the thread-id.
c5c20 20 20 53 65 65 20 74 68 65 20 0a 2a 2a 20 74 72    See the .** tr
c5c30 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28  ansferOwnership(
c5c40 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f 77  ) function below
c5c50 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
c5c60 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a 23  information.*/.#
c5c70 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
c5c80 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28  SAFE && defined(
c5c90 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66  __linux__).# def
c5ca0 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49 44  ine SET_THREADID
c5cb0 28 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20 3d  (X)   (X)->tid =
c5cc0 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 0a   pthread_self().
c5cd0 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54  # define CHECK_T
c5ce0 48 52 45 41 44 49 44 28 58 29 20 28 74 68 72 65  HREADID(X) (thre
c5cf0 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
c5d00 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 26  thersLocks==0 &&
c5d10 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
c5d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21                 !
c5d30 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28 58  pthread_equal((X
c5d40 29 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64 5f  )->tid, pthread_
c5d50 73 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a 23  self())).#else.#
c5d60 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45   define SET_THRE
c5d70 41 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e 65  ADID(X).# define
c5d80 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28   CHECK_THREADID(
c5d90 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  X) 0.#endif../*.
c5da0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
c5db0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
c5dc0 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73  structure serves
c5dd0 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64   as the key used
c5de0 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20  .** to locate a 
c5df0 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4f  particular unixO
c5e00 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65  penCnt structure
c5e10 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65   given its inode
c5e20 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 74 68  .  This.** is th
c5e30 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 75 6e  e same as the un
c5e40 69 78 4c 6f 63 6b 4b 65 79 20 65 78 63 65 70 74  ixLockKey except
c5e50 20 74 68 61 74 20 74 68 65 20 74 68 72 65 61 64   that the thread
c5e60 20 49 44 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a   ID is omitted..
c5e70 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69  */.struct unixFi
c5e80 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f 74 20 64  leId {.  dev_t d
c5e90 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ev;             
c5ea0 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 6e       /* Device n
c5eb0 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53 5f  umber */.#if OS_
c5ec0 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74  VXWORKS.  struct
c5ed0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a   vxworksFileId *
c5ee0 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20  pId;  /* Unique 
c5ef0 66 69 6c 65 20 49 44 20 66 6f 72 20 76 78 77 6f  file ID for vxwo
c5f00 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20 20  rks. */.#else.  
c5f10 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20  ino_t ino;      
c5f20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
c5f30 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23  node number */.#
c5f40 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
c5f50 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
c5f60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
c5f70 75 63 74 75 72 65 20 73 65 72 76 65 73 20 61 73  ucture serves as
c5f80 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a   the key used.**
c5f90 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 72   to locate a par
c5fa0 74 69 63 75 6c 61 72 20 75 6e 69 78 4c 6f 63 6b  ticular unixLock
c5fb0 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 67  Info structure g
c5fc0 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 0a  iven its inode..
c5fd0 2a 2a 0a 2a 2a 20 49 66 20 74 68 72 65 61 64 73  **.** If threads
c5fe0 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65   cannot override
c5ff0 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
c6000 6b 73 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73  ks (LinuxThreads
c6010 29 2c 20 74 68 65 6e 20 77 65 0a 2a 2a 20 73 65  ), then we.** se
c6020 74 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65  t the unixLockKe
c6030 79 2e 74 69 64 20 66 69 65 6c 64 20 74 6f 20 74  y.tid field to t
c6040 68 65 20 74 68 72 65 61 64 20 49 44 2e 20 20 49  he thread ID.  I
c6050 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76  f threads can ov
c6060 65 72 72 69 64 65 0a 2a 2a 20 65 61 63 68 20 6f  erride.** each o
c6070 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 50 6f 73  thers locks (Pos
c6080 69 78 20 61 6e 64 20 4e 50 54 4c 29 20 74 68 65  ix and NPTL) the
c6090 6e 20 74 69 64 20 69 73 20 61 6c 77 61 79 73 20  n tid is always 
c60a0 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20  set to zero..** 
c60b0 74 69 64 20 69 73 20 6f 6d 69 74 74 65 64 20 69  tid is omitted i
c60c0 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74  f we compile wit
c60d0 68 6f 75 74 20 74 68 72 65 61 64 69 6e 67 20 73  hout threading s
c60e0 75 70 70 6f 72 74 20 6f 72 20 6f 6e 20 61 6e 20  upport or on an 
c60f0 4f 53 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e  OS.** other than
c6100 20 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 74 72 75 63   linux..*/.struc
c6110 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 7b 0a  t unixLockKey {.
c6120 20 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c    struct unixFil
c6130 65 49 64 20 66 69 64 3b 20 20 2f 2a 20 55 6e 69  eId fid;  /* Uni
c6140 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 66  que identifier f
c6150 6f 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 23  or the file */.#
c6160 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
c6170 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28  SAFE && defined(
c6180 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68  __linux__).  pth
c6190 72 65 61 64 5f 74 20 74 69 64 3b 20 20 2f 2a 20  read_t tid;  /* 
c61a0 54 68 72 65 61 64 20 49 44 20 6f 66 20 6c 6f 63  Thread ID of loc
c61b0 6b 20 6f 77 6e 65 72 2e 20 5a 65 72 6f 20 69 66  k owner. Zero if
c61c0 20 6e 6f 74 20 75 73 69 6e 67 20 4c 69 6e 75 78   not using Linux
c61d0 54 68 72 65 61 64 73 20 2a 2f 0a 23 65 6e 64 69  Threads */.#endi
c61e0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  f.};../*.** An i
c61f0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
c6200 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
c6210 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  re is allocated 
c6220 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a  for each open.**
c6230 20 69 6e 6f 64 65 2e 20 20 4f 72 2c 20 6f 6e 20   inode.  Or, on 
c6240 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 74 68  LinuxThreads, th
c6250 65 72 65 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  ere is one of th
c6260 65 73 65 20 73 74 72 75 63 74 75 72 65 73 20 66  ese structures f
c6270 6f 72 0a 2a 2a 20 65 61 63 68 20 69 6e 6f 64 65  or.** each inode
c6280 20 6f 70 65 6e 65 64 20 62 79 20 65 61 63 68 20   opened by each 
c6290 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20  thread..**.** A 
c62a0 73 69 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61 6e  single inode can
c62b0 20 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 66   have multiple f
c62c0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c  ile descriptors,
c62d0 20 73 6f 20 65 61 63 68 20 75 6e 69 78 46 69 6c   so each unixFil
c62e0 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63  e.** structure c
c62f0 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
c6300 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
c6310 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20   of this object 
c6320 61 6e 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65  and this.** obje
c6330 63 74 20 6b 65 65 70 73 20 61 20 63 6f 75 6e 74  ct keeps a count
c6340 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
c6350 66 20 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e 74  f unixFile point
c6360 69 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73 74  ing to it..*/.st
c6370 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66  ruct unixLockInf
c6380 6f 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69  o {.  struct uni
c6390 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 79  xLockKey lockKey
c63a0 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f  ;     /* The loo
c63b0 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74  kup key */.  int
c63c0 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20   cnt;           
c63d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c63e0 4e 75 6d 62 65 72 20 6f 66 20 53 48 41 52 45 44  Number of SHARED
c63f0 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20   locks held */. 
c6400 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20   int locktype;  
c6410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6420 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52 45   /* One of SHARE
c6430 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45 44  D_LOCK, RESERVED
c6440 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20 20  _LOCK etc. */.  
c6450 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
c6460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6470 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69  /* Number of poi
c6480 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74  nters to this st
c6490 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72  ructure */.  str
c64a0 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  uct unixLockInfo
c64b0 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a 20   *pNext;     /* 
c64c0 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78  List of all unix
c64d0 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 73  LockInfo objects
c64e0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
c64f0 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 50 72 65 76  xLockInfo *pPrev
c6500 3b 20 20 20 20 20 2f 2a 20 20 20 20 2e 2e 2e 2e  ;     /*    ....
c6510 20 64 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20 2a   doubly linked *
c6520 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
c6530 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
c6540 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
c6550 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  re is allocated 
c6560 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a  for each open.**
c6570 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74   inode.  This st
c6580 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
c6590 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ack of the numbe
c65a0 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68  r of locks on th
c65b0 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66  at.** inode.  If
c65c0 20 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65   a close is atte
c65d0 6d 70 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e  mpted against an
c65e0 20 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68   inode that is h
c65f0 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c  olding.** locks,
c6600 20 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65   the close is de
c6610 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c  ferred until all
c6620 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20   locks clear by 
c6630 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69  adding the.** fi
c6640 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
c6650 20 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68   be closed to th
c6660 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a  e pending list..
c6670 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 20 43 6f 6e  **.** TODO:  Con
c6680 73 69 64 65 72 20 63 68 61 6e 67 69 6e 67 20 74  sider changing t
c6690 68 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 72  his so that ther
c66a0 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67  e is only a sing
c66b0 6c 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72  le file.** descr
c66c0 69 70 74 6f 72 20 66 6f 72 20 65 61 63 68 20 6f  iptor for each o
c66d0 70 65 6e 20 66 69 6c 65 2c 20 65 76 65 6e 20 77  pen file, even w
c66e0 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64  hen it is opened
c66f0 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e   multiple times.
c6700 0a 2a 2a 20 54 68 65 20 63 6c 6f 73 65 28 29 20  .** The close() 
c6710 73 79 73 74 65 6d 20 63 61 6c 6c 20 77 6f 75 6c  system call woul
c6720 64 20 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68 65  d only occur whe
c6730 6e 20 74 68 65 20 6c 61 73 74 20 64 61 74 61 62  n the last datab
c6740 61 73 65 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  ase.** using the
c6750 20 66 69 6c 65 20 63 6c 6f 73 65 73 2e 0a 2a 2f   file closes..*/
c6760 0a 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e  .struct unixOpen
c6770 43 6e 74 20 7b 0a 20 20 73 74 72 75 63 74 20 75  Cnt {.  struct u
c6780 6e 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64  nixFileId fileId
c6790 3b 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75  ;   /* The looku
c67a0 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  p key */.  int n
c67b0 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
c67c0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
c67d0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
c67e0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
c67f0 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20  /.  int nLock;  
c6800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6810 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74  /* Number of out
c6820 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 2a  standing locks *
c6830 2f 0a 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e 67  /.  int nPending
c6840 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c6850 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 65 6e  /* Number of pen
c6860 64 69 6e 67 20 63 6c 6f 73 65 28 29 20 6f 70 65  ding close() ope
c6870 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  rations */.  int
c6880 20 2a 61 50 65 6e 64 69 6e 67 3b 20 20 20 20 20   *aPending;     
c6890 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63         /* Malloc
c68a0 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69 6e 67  ed space holding
c68b0 20 66 64 27 73 20 61 77 61 69 74 69 6e 67 20 61   fd's awaiting a
c68c0 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 23 69 66 20   close() */.#if 
c68d0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d  OS_VXWORKS.  sem
c68e0 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20  _t *pSem;       
c68f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c6900 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d   Named POSIX sem
c6910 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 72  aphore */.  char
c6920 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 41   aSemName[MAX_PA
c6930 54 48 4e 41 4d 45 2b 31 5d 3b 20 20 20 2f 2a 20  THNAME+1];   /* 
c6940 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65 6d  Name of that sem
c6950 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 66  aphore */.#endif
c6960 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70  .  struct unixOp
c6970 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a 70  enCnt *pNext, *p
c6980 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20  Prev;   /* List 
c6990 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 43  of all unixOpenC
c69a0 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b  nt objects */.};
c69b0 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f 66  ../*.** Lists of
c69c0 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66   all unixLockInf
c69d0 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e  o and unixOpenCn
c69e0 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 73  t objects.  Thes
c69f0 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 61 73  e used to be has
c6a00 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 75  h.** tables.  Bu
c6a10 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
c6a20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 65 6c  objects is rarel
c6a30 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f  y more than a do
c6a40 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 72  zen and.** never
c6a50 20 65 78 63 65 65 64 73 20 61 20 66 65 77 20 74   exceeds a few t
c6a60 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c 6f  housand.  And lo
c6a70 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 61  okup is not on a
c6a80 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 74   critical.** pat
c6a90 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c 69  h so a simple li
c6aa0 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 73  nked list will s
c6ab0 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 69  uffice..*/.stati
c6ac0 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63  c struct unixLoc
c6ad0 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20  kInfo *lockList 
c6ae0 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75  = 0;.static stru
c6af0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
c6b00 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f  openList = 0;../
c6b10 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62  *.** This variab
c6b20 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 68 65  le remembers whe
c6b30 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65  ther or not thre
c6b40 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65  ads can override
c6b50 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20   each others.** 
c6b60 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  locks..**.**    
c6b70 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73  0:  No.  Threads
c6b80 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65   cannot override
c6b90 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
c6ba0 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 61  ks.  (LinuxThrea
c6bb0 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65  ds).**    1:  Ye
c6bc0 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e 20  s.  Threads can 
c6bd0 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74  override each ot
c6be0 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 6f  hers locks.  (Po
c6bf0 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 20  six & NLPT).**  
c6c00 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b   -1:  We don't k
c6c10 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f  now yet..**.** O
c6c20 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20  n some systems, 
c6c30 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69  we know at compi
c6c40 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61  le-time if threa
c6c50 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20  ds can override 
c6c60 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c  each.** others l
c6c70 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20  ocks.  On those 
c6c80 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c  systems, the SQL
c6c90 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52  ITE_THREAD_OVERR
c6ca0 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a  IDE_LOCK macro.*
c6cb0 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70  * will be set ap
c6cc0 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e  propriately.  On
c6cd0 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20   other systems, 
c6ce0 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b  we have to check
c6cf0 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20   at.** runtime. 
c6d00 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72   On these latter
c6d10 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45   systems, SQLTIE
c6d20 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
c6d30 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65  _LOCK is.** unde
c6d40 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  fined..**.** Thi
c6d50 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61  s variable norma
c6d60 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f  lly has file sco
c6d70 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75  pe only.  But du
c6d80 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65  ring testing, we
c6d90 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c   make.** it a gl
c6da0 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65  obal so that the
c6db0 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63   test code can c
c6dc0 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20  hange its value 
c6dd0 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69  in order to veri
c6de0 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72  fy.** that the r
c6df0 69 67 68 74 20 73 74 75 66 66 20 68 61 70 70 65  ight stuff happe
c6e00 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 61 73  ns in either cas
c6e10 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  e..*/.#if SQLITE
c6e20 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
c6e30 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
c6e40 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 4c 49  ).#  ifndef SQLI
c6e50 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49  TE_THREAD_OVERRI
c6e60 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 66  DE_LOCK.#    def
c6e70 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41  ine SQLITE_THREA
c6e80 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20  D_OVERRIDE_LOCK 
c6e90 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 69  -1.#  endif.#  i
c6ea0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
c6eb0 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72  .int threadsOver
c6ec0 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
c6ed0 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52  cks = SQLITE_THR
c6ee0 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43  EAD_OVERRIDE_LOC
c6ef0 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 69  K;.#  else.stati
c6f00 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65  c int threadsOve
c6f10 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
c6f20 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48  ocks = SQLITE_TH
c6f30 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
c6f40 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e  CK;.#  endif.#en
c6f50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
c6f60 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20  structure holds 
c6f70 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73  information pass
c6f80 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75  ed into individu
c6f90 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61  al test.** threa
c6fa0 64 73 20 62 79 20 74 68 65 20 74 65 73 74 54 68  ds by the testTh
c6fb0 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76  readLockingBehav
c6fc0 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a  ior() routine..*
c6fd0 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 64 54  /.struct threadT
c6fe0 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20  estData {.  int 
c6ff0 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fd;             
c7000 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65     /* File to be
c7010 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72   locked */.  str
c7020 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20  uct flock lock; 
c7030 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69      /* The locki
c7040 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ng operation */.
c7050 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20    int result;   
c7060 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
c7070 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e  lt of the lockin
c7080 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d  g operation */.}
c7090 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  ;..#if SQLITE_TH
c70a0 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
c70b0 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f  ned(__linux__)./
c70c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
c70d0 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74 68  on is used as th
c70e0 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 66  e main routine f
c70f0 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 75 6e  or a thread laun
c7100 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 54  ched by.** testT
c7110 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61  hreadLockingBeha
c7120 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 74 73  vior(). It tests
c7130 20 77 68 65 74 68 65 72 20 74 68 65 20 73 68 61   whether the sha
c7140 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65  red-lock obtaine
c7150 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 6e  d.** by the main
c7160 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 74 54   thread in testT
c7170 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61  hreadLockingBeha
c7180 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 73  vior() conflicts
c7190 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f 74   with a.** hypot
c71a0 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d 6c 6f  hetical write-lo
c71b0 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74  ck obtained by t
c71c0 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 74 68  his thread on th
c71d0 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a 0a  e same file..**.
c71e0 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c 6f 63  ** The write-loc
c71f0 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c  k is not actuall
c7200 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 20 74  y acquired, as t
c7210 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69  his is not possi
c7220 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 66  ble if .** the f
c7230 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 72  ile is open in r
c7240 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 73  ead-only mode (s
c7250 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 32 29  ee ticket #3472)
c7260 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69  ..*/ .static voi
c7270 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67  d *threadLocking
c7280 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67 29  Test(void *pArg)
c7290 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 61  {.  struct threa
c72a0 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74 61  dTestData *pData
c72b0 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65 61   = (struct threa
c72c0 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67 3b  dTestData*)pArg;
c72d0 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c 74  .  pData->result
c72e0 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d 3e   = fcntl(pData->
c72f0 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 44  fd, F_GETLK, &pD
c7300 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 65  ata->lock);.  re
c7310 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 6e  turn pArg;.}.#en
c7320 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48  dif /* SQLITE_TH
c7330 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
c7340 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a  ned(__linux__) *
c7350 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  /...#if SQLITE_T
c7360 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
c7370 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
c7380 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65  /*.** This proce
c7390 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f  dure attempts to
c73a0 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   determine wheth
c73b0 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64  er or not thread
c73c0 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64  s.** can overrid
c73d0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  e each others lo
c73e0 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 68  cks then sets th
c73f0 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65  e .** threadsOve
c7400 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
c7410 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70  ocks variable ap
c7420 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a  propriately..*/.
c7430 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74  static void test
c7440 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
c7450 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69  avior(int fd_ori
c7460 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20  g){.  int fd;.  
c7470 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74  int rc;.  struct
c7480 20 74 68 72 65 61 64 54 65 73 74 44 61 74 61 20   threadTestData 
c7490 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63  d;.  struct floc
c74a0 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f 74  k l;.  pthread_t
c74b0 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 28   t;..  fd = dup(
c74c0 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 20  fd_orig);.  if( 
c74d0 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  fd<0 ) return;. 
c74e0 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 73   memset(&l, 0, s
c74f0 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e 6c  izeof(l));.  l.l
c7500 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b  _type = F_RDLCK;
c7510 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a  .  l.l_len = 1;.
c7520 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b    l.l_start = 0;
c7530 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  .  l.l_whence = 
c7540 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 3d  SEEK_SET;.  rc =
c7550 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c 20   fcntl(fd_orig, 
c7560 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 20  F_SETLK, &l);.  
c7570 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74 75  if( rc!=0 ) retu
c7580 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 2c  rn;.  memset(&d,
c7590 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a   0, sizeof(d));.
c75a0 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64    d.fd = fd;.  d
c75b0 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e 6c  .lock = l;.  d.l
c75c0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57  ock.l_type = F_W
c75d0 52 4c 43 4b 3b 0a 20 20 70 74 68 72 65 61 64 5f  RLCK;.  pthread_
c75e0 63 72 65 61 74 65 28 26 74 2c 20 30 2c 20 74 68  create(&t, 0, th
c75f0 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 2c  readLockingTest,
c7600 20 26 64 29 3b 0a 20 20 70 74 68 72 65 61 64 5f   &d);.  pthread_
c7610 6a 6f 69 6e 28 74 2c 20 30 29 3b 0a 20 20 63 6c  join(t, 0);.  cl
c7620 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20 64  ose(fd);.  if( d
c7630 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65 74  .result!=0 ) ret
c7640 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f 76  urn;.  threadsOv
c7650 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
c7660 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e  Locks = (d.lock.
c7670 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29  l_type==F_UNLCK)
c7680 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
c7690 4c 49 54 45 5f 54 48 45 52 41 44 53 41 46 45 20  LITE_THERADSAFE 
c76a0 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e  && defined(__lin
c76b0 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ux__) */../*.** 
c76c0 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c 6f  Release a unixLo
c76d0 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ckInfo structure
c76e0 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
c76f0 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63  cated by findLoc
c7700 6b 49 6e 66 6f 28 29 2e 0a 2a 2f 0a 73 74 61 74  kInfo()..*/.stat
c7710 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4c  ic void releaseL
c7720 6f 63 6b 49 6e 66 6f 28 73 74 72 75 63 74 20 75  ockInfo(struct u
c7730 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f  nixLockInfo *pLo
c7740 63 6b 29 7b 0a 20 20 69 66 28 20 70 4c 6f 63 6b  ck){.  if( pLock
c7750 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e   ){.    pLock->n
c7760 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  Ref--;.    if( p
c7770 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  Lock->nRef==0 ){
c7780 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b  .      if( pLock
c7790 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20  ->pPrev ){.     
c77a0 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
c77b0 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d  ->pPrev->pNext==
c77c0 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20  pLock );.       
c77d0 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70   pLock->pPrev->p
c77e0 4e 65 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e  Next = pLock->pN
c77f0 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ext;.      }else
c7800 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
c7810 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f 63  ( lockList==pLoc
c7820 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63  k );.        loc
c7830 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70  kList = pLock->p
c7840 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
c7850 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70      if( pLock->p
c7860 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Next ){.        
c7870 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70  assert( pLock->p
c7880 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c 6f  Next->pPrev==pLo
c7890 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c  ck );.        pL
c78a0 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  ock->pNext->pPre
c78b0 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76  v = pLock->pPrev
c78c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
c78d0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f  sqlite3_free(pLo
c78e0 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ck);.    }.  }.}
c78f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
c7900 61 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74  a unixOpenCnt st
c7910 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73  ructure previous
c7920 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ly allocated by 
c7930 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a  findLockInfo()..
c7940 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
c7950 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74  eleaseOpenCnt(st
c7960 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74  ruct unixOpenCnt
c7970 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 69 66 28 20   *pOpen){.  if( 
c7980 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70  pOpen ){.    pOp
c7990 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20  en->nRef--;.    
c79a0 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d  if( pOpen->nRef=
c79b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
c79c0 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a  pOpen->pPrev ){.
c79d0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
c79e0 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e  pOpen->pPrev->pN
c79f0 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20  ext==pOpen );.  
c7a00 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72        pOpen->pPr
c7a10 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65  ev->pNext = pOpe
c7a20 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  n->pNext;.      
c7a30 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
c7a40 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d  ssert( openList=
c7a50 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20  =pOpen );.      
c7a60 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70    openList = pOp
c7a70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  en->pNext;.     
c7a80 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70   }.      if( pOp
c7a90 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  en->pNext ){.   
c7aa0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
c7ab0 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76  en->pNext->pPrev
c7ac0 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20  ==pOpen );.     
c7ad0 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d     pOpen->pNext-
c7ae0 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e  >pPrev = pOpen->
c7af0 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20  pPrev;.      }. 
c7b00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
c7b10 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e  e(pOpen->aPendin
c7b20 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
c7b30 33 5f 66 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20  3_free(pOpen);. 
c7b40 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
c7b50 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65 20 64  * Given a file d
c7b60 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63 61 74  escriptor, locat
c7b70 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61  e unixLockInfo a
c7b80 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73  nd unixOpenCnt s
c7b90 74 72 75 63 74 75 72 65 73 20 74 68 61 74 0a 2a  tructures that.*
c7ba0 2a 20 64 65 73 63 72 69 62 65 73 20 74 68 61 74  * describes that
c7bb0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
c7bc0 2e 20 20 43 72 65 61 74 65 20 6e 65 77 20 6f 6e  .  Create new on
c7bd0 65 73 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  es if necessary.
c7be0 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20    The.** return 
c7bf0 76 61 6c 75 65 73 20 6d 69 67 68 74 20 62 65 20  values might be 
c7c00 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66  uninitialized if
c7c10 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
c7c20 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ..**.** Return a
c7c30 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72  n appropriate er
c7c40 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61  ror code..*/.sta
c7c50 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b  tic int findLock
c7c60 49 6e 66 6f 28 0a 20 20 75 6e 69 78 46 69 6c 65  Info(.  unixFile
c7c70 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20   *pFile,        
c7c80 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 78 20 66         /* Unix f
c7c90 69 6c 65 20 77 69 74 68 20 66 69 6c 65 20 64 65  ile with file de
c7ca0 73 63 20 75 73 65 64 20 69 6e 20 74 68 65 20 6b  sc used in the k
c7cb0 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  ey */.  struct u
c7cc0 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70  nixLockInfo **pp
c7cd0 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 74 75 72 6e  Lock,  /* Return
c7ce0 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66   the unixLockInf
c7cf0 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65  o structure here
c7d00 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
c7d10 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 70 4f 70 65  xOpenCnt **ppOpe
c7d20 6e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74  n    /* Return t
c7d30 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73  he unixOpenCnt s
c7d40 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f  tructure here */
c7d50 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
c7d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7d70 20 20 20 20 20 2f 2a 20 53 79 73 74 65 6d 20 63       /* System c
c7d80 61 6c 6c 20 72 65 74 75 72 6e 20 63 6f 64 65 20  all return code 
c7d90 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20  */.  int fd;    
c7da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7db0 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20      /* The file 
c7dc0 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 70  descriptor for p
c7dd0 46 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  File */.  struct
c7de0 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63   unixLockKey loc
c7df0 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 4c 6f 6f 6b  kKey;    /* Look
c7e00 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 75  up key for the u
c7e10 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75  nixLockInfo stru
c7e20 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 63  cture */.  struc
c7e30 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c  t unixFileId fil
c7e40 65 49 64 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 6f  eId;      /* Loo
c7e50 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20  kup key for the 
c7e60 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75  unixOpenCnt stru
c7e70 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  ct */.  struct s
c7e80 74 61 74 20 73 74 61 74 62 75 66 3b 20 20 20 20  tat statbuf;    
c7e90 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65         /* Low-le
c7ea0 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61  vel file informa
c7eb0 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74  tion */.  struct
c7ec0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70   unixLockInfo *p
c7ed0 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 43 61 6e 64  Lock;    /* Cand
c7ee0 69 64 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e  idate unixLockIn
c7ef0 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73  fo object */.  s
c7f00 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
c7f10 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 2f 2a  t *pOpen;     /*
c7f20 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4f   Candidate unixO
c7f30 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 20 2a 2f  penCnt object */
c7f40 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c  ..  /* Get low-l
c7f50 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  evel information
c7f60 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20   about the file 
c7f70 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 64  that we can used
c7f80 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20   to.  ** create 
c7f90 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f  a unique name fo
c7fa0 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f  r the file..  */
c7fb0 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68  .  fd = pFile->h
c7fc0 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 66  ;.  rc = fstat(f
c7fd0 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20  d, &statbuf);.  
c7fe0 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20  if( rc!=0 ){.   
c7ff0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
c8000 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 65  o = errno;.#ifde
c8010 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20  f EOVERFLOW.    
c8020 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45  if( pFile->lastE
c8030 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 20  rrno==EOVERFLOW 
c8040 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
c8050 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 20  NOLFS;.#endif.  
c8060 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c8070 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66 64  IOERR;.  }..#ifd
c8080 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20 2f  ef __APPLE__.  /
c8090 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e 20  * On OS X on an 
c80a0 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d  msdos filesystem
c80b0 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62  , the inode numb
c80c0 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 0a 20  er is reported. 
c80d0 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79 20   ** incorrectly 
c80e0 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66 69  for zero-size fi
c80f0 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74  les.  See ticket
c8100 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f 72 6b   #3260.  To work
c8110 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68 69  .  ** around thi
c8120 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 63 6f  s problem (we co
c8130 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 67 20  nsider it a bug 
c8140 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51 4c  in OS X, not SQL
c8150 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c 77  ite).  ** we alw
c8160 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 68 65  ays increase the
c8170 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 31 20   file size to 1 
c8180 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 69 6e  by writing a sin
c8190 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 70 72  gle byte.  ** pr
c81a0 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 6e 67  ior to accessing
c81b0 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65   the inode numbe
c81c0 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 74 65  r.  The one byte
c81d0 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 2a 2a   written is.  **
c81e0 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 63 68   an ASCII 'S' ch
c81f0 61 72 61 63 74 65 72 20 77 68 69 63 68 20 61 6c  aracter which al
c8200 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65  so happens to be
c8210 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a   the first byte.
c8220 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 64    ** in the head
c8230 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 4c 69  er of every SQLi
c8240 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 6e  te database.  In
c8250 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68   this way, if th
c8260 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 72 61  ere.  ** is a ra
c8270 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63  ce condition suc
c8280 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74  h that another t
c8290 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 61 64  hread has alread
c82a0 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a 2a  y populated.  **
c82b0 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
c82c0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  of the database,
c82d0 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64 6f   no damage is do
c82e0 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ne..  */.  if( s
c82f0 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d 3d  tatbuf.st_size==
c8300 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 72  0 ){.    rc = wr
c8310 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b  ite(fd, "S", 1);
c8320 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 20 29  .    if( rc!=1 )
c8330 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
c8340 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20  QLITE_IOERR;.   
c8350 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73 74 61   }.    rc = fsta
c8360 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b  t(fd, &statbuf);
c8370 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29  .    if( rc!=0 )
c8380 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
c8390 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
c83a0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
c83b0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20  QLITE_IOERR;.   
c83c0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
c83d0 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 79   memset(&lockKey
c83e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b  , 0, sizeof(lock
c83f0 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 79  Key));.  lockKey
c8400 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61 74 62  .fid.dev = statb
c8410 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 4f  uf.st_dev;.#if O
c8420 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 6b  S_VXWORKS.  lock
c8430 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 46  Key.fid.pId = pF
c8440 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a  ile->pId;.#else.
c8450 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 6e    lockKey.fid.in
c8460 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 69  o = statbuf.st_i
c8470 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  no;.#endif.#if S
c8480 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
c8490 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
c84a0 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 72  nux__).  if( thr
c84b0 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
c84c0 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b  OthersLocks<0 ){
c84d0 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c  .    testThreadL
c84e0 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66  ockingBehavior(f
c84f0 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b 65  d);.  }.  lockKe
c8500 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64 73 4f  y.tid = threadsO
c8510 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
c8520 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 68  sLocks ? 0 : pth
c8530 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e  read_self();.#en
c8540 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 6c  dif.  fileId = l
c8550 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 66  ockKey.fid;.  if
c8560 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a 20  ( ppLock!=0 ){. 
c8570 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c     pLock = lockL
c8580 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ist;.    while( 
c8590 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28  pLock && memcmp(
c85a0 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 6b  &lockKey, &pLock
c85b0 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 6f  ->lockKey, sizeo
c85c0 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a 20  f(lockKey)) ){. 
c85d0 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f       pLock = pLo
c85e0 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  ck->pNext;.    }
c85f0 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d  .    if( pLock==
c8600 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b  0 ){.      pLock
c8610 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
c8620 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 6b  c( sizeof(*pLock
c8630 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ) );.      if( p
c8640 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Lock==0 ){.     
c8650 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
c8660 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f  OMEM;.        go
c8670 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b  to exit_findlock
c8680 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20  info;.      }.  
c8690 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b      pLock->lockK
c86a0 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a 20 20  ey = lockKey;.  
c86b0 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 20      pLock->nRef 
c86c0 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b  = 1;.      pLock
c86d0 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  ->cnt = 0;.     
c86e0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
c86f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63   = 0;.      pLoc
c8700 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c  k->pNext = lockL
c8710 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b  ist;.      pLock
c8720 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20  ->pPrev = 0;.   
c8730 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 74 20     if( lockList 
c8740 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 72 65  ) lockList->pPre
c8750 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 20  v = pLock;.     
c8760 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63   lockList = pLoc
c8770 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  k;.    }else{.  
c8780 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2b      pLock->nRef+
c8790 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70  +;.    }.    *pp
c87a0 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20  Lock = pLock;.  
c87b0 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e 21 3d  }.  if( ppOpen!=
c87c0 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 20 3d  0 ){.    pOpen =
c87d0 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 20 77   openList;.    w
c87e0 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 20 6d  hile( pOpen && m
c87f0 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c 20 26  emcmp(&fileId, &
c8800 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c 20 73  pOpen->fileId, s
c8810 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 20 29  izeof(fileId)) )
c8820 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d 20  {.      pOpen = 
c8830 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20  pOpen->pNext;.  
c8840 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 65    }.    if( pOpe
c8850 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4f  n==0 ){.      pO
c8860 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  pen = sqlite3_ma
c8870 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4f  lloc( sizeof(*pO
c8880 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 69 66  pen) );.      if
c8890 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20  ( pOpen==0 ){.  
c88a0 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63        releaseLoc
c88b0 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a 20 20  kInfo(pLock);.  
c88c0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
c88d0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20  E_NOMEM;.       
c88e0 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c   goto exit_findl
c88f0 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d  ockinfo;.      }
c8900 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 69  .      pOpen->fi
c8910 6c 65 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a 20  leId = fileId;. 
c8920 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66       pOpen->nRef
c8930 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65   = 1;.      pOpe
c8940 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  n->nLock = 0;.  
c8950 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64      pOpen->nPend
c8960 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ing = 0;.      p
c8970 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d  Open->aPending =
c8980 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d   0;.      pOpen-
c8990 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 73  >pNext = openLis
c89a0 74 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  t;.      pOpen->
c89b0 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20  pPrev = 0;.     
c89c0 20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20   if( openList ) 
c89d0 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20  openList->pPrev 
c89e0 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f  = pOpen;.      o
c89f0 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b  penList = pOpen;
c8a00 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
c8a10 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 53 65        pOpen->pSe
c8a20 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20  m = NULL;.      
c8a30 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b  pOpen->aSemName[
c8a40 30 5d 20 3d 20 27 5c 30 27 3b 0a 23 65 6e 64 69  0] = '\0';.#endi
c8a50 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  f.    }else{.   
c8a60 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b     pOpen->nRef++
c8a70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f  ;.    }.    *ppO
c8a80 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d  pen = pOpen;.  }
c8a90 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69  ..exit_findlocki
c8aa0 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63  nfo:.  return rc
c8ab0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65  ;.}../*.** If we
c8ac0 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 69   are currently i
c8ad0 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68  n a different th
c8ae0 72 65 61 64 20 74 68 61 6e 20 74 68 65 20 74 68  read than the th
c8af0 72 65 61 64 20 74 68 61 74 20 74 68 65 0a 2a 2a  read that the.**
c8b00 20 75 6e 69 78 46 69 6c 65 20 61 72 67 75 6d 65   unixFile argume
c8b10 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 74  nt belongs to, t
c8b20 68 65 6e 20 74 72 61 6e 73 66 65 72 20 6f 77 6e  hen transfer own
c8b30 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 75 6e  ership of the un
c8b40 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 72 20 74  ixFile.** over t
c8b50 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68  o the current th
c8b60 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e  read..**.** A un
c8b70 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c 79 20 6f  ixFile is only o
c8b80 77 6e 65 64 20 62 79 20 61 20 74 68 72 65 61 64  wned by a thread
c8b90 20 6f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74   on systems that
c8ba0 20 75 73 65 20 4c 69 6e 75 78 54 68 72 65 61 64   use LinuxThread
c8bb0 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68  s..**.** Ownersh
c8bc0 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f  ip transfer is o
c8bd0 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74  nly allowed if t
c8be0 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63  he unixFile is c
c8bf0 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65  urrently unlocke
c8c00 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69  d..** If the uni
c8c10 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20  xFile is locked 
c8c20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70  and an ownership
c8c30 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20   is wrong, then 
c8c40 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
c8c50 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45  _MISUSE.  SQLITE
c8c60 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
c8c70 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f  if everything wo
c8c80 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  rks..*/.#if SQLI
c8c90 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
c8ca0 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
c8cb0 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e 74 20 74  __).static int t
c8cc0 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70  ransferOwnership
c8cd0 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
c8ce0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70  ){.  int rc;.  p
c8cf0 74 68 72 65 61 64 5f 74 20 68 53 65 6c 66 3b 0a  thread_t hSelf;.
c8d00 20 20 69 66 28 20 74 68 72 65 61 64 73 4f 76 65    if( threadsOve
c8d10 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
c8d20 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4f  ocks ){.    /* O
c8d30 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65  wnership transfe
c8d40 72 73 20 6e 6f 74 20 6e 65 65 64 65 64 20 6f 6e  rs not needed on
c8d50 20 74 68 69 73 20 73 79 73 74 65 6d 20 2a 2f 0a   this system */.
c8d60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
c8d70 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 53 65 6c  E_OK;.  }.  hSel
c8d80 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66  f = pthread_self
c8d90 28 29 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61  ();.  if( pthrea
c8da0 64 5f 65 71 75 61 6c 28 70 46 69 6c 65 2d 3e 74  d_equal(pFile->t
c8db0 69 64 2c 20 68 53 65 6c 66 29 20 29 7b 0a 20 20  id, hSelf) ){.  
c8dc0 20 20 2f 2a 20 57 65 20 61 72 65 20 73 74 69 6c    /* We are stil
c8dd0 6c 20 69 6e 20 74 68 65 20 73 61 6d 65 20 74 68  l in the same th
c8de0 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f 53 54 52  read */.    OSTR
c8df0 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e 73 66 65  ACE1("No-transfe
c8e00 72 2c 20 73 61 6d 65 20 74 68 72 65 61 64 5c 6e  r, same thread\n
c8e10 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ");.    return S
c8e20 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
c8e30 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
c8e40 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a  ype!=NO_LOCK ){.
c8e50 20 20 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74      /* We cannot
c8e60 20 63 68 61 6e 67 65 20 6f 77 6e 65 72 73 68 69   change ownershi
c8e70 70 20 77 68 69 6c 65 20 77 65 20 61 72 65 20 68  p while we are h
c8e80 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b 21 20 2a  olding a lock! *
c8e90 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  /.    return SQL
c8ea0 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
c8eb0 20 20 4f 53 54 52 41 43 45 34 28 22 54 72 61 6e    OSTRACE4("Tran
c8ec0 73 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f  sfer ownership o
c8ed0 66 20 25 64 20 66 72 6f 6d 20 25 64 20 74 6f 20  f %d from %d to 
c8ee0 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %d\n",.         
c8ef0 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69     pFile->h, pFi
c8f00 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 3b  le->tid, hSelf);
c8f10 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64 20 3d 20  .  pFile->tid = 
c8f20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28 70 46 69  hSelf;.  if (pFi
c8f30 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c  le->pLock != NUL
c8f40 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  L) {.    release
c8f50 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e  LockInfo(pFile->
c8f60 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d  pLock);.    rc =
c8f70 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 46   findLockInfo(pF
c8f80 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f  ile, &pFile->pLo
c8f90 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f 53 54 52  ck, 0);.    OSTR
c8fa0 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE5("LOCK    %d
c8fb0 20 69 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 64   is now %s(%s,%d
c8fc0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  )\n", pFile->h,.
c8fd0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74             lockt
c8fe0 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c  ypeName(pFile->l
c8ff0 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20  ocktype),.      
c9000 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d       locktypeNam
c9010 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e  e(pFile->pLock->
c9020 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46 69 6c 65  locktype), pFile
c9030 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20  ->pLock->cnt);. 
c9040 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
c9050 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74  } else {.    ret
c9060 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
c9070 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f 2a 20 69   }.}.#else  /* i
c9080 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f 54 48 52  f not SQLITE_THR
c9090 45 41 44 53 41 46 45 20 2a 2f 0a 20 20 2f 2a 20  EADSAFE */.  /* 
c90a0 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64  On single-thread
c90b0 65 64 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72  ed builds, owner
c90c0 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 73  ship transfer is
c90d0 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65   a no-op */.# de
c90e0 66 69 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 6e  fine transferOwn
c90f0 65 72 73 68 69 70 28 58 29 20 53 51 4c 49 54 45  ership(X) SQLITE
c9100 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  _OK.#endif /* SQ
c9110 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
c9120 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  */.../*.** This 
c9130 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
c9140 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
c9150 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
c9160 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
c9170 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
c9180 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
c9190 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
c91a0 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73   lock is held, s
c91b0 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74  et *pResOut.** t
c91c0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
c91d0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52  ue otherwise *pR
c91e0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
c91f0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72  zero.  The retur
c9200 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  n value.** is se
c9210 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75  t to SQLITE_OK u
c9220 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72  nless an I/O err
c9230 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
c9240 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a   lock checking..
c9250 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
c9260 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  ixCheckReservedL
c9270 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
c9280 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f   *id, int *pResO
c9290 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ut){.  int rc = 
c92a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
c92b0 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20   reserved = 0;. 
c92c0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
c92d0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
c92e0 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  ;..  SimulateIOE
c92f0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
c9300 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
c9310 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a  ESERVEDLOCK; );.
c9320 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
c9330 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d   );.  unixEnterM
c9340 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75  utex(); /* Becau
c9350 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20  se pFile->pLock 
c9360 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73  is shared across
c9370 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f   threads */..  /
c9380 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72  * Check if a thr
c9390 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63  ead in this proc
c93a0 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61  ess holds such a
c93b0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70   lock */.  if( p
c93c0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63  File->pLock->loc
c93d0 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43  ktype>SHARED_LOC
c93e0 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65  K ){.    reserve
c93f0 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  d = 1;.  }..  /*
c9400 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
c9410 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
c9420 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20  cess holds it.. 
c9430 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 5f 44 4a   */.#ifndef __DJ
c9440 47 50 50 5f 5f 0a 20 20 69 66 28 20 21 72 65 73  GPP__.  if( !res
c9450 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 74 72  erved ){.    str
c9460 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a  uct flock lock;.
c9470 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63      lock.l_whenc
c9480 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
c9490 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d    lock.l_start =
c94a0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
c94b0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
c94c0 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74   1;.    lock.l_t
c94d0 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20  ype = F_WRLCK;. 
c94e0 20 20 20 69 66 20 28 2d 31 20 3d 3d 20 66 63 6e     if (-1 == fcn
c94f0 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 47  tl(pFile->h, F_G
c9500 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a  ETLK, &lock)) {.
c9510 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f        int tErrno
c9520 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
c9530 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
c9540 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
c9550 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
c9560 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45  ERR_CHECKRESERVE
c9570 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 70 46  DLOCK);.      pF
c9580 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
c9590 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 20 65   tErrno;.    } e
c95a0 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74  lse if( lock.l_t
c95b0 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a  ype!=F_UNLCK ){.
c95c0 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d        reserved =
c95d0 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65   1;.    }.  }.#e
c95e0 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69 78 4c 65  ndif.  .  unixLe
c95f0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53  aveMutex();.  OS
c9600 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d  TRACE4("TEST WR-
c9610 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22  LOCK %d %d %d\n"
c9620 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20  , pFile->h, rc, 
c9630 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70  reserved);..  *p
c9640 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65  ResOut = reserve
c9650 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  d;.  return rc;.
c9660 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
c9670 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
c9680 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
c9690 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
c96a0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
c96b0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
c96c0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
c96d0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
c96e0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
c96f0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
c9700 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
c9710 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
c9720 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
c9730 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
c9740 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
c9750 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
c9760 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
c9770 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
c9780 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
c9790 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
c97a0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
c97b0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
c97c0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
c97d0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
c97e0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
c97f0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
c9800 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
c9810 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
c9820 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
c9830 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
c9840 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
c9850 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
c9860 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
c9870 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
c9880 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
c9890 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
c98a0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
c98b0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
c98c0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
c98d0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
c98e0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
c98f0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
c9900 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
c9910 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
c9920 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
c9930 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
c9940 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69  k.  Use the sqli
c9950 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a  te3OsUnlock().**
c9960 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65   routine to lowe
c9970 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  r a locking leve
c9980 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
c9990 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 65   unixLock(sqlite
c99a0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
c99b0 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a 20  locktype){.  /* 
c99c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65  The following de
c99d0 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70 6c  scribes the impl
c99e0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
c99f0 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20  e various locks 
c9a00 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 72  and.  ** lock tr
c9a10 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65 72  ansitions in ter
c9a20 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58 20  ms of the POSIX 
c9a30 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64 20  advisory shared 
c9a40 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20 20  and exclusive.  
c9a50 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69 76  ** lock primitiv
c9a60 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64 2d  es (called read-
c9a70 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65 2d  locks and write-
c9a80 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f 20  locks below, to 
c9a90 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 75  avoid.  ** confu
c9aa0 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74 65  sion with SQLite
c9ab0 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54 68   lock names). Th
c9ac0 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65  e algorithms are
c9ad0 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20 2a   complicated.  *
c9ae0 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f 72  * slightly in or
c9af0 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61 74  der to be compat
c9b00 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f 77  ible with window
c9b10 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c 74  s systems simult
c9b20 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 63  aneously.  ** ac
c9b30 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65  cessing the same
c9b40 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
c9b50 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73 20  in case that is 
c9b60 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a 20  ever required.. 
c9b70 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c 73   **.  ** Symbols
c9b80 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e 68   defined in os.h
c9b90 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20 27   indentify the '
c9ba0 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61 6e  pending byte' an
c9bb0 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64 0a  d the 'reserved.
c9bc0 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63 68    ** byte', each
c9bd0 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61 74   single bytes at
c9be0 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73   well known offs
c9bf0 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73 68  ets, and the 'sh
c9c00 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72  ared byte.  ** r
c9c10 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20 6f  ange', a range o
c9c20 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20 61  f 510 bytes at a
c9c30 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 73   well known offs
c9c40 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f  et..  **.  ** To
c9c50 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45 44   obtain a SHARED
c9c60 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c 6f   lock, a read-lo
c9c70 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f  ck is obtained o
c9c80 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a 20  n the 'pending. 
c9c90 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20 74   ** byte'.  If t
c9ca0 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66 75  his is successfu
c9cb0 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65  l, a random byte
c9cc0 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72 65   from the 'share
c9cd0 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67  d byte.  ** rang
c9ce0 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b 65  e' is read-locke
c9cf0 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 6f  d and the lock o
c9d00 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62  n the 'pending b
c9d10 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a 20  yte' released.. 
c9d20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65   **.  ** A proce
c9d30 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61  ss may only obta
c9d40 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  in a RESERVED lo
c9d50 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20  ck after it has 
c9d60 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a 20  a SHARED lock.. 
c9d70 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20 6c   ** A RESERVED l
c9d80 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  ock is implement
c9d90 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20 61  ed by grabbing a
c9da0 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74   write-lock on t
c9db0 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76 65  he.  ** 'reserve
c9dc0 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a 20  d byte'. .  **. 
c9dd0 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d 61   ** A process ma
c9de0 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20  y only obtain a 
c9df0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66 74  PENDING lock aft
c9e00 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69 6e  er it has obtain
c9e10 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45 44  ed a.  ** SHARED
c9e20 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e 47   lock. A PENDING
c9e30 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65   lock is impleme
c9e40 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 6e  nted by obtainin
c9e50 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a 20  g a write-lock. 
c9e60 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e 64   ** on the 'pend
c9e70 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73 20  ing byte'. This 
c9e80 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f 20  ensures that no 
c9e90 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73  new SHARED locks
c9ea0 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 74   can be.  ** obt
c9eb0 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73 74  ained, but exist
c9ec0 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b 73  ing SHARED locks
c9ed0 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20   are allowed to 
c9ee0 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63 65  persist. A proce
c9ef0 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74  ss.  ** does not
c9f00 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e 20   have to obtain 
c9f10 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
c9f20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61 20  on the way to a 
c9f30 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 20  PENDING lock..  
c9f40 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74 79  ** This property
c9f50 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
c9f60 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72 6f  algorithm for ro
c9f70 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75  lling back a jou
c9f80 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 61  rnal file.  ** a
c9f90 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20 20  fter a crash..  
c9fa0 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c 55  **.  ** An EXCLU
c9fb0 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 69  SIVE lock, obtai
c9fc0 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e 44  ned after a PEND
c9fd0 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  ING lock is held
c9fe0 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d  , is.  ** implem
c9ff0 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69  ented by obtaini
ca000 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  ng a write-lock 
ca010 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27 73  on the entire 's
ca020 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20  hared byte.  ** 
ca030 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 6c  range'. Since al
ca040 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 65  l other locks re
ca050 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f 63  quire a read-loc
ca060 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  k on one of the 
ca070 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68 69  bytes.  ** withi
ca080 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 68  n this range, th
ca090 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20  is ensures that 
ca0a0 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 61  no other locks a
ca0b0 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a 20  re held on the. 
ca0c0 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a 20   ** database. . 
ca0d0 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65 61   **.  ** The rea
ca0e0 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79 74  son a single byt
ca0f0 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64  e cannot be used
ca100 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20   instead of the 
ca110 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a  'shared byte.  *
ca120 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61 74  * range' is that
ca130 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f   some versions o
ca140 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f 74  f windows do not
ca150 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c 6f   support read-lo
ca160 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f 63  cks. By.  ** loc
ca170 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62 79  king a random by
ca180 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65 2c  te from a range,
ca190 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41 52   concurrent SHAR
ca1a0 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78 69  ED locks may exi
ca1b0 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 20  st.  ** even if 
ca1c0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d  the locking prim
ca1d0 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61 6c  itive used is al
ca1e0 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f 63  ways a write-loc
ca1f0 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 63  k..  */.  int rc
ca200 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
ca210 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
ca220 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
ca230 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  .  struct unixLo
ca240 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20  ckInfo *pLock = 
ca250 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20  pFile->pLock;.  
ca260 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
ca270 6b 3b 0a 20 20 69 6e 74 20 73 3b 0a 0a 20 20 61  k;.  int s;..  a
ca280 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
ca290 20 20 4f 53 54 52 41 43 45 37 28 22 4c 4f 43 4b    OSTRACE7("LOCK
ca2a0 20 20 20 20 25 64 20 25 73 20 77 61 73 20 25 73      %d %s was %s
ca2b0 28 25 73 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e  (%s,%d) pid=%d\n
ca2c0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20  ", pFile->h,.   
ca2d0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
ca2e0 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74  locktype), lockt
ca2f0 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c  ypeName(pFile->l
ca300 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20  ocktype),.      
ca310 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c 6f  locktypeName(pLo
ca320 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70  ck->locktype), p
ca330 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 70  Lock->cnt , getp
ca340 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  id());..  /* If 
ca350 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79  there is already
ca360 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20   a lock of this 
ca370 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73  type or more res
ca380 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a  trictive on the.
ca390 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64    ** unixFile, d
ca3a0 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74  o nothing. Don't
ca3b0 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63   use the end_loc
ca3c0 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73  k: exit path, as
ca3d0 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d  .  ** unixEnterM
ca3e0 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65  utex() hasn't be
ca3f0 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20  en called yet.. 
ca400 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
ca410 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74  >locktype>=lockt
ca420 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41  ype ){.    OSTRA
ca430 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20  CE3("LOCK    %d 
ca440 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68  %s ok (already h
ca450 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  eld)\n", pFile->
ca460 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c  h,.            l
ca470 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b  ocktypeName(lock
ca480 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75  type));.    retu
ca490 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
ca4a0 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
ca4b0 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65  e the locking se
ca4c0 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63  quence is correc
ca4d0 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  t.  */.  assert(
ca4e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
ca4f0 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63  !=NO_LOCK || loc
ca500 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
ca510 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
ca520 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e  locktype!=PENDIN
ca530 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  G_LOCK );.  asse
ca540 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45  rt( locktype!=RE
ca550 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70  SERVED_LOCK || p
ca560 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
ca570 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a  SHARED_LOCK );..
ca580 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20    /* This mutex 
ca590 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73  is needed becaus
ca5a0 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69  e pFile->pLock i
ca5b0 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20  s shared across 
ca5c0 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 75  threads.  */.  u
ca5d0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
ca5e0 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
ca5f0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
ca600 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 69  ead owns the pFi
ca610 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  le..  */.  rc = 
ca620 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69  transferOwnershi
ca630 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  p(pFile);.  if( 
ca640 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
ca650 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  .    unixLeaveMu
ca660 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72  tex();.    retur
ca670 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f 63  n rc;.  }.  pLoc
ca680 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  k = pFile->pLock
ca690 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20  ;..  /* If some 
ca6a0 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 69  thread using thi
ca6b0 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63 6b  s PID has a lock
ca6c0 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e 74   via a different
ca6d0 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a 20   unixFile*.  ** 
ca6e0 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65 63  handle that prec
ca6f0 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65 73  ludes the reques
ca700 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72 6e  ted lock, return
ca710 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66   BUSY..  */.  if
ca720 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  ( (pFile->lockty
ca730 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74  pe!=pLock->lockt
ca740 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 20 20  ype && .        
ca750 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79    (pLock->lockty
ca760 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  pe>=PENDING_LOCK
ca770 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48 41   || locktype>SHA
ca780 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a  RED_LOCK)).  ){.
ca790 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
ca7a0 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 65  BUSY;.    goto e
ca7b0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20  nd_lock;.  }..  
ca7c0 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20 6c  /* If a SHARED l
ca7d0 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64  ock is requested
ca7e0 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65 61  , and some threa
ca7f0 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44  d using this PID
ca800 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68 61   already.  ** ha
ca810 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52 45  s a SHARED or RE
ca820 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68 65  SERVED lock, the
ca830 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66 65  n increment refe
ca840 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e 64  rence counts and
ca850 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  .  ** return SQL
ca860 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20 69  ITE_OK..  */.  i
ca870 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
ca880 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20 20  RED_LOCK && .   
ca890 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74     (pLock->lockt
ca8a0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
ca8b0 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74   || pLock->lockt
ca8c0 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype==RESERVED_LO
ca8d0 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72  CK) ){.    asser
ca8e0 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  t( locktype==SHA
ca8f0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
ca900 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
ca910 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20 20  ocktype==0 );.  
ca920 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d    assert( pLock-
ca930 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70 46  >cnt>0 );.    pF
ca940 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
ca950 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20  SHARED_LOCK;.   
ca960 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20   pLock->cnt++;. 
ca970 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d     pFile->pOpen-
ca980 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67 6f  >nLock++;.    go
ca990 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d  to end_lock;.  }
ca9a0 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d  ..  lock.l_len =
ca9b0 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 77   1L;..  lock.l_w
ca9c0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
ca9d0 3b 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e  ;..  /* A PENDIN
ca9e0 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64  G lock is needed
ca9f0 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e   before acquirin
caa00 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20  g a SHARED lock 
caa10 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20  and before.  ** 
caa20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43  acquiring an EXC
caa30 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f  LUSIVE lock.  Fo
caa40 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63  r the SHARED loc
caa50 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77  k, the PENDING w
caa60 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65  ill.  ** be rele
caa70 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ased..  */.  if(
caa80 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
caa90 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c  D_LOCK .      ||
caaa0 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c   (locktype==EXCL
caab0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46  USIVE_LOCK && pF
caac0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45  ile->locktype<PE
caad0 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b  NDING_LOCK).  ){
caae0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  .    lock.l_type
caaf0 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   = (locktype==SH
cab00 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c 43  ARED_LOCK?F_RDLC
cab10 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 20  K:F_WRLCK);.    
cab20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50  lock.l_start = P
cab30 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
cab40 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65   s = fcntl(pFile
cab50 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
cab60 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 3d  ock);.    if( s=
cab70 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69  =(-1) ){.      i
cab80 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
cab90 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  o;.      rc = sq
caba0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
cabb0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
cabc0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
cabd0 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53  K);.      if( IS
cabe0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
cabf0 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
cac00 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
cac10 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rrno;.      }.  
cac20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63      goto end_loc
cac30 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20  k;.    }.  }... 
cac40 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67   /* If control g
cac50 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e  ets to this poin
cac60 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79  t, then actually
cac70 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61   go ahead and ma
cac80 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e  ke.  ** operatin
cac90 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66  g system calls f
caca0 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64  or the specified
cacb0 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66   lock..  */.  if
cacc0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
cacd0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69  ED_LOCK ){.    i
cace0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 20  nt tErrno = 0;. 
cacf0 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
cad00 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20  ->cnt==0 );.    
cad10 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c  assert( pLock->l
cad20 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20  ocktype==0 );.. 
cad30 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68     /* Now get th
cad40 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20  e read-lock */. 
cad50 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
cad60 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a  = SHARED_FIRST;.
cad70 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
cad80 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20   SHARED_SIZE;.  
cad90 20 20 69 66 28 20 28 73 20 3d 20 66 63 6e 74 6c    if( (s = fcntl
cada0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54  (pFile->h, F_SET
cadb0 4c 4b 2c 20 26 6c 6f 63 6b 29 29 3d 3d 28 2d 31  LK, &lock))==(-1
cadc0 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e  ) ){.      tErrn
cadd0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d  o = errno;.    }
cade0 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 65  .    /* Drop the
cadf0 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 49   temporary PENDI
cae00 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c  NG lock */.    l
cae10 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45  ock.l_start = PE
cae20 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20  NDING_BYTE;.    
cae30 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b  lock.l_len = 1L;
cae40 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  .    lock.l_type
cae50 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20   = F_UNLCK;.    
cae60 69 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d  if( fcntl(pFile-
cae70 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f  >h, F_SETLK, &lo
cae80 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ck)!=0 ){.      
cae90 69 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a 20  if( s != -1 ){. 
caea0 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63         /* This c
caeb0 6f 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74 68  ould happen with
caec0 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74   a network mount
caed0 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72 72   */.        tErr
caee0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 20  no = errno; .   
caef0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
caf00 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
caf10 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
caf20 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
caf30 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  ; .        if( I
caf40 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
caf50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46   ){.          pF
caf60 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
caf70 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20   tErrno;.       
caf80 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20   }.        goto 
caf90 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20  end_lock;.      
cafa0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
cafb0 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  s==(-1) ){.     
cafc0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
cafd0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
cafe0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
caff0 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  OERR_LOCK);.    
cb000 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
cb010 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
cb020 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
cb030 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
cb040 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
cb050 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
cb060 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f  cktype = SHARED_
cb070 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c  LOCK;.      pFil
cb080 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b  e->pOpen->nLock+
cb090 2b 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  +;.      pLock->
cb0a0 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  cnt = 1;.    }. 
cb0b0 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74   }else if( lockt
cb0c0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
cb0d0 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e  OCK && pLock->cn
cb0e0 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65  t>1 ){.    /* We
cb0f0 20 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72 20   are trying for 
cb100 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
cb110 6b 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74 68  k but another th
cb120 72 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20 20  read in this.   
cb130 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73 73   ** same process
cb140 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e   is still holdin
cb150 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e  g a shared lock.
cb160 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   */.    rc = SQL
cb170 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73  ITE_BUSY;.  }els
cb180 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65  e{.    /* The re
cb190 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20  quest was for a 
cb1a0 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c  RESERVED or EXCL
cb1b0 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20  USIVE lock.  It 
cb1c0 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65  is.    ** assume
cb1d0 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20  d that there is 
cb1e0 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61  a SHARED or grea
cb1f0 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ter lock on the 
cb200 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65  file.    ** alre
cb210 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ady..    */.    
cb220 61 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65  assert( 0!=pFile
cb230 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
cb240 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
cb250 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69  F_WRLCK;.    swi
cb260 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b  tch( locktype ){
cb270 0a 20 20 20 20 20 20 63 61 73 65 20 52 45 53 45  .      case RESE
cb280 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20  RVED_LOCK:.     
cb290 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
cb2a0 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b  = RESERVED_BYTE;
cb2b0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
cb2c0 20 20 20 20 20 20 63 61 73 65 20 45 58 43 4c 55        case EXCLU
cb2d0 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20  SIVE_LOCK:.     
cb2e0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
cb2f0 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a  = SHARED_FIRST;.
cb300 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c          lock.l_l
cb310 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  en = SHARED_SIZE
cb320 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
cb330 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a  .      default:.
cb340 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 30          assert(0
cb350 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 20 3d  );.    }.    s =
cb360 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
cb370 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
cb380 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31  ;.    if( s==(-1
cb390 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74  ) ){.      int t
cb3a0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
cb3b0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
cb3c0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
cb3d0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
cb3e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a  TE_IOERR_LOCK);.
cb3f0 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43        if( IS_LOC
cb400 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
cb410 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
cb420 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
cb430 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
cb440 20 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 20    }.  ..#ifndef 
cb450 4e 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20  NDEBUG.  /* Set 
cb460 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  up the transacti
cb470 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67  on-counter chang
cb480 65 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73  e checking flags
cb490 20 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73   when.  ** trans
cb4a0 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20  itioning from a 
cb4b0 53 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 45  SHARED to a RESE
cb4c0 52 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20  RVED lock.  The 
cb4d0 63 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d  change.  ** from
cb4e0 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52   SHARED to RESER
cb4f0 56 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65  VED marks the be
cb500 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72  ginning of a nor
cb510 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f  mal.  ** write o
cb520 70 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20  peration (not a 
cb530 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c  hot journal roll
cb540 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66  back)..  */.  if
cb550 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a  ( rc==SQLITE_OK.
cb560 20 20 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63     && pFile->loc
cb570 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f  ktype<=SHARED_LO
cb580 43 4b 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 70  CK.   && locktyp
cb590 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e==RESERVED_LOCK
cb5a0 0a 20 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  .  ){.    pFile-
cb5b0 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d  >transCntrChng =
cb5c0 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64   0;.    pFile->d
cb5d0 62 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20  bUpdate = 0;.   
cb5e0 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c   pFile->inNormal
cb5f0 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a 23  Write = 1;.  }.#
cb600 65 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 63  endif...  if( rc
cb610 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
cb620 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
cb630 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
cb640 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
cb650 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
cb660 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74   }else if( lockt
cb670 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
cb680 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  OCK ){.    pFile
cb690 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e  ->locktype = PEN
cb6a0 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70  DING_LOCK;.    p
cb6b0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  Lock->locktype =
cb6c0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20   PENDING_LOCK;. 
cb6d0 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20   }..end_lock:.  
cb6e0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
cb6f0 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f  ;.  OSTRACE4("LO
cb700 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e  CK    %d %s %s\n
cb710 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
cb720 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79  ktypeName(lockty
cb730 70 65 29 2c 20 0a 20 20 20 20 20 20 72 63 3d 3d  pe), .      rc==
cb740 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22  SQLITE_OK ? "ok"
cb750 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20   : "failed");.  
cb760 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
cb770 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
cb780 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
cb790 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
cb7a0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
cb7b0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
cb7c0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
cb7d0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
cb7e0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
cb7f0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
cb800 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
cb810 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
cb820 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
cb830 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
cb840 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
cb850 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
cb860 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
cb870 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 6b  c int unixUnlock
cb880 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
cb890 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
cb8a0 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  {.  struct unixL
cb8b0 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 0a  ockInfo *pLock;.
cb8c0 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
cb8d0 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ock;.  int rc = 
cb8e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69  SQLITE_OK;.  uni
cb8f0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
cb900 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
cb910 69 6e 74 20 68 3b 0a 0a 20 20 61 73 73 65 72 74  int h;..  assert
cb920 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54  ( pFile );.  OST
cb930 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 25  RACE7("UNLOCK  %
cb940 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c 25  d %d was %d(%d,%
cb950 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  d) pid=%d\n", pF
cb960 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
cb970 2c 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ,.      pFile->l
cb980 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
cb990 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pLock->locktype,
cb9a0 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63   pFile->pLock->c
cb9b0 6e 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a  nt, getpid());..
cb9c0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
cb9d0 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
cb9e0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
cb9f0 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79  locktype<=lockty
cba00 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  pe ){.    return
cba10 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
cba20 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45    if( CHECK_THRE
cba30 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20  ADID(pFile) ){. 
cba40 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
cba50 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75  _MISUSE;.  }.  u
cba60 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
cba70 0a 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68 3b  .  h = pFile->h;
cba80 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65  .  pLock = pFile
cba90 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65 72  ->pLock;.  asser
cbaa0 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d 30  t( pLock->cnt!=0
cbab0 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   );.  if( pFile-
cbac0 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44  >locktype>SHARED
cbad0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73  _LOCK ){.    ass
cbae0 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  ert( pLock->lock
cbaf0 74 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f 63  type==pFile->loc
cbb00 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 53 69 6d  ktype );.    Sim
cbb10 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
cbb20 67 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 6c  gn(1);.    Simul
cbb30 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d  ateIOError( h=(-
cbb40 31 29 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 74  1) ).    Simulat
cbb50 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30  eIOErrorBenign(0
cbb60 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  );..#ifndef NDEB
cbb70 55 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72  UG.    /* When r
cbb80 65 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73  educing a lock s
cbb90 75 63 68 20 74 68 61 74 20 6f 74 68 65 72 20 70  uch that other p
cbba0 72 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74 61  rocesses can sta
cbbb0 72 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e  rt.    ** readin
cbbc0 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  g the database f
cbbd0 69 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20  ile again, make 
cbbe0 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 20 20  sure that the.  
cbbf0 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
cbc00 20 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64   counter was upd
cbc10 61 74 65 64 20 69 66 20 61 6e 79 20 70 61 72 74  ated if any part
cbc20 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
cbc30 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61  .    ** file cha
cbc40 6e 67 65 64 2e 20 20 49 66 20 74 68 65 20 74 72  nged.  If the tr
cbc50 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65  ansaction counte
cbc60 72 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64  r is not updated
cbc70 2c 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63  ,.    ** other c
cbc80 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68  onnections to th
cbc90 65 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68  e same file migh
cbca0 74 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68  t not realize th
cbcb0 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69  at.    ** the fi
cbcc0 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 61  le has changed a
cbcd0 6e 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e  nd hence might n
cbce0 6f 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68  ot know to flush
cbcf0 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61   their.    ** ca
cbd00 63 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66  che.  The use of
cbd10 20 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 63   a stale cache c
cbd20 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62  an lead to datab
cbd30 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  ase corruption..
cbd40 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
cbd50 74 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d  t( pFile->inNorm
cbd60 61 6c 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 20  alWrite==0.     
cbd70 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 62      || pFile->db
cbd80 55 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 20  Update==0.      
cbd90 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72 61     || pFile->tra
cbda0 6e 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29 3b  nsCntrChng==1 );
cbdb0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f  .    pFile->inNo
cbdc0 72 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a 23  rmalWrite = 0;.#
cbdd0 65 6e 64 69 66 0a 0a 0a 20 20 20 20 69 66 28 20  endif...    if( 
cbde0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
cbdf0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 6c  _LOCK ){.      l
cbe00 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52  ock.l_type = F_R
cbe10 44 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b  DLCK;.      lock
cbe20 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b  .l_whence = SEEK
cbe30 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b  _SET;.      lock
cbe40 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45  .l_start = SHARE
cbe50 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 6c  D_FIRST;.      l
cbe60 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52  ock.l_len = SHAR
cbe70 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 69  ED_SIZE;.      i
cbe80 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45  f( fcntl(h, F_SE
cbe90 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31  TLK, &lock)==(-1
cbea0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ) ){.        int
cbeb0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
cbec0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
cbed0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
cbee0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
cbef0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c  SQLITE_IOERR_RDL
cbf00 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66  OCK);.        if
cbf10 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
cbf20 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  rc) ){.         
cbf30 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
cbf40 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
cbf50 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f      }.        go
cbf60 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20  to end_unlock;. 
cbf70 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
cbf80 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46   lock.l_type = F
cbf90 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b  _UNLCK;.    lock
cbfa0 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b  .l_whence = SEEK
cbfb0 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  _SET;.    lock.l
cbfc0 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47  _start = PENDING
cbfd0 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e  _BYTE;.    lock.
cbfe0 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 73 73  l_len = 2L;  ass
cbff0 65 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 59 54  ert( PENDING_BYT
cc000 45 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f 42 59  E+1==RESERVED_BY
cc010 54 45 20 29 3b 0a 20 20 20 20 69 66 28 20 66 63  TE );.    if( fc
cc020 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  ntl(h, F_SETLK, 
cc030 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a  &lock)!=(-1) ){.
cc040 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63        pLock->loc
cc050 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c  ktype = SHARED_L
cc060 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  OCK;.    }else{.
cc070 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f        int tErrno
cc080 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
cc090 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
cc0a0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
cc0b0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
cc0c0 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20  ERR_UNLOCK);.   
cc0d0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
cc0e0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
cc0f0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
cc100 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
cc110 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
cc120 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20  o end_unlock;.  
cc130 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f    }.  }.  if( lo
cc140 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20  cktype==NO_LOCK 
cc150 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e  ){.    struct un
cc160 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  ixOpenCnt *pOpen
cc170 3b 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20  ;.    int rc2 = 
cc180 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 20 20  SQLITE_OK;..    
cc190 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  /* Decrement the
cc1a0 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75   shared lock cou
cc1b0 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74  nter.  Release t
cc1c0 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e  he lock using an
cc1d0 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20  .    ** OS call 
cc1e0 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68  only when all th
cc1f0 72 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61  reads in this sa
cc200 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20  me process have 
cc210 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20  released.    ** 
cc220 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f  the lock..    */
cc230 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d  .    pLock->cnt-
cc240 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b  -;.    if( pLock
cc250 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ->cnt==0 ){.    
cc260 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
cc270 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 6c  F_UNLCK;.      l
cc280 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53  ock.l_whence = S
cc290 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c  EEK_SET;.      l
cc2a0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c 6f  ock.l_start = lo
cc2b0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20  ck.l_len = 0L;. 
cc2c0 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
cc2d0 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20  rrorBenign(1);. 
cc2e0 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
cc2f0 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20  rror( h=(-1) ). 
cc300 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
cc310 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20  rrorBenign(0);. 
cc320 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68       if( fcntl(h
cc330 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
cc340 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  )!=(-1) ){.     
cc350 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
cc360 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20  pe = NO_LOCK;.  
cc370 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
cc380 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
cc390 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72  errno;.        r
cc3a0 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
cc3b0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
cc3c0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
cc3d0 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20  RR_UNLOCK);.    
cc3e0 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
cc3f0 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20  ERROR(rc) ){.   
cc400 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
cc410 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
cc420 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
cc430 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74      pLock->lockt
cc440 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20  ype = NO_LOCK;. 
cc450 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f         pFile->lo
cc460 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b  cktype = NO_LOCK
cc470 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
cc480 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e  .    /* Decremen
cc490 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c  t the count of l
cc4a0 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68 69  ocks against thi
cc4b0 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 68  s same file.  Wh
cc4c0 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f  en the.    ** co
cc4d0 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f  unt reaches zero
cc4e0 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65  , close any othe
cc4f0 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  r file descripto
cc500 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a 20  rs whose close. 
cc510 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72 72     ** was deferr
cc520 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f 75  ed because of ou
cc530 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e  tstanding locks.
cc540 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70 65  .    */.    pOpe
cc550 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  n = pFile->pOpen
cc560 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f  ;.    pOpen->nLo
cc570 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74  ck--;.    assert
cc580 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d  ( pOpen->nLock>=
cc590 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70  0 );.    if( pOp
cc5a0 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 26 26 20  en->nLock==0 && 
cc5b0 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3e  pOpen->nPending>
cc5c0 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  0 ){.      int i
cc5d0 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
cc5e0 20 69 3c 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69   i<pOpen->nPendi
cc5f0 6e 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ng; i++){.      
cc600 20 20 2f 2a 20 63 6c 6f 73 65 20 70 65 6e 64 69    /* close pendi
cc610 6e 67 20 66 64 73 2c 20 62 75 74 20 69 66 20 63  ng fds, but if c
cc620 6c 6f 73 69 6e 67 20 66 61 69 6c 73 20 64 6f 6e  losing fails don
cc630 27 74 20 66 72 65 65 20 74 68 65 20 61 72 72 61  't free the arra
cc640 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73 73  y.        ** ass
cc650 69 67 6e 20 2d 31 20 74 6f 20 74 68 65 20 73 75  ign -1 to the su
cc660 63 63 65 73 73 66 75 6c 6c 79 20 63 6c 6f 73 65  ccessfully close
cc670 64 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 6e  d descriptors an
cc680 64 20 72 65 63 6f 72 64 20 74 68 65 0a 20 20 20  d record the.   
cc690 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 2e 20 20       ** error.  
cc6a0 54 68 65 20 6e 65 78 74 20 61 74 74 65 6d 70 74  The next attempt
cc6b0 20 74 6f 20 75 6e 6c 6f 63 6b 20 77 69 6c 6c 20   to unlock will 
cc6c0 74 72 79 20 61 67 61 69 6e 2e 20 2a 2f 0a 20 20  try again. */.  
cc6d0 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d        if( pOpen-
cc6e0 3e 61 50 65 6e 64 69 6e 67 5b 69 5d 20 3c 20 30  >aPending[i] < 0
cc6f0 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
cc700 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 70       if( close(p
cc710 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b 69  Open->aPending[i
cc720 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ]) ){.          
cc730 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
cc740 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
cc750 20 20 20 20 72 63 32 20 3d 20 53 51 4c 49 54 45      rc2 = SQLITE
cc760 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20  _IOERR_CLOSE;.  
cc770 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
cc780 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50         pOpen->aP
cc790 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d 31 3b 0a  ending[i] = -1;.
cc7a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
cc7b0 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 32 3d  }.      if( rc2=
cc7c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
cc7d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
cc7e0 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69  ee(pOpen->aPendi
cc7f0 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70  ng);.        pOp
cc800 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d 20 30  en->nPending = 0
cc810 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d  ;.        pOpen-
cc820 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20  >aPending = 0;. 
cc830 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
cc840 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
cc850 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
cc860 20 72 63 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   rc2;.    }.  }.
cc870 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 20  ..end_unlock:.  
cc880 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
cc890 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
cc8a0 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c  TE_OK ) pFile->l
cc8b0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
cc8c0 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  pe;.  return rc;
cc8d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
cc8e0 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73  unction performs
cc8f0 20 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68   the parts of th
cc900 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f  e "close file" o
cc910 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d  peration .** com
cc920 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69  mon to all locki
cc930 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63  ng schemes. It c
cc940 6c 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74  loses the direct
cc950 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20  ory and file.** 
cc960 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79  handles, if they
cc970 20 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20   are valid, and 
cc980 73 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20  sets all fields 
cc990 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a  of the unixFile.
cc9a0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  ** structure to 
cc9b0 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2a  0..**.** It is *
cc9c0 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20 74  not* necessary t
cc9d0 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78  o hold the mutex
cc9e0 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
cc9f0 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a  ne is called,.**
cca00 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b 73   even on VxWorks
cca10 2e 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c 20  .  A mutex will 
cca20 62 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20 56  be acquired on V
cca30 78 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a 2a  xWorks by the.**
cca40 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46   vxworksReleaseF
cca50 69 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65 2e  ileId() routine.
cca60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
cca70 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c  loseUnixFile(sql
cca80 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a  ite3_file *id){.
cca90 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
ccaa0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
ccab0 64 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29  d;.  if( pFile )
ccac0 7b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d  {.    if( pFile-
ccad0 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20  >dirfd>=0 ){.   
ccae0 20 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f     int err = clo
ccaf0 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29  se(pFile->dirfd)
ccb00 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20  ;.      if( err 
ccb10 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
ccb20 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
ccb30 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74  rno;.        ret
ccb40 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
ccb50 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20  _DIR_CLOSE;.    
ccb60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
ccb70 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d 2d 31   pFile->dirfd=-1
ccb80 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
ccb90 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68      if( pFile->h
ccba0 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  >=0 ){.      int
ccbb0 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69   err = close(pFi
ccbc0 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 69 66  le->h);.      if
ccbd0 28 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20  ( err ){.       
ccbe0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
ccbf0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
ccc00 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ccc10 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20  _IOERR_CLOSE;.  
ccc20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20      }.    }.#if 
ccc30 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 69  OS_VXWORKS.    i
ccc40 66 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 29 7b  f( pFile->pId ){
ccc50 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65  .      if( pFile
ccc60 2d 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a 20 20  ->isDelete ){.  
ccc70 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46 69        unlink(pFi
ccc80 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69  le->pId->zCanoni
ccc90 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  calName);.      
ccca0 7d 0a 20 20 20 20 20 20 76 78 77 6f 72 6b 73 52  }.      vxworksR
cccb0 65 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46 69  eleaseFileId(pFi
cccc0 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 20 20  le->pId);.      
cccd0 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b 0a  pFile->pId = 0;.
ccce0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
cccf0 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45   OSTRACE2("CLOSE
ccd00 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c     %-3d\n", pFil
ccd10 65 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 6e 43  e->h);.    OpenC
ccd20 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20 20  ounter(-1);.    
ccd30 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 2c  memset(pFile, 0,
ccd40 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65   sizeof(unixFile
ccd50 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ));.  }.  return
ccd60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
ccd70 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c  *.** Close a fil
ccd80 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
ccd90 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 74   unixClose(sqlit
ccda0 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20  e3_file *id){.  
ccdb0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
ccdc0 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a  OK;.  if( id ){.
ccdd0 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46      unixFile *pF
ccde0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 20  ile = (unixFile 
ccdf0 2a 29 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 6e  *)id;.    unixUn
cce00 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b  lock(id, NO_LOCK
cce10 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72  );.    unixEnter
cce20 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28  Mutex();.    if(
cce30 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 26   pFile->pOpen &&
cce40 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e   pFile->pOpen->n
cce50 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Lock ){.      /*
cce60 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75   If there are ou
cce70 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c  tstanding locks,
cce80 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79   do not actually
cce90 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20   close the file 
ccea0 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 65  just.      ** ye
cceb0 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20 77  t because that w
ccec0 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 65  ould clear those
cced0 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 64   locks.  Instead
ccee0 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a 20  , add the file. 
ccef0 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 74       ** descript
ccf00 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61 50 65  or to pOpen->aPe
ccf10 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c 6c 20  nding.  It will 
ccf20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
ccf30 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20 20 20   closed when.   
ccf40 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 6c     ** the last l
ccf50 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a  ock is cleared..
ccf60 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
ccf70 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 20 20 20 20  nt *aNew;.      
ccf80 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
ccf90 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c  nt *pOpen = pFil
ccfa0 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20  e->pOpen;.      
ccfb0 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  aNew = sqlite3_r
ccfc0 65 61 6c 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 50  ealloc(pOpen->aP
ccfd0 65 6e 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e  ending, (pOpen->
ccfe0 6e 50 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65  nPending+1)*size
ccff0 6f 66 28 69 6e 74 29 20 29 3b 0a 20 20 20 20 20  of(int) );.     
cd000 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a   if( aNew==0 ){.
cd010 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20          /* If a 
cd020 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75  malloc fails, ju
cd030 73 74 20 6c 65 61 6b 20 74 68 65 20 66 69 6c 65  st leak the file
cd040 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
cd050 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
cd060 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64      pOpen->aPend
cd070 69 6e 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20  ing = aNew;.    
cd080 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64      pOpen->aPend
cd090 69 6e 67 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64  ing[pOpen->nPend
cd0a0 69 6e 67 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 3b  ing] = pFile->h;
cd0b0 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  .        pOpen->
cd0c0 6e 50 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 20  nPending++;.    
cd0d0 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d      pFile->h = -
cd0e0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
cd0f0 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b  .    releaseLock
cd100 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63  Info(pFile->pLoc
cd110 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f  k);.    releaseO
cd120 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f  penCnt(pFile->pO
cd130 70 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d 20 63  pen);.    rc = c
cd140 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29  loseUnixFile(id)
cd150 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d  ;.    unixLeaveM
cd160 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65  utex();.  }.  re
cd170 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a  turn rc;.}../***
cd180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
cd190 6f 66 20 74 68 65 20 70 6f 73 69 78 20 61 64 76  of the posix adv
cd1a0 69 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c 65  isory lock imple
cd1b0 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a  mentation ******
cd1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
cd1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a  **********/../**
cd220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
cd270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f  *********** No-o
cd290 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a  p Locking ******
cd2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
cd2c0 2a 2a 20 4f 66 20 74 68 65 20 76 61 72 69 6f 75  ** Of the variou
cd2d0 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d  s locking implem
cd2e0 65 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 6c 61  entations availa
cd2f0 62 6c 65 2c 20 74 68 69 73 20 69 73 20 62 79 20  ble, this is by 
cd300 66 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 6c  far the.** simpl
cd310 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 73  est:  locking is
cd320 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 74   ignored.  No at
cd330 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f  tempt is made to
cd340 20 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61   lock the databa
cd350 73 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 72  se.** file for r
cd360 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e  eading or writin
cd370 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f  g..**.** This lo
cd380 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 70  cking mode is ap
cd390 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73  propriate for us
cd3a0 65 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 64  e on read-only d
cd3b0 61 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 3a  atabases.** (ex:
cd3c0 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20   databases that 
cd3d0 61 72 65 20 62 75 72 6e 65 64 20 69 6e 74 6f 20  are burned into 
cd3e0 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 6d  CD-ROM, for exam
cd3f0 70 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a 2a  ple.)  It can.**
cd400 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 69 66   also be used if
cd410 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
cd420 20 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 78   employs some ex
cd430 74 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 6d  ternal mechanism
cd440 20 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 73   to.** prevent s
cd450 69 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 65  imultaneous acce
cd460 73 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 64  ss of the same d
cd470 61 74 61 62 61 73 65 20 62 79 20 74 77 6f 20 6f  atabase by two o
cd480 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 61  r more.** databa
cd490 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20  se connections. 
cd4a0 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20   But there is a 
cd4b0 73 65 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 20  serious risk of 
cd4c0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72  database.** corr
cd4d0 75 70 74 69 6f 6e 20 69 66 20 74 68 69 73 20 6c  uption if this l
cd4e0 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 75  ocking mode is u
cd4f0 73 65 64 20 69 6e 20 73 69 74 75 61 74 69 6f 6e  sed in situation
cd500 73 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65  s where multiple
cd510 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
cd520 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 61 63 63  nections are acc
cd530 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20  essing the same 
cd540 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74  database file at
cd550 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 6d   the same.** tim
cd560 65 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  e and one or mor
cd570 65 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65  e of those conne
cd580 63 74 69 6f 6e 73 20 61 72 65 20 77 72 69 74 69  ctions are writi
cd590 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 69  ng..*/..static i
cd5a0 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65  nt nolockCheckRe
cd5b0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
cd5c0 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64  e3_file *NotUsed
cd5d0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b  , int *pResOut){
cd5e0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
cd5f0 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
cd600 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20  *pResOut = 0;.  
cd610 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
cd620 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  ;.}.static int n
cd630 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65  olockLock(sqlite
cd640 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c  3_file *NotUsed,
cd650 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a   int NotUsed2){.
cd660 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
cd670 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
cd680 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e  Used2);.  return
cd690 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74   SQLITE_OK;.}.st
cd6a0 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55  atic int nolockU
cd6b0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
cd6c0 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74  le *NotUsed, int
cd6d0 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e   NotUsed2){.  UN
cd6e0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
cd6f0 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
cd700 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  2);.  return SQL
cd710 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
cd720 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e   Close the file.
cd730 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
cd740 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74  olockClose(sqlit
cd750 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20  e3_file *id) {. 
cd760 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69   return closeUni
cd770 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a  xFile(id);.}../*
cd780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd790 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 6f  ** End of the no
cd7a0 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65  -op lock impleme
cd7b0 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a  ntation ********
cd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
cd7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
cd820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
cd870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd880 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 64  ******** Begin d
cd890 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 20  ot-file Locking 
cd8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cd8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
cd8c0 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c 65  *.** The dotfile
cd8d0 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65   locking impleme
cd8e0 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65  ntation uses the
cd8f0 20 65 78 69 73 74 69 6e 67 20 6f 66 20 73 65 70   existing of sep
cd900 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69  arate lock.** fi
cd910 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  les in order to 
cd920 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74  control access t
cd930 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  o the database. 
cd940 20 54 68 69 73 20 77 6f 72 6b 73 20 6f 6e 20 6a   This works on j
cd950 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 65 76 65  ust.** about eve
cd960 72 79 20 66 69 6c 65 73 79 73 74 65 6d 20 69 6d  ry filesystem im
cd970 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 74 20 74  aginable.  But t
cd980 68 65 72 65 20 61 72 65 20 73 65 72 69 6f 75 73  here are serious
cd990 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a 2a   downsides:.**.*
cd9a0 2a 20 20 20 20 28 31 29 20 20 54 68 65 72 65 20  *    (1)  There 
cd9b0 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 72 72 65  is zero concurre
cd9c0 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 72  ncy.  A single r
cd9d0 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 61 6c 6c  eader blocks all
cd9e0 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20   other.**       
cd9f0 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72    connections fr
cda00 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72  om reading or wr
cda10 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  iting the databa
cda20 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29  se..**.**    (2)
cda30 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e    An application
cda40 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20   crash or power 
cda50 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20 73  loss can leave s
cda60 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0a  tale lock files.
cda70 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74 69  **         sitti
cda80 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20 6e  ng around that n
cda90 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65  eed to be cleare
cdaa0 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a  d manually..**.*
cdab0 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20  * Nevertheless, 
cdac0 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61 6e 20  a dotlock is an 
cdad0 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b  appropriate lock
cdae0 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75 73 65  ing mode for use
cdaf0 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 20   if no.** other 
cdb00 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79  locking strategy
cdb10 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a   is available..*
cdb20 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63  *.** Dotfile loc
cdb30 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 20 63 72  king works by cr
cdb40 65 61 74 69 6e 67 20 61 20 66 69 6c 65 20 69 6e  eating a file in
cdb50 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74   the same direct
cdb60 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a 20 64 61  ory as the.** da
cdb70 74 61 62 61 73 65 20 61 6e 64 20 77 69 74 68 20  tabase and with 
cdb80 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75  the same name bu
cdb90 74 20 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22  t with a ".lock"
cdba0 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 64 65 64   extension added
cdbb0 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61 6e  ..** The existan
cdbc0 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 66 69 6c  ce of a lock fil
cdbd0 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58 43  e implies an EXC
cdbe0 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41 6c  LUSIVE lock.  Al
cdbf0 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a 20  l other lock.** 
cdc00 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20 52  types (SHARED, R
cdc10 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e 47  ESERVED, PENDING
cdc20 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e 74  ) are mapped int
cdc30 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f 0a  o EXCLUSIVE..*/.
cdc40 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  ./*.** The file 
cdc50 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f 20  suffix added to 
cdc60 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66 69  the data base fi
cdc70 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72 20  lename in order 
cdc80 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a 2a  to create the.**
cdc90 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 23   lock file..*/.#
cdca0 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53  define DOTLOCK_S
cdcb0 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f  UFFIX ".lock"../
cdcc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
cdcd0 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
cdce0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
cdcf0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
cdd00 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
cdd10 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
cdd20 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
cdd30 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
cdd40 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52  is held, set *pR
cdd50 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f  esOut.** to a no
cdd60 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68  n-zero value oth
cdd70 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20  erwise *pResOut 
cdd80 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20  is set to zero. 
cdd90 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
cdda0 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  e.** is set to S
cddb0 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20  QLITE_OK unless 
cddc0 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  an I/O error occ
cddd0 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20  urs during lock 
cdde0 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  checking..**.** 
cddf0 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69  In dotfile locki
cde00 6e 67 2c 20 65 69 74 68 65 72 20 61 20 6c 6f 63  ng, either a loc
cde10 6b 20 65 78 69 73 74 73 20 6f 72 20 69 74 20 64  k exists or it d
cde20 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20  oes not.  So in 
cde30 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f  this.** variatio
cde40 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 65 72 76  n of CheckReserv
cde50 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f  edLock(), *pResO
cde60 75 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  ut is set to tru
cde70 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a  e if any lock.**
cde80 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20   is held on the 
cde90 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 65 20 69  file and false i
cdea0 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e  f the file is un
cdeb0 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  locked..*/.stati
cdec0 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65  c int dotlockChe
cded0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
cdee0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
cdef0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b   int *pResOut) {
cdf00 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
cdf10 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73  TE_OK;.  int res
cdf20 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69  erved = 0;.  uni
cdf30 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
cdf40 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  unixFile*)id;.. 
cdf50 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
cdf60 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
cdf70 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
cdf80 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20  VEDLOCK; );.  . 
cdf90 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
cdfa0 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
cdfb0 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69   a thread in thi
cdfc0 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  s process holds 
cdfd0 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20  such a lock */. 
cdfe0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
cdff0 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
ce000 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65   ){.    /* Eithe
ce010 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f  r this connectio
ce020 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  n or some other 
ce030 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68  connection in th
ce040 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 0a 20  e same process. 
ce050 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f     ** holds a lo
ce060 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20  ck on the file. 
ce070 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63   No need to chec
ce080 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20  k further. */.  
ce090 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
ce0a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
ce0b0 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  The lock is held
ce0c0 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20   if and only if 
ce0d0 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69  the lockfile exi
ce0e0 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  sts */.    const
ce0f0 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65   char *zLockFile
ce100 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
ce110 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
ce120 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 73 65 72  ntext;.    reser
ce130 76 65 64 20 3d 20 61 63 63 65 73 73 28 7a 4c 6f  ved = access(zLo
ce140 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20  ckFile, 0)==0;. 
ce150 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54   }.  OSTRACE4("T
ce160 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
ce170 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d %d\n", pFile->
ce180 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29  h, rc, reserved)
ce190 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72  ;.  *pResOut = r
ce1a0 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72  eserved;.  retur
ce1b0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
ce1c0 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74  ock the file wit
ce1d0 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69  h the lock speci
ce1e0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
ce1f0 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65  r locktype - one
ce200 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
ce210 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
ce220 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a  (1) SHARED_LOCK.
ce230 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52  **     (2) RESER
ce240 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  VED_LOCK.**     
ce250 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  (3) PENDING_LOCK
ce260 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c  .**     (4) EXCL
ce270 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a  USIVE_LOCK.**.**
ce280 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20   Sometimes when 
ce290 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c  requesting one l
ce2a0 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74  ock state, addit
ce2b0 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65  ional lock state
ce2c0 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65  s.** are inserte
ce2d0 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54  d in between.  T
ce2e0 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74  he locking might
ce2f0 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20   fail on one of 
ce300 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61  the later.** tra
ce310 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67  nsitions leaving
ce320 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20   the lock state 
ce330 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77  different from w
ce340 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62  hat it started b
ce350 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72  ut.** still shor
ce360 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20  t of its goal.  
ce370 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  The following ch
ce380 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c  art shows the al
ce390 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74  lowed.** transit
ce3a0 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73  ions and the ins
ce3b0 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61  erted intermedia
ce3c0 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  te states:.**.**
ce3d0 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20      UNLOCKED -> 
ce3e0 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41  SHARED.**    SHA
ce3f0 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a  RED -> RESERVED.
ce400 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
ce410 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
ce420 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53  LUSIVE.**    RES
ce430 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  ERVED -> (PENDIN
ce440 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
ce450 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e  **    PENDING ->
ce460 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a   EXCLUSIVE.**.**
ce470 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
ce480 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65  ll only increase
ce490 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68   a lock.  Use th
ce4a0 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63  e sqlite3OsUnloc
ce4b0 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  k().** routine t
ce4c0 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e  o lower a lockin
ce4d0 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57  g level..**.** W
ce4e0 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b  ith dotfile lock
ce4f0 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 20 6f  ing, we really o
ce500 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 61 74  nly support stat
ce510 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 56 45  e (4): EXCLUSIVE
ce520 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 61 63  ..** But we trac
ce530 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b  k the other lock
ce540 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 65 72  ing levels inter
ce550 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  nally..*/.static
ce560 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b   int dotlockLock
ce570 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
ce580 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
ce590 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
ce5a0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
ce5b0 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a  *)id;.  int fd;.
ce5c0 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c    char *zLockFil
ce5d0 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c  e = (char *)pFil
ce5e0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
ce5f0 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
ce600 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20  LITE_OK;...  /* 
ce610 49 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 6c  If we have any l
ce620 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f  ock, then the lo
ce630 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 64 79 20  ck file already 
ce640 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 77 65 20  exists.  All we 
ce650 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20  have.  ** to do 
ce660 69 73 20 61 64 6a 75 73 74 20 6f 75 72 20 69 6e  is adjust our in
ce670 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66  ternal record of
ce680 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e   the lock level.
ce690 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
ce6a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f  e->locktype > NO
ce6b0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69  _LOCK ){.    pFi
ce6c0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
ce6d0 6f 63 6b 74 79 70 65 3b 0a 23 69 66 20 21 4f 53  ocktype;.#if !OS
ce6e0 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 2f 2a 20  _VXWORKS.    /* 
ce6f0 41 6c 77 61 79 73 20 75 70 64 61 74 65 20 74 68  Always update th
ce700 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e 20 74  e timestamp on t
ce710 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a 20  he old file */. 
ce720 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46     utimes(zLockF
ce730 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64  ile, NULL);.#end
ce740 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  if.    return SQ
ce750 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a  LITE_OK;.  }.  .
ce760 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63    /* grab an exc
ce770 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20  lusive lock */. 
ce780 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b   fd = open(zLock
ce790 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f  File,O_RDONLY|O_
ce7a0 43 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30  CREAT|O_EXCL,060
ce7b0 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29  0);.  if( fd<0 )
ce7c0 7b 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20  {.    /* failed 
ce7d0 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74  to open/create t
ce7e0 68 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65  he file, someone
ce7f0 20 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 73   else may have s
ce800 74 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a  tolen the lock *
ce810 2f 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f  /.    int tErrno
ce820 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66   = errno;.    if
ce830 28 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72  ( EEXIST == tErr
ce840 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  no ){.      rc =
ce850 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
ce860 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
ce870 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
ce880 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
ce890 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
ce8a0 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  OERR_LOCK);.    
ce8b0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
ce8c0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
ce8d0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
ce8e0 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
ce8f0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
ce900 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 0a  return rc;.  } .
ce910 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 20    if( close(fd) 
ce920 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  ){.    pFile->la
ce930 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
ce940 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
ce950 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20  _IOERR_CLOSE;.  
ce960 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74  }.  .  /* got it
ce970 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61  , set the type a
ce980 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a  nd return ok */.
ce990 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
ce9a0 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
ce9b0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
ce9c0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
ce9d0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
ce9e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
ce9f0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
cea00 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
cea10 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
cea20 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
cea30 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
cea40 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
cea50 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
cea60 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
cea70 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
cea80 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
cea90 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
ceaa0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
ceab0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 68   no-op..**.** Wh
ceac0 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  en the locking l
cead0 65 76 65 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f  evel reaches NO_
ceae0 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 74 68 65  LOCK, delete the
ceaf0 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 73   lock file..*/.s
ceb00 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63  tatic int dotloc
ceb10 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  kUnlock(sqlite3_
ceb20 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
ceb30 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78  cktype) {.  unix
ceb40 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
ceb50 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 63  nixFile*)id;.  c
ceb60 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d  har *zLockFile =
ceb70 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e   (char *)pFile->
ceb80 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
ceb90 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
ceba0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22   );.  OSTRACE5("
cebb0 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61  UNLOCK  %d %d wa
cebc0 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20  s %d pid=%d\n", 
cebd0 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
cebe0 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c  pe,..   pFile->l
cebf0 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28  ocktype, getpid(
cec00 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f  ));.  assert( lo
cec10 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
cec20 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e  OCK );.  .  /* n
cec30 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65  o-op if possible
cec40 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
cec50 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74  >locktype==lockt
cec60 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
cec70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
cec80 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 72  ..  /* To downgr
cec90 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20 73  ade to shared, s
ceca0 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75 72  imply update our
cecb0 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f 6e   internal notion
cecc0 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63   of the.  ** loc
cecd0 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65 65  k state.  No nee
cece0 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20 74  d to mess with t
cecf0 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  he file on disk.
ced00 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
ced10 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
ced20 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
ced30 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  locktype = SHARE
ced40 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75  D_LOCK;.    retu
ced50 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
ced60 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 6c  }.  .  /* To ful
ced70 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61  ly unlock the da
ced80 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 74  tabase, delete t
ced90 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a  he lock file */.
ceda0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
cedb0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20  pe==NO_LOCK );. 
cedc0 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f 63   if( unlink(zLoc
cedd0 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69 6e  kFile) ){.    in
cede0 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65  t rc, tErrno = e
cedf0 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45 4e  rrno;.    if( EN
cee00 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 29  OENT != tErrno )
cee10 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
cee20 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
cee30 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
cee40 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
cee50 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  CK);.    }.    i
cee60 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
cee70 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46  (rc) ){.      pF
cee80 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
cee90 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20   tErrno;.    }. 
ceea0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20     return rc; . 
ceeb0 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   }.  pFile->lock
ceec0 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
ceed0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ceee0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  OK;.}../*.** Clo
ceef0 73 65 20 61 20 66 69 6c 65 2e 20 20 4d 61 6b 65  se a file.  Make
cef00 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 20 68   sure the lock h
cef10 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64  as been released
cef20 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67 2e   before closing.
cef30 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
cef40 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69  otlockClose(sqli
cef50 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a  te3_file *id) {.
cef60 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20    int rc;.  if( 
cef70 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69  id ){.    unixFi
cef80 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
cef90 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64  xFile*)id;.    d
cefa0 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c  otlockUnlock(id,
cefb0 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73   NO_LOCK);.    s
cefc0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c  qlite3_free(pFil
cefd0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
cefe0 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63  t);.  }.  rc = c
ceff0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29  loseUnixFile(id)
cf000 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
cf010 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
cf020 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  **** End of the 
cf030 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20 69 6d  dot-file lock im
cf040 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a  plementation ***
cf050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf060 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
cf070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
cf0b0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
cf0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf100 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
cf110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
cf120 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67  in flock Locking
cf130 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
cf140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cf150 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20  *.**.** Use the 
cf160 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 6d 20 63  flock() system c
cf170 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c 65 20 6c  all to do file l
cf180 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c  ocking..**.** fl
cf190 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 69 73  ock() locking is
cf1a0 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c 65 20 6c   like dot-file l
cf1b0 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 74 20 74  ocking in that t
cf1c0 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 66 69  he various.** fi
cf1d0 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b 69 6e 67  ne-grain locking
cf1e0 20 6c 65 76 65 6c 73 20 73 75 70 70 6f 72 74 65   levels supporte
cf1f0 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 20  d by SQLite are 
cf200 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 6f 0a 2a  collapsed into.*
cf210 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 63 6c 75  * a single exclu
cf220 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f  sive lock.  In o
cf230 74 68 65 72 20 77 6f 72 64 73 2c 20 53 48 41 52  ther words, SHAR
cf240 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 61 6e  ED, RESERVED, an
cf250 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 6c 6f 63  d.** PENDING loc
cf260 6b 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 20  ks are the same 
cf270 74 68 69 6e 67 20 61 73 20 61 6e 20 45 58 43 4c  thing as an EXCL
cf280 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 53 51 4c  USIVE lock.  SQL
cf290 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f 72  ite.** still wor
cf2a0 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 74  ks when you do t
cf2b0 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 75 72 72  his, but concurr
cf2c0 65 6e 63 79 20 69 73 20 72 65 64 75 63 65 64 20  ency is reduced 
cf2d0 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20  since.** only a 
cf2e0 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63  single process c
cf2f0 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 74 68  an be reading th
cf300 65 20 64 61 74 61 62 61 73 65 20 61 74 20 61 20  e database at a 
cf310 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74  time..**.** Omit
cf320 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20 69 66   this section if
cf330 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
cf340 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 20  OCKING_STYLE is 
cf350 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 69 66  turned off or if
cf360 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f  .** compiling fo
cf370 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69  r VXWORKS..*/.#i
cf380 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
cf390 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26  LOCKING_STYLE &&
cf3a0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a   !OS_VXWORKS../*
cf3b0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
cf3c0 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
cf3d0 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
cf3e0 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
cf3f0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
cf400 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
cf410 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
cf420 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
cf430 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65  s held, set *pRe
cf440 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e  sOut.** to a non
cf450 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65  -zero value othe
cf460 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69  rwise *pResOut i
cf470 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20  s set to zero.  
cf480 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
cf490 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51  .** is set to SQ
cf4a0 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61  LITE_OK unless a
cf4b0 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75  n I/O error occu
cf4c0 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63  rs during lock c
cf4d0 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  hecking..*/.stat
cf4e0 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63  ic int flockChec
cf4f0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
cf500 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
cf510 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20  int *pResOut){. 
cf520 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
cf530 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72  _OK;.  int reser
cf540 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46  ved = 0;.  unixF
cf550 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
cf560 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20  ixFile*)id;.  . 
cf570 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
cf580 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
cf590 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
cf5a0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20  VEDLOCK; );.  . 
cf5b0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
cf5c0 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20  ;.  .  /* Check 
cf5d0 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74  if a thread in t
cf5e0 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  his process hold
cf5f0 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f  s such a lock */
cf600 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
cf610 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f  cktype>SHARED_LO
cf620 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76  CK ){.    reserv
cf630 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20  ed = 1;.  }.  . 
cf640 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65   /* Otherwise se
cf650 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  e if some other 
cf660 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74  process holds it
cf670 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65  . */.  if( !rese
cf680 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61  rved ){.    /* a
cf690 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 68  ttempt to get th
cf6a0 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e  e lock */.    in
cf6b0 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46  t lrc = flock(pF
cf6c0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20  ile->h, LOCK_EX 
cf6d0 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 20  | LOCK_NB);.    
cf6e0 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 20 20 20  if( !lrc ){.    
cf6f0 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 6c 6f 63    /* got the loc
cf700 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a  k, unlock it */.
cf710 20 20 20 20 20 20 6c 72 63 20 3d 20 66 6c 6f 63        lrc = floc
cf720 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b  k(pFile->h, LOCK
cf730 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 66 20 28  _UN);.      if (
cf740 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 20 20 20   lrc ) {.       
cf750 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
cf760 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  rno;.        /* 
cf770 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77 69  unlock failed wi
cf780 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20  th an error */. 
cf790 20 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c         lrc = sql
cf7a0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
cf7b0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
cf7c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
cf7d0 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66  CK); .        if
cf7e0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
cf7f0 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  lrc) ){.        
cf800 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
cf810 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
cf820 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 3b         rc = lrc;
cf830 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
cf840 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a   }.    } else {.
cf850 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f        int tErrno
cf860 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
cf870 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
cf880 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65      /* someone e
cf890 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65 20 69  lse might have i
cf8a0 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20  t reserved */.  
cf8b0 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65      lrc = sqlite
cf8c0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
cf8d0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
cf8e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20  TE_IOERR_LOCK); 
cf8f0 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  .      if( IS_LO
cf900 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b  CK_ERROR(lrc) ){
cf910 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
cf920 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
cf930 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  no;.        rc =
cf940 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20   lrc;.      }.  
cf950 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43    }.  }.  OSTRAC
cf960 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E4("TEST WR-LOCK
cf970 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46   %d %d %d\n", pF
cf980 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65  ile->h, rc, rese
cf990 72 76 65 64 29 3b 0a 0a 23 69 66 64 65 66 20 53  rved);..#ifdef S
cf9a0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
cf9b0 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20  CK_LOCK_ERRORS. 
cf9c0 20 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54   if( (rc & SQLIT
cf9d0 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49  E_IOERR) == SQLI
cf9e0 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20  TE_IOERR ){.    
cf9f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
cfa00 20 20 20 20 72 65 73 65 72 76 65 64 3d 31 3b 0a      reserved=1;.
cfa10 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
cfa20 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43  LITE_IGNORE_FLOC
cfa30 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f  K_LOCK_ERRORS */
cfa40 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65  .  *pResOut = re
cfa50 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e  served;.  return
cfa60 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
cfa70 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68  ck the file with
cfa80 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66   the lock specif
cfa90 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
cfaa0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a   locktype - one.
cfab0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
cfac0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ing:.**.**     (
cfad0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a  1) SHARED_LOCK.*
cfae0 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56  *     (2) RESERV
cfaf0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
cfb00 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a  3) PENDING_LOCK.
cfb10 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
cfb20 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20  SIVE_LOCK.**.** 
cfb30 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
cfb40 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
cfb50 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
cfb60 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
cfb70 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
cfb80 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68   in between.  Th
cfb90 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20  e locking might 
cfba0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  fail on one of t
cfbb0 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e  he later.** tran
cfbc0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
cfbd0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
cfbe0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
cfbf0 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
cfc00 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  t.** still short
cfc10 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
cfc20 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
cfc30 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
cfc40 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69  owed.** transiti
cfc50 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65  ons and the inse
cfc60 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74  rted intermediat
cfc70 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
cfc80 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53     UNLOCKED -> S
cfc90 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  HARED.**    SHAR
cfca0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a  ED -> RESERVED.*
cfcb0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28  *    SHARED -> (
cfcc0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
cfcd0 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45  USIVE.**    RESE
cfce0 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  RVED -> (PENDING
cfcf0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
cfd00 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
cfd10 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20  EXCLUSIVE.**.** 
cfd20 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 65 61  flock() only rea
cfd30 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c  lly support EXCL
cfd40 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65  USIVE locks.  We
cfd50 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69   track intermedi
cfd60 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74  ate.** lock stat
cfd70 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  es in the sqlite
cfd80 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65  3_file structure
cfd90 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20  , but all locks 
cfda0 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f  SHARED or.** abo
cfdb0 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58  ve are really EX
cfdc0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e  CLUSIVE locks an
cfdd0 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74  d exclude all ot
cfde0 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66 72  her processes fr
cfdf0 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65  om.** access the
cfe00 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   file..**.** Thi
cfe10 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  s routine will o
cfe20 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c  nly increase a l
cfe30 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71  ock.  Use the sq
cfe40 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a  lite3OsUnlock().
cfe50 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f  ** routine to lo
cfe60 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65  wer a locking le
cfe70 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
cfe80 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c  nt flockLock(sql
cfe90 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
cfea0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
cfeb0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
cfec0 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
cfed0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
cfee0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72  le*)id;..  asser
cfef0 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f  t( pFile );..  /
cff00 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20  * if we already 
cff10 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20  have a lock, it 
cff20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a  is exclusive.  .
cff30 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74    ** Just adjust
cff40 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20   level and punt 
cff50 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a  on outta here. *
cff60 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c  /.  if (pFile->l
cff70 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43  ocktype > NO_LOC
cff80 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K) {.    pFile->
cff90 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
cffa0 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ype;.    return 
cffb0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
cffc0 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65   .  /* grab an e
cffd0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f  xclusive lock */
cffe0 0a 20 20 0a 20 20 69 66 20 28 66 6c 6f 63 6b 28  .  .  if (flock(
cfff0 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45  pFile->h, LOCK_E
d0000 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a  X | LOCK_NB)) {.
d0010 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
d0020 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64   errno;.    /* d
d0030 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74 20  idn't get, must 
d0040 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20 72  be busy */.    r
d0050 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d0060 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d0070 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d0080 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  RR_LOCK);.    if
d0090 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d00a0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69  rc) ){.      pFi
d00b0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d00c0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  tErrno;.    }.  
d00d0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20  } else {.    /* 
d00e0 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20  got it, set the 
d00f0 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20  type and return 
d0100 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d  ok */.    pFile-
d0110 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
d0120 74 79 70 65 3b 0a 20 20 7d 0a 20 20 4f 53 54 52  type;.  }.  OSTR
d0130 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE4("LOCK    %d
d0140 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65   %s %s\n", pFile
d0150 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d  ->h, locktypeNam
d0160 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20  e(locktype), .  
d0170 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c           rc==SQL
d0180 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20  ITE_OK ? "ok" : 
d0190 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 66 64 65  "failed");.#ifde
d01a0 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f  f SQLITE_IGNORE_
d01b0 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52  FLOCK_LOCK_ERROR
d01c0 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 51  S.  if( (rc & SQ
d01d0 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53  LITE_IOERR) == S
d01e0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20  QLITE_IOERR ){. 
d01f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
d0200 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  USY;.  }.#endif 
d0210 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  /* SQLITE_IGNORE
d0220 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
d0230 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  RS */.  return r
d0240 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77  c;.}.../*.** Low
d0250 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
d0260 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
d0270 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f  criptor pFile to
d0280 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b   locktype.  lock
d0290 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20  type.** must be 
d02a0 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f  either NO_LOCK o
d02b0 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a  r SHARED_LOCK..*
d02c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b  *.** If the lock
d02d0 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  ing level of the
d02e0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d02f0 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f   is already at o
d0300 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72  r below.** the r
d0310 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67  equested locking
d0320 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75   level, this rou
d0330 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
d0340 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
d0350 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  lockUnlock(sqlit
d0360 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d0370 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
d0380 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d0390 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d03a0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
d03b0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35  le );.  OSTRACE5
d03c0 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20  ("UNLOCK  %d %d 
d03d0 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22  was %d pid=%d\n"
d03e0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
d03f0 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20  type,.          
d0400 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d0410 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61  , getpid());.  a
d0420 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
d0430 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
d0440 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66    .  /* no-op if
d0450 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69   possible */.  i
d0460 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d0470 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe==locktype ){.
d0480 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d0490 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
d04a0 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 73  * shared can jus
d04b0 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 65  t be set because
d04c0 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 20   we always have 
d04d0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a  an exclusive */.
d04e0 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d    if (locktype==
d04f0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20  SHARED_LOCK) {. 
d0500 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d0510 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
d0520 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d0530 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  _OK;.  }.  .  /*
d0540 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c   no, really, unl
d0550 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ock. */.  int rc
d0560 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e   = flock(pFile->
d0570 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69  h, LOCK_UN);.  i
d0580 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69 6e 74  f (rc) {.    int
d0590 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72   r, tErrno = err
d05a0 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71 6c 69  no;.    r = sqli
d05b0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d05c0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d05d0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
d05e0 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c  K);.    if( IS_L
d05f0 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29 7b 0a  OCK_ERROR(r) ){.
d0600 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d0610 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d0620 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51  .    }.#ifdef SQ
d0630 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43  LITE_IGNORE_FLOC
d0640 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20  K_LOCK_ERRORS.  
d0650 20 20 69 66 28 20 28 72 20 26 20 53 51 4c 49 54    if( (r & SQLIT
d0660 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49  E_IOERR) == SQLI
d0670 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20  TE_IOERR ){.    
d0680 20 20 72 20 3d 20 53 51 4c 49 54 45 5f 42 55 53    r = SQLITE_BUS
d0690 59 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20  Y;.    }.#endif 
d06a0 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  /* SQLITE_IGNORE
d06b0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
d06c0 52 53 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 72  RS */.    .    r
d06d0 65 74 75 72 6e 20 72 3b 0a 20 20 7d 20 65 6c 73  eturn r;.  } els
d06e0 65 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  e {.    pFile->l
d06f0 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
d0700 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  K;.    return SQ
d0710 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a  LITE_OK;.  }.}..
d0720 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
d0730 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
d0740 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c  t flockClose(sql
d0750 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
d0760 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20  .  if( id ){.   
d0770 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c   flockUnlock(id,
d0780 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20   NO_LOCK);.  }. 
d0790 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69   return closeUni
d07a0 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23 65  xFile(id);.}..#e
d07b0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
d07c0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
d07d0 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52  YLE && !OS_VXWOR
d07e0 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  K */../*********
d07f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
d0800 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b  f the flock lock
d0810 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
d0820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0830 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
d0840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0880 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
d0890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d08d0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
d08e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
d08f0 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 6d 61  Begin Named Sema
d0900 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a  phore Locking **
d0910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d0920 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d  ******.**.** Nam
d0930 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ed semaphore loc
d0940 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 75 70  king is only sup
d0950 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f 72 6b  ported on VxWork
d0960 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f  s..**.** Semapho
d0970 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69  re locking is li
d0980 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20  ke dot-lock and 
d0990 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 69 74  flock in that it
d09a0 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20   really only.** 
d09b0 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 53 49  supports EXCLUSI
d09c0 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c  VE locking.  Onl
d09d0 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65  y a single proce
d09e0 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 20 77  ss can read or w
d09f0 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 74 61  rite.** the data
d0a00 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 20 74  base file at a t
d0a10 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 75 63  ime.  This reduc
d0a20 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 6f 6e  es potential con
d0a30 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a 2a 2a  currency, but.**
d0a40 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 6b 20   makes the lock 
d0a50 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d  implementation m
d0a60 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f 0a 23  uch easier..*/.#
d0a70 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f  if OS_VXWORKS../
d0a80 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
d0a90 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
d0aa0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
d0ab0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
d0ac0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
d0ad0 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
d0ae0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
d0af0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
d0b00 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52  is held, set *pR
d0b10 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f  esOut.** to a no
d0b20 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68  n-zero value oth
d0b30 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20  erwise *pResOut 
d0b40 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20  is set to zero. 
d0b50 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
d0b60 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  e.** is set to S
d0b70 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20  QLITE_OK unless 
d0b80 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  an I/O error occ
d0b90 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20  urs during lock 
d0ba0 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61  checking..*/.sta
d0bb0 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65 63 6b  tic int semCheck
d0bc0 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
d0bd0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d0be0 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20  nt *pResOut) {. 
d0bf0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d0c00 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72  _OK;.  int reser
d0c10 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46  ved = 0;.  unixF
d0c20 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d0c30 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53  ixFile*)id;..  S
d0c40 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
d0c50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
d0c60 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45  ERR_CHECKRESERVE
d0c70 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61  DLOCK; );.  .  a
d0c80 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
d0c90 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61  .  /* Check if a
d0ca0 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20   thread in this 
d0cb0 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75  process holds su
d0cc0 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69  ch a lock */.  i
d0cd0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d0ce0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  pe>SHARED_LOCK )
d0cf0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d  {.    reserved =
d0d00 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20   1;.  }.  .  /* 
d0d10 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66  Otherwise see if
d0d20 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
d0d30 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f  ess holds it. */
d0d40 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64  .  if( !reserved
d0d50 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20 2a 70   ){.    sem_t *p
d0d60 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70  Sem = pFile->pOp
d0d70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 20 20 73 74  en->pSem;.    st
d0d80 72 75 63 74 20 73 74 61 74 20 73 74 61 74 42 75  ruct stat statBu
d0d90 66 3b 0a 0a 20 20 20 20 69 66 28 20 73 65 6d 5f  f;..    if( sem_
d0da0 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d  trywait(pSem)==-
d0db0 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74  1 ){.      int t
d0dc0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d0dd0 20 20 20 20 20 69 66 28 20 45 41 47 41 49 4e 20       if( EAGAIN 
d0de0 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20  != tErrno ){.   
d0df0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d0e00 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d0e10 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d0e20 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
d0e30 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20  SERVEDLOCK);.   
d0e40 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d0e50 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d0e60 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20        } else {. 
d0e70 20 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e         /* someon
d0e80 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20 6c  e else has the l
d0e90 6f 63 6b 20 77 68 65 6e 20 77 65 20 61 72 65 20  ock when we are 
d0ea0 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20  in NO_LOCK */.  
d0eb0 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d        reserved =
d0ec0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
d0ed0 65 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29  e < SHARED_LOCK)
d0ee0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
d0ef0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 77 65  lse{.      /* we
d0f00 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74 20 69   could have it i
d0f10 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a 2f 0a  f we want it */.
d0f20 20 20 20 20 20 20 73 65 6d 5f 70 6f 73 74 28 70        sem_post(p
d0f30 53 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Sem);.    }.  }.
d0f40 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54    OSTRACE4("TEST
d0f50 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25   WR-LOCK %d %d %
d0f60 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
d0f70 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a  rc, reserved);..
d0f80 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73    *pResOut = res
d0f90 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20  erved;.  return 
d0fa0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  rc;.}../*.** Loc
d0fb0 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20  k the file with 
d0fc0 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  the lock specifi
d0fd0 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
d0fe0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a  locktype - one.*
d0ff0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
d1000 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
d1010 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
d1020 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
d1030 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
d1040 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
d1050 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
d1060 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
d1070 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
d1080 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
d1090 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
d10a0 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
d10b0 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
d10c0 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
d10d0 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
d10e0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
d10f0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
d1100 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
d1110 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
d1120 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
d1130 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
d1140 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
d1150 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
d1160 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
d1170 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
d1180 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
d1190 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
d11a0 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
d11b0 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
d11c0 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
d11d0 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
d11e0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
d11f0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
d1200 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
d1210 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
d1220 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
d1230 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
d1240 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
d1250 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53  XCLUSIVE.**.** S
d1260 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 6f  emaphore locks o
d1270 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f  nly really suppo
d1280 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  rt EXCLUSIVE loc
d1290 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e  ks.  We track in
d12a0 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f  termediate.** lo
d12b0 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68 65  ck states in the
d12c0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74   sqlite3_file st
d12d0 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c  ructure, but all
d12e0 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72   locks SHARED or
d12f0 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65  .** above are re
d1300 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c  ally EXCLUSIVE l
d1310 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65  ocks and exclude
d1320 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65   all other proce
d1330 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63  sses from.** acc
d1340 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a  ess the file..**
d1350 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
d1360 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65   will only incre
d1370 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65  ase a lock.  Use
d1380 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e   the sqlite3OsUn
d1390 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  lock().** routin
d13a0 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63  e to lower a loc
d13b0 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73  king level..*/.s
d13c0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c 6f 63  tatic int semLoc
d13d0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d13e0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
d13f0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
d1400 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d1410 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b  e*)id;.  int fd;
d1420 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d  .  sem_t *pSem =
d1430 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70   pFile->pOpen->p
d1440 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  Sem;.  int rc = 
d1450 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a  SQLITE_OK;..  /*
d1460 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68   if we already h
d1470 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69  ave a lock, it i
d1480 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20  s exclusive.  . 
d1490 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20   ** Just adjust 
d14a0 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f  level and punt o
d14b0 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f  n outta here. */
d14c0 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f  .  if (pFile->lo
d14d0 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b  cktype > NO_LOCK
d14e0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ) {.    pFile->l
d14f0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d1500 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  pe;.    rc = SQL
d1510 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74 6f  ITE_OK;.    goto
d1520 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20   sem_end_lock;. 
d1530 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 6b 20   }.  .  /* lock 
d1540 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62 75  semaphore now bu
d1550 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 6e 20  t bail out when 
d1560 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e 20  already locked. 
d1570 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 72 79  */.  if( sem_try
d1580 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29  wait(pSem)==-1 )
d1590 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
d15a0 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f  E_BUSY;.    goto
d15b0 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20   sem_end_lock;. 
d15c0 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c   }..  /* got it,
d15d0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e   set the type an
d15e0 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20  d return ok */. 
d15f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d1600 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 73   = locktype;.. s
d1610 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72  em_end_lock:.  r
d1620 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d1630 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63  ** Lower the loc
d1640 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69  king level on fi
d1650 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46  le descriptor pF
d1660 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e  ile to locktype.
d1670 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75    locktype.** mu
d1680 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f  st be either NO_
d1690 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c  LOCK or SHARED_L
d16a0 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  OCK..**.** If th
d16b0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
d16c0 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  of the file desc
d16d0 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64  riptor is alread
d16e0 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a  y at or below.**
d16f0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
d1700 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
d1710 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
d1720 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
d1730 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b 28 73   int semUnlock(s
d1740 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d1750 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
d1760 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d1770 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d1780 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65  id;.  sem_t *pSe
d1790 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  m = pFile->pOpen
d17a0 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 65 72  ->pSem;..  asser
d17b0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 73  t( pFile );.  as
d17c0 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a 20 20  sert( pSem );.  
d17d0 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b  OSTRACE5("UNLOCK
d17e0 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70    %d %d was %d p
d17f0 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  id=%d\n", pFile-
d1800 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20  >h, locktype,.. 
d1810 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d1820 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20  e, getpid());.  
d1830 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
d1840 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
d1850 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69  .  .  /* no-op i
d1860 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20  f possible */.  
d1870 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
d1880 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype==locktype ){
d1890 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d18a0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d18b0 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75  /* shared can ju
d18c0 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73  st be set becaus
d18d0 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65  e we always have
d18e0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f   an exclusive */
d18f0 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d  .  if (locktype=
d1900 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a  =SHARED_LOCK) {.
d1910 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d1920 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d1930 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d1940 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
d1950 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e 6c  * no, really unl
d1960 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20 73  ock. */.  if ( s
d1970 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d 2d  em_post(pSem)==-
d1980 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63  1 ) {.    int rc
d1990 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  , tErrno = errno
d19a0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
d19b0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
d19c0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
d19d0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
d19e0 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  );.    if( IS_LO
d19f0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
d1a00 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d1a10 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d1a20 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
d1a30 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69  n rc; .  }.  pFi
d1a40 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e  le->locktype = N
d1a50 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e  O_LOCK;.  return
d1a60 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
d1a70 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  *. ** Close a fi
d1a80 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69  le.. */.static i
d1a90 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c 69  nt semClose(sqli
d1aa0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a  te3_file *id) {.
d1ab0 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20    if( id ){.    
d1ac0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d1ad0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d1ae0 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 69  .    semUnlock(i
d1af0 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20  d, NO_LOCK);.   
d1b00 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d1b10 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d  ;.    unixEnterM
d1b20 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c 65  utex();.    rele
d1b30 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c  aseLockInfo(pFil
d1b40 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72  e->pLock);.    r
d1b50 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 46  eleaseOpenCnt(pF
d1b60 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20  ile->pOpen);.   
d1b70 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
d1b80 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78  );.    closeUnix
d1b90 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20  File(id);.  }.  
d1ba0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d1bb0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f  ;.}..#endif /* O
d1bc0 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 0a  S_VXWORKS */./*.
d1bd0 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f  ** Named semapho
d1be0 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e  re locking is on
d1bf0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  ly available on 
d1c00 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a 2a  VxWorks..**.****
d1c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
d1c20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d  of the named sem
d1c30 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d 70 6c  aphore lock impl
d1c40 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a  ementation *****
d1c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
d1c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a  **********/.../*
d1cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d1d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
d1d20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a  AFP Locking ****
d1d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d1d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d1d50 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 20 41  .** AFP is the A
d1d60 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 6f 74  pple Filing Prot
d1d70 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 61 20  ocol.  AFP is a 
d1d80 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73 74  network filesyst
d1d90 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 41  em found.** on A
d1da0 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20 63  pple Macintosh c
d1db0 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 68 20  omputers - both 
d1dc0 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a 2a 0a  OS9 and OSX..**.
d1dd0 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20 69  ** Third-party i
d1de0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
d1df0 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c 61  f AFP are availa
d1e00 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20 63  ble.  But this c
d1e10 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e 6c 79  ode here.** only
d1e20 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0a 2a   works on OSX..*
d1e30 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  /..#if defined(_
d1e40 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
d1e50 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
d1e60 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54  NG_STYLE./*.** T
d1e70 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  he afpLockingCon
d1e80 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20 63  text structure c
d1e90 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 70 20  ontains all afp 
d1ea0 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 73 74  lock specific st
d1eb0 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ate.*/.typedef s
d1ec0 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67  truct afpLocking
d1ed0 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 6b 69  Context afpLocki
d1ee0 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63  ngContext;.struc
d1ef0 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  t afpLockingCont
d1f00 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ext {.  unsigned
d1f10 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72 65   long long share
d1f20 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20 63  dByte;.  const c
d1f30 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 20  har *dbPath;    
d1f40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
d1f50 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c   of the open fil
d1f60 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 20  e */.};..struct 
d1f70 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
d1f80 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f  .{.  unsigned lo
d1f90 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b 20  ng long offset; 
d1fa0 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74         /* offset
d1fb0 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 74   to first byte t
d1fc0 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69  o lock */.  unsi
d1fd0 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c  gned long long l
d1fe0 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f 2a  ength;        /*
d1ff0 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 6f   nbr of bytes to
d2000 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67   lock */.  unsig
d2010 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
d2020 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a 20  tRangeStart; /* 
d2030 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65 20  nbr of 1st byte 
d2040 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65 73  locked if succes
d2050 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  sful */.  unsign
d2060 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46 6c  ed char unLockFl
d2070 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 31  ag;         /* 1
d2080 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c   = unlock, 0 = l
d2090 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ock */.  unsigne
d20a0 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64 46  d char startEndF
d20b0 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31 3d  lag;       /* 1=
d20c0 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66 6f  rel to end of fo
d20d0 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74 61  rk, 0=rel to sta
d20e0 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20  rt */.  int fd; 
d20f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d2100 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c            /* fil
d2110 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63 20  e desc to assoc 
d2120 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20 2a  this lock with *
d2130 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61 66  /.};..#define af
d2140 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b  pfsByteRangeLock
d2150 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f 49  2FSCTL        _I
d2160 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72  OWR('z', 23, str
d2170 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63  uct ByteRangeLoc
d2180 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  kPB2)../*.** Thi
d2190 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 66  s is a utility f
d21a0 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63 6c  or setting or cl
d21b0 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61 6e  earing a bit-ran
d21c0 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a 2a  ge lock on an.**
d21d0 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 2e   AFP filesystem.
d21e0 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 53  .** .** Return S
d21f0 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
d2200 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53 59  ess, SQLITE_BUSY
d2210 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   on failure..*/.
d2220 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53 65  static int afpSe
d2230 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20 63  tLock(.  const c
d2240 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 20  har *path,      
d2250 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
d2260 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  of the file to b
d2270 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c 6f  e locked or unlo
d2280 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46 69  cked */.  unixFi
d2290 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20  le *pFile,      
d22a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
d22b0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d22c0 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75 6e   on path */.  un
d22d0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
d22e0 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a 20   offset,     /* 
d22f0 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62 65  First byte to be
d2300 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 73   locked */.  uns
d2310 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
d2320 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20 4e  length,     /* N
d2330 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
d2340 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20  o lock */.  int 
d2350 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20 20  setLockFlag     
d2360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
d2370 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e 20  ue to set lock. 
d2380 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72 20   False to clear 
d2390 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74 72  lock */.){.  str
d23a0 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63  uct ByteRangeLoc
d23b0 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20 65  kPB2 pb;.  int e
d23c0 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c 6f  rr;.  .  pb.unLo
d23d0 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63 6b  ckFlag = setLock
d23e0 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20 20  Flag ? 0 : 1;.  
d23f0 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67 20  pb.startEndFlag 
d2400 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65 74  = 0;.  pb.offset
d2410 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62 2e   = offset;.  pb.
d2420 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68 3b  length = length;
d2430 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69 6c   .  pb.fd = pFil
d2440 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52 41  e->h;.  .  OSTRA
d2450 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 4b 20  CE6("AFPSETLOCK 
d2460 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69 6e  [%s] for %d%s in
d2470 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c 78   range %llx:%llx
d2480 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c 6f  \n", .    (setLo
d2490 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 46  ckFlag?"ON":"OFF
d24a0 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28 70  "), pFile->h, (p
d24b0 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74 76  b.fd==-1?"[testv
d24c0 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20 20  al-1]":""),.    
d24d0 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 3b  offset, length);
d24e0 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28 70  .  err = fsctl(p
d24f0 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 52 61  ath, afpfsByteRa
d2500 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 26  ngeLock2FSCTL, &
d2510 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 65  pb, 0);.  if ( e
d2520 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69  rr==-1 ) {.    i
d2530 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 20 74  nt rc;.    int t
d2540 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d2550 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 46 50     OSTRACE4("AFP
d2560 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 20 74  SETLOCK failed t
d2570 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 20 25  o fsctl() '%s' %
d2580 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  d %s\n",.       
d2590 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 72 72        path, tErr
d25a0 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 45 72  no, strerror(tEr
d25b0 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 53 51  rno));.#ifdef SQ
d25c0 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f  LITE_IGNORE_AFP_
d25d0 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20  LOCK_ERRORS.    
d25e0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
d25f0 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20 3d  ;.#else.    rc =
d2600 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d2610 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d2620 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o,.             
d2630 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b 46 6c         setLockFl
d2640 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45 52  ag ? SQLITE_IOER
d2650 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45 5f  R_LOCK : SQLITE_
d2660 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 23  IOERR_UNLOCK);.#
d2670 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
d2680 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f  IGNORE_AFP_LOCK_
d2690 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 69 66  ERRORS */.    if
d26a0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d26b0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69  rc) ){.      pFi
d26c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d26d0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  tErrno;.    }.  
d26e0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
d26f0 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75   else {.    retu
d2700 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d2710 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
d2720 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
d2730 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
d2740 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
d2750 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
d2760 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
d2770 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
d2780 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
d2790 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73   lock is held, s
d27a0 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74  et *pResOut.** t
d27b0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
d27c0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52  ue otherwise *pR
d27d0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
d27e0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72  zero.  The retur
d27f0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  n value.** is se
d2800 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75  t to SQLITE_OK u
d2810 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72  nless an I/O err
d2820 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
d2830 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a   lock checking..
d2840 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66  */.static int af
d2850 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  pCheckReservedLo
d2860 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d2870 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
d2880 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
d2890 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
d28a0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20  reserved = 0;.  
d28b0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d28c0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d28d0 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  .  .  SimulateIO
d28e0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
d28f0 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
d2900 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b  RESERVEDLOCK; );
d2910 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46  .  .  assert( pF
d2920 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63 6b  ile );.  afpLock
d2930 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  ingContext *cont
d2940 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e  ext = (afpLockin
d2950 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c  gContext *) pFil
d2960 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
d2970 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b  t;.  .  /* Check
d2980 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
d2990 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
d29a0 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
d29b0 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d29c0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
d29d0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72  OCK ){.    reser
d29e0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a  ved = 1;.  }.  .
d29f0 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73    /* Otherwise s
d2a00 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  ee if some other
d2a10 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69   process holds i
d2a20 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 21  t..   */.  if( !
d2a30 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20  reserved ){.    
d2a40 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53 45  /* lock the RESE
d2a50 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20 20  RVED byte */.   
d2a60 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 53 65   int lrc = afpSe
d2a70 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
d2a80 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45  bPath, pFile, RE
d2a90 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31  SERVED_BYTE, 1,1
d2aa0 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 51 4c  );  .    if( SQL
d2ab0 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20  ITE_OK==lrc ){. 
d2ac0 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73 75       /* if we su
d2ad0 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69 6e  cceeded in takin
d2ae0 67 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c  g the reserved l
d2af0 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 74  ock, unlock it t
d2b00 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20 20  o restore.      
d2b10 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  ** the original 
d2b20 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20 6c  state */.      l
d2b30 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  rc = afpSetLock(
d2b40 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c  context->dbPath,
d2b50 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44   pFile, RESERVED
d2b60 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20  _BYTE, 1, 0);.  
d2b70 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
d2b80 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65 64   /* if we failed
d2b90 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b   to get the lock
d2ba0 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c   then someone el
d2bb0 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74 20  se must have it 
d2bc0 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 76 65  */.      reserve
d2bd0 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  d = 1;.    }.   
d2be0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d2bf0 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20  OR(lrc) ){.     
d2c00 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d 0a 20   rc=lrc;.    }. 
d2c10 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 34   }.  .  OSTRACE4
d2c20 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25  ("TEST WR-LOCK %
d2c30 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c  d %d %d\n", pFil
d2c40 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76  e->h, rc, reserv
d2c50 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65 73 4f  ed);.  .  *pResO
d2c60 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20  ut = reserved;. 
d2c70 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
d2c80 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  *.** Lock the fi
d2c90 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
d2ca0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
d2cb0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65  rameter locktype
d2cc0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
d2cd0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
d2ce0 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
d2cf0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
d2d00 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
d2d10 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
d2d20 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
d2d30 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
d2d40 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
d2d50 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
d2d60 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
d2d70 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
d2d80 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
d2d90 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
d2da0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
d2db0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
d2dc0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
d2dd0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
d2de0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
d2df0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
d2e00 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
d2e10 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
d2e20 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
d2e30 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
d2e40 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
d2e50 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
d2e60 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
d2e70 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
d2e80 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
d2e90 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
d2ea0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
d2eb0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
d2ec0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
d2ed0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
d2ee0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
d2ef0 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
d2f00 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
d2f10 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
d2f20 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
d2f30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
d2f40 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
d2f50 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
d2f60 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f  Use the sqlite3O
d2f70 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75  sUnlock().** rou
d2f80 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20  tine to lower a 
d2f90 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a  locking level..*
d2fa0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70  /.static int afp
d2fb0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
d2fc0 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
d2fd0 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ype){.  int rc =
d2fe0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e   SQLITE_OK;.  un
d2ff0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
d3000 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
d3010 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
d3020 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61  xt *context = (a
d3030 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
d3040 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69   *) pFile->locki
d3050 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20  ngContext;.  .  
d3060 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d3070 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43  .  OSTRACE5("LOC
d3080 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 25  K    %d %s was %
d3090 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69  s pid=%d\n", pFi
d30a0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
d30b0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
d30c0 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65  ktype), locktype
d30d0 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  Name(pFile->lock
d30e0 74 79 70 65 29 2c 20 67 65 74 70 69 64 28 29 29  type), getpid())
d30f0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
d3100 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f   is already a lo
d3110 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ck of this type 
d3120 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74  or more restrict
d3130 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ive on the.  ** 
d3140 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74  unixFile, do not
d3150 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
d3160 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  the afp_end_lock
d3170 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a  : exit path, as.
d3180 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75    ** unixEnterMu
d3190 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 65  tex() hasn't bee
d31a0 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20  n called yet..  
d31b0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
d31c0 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79  locktype>=lockty
d31d0 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43  pe ){.    OSTRAC
d31e0 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25  E3("LOCK    %d %
d31f0 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65  s ok (already he
d3200 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  ld)\n", pFile->h
d3210 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63  ,.           loc
d3220 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79  ktypeName(lockty
d3230 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  pe));.    return
d3240 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
d3250 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
d3260 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75  the locking sequ
d3270 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a  ence is correct.
d3280 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
d3290 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d  File->locktype!=
d32a0 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74  NO_LOCK || lockt
d32b0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d32c0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
d32d0 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f  cktype!=PENDING_
d32e0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
d32f0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45  ( locktype!=RESE
d3300 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69  RVED_LOCK || pFi
d3310 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  le->locktype==SH
d3320 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a  ARED_LOCK );.  .
d3330 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20    /* This mutex 
d3340 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73  is needed becaus
d3350 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69  e pFile->pLock i
d3360 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20  s shared across 
d3370 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 75  threads.  */.  u
d3380 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
d3390 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
d33a0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
d33b0 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 69  ead owns the pFi
d33c0 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  le..  */.  rc = 
d33d0 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69  transferOwnershi
d33e0 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  p(pFile);.  if( 
d33f0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
d3400 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  .    unixLeaveMu
d3410 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72  tex();.    retur
d3420 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a 20  n rc;.  }.    . 
d3430 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f   /* A PENDING lo
d3440 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66  ck is needed bef
d3450 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20  ore acquiring a 
d3460 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20  SHARED lock and 
d3470 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75  before.  ** acqu
d3480 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49  iring an EXCLUSI
d3490 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68  VE lock.  For th
d34a0 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74  e SHARED lock, t
d34b0 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a  he PENDING will.
d34c0 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64    ** be released
d34d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  ..  */.  if( loc
d34e0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d34f0 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f  CK .      || (lo
d3500 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
d3510 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d  E_LOCK && pFile-
d3520 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e  >locktype<PENDIN
d3530 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20  G_LOCK).  ){.   
d3540 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20 20   int failed;.   
d3550 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74   failed = afpSet
d3560 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
d3570 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e  Path, pFile, PEN
d3580 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 31 29  DING_BYTE, 1, 1)
d3590 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c 65 64  ;.    if (failed
d35a0 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66  ) {.      rc = f
d35b0 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 67 6f 74  ailed;.      got
d35c0 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  o afp_end_lock;.
d35d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f      }.  }.  .  /
d35e0 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74  * If control get
d35f0 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c  s to this point,
d3600 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67   then actually g
d3610 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65  o ahead and make
d3620 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20  .  ** operating 
d3630 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72  system calls for
d3640 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c   the specified l
d3650 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ock..  */.  if( 
d3660 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
d3670 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74  _LOCK ){.    int
d3680 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 63 32 2c   lk, lrc1, lrc2,
d3690 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20   lrc1Errno;.    
d36a0 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20  .    /* Now get 
d36b0 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 48  the read-lock SH
d36c0 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20  ARED_LOCK */.   
d36d0 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 68   /* note that th
d36e0 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68 65  e quality of the
d36f0 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 73   randomness does
d3700 6e 27 74 20 6d 61 74 74 65 72 20 74 68 61 74 20  n't matter that 
d3710 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d  much */.    lk =
d3720 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 20   random(); .    
d3730 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42  context->sharedB
d3740 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66  yte = (lk & 0x7f
d3750 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f  ffffff)%(SHARED_
d3760 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c  SIZE - 1);.    l
d3770 72 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  rc1 = afpSetLock
d3780 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
d3790 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20  , pFile, .      
d37a0 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54      SHARED_FIRST
d37b0 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64  +context->shared
d37c0 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20  Byte, 1, 1);.   
d37d0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d37e0 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20  OR(lrc1) ){.    
d37f0 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46    lrc1Errno = pF
d3800 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a  ile->lastErrno;.
d3810 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f      }.    /* Dro
d3820 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  p the temporary 
d3830 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a  PENDING lock */.
d3840 20 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65      lrc2 = afpSe
d3850 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
d3860 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45  bPath, pFile, PE
d3870 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30  NDING_BYTE, 1, 0
d3880 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20  );.    .    if( 
d3890 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72  IS_LOCK_ERROR(lr
d38a0 63 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46  c1) ) {.      pF
d38b0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d38c0 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20   lrc1Errno;.    
d38d0 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20    rc = lrc1;.   
d38e0 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f     goto afp_end_
d38f0 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65  lock;.    } else
d3900 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d3910 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 20  OR(lrc2) ){.    
d3920 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20    rc = lrc2;.   
d3930 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f     goto afp_end_
d3940 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65  lock;.    } else
d3950 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51 4c   if( lrc1 != SQL
d3960 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20  ITE_OK ) {.     
d3970 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20   rc = lrc1;.    
d3980 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70  } else {.      p
d3990 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d39a0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
d39b0 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e      pFile->pOpen
d39c0 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d  ->nLock++;.    }
d39d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
d39e0 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73   The request was
d39f0 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20   for a RESERVED 
d3a00 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  or EXCLUSIVE loc
d3a10 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a  k.  It is.    **
d3a20 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
d3a30 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20  ere is a SHARED 
d3a40 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20  or greater lock 
d3a50 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20  on the file.    
d3a60 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20  ** already..    
d3a70 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65  */.    int faile
d3a80 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72  d = 0;.    asser
d3a90 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63  t( 0!=pFile->loc
d3aa0 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20  ktype );.    if 
d3ab0 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53  (locktype >= RES
d3ac0 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46  ERVED_LOCK && pF
d3ad0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20  ile->locktype < 
d3ae0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b  RESERVED_LOCK) {
d3af0 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75  .        /* Acqu
d3b00 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c  ire a RESERVED l
d3b10 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  ock */.        f
d3b20 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f  ailed = afpSetLo
d3b30 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61  ck(context->dbPa
d3b40 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52  th, pFile, RESER
d3b50 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a  VED_BYTE, 1,1);.
d3b60 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66      }.    if (!f
d3b70 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70  ailed && locktyp
d3b80 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c  e == EXCLUSIVE_L
d3b90 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20  OCK) {.      /* 
d3ba0 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55  Acquire an EXCLU
d3bb0 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  SIVE lock */.   
d3bc0 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52       .      /* R
d3bd0 65 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65 64  emove the shared
d3be0 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79   lock before try
d3bf0 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20  ing the range.  
d3c00 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20  we'll need to . 
d3c10 20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c       ** reestabl
d3c20 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c  ish the shared l
d3c30 6f 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20  ock if we can't 
d3c40 67 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f  get the  afpUnlo
d3c50 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ck.      */.    
d3c60 20 20 69 66 28 20 21 28 66 61 69 6c 65 64 20 3d    if( !(failed =
d3c70 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
d3c80 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
d3c90 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54  le, SHARED_FIRST
d3ca0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   +.             
d3cb0 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74              cont
d3cc0 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c  ext->sharedByte,
d3cd0 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 20   1, 0)) ){.     
d3ce0 20 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20 3d     int failed2 =
d3cf0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
d3d00 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d      /* now attem
d3d10 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 65  mpt to get the e
d3d20 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61  xclusive lock ra
d3d30 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  nge */.        f
d3d40 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f  ailed = afpSetLo
d3d50 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61  ck(context->dbPa
d3d60 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45  th, pFile, SHARE
d3d70 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20 20  D_FIRST, .      
d3d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d3d90 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f           SHARED_
d3da0 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 20  SIZE, 1);.      
d3db0 20 20 69 66 28 20 66 61 69 6c 65 64 20 26 26 20    if( failed && 
d3dc0 28 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65  (failed2 = afpSe
d3dd0 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
d3de0 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20  bPath, pFile, . 
d3df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d3e00 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52        SHARED_FIR
d3e10 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68  ST + context->sh
d3e20 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29  aredByte, 1, 1))
d3e30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
d3e40 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69   Can't reestabli
d3e50 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f  sh the shared lo
d3e60 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27  ck.  Sqlite can'
d3e70 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0a  t deal, this is.
d3e80 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63            ** a c
d3e90 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f  ritical I/O erro
d3ea0 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  r.          */. 
d3eb0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 28 28           rc = ((
d3ec0 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45 5f  failed & SQLITE_
d3ed0 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45  IOERR) == SQLITE
d3ee0 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 64  _IOERR) ? failed
d3ef0 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20 20  2 : .           
d3f00 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52      SQLITE_IOERR
d3f10 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 20  _LOCK;.         
d3f20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f   goto afp_end_lo
d3f30 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20  ck;.        } . 
d3f40 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
d3f50 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b      rc = failed;
d3f60 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a   .      }.    }.
d3f70 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 29      if( failed )
d3f80 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61 69  {.      rc = fai
d3f90 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  led;.    }.  }. 
d3fa0 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49   .  if( rc==SQLI
d3fb0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69  TE_OK ){.    pFi
d3fc0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d3fd0 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65  ocktype;.  }else
d3fe0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45   if( locktype==E
d3ff0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
d4000 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d4010 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c  type = PENDING_L
d4020 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f  OCK;.  }.  .afp_
d4030 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78  end_lock:.  unix
d4040 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
d4050 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20  OSTRACE4("LOCK  
d4060 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70    %d %s %s\n", p
d4070 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
d4080 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c  eName(locktype),
d4090 20 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53   .         rc==S
d40a0 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20  QLITE_OK ? "ok" 
d40b0 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72  : "failed");.  r
d40c0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d40d0 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63  ** Lower the loc
d40e0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69  king level on fi
d40f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46  le descriptor pF
d4100 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e  ile to locktype.
d4110 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75    locktype.** mu
d4120 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f  st be either NO_
d4130 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c  LOCK or SHARED_L
d4140 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  OCK..**.** If th
d4150 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
d4160 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  of the file desc
d4170 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64  riptor is alread
d4180 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a  y at or below.**
d4190 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
d41a0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
d41b0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
d41c0 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
d41d0 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73   int afpUnlock(s
d41e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d41f0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
d4200 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d4210 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c  TE_OK;.  unixFil
d4220 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
d4230 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c  File*)id;.  afpL
d4240 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
d4250 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e  Ctx = (afpLockin
d4260 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c  gContext *) pFil
d4270 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
d4280 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  t;..  assert( pF
d4290 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  ile );.  OSTRACE
d42a0 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64  5("UNLOCK  %d %d
d42b0 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e   was %d pid=%d\n
d42c0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
d42d0 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20  ktype,.         
d42e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pFile->locktype,
d42f0 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61   getpid());..  a
d4300 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
d4310 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
d4320 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
d4330 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20  ktype<=locktype 
d4340 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
d4350 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
d4360 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49  f( CHECK_THREADI
d4370 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20  D(pFile) ){.    
d4380 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
d4390 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78  SUSE;.  }.  unix
d43a0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
d43b0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
d43c0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
d43d0 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20  ){.    .    if( 
d43e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
d43f0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
d4400 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66  ){.      rc = af
d4410 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64  pSetLock(pCtx->d
d4420 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48  bPath, pFile, SH
d4430 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41 52  ARED_FIRST, SHAR
d4440 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20  ED_SIZE, 0);.   
d4450 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
d4460 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65  E_OK && locktype
d4470 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
d4480 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79  .        /* only
d4490 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 68   re-establish th
d44a0 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66  e shared lock if
d44b0 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20   necessary */.  
d44c0 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65 64        int shared
d44d0 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45  LockByte = SHARE
d44e0 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68  D_FIRST+pCtx->sh
d44f0 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20 20  aredByte;.      
d4500 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63    rc = afpSetLoc
d4510 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20  k(pCtx->dbPath, 
d4520 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63  pFile, sharedLoc
d4530 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20  kByte, 1, 1);.  
d4540 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
d4550 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
d4560 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K && pFile->lock
d4570 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type>=PENDING_LO
d4580 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  CK ){.      rc =
d4590 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78   afpSetLock(pCtx
d45a0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
d45b0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31   PENDING_BYTE, 1
d45c0 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 20  , 0);.    } .   
d45d0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
d45e0 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  OK && pFile->loc
d45f0 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f  ktype>=RESERVED_
d4600 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  LOCK ){.      rc
d4610 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43   = afpSetLock(pC
d4620 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  tx->dbPath, pFil
d4630 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  e, RESERVED_BYTE
d4640 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  , 1, 0);.    }. 
d4650 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74   }else if( lockt
d4660 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a  ype==NO_LOCK ){.
d4670 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 65      /* clear the
d4680 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a   shared lock */.
d4690 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f      int sharedLo
d46a0 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f  ckByte = SHARED_
d46b0 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72  FIRST+pCtx->shar
d46c0 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20 3d  edByte;.    rc =
d46d0 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78   afpSetLock(pCtx
d46e0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
d46f0 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c   sharedLockByte,
d4700 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69   1, 0);.  }..  i
d4710 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
d4720 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b   ){.    if( lock
d4730 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  type==NO_LOCK ){
d4740 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75 6e  .      struct un
d4750 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  ixOpenCnt *pOpen
d4760 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b   = pFile->pOpen;
d4770 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c  .      pOpen->nL
d4780 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73  ock--;.      ass
d4790 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63  ert( pOpen->nLoc
d47a0 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  k>=0 );.      if
d47b0 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d  ( pOpen->nLock==
d47c0 30 20 26 26 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e  0 && pOpen->nPen
d47d0 64 69 6e 67 3e 30 20 29 7b 0a 20 20 20 20 20 20  ding>0 ){.      
d47e0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20    int i;.       
d47f0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70 65   for(i=0; i<pOpe
d4800 6e 2d 3e 6e 50 65 6e 64 69 6e 67 3b 20 69 2b 2b  n->nPending; i++
d4810 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
d4820 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67   pOpen->aPending
d4830 5b 69 5d 20 3c 20 30 20 29 20 63 6f 6e 74 69 6e  [i] < 0 ) contin
d4840 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ue;.          if
d4850 28 20 63 6c 6f 73 65 28 70 4f 70 65 6e 2d 3e 61  ( close(pOpen->a
d4860 50 65 6e 64 69 6e 67 5b 69 5d 29 20 29 7b 0a 20  Pending[i]) ){. 
d4870 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65             pFile
d4880 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
d4890 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 20  rno;.           
d48a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
d48b0 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20  RR_CLOSE;.      
d48c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d48d0 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50         pOpen->aP
d48e0 65 6e 64 69 6e 67 5b 69 5d 20 3d 20 2d 31 3b 0a  ending[i] = -1;.
d48f0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
d4900 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
d4910 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
d4920 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
d4930 69 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e 2d  ite3_free(pOpen-
d4940 3e 61 50 65 6e 64 69 6e 67 29 3b 0a 20 20 20 20  >aPending);.    
d4950 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65        pOpen->nPe
d4960 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20  nding = 0;.     
d4970 20 20 20 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e       pOpen->aPen
d4980 64 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  ding = 0;.      
d4990 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
d49a0 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76  }.  }.  unixLeav
d49b0 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20  eMutex();.  if( 
d49c0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc==SQLITE_OK ) 
d49d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d49e0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65  = locktype;.  re
d49f0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
d4a00 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26  * Close a file &
d4a10 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65   cleanup AFP spe
d4a20 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f  cific locking co
d4a30 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63  ntext .*/.static
d4a40 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71   int afpClose(sq
d4a50 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20  lite3_file *id) 
d4a60 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20  {.  if( id ){.  
d4a70 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d4a80 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d4a90 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b  d;.    afpUnlock
d4aa0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  (id, NO_LOCK);. 
d4ab0 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65     unixEnterMute
d4ac0 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69  x();.    if( pFi
d4ad0 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69  le->pOpen && pFi
d4ae0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b  le->pOpen->nLock
d4af0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
d4b00 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61  there are outsta
d4b10 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20  nding locks, do 
d4b20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f  not actually clo
d4b30 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74  se the file just
d4b40 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65  .      ** yet be
d4b50 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64  cause that would
d4b60 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63   clear those loc
d4b70 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64  ks.  Instead, ad
d4b80 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20  d the file.     
d4b90 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74   ** descriptor t
d4ba0 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e  o pOpen->aPendin
d4bb0 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61  g.  It will be a
d4bc0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f  utomatically clo
d4bd0 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a  sed when.      *
d4be0 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20  * the last lock 
d4bf0 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20  is cleared..    
d4c00 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a    */.      int *
d4c10 61 4e 65 77 3b 0a 20 20 20 20 20 20 73 74 72 75  aNew;.      stru
d4c20 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
d4c30 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70  pOpen = pFile->p
d4c40 4f 70 65 6e 3b 0a 20 20 20 20 20 20 61 4e 65 77  Open;.      aNew
d4c50 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
d4c60 6f 63 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69  oc(pOpen->aPendi
d4c70 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e 6e 50 65 6e  ng, (pOpen->nPen
d4c80 64 69 6e 67 2b 31 29 2a 73 69 7a 65 6f 66 28 69  ding+1)*sizeof(i
d4c90 6e 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  nt) );.      if(
d4ca0 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   aNew==0 ){.    
d4cb0 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c      /* If a mall
d4cc0 6f 63 20 66 61 69 6c 73 2c 20 6a 75 73 74 20 6c  oc fails, just l
d4cd0 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64 65 73  eak the file des
d4ce0 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 20 20 20  criptor */.     
d4cf0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
d4d00 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20  pOpen->aPending 
d4d10 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  = aNew;.        
d4d20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 5b  pOpen->aPending[
d4d30 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 5d  pOpen->nPending]
d4d40 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 20   = pFile->h;.   
d4d50 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e       pOpen->nPen
d4d60 64 69 6e 67 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ding++;.        
d4d70 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20  pFile->h = -1;. 
d4d80 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
d4d90 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28   releaseOpenCnt(
d4da0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20  pFile->pOpen);. 
d4db0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
d4dc0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
d4dd0 6e 74 65 78 74 29 3b 0a 20 20 20 20 63 6c 6f 73  ntext);.    clos
d4de0 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20  eUnixFile(id);. 
d4df0 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65     unixLeaveMute
d4e00 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  x();.  }.  retur
d4e10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d4e20 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
d4e30 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20  d(__APPLE__) && 
d4e40 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
d4e50 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f  CKING_STYLE */./
d4e60 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 61 62  *.** The code ab
d4e70 6f 76 65 20 69 73 20 74 68 65 20 41 46 50 20 6c  ove is the AFP l
d4e80 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ock implementati
d4e90 6f 6e 2e 20 20 54 68 65 20 63 6f 64 65 20 69 73  on.  The code is
d4ea0 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20   specific.** to 
d4eb0 4d 61 63 4f 53 58 20 61 6e 64 20 64 6f 65 73 20  MacOSX and does 
d4ec0 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 6f 74 68 65  not work on othe
d4ed0 72 20 75 6e 69 78 20 70 6c 61 74 66 6f 72 6d 73  r unix platforms
d4ee0 2e 20 20 4e 6f 20 61 6c 74 65 72 6e 61 74 69 76  .  No alternativ
d4ef0 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 62 6c  e.** is availabl
d4f00 65 2e 20 20 49 66 20 79 6f 75 20 64 6f 6e 27 74  e.  If you don't
d4f10 20 63 6f 6d 70 69 6c 65 20 66 6f 72 20 61 20 6d   compile for a m
d4f20 61 63 2c 20 74 68 65 6e 20 74 68 65 20 22 75 6e  ac, then the "un
d4f30 69 78 2d 61 66 70 22 0a 2a 2a 20 56 46 53 20 69  ix-afp".** VFS i
d4f40 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e  s not available.
d4f50 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
d4f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
d4f70 20 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d   the AFP lock im
d4f80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a  plementation ***
d4f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4fa0 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
d4fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d4ff0 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/.../*********
d5000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5040 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
d5050 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c 6f 63 6b 69  ****** Non-locki
d5060 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ng sqlite3_file 
d5070 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a  methods ********
d5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5090 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  *****.**.** The 
d50a0 6e 65 78 74 20 64 69 76 69 73 69 6f 6e 20 63 6f  next division co
d50b0 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74  ntains implement
d50c0 61 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 20 6d  ations for all m
d50d0 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20 0a 2a  ethods of the .*
d50e0 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f  * sqlite3_file o
d50f0 62 6a 65 63 74 20 6f 74 68 65 72 20 74 68 61 6e  bject other than
d5100 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74   the locking met
d5110 68 6f 64 73 2e 20 20 54 68 65 20 6c 6f 63 6b 69  hods.  The locki
d5120 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 77 65  ng.** methods we
d5130 72 65 20 64 65 66 69 6e 65 64 20 69 6e 20 64 69  re defined in di
d5140 76 69 73 69 6f 6e 73 20 61 62 6f 76 65 20 28 6f  visions above (o
d5150 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  ne locking metho
d5160 64 20 70 65 72 0a 2a 2a 20 64 69 76 69 73 69 6f  d per.** divisio
d5170 6e 29 2e 20 20 54 68 6f 73 65 20 6d 65 74 68 6f  n).  Those metho
d5180 64 73 20 74 68 61 74 20 61 72 65 20 63 6f 6d 6d  ds that are comm
d5190 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e  on to all lockin
d51a0 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 72 65 20 67  g modes.** are g
d51b0 61 74 68 65 72 20 74 6f 67 65 74 68 65 72 20 69  ather together i
d51c0 6e 74 6f 20 74 68 69 73 20 64 69 76 69 73 69 6f  nto this divisio
d51d0 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 65  n..*/../*.** See
d51e0 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20  k to the offset 
d51f0 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
d5200 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 74  cond argument, t
d5210 68 65 6e 20 72 65 61 64 20 63 6e 74 20 0a 2a 2a  hen read cnt .**
d5220 20 62 79 74 65 73 20 69 6e 74 6f 20 70 42 75 66   bytes into pBuf
d5230 2e 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  . Return the num
d5240 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74  ber of bytes act
d5250 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a 2a 0a 2a  ually read..**.*
d5260 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75 20 64 65  * NB:  If you de
d5270 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 20 6f  fine USE_PREAD o
d5280 72 20 55 53 45 5f 50 52 45 41 44 36 34 2c 20 74  r USE_PREAD64, t
d5290 68 65 6e 20 69 74 20 6d 69 67 68 74 20 61 6c 73  hen it might als
d52a0 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 61 72  o.** be necessar
d52b0 79 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50  y to define _XOP
d52c0 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20 62 65 20  EN_SOURCE to be 
d52d0 35 30 30 2e 20 20 54 68 69 73 20 76 61 72 69 65  500.  This varie
d52e0 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 73 79  s from.** one sy
d52f0 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68 65 72 2e  stem to another.
d5300 20 20 53 69 6e 63 65 20 53 51 4c 69 74 65 20 64    Since SQLite d
d5310 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 55  oes not define U
d5320 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61 6e 79 20  SE_PREAD.** any 
d5330 61 6e 79 20 66 6f 72 6d 20 62 79 20 64 65 66 61  any form by defa
d5340 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20 6e 6f 74  ult, we will not
d5350 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 66 69   attempt to defi
d5360 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45  ne _XOPEN_SOURCE
d5370 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b 65 74 73  ..** See tickets
d5380 20 23 32 37 34 31 20 61 6e 64 20 23 32 36 38 31   #2741 and #2681
d5390 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64  ..**.** To avoid
d53a0 20 73 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72   stomping the er
d53b0 72 6e 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66  rno value on a f
d53c0 61 69 6c 65 64 20 72 65 61 64 20 74 68 65 20 6c  ailed read the l
d53d0 61 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a  astErrno value.*
d53e0 2a 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20  * is set before 
d53f0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  returning..*/.st
d5400 61 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64  atic int seekAnd
d5410 52 65 61 64 28 75 6e 69 78 46 69 6c 65 20 2a 69  Read(unixFile *i
d5420 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
d5430 20 6f 66 66 73 65 74 2c 20 76 6f 69 64 20 2a 70   offset, void *p
d5440 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b 0a 20  Buf, int cnt){. 
d5450 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36 34 20   int got;.  i64 
d5460 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54 49 4d  newOffset;.  TIM
d5470 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20 64 65  ER_START;.#if de
d5480 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 29  fined(USE_PREAD)
d5490 0a 20 20 67 6f 74 20 3d 20 70 72 65 61 64 28 69  .  got = pread(i
d54a0 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c  d->h, pBuf, cnt,
d54b0 20 6f 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75   offset);.  Simu
d54c0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74  lateIOError( got
d54d0 20 3d 20 2d 31 20 29 3b 0a 23 65 6c 69 66 20 64   = -1 );.#elif d
d54e0 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44  efined(USE_PREAD
d54f0 36 34 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 61  64).  got = prea
d5500 64 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c  d64(id->h, pBuf,
d5510 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20   cnt, offset);. 
d5520 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d5530 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65  ( got = -1 );.#e
d5540 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20  lse.  newOffset 
d5550 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f  = lseek(id->h, o
d5560 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29  ffset, SEEK_SET)
d5570 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
d5580 72 6f 72 28 20 6e 65 77 4f 66 66 73 65 74 2d 2d  ror( newOffset--
d5590 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66   );.  if( newOff
d55a0 73 65 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20  set!=offset ){. 
d55b0 20 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74     if( newOffset
d55c0 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20   == -1 ){.      
d55d0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
d55e0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
d55f0 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  no;.    }else{. 
d5600 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a       ((unixFile*
d5610 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  )id)->lastErrno 
d5620 3d 20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20  = 0;....    }.  
d5630 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d    return -1;.  }
d5640 0a 20 20 67 6f 74 20 3d 20 72 65 61 64 28 69 64  .  got = read(id
d5650 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b  ->h, pBuf, cnt);
d5660 0a 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f  .#endif.  TIMER_
d5670 45 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30  END;.  if( got<0
d5680 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69   ){.    ((unixFi
d5690 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72  le*)id)->lastErr
d56a0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a  no = errno;.  }.
d56b0 20 20 4f 53 54 52 41 43 45 35 28 22 52 45 41 44    OSTRACE5("READ
d56c0 20 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c      %-3d %5d %7l
d56d0 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e  ld %llu\n", id->
d56e0 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20  h, got, offset, 
d56f0 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a  TIMER_ELAPSED);.
d5700 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a    return got;.}.
d5710 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
d5720 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74   from a file int
d5730 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74  o a buffer.  Ret
d5740 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
d5750 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65   all.** bytes we
d5760 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66  re read successf
d5770 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f  ully and SQLITE_
d5780 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e  IOERR if anythin
d5790 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e  g goes.** wrong.
d57a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
d57b0 6e 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69 74  nixRead(.  sqlit
d57c0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20  e3_file *id, .  
d57d0 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 69  void *pBuf, .  i
d57e0 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65  nt amt,.  sqlite
d57f0 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a 29  3_int64 offset.)
d5800 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61  {.  int got;.  a
d5810 73 73 65 72 74 28 20 69 64 20 29 3b 0a 0a 20 20  ssert( id );..  
d5820 2f 2a 20 4e 65 76 65 72 20 72 65 61 64 20 6f 72  /* Never read or
d5830 20 77 72 69 74 65 20 61 6e 79 20 6f 66 20 74 68   write any of th
d5840 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6c  e bytes in the l
d5850 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 2a 2f 0a  ocking range */.
d5860 20 20 61 73 73 65 72 74 28 20 28 28 75 6e 69 78    assert( ((unix
d5870 46 69 6c 65 2a 29 69 64 29 2d 3e 69 73 4c 6f 63  File*)id)->isLoc
d5880 6b 61 62 6c 65 3d 3d 30 0a 20 20 20 20 20 20 20  kable==0.       
d5890 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 45     || offset>=PE
d58a0 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a 20  NDING_BYTE+512. 
d58b0 20 20 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73           || offs
d58c0 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f  et+amt<=PENDING_
d58d0 42 59 54 45 20 29 3b 0a 0a 20 20 67 6f 74 20 3d  BYTE );..  got =
d58e0 20 73 65 65 6b 41 6e 64 52 65 61 64 28 28 75 6e   seekAndRead((un
d58f0 69 78 46 69 6c 65 2a 29 69 64 2c 20 6f 66 66 73  ixFile*)id, offs
d5900 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 3b 0a  et, pBuf, amt);.
d5910 20 20 69 66 28 20 67 6f 74 3d 3d 61 6d 74 20 29    if( got==amt )
d5920 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
d5930 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20  ITE_OK;.  }else 
d5940 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20  if( got<0 ){.   
d5950 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65   /* lastErrno se
d5960 74 20 62 79 20 73 65 65 6b 41 6e 64 52 65 61 64  t by seekAndRead
d5970 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53   */.    return S
d5980 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
d5990 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 28  ;.  }else{.    (
d59a0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
d59b0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f  lastErrno = 0; /
d59c0 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65  * not a system e
d59d0 72 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55  rror */.    /* U
d59e0 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74  nread parts of t
d59f0 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62  he buffer must b
d5a00 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f  e zero-filled */
d5a10 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63  .    memset(&((c
d5a20 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c  har*)pBuf)[got],
d5a30 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20   0, amt-got);.  
d5a40 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d5a50 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
d5a60 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ;.  }.}../*.** S
d5a70 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65  eek to the offse
d5a80 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20  t in id->offset 
d5a90 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79  then read cnt by
d5aa0 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a  tes into pBuf..*
d5ab0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
d5ac0 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74  ber of bytes act
d5ad0 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64  ually read.  Upd
d5ae0 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a  ate the offset..
d5af0 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73  **.** To avoid s
d5b00 74 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e  tomping the errn
d5b10 6f 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69  o value on a fai
d5b20 6c 65 64 20 77 72 69 74 65 20 74 68 65 20 6c 61  led write the la
d5b30 73 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a  stErrno value.**
d5b40 20 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 72   is set before r
d5b50 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61  eturning..*/.sta
d5b60 74 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57  tic int seekAndW
d5b70 72 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69  rite(unixFile *i
d5b80 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63  d, i64 offset, c
d5b90 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c  onst void *pBuf,
d5ba0 20 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74   int cnt){.  int
d5bb0 20 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f   got;.  i64 newO
d5bc0 66 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53  ffset;.  TIMER_S
d5bd0 54 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65  TART;.#if define
d5be0 64 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67  d(USE_PREAD).  g
d5bf0 6f 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e  ot = pwrite(id->
d5c00 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66  h, pBuf, cnt, of
d5c10 66 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66  fset);.#elif def
d5c20 69 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34  ined(USE_PREAD64
d5c30 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65  ).  got = pwrite
d5c40 36 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20  64(id->h, pBuf, 
d5c50 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65  cnt, offset);.#e
d5c60 6c 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20  lse.  newOffset 
d5c70 3d 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f  = lseek(id->h, o
d5c80 66 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29  ffset, SEEK_SET)
d5c90 3b 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65  ;.  if( newOffse
d5ca0 74 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20  t!=offset ){.   
d5cb0 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20 3d   if( newOffset =
d5cc0 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 28  = -1 ){.      ((
d5cd0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
d5ce0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
d5cf0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
d5d00 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
d5d10 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
d5d20 30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 20  0;....    }.    
d5d30 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20  return -1;.  }. 
d5d40 20 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d   got = write(id-
d5d50 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a  >h, pBuf, cnt);.
d5d60 23 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45  #endif.  TIMER_E
d5d70 4e 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 20  ND;.  if( got<0 
d5d80 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c  ){.    ((unixFil
d5d90 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e  e*)id)->lastErrn
d5da0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a  o = errno;.  }..
d5db0 20 20 4f 53 54 52 41 43 45 35 28 22 57 52 49 54    OSTRACE5("WRIT
d5dc0 45 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c  E   %-3d %5d %7l
d5dd0 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e  ld %llu\n", id->
d5de0 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20  h, got, offset, 
d5df0 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a  TIMER_ELAPSED);.
d5e00 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a    return got;.}.
d5e10 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
d5e20 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72  ta from a buffer
d5e30 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52   into a file.  R
d5e40 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
d5e50 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  on success.** or
d5e60 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f   some other erro
d5e70 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72  r code on failur
d5e80 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
d5e90 20 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73 71   unixWrite(.  sq
d5ea0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d5eb0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
d5ec0 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c  Buf, .  int amt,
d5ed0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
d5ee0 20 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 69 6e   offset .){.  in
d5ef0 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61  t wrote = 0;.  a
d5f00 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 61  ssert( id );.  a
d5f10 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a  ssert( amt>0 );.
d5f20 0a 20 20 2f 2a 20 4e 65 76 65 72 20 72 65 61 64  .  /* Never read
d5f30 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20 6f 66   or write any of
d5f40 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68   the bytes in th
d5f50 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20  e locking range 
d5f60 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 28 75  */.  assert( ((u
d5f70 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 73  nixFile*)id)->is
d5f80 4c 6f 63 6b 61 62 6c 65 3d 3d 30 0a 20 20 20 20  Lockable==0.    
d5f90 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e        || offset>
d5fa0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31  =PENDING_BYTE+51
d5fb0 32 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6f  2.          || o
d5fc0 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49  ffset+amt<=PENDI
d5fd0 4e 47 5f 42 59 54 45 20 29 3b 0a 0a 23 69 66 6e  NG_BYTE );..#ifn
d5fe0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20  def NDEBUG.  /* 
d5ff0 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20  If we are doing 
d6000 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 74  a normal write t
d6010 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  o a database fil
d6020 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 6f  e (as opposed to
d6030 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68 6f  .  ** doing a ho
d6040 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61  t-journal rollba
d6050 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74 6f  ck or a write to
d6060 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65 72   some file other
d6070 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f 72   than a.  ** nor
d6080 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  mal database fil
d6090 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20 74  e) then record t
d60a0 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
d60b0 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 68   database.  ** h
d60c0 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20  as changed.  If 
d60d0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
d60e0 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69 66  counter is modif
d60f0 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61 74  ied, record that
d6100 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e 0a  .  ** fact too..
d6110 20 20 2a 2f 0a 20 20 69 66 28 20 28 28 75 6e 69    */.  if( ((uni
d6120 78 46 69 6c 65 2a 29 69 64 29 2d 3e 69 6e 4e 6f  xFile*)id)->inNo
d6130 72 6d 61 6c 57 72 69 74 65 20 29 7b 0a 20 20 20  rmalWrite ){.   
d6140 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d6150 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d6160 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62 55  ;.    pFile->dbU
d6170 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a 20 54  pdate = 1;  /* T
d6180 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20  he database has 
d6190 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 2a 2f  been modified */
d61a0 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 74 3c  .    if( offset<
d61b0 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b 61 6d  =24 && offset+am
d61c0 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 20 69  t>=27 ){.      i
d61d0 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63 68 61  nt rc;.      cha
d61e0 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a 20 20  r oldCntr[4];.  
d61f0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
d6200 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20  rorBenign(1);.  
d6210 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41 6e 64      rc = seekAnd
d6220 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34 2c 20  Read(pFile, 24, 
d6230 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 20 20  oldCntr, 4);.   
d6240 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72     SimulateIOErr
d6250 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20  orBenign(0);.   
d6260 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c 7c 20     if( rc!=4 || 
d6270 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72 2c 20  memcmp(oldCntr, 
d6280 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 32  &((char*)pBuf)[2
d6290 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21 3d 30  4-offset], 4)!=0
d62a0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d62b0 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67  e->transCntrChng
d62c0 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20 74 72   = 1;  /* The tr
d62d0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65  ansaction counte
d62e0 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 2a 2f  r has changed */
d62f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d6300 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77 68 69   }.#endif..  whi
d6310 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77 72  le( amt>0 && (wr
d6320 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69  ote = seekAndWri
d6330 74 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  te((unixFile*)id
d6340 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20  , offset, pBuf, 
d6350 61 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61  amt))>0 ){.    a
d6360 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20  mt -= wrote;.   
d6370 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65   offset += wrote
d6380 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28  ;.    pBuf = &((
d6390 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74  char*)pBuf)[wrot
d63a0 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61  e];.  }.  Simula
d63b0 74 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74  teIOError(( wrot
d63c0 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29  e=(-1), amt=1 ))
d63d0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ;.  SimulateDisk
d63e0 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74  fullError(( wrot
d63f0 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20  e=0, amt=1 ));. 
d6400 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20   if( amt>0 ){.  
d6410 20 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b    if( wrote<0 ){
d6420 0a 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72  .      /* lastEr
d6430 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41  rno set by seekA
d6440 6e 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 20  ndWrite */.     
d6450 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
d6460 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20  OERR_WRITE;.    
d6470 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75  }else{.      ((u
d6480 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
d6490 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20  stErrno = 0; /* 
d64a0 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72  not a system err
d64b0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  or */.      retu
d64c0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  rn SQLITE_FULL;.
d64d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
d64e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
d64f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
d6500 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  EST./*.** Count 
d6510 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75  the number of fu
d6520 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d  llsyncs and norm
d6530 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20  al syncs.  This 
d6540 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a  is used to test.
d6550 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e  ** that syncs an
d6560 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20  d fullsyncs are 
d6570 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65  occurring at the
d6580 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f   right times..*/
d6590 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
d65a0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
d65b0 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  nt = 0;.SQLITE_A
d65c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
d65d0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20  ullsync_count = 
d65e0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
d65f0 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74   We do not trust
d6600 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76   systems to prov
d6610 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64  ide a working fd
d6620 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65  atasync().  Some
d6630 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64   do..** Others d
d6640 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66  o no.  To be saf
d6650 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b  e, we will stick
d6660 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 65   with the (slowe
d6670 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 49  r) fsync()..** I
d6680 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 20  f you know that 
d6690 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 73  your system does
d66a0 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 79   support fdatasy
d66b0 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c 0a  nc() correctly,.
d66c0 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 63  ** then simply c
d66d0 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 64  ompile with -Dfd
d66e0 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 6e  atasync=fdatasyn
d66f0 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  c.*/.#if !define
d6700 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 20  d(fdatasync) && 
d6710 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78  !defined(__linux
d6720 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 61  __).# define fda
d6730 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e  tasync fsync.#en
d6740 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e  dif../*.** Defin
d6750 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  e HAVE_FULLFSYNC
d6760 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e   to 0 or 1 depen
d6770 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20  ding on whether 
d6780 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f  or not.** the F_
d6790 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20  FULLFSYNC macro 
d67a0 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46  is defined.  F_F
d67b0 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72  ULLFSYNC is curr
d67c0 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76  ently.** only av
d67d0 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f  ailable on Mac O
d67e0 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63  S X.  But that c
d67f0 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  ould change..*/.
d6800 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59  #ifdef F_FULLFSY
d6810 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45  NC.# define HAVE
d6820 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c  _FULLFSYNC 1.#el
d6830 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45  se.# define HAVE
d6840 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e  _FULLFSYNC 0.#en
d6850 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dif.../*.** The 
d6860 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63  fsync() system c
d6870 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  all does not wor
d6880 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20  k as advertised 
d6890 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20  on many.** unix 
d68a0 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f  systems.  The fo
d68b0 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72  llowing procedur
d68c0 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20  e is an attempt 
d68d0 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f  to make.** it wo
d68e0 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a  rk better..**.**
d68f0 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53   The SQLITE_NO_S
d6900 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c  YNC macro disabl
d6910 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e  es all fsync()s.
d6920 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c    This is useful
d6930 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20  .** for testing 
d6940 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20  when we want to 
d6950 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  run through the 
d6960 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b  test suite quick
d6970 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73  ly..** You are s
d6980 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20  trongly advised 
d6990 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20  *not* to deploy 
d69a0 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  with SQLITE_NO_S
d69b0 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20  YNC.** enabled, 
d69c0 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77  however, since w
d69d0 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  ith SQLITE_NO_SY
d69e0 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f  NC enabled, an O
d69f0 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f  S crash.** or po
d6a00 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c  wer failure will
d6a10 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20   likely corrupt 
d6a20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
d6a30 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  e..**.** SQLite 
d6a40 73 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c  sets the dataOnl
d6a50 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 69  y flag if the si
d6a60 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
d6a70 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20  s unchanged..** 
d6a80 54 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20  The idea behind 
d6a90 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74  dataOnly is that
d6aa0 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20   it should only 
d6ab0 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63  write the file c
d6ac0 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 73  ontent.** to dis
d6ad0 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65  k, not the inode
d6ae0 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64  .  We only set d
d6af0 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66  ataOnly if the f
d6b00 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a 20  ile size is .** 
d6b10 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20  unchanged since 
d6b20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73  the file size is
d6b30 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 6f   part of the ino
d6b40 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a 2a  de.  However, .*
d6b50 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c 73  * Ted Ts'o tells
d6b60 20 75 73 20 74 68 61 74 20 66 64 61 74 61 73 79   us that fdatasy
d6b70 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 77  nc() will also w
d6b80 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 69  rite the inode i
d6b90 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 69  f the.** file si
d6ba0 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20  ze has changed. 
d6bb0 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 64   The only real d
d6bc0 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
d6bd0 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a 2a  n fdatasync().**
d6be0 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 65   and fsync(), Te
d6bf0 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 74  d tells us, is t
d6c00 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 20  hat fdatasync() 
d6c10 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 74  will not flush t
d6c20 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 74  he.** inode if t
d6c30 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e 65  he mtime or owne
d6c40 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 65  r or other inode
d6c50 20 61 74 74 72 69 62 75 74 65 73 20 68 61 76 65   attributes have
d6c60 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 20   changed..** We 
d6c70 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 20  only care about 
d6c80 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 6e  the file size, n
d6c90 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 6c  ot the other fil
d6ca0 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 6f  e attributes, so
d6cb0 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 51  .** as far as SQ
d6cc0 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e 65  Lite is concerne
d6cd0 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 28  d, an fdatasync(
d6ce0 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 71  ) is always adeq
d6cf0 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 20  uate..** So, we 
d6d00 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 61  always use fdata
d6d10 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 20  sync() if it is 
d6d20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 72  available, regar
d6d30 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 20  dless of.** the 
d6d40 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74  value of the dat
d6d50 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a 73  aOnly flag..*/.s
d6d60 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66  tatic int full_f
d6d70 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74  sync(int fd, int
d6d80 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64   fullSync, int d
d6d90 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20  ataOnly){.  int 
d6da0 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f  rc;..  /* The fo
d6db0 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65  llowing "ifdef/e
d6dc0 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b  lif/else/" block
d6dd0 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74   has the same st
d6de0 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20  ructure as.  ** 
d6df0 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49  the one below. I
d6e00 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20  t is replicated 
d6e10 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61  here solely to a
d6e20 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20  void cluttering 
d6e30 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61  .  ** up the rea
d6e40 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20  l code with the 
d6e50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
d6e60 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a  () macros..  */.
d6e70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f  #ifdef SQLITE_NO
d6e80 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50  _SYNC.  UNUSED_P
d6e90 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20  ARAMETER(fd);.  
d6ea0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
d6eb0 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e  (fullSync);.  UN
d6ec0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64  USED_PARAMETER(d
d6ed0 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20  ataOnly);.#elif 
d6ee0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20  HAVE_FULLFSYNC. 
d6ef0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
d6f00 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c  R(dataOnly);.#el
d6f10 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
d6f20 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b  METER(fullSync);
d6f30 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
d6f40 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23  TER(dataOnly);.#
d6f50 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f  endif..  /* Reco
d6f60 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rd the number of
d6f70 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 64   times that we d
d6f80 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63  o a normal fsync
d6f90 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c  () and .  ** FUL
d6fa0 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20  LSYNC.  This is 
d6fb0 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74  used during test
d6fc0 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 68  ing to verify th
d6fd0 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  at this procedur
d6fe0 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c  e.  ** gets call
d6ff0 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72  ed with the corr
d7000 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20  ect arguments.. 
d7010 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
d7020 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 6c  E_TEST.  if( ful
d7030 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 5f  lSync ) sqlite3_
d7040 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  fullsync_count++
d7050 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63  ;.  sqlite3_sync
d7060 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
d7070 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d  ..  /* If we com
d7080 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53  piled with the S
d7090 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c  QLITE_NO_SYNC fl
d70a0 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67  ag, then syncing
d70b0 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70   is a.  ** no-op
d70c0 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
d70d0 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63  ITE_NO_SYNC.  rc
d70e0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65   = SQLITE_OK;.#e
d70f0 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  lif HAVE_FULLFSY
d7100 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 6e  NC.  if( fullSyn
d7110 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 63  c ){.    rc = fc
d7120 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53  ntl(fd, F_FULLFS
d7130 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  YNC, 0);.  }else
d7140 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  {.    rc = 1;.  
d7150 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 55  }.  /* If the FU
d7160 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c 20  LLFSYNC failed, 
d7170 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 74  fall back to att
d7180 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e 63  empting an fsync
d7190 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f 75  ()..  ** It shou
d71a0 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c  ldn't be possibl
d71b0 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 20  e for fullfsync 
d71c0 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 6c  to fail on the l
d71d0 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20  ocal .  ** file 
d71e0 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 2c  system (on OSX),
d71f0 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 69   so failure indi
d7200 63 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c 46  cates that FULLF
d7210 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 20  SYNC.  ** isn't 
d7220 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 68  supported for th
d7230 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20  is file system. 
d7240 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 66  So, attempt an f
d7250 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 28  sync .  ** and (
d7260 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 20  for now) ignore 
d7270 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20  the overhead of 
d7280 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 63  a superfluous fc
d7290 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a 2a  ntl call.  .  **
d72a0 20 49 74 27 64 20 62 65 20 62 65 74 74 65 72 20   It'd be better 
d72b0 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 73  to detect fullfs
d72c0 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 65  ync support once
d72d0 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a 2a   and avoid .  **
d72e0 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c 20   the fcntl call 
d72f0 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 20  every time sync 
d7300 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f 0a  is called..  */.
d7310 20 20 69 66 28 20 72 63 20 29 20 72 63 20 3d 20    if( rc ) rc = 
d7320 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c 73  fsync(fd);..#els
d7330 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73  e .  rc = fdatas
d7340 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 5f  ync(fd);.#if OS_
d7350 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63  VXWORKS.  if( rc
d7360 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d 45  ==-1 && errno==E
d7370 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 63  NOTSUP ){.    rc
d7380 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20   = fsync(fd);.  
d7390 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56  }.#endif /* OS_V
d73a0 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 66  XWORKS */.#endif
d73b0 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 45   /* ifdef SQLITE
d73c0 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 41  _NO_SYNC elif HA
d73d0 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a  VE_FULLFSYNC */.
d73e0 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 4b  .  if( OS_VXWORK
d73f0 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b 0a  S && rc!= -1 ){.
d7400 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 0a      rc = 0;.  }.
d7410 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
d7420 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
d7430 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20  all writes to a 
d7440 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20  particular file 
d7450 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f  are committed to
d7460 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   disk..**.** If 
d7470 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e  dataOnly==0 then
d7480 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 69   both the file i
d7490 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d 65  tself and its me
d74a0 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a 20  tadata (file.** 
d74b0 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 6d  size, access tim
d74c0 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e 63  e, etc) are sync
d74d0 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c 79  ed.  If dataOnly
d74e0 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68  !=0 then only th
d74f0 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 69  e.** file data i
d7500 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20  s synced..**.** 
d7510 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f  Under Unix, also
d7520 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
d7530 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e  the directory en
d7540 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65  try for the file
d7550 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65  .** has been cre
d7560 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e  ated by fsync-in
d7570 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  g the directory 
d7580 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
d7590 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 65  e file..** If we
d75a0 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20   do not do this 
d75b0 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 72  and we encounter
d75c0 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65   a power failure
d75d0 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a  , the directory.
d75e0 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ** entry for the
d75f0 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e   journal might n
d7600 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 20 77  ot exist after w
d7610 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 6e  e reboot.  The n
d7620 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f  ext.** SQLite to
d7630 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65   access the file
d7640 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74   will not know t
d7650 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
d7660 65 78 69 73 74 73 20 28 62 65 63 61 75 73 65 0a  exists (because.
d7670 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  ** the directory
d7680 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a   entry for the j
d7690 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 72  ournal was never
d76a0 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 68   created) and th
d76b0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  e transaction.**
d76c0 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62   will not roll b
d76d0 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 6c  ack - possibly l
d76e0 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61  eading to databa
d76f0 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a  se corruption..*
d7700 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
d7710 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69  xSync(sqlite3_fi
d7720 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67  le *id, int flag
d7730 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  s){.  int rc;.  
d7740 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d7750 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d7760 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f 6e  ..  int isDataOn
d7770 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c 49  ly = (flags&SQLI
d7780 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
d7790 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c 73  );.  int isFulls
d77a0 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 30  ync = (flags&0x0
d77b0 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f  F)==SQLITE_SYNC_
d77c0 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  FULL;..  /* Chec
d77d0 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51  k that one of SQ
d77e0 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
d77f0 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73   or FULL was pas
d7800 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  sed */.  assert(
d7810 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51  (flags&0x0F)==SQ
d7820 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
d7830 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73  .      || (flags
d7840 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53  &0x0F)==SQLITE_S
d7850 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20  YNC_FULL.  );.. 
d7860 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 2c   /* Unix cannot,
d7870 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 6d   but some system
d7880 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  s may return SQL
d7890 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 65  ITE_FULL from he
d78a0 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c 69  re. This.  ** li
d78b0 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 68  ne is to test th
d78c0 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 73  at doing so does
d78d0 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 70   not cause any p
d78e0 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20  roblems..  */.  
d78f0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
d7900 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
d7910 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20  LITE_FULL );..  
d7920 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d7930 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 4e  .  OSTRACE2("SYN
d7940 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46  C    %-3d\n", pF
d7950 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d 20  ile->h);.  rc = 
d7960 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65  full_fsync(pFile
d7970 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 2c  ->h, isFullsync,
d7980 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 20   isDataOnly);.  
d7990 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
d79a0 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72   rc=1 );.  if( r
d79b0 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  c ){.    pFile->
d79c0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
d79d0 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  o;.    return SQ
d79e0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43  LITE_IOERR_FSYNC
d79f0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c  ;.  }.  if( pFil
d7a00 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20  e->dirfd>=0 ){. 
d7a10 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20     int err;.    
d7a20 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e  OSTRACE4("DIRSYN
d7a30 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c  C %-3d (have_ful
d7a40 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79  lfsync=%d fullsy
d7a50 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65  nc=%d)\n", pFile
d7a60 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20  ->dirfd,.       
d7a70 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53       HAVE_FULLFS
d7a80 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29  YNC, isFullsync)
d7a90 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
d7aa0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43  _DISABLE_DIRSYNC
d7ab0 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65  .    /* The dire
d7ac0 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e  ctory sync is on
d7ad0 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20  ly attempted if 
d7ae0 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20  full_fsync is.  
d7af0 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20    ** turned off 
d7b00 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20  or unavailable. 
d7b10 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63   If a full_fsync
d7b20 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c   occurred above,
d7b30 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  .    ** then the
d7b40 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20   directory sync 
d7b50 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a  is superfluous..
d7b60 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28      */.    if( (
d7b70 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20  !HAVE_FULLFSYNC 
d7b80 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20  || !isFullsync) 
d7b90 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46  && full_fsync(pF
d7ba0 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20  ile->dirfd,0,0) 
d7bb0 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20  ){.       /*.   
d7bc0 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72      ** We have r
d7bd0 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65  eceived multiple
d7be0 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e   reports of fsyn
d7bf0 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20  c() returning.  
d7c00 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77       ** errors w
d7c10 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64  hen applied to d
d7c20 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65  irectories on ce
d7c30 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65  rtain file syste
d7c40 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20  ms..       ** A 
d7c50 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79  failed directory
d7c60 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62   sync is not a b
d7c70 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20  ig deal.  So it 
d7c80 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20  seems.       ** 
d7c90 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65  better to ignore
d7ca0 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63   the error.  Tic
d7cb0 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20  ket #1657.      
d7cc0 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 46   */.       /* pF
d7cd0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d7ce0 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20   errno; */.     
d7cf0 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49    /* return SQLI
d7d00 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20  TE_IOERR; */.   
d7d10 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 72   }.#endif.    er
d7d20 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d  r = close(pFile-
d7d30 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79  >dirfd); /* Only
d7d40 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e   need to sync on
d7d50 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65  ce, so close the
d7d60 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 3d   */.    if( err=
d7d70 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  =0 ){           
d7d80 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 20     /* directory 
d7d90 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65  when we are done
d7da0 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   */.      pFile-
d7db0 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 20  >dirfd = -1;.   
d7dc0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46   }else{.      pF
d7dd0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d7de0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63   errno;.      rc
d7df0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
d7e00 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 7d  DIR_CLOSE;.    }
d7e10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
d7e20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63  ;.}../*.** Trunc
d7e30 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65  ate an open file
d7e40 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20   to a specified 
d7e50 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69  size.*/.static i
d7e60 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28  nt unixTruncate(
d7e70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d7e80 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20  , i64 nByte){.  
d7e90 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
d7ea0 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c 61  ( id );.  Simula
d7eb0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
d7ec0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  n SQLITE_IOERR_T
d7ed0 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20  RUNCATE );.  rc 
d7ee0 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 6e  = ftruncate(((un
d7ef0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  ixFile*)id)->h, 
d7f00 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20  (off_t)nByte);. 
d7f10 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 28   if( rc ){.    (
d7f20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
d7f30 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
d7f40 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  o;.    return SQ
d7f50 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
d7f60 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ATE;.  }else{.  
d7f70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d7f80 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OK;.  }.}../*.**
d7f90 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63   Determine the c
d7fa0 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61  urrent size of a
d7fb0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a   file in bytes.*
d7fc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
d7fd0 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  xFileSize(sqlite
d7fe0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20  3_file *id, i64 
d7ff0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  *pSize){.  int r
d8000 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74  c;.  struct stat
d8010 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20   buf;.  assert( 
d8020 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74  id );.  rc = fst
d8030 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69  at(((unixFile*)i
d8040 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20  d)->h, &buf);.  
d8050 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
d8060 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72   rc=1 );.  if( r
d8070 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e  c!=0 ){.    ((un
d8080 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
d8090 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d80a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d80b0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20  E_IOERR_FSTAT;. 
d80c0 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75   }.  *pSize = bu
d80d0 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a  f.st_size;..  /*
d80e0 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20   When opening a 
d80f0 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61  zero-size databa
d8100 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b  se, the findLock
d8110 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65  Info() procedure
d8120 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73  .  ** writes a s
d8130 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20  ingle byte into 
d8140 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64  that file in ord
d8150 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  er to work aroun
d8160 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20  d a bug.  ** in 
d8170 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66  the OS-X msdos f
d8180 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f  ilesystem.  In o
d8190 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72  rder to avoid pr
d81a0 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65  oblems with uppe
d81b0 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77  r.  ** layers, w
d81c0 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74  e need to report
d81d0 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20   this file size 
d81e0 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f  as zero even tho
d81f0 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72  ugh it is.  ** r
d8200 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65  eally 1.   Ticke
d8210 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20  t #3260..  */.  
d8220 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20  if( *pSize==1 ) 
d8230 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20  *pSize = 0;...  
d8240 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d8250 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  ;.}..#if SQLITE_
d8260 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
d8270 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28  TYLE && defined(
d8280 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a  __APPLE__)./*.**
d8290 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f   Handler for pro
d82a0 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d  xy-locking file-
d82b0 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20  control verbs.  
d82c0 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e  Defined below in
d82d0 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67   the.** proxying
d82e0 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f   locking divisio
d82f0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
d8300 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f   proxyFileContro
d8310 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  l(sqlite3_file*,
d8320 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64  int,void*);.#end
d8330 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72  if.../*.** Infor
d8340 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72  mation and contr
d8350 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69  ol of an open fi
d8360 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74  le handle..*/.st
d8370 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c  atic int unixFil
d8380 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  eControl(sqlite3
d8390 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f  _file *id, int o
d83a0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  p, void *pArg){.
d83b0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
d83c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
d83d0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a  FCNTL_LOCKSTATE:
d83e0 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29   {.      *(int*)
d83f0 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c  pArg = ((unixFil
d8400 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65  e*)id)->locktype
d8410 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
d8420 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
d8430 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
d8440 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20  LAST_ERRNO: {.  
d8450 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20      *(int*)pArg 
d8460 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  = ((unixFile*)id
d8470 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20  )->lastErrno;.  
d8480 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d8490 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  E_OK;.    }.#ifn
d84a0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f  def NDEBUG.    /
d84b0 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c  * The pager call
d84c0 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f  s this method to
d84d0 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20   signal that it 
d84e0 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20  has done.    ** 
d84f0 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74  a rollback and t
d8500 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
d8510 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e   is therefore un
d8520 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20  changed and.    
d8530 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69  ** it hence it i
d8540 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61  s OK for the tra
d8550 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20  nsaction change 
d8560 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20  counter to be.  
d8570 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a    ** unchanged..
d8580 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
d8590 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f  SQLITE_FCNTL_DB_
d85a0 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20  UNCHANGED: {.   
d85b0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
d85c0 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30  d)->dbUpdate = 0
d85d0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
d85e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
d85f0 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
d8600 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
d8610 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
d8620 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20  d(__APPLE__).   
d8630 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54   case SQLITE_SET
d8640 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a  _LOCKPROXYFILE:.
d8650 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
d8660 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  GET_LOCKPROXYFIL
d8670 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  E: {.      retur
d8680 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72  n proxyFileContr
d8690 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a  ol(id,op,pArg);.
d86a0 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
d86b0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
d86c0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64  CKING_STYLE && d
d86d0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
d86e0 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) */.  }.  retur
d86f0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
d8700 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
d8710 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
d8720 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  in bytes of the 
d8730 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b  underlying block
d8740 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74   device for.** t
d8750 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c  he specified fil
d8760 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73  e. This is almos
d8770 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74  t always 512 byt
d8780 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a  es, but may be.*
d8790 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d  * larger for som
d87a0 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a  e devices..**.**
d87b0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73   SQLite code ass
d87c0 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69  umes this functi
d87d0 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20  on cannot fail. 
d87e0 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  It also assumes 
d87f0 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66  that.** if two f
d8800 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64  iles are created
d8810 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c   in the same fil
d8820 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f  e-system directo
d8830 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61  ry (i.e..** a da
d8840 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a  tabase and its j
d8850 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61  ournal file) tha
d8860 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  t the sector siz
d8870 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a  e will be the.**
d8880 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a   same for both..
d8890 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
d88a0 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  ixSectorSize(sql
d88b0 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73  ite3_file *NotUs
d88c0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
d88d0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
d88e0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
d88f0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
d8900 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  _SIZE;.}../*.** 
d8910 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63  Return the devic
d8920 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
d8930 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20  s for the file. 
d8940 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30  This is always 0
d8950 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74   for unix..*/.st
d8960 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76  atic int unixDev
d8970 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
d8980 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
d8990 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55  *NotUsed){.  UNU
d89a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
d89b0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e  tUsed);.  return
d89c0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72   0;.}../*.** Her
d89d0 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65  e ends the imple
d89e0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c  mentation of all
d89f0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65   sqlite3_file me
d8a00 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a  thods..**.******
d8a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a20 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c   End sqlite3_fil
d8a30 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a  e Methods ******
d8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
d8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8aa0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
d8ab0 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63   This division c
d8ac0 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69  ontains definiti
d8ad0 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69  ons of sqlite3_i
d8ae0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
d8af0 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d  s that.** implem
d8b00 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65  ent various file
d8b10 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
d8b20 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f  ies.  It also co
d8b30 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f  ntains definitio
d8b40 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72  ns.** of "finder
d8b50 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20  " functions.  A 
d8b60 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
d8b70 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  is used to locat
d8b80 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  e the appropriat
d8b90 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f  e.** sqlite3_io_
d8ba0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66  methods object f
d8bb0 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
d8bc0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
d8bd0 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20  The pAppData.** 
d8be0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c  field of the sql
d8bf0 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a  ite3_vfs VFS obj
d8c00 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c  ects are initial
d8c10 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74  ized to be point
d8c20 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f  ers to.** the co
d8c30 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e  rrect finder-fun
d8c40 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56  ction for that V
d8c50 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66  FS..**.** Most f
d8c60 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  inder functions 
d8c70 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
d8c80 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69   to a fixed sqli
d8c90 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a  te3_io_methods.*
d8ca0 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f  * object.  The o
d8cb0 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20  nly interesting 
d8cc0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
d8cd0 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e  is autolockIoFin
d8ce0 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f  der, which.** lo
d8cf0 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73  oks at the files
d8d00 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74  ystem type and t
d8d10 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68  ries to guess th
d8d20 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a  e best locking.*
d8d30 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20  * strategy from 
d8d40 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  that..**.** For 
d8d50 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46  finder-funtion F
d8d60 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72  , two objects ar
d8d70 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a  e created:.**.**
d8d80 20 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c      (1) The real
d8d90 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
d8da0 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22   named "FImpt()"
d8db0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41  ..**.**    (2) A
d8dc0 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65   constant pointe
d8dd0 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  r to this functi
d8de0 6f 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 22  o named just "F"
d8df0 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69  ..**.**.** A poi
d8e00 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 6f  nter to the F po
d8e10 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73  inter is used as
d8e20 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 61   the pAppData va
d8e30 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 6f  lue for VFS.** o
d8e40 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 65  bjects.  We have
d8e50 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 74   to do this inst
d8e60 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 70  ead of letting p
d8e70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a 2a  AppData point.**
d8e80 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 65   directly at the
d8e90 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
d8ea0 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 73   since C90 rules
d8eb0 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 2a   prevent a void*
d8ec0 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 74  .** from be cast
d8ed0 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e   into a function
d8ee0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a   pointer..**.**.
d8ef0 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65  ** Each instance
d8f00 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 67   of this macro g
d8f10 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 6a  enerates two obj
d8f20 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  ects:.**.**   * 
d8f30 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c 69   A constant sqli
d8f40 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
d8f50 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 4f  bject call METHO
d8f60 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 69  D that has locki
d8f70 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f  ng.**      metho
d8f80 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20  ds CLOSE, LOCK, 
d8f90 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f 43  UNLOCK, CKRESLOC
d8fa0 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e  K..**.**   *  An
d8fb0 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64   I/O method find
d8fc0 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c  er function call
d8fd0 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 72  ed FINDER that r
d8fe0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
d8ff0 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20  .**      to the 
d9000 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 6e  METHOD object in
d9010 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 75   the previous bu
d9020 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  llet..*/.#define
d9030 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 45   IOMETHODS(FINDE
d9040 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 45  R, METHOD, CLOSE
d9050 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20  , LOCK, UNLOCK, 
d9060 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 20  CKLOCK)         
d9070 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63        \.static c
d9080 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
d9090 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 3d  methods METHOD =
d90a0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
d90b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d90c0 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 20       \.   1,    
d90d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d90e0 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
d90f0 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  n */            
d9100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9110 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c 20      \.   CLOSE, 
d9120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9130 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a       /* xClose *
d9140 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
d9150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9160 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61 64     \.   unixRead
d9170 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
d9180 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 20      /* xRead */ 
d9190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d91a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d91b0 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 65    \.   unixWrite
d91c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
d91d0 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 20     /* xWrite */ 
d91e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d91f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9200 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 61   \.   unixTrunca
d9210 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
d9220 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a    /* xTruncate *
d9230 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
d9240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9250 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 20  \.   unixSync,  
d9260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9270 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 20   /* xSync */    
d9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
d92a0 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65  .   unixFileSize
d92b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
d92c0 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 20  /* xFileSize */ 
d92d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d92e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
d92f0 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20     LOCK,        
d9300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d9310 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20  * xLock */      
d9320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9330 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
d9340 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 20    UNLOCK,       
d9350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d9360 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 20   xUnlock */     
d9370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9380 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
d9390 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20   CKLOCK,        
d93a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d93b0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
d93c0 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  ck */           
d93d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
d93e0 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c  unixFileControl,
d93f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
d9400 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20  FileControl */  
d9410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9420 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75            \.   u
d9430 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 20  nixSectorSize,  
d9440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
d9450 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 20  ectorSize */    
d9460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9470 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e           \.   un
d9480 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65  ixDeviceCharacte
d9490 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 65  ristics   /* xDe
d94a0 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73  viceCapabilities
d94b0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
d94c0 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 20          \.};    
d94d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d94e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d94f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9510 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20         \.static 
d9520 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
d9530 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 52  _methods *FINDER
d9540 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61  ##Impl(const cha
d9550 72 20 2a 7a 2c 20 69 6e 74 20 68 29 7b 20 20 20  r *z, int h){   
d9560 20 20 20 20 20 20 5c 0a 20 20 55 4e 55 53 45 44        \.  UNUSED
d9570 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 55  _PARAMETER(z); U
d9580 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
d9590 68 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  h);             
d95a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d95b0 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e 20       \.  return 
d95c0 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 20  &METHOD;        
d95d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d95e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d95f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9600 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 20      \.}         
d9610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9650 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73     \.static cons
d9660 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
d9670 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 49  hods *(*const FI
d9680 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 72  NDER)(const char
d9690 2a 2c 69 6e 74 29 20 20 20 20 20 20 20 20 20 20  *,int)          
d96a0 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 52    \.    = FINDER
d96b0 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 48  ##Impl;../*.** H
d96c0 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 74  ere are all of t
d96d0 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
d96e0 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f  thods objects fo
d96f0 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a 2a  r each of the.**
d9700 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
d9710 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 20  ies.  Functions 
d9720 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 6e  that return poin
d9730 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d 65  ters to these me
d9740 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c 73  thods.** are als
d9750 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 4f  o created..*/.IO
d9760 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 78  METHODS(.  posix
d9770 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
d9780 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
d9790 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
d97a0 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73    posixIoMethods
d97b0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  ,           /* s
d97c0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
d97d0 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  s object name */
d97e0 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20  .  unixClose,   
d97f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d9800 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f  xClose method */
d9810 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20  .  unixLock,    
d9820 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d9830 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
d9840 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 20    unixUnlock,   
d9850 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
d9860 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
d9870 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65  .  unixCheckRese
d9880 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a 20  rvedLock     /* 
d9890 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
d98a0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49  ck method */.).I
d98b0 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f  OMETHODS(.  nolo
d98c0 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20  ckIoFinder,     
d98d0 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
d98e0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
d98f0 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f  .  nolockIoMetho
d9900 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ds,          /* 
d9910 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
d9920 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
d9930 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c  /.  nolockClose,
d9940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d9950 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
d9960 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20  /.  nolockLock, 
d9970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d9980 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
d9990 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c  .  nolockUnlock,
d99a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d99b0 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
d99c0 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52  /.  nolockCheckR
d99d0 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f 2a  eservedLock   /*
d99e0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
d99f0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
d9a00 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f 74  IOMETHODS(.  dot
d9a10 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20  lockIoFinder,   
d9a20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72         /* Finder
d9a30 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
d9a40 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74  /.  dotlockIoMet
d9a50 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f 2a  hods,         /*
d9a60 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
d9a70 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ods object name 
d9a80 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73  */.  dotlockClos
d9a90 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e,             /
d9aa0 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
d9ab0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b  */.  dotlockLock
d9ac0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
d9ad0 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  * xLock method *
d9ae0 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63  /.  dotlockUnloc
d9af0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  k,            /*
d9b00 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20   xUnlock method 
d9b10 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 63  */.  dotlockChec
d9b20 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 2f  kReservedLock  /
d9b30 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
d9b40 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29  Lock method */.)
d9b50 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
d9b60 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
d9b70 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53  E && !OS_VXWORKS
d9b80 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66 6c  .IOMETHODS(.  fl
d9b90 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  ockIoFinder,    
d9ba0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
d9bb0 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
d9bc0 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 68  */.  flockIoMeth
d9bd0 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ods,           /
d9be0 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
d9bf0 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
d9c00 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65   */.  flockClose
d9c10 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
d9c20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
d9c30 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c   */.  flockLock,
d9c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9c50 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
d9c60 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b  */.  flockUnlock
d9c70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
d9c80 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
d9c90 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63 6b   */.  flockCheck
d9ca0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20  ReservedLock    
d9cb0 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
d9cc0 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
d9cd0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f 53  ).#endif..#if OS
d9ce0 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f  _VXWORKS.IOMETHO
d9cf0 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64 65  DS(.  semIoFinde
d9d00 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
d9d10 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
d9d20 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d  on name */.  sem
d9d30 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20  IoMethods,      
d9d40 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
d9d50 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
d9d60 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65  ect name */.  se
d9d70 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20  mClose,         
d9d80 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
d9d90 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65  e method */.  se
d9da0 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  mLock,          
d9db0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
d9dc0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d   method */.  sem
d9dd0 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
d9de0 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63         /* xUnloc
d9df0 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65  k method */.  se
d9e00 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  mCheckReservedLo
d9e10 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63  ck      /* xChec
d9e20 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65  kReservedLock me
d9e30 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66  thod */.).#endif
d9e40 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  ..#if defined(__
d9e50 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49  APPLE__) && SQLI
d9e60 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
d9e70 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44  G_STYLE.IOMETHOD
d9e80 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 72  S(.  afpIoFinder
d9e90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
d9ea0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f  * Finder functio
d9eb0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 49  n name */.  afpI
d9ec0 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
d9ed0 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
d9ee0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
d9ef0 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70  ct name */.  afp
d9f00 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
d9f10 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
d9f20 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70   method */.  afp
d9f30 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
d9f40 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
d9f50 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55  method */.  afpU
d9f60 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  nlock,          
d9f70 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
d9f80 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70   method */.  afp
d9f90 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d9fa0 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b  k      /* xCheck
d9fb0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74  ReservedLock met
d9fc0 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a  hod */.).#endif.
d9fd0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79  ./*.** The proxy
d9fe0 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20   locking method 
d9ff0 69 73 20 61 20 22 73 75 70 65 72 2d 6d 65 74 68  is a "super-meth
da000 6f 64 22 20 69 6e 20 74 68 65 20 73 65 6e 73 65  od" in the sense
da010 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e   that it.** open
da020 73 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65  s secondary file
da030 20 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f 72   descriptors for
da040 20 74 68 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c   the conch and l
da050 6f 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a  ock files and.**
da060 20 69 74 20 75 73 65 73 20 70 72 6f 78 79 2c 20   it uses proxy, 
da070 64 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61  dot-file, AFP, a
da080 6e 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69  nd flock() locki
da090 6e 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68  ng methods on th
da0a0 6f 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79  ose.** secondary
da0b0 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 74 68 69   files.  For thi
da0c0 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69  s reason, the di
da0d0 76 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c  vision that impl
da0e0 65 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20  ements.** proxy 
da0f0 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74  locking is locat
da100 65 64 20 6d 75 63 68 20 66 75 72 74 68 65 72 20  ed much further 
da110 64 6f 77 6e 20 69 6e 20 74 68 65 20 66 69 6c 65  down in the file
da120 2e 20 20 42 75 74 20 77 65 20 6e 65 65 64 0a 2a  .  But we need.*
da130 2a 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e  * to go ahead an
da140 64 20 64 65 66 69 6e 65 20 74 68 65 20 73 71 6c  d define the sql
da150 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
da160 61 6e 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74  and finder funct
da170 69 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79  ion.** for proxy
da180 20 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e 20 20   locking here.  
da190 53 6f 20 77 65 20 66 6f 72 77 61 72 64 20 64 65  So we forward de
da1a0 63 6c 61 72 65 20 74 68 65 20 49 2f 4f 20 6d 65  clare the I/O me
da1b0 74 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65  thods..*/.#if de
da1c0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
da1d0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
da1e0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
da1f0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
da200 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69  Close(sqlite3_fi
da210 6c 65 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74  le*);.static int
da220 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74   proxyLock(sqlit
da230 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
da240 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
da250 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
da260 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74  ile*, int);.stat
da270 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63  ic int proxyChec
da280 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
da290 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
da2a0 2a 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20  *);.IOMETHODS(. 
da2b0 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20   proxyIoFinder, 
da2c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
da2d0 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61  nder function na
da2e0 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d  me */.  proxyIoM
da2f0 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20  ethods,         
da300 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f    /* sqlite3_io_
da310 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e  methods object n
da320 61 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 43 6c  ame */.  proxyCl
da330 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ose,            
da340 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74     /* xClose met
da350 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f  hod */.  proxyLo
da360 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
da370 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68     /* xLock meth
da380 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 55 6e 6c  od */.  proxyUnl
da390 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
da3a0 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74    /* xUnlock met
da3b0 68 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 43 68  hod */.  proxyCh
da3c0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
da3d0 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65     /* xCheckRese
da3e0 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  rvedLock method 
da3f0 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  */.).#endif...#i
da400 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
da410 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
da420 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
da430 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20  YLE./* .** This 
da440 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f  "finder" functio
da450 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65  n attempts to de
da460 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74  termine the best
da470 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
da480 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61  y .** for the da
da490 74 61 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c  tabase file "fil
da4a0 65 50 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e  ePath".  It then
da4b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c   returns the sql
da4c0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a  ite3_io_methods.
da4d0 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69  ** object that i
da4e0 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73  mplements that s
da4f0 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54  trategy..**.** T
da500 68 69 73 20 69 73 20 66 6f 72 20 4d 61 63 4f 53  his is for MacOS
da510 58 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69  X only..*/.stati
da520 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
da530 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f  io_methods *auto
da540 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c  lockIoFinderImpl
da550 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
da560 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20  filePath,    /* 
da570 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
da580 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69  base file */.  i
da590 6e 74 20 66 64 20 20 20 20 20 20 20 20 20 20 20  nt fd           
da5a0 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20          /* file 
da5b0 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20  descriptor open 
da5c0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
da5d0 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 61  file */.){.  sta
da5e0 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
da5f0 20 4d 61 70 70 69 6e 67 20 7b 0a 20 20 20 20 63   Mapping {.    c
da600 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
da610 73 79 73 74 65 6d 3b 20 20 20 20 20 20 20 20 20  system;         
da620 20 20 20 20 20 2f 2a 20 46 69 6c 65 73 79 73 74       /* Filesyst
da630 65 6d 20 74 79 70 65 20 6e 61 6d 65 20 2a 2f 0a  em type name */.
da640 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65      const sqlite
da650 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
da660 65 74 68 6f 64 73 3b 20 20 20 2f 2a 20 41 70 70  ethods;   /* App
da670 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69 6e 67  ropriate locking
da680 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7d 20 61   method */.  } a
da690 4d 61 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20  Map[] = {.    { 
da6a0 22 68 66 73 22 2c 20 20 20 20 26 70 6f 73 69 78  "hfs",    &posix
da6b0 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20  IoMethods },.   
da6c0 20 7b 20 22 75 66 73 22 2c 20 20 20 20 26 70 6f   { "ufs",    &po
da6d0 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a  sixIoMethods },.
da6e0 20 20 20 20 7b 20 22 61 66 70 66 73 22 2c 20 20      { "afpfs",  
da6f0 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c  &afpIoMethods },
da700 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
da710 4e 41 42 4c 45 5f 41 46 50 5f 4c 4f 43 4b 49 4e  NABLE_AFP_LOCKIN
da720 47 5f 53 4d 42 0a 20 20 20 20 7b 20 22 73 6d 62  G_SMB.    { "smb
da730 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 68  fs",  &afpIoMeth
da740 6f 64 73 20 7d 2c 0a 23 65 6c 73 65 0a 20 20 20  ods },.#else.   
da750 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 66 6c   { "smbfs",  &fl
da760 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a  ockIoMethods },.
da770 23 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 77 65  #endif.    { "we
da780 62 64 61 76 22 2c 20 26 6e 6f 6c 6f 63 6b 49 6f  bdav", &nolockIo
da790 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b  Methods },.    {
da7a0 20 30 2c 20 30 20 7d 0a 20 20 7d 3b 0a 20 20 69   0, 0 }.  };.  i
da7b0 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 73  nt i;.  struct s
da7c0 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20  tatfs fsInfo;.  
da7d0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
da7e0 6b 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66  kInfo;..  if( !f
da7f0 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20 20 2f  ilePath ){.    /
da800 2a 20 49 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e  * If filePath==N
da810 55 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 77  ULL that means w
da820 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
da830 74 68 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66  th a transient f
da840 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  ile.    ** that 
da850 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
da860 20 62 65 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20   be locked. */. 
da870 20 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63     return &noloc
da880 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a  kIoMethods;.  }.
da890 20 20 69 66 28 20 73 74 61 74 66 73 28 66 69 6c    if( statfs(fil
da8a0 65 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20  ePath, &fsInfo) 
da8b0 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 69 66 28  != -1 ){.    if(
da8c0 20 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 20   fsInfo.f_flags 
da8d0 26 20 4d 4e 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a  & MNT_RDONLY ){.
da8e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 26 6e 6f        return &no
da8f0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  lockIoMethods;. 
da900 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30     }.    for(i=0
da910 3b 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73  ; aMap[i].zFiles
da920 79 73 74 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20  ystem; i++){.   
da930 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 66 73     if( strcmp(fs
da940 49 6e 66 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d  Info.f_fstypenam
da950 65 2c 20 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65  e, aMap[i].zFile
da960 73 79 73 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 20  system)==0 ){.  
da970 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61        return aMa
da980 70 5b 69 5d 2e 70 4d 65 74 68 6f 64 73 3b 0a 20  p[i].pMethods;. 
da990 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
da9a0 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63  ..  /* Default c
da9b0 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20 61 6d  ase. Handles, am
da9c0 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20 22 6e  ongst others, "n
da9d0 66 73 22 2e 0a 20 20 2a 2a 20 54 65 73 74 20 62  fs"..  ** Test b
da9e0 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 75  yte-range lock u
da9f0 73 69 6e 67 20 66 63 6e 74 6c 28 29 2e 20 49 66  sing fcntl(). If
daa00 20 74 68 65 20 63 61 6c 6c 20 73 75 63 63 65 65   the call succee
daa10 64 73 2c 20 0a 20 20 2a 2a 20 61 73 73 75 6d 65  ds, .  ** assume
daa20 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73   that the file-s
daa30 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 50  ystem supports P
daa40 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 6b 73  OSIX style locks
daa50 2e 20 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e  . .  */.  lockIn
daa60 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20  fo.l_len = 1;.  
daa70 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74  lockInfo.l_start
daa80 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f   = 0;.  lockInfo
daa90 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b  .l_whence = SEEK
daaa0 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f  _SET;.  lockInfo
daab0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43  .l_type = F_RDLC
daac0 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 66  K;.  if( fcntl(f
daad0 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63  d, F_GETLK, &loc
daae0 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20  kInfo)!=-1 ) {. 
daaf0 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78     return &posix
dab00 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c  IoMethods;.  }el
dab10 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26  se{.    return &
dab20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73  dotlockIoMethods
dab30 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63  ;.  }.}.static c
dab40 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
dab50 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74  methods *(*const
dab60 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65   autolockIoFinde
dab70 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  r)(const char*,i
dab80 6e 74 29 0a 20 20 20 20 20 20 20 20 3d 20 61 75  nt).        = au
dab90 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d  tolockIoFinderIm
daba0 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64  pl;..#endif /* d
dabb0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
dabc0 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42  ) && SQLITE_ENAB
dabd0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
dabe0 20 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f   */..#if OS_VXWO
dabf0 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e  RKS && SQLITE_EN
dac00 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
dac10 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22  LE./* .** This "
dac20 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e  finder" function
dac30 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74   attempts to det
dac40 65 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20  ermine the best 
dac50 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79  locking strategy
dac60 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74   .** for the dat
dac70 61 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65  abase file "file
dac80 50 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20  Path".  It then 
dac90 72 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69  returns the sqli
daca0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a  te3_io_methods.*
dacb0 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d  * object that im
dacc0 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74  plements that st
dacd0 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rategy..**.** Th
dace0 69 73 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b  is is for VXWork
dacf0 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69  s only..*/.stati
dad00 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
dad10 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f  io_methods *auto
dad20 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c  lockIoFinderImpl
dad30 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
dad40 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20  filePath,    /* 
dad50 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
dad60 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69  base file */.  i
dad70 6e 74 20 66 64 20 20 20 20 20 20 20 20 20 20 20  nt fd           
dad80 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20          /* file 
dad90 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20  descriptor open 
dada0 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
dadb0 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72  file */.){.  str
dadc0 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e  uct flock lockIn
dadd0 66 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c 65  fo;..  if( !file
dade0 50 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Path ){.    /* I
dadf0 66 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c  f filePath==NULL
dae00 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61   that means we a
dae10 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  re dealing with 
dae20 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65  a transient file
dae30 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65  .    ** that doe
dae40 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  s not need to be
dae50 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20   locked. */.    
dae60 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f  return &nolockIo
dae70 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 0a 20 20  Methods;.  }..  
dae80 2f 2a 20 54 65 73 74 20 69 66 20 66 63 6e 74 6c  /* Test if fcntl
dae90 28 29 20 69 73 20 73 75 70 70 6f 72 74 65 64 20  () is supported 
daea0 61 6e 64 20 75 73 65 20 50 4f 53 49 58 20 73 74  and use POSIX st
daeb0 79 6c 65 20 6c 6f 63 6b 73 2e 0a 20 20 2a 2a 20  yle locks..  ** 
daec0 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 62  Otherwise fall b
daed0 61 63 6b 20 74 6f 20 74 68 65 20 6e 61 6d 65 64  ack to the named
daee0 20 73 65 6d 61 70 68 6f 72 65 20 6d 65 74 68 6f   semaphore metho
daef0 64 2e 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e  d..  */.  lockIn
daf00 66 6f 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20  fo.l_len = 1;.  
daf10 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74  lockInfo.l_start
daf20 20 3d 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f   = 0;.  lockInfo
daf30 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b  .l_whence = SEEK
daf40 5f 53 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f  _SET;.  lockInfo
daf50 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43  .l_type = F_RDLC
daf60 4b 3b 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 66  K;.  if( fcntl(f
daf70 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63  d, F_GETLK, &loc
daf80 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20  kInfo)!=-1 ) {. 
daf90 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78     return &posix
dafa0 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c  IoMethods;.  }el
dafb0 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26  se{.    return &
dafc0 73 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20  semIoMethods;.  
dafd0 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  }.}.static const
dafe0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
daff0 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 61 75 74  ods *(*const aut
db000 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28 63  olockIoFinder)(c
db010 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 0a  onst char*,int).
db020 20 20 20 20 20 20 20 20 3d 20 61 75 74 6f 6c 6f          = autolo
db030 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a  ckIoFinderImpl;.
db040 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58  .#endif /* OS_VX
db050 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f  WORKS && SQLITE_
db060 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
db070 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  TYLE */../*.** A
db080 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20  n abstract type 
db090 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  for a pointer to
db0a0 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e   a IO method fin
db0b0 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f  der function:.*/
db0c0 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73  .typedef const s
db0d0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
db0e0 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65  s *(*finder_type
db0f0 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  )(const char*,in
db100 74 29 3b 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  t);.../*********
db110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db150 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
db160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db170 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74   sqlite3_vfs met
db180 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  hods ***********
db190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
db1a0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76  *.**.** This div
db1b0 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74  ision contains t
db1c0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
db1d0 6e 20 6f 66 20 6d 65 74 68 6f 64 73 20 6f 6e 20  n of methods on 
db1e0 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  the.** sqlite3_v
db1f0 66 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 0a 2f  fs object..*/../
db200 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
db210 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
db220 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72  the unixFile str
db230 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74  ucture pointed t
db240 6f 20 62 79 20 70 49 64 2e 0a 2a 2f 0a 73 74 61  o by pId..*/.sta
db250 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e  tic int fillInUn
db260 69 78 46 69 6c 65 28 0a 20 20 73 71 6c 69 74 65  ixFile(.  sqlite
db270 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
db280 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
db290 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  vfs object */.  
db2a0 69 6e 74 20 68 2c 20 20 20 20 20 20 20 20 20 20  int h,          
db2b0 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20          /* Open 
db2c0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
db2d0 6f 66 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70  of file being op
db2e0 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69  ened */.  int di
db2f0 72 66 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  rfd,            
db300 20 20 2f 2a 20 44 69 72 65 63 74 6f 72 79 20 66    /* Directory f
db310 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
db320 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
db330 20 2a 70 49 64 2c 20 20 20 20 20 20 2f 2a 20 57   *pId,      /* W
db340 72 69 74 65 20 74 6f 20 74 68 65 20 75 6e 69 78  rite to the unix
db350 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 68  File structure h
db360 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ere */.  const c
db370 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
db380 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
db390 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65  file being opene
db3a0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63  d */.  int noLoc
db3b0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k,             /
db3c0 2a 20 4f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69  * Omit locking i
db3d0 66 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20  f true */.  int 
db3e0 69 73 44 65 6c 65 74 65 20 20 20 20 20 20 20 20  isDelete        
db3f0 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e      /* Delete on
db400 20 63 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a   close if true *
db410 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  /.){.  const sql
db420 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
db430 2a 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a  *pLockingStyle;.
db440 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77    unixFile *pNew
db450 20 3d 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 70   = (unixFile *)p
db460 49 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  Id;.  int rc = S
db470 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73  QLITE_OK;..  ass
db480 65 72 74 28 20 70 4e 65 77 2d 3e 70 4c 6f 63 6b  ert( pNew->pLock
db490 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65  ==NULL );.  asse
db4a0 72 74 28 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 3d  rt( pNew->pOpen=
db4b0 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a 20 50  =NULL );..  /* P
db4c0 61 72 61 6d 65 74 65 72 20 69 73 44 65 6c 65 74  arameter isDelet
db4d0 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 6f  e is only used o
db4e0 6e 20 76 78 77 6f 72 6b 73 2e 0a 20 20 2a 2a 20  n vxworks..  ** 
db4f0 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78 70  Express this exp
db500 6c 69 63 69 74 6c 79 20 68 65 72 65 20 74 6f 20  licitly here to 
db510 70 72 65 76 65 6e 74 20 63 6f 6d 70 69 6c 65 72  prevent compiler
db520 20 77 61 72 6e 69 6e 67 73 0a 20 20 2a 2a 20 61   warnings.  ** a
db530 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61 72 61  bout unused para
db540 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 23 69 66  meters..  */.#if
db550 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 55   !OS_VXWORKS.  U
db560 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
db570 69 73 44 65 6c 65 74 65 29 3b 0a 23 65 6e 64 69  isDelete);.#endi
db580 66 0a 0a 20 20 4f 53 54 52 41 43 45 33 28 22 4f  f..  OSTRACE3("O
db590 50 45 4e 20 20 20 20 25 2d 33 64 20 25 73 5c 6e  PEN    %-3d %s\n
db5a0 22 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29  ", h, zFilename)
db5b0 3b 20 20 20 20 0a 20 20 70 4e 65 77 2d 3e 68 20  ;    .  pNew->h 
db5c0 3d 20 68 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72  = h;.  pNew->dir
db5d0 66 64 20 3d 20 64 69 72 66 64 3b 0a 20 20 53 45  fd = dirfd;.  SE
db5e0 54 5f 54 48 52 45 41 44 49 44 28 70 4e 65 77 29  T_THREADID(pNew)
db5f0 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  ;..#if OS_VXWORK
db600 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20 3d 20  S.  pNew->pId = 
db610 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49  vxworksFindFileI
db620 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  d(zFilename);.  
db630 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d 3d 30  if( pNew->pId==0
db640 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b 20 3d   ){.    noLock =
db650 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   1;.    rc = SQL
db660 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23  ITE_NOMEM;.  }.#
db670 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f 4c  endif..  if( noL
db680 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b  ock ){.    pLock
db690 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f 6c 6f  ingStyle = &nolo
db6a0 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  ckIoMethods;.  }
db6b0 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 69  else{.    pLocki
db6c0 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28 66 69  ngStyle = (**(fi
db6d0 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66 73 2d  nder_type*)pVfs-
db6e0 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69 6c 65  >pAppData)(zFile
db6f0 6e 61 6d 65 2c 20 68 29 3b 0a 23 69 66 20 53 51  name, h);.#if SQ
db700 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
db710 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 2f 2a  ING_STYLE.    /*
db720 20 43 61 63 68 65 20 7a 46 69 6c 65 6e 61 6d 65   Cache zFilename
db730 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   in the locking 
db740 63 6f 6e 74 65 78 74 20 28 41 46 50 20 61 6e 64  context (AFP and
db750 20 64 6f 74 6c 6f 63 6b 20 6f 76 65 72 72 69 64   dotlock overrid
db760 65 29 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70 72  e) for.    ** pr
db770 6f 78 79 4c 6f 63 6b 20 61 63 74 69 76 61 74 69  oxyLock activati
db780 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 20 28  on is possible (
db790 72 65 6d 6f 74 65 20 70 72 6f 78 79 20 69 73 20  remote proxy is 
db7a0 62 61 73 65 64 20 6f 6e 20 64 62 20 6e 61 6d 65  based on db name
db7b0 29 0a 20 20 20 20 2a 2a 20 7a 46 69 6c 65 6e 61  ).    ** zFilena
db7c0 6d 65 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64  me remains valid
db7d0 20 75 6e 74 69 6c 20 66 69 6c 65 20 69 73 20 63   until file is c
db7e0 6c 6f 73 65 64 2c 20 74 6f 20 73 75 70 70 6f 72  losed, to suppor
db7f0 74 20 2a 2f 0a 20 20 20 20 70 4e 65 77 2d 3e 6c  t */.    pNew->l
db800 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
db810 28 76 6f 69 64 2a 29 7a 46 69 6c 65 6e 61 6d 65  (void*)zFilename
db820 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  ;.#endif.  }..  
db830 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c  if( pLockingStyl
db840 65 20 3d 3d 20 26 70 6f 73 69 78 49 6f 4d 65 74  e == &posixIoMet
db850 68 6f 64 73 20 29 7b 0a 20 20 20 20 75 6e 69 78  hods ){.    unix
db860 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
db870 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49    rc = findLockI
db880 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e 65 77 2d  nfo(pNew, &pNew-
db890 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 2d 3e 70  >pLock, &pNew->p
db8a0 4f 70 65 6e 29 3b 0a 20 20 20 20 75 6e 69 78 4c  Open);.    unixL
db8b0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
db8c0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
db8d0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
db8e0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41  E && defined(__A
db8f0 50 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69  PPLE__).  else i
db900 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  f( pLockingStyle
db910 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64   == &afpIoMethod
db920 73 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20  s ){.    /* AFP 
db930 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65  locking uses the
db940 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74   file path so it
db950 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63   needs to be inc
db960 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20  luded in.    ** 
db970 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  the afpLockingCo
db980 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ntext..    */.  
db990 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74    afpLockingCont
db9a0 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70  ext *pCtx;.    p
db9b0 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  New->lockingCont
db9c0 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c  ext = pCtx = sql
db9d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
db9e0 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20  eof(*pCtx) );.  
db9f0 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b    if( pCtx==0 ){
dba00 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
dba10 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
dba20 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42  lse{.      /* NB
dba30 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73  : zFilename exis
dba40 74 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76  ts and remains v
dba50 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66  alid until the f
dba60 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20  ile is closed.  
dba70 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67      ** according
dba80 20 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20   to requirement 
dba90 46 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64  F11141.  So we d
dbaa0 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61  o not need to ma
dbab0 6b 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f  ke a.      ** co
dbac0 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61  py of the filena
dbad0 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74  me. */.      pCt
dbae0 78 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c  x->dbPath = zFil
dbaf0 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61  ename;.      sra
dbb00 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20  ndomdev();.     
dbb10 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
dbb20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69  );.      rc = fi
dbb30 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c  ndLockInfo(pNew,
dbb40 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f   NULL, &pNew->pO
dbb50 70 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78  pen);.      unix
dbb60 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20  LeaveMutex();   
dbb70 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a       .    }.  }.
dbb80 23 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69  #endif..  else i
dbb90 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  f( pLockingStyle
dbba0 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65   == &dotlockIoMe
dbbb0 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
dbbc0 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  Dotfile locking 
dbbd0 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61  uses the file pa
dbbe0 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74  th so it needs t
dbbf0 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e  o be included in
dbc00 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c  .    ** the dotl
dbc10 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
dbc20 74 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68  t .    */.    ch
dbc30 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20  ar *zLockFile;. 
dbc40 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65     int nFilename
dbc50 3b 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20  ;.    nFilename 
dbc60 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46  = (int)strlen(zF
dbc70 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20  ilename) + 6;.  
dbc80 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63    zLockFile = (c
dbc90 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  har *)sqlite3_ma
dbca0 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b  lloc(nFilename);
dbcb0 0a 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69  .    if( zLockFi
dbcc0 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  le==0 ){.      r
dbcd0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
dbce0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
dbcf0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
dbd00 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a  ntf(nFilename, z
dbd10 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44  LockFile, "%s" D
dbd20 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a  OTLOCK_SUFFIX, z
dbd30 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d  Filename);.    }
dbd40 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69  .    pNew->locki
dbd50 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63  ngContext = zLoc
dbd60 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20  kFile;.  }..#if 
dbd70 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73  OS_VXWORKS.  els
dbd80 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74  e if( pLockingSt
dbd90 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74  yle == &semIoMet
dbda0 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e  hods ){.    /* N
dbdb0 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c  amed semaphore l
dbdc0 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20  ocking uses the 
dbdd0 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20  file path so it 
dbde0 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20  needs to be.    
dbdf0 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74  ** included in t
dbe00 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e  he semLockingCon
dbe10 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20  text.    */.    
dbe20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
dbe30 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
dbe40 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70  ockInfo(pNew, &p
dbe50 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65  New->pLock, &pNe
dbe60 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69  w->pOpen);.    i
dbe70 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  f( (rc==SQLITE_O
dbe80 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70  K) && (pNew->pOp
dbe90 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20  en->pSem==NULL) 
dbea0 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
dbeb0 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e  SemName = pNew->
dbec0 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b  pOpen->aSemName;
dbed0 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  .      int n;.  
dbee0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
dbef0 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d  intf(MAX_PATHNAM
dbf00 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 25 73  E, zSemName, "%s
dbf10 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20  .sem",.         
dbf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
dbf30 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69  ew->pId->zCanoni
dbf40 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  calName);.      
dbf50 66 6f 72 28 20 6e 3d 30 3b 20 7a 53 65 6d 4e 61  for( n=0; zSemNa
dbf60 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20  me[n]; n++ ).   
dbf70 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d       if( zSemNam
dbf80 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d  e[n]=='/' ) zSem
dbf90 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20  Name[n] = '_';. 
dbfa0 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e       pNew->pOpen
dbfb0 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65  ->pSem = sem_ope
dbfc0 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52  n(zSemName, O_CR
dbfd0 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20  EAT, 0666, 1);. 
dbfe0 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70       if( pNew->p
dbff0 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 45  Open->pSem == SE
dc000 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 20  M_FAILED ){.    
dc010 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
dc020 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70  NOMEM;.        p
dc030 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d  New->pOpen->aSem
dc040 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a  Name[0] = '\0';.
dc050 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
dc060 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
dc070 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ();.  }.#endif. 
dc080 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 72   .  pNew->lastEr
dc090 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 5f  rno = 0;.#if OS_
dc0a0 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63  VXWORKS.  if( rc
dc0b0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
dc0c0 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e     unlink(zFilen
dc0d0 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65  ame);.    isDele
dc0e0 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e  te = 0;.  }.  pN
dc0f0 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 69  ew->isDelete = i
dc100 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 0a  sDelete;.#endif.
dc110 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
dc120 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 64  _OK ){.    if( d
dc130 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28  irfd>=0 ) close(
dc140 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e  dirfd); /* silen
dc150 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20  t leak if fail, 
dc160 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72  already in error
dc170 20 2a 2f 0a 20 20 20 20 63 6c 6f 73 65 28 68 29   */.    close(h)
dc180 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
dc190 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70  New->pMethod = p
dc1a0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20  LockingStyle;.  
dc1b0 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31    OpenCounter(+1
dc1c0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
dc1d0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  rc;.}../*.** Ope
dc1e0 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  n a file descrip
dc1f0 74 6f 72 20 74 6f 20 74 68 65 20 64 69 72 65 63  tor to the direc
dc200 74 6f 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20  tory containing 
dc210 66 69 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a  file zFilename..
dc220 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
dc230 2c 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f  , *pFd is set to
dc240 20 74 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65   the opened file
dc250 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a   descriptor and.
dc260 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
dc270 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20  returned. If an 
dc280 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69  error occurs, ei
dc290 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ther SQLITE_NOME
dc2a0 4d 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43  M.** or SQLITE_C
dc2b0 41 4e 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72  ANTOPEN is retur
dc2c0 6e 65 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20  ned and *pFd is 
dc2d0 73 65 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69  set to an undefi
dc2e0 6e 65 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a  ned.** value..**
dc2f0 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b  .** If SQLITE_OK
dc300 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
dc310 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70  e caller is resp
dc320 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73  onsible for clos
dc330 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20  ing.** the file 
dc340 64 65 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20  descriptor *pFd 
dc350 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a  using close()..*
dc360 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65  /.static int ope
dc370 6e 44 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74  nDirectory(const
dc380 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
dc390 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69  , int *pFd){.  i
dc3a0 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20  nt ii;.  int fd 
dc3b0 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69  = -1;.  char zDi
dc3c0 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41  rname[MAX_PATHNA
dc3d0 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65  ME+1];..  sqlite
dc3e0 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50  3_snprintf(MAX_P
dc3f0 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d  ATHNAME, zDirnam
dc400 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61  e, "%s", zFilena
dc410 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69  me);.  for(ii=(i
dc420 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61  nt)strlen(zDirna
dc430 6d 65 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69  me); ii>1 && zDi
dc440 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20  rname[ii]!='/'; 
dc450 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e  ii--);.  if( ii>
dc460 30 20 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d  0 ){.    zDirnam
dc470 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  e[ii] = '\0';.  
dc480 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72    fd = open(zDir
dc490 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f  name, O_RDONLY|O
dc4a0 5f 42 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20  _BINARY, 0);.   
dc4b0 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 23 69   if( fd>=0 ){.#i
dc4c0 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a  fdef FD_CLOEXEC.
dc4d0 20 20 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20        fcntl(fd, 
dc4e0 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66  F_SETFD, fcntl(f
dc4f0 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c  d, F_GETFD, 0) |
dc500 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65   FD_CLOEXEC);.#e
dc510 6e 64 69 66 0a 20 20 20 20 20 20 4f 53 54 52 41  ndif.      OSTRA
dc520 43 45 33 28 22 4f 50 45 4e 44 49 52 20 25 2d 33  CE3("OPENDIR %-3
dc530 64 20 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69  d %s\n", fd, zDi
dc540 72 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  rname);.    }.  
dc550 7d 0a 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20  }.  *pFd = fd;. 
dc560 20 72 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f 53   return (fd>=0?S
dc570 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f  QLITE_OK:SQLITE_
dc580 43 41 4e 54 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a  CANTOPEN);.}../*
dc590 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d  .** Create a tem
dc5a0 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
dc5b0 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20   in zBuf.  zBuf 
dc5c0 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65  must be allocate
dc5d0 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c  d.** by the call
dc5e0 69 6e 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20  ing process and 
dc5f0 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75  must be big enou
dc600 67 68 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65  gh to hold at le
dc610 61 73 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50  ast.** pVfs->mxP
dc620 61 74 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a  athname bytes..*
dc630 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
dc640 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75  Tempname(int nBu
dc650 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
dc660 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
dc670 68 61 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20  har *azDirs[] = 
dc680 7b 0a 20 20 20 20 20 30 2c 0a 20 20 20 20 20 30  {.     0,.     0
dc690 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70  ,.     "/var/tmp
dc6a0 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d  ",.     "/usr/tm
dc6b0 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c  p",.     "/tmp",
dc6c0 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a  .     ".",.  };.
dc6d0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
dc6e0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68  nsigned char zCh
dc6f0 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63  ars[] =.    "abc
dc700 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73  defghijklmnopqrs
dc710 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42  tuvwxyz".    "AB
dc720 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52  CDEFGHIJKLMNOPQR
dc730 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30  STUVWXYZ".    "0
dc740 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 75 6e  123456789";.  un
dc750 73 69 67 6e 65 64 20 69 6e 74 20 69 2c 20 6a 3b  signed int i, j;
dc760 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62  .  struct stat b
dc770 75 66 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  uf;.  const char
dc780 20 2a 7a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20   *zDir = ".";.. 
dc790 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20   /* It's odd to 
dc7a0 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65  simulate an io-e
dc7b0 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72  rror here, but r
dc7c0 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75  eally this is ju
dc7d0 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68  st.  ** using th
dc7e0 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61  e io-error infra
dc7f0 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73  structure to tes
dc800 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61  t that SQLite ha
dc810 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20  ndles this.  ** 
dc820 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67  function failing
dc830 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61  . .  */.  Simula
dc840 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
dc850 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  n SQLITE_IOERR )
dc860 3b 0a 0a 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d  ;..  azDirs[0] =
dc870 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
dc880 72 65 63 74 6f 72 79 3b 0a 20 20 69 66 20 28 4e  rectory;.  if (N
dc890 55 4c 4c 20 3d 3d 20 61 7a 44 69 72 73 5b 31 5d  ULL == azDirs[1]
dc8a0 29 20 7b 0a 20 20 20 20 61 7a 44 69 72 73 5b 31  ) {.    azDirs[1
dc8b0 5d 20 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44  ] = getenv("TMPD
dc8c0 49 52 22 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 66  IR");.  }.  .  f
dc8d0 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
dc8e0 28 61 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28  (azDirs)/sizeof(
dc8f0 61 7a 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29  azDirs[0]); i++)
dc900 7b 0a 20 20 20 20 69 66 28 20 61 7a 44 69 72 73  {.    if( azDirs
dc910 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  [i]==0 ) continu
dc920 65 3b 0a 20 20 20 20 69 66 28 20 73 74 61 74 28  e;.    if( stat(
dc930 61 7a 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29  azDirs[i], &buf)
dc940 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
dc950 20 69 66 28 20 21 53 5f 49 53 44 49 52 28 62 75   if( !S_ISDIR(bu
dc960 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e  f.st_mode) ) con
dc970 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61  tinue;.    if( a
dc980 63 63 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c  ccess(azDirs[i],
dc990 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b   07) ) continue;
dc9a0 0a 20 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69  .    zDir = azDi
dc9b0 72 73 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b  rs[i];.    break
dc9c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
dc9d0 6b 20 74 68 61 74 20 74 68 65 20 6f 75 74 70 75  k that the outpu
dc9e0 74 20 62 75 66 66 65 72 20 69 73 20 6c 61 72 67  t buffer is larg
dc9f0 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 74 68 65  e enough for the
dca00 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
dca10 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 49 66 20 69  .  ** name. If i
dca20 74 20 69 73 20 6e 6f 74 2c 20 72 65 74 75 72 6e  t is not, return
dca30 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 20   SQLITE_ERROR.. 
dca40 20 2a 2f 0a 20 20 69 66 28 20 28 73 74 72 6c 65   */.  if( (strle
dca50 6e 28 7a 44 69 72 29 20 2b 20 73 74 72 6c 65 6e  n(zDir) + strlen
dca60 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c  (SQLITE_TEMP_FIL
dca70 45 5f 50 52 45 46 49 58 29 20 2b 20 31 37 29 20  E_PREFIX) + 17) 
dca80 3e 3d 20 28 73 69 7a 65 5f 74 29 6e 42 75 66 20  >= (size_t)nBuf 
dca90 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
dcaa0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
dcab0 0a 20 20 64 6f 7b 0a 20 20 20 20 73 71 6c 69 74  .  do{.    sqlit
dcac0 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
dcad0 2d 31 37 2c 20 7a 42 75 66 2c 20 22 25 73 2f 22  -17, zBuf, "%s/"
dcae0 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45  SQLITE_TEMP_FILE
dcaf0 5f 50 52 45 46 49 58 2c 20 7a 44 69 72 29 3b 0a  _PREFIX, zDir);.
dcb00 20 20 20 20 6a 20 3d 20 28 69 6e 74 29 73 74 72      j = (int)str
dcb10 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73  len(zBuf);.    s
dcb20 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
dcb30 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b  s(15, &zBuf[j]);
dcb40 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
dcb50 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20  15; i++, j++){. 
dcb60 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28       zBuf[j] = (
dcb70 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75  char)zChars[ ((u
dcb80 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75  nsigned char)zBu
dcb90 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43  f[j])%(sizeof(zC
dcba0 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20  hars)-1) ];.    
dcbb0 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20  }.    zBuf[j] = 
dcbc0 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63  0;.  }while( acc
dcbd0 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29  ess(zBuf,0)==0 )
dcbe0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
dcbf0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c  E_OK;.}..#if SQL
dcc00 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
dcc10 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69  NG_STYLE && defi
dcc20 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f  ned(__APPLE__)./
dcc30 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20  *.** Routine to 
dcc40 74 72 61 6e 73 66 6f 72 6d 20 61 20 75 6e 69 78  transform a unix
dcc50 46 69 6c 65 20 69 6e 74 6f 20 61 20 70 72 6f 78  File into a prox
dcc60 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e 69 78 46 69  y-locking unixFi
dcc70 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  le..** Implement
dcc80 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f  ation in the pro
dcc90 78 79 2d 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e  xy-lock division
dcca0 2c 20 62 75 74 20 75 73 65 64 20 62 79 20 75 6e  , but used by un
dccb0 69 78 4f 70 65 6e 28 29 0a 2a 2a 20 69 66 20 53  ixOpen().** if S
dccc0 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f  QLITE_PREFER_PRO
dccd0 58 59 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65  XY_LOCKING is de
dcce0 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  fined..*/.static
dccf0 20 69 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66   int proxyTransf
dcd00 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78  ormUnixFile(unix
dcd10 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  File*, const cha
dcd20 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  r*);.#endif.../*
dcd30 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c  .** Open the fil
dcd40 65 20 7a 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20  e zPath..** .** 
dcd50 50 72 65 76 69 6f 75 73 6c 79 2c 20 74 68 65 20  Previously, the 
dcd60 53 51 4c 69 74 65 20 4f 53 20 6c 61 79 65 72 20  SQLite OS layer 
dcd70 75 73 65 64 20 74 68 72 65 65 20 66 75 6e 63 74  used three funct
dcd80 69 6f 6e 73 20 69 6e 20 70 6c 61 63 65 20 6f 66  ions in place of
dcd90 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a   this.** one:.**
dcda0 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f  .**     sqlite3O
dcdb0 73 4f 70 65 6e 52 65 61 64 57 72 69 74 65 28 29  sOpenReadWrite()
dcdc0 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ;.**     sqlite3
dcdd0 4f 73 4f 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29  OsOpenReadOnly()
dcde0 3b 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ;.**     sqlite3
dcdf0 4f 73 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28  OsOpenExclusive(
dce00 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  );.**.** These c
dce10 61 6c 6c 73 20 63 6f 72 72 65 73 70 6f 6e 64 20  alls correspond 
dce20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
dce30 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66   combinations of
dce40 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20   flags:.**.**   
dce50 20 20 52 65 61 64 57 72 69 74 65 28 29 20 2d 3e    ReadWrite() ->
dce60 20 20 20 20 20 28 52 45 41 44 57 52 49 54 45 20       (READWRITE 
dce70 7c 20 43 52 45 41 54 45 29 0a 2a 2a 20 20 20 20  | CREATE).**    
dce80 20 52 65 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20   ReadOnly()  -> 
dce90 20 20 20 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a      (READONLY) .
dcea0 2a 2a 20 20 20 20 20 4f 70 65 6e 45 78 63 6c 75  **     OpenExclu
dceb0 73 69 76 65 28 29 20 2d 3e 20 28 52 45 41 44 57  sive() -> (READW
dcec0 52 49 54 45 20 7c 20 43 52 45 41 54 45 20 7c 20  RITE | CREATE | 
dced0 45 58 43 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a  EXCLUSIVE).**.**
dcee0 20 54 68 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63   The old OpenExc
dcef0 6c 75 73 69 76 65 28 29 20 61 63 63 65 70 74 65  lusive() accepte
dcf00 64 20 61 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75  d a boolean argu
dcf10 6d 65 6e 74 20 2d 20 22 64 65 6c 46 6c 61 67 22  ment - "delFlag"
dcf20 2e 20 49 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68  . If.** true, th
dcf30 65 20 66 69 6c 65 20 77 61 73 20 63 6f 6e 66 69  e file was confi
dcf40 67 75 72 65 64 20 74 6f 20 62 65 20 61 75 74 6f  gured to be auto
dcf50 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65  matically delete
dcf60 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69  d when the.** fi
dcf70 6c 65 20 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64  le handle closed
dcf80 2e 20 54 6f 20 61 63 68 69 65 76 65 20 74 68 65  . To achieve the
dcf90 20 73 61 6d 65 20 65 66 66 65 63 74 20 75 73 69   same effect usi
dcfa0 6e 67 20 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20  ng this new .** 
dcfb0 69 6e 74 65 72 66 61 63 65 2c 20 61 64 64 20 74  interface, add t
dcfc0 68 65 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  he DELETEONCLOSE
dcfd0 20 66 6c 61 67 20 74 6f 20 74 68 6f 73 65 20 73   flag to those s
dcfe0 70 65 63 69 66 69 65 64 20 61 62 6f 76 65 20 66  pecified above f
dcff0 6f 72 20 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75  or .** OpenExclu
dd000 73 69 76 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  sive()..*/.stati
dd010 63 20 69 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a  c int unixOpen(.
dd020 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
dd030 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Vfs,           /
dd040 2a 20 54 68 65 20 56 46 53 20 66 6f 72 20 77 68  * The VFS for wh
dd050 69 63 68 20 74 68 69 73 20 69 73 20 74 68 65 20  ich this is the 
dd060 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a  xOpen method */.
dd070 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
dd080 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ath,           /
dd090 2a 20 50 61 74 68 6e 61 6d 65 20 6f 66 20 66 69  * Pathname of fi
dd0a0 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20  le to be opened 
dd0b0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
dd0c0 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20  e *pFile,       
dd0d0 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
dd0e0 73 63 72 69 70 74 6f 72 20 74 6f 20 62 65 20 66  scriptor to be f
dd0f0 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e  illed in */.  in
dd100 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
dd110 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
dd120 70 75 74 20 66 6c 61 67 73 20 74 6f 20 63 6f 6e  put flags to con
dd130 74 72 6f 6c 20 74 68 65 20 6f 70 65 6e 69 6e 67  trol the opening
dd140 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46   */.  int *pOutF
dd150 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20  lags            
dd160 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 66 6c 61     /* Output fla
dd170 67 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 53  gs returned to S
dd180 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 29 7b  QLite core */.){
dd190 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b 20  .  int fd = -1; 
dd1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd1b0 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72     /* File descr
dd1c0 69 70 74 6f 72 20 72 65 74 75 72 6e 65 64 20 62  iptor returned b
dd1d0 79 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  y open() */.  in
dd1e0 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 20 20 20  t dirfd = -1;   
dd1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
dd200 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64  Directory file d
dd210 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69  escriptor */.  i
dd220 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 20 30  nt openFlags = 0
dd230 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
dd240 20 46 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74   Flags to pass t
dd250 6f 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  o open() */.  in
dd260 74 20 65 54 79 70 65 20 3d 20 66 6c 61 67 73 26  t eType = flags&
dd270 30 78 46 46 46 46 46 46 30 30 3b 20 20 2f 2a 20  0xFFFFFF00;  /* 
dd280 54 79 70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Type of file to 
dd290 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  open */.  int no
dd2a0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Lock;           
dd2b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
dd2c0 20 74 6f 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67   to omit locking
dd2d0 20 70 72 69 6d 69 74 69 76 65 73 20 2a 2f 0a 20   primitives */. 
dd2e0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
dd2f0 5f 4f 4b 3b 0a 0a 20 20 69 6e 74 20 69 73 45 78  _OK;..  int isEx
dd300 63 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67  clusive  = (flag
dd310 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
dd320 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e  EXCLUSIVE);.  in
dd330 74 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d  t isDelete     =
dd340 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
dd350 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
dd360 4f 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72  OSE);.  int isCr
dd370 65 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67  eate     = (flag
dd380 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
dd390 43 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69  CREATE);.  int i
dd3a0 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66  sReadonly   = (f
dd3b0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
dd3c0 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20  EN_READONLY);.  
dd3d0 69 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 20  int isReadWrite 
dd3e0 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49   = (flags & SQLI
dd3f0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
dd400 45 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65  E);..  /* If cre
dd410 61 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f  ating a master o
dd420 72 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72  r main-file jour
dd430 6e 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69  nal, this functi
dd440 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a  on will open.  *
dd450 2a 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70  * a file-descrip
dd460 74 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63  tor on the direc
dd470 74 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69  tory too. The fi
dd480 72 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e  rst time unixSyn
dd490 63 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c  c().  ** is call
dd4a0 65 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  ed the directory
dd4b0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
dd4c0 20 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29   will be fsync()
dd4d0 65 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e  ed and close()d.
dd4e0 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70  .  */.  int isOp
dd4f0 65 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69  enDirectory = (i
dd500 73 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 20  sCreate && .    
dd510 20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45    (eType==SQLITE
dd520 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55  _OPEN_MASTER_JOU
dd530 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53  RNAL || eType==S
dd540 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
dd550 4a 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20  JOURNAL).  );.. 
dd560 20 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20   /* If argument 
dd570 7a 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20  zPath is a NULL 
dd580 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75  pointer, this fu
dd590 6e 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72  nction is requir
dd5a0 65 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20  ed to open.  ** 
dd5b0 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
dd5c0 2e 20 55 73 65 20 74 68 69 73 20 62 75 66 66 65  . Use this buffe
dd5d0 72 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66  r to store the f
dd5e0 69 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a  ile name in..  *
dd5f0 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d  /.  char zTmpnam
dd600 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31  e[MAX_PATHNAME+1
dd610 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ];.  const char 
dd620 2a 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a  *zName = zPath;.
dd630 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20  .  /* Check the 
dd640 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d  following statem
dd650 65 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a  ents are true: .
dd660 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20    **.  **   (a) 
dd670 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74  Exactly one of t
dd680 68 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64  he READWRITE and
dd690 20 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20   READONLY flags 
dd6a0 6d 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64  must be set, and
dd6b0 20 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20   .  **   (b) if 
dd6c0 43 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74  CREATE is set, t
dd6d0 68 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75  hen READWRITE mu
dd6e0 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20  st also be set, 
dd6f0 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69  and.  **   (c) i
dd700 66 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73  f EXCLUSIVE is s
dd710 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20  et, then CREATE 
dd720 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74  must also be set
dd730 2e 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20  ..  **   (d) if 
dd740 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73  DELETEONCLOSE is
dd750 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54   set, then CREAT
dd760 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73  E must also be s
dd770 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  et..  */.  asser
dd780 74 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30  t((isReadonly==0
dd790 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d   || isReadWrite=
dd7a0 3d 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72  =0) && (isReadWr
dd7b0 69 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c  ite || isReadonl
dd7c0 79 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73  y));.  assert(is
dd7d0 43 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52  Create==0 || isR
dd7e0 65 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73  eadWrite);.  ass
dd7f0 65 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d  ert(isExclusive=
dd800 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b  =0 || isCreate);
dd810 0a 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65  .  assert(isDele
dd820 74 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74  te==0 || isCreat
dd830 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61  e);..  /* The ma
dd840 69 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72  in DB, main jour
dd850 6e 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20  nal, and master 
dd860 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65  journal are neve
dd870 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  r automatically.
dd880 20 20 2a 2a 20 64 65 6c 65 74 65 64 0a 20 20 2a    ** deleted.  *
dd890 2f 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70  /.  assert( eTyp
dd8a0 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e!=SQLITE_OPEN_M
dd8b0 41 49 4e 5f 44 42 20 7c 7c 20 21 69 73 44 65 6c  AIN_DB || !isDel
dd8c0 65 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ete );.  assert(
dd8d0 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f   eType!=SQLITE_O
dd8e0 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
dd8f0 20 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b   || !isDelete );
dd900 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65  .  assert( eType
dd910 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  !=SQLITE_OPEN_MA
dd920 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20  STER_JOURNAL || 
dd930 21 69 73 44 65 6c 65 74 65 20 29 3b 0a 0a 20 20  !isDelete );..  
dd940 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74  /* Assert that t
dd950 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 20 68  he upper layer h
dd960 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 74 68  as set one of th
dd970 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20 66 6c  e "file-type" fl
dd980 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ags. */.  assert
dd990 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  ( eType==SQLITE_
dd9a0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 20 20  OPEN_MAIN_DB    
dd9b0 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
dd9c0 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20  TE_OPEN_TEMP_DB 
dd9d0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
dd9e0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
dd9f0 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54  IN_JOURNAL || eT
dda00 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
dda10 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 0a 20  _TEMP_JOURNAL . 
dda20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d        || eType==
dda30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a  SQLITE_OPEN_SUBJ
dda40 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 79 70  OURNAL   || eTyp
dda50 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e==SQLITE_OPEN_M
dda60 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 0a 20  ASTER_JOURNAL . 
dda70 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d        || eType==
dda80 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e  SQLITE_OPEN_TRAN
dda90 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a 0a 20  SIENT_DB.  );.. 
ddaa0 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30   memset(pFile, 0
ddab0 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c  , sizeof(unixFil
ddac0 65 29 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4e 61  e));..  if( !zNa
ddad0 6d 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  me ){.    assert
ddae0 28 69 73 44 65 6c 65 74 65 20 26 26 20 21 69 73  (isDelete && !is
ddaf0 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 29 3b 0a  OpenDirectory);.
ddb00 20 20 20 20 72 63 20 3d 20 67 65 74 54 65 6d 70      rc = getTemp
ddb10 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 4e 41 4d  name(MAX_PATHNAM
ddb20 45 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a  E+1, zTmpname);.
ddb30 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
ddb40 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
ddb50 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
ddb60 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70      zName = zTmp
ddb70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  name;.  }..  if(
ddb80 20 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f   isReadonly )  o
ddb90 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44  penFlags |= O_RD
ddba0 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65  ONLY;.  if( isRe
ddbb0 61 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c  adWrite ) openFl
ddbc0 61 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20  ags |= O_RDWR;. 
ddbd0 20 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20   if( isCreate ) 
ddbe0 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20     openFlags |= 
ddbf0 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69  O_CREAT;.  if( i
ddc00 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65  sExclusive ) ope
ddc10 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43  nFlags |= (O_EXC
ddc20 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20  L|O_NOFOLLOW);. 
ddc30 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f   openFlags |= (O
ddc40 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e  _LARGEFILE|O_BIN
ddc50 41 52 59 29 3b 0a 0a 20 20 66 64 20 3d 20 6f 70  ARY);..  fd = op
ddc60 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c  en(zName, openFl
ddc70 61 67 73 2c 20 69 73 44 65 6c 65 74 65 3f 30 36  ags, isDelete?06
ddc80 30 30 3a 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  00:SQLITE_DEFAUL
ddc90 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f  T_FILE_PERMISSIO
ddca0 4e 53 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28  NS);.  OSTRACE4(
ddcb0 22 4f 50 45 4e 58 20 20 20 25 2d 33 64 20 25 73  "OPENX   %-3d %s
ddcc0 20 30 25 6f 5c 6e 22 2c 20 66 64 2c 20 7a 4e 61   0%o\n", fd, zNa
ddcd0 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 29 3b 0a  me, openFlags);.
ddce0 20 20 69 66 28 20 66 64 3c 30 20 26 26 20 65 72    if( fd<0 && er
ddcf0 72 6e 6f 21 3d 45 49 53 44 49 52 20 26 26 20 69  rno!=EISDIR && i
ddd00 73 52 65 61 64 57 72 69 74 65 20 26 26 20 21 69  sReadWrite && !i
ddd10 73 45 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20  sExclusive ){.  
ddd20 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f    /* Failed to o
ddd30 70 65 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72  pen the file for
ddd40 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65   read/write acce
ddd50 73 73 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c  ss. Try read-onl
ddd60 79 2e 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20  y. */.    flags 
ddd70 26 3d 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e  &= ~(SQLITE_OPEN
ddd80 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
ddd90 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a  E_OPEN_CREATE);.
ddda0 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c      flags |= SQL
dddb0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
dddc0 59 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e  Y;.    return un
dddd0 69 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61  ixOpen(pVfs, zPa
ddde0 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73  th, pFile, flags
dddf0 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20  , pOutFlags);.  
dde00 7d 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a  }.  if( fd<0 ){.
dde10 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
dde20 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a  E_CANTOPEN;.  }.
dde30 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 20 29    if( isDelete )
dde40 7b 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  {.#if OS_VXWORKS
dde50 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61  .    zPath = zNa
dde60 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e  me;.#else.    un
dde70 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e  link(zName);.#en
dde80 64 69 66 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49  dif.  }.#if SQLI
dde90 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
ddea0 47 5f 53 54 59 4c 45 0a 20 20 65 6c 73 65 7b 0a  G_STYLE.  else{.
ddeb0 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
ddec0 70 46 69 6c 65 29 2d 3e 6f 70 65 6e 46 6c 61 67  pFile)->openFlag
dded0 73 20 3d 20 6f 70 65 6e 46 6c 61 67 73 3b 0a 20  s = openFlags;. 
ddee0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
ddef0 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20  pOutFlags ){.   
ddf00 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c   *pOutFlags = fl
ddf10 61 67 73 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  ags;.  }..#ifnde
ddf20 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 28  f NDEBUG.  if( (
ddf30 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
ddf40 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20  PEN_MAIN_DB)!=0 
ddf50 29 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c  ){.    ((unixFil
ddf60 65 2a 29 70 46 69 6c 65 29 2d 3e 69 73 4c 6f 63  e*)pFile)->isLoc
ddf70 6b 61 62 6c 65 20 3d 20 31 3b 0a 20 20 7d 0a 23  kable = 1;.  }.#
ddf80 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28  endif..  assert(
ddf90 20 66 64 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20   fd>=0 );.  if( 
ddfa0 69 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20  isOpenDirectory 
ddfb0 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e  ){.    rc = open
ddfc0 44 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c  Directory(zPath,
ddfd0 20 26 64 69 72 66 64 29 3b 0a 20 20 20 20 69 66   &dirfd);.    if
ddfe0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
ddff0 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66  ){.      close(f
de000 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20  d); /* silently 
de010 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c  leak if fail, al
de020 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a  ready in error *
de030 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  /.      return r
de040 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69  c;.    }.  }..#i
de050 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a  fdef FD_CLOEXEC.
de060 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45    fcntl(fd, F_SE
de070 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46  TFD, fcntl(fd, F
de080 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f  _GETFD, 0) | FD_
de090 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66  CLOEXEC);.#endif
de0a0 0a 0a 20 20 6e 6f 4c 6f 63 6b 20 3d 20 65 54 79  ..  noLock = eTy
de0b0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe!=SQLITE_OPEN_
de0c0 4d 41 49 4e 5f 44 42 3b 0a 0a 23 69 66 20 53 51  MAIN_DB;..#if SQ
de0d0 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58  LITE_PREFER_PROX
de0e0 59 5f 4c 4f 43 4b 49 4e 47 0a 20 20 69 66 28 20  Y_LOCKING.  if( 
de0f0 7a 50 61 74 68 21 3d 4e 55 4c 4c 20 26 26 20 21  zPath!=NULL && !
de100 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 63 68  noLock ){.    ch
de110 61 72 20 2a 65 6e 76 66 6f 72 63 65 20 3d 20 67  ar *envforce = g
de120 65 74 65 6e 76 28 22 53 51 4c 49 54 45 5f 46 4f  etenv("SQLITE_FO
de130 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  RCE_PROXY_LOCKIN
de140 47 22 29 3b 0a 20 20 20 20 69 6e 74 20 75 73 65  G");.    int use
de150 50 72 6f 78 79 20 3d 20 30 3b 0a 0a 20 20 20 20  Proxy = 0;..    
de160 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f  /* SQLITE_FORCE_
de170 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 3d 3d 31  PROXY_LOCKING==1
de180 20 6d 65 61 6e 73 20 66 6f 72 63 65 20 61 6c 77   means force alw
de190 61 79 73 20 75 73 65 20 70 72 6f 78 79 2c 20 0a  ays use proxy, .
de1a0 20 20 20 20 2a 2a 20 30 20 6d 65 61 6e 73 20 6e      ** 0 means n
de1b0 65 76 65 72 20 75 73 65 20 70 72 6f 78 79 2c 20  ever use proxy, 
de1c0 4e 55 4c 4c 20 6d 65 61 6e 73 20 75 73 65 20 70  NULL means use p
de1d0 72 6f 78 79 20 66 6f 72 20 6e 6f 6e 2d 6c 6f 63  roxy for non-loc
de1e0 61 6c 20 66 69 6c 65 73 20 6f 6e 6c 79 0a 20 20  al files only.  
de1f0 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 6e 76    */.    if( env
de200 66 6f 72 63 65 21 3d 4e 55 4c 4c 20 29 7b 0a 20  force!=NULL ){. 
de210 20 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20       useProxy = 
de220 61 74 6f 69 28 65 6e 76 66 6f 72 63 65 29 3e 30  atoi(envforce)>0
de230 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
de240 20 20 20 73 74 72 75 63 74 20 73 74 61 74 66 73     struct statfs
de250 20 66 73 49 6e 66 6f 3b 0a 0a 20 20 20 20 20 20   fsInfo;..      
de260 69 66 28 20 73 74 61 74 66 73 28 7a 50 61 74 68  if( statfs(zPath
de270 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d 20 2d 31  , &fsInfo) == -1
de280 20 29 7b 0a 09 09 09 09 28 28 75 6e 69 78 46 69   ){.....((unixFi
de290 6c 65 2a 29 70 46 69 6c 65 29 2d 3e 6c 61 73 74  le*)pFile)->last
de2a0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
de2b0 20 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64         if( dirfd
de2c0 3e 3d 30 20 29 20 63 6c 6f 73 65 28 64 69 72 66  >=0 ) close(dirf
de2d0 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20  d); /* silently 
de2e0 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e  leak if fail, in
de2f0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20   error */.      
de300 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20    close(fd); /* 
de310 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 66  silently leak if
de320 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72 20   fail, in error 
de330 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
de340 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41  n SQLITE_IOERR_A
de350 43 43 45 53 53 3b 0a 20 20 20 20 20 20 7d 0a 20  CCESS;.      }. 
de360 20 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20       useProxy = 
de370 21 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73  !(fsInfo.f_flags
de380 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20  &MNT_LOCAL);.   
de390 20 7d 0a 20 20 20 20 69 66 28 20 75 73 65 50 72   }.    if( usePr
de3a0 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20  oxy ){.      rc 
de3b0 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65  = fillInUnixFile
de3c0 28 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64  (pVfs, fd, dirfd
de3d0 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20  , pFile, zPath, 
de3e0 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65  noLock, isDelete
de3f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
de400 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
de410 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
de420 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c  TransformUnixFil
de430 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69  e((unixFile*)pFi
de440 6c 65 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20  le, ":auto:");. 
de450 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
de460 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
de470 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 72 65  }.#endif.  .  re
de480 74 75 72 6e 20 66 69 6c 6c 49 6e 55 6e 69 78 46  turn fillInUnixF
de490 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64 69  ile(pVfs, fd, di
de4a0 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74  rfd, pFile, zPat
de4b0 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c  h, noLock, isDel
de4c0 65 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ete);.}../*.** D
de4d0 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20 61  elete the file a
de4e0 74 20 7a 50 61 74 68 2e 20 49 66 20 74 68 65 20  t zPath. If the 
de4f0 64 69 72 53 79 6e 63 20 61 72 67 75 6d 65 6e 74  dirSync argument
de500 20 69 73 20 74 72 75 65 2c 20 66 73 79 6e 63 28   is true, fsync(
de510 29 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f  ).** the directo
de520 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e  ry after deletin
de530 67 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  g the file..*/.s
de540 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65  tatic int unixDe
de550 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  lete(.  sqlite3_
de560 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20  vfs *NotUsed,   
de570 20 20 2f 2a 20 56 46 53 20 63 6f 6e 74 61 69 6e    /* VFS contain
de580 69 6e 67 20 74 68 69 73 20 61 73 20 74 68 65 20  ing this as the 
de590 78 44 65 6c 65 74 65 20 6d 65 74 68 6f 64 20 2a  xDelete method *
de5a0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
de5b0 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 2f 2a  zPath,        /*
de5c0 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f   Name of file to
de5d0 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20   be deleted */. 
de5e0 20 69 6e 74 20 64 69 72 53 79 6e 63 20 20 20 20   int dirSync    
de5f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
de600 20 74 72 75 65 2c 20 66 73 79 6e 63 28 29 20 64   true, fsync() d
de610 69 72 65 63 74 6f 72 79 20 61 66 74 65 72 20 64  irectory after d
de620 65 6c 65 74 69 6e 67 20 66 69 6c 65 20 2a 2f 0a  eleting file */.
de630 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
de640 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45  LITE_OK;.  UNUSE
de650 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
de660 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  sed);.  Simulate
de670 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53  IOError(return S
de680 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
de690 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50  TE);.  unlink(zP
de6a0 61 74 68 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ath);.#ifndef SQ
de6b0 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52  LITE_DISABLE_DIR
de6c0 53 59 4e 43 0a 20 20 69 66 28 20 64 69 72 53 79  SYNC.  if( dirSy
de6d0 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64  nc ){.    int fd
de6e0 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44  ;.    rc = openD
de6f0 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20  irectory(zPath, 
de700 26 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63  &fd);.    if( rc
de710 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23  ==SQLITE_OK ){.#
de720 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
de730 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64      if( fsync(fd
de740 29 3d 3d 2d 31 20 29 0a 23 65 6c 73 65 0a 20 20  )==-1 ).#else.  
de750 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64      if( fsync(fd
de760 29 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ) ).#endif.     
de770 20 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   {.        rc = 
de780 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52  SQLITE_IOERR_DIR
de790 5f 46 53 59 4e 43 3b 0a 20 20 20 20 20 20 7d 0a  _FSYNC;.      }.
de7a0 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28        if( close(
de7b0 66 64 29 26 26 21 72 63 20 29 7b 0a 20 20 20 20  fd)&&!rc ){.    
de7c0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
de7d0 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b  IOERR_DIR_CLOSE;
de7e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
de7f0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
de800 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
de810 54 65 73 74 20 74 68 65 20 65 78 69 73 74 61 6e  Test the existan
de820 63 65 20 6f 66 20 6f 72 20 61 63 63 65 73 73 20  ce of or access 
de830 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 66  permissions of f
de840 69 6c 65 20 7a 50 61 74 68 2e 20 54 68 65 0a 2a  ile zPath. The.*
de850 2a 20 74 65 73 74 20 70 65 72 66 6f 72 6d 65 64  * test performed
de860 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20   depends on the 
de870 76 61 6c 75 65 20 6f 66 20 66 6c 61 67 73 3a 0a  value of flags:.
de880 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  **.**     SQLITE
de890 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 20  _ACCESS_EXISTS: 
de8a0 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
de8b0 66 69 6c 65 20 65 78 69 73 74 73 0a 2a 2a 20 20  file exists.**  
de8c0 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53     SQLITE_ACCESS
de8d0 5f 52 45 41 44 57 52 49 54 45 3a 20 52 65 74 75  _READWRITE: Retu
de8e0 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65  rn 1 if the file
de8f0 20 69 73 20 72 65 61 64 20 61 6e 64 20 77 72 69   is read and wri
de900 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 53 51  table..**     SQ
de910 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
de920 4f 4e 4c 59 3a 20 52 65 74 75 72 6e 20 31 20 69  ONLY: Return 1 i
de930 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65  f the file is re
de940 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74  adable..**.** Ot
de950 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 30  herwise return 0
de960 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
de970 75 6e 69 78 41 63 63 65 73 73 28 0a 20 20 73 71  unixAccess(.  sq
de980 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73  lite3_vfs *NotUs
de990 65 64 2c 20 20 20 2f 2a 20 54 68 65 20 56 46 53  ed,   /* The VFS
de9a0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73   containing this
de9b0 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20   xAccess method 
de9c0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
de9d0 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 2f 2a 20  *zPath,      /* 
de9e0 50 61 74 68 20 6f 66 20 74 68 65 20 66 69 6c 65  Path of the file
de9f0 20 74 6f 20 65 78 61 6d 69 6e 65 20 2a 2f 0a 20   to examine */. 
dea00 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20   int flags,     
dea10 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74           /* What
dea20 20 64 6f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c   do we want to l
dea30 65 61 72 6e 20 61 62 6f 75 74 20 74 68 65 20 7a  earn about the z
dea40 50 61 74 68 20 66 69 6c 65 3f 20 2a 2f 0a 20 20  Path file? */.  
dea50 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20  int *pResOut    
dea60 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
dea70 20 72 65 73 75 6c 74 20 62 6f 6f 6c 65 61 6e 20   result boolean 
dea80 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
dea90 20 61 6d 6f 64 65 20 3d 20 30 3b 0a 20 20 55 4e   amode = 0;.  UN
deaa0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
deab0 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c  otUsed);.  Simul
deac0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
dead0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
deae0 41 43 43 45 53 53 3b 20 29 3b 0a 20 20 73 77 69  ACCESS; );.  swi
deaf0 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20  tch( flags ){.  
deb00 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
deb10 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20  CESS_EXISTS:.   
deb20 20 20 20 61 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b     amode = F_OK;
deb30 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
deb40 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
deb50 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a  CESS_READWRITE:.
deb60 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 57 5f        amode = W_
deb70 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62  OK|R_OK;.      b
deb80 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
deb90 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
deba0 44 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d  D:.      amode =
debb0 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65   R_OK;.      bre
debc0 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74  ak;..    default
debd0 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21  :.      assert(!
debe0 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61  "Invalid flags a
debf0 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20  rgument");.  }. 
dec00 20 2a 70 52 65 73 4f 75 74 20 3d 20 28 61 63 63   *pResOut = (acc
dec10 65 73 73 28 7a 50 61 74 68 2c 20 61 6d 6f 64 65  ess(zPath, amode
dec20 29 3d 3d 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  )==0);.  return 
dec30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
dec40 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61  *.** Turn a rela
dec50 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e  tive pathname in
dec60 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61  to a full pathna
dec70 6d 65 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65  me. The relative
dec80 20 70 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72   path.** is stor
dec90 65 64 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d  ed as a nul-term
deca0 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e  inated string in
decb0 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e   the buffer poin
decc0 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61  ted to by.** zPa
decd0 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20  th. .**.** zOut 
dece0 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66  points to a buff
decf0 65 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73  er of at least s
ded00 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74  qlite3_vfs.mxPat
ded10 68 6e 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20  hname bytes .** 
ded20 28 69 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d  (in this case, M
ded30 41 58 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65  AX_PATHNAME byte
ded40 73 29 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74  s). The full-pat
ded50 68 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a  h is written to.
ded60 2a 2a 20 74 68 69 73 20 62 75 66 66 65 72 20 62  ** this buffer b
ded70 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
ded80 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
ded90 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  nixFullPathname(
deda0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
dedb0 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
dedc0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
dedd0 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63  fs object */.  c
dede0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
dedf0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
dee00 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76  Possibly relativ
dee10 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a  e input path */.
dee20 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20    int nOut,     
dee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dee40 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75  /* Size of outpu
dee50 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65  t buffer in byte
dee60 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75  s */.  char *zOu
dee70 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
dee80 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62       /* Output b
dee90 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f  uffer */.){..  /
deea0 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69  * It's odd to si
deeb0 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72  mulate an io-err
deec0 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61  or here, but rea
deed0 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74  lly this is just
deee0 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  .  ** using the 
deef0 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74  io-error infrast
def00 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20  ructure to test 
def10 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64  that SQLite hand
def20 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75  les this.  ** fu
def30 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20  nction failing. 
def40 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
def50 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72  uld fail if, for
def60 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20   example, the.  
def70 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69  ** current worki
def80 6e 67 20 64 69 72 65 63 74 6f 72 79 20 68 61 73  ng directory has
def90 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a   been unlinked..
defa0 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49    */.  SimulateI
defb0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
defc0 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a  QLITE_ERROR );..
defd0 20 20 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e    assert( pVfs->
defe0 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f  mxPathname==MAX_
deff0 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e  PATHNAME );.  UN
df000 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
df010 56 66 73 29 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f  Vfs);..  zOut[nO
df020 75 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  ut-1] = '\0';.  
df030 69 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f  if( zPath[0]=='/
df040 27 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ' ){.    sqlite3
df050 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20  _snprintf(nOut, 
df060 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74  zOut, "%s", zPat
df070 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  h);.  }else{.   
df080 20 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69   int nCwd;.    i
df090 66 28 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20  f( getcwd(zOut, 
df0a0 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20  nOut-1)==0 ){.  
df0b0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
df0c0 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20  E_CANTOPEN;.    
df0d0 7d 0a 20 20 20 20 6e 43 77 64 20 3d 20 28 69 6e  }.    nCwd = (in
df0e0 74 29 73 74 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a  t)strlen(zOut);.
df0f0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
df100 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 77 64 2c 20  intf(nOut-nCwd, 
df110 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25  &zOut[nCwd], "/%
df120 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d 0a  s", zPath);.  }.
df130 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
df140 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20  OK;.}...#ifndef 
df150 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
df160 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a  _EXTENSION./*.**
df170 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20   Interfaces for 
df180 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64  opening a shared
df190 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e   library, findin
df1a0 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a  g entry points.*
df1b0 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61  * within the sha
df1c0 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64  red library, and
df1d0 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61   closing the sha
df1e0 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  red library..*/.
df1f0 23 69 6e 63 6c 75 64 65 20 3c 64 6c 66 63 6e 2e  #include <dlfcn.
df200 68 3e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  h>.static void *
df210 75 6e 69 78 44 6c 4f 70 65 6e 28 73 71 6c 69 74  unixDlOpen(sqlit
df220 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
df230 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
df240 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 4e 55 53 45  lename){.  UNUSE
df250 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
df260 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 64  sed);.  return d
df270 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c  lopen(zFilename,
df280 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44   RTLD_NOW | RTLD
df290 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a  _GLOBAL);.}../*.
df2a0 2a 2a 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20  ** SQLite calls 
df2b0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d  this function im
df2c0 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20  mediately after 
df2d0 61 20 63 61 6c 6c 20 74 6f 20 75 6e 69 78 44 6c  a call to unixDl
df2e0 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e 69 78  Sym() or.** unix
df2f0 44 6c 4f 70 65 6e 28 29 20 66 61 69 6c 73 20 28  DlOpen() fails (
df300 72 65 74 75 72 6e 73 20 61 20 6e 75 6c 6c 20 70  returns a null p
df310 6f 69 6e 74 65 72 29 2e 20 49 66 20 61 20 6d 6f  ointer). If a mo
df320 72 65 20 64 65 74 61 69 6c 65 64 20 65 72 72 6f  re detailed erro
df330 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20  r.** message is 
df340 61 76 61 69 6c 61 62 6c 65 2c 20 69 74 20 69 73  available, it is
df350 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 42 75 66   written to zBuf
df360 4f 75 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72  Out. If no error
df370 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 61   message.** is a
df380 76 61 69 6c 61 62 6c 65 2c 20 7a 42 75 66 4f 75  vailable, zBufOu
df390 74 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69  t is left unmodi
df3a0 66 69 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20  fied and SQLite 
df3b0 75 73 65 73 20 61 20 64 65 66 61 75 6c 74 0a 2a  uses a default.*
df3c0 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e  * error message.
df3d0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
df3e0 75 6e 69 78 44 6c 45 72 72 6f 72 28 73 71 6c 69  unixDlError(sqli
df3f0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
df400 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
df410 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 63 68   *zBufOut){.  ch
df420 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53  ar *zErr;.  UNUS
df430 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
df440 55 73 65 64 29 3b 0a 20 20 75 6e 69 78 45 6e 74  Used);.  unixEnt
df450 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 7a 45 72  erMutex();.  zEr
df460 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 3b 0a 20  r = dlerror();. 
df470 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20   if( zErr ){.   
df480 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
df490 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 2c  f(nBuf, zBufOut,
df4a0 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20   "%s", zErr);.  
df4b0 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  }.  unixLeaveMut
df4c0 65 78 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ex();.}.static v
df4d0 6f 69 64 20 28 2a 75 6e 69 78 44 6c 53 79 6d 28  oid (*unixDlSym(
df4e0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74  sqlite3_vfs *Not
df4f0 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 2c 20 63  Used, void *p, c
df500 6f 6e 73 74 20 63 68 61 72 2a 7a 53 79 6d 29 29  onst char*zSym))
df510 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 20 0a 20 20  (void){.  /* .  
df520 2a 2a 20 47 43 43 20 77 69 74 68 20 2d 70 65 64  ** GCC with -ped
df530 61 6e 74 69 63 2d 65 72 72 6f 72 73 20 73 61 79  antic-errors say
df540 73 20 74 68 61 74 20 43 39 30 20 64 6f 65 73 20  s that C90 does 
df550 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 76 6f 69 64  not allow a void
df560 2a 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 61 73  * to be.  ** cas
df570 74 20 69 6e 74 6f 20 61 20 70 6f 69 6e 74 65 72  t into a pointer
df580 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20   to a function. 
df590 20 41 6e 64 20 79 65 74 20 74 68 65 20 6c 69 62   And yet the lib
df5a0 72 61 72 79 20 64 6c 73 79 6d 28 29 20 72 6f 75  rary dlsym() rou
df5b0 74 69 6e 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e  tine.  ** return
df5c0 73 20 61 20 76 6f 69 64 2a 20 77 68 69 63 68 20  s a void* which 
df5d0 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e  is really a poin
df5e0 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
df5f0 6e 2e 20 20 53 6f 20 68 6f 77 20 64 6f 20 77 65  n.  So how do we
df600 0a 20 20 2a 2a 20 75 73 65 20 64 6c 73 79 6d 28  .  ** use dlsym(
df610 29 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63  ) with -pedantic
df620 2d 65 72 72 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20  -errors?.  **.  
df630 2a 2a 20 56 61 72 69 61 62 6c 65 20 78 20 62 65  ** Variable x be
df640 6c 6f 77 20 69 73 20 64 65 66 69 6e 65 64 20 74  low is defined t
df650 6f 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74  o be a pointer t
df660 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  o a function tak
df670 69 6e 67 0a 20 20 2a 2a 20 70 61 72 61 6d 65 74  ing.  ** paramet
df680 65 72 73 20 76 6f 69 64 2a 20 61 6e 64 20 63 6f  ers void* and co
df690 6e 73 74 20 63 68 61 72 2a 20 61 6e 64 20 72 65  nst char* and re
df6a0 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  turning a pointe
df6b0 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e  r to a function.
df6c0 0a 20 20 2a 2a 20 57 65 20 69 6e 69 74 69 61 6c  .  ** We initial
df6d0 69 7a 65 20 78 20 62 79 20 61 73 73 69 67 6e 69  ize x by assigni
df6e0 6e 67 20 69 74 20 61 20 70 6f 69 6e 74 65 72 20  ng it a pointer 
df6f0 74 6f 20 74 68 65 20 64 6c 73 79 6d 28 29 20 66  to the dlsym() f
df700 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54  unction..  ** (T
df710 68 61 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 72  hat assignment r
df720 65 71 75 69 72 65 73 20 61 20 63 61 73 74 2e 29  equires a cast.)
df730 20 20 54 68 65 6e 20 77 65 20 63 61 6c 6c 20 74    Then we call t
df740 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  he function that
df750 0a 20 20 2a 2a 20 78 20 70 6f 69 6e 74 73 20 74  .  ** x points t
df760 6f 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  o.  .  **.  ** T
df770 68 69 73 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 20  his work-around 
df780 69 73 20 75 6e 6c 69 6b 65 6c 79 20 74 6f 20 77  is unlikely to w
df790 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e  ork correctly on
df7a0 20 61 6e 79 20 73 79 73 74 65 6d 20 77 68 65 72   any system wher
df7b0 65 0a 20 20 2a 2a 20 79 6f 75 20 72 65 61 6c 6c  e.  ** you reall
df7c0 79 20 63 61 6e 6e 6f 74 20 63 61 73 74 20 61 20  y cannot cast a 
df7d0 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
df7e0 20 69 6e 74 6f 20 76 6f 69 64 2a 2e 20 20 42 75   into void*.  Bu
df7f0 74 20 74 68 65 6e 2c 20 6f 6e 20 74 68 65 0a 20  t then, on the. 
df800 20 2a 2a 20 6f 74 68 65 72 20 68 61 6e 64 2c 20   ** other hand, 
df810 64 6c 73 79 6d 28 29 20 77 69 6c 6c 20 6e 6f 74  dlsym() will not
df820 20 77 6f 72 6b 20 6f 6e 20 73 75 63 68 20 61 20   work on such a 
df830 73 79 73 74 65 6d 20 65 69 74 68 65 72 2c 20 73  system either, s
df840 6f 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 6e  o we have.  ** n
df850 6f 74 20 72 65 61 6c 6c 79 20 6c 6f 73 74 20 61  ot really lost a
df860 6e 79 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  nything..  */.  
df870 76 6f 69 64 20 28 2a 28 2a 78 29 28 76 6f 69 64  void (*(*x)(void
df880 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 28  *,const char*))(
df890 76 6f 69 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f  void);.  UNUSED_
df8a0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
df8b0 64 29 3b 0a 20 20 78 20 3d 20 28 76 6f 69 64 28  d);.  x = (void(
df8c0 2a 28 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  *(*)(void*,const
df8d0 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 29 64   char*))(void))d
df8e0 6c 73 79 6d 3b 0a 20 20 72 65 74 75 72 6e 20 28  lsym;.  return (
df8f0 2a 78 29 28 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a  *x)(p, zSym);.}.
df900 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78  static void unix
df910 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  DlClose(sqlite3_
df920 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f  vfs *NotUsed, vo
df930 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20  id *pHandle){.  
df940 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
df950 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 64 6c 63  (NotUsed);.  dlc
df960 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 3b 0a 7d  lose(pHandle);.}
df970 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c  .#else /* if SQL
df980 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
df990 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e  TENSION is defin
df9a0 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  ed: */.  #define
df9b0 20 75 6e 69 78 44 6c 4f 70 65 6e 20 20 30 0a 20   unixDlOpen  0. 
df9c0 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 45   #define unixDlE
df9d0 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65  rror 0.  #define
df9e0 20 75 6e 69 78 44 6c 53 79 6d 20 20 20 30 0a 20   unixDlSym   0. 
df9f0 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 43   #define unixDlC
dfa00 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 2f  lose 0.#endif../
dfa10 2a 0a 2a 2a 20 57 72 69 74 65 20 6e 42 75 66 20  *.** Write nBuf 
dfa20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 20  bytes of random 
dfa30 64 61 74 61 20 74 6f 20 74 68 65 20 73 75 70 70  data to the supp
dfa40 6c 69 65 64 20 62 75 66 66 65 72 20 7a 42 75 66  lied buffer zBuf
dfa50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
dfa60 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 28 73  unixRandomness(s
dfa70 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
dfa80 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63  sed, int nBuf, c
dfa90 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e  har *zBuf){.  UN
dfaa0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
dfab0 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65 72  otUsed);.  asser
dfac0 74 28 28 73 69 7a 65 5f 74 29 6e 42 75 66 3e 3d  t((size_t)nBuf>=
dfad0 28 73 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b  (sizeof(time_t)+
dfae0 73 69 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0a 0a  sizeof(int)));..
dfaf0 20 20 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20    /* We have to 
dfb00 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20  initialize zBuf 
dfb10 74 6f 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72  to prevent valgr
dfb20 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69  ind from reporti
dfb30 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20  ng.  ** errors. 
dfb40 20 54 68 65 20 72 65 70 6f 72 74 73 20 69 73 73   The reports iss
dfb50 75 65 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20  ued by valgrind 
dfb60 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20  are incorrect - 
dfb70 77 65 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72  we would.  ** pr
dfb80 65 66 65 72 20 74 68 61 74 20 74 68 65 20 72 61  efer that the ra
dfb90 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72  ndomness be incr
dfba0 65 61 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20  eased by making 
dfbb0 75 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  use of the.  ** 
dfbc0 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70  uninitialized sp
dfbd0 61 63 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75  ace in zBuf - bu
dfbe0 74 20 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72  t valgrind error
dfbf0 73 20 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a  s tend to worry.
dfc00 20 20 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e    ** some users.
dfc10 20 20 52 61 74 68 65 72 20 74 68 61 6e 20 61 72    Rather than ar
dfc20 67 75 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61  gue, it seems ea
dfc30 73 69 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69  sier just to ini
dfc40 74 69 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65  tialize.  ** the
dfc50 20 77 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64   whole array and
dfc60 20 73 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e   silence valgrin
dfc70 64 2c 20 65 76 65 6e 20 69 66 20 74 68 61 74 20  d, even if that 
dfc80 6d 65 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f  means less rando
dfc90 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68  mness.  ** in th
dfca0 65 20 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20  e random seed.. 
dfcb0 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65   **.  ** When te
dfcc0 73 74 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a  sting, initializ
dfcd0 69 6e 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65  ing zBuf[] to ze
dfce0 72 6f 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e  ro is all we do.
dfcf0 20 20 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a    That means.  *
dfd00 2a 20 74 68 61 74 20 77 65 20 61 6c 77 61 79 73  * that we always
dfd10 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 72 61   use the same ra
dfd20 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75  ndom number sequ
dfd30 65 6e 63 65 2e 20 20 54 68 69 73 20 6d 61 6b 65  ence.  This make
dfd40 73 20 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73  s the.  ** tests
dfd50 20 72 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a   repeatable..  *
dfd60 2f 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c  /.  memset(zBuf,
dfd70 20 30 2c 20 6e 42 75 66 29 3b 0a 23 69 66 20 21   0, nBuf);.#if !
dfd80 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
dfd90 45 53 54 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74  EST).  {.    int
dfda0 20 70 69 64 2c 20 66 64 3b 0a 20 20 20 20 66 64   pid, fd;.    fd
dfdb0 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72   = open("/dev/ur
dfdc0 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59  andom", O_RDONLY
dfdd0 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20  );.    if( fd<0 
dfde0 29 7b 0a 20 20 20 20 20 20 74 69 6d 65 5f 74 20  ){.      time_t 
dfdf0 74 3b 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74  t;.      time(&t
dfe00 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
dfe10 7a 42 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66  zBuf, &t, sizeof
dfe20 28 74 29 29 3b 0a 20 20 20 20 20 20 70 69 64 20  (t));.      pid 
dfe30 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20 20 20  = getpid();.    
dfe40 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 73    memcpy(&zBuf[s
dfe50 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c  izeof(t)], &pid,
dfe60 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20   sizeof(pid));. 
dfe70 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a       assert( siz
dfe80 65 6f 66 28 74 29 2b 73 69 7a 65 6f 66 28 70 69  eof(t)+sizeof(pi
dfe90 64 29 3c 3d 28 73 69 7a 65 5f 74 29 6e 42 75 66  d)<=(size_t)nBuf
dfea0 20 29 3b 0a 20 20 20 20 20 20 6e 42 75 66 20 3d   );.      nBuf =
dfeb0 20 73 69 7a 65 6f 66 28 74 29 20 2b 20 73 69 7a   sizeof(t) + siz
dfec0 65 6f 66 28 70 69 64 29 3b 0a 20 20 20 20 7d 65  eof(pid);.    }e
dfed0 6c 73 65 7b 0a 20 20 20 20 20 20 6e 42 75 66 20  lse{.      nBuf 
dfee0 3d 20 72 65 61 64 28 66 64 2c 20 7a 42 75 66 2c  = read(fd, zBuf,
dfef0 20 6e 42 75 66 29 3b 0a 20 20 20 20 20 20 63 6c   nBuf);.      cl
dff00 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20  ose(fd);.    }. 
dff10 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
dff20 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a  rn nBuf;.}.../*.
dff30 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c  ** Sleep for a l
dff40 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65  ittle while.  Re
dff50 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  turn the amount 
dff60 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a  of time slept..*
dff70 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
dff80 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
dff90 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20  microseconds we 
dffa0 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a  want to sleep..*
dffb0 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * The return val
dffc0 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ue is the number
dffd0 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73   of microseconds
dffe0 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c   of sleep actual
dfff0 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20  ly.** requested 
e0000 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79  from the underly
e0010 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79  ing operating sy
e0020 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77  stem, a number w
e0030 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65  hich.** might be
e0040 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
e0050 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72   equal to the ar
e0060 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20  gument, but not 
e0070 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65  less.** than the
e0080 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
e0090 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 6c 65  atic int unixSle
e00a0 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ep(sqlite3_vfs *
e00b0 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6d 69 63  NotUsed, int mic
e00c0 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20  roseconds){.#if 
e00d0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72  OS_VXWORKS.  str
e00e0 75 63 74 20 74 69 6d 65 73 70 65 63 20 73 70 3b  uct timespec sp;
e00f0 0a 0a 20 20 73 70 2e 74 76 5f 73 65 63 20 3d 20  ..  sp.tv_sec = 
e0100 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 2f 20 31  microseconds / 1
e0110 30 30 30 30 30 30 3b 0a 20 20 73 70 2e 74 76 5f  000000;.  sp.tv_
e0120 6e 73 65 63 20 3d 20 28 6d 69 63 72 6f 73 65 63  nsec = (microsec
e0130 6f 6e 64 73 20 25 20 31 30 30 30 30 30 30 29 20  onds % 1000000) 
e0140 2a 20 31 30 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c  * 1000;.  nanosl
e0150 65 65 70 28 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a  eep(&sp, NULL);.
e0160 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
e0170 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
e0180 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e  eturn microsecon
e0190 64 73 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65  ds;.#elif define
e01a0 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20 26  d(HAVE_USLEEP) &
e01b0 26 20 48 41 56 45 5f 55 53 4c 45 45 50 0a 20 20  & HAVE_USLEEP.  
e01c0 75 73 6c 65 65 70 28 6d 69 63 72 6f 73 65 63 6f  usleep(microseco
e01d0 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  nds);.  UNUSED_P
e01e0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
e01f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72  );.  return micr
e0200 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a  oseconds;.#else.
e0210 20 20 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20    int seconds = 
e0220 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39  (microseconds+99
e0230 39 39 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20  9999)/1000000;. 
e0240 20 73 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b   sleep(seconds);
e0250 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e0260 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
e0270 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 73 2a 31  return seconds*1
e0280 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 7d  000000;.#endif.}
e0290 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
e02a0 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20  owing variable, 
e02b0 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d  if set to a non-
e02c0 7a 65 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69  zero value, is i
e02d0 6e 74 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a  nterpreted as.**
e02e0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
e02f0 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37  econds since 197
e0300 30 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f  0 and is used to
e0310 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20   set the result 
e0320 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43  of.** sqlite3OsC
e0330 75 72 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72  urrentTime() dur
e0340 69 6e 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a  ing testing..*/.
e0350 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
e0360 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
e0370 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  t sqlite3_curren
e0380 74 5f 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20  t_time = 0;  /* 
e0390 46 61 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65  Fake system time
e03a0 20 69 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63   in seconds sinc
e03b0 65 20 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69  e 1970. */.#endi
e03c0 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  f../*.** Find th
e03d0 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28  e current time (
e03e0 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f  in Universal Coo
e03f0 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20  rdinated Time). 
e0400 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75   Write the.** cu
e0410 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64  rrent time and d
e0420 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20  ate as a Julian 
e0430 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20  Day number into 
e0440 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65  *prNow and.** re
e0450 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20  turn 0.  Return 
e0460 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e  1 if the time an
e0470 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65  d date cannot be
e0480 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69   found..*/.stati
e0490 63 20 69 6e 74 20 75 6e 69 78 43 75 72 72 65 6e  c int unixCurren
e04a0 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  tTime(sqlite3_vf
e04b0 73 20 2a 4e 6f 74 55 73 65 64 2c 20 64 6f 75 62  s *NotUsed, doub
e04c0 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 20  le *prNow){.#if 
e04d0 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54 4f  defined(NO_GETTO
e04e0 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20  D).  time_t t;. 
e04f0 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72   time(&t);.  *pr
e0500 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e 30 20  Now = t/86400.0 
e0510 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 65 6c  + 2440587.5;.#el
e0520 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
e0530 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 20  struct timespec 
e0540 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f 67 65  sNow;.  clock_ge
e0550 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 41 4c  ttime(CLOCK_REAL
e0560 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a 20 20  TIME, &sNow);.  
e0570 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 37  *prNow = 2440587
e0580 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 63  .5 + sNow.tv_sec
e0590 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 2e  /86400.0 + sNow.
e05a0 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 30 30  tv_nsec/86400000
e05b0 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 65 0a  000000.0;.#else.
e05c0 20 20 73 74 72 75 63 74 20 74 69 6d 65 76 61 6c    struct timeval
e05d0 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 6d 65   sNow;.  gettime
e05e0 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 29 3b  ofday(&sNow, 0);
e05f0 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30  .  *prNow = 2440
e0600 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f  587.5 + sNow.tv_
e0610 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e  sec/86400.0 + sN
e0620 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 30 30  ow.tv_usec/86400
e0630 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 69 66  000000.0;.#endif
e0640 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
e0650 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74  TEST.  if( sqlit
e0660 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
e0670 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20  ){.    *prNow = 
e0680 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
e0690 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32  time/86400.0 + 2
e06a0 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65  440587.5;.  }.#e
e06b0 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41  ndif.  UNUSED_PA
e06c0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e06d0 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
e06e0 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 64 20  ./*.** We added 
e06f0 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 72 6f  the xGetLastErro
e0700 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20  r() method with 
e0710 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 6f 66  the intention of
e0720 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 62 65   providing.** be
e0730 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c 20 65  tter low-level e
e0740 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 68  rror messages wh
e0750 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73  en operating-sys
e0760 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f 6d  tem problems com
e0770 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20 53  e up.** during S
e0780 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 2e  QLite operation.
e0790 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 6e 6f    But so far, no
e07a0 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 20 62  ne of that has b
e07b0 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a  een implemented.
e07c0 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 2e 20  ** in the core. 
e07d0 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   So this routine
e07e0 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64   is never called
e07f0 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 20 69  .  For now, it i
e0800 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 70 6c  s merely.** a pl
e0810 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f 0a 73  ace-holder..*/.s
e0820 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47 65  tatic int unixGe
e0830 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74  tLastError(sqlit
e0840 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e0850 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c 20 63   int NotUsed2, c
e0860 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 7b 0a  har *NotUsed3){.
e0870 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
e0880 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 55  ER(NotUsed);.  U
e0890 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e08a0 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 4e 55  NotUsed2);.  UNU
e08b0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
e08c0 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 75 72  tUsed3);.  retur
e08d0 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a  n 0;.}../*.*****
e08e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e08f0 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
e0900 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a  e3_vfs methods *
e0910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
e0930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a  *********/../***
e0980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e09a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e09b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e09c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
e09d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e09e0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 72 6f  ****** Begin Pro
e09f0 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a  xy Locking *****
e0a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
e0a20 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  * Proxy locking 
e0a30 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 6b 69  is a "uber-locki
e0a40 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74 68  ng-method" in th
e0a50 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20 75 73  is sense:  It us
e0a60 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 72 20  es the.** other 
e0a70 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20  locking methods 
e0a80 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c 6f 63  on secondary loc
e0a90 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 79 20  k files.  Proxy 
e0aa0 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a 2a 20  locking is a.** 
e0ab0 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 72 20  meta-layer over 
e0ac0 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69 6d 69  top of the primi
e0ad0 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70  tive locking imp
e0ae0 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 2e 20  lemented above. 
e0af0 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 65 61   For.** this rea
e0b00 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f  son, the divisio
e0b10 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  n that implement
e0b20 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69  s of proxy locki
e0b30 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 0a 2a  ng is deferred.*
e0b40 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e 20  * until late in 
e0b50 74 68 65 20 66 69 6c 65 20 28 68 65 72 65 29 20  the file (here) 
e0b60 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65  after all of the
e0b70 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68 6f   other I/O metho
e0b80 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20  ds have.** been 
e0b90 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68 61  defined - so tha
e0ba0 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20  t the primitive 
e0bb0 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 20  locking methods 
e0bc0 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  are available.**
e0bd0 20 61 73 20 73 65 72 76 69 63 65 73 20 74 6f 20   as services to 
e0be0 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 69 6d  help with the im
e0bf0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
e0c00 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a  proxy locking..*
e0c10 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  *.****.**.** The
e0c20 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67   default locking
e0c30 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51 4c 69   schemes in SQLi
e0c40 74 65 20 75 73 65 20 62 79 74 65 2d 72 61 6e 67  te use byte-rang
e0c50 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 0a 2a  e locks on the.*
e0c60 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
e0c70 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 73 61  to coordinate sa
e0c80 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 61  fe, concurrent a
e0c90 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 6c  ccess by multipl
e0ca0 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 6e 64  e readers.** and
e0cb0 20 77 72 69 74 65 72 73 20 5b 68 74 74 70 3a 2f   writers [http:/
e0cc0 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f 63 6b  /sqlite.org/lock
e0cd0 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 54 68  ingv3.html].  Th
e0ce0 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f 63 6b  e five file lock
e0cf0 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 28 55  ing.** states (U
e0d00 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 4e 47  NLOCKED, PENDING
e0d10 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 56  , SHARED, RESERV
e0d20 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29 20 61  ED, EXCLUSIVE) a
e0d30 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a  re implemented.*
e0d40 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61 64 20  * as POSIX read 
e0d50 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 6f 76  & write locks ov
e0d60 65 72 20 66 69 78 65 64 20 73 65 74 20 6f 66 20  er fixed set of 
e0d70 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 20 66  locations (via f
e0d80 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 46 50  sctl),.** on AFP
e0d90 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 65 78   and SMB only ex
e0da0 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 6e  clusive byte-ran
e0db0 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 76 61  ge locks are ava
e0dc0 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63 74 6c  ilable via fsctl
e0dd0 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 28 27  .** with _IOWR('
e0de0 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42  z', 23, struct B
e0df0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29  yteRangeLockPB2)
e0e00 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20 73 61   to track the sa
e0e10 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a 2a 20  me 5 states..** 
e0e20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 46 5f  To simulate a F_
e0e30 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 68 61  RDLCK on the sha
e0e40 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 41 46  red range, on AF
e0e50 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 65 6c  P a randomly sel
e0e60 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 73 73  ected.** address
e0e70 20 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 72   in the shared r
e0e80 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66 6f  ange is taken fo
e0e90 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c  r a SHARED lock,
e0ea0 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 73   the entire.** s
e0eb0 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 20 74  hared range is t
e0ec0 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 43 4c  aken for an EXCL
e0ed0 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a 2a 0a  USIVE lock):.**.
e0ee0 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47 5f  **      PENDING_
e0ef0 42 59 54 45 20 20 20 20 20 20 20 20 30 78 34 30  BYTE        0x40
e0f00 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a 2a 20  000000..   ..** 
e0f10 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42 59       RESERVED_BY
e0f20 54 45 20 20 20 20 20 20 20 30 78 34 30 30 30 30  TE       0x40000
e0f30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 41 52  001.**      SHAR
e0f40 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20 20 20  ED_RANGE        
e0f50 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20 30 78  0x40000002 -> 0x
e0f60 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20 54  40000200.**.** T
e0f70 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c 20 6f  his works well o
e0f80 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65  n the local file
e0f90 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73 68 6f   system, but sho
e0fa0 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30 30 78  ws a nearly 100x
e0fb0 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 6e 20  .** slowdown in 
e0fc0 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e 63 65  read performance
e0fd0 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73 65 20   on AFP because 
e0fe0 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74 20 64  the AFP client d
e0ff0 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 20 72  isables.** the r
e1000 65 61 64 20 63 61 63 68 65 20 77 68 65 6e 20 62  ead cache when b
e1010 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20  yte-range locks 
e1020 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20 45 6e  are present.  En
e1030 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61 64 0a  abling the read.
e1040 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73 65 73  ** cache exposes
e1050 20 61 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e   a cache coheren
e1060 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 74 20  cy problem that 
e1070 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 61 6c  is present on al
e1080 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 6f 72  l OS X.** suppor
e1090 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65  ted network file
e10a0 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 20 61   systems.  NFS a
e10b0 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62 73 65  nd AFP both obse
e10c0 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f 73 65  rve the.** close
e10d0 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e 74 69  -to-open semanti
e10e0 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20  cs for ensuring 
e10f0 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 0a  cache coherency.
e1100 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e 73  ** [http://nfs.s
e1110 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 2f 23  ourceforge.net/#
e1120 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 20 64  faq_a8], which d
e1130 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74 69 76  oes not effectiv
e1140 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 20 74  ely.** address t
e1150 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 20  he requirements 
e1160 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 20 64  for concurrent d
e1170 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 62  atabase access b
e1180 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 72 65  y multiple.** re
e1190 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65 72  aders and writer
e11a0 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 77  s.** [http://www
e11b0 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c 69  .nabble.com/SQLi
e11c0 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 2d  te-on-NFS-cache-
e11d0 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 36 35  coherency-td1565
e11e0 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a 2a  5701.html]..**.*
e11f0 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74 68 65  * To address the
e1200 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e 64   performance and
e1210 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79   cache coherency
e1220 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20 66   issues, proxy f
e1230 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 63  ile locking.** c
e1240 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 20 64  hanges the way d
e1250 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 69  atabase access i
e1260 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20  s controlled by 
e1270 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73 20  limiting access 
e1280 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 68  to a.** single h
e1290 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 61 6e  ost at a time an
e12a0 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c 6f  d moving file lo
e12b0 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20 64  cks off of the d
e12c0 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20  atabase file.** 
e12d0 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f 78 79  and onto a proxy
e12e0 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63   file on the loc
e12f0 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20  al file system. 
e1300 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e 67   .**.**.** Using
e1310 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a 20   proxy locks.** 
e1320 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e1330 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 0a 2a  -.**.** C APIs.*
e1340 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 69  *.**  sqlite3_fi
e1350 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 64  le_control(db, d
e1360 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 53 45  bname, SQLITE_SE
e1370 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c  T_LOCKPROXYFILE,
e1380 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
e1390 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f 78 79            <proxy
e13a0 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 6f 3a  _path> | ":auto:
e13b0 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  ");.**  sqlite3_
e13c0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c  file_control(db,
e13d0 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f   dbname, SQLITE_
e13e0 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  GET_LOCKPROXYFIL
e13f0 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 68 3e  E, &<proxy_path>
e1400 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c 20  );.**.**.** SQL 
e1410 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20 50  pragmas.**.**  P
e1420 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
e1430 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65  ]lock_proxy_file
e1440 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20  =<proxy_path> | 
e1450 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 47 4d  :auto:.**  PRAGM
e1460 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63  A [database.]loc
e1470 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a 0a  k_proxy_file.**.
e1480 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20 22 3a  ** Specifying ":
e1490 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 68 61  auto:" means tha
e14a0 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  t if there is a 
e14b0 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 68 20  conch file with 
e14c0 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 68 6f  a matching.** ho
e14d0 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74 68 65  st ID in it, the
e14e0 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e 20 74   proxy path in t
e14f0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69  he conch file wi
e1500 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 68 65  ll be used, othe
e1510 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f 78 79  rwise.** a proxy
e1520 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e 20 74   path based on t
e1530 68 65 20 75 73 65 72 27 73 20 74 65 6d 70 20 64  he user's temp d
e1540 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e 66 73  ir.** (via confs
e1550 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53  tr(_CS_DARWIN_US
e1560 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e 29  ER_TEMP_DIR,...)
e1570 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 61  ) will be used a
e1580 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c  nd the.** actual
e1590 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 6d 65   proxy file name
e15a0 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 72   is generated fr
e15b0 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 20  om the name and 
e15c0 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64  path of the.** d
e15d0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 46  atabase file.  F
e15e0 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a  or example:.**.*
e15f0 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61 74 61  *       For data
e1600 62 61 73 65 20 70 61 74 68 20 22 2f 55 73 65 72  base path "/User
e1610 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a 2a  s/me/foo.db" .**
e1620 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63 6b 20         The lock 
e1630 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22 3c 74  path will be "<t
e1640 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 6c 6f  mpdir>/sqliteplo
e1650 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f 66 6f  cks/_Users_me_fo
e1660 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a 0a  o.db:auto:").**.
e1670 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 70  ** Once a lock p
e1680 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 75 72  roxy is configur
e1690 65 64 20 66 6f 72 20 61 20 64 61 74 61 62 61 73  ed for a databas
e16a0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 74  e connection, it
e16b0 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20 72   can not.** be r
e16c0 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72 20  emoved, however 
e16d0 69 74 20 6d 61 79 20 62 65 20 73 77 69 74 63 68  it may be switch
e16e0 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e  ed to a differen
e16f0 74 20 70 72 6f 78 79 20 70 61 74 68 20 76 69 61  t proxy path via
e1700 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41 50  .** the above AP
e1710 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68 65  Is (assuming the
e1720 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20 6e   conch file is n
e1730 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62 79  ot being held by
e1740 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 6e   another.** conn
e1750 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65 73  ection or proces
e1760 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48 6f  s). .**.**.** Ho
e1770 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  w proxy locking 
e1780 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d  works.** -------
e1790 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e17a0 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 69 6c  .**.** Proxy fil
e17b0 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 65 73  e locking relies
e17c0 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 74 77   primarily on tw
e17d0 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69 6e 67  o new supporting
e17e0 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20 20   files: .**.**  
e17f0 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 20 74   *  conch file t
e1800 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73 20 74  o limit access t
e1810 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
e1820 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 20  ile to a single 
e1830 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 74 20  host.**      at 
e1840 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20 2a  a time.**.**   *
e1850 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74 6f 20    proxy file to 
e1860 61 63 74 20 61 73 20 61 20 70 72 6f 78 79 20 66  act as a proxy f
e1870 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72 79 20  or the advisory 
e1880 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a  locks normally.*
e1890 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f 6e 20  *      taken on 
e18a0 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 0a  the database.**.
e18b0 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69 6c  ** The conch fil
e18c0 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72 6f  e - to use a pro
e18d0 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65 20  xy file, sqlite 
e18e0 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c 64  must first "hold
e18f0 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a 20 62   the conch".** b
e1900 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c 69  y taking an sqli
e1910 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64 20  te-style shared 
e1920 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63  lock on the conc
e1930 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67 20  h file, reading 
e1940 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20  the.** contents 
e1950 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74 68  and comparing th
e1960 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65 20  e host's unique 
e1970 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 65 6c  host ID (see bel
e1980 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a 20  ow) and lock.** 
e1990 70 72 6f 78 79 20 70 61 74 68 20 61 67 61 69 6e  proxy path again
e19a0 73 74 20 74 68 65 20 76 61 6c 75 65 73 20 73 74  st the values st
e19b0 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e 63  ored in the conc
e19c0 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 66 69  h.  The conch fi
e19d0 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20  le is.** stored 
e19e0 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 65  in the same dire
e19f0 63 74 6f 72 79 20 61 73 20 74 68 65 20 64 61 74  ctory as the dat
e1a00 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74  abase file and t
e1a10 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20  he file name.** 
e1a20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61 66 74  is patterned aft
e1a30 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  er the database 
e1a40 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 2e 3c  file name as ".<
e1a50 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d 63 6f  databasename>-co
e1a60 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 65 20  nch"..** If the 
e1a70 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 73 20  conch file does 
e1a80 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 69 74  not exist, or it
e1a90 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f 20 6e  's contents do n
e1aa0 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a 2a 20  ot match the.** 
e1ab0 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 20 70  host ID and/or p
e1ac0 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65 6e 20  roxy path, then 
e1ad0 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 63 61  the lock is esca
e1ae0 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78 63 6c  lated to an excl
e1af0 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61 6e  usive.** lock an
e1b00 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  d the conch file
e1b10 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 70 64   contents is upd
e1b20 61 74 65 64 20 77 69 74 68 20 74 68 65 20 68 6f  ated with the ho
e1b30 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78 79 0a  st ID and proxy.
e1b40 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68 65 20  ** path and the 
e1b50 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 61 64  lock is downgrad
e1b60 65 64 20 74 6f 20 61 20 73 68 61 72 65 64 20 6c  ed to a shared l
e1b70 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 20 74  ock again.  If t
e1b80 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 20 68  he conch.** is h
e1b90 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 70  eld by another p
e1ba0 72 6f 63 65 73 73 20 28 77 69 74 68 20 61 20 73  rocess (with a s
e1bb0 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 68 65  hared lock), the
e1bc0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a   exclusive lock.
e1bd0 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64  ** will fail and
e1be0 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
e1bf0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
e1c00 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 2d  The proxy file -
e1c10 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 66   a single-byte f
e1c20 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61 6c 6c  ile used for all
e1c30 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65 20 6c   advisory file l
e1c40 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79  ocks.** normally
e1c50 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 61   taken on the da
e1c60 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 20 54  tabase file.   T
e1c70 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 20 73  his allows for s
e1c80 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 6f  afe sharing.** o
e1c90 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
e1ca0 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65  ile for multiple
e1cb0 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 69   readers and wri
e1cc0 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61 6d 65  ters on the same
e1cd0 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63 6f  .** host (the co
e1ce0 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68 61 74  nch ensures that
e1cf0 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74 68   they all use th
e1d00 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f 63  e same local loc
e1d10 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20 54  k file)..**.** T
e1d20 68 65 72 65 20 69 73 20 61 20 74 68 69 72 64 20  here is a third 
e1d30 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 74 20  file - the host 
e1d40 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64 20 61  ID file - used a
e1d50 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 20 72  s a persistent r
e1d60 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 75 6e  ecord.** of a un
e1d70 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 20  ique identifier 
e1d80 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 61 20  for the host, a 
e1d90 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75 65 20  128-byte unique 
e1da0 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a 20  host id file.** 
e1db0 69 6e 20 74 68 65 20 70 61 74 68 20 64 65 66 69  in the path defi
e1dc0 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53 54 49  ned by the HOSTI
e1dd0 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64 65 66  DPATH macro (def
e1de0 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a 2a 2a  ault value is.**
e1df0 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65 73   /Library/Caches
e1e00 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69  /.com.apple.sqli
e1e10 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 2e 0a  teConchHostId)..
e1e20 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 6e 67  **.** Requesting
e1e30 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 79 20   the lock proxy 
e1e40 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 69 61  does not immedia
e1e50 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20 63 6f  tely take the co
e1e60 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 6f 6e  nch, it is.** on
e1e70 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 68  ly taken when th
e1e80 65 20 66 69 72 73 74 20 72 65 71 75 65 73 74 20  e first request 
e1e90 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 73 65  to lock database
e1ea0 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e 20 20   file is made.  
e1eb0 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 65 73  .** This matches
e1ec0 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 20 6f   the semantics o
e1ed0 66 20 74 68 65 20 74 72 61 64 69 74 69 6f 6e 61  f the traditiona
e1ee0 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 76 69  l locking behavi
e1ef0 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f 70 65  or, where.** ope
e1f00 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 69 6f  ning a connectio
e1f10 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  n to a database 
e1f20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 74 61  file does not ta
e1f30 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 74 2e  ke a lock on it.
e1f40 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 6c  .** The shared l
e1f50 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e 20  ock and an open 
e1f60 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e1f70 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 20 75  are maintained u
e1f80 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 6f 6e  ntil .** the con
e1f90 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 64  nection to the d
e1fa0 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 65  atabase is close
e1fb0 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72  d. .**.** The pr
e1fc0 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68 65  oxy file and the
e1fd0 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20 6e   lock file are n
e1fe0 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f 20  ever deleted so 
e1ff0 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0a 2a  they only need.*
e2000 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  * to be created 
e2010 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  the first time t
e2020 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a 2a 2a  hey are used..**
e2030 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f  .** Configuratio
e2040 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d  n options.** ---
e2050 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e2060 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 45  --.**.**  SQLITE
e2070 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f  _PREFER_PROXY_LO
e2080 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 20 20  CKING.**.**     
e2090 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 73    Database files
e20a0 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e 6f 6e   accessed on non
e20b0 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74  -local file syst
e20c0 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 20  ems are.**      
e20d0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
e20e0 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 70 72  onfigured for pr
e20f0 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c 6f 63  oxy locking, loc
e2100 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20 20  k files are.**  
e2110 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74 6f 6d       named autom
e2120 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 20 74  atically using t
e2130 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 61 73  he same logic as
e2140 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47 4d 41  .**       PRAGMA
e2150 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65   lock_proxy_file
e2160 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 20 20  =":auto:".**    
e2170 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f 58  .**  SQLITE_PROX
e2180 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 20 20  Y_DEBUG.**.**   
e2190 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65 20      Enables the 
e21a0 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f 72  logging of error
e21b0 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e 67   messages during
e21c0 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a   host id file.**
e21d0 20 20 20 20 20 20 20 72 65 74 72 69 65 76 61 6c         retrieval
e21e0 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a 2a 2a   and creation.**
e21f0 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 48 0a  .**  HOSTIDPATH.
e2200 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 72  **.**       Over
e2210 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75 6c  rides the defaul
e2220 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 70  t host ID file p
e2230 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 0a  ath location.**.
e2240 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52  **  LOCKPROXYDIR
e2250 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65  .**.**       Ove
e2260 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75  rrides the defau
e2270 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75 73 65  lt directory use
e2280 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f 78 79  d for lock proxy
e2290 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a 20 20   files that.**  
e22a0 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64 20 61       are named a
e22b0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 69 61  utomatically via
e22c0 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 73 65   the ":auto:" se
e22d0 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 51 4c  tting.**.**  SQL
e22e0 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58  ITE_DEFAULT_PROX
e22f0 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53  YDIR_PERMISSIONS
e2300 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 65 72  .**.**       Per
e2310 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 65 20  missions to use 
e2320 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20  when creating a 
e2330 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20 73 74  directory for st
e2340 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20 20  oring the.**    
e2350 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69     lock proxy fi
e2360 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 20 77  les, only used w
e2370 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52  hen LOCKPROXYDIR
e2380 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a 20   is not set..** 
e2390 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 41 73     .**    .** As
e23a0 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76 65   mentioned above
e23b0 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20  , when compiled 
e23c0 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45 46  with SQLITE_PREF
e23d0 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47  ER_PROXY_LOCKING
e23e0 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68 65  ,.** setting the
e23f0 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 72   environment var
e2400 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f 52  iable SQLITE_FOR
e2410 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47  CE_PROXY_LOCKING
e2420 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 66 6f   to 1 will.** fo
e2430 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  rce proxy lockin
e2440 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72  g to be used for
e2450 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20   every database 
e2460 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e 64  file opened, and
e2470 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 63 65   0.** will force
e2480 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f 78 79   automatic proxy
e2490 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 64   locking to be d
e24a0 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c 6c 20  isabled for all 
e24b0 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65  database.** file
e24c0 73 20 28 65 78 70 6c 69 63 69 74 79 20 63 61 6c  s (explicity cal
e24d0 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f  ling the SQLITE_
e24e0 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  SET_LOCKPROXYFIL
e24f0 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a 20 73  E pragma or.** s
e2500 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e 74 72  qlite_file_contr
e2510 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 61 66  ol API is not af
e2520 66 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 45  fected by SQLITE
e2530 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43  _FORCE_PROXY_LOC
e2540 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  KING)..*/../*.**
e2550 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69   Proxy locking i
e2560 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
e2570 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f 0a 23   on MacOSX .*/.#
e2580 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  if defined(__APP
e2590 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
e25a0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e25b0 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 51 4c  TYLE..#ifdef SQL
e25c0 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 6d 75  ITE_TEST./* simu
e25d0 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68 6f  late multiple ho
e25e0 73 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 20  sts by creating 
e25f0 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66 69  unique hostid fi
e2600 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 4c 49  le paths */.SQLI
e2610 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
e2620 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d 20  e3_hostid_num = 
e2630 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
e2640 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b 69 6e   The proxyLockin
e2650 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 68 65  gContext has the
e2660 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 20 73   path and file s
e2670 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 74 68  tructures for th
e2680 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 6e 64  e remote .** and
e2690 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 69 6c   local proxy fil
e26a0 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 70 65  es in it.*/.type
e26b0 64 65 66 20 73 74 72 75 63 74 20 70 72 6f 78 79  def struct proxy
e26c0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 70  LockingContext p
e26d0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
e26e0 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f 78 79  xt;.struct proxy
e26f0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 7b  LockingContext {
e2700 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e  .  unixFile *con
e2710 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20  chFile;         
e2720 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 66 69  /* Open conch fi
e2730 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 6f  le */.  char *co
e2740 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 20 20  nchFilePath;    
e2750 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
e2760 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 2a  the conch file *
e2770 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c 6f  /.  unixFile *lo
e2780 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20 20  ckProxy;        
e2790 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20 6c   /* Open proxy l
e27a0 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68  ock file */.  ch
e27b0 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61 74  ar *lockProxyPat
e27c0 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  h;         /* Na
e27d0 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20  me of the proxy 
e27e0 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63  lock file */.  c
e27f0 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 20  har *dbPath;    
e2800 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
e2810 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20  ame of the open 
e2820 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6f  file */.  int co
e2830 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 20 20  nchHeld;        
e2840 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
e2850 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 63  f the conch is c
e2860 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 2a 2f  urrently held */
e2870 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63 6b  .  void *oldLock
e2880 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20  ingContext;     
e2890 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63 6b  /* Original lock
e28a0 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 72 65  ingcontext to re
e28b0 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20 2a  store on close *
e28c0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  /.  sqlite3_io_m
e28d0 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4f  ethods const *pO
e28e0 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f 2a  ldMethod;     /*
e28f0 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d 65   Original I/O me
e2900 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65 20  thods for close 
e2910 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 49 44  */.};../* HOSTID
e2920 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c 45 4e  LEN and CONCHLEN
e2930 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 73 70   both include sp
e2940 61 63 65 20 66 6f 72 20 74 68 65 20 73 74 72 69  ace for the stri
e2950 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 69  ng .** terminati
e2960 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 66 69  ng nul .*/.#defi
e2970 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 20 20  ne HOSTIDLEN    
e2980 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65       128.#define
e2990 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 20 20   CONCHLEN       
e29a0 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e 2b 48     (MAXPATHLEN+H
e29b0 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 66 6e  OSTIDLEN+1).#ifn
e29c0 64 65 66 20 48 4f 53 54 49 44 50 41 54 48 0a 23  def HOSTIDPATH.#
e29d0 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44 50 41   define HOSTIDPA
e29e0 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62 72 61  TH       "/Libra
e29f0 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e 61  ry/Caches/.com.a
e2a00 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63 68  pple.sqliteConch
e2a10 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 0a 0a  HostId".#endif..
e2a20 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 20 63  /* basically a c
e2a30 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e 64 6f  opy of unixRando
e2a40 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66 66 65  mness with diffe
e2a50 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 65 68  rent.** test beh
e2a60 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e 20 2a  avior built in *
e2a70 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
e2a80 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 44  xyGenerateHostID
e2a90 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 29 7b  (char *pHostID){
e2aa0 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64 2c 20  .  int pid, fd, 
e2ab0 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  len;.  unsigned 
e2ac0 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 6e 73  char *key = (uns
e2ad0 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 48 6f  igned char *)pHo
e2ae0 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d 73 65  stID;.  .  memse
e2af0 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 49 44  t(key, 0, HOSTID
e2b00 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 3b  LEN);.  len = 0;
e2b10 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64  .  fd = open("/d
e2b20 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52  ev/urandom", O_R
e2b30 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 66 64  DONLY);.  if( fd
e2b40 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d  >=0 ){.    len =
e2b50 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c 20 48   read(fd, key, H
e2b60 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20 63  OSTIDLEN);.    c
e2b70 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 6c  lose(fd); /* sil
e2b80 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 66  ently leak the f
e2b90 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a 2f  d if it fails */
e2ba0 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e 20 3c  .  }.  if( len <
e2bb0 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20   HOSTIDLEN ){.  
e2bc0 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20    time_t t;.    
e2bd0 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 6d 65  time(&t);.    me
e2be0 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 73 69  mcpy(key, &t, si
e2bf0 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 70 69  zeof(t));.    pi
e2c00 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20  d = getpid();.  
e2c10 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b 73 69    memcpy(&key[si
e2c20 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c 20  zeof(t)], &pid, 
e2c30 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20  sizeof(pid));.  
e2c40 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 4b 45  }.  .#ifdef MAKE
e2c50 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44 0a 20  _PRETTY_HOSTID. 
e2c60 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20   {.    int i;.  
e2c70 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 65 20    /* filter the 
e2c80 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69 6e 74  bytes into print
e2c90 61 62 6c 65 20 61 73 63 69 69 20 63 68 61 72 61  able ascii chara
e2ca0 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 74 65  cters and NUL te
e2cb0 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 20 6b  rminate */.    k
e2cc0 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d 31 29  ey[(HOSTIDLEN-1)
e2cd0 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 66 6f  ] = 0x00;.    fo
e2ce0 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 54 49  r( i=0; i<(HOSTI
e2cf0 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 7b 0a  DLEN-1); i++ ){.
e2d00 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63        unsigned c
e2d10 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 5d 26  har pa = key[i]&
e2d20 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66 28 20  0x7F;.      if( 
e2d30 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 20 20  pa<0x20 ){.     
e2d40 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 79     key[i] = (key
e2d50 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 30  [i]&0x80 == 0x80
e2d60 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 70 61  ) ? pa+0x40 : pa
e2d70 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d 65 6c  +0x20;.      }el
e2d80 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37 46 20  se if( pa==0x7F 
e2d90 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b 69  ){.        key[i
e2da0 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 30  ] = (key[i]&0x80
e2db0 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 3d 30   == 0x80) ? pa=0
e2dc0 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b 0a 20  x20 : pa+0x7E;. 
e2dd0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
e2de0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
e2df0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
e2e00 2a 20 77 72 69 74 65 73 20 74 68 65 20 68 6f 73  * writes the hos
e2e10 74 20 69 64 20 70 61 74 68 20 74 6f 20 70 61 74  t id path to pat
e2e20 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 20 62  h, path should b
e2e30 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 61 74  e an pre-allocat
e2e40 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 69 74  ed buffer.** wit
e2e50 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 66  h enough space f
e2e60 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a 73 74  or a path .*/.st
e2e70 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78 79 47  atic void proxyG
e2e80 65 74 48 6f 73 74 49 44 50 61 74 68 28 63 68 61  etHostIDPath(cha
e2e90 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f 74 20  r *path, size_t 
e2ea0 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 79 28  len){.  strlcpy(
e2eb0 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41 54 48  path, HOSTIDPATH
e2ec0 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 20 53  , len);.#ifdef S
e2ed0 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28  QLITE_TEST.  if(
e2ee0 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f   sqlite3_hostid_
e2ef0 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 68 61  num>0 ){.    cha
e2f00 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 22 31  r suffix[2] = "1
e2f10 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b 30 5d  ";.    suffix[0]
e2f20 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 20 73   = suffix[0] + s
e2f30 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75  qlite3_hostid_nu
e2f40 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 28 70  m;.    strlcat(p
e2f50 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c 65 6e  ath, suffix, len
e2f60 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
e2f70 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53  OSTRACE3("GETHOS
e2f80 54 49 44 50 41 54 48 20 20 25 73 20 70 69 64 3d  TIDPATH  %s pid=
e2f90 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 65 74  %d\n", path, get
e2fa0 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 67 65  pid());.}../* ge
e2fb0 74 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 72  t the host ID fr
e2fc0 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f 73 74  om a sqlite host
e2fd0 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64 20 69  id file stored i
e2fe0 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 2d 73  n the .** user-s
e2ff0 70 65 63 69 66 69 63 20 74 6d 70 20 64 69 72 65  pecific tmp dire
e3000 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20 74 68  ctory, create th
e3010 65 20 49 44 20 69 66 20 69 74 27 73 20 6e 6f 74  e ID if it's not
e3020 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 20 0a   there already .
e3030 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
e3040 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63 68 61  oxyGetHostID(cha
e3050 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74 20  r *pHostID, int 
e3060 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e 74 20  *pError){.  int 
e3070 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74 68 5b  fd;.  char path[
e3080 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a 20 20  MAXPATHLEN]; .  
e3090 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 69 6e  size_t len;.  in
e30a0 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b 0a  t rc=SQLITE_OK;.
e30b0 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49  .  proxyGetHostI
e30c0 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41 58 50  DPath(path, MAXP
e30d0 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 74 72  ATHLEN);.  /* tr
e30e0 79 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  y to create the 
e30f0 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 69 66  host ID file, if
e3100 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 73   it already exis
e3110 74 73 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74  ts read the cont
e3120 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d 20 6f  ents */.  fd = o
e3130 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 45 41  pen(path, O_CREA
e3140 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 58 43  T|O_WRONLY|O_EXC
e3150 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 28 20  L, 0644);.  if( 
e3160 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  fd<0 ){.    int 
e3170 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a 20 20  err=errno;....  
e3180 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 53    if( err!=EEXIS
e3190 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  T ){.#ifdef SQLI
e31a0 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f  TE_PROXY_DEBUG /
e31b0 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65  * set the sqlite
e31c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
e31d0 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20  nstead */.      
e31e0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
e31f0 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 63 72  "sqlite error cr
e3200 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44 20 66  eating host ID f
e3210 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a 20  ile %s: %s\n",. 
e3220 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74               pat
e3230 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29  h, strerror(err)
e3240 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
e3250 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45  return SQLITE_PE
e3260 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a  RM;.    }.    /*
e3270 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 65   couldn't create
e3280 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61 64 20   the file, read 
e3290 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20  it instead */.  
e32a0 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68    fd = open(path
e32b0 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 58 43  , O_RDONLY|O_EXC
e32c0 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30  L);.    if( fd<0
e32d0 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   ){.#ifdef SQLIT
e32e0 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f 2a  E_PROXY_DEBUG /*
e32f0 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 20   set the sqlite 
e3300 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
e3310 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 69  stead */.      i
e3320 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a  nt err = errno;.
e3330 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
e3340 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72  derr, "sqlite er
e3350 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f 73 74  ror opening host
e3360 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 5c   ID file %s: %s\
e3370 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
e3380 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f 72    path, strerror
e3390 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20  (err));.#endif. 
e33a0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
e33b0 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20  TE_PERM;.    }. 
e33c0 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 28 66     len = pread(f
e33d0 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 54  d, pHostID, HOST
e33e0 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 69  IDLEN, 0);.    i
e33f0 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 20  f( len<0 ){.    
e3400 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 6e    *pError = errn
e3410 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  o;.      rc = SQ
e3420 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b  LITE_IOERR_READ;
e3430 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c  .    }else if( l
e3440 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a  en<HOSTIDLEN ){.
e3450 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20        *pError = 
e3460 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  0;.      rc = SQ
e3470 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
e3480 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 20 20  _READ;.    }.   
e3490 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73   close(fd); /* s
e34a0 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65  ilently leak the
e34b0 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20   fd if it fails 
e34c0 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  */.    OSTRACE3(
e34d0 22 47 45 54 48 4f 53 54 49 44 20 20 72 65 61 64  "GETHOSTID  read
e34e0 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %s pid=%d\n", p
e34f0 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 29  HostID, getpid()
e3500 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
e3510 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
e3520 2a 20 77 65 27 72 65 20 63 72 65 61 74 69 6e 67  * we're creating
e3530 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c   the host ID fil
e3540 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f 6d 20  e (use a random 
e3550 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65 73 29  string of bytes)
e3560 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 65 6e   */.    proxyGen
e3570 65 72 61 74 65 48 6f 73 74 49 44 28 70 48 6f 73  erateHostID(pHos
e3580 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20  tID);.    len = 
e3590 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f 73 74  pwrite(fd, pHost
e35a0 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 30  ID, HOSTIDLEN, 0
e35b0 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 30  );.    if( len<0
e35c0 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f   ){.      *pErro
e35d0 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  r = errno;.     
e35e0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45   rc = SQLITE_IOE
e35f0 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65  RR_WRITE;.    }e
e3600 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53 54  lse if( len<HOST
e3610 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20 2a  IDLEN ){.      *
e3620 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pError = 0;.    
e3630 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
e3640 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d  ERR_WRITE;.    }
e3650 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20  .    close(fd); 
e3660 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b  /* silently leak
e3670 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61   the fd if it fa
e3680 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41  ils */.    OSTRA
e3690 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 20  CE3("GETHOSTID  
e36a0 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25 64 5c  wrote %s pid=%d\
e36b0 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 74  n", pHostID, get
e36c0 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 75  pid());.    retu
e36d0 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 73 74  rn rc;.  }.}..st
e36e0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65  atic int proxyGe
e36f0 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 74 20  tLockPath(const 
e3700 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68  char *dbPath, ch
e3710 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a 65 5f  ar *lPath, size_
e3720 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 6e 74  t maxLen){.  int
e3730 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 4c 65   len;.  int dbLe
e3740 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 69 66  n;.  int i;..#if
e3750 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49 52  def LOCKPROXYDIR
e3760 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 79  .  len = strlcpy
e3770 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 4f 58  (lPath, LOCKPROX
e3780 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23  YDIR, maxLen);.#
e3790 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f 43 53  else.# ifdef _CS
e37a0 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d  _DARWIN_USER_TEM
e37b0 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 63 6f  P_DIR.  {.    co
e37c0 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e  nfstr(_CS_DARWIN
e37d0 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 20  _USER_TEMP_DIR, 
e37e0 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 3b 0a  lPath, maxLen);.
e37f0 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61      len = strlca
e3800 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74 65  t(lPath, "sqlite
e3810 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e 29  plocks", maxLen)
e3820 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 72 28  ;.    if( mkdir(
e3830 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f 44 45  lPath, SQLITE_DE
e3840 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50  FAULT_PROXYDIR_P
e3850 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b 0a 20  ERMISSIONS) ){. 
e3860 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 69 72       /* if mkdir
e3870 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 20 61   fails, handle a
e3880 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 65 61  s lock file crea
e3890 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a 2f 0a  tion failure */.
e38a0 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20        int err = 
e38b0 65 72 72 6e 6f 3b 0a 23 20 20 69 66 64 65 66 20  errno;.#  ifdef 
e38c0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
e38d0 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49     if( err!=EEXI
e38e0 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20 66 70  ST ){.        fp
e38f0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 70  rintf(stderr, "p
e3900 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 3a  roxyGetLockPath:
e3910 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 20 65   mkdir(%s,0%o) e
e3920 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c 20 6c  rror %d %s\n", l
e3930 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 20 20  Path,.          
e3940 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 46        SQLITE_DEF
e3950 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45  AULT_PROXYDIR_PE
e3960 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 2c 20  RMISSIONS, err, 
e3970 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a  strerror(err));.
e3980 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 69 66        }.#  endif
e3990 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
e39a0 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 4c    OSTRACE3("GETL
e39b0 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 20 25  OCKPATH  mkdir %
e39c0 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50 61  s pid=%d\n", lPa
e39d0 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20  th, getpid());. 
e39e0 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a 23 20     }.    .  }.# 
e39f0 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74 72  else.  len = str
e3a00 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74 6d  lcpy(lPath, "/tm
e3a10 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 20  p/", maxLen);.# 
e3a20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20  endif.#endif..  
e3a30 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 5d  if( lPath[len-1]
e3a40 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65 6e  !='/' ){.    len
e3a50 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68   = strlcat(lPath
e3a60 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a  , "/", maxLen);.
e3a70 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61 6e    }.  .  /* tran
e3a80 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61 74  sform the db pat
e3a90 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63 61  h to a unique ca
e3aa0 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 62  che name */.  db
e3ab0 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  Len = (int)strle
e3ac0 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f 72  n(dbPath);.  for
e3ad0 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20 26  ( i=0; i<dbLen &
e3ae0 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 78 4c  & (i+len+7)<maxL
e3af0 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68  en; i++){.    ch
e3b00 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69 5d  ar c = dbPath[i]
e3b10 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c 65  ;.    lPath[i+le
e3b20 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27 5f  n] = (c=='/')?'_
e3b30 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74 68  ':c;.  }.  lPath
e3b40 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20 20  [i+len]='\0';.  
e3b50 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22  strlcat(lPath, "
e3b60 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e 29  :auto:", maxLen)
e3b70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
e3b80 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
e3b90 72 65 61 74 65 20 61 20 6e 65 77 20 56 46 53 20  reate a new VFS 
e3ba0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e3bb0 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72  (stored in memor
e3bc0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a  y obtained from.
e3bd0 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
e3be0 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 65 20  c) and open the 
e3bf0 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 74 68  file named "path
e3c00 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65  " in the file de
e3c10 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  scriptor..**.** 
e3c20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65  The caller is re
e3c30 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f 6e  sponsible not on
e3c40 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20 74  ly for closing t
e3c50 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
e3c60 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20 66  or.** but also f
e3c70 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 6d  or freeing the m
e3c80 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
e3c90 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 64   with the file d
e3ca0 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a 73 74  escriptor..*/.st
e3cb0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72  atic int proxyCr
e3cc0 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63 6f 6e  eateUnixFile(con
e3cd0 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 75  st char *path, u
e3ce0 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 6c 65  nixFile **ppFile
e3cf0 29 20 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20  ) {.  int fd;.  
e3d00 69 6e 74 20 64 69 72 66 64 20 3d 20 2d 31 3b 0a  int dirfd = -1;.
e3d10 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77    unixFile *pNew
e3d20 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
e3d30 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65  ITE_OK;.  sqlite
e3d40 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b 0a  3_vfs dummyVfs;.
e3d50 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74  .  fd = open(pat
e3d60 68 2c 20 4f 5f 52 44 57 52 20 7c 20 4f 5f 43 52  h, O_RDWR | O_CR
e3d70 45 41 54 2c 20 53 51 4c 49 54 45 5f 44 45 46 41  EAT, SQLITE_DEFA
e3d80 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53  ULT_FILE_PERMISS
e3d90 49 4f 4e 53 29 3b 0a 20 20 69 66 28 20 66 64 3c  IONS);.  if( fd<
e3da0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
e3db0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
e3dc0 0a 20 20 7d 0a 20 20 0a 20 20 70 4e 65 77 20 3d  .  }.  .  pNew =
e3dd0 20 28 75 6e 69 78 46 69 6c 65 20 2a 29 73 71 6c   (unixFile *)sql
e3de0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
e3df0 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20  of(unixFile));. 
e3e00 20 69 66 28 20 70 4e 65 77 3d 3d 4e 55 4c 4c 20   if( pNew==NULL 
e3e10 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
e3e20 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
e3e30 74 6f 20 65 6e 64 5f 63 72 65 61 74 65 5f 70 72  to end_create_pr
e3e40 6f 78 79 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  oxy;.  }.  memse
e3e50 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f  t(pNew, 0, sizeo
e3e60 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20  f(unixFile));.. 
e3e70 20 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 44 61   dummyVfs.pAppDa
e3e80 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 75 74  ta = (void*)&aut
e3e90 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b 0a 20  olockIoFinder;. 
e3ea0 20 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78   rc = fillInUnix
e3eb0 46 69 6c 65 28 26 64 75 6d 6d 79 56 66 73 2c 20  File(&dummyVfs, 
e3ec0 66 64 2c 20 64 69 72 66 64 2c 20 28 73 71 6c 69  fd, dirfd, (sqli
e3ed0 74 65 33 5f 66 69 6c 65 2a 29 70 4e 65 77 2c 20  te3_file*)pNew, 
e3ee0 70 61 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20 69  path, 0, 0);.  i
e3ef0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
e3f00 20 29 7b 0a 20 20 20 20 2a 70 70 46 69 6c 65 20   ){.    *ppFile 
e3f10 3d 20 70 4e 65 77 3b 0a 20 20 20 20 72 65 74 75  = pNew;.    retu
e3f20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
e3f30 7d 0a 65 6e 64 5f 63 72 65 61 74 65 5f 70 72 6f  }.end_create_pro
e3f40 78 79 3a 20 20 20 20 0a 20 20 63 6c 6f 73 65 28  xy:    .  close(
e3f50 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79  fd); /* silently
e3f60 20 6c 65 61 6b 20 66 64 20 69 66 20 65 72 72 6f   leak fd if erro
e3f70 72 2c 20 77 65 27 72 65 20 61 6c 72 65 61 64 79  r, we're already
e3f80 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73   in error */.  s
e3f90 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77  qlite3_free(pNew
e3fa0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
e3fb0 7d 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20  }../* takes the 
e3fc0 63 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20  conch by taking 
e3fd0 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e  a shared lock an
e3fe0 64 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65  d read the conte
e3ff0 6e 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a  nts conch, if .*
e4000 2a 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f  * lockPath is no
e4010 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74  n-NULL, the host
e4020 20 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c   ID and lock fil
e4030 65 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63  e path must matc
e4040 68 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c  h.  A NULL .** l
e4050 6f 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68  ockPath means th
e4060 61 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20  at the lockPath 
e4070 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  in the conch fil
e4080 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69  e will be used i
e4090 66 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49  f the .** host I
e40a0 44 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e  Ds match, or a n
e40b0 65 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c  ew lock path wil
e40c0 6c 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61  l be generated a
e40d0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a  utomatically .**
e40e0 20 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20   and written to 
e40f0 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a  the conch file..
e4100 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
e4110 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69  oxyTakeConch(uni
e4120 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  xFile *pFile){. 
e4130 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
e4140 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
e4150 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
e4160 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
e4170 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20  ngContext; .  . 
e4180 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68   if( pCtx->conch
e4190 48 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65  Held>0 ){.    re
e41a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e41b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69    }else{.    uni
e41c0 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65  xFile *conchFile
e41d0 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69   = pCtx->conchFi
e41e0 6c 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73  le;.    char tes
e41f0 74 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d  tValue[CONCHLEN]
e4200 3b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68  ;.    char conch
e4210 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b  Value[CONCHLEN];
e4220 0a 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61  .    char lockPa
e4230 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a  th[MAXPATHLEN];.
e4240 20 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50      char *tLockP
e4250 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ath = NULL;.    
e4260 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
e4270 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64  OK;.    int read
e4280 52 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  Rc = SQLITE_OK;.
e4290 20 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d      int syncPerm
e42a0 73 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52  s = 0;..    OSTR
e42b0 41 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20  ACE4("TAKECONCH 
e42c0 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25   %d for %s pid=%
e42d0 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d  d\n", conchFile-
e42e0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  >h,.            
e42f0 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78   (pCtx->lockProx
e4300 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f  yPath ? pCtx->lo
e4310 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a  ckProxyPath : ":
e4320 61 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28  auto:"), getpid(
e4330 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f  ));..    rc = co
e4340 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  nchFile->pMethod
e4350 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33  ->xLock((sqlite3
e4360 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65  _file*)conchFile
e4370 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a  , SHARED_LOCK);.
e4380 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
e4390 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
e43a0 6e 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20  nt pError = 0;. 
e43b0 20 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74       memset(test
e43c0 56 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c  Value, 0, CONCHL
e43d0 45 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73  EN); /* conch is
e43e0 20 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20   fixed size */. 
e43f0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47       rc = proxyG
e4400 65 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c  etHostID(testVal
e4410 75 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20  ue, &pError);.  
e4420 20 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66      if( (rc&0xff
e4430 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  )==SQLITE_IOERR 
e4440 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
e4450 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45  ->lastErrno = pE
e4460 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rror;.      }.  
e4470 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f      if( pCtx->lo
e4480 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
e4490 20 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26         strlcpy(&
e44a0 74 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44  testValue[HOSTID
e44b0 4c 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b  LEN], pCtx->lock
e44c0 50 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41  ProxyPath, MAXPA
e44d0 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a  THLEN);.      }.
e44e0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
e44f0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
e4500 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61       goto end_ta
e4510 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20  keconch;.    }. 
e4520 20 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d     .    readRc =
e4530 20 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74   unixRead((sqlit
e4540 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46  e3_file *)conchF
e4550 69 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c  ile, conchValue,
e4560 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20   CONCHLEN, 0);. 
e4570 20 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53     if( readRc!=S
e4580 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
e4590 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20  T_READ ){.      
e45a0 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49  if( readRc!=SQLI
e45b0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
e45c0 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d   if( (rc&0xff)==
e45d0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a  SQLITE_IOERR ){.
e45e0 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
e45f0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e  >lastErrno = con
e4600 63 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e  chFile->lastErrn
e4610 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  o;.        }.   
e4620 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 63       rc = readRc
e4630 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  ;.        goto e
e4640 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20  nd_takeconch;.  
e4650 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69      }.      /* i
e4660 66 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20  f the conch has 
e4670 64 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65  data compare the
e4680 20 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20   contents */.   
e4690 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f     if( !pCtx->lo
e46a0 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
e46b0 20 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75         /* for au
e46c0 74 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c  to-named local l
e46d0 6f 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63  ock file, just c
e46e0 68 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44  heck the host ID
e46f0 20 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20   and we'll.     
e4700 20 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c      ** use the l
e4710 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70  ocal lock file p
e4720 61 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61  ath that's alrea
e4730 64 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20  dy in there */. 
e4740 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63         if( !memc
e4750 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f  mp(testValue, co
e4760 6e 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44  nchValue, HOSTID
e4770 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20  LEN) ){.        
e4780 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63    tLockPath = (c
e4790 68 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75  har *)&conchValu
e47a0 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20  e[HOSTIDLEN];.  
e47b0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
e47c0 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20  _takeconch;.    
e47d0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
e47e0 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65  e{.        /* we
e47f0 27 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63  've got the conc
e4800 68 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20  h if conchValue 
e4810 6d 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 68  matches our path
e4820 20 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a   and host ID */.
e4830 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d          if( !mem
e4840 63 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63  cmp(testValue, c
e4850 6f 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48  onchValue, CONCH
e4860 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20  LEN) ){.        
e4870 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63    goto end_takec
e4880 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a  onch;.        }.
e4890 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
e48a0 65 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68  e{.      /* a sh
e48b0 6f 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77  ort read means w
e48c0 65 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 20  e're "creating" 
e48d0 74 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20  the conch (even 
e48e0 74 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20  though it could 
e48f0 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62  .      ** have b
e4900 65 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65  een user-interve
e4910 6e 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63  ntion), if we ac
e4920 71 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 73  quire the exclus
e4930 69 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20  ive lock,.      
e4940 2a 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20  ** we'll try to 
e4950 6d 61 74 63 68 20 74 68 65 20 63 75 72 72 65 6e  match the curren
e4960 74 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73  t on-disk permis
e4970 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74  sions of the dat
e4980 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20  abase.      */. 
e4990 20 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d       syncPerms =
e49a0 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20   1;.    }.    . 
e49b0 20 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e     /* either con
e49c0 63 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20  ch was emtpy or 
e49d0 64 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a  didn't match */.
e49e0 20 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c      if( !pCtx->l
e49f0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a  ockProxyPath ){.
e4a00 20 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f        proxyGetLo
e4a10 63 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50  ckPath(pCtx->dbP
e4a20 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d  ath, lockPath, M
e4a30 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20  AXPATHLEN);.    
e4a40 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f    tLockPath = lo
e4a50 63 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74  ckPath;.      st
e4a60 72 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65  rlcpy(&testValue
e4a70 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63  [HOSTIDLEN], loc
e4a80 6b 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45  kPath, MAXPATHLE
e4a90 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20  N);.    }.    . 
e4aa0 20 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e     /* update con
e4ab0 63 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64  ch with host and
e4ac0 20 70 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c   path (this will
e4ad0 20 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70   fail if other p
e4ae0 72 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68  rocess.     ** h
e4af0 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  as a shared lock
e4b00 20 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20   already) */.   
e4b10 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d   rc = conchFile-
e4b20 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28  >pMethod->xLock(
e4b30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63  (sqlite3_file*)c
e4b40 6f 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53  onchFile, EXCLUS
e4b50 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69  IVE_LOCK);.    i
e4b60 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
e4b70 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75   ){.      rc = u
e4b80 6e 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 65  nixWrite((sqlite
e4b90 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69  3_file *)conchFi
e4ba0 6c 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43  le, testValue, C
e4bb0 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20  ONCHLEN, 0);.   
e4bc0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
e4bd0 45 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d  E_OK && syncPerm
e4be0 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72  s ){.        str
e4bf0 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20  uct stat buf;.  
e4c00 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20        int err = 
e4c10 66 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20  fstat(pFile->h, 
e4c20 26 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69  &buf);.        i
e4c30 66 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20  f( err==0 ){.   
e4c40 20 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f         /* try to
e4c50 20 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 62   match the datab
e4c60 61 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73  ase file permiss
e4c70 69 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69  ions, ignore fai
e4c80 6c 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20  lure */.#ifndef 
e4c90 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42  SQLITE_PROXY_DEB
e4ca0 55 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 68  UG.          fch
e4cb0 6d 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68  mod(conchFile->h
e4cc0 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a  , buf.st_mode);.
e4cd0 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20  #else.          
e4ce0 69 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68  if( fchmod(conch
e4cf0 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f  File->h, buf.st_
e4d00 6d 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  mode)!=0 ){.    
e4d10 20 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65          int code
e4d20 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
e4d30 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
e4d40 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f  derr, "fchmod %o
e4d50 20 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 20   FAILED with %d 
e4d60 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %s\n",.         
e4d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4d80 20 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c      buf.st_mode,
e4d90 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28   code, strerror(
e4da0 63 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20  code));.        
e4db0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
e4dc0 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73         fprintf(s
e4dd0 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25  tderr, "fchmod %
e4de0 6f 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75  o SUCCEDED\n",bu
e4df0 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20  f.st_mode);.    
e4e00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
e4e10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
e4e20 20 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e   int code = errn
e4e30 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72  o;.          fpr
e4e40 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 54  intf(stderr, "ST
e4e50 41 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69  AT FAILED[%d] wi
e4e60 74 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20  th %d %s\n", .  
e4e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4e80 20 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64          err, cod
e4e90 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65  e, strerror(code
e4ea0 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ));.#endif.     
e4eb0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
e4ec0 20 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65   }.    conchFile
e4ed0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f  ->pMethod->xUnlo
e4ee0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
e4ef0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41  *)conchFile, SHA
e4f00 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e  RED_LOCK);.  .en
e4f10 64 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20  d_takeconch:.   
e4f20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e 53   OSTRACE2("TRANS
e4f30 50 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64  PROXY: CLOSE  %d
e4f40 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a  \n", pFile->h);.
e4f50 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
e4f60 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e  TE_OK && pFile->
e4f70 6f 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20  openFlags ){.   
e4f80 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e     if( pFile->h>
e4f90 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52  =0 ){.#ifdef STR
e4fa0 49 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a  ICT_CLOSE_ERROR.
e4fb0 20 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73          if( clos
e4fc0 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20  e(pFile->h) ){. 
e4fd0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
e4fe0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
e4ff0 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  o;.          ret
e5000 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
e5010 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20  _CLOSE;.        
e5020 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20  }.#else.        
e5030 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b  close(pFile->h);
e5040 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61   /* silently lea
e5050 6b 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a  k fd if fail */.
e5060 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20  #endif.      }. 
e5070 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20       pFile->h = 
e5080 2d 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64  -1;.      int fd
e5090 20 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62   = open(pCtx->db
e50a0 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65  Path, pFile->ope
e50b0 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20  nFlags,.        
e50c0 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
e50d0 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
e50e0 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20  PERMISSIONS);.  
e50f0 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52      OSTRACE2("TR
e5100 41 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20  ANSPROXY: OPEN  
e5110 25 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20  %d\n", fd);.    
e5120 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20    if( fd>=0 ){. 
e5130 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20         pFile->h 
e5140 3d 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73  = fd;.      }els
e5150 65 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51  e{.        rc=SQ
e5160 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f  LITE_CANTOPEN; /
e5170 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70  * SQLITE_BUSY? p
e5180 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61  roxyTakeConch ca
e5190 6c 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20  lled.           
e51a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e51b0 20 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69      during locki
e51c0 6e 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  ng */.      }.  
e51d0 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
e51e0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43  SQLITE_OK && !pC
e51f0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b  tx->lockProxy ){
e5200 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 74  .      char *pat
e5210 68 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20  h = tLockPath ? 
e5220 74 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78  tLockPath : pCtx
e5230 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b  ->lockProxyPath;
e5240 0a 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e  .      /* ACS: N
e5250 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f  eed to make a co
e5260 70 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74  py of path somet
e5270 69 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63  imes */.      rc
e5280 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e   = proxyCreateUn
e5290 69 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43  ixFile(path, &pC
e52a0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a  tx->lockProxy);.
e52b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
e52c0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
e52d0 20 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68       pCtx->conch
e52e0 48 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20  Held = 1;..     
e52f0 20 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29   if( tLockPath )
e5300 7b 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e  {.        pCtx->
e5310 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20  lockProxyPath = 
e5320 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
e5330 30 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20  0, tLockPath);. 
e5340 20 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d         if( pCtx-
e5350 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74  >lockProxy->pMet
e5360 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74  hod == &afpIoMet
e5370 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20  hods ){.        
e5380 20 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f    ((afpLockingCo
e5390 6e 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f  ntext *)pCtx->lo
e53a0 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67  ckProxy->locking
e53b0 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68  Context)->dbPath
e53c0 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   =.             
e53d0 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f          pCtx->lo
e53e0 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20  ckProxyPath;.   
e53f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
e5400 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
e5410 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65    conchFile->pMe
e5420 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73  thod->xUnlock((s
e5430 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
e5440 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29  chFile, NO_LOCK)
e5450 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52  ;.    }.    OSTR
e5460 41 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20  ACE3("TAKECONCH 
e5470 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68   %d %s\n", conch
e5480 46 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c  File->h, rc==SQL
e5490 49 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69  ITE_OK?"ok":"fai
e54a0 6c 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72  led");.    retur
e54b0 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  n rc;.  }.}../*.
e54c0 2a 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64  ** If pFile hold
e54d0 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f  s a lock on a co
e54e0 6e 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72  nch file, then r
e54f0 65 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b  elease that lock
e5500 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e5510 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63  proxyReleaseConc
e5520 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  h(unixFile *pFil
e5530 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  e){.  int rc;   
e5540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e5550 20 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20    /* Subroutine 
e5560 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  return code */. 
e5570 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
e5580 74 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20  text *pCtx;  /* 
e5590 54 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  The locking cont
e55a0 65 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78  ext for the prox
e55b0 79 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78  y lock */.  unix
e55c0 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b  File *conchFile;
e55d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
e55e0 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  of the conch fil
e55f0 65 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28  e */..  pCtx = (
e5600 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
e5610 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  ext *)pFile->loc
e5620 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63  kingContext;.  c
e5630 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d  onchFile = pCtx-
e5640 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53  >conchFile;.  OS
e5650 54 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 43  TRACE4("RELEASEC
e5660 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20  ONCH  %d for %s 
e5670 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68  pid=%d\n", conch
e5680 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
e5690 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50      (pCtx->lockP
e56a0 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d  roxyPath ? pCtx-
e56b0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a  >lockProxyPath :
e56c0 20 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20   ":auto:"), .   
e56d0 20 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29          getpid()
e56e0 29 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68  );.  pCtx->conch
e56f0 48 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d  Held = 0;.  rc =
e5700 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74   conchFile->pMet
e5710 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71  hod->xUnlock((sq
e5720 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63  lite3_file*)conc
e5730 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  hFile, NO_LOCK);
e5740 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c  .  OSTRACE3("REL
e5750 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73  EASECONCH  %d %s
e5760 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  \n", conchFile->
e5770 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72  h,.           (r
e5780 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22  c==SQLITE_OK ? "
e5790 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29  ok" : "failed"))
e57a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
e57b0 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68  ../*.** Given th
e57c0 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61  e name of a data
e57d0 62 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75  base file, compu
e57e0 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69  te the name of i
e57f0 74 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a  ts conch file..*
e5800 2a 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63  * Store the conc
e5810 68 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65  h filename in me
e5820 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
e5830 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
e5840 63 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43  c()..** Make *pC
e5850 6f 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74  onchPath point t
e5860 6f 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20  o the new name. 
e5870 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
e5880 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20  K on success.** 
e5890 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  or SQLITE_NOMEM 
e58a0 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74  if unable to obt
e58b0 61 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a  ain memory..**.*
e58c0 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20  * The caller is 
e58d0 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
e58e0 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68  ensuring that th
e58f0 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  e allocated memo
e5900 72 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65  ry.** space is e
e5910 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e  ventually freed.
e5920 0a 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61  .**.** *pConchPa
e5930 74 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c  th is set to NUL
e5940 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  L if a memory al
e5950 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f  location error o
e5960 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ccurs..*/.static
e5970 20 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65   int proxyCreate
e5980 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68  ConchPathname(ch
e5990 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72  ar *dbPath, char
e59a0 20 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a   **pConchPath){.
e59b0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
e59c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e59d0 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
e59e0 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28  */.  int len = (
e59f0 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74  int)strlen(dbPat
e5a00 68 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66  h); /* Length of
e5a10 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61   database filena
e5a20 6d 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20  me - dbPath */. 
e5a30 20 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68   char *conchPath
e5a40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
e5a50 2a 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 63  * buffer in whic
e5a60 68 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63  h to construct c
e5a70 6f 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20  onch name */..  
e5a80 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  /* Allocate spac
e5a90 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20  e for the conch 
e5aa0 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69  filename and ini
e5ab0 74 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65  tialize the name
e5ac0 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d   to.  ** the nam
e5ad0 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  e of the origina
e5ae0 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  l database file.
e5af0 20 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50   */  .  *pConchP
e5b00 61 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20  ath = conchPath 
e5b10 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
e5b20 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38  3_malloc(len + 8
e5b30 29 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61  );.  if( conchPa
e5b40 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  th==0 ){.    ret
e5b50 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
e5b60 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63  ;.  }.  memcpy(c
e5b70 6f 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 68  onchPath, dbPath
e5b80 2c 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f  , len+1);.  .  /
e5b90 2a 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22  * now insert a "
e5ba0 2e 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61  ." before the la
e5bb0 73 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a  st / character *
e5bc0 2f 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d  /.  for( i=(len-
e5bd0 31 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b  1); i>=0; i-- ){
e5be0 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61  .    if( conchPa
e5bf0 74 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20  th[i]=='/' ){.  
e5c00 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62      i++;.      b
e5c10 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
e5c20 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27    conchPath[i]='
e5c30 2e 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c  .';.  while ( i<
e5c40 6c 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68  len ){.    conch
e5c50 50 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68  Path[i+1]=dbPath
e5c60 5b 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20  [i];.    i++;.  
e5c70 7d 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74  }..  /* append t
e5c80 68 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66  he "-conch" suff
e5c90 69 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a  ix to the file *
e5ca0 2f 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63  /.  memcpy(&conc
e5cb0 68 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f  hPath[i+1], "-co
e5cc0 6e 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65  nch", 7);.  asse
e5cd0 72 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  rt( (int)strlen(
e5ce0 63 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65  conchPath) == le
e5cf0 6e 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e  n+7 );..  return
e5d00 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
e5d10 2f 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79  /* Takes a fully
e5d20 20 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78   configured prox
e5d30 79 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20  y locking-style 
e5d40 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77  unix file and sw
e5d50 69 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f  itches.** the lo
e5d60 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61  cal lock file pa
e5d70 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th .*/.static in
e5d80 74 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78  t switchLockProx
e5d90 79 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a  yPath(unixFile *
e5da0 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61  pFile, const cha
e5db0 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f  r *path) {.  pro
e5dc0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
e5dd0 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c   *pCtx = (proxyL
e5de0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70  ockingContext*)p
e5df0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
e5e00 74 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c  text;.  char *ol
e5e10 64 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f  dPath = pCtx->lo
e5e20 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69  ckProxyPath;.  i
e5e30 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
e5e40 4b 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  K;..  if( pFile-
e5e50 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f  >locktype!=NO_LO
e5e60 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  CK ){.    return
e5e70 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
e5e80 7d 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e  }  ..  /* nothin
e5e90 67 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70  g to do if the p
e5ea0 61 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75  ath is NULL, :au
e5eb0 74 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74  to: or matches t
e5ec0 68 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 68  he existing path
e5ed0 20 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20   */.  if( !path 
e5ee0 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27  || path[0]=='\0'
e5ef0 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68   || !strcmp(path
e5f00 2c 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20  , ":auto:") ||. 
e5f10 20 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21     (oldPath && !
e5f20 73 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c  strncmp(oldPath,
e5f30 20 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45   path, MAXPATHLE
e5f40 4e 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  N)) ){.    retur
e5f50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
e5f60 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69  else{.    unixFi
e5f70 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20  le *lockProxy = 
e5f80 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b  pCtx->lockProxy;
e5f90 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  .    pCtx->lockP
e5fa0 72 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70  roxy=NULL;.    p
e5fb0 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d  Ctx->conchHeld =
e5fc0 20 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b   0;.    if( lock
e5fd0 50 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20  Proxy!=NULL ){. 
e5fe0 20 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78       rc=lockProx
e5ff0 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f  y->pMethod->xClo
e6000 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  se((sqlite3_file
e6010 20 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20   *)lockProxy);. 
e6020 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
e6030 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73  turn rc;.      s
e6040 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b  qlite3_free(lock
e6050 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20  Proxy);.    }.  
e6060 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f    sqlite3_free(o
e6070 6c 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74  ldPath);.    pCt
e6080 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
e6090 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
e60a0 75 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d  up(0, path);.  }
e60b0 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  .  .  return rc;
e60c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20  .}../*.** pFile 
e60d0 69 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 68  is a file that h
e60e0 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62  as been opened b
e60f0 79 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20  y a prior xOpen 
e6100 63 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a  call.  dbPath.**
e6110 20 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 66   is a string buf
e6120 66 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58  fer at least MAX
e6130 50 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63  PATHLEN+1 charac
e6140 74 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a  ters in size..**
e6150 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
e6160 20 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61   find the filena
e6170 6d 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  me associated wi
e6180 74 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69  th pFile and wri
e6190 74 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62  tes it.** int db
e61a0 50 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Path..*/.static 
e61b0 69 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 61  int proxyGetDbPa
e61c0 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e  thForUnixFile(un
e61d0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63  ixFile *pFile, c
e61e0 68 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69  har *dbPath){.#i
e61f0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
e6200 45 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65  E__).  if( pFile
e6210 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66  ->pMethod == &af
e6220 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  pIoMethods ){.  
e6230 20 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b    /* afp style k
e6240 65 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65  eeps a reference
e6250 20 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20   to the db path 
e6260 69 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20  in the filePath 
e6270 66 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66  field .    ** of
e6280 20 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20   the struct */. 
e6290 20 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29     assert( (int)
e62a0 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46  strlen((char*)pF
e62b0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
e62c0 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e  ext)<=MAXPATHLEN
e62d0 20 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64   );.    strcpy(d
e62e0 62 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b  bPath, ((afpLock
e62f0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69  ingContext *)pFi
e6300 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
e6310 78 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20  xt)->dbPath);.  
e6320 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69  }else.#endif.  i
e6330 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  f( pFile->pMetho
e6340 64 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d  d == &dotlockIoM
e6350 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a  ethods ){.    /*
e6360 20 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20   dot lock style 
e6370 75 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67  uses the locking
e6380 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72   context to stor
e6390 65 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20  e the dot lock. 
e63a0 20 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20     ** file path 
e63b0 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d  */.    int len =
e63c0 20 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29   strlen((char *)
e63d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
e63e0 6e 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28  ntext) - strlen(
e63f0 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b  DOTLOCK_SUFFIX);
e6400 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61  .    memcpy(dbPa
e6410 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c  th, (char *)pFil
e6420 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
e6430 74 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d  t, len + 1);.  }
e6440 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c  else{.    /* all
e6450 20 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 73   other styles us
e6460 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f  e the locking co
e6470 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74  ntext to store t
e6480 68 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 20  he db file path 
e6490 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  */.    assert( s
e64a0 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69  trlen((char*)pFi
e64b0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
e64c0 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20  xt)<=MAXPATHLEN 
e64d0 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62  );.    strcpy(db
e64e0 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46  Path, (char *)pF
e64f0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
e6500 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ext);.  }.  retu
e6510 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
e6520 0a 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20  ./*.** Takes an 
e6530 61 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69  already filled i
e6540 6e 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20  n unix file and 
e6550 61 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c  alters it so all
e6560 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a   file locking .*
e6570 2a 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72  * will be perfor
e6580 6d 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c  med on the local
e6590 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65   proxy lock file
e65a0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
e65b0 20 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70   fields.** are p
e65c0 72 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20  reserved in the 
e65d0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20  locking context 
e65e0 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e  so that they can
e65f0 20 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64   be restored and
e6600 20 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74   .** the unix st
e6610 72 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79  ructure properly
e6620 20 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63   cleaned up at c
e6630 6c 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d  lose time:.**  -
e6640 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a  >lockingContext.
e6650 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f  **  ->pMethod.*/
e6660 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
e6670 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69  yTransformUnixFi
e6680 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  le(unixFile *pFi
e6690 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  le, const char *
e66a0 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c  path) {.  proxyL
e66b0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
e66c0 43 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61  Ctx;.  char dbPa
e66d0 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d  th[MAXPATHLEN+1]
e66e0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ;       /* Name 
e66f0 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
e6700 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  file */.  char *
e6710 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20  lockPath=NULL;. 
e6720 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
e6730 5f 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46  _OK;.  .  if( pF
e6740 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e  ile->locktype!=N
e6750 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65  O_LOCK ){.    re
e6760 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
e6770 3b 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74  ;.  }.  proxyGet
e6780 44 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c  DbPathForUnixFil
e6790 65 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29  e(pFile, dbPath)
e67a0 3b 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c  ;.  if( !path ||
e67b0 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c   path[0]=='\0' |
e67c0 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20  | !strcmp(path, 
e67d0 22 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20  ":auto:") ){.   
e67e0 20 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a   lockPath=NULL;.
e67f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63    }else{.    loc
e6800 6b 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61  kPath=(char *)pa
e6810 74 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54  th;.  }.  .  OST
e6820 52 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58  RACE4("TRANSPROX
e6830 59 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64  Y  %d for %s pid
e6840 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
e6850 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f  ,.           (lo
e6860 63 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74  ckPath ? lockPat
e6870 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67  h : ":auto:"), g
e6880 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74  etpid());..  pCt
e6890 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  x = sqlite3_mall
e68a0 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78  oc( sizeof(*pCtx
e68b0 29 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d  ) );.  if( pCtx=
e68c0 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
e68d0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
e68e0 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78   }.  memset(pCtx
e68f0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74  , 0, sizeof(*pCt
e6900 78 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f  x));..  rc = pro
e6910 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74  xyCreateConchPat
e6920 68 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70  hname(dbPath, &p
e6930 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61  Ctx->conchFilePa
e6940 74 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  th);.  if( rc==S
e6950 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e6960 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65  rc = proxyCreate
e6970 55 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63  UnixFile(pCtx->c
e6980 6f 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70  onchFilePath, &p
e6990 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b  Ctx->conchFile);
e69a0 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d  .  }  .  if( rc=
e69b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f  =SQLITE_OK && lo
e69c0 63 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43  ckPath ){.    pC
e69d0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
e69e0 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  h = sqlite3DbStr
e69f0 44 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29  Dup(0, lockPath)
e6a00 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
e6a10 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e6a20 20 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20    /* all memory 
e6a30 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72  is allocated, pr
e6a40 6f 78 79 73 20 61 72 65 20 63 72 65 61 74 65 64  oxys are created
e6a50 20 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a   and assigned, .
e6a60 20 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68      ** switch th
e6a70 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78  e locking contex
e6a80 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68  t and pMethod th
e6a90 65 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a  en return..    *
e6aa0 2f 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61  /.    pCtx->dbPa
e6ab0 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  th = sqlite3DbSt
e6ac0 72 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b  rDup(0, dbPath);
e6ad0 0a 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f  .    pCtx->oldLo
e6ae0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70  ckingContext = p
e6af0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
e6b00 74 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d  text;.    pFile-
e6b10 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  >lockingContext 
e6b20 3d 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78  = pCtx;.    pCtx
e6b30 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70  ->pOldMethod = p
e6b40 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20  File->pMethod;. 
e6b50 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f     pFile->pMetho
e6b60 64 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68  d = &proxyIoMeth
e6b70 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ods;.  }else{.  
e6b80 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63    if( pCtx->conc
e6b90 68 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20  hFile ){ .      
e6ba0 72 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68  rc = pCtx->conch
e6bb0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  File->pMethod->x
e6bc0 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66  Close((sqlite3_f
e6bd0 69 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63  ile *)pCtx->conc
e6be0 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66  hFile);.      if
e6bf0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
e6c00 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
e6c10 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68  free(pCtx->conch
e6c20 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  File);.    }.   
e6c30 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
e6c40 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74  tx->conchFilePat
e6c50 68 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33  h); .    sqlite3
e6c60 5f 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d  _free(pCtx);.  }
e6c70 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 41  .  OSTRACE3("TRA
e6c80 4e 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e  NSPROXY  %d %s\n
e6c90 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20  ", pFile->h,.   
e6ca0 20 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c          (rc==SQL
e6cb0 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20  ITE_OK ? "ok" : 
e6cc0 22 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65  "failed"));.  re
e6cd0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
e6ce0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
e6cf0 68 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f  handles sqlite3_
e6d00 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63  file_control() c
e6d10 61 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70  alls that are sp
e6d20 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f  ecific.** to pro
e6d30 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73  xy locking..*/.s
e6d40 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46  tatic int proxyF
e6d50 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  ileControl(sqlit
e6d60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
e6d70 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29   op, void *pArg)
e6d80 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  {.  switch( op )
e6d90 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
e6da0 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_GET_LOCKPROXYF
e6db0 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69  ILE: {.      uni
e6dc0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
e6dd0 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
e6de0 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70      if( pFile->p
e6df0 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79  Method == &proxy
e6e00 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
e6e10 20 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e       proxyLockin
e6e20 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d  gContext *pCtx =
e6e30 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f   (proxyLockingCo
e6e40 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f  ntext*)pFile->lo
e6e50 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
e6e60 20 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43        proxyTakeC
e6e70 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20  onch(pFile);.   
e6e80 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c       if( pCtx->l
e6e90 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a  ockProxyPath ){.
e6ea0 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73            *(cons
e6eb0 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d  t char **)pArg =
e6ec0 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
e6ed0 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65  Path;.        }e
e6ee0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  lse{.          *
e6ef0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70  (const char **)p
e6f00 41 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e  Arg = ":auto: (n
e6f10 6f 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20  ot held)";.     
e6f20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73     }.      } els
e6f30 65 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f  e {.        *(co
e6f40 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67  nst char **)pArg
e6f50 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d   = NULL;.      }
e6f60 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
e6f70 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
e6f80 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53     case SQLITE_S
e6f90 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45  ET_LOCKPROXYFILE
e6fa0 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69  : {.      unixFi
e6fb0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
e6fc0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20  xFile*)id;.     
e6fd0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
e6fe0 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  _OK;.      int i
e6ff0 73 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70  sProxyStyle = (p
e7000 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d  File->pMethod ==
e7010 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73   &proxyIoMethods
e7020 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 72  );.      if( pAr
e7030 67 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73  g==NULL || (cons
e7040 74 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30  t char *)pArg==0
e7050 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
e7060 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a  isProxyStyle ){.
e7070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72            /* tur
e7080 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b  n off proxy lock
e7090 69 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72  ing - not suppor
e70a0 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ted */.         
e70b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
e70c0 4f 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54  OR /*SQLITE_PROT
e70d0 4f 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53  OCOL? SQLITE_MIS
e70e0 55 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20  USE?*/;.        
e70f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
e7100 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f   /* turn off pro
e7110 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72  xy locking - alr
e7120 65 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20  eady off - NOOP 
e7130 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  */.          rc 
e7140 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
e7150 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
e7160 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  se{.        cons
e7170 74 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74  t char *proxyPat
e7180 68 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  h = (const char 
e7190 2a 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20  *)pArg;.        
e71a0 69 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65  if( isProxyStyle
e71b0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72   ){.          pr
e71c0 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
e71d0 74 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20  t *pCtx = .     
e71e0 20 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63         (proxyLoc
e71f0 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69  kingContext*)pFi
e7200 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
e7210 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  xt;.          if
e7220 28 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20  ( !strcmp(pArg, 
e7230 22 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20  ":auto:") .     
e7240 20 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e        || (pCtx->
e7250 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26  lockProxyPath &&
e7260 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
e7270 21 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c  !strncmp(pCtx->l
e7280 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72  ockProxyPath, pr
e7290 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48  oxyPath, MAXPATH
e72a0 4c 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20  LEN)).          
e72b0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
e72c0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
e72d0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
e72e0 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
e72f0 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79   switchLockProxy
e7300 50 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78  Path(pFile, prox
e7310 79 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20  yPath);.        
e7320 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
e7330 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e{.          /* 
e7340 74 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69  turn on proxy fi
e7350 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20  le locking */.  
e7360 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f          rc = pro
e7370 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46  xyTransformUnixF
e7380 69 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79  ile(pFile, proxy
e7390 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d  Path);.        }
e73a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
e73b0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
e73c0 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
e73d0 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29       assert( 0 )
e73e0 3b 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61  ;  /* The call a
e73f0 73 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79  ssures that only
e7400 20 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61   valid opcodes a
e7410 72 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d  re sent */.    }
e7420 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43  .  }.  /*NOTREAC
e7430 48 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53  HED*/.  return S
e7440 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a  QLITE_ERROR;.}..
e7450 2f 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69  /*.** Within thi
e7460 73 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20  s division (the 
e7470 70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67  proxying locking
e7480 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29   implementation)
e7490 20 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a   the procedures.
e74a0 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f  ** above this po
e74b0 69 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c  int are all util
e74c0 69 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b  ities.  The lock
e74d0 2d 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73  -related methods
e74e0 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79   of the.** proxy
e74f0 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33  -locking sqlite3
e7500 5f 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63  _io_method objec
e7510 74 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f  t follow..*/.../
e7520 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
e7530 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
e7540 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
e7550 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
e7560 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
e7570 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
e7580 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
e7590 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
e75a0 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52  is held, set *pR
e75b0 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f  esOut.** to a no
e75c0 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68  n-zero value oth
e75d0 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20  erwise *pResOut 
e75e0 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20  is set to zero. 
e75f0 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
e7600 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  e.** is set to S
e7610 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20  QLITE_OK unless 
e7620 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  an I/O error occ
e7630 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20  urs during lock 
e7640 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61  checking..*/.sta
e7650 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65  tic int proxyChe
e7660 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
e7670 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
e7680 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b   int *pResOut) {
e7690 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
e76a0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
e76b0 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70  id;.  int rc = p
e76c0 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46  roxyTakeConch(pF
e76d0 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ile);.  if( rc==
e76e0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
e76f0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
e7700 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
e7710 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
e7720 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
e7730 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75  ngContext;.    u
e7740 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d  nixFile *proxy =
e7750 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
e7760 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f  ;.    return pro
e7770 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68  xy->pMethod->xCh
e7780 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
e7790 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70  (sqlite3_file*)p
e77a0 72 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a  roxy, pResOut);.
e77b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
e77c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
e77d0 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
e77e0 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
e77f0 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
e7800 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
e7810 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
e7820 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
e7830 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
e7840 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
e7850 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
e7860 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
e7870 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
e7880 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
e7890 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
e78a0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
e78b0 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
e78c0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
e78d0 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
e78e0 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
e78f0 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
e7900 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
e7910 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
e7920 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
e7930 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
e7940 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
e7950 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
e7960 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
e7970 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
e7980 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
e7990 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
e79a0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
e79b0 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
e79c0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
e79d0 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
e79e0 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
e79f0 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
e7a00 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
e7a10 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
e7a20 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
e7a30 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
e7a40 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
e7a50 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
e7a60 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
e7a70 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
e7a80 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
e7a90 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
e7aa0 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
e7ab0 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a  ite3OsUnlock().*
e7ac0 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77  * routine to low
e7ad0 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  er a locking lev
e7ae0 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  el..*/.static in
e7af0 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69  t proxyLock(sqli
e7b00 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
e7b10 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20  t locktype) {.  
e7b20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
e7b30 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
e7b40 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78  .  int rc = prox
e7b50 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65  yTakeConch(pFile
e7b60 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
e7b70 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72  ITE_OK ){.    pr
e7b80 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
e7b90 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79  t *pCtx = (proxy
e7ba0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
e7bb0 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
e7bc0 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78  ontext;.    unix
e7bd0 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43  File *proxy = pC
e7be0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20  tx->lockProxy;. 
e7bf0 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70     rc = proxy->p
e7c00 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73  Method->xLock((s
e7c10 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f  qlite3_file*)pro
e7c20 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20  xy, locktype);. 
e7c30 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
e7c40 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b  pe = proxy->lock
e7c50 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75  type;.  }.  retu
e7c60 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
e7c70 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
e7c80 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
e7c90 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
e7ca0 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20  e to locktype.  
e7cb0 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74  locktype.** must
e7cc0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
e7cd0 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
e7ce0 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
e7cf0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
e7d00 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
e7d10 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
e7d20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
e7d30 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
e7d40 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
e7d50 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
e7d60 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
e7d70 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73  nt proxyUnlock(s
e7d80 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
e7d90 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
e7da0 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
e7db0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
e7dc0 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70  id;.  int rc = p
e7dd0 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46  roxyTakeConch(pF
e7de0 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ile);.  if( rc==
e7df0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
e7e00 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
e7e10 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
e7e20 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
e7e30 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
e7e40 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75  ngContext;.    u
e7e50 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d  nixFile *proxy =
e7e60 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
e7e70 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79  ;.    rc = proxy
e7e80 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f  ->pMethod->xUnlo
e7e90 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
e7ea0 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70  *)proxy, locktyp
e7eb0 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  e);.    pFile->l
e7ec0 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d  ocktype = proxy-
e7ed0 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20  >locktype;.  }. 
e7ee0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
e7ef0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c  *.** Close a fil
e7f00 65 20 74 68 61 74 20 75 73 65 73 20 70 72 6f 78  e that uses prox
e7f10 79 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74  y locks..*/.stat
e7f20 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73  ic int proxyClos
e7f30 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
e7f40 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29  id) {.  if( id )
e7f50 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a  {.    unixFile *
e7f60 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
e7f70 65 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79  e*)id;.    proxy
e7f80 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
e7f90 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63  pCtx = (proxyLoc
e7fa0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46  kingContext *)pF
e7fb0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
e7fc0 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c  ext;.    unixFil
e7fd0 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70  e *lockProxy = p
e7fe0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
e7ff0 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f      unixFile *co
e8000 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e  nchFile = pCtx->
e8010 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69  conchFile;.    i
e8020 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
e8030 4b 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20  K;.    .    if( 
e8040 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20  lockProxy ){.   
e8050 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78     rc = lockProx
e8060 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c  y->pMethod->xUnl
e8070 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
e8080 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f  e*)lockProxy, NO
e8090 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66  _LOCK);.      if
e80a0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
e80b0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63  ;.      rc = loc
e80c0 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d  kProxy->pMethod-
e80d0 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33  >xClose((sqlite3
e80e0 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79  _file*)lockProxy
e80f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
e8100 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
e8110 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
e8120 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20  lockProxy);.    
e8130 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78    pCtx->lockProx
e8140 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  y = 0;.    }.   
e8150 20 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29   if( conchFile )
e8160 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78  {.      if( pCtx
e8170 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20  ->conchHeld ){. 
e8180 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78         rc = prox
e8190 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46  yReleaseConch(pF
e81a0 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ile);.        if
e81b0 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
e81c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
e81d0 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  rc = conchFile->
e81e0 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28  pMethod->xClose(
e81f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63  (sqlite3_file*)c
e8200 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20  onchFile);.     
e8210 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
e8220 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   rc;.      sqlit
e8230 65 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c  e3_free(conchFil
e8240 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  e);.    }.    sq
e8250 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d  lite3_free(pCtx-
e8260 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b  >lockProxyPath);
e8270 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
e8280 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  e(pCtx->conchFil
e8290 65 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69  ePath);.    sqli
e82a0 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64  te3_free(pCtx->d
e82b0 62 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72  bPath);.    /* r
e82c0 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69  estore the origi
e82d0 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  nal locking cont
e82e0 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20  ext and pMethod 
e82f0 74 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f  then close it */
e8300 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
e8310 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74  ingContext = pCt
e8320 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e  x->oldLockingCon
e8330 74 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d  text;.    pFile-
e8340 3e 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d  >pMethod = pCtx-
e8350 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20  >pOldMethod;.   
e8360 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
e8370 74 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  tx);.    return 
e8380 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e  pFile->pMethod->
e8390 78 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a  xClose(id);.  }.
e83a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e83b0 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20  OK;.}....#endif 
e83c0 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  /* defined(__APP
e83d0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
e83e0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e83f0 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68  TYLE */./*.** Th
e8400 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  e proxy locking 
e8410 73 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65  style is intende
e8420 64 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 41  d for use with A
e8430 46 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a  FP filesystems..
e8440 2a 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50  ** And since AFP
e8450 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74   is only support
e8460 65 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68  ed on MacOSX, th
e8470 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  e proxy locking 
e8480 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72  is also.** restr
e8490 69 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e  icted to MacOSX.
e84a0 0a 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  .** .**.********
e84b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
e84c0 6f 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63  of the proxy loc
e84d0 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
e84e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
e84f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
e8500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8540 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  ******/../*.** I
e8550 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70  nitialize the op
e8560 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
e8570 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
e8580 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67  This routine reg
e8590 69 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69  isters all VFS i
e85a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66  mplementations f
e85b0 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65  or unix-like ope
e85c0 72 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d  rating.** system
e85d0 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  s.  This routine
e85e0 2c 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65  , and the sqlite
e85f0 33 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69  3_os_end() routi
e8600 6e 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c  ne that follows,
e8610 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68  .** should be th
e8620 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20  e only routines 
e8630 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 61  in this file tha
e8640 74 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 72  t are visible fr
e8650 6f 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65  om other.** file
e8660 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
e8670 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
e8680 6f 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69  once during SQLi
e8690 74 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  te initializatio
e86a0 6e 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69  n and by a.** si
e86b0 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68  ngle thread.  Th
e86c0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
e86d0 69 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75  ion and mutex su
e86e0 62 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f  bsystems have no
e86f0 74 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79  t.** necessarily
e8700 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
e8710 64 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  d when this rout
e8720 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61  ine is called, a
e8730 6e 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68  nd so they.** sh
e8740 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64  ould not be used
e8750 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
e8760 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69  int sqlite3_os_i
e8770 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a  nit(void){ .  /*
e8780 20 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f   .  ** The follo
e8790 77 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e  wing macro defin
e87a0 65 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65  es an initialize
e87b0 72 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33  r for an sqlite3
e87c0 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a  _vfs object..  *
e87d0 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
e87e0 65 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20  e VFS is NAME.  
e87f0 54 68 65 20 70 41 70 70 44 61 74 61 20 69 73 20  The pAppData is 
e8800 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70  a pointer to a p
e8810 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74  ointer.  ** to t
e8820 68 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63  he "finder" func
e8830 74 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61  tion.  (pAppData
e8840 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
e8850 20 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75   a pointer becau
e8860 73 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39  se.  ** silly C9
e8870 30 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74  0 rules prohibit
e8880 20 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65   a void* from be
e8890 69 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 75  ing cast to a fu
e88a0 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20  nction pointer. 
e88b0 20 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61   ** and so we ha
e88c0 76 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68  ve to go through
e88d0 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74   the intermediat
e88e0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f  e pointer to avo
e88f0 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a  id problems.  **
e8900 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20   when compiling 
e8910 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65  with -pedantic-e
e8920 72 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20  rrors on GCC.). 
e8930 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e   **.  ** The FIN
e8940 44 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f  DER parameter to
e8950 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74   this macro is t
e8960 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70  he name of the p
e8970 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20  ointer to the.  
e8980 2a 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69  ** finder-functi
e8990 6f 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d  on.  The finder-
e89a0 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
e89b0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
e89c0 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f  e.  ** sqlite_io
e89d0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20  _methods object 
e89e0 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
e89f0 74 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b  the desired lock
e8a00 69 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f  ing.  ** behavio
e8a10 72 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 76  rs.  See the div
e8a20 69 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74  ision above that
e8a30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f   contains the IO
e8a40 4d 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63  METHODS.  ** mac
e8a50 72 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20  ro for addition 
e8a60 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66  information on f
e8a70 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e  inder-functions.
e8a80 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20  .  **.  ** Most 
e8a90 66 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72  finders simply r
e8aa0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
e8ab0 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74  to a fixed sqlit
e8ac0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20  e3_io_methods.  
e8ad0 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20  ** object.  But 
e8ae0 74 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46  the "autolockIoF
e8af0 69 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65  inder" available
e8b00 20 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20   on MacOSX does 
e8b10 61 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f  a little.  ** mo
e8b20 72 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74  re than that; it
e8b30 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69   looks at the fi
e8b40 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 68  lesystem type th
e8b50 61 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20  at hosts the .  
e8b60 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
e8b70 20 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68   and tries to ch
e8b80 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20  oose an locking 
e8b90 6d 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61  method appropria
e8ba0 74 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74  te for.  ** that
e8bb0 20 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65   filesystem time
e8bc0 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  ..  */.  #define
e8bd0 20 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45   UNIXVFS(VFSNAME
e8be0 2c 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20  , FINDER) {     
e8bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8c00 20 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20     \.    1,     
e8c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e8c20 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20  * iVersion */   
e8c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8c40 20 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e   \.    sizeof(un
e8c50 69 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20  ixFile),     /* 
e8c60 73 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20  szOsFile */     
e8c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e8c80 0a 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d  .    MAX_PATHNAM
e8c90 45 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78  E,         /* mx
e8ca0 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20  Pathname */     
e8cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e8cc0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
e8cd0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78           /* pNex
e8ce0 74 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  t */            
e8cf0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e8d00 20 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20   VFSNAME,       
e8d10 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20         /* zName 
e8d20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e8d30 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28           \.    (
e8d40 76 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20  void*)&FINDER,  
e8d50 20 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61       /* pAppData
e8d60 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e8d70 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
e8d80 78 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  xOpen,          
e8d90 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20     /* xOpen */  
e8da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8db0 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44       \.    unixD
e8dc0 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20  elete,          
e8dd0 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20   /* xDelete */  
e8de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8df0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63     \.    unixAcc
e8e00 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ess,           /
e8e10 2a 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20  * xAccess */    
e8e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8e30 20 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50   \.    unixFullP
e8e40 61 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20  athname,     /* 
e8e50 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f  xFullPathname */
e8e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e8e70 0a 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c  .    unixDlOpen,
e8e80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44             /* xD
e8e90 6c 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20  lOpen */        
e8ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e8eb0 20 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20     unixDlError, 
e8ec0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45           /* xDlE
e8ed0 72 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20  rror */         
e8ee0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e8ef0 20 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20   unixDlSym,     
e8f00 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d         /* xDlSym
e8f10 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e8f20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
e8f30 6e 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20  nixDlClose,     
e8f40 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65       /* xDlClose
e8f50 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e8f60 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
e8f70 78 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20  xRandomness,    
e8f80 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73     /* xRandomnes
e8f90 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  s */            
e8fa0 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53       \.    unixS
e8fb0 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 20  leep,           
e8fc0 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20   /* xSleep */   
e8fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8fe0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72     \.    unixCur
e8ff0 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f  rentTime,      /
e9000 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a  * xCurrentTime *
e9010 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e9020 20 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61   \.    unixGetLa
e9030 73 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20  stError      /* 
e9040 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f  xGetLastError */
e9050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e9060 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
e9070 41 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65  All default VFSe
e9080 73 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63  s for unix are c
e9090 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
e90a0 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e  following array.
e90b0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20  .  **.  ** Note 
e90c0 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33  that the sqlite3
e90d0 5f 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64  _vfs.pNext field
e90e0 20 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65   of the VFS obje
e90f0 63 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20  ct is modified. 
e9100 20 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74   ** by the SQLit
e9110 65 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20  e core when the 
e9120 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65  VFS is registere
e9130 64 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f  d.  So the follo
e9140 77 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20  wing.  ** array 
e9150 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e  cannot be const.
e9160 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73  .  */.  static s
e9170 71 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b  qlite3_vfs aVfs[
e9180 5d 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45  ] = {.#if SQLITE
e9190 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e91a0 53 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57  STYLE && (OS_VXW
e91b0 4f 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORKS || defined(
e91c0 5f 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20  __APPLE__)).    
e91d0 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20  UNIXVFS("unix", 
e91e0 20 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63           autoloc
e91f0 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c  kIoFinder ),.#el
e9200 73 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22  se.    UNIXVFS("
e9210 75 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20  unix",          
e9220 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c  posixIoFinder ),
e9230 0a 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58  .#endif.    UNIX
e9240 56 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c  VFS("unix-none",
e9250 20 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e       nolockIoFin
e9260 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56  der ),.    UNIXV
e9270 46 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65  FS("unix-dotfile
e9280 22 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e  ",  dotlockIoFin
e9290 64 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58  der ),.#if OS_VX
e92a0 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46  WORKS.    UNIXVF
e92b0 53 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d  S("unix-namedsem
e92c0 22 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29  ", semIoFinder )
e92d0 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  ,.#endif.#if SQL
e92e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e92f0 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49  NG_STYLE.    UNI
e9300 58 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78  XVFS("unix-posix
e9310 22 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e  ",    posixIoFin
e9320 64 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56  der ),.#if !OS_V
e9330 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56  XWORKS.    UNIXV
e9340 46 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c  FS("unix-flock",
e9350 20 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65      flockIoFinde
e9360 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64  r ),.#endif.#end
e9370 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  if.#if SQLITE_EN
e9380 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e9390 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  LE && defined(__
e93a0 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49  APPLE__).    UNI
e93b0 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c  XVFS("unix-afp",
e93c0 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65        afpIoFinde
e93d0 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
e93e0 28 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20  ("unix-proxy",  
e93f0 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20    proxyIoFinder 
e9400 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20  ),.#endif.  };. 
e9410 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b   unsigned int i;
e9420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
e9430 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20  p counter */..  
e9440 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20  /* Register all 
e9450 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e  VFSes defined in
e9460 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61   the aVfs[] arra
e9470 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  y */.  for(i=0; 
e9480 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f  i<(sizeof(aVfs)/
e9490 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76  sizeof(sqlite3_v
e94a0 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  fs)); i++){.    
e94b0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
e94c0 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69  ster(&aVfs[i], i
e94d0 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ==0);.  }.  retu
e94e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d  rn SQLITE_OK; .}
e94f0 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e  ../*.** Shutdown
e9500 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
e9510 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e  ystem interface.
e9520 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72  .**.** Some oper
e9530 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69  ating systems mi
e9540 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73  ght need to do s
e9550 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74  ome cleanup in t
e9560 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20  his routine,.** 
e9570 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d  to release dynam
e9580 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
e9590 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e   objects.  But n
e95a0 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54  ot on unix..** T
e95b0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
e95c0 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e   no-op for unix.
e95d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
e95e0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nt sqlite3_os_en
e95f0 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75  d(void){ .  retu
e9600 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d  rn SQLITE_OK; .}
e9610 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  . .#endif /* SQL
e9620 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a  ITE_OS_UNIX */..
e9630 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
e9640 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63  End of os_unix.c
e9650 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
e9660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
e9680 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
e9690 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69  Begin file os_wi
e96a0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
e96b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e96c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
e96d0 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
e96e0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
e96f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
e9700 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
e9710 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
e9720 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
e9730 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
e9740 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
e9750 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
e9760 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
e9770 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
e9780 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
e9790 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
e97a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
e97b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
e97c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
e97d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
e97e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
e97f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
e9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9840 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
e9850 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
e9860 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63  ode that is spec
e9870 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e  ific to windows.
e9880 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 77  .**.** $Id: os_w
e9890 69 6e 2e 63 2c 76 20 31 2e 31 35 36 20 32 30 30  in.c,v 1.156 200
e98a0 39 2f 30 34 2f 32 33 20 31 39 3a 30 38 3a 33 33  9/04/23 19:08:33
e98b0 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
e98c0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
e98d0 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
e98e0 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  /* This file is 
e98f0 75 73 65 64 20 66 6f 72 20 77 69 6e 64 6f 77 73  used for windows
e9900 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a   only */.../*.**
e9910 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65   A Note About Me
e9920 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a  mory Allocation:
e9930 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 76  .**.** This driv
e9940 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29  er uses malloc()
e9950 2f 66 72 65 65 28 29 20 64 69 72 65 63 74 6c 79  /free() directly
e9960 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 6f 69   rather than goi
e9970 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68  ng through.** th
e9980 65 20 53 51 4c 69 74 65 2d 77 72 61 70 70 65 72  e SQLite-wrapper
e9990 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  s sqlite3_malloc
e99a0 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ()/sqlite3_free(
e99b0 29 2e 20 20 54 68 6f 73 65 20 77 72 61 70 70 65  ).  Those wrappe
e99c0 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 6e  rs.** are design
e99d0 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d  ed for use on em
e99e0 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 20 77  bedded systems w
e99f0 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 73  here memory is s
e9a00 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c  carce and.** mal
e9a10 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 68 61 70  loc failures hap
e9a20 70 65 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e 20  pen frequently. 
e9a30 20 57 69 6e 33 32 20 64 6f 65 73 20 6e 6f 74 20   Win32 does not 
e9a40 74 79 70 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e  typically run on
e9a50 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 73 79 73  .** embedded sys
e9a60 74 65 6d 73 2c 20 61 6e 64 20 77 68 65 6e 20 69  tems, and when i
e9a70 74 20 64 6f 65 73 20 74 68 65 20 64 65 76 65 6c  t does the devel
e9a80 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68  opers normally h
e9a90 61 76 65 20 62 69 67 67 65 72 0a 2a 2a 20 70 72  ave bigger.** pr
e9aa0 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72 72 79 20  oblems to worry 
e9ab0 61 62 6f 75 74 20 74 68 61 6e 20 72 75 6e 6e 69  about than runni
e9ac0 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  ng out of memory
e9ad0 2e 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e  .  So there is n
e9ae0 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69  ot.** a compelli
e9af0 6e 67 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74  ng need to use t
e9b00 68 65 20 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a  he wrappers..**.
e9b10 2a 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20  ** But there is 
e9b20 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f  a good reason to
e9b30 20 6e 6f 74 20 75 73 65 20 74 68 65 20 77 72 61   not use the wra
e9b40 70 70 65 72 73 2e 20 20 49 66 20 77 65 20 75 73  ppers.  If we us
e9b50 65 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72  e the.** wrapper
e9b60 73 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67  s then we will g
e9b70 65 74 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c  et simulated mal
e9b80 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 77  loc() failures w
e9b90 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72  ithin this.** dr
e9ba0 69 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20  iver.  And that 
e9bb0 63 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73  causes all kinds
e9bc0 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72   of problems for
e9bd0 20 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0a   our tests.  We.
e9be0 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65  ** could enhance
e9bf0 20 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20   SQLite to deal 
e9c00 77 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d  with simulated m
e9c10 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77  alloc failures w
e9c20 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20  ithin.** the OS 
e9c30 64 72 69 76 65 72 2c 20 62 75 74 20 74 68 65 20  driver, but the 
e9c40 63 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69 74  code to deal wit
e9c50 68 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65 20  h those failure 
e9c60 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20  would not.** be 
e9c70 65 78 65 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e  exercised on Lin
e9c80 75 78 20 28 77 68 69 63 68 20 64 6f 65 73 20 6e  ux (which does n
e9c90 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f  ot need to mallo
e9ca0 63 28 29 20 69 6e 20 74 68 65 20 64 72 69 76 65  c() in the drive
e9cb0 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20  r).** and so we 
e9cc0 77 6f 75 6c 64 20 68 61 76 65 20 64 69 66 66 69  would have diffi
e9cd0 63 75 6c 74 79 20 77 72 69 74 69 6e 67 20 63 6f  culty writing co
e9ce0 76 65 72 61 67 65 20 74 65 73 74 73 20 66 6f 72  verage tests for
e9cf0 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20   that.** code.  
e9d00 42 65 74 74 65 72 20 74 6f 20 6c 65 61 76 65 20  Better to leave 
e9d10 74 68 65 20 63 6f 64 65 20 6f 75 74 2c 20 77 65  the code out, we
e9d20 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68   think..**.** Th
e9d30 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20  e point of this 
e9d40 64 69 73 63 75 73 73 69 6f 6e 20 69 73 20 61 73  discussion is as
e9d50 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20   follows:  When 
e9d60 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a  creating a new.*
e9d70 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f 72 20 61  * OS layer for a
e9d80 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
e9d90 6d 2c 20 69 66 20 79 6f 75 20 75 73 65 20 74 68  m, if you use th
e9da0 69 73 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78  is file as an ex
e9db0 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20  ample,.** avoid 
e9dc0 74 68 65 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f  the use of mallo
e9dd0 63 28 29 2f 66 72 65 65 28 29 2e 20 20 54 68 6f  c()/free().  Tho
e9de0 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b  se routines work
e9df0 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77 73 0a 2a   ok on windows.*
e9e00 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 74 20 6e  * desktops but n
e9e10 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e 20 65 6d  ot so well in em
e9e20 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 2e 0a  bedded systems..
e9e30 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 77 69  */..#include <wi
e9e40 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 66 64 65 66  nbase.h>..#ifdef
e9e50 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23 20 69 6e   __CYGWIN__.# in
e9e60 63 6c 75 64 65 20 3c 73 79 73 2f 63 79 67 77 69  clude <sys/cygwi
e9e70 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  n.h>.#endif../*.
e9e80 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20 74  ** Macros used t
e9e90 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74  o determine whet
e9ea0 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73  her or not to us
e9eb0 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69  e threads..*/.#i
e9ec0 66 20 64 65 66 69 6e 65 64 28 54 48 52 45 41 44  f defined(THREAD
e9ed0 53 41 46 45 29 20 26 26 20 54 48 52 45 41 44 53  SAFE) && THREADS
e9ee0 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  AFE.# define SQL
e9ef0 49 54 45 5f 57 33 32 5f 54 48 52 45 41 44 53 20  ITE_W32_THREADS 
e9f00 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  1.#endif../*.** 
e9f10 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61  Include code tha
e9f20 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61  t is common to a
e9f30 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a  ll os_*.c files.
e9f40 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
e9f50 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f  ** Include os_co
e9f60 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69  mmon.h in the mi
e9f70 64 64 6c 65 20 6f 66 20 6f 73 5f 77 69 6e 2e 63  ddle of os_win.c
e9f80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
e9f90 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
e9fa0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73  ** Begin file os
e9fb0 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
e9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9fe0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61  */./*.** 2004 Ma
e9ff0 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 22.**.** The a
ea000 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
ea010 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
ea020 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
ea030 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
ea040 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
ea050 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
ea060 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
ea070 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
ea080 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
ea090 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
ea0a0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
ea0b0 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
ea0c0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
ea0d0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
ea0e0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
ea0f0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
ea100 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
ea110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea150 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
ea160 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
ea170 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c  s macros and a l
ea180 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64  ittle bit of cod
ea190 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e  e that is common
ea1a0 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68   to.** all of th
ea1b0 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69  e platform-speci
ea1c0 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e  fic files (os_*.
ea1d0 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75  c) and is #inclu
ea1e0 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a  ded into those.*
ea1f0 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  * files..**.** T
ea200 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20  his file should 
ea210 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20  be #included by 
ea220 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73  the os_*.c files
ea230 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f   only.  It is no
ea240 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70  t a.** general p
ea250 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69  urpose header fi
ea260 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f  le..**.** $Id: o
ea270 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33  s_common.h,v 1.3
ea280 38 20 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a  8 2009/02/24 18:
ea290 34 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37  40:50 danielk197
ea2a0 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  7 Exp $.*/.#ifnd
ea2b0 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ef _OS_COMMON_H_
ea2c0 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d  .#define _OS_COM
ea2d0 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74  MON_H_../*.** At
ea2e0 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20   least two bugs 
ea2f0 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20  have slipped in 
ea300 62 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67  because we chang
ea310 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45  ed the MEMORY_DE
ea320 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20  BUG.** macro to 
ea330 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64  SQLITE_DEBUG and
ea340 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65   some older make
ea350 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79  files have not y
ea360 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73  et made the.** s
ea370 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c  witch.  The foll
ea380 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c  owing code shoul
ea390 64 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f  d catch this pro
ea3a0 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d  blem at compile-
ea3b0 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  time..*/.#ifdef 
ea3c0 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65  MEMORY_DEBUG.# e
ea3d0 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59  rror "The MEMORY
ea3e0 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20  _DEBUG macro is 
ea3f0 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53  obsolete.  Use S
ea400 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74  QLITE_DEBUG inst
ea410 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69  ead.".#endif..#i
ea420 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
ea430 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
ea440 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72   int sqlite3OSTr
ea450 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  ace = 0;.#define
ea460 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20   OSTRACE1(X)    
ea470 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
ea480 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
ea490 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a  3DebugPrintf(X).
ea4a0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
ea4b0 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20  (X,Y)       if( 
ea4c0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
ea4d0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
ea4e0 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65  ntf(X,Y).#define
ea4f0 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29   OSTRACE3(X,Y,Z)
ea500 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
ea510 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
ea520 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
ea530 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Z).#define OSTR
ea540 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20  ACE4(X,Y,Z,A)   
ea550 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
ea560 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
ea570 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29  gPrintf(X,Y,Z,A)
ea580 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
ea590 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28  5(X,Y,Z,A,B) if(
ea5a0 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
ea5b0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
ea5c0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a  intf(X,Y,Z,A,B).
ea5d0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36  #define OSTRACE6
ea5e0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a  (X,Y,Z,A,B,C) \.
ea5f0 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53      if(sqlite3OS
ea600 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65  Trace) sqlite3De
ea610 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
ea620 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f  A,B,C).#define O
ea630 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c  STRACE7(X,Y,Z,A,
ea640 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28  B,C,D) \.    if(
ea650 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20  sqlite3OSTrace) 
ea660 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
ea670 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  tf(X,Y,Z,A,B,C,D
ea680 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
ea690 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66  OSTRACE1(X).#def
ea6a0 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59  ine OSTRACE2(X,Y
ea6b0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
ea6c0 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e  E3(X,Y,Z).#defin
ea6d0 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a  e OSTRACE4(X,Y,Z
ea6e0 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,A).#define OSTR
ea6f0 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a  ACE5(X,Y,Z,A,B).
ea700 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36  #define OSTRACE6
ea710 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64  (X,Y,Z,A,B,C).#d
ea720 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58  efine OSTRACE7(X
ea730 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65  ,Y,Z,A,B,C,D).#e
ea740 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ndif../*.** Macr
ea750 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e  os for performan
ea760 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72  ce tracing.  Nor
ea770 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66  mally turned off
ea780 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a  .  Only works.**
ea790 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72   on i486 hardwar
ea7a0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
ea7b0 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f  ITE_PERFORMANCE_
ea7c0 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77  TRACE../* .** hw
ea7d0 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20  time.h contains 
ea7e0 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72  inline assembler
ea7f0 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d   code for implem
ea800 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d  enting .** high-
ea810 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69  performance timi
ea820 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  ng routines..*/.
ea830 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
ea840 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68  Include hwtime.h
ea850 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
ea860 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
ea870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
ea880 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
ea890 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d  Begin file hwtim
ea8a0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
ea8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea8c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
ea8d0 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
ea8e0 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
ea8f0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
ea900 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
ea910 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
ea920 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
ea930 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
ea940 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
ea950 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
ea960 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
ea970 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
ea980 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
ea990 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
ea9a0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
ea9b0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
ea9c0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
ea9d0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
ea9e0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
ea9f0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
eaa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa40 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
eaa50 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69   file contains i
eaa60 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66  nline asm code f
eaa70 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68  or retrieving "h
eaa80 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22  igh-performance"
eaa90 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72  .** counters for
eaaa0 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e   x86 class CPUs.
eaab0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69  .**.** $Id: hwti
eaac0 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f  me.h,v 1.3 2008/
eaad0 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73  08/01 14:33:15 s
eaae0 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69  hane Exp $.*/.#i
eaaf0 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f  fndef _HWTIME_H_
eab00 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45  .#define _HWTIME
eab10 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  _H_../*.** The f
eab20 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
eab30 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70   only works on p
eab40 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72  entium-class (or
eab50 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f   newer) processo
eab60 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74  rs..** It uses t
eab70 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20  he RDTSC opcode 
eab80 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c  to read the cycl
eab90 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75  e count value ou
eaba0 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63  t of the.** proc
eabb0 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e  essor and return
eabc0 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54  s that value.  T
eabd0 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
eabe0 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20  for high-res.** 
eabf0 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69  profiling..*/.#i
eac00 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  f (defined(__GNU
eac10 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  C__) || defined(
eac20 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a  _MSC_VER)) && \.
eac30 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69        (defined(i
eac40 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  386) || defined(
eac50 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66  __i386__) || def
eac60 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a  ined(_M_IX86))..
eac70 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f    #if defined(__
eac80 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c  GNUC__)..  __inl
eac90 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
eaca0 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
eacb0 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e  e(void){.     un
eacc0 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68  signed int lo, h
eacd0 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  i;.     __asm__ 
eace0 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72  __volatile__ ("r
eacf0 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f  dtsc" : "=a" (lo
ead00 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20  ), "=d" (hi));. 
ead10 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69      return (sqli
ead20 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20  te_uint64)hi << 
ead30 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20  32 | lo;.  }..  
ead40 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d  #elif defined(_M
ead50 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63  SC_VER)..  __dec
ead60 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69  lspec(naked) __i
ead70 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e  nline sqlite_uin
ead80 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69  t64 __cdecl sqli
ead90 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
eada0 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20  .     __asm {.  
eadb0 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20        rdtsc.    
eadc0 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20      ret       ; 
eadd0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20  return value at 
eade0 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20  EDX:EAX.     }. 
eadf0 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65   }..  #endif..#e
eae00 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  lif (defined(__G
eae10 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65  NUC__) && define
eae20 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a  d(__x86_64__))..
eae30 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
eae40 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
eae50 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
eae60 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c        unsigned l
eae70 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f  ong val;.      _
eae80 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
eae90 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22  e__ ("rdtsc" : "
eaea0 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20  =A" (val));.    
eaeb0 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20    return val;.  
eaec0 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  }. .#elif (defin
eaed0 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
eaee0 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29  defined(__ppc__)
eaef0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  )..  __inline__ 
eaf00 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
eaf10 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
eaf20 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
eaf30 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76  d long long retv
eaf40 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  al;.      unsign
eaf50 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20  ed long junk;.  
eaf60 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
eaf70 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20  latile__ ("\n\. 
eaf80 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20           1:     
eaf90 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20   mftbu   %1\n\. 
eafa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eafb0 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a   mftb    %L0\n\.
eafc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eafd0 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a    mftbu   %0\n\.
eafe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eaff0 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c    cmpw    %0,%1\
eb000 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
eb010 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22       bne     1b"
eb020 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
eb030 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61     : "=r" (retva
eb040 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29  l), "=r" (junk))
eb050 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
eb060 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73  etval;.  }..#els
eb070 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64  e..  #error Need
eb080 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
eb090 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  of sqlite3Hwtime
eb0a0 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
eb0b0 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  form...  /*.  **
eb0c0 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68   To compile with
eb0d0 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  out implementing
eb0e0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
eb0f0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f   for your platfo
eb100 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e  rm,.  ** you can
eb110 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76   remove the abov
eb120 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65  e #error and use
eb130 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
eb140 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f   ** stub functio
eb150 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73  n.  You will los
eb160 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74  e timing support
eb170 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f   for many.  ** o
eb180 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20  f the debugging 
eb190 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c  and testing util
eb1a0 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68  ities, but it sh
eb1b0 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61  ould at.  ** lea
eb1c0 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72  st compile and r
eb1d0 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f  un..  */.SQLITE_
eb1e0 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65  PRIVATE   sqlite
eb1f0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
eb200 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74  wtime(void){ ret
eb210 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e  urn ((sqlite_uin
eb220 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69  t64)0); }..#endi
eb230 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
eb240 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f  fined(_HWTIME_H_
eb250 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
eb260 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74  ***** End of hwt
eb270 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
eb280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb2a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
eb2b0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
eb2c0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
eb2d0 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  ff in os_common.
eb2e0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
eb2f0 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71  ****/..static sq
eb300 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74  lite_uint64 g_st
eb310 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69  art;.static sqli
eb320 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70  te_uint64 g_elap
eb330 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d  sed;.#define TIM
eb340 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67  ER_START       g
eb350 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77  _start=sqlite3Hw
eb360 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54  time().#define T
eb370 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20  IMER_END        
eb380 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74   g_elapsed=sqlit
eb390 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61  e3Hwtime()-g_sta
eb3a0 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  rt.#define TIMER
eb3b0 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65  _ELAPSED     g_e
eb3c0 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65  lapsed.#else.#de
eb3d0 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54  fine TIMER_START
eb3e0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
eb3f0 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  ND.#define TIMER
eb400 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73  _ELAPSED     ((s
eb410 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a  qlite_uint64)0).
eb420 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
eb430 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68   we compile with
eb440 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54   the SQLITE_TEST
eb450 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e   macro set, then
eb460 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   the following b
eb470 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20  lock.** of code 
eb480 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65  will give us the
eb490 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75   ability to simu
eb4a0 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20  late a disk I/O 
eb4b0 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20  error.  This.** 
eb4c0 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
eb4d0 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f  ing the I/O reco
eb4e0 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23  very logic..*/.#
eb4f0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
eb500 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
eb510 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
eb520 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20  r_hit = 0;      
eb530 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
eb540 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72  umber of I/O Err
eb550 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ors */.SQLITE_AP
eb560 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
eb570 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d  _error_hardhit =
eb580 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75   0;        /* Nu
eb590 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69  mber of non-beni
eb5a0 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c  gn errors */.SQL
eb5b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
eb5c0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
eb5d0 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20  ding = 0;       
eb5e0 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74   /* Count down t
eb5f0 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f  o first I/O erro
eb600 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  r */.SQLITE_API 
eb610 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
eb620 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30  rror_persist = 0
eb630 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
eb640 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70   if I/O errors p
eb650 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45  ersist */.SQLITE
eb660 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
eb670 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e  _io_error_benign
eb680 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
eb690 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20   True if errors 
eb6a0 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51  are benign */.SQ
eb6b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
eb6c0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
eb6d0 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54  nding = 0;.SQLIT
eb6e0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
eb6f0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a  3_diskfull = 0;.
eb700 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
eb710 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29  IOErrorBenign(X)
eb720 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
eb730 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65  r_benign=(X).#de
eb740 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
eb750 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20  rror(CODE)  \.  
eb760 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f  if( (sqlite3_io_
eb770 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26  error_persist &&
eb780 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
eb790 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20  r_hit) \.       
eb7a0 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  || sqlite3_io_er
eb7b0 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d  ror_pending-- ==
eb7c0 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20   1 )  \.        
eb7d0 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f        { local_io
eb7e0 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73  err(); CODE; }.s
eb7f0 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c  tatic void local
eb800 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52  _ioerr(){.  IOTR
eb810 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29  ACE(("IOERR\n"))
eb820 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  ;.  sqlite3_io_e
eb830 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66  rror_hit++;.  if
eb840 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  ( !sqlite3_io_er
eb850 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c  ror_benign ) sql
eb860 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61  ite3_io_error_ha
eb870 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69  rdhit++;.}.#defi
eb880 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  ne SimulateDiskf
eb890 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c  ullError(CODE) \
eb8a0 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  .   if( sqlite3_
eb8b0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
eb8c0 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73   ){ \.     if( s
eb8d0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
eb8e0 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20  pending == 1 ){ 
eb8f0 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69  \.       local_i
eb900 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20  oerr(); \.      
eb910 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
eb920 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20  l = 1; \.       
eb930 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
eb940 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20  _hit = 1; \.    
eb950 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20     CODE; \.     
eb960 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20  }else{ \.       
eb970 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
eb980 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20  _pending--; \.  
eb990 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73     } \.   }.#els
eb9a0 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  e.#define Simula
eb9b0 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
eb9c0 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  X).#define Simul
eb9d0 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64  ateIOError(A).#d
eb9e0 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69  efine SimulateDi
eb9f0 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23  skfullError(A).#
eba00 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  endif../*.** Whe
eba10 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20  n testing, keep 
eba20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e  a count of the n
eba30 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69  umber of open fi
eba40 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  les..*/.#ifdef S
eba50 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
eba60 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
eba70 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e  3_open_file_coun
eba80 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f  t = 0;.#define O
eba90 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73  penCounter(X)  s
ebaa0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65  qlite3_open_file
ebab0 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73  _count+=(X).#els
ebac0 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f  e.#define OpenCo
ebad0 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a  unter(X).#endif.
ebae0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
ebaf0 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ned(_OS_COMMON_H
ebb00 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _) */../********
ebb10 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
ebb20 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
ebb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebb50 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
ebb60 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
ebb70 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
ebb80 6f 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e 63 20  off in os_win.c 
ebb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ebba0 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 6f  *****/../*.** So
ebbb0 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d  me microsoft com
ebbc0 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73  pilers lack this
ebbd0 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a   definition..*/.
ebbe0 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f  #ifndef INVALID_
ebbf0 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 0a  FILE_ATTRIBUTES.
ebc00 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 44  # define INVALID
ebc10 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53  _FILE_ATTRIBUTES
ebc20 20 28 28 44 57 4f 52 44 29 2d 31 29 20 0a 23 65   ((DWORD)-1) .#e
ebc30 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65  ndif../*.** Dete
ebc40 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20  rmine if we are 
ebc50 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e  dealing with Win
ebc60 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68  dowsCE - which h
ebc70 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64  as a much.** red
ebc80 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66  uced API..*/.#if
ebc90 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ebca0 0a 23 20 64 65 66 69 6e 65 20 41 72 65 46 69 6c  .# define AreFil
ebcb0 65 41 70 69 73 41 4e 53 49 28 29 20 31 0a 23 20  eApisANSI() 1.# 
ebcc0 64 65 66 69 6e 65 20 47 65 74 44 69 73 6b 46 72  define GetDiskFr
ebcd0 65 65 53 70 61 63 65 57 28 29 20 30 0a 23 65 6e  eeSpaceW() 0.#en
ebce0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45  dif../*.** WinCE
ebcf0 20 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75   lacks native su
ebd00 70 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c  pport for file l
ebd10 6f 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76  ocking so we hav
ebd20 65 20 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20  e to fake it.** 
ebd30 77 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f  with some code o
ebd40 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69  f our own..*/.#i
ebd50 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
ebd60 45 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  E.typedef struct
ebd70 20 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69   winceLock {.  i
ebd80 6e 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20  nt nReaders;    
ebd90 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
ebda0 72 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74  reader locks obt
ebdb0 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20  ained */.  BOOL 
ebdc0 62 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f  bPending;      /
ebdd0 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65  * Indicates a pe
ebde0 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62  nding lock has b
ebdf0 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a  een obtained */.
ebe00 20 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64    BOOL bReserved
ebe10 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74  ;     /* Indicat
ebe20 65 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f  es a reserved lo
ebe30 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61  ck has been obta
ebe40 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62  ined */.  BOOL b
ebe50 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a  Exclusive;    /*
ebe60 20 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78   Indicates an ex
ebe70 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73  clusive lock has
ebe80 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a   been obtained *
ebe90 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23  /.} winceLock;.#
ebea0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
ebeb0 20 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75   winFile structu
ebec0 72 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73  re is a subclass
ebed0 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
ebee0 2a 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68  * specific to th
ebef0 65 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61  e win32.** porta
ebf00 62 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f  bility layer..*/
ebf10 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
ebf20 77 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b  winFile winFile;
ebf30 0a 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20  .struct winFile 
ebf40 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  {.  const sqlite
ebf50 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
ebf60 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65  ethod;/* Must be
ebf70 20 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44   first */.  HAND
ebf80 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20  LE h;           
ebf90 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f      /* Handle fo
ebfa0 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20  r accessing the 
ebfb0 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  file */.  unsign
ebfc0 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65  ed char locktype
ebfd0 3b 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63  ; /* Type of loc
ebfe0 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64  k currently held
ebff0 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   on this file */
ec000 0a 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c  .  short sharedL
ec010 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61  ockByte;   /* Ra
ec020 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79  ndomly chosen by
ec030 74 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61  te used as a sha
ec040 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 44 57  red lock */.  DW
ec050 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20  ORD lastErrno;  
ec060 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e        /* The Win
ec070 64 6f 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20  dows errno from 
ec080 74 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72  the last I/O err
ec090 6f 72 20 2a 2f 0a 20 20 44 57 4f 52 44 20 73 65  or */.  DWORD se
ec0a0 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20  ctorSize;       
ec0b0 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 6f  /* Sector size o
ec0c0 66 20 74 68 65 20 64 65 76 69 63 65 20 66 69 6c  f the device fil
ec0d0 65 20 69 73 20 6f 6e 20 2a 2f 0a 23 69 66 20 53  e is on */.#if S
ec0e0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
ec0f0 20 57 43 48 41 52 20 2a 7a 44 65 6c 65 74 65 4f   WCHAR *zDeleteO
ec100 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e 61 6d 65  nClose;  /* Name
ec110 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65   of file to dele
ec120 74 65 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20  te when closing 
ec130 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 68 4d 75 74  */.  HANDLE hMut
ec140 65 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ex;          /* 
ec150 4d 75 74 65 78 20 75 73 65 64 20 74 6f 20 63 6f  Mutex used to co
ec160 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20  ntrol access to 
ec170 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 20 20  shared lock */  
ec180 0a 20 20 48 41 4e 44 4c 45 20 68 53 68 61 72 65  .  HANDLE hShare
ec190 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68  d;         /* Sh
ec1a0 61 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d  ared memory segm
ec1b0 65 6e 74 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  ent used for loc
ec1c0 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4c  king */.  winceL
ec1d0 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 20 20 20 20  ock local;      
ec1e0 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f 62 74 61 69    /* Locks obtai
ec1f0 6e 65 64 20 62 79 20 74 68 69 73 20 69 6e 73 74  ned by this inst
ec200 61 6e 63 65 20 6f 66 20 77 69 6e 46 69 6c 65 20  ance of winFile 
ec210 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 2a  */.  winceLock *
ec220 73 68 61 72 65 64 3b 20 20 20 20 20 20 2f 2a 20  shared;      /* 
ec230 47 6c 6f 62 61 6c 20 73 68 61 72 65 64 20 6c 6f  Global shared lo
ec240 63 6b 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68  ck memory for th
ec250 65 20 66 69 6c 65 20 20 2a 2f 0a 23 65 6e 64 69  e file  */.#endi
ec260 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77  f.};../*.** Forw
ec270 61 72 64 20 70 72 6f 74 6f 74 79 70 65 73 2e 0a  ard prototypes..
ec280 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
ec290 74 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20  tSectorSize(.   
ec2a0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
ec2b0 66 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  fs,.    const ch
ec2c0 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20  ar *zRelative   
ec2d0 20 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20    /* UTF-8 file 
ec2e0 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  name */.);../*.*
ec2f0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
ec300 76 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72  variable is (nor
ec310 6d 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20  mally) set once 
ec320 61 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65  and never change
ec330 73 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e  s.** thereafter.
ec340 20 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65    It records whe
ec350 74 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69  ther the operati
ec360 6e 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e  ng system is Win
ec370 39 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a  95.** or WinNT..
ec380 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61  **.** 0:   Opera
ec390 74 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e  ting system unkn
ec3a0 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65  own..** 1:   Ope
ec3b0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73  rating system is
ec3c0 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20   Win95..** 2:   
ec3d0 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  Operating system
ec3e0 20 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a   is WinNT..**.**
ec3f0 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63   In order to fac
ec400 69 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20  ilitate testing 
ec410 6f 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65  on a WinNT syste
ec420 6d 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74  m, the test fixt
ec430 75 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61  ure.** can manua
ec440 6c 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c  lly set this val
ec450 75 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61  ue to 1 to emula
ec460 74 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f  te Win98 behavio
ec470 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  r..*/.#ifdef SQL
ec480 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
ec490 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
ec4a0 6f 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c  os_type = 0;.#el
ec4b0 73 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  se.static int sq
ec4c0 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20  lite3_os_type = 
ec4d0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
ec4e0 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f   Return true (no
ec4f0 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72  n-zero) if we ar
ec500 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20  e running under 
ec510 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69  WinNT, Win2K, Wi
ec520 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45  nXP,.** or WinCE
ec530 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20  .  Return false 
ec540 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35  (zero) for Win95
ec550 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d  , Win98, or WinM
ec560 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73  E..**.** Here is
ec570 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20   an interesting 
ec580 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69  observation:  Wi
ec590 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20  n95, Win98, and 
ec5a0 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68  WinME lack.** th
ec5b0 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41  e LockFileEx() A
ec5c0 50 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20  PI.  But we can 
ec5d0 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79  still statically
ec5e0 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68   link against th
ec5f0 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e  at.** API as lon
ec600 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61  g as we don't ca
ec610 6c 6c 20 69 74 20 77 68 65 6e 20 72 75 6e 6e 69  ll it when runni
ec620 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20  ng Win95/98/ME. 
ec630 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68   A call to.** th
ec640 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
ec650 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
ec660 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57  if the host is W
ec670 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a  in95/98/ME or.**
ec680 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20   WinNT/2K/XP so 
ec690 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f  that we will kno
ec6a0 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  w whether or not
ec6b0 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63   we can safely c
ec6c0 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46  all.** the LockF
ec6d0 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a  ileEx() API..*/.
ec6e0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
ec6f0 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e  NCE.# define isN
ec700 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20  T()  (1).#else. 
ec710 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54   static int isNT
ec720 28 76 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20  (void){.    if( 
ec730 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d  sqlite3_os_type=
ec740 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45  =0 ){.      OSVE
ec750 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b  RSIONINFO sInfo;
ec760 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f  .      sInfo.dwO
ec770 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65  SVersionInfoSize
ec780 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29   = sizeof(sInfo)
ec790 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69  ;.      GetVersi
ec7a0 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20  onEx(&sInfo);.  
ec7b0 20 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74      sqlite3_os_t
ec7c0 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c  ype = sInfo.dwPl
ec7d0 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c  atformId==VER_PL
ec7e0 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20  ATFORM_WIN32_NT 
ec7f0 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20  ? 2 : 1;.    }. 
ec800 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
ec810 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20  3_os_type==2;.  
ec820 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
ec830 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a  TE_OS_WINCE */..
ec840 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
ec850 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20  UTF-8 string to 
ec860 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64  microsoft unicod
ec870 65 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a  e (UTF-16?). .**
ec880 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
ec890 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  d the returned s
ec8a0 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
ec8b0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a  d from malloc..*
ec8c0 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a  /.static WCHAR *
ec8d0 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f  utf8ToUnicode(co
ec8e0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
ec8f0 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61  ame){.  int nCha
ec900 72 3b 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64  r;.  WCHAR *zWid
ec910 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43  eFilename;..  nC
ec920 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54  har = MultiByteT
ec930 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46  oWideChar(CP_UTF
ec940 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  8, 0, zFilename,
ec950 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20   -1, NULL, 0);. 
ec960 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d   zWideFilename =
ec970 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73   malloc( nChar*s
ec980 69 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e  izeof(zWideFilen
ec990 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  ame[0]) );.  if(
ec9a0 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d   zWideFilename==
ec9b0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
ec9c0 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d  0;.  }.  nChar =
ec9d0 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65   MultiByteToWide
ec9e0 43 68 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c  Char(CP_UTF8, 0,
ec9f0 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20   zFilename, -1, 
eca00 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e  zWideFilename, n
eca10 43 68 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68  Char);.  if( nCh
eca20 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65  ar==0 ){.    fre
eca30 65 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29  e(zWideFilename)
eca40 3b 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e  ;.    zWideFilen
eca50 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ame = 0;.  }.  r
eca60 65 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e  eturn zWideFilen
eca70 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ame;.}../*.** Co
eca80 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20  nvert microsoft 
eca90 75 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38  unicode to UTF-8
ecaa0 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
ecab0 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
ecac0 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69  ring is.** obtai
ecad0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28  ned from malloc(
ecae0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  )..*/.static cha
ecaf0 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38  r *unicodeToUtf8
ecb00 28 63 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57  (const WCHAR *zW
ecb10 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20  ideFilename){.  
ecb20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61  int nByte;.  cha
ecb30 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20  r *zFilename;.. 
ecb40 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61   nByte = WideCha
ecb50 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f  rToMultiByte(CP_
ecb60 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69  UTF8, 0, zWideFi
ecb70 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30  lename, -1, 0, 0
ecb80 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65  , 0, 0);.  zFile
ecb90 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  name = malloc( n
ecba0 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46  Byte );.  if( zF
ecbb0 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ilename==0 ){.  
ecbc0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
ecbd0 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68    nByte = WideCh
ecbe0 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50  arToMultiByte(CP
ecbf0 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46  _UTF8, 0, zWideF
ecc00 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69  ilename, -1, zFi
ecc10 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20  lename, nByte,. 
ecc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ecc30 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
ecc40 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20  0);.  if( nByte 
ecc50 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65  == 0 ){.    free
ecc60 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20  (zFilename);.   
ecc70 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a   zFilename = 0;.
ecc80 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69    }.  return zFi
ecc90 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  lename;.}../*.**
ecca0 20 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69   Convert an ansi
eccb0 20 73 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f   string to micro
eccc0 73 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61  soft unicode, ba
eccd0 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75  sed on the.** cu
ecce0 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73  rrent codepage s
eccf0 65 74 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65  ettings for file
ecd00 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70   apis..** .** Sp
ecd10 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
ecd20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
ecd30 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  is obtained.** f
ecd40 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73  rom malloc..*/.s
ecd50 74 61 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63  tatic WCHAR *mbc
ecd60 73 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74  sToUnicode(const
ecd70 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
ecd80 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a  ){.  int nByte;.
ecd90 20 20 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69    WCHAR *zMbcsFi
ecda0 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f  lename;.  int co
ecdb0 64 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65  depage = AreFile
ecdc0 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f  ApisANSI() ? CP_
ecdd0 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a  ACP : CP_OEMCP;.
ecde0 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69  .  nByte = Multi
ecdf0 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63  ByteToWideChar(c
ece00 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c  odepage, 0, zFil
ece10 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c  ename, -1, NULL,
ece20 30 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29  0)*sizeof(WCHAR)
ece30 3b 0a 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d  ;.  zMbcsFilenam
ece40 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74  e = malloc( nByt
ece50 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69  e*sizeof(zMbcsFi
ece60 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20  lename[0]) );.  
ece70 69 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d  if( zMbcsFilenam
ece80 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
ece90 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74  rn 0;.  }.  nByt
ecea0 65 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57  e = MultiByteToW
eceb0 69 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65  ideChar(codepage
ecec0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
eced0 2d 31 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d  -1, zMbcsFilenam
ecee0 65 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28  e, nByte);.  if(
ecef0 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20   nByte==0 ){.   
ecf00 20 66 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e   free(zMbcsFilen
ecf10 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46  ame);.    zMbcsF
ecf20 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
ecf30 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46  .  return zMbcsF
ecf40 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ilename;.}../*.*
ecf50 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73  * Convert micros
ecf60 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d  oft unicode to m
ecf70 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74  ultibyte charact
ecf80 65 72 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64  er string, based
ecf90 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27   on the.** user'
ecfa0 73 20 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e  s Ansi codepage.
ecfb0 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20  .**.** Space to 
ecfc0 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
ecfd0 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61  d string is obta
ecfe0 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
ecff0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
ed000 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f   char *unicodeTo
ed010 4d 62 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52  Mbcs(const WCHAR
ed020 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29   *zWideFilename)
ed030 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  {.  int nByte;. 
ed040 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
ed050 3b 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65  ;.  int codepage
ed060 20 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e   = AreFileApisAN
ed070 53 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20  SI() ? CP_ACP : 
ed080 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79  CP_OEMCP;..  nBy
ed090 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d  te = WideCharToM
ed0a0 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67  ultiByte(codepag
ed0b0 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e  e, 0, zWideFilen
ed0c0 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30  ame, -1, 0, 0, 0
ed0d0 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d  , 0);.  zFilenam
ed0e0 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74  e = malloc( nByt
ed0f0 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65  e );.  if( zFile
ed100 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  name==0 ){.    r
ed110 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
ed120 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54  Byte = WideCharT
ed130 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70  oMultiByte(codep
ed140 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c  age, 0, zWideFil
ed150 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65  ename, -1, zFile
ed160 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20  name, nByte,.   
ed170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ed180 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29             0, 0)
ed190 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d  ;.  if( nByte ==
ed1a0 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a   0 ){.    free(z
ed1b0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a  Filename);.    z
ed1c0 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20  Filename = 0;.  
ed1d0 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65  }.  return zFile
ed1e0 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  name;.}../*.** C
ed1f0 6f 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65  onvert multibyte
ed200 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e   character strin
ed210 67 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61  g to UTF-8.  Spa
ed220 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a  ce to hold the.*
ed230 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  * returned strin
ed240 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  g is obtained fr
ed250 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  om malloc()..*/.
ed260 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
ed270 2a 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d  *sqlite3_win32_m
ed280 62 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73  bcs_to_utf8(cons
ed290 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
ed2a0 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  e){.  char *zFil
ed2b0 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48  enameUtf8;.  WCH
ed2c0 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20  AR *zTmpWide;.. 
ed2d0 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73   zTmpWide = mbcs
ed2e0 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e  ToUnicode(zFilen
ed2f0 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70  ame);.  if( zTmp
ed300 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Wide==0 ){.    r
ed310 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a  eturn 0;.  }.  z
ed320 46 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75  FilenameUtf8 = u
ed330 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d  nicodeToUtf8(zTm
ed340 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a  pWide);.  free(z
ed350 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75  TmpWide);.  retu
ed360 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38  rn zFilenameUtf8
ed370 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
ed380 72 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74  rt UTF-8 to mult
ed390 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20  ibyte character 
ed3a0 73 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74  string.  Space t
ed3b0 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72  o hold the .** r
ed3c0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
ed3d0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
ed3e0 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61  malloc()..*/.sta
ed3f0 74 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f  tic char *utf8To
ed400 4d 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20  Mbcs(const char 
ed410 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63  *zFilename){.  c
ed420 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62  har *zFilenameMb
ed430 63 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d  cs;.  WCHAR *zTm
ed440 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69  pWide;..  zTmpWi
ed450 64 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f  de = utf8ToUnico
ed460 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  de(zFilename);. 
ed470 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30   if( zTmpWide==0
ed480 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
ed490 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d  ;.  }.  zFilenam
ed4a0 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54  eMbcs = unicodeT
ed4b0 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b  oMbcs(zTmpWide);
ed4c0 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65  .  free(zTmpWide
ed4d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c  );.  return zFil
ed4e0 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69  enameMbcs;.}..#i
ed4f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
ed500 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  E./*************
ed510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ed540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
ed550 54 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e  This section con
ed560 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57  tains code for W
ed570 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a  inCE only..*/./*
ed580 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f  .** WindowsCE do
ed590 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f  es not have a lo
ed5a0 63 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69  caltime() functi
ed5b0 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61  on.  So create a
ed5c0 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a  .** substitute..
ed5d0 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f  */.struct tm *__
ed5e0 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28  cdecl localtime(
ed5f0 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29  const time_t *t)
ed600 0a 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75  .{.  static stru
ed610 63 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54  ct tm y;.  FILET
ed620 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20  IME uTm, lTm;.  
ed630 53 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a  SYSTEMTIME pTm;.
ed640 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
ed650 74 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b  t64;.  t64 = *t;
ed660 0a 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20  .  t64 = (t64 + 
ed670 31 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 30  11644473600)*100
ed680 30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c  00000;.  uTm.dwL
ed690 6f 77 44 61 74 65 54 69 6d 65 20 3d 20 74 36 34  owDateTime = t64
ed6a0 20 26 20 30 78 46 46 46 46 46 46 46 46 3b 0a 20   & 0xFFFFFFFF;. 
ed6b0 20 75 54 6d 2e 64 77 48 69 67 68 44 61 74 65 54   uTm.dwHighDateT
ed6c0 69 6d 65 3d 20 74 36 34 20 3e 3e 20 33 32 3b 0a  ime= t64 >> 32;.
ed6d0 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 61    FileTimeToLoca
ed6e0 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c 26  lFileTime(&uTm,&
ed6f0 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65  lTm);.  FileTime
ed700 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c 54  ToSystemTime(&lT
ed710 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d 5f  m,&pTm);.  y.tm_
ed720 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 72  year = pTm.wYear
ed730 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d 5f   - 1900;.  y.tm_
ed740 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 68  mon = pTm.wMonth
ed750 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 61   - 1;.  y.tm_wda
ed760 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 65  y = pTm.wDayOfWe
ed770 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 20  ek;.  y.tm_mday 
ed780 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 2e  = pTm.wDay;.  y.
ed790 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 48  tm_hour = pTm.wH
ed7a0 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e 20  our;.  y.tm_min 
ed7b0 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a 20  = pTm.wMinute;. 
ed7c0 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d 2e   y.tm_sec = pTm.
ed7d0 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 72  wSecond;.  retur
ed7e0 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73  n &y;.}../* This
ed7f0 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 63   will never be c
ed800 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 69 6e  alled, but defin
ed810 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63  ed to make the c
ed820 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a 23  ode compile */.#
ed830 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 50 61  define GetTempPa
ed840 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 69 6e  thA(a,b)..#defin
ed850 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63  e LockFile(a,b,c
ed860 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 6e 63  ,d,e)       winc
ed870 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c  eLockFile(&a, b,
ed880 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 6e   c, d, e).#defin
ed890 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c 62  e UnlockFile(a,b
ed8a0 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 6e 63  ,c,d,e)     winc
ed8b0 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c 20  eUnlockFile(&a, 
ed8c0 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66  b, c, d, e).#def
ed8d0 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 61  ine LockFileEx(a
ed8e0 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 69  ,b,c,d,e,f)   wi
ed8f0 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 61  nceLockFileEx(&a
ed900 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 29  , b, c, d, e, f)
ed910 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 4c 45  ..#define HANDLE
ed920 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20 28  _TO_WINFILE(a) (
ed930 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 61 72  winFile*)&((char
ed940 2a 29 61 29 5b 2d 6f 66 66 73 65 74 6f 66 28 77  *)a)[-offsetof(w
ed950 69 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a  inFile,h)]../*.*
ed960 2a 20 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b  * Acquire a lock
ed970 20 6f 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68   on the handle h
ed980 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
ed990 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72  winceMutexAcquir
ed9a0 65 28 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20  e(HANDLE h){.   
ed9b0 44 57 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20  DWORD dwErr;.   
ed9c0 64 6f 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20  do {.     dwErr 
ed9d0 3d 20 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f  = WaitForSingleO
ed9e0 62 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54  bject(h, INFINIT
ed9f0 45 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28  E);.   } while (
eda00 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42  dwErr != WAIT_OB
eda10 4a 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 20  JECT_0 && dwErr 
eda20 21 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45  != WAIT_ABANDONE
eda30 44 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  D);.}./*.** Rele
eda40 61 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69  ase a lock acqui
eda50 72 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65  red by winceMute
eda60 78 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64  xAcquire().*/.#d
eda70 65 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78  efine winceMutex
eda80 52 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61  Release(h) Relea
eda90 73 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a  seMutex(h)../*.*
edaa0 2a 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 74  * Create the mut
edab0 65 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65  ex and shared me
edac0 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f  mory used for lo
edad0 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c  cking in the fil
edae0 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20  e.** descriptor 
edaf0 70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20  pFile.*/.static 
edb00 42 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65  BOOL winceCreate
edb10 4c 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20  Lock(const char 
edb20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46  *zFilename, winF
edb30 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57  ile *pFile){.  W
edb40 43 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43  CHAR *zTok;.  WC
edb50 48 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66  HAR *zName = utf
edb60 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65  8ToUnicode(zFile
edb70 6e 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49  name);.  BOOL bI
edb80 6e 69 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f  nit = TRUE;..  /
edb90 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
edba0 20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20   local lockdata 
edbb0 2a 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28  */.  ZeroMemory(
edbc0 26 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73  &pFile->local, s
edbd0 69 7a 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63  izeof(pFile->loc
edbe0 61 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c  al));..  /* Repl
edbf0 61 63 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73  ace the backslas
edc00 68 65 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c  hes from the fil
edc10 65 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63  ename and lowerc
edc20 61 73 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64  ase it.  ** to d
edc30 65 72 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61  erive a mutex na
edc40 6d 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20  me. */.  zTok = 
edc50 43 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65  CharLowerW(zName
edc60 29 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b  );.  for (;*zTok
edc70 3b 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66  ;zTok++){.    if
edc80 20 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29   (*zTok == '\\')
edc90 20 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20   *zTok = '_';.  
edca0 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f  }..  /* Create/o
edcb0 70 65 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75  pen the named mu
edcc0 74 65 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e  tex */.  pFile->
edcd0 68 4d 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d  hMutex = CreateM
edce0 75 74 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53  utexW(NULL, FALS
edcf0 45 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20  E, zName);.  if 
edd00 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  (!pFile->hMutex)
edd10 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
edd20 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
edd30 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 66 72 65  Error();.    fre
edd40 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  e(zName);.    re
edd50 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a  turn FALSE;.  }.
edd60 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68  .  /* Acquire th
edd70 65 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 63  e mutex before c
edd80 6f 6e 74 69 6e 75 69 6e 67 20 2a 2f 0a 20 20 77  ontinuing */.  w
edd90 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65  inceMutexAcquire
edda0 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
eddb0 0a 20 20 0a 20 20 2f 2a 20 53 69 6e 63 65 20 74  .  .  /* Since t
eddc0 68 65 20 6e 61 6d 65 73 20 6f 66 20 6e 61 6d 65  he names of name
eddd0 64 20 6d 75 74 65 78 65 73 2c 20 73 65 6d 61 70  d mutexes, semap
edde0 68 6f 72 65 73 2c 20 66 69 6c 65 20 6d 61 70 70  hores, file mapp
eddf0 69 6e 67 73 20 65 74 63 20 61 72 65 20 0a 20 20  ings etc are .  
ede00 2a 2a 20 63 61 73 65 2d 73 65 6e 73 69 74 69 76  ** case-sensitiv
ede10 65 2c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  e, take advantag
ede20 65 20 6f 66 20 74 68 61 74 20 62 79 20 75 70 70  e of that by upp
ede30 65 72 63 61 73 69 6e 67 20 74 68 65 20 6d 75 74  ercasing the mut
ede40 65 78 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64  ex name.  ** and
ede50 20 75 73 69 6e 67 20 74 68 61 74 20 61 73 20 74   using that as t
ede60 68 65 20 73 68 61 72 65 64 20 66 69 6c 65 6d 61  he shared filema
ede70 70 70 69 6e 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f  pping name..  */
ede80 0a 20 20 43 68 61 72 55 70 70 65 72 57 28 7a 4e  .  CharUpperW(zN
ede90 61 6d 65 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 68  ame);.  pFile->h
edea0 53 68 61 72 65 64 20 3d 20 43 72 65 61 74 65 46  Shared = CreateF
edeb0 69 6c 65 4d 61 70 70 69 6e 67 57 28 49 4e 56 41  ileMappingW(INVA
edec0 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
eded0 2c 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20  , NULL,.        
edee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
edef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
edf00 41 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 30  AGE_READWRITE, 0
edf10 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f  , sizeof(winceLo
edf20 63 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ck),.           
edf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
edf40 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d              zNam
edf50 65 29 3b 20 20 0a 0a 20 20 2f 2a 20 53 65 74 20  e);  ..  /* Set 
edf60 61 20 66 6c 61 67 20 74 68 61 74 20 69 6e 64 69  a flag that indi
edf70 63 61 74 65 73 20 77 65 27 72 65 20 74 68 65 20  cates we're the 
edf80 66 69 72 73 74 20 74 6f 20 63 72 65 61 74 65 20  first to create 
edf90 74 68 65 20 6d 65 6d 6f 72 79 20 73 6f 20 69 74  the memory so it
edfa0 20 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 7a   .  ** must be z
edfb0 65 72 6f 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20  ero-initialized 
edfc0 2a 2f 0a 20 20 69 66 20 28 47 65 74 4c 61 73 74  */.  if (GetLast
edfd0 45 72 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f 52  Error() == ERROR
edfe0 5f 41 4c 52 45 41 44 59 5f 45 58 49 53 54 53 29  _ALREADY_EXISTS)
edff0 7b 0a 20 20 20 20 62 49 6e 69 74 20 3d 20 46 41  {.    bInit = FA
ee000 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65  LSE;.  }..  free
ee010 28 7a 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49  (zName);..  /* I
ee020 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69  f we succeeded i
ee030 6e 20 6d 61 6b 69 6e 67 20 74 68 65 20 73 68 61  n making the sha
ee040 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c  red memory handl
ee050 65 2c 20 6d 61 70 20 69 74 2e 20 2a 2f 0a 20 20  e, map it. */.  
ee060 69 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72  if (pFile->hShar
ee070 65 64 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ed){.    pFile->
ee080 73 68 61 72 65 64 20 3d 20 28 77 69 6e 63 65 4c  shared = (winceL
ee090 6f 63 6b 2a 29 4d 61 70 56 69 65 77 4f 66 46 69  ock*)MapViewOfFi
ee0a0 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65  le(pFile->hShare
ee0b0 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  d, .            
ee0c0 20 46 49 4c 45 5f 4d 41 50 5f 52 45 41 44 7c 46   FILE_MAP_READ|F
ee0d0 49 4c 45 5f 4d 41 50 5f 57 52 49 54 45 2c 20 30  ILE_MAP_WRITE, 0
ee0e0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63  , 0, sizeof(winc
ee0f0 65 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 2f 2a 20  eLock));.    /* 
ee100 49 66 20 6d 61 70 70 69 6e 67 20 66 61 69 6c 65  If mapping faile
ee110 64 2c 20 63 6c 6f 73 65 20 74 68 65 20 73 68 61  d, close the sha
ee120 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c  red memory handl
ee130 65 20 61 6e 64 20 65 72 61 73 65 20 69 74 20 2a  e and erase it *
ee140 2f 0a 20 20 20 20 69 66 20 28 21 70 46 69 6c 65  /.    if (!pFile
ee150 2d 3e 73 68 61 72 65 64 29 7b 0a 20 20 20 20 20  ->shared){.     
ee160 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
ee170 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  o = GetLastError
ee180 28 29 3b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48  ();.      CloseH
ee190 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68  andle(pFile->hSh
ee1a0 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69  ared);.      pFi
ee1b0 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55  le->hShared = NU
ee1c0 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  LL;.    }.  }.. 
ee1d0 20 2f 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65   /* If shared me
ee1e0 6d 6f 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62  mory could not b
ee1f0 65 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20  e created, then 
ee200 63 6c 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20  close the mutex 
ee210 61 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66  and fail */.  if
ee220 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64   (pFile->hShared
ee230 20 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77   == NULL){.    w
ee240 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65  inceMutexRelease
ee250 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
ee260 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65  .    CloseHandle
ee270 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
ee280 0a 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74  .    pFile->hMut
ee290 65 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72  ex = NULL;.    r
ee2a0 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d  eturn FALSE;.  }
ee2b0 0a 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c  .  .  /* Initial
ee2c0 69 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d  ize the shared m
ee2d0 65 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 73  emory if we're s
ee2e0 75 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20  upposed to */.  
ee2f0 69 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20  if (bInit) {.   
ee300 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c   ZeroMemory(pFil
ee310 65 2d 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f  e->shared, sizeo
ee320 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20  f(winceLock));. 
ee330 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78   }..  winceMutex
ee340 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68  Release(pFile->h
ee350 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  Mutex);.  return
ee360 20 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   TRUE;.}../*.** 
ee370 44 65 73 74 72 6f 79 20 74 68 65 20 70 61 72 74  Destroy the part
ee380 20 6f 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74   of winFile that
ee390 20 64 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63   deals with winc
ee3a0 65 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69  e locks.*/.stati
ee3b0 63 20 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74  c void winceDest
ee3c0 72 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20  royLock(winFile 
ee3d0 2a 70 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70  *pFile){.  if (p
ee3e0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20  File->hMutex){. 
ee3f0 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68     /* Acquire th
ee400 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77  e mutex */.    w
ee410 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65  inceMutexAcquire
ee420 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
ee430 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c  ..    /* The fol
ee440 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68  lowing blocks sh
ee450 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73  ould probably as
ee460 73 65 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f  sert in debug mo
ee470 64 65 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20  de, but they.   
ee480 20 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e      are to clean
ee490 75 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c  up in case any l
ee4a0 6f 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70  ocks remained op
ee4b0 65 6e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46  en */.    if (pF
ee4c0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
ee4d0 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  ers){.      pFil
ee4e0 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64  e->shared->nRead
ee4f0 65 72 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20  ers --;.    }.  
ee500 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
ee510 61 6c 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20  al.bReserved){. 
ee520 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
ee530 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20  ed->bReserved = 
ee540 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20  FALSE;.    }.   
ee550 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61   if (pFile->loca
ee560 6c 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20  l.bPending){.   
ee570 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
ee580 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c  ->bPending = FAL
ee590 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  SE;.    }.    if
ee5a0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62   (pFile->local.b
ee5b0 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20  Exclusive){.    
ee5c0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
ee5d0 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41  >bExclusive = FA
ee5e0 4c 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  LSE;.    }..    
ee5f0 2f 2a 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20  /* De-reference 
ee600 61 6e 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f  and close our co
ee610 70 79 20 6f 66 20 74 68 65 20 73 68 61 72 65 64  py of the shared
ee620 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a   memory handle *
ee630 2f 0a 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f  /.    UnmapViewO
ee640 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61  fFile(pFile->sha
ee650 72 65 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48  red);.    CloseH
ee660 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68  andle(pFile->hSh
ee670 61 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  ared);..    /* D
ee680 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75 74  one with the mut
ee690 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d  ex */.    winceM
ee6a0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
ee6b0 65 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 0a  e->hMutex);    .
ee6c0 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28      CloseHandle(
ee6d0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
ee6e0 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65      pFile->hMute
ee6f0 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a  x = NULL;.  }.}.
ee700 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65  ./* .** An imple
ee710 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
ee720 20 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20   LockFile() API 
ee730 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77  of windows for w
ee740 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42  ince.*/.static B
ee750 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c  OOL winceLockFil
ee760 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46  e(.  HANDLE *phF
ee770 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46  ile,.  DWORD dwF
ee780 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20  ileOffsetLow,.  
ee790 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73  DWORD dwFileOffs
ee7a0 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20  etHigh,.  DWORD 
ee7b0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
ee7c0 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  LockLow,.  DWORD
ee7d0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
ee7e0 6f 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77  oLockHigh.){.  w
ee7f0 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  inFile *pFile = 
ee800 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c  HANDLE_TO_WINFIL
ee810 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f  E(phFile);.  BOO
ee820 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53  L bReturn = FALS
ee830 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65  E;..  if (!pFile
ee840 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e  ->hMutex) return
ee850 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75   TRUE;.  winceMu
ee860 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65  texAcquire(pFile
ee870 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a  ->hMutex);..  /*
ee880 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 6c   Wanting an excl
ee890 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20  usive lock? */. 
ee8a0 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65   if (dwFileOffse
ee8b0 74 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 46  tLow == SHARED_F
ee8c0 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 20 6e  IRST.       && n
ee8d0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
ee8e0 6f 63 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44  ockLow == SHARED
ee8f0 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28  _SIZE){.    if (
ee900 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e  pFile->shared->n
ee910 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20  Readers == 0 && 
ee920 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
ee930 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b  Exclusive == 0){
ee940 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  .       pFile->s
ee950 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76  hared->bExclusiv
ee960 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20  e = TRUE;.      
ee970 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45   pFile->local.bE
ee980 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b  xclusive = TRUE;
ee990 0a 20 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  .       bReturn 
ee9a0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
ee9b0 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72  }..  /* Want a r
ee9c0 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a  ead-only lock? *
ee9d0 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 28 64 77  /.  else if ((dw
ee9e0 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d  FileOffsetLow >=
ee9f0 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26   SHARED_FIRST &&
eea00 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 77 46  .            dwF
eea10 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3c 20 53  ileOffsetLow < S
eea20 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 53 48  HARED_FIRST + SH
eea30 41 52 45 44 5f 53 49 5a 45 29 20 26 26 0a 20 20  ARED_SIZE) &&.  
eea40 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62 65            nNumbe
eea50 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f  rOfBytesToLockLo
eea60 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20  w == 1){.    if 
eea70 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e  (pFile->shared->
eea80 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29  bExclusive == 0)
eea90 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
eeaa0 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b 2b  ocal.nReaders ++
eeab0 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69 6c  ;.      if (pFil
eeac0 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
eead0 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20 20  s == 1){.       
eeae0 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
eeaf0 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20  nReaders ++;.   
eeb00 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75     }.      bRetu
eeb10 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d  rn = TRUE;.    }
eeb20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20  .  }..  /* Want 
eeb30 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f 20  a pending lock? 
eeb40 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77  */.  else if (dw
eeb50 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d  FileOffsetLow ==
eeb60 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26   PENDING_BYTE &&
eeb70 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
eeb80 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a  oLockLow == 1){.
eeb90 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 6e      /* If no pen
eeba0 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65  ding lock has be
eebb0 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68 65  en acquired, the
eebc0 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f 0a  n acquire it */.
eebd0 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73      if (pFile->s
eebe0 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20  hared->bPending 
eebf0 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46  == 0) {.      pF
eec00 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65  ile->shared->bPe
eec10 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20  nding = TRUE;.  
eec20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
eec30 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45  .bPending = TRUE
eec40 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  ;.      bReturn 
eec50 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
eec60 7d 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65  }.  /* Want a re
eec70 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f 0a  served lock? */.
eec80 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c    else if (dwFil
eec90 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52 45  eOffsetLow == RE
eeca0 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e  SERVED_BYTE && n
eecb0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
eecc0 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20  ockLow == 1){.  
eecd0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61    if (pFile->sha
eece0 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d  red->bReserved =
eecf0 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69  = 0) {.      pFi
eed00 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73  le->shared->bRes
eed10 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20  erved = TRUE;.  
eed20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
eed30 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55  .bReserved = TRU
eed40 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
eed50 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
eed60 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78   }..  winceMutex
eed70 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68  Release(pFile->h
eed80 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  Mutex);.  return
eed90 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a   bReturn;.}../*.
eeda0 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ** An implementa
eedb0 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c 6f  tion of the Unlo
eedc0 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77 69  ckFile API of wi
eedd0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a  ndows for wince.
eede0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77  */.static BOOL w
eedf0 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 0a  inceUnlockFile(.
eee00 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65    HANDLE *phFile
eee10 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65  ,.  DWORD dwFile
eee20 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f  OffsetLow,.  DWO
eee30 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48  RD dwFileOffsetH
eee40 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75  igh,.  DWORD nNu
eee50 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c  mberOfBytesToUnl
eee60 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20  ockLow,.  DWORD 
eee70 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
eee80 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20  UnlockHigh.){.  
eee90 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
eeea0 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49   HANDLE_TO_WINFI
eeeb0 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f  LE(phFile);.  BO
eeec0 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c  OL bReturn = FAL
eeed0 53 45 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c  SE;..  if (!pFil
eeee0 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72  e->hMutex) retur
eeef0 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d  n TRUE;.  winceM
eef00 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
eef10 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f  e->hMutex);..  /
eef20 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65  * Releasing a re
eef30 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e 20  ader lock or an 
eef40 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a  exclusive lock *
eef50 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66  /.  if (dwFileOf
eef60 66 73 65 74 4c 6f 77 20 3e 3d 20 53 48 41 52 45  fsetLow >= SHARE
eef70 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20  D_FIRST &&.     
eef80 20 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f    dwFileOffsetLo
eef90 77 20 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54  w < SHARED_FIRST
eefa0 20 2b 20 53 48 41 52 45 44 5f 53 49 5a 45 29 7b   + SHARED_SIZE){
eefb0 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 68  .    /* Did we h
eefc0 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ave an exclusive
eefd0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 66   lock? */.    if
eefe0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62   (pFile->local.b
eeff0 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20  Exclusive){.    
ef000 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
ef010 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53  Exclusive = FALS
ef020 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  E;.      pFile->
ef030 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69  shared->bExclusi
ef040 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ve = FALSE;.    
ef050 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45    bReturn = TRUE
ef060 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
ef070 44 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 65  Did we just have
ef080 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20   a reader lock? 
ef090 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28  */.    else if (
ef0a0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65  pFile->local.nRe
ef0b0 61 64 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46  aders){.      pF
ef0c0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
ef0d0 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 66  ers --;.      if
ef0e0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e   (pFile->local.n
ef0f0 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 20  Readers == 0).  
ef100 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 46      {.        pF
ef110 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65  ile->shared->nRe
ef120 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20  aders --;.      
ef130 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  }.      bReturn 
ef140 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
ef150 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e  }..  /* Releasin
ef160 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  g a pending lock
ef170 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
ef180 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
ef190 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26  = PENDING_BYTE &
ef1a0 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  & nNumberOfBytes
ef1b0 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31  ToUnlockLow == 1
ef1c0 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65  ){.    if (pFile
ef1d0 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67  ->local.bPending
ef1e0 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
ef1f0 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d  local.bPending =
ef200 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46   FALSE;.      pF
ef210 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65  ile->shared->bPe
ef220 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20  nding = FALSE;. 
ef230 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54       bReturn = T
ef240 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  RUE;.    }.  }. 
ef250 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20   /* Releasing a 
ef260 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f  reserved lock */
ef270 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69  .  else if (dwFi
ef280 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52  leOffsetLow == R
ef290 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20  ESERVED_BYTE && 
ef2a0 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
ef2b0 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  UnlockLow == 1){
ef2c0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
ef2d0 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29  local.bReserved)
ef2e0 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e   {.      pFile->
ef2f0 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20  local.bReserved 
ef300 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70  = FALSE;.      p
ef310 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52  File->shared->bR
ef320 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b  eserved = FALSE;
ef330 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d  .      bReturn =
ef340 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d   TRUE;.    }.  }
ef350 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65  ..  winceMutexRe
ef360 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75  lease(pFile->hMu
ef370 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62  tex);.  return b
ef380 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Return;.}../*.**
ef390 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   An implementati
ef3a0 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69  on of the LockFi
ef3b0 6c 65 45 78 28 29 20 41 50 49 20 6f 66 20 77 69  leEx() API of wi
ef3c0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a  ndows for wince.
ef3d0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77  */.static BOOL w
ef3e0 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 0a  inceLockFileEx(.
ef3f0 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65    HANDLE *phFile
ef400 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67  ,.  DWORD dwFlag
ef410 73 2c 0a 20 20 44 57 4f 52 44 20 64 77 52 65 73  s,.  DWORD dwRes
ef420 65 72 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 6e  erved,.  DWORD n
ef430 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
ef440 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20  ockLow,.  DWORD 
ef450 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
ef460 4c 6f 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f 56  LockHigh,.  LPOV
ef470 45 52 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 6c  ERLAPPED lpOverl
ef480 61 70 70 65 64 0a 29 7b 0a 20 20 2f 2a 20 49 66  apped.){.  /* If
ef490 20 74 68 65 20 63 61 6c 6c 65 72 20 77 61 6e 74   the caller want
ef4a0 73 20 61 20 73 68 61 72 65 64 20 72 65 61 64 20  s a shared read 
ef4b0 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 64 20 74 68  lock, forward th
ef4c0 69 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20  is call.  ** to 
ef4d0 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f  winceLockFile */
ef4e0 0a 20 20 69 66 20 28 6c 70 4f 76 65 72 6c 61 70  .  if (lpOverlap
ef4f0 70 65 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 53  ped->Offset == S
ef500 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20  HARED_FIRST &&. 
ef510 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d 20       dwFlags == 
ef520 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d 62  1 &&.      nNumb
ef530 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
ef540 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53 49 5a  ow == SHARED_SIZ
ef550 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 77  E){.    return w
ef560 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70 68 46  inceLockFile(phF
ef570 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ile, SHARED_FIRS
ef580 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d  T, 0, 1, 0);.  }
ef590 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b  .  return FALSE;
ef5a0 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f 66 20  .}./*.** End of 
ef5b0 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 64 65  the special code
ef5c0 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a 2a 2a   for wince.*****
ef5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef610 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
ef620 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   /* SQLITE_OS_WI
ef630 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  NCE */../*******
ef640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef680 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  ******.** The ne
ef690 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74  xt group of rout
ef6a0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74  ines implement t
ef6b0 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73  he I/O methods s
ef6c0 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74  pecified.** by t
ef6d0 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
ef6e0 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
ef6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ef730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
ef740 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c  *.** Close a fil
ef750 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 72  e..**.** It is r
ef760 65 70 6f 72 74 65 64 20 74 68 61 74 20 61 6e 20  eported that an 
ef770 61 74 74 65 6d 70 74 20 74 6f 20 63 6c 6f 73 65  attempt to close
ef780 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 68 74 20   a handle might 
ef790 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 66 61 69  sometimes.** fai
ef7a0 6c 2e 20 20 54 68 69 73 20 69 73 20 61 20 76 65  l.  This is a ve
ef7b0 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20  ry unreasonable 
ef7c0 72 65 73 75 6c 74 2c 20 62 75 74 20 77 69 6e 64  result, but wind
ef7d0 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f 75 73  ows is notorious
ef7e0 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20 75 6e  .** for being un
ef7f0 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20 49 20  reasonable so I 
ef800 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74 68 61  do not doubt tha
ef810 74 20 69 74 20 6d 69 67 68 74 20 68 61 70 70 65  t it might happe
ef820 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 63 6c  n.  If.** the cl
ef830 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20 70 61  ose fails, we pa
ef840 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69 6c 6c  use for 100 mill
ef850 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79  iseconds and try
ef860 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a 2a 20 6d   again.  As.** m
ef870 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53 45 5f  any as MX_CLOSE_
ef880 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70 74 73  ATTEMPT attempts
ef890 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 68 61   to close the ha
ef8a0 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20 62 65  ndle are made be
ef8b0 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e 67 20 75  fore.** giving u
ef8c0 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20  p and returning 
ef8d0 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65  an error..*/.#de
ef8e0 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f 41 54  fine MX_CLOSE_AT
ef8f0 54 45 4d 50 54 20 33 0a 73 74 61 74 69 63 20 69  TEMPT 3.static i
ef900 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 71 6c 69  nt winClose(sqli
ef910 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
ef920 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d 20 30   int rc, cnt = 0
ef930 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  ;.  winFile *pFi
ef940 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
ef950 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64  d;..  assert( id
ef960 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  !=0 );.  OSTRACE
ef970 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20  2("CLOSE %d\n", 
ef980 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64 6f 7b  pFile->h);.  do{
ef990 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f 73 65 48  .    rc = CloseH
ef9a0 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 29 3b  andle(pFile->h);
ef9b0 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 30  .  }while( rc==0
ef9c0 20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d 58 5f 43   && ++cnt < MX_C
ef9d0 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 26 26 20  LOSE_ATTEMPT && 
ef9e0 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20  (Sleep(100), 1) 
ef9f0 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  );.#if SQLITE_OS
efa00 5f 57 49 4e 43 45 0a 23 64 65 66 69 6e 65 20 57  _WINCE.#define W
efa10 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54  INCE_DELETION_AT
efa20 54 45 4d 50 54 53 20 33 0a 20 20 77 69 6e 63 65  TEMPTS 3.  wince
efa30 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46 69 6c  DestroyLock(pFil
efa40 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  e);.  if( pFile-
efa50 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20  >zDeleteOnClose 
efa60 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 3d  ){.    int cnt =
efa70 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 0a 20   0;.    while(. 
efa80 20 20 20 20 20 20 20 20 20 20 44 65 6c 65 74 65            Delete
efa90 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e 7a 44 65  FileW(pFile->zDe
efaa0 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3d 3d 30 0a  leteOnClose)==0.
efab0 20 20 20 20 20 20 20 20 26 26 20 47 65 74 46 69          && GetFi
efac0 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 70 46  leAttributesW(pF
efad0 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c  ile->zDeleteOnCl
efae0 6f 73 65 29 21 3d 30 78 66 66 66 66 66 66 66 66  ose)!=0xffffffff
efaf0 20 0a 20 20 20 20 20 20 20 20 26 26 20 63 6e 74   .        && cnt
efb00 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 45 4c 45 54  ++ < WINCE_DELET
efb10 49 4f 4e 5f 41 54 54 45 4d 50 54 53 0a 20 20 20  ION_ATTEMPTS.   
efb20 20 29 7b 0a 20 20 20 20 20 20 20 53 6c 65 65 70   ){.       Sleep
efb30 28 31 30 30 29 3b 20 20 2f 2a 20 57 61 69 74 20  (100);  /* Wait 
efb40 61 20 6c 69 74 74 6c 65 20 62 65 66 6f 72 65 20  a little before 
efb50 74 72 79 69 6e 67 20 61 67 61 69 6e 20 2a 2f 0a  trying again */.
efb60 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65 28 70      }.    free(p
efb70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43  File->zDeleteOnC
efb80 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  lose);.  }.#endi
efb90 66 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  f.  OpenCounter(
efba0 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  -1);.  return rc
efbb0 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
efbc0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a  QLITE_IOERR;.}..
efbd0 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f  /*.** Some micro
efbe0 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c  soft compilers l
efbf0 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74  ack this definit
efc00 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ion..*/.#ifndef 
efc10 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45  INVALID_SET_FILE
efc20 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 66 69 6e  _POINTER.# defin
efc30 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  e INVALID_SET_FI
efc40 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 44 57 4f  LE_POINTER ((DWO
efc50 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a 0a 2f  RD)-1).#endif../
efc60 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66  *.** Read data f
efc70 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 6f 20  rom a file into 
efc80 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 75 72  a buffer.  Retur
efc90 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61  n SQLITE_OK if a
efca0 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 72 65  ll.** bytes were
efcb0 20 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c   read successful
efcc0 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f  ly and SQLITE_IO
efcd0 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ERR if anything 
efce0 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a  goes.** wrong..*
efcf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
efd00 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f  Read(.  sqlite3_
efd10 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20  file *id,       
efd20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72 65     /* File to re
efd30 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69  ad from */.  voi
efd40 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20  d *pBuf,        
efd50 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
efd60 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68   content into th
efd70 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69  is buffer */.  i
efd80 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
efd90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
efda0 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
efdb0 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  read */.  sqlite
efdc0 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20  3_int64 offset  
efdd0 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65       /* Begin re
efde0 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66  ading at this of
efdf0 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e  fset */.){.  LON
efe00 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c  G upperBits = (L
efe10 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32  ONG)((offset>>32
efe20 29 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b  ) & 0x7fffffff);
efe30 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74  .  LONG lowerBit
efe40 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65  s = (LONG)(offse
efe50 74 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b  t & 0xffffffff);
efe60 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77  .  DWORD rc;.  w
efe70 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  inFile *pFile = 
efe80 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (winFile*)id;.  
efe90 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 20 44  DWORD error;.  D
efea0 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 20 61 73 73  WORD got;..  ass
efeb0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
efec0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
efed0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
efee0 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 4f 53 54  ERR_READ);.  OST
efef0 52 41 43 45 33 28 22 52 45 41 44 20 25 64 20 6c  RACE3("READ %d l
eff00 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
eff10 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
eff20 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65  type);.  rc = Se
eff30 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69  tFilePointer(pFi
eff40 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73  le->h, lowerBits
eff50 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49  , &upperBits, FI
eff60 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28  LE_BEGIN);.  if(
eff70 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54   rc==INVALID_SET
eff80 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26  _FILE_POINTER &&
eff90 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45   (error=GetLastE
effa0 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f  rror())!=NO_ERRO
effb0 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  R ){.    pFile->
effc0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f  lastErrno = erro
effd0 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  r;.    return SQ
effe0 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20  LITE_FULL;.  }. 
efff0 20 69 66 28 20 21 52 65 61 64 46 69 6c 65 28 70   if( !ReadFile(p
f0000 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61  File->h, pBuf, a
f0010 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20 29 7b 0a  mt, &got, 0) ){.
f0020 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
f0030 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72  rrno = GetLastEr
f0040 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72  ror();.    retur
f0050 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  n SQLITE_IOERR_R
f0060 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67  EAD;.  }.  if( g
f0070 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74 20 29  ot==(DWORD)amt )
f0080 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
f0090 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
f00a0 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70  .    /* Unread p
f00b0 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66 66  arts of the buff
f00c0 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d  er must be zero-
f00d0 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65  filled */.    me
f00e0 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42  mset(&((char*)pB
f00f0 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74  uf)[got], 0, amt
f0100 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72  -got);.    retur
f0110 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  n SQLITE_IOERR_S
f0120 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d  HORT_READ;.  }.}
f0130 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
f0140 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72  ta from a buffer
f0150 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52   into a file.  R
f0160 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
f0170 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  on success.** or
f0180 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f   some other erro
f0190 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72  r code on failur
f01a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
f01b0 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 71 6c   winWrite(.  sql
f01c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20  ite3_file *id,  
f01d0 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74         /* File t
f01e0 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a  o write into */.
f01f0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42    const void *pB
f0200 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  uf,         /* T
f0210 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 20 77  he bytes to be w
f0220 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  ritten */.  int 
f0230 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  amt,            
f0240 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
f0250 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74  of bytes to writ
f0260 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  e */.  sqlite3_i
f0270 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20  nt64 offset     
f0280 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
f0290 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69  the file to begi
f02a0 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a  n writing at */.
f02b0 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42  ){.  LONG upperB
f02c0 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66  its = (LONG)((of
f02d0 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66  fset>>32) & 0x7f
f02e0 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20  ffffff);.  LONG 
f02f0 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e  lowerBits = (LON
f0300 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66  G)(offset & 0xff
f0310 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44  ffffff);.  DWORD
f0320 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a   rc;.  winFile *
f0330 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
f0340 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72  *)id;.  DWORD er
f0350 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 77 72 6f  ror;.  DWORD wro
f0360 74 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  te = 0;..  asser
f0370 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69  t( id!=0 );.  Si
f0380 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65  mulateIOError(re
f0390 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
f03a0 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69 6d 75  R_WRITE);.  Simu
f03b0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
f03c0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
f03d0 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41 43 45  FULL);.  OSTRACE
f03e0 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b  3("WRITE %d lock
f03f0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
f0400 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
f0410 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69  e);.  rc = SetFi
f0420 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d  lePointer(pFile-
f0430 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26  >h, lowerBits, &
f0440 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f  upperBits, FILE_
f0450 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63  BEGIN);.  if( rc
f0460 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  ==INVALID_SET_FI
f0470 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65  LE_POINTER && (e
f0480 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f  rror=GetLastErro
f0490 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29  r())!=NO_ERROR )
f04a0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
f04b0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a  tErrno = error;.
f04c0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
f04d0 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 61 73  E_FULL;.  }.  as
f04e0 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20  sert( amt>0 );. 
f04f0 20 77 68 69 6c 65 28 0a 20 20 20 20 20 61 6d 74   while(.     amt
f0500 3e 30 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d  >0.     && (rc =
f0510 20 57 72 69 74 65 46 69 6c 65 28 70 46 69 6c 65   WriteFile(pFile
f0520 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20  ->h, pBuf, amt, 
f0530 26 77 72 6f 74 65 2c 20 30 29 29 21 3d 30 0a 20  &wrote, 0))!=0. 
f0540 20 20 20 20 26 26 20 77 72 6f 74 65 3e 30 0a 20      && wrote>0. 
f0550 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77   ){.    amt -= w
f0560 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d  rote;.    pBuf =
f0570 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b   &((char*)pBuf)[
f0580 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20 69 66  wrote];.  }.  if
f0590 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e 28 69 6e  ( !rc || amt>(in
f05a0 74 29 77 72 6f 74 65 20 29 7b 0a 20 20 20 20 70  t)wrote ){.    p
f05b0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
f05c0 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
f05d0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
f05e0 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20  ITE_FULL;.  }.  
f05f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f0600 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63  ;.}../*.** Trunc
f0610 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65  ate an open file
f0620 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20   to a specified 
f0630 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69  size.*/.static i
f0640 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65 28 73  nt winTruncate(s
f0650 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
f0660 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e   sqlite3_int64 n
f0670 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 20 75 70  Byte){.  LONG up
f0680 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29  perBits = (LONG)
f0690 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 20 30  ((nByte>>32) & 0
f06a0 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f  x7fffffff);.  LO
f06b0 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28  NG lowerBits = (
f06c0 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 20 30 78  LONG)(nByte & 0x
f06d0 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f  ffffffff);.  DWO
f06e0 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65  RD rc;.  winFile
f06f0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
f0700 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20  le*)id;.  DWORD 
f0710 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74  error;..  assert
f0720 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54  ( id!=0 );.  OST
f0730 52 41 43 45 33 28 22 54 52 55 4e 43 41 54 45 20  RACE3("TRUNCATE 
f0740 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c  %d %lld\n", pFil
f0750 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b 0a 20 20  e->h, nByte);.  
f0760 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
f0770 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
f0780 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b 0a 20  ERR_TRUNCATE);. 
f0790 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69   rc = SetFilePoi
f07a0 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c  nter(pFile->h, l
f07b0 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72  owerBits, &upper
f07c0 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e  Bits, FILE_BEGIN
f07d0 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56  );.  if( rc==INV
f07e0 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f  ALID_SET_FILE_PO
f07f0 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d  INTER && (error=
f0800 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21  GetLastError())!
f0810 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20  =NO_ERROR ){.   
f0820 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
f0830 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72  o = error;.    r
f0840 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
f0850 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d  RR_TRUNCATE;.  }
f0860 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f 66 46 69  .  /* SetEndOfFi
f0870 6c 65 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 20  le will fail if 
f0880 6e 42 79 74 65 20 69 73 20 6e 65 67 61 74 69 76  nByte is negativ
f0890 65 20 2a 2f 0a 20 20 69 66 28 20 21 53 65 74 45  e */.  if( !SetE
f08a0 6e 64 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e  ndOfFile(pFile->
f08b0 68 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  h) ){.    pFile-
f08c0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74  >lastErrno = Get
f08d0 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
f08e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
f08f0 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20  OERR_TRUNCATE;. 
f0900 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
f0910 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66  TE_OK;.}..#ifdef
f0920 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a   SQLITE_TEST./*.
f0930 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d  ** Count the num
f0940 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73  ber of fullsyncs
f0950 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63   and normal sync
f0960 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  s.  This is used
f0970 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74   to test.** that
f0980 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73   syncs and fulls
f0990 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 69 6e  yncs are occurin
f09a0 67 20 61 74 20 74 68 65 20 72 69 67 68 74 20 74  g at the right t
f09b0 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  imes..*/.SQLITE_
f09c0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f09d0 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  sync_count = 0;.
f09e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
f09f0 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f  qlite3_fullsync_
f0a00 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69  count = 0;.#endi
f0a10 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  f../*.** Make su
f0a20 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f  re all writes to
f0a30 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69   a particular fi
f0a40 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64  le are committed
f0a50 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61   to disk..*/.sta
f0a60 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e 63 28  tic int winSync(
f0a70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
f0a80 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 23 69  , int flags){.#i
f0a90 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f  fndef SQLITE_NO_
f0aa0 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c 65 20 2a  SYNC.  winFile *
f0ab0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
f0ac0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28  *)id;..  assert(
f0ad0 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52   id!=0 );.  OSTR
f0ae0 41 43 45 33 28 22 53 59 4e 43 20 25 64 20 6c 6f  ACE3("SYNC %d lo
f0af0 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
f0b00 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
f0b10 79 70 65 29 3b 0a 23 65 6c 73 65 0a 20 20 55 4e  ype);.#else.  UN
f0b20 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69  USED_PARAMETER(i
f0b30 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  d);.#endif.#ifnd
f0b40 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
f0b50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f0b60 52 28 66 6c 61 67 73 29 3b 0a 23 65 6c 73 65 0a  R(flags);.#else.
f0b70 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51    if( flags & SQ
f0b80 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20 29  LITE_SYNC_FULL )
f0b90 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 75  {.    sqlite3_fu
f0ba0 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a  llsync_count++;.
f0bb0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 79    }.  sqlite3_sy
f0bc0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64  nc_count++;.#end
f0bd0 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f  if.  /* If we co
f0be0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20  mpiled with the 
f0bf0 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66  SQLITE_NO_SYNC f
f0c00 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e  lag, then syncin
f0c10 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f  g is a.  ** no-o
f0c20 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  p.  */.#ifdef SQ
f0c30 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 20  LITE_NO_SYNC.   
f0c40 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f0c50 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 46  K;.#else.  if( F
f0c60 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 73 28  lushFileBuffers(
f0c70 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20  pFile->h) ){.   
f0c80 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f0c90 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
f0ca0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
f0cb0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
f0cc0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
f0cd0 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a  LITE_IOERR;.  }.
f0ce0 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
f0cf0 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75  Determine the cu
f0d00 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20  rrent size of a 
f0d10 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f  file in bytes.*/
f0d20 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46  .static int winF
f0d30 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  ileSize(sqlite3_
f0d40 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65  file *id, sqlite
f0d50 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b  3_int64 *pSize){
f0d60 0a 20 20 44 57 4f 52 44 20 75 70 70 65 72 42 69  .  DWORD upperBi
f0d70 74 73 3b 0a 20 20 44 57 4f 52 44 20 6c 6f 77 65  ts;.  DWORD lowe
f0d80 72 42 69 74 73 3b 0a 20 20 77 69 6e 46 69 6c 65  rBits;.  winFile
f0d90 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
f0da0 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20  le*)id;.  DWORD 
f0db0 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74  error;..  assert
f0dc0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d  ( id!=0 );.  Sim
f0dd0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74  ulateIOError(ret
f0de0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
f0df0 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f 77 65 72  _FSTAT);.  lower
f0e00 42 69 74 73 20 3d 20 47 65 74 46 69 6c 65 53 69  Bits = GetFileSi
f0e10 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20 26 75 70  ze(pFile->h, &up
f0e20 70 65 72 42 69 74 73 29 3b 0a 20 20 69 66 28 20  perBits);.  if( 
f0e30 20 20 28 6c 6f 77 65 72 42 69 74 73 20 3d 3d 20    (lowerBits == 
f0e40 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 53 49 5a  INVALID_FILE_SIZ
f0e50 45 29 0a 20 20 20 20 20 26 26 20 28 28 65 72 72  E).     && ((err
f0e60 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  or = GetLastErro
f0e70 72 28 29 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  r()) != NO_ERROR
f0e80 29 20 29 0a 20 20 7b 0a 20 20 20 20 70 46 69 6c  ) ).  {.    pFil
f0e90 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
f0ea0 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e  rror;.    return
f0eb0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53   SQLITE_IOERR_FS
f0ec0 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a  TAT;.  }.  *pSiz
f0ed0 65 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69  e = (((sqlite3_i
f0ee0 6e 74 36 34 29 75 70 70 65 72 42 69 74 73 29 3c  nt64)upperBits)<
f0ef0 3c 33 32 29 20 2b 20 6c 6f 77 65 72 42 69 74 73  <32) + lowerBits
f0f00 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
f0f10 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  E_OK;.}../*.** L
f0f20 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d  OCKFILE_FAIL_IMM
f0f30 45 44 49 41 54 45 4c 59 20 69 73 20 75 6e 64 65  EDIATELY is unde
f0f40 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 57 69  fined on some Wi
f0f50 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a  ndows systems..*
f0f60 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b 46 49  /.#ifndef LOCKFI
f0f70 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54  LE_FAIL_IMMEDIAT
f0f80 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c 4f 43  ELY.# define LOC
f0f90 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44  KFILE_FAIL_IMMED
f0fa0 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 69 66 0a  IATELY 1.#endif.
f0fb0 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61  ./*.** Acquire a
f0fc0 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2a   reader lock..**
f0fd0 20 44 69 66 66 65 72 65 6e 74 20 41 50 49 20 72   Different API r
f0fe0 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c  outines are call
f0ff0 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  ed depending on 
f1000 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
f1010 68 69 73 0a 2a 2a 20 69 73 20 57 69 6e 39 35 20  his.** is Win95 
f1020 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 73 74 61  or WinNT..*/.sta
f1030 74 69 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c  tic int getReadL
f1040 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69  ock(winFile *pFi
f1050 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a  le){.  int res;.
f1060 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
f1070 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44 20 6f      OVERLAPPED o
f1080 76 6c 70 3b 0a 20 20 20 20 6f 76 6c 70 2e 4f 66  vlp;.    ovlp.Of
f1090 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49  fset = SHARED_FI
f10a0 52 53 54 3b 0a 20 20 20 20 6f 76 6c 70 2e 4f 66  RST;.    ovlp.Of
f10b0 66 73 65 74 48 69 67 68 20 3d 20 30 3b 0a 20 20  fsetHigh = 0;.  
f10c0 20 20 6f 76 6c 70 2e 68 45 76 65 6e 74 20 3d 20    ovlp.hEvent = 
f10d0 30 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63  0;.    res = Loc
f10e0 6b 46 69 6c 65 45 78 28 70 46 69 6c 65 2d 3e 68  kFileEx(pFile->h
f10f0 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f  , LOCKFILE_FAIL_
f1100 49 4d 4d 45 44 49 41 54 45 4c 59 2c 0a 20 20 20  IMMEDIATELY,.   
f1110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1120 20 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45    0, SHARED_SIZE
f1130 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0a 2f 2a 20  , 0, &ovlp);./* 
f1140 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
f1150 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
f1160 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
f1170 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
f1180 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  d. .*/.#if SQLIT
f1190 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20  E_OS_WINCE==0.  
f11a0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6c  }else{.    int l
f11b0 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  k;.    sqlite3_r
f11c0 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
f11d0 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20 20 20  (lk), &lk);.    
f11e0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63  pFile->sharedLoc
f11f0 6b 42 79 74 65 20 3d 20 28 73 68 6f 72 74 29 28  kByte = (short)(
f1200 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66 66 66  (lk & 0x7fffffff
f1210 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d  )%(SHARED_SIZE -
f1220 20 31 29 29 3b 0a 20 20 20 20 72 65 73 20 3d 20   1));.    res = 
f1230 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  LockFile(pFile->
f1240 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b  h, SHARED_FIRST+
f1250 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63  pFile->sharedLoc
f1260 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b  kByte, 0, 1, 0);
f1270 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66  .#endif.  }.  if
f1280 28 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20  ( res == 0 ){.  
f1290 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
f12a0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  no = GetLastErro
f12b0 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  r();.  }.  retur
f12c0 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n res;.}../*.** 
f12d0 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a  Undo a readlock.
f12e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
f12f0 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 77 69 6e  lockReadLock(win
f1300 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20  File *pFile){.  
f1310 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69  int res;.  if( i
f1320 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73  sNT() ){.    res
f1330 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46   = UnlockFile(pF
f1340 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46  ile->h, SHARED_F
f1350 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f  IRST, 0, SHARED_
f1360 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69 73 4e  SIZE, 0);./* isN
f1370 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49  T() is 1 if SQLI
f1380 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20  TE_OS_WINCE==1, 
f1390 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20  so this else is 
f13a0 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20  never executed. 
f13b0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
f13c0 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c  S_WINCE==0.  }el
f13d0 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e  se{.    res = Un
f13e0 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
f13f0 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20  h, SHARED_FIRST 
f1400 2b 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c  + pFile->sharedL
f1410 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30  ockByte, 0, 1, 0
f1420 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
f1430 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a  if( res == 0 ){.
f1440 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
f1450 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72  rrno = GetLastEr
f1460 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ror();.  }.  ret
f1470 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn res;.}../*.*
f1480 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20  * Lock the file 
f1490 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70  with the lock sp
f14a0 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ecified by param
f14b0 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20  eter locktype - 
f14c0 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f  one.** of the fo
f14d0 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
f14e0 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f     (1) SHARED_LO
f14f0 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45  CK.**     (2) RE
f1500 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SERVED_LOCK.**  
f1510 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c     (3) PENDING_L
f1520 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45  OCK.**     (4) E
f1530 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a  XCLUSIVE_LOCK.**
f1540 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68  .** Sometimes wh
f1550 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e  en requesting on
f1560 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64  e lock state, ad
f1570 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74  ditional lock st
f1580 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65  ates.** are inse
f1590 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e  rted in between.
f15a0 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69    The locking mi
f15b0 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20  ght fail on one 
f15c0 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20  of the later.** 
f15d0 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76  transitions leav
f15e0 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61  ing the lock sta
f15f0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  te different fro
f1600 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65  m what it starte
f1610 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73  d but.** still s
f1620 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c  hort of its goal
f1630 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
f1640 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65   chart shows the
f1650 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e   allowed.** tran
f1660 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20  sitions and the 
f1670 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65  inserted interme
f1680 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a  diate states:.**
f1690 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20  .**    UNLOCKED 
f16a0 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20  -> SHARED.**    
f16b0 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56  SHARED -> RESERV
f16c0 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
f16d0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
f16e0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
f16f0 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e  RESERVED -> (PEN
f1700 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
f1710 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47  VE.**    PENDING
f1720 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
f1730 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
f1740 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65   will only incre
f1750 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68 65  ase a lock.  The
f1760 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 6f 75   winUnlock() rou
f1770 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20 61  tine.** erases a
f1780 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63 65  ll locks at once
f1790 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73 20   and returns us 
f17a0 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 6c  immediately to l
f17b0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a  ocking level 0..
f17c0 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  ** It is not pos
f17d0 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20 74  sible to lower t
f17e0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
f17f0 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20 74   one step at a t
f1800 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75 73  ime.  You.** mus
f1810 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74 6f  t go straight to
f1820 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30   locking level 0
f1830 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f1840 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  winLock(sqlite3_
f1850 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
f1860 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72  cktype){.  int r
f1870 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
f1880 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
f1890 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65   from subroutine
f18a0 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d  s */.  int res =
f18b0 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   1;           /*
f18c0 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 69 6e   Result of a win
f18d0 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a  dows lock call *
f18e0 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74  /.  int newLockt
f18f0 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65  ype;       /* Se
f1900 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  t pFile->locktyp
f1910 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20  e to this value 
f1920 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a  before exiting *
f1930 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69  /.  int gotPendi
f1940 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72  ngLock = 0;/* Tr
f1950 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65  ue if we acquire
f1960 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  d a PENDING lock
f1970 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20   this time */.  
f1980 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
f1990 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20   (winFile*)id;. 
f19a0 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d 20 4e   DWORD error = N
f19b0 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61 73 73 65  O_ERROR;..  asse
f19c0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f  rt( id!=0 );.  O
f19d0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 25 64  STRACE5("LOCK %d
f19e0 20 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e   %d was %d(%d)\n
f19f0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ",.          pFi
f1a00 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
f1a10 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
f1a20 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c  , pFile->sharedL
f1a30 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f 2a 20  ockByte);..  /* 
f1a40 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65  If there is alre
f1a50 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68  ady a lock of th
f1a60 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20  is type or more 
f1a70 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74  restrictive on t
f1a80 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 2c 20  he.  ** OsFile, 
f1a90 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27  do nothing. Don'
f1aa0 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f  t use the end_lo
f1ab0 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
f1ac0 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4f 73  s.  ** sqlite3Os
f1ad0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73  EnterMutex() has
f1ae0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  n't been called 
f1af0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  yet..  */.  if( 
f1b00 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
f1b10 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
f1b20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f1b30 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  K;.  }..  /* Mak
f1b40 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69  e sure the locki
f1b50 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63  ng sequence is c
f1b60 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73  orrect.  */.  as
f1b70 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
f1b80 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c  ktype!=NO_LOCK |
f1b90 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  | locktype==SHAR
f1ba0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
f1bb0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50  ert( locktype!=P
f1bc0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20  ENDING_LOCK );. 
f1bd0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
f1be0 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e!=RESERVED_LOCK
f1bf0 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   || pFile->lockt
f1c00 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
f1c10 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74   );..  /* Lock t
f1c20 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  he PENDING_LOCK 
f1c30 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20  byte if we need 
f1c40 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e  to acquire a PEN
f1c50 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a  DING lock or.  *
f1c60 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e  * a SHARED lock.
f1c70 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75    If we are acqu
f1c80 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c  iring a SHARED l
f1c90 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69  ock, the acquisi
f1ca0 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65  tion of.  ** the
f1cb0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79   PENDING_LOCK by
f1cc0 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e  te is temporary.
f1cd0 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74  .  */.  newLockt
f1ce0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63  ype = pFile->loc
f1cf0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 20 20 28  ktype;.  if(   (
f1d00 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
f1d10 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 7c  =NO_LOCK).     |
f1d20 7c 20 28 20 20 20 28 6c 6f 63 6b 74 79 70 65 3d  | (   (locktype=
f1d30 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29  =EXCLUSIVE_LOCK)
f1d40 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70 46  .         && (pF
f1d50 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52  ile->locktype==R
f1d60 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 29 0a 20  ESERVED_LOCK)). 
f1d70 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20   ){.    int cnt 
f1d80 3d 20 33 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  = 3;.    while( 
f1d90 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72 65 73 20  cnt-->0 && (res 
f1da0 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
f1db0 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54  ->h, PENDING_BYT
f1dc0 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d 3d 30 20  E, 0, 1, 0))==0 
f1dd0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72 79 20  ){.      /* Try 
f1de0 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74 20 74  3 times to get t
f1df0 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e  he pending lock.
f1e00 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f    The pending lo
f1e10 63 6b 20 6d 69 67 68 74 20 62 65 0a 20 20 20 20  ck might be.    
f1e20 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 61 6e 6f    ** held by ano
f1e30 74 68 65 72 20 72 65 61 64 65 72 20 70 72 6f 63  ther reader proc
f1e40 65 73 73 20 77 68 6f 20 77 69 6c 6c 20 72 65 6c  ess who will rel
f1e50 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e 74 61 72  ease it momentar
f1e60 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ily..      */.  
f1e70 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 63 6f      OSTRACE2("co
f1e80 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20 50 45  uld not get a PE
f1e90 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e 74 3d  NDING lock. cnt=
f1ea0 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a 20 20 20  %d\n", cnt);.   
f1eb0 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 20 20     Sleep(1);.   
f1ec0 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e   }.    gotPendin
f1ed0 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a 20 20 20  gLock = res;.   
f1ee0 20 69 66 28 20 21 72 65 73 20 29 7b 0a 20 20 20   if( !res ){.   
f1ef0 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61     error = GetLa
f1f00 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d  stError();.    }
f1f10 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
f1f20 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  re a shared lock
f1f30 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
f1f40 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
f1f50 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20  K && res ){.    
f1f60 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
f1f70 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
f1f80 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65   );.    res = ge
f1f90 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
f1fa0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
f1fb0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
f1fc0 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b  pe = SHARED_LOCK
f1fd0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
f1fe0 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61     error = GetLa
f1ff0 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d  stError();.    }
f2000 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
f2010 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  re a RESERVED lo
f2020 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
f2030 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44  cktype==RESERVED
f2040 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
f2050 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
f2060 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  e->locktype==SHA
f2070 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
f2080 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70  res = LockFile(p
f2090 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45  File->h, RESERVE
f20a0 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  D_BYTE, 0, 1, 0)
f20b0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
f20c0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
f20d0 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f  pe = RESERVED_LO
f20e0 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  CK;.    }else{. 
f20f0 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74       error = Get
f2100 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
f2110 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71   }.  }..  /* Acq
f2120 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c  uire a PENDING l
f2130 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ock.  */.  if( l
f2140 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
f2150 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29  VE_LOCK && res )
f2160 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70  {.    newLocktyp
f2170 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  e = PENDING_LOCK
f2180 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67  ;.    gotPending
f2190 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  Lock = 0;.  }.. 
f21a0 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45   /* Acquire an E
f21b0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20  XCLUSIVE lock.  
f21c0 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
f21d0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
f21e0 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20  K && res ){.    
f21f0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
f2200 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f  ocktype>=SHARED_
f2210 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20  LOCK );.    res 
f2220 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b  = unlockReadLock
f2230 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54  (pFile);.    OST
f2240 52 41 43 45 32 28 22 75 6e 72 65 61 64 6c 6f 63  RACE2("unreadloc
f2250 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 29 3b  k = %d\n", res);
f2260 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46  .    res = LockF
f2270 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48  ile(pFile->h, SH
f2280 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53  ARED_FIRST, 0, S
f2290 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a  HARED_SIZE, 0);.
f22a0 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20      if( res ){. 
f22b0 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65       newLocktype
f22c0 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43   = EXCLUSIVE_LOC
f22d0 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  K;.    }else{.  
f22e0 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 4c      error = GetL
f22f0 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20  astError();.    
f2300 20 20 4f 53 54 52 41 43 45 32 28 22 65 72 72 6f    OSTRACE2("erro
f2310 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20  r-code = %d\n", 
f2320 65 72 72 6f 72 29 3b 0a 20 20 20 20 20 20 67 65  error);.      ge
f2330 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
f2340 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
f2350 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64  * If we are hold
f2360 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  ing a PENDING lo
f2370 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f  ck that ought to
f2380 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68   be released, th
f2390 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20  en.  ** release 
f23a0 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69  it now..  */.  i
f23b0 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63  f( gotPendingLoc
f23c0 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  k && locktype==S
f23d0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
f23e0 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69    UnlockFile(pFi
f23f0 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42  le->h, PENDING_B
f2400 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  YTE, 0, 1, 0);. 
f2410 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20   }..  /* Update 
f2420 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65  the state of the
f2430 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69   lock has held i
f2440 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  n the file descr
f2450 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20  iptor then.  ** 
f2460 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f  return the appro
f2470 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f  priate result co
f2480 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72  de..  */.  if( r
f2490 65 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  es ){.    rc = S
f24a0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
f24b0 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28  e{.    OSTRACE4(
f24c0 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20  "LOCK FAILED %d 
f24d0 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75  trying for %d bu
f24e0 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69  t got %d\n", pFi
f24f0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
f2500 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c    locktype, newL
f2510 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 70 46  ocktype);.    pF
f2520 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
f2530 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 63 20 3d   error;.    rc =
f2540 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
f2550 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  }.  pFile->lockt
f2560 79 70 65 20 3d 20 28 75 38 29 6e 65 77 4c 6f 63  ype = (u8)newLoc
f2570 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20  ktype;.  return 
f2580 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
f2590 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
f25a0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
f25b0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
f25c0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
f25d0 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  ed.** file by th
f25e0 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
f25f0 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
f2600 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
f2610 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a   return.** non-z
f2620 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a  ero, otherwise z
f2630 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ero..*/.static i
f2640 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 73 65 72  nt winCheckReser
f2650 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
f2660 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
f2670 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72  ResOut){.  int r
f2680 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  c;.  winFile *pF
f2690 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
f26a0 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  id;..  assert( i
f26b0 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46  d!=0 );.  if( pF
f26c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52  ile->locktype>=R
f26d0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a  ESERVED_LOCK ){.
f26e0 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20      rc = 1;.    
f26f0 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57  OSTRACE3("TEST W
f2700 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f  R-LOCK %d %d (lo
f2710 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  cal)\n", pFile->
f2720 68 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73 65 7b  h, rc);.  }else{
f2730 0a 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b 46 69  .    rc = LockFi
f2740 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53  le(pFile->h, RES
f2750 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31  ERVED_BYTE, 0, 1
f2760 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
f2770 20 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b   ){.      Unlock
f2780 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52  File(pFile->h, R
f2790 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c  ESERVED_BYTE, 0,
f27a0 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20   1, 0);.    }.  
f27b0 20 20 72 63 20 3d 20 21 72 63 3b 0a 20 20 20 20    rc = !rc;.    
f27c0 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 20 57  OSTRACE3("TEST W
f27d0 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65  R-LOCK %d %d (re
f27e0 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  mote)\n", pFile-
f27f0 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20 2a  >h, rc);.  }.  *
f2800 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20  pResOut = rc;.  
f2810 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f2820 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72  ;.}../*.** Lower
f2830 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
f2840 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72  el on file descr
f2850 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b  iptor id to lock
f2860 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
f2870 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
f2880 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
f2890 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
f28a0 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
f28b0 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
f28c0 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
f28d0 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
f28e0 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
f28f0 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
f2900 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
f2910 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
f2920 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
f2930 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f  ible for this ro
f2940 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66  utine to fail if
f2950 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
f2960 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f  ment.** is NO_LO
f2970 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f  CK.  If the seco
f2980 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53  nd argument is S
f2990 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20  HARED_LOCK then 
f29a0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
f29b0 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c  might return SQL
f29c0 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74  ITE_IOERR;.*/.st
f29d0 61 74 69 63 20 69 6e 74 20 77 69 6e 55 6e 6c 6f  atic int winUnlo
f29e0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
f29f0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
f2a00 65 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a  e){.  int type;.
f2a10 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
f2a20 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
f2a30 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
f2a40 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28  TE_OK;.  assert(
f2a50 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61   pFile!=0 );.  a
f2a60 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
f2a70 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
f2a80 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f    OSTRACE5("UNLO
f2a90 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20  CK %d to %d was 
f2aa0 25 64 28 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65  %d(%d)\n", pFile
f2ab0 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20  ->h, locktype,. 
f2ac0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
f2ad0 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d  locktype, pFile-
f2ae0 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29  >sharedLockByte)
f2af0 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65  ;.  type = pFile
f2b00 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66  ->locktype;.  if
f2b10 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56  ( type>=EXCLUSIV
f2b20 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e  E_LOCK ){.    Un
f2b30 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
f2b40 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c  h, SHARED_FIRST,
f2b50 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c   0, SHARED_SIZE,
f2b60 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63   0);.    if( loc
f2b70 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
f2b80 43 4b 20 26 26 20 21 67 65 74 52 65 61 64 4c 6f  CK && !getReadLo
f2b90 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20  ck(pFile) ){.   
f2ba0 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c     /* This shoul
f2bb0 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e 20  d never happen. 
f2bc0 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79   We should alway
f2bd0 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20 20  s be able to.   
f2be0 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65 20     ** reacquire 
f2bf0 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f  the read lock */
f2c00 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
f2c10 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b  TE_IOERR_UNLOCK;
f2c20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
f2c30 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f   type>=RESERVED_
f2c40 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f  LOCK ){.    Unlo
f2c50 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
f2c60 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
f2c70 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  0, 1, 0);.  }.  
f2c80 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  if( locktype==NO
f2c90 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e 3d 53  _LOCK && type>=S
f2ca0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
f2cb0 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b    unlockReadLock
f2cc0 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20 20 69  (pFile);.  }.  i
f2cd0 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47  f( type>=PENDING
f2ce0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c  _LOCK ){.    Unl
f2cf0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
f2d00 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
f2d10 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  0, 1, 0);.  }.  
f2d20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
f2d30 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a  = (u8)locktype;.
f2d40 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
f2d50 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e  /*.** Control an
f2d60 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 6f  d query of the o
f2d70 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e  pen file handle.
f2d80 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
f2d90 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71  inFileControl(sq
f2da0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
f2db0 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41  int op, void *pA
f2dc0 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f  rg){.  switch( o
f2dd0 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  p ){.    case SQ
f2de0 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
f2df0 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28  TATE: {.      *(
f2e00 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 77 69  int*)pArg = ((wi
f2e10 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b  nFile*)id)->lock
f2e20 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 74 75  type;.      retu
f2e30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
f2e40 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
f2e50 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20  ITE_LAST_ERRNO: 
f2e60 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70  {.      *(int*)p
f2e70 41 72 67 20 3d 20 28 69 6e 74 29 28 28 77 69 6e  Arg = (int)((win
f2e80 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45  File*)id)->lastE
f2e90 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75  rrno;.      retu
f2ea0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
f2eb0 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
f2ec0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d   SQLITE_ERROR;.}
f2ed0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
f2ee0 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69  he sector size i
f2ef0 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75  n bytes of the u
f2f00 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20  nderlying block 
f2f10 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68  device for.** th
f2f20 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65  e specified file
f2f30 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74  . This is almost
f2f40 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65   always 512 byte
f2f50 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a  s, but may be.**
f2f60 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65   larger for some
f2f70 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20   devices..**.** 
f2f80 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75  SQLite code assu
f2f90 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  mes this functio
f2fa0 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49  n cannot fail. I
f2fb0 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74  t also assumes t
f2fc0 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69  hat.** if two fi
f2fd0 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20  les are created 
f2fe0 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  in the same file
f2ff0 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72  -system director
f3000 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74  y (i.e..** a dat
f3010 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f  abase and its jo
f3020 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74  urnal file) that
f3030 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
f3040 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20   will be the.** 
f3050 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a  same for both..*
f3060 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
f3070 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74  SectorSize(sqlit
f3080 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20  e3_file *id){.  
f3090 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
f30a0 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28  .  return (int)(
f30b0 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e  ((winFile*)id)->
f30c0 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 7d 0a 0a  sectorSize);.}..
f30d0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 76  /*.** Return a v
f30e0 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65 20  ector of device 
f30f0 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 2e  characteristics.
f3100 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
f3110 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65  inDeviceCharacte
f3120 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f  ristics(sqlite3_
f3130 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 55 4e 55  file *id){.  UNU
f3140 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 64  SED_PARAMETER(id
f3150 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  );.  return 0;.}
f3160 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63  ../*.** This vec
f3170 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20  tor defines all 
f3180 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74  the methods that
f3190 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20   can operate on 
f31a0 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69  an.** sqlite3_fi
f31b0 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e 0a 2a 2f  le for win32..*/
f31c0 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
f31d0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
f31e0 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 3d 20 7b   winIoMethod = {
f31f0 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20  .  1,           
f3200 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f3210 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 77 69  iVersion */.  wi
f3220 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e 52 65 61  nClose,.  winRea
f3230 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 2c 0a 20  d,.  winWrite,. 
f3240 20 77 69 6e 54 72 75 6e 63 61 74 65 2c 0a 20 20   winTruncate,.  
f3250 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 6e 46 69  winSync,.  winFi
f3260 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e 4c 6f 63  leSize,.  winLoc
f3270 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 6b 2c 0a  k,.  winUnlock,.
f3280 20 20 77 69 6e 43 68 65 63 6b 52 65 73 65 72 76    winCheckReserv
f3290 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e 46 69 6c  edLock,.  winFil
f32a0 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 69 6e 53  eControl,.  winS
f32b0 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 77 69 6e  ectorSize,.  win
f32c0 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
f32d0 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a  stics.};../*****
f32e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f32f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3320 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65  ******.** Here e
f3330 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65 74 68  nds the I/O meth
f3340 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68  ods that form th
f3350 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  e sqlite3_io_met
f3360 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  hods object..**.
f3370 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c 6f 63  ** The next bloc
f3380 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d  k of code implem
f3390 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d 65 74  ents the VFS met
f33a0 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  hods..**********
f33b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f33c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f33d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f33e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f33f0 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  **/../*.** Conve
f3400 72 74 20 61 20 55 54 46 2d 38 20 66 69 6c 65 6e  rt a UTF-8 filen
f3410 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 65 76 65  ame into whateve
f3420 72 20 66 6f 72 6d 20 74 68 65 20 75 6e 64 65 72  r form the under
f3430 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69  lying.** operati
f3440 6e 67 20 73 79 73 74 65 6d 20 77 61 6e 74 73 20  ng system wants 
f3450 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e 20 20 53  filenames in.  S
f3460 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
f3470 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 6f 62   result.** is ob
f3480 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
f3490 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66  oc and must be f
f34a0 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  reed by the call
f34b0 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e  ing.** function.
f34c0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
f34d0 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65  *convertUtf8File
f34e0 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 61 72 20  name(const char 
f34f0 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 76  *zFilename){.  v
f3500 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20  oid *zConverted 
f3510 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 4e 54 28  = 0;.  if( isNT(
f3520 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72  ) ){.    zConver
f3530 74 65 64 20 3d 20 75 74 66 38 54 6f 55 6e 69 63  ted = utf8ToUnic
f3540 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ode(zFilename);.
f3550 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
f3560 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
f3570 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
f3580 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
f3590 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51  uted. .*/.#if SQ
f35a0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
f35b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43  .  }else{.    zC
f35c0 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66 38 54  onverted = utf8T
f35d0 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 6d 65 29  oMbcs(zFilename)
f35e0 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 2f  ;.#endif.  }.  /
f35f0 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 68 61  * caller will ha
f3600 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d 65 6d 6f  ndle out of memo
f3610 72 79 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 7a  ry */.  return z
f3620 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a 0a 2f 2a  Converted;.}../*
f3630 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d  .** Create a tem
f3640 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
f3650 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20   in zBuf.  zBuf 
f3660 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75  must be big enou
f3670 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74  gh to.** hold at
f3680 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
f3690 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f  e characters..*/
f36a0 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54  .static int getT
f36b0 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66  empname(int nBuf
f36c0 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
f36d0 20 73 74 61 74 69 63 20 63 68 61 72 20 7a 43 68   static char zCh
f36e0 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63  ars[] =.    "abc
f36f0 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73  defghijklmnopqrs
f3700 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42  tuvwxyz".    "AB
f3710 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52  CDEFGHIJKLMNOPQR
f3720 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30  STUVWXYZ".    "0
f3730 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 73 69  123456789";.  si
f3740 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61  ze_t i, j;.  cha
f3750 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d 41 58 5f  r zTempPath[MAX_
f3760 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66 28 20 73  PATH+1];.  if( s
f3770 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
f3780 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 73 71 6c  ctory ){.    sql
f3790 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41  ite3_snprintf(MA
f37a0 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70  X_PATH-30, zTemp
f37b0 50 61 74 68 2c 20 22 25 73 22 2c 20 73 71 6c 69  Path, "%s", sqli
f37c0 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
f37d0 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ry);.  }else if(
f37e0 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 63   isNT() ){.    c
f37f0 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20 20 20  har *zMulti;.   
f3800 20 57 43 48 41 52 20 7a 57 69 64 65 50 61 74 68   WCHAR zWidePath
f3810 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20  [MAX_PATH];.    
f3820 47 65 74 54 65 6d 70 50 61 74 68 57 28 4d 41 58  GetTempPathW(MAX
f3830 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 64 65 50  _PATH-30, zWideP
f3840 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c 74 69  ath);.    zMulti
f3850 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38   = unicodeToUtf8
f3860 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20 20  (zWidePath);.   
f3870 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b 0a 20   if( zMulti ){. 
f3880 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
f3890 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33  rintf(MAX_PATH-3
f38a0 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25  0, zTempPath, "%
f38b0 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20 20 20  s", zMulti);.   
f38c0 20 20 20 66 72 65 65 28 7a 4d 75 6c 74 69 29 3b     free(zMulti);
f38d0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
f38e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f38f0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 2f 2a 20  NOMEM;.    }./* 
f3900 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
f3910 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
f3920 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
f3930 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
f3940 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65  d. .** Since the
f3950 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f   ASCII version o
f3960 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20  f these Windows 
f3970 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  API do not exist
f3980 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69   for WINCE,.** i
f3990 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  t's important to
f39a0 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74   not reference t
f39b0 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75  hem for WINCE bu
f39c0 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ilds..*/.#if SQL
f39d0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
f39e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61    }else{.    cha
f39f0 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 20 63 68  r *zUtf8;.    ch
f3a00 61 72 20 7a 4d 62 63 73 50 61 74 68 5b 4d 41 58  ar zMbcsPath[MAX
f3a10 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 74 54  _PATH];.    GetT
f3a20 65 6d 70 50 61 74 68 41 28 4d 41 58 5f 50 41 54  empPathA(MAX_PAT
f3a30 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74 68 29  H-30, zMbcsPath)
f3a40 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d 20 73 71  ;.    zUtf8 = sq
f3a50 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73  lite3_win32_mbcs
f3a60 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63 73 50 61  _to_utf8(zMbcsPa
f3a70 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 55 74  th);.    if( zUt
f3a80 66 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  f8 ){.      sqli
f3a90 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58  te3_snprintf(MAX
f3aa0 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50  _PATH-30, zTempP
f3ab0 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 74 66 38  ath, "%s", zUtf8
f3ac0 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 55  );.      free(zU
f3ad0 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  tf8);.    }else{
f3ae0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
f3af0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
f3b00 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66  }.#endif.  }.  f
f3b10 6f 72 28 69 3d 73 71 6c 69 74 65 33 53 74 72 6c  or(i=sqlite3Strl
f3b20 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b  en30(zTempPath);
f3b30 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50 61 74   i>0 && zTempPat
f3b40 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20 69 2d  h[i-1]=='\\'; i-
f3b50 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68  -){}.  zTempPath
f3b60 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  [i] = 0;.  sqlit
f3b70 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
f3b80 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20  -30, zBuf,.     
f3b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25                "%
f3ba0 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f  s\\"SQLITE_TEMP_
f3bb0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65  FILE_PREFIX, zTe
f3bc0 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d 20 73  mpPath);.  j = s
f3bd0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
f3be0 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Buf);.  sqlite3_
f3bf0 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 2c 20 26  randomness(20, &
f3c00 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 6f 72 28  zBuf[j]);.  for(
f3c10 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b 2b 2c 20  i=0; i<20; i++, 
f3c20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a  j++){.    zBuf[j
f3c30 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73  ] = (char)zChars
f3c40 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  [ ((unsigned cha
f3c50 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65  r)zBuf[j])%(size
f3c60 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b  of(zChars)-1) ];
f3c70 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d  .  }.  zBuf[j] =
f3c80 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22   0;.  OSTRACE2("
f3c90 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25  TEMP FILENAME: %
f3ca0 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20 72  s\n", zBuf);.  r
f3cb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f3cc0 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72   .}../*.** The r
f3cd0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 67  eturn value of g
f3ce0 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 0a 2a  etLastErrorMsg.*
f3cf0 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74 68 65  * is zero if the
f3d00 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66   error message f
f3d10 69 74 73 20 69 6e 20 74 68 65 20 62 75 66 66 65  its in the buffe
f3d20 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a  r, or non-zero.*
f3d30 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69 66 20  * otherwise (if 
f3d40 74 68 65 20 6d 65 73 73 61 67 65 20 77 61 73 20  the message was 
f3d50 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f 0a 73  truncated)..*/.s
f3d60 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c 61 73  tatic int getLas
f3d70 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 20 6e 42  tErrorMsg(int nB
f3d80 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b  uf, char *zBuf){
f3d90 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d  .  DWORD error =
f3da0 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b   GetLastError();
f3db0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
f3dc0 57 49 4e 43 45 0a 20 20 73 71 6c 69 74 65 33 5f  WINCE.  sqlite3_
f3dd0 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a  snprintf(nBuf, z
f3de0 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 78  Buf, "OsError 0x
f3df0 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72 2c  %x (%u)", error,
f3e00 20 65 72 72 6f 72 29 3b 0a 23 65 6c 73 65 0a 20   error);.#else. 
f3e10 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 73 73 61 67   /* FormatMessag
f3e20 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e 20 66  e returns 0 on f
f3e30 61 69 6c 75 72 65 2e 20 20 4f 74 68 65 72 77 69  ailure.  Otherwi
f3e40 73 65 20 69 74 0a 20 20 2a 2a 20 72 65 74 75 72  se it.  ** retur
f3e50 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
f3e60 20 54 43 48 41 52 73 20 77 72 69 74 74 65 6e 20   TCHARs written 
f3e70 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20  to the output.  
f3e80 2a 2a 20 62 75 66 66 65 72 2c 20 65 78 63 6c 75  ** buffer, exclu
f3e90 64 69 6e 67 20 74 68 65 20 74 65 72 6d 69 6e 61  ding the termina
f3ea0 74 69 6e 67 20 6e 75 6c 6c 20 63 68 61 72 2e 0a  ting null char..
f3eb0 20 20 2a 2f 0a 20 20 69 66 20 28 21 46 6f 72 6d    */.  if (!Form
f3ec0 61 74 4d 65 73 73 61 67 65 41 28 46 4f 52 4d 41  atMessageA(FORMA
f3ed0 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53  T_MESSAGE_FROM_S
f3ee0 59 53 54 45 4d 2c 0a 20 20 20 20 20 20 20 20 20  YSTEM,.         
f3ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c               NUL
f3f00 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  L,.             
f3f10 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c 0a           error,.
f3f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3f30 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20        0,.       
f3f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
f3f50 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Buf,.           
f3f60 20 20 20 20 20 20 20 20 20 20 20 6e 42 75 66 2d             nBuf-
f3f70 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
f3f80 20 20 20 20 20 20 20 20 20 30 29 29 0a 20 20 7b           0)).  {
f3f90 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
f3fa0 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66  rintf(nBuf, zBuf
f3fb0 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20  , "OsError 0x%x 
f3fc0 28 25 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 72  (%u)", error, er
f3fd0 72 6f 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ror);.  }.#endif
f3fe0 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ..  return 0;.}.
f3ff0 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69  ./*.** Open a fi
f4000 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
f4010 74 20 77 69 6e 4f 70 65 6e 28 0a 20 20 73 71 6c  t winOpen(.  sql
f4020 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
f4030 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
f4040 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
f4050 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
f4060 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
f4070 20 66 69 6c 65 20 28 55 54 46 2d 38 29 20 2a 2f   file (UTF-8) */
f4080 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
f4090 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  *id,         /* 
f40a0 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 74 65  Write the SQLite
f40b0 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 65 72   file handle her
f40c0 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
f40d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
f40e0 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c   /* Open mode fl
f40f0 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f  ags */.  int *pO
f4100 75 74 46 6c 61 67 73 20 20 20 20 20 20 20 20 20  utFlags         
f4110 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74     /* Status ret
f4120 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a  urn flags */.){.
f4130 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 44 57    HANDLE h;.  DW
f4140 4f 52 44 20 64 77 44 65 73 69 72 65 64 41 63 63  ORD dwDesiredAcc
f4150 65 73 73 3b 0a 20 20 44 57 4f 52 44 20 64 77 53  ess;.  DWORD dwS
f4160 68 61 72 65 4d 6f 64 65 3b 0a 20 20 44 57 4f 52  hareMode;.  DWOR
f4170 44 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70  D dwCreationDisp
f4180 6f 73 69 74 69 6f 6e 3b 0a 20 20 44 57 4f 52 44  osition;.  DWORD
f4190 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69   dwFlagsAndAttri
f41a0 62 75 74 65 73 20 3d 20 30 3b 0a 23 69 66 20 53  butes = 0;.#if S
f41b0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
f41c0 20 69 6e 74 20 69 73 54 65 6d 70 20 3d 20 30 3b   int isTemp = 0;
f41d0 0a 23 65 6e 64 69 66 0a 20 20 77 69 6e 46 69 6c  .#endif.  winFil
f41e0 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46  e *pFile = (winF
f41f0 69 6c 65 2a 29 69 64 3b 0a 20 20 76 6f 69 64 20  ile*)id;.  void 
f4200 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 20 20 20 20  *zConverted;    
f4210 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f4220 46 69 6c 65 6e 61 6d 65 20 69 6e 20 4f 53 20 65  Filename in OS e
f4230 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e  ncoding */.  con
f4240 73 74 20 63 68 61 72 20 2a 7a 55 74 66 38 4e 61  st char *zUtf8Na
f4250 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20 20 20 20 2f  me = zName;    /
f4260 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 55 54  * Filename in UT
f4270 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a  F-8 encoding */.
f4280 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b    char zTmpname[
f4290 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 20 20 20 20  MAX_PATH+1];    
f42a0 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 75 73      /* Buffer us
f42b0 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 65 6d  ed to create tem
f42c0 70 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 0a 20  p filename */.. 
f42d0 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
f42e0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
f42f0 45 54 45 52 28 70 56 66 73 29 3b 0a 0a 20 20 2f  ETER(pVfs);..  /
f4300 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20  * If the second 
f4310 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73  argument to this
f4320 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c   function is NUL
f4330 4c 2c 20 67 65 6e 65 72 61 74 65 20 61 20 0a 20  L, generate a . 
f4340 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69   ** temporary fi
f4350 6c 65 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a  le name to use .
f4360 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 55 74 66    */.  if( !zUtf
f4370 38 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74  8Name ){.    int
f4380 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d   rc = getTempnam
f4390 65 28 4d 41 58 5f 50 41 54 48 2b 31 2c 20 7a 54  e(MAX_PATH+1, zT
f43a0 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28  mpname);.    if(
f43b0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
f43c0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
f43d0 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 55 74  c;.    }.    zUt
f43e0 66 38 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d  f8Name = zTmpnam
f43f0 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e  e;.  }..  /* Con
f4400 76 65 72 74 20 74 68 65 20 66 69 6c 65 6e 61 6d  vert the filenam
f4410 65 20 74 6f 20 74 68 65 20 73 79 73 74 65 6d 20  e to the system 
f4420 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 7a  encoding. */.  z
f4430 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76  Converted = conv
f4440 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28  ertUtf8Filename(
f4450 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 20 20 69 66  zUtf8Name);.  if
f4460 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20  ( zConverted==0 
f4470 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
f4480 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
f4490 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  .  if( flags & S
f44a0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
f44b0 52 49 54 45 20 29 7b 0a 20 20 20 20 64 77 44 65  RITE ){.    dwDe
f44c0 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 47 45  siredAccess = GE
f44d0 4e 45 52 49 43 5f 52 45 41 44 20 7c 20 47 45 4e  NERIC_READ | GEN
f44e0 45 52 49 43 5f 57 52 49 54 45 3b 0a 20 20 7d 65  ERIC_WRITE;.  }e
f44f0 6c 73 65 7b 0a 20 20 20 20 64 77 44 65 73 69 72  lse{.    dwDesir
f4500 65 64 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52  edAccess = GENER
f4510 49 43 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 2f  IC_READ;.  }.  /
f4520 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58  * SQLITE_OPEN_EX
f4530 43 4c 55 53 49 56 45 20 69 73 20 75 73 65 64 20  CLUSIVE is used 
f4540 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
f4550 74 20 61 20 6e 65 77 20 66 69 6c 65 20 69 73 20  t a new file is 
f4560 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 2e 20 53  .  ** created. S
f4570 51 4c 69 74 65 20 64 6f 65 73 6e 27 74 20 75 73  QLite doesn't us
f4580 65 20 69 74 20 74 6f 20 69 6e 64 69 63 61 74 65  e it to indicate
f4590 20 22 65 78 63 6c 75 73 69 76 65 20 61 63 63 65   "exclusive acce
f45a0 73 73 22 20 0a 20 20 2a 2a 20 61 73 20 69 74 20  ss" .  ** as it 
f45b0 69 73 20 75 73 75 61 6c 6c 79 20 75 6e 64 65 72  is usually under
f45c0 73 74 6f 6f 64 2e 0a 20 20 2a 2f 0a 20 20 61 73  stood..  */.  as
f45d0 73 65 72 74 28 21 28 66 6c 61 67 73 20 26 20 53  sert(!(flags & S
f45e0 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
f45f0 53 49 56 45 29 20 7c 7c 20 28 66 6c 61 67 73 20  SIVE) || (flags 
f4600 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  & SQLITE_OPEN_CR
f4610 45 41 54 45 29 29 3b 0a 20 20 69 66 28 20 66 6c  EATE));.  if( fl
f4620 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
f4630 4e 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20  N_EXCLUSIVE ){. 
f4640 20 20 20 2f 2a 20 43 72 65 61 74 65 73 20 61 20     /* Creates a 
f4650 6e 65 77 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69  new file, only i
f4660 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c  f it does not al
f4670 72 65 61 64 79 20 65 78 69 73 74 2e 20 2a 2f 0a  ready exist. */.
f4680 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69      /* If the fi
f4690 6c 65 20 65 78 69 73 74 73 2c 20 69 74 20 66 61  le exists, it fa
f46a0 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 64 77 43 72  ils. */.    dwCr
f46b0 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
f46c0 6e 20 3d 20 43 52 45 41 54 45 5f 4e 45 57 3b 0a  n = CREATE_NEW;.
f46d0 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67    }else if( flag
f46e0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
f46f0 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 2f 2a  CREATE ){.    /*
f4700 20 4f 70 65 6e 20 65 78 69 73 74 69 6e 67 20 66   Open existing f
f4710 69 6c 65 2c 20 6f 72 20 63 72 65 61 74 65 20 69  ile, or create i
f4720 66 20 69 74 20 64 6f 65 73 6e 27 74 20 65 78 69  f it doesn't exi
f4730 73 74 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61  st */.    dwCrea
f4740 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20  tionDisposition 
f4750 3d 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b 0a 20  = OPEN_ALWAYS;. 
f4760 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4f   }else{.    /* O
f4770 70 65 6e 73 20 61 20 66 69 6c 65 2c 20 6f 6e 6c  pens a file, onl
f4780 79 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 20  y if it exists. 
f4790 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f  */.    dwCreatio
f47a0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f  nDisposition = O
f47b0 50 45 4e 5f 45 58 49 53 54 49 4e 47 3b 0a 20 20  PEN_EXISTING;.  
f47c0 7d 0a 20 20 64 77 53 68 61 72 65 4d 6f 64 65 20  }.  dwShareMode 
f47d0 3d 20 46 49 4c 45 5f 53 48 41 52 45 5f 52 45 41  = FILE_SHARE_REA
f47e0 44 20 7c 20 46 49 4c 45 5f 53 48 41 52 45 5f 57  D | FILE_SHARE_W
f47f0 52 49 54 45 3b 0a 20 20 69 66 28 20 66 6c 61 67  RITE;.  if( flag
f4800 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
f4810 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b  DELETEONCLOSE ){
f4820 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
f4830 49 4e 43 45 0a 20 20 20 20 64 77 46 6c 61 67 73  INCE.    dwFlags
f4840 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20  AndAttributes = 
f4850 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48  FILE_ATTRIBUTE_H
f4860 49 44 44 45 4e 3b 0a 20 20 20 20 69 73 54 65 6d  IDDEN;.    isTem
f4870 70 20 3d 20 31 3b 0a 23 65 6c 73 65 0a 20 20 20  p = 1;.#else.   
f4880 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69   dwFlagsAndAttri
f4890 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54  butes = FILE_ATT
f48a0 52 49 42 55 54 45 5f 54 45 4d 50 4f 52 41 52 59  RIBUTE_TEMPORARY
f48b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f48c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f48d0 7c 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  | FILE_ATTRIBUTE
f48e0 5f 48 49 44 44 45 4e 0a 20 20 20 20 20 20 20 20  _HIDDEN.        
f48f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4900 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 46 4c         | FILE_FL
f4910 41 47 5f 44 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f  AG_DELETE_ON_CLO
f4920 53 45 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c  SE;.#endif.  }el
f4930 73 65 7b 0a 20 20 20 20 64 77 46 6c 61 67 73 41  se{.    dwFlagsA
f4940 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46  ndAttributes = F
f4950 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f  ILE_ATTRIBUTE_NO
f4960 52 4d 41 4c 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52  RMAL;.  }.  /* R
f4970 65 70 6f 72 74 73 20 66 72 6f 6d 20 74 68 65 20  eports from the 
f4980 69 6e 74 65 72 6e 65 74 20 61 72 65 20 74 68 61  internet are tha
f4990 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73  t performance is
f49a0 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 62 65 74   always.  ** bet
f49b0 74 65 72 20 69 66 20 46 49 4c 45 5f 46 4c 41 47  ter if FILE_FLAG
f49c0 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53 20 69  _RANDOM_ACCESS i
f49d0 73 20 75 73 65 64 2e 20 20 54 69 63 6b 65 74 20  s used.  Ticket 
f49e0 23 32 36 39 39 2e 20 2a 2f 0a 23 69 66 20 53 51  #2699. */.#if SQ
f49f0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
f4a00 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
f4a10 75 74 65 73 20 7c 3d 20 46 49 4c 45 5f 46 4c 41  utes |= FILE_FLA
f4a20 47 5f 52 41 4e 44 4f 4d 5f 41 43 43 45 53 53 3b  G_RANDOM_ACCESS;
f4a30 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69 73  .#endif.  if( is
f4a40 4e 54 28 29 20 29 7b 0a 20 20 20 20 68 20 3d 20  NT() ){.    h = 
f4a50 43 72 65 61 74 65 46 69 6c 65 57 28 28 57 43 48  CreateFileW((WCH
f4a60 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a  AR*)zConverted,.
f4a70 20 20 20 20 20 20 20 64 77 44 65 73 69 72 65 64         dwDesired
f4a80 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20 64  Access,.       d
f4a90 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20  wShareMode,.    
f4aa0 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20     NULL,.       
f4ab0 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73  dwCreationDispos
f4ac0 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77  ition,.       dw
f4ad0 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74  FlagsAndAttribut
f4ae0 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a  es,.       NULL.
f4af0 20 20 20 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29      );./* isNT()
f4b00 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
f4b10 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
f4b20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
f4b30 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a  er executed. .**
f4b40 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49   Since the ASCII
f4b50 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73   version of thes
f4b60 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f  e Windows API do
f4b70 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57   not exist for W
f4b80 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d  INCE,.** it's im
f4b90 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72  portant to not r
f4ba0 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f  eference them fo
f4bb0 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a  r WINCE builds..
f4bc0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f4bd0 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73  _WINCE==0.  }els
f4be0 65 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61 74  e{.    h = Creat
f4bf0 65 46 69 6c 65 41 28 28 63 68 61 72 2a 29 7a 43  eFileA((char*)zC
f4c00 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20  onverted,.      
f4c10 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73   dwDesiredAccess
f4c20 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61 72 65  ,.       dwShare
f4c30 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c  Mode,.       NUL
f4c40 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72 65 61  L,.       dwCrea
f4c50 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c  tionDisposition,
f4c60 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67 73 41  .       dwFlagsA
f4c70 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a 20 20  ndAttributes,.  
f4c80 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b       NULL.    );
f4c90 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66  .#endif.  }.  if
f4ca0 28 20 68 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e  ( h==INVALID_HAN
f4cb0 44 4c 45 5f 56 41 4c 55 45 20 29 7b 0a 20 20 20  DLE_VALUE ){.   
f4cc0 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64   free(zConverted
f4cd0 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  );.    if( flags
f4ce0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
f4cf0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20  EADWRITE ){.    
f4d00 20 20 72 65 74 75 72 6e 20 77 69 6e 4f 70 65 6e    return winOpen
f4d10 28 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64  (pVfs, zName, id
f4d20 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
f4d30 28 28 66 6c 61 67 73 7c 53 51 4c 49 54 45 5f 4f  ((flags|SQLITE_O
f4d40 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 26 7e 53  PEN_READONLY)&~S
f4d50 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
f4d60 52 49 54 45 29 2c 20 70 4f 75 74 46 6c 61 67 73  RITE), pOutFlags
f4d70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
f4d80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
f4d90 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20  E_CANTOPEN;.    
f4da0 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74  }.  }.  if( pOut
f4db0 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 66 28  Flags ){.    if(
f4dc0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
f4dd0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29  OPEN_READWRITE )
f4de0 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61  {.      *pOutFla
f4df0 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs = SQLITE_OPEN
f4e00 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20  _READWRITE;.    
f4e10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 4f  }else{.      *pO
f4e20 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45  utFlags = SQLITE
f4e30 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a  _OPEN_READONLY;.
f4e40 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73      }.  }.  mems
f4e50 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a  et(pFile, 0, siz
f4e60 65 6f 66 28 2a 70 46 69 6c 65 29 29 3b 0a 20 20  eof(*pFile));.  
f4e70 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d  pFile->pMethod =
f4e80 20 26 77 69 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20   &winIoMethod;. 
f4e90 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20   pFile->h = h;. 
f4ea0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
f4eb0 6f 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  o = NO_ERROR;.  
f4ec0 70 46 69 6c 65 2d 3e 73 65 63 74 6f 72 53 69 7a  pFile->sectorSiz
f4ed0 65 20 3d 20 67 65 74 53 65 63 74 6f 72 53 69 7a  e = getSectorSiz
f4ee0 65 28 70 56 66 73 2c 20 7a 55 74 66 38 4e 61 6d  e(pVfs, zUtf8Nam
f4ef0 65 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  e);.#if SQLITE_O
f4f00 53 5f 57 49 4e 43 45 0a 20 20 69 66 28 20 28 66  S_WINCE.  if( (f
f4f10 6c 61 67 73 20 26 20 28 53 51 4c 49 54 45 5f 4f  lags & (SQLITE_O
f4f20 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51  PEN_READWRITE|SQ
f4f30 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
f4f40 42 29 29 20 3d 3d 0a 20 20 20 20 20 20 20 20 20  B)) ==.         
f4f50 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50        (SQLITE_OP
f4f60 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c  EN_READWRITE|SQL
f4f70 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
f4f80 29 0a 20 20 20 20 20 20 20 26 26 20 21 77 69 6e  ).       && !win
f4f90 63 65 43 72 65 61 74 65 4c 6f 63 6b 28 7a 4e 61  ceCreateLock(zNa
f4fa0 6d 65 2c 20 70 46 69 6c 65 29 0a 20 20 29 7b 0a  me, pFile).  ){.
f4fb0 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28      CloseHandle(
f4fc0 68 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f  h);.    free(zCo
f4fd0 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 72 65  nverted);.    re
f4fe0 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54  turn SQLITE_CANT
f4ff0 4f 50 45 4e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OPEN;.  }.  if( 
f5000 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 70 46  isTemp ){.    pF
f5010 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c  ile->zDeleteOnCl
f5020 6f 73 65 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64  ose = zConverted
f5030 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  ;.  }else.#endif
f5040 0a 20 20 7b 0a 20 20 20 20 66 72 65 65 28 7a 43  .  {.    free(zC
f5050 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 7d 0a 20  onverted);.  }. 
f5060 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29   OpenCounter(+1)
f5070 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
f5080 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
f5090 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 64 20  elete the named 
f50a0 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  file..**.** Note
f50b0 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 64 6f   that windows do
f50c0 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 66  es not allow a f
f50d0 69 6c 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65  ile to be delete
f50e0 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 0a  d if some other.
f50f0 2a 2a 20 70 72 6f 63 65 73 73 20 68 61 73 20 69  ** process has i
f5100 74 20 6f 70 65 6e 2e 20 20 53 6f 6d 65 74 69 6d  t open.  Sometim
f5110 65 73 20 61 20 76 69 72 75 73 20 73 63 61 6e 6e  es a virus scann
f5120 65 72 20 6f 72 20 69 6e 64 65 78 69 6e 67 20 70  er or indexing p
f5130 72 6f 67 72 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f  rogram.** will o
f5140 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  pen a journal fi
f5150 6c 65 20 73 68 6f 72 74 6c 79 20 61 66 74 65 72  le shortly after
f5160 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20 69   it is created i
f5170 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 0a 2a 2a  n order to do.**
f5180 20 77 68 61 74 65 76 65 72 20 69 74 20 64 6f 65   whatever it doe
f5190 73 2e 20 20 57 68 69 6c 65 20 74 68 69 73 20 6f  s.  While this o
f51a0 74 68 65 72 20 70 72 6f 63 65 73 73 20 69 73 20  ther process is 
f51b0 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66  holding the.** f
f51c0 69 6c 65 20 6f 70 65 6e 2c 20 77 65 20 77 69 6c  ile open, we wil
f51d0 6c 20 62 65 20 75 6e 61 62 6c 65 20 74 6f 20 64  l be unable to d
f51e0 65 6c 65 74 65 20 69 74 2e 20 20 54 6f 20 77 6f  elete it.  To wo
f51f0 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 0a 2a  rk around this.*
f5200 2a 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 64 65  * problem, we de
f5210 6c 61 79 20 31 30 30 20 6d 69 6c 6c 69 73 65 63  lay 100 millisec
f5220 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 74 6f 20  onds and try to 
f5230 64 65 6c 65 74 65 20 61 67 61 69 6e 2e 20 20 55  delete again.  U
f5240 70 0a 2a 2a 20 74 6f 20 4d 58 5f 44 45 4c 45 54  p.** to MX_DELET
f5250 49 4f 4e 5f 41 54 54 45 4d 50 54 73 20 64 65 6c  ION_ATTEMPTs del
f5260 65 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 61  etion attempts a
f5270 72 65 20 72 75 6e 20 62 65 66 6f 72 65 20 67 69  re run before gi
f5280 76 69 6e 67 0a 2a 2a 20 75 70 20 61 6e 64 20 72  ving.** up and r
f5290 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f  eturning an erro
f52a0 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58  r..*/.#define MX
f52b0 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50  _DELETION_ATTEMP
f52c0 54 53 20 35 0a 73 74 61 74 69 63 20 69 6e 74 20  TS 5.static int 
f52d0 77 69 6e 44 65 6c 65 74 65 28 0a 20 20 73 71 6c  winDelete(.  sql
f52e0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
f52f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20           /* Not 
f5300 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f  used on win32 */
f5310 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
f5320 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 20 2f  Filename,      /
f5330 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
f5340 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e  o delete */.  in
f5350 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20 20  t syncDir       
f5360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74            /* Not
f5370 20 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a   used on win32 *
f5380 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 6e 74 20 3d  /.){.  int cnt =
f5390 20 30 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a   0;.  DWORD rc;.
f53a0 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d 20    DWORD error = 
f53b0 30 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76  0;.  void *zConv
f53c0 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55  erted = convertU
f53d0 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c  tf8Filename(zFil
f53e0 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44  ename);.  UNUSED
f53f0 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
f5400 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
f5410 45 54 45 52 28 73 79 6e 63 44 69 72 29 3b 0a 20  ETER(syncDir);. 
f5420 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d   if( zConverted=
f5430 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
f5440 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
f5450 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45   }.  SimulateIOE
f5460 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49  rror(return SQLI
f5470 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29  TE_IOERR_DELETE)
f5480 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29  ;.  if( isNT() )
f5490 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20  {.    do{.      
f54a0 44 65 6c 65 74 65 46 69 6c 65 57 28 7a 43 6f 6e  DeleteFileW(zCon
f54b0 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d 77 68  verted);.    }wh
f54c0 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 63 20  ile(   (   ((rc 
f54d0 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  = GetFileAttribu
f54e0 74 65 73 57 28 7a 43 6f 6e 76 65 72 74 65 64 29  tesW(zConverted)
f54f0 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c  ) != INVALID_FIL
f5500 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a 20 20  E_ATTRIBUTES).  
f5510 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
f5520 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73  ((error = GetLas
f5530 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 52 52  tError()) == ERR
f5540 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 45 44  OR_ACCESS_DENIED
f5550 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26  )).           &&
f5560 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 45 4c   (++cnt < MX_DEL
f5570 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 29 0a  ETION_ATTEMPTS).
f5580 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 53             && (S
f5590 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 29 3b  leep(100), 1) );
f55a0 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
f55b0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f55c0 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
f55d0 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
f55e0 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65  cuted. .** Since
f55f0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69   the ASCII versi
f5600 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
f5610 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
f5620 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a  xist for WINCE,.
f5630 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e  ** it's importan
f5640 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e  t to not referen
f5650 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43  ce them for WINC
f5660 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  E builds..*/.#if
f5670 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f5680 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
f5690 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74   do{.      Delet
f56a0 65 46 69 6c 65 41 28 7a 43 6f 6e 76 65 72 74 65  eFileA(zConverte
f56b0 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20  d);.    }while( 
f56c0 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74    (   ((rc = Get
f56d0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41 28  FileAttributesA(
f56e0 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20  zConverted)) != 
f56f0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
f5700 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20  RIBUTES).       
f5710 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72          || ((err
f5720 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  or = GetLastErro
f5730 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43  r()) == ERROR_AC
f5740 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20  CESS_DENIED)).  
f5750 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63           && (++c
f5760 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e  nt < MX_DELETION
f5770 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20  _ATTEMPTS).     
f5780 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28        && (Sleep(
f5790 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 65 6e 64  100), 1) );.#end
f57a0 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43  if.  }.  free(zC
f57b0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 4f 53 54  onverted);.  OST
f57c0 52 41 43 45 32 28 22 44 45 4c 45 54 45 20 5c 22  RACE2("DELETE \"
f57d0 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61  %s\"\n", zFilena
f57e0 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 20  me);.  return ( 
f57f0 20 20 28 72 63 20 3d 3d 20 49 4e 56 41 4c 49 44    (rc == INVALID
f5800 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53  _FILE_ATTRIBUTES
f5810 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  ) .          && 
f5820 28 65 72 72 6f 72 20 3d 3d 20 45 52 52 4f 52 5f  (error == ERROR_
f5830 46 49 4c 45 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29  FILE_NOT_FOUND))
f5840 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
f5850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
f5860 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  TE;.}../*.** Che
f5870 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  ck the existance
f5880 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61   and status of a
f5890 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
f58a0 20 69 6e 74 20 77 69 6e 41 63 63 65 73 73 28 0a   int winAccess(.
f58b0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
f58c0 56 66 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Vfs,         /* 
f58d0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e 33  Not used on win3
f58e0 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  2 */.  const cha
f58f0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
f5900 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c    /* Name of fil
f5910 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20  e to check */.  
f5920 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
f5930 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79             /* Ty
f5940 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20 6d 61  pe of test to ma
f5950 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  ke on this file 
f5960 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75  */.  int *pResOu
f5970 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
f5980 2f 2a 20 4f 55 54 3a 20 52 65 73 75 6c 74 20 2a  /* OUT: Result *
f5990 2f 0a 29 7b 0a 20 20 44 57 4f 52 44 20 61 74 74  /.){.  DWORD att
f59a0 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  r;.  int rc = 0;
f59b0 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72  .  void *zConver
f59c0 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  ted = convertUtf
f59d0 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e  8Filename(zFilen
f59e0 61 6d 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ame);.  UNUSED_P
f59f0 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
f5a00 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64    if( zConverted
f5a10 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f5a20 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
f5a30 20 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29    }.  if( isNT()
f5a40 20 29 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 47   ){.    attr = G
f5a50 65 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73  etFileAttributes
f5a60 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65  W((WCHAR*)zConve
f5a70 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29  rted);./* isNT()
f5a80 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
f5a90 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
f5aa0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
f5ab0 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a  er executed. .**
f5ac0 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49   Since the ASCII
f5ad0 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73   version of thes
f5ae0 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f  e Windows API do
f5af0 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57   not exist for W
f5b00 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d  INCE,.** it's im
f5b10 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72  portant to not r
f5b20 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f  eference them fo
f5b30 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a  r WINCE builds..
f5b40 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f5b50 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73  _WINCE==0.  }els
f5b60 65 7b 0a 20 20 20 20 61 74 74 72 20 3d 20 47 65  e{.    attr = Ge
f5b70 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 41  tFileAttributesA
f5b80 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74  ((char*)zConvert
f5b90 65 64 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  ed);.#endif.  }.
f5ba0 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
f5bb0 64 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 6c  d);.  switch( fl
f5bc0 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ags ){.    case 
f5bd0 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
f5be0 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  AD:.    case SQL
f5bf0 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
f5c00 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 61 74  S:.      rc = at
f5c10 74 72 21 3d 49 4e 56 41 4c 49 44 5f 46 49 4c 45  tr!=INVALID_FILE
f5c20 5f 41 54 54 52 49 42 55 54 45 53 3b 0a 20 20 20  _ATTRIBUTES;.   
f5c30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
f5c40 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
f5c50 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20  _READWRITE:.    
f5c60 20 20 72 63 20 3d 20 28 61 74 74 72 20 26 20 46    rc = (attr & F
f5c70 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 52 45  ILE_ATTRIBUTE_RE
f5c80 41 44 4f 4e 4c 59 29 3d 3d 30 3b 0a 20 20 20 20  ADONLY)==0;.    
f5c90 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66    break;.    def
f5ca0 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65  ault:.      asse
f5cb0 72 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61  rt(!"Invalid fla
f5cc0 67 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20  gs argument");. 
f5cd0 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20   }.  *pResOut = 
f5ce0 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  rc;.  return SQL
f5cf0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ITE_OK;.}.../*.*
f5d00 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76  * Turn a relativ
f5d10 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20  e pathname into 
f5d20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e  a full pathname.
f5d30 20 20 57 72 69 74 65 20 74 68 65 20 66 75 6c 6c    Write the full
f5d40 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69 6e 74  .** pathname int
f5d50 6f 20 7a 4f 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b  o zOut[].  zOut[
f5d60 5d 20 77 69 6c 6c 20 62 65 20 61 74 20 6c 65 61  ] will be at lea
f5d70 73 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  st pVfs->mxPathn
f5d80 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20  ame.** bytes in 
f5d90 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  size..*/.static 
f5da0 69 6e 74 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e  int winFullPathn
f5db0 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ame(.  sqlite3_v
f5dc0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
f5dd0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
f5de0 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f  to vfs object */
f5df0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
f5e00 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20 20 20  Relative,       
f5e10 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c   /* Possibly rel
f5e20 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68  ative input path
f5e30 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c   */.  int nFull,
f5e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f5e50 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f      /* Size of o
f5e60 75 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20  utput buffer in 
f5e70 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20  bytes */.  char 
f5e80 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 20 20  *zFull          
f5e90 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
f5ea0 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a  ut buffer */.){.
f5eb0 20 20 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f    .#if defined(_
f5ec0 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 55 4e 55  _CYGWIN__).  UNU
f5ed0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 46  SED_PARAMETER(nF
f5ee0 75 6c 6c 29 3b 0a 20 20 63 79 67 77 69 6e 5f 63  ull);.  cygwin_c
f5ef0 6f 6e 76 5f 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33  onv_to_full_win3
f5f00 32 5f 70 61 74 68 28 7a 52 65 6c 61 74 69 76 65  2_path(zRelative
f5f10 2c 20 7a 46 75 6c 6c 29 3b 0a 20 20 72 65 74 75  , zFull);.  retu
f5f20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65  rn SQLITE_OK;.#e
f5f30 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45  ndif..#if SQLITE
f5f40 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53  _OS_WINCE.  UNUS
f5f50 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75  ED_PARAMETER(nFu
f5f60 6c 6c 29 3b 0a 20 20 2f 2a 20 57 69 6e 43 45 20  ll);.  /* WinCE 
f5f70 68 61 73 20 6e 6f 20 63 6f 6e 63 65 70 74 20 6f  has no concept o
f5f80 66 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74  f a relative pat
f5f90 68 6e 61 6d 65 2c 20 6f 72 20 73 6f 20 49 20 61  hname, or so I a
f5fa0 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20 20 73 71 6c  m told. */.  sql
f5fb0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 70 56  ite3_snprintf(pV
f5fc0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20  fs->mxPathname, 
f5fd0 7a 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 52 65  zFull, "%s", zRe
f5fe0 6c 61 74 69 76 65 29 3b 0a 20 20 72 65 74 75 72  lative);.  retur
f5ff0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e  n SQLITE_OK;.#en
f6000 64 69 66 0a 0a 23 69 66 20 21 53 51 4c 49 54 45  dif..#if !SQLITE
f6010 5f 4f 53 5f 57 49 4e 43 45 20 26 26 20 21 64 65  _OS_WINCE && !de
f6020 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f  fined(__CYGWIN__
f6030 29 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  ).  int nByte;. 
f6040 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
f6050 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 3b  d;.  char *zOut;
f6060 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
f6070 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 7a 43  TER(nFull);.  zC
f6080 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65  onverted = conve
f6090 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a  rtUtf8Filename(z
f60a0 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 69 66 28  Relative);.  if(
f60b0 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 57   isNT() ){.    W
f60c0 43 48 41 52 20 2a 7a 54 65 6d 70 3b 0a 20 20 20  CHAR *zTemp;.   
f60d0 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c 6c   nByte = GetFull
f60e0 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52  PathNameW((WCHAR
f60f0 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c  *)zConverted, 0,
f6100 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20   0, 0) + 3;.    
f6110 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20  zTemp = malloc( 
f6120 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65  nByte*sizeof(zTe
f6130 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66  mp[0]) );.    if
f6140 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20  ( zTemp==0 ){.  
f6150 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
f6160 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ted);.      retu
f6170 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
f6180 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75  .    }.    GetFu
f6190 6c 6c 50 61 74 68 4e 61 6d 65 57 28 28 57 43 48  llPathNameW((WCH
f61a0 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20  AR*)zConverted, 
f61b0 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29  nByte, zTemp, 0)
f61c0 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76  ;.    free(zConv
f61d0 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74  erted);.    zOut
f61e0 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38   = unicodeToUtf8
f61f0 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65  (zTemp);.    fre
f6200 65 28 7a 54 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e  e(zTemp);./* isN
f6210 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49  T() is 1 if SQLI
f6220 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20  TE_OS_WINCE==1, 
f6230 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20  so this else is 
f6240 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20  never executed. 
f6250 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53  .** Since the AS
f6260 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  CII version of t
f6270 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49  hese Windows API
f6280 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f   do not exist fo
f6290 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73  r WINCE,.** it's
f62a0 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f   important to no
f62b0 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d  t reference them
f62c0 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64   for WINCE build
f62d0 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s..*/.#if SQLITE
f62e0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d  _OS_WINCE==0.  }
f62f0 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
f6300 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65  zTemp;.    nByte
f6310 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61   = GetFullPathNa
f6320 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76  meA((char*)zConv
f6330 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20  erted, 0, 0, 0) 
f6340 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d  + 3;.    zTemp =
f6350 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73   malloc( nByte*s
f6360 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20  izeof(zTemp[0]) 
f6370 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70  );.    if( zTemp
f6380 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65  ==0 ){.      fre
f6390 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
f63a0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
f63b0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
f63c0 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e      GetFullPathN
f63d0 61 6d 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e  ameA((char*)zCon
f63e0 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a  verted, nByte, z
f63f0 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72  Temp, 0);.    fr
f6400 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
f6410 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74      zOut = sqlit
f6420 65 33 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f  e3_win32_mbcs_to
f6430 5f 75 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20  _utf8(zTemp);.  
f6440 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 23    free(zTemp);.#
f6450 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20  endif.  }.  if( 
f6460 7a 4f 75 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  zOut ){.    sqli
f6470 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 70 56 66  te3_snprintf(pVf
f6480 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a  s->mxPathname, z
f6490 46 75 6c 6c 2c 20 22 25 73 22 2c 20 7a 4f 75 74  Full, "%s", zOut
f64a0 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4f 75 74  );.    free(zOut
f64b0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
f64c0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
f64d0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
f64e0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23  ITE_NOMEM;.  }.#
f64f0 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  endif.}../*.** G
f6500 65 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  et the sector si
f6510 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65  ze of the device
f6520 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a   used to store.*
f6530 2a 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  * file..*/.stati
f6540 63 20 69 6e 74 20 67 65 74 53 65 63 74 6f 72 53  c int getSectorS
f6550 69 7a 65 28 0a 20 20 20 20 73 71 6c 69 74 65 33  ize(.    sqlite3
f6560 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 20 20 20  _vfs *pVfs,.    
f6570 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c  const char *zRel
f6580 61 74 69 76 65 20 20 20 20 20 2f 2a 20 55 54 46  ative     /* UTF
f6590 2d 38 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a  -8 file name */.
f65a0 29 7b 0a 20 20 44 57 4f 52 44 20 62 79 74 65 73  ){.  DWORD bytes
f65b0 50 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49  PerSector = SQLI
f65c0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
f65d0 52 5f 53 49 5a 45 3b 0a 20 20 63 68 61 72 20 7a  R_SIZE;.  char z
f65e0 46 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54  Fullpath[MAX_PAT
f65f0 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  H+1];.  int rc;.
f6600 20 20 44 57 4f 52 44 20 64 77 52 65 74 20 3d 20    DWORD dwRet = 
f6610 30 2c 20 64 77 44 75 6d 6d 79 3b 0a 0a 20 20 2f  0, dwDummy;..  /
f6620 2a 0a 20 20 2a 2a 20 57 65 20 6e 65 65 64 20 74  *.  ** We need t
f6630 6f 20 67 65 74 20 74 68 65 20 66 75 6c 6c 20 70  o get the full p
f6640 61 74 68 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  ath name of the 
f6650 66 69 6c 65 0a 20 20 2a 2a 20 74 6f 20 67 65 74  file.  ** to get
f6660 20 74 68 65 20 64 72 69 76 65 20 6c 65 74 74 65   the drive lette
f6670 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20 74 68 65  r to look up the
f6680 20 73 65 63 74 6f 72 0a 20 20 2a 2a 20 73 69 7a   sector.  ** siz
f6690 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 77  e..  */.  rc = w
f66a0 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70  inFullPathname(p
f66b0 56 66 73 2c 20 7a 52 65 6c 61 74 69 76 65 2c 20  Vfs, zRelative, 
f66c0 4d 41 58 5f 50 41 54 48 2c 20 7a 46 75 6c 6c 70  MAX_PATH, zFullp
f66d0 61 74 68 29 3b 0a 20 20 69 66 28 20 72 63 20 3d  ath);.  if( rc =
f66e0 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 0a 20 20  = SQLITE_OK ).  
f66f0 7b 0a 20 20 20 20 76 6f 69 64 20 2a 7a 43 6f 6e  {.    void *zCon
f6700 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74  verted = convert
f6710 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 75  Utf8Filename(zFu
f6720 6c 6c 70 61 74 68 29 3b 0a 20 20 20 20 69 66 28  llpath);.    if(
f6730 20 7a 43 6f 6e 76 65 72 74 65 64 20 29 7b 0a 20   zConverted ){. 
f6740 20 20 20 20 20 69 66 28 20 69 73 4e 54 28 29 20       if( isNT() 
f6750 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 74 72  ){.        /* tr
f6760 69 6d 20 70 61 74 68 20 74 6f 20 6a 75 73 74 20  im path to just 
f6770 64 72 69 76 65 20 72 65 66 65 72 65 6e 63 65 20  drive reference 
f6780 2a 2f 0a 20 20 20 20 20 20 20 20 57 43 48 41 52  */.        WCHAR
f6790 20 2a 70 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64   *p = zConverted
f67a0 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a  ;.        for(;*
f67b0 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  p;p++){.        
f67c0 20 20 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27    if( *p == '\\'
f67d0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
f67e0 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  *p = '\0';.     
f67f0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
f6800 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
f6810 20 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65    }.        dwRe
f6820 74 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53  t = GetDiskFreeS
f6830 70 61 63 65 57 28 28 57 43 48 41 52 2a 29 7a 43  paceW((WCHAR*)zC
f6840 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20  onverted,.      
f6850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6860 20 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44              &dwD
f6870 75 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20  ummy,.          
f6880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6890 20 20 20 20 20 20 20 20 26 62 79 74 65 73 50 65          &bytesPe
f68a0 72 53 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20  rSector,.       
f68b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f68c0 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75             &dwDu
f68d0 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  mmy,.           
f68e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f68f0 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 29         &dwDummy)
f6900 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ;.#if SQLITE_OS_
f6910 57 49 4e 43 45 3d 3d 30 0a 20 20 20 20 20 20 7d  WINCE==0.      }
f6920 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
f6930 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a 75   trim path to ju
f6940 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 6e  st drive referen
f6950 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 48  ce */.        CH
f6960 41 52 20 2a 70 20 3d 20 28 43 48 41 52 20 2a 29  AR *p = (CHAR *)
f6970 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20  zConverted;.    
f6980 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29      for(;*p;p++)
f6990 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
f69a0 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20  *p == '\\' ){.  
f69b0 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27            *p = '
f69c0 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20  \0';.           
f69d0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
f69e0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
f69f0 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47 65        dwRet = Ge
f6a00 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 41 28  tDiskFreeSpaceA(
f6a10 28 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65  (CHAR*)zConverte
f6a20 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
f6a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6a40 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20       &dwDummy,. 
f6a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6a70 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f 72   &bytesPerSector
f6a80 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
f6a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6aa0 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20      &dwDummy,.  
f6ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6ad0 26 64 77 44 75 6d 6d 79 29 3b 0a 23 65 6e 64 69  &dwDummy);.#endi
f6ae0 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  f.      }.      
f6af0 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
f6b00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
f6b10 21 64 77 52 65 74 20 29 7b 0a 20 20 20 20 20 20  !dwRet ){.      
f6b20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d  bytesPerSector =
f6b30 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
f6b40 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20  SECTOR_SIZE;.   
f6b50 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
f6b60 28 69 6e 74 29 20 62 79 74 65 73 50 65 72 53 65  (int) bytesPerSe
f6b70 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 6e 64 65  ctor; .}..#ifnde
f6b80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
f6b90 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a  AD_EXTENSION./*.
f6ba0 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f  ** Interfaces fo
f6bb0 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72  r opening a shar
f6bc0 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64  ed library, find
f6bd0 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  ing entry points
f6be0 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73  .** within the s
f6bf0 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61  hared library, a
f6c00 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73  nd closing the s
f6c10 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a  hared library..*
f6c20 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63  /./*.** Interfac
f6c30 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61  es for opening a
f6c40 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
f6c50 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70   finding entry p
f6c60 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20  oints.** within 
f6c70 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
f6c80 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20  ry, and closing 
f6c90 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
f6ca0 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ry..*/.static vo
f6cb0 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73 71  id *winDlOpen(sq
f6cc0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
f6cd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
f6ce0 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44 4c  lename){.  HANDL
f6cf0 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  E h;.  void *zCo
f6d00 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72  nverted = conver
f6d10 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46  tUtf8Filename(zF
f6d20 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 53  ilename);.  UNUS
f6d30 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
f6d40 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65  s);.  if( zConve
f6d50 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72  rted==0 ){.    r
f6d60 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
f6d70 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
f6d80 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79   h = LoadLibrary
f6d90 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65  W((WCHAR*)zConve
f6da0 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29  rted);./* isNT()
f6db0 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
f6dc0 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
f6dd0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
f6de0 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a  er executed. .**
f6df0 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49   Since the ASCII
f6e00 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73   version of thes
f6e10 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f  e Windows API do
f6e20 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57   not exist for W
f6e30 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d  INCE,.** it's im
f6e40 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72  portant to not r
f6e50 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f  eference them fo
f6e60 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a  r WINCE builds..
f6e70 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f6e80 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73  _WINCE==0.  }els
f6e90 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c  e{.    h = LoadL
f6ea0 69 62 72 61 72 79 41 28 28 63 68 61 72 2a 29 7a  ibraryA((char*)z
f6eb0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64  Converted);.#end
f6ec0 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43  if.  }.  free(zC
f6ed0 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65 74  onverted);.  ret
f6ee0 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d 0a  urn (void*)h;.}.
f6ef0 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 44  static void winD
f6f00 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76  lError(sqlite3_v
f6f10 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42  fs *pVfs, int nB
f6f20 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  uf, char *zBufOu
f6f30 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  t){.  UNUSED_PAR
f6f40 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
f6f50 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 28  getLastErrorMsg(
f6f60 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 29 3b 0a  nBuf, zBufOut);.
f6f70 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53 79  }.void (*winDlSy
f6f80 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  m(sqlite3_vfs *p
f6f90 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64  Vfs, void *pHand
f6fa0 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  le, const char *
f6fb0 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 7b  zSymbol))(void){
f6fc0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
f6fd0 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 20 53  TER(pVfs);.#if S
f6fe0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
f6ff0 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f 63 41   /* The GetProcA
f7000 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 69 6e  ddressA() routin
f7010 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  e is only availa
f7020 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 2a 2f  ble on wince. */
f7030 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 28  .  return (void(
f7040 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f 63  *)(void))GetProc
f7050 41 64 64 72 65 73 73 41 28 28 48 41 4e 44 4c 45  AddressA((HANDLE
f7060 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 6f  )pHandle, zSymbo
f7070 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 41  l);.#else.  /* A
f7080 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 6f 77 73  ll other windows
f7090 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 70 65 63   platforms expec
f70a0 74 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73  t GetProcAddress
f70b0 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a 20  () to take.  ** 
f70c0 61 6e 20 41 6e 73 69 20 73 74 72 69 6e 67 20 72  an Ansi string r
f70d0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65  egardless of the
f70e0 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 74 69 6e   _UNICODE settin
f70f0 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76  g */.  return (v
f7100 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 74  oid(*)(void))Get
f7110 50 72 6f 63 41 64 64 72 65 73 73 28 28 48 41 4e  ProcAddress((HAN
f7120 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79  DLE)pHandle, zSy
f7130 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  mbol);.#endif.}.
f7140 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65 28  void winDlClose(
f7150 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
f7160 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  s, void *pHandle
f7170 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
f7180 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 46  METER(pVfs);.  F
f7190 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e 44  reeLibrary((HAND
f71a0 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23  LE)pHandle);.}.#
f71b0 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54  else /* if SQLIT
f71c0 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
f71d0 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64  NSION is defined
f71e0 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 77  : */.  #define w
f71f0 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64  inDlOpen  0.  #d
f7200 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f 72  efine winDlError
f7210 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e   0.  #define win
f7220 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66  DlSym   0.  #def
f7230 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20 30  ine winDlClose 0
f7240 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
f7250 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66  Write up to nBuf
f7260 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d   bytes of random
f7270 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a  ness into zBuf..
f7280 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
f7290 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  nRandomness(sqli
f72a0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
f72b0 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
f72c0 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  Buf){.  int n = 
f72d0 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  0;.  UNUSED_PARA
f72e0 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66  METER(pVfs);.#if
f72f0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
f7300 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 66  TEST).  n = nBuf
f7310 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c  ;.  memset(zBuf,
f7320 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 65   0, nBuf);.#else
f7330 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 53 59  .  if( sizeof(SY
f7340 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66 2d  STEMTIME)<=nBuf-
f7350 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 4d 54  n ){.    SYSTEMT
f7360 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 53 79  IME x;.    GetSy
f7370 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20 20  stemTime(&x);.  
f7380 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
f7390 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29  ], &x, sizeof(x)
f73a0 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
f73b0 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28  of(x);.  }.  if(
f73c0 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d   sizeof(DWORD)<=
f73d0 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57  nBuf-n ){.    DW
f73e0 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 75 72  ORD pid = GetCur
f73f0 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29 3b  rentProcessId();
f7400 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
f7410 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65  f[n], &pid, size
f7420 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 6e 20  of(pid));.    n 
f7430 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 3b 0a  += sizeof(pid);.
f7440 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f 66    }.  if( sizeof
f7450 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20  (DWORD)<=nBuf-n 
f7460 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 6e 74  ){.    DWORD cnt
f7470 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e 74 28   = GetTickCount(
f7480 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
f7490 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 69  Buf[n], &cnt, si
f74a0 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20 20  zeof(cnt));.    
f74b0 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74 29  n += sizeof(cnt)
f74c0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65  ;.  }.  if( size
f74d0 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 52  of(LARGE_INTEGER
f74e0 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20  )<=nBuf-n ){.   
f74f0 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 20 69   LARGE_INTEGER i
f7500 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 66 6f  ;.    QueryPerfo
f7510 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 69  rmanceCounter(&i
f7520 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
f7530 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a 65  Buf[n], &i, size
f7540 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b 3d  of(i));.    n +=
f7550 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d 0a   sizeof(i);.  }.
f7560 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
f7570 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65  n;.}.../*.** Sle
f7580 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20  ep for a little 
f7590 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74  while.  Return t
f75a0 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  he amount of tim
f75b0 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 74  e slept..*/.stat
f75c0 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 28  ic int winSleep(
f75d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
f75e0 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 29  s, int microsec)
f75f0 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72 6f  {.  Sleep((micro
f7600 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b 0a  sec+999)/1000);.
f7610 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
f7620 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75  ER(pVfs);.  retu
f7630 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 39  rn ((microsec+99
f7640 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d  9)/1000)*1000;.}
f7650 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
f7660 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20  owing variable, 
f7670 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d  if set to a non-
f7680 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f  zero value, beco
f7690 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  mes the result.*
f76a0 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  * returned from 
f76b0 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
f76c0 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73  Time().  This is
f76d0 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
f76e0 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  g..*/.#ifdef SQL
f76f0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
f7700 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f7710 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30  current_time = 0
f7720 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
f7730 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74  Find the current
f7740 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72   time (in Univer
f7750 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20  sal Coordinated 
f7760 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68  Time).  Write th
f7770 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d  e.** current tim
f7780 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20  e and date as a 
f7790 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65  Julian Day numbe
f77a0 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e  r into *prNow an
f77b0 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20  d.** return 0.  
f77c0 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
f77d0 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61  time and date ca
f77e0 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a  nnot be found..*
f77f0 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e 74  /.int winCurrent
f7800 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73  Time(sqlite3_vfs
f7810 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a   *pVfs, double *
f7820 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 49  prNow){.  FILETI
f7830 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c 45  ME ft;.  /* FILE
f7840 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 69  TIME structure i
f7850 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 65  s a 64-bit value
f7860 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
f7870 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 20  e number of .   
f7880 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64    100-nanosecond
f7890 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 65   intervals since
f78a0 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 31   January 1, 1601
f78b0 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 35   (= JD 2305813.5
f78c0 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ). .  */.  sqlit
f78d0 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 57 3b 20  e3_int64 timeW; 
f78e0 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 79 73 20    /* Whole days 
f78f0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
f7900 36 34 20 74 69 6d 65 46 3b 20 20 20 2f 2a 20 46  64 timeF;   /* F
f7910 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 73 20 2a  ractional Days *
f7920 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  /..  /* Number o
f7930 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64  f 100-nanosecond
f7940 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 61 20   intervals in a 
f7950 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f 0a 20 20  single day */.  
f7960 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
f7970 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 65  ite3_int64 ntuPe
f7980 72 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 30  rDay = .      10
f7990 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 5f  000000*(sqlite3_
f79a0 69 6e 74 36 34 29 38 36 34 30 30 3b 0a 0a 20 20  int64)86400;..  
f79b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 30  /* Number of 100
f79c0 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 65  -nanosecond inte
f79d0 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 20 6f 66  rvals in half of
f79e0 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 74   a day */.  stat
f79f0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
f7a00 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 48 61 6c  _int64 ntuPerHal
f7a10 66 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 30  fDay = .      10
f7a20 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 5f  000000*(sqlite3_
f7a30 69 6e 74 36 34 29 34 33 32 30 30 3b 0a 0a 20 20  int64)43200;..  
f7a40 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76 6f  /* 2^32 - to avo
f7a50 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e 64  id use of LL and
f7a60 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63 63   warnings in gcc
f7a70 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
f7a80 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  st sqlite3_int64
f7a90 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20 3d   max32BitValue =
f7aa0 20 0a 20 20 20 20 20 20 28 73 71 6c 69 74 65 33   .      (sqlite3
f7ab0 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 30  _int64)200000000
f7ac0 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  0 + (sqlite3_int
f7ad0 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b 20  64)2000000000 + 
f7ae0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32  (sqlite3_int64)2
f7af0 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 66 20 53  94967296;..#if S
f7b00 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
f7b10 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69 6d 65   SYSTEMTIME time
f7b20 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d  ;.  GetSystemTim
f7b30 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f 2a 20 69  e(&time);.  /* i
f7b40 66 20 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69  f SystemTimeToFi
f7b50 6c 65 54 69 6d 65 28 29 20 66 61 69 6c 73 2c 20  leTime() fails, 
f7b60 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2e  it returns zero.
f7b70 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 73 74 65   */.  if (!Syste
f7b80 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28  mTimeToFileTime(
f7b90 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a 20 20 20  &time,&ft)){.   
f7ba0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23   return 1;.  }.#
f7bb0 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65 6d  else.  GetSystem
f7bc0 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28 20  TimeAsFileTime( 
f7bd0 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20  &ft );.#endif.  
f7be0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f7bf0 28 70 56 66 73 29 3b 0a 20 20 74 69 6d 65 57 20  (pVfs);.  timeW 
f7c00 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74  = (((sqlite3_int
f7c10 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 74 65  64)ft.dwHighDate
f7c20 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 56 61  Time)*max32BitVa
f7c30 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 65 33 5f  lue) + (sqlite3_
f7c40 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 44 61  int64)ft.dwLowDa
f7c50 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d 65 46 20  teTime;.  timeF 
f7c60 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 50 65 72  = timeW % ntuPer
f7c70 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Day;          /*
f7c80 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 73   fractional days
f7c90 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64   (100-nanosecond
f7ca0 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20  s) */.  timeW = 
f7cb0 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 72 44 61  timeW / ntuPerDa
f7cc0 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 77  y;          /* w
f7cd0 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20 74  hole days */.  t
f7ce0 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 32  imeW = timeW + 2
f7cf0 33 30 35 38 31 33 3b 20 20 20 20 20 20 20 20 20  305813;         
f7d00 20 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20     /* add whole 
f7d10 64 61 79 73 20 28 66 72 6f 6d 20 32 33 30 35 38  days (from 23058
f7d20 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 46  13.5) */.  timeF
f7d30 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 75 50 65   = timeF + ntuPe
f7d40 72 48 61 6c 66 44 61 79 3b 20 20 20 20 20 20 2f  rHalfDay;      /
f7d50 2a 20 61 64 64 20 68 61 6c 66 20 61 20 64 61 79  * add half a day
f7d60 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e 35   (from 2305813.5
f7d70 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74  ) */.  timeW = t
f7d80 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 2f 6e 74  imeW + (timeF/nt
f7d90 75 50 65 72 44 61 79 29 3b 20 20 2f 2a 20 61 64  uPerDay);  /* ad
f7da0 64 20 77 68 6f 6c 65 20 64 61 79 20 69 66 20 68  d whole day if h
f7db0 61 6c 66 20 64 61 79 20 6d 61 64 65 20 6f 6e 65  alf day made one
f7dc0 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 69   */.  timeF = ti
f7dd0 6d 65 46 20 25 20 6e 74 75 50 65 72 44 61 79 3b  meF % ntuPerDay;
f7de0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6d            /* com
f7df0 70 75 74 65 20 6e 65 77 20 66 72 61 63 74 69 6f  pute new fractio
f7e00 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 20 2a 70  nal days */.  *p
f7e10 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29 74  rNow = (double)t
f7e20 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 6c 65 29  imeW + ((double)
f7e30 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 6c 65 29  timeF / (double)
f7e40 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 69 66 64  ntuPerDay);.#ifd
f7e50 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
f7e60 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72   if( sqlite3_cur
f7e70 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20  rent_time ){.   
f7e80 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 6f 75 62   *prNow = ((doub
f7e90 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 72 72 65  le)sqlite3_curre
f7ea0 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f 75 62 6c  nt_time + (doubl
f7eb0 65 29 34 33 32 30 30 29 20 2f 20 28 64 6f 75 62  e)43200) / (doub
f7ec0 6c 65 29 38 36 34 30 30 20 2b 20 28 64 6f 75 62  le)86400 + (doub
f7ed0 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 20 7d 0a  le)2440587;.  }.
f7ee0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
f7ef0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
f7f00 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68 69  idea is that thi
f7f10 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73  s function works
f7f20 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 74   like a combinat
f7f30 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 73  ion of.** GetLas
f7f40 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f 72  tError() and For
f7f50 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e 20  matMessage() on 
f7f60 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 6e  windows (or errn
f7f70 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 6f  o and.** strerro
f7f80 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e 20  r_r() on unix). 
f7f90 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 69  After an error i
f7fa0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 6e  s returned by an
f7fb0 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c   OS.** function,
f7fc0 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68   SQLite calls th
f7fd0 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  is function with
f7fe0 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 74   zBuf pointing t
f7ff0 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f 66  o.** a buffer of
f8000 20 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68 65   nBuf bytes. The
f8010 20 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c 64   OS layer should
f8020 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a 2a   populate the.**
f8030 20 62 75 66 66 65 72 20 77 69 74 68 20 61 20 6e   buffer with a n
f8040 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54  ul-terminated UT
f8050 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72 6f  F-8 encoded erro
f8060 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 73  r message.** des
f8070 63 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73 74  cribing the last
f8080 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 76   IO error to hav
f8090 65 20 6f 63 63 75 72 72 65 64 20 77 69 74 68 69  e occurred withi
f80a0 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a  n the calling.**
f80b0 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49   thread..**.** I
f80c0 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73  f the error mess
f80d0 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65  age is too large
f80e0 20 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69 65   for the supplie
f80f0 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74 20  d buffer,.** it 
f8100 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 61  should be trunca
f8110 74 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e 20  ted. The return 
f8120 76 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61 73  value of xGetLas
f8130 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 72  tError.** is zer
f8140 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d  o if the error m
f8150 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74  essage fits in t
f8160 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f  he buffer, or no
f8170 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77  n-zero.** otherw
f8180 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73  ise (if the mess
f8190 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65  age was truncate
f81a0 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20  d). If non-zero 
f81b0 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20  is returned,.** 
f81c0 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 6e  then it is not n
f81d0 65 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 6c  ecessary to incl
f81e0 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 6d  ude the nul-term
f81f0 69 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 72  inator character
f8200 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 75  .** in the outpu
f8210 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  t buffer..**.** 
f8220 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 6e  Not supplying an
f8230 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
f8240 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 65  ill have no adve
f8250 72 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f 6e  rse effect.** on
f8260 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20 66   SQLite. It is f
f8270 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20 69  ine to have an i
f8280 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68  mplementation th
f8290 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74 75  at never.** retu
f82a0 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  rns an error mes
f82b0 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 6e  sage:.**.**   in
f82c0 74 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28  t xGetLastError(
f82d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
f82e0 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  s, int nBuf, cha
f82f0 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 20  r *zBuf){.**    
f8300 20 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d 3d   assert(zBuf[0]=
f8310 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20 72  ='\0');.**     r
f8320 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d 0a  eturn 0;.**   }.
f8330 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 66  **.** However if
f8340 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
f8350 65 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 69  e is supplied, i
f8360 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 70  t will be incorp
f8370 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71 6c  orated.** by sql
f8380 69 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72 72  ite into the err
f8390 6f 72 20 6d 65 73 73 61 67 65 20 61 76 61 69 6c  or message avail
f83a0 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 72  able to the user
f83b0 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65   using.** sqlite
f83c0 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73 73  3_errmsg(), poss
f83d0 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20 65  ibly making IO e
f83e0 72 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f 20  rrors easier to 
f83f0 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  debug..*/.static
f8400 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 45   int winGetLastE
f8410 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
f8420 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
f8430 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
f8440 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f8450 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 72  R(pVfs);.  retur
f8460 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73  n getLastErrorMs
f8470 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a 7d  g(nBuf, zBuf);.}
f8480 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
f8490 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c  ze and deinitial
f84a0 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e  ize the operatin
f84b0 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61  g system interfa
f84c0 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ce..*/.SQLITE_AP
f84d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  I int sqlite3_os
f84e0 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73  _init(void){.  s
f84f0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66  tatic sqlite3_vf
f8500 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 20  s winVfs = {.   
f8510 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
f8520 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
f8530 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 69  */.    sizeof(wi
f8540 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f  nFile),   /* szO
f8550 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58  sFile */.    MAX
f8560 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 20  _PATH,          
f8570 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f  /* mxPathname */
f8580 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
f8590 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
f85a0 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 2c   */.    "win32",
f85b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e             /* zN
f85c0 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
f85d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f85e0 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a 20   pAppData */. . 
f85f0 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 20     winOpen,     
f8600 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a        /* xOpen *
f8610 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 2c  /.    winDelete,
f8620 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c           /* xDel
f8630 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63  ete */.    winAc
f8640 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a  cess,         /*
f8650 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20   xAccess */.    
f8660 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c  winFullPathname,
f8670 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e     /* xFullPathn
f8680 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c  ame */.    winDl
f8690 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a  Open,         /*
f86a0 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20   xDlOpen */.    
f86b0 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 20  winDlError,     
f86c0 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a     /* xDlError *
f86d0 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20  /.    winDlSym, 
f86e0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53           /* xDlS
f86f0 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43  ym */.    winDlC
f8700 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  lose,        /* 
f8710 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20  xDlClose */.    
f8720 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20  winRandomness,  
f8730 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73     /* xRandomnes
f8740 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65  s */.    winSlee
f8750 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  p,          /* x
f8760 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e  Sleep */.    win
f8770 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20  CurrentTime,    
f8780 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20  /* xCurrentTime 
f8790 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 73  */.    winGetLas
f87a0 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65  tError    /* xGe
f87b0 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20  tLastError */.  
f87c0 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  };.  sqlite3_vfs
f87d0 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56 66  _register(&winVf
f87e0 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e 20  s, 1);.  return 
f87f0 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 51  SQLITE_OK; .}.SQ
f8800 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
f8810 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64  ite3_os_end(void
f8820 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ){ .  return SQL
f8830 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69  ITE_OK;.}..#endi
f8840 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57  f /* SQLITE_OS_W
f8850 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  IN */../********
f8860 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
f8870 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _win.c *********
f8880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88a0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
f88b0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
f88c0 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a  e bitvec.c *****
f88d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f88f0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
f8900 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 2a 2a  8 February 16.**
f8910 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
f8920 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
f8930 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
f8940 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
f8950 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
f8960 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
f8970 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
f8980 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
f8990 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
f89a0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
f89b0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
f89c0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
f89d0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
f89e0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
f89f0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
f8a00 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
f8a10 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
f8a20 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
f8a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
f8a70 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  This file implem
f8a80 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 74  ents an object t
f8a90 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 61  hat represents a
f8aa0 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a 2a 2a   fixed-length.**
f8ab0 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20 61   bitmap.  Bits a
f8ac0 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72  re numbered star
f8ad0 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a 2a 0a  ting with 1..**.
f8ae0 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20 75  ** A bitmap is u
f8af0 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68  sed to record wh
f8b00 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20 64  ich pages of a d
f8b10 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 76  atabase file hav
f8b20 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e 61  e been.** journa
f8b30 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 74 72  lled during a tr
f8b40 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 77 68  ansaction, or wh
f8b50 69 63 68 20 70 61 67 65 73 20 68 61 76 65 20 74  ich pages have t
f8b60 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 22 0a  he "dont-write".
f8b70 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 55 73  ** property.  Us
f8b80 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 65 77  ually only a few
f8b90 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 74 20   pages are meet 
f8ba0 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e  either condition
f8bb0 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 74 6d  ..** So the bitm
f8bc0 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 73 70  ap is usually sp
f8bd0 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c 6f 77  arse and has low
f8be0 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a 2a 2a   cardinality..**
f8bf0 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 28   But sometimes (
f8c00 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65 6e  for example when
f8c10 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 20 6f   during a DROP o
f8c20 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c 65 29  f a large table)
f8c30 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c 20   most.** or all 
f8c40 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e 20  of the pages in 
f8c50 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 67  a database can g
f8c60 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20  et journalled.  
f8c70 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20  In those cases, 
f8c80 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 20 62  .** the bitmap b
f8c90 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 69 74  ecomes dense wit
f8ca0 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 6c 69  h high cardinali
f8cb0 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 69 74  ty.  The algorit
f8cc0 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 6f 20  hm needs .** to 
f8cd0 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 73 65  handle both case
f8ce0 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68  s well..**.** Th
f8cf0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 69  e size of the bi
f8d00 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 77 68  tmap is fixed wh
f8d10 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73  en the object is
f8d20 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   created..**.** 
f8d30 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 6c 65  All bits are cle
f8d40 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 74 6d  ar when the bitm
f8d50 61 70 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  ap is created.  
f8d60 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 0a  Individual bits.
f8d70 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 6f 72  ** may be set or
f8d80 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 74 20   cleared one at 
f8d90 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65  a time..**.** Te
f8da0 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72  st operations ar
f8db0 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d 65  e about 100 time
f8dc0 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68  s more common th
f8dd0 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e  at set operation
f8de0 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72  s..** Clear oper
f8df0 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65 65  ations are excee
f8e00 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54 68  dingly rare.  Th
f8e10 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79 20  ere are usually 
f8e20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64  between.** 5 and
f8e30 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74 69   500 set operati
f8e40 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20 6f  ons per Bitvec o
f8e50 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74 68  bject, though th
f8e60 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74 73  e number of sets
f8e70 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65   can.** sometime
f8e80 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73  s grow into tens
f8e90 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 6f 72   of thousands or
f8ea0 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 73 69   larger.  The si
f8eb0 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74  ze of the.** Bit
f8ec0 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 74 68  vec object is th
f8ed0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
f8ee0 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
f8ef0 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a  e file at the.**
f8f00 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 61 6e   start of a tran
f8f10 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20  saction, and is 
f8f20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c 65 73  thus usually les
f8f30 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 68 6f  s than a few tho
f8f40 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61  usand,.** but ca
f8f50 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 61 73  n be as large as
f8f60 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61   2 billion for a
f8f70 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 74 61   really big data
f8f80 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  base..**.** @(#)
f8f90 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63 2c 76   $Id: bitvec.c,v
f8fa0 20 31 2e 31 34 20 32 30 30 39 2f 30 34 2f 30 31   1.14 2009/04/01
f8fb0 20 32 33 3a 34 39 3a 30 34 20 64 72 68 20 45 78   23:49:04 drh Ex
f8fc0 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a 65 20  p $.*/../* Size 
f8fd0 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 73 74  of the Bitvec st
f8fe0 72 75 63 74 75 72 65 20 69 6e 20 62 79 74 65 73  ructure in bytes
f8ff0 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54  . */.#define BIT
f9000 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20 35 31  VEC_SZ        51
f9010 32 0a 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20  2../* Round the 
f9020 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20  union size down 
f9030 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 70  to the nearest p
f9040 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 79 2c  ointer boundary,
f9050 20 73 69 6e 63 65 20 74 68 61 74 27 73 20 68 6f   since that's ho
f9060 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65  w .** it will be
f9070 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69 6e 20   aligned within 
f9080 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75 63  the Bitvec struc
f9090 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  t. */.#define BI
f90a0 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20 20 28  TVEC_USIZE     (
f90b0 28 28 42 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73  ((BITVEC_SZ-(3*s
f90c0 69 7a 65 6f 66 28 75 33 32 29 29 29 2f 73 69 7a  izeof(u32)))/siz
f90d0 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a 73 69  eof(Bitvec*))*si
f90e0 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 0a 0a  zeof(Bitvec*))..
f90f0 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 61  /* Type of the a
f9100 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22 20 66  rray "element" f
f9110 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20 72 65  or the bitmap re
f9120 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a  presentation. .*
f9130 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20 70 6f  * Should be a po
f9140 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20 69 64  wer of 2, and id
f9150 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20 64 69  eally, evenly di
f9160 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56 45 43  vide into BITVEC
f9170 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 74  _USIZE. .** Sett
f9180 69 6e 67 20 74 68 69 73 20 74 6f 20 74 68 65 20  ing this to the 
f9190 22 6e 61 74 75 72 61 6c 20 77 6f 72 64 22 20 73  "natural word" s
f91a0 69 7a 65 20 6f 66 20 79 6f 75 72 20 43 50 55 20  ize of your CPU 
f91b0 6d 61 79 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 70  may improve.** p
f91c0 65 72 66 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23  erformance. */.#
f91d0 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 54 45  define BITVEC_TE
f91e0 4c 45 4d 20 20 20 20 20 75 38 0a 2f 2a 20 53 69  LEM     u8./* Si
f91f0 7a 65 2c 20 69 6e 20 62 69 74 73 2c 20 6f 66 20  ze, in bits, of 
f9200 74 68 65 20 62 69 74 6d 61 70 20 65 6c 65 6d 65  the bitmap eleme
f9210 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  nt. */.#define B
f9220 49 54 56 45 43 5f 53 5a 45 4c 45 4d 20 20 20 20  ITVEC_SZELEM    
f9230 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65  8./* Number of e
f9240 6c 65 6d 65 6e 74 73 20 69 6e 20 61 20 62 69 74  lements in a bit
f9250 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64  map array. */.#d
f9260 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 45 4c  efine BITVEC_NEL
f9270 45 4d 20 20 20 20 20 28 42 49 54 56 45 43 5f 55  EM     (BITVEC_U
f9280 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 49 54 56  SIZE/sizeof(BITV
f9290 45 43 5f 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75  EC_TELEM))./* Nu
f92a0 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20  mber of bits in 
f92b0 74 68 65 20 62 69 74 6d 61 70 20 61 72 72 61 79  the bitmap array
f92c0 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54  . */.#define BIT
f92d0 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20 28 42  VEC_NBIT      (B
f92e0 49 54 56 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 56  ITVEC_NELEM*BITV
f92f0 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e  EC_SZELEM)../* N
f9300 75 6d 62 65 72 20 6f 66 20 75 33 32 20 76 61 6c  umber of u32 val
f9310 75 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c  ues in hash tabl
f9320 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  e. */.#define BI
f9330 54 56 45 43 5f 4e 49 4e 54 20 20 20 20 20 20 28  TVEC_NINT      (
f9340 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a  BITVEC_USIZE/siz
f9350 65 6f 66 28 75 33 32 29 29 0a 2f 2a 20 4d 61 78  eof(u32))./* Max
f9360 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65  imum number of e
f9370 6e 74 72 69 65 73 20 69 6e 20 68 61 73 68 20 74  ntries in hash t
f9380 61 62 6c 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20  able before .** 
f9390 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e 64  sub-dividing and
f93a0 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a   re-hashing. */.
f93b0 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4d  #define BITVEC_M
f93c0 58 48 41 53 48 20 20 20 20 28 42 49 54 56 45 43  XHASH    (BITVEC
f93d0 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 68  _NINT/2)./* Hash
f93e0 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  ing function for
f93f0 20 74 68 65 20 61 48 61 73 68 20 72 65 70 72 65   the aHash repre
f9400 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d  sentation..** Em
f9410 70 69 72 69 63 61 6c 20 74 65 73 74 69 6e 67 20  pirical testing 
f9420 73 68 6f 77 65 64 20 74 68 61 74 20 74 68 65 20  showed that the 
f9430 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65 72 20 0a  *37 multiplier .
f9440 2a 2a 20 28 61 6e 20 61 72 62 69 74 72 61 72 79  ** (an arbitrary
f9450 20 70 72 69 6d 65 29 69 6e 20 74 68 65 20 68 61   prime)in the ha
f9460 73 68 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76  sh function prov
f9470 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 65  ided .** no fewe
f9480 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 61  r collisions tha
f9490 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20  n the no-op *1. 
f94a0 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
f94b0 43 5f 48 41 53 48 28 58 29 20 20 20 28 28 28 58  C_HASH(X)   (((X
f94c0 29 2a 31 29 25 42 49 54 56 45 43 5f 4e 49 4e 54  )*1)%BITVEC_NINT
f94d0 29 0a 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  )..#define BITVE
f94e0 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42 49 54  C_NPTR      (BIT
f94f0 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66  VEC_USIZE/sizeof
f9500 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a  (Bitvec *)).../*
f9510 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20  .** A bitmap is 
f9520 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
f9530 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
f9540 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
f9550 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f 72 64  is bitmap record
f9560 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20  s the existance 
f9570 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  of zero or more 
f9580 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76 61 6c  bits.** with val
f9590 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e  ues between 1 an
f95a0 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69  d iSize, inclusi
f95b0 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  ve..**.** There 
f95c0 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 69 62  are three possib
f95d0 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  le representatio
f95e0 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70  ns of the bitmap
f95f0 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c 3d 42  ..** If iSize<=B
f9600 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68 65 6e  ITVEC_NBIT, then
f9610 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74 6d 61   Bitvec.u.aBitma
f9620 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69 67 68  p[] is a straigh
f9630 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 54 68  t.** bitmap.  Th
f9640 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69 63  e least signific
f9650 61 6e 74 20 62 69 74 20 69 73 20 62 69 74 20 31  ant bit is bit 1
f9660 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 65  ..**.** If iSize
f9670 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61 6e 64  >BITVEC_NBIT and
f9680 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74 68 65   iDivisor==0 the
f9690 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61 73 68  n Bitvec.u.aHash
f96a0 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73 68 20  [] is.** a hash 
f96b0 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c 6c 20  table that will 
f96c0 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54 56 45  hold up to BITVE
f96d0 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69 6e 63  C_MXHASH distinc
f96e0 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  t values..**.** 
f96f0 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 76  Otherwise, the v
f9700 61 6c 75 65 20 69 20 69 73 20 72 65 64 69 72 65  alue i is redire
f9710 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20 6f 66  cted into one of
f9720 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a 2a 20   BITVEC_NPTR.** 
f9730 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f 69 6e  sub-bitmaps poin
f9740 74 65 64 20 74 6f 20 62 79 20 42 69 74 76 65 63  ted to by Bitvec
f9750 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45 61 63  .u.apSub[].  Eac
f9760 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a 20 68  h subbitmap.** h
f9770 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69 44 69  andles up to iDi
f9780 76 69 73 6f 72 20 73 65 70 61 72 61 74 65 20 76  visor separate v
f9790 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61 70 53  alues of i.  apS
f97a0 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76  ub[0] holds.** v
f97b0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20  alues between 1 
f97c0 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20 20 61  and iDivisor.  a
f97d0 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20 76 61  pSub[1] holds va
f97e0 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20  lues between.** 
f97f0 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 32  iDivisor+1 and 2
f9800 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75  *iDivisor.  apSu
f9810 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65  b[N] holds value
f9820 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69  s between.** N*i
f9830 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e  Divisor+1 and (N
f9840 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20 20 45  +1)*iDivisor.  E
f9850 61 63 68 20 73 75 62 62 69 74 6d 61 70 20 69 73  ach subbitmap is
f9860 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74   normalized.** t
f9870 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69 74 68  o hold deal with
f9880 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20   values between 
f9890 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 0a  1 and iDivisor..
f98a0 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76 65 63  */.struct Bitvec
f98b0 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65 3b 20   {.  u32 iSize; 
f98c0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
f98d0 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61 78 20  bit index.  Max 
f98e0 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34 2c 39  iSize is 4,294,9
f98f0 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 20 75 33 32  67,296. */.  u32
f9900 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f 2a 20   nSet;       /* 
f9910 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 74  Number of bits t
f9920 68 61 74 20 61 72 65 20 73 65 74 20 2d 20 6f 6e  hat are set - on
f9930 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 48 61  ly valid for aHa
f9940 73 68 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  sh.             
f9950 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e       ** element.
f9960 20 20 4d 61 78 20 69 73 20 42 49 54 56 45 43 5f    Max is BITVEC_
f9970 4e 49 4e 54 2e 20 20 46 6f 72 20 42 49 54 56 45  NINT.  For BITVE
f9980 43 5f 53 5a 20 6f 66 20 35 31 32 2c 0a 20 20 20  C_SZ of 512,.   
f9990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
f99a0 2a 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20  * this would be 
f99b0 31 32 35 2e 20 2a 2f 0a 20 20 75 33 32 20 69 44  125. */.  u32 iD
f99c0 69 76 69 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d  ivisor;   /* Num
f99d0 62 65 72 20 6f 66 20 62 69 74 73 20 68 61 6e 64  ber of bits hand
f99e0 6c 65 64 20 62 79 20 65 61 63 68 20 61 70 53 75  led by each apSu
f99f0 62 5b 5d 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20  b[] entry. */.  
f9a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f9a10 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d 30 20 66 6f  /* Should >=0 fo
f9a20 72 20 61 70 53 75 62 20 65 6c 65 6d 65 6e 74 2e  r apSub element.
f9a30 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
f9a40 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 69 44 69        /* Max iDi
f9a50 76 69 73 6f 72 20 69 73 20 6d 61 78 28 75 33 32  visor is max(u32
f9a60 29 20 2f 20 42 49 54 56 45 43 5f 4e 50 54 52 20  ) / BITVEC_NPTR 
f9a70 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  + 1.  */.       
f9a80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
f9a90 72 20 61 20 42 49 54 56 45 43 5f 53 5a 20 6f 66  r a BITVEC_SZ of
f9aa0 20 35 31 32 2c 20 74 68 69 73 20 77 6f 75 6c 64   512, this would
f9ab0 20 62 65 20 33 34 2c 33 35 39 2c 37 33 39 2e 20   be 34,359,739. 
f9ac0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20  */.  union {.   
f9ad0 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20 61 42   BITVEC_TELEM aB
f9ae0 69 74 6d 61 70 5b 42 49 54 56 45 43 5f 4e 45 4c  itmap[BITVEC_NEL
f9af0 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61  EM];    /* Bitma
f9b00 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  p representation
f9b10 20 2a 2f 0a 20 20 20 20 75 33 32 20 61 48 61 73   */.    u32 aHas
f9b20 68 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20  h[BITVEC_NINT]; 
f9b30 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62       /* Hash tab
f9b40 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  le representatio
f9b50 6e 20 2a 2f 0a 20 20 20 20 42 69 74 76 65 63 20  n */.    Bitvec 
f9b60 2a 61 70 53 75 62 5b 42 49 54 56 45 43 5f 4e 50  *apSub[BITVEC_NP
f9b70 54 52 5d 3b 20 20 2f 2a 20 52 65 63 75 72 73 69  TR];  /* Recursi
f9b80 76 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ve representatio
f9b90 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a  n */.  } u;.};..
f9ba0 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
f9bb0 65 77 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 74  ew bitmap object
f9bc0 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20   able to handle 
f9bd0 62 69 74 73 20 62 65 74 77 65 65 6e 20 30 20 61  bits between 0 a
f9be0 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63  nd iSize,.** inc
f9bf0 6c 75 73 69 76 65 2e 20 20 52 65 74 75 72 6e 20  lusive.  Return 
f9c00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
f9c10 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 20 52 65   new object.  Re
f9c20 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a  turn NULL if .**
f9c30 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a   malloc fails..*
f9c40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
f9c50 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74 65 33   Bitvec *sqlite3
f9c60 42 69 74 76 65 63 43 72 65 61 74 65 28 75 33 32  BitvecCreate(u32
f9c70 20 69 53 69 7a 65 29 7b 0a 20 20 42 69 74 76 65   iSize){.  Bitve
f9c80 63 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  c *p;.  assert( 
f9c90 73 69 7a 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56  sizeof(*p)==BITV
f9ca0 45 43 5f 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73  EC_SZ );.  p = s
f9cb0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
f9cc0 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a  ( sizeof(*p) );.
f9cd0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70    if( p ){.    p
f9ce0 2d 3e 69 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b  ->iSize = iSize;
f9cf0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
f9d00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
f9d10 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 69 2d  to see if the i-
f9d20 74 68 20 62 69 74 20 69 73 20 73 65 74 2e 20 20  th bit is set.  
f9d30 52 65 74 75 72 6e 20 74 72 75 65 20 6f 72 20 66  Return true or f
f9d40 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73  alse..** If p is
f9d50 20 4e 55 4c 4c 20 28 69 66 20 74 68 65 20 62 69   NULL (if the bi
f9d60 74 6d 61 70 20 68 61 73 20 6e 6f 74 20 62 65 65  tmap has not bee
f9d70 6e 20 63 72 65 61 74 65 64 29 20 6f 72 20 69 66  n created) or if
f9d80 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20 6f 66 20  .** i is out of 
f9d90 72 61 6e 67 65 2c 20 74 68 65 6e 20 72 65 74 75  range, then retu
f9da0 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c  rn false..*/.SQL
f9db0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
f9dc0 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
f9dd0 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32  t(Bitvec *p, u32
f9de0 20 69 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20   i){.  if( p==0 
f9df0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
f9e00 28 20 69 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20  ( i>p->iSize || 
f9e10 69 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  i==0 ) return 0;
f9e20 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28  .  i--;.  while(
f9e30 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a   p->iDivisor ){.
f9e40 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f      u32 bin = i/
f9e50 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20  p->iDivisor;.   
f9e60 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73   i = i%p->iDivis
f9e70 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75  or;.    p = p->u
f9e80 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20  .apSub[bin];.   
f9e90 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20 20   if (!p) {.     
f9ea0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
f9eb0 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53  .  }.  if( p->iS
f9ec0 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54  ize<=BITVEC_NBIT
f9ed0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
f9ee0 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42  p->u.aBitmap[i/B
f9ef0 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 20  ITVEC_SZELEM] & 
f9f00 28 31 3c 3c 28 69 26 28 42 49 54 56 45 43 5f 53  (1<<(i&(BITVEC_S
f9f10 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d 30 3b 0a  ZELEM-1))))!=0;.
f9f20 20 20 7d 20 65 6c 73 65 7b 0a 20 20 20 20 75 33    } else{.    u3
f9f30 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53  2 h = BITVEC_HAS
f9f40 48 28 69 2b 2b 29 3b 0a 20 20 20 20 77 68 69 6c  H(i++);.    whil
f9f50 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  e( p->u.aHash[h]
f9f60 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d   ){.      if( p-
f9f70 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29  >u.aHash[h]==i )
f9f80 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
f9f90 20 68 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20   h++;.      if( 
f9fa0 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29  h>=BITVEC_NINT )
f9fb0 20 68 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20   h = 0;.    }.  
f9fc0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
f9fd0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
f9fe0 20 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75   i-th bit.  Retu
f9ff0 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20  rn 0 on success 
fa000 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
fa010 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  e if.** anything
fa020 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a   goes wrong..**.
fa030 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
fa040 6d 69 67 68 74 20 63 61 75 73 65 20 73 75 62 2d  might cause sub-
fa050 62 69 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c  bitmaps to be al
fa060 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e  located.  Failin
fa070 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20  g.** to get the 
fa080 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f  memory needed to
fa090 20 68 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69   hold the sub-bi
fa0a0 74 6d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79  tmap is the only
fa0b0 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20  .** that can go 
fa0c0 77 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e  wrong with an in
fa0d0 73 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70  sert, assuming p
fa0e0 20 61 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64   and i are valid
fa0f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
fa100 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  ing function mus
fa110 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 20  t ensure that p 
fa120 69 73 20 61 20 76 61 6c 69 64 20 42 69 74 76 65  is a valid Bitve
fa130 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20  c object.** and 
fa140 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 66  that the value f
fa150 6f 72 20 22 69 22 20 69 73 20 77 69 74 68 69 6e  or "i" is within
fa160 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69   range of the Bi
fa170 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20  tvec object..** 
fa180 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 62 65  Otherwise the be
fa190 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
fa1a0 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
fa1b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
fa1c0 65 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76  e3BitvecSet(Bitv
fa1d0 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20  ec *p, u32 i){. 
fa1e0 20 75 33 32 20 68 3b 0a 20 20 61 73 73 65 72 74   u32 h;.  assert
fa1f0 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
fa200 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 61 73 73  rt( i>0 );.  ass
fa210 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 69 7a 65  ert( i<=p->iSize
fa220 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69   );.  i--;.  whi
fa230 6c 65 28 28 70 2d 3e 69 53 69 7a 65 20 3e 20 42  le((p->iSize > B
fa240 49 54 56 45 43 5f 4e 42 49 54 29 20 26 26 20 70  ITVEC_NBIT) && p
fa250 2d 3e 69 44 69 76 69 73 6f 72 29 20 7b 0a 20 20  ->iDivisor) {.  
fa260 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d    u32 bin = i/p-
fa270 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69  >iDivisor;.    i
fa280 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72   = i%p->iDivisor
fa290 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61  ;.    if( p->u.a
fa2a0 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a  pSub[bin]==0 ){.
fa2b0 20 20 20 20 20 20 70 2d 3e 75 2e 61 70 53 75 62        p->u.apSub
fa2c0 5b 62 69 6e 5d 20 3d 20 73 71 6c 69 74 65 33 42  [bin] = sqlite3B
fa2d0 69 74 76 65 63 43 72 65 61 74 65 28 20 70 2d 3e  itvecCreate( p->
fa2e0 69 44 69 76 69 73 6f 72 20 29 3b 0a 20 20 20 20  iDivisor );.    
fa2f0 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62    if( p->u.apSub
fa300 5b 62 69 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72  [bin]==0 ) retur
fa310 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
fa320 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d      }.    p = p-
fa330 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20  >u.apSub[bin];. 
fa340 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a   }.  if( p->iSiz
fa350 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29  e<=BITVEC_NBIT )
fa360 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d  {.    p->u.aBitm
fa370 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c  ap[i/BITVEC_SZEL
fa380 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20 28 69 26 28  EM] |= 1 << (i&(
fa390 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29  BITVEC_SZELEM-1)
fa3a0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
fa3b0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68  LITE_OK;.  }.  h
fa3c0 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69   = BITVEC_HASH(i
fa3d0 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66 20 74 68 65  ++);.  /* if the
fa3e0 72 65 20 77 61 73 6e 27 74 20 61 20 68 61 73 68  re wasn't a hash
fa3f0 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 61 6e 64 20   collision, and 
fa400 74 68 69 73 20 64 6f 65 73 6e 27 74 20 2a 2f 0a  this doesn't */.
fa410 20 20 2f 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20    /* completely 
fa420 66 69 6c 6c 20 74 68 65 20 68 61 73 68 2c 20 74  fill the hash, t
fa430 68 65 6e 20 6a 75 73 74 20 61 64 64 20 69 74 20  hen just add it 
fa440 77 69 74 68 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20  without */.  /* 
fa450 77 6f 72 72 69 6e 67 20 61 62 6f 75 74 20 73 75  worring about su
fa460 62 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72  b-dividing and r
fa470 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 20 20  e-hashing. */.  
fa480 69 66 28 20 21 70 2d 3e 75 2e 61 48 61 73 68 5b  if( !p->u.aHash[
fa490 68 5d 20 29 7b 0a 20 20 20 20 69 66 20 28 70 2d  h] ){.    if (p-
fa4a0 3e 6e 53 65 74 3c 28 42 49 54 56 45 43 5f 4e 49  >nSet<(BITVEC_NI
fa4b0 4e 54 2d 31 29 29 20 7b 0a 20 20 20 20 20 20 67  NT-1)) {.      g
fa4c0 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 65  oto bitvec_set_e
fa4d0 6e 64 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b  nd;.    } else {
fa4e0 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76  .      goto bitv
fa4f0 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3b 0a 20  ec_set_rehash;. 
fa500 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 74 68     }.  }.  /* th
fa510 65 72 65 20 77 61 73 20 61 20 63 6f 6c 6c 69 73  ere was a collis
fa520 69 6f 6e 2c 20 63 68 65 63 6b 20 74 6f 20 73 65  ion, check to se
fa530 65 20 69 66 20 69 74 27 73 20 61 6c 72 65 61 64  e if it's alread
fa540 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e 20 68 61 73  y */.  /* in has
fa550 68 2c 20 69 66 20 6e 6f 74 2c 20 74 72 79 20 74  h, if not, try t
fa560 6f 20 66 69 6e 64 20 61 20 73 70 6f 74 20 66 6f  o find a spot fo
fa570 72 20 69 74 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20  r it */.  do {. 
fa580 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73     if( p->u.aHas
fa590 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e  h[h]==i ) return
fa5a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
fa5b0 68 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3e 3d  h++;.    if( h>=
fa5c0 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20  BITVEC_NINT ) h 
fa5d0 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20  = 0;.  } while( 
fa5e0 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 3b  p->u.aHash[h] );
fa5f0 0a 20 20 2f 2a 20 77 65 20 64 69 64 6e 27 74 20  .  /* we didn't 
fa600 66 69 6e 64 20 69 74 20 69 6e 20 74 68 65 20 68  find it in the h
fa610 61 73 68 2e 20 20 68 20 70 6f 69 6e 74 73 20 74  ash.  h points t
fa620 6f 20 74 68 65 20 66 69 72 73 74 20 2a 2f 0a 20  o the first */. 
fa630 20 2f 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 72   /* available fr
fa640 65 65 20 73 70 6f 74 2e 20 63 68 65 63 6b 20 74  ee spot. check t
fa650 6f 20 73 65 65 20 69 66 20 74 68 69 73 20 69 73  o see if this is
fa660 20 67 6f 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f   going to */.  /
fa670 2a 20 6d 61 6b 65 20 6f 75 72 20 68 61 73 68 20  * make our hash 
fa680 74 6f 6f 20 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a  too "full".  */.
fa690 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73  bitvec_set_rehas
fa6a0 68 3a 0a 20 20 69 66 28 20 70 2d 3e 6e 53 65 74  h:.  if( p->nSet
fa6b0 3e 3d 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20  >=BITVEC_MXHASH 
fa6c0 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  ){.    unsigned 
fa6d0 69 6e 74 20 6a 3b 0a 20 20 20 20 69 6e 74 20 72  int j;.    int r
fa6e0 63 3b 0a 20 20 20 20 75 33 32 20 61 69 56 61 6c  c;.    u32 aiVal
fa6f0 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d  ues[BITVEC_NINT]
fa700 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56  ;.    memcpy(aiV
fa710 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73  alues, p->u.aHas
fa720 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75  h, sizeof(aiValu
fa730 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  es));.    memset
fa740 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20  (p->u.apSub, 0, 
fa750 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29  sizeof(aiValues)
fa760 29 3b 0a 20 20 20 20 70 2d 3e 69 44 69 76 69 73  );.    p->iDivis
fa770 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b  or = (p->iSize +
fa780 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31   BITVEC_NPTR - 1
fa790 29 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20  )/BITVEC_NPTR;. 
fa7a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
fa7b0 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a  itvecSet(p, i);.
fa7c0 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42      for(j=0; j<B
fa7d0 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29  ITVEC_NINT; j++)
fa7e0 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69 56 61  {.      if( aiVa
fa7f0 6c 75 65 73 5b 6a 5d 20 29 20 72 63 20 7c 3d 20  lues[j] ) rc |= 
fa800 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
fa810 28 70 2c 20 61 69 56 61 6c 75 65 73 5b 6a 5d 29  (p, aiValues[j])
fa820 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
fa830 72 6e 20 72 63 3b 0a 20 20 7d 0a 62 69 74 76 65  rn rc;.  }.bitve
fa840 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 20 70 2d 3e  c_set_end:.  p->
fa850 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d 3e 75 2e 61  nSet++;.  p->u.a
fa860 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a 20 20 72  Hash[h] = i;.  r
fa870 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
fa880 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
fa890 74 68 65 20 69 2d 74 68 20 62 69 74 2e 0a 2a 2f  the i-th bit..*/
fa8a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
fa8b0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76  void sqlite3Bitv
fa8c0 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a  ecClear(Bitvec *
fa8d0 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 61 73 73  p, u32 i){.  ass
fa8e0 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61  ert( p!=0 );.  a
fa8f0 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20  ssert( i>0 );.  
fa900 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d  i--;.  while( p-
fa910 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20  >iDivisor ){.   
fa920 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e   u32 bin = i/p->
fa930 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20  iDivisor;.    i 
fa940 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b  = i%p->iDivisor;
fa950 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70  .    p = p->u.ap
fa960 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66  Sub[bin];.    if
fa970 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65   (!p) {.      re
fa980 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
fa990 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d    if( p->iSize<=
fa9a0 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20  BITVEC_NBIT ){. 
fa9b0 20 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b     p->u.aBitmap[
fa9c0 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d  i/BITVEC_SZELEM]
fa9d0 20 26 3d 20 7e 28 31 20 3c 3c 20 28 69 26 28 42   &= ~(1 << (i&(B
fa9e0 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29  ITVEC_SZELEM-1))
fa9f0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
faa00 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a  unsigned int j;.
faa10 20 20 20 20 75 33 32 20 61 69 56 61 6c 75 65 73      u32 aiValues
faa20 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 0a 20  [BITVEC_NINT];. 
faa30 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75     memcpy(aiValu
faa40 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20  es, p->u.aHash, 
faa50 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73 29  sizeof(aiValues)
faa60 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2d  );.    memset(p-
faa70 3e 75 2e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a  >u.aHash, 0, siz
faa80 65 6f 66 28 61 69 56 61 6c 75 65 73 29 29 3b 0a  eof(aiValues));.
faa90 20 20 20 20 70 2d 3e 6e 53 65 74 20 3d 20 30 3b      p->nSet = 0;
faaa0 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
faab0 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b  BITVEC_NINT; j++
faac0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 69 56  ){.      if( aiV
faad0 61 6c 75 65 73 5b 6a 5d 20 26 26 20 61 69 56 61  alues[j] && aiVa
faae0 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 29  lues[j]!=(i+1) )
faaf0 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 68 20  {.        u32 h 
fab00 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 61 69  = BITVEC_HASH(ai
fab10 56 61 6c 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20  Values[j]-1);.  
fab20 20 20 20 20 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b        p->nSet++;
fab30 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
fab40 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b  p->u.aHash[h] ){
fab50 0a 20 20 20 20 20 20 20 20 20 20 68 2b 2b 3b 0a  .          h++;.
fab60 20 20 20 20 20 20 20 20 20 20 69 66 28 20 68 3e            if( h>
fab70 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20 29 20 68  =BITVEC_NINT ) h
fab80 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
fab90 20 20 20 20 20 20 20 20 70 2d 3e 75 2e 61 48 61          p->u.aHa
faba0 73 68 5b 68 5d 20 3d 20 61 69 56 61 6c 75 65 73  sh[h] = aiValues
fabb0 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [j];.      }.   
fabc0 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
fabd0 44 65 73 74 72 6f 79 20 61 20 62 69 74 6d 61 70  Destroy a bitmap
fabe0 20 6f 62 6a 65 63 74 2e 20 20 52 65 63 6c 61 69   object.  Reclai
fabf0 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65  m all memory use
fac00 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
fac10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
fac20 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 42  3BitvecDestroy(B
fac30 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 69 66 28  itvec *p){.  if(
fac40 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   p==0 ) return;.
fac50 20 20 69 66 28 20 70 2d 3e 69 44 69 76 69 73 6f    if( p->iDiviso
fac60 72 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65  r ){.    unsigne
fac70 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72  d int i;.    for
fac80 28 69 3d 30 3b 20 69 3c 42 49 54 56 45 43 5f 4e  (i=0; i<BITVEC_N
fac90 50 54 52 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  PTR; i++){.     
faca0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
facb0 73 74 72 6f 79 28 70 2d 3e 75 2e 61 70 53 75 62  stroy(p->u.apSub
facc0 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  [i]);.    }.  }.
facd0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
face0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
facf0 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  rn the value of 
fad00 74 68 65 20 69 53 69 7a 65 20 70 61 72 61 6d 65  the iSize parame
fad10 74 65 72 20 73 70 65 63 69 66 69 65 64 20 77 68  ter specified wh
fad20 65 6e 20 42 69 74 76 65 63 20 2a 70 0a 2a 2a 20  en Bitvec *p.** 
fad30 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a  was created..*/.
fad40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
fad50 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  32 sqlite3Bitvec
fad60 53 69 7a 65 28 42 69 74 76 65 63 20 2a 70 29 7b  Size(Bitvec *p){
fad70 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 53 69  .  return p->iSi
fad80 7a 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ze;.}..#ifndef S
fad90 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
fada0 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65  IN_TEST./*.** Le
fadb0 74 20 56 5b 5d 20 62 65 20 61 6e 20 61 72 72 61  t V[] be an arra
fadc0 79 20 6f 66 20 75 6e 73 69 67 6e 65 64 20 63 68  y of unsigned ch
fadd0 61 72 61 63 74 65 72 73 20 73 75 66 66 69 63 69  aracters suffici
fade0 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75  ent to hold.** u
fadf0 70 20 74 6f 20 4e 20 62 69 74 73 2e 20 20 4c 65  p to N bits.  Le
fae00 74 20 49 20 62 65 20 61 6e 20 69 6e 74 65 67 65  t I be an intege
fae10 72 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  r between 0 and 
fae20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54  N.  0<=I<N..** T
fae30 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  hen the followin
fae40 67 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20  g macros can be 
fae50 75 73 65 64 20 74 6f 20 73 65 74 2c 20 63 6c 65  used to set, cle
fae60 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a 2a 20 69  ar, or test.** i
fae70 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 20 77  ndividual bits w
fae80 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66  ithin V..*/.#def
fae90 69 6e 65 20 53 45 54 42 49 54 28 56 2c 49 29 20  ine SETBIT(V,I) 
faea0 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20       V[I>>3] |= 
faeb0 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 65 66 69  (1<<(I&7)).#defi
faec0 6e 65 20 43 4c 45 41 52 42 49 54 28 56 2c 49 29  ne CLEARBIT(V,I)
faed0 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e      V[I>>3] &= ~
faee0 28 31 3c 3c 28 49 26 37 29 29 0a 23 64 65 66 69  (1<<(I&7)).#defi
faef0 6e 65 20 54 45 53 54 42 49 54 28 56 2c 49 29 20  ne TESTBIT(V,I) 
faf00 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c      (V[I>>3]&(1<
faf10 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a  <(I&7)))!=0../*.
faf20 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
faf30 72 75 6e 73 20 61 6e 20 65 78 74 65 6e 73 69 76  runs an extensiv
faf40 65 20 74 65 73 74 20 6f 66 20 74 68 65 20 42 69  e test of the Bi
faf50 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  tvec code..**.**
faf60 20 54 68 65 20 69 6e 70 75 74 20 69 73 20 61 6e   The input is an
faf70 20 61 72 72 61 79 20 6f 66 20 69 6e 74 65 67 65   array of intege
faf80 72 73 20 74 68 61 74 20 61 63 74 73 20 61 73 20  rs that acts as 
faf90 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20  a program.** to 
fafa0 74 65 73 74 20 74 68 65 20 42 69 74 76 65 63 2e  test the Bitvec.
fafb0 20 20 54 68 65 20 69 6e 74 65 67 65 72 73 20 61    The integers a
fafc0 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 6c 6c 6f  re opcodes follo
fafd0 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20 31 2c 20  wed.** by 0, 1, 
fafe0 6f 72 20 33 20 6f 70 65 72 61 6e 64 73 2c 20 64  or 3 operands, d
faff0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
fb000 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74 68 65 72  opcode.  Another
fb010 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f  .** opcode follo
fb020 77 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61  ws immediately a
fb030 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 6f 70  fter the last op
fb040 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  erand..**.** The
fb050 72 65 20 61 72 65 20 36 20 6f 70 63 6f 64 65 73  re are 6 opcodes
fb060 20 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30   numbered from 0
fb070 20 74 68 72 6f 75 67 68 20 35 2e 20 20 30 20 69   through 5.  0 i
fb080 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c 74 22 20  s the.** "halt" 
fb090 6f 70 63 6f 64 65 20 61 6e 64 20 63 61 75 73 65  opcode and cause
fb0a0 73 20 74 68 65 20 74 65 73 74 20 74 6f 20 65 6e  s the test to en
fb0b0 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20  d..**.**    0   
fb0c0 20 20 20 20 20 20 20 48 61 6c 74 20 61 6e 64 20         Halt and 
fb0d0 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
fb0e0 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a 2a 20 20  r of errors.**  
fb0f0 20 20 31 20 4e 20 53 20 58 20 20 20 20 53 65 74    1 N S X    Set
fb100 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69 6e   N bits beginnin
fb110 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e 63  g with S and inc
fb120 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a  rementing by X.*
fb130 2a 20 20 20 20 32 20 4e 20 53 20 58 20 20 20 20  *    2 N S X    
fb140 43 6c 65 61 72 20 4e 20 62 69 74 73 20 62 65 67  Clear N bits beg
fb150 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e  inning with S an
fb160 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62  d incrementing b
fb170 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20  y X.**    3 N   
fb180 20 20 20 20 20 53 65 74 20 4e 20 72 61 6e 64 6f       Set N rando
fb190 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73 0a  mly chosen bits.
fb1a0 2a 2a 20 20 20 20 34 20 4e 20 20 20 20 20 20 20  **    4 N       
fb1b0 20 43 6c 65 61 72 20 4e 20 72 61 6e 64 6f 6d 6c   Clear N randoml
fb1c0 79 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a  y chosen bits.**
fb1d0 20 20 20 20 35 20 4e 20 53 20 58 20 20 20 20 53      5 N S X    S
fb1e0 65 74 20 4e 20 62 69 74 73 20 66 72 6f 6d 20 53  et N bits from S
fb1f0 20 69 6e 63 72 65 6d 65 6e 74 20 58 20 69 6e 20   increment X in 
fb200 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20  array only, not 
fb210 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20  in bitvec.**.** 
fb220 54 68 65 20 6f 70 63 6f 64 65 73 20 31 20 74 68  The opcodes 1 th
fb230 72 6f 75 67 68 20 34 20 70 65 72 66 6f 72 6d 20  rough 4 perform 
fb240 73 65 74 20 61 6e 64 20 63 6c 65 61 72 20 6f 70  set and clear op
fb250 65 72 61 74 69 6f 6e 73 20 61 72 65 20 70 65 72  erations are per
fb260 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74  formed.** on bot
fb270 68 20 61 20 42 69 74 76 65 63 20 6f 62 6a 65 63  h a Bitvec objec
fb280 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 61  t and on a linea
fb290 72 20 61 72 72 61 79 20 6f 66 20 62 69 74 73 20  r array of bits 
fb2a0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
fb2b0 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20  lloc..** Opcode 
fb2c0 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68 65 20 6c  5 works on the l
fb2d0 69 6e 65 61 72 20 61 72 72 61 79 20 6f 6e 6c 79  inear array only
fb2e0 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20 42 69 74  , not on the Bit
fb2f0 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35  vec..** Opcode 5
fb300 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c 69   is used to deli
fb310 62 65 72 61 74 65 6c 79 20 69 6e 64 75 63 65 20  berately induce 
fb320 61 20 66 61 75 6c 74 20 69 6e 20 6f 72 64 65 72  a fault in order
fb330 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 74   to.** confirm t
fb340 68 61 74 20 65 72 72 6f 72 20 64 65 74 65 63 74  hat error detect
fb350 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a  ion works..**.**
fb360 20 41 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   At the conclusi
fb370 6f 6e 20 6f 66 20 74 68 65 20 74 65 73 74 20 74  on of the test t
fb380 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20  he linear array 
fb390 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 61  is compared.** a
fb3a0 67 61 69 6e 73 74 20 74 68 65 20 42 69 74 76 65  gainst the Bitve
fb3b0 63 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 74 68  c object.  If th
fb3c0 65 72 65 20 61 72 65 20 61 6e 79 20 64 69 66 66  ere are any diff
fb3d0 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 65  erences,.** an e
fb3e0 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
fb3f0 2e 20 20 49 66 20 74 68 65 79 20 61 72 65 20 74  .  If they are t
fb400 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f 20 69 73  he same, zero is
fb410 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
fb420 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   If a memory all
fb430 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63  ocation error oc
fb440 63 75 72 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e  curs, return -1.
fb450 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fb460 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69  TE int sqlite3Bi
fb470 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28  tvecBuiltinTest(
fb480 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70  int sz, int *aOp
fb490 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 42 69  ){.  Bitvec *pBi
fb4a0 74 76 65 63 20 3d 20 30 3b 0a 20 20 75 6e 73 69  tvec = 0;.  unsi
fb4b0 67 6e 65 64 20 63 68 61 72 20 2a 70 56 20 3d 20  gned char *pV = 
fb4c0 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 2d 31  0;.  int rc = -1
fb4d0 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78 2c 20 70  ;.  int i, nx, p
fb4e0 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a 20 41 6c 6c  c, op;..  /* All
fb4f0 6f 63 61 74 65 20 74 68 65 20 42 69 74 76 65 63  ocate the Bitvec
fb500 20 74 6f 20 62 65 20 74 65 73 74 65 64 20 61 6e   to be tested an
fb510 64 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79  d a linear array
fb520 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f   of.  ** bits to
fb530 20 61 63 74 20 61 73 20 74 68 65 20 72 65 66 65   act as the refe
fb540 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42 69 74 76  rence */.  pBitv
fb550 65 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  ec = sqlite3Bitv
fb560 65 63 43 72 65 61 74 65 28 20 73 7a 20 29 3b 0a  ecCreate( sz );.
fb570 20 20 70 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d    pV = sqlite3_m
fb580 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20  alloc( (sz+7)/8 
fb590 2b 20 31 20 29 3b 0a 20 20 69 66 28 20 70 42 69  + 1 );.  if( pBi
fb5a0 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 3d 3d 30  tvec==0 || pV==0
fb5b0 20 29 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 65   ) goto bitvec_e
fb5c0 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 56 2c  nd;.  memset(pV,
fb5d0 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31   0, (sz+7)/8 + 1
fb5e0 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65  );..  /* Run the
fb5f0 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 63   program */.  pc
fb600 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28   = 0;.  while( (
fb610 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30  op = aOp[pc])!=0
fb620 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20   ){.    switch( 
fb630 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  op ){.      case
fb640 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32   1:.      case 2
fb650 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 20  :.      case 5: 
fb660 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 34  {.        nx = 4
fb670 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 61 4f  ;.        i = aO
fb680 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 20  p[pc+2] - 1;.   
fb690 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d 20 2b       aOp[pc+2] +
fb6a0 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 20  = aOp[pc+3];.   
fb6b0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
fb6c0 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 33    }.      case 3
fb6d0 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 20  :.      case 4: 
fb6e0 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20  .      default: 
fb6f0 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 32  {.        nx = 2
fb700 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
fb710 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
fb720 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 20 20  eof(i), &i);.   
fb730 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
fb740 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
fb750 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 20  ( (--aOp[pc+1]) 
fb760 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a 20 20  > 0 ) nx = 0;.  
fb770 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 20    pc += nx;.    
fb780 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 66 66  i = (i & 0x7ffff
fb790 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 66 28  fff)%sz;.    if(
fb7a0 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 7b 0a   (op & 1)!=0 ){.
fb7b0 20 20 20 20 20 20 53 45 54 42 49 54 28 70 56 2c        SETBIT(pV,
fb7c0 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 69   (i+1));.      i
fb7d0 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 20 20  f( op!=5 ){.    
fb7e0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42      if( sqlite3B
fb7f0 69 74 76 65 63 53 65 74 28 70 42 69 74 76 65 63  itvecSet(pBitvec
fb800 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 62 69  , i+1) ) goto bi
fb810 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 20 20  tvec_end;.      
fb820 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
fb830 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 2c 20     CLEARBIT(pV, 
fb840 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 73 71  (i+1));.      sq
fb850 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72  lite3BitvecClear
fb860 28 70 42 69 74 76 65 63 2c 20 69 2b 31 29 3b 0a  (pBitvec, i+1);.
fb870 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
fb880 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72  Test to make sur
fb890 65 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72  e the linear arr
fb8a0 61 79 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68  ay exactly match
fb8b0 65 73 20 74 68 65 0a 20 20 2a 2a 20 42 69 74 76  es the.  ** Bitv
fb8c0 65 63 20 6f 62 6a 65 63 74 2e 20 20 53 74 61 72  ec object.  Star
fb8d0 74 20 77 69 74 68 20 74 68 65 20 61 73 73 75 6d  t with the assum
fb8e0 70 74 69 6f 6e 20 74 68 61 74 20 74 68 65 79 20  ption that they 
fb8f0 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 20 28 72  do.  ** match (r
fb900 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67 65 20 72  c==0).  Change r
fb910 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66  c to non-zero if
fb920 20 61 20 64 69 73 63 72 65 70 61 6e 63 79 0a 20   a discrepancy. 
fb930 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20   ** is found..  
fb940 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
fb950 33 42 69 74 76 65 63 54 65 73 74 28 30 2c 30 29  3BitvecTest(0,0)
fb960 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   + sqlite3Bitvec
fb970 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 73 7a  Test(pBitvec, sz
fb980 2b 31 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20  +1).          + 
fb990 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
fb9a0 74 28 70 42 69 74 76 65 63 2c 20 30 29 0a 20 20  t(pBitvec, 0).  
fb9b0 20 20 20 20 20 20 20 20 2b 20 28 73 71 6c 69 74          + (sqlit
fb9c0 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 69  e3BitvecSize(pBi
fb9d0 74 76 65 63 29 20 2d 20 73 7a 29 3b 0a 20 20 66  tvec) - sz);.  f
fb9e0 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69  or(i=1; i<=sz; i
fb9f0 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 20 28 54  ++){.    if(  (T
fba00 45 53 54 42 49 54 28 70 56 2c 69 29 29 21 3d 73  ESTBIT(pV,i))!=s
fba10 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
fba20 28 70 42 69 74 76 65 63 2c 69 29 20 29 7b 0a 20  (pBitvec,i) ){. 
fba30 20 20 20 20 20 72 63 20 3d 20 69 3b 0a 20 20 20       rc = i;.   
fba40 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
fba50 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61    }..  /* Free a
fba60 6c 6c 6f 63 61 74 65 64 20 73 74 72 75 63 74 75  llocated structu
fba70 72 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65 6e 64  re */.bitvec_end
fba80 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  :.  sqlite3_free
fba90 28 70 56 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  (pV);.  sqlite3B
fbaa0 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 42 69  itvecDestroy(pBi
fbab0 74 76 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  tvec);.  return 
fbac0 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  rc;.}.#endif /* 
fbad0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
fbae0 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a  TIN_TEST */../**
fbaf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
fbb00 20 6f 66 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a   of bitvec.c ***
fbb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
fbb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
fbb50 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65 2e 63  in file pcache.c
fbb60 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
fbb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
fbb90 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 20 30  ** 2008 August 0
fbba0 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
fbbb0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
fbbc0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
fbbd0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
fbbe0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
fbbf0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
fbc00 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
fbc10 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
fbc20 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
fbc30 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
fbc40 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
fbc50 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
fbc60 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
fbc70 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
fbc80 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
fbc90 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
fbca0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
fbcb0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
fbcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fbd00 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
fbd10 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 70 61  plements that pa
fbd20 67 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20  ge cache..**.** 
fbd30 40 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 65  @(#) $Id: pcache
fbd40 2e 63 2c 76 20 31 2e 34 34 20 32 30 30 39 2f 30  .c,v 1.44 2009/0
fbd50 33 2f 33 31 20 30 31 3a 33 32 3a 31 38 20 64 72  3/31 01:32:18 dr
fbd60 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
fbd70 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67  * A complete pag
fbd80 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e  e cache is an in
fbd90 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
fbda0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72  tructure..*/.str
fbdb0 75 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50  uct PCache {.  P
fbdc0 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70  gHdr *pDirty, *p
fbdd0 44 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20  DirtyTail;      
fbde0 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69     /* List of di
fbdf0 72 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55  rty pages in LRU
fbe00 20 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64   order */.  PgHd
fbe10 72 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20  r *pSynced;     
fbe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbe30 2f 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70  /* Last synced p
fbe40 61 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67  age in dirty pag
fbe50 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  e list */.  int 
fbe60 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
fbe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbe80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66  /* Number of ref
fbe90 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f  erenced pages */
fbea0 0a 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20  .  int nMax;    
fbeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbec0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67         /* Config
fbed0 75 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20  ured cache size 
fbee0 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b  */.  int szPage;
fbef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbf00 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
fbf10 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 69   of every page i
fbf20 6e 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a  n this cache */.
fbf30 20 20 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20    int szExtra;  
fbf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbf50 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
fbf60 20 65 78 74 72 61 20 73 70 61 63 65 20 66 6f 72   extra space for
fbf70 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20   each page */.  
fbf80 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20  int bPurgeable; 
fbf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbfa0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
fbfb0 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b  ages are on back
fbfc0 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69  ing store */.  i
fbfd0 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f  nt (*xStress)(vo
fbfe0 69 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20  id*,PgHdr*);    
fbff0 20 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72     /* Call to tr
fc000 79 20 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c  y make a page cl
fc010 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ean */.  void *p
fc020 53 74 72 65 73 73 3b 20 20 20 20 20 20 20 20 20  Stress;         
fc030 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fc040 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72  Argument to xStr
fc050 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ess */.  sqlite3
fc060 5f 70 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b  _pcache *pCache;
fc070 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fc080 50 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20  Pluggable cache 
fc090 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64  module */.  PgHd
fc0a0 72 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20  r *pPage1;      
fc0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fc0c0 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20  /* Reference to 
fc0d0 70 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  page 1 */.};../*
fc0e0 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20  .** Some of the 
fc0f0 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20  assert() macros 
fc100 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72 65  in this code are
fc110 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74   too expensive t
fc120 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75  o run.** even du
fc130 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75  ring normal debu
fc140 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d  gging.  Use them
fc150 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20   only rarely on 
fc160 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20  long-running.** 
fc170 74 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74  tests.  Enable t
fc180 68 65 20 65 78 70 65 6e 73 69 76 65 20 61 73 73  he expensive ass
fc190 65 72 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a  erts using the.*
fc1a0 2a 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c  * -DSQLITE_ENABL
fc1b0 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45  E_EXPENSIVE_ASSE
fc1c0 52 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  RT=1 compile-tim
fc1d0 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66  e option..*/.#if
fc1e0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
fc1f0 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45  E_EXPENSIVE_ASSE
fc200 52 54 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65  RT.# define expe
fc210 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 20  nsive_assert(X) 
fc220 20 61 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65   assert(X).#else
fc230 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73  .# define expens
fc240 69 76 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65  ive_assert(X).#e
fc250 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
fc260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fc270 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64  ********* Linked
fc280 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74   List Management
fc290 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
fc2a0 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66  *****/..#if !def
fc2b0 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20  ined(NDEBUG) && 
fc2c0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
fc2d0 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f  NABLE_EXPENSIVE_
fc2e0 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68  ASSERT)./*.** Ch
fc2f0 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61  eck that the pCa
fc300 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72  che->pSynced var
fc310 69 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72  iable is set cor
fc320 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a  rectly. If it.**
fc330 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20   is not, either 
fc340 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f  fail an assert o
fc350 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f  r return zero. O
fc360 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
fc370 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68  .** non-zero. Th
fc380 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  is is only used 
fc390 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62 75 69  in debugging bui
fc3a0 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  lds, as follows:
fc3b0 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69  .**.**   expensi
fc3c0 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68  ve_assert( pcach
fc3d0 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61  eCheckSynced(pCa
fc3e0 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69  che) );.*/.stati
fc3f0 63 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 63  c int pcacheChec
fc400 6b 53 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a  kSynced(PCache *
fc410 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72  pCache){.  PgHdr
fc420 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61   *p;.  for(p=pCa
fc430 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b  che->pDirtyTail;
fc440 20 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e   p!=pCache->pSyn
fc450 63 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79  ced; p=p->pDirty
fc460 50 72 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72  Prev){.    asser
fc470 74 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70  t( p->nRef || (p
fc480 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
fc490 45 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a  ED_SYNC) );.  }.
fc4a0 20 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c    return (p==0 |
fc4b0 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d  | p->nRef || (p-
fc4c0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
fc4d0 44 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23  D_SYNC)==0);.}.#
fc4e0 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47  endif /* !NDEBUG
fc4f0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
fc500 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45  E_EXPENSIVE_ASSE
fc510 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d  RT */../*.** Rem
fc520 6f 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66  ove page pPage f
fc530 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20  rom the list of 
fc540 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a  dirty pages..*/.
fc550 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
fc560 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
fc570 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 61  yList(PgHdr *pPa
fc580 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70  ge){.  PCache *p
fc590 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65   = pPage->pCache
fc5a0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
fc5b0 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c  ge->pDirtyNext |
fc5c0 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72  | pPage==p->pDir
fc5d0 74 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65  tyTail );.  asse
fc5e0 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74  rt( pPage->pDirt
fc5f0 79 50 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d  yPrev || pPage==
fc600 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20  p->pDirty );..  
fc610 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43  /* Update the PC
fc620 61 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61  ache1.pSynced va
fc630 72 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73  riable if necess
fc640 61 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d  ary. */.  if( p-
fc650 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20  >pSynced==pPage 
fc660 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53  ){.    PgHdr *pS
fc670 79 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70  ynced = pPage->p
fc680 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77  DirtyPrev;.    w
fc690 68 69 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26  hile( pSynced &&
fc6a0 20 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73   (pSynced->flags
fc6b0 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
fc6c0 29 20 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63  ) ){.      pSync
fc6d0 65 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44  ed = pSynced->pD
fc6e0 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a  irtyPrev;.    }.
fc6f0 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d      p->pSynced =
fc700 20 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20   pSynced;.  }.. 
fc710 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72   if( pPage->pDir
fc720 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50  tyNext ){.    pP
fc730 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d  age->pDirtyNext-
fc740 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50  >pDirtyPrev = pP
fc750 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b  age->pDirtyPrev;
fc760 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
fc770 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e  sert( pPage==p->
fc780 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20  pDirtyTail );.  
fc790 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20    p->pDirtyTail 
fc7a0 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  = pPage->pDirtyP
fc7b0 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  rev;.  }.  if( p
fc7c0 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76  Page->pDirtyPrev
fc7d0 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70   ){.    pPage->p
fc7e0 44 69 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74  DirtyPrev->pDirt
fc7f0 79 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70  yNext = pPage->p
fc800 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c  DirtyNext;.  }el
fc810 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
fc820 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79  pPage==p->pDirty
fc830 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74   );.    p->pDirt
fc840 79 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74  y = pPage->pDirt
fc850 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61  yNext;.  }.  pPa
fc860 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d  ge->pDirtyNext =
fc870 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69   0;.  pPage->pDi
fc880 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20  rtyPrev = 0;..  
fc890 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74  expensive_assert
fc8a0 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e  ( pcacheCheckSyn
fc8b0 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a  ced(p) );.}../*.
fc8c0 2a 2a 20 41 64 64 20 70 61 67 65 20 70 50 61 67  ** Add page pPag
fc8d0 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  e to the head of
fc8e0 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20   the dirty list 
fc8f0 28 50 43 61 63 68 65 31 2e 70 44 69 72 74 79 20  (PCache1.pDirty 
fc900 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61  is set to.** pPa
fc910 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ge)..*/.static v
fc920 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 44  oid pcacheAddToD
fc930 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a  irtyList(PgHdr *
fc940 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65  pPage){.  PCache
fc950 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61   *p = pPage->pCa
fc960 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  che;..  assert( 
fc970 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
fc980 74 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70  t==0 && pPage->p
fc990 44 69 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20  DirtyPrev==0 && 
fc9a0 70 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65  p->pDirty!=pPage
fc9b0 20 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44   );..  pPage->pD
fc9c0 69 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44  irtyNext = p->pD
fc9d0 69 72 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67  irty;.  if( pPag
fc9e0 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b  e->pDirtyNext ){
fc9f0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
fca00 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e  ge->pDirtyNext->
fca10 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b  pDirtyPrev==0 );
fca20 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72  .    pPage->pDir
fca30 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72  tyNext->pDirtyPr
fca40 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a  ev = pPage;.  }.
fca50 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50    p->pDirty = pP
fca60 61 67 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70  age;.  if( !p->p
fca70 44 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20  DirtyTail ){.   
fca80 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d   p->pDirtyTail =
fca90 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66   pPage;.  }.  if
fcaa0 28 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26  ( !p->pSynced &&
fcab0 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67   0==(pPage->flag
fcac0 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
fcad0 43 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79  C) ){.    p->pSy
fcae0 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20  nced = pPage;.  
fcaf0 7d 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73  }.  expensive_as
fcb00 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63  sert( pcacheChec
fcb10 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a  kSynced(p) );.}.
fcb20 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61  ./*.** Wrapper a
fcb30 72 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61  round the plugga
fcb40 62 6c 65 20 63 61 63 68 65 73 20 78 55 6e 70 69  ble caches xUnpi
fcb50 6e 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65  n method. If the
fcb60 20 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69   cache is.** bei
fcb70 6e 67 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69  ng used for an i
fcb80 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
fcb90 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  e, this function
fcba0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
fcbb0 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
fcbc0 68 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70  heUnpin(PgHdr *p
fcbd0 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  ){.  PCache *pCa
fcbe0 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
fcbf0 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62  .  if( pCache->b
fcc00 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20  Purgeable ){.   
fcc10 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20   if( p->pgno==1 
fcc20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d  ){.      pCache-
fcc30 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20  >pPage1 = 0;.   
fcc40 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c   }.    sqlite3Gl
fcc50 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
fcc60 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d  e.xUnpin(pCache-
fcc70 3e 70 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a  >pCache, p, 0);.
fcc80 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
fcc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fccb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65  *********** Gene
fccc0 72 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a  ral Interfaces *
fccd0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74  *****.**.** Init
fcce0 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64  ialize and shutd
fccf0 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63  own the page cac
fcd00 68 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65  he subsystem. Ne
fcd10 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a  ither of these .
fcd20 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  ** functions are
fcd30 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a   threadsafe..*/.
fcd40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
fcd50 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
fcd60 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29  Initialize(void)
fcd70 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  {.  if( sqlite3G
fcd80 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
fcd90 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20  he.xInit==0 ){. 
fcda0 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65     sqlite3PCache
fcdb0 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20  SetDefault();.  
fcdc0 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
fcdd0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
fcde0 63 61 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69  cache.xInit(sqli
fcdf0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
fce00 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a  pcache.pArg);.}.
fce10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
fce20 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
fce30 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b  eShutdown(void){
fce40 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
fce50 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
fce60 65 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20  e.xShutdown ){. 
fce70 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
fce80 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53  Config.pcache.xS
fce90 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47  hutdown(sqlite3G
fcea0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
fceb0 68 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a  he.pArg);.  }.}.
fcec0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
fced0 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
fcee0 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65  of a PCache obje
fcef0 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ct..*/.SQLITE_PR
fcf00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
fcf10 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64  3PcacheSize(void
fcf20 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66  ){ return sizeof
fcf30 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a  (PCache); }../*.
fcf40 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
fcf50 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53  PCache object. S
fcf60 74 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f 20  torage space to 
fcf70 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a  hold the object.
fcf80 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ** has already b
fcf90 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e  een allocated an
fcfa0 64 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61  d is passed in a
fcfb0 73 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e  s the p pointer.
fcfc0 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20   .** The caller 
fcfd0 64 69 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75  discovers how mu
fcfe0 63 68 20 73 70 61 63 65 20 6e 65 65 64 73 20 74  ch space needs t
fcff0 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62  o be allocated b
fd000 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71  y .** calling sq
fd010 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28  lite3PcacheSize(
fd020 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
fd030 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
fd040 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69  3PcacheOpen(.  i
fd050 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20  nt szPage,      
fd060 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
fd070 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67  ize of every pag
fd080 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74  e */.  int szExt
fd090 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ra,             
fd0a0 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61      /* Extra spa
fd0b0 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ce associated wi
fd0c0 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  th each page */.
fd0d0 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65    int bPurgeable
fd0e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
fd0f0 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20  * True if pages 
fd100 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73  are on backing s
fd110 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  tore */.  int (*
fd120 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50  xStress)(void*,P
fd130 67 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74  gHdr*),/* Call t
fd140 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61  o try to make pa
fd150 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76  ges clean */.  v
fd160 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20  oid *pStress,   
fd170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
fd180 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65  rgument to xStre
fd190 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a  ss */.  PCache *
fd1a0 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
fd1b0 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63       /* Prealloc
fd1c0 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74  ated space for t
fd1d0 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a  he PCache */.){.
fd1e0 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73    memset(p, 0, s
fd1f0 69 7a 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a  izeof(PCache));.
fd200 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a    p->szPage = sz
fd210 50 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74  Page;.  p->szExt
fd220 72 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20  ra = szExtra;.  
fd230 70 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20  p->bPurgeable = 
fd240 62 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d  bPurgeable;.  p-
fd250 3e 78 53 74 72 65 73 73 20 3d 20 78 53 74 72 65  >xStress = xStre
fd260 73 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73  ss;.  p->pStress
fd270 20 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d   = pStress;.  p-
fd280 3e 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a  >nMax = 100;.}..
fd290 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
fd2a0 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50   page size for P
fd2b0 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68  Cache object. Th
fd2c0 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e  e caller must en
fd2d0 73 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0a  sure that there.
fd2e0 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61  ** are no outsta
fd2f0 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72  nding page refer
fd300 65 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20  ences when this 
fd310 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
fd320 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
fd330 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
fd340 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53  e3PcacheSetPageS
fd350 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63  ize(PCache *pCac
fd360 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b  he, int szPage){
fd370 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68  .  assert( pCach
fd380 65 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43  e->nRef==0 && pC
fd390 61 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20  ache->pDirty==0 
fd3a0 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  );.  if( pCache-
fd3b0 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73  >pCache ){.    s
fd3c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
fd3d0 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72  ig.pcache.xDestr
fd3e0 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  oy(pCache->pCach
fd3f0 65 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  e);.    pCache->
fd400 70 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a  pCache = 0;.  }.
fd410 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65    pCache->szPage
fd420 20 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a   = szPage;.}../*
fd430 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69  .** Try to obtai
fd440 6e 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  n a page from th
fd450 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49  e cache..*/.SQLI
fd460 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
fd470 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63  qlite3PcacheFetc
fd480 68 28 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  h(.  PCache *pCa
fd490 63 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62  che,       /* Ob
fd4a0 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 72  tain the page fr
fd4b0 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f  om this cache */
fd4c0 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20  .  Pgno pgno,   
fd4d0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
fd4e0 20 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69   number to obtai
fd4f0 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74  n */.  int creat
fd500 65 46 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20  eFlag,       /* 
fd510 49 66 20 74 72 75 65 2c 20 63 72 65 61 74 65 20  If true, create 
fd520 70 61 67 65 20 69 66 20 69 74 20 64 6f 65 73 20  page if it does 
fd530 6e 6f 74 20 65 78 69 73 74 20 61 6c 72 65 61 64  not exist alread
fd540 79 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70  y */.  PgHdr **p
fd550 70 50 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20  pPage        /* 
fd560 57 72 69 74 65 20 74 68 65 20 70 61 67 65 20 68  Write the page h
fd570 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64  ere */.){.  PgHd
fd580 72 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20  r *pPage = 0;.  
fd590 69 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20  int eCreate;..  
fd5a0 61 73 73 65 72 74 28 20 70 43 61 63 68 65 21 3d  assert( pCache!=
fd5b0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
fd5c0 67 6e 6f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49  gno>0 );..  /* I
fd5d0 66 20 74 68 65 20 70 6c 75 67 67 61 62 6c 65 20  f the pluggable 
fd5e0 63 61 63 68 65 20 28 73 71 6c 69 74 65 33 5f 70  cache (sqlite3_p
fd5f0 63 61 63 68 65 2a 29 20 68 61 73 20 6e 6f 74 20  cache*) has not 
fd600 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 0a  been allocated,.
fd610 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 69 74    ** allocate it
fd620 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   now..  */.  if(
fd630 20 21 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65   !pCache->pCache
fd640 20 26 26 20 63 72 65 61 74 65 46 6c 61 67 20 29   && createFlag )
fd650 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 70 63  {.    sqlite3_pc
fd660 61 63 68 65 20 2a 70 3b 0a 20 20 20 20 69 6e 74  ache *p;.    int
fd670 20 6e 42 79 74 65 3b 0a 20 20 20 20 6e 42 79 74   nByte;.    nByt
fd680 65 20 3d 20 70 43 61 63 68 65 2d 3e 73 7a 50 61  e = pCache->szPa
fd690 67 65 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45  ge + pCache->szE
fd6a0 78 74 72 61 20 2b 20 73 69 7a 65 6f 66 28 50 67  xtra + sizeof(Pg
fd6b0 48 64 72 29 3b 0a 20 20 20 20 70 20 3d 20 73 71  Hdr);.    p = sq
fd6c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
fd6d0 67 2e 70 63 61 63 68 65 2e 78 43 72 65 61 74 65  g.pcache.xCreate
fd6e0 28 6e 42 79 74 65 2c 20 70 43 61 63 68 65 2d 3e  (nByte, pCache->
fd6f0 62 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 20  bPurgeable);.   
fd700 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20   if( !p ){.     
fd710 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
fd720 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
fd730 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
fd740 66 69 67 2e 70 63 61 63 68 65 2e 78 43 61 63 68  fig.pcache.xCach
fd750 65 73 69 7a 65 28 70 2c 20 70 43 61 63 68 65 2d  esize(p, pCache-
fd760 3e 6e 4d 61 78 29 3b 0a 20 20 20 20 70 43 61 63  >nMax);.    pCac
fd770 68 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 3b 0a  he->pCache = p;.
fd780 20 20 7d 0a 0a 20 20 65 43 72 65 61 74 65 20 3d    }..  eCreate =
fd790 20 63 72 65 61 74 65 46 6c 61 67 20 3f 20 31 20   createFlag ? 1 
fd7a0 3a 20 30 3b 0a 20 20 69 66 28 20 65 43 72 65 61  : 0;.  if( eCrea
fd7b0 74 65 20 26 26 20 28 21 70 43 61 63 68 65 2d 3e  te && (!pCache->
fd7c0 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 21 70  bPurgeable || !p
fd7d0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 29 20 29  Cache->pDirty) )
fd7e0 7b 0a 20 20 20 20 65 43 72 65 61 74 65 20 3d 20  {.    eCreate = 
fd7f0 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 61  2;.  }.  if( pCa
fd800 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20  che->pCache ){. 
fd810 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74     pPage = sqlit
fd820 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
fd830 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 61  cache.xFetch(pCa
fd840 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e  che->pCache, pgn
fd850 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20 20 7d  o, eCreate);.  }
fd860 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 20 26  ..  if( !pPage &
fd870 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29 7b 0a  & eCreate==1 ){.
fd880 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a      PgHdr *pPg;.
fd890 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 20 64  .    /* Find a d
fd8a0 69 72 74 79 20 70 61 67 65 20 74 6f 20 77 72 69  irty page to wri
fd8b0 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63 79 63  te-out and recyc
fd8c0 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20 74 6f  le. First try to
fd8d0 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a 2a 20   find a .    ** 
fd8e0 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 6e  page that does n
fd8f0 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75  ot require a jou
fd900 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 20 77  rnal-sync (one w
fd910 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  ith PGHDR_NEED_S
fd920 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65 61 72  YNC.    ** clear
fd930 65 64 29 2c 20 62 75 74 20 69 66 20 74 68 61 74  ed), but if that
fd940 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
fd950 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e 79 20   settle for any 
fd960 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 75 6e  other .    ** un
fd970 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 74 79  referenced dirty
fd980 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   page..    */.  
fd990 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
fd9a0 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53  rt( pcacheCheckS
fd9b0 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b  ynced(pCache) );
fd9c0 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 61  .    for(pPg=pCa
fd9d0 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 0a 20  che->pSynced; . 
fd9e0 20 20 20 20 20 20 20 70 50 67 20 26 26 20 28 70         pPg && (p
fd9f0 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 50 67  Pg->nRef || (pPg
fda00 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
fda10 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 20 20  ED_SYNC)); .    
fda20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 44 69      pPg=pPg->pDi
fda30 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b 0a 20  rtyPrev.    );. 
fda40 20 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a 20     if( !pPg ){. 
fda50 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 61       for(pPg=pCa
fda60 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b  che->pDirtyTail;
fda70 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52 65   pPg && pPg->nRe
fda80 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69 72  f; pPg=pPg->pDir
fda90 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a 20  tyPrev);.    }. 
fdaa0 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20     if( pPg ){.  
fdab0 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
fdac0 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e 78    rc = pCache->x
fdad0 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e 70  Stress(pCache->p
fdae0 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20 20  Stress, pPg);.  
fdaf0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
fdb00 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c  TE_OK && rc!=SQL
fdb10 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
fdb20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
fdb30 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
fdb40 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74 65    pPage = sqlite
fdb50 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
fdb60 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 61 63  ache.xFetch(pCac
fdb70 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e 6f  he->pCache, pgno
fdb80 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  , 2);.  }..  if(
fdb90 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 69 66   pPage ){.    if
fdba0 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74 61 20  ( !pPage->pData 
fdbb0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  ){.      memset(
fdbc0 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f 66  pPage, 0, sizeof
fdbd0 28 50 67 48 64 72 29 20 2b 20 70 43 61 63 68 65  (PgHdr) + pCache
fdbe0 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 20 20  ->szExtra);.    
fdbf0 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 61 20    pPage->pExtra 
fdc00 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67 65 5b  = (void*)&pPage[
fdc10 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d  1];.      pPage-
fdc20 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 20 2a  >pData = (void *
fdc30 29 26 28 28 63 68 61 72 20 2a 29 70 50 61 67 65  )&((char *)pPage
fdc40 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 29 20  )[sizeof(PgHdr) 
fdc50 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72  + pCache->szExtr
fdc60 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d  a];.      pPage-
fdc70 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65  >pCache = pCache
fdc80 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70  ;.      pPage->p
fdc90 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20  gno = pgno;.    
fdca0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  }.    assert( pP
fdcb0 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61  age->pCache==pCa
fdcc0 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72  che );.    asser
fdcd0 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d  t( pPage->pgno==
fdce0 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73 73 65  pgno );.    asse
fdcf0 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78 74 72  rt( pPage->pExtr
fdd00 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50 61 67  a==(void *)&pPag
fdd10 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69 66 28  e[1] );..    if(
fdd20 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65 66 20   0==pPage->nRef 
fdd30 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d  ){.      pCache-
fdd40 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20  >nRef++;.    }. 
fdd50 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66 2b 2b     pPage->nRef++
fdd60 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d  ;.    if( pgno==
fdd70 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68  1 ){.      pCach
fdd80 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 67  e->pPage1 = pPag
fdd90 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a  e;.    }.  }.  *
fdda0 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a  ppPage = pPage;.
fddb0 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 3d    return (pPage=
fddc0 3d 30 20 26 26 20 65 43 72 65 61 74 65 29 20 3f  =0 && eCreate) ?
fddd0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20   SQLITE_NOMEM : 
fdde0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
fddf0 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68  .** Decrement th
fde00 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
fde10 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 66 20  t on a page. If 
fde20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c 65 61  the page is clea
fde30 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65 66  n and the.** ref
fde40 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 72 6f  erence count dro
fde50 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 69 74  ps to 0, then it
fde60 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c 65 20   is made elible 
fde70 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e 0a 2a  for recycling..*
fde80 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
fde90 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
fdea0 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 72  cheRelease(PgHdr
fdeb0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
fdec0 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 70  p->nRef>0 );.  p
fded0 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20  ->nRef--;.  if( 
fdee0 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  p->nRef==0 ){.  
fdef0 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65    PCache *pCache
fdf00 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20   = p->pCache;.  
fdf10 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d 2d    pCache->nRef--
fdf20 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e 66 6c  ;.    if( (p->fl
fdf30 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29  ags&PGHDR_DIRTY)
fdf40 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 61  ==0 ){.      pca
fdf50 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 20 20  cheUnpin(p);.   
fdf60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
fdf70 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 74   Move the page t
fdf80 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68  o the head of th
fdf90 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20 2a 2f  e dirty list. */
fdfa0 0a 20 20 20 20 20 20 70 63 61 63 68 65 52 65 6d  .      pcacheRem
fdfb0 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 74  oveFromDirtyList
fdfc0 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68  (p);.      pcach
fdfd0 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74 28  eAddToDirtyList(
fdfe0 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  p);.    }.  }.}.
fdff0 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73 65 20  ./*.** Increase 
fe000 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
fe010 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c 69 65  unt of a supplie
fe020 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a 2f 0a  d page by 1..*/.
fe030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
fe040 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
fe050 65 52 65 66 28 50 67 48 64 72 20 2a 70 29 7b 0a  eRef(PgHdr *p){.
fe060 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52 65 66    assert(p->nRef
fe070 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b  >0);.  p->nRef++
fe080 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20  ;.}../*.** Drop 
fe090 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
fe0a0 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d 75 73  cache. There mus
fe0b0 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65  t be exactly one
fe0c0 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
fe0d0 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69 73 20  e.** page. This 
fe0e0 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73  function deletes
fe0f0 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65 2c   that reference,
fe100 20 73 6f 20 61 66 74 65 72 20 69 74 20 72 65 74   so after it ret
fe110 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61 67 65  urns the.** page
fe120 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
fe130 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a   is invalid..*/.
fe140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
fe150 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
fe160 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 29 7b  eDrop(PgHdr *p){
fe170 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68  .  PCache *pCach
fe180 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  e;.  assert( p->
fe190 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66 28  nRef==1 );.  if(
fe1a0 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f   p->flags&PGHDR_
fe1b0 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70 63 61  DIRTY ){.    pca
fe1c0 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72  cheRemoveFromDir
fe1d0 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 20  tyList(p);.  }. 
fe1e0 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61   pCache = p->pCa
fe1f0 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d 3e 6e  che;.  pCache->n
fe200 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d 3e  Ref--;.  if( p->
fe210 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 70  pgno==1 ){.    p
fe220 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20  Cache->pPage1 = 
fe230 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
fe240 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
fe250 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 68  che.xUnpin(pCach
fe260 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 31 29  e->pCache, p, 1)
fe270 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
fe280 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73  sure the page is
fe290 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79   marked as dirty
fe2a0 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 64 69  . If it isn't di
fe2b0 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20  rty already,.** 
fe2c0 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53  make it so..*/.S
fe2d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
fe2e0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
fe2f0 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 20  MakeDirty(PgHdr 
fe300 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 73 20  *p){.  p->flags 
fe310 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 5f 57  &= ~PGHDR_DONT_W
fe320 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 28 20  RITE;.  assert( 
fe330 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 69  p->nRef>0 );.  i
fe340 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 73 20  f( 0==(p->flags 
fe350 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 29  & PGHDR_DIRTY) )
fe360 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c  {.    p->flags |
fe370 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a 20  = PGHDR_DIRTY;. 
fe380 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69     pcacheAddToDi
fe390 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20 7d  rtyList( p);.  }
fe3a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
fe3b0 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73 20  ure the page is 
fe3c0 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e 2e  marked as clean.
fe3d0 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c 65   If it isn't cle
fe3e0 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d  an already,.** m
fe3f0 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51  ake it so..*/.SQ
fe400 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
fe410 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d  d sqlite3PcacheM
fe420 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 2a  akeClean(PgHdr *
fe430 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c  p){.  if( (p->fl
fe440 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54  ags & PGHDR_DIRT
fe450 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65  Y) ){.    pcache
fe460 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c  RemoveFromDirtyL
fe470 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66  ist(p);.    p->f
fe480 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52 5f  lags &= ~(PGHDR_
fe490 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45 44  DIRTY|PGHDR_NEED
fe4a0 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 20  _SYNC);.    if( 
fe4b0 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  p->nRef==0 ){.  
fe4c0 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28      pcacheUnpin(
fe4d0 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  p);.    }.  }.}.
fe4e0 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65 72  ./*.** Make ever
fe4f0 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61  y page in the ca
fe500 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51  che clean..*/.SQ
fe510 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
fe520 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43  d sqlite3PcacheC
fe530 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20 2a  leanAll(PCache *
fe540 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72  pCache){.  PgHdr
fe550 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28 70   *p;.  while( (p
fe560 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74   = pCache->pDirt
fe570 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  y)!=0 ){.    sql
fe580 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c  ite3PcacheMakeCl
fe590 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ean(p);.  }.}../
fe5a0 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 50  *.** Clear the P
fe5b0 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66  GHDR_NEED_SYNC f
fe5c0 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69 72  lag from all dir
fe5d0 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51 4c  ty pages..*/.SQL
fe5e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
fe5f0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
fe600 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61  earSyncFlags(PCa
fe610 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  che *pCache){.  
fe620 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28  PgHdr *p;.  for(
fe630 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  p=pCache->pDirty
fe640 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79  ; p; p=p->pDirty
fe650 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66 6c  Next){.    p->fl
fe660 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45  ags &= ~PGHDR_NE
fe670 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 70  ED_SYNC;.  }.  p
fe680 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 3d  Cache->pSynced =
fe690 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54   pCache->pDirtyT
fe6a0 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  ail;.}../*.** Ch
fe6b0 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e 75  ange the page nu
fe6c0 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 20 74  mber of page p t
fe6d0 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 53  o newPgno. .*/.S
fe6e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
fe6f0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
fe700 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 50  Move(PgHdr *p, P
fe710 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20  gno newPgno){.  
fe720 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d  PCache *pCache =
fe730 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61 73   p->pCache;.  as
fe740 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20  sert( p->nRef>0 
fe750 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65 77  );.  assert( new
fe760 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c 69  Pgno>0 );.  sqli
fe770 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
fe780 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70 43  pcache.xRekey(pC
fe790 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c  ache->pCache, p,
fe7a0 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67 6e   p->pgno, newPgn
fe7b0 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d 20  o);.  p->pgno = 
fe7c0 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20 28  newPgno;.  if( (
fe7d0 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  p->flags&PGHDR_D
fe7e0 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c 61  IRTY) && (p->fla
fe7f0 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
fe800 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68  NC) ){.    pcach
fe810 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79  eRemoveFromDirty
fe820 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63 61  List(p);.    pca
fe830 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73  cheAddToDirtyLis
fe840 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  t(p);.  }.}../*.
fe850 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 61  ** Drop every ca
fe860 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 20  che entry whose 
fe870 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67  page number is g
fe880 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 6e  reater than "pgn
fe890 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 65  o". The.** calle
fe8a0 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68  r must ensure th
fe8b0 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  at there are no 
fe8c0 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65  outstanding refe
fe8d0 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61  rences to any pa
fe8e0 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61  ges.** other tha
fe8f0 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61 20  n page 1 with a 
fe900 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65 61  page number grea
fe910 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a 2a  ter than pgno..*
fe920 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  *.** If there is
fe930 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
fe940 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 20 70  page 1 and the p
fe950 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70 61  gno parameter pa
fe960 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20  ssed to this.** 
fe970 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20 74  function is 0, t
fe980 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 72 65  hen the data are
fe990 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
fe9a0 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 72 6f  h page 1 is zero
fe9b0 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20 70  ed, but.** the p
fe9c0 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f  age object is no
fe9d0 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51  t dropped..*/.SQ
fe9e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
fe9f0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54  d sqlite3PcacheT
fea00 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 20 2a  runcate(PCache *
fea10 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67 6e  pCache, Pgno pgn
fea20 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65  o){.  if( pCache
fea30 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20  ->pCache ){.    
fea40 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 67  PgHdr *p;.    Pg
fea50 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20 20  Hdr *pNext;.    
fea60 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44  for(p=pCache->pD
fea70 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78 74  irty; p; p=pNext
fea80 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20 3d  ){.      pNext =
fea90 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a   p->pDirtyNext;.
feaa0 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e        if( p->pgn
feab0 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20  o>pgno ){.      
feac0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61    assert( p->fla
fead0 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29  gs&PGHDR_DIRTY )
feae0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
feaf0 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e  3PcacheMakeClean
feb00 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  (p);.      }.   
feb10 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d   }.    if( pgno=
feb20 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 50  =0 && pCache->pP
feb30 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d 65  age1 ){.      me
feb40 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50 61  mset(pCache->pPa
feb50 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70  ge1->pData, 0, p
feb60 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b 0a  Cache->szPage);.
feb70 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b 0a        pgno = 1;.
feb80 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
feb90 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
feba0 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28 70  ache.xTruncate(p
febb0 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
febc0 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  gno+1);.  }.}../
febd0 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61 63  *.** Close a cac
febe0 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  he..*/.SQLITE_PR
febf0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
fec00 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 43  e3PcacheClose(PC
fec10 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
fec20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61   if( pCache->pCa
fec30 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  che ){.    sqlit
fec40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
fec50 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28 70  cache.xDestroy(p
fec60 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a  Cache->pCache);.
fec70 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 69    }.}../* .** Di
fec80 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e  scard the conten
fec90 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 2e  ts of the cache.
feca0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
fecb0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
fecc0 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 68  cacheClear(PCach
fecd0 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 73 71  e *pCache){.  sq
fece0 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63  lite3PcacheTrunc
fecf0 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a  ate(pCache, 0);.
fed00 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74  }../*.** Merge t
fed10 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 67 65  wo lists of page
fed20 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70  s connected by p
fed30 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 67 6e  Dirty and in pgn
fed40 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f 20 6e  o order..** Do n
fed50 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 20 74  ot both fixing t
fed60 68 65 20 70 44 69 72 74 79 50 72 65 76 20 70 6f  he pDirtyPrev po
fed70 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  inters..*/.stati
fed80 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65 4d  c PgHdr *pcacheM
fed90 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 50 67  ergeDirtyList(Pg
feda0 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20 2a  Hdr *pA, PgHdr *
fedb0 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 65 73  pB){.  PgHdr res
fedc0 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 70  ult, *pTail;.  p
fedd0 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 3b 0a  Tail = &result;.
fede0 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70    while( pA && p
fedf0 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41 2d  B ){.    if( pA-
fee00 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f 20 29  >pgno<pB->pgno )
fee10 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70  {.      pTail->p
fee20 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 20 20  Dirty = pA;.    
fee30 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a 20 20    pTail = pA;.  
fee40 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 44 69      pA = pA->pDi
fee50 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rty;.    }else{.
fee60 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 69        pTail->pDi
fee70 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20 20  rty = pB;.      
fee80 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20 20  pTail = pB;.    
fee90 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72 74    pB = pB->pDirt
feea0 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  y;.    }.  }.  i
feeb0 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 54 61  f( pA ){.    pTa
feec0 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b  il->pDirty = pA;
feed0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 20  .  }else if( pB 
feee0 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 44  ){.    pTail->pD
feef0 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d 65 6c  irty = pB;.  }el
fef00 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70  se{.    pTail->p
fef10 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d 0a 20  Dirty = 0;.  }. 
fef20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 2e 70   return result.p
fef30 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Dirty;.}../*.** 
fef40 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 6f 66  Sort the list of
fef50 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64   pages in accend
fef60 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 67 6e  ing order by pgn
fef70 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a 2a 2a  o.  Pages are.**
fef80 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 44   connected by pD
fef90 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 20 20  irty pointers.  
fefa0 54 68 65 20 70 44 69 72 74 79 50 72 65 76 20 70  The pDirtyPrev p
fefb0 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a 20 63  ointers are.** c
fefc0 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 69 73  orrupted by this
fefd0 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e   sort..*/.#defin
fefe0 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f  e N_SORT_BUCKET_
feff0 41 4c 4c 4f 43 20 32 35 0a 23 64 65 66 69 6e 65  ALLOC 25.#define
ff000 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 20 20   N_SORT_BUCKET  
ff010 20 20 20 20 20 32 35 0a 23 69 66 64 65 66 20 53       25.#ifdef S
ff020 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 6e 74  QLITE_TEST.  int
ff030 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 6e   sqlite3_pager_n
ff040 5f 73 6f 72 74 5f 62 75 63 6b 65 74 20 3d 20 30  _sort_bucket = 0
ff050 3b 0a 20 20 23 75 6e 64 65 66 20 4e 5f 53 4f 52  ;.  #undef N_SOR
ff060 54 5f 42 55 43 4b 45 54 0a 20 20 23 64 65 66 69  T_BUCKET.  #defi
ff070 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  ne N_SORT_BUCKET
ff080 20 5c 0a 20 20 20 28 73 71 6c 69 74 65 33 5f 70   \.   (sqlite3_p
ff090 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b  ager_n_sort_buck
ff0a0 65 74 3f 73 71 6c 69 74 65 33 5f 70 61 67 65 72  et?sqlite3_pager
ff0b0 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 3a 4e  _n_sort_bucket:N
ff0c0 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41 4c 4c  _SORT_BUCKET_ALL
ff0d0 4f 43 29 0a 23 65 6e 64 69 66 0a 73 74 61 74 69  OC).#endif.stati
ff0e0 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65 53  c PgHdr *pcacheS
ff0f0 6f 72 74 44 69 72 74 79 4c 69 73 74 28 50 67 48  ortDirtyList(PgH
ff100 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50 67 48 64  dr *pIn){.  PgHd
ff110 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55 43 4b  r *a[N_SORT_BUCK
ff120 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a 70 3b 0a 20  ET_ALLOC], *p;. 
ff130 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74   int i;.  memset
ff140 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29  (a, 0, sizeof(a)
ff150 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20  );.  while( pIn 
ff160 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a  ){.    p = pIn;.
ff170 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69      pIn = p->pDi
ff180 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72  rty;.    p->pDir
ff190 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  ty = 0;.    for(
ff1a0 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55  i=0; i<N_SORT_BU
ff1b0 43 4b 45 54 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20  CKET-1; i++){.  
ff1c0 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d 30 20      if( a[i]==0 
ff1d0 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20  ){.        a[i] 
ff1e0 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = p;.        bre
ff1f0 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ak;.      }else{
ff200 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 63 61  .        p = pca
ff210 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73  cheMergeDirtyLis
ff220 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20  t(a[i], p);.    
ff230 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a 20 20      a[i] = 0;.  
ff240 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
ff250 69 66 28 20 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55  if( i==N_SORT_BU
ff260 43 4b 45 54 2d 31 20 29 7b 0a 20 20 20 20 20 20  CKET-1 ){.      
ff270 2f 2a 20 43 6f 76 65 72 61 67 65 3a 20 54 6f 20  /* Coverage: To 
ff280 67 65 74 20 68 65 72 65 2c 20 74 68 65 72 65 20  get here, there 
ff290 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28 4e 5f  need to be 2^(N_
ff2a0 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 0a 20 20  SORT_BUCKET) .  
ff2b0 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20      ** elements 
ff2c0 69 6e 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73  in the input lis
ff2d0 74 2e 20 54 68 69 73 20 69 73 20 70 6f 73 73 69  t. This is possi
ff2e0 62 6c 65 2c 20 62 75 74 20 69 6d 70 72 61 63 74  ble, but impract
ff2f0 69 63 61 6c 2e 0a 20 20 20 20 20 20 2a 2a 20 54  ical..      ** T
ff300 65 73 74 69 6e 67 20 74 68 69 73 20 6c 69 6e 65  esting this line
ff310 20 69 73 20 74 68 65 20 70 6f 69 6e 74 20 6f 66   is the point of
ff320 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
ff330 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
ff340 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62  3_pager_n_sort_b
ff350 75 63 6b 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  ucket..      */.
ff360 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70 63 61        a[i] = pca
ff370 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73  cheMergeDirtyLis
ff380 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20 20 20  t(a[i], p);.    
ff390 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b 30 5d  }.  }.  p = a[0]
ff3a0 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e  ;.  for(i=1; i<N
ff3b0 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20 69 2b  _SORT_BUCKET; i+
ff3c0 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63 61 63  +){.    p = pcac
ff3d0 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74  heMergeDirtyList
ff3e0 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d 0a 20  (p, a[i]);.  }. 
ff3f0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
ff400 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69 73  .** Return a lis
ff410 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70  t of all dirty p
ff420 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68  ages in the cach
ff430 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67  e, sorted by pag
ff440 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53 51 4c  e number..*/.SQL
ff450 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 64  ITE_PRIVATE PgHd
ff460 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65  r *sqlite3Pcache
ff470 44 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65  DirtyList(PCache
ff480 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48   *pCache){.  PgH
ff490 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70  dr *p;.  for(p=p
ff4a0 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20 70  Cache->pDirty; p
ff4b0 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e 65 78  ; p=p->pDirtyNex
ff4c0 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74  t){.    p->pDirt
ff4d0 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78  y = p->pDirtyNex
ff4e0 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
ff4f0 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c  pcacheSortDirtyL
ff500 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44 69 72  ist(pCache->pDir
ff510 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  ty);.}../* .** R
ff520 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20  eturn the total 
ff530 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65  number of refere
ff540 6e 63 65 64 20 70 61 67 65 73 20 68 65 6c 64 20  nced pages held 
ff550 62 79 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f  by the cache..*/
ff560 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ff570 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
ff580 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65  eRefCount(PCache
ff590 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74   *pCache){.  ret
ff5a0 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52 65 66  urn pCache->nRef
ff5b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
ff5c0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
ff5d0 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
ff5e0 65 20 70 61 67 65 20 73 75 70 70 6c 69 65 64 20  e page supplied 
ff5f0 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a  as an argument..
ff600 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ff610 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
ff620 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 28  chePageRefcount(
ff630 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 72 65 74  PgHdr *p){.  ret
ff640 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a  urn p->nRef;.}..
ff650 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /* .** Return th
ff660 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
ff670 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63  f pages in the c
ff680 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ache..*/.SQLITE_
ff690 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ff6a0 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75  te3PcachePagecou
ff6b0 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 68  nt(PCache *pCach
ff6c0 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 20  e){.  int nPage 
ff6d0 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 61 63 68  = 0;.  if( pCach
ff6e0 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20  e->pCache ){.   
ff6f0 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 74 65 33   nPage = sqlite3
ff700 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
ff710 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 28 70  che.xPagecount(p
ff720 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a  Cache->pCache);.
ff730 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 50 61    }.  return nPa
ff740 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  ge;.}..#ifdef SQ
ff750 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LITE_TEST./*.** 
ff760 47 65 74 20 74 68 65 20 73 75 67 67 65 73 74 65  Get the suggeste
ff770 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c  d cache-size val
ff780 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ue..*/.SQLITE_PR
ff790 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ff7a0 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73  3PcacheGetCaches
ff7b0 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63  ize(PCache *pCac
ff7c0 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43  he){.  return pC
ff7d0 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 7d 0a 23 65  ache->nMax;.}.#e
ff7e0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  ndif../*.** Set 
ff7f0 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61  the suggested ca
ff800 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a  che-size value..
ff810 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ff820 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
ff830 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65  acheSetCachesize
ff840 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c  (PCache *pCache,
ff850 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20   int mxPage){.  
ff860 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d 20 6d  pCache->nMax = m
ff870 78 50 61 67 65 3b 0a 20 20 69 66 28 20 70 43 61  xPage;.  if( pCa
ff880 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20  che->pCache ){. 
ff890 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
ff8a0 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 43  Config.pcache.xC
ff8b0 61 63 68 65 73 69 7a 65 28 70 43 61 63 68 65 2d  achesize(pCache-
ff8c0 3e 70 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29  >pCache, mxPage)
ff8d0 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66 20  ;.  }.}..#ifdef 
ff8e0 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
ff8f0 45 53 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61 6c 6c  ES./*.** For all
ff900 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72   dirty pages cur
ff910 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 63 61  rently in the ca
ff920 63 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20  che, invoke the 
ff930 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 63 61 6c  specified.** cal
ff940 6c 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 6f  lback. This is o
ff950 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20  nly used if the 
ff960 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
ff970 45 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 64  ES macro is.** d
ff980 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
ff990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
ff9a0 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72  qlite3PcacheIter
ff9b0 61 74 65 44 69 72 74 79 28 50 43 61 63 68 65 20  ateDirty(PCache 
ff9c0 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 28 2a  *pCache, void (*
ff9d0 78 49 74 65 72 29 28 50 67 48 64 72 20 2a 29 29  xIter)(PgHdr *))
ff9e0 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74  {.  PgHdr *pDirt
ff9f0 79 3b 0a 20 20 66 6f 72 28 70 44 69 72 74 79 3d  y;.  for(pDirty=
ffa00 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b 20  pCache->pDirty; 
ffa10 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 3d 70  pDirty; pDirty=p
ffa20 44 69 72 74 79 2d 3e 70 44 69 72 74 79 4e 65 78  Dirty->pDirtyNex
ffa30 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28 70 44  t){.    xIter(pD
ffa40 69 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  irty);.  }.}.#en
ffa50 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
ffa60 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63  **** End of pcac
ffa70 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  he.c ***********
ffa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffaa0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
ffab0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
ffac0 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a 2a 2a 2a  pcache1.c ******
ffad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffaf0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
ffb00 4e 6f 76 65 6d 62 65 72 20 30 35 0a 2a 2a 0a 2a  November 05.**.*
ffb10 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
ffb20 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
ffb30 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
ffb40 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
ffb50 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
ffb60 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
ffb70 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
ffb80 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
ffb90 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
ffba0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
ffbb0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
ffbc0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
ffbd0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
ffbe0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
ffbf0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
ffc00 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
ffc10 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
ffc20 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
ffc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ffc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
ffc70 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
ffc80 6d 65 6e 74 73 20 74 68 65 20 64 65 66 61 75 6c  ments the defaul
ffc90 74 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  t page cache imp
ffca0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 74 68 65  lementation (the
ffcb0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63  .** sqlite3_pcac
ffcc0 68 65 20 69 6e 74 65 72 66 61 63 65 29 2e 20 49  he interface). I
ffcd0 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20  t also contains 
ffce0 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70 6c  part of the impl
ffcf0 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66  ementation.** of
ffd00 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46   the SQLITE_CONF
ffd10 49 47 5f 50 41 47 45 43 41 43 48 45 20 61 6e 64  IG_PAGECACHE and
ffd20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
ffd30 5f 6d 65 6d 6f 72 79 28 29 20 66 65 61 74 75 72  _memory() featur
ffd40 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65  es..** If the de
ffd50 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
ffd60 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
ffd70 69 73 20 6f 76 65 72 72 69 64 65 6e 2c 20 74 68  is overriden, th
ffd80 65 6e 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a  en neither of.**
ffd90 20 74 68 65 73 65 20 74 77 6f 20 66 65 61 74 75   these two featu
ffda0 72 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  res are availabl
ffdb0 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  e..**.** @(#) $I
ffdc0 64 3a 20 70 63 61 63 68 65 31 2e 63 2c 76 20 31  d: pcache1.c,v 1
ffdd0 2e 31 31 20 32 30 30 39 2f 30 34 2f 31 34 20 31  .11 2009/04/14 1
ffde0 38 3a 34 34 3a 33 39 20 61 73 77 69 66 74 20 45  8:44:39 aswift E
ffdf0 78 70 20 24 0a 2a 2f 0a 0a 0a 74 79 70 65 64 65  xp $.*/...typede
ffe00 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 31  f struct PCache1
ffe10 20 50 43 61 63 68 65 31 3b 0a 74 79 70 65 64 65   PCache1;.typede
ffe20 66 20 73 74 72 75 63 74 20 50 67 48 64 72 31 20  f struct PgHdr1 
ffe30 50 67 48 64 72 31 3b 0a 74 79 70 65 64 65 66 20  PgHdr1;.typedef 
ffe40 73 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f  struct PgFreeslo
ffe50 74 20 50 67 46 72 65 65 73 6c 6f 74 3b 0a 0a 2f  t PgFreeslot;../
ffe60 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 73 74  * Pointers to st
ffe70 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 69 73  ructures of this
ffe80 20 74 79 70 65 20 61 72 65 20 63 61 73 74 20 61   type are cast a
ffe90 6e 64 20 72 65 74 75 72 6e 65 64 20 61 73 20 0a  nd returned as .
ffea0 2a 2a 20 6f 70 61 71 75 65 20 73 71 6c 69 74 65  ** opaque sqlite
ffeb0 33 5f 70 63 61 63 68 65 2a 20 68 61 6e 64 6c 65  3_pcache* handle
ffec0 73 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43 61 63  s.*/.struct PCac
ffed0 68 65 31 20 7b 0a 20 20 2f 2a 20 43 61 63 68 65  he1 {.  /* Cache
ffee0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 70   configuration p
ffef0 61 72 61 6d 65 74 65 72 73 2e 20 50 61 67 65 20  arameters. Page 
fff00 73 69 7a 65 20 28 73 7a 50 61 67 65 29 20 61 6e  size (szPage) an
fff10 64 20 74 68 65 20 70 75 72 67 65 61 62 6c 65 0a  d the purgeable.
fff20 20 20 2a 2a 20 66 6c 61 67 20 28 62 50 75 72 67    ** flag (bPurg
fff30 65 61 62 6c 65 29 20 61 72 65 20 73 65 74 20 77  eable) are set w
fff40 68 65 6e 20 74 68 65 20 63 61 63 68 65 20 69 73  hen the cache is
fff50 20 63 72 65 61 74 65 64 2e 20 6e 4d 61 78 20 6d   created. nMax m
fff60 61 79 20 62 65 20 0a 20 20 2a 2a 20 6d 6f 64 69  ay be .  ** modi
fff70 66 69 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65  fied at any time
fff80 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 74 68   by a call to th
fff90 65 20 70 63 61 63 68 65 31 43 61 63 68 65 53 69  e pcache1CacheSi
fffa0 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0a 20 20 2a  ze() method..  *
fffb0 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74  * The global mut
fffc0 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20  ex must be held 
fffd0 77 68 65 6e 20 61 63 63 65 73 73 69 6e 67 20 6e  when accessing n
fffe0 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  Max..  */.  int 
ffff0 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  szPage;         
10000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10001 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63  /* Size of alloc
10002 61 74 65 64 20 70 61 67 65 73 20 69 6e 20 62 79  ated pages in by
10003 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75  tes */.  int bPu
10004 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20  rgeable;        
10005 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10006 54 72 75 65 20 69 66 20 63 61 63 68 65 20 69 73  True if cache is
10007 20 70 75 72 67 65 61 62 6c 65 20 2a 2f 0a 20 20   purgeable */.  
10008 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 69  unsigned int nMi
10009 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
1000a 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6e      /* Minimum n
1000b 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 72  umber of pages r
1000c 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 75 6e 73  eserved */.  uns
1000d 69 67 6e 65 64 20 69 6e 74 20 6e 4d 61 78 3b 20  igned int nMax; 
1000e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1000f 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 20 22   /* Configured "
10010 63 61 63 68 65 5f 73 69 7a 65 22 20 76 61 6c 75  cache_size" valu
10011 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 61 73 68 20  e */..  /* Hash 
10012 74 61 62 6c 65 20 6f 66 20 61 6c 6c 20 70 61 67  table of all pag
10013 65 73 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  es. The followin
10014 67 20 76 61 72 69 61 62 6c 65 73 20 6d 61 79 20  g variables may 
10015 6f 6e 6c 79 20 62 65 20 61 63 63 65 73 73 65 64  only be accessed
10016 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 61  .  ** when the a
10017 63 63 65 73 73 6f 72 20 69 73 20 68 6f 6c 64 69  ccessor is holdi
10018 6e 67 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75  ng the global mu
10019 74 65 78 20 28 73 65 65 20 70 63 61 63 68 65 31  tex (see pcache1
1001a 45 6e 74 65 72 4d 75 74 65 78 28 29 20 0a 20 20  EnterMutex() .  
1001b 2a 2a 20 61 6e 64 20 70 63 61 63 68 65 31 4c 65  ** and pcache1Le
1001c 61 76 65 4d 75 74 65 78 28 29 29 2e 0a 20 20 2a  aveMutex())..  *
1001d 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
1001e 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20 20 20   nRecyclable;   
1001f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
10020 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
10021 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a 20 20  e LRU list */.  
10022 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 61  unsigned int nPa
10023 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
10024 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
10025 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
10026 61 70 48 61 73 68 20 2a 2f 0a 20 20 75 6e 73 69  apHash */.  unsi
10027 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68 3b 20  gned int nHash; 
10028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10029 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f  /* Number of slo
1002a 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d 20 2a  ts in apHash[] *
1002b 2f 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 70 48  /.  PgHdr1 **apH
1002c 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  ash;            
1002d 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
1002e 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74 20 6c  table for fast l
1002f 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20 2a 2f 0a  ookup by key */.
10030 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10031 69 4d 61 78 4b 65 79 3b 20 20 20 20 20 20 20 20  iMaxKey;        
10032 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73         /* Larges
10033 74 20 6b 65 79 20 73 65 65 6e 20 73 69 6e 63 65  t key seen since
10034 20 78 54 72 75 6e 63 61 74 65 28 29 20 2a 2f 0a   xTruncate() */.
10035 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 63  };../*.** Each c
10036 61 63 68 65 20 65 6e 74 72 79 20 69 73 20 72 65  ache entry is re
10037 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e 20  presented by an 
10038 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
10039 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74  following .** st
1003a 72 75 63 74 75 72 65 2e 20 41 20 62 75 66 66 65  ructure. A buffe
1003b 72 20 6f 66 20 50 67 48 64 72 31 2e 70 43 61 63  r of PgHdr1.pCac
1003c 68 65 2d 3e 73 7a 50 61 67 65 20 62 79 74 65 73  he->szPage bytes
1003d 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a   is allocated .*
1003e 2a 20 64 69 72 65 63 74 6c 79 20 61 66 74 65 72  * directly after
1003f 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 69   the structure i
10040 6e 20 6d 65 6d 6f 72 79 20 28 73 65 65 20 74 68  n memory (see th
10041 65 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45  e PGHDR1_TO_PAGE
10042 28 29 20 0a 2a 2a 20 6d 61 63 72 6f 20 62 65 6c  () .** macro bel
10043 6f 77 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50  ow)..*/.struct P
10044 67 48 64 72 31 20 7b 0a 20 20 75 6e 73 69 67 6e  gHdr1 {.  unsign
10045 65 64 20 69 6e 74 20 69 4b 65 79 3b 20 20 20 20  ed int iKey;    
10046 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20           /* Key 
10047 76 61 6c 75 65 20 28 70 61 67 65 20 6e 75 6d 62  value (page numb
10048 65 72 29 20 2a 2f 0a 20 20 50 67 48 64 72 31 20  er) */.  PgHdr1 
10049 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
1004a 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
1004b 69 6e 20 68 61 73 68 20 74 61 62 6c 65 20 63 68  in hash table ch
1004c 61 69 6e 20 2a 2f 0a 20 20 50 43 61 63 68 65 31  ain */.  PCache1
1004d 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 20   *pCache;       
1004e 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65          /* Cache
1004f 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20   that currently 
10050 6f 77 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a  owns this page *
10051 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75  /.  PgHdr1 *pLru
10052 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
10053 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 4c 52     /* Next in LR
10054 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e  U list of unpinn
10055 65 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67  ed pages */.  Pg
10056 48 64 72 31 20 2a 70 4c 72 75 50 72 65 76 3b 20  Hdr1 *pLruPrev; 
10057 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10058 50 72 65 76 69 6f 75 73 20 69 6e 20 4c 52 55 20  Previous in LRU 
10059 6c 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64  list of unpinned
1005a 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pages */.};../*
1005b 0a 2a 2a 20 46 72 65 65 20 73 6c 6f 74 73 20 69  .** Free slots i
1005c 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 6f 72 20  n the allocator 
1005d 75 73 65 64 20 74 6f 20 64 69 76 69 64 65 20 75  used to divide u
1005e 70 20 74 68 65 20 62 75 66 66 65 72 20 70 72 6f  p the buffer pro
1005f 76 69 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 74  vided using.** t
10060 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  he SQLITE_CONFIG
10061 5f 50 41 47 45 43 41 43 48 45 20 6d 65 63 68 61  _PAGECACHE mecha
10062 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  nism..*/.struct 
10063 50 67 46 72 65 65 73 6c 6f 74 20 7b 0a 20 20 50  PgFreeslot {.  P
10064 67 46 72 65 65 73 6c 6f 74 20 2a 70 4e 65 78 74  gFreeslot *pNext
10065 3b 20 20 2f 2a 20 4e 65 78 74 20 66 72 65 65 20  ;  /* Next free 
10066 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  slot */.};../*.*
10067 2a 20 47 6c 6f 62 61 6c 20 64 61 74 61 20 75 73  * Global data us
10068 65 64 20 62 79 20 74 68 69 73 20 63 61 63 68 65  ed by this cache
10069 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49  ..*/.static SQLI
1006a 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 50 43  TE_WSD struct PC
1006b 61 63 68 65 47 6c 6f 62 61 6c 20 7b 0a 20 20 73  acheGlobal {.  s
1006c 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
1006d 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  tex;            
1006e 20 20 20 2f 2a 20 73 74 61 74 69 63 20 6d 75 74     /* static mut
1006f 65 78 20 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  ex MUTEX_STATIC_
10070 4c 52 55 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 4d  LRU */..  int nM
10071 61 78 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  axPage;         
10072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10073 20 53 75 6d 20 6f 66 20 6e 4d 61 78 50 61 67 65   Sum of nMaxPage
10074 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63   for purgeable c
10075 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  aches */.  int n
10076 4d 69 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20  MinPage;        
10077 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10078 2a 20 53 75 6d 20 6f 66 20 6e 4d 69 6e 50 61 67  * Sum of nMinPag
10079 65 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20  e for purgeable 
1007a 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20  caches */.  int 
1007b 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 20 20 20  nCurrentPage;   
1007c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1007d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 75 72  /* Number of pur
1007e 67 65 61 62 6c 65 20 70 61 67 65 73 20 61 6c 6c  geable pages all
1007f 6f 63 61 74 65 64 20 2a 2f 0a 20 20 50 67 48 64  ocated */.  PgHd
10080 72 31 20 2a 70 4c 72 75 48 65 61 64 2c 20 2a 70  r1 *pLruHead, *p
10081 4c 72 75 54 61 69 6c 3b 20 20 20 20 20 20 20 20  LruTail;        
10082 2f 2a 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75  /* LRU list of u
10083 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f  npinned pages */
10084 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 73  ..  /* Variables
10085 20 72 65 6c 61 74 65 64 20 74 6f 20 53 51 4c 49   related to SQLI
10086 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
10087 43 48 45 20 73 65 74 74 69 6e 67 73 2e 20 2a 2f  CHE settings. */
10088 0a 20 20 69 6e 74 20 73 7a 53 6c 6f 74 3b 20 20  .  int szSlot;  
10089 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1008a 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1008b 66 20 65 61 63 68 20 66 72 65 65 20 73 6c 6f 74  f each free slot
1008c 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 61   */.  void *pSta
1008d 72 74 2c 20 2a 70 45 6e 64 3b 20 20 20 20 20 20  rt, *pEnd;      
1008e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6f 75            /* Bou
1008f 6e 64 73 20 6f 66 20 70 61 67 65 63 61 63 68 65  nds of pagecache
10090 20 6d 61 6c 6c 6f 63 20 72 61 6e 67 65 20 2a 2f   malloc range */
10091 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70  .  PgFreeslot *p
10092 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  Free;           
10093 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 70         /* Free p
10094 61 67 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 7d 20  age blocks */.} 
10095 70 63 61 63 68 65 31 5f 67 3b 0a 0a 2f 2a 0a 2a  pcache1_g;../*.*
10096 2a 20 41 6c 6c 20 63 6f 64 65 20 69 6e 20 74 68  * All code in th
10097 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61  is file should a
10098 63 63 65 73 73 20 74 68 65 20 67 6c 6f 62 61 6c  ccess the global
10099 20 73 74 72 75 63 74 75 72 65 20 61 62 6f 76 65   structure above
1009a 20 76 69 61 20 74 68 65 0a 2a 2a 20 61 6c 69 61   via the.** alia
1009b 73 20 22 70 63 61 63 68 65 31 22 2e 20 54 68 69  s "pcache1". Thi
1009c 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 74  s ensures that t
1009d 68 65 20 57 53 44 20 65 6d 75 6c 61 74 69 6f 6e  he WSD emulation
1009e 20 69 73 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a   is used when.**
1009f 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 73   compiling for s
100a0 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e  ystems that do n
100a1 6f 74 20 73 75 70 70 6f 72 74 20 72 65 61 6c 20  ot support real 
100a2 57 53 44 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  WSD..*/.#define 
100a3 70 63 61 63 68 65 31 20 28 47 4c 4f 42 41 4c 28  pcache1 (GLOBAL(
100a4 73 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f  struct PCacheGlo
100a5 62 61 6c 2c 20 70 63 61 63 68 65 31 5f 67 29 29  bal, pcache1_g))
100a6 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 50  ../*.** When a P
100a7 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20  gHdr1 structure 
100a8 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68  is allocated, th
100a9 65 20 61 73 73 6f 63 69 61 74 65 64 20 50 43 61  e associated PCa
100aa 63 68 65 31 2e 73 7a 50 61 67 65 0a 2a 2a 20 62  che1.szPage.** b
100ab 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
100ac 20 6c 6f 63 61 74 65 64 20 64 69 72 65 63 74 6c   located directl
100ad 79 20 61 66 74 65 72 20 69 74 20 69 6e 20 6d 65  y after it in me
100ae 6d 6f 72 79 20 28 69 2e 65 2e 20 74 68 65 20 74  mory (i.e. the t
100af 6f 74 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  otal.** size of 
100b0 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
100b1 73 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29  s sizeof(PgHdr1)
100b2 2b 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 20  +PCache1.szPage 
100b3 62 79 74 65 29 2e 20 54 68 65 0a 2a 2a 20 50 47  byte). The.** PG
100b4 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 29 20 6d  HDR1_TO_PAGE() m
100b5 61 63 72 6f 20 74 61 6b 65 73 20 61 20 70 6f 69  acro takes a poi
100b6 6e 74 65 72 20 74 6f 20 61 20 50 67 48 64 72 31  nter to a PgHdr1
100b7 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 2a 2a   structure as.**
100b8 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 61 6e 64   an argument and
100b9 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
100ba 65 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69  er to the associ
100bb 61 74 65 64 20 62 6c 6f 63 6b 20 6f 66 20 73 7a  ated block of sz
100bc 50 61 67 65 0a 2a 2a 20 62 79 74 65 73 2e 20 54  Page.** bytes. T
100bd 68 65 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52  he PAGE_TO_PGHDR
100be 31 28 29 20 6d 61 63 72 6f 20 64 6f 65 73 20 74  1() macro does t
100bf 68 65 20 6f 70 70 6f 73 69 74 65 3a 20 69 74 73  he opposite: its
100c0 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20   argument is.** 
100c1 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62  a pointer to a b
100c2 6c 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 20 62  lock of szPage b
100c3 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 6e 64  ytes of data and
100c4 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
100c5 65 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  e is.** a pointe
100c6 72 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61  r to the associa
100c7 74 65 64 20 50 67 48 64 72 31 20 73 74 72 75 63  ted PgHdr1 struc
100c8 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 73  ture..**.**   as
100c9 73 65 72 74 28 20 50 47 48 44 52 31 5f 54 4f 5f  sert( PGHDR1_TO_
100ca 50 41 47 45 28 50 41 47 45 5f 54 4f 5f 50 47 48  PAGE(PAGE_TO_PGH
100cb 44 52 31 28 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f  DR1(X))==X );.*/
100cc 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 5f  .#define PGHDR1_
100cd 54 4f 5f 50 41 47 45 28 70 29 20 28 76 6f 69 64  TO_PAGE(p) (void
100ce 20 2a 29 28 26 28 28 75 6e 73 69 67 6e 65 64 20   *)(&((unsigned 
100cf 63 68 61 72 20 2a 29 70 29 5b 73 69 7a 65 6f 66  char *)p)[sizeof
100d0 28 50 67 48 64 72 31 29 5d 29 0a 23 64 65 66 69  (PgHdr1)]).#defi
100d1 6e 65 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52  ne PAGE_TO_PGHDR
100d2 31 28 70 29 20 28 50 67 48 64 72 31 20 2a 29 28  1(p) (PgHdr1 *)(
100d3 26 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  &((unsigned char
100d4 20 2a 29 70 29 5b 2d 31 2a 28 69 6e 74 29 73 69   *)p)[-1*(int)si
100d5 7a 65 6f 66 28 50 67 48 64 72 31 29 5d 29 0a 0a  zeof(PgHdr1)])..
100d6 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20  /*.** Macros to 
100d7 65 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20  enter and leave 
100d8 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6d  the global LRU m
100d9 75 74 65 78 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  utex..*/.#define
100da 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
100db 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74  ex() sqlite3_mut
100dc 65 78 5f 65 6e 74 65 72 28 70 63 61 63 68 65 31  ex_enter(pcache1
100dd 2e 6d 75 74 65 78 29 0a 23 64 65 66 69 6e 65 20  .mutex).#define 
100de 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65  pcache1LeaveMute
100df 78 28 29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  x() sqlite3_mute
100e0 78 5f 6c 65 61 76 65 28 70 63 61 63 68 65 31 2e  x_leave(pcache1.
100e1 6d 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  mutex)../*******
100e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
100e6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
100e7 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63 61 74 69  ** Page Allocati
100e8 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  on/SQLITE_CONFIG
100e9 5f 50 43 41 43 48 45 20 52 65 6c 61 74 65 64 20  _PCACHE Related 
100ea 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a  Functions ******
100eb 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
100ec 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
100ed 73 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20  s called during 
100ee 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69  initialization i
100ef 66 20 61 20 73 74 61 74 69 63 20 62 75 66 66 65  f a static buffe
100f0 72 20 69 73 20 0a 2a 2a 20 73 75 70 70 6c 69 65  r is .** supplie
100f1 64 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65  d to use for the
100f2 20 70 61 67 65 2d 63 61 63 68 65 20 62 79 20 70   page-cache by p
100f3 61 73 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54  assing the SQLIT
100f4 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
100f5 48 45 0a 2a 2a 20 76 65 72 62 20 74 6f 20 73 71  HE.** verb to sq
100f6 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 2e 20  lite3_config(). 
100f7 50 61 72 61 6d 65 74 65 72 20 70 42 75 66 20 70  Parameter pBuf p
100f8 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f  oints to an allo
100f9 63 61 74 69 6f 6e 20 6c 61 72 67 65 0a 2a 2a 20  cation large.** 
100fa 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e 74 61 69  enough to contai
100fb 6e 20 27 6e 27 20 62 75 66 66 65 72 73 20 6f 66  n 'n' buffers of
100fc 20 27 73 7a 27 20 62 79 74 65 73 20 65 61 63 68   'sz' bytes each
100fd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
100fe 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
100ff 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74 75  PCacheBufferSetu
10100 70 28 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e  p(void *pBuf, in
10101 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  t sz, int n){.  
10102 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20  PgFreeslot *p;. 
10103 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38   sz = ROUNDDOWN8
10104 28 73 7a 29 3b 0a 20 20 70 63 61 63 68 65 31 2e  (sz);.  pcache1.
10105 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20 70  szSlot = sz;.  p
10106 63 61 63 68 65 31 2e 70 53 74 61 72 74 20 3d 20  cache1.pStart = 
10107 70 42 75 66 3b 0a 20 20 70 63 61 63 68 65 31 2e  pBuf;.  pcache1.
10108 70 46 72 65 65 20 3d 20 30 3b 0a 20 20 77 68 69  pFree = 0;.  whi
10109 6c 65 28 20 6e 2d 2d 20 29 7b 0a 20 20 20 20 70  le( n-- ){.    p
1010a 20 3d 20 28 50 67 46 72 65 65 73 6c 6f 74 2a 29   = (PgFreeslot*)
1010b 70 42 75 66 3b 0a 20 20 20 20 70 2d 3e 70 4e 65  pBuf;.    p->pNe
1010c 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72  xt = pcache1.pFr
1010d 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e  ee;.    pcache1.
1010e 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70  pFree = p;.    p
1010f 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 28  Buf = (void*)&((
10110 63 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d 3b  char*)pBuf)[sz];
10111 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e 70  .  }.  pcache1.p
10112 45 6e 64 20 3d 20 70 42 75 66 3b 0a 7d 0a 0a 2f  End = pBuf;.}../
10113 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63  *.** Malloc func
10114 74 69 6f 6e 20 75 73 65 64 20 77 69 74 68 69 6e  tion used within
10115 20 74 68 69 73 20 66 69 6c 65 20 74 6f 20 61 6c   this file to al
10116 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 72 6f  locate space fro
10117 6d 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a 20  m the buffer.** 
10118 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67  configured using
10119 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
1011a 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
1011b 47 45 43 41 43 48 45 29 20 6f 70 74 69 6f 6e 2e  GECACHE) option.
1011c 20 49 66 20 6e 6f 20 0a 2a 2a 20 73 75 63 68 20   If no .** such 
1011d 62 75 66 66 65 72 20 65 78 69 73 74 73 20 6f 72  buffer exists or
1011e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 70 61   there is no spa
1011f 63 65 20 6c 65 66 74 20 69 6e 20 69 74 2c 20 74  ce left in it, t
10120 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 61 6c  his function fal
10121 6c 73 20 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 73  ls .** back to s
10122 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a  qlite3Malloc()..
10123 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
10124 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 69 6e 74  pcache1Alloc(int
10125 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f 69 64 20   nByte){.  void 
10126 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  *p;.  assert( sq
10127 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
10128 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20  (pcache1.mutex) 
10129 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d  );.  if( nByte<=
1012a 70 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 26  pcache1.szSlot &
1012b 26 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20  & pcache1.pFree 
1012c 29 7b 0a 20 20 20 20 70 20 3d 20 28 50 67 48 64  ){.    p = (PgHd
1012d 72 31 20 2a 29 70 63 61 63 68 65 31 2e 70 46 72  r1 *)pcache1.pFr
1012e 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e  ee;.    pcache1.
1012f 70 46 72 65 65 20 3d 20 70 63 61 63 68 65 31 2e  pFree = pcache1.
10130 70 46 72 65 65 2d 3e 70 4e 65 78 74 3b 0a 20 20  pFree->pNext;.  
10131 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
10132 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
10133 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 2c  _PAGECACHE_SIZE,
10134 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c   nByte);.    sql
10135 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51  ite3StatusAdd(SQ
10136 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
10137 43 41 43 48 45 5f 55 53 45 44 2c 20 31 29 3b 0a  CACHE_USED, 1);.
10138 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 2f 2a    }else{..    /*
10139 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
1013a 62 75 66 66 65 72 20 75 73 69 6e 67 20 73 71 6c  buffer using sql
1013b 69 74 65 33 4d 61 6c 6c 6f 63 2e 20 42 65 66 6f  ite3Malloc. Befo
1013c 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20 65 78 69  re doing so, exi
1013d 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 67 6c 6f  t the.    ** glo
1013e 62 61 6c 20 70 63 61 63 68 65 20 6d 75 74 65 78  bal pcache mutex
1013f 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20   and unlock the 
10140 70 61 67 65 72 2d 63 61 63 68 65 20 6f 62 6a 65  pager-cache obje
10141 63 74 20 70 43 61 63 68 65 2e 20 54 68 69 73 20  ct pCache. This 
10142 69 73 20 0a 20 20 20 20 2a 2a 20 73 6f 20 74 68  is .    ** so th
10143 61 74 20 69 66 20 74 68 65 20 61 74 74 65 6d 70  at if the attemp
10144 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  t to allocate a 
10145 6e 65 77 20 62 75 66 66 65 72 20 63 61 75 73 65  new buffer cause
10146 73 20 74 68 65 20 74 68 65 20 0a 20 20 20 20 2a  s the the .    *
10147 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 73 6f 66  * configured sof
10148 74 2d 68 65 61 70 2d 6c 69 6d 69 74 20 74 6f 20  t-heap-limit to 
10149 62 65 20 62 72 65 61 63 68 65 64 2c 20 69 74 20  be breached, it 
1014a 77 69 6c 6c 20 62 65 20 70 6f 73 73 69 62 6c 65  will be possible
1014b 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 63 6c 61   to.    ** recla
1014c 69 6d 20 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74  im memory from t
1014d 68 69 73 20 70 61 67 65 72 2d 63 61 63 68 65 2e  his pager-cache.
1014e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 61 63  .    */.    pcac
1014f 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  he1LeaveMutex();
10150 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
10151 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20  Malloc(nByte);. 
10152 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d     pcache1EnterM
10153 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 28 20  utex();.    if( 
10154 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  p ){.      int s
10155 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  z = sqlite3Mallo
10156 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20  cSize(p);.      
10157 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
10158 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50  (SQLITE_STATUS_P
10159 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f  AGECACHE_OVERFLO
1015a 57 2c 20 73 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  W, sz);.    }.  
1015b 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
1015c 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 61  ./*.** Free an a
1015d 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20  llocated buffer 
1015e 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 70 63  obtained from pc
1015f 61 63 68 65 31 41 6c 6c 6f 63 28 29 2e 0a 2a 2f  ache1Alloc()..*/
10160 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10161 63 68 65 31 46 72 65 65 28 76 6f 69 64 20 2a 70  che1Free(void *p
10162 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
10163 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10164 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
10165 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
10166 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 3e 3d  eturn;.  if( p>=
10167 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 20 26  pcache1.pStart &
10168 26 20 70 3c 70 63 61 63 68 65 31 2e 70 45 6e 64  & p<pcache1.pEnd
10169 20 29 7b 0a 20 20 20 20 50 67 46 72 65 65 73 6c   ){.    PgFreesl
1016a 6f 74 20 2a 70 53 6c 6f 74 3b 0a 20 20 20 20 73  ot *pSlot;.    s
1016b 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
1016c 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
1016d 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20 2d 31  GECACHE_USED, -1
1016e 29 3b 0a 20 20 20 20 70 53 6c 6f 74 20 3d 20 28  );.    pSlot = (
1016f 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 3b 0a 20  PgFreeslot*)p;. 
10170 20 20 20 70 53 6c 6f 74 2d 3e 70 4e 65 78 74 20     pSlot->pNext 
10171 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b  = pcache1.pFree;
10172 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72  .    pcache1.pFr
10173 65 65 20 3d 20 70 53 6c 6f 74 3b 0a 20 20 7d 65  ee = pSlot;.  }e
10174 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 53 69  lse{.    int iSi
10175 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ze = sqlite3Mall
10176 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 73  ocSize(p);.    s
10177 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
10178 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
10179 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57  GECACHE_OVERFLOW
1017a 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 73  , -iSize);.    s
1017b 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
1017c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c    }.}../*.** All
1017d 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  ocate a new page
1017e 20 6f 62 6a 65 63 74 20 69 6e 69 74 69 61 6c 6c   object initiall
1017f 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
10180 68 20 63 61 63 68 65 20 70 43 61 63 68 65 2e 0a  h cache pCache..
10181 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 31  */.static PgHdr1
10182 20 2a 70 63 61 63 68 65 31 41 6c 6c 6f 63 50 61   *pcache1AllocPa
10183 67 65 28 50 43 61 63 68 65 31 20 2a 70 43 61 63  ge(PCache1 *pCac
10184 68 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65  he){.  int nByte
10185 20 3d 20 73 69 7a 65 6f 66 28 50 67 48 64 72 31   = sizeof(PgHdr1
10186 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 50 61  ) + pCache->szPa
10187 67 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 20  ge;.  PgHdr1 *p 
10188 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63  = (PgHdr1 *)pcac
10189 68 65 31 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b  he1Alloc(nByte);
1018a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
1018b 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72  if( pCache->bPur
1018c 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  geable ){.      
1018d 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74  pcache1.nCurrent
1018e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  Page++;.    }.  
1018f 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
10190 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61  ./*.** Free a pa
10191 67 65 20 6f 62 6a 65 63 74 20 61 6c 6c 6f 63 61  ge object alloca
10192 74 65 64 20 62 79 20 70 63 61 63 68 65 31 41 6c  ted by pcache1Al
10193 6c 6f 63 50 61 67 65 28 29 2e 0a 2a 2f 0a 73 74  locPage()..*/.st
10194 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10195 31 46 72 65 65 50 61 67 65 28 50 67 48 64 72 31  1FreePage(PgHdr1
10196 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
10197 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63  .    if( p->pCac
10198 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29  he->bPurgeable )
10199 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  {.      pcache1.
1019a 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a  nCurrentPage--;.
1019b 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
1019c 31 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  1Free(p);.  }.}.
1019d 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75  ./*.** Malloc fu
1019e 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53  nction used by S
1019f 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20  QLite to obtain 
101a0 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62  space from the b
101a1 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64  uffer configured
101a2 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65  .** using sqlite
101a3 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
101a4 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
101a5 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  ) option. If no 
101a6 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65  such buffer.** e
101a7 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63  xists, this func
101a8 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20  tion falls back 
101a9 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  to sqlite3Malloc
101aa 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
101ab 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
101ac 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e  te3PageMalloc(in
101ad 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70  t sz){.  void *p
101ae 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
101af 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70  Mutex();.  p = p
101b0 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b  cache1Alloc(sz);
101b1 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
101b2 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e  utex();.  return
101b3 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   p;.}../*.** Fre
101b4 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  e an allocated b
101b5 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66  uffer obtained f
101b6 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d  rom sqlite3PageM
101b7 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
101b8 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
101b9 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
101ba 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63  void *p){.  pcac
101bb 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
101bc 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70  .  pcache1Free(p
101bd 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  );.  pcache1Leav
101be 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a  eMutex();.}../**
101bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
101c4 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20  ******* General 
101c5 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46  Implementation F
101c6 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a  unctions *******
101c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
101c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
101c9 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
101ca 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
101cb 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74  esize the hash t
101cc 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65  able used by the
101cd 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a   cache passed.**
101ce 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
101cf 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
101d0 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d  e global mutex m
101d1 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e  ust be held when
101d2 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
101d3 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
101d4 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52  tic int pcache1R
101d5 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65  esizeHash(PCache
101d6 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20  1 *p){.  PgHdr1 
101d7 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67  **apNew;.  unsig
101d8 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20  ned int nNew;.  
101d9 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a  unsigned int i;.
101da 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
101db 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
101dc 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
101dd 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61  .  nNew = p->nHa
101de 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77  sh*2;.  if( nNew
101df 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77  <256 ){.    nNew
101e0 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70   = 256;.  }..  p
101e1 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
101e2 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61  ();.  if( p->nHa
101e3 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67  sh ){ sqlite3Beg
101e4 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
101e5 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50  ; }.  apNew = (P
101e6 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33  gHdr1 **)sqlite3
101e7 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50  _malloc(sizeof(P
101e8 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a  gHdr1 *)*nNew);.
101e9 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29    if( p->nHash )
101ea 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  { sqlite3EndBeni
101eb 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20  gnMalloc(); }.  
101ec 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
101ed 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77  x();.  if( apNew
101ee 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61   ){.    memset(a
101ef 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pNew, 0, sizeof(
101f0 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b  PgHdr1 *)*nNew);
101f1 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
101f2 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a  p->nHash; i++){.
101f3 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 50        PgHdr1 *pP
101f4 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72  age;.      PgHdr
101f5 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70  1 *pNext = p->ap
101f6 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77  Hash[i];.      w
101f7 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70  hile( (pPage = p
101f8 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20  Next)!=0 ){.    
101f9 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
101fa 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79   h = pPage->iKey
101fb 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20   % nNew;.       
101fc 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e   pNext = pPage->
101fd 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70  pNext;.        p
101fe 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70  Page->pNext = ap
101ff 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20  New[h];.        
10200 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65  apNew[h] = pPage
10201 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
10202 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
10203 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20  (p->apHash);.   
10204 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e   p->apHash = apN
10205 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68  ew;.    p->nHash
10206 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20   = nNew;.  }..  
10207 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73  return (p->apHas
10208 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  h ? SQLITE_OK : 
10209 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d  SQLITE_NOMEM);.}
1020a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
1020b 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e  ction is used in
1020c 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f  ternally to remo
1020d 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67  ve the page pPag
1020e 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67  e from the .** g
1020f 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20  lobal LRU list, 
10210 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74  if is part of it
10211 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f  . If pPage is no
10212 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c  t part of the gl
10213 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74  obal.** LRU list
10214 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
10215 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
10216 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10217 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10218 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10219 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1021a 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
1021b 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65  d pcache1PinPage
1021c 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b  (PgHdr1 *pPage){
1021d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1021e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
1021f 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10220 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28    if( pPage && (
10221 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
10222 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65  || pPage==pcache
10223 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20  1.pLruTail) ){. 
10224 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c     if( pPage->pL
10225 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20  ruPrev ){.      
10226 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d  pPage->pLruPrev-
10227 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67  >pLruNext = pPag
10228 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20  e->pLruNext;.   
10229 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65   }.    if( pPage
1022a 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20  ->pLruNext ){.  
1022b 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e      pPage->pLruN
1022c 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20  ext->pLruPrev = 
1022d 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b  pPage->pLruPrev;
1022e 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1022f 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d  cache1.pLruHead=
10230 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20  =pPage ){.      
10231 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
10232 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65   = pPage->pLruNe
10233 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  xt;.    }.    if
10234 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  ( pcache1.pLruTa
10235 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20  il==pPage ){.   
10236 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54     pcache1.pLruT
10237 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72  ail = pPage->pLr
10238 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20  uPrev;.    }.   
10239 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
1023a 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d   = 0;.    pPage-
1023b 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20  >pLruPrev = 0;. 
1023c 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65     pPage->pCache
1023d 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b  ->nRecyclable--;
1023e 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  .  }.}.../*.** R
1023f 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73  emove the page s
10240 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72  upplied as an ar
10241 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  gument from the 
10242 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28  hash table .** (
10243 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73  PCache1.apHash s
10244 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69  tructure) that i
10245 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73  t is currently s
10246 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20  tored in..**.** 
10247 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  The global mutex
10248 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
10249 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
1024a 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
1024b 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
1024c 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  e1RemoveFromHash
1024d 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b  (PgHdr1 *pPage){
1024e 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
1024f 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  h;.  PCache1 *pC
10250 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43  ache = pPage->pC
10251 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a  ache;.  PgHdr1 *
10252 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67  *pp;..  h = pPag
10253 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65  e->iKey % pCache
10254 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70  ->nHash;.  for(p
10255 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73  p=&pCache->apHas
10256 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61  h[h]; (*pp)!=pPa
10257 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70  ge; pp=&(*pp)->p
10258 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28  Next);.  *pp = (
10259 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20  *pp)->pNext;..  
1025a 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b  pCache->nPage--;
1025b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
1025c 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  re are currently
1025d 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68   more than pcach
1025e 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73  e.nMaxPage pages
1025f 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a   allocated, try.
10260 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61  ** to recycle pa
10261 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68  ges to reduce th
10262 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74  e number allocat
10263 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61  ed to pcache.nMa
10264 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  xPage..*/.static
10265 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66   void pcache1Enf
10266 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64  orceMaxPage(void
10267 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
10268 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10269 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
1026a 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68  ;.  while( pcach
1026b 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e  e1.nCurrentPage>
1026c 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65  pcache1.nMaxPage
1026d 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75   && pcache1.pLru
1026e 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64  Tail ){.    PgHd
1026f 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e  r1 *p = pcache1.
10270 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63  pLruTail;.    pc
10271 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b  ache1PinPage(p);
10272 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f  .    pcache1Remo
10273 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20  veFromHash(p);. 
10274 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61     pcache1FreePa
10275 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ge(p);.  }.}../*
10276 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20  .** Discard all 
10277 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65  pages from cache
10278 20 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70   pCache with a p
10279 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20  age number (key 
1027a 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74  value) .** great
1027b 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
1027c 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20   to iLimit. Any 
1027d 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61  pinned pages tha
1027e 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20  t meet this .** 
1027f 63 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70  criteria are unp
10280 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65  inned before the
10281 79 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e  y are discarded.
10282 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10283 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10284 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10285 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10286 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
10287 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74  d pcache1Truncat
10288 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68  eUnsafe(.  PCach
10289 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75  e1 *pCache, .  u
1028a 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d  nsigned int iLim
1028b 69 74 20 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65  it .){.  unsigne
1028c 64 20 69 6e 74 20 68 3b 0a 20 20 61 73 73 65 72  d int h;.  asser
1028d 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1028e 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75  _held(pcache1.mu
1028f 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 68 3d  tex) );.  for(h=
10290 30 3b 20 68 3c 70 43 61 63 68 65 2d 3e 6e 48 61  0; h<pCache->nHa
10291 73 68 3b 20 68 2b 2b 29 7b 0a 20 20 20 20 50 67  sh; h++){.    Pg
10292 48 64 72 31 20 2a 2a 70 70 20 3d 20 26 70 43 61  Hdr1 **pp = &pCa
10293 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20  che->apHash[h]; 
10294 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 61  .    PgHdr1 *pPa
10295 67 65 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  ge;.    while( (
10296 70 50 61 67 65 20 3d 20 2a 70 70 29 21 3d 30 20  pPage = *pp)!=0 
10297 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  ){.      if( pPa
10298 67 65 2d 3e 69 4b 65 79 3e 3d 69 4c 69 6d 69 74  ge->iKey>=iLimit
10299 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63 61 63   ){.        pcac
1029a 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65  he1PinPage(pPage
1029b 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 3d  );.        *pp =
1029c 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20   pPage->pNext;. 
1029d 20 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72         pcache1Fr
1029e 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  eePage(pPage);. 
1029f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
102a0 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d      pp = &pPage-
102a1 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  >pNext;.      }.
102a2 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a      }.  }.}../**
102a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
102a8 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f  ******* sqlite3_
102a9 70 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 2a  pcache Methods *
102aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
102ad 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
102ae 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
102af 74 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 74  te3_pcache.xInit
102b0 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
102b1 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 6e  ic int pcache1In
102b2 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  it(void *NotUsed
102b3 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
102b4 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
102b5 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65    memset(&pcache
102b6 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61  1, 0, sizeof(pca
102b7 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71  che1));.  if( sq
102b8 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
102b9 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
102ba 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65      pcache1.mute
102bb 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  x = sqlite3_mute
102bc 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  x_alloc(SQLITE_M
102bd 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29  UTEX_STATIC_LRU)
102be 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
102bf 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
102c0 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
102c1 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
102c2 5f 70 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77  _pcache.xShutdow
102c3 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  n method..*/.sta
102c4 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
102c5 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e  Shutdown(void *N
102c6 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45  otUsed){.  UNUSE
102c7 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
102c8 73 65 64 29 3b 0a 20 20 2f 2a 20 6e 6f 2d 6f 70  sed);.  /* no-op
102c9 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70   */.}../*.** Imp
102ca 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
102cb 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
102cc 65 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64  e.xCreate method
102cd 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ..**.** Allocate
102ce 20 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f   a new cache..*/
102cf 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
102d0 70 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 43  pcache *pcache1C
102d1 72 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 65  reate(int szPage
102d2 2c 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65  , int bPurgeable
102d3 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  ){.  PCache1 *pC
102d4 61 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 20  ache;..  pCache 
102d5 3d 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 6c  = (PCache1 *)sql
102d6 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
102d7 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20  of(PCache1));.  
102d8 69 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 20  if( pCache ){.  
102d9 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2c    memset(pCache,
102da 20 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68   0, sizeof(PCach
102db 65 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65  e1));.    pCache
102dc 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67  ->szPage = szPag
102dd 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 62  e;.    pCache->b
102de 50 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 75  Purgeable = (bPu
102df 72 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 29  rgeable ? 1 : 0)
102e0 3b 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 65  ;.    if( bPurge
102e1 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43  able ){.      pC
102e2 61 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b  ache->nMin = 10;
102e3 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 45 6e  .      pcache1En
102e4 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  terMutex();.    
102e5 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61    pcache1.nMinPa
102e6 67 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d  ge += pCache->nM
102e7 69 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65  in;.      pcache
102e8 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
102e9 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
102ea 6e 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  n (sqlite3_pcach
102eb 65 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f  e *)pCache;.}../
102ec 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
102ed 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
102ee 65 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 65  e3_pcache.xCache
102ef 73 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a  size method. .**
102f0 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68  .** Configure th
102f1 65 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d  e cache_size lim
102f2 69 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e 0a  it for a cache..
102f3 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
102f4 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 28  cache1Cachesize(
102f5 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
102f6 70 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20  p, int nMax){.  
102f7 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20  PCache1 *pCache 
102f8 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a  = (PCache1 *)p;.
102f9 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50    if( pCache->bP
102fa 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20  urgeable ){.    
102fb 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
102fc 78 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31  x();.    pcache1
102fd 2e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d  .nMaxPage += (nM
102fe 61 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61  ax - pCache->nMa
102ff 78 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  x);.    pCache->
10300 6e 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20  nMax = nMax;.   
10301 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d   pcache1EnforceM
10302 61 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 63  axPage();.    pc
10303 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
10304 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
10305 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
10306 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
10307 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20  ache.xPagecount 
10308 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74  method. .*/.stat
10309 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61  ic int pcache1Pa
1030a 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  gecount(sqlite3_
1030b 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e  pcache *p){.  in
1030c 74 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e  t n;.  pcache1En
1030d 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20  terMutex();.  n 
1030e 3d 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 29  = ((PCache1 *)p)
1030f 2d 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 68  ->nPage;.  pcach
10310 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  e1LeaveMutex();.
10311 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f    return n;.}../
10312 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10313 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10314 65 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 68  e3_pcache.xFetch
10315 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20   method. .**.** 
10316 46 65 74 63 68 20 61 20 70 61 67 65 20 62 79 20  Fetch a page by 
10317 6b 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  key value..**.**
10318 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
10319 61 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 62  a new page may b
1031a 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74  e allocated by t
1031b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 70  his function dep
1031c 65 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76  ends on.** the v
1031d 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61  alue of the crea
1031e 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e  teFlag argument.
1031f 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
10320 20 74 68 72 65 65 20 64 69 66 66 65 72 65 6e 74   three different
10321 20 61 70 70 72 6f 61 63 68 65 73 20 74 6f 20 6f   approaches to o
10322 62 74 61 69 6e 69 6e 67 20 73 70 61 63 65 20 66  btaining space f
10323 6f 72 20 61 20 70 61 67 65 2c 0a 2a 2a 20 64 65  or a page,.** de
10324 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76  pending on the v
10325 61 6c 75 65 20 6f 66 20 70 61 72 61 6d 65 74 65  alue of paramete
10326 72 20 63 72 65 61 74 65 46 6c 61 67 20 28 77 68  r createFlag (wh
10327 69 63 68 20 6d 61 79 20 62 65 20 30 2c 20 31 20  ich may be 0, 1 
10328 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31  or 2)..**.**   1
10329 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20  . Regardless of 
1032a 74 68 65 20 76 61 6c 75 65 20 6f 66 20 63 72 65  the value of cre
1032b 61 74 65 46 6c 61 67 2c 20 74 68 65 20 63 61 63  ateFlag, the cac
1032c 68 65 20 69 73 20 73 65 61 72 63 68 65 64 20 66  he is searched f
1032d 6f 72 20 61 20 0a 2a 2a 20 20 20 20 20 20 63 6f  or a .**      co
1032e 70 79 20 6f 66 20 74 68 65 20 72 65 71 75 65 73  py of the reques
1032f 74 65 64 20 70 61 67 65 2e 20 49 66 20 6f 6e 65  ted page. If one
10330 20 69 73 20 66 6f 75 6e 64 2c 20 69 74 20 69 73   is found, it is
10331 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
10332 20 20 20 32 2e 20 49 66 20 63 72 65 61 74 65 46     2. If createF
10333 6c 61 67 3d 3d 30 20 61 6e 64 20 74 68 65 20 70  lag==0 and the p
10334 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
10335 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  dy in the cache,
10336 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 20 20 20 20   NULL is.**     
10337 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
10338 20 20 20 33 2e 20 49 66 20 63 72 65 61 74 65 46     3. If createF
10339 6c 61 67 20 69 73 20 31 2c 20 74 68 65 20 63 61  lag is 1, the ca
1033a 63 68 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73  che is marked as
1033b 20 70 75 72 67 65 61 62 6c 65 20 61 6e 64 20 74   purgeable and t
1033c 68 65 20 70 61 67 65 20 69 73 20 0a 2a 2a 20 20  he page is .**  
1033d 20 20 20 20 6e 6f 74 20 61 6c 72 65 61 64 79 20      not already 
1033e 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 61 6e  in the cache, an
1033f 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 20 74  d if either of t
10340 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
10341 20 74 72 75 65 2c 20 0a 2a 2a 20 20 20 20 20 20   true, .**      
10342 72 65 74 75 72 6e 20 4e 55 4c 4c 3a 0a 2a 2a 0a  return NULL:.**.
10343 2a 2a 20 20 20 20 20 20 20 28 61 29 20 74 68 65  **       (a) the
10344 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10345 20 70 69 6e 6e 65 64 20 62 79 20 74 68 65 20 63   pinned by the c
10346 61 63 68 65 20 69 73 20 67 72 65 61 74 65 72 20  ache is greater 
10347 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  than.**         
10348 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20    PCache1.nMax, 
10349 6f 72 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20  or.**       (b) 
1034a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
1034b 67 65 73 20 70 69 6e 6e 65 64 20 62 79 20 74 68  ges pinned by th
1034c 65 20 63 61 63 68 65 20 69 73 20 67 72 65 61 74  e cache is great
1034d 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20  er than.**      
1034e 20 20 20 20 20 74 68 65 20 73 75 6d 20 6f 66 20       the sum of 
1034f 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 20 70 75 72  nMax for all pur
10350 67 65 61 62 6c 65 20 63 61 63 68 65 73 2c 20 6c  geable caches, l
10351 65 73 73 20 74 68 65 20 73 75 6d 20 6f 66 20 0a  ess the sum of .
10352 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6e 4d 69  **           nMi
10353 6e 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20  n for all other 
10354 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73  purgeable caches
10355 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 34 2e 20 49 66  . .**.**   4. If
10356 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 72   none of the fir
10357 73 74 20 74 68 72 65 65 20 63 6f 6e 64 69 74 69  st three conditi
10358 6f 6e 73 20 61 70 70 6c 79 20 61 6e 64 20 74 68  ons apply and th
10359 65 20 63 61 63 68 65 20 69 73 20 6d 61 72 6b 65  e cache is marke
1035a 64 0a 2a 2a 20 20 20 20 20 20 61 73 20 70 75 72  d.**      as pur
1035b 67 65 61 62 6c 65 2c 20 61 6e 64 20 69 66 20 6f  geable, and if o
1035c 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
1035d 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 2a 2a 0a  ing is true:.**.
1035e 2a 2a 20 20 20 20 20 20 20 28 61 29 20 54 68 65  **       (a) The
1035f 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10360 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
10361 68 65 20 63 61 63 68 65 20 69 73 20 61 6c 72 65  he cache is alre
10362 61 64 79 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ady .**         
10363 20 20 50 43 61 63 68 65 31 2e 6e 4d 61 78 2c 20    PCache1.nMax, 
10364 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 28  or.**.**       (
10365 62 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  b) The number of
10366 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64   pages allocated
10367 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 62   for all purgeab
10368 6c 65 20 63 61 63 68 65 73 20 69 73 0a 2a 2a 20  le caches is.** 
10369 20 20 20 20 20 20 20 20 20 20 61 6c 72 65 61 64            alread
1036a 79 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67 72  y equal to or gr
1036b 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73  eater than the s
1036c 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 20 61  um of nMax for a
1036d 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ll.**           
1036e 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73  purgeable caches
1036f 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 74 68 65  ,.**.**      the
10370 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 63  n attempt to rec
10371 79 63 6c 65 20 61 20 70 61 67 65 20 66 72 6f 6d  ycle a page from
10372 20 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 49   the LRU list. I
10373 66 20 69 74 20 69 73 20 74 68 65 20 72 69 67 68  f it is the righ
10374 74 0a 2a 2a 20 20 20 20 20 20 73 69 7a 65 2c 20  t.**      size, 
10375 72 65 74 75 72 6e 20 74 68 65 20 72 65 63 79 63  return the recyc
10376 6c 65 64 20 62 75 66 66 65 72 2e 20 4f 74 68 65  led buffer. Othe
10377 72 77 69 73 65 2c 20 66 72 65 65 20 74 68 65 20  rwise, free the 
10378 62 75 66 66 65 72 20 61 6e 64 0a 2a 2a 20 20 20  buffer and.**   
10379 20 20 20 70 72 6f 63 65 65 64 20 74 6f 20 73 74     proceed to st
1037a 65 70 20 35 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 35  ep 5. .**.**   5
1037b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6c 6c  . Otherwise, all
1037c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e  ocate and return
1037d 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66 66   a new page buff
1037e 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
1037f 69 64 20 2a 70 63 61 63 68 65 31 46 65 74 63 68  id *pcache1Fetch
10380 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20  (sqlite3_pcache 
10381 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  *p, unsigned int
10382 20 69 4b 65 79 2c 20 69 6e 74 20 63 72 65 61 74   iKey, int creat
10383 65 46 6c 61 67 29 7b 0a 20 20 75 6e 73 69 67 6e  eFlag){.  unsign
10384 65 64 20 69 6e 74 20 6e 50 69 6e 6e 65 64 3b 0a  ed int nPinned;.
10385 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
10386 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70  e = (PCache1 *)p
10387 3b 0a 20 20 50 67 48 64 72 31 20 2a 70 50 61 67  ;.  PgHdr1 *pPag
10388 65 20 3d 20 30 3b 0a 0a 20 20 70 63 61 63 68 65  e = 0;..  pcache
10389 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
1038a 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d   if( createFlag=
1038b 3d 31 20 29 20 73 71 6c 69 74 65 33 42 65 67 69  =1 ) sqlite3Begi
1038c 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
1038d 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68  ..  /* Search th
1038e 65 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72  e hash table for
1038f 20 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74   an existing ent
10390 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 43 61  ry. */.  if( pCa
10391 63 68 65 2d 3e 6e 48 61 73 68 3e 30 20 29 7b 0a  che->nHash>0 ){.
10392 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
10393 20 68 20 3d 20 69 4b 65 79 20 25 20 70 43 61 63   h = iKey % pCac
10394 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 20 20 66  he->nHash;.    f
10395 6f 72 28 70 50 61 67 65 3d 70 43 61 63 68 65 2d  or(pPage=pCache-
10396 3e 61 70 48 61 73 68 5b 68 5d 3b 20 70 50 61 67  >apHash[h]; pPag
10397 65 26 26 70 50 61 67 65 2d 3e 69 4b 65 79 21 3d  e&&pPage->iKey!=
10398 69 4b 65 79 3b 20 70 50 61 67 65 3d 70 50 61 67  iKey; pPage=pPag
10399 65 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 7d 0a 0a  e->pNext);.  }..
1039a 20 20 69 66 28 20 70 50 61 67 65 20 7c 7c 20 63    if( pPage || c
1039b 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 7b 0a  reateFlag==0 ){.
1039c 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61      pcache1PinPa
1039d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 67  ge(pPage);.    g
1039e 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20  oto fetch_out;. 
1039f 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 33 20   }..  /* Step 3 
103a0 6f 66 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  of header commen
103a1 74 2e 20 2a 2f 0a 20 20 6e 50 69 6e 6e 65 64 20  t. */.  nPinned 
103a2 3d 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20  = pCache->nPage 
103a3 2d 20 70 43 61 63 68 65 2d 3e 6e 52 65 63 79 63  - pCache->nRecyc
103a4 6c 61 62 6c 65 3b 0a 20 20 69 66 28 20 63 72 65  lable;.  if( cre
103a5 61 74 65 46 6c 61 67 3d 3d 31 20 26 26 20 70 43  ateFlag==1 && pC
103a6 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
103a7 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 6e 50   && (.        nP
103a8 69 6e 6e 65 64 3e 3d 28 70 63 61 63 68 65 31 2e  inned>=(pcache1.
103a9 6e 4d 61 78 50 61 67 65 2b 70 43 61 63 68 65 2d  nMaxPage+pCache-
103aa 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 31 2e 6e 4d  >nMin-pcache1.nM
103ab 69 6e 50 61 67 65 29 0a 20 20 20 20 20 7c 7c 20  inPage).     || 
103ac 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 61 63 68 65  nPinned>=(pCache
103ad 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f 20 31 30 29  ->nMax * 9 / 10)
103ae 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f 74 6f 20  .  )){.    goto 
103af 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  fetch_out;.  }..
103b0 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 50    if( pCache->nP
103b1 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e 6e 48 61  age>=pCache->nHa
103b2 73 68 20 26 26 20 70 63 61 63 68 65 31 52 65 73  sh && pcache1Res
103b3 69 7a 65 48 61 73 68 28 70 43 61 63 68 65 29 20  izeHash(pCache) 
103b4 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63  ){.    goto fetc
103b5 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  h_out;.  }..  /*
103b6 20 53 74 65 70 20 34 2e 20 54 72 79 20 74 6f 20   Step 4. Try to 
103b7 72 65 63 79 63 6c 65 20 61 20 70 61 67 65 20 62  recycle a page b
103b8 75 66 66 65 72 20 69 66 20 61 70 70 72 6f 70 72  uffer if appropr
103b9 69 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70  iate. */.  if( p
103ba 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
103bb 65 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72  e && pcache1.pLr
103bc 75 54 61 69 6c 20 26 26 20 28 0a 20 20 20 20 20  uTail && (.     
103bd 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 31  (pCache->nPage+1
103be 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 20  >=pCache->nMax) 
103bf 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72  || pcache1.nCurr
103c0 65 6e 74 50 61 67 65 3e 3d 70 63 61 63 68 65 31  entPage>=pcache1
103c1 2e 6e 4d 61 78 50 61 67 65 0a 20 20 29 29 7b 0a  .nMaxPage.  )){.
103c2 20 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63      pPage = pcac
103c3 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20  he1.pLruTail;.  
103c4 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46    pcache1RemoveF
103c5 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29 3b 0a  romHash(pPage);.
103c6 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61      pcache1PinPa
103c7 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69  ge(pPage);.    i
103c8 66 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65  f( pPage->pCache
103c9 2d 3e 73 7a 50 61 67 65 21 3d 70 43 61 63 68 65  ->szPage!=pCache
103ca 2d 3e 73 7a 50 61 67 65 20 29 7b 0a 20 20 20 20  ->szPage ){.    
103cb 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67    pcache1FreePag
103cc 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
103cd 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d  pPage = 0;.    }
103ce 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 61 63  else{.      pcac
103cf 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65  he1.nCurrentPage
103d0 20 2d 3d 20 28 70 50 61 67 65 2d 3e 70 43 61 63   -= (pPage->pCac
103d1 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 2d  he->bPurgeable -
103d2 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
103d3 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ble);.    }.  }.
103d4 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20 49 66  .  /* Step 5. If
103d5 20 61 20 75 73 61 62 6c 65 20 70 61 67 65 20 62   a usable page b
103d6 75 66 66 65 72 20 68 61 73 20 73 74 69 6c 6c 20  uffer has still 
103d7 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64 2c 20  not been found, 
103d8 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f  .  ** attempt to
103d9 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
103da 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  one. .  */.  if(
103db 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70   !pPage ){.    p
103dc 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 41 6c  Page = pcache1Al
103dd 6c 6f 63 50 61 67 65 28 70 43 61 63 68 65 29 3b  locPage(pCache);
103de 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67  .  }..  if( pPag
103df 65 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65  e ){.    unsigne
103e0 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 20 25  d int h = iKey %
103e1 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a   pCache->nHash;.
103e2 20 20 20 20 2a 28 76 6f 69 64 20 2a 2a 29 28 50      *(void **)(P
103e3 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50  GHDR1_TO_PAGE(pP
103e4 61 67 65 29 29 20 3d 20 30 3b 0a 20 20 20 20 70  age)) = 0;.    p
103e5 43 61 63 68 65 2d 3e 6e 50 61 67 65 2b 2b 3b 0a  Cache->nPage++;.
103e6 20 20 20 20 70 50 61 67 65 2d 3e 69 4b 65 79 20      pPage->iKey 
103e7 3d 20 69 4b 65 79 3b 0a 20 20 20 20 70 50 61 67  = iKey;.    pPag
103e8 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 61 63 68  e->pNext = pCach
103e9 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20  e->apHash[h];.  
103ea 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 20    pPage->pCache 
103eb 3d 20 70 43 61 63 68 65 3b 0a 20 20 20 20 70 50  = pCache;.    pP
103ec 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20  age->pLruPrev = 
103ed 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c  0;.    pPage->pL
103ee 72 75 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20  ruNext = 0;.    
103ef 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68  pCache->apHash[h
103f0 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 0a  ] = pPage;.  }..
103f1 66 65 74 63 68 5f 6f 75 74 3a 0a 20 20 69 66 28  fetch_out:.  if(
103f2 20 70 50 61 67 65 20 26 26 20 69 4b 65 79 3e 70   pPage && iKey>p
103f3 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29  Cache->iMaxKey )
103f4 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d  {.    pCache->iM
103f5 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 20  axKey = iKey;.  
103f6 7d 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c  }.  if( createFl
103f7 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65 33 45  ag==1 ) sqlite3E
103f8 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
103f9 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65  ;.  pcache1Leave
103fa 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72  Mutex();.  retur
103fb 6e 20 28 70 50 61 67 65 20 3f 20 50 47 48 44 52  n (pPage ? PGHDR
103fc 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67 65 29  1_TO_PAGE(pPage)
103fd 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a   : 0);.}.../*.**
103fe 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
103ff 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
10400 63 61 63 68 65 2e 78 55 6e 70 69 6e 20 6d 65 74  cache.xUnpin met
10401 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 6b 20  hod..**.** Mark 
10402 61 20 70 61 67 65 20 61 73 20 75 6e 70 69 6e 6e  a page as unpinn
10403 65 64 20 28 65 6c 69 67 69 62 6c 65 20 66 6f 72  ed (eligible for
10404 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 72 65   asynchronous re
10405 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 74 61  cycling)..*/.sta
10406 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
10407 55 6e 70 69 6e 28 73 71 6c 69 74 65 33 5f 70 63  Unpin(sqlite3_pc
10408 61 63 68 65 20 2a 70 2c 20 76 6f 69 64 20 2a 70  ache *p, void *p
10409 50 67 2c 20 69 6e 74 20 72 65 75 73 65 55 6e 6c  Pg, int reuseUnl
1040a 69 6b 65 6c 79 29 7b 0a 20 20 50 43 61 63 68 65  ikely){.  PCache
1040b 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61  1 *pCache = (PCa
1040c 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64  che1 *)p;.  PgHd
1040d 72 31 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45  r1 *pPage = PAGE
1040e 5f 54 4f 5f 50 47 48 44 52 31 28 70 50 67 29 3b  _TO_PGHDR1(pPg);
1040f 0a 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ..  pcache1Enter
10410 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49  Mutex();..  /* I
10411 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f  t is an error to
10412 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
10413 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20  ion if the page 
10414 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a  is already .  **
10415 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f   part of the glo
10416 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20  bal LRU list..  
10417 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
10418 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20  ge->pLruPrev==0 
10419 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65  && pPage->pLruNe
1041a 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  xt==0 );.  asser
1041b 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48  t( pcache1.pLruH
1041c 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 63  ead!=pPage && pc
1041d 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d  ache1.pLruTail!=
1041e 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20  pPage );..  if( 
1041f 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c  reuseUnlikely ||
10420 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10421 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d  tPage>pcache1.nM
10422 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63  axPage ){.    pc
10423 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
10424 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ash(pPage);.    
10425 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10426 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  pPage);.  }else{
10427 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20  .    /* Add the 
10428 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62  page to the glob
10429 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72  al LRU list. Nor
1042a 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20  mally, the page 
1042b 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20  is added to.    
1042c 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  ** the head of t
1042d 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61  he list (last pa
1042e 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65  ge to be recycle
1042f 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20  d). However, if 
10430 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73  the .    ** reus
10431 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70  eUnlikely flag p
10432 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75  assed to this fu
10433 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20  nction is true, 
10434 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65  the page is adde
10435 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  d.    ** to the 
10436 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74  tail of the list
10437 20 28 66 69 72 73 74 20 70 61 67 65 20 74 6f 20   (first page to 
10438 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20  be recycled)..  
10439 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61    */.    if( pca
1043a 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b  che1.pLruHead ){
1043b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70  .      pcache1.p
1043c 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65  LruHead->pLruPre
1043d 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20  v = pPage;.     
1043e 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
1043f 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48   = pcache1.pLruH
10440 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68  ead;.      pcach
10441 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50  e1.pLruHead = pP
10442 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  age;.    }else{.
10443 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10444 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a  ruTail = pPage;.
10445 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10446 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a  ruHead = pPage;.
10447 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65      }.    pCache
10448 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b  ->nRecyclable++;
10449 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c  .  }..  pcache1L
1044a 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a  eaveMutex();.}..
1044b 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
1044c 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
1044d 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65  te3_pcache.xReke
1044e 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74  y method. .*/.st
1044f 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10450 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  1Rekey(.  sqlite
10451 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76  3_pcache *p,.  v
10452 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69  oid *pPg,.  unsi
10453 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20  gned int iOld,. 
10454 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e   unsigned int iN
10455 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20  ew.){.  PCache1 
10456 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
10457 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31  e1 *)p;.  PgHdr1
10458 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54   *pPage = PAGE_T
10459 4f 5f 50 47 48 44 52 31 28 70 50 67 29 3b 0a 20  O_PGHDR1(pPg);. 
1045a 20 50 67 48 64 72 31 20 2a 2a 70 70 3b 0a 20 20   PgHdr1 **pp;.  
1045b 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20  unsigned int h; 
1045c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1045d 2d 3e 69 4b 65 79 3d 3d 69 4f 6c 64 20 29 3b 0a  ->iKey==iOld );.
1045e 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d  .  pcache1EnterM
1045f 75 74 65 78 28 29 3b 0a 0a 20 20 68 20 3d 20 69  utex();..  h = i
10460 4f 6c 64 25 70 43 61 63 68 65 2d 3e 6e 48 61 73  Old%pCache->nHas
10461 68 3b 0a 20 20 70 70 20 3d 20 26 70 43 61 63 68  h;.  pp = &pCach
10462 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20  e->apHash[h];.  
10463 77 68 69 6c 65 28 20 28 2a 70 70 29 21 3d 70 50  while( (*pp)!=pP
10464 61 67 65 20 29 7b 0a 20 20 20 20 70 70 20 3d 20  age ){.    pp = 
10465 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20  &(*pp)->pNext;. 
10466 20 7d 0a 20 20 2a 70 70 20 3d 20 70 50 61 67 65   }.  *pp = pPage
10467 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 68 20 3d 20  ->pNext;..  h = 
10468 69 4e 65 77 25 70 43 61 63 68 65 2d 3e 6e 48 61  iNew%pCache->nHa
10469 73 68 3b 0a 20 20 70 50 61 67 65 2d 3e 69 4b 65  sh;.  pPage->iKe
1046a 79 20 3d 20 69 4e 65 77 3b 0a 20 20 70 50 61 67  y = iNew;.  pPag
1046b 65 2d 3e 70 4e 65 78 74 20 3d 20 70 43 61 63 68  e->pNext = pCach
1046c 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20  e->apHash[h];.  
1046d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68  pCache->apHash[h
1046e 5d 20 3d 20 70 50 61 67 65 3b 0a 0a 20 20 69 66  ] = pPage;..  if
1046f 28 20 69 4e 65 77 3e 70 43 61 63 68 65 2d 3e 69  ( iNew>pCache->i
10470 4d 61 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 43  MaxKey ){.    pC
10471 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20  ache->iMaxKey = 
10472 69 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61  iNew;.  }..  pca
10473 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10474 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
10475 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
10476 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e   sqlite3_pcache.
10477 78 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f 64  xTruncate method
10478 2e 20 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64  . .**.** Discard
10479 20 61 6c 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61   all unpinned pa
1047a 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65  ges in the cache
1047b 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d   with a page num
1047c 62 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20  ber equal to.** 
1047d 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  or greater than 
1047e 70 61 72 61 6d 65 74 65 72 20 69 4c 69 6d 69 74  parameter iLimit
1047f 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67  . Any pinned pag
10480 65 73 20 77 69 74 68 20 61 20 70 61 67 65 20 6e  es with a page n
10481 75 6d 62 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74  umber.** equal t
10482 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61  o or greater tha
10483 6e 20 69 4c 69 6d 69 74 20 61 72 65 20 69 6d 70  n iLimit are imp
10484 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64  licitly unpinned
10485 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
10486 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65   pcache1Truncate
10487 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20  (sqlite3_pcache 
10488 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  *p, unsigned int
10489 20 69 4c 69 6d 69 74 29 7b 0a 20 20 50 43 61 63   iLimit){.  PCac
1048a 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50  he1 *pCache = (P
1048b 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 63  Cache1 *)p;.  pc
1048c 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
1048d 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3c  );.  if( iLimit<
1048e 3d 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79  =pCache->iMaxKey
1048f 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 54   ){.    pcache1T
10490 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43  runcateUnsafe(pC
10491 61 63 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20  ache, iLimit);. 
10492 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b     pCache->iMaxK
10493 65 79 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20  ey = iLimit-1;. 
10494 20 7d 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76   }.  pcache1Leav
10495 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a  eMutex();.}../*.
10496 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
10497 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
10498 5f 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79  _pcache.xDestroy
10499 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20   method. .**.** 
1049a 44 65 73 74 72 6f 79 20 61 20 63 61 63 68 65 20  Destroy a cache 
1049b 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
1049c 70 63 61 63 68 65 31 43 72 65 61 74 65 28 29 2e  pcache1Create().
1049d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1049e 70 63 61 63 68 65 31 44 65 73 74 72 6f 79 28 73  pcache1Destroy(s
1049f 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70  qlite3_pcache *p
104a0 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  ){.  PCache1 *pC
104a1 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20  ache = (PCache1 
104a2 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e  *)p;.  pcache1En
104a3 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63  terMutex();.  pc
104a4 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73  ache1TruncateUns
104a5 61 66 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a  afe(pCache, 0);.
104a6 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61    pcache1.nMaxPa
104a7 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d  ge -= pCache->nM
104a8 61 78 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d  ax;.  pcache1.nM
104a9 69 6e 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65  inPage -= pCache
104aa 2d 3e 6e 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65  ->nMin;.  pcache
104ab 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28  1EnforceMaxPage(
104ac 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  );.  pcache1Leav
104ad 65 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c 69  eMutex();.  sqli
104ae 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 2d  te3_free(pCache-
104af 3e 61 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69  >apHash);.  sqli
104b0 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 29  te3_free(pCache)
104b1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
104b2 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
104b3 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61  ed during initia
104b4 6c 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69 74 65  lization (sqlite
104b5 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 20  3_initialize()) 
104b6 74 6f 0a 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68  to.** install th
104b7 65 20 64 65 66 61 75 6c 74 20 70 6c 75 67 67 61  e default plugga
104b8 62 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65  ble cache module
104b9 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20 75  , assuming the u
104ba 73 65 72 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61  ser has not.** a
104bb 6c 72 65 61 64 79 20 70 72 6f 76 69 64 65 64 20  lready provided 
104bc 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a  an alternative..
104bd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
104be 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43  E void sqlite3PC
104bf 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 76  acheSetDefault(v
104c0 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73  oid){.  static s
104c1 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
104c2 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74  thods defaultMet
104c3 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20  hods = {.    0, 
104c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
104c5 20 20 20 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f        /* pArg */
104c6 0a 20 20 20 20 70 63 61 63 68 65 31 49 6e 69 74  .    pcache1Init
104c7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
104c8 20 78 49 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63   xInit */.    pc
104c9 61 63 68 65 31 53 68 75 74 64 6f 77 6e 2c 20 20  ache1Shutdown,  
104ca 20 20 20 20 20 20 20 2f 2a 20 78 53 68 75 74 64         /* xShutd
104cb 6f 77 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  own */.    pcach
104cc 65 31 43 72 65 61 74 65 2c 20 20 20 20 20 20 20  e1Create,       
104cd 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2a      /* xCreate *
104ce 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43 61 63  /.    pcache1Cac
104cf 68 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20 2f  hesize,        /
104d0 2a 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a  * xCachesize */.
104d1 20 20 20 20 70 63 61 63 68 65 31 50 61 67 65 63      pcache1Pagec
104d2 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20  ount,        /* 
104d3 78 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20  xPagecount */.  
104d4 20 20 70 63 61 63 68 65 31 46 65 74 63 68 2c 20    pcache1Fetch, 
104d5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
104d6 65 74 63 68 20 2a 2f 0a 20 20 20 20 70 63 61 63  etch */.    pcac
104d7 68 65 31 55 6e 70 69 6e 2c 20 20 20 20 20 20 20  he1Unpin,       
104d8 20 20 20 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a       /* xUnpin *
104d9 2f 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6b  /.    pcache1Rek
104da 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ey,            /
104db 2a 20 78 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20  * xRekey */.    
104dc 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 2c  pcache1Truncate,
104dd 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75           /* xTru
104de 6e 63 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61  ncate */.    pca
104df 63 68 65 31 44 65 73 74 72 6f 79 20 20 20 20 20  che1Destroy     
104e0 20 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72 6f        /* xDestro
104e1 79 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69  y */.  };.  sqli
104e2 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
104e3 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c  E_CONFIG_PCACHE,
104e4 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73   &defaultMethods
104e5 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  );.}..#ifdef SQL
104e6 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
104e7 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a  Y_MANAGEMENT./*.
104e8 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
104e9 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66 72   is called to fr
104ea 65 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20 64  ee superfluous d
104eb 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
104ec 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68  ated memory.** h
104ed 65 6c 64 20 62 79 20 74 68 65 20 70 61 67 65 72  eld by the pager
104ee 20 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20   system. Memory 
104ef 69 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53 51  in use by any SQ
104f0 4c 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f 63  Lite pager alloc
104f1 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63  ated.** by the c
104f2 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d 61  urrent thread ma
104f3 79 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72 65  y be sqlite3_fre
104f4 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65  e()ed..**.** nRe
104f5 71 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  q is the number 
104f6 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  of bytes of memo
104f7 72 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e 63  ry required. Onc
104f8 65 20 74 68 69 73 20 6d 75 63 68 20 68 61 73 0a  e this much has.
104f9 2a 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64  ** been released
104fa 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72  , the function r
104fb 65 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 75  eturns. The retu
104fc 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
104fd 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a  total number .**
104fe 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d   of bytes of mem
104ff 6f 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f  ory released..*/
10500 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10501 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
10502 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69  eReleaseMemory(i
10503 6e 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20  nt nReq){.  int 
10504 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 66 28  nFree = 0;.  if(
10505 20 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 3d   pcache1.pStart=
10506 3d 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31  =0 ){.    PgHdr1
10507 20 2a 70 3b 0a 20 20 20 20 70 63 61 63 68 65 31   *p;.    pcache1
10508 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
10509 20 20 77 68 69 6c 65 28 20 28 6e 52 65 71 3c 30    while( (nReq<0
1050a 20 7c 7c 20 6e 46 72 65 65 3c 6e 52 65 71 29 20   || nFree<nReq) 
1050b 26 26 20 28 70 3d 70 63 61 63 68 65 31 2e 70 4c  && (p=pcache1.pL
1050c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 20  ruTail) ){.     
1050d 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65   nFree += sqlite
1050e 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a  3MallocSize(p);.
1050f 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e        pcache1Pin
10510 50 61 67 65 28 70 29 3b 0a 20 20 20 20 20 20 70  Page(p);.      p
10511 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d  cache1RemoveFrom
10512 48 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20 70  Hash(p);.      p
10513 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70  cache1FreePage(p
10514 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61  );.    }.    pca
10515 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10516 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
10517 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Free;.}.#endif /
10518 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
10519 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
1051a 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  T */..#ifdef SQL
1051b 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54  ITE_TEST./*.** T
1051c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1051d 75 73 65 64 20 62 79 20 74 65 73 74 20 70 72 6f  used by test pro
1051e 63 65 64 75 72 65 73 20 74 6f 20 69 6e 73 70 65  cedures to inspe
1051f 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ct the internal 
10520 73 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20  state.** of the 
10521 67 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f  global cache..*/
10522 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10523 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
10524 68 65 53 74 61 74 73 28 0a 20 20 69 6e 74 20 2a  heStats(.  int *
10525 70 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20  pnCurrent,      
10526 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75  /* OUT: Total nu
10527 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61  mber of pages ca
10528 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ched */.  int *p
10529 6e 4d 61 78 2c 20 20 20 20 20 20 20 20 20 20 2f  nMax,          /
1052a 2a 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61  * OUT: Global ma
1052b 78 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a 65  ximum cache size
1052c 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e   */.  int *pnMin
1052d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  ,          /* OU
1052e 54 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68 65  T: Sum of PCache
1052f 31 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67 65  1.nMin for purge
10530 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20  able caches */. 
10531 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62   int *pnRecyclab
10532 6c 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f  le    /* OUT: To
10533 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
10534 67 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  ges available fo
10535 72 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29  r recycling */.)
10536 7b 0a 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20  {.  PgHdr1 *p;. 
10537 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65   int nRecyclable
10538 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63   = 0;.  for(p=pc
10539 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 20  ache1.pLruHead; 
1053a 70 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74  p; p=p->pLruNext
1053b 29 7b 0a 20 20 20 20 6e 52 65 63 79 63 6c 61 62  ){.    nRecyclab
1053c 6c 65 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43  le++;.  }.  *pnC
1053d 75 72 72 65 6e 74 20 3d 20 70 63 61 63 68 65 31  urrent = pcache1
1053e 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0a 20  .nCurrentPage;. 
1053f 20 2a 70 6e 4d 61 78 20 3d 20 70 63 61 63 68 65   *pnMax = pcache
10540 31 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70  1.nMaxPage;.  *p
10541 6e 4d 69 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e  nMin = pcache1.n
10542 4d 69 6e 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65  MinPage;.  *pnRe
10543 63 79 63 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79  cyclable = nRecy
10544 63 6c 61 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66  clable;.}.#endif
10545 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
10546 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 31  * End of pcache1
10547 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
10548 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10549 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1054a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1054b 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 6f 77  * Begin file row
1054c 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  set.c **********
1054d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1054e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1054f 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63  /./*.** 2008 Dec
10550 65 6d 62 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68  ember 3.**.** Th
10551 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
10552 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
10553 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
10554 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
10555 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
10556 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
10557 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
10558 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
10559 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1055a 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1055b 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1055c 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1055d 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1055e 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1055f 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
10560 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
10561 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
10562 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10563 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10564 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10565 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10566 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
10567 73 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  s module impleme
10568 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 77 65  nts an object we
10569 20 63 61 6c 6c 20 61 20 22 52 6f 77 53 65 74 22   call a "RowSet"
1056a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53  ..**.** The RowS
1056b 65 74 20 6f 62 6a 65 63 74 20 69 73 20 61 20 63  et object is a c
1056c 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77  ollection of row
1056d 69 64 73 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20  ids.  Rowids.** 
1056e 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74  are inserted int
1056f 6f 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e 20  o the RowSet in 
10570 61 6e 20 61 72 62 69 74 72 61 72 79 20 6f 72 64  an arbitrary ord
10571 65 72 2e 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20  er.  Inserts.** 
10572 63 61 6e 20 62 65 20 69 6e 74 65 72 6d 69 78 65  can be intermixe
10573 64 20 77 69 74 68 20 74 65 73 74 73 20 74 6f 20  d with tests to 
10574 73 65 65 20 69 66 20 61 20 67 69 76 65 6e 20 72  see if a given r
10575 6f 77 69 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a  owid has been.**
10576 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 65   previously inse
10577 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52 6f  rted into the Ro
10578 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  wSet..**.** Afte
10579 72 20 61 6c 6c 20 69 6e 73 65 72 74 73 20 61 72  r all inserts ar
1057a 65 20 66 69 6e 69 73 68 65 64 2c 20 69 74 20 69  e finished, it i
1057b 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 65 78  s possible to ex
1057c 74 72 61 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65  tract the.** ele
1057d 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 52 6f 77  ments of the Row
1057e 53 65 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72  Set in sorted or
1057f 64 65 72 2e 20 20 4f 6e 63 65 20 74 68 69 73 20  der.  Once this 
10580 65 78 74 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72  extraction.** pr
10581 6f 63 65 73 73 20 68 61 73 20 73 74 61 72 74 65  ocess has starte
10582 64 2c 20 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e  d, no new elemen
10583 74 73 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74  ts may be insert
10584 65 64 2e 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c  ed..**.** Hence,
10585 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 6f   the primitive o
10586 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 61 20  perations for a 
10587 52 6f 77 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a  RowSet are:.**.*
10588 2a 20 20 20 20 43 52 45 41 54 45 0a 2a 2a 20 20  *    CREATE.**  
10589 20 20 49 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54    INSERT.**    T
1058a 45 53 54 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45  EST.**    SMALLE
1058b 53 54 0a 2a 2a 20 20 20 20 44 45 53 54 52 4f 59  ST.**    DESTROY
1058c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 52 45 41 54  .**.** The CREAT
1058d 45 20 61 6e 64 20 44 45 53 54 52 4f 59 20 70 72  E and DESTROY pr
1058e 69 6d 69 74 69 76 65 73 20 61 72 65 20 74 68 65  imitives are the
1058f 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 61 6e 64   constructor and
10590 20 64 65 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20   destructor,.** 
10591 6f 62 76 69 6f 75 73 6c 79 2e 20 20 54 68 65 20  obviously.  The 
10592 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65  INSERT primitive
10593 20 61 64 64 73 20 61 20 6e 65 77 20 65 6c 65 6d   adds a new elem
10594 65 6e 74 20 74 6f 20 74 68 65 20 52 6f 77 53 65  ent to the RowSe
10595 74 2e 0a 2a 2a 20 54 45 53 54 20 63 68 65 63 6b  t..** TEST check
10596 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65  s to see if an e
10597 6c 65 6d 65 6e 74 20 69 73 20 61 6c 72 65 61 64  lement is alread
10598 79 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e  y in the RowSet.
10599 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78    SMALLEST.** ex
1059a 74 72 61 63 74 73 20 74 68 65 20 6c 65 61 73 74  tracts the least
1059b 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20   value from the 
1059c 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  RowSet..**.** Th
1059d 65 20 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69  e INSERT primiti
1059e 76 65 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74  ve might allocat
1059f 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d  e additional mem
105a0 6f 72 79 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a  ory.  Memory is.
105a1 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  ** allocated in 
105a2 63 68 75 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49  chunks so most I
105a3 4e 53 45 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c  NSERTs do no all
105a4 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 72 65 20  ocation.  There 
105a5 69 73 20 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20  is an .** upper 
105a6 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a  bound on the siz
105a7 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d  e of allocated m
105a8 65 6d 6f 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72  emory.  No memor
105a9 79 20 69 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e  y is freed.** un
105aa 74 69 6c 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a  til DESTROY..**.
105ab 2a 2a 20 54 68 65 20 54 45 53 54 20 70 72 69 6d  ** The TEST prim
105ac 69 74 69 76 65 20 69 6e 63 6c 75 64 65 73 20 61  itive includes a
105ad 20 22 62 61 74 63 68 22 20 6e 75 6d 62 65 72 2e   "batch" number.
105ae 20 20 54 68 65 20 54 45 53 54 20 70 72 69 6d 69    The TEST primi
105af 74 69 76 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c  tive.** will onl
105b0 79 20 73 65 65 20 65 6c 65 6d 65 6e 74 73 20 74  y see elements t
105b1 68 61 74 20 77 65 72 65 20 69 6e 73 65 72 74 65  hat were inserte
105b2 64 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73  d before the las
105b3 74 20 63 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74  t change.** in t
105b4 68 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 2e  he batch number.
105b5 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
105b6 2c 20 69 66 20 61 6e 20 49 4e 53 45 52 54 20 6f  , if an INSERT o
105b7 63 63 75 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a  ccurs between.**
105b8 20 74 77 6f 20 54 45 53 54 73 20 77 68 65 72 65   two TESTs where
105b9 20 74 68 65 20 54 45 53 54 73 20 68 61 76 65 20   the TESTs have 
105ba 74 68 65 20 73 61 6d 65 20 62 61 74 63 68 20 6e  the same batch n
105bb 75 62 6d 65 72 2c 20 74 68 65 6e 20 74 68 65 0a  ubmer, then the.
105bc 2a 2a 20 76 61 6c 75 65 20 61 64 64 65 64 20 62  ** value added b
105bd 79 20 74 68 65 20 49 4e 53 45 52 54 20 77 69 6c  y the INSERT wil
105be 6c 20 6e 6f 74 20 62 65 20 76 69 73 69 62 6c 65  l not be visible
105bf 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 54   to the second T
105c0 45 53 54 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74  EST..** The init
105c1 69 61 6c 20 62 61 74 63 68 20 6e 75 6d 62 65 72  ial batch number
105c2 20 69 73 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20   is zero, so if 
105c3 74 68 65 20 76 65 72 79 20 66 69 72 73 74 20 54  the very first T
105c4 45 53 54 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  EST contains.** 
105c5 61 20 6e 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68  a non-zero batch
105c6 20 6e 75 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c   number, it will
105c7 20 73 65 65 20 61 6c 6c 20 70 72 69 6f 72 20 49   see all prior I
105c8 4e 53 45 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  NSERTs..**.** No
105c9 20 49 4e 53 45 52 54 73 20 6d 61 79 20 6f 63 63   INSERTs may occ
105ca 75 72 73 20 61 66 74 65 72 20 61 20 53 4d 41 4c  urs after a SMAL
105cb 4c 45 53 54 2e 20 20 41 6e 20 61 73 73 65 72 74  LEST.  An assert
105cc 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 20 69 66  ion will fail if
105cd 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 74 74 65  .** that is atte
105ce 6d 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  mpted..**.** The
105cf 20 63 6f 73 74 20 6f 66 20 61 6e 20 49 4e 53 45   cost of an INSE
105d0 52 54 20 69 73 20 72 6f 75 67 68 6c 79 20 63 6f  RT is roughly co
105d1 6e 73 74 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69  nstant.  (Someti
105d2 6d 65 20 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a  me new memory.**
105d3 20 68 61 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63   has to be alloc
105d4 61 74 65 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52  ated on an INSER
105d5 54 2e 29 20 20 54 68 65 20 63 6f 73 74 20 6f 66  T.)  The cost of
105d6 20 61 20 54 45 53 54 20 77 69 74 68 20 61 20 6e   a TEST with a n
105d7 65 77 0a 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62  ew.** batch numb
105d8 65 72 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77  er is O(NlogN) w
105d9 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75  here N is the nu
105da 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
105db 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 0a   in the RowSet..
105dc 2a 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61  ** The cost of a
105dd 20 54 45 53 54 20 75 73 69 6e 67 20 74 68 65 20   TEST using the 
105de 73 61 6d 65 20 62 61 74 63 68 20 6e 75 6d 62 65  same batch numbe
105df 72 20 69 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54  r is O(logN).  T
105e0 68 65 20 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68  he cost.** of th
105e1 65 20 66 69 72 73 74 20 53 4d 41 4c 4c 45 53 54  e first SMALLEST
105e2 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53   is O(NlogN).  S
105e3 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71  econd and subseq
105e4 75 65 6e 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a  uent SMALLEST.**
105e5 20 70 72 69 6d 69 74 69 76 65 73 20 61 72 65 20   primitives are 
105e6 63 6f 6e 73 74 61 6e 74 20 74 69 6d 65 2e 20 20  constant time.  
105e7 54 68 65 20 63 6f 73 74 20 6f 66 20 44 45 53 54  The cost of DEST
105e8 52 4f 59 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a  ROY is O(N)..**.
105e9 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20 61  ** There is an a
105ea 64 64 65 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e  dded cost of O(N
105eb 29 20 77 68 65 6e 20 73 77 69 74 63 68 69 6e 67  ) when switching
105ec 20 62 65 74 77 65 65 6e 20 54 45 53 54 20 61 6e   between TEST an
105ed 64 0a 2a 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72  d.** SMALLEST pr
105ee 69 6d 69 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20  imitives..**.** 
105ef 24 49 64 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20  $Id: rowset.c,v 
105f0 31 2e 36 20 32 30 30 39 2f 30 34 2f 32 32 20 31  1.6 2009/04/22 1
105f1 35 3a 33 32 3a 35 39 20 64 72 68 20 45 78 70 20  5:32:59 drh Exp 
105f2 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72  $.*/.../*.** Tar
105f3 67 65 74 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c  get size for all
105f4 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a  ocation chunks..
105f5 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45  */.#define ROWSE
105f6 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a  T_ALLOCATION_SIZ
105f7 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68  E 1024../*.** Th
105f8 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
105f9 65 74 20 65 6e 74 72 69 65 73 20 70 65 72 20 61  et entries per a
105fa 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e  llocation chunk.
105fb 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53  .*/.#define ROWS
105fc 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55  ET_ENTRY_PER_CHU
105fd 4e 4b 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20  NK  \.          
105fe 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 52               ((R
105ff 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e  OWSET_ALLOCATION
10600 5f 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28  _SIZE-8)/sizeof(
10601 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
10602 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  ry))../*.** Each
10603 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f 77 53   entry in a RowS
10604 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  et is an instanc
10605 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
10606 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74  ng object..*/.st
10607 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
10608 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 0a 20   {            . 
10609 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20   i64 v;         
1060a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1060b 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20 66 6f  * ROWID value fo
1060c 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a  r this entry */.
1060d 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
1060e 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20 20 20  ntry *pRight;   
1060f 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72 65 65  /* Right subtree
10610 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69 65 73   (larger entries
10611 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73  ) or list */.  s
10612 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
10613 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20  y *pLeft;    /* 
10614 4c 65 66 74 20 73 75 62 74 72 65 65 20 28 73 6d  Left subtree (sm
10615 61 6c 6c 65 72 20 65 6e 74 72 69 65 73 29 20 2a  aller entries) *
10616 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53  /.};../*.** RowS
10617 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 20  etEntry objects 
10618 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  are allocated in
10619 20 6c 61 72 67 65 20 63 68 75 6e 6b 73 20 28 69   large chunks (i
1061a 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 0a  nstances of the.
1061b 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  ** following str
1061c 75 63 74 75 72 65 29 20 74 6f 20 72 65 64 75 63  ucture) to reduc
1061d 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
1061e 69 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20 20 54  ion overhead.  T
1061f 68 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65  he.** chunks are
10620 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65   kept on a linke
10621 64 20 6c 69 73 74 20 73 6f 20 74 68 61 74 20 74  d list so that t
10622 68 65 79 20 63 61 6e 20 62 65 20 64 65 61 6c 6c  hey can be deall
10623 6f 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74  ocated.** when t
10624 68 65 20 52 6f 77 53 65 74 20 69 73 20 64 65 73  he RowSet is des
10625 74 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  troyed..*/.struc
10626 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a  t RowSetChunk {.
10627 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43    struct RowSetC
10628 68 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b  hunk *pNextChunk
10629 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74  ;        /* Next
1062a 20 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f   chunk on list o
1062b 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20  f them all */.  
1062c 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
1062d 72 79 20 61 45 6e 74 72 79 5b 52 4f 57 53 45 54  ry aEntry[ROWSET
1062e 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b  _ENTRY_PER_CHUNK
1062f 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20  ]; /* Allocated 
10630 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f  entries */.};../
10631 2a 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20 69 6e  *.** A RowSet in
10632 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
10633 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
10634 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41  ructure..**.** A
10635 20 74 79 70 65 64 65 66 20 6f 66 20 74 68 69 73   typedef of this
10636 20 73 74 72 75 63 74 75 72 65 20 69 66 20 66 6f   structure if fo
10637 75 6e 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  und in sqliteInt
10638 2e 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f  .h..*/.struct Ro
10639 77 53 65 74 20 7b 0a 20 20 73 74 72 75 63 74 20  wSet {.  struct 
1063a 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68  RowSetChunk *pCh
1063b 75 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20  unk;    /* List 
1063c 6f 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c  of all chunk all
1063d 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71  ocations */.  sq
1063e 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
1063f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10640 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
10641 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72  nection */.  str
10642 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
10643 2a 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c  *pEntry;    /* L
10644 69 73 74 20 6f 66 20 65 6e 74 72 69 65 73 20 75  ist of entries u
10645 73 69 6e 67 20 70 52 69 67 68 74 20 2a 2f 0a 20  sing pRight */. 
10646 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
10647 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 20 20  try *pLast;     
10648 2f 2a 20 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e  /* Last entry on
10649 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69 73 74   the pEntry list
1064a 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77   */.  struct Row
1064b 53 65 74 45 6e 74 72 79 20 2a 70 46 72 65 73 68  SetEntry *pFresh
1064c 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f  ;    /* Source o
1064d 66 20 6e 65 77 20 65 6e 74 72 79 20 6f 62 6a 65  f new entry obje
1064e 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  cts */.  struct 
1064f 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 54 72  RowSetEntry *pTr
10650 65 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e 61 72  ee;     /* Binar
10651 79 20 74 72 65 65 20 6f 66 20 65 6e 74 72 69 65  y tree of entrie
10652 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 73  s */.  u16 nFres
10653 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
10654 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
10655 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46  of objects on pF
10656 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 69 73 53  resh */.  u8 isS
10657 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 20  orted;          
10658 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
10659 20 69 66 20 70 45 6e 74 72 79 20 69 73 20 73 6f   if pEntry is so
1065a 72 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 42 61  rted */.  u8 iBa
1065b 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  tch;            
1065c 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
1065d 65 6e 74 20 69 6e 73 65 72 74 20 62 61 74 63 68  ent insert batch
1065e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75   */.};../*.** Tu
1065f 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69  rn bulk memory i
10660 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f 62 6a  nto a RowSet obj
10661 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20 6f 66  ect.  N bytes of
10662 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61   memory.** are a
10663 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53 70 61  vailable at pSpa
10664 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f 69 6e  ce.  The db poin
10665 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20 61  ter is used as a
10666 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a   memory context.
10667 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 73 65  ** for any subse
10668 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e  quent allocation
10669 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 6f  s that need to o
1066a 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  ccur..** Return 
1066b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1066c 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 6a 65   new RowSet obje
1066d 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73  ct..**.** It mus
1066e 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  t be the case th
1066f 61 74 20 4e 20 69 73 20 73 75 66 66 69 63 69 65  at N is sufficie
10670 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77  nt to make a Row
10671 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20  set.  If not.** 
10672 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75  an assertion fau
10673 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a  lt occurs..** .*
10674 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67 65 72  * If N is larger
10675 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75   than the minimu
10676 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72 70 6c  m, use the surpl
10677 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69 61 6c  us as an initial
10678 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  .** allocation o
10679 66 20 65 6e 74 72 69 65 73 20 61 76 61 69 6c 61  f entries availa
1067a 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c 65 64  ble to be filled
1067b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1067c 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69  ATE RowSet *sqli
1067d 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71  te3RowSetInit(sq
1067e 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
1067f 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67 6e 65  *pSpace, unsigne
10680 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53  d int N){.  RowS
10681 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  et *p;.  assert(
10682 20 4e 20 3e 3d 20 73 69 7a 65 6f 66 28 2a 70 29   N >= sizeof(*p)
10683 20 29 3b 0a 20 20 70 20 3d 20 70 53 70 61 63 65   );.  p = pSpace
10684 3b 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20  ;.  p->pChunk = 
10685 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  0;.  p->db = db;
10686 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30  .  p->pEntry = 0
10687 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30  ;.  p->pLast = 0
10688 3b 0a 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30  ;.  p->pTree = 0
10689 3b 0a 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20  ;.  p->pFresh = 
1068a 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e  (struct RowSetEn
1068b 74 72 79 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d  try*)&p[1];.  p-
1068c 3e 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 28  >nFresh = (u16)(
1068d 28 4e 20 2d 20 73 69 7a 65 6f 66 28 2a 70 29 29  (N - sizeof(*p))
1068e 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 52  /sizeof(struct R
1068f 6f 77 53 65 74 45 6e 74 72 79 29 29 3b 0a 20 20  owSetEntry));.  
10690 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b  p->isSorted = 1;
10691 0a 20 20 70 2d 3e 69 42 61 74 63 68 20 3d 20 30  .  p->iBatch = 0
10692 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
10693 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74  ./*.** Deallocat
10694 65 20 61 6c 6c 20 63 68 75 6e 6b 73 20 66 72 6f  e all chunks fro
10695 6d 20 61 20 52 6f 77 53 65 74 2e 20 20 54 68 69  m a RowSet.  Thi
10696 73 20 66 72 65 65 73 20 61 6c 6c 20 6d 65 6d 6f  s frees all memo
10697 72 79 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 52  ry that.** the R
10698 6f 77 53 65 74 20 68 61 73 20 61 6c 6c 6f 63 61  owSet has alloca
10699 74 65 64 20 6f 76 65 72 20 69 74 73 20 6c 69 66  ted over its lif
1069a 65 74 69 6d 65 2e 20 20 54 68 69 73 20 72 6f 75  etime.  This rou
1069b 74 69 6e 65 20 69 73 0a 2a 2a 20 74 68 65 20 64  tine is.** the d
1069c 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68  estructor for th
1069d 65 20 52 6f 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c  e RowSet..*/.SQL
1069e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1069f 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c   sqlite3RowSetCl
106a0 65 61 72 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a  ear(RowSet *p){.
106a1 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43    struct RowSetC
106a2 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 2c 20 2a 70  hunk *pChunk, *p
106a3 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 66 6f 72  NextChunk;.  for
106a4 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e  (pChunk=p->pChun
106a5 6b 3b 20 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e  k; pChunk; pChun
106a6 6b 20 3d 20 70 4e 65 78 74 43 68 75 6e 6b 29 7b  k = pNextChunk){
106a7 0a 20 20 20 20 70 4e 65 78 74 43 68 75 6e 6b 20  .    pNextChunk 
106a8 3d 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 43  = pChunk->pNextC
106a9 68 75 6e 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65  hunk;.    sqlite
106aa 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70  3DbFree(p->db, p
106ab 43 68 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d  Chunk);.  }.  p-
106ac 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70  >pChunk = 0;.  p
106ad 2d 3e 6e 46 72 65 73 68 20 3d 20 30 3b 0a 20 20  ->nFresh = 0;.  
106ae 70 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20  p->pEntry = 0;. 
106af 20 70 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20   p->pLast = 0;. 
106b0 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20   p->pTree = 0;. 
106b1 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 31   p->isSorted = 1
106b2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72  ;.}../*.** Inser
106b3 74 20 61 20 6e 65 77 20 76 61 6c 75 65 20 69 6e  t a new value in
106b4 74 6f 20 61 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a  to a RowSet..**.
106b5 2a 2a 20 54 68 65 20 6d 61 6c 6c 6f 63 46 61 69  ** The mallocFai
106b6 6c 65 64 20 66 6c 61 67 20 6f 66 20 74 68 65 20  led flag of the 
106b7 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
106b8 69 6f 6e 20 69 73 20 73 65 74 20 69 66 20 61 0a  ion is set if a.
106b9 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** memory alloca
106ba 74 69 6f 6e 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53  tion fails..*/.S
106bb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
106bc 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74  id sqlite3RowSet
106bd 49 6e 73 65 72 74 28 52 6f 77 53 65 74 20 2a 70  Insert(RowSet *p
106be 2c 20 69 36 34 20 72 6f 77 69 64 29 7b 0a 20 20  , i64 rowid){.  
106bf 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
106c0 72 79 20 2a 70 45 6e 74 72 79 3b 20 20 2f 2a 20  ry *pEntry;  /* 
106c1 54 68 65 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f  The new entry */
106c2 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
106c3 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20  Entry *pLast;   
106c4 2f 2a 20 54 68 65 20 6c 61 73 74 20 70 72 69 6f  /* The last prio
106c5 72 20 65 6e 74 72 79 20 2a 2f 0a 20 20 61 73 73  r entry */.  ass
106c6 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69  ert( p!=0 );.  i
106c7 66 28 20 70 2d 3e 6e 46 72 65 73 68 3d 3d 30 20  f( p->nFresh==0 
106c8 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f  ){.    struct Ro
106c9 77 53 65 74 43 68 75 6e 6b 20 2a 70 4e 65 77 3b  wSetChunk *pNew;
106ca 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  .    pNew = sqli
106cb 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
106cc 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e  ->db, sizeof(*pN
106cd 65 77 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  ew));.    if( pN
106ce 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  ew==0 ){.      r
106cf 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
106d0 20 70 4e 65 77 2d 3e 70 4e 65 78 74 43 68 75 6e   pNew->pNextChun
106d1 6b 20 3d 20 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20  k = p->pChunk;. 
106d2 20 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d 20 70     p->pChunk = p
106d3 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 70 46 72 65  New;.    p->pFre
106d4 73 68 20 3d 20 70 4e 65 77 2d 3e 61 45 6e 74 72  sh = pNew->aEntr
106d5 79 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 65 73 68  y;.    p->nFresh
106d6 20 3d 20 52 4f 57 53 45 54 5f 45 4e 54 52 59 5f   = ROWSET_ENTRY_
106d7 50 45 52 5f 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20  PER_CHUNK;.  }. 
106d8 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 46 72   pEntry = p->pFr
106d9 65 73 68 2b 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65  esh++;.  p->nFre
106da 73 68 2d 2d 3b 0a 20 20 70 45 6e 74 72 79 2d 3e  sh--;.  pEntry->
106db 76 20 3d 20 72 6f 77 69 64 3b 0a 20 20 70 45 6e  v = rowid;.  pEn
106dc 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b  try->pRight = 0;
106dd 0a 20 20 70 4c 61 73 74 20 3d 20 70 2d 3e 70 4c  .  pLast = p->pL
106de 61 73 74 3b 0a 20 20 69 66 28 20 70 4c 61 73 74  ast;.  if( pLast
106df 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69   ){.    if( p->i
106e0 73 53 6f 72 74 65 64 20 26 26 20 72 6f 77 69 64  sSorted && rowid
106e1 3c 3d 70 4c 61 73 74 2d 3e 76 20 29 7b 0a 20 20  <=pLast->v ){.  
106e2 20 20 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 20      p->isSorted 
106e3 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = 0;.    }.    p
106e4 4c 61 73 74 2d 3e 70 52 69 67 68 74 20 3d 20 70  Last->pRight = p
106e5 45 6e 74 72 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Entry;.  }else{.
106e6 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
106e7 45 6e 74 72 79 3d 3d 30 20 29 3b 20 2f 2a 20 46  Entry==0 ); /* F
106e8 69 72 65 73 20 69 66 20 49 4e 53 45 52 54 20 61  ires if INSERT a
106e9 66 74 65 72 20 53 4d 41 4c 4c 45 53 54 20 2a 2f  fter SMALLEST */
106ea 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d  .    p->pEntry =
106eb 20 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70   pEntry;.  }.  p
106ec 2d 3e 70 4c 61 73 74 20 3d 20 70 45 6e 74 72 79  ->pLast = pEntry
106ed 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65  ;.}../*.** Merge
106ee 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 52 6f   two lists of Ro
106ef 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74  wSetEntry object
106f0 73 2e 20 20 52 65 6d 6f 76 65 20 64 75 70 6c 69  s.  Remove dupli
106f1 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cates..**.** The
106f2 20 69 6e 70 75 74 20 6c 69 73 74 73 20 61 72 65   input lists are
106f3 20 63 6f 6e 6e 65 63 74 65 64 20 76 69 61 20 70   connected via p
106f4 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 20 61  Right pointers a
106f5 6e 64 20 61 72 65 20 0a 2a 2a 20 61 73 73 75 6d  nd are .** assum
106f6 65 64 20 74 6f 20 65 61 63 68 20 61 6c 72 65 61  ed to each alrea
106f7 64 79 20 62 65 20 69 6e 20 73 6f 72 74 65 64 20  dy be in sorted 
106f8 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  order..*/.static
106f9 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
106fa 74 72 79 20 2a 72 6f 77 53 65 74 4d 65 72 67 65  try *rowSetMerge
106fb 28 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  (.  struct RowSe
106fc 74 45 6e 74 72 79 20 2a 70 41 2c 20 20 20 20 2f  tEntry *pA,    /
106fd 2a 20 46 69 72 73 74 20 73 6f 72 74 65 64 20 6c  * First sorted l
106fe 69 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64  ist to be merged
106ff 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77   */.  struct Row
10700 53 65 74 45 6e 74 72 79 20 2a 70 42 20 20 20 20  SetEntry *pB    
10701 20 2f 2a 20 53 65 63 6f 6e 64 20 73 6f 72 74 65   /* Second sorte
10702 64 20 6c 69 73 74 20 74 6f 20 62 65 20 6d 65 72  d list to be mer
10703 67 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75  ged */.){.  stru
10704 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 68  ct RowSetEntry h
10705 65 61 64 3b 0a 20 20 73 74 72 75 63 74 20 52 6f  ead;.  struct Ro
10706 77 53 65 74 45 6e 74 72 79 20 2a 70 54 61 69 6c  wSetEntry *pTail
10707 3b 0a 0a 20 20 70 54 61 69 6c 20 3d 20 26 68 65  ;..  pTail = &he
10708 61 64 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20  ad;.  while( pA 
10709 26 26 20 70 42 20 29 7b 0a 20 20 20 20 61 73 73  && pB ){.    ass
1070a 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d  ert( pA->pRight=
1070b 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d  =0 || pA->v<=pA-
1070c 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20  >pRight->v );.  
1070d 20 20 61 73 73 65 72 74 28 20 70 42 2d 3e 70 52    assert( pB->pR
1070e 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76  ight==0 || pB->v
1070f 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d 3e 76 20  <=pB->pRight->v 
10710 29 3b 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 76  );.    if( pA->v
10711 3c 70 42 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20  <pB->v ){.      
10712 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20  pTail->pRight = 
10713 70 41 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70  pA;.      pA = p
10714 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20  A->pRight;.     
10715 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c 2d 3e   pTail = pTail->
10716 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c 73  pRight;.    }els
10717 65 20 69 66 28 20 70 42 2d 3e 76 3c 70 41 2d 3e  e if( pB->v<pA->
10718 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c  v ){.      pTail
10719 2d 3e 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20  ->pRight = pB;. 
1071a 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 52       pB = pB->pR
1071b 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 61 69  ight;.      pTai
1071c 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68  l = pTail->pRigh
1071d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  t;.    }else{.  
1071e 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 52 69      pA = pA->pRi
1071f 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ght;.    }.  }. 
10720 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 61   if( pA ){.    a
10721 73 73 65 72 74 28 20 70 41 2d 3e 70 52 69 67 68  ssert( pA->pRigh
10722 74 3d 3d 30 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70  t==0 || pA->v<=p
10723 41 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a  A->pRight->v );.
10724 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68      pTail->pRigh
10725 74 20 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 7b  t = pA;.  }else{
10726 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 3d  .    assert( pB=
10727 3d 30 20 7c 7c 20 70 42 2d 3e 70 52 69 67 68 74  =0 || pB->pRight
10728 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42  ==0 || pB->v<=pB
10729 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20  ->pRight->v );. 
1072a 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74     pTail->pRight
1072b 20 3d 20 70 42 3b 0a 20 20 7d 0a 20 20 72 65 74   = pB;.  }.  ret
1072c 75 72 6e 20 68 65 61 64 2e 70 52 69 67 68 74 3b  urn head.pRight;
1072d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61  .}../*.** Sort a
1072e 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 6e 20 74  ll elements on t
1072f 68 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 6f  he pEntry list o
10730 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e 74  f the RowSet int
10731 6f 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  o ascending orde
10732 72 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f  r..*/ .static vo
10733 69 64 20 72 6f 77 53 65 74 53 6f 72 74 28 52 6f  id rowSetSort(Ro
10734 77 53 65 74 20 2a 70 29 7b 0a 20 20 75 6e 73 69  wSet *p){.  unsi
10735 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 73 74  gned int i;.  st
10736 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
10737 20 2a 70 45 6e 74 72 79 3b 0a 20 20 73 74 72 75   *pEntry;.  stru
10738 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
10739 61 42 75 63 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20  aBucket[40];..  
1073a 61 73 73 65 72 74 28 20 70 2d 3e 69 73 53 6f 72  assert( p->isSor
1073b 74 65 64 3d 3d 30 20 29 3b 0a 20 20 6d 65 6d 73  ted==0 );.  mems
1073c 65 74 28 61 42 75 63 6b 65 74 2c 20 30 2c 20 73  et(aBucket, 0, s
1073d 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 29 3b  izeof(aBucket));
1073e 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 45 6e  .  while( p->pEn
1073f 74 72 79 20 29 7b 0a 20 20 20 20 70 45 6e 74 72  try ){.    pEntr
10740 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 3b 0a 20  y = p->pEntry;. 
10741 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70     p->pEntry = p
10742 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20  Entry->pRight;. 
10743 20 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 68     pEntry->pRigh
10744 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69  t = 0;.    for(i
10745 3d 30 3b 20 61 42 75 63 6b 65 74 5b 69 5d 3b 20  =0; aBucket[i]; 
10746 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 45 6e 74  i++){.      pEnt
10747 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 72 67 65  ry = rowSetMerge
10748 28 61 42 75 63 6b 65 74 5b 69 5d 2c 20 70 45 6e  (aBucket[i], pEn
10749 74 72 79 29 3b 0a 20 20 20 20 20 20 61 42 75 63  try);.      aBuc
1074a 6b 65 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  ket[i] = 0;.    
1074b 7d 0a 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d  }.    aBucket[i]
1074c 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 7d 0a 20   = pEntry;.  }. 
1074d 20 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 66   pEntry = 0;.  f
1074e 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
1074f 28 61 42 75 63 6b 65 74 29 2f 73 69 7a 65 6f 66  (aBucket)/sizeof
10750 28 61 42 75 63 6b 65 74 5b 30 5d 29 3b 20 69 2b  (aBucket[0]); i+
10751 2b 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d  +){.    pEntry =
10752 20 72 6f 77 53 65 74 4d 65 72 67 65 28 70 45 6e   rowSetMerge(pEn
10753 74 72 79 2c 20 61 42 75 63 6b 65 74 5b 69 5d 29  try, aBucket[i])
10754 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 45 6e 74 72  ;.  }.  p->pEntr
10755 79 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 70 2d  y = pEntry;.  p-
10756 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d  >pLast = 0;.  p-
10757 3e 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d  >isSorted = 1;.}
10758 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  .../*.** The inp
10759 75 74 2c 20 70 49 6e 2c 20 69 73 20 61 20 62 69  ut, pIn, is a bi
1075a 6e 61 72 79 20 74 72 65 65 20 28 6f 72 20 73 75  nary tree (or su
1075b 62 74 72 65 65 29 20 6f 66 20 52 6f 77 53 65 74  btree) of RowSet
1075c 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 0a 2a  Entry objects..*
1075d 2a 20 43 6f 6e 76 65 72 74 20 74 68 69 73 20 74  * Convert this t
1075e 72 65 65 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65  ree into a linke
1075f 64 20 6c 69 73 74 20 63 6f 6e 6e 65 63 74 65 64  d list connected
10760 20 62 79 20 74 68 65 20 70 52 69 67 68 74 20 70   by the pRight p
10761 6f 69 6e 74 65 72 73 0a 2a 2a 20 61 6e 64 20 72  ointers.** and r
10762 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 73 20 74  eturn pointers t
10763 6f 20 74 68 65 20 66 69 72 73 74 20 61 6e 64 20  o the first and 
10764 6c 61 73 74 20 65 6c 65 6d 65 6e 74 73 20 6f 66  last elements of
10765 20 74 68 65 20 6e 65 77 20 6c 69 73 74 2e 0a 2a   the new list..*
10766 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f  /.static void ro
10767 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 0a  wSetTreeToList(.
10768 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
10769 6e 74 72 79 20 2a 70 49 6e 2c 20 20 20 20 20 20  ntry *pIn,      
1076a 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 20 74 68     /* Root of th
1076b 65 20 69 6e 70 75 74 20 74 72 65 65 20 2a 2f 0a  e input tree */.
1076c 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
1076d 6e 74 72 79 20 2a 2a 70 70 46 69 72 73 74 2c 20  ntry **ppFirst, 
1076e 20 20 20 2f 2a 20 57 72 69 74 65 20 68 65 61 64     /* Write head
1076f 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 6c   of the output l
10770 69 73 74 20 68 65 72 65 20 2a 2f 0a 20 20 73 74  ist here */.  st
10771 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
10772 20 2a 2a 70 70 4c 61 73 74 20 20 20 20 20 20 2f   **ppLast      /
10773 2a 20 57 72 69 74 65 20 74 61 69 6c 20 6f 66 20  * Write tail of 
10774 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20  the output list 
10775 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73  here */.){.  ass
10776 65 72 74 28 20 70 49 6e 21 3d 30 20 29 3b 0a 20  ert( pIn!=0 );. 
10777 20 69 66 28 20 70 49 6e 2d 3e 70 4c 65 66 74 20   if( pIn->pLeft 
10778 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f  ){.    struct Ro
10779 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20  wSetEntry *p;.  
1077a 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c 69    rowSetTreeToLi
1077b 73 74 28 70 49 6e 2d 3e 70 4c 65 66 74 2c 20 70  st(pIn->pLeft, p
1077c 70 46 69 72 73 74 2c 20 26 70 29 3b 0a 20 20 20  pFirst, &p);.   
1077d 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 70 49 6e   p->pRight = pIn
1077e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
1077f 70 70 46 69 72 73 74 20 3d 20 70 49 6e 3b 0a 20  ppFirst = pIn;. 
10780 20 7d 0a 20 20 69 66 28 20 70 49 6e 2d 3e 70 52   }.  if( pIn->pR
10781 69 67 68 74 20 29 7b 0a 20 20 20 20 72 6f 77 53  ight ){.    rowS
10782 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 49 6e  etTreeToList(pIn
10783 2d 3e 70 52 69 67 68 74 2c 20 26 70 49 6e 2d 3e  ->pRight, &pIn->
10784 70 52 69 67 68 74 2c 20 70 70 4c 61 73 74 29 3b  pRight, ppLast);
10785 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
10786 70 4c 61 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d  pLast = pIn;.  }
10787 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 4c  .  assert( (*ppL
10788 61 73 74 29 2d 3e 70 52 69 67 68 74 3d 3d 30 20  ast)->pRight==0 
10789 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  );.}.../*.** Con
1078a 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69  vert a sorted li
1078b 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 28  st of elements (
1078c 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 52 69  connected by pRi
1078d 67 68 74 29 20 69 6e 74 6f 20 61 20 62 69 6e 61  ght) into a bina
1078e 72 79 0a 2a 2a 20 74 72 65 65 20 77 69 74 68 20  ry.** tree with 
1078f 64 65 70 74 68 20 6f 66 20 69 44 65 70 74 68 2e  depth of iDepth.
10790 20 20 41 20 64 65 70 74 68 20 6f 66 20 31 20 6d    A depth of 1 m
10791 65 61 6e 73 20 74 68 65 20 74 72 65 65 20 63 6f  eans the tree co
10792 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 0a  ntains a single.
10793 2a 2a 20 6e 6f 64 65 20 74 61 6b 65 6e 20 66 72  ** node taken fr
10794 6f 6d 20 74 68 65 20 68 65 61 64 20 6f 66 20 2a  om the head of *
10795 70 70 4c 69 73 74 2e 20 20 41 20 64 65 70 74 68  ppList.  A depth
10796 20 6f 66 20 32 20 6d 65 61 6e 73 20 61 20 74 72   of 2 means a tr
10797 65 65 20 77 69 74 68 0a 2a 2a 20 74 68 72 65 65  ee with.** three
10798 20 6e 6f 64 65 73 2e 20 20 41 6e 64 20 73 6f 20   nodes.  And so 
10799 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65  forth..**.** Use
1079a 20 61 73 20 6d 61 6e 79 20 65 6e 74 72 69 65 73   as many entries
1079b 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74 20   from the input 
1079c 6c 69 73 74 20 61 73 20 72 65 71 75 69 72 65 64  list as required
1079d 20 61 6e 64 20 75 70 64 61 74 65 20 74 68 65 0a   and update the.
1079e 2a 2a 20 2a 70 70 4c 69 73 74 20 74 6f 20 70 6f  ** *ppList to po
1079f 69 6e 74 20 74 6f 20 74 68 65 20 75 6e 75 73 65  int to the unuse
107a0 64 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  d elements of th
107a1 65 20 6c 69 73 74 2e 20 20 49 66 20 74 68 65 20  e list.  If the 
107a2 69 6e 70 75 74 0a 2a 2a 20 6c 69 73 74 20 63 6f  input.** list co
107a3 6e 74 61 69 6e 73 20 74 6f 6f 20 66 65 77 20 65  ntains too few e
107a4 6c 65 6d 65 6e 74 73 2c 20 74 68 65 6e 20 63 6f  lements, then co
107a5 6e 73 74 72 75 63 74 20 61 6e 20 69 6e 63 6f 6d  nstruct an incom
107a6 70 6c 65 74 65 20 74 72 65 65 0a 2a 2a 20 61 6e  plete tree.** an
107a7 64 20 6c 65 61 76 65 20 2a 70 70 4c 69 73 74 20  d leave *ppList 
107a8 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a  set to NULL..**.
107a9 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
107aa 74 65 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20  ter to the root 
107ab 6f 66 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74  of the construct
107ac 65 64 20 62 69 6e 61 72 79 20 74 72 65 65 2e 0a  ed binary tree..
107ad 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
107ae 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f   RowSetEntry *ro
107af 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 0a 20  wSetNDeepTree(. 
107b0 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
107b1 74 72 79 20 2a 2a 70 70 4c 69 73 74 2c 0a 20 20  try **ppList,.  
107b2 69 6e 74 20 69 44 65 70 74 68 0a 29 7b 0a 20 20  int iDepth.){.  
107b3 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
107b4 72 79 20 2a 70 3b 20 20 20 20 20 20 20 20 20 2f  ry *p;         /
107b5 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 6e 65  * Root of the ne
107b6 77 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 72 75  w tree */.  stru
107b7 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
107b8 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65  pLeft;     /* Le
107b9 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 20 20  ft subtree */.  
107ba 69 66 28 20 2a 70 70 4c 69 73 74 3d 3d 30 20 29  if( *ppList==0 )
107bb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
107bc 20 20 7d 0a 20 20 69 66 28 20 69 44 65 70 74 68    }.  if( iDepth
107bd 3d 3d 31 20 29 7b 0a 20 20 20 20 70 20 3d 20 2a  ==1 ){.    p = *
107be 70 70 4c 69 73 74 3b 0a 20 20 20 20 2a 70 70 4c  ppList;.    *ppL
107bf 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b  ist = p->pRight;
107c0 0a 20 20 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20  .    p->pLeft = 
107c1 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20  p->pRight = 0;. 
107c2 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 7d     return p;.  }
107c3 0a 20 20 70 4c 65 66 74 20 3d 20 72 6f 77 53 65  .  pLeft = rowSe
107c4 74 4e 44 65 65 70 54 72 65 65 28 70 70 4c 69 73  tNDeepTree(ppLis
107c5 74 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 20  t, iDepth-1);.  
107c6 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 20 69  p = *ppList;.  i
107c7 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72  f( p==0 ){.    r
107c8 65 74 75 72 6e 20 70 4c 65 66 74 3b 0a 20 20 7d  eturn pLeft;.  }
107c9 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c  .  p->pLeft = pL
107ca 65 66 74 3b 0a 20 20 2a 70 70 4c 69 73 74 20 3d  eft;.  *ppList =
107cb 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d   p->pRight;.  p-
107cc 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53 65 74  >pRight = rowSet
107cd 4e 44 65 65 70 54 72 65 65 28 70 70 4c 69 73 74  NDeepTree(ppList
107ce 2c 20 69 44 65 70 74 68 2d 31 29 3b 0a 20 20 72  , iDepth-1);.  r
107cf 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
107d0 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f 72 74  * Convert a sort
107d1 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 6d 65  ed list of eleme
107d2 6e 74 73 20 69 6e 74 6f 20 61 20 62 69 6e 61 72  nts into a binar
107d3 79 20 74 72 65 65 2e 20 4d 61 6b 65 20 74 68 65  y tree. Make the
107d4 20 74 72 65 65 0a 2a 2a 20 61 73 20 64 65 65 70   tree.** as deep
107d5 20 61 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20   as it needs to 
107d6 62 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63  be in order to c
107d7 6f 6e 74 61 69 6e 20 74 68 65 20 65 6e 74 69 72  ontain the entir
107d8 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  e list..*/.stati
107d9 63 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45  c struct RowSetE
107da 6e 74 72 79 20 2a 72 6f 77 53 65 74 4c 69 73 74  ntry *rowSetList
107db 54 6f 54 72 65 65 28 73 74 72 75 63 74 20 52 6f  ToTree(struct Ro
107dc 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 69 73 74  wSetEntry *pList
107dd 29 7b 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b  ){.  int iDepth;
107de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
107df 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20  pth of the tree 
107e0 73 6f 20 66 61 72 20 2a 2f 0a 20 20 73 74 72 75  so far */.  stru
107e1 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
107e2 70 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72  p;       /* Curr
107e3 65 6e 74 20 74 72 65 65 20 72 6f 6f 74 20 2a 2f  ent tree root */
107e4 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
107e5 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20  Entry *pLeft;   
107e6 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20  /* Left subtree 
107e7 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4c  */..  assert( pL
107e8 69 73 74 21 3d 30 20 29 3b 0a 20 20 70 20 3d 20  ist!=0 );.  p = 
107e9 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74 20 3d  pList;.  pList =
107ea 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 70 2d   p->pRight;.  p-
107eb 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67  >pLeft = p->pRig
107ec 68 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 44  ht = 0;.  for(iD
107ed 65 70 74 68 3d 31 3b 20 70 4c 69 73 74 3b 20 69  epth=1; pList; i
107ee 44 65 70 74 68 2b 2b 29 7b 0a 20 20 20 20 70 4c  Depth++){.    pL
107ef 65 66 74 20 3d 20 70 3b 0a 20 20 20 20 70 20 3d  eft = p;.    p =
107f0 20 70 4c 69 73 74 3b 0a 20 20 20 20 70 4c 69 73   pList;.    pLis
107f1 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20  t = p->pRight;. 
107f2 20 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 4c     p->pLeft = pL
107f3 65 66 74 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67  eft;.    p->pRig
107f4 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70  ht = rowSetNDeep
107f5 54 72 65 65 28 26 70 4c 69 73 74 2c 20 69 44 65  Tree(&pList, iDe
107f6 70 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  pth);.  }.  retu
107f7 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rn p;.}../*.** C
107f8 6f 6e 76 65 72 74 20 74 68 65 20 6c 69 73 74 20  onvert the list 
107f9 69 6e 20 70 2d 3e 70 45 6e 74 72 79 20 69 6e 74  in p->pEntry int
107fa 6f 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20  o a sorted list 
107fb 69 66 20 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20  if it is not.** 
107fc 73 6f 72 74 65 64 20 61 6c 72 65 61 64 79 2e 20  sorted already. 
107fd 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 62   If there is a b
107fe 69 6e 61 72 79 20 74 72 65 65 20 6f 6e 20 70 2d  inary tree on p-
107ff 3e 70 54 72 65 65 2c 20 74 68 65 6e 0a 2a 2a 20  >pTree, then.** 
10800 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20  convert it into 
10801 61 20 6c 69 73 74 20 74 6f 6f 20 61 6e 64 20 6d  a list too and m
10802 65 72 67 65 20 69 74 20 69 6e 74 6f 20 74 68 65  erge it into the
10803 20 70 2d 3e 70 45 6e 74 72 79 20 6c 69 73 74 2e   p->pEntry list.
10804 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10805 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 52 6f 77  rowSetToList(Row
10806 53 65 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 21  Set *p){.  if( !
10807 70 2d 3e 69 73 53 6f 72 74 65 64 20 29 7b 0a 20  p->isSorted ){. 
10808 20 20 20 72 6f 77 53 65 74 53 6f 72 74 28 70 29     rowSetSort(p)
10809 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
1080a 54 72 65 65 20 29 7b 0a 20 20 20 20 73 74 72 75  Tree ){.    stru
1080b 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
1080c 70 48 65 61 64 2c 20 2a 70 54 61 69 6c 3b 0a 20  pHead, *pTail;. 
1080d 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c     rowSetTreeToL
1080e 69 73 74 28 70 2d 3e 70 54 72 65 65 2c 20 26 70  ist(p->pTree, &p
1080f 48 65 61 64 2c 20 26 70 54 61 69 6c 29 3b 0a 20  Head, &pTail);. 
10810 20 20 20 70 2d 3e 70 54 72 65 65 20 3d 20 30 3b     p->pTree = 0;
10811 0a 20 20 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d  .    p->pEntry =
10812 20 72 6f 77 53 65 74 4d 65 72 67 65 28 70 2d 3e   rowSetMerge(p->
10813 70 45 6e 74 72 79 2c 20 70 48 65 61 64 29 3b 0a  pEntry, pHead);.
10814 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74    }.}../*.** Ext
10815 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65 73  ract the smalles
10816 74 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d 20 74  t element from t
10817 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 57 72  he RowSet..** Wr
10818 69 74 65 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  ite the element 
10819 69 6e 74 6f 20 2a 70 52 6f 77 69 64 2e 20 20 52  into *pRowid.  R
1081a 65 74 75 72 6e 20 31 20 6f 6e 20 73 75 63 63 65  eturn 1 on succe
1081b 73 73 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 30  ss.  Return.** 0
1081c 20 69 66 20 74 68 65 20 52 6f 77 53 65 74 20 69   if the RowSet i
1081d 73 20 61 6c 72 65 61 64 79 20 65 6d 70 74 79 2e  s already empty.
1081e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69  .**.** After thi
1081f 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65  s routine has be
10820 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 73  en called, the s
10821 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65  qlite3RowSetInse
10822 72 74 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  rt().** routine 
10823 6d 61 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65  may not be calle
10824 64 20 61 67 61 69 6e 2e 20 20 0a 2a 2f 0a 53 51  d again.  .*/.SQ
10825 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10826 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65   sqlite3RowSetNe
10827 78 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36  xt(RowSet *p, i6
10828 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20 72 6f  4 *pRowid){.  ro
10829 77 53 65 74 54 6f 4c 69 73 74 28 70 29 3b 0a 20  wSetToList(p);. 
1082a 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 20 29   if( p->pEntry )
1082b 7b 0a 20 20 20 20 2a 70 52 6f 77 69 64 20 3d 20  {.    *pRowid = 
1082c 70 2d 3e 70 45 6e 74 72 79 2d 3e 76 3b 0a 20 20  p->pEntry->v;.  
1082d 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 2d    p->pEntry = p-
1082e 3e 70 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 3b  >pEntry->pRight;
1082f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 45 6e 74  .    if( p->pEnt
10830 72 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ry==0 ){.      s
10831 71 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61  qlite3RowSetClea
10832 72 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  r(p);.    }.    
10833 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73  return 1;.  }els
10834 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  e{.    return 0;
10835 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  .  }.}../*.** Ch
10836 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 65 6c  eck to see if el
10837 65 6d 65 6e 74 20 69 52 6f 77 69 64 20 77 61 73  ement iRowid was
10838 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
10839 68 65 20 74 68 65 20 72 6f 77 73 65 74 20 61 73  he the rowset as
1083a 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e 79 20  .** part of any 
1083b 69 6e 73 65 72 74 20 62 61 74 63 68 20 70 72 69  insert batch pri
1083c 6f 72 20 74 6f 20 69 42 61 74 63 68 2e 20 20 52  or to iBatch.  R
1083d 65 74 75 72 6e 20 31 20 6f 72 20 30 2e 0a 2a 2f  eturn 1 or 0..*/
1083e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1083f 69 6e 74 20 73 71 6c 69 74 65 33 52 6f 77 53 65  int sqlite3RowSe
10840 74 54 65 73 74 28 52 6f 77 53 65 74 20 2a 70 52  tTest(RowSet *pR
10841 6f 77 53 65 74 2c 20 75 38 20 69 42 61 74 63 68  owSet, u8 iBatch
10842 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
10843 69 52 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 63  iRowid){.  struc
10844 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
10845 3b 0a 20 20 69 66 28 20 69 42 61 74 63 68 21 3d  ;.  if( iBatch!=
10846 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 20  pRowSet->iBatch 
10847 29 7b 0a 20 20 20 20 69 66 28 20 70 52 6f 77 53  ){.    if( pRowS
10848 65 74 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20  et->pEntry ){.  
10849 20 20 20 20 72 6f 77 53 65 74 54 6f 4c 69 73 74      rowSetToList
1084a 28 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 20  (pRowSet);.     
1084b 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 20   pRowSet->pTree 
1084c 3d 20 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72  = rowSetListToTr
1084d 65 65 28 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74  ee(pRowSet->pEnt
1084e 72 79 29 3b 0a 20 20 20 20 20 20 70 52 6f 77 53  ry);.      pRowS
1084f 65 74 2d 3e 70 45 6e 74 72 79 20 3d 20 30 3b 0a  et->pEntry = 0;.
10850 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 70        pRowSet->p
10851 4c 61 73 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  Last = 0;.    }.
10852 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 69 42 61      pRowSet->iBa
10853 74 63 68 20 3d 20 69 42 61 74 63 68 3b 0a 20 20  tch = iBatch;.  
10854 7d 0a 20 20 70 20 3d 20 70 52 6f 77 53 65 74 2d  }.  p = pRowSet-
10855 3e 70 54 72 65 65 3b 0a 20 20 77 68 69 6c 65 28  >pTree;.  while(
10856 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d   p ){.    if( p-
10857 3e 76 3c 69 52 6f 77 69 64 20 29 7b 0a 20 20 20  >v<iRowid ){.   
10858 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67 68 74     p = p->pRight
10859 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1085a 70 2d 3e 76 3e 69 52 6f 77 69 64 20 29 7b 0a 20  p->v>iRowid ){. 
1085b 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66       p = p->pLef
1085c 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  t;.    }else{.  
1085d 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1085e 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
1085f 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   0;.}../********
10860 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 6f  ****** End of ro
10861 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  wset.c *********
10862 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10863 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10864 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
10865 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
10866 65 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a  e pager.c ******
10867 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10868 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10869 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1086a 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
1086b 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1086c 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1086d 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1086e 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1086f 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
10870 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
10871 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
10872 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
10873 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
10874 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
10875 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
10876 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
10877 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
10878 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
10879 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1087a 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1087b 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1087c 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1087d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1087e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1087f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
10881 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70   This is the imp
10882 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10883 68 65 20 70 61 67 65 20 63 61 63 68 65 20 73 75  he page cache su
10884 62 73 79 73 74 65 6d 20 6f 72 20 22 70 61 67 65  bsystem or "page
10885 72 22 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 70  r"..** .** The p
10886 61 67 65 72 20 69 73 20 75 73 65 64 20 74 6f 20  ager is used to 
10887 61 63 63 65 73 73 20 61 20 64 61 74 61 62 61 73  access a databas
10888 65 20 64 69 73 6b 20 66 69 6c 65 2e 20 20 49 74  e disk file.  It
10889 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61   implements.** a
1088a 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 20 61 6e 64  tomic commit and
1088b 20 72 6f 6c 6c 62 61 63 6b 20 74 68 72 6f 75 67   rollback throug
1088c 68 20 74 68 65 20 75 73 65 20 6f 66 20 61 20 6a  h the use of a j
1088d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74  ournal file that
1088e 0a 2a 2a 20 69 73 20 73 65 70 61 72 61 74 65 20  .** is separate 
1088f 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
10890 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 67  e file.  The pag
10891 65 72 20 61 6c 73 6f 20 69 6d 70 6c 65 6d 65 6e  er also implemen
10892 74 73 20 66 69 6c 65 0a 2a 2a 20 6c 6f 63 6b 69  ts file.** locki
10893 6e 67 20 74 6f 20 70 72 65 76 65 6e 74 20 74 77  ng to prevent tw
10894 6f 20 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d  o processes from
10895 20 77 72 69 74 69 6e 67 20 74 68 65 20 73 61 6d   writing the sam
10896 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  e database.** fi
10897 6c 65 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c  le simultaneousl
10898 79 2c 20 6f 72 20 6f 6e 65 20 70 72 6f 63 65 73  y, or one proces
10899 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 74  s from reading t
1089a 68 65 20 64 61 74 61 62 61 73 65 20 77 68 69 6c  he database whil
1089b 65 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 69 73 20  e.** another is 
1089c 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 40  writing..**.** @
1089d 28 23 29 20 24 49 64 3a 20 70 61 67 65 72 2e 63  (#) $Id: pager.c
1089e 2c 76 20 31 2e 35 38 36 20 32 30 30 39 2f 30 35  ,v 1.586 2009/05
1089f 2f 30 36 20 31 38 3a 35 37 3a 31 30 20 73 68 61  /06 18:57:10 sha
108a0 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  ne Exp $.*/.#ifn
108a1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
108a2 44 49 53 4b 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61  DISKIO../*.** Ma
108a3 63 72 6f 73 20 66 6f 72 20 74 72 6f 75 62 6c 65  cros for trouble
108a4 73 68 6f 6f 74 69 6e 67 2e 20 20 4e 6f 72 6d 61  shooting.  Norma
108a5 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a  lly turned off.*
108a6 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69  /.#if 0.int sqli
108a7 74 65 33 50 61 67 65 72 54 72 61 63 65 3d 31 3b  te3PagerTrace=1;
108a8 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61    /* True to ena
108a9 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23  ble tracing */.#
108aa 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65  define sqlite3De
108ab 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74 66  bugPrintf printf
108ac 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52  .#define PAGERTR
108ad 41 43 45 28 58 29 20 20 20 20 20 69 66 28 20 73  ACE(X)     if( s
108ae 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 63 65  qlite3PagerTrace
108af 20 29 7b 20 73 71 6c 69 74 65 33 44 65 62 75 67   ){ sqlite3Debug
108b0 50 72 69 6e 74 66 20 58 3b 20 7d 0a 23 65 6c 73  Printf X; }.#els
108b1 65 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54  e.#define PAGERT
108b2 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a  RACE(X).#endif..
108b3 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
108b4 69 6e 67 20 74 77 6f 20 6d 61 63 72 6f 73 20 61  ing two macros a
108b5 72 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 74  re used within t
108b6 68 65 20 50 41 47 45 52 54 52 41 43 45 28 29 20  he PAGERTRACE() 
108b7 6d 61 63 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20  macros above.** 
108b8 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 66 69 6c  to print out fil
108b9 65 2d 64 65 73 63 72 69 70 74 6f 72 73 2e 20 0a  e-descriptors. .
108ba 2a 2a 0a 2a 2a 20 50 41 47 45 52 49 44 28 29 20  **.** PAGERID() 
108bb 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20  takes a pointer 
108bc 74 6f 20 61 20 50 61 67 65 72 20 73 74 72 75 63  to a Pager struc
108bd 74 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e  t as its argumen
108be 74 2e 20 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69  t. The.** associ
108bf 61 74 65 64 20 66 69 6c 65 2d 64 65 73 63 72 69  ated file-descri
108c0 70 74 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  ptor is returned
108c1 2e 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 29  . FILEHANDLEID()
108c2 20 74 61 6b 65 73 20 61 6e 20 73 71 6c 69 74 65   takes an sqlite
108c3 33 5f 66 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74  3_file.** struct
108c4 20 61 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74   as its argument
108c5 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  ..*/.#define PAG
108c6 45 52 49 44 28 70 29 20 28 28 69 6e 74 29 28 70  ERID(p) ((int)(p
108c7 2d 3e 66 64 29 29 0a 23 64 65 66 69 6e 65 20 46  ->fd)).#define F
108c8 49 4c 45 48 41 4e 44 4c 45 49 44 28 66 64 29 20  ILEHANDLEID(fd) 
108c9 28 28 69 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a  ((int)fd)../*.**
108ca 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
108cb 61 73 20 61 20 77 68 6f 6c 65 20 69 73 20 61 6c  as a whole is al
108cc 77 61 79 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74  ways in one of t
108cd 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
108ce 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
108cf 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20  PAGER_UNLOCK    
108d0 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63      The page cac
108d1 68 65 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  he is not curren
108d2 74 6c 79 20 72 65 61 64 69 6e 67 20 6f 72 20 0a  tly reading or .
108d3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
108d4 20 20 20 20 20 20 20 20 20 77 72 69 74 69 6e 67           writing
108d5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
108d6 6c 65 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f  le.  There is no
108d7 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
108d8 20 20 20 20 20 20 20 20 20 20 64 61 74 61 20 68            data h
108d9 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20  eld in memory.  
108da 54 68 69 73 20 69 73 20 74 68 65 20 69 6e 69 74  This is the init
108db 69 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ial.**          
108dc 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
108dd 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  te..**.**   PAGE
108de 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 20 20  R_SHARED        
108df 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69  The page cache i
108e0 73 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61  s reading the da
108e1 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20  tabase..**      
108e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108e3 20 57 72 69 74 69 6e 67 20 69 73 20 6e 6f 74 20   Writing is not 
108e4 70 65 72 6d 69 74 74 65 64 2e 20 20 54 68 65 72  permitted.  Ther
108e5 65 20 63 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20  e can be.**     
108e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108e7 20 20 6d 75 6c 74 69 70 6c 65 20 72 65 61 64 65    multiple reade
108e8 72 73 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  rs accessing the
108e9 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a   same database.*
108ea 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
108eb 20 20 20 20 20 20 20 20 66 69 6c 65 20 61 74 20          file at 
108ec 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a  the same time..*
108ed 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 45 53  *.**   PAGER_RES
108ee 45 52 56 45 44 20 20 20 20 20 20 54 68 69 73 20  ERVED      This 
108ef 70 72 6f 63 65 73 73 20 68 61 73 20 72 65 73 65  process has rese
108f0 72 76 65 64 20 74 68 65 20 64 61 74 61 62 61 73  rved the databas
108f1 65 20 66 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a  e for writing.**
108f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108f3 20 20 20 20 20 20 20 62 75 74 20 68 61 73 20 6e         but has n
108f4 6f 74 20 79 65 74 20 6d 61 64 65 20 61 6e 79 20  ot yet made any 
108f5 63 68 61 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f  changes.  Only o
108f6 6e 65 20 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20  ne process.**   
108f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
108f8 20 20 20 20 61 74 20 61 20 74 69 6d 65 20 63 61      at a time ca
108f9 6e 20 72 65 73 65 72 76 65 20 74 68 65 20 64 61  n reserve the da
108fa 74 61 62 61 73 65 2e 20 20 54 68 65 20 6f 72 69  tabase.  The ori
108fb 67 69 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20  ginal.**        
108fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
108fd 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
108fe 20 6e 6f 74 20 62 65 65 6e 20 6d 6f 64 69 66 69   not been modifi
108ff 65 64 20 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20  ed so other.**  
10900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10901 20 20 20 20 20 70 72 6f 63 65 73 73 65 73 20 6d       processes m
10902 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61 64  ay still be read
10903 69 6e 67 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a  ing the on-disk.
10904 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
10905 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73           databas
10906 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  e file..**.**   
10907 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
10908 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 63      The page cac
10909 68 65 20 69 73 20 77 72 69 74 69 6e 67 20 74 68  he is writing th
1090a 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20  e database..**  
1090b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1090c 20 20 20 20 20 41 63 63 65 73 73 20 69 73 20 65       Access is e
1090d 78 63 6c 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74  xclusive.  No ot
1090e 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6f 72  her processes or
1090f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
10910 20 20 20 20 20 20 20 20 20 20 74 68 72 65 61 64            thread
10911 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67  s can be reading
10912 20 6f 72 20 77 72 69 74 69 6e 67 20 77 68 69 6c   or writing whil
10913 65 20 6f 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20  e one.**        
10914 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
10915 72 6f 63 65 73 73 20 69 73 20 77 72 69 74 69 6e  rocess is writin
10916 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52  g..**.**   PAGER
10917 5f 53 59 4e 43 45 44 20 20 20 20 20 20 20 20 54  _SYNCED        T
10918 68 65 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74  he pager moves t
10919 6f 20 74 68 69 73 20 73 74 61 74 65 20 66 72 6f  o this state fro
1091a 6d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  m PAGER_EXCLUSIV
1091b 45 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  E.**            
1091c 20 20 20 20 20 20 20 20 20 20 20 61 66 74 65 72             after
1091d 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
1091e 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74   have been writt
1091f 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20  en to the.**    
10920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10921 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65     database file
10922 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61   and the file ha
10923 73 20 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f  s been synced to
10924 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
10925 20 20 20 20 20 20 20 20 20 20 64 69 73 6b 2e 20            disk. 
10926 41 6c 6c 20 74 68 61 74 20 72 65 6d 61 69 6e 73  All that remains
10927 20 74 6f 20 64 6f 20 69 73 20 74 6f 20 72 65 6d   to do is to rem
10928 6f 76 65 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  ove or.**       
10929 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1092a 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f 75  truncate the jou
1092b 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 74 68  rnal file and th
1092c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a  e transaction .*
1092d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
1092e 20 20 20 20 20 20 20 20 77 69 6c 6c 20 62 65 20          will be 
1092f 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a  committed..**.**
10930 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
10931 63 6f 6d 65 73 20 75 70 20 69 6e 20 50 41 47 45  comes up in PAGE
10932 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66  R_UNLOCK.  The f
10933 69 72 73 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73  irst time a.** s
10934 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
10935 20 6f 63 63 75 72 73 2c 20 74 68 65 20 73 74 61   occurs, the sta
10936 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74  te transitions t
10937 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2e 0a  o PAGER_SHARED..
10938 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 70 61 67  ** After all pag
10939 65 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c  es have been rel
1093a 65 61 73 65 64 20 75 73 69 6e 67 20 73 71 6c 69  eased using sqli
1093b 74 65 5f 70 61 67 65 5f 75 6e 72 65 66 28 29 2c  te_page_unref(),
1093c 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 74 72  .** the state tr
1093d 61 6e 73 69 74 69 6f 6e 73 20 62 61 63 6b 20 74  ansitions back t
1093e 6f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20  o PAGER_UNLOCK. 
1093f 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a   The first time.
10940 2a 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 50  ** that sqlite3P
10941 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63  agerWrite() is c
10942 61 6c 6c 65 64 2c 20 74 68 65 20 73 74 61 74 65  alled, the state
10943 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a   transitions to.
10944 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45  ** PAGER_RESERVE
10945 44 2e 20 20 28 4e 6f 74 65 20 74 68 61 74 20 73  D.  (Note that s
10946 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
10947 28 29 20 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a  () can only be.*
10948 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f  * called on an o
10949 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20  utstanding page 
1094a 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74  which means that
1094b 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 0a   the pager must.
1094c 2a 2a 20 62 65 20 69 6e 20 50 41 47 45 52 5f 53  ** be in PAGER_S
1094d 48 41 52 45 44 20 62 65 66 6f 72 65 20 69 74 20  HARED before it 
1094e 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50  transitions to P
1094f 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 29 0a  AGER_RESERVED.).
10950 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45  ** PAGER_RESERVE
10951 44 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  D means that the
10952 72 65 20 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f  re is an open ro
10953 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a  llback journal..
10954 2a 2a 20 54 68 65 20 74 72 61 6e 73 69 74 69 6f  ** The transitio
10955 6e 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55  n to PAGER_EXCLU
10956 53 49 56 45 20 6f 63 63 75 72 73 20 62 65 66 6f  SIVE occurs befo
10957 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a  re any changes.*
10958 2a 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68  * are made to th
10959 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
1095a 20 74 68 6f 75 67 68 20 77 72 69 74 65 73 20 74   though writes t
1095b 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a  o the rollback.*
1095c 2a 20 6a 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73  * journal occurs
1095d 20 77 69 74 68 20 6a 75 73 74 20 50 41 47 45 52   with just PAGER
1095e 5f 52 45 53 45 52 56 45 44 2e 20 20 41 66 74 65  _RESERVED.  Afte
1095f 72 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65  r an sqlite3Page
10960 72 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f  rRollback().** o
10961 72 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  r sqlite3PagerCo
10962 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 2c 20  mmitPhaseTwo(), 
10963 74 68 65 20 73 74 61 74 65 20 63 61 6e 20 67 6f  the state can go
10964 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 53   back to PAGER_S
10965 48 41 52 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20  HARED,.** or it 
10966 63 61 6e 20 73 74 61 79 20 61 74 20 50 41 47 45  can stay at PAGE
10967 52 5f 45 58 43 4c 55 53 49 56 45 20 69 66 20 77  R_EXCLUSIVE if w
10968 65 20 61 72 65 20 69 6e 20 65 78 63 6c 75 73 69  e are in exclusi
10969 76 65 20 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a  ve access mode..
1096a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
1096b 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23  _UNLOCK      0.#
1096c 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 48 41  define PAGER_SHA
1096d 52 45 44 20 20 20 20 20 20 31 20 20 20 2f 2a 20  RED      1   /* 
1096e 73 61 6d 65 20 61 73 20 53 48 41 52 45 44 5f 4c  same as SHARED_L
1096f 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  OCK */.#define P
10970 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20  AGER_RESERVED   
10971 20 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20   2   /* same as 
10972 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f  RESERVED_LOCK */
10973 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 45  .#define PAGER_E
10974 58 43 4c 55 53 49 56 45 20 20 20 34 20 20 20 2f  XCLUSIVE   4   /
10975 2a 20 73 61 6d 65 20 61 73 20 45 58 43 4c 55 53  * same as EXCLUS
10976 49 56 45 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66  IVE_LOCK */.#def
10977 69 6e 65 20 50 41 47 45 52 5f 53 59 4e 43 45 44  ine PAGER_SYNCED
10978 20 20 20 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41        5../*.** A
10979 20 6d 61 63 72 6f 20 75 73 65 64 20 66 6f 72 20   macro used for 
1097a 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 64  invoking the cod
1097b 65 63 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ec if there is o
1097c 6e 65 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ne.*/.#ifdef SQL
1097d 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 23 20  ITE_HAS_CODEC.# 
1097e 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c  define CODEC1(P,
1097f 44 2c 4e 2c 58 29 20 69 66 28 20 50 2d 3e 78 43  D,N,X) if( P->xC
10980 6f 64 65 63 21 3d 30 20 29 7b 20 50 2d 3e 78 43  odec!=0 ){ P->xC
10981 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72  odec(P->pCodecAr
10982 67 2c 44 2c 4e 2c 58 29 3b 20 7d 0a 23 20 64 65  g,D,N,X); }.# de
10983 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c  fine CODEC2(P,D,
10984 4e 2c 58 29 20 28 28 63 68 61 72 2a 29 28 50 2d  N,X) ((char*)(P-
10985 3e 78 43 6f 64 65 63 21 3d 30 3f 50 2d 3e 78 43  >xCodec!=0?P->xC
10986 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 41 72  odec(P->pCodecAr
10987 67 2c 44 2c 4e 2c 58 29 3a 44 29 29 0a 23 65 6c  g,D,N,X):D)).#el
10988 73 65 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45  se.# define CODE
10989 43 31 28 50 2c 44 2c 4e 2c 58 29 20 2f 2a 20 4e  C1(P,D,N,X) /* N
1098a 4f 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65  O-OP */.# define
1098b 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 29   CODEC2(P,D,N,X)
1098c 20 28 28 63 68 61 72 2a 29 44 29 0a 23 65 6e 64   ((char*)D).#end
1098d 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
1098e 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65  ximum allowed se
1098f 63 74 6f 72 20 73 69 7a 65 2e 20 31 36 4d 42 2e  ctor size. 16MB.
10990 20 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 73   If the xSectors
10991 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a  ize() method .**
10992 20 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65   returns a value
10993 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69   larger than thi
10994 73 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54  s, then MAX_SECT
10995 4f 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20  OR_SIZE is used 
10996 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73  instead..** This
10997 20 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62   could conceivab
10998 6c 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 74  ly cause corrupt
10999 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  ion following a 
1099a 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e  power failure on
1099b 0a 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 65  .** such a syste
1099c 6d 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 65  m. This is curre
1099d 6e 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65  ntly an undocume
1099e 6e 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23  nted limit..*/.#
1099f 64 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f  define MAX_SECTO
109a0 52 5f 53 49 5a 45 20 30 78 30 31 30 30 30 30 30  R_SIZE 0x0100000
109a1 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
109a2 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
109a3 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69  wing structure i
109a4 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
109a5 65 61 63 68 20 61 63 74 69 76 65 0a 2a 2a 20 73  each active.** s
109a6 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61  avepoint and sta
109a7 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
109a8 6f 6e 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d  on in the system
109a9 2e 20 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63  . All such struc
109aa 74 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f  tures.** are sto
109ab 72 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72  red in the Pager
109ac 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72  .aSavepoint[] ar
109ad 72 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c  ray, which is al
109ae 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72  located and.** r
109af 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c  esized using sql
109b0 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a  ite3Realloc()..*
109b1 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 76 65  *.** When a save
109b2 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74 65 64  point is created
109b3 2c 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70  , the PagerSavep
109b4 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20  oint.iHdrOffset 
109b5 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20  field is.** set 
109b6 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e  to 0. If a journ
109b7 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69  al-header is wri
109b8 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61  tten into the ma
109b9 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65  in journal while
109ba 0a 2a 2a 20 74 68 65 20 73 61 76 65 70 6f 69 6e  .** the savepoin
109bb 74 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 65  t is active, the
109bc 6e 20 69 48 64 72 4f 66 66 73 65 74 20 69 73 20  n iHdrOffset is 
109bd 73 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20  set to the byte 
109be 6f 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64  offset .** immed
109bf 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67  iately following
109c0 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61   the last journa
109c1 6c 20 72 65 63 6f 72 64 20 77 72 69 74 74 65 6e  l record written
109c2 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a   into the main.*
109c3 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65  * journal before
109c4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61   the journal-hea
109c5 64 65 72 2e 20 54 68 69 73 20 69 73 20 72 65 71  der. This is req
109c6 75 69 72 65 64 20 64 75 72 69 6e 67 20 73 61 76  uired during sav
109c7 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61  epoint.** rollba
109c8 63 6b 20 28 73 65 65 20 70 61 67 65 72 50 6c 61  ck (see pagerPla
109c9 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 29  ybackSavepoint()
109ca 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  )..*/.typedef st
109cb 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f  ruct PagerSavepo
109cc 69 6e 74 20 50 61 67 65 72 53 61 76 65 70 6f 69  int PagerSavepoi
109cd 6e 74 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72  nt;.struct Pager
109ce 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36  Savepoint {.  i6
109cf 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20  4 iOffset;      
109d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
109d1 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 69 6e  arting offset in
109d2 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f   main journal */
109d3 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 73 65  .  i64 iHdrOffse
109d4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
109d5 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a  /* See above */.
109d6 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53 61 76    Bitvec *pInSav
109d7 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f  epoint;        /
109d8 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 69  * Set of pages i
109d9 6e 20 74 68 69 73 20 73 61 76 65 70 6f 69 6e 74  n this savepoint
109da 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67   */.  Pgno nOrig
109db 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
109dc 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e     /* Original n
109dd 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
109de 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  n file */.  Pgno
109df 20 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20   iSubRec;       
109e0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
109e1 78 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72  x of first recor
109e2 64 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  d in sub-journal
109e3 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
109e4 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20  open page cache 
109e5 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
109e6 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
109e7 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
109e8 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20   errCode.**.**  
109e9 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 6d   Pager.errCode m
109ea 61 79 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c  ay be set to SQL
109eb 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54  ITE_IOERR, SQLIT
109ec 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a  E_CORRUPT, or.**
109ed 20 20 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c     or SQLITE_FUL
109ee 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74  L. Once one of t
109ef 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 65  he first three e
109f0 72 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 69 74  rrors occurs, it
109f1 20 70 65 72 73 69 73 74 73 0a 2a 2a 20 20 20 61   persists.**   a
109f2 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  nd is returned a
109f3 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  s the result of 
109f4 65 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 67 65  every major page
109f5 72 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 68 65  r API call.  The
109f6 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c  .**   SQLITE_FUL
109f7 4c 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73  L return code is
109f8 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72   slightly differ
109f9 65 6e 74 2e 20 49 74 20 70 65 72 73 69 73 74 73  ent. It persists
109fa 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a   only until the.
109fb 2a 2a 20 20 20 6e 65 78 74 20 73 75 63 63 65 73  **   next succes
109fc 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 69 73  sful rollback is
109fd 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68   performed on th
109fe 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 41  e pager cache. A
109ff 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49 54 45  lso,.**   SQLITE
10a00 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61  _FULL does not a
10a01 66 66 65 63 74 20 74 68 65 20 73 71 6c 69 74 65  ffect the sqlite
10a02 33 50 61 67 65 72 47 65 74 28 29 20 61 6e 64 20  3PagerGet() and 
10a03 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b  sqlite3PagerLook
10a04 75 70 28 29 0a 2a 2a 20 20 20 41 50 49 73 2c 20  up().**   APIs, 
10a05 74 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20 62  they may still b
10a06 65 20 75 73 65 64 20 73 75 63 63 65 73 73 66 75  e used successfu
10a07 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a  lly..**.** dbSiz
10a08 65 56 61 6c 69 64 2c 20 64 62 53 69 7a 65 2c 20  eValid, dbSize, 
10a09 64 62 4f 72 69 67 53 69 7a 65 2c 20 64 62 46 69  dbOrigSize, dbFi
10a0a 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d  leSize.**.**   M
10a0b 61 6e 61 67 69 6e 67 20 74 68 65 20 73 69 7a 65  anaging the size
10a0c 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
10a0d 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 69   file in pages i
10a0e 73 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c  s a little compl
10a0f 69 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54 68 65  icated..**   The
10a10 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e   variable Pager.
10a11 64 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20  dbSize contains 
10a12 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
10a13 67 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74  ges that the dat
10a14 61 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61 67 65  abase.**   image
10a15 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
10a16 69 6e 73 2e 20 41 73 20 74 68 65 20 64 61 74 61  ins. As the data
10a17 62 61 73 65 20 69 6d 61 67 65 20 67 72 6f 77 73  base image grows
10a18 20 6f 72 20 73 68 72 69 6e 6b 73 20 74 68 69 73   or shrinks this
10a19 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20 69  .**   variable i
10a1a 73 20 75 70 64 61 74 65 64 2e 20 54 68 65 20 76  s updated. The v
10a1b 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62  ariable Pager.db
10a1c 46 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 69 6e  FileSize contain
10a1d 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20  s the number.** 
10a1e 20 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68    of pages in th
10a1f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
10a20 20 54 68 69 73 20 6d 61 79 20 62 65 20 64 69 66   This may be dif
10a21 66 65 72 65 6e 74 20 66 72 6f 6d 20 50 61 67 65  ferent from Page
10a22 72 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20 69 66  r.dbSize.**   if
10a23 20 73 6f 6d 65 20 70 61 67 65 73 20 68 61 76 65   some pages have
10a24 20 62 65 65 6e 20 61 70 70 65 6e 64 65 64 20 74   been appended t
10a25 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  o the database i
10a26 6d 61 67 65 20 62 75 74 20 6e 6f 74 20 79 65 74  mage but not yet
10a27 20 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 6f 75   written.**   ou
10a28 74 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65  t from the cache
10a29 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66   to the actual f
10a2a 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f 72 20  ile on disk. Or 
10a2b 69 66 20 74 68 65 20 69 6d 61 67 65 20 68 61 73  if the image has
10a2c 20 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75 6e 63   been.**   trunc
10a2d 61 74 65 64 20 62 79 20 61 6e 20 69 6e 63 72 65  ated by an incre
10a2e 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 6f 70  mental-vacuum op
10a2f 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 61 67  eration. The Pag
10a30 65 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 76 61  er.dbOrigSize va
10a31 72 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74  riable.**   cont
10a32 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  ains the number 
10a33 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
10a34 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77  database image w
10a35 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a  hen the current.
10a36 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e  **   transaction
10a37 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 65   was opened. The
10a38 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c   contents of all
10a39 20 74 68 72 65 65 20 6f 66 20 74 68 65 73 65 20   three of these 
10a3a 76 61 72 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20  variables is.** 
10a3b 20 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65    only guarantee
10a3c 64 20 74 6f 20 62 65 20 63 6f 72 72 65 63 74 20  d to be correct 
10a3d 69 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 50  if the boolean P
10a3e 61 67 65 72 2e 64 62 53 69 7a 65 56 61 6c 69 64  ager.dbSizeValid
10a3f 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20   is true..**.** 
10a40 20 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20 77 68    TODO: Under wh
10a41 61 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73  at conditions is
10a42 20 64 62 53 69 7a 65 56 61 6c 69 64 20 73 65 74   dbSizeValid set
10a43 3f 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a  ? Cleared?.**.**
10a44 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65   changeCountDone
10a45 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f  .**.**   This bo
10a46 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 20 69  olean variable i
10a47 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73  s used to make s
10a48 75 72 65 20 74 68 61 74 20 74 68 65 20 63 68 61  ure that the cha
10a49 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a 2a 20  nge-counter .** 
10a4a 20 20 28 74 68 65 20 34 2d 62 79 74 65 20 68 65    (the 4-byte he
10a4b 61 64 65 72 20 66 69 65 6c 64 20 61 74 20 62 79  ader field at by
10a4c 74 65 20 6f 66 66 73 65 74 20 32 34 20 6f 66 20  te offset 24 of 
10a4d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
10a4e 65 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f 74 20  e) is .**   not 
10a4f 75 70 64 61 74 65 64 20 6d 6f 72 65 20 6f 66 74  updated more oft
10a50 65 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72  en than necessar
10a51 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69  y. .**.**   It i
10a52 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 77 68  s set to true wh
10a53 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  en the change-co
10a54 75 6e 74 65 72 20 66 69 65 6c 64 20 69 73 20 75  unter field is u
10a55 70 64 61 74 65 64 2c 20 77 68 69 63 68 20 0a 2a  pdated, which .*
10a56 2a 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70  *   can only hap
10a57 70 65 6e 20 69 66 20 61 6e 20 65 78 63 6c 75 73  pen if an exclus
10a58 69 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  ive lock is held
10a59 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
10a5a 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74 20 69   file..**   It i
10a5b 73 20 63 6c 65 61 72 65 64 20 28 73 65 74 20 74  s cleared (set t
10a5c 6f 20 66 61 6c 73 65 29 20 77 68 65 6e 65 76 65  o false) wheneve
10a5d 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  r an exclusive l
10a5e 6f 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72 65 6c  ock is .**   rel
10a5f 69 6e 71 75 69 73 68 65 64 20 6f 6e 20 74 68 65  inquished on the
10a60 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
10a61 45 61 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e  Each time a tran
10a62 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69  saction is commi
10a63 74 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65 20 63  tted,.**   The c
10a64 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66  hangeCountDone f
10a65 6c 61 67 20 69 73 20 69 6e 73 70 65 63 74 65 64  lag is inspected
10a66 2e 20 49 66 20 69 74 20 69 73 20 74 72 75 65 2c  . If it is true,
10a67 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20   the work of.** 
10a68 20 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 63    updating the c
10a69 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 73  hange-counter is
10a6a 20 6f 6d 69 74 74 65 64 20 66 6f 72 20 74 68 65   omitted for the
10a6b 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
10a6c 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68  tion..**.**   Th
10a6d 69 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d 65 61  is mechanism mea
10a6e 6e 73 20 74 68 61 74 20 77 68 65 6e 20 72 75 6e  ns that when run
10a6f 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76  ning in exclusiv
10a70 65 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e 65 63  e mode, a connec
10a71 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65 64 20  tion .**   need 
10a72 6f 6e 6c 79 20 75 70 64 61 74 65 20 74 68 65 20  only update the 
10a73 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 6f  change-counter o
10a74 6e 63 65 2c 20 66 6f 72 20 74 68 65 20 66 69 72  nce, for the fir
10a75 73 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a  st transaction.*
10a76 2a 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a  *   committed..*
10a77 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65 64 0a  *.** dbModified.
10a78 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62 4d 6f  **.**   The dbMo
10a79 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 73  dified flag is s
10a7a 65 74 20 77 68 65 6e 65 76 65 72 20 61 20 64 61  et whenever a da
10a7b 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 64  tabase page is d
10a7c 69 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49 74 20  irtied..**   It 
10a7d 69 73 20 63 6c 65 61 72 65 64 20 61 74 20 74 68  is cleared at th
10a7e 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 74 72  e end of each tr
10a7f 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
10a80 20 20 20 49 74 20 69 73 20 75 73 65 64 20 77 68     It is used wh
10a81 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 6f 72  en committing or
10a82 20 6f 74 68 65 72 77 69 73 65 20 65 6e 64 69 6e   otherwise endin
10a83 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  g a transaction.
10a84 20 49 66 0a 2a 2a 20 20 20 74 68 65 20 64 62 4d   If.**   the dbM
10a85 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 20  odified flag is 
10a86 63 6c 65 61 72 20 74 68 65 6e 20 6c 65 73 73 20  clear then less 
10a87 77 6f 72 6b 20 68 61 73 20 74 6f 20 62 65 20 64  work has to be d
10a88 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e  one..**.** journ
10a89 61 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20  alStarted.**.** 
10a8a 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 73    This flag is s
10a8b 65 74 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  et whenever the 
10a8c 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
10a8d 20 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a   is synced. .**.
10a8e 2a 2a 20 20 20 54 68 65 20 70 6f 69 6e 74 20 6f  **   The point o
10a8f 66 20 74 68 69 73 20 66 6c 61 67 20 69 73 20 74  f this flag is t
10a90 68 61 74 20 69 74 20 6d 75 73 74 20 62 65 20 73  hat it must be s
10a91 65 74 20 61 66 74 65 72 20 74 68 65 20 0a 2a 2a  et after the .**
10a92 20 20 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c     first journal
10a93 20 68 65 61 64 65 72 20 69 6e 20 61 20 6a 6f 75   header in a jou
10a94 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 62 65  rnal file has be
10a95 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73  en synced to dis
10a96 6b 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20 74 68  k..**   After th
10a97 69 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c  is has happened,
10a98 20 6e 65 77 20 70 61 67 65 73 20 61 70 70 65 6e   new pages appen
10a99 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62  ded to the datab
10a9a 61 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74  ase .**   do not
10a9b 20 6e 65 65 64 20 74 68 65 20 50 47 48 44 52 5f   need the PGHDR_
10a9c 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 73  NEED_SYNC flag s
10a9d 65 74 2c 20 61 73 20 74 68 65 79 20 64 6f 20 6e  et, as they do n
10a9e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74 6f 20  ot need.**   to 
10a9f 77 61 69 74 20 66 6f 72 20 61 20 6a 6f 75 72 6e  wait for a journ
10aa0 61 6c 20 73 79 6e 63 20 62 65 66 6f 72 65 20 74  al sync before t
10aa1 68 65 79 20 63 61 6e 20 62 65 20 77 72 69 74 74  hey can be writt
10aa2 65 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20 20 74  en out to.**   t
10aa3 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
10aa4 20 28 73 65 65 20 66 75 6e 63 74 69 6f 6e 20 70   (see function p
10aa5 61 67 65 72 5f 77 72 69 74 65 28 29 29 2e 0a 2a  ager_write())..*
10aa6 2a 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73 74 65  *   .** setMaste
10aa7 72 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 76  r.**.**   This v
10aa8 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20  ariable is used 
10aa9 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  to ensure that t
10aaa 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
10aab 6c 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20  l file name.**  
10aac 20 28 69 66 20 61 6e 79 29 20 69 73 20 6f 6e 6c   (if any) is onl
10aad 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  y written into t
10aae 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
10aaf 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68  once..**.**   Wh
10ab0 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20  en committing a 
10ab1 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
10ab2 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
10ab3 66 69 6c 65 20 6e 61 6d 65 20 28 69 66 20 61 6e  file name (if an
10ab4 79 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65 20 77  y).**   may be w
10ab5 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
10ab6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 69  journal file whi
10ab7 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20  le the pager is 
10ab8 73 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20 50 41  still in.**   PA
10ab9 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61  GER_RESERVED sta
10aba 74 65 20 28 73 65 65 20 43 6f 6d 6d 69 74 50 68  te (see CommitPh
10abb 61 73 65 4f 6e 65 28 29 20 66 6f 72 20 74 68 65  aseOne() for the
10abc 20 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20   action). It.** 
10abd 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20    then attempts 
10abe 74 6f 20 75 70 67 72 61 64 65 20 74 6f 20 61 6e  to upgrade to an
10abf 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e   exclusive lock.
10ac0 20 49 66 20 74 68 69 73 20 61 74 74 65 6d 70 74   If this attempt
10ac1 0a 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74 68 65  .**   fails, the
10ac2 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6d 61  n SQLITE_BUSY ma
10ac3 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 74 6f  y be returned to
10ac4 20 74 68 65 20 75 73 65 72 20 61 6e 64 20 74 68   the user and th
10ac5 65 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61 79 20  e user.**   may 
10ac6 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 6d 69  attempt to commi
10ac7 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  t the transactio
10ac8 6e 20 61 67 61 69 6e 20 6c 61 74 65 72 20 28 63  n again later (c
10ac9 61 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d  alling.**   Comm
10aca 69 74 50 68 61 73 65 4f 6e 65 28 29 20 61 67 61  itPhaseOne() aga
10acb 69 6e 29 2e 20 54 68 69 73 20 66 6c 61 67 20 69  in). This flag i
10acc 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65  s used to ensure
10acd 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 20 20   that the .**   
10ace 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
10acf 61 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72 69 74  ame is only writ
10ad0 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
10ad1 61 6c 20 66 69 6c 65 20 74 68 65 20 66 69 72 73  al file the firs
10ad2 74 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f 6d 6d  t.**   time Comm
10ad3 69 74 50 68 61 73 65 4f 6e 65 28 29 20 69 73 20  itPhaseOne() is 
10ad4 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f  called..**.** do
10ad5 4e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20  NotSync.**.**   
10ad6 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73  This variable is
10ad7 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72 65 64   set and cleared
10ad8 20 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 72   by sqlite3Pager
10ad9 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e  Write()..**.** n
10ada 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20  eedSync.**.**   
10adb 54 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74 20 62  TODO: It might b
10adc 65 20 65 61 73 69 65 72 20 74 6f 20 73 65 74 20  e easier to set 
10add 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e  this variable in
10ade 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72   writeJournalHdr
10adf 28 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72 69 74  ().**   and writ
10ae0 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29  eMasterJournal()
10ae1 20 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20 69 74   only. Change it
10ae2 73 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22 75 6e  s meaning to "un
10ae3 73 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a 20 20  synced data.**  
10ae4 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65   has been writte
10ae5 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
10ae6 22 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d  "..**.** subjInM
10ae7 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68  emory.**.**   Th
10ae8 69 73 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20  is is a boolean 
10ae9 76 61 72 69 61 62 6c 65 2e 20 49 66 20 74 72 75  variable. If tru
10aea 65 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75  e, then any requ
10aeb 69 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  ired sub-journal
10aec 0a 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65 64 20  .**   is opened 
10aed 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  as an in-memory 
10aee 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66  journal file. If
10aef 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 6e 2d   false, then in-
10af0 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75 62 2d  memory.**   sub-
10af1 6a 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f 6e 6c  journals are onl
10af2 79 20 75 73 65 64 20 66 6f 72 20 69 6e 2d 6d 65  y used for in-me
10af3 6d 6f 72 79 20 70 61 67 65 72 20 66 69 6c 65 73  mory pager files
10af4 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 67 65  ..*/.struct Page
10af5 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  r {.  sqlite3_vf
10af6 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
10af7 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e    /* OS function
10af8 73 20 74 6f 20 75 73 65 20 66 6f 72 20 49 4f 20  s to use for IO 
10af9 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75 73 69 76  */.  u8 exclusiv
10afa 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  eMode;          
10afb 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75   /* Boolean. Tru
10afc 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  e if locking_mod
10afd 65 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a  e==EXCLUSIVE */.
10afe 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65    u8 journalMode
10aff 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
10b00 20 4f 6e 20 6f 66 20 74 68 65 20 50 41 47 45 52   On of the PAGER
10b01 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76  _JOURNALMODE_* v
10b02 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 75 73  alues */.  u8 us
10b03 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20  eJournal;       
10b04 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20         /* Use a 
10b05 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
10b06 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   on this file */
10b07 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b  .  u8 noReadlock
10b08 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
10b09 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20  * Do not bother 
10b0a 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f  to obtain readlo
10b0b 63 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79  cks */.  u8 noSy
10b0c 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
10b0d 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73       /* Do not s
10b0e 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ync the journal 
10b0f 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20  if true */.  u8 
10b10 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20  fullSync;       
10b11 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65           /* Do e
10b12 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68  xtra syncs of th
10b13 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f  e journal for ro
10b14 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38  bustness */.  u8
10b15 20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20   sync_flags;    
10b16 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
10b17 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20   of SYNC_NORMAL 
10b18 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a  or SYNC_FULL */.
10b19 20 20 75 38 20 74 65 6d 70 46 69 6c 65 3b 20 20    u8 tempFile;  
10b1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b1b 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20   zFilename is a 
10b1c 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 2a  temporary file *
10b1d 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b  /.  u8 readOnly;
10b1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b1f 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 72 65  /* True for a re
10b20 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65  ad-only database
10b21 20 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 3b 20   */.  u8 memDb; 
10b22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b23 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e 68    /* True to inh
10b24 69 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 49 2f  ibit all file I/
10b25 4f 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66  O */..  /* The f
10b26 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 63  ollowing block c
10b27 6f 6e 74 61 69 6e 73 20 74 68 6f 73 65 20 63 6c  ontains those cl
10b28 61 73 73 20 6d 65 6d 62 65 72 73 20 74 68 61 74  ass members that
10b29 20 61 72 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79   are dynamically
10b2a 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 64  .  ** modified d
10b2b 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f 70 65  uring normal ope
10b2c 72 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f 74 68  rations. The oth
10b2d 65 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20  er variables in 
10b2e 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 20  this structure. 
10b2f 20 2a 2a 20 61 72 65 20 65 69 74 68 65 72 20 63   ** are either c
10b30 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f  onstant througho
10b31 75 74 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20  ut the lifetime 
10b32 6f 66 20 74 68 65 20 70 61 67 65 72 2c 20 6f 72  of the pager, or
10b33 20 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65 64 20   else.  ** used 
10b34 74 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69 67 75  to store configu
10b35 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72  ration parameter
10b36 73 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68  s that affect th
10b37 65 20 77 61 79 20 74 68 65 20 70 61 67 65 72 20  e way the pager 
10b38 0a 20 20 2a 2a 20 6f 70 65 72 61 74 65 73 2e 0a  .  ** operates..
10b39 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 27 73    **.  ** The 's
10b3a 74 61 74 65 27 20 76 61 72 69 61 62 6c 65 20 69  tate' variable i
10b3b 73 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 6d  s described in m
10b3c 6f 72 65 20 64 65 74 61 69 6c 20 61 6c 6f 6e 67  ore detail along
10b3d 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 64   with the.  ** d
10b3e 65 73 63 72 69 70 74 69 6f 6e 73 20 6f 66 20 74  escriptions of t
10b3f 68 65 20 76 61 6c 75 65 73 20 69 74 20 6d 61 79  he values it may
10b40 20 74 61 6b 65 20 2d 20 50 41 47 45 52 5f 55 4e   take - PAGER_UN
10b41 4c 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79 20 6f  LOCK etc. Many o
10b42 66 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72  f the.  ** other
10b43 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68   variables in th
10b44 69 73 20 62 6c 6f 63 6b 20 61 72 65 20 64 65 73  is block are des
10b45 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 63 6f  cribed in the co
10b46 6d 6d 65 6e 74 20 64 69 72 65 63 74 6c 79 20 0a  mment directly .
10b47 20 20 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20    ** above this 
10b48 63 6c 61 73 73 20 64 65 66 69 6e 69 74 69 6f 6e  class definition
10b49 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74 61 74  ..  */.  u8 stat
10b4a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
10b4b 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e       /* PAGER_UN
10b4c 4c 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f  LOCK, _SHARED, _
10b4d 52 45 53 45 52 56 45 44 2c 20 65 74 63 2e 20 2a  RESERVED, etc. *
10b4e 2f 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65  /.  u8 dbModifie
10b4f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
10b50 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65  /* True if there
10b51 20 61 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73   are any changes
10b52 20 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20   to the Db */.  
10b53 75 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20 20  u8 needSync;    
10b54 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
10b55 72 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63 28  rue if an fsync(
10b56 29 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20 74  ) is needed on t
10b57 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  he journal */.  
10b58 75 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65  u8 journalStarte
10b59 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  d;          /* T
10b5a 72 75 65 20 69 66 20 68 65 61 64 65 72 20 6f 66  rue if header of
10b5b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63   journal is sync
10b5c 65 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67  ed */.  u8 chang
10b5d 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20  eCountDone;     
10b5e 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74 65 72      /* Set after
10b5f 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 74 68   incrementing th
10b60 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
10b61 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 73 74   */.  u8 setMast
10b62 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
10b63 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6d    /* True if a m
10b64 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e  -j name has been
10b65 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c   written to jrnl
10b66 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 79   */.  u8 doNotSy
10b67 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
10b68 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 57 68    /* Boolean. Wh
10b69 69 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e 6f 74  ile true, do not
10b6a 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65   spill the cache
10b6b 20 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a 65 56   */.  u8 dbSizeV
10b6c 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  alid;           
10b6d 20 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 64 62    /* Set when db
10b6e 53 69 7a 65 20 69 73 20 63 6f 72 72 65 63 74 20  Size is correct 
10b6f 2a 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e 4d 65  */.  u8 subjInMe
10b70 6d 6f 72 79 3b 20 20 20 20 20 20 20 20 20 20 20  mory;           
10b71 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 73 65 20   /* True to use 
10b72 69 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f  in-memory sub-jo
10b73 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f  urnals */.  Pgno
10b74 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20   dbSize;        
10b75 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
10b76 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
10b77 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
10b78 50 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b  Pgno dbOrigSize;
10b79 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64              /* d
10b7a 62 53 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65  bSize before the
10b7b 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
10b7c 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64  tion */.  Pgno d
10b7d 62 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20  bFileSize;      
10b7e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
10b7f 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
10b80 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
10b81 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20  .  int errCode; 
10b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10b83 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c  * One of several
10b84 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73   kinds of errors
10b85 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20   */.  int nRec; 
10b86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b87 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e    /* Pages journ
10b88 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74  alled since last
10b89 20 6a 2d 68 65 61 64 65 72 20 77 72 69 74 74 65   j-header writte
10b8a 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d  n */.  u32 cksum
10b8b 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  Init;           
10b8c 20 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64     /* Quasi-rand
10b8d 6f 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74  om value added t
10b8e 6f 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d  o every checksum
10b8f 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65   */.  u32 nSubRe
10b90 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
10b91 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
10b92 65 63 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74  ecords written t
10b93 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f  o sub-journal */
10b94 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f  .  Bitvec *pInJo
10b95 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f  urnal;         /
10b96 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61  * One bit for ea
10b97 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ch page in the d
10b98 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
10b99 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
10b9a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  fd;           /*
10b9b 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   File descriptor
10b9c 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f   for database */
10b9d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
10b9e 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f  *jfd;          /
10b9f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * File descripto
10ba0 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e  r for main journ
10ba1 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  al */.  sqlite3_
10ba2 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20  file *sjfd;     
10ba3 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63      /* File desc
10ba4 72 69 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a  riptor for sub-j
10ba5 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20  ournal */.  i64 
10ba6 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20  journalOff;     
10ba7 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
10ba8 6e 74 20 77 72 69 74 65 20 6f 66 66 73 65 74 20  nt write offset 
10ba9 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  in the journal f
10baa 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75  ile */.  i64 jou
10bab 72 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20  rnalHdr;        
10bac 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66       /* Byte off
10bad 73 65 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20  set to previous 
10bae 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a  journal header *
10baf 2f 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69  /.  PagerSavepoi
10bb0 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20  nt *aSavepoint; 
10bb1 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69  /* Array of acti
10bb2 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f  ve savepoints */
10bb3 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e  .  int nSavepoin
10bb4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t;             /
10bb5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
10bb6 65 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69  ents in aSavepoi
10bb7 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64  nt[] */.  char d
10bb8 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20  bFileVers[16];  
10bb9 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73        /* Changes
10bba 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 61   whenever databa
10bbb 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20  se file changes 
10bbc 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53  */.  u32 sectorS
10bbd 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
10bbe 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74   /* Assumed sect
10bbf 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72  or size during r
10bc0 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 69 6e  ollback */..  in
10bc1 74 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20  t nExtra;       
10bc2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
10bc3 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73   this many bytes
10bc4 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f   to each in-memo
10bc5 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32  ry page */.  u32
10bc6 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20   vfsFlags;      
10bc7 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
10bc8 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66  s for sqlite3_vf
10bc9 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69  s.xOpen() */.  i
10bca 6e 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20  nt pageSize;    
10bcb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
10bcc 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
10bcd 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e   a page */.  Pgn
10bce 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20  o mxPgno;       
10bcf 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
10bd0 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65  mum allowed size
10bd1 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
10bd2 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c   */.  char *zFil
10bd3 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  ename;          
10bd4 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
10bd5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
10bd6 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e  /.  char *zJourn
10bd7 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  al;             
10bd8 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a  /* Name of the j
10bd9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20  ournal file */. 
10bda 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64   int (*xBusyHand
10bdb 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20  ler)(void*); /* 
10bdc 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c  Function to call
10bdd 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20   when busy */.  
10bde 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c  void *pBusyHandl
10bdf 65 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43  erArg;      /* C
10be0 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20  ontext argument 
10be1 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72  for xBusyHandler
10be2 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
10be3 45 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69  E_TEST.  int nHi
10be4 74 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20  t, nMiss;       
10be5 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69       /* Cache hi
10be6 74 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a  ts and missing *
10be7 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e  /.  int nRead, n
10be8 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 20  Write;          
10be9 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 65  /* Database page
10bea 73 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a  s read/written *
10beb 2f 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20  /.#endif.  void 
10bec 28 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62 50  (*xReiniter)(DbP
10bed 61 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74  age*); /* Call t
10bee 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
10bef 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73   reloading pages
10bf0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
10bf1 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f  E_HAS_CODEC.  vo
10bf2 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f  id *(*xCodec)(vo
10bf3 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69  id*,void*,Pgno,i
10bf4 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20  nt); /* Routine 
10bf5 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20  for en/decoding 
10bf6 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  data */.  void *
10bf7 70 43 6f 64 65 63 41 72 67 3b 20 20 20 20 20 20  pCodecArg;      
10bf8 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
10bf9 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65  rgument to xCode
10bfa 63 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  c() */.#endif.  
10bfb 63 68 61 72 20 2a 70 54 6d 70 53 70 61 63 65 3b  char *pTmpSpace;
10bfc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
10bfd 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 62 79  ager.pageSize by
10bfe 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72  tes of space for
10bff 20 74 6d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36   tmp use */.  i6
10c00 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  4 journalSizeLim
10c01 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a  it;       /* Siz
10c02 65 20 6c 69 6d 69 74 20 66 6f 72 20 70 65 72 73  e limit for pers
10c03 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66  istent journal f
10c04 69 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63 68 65  iles */.  PCache
10c05 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20 20 20   *pPCache;      
10c06 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
10c07 20 74 6f 20 70 61 67 65 20 63 61 63 68 65 20 6f   to page cache o
10c08 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74  bject */.  sqlit
10c09 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b  e3_backup *pBack
10c0a 75 70 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  up;    /* Pointe
10c0b 72 20 74 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67  r to list of ong
10c0c 6f 69 6e 67 20 62 61 63 6b 75 70 20 70 72 6f 63  oing backup proc
10c0d 65 73 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  esses */.};../*.
10c0e 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
10c0f 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
10c10 73 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20  s hold counters 
10c11 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74  used for.** test
10c12 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c  ing purposes onl
10c13 79 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62  y.  These variab
10c14 6c 65 73 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  les do not exist
10c15 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73   in.** a non-tes
10c16 74 69 6e 67 20 62 75 69 6c 64 2e 20 20 54 68 65  ting build.  The
10c17 73 65 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  se variables are
10c18 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65   not thread-safe
10c19 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
10c1a 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
10c1b 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
10c1c 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e  ager_readdb_coun
10c1d 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d  t = 0;    /* Num
10c1e 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65  ber of full page
10c1f 73 20 72 65 61 64 20 66 72 6f 6d 20 44 42 20 2a  s read from DB *
10c20 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
10c21 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77   sqlite3_pager_w
10c22 72 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30  ritedb_count = 0
10c23 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ;   /* Number of
10c24 20 66 75 6c 6c 20 70 61 67 65 73 20 77 72 69 74   full pages writ
10c25 74 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c  ten to DB */.SQL
10c26 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
10c27 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 6a  te3_pager_writej
10c28 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f  _count = 0;    /
10c29 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
10c2a 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75  s written to jou
10c2b 72 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65  rnal */.# define
10c2c 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29 20 20   PAGER_INCR(v)  
10c2d 76 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  v++.#else.# defi
10c2e 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29  ne PAGER_INCR(v)
10c2f 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a  .#endif..../*.**
10c30 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 62   Journal files b
10c31 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 66 6f  egin with the fo
10c32 6c 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20 73 74  llowing magic st
10c33 72 69 6e 67 2e 20 20 54 68 65 20 64 61 74 61 0a  ring.  The data.
10c34 2a 2a 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20  ** was obtained 
10c35 66 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d  from /dev/random
10c36 2e 20 20 49 74 20 69 73 20 75 73 65 64 20 6f 6e  .  It is used on
10c37 6c 79 20 61 73 20 61 20 73 61 6e 69 74 79 20 63  ly as a sanity c
10c38 68 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63  heck..**.** Sinc
10c39 65 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c  e version 2.8.0,
10c3a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72   the journal for
10c3b 6d 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 64 64  mat contains add
10c3c 69 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a  itional sanity.*
10c3d 2a 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72  * checking infor
10c3e 6d 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20  mation.  If the 
10c3f 70 6f 77 65 72 20 66 61 69 6c 73 20 77 68 69 6c  power fails whil
10c40 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  e the journal is
10c41 20 62 65 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65   being.** writte
10c42 6e 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67  n, semi-random g
10c43 61 72 62 61 67 65 20 64 61 74 61 20 6d 69 67 68  arbage data migh
10c44 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20  t appear in the 
10c45 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20  journal.** file 
10c46 61 66 74 65 72 20 70 6f 77 65 72 20 69 73 20 72  after power is r
10c47 65 73 74 6f 72 65 64 2e 20 20 49 66 20 61 6e 20  estored.  If an 
10c48 61 74 74 65 6d 70 74 20 69 73 20 74 68 65 6e 20  attempt is then 
10c49 6d 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20  made.** to roll 
10c4a 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b  the journal back
10c4b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  , the database c
10c4c 6f 75 6c 64 20 62 65 20 63 6f 72 72 75 70 74 65  ould be corrupte
10c4d 64 2e 20 20 54 68 65 20 61 64 64 69 74 69 6f 6e  d.  The addition
10c4e 61 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63 68 65  al.** sanity che
10c4f 63 6b 69 6e 67 20 64 61 74 61 20 69 73 20 61 6e  cking data is an
10c50 20 61 74 74 65 6d 70 74 20 74 6f 20 64 69 73 63   attempt to disc
10c51 6f 76 65 72 20 74 68 65 20 67 61 72 62 61 67 65  over the garbage
10c52 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e   in the.** journ
10c53 61 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74  al and ignore it
10c54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69  ..**.** The sani
10c55 74 79 20 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f  ty checking info
10c56 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  rmation for the 
10c57 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d  new journal form
10c58 61 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f  at consists.** o
10c59 66 20 61 20 33 32 2d 62 69 74 20 63 68 65 63 6b  f a 32-bit check
10c5a 73 75 6d 20 6f 6e 20 65 61 63 68 20 70 61 67 65  sum on each page
10c5b 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 63   of data.  The c
10c5c 68 65 63 6b 73 75 6d 20 63 6f 76 65 72 73 20 62  hecksum covers b
10c5d 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 67 65 20  oth.** the page 
10c5e 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 70  number and the p
10c5f 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20  Pager->pageSize 
10c60 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 66 6f  bytes of data fo
10c61 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54  r the page..** T
10c62 68 69 73 20 63 6b 73 75 6d 20 69 73 20 69 6e 69  his cksum is ini
10c63 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 33 32  tialized to a 32
10c64 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75  -bit random valu
10c65 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20 69  e that appears i
10c66 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  n the.** journal
10c67 20 66 69 6c 65 20 72 69 67 68 74 20 61 66 74 65   file right afte
10c68 72 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 54  r the header.  T
10c69 68 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61  he random initia
10c6a 6c 69 7a 65 72 20 69 73 20 69 6d 70 6f 72 74 61  lizer is importa
10c6b 6e 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65 20 67  nt,.** because g
10c6c 61 72 62 61 67 65 20 64 61 74 61 20 74 68 61 74  arbage data that
10c6d 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20   appears at the 
10c6e 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c  end of a journal
10c6f 20 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61   is likely.** da
10c70 74 61 20 74 68 61 74 20 77 61 73 20 6f 6e 63 65  ta that was once
10c71 20 69 6e 20 6f 74 68 65 72 20 66 69 6c 65 73 20   in other files 
10c72 74 68 61 74 20 68 61 76 65 20 6e 6f 77 20 62 65  that have now be
10c73 65 6e 20 64 65 6c 65 74 65 64 2e 20 20 49 66 20  en deleted.  If 
10c74 74 68 65 0a 2a 2a 20 67 61 72 62 61 67 65 20 64  the.** garbage d
10c75 61 74 61 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e  ata came from an
10c76 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61   obsolete journa
10c77 6c 20 66 69 6c 65 2c 20 74 68 65 20 63 68 65 63  l file, the chec
10c78 6b 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62  ksums might.** b
10c79 65 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74 20  e correct.  But 
10c7a 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  by initializing 
10c7b 74 68 65 20 63 68 65 63 6b 73 75 6d 20 74 6f 20  the checksum to 
10c7c 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77 68 69  random value whi
10c7d 63 68 0a 2a 2a 20 69 73 20 64 69 66 66 65 72 65  ch.** is differe
10c7e 6e 74 20 66 6f 72 20 65 76 65 72 79 20 6a 6f 75  nt for every jou
10c7f 72 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a  rnal, we minimiz
10c80 65 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a  e that risk..*/.
10c81 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
10c82 69 67 6e 65 64 20 63 68 61 72 20 61 4a 6f 75 72  igned char aJour
10c83 6e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20  nalMagic[] = {. 
10c84 20 30 78 64 39 2c 20 30 78 64 35 2c 20 30 78 30   0xd9, 0xd5, 0x0
10c85 35 2c 20 30 78 66 39 2c 20 30 78 32 30 2c 20 30  5, 0xf9, 0x20, 0
10c86 78 61 31 2c 20 30 78 36 33 2c 20 30 78 64 37 2c  xa1, 0x63, 0xd7,
10c87 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  .};../*.** The s
10c88 69 7a 65 20 6f 66 20 74 68 65 20 6f 66 20 65 61  ize of the of ea
10c89 63 68 20 70 61 67 65 20 72 65 63 6f 72 64 20 69  ch page record i
10c8a 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  n the journal is
10c8b 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65   given by.** the
10c8c 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
10c8d 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55  ..*/.#define JOU
10c8e 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
10c8f 72 29 20 20 28 28 70 50 61 67 65 72 2d 3e 70 61  r)  ((pPager->pa
10c90 67 65 53 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a  geSize) + 8)../*
10c91 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
10c92 68 65 61 64 65 72 20 73 69 7a 65 20 66 6f 72 20  header size for 
10c93 74 68 69 73 20 70 61 67 65 72 2e 20 54 68 69 73  this pager. This
10c94 20 69 73 20 75 73 75 61 6c 6c 79 20 74 68 65 20   is usually the 
10c95 73 61 6d 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73  same .** size as
10c96 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73   a single disk s
10c97 65 63 74 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20  ector. See also 
10c98 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 29 2e  setSectorSize().
10c99 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52  .*/.#define JOUR
10c9a 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
10c9b 72 29 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74  r) (pPager->sect
10c9c 6f 72 53 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54  orSize)../*.** T
10c9d 68 65 20 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69  he macro MEMDB i
10c9e 73 20 74 72 75 65 20 69 66 20 77 65 20 61 72 65  s true if we are
10c9f 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e   dealing with an
10ca0 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
10ca1 61 73 65 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68  ase..** We do th
10ca2 69 73 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f  is as a macro so
10ca3 20 74 68 61 74 20 69 66 20 74 68 65 20 53 51 4c   that if the SQL
10ca4 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44  ITE_OMIT_MEMORYD
10ca5 42 20 6d 61 63 72 6f 20 69 73 20 73 65 74 2c 0a  B macro is set,.
10ca6 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
10ca7 4d 45 4d 44 42 20 77 69 6c 6c 20 62 65 20 61 20  MEMDB will be a 
10ca8 63 6f 6e 73 74 61 6e 74 20 61 6e 64 20 74 68 65  constant and the
10ca9 20 63 6f 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f   compiler will o
10caa 70 74 69 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63  ptimize.** out c
10cab 6f 64 65 20 74 68 61 74 20 77 6f 75 6c 64 20 6e  ode that would n
10cac 65 76 65 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f  ever execute..*/
10cad 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
10cae 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64  MIT_MEMORYDB.# d
10caf 65 66 69 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65  efine MEMDB 0.#e
10cb0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d  lse.# define MEM
10cb1 44 42 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62  DB pPager->memDb
10cb2 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
10cb3 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c  he maximum legal
10cb4 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
10cb5 28 32 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23  (2^31 - 1)..*/.#
10cb6 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58  define PAGER_MAX
10cb7 5f 50 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37  _PGNO 2147483647
10cb8 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
10cb9 20 0a 2f 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a   ./*.** Usage:.*
10cba 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 61  *.**   assert( a
10cbb 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61 74  ssert_pager_stat
10cbc 65 28 70 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a  e(pPager) );.*/.
10cbd 73 74 61 74 69 63 20 69 6e 74 20 61 73 73 65 72  static int asser
10cbe 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 50 61  t_pager_state(Pa
10cbf 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20  ger *pPager){.. 
10cc0 20 2f 2a 20 41 20 74 65 6d 70 2d 66 69 6c 65 20   /* A temp-file 
10cc1 69 73 20 61 6c 77 61 79 73 20 69 6e 20 50 41 47  is always in PAG
10cc2 45 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 72 20  ER_EXCLUSIVE or 
10cc3 50 41 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61  PAGER_SYNCED sta
10cc4 74 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  te. */.  assert(
10cc5 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
10cc6 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e  e==0 || pPager->
10cc7 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
10cc8 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20  LUSIVE );..  /* 
10cc9 54 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44  The changeCountD
10cca 6f 6e 65 20 66 6c 61 67 20 69 73 20 61 6c 77 61  one flag is alwa
10ccb 79 73 20 73 65 74 20 66 6f 72 20 74 65 6d 70 2d  ys set for temp-
10ccc 66 69 6c 65 73 20 2a 2f 0a 20 20 61 73 73 65 72  files */.  asser
10ccd 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  t( pPager->tempF
10cce 69 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72  ile==0 || pPager
10ccf 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e  ->changeCountDon
10cd0 65 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31  e );..  return 1
10cd1 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
10cd2 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
10cd3 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72 79   it is necessary
10cd4 20 74 6f 20 77 72 69 74 65 20 70 61 67 65 20 2a   to write page *
10cd5 70 50 67 20 69 6e 74 6f 20 74 68 65 20 73 75 62  pPg into the sub
10cd6 2d 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70  -journal..** A p
10cd7 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20  age needs to be 
10cd8 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
10cd9 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20   sub-journal if 
10cda 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e 65  there exists one
10cdb 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e  .** or more open
10cdc 20 73 61 76 65 70 6f 69 6e 74 73 20 66 6f 72 20   savepoints for 
10cdd 77 68 69 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  which:.**.**   *
10cde 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   The page-number
10cdf 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
10ce0 20 65 71 75 61 6c 20 74 6f 20 50 61 67 65 72 53   equal to PagerS
10ce1 61 76 65 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20  avepoint.nOrig, 
10ce2 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62  and.**   * The b
10ce3 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  it corresponding
10ce4 20 74 6f 20 74 68 65 20 70 61 67 65 2d 6e 75 6d   to the page-num
10ce5 62 65 72 20 69 73 20 6e 6f 74 20 73 65 74 20 69  ber is not set i
10ce6 6e 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61  n.**     PagerSa
10ce7 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70  vepoint.pInSavep
10ce8 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  oint..*/.static 
10ce9 69 6e 74 20 73 75 62 6a 52 65 71 75 69 72 65 73  int subjRequires
10cea 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29  Page(PgHdr *pPg)
10ceb 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20  {.  Pgno pgno = 
10cec 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67  pPg->pgno;.  Pag
10ced 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
10cee 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20  ->pPager;.  int 
10cef 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
10cf0 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69  pPager->nSavepoi
10cf1 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61  nt; i++){.    Pa
10cf2 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20  gerSavepoint *p 
10cf3 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65  = &pPager->aSave
10cf4 70 6f 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66  point[i];.    if
10cf5 28 20 70 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f  ( p->nOrig>=pgno
10cf6 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 69   && 0==sqlite3Bi
10cf7 74 76 65 63 54 65 73 74 28 70 2d 3e 70 49 6e 53  tvecTest(p->pInS
10cf8 61 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20  avepoint, pgno) 
10cf9 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
10cfa 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
10cfb 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
10cfc 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
10cfd 20 74 68 65 20 70 61 67 65 20 69 73 20 61 6c 72   the page is alr
10cfe 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72  eady in the jour
10cff 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  nal file..*/.sta
10d00 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f  tic int pageInJo
10d01 75 72 6e 61 6c 28 50 67 48 64 72 20 2a 70 50 67  urnal(PgHdr *pPg
10d02 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
10d03 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50  te3BitvecTest(pP
10d04 67 2d 3e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  g->pPager->pInJo
10d05 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f  urnal, pPg->pgno
10d06 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  );.}../*.** Read
10d07 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65   a 32-bit intege
10d08 72 20 66 72 6f 6d 20 74 68 65 20 67 69 76 65 6e  r from the given
10d09 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
10d0a 2e 20 20 53 74 6f 72 65 20 74 68 65 20 69 6e 74  .  Store the int
10d0b 65 67 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20  eger.** that is 
10d0c 72 65 61 64 20 69 6e 20 2a 70 52 65 73 2e 20 20  read in *pRes.  
10d0d 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
10d0e 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   if everything w
10d0f 6f 72 6b 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20  orked, or an.** 
10d10 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f  error code is so
10d11 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
10d12 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61  ng..**.** All va
10d13 6c 75 65 73 20 61 72 65 20 73 74 6f 72 65 64 20  lues are stored 
10d14 6f 6e 20 64 69 73 6b 20 61 73 20 62 69 67 2d 65  on disk as big-e
10d15 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ndian..*/.static
10d16 20 69 6e 74 20 72 65 61 64 33 32 62 69 74 73 28   int read32bits(
10d17 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64  sqlite3_file *fd
10d18 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33  , i64 offset, u3
10d19 32 20 2a 70 52 65 73 29 7b 0a 20 20 75 6e 73 69  2 *pRes){.  unsi
10d1a 67 6e 65 64 20 63 68 61 72 20 61 63 5b 34 5d 3b  gned char ac[4];
10d1b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69  .  int rc = sqli
10d1c 74 65 33 4f 73 52 65 61 64 28 66 64 2c 20 61 63  te3OsRead(fd, ac
10d1d 2c 20 73 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66  , sizeof(ac), of
10d1e 66 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d  fset);.  if( rc=
10d1f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
10d20 20 20 2a 70 52 65 73 20 3d 20 73 71 6c 69 74 65    *pRes = sqlite
10d21 33 47 65 74 34 62 79 74 65 28 61 63 29 3b 0a 20  3Get4byte(ac);. 
10d22 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
10d23 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61  }../*.** Write a
10d24 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
10d25 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 62 75  into a string bu
10d26 66 66 65 72 20 69 6e 20 62 69 67 2d 65 6e 64 69  ffer in big-endi
10d27 61 6e 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a  an byte order..*
10d28 2f 0a 23 64 65 66 69 6e 65 20 70 75 74 33 32 62  /.#define put32b
10d29 69 74 73 28 41 2c 42 29 20 20 73 71 6c 69 74 65  its(A,B)  sqlite
10d2a 33 50 75 74 34 62 79 74 65 28 28 75 38 2a 29 41  3Put4byte((u8*)A
10d2b 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ,B)../*.** Write
10d2c 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65   a 32-bit intege
10d2d 72 20 69 6e 74 6f 20 74 68 65 20 67 69 76 65 6e  r into the given
10d2e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
10d2f 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
10d30 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73  _OK.** on succes
10d31 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  s or an error co
10d32 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20  de is something 
10d33 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73  goes wrong..*/.s
10d34 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 33  tatic int write3
10d35 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69  2bits(sqlite3_fi
10d36 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73  le *fd, i64 offs
10d37 65 74 2c 20 75 33 32 20 76 61 6c 29 7b 0a 20 20  et, u32 val){.  
10d38 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75  char ac[4];.  pu
10d39 74 33 32 62 69 74 73 28 61 63 2c 20 76 61 6c 29  t32bits(ac, val)
10d3a 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
10d3b 65 33 4f 73 57 72 69 74 65 28 66 64 2c 20 61 63  e3OsWrite(fd, ac
10d3c 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a  , 4, offset);.}.
10d3d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
10d3e 65 6e 74 20 74 6f 20 74 68 69 73 20 6d 61 63 72  ent to this macr
10d3f 6f 20 69 73 20 61 20 66 69 6c 65 20 64 65 73 63  o is a file desc
10d40 72 69 70 74 6f 72 20 28 74 79 70 65 20 73 71 6c  riptor (type sql
10d41 69 74 65 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20  ite3_file*)..** 
10d42 52 65 74 75 72 6e 20 30 20 69 66 20 69 74 20 69  Return 0 if it i
10d43 73 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e  s not open, or n
10d44 6f 6e 2d 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74  on-zero (but not
10d45 20 31 29 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a   1) if it is..**
10d46 0a 2a 2a 20 54 68 69 73 20 69 73 20 73 6f 20 74  .** This is so t
10d47 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20  hat expressions 
10d48 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61  can be written a
10d49 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69  s:.**.**   if( i
10d4a 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
10d4b 64 29 20 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20  d) ){ ....**.** 
10d4c 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a  instead of.**.**
10d4d 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a     if( pPager->j
10d4e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20  fd->pMethods ){ 
10d4f 2e 2e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69  ....*/.#define i
10d50 73 4f 70 65 6e 28 70 46 64 29 20 28 28 70 46 64  sOpen(pFd) ((pFd
10d51 29 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a  )->pMethods)../*
10d52 0a 2a 2a 20 49 66 20 66 69 6c 65 20 70 46 64 20  .** If file pFd 
10d53 69 73 20 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71  is open, call sq
10d54 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20  lite3OsUnlock() 
10d55 6f 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  on it..*/.static
10d56 20 69 6e 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71   int osUnlock(sq
10d57 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c  lite3_file *pFd,
10d58 20 69 6e 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69   int eLock){.  i
10d59 66 28 20 21 69 73 4f 70 65 6e 28 70 46 64 29 20  f( !isOpen(pFd) 
10d5a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
10d5b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72  LITE_OK;.  }.  r
10d5c 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 55  eturn sqlite3OsU
10d5d 6e 6c 6f 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b  nlock(pFd, eLock
10d5e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
10d5f 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d   function determ
10d60 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20  ines whether or 
10d61 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77  not the atomic-w
10d62 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  rite optimizatio
10d63 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  n.** can be used
10d64 20 77 69 74 68 20 74 68 69 73 20 70 61 67 65 72   with this pager
10d65 2e 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  . The optimizati
10d66 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69  on can be used i
10d67 66 3a 0a 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68  f:.**.**  (a) th
10d68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
10d69 20 62 79 20 4f 73 44 65 76 69 63 65 43 68 61 72   by OsDeviceChar
10d6a 61 63 74 65 72 69 73 74 69 63 73 28 29 20 69 6e  acteristics() in
10d6b 64 69 63 61 74 65 73 20 74 68 61 74 0a 2a 2a 20  dicates that.** 
10d6c 20 20 20 20 20 61 20 64 61 74 61 62 61 73 65 20       a database 
10d6d 70 61 67 65 20 6d 61 79 20 62 65 20 77 72 69 74  page may be writ
10d6e 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20  ten atomically, 
10d6f 61 6e 64 0a 2a 2a 20 20 28 62 29 20 74 68 65 20  and.**  (b) the 
10d70 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
10d71 79 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29  y OsSectorSize()
10d72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
10d73 20 65 71 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74   equal.**      t
10d74 6f 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 2e  o the page size.
10d75 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d  .**.** The optim
10d76 69 7a 61 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20  ization is also 
10d77 61 6c 77 61 79 73 20 65 6e 61 62 6c 65 64 20 66  always enabled f
10d78 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  or temporary fil
10d79 65 73 2e 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20  es. It is.** an 
10d7a 65 72 72 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68  error to call th
10d7b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 70  is function if p
10d7c 50 61 67 65 72 20 69 73 20 6f 70 65 6e 65 64 20  Pager is opened 
10d7d 6f 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a  on an in-memory.
10d7e 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  ** database..**.
10d7f 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69  ** If the optimi
10d80 7a 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65  zation cannot be
10d81 20 75 73 65 64 2c 20 30 20 69 73 20 72 65 74 75   used, 0 is retu
10d82 72 6e 65 64 2e 20 49 66 20 69 74 20 63 61 6e 20  rned. If it can 
10d83 62 65 20 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e  be used,.** then
10d84 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
10d85 6e 65 64 20 69 73 20 74 68 65 20 73 69 7a 65 20  ned is the size 
10d86 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
10d87 69 6c 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63  ile when it.** c
10d88 6f 6e 74 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b  ontains rollback
10d89 20 64 61 74 61 20 66 6f 72 20 65 78 61 63 74 6c   data for exactl
10d8a 79 20 6f 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23  y one page..*/.#
10d8b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
10d8c 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
10d8d 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
10d8e 42 75 66 66 65 72 53 69 7a 65 28 50 61 67 65 72  BufferSize(Pager
10d8f 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 61 73 73   *pPager){.  ass
10d90 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20  ert( !MEMDB );. 
10d91 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65   if( !pPager->te
10d92 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e  mpFile ){.    in
10d93 74 20 64 63 3b 20 20 20 20 20 20 20 20 20 20 20  t dc;           
10d94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10d95 2f 2a 20 44 65 76 69 63 65 20 63 68 61 72 61 63  /* Device charac
10d96 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 20 20 20  teristics */.   
10d97 20 69 6e 74 20 6e 53 65 63 74 6f 72 3b 20 20 20   int nSector;   
10d98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10d99 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a     /* Sector siz
10d9a 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50  e */.    int szP
10d9b 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
10d9c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
10d9d 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20  ge size */..    
10d9e 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
10d9f 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20  Pager->fd) );.  
10da0 20 20 64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    dc = sqlite3Os
10da1 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
10da2 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64  stics(pPager->fd
10da3 29 3b 0a 20 20 20 20 6e 53 65 63 74 6f 72 20 3d  );.    nSector =
10da4 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53   pPager->sectorS
10da5 69 7a 65 3b 0a 20 20 20 20 73 7a 50 61 67 65 20  ize;.    szPage 
10da6 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
10da7 7a 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  ze;..    assert(
10da8 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
10da9 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29  MIC512==(512>>8)
10daa 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 53 51  );.    assert(SQ
10dab 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
10dac 43 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29  C64K==(65536>>8)
10dad 29 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 64  );.    if( 0==(d
10dae 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  c&(SQLITE_IOCAP_
10daf 41 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e  ATOMIC|(szPage>>
10db0 38 29 29 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73  8)) || nSector>s
10db1 7a 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20  zPage) ){.      
10db2 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
10db3 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f    }..  return JO
10db4 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
10db5 67 65 72 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50  ger) + JOURNAL_P
10db6 47 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a  G_SZ(pPager);.}.
10db7 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
10db8 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
10db9 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74  GES is defined t
10dba 68 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73  hen we do some s
10dbb 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a  anity checking.*
10dbc 2a 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75  * on the cache u
10dbd 73 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63  sing a hash func
10dbe 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75  tion.  This is u
10dbf 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a  sed for testing.
10dc0 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ** and debugging
10dc1 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66   only..*/.#ifdef
10dc2 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
10dc3 47 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  GES./*.** Return
10dc4 20 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f   a 32-bit hash o
10dc5 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20  f the page data 
10dc6 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74  for pPage..*/.st
10dc7 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64  atic u32 pager_d
10dc8 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74  atahash(int nByt
10dc9 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  e, unsigned char
10dca 20 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20   *pData){.  u32 
10dcb 68 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20  hash = 0;.  int 
10dcc 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
10dcd 6e 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  nByte; i++){.   
10dce 20 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30   hash = (hash*10
10dcf 33 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a  39) + pData[i];.
10dd0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73    }.  return has
10dd1 68 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20  h;.}.static u32 
10dd2 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50  pager_pagehash(P
10dd3 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20  gHdr *pPage){.  
10dd4 72 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74  return pager_dat
10dd5 61 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61  ahash(pPage->pPa
10dd6 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28  ger->pageSize, (
10dd7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
10dd8 70 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d  pPage->pData);.}
10dd9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67  .static void pag
10dda 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28  er_set_pagehash(
10ddb 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20  PgHdr *pPage){. 
10ddc 20 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68   pPage->pageHash
10ddd 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
10dde 68 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a  h(pPage);.}../*.
10ddf 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47  ** The CHECK_PAG
10de0 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20  E macro takes a 
10de1 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67  PgHdr* as an arg
10de2 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45  ument. If SQLITE
10de3 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20  _CHECK_PAGES.** 
10de4 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20  is defined, and 
10de5 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65  NDEBUG is not de
10de6 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74  fined, an assert
10de7 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65  () statement che
10de8 63 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  cks.** that the 
10de9 70 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64  page is either d
10dea 69 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61  irty or still ma
10deb 74 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c  tches the calcul
10dec 61 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a  ated page-hash..
10ded 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b  */.#define CHECK
10dee 5f 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61  _PAGE(x) checkPa
10def 67 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69  ge(x).static voi
10df0 64 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64  d checkPage(PgHd
10df1 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72  r *pPg){.  Pager
10df2 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
10df3 70 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74  pPager;.  assert
10df4 28 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68  ( !pPg->pageHash
10df5 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43   || pPager->errC
10df6 6f 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50  ode.      || (pP
10df7 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
10df8 49 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61  IRTY) || pPg->pa
10df9 67 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61  geHash==pager_pa
10dfa 67 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d  gehash(pPg) );.}
10dfb 0a 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ..#else.#define 
10dfc 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 58  pager_datahash(X
10dfd 2c 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70  ,Y)  0.#define p
10dfe 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 29  ager_pagehash(X)
10dff 20 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43    0.#define CHEC
10e00 4b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66  K_PAGE(x).#endif
10e01 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43    /* SQLITE_CHEC
10e02 4b 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a  K_PAGES */../*.*
10e03 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63  * When this is c
10e04 61 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61  alled the journa
10e05 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72  l file for pager
10e06 20 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 20   pPager must be 
10e07 6f 70 65 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75  open..** This fu
10e08 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20  nction attempts 
10e09 74 6f 20 72 65 61 64 20 61 20 6d 61 73 74 65 72  to read a master
10e0a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
10e0b 6d 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20  me from the .** 
10e0c 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20  end of the file 
10e0d 61 6e 64 2c 20 69 66 20 73 75 63 63 65 73 73 66  and, if successf
10e0e 75 6c 2c 20 63 6f 70 69 65 73 20 69 74 20 69 6e  ul, copies it in
10e0f 74 6f 20 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69  to memory suppli
10e10 65 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 63 61  ed .** by the ca
10e11 6c 6c 65 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e  ller. See commen
10e12 74 73 20 61 62 6f 76 65 20 77 72 69 74 65 4d 61  ts above writeMa
10e13 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f  sterJournal() fo
10e14 72 20 74 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20  r the format.** 
10e15 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20  used to store a 
10e16 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
10e17 69 6c 65 20 6e 61 6d 65 20 61 74 20 74 68 65 20  ile name at the 
10e18 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c  end of a journal
10e19 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61   file..**.** zMa
10e1a 73 74 65 72 20 6d 75 73 74 20 70 6f 69 6e 74 20  ster must point 
10e1b 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 61  to a buffer of a
10e1c 74 20 6c 65 61 73 74 20 6e 4d 61 73 74 65 72 20  t least nMaster 
10e1d 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  bytes allocated 
10e1e 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72  by.** the caller
10e1f 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65  . This should be
10e20 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50   sqlite3_vfs.mxP
10e21 61 74 68 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e  athname+1 (to en
10e22 73 75 72 65 20 74 68 65 72 65 20 69 73 0a 2a 2a  sure there is.**
10e23 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 74 6f   enough space to
10e24 20 77 72 69 74 65 20 74 68 65 20 6d 61 73 74 65   write the maste
10e25 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e  r journal name).
10e26 20 49 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a   If the master j
10e27 6f 75 72 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69  ournal.** name i
10e28 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  n the journal is
10e29 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61   longer than nMa
10e2a 73 74 65 72 20 62 79 74 65 73 20 28 69 6e 63 6c  ster bytes (incl
10e2b 75 64 69 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74  uding a.** nul-t
10e2c 65 72 6d 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e  erminator), then
10e2d 20 74 68 69 73 20 69 73 20 68 61 6e 64 6c 65 64   this is handled
10e2e 20 61 73 20 69 66 20 6e 6f 20 6d 61 73 74 65 72   as if no master
10e2f 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a   journal name.**
10e30 20 77 65 72 65 20 70 72 65 73 65 6e 74 20 69 6e   were present in
10e31 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a   the journal..**
10e32 0a 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20  .** If a master 
10e33 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
10e34 65 20 69 73 20 70 72 65 73 65 6e 74 20 61 74 20  e is present at 
10e35 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a  the end of the j
10e36 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20  ournal.** file, 
10e37 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65  then it is copie
10e38 64 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65  d into the buffe
10e39 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  r pointed to by 
10e3a 7a 4d 61 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75  zMaster. A.** nu
10e3b 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74  l-terminator byt
10e3c 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f  e is appended to
10e3d 20 74 68 65 20 62 75 66 66 65 72 20 66 6f 6c 6c   the buffer foll
10e3e 6f 77 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72  owing the master
10e3f 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
10e40 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   name..**.** If 
10e41 69 74 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  it is determined
10e42 20 74 68 61 74 20 6e 6f 20 6d 61 73 74 65 72 20   that no master 
10e43 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
10e44 65 20 69 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a  e is present .**
10e45 20 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73 20 73   zMaster[0] is s
10e46 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49  et to 0 and SQLI
10e47 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a  TE_OK returned..
10e48 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
10e49 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72  r occurs while r
10e4a 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20  eading from the 
10e4b 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e  journal file, an
10e4c 20 53 51 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72   SQLite.** error
10e4d 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
10e4e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
10e4f 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e   readMasterJourn
10e50 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  al(sqlite3_file 
10e51 2a 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d  *pJrnl, char *zM
10e52 61 73 74 65 72 2c 20 75 33 32 20 6e 4d 61 73 74  aster, u32 nMast
10e53 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  er){.  int rc;  
10e54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e55 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
10e56 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20   */.  u32 len;  
10e57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e58 20 2f 2a 20 4c 65 6e 67 74 68 20 69 6e 20 62 79   /* Length in by
10e59 74 65 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f  tes of master jo
10e5a 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20  urnal name */.  
10e5b 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20  i64 szJ;        
10e5c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
10e5d 74 61 6c 20 73 69 7a 65 20 69 6e 20 62 79 74 65  tal size in byte
10e5e 73 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  s of journal fil
10e5f 65 20 70 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32  e pJrnl */.  u32
10e60 20 63 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20   cksum;         
10e61 20 20 20 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68          /* MJ ch
10e62 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 72 65 61  ecksum value rea
10e63 64 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a  d from journal *
10e64 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20  /.  u32 u;      
10e65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10e66 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20  * Unsigned loop 
10e67 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73  counter */.  uns
10e68 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69  igned char aMagi
10e69 63 5b 38 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66  c[8];   /* A buf
10e6a 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  fer to hold the 
10e6b 6d 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a  magic header */.
10e6c 20 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27    zMaster[0] = '
10e6d 5c 30 27 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49  \0';..  if( SQLI
10e6e 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
10e6f 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
10e70 4a 72 6e 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20  Jrnl, &szJ)).   
10e71 7c 7c 20 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20  || szJ<16.   || 
10e72 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
10e73 20 72 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e   read32bits(pJrn
10e74 6c 2c 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29  l, szJ-16, &len)
10e75 29 0a 20 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61  ).   || len>=nMa
10e76 73 74 65 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49  ster .   || SQLI
10e77 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61  TE_OK!=(rc = rea
10e78 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73  d32bits(pJrnl, s
10e79 7a 4a 2d 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a  zJ-12, &cksum)).
10e7a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
10e7b 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  =(rc = sqlite3Os
10e7c 52 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67  Read(pJrnl, aMag
10e7d 69 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20  ic, 8, szJ-8)). 
10e7e 20 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67    || memcmp(aMag
10e7f 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  ic, aJournalMagi
10e80 63 2c 20 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49  c, 8).   || SQLI
10e81 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
10e82 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c  ite3OsRead(pJrnl
10e83 2c 20 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20  , zMaster, len, 
10e84 73 7a 4a 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29  szJ-16-len)).  )
10e85 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
10e86 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69  .  }..  /* See i
10e87 66 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 6d  f the checksum m
10e88 61 74 63 68 65 73 20 74 68 65 20 6d 61 73 74 65  atches the maste
10e89 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a  r journal name *
10e8a 2f 0a 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c  /.  for(u=0; u<l
10e8b 65 6e 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b  en; u++){.    ck
10e8c 73 75 6d 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75  sum -= zMaster[u
10e8d 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73  ];.  }.  if( cks
10e8e 75 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  um ){.    /* If 
10e8f 74 68 65 20 63 68 65 63 6b 73 75 6d 20 64 6f 65  the checksum doe
10e90 73 6e 27 74 20 61 64 64 20 75 70 2c 20 74 68 65  sn't add up, the
10e91 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66  n one or more of
10e92 20 74 68 65 20 64 69 73 6b 20 73 65 63 74 6f 72   the disk sector
10e93 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e  s.    ** contain
10e94 69 6e 67 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ing the master j
10e95 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20  ournal filename 
10e96 69 73 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68  is corrupted. Th
10e97 69 73 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20  is means.    ** 
10e98 64 65 66 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20  definitely roll 
10e99 62 61 63 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65  back, so just re
10e9a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61  turn SQLITE_OK a
10e9b 6e 64 20 72 65 70 6f 72 74 20 61 20 28 6e 75 6c  nd report a (nul
10e9c 29 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d  ).    ** master-
10e9d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
10e9e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e  ..    */.    len
10e9f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73   = 0;.  }.  zMas
10ea0 74 65 72 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b  ter[len] = '\0';
10ea1 0a 20 20 20 0a 20 20 72 65 74 75 72 6e 20 53 51  .   .  return SQ
10ea2 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
10ea3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6f 66 66  * Return the off
10ea4 73 65 74 20 6f 66 20 74 68 65 20 73 65 63 74 6f  set of the secto
10ea5 72 20 62 6f 75 6e 64 61 72 79 20 61 74 20 6f 72  r boundary at or
10ea6 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a   immediately .**
10ea7 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76   following the v
10ea8 61 6c 75 65 20 69 6e 20 70 50 61 67 65 72 2d 3e  alue in pPager->
10ea9 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75  journalOff, assu
10eaa 6d 69 6e 67 20 61 20 73 65 63 74 6f 72 20 0a 2a  ming a sector .*
10eab 2a 20 73 69 7a 65 20 6f 66 20 70 50 61 67 65 72  * size of pPager
10eac 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 62 79 74  ->sectorSize byt
10ead 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f  es..**.** i.e fo
10eae 72 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65 20  r a sector size 
10eaf 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20  of 512:.**.**   
10eb0 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66  Pager.journalOff
10eb1 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e            Return
10eb2 20 76 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d   value.**   ----
10eb3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10eb4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10eb5 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20  ---.**   0      
10eb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10eb7 20 20 20 30 0a 2a 2a 20 20 20 35 31 32 20 20 20     0.**   512   
10eb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10eb9 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 31 30 30      512.**   100
10eba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ebb 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 20 20 20         512.**   
10ebc 32 30 30 30 20 20 20 20 20 20 20 20 20 20 20 20  2000            
10ebd 20 20 20 20 20 20 20 20 20 20 32 30 34 38 0a 2a            2048.*
10ebe 2a 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34  * .*/.static i64
10ebf 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65   journalHdrOffse
10ec0 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  t(Pager *pPager)
10ec1 7b 0a 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d  {.  i64 offset =
10ec2 20 30 3b 0a 20 20 69 36 34 20 63 20 3d 20 70 50   0;.  i64 c = pP
10ec3 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
10ec4 3b 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20  ;.  if( c ){.   
10ec5 20 6f 66 66 73 65 74 20 3d 20 28 28 63 2d 31 29   offset = ((c-1)
10ec6 2f 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  /JOURNAL_HDR_SZ(
10ec7 70 50 61 67 65 72 29 20 2b 20 31 29 20 2a 20 4a  pPager) + 1) * J
10ec8 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
10ec9 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ager);.  }.  ass
10eca 65 72 74 28 20 6f 66 66 73 65 74 25 4a 4f 55 52  ert( offset%JOUR
10ecb 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
10ecc 72 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  r)==0 );.  asser
10ecd 74 28 20 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a  t( offset>=c );.
10ece 20 20 61 73 73 65 72 74 28 20 28 6f 66 66 73 65    assert( (offse
10ecf 74 2d 63 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52  t-c)<JOURNAL_HDR
10ed0 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20  _SZ(pPager) );. 
10ed1 20 72 65 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a   return offset;.
10ed2 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75  }../*.** The jou
10ed3 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62  rnal file must b
10ed4 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73  e open when this
10ed5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
10ed6 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  led..**.** This 
10ed7 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
10ed8 2d 6f 70 20 69 66 20 74 68 65 20 6a 6f 75 72 6e  -op if the journ
10ed9 61 6c 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20  al file has not 
10eda 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a  been written to.
10edb 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 63 75  ** within the cu
10edc 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
10edd 6e 20 28 69 2e 65 2e 20 69 66 20 50 61 67 65 72  n (i.e. if Pager
10ede 2e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e  .journalOff==0).
10edf 0a 2a 2a 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e  .**.** If doTrun
10ee0 63 61 74 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  cate is non-zero
10ee1 20 6f 72 20 74 68 65 20 50 61 67 65 72 2e 6a 6f   or the Pager.jo
10ee2 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76  urnalSizeLimit v
10ee3 61 72 69 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65  ariable is.** se
10ee4 74 20 74 6f 20 30 2c 20 74 68 65 6e 20 74 72 75  t to 0, then tru
10ee5 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  ncate the journa
10ee6 6c 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62  l file to zero b
10ee7 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74  ytes in size. Ot
10ee8 68 65 72 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f  herwise,.** zero
10ee9 20 74 68 65 20 32 38 2d 62 79 74 65 20 68 65 61   the 28-byte hea
10eea 64 65 72 20 61 74 20 74 68 65 20 73 74 61 72 74  der at the start
10eeb 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
10eec 66 69 6c 65 2e 20 49 6e 20 65 69 74 68 65 72 20  file. In either 
10eed 63 61 73 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65  case, .** if the
10eee 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e   pager is not in
10eef 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73   no-sync mode, s
10ef0 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ync the journal 
10ef1 66 69 6c 65 20 69 6d 6d 65 64 69 61 74 65 6c 79  file immediately
10ef2 20 0a 2a 2a 20 61 66 74 65 72 20 77 72 69 74 69   .** after writi
10ef3 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67  ng or truncating
10ef4 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61   it..**.** If Pa
10ef5 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c  ger.journalSizeL
10ef6 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 61  imit is set to a
10ef7 20 70 6f 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a   positive, non-z
10ef8 65 72 6f 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a  ero value, and.*
10ef9 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  * following the 
10efa 74 72 75 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65  truncation or ze
10efb 72 6f 69 6e 67 20 64 65 73 63 72 69 62 65 64 20  roing described 
10efc 61 62 6f 76 65 20 74 68 65 20 73 69 7a 65 20 6f  above the size o
10efd 66 20 74 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61  f the .** journa
10efe 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20  l file in bytes 
10eff 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
10f00 68 69 73 20 76 61 6c 75 65 2c 20 74 68 65 6e 20  his value, then 
10f01 74 72 75 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20  truncate the.** 
10f02 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20  journal file to 
10f03 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a  Pager.journalSiz
10f04 65 4c 69 6d 69 74 20 62 79 74 65 73 2e 20 54 68  eLimit bytes. Th
10f05 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64  e journal file d
10f06 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20  oes.** not need 
10f07 74 6f 20 62 65 20 73 79 6e 63 65 64 20 66 6f 6c  to be synced fol
10f08 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72  lowing this oper
10f09 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ation..**.** If 
10f0a 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
10f0b 72 73 2c 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63  rs, abandon proc
10f0c 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72  essing and retur
10f0d 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63  n the IO error c
10f0e 6f 64 65 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ode..** Otherwis
10f0f 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  e, return SQLITE
10f10 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  _OK..*/.static i
10f11 6e 74 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64  nt zeroJournalHd
10f12 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  r(Pager *pPager,
10f13 20 69 6e 74 20 64 6f 54 72 75 6e 63 61 74 65 29   int doTruncate)
10f14 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
10f15 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
10f16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f17 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
10f18 63 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  code */.  assert
10f19 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
10f1a 3e 6a 66 64 29 20 29 3b 0a 20 20 69 66 28 20 70  >jfd) );.  if( p
10f1b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
10f1c 66 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  f ){.    const i
10f1d 36 34 20 69 4c 69 6d 69 74 20 3d 20 70 50 61 67  64 iLimit = pPag
10f1e 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c  er->journalSizeL
10f1f 69 6d 69 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61  imit;    /* Loca
10f20 6c 20 63 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a  l cache of jsl *
10f21 2f 0a 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28  /..    IOTRACE((
10f22 22 4a 5a 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c  "JZEROHDR %p\n",
10f23 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 69 66   pPager)).    if
10f24 28 20 64 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20  ( doTruncate || 
10f25 69 4c 69 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20  iLimit==0 ){.   
10f26 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
10f27 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  sTruncate(pPager
10f28 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d  ->jfd, 0);.    }
10f29 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 61 74  else{.      stat
10f2a 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 65  ic const char ze
10f2b 72 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b  roHdr[28] = {0};
10f2c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
10f2d 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
10f2e 72 2d 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c  r->jfd, zeroHdr,
10f2f 20 73 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29   sizeof(zeroHdr)
10f30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
10f31 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
10f32 4b 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f  K && !pPager->no
10f33 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63  Sync ){.      rc
10f34 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
10f35 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51  (pPager->jfd, SQ
10f36 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e  LITE_SYNC_DATAON
10f37 4c 59 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  LY|pPager->sync_
10f38 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20  flags);.    }.. 
10f39 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f     /* At this po
10f3a 69 6e 74 20 74 68 65 20 74 72 61 6e 73 61 63 74  int the transact
10f3b 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
10f3c 20 62 75 74 20 74 68 65 20 77 72 69 74 65 20 6c   but the write l
10f3d 6f 63 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73  ock .    ** is s
10f3e 74 69 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65  till held on the
10f3f 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20   file. If there 
10f40 69 73 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20  is a size limit 
10f41 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a  configured for .
10f42 20 20 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69      ** the persi
10f43 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e  stent journal an
10f44 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  d the journal fi
10f45 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  le currently con
10f46 73 75 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a  sumes more.    *
10f47 2a 20 73 70 61 63 65 20 74 68 61 6e 20 74 68 61  * space than tha
10f48 74 20 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66  t limit allows f
10f49 6f 72 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20  or, truncate it 
10f4a 6e 6f 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f  now. There is no
10f4b 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20   need.    ** to 
10f4c 73 79 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f  sync the file fo
10f4d 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65  llowing this ope
10f4e 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  ration..    */. 
10f4f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
10f50 45 5f 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30  E_OK && iLimit>0
10f51 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a   ){.      i64 sz
10f52 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
10f53 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
10f54 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29  Pager->jfd, &sz)
10f55 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
10f56 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e  SQLITE_OK && sz>
10f57 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20  iLimit ){.      
10f58 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
10f59 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
10f5a 3e 6a 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20  >jfd, iLimit);. 
10f5b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
10f5c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
10f5d 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e  ./*.** The journ
10f5e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20  al file must be 
10f5f 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72  open when this r
10f60 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
10f61 2e 20 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68  . A journal.** h
10f62 65 61 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48  eader (JOURNAL_H
10f63 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20  DR_SZ bytes) is 
10f64 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
10f65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74   journal file at
10f66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
10f67 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  location..**.** 
10f68 54 68 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74  The format for t
10f69 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
10f6a 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  r is as follows:
10f6b 0a 2a 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d  .** - 8 bytes: M
10f6c 61 67 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67  agic identifying
10f6d 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e   journal format.
10f6e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e  .** - 4 bytes: N
10f6f 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73  umber of records
10f70 20 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20   in journal, or 
10f71 2d 31 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20  -1 no-sync mode 
10f72 69 73 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79  is on..** - 4 by
10f73 74 65 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62  tes: Random numb
10f74 65 72 20 75 73 65 64 20 66 6f 72 20 70 61 67 65  er used for page
10f75 20 68 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79   hash..** - 4 by
10f76 74 65 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74  tes: Initial dat
10f77 61 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74  abase page count
10f78 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20  ..** - 4 bytes: 
10f79 53 65 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64  Sector size used
10f7a 20 62 79 20 74 68 65 20 70 72 6f 63 65 73 73 20   by the process 
10f7b 74 68 61 74 20 77 72 6f 74 65 20 74 68 69 73 20  that wrote this 
10f7c 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20  journal..** - 4 
10f7d 62 79 74 65 73 3a 20 44 61 74 61 62 61 73 65 20  bytes: Database 
10f7e 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a  page size..** .*
10f7f 2a 20 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a  * Followed by (J
10f80 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20  OURNAL_HDR_SZ - 
10f81 32 38 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75  28) bytes of unu
10f82 73 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74  sed space..*/.st
10f83 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f  atic int writeJo
10f84 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a  urnalHdr(Pager *
10f85 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72  pPager){.  int r
10f86 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
10f87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10f88 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
10f89 0a 20 20 63 68 61 72 20 2a 7a 48 65 61 64 65 72  .  char *zHeader
10f8a 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53   = pPager->pTmpS
10f8b 70 61 63 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72  pace;  /* Tempor
10f8c 61 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74  ary space used t
10f8d 6f 20 62 75 69 6c 64 20 68 65 61 64 65 72 20 2a  o build header *
10f8e 2f 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72 20  /.  u32 nHeader 
10f8f 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
10f90 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  ze;     /* Size 
10f91 6f 66 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  of buffer pointe
10f92 64 20 74 6f 20 62 79 20 7a 48 65 61 64 65 72 20  d to by zHeader 
10f93 2a 2f 0a 20 20 75 33 32 20 6e 57 72 69 74 65 3b  */.  u32 nWrite;
10f94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f95 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
10f96 73 20 6f 66 20 68 65 61 64 65 72 20 73 65 63 74  s of header sect
10f97 6f 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  or written */.  
10f98 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20  int ii;         
10f99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f9a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
10f9b 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ter */..  assert
10f9c 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
10f9d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a  >jfd) );      /*
10f9e 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75   Journal file mu
10f9f 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a  st be open. */..
10fa0 20 20 69 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f    if( nHeader>JO
10fa1 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
10fa2 67 65 72 29 20 29 7b 0a 20 20 20 20 6e 48 65 61  ger) ){.    nHea
10fa3 64 65 72 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44  der = JOURNAL_HD
10fa4 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20  R_SZ(pPager);.  
10fa5 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  }..  /* If there
10fa6 20 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65   are active save
10fa7 70 6f 69 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f  points and any o
10fa8 66 20 74 68 65 6d 20 77 65 72 65 20 63 72 65 61  f them were crea
10fa9 74 65 64 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20  ted .  ** since 
10faa 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
10fab 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 77  journal header w
10fac 61 73 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61  as written, upda
10fad 74 65 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67  te the .  ** Pag
10fae 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72  erSavepoint.iHdr
10faf 4f 66 66 73 65 74 20 66 69 65 6c 64 73 20 6e 6f  Offset fields no
10fb0 77 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69  w..  */.  for(ii
10fb1 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e  =0; ii<pPager->n
10fb2 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29  Savepoint; ii++)
10fb3 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  {.    if( pPager
10fb4 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d  ->aSavepoint[ii]
10fb5 2e 69 48 64 72 4f 66 66 73 65 74 3d 3d 30 20 29  .iHdrOffset==0 )
10fb6 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
10fb7 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69  aSavepoint[ii].i
10fb8 48 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67  HdrOffset = pPag
10fb9 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a  er->journalOff;.
10fba 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61      }.  }..  pPa
10fbb 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20  ger->journalHdr 
10fbc 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
10fbd 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64  lOff = journalHd
10fbe 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b  rOffset(pPager);
10fbf 0a 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 65  .  memcpy(zHeade
10fc0 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  r, aJournalMagic
10fc1 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  , sizeof(aJourna
10fc2 6c 4d 61 67 69 63 29 29 3b 0a 0a 20 20 2f 2a 20  lMagic));..  /* 
10fc3 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20  .  ** Write the 
10fc4 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65  nRec Field - the
10fc5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20   number of page 
10fc6 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c  records that fol
10fc7 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f  low this.  ** jo
10fc8 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f  urnal header. No
10fc9 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20  rmally, zero is 
10fca 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20  written to this 
10fcb 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 69  value at this ti
10fcc 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74  me..  ** After t
10fcd 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 61  he records are a
10fce 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72  dded to the jour
10fcf 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75  nal (and the jou
10fd0 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20  rnal synced, .  
10fd1 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79  ** if in full-sy
10fd2 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65  nc mode), the ze
10fd3 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 65  ro is overwritte
10fd4 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 65 20  n with the true 
10fd5 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72  number.  ** of r
10fd6 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63  ecords (see sync
10fd7 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a  Journal())..  **
10fd8 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61  .  ** A faster a
10fd9 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f  lternative is to
10fda 20 77 72 69 74 65 20 30 78 46 46 46 46 46 46 46   write 0xFFFFFFF
10fdb 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69  F to the nRec fi
10fdc 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72  eld. When.  ** r
10fdd 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e  eading the journ
10fde 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 65  al this value te
10fdf 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73  lls SQLite to as
10fe0 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20  sume that the.  
10fe1 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a  ** rest of the j
10fe2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74  ournal file cont
10fe3 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20  ains valid page 
10fe4 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73  records. This as
10fe5 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73  sumption.  ** is
10fe6 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69   dangerous, as i
10fe7 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75  f a failure occu
10fe8 72 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74  rred whilst writ
10fe9 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ing to the journ
10fea 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20  al.  ** file it 
10feb 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65  may contain some
10fec 20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54   garbage data. T
10fed 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65  here are two sce
10fee 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72  narios.  ** wher
10fef 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20  e this risk can 
10ff0 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a  be ignored:.  **
10ff1 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74  .  **   * When t
10ff2 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e  he pager is in n
10ff3 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72  o-sync mode. Cor
10ff4 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c  ruption can foll
10ff5 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f  ow a.  **     po
10ff6 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74  wer failure in t
10ff7 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e  his case anyway.
10ff8 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57  .  **.  **   * W
10ff9 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49  hen the SQLITE_I
10ffa 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
10ffb 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68   flag is set. Th
10ffc 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20  is guarantees.  
10ffd 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62  **     that garb
10ffe 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65  age data is neve
10fff 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  r appended to th
11000 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
11001 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69    */.  assert( i
11002 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
11003 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53  ) || pPager->noS
11004 79 6e 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50  ync );.  if( (pP
11005 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c  ager->noSync) ||
11006 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61   (pPager->journa
11007 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
11008 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29  RNALMODE_MEMORY)
11009 0a 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f  .   || (sqlite3O
1100a 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
1100b 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
1100c 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  d)&SQLITE_IOCAP_
1100d 53 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20  SAFE_APPEND) .  
1100e 29 7b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73  ){.    put32bits
1100f 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66  (&zHeader[sizeof
11010 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d  (aJournalMagic)]
11011 2c 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20  , 0xffffffff);. 
11012 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 75 74 33   }else{.    put3
11013 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73  2bits(&zHeader[s
11014 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
11015 67 69 63 29 5d 2c 20 30 29 3b 0a 20 20 7d 0a 0a  gic)], 0);.  }..
11016 20 20 2f 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20    /* The random 
11017 63 68 65 63 6b 2d 68 61 73 68 20 69 6e 69 74 69  check-hash initi
11018 61 6c 69 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c  aliser */ .  sql
11019 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
1101a 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 63  sizeof(pPager->c
1101b 6b 73 75 6d 49 6e 69 74 29 2c 20 26 70 50 61 67  ksumInit), &pPag
1101c 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a  er->cksumInit);.
1101d 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65    put32bits(&zHe
1101e 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75  ader[sizeof(aJou
1101f 72 6e 61 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70  rnalMagic)+4], p
11020 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74  Pager->cksumInit
11021 29 3b 0a 20 20 2f 2a 20 54 68 65 20 69 6e 69 74  );.  /* The init
11022 69 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a  ial database siz
11023 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73  e */.  put32bits
11024 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66  (&zHeader[sizeof
11025 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b  (aJournalMagic)+
11026 38 5d 2c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72  8], pPager->dbOr
11027 69 67 53 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68  igSize);.  /* Th
11028 65 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72  e assumed sector
11029 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70   size for this p
1102a 72 6f 63 65 73 73 20 2a 2f 0a 20 20 70 75 74 33  rocess */.  put3
1102b 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73  2bits(&zHeader[s
1102c 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
1102d 67 69 63 29 2b 31 32 5d 2c 20 70 50 61 67 65 72  gic)+12], pPager
1102e 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a  ->sectorSize);..
1102f 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 73 69    /* The page si
11030 7a 65 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74  ze */.  put32bit
11031 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f  s(&zHeader[sizeo
11032 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
11033 2b 31 36 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61  +16], pPager->pa
11034 67 65 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49  geSize);..  /* I
11035 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20  nitializing the 
11036 74 61 69 6c 20 6f 66 20 74 68 65 20 62 75 66 66  tail of the buff
11037 65 72 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  er is not necess
11038 61 72 79 2e 20 20 45 76 65 72 79 74 68 69 6e 67  ary.  Everything
11039 0a 20 20 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64  .  ** works find
1103a 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   if the followin
1103b 67 20 6d 65 6d 73 65 74 28 29 20 69 73 20 6f 6d  g memset() is om
1103c 69 74 74 65 64 2e 20 20 42 75 74 20 69 6e 69 74  itted.  But init
1103d 69 61 6c 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68  ializing.  ** th
1103e 65 20 6d 65 6d 6f 72 79 20 70 72 65 76 65 6e 74  e memory prevent
1103f 73 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20  s valgrind from 
11040 63 6f 6d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20  complaining, so 
11041 77 65 20 61 72 65 20 77 69 6c 6c 69 6e 67 20 74  we are willing t
11042 6f 0a 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20  o.  ** take the 
11043 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e  performance hit.
11044 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  .  */.  memset(&
11045 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
11046 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30  JournalMagic)+20
11047 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 6e  ], 0,.         n
11048 48 65 61 64 65 72 2d 28 73 69 7a 65 6f 66 28 61  Header-(sizeof(a
11049 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30  JournalMagic)+20
1104a 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65  ));..  /* In the
1104b 6f 72 79 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20  ory, it is only 
1104c 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69  necessary to wri
1104d 74 65 20 74 68 65 20 32 38 20 62 79 74 65 73 20  te the 28 bytes 
1104e 74 68 61 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a  that the .  ** j
1104f 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 63 6f  ournal header co
11050 6e 73 75 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f  nsumes to the jo
11051 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e  urnal file here.
11052 20 54 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20   Then increment 
11053 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e  the .  ** Pager.
11054 6a 6f 75 72 6e 61 6c 4f 66 66 20 76 61 72 69 61  journalOff varia
11055 62 6c 65 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48  ble by JOURNAL_H
11056 44 52 5f 53 5a 20 73 6f 20 74 68 61 74 20 74 68  DR_SZ so that th
11057 65 20 6e 65 78 74 20 0a 20 20 2a 2a 20 72 65 63  e next .  ** rec
11058 6f 72 64 20 69 73 20 77 72 69 74 74 65 6e 20 74  ord is written t
11059 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
1105a 73 65 63 74 6f 72 20 28 6c 65 61 76 69 6e 67 20  sector (leaving 
1105b 61 20 67 61 70 20 69 6e 20 74 68 65 20 66 69 6c  a gap in the fil
1105c 65 0a 20 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c  e.  ** that will
1105d 20 62 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 66   be implicitly f
1105e 69 6c 6c 65 64 20 69 6e 20 62 79 20 74 68 65 20  illed in by the 
1105f 4f 53 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48  OS)..  **.  ** H
11060 6f 77 65 76 65 72 20 69 74 20 68 61 73 20 62 65  owever it has be
11061 65 6e 20 64 69 73 63 6f 76 65 72 65 64 20 74 68  en discovered th
11062 61 74 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65  at on some syste
11063 6d 73 20 74 68 69 73 20 70 61 74 74 65 72 6e 20  ms this pattern 
11064 63 61 6e 20 0a 20 20 2a 2a 20 62 65 20 73 69 67  can .  ** be sig
11065 6e 69 66 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65  nificantly slowe
11066 72 20 74 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75  r than contiguou
11067 73 6c 79 20 77 72 69 74 69 6e 67 20 64 61 74 61  sly writing data
11068 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 0a 20 20   to the file,.  
11069 2a 2a 20 65 76 65 6e 20 69 66 20 74 68 61 74 20  ** even if that 
1106a 6d 65 61 6e 73 20 65 78 70 6c 69 63 69 74 6c 79  means explicitly
1106b 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f   writing data to
1106c 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20   the block of . 
1106d 20 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52   ** (JOURNAL_HDR
1106e 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20  _SZ - 28) bytes 
1106f 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65  that will not be
11070 20 75 73 65 64 2e 20 53 6f 20 74 68 61 74 20 69   used. So that i
11071 73 20 77 68 61 74 0a 20 20 2a 2a 20 69 73 20 64  s what.  ** is d
11072 6f 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  one. .  **.  ** 
11073 54 68 65 20 6c 6f 6f 70 20 69 73 20 72 65 71 75  The loop is requ
11074 69 72 65 64 20 68 65 72 65 20 69 6e 20 63 61 73  ired here in cas
11075 65 20 74 68 65 20 73 65 63 74 6f 72 2d 73 69 7a  e the sector-siz
11076 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  e is larger than
11077 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61 62   the .  ** datab
11078 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e 20 53  ase page size. S
11079 69 6e 63 65 20 74 68 65 20 7a 48 65 61 64 65 72  ince the zHeader
1107a 20 62 75 66 66 65 72 20 69 73 20 6f 6e 6c 79 20   buffer is only 
1107b 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 0a 20  Pager.pageSize. 
1107c 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a   ** bytes in siz
1107d 65 2c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  e, more than one
1107e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
1107f 4f 73 57 72 69 74 65 28 29 20 6d 61 79 20 62 65  OsWrite() may be
11080 20 72 65 71 75 69 72 65 64 0a 20 20 2a 2a 20 74   required.  ** t
11081 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 65  o populate the e
11082 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65  ntire journal he
11083 61 64 65 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a  ader sector..  *
11084 2f 20 0a 20 20 66 6f 72 28 6e 57 72 69 74 65 3d  / .  for(nWrite=
11085 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
11086 26 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c  &&nWrite<JOURNAL
11087 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b  _HDR_SZ(pPager);
11088 20 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64 65 72   nWrite+=nHeader
11089 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28  ){.    IOTRACE((
1108a 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25 64  "JHDR %p %lld %d
1108b 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61  \n", pPager, pPa
1108c 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c  ger->journalHdr,
1108d 20 6e 48 65 61 64 65 72 29 29 0a 20 20 20 20 72   nHeader)).    r
1108e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
1108f 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
11090 7a 48 65 61 64 65 72 2c 20 6e 48 65 61 64 65 72  zHeader, nHeader
11091 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  , pPager->journa
11092 6c 4f 66 66 29 3b 0a 20 20 20 20 70 50 61 67 65  lOff);.    pPage
11093 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d  r->journalOff +=
11094 20 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20   nHeader;.  }.. 
11095 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
11096 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c  *.** The journal
11097 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70   file must be op
11098 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69 73 20  en when this is 
11099 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61  called. A journa
1109a 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a  l header file.**
1109b 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a   (JOURNAL_HDR_SZ
1109c 20 62 79 74 65 73 29 20 69 73 20 72 65 61 64 20   bytes) is read 
1109d 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74  from the current
1109e 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74 68 65   location in the
1109f 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65   journal.** file
110a0 2e 20 54 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  . The current lo
110a1 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f  cation in the jo
110a2 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 67 69  urnal file is gi
110a3 76 65 6e 20 62 79 0a 2a 2a 20 70 50 61 67 65 72  ven by.** pPager
110a4 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65  ->journalOff. Se
110a5 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65  e comments above
110a6 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a   function writeJ
110a7 6f 75 72 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a  ournalHdr() for.
110a8 2a 2a 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e  ** a description
110a9 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
110aa 68 65 61 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a  header format..*
110ab 2a 0a 2a 2a 20 49 66 20 74 68 65 20 68 65 61 64  *.** If the head
110ac 65 72 20 69 73 20 72 65 61 64 20 73 75 63 63 65  er is read succe
110ad 73 73 66 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20  ssfully, *pNRec 
110ae 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
110af 6d 62 65 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20  mber of.** page 
110b0 72 65 63 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e  records followin
110b1 67 20 74 68 69 73 20 68 65 61 64 65 72 20 61 6e  g this header an
110b2 64 20 2a 70 44 62 53 69 7a 65 20 69 73 20 73 65  d *pDbSize is se
110b3 74 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66  t to the size of
110b4 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
110b5 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e   before the tran
110b6 73 61 63 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69  saction began, i
110b7 6e 20 70 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70  n pages. Also, p
110b8 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74  Pager->cksumInit
110b9 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68  .** is set to th
110ba 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f  e value read fro
110bb 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  m the journal he
110bc 61 64 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20  ader. SQLITE_OK 
110bd 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69  is returned.** i
110be 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a  n this case..**.
110bf 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  ** If the journa
110c0 6c 20 68 65 61 64 65 72 20 66 69 6c 65 20 61 70  l header file ap
110c1 70 65 61 72 73 20 74 6f 20 62 65 20 63 6f 72 72  pears to be corr
110c2 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f  upted, SQLITE_DO
110c3 4e 45 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65  NE is.** returne
110c4 64 20 61 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64  d and *pNRec and
110c5 20 2a 50 44 62 53 69 7a 65 20 61 72 65 20 75 6e   *PDbSize are un
110c6 64 65 66 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55  defined.  If JOU
110c7 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65  RNAL_HDR_SZ byte
110c8 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72  s.** cannot be r
110c9 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  ead from the jou
110ca 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72  rnal file an err
110cb 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
110cc 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
110cd 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64  nt readJournalHd
110ce 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  r(.  Pager *pPag
110cf 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
110d0 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63    /* Pager objec
110d1 74 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e  t */.  i64 journ
110d2 61 6c 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20  alSize,         
110d3 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
110d4 68 65 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20  he open journal 
110d5 66 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f  file in bytes */
110d6 0a 20 20 75 33 32 20 2a 70 4e 52 65 63 2c 20 20  .  u32 *pNRec,  
110d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110d8 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 20 72 65  /* OUT: Value re
110d9 61 64 20 66 72 6f 6d 20 74 68 65 20 6e 52 65 63  ad from the nRec
110da 20 66 69 65 6c 64 20 2a 2f 0a 20 20 75 33 32 20   field */.  u32 
110db 2a 70 44 62 53 69 7a 65 20 20 20 20 20 20 20 20  *pDbSize        
110dc 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
110dd 20 56 61 6c 75 65 20 6f 66 20 6f 72 69 67 69 6e   Value of origin
110de 61 6c 20 64 61 74 61 62 61 73 65 20 73 69 7a 65  al database size
110df 20 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a 20 20 69   field */.){.  i
110e0 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
110e1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
110e2 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
110e3 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4d  unsigned char aM
110e4 61 67 69 63 5b 38 5d 3b 20 20 20 20 20 2f 2a 20  agic[8];     /* 
110e5 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64  A buffer to hold
110e6 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 65   the magic heade
110e7 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f  r */.  i64 iHdrO
110e8 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ff;             
110e9 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66      /* Offset of
110ea 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
110eb 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 0a 20  being read */.. 
110ec 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
110ed 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20  pPager->jfd) ); 
110ee 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20       /* Journal 
110ef 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65  file must be ope
110f0 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41 64 76 61  n. */..  /* Adva
110f1 6e 63 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61  nce Pager.journa
110f2 6c 4f 66 66 20 74 6f 20 74 68 65 20 73 74 61 72  lOff to the star
110f3 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 73 65  t of the next se
110f4 63 74 6f 72 2e 20 49 66 20 74 68 65 0a 20 20 2a  ctor. If the.  *
110f5 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  * journal file i
110f6 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20  s too small for 
110f7 74 68 65 72 65 20 74 6f 20 62 65 20 61 20 68 65  there to be a he
110f8 61 64 65 72 20 73 74 6f 72 65 64 20 61 74 20 74  ader stored at t
110f9 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 2c 20  his.  ** point, 
110fa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
110fb 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65  NE..  */.  pPage
110fc 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
110fd 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74  journalHdrOffset
110fe 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20  (pPager);.  if( 
110ff 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11100 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  ff+JOURNAL_HDR_S
11101 5a 28 70 50 61 67 65 72 29 20 3e 20 6a 6f 75 72  Z(pPager) > jour
11102 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 72  nalSize ){.    r
11103 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e  eturn SQLITE_DON
11104 45 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66  E;.  }.  iHdrOff
11105 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
11106 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 52 65 61  alOff;..  /* Rea
11107 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 38  d in the first 8
11108 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f   bytes of the jo
11109 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 49 66  urnal header. If
1110a 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6d 61 74   they do not mat
1110b 63 68 0a 20 20 2a 2a 20 74 68 65 20 20 6d 61 67  ch.  ** the  mag
1110c 69 63 20 73 74 72 69 6e 67 20 66 6f 75 6e 64 20  ic string found 
1110d 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
1110e 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 68 65 61  each journal hea
1110f 64 65 72 2c 20 72 65 74 75 72 6e 0a 20 20 2a 2a  der, return.  **
11110 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 49 66   SQLITE_DONE. If
11111 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
11112 75 72 73 2c 20 72 65 74 75 72 6e 20 61 6e 20 65  urs, return an e
11113 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68 65 72  rror code. Other
11114 77 69 73 65 2c 0a 20 20 2a 2a 20 70 72 6f 63 65  wise,.  ** proce
11115 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  ed..  */.  rc = 
11116 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50  sqlite3OsRead(pP
11117 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69  ager->jfd, aMagi
11118 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63  c, sizeof(aMagic
11119 29 2c 20 69 48 64 72 4f 66 66 29 3b 0a 20 20 69  ), iHdrOff);.  i
1111a 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
1111b 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66  urn rc;.  }.  if
1111c 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c  ( memcmp(aMagic,
1111d 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
1111e 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21  sizeof(aMagic))!
1111f 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
11120 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
11121 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  }..  /* Read the
11122 20 66 69 72 73 74 20 74 68 72 65 65 20 33 32 2d   first three 32-
11123 62 69 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68  bit fields of th
11124 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
11125 3a 20 54 68 65 20 6e 52 65 63 0a 20 20 2a 2a 20  : The nRec.  ** 
11126 66 69 65 6c 64 2c 20 74 68 65 20 63 68 65 63 6b  field, the check
11127 73 75 6d 2d 69 6e 69 74 69 61 6c 69 7a 65 72 20  sum-initializer 
11128 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
11129 20 73 69 7a 65 20 61 74 20 74 68 65 20 73 74 61   size at the sta
1112a 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74  rt.  ** of the t
1112b 72 61 6e 73 61 63 74 69 6f 6e 2e 20 52 65 74 75  ransaction. Retu
1112c 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  rn an error code
1112d 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65   if anything goe
1112e 73 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20  s wrong..  */.  
1112f 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
11130 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
11131 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
11132 72 4f 66 66 2b 38 2c 20 70 4e 52 65 63 29 29 0a  rOff+8, pNRec)).
11133 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
11134 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74  =(rc = read32bit
11135 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69  s(pPager->jfd, i
11136 48 64 72 4f 66 66 2b 31 32 2c 20 26 70 50 61 67  HdrOff+12, &pPag
11137 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 29 0a  er->cksumInit)).
11138 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
11139 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74  =(rc = read32bit
1113a 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69  s(pPager->jfd, i
1113b 48 64 72 4f 66 66 2b 31 36 2c 20 70 44 62 53 69  HdrOff+16, pDbSi
1113c 7a 65 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65  ze)).  ){.    re
1113d 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
1113e 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
1113f 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20  nalOff==0 ){.   
11140 20 75 33 32 20 69 50 61 67 65 53 69 7a 65 3b 20   u32 iPageSize; 
11141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11142 20 50 61 67 65 2d 73 69 7a 65 20 66 69 65 6c 64   Page-size field
11143 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64   of journal head
11144 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 69 53  er */.    u32 iS
11145 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20  ectorSize;      
11146 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74 6f 72         /* Sector
11147 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a  -size field of j
11148 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f  ournal header */
11149 0a 20 20 20 20 75 31 36 20 69 50 61 67 65 53 69  .    u16 iPageSi
1114a 7a 65 31 36 3b 20 20 20 20 20 20 20 20 20 20 20  ze16;           
1114b 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 50 61    /* Copy of iPa
1114c 67 65 53 69 7a 65 20 69 6e 20 31 36 2d 62 69 74  geSize in 16-bit
1114d 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20   variable */..  
1114e 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61    /* Read the pa
1114f 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74  ge-size and sect
11150 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e 61 6c 20  or-size journal 
11151 68 65 61 64 65 72 20 66 69 65 6c 64 73 2e 20 2a  header fields. *
11152 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  /.    if( SQLITE
11153 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33  _OK!=(rc = read3
11154 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
11155 64 2c 20 69 48 64 72 4f 66 66 2b 32 30 2c 20 26  d, iHdrOff+20, &
11156 69 53 65 63 74 6f 72 53 69 7a 65 29 29 0a 20 20  iSectorSize)).  
11157 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
11158 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74  =(rc = read32bit
11159 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69  s(pPager->jfd, i
1115a 48 64 72 4f 66 66 2b 32 34 2c 20 26 69 50 61 67  HdrOff+24, &iPag
1115b 65 53 69 7a 65 29 29 0a 20 20 20 20 29 7b 0a 20  eSize)).    ){. 
1115c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1115d 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68      }..    /* Ch
1115e 65 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c  eck that the val
1115f 75 65 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68  ues read from th
11160 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20  e page-size and 
11161 73 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c  sector-size fiel
11162 64 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 77 69  ds.    ** are wi
11163 74 68 69 6e 20 72 61 6e 67 65 2e 20 54 6f 20 62  thin range. To b
11164 65 20 27 69 6e 20 72 61 6e 67 65 27 2c 20 62 6f  e 'in range', bo
11165 74 68 20 76 61 6c 75 65 73 20 6e 65 65 64 20 74  th values need t
11166 6f 20 62 65 20 61 20 70 6f 77 65 72 0a 20 20 20  o be a power.   
11167 20 2a 2a 20 6f 66 20 74 77 6f 20 67 72 65 61 74   ** of two great
11168 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
11169 20 74 6f 20 35 31 32 2c 20 61 6e 64 20 6e 6f 74   to 512, and not
1116a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
1116b 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70  eir .    ** resp
1116c 65 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74  ective compile t
1116d 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69  ime maximum limi
1116e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ts..    */.    i
1116f 66 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32  f( iPageSize<512
11170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11171 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65    || iSectorSize
11172 3c 35 31 32 0a 20 20 20 20 20 7c 7c 20 69 50 61  <512.     || iPa
11173 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41  geSize>SQLITE_MA
11174 58 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69  X_PAGE_SIZE || i
11175 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53  SectorSize>MAX_S
11176 45 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20  ECTOR_SIZE.     
11177 7c 7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31  || ((iPageSize-1
11178 29 26 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20  )&iPageSize)!=0 
11179 20 20 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69    || ((iSectorSi
1117a 7a 65 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a  ze-1)&iSectorSiz
1117b 65 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20  e)!=0 .    ){.  
1117c 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69      /* If the ei
1117d 74 68 65 72 20 74 68 65 20 70 61 67 65 2d 73 69  ther the page-si
1117e 7a 65 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a  ze or sector-siz
1117f 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e in the journal
11180 2d 68 65 61 64 65 72 20 69 73 20 0a 20 20 20 20  -header is .    
11181 20 20 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68    ** invalid, th
11182 65 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 74  en the process t
11183 68 61 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f  hat wrote the jo
11184 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73  urnal-header mus
11185 74 20 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a  t have .      **
11186 20 63 72 61 73 68 65 64 20 62 65 66 6f 72 65 20   crashed before 
11187 74 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73  the header was s
11188 79 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63  ynced. In this c
11189 61 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67  ase stop reading
1118a 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a   .      ** the j
1118b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65  ournal file here
1118c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1118d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
1118e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ONE;.    }..    
1118f 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 70 61  /* Update the pa
11190 67 65 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68  ge-size to match
11191 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20   the value read 
11192 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c  from the journal
11193 2e 20 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20  . .    ** Use a 
11194 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f  testcase() macro
11195 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
11196 61 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  at malloc failur
11197 65 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a  e within .    **
11198 20 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a   PagerSetPagesiz
11199 65 28 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20  e() is tested.. 
1119a 20 20 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53     */.    iPageS
1119b 69 7a 65 31 36 20 3d 20 28 75 31 36 29 69 50 61  ize16 = (u16)iPa
1119c 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d  geSize;.    rc =
1119d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
1119e 50 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c  Pagesize(pPager,
1119f 20 26 69 50 61 67 65 53 69 7a 65 31 36 29 3b 0a   &iPageSize16);.
111a0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
111a1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
111a2 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
111a3 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 50 61 67  QLITE_OK || iPag
111a4 65 53 69 7a 65 31 36 3d 3d 28 75 31 36 29 69 50  eSize16==(u16)iP
111a5 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20 20 20 20  ageSize );..    
111a6 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 73  /* Update the as
111a7 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a  sumed sector-siz
111a8 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76  e to match the v
111a9 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20  alue used by .  
111aa 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73    ** the process
111ab 20 74 68 61 74 20 63 72 65 61 74 65 64 20 74 68   that created th
111ac 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74  is journal. If t
111ad 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a  his journal was.
111ae 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62      ** created b
111af 79 20 61 20 70 72 6f 63 65 73 73 20 6f 74 68 65  y a process othe
111b0 72 20 74 68 61 6e 20 74 68 69 73 20 6f 6e 65 2c  r than this one,
111b1 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
111b2 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 62 65 69  ne.    ** is bei
111b3 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  ng called from w
111b4 69 74 68 69 6e 20 70 61 67 65 72 5f 70 6c 61 79  ithin pager_play
111b5 62 61 63 6b 28 29 2e 20 54 68 65 20 6c 6f 63 61  back(). The loca
111b6 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20 6f  l value.    ** o
111b7 66 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69  f Pager.sectorSi
111b8 7a 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 61  ze is restored a
111b9 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 61  t the end of tha
111ba 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 20 20 2a  t routine..    *
111bb 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65  /.    pPager->se
111bc 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 65 63 74  ctorSize = iSect
111bd 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a 20 20 70  orSize;.  }..  p
111be 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
111bf 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  f += JOURNAL_HDR
111c0 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 72  _SZ(pPager);.  r
111c1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
111c2 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 75  .** Write the su
111c3 70 70 6c 69 65 64 20 6d 61 73 74 65 72 20 6a 6f  pplied master jo
111c4 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20  urnal name into 
111c5 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
111c6 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a 20 70 50   for pager.** pP
111c7 61 67 65 72 20 61 74 20 74 68 65 20 63 75 72 72  ager at the curr
111c8 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 20 54 68  ent location. Th
111c9 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
111ca 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 74 68   name must be th
111cb 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 6e 67 20  e last.** thing 
111cc 77 72 69 74 74 65 6e 20 74 6f 20 61 20 6a 6f 75  written to a jou
111cd 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68  rnal file. If th
111ce 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 66 75  e pager is in fu
111cf 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 74 68  ll-sync mode, th
111d0 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  e.** journal fil
111d1 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
111d2 61 64 76 61 6e 63 65 64 20 74 6f 20 74 68 65 20  advanced to the 
111d3 6e 65 78 74 20 73 65 63 74 6f 72 20 62 6f 75 6e  next sector boun
111d4 64 61 72 79 20 62 65 66 6f 72 65 0a 2a 2a 20 61  dary before.** a
111d5 6e 79 74 68 69 6e 67 20 69 73 20 77 72 69 74 74  nything is writt
111d6 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 69  en. The format i
111d7 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 34 20 62  s:.**.**   + 4 b
111d8 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4a 5f 50  ytes: PAGER_MJ_P
111d9 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e 20 62 79  GNO..**   + N by
111da 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75 72  tes: Master jour
111db 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20  nal filename in 
111dc 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b 20 34 20  utf-8..**   + 4 
111dd 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e 67 74 68  bytes: N (length
111de 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   of master journ
111df 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79 74 65 73  al name in bytes
111e0 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  , no nul-termina
111e1 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 34 20 62  tor)..**   + 4 b
111e2 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f 75  ytes: Master jou
111e3 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b 73  rnal name checks
111e4 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 62 79 74  um..**   + 8 byt
111e5 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  es: aJournalMagi
111e6 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  c[]..**.** The m
111e7 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61  aster journal pa
111e8 67 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 74  ge checksum is t
111e9 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 62 79  he sum of the by
111ea 74 65 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65  tes in the maste
111eb 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  r.** journal nam
111ec 65 2c 20 77 68 65 72 65 20 65 61 63 68 20 62 79  e, where each by
111ed 74 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 65  te is interprete
111ee 64 20 61 73 20 61 20 73 69 67 6e 65 64 20 38 2d  d as a signed 8-
111ef 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a  bit integer..**.
111f0 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 20 69 73  ** If zMaster is
111f1 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
111f2 28 6f 63 63 75 72 73 20 66 6f 72 20 61 20 73 69  (occurs for a si
111f3 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
111f4 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a 2a 2a 20  ansaction), .** 
111f5 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e  this call is a n
111f6 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
111f7 69 6e 74 20 77 72 69 74 65 4d 61 73 74 65 72 4a  int writeMasterJ
111f8 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50  ournal(Pager *pP
111f9 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 61 72  ager, const char
111fa 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e   *zMaster){.  in
111fb 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
111fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
111fd 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
111fe 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 3b 20  .  int nMaster; 
111ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11200 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
11201 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20   string zMaster 
11202 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66  */.  i64 iHdrOff
11203 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11204 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
11205 6f 66 20 68 65 61 64 65 72 20 69 6e 20 6a 6f 75  of header in jou
11206 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69  rnal file */.  i
11207 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 20 20 20  64 jrnlSize;    
11208 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11209 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e  /* Size of journ
1120a 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20  al file on disk 
1120b 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d  */.  u32 cksum =
1120c 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1120d 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75        /* Checksu
1120e 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73  m of string zMas
1120f 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 21 7a  ter */..  if( !z
11210 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67 65 72  Master || pPager
11211 2d 3e 73 65 74 4d 61 73 74 65 72 0a 20 20 20 7c  ->setMaster.   |
11212 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  | pPager->journa
11213 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
11214 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
11215 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a  .   || pPager->j
11216 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
11217 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
11218 46 20 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  F .  ){.    retu
11219 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1121a 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d  }.  pPager->setM
1121b 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 61 73 73  aster = 1;.  ass
1121c 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
1121d 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f  er->jfd) );..  /
1121e 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20  * Calculate the 
1121f 6c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20  length in bytes 
11220 61 6e 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d  and the checksum
11221 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20   of zMaster */. 
11222 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d 30 3b 20   for(nMaster=0; 
11223 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d  zMaster[nMaster]
11224 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b 0a 20 20  ; nMaster++){.  
11225 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d 61 73 74    cksum += zMast
11226 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a 20 20 7d  er[nMaster];.  }
11227 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 75 6c  ..  /* If in ful
11228 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64 76  l-sync mode, adv
11229 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74  ance to the next
1122a 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62 65 66   disk sector bef
1122b 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20 2a 2a  ore writing.  **
1122c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1122d 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20 69  nal name. This i
1122e 73 20 69 6e 20 63 61 73 65 20 74 68 65 20 70 72  s in case the pr
1122f 65 76 69 6f 75 73 20 70 61 67 65 20 77 72 69 74  evious page writ
11230 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20  ten to.  ** the 
11231 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c 72 65  journal has alre
11232 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2e  ady been synced.
11233 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  .  */.  if( pPag
11234 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b 0a  er->fullSync ){.
11235 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
11236 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c  nalOff = journal
11237 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72  HdrOffset(pPager
11238 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72 4f 66 66  );.  }.  iHdrOff
11239 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
1123a 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 57 72 69  alOff;..  /* Wri
1123b 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
1123c 75 72 6e 61 6c 20 64 61 74 61 20 74 6f 20 74 68  urnal data to th
1123d 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75  e end of the jou
1123e 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 0a 20 20  rnal file. If.  
1123f 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  ** an error occu
11240 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65  rs, return the e
11241 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65  rror code to the
11242 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f 0a 20 20   caller..  */.  
11243 69 66 28 20 28 30 20 21 3d 20 28 72 63 20 3d 20  if( (0 != (rc = 
11244 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
11245 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
11246 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  , PAGER_MJ_PGNO(
11247 70 50 61 67 65 72 29 29 29 29 0a 20 20 20 7c 7c  pPager)))).   ||
11248 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c   (0 != (rc = sql
11249 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
1124a 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72  er->jfd, zMaster
1124b 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48 64 72 4f  , nMaster, iHdrO
1124c 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c 20 28 30  ff+4))).   || (0
1124d 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33   != (rc = write3
1124e 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
1124f 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61  d, iHdrOff+4+nMa
11250 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 29 29 29  ster, nMaster)))
11251 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63  .   || (0 != (rc
11252 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
11253 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72  Pager->jfd, iHdr
11254 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 34 2c  Off+4+nMaster+4,
11255 20 63 6b 73 75 6d 29 29 29 0a 20 20 20 7c 7c 20   cksum))).   || 
11256 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69  (0 != (rc = sqli
11257 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
11258 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 6e 61 6c  r->jfd, aJournal
11259 4d 61 67 69 63 2c 20 38 2c 20 69 48 64 72 4f 66  Magic, 8, iHdrOf
1125a 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 29 29 29  f+4+nMaster+8)))
1125b 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
1125c 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65   rc;.  }.  pPage
1125d 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d  r->journalOff +=
1125e 20 28 6e 4d 61 73 74 65 72 2b 32 30 29 3b 0a 20   (nMaster+20);. 
1125f 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
11260 63 20 3d 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53  c = !pPager->noS
11261 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ync;..  /* If th
11262 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 70 65  e pager is in pe
11263 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 20  ristent-journal 
11264 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 70  mode, then the p
11265 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a 20 6a 6f  hysical .  ** jo
11266 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 79 20 65  urnal-file may e
11267 78 74 65 6e 64 20 70 61 73 74 20 74 68 65 20 65  xtend past the e
11268 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  nd of the master
11269 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 0a 20 20  -journal name.  
1126a 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 73 20 6f  ** and 8 bytes o
1126b 66 20 6d 61 67 69 63 20 64 61 74 61 20 6a 75 73  f magic data jus
1126c 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  t written to the
1126d 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 0a   file. This is .
1126e 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 73 20 62    ** dangerous b
1126f 65 63 61 75 73 65 20 74 68 65 20 63 6f 64 65 20  ecause the code 
11270 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 68 6f  to rollback a ho
11271 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20  t-journal file. 
11272 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20   ** will not be 
11273 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 74 68 65  able to find the
11274 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
11275 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e  name to determin
11276 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 65 72 20  e .  ** whether 
11277 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e  or not the journ
11278 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 20 2a 2a  al is hot. .  **
11279 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 20 74 68  .  ** Easiest th
1127a 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 74 68 69  ing to do in thi
1127b 73 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 6f  s scenario is to
1127c 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 6a 6f   truncate the jo
1127d 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65  urnal .  ** file
1127e 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 65 64   to the required
1127f 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a 20 20 69   size..  */ .  i
11280 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  f( SQLITE_OK==(r
11281 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
11282 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66  eSize(pPager->jf
11283 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 29 0a 20  d, &jrnlSize)). 
11284 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e 70 50    && jrnlSize>pP
11285 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11286 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  .  ){.    rc = s
11287 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
11288 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
11289 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
1128a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1128b 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  rc;.}../*.** Fin
1128c 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20  d a page in the 
1128d 68 61 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e  hash table given
1128e 20 69 74 73 20 70 61 67 65 20 6e 75 6d 62 65 72   its page number
1128f 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f  . Return.** a po
11290 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67  inter to the pag
11291 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65  e or NULL if the
11292 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20   requested page 
11293 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c 72 65 61  is not .** alrea
11294 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2f  dy in memory..*/
11295 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70  .static PgHdr *p
11296 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 61 67 65  ager_lookup(Page
11297 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20  r *pPager, Pgno 
11298 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a  pgno){.  PgHdr *
11299 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
1129a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1129b 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20  turn value */.. 
1129c 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f   /* It is not po
1129d 73 73 69 62 6c 65 20 66 6f 72 20 61 20 63 61 6c  ssible for a cal
1129e 6c 20 74 6f 20 50 63 61 63 68 65 46 65 74 63 68  l to PcacheFetch
1129f 28 29 20 77 69 74 68 20 63 72 65 61 74 65 46 6c  () with createFl
112a0 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a 20 66 61  ag==0 to.  ** fa
112a1 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 61 74 74  il, since no att
112a2 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65  empt to allocate
112a3 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20   dynamic memory 
112a4 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e 0a 20 20  will be made..  
112a5 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  */.  (void)sqlit
112a6 65 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50  e3PcacheFetch(pP
112a7 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
112a8 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72  gno, 0, &p);.  r
112a9 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
112aa 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 70 61 67  * Unless the pag
112ab 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73  er is in error-s
112ac 74 61 74 65 2c 20 64 69 73 63 61 72 64 20 61 6c  tate, discard al
112ad 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  l in-memory page
112ae 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 70 61 67  s. If.** the pag
112af 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 2d 73  er is in error-s
112b0 74 61 74 65 2c 20 74 68 65 6e 20 74 68 69 73 20  tate, then this 
112b1 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  call is a no-op.
112b2 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 57 68 79  .**.** TODO: Why
112b3 20 63 61 6e 20 77 65 20 6e 6f 74 20 72 65 73 65   can we not rese
112b4 74 20 74 68 65 20 70 61 67 65 72 20 77 68 69 6c  t the pager whil
112b5 65 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65  e in error state
112b6 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ?.*/.static void
112b7 20 70 61 67 65 72 5f 72 65 73 65 74 28 50 61 67   pager_reset(Pag
112b8 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
112b9 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 50  f( SQLITE_OK==pP
112ba 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b  ager->errCode ){
112bb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b  .    sqlite3Back
112bc 75 70 52 65 73 74 61 72 74 28 70 50 61 67 65 72  upRestart(pPager
112bd 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 20 20  ->pBackup);.    
112be 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
112bf 61 72 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ar(pPager->pPCac
112c0 68 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  he);.    pPager-
112c1 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30  >dbSizeValid = 0
112c2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  ;.  }.}../*.** F
112c3 72 65 65 20 61 6c 6c 20 73 74 72 75 63 74 75 72  ree all structur
112c4 65 73 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e  es in the Pager.
112c5 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72  aSavepoint[] arr
112c6 61 79 20 61 6e 64 20 73 65 74 20 62 6f 74 68 0a  ay and set both.
112c7 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f  ** Pager.aSavepo
112c8 69 6e 74 20 61 6e 64 20 50 61 67 65 72 2e 6e 53  int and Pager.nS
112c9 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a 65 72 6f  avepoint to zero
112ca 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 75 62 2d  . Close the sub-
112cb 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 20 69 74  journal.** if it
112cc 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65   is open and the
112cd 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e   pager is not in
112ce 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e   exclusive mode.
112cf 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
112d0 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f  releaseAllSavepo
112d1 69 6e 74 73 28 50 61 67 65 72 20 2a 70 50 61 67  ints(Pager *pPag
112d2 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20  er){.  int ii;  
112d3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
112d4 49 74 65 72 61 74 6f 72 20 66 6f 72 20 6c 6f 6f  Iterator for loo
112d5 70 69 6e 67 20 74 68 72 6f 75 67 68 20 50 61 67  ping through Pag
112d6 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 2a 2f  er.aSavepoint */
112d7 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
112d8 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69  pPager->nSavepoi
112d9 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73  nt; ii++){.    s
112da 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
112db 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 61 76  roy(pPager->aSav
112dc 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61  epoint[ii].pInSa
112dd 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a 20 20  vepoint);.  }.  
112de 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63  if( !pPager->exc
112df 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 73 71  lusiveMode || sq
112e0 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61  lite3IsMemJourna
112e1 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20  l(pPager->sjfd) 
112e2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ){.    sqlite3Os
112e3 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 73 6a  Close(pPager->sj
112e4 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  fd);.  }.  sqlit
112e5 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 2d 3e  e3_free(pPager->
112e6 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 70  aSavepoint);.  p
112e7 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
112e8 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  t = 0;.  pPager-
112e9 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b  >nSavepoint = 0;
112ea 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52  .  pPager->nSubR
112eb 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ec = 0;.}../*.**
112ec 20 53 65 74 20 74 68 65 20 62 69 74 20 6e 75 6d   Set the bit num
112ed 62 65 72 20 70 67 6e 6f 20 69 6e 20 74 68 65 20  ber pgno in the 
112ee 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70  PagerSavepoint.p
112ef 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20  InSavepoint .** 
112f0 62 69 74 76 65 63 73 20 6f 66 20 61 6c 6c 20 6f  bitvecs of all o
112f1 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20  pen savepoints. 
112f2 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
112f3 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 0a 2a   if successful.*
112f4 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  * or SQLITE_NOME
112f5 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61  M if a malloc fa
112f6 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 2a 2f  ilure occurs..*/
112f7 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54  .static int addT
112f8 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63  oSavepointBitvec
112f9 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  s(Pager *pPager,
112fa 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69   Pgno pgno){.  i
112fb 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
112fc 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
112fd 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
112fe 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
112ff 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c  ;       /* Resul
11300 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 66 6f 72  t code */..  for
11301 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72  (ii=0; ii<pPager
11302 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69  ->nSavepoint; ii
11303 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 53 61  ++){.    PagerSa
11304 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50  vepoint *p = &pP
11305 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
11306 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 67  [ii];.    if( pg
11307 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 29 7b 0a  no<=p->nOrig ){.
11308 20 20 20 20 20 20 72 63 20 7c 3d 20 73 71 6c 69        rc |= sqli
11309 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2d 3e  te3BitvecSet(p->
1130a 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 70 67  pInSavepoint, pg
1130b 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  no);.      testc
1130c 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ase( rc==SQLITE_
1130d 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 61  NOMEM );.      a
1130e 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1130f 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  E_OK || rc==SQLI
11310 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
11311 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
11312 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f  c;.}../*.** Unlo
11313 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ck the database 
11314 66 69 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74  file. This funct
11315 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  ion is a no-op i
11316 66 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 69  f the pager.** i
11317 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  s in exclusive m
11318 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ode..**.** If th
11319 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72 65  e pager is curre
1131a 6e 74 6c 79 20 69 6e 20 65 72 72 6f 72 20 73 74  ntly in error st
1131b 61 74 65 2c 20 64 69 73 63 61 72 64 20 74 68 65  ate, discard the
1131c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 0a 2a 2a   contents of .**
1131d 20 74 68 65 20 63 61 63 68 65 20 61 6e 64 20 72   the cache and r
1131e 65 73 65 74 20 74 68 65 20 50 61 67 65 72 20 73  eset the Pager s
1131f 74 72 75 63 74 75 72 65 20 69 6e 74 65 72 6e 61  tructure interna
11320 6c 20 73 74 61 74 65 2e 20 49 66 20 74 68 65 72  l state. If ther
11321 65 20 69 73 0a 2a 2a 20 61 6e 20 6f 70 65 6e 20  e is.** an open 
11322 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c 20 74 68  journal-file, th
11323 65 6e 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65  en the next time
11324 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69   a shared-lock i
11325 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e  s obtained.** on
11326 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20   the pager file 
11327 28 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20  (by this or any 
11328 6f 74 68 65 72 20 70 72 6f 63 65 73 73 29 2c 20  other process), 
11329 69 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 74 72  it will be.** tr
1132a 65 61 74 65 64 20 61 73 20 61 20 68 6f 74 2d 6a  eated as a hot-j
1132b 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f 6c 6c 65  ournal and rolle
1132c 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69  d back..*/.stati
1132d 63 20 76 6f 69 64 20 70 61 67 65 72 5f 75 6e 6c  c void pager_unl
1132e 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65  ock(Pager *pPage
1132f 72 29 7b 0a 20 20 69 66 28 20 21 70 50 61 67 65  r){.  if( !pPage
11330 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
11331 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 20   ){.    int rc; 
11332 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11333 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
11334 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41  ode */..    /* A
11335 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 68 65 20  lways close the 
11336 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65  journal file whe
11337 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 65 20 64  n dropping the d
11338 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e 0a 20 20  atabase lock..  
11339 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20    ** Otherwise, 
1133a 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  another connecti
1133b 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61 6c 5f  on with journal_
1133c 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69 67 68  mode=delete migh
1133d 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 65 20  t.    ** delete 
1133e 74 68 65 20 66 69 6c 65 20 6f 75 74 20 66 72 6f  the file out fro
1133f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 20 20 20  m under us..    
11340 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  */.    sqlite3Os
11341 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66  Close(pPager->jf
11342 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  d);.    sqlite3B
11343 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
11344 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29  ger->pInJournal)
11345 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49  ;.    pPager->pI
11346 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  nJournal = 0;.  
11347 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 65    releaseAllSave
11348 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b 0a  points(pPager);.
11349 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66  .    /* If the f
1134a 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c  ile is unlocked,
1134b 20 73 6f 6d 65 62 6f 64 79 20 65 6c 73 65 20 6d   somebody else m
1134c 69 67 68 74 20 63 68 61 6e 67 65 20 69 74 2e 20  ight change it. 
1134d 54 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65  The.    ** value
1134e 73 20 73 74 6f 72 65 64 20 69 6e 20 50 61 67 65  s stored in Page
1134f 72 2e 64 62 53 69 7a 65 20 65 74 63 2e 20 6d 69  r.dbSize etc. mi
11350 67 68 74 20 62 65 63 6f 6d 65 20 69 6e 76 61 6c  ght become inval
11351 69 64 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 69  id if.    ** thi
11352 73 20 68 61 70 70 65 6e 73 2e 20 54 4f 44 4f 3a  s happens. TODO:
11353 20 52 65 61 6c 6c 79 2c 20 74 68 69 73 20 64 6f   Really, this do
11354 65 73 6e 27 74 20 6e 65 65 64 20 74 6f 20 62 65  esn't need to be
11355 20 63 6c 65 61 72 65 64 0a 20 20 20 20 2a 2a 20   cleared.    ** 
11356 75 6e 74 69 6c 20 74 68 65 20 63 68 61 6e 67 65  until the change
11357 2d 63 6f 75 6e 74 65 72 20 63 68 65 63 6b 20 66  -counter check f
11358 61 69 6c 73 20 69 6e 20 70 61 67 65 72 53 68 61  ails in pagerSha
11359 72 65 64 4c 6f 63 6b 28 29 2e 0a 20 20 20 20 2a  redLock()..    *
1135a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62  /.    pPager->db
1135b 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 0a  SizeValid = 0;..
1135c 20 20 20 20 72 63 20 3d 20 6f 73 55 6e 6c 6f 63      rc = osUnloc
1135d 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 4e 4f  k(pPager->fd, NO
1135e 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20  _LOCK);.    if( 
1135f 72 63 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67  rc ){.      pPag
11360 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63  er->errCode = rc
11361 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52  ;.    }.    IOTR
11362 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 25 70 5c  ACE(("UNLOCK %p\
11363 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20  n", pPager))..  
11364 20 20 2f 2a 20 49 66 20 50 61 67 65 72 2e 65 72    /* If Pager.er
11365 72 43 6f 64 65 20 69 73 20 73 65 74 2c 20 74 68  rCode is set, th
11366 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
11367 65 20 70 61 67 65 72 20 63 61 63 68 65 20 63 61  e pager cache ca
11368 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a 2a 20 74  nnot be.    ** t
11369 72 75 73 74 65 64 2e 20 4e 6f 77 20 74 68 61 74  rusted. Now that
1136a 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20   the pager file 
1136b 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65  is unlocked, the
1136c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
1136d 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 20 63 61  .    ** cache ca
1136e 6e 20 62 65 20 64 69 73 63 61 72 64 65 64 20 61  n be discarded a
1136f 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  nd the error cod
11370 65 20 73 61 66 65 6c 79 20 63 6c 65 61 72 65 64  e safely cleared
11371 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
11372 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11373 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63   ){.      if( rc
11374 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
11375 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65         pPager->e
11376 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  rrCode = SQLITE_
11377 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OK;.      }.    
11378 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50    pager_reset(pP
11379 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  ager);.    }..  
1137a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65    pPager->change
1137b 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20  CountDone = 0;. 
1137c 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65     pPager->state
1137d 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b   = PAGER_UNLOCK;
1137e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
1137f 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  is function shou
11380 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65  ld be called whe
11381 6e 20 61 6e 20 49 4f 45 52 52 2c 20 43 4f 52 52  n an IOERR, CORR
11382 55 50 54 20 6f 72 20 46 55 4c 4c 20 65 72 72 6f  UPT or FULL erro
11383 72 0a 2a 2a 20 6d 61 79 20 68 61 76 65 20 6f 63  r.** may have oc
11384 63 75 72 72 65 64 2e 20 54 68 65 20 66 69 72 73  curred. The firs
11385 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  t argument is a 
11386 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
11387 61 67 65 72 20 0a 2a 2a 20 73 74 72 75 63 74 75  ager .** structu
11388 72 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 74  re, the second t
11389 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62  he error-code ab
1138a 6f 75 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e  out to be return
1138b 65 64 20 62 79 20 61 20 70 61 67 65 72 20 0a 2a  ed by a pager .*
1138c 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20  * API function. 
1138d 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
1138e 65 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  ed is a copy of 
1138f 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
11390 65 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 69 73 20  ent .** to this 
11391 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a  function. .**.**
11392 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   If the second a
11393 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54  rgument is SQLIT
11394 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f  E_IOERR, SQLITE_
11395 43 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49  CORRUPT, or SQLI
11396 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65  TE_FULL.** the e
11397 72 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72  rror becomes per
11398 73 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74  sistent. Until t
11399 68 65 20 70 65 72 73 69 73 74 65 6e 20 65 72 72  he persisten err
1139a 6f 72 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a  or is cleared,.*
1139b 2a 20 73 75 62 73 65 71 75 65 6e 74 20 41 50 49  * subsequent API
1139c 20 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50   calls on this P
1139d 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69  ager will immedi
1139e 61 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65  ately return the
1139f 20 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20   same .** error 
113a0 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65  code..**.** A pe
113a1 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69  rsistent error i
113a2 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68  ndicates that th
113a3 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
113a4 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a  e pager-cache .*
113a5 2a 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73  * cannot be trus
113a6 74 65 64 2e 20 54 68 69 73 20 73 74 61 74 65 20  ted. This state 
113a7 63 61 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62  can be cleared b
113a8 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73  y completely dis
113a9 63 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20  carding .** the 
113aa 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
113ab 70 61 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20  pager-cache. If 
113ac 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61  a transaction wa
113ad 73 20 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a  s active when.**
113ae 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20   the persistent 
113af 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20  error occurred, 
113b0 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  then the rollbac
113b1 6b 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65  k journal may ne
113b2 65 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c  ed.** to be repl
113b3 61 79 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20  ayed to restore 
113b4 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
113b5 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
113b6 65 20 28 61 73 20 69 66 0a 2a 2a 20 69 74 20 77  e (as if.** it w
113b7 65 72 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  ere a hot-journa
113b8 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  l)..*/.static in
113b9 74 20 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61  t pager_error(Pa
113ba 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
113bb 20 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 32 20   rc){.  int rc2 
113bc 3d 20 72 63 20 26 20 30 78 66 66 3b 0a 20 20 61  = rc & 0xff;.  a
113bd 73 73 65 72 74 28 0a 20 20 20 20 20 20 20 70 50  ssert(.       pP
113be 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53  ager->errCode==S
113bf 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20  QLITE_FULL ||.  
113c0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72       pPager->err
113c1 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code==SQLITE_OK 
113c2 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 61 67 65  ||.       (pPage
113c3 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66  r->errCode & 0xf
113c4 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  f)==SQLITE_IOERR
113c5 0a 20 20 29 3b 0a 20 20 69 66 28 0a 20 20 20 20  .  );.  if(.    
113c6 72 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c  rc2==SQLITE_FULL
113c7 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53 51 4c   ||.    rc2==SQL
113c8 49 54 45 5f 49 4f 45 52 52 20 7c 7c 0a 20 20 20  ITE_IOERR ||.   
113c9 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 43 4f 52   rc2==SQLITE_COR
113ca 52 55 50 54 0a 20 20 29 7b 0a 20 20 20 20 70 50  RUPT.  ){.    pP
113cb 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20  ager->errCode = 
113cc 72 63 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67  rc;.    if( pPag
113cd 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
113ce 5f 55 4e 4c 4f 43 4b 20 0a 20 20 20 20 20 26 26  _UNLOCK .     &&
113cf 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
113d0 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
113d1 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20  PCache)==0 .    
113d2 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ){.      /* If t
113d3 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 72 65  he pager is alre
113d4 61 64 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 63 61  ady unlocked, ca
113d5 6c 6c 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28  ll pager_unlock(
113d6 29 20 6e 6f 77 20 74 6f 0a 20 20 20 20 20 20 2a  ) now to.      *
113d7 2a 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f  * clear the erro
113d8 72 20 73 74 61 74 65 20 61 6e 64 20 65 6e 73 75  r state and ensu
113d9 72 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65  re that the page
113da 72 2d 63 61 63 68 65 20 69 73 20 0a 20 20 20 20  r-cache is .    
113db 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c 79 20    ** completely 
113dc 65 6d 70 74 79 2e 0a 20 20 20 20 20 20 2a 2f 0a  empty..      */.
113dd 20 20 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f        pager_unlo
113de 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  ck(pPager);.    
113df 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
113e0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  c;.}../*.** Exec
113e1 75 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69  ute a rollback i
113e2 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
113e3 69 73 20 61 63 74 69 76 65 20 61 6e 64 20 75 6e  is active and un
113e4 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74  lock the .** dat
113e5 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a  abase file. .**.
113e6 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20  ** If the pager 
113e7 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65  has already ente
113e8 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74  red the error st
113e9 61 74 65 2c 20 64 6f 20 6e 6f 74 20 61 74 74 65  ate, do not atte
113ea 6d 70 74 20 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c  mpt .** the roll
113eb 62 61 63 6b 20 61 74 20 74 68 69 73 20 74 69 6d  back at this tim
113ec 65 2e 20 49 6e 73 74 65 61 64 2c 20 70 61 67 65  e. Instead, page
113ed 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 63 61  r_unlock() is ca
113ee 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c  lled. The.** cal
113ef 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63  l to pager_unloc
113f0 6b 28 29 20 77 69 6c 6c 20 64 69 73 63 61 72 64  k() will discard
113f1 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70   all in-memory p
113f2 61 67 65 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20  ages, unlock.** 
113f3 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
113f4 65 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65 20  e and clear the 
113f5 65 72 72 6f 72 20 73 74 61 74 65 2e 20 49 66 20  error state. If 
113f6 74 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a  this means that.
113f7 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 6f  ** there is a ho
113f8 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 69  t-journal left i
113f9 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65  n the file-syste
113fa 6d 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e 6e  m, the next conn
113fb 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74  ection.** to obt
113fc 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63  ain a shared loc
113fd 6b 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 28  k on the pager (
113fe 77 68 69 63 68 20 6d 61 79 20 62 65 20 74 68 69  which may be thi
113ff 73 20 6f 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20 72  s one) will.** r
11400 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a  oll it back..**.
11401 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20  ** If the pager 
11402 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20  has not already 
11403 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f  entered the erro
11404 72 20 73 74 61 74 65 2c 20 62 75 74 20 61 6e 20  r state, but an 
11405 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20  IO or.** malloc 
11406 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
11407 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20  ing a rollback, 
11408 74 68 65 6e 20 74 68 69 73 20 77 69 6c 6c 20 69  then this will i
11409 74 73 65 6c 66 20 63 61 75 73 65 20 0a 2a 2a 20  tself cause .** 
1140a 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e 74  the pager to ent
1140b 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  er the error sta
1140c 74 65 2e 20 57 68 69 63 68 20 77 69 6c 6c 20 62  te. Which will b
1140d 65 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65  e cleared by the
1140e 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67 65  .** call to page
1140f 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61 73 20 64  r_unlock(), as d
11410 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e 0a  escribed above..
11411 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
11412 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c  agerUnlockAndRol
11413 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61  lback(Pager *pPa
11414 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67  ger){.  if( pPag
11415 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c  er->errCode==SQL
11416 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72  ITE_OK && pPager
11417 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52  ->state>=PAGER_R
11418 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 73  ESERVED ){.    s
11419 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
1141a 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 73  nMalloc();.    s
1141b 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62  qlite3PagerRollb
1141c 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20  ack(pPager);.   
1141d 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
1141e 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20  nMalloc();.  }. 
1141f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50   pager_unlock(pP
11420 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ager);.}../*.** 
11421 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 64  This routine end
11422 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  s a transaction.
11423 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   A transaction i
11424 73 20 75 73 75 61 6c 6c 79 20 65 6e 64 65 64 20  s usually ended 
11425 62 79 20 0a 2a 2a 20 65 69 74 68 65 72 20 61 20  by .** either a 
11426 43 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f 4c 4c  COMMIT or a ROLL
11427 42 41 43 4b 20 6f 70 65 72 61 74 69 6f 6e 2e 20  BACK operation. 
11428 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 79  This routine may
11429 20 62 65 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 61   be called .** a
1142a 66 74 65 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66  fter rollback of
1142b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20   a hot-journal, 
1142c 6f 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  or if an error o
1142d 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65 6e  ccurs while open
1142e 69 6e 67 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e  ing.** the journ
1142f 61 6c 20 66 69 6c 65 20 6f 72 20 77 72 69 74 69  al file or writi
11430 6e 67 20 74 68 65 20 76 65 72 79 20 66 69 72 73  ng the very firs
11431 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72  t journal-header
11432 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73   of a.** databas
11433 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  e transaction..*
11434 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  * .** If the pag
11435 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53  er is in PAGER_S
11436 48 41 52 45 44 20 6f 72 20 50 41 47 45 52 5f 55  HARED or PAGER_U
11437 4e 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e  NLOCK state when
11438 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
11439 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69   is called, it i
1143a 73 20 61 20 6e 6f 2d 6f 70 20 28 72 65 74 75 72  s a no-op (retur
1143b 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a  ns SQLITE_OK)..*
1143c 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  *.** Otherwise, 
1143d 61 6e 79 20 61 63 74 69 76 65 20 73 61 76 65 70  any active savep
1143e 6f 69 6e 74 73 20 61 72 65 20 72 65 6c 65 61 73  oints are releas
1143f 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
11440 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
11441 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 69   open, then it i
11442 73 20 22 66 69 6e 61 6c 69 7a 65 64 22 2e 20 4f  s "finalized". O
11443 6e 63 65 20 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a  nce a journal .*
11444 2a 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  * file has been 
11445 66 69 6e 61 6c 69 7a 65 64 20 69 74 20 69 73 20  finalized it is 
11446 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
11447 75 73 65 20 69 74 20 74 6f 20 72 6f 6c 6c 20 62  use it to roll b
11448 61 63 6b 20 61 20 0a 2a 2a 20 74 72 61 6e 73 61  ack a .** transa
11449 63 74 69 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20  ction. Nor will 
1144a 69 74 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64  it be considered
1144b 20 74 6f 20 62 65 20 61 20 68 6f 74 2d 6a 6f 75   to be a hot-jou
1144c 72 6e 61 6c 20 62 79 20 74 68 69 73 0a 2a 2a 20  rnal by this.** 
1144d 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 64 61 74  or any other dat
1144e 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1144f 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20 61 20  . Exactly how a 
11450 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c  journal is final
11451 69 7a 65 64 0a 2a 2a 20 64 65 70 65 6e 64 73 20  ized.** depends 
11452 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  on whether or no
11453 74 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72  t the pager is r
11454 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73  unning in exclus
11455 69 76 65 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20  ive mode and.** 
11456 74 68 65 20 63 75 72 72 65 6e 74 20 6a 6f 75 72  the current jour
11457 6e 61 6c 2d 6d 6f 64 65 20 28 50 61 67 65 72 2e  nal-mode (Pager.
11458 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 6c 75  journalMode valu
11459 65 29 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  e), as follows:.
1145a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d  **.**   journalM
1145b 6f 64 65 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20  ode==MEMORY.**  
1145c 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20     Journal file 
1145d 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 73 69  descriptor is si
1145e 6d 70 6c 79 20 63 6c 6f 73 65 64 2e 20 54 68 69  mply closed. Thi
1145f 73 20 64 65 73 74 72 6f 79 73 20 61 6e 20 0a 2a  s destroys an .*
11460 2a 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20  *     in-memory 
11461 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20  journal..**.**  
11462 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52   journalMode==TR
11463 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 20 4a 6f  UNCATE.**     Jo
11464 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 72  urnal file is tr
11465 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f 20  uncated to zero 
11466 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a  bytes in size..*
11467 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f  *.**   journalMo
11468 64 65 3d 3d 50 45 52 53 49 53 54 0a 2a 2a 20 20  de==PERSIST.**  
11469 20 20 20 54 68 65 20 66 69 72 73 74 20 32 38 20     The first 28 
1146a 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f 75  bytes of the jou
1146b 72 6e 61 6c 20 66 69 6c 65 20 61 72 65 20 7a 65  rnal file are ze
1146c 72 6f 65 64 2e 20 54 68 69 73 20 69 6e 76 61 6c  roed. This inval
1146d 69 64 61 74 65 73 0a 2a 2a 20 20 20 20 20 74 68  idates.**     th
1146e 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20  e first journal 
1146f 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 66 69  header in the fi
11470 6c 65 2c 20 61 6e 64 20 68 65 6e 63 65 20 74 68  le, and hence th
11471 65 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c  e entire journal
11472 0a 2a 2a 20 20 20 20 20 66 69 6c 65 2e 20 41 6e  .**     file. An
11473 20 69 6e 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c   invalid journal
11474 20 66 69 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20   file cannot be 
11475 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
11476 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65  **   journalMode
11477 3d 3d 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20  ==DELETE.**     
11478 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  The journal file
11479 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 64   is closed and d
1147a 65 6c 65 74 65 64 20 75 73 69 6e 67 20 73 71 6c  eleted using sql
1147b 69 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a  ite3OsDelete()..
1147c 2a 2a 0a 2a 2a 20 20 20 20 20 49 66 20 74 68 65  **.**     If the
1147d 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e   pager is runnin
1147e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  g in exclusive m
1147f 6f 64 65 2c 20 74 68 69 73 20 6d 65 74 68 6f 64  ode, this method
11480 20 6f 66 20 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a   of finalizing.*
11481 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61  *     the journa
11482 6c 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72 20  l file is never 
11483 75 73 65 64 2e 20 49 6e 73 74 65 61 64 2c 20 69  used. Instead, i
11484 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64  f the journalMod
11485 65 20 69 73 0a 2a 2a 20 20 20 20 20 44 45 4c 45  e is.**     DELE
11486 54 45 20 61 6e 64 20 74 68 65 20 70 61 67 65 72  TE and the pager
11487 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65   is in exclusive
11488 20 6d 6f 64 65 2c 20 74 68 65 20 6d 65 74 68 6f   mode, the metho
11489 64 20 64 65 73 63 72 69 62 65 64 20 75 6e 64 65  d described unde
1148a 72 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c  r.**     journal
1148b 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 20 69 73  Mode==PERSIST is
1148c 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a   used instead..*
1148d 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6a  *.** After the j
1148e 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69  ournal is finali
1148f 7a 65 64 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20  zed, if running 
11490 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 65  in non-exclusive
11491 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 70 61   mode, the.** pa
11492 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 50 41 47  ger moves to PAG
11493 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 20  ER_SHARED state 
11494 28 61 6e 64 20 64 6f 77 6e 67 72 61 64 65 73 20  (and downgrades 
11495 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a  the lock on the.
11496 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
11497 20 61 63 63 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a   accordingly)..*
11498 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
11499 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20  r is running in 
1149a 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61  exclusive mode a
1149b 6e 64 20 69 73 20 69 6e 20 50 41 47 45 52 5f 53  nd is in PAGER_S
1149c 59 4e 43 45 44 20 73 74 61 74 65 2c 0a 2a 2a 20  YNCED state,.** 
1149d 69 74 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45  it moves to PAGE
1149e 52 5f 45 58 43 4c 55 53 49 56 45 2e 20 4e 6f 20  R_EXCLUSIVE. No 
1149f 6c 6f 63 6b 73 20 61 72 65 20 64 6f 77 6e 67 72  locks are downgr
114a0 61 64 65 64 20 77 68 65 6e 20 72 75 6e 6e 69 6e  aded when runnin
114a1 67 20 69 6e 0a 2a 2a 20 65 78 63 6c 75 73 69 76  g in.** exclusiv
114a2 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51  e mode..**.** SQ
114a3 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
114a4 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72 20  ned if no error 
114a5 6f 63 63 75 72 73 2e 20 49 66 20 61 6e 20 65 72  occurs. If an er
114a6 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
114a7 67 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 68 65 20  g.** any of the 
114a8 49 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f  IO operations to
114a9 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f   finalize the jo
114aa 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 75 6e  urnal file or un
114ab 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20 64 61 74 61  lock the.** data
114ac 62 61 73 65 20 74 68 65 6e 20 74 68 65 20 49 4f  base then the IO
114ad 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
114ae 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75  eturned to the u
114af 73 65 72 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20  ser. If the .** 
114b0 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 66 69 6e  operation to fin
114b1 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61  alize the journa
114b2 6c 20 66 69 6c 65 20 66 61 69 6c 73 2c 20 74 68  l file fails, th
114b3 65 6e 20 74 68 65 20 63 6f 64 65 20 73 74 69 6c  en the code stil
114b4 6c 0a 2a 2a 20 74 72 69 65 73 20 74 6f 20 75 6e  l.** tries to un
114b5 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  lock the databas
114b6 65 20 66 69 6c 65 20 69 66 20 6e 6f 74 20 69 6e  e file if not in
114b7 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e   exclusive mode.
114b8 20 49 66 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63   If the.** unloc
114b9 6b 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c  k operation fail
114ba 73 20 61 73 20 77 65 6c 6c 2c 20 74 68 65 6e 20  s as well, then 
114bb 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20  the first error 
114bc 63 6f 64 65 20 72 65 6c 61 74 65 64 0a 2a 2a 20  code related.** 
114bd 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 72 72  to the first err
114be 6f 72 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28  or encountered (
114bf 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61  the journal fina
114c0 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65 29 20 69 73  lization one) is
114c1 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f  .** returned..*/
114c2 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
114c3 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
114c4 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  n(Pager *pPager,
114c5 20 69 6e 74 20 68 61 73 4d 61 73 74 65 72 29 7b   int hasMaster){
114c6 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
114c7 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 45  TE_OK;      /* E
114c8 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 6a  rror code from j
114c9 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74  ournal finalizat
114ca 69 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  ion operation */
114cb 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c  .  int rc2 = SQL
114cc 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 45  ITE_OK;     /* E
114cd 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 64  rror code from d
114ce 62 20 66 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70  b file unlock op
114cf 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  eration */..  if
114d0 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c  ( pPager->state<
114d1 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29  PAGER_RESERVED )
114d2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
114d3 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65  ITE_OK;.  }.  re
114d4 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e  leaseAllSavepoin
114d5 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 61  ts(pPager);..  a
114d6 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
114d7 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50  ager->jfd) || pP
114d8 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
114d9 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 4f  ==0 );.  if( isO
114da 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
114db 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f   ){..    /* TODO
114dc 3a 20 54 68 65 72 65 27 73 20 61 20 70 72 6f 62  : There's a prob
114dd 6c 65 6d 20 68 65 72 65 20 69 66 20 61 20 6a 6f  lem here if a jo
114de 75 72 6e 61 6c 2d 66 69 6c 65 20 77 61 73 20 6f  urnal-file was o
114df 70 65 6e 65 64 20 69 6e 20 4d 45 4d 4f 52 59 0a  pened in MEMORY.
114e0 20 20 20 20 2a 2a 20 6d 6f 64 65 20 61 6e 64 20      ** mode and 
114e1 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
114e2 2d 6d 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64  -mode is changed
114e3 20 74 6f 20 54 52 55 4e 43 41 54 45 20 6f 72 20   to TRUNCATE or 
114e4 50 45 52 53 49 53 54 0a 20 20 20 20 2a 2a 20 64  PERSIST.    ** d
114e5 75 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61  uring the transa
114e6 63 74 69 6f 6e 2e 20 54 68 69 73 20 63 6f 64 65  ction. This code
114e7 20 73 68 6f 75 6c 64 20 62 65 20 63 68 61 6e 67   should be chang
114e8 65 64 20 74 6f 20 61 73 73 75 6d 65 0a 20 20 20  ed to assume.   
114e9 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6a 6f 75   ** that the jou
114ea 72 6e 61 6c 20 6d 6f 64 65 20 68 61 73 20 6e 6f  rnal mode has no
114eb 74 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20  t changed since 
114ec 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
114ed 77 61 73 0a 20 20 20 20 2a 2a 20 73 74 61 72 74  was.    ** start
114ee 65 64 2e 20 41 6e 64 20 74 68 65 20 73 71 6c 69  ed. And the sqli
114ef 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d  te3PagerJournalM
114f0 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 73  ode() function s
114f1 68 6f 75 6c 64 20 62 65 0a 20 20 20 20 2a 2a 20  hould be.    ** 
114f2 63 68 61 6e 67 65 64 20 74 6f 20 6d 61 6b 65 20  changed to make 
114f3 73 75 72 65 20 74 68 61 74 20 74 68 69 73 20 69  sure that this i
114f4 73 20 74 68 65 20 63 61 73 65 20 74 6f 6f 2e 0a  s the case too..
114f5 20 20 20 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46      */..    /* F
114f6 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72  inalize the jour
114f7 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20  nal file. */.   
114f8 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
114f9 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
114fa 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
114fb 52 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  RY ){.      int 
114fc 69 73 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61 6c 20  isMemoryJournal 
114fd 3d 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f  = sqlite3IsMemJo
114fe 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66  urnal(pPager->jf
114ff 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
11500 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
11501 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28  >jfd);.      if(
11502 20 21 69 73 4d 65 6d 6f 72 79 4a 6f 75 72 6e 61   !isMemoryJourna
11503 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  l ){.        rc 
11504 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74  = sqlite3OsDelet
11505 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20  e(pPager->pVfs, 
11506 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
11507 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
11508 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67    }else if( pPag
11509 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
1150a 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
1150b 44 45 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20  DE_TRUNCATE ){. 
1150c 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d       if( pPager-
1150d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29  >journalOff==0 )
1150e 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
1150f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
11510 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
11511 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
11512 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  ncate(pPager->jf
11513 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  d, 0);.      }. 
11514 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
11515 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20  rnalOff = 0;.   
11516 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
11517 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20  alStarted = 0;. 
11518 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61     }else if( pPa
11519 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
1151a 64 65 20 0a 20 20 20 20 20 7c 7c 20 70 50 61 67  de .     || pPag
1151b 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
1151c 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
1151d 44 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 29  DE_PERSIST.    )
1151e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72  {.      rc = zer
1151f 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67  oJournalHdr(pPag
11520 65 72 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a  er, hasMaster);.
11521 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f        pager_erro
11522 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20  r(pPager, rc);. 
11523 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
11524 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20  rnalOff = 0;.   
11525 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
11526 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20  alStarted = 0;. 
11527 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
11528 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
11529 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
1152a 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44  ER_JOURNALMODE_D
1152b 45 4c 45 54 45 20 7c 7c 20 72 63 20 29 3b 0a 20  ELETE || rc );. 
1152c 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c       sqlite3OsCl
1152d 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  ose(pPager->jfd)
1152e 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
1152f 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50  SQLITE_OK && !pP
11530 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
11531 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
11532 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
11533 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61  Pager->pVfs, pPa
11534 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30  ger->zJournal, 0
11535 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
11536 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
11537 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20  CHECK_PAGES.    
11538 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65  sqlite3PcacheIte
11539 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72  rateDirty(pPager
1153a 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72  ->pPCache, pager
1153b 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a  _set_pagehash);.
1153c 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69  #endif..    sqli
1153d 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c  te3PcacheCleanAl
1153e 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  l(pPager->pPCach
1153f 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  e);.    sqlite3B
11540 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
11541 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29  ger->pInJournal)
11542 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49  ;.    pPager->pI
11543 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  nJournal = 0;.  
11544 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
11545 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21   0;.  }..  if( !
11546 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
11547 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32  eMode ){.    rc2
11548 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67   = osUnlock(pPag
11549 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c  er->fd, SHARED_L
1154a 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72  OCK);.    pPager
1154b 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
1154c 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67  SHARED;.    pPag
1154d 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44  er->changeCountD
1154e 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  one = 0;.  }else
1154f 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
11550 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44  te==PAGER_SYNCED
11551 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
11552 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58  state = PAGER_EX
11553 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70  CLUSIVE;.  }.  p
11554 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72  Pager->setMaster
11555 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   = 0;.  pPager->
11556 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
11557 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
11558 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f  ed = 0;..  /* TO
11559 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69  DO: Is this opti
1155a 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20  mal? Why is the 
1155b 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61  db size invalida
1155c 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77  ted here .  ** w
1155d 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
1155e 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c   file is not unl
1155f 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67  ocked? */.  pPag
11560 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d  er->dbOrigSize =
11561 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61   0;.  sqlite3Pca
11562 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67  cheTruncate(pPag
11563 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61  er->pPCache, pPa
11564 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20  ger->dbSize);.  
11565 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20  if( !MEMDB ){.  
11566 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
11567 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Valid = 0;.  }..
11568 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51    return (rc==SQ
11569 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b  LITE_OK?rc2:rc);
1156a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65  .}../*.** Parame
1156b 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70  ter aData must p
1156c 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
1156d 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65   of pPager->page
1156e 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66  Size bytes.** of
1156f 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61   data. Compute a
11570 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63  nd return a chec
11571 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74  ksum based ont t
11572 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
11573 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64  he .** page of d
11574 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72  ata and the curr
11575 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61  ent value of pPa
11576 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a  ger->cksumInit..
11577 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f  **.** This is no
11578 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75  t a real checksu
11579 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20  m. It is really 
1157a 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20  just the sum of 
1157b 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69  the .** random i
1157c 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50  nitial value (pP
1157d 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29  ager->cksumInit)
1157e 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68   and every 200th
1157f 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20   byte.** of the 
11580 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74  page data, start
11581 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66  ing with byte of
11582 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61  fset (pPager->pa
11583 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20  geSize%200)..** 
11584 45 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74  Each byte is int
11585 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38  erpreted as an 8
11586 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e  -bit unsigned in
11587 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61  teger..**.** Cha
11588 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c  nging the formul
11589 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74  a used to comput
1158a 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20  e this checksum 
1158b 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a  results in an.**
1158c 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f   incompatible jo
1158d 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61  urnal file forma
1158e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72  t..**.** If jour
1158f 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f  nal corruption o
11590 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70  ccurs due to a p
11591 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68  ower failure, th
11592 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a  e most likely .*
11593 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68  * scenario is th
11594 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68  at one end or th
11595 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72  e other of the r
11596 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68  ecord will be ch
11597 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73  anged. .** It is
11598 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c   much less likel
11599 79 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65  y that the two e
1159a 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  nds of the journ
1159b 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62  al record will b
1159c 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64  e.** correct and
1159d 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63   the middle be c
1159e 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74  orrupt.  Thus, t
1159f 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73  his "checksum" s
115a0 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68  cheme,.** though
115a1 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65   fast and simple
115a2 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f  , catches the mo
115a3 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64  stly likely kind
115a4 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a   of corruption..
115a5 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61  */.static u32 pa
115a6 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20  ger_cksum(Pager 
115a7 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75  *pPager, const u
115a8 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32  8 *aData){.  u32
115a9 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d   cksum = pPager-
115aa 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20  >cksumInit;     
115ab 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20      /* Checksum 
115ac 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  value to return 
115ad 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61  */.  int i = pPa
115ae 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30  ger->pageSize-20
115af 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  0;          /* L
115b0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
115b1 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20   while( i>0 ){. 
115b2 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74     cksum += aDat
115b3 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32  a[i];.    i -= 2
115b4 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  00;.  }.  return
115b5 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   cksum;.}../*.**
115b6 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70   Read a single p
115b7 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20  age from either 
115b8 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
115b9 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d   (if isMainJrnl=
115ba 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74  =1) or.** from t
115bb 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28  he sub-journal (
115bc 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30  if isMainJrnl==0
115bd 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74  ) and playback t
115be 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65  hat page..** The
115bf 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20   page begins at 
115c0 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20  offset *pOffset 
115c1 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54  into the file. T
115c2 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76  he *pOffset.** v
115c3 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65  alue is increase
115c4 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  d to the start o
115c5 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  f the next page 
115c6 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a  in the journal..
115c7 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e  **.** The isMain
115c8 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75  Jrnl flag is tru
115c9 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  e if this is the
115ca 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a   main rollback j
115cb 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61  ournal and.** fa
115cc 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74  lse for the stat
115cd 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20  ement journal.  
115ce 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63  The main rollbac
115cf 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a  k journal uses.*
115d0 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68  * checksums - th
115d1 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  e statement jour
115d2 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a  nal does not..**
115d3 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20  .** If the page 
115d4 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61  number of the pa
115d5 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66  ge record read f
115d6 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f  rom the (sub-)jo
115d7 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73  urnal file.** is
115d8 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
115d9 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
115da 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c  of Pager.dbSize,
115db 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69   then playback i
115dc 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64  s.** skipped and
115dd 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
115de 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
115df 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55   pDone is not NU
115e0 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61  LL, then it is a
115e1 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73   record of pages
115e2 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61   that have alrea
115e3 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65  dy.** been playe
115e4 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20  d back.  If the 
115e5 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74  page at *pOffset
115e6 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
115e7 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a  n played back.**
115e8 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70   (if the corresp
115e9 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74  onding pDone bit
115ea 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b   is set) then sk
115eb 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e  ip the playback.
115ec 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
115ed 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72  e pDone bit corr
115ee 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
115ef 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69   *pOffset page i
115f0 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74  s set.** prior t
115f1 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a  o returning..**.
115f2 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72  ** If the page r
115f3 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73  ecord is success
115f4 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20  fully read from 
115f5 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61  the (sub-)journa
115f6 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c  l file.** and pl
115f7 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20  ayed back, then 
115f8 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
115f9 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20  urned. If an IO 
115fa 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20  error occurs.** 
115fb 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68  while reading th
115fc 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68  e record from th
115fd 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20  e (sub-)journal 
115fe 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72  file or while wr
115ff 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20  iting.** to the 
11600 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
11601 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72  hen the IO error
11602 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
11603 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73  d. If data.** is
11604 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65   successfully re
11605 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62  ad from the (sub
11606 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62  -)journal file b
11607 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65  ut appears to be
11608 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53  .** corrupted, S
11609 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65  QLITE_DONE is re
1160a 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20  turned. Data is 
1160b 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75  considered corru
1160c 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63  pted in.** two c
1160d 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a  ircumstances:.**
1160e 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20   .**   * If the 
1160f 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62  record page-numb
11610 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30  er is illegal (0
11611 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e   or PAGER_MJ_PGN
11612 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66  O), or.**   * If
11613 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62   the record is b
11614 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b  eing rolled back
11615 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a   from the main j
11616 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20  ournal file.**  
11617 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b     and the check
11618 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e  sum field does n
11619 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63  ot match the rec
1161a 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a  ord content..**.
1161b 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68  ** Neither of th
1161c 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f  ese two scenario
1161d 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64  s are possible d
1161e 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e  uring a savepoin
1161f 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  t rollback..**.*
11620 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73  * If this is a s
11621 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63  avepoint rollbac
11622 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d  k, then memory m
11623 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79  ay have to be dy
11624 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c  namically.** all
11625 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66  ocated by this f
11626 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73  unction. If this
11627 20 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64   is the case and
11628 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66   an allocation f
11629 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  ails,.** SQLITE_
1162a 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65  NOMEM is returne
1162b 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
1162c 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
1162d 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65  one_page(.  Page
1162e 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
1162f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
11630 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61   pager being pla
11631 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  yed back */.  in
11632 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20  t isMainJrnl,   
11633 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
11634 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c   -> main journal
11635 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e  . 0 -> sub-journ
11636 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55  al. */.  int isU
11637 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20  nsync,          
11638 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
11639 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 75  f reading from u
1163a 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f 75  nsynced main jou
1163b 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a 70  rnal */.  i64 *p
1163c 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
1163d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
1163e 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70  t of record to p
1163f 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  layback */.  int
11640 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 20   isSavepnt,     
11641 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
11642 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69  ue for a savepoi
11643 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20  nt rollback */. 
11644 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 20   Bitvec *pDone  
11645 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11646 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65  * Bitvec of page
11647 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64  s already played
11648 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e   back */.){.  in
11649 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70  t rc;.  PgHdr *p
1164a 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Pg;             
1164b 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73        /* An exis
1164c 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65  ting page in the
1164d 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f   cache */.  Pgno
1164e 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
1164f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
11650 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
11651 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61  a page in journa
11652 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d  l */.  u32 cksum
11653 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11654 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d       /* Checksum
11655 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79   used for sanity
11656 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75   checking */.  u
11657 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20  8 *aData;       
11658 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11659 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67  Temporary storag
1165a 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a  e for the page *
1165b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
1165c 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20   *jfd;          
1165d 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
1165e 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65  scriptor for the
1165f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
11660 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d  ..  assert( (isM
11661 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29  ainJrnl&~1)==0 )
11662 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e  ;      /* isMain
11663 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a  Jrnl is 0 or 1 *
11664 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53  /.  assert( (isS
11665 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b  avepnt&~1)==0 );
11666 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65         /* isSave
11667 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f  pnt is 0 or 1 */
11668 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69  .  assert( isMai
11669 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29  nJrnl || pDone )
1166a 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61  ;     /* pDone a
1166b 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75  lways used on su
1166c 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20  b-journals */.  
1166d 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e  assert( isSavepn
1166e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b  t || pDone==0 );
1166f 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65     /* pDone neve
11670 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61  r used on non-sa
11671 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44  vepoint */..  aD
11672 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 65  ata = (u8*)pPage
11673 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20  r->pTmpSpace;.  
11674 61 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b  assert( aData );
11675 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
11676 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61   storage must ha
11677 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
11678 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20  allocated */..  
11679 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65  /* Read the page
1167a 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65   number and page
1167b 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a   data from the j
1167c 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f  ournal or sub-jo
1167d 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e  urnal.  ** file.
1167e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
1167f 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c   code to the cal
11680 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72  ler if an IO err
11681 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a  or occurs..  */.
11682 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72    jfd = isMainJr
11683 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64  nl ? pPager->jfd
11684 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b   : pPager->sjfd;
11685 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69  .  rc = read32bi
11686 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74  ts(jfd, *pOffset
11687 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20  , &pgno);.  if( 
11688 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
11689 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20  return rc;.  rc 
1168a 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
1168b 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67  jfd, aData, pPag
1168c 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 2a  er->pageSize, (*
1168d 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 69  pOffset)+4);.  i
1168e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1168f 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
11690 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 67  *pOffset += pPag
11691 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34  er->pageSize + 4
11692 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b   + isMainJrnl*4;
11693 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68  ..  /* Sanity ch
11694 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61  ecking on the pa
11695 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72  ge.  This is mor
11696 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74  e important that
11697 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20   I originally.  
11698 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20  ** thought.  If 
11699 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  a power failure 
1169a 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65  occurs while the
1169b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e   journal is bein
1169c 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20  g written,.  ** 
1169d 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69  it could cause i
1169e 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62  nvalid data to b
1169f 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
116a0 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20  he journal.  We 
116a1 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74  need to.  ** det
116a2 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64  ect this invalid
116a3 20 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68   data (with high
116a4 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e   probability) an
116a5 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a  d ignore it..  *
116a6 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20  /.  if( pgno==0 
116a7 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d  || pgno==PAGER_M
116a8 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29  J_PGNO(pPager) )
116a9 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69  {.    assert( !i
116aa 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20  sSavepnt );.    
116ab 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
116ac 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67  NE;.  }.  if( pg
116ad 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d  no>(Pgno)pPager-
116ae 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 74  >dbSize || sqlit
116af 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44 6f  e3BitvecTest(pDo
116b0 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20  ne, pgno) ){.   
116b1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
116b2 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4d  K;.  }.  if( isM
116b3 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72  ainJrnl ){.    r
116b4 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a  c = read32bits(j
116b5 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d 34  fd, (*pOffset)-4
116b6 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69  , &cksum);.    i
116b7 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
116b8 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 61  c;.    if( !isSa
116b9 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f 63  vepnt && pager_c
116ba 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61  ksum(pPager, aDa
116bb 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20  ta)!=cksum ){.  
116bc 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
116bd 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  E_DONE;.    }.  
116be 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26  }..  if( pDone &
116bf 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42  & (rc = sqlite3B
116c0 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20  itvecSet(pDone, 
116c1 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f  pgno))!=SQLITE_O
116c2 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
116c3 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  rc;.  }..  asser
116c4 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
116c5 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  ==PAGER_RESERVED
116c6 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74   || pPager->stat
116c7 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49  e>=PAGER_EXCLUSI
116c8 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  VE );..  /* If t
116c9 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 52  he pager is in R
116ca 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74  ESERVED state, t
116cb 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62  hen there must b
116cc 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73  e a copy of this
116cd 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68  .  ** page in th
116ce 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49  e pager cache. I
116cf 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74  n this case just
116d0 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65   update the page
116d1 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f  r cache,.  ** no
116d2 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  t the database f
116d3 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73  ile. The page is
116d4 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72   left marked dir
116d5 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ty in this case.
116d6 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78  .  **.  ** An ex
116d7 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61  ception to the a
116d8 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68  bove rule: If th
116d9 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e  e database is in
116da 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20   no-sync mode.  
116db 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73  ** and a page is
116dc 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e   moved during an
116dd 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   incremental vac
116de 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67  uum then the pag
116df 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62  e may.  ** not b
116e0 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63  e in the pager c
116e1 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20  ache. Later: if 
116e2 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f  a malloc() or IO
116e3 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20   error occurs.  
116e4 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65  ** during a Move
116e5 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65  page() call, the
116e6 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e  n the page may n
116e7 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63  ot be in the cac
116e8 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20  he.  ** either. 
116e9 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  So the condition
116ea 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68   described in th
116eb 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70  e above paragrap
116ec 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73  h is not.  ** as
116ed 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a  sert()able..  **
116ee 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c  .  ** If in EXCL
116ef 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65  USIVE state, the
116f0 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 20  n we update the 
116f1 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69  pager cache if i
116f2 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e  t exists.  ** an
116f3 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e  d the main file.
116f4 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65   The page is the
116f5 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72  n marked not dir
116f6 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69  ty..  **.  ** Ti
116f7 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 65  cket #1171:  The
116f8 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
116f9 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e  al might contain
116fa 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68   page content th
116fb 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65  at is.  ** diffe
116fc 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61  rent from the pa
116fd 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68  ge content at th
116fe 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74  e start of the t
116ff 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a  ransaction..  **
11700 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65   This occurs whe
11701 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61 6e  n a page is chan
11702 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65  ged prior to the
11703 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 74   start of a stat
11704 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20  ement.  ** then 
11705 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69  changed again wi
11706 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65  thin the stateme
11707 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e  nt.  When rollin
11708 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20  g back such a.  
11709 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20  ** statement we 
1170a 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74  must not write t
1170b 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  o the original d
1170c 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77  atabase unless w
1170d 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20  e know.  ** for 
1170e 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69  certain that ori
1170f 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65  ginal page conte
11710 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 69  nts are synced i
11711 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c  nto the main rol
11712 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e  lback.  ** journ
11713 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  al.  Otherwise, 
11714 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67  a power loss mig
11715 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65  ht leave modifie
11716 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20  d data in the.  
11717 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
11718 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72   without an entr
11719 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  y in the rollbac
1171a 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63  k journal that c
1171b 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20  an.  ** restore 
1171c 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
1171d 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72  its original for
1171e 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f  m.  Two conditio
1171f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20  ns must be.  ** 
11720 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 69  met before writi
11721 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
11722 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68  se files. (1) th
11723 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20  e database must 
11724 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20  be.  ** locked. 
11725 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61   (2) we know tha
11726 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  t the original p
11727 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66  age content is f
11728 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a  ully synced.  **
11729 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   in the main jou
1172a 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 61  rnal either beca
1172b 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73 20  use the page is 
1172c 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20  not in cache or 
1172d 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61  else.  ** the pa
1172e 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  ge is marked as 
1172f 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a  needSync==0..  *
11730 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31  *.  ** 2008-04-1
11731 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74  4:  When attempt
11732 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20  ing to vacuum a 
11733 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
11734 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69   file, it.  ** i
11735 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61  s possible to fa
11736 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f  il a statement o
11737 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68 61  n a database tha
11738 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65  t does not yet e
11739 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f  xist..  ** Do no
1173a 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69  t attempt to wri
1173b 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20 66  te if database f
1173c 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65  ile has never be
1173d 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a  en opened..  */.
1173e 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f    pPg = pager_lo
1173f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e  okup(pPager, pgn
11740 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  o);.  assert( pP
11741 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20  g || !MEMDB );. 
11742 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50 4c   PAGERTRACE(("PL
11743 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25  AYBACK %d page %
11744 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73 5c  d hash(%08x) %s\
11745 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
11746 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65     PAGERID(pPage
11747 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f  r), pgno, pager_
11748 64 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d  datahash(pPager-
11749 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61  >pageSize, aData
1174a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
1174b 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d    (isMainJrnl?"m
1174c 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75  ain-journal":"su
1174d 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29  b-journal").  ))
1174e 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 2d  ;.  if( (pPager-
1174f 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58  >state>=PAGER_EX
11750 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 28  CLUSIVE).   && (
11751 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50  pPg==0 || 0==(pP
11752 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
11753 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26  EED_SYNC)).   &&
11754 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
11755 66 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73  fd).   && !isUns
11756 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34  ync.  ){.    i64
11757 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29   ofst = (pgno-1)
11758 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61  *(i64)pPager->pa
11759 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d  geSize;.    rc =
1175a 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
1175b 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74  pPager->fd, aDat
1175c 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
1175d 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20  ize, ofst);.    
1175e 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d  if( pgno>pPager-
1175f 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20  >dbFileSize ){. 
11760 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46       pPager->dbF
11761 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a  ileSize = pgno;.
11762 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
11763 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50  3BackupUpdate(pP
11764 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70  ager->pBackup, p
11765 67 6e 6f 2c 20 61 44 61 74 61 29 3b 0a 20 20 7d  gno, aData);.  }
11766 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e  else if( !isMain
11767 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29  Jrnl && pPg==0 )
11768 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
11769 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f   is a rollback o
1176a 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e  f a savepoint an
1176b 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77  d data was not w
1176c 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a  ritten to.    **
1176d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
1176e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  d the page is no
1176f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65  t in-memory, the
11770 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61  re is a potentia
11771 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d  l.    ** problem
11772 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20  . When the page 
11773 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20  is next fetched 
11774 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  by the b-tree la
11775 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20  yer, it .    ** 
11776 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f  will be read fro
11777 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
11778 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f  ile, which may o
11779 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20  r may not be .  
1177a 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20    ** current. . 
1177b 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
1177c 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20  re are a couple 
1177d 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 79  of different way
1177e 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65  s this can happe
1177f 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65  n. All are quite
11780 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e  .    ** obscure.
11781 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e   When running in
11782 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64   synchronous mod
11783 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79  e, this can only
11784 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20   happen .    ** 
11785 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f  if the page is o
11786 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
11787 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
11788 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  the transaction,
11789 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70   then.    ** pop
1178a 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76  ulated, then mov
1178b 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
1178c 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e  PagerMovepage().
1178d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1178e 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74  he solution is t
1178f 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f  o add an in-memo
11790 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 63  ry page to the c
11791 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a  ache containing.
11792 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20      ** the data 
11793 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74  just read from t
11794 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20  he sub-journal. 
11795 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73  Mark the page as
11796 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61   dirty .    ** a
11797 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20  nd if the pager 
11798 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e  requires a journ
11799 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61  al-sync, then ma
1179a 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 0a  rk the page as .
1179b 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67      ** requiring
1179c 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20   a journal-sync 
1179d 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 69  before it is wri
1179e 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tten..    */.   
1179f 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70   assert( isSavep
117a0 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72  nt );.    if( (r
117a1 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
117a2 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20  Acquire(pPager, 
117a3 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 21  pgno, &pPg, 1))!
117a4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
117a5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
117a6 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 6c     }.    pPg->fl
117a7 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45  ags &= ~PGHDR_NE
117a8 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 6c  ED_READ;.    sql
117a9 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69  ite3PcacheMakeDi
117aa 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20  rty(pPg);.  }.  
117ab 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 2f  if( pPg ){.    /
117ac 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64  * No page should
117ad 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 69   ever be explici
117ae 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  tly rolled back 
117af 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20  that is in use, 
117b0 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f  except.    ** fo
117b1 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20 69  r page 1 which i
117b2 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69 6e  s held in use in
117b3 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74   order to keep t
117b4 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20  he lock on the. 
117b5 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 61     ** database a
117b6 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20 73  ctive. However s
117b7 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 20 62  uch a page may b
117b8 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73  e rolled back as
117b9 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a   a result.    **
117ba 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   of an internal 
117bb 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 20  error resulting 
117bc 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20  in an automatic 
117bd 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73  call to.    ** s
117be 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62  qlite3PagerRollb
117bf 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ack()..    */.  
117c0 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20    void *pData;. 
117c1 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d 3e     pData = pPg->
117c2 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70  pData;.    memcp
117c3 79 28 70 44 61 74 61 2c 20 61 44 61 74 61 2c 20  y(pData, aData, 
117c4 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
117c5 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
117c6 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 29 7b 0a  r->xReiniter ){.
117c7 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 78 52        pPager->xR
117c8 65 69 6e 69 74 65 72 28 70 50 67 29 3b 0a 20 20  einiter(pPg);.  
117c9 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4d 61    }.    if( isMa
117ca 69 6e 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61  inJrnl && (!isSa
117cb 76 65 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65  vepnt || *pOffse
117cc 74 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t<=pPager->journ
117cd 61 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20  alHdr) ){.      
117ce 2f 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e  /* If the conten
117cf 74 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  ts of this page 
117d0 77 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72  were just restor
117d1 65 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ed from the main
117d2 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e   .      ** journ
117d3 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74  al file, then it
117d4 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62  s content must b
117d5 65 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77  e as they were w
117d6 68 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a  hen the .      *
117d7 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61  * transaction wa
117d8 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20  s first opened. 
117d9 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20  In this case we 
117da 63 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67  can mark the pag
117db 65 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c  e.      ** as cl
117dc 65 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65  ean, since there
117dd 20 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64   will be no need
117de 20 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74   to write it out
117df 20 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a   to the..      *
117e0 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65  *.      ** There
117e1 20 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f   is one exceptio
117e2 6e 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20  n to this rule. 
117e3 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62  If the page is b
117e4 65 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20  eing rolled.    
117e5 20 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72    ** back as par
117e6 74 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74  t of a savepoint
117e7 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20   (or statement) 
117e8 72 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e  rollback from an
117e9 20 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e   .      ** unsyn
117ea 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74  ced portion of t
117eb 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
117ec 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73  file, then it is
117ed 20 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20   not safe.      
117ee 2a 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70  ** to mark the p
117ef 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68  age as clean. Th
117f0 69 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61  is is because ma
117f1 72 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61  rking the page a
117f2 73 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e  s.      ** clean
117f3 20 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20   will clear the 
117f4 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
117f5 66 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20  flag. Since the 
117f6 70 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a  page is.      **
117f7 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
117f8 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65  journal file (re
117f9 63 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e  corded in Pager.
117fa 70 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a  pInJournal) and.
117fb 20 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48        ** the PGH
117fc 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61  DR_NEED_SYNC fla
117fd 67 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66  g is cleared, if
117fe 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69   the page is wri
117ff 74 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a  tten to.      **
11800 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68   again within th
11801 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  is transaction, 
11802 69 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65  it will be marke
11803 64 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20  d as dirty but. 
11804 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44       ** the PGHD
11805 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
11806 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74   will not be set
11807 2e 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20  . It could then 
11808 70 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20  potentially.    
11809 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20    ** be written 
1180a 6f 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74  out into the dat
1180b 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72  abase file befor
1180c 65 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69  e its journal fi
1180d 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d  le.      ** segm
1180e 65 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49  ent is synced. I
1180f 66 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73  f a crash occurs
11810 20 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f   during or follo
11811 77 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20  wing this,.     
11812 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72   ** database cor
11813 72 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75  ruption may ensu
11814 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
11815 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
11816 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20  akeClean(pPg);. 
11817 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
11818 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20  TE_CHECK_PAGES. 
11819 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68     pPg->pageHash
1181a 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
1181b 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20  h(pPg);.#endif. 
1181c 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61     /* If this wa
1181d 73 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72  s page 1, then r
1181e 65 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65  estore the value
1181f 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65   of Pager.dbFile
11820 56 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20  Vers..    ** Do 
11821 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20  this before any 
11822 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20  decoding. */.   
11823 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a   if( pgno==1 ){.
11824 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50        memcpy(&pP
11825 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
11826 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b  , &((u8*)pData)[
11827 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65  24],sizeof(pPage
11828 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b  r->dbFileVers));
11829 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
1182a 65 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a  ecode the page j
1182b 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69  ust read from di
1182c 73 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31  sk */.    CODEC1
1182d 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20  (pPager, pData, 
1182e 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20  pPg->pgno, 3);. 
1182f 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
11830 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20  Release(pPg);.  
11831 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
11832 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e  ..#if !defined(N
11833 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65  DEBUG) || define
11834 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47  d(SQLITE_COVERAG
11835 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68  E_TEST)./*.** Th
11836 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73  is routine looks
11837 20 61 68 65 61 64 20 69 6e 74 6f 20 74 68 65 20   ahead into the 
11838 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  main journal fil
11839 65 20 61 6e 64 20 64 65 74 65 72 6d 69 6e 65 73  e and determines
1183a 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e  .** whether or n
1183b 6f 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f  ot the next reco
1183c 72 64 20 28 74 68 65 20 72 65 63 6f 72 64 20 74  rd (the record t
1183d 68 61 74 20 62 65 67 69 6e 73 20 61 74 20 66 69  hat begins at fi
1183e 6c 65 0a 2a 2a 20 6f 66 66 73 65 74 20 70 50 61  le.** offset pPa
1183f 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29  ger->journalOff)
11840 20 69 73 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65   is a well-forme
11841 64 20 70 61 67 65 20 72 65 63 6f 72 64 20 63 6f  d page record co
11842 6e 73 69 73 74 69 6e 67 0a 2a 2a 20 6f 66 20 61  nsisting.** of a
11843 20 76 61 6c 69 64 20 70 61 67 65 20 6e 75 6d 62   valid page numb
11844 65 72 2c 20 70 50 61 67 65 2d 3e 70 61 67 65 53  er, pPage->pageS
11845 69 7a 65 20 62 79 74 65 73 20 6f 66 20 63 6f 6e  ize bytes of con
11846 74 65 6e 74 2c 20 66 6f 6c 6c 6f 77 65 64 0a 2a  tent, followed.*
11847 2a 20 62 79 20 61 20 76 61 6c 69 64 20 63 68 65  * by a valid che
11848 63 6b 73 75 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cksum..**.** The
11849 20 70 61 67 65 72 20 6e 65 76 65 72 20 6e 65 65   pager never nee
1184a 64 73 20 74 6f 20 6b 6e 6f 77 20 74 68 69 73 20  ds to know this 
1184b 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 6f 20 69  in order to do i
1184c 74 73 20 6a 6f 62 2e 20 20 20 54 68 69 73 0a 2a  ts job.   This.*
1184d 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c  * routine is onl
1184e 79 20 75 73 65 64 20 66 72 6f 6d 20 77 69 74 68  y used from with
1184f 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 74 65   assert() and te
11850 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 73 2e  stcase() macros.
11851 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
11852 61 67 65 72 4e 65 78 74 4a 6f 75 72 6e 61 6c 50  agerNextJournalP
11853 61 67 65 49 73 56 61 6c 69 64 28 50 61 67 65 72  ageIsValid(Pager
11854 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 50 67 6e   *pPager){.  Pgn
11855 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20  o pgno;         
11856 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75    /* The page nu
11857 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65  mber of the page
11858 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b   */.  u32 cksum;
11859 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1185a 65 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20  e page checksum 
1185b 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
1185c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
1185d 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 72 65  urn code from re
1185e 61 64 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f  ad operations */
1185f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
11860 2a 66 64 3b 20 20 20 20 2f 2a 20 54 68 65 20 66  *fd;    /* The f
11861 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66  ile descriptor f
11862 72 6f 6d 20 77 68 69 63 68 20 77 65 20 61 72 65  rom which we are
11863 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 75 38   reading */.  u8
11864 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20   *aData;        
11865 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
11866 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20   the page */..  
11867 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65  /* Read the page
11868 20 6e 75 6d 62 65 72 20 68 65 61 64 65 72 20 2a   number header *
11869 2f 0a 20 20 66 64 20 3d 20 70 50 61 67 65 72 2d  /.  fd = pPager-
1186a 3e 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61  >jfd;.  rc = rea
1186b 64 33 32 62 69 74 73 28 66 64 2c 20 70 50 61 67  d32bits(fd, pPag
1186c 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20  er->journalOff, 
1186d 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 72 63  &pgno);.  if( rc
1186e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 72  !=SQLITE_OK ){ r
1186f 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20  eturn 0; }      
11870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11871 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f              /*NO
11872 5f 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67  _TEST*/.  if( pg
11873 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50  no==0 || pgno==P
11874 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
11875 67 65 72 29 20 29 7b 20 72 65 74 75 72 6e 20 30  ger) ){ return 0
11876 3b 20 7d 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f  ; }         /*NO
11877 5f 54 45 53 54 2a 2f 0a 20 20 69 66 28 20 70 67  _TEST*/.  if( pg
11878 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d  no>(Pgno)pPager-
11879 3e 64 62 53 69 7a 65 20 29 7b 20 72 65 74 75 72  >dbSize ){ retur
1187a 6e 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20  n 0; }          
1187b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f              /*NO
1187c 5f 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52 65  _TEST*/..  /* Re
1187d 61 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20  ad the checksum 
1187e 2a 2f 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32  */.  rc = read32
1187f 62 69 74 73 28 66 64 2c 20 70 50 61 67 65 72 2d  bits(fd, pPager-
11880 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 70 50 61 67  >journalOff+pPag
11881 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 2c 20  er->pageSize+4, 
11882 26 63 6b 73 75 6d 29 3b 0a 20 20 69 66 28 20 72  &cksum);.  if( r
11883 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20  c!=SQLITE_OK ){ 
11884 72 65 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20  return 0; }     
11885 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11886 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e               /*N
11887 4f 5f 54 45 53 54 2a 2f 0a 0a 20 20 2f 2a 20 52  O_TEST*/..  /* R
11888 65 61 64 20 74 68 65 20 64 61 74 61 20 61 6e 64  ead the data and
11889 20 76 65 72 69 66 79 20 74 68 65 20 63 68 65 63   verify the chec
1188a 6b 73 75 6d 20 2a 2f 0a 20 20 61 44 61 74 61 20  ksum */.  aData 
1188b 3d 20 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 70  = (u8*)pPager->p
1188c 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d  TmpSpace;.  rc =
1188d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66   sqlite3OsRead(f
1188e 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 65 72  d, aData, pPager
1188f 2d 3e 70 61 67 65 53 69 7a 65 2c 20 70 50 61 67  ->pageSize, pPag
11890 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 34  er->journalOff+4
11891 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
11892 49 54 45 5f 4f 4b 20 29 7b 20 72 65 74 75 72 6e  ITE_OK ){ return
11893 20 30 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20   0; }           
11894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11895 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54         /*NO_TEST
11896 2a 2f 0a 20 20 69 66 28 20 70 61 67 65 72 5f 63  */.  if( pager_c
11897 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61  ksum(pPager, aDa
11898 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 20 72 65  ta)!=cksum ){ re
11899 74 75 72 6e 20 30 3b 20 7d 20 20 20 20 20 20 20  turn 0; }       
1189a 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54         /*NO_TEST
1189b 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 63 68 20 74  */..  /* Reach t
1189c 68 69 73 20 70 6f 69 6e 74 20 6f 6e 6c 79 20 69  his point only i
1189d 66 20 74 68 65 20 70 61 67 65 20 69 73 20 76 61  f the page is va
1189e 6c 69 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  lid */.  return 
1189f 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  1;.}.#endif /* !
118a0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20  defined(NDEBUG) 
118a1 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
118a2 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29  E_COVERAGE_TEST)
118a3 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d   */../*.** Param
118a4 65 74 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20  eter zMaster is 
118a5 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61  the name of a ma
118a6 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
118a7 65 2e 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72  e. A single jour
118a8 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74  nal.** file that
118a9 20 72 65 66 65 72 72 65 64 20 74 6f 20 74 68 65   referred to the
118aa 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
118ab 66 69 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65  file has just be
118ac 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a  en rolled back..
118ad 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
118ae 63 68 65 63 6b 73 20 69 66 20 69 74 20 69 73 20  checks if it is 
118af 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65  possible to dele
118b0 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
118b1 75 72 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61  urnal file,.** a
118b2 6e 64 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74  nd does so if it
118b3 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d   is..**.** Argum
118b4 65 6e 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20  ent zMaster may 
118b5 70 6f 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70  point to Pager.p
118b6 54 6d 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61  TmpSpace. So tha
118b7 74 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20  t buffer is not 
118b8 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  .** available fo
118b9 72 20 75 73 65 20 77 69 74 68 69 6e 20 74 68 69  r use within thi
118ba 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  s function..**.*
118bb 2a 20 57 68 65 6e 20 61 20 6d 61 73 74 65 72 20  * When a master 
118bc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
118bd 63 72 65 61 74 65 64 2c 20 69 74 20 69 73 20 70  created, it is p
118be 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68  opulated with th
118bf 65 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 61  e names .** of a
118c0 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20  ll of its child 
118c1 6a 6f 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 61 66  journals, one af
118c2 74 65 72 20 61 6e 6f 74 68 65 72 2c 20 66 6f 72  ter another, for
118c3 6d 61 74 74 65 64 20 61 73 20 75 74 66 2d 38 20  matted as utf-8 
118c4 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 74 65 78 74  .** encoded text
118c5 2e 20 54 68 65 20 65 6e 64 20 6f 66 20 65 61 63  . The end of eac
118c6 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20  h child journal 
118c7 66 69 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 77  file is marked w
118c8 69 74 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65  ith a .** nul-te
118c9 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 28 30  rminator byte (0
118ca 78 30 30 29 2e 20 69 2e 65 2e 20 74 68 65 20 65  x00). i.e. the e
118cb 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f  ntire contents o
118cc 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  f a master journ
118cd 61 6c 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 61  al.** file for a
118ce 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 76   transaction inv
118cf 6f 6c 76 69 6e 67 20 74 77 6f 20 64 61 74 61 62  olving two datab
118d0 61 73 65 73 20 6d 69 67 68 74 20 62 65 3a 0a 2a  ases might be:.*
118d1 2a 0a 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f 62 69  *.**   "/home/bi
118d2 6c 6c 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c  ll/a.db-journal\
118d3 78 30 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e  x00/home/bill/b.
118d4 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 22 0a  db-journal\x00".
118d5 2a 2a 0a 2a 2a 20 41 20 6d 61 73 74 65 72 20 6a  **.** A master j
118d6 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20  ournal file may 
118d7 6f 6e 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  only be deleted 
118d8 6f 6e 63 65 20 61 6c 6c 20 6f 66 20 69 74 73 20  once all of its 
118d9 63 68 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61  child .** journa
118da 6c 73 20 68 61 76 65 20 62 65 65 6e 20 72 6f 6c  ls have been rol
118db 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  led back..**.** 
118dc 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
118dd 61 64 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ads the contents
118de 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a   of the master-j
118df 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f  ournal file into
118e0 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20   .** memory and 
118e1 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61  loops through ea
118e2 63 68 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  ch of the child 
118e3 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46  journal names. F
118e4 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64  or.** each child
118e5 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65   journal, it che
118e6 63 6b 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20  cks if:.**.**   
118e7 2a 20 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a  * if the child j
118e8 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2c 20 61  ournal exists, a
118e9 6e 64 20 69 66 20 73 6f 0a 2a 2a 20 20 20 2a 20  nd if so.**   * 
118ea 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75  if the child jou
118eb 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20  rnal contains a 
118ec 72 65 66 65 72 65 6e 63 65 20 74 6f 20 6d 61 73  reference to mas
118ed 74 65 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20  ter journal .** 
118ee 20 20 20 20 66 69 6c 65 20 7a 4d 61 73 74 65 72      file zMaster
118ef 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 68 69 6c  .**.** If a chil
118f0 64 20 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65  d journal can be
118f1 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 74 63   found that matc
118f2 68 65 73 20 62 6f 74 68 20 6f 66 20 74 68 65 20  hes both of the 
118f3 63 72 69 74 65 72 69 61 0a 2a 2a 20 61 62 6f 76  criteria.** abov
118f4 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  e, this function
118f5 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74   returns without
118f6 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e   doing anything.
118f7 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 0a 2a   Otherwise, if.*
118f8 2a 20 6e 6f 20 73 75 63 68 20 63 68 69 6c 64 20  * no such child 
118f9 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66  journal can be f
118fa 6f 75 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 73 74  ound, file zMast
118fb 65 72 20 69 73 20 64 65 6c 65 74 65 64 20 66 72  er is deleted fr
118fc 6f 6d 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73  om.** the file-s
118fd 79 73 74 65 6d 20 75 73 69 6e 67 20 73 71 6c 69  ystem using sqli
118fe 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a  te3OsDelete()..*
118ff 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72  *.** If an IO er
11900 72 6f 72 20 77 69 74 68 69 6e 20 74 68 69 73 20  ror within this 
11901 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 72 72  function, an err
11902 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
11903 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e  ned. This.** fun
11904 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20  ction allocates 
11905 6d 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e  memory by callin
11906 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  g sqlite3Malloc(
11907 29 2e 20 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74  ). If an allocat
11908 69 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51  ion.** fails, SQ
11909 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65  LITE_NOMEM is re
1190a 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
1190b 65 2c 20 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d  e, if no IO or m
1190c 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 0a 2a 2a  alloc errors .**
1190d 20 6f 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f   occur, SQLITE_O
1190e 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  K is returned..*
1190f 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20  *.** TODO: This 
11910 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74  function allocat
11911 65 73 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63  es a single bloc
11912 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c  k of memory to l
11913 6f 61 64 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72  oad.** the entir
11914 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
11915 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11916 20 66 69 6c 65 2e 20 54 68 69 73 20 63 6f 75 6c   file. This coul
11917 64 20 62 65 0a 2a 2a 20 61 20 63 6f 75 70 6c 65  d be.** a couple
11918 20 6f 66 20 6b 69 6c 6f 62 79 74 65 73 20 6f 72   of kilobytes or
11919 20 73 6f 20 2d 20 70 6f 74 65 6e 74 69 61 6c 6c   so - potentiall
1191a 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  y larger than th
1191b 65 20 70 61 67 65 20 0a 2a 2a 20 73 69 7a 65 2e  e page .** size.
1191c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
1191d 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50  ager_delmaster(P
1191e 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f  ager *pPager, co
1191f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
11920 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  r){.  sqlite3_vf
11921 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72  s *pVfs = pPager
11922 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63  ->pVfs;.  int rc
11923 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11924 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
11925 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  de */.  sqlite3_
11926 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 3b 20 20  file *pMaster;  
11927 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 6d 61    /* Malloc'd ma
11928 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster-journal fil
11929 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a  e descriptor */.
1192a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1192b 70 4a 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d  pJournal;   /* M
1192c 61 6c 6c 6f 63 27 64 20 63 68 69 6c 64 2d 6a 6f  alloc'd child-jo
1192d 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72  urnal file descr
1192e 69 70 74 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20  iptor */.  char 
1192f 2a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20  *zMasterJournal 
11930 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73  = 0; /* Contents
11931 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   of master journ
11932 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34  al file */.  i64
11933 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b   nMasterJournal;
11934 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
11935 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  f master journal
11936 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41   file */..  /* A
11937 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
11938 72 20 62 6f 74 68 20 74 68 65 20 70 4a 6f 75 72  r both the pJour
11939 6e 61 6c 20 61 6e 64 20 70 4d 61 73 74 65 72 20  nal and pMaster 
1193a 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
1193b 2e 0a 20 20 2a 2a 20 49 66 20 73 75 63 63 65 73  ..  ** If succes
1193c 73 66 75 6c 2c 20 6f 70 65 6e 20 74 68 65 20 6d  sful, open the m
1193d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1193e 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a  le for reading..
1193f 20 20 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d    */.  pMaster =
11940 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a   (sqlite3_file *
11941 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  )sqlite3MallocZe
11942 72 6f 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  ro(pVfs->szOsFil
11943 65 20 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e  e * 2);.  pJourn
11944 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69  al = (sqlite3_fi
11945 6c 65 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61  le *)(((u8 *)pMa
11946 73 74 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a  ster) + pVfs->sz
11947 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21  OsFile);.  if( !
11948 70 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72  pMaster ){.    r
11949 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1194a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
1194b 6f 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d  onst int flags =
1194c 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   (SQLITE_OPEN_RE
1194d 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50  ADONLY|SQLITE_OP
1194e 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41  EN_MASTER_JOURNA
1194f 4c 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  L);.    rc = sql
11950 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c  ite3OsOpen(pVfs,
11951 20 7a 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65   zMaster, pMaste
11952 72 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20  r, flags, 0);.  
11953 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  }.  if( rc!=SQLI
11954 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c  TE_OK ) goto del
11955 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 72  master_out;..  r
11956 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
11957 65 53 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26  eSize(pMaster, &
11958 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b  nMasterJournal);
11959 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1195a 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d  E_OK ) goto delm
1195b 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66  aster_out;..  if
1195c 28 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  ( nMasterJournal
1195d 3e 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  >0 ){.    char *
1195e 7a 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68  zJournal;.    ch
1195f 61 72 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d  ar *zMasterPtr =
11960 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73   0;.    int nMas
11961 74 65 72 50 74 72 20 3d 20 70 56 66 73 2d 3e 6d  terPtr = pVfs->m
11962 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20  xPathname+1;..  
11963 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e    /* Load the en
11964 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72  tire master jour
11965 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70  nal file into sp
11966 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ace obtained fro
11967 6d 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  m.    ** sqlite3
11968 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f  _malloc() and po
11969 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73  inted to by zMas
1196a 74 65 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20  terJournal. .   
1196b 20 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a   */.    zMasterJ
1196c 6f 75 72 6e 61 6c 20 3d 20 28 63 68 61 72 20 2a  ournal = (char *
1196d 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28  )sqlite3Malloc((
1196e 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 6e  int)nMasterJourn
1196f 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 72 29  al + nMasterPtr)
11970 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74  ;.    if( !zMast
11971 65 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  erJournal ){.   
11972 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
11973 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f  OMEM;.      goto
11974 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
11975 20 20 20 20 7d 0a 20 20 20 20 7a 4d 61 73 74 65      }.    zMaste
11976 72 50 74 72 20 3d 20 26 7a 4d 61 73 74 65 72 4a  rPtr = &zMasterJ
11977 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a 6f  ournal[nMasterJo
11978 75 72 6e 61 6c 5d 3b 0a 20 20 20 20 72 63 20 3d  urnal];.    rc =
11979 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70   sqlite3OsRead(p
1197a 4d 61 73 74 65 72 2c 20 7a 4d 61 73 74 65 72 4a  Master, zMasterJ
1197b 6f 75 72 6e 61 6c 2c 20 28 69 6e 74 29 6e 4d 61  ournal, (int)nMa
1197c 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b  sterJournal, 0);
1197d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1197e 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65  ITE_OK ) goto de
1197f 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20  lmaster_out;..  
11980 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61    zJournal = zMa
11981 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20  sterJournal;.   
11982 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61   while( (zJourna
11983 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  l-zMasterJournal
11984 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  )<nMasterJournal
11985 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78   ){.      int ex
11986 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d  ists;.      rc =
11987 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
11988 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c  (pVfs, zJournal,
11989 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
1198a 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b  XISTS, &exists);
1198b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1198c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1198d 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74      goto delmast
1198e 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  er_out;.      }.
1198f 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73        if( exists
11990 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f   ){.        /* O
11991 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ne of the journa
11992 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ls pointed to by
11993 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11994 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20  nal exists..    
11995 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61      ** Open it a
11996 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70  nd check if it p
11997 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73  oints at the mas
11998 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a  ter journal. If.
11999 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72          ** so, r
1199a 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65  eturn without de
1199b 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65  leting the maste
1199c 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  r journal file..
1199d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1199e 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20     int c;.      
1199f 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53    int flags = (S
119a0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
119a1 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  NLY|SQLITE_OPEN_
119a2 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20  MAIN_JOURNAL);. 
119a3 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
119a4 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
119a5 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e  zJournal, pJourn
119a6 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20  al, flags, 0);. 
119a7 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
119a8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
119a9 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
119aa 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20  ster_out;.      
119ab 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20    }..        rc 
119ac 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  = readMasterJour
119ad 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d  nal(pJournal, zM
119ae 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65  asterPtr, nMaste
119af 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73  rPtr);.        s
119b0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a  qlite3OsClose(pJ
119b1 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20  ournal);.       
119b2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
119b3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
119b4 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f  goto delmaster_o
119b5 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ut;.        }.. 
119b6 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74         c = zMast
119b7 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73  erPtr[0]!=0 && s
119b8 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72  trcmp(zMasterPtr
119b9 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20  , zMaster)==0;. 
119ba 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a         if( c ){.
119bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20            /* We 
119bc 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f  have a match. Do
119bd 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20   not delete the 
119be 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
119bf 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ile. */.        
119c0 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
119c1 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _out;.        }.
119c2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a        }.      zJ
119c3 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74  ournal += (sqlit
119c4 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72  e3Strlen30(zJour
119c5 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20  nal)+1);.    }. 
119c6 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c   }.  .  rc = sql
119c7 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
119c8 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a  s, zMaster, 0);.
119c9 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a  .delmaster_out:.
119ca 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75    if( zMasterJou
119cb 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  rnal ){.    sqli
119cc 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72  te3_free(zMaster
119cd 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a  Journal);.  }  .
119ce 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b    if( pMaster ){
119cf 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
119d0 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ose(pMaster);.  
119d1 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65    assert( !isOpe
119d2 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20  n(pJournal) );. 
119d3 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
119d4 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65  e(pMaster);.  re
119d5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
119d6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
119d7 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e   is used to chan
119d8 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69  ge the actual si
119d9 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
119da 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74  se .** file in t
119db 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20  he file-system. 
119dc 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e  This only happen
119dd 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e  s when committin
119de 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  g a transaction,
119df 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62  .** or rolling b
119e0 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ack a transactio
119e1 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c  n (including rol
119e2 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d  ling back a hot-
119e3 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20  journal)..**.** 
119e4 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  If the main data
119e5 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74  base file is not
119e6 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63   open, or an exc
119e7 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e  lusive lock is n
119e8 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73  ot.** held, this
119e9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
119ea 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c  o-op. Otherwise,
119eb 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
119ec 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e   file is.** chan
119ed 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67  ged to nPage pag
119ee 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72  es (nPage*pPager
119ef 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
119f0 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a  ). If the file.*
119f1 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72  * on disk is cur
119f2 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68  rently larger th
119f3 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20  an nPage pages, 
119f4 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53  then use the VFS
119f5 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20  .** xTruncate() 
119f6 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61  method to trunca
119f7 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c  te it..**.** Or,
119f8 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20   it might might 
119f9 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
119fa 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73   the file on dis
119fb 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  k is smaller tha
119fc 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65  n .** nPage page
119fd 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e  s. Some operatin
119fe 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65  g system impleme
119ff 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74  ntations can get
11a00 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a   confused if .**
11a01 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e   you try to trun
11a02 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73  cate a file to s
11a03 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73  ome size that is
11a04 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20   larger than it 
11a05 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73  .** currently is
11a06 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73  , so detect this
11a07 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20   case and write 
11a08 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79  a single zero by
11a09 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e  te to .** the en
11a0a 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c  d of the new fil
11a0b 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a  e instead..**.**
11a0c 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
11a0d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
11a0e 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  . If an IO error
11a0f 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f   occurs while mo
11a10 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64  difying.** the d
11a11 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65  atabase file, re
11a12 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63  turn the error c
11a13 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ode to the calle
11a14 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
11a15 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28   pager_truncate(
11a16 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
11a17 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e  gno nPage){.  in
11a18 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
11a19 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
11a1a 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
11a1b 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e  LUSIVE && isOpen
11a1c 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a  (pPager->fd) ){.
11a1d 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53      i64 currentS
11a1e 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20  ize, newSize;.  
11a1f 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74    /* TODO: Is it
11a20 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67   safe to use Pag
11a21 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65  er.dbFileSize he
11a22 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  re? */.    rc = 
11a23 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
11a24 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63  e(pPager->fd, &c
11a25 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20  urrentSize);.   
11a26 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65   newSize = pPage
11a27 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34  r->pageSize*(i64
11a28 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20  )nPage;.    if( 
11a29 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
11a2a 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65   currentSize!=ne
11a2b 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69  wSize ){.      i
11a2c 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e  f( currentSize>n
11a2d 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  ewSize ){.      
11a2e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11a2f 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
11a30 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20  >fd, newSize);. 
11a31 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
11a32 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
11a33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
11a34 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69  fd, "", 1, newSi
11a35 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ze-1);.      }. 
11a36 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
11a37 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
11a38 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65    pPager->dbFile
11a39 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20  Size = nPage;.  
11a3a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
11a3b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11a3c 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61  /*.** Set the va
11a3d 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72  lue of the Pager
11a3e 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69  .sectorSize vari
11a3f 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76  able for the giv
11a40 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65  en.** pager base
11a41 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72  d on the value r
11a42 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78  eturned by the x
11a43 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f  SectorSize metho
11a44 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e  d.** of the open
11a45 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
11a46 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  The sector size 
11a47 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65  will be used use
11a48 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69  d .** to determi
11a49 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20  ne the size and 
11a4a 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75  alignment of jou
11a4b 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20  rnal header and 
11a4c 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  .** master journ
11a4d 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68  al pointers with
11a4e 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e  in created journ
11a4f 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  al files..**.** 
11a50 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69  For temporary fi
11a51 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76  les the effectiv
11a52 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73  e sector size is
11a53 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65   always 512 byte
11a54 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  s..**.** Otherwi
11a55 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70  se, for non-temp
11a56 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65  orary files, the
11a57 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f   effective secto
11a58 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65  r size is.** the
11a59 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
11a5a 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69  by the xSectorSi
11a5b 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e  ze() method roun
11a5c 64 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 66  ded up to 512 if
11a5d 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74  .** it is less t
11a5e 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 6e  han 512, or roun
11a5f 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f  ded down to MAX_
11a60 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69  SECTOR_SIZE if i
11a61 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20  t.** is greater 
11a62 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f  than MAX_SECTOR_
11a63 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  SIZE..*/.static 
11a64 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 69  void setSectorSi
11a65 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
11a66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  ){.  assert( isO
11a67 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
11a68 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  || pPager->tempF
11a69 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70  ile );..  if( !p
11a6a 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
11a6b 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72  ){.    /* Sector
11a6c 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61   size doesn't ma
11a6d 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61  tter for tempora
11a6e 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20  ry files. Also, 
11a6f 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
11a70 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65  may not have bee
11a71 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e  n opened yet, in
11a72 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
11a73 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20  OsSectorSize(). 
11a74 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20     ** call will 
11a75 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f  segfault..    */
11a76 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
11a77 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  torSize = sqlite
11a78 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50  3OsSectorSize(pP
11a79 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20  ager->fd);.  }. 
11a7a 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63   if( pPager->sec
11a7b 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20  torSize<512 ){. 
11a7c 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f     pPager->secto
11a7d 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d  rSize = 512;.  }
11a7e 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
11a7f 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45  ectorSize>MAX_SE
11a80 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20  CTOR_SIZE ){.   
11a81 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43   assert( MAX_SEC
11a82 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b  TOR_SIZE>=512 );
11a83 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
11a84 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45  torSize = MAX_SE
11a85 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d  CTOR_SIZE;.  }.}
11a86 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b  ../*.** Playback
11a87 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64   the journal and
11a88 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68   thus restore th
11a89 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
11a8a 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20  to.** the state 
11a8b 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65  it was in before
11a8c 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69   we started maki
11a8d 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a  ng changes.  .**
11a8e 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
11a8f 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61  file format is a
11a90 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a  s follows: .**.*
11a91 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70  *  (1)  8 byte p
11a92 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f  refix.  A copy o
11a93 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b  f aJournalMagic[
11a94 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79  ]..**  (2)  4 by
11a95 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
11a96 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
11a97 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c  he number of val
11a98 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a  id page records.
11a99 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20  **       in the 
11a9a 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69  journal.  If thi
11a9b 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66  s value is 0xfff
11a9c 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70  fffff, then comp
11a9d 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  ute the.**      
11a9e 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20   number of page 
11a9f 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
11aa0 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a   journal size..*
11aa1 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62  *  (3)  4 byte b
11aa2 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
11aa3 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69  r which is the i
11aa4 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72  nitial value for
11aa5 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73   the .**       s
11aa6 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a  anity checksum..
11aa7 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20  **  (4)  4 byte 
11aa8 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
11aa9 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
11aaa 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65  ages to truncate
11aab 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61   the.**       da
11aac 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67  tabase to during
11aad 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20   a rollback..** 
11aae 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69 67   (5)  4 byte big
11aaf 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
11ab0 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65 63  which is the sec
11ab1 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 68  tor size.  The h
11ab2 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 69  eader.**       i
11ab3 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65  s this many byte
11ab4 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28  s in size..**  (
11ab5 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65  6)  4 byte big-e
11ab6 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68  ndian integer wh
11ab7 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65 20  ich is the page 
11ab8 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 34  case..**  (7)  4
11ab9 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68   byte integer wh
11aba 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ich is the numbe
11abb 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
11abc 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11abd 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e 20  .**       name. 
11abe 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 62   The value may b
11abf 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 65  e zero (indicate
11ac0 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e   that there is n
11ac1 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20  o master.**     
11ac2 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 20    journal.).**  
11ac3 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 20  (8)  N bytes of 
11ac4 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
11ac5 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e 61  al name.  The na
11ac6 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d 74  me will be nul-t
11ac7 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20  erminated.**    
11ac8 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 20     and might be 
11ac9 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 65  shorter than the
11aca 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d   value read from
11acb 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 69   (5).  If the fi
11acc 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20  rst byte.**     
11acd 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73    of the name is
11ace 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 65   \000 then there
11acf 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f   is no master jo
11ad0 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 74  urnal.  The mast
11ad1 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72  er.**       jour
11ad2 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f 72  nal name is stor
11ad3 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 20  ed in UTF-8..** 
11ad4 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f   (9)  Zero or mo
11ad5 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63  re pages instanc
11ad6 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c  es, each as foll
11ad7 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b  ows:.**        +
11ad8 20 20 34 20 62 79 74 65 20 70 61 67 65 20 6e 75    4 byte page nu
11ad9 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20  mber..**        
11ada 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  +  pPager->pageS
11adb 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  ize bytes of dat
11adc 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20  a..**        +  
11add 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a  4 byte checksum.
11ade 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70  **.** When we sp
11adf 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  eak of the journ
11ae0 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65  al header, we me
11ae1 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 69  an the first 8 i
11ae2 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45  tems above..** E
11ae3 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ach entry in the
11ae4 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69   journal is an i
11ae5 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 39  nstance of the 9
11ae6 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43  th item..**.** C
11ae7 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72  all the value fr
11ae8 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75  om the second bu
11ae9 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52  llet "nRec".  nR
11aea 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ec is the number
11aeb 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67   of.** valid pag
11aec 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  e entries in the
11aed 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f   journal.  In mo
11aee 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61  st cases, you ca
11aef 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a  n compute the.**
11af0 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66   value of nRec f
11af1 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20  rom the size of 
11af2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11af3 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 65  .  But if a powe
11af4 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63  r.** failure occ
11af5 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 20  urred while the 
11af6 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e  journal was bein
11af7 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f  g written, it co
11af8 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61  uld be the.** ca
11af9 73 65 20 74 68 61 74 20 74 68 65 20 73 69 7a 65  se that the size
11afa 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
11afb 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79  file had already
11afc 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20   been increased 
11afd 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61  but.** the extra
11afe 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74   entries had not
11aff 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 66   yet made it saf
11b00 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e  ely to disk.  In
11b01 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a   such a case,.**
11b02 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52   the value of nR
11b03 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d  ec computed from
11b04 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77   the file size w
11b05 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67  ould be too larg
11b06 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20  e.  For.** that 
11b07 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79  reason, we alway
11b08 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 76  s use the nRec v
11b09 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 64  alue in the head
11b0a 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  er..**.** If the
11b0b 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 30   nRec value is 0
11b0c 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 61  xffffffff it mea
11b0d 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f  ns that nRec sho
11b0e 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a  uld be computed.
11b0f 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65  ** from the file
11b10 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c   size.  This val
11b11 75 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  ue is used when 
11b12 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 73  the user selects
11b13 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20   the.** no-sync 
11b14 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a  option for the j
11b15 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72  ournal.  A power
11b16 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c   failure could l
11b17 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f  ead to corruptio
11b18 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73  n.** in this cas
11b19 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e  e.  But for thin
11b1a 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72  gs like temporar
11b1b 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77  y table (which w
11b1c 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65  ill be.** delete
11b1d 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72  d when the power
11b1e 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 65   is restored) we
11b1f 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a   don't care.  .*
11b20 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65  *.** If the file
11b21 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a   opened as the j
11b22 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
11b23 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  ot a well-formed
11b24 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
11b25 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20   then all pages 
11b26 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  up to the first 
11b27 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 61  corrupted page a
11b28 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63  re rolled.** bac
11b29 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69  k (or no pages i
11b2a 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  f the journal he
11b2b 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65  ader is corrupte
11b2c 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  d). The journal 
11b2d 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20  file.** is then 
11b2e 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49  deleted and SQLI
11b2f 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20  TE_OK returned, 
11b30 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f  just as if no co
11b31 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20  rruption had.** 
11b32 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64  been encountered
11b33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f  ..**.** If an I/
11b34 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72  O or malloc() er
11b35 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20  ror occurs, the 
11b36 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20  journal-file is 
11b37 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61  not deleted.** a
11b38 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  nd an error code
11b39 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
11b3a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61  .** The isHot pa
11b3b 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65  rameter indicate
11b3c 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72  s that we are tr
11b3d 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  ying to rollback
11b3e 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68   a journal.** th
11b3f 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f  at might be a ho
11b40 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20  t journal.  Or, 
11b41 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74  it could be that
11b42 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
11b43 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 65  .** preserved be
11b44 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c  cause of JOURNAL
11b45 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20  MODE_PERSIST or 
11b46 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e  JOURNALMODE_TRUN
11b47 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20  CATE..** If the 
11b48 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69  journal really i
11b49 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65  s hot, reset the
11b4a 20 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69   pager cache pri
11b4b 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61  or rolling.** ba
11b4c 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20  ck any content. 
11b4d 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
11b4e 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73  is merely persis
11b4f 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69  tent, no reset i
11b50 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a  s.** needed..*/.
11b51 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
11b52 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20  _playback(Pager 
11b53 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48  *pPager, int isH
11b54 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  ot){.  sqlite3_v
11b55 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65  fs *pVfs = pPage
11b56 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73  r->pVfs;.  i64 s
11b57 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  zJ;             
11b58 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
11b59 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11b5a 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33  in bytes */.  u3
11b5b 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20  2 nRec;         
11b5c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
11b5d 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74   of Records in t
11b5e 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  he journal */.  
11b5f 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20  u32 u;          
11b60 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69           /* Unsi
11b61 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65  gned loop counte
11b62 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67  r */.  Pgno mxPg
11b63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
11b64 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f  /* Size of the o
11b65 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20  riginal file in 
11b66 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72  pages */.  int r
11b67 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
11b68 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
11b69 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69  de of a subrouti
11b6a 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20  ne */.  int res 
11b6b 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
11b6c 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e   /* Value return
11b6d 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41  ed by sqlite3OsA
11b6e 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61  ccess() */.  cha
11b6f 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20  r *zMaster = 0; 
11b70 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
11b71 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11b72 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20  file if any */. 
11b73 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65   int needPagerRe
11b74 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  set;      /* Tru
11b75 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 20  e to reset page 
11b76 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70  prior to first p
11b77 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a  age rollback */.
11b78 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
11b79 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64   how many record
11b7a 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75  s are in the jou
11b7b 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72  rnal.  Abort ear
11b7c 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a  ly if.  ** the j
11b7d 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e  ournal is empty.
11b7e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
11b7f 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
11b80 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  fd) );.  rc = sq
11b81 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
11b82 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a  pPager->jfd, &sz
11b83 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  J);.  if( rc!=SQ
11b84 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d  LITE_OK || szJ==
11b85 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e  0 ){.    goto en
11b86 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a  d_playback;.  }.
11b87 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6d  .  /* Read the m
11b88 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
11b89 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  me from the jour
11b8a 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 72  nal, if it is pr
11b8b 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61  esent..  ** If a
11b8c 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11b8d 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65  file name is spe
11b8e 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 20  cified, but the 
11b8f 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a  file is not.  **
11b90 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b   present on disk
11b91 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
11b92 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e  al is not hot an
11b93 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  d does not need 
11b94 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65  to be.  ** playe
11b95 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a  d back..  **.  *
11b96 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61  * TODO: Technica
11b97 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  lly the followin
11b98 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 65  g is an error be
11b99 63 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 73  cause it assumes
11b9a 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 65   that.  ** buffe
11b9b 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63  r Pager.pTmpSpac
11b9c 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d 65  e is (mxPathname
11b9d 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 72  +1) bytes or lar
11b9e 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20  ger. i.e. that. 
11b9f 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 67   ** (pPager->pag
11ba0 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d  eSize >= pPager-
11ba1 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d  >pVfs->mxPathnam
11ba2 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75  e+1). Using os_u
11ba3 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50  nix.c,.  **  mxP
11ba4 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c 20  athname is 512, 
11ba5 77 68 69 63 68 20 69 73 20 74 68 65 20 73 61 6d  which is the sam
11ba6 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d  e as the minimum
11ba7 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65   allowable value
11ba8 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 69  .  ** for pageSi
11ba9 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74  ze..  */.  zMast
11baa 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d  er = pPager->pTm
11bab 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72  pSpace;.  rc = r
11bac 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  eadMasterJournal
11bad 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d  (pPager->jfd, zM
11bae 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70  aster, pPager->p
11baf 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
11bb0 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  1);.  if( rc==SQ
11bb1 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74  LITE_OK && zMast
11bb2 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 20  er[0] ){.    rc 
11bb3 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  = sqlite3OsAcces
11bb4 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c  s(pVfs, zMaster,
11bb5 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
11bb6 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20  XISTS, &res);.  
11bb7 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b  }.  zMaster = 0;
11bb8 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
11bb9 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a  E_OK || !res ){.
11bba 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
11bbb 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61  yback;.  }.  pPa
11bbc 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
11bbd 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 72  = 0;.  needPager
11bbe 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a  Reset = isHot;..
11bbf 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74    /* This loop t
11bc0 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72  erminates either
11bc1 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 72   when a readJour
11bc2 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 2a  nalHdr() or .  *
11bc3 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  * pager_playback
11bc4 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c 6c  _one_page() call
11bc5 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
11bc6 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72  DONE or an IO er
11bc7 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73  ror .  ** occurs
11bc8 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  . .  */.  while(
11bc9 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73   1 ){.    int is
11bca 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20  Unsync = 0;..   
11bcb 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78   /* Read the nex
11bcc 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  t journal header
11bcd 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
11bce 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72  l file.  If ther
11bcf 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74  e are.    ** not
11bd0 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c 65   enough bytes le
11bd1 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ft in the journa
11bd2 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d  l file for a com
11bd3 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f 72  plete header, or
11bd4 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 6f  .    ** it is co
11bd5 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 20  rrupted, then a 
11bd6 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 20  process must of 
11bd7 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69  failed while wri
11bd8 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20  ting it..    ** 
11bd9 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e  This indicates n
11bda 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64  othing more need
11bdb 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  s to be rolled b
11bdc 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ack..    */.    
11bdd 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c  rc = readJournal
11bde 48 64 72 28 70 50 61 67 65 72 2c 20 73 7a 4a 2c  Hdr(pPager, szJ,
11bdf 20 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0a   &nRec, &mxPg);.
11be0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
11be1 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 20  TE_OK ){ .      
11be2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
11be3 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ONE ){.        r
11be4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
11be5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
11be6 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
11be7 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
11be8 20 6e 52 65 63 20 69 73 20 30 78 66 66 66 66 66   nRec is 0xfffff
11be9 66 66 66 2c 20 74 68 65 6e 20 74 68 69 73 20 6a  fff, then this j
11bea 6f 75 72 6e 61 6c 20 77 61 73 20 63 72 65 61 74  ournal was creat
11beb 65 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 0a  ed by a process.
11bec 20 20 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 69      ** working i
11bed 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20  n no-sync mode. 
11bee 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20  This means that 
11bef 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20  the rest of the 
11bf0 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66  journal.    ** f
11bf1 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ile consists of 
11bf2 70 61 67 65 73 2c 20 74 68 65 72 65 20 61 72 65  pages, there are
11bf3 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 6c   no more journal
11bf4 20 68 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 74   headers. Comput
11bf5 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c  e.    ** the val
11bf6 75 65 20 6f 66 20 6e 52 65 63 20 62 61 73 65 64  ue of nRec based
11bf7 20 6f 6e 20 74 68 69 73 20 61 73 73 75 6d 70 74   on this assumpt
11bf8 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
11bf9 69 66 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 66  if( nRec==0xffff
11bfa 66 66 66 66 20 29 7b 0a 20 20 20 20 20 20 61 73  ffff ){.      as
11bfb 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f  sert( pPager->jo
11bfc 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41  urnalOff==JOURNA
11bfd 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
11bfe 20 29 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d   );.      nRec =
11bff 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f   (int)((szJ - JO
11c00 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
11c01 67 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47  ger))/JOURNAL_PG
11c02 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20  _SZ(pPager));.  
11c03 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e    }..    /* If n
11c04 52 65 63 20 69 73 20 30 20 61 6e 64 20 74 68 69  Rec is 0 and thi
11c05 73 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66  s rollback is of
11c06 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63   a transaction c
11c07 72 65 61 74 65 64 20 62 79 20 74 68 69 73 0a 20  reated by this. 
11c08 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e     ** process an
11c09 64 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  d if this is the
11c0a 20 66 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e   final header in
11c0b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68   the journal, th
11c0c 65 6e 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20  en it means.    
11c0d 2a 2a 20 74 68 61 74 20 74 68 69 73 20 70 61 72  ** that this par
11c0e 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
11c0f 20 77 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65   was being fille
11c10 64 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65  d but has not ye
11c11 74 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79  t been.    ** sy
11c12 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43  nced to disk.  C
11c13 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65  ompute the numbe
11c14 72 20 6f 66 20 70 61 67 65 73 20 62 61 73 65 64  r of pages based
11c15 20 6f 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e   on the remainin
11c16 67 0a 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66  g.    ** size of
11c17 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a   the file..    *
11c18 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69  *.    ** The thi
11c19 72 64 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74  rd term of the t
11c1a 65 73 74 20 77 61 73 20 61 64 64 65 64 20 74 6f  est was added to
11c1b 20 66 69 78 20 74 69 63 6b 65 74 20 23 32 35 36   fix ticket #256
11c1c 35 2e 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 72  5..    ** When r
11c1d 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f  olling back a ho
11c1e 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e 52 65 63 3d  t journal, nRec=
11c1f 3d 30 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20  =0 always means 
11c20 74 68 61 74 20 74 68 65 20 6e 65 78 74 0a 20 20  that the next.  
11c21 20 20 2a 2a 20 63 68 75 6e 6b 20 6f 66 20 74 68    ** chunk of th
11c22 65 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69  e journal contai
11c23 6e 73 20 7a 65 72 6f 20 70 61 67 65 73 20 74 6f  ns zero pages to
11c24 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   be rolled back.
11c25 20 20 42 75 74 0a 20 20 20 20 2a 2a 20 77 68 65    But.    ** whe
11c26 6e 20 64 6f 69 6e 67 20 61 20 52 4f 4c 4c 42 41  n doing a ROLLBA
11c27 43 4b 20 61 6e 64 20 74 68 65 20 6e 52 65 63 3d  CK and the nRec=
11c28 3d 30 20 63 68 75 6e 6b 20 69 73 20 74 68 65 20  =0 chunk is the 
11c29 6c 61 73 74 20 63 68 75 6e 6b 20 69 6e 0a 20 20  last chunk in.  
11c2a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c    ** the journal
11c2b 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
11c2c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68  the journal migh
11c2d 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69  t contain additi
11c2e 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65  onal.    ** page
11c2f 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62  s that need to b
11c30 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e  e rolled back an
11c31 64 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65  d that the numbe
11c32 72 20 6f 66 20 70 61 67 65 73 20 0a 20 20 20 20  r of pages .    
11c33 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d  ** should be com
11c34 70 75 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74  puted based on t
11c35 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11c36 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  size..    */.   
11c37 20 74 65 73 74 63 61 73 65 28 20 6e 52 65 63 3d   testcase( nRec=
11c38 3d 30 20 26 26 20 21 69 73 48 6f 74 0a 20 20 20  =0 && !isHot.   
11c39 20 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d        && pPager-
11c3a 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52  >journalHdr+JOUR
11c3b 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
11c3c 72 29 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  r)!=pPager->jour
11c3d 6e 61 6c 4f 66 66 0a 20 20 20 20 20 20 20 20 20  nalOff.         
11c3e 26 26 20 28 28 73 7a 4a 20 2d 20 70 50 61 67 65  && ((szJ - pPage
11c3f 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f  r->journalOff) /
11c40 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70   JOURNAL_PG_SZ(p
11c41 50 61 67 65 72 29 29 3e 30 0a 20 20 20 20 20 20  Pager))>0.      
11c42 20 20 20 26 26 20 70 61 67 65 72 4e 65 78 74 4a     && pagerNextJ
11c43 6f 75 72 6e 61 6c 50 61 67 65 49 73 56 61 6c 69  ournalPageIsVali
11c44 64 28 70 50 61 67 65 72 29 0a 20 20 20 20 29 3b  d(pPager).    );
11c45 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30  .    if( nRec==0
11c46 20 26 26 20 21 69 73 48 6f 74 20 26 26 0a 20 20   && !isHot &&.  
11c47 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
11c48 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c  urnalHdr+JOURNAL
11c49 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d  _HDR_SZ(pPager)=
11c4a 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  =pPager->journal
11c4b 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 6e 52 65  Off ){.      nRe
11c4c 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d  c = (int)((szJ -
11c4d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11c4e 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50  Off) / JOURNAL_P
11c4f 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20  G_SZ(pPager));. 
11c50 20 20 20 20 20 69 73 55 6e 73 79 6e 63 20 3d 20       isUnsync = 
11c51 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  1;.    }..    /*
11c52 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
11c53 66 69 72 73 74 20 68 65 61 64 65 72 20 72 65 61  first header rea
11c54 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  d from the journ
11c55 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65  al, truncate the
11c56 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
11c57 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74   file back to it
11c58 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e  s original size.
11c59 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
11c5a 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11c5b 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ff==JOURNAL_HDR_
11c5c 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  SZ(pPager) ){.  
11c5d 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74      rc = pager_t
11c5e 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20  runcate(pPager, 
11c5f 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28  mxPg);.      if(
11c60 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11c61 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  {.        goto e
11c62 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20  nd_playback;.   
11c63 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65     }.      pPage
11c64 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67  r->dbSize = mxPg
11c65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
11c66 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61  Copy original pa
11c67 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a  ges out of the j
11c68 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20  ournal and back 
11c69 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a  into the .    **
11c6a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
11c6b 6e 64 2f 6f 72 20 70 61 67 65 20 63 61 63 68 65  nd/or page cache
11c6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
11c6d 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75 2b  (u=0; u<nRec; u+
11c6e 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65  +){.      if( ne
11c6f 65 64 50 61 67 65 72 52 65 73 65 74 20 29 7b 0a  edPagerReset ){.
11c70 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65          pager_re
11c71 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20  set(pPager);.   
11c72 20 20 20 20 20 6e 65 65 64 50 61 67 65 72 52 65       needPagerRe
11c73 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  set = 0;.      }
11c74 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
11c75 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
11c76 61 67 65 28 70 50 61 67 65 72 2c 31 2c 69 73 55  age(pPager,1,isU
11c77 6e 73 79 6e 63 2c 26 70 50 61 67 65 72 2d 3e 6a  nsync,&pPager->j
11c78 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 30 29 3b 0a  ournalOff,0,0);.
11c79 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
11c7a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
11c7b 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
11c7c 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  E_DONE ){.      
11c7d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
11c7e 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50  OK;.          pP
11c7f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11c80 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 20 20   = szJ;.        
11c81 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
11c82 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
11c83 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 75    /* If we are u
11c84 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63  nable to rollbac
11c85 6b 2c 20 71 75 69 74 20 61 6e 64 20 72 65 74 75  k, quit and retu
11c86 72 6e 20 74 68 65 20 65 72 72 6f 72 0a 20 20 20  rn the error.   
11c87 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e 20         ** code. 
11c88 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65   This will cause
11c89 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e   the pager to en
11c8a 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74  ter the error st
11c8b 61 74 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ate.          **
11c8c 20 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72 74   so that no furt
11c8d 68 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62 65  her harm will be
11c8e 20 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73 20   done.  Perhaps 
11c8f 74 68 65 20 6e 65 78 74 0a 20 20 20 20 20 20 20  the next.       
11c90 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74 6f     ** process to
11c91 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c 6c   come along will
11c92 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c   be able to roll
11c93 62 61 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  back the databas
11c94 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
11c95 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
11c96 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20  nd_playback;.   
11c97 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
11c98 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54     }.  }.  /*NOT
11c99 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73 65  REACHED*/.  asse
11c9a 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c  rt( 0 );..end_pl
11c9b 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c  ayback:.  /* Fol
11c9c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63  lowing a rollbac
11c9d 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  k, the database 
11c9e 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 62  file should be b
11c9f 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 69 67 69  ack in its origi
11ca0 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 65 20 70  nal.  ** state p
11ca1 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72  rior to the star
11ca2 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  t of the transac
11ca3 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65 20  tion, so invoke 
11ca4 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  the.  ** SQLITE_
11ca5 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47  FCNTL_DB_UNCHANG
11ca6 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20  ED file-control 
11ca7 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 61 62 6c  method to disabl
11ca8 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 73 65 72  e the.  ** asser
11ca9 74 69 6f 6e 20 74 68 61 74 20 74 68 65 20 74 72  tion that the tr
11caa 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65  ansaction counte
11cab 72 20 77 61 73 20 6d 6f 64 69 66 69 65 64 2e 0a  r was modified..
11cac 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 0a 20    */.  assert(. 
11cad 20 20 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70     pPager->fd->p
11cae 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c 0a 20 20  Methods==0 ||.  
11caf 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43    sqlite3OsFileC
11cb0 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 2d 3e 66  ontrol(pPager->f
11cb1 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44  d,SQLITE_FCNTL_D
11cb2 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30 29 3e 3d  B_UNCHANGED,0)>=
11cb3 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 3b 0a 0a  SQLITE_OK.  );..
11cb4 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 6c 61    /* If this pla
11cb5 79 62 61 63 6b 20 69 73 20 68 61 70 70 65 6e 69  yback is happeni
11cb6 6e 67 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ng automatically
11cb7 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
11cb8 61 6e 20 49 4f 20 6f 72 20 0a 20 20 2a 2a 20 6d  an IO or .  ** m
11cb9 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74 68 61 74  alloc error that
11cba 20 6f 63 63 75 72 72 65 64 20 61 66 74 65 72 20   occurred after 
11cbb 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
11cbc 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20 62  er was updated b
11cbd 75 74 20 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20  ut .  ** before 
11cbe 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
11cbf 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74  was committed, t
11cc0 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63  hen the change-c
11cc1 6f 75 6e 74 65 72 20 0a 20 20 2a 2a 20 6d 6f 64  ounter .  ** mod
11cc2 69 66 69 63 61 74 69 6f 6e 20 6d 61 79 20 6a 75  ification may ju
11cc3 73 74 20 68 61 76 65 20 62 65 65 6e 20 72 65 76  st have been rev
11cc4 65 72 74 65 64 2e 20 49 66 20 74 68 69 73 20 68  erted. If this h
11cc5 61 70 70 65 6e 73 20 69 6e 20 65 78 63 6c 75 73  appens in exclus
11cc6 69 76 65 20 0a 20 20 2a 2a 20 6d 6f 64 65 2c 20  ive .  ** mode, 
11cc7 74 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20  then subsequent 
11cc8 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 70 65 72  transactions per
11cc9 66 6f 72 6d 65 64 20 62 79 20 74 68 65 20 63 6f  formed by the co
11cca 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f  nnection will no
11ccb 74 0a 20 20 2a 2a 20 75 70 64 61 74 65 20 74 68  t.  ** update th
11ccc 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
11ccd 20 61 74 20 61 6c 6c 2e 20 54 68 69 73 20 6d 61   at all. This ma
11cce 79 20 6c 65 61 64 20 74 6f 20 63 61 63 68 65 20  y lead to cache 
11ccf 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 20  inconsistency.  
11cd0 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  ** problems for 
11cd1 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20  other processes 
11cd2 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e  at some point in
11cd3 20 74 68 65 20 66 75 74 75 72 65 2e 20 53 6f 2c   the future. So,
11cd4 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e 20 63 61   just.  ** in ca
11cd5 73 65 20 74 68 69 73 20 68 61 73 20 68 61 70 70  se this has happ
11cd6 65 6e 65 64 2c 20 63 6c 65 61 72 20 74 68 65 20  ened, clear the 
11cd7 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20  changeCountDone 
11cd8 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20  flag now..  */. 
11cd9 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43   pPager->changeC
11cda 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65  ountDone = pPage
11cdb 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 0a 20 20  r->tempFile;..  
11cdc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
11cdd 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72  K ){.    zMaster
11cde 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53   = pPager->pTmpS
11cdf 70 61 63 65 3b 0a 20 20 20 20 72 63 20 3d 20 72  pace;.    rc = r
11ce0 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  eadMasterJournal
11ce1 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d  (pPager->jfd, zM
11ce2 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70  aster, pPager->p
11ce3 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
11ce4 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  1);.    testcase
11ce5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
11ce6 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
11ce7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11ce8 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64    rc = pager_end
11ce9 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  _transaction(pPa
11cea 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21  ger, zMaster[0]!
11ceb 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74 65 73 74  ='\0');.    test
11cec 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45  case( rc!=SQLITE
11ced 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28  _OK );.  }.  if(
11cee 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
11cef 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26 20  & zMaster[0] && 
11cf0 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  res ){.    /* If
11cf1 20 74 68 65 72 65 20 77 61 73 20 61 20 6d 61 73   there was a mas
11cf2 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  ter journal and 
11cf3 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
11cf4 6c 20 72 65 74 75 72 6e 20 73 75 63 63 65 73 73  l return success
11cf5 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20  ,.    ** see if 
11cf6 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
11cf7 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  o delete the mas
11cf8 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20  ter journal..   
11cf9 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67   */.    rc = pag
11cfa 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 50 61  er_delmaster(pPa
11cfb 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  ger, zMaster);. 
11cfc 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21     testcase( rc!
11cfd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
11cfe 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 67 65  }..  /* The Page
11cff 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72  r.sectorSize var
11d00 69 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20 62  iable may have b
11d01 65 65 6e 20 75 70 64 61 74 65 64 20 77 68 69 6c  een updated whil
11d02 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a 20 62  e rolling.  ** b
11d03 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 63 72  ack a journal cr
11d04 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65  eated by a proce
11d05 73 73 20 77 69 74 68 20 61 20 64 69 66 66 65 72  ss with a differ
11d06 65 6e 74 20 73 65 63 74 6f 72 20 73 69 7a 65 0a  ent sector size.
11d07 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 73 65    ** value. Rese
11d08 74 20 69 74 20 74 6f 20 74 68 65 20 63 6f 72 72  t it to the corr
11d09 65 63 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ect value for th
11d0a 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a 2f  is process..  */
11d0b 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65  .  setSectorSize
11d0c 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75  (pPager);.  retu
11d0d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11d0e 50 6c 61 79 62 61 63 6b 20 73 61 76 65 70 6f 69  Playback savepoi
11d0f 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 2e 20 4f  nt pSavepoint. O
11d10 72 2c 20 69 66 20 70 53 61 76 65 70 6f 69 6e 74  r, if pSavepoint
11d11 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 6c 61  ==NULL, then pla
11d12 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 6e 74  yback.** the ent
11d13 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  ire master journ
11d14 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 61 73  al file. The cas
11d15 65 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55  e pSavepoint==NU
11d16 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 0a  LL occurs when .
11d17 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f  ** a ROLLBACK TO
11d18 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e 76 6f   command is invo
11d19 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 50 4f 49  ked on a SAVEPOI
11d1a 4e 54 20 74 68 61 74 20 69 73 20 61 20 74 72 61  NT that is a tra
11d1b 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 61 76  nsaction .** sav
11d1c 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68  epoint..**.** Wh
11d1d 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73  en pSavepoint is
11d1e 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 6e 69   not NULL (meani
11d1f 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 63  ng a non-transac
11d20 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  tion savepoint i
11d21 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c 6c  s .** being roll
11d22 65 64 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 74  ed back), then t
11d23 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73  he rollback cons
11d24 69 73 74 73 20 6f 66 20 75 70 20 74 6f 20 74 68  ists of up to th
11d25 72 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a 20 70  ree stages,.** p
11d26 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 20  erformed in the 
11d27 6f 72 64 65 72 20 73 70 65 63 69 66 69 65 64 3a  order specified:
11d28 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73  .**.**   * Pages
11d29 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b   are played back
11d2a 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a   from the main j
11d2b 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20  ournal starting 
11d2c 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 6f  at byte.**     o
11d2d 66 66 73 65 74 20 50 61 67 65 72 53 61 76 65 70  ffset PagerSavep
11d2e 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64  oint.iOffset and
11d2f 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 0a   continuing to .
11d30 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65  **     PagerSave
11d31 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74  point.iHdrOffset
11d32 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 6e 64 20  , or to the end 
11d33 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  of the main jour
11d34 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20  nal.**     file 
11d35 69 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  if PagerSavepoin
11d36 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20  t.iHdrOffset is 
11d37 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  zero..**.**   * 
11d38 49 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  If PagerSavepoin
11d39 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20  t.iHdrOffset is 
11d3a 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 70  not zero, then p
11d3b 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 0a  ages are played.
11d3c 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 74 61 72  **     back star
11d3d 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f  ting from the jo
11d3e 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6d 6d  urnal header imm
11d3f 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
11d40 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72  ng .**     Pager
11d41 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66  Savepoint.iHdrOf
11d42 66 73 65 74 20 74 6f 20 74 68 65 20 65 6e 64 20  fset to the end 
11d43 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  of the main jour
11d44 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  nal file..**.** 
11d45 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 74 68    * Pages are th
11d46 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66  en played back f
11d47 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  rom the sub-jour
11d48 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 74 69  nal file, starti
11d49 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 20 74  ng.**     with t
11d4a 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  he PagerSavepoin
11d4b 74 2e 69 53 75 62 52 65 63 20 61 6e 64 20 63 6f  t.iSubRec and co
11d4c 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20  ntinuing to the 
11d4d 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 74 68  end of.**     th
11d4e 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
11d4f 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 74  **.** Throughout
11d50 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 72   the rollback pr
11d51 6f 63 65 73 73 2c 20 65 61 63 68 20 74 69 6d 65  ocess, each time
11d52 20 61 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65   a page is rolle
11d53 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 63  d back, the.** c
11d54 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74  orresponding bit
11d55 20 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 74   is set in a bit
11d56 76 65 63 20 73 74 72 75 63 74 75 72 65 20 28 76  vec structure (v
11d57 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 6e  ariable pDone in
11d58 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e   the.** implemen
11d59 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 54  tation below). T
11d5a 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 65  his is used to e
11d5b 6e 73 75 72 65 20 74 68 61 74 20 61 20 70 61 67  nsure that a pag
11d5c 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c  e is only.** rol
11d5d 6c 65 64 20 62 61 63 6b 20 74 68 65 20 66 69 72  led back the fir
11d5e 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e  st time it is en
11d5f 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69 74  countered in eit
11d60 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a  her journal..**.
11d61 2a 2a 20 49 66 20 70 53 61 76 65 70 6f 69 6e 74  ** If pSavepoint
11d62 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70   is NULL, then p
11d63 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 70 6c  ages are only pl
11d64 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74  ayed back from t
11d65 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e  he main.** journ
11d66 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 20 69  al file. There i
11d67 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20  s no need for a 
11d68 62 69 74 76 65 63 20 69 6e 20 74 68 69 73 20 63  bitvec in this c
11d69 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69  ase..**.** In ei
11d6a 74 68 65 72 20 63 61 73 65 2c 20 62 65 66 6f 72  ther case, befor
11d6b 65 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d 6d 65  e playback comme
11d6c 6e 63 65 73 20 74 68 65 20 50 61 67 65 72 2e 64  nces the Pager.d
11d6d 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a  bSize variable.*
11d6e 2a 20 69 73 20 72 65 73 65 74 20 74 6f 20 74 68  * is reset to th
11d6f 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 74 20  e value that it 
11d70 68 65 6c 64 20 61 74 20 74 68 65 20 73 74 61 72  held at the star
11d71 74 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69  t of the savepoi
11d72 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 6e 73  nt .** (or trans
11d73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 67 65  action). No page
11d74 20 77 69 74 68 20 61 20 70 61 67 65 2d 6e 75 6d   with a page-num
11d75 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e  ber greater than
11d76 20 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a 20 69   this value.** i
11d77 73 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 49  s played back. I
11d78 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 6e 74  f one is encount
11d79 65 72 65 64 20 69 74 20 69 73 20 73 69 6d 70 6c  ered it is simpl
11d7a 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a 73 74  y skipped..*/.st
11d7b 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 50 6c  atic int pagerPl
11d7c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28  aybackSavepoint(
11d7d 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
11d7e 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70  agerSavepoint *p
11d7f 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 36  Savepoint){.  i6
11d80 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20  4 szJ;          
11d81 20 20 20 20 20 20 20 2f 2a 20 45 66 66 65 63 74         /* Effect
11d82 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  ive size of the 
11d83 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  main journal */.
11d84 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20    i64 iHdrOff;  
11d85 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
11d86 64 20 6f 66 20 66 69 72 73 74 20 73 65 67 6d 65  d of first segme
11d87 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e  nt of main-journ
11d88 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a 20 20  al records */.  
11d89 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
11d8a 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75  OK;      /* Retu
11d8b 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 69 74  rn code */.  Bit
11d8c 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b 20  vec *pDone = 0; 
11d8d 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20        /* Bitvec 
11d8e 74 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 20  to ensure pages 
11d8f 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c 79  played back only
11d90 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65   once */..  asse
11d91 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
11d92 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20  e>=PAGER_SHARED 
11d93 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  );..  /* Allocat
11d94 65 20 61 20 62 69 74 76 65 63 20 74 6f 20 75 73  e a bitvec to us
11d95 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73  e to store the s
11d96 65 74 20 6f 66 20 70 61 67 65 73 20 72 6f 6c 6c  et of pages roll
11d97 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28  ed back */.  if(
11d98 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20   pSavepoint ){. 
11d99 20 20 20 70 44 6f 6e 65 20 3d 20 73 71 6c 69 74     pDone = sqlit
11d9a 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70  e3BitvecCreate(p
11d9b 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67  Savepoint->nOrig
11d9c 29 3b 0a 20 20 20 20 69 66 28 20 21 70 44 6f 6e  );.    if( !pDon
11d9d 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  e ){.      retur
11d9e 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
11d9f 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
11da0 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  Set the database
11da1 20 73 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 68   size back to th
11da2 65 20 76 61 6c 75 65 20 69 74 20 77 61 73 20 62  e value it was b
11da3 65 66 6f 72 65 20 74 68 65 20 73 61 76 65 70 6f  efore the savepo
11da4 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69 6e 67 20  int .  ** being 
11da5 72 65 76 65 72 74 65 64 20 77 61 73 20 6f 70 65  reverted was ope
11da6 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67  ned..  */.  pPag
11da7 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 53 61  er->dbSize = pSa
11da8 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 65 70  vepoint ? pSavep
11da9 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 70 50  oint->nOrig : pP
11daa 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
11dab 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70 50 61 67  ;..  /* Use pPag
11dac 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 61  er->journalOff a
11dad 73 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20  s the effective 
11dae 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e  size of the main
11daf 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a   rollback.  ** j
11db0 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 61 63 74  ournal.  The act
11db1 75 61 6c 20 66 69 6c 65 20 6d 69 67 68 74 20 62  ual file might b
11db2 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  e larger than th
11db3 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41 47 45 52  is in.  ** PAGER
11db4 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55  _JOURNALMODE_TRU
11db5 4e 43 41 54 45 20 6f 72 20 50 41 47 45 52 5f 4a  NCATE or PAGER_J
11db6 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49  OURNALMODE_PERSI
11db7 53 54 2e 20 20 42 75 74 20 61 6e 79 74 68 69 6e  ST.  But anythin
11db8 67 0a 20 20 2a 2a 20 70 61 73 74 20 70 50 61 67  g.  ** past pPag
11db9 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69  er->journalOff i
11dba 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20 74 6f 20  s off-limits to 
11dbb 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a 4a 20 3d  us..  */.  szJ =
11dbc 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11dbd 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  Off;..  /* Begin
11dbe 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b   by rolling back
11dbf 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68   records from th
11dc0 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73  e main journal s
11dc1 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20  tarting at.  ** 
11dc2 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69  PagerSavepoint.i
11dc3 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69  Offset and conti
11dc4 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 78  nuing to the nex
11dc5 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  t journal header
11dc6 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 6d 69 67  ..  ** There mig
11dc7 68 74 20 62 65 20 72 65 63 6f 72 64 73 20 69 6e  ht be records in
11dc8 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
11dc9 6c 20 74 68 61 74 20 68 61 76 65 20 61 20 70 61  l that have a pa
11dca 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 67  ge number.  ** g
11dcb 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
11dcc 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65  current database
11dcd 20 73 69 7a 65 20 28 70 50 61 67 65 72 2d 3e 64   size (pPager->d
11dce 62 53 69 7a 65 29 20 62 75 74 20 74 68 6f 73 65  bSize) but those
11dcf 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 6b  .  ** will be sk
11dd0 69 70 70 65 64 20 61 75 74 6f 6d 61 74 69 63 61  ipped automatica
11dd1 6c 6c 79 2e 20 20 50 61 67 65 73 20 61 72 65 20  lly.  Pages are 
11dd2 61 64 64 65 64 20 74 6f 20 70 44 6f 6e 65 20 61  added to pDone a
11dd3 73 20 74 68 65 79 0a 20 20 2a 2a 20 61 72 65 20  s they.  ** are 
11dd4 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a  played back..  *
11dd5 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69  /.  if( pSavepoi
11dd6 6e 74 20 29 7b 0a 20 20 20 20 69 48 64 72 4f 66  nt ){.    iHdrOf
11dd7 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e  f = pSavepoint->
11dd8 69 48 64 72 4f 66 66 73 65 74 20 3f 20 70 53 61  iHdrOffset ? pSa
11dd9 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66  vepoint->iHdrOff
11dda 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 20 20 70  set : szJ;.    p
11ddb 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11ddc 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e  f = pSavepoint->
11ddd 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69  iOffset;.    whi
11dde 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
11ddf 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75  K && pPager->jou
11de0 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f 66 66 20  rnalOff<iHdrOff 
11de1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  ){.      rc = pa
11de2 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65  ger_playback_one
11de3 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c  _page(pPager, 1,
11de4 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75   0, &pPager->jou
11de5 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e  rnalOff, 1, pDon
11de6 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  e);.    }.    as
11de7 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
11de8 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 65 6c 73 65  _DONE );.  }else
11de9 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
11dea 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20  urnalOff = 0;.  
11deb 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65  }..  /* Continue
11dec 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65   rolling back re
11ded 63 6f 72 64 73 20 6f 75 74 20 6f 66 20 74 68 65  cords out of the
11dee 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74   main journal st
11def 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 74  arting at.  ** t
11df0 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c  he first journal
11df1 20 68 65 61 64 65 72 20 73 65 65 6e 20 61 6e 64   header seen and
11df2 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75 6e 74 69   continuing unti
11df3 6c 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20  l the effective 
11df4 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  end.  ** of the 
11df5 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  main journal fil
11df6 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74 6f 20  e.  Continue to 
11df7 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 61 6e 67  skip out-of-rang
11df8 65 20 70 61 67 65 73 20 61 6e 64 0a 20 20 2a 2a  e pages and.  **
11df9 20 63 6f 6e 74 69 6e 75 65 20 61 64 64 69 6e 67   continue adding
11dfa 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61   pages rolled ba
11dfb 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a 20 20 2a  ck to pDone..  *
11dfc 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  /.  while( rc==S
11dfd 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67  QLITE_OK && pPag
11dfe 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73  er->journalOff<s
11dff 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32 20 69 69  zJ ){.    u32 ii
11e00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
11e01 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
11e02 20 20 20 20 75 33 32 20 6e 4a 52 65 63 20 3d 20      u32 nJRec = 
11e03 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  0;     /* Number
11e04 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f   of Journal Reco
11e05 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64  rds */.    u32 d
11e06 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72  ummy;.    rc = r
11e07 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  eadJournalHdr(pP
11e08 61 67 65 72 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65  ager, szJ, &nJRe
11e09 63 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20  c, &dummy);.    
11e0a 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
11e0b 54 45 5f 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20  TE_DONE );..    
11e0c 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 22 70  /*.    ** The "p
11e0d 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
11e0e 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  r+JOURNAL_HDR_SZ
11e0f 28 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72  (pPager)==pPager
11e10 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20  ->journalOff".  
11e11 20 20 2a 2a 20 74 65 73 74 20 69 73 20 72 65 6c    ** test is rel
11e12 61 74 65 64 20 74 6f 20 74 69 63 6b 65 74 20 23  ated to ticket #
11e13 32 35 36 35 2e 20 20 53 65 65 20 74 68 65 20 64  2565.  See the d
11e14 69 73 63 75 73 73 69 6f 6e 20 69 6e 20 74 68 65  iscussion in the
11e15 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c  .    ** pager_pl
11e16 61 79 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f  ayback() functio
11e17 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  n for additional
11e18 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20   information..  
11e19 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
11e1a 20 21 28 6e 4a 52 65 63 3d 3d 30 0a 20 20 20 20   !(nJRec==0.    
11e1b 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e       && pPager->
11e1c 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e  journalHdr+JOURN
11e1d 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
11e1e 29 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  )!=pPager->journ
11e1f 61 6c 4f 66 66 0a 20 20 20 20 20 20 20 20 20 26  alOff.         &
11e20 26 20 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72  & ((szJ - pPager
11e21 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20  ->journalOff) / 
11e22 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
11e23 61 67 65 72 29 29 3e 30 0a 20 20 20 20 20 20 20  ager))>0.       
11e24 20 20 26 26 20 70 61 67 65 72 4e 65 78 74 4a 6f    && pagerNextJo
11e25 75 72 6e 61 6c 50 61 67 65 49 73 56 61 6c 69 64  urnalPageIsValid
11e26 28 70 50 61 67 65 72 29 29 0a 20 20 20 20 29 3b  (pPager)).    );
11e27 0a 20 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d  .    if( nJRec==
11e28 30 20 0a 20 20 20 20 20 26 26 20 70 50 61 67 65  0 .     && pPage
11e29 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f  r->journalHdr+JO
11e2a 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
11e2b 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f  ger)==pPager->jo
11e2c 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a  urnalOff.    ){.
11e2d 20 20 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 75        nJRec = (u
11e2e 33 32 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65  32)((szJ - pPage
11e2f 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a  r->journalOff)/J
11e30 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61  OURNAL_PG_SZ(pPa
11e31 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ger));.    }.   
11e32 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53   for(ii=0; rc==S
11e33 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e  QLITE_OK && ii<n
11e34 4a 52 65 63 20 26 26 20 70 50 61 67 65 72 2d 3e  JRec && pPager->
11e35 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20  journalOff<szJ; 
11e36 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20  ii++){.      rc 
11e37 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
11e38 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72  _one_page(pPager
11e39 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d  , 1, 0, &pPager-
11e3a 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20  >journalOff, 1, 
11e3b 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pDone);.    }.  
11e3c 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
11e3d 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d  LITE_DONE );.  }
11e3e 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53  .  assert( rc!=S
11e3f 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
11e40 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d  er->journalOff==
11e41 73 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e  szJ );..  /* Fin
11e42 61 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20  ally,  rollback 
11e43 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73  pages from the s
11e44 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67  ub-journal.  Pag
11e45 65 20 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a  e that were.  **
11e46 20 70 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c   previously roll
11e47 65 64 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74  ed back out of t
11e48 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
11e49 28 61 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69  (and are hence i
11e4a 6e 20 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69  n pDone).  ** wi
11e4b 6c 6c 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20  ll be skipped.  
11e4c 4f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67  Out-of-range pag
11e4d 65 73 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70  es are also skip
11e4e 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ped..  */.  if( 
11e4f 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20  pSavepoint ){.  
11e50 20 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 20    u32 ii;       
11e51 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
11e52 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 20  nter */.    i64 
11e53 6f 66 66 73 65 74 20 3d 20 70 53 61 76 65 70 6f  offset = pSavepo
11e54 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a 28 34 2b  int->iSubRec*(4+
11e55 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11e56 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 53  );.    for(ii=pS
11e57 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65  avepoint->iSubRe
11e58 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  c; rc==SQLITE_OK
11e59 20 26 26 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e   && ii<pPager->n
11e5a 53 75 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20  SubRec; ii++){. 
11e5b 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 66 66       assert( off
11e5c 73 65 74 3d 3d 69 69 2a 28 34 2b 70 50 61 67 65  set==ii*(4+pPage
11e5d 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 29 3b 0a  r->pageSize) );.
11e5e 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
11e5f 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61  _playback_one_pa
11e60 67 65 28 70 50 61 67 65 72 2c 20 30 2c 20 30 2c  ge(pPager, 0, 0,
11e61 20 26 6f 66 66 73 65 74 2c 20 31 2c 20 70 44 6f   &offset, 1, pDo
11e62 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  ne);.    }.    a
11e63 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
11e64 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20  E_DONE );.  }.. 
11e65 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
11e66 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 20  stroy(pDone);.  
11e67 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
11e68 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  K ){.    pPager-
11e69 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a  >journalOff = sz
11e6a 4a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  J;.  }.  return 
11e6b 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  rc;.}../*.** Cha
11e6c 6e 67 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  nge the maximum 
11e6d 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d  number of in-mem
11e6e 6f 72 79 20 70 61 67 65 73 20 74 68 61 74 20 61  ory pages that a
11e6f 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53  re allowed..*/.S
11e70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
11e71 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
11e72 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65  etCachesize(Page
11e73 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d  r *pPager, int m
11e74 78 50 61 67 65 29 7b 0a 20 20 73 71 6c 69 74 65  xPage){.  sqlite
11e75 33 50 63 61 63 68 65 53 65 74 43 61 63 68 65 73  3PcacheSetCaches
11e76 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ize(pPager->pPCa
11e77 63 68 65 2c 20 6d 78 50 61 67 65 29 3b 0a 7d 0a  che, mxPage);.}.
11e78 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68  ./*.** Adjust th
11e79 65 20 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20  e robustness of 
11e7a 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
11e7b 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53  damage due to OS
11e7c 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70   crashes.** or p
11e7d 6f 77 65 72 20 66 61 69 6c 75 72 65 73 20 62 79  ower failures by
11e7e 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75   changing the nu
11e7f 6d 62 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73  mber of syncs()s
11e80 20 77 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a   when writing.**
11e81 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
11e82 75 72 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72  urnal.  There ar
11e83 65 20 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a  e three levels:.
11e84 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20  **.**    OFF    
11e85 20 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63     sqlite3OsSync
11e86 28 29 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c  () is never call
11e87 65 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  ed.  This is the
11e88 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20   default.**     
11e89 20 20 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d           for tem
11e8a 70 6f 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73  porary and trans
11e8b 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  ient files..**.*
11e8c 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54  *    NORMAL    T
11e8d 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79  he journal is sy
11e8e 6e 63 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65  nced once before
11e8f 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e   writes begin on
11e90 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
11e91 20 20 20 20 20 64 61 74 61 62 61 73 65 2e 20 20       database.  
11e92 54 68 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79  This is normally
11e93 20 61 64 65 71 75 61 74 65 20 70 72 6f 74 65 63   adequate protec
11e94 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20  tion, but.**    
11e95 20 20 20 20 20 20 20 20 20 20 69 74 20 69 73 20            it is 
11e96 74 68 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f  theoretically po
11e97 73 73 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76  ssible, though v
11e98 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a  ery unlikely,.**
11e99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
11e9a 61 74 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65  at an inopertune
11e9b 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63   power failure c
11e9c 6f 75 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a  ould leave the j
11e9d 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20  ournal.**       
11e9e 20 20 20 20 20 20 20 69 6e 20 61 20 73 74 61 74         in a stat
11e9f 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61  e which would ca
11ea0 75 73 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68  use damage to th
11ea1 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20  e database.**   
11ea2 20 20 20 20 20 20 20 20 20 20 20 77 68 65 6e 20             when 
11ea3 69 74 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63  it is rolled bac
11ea4 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c  k..**.**    FULL
11ea5 20 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61        The journa
11ea6 6c 20 69 73 20 73 79 6e 63 65 64 20 74 77 69 63  l is synced twic
11ea7 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20  e before writes 
11ea8 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20  begin on the.** 
11ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74               dat
11eaa 61 62 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65  abase (with some
11eab 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
11eac 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52  rmation - the nR
11ead 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20  ec field.**     
11eae 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20           of the 
11eaf 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d  journal header -
11eb0 20 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69   being written i
11eb1 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77  n between the tw
11eb2 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  o.**            
11eb3 20 20 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65    syncs).  If we
11eb4 20 61 73 73 75 6d 65 20 74 68 61 74 20 77 72 69   assume that wri
11eb5 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20  ting a.**       
11eb6 20 20 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69         single di
11eb7 73 6b 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f  sk sector is ato
11eb8 6d 69 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d  mic, then this m
11eb9 6f 64 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20  ode provides.** 
11eba 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73               ass
11ebb 75 72 61 6e 63 65 20 74 68 61 74 20 74 68 65 20  urance that the 
11ebc 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74  journal will not
11ebd 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f   be corrupted to
11ebe 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
11ebf 20 20 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61       point of ca
11ec0 75 73 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20  using damage to 
11ec1 74 68 65 20 64 61 74 61 62 61 73 65 20 64 75 72  the database dur
11ec2 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a  ing rollback..**
11ec3 0a 2a 2a 20 4e 75 6d 65 72 69 63 20 76 61 6c 75  .** Numeric valu
11ec4 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
11ec5 74 68 20 74 68 65 73 65 20 73 74 61 74 65 73 20  th these states 
11ec6 61 72 65 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d  are OFF==1, NORM
11ec7 41 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c  AL=2,.** and FUL
11ec8 4c 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  L=3..*/.#ifndef 
11ec9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45  SQLITE_OMIT_PAGE
11eca 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45  R_PRAGMAS.SQLITE
11ecb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
11ecc 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66  lite3PagerSetSaf
11ecd 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 20 2a  etyLevel(Pager *
11ece 70 50 61 67 65 72 2c 20 69 6e 74 20 6c 65 76 65  pPager, int leve
11ecf 6c 2c 20 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e  l, int bFullFsyn
11ed0 63 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f  c){.  pPager->no
11ed1 53 79 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d  Sync =  (level==
11ed2 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d  1 || pPager->tem
11ed3 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70  pFile) ?1:0;.  p
11ed4 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20  Pager->fullSync 
11ed5 3d 20 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21  = (level==3 && !
11ed6 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
11ed7 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72  ) ?1:0;.  pPager
11ed8 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28  ->sync_flags = (
11ed9 62 46 75 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54  bFullFsync?SQLIT
11eda 45 5f 53 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49  E_SYNC_FULL:SQLI
11edb 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b  TE_SYNC_NORMAL);
11edc 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e  .  if( pPager->n
11edd 6f 53 79 6e 63 20 29 20 70 50 61 67 65 72 2d 3e  oSync ) pPager->
11ede 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a  needSync = 0;.}.
11edf 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
11ee0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62  e following glob
11ee1 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69  al variable is i
11ee2 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65  ncremented whene
11ee3 76 65 72 20 74 68 65 20 6c 69 62 72 61 72 79 0a  ver the library.
11ee4 2a 2a 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6f  ** attempts to o
11ee5 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20  pen a temporary 
11ee6 66 69 6c 65 2e 20 20 54 68 69 73 20 69 6e 66 6f  file.  This info
11ee7 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20  rmation is used 
11ee8 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61  for.** testing a
11ee9 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79  nd analysis only
11eea 2e 20 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  .  .*/.#ifdef SQ
11eeb 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
11eec 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
11eed 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20  _opentemp_count 
11eee 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
11eef 2a 2a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72  ** Open a tempor
11ef0 61 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ary file..**.** 
11ef1 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64  Write the file d
11ef2 65 73 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a  escriptor into *
11ef3 70 46 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51  pFile. Return SQ
11ef4 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
11ef5 73 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f  ss .** or some o
11ef6 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20  ther error code 
11ef7 69 66 20 77 65 20 66 61 69 6c 2e 20 54 68 65 20  if we fail. The 
11ef8 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69  OS will automati
11ef9 63 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65  cally .** delete
11efa 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66   the temporary f
11efb 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73 20 63  ile when it is c
11efc 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  losed..**.** The
11efd 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f   flags passed to
11efe 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 20 78   the VFS layer x
11eff 4f 70 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 20  Open() call are 
11f00 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 0a  those specified.
11f01 2a 2a 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ** by parameter 
11f02 76 66 73 46 6c 61 67 73 20 4f 52 65 64 20 77 69  vfsFlags ORed wi
11f03 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  th the following
11f04 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49  :.**.**     SQLI
11f05 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
11f06 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f  E.**     SQLITE_
11f07 4f 50 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20  OPEN_CREATE.**  
11f08 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
11f09 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20  XCLUSIVE.**     
11f0a 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
11f0b 54 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61  TEONCLOSE.*/.sta
11f0c 74 69 63 20 69 6e 74 20 70 61 67 65 72 4f 70 65  tic int pagerOpe
11f0d 6e 74 65 6d 70 28 0a 20 20 50 61 67 65 72 20 2a  ntemp(.  Pager *
11f0e 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f  pPager,        /
11f0f 2a 20 54 68 65 20 70 61 67 65 72 20 6f 62 6a 65  * The pager obje
11f10 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ct */.  sqlite3_
11f11 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a  file *pFile,  /*
11f12 20 57 72 69 74 65 20 74 68 65 20 66 69 6c 65 20   Write the file 
11f13 64 65 73 63 72 69 70 74 6f 72 20 68 65 72 65 20  descriptor here 
11f14 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67  */.  int vfsFlag
11f15 73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c  s          /* Fl
11f16 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75  ags passed throu
11f17 67 68 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f  gh to the VFS */
11f18 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
11f19 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11f1a 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23  eturn code */..#
11f1b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
11f1c 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  T.  sqlite3_open
11f1d 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f  temp_count++;  /
11f1e 2a 20 55 73 65 64 20 66 6f 72 20 74 65 73 74 69  * Used for testi
11f1f 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20  ng and analysis 
11f20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  only */.#endif..
11f21 20 20 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53    vfsFlags |=  S
11f22 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
11f23 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50  RITE | SQLITE_OP
11f24 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20  EN_CREATE |.    
11f25 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
11f26 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20  PEN_EXCLUSIVE | 
11f27 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
11f28 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20  TEONCLOSE;.  rc 
11f29 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  = sqlite3OsOpen(
11f2a 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c  pPager->pVfs, 0,
11f2b 20 70 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73   pFile, vfsFlags
11f2c 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20  , 0);.  assert( 
11f2d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
11f2e 20 69 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 29   isOpen(pFile) )
11f2f 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
11f30 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
11f31 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e  busy handler fun
11f32 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ction..**.** The
11f33 20 70 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 74   pager invokes t
11f34 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20  he busy-handler 
11f35 69 66 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b  if sqlite3OsLock
11f36 28 29 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53  () returns .** S
11f37 51 4c 49 54 45 5f 42 55 53 59 20 77 68 65 6e 20  QLITE_BUSY when 
11f38 74 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64  trying to upgrad
11f39 65 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74  e from no-lock t
11f3a 6f 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c  o a SHARED lock,
11f3b 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 69  .** or when tryi
11f3c 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 72  ng to upgrade fr
11f3d 6f 6d 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  om a RESERVED lo
11f3e 63 6b 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49  ck to an EXCLUSI
11f3f 56 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20  VE .** lock. It 
11f40 64 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b  does *not* invok
11f41 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  e the busy handl
11f42 65 72 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e  er when upgradin
11f43 67 20 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44  g from.** SHARED
11f44 20 74 6f 20 52 45 53 45 52 56 45 44 2c 20 6f 72   to RESERVED, or
11f45 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20   when upgrading 
11f46 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 45  from SHARED to E
11f47 58 43 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 69  XCLUSIVE.** (whi
11f48 63 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  ch occurs during
11f49 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
11f4a 6c 62 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a  lback). Summary:
11f4b 0a 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74  .**.**   Transit
11f4c 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
11f4d 20 20 20 20 20 20 20 20 20 20 20 7c 20 49 6e 76             | Inv
11f4e 6f 6b 65 73 20 78 42 75 73 79 48 61 6e 64 6c 65  okes xBusyHandle
11f4f 72 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  r.**   ---------
11f50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11f51 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11f52 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
11f53 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20  **   NO_LOCK    
11f54 20 20 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43     -> SHARED_LOC
11f55 4b 20 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20  K      | Yes.** 
11f56 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20    SHARED_LOCK   
11f57 2d 3e 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  -> RESERVED_LOCK
11f58 20 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48      | No.**   SH
11f59 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45  ARED_LOCK   -> E
11f5a 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20  XCLUSIVE_LOCK   
11f5b 7c 20 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56  | No.**   RESERV
11f5c 45 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55  ED_LOCK -> EXCLU
11f5d 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65  SIVE_LOCK   | Ye
11f5e 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62  s.**.** If the b
11f5f 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c  usy-handler call
11f60 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e  back returns non
11f61 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20  -zero, the lock 
11f62 69 73 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20  is .** retried. 
11f63 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65  If it returns ze
11f64 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c  ro, then the SQL
11f65 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69  ITE_BUSY error i
11f66 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 6f  s.** returned to
11f67 20 74 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 74   the caller of t
11f68 68 65 20 70 61 67 65 72 20 41 50 49 20 66 75 6e  he pager API fun
11f69 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
11f6a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
11f6b 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73  lite3PagerSetBus
11f6c 79 68 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 65  yhandler(.  Page
11f6d 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
11f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11f6f 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74   /* Pager object
11f70 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73   */.  int (*xBus
11f71 79 48 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a  yHandler)(void *
11f72 29 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ),         /* Po
11f73 69 6e 74 65 72 20 74 6f 20 62 75 73 79 2d 68 61  inter to busy-ha
11f74 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a  ndler function *
11f75 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48  /.  void *pBusyH
11f76 61 6e 64 6c 65 72 41 72 67 20 20 20 20 20 20 20  andlerArg       
11f77 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
11f78 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20  ment to pass to 
11f79 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a  xBusyHandler */.
11f7a 29 7b 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78  ){  .  pPager->x
11f7b 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42  BusyHandler = xB
11f7c 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50  usyHandler;.  pP
11f7d 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
11f7e 65 72 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e  erArg = pBusyHan
11f7f 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a  dlerArg;.}../*.*
11f80 2a 20 53 65 74 20 74 68 65 20 72 65 69 6e 69 74  * Set the reinit
11f81 69 61 6c 69 7a 65 72 20 66 6f 72 20 74 68 69 73  ializer for this
11f82 20 70 61 67 65 72 2e 20 49 66 20 6e 6f 74 20 4e   pager. If not N
11f83 55 4c 4c 2c 20 74 68 65 20 72 65 69 6e 69 74 69  ULL, the reiniti
11f84 61 6c 69 7a 65 72 0a 2a 2a 20 69 73 20 63 61 6c  alizer.** is cal
11f85 6c 65 64 20 77 68 65 6e 20 74 68 65 20 63 6f 6e  led when the con
11f86 74 65 6e 74 20 6f 66 20 61 20 70 61 67 65 20 69  tent of a page i
11f87 6e 20 63 61 63 68 65 20 69 73 20 6d 6f 64 69 66  n cache is modif
11f88 69 65 64 20 28 72 65 73 74 6f 72 65 64 29 0a 2a  ied (restored).*
11f89 2a 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 74  * as part of a t
11f8a 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 73 61  ransaction or sa
11f8b 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b  vepoint rollback
11f8c 2e 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 67  . The callback g
11f8d 69 76 65 73 20 0a 2a 2a 20 68 69 67 68 65 72 2d  ives .** higher-
11f8e 6c 65 76 65 6c 20 63 6f 64 65 20 61 6e 20 6f 70  level code an op
11f8f 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 72 65 73  portunity to res
11f90 74 6f 72 65 20 74 68 65 20 45 58 54 52 41 20 73  tore the EXTRA s
11f91 65 63 74 69 6f 6e 20 74 6f 20 0a 2a 2a 20 61 67  ection to .** ag
11f92 72 65 65 20 77 69 74 68 20 74 68 65 20 72 65 73  ree with the res
11f93 74 6f 72 65 64 20 70 61 67 65 20 64 61 74 61 2e  tored page data.
11f94 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
11f95 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
11f96 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28  agerSetReiniter(
11f97 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 76  Pager *pPager, v
11f98 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44  oid (*xReinit)(D
11f99 62 50 61 67 65 2a 29 29 7b 0a 20 20 70 50 61 67  bPage*)){.  pPag
11f9a 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20  er->xReiniter = 
11f9b 78 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  xReinit;.}../*.*
11f9c 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 67  * Change the pag
11f9d 65 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 74  e size used by t
11f9e 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e  he Pager object.
11f9f 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 73 69   The new page si
11fa0 7a 65 20 0a 2a 2a 20 69 73 20 70 61 73 73 65 64  ze .** is passed
11fa1 20 69 6e 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a   in *pPageSize..
11fa2 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  **.** If the pag
11fa3 65 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72  er is in the err
11fa4 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  or state when th
11fa5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
11fa6 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a 20 69 73 20  alled, it.** is 
11fa7 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 76 61 6c  a no-op. The val
11fa8 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
11fa9 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65  he error state e
11faa 72 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e 20  rror code (i.e. 
11fab 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54  .** one of SQLIT
11fac 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f  E_IOERR, SQLITE_
11fad 43 4f 52 52 55 50 54 20 6f 72 20 53 51 4c 49 54  CORRUPT or SQLIT
11fae 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f  E_FULL)..**.** O
11faf 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6c 6c  therwise, if all
11fb0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
11fb1 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a  g are true:.**.*
11fb2 2a 20 20 20 2a 20 74 68 65 20 6e 65 77 20 70 61  *   * the new pa
11fb3 67 65 20 73 69 7a 65 20 28 76 61 6c 75 65 20 6f  ge size (value o
11fb4 66 20 2a 70 50 61 67 65 53 69 7a 65 29 20 69 73  f *pPageSize) is
11fb5 20 76 61 6c 69 64 20 28 61 20 70 6f 77 65 72 20   valid (a power 
11fb6 0a 2a 2a 20 20 20 20 20 6f 66 20 74 77 6f 20 62  .**     of two b
11fb7 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 53  etween 512 and S
11fb8 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
11fb9 49 5a 45 2c 20 69 6e 63 6c 75 73 69 76 65 29 2c  IZE, inclusive),
11fba 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74   and.**.**   * t
11fbb 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73  here are no outs
11fbc 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66  tanding page ref
11fbd 65 72 65 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a 0a  erences, and.**.
11fbe 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 62  **   * the datab
11fbf 61 73 65 20 69 73 20 65 69 74 68 65 72 20 6e 6f  ase is either no
11fc0 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  t an in-memory d
11fc1 61 74 61 62 61 73 65 20 6f 72 20 69 74 20 69 73  atabase or it is
11fc2 0a 2a 2a 20 20 20 20 20 61 6e 20 69 6e 2d 6d 65  .**     an in-me
11fc3 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 74 68  mory database th
11fc4 61 74 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  at currently con
11fc5 73 69 73 74 73 20 6f 66 20 7a 65 72 6f 20 70 61  sists of zero pa
11fc6 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20  ges..**.** then 
11fc7 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74  the pager object
11fc8 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 65   page size is se
11fc9 74 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65 2e  t to *pPageSize.
11fca 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
11fcb 67 65 20 73 69 7a 65 20 69 73 20 63 68 61 6e 67  ge size is chang
11fcc 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  ed, then this fu
11fcd 6e 63 74 69 6f 6e 20 75 73 65 73 20 73 71 6c 69  nction uses sqli
11fce 74 65 33 50 61 67 65 72 4d 61 6c 6c 6f 63 28 29  te3PagerMalloc()
11fcf 20 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61   .** to obtain a
11fd0 20 6e 65 77 20 50 61 67 65 72 2e 70 54 6d 70 53   new Pager.pTmpS
11fd1 70 61 63 65 20 62 75 66 66 65 72 2e 20 49 66 20  pace buffer. If 
11fd2 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  this allocation 
11fd3 61 74 74 65 6d 70 74 20 0a 2a 2a 20 66 61 69 6c  attempt .** fail
11fd4 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  s, SQLITE_NOMEM 
11fd5 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
11fd6 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 72 65  the page size re
11fd7 6d 61 69 6e 73 20 75 6e 63 68 61 6e 67 65 64 2e  mains unchanged.
11fd8 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74 68 65   .** In all othe
11fd9 72 20 63 61 73 65 73 2c 20 53 51 4c 49 54 45 5f  r cases, SQLITE_
11fda 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
11fdb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  **.** If the pag
11fdc 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 20 63 68  e size is not ch
11fdd 61 6e 67 65 64 2c 20 65 69 74 68 65 72 20 62 65  anged, either be
11fde 63 61 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 65  cause one of the
11fdf 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 63   enumerated.** c
11fe0 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20  onditions above 
11fe1 69 73 20 6e 6f 74 20 74 72 75 65 2c 20 74 68 65  is not true, the
11fe2 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 72   pager was in er
11fe3 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74  ror state when t
11fe4 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
11fe5 77 61 73 20 63 61 6c 6c 65 64 2c 20 6f 72 20 62  was called, or b
11fe6 65 63 61 75 73 65 20 74 68 65 20 6d 65 6d 6f 72  ecause the memor
11fe7 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74  y allocation att
11fe8 65 6d 70 74 20 66 61 69 6c 65 64 2c 20 0a 2a 2a  empt failed, .**
11fe9 20 74 68 65 6e 20 2a 70 50 61 67 65 53 69 7a 65   then *pPageSize
11fea 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6f   is set to the o
11feb 6c 64 2c 20 72 65 74 61 69 6e 65 64 20 70 61 67  ld, retained pag
11fec 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 72 65  e size before re
11fed 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  turning..*/.SQLI
11fee 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
11fef 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
11ff0 67 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50  gesize(Pager *pP
11ff1 61 67 65 72 2c 20 75 31 36 20 2a 70 50 61 67 65  ager, u16 *pPage
11ff2 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  Size){.  int rc 
11ff3 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  = pPager->errCod
11ff4 65 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  e;.  if( rc==SQL
11ff5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 31  ITE_OK ){.    u1
11ff6 36 20 70 61 67 65 53 69 7a 65 20 3d 20 2a 70 50  6 pageSize = *pP
11ff7 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 61 73 73  ageSize;.    ass
11ff8 65 72 74 28 20 70 61 67 65 53 69 7a 65 3d 3d 30  ert( pageSize==0
11ff9 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e 3d 35   || (pageSize>=5
11ffa 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d  12 && pageSize<=
11ffb 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
11ffc 53 49 5a 45 29 20 29 3b 0a 20 20 20 20 69 66 28  SIZE) );.    if(
11ffd 20 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67   pageSize && pag
11ffe 65 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70  eSize!=pPager->p
11fff 61 67 65 53 69 7a 65 20 0a 20 20 20 20 20 26 26  ageSize .     &&
12000 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d   (pPager->memDb=
12001 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62  =0 || pPager->db
12002 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20 20 26 26  Size==0).     &&
12003 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
12004 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
12005 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20  PCache)==0 .    
12006 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70  ){.      char *p
12007 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73 71  New = (char *)sq
12008 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
12009 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20  pageSize);.     
1200a 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20   if( !pNew ){.  
1200b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1200c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
1200d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 61  else{.        pa
1200e 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
1200f 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  );.        pPage
12010 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61  r->pageSize = pa
12011 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20  geSize;.        
12012 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
12013 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
12014 65 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67  e);.        pPag
12015 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20  er->pTmpSpace = 
12016 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71  pNew;.        sq
12017 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61  lite3PcacheSetPa
12018 67 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70  geSize(pPager->p
12019 50 43 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65  PCache, pageSize
1201a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1201b 0a 20 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20  .    *pPageSize 
1201c 3d 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 70  = (u16)pPager->p
1201d 61 67 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72  ageSize;.  }.  r
1201e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1201f 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
12020 74 65 72 20 74 6f 20 74 68 65 20 22 74 65 6d 70  ter to the "temp
12021 6f 72 61 72 79 20 70 61 67 65 22 20 62 75 66 66  orary page" buff
12022 65 72 20 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c  er held internal
12023 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 70 61 67  ly.** by the pag
12024 65 72 2e 20 20 54 68 69 73 20 69 73 20 61 20 62  er.  This is a b
12025 75 66 66 65 72 20 74 68 61 74 20 69 73 20 62 69  uffer that is bi
12026 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  g enough to hold
12027 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63   the.** entire c
12028 6f 6e 74 65 6e 74 20 6f 66 20 61 20 64 61 74 61  ontent of a data
12029 62 61 73 65 20 70 61 67 65 2e 20 20 54 68 69 73  base page.  This
1202a 20 62 75 66 66 65 72 20 69 73 20 75 73 65 64 20   buffer is used 
1202b 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75  internally.** du
1202c 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e  ring rollback an
1202d 64 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 72  d will be overwr
1202e 69 74 74 65 6e 20 77 68 65 6e 65 76 65 72 20 61  itten whenever a
1202f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63   rollback.** occ
12030 75 72 73 2e 20 20 42 75 74 20 6f 74 68 65 72 20  urs.  But other 
12031 6d 6f 64 75 6c 65 73 20 61 72 65 20 66 72 65 65  modules are free
12032 20 74 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c 20   to use it too, 
12033 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f  as long as.** no
12034 20 72 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20 68   rollbacks are h
12035 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  appening..*/.SQL
12036 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
12037 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 65   *sqlite3PagerTe
12038 6d 70 53 70 61 63 65 28 50 61 67 65 72 20 2a 70  mpSpace(Pager *p
12039 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
1203a 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61   pPager->pTmpSpa
1203b 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74  ce;.}../*.** Att
1203c 65 6d 70 74 20 74 6f 20 73 65 74 20 74 68 65 20  empt to set the 
1203d 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65  maximum database
1203e 20 70 61 67 65 20 63 6f 75 6e 74 20 69 66 20 6d   page count if m
1203f 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76  xPage is positiv
12040 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63  e. .** Make no c
12041 68 61 6e 67 65 73 20 69 66 20 6d 78 50 61 67 65  hanges if mxPage
12042 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61   is zero or nega
12043 74 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 65 72  tive.  And never
12044 20 72 65 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d   reduce the.** m
12045 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e  aximum page coun
12046 74 20 62 65 6c 6f 77 20 74 68 65 20 63 75 72 72  t below the curr
12047 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20  ent size of the 
12048 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
12049 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6d 78  Regardless of mx
1204a 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65  Page, return the
1204b 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d   current maximum
1204c 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a   page count..*/.
1204d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1204e 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d  nt sqlite3PagerM
1204f 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 65  axPageCount(Page
12050 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d  r *pPager, int m
12051 78 50 61 67 65 29 7b 0a 20 20 69 66 28 20 6d 78  xPage){.  if( mx
12052 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 70 50  Page>0 ){.    pP
12053 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d  ager->mxPgno = m
12054 78 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c  xPage;.  }.  sql
12055 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
12056 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20  nt(pPager, 0);. 
12057 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
12058 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  mxPgno;.}../*.**
12059 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
1205a 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61  et of routines a
1205b 72 65 20 75 73 65 64 20 74 6f 20 64 69 73 61 62  re used to disab
1205c 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65 64  le the simulated
1205d 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65  .** I/O error me
1205e 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 20  chanism.  These 
1205f 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
12060 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c  d to avoid simul
12061 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69  ated.** errors i
12062 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 77  n places where w
12063 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62  e do not care ab
12064 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a  out errors..**.*
12065 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54  * Unless -DSQLIT
12066 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65 64  E_TEST=1 is used
12067 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  , these routines
12068 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a   are all no-ops.
12069 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20  ** and generate 
1206a 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64  no code..*/.#ifd
1206b 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
1206c 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e  QLITE_API extern
1206d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
1206e 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53  error_pending;.S
1206f 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e  QLITE_API extern
12070 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
12071 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69  error_hit;.stati
12072 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b  c int saved_cnt;
12073 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69  .void disable_si
12074 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
12075 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 65 64  s(void){.  saved
12076 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 69  _cnt = sqlite3_i
12077 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b  o_error_pending;
12078 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
12079 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31  ror_pending = -1
1207a 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f  ;.}.void enable_
1207b 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
1207c 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c  ors(void){.  sql
1207d 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
1207e 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e  nding = saved_cn
1207f 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66  t;.}.#else.# def
12080 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  ine disable_simu
12081 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
12082 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c  ).# define enabl
12083 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
12084 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a  rrors().#endif..
12085 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 66  /*.** Read the f
12086 69 72 73 74 20 4e 20 62 79 74 65 73 20 66 72 6f  irst N bytes fro
12087 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
12088 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 74 6f  of the file into
12089 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20   memory.** that 
1208a 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e  pDest points to.
1208b 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70   .**.** If the p
1208c 61 67 65 72 20 77 61 73 20 6f 70 65 6e 65 64 20  ager was opened 
1208d 6f 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20 66  on a transient f
1208e 69 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d  ile (zFilename==
1208f 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f 70 65 6e 65  ""), or.** opene
12090 64 20 6f 6e 20 61 20 66 69 6c 65 20 6c 65 73 73  d on a file less
12091 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 69 6e   than N bytes in
12092 20 73 69 7a 65 2c 20 74 68 65 20 6f 75 74 70 75   size, the outpu
12093 74 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 7a  t buffer is.** z
12094 65 72 6f 65 64 20 61 6e 64 20 53 51 4c 49 54 45  eroed and SQLITE
12095 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 54 68  _OK returned. Th
12096 65 20 72 61 74 69 6f 6e 61 6c 65 20 66 6f 72 20  e rationale for 
12097 74 68 69 73 20 69 73 20 74 68 61 74 20 74 68 69  this is that thi
12098 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69  s .** function i
12099 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64  s used to read d
1209a 61 74 61 62 61 73 65 20 68 65 61 64 65 72 73 2c  atabase headers,
1209b 20 61 6e 64 20 61 20 6e 65 77 20 74 72 61 6e 73   and a new trans
1209c 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20  ient or.** zero 
1209d 73 69 7a 65 64 20 64 61 74 61 62 61 73 65 20 68  sized database h
1209e 61 73 20 61 20 68 65 61 64 65 72 20 74 68 61 6e  as a header than
1209f 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65   consists entire
120a0 6c 79 20 6f 66 20 7a 65 72 6f 65 73 2e 0a 2a 2a  ly of zeroes..**
120a1 0a 2a 2a 20 49 66 20 61 6e 79 20 49 4f 20 65 72  .** If any IO er
120a2 72 6f 72 20 61 70 61 72 74 20 66 72 6f 6d 20 53  ror apart from S
120a3 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
120a4 54 5f 52 45 41 44 20 69 73 20 65 6e 63 6f 75 6e  T_READ is encoun
120a5 74 65 72 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72  tered,.** the er
120a6 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
120a7 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c  rned to the call
120a8 65 72 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65  er and the conte
120a9 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 75  nts of the.** ou
120aa 74 70 75 74 20 62 75 66 66 65 72 20 75 6e 64 65  tput buffer unde
120ab 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fined..*/.SQLITE
120ac 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
120ad 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c  ite3PagerReadFil
120ae 65 68 65 61 64 65 72 28 50 61 67 65 72 20 2a 70  eheader(Pager *p
120af 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 75 6e  Pager, int N, un
120b0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44 65  signed char *pDe
120b1 73 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  st){.  int rc = 
120b2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65 6d  SQLITE_OK;.  mem
120b3 73 65 74 28 70 44 65 73 74 2c 20 30 2c 20 4e 29  set(pDest, 0, N)
120b4 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ;.  assert( isOp
120b5 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
120b6 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  | pPager->tempFi
120b7 6c 65 20 29 3b 0a 20 20 69 66 28 20 69 73 4f 70  le );.  if( isOp
120b8 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29  en(pPager->fd) )
120b9 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  {.    IOTRACE(("
120ba 44 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e 22  DBHDR %p 0 %d\n"
120bb 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 20  , pPager, N)).  
120bc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
120bd 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c  Read(pPager->fd,
120be 20 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20   pDest, N, 0);. 
120bf 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
120c0 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45  E_IOERR_SHORT_RE
120c1 41 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  AD ){.      rc =
120c2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
120c3 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
120c4 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
120c5 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  rn the total num
120c6 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
120c7 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
120c8 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a  e associated .**
120c9 20 77 69 74 68 20 70 50 61 67 65 72 2e 20 4e 6f   with pPager. No
120ca 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20  rmally, this is 
120cb 63 61 6c 63 75 6c 61 74 65 64 20 61 73 20 28 3c  calculated as (<
120cc 64 62 20 66 69 6c 65 20 73 69 7a 65 3e 2f 3c 70  db file size>/<p
120cd 61 67 65 2d 73 69 7a 65 3e 29 2e 0a 2a 2a 20 48  age-size>)..** H
120ce 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 66  owever, if the f
120cf 69 6c 65 20 69 73 20 62 65 74 77 65 65 6e 20 31  ile is between 1
120d0 20 61 6e 64 20 3c 70 61 67 65 2d 73 69 7a 65 3e   and <page-size>
120d1 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20   bytes in size, 
120d2 74 68 65 6e 20 0a 2a 2a 20 74 68 69 73 20 69 73  then .** this is
120d3 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 31 20   considered a 1 
120d4 70 61 67 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  page file..**.**
120d5 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73   If the pager is
120d6 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20   in error state 
120d7 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
120d8 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  on is called, th
120d9 65 6e 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20  en the.** error 
120da 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65  state error code
120db 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
120dc 20 2a 70 6e 50 61 67 65 20 6c 65 66 74 20 75 6e   *pnPage left un
120dd 63 68 61 6e 67 65 64 2e 20 4f 72 2c 0a 2a 2a 20  changed. Or,.** 
120de 69 66 20 74 68 65 20 66 69 6c 65 20 73 79 73 74  if the file syst
120df 65 6d 20 68 61 73 20 74 6f 20 62 65 20 71 75 65  em has to be que
120e0 72 69 65 64 20 66 6f 72 20 74 68 65 20 73 69 7a  ried for the siz
120e1 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e  e of the file an
120e2 64 0a 2a 2a 20 74 68 65 20 71 75 65 72 79 20 61  d.** the query a
120e3 74 74 65 6d 70 74 20 72 65 74 75 72 6e 73 20 61  ttempt returns a
120e4 6e 20 49 4f 20 65 72 72 6f 72 2c 20 74 68 65 20  n IO error, the 
120e5 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  IO error code is
120e6 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64   returned.** and
120e7 20 2a 70 6e 50 61 67 65 20 69 73 20 6c 65 66 74   *pnPage is left
120e8 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a   unchanged..**.*
120e9 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  * Otherwise, if 
120ea 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75  everything is su
120eb 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 53  ccessful, then S
120ec 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
120ed 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50  rned.** and *pnP
120ee 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 74 68  age is set to th
120ef 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
120f0 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
120f1 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
120f2 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
120f3 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50  PagerPagecount(P
120f4 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
120f5 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 50 67  t *pnPage){.  Pg
120f6 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20  no nPage;       
120f7 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
120f8 20 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20 2a   to return via *
120f9 70 6e 50 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20  pnPage */..  /* 
120fa 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
120fb 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65  already in the e
120fc 72 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 75  rror state, retu
120fd 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  rn the error cod
120fe 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  e. */.  if( pPag
120ff 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20  er->errCode ){. 
12100 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72     return pPager
12101 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a  ->errCode;.  }..
12102 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74    /* Determine t
12103 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
12104 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20  es in the file. 
12105 53 74 6f 72 65 20 74 68 69 73 20 69 6e 20 6e 50  Store this in nP
12106 61 67 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50  age. */.  if( pP
12107 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
12108 64 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d  d ){.    nPage =
12109 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b   pPager->dbSize;
1210a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
1210b 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
1210c 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 72        /* Error r
1210d 65 74 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c  eturned by OsFil
1210e 65 53 69 7a 65 28 29 20 2a 2f 0a 20 20 20 20 69  eSize() */.    i
1210f 36 34 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  64 n = 0;       
12110 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 73         /* File s
12111 69 7a 65 20 69 6e 20 62 79 74 65 73 20 72 65 74  ize in bytes ret
12112 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53  urned by OsFileS
12113 69 7a 65 28 29 20 2a 2f 0a 0a 20 20 20 20 61 73  ize() */..    as
12114 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61  sert( isOpen(pPa
12115 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67  ger->fd) || pPag
12116 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a  er->tempFile );.
12117 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70      if( isOpen(p
12118 50 61 67 65 72 2d 3e 66 64 29 20 26 26 20 28 30  Pager->fd) && (0
12119 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65   != (rc = sqlite
1211a 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67  3OsFileSize(pPag
1211b 65 72 2d 3e 66 64 2c 20 26 6e 29 29 29 20 29 7b  er->fd, &n))) ){
1211c 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 72  .      pager_err
1211d 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
1211e 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1211f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
12120 3e 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d 3e  >0 && n<pPager->
12121 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  pageSize ){.    
12122 20 20 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 20    nPage = 1;.   
12123 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50   }else{.      nP
12124 61 67 65 20 3d 20 28 50 67 6e 6f 29 28 6e 20 2f  age = (Pgno)(n /
12125 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
12126 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
12127 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21  ( pPager->state!
12128 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b  =PAGER_UNLOCK ){
12129 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
1212a 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20  bSize = nPage;. 
1212b 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46       pPager->dbF
1212c 69 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b  ileSize = nPage;
1212d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
1212e 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 31 3b 0a  bSizeValid = 1;.
1212f 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
12130 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e  If the current n
12131 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
12132 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 67 72  n the file is gr
12133 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 0a  eater than the .
12134 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20    ** configured 
12135 6d 61 78 69 6d 75 6d 20 70 61 67 65 72 20 6e 75  maximum pager nu
12136 6d 62 65 72 2c 20 69 6e 63 72 65 61 73 65 20 74  mber, increase t
12137 68 65 20 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 74  he allowed limit
12138 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 74 68   so.  ** that th
12139 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 72 65  e file can be re
1213a 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  ad..  */.  if( n
1213b 50 61 67 65 3e 70 50 61 67 65 72 2d 3e 6d 78 50  Page>pPager->mxP
1213c 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65  gno ){.    pPage
1213d 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e  r->mxPgno = (Pgn
1213e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d 0a 0a 20 20  o)nPage;.  }..  
1213f 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 75  /* Set the outpu
12140 74 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 72  t variable and r
12141 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
12142 2a 2f 0a 20 20 69 66 28 20 70 6e 50 61 67 65 20  */.  if( pnPage 
12143 29 7b 0a 20 20 20 20 2a 70 6e 50 61 67 65 20 3d  ){.    *pnPage =
12144 20 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 72 65   nPage;.  }.  re
12145 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
12146 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f  }.../*.** Try to
12147 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f   obtain a lock o
12148 66 20 74 79 70 65 20 6c 6f 63 6b 74 79 70 65 20  f type locktype 
12149 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
1214a 66 69 6c 65 2e 20 49 66 0a 2a 2a 20 61 20 73 69  file. If.** a si
1214b 6d 69 6c 61 72 20 6f 72 20 67 72 65 61 74 65 72  milar or greater
1214c 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79   lock is already
1214d 20 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e 63   held, this func
1214e 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 0a  tion is a no-op.
1214f 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 53 51  ** (returning SQ
12150 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65 64 69 61 74  LITE_OK immediat
12151 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  ely)..**.** Othe
12152 72 77 69 73 65 2c 20 61 74 74 65 6d 70 74 20 74  rwise, attempt t
12153 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63  o obtain the loc
12154 6b 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f  k using sqlite3O
12155 73 4c 6f 63 6b 28 29 2e 20 49 6e 76 6f 6b 65 20  sLock(). Invoke 
12156 0a 2a 2a 20 74 68 65 20 62 75 73 79 20 63 61 6c  .** the busy cal
12157 6c 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f 63  lback if the loc
12158 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6e  k is currently n
12159 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 52 65  ot available. Re
1215a 70 65 61 74 20 0a 2a 2a 20 75 6e 74 69 6c 20 74  peat .** until t
1215b 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
1215c 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 6f   returns false o
1215d 72 20 75 6e 74 69 6c 20 74 68 65 20 61 74 74 65  r until the atte
1215e 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f 62 74 61 69  mpt to .** obtai
1215f 6e 20 74 68 65 20 6c 6f 63 6b 20 73 75 63 63 65  n the lock succe
12160 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  eds..**.** Retur
12161 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
12162 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65 72  uccess and an er
12163 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 63  ror code if we c
12164 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20  annot obtain.** 
12165 74 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65  the lock. If the
12166 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65   lock is obtaine
12167 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  d successfully, 
12168 73 65 74 20 74 68 65 20 50 61 67 65 72 2e 73 74  set the Pager.st
12169 61 74 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65  ate .** variable
1216a 20 74 6f 20 6c 6f 63 6b 74 79 70 65 20 62 65 66   to locktype bef
1216b 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
1216c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
1216d 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28  er_wait_on_lock(
1216e 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
1216f 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20  nt locktype){.  
12170 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
12171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12172 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
12173 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  ode */..  /* The
12174 20 4f 53 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20   OS lock values 
12175 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65  must be the same
12176 20 61 73 20 74 68 65 20 50 61 67 65 72 20 6c 6f   as the Pager lo
12177 63 6b 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 61  ck values */.  a
12178 73 73 65 72 74 28 20 50 41 47 45 52 5f 53 48 41  ssert( PAGER_SHA
12179 52 45 44 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  RED==SHARED_LOCK
1217a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41   );.  assert( PA
1217b 47 45 52 5f 52 45 53 45 52 56 45 44 3d 3d 52 45  GER_RESERVED==RE
1217c 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SERVED_LOCK );. 
1217d 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 45   assert( PAGER_E
1217e 58 43 4c 55 53 49 56 45 3d 3d 45 58 43 4c 55 53  XCLUSIVE==EXCLUS
1217f 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f  IVE_LOCK );..  /
12180 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73  * If the file is
12181 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63   currently unloc
12182 6b 65 64 20 74 68 65 6e 20 74 68 65 20 73 69 7a  ked then the siz
12183 65 20 6d 75 73 74 20 62 65 20 75 6e 6b 6e 6f 77  e must be unknow
12184 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  n */.  assert( p
12185 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
12186 47 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 50  GER_SHARED || pP
12187 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
12188 64 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 68  d==0 );..  /* Ch
12189 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73  eck that this is
1218a 20 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70 20   either a no-op 
1218b 28 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71  (because the req
1218c 75 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20 0a  uested lock is .
1218d 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c    ** already hel
1218e 64 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  d, or one of the
1218f 20 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74 68   transistions th
12190 61 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64  at the busy-hand
12191 6c 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20  ler.  ** may be 
12192 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c 20  invoked during, 
12193 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
12194 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 20   comment above. 
12195 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72   ** sqlite3Pager
12196 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 29  SetBusyhandler()
12197 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
12198 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e   (pPager->state>
12199 3d 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20 20  =locktype).     
1219a 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 74    || (pPager->st
1219b 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43  ate==PAGER_UNLOC
1219c 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 50  K && locktype==P
1219d 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20  AGER_SHARED).   
1219e 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e      || (pPager->
1219f 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53  state==PAGER_RES
121a0 45 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79 70  ERVED && locktyp
121a1 65 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49  e==PAGER_EXCLUSI
121a2 56 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20  VE).  );..  if( 
121a3 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 6c  pPager->state>=l
121a4 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72  ocktype ){.    r
121a5 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
121a6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 7b   }else{.    do {
121a7 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
121a8 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72  te3OsLock(pPager
121a9 2d 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 3b  ->fd, locktype);
121aa 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d  .    }while( rc=
121ab 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20  =SQLITE_BUSY && 
121ac 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e  pPager->xBusyHan
121ad 64 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 75  dler(pPager->pBu
121ae 73 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 3b  syHandlerArg) );
121af 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
121b0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
121b1 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
121b2 28 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  (u8)locktype;.  
121b3 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c 4f      IOTRACE(("LO
121b4 43 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61  CK %p %d\n", pPa
121b5 67 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29 0a  ger, locktype)).
121b6 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
121b7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
121b8 54 72 75 6e 63 61 74 65 20 74 68 65 20 69 6e 2d  Truncate the in-
121b9 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
121ba 66 69 6c 65 20 69 6d 61 67 65 20 74 6f 20 6e 50  file image to nP
121bb 61 67 65 20 70 61 67 65 73 2e 20 54 68 69 73 20  age pages. This 
121bc 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  .** function doe
121bd 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d  s not actually m
121be 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61  odify the databa
121bf 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  se file on disk.
121c0 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20 73 65 74   It .** just set
121c1 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  s the internal s
121c2 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 65  tate of the page
121c3 72 20 6f 62 6a 65 63 74 20 73 6f 20 74 68 61 74  r object so that
121c4 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e 63 61 74   the .** truncat
121c5 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 6e 65  ion will be done
121c6 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e   when the curren
121c7 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  t transaction is
121c8 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2f 0a 53   committed..*/.S
121c9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
121ca 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 54  id sqlite3PagerT
121cb 72 75 6e 63 61 74 65 49 6d 61 67 65 28 50 61 67  runcateImage(Pag
121cc 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
121cd 20 6e 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72   nPage){.  asser
121ce 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  t( pPager->dbSiz
121cf 65 56 61 6c 69 64 20 29 3b 0a 20 20 61 73 73 65  eValid );.  asse
121d0 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  rt( pPager->dbSi
121d1 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a 20 20 61  ze>=nPage );.  a
121d2 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
121d3 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45  tate>=PAGER_RESE
121d4 52 56 45 44 20 29 3b 0a 20 20 70 50 61 67 65 72  RVED );.  pPager
121d5 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65  ->dbSize = nPage
121d6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64  ;.}../*.** Shutd
121d7 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63  own the page cac
121d8 68 65 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d 65  he.  Free all me
121d9 6d 6f 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 61  mory and close a
121da 6c 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  ll files..**.** 
121db 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  If a transaction
121dc 20 77 61 73 20 69 6e 20 70 72 6f 67 72 65 73 73   was in progress
121dd 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
121de 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  ne is called, th
121df 61 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  at.** transactio
121e0 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n is rolled back
121e1 2e 20 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64 69  .  All outstandi
121e2 6e 67 20 70 61 67 65 73 20 61 72 65 20 69 6e 76  ng pages are inv
121e3 61 6c 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64 20  alidated.** and 
121e4 74 68 65 69 72 20 6d 65 6d 6f 72 79 20 69 73 20  their memory is 
121e5 66 72 65 65 64 2e 20 20 41 6e 79 20 61 74 74 65  freed.  Any atte
121e6 6d 70 74 20 74 6f 20 75 73 65 20 61 20 70 61 67  mpt to use a pag
121e7 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  e associated.** 
121e8 77 69 74 68 20 74 68 69 73 20 70 61 67 65 20 63  with this page c
121e9 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73 20  ache after this 
121ea 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
121eb 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20   will likely.** 
121ec 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 65  result in a core
121ed 64 75 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  dump..**.** This
121ee 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73   function always
121ef 20 73 75 63 63 65 65 64 73 2e 20 49 66 20 61 20   succeeds. If a 
121f0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
121f1 63 74 69 76 65 20 61 6e 20 61 74 74 65 6d 70 74  ctive an attempt
121f2 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 72  .** is made to r
121f3 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 49 66 20  oll it back. If 
121f4 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
121f5 64 75 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62  during the rollb
121f6 61 63 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f  ack .** a hot jo
121f7 75 72 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65 66  urnal may be lef
121f8 74 20 69 6e 20 74 68 65 20 66 69 6c 65 73 79 73  t in the filesys
121f9 74 65 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f 72  tem but no error
121fa 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20   is returned.** 
121fb 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a  to the caller..*
121fc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
121fd 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
121fe 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50  rClose(Pager *pP
121ff 61 67 65 72 29 7b 0a 20 20 64 69 73 61 62 6c 65  ager){.  disable
12200 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
12201 72 6f 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65  rors();.  sqlite
12202 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
12203 6f 63 28 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  oc();.  pPager->
12204 65 72 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20 70  errCode = 0;.  p
12205 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
12206 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 70 61 67 65  Mode = 0;.  page
12207 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b  r_reset(pPager);
12208 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a  .  if( MEMDB ){.
12209 20 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b      pager_unlock
1220a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73  (pPager);.  }els
1220b 65 7b 0a 20 20 20 20 2f 2a 20 53 65 74 20 50 61  e{.    /* Set Pa
1220c 67 65 72 2e 6a 6f 75 72 6e 61 6c 48 64 72 20 74  ger.journalHdr t
1220d 6f 20 2d 31 20 66 6f 72 20 74 68 65 20 62 65 6e  o -1 for the ben
1220e 65 66 69 74 20 6f 66 20 74 68 65 20 70 61 67 65  efit of the page
1220f 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 0a 20 20  r_playback() .  
12210 20 20 2a 2a 20 63 61 6c 6c 20 77 68 69 63 68 20    ** call which 
12211 6d 61 79 20 62 65 20 6d 61 64 65 20 66 72 6f 6d  may be made from
12212 20 77 69 74 68 69 6e 20 70 61 67 65 72 55 6e 6c   within pagerUnl
12213 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 29  ockAndRollback()
12214 2e 20 49 66 20 69 74 0a 20 20 20 20 2a 2a 20 69  . If it.    ** i
12215 73 20 6e 6f 74 20 2d 31 2c 20 74 68 65 6e 20 74  s not -1, then t
12216 68 65 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74  he unsynced port
12217 69 6f 6e 20 6f 66 20 61 6e 20 6f 70 65 6e 20 6a  ion of an open j
12218 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 0a  ournal file may.
12219 20 20 20 20 2a 2a 20 62 65 20 70 6c 61 79 65 64      ** be played
1221a 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64   back into the d
1221b 61 74 61 62 61 73 65 2e 20 49 66 20 61 20 70 6f  atabase. If a po
1221c 77 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75  wer failure occu
1221d 72 73 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20  rs while.    ** 
1221e 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e  this is happenin
1221f 67 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  g, the database 
12220 6d 61 79 20 62 65 63 6f 6d 65 20 63 6f 72 72 75  may become corru
12221 70 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  pt..    */.    p
12222 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
12223 72 20 3d 20 2d 31 3b 0a 20 20 20 20 70 61 67 65  r = -1;.    page
12224 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61  rUnlockAndRollba
12225 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  ck(pPager);.  }.
12226 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
12227 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e  gnMalloc();.  en
12228 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
12229 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41  o_errors();.  PA
1222a 47 45 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45  GERTRACE(("CLOSE
1222b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
1222c 70 50 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54  pPager)));.  IOT
1222d 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c  RACE(("CLOSE %p\
1222e 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73  n", pPager)).  s
1222f 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
12230 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c  ager->fd);.  sql
12231 69 74 65 33 50 61 67 65 46 72 65 65 28 70 50 61  ite3PageFree(pPa
12232 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b  ger->pTmpSpace);
12233 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
12234 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 70 50  Close(pPager->pP
12235 43 61 63 68 65 29 3b 0a 0a 20 20 61 73 73 65 72  Cache);..  asser
12236 74 28 20 21 70 50 61 67 65 72 2d 3e 61 53 61 76  t( !pPager->aSav
12237 65 70 6f 69 6e 74 20 26 26 20 21 70 50 61 67 65  epoint && !pPage
12238 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b  r->pInJournal );
12239 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70  .  assert( !isOp
1223a 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
1223b 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  && !isOpen(pPage
1223c 72 2d 3e 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73  r->sjfd) );..  s
1223d 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67  qlite3_free(pPag
1223e 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  er);.  return SQ
1223f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20  LITE_OK;.}..#if 
12240 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
12241 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
12242 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52  TE_TEST)./*.** R
12243 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 6e  eturn the page n
12244 75 6d 62 65 72 20 66 6f 72 20 70 61 67 65 20 70  umber for page p
12245 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  Pg..*/.SQLITE_PR
12246 49 56 41 54 45 20 50 67 6e 6f 20 73 71 6c 69 74  IVATE Pgno sqlit
12247 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65  e3PagerPagenumbe
12248 72 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  r(DbPage *pPg){.
12249 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 67    return pPg->pg
1224a 6e 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  no;.}.#endif../*
1224b 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68  .** Increment th
1224c 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
1224d 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a  t for page pPg..
1224e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1224f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
12250 67 65 72 52 65 66 28 44 62 50 61 67 65 20 2a 70  gerRef(DbPage *p
12251 50 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63  Pg){.  sqlite3Pc
12252 61 63 68 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a  acheRef(pPg);.}.
12253 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20  ./*.** Sync the 
12254 6a 6f 75 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65  journal. In othe
12255 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75  r words, make su
12256 72 65 20 61 6c 6c 20 74 68 65 20 70 61 67 65 73  re all the pages
12257 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 62 65   that have.** be
12258 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  en written to th
12259 65 20 6a 6f 75 72 6e 61 6c 20 68 61 76 65 20 61  e journal have a
1225a 63 74 75 61 6c 6c 79 20 72 65 61 63 68 65 64 20  ctually reached 
1225b 74 68 65 20 73 75 72 66 61 63 65 20 6f 66 20 74  the surface of t
1225c 68 65 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63  he.** disk and c
1225d 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 69  an be restored i
1225e 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
1225f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
12260 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  lback..**.** If 
12261 74 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79  the Pager.needSy
12262 6e 63 20 66 6c 61 67 20 69 73 20 6e 6f 74 20 73  nc flag is not s
12263 65 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  et, then this fu
12264 6e 63 74 69 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e  nction is a.** n
12265 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c  o-op. Otherwise,
12266 20 74 68 65 20 61 63 74 69 6f 6e 73 20 72 65 71   the actions req
12267 75 69 72 65 64 20 64 65 70 65 6e 64 20 6f 6e 20  uired depend on 
12268 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65  the journal-mode
12269 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 65 76 69  .** and the devi
1226a 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69  ce characteristi
1226b 63 73 20 6f 66 20 74 68 65 20 74 68 65 20 66 69  cs of the the fi
1226c 6c 65 2d 73 79 73 74 65 6d 2c 20 61 73 20 66 6f  le-system, as fo
1226d 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  llows:.**.**   *
1226e 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
1226f 66 69 6c 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65  file is an in-me
12270 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  mory journal fil
12271 65 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65  e, no action nee
12272 64 0a 2a 2a 20 20 20 20 20 62 65 20 74 61 6b 65  d.**     be take
12273 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68  n..**.**   * Oth
12274 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 64  erwise, if the d
12275 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73  evice does not s
12276 75 70 70 6f 72 74 20 74 68 65 20 53 41 46 45 5f  upport the SAFE_
12277 41 50 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2c  APPEND property,
12278 0a 2a 2a 20 20 20 20 20 74 68 65 6e 20 74 68 65  .**     then the
12279 20 6e 52 65 63 20 66 69 65 6c 64 20 6f 66 20 74   nRec field of t
1227a 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
1227b 20 77 72 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c   written journal
1227c 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 69   header.**     i
1227d 73 20 75 70 64 61 74 65 64 20 74 6f 20 63 6f 6e  s updated to con
1227e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  tain the number 
1227f 6f 66 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72  of journal recor
12280 64 73 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20  ds that have.** 
12281 20 20 20 20 62 65 65 6e 20 77 72 69 74 74 65 6e      been written
12282 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 74 2e 20 49   following it. I
12283 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6f  f the pager is o
12284 70 65 72 61 74 69 6e 67 20 69 6e 20 66 75 6c 6c  perating in full
12285 2d 73 79 6e 63 0a 2a 2a 20 20 20 20 20 6d 6f 64  -sync.**     mod
12286 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72  e, then the jour
12287 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63  nal file is sync
12288 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66  ed before this f
12289 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2e  ield is updated.
1228a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68  .**.**   * If th
1228b 65 20 64 65 76 69 63 65 20 64 6f 65 73 20 6e 6f  e device does no
1228c 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 53 45  t support the SE
1228d 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74  QUENTIAL propert
1228e 79 2c 20 74 68 65 6e 20 0a 2a 2a 20 20 20 20 20  y, then .**     
1228f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
12290 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72  synced..**.** Or
12291 2c 20 69 6e 20 70 73 65 75 64 6f 2d 63 6f 64 65  , in pseudo-code
12292 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 4e 4f  :.**.**   if( NO
12293 54 20 3c 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  T <in-memory jou
12294 72 6e 61 6c 3e 20 29 7b 0a 2a 2a 20 20 20 20 20  rnal> ){.**     
12295 69 66 28 20 4e 4f 54 20 53 41 46 45 5f 41 50 50  if( NOT SAFE_APP
12296 45 4e 44 20 29 7b 0a 2a 2a 20 20 20 20 20 20 20  END ){.**       
12297 69 66 28 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d  if( <full-sync m
12298 6f 64 65 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f  ode> ) xSync(<jo
12299 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a  urnal file>);.**
1229a 20 20 20 20 20 20 20 3c 75 70 64 61 74 65 20 6e         <update n
1229b 52 65 63 20 66 69 65 6c 64 3e 0a 2a 2a 20 20 20  Rec field>.**   
1229c 20 20 7d 20 0a 2a 2a 20 20 20 20 20 69 66 28 20    } .**     if( 
1229d 4e 4f 54 20 53 45 51 55 45 4e 54 49 41 4c 20 29  NOT SEQUENTIAL )
1229e 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20   xSync(<journal 
1229f 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a  file>);.**   }.*
122a0 2a 0a 2a 2a 20 54 68 65 20 50 61 67 65 72 2e 6e  *.** The Pager.n
122a1 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 20  eedSync flag is 
122a2 6e 65 76 65 72 20 62 65 20 73 65 74 20 66 6f 72  never be set for
122a3 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
122a4 2c 20 6f 72 20 61 6e 79 0a 2a 2a 20 66 69 6c 65  , or any.** file
122a5 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 6e 6f   operating in no
122a6 2d 73 79 6e 63 20 6d 6f 64 65 20 28 50 61 67 65  -sync mode (Page
122a7 72 2e 6e 6f 53 79 6e 63 20 73 65 74 20 74 6f 20  r.noSync set to 
122a8 6e 6f 6e 2d 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a  non-zero)..**.**
122a9 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
122aa 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6c 65  this routine cle
122ab 61 72 73 20 74 68 65 20 50 47 48 44 52 5f 4e 45  ars the PGHDR_NE
122ac 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 6f 66 20  ED_SYNC flag of 
122ad 65 76 65 72 79 20 0a 2a 2a 20 70 61 67 65 20 63  every .** page c
122ae 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e  urrently held in
122af 20 6d 65 6d 6f 72 79 20 62 65 66 6f 72 65 20 72   memory before r
122b0 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f  eturning SQLITE_
122b1 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20  OK. If an IO.** 
122b2 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
122b3 65 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 49  ered, then the I
122b4 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
122b5 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
122b6 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  caller..*/.stati
122b7 63 20 69 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61  c int syncJourna
122b8 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  l(Pager *pPager)
122b9 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  {.  if( pPager->
122ba 6e 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20  needSync ){.    
122bb 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d  assert( !pPager-
122bc 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20  >tempFile );.   
122bd 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
122be 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f  rnalMode!=PAGER_
122bf 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
122c0 52 59 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  RY ){.      int 
122c1 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
122c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
122c3 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
122c4 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69  */.      const i
122c5 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33  nt iDc = sqlite3
122c6 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
122c7 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e  ristics(pPager->
122c8 66 64 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  fd);.      asser
122c9 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
122ca 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 20 20 20  ->jfd) );..     
122cb 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 51 4c   if( 0==(iDc&SQL
122cc 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
122cd 50 50 45 4e 44 29 20 29 7b 0a 20 20 20 20 20 20  PPEND) ){.      
122ce 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 69 4e    /* Variable iN
122cf 52 65 63 4f 66 66 73 65 74 20 69 73 20 73 65 74  RecOffset is set
122d0 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69   to the offset i
122d1 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
122d2 6c 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66  le.        ** of
122d3 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20   the nRec field 
122d4 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
122d5 6e 74 6c 79 20 77 72 69 74 74 65 6e 20 6a 6f 75  ntly written jou
122d6 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20 20  rnal header..   
122d7 20 20 20 20 20 2a 2a 20 54 68 69 73 20 66 69 65       ** This fie
122d8 6c 64 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74  ld will be updat
122d9 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ed following the
122da 20 78 53 79 6e 63 28 29 20 6f 70 65 72 61 74 69   xSync() operati
122db 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e  on.        ** on
122dc 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
122dd 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36  e. */.        i6
122de 34 20 69 4e 52 65 63 4f 66 66 73 65 74 20 3d 20  4 iNRecOffset = 
122df 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48  pPager->journalH
122e0 64 72 20 2b 20 73 69 7a 65 6f 66 28 61 4a 6f 75  dr + sizeof(aJou
122e1 72 6e 61 6c 4d 61 67 69 63 29 3b 0a 0a 20 20 20  rnalMagic);..   
122e2 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f       /* This blo
122e3 63 6b 20 64 65 61 6c 73 20 77 69 74 68 20 61 6e  ck deals with an
122e4 20 6f 62 73 63 75 72 65 20 70 72 6f 62 6c 65 6d   obscure problem
122e5 2e 20 49 66 20 74 68 65 20 6c 61 73 74 20 63 6f  . If the last co
122e6 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20 20 20  nnection.       
122e7 20 2a 2a 20 74 68 61 74 20 77 72 6f 74 65 20 74   ** that wrote t
122e8 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
122e9 77 61 73 20 6f 70 65 72 61 74 69 6e 67 20 69 6e  was operating in
122ea 20 70 65 72 73 69 73 74 65 6e 74 2d 6a 6f 75 72   persistent-jour
122eb 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d  nal.        ** m
122ec 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f  ode, then the jo
122ed 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 61  urnal file may a
122ee 74 20 74 68 69 73 20 70 6f 69 6e 74 20 61 63 74  t this point act
122ef 75 61 6c 6c 79 20 62 65 20 6c 61 72 67 65 72 0a  ually be larger.
122f0 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20          ** than 
122f1 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66  Pager.journalOff
122f2 20 62 79 74 65 73 2e 20 49 66 20 74 68 65 20 6e   bytes. If the n
122f3 65 78 74 20 74 68 69 6e 67 20 69 6e 20 74 68 65  ext thing in the
122f4 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20   journal.       
122f5 20 2a 2a 20 66 69 6c 65 20 68 61 70 70 65 6e 73   ** file happens
122f6 20 74 6f 20 62 65 20 61 20 6a 6f 75 72 6e 61 6c   to be a journal
122f7 2d 68 65 61 64 65 72 20 28 77 72 69 74 74 65 6e  -header (written
122f8 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a   as part of the.
122f9 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69          ** previ
122fa 6f 75 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ous connections 
122fb 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 61 6e  transaction), an
122fc 64 20 61 20 63 72 61 73 68 20 6f 72 20 70 6f 77  d a crash or pow
122fd 65 72 2d 66 61 69 6c 75 72 65 20 0a 20 20 20 20  er-failure .    
122fe 20 20 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 66      ** occurs af
122ff 74 65 72 20 6e 52 65 63 20 69 73 20 75 70 64 61  ter nRec is upda
12300 74 65 64 20 62 75 74 20 62 65 66 6f 72 65 20 74  ted but before t
12301 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  his connection w
12302 72 69 74 65 73 20 0a 20 20 20 20 20 20 20 20 2a  rites .        *
12303 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20  * anything else 
12304 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
12305 69 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 74 73 2f  ile (or commits/
12306 72 6f 6c 6c 73 20 62 61 63 6b 20 69 74 73 20 0a  rolls back its .
12307 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73          ** trans
12308 61 63 74 69 6f 6e 29 2c 20 74 68 65 6e 20 53 51  action), then SQ
12309 4c 69 74 65 20 6d 61 79 20 62 65 63 6f 6d 65 20  Lite may become 
1230a 63 6f 6e 66 75 73 65 64 20 77 68 65 6e 20 64 6f  confused when do
1230b 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20  ing the .       
1230c 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20   ** hot-journal 
1230d 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c 6f 77 69  rollback followi
1230e 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 49 74 20  ng recovery. It 
1230f 6d 61 79 20 72 6f 6c 6c 20 62 61 63 6b 20 61 6c  may roll back al
12310 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20  l.        ** of 
12311 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  this connections
12312 20 64 61 74 61 2c 20 74 68 65 6e 20 70 72 6f 63   data, then proc
12313 65 65 64 20 74 6f 20 72 6f 6c 6c 69 6e 67 20 62  eed to rolling b
12314 61 63 6b 20 74 68 65 20 6f 6c 64 2c 0a 20 20 20  ack the old,.   
12315 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66 2d 64       ** out-of-d
12316 61 74 65 20 64 61 74 61 20 74 68 61 74 20 66 6f  ate data that fo
12317 6c 6c 6f 77 73 20 69 74 2e 20 44 61 74 61 62 61  llows it. Databa
12318 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20  se corruption.. 
12319 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
1231a 20 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f    ** To work aro
1231b 75 6e 64 20 74 68 69 73 2c 20 69 66 20 74 68 65  und this, if the
1231c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f   journal file do
1231d 65 73 20 61 70 70 65 61 72 20 74 6f 20 63 6f 6e  es appear to con
1231e 74 61 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  tain.        ** 
1231f 61 20 76 61 6c 69 64 20 68 65 61 64 65 72 20 66  a valid header f
12320 6f 6c 6c 6f 77 69 6e 67 20 50 61 67 65 72 2e 6a  ollowing Pager.j
12321 6f 75 72 6e 61 6c 4f 66 66 2c 20 74 68 65 6e 20  ournalOff, then 
12322 77 72 69 74 65 20 61 20 30 78 30 30 0a 20 20 20  write a 0x00.   
12323 20 20 20 20 20 2a 2a 20 62 79 74 65 20 74 6f 20       ** byte to 
12324 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 20  the start of it 
12325 74 6f 20 70 72 65 76 65 6e 74 20 69 74 20 66 72  to prevent it fr
12326 6f 6d 20 62 65 69 6e 67 20 72 65 63 6f 67 6e 69  om being recogni
12327 7a 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  zed..        **.
12328 20 20 20 20 20 20 20 20 2a 2a 20 56 61 72 69 61          ** Varia
12329 62 6c 65 20 69 4e 65 78 74 48 64 72 4f 66 66 73  ble iNextHdrOffs
1232a 65 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  et is set to the
1232b 20 6f 66 66 73 65 74 20 61 74 20 77 68 69 63 68   offset at which
1232c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a   this.        **
1232d 20 70 72 6f 62 6c 65 6d 61 74 69 63 20 68 65 61   problematic hea
1232e 64 65 72 20 77 69 6c 6c 20 6f 63 63 75 72 2c 20  der will occur, 
1232f 69 66 20 69 74 20 65 78 69 73 74 73 2e 20 61 4d  if it exists. aM
12330 61 67 69 63 20 69 73 20 75 73 65 64 20 0a 20 20  agic is used .  
12331 20 20 20 20 20 20 2a 2a 20 61 73 20 61 20 74 65        ** as a te
12332 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72 20 74  mporary buffer t
12333 6f 20 69 6e 73 70 65 63 74 20 74 68 65 20 66 69  o inspect the fi
12334 72 73 74 20 63 6f 75 70 6c 65 20 6f 66 20 62 79  rst couple of by
12335 74 65 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a  tes of.        *
12336 2a 20 74 68 65 20 70 6f 74 65 6e 74 69 61 6c 20  * the potential 
12337 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a  journal header..
12338 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
12339 20 20 20 69 36 34 20 69 4e 65 78 74 48 64 72 4f     i64 iNextHdrO
1233a 66 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48  ffset = journalH
1233b 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29  drOffset(pPager)
1233c 3b 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d 61  ;.        u8 aMa
1233d 67 69 63 5b 38 5d 3b 0a 20 20 20 20 20 20 20 20  gic[8];.        
1233e 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
1233f 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ad(pPager->jfd, 
12340 61 4d 61 67 69 63 2c 20 38 2c 20 69 4e 65 78 74  aMagic, 8, iNext
12341 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  HdrOffset);.    
12342 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
12343 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 6d 65 6d 63  TE_OK && 0==memc
12344 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72  mp(aMagic, aJour
12345 6e 61 6c 4d 61 67 69 63 2c 20 38 29 20 29 7b 0a  nalMagic, 8) ){.
12346 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63            static
12347 20 63 6f 6e 73 74 20 75 38 20 7a 65 72 6f 62 79   const u8 zeroby
12348 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  te = 0;.        
12349 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1234a 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  Write(pPager->jf
1234b 64 2c 20 26 7a 65 72 6f 62 79 74 65 2c 20 31 2c  d, &zerobyte, 1,
1234c 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29   iNextHdrOffset)
1234d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1234e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1234f 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c  TE_OK && rc!=SQL
12350 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
12351 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20  READ ){.        
12352 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
12353 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
12354 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65  /* Write the nRe
12355 63 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65  c value into the
12356 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 65   journal file he
12357 61 64 65 72 2e 20 49 66 20 69 6e 0a 20 20 20 20  ader. If in.    
12358 20 20 20 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63      ** full-sync
12359 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79  hronous mode, sy
1235a 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nc the journal f
1235b 69 72 73 74 2e 20 54 68 69 73 20 65 6e 73 75 72  irst. This ensur
1235c 65 73 20 74 68 61 74 0a 20 20 20 20 20 20 20 20  es that.        
1235d 2a 2a 20 61 6c 6c 20 64 61 74 61 20 68 61 73 20  ** all data has 
1235e 72 65 61 6c 6c 79 20 68 69 74 20 74 68 65 20 64  really hit the d
1235f 69 73 6b 20 62 65 66 6f 72 65 20 6e 52 65 63 20  isk before nRec 
12360 69 73 20 75 70 64 61 74 65 64 20 74 6f 20 6d 61  is updated to ma
12361 72 6b 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74  rk.        ** it
12362 20 61 73 20 61 20 63 61 6e 64 69 64 61 74 65 20   as a candidate 
12363 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20  for rollback..  
12364 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
12365 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20   ** This is not 
12366 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65 20  required if the 
12367 70 65 72 73 69 73 74 65 6e 74 20 6d 65 64 69 61  persistent media
12368 20 73 75 70 70 6f 72 74 73 20 74 68 65 0a 20 20   supports the.  
12369 20 20 20 20 20 20 2a 2a 20 53 41 46 45 5f 41 50        ** SAFE_AP
1236a 50 45 4e 44 20 70 72 6f 70 65 72 74 79 2e 20 42  PEND property. B
1236b 65 63 61 75 73 65 20 69 6e 20 74 68 69 73 20 63  ecause in this c
1236c 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 70 6f  ase it is not po
1236d 73 73 69 62 6c 65 20 0a 20 20 20 20 20 20 20 20  ssible .        
1236e 2a 2a 20 66 6f 72 20 67 61 72 62 61 67 65 20 64  ** for garbage d
1236f 61 74 61 20 74 6f 20 62 65 20 61 70 70 65 6e 64  ata to be append
12370 65 64 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 20  ed to the file, 
12371 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a 20  the nRec field. 
12372 20 20 20 20 20 20 20 2a 2a 20 69 73 20 70 6f 70         ** is pop
12373 75 6c 61 74 65 64 20 77 69 74 68 20 30 78 46 46  ulated with 0xFF
12374 46 46 46 46 46 46 20 77 68 65 6e 20 74 68 65 20  FFFFFF when the 
12375 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69  journal header i
12376 73 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20  s written.      
12377 20 20 2a 2a 20 61 6e 64 20 6e 65 76 65 72 20 6e    ** and never n
12378 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 61 74  eeds to be updat
12379 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ed..        */. 
1237a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
1237b 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 30  r->fullSync && 0
1237c 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f  ==(iDc&SQLITE_IO
1237d 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20  CAP_SEQUENTIAL) 
1237e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 41 47  ){.          PAG
1237f 45 52 54 52 41 43 45 28 28 22 53 59 4e 43 20 6a  ERTRACE(("SYNC j
12380 6f 75 72 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c  ournal of %d\n",
12381 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
12382 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4f  ));.          IO
12383 54 52 41 43 45 28 28 22 4a 53 59 4e 43 20 25 70  TRACE(("JSYNC %p
12384 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 20 20  \n", pPager)).  
12385 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
12386 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
12387 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e  r->jfd, pPager->
12388 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20  sync_flags);.   
12389 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
1238a 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
1238b 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
1238c 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28          IOTRACE(
1238d 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20 25  ("JHDR %p %lld %
1238e 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 69 4e  d\n", pPager, iN
1238f 52 65 63 4f 66 66 73 65 74 2c 20 34 29 29 3b 0a  RecOffset, 4));.
12390 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69          rc = wri
12391 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  te32bits(pPager-
12392 3e 6a 66 64 2c 20 69 4e 52 65 63 4f 66 66 73 65  >jfd, iNRecOffse
12393 74 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29  t, pPager->nRec)
12394 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
12395 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
12396 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
12397 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69  .      if( 0==(i
12398 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  Dc&SQLITE_IOCAP_
12399 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20  SEQUENTIAL) ){. 
1239a 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
1239b 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c  E(("SYNC journal
1239c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52   of %d\n", PAGER
1239d 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
1239e 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
1239f 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61  JSYNC %p\n", pPa
123a0 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63  ger)).        rc
123a1 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
123a2 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
123a3 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73  ager->sync_flags
123a4 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50  | .          (pP
123a5 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73  ager->sync_flags
123a6 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  ==SQLITE_SYNC_FU
123a7 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44  LL?SQLITE_SYNC_D
123a8 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20  ATAONLY:0).     
123a9 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66     );.        if
123aa 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
123ab 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
123ac 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
123ad 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
123ae 69 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63  ile was just suc
123af 63 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64  cessfully synced
123b0 2e 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64  . Set Pager.need
123b1 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20  Sync .    ** to 
123b2 7a 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74  zero and clear t
123b3 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
123b4 4e 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70  NC flag on all p
123b5 61 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  agess..    */.  
123b6 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79    pPager->needSy
123b7 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  nc = 0;.    pPag
123b8 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74  er->journalStart
123b9 65 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69  ed = 1;.    sqli
123ba 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79  te3PcacheClearSy
123bb 6e 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e  ncFlags(pPager->
123bc 70 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20  pPCache);.  }.. 
123bd 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
123be 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K;.}../*.** The 
123bf 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
123c0 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65  first in a linke
123c1 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20  d list of dirty 
123c2 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a  pages connected.
123c3 2a 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e  ** by the PgHdr.
123c4 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20  pDirty pointer. 
123c5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72  This function wr
123c6 69 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66  ites each one of
123c7 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72   the.** in-memor
123c8 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c  y pages in the l
123c9 69 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62  ist to the datab
123ca 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72  ase file. The ar
123cb 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65  gument may.** be
123cc 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74   NULL, represent
123cd 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73  ing an empty lis
123ce 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  t. In this case 
123cf 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
123d0 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a  .** a no-op..**.
123d1 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73  ** The pager mus
123d2 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20  t hold at least 
123d3 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
123d4 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
123d5 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e  on.** is called.
123d6 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20   Before writing 
123d7 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20  anything to the 
123d8 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
123d9 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75  his lock.** is u
123da 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58  pgraded to an EX
123db 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66  CLUSIVE lock. If
123dc 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74   the lock cannot
123dd 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a   be obtained,.**
123de 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
123df 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
123e0 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20  data is written 
123e1 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
123e2 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20  file..** .** If 
123e3 74 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74  the pager is a t
123e4 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61  emp-file pager a
123e5 6e 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69  nd the actual fi
123e6 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a  le-system file.*
123e7 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65  * is not yet ope
123e8 6e 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64  n, it is created
123e9 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f   and opened befo
123ea 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a  re any data is .
123eb 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a  ** written out..
123ec 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c  **.** Once the l
123ed 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67  ock has been upg
123ee 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65  raded and, if ne
123ef 63 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c  cessary, the fil
123f0 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65  e opened,.** the
123f1 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74   pages are writt
123f2 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61  en out to the da
123f3 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c  tabase file in l
123f4 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69  ist order. Writi
123f5 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20  ng.** a page is 
123f6 73 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65  skipped if it me
123f7 65 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68  ets either of th
123f8 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74  e following crit
123f9 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  eria:.**.**   * 
123fa 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  The page number 
123fb 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
123fc 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72  Pager.dbSize, or
123fd 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44  .**   * The PGHD
123fe 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61  R_DONT_WRITE fla
123ff 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20  g is set on the 
12400 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77  page..**.** If w
12401 72 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67  riting out a pag
12402 65 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74  e causes the dat
12403 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72  abase file to gr
12404 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65  ow, Pager.dbFile
12405 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74  Size.** is updat
12406 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20  ed accordingly. 
12407 49 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69  If page 1 is wri
12408 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74  tten out, then t
12409 68 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a  he value cached.
1240a 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69  ** in Pager.dbFi
1240b 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61  leVers[] is upda
1240c 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65  ted to match the
1240d 20 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65   new value store
1240e 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61  d in.** the data
1240f 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
12410 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69   If everything i
12411 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51  s successful, SQ
12412 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
12413 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72  ned. If an IO er
12414 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20  ror .** occurs, 
12415 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
12416 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72   is returned. Or
12417 2c 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49  , if the EXCLUSI
12418 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a  VE lock cannot.*
12419 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53  * be obtained, S
1241a 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65  QLITE_BUSY is re
1241b 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
1241c 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74  c int pager_writ
1241d 65 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72  e_pagelist(PgHdr
1241e 20 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65   *pList){.  Page
1241f 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20  r *pPager;      
12420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12421 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74   /* Pager object
12422 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
12423 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12424 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
12425 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  turn code */..  
12426 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
12427 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
12428 0a 20 20 70 50 61 67 65 72 20 3d 20 70 4c 69 73  .  pPager = pLis
12429 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 20 2f 2a  t->pPager;..  /*
1242a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
1242b 68 65 72 65 20 6d 61 79 20 62 65 20 65 69 74 68  here may be eith
1242c 65 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  er a RESERVED or
1242d 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
1242e 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  on the.  ** data
1242f 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68  base file. If th
12430 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
12431 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
12432 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  , the following.
12433 20 20 2a 2a 20 63 61 6c 6c 20 69 73 20 61 20 6e    ** call is a n
12434 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  o-op..  **.  ** 
12435 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  Moving the lock 
12436 66 72 6f 6d 20 52 45 53 45 52 56 45 44 20 74 6f  from RESERVED to
12437 20 45 58 43 4c 55 53 49 56 45 20 61 63 74 75 61   EXCLUSIVE actua
12438 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 67 6f 69  lly involves goi
12439 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 20  ng.  ** through 
1243a 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  an intermediate 
1243b 73 74 61 74 65 20 50 45 4e 44 49 4e 47 2e 20 20  state PENDING.  
1243c 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   A PENDING lock 
1243d 70 72 65 76 65 6e 74 73 20 6e 65 77 0a 20 20 2a  prevents new.  *
1243e 2a 20 72 65 61 64 65 72 73 20 66 72 6f 6d 20 61  * readers from a
1243f 74 74 61 63 68 69 6e 67 20 74 6f 20 74 68 65 20  ttaching to the 
12440 64 61 74 61 62 61 73 65 20 62 75 74 20 69 73 20  database but is 
12441 75 6e 73 75 66 66 69 63 69 65 6e 74 20 66 6f 72  unsufficient for
12442 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 72 69 74   us to.  ** writ
12443 65 2e 20 20 54 68 65 20 69 64 65 61 20 6f 66 20  e.  The idea of 
12444 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69  a PENDING lock i
12445 73 20 74 6f 20 70 72 65 76 65 6e 74 20 6e 65 77  s to prevent new
12446 20 72 65 61 64 65 72 73 20 66 72 6f 6d 0a 20 20   readers from.  
12447 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 77 68 69  ** coming in whi
12448 6c 65 20 77 65 20 77 61 69 74 20 66 6f 72 20 65  le we wait for e
12449 78 69 73 74 69 6e 67 20 72 65 61 64 65 72 73 20  xisting readers 
1244a 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a 2a 0a 20  to clear..  **. 
1244b 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 20 70 61   ** While the pa
1244c 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 52 45  ger is in the RE
1244d 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 68  SERVED state, th
1244e 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62  e original datab
1244f 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73  ase file.  ** is
12450 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 77   unchanged and w
12451 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 6b 20 77  e can rollback w
12452 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
12453 20 70 6c 61 79 62 61 63 6b 20 74 68 65 0a 20 20   playback the.  
12454 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e 74 6f 20  ** journal into 
12455 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74  the original dat
12456 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4f 6e 63  abase file.  Onc
12457 65 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20  e we transition 
12458 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 53 49 56  to.  ** EXCLUSIV
12459 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20  E, it means the 
1245a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
1245b 73 20 62 65 65 6e 20 63 68 61 6e 67 65 64 20 61  s been changed a
1245c 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 63 6b 0a  nd any rollback.
1245d 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 75 69 72    ** will requir
1245e 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79  e a journal play
1245f 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  back..  */.  ass
12460 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
12461 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te>=PAGER_RESERV
12462 45 44 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67  ED );.  rc = pag
12463 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28  er_wait_on_lock(
12464 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56  pPager, EXCLUSIV
12465 45 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f 2a 20 49  E_LOCK);..  /* I
12466 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 61 20  f the file is a 
12467 74 65 6d 70 2d 66 69 6c 65 20 68 61 73 20 6e 6f  temp-file has no
12468 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65  t yet been opene
12469 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20  d, open it now. 
1246a 49 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70  It.  ** is not p
1246b 6f 73 73 69 62 6c 65 20 66 6f 72 20 72 63 20 74  ossible for rc t
1246c 6f 20 62 65 20 6f 74 68 65 72 20 74 68 61 6e 20  o be other than 
1246d 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 69  SQLITE_OK if thi
1246e 73 20 62 72 61 6e 63 68 0a 20 20 2a 2a 20 69 73  s branch.  ** is
1246f 20 74 61 6b 65 6e 2c 20 61 73 20 70 61 67 65 72   taken, as pager
12470 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 29 20  _wait_on_lock() 
12471 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 74  is a no-op for t
12472 65 6d 70 2d 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a  emp-files..  */.
12473 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50    if( !isOpen(pP
12474 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20  ager->fd) ){.   
12475 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
12476 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 72 63 3d  >tempFile && rc=
12477 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
12478 20 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e    rc = pagerOpen
12479 74 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61  temp(pPager, pPa
1247a 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d  ger->fd, pPager-
1247b 3e 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 7d 0a  >vfsFlags);.  }.
1247c 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
1247d 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4c 69 73 74  LITE_OK && pList
1247e 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e   ){.    Pgno pgn
1247f 6f 20 3d 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 3b  o = pList->pgno;
12480 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  ..    /* If ther
12481 65 20 61 72 65 20 64 69 72 74 79 20 70 61 67 65  e are dirty page
12482 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  s in the page ca
12483 63 68 65 20 77 69 74 68 20 70 61 67 65 20 6e 75  che with page nu
12484 6d 62 65 72 73 20 67 72 65 61 74 65 72 0a 20 20  mbers greater.  
12485 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e    ** than Pager.
12486 64 62 53 69 7a 65 2c 20 74 68 69 73 20 6d 65 61  dbSize, this mea
12487 6e 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 54  ns sqlite3PagerT
12488 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 20 77  runcateImage() w
12489 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a 20 20 20  as called to.   
1248a 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 66 69 6c   ** make the fil
1248b 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 65 73 75  e smaller (presu
1248c 6d 61 62 6c 79 20 62 79 20 61 75 74 6f 2d 76 61  mably by auto-va
1248d 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 6f 20 6e  cuum code). Do n
1248e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 2a 2a 20  ot write.    ** 
1248f 61 6e 79 20 73 75 63 68 20 70 61 67 65 73 20 74  any such pages t
12490 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20  o the file..    
12491 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20  **.    ** Also, 
12492 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 6f 75 74  do not write out
12493 20 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 68   any page that h
12494 61 73 20 74 68 65 20 50 47 48 44 52 5f 44 4f 4e  as the PGHDR_DON
12495 54 5f 57 52 49 54 45 20 66 6c 61 67 0a 20 20 20  T_WRITE flag.   
12496 20 2a 2a 20 73 65 74 20 28 73 65 74 20 62 79 20   ** set (set by 
12497 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
12498 57 72 69 74 65 28 29 29 2e 0a 20 20 20 20 2a 2f  Write())..    */
12499 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70  .    if( pgno<=p
1249a 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26  Pager->dbSize &&
1249b 20 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67   0==(pList->flag
1249c 73 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49  s&PGHDR_DONT_WRI
1249d 54 45 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34  TE) ){.      i64
1249e 20 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d   offset = (pgno-
1249f 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e  1)*(i64)pPager->
124a0 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20  pageSize;       
124a1 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 77    /* Offset to w
124a2 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 63 68  rite */.      ch
124a3 61 72 20 2a 70 44 61 74 61 20 3d 20 43 4f 44 45  ar *pData = CODE
124a4 43 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74  C2(pPager, pList
124a5 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36  ->pData, pgno, 6
124a6 29 3b 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72  ); /* Data to wr
124a7 69 74 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a  ite */..      /*
124a8 20 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 70   Write out the p
124a9 61 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20  age data. */.   
124aa 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
124ab 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66  sWrite(pPager->f
124ac 64 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 72  d, pData, pPager
124ad 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73  ->pageSize, offs
124ae 65 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49  et);..      /* I
124af 66 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 73  f page 1 was jus
124b0 74 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74  t written, updat
124b1 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65  e Pager.dbFileVe
124b2 72 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 20  rs to match.    
124b3 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6e    ** the value n
124b4 6f 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ow stored in the
124b5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
124b6 49 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20  If writing this 
124b7 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 63  .      ** page c
124b8 61 75 73 65 64 20 74 68 65 20 64 61 74 61 62 61  aused the databa
124b9 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c  se file to grow,
124ba 20 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 69   update dbFileSi
124bb 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ze. .      */.  
124bc 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20      if( pgno==1 
124bd 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
124be 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  y(&pPager->dbFil
124bf 65 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 34  eVers, &pData[24
124c0 5d 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72  ], sizeof(pPager
124c1 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a  ->dbFileVers));.
124c2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
124c3 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64  ( pgno>pPager->d
124c4 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  bFileSize ){.   
124c5 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46       pPager->dbF
124c6 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a  ileSize = pgno;.
124c7 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
124c8 2a 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 63  * Update any bac
124c9 6b 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 79  kup objects copy
124ca 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ing the contents
124cb 20 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e 20   of this pager. 
124cc 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
124cd 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 61  BackupUpdate(pPa
124ce 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 67  ger->pBackup, pg
124cf 6e 6f 2c 20 28 75 38 20 2a 29 70 44 61 74 61 29  no, (u8 *)pData)
124d0 3b 0a 0a 20 20 20 20 20 20 50 41 47 45 52 54 52  ;..      PAGERTR
124d1 41 43 45 28 28 22 53 54 4f 52 45 20 25 64 20 70  ACE(("STORE %d p
124d2 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78  age %d hash(%08x
124d3 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  )\n",.          
124d4 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44           PAGERID
124d5 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20  (pPager), pgno, 
124d6 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
124d7 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49  List)));.      I
124d8 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 20 25  OTRACE(("PGOUT %
124d9 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
124da 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50   pgno));.      P
124db 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
124dc 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f  3_pager_writedb_
124dd 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41  count);.      PA
124de 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d  GER_INCR(pPager-
124df 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65  >nWrite);.    }e
124e0 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52  lse{.      PAGER
124e1 54 52 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20  TRACE(("NOSTORE 
124e2 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50  %d page %d\n", P
124e3 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
124e4 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69  pgno));.    }.#i
124e5 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
124e6 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73  K_PAGES.    pLis
124e7 74 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61  t->pageHash = pa
124e8 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69  ger_pagehash(pLi
124e9 73 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  st);.#endif.    
124ea 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70  pList = pList->p
124eb 44 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65  Dirty;.  }..  re
124ec 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
124ed 2a 20 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72  * Append a recor
124ee 64 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  d of the current
124ef 20 73 74 61 74 65 20 6f 66 20 70 61 67 65 20 70   state of page p
124f0 50 67 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f  Pg to the sub-jo
124f1 75 72 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73  urnal. .** It is
124f2 20 74 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73   the callers res
124f3 70 6f 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75  ponsibility to u
124f4 73 65 20 73 75 62 6a 52 65 71 75 69 72 65 73 50  se subjRequiresP
124f5 61 67 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a  age() to check .
124f6 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 72 65  ** that it is re
124f7 61 6c 6c 79 20 72 65 71 75 69 72 65 64 20 62 65  ally required be
124f8 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69  fore calling thi
124f9 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  s function..**.*
124fa 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
124fb 20 73 65 74 20 74 68 65 20 62 69 74 20 63 6f 72   set the bit cor
124fc 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50  responding to pP
124fd 67 2d 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62  g->pgno in the b
124fe 69 74 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c  itvecs.** for al
124ff 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  l open savepoint
12500 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  s before returni
12501 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ng..**.** This f
12502 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
12503 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65  SQLITE_OK if eve
12504 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65  rything is succe
12505 73 73 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20  ssful, an IO.** 
12506 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68  error code if th
12507 65 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69  e attempt to wri
12508 74 65 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f  te to the sub-jo
12509 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20  urnal fails, or 
1250a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .** SQLITE_NOMEM
1250b 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   if a malloc fai
1250c 6c 73 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67  ls while setting
1250d 20 61 20 62 69 74 20 69 6e 20 61 20 73 61 76 65   a bit in a save
1250e 70 6f 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e  point.** bitvec.
1250f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
12510 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67  ubjournalPage(Pg
12511 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74  Hdr *pPg){.  int
12512 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
12513 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
12514 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
12515 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
12516 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20  ger->sjfd) ){.  
12517 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20    void *pData = 
12518 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20  pPg->pData;.    
12519 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61  i64 offset = pPa
1251a 67 65 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b  ger->nSubRec*(4+
1251b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
1251c 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61  );.    char *pDa
1251d 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 50 61  ta2 = CODEC2(pPa
1251e 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d  ger, pData, pPg-
1251f 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 0a 20 20  >pgno, 7);.  .  
12520 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53    PAGERTRACE(("S
12521 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70  TMT-JOURNAL %d p
12522 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  age %d\n", PAGER
12523 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
12524 3e 70 67 6e 6f 29 29 3b 0a 20 20 0a 20 20 20 20  >pgno));.  .    
12525 61 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f  assert( pageInJo
12526 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50  urnal(pPg) || pP
12527 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e  g->pgno>pPager->
12528 64 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20  dbOrigSize );.  
12529 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69    rc = write32bi
1252a 74 73 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c  ts(pPager->sjfd,
1252b 20 6f 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67   offset, pPg->pg
1252c 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  no);.    if( rc=
1252d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1252e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1252f 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
12530 73 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50  sjfd, pData2, pP
12531 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
12532 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 7d  offset+4);.    }
12533 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
12534 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12535 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b  pPager->nSubRec+
12536 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  +;.    assert( p
12537 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e  Pager->nSavepoin
12538 74 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  t>0 );.    rc = 
12539 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69  addToSavepointBi
1253a 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50  tvecs(pPager, pP
1253b 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 74 65  g->pgno);.    te
1253c 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49  stcase( rc!=SQLI
1253d 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 72  TE_OK );.  }.  r
1253e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
1253f 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
12540 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  n is called by t
12541 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20  he pcache layer 
12542 77 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 63  when it has reac
12543 68 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74  hed some.** soft
12544 20 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54   memory limit. T
12545 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
12546 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
12547 6f 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74  o a Pager object
12548 0a 2a 2a 20 28 63 61 73 74 20 61 73 20 61 20 76  .** (cast as a v
12549 6f 69 64 2a 29 2e 20 54 68 65 20 70 61 67 65 72  oid*). The pager
1254a 20 69 73 20 61 6c 77 61 79 73 20 27 70 75 72 67   is always 'purg
1254b 65 61 62 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69  eable' (not an i
1254c 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61  n-memory.** data
1254d 62 61 73 65 29 2e 20 54 68 65 20 73 65 63 6f 6e  base). The secon
1254e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  d argument is a 
1254f 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 70  reference to a p
12550 61 67 65 20 74 68 61 74 20 69 73 20 0a 2a 2a 20  age that is .** 
12551 63 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79 20  currently dirty 
12552 62 75 74 20 68 61 73 20 6e 6f 20 6f 75 74 73 74  but has no outst
12553 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65  anding reference
12554 73 2e 20 54 68 65 20 70 61 67 65 0a 2a 2a 20 69  s. The page.** i
12555 73 20 61 6c 77 61 79 73 20 61 73 73 6f 63 69 61  s always associa
12556 74 65 64 20 77 69 74 68 20 74 68 65 20 50 61 67  ted with the Pag
12557 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64  er object passed
12558 20 61 73 20 74 68 65 20 66 69 72 73 74 20 0a 2a   as the first .*
12559 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  * argument..**.*
1255a 2a 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69  * The job of thi
1255b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f  s function is to
1255c 20 6d 61 6b 65 20 70 50 67 20 63 6c 65 61 6e 20   make pPg clean 
1255d 62 79 20 77 72 69 74 69 6e 67 20 69 74 73 20 63  by writing its c
1255e 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74  ontents.** out t
1255f 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
12560 69 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c 65  ile, if possible
12561 2e 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c  . This may invol
12562 76 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a  ve syncing the.*
12563 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  * journal file. 
12564 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
12565 73 66 75 6c 2c 20 73 71 6c 69 74 65 33 50 63 61  sful, sqlite3Pca
12566 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69  cheMakeClean() i
12567 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20  s called on the 
12568 70 61 67 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49  page and.** SQLI
12569 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20  TE_OK returned. 
1256a 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f  If an IO error o
1256b 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69  ccurs while tryi
1256c 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a  ng to make the.*
1256d 2a 20 70 61 67 65 20 63 6c 65 61 6e 2c 20 74 68  * page clean, th
1256e 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  e IO error code 
1256f 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  is returned. If 
12570 74 68 65 20 70 61 67 65 20 63 61 6e 6e 6f 74 20  the page cannot 
12571 62 65 0a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e  be.** made clean
12572 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20   for some other 
12573 72 65 61 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65  reason, but no e
12574 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65  rror occurs, the
12575 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69  n SQLITE_OK.** i
12576 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  s returned by sq
12577 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
12578 6c 65 61 6e 28 29 20 69 73 20 6e 6f 74 20 63 61  lean() is not ca
12579 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lled..*/.static 
1257a 69 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 28  int pagerStress(
1257b 76 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20 2a  void *p, PgHdr *
1257c 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  pPg){.  Pager *p
1257d 50 61 67 65 72 20 3d 20 28 50 61 67 65 72 20 2a  Pager = (Pager *
1257e 29 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  )p;.  int rc = S
1257f 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73  QLITE_OK;..  ass
12580 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72  ert( pPg->pPager
12581 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 61 73  ==pPager );.  as
12582 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73  sert( pPg->flags
12583 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a  &PGHDR_DIRTY );.
12584 0a 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53  .  /* The doNotS
12585 79 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 20  ync flag is set 
12586 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 50 61  by the sqlite3Pa
12587 67 65 72 57 72 69 74 65 28 29 20 66 75 6e 63 74  gerWrite() funct
12588 69 6f 6e 20 77 68 69 6c 65 20 69 74 0a 20 20 2a  ion while it.  *
12589 2a 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67  * is journalling
1258a 20 61 20 73 65 74 20 6f 66 20 74 77 6f 20 6f 72   a set of two or
1258b 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 70   more database p
1258c 61 67 65 73 20 74 68 61 74 20 61 72 65 20 73 74  ages that are st
1258d 6f 72 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65  ored.  ** on the
1258e 20 73 61 6d 65 20 64 69 73 6b 20 73 65 63 74 6f   same disk secto
1258f 72 2e 20 53 79 6e 63 69 6e 67 20 74 68 65 20 6a  r. Syncing the j
12590 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c  ournal is not al
12591 6c 6f 77 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a  lowed while.  **
12592 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69   this is happeni
12593 6e 67 20 61 73 20 69 74 20 69 73 20 69 6d 70 6f  ng as it is impo
12594 72 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d  rtant that all m
12595 65 6d 62 65 72 73 20 6f 66 20 73 75 63 68 20 61  embers of such a
12596 0a 20 20 2a 2a 20 73 65 74 20 6f 66 20 70 61 67  .  ** set of pag
12597 65 73 20 61 72 65 20 73 79 6e 63 65 64 20 74 6f  es are synced to
12598 20 64 69 73 6b 20 74 6f 67 65 74 68 65 72 2e 20   disk together. 
12599 53 6f 2c 20 69 66 20 74 68 65 20 70 61 67 65 20  So, if the page 
1259a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20  this function.  
1259b 2a 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20  ** is trying to 
1259c 6d 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20  make clean will 
1259d 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61  require a journa
1259e 6c 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20 64  l sync and the d
1259f 6f 4e 6f 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c  oNotSync.  ** fl
125a0 61 67 20 69 73 20 73 65 74 2c 20 72 65 74 75 72  ag is set, retur
125a1 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20  n without doing 
125a2 61 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70 63  anything. The pc
125a3 61 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a  ache layer will.
125a4 20 20 2a 2a 20 6a 75 73 74 20 68 61 76 65 20 74    ** just have t
125a5 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 61  o go ahead and a
125a6 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61  llocate a new pa
125a7 67 65 20 62 75 66 66 65 72 20 69 6e 73 74 65 61  ge buffer instea
125a8 64 20 6f 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e  d of.  ** reusin
125a9 67 20 70 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  g pPg..  **.  **
125aa 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74   Similarly, if t
125ab 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72  he pager has alr
125ac 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65  eady entered the
125ad 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f   error state, do
125ae 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f   not.  ** try to
125af 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65   write the conte
125b0 6e 74 73 20 6f 66 20 70 50 67 20 74 6f 20 64 69  nts of pPg to di
125b1 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  sk..  */.  if( p
125b2 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 7c  Pager->errCode |
125b3 7c 20 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74  | (pPager->doNot
125b4 53 79 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61  Sync && pPg->fla
125b5 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
125b6 4e 43 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  NC) ){.    retur
125b7 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
125b8 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20  ..  /* Sync the 
125b9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20  journal file if 
125ba 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69  required. */.  i
125bb 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  f( pPg->flags&PG
125bc 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b  HDR_NEED_SYNC ){
125bd 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f  .    rc = syncJo
125be 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
125bf 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
125c0 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
125c1 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 20 20  fullSync && .   
125c2 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75     !(pPager->jou
125c3 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
125c4 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
125c5 52 59 29 20 26 26 0a 20 20 20 20 20 20 21 28 73  RY) &&.      !(s
125c6 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68  qlite3OsDeviceCh
125c7 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50  aracteristics(pP
125c8 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 54 45  ager->fd)&SQLITE
125c9 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
125ca 4e 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ND).    ){.     
125cb 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20   pPager->nRec = 
125cc 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72  0;.      rc = wr
125cd 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  iteJournalHdr(pP
125ce 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ager);.    }.  }
125cf 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
125d0 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 69  ge number of thi
125d1 73 20 70 61 67 65 20 69 73 20 6c 61 72 67 65 72  s page is larger
125d2 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e   than the curren
125d3 74 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74  t size of.  ** t
125d4 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67  he database imag
125d5 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74  e, it may need t
125d6 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20  o be written to 
125d7 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e  the sub-journal.
125d8 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 62 65  .  ** This is be
125d9 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74  cause the call t
125da 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61  o pager_write_pa
125db 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 20 77  gelist() below w
125dc 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74  ill not.  ** act
125dd 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61 74 61  ually write data
125de 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69 6e 20   to the file in 
125df 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a  this case..  **.
125e0 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68    ** Consider th
125e1 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75  e following sequ
125e2 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a  ence of events:.
125e3 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49    **.  **   BEGI
125e4 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75  N;.  **     <jou
125e5 72 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a  rnal page X>.  *
125e6 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 70 61  *     <modify pa
125e7 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53  ge X>.  **     S
125e8 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a  AVEPOINT sp;.  *
125e9 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e 6b 20  *       <shrink 
125ea 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
125eb 20 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20   Y pages>.  **  
125ec 20 20 20 20 20 70 61 67 65 72 53 74 72 65 73 73       pagerStress
125ed 28 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 20 20  (page X).  **   
125ee 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70    ROLLBACK TO sp
125ef 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28  ;.  **.  ** If (
125f0 58 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 6e 20  X>Y), then when 
125f1 70 61 67 65 72 53 74 72 65 73 73 20 69 73 20 63  pagerStress is c
125f2 61 6c 6c 65 64 20 70 61 67 65 20 58 20 77 69 6c  alled page X wil
125f3 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e  l not be written
125f4 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65  .  ** out to the
125f5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
125f6 62 75 74 20 77 69 6c 6c 20 62 65 20 64 72 6f 70  but will be drop
125f7 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63  ped from the cac
125f8 68 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66  he. Then,.  ** f
125f9 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 52 4f  ollowing the "RO
125fa 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 73 74  LLBACK TO sp" st
125fb 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67  atement, reading
125fc 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72 65 61   page X will rea
125fd 64 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d  d.  ** data from
125fe 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
125ff 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 62 65  le. This will be
12600 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70 61 67   the copy of pag
12601 65 20 58 20 61 73 20 69 74 0a 20 20 2a 2a 20 77  e X as it.  ** w
12602 61 73 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e  as when the tran
12603 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 64 2c  saction started,
12604 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73 20 77   not as it was w
12605 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 20 73  hen "SAVEPOINT s
12606 70 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 65 63  p".  ** was exec
12607 75 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uted..  **.  ** 
12608 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
12609 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 75 72  to write the cur
1260a 72 65 6e 74 20 64 61 74 61 20 66 6f 72 20 70 61  rent data for pa
1260b 67 65 20 58 20 69 6e 74 6f 20 74 68 65 20 0a 20  ge X into the . 
1260c 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20   ** sub-journal 
1260d 66 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 74 20  file now (if it 
1260e 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 74  is not already t
1260f 68 65 72 65 29 2c 20 73 6f 20 74 68 61 74 20 69  here), so that i
12610 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72  t will.  ** be r
12611 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 63  estored to its c
12612 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77 68 65  urrent value whe
12613 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20  n the "ROLLBACK 
12614 54 4f 20 73 70 22 20 69 73 20 0a 20 20 2a 2a 20  TO sp" is .  ** 
12615 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20  executed..  */. 
12616 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
12617 4f 4b 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e  OK && pPg->pgno>
12618 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26  pPager->dbSize &
12619 26 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61  & subjRequiresPa
1261a 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 72  ge(pPg) ){.    r
1261b 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61  c = subjournalPa
1261c 67 65 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20  ge(pPg);.  }..  
1261d 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e  /* Write the con
1261e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67  tents of the pag
1261f 65 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74  e out to the dat
12620 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20  abase file. */. 
12621 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
12622 4f 4b 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 70  OK ){.    pPg->p
12623 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 72  Dirty = 0;.    r
12624 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f  c = pager_write_
12625 70 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a 20  pagelist(pPg);. 
12626 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68   }..  /* Mark th
12627 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e  e page as clean.
12628 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
12629 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50  LITE_OK ){.    P
1262a 41 47 45 52 54 52 41 43 45 28 28 22 53 54 52 45  AGERTRACE(("STRE
1262b 53 53 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22  SS %d page %d\n"
1262c 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
1262d 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a  ), pPg->pgno));.
1262e 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
1262f 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b  eMakeClean(pPg);
12630 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
12631 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
12632 72 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  r, rc);.}.../*.*
12633 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69  * Allocate and i
12634 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 20  nitialize a new 
12635 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 6e 64  Pager object and
12636 20 70 75 74 20 61 20 70 6f 69 6e 74 65 72 20 74   put a pointer t
12637 6f 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 61  o it.** in *ppPa
12638 67 65 72 2e 20 54 68 65 20 70 61 67 65 72 20 73  ger. The pager s
12639 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79  hould eventually
1263a 20 62 65 20 66 72 65 65 64 20 62 79 20 70 61 73   be freed by pas
1263b 73 69 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 71  sing it.** to sq
1263c 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28  lite3PagerClose(
1263d 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 69  )..**.** The zFi
1263e 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20  lename argument 
1263f 69 73 20 74 68 65 20 70 61 74 68 20 74 6f 20 74  is the path to t
12640 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12641 20 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20   to open..** If 
12642 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c  zFilename is NUL
12643 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c  L then a randoml
12644 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72  y-named temporar
12645 79 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65  y file is create
12646 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73  d.** and used as
12647 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20   the file to be 
12648 63 61 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 72  cached. Temporar
12649 79 20 66 69 6c 65 73 20 61 72 65 20 62 65 20 64  y files are be d
1264a 65 6c 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61  eleted.** automa
1264b 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65  tically when the
1264c 79 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 66  y are closed. If
1264d 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a   zFilename is ":
1264e 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a 2a  memory:" then .*
1264f 2a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  * all informatio
12650 6e 20 69 73 20 68 65 6c 64 20 69 6e 20 63 61 63  n is held in cac
12651 68 65 2e 20 49 74 20 69 73 20 6e 65 76 65 72 20  he. It is never 
12652 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e  written to disk.
12653 20 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 65   .** This can be
12654 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
12655 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  nt an in-memory 
12656 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
12657 54 68 65 20 6e 45 78 74 72 61 20 70 61 72 61 6d  The nExtra param
12658 65 74 65 72 20 73 70 65 63 69 66 69 65 73 20 74  eter specifies t
12659 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
1265a 65 73 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f  es of space allo
1265b 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77  cated.** along w
1265c 69 74 68 20 65 61 63 68 20 70 61 67 65 20 72 65  ith each page re
1265d 66 65 72 65 6e 63 65 2e 20 54 68 69 73 20 73 70  ference. This sp
1265e 61 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ace is available
1265f 20 74 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a 20   to the user.** 
12660 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 50  via the sqlite3P
12661 61 67 65 72 47 65 74 45 78 74 72 61 28 29 20 41  agerGetExtra() A
12662 50 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c  PI..**.** The fl
12663 61 67 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ags argument is 
12664 75 73 65 64 20 74 6f 20 73 70 65 63 69 66 79 20  used to specify 
12665 70 72 6f 70 65 72 74 69 65 73 20 74 68 61 74 20  properties that 
12666 61 66 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f 70  affect the.** op
12667 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70  eration of the p
12668 61 67 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20  ager. It should 
12669 62 65 20 70 61 73 73 65 64 20 73 6f 6d 65 20 62  be passed some b
1266a 69 74 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 69  itwise combinati
1266b 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 47  on.** of the PAG
1266c 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20  ER_OMIT_JOURNAL 
1266d 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41  and PAGER_NO_REA
1266e 44 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a 0a  DLOCK flags..**.
1266f 2a 2a 20 54 68 65 20 76 66 73 46 6c 61 67 73 20  ** The vfsFlags 
12670 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62  parameter is a b
12671 69 74 6d 61 73 6b 20 74 6f 20 70 61 73 73 20 74  itmask to pass t
12672 6f 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61  o the flags para
12673 6d 65 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20  meter.** of the 
12674 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f  xOpen() method o
12675 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 56  f the supplied V
12676 46 53 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20  FS when opening 
12677 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  files. .**.** If
12678 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63   the pager objec
12679 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  t is allocated a
1267a 6e 64 20 74 68 65 20 73 70 65 63 69 66 69 65 64  nd the specified
1267b 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a 2a   file opened .**
1267c 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 53   successfully, S
1267d 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
1267e 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65  rned and *ppPage
1267f 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  r set to point t
12680 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 70 61 67  o.** the new pag
12681 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 6e  er object. If an
12682 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a   error occurs, *
12683 70 70 50 61 67 65 72 20 69 73 20 73 65 74 20 74  ppPager is set t
12684 6f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72  o NULL.** and er
12685 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65  ror code returne
12686 64 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  d. This function
12687 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49   may return SQLI
12688 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c  TE_NOMEM.** (sql
12689 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73 20  ite3Malloc() is 
1268a 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  used to allocate
1268b 20 6d 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54 45   memory), SQLITE
1268c 5f 43 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a  _CANTOPEN or .**
1268d 20 76 61 72 69 6f 75 73 20 53 51 4c 49 54 45 5f   various SQLITE_
1268e 49 4f 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a 2a  IO_XXX errors..*
1268f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
12690 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
12691 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  rOpen(.  sqlite3
12692 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
12693 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
12694 20 66 69 6c 65 20 73 79 73 74 65 6d 20 74 6f 20   file system to 
12695 75 73 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a  use */.  Pager *
12696 2a 70 70 50 61 67 65 72 2c 20 20 20 20 20 20 20  *ppPager,       
12697 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e    /* OUT: Return
12698 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63   the Pager struc
12699 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63  ture here */.  c
1269a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
1269b 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20  name,   /* Name 
1269c 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1269d 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  file to open */.
1269e 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20    int nExtra,   
1269f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
126a0 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e 64  tra bytes append
126a1 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f   to each in-memo
126a2 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ry page */.  int
126a3 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
126a4 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63        /* flags c
126a5 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20  ontrolling this 
126a6 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66  file */.  int vf
126a7 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20  sFlags          
126a8 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73     /* flags pass
126a9 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71  ed through to sq
126aa 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28  lite3_vfs.xOpen(
126ab 29 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50  ) */.){.  u8 *pP
126ac 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  tr;.  Pager *pPa
126ad 67 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  ger = 0;       /
126ae 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 74  * Pager object t
126af 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72  o allocate and r
126b0 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72  eturn */.  int r
126b1 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
126b2 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
126b3 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d 70  de */.  int temp
126b4 46 69 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 20  File = 0;       
126b5 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 74 65 6d   /* True for tem
126b6 70 20 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 69  p files (incl. i
126b7 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 20  n-memory files) 
126b8 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d  */.  int memDb =
126b9 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
126ba 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73   True if this is
126bb 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69   an in-memory fi
126bc 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 64  le */.  int read
126bd 4f 6e 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 20  Only = 0;       
126be 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
126bf 20 69 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20   is a read-only 
126c0 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f  file */.  int jo
126c1 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 20  urnalFileSize;  
126c2 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61     /* Bytes to a
126c3 6c 6c 6f 63 61 74 65 20 66 6f 72 20 65 61 63 68  llocate for each
126c4 20 6a 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a 20   journal fd */. 
126c5 20 63 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65   char *zPathname
126c6 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 6c   = 0;     /* Ful
126c7 6c 20 70 61 74 68 20 74 6f 20 64 61 74 61 62 61  l path to databa
126c8 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  se file */.  int
126c9 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20   nPathname = 0; 
126ca 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
126cb 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 50 61 74  of bytes in zPat
126cc 68 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 75  hname */.  int u
126cd 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61  seJournal = (fla
126ce 67 73 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f  gs & PAGER_OMIT_
126cf 4a 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20  JOURNAL)==0; /* 
126d0 46 61 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a 6f  False to omit jo
126d1 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e  urnal */.  int n
126d2 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61  oReadlock = (fla
126d3 67 73 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45  gs & PAGER_NO_RE
126d4 41 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a 20  ADLOCK)!=0;  /* 
126d5 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 61  True to omit rea
126d6 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20  d-lock */.  int 
126d7 70 63 61 63 68 65 53 69 7a 65 20 3d 20 73 71 6c  pcacheSize = sql
126d8 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29  ite3PcacheSize()
126d9 3b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73  ;       /* Bytes
126da 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72   to allocate for
126db 20 50 43 61 63 68 65 20 2a 2f 0a 20 20 75 31 36   PCache */.  u16
126dc 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51   szPageDflt = SQ
126dd 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
126de 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 61  E_SIZE;  /* Defa
126df 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 2a 2f  ult page size */
126e0 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
126e1 74 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65  t how much space
126e2 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
126e3 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 69   each journal fi
126e4 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 28  le-handle.  ** (
126e5 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 6f 66  there are two of
126e6 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e 20   them, the main 
126e7 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20  journal and the 
126e8 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 68  sub-journal). Th
126e9 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 6d  is.  ** is the m
126ea 61 78 69 6d 75 6d 20 73 70 61 63 65 20 72 65 71  aximum space req
126eb 75 69 72 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d  uired for an in-
126ec 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66  memory journal f
126ed 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a 2a  ile handle .  **
126ee 20 61 6e 64 20 61 20 72 65 67 75 6c 61 72 20 6a   and a regular j
126ef 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64  ournal file-hand
126f0 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 20  le. Note that a 
126f1 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c  "regular journal
126f2 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d 61  -handle".  ** ma
126f3 79 20 62 65 20 61 20 77 72 61 70 70 65 72 20 63  y be a wrapper c
126f4 61 70 61 62 6c 65 20 6f 66 20 63 61 63 68 69 6e  apable of cachin
126f5 67 20 74 68 65 20 66 69 72 73 74 20 70 6f 72 74  g the first port
126f6 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ion of the journ
126f7 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e 20  al.  ** file in 
126f8 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 6d  memory to implem
126f9 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77  ent the atomic-w
126fa 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  rite optimizatio
126fb 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f 75  n (see .  ** sou
126fc 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c  rce file journal
126fd 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  .c)..  */.  if( 
126fe 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69  sqlite3JournalSi
126ff 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 74 65 33  ze(pVfs)>sqlite3
12700 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29  MemJournalSize()
12701 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46   ){.    journalF
12702 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38  ileSize = ROUND8
12703 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53  (sqlite3JournalS
12704 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 7d 65  ize(pVfs));.  }e
12705 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c  lse{.    journal
12706 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44  FileSize = ROUND
12707 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72  8(sqlite3MemJour
12708 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d 0a  nalSize());.  }.
12709 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75  .  /* Set the ou
1270a 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 74 6f  tput variable to
1270b 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 6e   NULL in case an
1270c 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a   error occurs. *
1270d 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 30  /.  *ppPager = 0
1270e 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  ;..  /* Compute 
1270f 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 66 75  and store the fu
12710 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 61  ll pathname in a
12711 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66  n allocated buff
12712 65 72 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a 20  er pointed.  ** 
12713 74 6f 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 2c  to by zPathname,
12714 20 6c 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 6d   length nPathnam
12715 65 2e 20 4f 72 2c 20 69 66 20 74 68 69 73 20 69  e. Or, if this i
12716 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  s a temporary fi
12717 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20 62  le,.  ** leave b
12718 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 6e  oth nPathname an
12719 64 20 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 20  d zPathname set 
1271a 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  to 0..  */.  if(
1271b 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46   zFilename && zF
1271c 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20  ilename[0] ){.  
1271d 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70 56    nPathname = pV
1271e 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
1271f 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 20  ;.    zPathname 
12720 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
12721 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 20  nPathname*2);.  
12722 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 3d    if( zPathname=
12723 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
12724 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
12725 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
12726 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
12727 59 44 42 0a 20 20 20 20 69 66 28 20 73 74 72 63  YDB.    if( strc
12728 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d  mp(zFilename,":m
12729 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a 20  emory:")==0 ){. 
1272a 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a       memDb = 1;.
1272b 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b        zPathname[
1272c 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  0] = 0;.    }els
1272d 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20  e.#endif.    {. 
1272e 20 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30       zPathname[0
1272f 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20 73  ] = 0; /* Make s
12730 75 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  ure initialized 
12731 65 76 65 6e 20 69 66 20 46 75 6c 6c 50 61 74 68  even if FullPath
12732 6e 61 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f 0a  name() fails */.
12733 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12734 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e3OsFullPathname
12735 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65  (pVfs, zFilename
12736 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 61  , nPathname, zPa
12737 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a  thname);.    }..
12738 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20      nPathname = 
12739 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1273a 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  zPathname);.    
1273b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1273c 4b 20 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b 38  K && nPathname+8
1273d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d  >pVfs->mxPathnam
1273e 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  e ){.      /* Th
1273f 69 73 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b  is branch is tak
12740 65 6e 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72  en when the jour
12741 6e 61 6c 20 70 61 74 68 20 72 65 71 75 69 72 65  nal path require
12742 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 74 68  d by.      ** th
12743 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67  e database being
12744 20 6f 70 65 6e 65 64 20 77 69 6c 6c 20 62 65 20   opened will be 
12745 6d 6f 72 65 20 74 68 61 6e 20 70 56 66 73 2d 3e  more than pVfs->
12746 6d 78 50 61 74 68 6e 61 6d 65 0a 20 20 20 20 20  mxPathname.     
12747 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 6e   ** bytes in len
12748 67 74 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  gth. This means 
12749 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e  the database can
1274a 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a 20  not be opened,. 
1274b 20 20 20 20 20 2a 2a 20 61 73 20 69 74 20 77 69       ** as it wi
1274c 6c 6c 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62  ll not be possib
1274d 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 6a  le to open the j
1274e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 65  ournal file or e
1274f 76 65 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68 65  ven.      ** che
12750 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75  ck for a hot-jou
12751 72 6e 61 6c 20 62 65 66 6f 72 65 20 72 65 61 64  rnal before read
12752 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ing..      */.  
12753 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
12754 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a  CANTOPEN;.    }.
12755 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12756 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
12757 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74  qlite3_free(zPat
12758 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65  hname);.      re
12759 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
1275a 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74   }..  /* Allocat
1275b 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65  e memory for the
1275c 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65   Pager structure
1275d 2c 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c  , PCache object,
1275e 20 74 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 20   the.  ** three 
1275f 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
12760 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  , the database f
12761 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65  ile name and the
12762 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66   journal .  ** f
12763 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61  ile name. The la
12764 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69  yout in memory i
12765 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20  s as follows:.  
12766 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 65  **.  **     Page
12767 72 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20  r object        
12768 20 20 20 20 20 20 20 20 20 20 20 20 28 73 69 7a              (siz
12769 65 6f 66 28 50 61 67 65 72 29 20 62 79 74 65 73  eof(Pager) bytes
1276a 29 0a 20 20 2a 2a 20 20 20 20 20 50 43 61 63 68  ).  **     PCach
1276b 65 20 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20  e object        
1276c 20 20 20 20 20 20 20 20 20 20 20 28 73 71 6c 69             (sqli
1276d 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 20  te3PcacheSize() 
1276e 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20  bytes).  **     
1276f 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  Database file ha
12770 6e 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 20  ndle            
12771 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20  (pVfs->szOsFile 
12772 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20  bytes).  **     
12773 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  Sub-journal file
12774 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20   handle         
12775 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65  (journalFileSize
12776 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20   bytes).  **    
12777 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   Main journal fi
12778 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20  le handle       
12779 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a   (journalFileSiz
1277a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20  e bytes).  **   
1277b 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 20    Database file 
1277c 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
1277d 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62    (nPathname+1 b
1277e 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a  ytes).  **     J
1277f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
12780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
12781 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79  nPathname+8+1 by
12782 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72  tes).  */.  pPtr
12783 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33   = (u8 *)sqlite3
12784 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20  MallocZero(.    
12785 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70  ROUND8(sizeof(*p
12786 50 61 67 65 72 29 29 20 2b 20 20 20 20 20 20 2f  Pager)) +      /
12787 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  * Pager structur
12788 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28  e */.    ROUND8(
12789 70 63 61 63 68 65 53 69 7a 65 29 20 2b 20 20 20  pcacheSize) +   
1278a 20 20 20 20 20 20 20 20 2f 2a 20 50 43 61 63 68          /* PCach
1278b 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20  e object */.    
1278c 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f  ROUND8(pVfs->szO
1278d 73 46 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f  sFile) +       /
1278e 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69  * The main db fi
1278f 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61  le */.    journa
12790 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20  lFileSize * 2 + 
12791 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
12792 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  two journal file
12793 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e  s */ .    nPathn
12794 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20 20  ame + 1 +       
12795 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c           /* zFil
12796 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61  ename */.    nPa
12797 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20 20  thname + 8 + 1  
12798 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a              /* z
12799 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a  Journal */.  );.
1279a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
1279b 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 53  BYTE_ALIGNMENT(S
1279c 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
1279d 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65  (journalFileSize
1279e 29 29 20 29 3b 0a 20 20 69 66 28 20 21 70 50 74  )) );.  if( !pPt
1279f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
127a0 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29  _free(zPathname)
127a1 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
127a2 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
127a3 20 70 50 61 67 65 72 20 3d 20 20 20 20 20 20 20   pPager =       
127a4 20 20 20 20 20 20 20 28 50 61 67 65 72 2a 29 28         (Pager*)(
127a5 70 50 74 72 29 3b 0a 20 20 70 50 61 67 65 72 2d  pPtr);.  pPager-
127a6 3e 70 50 43 61 63 68 65 20 3d 20 20 20 20 28 50  >pPCache =    (P
127a7 43 61 63 68 65 2a 29 28 70 50 74 72 20 2b 3d 20  Cache*)(pPtr += 
127a8 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70  ROUND8(sizeof(*p
127a9 50 61 67 65 72 29 29 29 3b 0a 20 20 70 50 61 67  Pager)));.  pPag
127aa 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c 69  er->fd =   (sqli
127ab 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20  te3_file*)(pPtr 
127ac 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65  += ROUND8(pcache
127ad 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 72  Size));.  pPager
127ae 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 65  ->sjfd = (sqlite
127af 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d  3_file*)(pPtr +=
127b0 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a   ROUND8(pVfs->sz
127b1 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 67  OsFile));.  pPag
127b2 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69  er->jfd =  (sqli
127b3 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20  te3_file*)(pPtr 
127b4 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69  += journalFileSi
127b5 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a  ze);.  pPager->z
127b6 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 63  Filename =    (c
127b7 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f  har*)(pPtr += jo
127b8 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a  urnalFileSize);.
127b9 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
127ba 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70  BYTE_ALIGNMENT(p
127bb 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a  Pager->jfd) );..
127bc 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65    /* Fill in the
127bd 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65   Pager.zFilename
127be 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72   and Pager.zJour
127bf 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20  nal buffers, if 
127c0 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69  required. */.  i
127c1 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a  f( zPathname ){.
127c2 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75      pPager->zJou
127c3 72 6e 61 6c 20 3d 20 20 20 28 63 68 61 72 2a 29  rnal =   (char*)
127c4 28 70 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61  (pPtr += nPathna
127c5 6d 65 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65 6d  me + 1);.    mem
127c6 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c  cpy(pPager->zFil
127c7 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65  ename, zPathname
127c8 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  , nPathname);.  
127c9 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d    memcpy(pPager-
127ca 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 68  >zJournal, zPath
127cb 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29  name, nPathname)
127cc 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50  ;.    memcpy(&pP
127cd 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e  ager->zJournal[n
127ce 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75  Pathname], "-jou
127cf 72 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20 73  rnal", 8);.    s
127d0 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74  qlite3_free(zPat
127d1 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 50  hname);.  }.  pP
127d2 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70 56 66  ager->pVfs = pVf
127d3 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76 66 73  s;.  pPager->vfs
127d4 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61 67 73  Flags = vfsFlags
127d5 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65  ;..  /* Open the
127d6 20 70 61 67 65 72 20 66 69 6c 65 2e 0a 20 20 2a   pager file..  *
127d7 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d  /.  if( zFilenam
127d8 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30  e && zFilename[0
127d9 5d 20 26 26 20 21 6d 65 6d 44 62 20 29 7b 0a 20  ] && !memDb ){. 
127da 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b     int fout = 0;
127db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
127dc 20 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73      /* VFS flags
127dd 20 72 65 74 75 72 6e 65 64 20 62 79 20 78 4f 70   returned by xOp
127de 65 6e 28 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d  en() */.    rc =
127df 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
127e0 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69  Vfs, pPager->zFi
127e1 6c 65 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e  lename, pPager->
127e2 66 64 2c 20 76 66 73 46 6c 61 67 73 2c 20 26 66  fd, vfsFlags, &f
127e3 6f 75 74 29 3b 0a 20 20 20 20 72 65 61 64 4f 6e  out);.    readOn
127e4 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 54  ly = (fout&SQLIT
127e5 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29  E_OPEN_READONLY)
127e6 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ;..    /* If the
127e7 20 66 69 6c 65 20 77 61 73 20 73 75 63 63 65 73   file was succes
127e8 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f  sfully opened fo
127e9 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63  r read/write acc
127ea 65 73 73 2c 0a 20 20 20 20 2a 2a 20 63 68 6f 6f  ess,.    ** choo
127eb 73 65 20 61 20 64 65 66 61 75 6c 74 20 70 61 67  se a default pag
127ec 65 20 73 69 7a 65 20 69 6e 20 63 61 73 65 20 77  e size in case w
127ed 65 20 68 61 76 65 20 74 6f 20 63 72 65 61 74 65  e have to create
127ee 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61   the.    ** data
127ef 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 64  base file. The d
127f0 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65  efault page size
127f1 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
127f2 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  of:.    **.    *
127f3 2a 20 20 20 20 2b 20 53 51 4c 49 54 45 5f 44 45  *    + SQLITE_DE
127f4 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c  FAULT_PAGE_SIZE,
127f5 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65  .    **    + The
127f6 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
127f7 62 79 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74  by sqlite3OsSect
127f8 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20  orSize().    ** 
127f9 20 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74     + The largest
127fa 20 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20   page size that 
127fb 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61  can be written a
127fc 74 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 2a  tomically..    *
127fd 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
127fe 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65 61 64  LITE_OK && !read
127ff 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 65  Only ){.      se
12800 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67  tSectorSize(pPag
12801 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  er);.      asser
12802 74 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  t(SQLITE_DEFAULT
12803 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 53 51 4c 49  _PAGE_SIZE<=SQLI
12804 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50  TE_MAX_DEFAULT_P
12805 41 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20  AGE_SIZE);.     
12806 20 69 66 28 20 73 7a 50 61 67 65 44 66 6c 74 3c   if( szPageDflt<
12807 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
12808 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ze ){.        if
12809 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72  ( pPager->sector
1280a 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  Size>SQLITE_MAX_
1280b 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
1280c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  E ){.          s
1280d 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49  zPageDflt = SQLI
1280e 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50  TE_MAX_DEFAULT_P
1280f 41 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20  AGE_SIZE;.      
12810 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
12811 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20     szPageDflt = 
12812 28 75 31 36 29 70 50 61 67 65 72 2d 3e 73 65 63  (u16)pPager->sec
12813 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20  torSize;.       
12814 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66 64 65   }.      }.#ifde
12815 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
12816 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20  ATOMIC_WRITE.   
12817 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74     {.        int
12818 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73   iDc = sqlite3Os
12819 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
1281a 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64  stics(pPager->fd
1281b 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  );.        int i
1281c 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  i;.        asser
1281d 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  t(SQLITE_IOCAP_A
1281e 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e  TOMIC512==(512>>
1281f 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  8));.        ass
12820 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50  ert(SQLITE_IOCAP
12821 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35  _ATOMIC64K==(655
12822 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20  36>>8));.       
12823 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 4d   assert(SQLITE_M
12824 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
12825 53 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0a 20 20  SIZE<=65536);.  
12826 20 20 20 20 20 20 66 6f 72 28 69 69 3d 73 7a 50        for(ii=szP
12827 61 67 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c  ageDflt; ii<=SQL
12828 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f  ITE_MAX_DEFAULT_
12829 50 41 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69  PAGE_SIZE; ii=ii
1282a 2a 32 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  *2){.          i
1282b 66 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49  f( iDc&(SQLITE_I
1282c 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e  OCAP_ATOMIC|(ii>
1282d 3e 38 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20  >8)) ){.        
1282e 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d      szPageDflt =
1282f 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   ii;.          }
12830 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
12831 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a   }.#endif.    }.
12832 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
12833 49 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  If a temporary f
12834 69 6c 65 20 69 73 20 72 65 71 75 65 73 74 65 64  ile is requested
12835 2c 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e  , it is not open
12836 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a  ed immediately..
12837 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63      ** In this c
12838 61 73 65 20 77 65 20 61 63 63 65 70 74 20 74 68  ase we accept th
12839 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73  e default page s
1283a 69 7a 65 20 61 6e 64 20 64 65 6c 61 79 20 61 63  ize and delay ac
1283b 74 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f 70  tually.    ** op
1283c 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 75  ening the file u
1283d 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 63  ntil the first c
1283e 61 6c 6c 20 74 6f 20 4f 73 57 72 69 74 65 28 29  all to OsWrite()
1283f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
12840 54 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 61  This branch is a
12841 6c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e 20 69  lso run for an i
12842 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
12843 65 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a  e. An in-memory.
12844 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
12845 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61  is the same as a
12846 20 74 65 6d 70 2d 66 69 6c 65 20 74 68 61 74 20   temp-file that 
12847 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e  is never written
12848 20 6f 75 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64   out to.    ** d
12849 69 73 6b 20 61 6e 64 20 75 73 65 73 20 61 6e 20  isk and uses an 
1284a 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61  in-memory rollba
1284b 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20  ck journal..    
1284c 2a 2f 20 0a 20 20 20 20 74 65 6d 70 46 69 6c 65  */ .    tempFile
1284d 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 72   = 1;.    pPager
1284e 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
1284f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 0a  EXCLUSIVE;.  }..
12850 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
12851 6e 67 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72  ng call to Pager
12852 53 65 74 50 61 67 65 73 69 7a 65 28 29 20 73 65  SetPagesize() se
12853 72 76 65 73 20 74 6f 20 73 65 74 20 74 68 65 20  rves to set the 
12854 76 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a 20 50  value of .  ** P
12855 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20 61 6e  ager.pageSize an
12856 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68  d to allocate th
12857 65 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63  e Pager.pTmpSpac
12858 65 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f 0a 20  e buffer..  */. 
12859 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1285a 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OK ){.    assert
1285b 28 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d  ( pPager->memDb=
1285c 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  =0 );.    rc = s
1285d 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
1285e 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26  gesize(pPager, &
1285f 73 7a 50 61 67 65 44 66 6c 74 29 3b 0a 20 20 20  szPageDflt);.   
12860 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
12861 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a  QLITE_OK );.  }.
12862 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f  .  /* If an erro
12863 72 20 6f 63 63 75 72 72 65 64 20 69 6e 20 65 69  r occurred in ei
12864 74 68 65 72 20 6f 66 20 74 68 65 20 62 6c 6f 63  ther of the bloc
12865 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 65 20 74  ks above, free t
12866 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 20 73  he .  ** Pager s
12867 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 6c 6f  tructure and clo
12868 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a  se the file..  *
12869 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  /.  if( rc!=SQLI
1286a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73  TE_OK ){.    ass
1286b 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 70 54  ert( !pPager->pT
1286c 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 20 20 73  mpSpace );.    s
1286d 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
1286e 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73  ager->fd);.    s
1286f 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67  qlite3_free(pPag
12870 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  er);.    return 
12871 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e  rc;.  }..  /* In
12872 69 74 69 61 6c 69 7a 65 20 74 68 65 20 50 43 61  itialize the PCa
12873 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20  che object. */. 
12874 20 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38   nExtra = ROUND8
12875 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 6c 69  (nExtra);.  sqli
12876 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a  te3PcacheOpen(sz
12877 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61  PageDflt, nExtra
12878 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 20 20  , !memDb,.      
12879 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d                !m
1287a 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73  emDb?pagerStress
1287b 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67  :0, (void *)pPag
1287c 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61  er, pPager->pPCa
1287d 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 54 52  che);..  PAGERTR
1287e 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25 73  ACE(("OPEN %d %s
1287f 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49  \n", FILEHANDLEI
12880 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c 20 70  D(pPager->fd), p
12881 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65  Pager->zFilename
12882 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22  ));.  IOTRACE(("
12883 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70  OPEN %p %s\n", p
12884 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 7a  Pager, pPager->z
12885 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 70 50  Filename))..  pP
12886 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c  ager->useJournal
12887 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61   = (u8)useJourna
12888 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52  l;.  pPager->noR
12889 65 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61  eadlock = (noRea
1288a 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c  dlock && readOnl
1288b 79 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50  y) ?1:0;.  /* pP
1288c 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d  ager->stmtOpen =
1288d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
1288e 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20  er->stmtInUse = 
1288f 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
12890 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a  r->nRef = 0; */.
12891 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
12892 56 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44  Valid = (u8)memD
12893 62 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  b;.  /* pPager->
12894 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f  stmtSize = 0; */
12895 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74  .  /* pPager->st
12896 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a  mtJSize = 0; */.
12897 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61    /* pPager->nPa
12898 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  ge = 0; */.  pPa
12899 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51  ger->mxPgno = SQ
1289a 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f  LITE_MAX_PAGE_CO
1289b 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72  UNT;.  /* pPager
1289c 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
1289d 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73  UNLOCK; */.  ass
1289e 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
1289f 74 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20  te == (tempFile 
128a0 3f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  ? PAGER_EXCLUSIV
128a1 45 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  E : PAGER_UNLOCK
128a2 29 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72  ) );.  /* pPager
128a3 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a  ->errMask = 0; *
128a4 2f 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70  /.  pPager->temp
128a5 46 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46  File = (u8)tempF
128a6 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74  ile;.  assert( t
128a7 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c  empFile==PAGER_L
128a8 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
128a9 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20  L .          || 
128aa 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f  tempFile==PAGER_
128ab 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
128ac 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72  USIVE );.  asser
128ad 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  t( PAGER_LOCKING
128ae 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d  MODE_EXCLUSIVE==
128af 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65  1 );.  pPager->e
128b0 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28  xclusiveMode = (
128b1 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20  u8)tempFile; .  
128b2 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
128b3 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72  untDone = pPager
128b4 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50  ->tempFile;.  pP
128b5 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75  ager->memDb = (u
128b6 38 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65  8)memDb;.  pPage
128b7 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75  r->readOnly = (u
128b8 38 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a  8)readOnly;.  /*
128b9 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
128ba 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  c = 0; */.  pPag
128bb 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 28 70 50  er->noSync = (pP
128bc 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 7c  ager->tempFile |
128bd 7c 20 21 75 73 65 4a 6f 75 72 6e 61 6c 29 20 3f  | !useJournal) ?
128be 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66  1:0;.  pPager->f
128bf 75 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 72  ullSync = pPager
128c0 2d 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a 20  ->noSync ?0:1;. 
128c1 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
128c2 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 4e  ags = SQLITE_SYN
128c3 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70  C_NORMAL;.  /* p
128c4 50 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d 20  Pager->pFirst = 
128c5 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
128c6 72 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 20  r->pFirstSynced 
128c7 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61  = 0; */.  /* pPa
128c8 67 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b 20  ger->pLast = 0; 
128c9 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 78  */.  pPager->nEx
128ca 74 72 61 20 3d 20 6e 45 78 74 72 61 3b 0a 20 20  tra = nExtra;.  
128cb 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53  pPager->journalS
128cc 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54  izeLimit = SQLIT
128cd 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41  E_DEFAULT_JOURNA
128ce 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20  L_SIZE_LIMIT;.  
128cf 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
128d0 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65  Pager->fd) || te
128d1 6d 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53  mpFile );.  setS
128d2 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72  ectorSize(pPager
128d3 29 3b 0a 20 20 69 66 28 20 6d 65 6d 44 62 20 29  );.  if( memDb )
128d4 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
128d5 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45  urnalMode = PAGE
128d6 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
128d7 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70  MORY;.  }.  /* p
128d8 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64  Pager->xBusyHand
128d9 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a  ler = 0; */.  /*
128da 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61   pPager->pBusyHa
128db 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f  ndlerArg = 0; */
128dc 0a 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 61  .  /* memset(pPa
128dd 67 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 73  ger->aHash, 0, s
128de 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 48  izeof(pPager->aH
128df 61 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 50  ash)); */.  *ppP
128e0 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 20  ager = pPager;. 
128e1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
128e2 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  K;.}..../*.** Th
128e3 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
128e4 61 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 6e  alled after tran
128e5 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 50  sitioning from P
128e6 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a  AGER_UNLOCK to.*
128e7 2a 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73  * PAGER_SHARED s
128e8 74 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 69  tate. It tests i
128e9 66 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74  f there is a hot
128ea 20 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e 74   journal present
128eb 20 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d   in.** the file-
128ec 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 67  system for the g
128ed 69 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 6f  iven pager. A ho
128ee 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e 65  t journal is one
128ef 20 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 20   that .** needs 
128f0 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63  to be played bac
128f1 6b 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f 20  k. According to 
128f2 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61  this function, a
128f3 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20   hot-journal.** 
128f4 66 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 74  file exists if t
128f5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69  he following cri
128f6 74 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a 2a  teria are met:.*
128f7 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f 75  *.**   * The jou
128f8 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73  rnal file exists
128f9 20 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 73   in the file sys
128fa 74 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20  tem, and.**   * 
128fb 4e 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  No process holds
128fc 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 67   a RESERVED or g
128fd 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74  reater lock on t
128fe 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
128ff 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65  , and.**   * The
12900 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
12901 74 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 72  tself is greater
12902 20 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 6e   than 0 bytes in
12903 20 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 20   size, and.**   
12904 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65  * The first byte
12905 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
12906 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20  file exists and 
12907 69 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a  is not 0x00..**.
12908 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e  ** If the curren
12909 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  t size of the da
1290a 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 30  tabase file is 0
1290b 20 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 66   but a journal f
1290c 69 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 74  ile.** exists, t
1290d 68 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 20  hat is probably 
1290e 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c  an old journal l
1290f 65 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 20  eft over from a 
12910 70 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 73  prior.** databas
12911 65 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  e with the same 
12912 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61  name. In this ca
12913 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  se the journal f
12914 69 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 64  ile is.** just d
12915 65 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 44  eleted using OsD
12916 65 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 20  elete, *pExists 
12917 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20  is set to 0 and 
12918 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20  SQLITE_OK.** is 
12919 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
1291a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
1291b 73 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 74  s not check if t
1291c 68 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 72  here is a master
1291d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d   journal filenam
1291e 65 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20  e.** at the end 
1291f 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20  of the file. If 
12920 74 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 68  there is, and th
12921 61 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  at master journa
12922 6c 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e  l file.** does n
12923 6f 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 74  ot exist, then t
12924 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
12925 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f  is not really ho
12926 74 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 61  t. In this.** ca
12927 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  se this routine 
12928 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 61  will return a fa
12929 6c 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 68  lse-positive. Th
1292a 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  e pager_playback
1292b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69  ().** routine wi
1292c 6c 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 74  ll discover that
1292d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1292e 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20  e is not really 
1292f 68 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c  hot and .** will
12930 20 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 63   not roll it bac
12931 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 68  k. .**.** If a h
12932 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ot-journal file 
12933 69 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 73  is found to exis
12934 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73  t, *pExists is s
12935 65 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a 20  et to 1 and .** 
12936 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
12937 65 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f  ed. If no hot-jo
12938 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72  urnal file is pr
12939 65 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 20  esent, *pExists 
1293a 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 61  is.** set to 0 a
1293b 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
1293c 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20  urned. If an IO 
1293d 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69  error occurs whi
1293e 6c 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20  le trying.** to 
1293f 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65  determine whethe
12940 72 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d 6a  r or not a hot-j
12941 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73  ournal file exis
12942 74 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72  ts, the IO error
12943 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75  .** code is retu
12944 72 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 6c  rned and the val
12945 75 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 69  ue of *pExists i
12946 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
12947 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 6f  static int hasHo
12948 74 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a  tJournal(Pager *
12949 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 78  pPager, int *pEx
1294a 69 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  ists){.  sqlite3
1294b 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66  _vfs * const pVf
1294c 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73  s = pPager->pVfs
1294d 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ;.  int rc;     
1294e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1294f 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
12950 20 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 73   */.  int exists
12951 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12952 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
12953 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
12954 20 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 61   present */..  a
12955 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30  ssert( pPager!=0
12956 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
12957 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c  ager->useJournal
12958 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73   );.  assert( is
12959 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
1295a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69   );.  assert( !i
1295b 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
1295c 64 29 20 29 3b 0a 0a 20 20 2a 70 45 78 69 73 74  d) );..  *pExist
1295d 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71  s = 0;.  rc = sq
1295e 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56  lite3OsAccess(pV
1295f 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  fs, pPager->zJou
12960 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43  rnal, SQLITE_ACC
12961 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69  ESS_EXISTS, &exi
12962 73 74 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  sts);.  if( rc==
12963 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 78 69  SQLITE_OK && exi
12964 73 74 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c  sts ){.    int l
12965 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 20  ocked;          
12966 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
12967 66 20 73 6f 6d 65 20 70 72 6f 63 65 73 73 20 68  f some process h
12968 6f 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 20  olds a RESERVED 
12969 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 72 63 20 3d  lock */.    rc =
1296a 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52   sqlite3OsCheckR
1296b 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 50 61 67  eservedLock(pPag
1296c 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b 65 64 29  er->fd, &locked)
1296d 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1296e 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b  LITE_OK && !lock
1296f 65 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ed ){.      int 
12970 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 2f 2a  nPage;..      /*
12971 20 43 68 65 63 6b 20 74 68 65 20 73 69 7a 65 20   Check the size 
12972 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
12973 66 69 6c 65 2e 20 49 66 20 69 74 20 63 6f 6e 73  file. If it cons
12974 69 73 74 73 20 6f 66 20 30 20 70 61 67 65 73 2c  ists of 0 pages,
12975 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 64  .      ** then d
12976 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  elete the journa
12977 6c 20 66 69 6c 65 2e 20 53 65 65 20 74 68 65 20  l file. See the 
12978 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61  header comment a
12979 62 6f 76 65 20 66 6f 72 20 0a 20 20 20 20 20 20  bove for .      
1297a 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 69 6e 67  ** the reasoning
1297b 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a   here..      */.
1297c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1297d 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
1297e 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  (pPager, &nPage)
1297f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
12980 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12981 20 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d       if( nPage==
12982 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
12983 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c  c = sqlite3OsDel
12984 65 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 72  ete(pVfs, pPager
12985 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a  ->zJournal, 0);.
12986 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
12987 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
12988 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69  journal file exi
12989 73 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72  sts and no other
1298a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20   connection has 
1298b 61 20 72 65 73 65 72 76 65 64 0a 20 20 20 20 20  a reserved.     
1298c 20 20 20 20 20 2a 2a 20 6f 72 20 67 72 65 61 74       ** or great
1298d 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  er lock on the d
1298e 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f  atabase file. No
1298f 77 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65  w check that the
12990 72 65 20 69 73 0a 20 20 20 20 20 20 20 20 20 20  re is.          
12991 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  ** at least one 
12992 6e 6f 6e 2d 7a 65 72 6f 20 62 79 74 65 73 20 61  non-zero bytes a
12993 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
12994 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
12995 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66  .          ** If
12996 20 74 68 65 72 65 20 69 73 2c 20 74 68 65 6e 20   there is, then 
12997 77 65 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73  we consider this
12998 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 68   journal to be h
12999 6f 74 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 20 20  ot. If not, .   
1299a 20 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e         ** it can
1299b 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20   be ignored..   
1299c 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1299d 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49      int f = SQLI
1299e 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
1299f 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  |SQLITE_OPEN_MAI
129a0 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20  N_JOURNAL;.     
129a1 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
129a2 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50  3OsOpen(pVfs, pP
129a3 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
129a4 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20  pPager->jfd, f, 
129a5 26 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  &f);.          i
129a6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
129a7 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
129a8 75 38 20 66 69 72 73 74 20 3d 20 30 3b 0a 20 20  u8 first = 0;.  
129a9 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
129aa 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61  qlite3OsRead(pPa
129ab 67 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 64 20  ger->jfd, (void 
129ac 2a 29 26 66 69 72 73 74 2c 20 31 2c 20 30 29 3b  *)&first, 1, 0);
129ad 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
129ae 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
129af 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a  R_SHORT_READ ){.
129b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63                rc
129b1 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
129b2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
129b3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
129b4 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a  sClose(pPager->j
129b5 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  fd);.           
129b6 20 2a 70 45 78 69 73 74 73 20 3d 20 28 66 69 72   *pExists = (fir
129b7 73 74 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20  st!=0);.        
129b8 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
129b9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
129ba 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
129bb 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20  ./*.** Read the 
129bc 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 65  content for page
129bd 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20   pPg out of the 
129be 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
129bf 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e  d into .** pPg->
129c0 70 44 61 74 61 2e 20 41 20 73 68 61 72 65 64 20  pData. A shared 
129c1 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20  lock or greater 
129c2 6d 75 73 74 20 62 65 20 68 65 6c 64 20 6f 6e 20  must be held on 
129c3 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
129c4 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69 73  file before this
129c5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
129c6 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61  led..**.** If pa
129c7 67 65 20 31 20 69 73 20 72 65 61 64 2c 20 74 68  ge 1 is read, th
129c8 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  en the value of 
129c9 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73  Pager.dbFileVers
129ca 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20  [] is set to.** 
129cb 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66  the value read f
129cc 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
129cd 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
129ce 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
129cf 72 73 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20  rs, then the IO 
129d0 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
129d1 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e  d to the caller.
129d2 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 53  .** Otherwise, S
129d3 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
129d4 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
129d5 69 6e 74 20 72 65 61 64 44 62 50 61 67 65 28 50  int readDbPage(P
129d6 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61  gHdr *pPg){.  Pa
129d7 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
129d8 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 61  g->pPager; /* Pa
129d9 67 65 72 20 6f 62 6a 65 63 74 20 61 73 73 6f 63  ger object assoc
129da 69 61 74 65 64 20 77 69 74 68 20 70 61 67 65 20  iated with page 
129db 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  pPg */.  Pgno pg
129dc 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20  no = pPg->pgno; 
129dd 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
129de 6d 62 65 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a  mber to read */.
129df 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
129e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
129e1 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
129e2 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20  .  i64 iOffset; 
129e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129e4 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f  /* Byte offset o
129e5 66 20 66 69 6c 65 20 74 6f 20 72 65 61 64 20 66  f file to read f
129e6 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  rom */..  assert
129e7 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
129e8 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 26 26  =PAGER_SHARED &&
129e9 20 21 4d 45 4d 44 42 20 29 3b 0a 0a 20 20 69 66   !MEMDB );..  if
129ea 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  ( !isOpen(pPager
129eb 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73  ->fd) ){.    ass
129ec 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d  ert( pPager->tem
129ed 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 6d  pFile );.    mem
129ee 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20  set(pPg->pData, 
129ef 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  0, pPager->pageS
129f0 69 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ize);.    return
129f1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
129f2 20 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 6e    iOffset = (pgn
129f3 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72  o-1)*(i64)pPager
129f4 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 63  ->pageSize;.  rc
129f5 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
129f6 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 67  (pPager->fd, pPg
129f7 2d 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 2d  ->pData, pPager-
129f8 3e 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 73  >pageSize, iOffs
129f9 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  et);.  if( rc==S
129fa 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
129fb 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 63  T_READ ){.    rc
129fc 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
129fd 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20  }.  if( pgno==1 
129fe 29 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 6c  ){.    u8 *dbFil
129ff 65 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 70  eVers = &((u8*)p
12a00 50 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b 0a  Pg->pData)[24];.
12a01 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67      memcpy(&pPag
12a02 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20  er->dbFileVers, 
12a03 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65  dbFileVers, size
12a04 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  of(pPager->dbFil
12a05 65 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 43  eVers));.  }.  C
12a06 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 50  ODEC1(pPager, pP
12a07 67 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20  g->pData, pgno, 
12a08 33 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 4e 43  3);..  PAGER_INC
12a09 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  R(sqlite3_pager_
12a0a 72 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20  readdb_count);. 
12a0b 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67   PAGER_INCR(pPag
12a0c 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f  er->nRead);.  IO
12a0d 54 52 41 43 45 28 28 22 50 47 49 4e 20 25 70 20  TRACE(("PGIN %p 
12a0e 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
12a0f 67 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 54 52  gno));.  PAGERTR
12a10 41 43 45 28 28 22 46 45 54 43 48 20 25 64 20 70  ACE(("FETCH %d p
12a11 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78  age %d hash(%08x
12a12 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  )\n",.          
12a13 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61       PAGERID(pPa
12a14 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65  ger), pgno, page
12a15 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 29  r_pagehash(pPg))
12a16 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
12a17 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
12a18 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
12a19 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 75  d whenever the u
12a1a 70 70 65 72 20 6c 61 79 65 72 20 72 65 71 75 65  pper layer reque
12a1b 73 74 73 20 61 20 64 61 74 61 62 61 73 65 0a 2a  sts a database.*
12a1c 2a 20 70 61 67 65 20 69 73 20 72 65 71 75 65 73  * page is reques
12a1d 74 65 64 2c 20 62 65 66 6f 72 65 20 74 68 65 20  ted, before the 
12a1e 63 61 63 68 65 20 69 73 20 63 68 65 63 6b 65 64  cache is checked
12a1f 20 66 6f 72 20 61 20 73 75 69 74 61 62 6c 65 20   for a suitable 
12a20 70 61 67 65 0a 2a 2a 20 6f 72 20 61 6e 79 20 64  page.** or any d
12a21 61 74 61 20 69 73 20 72 65 61 64 20 66 72 6f 6d  ata is read from
12a22 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49   the database. I
12a23 74 20 70 65 72 66 6f 72 6d 73 20 74 68 65 20 66  t performs the f
12a24 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 77 6f 20  ollowing.** two 
12a25 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a  functions:.**.**
12a26 20 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67     1) If the pag
12a27 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  er is currently 
12a28 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  in PAGER_UNLOCK 
12a29 73 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68  state (no lock h
12a2a 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74  eld.**      on t
12a2b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12a2c 29 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d  ), then an attem
12a2d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62  pt is made to ob
12a2e 74 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53  tain a.**      S
12a2f 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68  HARED lock on th
12a30 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
12a31 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74   Immediately aft
12a32 65 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20  er obtaining.** 
12a33 20 20 20 20 20 74 68 65 20 53 48 41 52 45 44 20       the SHARED 
12a34 6c 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73  lock, the file-s
12a35 79 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64  ystem is checked
12a36 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e   for a hot-journ
12a37 61 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63  al,.**      whic
12a38 68 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b  h is played back
12a39 20 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c   if present. Fol
12a3a 6c 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a  lowing any hot-j
12a3b 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20  ournal .**      
12a3c 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f  rollback, the co
12a3d 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61  ntents of the ca
12a3e 63 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 65  che are validate
12a3f 64 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a  d by checking.**
12a40 20 20 20 20 20 20 74 68 65 20 27 63 68 61 6e 67        the 'chang
12a41 65 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64  e-counter' field
12a42 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
12a43 20 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64   file header and
12a44 0a 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 64  .**      discard
12a45 65 64 20 69 66 20 74 68 65 79 20 61 72 65 20 66  ed if they are f
12a46 6f 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c  ound to be inval
12a47 69 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49  id..**.**   2) I
12a48 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72  f the pager is r
12a49 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73  unning in exclus
12a4a 69 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68  ive-mode, and th
12a4b 65 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c  ere are currentl
12a4c 79 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74  y.**      no out
12a4d 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e  standing referen
12a4e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73  ces to any pages
12a4f 2c 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20  , and is in the 
12a50 65 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20  error state,.** 
12a51 20 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74       then an att
12a52 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
12a53 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20  clear the error 
12a54 73 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64  state by discard
12a55 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20  ing.**      the 
12a56 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
12a57 70 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72  page cache and r
12a58 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20  olling back any 
12a59 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  open journal.** 
12a5a 20 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a       file..**.**
12a5b 20 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f   If the operatio
12a5c 6e 20 64 65 73 63 72 69 62 65 64 20 62 79 20 28  n described by (
12a5d 32 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20  2) above is not 
12a5e 61 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 69  attempted, and i
12a5f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 69  f the.** pager i
12a60 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74  s in an error st
12a61 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53  ate other than S
12a62 51 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e 20  QLITE_FULL when 
12a63 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 0a  this is called,.
12a64 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  ** the error sta
12a65 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  te error code is
12a66 20 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 73   returned. It is
12a67 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65   permitted to re
12a68 61 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  ad the.** databa
12a69 73 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 54  se when in SQLIT
12a6a 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 61  E_FULL error sta
12a6b 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  te..**.** Otherw
12a6c 69 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69  ise, if everythi
12a6d 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ng is successful
12a6e 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
12a6f 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a 2a  eturned. If an.*
12a70 2a 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  * IO error occur
12a71 73 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  s while locking 
12a72 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 68  the database, ch
12a73 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f 74  ecking for a hot
12a74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65  -journal.** file
12a75 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b   or rolling back
12a76 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c   a journal file,
12a77 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f   the IO error co
12a78 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
12a79 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
12a7a 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61  gerSharedLock(Pa
12a7b 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
12a7c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
12a7d 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
12a7e 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
12a7f 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72  e */.  int isErr
12a80 6f 72 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20  orReset = 0;    
12a81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
12a82 65 20 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20  e if recovering 
12a83 66 72 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65  from error state
12a84 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69   */..  /* If thi
12a85 73 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  s database is op
12a86 65 6e 65 64 20 66 6f 72 20 65 78 63 6c 75 73 69  ened for exclusi
12a87 76 65 20 61 63 63 65 73 73 2c 20 68 61 73 20 6e  ve access, has n
12a88 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 0a 20  o outstanding . 
12a89 20 2a 2a 20 70 61 67 65 20 72 65 66 65 72 65 6e   ** page referen
12a8a 63 65 73 20 61 6e 64 20 69 73 20 69 6e 20 61 6e  ces and is in an
12a8b 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 68   error-state, th
12a8c 69 73 20 69 73 20 61 20 63 68 61 6e 63 65 20 74  is is a chance t
12a8d 6f 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65  o clear.  ** the
12a8e 20 65 72 72 6f 72 2e 20 44 69 73 63 61 72 64 20   error. Discard 
12a8f 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
12a90 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
12a91 61 6e 64 20 74 72 65 61 74 20 61 6e 79 0a 20 20  and treat any.  
12a92 2a 2a 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20  ** open journal 
12a93 66 69 6c 65 20 61 73 20 61 20 68 6f 74 2d 6a 6f  file as a hot-jo
12a94 75 72 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66  urnal..  */.  if
12a95 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67  ( !MEMDB && pPag
12a96 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
12a97 65 20 0a 20 20 20 26 26 20 73 71 6c 69 74 65 33  e .   && sqlite3
12a98 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70  PcacheRefCount(p
12a99 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d  Pager->pPCache)=
12a9a 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72  =0 && pPager->er
12a9b 72 43 6f 64 65 20 0a 20 20 29 7b 0a 20 20 20 20  rCode .  ){.    
12a9c 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  if( isOpen(pPage
12a9d 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20  r->jfd) ){.     
12a9e 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d 20   isErrorReset = 
12a9f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  1;.    }.    pPa
12aa0 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53  ger->errCode = S
12aa1 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61  QLITE_OK;.    pa
12aa2 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
12aa3 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
12aa4 74 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69  the pager is sti
12aa5 6c 6c 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73  ll in an error s
12aa6 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20 70 72 6f  tate, do not pro
12aa7 63 65 65 64 2e 20 54 68 65 20 65 72 72 6f 72 20  ceed. The error 
12aa8 0a 20 20 2a 2a 20 73 74 61 74 65 20 77 69 6c 6c  .  ** state will
12aa9 20 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 73   be cleared at s
12aaa 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  ome point in the
12aab 20 66 75 74 75 72 65 20 77 68 65 6e 20 61 6c 6c   future when all
12aac 20 70 61 67 65 20 0a 20 20 2a 2a 20 72 65 66 65   page .  ** refe
12aad 72 65 6e 63 65 73 20 61 72 65 20 64 72 6f 70 70  rences are dropp
12aae 65 64 20 61 6e 64 20 74 68 65 20 63 61 63 68 65  ed and the cache
12aaf 20 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65   can be discarde
12ab0 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  d..  */.  if( pP
12ab1 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26  ager->errCode &&
12ab2 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
12ab3 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b  !=SQLITE_FULL ){
12ab4 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67  .    return pPag
12ab5 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d  er->errCode;.  }
12ab6 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
12ab7 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c  state==PAGER_UNL
12ab8 4f 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 65  OCK || isErrorRe
12ab9 73 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  set ){.    sqlit
12aba 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70  e3_vfs * const p
12abb 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56  Vfs = pPager->pV
12abc 66 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 6f  fs;.    int isHo
12abd 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  tJournal = 0;.  
12abe 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42    assert( !MEMDB
12abf 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
12ac0 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
12ac1 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50  Count(pPager->pP
12ac2 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20  Cache)==0 );.   
12ac3 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f   if( !pPager->no
12ac4 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20  Readlock ){.    
12ac5 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69    rc = pager_wai
12ac6 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72  t_on_lock(pPager
12ac7 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a  , SHARED_LOCK);.
12ac8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
12ac9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12aca 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
12acb 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  r->state==PAGER_
12acc 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20  UNLOCK );.      
12acd 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65    return pager_e
12ace 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
12acf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
12ad0 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e  lse if( pPager->
12ad1 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c  state==PAGER_UNL
12ad2 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61  OCK ){.      pPa
12ad3 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47  ger->state = PAG
12ad4 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 7d  ER_SHARED;.    }
12ad5 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
12ad6 67 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52  ger->state>=SHAR
12ad7 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20  ED_LOCK );..    
12ad8 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20  /* If a journal 
12ad9 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64  file exists, and
12ada 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53   there is no RES
12adb 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68  ERVED lock on th
12adc 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73  e.    ** databas
12add 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20  e file, then it 
12ade 65 69 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20  either needs to 
12adf 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f  be played back o
12ae0 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  r deleted..    *
12ae1 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45 72 72  /.    if( !isErr
12ae2 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20  orReset ){.     
12ae3 20 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72   rc = hasHotJour
12ae4 6e 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 48  nal(pPager, &isH
12ae5 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20  otJournal);.    
12ae6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12ae7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
12ae8 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
12ae9 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
12aea 28 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 7c  ( isErrorReset |
12aeb 7c 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29  | isHotJournal )
12aec 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61  {.      /* Get a
12aed 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
12aee 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
12aef 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70   file. At this p
12af0 6f 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 20  oint it is.     
12af1 20 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 68   ** important th
12af2 61 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  at a RESERVED lo
12af3 63 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e  ck is not obtain
12af4 65 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f  ed on the way to
12af5 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 58   the.      ** EX
12af6 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66  CLUSIVE lock. If
12af7 20 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65   it were, anothe
12af8 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20  r process might 
12af9 6f 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 2a  open the.      *
12afa 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  * database file,
12afb 20 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45   detect the RESE
12afc 52 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63  RVED lock, and c
12afd 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65  onclude that the
12afe 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61  .      ** databa
12aff 73 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 65  se is safe to re
12b00 61 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 72  ad while this pr
12b01 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 72  ocess is still r
12b02 6f 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20 20  olling the .    
12b03 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c    ** hot-journal
12b04 20 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20   back..      ** 
12b05 0a 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 73  .      ** Becaus
12b06 65 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61  e the intermedia
12b07 74 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b  te RESERVED lock
12b08 20 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 65   is not requeste
12b09 64 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20  d, any.      ** 
12b0a 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 61 74  other process at
12b0b 74 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63 65  tempting to acce
12b0c 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ss the database 
12b0d 66 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74 6f  file will get to
12b0e 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20   .      ** this 
12b0f 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64  point in the cod
12b10 65 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62  e and fail to ob
12b11 74 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 43  tain its own EXC
12b12 4c 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20  LUSIVE lock .   
12b13 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74     ** on the dat
12b14 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20  abase file..    
12b15 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
12b16 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58 43  Pager->state<EXC
12b17 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20  LUSIVE_LOCK ){. 
12b18 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
12b19 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72  te3OsLock(pPager
12b1a 2d 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 5f  ->fd, EXCLUSIVE_
12b1b 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69  LOCK);.        i
12b1c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12b1d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
12b1e 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70   = pager_error(p
12b1f 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20  Pager, rc);.    
12b20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65        goto faile
12b21 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  d;.        }.   
12b22 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61       pPager->sta
12b23 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55  te = PAGER_EXCLU
12b24 53 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a  SIVE;.      }. .
12b25 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68        /* Open th
12b26 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65  e journal for re
12b27 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e  ad/write access.
12b28 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
12b29 20 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65 78   in .      ** ex
12b2a 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d  clusive-access m
12b2b 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 73  ode the file des
12b2c 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20  criptor will be 
12b2d 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20  kept open and.  
12b2e 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20      ** possibly 
12b2f 75 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e 73  used for a trans
12b30 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e  action later on.
12b31 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73   On some systems
12b32 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f  , the.      ** O
12b33 73 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c  sTruncate() call
12b34 20 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 69   used in exclusi
12b35 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 61  ve-access mode a
12b36 6c 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 20  lso requires.   
12b37 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72 69     ** a read/wri
12b38 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a  te file handle..
12b39 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
12b3a 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  f( !isOpen(pPage
12b3b 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20  r->jfd) ){.     
12b3c 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
12b3d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12b3e 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 50  OsAccess(pVfs,pP
12b3f 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53  ager->zJournal,S
12b40 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
12b41 53 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 20  STS,&res);.     
12b42 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
12b43 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
12b44 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
12b45 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75           int fou
12b46 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  t = 0;.         
12b47 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54     int f = SQLIT
12b48 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
12b49 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  |SQLITE_OPEN_MAI
12b4a 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20  N_JOURNAL;.     
12b4b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
12b4c 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
12b4d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
12b4e 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
12b4f 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d  en(pVfs, pPager-
12b50 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65  >zJournal, pPage
12b51 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74  r->jfd, f, &fout
12b52 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  );.            a
12b53 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
12b54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70  E_OK || isOpen(p
12b55 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20  Pager->jfd) );. 
12b56 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72             if( r
12b57 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
12b58 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e  fout&SQLITE_OPEN
12b59 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20  _READONLY ){.   
12b5a 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
12b5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
12b5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
12b5d 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
12b5e 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20  ager->jfd);.    
12b5f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12b60 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12b61 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
12b62 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f   journal does no
12b63 74 20 65 78 69 73 74 2c 20 74 68 61 74 20 6d 65  t exist, that me
12b64 61 6e 73 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  ans some other p
12b65 72 6f 63 65 73 73 0a 20 20 20 20 20 20 20 20 20  rocess.         
12b66 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64     ** has alread
12b67 79 20 72 6f 6c 6c 65 64 20 69 74 20 62 61 63 6b  y rolled it back
12b68 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
12b69 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
12b6a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
12b6b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
12b6c 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
12b6d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12b6e 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
12b6f 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
12b70 2a 20 54 4f 44 4f 3a 20 57 68 79 20 61 72 65 20  * TODO: Why are 
12b71 74 68 65 73 65 20 63 6c 65 61 72 65 64 20 68 65  these cleared he
12b72 72 65 3f 20 49 73 20 69 74 20 6e 65 63 65 73 73  re? Is it necess
12b73 61 72 79 3f 20 2a 2f 0a 20 20 20 20 20 20 70 50  ary? */.      pP
12b74 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
12b75 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  rted = 0;.      
12b76 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12b77 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ff = 0;.      pP
12b78 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20  ager->setMaster 
12b79 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65  = 0;.      pPage
12b7a 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20  r->journalHdr = 
12b7b 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c  0;. .      /* Pl
12b7c 61 79 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74  ayback and delet
12b7d 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  e the journal.  
12b7e 44 72 6f 70 20 74 68 65 20 64 61 74 61 62 61 73  Drop the databas
12b7f 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a  e write.      **
12b80 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75   lock and reacqu
12b81 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63  ire the read loc
12b82 6b 2e 20 50 75 72 67 65 20 74 68 65 20 63 61 63  k. Purge the cac
12b83 68 65 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20  he before.      
12b84 2a 2a 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20  ** playing back 
12b85 74 68 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  the hot-journal 
12b86 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74  so that we don't
12b87 20 65 6e 64 20 75 70 20 77 69 74 68 0a 20 20 20   end up with.   
12b88 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69     ** an inconsi
12b89 73 74 65 6e 74 20 63 61 63 68 65 2e 0a 20 20 20  stent cache..   
12b8a 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d     */.      rc =
12b8b 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
12b8c 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20  pPager, 1);.    
12b8d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12b8e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
12b8f 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28  c = pager_error(
12b90 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20  pPager, rc);.   
12b91 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
12b92 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
12b93 61 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d  assert( (pPager-
12b94 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48  >state==PAGER_SH
12b95 41 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20  ARED).          
12b96 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63   || (pPager->exc
12b97 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50  lusiveMode && pP
12b98 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45  ager->state>PAGE
12b99 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20  R_SHARED).      
12b9a 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
12b9b 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50  ( sqlite3PcacheP
12b9c 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d  agecount(pPager-
12b9d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b 0a 20  >pPCache)>0 ){. 
12b9e 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61 72       /* The shar
12b9f 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 73 74  ed-lock has just
12ba0 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 20 6f   been acquired o
12ba1 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
12ba2 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64  ile.      ** and
12ba3 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 65 61   there are alrea
12ba4 64 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  dy pages in the 
12ba5 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20 70 72  cache (from a pr
12ba6 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a 2a 20  evious.      ** 
12ba7 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72  read or write tr
12ba8 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 68 65  ansaction).  Che
12ba9 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
12baa 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20   database.      
12bab 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69  ** has been modi
12bac 66 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61  fied.  If the da
12bad 74 61 62 61 73 65 20 68 61 73 20 63 68 61 6e 67  tabase has chang
12bae 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a 20 20  ed, flush the.  
12baf 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 20      ** cache..  
12bb0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
12bb1 44 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73  Database changes
12bb2 20 69 73 20 64 65 74 65 63 74 65 64 20 62 79 20   is detected by 
12bb3 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 62 79  looking at 15 by
12bb4 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20  tes beginning.  
12bb5 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 65 74      ** at offset
12bb6 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66 69 6c   24 into the fil
12bb7 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 34 20  e.  The first 4 
12bb8 6f 66 20 74 68 65 73 65 20 31 36 20 62 79 74 65  of these 16 byte
12bb9 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61  s are.      ** a
12bba 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 72 20   32-bit counter 
12bbb 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e  that is incremen
12bbc 74 65 64 20 77 69 74 68 20 65 61 63 68 20 63 68  ted with each ch
12bbd 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 20 20  ange.  The.     
12bbe 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20   ** other bytes 
12bbf 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c 79 20  change randomly 
12bc0 77 69 74 68 20 65 61 63 68 20 66 69 6c 65 20 63  with each file c
12bc1 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 20 20  hange when.     
12bc2 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 20 69   ** a codec is i
12bc3 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a 2a 20  n use..      ** 
12bc4 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20  .      ** There 
12bc5 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 6c 79  is a vanishingly
12bc6 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 68   small chance th
12bc7 61 74 20 61 20 63 68 61 6e 67 65 20 77 69 6c 6c  at a change will
12bc8 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 20 2a   not be .      *
12bc9 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 68 65  * detected.  The
12bca 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 75 6e   chance of an un
12bcb 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 65 20  detected change 
12bcc 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74  is so small that
12bcd 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e  .      ** it can
12bce 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e 0a 20   be neglected.. 
12bcf 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 68       */.      ch
12bd0 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b 73 69  ar dbFileVers[si
12bd1 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46  zeof(pPager->dbF
12bd2 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20 20 20  ileVers)];.     
12bd3 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12bd4 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30  ecount(pPager, 0
12bd5 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 50  );..      if( pP
12bd6 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b  ager->errCode ){
12bd7 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 50  .        rc = pP
12bd8 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
12bd9 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c         goto fail
12bda 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ed;.      }..   
12bdb 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
12bdc 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29  r->dbSizeValid )
12bdd 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  ;.      if( pPag
12bde 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a  er->dbSize>0 ){.
12bdf 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28          IOTRACE(
12be0 28 22 43 4b 56 45 52 53 20 25 70 20 25 64 5c 6e  ("CKVERS %p %d\n
12be1 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a 65 6f  ", pPager, sizeo
12be2 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 29 3b  f(dbFileVers)));
12be3 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
12be4 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
12be5 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56  er->fd, &dbFileV
12be6 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69  ers, sizeof(dbFi
12be7 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 20  leVers), 24);.  
12be8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
12be9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12bea 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
12beb 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
12bec 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
12bed 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 56 65   memset(dbFileVe
12bee 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 62  rs, 0, sizeof(db
12bef 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20  FileVers));.    
12bf0 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6d    }..      if( m
12bf1 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e 64 62  emcmp(pPager->db
12bf2 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65  FileVers, dbFile
12bf3 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46  Vers, sizeof(dbF
12bf4 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29 7b 0a  ileVers))!=0 ){.
12bf5 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65          pager_re
12bf6 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20  set(pPager);.   
12bf7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
12bf8 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65  ssert( pPager->e
12bf9 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20  xclusiveMode || 
12bfa 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
12bfb 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20  AGER_SHARED );. 
12bfc 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 20 69   }.. failed:.  i
12bfd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12bfe 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72   ){.    /* pager
12bff 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e  _unlock() is a n
12c00 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 73 69  o-op for exclusi
12c01 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e 2d 6d  ve mode and in-m
12c02 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 2e  emory databases.
12c03 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 75 6e   */.    pager_un
12c04 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  lock(pPager);.  
12c05 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
12c06 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ../*.** If the r
12c07 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 68  eference count h
12c08 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f 2c  as reached zero,
12c09 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63   rollback any ac
12c0a 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74  tive.** transact
12c0b 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74  ion and unlock t
12c0c 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20  he pager..**.** 
12c0d 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 6b 69  Except, in locki
12c0e 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56  ng_mode=EXCLUSIV
12c0f 45 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  E when there is 
12c10 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a 2a 2a  nothing to in.**
12c11 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
12c12 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c 6f 63  urnal, the unloc
12c13 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f 72 6d  k is not perform
12c14 65 64 20 61 6e 64 20 74 68 65 72 65 20 69 73 0a  ed and there is.
12c15 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 6f  ** nothing to ro
12c16 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 73 20  llback, so this 
12c17 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
12c18 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76  op..*/ .static v
12c19 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49  oid pagerUnlockI
12c1a 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a 70  fUnused(Pager *p
12c1b 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 28 73  Pager){.  if( (s
12c1c 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
12c1d 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
12c1e 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26 26 20  ache)==0).   && 
12c1f 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73  (!pPager->exclus
12c20 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65  iveMode || pPage
12c21 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29  r->journalOff>0)
12c22 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67 65 72   .  ){.    pager
12c23 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63  UnlockAndRollbac
12c24 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d  k(pPager);.  }.}
12c25 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 70  ../*.** Drop a p
12c26 61 67 65 20 66 72 6f 6d 20 74 68 65 20 63 61 63  age from the cac
12c27 68 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  he using sqlite3
12c28 50 63 61 63 68 65 44 72 6f 70 28 29 2e 0a 2a 2a  PcacheDrop()..**
12c29 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d 65 61 6e  .** If this mean
12c2a 73 20 74 68 65 72 65 20 61 72 65 20 6e 6f 77 20  s there are now 
12c2b 6e 6f 20 70 61 67 65 73 20 77 69 74 68 20 72 65  no pages with re
12c2c 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 6d  ferences to them
12c2d 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20  , a rollback.** 
12c2e 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 20 6c  occurs and the l
12c2f 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
12c30 61 73 65 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a  ase is removed..
12c31 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
12c32 61 67 65 72 44 72 6f 70 50 61 67 65 28 44 62 50  agerDropPage(DbP
12c33 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 50 61 67  age *pPg){.  Pag
12c34 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
12c35 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 71 6c 69  ->pPager;.  sqli
12c36 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50  te3PcacheDrop(pP
12c37 67 29 3b 0a 20 20 70 61 67 65 72 55 6e 6c 6f 63  g);.  pagerUnloc
12c38 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72  kIfUnused(pPager
12c39 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75  );.}../*.** Acqu
12c3a 69 72 65 20 61 20 72 65 66 65 72 65 6e 63 65 20  ire a reference 
12c3b 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70  to page number p
12c3c 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70 50 61  gno in pager pPa
12c3d 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a 20 72  ger (a page.** r
12c3e 65 66 65 72 65 6e 63 65 20 68 61 73 20 74 79 70  eference has typ
12c3f 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66 20 74  e DbPage*). If t
12c40 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 66  he requested ref
12c41 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75  erence is .** su
12c42 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 69  ccessfully obtai
12c43 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70 69 65  ned, it is copie
12c44 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61 6e 64  d to *ppPage and
12c45 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
12c46 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ned..**.** This 
12c47 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 70  function calls p
12c48 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29  agerSharedLock()
12c49 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41   to obtain a SHA
12c4a 52 45 44 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 74  RED lock on.** t
12c4b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12c4c 20 69 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   if such a lock 
12c4d 6f 72 20 67 72 65 61 74 65 72 20 69 73 20 6e 6f  or greater is no
12c4e 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a  t already held..
12c4f 2a 2a 20 54 68 69 73 20 6d 61 79 20 63 61 75 73  ** This may caus
12c50 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f  e hot-journal ro
12c51 6c 6c 62 61 63 6b 20 6f 72 20 61 20 63 61 63 68  llback or a cach
12c52 65 20 70 75 72 67 65 2e 20 53 65 65 20 63 6f 6d  e purge. See com
12c53 6d 65 6e 74 73 0a 2a 2a 20 61 62 6f 76 65 20 66  ments.** above f
12c54 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 53 68 61  unction pagerSha
12c55 72 65 64 4c 6f 63 6b 28 29 20 66 6f 72 20 64 65  redLock() for de
12c56 74 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  tails..**.** If 
12c57 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
12c58 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ge is already in
12c59 20 74 68 65 20 63 61 63 68 65 2c 20 69 74 20 69   the cache, it i
12c5a 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20  s returned. .** 
12c5b 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77  Otherwise, a new
12c5c 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20   page object is 
12c5d 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f  allocated and po
12c5e 70 75 6c 61 74 65 64 20 77 69 74 68 20 64 61 74  pulated with dat
12c5f 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74  a.** read from t
12c60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12c61 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c  . In some cases,
12c62 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75   the pcache modu
12c63 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65  le may.** choose
12c64 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65   not to allocate
12c65 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65   a new page obje
12c66 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65  ct and may reuse
12c67 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20   an existing.** 
12c68 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f  object with no o
12c69 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
12c6a 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ences..**.** The
12c6b 20 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65   extra data appe
12c6c 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69  nded to a page i
12c6d 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c  s always initial
12c6e 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68  ized to zeros th
12c6f 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65  e .** first time
12c70 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65   a page is loade
12c71 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49  d into memory. I
12c72 66 20 74 68 65 20 70 61 67 65 20 72 65 71 75 65  f the page reque
12c73 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65  sted is .** alre
12c74 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65  ady in the cache
12c75 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
12c76 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  ion is called, t
12c77 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a  hen the extra.**
12c78 20 64 61 74 61 20 69 73 20 6c 65 66 74 20 61 73   data is left as
12c79 20 69 74 20 77 61 73 20 77 68 65 6e 20 74 68 65   it was when the
12c7a 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73   page object was
12c7b 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a   last used..**.*
12c7c 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
12c7d 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c  e image is small
12c7e 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75  er than the requ
12c7f 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 69 66  ested page or if
12c80 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20   a .** non-zero 
12c81 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20  value is passed 
12c82 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74  as the noContent
12c83 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74   parameter and t
12c84 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  he .** requested
12c85 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
12c86 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74  eady stored in t
12c87 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e  he cache, then n
12c88 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73  o .** actual dis
12c89 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49  k read occurs. I
12c8a 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
12c8b 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20  memory image of 
12c8c 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20  the .** page is 
12c8d 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61  initialized to a
12c8e 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a  ll zeros. .**.**
12c8f 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73   If noContent is
12c90 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20   true, it means 
12c91 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63  that we do not c
12c92 61 72 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f  are about the co
12c93 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65  ntents.** of the
12c94 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75   page. This occu
12c95 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61  rs in two sepera
12c96 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a  te scenarios:.**
12c97 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65  .**   a) When re
12c98 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73  ading a free-lis
12c99 74 20 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d  t leaf page from
12c9a 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 61   the database, a
12c9b 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68  nd.**.**   b) Wh
12c9c 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69  en a savepoint i
12c9d 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62  s being rolled b
12c9e 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20  ack and we need 
12c9f 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20  to load.**      
12ca0 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20  a new page into 
12ca1 74 68 65 20 63 61 63 68 65 20 74 6f 20 70 6f 70  the cache to pop
12ca2 75 6c 61 74 65 20 77 69 74 68 20 74 68 65 20 64  ulate with the d
12ca3 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20 20 20  ata read.**     
12ca4 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65 70 6f   from the savepo
12ca5 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a  int journal..**.
12ca6 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20  ** If noContent 
12ca7 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
12ca8 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20  e data returned 
12ca9 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 61  is zeroed instea
12caa 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 65  d of.** being re
12cab 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ad from the data
12cac 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e 61 6c  base. Additional
12cad 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 6f 72  ly, the bits cor
12cae 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f  responding.** to
12caf 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e 70   pgno in Pager.p
12cb0 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76 65  InJournal (bitve
12cb1 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 61  c of pages alrea
12cb2 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  dy written to th
12cb3 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  e.** journal fil
12cb4 65 29 20 61 6e 64 20 74 68 65 20 50 61 67 65 72  e) and the Pager
12cb5 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76  Savepoint.pInSav
12cb6 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 20 6f  epoint bitvecs o
12cb7 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73 61  f any open.** sa
12cb8 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 65 74  vepoints are set
12cb9 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20  . This means if 
12cba 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 64 65  the page is made
12cbb 20 77 72 69 74 61 62 6c 65 20 61 74 20 61 6e 79   writable at any
12cbc 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  .** point in the
12cbd 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67 20 61   future, using a
12cbe 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
12cbf 50 61 67 65 72 57 72 69 74 65 28 29 2c 20 69 74  PagerWrite(), it
12cc0 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77 69  s contents.** wi
12cc1 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e 61  ll not be journa
12cc2 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65 73 20  led. This saves 
12cc3 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63  IO..**.** The ac
12cc4 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 20  quisition might 
12cc5 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 61 6c  fail for several
12cc6 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c   reasons.  In al
12cc7 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61  l cases,.** an a
12cc8 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
12cc9 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
12cca 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 69 73  d and *ppPage is
12ccb 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a   set to NULL..**
12ccc 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c  .** See also sql
12ccd 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
12cce 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 72 6f  ).  Both this ro
12ccf 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70  utine and Lookup
12cd0 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f  () attempt.** to
12cd1 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20   find a page in 
12cd2 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61  the in-memory ca
12cd3 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 74  che first.  If t
12cd4 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
12cd5 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d  lready.** in mem
12cd6 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ory, this routin
12cd7 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 74  e goes to disk t
12cd8 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 68 65  o read it in whe
12cd9 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a  reas Lookup().**
12cda 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e   just returns 0.
12cdb 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61    This routine a
12cdc 63 71 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c  cquires a read-l
12cdd 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 69  ock the first ti
12cde 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20  me it.** has to 
12cdf 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20  go to disk, and 
12ce0 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62  could also playb
12ce1 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e  ack an old journ
12ce2 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  al if necessary.
12ce3 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70  .** Since Lookup
12ce4 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f  () never goes to
12ce5 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20   disk, it never 
12ce6 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68  has to deal with
12ce7 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75   locks.** or jou
12ce8 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53  rnal files..*/.S
12ce9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
12cea 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63  t sqlite3PagerAc
12ceb 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20 2a  quire(.  Pager *
12cec 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a 20  pPager,      /* 
12ced 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20 6f  The pager open o
12cee 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
12cef 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ile */.  Pgno pg
12cf0 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  no,          /* 
12cf1 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 66  Page number to f
12cf2 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 65  etch */.  DbPage
12cf3 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a   **ppPage,    /*
12cf4 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72   Write a pointer
12cf5 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 72   to the page her
12cf6 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e  e */.  int noCon
12cf7 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 6f  tent       /* Do
12cf8 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61 64   not bother read
12cf9 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d  ing content from
12cfa 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a 2f   disk if true */
12cfb 0a 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  .){.  PgHdr *pPg
12cfc 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   = 0;.  int rc;.
12cfd 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 65 72  .  assert( asser
12cfe 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 50  t_pager_state(pP
12cff 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 72  ager) );.  asser
12d00 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
12d01 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 0a  ==PAGER_UNLOCK .
12d02 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
12d03 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
12d04 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
12d05 3e 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 70 67  >0 .       || pg
12d06 6e 6f 3d 3d 31 0a 20 20 29 3b 0a 0a 20 20 2f 2a  no==1.  );..  /*
12d07 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   The maximum pag
12d08 65 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33 31  e number is 2^31
12d09 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  . Return SQLITE_
12d0a 43 4f 52 52 55 50 54 20 69 66 20 61 20 70 61 67  CORRUPT if a pag
12d0b 65 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 72  e.  ** number gr
12d0c 65 61 74 65 72 20 74 68 61 6e 20 74 68 69 73 2c  eater than this,
12d0d 20 6f 72 20 7a 65 72 6f 2c 20 69 73 20 72 65 71   or zero, is req
12d0e 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  uested..  */.  i
12d0f 66 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41  f( pgno>PAGER_MA
12d10 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d  X_PGNO || pgno==
12d11 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52  0 || pgno==PAGER
12d12 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29  _MJ_PGNO(pPager)
12d13 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
12d14 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
12d15 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  PT;.  }..  /* Ma
12d16 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65 20  ke sure we have 
12d17 6e 6f 74 20 68 69 74 20 61 6e 79 20 63 72 69 74  not hit any crit
12d18 69 63 61 6c 20 65 72 72 6f 72 73 2e 0a 20 20 2a  ical errors..  *
12d19 2f 20 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  / .  assert( pPa
12d1a 67 65 72 21 3d 30 20 29 3b 0a 20 20 2a 70 70 50  ger!=0 );.  *ppP
12d1b 61 67 65 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 49  age = 0;..  /* I
12d1c 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  f this is the fi
12d1d 72 73 74 20 70 61 67 65 20 61 63 63 65 73 73 65  rst page accesse
12d1e 64 2c 20 74 68 65 6e 20 67 65 74 20 61 20 53 48  d, then get a SH
12d1f 41 52 45 44 20 6c 6f 63 6b 0a 20 20 2a 2a 20 6f  ARED lock.  ** o
12d20 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
12d21 69 6c 65 2e 20 70 61 67 65 72 53 68 61 72 65 64  ile. pagerShared
12d22 4c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f  Lock() is a no-o
12d23 70 20 69 66 20 0a 20 20 2a 2a 20 61 20 64 61 74  p if .  ** a dat
12d24 61 62 61 73 65 20 6c 6f 63 6b 20 69 73 20 61 6c  abase lock is al
12d25 72 65 61 64 79 20 68 65 6c 64 2e 0a 20 20 2a 2f  ready held..  */
12d26 0a 20 20 72 63 20 3d 20 70 61 67 65 72 53 68 61  .  rc = pagerSha
12d27 72 65 64 4c 6f 63 6b 28 70 50 61 67 65 72 29 3b  redLock(pPager);
12d28 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
12d29 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
12d2a 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
12d2b 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
12d2c 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  te!=PAGER_UNLOCK
12d2d 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69   );..  rc = sqli
12d2e 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 70  te3PcacheFetch(p
12d2f 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20  Pager->pPCache, 
12d30 70 67 6e 6f 2c 20 31 2c 20 26 70 50 67 29 3b 0a  pgno, 1, &pPg);.
12d31 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12d32 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
12d33 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65  n rc;.  }.  asse
12d34 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 3d 3d 70  rt( pPg->pgno==p
12d35 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28  gno );.  assert(
12d36 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50   pPg->pPager==pP
12d37 61 67 65 72 20 7c 7c 20 70 50 67 2d 3e 70 50 61  ager || pPg->pPa
12d38 67 65 72 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  ger==0 );.  if( 
12d39 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29  pPg->pPager==0 )
12d3a 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67  {.    /* The pag
12d3b 65 72 20 63 61 63 68 65 20 68 61 73 20 63 72 65  er cache has cre
12d3c 61 74 65 64 20 61 20 6e 65 77 20 70 61 67 65 2e  ated a new page.
12d3d 20 49 74 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65   Its content nee
12d3e 64 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65  ds to .    ** be
12d3f 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20   initialized..  
12d40 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d 61    */.    int nMa
12d41 78 3b 0a 20 20 20 20 50 41 47 45 52 5f 49 4e 43  x;.    PAGER_INC
12d42 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 29  R(pPager->nMiss)
12d43 3b 0a 20 20 20 20 70 50 67 2d 3e 70 50 61 67 65  ;.    pPg->pPage
12d44 72 20 3d 20 70 50 61 67 65 72 3b 0a 0a 20 20 20  r = pPager;..   
12d45 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
12d46 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
12d47 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20  er, &nMax);.    
12d48 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12d49 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
12d4a 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67  e3PagerUnref(pPg
12d4b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
12d4c 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  rc;.    }..    i
12d4d 66 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e  f( nMax<(int)pgn
12d4e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f  o || MEMDB || no
12d4f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20  Content ){.     
12d50 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72   if( pgno>pPager
12d51 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20  ->mxPgno ){.    
12d52 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
12d53 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20  Unref(pPg);.    
12d54 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
12d55 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a  E_FULL;.      }.
12d56 20 20 20 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74        if( noCont
12d57 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ent ){.        /
12d58 2a 20 46 61 69 6c 75 72 65 20 74 6f 20 73 65 74  * Failure to set
12d59 20 74 68 65 20 62 69 74 73 20 69 6e 20 74 68 65   the bits in the
12d5a 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76   InJournal bit-v
12d5b 65 63 74 6f 72 73 20 69 73 20 62 65 6e 69 67 6e  ectors is benign
12d5c 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20  ..        ** It 
12d5d 6d 65 72 65 6c 79 20 6d 65 61 6e 73 20 74 68 61  merely means tha
12d5e 74 20 77 65 20 6d 69 67 68 74 20 64 6f 20 73 6f  t we might do so
12d5f 6d 65 20 65 78 74 72 61 20 77 6f 72 6b 20 74 6f  me extra work to
12d60 20 6a 6f 75 72 6e 61 6c 20 61 20 0a 20 20 20 20   journal a .    
12d61 20 20 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74      ** page that
12d62 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
12d63 6f 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20  o be journaled. 
12d64 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 62   Nevertheless, b
12d65 65 20 73 75 72 65 20 0a 20 20 20 20 20 20 20 20  e sure .        
12d66 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 63  ** to test the c
12d67 61 73 65 20 77 68 65 72 65 20 61 20 6d 61 6c 6c  ase where a mall
12d68 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  oc error occurs 
12d69 77 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20  while trying to 
12d6a 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  set .        ** 
12d6b 61 20 62 69 74 20 69 6e 20 61 20 62 69 74 20 76  a bit in a bit v
12d6c 65 63 74 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a  ector..        *
12d6d 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
12d6e 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
12d6f 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 69 66  oc();.        if
12d70 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e  ( pgno<=pPager->
12d71 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20  dbOrigSize ){.  
12d72 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59          TESTONLY
12d73 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65 33  ( rc = ) sqlite3
12d74 42 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72  BitvecSet(pPager
12d75 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67  ->pInJournal, pg
12d76 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74  no);.          t
12d77 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c  estcase( rc==SQL
12d78 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20  ITE_NOMEM );.   
12d79 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 54       }.        T
12d7a 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20  ESTONLY( rc = ) 
12d7b 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69  addToSavepointBi
12d7c 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 67  tvecs(pPager, pg
12d7d 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  no);.        tes
12d7e 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54  tcase( rc==SQLIT
12d7f 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
12d80 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e     sqlite3EndBen
12d81 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20  ignMalloc();.   
12d82 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
12d83 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44    memset(pPg->pD
12d84 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e  ata, 0, pPager->
12d85 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20  pageSize);.     
12d86 20 7d 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45   }.      IOTRACE
12d87 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22  (("ZERO %p %d\n"
12d88 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29  , pPager, pgno))
12d89 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
12d8a 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e     assert( pPg->
12d8b 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29  pPager==pPager )
12d8c 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 61  ;.      rc = rea
12d8d 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a 20 20  dDbPage(pPg);.  
12d8e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12d8f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
12d90 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 28 70   pagerDropPage(p
12d91 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  Pg);.        ret
12d92 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
12d93 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c      }.#ifdef SQL
12d94 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
12d95 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73      pPg->pageHas
12d96 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61  h = pager_pageha
12d97 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a  sh(pPg);.#endif.
12d98 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
12d99 54 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  The requested pa
12d9a 67 65 20 69 73 20 69 6e 20 74 68 65 20 70 61 67  ge is in the pag
12d9b 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20  e cache. */.    
12d9c 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65  PAGER_INCR(pPage
12d9d 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 7d 0a 0a 20  r->nHit);.  }.. 
12d9e 20 2a 70 70 50 61 67 65 20 3d 20 70 50 67 3b 0a   *ppPage = pPg;.
12d9f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12da0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71  OK;.}../*.** Acq
12da1 75 69 72 65 20 61 20 70 61 67 65 20 69 66 20 69  uire a page if i
12da2 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  t is already in 
12da3 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61  the in-memory ca
12da4 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20  che.  Do.** not 
12da5 72 65 61 64 20 74 68 65 20 70 61 67 65 20 66 72  read the page fr
12da6 6f 6d 20 64 69 73 6b 2e 20 20 52 65 74 75 72 6e  om disk.  Return
12da7 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
12da8 65 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 30 20  e page,.** or 0 
12da9 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  if the page is n
12daa 6f 74 20 69 6e 20 63 61 63 68 65 2e 20 41 6c 73  ot in cache. Als
12dab 6f 2c 20 72 65 74 75 72 6e 20 30 20 69 66 20 74  o, return 0 if t
12dac 68 65 20 0a 2a 2a 20 70 61 67 65 72 20 69 73 20  he .** pager is 
12dad 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  in PAGER_UNLOCK 
12dae 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20  state when this 
12daf 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
12db0 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74 68 65  ed,.** or if the
12db1 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20   pager is in an 
12db2 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65  error state othe
12db3 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55  r than SQLITE_FU
12db4 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  LL..**.** See al
12db5 73 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  so sqlite3PagerG
12db6 65 74 28 29 2e 20 20 54 68 65 20 64 69 66 66 65  et().  The diffe
12db7 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68  rence between th
12db8 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e  is routine.** an
12db9 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  d sqlite3PagerGe
12dba 74 28 29 20 69 73 20 74 68 61 74 20 5f 67 65 74  t() is that _get
12dbb 28 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68  () will go to th
12dbc 65 20 64 69 73 6b 20 61 6e 64 20 72 65 61 64 0a  e disk and read.
12dbd 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 69  ** in the page i
12dbe 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  f the page is no
12dbf 74 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63  t already in cac
12dc0 68 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  he.  This routin
12dc1 65 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c  e.** returns NUL
12dc2 4c 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73  L if the page is
12dc3 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72   not in cache or
12dc4 20 69 66 20 61 20 64 69 73 6b 20 49 2f 4f 20 65   if a disk I/O e
12dc5 72 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65 76 65  rror .** has eve
12dc6 72 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53  r happened..*/.S
12dc7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 44 62  QLITE_PRIVATE Db
12dc8 50 61 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67  Page *sqlite3Pag
12dc9 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a  erLookup(Pager *
12dca 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e  pPager, Pgno pgn
12dcb 6f 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67  o){.  PgHdr *pPg
12dcc 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
12dcd 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61  pPager!=0 );.  a
12dce 73 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 29  ssert( pgno!=0 )
12dcf 3b 0a 0a 20 20 69 66 28 20 28 70 50 61 67 65 72  ;..  if( (pPager
12dd0 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55  ->state!=PAGER_U
12dd1 4e 4c 4f 43 4b 29 0a 20 20 20 26 26 20 28 70 50  NLOCK).   && (pP
12dd2 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53  ager->errCode==S
12dd3 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
12dd4 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c  er->errCode==SQL
12dd5 49 54 45 5f 46 55 4c 4c 29 0a 20 20 29 7b 0a 20  ITE_FULL).  ){. 
12dd6 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
12dd7 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50  Fetch(pPager->pP
12dd8 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20  Cache, pgno, 0, 
12dd9 26 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  &pPg);.  }..  re
12dda 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a  turn pPg;.}../*.
12ddb 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 61 67  ** Release a pag
12ddc 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a  e reference..**.
12ddd 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72  ** If the number
12dde 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
12ddf 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f 70 20  o the page drop 
12de0 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  to zero, then th
12de1 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64 64  e.** page is add
12de2 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c 69  ed to the LRU li
12de3 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72 65  st.  When all re
12de4 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c 20  ferences to all 
12de5 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 65 6c  pages.** are rel
12de6 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61 63  eased, a rollbac
12de7 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65  k occurs and the
12de8 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
12de9 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d 6f  abase is.** remo
12dea 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ved..*/.SQLITE_P
12deb 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
12dec 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 62  te3PagerUnref(Db
12ded 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 69 66  Page *pPg){.  if
12dee 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 61 67  ( pPg ){.    Pag
12def 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
12df0 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 73 71  ->pPager;.    sq
12df1 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61  lite3PcacheRelea
12df2 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 61 67  se(pPg);.    pag
12df3 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64  erUnlockIfUnused
12df4 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a  (pPager);.  }.}.
12df5 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 61  ./*.** If the ma
12df6 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  in journal file 
12df7 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
12df8 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 72 65 20   opened, ensure 
12df9 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 75 62 2d  that the.** sub-
12dfa 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
12dfb 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 74 68 65  open too. If the
12dfc 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73   main journal is
12dfd 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 74 68   not open,.** th
12dfe 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
12dff 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51   no-op..**.** SQ
12e00 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
12e01 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e  ned if everythin
12e02 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69 6e 67  g goes according
12e03 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 41 6e   to plan. .** An
12e04 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58   SQLITE_IOERR_XX
12e05 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  X error code is 
12e06 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 63 61  returned if a ca
12e07 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65  ll to .** sqlite
12e08 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 6c 73 2e  3OsOpen() fails.
12e09 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
12e0a 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 50 61  penSubJournal(Pa
12e0b 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
12e0c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
12e0d 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e  OK;.  if( isOpen
12e0e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26  (pPager->jfd) &&
12e0f 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d   !isOpen(pPager-
12e10 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 66  >sjfd) ){.    if
12e11 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
12e12 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
12e13 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
12e14 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49  || pPager->subjI
12e15 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20 20  nMemory ){.     
12e16 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e   sqlite3MemJourn
12e17 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73  alOpen(pPager->s
12e18 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  jfd);.    }else{
12e19 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
12e1a 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 72  rOpentemp(pPager
12e1b 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20  , pPager->sjfd, 
12e1c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a  SQLITE_OPEN_SUBJ
12e1d 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a 20  OURNAL);.    }. 
12e1e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
12e1f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
12e20 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
12e21 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
12e22 20 65 76 65 72 79 20 77 72 69 74 65 20 74 72 61   every write tra
12e23 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65  nsaction..** The
12e24 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20  re must already 
12e25 62 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  be a RESERVED or
12e26 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
12e27 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
12e28 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 68  .** file when th
12e29 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
12e2a 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e  lled..**.** Open
12e2b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12e2c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 67  e for pager pPag
12e2d 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20 6a  er and write a j
12e2e 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a  ournal header.**
12e2f 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
12e30 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61 72   it. If there ar
12e31 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69  e active savepoi
12e32 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73 75  nts, open the su
12e33 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73 20  b-journal.** as 
12e34 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 74  well. This funct
12e35 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ion is only used
12e36 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   when the journa
12e37 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67 20  l file is being 
12e38 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77 72  .** opened to wr
12e39 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6c  ite a rollback l
12e3a 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63  og for a transac
12e3b 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74 20  tion. It is not 
12e3c 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f 70  used .** when op
12e3d 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75 72  ening a hot jour
12e3e 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c 6c  nal file to roll
12e3f 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20   it back..**.** 
12e40 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  If the journal f
12e41 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f  ile is already o
12e42 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 20 62  pen (as it may b
12e43 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  e in exclusive m
12e44 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ode),.** then th
12e45 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73 74  is function just
12e46 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 6e 61   writes a journa
12e47 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20  l header to the 
12e48 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20  start of the.** 
12e49 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 69 6c  already open fil
12e4a 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65  e. .**.** Whethe
12e4b 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75  r or not the jou
12e4c 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65  rnal file is ope
12e4d 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63  ned by this func
12e4e 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61 67  tion, the.** Pag
12e4f 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69  er.pInJournal bi
12e50 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20 69  tvec structure i
12e51 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a  s allocated..**.
12e52 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
12e53 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e  _OK if everythin
12e54 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e  g is successful.
12e55 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
12e56 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f  rn .** SQLITE_NO
12e57 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 65 6d  MEM if the attem
12e58 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 50  pt to allocate P
12e59 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ager.pInJournal 
12e5a 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61 6e  fails, or .** an
12e5b 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
12e5c 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 69  f opening or wri
12e5d 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ting the journal
12e5e 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f 0a   file fails..*/.
12e5f 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
12e60 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61  _open_journal(Pa
12e61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
12e62 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
12e63 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
12e64 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
12e65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73  turn code */.  s
12e66 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e  qlite3_vfs * con
12e67 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72  st pVfs = pPager
12e68 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f 63  ->pVfs;   /* Loc
12e69 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 73 20  al cache of vfs 
12e6a 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73  pointer */..  as
12e6b 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
12e6c 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52  ate>=PAGER_RESER
12e6d 56 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28  VED );.  assert(
12e6e 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72   pPager->useJour
12e6f 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nal );.  assert(
12e70 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
12e71 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f  nal==0 );.  .  /
12e72 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69 6e 20  * If already in 
12e73 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c  the error state,
12e74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
12e75 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20  s a no-op. */.  
12e76 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
12e77 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ode ){.    retur
12e78 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  n pPager->errCod
12e79 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 4f 44  e;.  }..  /* TOD
12e7a 4f 3a 20 49 73 20 69 74 20 72 65 61 6c 6c 79 20  O: Is it really 
12e7b 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 74 20  possible to get 
12e7c 68 65 72 65 20 77 69 74 68 20 64 62 53 69 7a 65  here with dbSize
12e7d 56 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e 6f 74  Valid==0? If not
12e7e 2c 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20  ,.  ** the call 
12e7f 74 6f 20 50 61 67 65 72 50 61 67 65 63 6f 75 6e  to PagerPagecoun
12e80 74 28 29 20 63 61 6e 20 62 65 20 72 65 6d 6f 76  t() can be remov
12e81 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63  ed..  */.  testc
12e82 61 73 65 28 20 70 50 61 67 65 72 2d 3e 64 62 53  ase( pPager->dbS
12e83 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20  izeValid==0 );. 
12e84 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12e85 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30  ecount(pPager, 0
12e86 29 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e 70 49  );..  pPager->pI
12e87 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74  nJournal = sqlit
12e88 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70  e3BitvecCreate(p
12e89 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a  Pager->dbSize);.
12e8a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49    if( pPager->pI
12e8b 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20  nJournal==0 ){. 
12e8c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
12e8d 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f  _NOMEM;.  }..  /
12e8e 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e  * Open the journ
12e8f 61 6c 20 66 69 6c 65 20 69 66 20 69 74 20 69 73  al file if it is
12e90 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65   not already ope
12e91 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f  n. */.  if( !isO
12e92 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
12e93 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67   ){.    if( pPag
12e94 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
12e95 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
12e96 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20  DE_MEMORY ){.   
12e97 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75     sqlite3MemJou
12e98 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d  rnalOpen(pPager-
12e99 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  >jfd);.    }else
12e9a 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e  {.      const in
12e9b 74 20 66 6c 61 67 73 20 3d 20 20 20 20 20 20 20  t flags =       
12e9c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
12e9d 46 53 20 66 6c 61 67 73 20 74 6f 20 6f 70 65 6e  FS flags to open
12e9e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
12e9f 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  .        SQLITE_
12ea0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53  OPEN_READWRITE|S
12ea1 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
12ea2 45 7c 0a 20 20 20 20 20 20 20 20 28 70 50 61 67  E|.        (pPag
12ea3 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f 20 0a  er->tempFile ? .
12ea4 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
12ea5 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
12ea6 4c 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  LOSE|SQLITE_OPEN
12ea7 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a  _TEMP_JOURNAL):.
12ea8 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
12ea9 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
12eaa 4e 41 4c 29 0a 20 20 20 20 20 20 20 20 29 3b 0a  NAL).        );.
12eab 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
12eac 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
12ead 45 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  E.      rc = sql
12eae 69 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28  ite3JournalOpen(
12eaf 0a 20 20 20 20 20 20 20 20 20 20 70 56 66 73 2c  .          pVfs,
12eb0 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61   pPager->zJourna
12eb1 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  l, pPager->jfd, 
12eb2 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65  flags, jrnlBuffe
12eb3 72 53 69 7a 65 28 70 50 61 67 65 72 29 0a 20 20  rSize(pPager).  
12eb4 20 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20      );.#else.   
12eb5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
12eb6 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67  sOpen(pVfs, pPag
12eb7 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50  er->zJournal, pP
12eb8 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73  ager->jfd, flags
12eb9 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  , 0);.#endif.   
12eba 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72   }.    assert( r
12ebb 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
12ebc 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
12ebd 66 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f  fd) );.  }...  /
12ebe 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 72 73  * Write the firs
12ebf 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  t journal header
12ec0 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
12ec1 66 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20 0a 20  file and open . 
12ec2 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72   ** the sub-jour
12ec3 6e 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79  nal if necessary
12ec4 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ..  */.  if( rc=
12ec5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12ec6 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b    /* TODO: Check
12ec7 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 73 65   if all of these
12ec8 20 61 72 65 20 72 65 61 6c 6c 79 20 72 65 71 75   are really requ
12ec9 69 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70 50 61  ired. */.    pPa
12eca 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
12ecb 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  = pPager->dbSize
12ecc 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  ;.    pPager->jo
12ecd 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30  urnalStarted = 0
12ece 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65  ;.    pPager->ne
12ecf 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20  edSync = 0;.    
12ed0 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30  pPager->nRec = 0
12ed1 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  ;.    pPager->jo
12ed2 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20  urnalOff = 0;.  
12ed3 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73    pPager->setMas
12ed4 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  ter = 0;.    pPa
12ed5 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20  ger->journalHdr 
12ed6 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 77 72  = 0;.    rc = wr
12ed7 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50  iteJournalHdr(pP
12ed8 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28  ager);.  }.  if(
12ed9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
12eda 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  & pPager->nSavep
12edb 6f 69 6e 74 20 29 7b 0a 20 20 20 20 72 63 20 3d  oint ){.    rc =
12edc 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28   openSubJournal(
12edd 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20  pPager);.  }..  
12ede 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12edf 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
12ee0 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50  BitvecDestroy(pP
12ee1 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
12ee2 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70  );.    pPager->p
12ee3 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20  InJournal = 0;. 
12ee4 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
12ee5 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61  }../*.** Begin a
12ee6 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
12ee7 6f 6e 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  on on the specif
12ee8 69 65 64 20 70 61 67 65 72 20 6f 62 6a 65 63 74  ied pager object
12ee9 2e 20 49 66 20 61 20 0a 2a 2a 20 77 72 69 74 65  . If a .** write
12eea 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73  -transaction has
12eeb 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6f 70   already been op
12eec 65 6e 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74  ened, this funct
12eed 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ion is a no-op..
12eee 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 46  **.** If the exF
12eef 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20  lag argument is 
12ef0 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 63 71 75  false, then acqu
12ef1 69 72 65 20 61 74 20 6c 65 61 73 74 20 61 20 52  ire at least a R
12ef2 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20  ESERVED.** lock 
12ef3 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
12ef4 66 69 6c 65 2e 20 49 66 20 65 78 46 6c 61 67 20  file. If exFlag 
12ef5 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 63  is true, then ac
12ef6 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 0a 2a  quire at least.*
12ef7 2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  * an EXCLUSIVE l
12ef8 6f 63 6b 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ock. If such a l
12ef9 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68  ock is already h
12efa 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20  eld, no locking 
12efb 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6e 65  .** functions ne
12efc 65 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ed be called..**
12efd 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 6e  .** If this is n
12efe 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79 20 6f  ot a temporary o
12eff 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65  r in-memory file
12f00 20 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72 6e 61   and, the journa
12f01 6c 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 6f 70  l file is .** op
12f02 65 6e 65 64 20 69 66 20 69 74 20 68 61 73 20 6e  ened if it has n
12f03 6f 74 20 62 65 65 6e 20 61 6c 72 65 61 64 79 2e  ot been already.
12f04 20 46 6f 72 20 61 20 74 65 6d 70 6f 72 61 72 79   For a temporary
12f05 20 66 69 6c 65 2c 20 74 68 65 20 6f 70 65 6e 69   file, the openi
12f06 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f  ng .** of the jo
12f07 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65  urnal file is de
12f08 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65  ferred until the
12f09 72 65 20 69 73 20 61 6e 20 61 63 74 75 61 6c 20  re is an actual 
12f0a 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72 69 74  need to .** writ
12f0b 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e to the journal
12f0c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 68 61 6e 64  . TODO: Why hand
12f0d 6c 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  le temporary fil
12f0e 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 3f 0a  es differently?.
12f0f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75  **.** If the jou
12f10 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65  rnal file is ope
12f11 6e 65 64 20 28 6f 72 20 69 66 20 69 74 20 69 73  ned (or if it is
12f12 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 2c 20   already open), 
12f13 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61  then a.** journa
12f14 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 74  l-header is writ
12f15 74 65 6e 20 74 6f 20 74 68 65 20 73 74 61 72 74  ten to the start
12f16 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66   of it..**.** If
12f17 20 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72   the subjInMemor
12f18 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f  y argument is no
12f19 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79  n-zero, then any
12f1a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65   sub-journal ope
12f1b 6e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68  ned.** within th
12f1c 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  is transaction w
12f1d 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 61 73  ill be opened as
12f1e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69   an in-memory fi
12f1f 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 73 20  le. This.** has 
12f20 6e 6f 20 65 66 66 65 63 74 20 69 66 20 74 68 65  no effect if the
12f21 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20   sub-journal is 
12f22 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 28  already opened (
12f23 61 73 20 69 74 20 6d 61 79 20 62 65 20 77 68 65  as it may be whe
12f24 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 6e 20  n.** running in 
12f25 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 20  exclusive mode) 
12f26 6f 72 20 69 66 20 74 68 65 20 74 72 61 6e 73 61  or if the transa
12f27 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72  ction does not r
12f28 65 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 62 2d  equire a.** sub-
12f29 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 65 20  journal. If the 
12f2a 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67  subjInMemory arg
12f2b 75 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c 20 74  ument is zero, t
12f2c 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64  hen any required
12f2d 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  .** sub-journal 
12f2e 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69  is implemented i
12f2f 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 61 67  n-memory if pPag
12f30 65 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f  er is an in-memo
12f31 72 79 20 64 61 74 61 62 61 73 65 2c 20 0a 2a 2a  ry database, .**
12f32 20 6f 72 20 75 73 69 6e 67 20 61 20 74 65 6d 70   or using a temp
12f33 6f 72 61 72 79 20 66 69 6c 65 20 6f 74 68 65 72  orary file other
12f34 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  wise..*/.SQLITE_
12f35 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
12f36 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 50 61  te3PagerBegin(Pa
12f37 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
12f38 20 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 75 62   exFlag, int sub
12f39 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 69 6e  jInMemory){.  in
12f3a 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
12f3b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
12f3c 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52  er->state!=PAGER
12f3d 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 50 61  _UNLOCK );.  pPa
12f3e 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72  ger->subjInMemor
12f3f 79 20 3d 20 28 75 38 29 73 75 62 6a 49 6e 4d 65  y = (u8)subjInMe
12f40 6d 6f 72 79 3b 0a 20 20 69 66 28 20 70 50 61 67  mory;.  if( pPag
12f41 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
12f42 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20 61  _SHARED ){.    a
12f43 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
12f44 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
12f45 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d      assert( !MEM
12f46 44 42 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74  DB && !pPager->t
12f47 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 20 20  empFile );..    
12f48 2f 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 53 45  /* Obtain a RESE
12f49 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  RVED lock on the
12f4a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
12f4b 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 70 61  If the exFlag pa
12f4c 72 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a 20 69  rameter.    ** i
12f4d 73 20 74 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d  s true, then imm
12f4e 65 64 69 61 74 65 6c 79 20 75 70 67 72 61 64 65  ediately upgrade
12f4f 20 74 68 69 73 20 74 6f 20 61 6e 20 45 58 43 4c   this to an EXCL
12f50 55 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0a  USIVE lock. The.
12f51 20 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64      ** busy-hand
12f52 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e  ler callback can
12f53 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 75 70   be used when up
12f54 67 72 61 64 69 6e 67 20 74 6f 20 74 68 65 20 45  grading to the E
12f55 58 43 4c 55 53 49 56 45 0a 20 20 20 20 2a 2a 20  XCLUSIVE.    ** 
12f56 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68  lock, but not wh
12f57 65 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 65  en obtaining the
12f58 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a   RESERVED lock..
12f59 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
12f5a 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50  sqlite3OsLock(pP
12f5b 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56  ager->fd, RESERV
12f5c 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  ED_LOCK);.    if
12f5d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
12f5e 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
12f5f 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52  >state = PAGER_R
12f60 45 53 45 52 56 45 44 3b 0a 20 20 20 20 20 20 69  ESERVED;.      i
12f61 66 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20  f( exFlag ){.   
12f62 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
12f63 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61  wait_on_lock(pPa
12f64 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c  ger, EXCLUSIVE_L
12f65 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OCK);.      }.  
12f66 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
12f67 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b  he required lock
12f68 73 20 77 65 72 65 20 73 75 63 63 65 73 73 66 75  s were successfu
12f69 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 6f 70  lly obtained, op
12f6a 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  en the journal. 
12f6b 20 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 20 77     ** file and w
12f6c 72 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a  rite the first j
12f6d 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 74 6f  ournal-header to
12f6e 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   it..    */.    
12f6f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12f70 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65  K && pPager->use
12f71 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 26 26 20  Journal.     && 
12f72 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
12f73 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode!=PAGER_JOURN
12f74 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20 20  ALMODE_OFF .    
12f75 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  ){.      rc = pa
12f76 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
12f77 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a  (pPager);.    }.
12f78 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4f 70    }else if( isOp
12f79 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
12f7a 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
12f7b 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20  alOff==0 ){.    
12f7c 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
12f7d 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 77  when the pager w
12f7e 61 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d  as in exclusive-
12f7f 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20  access mode the 
12f80 6c 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65  last.    ** time
12f81 20 61 20 28 72 65 61 64 20 6f 72 20 77 72 69 74   a (read or writ
12f82 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  e) transaction w
12f83 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  as successfully 
12f84 63 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a  concluded.    **
12f85 20 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 74   by this connect
12f86 69 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 20  ion. Instead of 
12f87 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75  deleting the jou
12f88 72 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 61 73  rnal file it was
12f89 20 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70   .    ** kept op
12f8a 65 6e 20 61 6e 64 20 65 69 74 68 65 72 20 77 61  en and either wa
12f8b 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 30  s truncated to 0
12f8c 20 62 79 74 65 73 20 6f 72 20 69 74 73 20 68 65   bytes or its he
12f8d 61 64 65 72 20 77 61 73 0a 20 20 20 20 2a 2a 20  ader was.    ** 
12f8e 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68  overwritten with
12f8f 20 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20   zeros..    */. 
12f90 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
12f91 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20  r->nRec==0 );.  
12f92 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12f93 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20  ->dbOrigSize==0 
12f94 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
12f95 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
12f96 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d  l==0 );.    rc =
12f97 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72   pager_open_jour
12f98 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  nal(pPager);.  }
12f99 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ..  PAGERTRACE((
12f9a 22 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c  "TRANSACTION %d\
12f9b 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
12f9c 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28  er)));.  assert(
12f9d 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d   !isOpen(pPager-
12f9e 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  >jfd) || pPager-
12f9f 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c  >journalOff>0 ||
12fa0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12fa1 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
12fa2 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73  ../*.** Mark a s
12fa3 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 20  ingle data page 
12fa4 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68  as writeable. Th
12fa5 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65  e page is writte
12fa6 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d  n into the .** m
12fa7 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73  ain journal or s
12fa8 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65  ub-journal as re
12fa9 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 70  quired. If the p
12faa 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  age is written i
12fab 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68  nto.** one of th
12fac 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20  e journals, the 
12fad 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69  corresponding bi
12fae 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  t is set in the 
12faf 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75  .** Pager.pInJou
12fb0 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 20  rnal bitvec and 
12fb1 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69  the PagerSavepoi
12fb2 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20  nt.pInSavepoint 
12fb3 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e  bitvecs.** of an
12fb4 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  y open savepoint
12fb5 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  s as appropriate
12fb6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
12fb7 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 64  pager_write(PgHd
12fb8 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 20  r *pPg){.  void 
12fb9 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44  *pData = pPg->pD
12fba 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  ata;.  Pager *pP
12fbb 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
12fbc 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  er;.  int rc = S
12fbd 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
12fbe 43 68 65 63 6b 20 66 6f 72 20 65 72 72 6f 72 73  Check for errors
12fbf 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  .  */.  if( pPag
12fc0 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 20 0a  er->errCode ){ .
12fc1 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65      return pPage
12fc2 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a  r->errCode;.  }.
12fc3 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 72 65    if( pPager->re
12fc4 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 65  adOnly ){.    re
12fc5 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d  turn SQLITE_PERM
12fc6 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
12fc7 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73   !pPager->setMas
12fc8 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b 5f  ter );..  CHECK_
12fc9 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a  PAGE(pPg);..  /*
12fca 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61   Mark the page a
12fcb 73 20 64 69 72 74 79 2e 20 20 49 66 20 74 68 65  s dirty.  If the
12fcc 20 70 61 67 65 20 68 61 73 20 61 6c 72 65 61 64   page has alread
12fcd 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 20  y been written. 
12fce 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e   ** to the journ
12fcf 61 6c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72  al then we can r
12fd0 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61 79  eturn right away
12fd1 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
12fd2 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28  PcacheMakeDirty(
12fd3 70 50 67 29 3b 0a 20 20 69 66 28 20 70 61 67 65  pPg);.  if( page
12fd4 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26  InJournal(pPg) &
12fd5 26 20 21 73 75 62 6a 52 65 71 75 69 72 65 73 50  & !subjRequiresP
12fd6 61 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20  age(pPg) ){.    
12fd7 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
12fd8 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  ed = 1;.  }else{
12fd9 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 67  ..    /* If we g
12fda 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20  et this far, it 
12fdb 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70  means that the p
12fdc 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a  age needs to be.
12fdd 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 74      ** written t
12fde 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  o the transactio
12fdf 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65  n journal or the
12fe0 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72   ckeckpoint jour
12fe1 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f  nal.    ** or bo
12fe2 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  th..    **.    *
12fe3 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74 6f  * First check to
12fe4 20 73 65 65 20 74 68 61 74 20 74 68 65 20 74 72   see that the tr
12fe5 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61  ansaction journa
12fe6 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20 20 20  l exists and.   
12fe7 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20 69 66   ** create it if
12fe8 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20   it does not..  
12fe9 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
12fea 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d   pPager->state!=
12feb 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a  PAGER_UNLOCK );.
12fec 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12fed 50 61 67 65 72 42 65 67 69 6e 28 70 50 61 67 65  PagerBegin(pPage
12fee 72 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 73 75  r, 0, pPager->su
12fef 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 20 20  bjInMemory);.   
12ff0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12ff1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
12ff2 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
12ff3 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
12ff4 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  >state>=PAGER_RE
12ff5 53 45 52 56 45 44 20 29 3b 0a 20 20 20 20 69 66  SERVED );.    if
12ff6 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  ( !isOpen(pPager
12ff7 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67 65 72  ->jfd) && pPager
12ff8 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 0a 20 20 20  ->useJournal.   
12ff9 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 72         && pPager
12ffa 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50  ->journalMode!=P
12ffb 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
12ffc 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 72 63  _OFF ){.      rc
12ffd 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f   = pager_open_jo
12ffe 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
12fff 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13000 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
13001 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rc;.    }.    pP
13002 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
13003 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20   = 1;.  .    /* 
13004 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  The transaction 
13005 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73  journal now exis
13006 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 61  ts and we have a
13007 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e 0a   RESERVED or an.
13008 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45      ** EXCLUSIVE
13009 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69   lock on the mai
1300a 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
1300b 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72    Write the curr
1300c 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 20  ent page to.    
1300d 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
1300e 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74  on journal if it
1300f 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c   is not there al
13010 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  ready..    */.  
13011 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a 6f 75    if( !pageInJou
13012 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69 73 4f  rnal(pPg) && isO
13013 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
13014 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50   ){.      if( pP
13015 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d  g->pgno<=pPager-
13016 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20  >dbOrigSize ){. 
13017 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75 6d         u32 cksum
13018 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  ;.        char *
13019 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 20 20  pData2;..       
1301a 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 6e 65   /* We should ne
1301b 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 68 65  ver write to the
1301c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
1301d 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 20 20  e page that.    
1301e 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20      ** contains 
1301f 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63  the database loc
13020 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ks.  The followi
13021 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 66 69  ng assert verifi
13022 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  es.        ** th
13023 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 2a 2f  at we do not. */
13024 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
13025 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 45   pPg->pgno!=PAGE
13026 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
13027 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44 61  ) );.        pDa
13028 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70 50 61  ta2 = CODEC2(pPa
13029 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d  ger, pData, pPg-
1302a 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20 20 20  >pgno, 7);.     
1302b 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 65 72     cksum = pager
1302c 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 28  _cksum(pPager, (
1302d 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 20 20  u8*)pData2);.   
1302e 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33       rc = write3
1302f 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
13030 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  d, pPager->journ
13031 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 6e 6f  alOff, pPg->pgno
13032 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
13033 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13034 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
13035 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
13036 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74 61  ager->jfd, pData
13037 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  2, pPager->pageS
13038 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ize,.           
13039 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1303a 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
1303b 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20 20 20  alOff + 4);.    
1303c 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
1303d 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 61 67  urnalOff += pPag
1303e 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 3b 0a  er->pageSize+4;.
1303f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13040 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13041 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
13042 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74   rc = write32bit
13043 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70  s(pPager->jfd, p
13044 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
13045 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20 20  f, cksum);.     
13046 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
13047 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20 20  rnalOff += 4;.  
13048 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13049 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54 20 25  IOTRACE(("JOUT %
1304a 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e 22 2c  p %d %lld %d\n",
1304b 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67   pPager, pPg->pg
1304c 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  no, .           
1304d 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
1304e 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 65 72  urnalOff, pPager
1304f 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a 20 20  ->pageSize));.  
13050 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52        PAGER_INCR
13051 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77  (sqlite3_pager_w
13052 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a 20 20  ritej_count);.  
13053 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
13054 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61  (("JOURNAL %d pa
13055 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d 25  ge %d needSync=%
13056 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c  d hash(%08x)\n",
13057 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41  .             PA
13058 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
13059 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20  Pg->pgno, .     
1305a 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66          ((pPg->f
1305b 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f  lags&PGHDR_NEED_
1305c 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61 67 65  SYNC)?1:0), page
1305d 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 29  r_pagehash(pPg))
1305e 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 45  );..        /* E
1305f 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f 72 20  ven if an IO or 
13060 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72 20 6f  diskfull error o
13061 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 6a 6f  ccurred while jo
13062 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a 20 20  urnalling the.  
13063 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e        ** page in
13064 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f 76 65   the block above
13065 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 2d 73  , set the need-s
13066 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 68 65  ync flag for the
13067 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a   page..        *
13068 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 68 65  * Otherwise, whe
13069 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  n the transactio
1306a 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n is rolled back
1306b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e 0a 20  , the logic in. 
1306c 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 62 61         ** playba
1306d 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 77 69  ck_one_page() wi
1306e 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 74 68  ll think that th
1306f 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20  e page needs to 
13070 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 20 20  be restored.    
13071 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 64 61      ** in the da
13072 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 6e 64  tabase file. And
13073 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   if an IO error 
13074 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69  occurs while doi
13075 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a  ng so,.        *
13076 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 69 6f  * then corruptio
13077 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20 20  n may follow..  
13078 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
13079 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f   if( !pPager->no
1307a 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20  Sync ){.        
1307b 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20    pPg->flags |= 
1307c 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b  PGHDR_NEED_SYNC;
1307d 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65  .          pPage
1307e 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b  r->needSync = 1;
1307f 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
13080 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20      /* An error 
13081 68 61 73 20 6f 63 63 75 72 72 65 64 20 77 72 69  has occurred wri
13082 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72  ting to the jour
13083 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 0a 20  nal file. The . 
13084 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73 61         ** transa
13085 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f  ction will be ro
13086 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65  lled back by the
13087 20 6c 61 79 65 72 20 61 62 6f 76 65 2e 0a 20 20   layer above..  
13088 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
13089 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1308a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1308b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1308c 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 70 50     }..        pP
1308d 61 67 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20  ager->nRec++;.  
1308e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1308f 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
13090 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72  !=0 );.        r
13091 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  c = sqlite3Bitve
13092 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e  cSet(pPager->pIn
13093 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67  Journal, pPg->pg
13094 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  no);.        tes
13095 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54  tcase( rc==SQLIT
13096 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
13097 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
13098 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d  QLITE_OK || rc==
13099 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
1309a 20 20 20 20 20 20 20 20 72 63 20 7c 3d 20 61 64          rc |= ad
1309b 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76  dToSavepointBitv
1309c 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d  ecs(pPager, pPg-
1309d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  >pgno);.        
1309e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1309f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  K ){.          a
130a0 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
130a1 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20  E_NOMEM );.     
130a2 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
130a3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
130a4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
130a5 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72  f( !pPager->jour
130a6 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20 21 70  nalStarted && !p
130a7 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b  Pager->noSync ){
130a8 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 2d 3e  .          pPg->
130a9 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e  flags |= PGHDR_N
130aa 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20  EED_SYNC;.      
130ab 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64      pPager->need
130ac 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20  Sync = 1;.      
130ad 20 20 7d 0a 20 20 20 20 20 20 20 20 50 41 47 45    }.        PAGE
130ae 52 54 52 41 43 45 28 28 22 41 50 50 45 4e 44 20  RTRACE(("APPEND 
130af 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53  %d page %d needS
130b0 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20  ync=%d\n",.     
130b1 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52             PAGER
130b2 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
130b3 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20  >pgno,.         
130b4 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61        ((pPg->fla
130b5 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
130b6 4e 43 29 3f 31 3a 30 29 29 29 3b 0a 20 20 20 20  NC)?1:0)));.    
130b7 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
130b8 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 65   /* If the state
130b9 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20  ment journal is 
130ba 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 67  open and the pag
130bb 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 2c 0a  e is not in it,.
130bc 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 69 74      ** then writ
130bd 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  e the current pa
130be 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 65 6d  ge to the statem
130bf 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f  ent journal.  No
130c0 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74  te that.    ** t
130c1 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  he statement jou
130c2 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 66 66  rnal format diff
130c3 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ers from the sta
130c4 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 66 6f  ndard journal fo
130c5 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e 20 74  rmat.    ** in t
130c6 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 68 65  hat it omits the
130c7 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 20 74   checksums and t
130c8 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20 2a  he header..    *
130c9 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a 52 65  /.    if( subjRe
130ca 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20  quiresPage(pPg) 
130cb 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 75  ){.      rc = su
130cc 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67  bjournalPage(pPg
130cd 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
130ce 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 61  /* Update the da
130cf 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e 64 20  tabase size and 
130d0 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 61  return..  */.  a
130d1 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
130d2 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
130d3 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  ED );.  if( pPag
130d4 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e  er->dbSize<pPg->
130d5 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67  pgno ){.    pPag
130d6 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 67  er->dbSize = pPg
130d7 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a 20 20 72 65  ->pgno;.  }.  re
130d8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
130d9 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20 70 61  * Mark a data pa
130da 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e  ge as writeable.
130db 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75   This routine mu
130dc 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66  st be called bef
130dd 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 6e 67 20 63  ore .** making c
130de 68 61 6e 67 65 73 20 74 6f 20 61 20 70 61 67 65  hanges to a page
130df 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  . The caller mus
130e0 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75  t check the retu
130e1 72 6e 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20  rn value .** of 
130e2 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e  this function an
130e3 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e 6f 74  d be careful not
130e4 20 74 6f 20 63 68 61 6e 67 65 20 61 6e 79 20 70   to change any p
130e5 61 67 65 20 64 61 74 61 20 75 6e 6c 65 73 73 20  age data unless 
130e6 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
130e7 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
130e8 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69  OK..**.** The di
130e9 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
130ea 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
130eb 6e 64 20 70 61 67 65 72 5f 77 72 69 74 65 28 29  nd pager_write()
130ec 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a   is that this.**
130ed 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64   function also d
130ee 65 61 6c 73 20 77 69 74 68 20 74 68 65 20 73 70  eals with the sp
130ef 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 65  ecial case where
130f0 20 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73   2 or more pages
130f1 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 69 6e  .** fit on a sin
130f2 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e  gle disk sector.
130f3 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c   In this case al
130f4 6c 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 70 61  l co-resident pa
130f5 67 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65  ges.** must have
130f6 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
130f7 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
130f8 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  e before returni
130f9 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ng..**.** If an 
130fa 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53 51  error occurs, SQ
130fb 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e  LITE_NOMEM or an
130fc 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
130fd 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 73  s returned.** as
130fe 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 4f 74   appropriate. Ot
130ff 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f  herwise, SQLITE_
13100 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  OK..*/.SQLITE_PR
13101 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
13102 33 50 61 67 65 72 57 72 69 74 65 28 44 62 50 61  3PagerWrite(DbPa
13103 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20 20  ge *pDbPage){.  
13104 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
13105 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a 70 50  OK;..  PgHdr *pP
13106 67 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 50  g = pDbPage;.  P
13107 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
13108 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 50 67  Pg->pPager;.  Pg
13109 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f  no nPagePerSecto
1310a 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 65 63  r = (pPager->sec
1310b 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e  torSize/pPager->
1310c 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 69 66  pageSize);..  if
1310d 28 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72  ( nPagePerSector
1310e 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e  >1 ){.    Pgno n
1310f 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 20  PageCount;      
13110 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
13111 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
13112 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
13113 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 20  .    Pgno pg1;  
13114 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13115 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20  * First page of 
13116 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69  the sector pPg i
13117 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f  s located on. */
13118 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 20  .    int nPage; 
13119 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1311a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
1311b 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67  s starting at pg
1311c 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  1 to journal */.
1311d 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20      int ii;     
1311e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1311f 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
13120 0a 20 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e  .    int needSyn
13121 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  c = 0;         /
13122 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61  * True if any pa
13123 67 65 20 68 61 73 20 50 47 48 44 52 5f 4e 45 45  ge has PGHDR_NEE
13124 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 20 20 20 2f  D_SYNC */..    /
13125 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f 74 53  * Set the doNotS
13126 79 6e 63 20 66 6c 61 67 20 74 6f 20 31 2e 20 54  ync flag to 1. T
13127 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 77  his is because w
13128 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 61  e cannot allow a
13129 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20   journal.    ** 
1312a 68 65 61 64 65 72 20 74 6f 20 62 65 20 77 72 69  header to be wri
1312b 74 74 65 6e 20 62 65 74 77 65 65 6e 20 74 68 65  tten between the
1312c 20 70 61 67 65 73 20 6a 6f 75 72 6e 61 6c 65 64   pages journaled
1312d 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
1312e 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  n..    */.    as
1312f 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a  sert( !MEMDB );.
13130 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
13131 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30  er->doNotSync==0
13132 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   );.    pPager->
13133 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 31 3b 0a 0a  doNotSync = 1;..
13134 20 20 20 20 2f 2a 20 54 68 69 73 20 74 72 69 63      /* This tric
13135 6b 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 62  k assumes that b
13136 6f 74 68 20 74 68 65 20 70 61 67 65 2d 73 69 7a  oth the page-siz
13137 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a  e and sector-siz
13138 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 61 6e 20  e are.    ** an 
13139 69 6e 74 65 67 65 72 20 70 6f 77 65 72 20 6f 66  integer power of
1313a 20 32 2e 20 49 74 20 73 65 74 73 20 76 61 72 69   2. It sets vari
1313b 61 62 6c 65 20 70 67 31 20 74 6f 20 74 68 65 20  able pg1 to the 
1313c 69 64 65 6e 74 69 66 69 65 72 0a 20 20 20 20 2a  identifier.    *
1313d 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 70  * of the first p
1313e 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 74 6f  age of the secto
1313f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65 64  r pPg is located
13140 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   on..    */.    
13141 70 67 31 20 3d 20 28 28 70 50 67 2d 3e 70 67 6e  pg1 = ((pPg->pgn
13142 6f 2d 31 29 20 26 20 7e 28 6e 50 61 67 65 50 65  o-1) & ~(nPagePe
13143 72 53 65 63 74 6f 72 2d 31 29 29 20 2b 20 31 3b  rSector-1)) + 1;
13144 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ..    sqlite3Pag
13145 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
13146 65 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 61 67  er, (int *)&nPag
13147 65 43 6f 75 6e 74 29 3b 0a 20 20 20 20 69 66 28  eCount);.    if(
13148 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65   pPg->pgno>nPage
13149 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e  Count ){.      n
1314a 50 61 67 65 20 3d 20 28 70 50 67 2d 3e 70 67 6e  Page = (pPg->pgn
1314b 6f 20 2d 20 70 67 31 29 2b 31 3b 0a 20 20 20 20  o - pg1)+1;.    
1314c 7d 65 6c 73 65 20 69 66 28 20 28 70 67 31 2b 6e  }else if( (pg1+n
1314d 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29  PagePerSector-1)
1314e 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20  >nPageCount ){. 
1314f 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61       nPage = nPa
13150 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b 0a 20  geCount+1-pg1;. 
13151 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
13152 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50 65 72  nPage = nPagePer
13153 53 65 63 74 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  Sector;.    }.  
13154 20 20 61 73 73 65 72 74 28 6e 50 61 67 65 3e 30    assert(nPage>0
13155 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 67  );.    assert(pg
13156 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20  1<=pPg->pgno);. 
13157 20 20 20 61 73 73 65 72 74 28 28 70 67 31 2b 6e     assert((pg1+n
13158 50 61 67 65 29 3e 70 50 67 2d 3e 70 67 6e 6f 29  Page)>pPg->pgno)
13159 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  ;..    for(ii=0;
1315a 20 69 69 3c 6e 50 61 67 65 20 26 26 20 72 63 3d   ii<nPage && rc=
1315b 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b  =SQLITE_OK; ii++
1315c 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67  ){.      Pgno pg
1315d 20 3d 20 70 67 31 2b 69 69 3b 0a 20 20 20 20 20   = pg1+ii;.     
1315e 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b 0a 20   PgHdr *pPage;. 
1315f 20 20 20 20 20 69 66 28 20 70 67 3d 3d 70 50 67       if( pg==pPg
13160 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 71 6c 69 74  ->pgno || !sqlit
13161 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61  e3BitvecTest(pPa
13162 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c  ger->pInJournal,
13163 20 70 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20   pg) ){.        
13164 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f 4d 4a  if( pg!=PAGER_MJ
13165 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b  _PGNO(pPager) ){
13166 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
13167 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
13168 70 50 61 67 65 72 2c 20 70 67 2c 20 26 70 50 61  pPager, pg, &pPa
13169 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ge);.          i
1316a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1316b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1316c 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65  rc = pager_write
1316d 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
1316e 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
1316f 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
13170 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20  _SYNC ){.       
13171 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20         needSync 
13172 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 1;.           
13173 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72     assert(pPager
13174 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20 20  ->needSync);.   
13175 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
13176 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
13177 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b  gerUnref(pPage);
13178 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
13179 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1317a 73 65 20 69 66 28 20 28 70 50 61 67 65 20 3d 20  se if( (pPage = 
1317b 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61  pager_lookup(pPa
1317c 67 65 72 2c 20 70 67 29 29 21 3d 30 20 29 7b 0a  ger, pg))!=0 ){.
1317d 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67          if( pPag
1317e 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  e->flags&PGHDR_N
1317f 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20  EED_SYNC ){.    
13180 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d        needSync =
13181 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
13182 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
13183 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a  erUnref(pPage);.
13184 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
13185 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 47 48     /* If the PGH
13186 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61  DR_NEED_SYNC fla
13187 67 20 69 73 20 73 65 74 20 66 6f 72 20 61 6e 79  g is set for any
13188 20 6f 66 20 74 68 65 20 6e 50 61 67 65 20 70 61   of the nPage pa
13189 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 74 61 72  ges .    ** star
1318a 74 69 6e 67 20 61 74 20 70 67 31 2c 20 74 68 65  ting at pg1, the
1318b 6e 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  n it needs to be
1318c 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 6f 66 20   set for all of 
1318d 74 68 65 6d 2e 20 42 65 63 61 75 73 65 0a 20 20  them. Because.  
1318e 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20    ** writing to 
1318f 61 6e 79 20 6f 66 20 74 68 65 73 65 20 6e 50 61  any of these nPa
13190 67 65 20 70 61 67 65 73 20 6d 61 79 20 64 61 6d  ge pages may dam
13191 61 67 65 20 74 68 65 20 6f 74 68 65 72 73 2c 20  age the others, 
13192 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e  the.    ** journ
13193 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 63 6f 6e  al file must con
13194 74 61 69 6e 20 73 79 6e 63 28 29 65 64 20 63 6f  tain sync()ed co
13195 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f 66 20 74  pies of all of t
13196 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72  hem.    ** befor
13197 65 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 63 61  e any of them ca
13198 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74  n be written out
13199 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
1319a 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   file..    */.  
1319b 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 20 29    if( needSync )
1319c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1319d 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72  !MEMDB && pPager
1319e 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b 0a 20  ->noSync==0 );. 
1319f 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
131a0 69 3c 6e 50 61 67 65 20 26 26 20 6e 65 65 64 53  i<nPage && needS
131a1 79 6e 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ync; ii++){.    
131a2 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65      PgHdr *pPage
131a3 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28   = pager_lookup(
131a4 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b  pPager, pg1+ii);
131a5 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
131a6 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ge ){.          
131a7 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20  pPage->flags |= 
131a8 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b  PGHDR_NEED_SYNC;
131a9 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
131aa 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
131ab 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ge);.        }. 
131ac 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
131ad 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64  ert(pPager->need
131ae 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Sync);.    }..  
131af 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
131b0 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29  ->doNotSync==1 )
131b1 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f  ;.    pPager->do
131b2 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d  NotSync = 0;.  }
131b3 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70  else{.    rc = p
131b4 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 50 61  ager_write(pDbPa
131b5 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ge);.  }.  retur
131b6 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
131b7 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
131b8 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 6e 20  e page given in 
131b9 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 61 73  the argument was
131ba 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 73 73   previously pass
131bb 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  ed.** to sqlite3
131bc 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 20 49  PagerWrite().  I
131bd 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72  n other words, r
131be 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74  eturn TRUE if it
131bf 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61   is ok.** to cha
131c0 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  nge the content 
131c1 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a  of the page..*/.
131c2 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
131c3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
131c4 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
131c5 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65  writeable(DbPage
131c6 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e   *pPg){.  return
131c7 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
131c8 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69  R_DIRTY;.}.#endi
131c9 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20  f../*.** A call 
131ca 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
131cb 74 65 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20  tells the pager 
131cc 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e  that it is not n
131cd 65 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77  ecessary to.** w
131ce 72 69 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61  rite the informa
131cf 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67  tion on page pPg
131d0 20 62 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73   back to the dis
131d1 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a  k, even though.*
131d2 2a 20 74 68 61 74 20 70 61 67 65 20 6d 69 67 68  * that page migh
131d3 74 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64  t be marked as d
131d4 69 72 74 79 2e 20 20 54 68 69 73 20 68 61 70 70  irty.  This happ
131d5 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ens, for example
131d6 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61  , when.** the pa
131d7 67 65 20 68 61 73 20 62 65 65 6e 20 61 64 64 65  ge has been adde
131d8 64 20 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74  d as a leaf of t
131d9 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20  he freelist and 
131da 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e  so its.** conten
131db 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74  t no longer matt
131dc 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  ers..**.** The o
131dd 76 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72  verlying softwar
131de 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68  e layer calls th
131df 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
131e0 61 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a  all of the data.
131e1 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  ** on the given 
131e2 70 61 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20  page is unused. 
131e3 54 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20  The pager marks 
131e4 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61  the page as clea
131e5 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20  n so.** that it 
131e6 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69  does not get wri
131e7 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a  tten to disk..**
131e8 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74  .** Tests show t
131e9 68 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a  hat this optimiz
131ea 61 74 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75  ation can quadru
131eb 70 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66  ple the speed of
131ec 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54   large .** DELET
131ed 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f  E operations..*/
131ee 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
131ef 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
131f0 72 44 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72  rDontWrite(PgHdr
131f1 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20   *pPg){.  Pager 
131f2 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
131f3 50 61 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50  Pager;.  if( (pP
131f4 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
131f5 49 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d  IRTY) && pPager-
131f6 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29  >nSavepoint==0 )
131f7 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45  {.    PAGERTRACE
131f8 28 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61  (("DONT_WRITE pa
131f9 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20  ge %d of %d\n", 
131fa 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52  pPg->pgno, PAGER
131fb 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
131fc 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41    IOTRACE(("CLEA
131fd 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  N %p %d\n", pPag
131fe 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a  er, pPg->pgno)).
131ff 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c      pPg->flags |
13200 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49  = PGHDR_DONT_WRI
13201 54 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  TE;.#ifdef SQLIT
13202 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
13203 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20    pPg->pageHash 
13204 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  = pager_pagehash
13205 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20  (pPg);.#endif.  
13206 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
13207 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
13208 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74  d to increment t
13209 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1320a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a 2a  database file .*
1320b 2a 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  * change-counter
1320c 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 2d  , stored as a 4-
1320d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
1320e 69 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e 67  integer starting
1320f 20 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66   at .** byte off
13210 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 61  set 24 of the pa
13211 67 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ger file..**.** 
13212 49 66 20 74 68 65 20 69 73 44 69 72 65 63 74 20  If the isDirect 
13213 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 74 68  flag is zero, th
13214 65 6e 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20  en this is done 
13215 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73  by calling .** s
13216 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
13217 28 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68  () on page 1, th
13218 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65  en modifying the
13219 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
1321a 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 20 49  .** page data. I
1321b 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
1321c 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 70 64  file will be upd
1321d 61 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 75  ated when the cu
1321e 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  rrent.** transac
1321f 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
13220 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44  d..**.** The isD
13221 69 72 65 63 74 20 66 6c 61 67 20 6d 61 79 20 6f  irect flag may o
13222 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 20  nly be non-zero 
13223 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77  if the library w
13224 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77  as compiled.** w
13225 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 45  ith the SQLITE_E
13226 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49  NABLE_ATOMIC_WRI
13227 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  TE macro defined
13228 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a  . In this case,.
13229 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20 69  ** if isDirect i
1322a 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1322b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1322c 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64 69  le is updated di
1322d 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72 69  rectly.** by wri
1322e 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 20  ting an updated 
1322f 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 20  version of page 
13230 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74  1 using a call t
13231 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  o the .** sqlite
13232 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 74  3OsWrite() funct
13233 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
13234 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68  nt pager_incr_ch
13235 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 65  angecounter(Page
13236 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69  r *pPager, int i
13237 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20 20  sDirectMode){.  
13238 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
13239 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61 72  OK;..  /* Declar
1323a 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  e and initialize
1323b 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65   constant intege
1323c 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49 66  r 'isDirect'. If
1323d 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69 63   the.  ** atomic
1323e 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74  -write optimizat
1323f 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 69  ion is enabled i
13240 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74 68  n this build, th
13241 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a 2a  en isDirect.  **
13242 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
13243 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 73  to the value pas
13244 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69 72  sed as the isDir
13245 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74 65  ectMode paramete
13246 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 66  r.  ** to this f
13247 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 69  unction. Otherwi
13248 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79 73  se, it is always
13249 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 20   set to zero..  
1324a 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65 61  **.  ** The idea
1324b 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65 20   is that if the 
1324c 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74  atomic-write opt
1324d 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74  imization is not
1324e 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61 74  .  ** enabled at
1324f 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 74   compile time, t
13250 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e 20  he compiler can 
13251 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20 6f  omit the tests o
13252 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63 74  f.  ** 'isDirect
13253 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c 6c  ' below, as well
13254 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65 6e   as the block en
13255 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20 20  closed in the.  
13256 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63 74  ** "if( isDirect
13257 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 20   )" condition.. 
13258 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
13259 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43  TE_ENABLE_ATOMIC
1325a 5f 57 52 49 54 45 0a 20 20 63 6f 6e 73 74 20 69  _WRITE.  const i
1325b 6e 74 20 69 73 44 69 72 65 63 74 20 3d 20 30 3b  nt isDirect = 0;
1325c 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 69 72  .  assert( isDir
1325d 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20  ectMode==0 );.  
1325e 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1325f 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a  (isDirectMode);.
13260 23 65 6c 73 65 0a 20 20 63 6f 6e 73 74 20 69 6e  #else.  const in
13261 74 20 69 73 44 69 72 65 63 74 20 3d 20 69 73 44  t isDirect = isD
13262 69 72 65 63 74 4d 6f 64 65 3b 0a 23 65 6e 64 69  irectMode;.#endi
13263 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  f..  assert( pPa
13264 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
13265 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20  R_RESERVED );.  
13266 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63 68 61  if( !pPager->cha
13267 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20  ngeCountDone && 
13268 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30  pPager->dbSize>0
13269 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70   ){.    PgHdr *p
1326a 50 67 48 64 72 3b 20 20 20 20 20 20 20 20 20 20  PgHdr;          
1326b 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72 65 6e        /* Referen
1326c 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a  ce to page 1 */.
1326d 20 20 20 20 75 33 32 20 63 68 61 6e 67 65 5f 63      u32 change_c
1326e 6f 75 6e 74 65 72 3b 20 20 20 20 20 20 20 20 20  ounter;         
1326f 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 76 61 6c    /* Initial val
13270 75 65 20 6f 66 20 63 68 61 6e 67 65 2d 63 6f 75  ue of change-cou
13271 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f 0a 0a 20  nter field */.. 
13272 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67     assert( !pPag
13273 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20  er->tempFile && 
13274 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
13275 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70  d) );..    /* Op
13276 65 6e 20 70 61 67 65 20 31 20 6f 66 20 74 68 65  en page 1 of the
13277 20 66 69 6c 65 20 66 6f 72 20 77 72 69 74 69 6e   file for writin
13278 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  g. */.    rc = s
13279 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
1327a 50 61 67 65 72 2c 20 31 2c 20 26 70 50 67 48 64  Pager, 1, &pPgHd
1327b 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  r);.    assert( 
1327c 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20 72 63 3d  pPgHdr==0 || rc=
1327d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20  =SQLITE_OK );.. 
1327e 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 6f 6e     /* If page on
1327f 65 20 77 61 73 20 66 65 74 63 68 65 64 20 73 75  e was fetched su
13280 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 6e 64 20  ccessfully, and 
13281 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
13282 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f 70 65 72   not.    ** oper
13283 61 74 69 6e 67 20 69 6e 20 64 69 72 65 63 74 2d  ating in direct-
13284 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 67 65 20  mode, make page 
13285 31 20 77 72 69 74 61 62 6c 65 2e 0a 20 20 20 20  1 writable..    
13286 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
13287 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 69 73 44  QLITE_OK && !isD
13288 69 72 65 63 74 20 29 7b 0a 20 20 20 20 20 20 72  irect ){.      r
13289 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1328a 57 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20  Write(pPgHdr);. 
1328b 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
1328c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1328d 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e       /* Incremen
1328e 74 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 74  t the value just
1328f 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20   read and write 
13290 69 74 20 62 61 63 6b 20 74 6f 20 62 79 74 65 20  it back to byte 
13291 32 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 68 61  24. */.      cha
13292 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71  nge_counter = sq
13293 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75  lite3Get4byte((u
13294 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  8*)pPager->dbFil
13295 65 56 65 72 73 29 3b 0a 20 20 20 20 20 20 63 68  eVers);.      ch
13296 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a  ange_counter++;.
13297 20 20 20 20 20 20 70 75 74 33 32 62 69 74 73 28        put32bits(
13298 28 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d 3e  ((char*)pPgHdr->
13299 70 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e 67  pData)+24, chang
1329a 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 20  e_counter);..   
1329b 20 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67     /* If running
1329c 20 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c   in direct mode,
1329d 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65   write the conte
1329e 6e 74 73 20 6f 66 20 70 61 67 65 20 31 20 74 6f  nts of page 1 to
1329f 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20   the file. */.  
132a0 20 20 20 20 69 66 28 20 69 73 44 69 72 65 63 74      if( isDirect
132a1 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
132a2 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d 20 70  t void *zBuf = p
132a3 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a 20 20  PgHdr->pData;.  
132a4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
132a5 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
132a6 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63  >0 );.        rc
132a7 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
132a8 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 7a 42  e(pPager->fd, zB
132a9 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  uf, pPager->page
132aa 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Size, 0);.      
132ab 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65  }..      /* If e
132ac 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 65 64  verything worked
132ad 2c 20 73 65 74 20 74 68 65 20 63 68 61 6e 67 65  , set the change
132ae 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 2e 20  CountDone flag. 
132af 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  */.      if( rc=
132b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
132b1 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68        pPager->ch
132b2 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20  angeCountDone = 
132b3 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
132b4 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65  ..    /* Release
132b5 20 74 68 65 20 70 61 67 65 20 72 65 66 65 72 65   the page refere
132b6 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  nce. */.    sqli
132b7 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
132b8 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  gHdr);.  }.  ret
132b9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
132ba 20 53 79 6e 63 20 74 68 65 20 70 61 67 65 72 20   Sync the pager 
132bb 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e 20 54 68  file to disk. Th
132bc 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f  is is a no-op fo
132bd 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65  r in-memory file
132be 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 20 77 69  s.** or pages wi
132bf 74 68 20 74 68 65 20 50 61 67 65 72 2e 6e 6f 53  th the Pager.noS
132c0 79 6e 63 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a  ync flag set..**
132c1 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
132c2 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 6f 6e 20  l, or called on 
132c3 61 20 70 61 67 65 72 20 66 6f 72 20 77 68 69 63  a pager for whic
132c4 68 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2c  h it is a no-op,
132c5 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
132c6 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  n returns SQLITE
132c7 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  _OK. Otherwise, 
132c8 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
132c9 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
132ca 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
132cb 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
132cc 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67  Sync(Pager *pPag
132cd 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  er){.  int rc;  
132ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
132d0 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
132d1 69 66 28 20 4d 45 4d 44 42 20 7c 7c 20 70 50 61  if( MEMDB || pPa
132d2 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20  ger->noSync ){. 
132d3 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
132d4 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
132d5 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79  rc = sqlite3OsSy
132d6 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70  nc(pPager->fd, p
132d7 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67  Pager->sync_flag
132d8 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  s);.  }.  return
132d9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79   rc;.}../*.** Sy
132da 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nc the database 
132db 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70 61 67  file for the pag
132dc 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61 73 74  er pPager. zMast
132dd 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  er points to the
132de 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 6d 61   name.** of a ma
132df 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
132e0 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  e that should be
132e1 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
132e2 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20  e individual.** 
132e3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 7a 4d  journal file. zM
132e4 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e 55 4c  aster may be NUL
132e5 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e 74 65  L, which is inte
132e6 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20 6d 61  rpreted as no ma
132e7 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  ster.** journal 
132e8 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61  (a single databa
132e9 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e  se transaction).
132ea 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
132eb 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 74  ine ensures that
132ec 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20  :.**.**   * The 
132ed 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68  database file ch
132ee 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20  ange-counter is 
132ef 75 70 64 61 74 65 64 2c 0a 2a 2a 20 20 20 2a 20  updated,.**   * 
132f0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  the journal is s
132f1 79 6e 63 65 64 20 28 75 6e 6c 65 73 73 20 74 68  ynced (unless th
132f2 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f  e atomic-write o
132f3 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 75  ptimization is u
132f4 73 65 64 29 2c 0a 2a 2a 20 20 20 2a 20 61 6c 6c  sed),.**   * all
132f5 20 64 69 72 74 79 20 70 61 67 65 73 20 61 72 65   dirty pages are
132f6 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
132f7 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 0a  database file, .
132f8 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 62  **   * the datab
132f9 61 73 65 20 66 69 6c 65 20 69 73 20 74 72 75 6e  ase file is trun
132fa 63 61 74 65 64 20 28 69 66 20 72 65 71 75 69 72  cated (if requir
132fb 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20  ed), and.**   * 
132fc 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
132fd 65 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a  e synced. .**.**
132fe 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   The only thing 
132ff 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 6f 20  that remains to 
13300 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73  commit the trans
13301 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 66 69 6e  action is to fin
13302 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 65 6c 65 74  alize .** (delet
13303 65 2c 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a  e, truncate or z
13304 65 72 6f 20 74 68 65 20 66 69 72 73 74 20 70 61  ero the first pa
13305 72 74 20 6f 66 29 20 74 68 65 20 6a 6f 75 72 6e  rt of) the journ
13306 61 6c 20 66 69 6c 65 20 28 6f 72 20 0a 2a 2a 20  al file (or .** 
13307 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65  delete the maste
13308 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  r journal file i
13309 66 20 73 70 65 63 69 66 69 65 64 29 2e 0a 2a 2a  f specified)..**
1330a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66  .** Note that if
1330b 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c 2c 20   zMaster==NULL, 
1330c 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20 6f 76  this does not ov
1330d 65 72 77 72 69 74 65 20 61 20 70 72 65 76 69 6f  erwrite a previo
1330e 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70 61 73 73  us value.** pass
1330f 65 64 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33  ed to an sqlite3
13310 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
13311 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a  One() call..**.*
13312 2a 20 49 66 20 74 68 65 20 66 69 6e 61 6c 20 70  * If the final p
13313 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e  arameter - noSyn
13314 63 20 2d 20 69 73 20 74 72 75 65 2c 20 74 68 65  c - is true, the
13315 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13316 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73  ile itself.** is
13317 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 54 68 65   not synced. The
13318 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c   caller must cal
13319 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  l sqlite3PagerSy
1331a 6e 63 28 29 20 64 69 72 65 63 74 6c 79 20 74 6f  nc() directly to
1331b 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 64 61 74  .** sync the dat
1331c 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72  abase file befor
1331d 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74  e calling Commit
1331e 50 68 61 73 65 54 77 6f 28 29 20 74 6f 20 64 65  PhaseTwo() to de
1331f 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  lete the.** jour
13320 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 68 69 73  nal file in this
13321 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   case..*/.SQLITE
13322 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
13323 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
13324 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 67 65 72  haseOne(.  Pager
13325 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20   *pPager,       
13326 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
13327 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ger object */.  
13328 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
13329 74 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ter,            
1332a 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20  /* If not NULL, 
1332b 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1332c 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74  al name */.  int
1332d 20 6e 6f 53 79 6e 63 20 20 20 20 20 20 20 20 20   noSync         
1332e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1332f 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 74 68 65  True to omit the
13330 20 78 53 79 6e 63 20 6f 6e 20 74 68 65 20 64 62   xSync on the db
13331 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   file */.){.  in
13332 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
13333 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
13334 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
13335 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  .  if( pPager->e
13336 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 65  rrCode ){.    re
13337 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
13338 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 50 41 47  Code;.  }..  PAG
13339 45 52 54 52 41 43 45 28 28 22 44 41 54 41 42 41  ERTRACE(("DATABA
1333a 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73  SE SYNC: File=%s
1333b 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a   zMaster=%s nSiz
1333c 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20  e=%d\n", .      
1333d 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
1333e 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67  e, zMaster, pPag
1333f 65 72 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20  er->dbSize));.. 
13340 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
13341 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20  n in-memory db, 
13342 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 65  or no pages have
13343 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
13344 2c 20 6f 72 20 74 68 69 73 0a 20 20 2a 2a 20 66  , or this.  ** f
13345 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65  unction has alre
13346 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c  ady been called,
13347 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a   it is a no-op..
13348 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42    */.  if( MEMDB
13349 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f   && pPager->dbMo
1334a 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 73 71  dified ){.    sq
1334b 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61  lite3BackupResta
1334c 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b  rt(pPager->pBack
1334d 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  up);.  }else if(
1334e 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d   pPager->state!=
1334f 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 26 20  PAGER_SYNCED && 
13350 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
13351 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 54 68  ed ){..    /* Th
13352 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
13353 6b 20 75 70 64 61 74 65 73 20 74 68 65 20 63 68  k updates the ch
13354 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20 45 78  ange-counter. Ex
13355 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20 20 20  actly how it.   
13356 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 64 65   ** does this de
13357 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72  pends on whether
13358 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d   or not the atom
13359 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69  ic-update optimi
1335a 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 77 61  zation.    ** wa
1335b 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d  s enabled at com
1335c 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 20 69  pile time, and i
1335d 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  f this transacti
1335e 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a 20 20  on meets the .  
1335f 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 72 69    ** runtime cri
13360 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 68 65  teria to use the
13361 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 20 20   operation: .   
13362 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20   **.    **    * 
13363 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  The file-system 
13364 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 74 6f  supports the ato
13365 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 65 72  mic-write proper
13366 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 20 20  ty for.    **   
13367 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a     blocks of siz
13368 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 6e 64  e page-size, and
13369 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 54 68   .    **    * Th
1336a 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e 6f 74  is commit is not
1336b 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69   part of a multi
1336c 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74 69 6f  -file transactio
1336d 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 20 20  n, and.    **   
1336e 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 70   * Exactly one p
1336f 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64  age has been mod
13370 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72 65 20  ified and store 
13371 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  in the journal f
13372 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
13373 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69  ** If the optimi
13374 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 65  zation was not e
13375 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c  nabled at compil
13376 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 68 65  e time, then the
13377 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 6e  .    ** pager_in
13378 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72  cr_changecounter
13379 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
1337a 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 65 20  alled to update 
1337b 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 20 2a  the change.    *
1337c 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27 69 6e  * counter in 'in
1337d 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20 49 66  direct-mode'. If
1337e 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f   the optimizatio
1337f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 6e  n is compiled in
13380 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e   but.    ** is n
13381 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20 74 6f  ot applicable to
13382 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
13383 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4a  n, call sqlite3J
13384 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 0a 20  ournalCreate(). 
13385 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 73 75     ** to make su
13386 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  re the journal f
13387 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c 6c 79  ile has actually
13388 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 74   been created, t
13389 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20  hen call.    ** 
1338a 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67  pager_incr_chang
1338b 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70  ecounter() to up
1338c 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d  date the change-
1338d 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64 69 72  counter in indir
1338e 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64 65 2e  ect.    ** mode.
1338f 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20   .    **.    ** 
13390 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68  Otherwise, if th
13391 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
13392 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 20 61  s both enabled a
13393 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c 0a 20  nd applicable,. 
13394 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20     ** then call 
13395 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67  pager_incr_chang
13396 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70  ecounter() to up
13397 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d  date the change-
13398 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 69  counter.    ** i
13399 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64 65 2e  n 'direct' mode.
1339a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
1339b 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77  e journal file w
1339c 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20 20 20  ill never be.   
1339d 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f 72 20   ** created for 
1339e 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
1339f 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20  ..    */.#ifdef 
133a0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
133a1 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 50  OMIC_WRITE.    P
133a2 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20 20 61  gHdr *pPg;.    a
133a3 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
133a4 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50  ager->jfd) || pP
133a5 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
133a6 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
133a7 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 20  MODE_OFF );.    
133a8 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26 26 20  if( !zMaster && 
133a9 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
133aa 66 64 29 20 0a 20 20 20 20 20 26 26 20 70 50 61  fd) .     && pPa
133ab 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
133ac 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28  =jrnlBufferSize(
133ad 70 50 61 67 65 72 29 20 0a 20 20 20 20 20 26 26  pPager) .     &&
133ae 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e   pPager->dbSize>
133af 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53  =pPager->dbFileS
133b0 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30 3d 3d  ize.     && (0==
133b1 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63  (pPg = sqlite3Pc
133b2 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50  acheDirtyList(pP
133b3 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 29 20  ager->pPCache)) 
133b4 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69 72 74  || 0==pPg->pDirt
133b5 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  y).    ){.      
133b6 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 62  /* Update the db
133b7 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75   file change cou
133b8 6e 74 65 72 20 76 69 61 20 74 68 65 20 64 69 72  nter via the dir
133b9 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68 6f 64  ect-write method
133ba 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a 2a 20  . The .      ** 
133bb 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 77  following call w
133bc 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 69  ill modify the i
133bd 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65  n-memory represe
133be 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67 65 20  ntation of page 
133bf 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 69  1 .      ** to i
133c0 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 61 74  nclude the updat
133c1 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  ed change counte
133c2 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 74 65  r and then write
133c3 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 20 2a   page 1 .      *
133c4 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68  * directly to th
133c5 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
133c6 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20   Because of the 
133c7 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a 20 20  atomic-write .  
133c8 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74 79 20      ** property 
133c9 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65  of the host file
133ca 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20 69 73  -system, this is
133cb 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a 2f 0a   safe..      */.
133cc 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
133cd 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
133ce 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 3b 0a  ter(pPager, 1);.
133cf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
133d0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75   rc = sqlite3Jou
133d1 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61 67 65  rnalCreate(pPage
133d2 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69  r->jfd);.      i
133d3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
133d4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
133d5 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
133d6 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72  gecounter(pPager
133d7 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
133d8 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 72 63    }.#else.    rc
133d9 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68   = pager_incr_ch
133da 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67  angecounter(pPag
133db 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20  er, 0);.#endif. 
133dc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
133dd 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d  E_OK ) goto comm
133de 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69  it_phase_one_exi
133df 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  t;..    /* If th
133e0 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68  is transaction h
133e1 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 74 61  as made the data
133e2 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 74 68  base smaller, th
133e3 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 20 20  en all pages.   
133e4 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63 61 72   ** being discar
133e5 64 65 64 20 62 79 20 74 68 65 20 74 72 75 6e 63  ded by the trunc
133e6 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 77 72  ation must be wr
133e7 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
133e8 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65  rnal.    ** file
133e9 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20  . This can only 
133ea 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76  happen in auto-v
133eb 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20 20 20  acuum mode..    
133ec 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f 72 65  **.    ** Before
133ed 20 72 65 61 64 69 6e 67 20 74 68 65 20 70 61 67   reading the pag
133ee 65 73 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d  es with page num
133ef 62 65 72 73 20 6c 61 72 67 65 72 20 74 68 61 6e  bers larger than
133f0 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 75 72   the .    ** cur
133f1 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61  rent value of Pa
133f2 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65 74 20  ger.dbSize, set 
133f3 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f 20 74  dbSize back to t
133f4 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20  he value.    ** 
133f5 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61 74 20  that it took at 
133f6 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
133f7 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 4f 74   transaction. Ot
133f8 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20 20 20  herwise, the.   
133f9 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c   ** calls to sql
133fa 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 72  ite3PagerGet() r
133fb 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70 61 67  eturn zeroed pag
133fc 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 20  es instead of . 
133fd 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 64 61     ** reading da
133fe 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ta from the data
133ff 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a  base file..    *
13400 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
13401 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
13402 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
13403 3e 64 62 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e  >dbSize<pPager->
13404 64 62 4f 72 69 67 53 69 7a 65 0a 20 20 20 20 20  dbOrigSize.     
13405 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
13406 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f  alMode!=PAGER_JO
13407 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20  URNALMODE_OFF . 
13408 20 20 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f     ){.      Pgno
13409 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1340a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1340b 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
1340c 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
1340d 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69      const Pgno i
1340e 53 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f  Skip = PAGER_MJ_
1340f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b 20 2f 2a  PGNO(pPager); /*
13410 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 70 61   Pending lock pa
13411 67 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73  ge */.      cons
13412 74 20 50 67 6e 6f 20 64 62 53 69 7a 65 20 3d 20  t Pgno dbSize = 
13413 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 20  pPager->dbSize; 
13414 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
13415 65 20 69 6d 61 67 65 20 73 69 7a 65 20 2a 2f 20  e image size */ 
13416 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
13417 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e  bSize = pPager->
13418 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 20 20  dbOrigSize;.    
13419 20 20 66 6f 72 28 20 69 3d 64 62 53 69 7a 65 2b    for( i=dbSize+
1341a 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d 3e 64 62  1; i<=pPager->db
1341b 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b 20 29 7b  OrigSize; i++ ){
1341c 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71  .        if( !sq
1341d 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
1341e 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
1341f 61 6c 2c 20 69 29 20 26 26 20 69 21 3d 69 53 6b  al, i) && i!=iSk
13420 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ip ){.          
13421 50 67 48 64 72 20 2a 70 50 61 67 65 3b 20 20 20  PgHdr *pPage;   
13422 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
13423 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  e to journal */.
13424 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
13425 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
13426 50 61 67 65 72 2c 20 69 2c 20 26 70 50 61 67 65  Pager, i, &pPage
13427 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
13428 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13429 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61   goto commit_pha
1342a 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20  se_one_exit;.   
1342b 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1342c 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
1342d 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
1342e 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
1342f 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  f(pPage);.      
13430 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
13431 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d  TE_OK ) goto com
13432 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78  mit_phase_one_ex
13433 69 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  it;.        }.  
13434 20 20 20 20 7d 20 0a 20 20 20 20 20 20 70 50 61      } .      pPa
13435 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 64 62  ger->dbSize = db
13436 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  Size;.    }.#end
13437 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72 69 74 65  if..    /* Write
13438 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
13439 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68  nal name into th
1343a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  e journal file. 
1343b 49 66 20 61 20 6d 61 73 74 65 72 20 0a 20 20 20  If a master .   
1343c 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   ** journal file
1343d 20 6e 61 6d 65 20 68 61 73 20 61 6c 72 65 61 64   name has alread
1343e 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  y been written t
1343f 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
13440 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f 72 20 69  le, .    ** or i
13441 66 20 7a 4d 61 73 74 65 72 20 69 73 20 4e 55 4c  f zMaster is NUL
13442 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  L (no master jou
13443 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 68 69 73  rnal), then this
13444 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70   call is a no-op
13445 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
13446 3d 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75  = writeMasterJou
13447 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61  rnal(pPager, zMa
13448 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ster);.    if( r
13449 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
1344a 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65  oto commit_phase
1344b 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20  _one_exit;..    
1344c 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72  /* Sync the jour
1344d 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65  nal file. If the
1344e 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f   atomic-update o
1344f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62  ptimization is b
13450 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73 65 64  eing.    ** used
13451 2c 20 74 68 69 73 20 63 61 6c 6c 20 77 69 6c 6c  , this call will
13452 20 6e 6f 74 20 63 72 65 61 74 65 20 74 68 65 20   not create the 
13453 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20  journal file or 
13454 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20 20 20 20  perform any.    
13455 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20 20 20 20  ** real IO..    
13456 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63  */.    rc = sync
13457 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b  Journal(pPager);
13458 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
13459 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f  ITE_OK ) goto co
1345a 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65  mmit_phase_one_e
1345b 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 57 72 69  xit;..    /* Wri
1345c 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  te all dirty pag
1345d 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  es to the databa
1345e 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20  se file. */.    
1345f 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65  rc = pager_write
13460 5f 70 61 67 65 6c 69 73 74 28 73 71 6c 69 74 65  _pagelist(sqlite
13461 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74  3PcacheDirtyList
13462 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
13463 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  ));.    if( rc!=
13464 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13465 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
13466 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43  QLITE_IOERR_BLOC
13467 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 67 6f 74  KED );.      got
13468 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f  o commit_phase_o
13469 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a 20  ne_exit;.    }. 
1346a 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
1346b 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d  CleanAll(pPager-
1346c 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 20 20  >pPCache);..    
1346d 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f  /* If the file o
1346e 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 74 68  n disk is not th
1346f 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20 74  e same size as t
13470 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67  he database imag
13471 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 75  e,.    ** then u
13472 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74  se pager_truncat
13473 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73 68 72  e to grow or shr
13474 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68 65 72  ink the file her
13475 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
13476 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
13477 21 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  !=pPager->dbFile
13478 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 50 67  Size ){.      Pg
13479 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61 67 65 72  no nNew = pPager
1347a 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70 50 61 67  ->dbSize - (pPag
1347b 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 41 47 45  er->dbSize==PAGE
1347c 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
1347d 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ));.      assert
1347e 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
1347f 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45  =PAGER_EXCLUSIVE
13480 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   );.      rc = p
13481 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50  ager_truncate(pP
13482 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20 20 20  ager, nNew);.   
13483 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13484 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d  E_OK ) goto comm
13485 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69  it_phase_one_exi
13486 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  t;.    }..    /*
13487 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e 63 20 74   Finally, sync t
13488 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13489 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50  . */.    if( !pP
1348a 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 26 26 20  ager->noSync && 
1348b 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20  !noSync ){.     
1348c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
1348d 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ync(pPager->fd, 
1348e 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61  pPager->sync_fla
1348f 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49  gs);.    }.    I
13490 4f 54 52 41 43 45 28 28 22 44 42 53 59 4e 43 20  OTRACE(("DBSYNC 
13491 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a  %p\n", pPager)).
13492 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61  .    pPager->sta
13493 74 65 20 3d 20 50 41 47 45 52 5f 53 59 4e 43 45  te = PAGER_SYNCE
13494 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69 74 5f 70  D;.  }..commit_p
13495 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3a 0a 20  hase_one_exit:. 
13496 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13497 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 7b  IOERR_BLOCKED ){
13498 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f 69 6e  .    /* pager_in
13499 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72  cr_changecounter
1349a 28 29 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74  () may attempt t
1349b 6f 20 6f 62 74 61 69 6e 20 61 6e 20 65 78 63 6c  o obtain an excl
1349c 75 73 69 76 65 0a 20 20 20 20 2a 2a 20 6c 6f 63  usive.    ** loc
1349d 6b 20 74 6f 20 73 70 69 6c 6c 20 74 68 65 20 63  k to spill the c
1349e 61 63 68 65 20 61 6e 64 20 72 65 74 75 72 6e 20  ache and return 
1349f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 2e 20 42  IOERR_BLOCKED. B
134a0 75 74 20 73 69 6e 63 65 20 0a 20 20 20 20 2a 2a  ut since .    **
134a1 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61   there is no cha
134a2 6e 63 65 20 74 68 65 20 63 61 63 68 65 20 69 73  nce the cache is
134a3 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 2c 20 69   inconsistent, i
134a4 74 20 69 73 0a 20 20 20 20 2a 2a 20 62 65 74 74  t is.    ** bett
134a5 65 72 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c  er to return SQL
134a6 49 54 45 5f 42 55 53 59 2e 0a 20 20 20 20 2a 2a  ITE_BUSY..    **
134a7 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  /.    rc = SQLIT
134a8 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 72 65  E_BUSY;.  }.  re
134a9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
134aa 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e  ** When this fun
134ab 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
134ac 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
134ad 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70  le has been comp
134ae 6c 65 74 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65  letely.** update
134af 64 20 74 6f 20 72 65 66 6c 65 63 74 20 74 68 65  d to reflect the
134b0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79   changes made by
134b1 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
134b2 6e 73 61 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20  nsaction and.** 
134b3 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20  synced to disk. 
134b4 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  The journal file
134b5 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 69 6e   still exists in
134b6 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
134b7 20 0a 2a 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64   .** though, and
134b8 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63   if a failure oc
134b9 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69  curs at this poi
134ba 6e 74 20 69 74 20 77 69 6c 6c 20 65 76 65 6e 74  nt it will event
134bb 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64  ually.** be used
134bc 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61   as a hot-journa
134bd 6c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  l and the curren
134be 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f  t transaction ro
134bf 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  lled back..**.**
134c0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   This function f
134c1 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75  inalizes the jou
134c2 72 6e 61 6c 20 66 69 6c 65 2c 20 65 69 74 68 65  rnal file, eithe
134c3 72 20 62 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a  r by deleting, .
134c4 2a 2a 20 74 72 75 6e 63 61 74 69 6e 67 20 6f 72  ** truncating or
134c5 20 70 61 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69   partially zeroi
134c6 6e 67 20 69 74 2c 20 73 6f 20 74 68 61 74 20 69  ng it, so that i
134c7 74 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64  t cannot be used
134c8 20 0a 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75   .** for hot-jou
134c9 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f  rnal rollback. O
134ca 6e 63 65 20 74 68 69 73 20 69 73 20 64 6f 6e 65  nce this is done
134cb 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
134cc 20 69 73 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62   is.** irrevocab
134cd 6c 79 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a  ly committed..**
134ce 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
134cf 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72  occurs, an IO er
134d0 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
134d1 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 67  rned and the pag
134d2 65 72 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f  er.** moves into
134d3 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
134d4 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
134d5 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
134d6 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
134d7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
134d8 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
134d9 65 54 77 6f 28 50 61 67 65 72 20 2a 70 50 61 67  eTwo(Pager *pPag
134da 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
134db 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
134dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
134dd 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
134de 20 2f 2a 20 44 6f 20 6e 6f 74 20 70 72 6f 63 65   /* Do not proce
134df 65 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20  ed if the pager 
134e0 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
134e1 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 2a  e error state. *
134e2 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  /.  if( pPager->
134e3 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 72  errCode ){.    r
134e4 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
134e5 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rCode;.  }..  /*
134e6 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
134e7 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c  hould not be cal
134e8 6c 65 64 20 69 66 20 74 68 65 20 70 61 67 65 72  led if the pager
134e9 20 69 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c 65   is not in at le
134ea 61 73 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f 52  ast.  ** PAGER_R
134eb 45 53 45 52 56 45 44 20 73 74 61 74 65 2e 20 41  ESERVED state. A
134ec 6e 64 20 69 6e 64 65 65 64 20 53 51 4c 69 74 65  nd indeed SQLite
134ed 20 6e 65 76 65 72 20 64 6f 65 73 20 74 68 69 73   never does this
134ee 2e 20 42 75 74 20 69 74 20 69 73 0a 20 20 2a 2a  . But it is.  **
134ef 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20 74 68   nice to have th
134f0 69 73 20 64 65 66 65 6e 73 69 76 65 20 62 6c 6f  is defensive blo
134f1 63 6b 20 68 65 72 65 20 61 6e 79 77 61 79 2e 0a  ck here anyway..
134f2 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52    */.  if( NEVER
134f3 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50  (pPager->state<P
134f4 41 47 45 52 5f 52 45 53 45 52 56 45 44 29 20 29  AGER_RESERVED) )
134f5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
134f6 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
134f7 20 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61    /* An optimiza
134f8 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 64 61 74  tion. If the dat
134f9 61 62 61 73 65 20 77 61 73 20 6e 6f 74 20 61 63  abase was not ac
134fa 74 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20  tually modified 
134fb 64 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73  during.  ** this
134fc 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
134fd 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69  e pager is runni
134fe 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d  ng in exclusive-
134ff 6d 6f 64 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a  mode and is.  **
13500 20 75 73 69 6e 67 20 70 65 72 73 69 73 74 65 6e   using persisten
13501 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e  t journals, then
13502 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13503 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a  s a no-op..  **.
13504 20 20 2a 2a 20 54 68 65 20 73 74 61 72 74 20 6f    ** The start o
13505 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
13506 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  le currently con
13507 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a  tains a single j
13508 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61  ournal .  ** hea
13509 64 65 72 20 77 69 74 68 20 74 68 65 20 6e 52 65  der with the nRe
1350a 63 20 66 69 65 6c 64 20 73 65 74 20 74 6f 20 30  c field set to 0
1350b 2e 20 49 66 20 73 75 63 68 20 61 20 6a 6f 75 72  . If such a jour
1350c 6e 61 6c 20 69 73 20 75 73 65 64 20 61 73 0a 20  nal is used as. 
1350d 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61   ** a hot-journa
1350e 6c 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75  l during hot-jou
1350f 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30  rnal rollback, 0
13510 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 62 65   changes will be
13511 20 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68   made.  ** to th
13512 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
13513 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20   So there is no 
13514 6e 65 65 64 20 74 6f 20 7a 65 72 6f 20 74 68 65  need to zero the
13515 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68   journal .  ** h
13516 65 61 64 65 72 2e 20 53 69 6e 63 65 20 74 68 65  eader. Since the
13517 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 63   pager is in exc
13518 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65  lusive mode, the
13519 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20  re is no need.  
1351a 2a 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c  ** to drop any l
1351b 6f 63 6b 73 20 65 69 74 68 65 72 2e 0a 20 20 2a  ocks either..  *
1351c 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  /.  if( pPager->
1351d 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 26  dbModified==0 &&
1351e 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
1351f 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70 50  veMode .   && pP
13520 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
13521 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
13522 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 29  MODE_PERSIST.  )
13523 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
13524 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
13525 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  ==JOURNAL_HDR_SZ
13526 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20  (pPager) );.    
13527 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
13528 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52  ;.  }..  PAGERTR
13529 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c  ACE(("COMMIT %d\
1352a 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
1352b 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28  er)));.  assert(
1352c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d   pPager->state==
1352d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20  PAGER_SYNCED || 
1352e 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 72  MEMDB || !pPager
1352f 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a  ->dbModified );.
13530 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64    rc = pager_end
13531 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  _transaction(pPa
13532 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74  ger, pPager->set
13533 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72  Master);.  retur
13534 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50  n pager_error(pP
13535 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a  ager, rc);.}../*
13536 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c  .** Rollback all
13537 20 63 68 61 6e 67 65 73 2e 20 54 68 65 20 64 61   changes. The da
13538 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61 63  tabase falls bac
13539 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45  k to PAGER_SHARE
1353a 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  D mode..**.** Th
1353b 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66  is function perf
1353c 6f 72 6d 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a  orms two tasks:.
1353d 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72 6f  **.**   1) It ro
1353e 6c 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f 75  lls back the jou
1353f 72 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f  rnal file, resto
13540 72 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61 73  ring all databas
13541 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20  e file and .**  
13542 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61      in-memory ca
13543 63 68 65 20 70 61 67 65 73 20 74 6f 20 74 68 65  che pages to the
13544 20 73 74 61 74 65 20 74 68 65 79 20 77 65 72 65   state they were
13545 20 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72 61   in when the tra
13546 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20  nsaction.**     
13547 20 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64   was opened, and
13548 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69 6e 61  .**   2) It fina
13549 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61  lizes the journa
1354a 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 74 20  l file, so that 
1354b 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 66  it is not used f
1354c 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72  or hot.**      r
1354d 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 20 70  ollback at any p
1354e 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75  oint in the futu
1354f 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63  re..**.** subjec
13550 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  t to the followi
13551 6e 67 20 71 75 61 6c 69 66 69 63 61 74 69 6f 6e  ng qualification
13552 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68  s:.**.** * If th
13553 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
13554 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 20 77  s not yet open w
13555 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
13556 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20  n is called,.** 
13557 20 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20    then only (2) 
13558 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e  is performed. In
13559 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65   this case there
1355a 20 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66   is no journal f
1355b 69 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c  ile.**   to roll
1355c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49   back..**.** * I
1355d 66 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74  f in an error st
1355e 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53  ate other than S
1355f 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e  QLITE_FULL, then
13560 20 74 61 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a   task (1) is .**
13561 20 20 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 66     performed. If
13562 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 61 73   successful, tas
13563 6b 20 28 32 29 2e 20 52 65 67 61 72 64 6c 65 73  k (2). Regardles
13564 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d 65  s of the outcome
13565 0a 2a 2a 20 20 20 6f 66 20 65 69 74 68 65 72 2c  .**   of either,
13566 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
13567 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
13568 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63  eturned to the c
13569 61 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e  aller.**   (i.e.
1356a 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 49   either SQLITE_I
1356b 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f 43  OERR or SQLITE_C
1356c 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a  ORRUPT)..**.** *
1356d 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73   If the pager is
1356e 20 69 6e 20 50 41 47 45 52 5f 52 45 53 45 52 56   in PAGER_RESERV
1356f 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 61  ED state, then a
13570 74 74 65 6d 70 74 20 28 31 29 2e 20 57 68 65 74  ttempt (1). Whet
13571 68 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20  her.**   or not 
13572 28 31 29 20 69 73 20 73 75 63 63 75 73 73 66 75  (1) is succussfu
13573 6c 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70 74 20  l, also attempt 
13574 28 32 29 2e 20 49 66 20 73 75 63 63 65 73 73 66  (2). If successf
13575 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20  ul, return.**   
13576 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72  SQLITE_OK. Other
13577 77 69 73 65 2c 20 65 6e 74 65 72 20 74 68 65 20  wise, enter the 
13578 65 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20  error state and 
13579 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74  return the first
1357a 20 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63 6f 64   .**   error cod
1357b 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a  e encountered. .
1357c 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20  **.**   In this 
1357d 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f  case there is no
1357e 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65   chance that the
1357f 20 64 61 74 61 62 61 73 65 20 77 61 73 20 77 72   database was wr
13580 69 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20  itten to. .**   
13581 53 6f 20 69 73 20 73 61 66 65 20 74 6f 20 66 69  So is safe to fi
13582 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e  nalize the journ
13583 61 6c 20 66 69 6c 65 20 65 76 65 6e 20 69 66 20  al file even if 
13584 74 68 65 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a  the playback .**
13585 20 20 20 28 6f 70 65 72 61 74 69 6f 6e 20 31 29     (operation 1)
13586 20 66 61 69 6c 65 64 2e 20 48 6f 77 65 76 65 72   failed. However
13587 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 20   the pager must 
13588 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20  enter the error 
13589 73 74 61 74 65 0a 2a 2a 20 20 20 61 73 20 74 68  state.**   as th
1358a 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
1358b 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68  e in-memory cach
1358c 65 20 61 72 65 20 6e 6f 77 20 73 75 73 70 65 63  e are now suspec
1358d 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c  t..**.** * Final
1358e 6c 79 2c 20 69 66 20 69 6e 20 50 41 47 45 52 5f  ly, if in PAGER_
1358f 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c  EXCLUSIVE state,
13590 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31   then attempt (1
13591 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74  ). Only.**   att
13592 65 6d 70 74 20 28 32 29 20 69 66 20 28 31 29 20  empt (2) if (1) 
13593 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 52  is successful. R
13594 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
13595 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a  if successful,.*
13596 2a 20 20 20 6f 74 68 65 72 77 69 73 65 20 65 6e  *   otherwise en
13597 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74  ter the error st
13598 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ate and return t
13599 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 72  he error code fr
1359a 6f 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66 61 69  om the .**   fai
1359b 6c 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  ling operation..
1359c 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20  **.**   In this 
1359d 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73  case the databas
1359e 65 20 66 69 6c 65 20 6d 61 79 20 68 61 76 65 20  e file may have 
1359f 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e  been written to.
135a0 20 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20 20 20   So if the.**   
135a1 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69  playback operati
135a2 6f 6e 20 64 69 64 20 6e 6f 74 20 73 75 63 63 65  on did not succe
135a3 65 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20  ed it would not 
135a4 62 65 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c  be safe to final
135a5 69 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75  ize.**   the jou
135a6 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65  rnal file. It ne
135a7 65 64 73 20 74 6f 20 62 65 20 6c 65 66 74 20 69  eds to be left i
135a8 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65  n the file-syste
135a9 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73  m so that.**   s
135aa 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ome other proces
135ab 73 20 63 61 6e 20 75 73 65 20 69 74 20 74 6f 20  s can use it to 
135ac 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61  restore the data
135ad 62 61 73 65 20 73 74 61 74 65 20 28 62 79 0a 2a  base state (by.*
135ae 2a 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  *   hot-journal 
135af 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 51  rollback)..*/.SQ
135b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
135b1 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
135b2 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61  lback(Pager *pPa
135b3 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
135b4 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
135b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
135b6 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
135b7 20 50 41 47 45 52 54 52 41 43 45 28 28 22 52 4f   PAGERTRACE(("RO
135b8 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41  LLBACK %d\n", PA
135b9 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b  GERID(pPager)));
135ba 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
135bb 64 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20 21 69  dbModified || !i
135bc 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
135bd 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  d) ){.    rc = p
135be 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63  ager_end_transac
135bf 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61  tion(pPager, pPa
135c0 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b  ger->setMaster);
135c1 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61  .  }else if( pPa
135c2 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 20  ger->errCode && 
135c3 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21  pPager->errCode!
135c4 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a  =SQLITE_FULL ){.
135c5 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
135c6 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
135c7 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20 20  LUSIVE ){.      
135c8 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
135c9 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d  Pager, 0);.    }
135ca 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 72  .    rc = pPager
135cb 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c  ->errCode;.  }el
135cc 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67  se{.    if( pPag
135cd 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
135ce 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20  _RESERVED ){.   
135cf 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20     int rc2;.    
135d0 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
135d1 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29  yback(pPager, 0)
135d2 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 70 61  ;.      rc2 = pa
135d3 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
135d4 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67  ion(pPager, pPag
135d5 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a  er->setMaster);.
135d6 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
135d7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
135d8 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20     rc = rc2;.   
135d9 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
135da 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
135db 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72  _playback(pPager
135dc 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  , 0);.    }..   
135dd 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20   if( !MEMDB ){. 
135de 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53       pPager->dbS
135df 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  izeValid = 0;.  
135e0 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61    }..    /* If a
135e1 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  n error occurs d
135e2 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b  uring a ROLLBACK
135e3 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67  , we can no long
135e4 65 72 20 74 72 75 73 74 20 74 68 65 20 70 61 67  er trust the pag
135e5 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e  er.    ** cache.
135e6 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f 65   So call pager_e
135e7 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 77 61  rror() on the wa
135e8 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e  y out to make an
135e9 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a 2a 20  y error .    ** 
135ea 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 20 20  persistent..    
135eb 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  */.    rc = page
135ec 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20  r_error(pPager, 
135ed 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  rc);.  }.  retur
135ee 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
135ef 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
135f0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
135f1 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f  is opened read-o
135f2 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41 4c  nly.  Return FAL
135f3 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74  SE.** if the dat
135f4 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 68 65  abase is (in the
135f5 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a 2a  ory) writable..*
135f6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
135f7 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 65 72   u8 sqlite3Pager
135f8 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65 72  Isreadonly(Pager
135f9 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
135fa 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65 61 64  urn pPager->read
135fb 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  Only;.}../*.** R
135fc 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
135fd 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
135fe 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a  o the pager..*/.
135ff 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
13600 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
13601 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70  efcount(Pager *p
13602 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
13603 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
13604 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
13605 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  PCache);.}../*.*
13606 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
13607 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
13608 73 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69  s to the specifi
13609 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49  ed page..*/.SQLI
1360a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1360b 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52  qlite3PagerPageR
1360c 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 20 2a  efcount(DbPage *
1360d 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e  pPage){.  return
1360e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61   sqlite3PcachePa
1360f 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65  geRefcount(pPage
13610 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  );.}..#ifdef SQL
13611 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54  ITE_TEST./*.** T
13612 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
13613 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
13614 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c  and analysis onl
13615 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
13616 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 74 65  VATE int *sqlite
13617 33 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65  3PagerStats(Page
13618 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 74  r *pPager){.  st
13619 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a  atic int a[11];.
1361a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33    a[0] = sqlite3
1361b 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70  PcacheRefCount(p
1361c 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b  Pager->pPCache);
1361d 0a 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69 74 65  .  a[1] = sqlite
1361e 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74  3PcachePagecount
1361f 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
13620 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71 6c 69  );.  a[2] = sqli
13621 74 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68  te3PcacheGetCach
13622 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50  esize(pPager->pP
13623 43 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d 20 3d  Cache);.  a[3] =
13624 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
13625 61 6c 69 64 20 3f 20 28 69 6e 74 29 20 70 50 61  alid ? (int) pPa
13626 67 65 72 2d 3e 64 62 53 69 7a 65 20 3a 20 2d 31  ger->dbSize : -1
13627 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67 65  ;.  a[4] = pPage
13628 72 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d  r->state;.  a[5]
13629 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f   = pPager->errCo
1362a 64 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61  de;.  a[6] = pPa
1362b 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37  ger->nHit;.  a[7
1362c 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73  ] = pPager->nMis
1362d 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20  s;.  a[8] = 0;  
1362e 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 50  /* Used to be pP
1362f 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20  ager->nOvfl */. 
13630 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[9] = pPager->
13631 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d  nRead;.  a[10] =
13632 20 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b   pPager->nWrite;
13633 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23  .  return a;.}.#
13634 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
13635 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 69 73  urn true if this
13636 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79   is an in-memory
13637 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54   pager..*/.SQLIT
13638 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13639 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64  lite3PagerIsMemd
1363a 62 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  b(Pager *pPager)
1363b 7b 0a 20 20 72 65 74 75 72 6e 20 4d 45 4d 44 42  {.  return MEMDB
1363c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  ;.}../*.** Check
1363d 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20   that there are 
1363e 61 74 20 6c 65 61 73 74 20 6e 53 61 76 65 70 6f  at least nSavepo
1363f 69 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20 6f  int savepoints o
13640 70 65 6e 2e 20 49 66 20 74 68 65 72 65 20 61 72  pen. If there ar
13641 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c  e.** currently l
13642 65 73 73 20 74 68 61 6e 20 6e 53 61 76 65 70 6f  ess than nSavepo
13643 69 6e 74 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20  ints open, then 
13644 6f 70 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  open one or more
13645 20 73 61 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 74   savepoints.** t
13646 6f 20 6d 61 6b 65 20 75 70 20 74 68 65 20 64 69  o make up the di
13647 66 66 65 72 65 6e 63 65 2e 20 49 66 20 74 68 65  fference. If the
13648 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70   number of savep
13649 6f 69 6e 74 73 20 69 73 20 61 6c 72 65 61 64 79  oints is already
1364a 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e 53 61  .** equal to nSa
1364b 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68  vepoint, then th
1364c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
1364d 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66   no-op..**.** If
1364e 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
1364f 74 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51 4c 49  tion fails, SQLI
13650 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75  TE_NOMEM is retu
13651 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f  rned. If an erro
13652 72 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77 68 69  r .** occurs whi
13653 6c 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 73  le opening the s
13654 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  ub-journal file,
13655 20 74 68 65 6e 20 61 6e 20 49 4f 20 65 72 72 6f   then an IO erro
13656 72 20 63 6f 64 65 20 69 73 0a 2a 2a 20 72 65 74  r code is.** ret
13657 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
13658 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a  , SQLITE_OK..*/.
13659 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1365a 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  nt sqlite3PagerO
1365b 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 67  penSavepoint(Pag
1365c 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
1365d 6e 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69  nSavepoint){.  i
1365e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1365f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K;              
13660 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
13661 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn code */.  int
13662 20 6e 43 75 72 72 65 6e 74 20 3d 20 70 50 61 67   nCurrent = pPag
13663 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20  er->nSavepoint; 
13664 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
13665 74 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65  t number of save
13666 70 6f 69 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28  points */..  if(
13667 20 6e 53 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72   nSavepoint>nCur
13668 72 65 6e 74 20 26 26 20 70 50 61 67 65 72 2d 3e  rent && pPager->
13669 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  useJournal ){.  
1366a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20    int ii;       
1366b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1366c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
1366d 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
1366e 2f 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70  /.    PagerSavep
1366f 6f 69 6e 74 20 2a 61 4e 65 77 3b 20 20 20 20 20  oint *aNew;     
13670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13671 20 4e 65 77 20 50 61 67 65 72 2e 61 53 61 76 65   New Pager.aSave
13672 70 6f 69 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a  point array */..
13673 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68      /* Either th
13674 65 72 65 20 69 73 20 6e 6f 20 61 63 74 69 76 65  ere is no active
13675 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20   journal or the 
13676 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f  sub-journal is o
13677 70 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74  pen or .    ** t
13678 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c  he journal is al
13679 77 61 79 73 20 73 74 6f 72 65 64 20 69 6e 20 6d  ways stored in m
1367a 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 73 73  emory */.    ass
1367b 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61  ert( pPager->nSa
1367c 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73  vepoint==0 || is
1367d 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66  Open(pPager->sjf
1367e 64 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20  d) ||.          
1367f 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
13680 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
13681 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
13682 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20  );..    /* Grow 
13683 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70  the Pager.aSavep
13684 6f 69 6e 74 20 61 72 72 61 79 20 75 73 69 6e 67  oint array using
13685 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75   realloc(). Retu
13686 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a  rn SQLITE_NOMEM.
13687 20 20 20 20 2a 2a 20 69 66 20 74 68 65 20 61 6c      ** if the al
13688 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20  location fails. 
13689 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72 6f 20  Otherwise, zero 
1368a 74 68 65 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20  the new portion 
1368b 69 6e 20 63 61 73 65 20 61 20 0a 20 20 20 20 2a  in case a .    *
1368c 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  * malloc failure
1368d 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 6f   occurs while po
1368e 70 75 6c 61 74 69 6e 67 20 69 74 20 69 6e 20 74  pulating it in t
1368f 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70  he for(...) loop
13690 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20   below..    */. 
13691 20 20 20 61 4e 65 77 20 3d 20 28 50 61 67 65 72     aNew = (Pager
13692 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69  Savepoint *)sqli
13693 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20  te3Realloc(.    
13694 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76      pPager->aSav
13695 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50  epoint, sizeof(P
13696 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e  agerSavepoint)*n
13697 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b  Savepoint.    );
13698 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 20 29  .    if( !aNew )
13699 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
1369a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1369b 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61   }.    memset(&a
1369c 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30  New[nCurrent], 0
1369d 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43  , (nSavepoint-nC
1369e 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66  urrent) * sizeof
1369f 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29  (PagerSavepoint)
136a0 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 61  );.    pPager->a
136a1 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77  Savepoint = aNew
136a2 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53  ;.    pPager->nS
136a3 61 76 65 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65  avepoint = nSave
136a4 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50  point;..    /* P
136a5 6f 70 75 6c 61 74 65 20 74 68 65 20 50 61 67 65  opulate the Page
136a6 72 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63  rSavepoint struc
136a7 74 75 72 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63  tures just alloc
136a8 61 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72  ated. */.    for
136a9 28 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69  (ii=nCurrent; ii
136aa 3c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b  <nSavepoint; ii+
136ab 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  +){.      assert
136ac 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
136ad 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 20 61  Valid );.      a
136ae 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20  New[ii].nOrig = 
136af 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a  pPager->dbSize;.
136b0 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e        if( isOpen
136b1 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26  (pPager->jfd) &&
136b2 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
136b3 4f 66 66 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  Off>0 ){.       
136b4 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65   aNew[ii].iOffse
136b5 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  t = pPager->jour
136b6 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20 20 7d 65  nalOff;.      }e
136b7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 4e 65  lse{.        aNe
136b8 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20  w[ii].iOffset = 
136b9 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
136ba 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  Pager);.      }.
136bb 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69        aNew[ii].i
136bc 53 75 62 52 65 63 20 3d 20 70 50 61 67 65 72 2d  SubRec = pPager-
136bd 3e 6e 53 75 62 52 65 63 3b 0a 20 20 20 20 20 20  >nSubRec;.      
136be 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65  aNew[ii].pInSave
136bf 70 6f 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 42  point = sqlite3B
136c0 69 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67  itvecCreate(pPag
136c1 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 20  er->dbSize);.   
136c2 20 20 20 69 66 28 20 21 61 4e 65 77 5b 69 69 5d     if( !aNew[ii]
136c3 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b  .pInSavepoint ){
136c4 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
136c5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
136c6 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
136c7 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 75 62   /* Open the sub
136c8 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20  -journal, if it 
136c9 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f  is not already o
136ca 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20 72 63  pened. */.    rc
136cb 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61   = openSubJourna
136cc 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a  l(pPager);.  }..
136cd 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
136ce 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
136cf 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ion is called to
136d0 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c   rollback or rel
136d1 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20  ease (commit) a 
136d2 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68  savepoint..** Th
136d3 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72  e savepoint to r
136d4 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61  elease or rollba
136d5 63 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 74  ck need not be t
136d6 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
136d7 20 0a 2a 2a 20 63 72 65 61 74 65 64 20 73 61 76   .** created sav
136d8 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 50 61  epoint..**.** Pa
136d9 72 61 6d 65 74 65 72 20 6f 70 20 69 73 20 61 6c  rameter op is al
136da 77 61 79 73 20 65 69 74 68 65 72 20 53 41 56 45  ways either SAVE
136db 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f  POINT_ROLLBACK o
136dc 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  r SAVEPOINT_RELE
136dd 41 53 45 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73  ASE..** If it is
136de 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41   SAVEPOINT_RELEA
136df 53 45 2c 20 74 68 65 6e 20 72 65 6c 65 61 73 65  SE, then release
136e0 20 61 6e 64 20 64 65 73 74 72 6f 79 20 74 68 65   and destroy the
136e1 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a   savepoint with.
136e2 2a 2a 20 69 6e 64 65 78 20 69 53 61 76 65 70 6f  ** index iSavepo
136e3 69 6e 74 2e 20 49 66 20 69 74 20 69 73 20 53 41  int. If it is SA
136e4 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
136e5 2c 20 74 68 65 6e 20 72 6f 6c 6c 62 61 63 6b 20  , then rollback 
136e6 61 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74  all changes.** t
136e7 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65  hat have occurre
136e8 64 20 73 69 6e 63 65 20 74 68 65 20 73 70 65 63  d since the spec
136e9 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 20  ified savepoint 
136ea 77 61 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a  was created..**.
136eb 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74  ** The savepoint
136ec 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20   to rollback or 
136ed 72 65 6c 65 61 73 65 20 69 73 20 69 64 65 6e 74  release is ident
136ee 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
136ef 65 72 20 0a 2a 2a 20 69 53 61 76 65 70 6f 69 6e  er .** iSavepoin
136f0 74 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20  t. A value of 0 
136f1 6d 65 61 6e 73 20 74 6f 20 6f 70 65 72 61 74 65  means to operate
136f2 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 6d 6f 73   on the outermos
136f3 74 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 28  t savepoint.** (
136f4 74 68 65 20 66 69 72 73 74 20 63 72 65 61 74 65  the first create
136f5 64 29 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 28  d). A value of (
136f6 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74  Pager.nSavepoint
136f7 2d 31 29 20 6d 65 61 6e 73 20 6f 70 65 72 61 74  -1) means operat
136f8 65 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 6f 73 74  e.** on the most
136f9 20 72 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65   recently create
136fa 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20  d savepoint. If 
136fb 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72  iSavepoint is gr
136fc 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 28 50  eater than.** (P
136fd 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d  ager.nSavepoint-
136fe 31 29 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  1), then this fu
136ff 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
13700 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6e 65  p..**.** If a ne
13701 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 73 20  gative value is 
13702 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
13703 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  unction, then th
13704 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61  e current.** tra
13705 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
13706 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 73  ed back. This is
13707 20 64 69 66 66 65 72 65 6e 74 20 74 6f 20 63 61   different to ca
13708 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65  lling .** sqlite
13709 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29  3PagerRollback()
1370a 20 62 65 63 61 75 73 65 20 74 68 69 73 20 66 75   because this fu
1370b 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  nction does not 
1370c 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 65  terminate.** the
1370d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20   transaction or 
1370e 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62  unlock the datab
1370f 61 73 65 2c 20 69 74 20 6a 75 73 74 20 72 65 73  ase, it just res
13710 74 6f 72 65 73 20 74 68 65 20 0a 2a 2a 20 63 6f  tores the .** co
13711 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61  ntents of the da
13712 74 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72  tabase to its or
13713 69 67 69 6e 61 6c 20 73 74 61 74 65 2e 20 0a 2a  iginal state. .*
13714 2a 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 61 73 65  *.** In any case
13715 2c 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73  , all savepoints
13716 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 20 67   with an index g
13717 72 65 61 74 65 72 20 74 68 61 6e 20 69 53 61 76  reater than iSav
13718 65 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 65 20 64  epoint .** are d
13719 65 73 74 72 6f 79 65 64 2e 20 49 66 20 74 68 69  estroyed. If thi
1371a 73 20 69 73 20 61 20 72 65 6c 65 61 73 65 20 6f  s is a release o
1371b 70 65 72 61 74 69 6f 6e 20 28 6f 70 3d 3d 53 41  peration (op==SA
1371c 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29  VEPOINT_RELEASE)
1371d 2c 0a 2a 2a 20 74 68 65 6e 20 73 61 76 65 70 6f  ,.** then savepo
1371e 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 69  int iSavepoint i
1371f 73 20 61 6c 73 6f 20 64 65 73 74 72 6f 79 65 64  s also destroyed
13720 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
13721 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e  ction may return
13722 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
13723 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
13724 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 6f  tion fails,.** o
13725 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f  r an IO error co
13726 64 65 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f  de if an IO erro
13727 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72  r occurs while r
13728 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 0a 2a  olling back a .*
13729 2a 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20  * savepoint. If 
1372a 6e 6f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c  no errors occur,
1372b 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
1372c 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 51 4c 49  turned..*/ .SQLI
1372d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1372e 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70  qlite3PagerSavep
1372f 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  oint(Pager *pPag
13730 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20  er, int op, int 
13731 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69  iSavepoint){.  i
13732 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
13733 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6f 70  K;..  assert( op
13734 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  ==SAVEPOINT_RELE
13735 41 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50  ASE || op==SAVEP
13736 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b  OINT_ROLLBACK );
13737 0a 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65  .  assert( iSave
13738 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d 3d  point>=0 || op==
13739 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
1373a 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 69 53 61  CK );..  if( iSa
1373b 76 65 70 6f 69 6e 74 3c 70 50 61 67 65 72 2d 3e  vepoint<pPager->
1373c 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20  nSavepoint ){.  
1373d 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20    int ii;       
1373e 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
1373f 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20   variable */.   
13740 20 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 20 20   int nNew;      
13741 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
13742 20 72 65 6d 61 69 6e 69 6e 67 20 73 61 76 65 70   remaining savep
13743 6f 69 6e 74 73 20 61 66 74 65 72 20 74 68 69 73  oints after this
13744 20 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20   op. */..    /* 
13745 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d  Figure out how m
13746 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 77  any savepoints w
13747 69 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61 63 74  ill still be act
13748 69 76 65 20 61 66 74 65 72 20 74 68 69 73 0a 20  ive after this. 
13749 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e     ** operation.
1374a 20 53 74 6f 72 65 20 74 68 69 73 20 76 61 6c 75   Store this valu
1374b 65 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65 6e 20  e in nNew. Then 
1374c 66 72 65 65 20 72 65 73 6f 75 72 63 65 73 20 61  free resources a
1374d 73 73 6f 63 69 61 74 65 64 20 0a 20 20 20 20 2a  ssociated .    *
1374e 2a 20 77 69 74 68 20 61 6e 79 20 73 61 76 65 70  * with any savep
1374f 6f 69 6e 74 73 20 74 68 61 74 20 61 72 65 20 64  oints that are d
13750 65 73 74 72 6f 79 65 64 20 62 79 20 74 68 69 73  estroyed by this
13751 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20   operation..    
13752 2a 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20 69 53  */.    nNew = iS
13753 61 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d  avepoint + (op==
13754 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
13755 43 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d  CK);.    for(ii=
13756 6e 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 72 2d  nNew; ii<pPager-
13757 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b  >nSavepoint; ii+
13758 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
13759 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
1375a 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
1375b 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69  t[ii].pInSavepoi
1375c 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  nt);.    }.    p
1375d 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e  Pager->nSavepoin
1375e 74 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f  t = nNew;..    /
1375f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 72  * If this is a r
13760 6f 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74 69 6f  ollback operatio
13761 6e 2c 20 70 6c 61 79 62 61 63 6b 20 74 68 65 20  n, playback the 
13762 73 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f  specified savepo
13763 69 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74  int..    ** If t
13764 68 69 73 20 69 73 20 61 20 74 65 6d 70 2d 66 69  his is a temp-fi
13765 6c 65 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62  le, it is possib
13766 6c 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72  le that the jour
13767 6e 61 6c 20 66 69 6c 65 20 68 61 73 0a 20 20 20  nal file has.   
13768 20 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e   ** not yet been
13769 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 73   opened. In this
1376a 20 63 61 73 65 20 74 68 65 72 65 20 68 61 76 65   case there have
1376b 20 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67 65 73   been no changes
1376c 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64   to.    ** the d
1376d 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 73 6f  atabase file, so
1376e 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 6f 70   the playback op
1376f 65 72 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 73  eration can be s
13770 6b 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  kipped..    */. 
13771 20 20 20 69 66 28 20 6f 70 3d 3d 53 41 56 45 50     if( op==SAVEP
13772 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26  OINT_ROLLBACK &&
13773 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
13774 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 50 61  jfd) ){.      Pa
13775 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 53  gerSavepoint *pS
13776 61 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77  avepoint = (nNew
13777 3d 3d 30 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e  ==0)?0:&pPager->
13778 61 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d  aSavepoint[nNew-
13779 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70  1];.      rc = p
1377a 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65  agerPlaybackSave
1377b 70 6f 69 6e 74 28 70 50 61 67 65 72 2c 20 70 53  point(pPager, pS
1377c 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20  avepoint);.     
1377d 20 61 73 73 65 72 74 28 72 63 21 3d 53 51 4c 49   assert(rc!=SQLI
1377e 54 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a  TE_DONE);.    }.
1377f 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69    .    /* If thi
13780 73 20 69 73 20 61 20 72 65 6c 65 61 73 65 20 6f  s is a release o
13781 66 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20  f the outermost 
13782 73 61 76 65 70 6f 69 6e 74 2c 20 74 72 75 6e 63  savepoint, trunc
13783 61 74 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 20  ate .    ** the 
13784 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 7a  sub-journal to z
13785 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 7a  ero bytes in siz
13786 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 4e  e. */.    if( nN
13787 65 77 3d 3d 30 20 26 26 20 6f 70 3d 3d 53 41 56  ew==0 && op==SAV
13788 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 26  EPOINT_RELEASE &
13789 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  & isOpen(pPager-
1378a 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  >sjfd) ){.      
1378b 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1378c 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72  TE_OK );.      r
1378d 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
1378e 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 73 6a  ncate(pPager->sj
1378f 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50  fd, 0);.      pP
13790 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20  ager->nSubRec = 
13791 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
13792 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
13793 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75  ** Return the fu
13794 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74  ll pathname of t
13795 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13796 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
13797 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
13798 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
13799 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67  name(Pager *pPag
1379a 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
1379b 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b  ager->zFilename;
1379c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1379d 20 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75   the VFS structu
1379e 72 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72  re for the pager
1379f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
137a0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
137a1 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61  3_vfs *sqlite3Pa
137a2 67 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50  gerVfs(Pager *pP
137a3 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
137a4 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a  pPager->pVfs;.}.
137a5 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
137a6 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f  e file handle fo
137a7 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  r the database f
137a8 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  ile associated.*
137a9 2a 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72  * with the pager
137aa 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65  .  This might re
137ab 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65  turn NULL if the
137ac 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74   file has.** not
137ad 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64   yet been opened
137ae 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
137af 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ATE sqlite3_file
137b0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
137b1 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  le(Pager *pPager
137b2 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
137b3 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  er->fd;.}../*.**
137b4 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c   Return the full
137b5 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65   pathname of the
137b6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
137b7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
137b8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
137b9 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
137ba 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67  name(Pager *pPag
137bb 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
137bc 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a  ager->zJournal;.
137bd 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
137be 74 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20  true if fsync() 
137bf 63 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c  calls are disabl
137c0 65 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  ed for this page
137c1 72 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45  r.  Return FALSE
137c2 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20  .** if fsync()s 
137c3 61 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72  are executed nor
137c4 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  mally..*/.SQLITE
137c5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
137c6 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28  ite3PagerNosync(
137c7 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
137c8 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
137c9 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64  >noSync;.}..#ifd
137ca 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  ef SQLITE_HAS_CO
137cb 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  DEC./*.** Set th
137cc 65 20 63 6f 64 65 63 20 66 6f 72 20 74 68 69 73  e codec for this
137cd 20 70 61 67 65 72 0a 2a 2f 0a 53 51 4c 49 54 45   pager.*/.SQLITE
137ce 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
137cf 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64  lite3PagerSetCod
137d0 65 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  ec(.  Pager *pPa
137d1 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78  ger,.  void *(*x
137d2 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69  Codec)(void*,voi
137d3 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20  d*,Pgno,int),.  
137d4 76 6f 69 64 20 2a 70 43 6f 64 65 63 41 72 67 0a  void *pCodecArg.
137d5 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f  ){.  pPager->xCo
137d6 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a 20 20  dec = xCodec;.  
137d7 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 41 72  pPager->pCodecAr
137d8 67 20 3d 20 70 43 6f 64 65 63 41 72 67 3b 0a 7d  g = pCodecArg;.}
137d9 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
137da 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
137db 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f  OVACUUM./*.** Mo
137dc 76 65 20 74 68 65 20 70 61 67 65 20 70 50 67 20  ve the page pPg 
137dd 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f  to location pgno
137de 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a   in the file..**
137df 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62  .** There must b
137e0 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73 20  e no references 
137e1 74 6f 20 74 68 65 20 70 61 67 65 20 70 72 65 76  to the page prev
137e2 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61  iously located a
137e3 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63 68  t.** pgno (which
137e4 20 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29   we call pPgOld)
137e5 20 74 68 6f 75 67 68 20 74 68 61 74 20 70 61 67   though that pag
137e6 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20  e is allowed to 
137e7 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20  be.** in cache. 
137e8 20 49 66 20 74 68 65 20 70 61 67 65 20 70 72 65   If the page pre
137e9 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20  viously located 
137ea 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61  at pgno is not a
137eb 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65  lready.** in the
137ec 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
137ed 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75 74  l, it is not put
137ee 20 74 68 65 72 65 20 62 79 20 62 79 20 74 68 69   there by by thi
137ef 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a  s routine..**.**
137f0 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   References to t
137f1 68 65 20 70 61 67 65 20 70 50 67 20 72 65 6d 61  he page pPg rema
137f2 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74 69  in valid. Updati
137f3 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64  ng any.** meta-d
137f4 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
137f5 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64 61  ith pPg (i.e. da
137f6 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ta stored in the
137f7 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a 2a   nExtra bytes.**
137f8 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67   allocated along
137f9 20 77 69 74 68 20 74 68 65 20 70 61 67 65 29 20   with the page) 
137fa 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62  is the responsib
137fb 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c  ility of the cal
137fc 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61  ler..**.** A tra
137fd 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  nsaction must be
137fe 20 61 63 74 69 76 65 20 77 68 65 6e 20 74 68 69   active when thi
137ff 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
13800 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f 20  led. It used to 
13801 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 74  be.** required t
13802 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20  hat a statement 
13803 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
13804 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74 20  not active, but 
13805 74 68 69 73 20 72 65 73 74 72 69 63 74 69 6f 6e  this restriction
13806 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65 6d  .** has been rem
13807 6f 76 65 64 20 28 43 52 45 41 54 45 20 49 4e 44  oved (CREATE IND
13808 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65  EX needs to move
13809 20 61 20 70 61 67 65 20 77 68 65 6e 20 61 20 73   a page when a s
1380a 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e  tatement.** tran
1380b 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
1380c 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  e)..**.** If the
1380d 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74   fourth argument
1380e 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 20 6e  , isCommit, is n
1380f 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  on-zero, then th
13810 69 73 20 70 61 67 65 20 69 73 20 62 65 69 6e 67  is page is being
13811 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 61 72  .** moved as par
13812 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  t of a database 
13813 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e 20 6a  reorganization j
13814 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 74  ust before the t
13815 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 69  ransaction .** i
13816 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65  s being committe
13817 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  d. In this case,
13818 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65   it is guarantee
13819 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  d that the datab
1381a 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 50 67  ase page .** pPg
1381b 20 72 65 66 65 72 73 20 74 6f 20 77 69 6c 6c 20   refers to will 
1381c 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 20 74  not be written t
1381d 6f 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74  o again within t
1381e 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  his transaction.
1381f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
13820 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20  tion may return 
13821 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20  SQLITE_NOMEM or 
13822 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
13823 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20   if an error.** 
13824 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 77 69 73  occurs. Otherwis
13825 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53 51  e, it returns SQ
13826 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49  LITE_OK..*/.SQLI
13827 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
13828 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70  qlite3PagerMovep
13829 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  age(Pager *pPage
1382a 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c 20  r, DbPage *pPg, 
1382b 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69  Pgno pgno, int i
1382c 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48 64  sCommit){.  PgHd
1382d 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20 20  r *pPgOld;      
1382e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1382f 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72 77  page being overw
13830 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 6e  ritten. */.  Pgn
13831 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d  o needSyncPgno =
13832 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c 64   0;       /* Old
13833 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e 70   value of pPg->p
13834 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73 20  gno, if sync is 
13835 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e  required */.  in
13836 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
13837 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
13838 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50  turn code */.  P
13839 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 20 20  gno origPgno;   
1383a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1383b 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
1383c 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 61 73   number */..  as
1383d 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e  sert( pPg->nRef>
1383e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  0 );..  /* If th
1383f 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f 76  e page being mov
13840 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64 20  ed is dirty and 
13841 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61 76  has not been sav
13842 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73 74  ed by the latest
13843 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2c  .  ** savepoint,
13844 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20 63   then save the c
13845 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20  urrent contents 
13846 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f  of the page into
13847 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d 6a   the .  ** sub-j
13848 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69 73  ournal now. This
13849 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20   is required to 
1384a 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c 6f  handle the follo
1384b 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a 20  wing scenario:. 
1384c 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e   **.  **   BEGIN
1384d 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72  ;.  **     <jour
1384e 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65 6e  nal page X, then
1384f 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d 65   modify it in me
13850 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20 53  mory>.  **     S
13851 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20  AVEPOINT one;.  
13852 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20 70  **       <Move p
13853 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69 6f  age X to locatio
13854 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52 4f  n Y>.  **     RO
13855 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a 20  LLBACK TO one;. 
13856 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67 65   **.  ** If page
13857 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69 74   X were not writ
13858 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d 6a  ten to the sub-j
13859 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74 20  ournal here, it 
1385a 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20 62  would not.  ** b
1385b 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65  e possible to re
1385c 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65 6e  store its conten
1385d 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f 4c  ts when the "ROL
1385e 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20 20  LBACK TO one".  
1385f 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 72  ** statement wer
13860 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e 0a  e is processed..
13861 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f 75    **.  ** subjou
13862 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20 6e  rnalPage() may n
13863 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  eed to allocate 
13864 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 70  space to store p
13865 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20 20  Pg->pgno into.  
13866 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73  ** one or more s
13867 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73  avepoint bitvecs
13868 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72 65  . This is the re
13869 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69  ason this functi
1386a 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74 75  on.  ** may retu
1386b 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e  rn SQLITE_NOMEM.
1386c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d  .  */.  if( pPg-
1386d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
1386e 54 59 20 0a 20 20 20 26 26 20 73 75 62 6a 52 65  TY .   && subjRe
1386f 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 0a  quiresPage(pPg).
13870 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21     && SQLITE_OK!
13871 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61  =(rc = subjourna
13872 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 29 7b  lPage(pPg)).  ){
13873 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
13874 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43    }..  PAGERTRAC
13875 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67 65  E(("MOVE %d page
13876 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25 64   %d (needSync=%d
13877 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e 22  ) moves to %d\n"
13878 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 49 44  , .      PAGERID
13879 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70  (pPager), pPg->p
1387a 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67 73  gno, (pPg->flags
1387b 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
1387c 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a 20  )?1:0, pgno));. 
1387d 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45 20   IOTRACE(("MOVE 
1387e 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50 61  %p %d %d\n", pPa
1387f 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  ger, pPg->pgno, 
13880 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66 20  pgno))..  /* If 
13881 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64  the journal need
13882 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64  s to be sync()ed
13883 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 50 67   before page pPg
13884 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a 20  ->pgno can.  ** 
13885 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 73  be written to, s
13886 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69  tore pPg->pgno i
13887 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  n local variable
13888 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a 20   needSyncPgno.. 
13889 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20   **.  ** If the 
1388a 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73  isCommit flag is
1388b 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 20 6e   set, there is n
1388c 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62  o need to rememb
1388d 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65  er that.  ** the
1388e 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74   journal needs t
1388f 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65  o be sync()ed be
13890 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61  fore database pa
13891 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20 20  ge pPg->pgno .  
13892 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65  ** can be writte
13893 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72  n to. The caller
13894 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f   has already pro
13895 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69  mised not to wri
13896 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a 20  te to it..  */. 
13897 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 73   if( (pPg->flags
13898 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
13899 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20 29  ) && !isCommit )
1389a 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50 67  {.    needSyncPg
1389b 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a  no = pPg->pgno;.
1389c 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65      assert( page
1389d 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c  InJournal(pPg) |
1389e 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67  | pPg->pgno>pPag
1389f 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29  er->dbOrigSize )
138a0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
138a1 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
138a2 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 73 65  IRTY );.    asse
138a3 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64  rt( pPager->need
138a4 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Sync );.  }..  /
138a5 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20 63  * If the cache c
138a6 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20 77  ontains a page w
138a7 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  ith page-number 
138a8 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74 0a  pgno, remove it.
138a9 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68 61    ** from its ha
138aa 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c 20  sh chain. Also, 
138ab 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65  if the PgHdr.nee
138ac 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 66 6f  dSync was set fo
138ad 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67 6e  r .  ** page pgn
138ae 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d 6f  o before the 'mo
138af 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20 69  ve' operation, i
138b0 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  t needs to be re
138b1 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f 72  tained .  ** for
138b2 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64 20   the page moved 
138b3 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70 50  there..  */.  pP
138b4 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48  g->flags &= ~PGH
138b5 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
138b6 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f 6c  pPgOld = pager_l
138b7 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67  ookup(pPager, pg
138b8 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  no);.  assert( !
138b9 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c 64  pPgOld || pPgOld
138ba 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69  ->nRef==1 );.  i
138bb 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20 20  f( pPgOld ){.   
138bc 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 28   pPg->flags |= (
138bd 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 50 47  pPgOld->flags&PG
138be 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a  HDR_NEED_SYNC);.
138bf 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
138c0 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a 20  eDrop(pPgOld);. 
138c1 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e 6f 20 3d   }..  origPgno =
138c2 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 73 71   pPg->pgno;.  sq
138c3 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28  lite3PcacheMove(
138c4 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 20 73 71  pPg, pgno);.  sq
138c5 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
138c6 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50 61  irty(pPg);.  pPa
138c7 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20  ger->dbModified 
138c8 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65 64  = 1;..  if( need
138c9 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20 20  SyncPgno ){.    
138ca 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50 67  /* If needSyncPg
138cb 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  no is non-zero, 
138cc 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
138cd 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 62   file needs to b
138ce 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28 29  e .    ** sync()
138cf 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 61  ed before any da
138d0 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ta is written to
138d1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 70   database file p
138d2 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f  age needSyncPgno
138d3 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e 74  ..    ** Current
138d4 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67 65  ly, no such page
138d5 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 70   exists in the p
138d6 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74 68  age-cache and th
138d7 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a 6f  e .    ** "is jo
138d8 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63 20  urnaled" bitvec 
138d9 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73 65  flag has been se
138da 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74 6f  t. This needs to
138db 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79 0a   be remedied by.
138dc 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20 74      ** loading t
138dd 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65  he page into the
138de 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64   pager-cache and
138df 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67 48   setting the PgH
138e0 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 20  dr.needSync .   
138e1 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a 2a   ** flag..    **
138e2 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 61  .    ** If the a
138e3 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 74  ttempt to load t
138e4 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65  he page into the
138e5 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69 6c   page-cache fail
138e6 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20 74  s, (due.    ** t
138e7 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20  o a malloc() or 
138e8 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c 65  IO failure), cle
138e9 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74 68  ar the bit in th
138ea 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a 20  e pInJournal[]. 
138eb 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74 68     ** array. Oth
138ec 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 70  erwise, if the p
138ed 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61 6e  age is loaded an
138ee 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e 20  d written again 
138ef 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 74  in.    ** this t
138f0 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 6d  ransaction, it m
138f1 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  ay be written to
138f2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
138f3 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a  le before.    **
138f4 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69 6e   it is synced in
138f5 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
138f6 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20 69  ile. This way, i
138f7 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e 0a  t may end up in.
138f8 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e      ** the journ
138f9 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20 62  al file twice, b
138fa 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61  ut that is not a
138fb 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a 2a   problem..    **
138fc 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c 69  .    ** The sqli
138fd 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63 61  te3PagerGet() ca
138fe 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68 65  ll may cause the
138ff 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e 63   journal to sync
13900 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a  . So make.    **
13901 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72 2e   sure the Pager.
13902 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73  needSync flag is
13903 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a 2f   set too..    */
13904 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 48  .    PgHdr *pPgH
13905 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  dr;.    assert( 
13906 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
13907 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
13908 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61  ite3PagerGet(pPa
13909 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e  ger, needSyncPgn
1390a 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20  o, &pPgHdr);.   
1390b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1390c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  OK ){.      if( 
1390d 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
1390e 61 6c 20 26 26 20 6e 65 65 64 53 79 6e 63 50 67  al && needSyncPg
1390f 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72  no<=pPager->dbOr
13910 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  igSize ){.      
13911 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43    sqlite3BitvecC
13912 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 49 6e  lear(pPager->pIn
13913 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 53 79 6e  Journal, needSyn
13914 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 7d 0a  cPgno);.      }.
13915 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
13916 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
13917 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b  r->needSync = 1;
13918 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13919 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26  ger->noSync==0 &
1391a 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20  & !MEMDB );.    
1391b 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d  pPgHdr->flags |=
1391c 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
1391d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ;.    sqlite3Pca
1391e 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67  cheMakeDirty(pPg
1391f 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Hdr);.    sqlite
13920 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48  3PagerUnref(pPgH
13921 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  dr);.  }..  /*. 
13922 20 2a 2a 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65   ** For an in-me
13923 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 6d  mory database, m
13924 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72 69  ake sure the ori
13925 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 69  ginal page conti
13926 6e 75 65 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69  nues.  ** to exi
13927 73 74 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20  st, in case the 
13928 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64  transaction need
13929 73 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20  s to roll back. 
1392a 20 57 65 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a   We allocate.  *
1392b 2a 20 74 68 65 20 70 61 67 65 20 6e 6f 77 2c 20  * the page now, 
1392c 69 6e 73 74 65 61 64 20 6f 66 20 61 74 20 72 6f  instead of at ro
1392d 6c 6c 62 61 63 6b 2c 20 62 65 63 61 75 73 65 20  llback, because 
1392e 77 65 20 63 61 6e 20 62 65 74 74 65 72 20 64 65  we can better de
1392f 61 6c 0a 20 20 2a 2a 20 77 69 74 68 20 61 6e 20  al.  ** with an 
13930 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72  out-of-memory er
13931 72 6f 72 20 6e 6f 77 2e 20 20 54 69 63 6b 65 74  ror now.  Ticket
13932 20 23 33 37 36 31 2e 0a 20 20 2a 2f 0a 20 20 69   #3761..  */.  i
13933 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20  f( MEMDB ){.    
13934 44 62 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20  DbPage *pNew;.  
13935 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
13936 67 65 72 41 63 71 75 69 72 65 28 70 50 61 67 65  gerAcquire(pPage
13937 72 2c 20 6f 72 69 67 50 67 6e 6f 2c 20 26 70 4e  r, origPgno, &pN
13938 65 77 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 20  ew, 1);.    if( 
13939 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
1393a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 73  return rc;.    s
1393b 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
1393c 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72  (pNew);.  }..  r
1393d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1393e 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1393f 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
13940 72 20 74 6f 20 74 68 65 20 64 61 74 61 20 66 6f  r to the data fo
13941 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  r the specified 
13942 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  page..*/.SQLITE_
13943 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
13944 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
13945 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  a(DbPage *pPg){.
13946 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e    assert( pPg->n
13947 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50  Ref>0 || pPg->pP
13948 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20  ager->memDb );. 
13949 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 44 61   return pPg->pDa
1394a 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ta;.}../*.** Ret
1394b 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
1394c 20 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72   the Pager.nExtr
1394d 61 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72  a bytes of "extr
1394e 61 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c  a" space .** all
1394f 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74  ocated along wit
13950 68 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  h the specified 
13951 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  page..*/.SQLITE_
13952 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
13953 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74  lite3PagerGetExt
13954 72 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  ra(DbPage *pPg){
13955 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
13956 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
13957 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 72    return (pPager
13958 3f 70 50 67 2d 3e 70 45 78 74 72 61 3a 30 29 3b  ?pPg->pExtra:0);
13959 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65  .}../*.** Get/se
1395a 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f  t the locking-mo
1395b 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  de for this page
1395c 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f  r. Parameter eMo
1395d 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a 2a  de must be one.*
1395e 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b 49  * of PAGER_LOCKI
1395f 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 41  NGMODE_QUERY, PA
13960 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
13961 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 41  NORMAL or .** PA
13962 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
13963 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 68  EXCLUSIVE. If th
13964 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  e parameter is n
13965 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e 0a  ot _QUERY, then.
13966 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  ** the locking-m
13967 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 68  ode is set to th
13968 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 65  e value specifie
13969 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  d..**.** The ret
1396a 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 65  urned value is e
1396b 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 4b  ither PAGER_LOCK
1396c 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f  INGMODE_NORMAL o
1396d 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49  r.** PAGER_LOCKI
1396e 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
1396f 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65  , indicating the
13970 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 62   current (possib
13971 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 6c  ly updated).** l
13972 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f 0a  ocking-mode..*/.
13973 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
13974 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c  nt sqlite3PagerL
13975 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 72  ockingMode(Pager
13976 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 4d   *pPager, int eM
13977 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ode){.  assert( 
13978 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
13979 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a 20  KINGMODE_QUERY. 
1397a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
1397b 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
1397c 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 20  NGMODE_NORMAL.  
1397d 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
1397e 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  de==PAGER_LOCKIN
1397f 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
13980 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
13981 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51  ER_LOCKINGMODE_Q
13982 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 65  UERY<0 );.  asse
13983 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  rt( PAGER_LOCKIN
13984 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 20  GMODE_NORMAL>=0 
13985 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  && PAGER_LOCKING
13986 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e 3d  MODE_EXCLUSIVE>=
13987 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 65  0 );.  if( eMode
13988 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d 3e  >=0 && !pPager->
13989 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  tempFile ){.    
1398a 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
1398b 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64  eMode = (u8)eMod
1398c 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
1398d 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63  (int)pPager->exc
1398e 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f  lusiveMode;.}../
1398f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65  *.** Get/set the
13990 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f   journal-mode fo
13991 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61  r this pager. Pa
13992 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75  rameter eMode mu
13993 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a  st be one of:.**
13994 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55  .**    PAGER_JOU
13995 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 2a  RNALMODE_QUERY.*
13996 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
13997 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a  ALMODE_DELETE.**
13998 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
13999 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a  LMODE_TRUNCATE.*
1399a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
1399b 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a  ALMODE_PERSIST.*
1399c 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
1399d 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 20  ALMODE_OFF.**   
1399e 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
1399f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a 20  DE_MEMORY.**.** 
139a0 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  If the parameter
139a1 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20   is not _QUERY, 
139a2 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
139a3 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20  _mode is set to 
139a4 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 65  the.** value spe
139a5 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63 68  cified if the ch
139a6 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 2e  ange is allowed.
139a7 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 73 20    The change is 
139a8 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66 6f  disallowed.** fo
139a9 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  r the following 
139aa 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20  reasons:.**.**  
139ab 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79   *  An in-memory
139ac 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f 6e   database can on
139ad 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 72  ly have its jour
139ae 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f 20  nal_mode set to 
139af 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 20  _OFF.**      or 
139b0 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 20  _MEMORY..**.**  
139b1 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20   *  The journal 
139b2 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65 20  mode may not be 
139b3 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61 20  changed while a 
139b4 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
139b5 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ctive..**.** The
139b6 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61   returned indica
139b7 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 28  te the current (
139b8 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64  possibly updated
139b9 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e 0a  ) journal-mode..
139ba 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
139bb 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
139bc 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61  erJournalMode(Pa
139bd 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
139be 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72   eMode){.  asser
139bf 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  t( eMode==PAGER_
139c0 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
139c1 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  Y.            ||
139c2 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
139c3 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45  URNALMODE_DELETE
139c4 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
139c5 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
139c6 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
139c7 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  E.            ||
139c8 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
139c9 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
139ca 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  T.            ||
139cb 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
139cc 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20  URNALMODE_OFF . 
139cd 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
139ce 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
139cf 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b  ALMODE_MEMORY );
139d0 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52  .  assert( PAGER
139d1 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
139d2 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65 4d  RY<0 );.  if( eM
139d3 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21 4d  ode>=0.   && (!M
139d4 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50  EMDB || eMode==P
139d5 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
139d6 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 20  _MEMORY .       
139d7 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d         || eMode=
139d8 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
139d9 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21 70  DE_OFF).   && !p
139da 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65  Pager->dbModifie
139db 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65 6e  d.   && (!isOpen
139dc 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c  (pPager->jfd) ||
139dd 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72   0==pPager->jour
139de 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20 20  nalOff).  ){.   
139df 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
139e0 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20  er->jfd) ){.    
139e1 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
139e2 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
139e3 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d     }.    pPager-
139e4 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 28  >journalMode = (
139e5 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20  u8)eMode;.  }.  
139e6 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67  return (int)pPag
139e7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b  er->journalMode;
139e8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65  .}../*.** Get/se
139e9 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 74  t the size-limit
139ea 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 69 73   used for persis
139eb 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  tent journal fil
139ec 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e  es..**.** Settin
139ed 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 74  g the size limit
139ee 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f 20   to -1 means no 
139ef 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63 65  limit is enforce
139f0 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74  d..** An attempt
139f1 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74 20   to set a limit 
139f2 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31 20  smaller than -1 
139f3 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  is a no-op..*/.S
139f4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36  QLITE_PRIVATE i6
139f5 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  4 sqlite3PagerJo
139f6 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50  urnalSizeLimit(P
139f7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 36  ager *pPager, i6
139f8 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 28  4 iLimit){.  if(
139f9 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a 20   iLimit>=-1 ){. 
139fa 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
139fb 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 4c  alSizeLimit = iL
139fc 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  imit;.  }.  retu
139fd 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  rn pPager->journ
139fe 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a  alSizeLimit;.}..
139ff 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  /*.** Return a p
13a00 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 50  ointer to the pP
13a01 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 61  ager->pBackup va
13a02 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 6b  riable. The back
13a03 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e 20  up module.** in 
13a04 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 69  backup.c maintai
13a05 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ns the content o
13a06 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 2e  f this variable.
13a07 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a 20   This module.** 
13a08 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c 79  uses it opaquely
13a09 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
13a0a 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70  to sqlite3Backup
13a0b 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a 2a  Restart() and.**
13a0c 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70   sqlite3BackupUp
13a0d 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f 0a  date() only..*/.
13a0e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
13a0f 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a  qlite3_backup **
13a10 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b  sqlite3PagerBack
13a11 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 61  upPtr(Pager *pPa
13a12 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 26  ger){.  return &
13a13 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 3b  pPager->pBackup;
13a14 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
13a15 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f  LITE_OMIT_DISKIO
13a16 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
13a17 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65  **** End of page
13a18 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
13a19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a1b 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
13a1c 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
13a1d 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a  btmutex.c ******
13a1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a20 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
13a21 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a 20  August 27.**.** 
13a22 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
13a23 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
13a24 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
13a25 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
13a26 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
13a27 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
13a28 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
13a29 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
13a2a 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
13a2b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
13a2c 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
13a2d 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
13a2e 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
13a2f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
13a30 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
13a31 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
13a32 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
13a33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a37 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24  ********.**.** $
13a38 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76 20  Id: btmutex.c,v 
13a39 31 2e 31 35 20 32 30 30 39 2f 30 34 2f 31 30 20  1.15 2009/04/10 
13a3a 31 32 3a 35 35 3a 31 37 20 64 61 6e 69 65 6c 6b  12:55:17 danielk
13a3b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a  1977 Exp $.**.**
13a3c 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
13a3d 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
13a3e 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
13a3f 65 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65  es on Btree obje
13a40 63 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64  cts..** This cod
13a41 65 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73  e really belongs
13a42 20 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 42 75   in btree.c.  Bu
13a43 74 20 62 74 72 65 65 2e 63 20 69 73 20 67 65 74  t btree.c is get
13a44 74 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20  ting too.** big 
13a45 61 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 20 62  and we want to b
13a46 72 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d  reak it down som
13a47 65 2e 20 20 54 68 69 73 20 70 61 63 6b 61 67 65  e.  This package
13a48 64 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a  d seemed like.**
13a49 20 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74   a good breakout
13a4a 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..*/./**********
13a4b 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72  **** Include btr
13a4c 65 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d  eeInt.h in the m
13a4d 69 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 65 78  iddle of btmutex
13a4e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
13a4f 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
13a50 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
13a51 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  btreeInt.h *****
13a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a54 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
13a55 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68  April 6.**.** Th
13a56 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
13a57 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
13a58 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
13a59 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
13a5a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
13a5b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
13a5c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
13a5d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
13a5e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
13a5f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
13a60 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
13a61 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
13a62 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
13a63 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
13a64 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
13a65 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
13a66 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
13a67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
13a6b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62  ******.** $Id: b
13a6c 74 72 65 65 49 6e 74 2e 68 2c 76 20 31 2e 34 36  treeInt.h,v 1.46
13a6d 20 32 30 30 39 2f 30 33 2f 32 30 20 31 34 3a 31   2009/03/20 14:1
13a6e 38 3a 35 32 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:52 danielk1977
13a6f 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69   Exp $.**.** Thi
13a70 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
13a71 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69  s a external (di
13a72 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61  sk-based) databa
13a73 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e  se using BTrees.
13a74 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69 6c  .** For a detail
13a75 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66  ed discussion of
13a76 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20 74   BTrees, refer t
13a77 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e 61  o.**.**     Dona
13a78 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48 45  ld E. Knuth, THE
13a79 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45 52   ART OF COMPUTER
13a7a 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56 6f   PROGRAMMING, Vo
13a7b 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20 22  lume 3:.**     "
13a7c 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61 72  Sorting And Sear
13a7d 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34 37  ching", pages 47
13a7e 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d 57  3-480. Addison-W
13a7f 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75 62  esley.**     Pub
13a80 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79 2c  lishing Company,
13a81 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61 63   Reading, Massac
13a82 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  husetts..**.** T
13a83 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69 73  he basic idea is
13a84 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65 20   that each page 
13a85 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74  of the file cont
13a86 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65 0a  ains N database.
13a87 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20 4e  ** entries and N
13a88 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73  +1 pointers to s
13a89 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  ubpages..**.**  
13a8a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
13a8b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a8c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a8d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a8e 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30 29  -.**   |  Ptr(0)
13a8f 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72 28   | Key(0) | Ptr(
13a90 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e 2e  1) | Key(1) | ..
13a91 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20 50  . | Key(N-1) | P
13a92 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d  tr(N) |.**   ---
13a93 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a94 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a95 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13a96 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
13a97 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b  .** All of the k
13a98 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20  eys on the page 
13a99 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69 6e  that Ptr(0) poin
13a9a 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75 65  ts to have value
13a9b 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 4b  s less.** than K
13a9c 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20 74  ey(0).  All of t
13a9d 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65 20  he keys on page 
13a9e 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20 73  Ptr(1) and its s
13a9f 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a 20  ubpages have.** 
13aa0 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 74  values greater t
13aa1 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20 6c  han Key(0) and l
13aa2 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29 2e  ess than Key(1).
13aa3 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79    All of the key
13aa4 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20 61  s.** on Ptr(N) a
13aa5 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 20  nd its subpages 
13aa6 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65 61  have values grea
13aa7 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d 31  ter than Key(N-1
13aa8 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f  ).  And.** so fo
13aa9 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69  rth..**.** Findi
13aaa 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
13aab 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65 61  key requires rea
13aac 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20 70  ding O(log(M)) p
13aad 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a  ages from the .*
13aae 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20 69  * disk where M i
13aaf 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
13ab0 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 74  entries in the t
13ab1 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ree..**.** In th
13ab2 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
13ab3 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65  n, a single file
13ab4 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f 72   can hold one or
13ab5 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 0a   more separate .
13ab6 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63 68  ** BTrees.  Each
13ab7 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74 69   BTree is identi
13ab8 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64 65  fied by the inde
13ab9 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70 61  x of its root pa
13aba 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79 20  ge.  The.** key 
13abb 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e 79  and data for any
13abc 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62 69   entry are combi
13abd 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 20  ned to form the 
13abe 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a  "payload".  A.**
13abf 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66   fixed amount of
13ac0 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65 20   payload can be 
13ac1 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c 79  carried directly
13ac2 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
13ac3 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74 68  .** page.  If th
13ac4 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61 72  e payload is lar
13ac5 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65  ger than the pre
13ac6 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e 20  set amount then 
13ac7 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65 73  surplus.** bytes
13ac8 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 6f   are stored on o
13ac9 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20  verflow pages.  
13aca 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 20  The payload for 
13acb 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64 20  an entry.** and 
13acc 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70 6f  the preceding po
13acd 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69 6e  inter are combin
13ace 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43 65  ed to form a "Ce
13acf 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20 70  ll".  Each .** p
13ad0 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c 20  age has a small 
13ad1 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f 6e  header which con
13ad2 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e 29  tains the Ptr(N)
13ad3 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74 68   pointer and oth
13ad4 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  er.** informatio
13ad5 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73 69  n such as the si
13ad6 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 61  ze of key and da
13ad7 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54  ta..**.** FORMAT
13ad8 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54   DETAILS.**.** T
13ad9 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69 64  he file is divid
13ada 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20 20  ed into pages.  
13adb 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  The first page i
13adc 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2c  s called page 1,
13add 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 69  .** the second i
13ade 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73 6f  s page 2, and so
13adf 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65 20   forth.  A page 
13ae0 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 69  number of zero i
13ae1 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f 20  ndicates.** "no 
13ae2 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68 65  such page".  The
13ae3 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 62   page size can b
13ae4 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 77 65  e anything betwe
13ae5 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 36  en 512 and 65536
13ae6 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 63  ..** Each page c
13ae7 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 62  an be either a b
13ae8 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72 65  tree page, a fre
13ae9 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 61 6e  elist page or an
13aea 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67   overflow.** pag
13aeb 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  e..**.** The fir
13aec 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 61 79  st page is alway
13aed 73 20 61 20 62 74 72 65 65 20 70 61 67 65 2e 20  s a btree page. 
13aee 20 54 68 65 20 66 69 72 73 74 20 31 30 30 20 62   The first 100 b
13aef 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 72 73  ytes of the firs
13af0 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 69  t.** page contai
13af1 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 61 64  n a special head
13af2 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 68 65  er (the "file he
13af3 61 64 65 72 22 29 20 74 68 61 74 20 64 65 73 63  ader") that desc
13af4 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 2e 0a  ribes the file..
13af5 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66  ** The format of
13af6 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 65 72   the file header
13af7 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a   is as follows:.
13af8 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20  **.**   OFFSET  
13af9 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50   SIZE    DESCRIP
13afa 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20  TION.**      0  
13afb 20 20 20 20 31 36 20 20 20 20 20 48 65 61 64 65      16     Heade
13afc 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c 69 74  r string: "SQLit
13afd 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 22 0a  e format 3\000".
13afe 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 20 20  **     16       
13aff 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 65 20  2     Page size 
13b00 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20 20  in bytes.  .**  
13b01 20 20 20 31 38 20 20 20 20 20 20 20 31 20 20 20     18       1   
13b02 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 77 72    File format wr
13b03 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20  ite version.**  
13b04 20 20 20 31 39 20 20 20 20 20 20 20 31 20 20 20     19       1   
13b05 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 72 65    File format re
13b06 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20  ad version.**   
13b07 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20 20    20       1    
13b08 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64   Bytes of unused
13b09 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 6e   space at the en
13b0a 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 0a 2a  d of each page.*
13b0b 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 20 31  *     21       1
13b0c 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 64 65       Max embedde
13b0d 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
13b0e 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 20 20  on.**     22    
13b0f 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 6d 62     1     Min emb
13b10 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72  edded payload fr
13b11 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 33  action.**     23
13b12 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e         1     Min
13b13 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72   leaf payload fr
13b14 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 34  action.**     24
13b15 20 20 20 20 20 20 20 34 20 20 20 20 20 46 69 6c         4     Fil
13b16 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  e change counter
13b17 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 20 20  .**     28      
13b18 20 34 20 20 20 20 20 52 65 73 65 72 76 65 64 20   4     Reserved 
13b19 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 0a 2a  for future use.*
13b1a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 20 34  *     32       4
13b1b 20 20 20 20 20 46 69 72 73 74 20 66 72 65 65 6c       First freel
13b1c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 20 20  ist page.**     
13b1d 33 36 20 20 20 20 20 20 20 34 20 20 20 20 20 4e  36       4     N
13b1e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73  umber of freelis
13b1f 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66  t pages in the f
13b20 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 20 20  ile.**     40   
13b21 20 20 20 36 30 20 20 20 20 20 31 35 20 34 2d 62     60     15 4-b
13b22 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 73 20  yte meta values 
13b23 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 65 72  passed to higher
13b24 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 41 6c   layers.**.** Al
13b25 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  l of the integer
13b26 20 76 61 6c 75 65 73 20 61 72 65 20 62 69 67 2d   values are big-
13b27 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73 69 67  endian (most sig
13b28 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 66 69  nificant byte fi
13b29 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rst)..**.** The 
13b2a 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e  file change coun
13b2b 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ter is increment
13b2c 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  ed when the data
13b2d 62 61 73 65 20 69 73 20 63 68 61 6e 67 65 64 0a  base is changed.
13b2e 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65 72 20  ** This counter 
13b2f 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f  allows other pro
13b30 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77 20 77  cesses to know w
13b31 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68 61 73  hen the file has
13b32 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20   changed.** and 
13b33 74 68 75 73 20 77 68 65 6e 20 74 68 65 79 20 6e  thus when they n
13b34 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74 68 65  eed to flush the
13b35 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20  ir cache..**.** 
13b36 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64 65 64  The max embedded
13b37 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
13b38 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20  n is the amount 
13b39 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75 73 61  of the total usa
13b3a 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69 6e 20  ble.** space in 
13b3b 61 20 70 61 67 65 20 74 68 61 74 20 63 61 6e 20  a page that can 
13b3c 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 61  be consumed by a
13b3d 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66 6f 72   single cell for
13b3e 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42 2d 74   standard.** B-t
13b3f 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 41 54  ree (non-LEAFDAT
13b40 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20 76 61  A) tables.  A va
13b41 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61 6e 73  lue of 255 means
13b42 20 31 30 30 25 2e 20 20 54 68 65 20 64 65 66 61   100%.  The defa
13b43 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c 69 6d  ult.** is to lim
13b44 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63  it the maximum c
13b45 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68 61 74  ell size so that
13b46 20 61 74 20 6c 65 61 73 74 20 34 20 63 65 6c 6c   at least 4 cell
13b47 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e  s will fit.** on
13b48 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68 75 73   one page.  Thus
13b49 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 61 78   the default max
13b4a 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61   embedded payloa
13b4b 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 36 34  d fraction is 64
13b4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
13b4d 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63 65 6c  ayload for a cel
13b4e 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  l is larger than
13b4f 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f 61 64   the max payload
13b50 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a 2a 20  , then extra.** 
13b51 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69 6c 6c  payload is spill
13b52 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70  ed to overflow p
13b53 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e 20 6f  ages.  Once an o
13b54 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20  verflow page is 
13b55 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 61 73  allocated,.** as
13b56 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73 20 70   many bytes as p
13b57 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f 76 65  ossible are move
13b58 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65 72 66  d into the overf
13b59 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68 6f 75  low pages withou
13b5a 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 68 65  t letting.** the
13b5b 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f 70 20   cell size drop 
13b5c 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20 65 6d  below the min em
13b5d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66  bedded payload f
13b5e 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  raction..**.** T
13b5f 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61 79 6c  he min leaf payl
13b60 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20  oad fraction is 
13b61 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65 6d 62  like the min emb
13b62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72  edded payload fr
13b63 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 70 74  action.** except
13b64 20 74 68 61 74 20 69 74 20 61 70 70 6c 69 65 73   that it applies
13b65 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73 20 69   to leaf nodes i
13b66 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65  n a LEAFDATA tre
13b67 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75 6d 0a  e.  The maximum.
13b68 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ** payload fract
13b69 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46 44 41  ion for a LEAFDA
13b6a 54 41 20 74 72 65 65 20 69 73 20 61 6c 77 61 79  TA tree is alway
13b6b 73 20 31 30 30 25 20 28 6f 72 20 32 35 35 29 20  s 100% (or 255) 
13b6c 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 73 70  and it.** not sp
13b6d 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 68  ecified in the h
13b6e 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63  eader..**.** Eac
13b6f 68 20 62 74 72 65 65 20 70 61 67 65 73 20 69 73  h btree pages is
13b70 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 68   divided into th
13b71 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20 20 54  ree sections:  T
13b72 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 0a 2a  he header, the.*
13b73 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  * cell pointer a
13b74 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20 63 65  rray, and the ce
13b75 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e  ll content area.
13b76 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20 68 61    Page 1 also ha
13b77 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a 2a 20  s a 100-byte.** 
13b78 66 69 6c 65 20 68 65 61 64 65 72 20 74 68 61 74  file header that
13b79 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20 74   occurs before t
13b7a 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a  he page header..
13b7b 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d  **.**      |----
13b7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a  ------------|.**
13b7d 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68 65 61        | file hea
13b7e 64 65 72 20 20 20 20 7c 20 20 20 31 30 30 20 62  der    |   100 b
13b7f 79 74 65 73 2e 20 20 50 61 67 65 20 31 20 6f 6e  ytes.  Page 1 on
13b80 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d  ly..**      |---
13b81 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a  -------------|.*
13b82 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20 68 65  *      | page he
13b83 61 64 65 72 20 20 20 20 7c 20 20 20 38 20 62 79  ader    |   8 by
13b84 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73 2e 20  tes for leaves. 
13b85 20 31 32 20 62 79 74 65 73 20 66 6f 72 20 69 6e   12 bytes for in
13b86 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20  terior nodes.** 
13b87 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       |----------
13b88 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20  ------|.**      
13b89 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 20  | cell pointer  
13b8a 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65 73 20   |   |  2 bytes 
13b8b 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72 74 65  per cell.  Sorte
13b8c 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 20 20  d order..**     
13b8d 20 7c 20 61 72 72 61 79 20 20 20 20 20 20 20 20   | array        
13b8e 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73 20 64    |   |  Grows d
13b8f 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20 20 20  ownward.**      
13b90 7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |               
13b91 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20 20 7c   |   v.**      |
13b92 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13b93 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e 61 6c  |.**      | unal
13b94 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a 2a 20  located    |.** 
13b95 20 20 20 20 20 7c 20 73 70 61 63 65 20 20 20 20       | space    
13b96 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20        |.**      
13b97 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  |---------------
13b98 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20 75 70  -|   ^  Grows up
13b99 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20 7c 20  wards.**      | 
13b9a 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 20 7c  cell content   |
13b9b 20 20 20 7c 20 20 41 72 62 69 74 72 61 72 79 20     |  Arbitrary 
13b9c 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65 72 73  order interspers
13b9d 65 64 20 77 69 74 68 20 66 72 65 65 62 6c 6f 63  ed with freebloc
13b9e 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72  ks..**      | ar
13b9f 65 61 20 20 20 20 20 20 20 20 20 20 20 7c 20 20  ea           |  
13ba0 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73 70 61   |  and free spa
13ba1 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a  ce fragments..**
13ba2 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d        |---------
13ba3 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54  -------|.**.** T
13ba4 68 65 20 70 61 67 65 20 68 65 61 64 65 72 73 20  he page headers 
13ba5 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
13ba6 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20  .**.**   OFFSET 
13ba7 20 20 53 49 5a 45 20 20 20 20 20 44 45 53 43 52    SIZE     DESCR
13ba8 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30  IPTION.**      0
13ba9 20 20 20 20 20 20 20 31 20 20 20 20 20 20 46 6c         1      Fl
13baa 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79 2c 20  ags. 1: intkey, 
13bab 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34 3a 20  2: zerodata, 4: 
13bac 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c 65 61  leafdata, 8: lea
13bad 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20  f.**      1     
13bae 20 20 32 20 20 20 20 20 20 62 79 74 65 20 6f 66    2      byte of
13baf 66 73 65 74 20 74 6f 20 74 68 65 20 66 69 72 73  fset to the firs
13bb0 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20  t freeblock.**  
13bb1 20 20 20 20 33 20 20 20 20 20 20 20 32 20 20 20      3       2   
13bb2 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c     number of cel
13bb3 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 65 0a  ls on this page.
13bb4 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20 20 20  **      5       
13bb5 32 20 20 20 20 20 20 66 69 72 73 74 20 62 79 74  2      first byt
13bb6 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  e of the cell co
13bb7 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20 20 20  ntent area.**   
13bb8 20 20 20 37 20 20 20 20 20 20 20 31 20 20 20 20     7       1    
13bb9 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 67    number of frag
13bba 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79 74 65  mented free byte
13bbb 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20  s.**      8     
13bbc 20 20 34 20 20 20 20 20 20 52 69 67 68 74 20 63    4      Right c
13bbd 68 69 6c 64 20 28 74 68 65 20 50 74 72 28 4e 29  hild (the Ptr(N)
13bbe 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74 74 65   value).  Omitte
13bbf 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a  d on leaves..**.
13bc0 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64 65 66  ** The flags def
13bc1 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74 20 6f  ine the format o
13bc2 66 20 74 68 69 73 20 62 74 72 65 65 20 70 61 67  f this btree pag
13bc3 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66 6c 61  e.  The leaf fla
13bc4 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20  g means that.** 
13bc5 74 68 69 73 20 70 61 67 65 20 68 61 73 20 6e 6f  this page has no
13bc6 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68 65 20   children.  The 
13bc7 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20 6d 65  zerodata flag me
13bc8 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 70 61  ans that this pa
13bc9 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20 6f 6e  ge carries.** on
13bca 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f 20 64  ly keys and no d
13bcb 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b 65 79  ata.  The intkey
13bcc 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74   flag means that
13bcd 20 74 68 65 20 6b 65 79 20 69 73 20 61 20 69 6e   the key is a in
13bce 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68 20 69  teger.** which i
13bcf 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
13bd0 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79 20 6f  key size entry o
13bd1 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61 64 65  f the cell heade
13bd2 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 69 6e  r rather than in
13bd3 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61 64 20  .** the payload 
13bd4 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  area..**.** The 
13bd5 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72  cell pointer arr
13bd6 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74 68 65  ay begins on the
13bd7 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65   first byte afte
13bd8 72 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  r the page heade
13bd9 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70  r..** The cell p
13bda 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63 6f 6e  ointer array con
13bdb 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f  tains zero or mo
13bdc 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62 65 72  re 2-byte number
13bdd 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 6f  s which are.** o
13bde 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68 65 20  ffsets from the 
13bdf 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
13be0 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 65 6c   page to the cel
13be1 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65  l content in the
13be2 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74   cell.** content
13be3 20 61 72 65 61 2e 20 20 54 68 65 20 63 65 6c 6c   area.  The cell
13be4 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75 72 20   pointers occur 
13be5 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e  in sorted order.
13be6 20 20 54 68 65 20 73 79 73 74 65 6d 20 73 74 72    The system str
13be7 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 70 20  ives.** to keep 
13be8 66 72 65 65 20 73 70 61 63 65 20 61 66 74 65 72  free space after
13be9 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70   the last cell p
13bea 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74 20 6e  ointer so that n
13beb 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20  ew cells can.** 
13bec 62 65 20 65 61 73 69 6c 79 20 61 64 64 65 64 20  be easily added 
13bed 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74  without having t
13bee 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74 68 65  o defragment the
13bef 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c   page..**.** Cel
13bf0 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f  l content is sto
13bf1 72 65 64 20 61 74 20 74 68 65 20 76 65 72 79 20  red at the very 
13bf2 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 20  end of the page 
13bf3 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61 72 64  and grows toward
13bf4 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e   the.** beginnin
13bf5 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a  g of the page..*
13bf6 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70 61 63  *.** Unused spac
13bf7 65 20 77 69 74 68 69 6e 20 74 68 65 20 63 65 6c  e within the cel
13bf8 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69  l content area i
13bf9 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f  s collected into
13bfa 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
13bfb 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e  f.** freeblocks.
13bfc 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f 63 6b    Each freeblock
13bfd 20 69 73 20 61 74 20 6c 65 61 73 74 20 34 20 62   is at least 4 b
13bfe 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 20 54  ytes in size.  T
13bff 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a 2a  he byte offset.*
13c00 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66  * to the first f
13c01 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69 76 65  reeblock is give
13c02 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e  n in the header.
13c03 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f 63 63    Freeblocks occ
13c04 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 61 73  ur in.** increas
13c05 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65 63 61  ing order.  Beca
13c06 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63 6b 20  use a freeblock 
13c07 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
13c08 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65   4 bytes in size
13c09 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70 20 6f  ,.** any group o
13c0a 66 20 33 20 6f 72 20 66 65 77 65 72 20 75 6e 75  f 3 or fewer unu
13c0b 73 65 64 20 62 79 74 65 73 20 69 6e 20 74 68 65  sed bytes in the
13c0c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
13c0d 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69  ea cannot.** exi
13c0e 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65 62 6c  st on the freebl
13c0f 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20 67 72  ock chain.  A gr
13c10 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65  oup of 3 or fewe
13c11 72 20 66 72 65 65 20 62 79 74 65 73 20 69 73 20  r free bytes is 
13c12 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 61 67  called.** a frag
13c13 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74 61 6c  ment.  The total
13c14 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
13c15 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65 6e 74   in all fragment
13c16 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e 0a 2a  s is recorded..*
13c17 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65  * in the page he
13c18 61 64 65 72 20 61 74 20 6f 66 66 73 65 74 20 37  ader at offset 7
13c19 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20  ..**.**    SIZE 
13c1a 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a     DESCRIPTION.*
13c1b 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 79 74  *      2     Byt
13c1c 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20  e offset of the 
13c1d 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a  next freeblock.*
13c1e 2a 20 20 20 20 20 20 32 20 20 20 20 20 42 79 74  *      2     Byt
13c1f 65 73 20 69 6e 20 74 68 69 73 20 66 72 65 65 62  es in this freeb
13c20 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73  lock.**.** Cells
13c21 20 61 72 65 20 6f 66 20 76 61 72 69 61 62 6c 65   are of variable
13c22 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c 73 20   length.  Cells 
13c23 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68  are stored in th
13c24 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
13c25 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e  rea at.** the en
13c26 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20  d of the page.  
13c27 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  Pointers to the 
13c28 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74 68 65  cells are in the
13c29 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
13c2a 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d 6d 65  ray.** that imme
13c2b 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20  diately follows 
13c2c 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e  the page header.
13c2d 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74 20 6e    Cells is not n
13c2e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 63 6f  ecessarily.** co
13c2f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e 20 6f  ntiguous or in o
13c30 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c 20 70  rder, but cell p
13c31 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f 6e 74  ointers are cont
13c32 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20 6f 72  iguous and in or
13c33 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20  der..**.** Cell 
13c34 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20 75 73  content makes us
13c35 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65  e of variable le
13c36 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20  ngth integers.  
13c37 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6c 65  A variable.** le
13c38 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69 73 20  ngth integer is 
13c39 31 20 74 6f 20 39 20 62 79 74 65 73 20 77 68 65  1 to 9 bytes whe
13c3a 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62  re the lower 7 b
13c3b 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a 2a 20  its of each .** 
13c3c 62 79 74 65 20 61 72 65 20 75 73 65 64 2e 20 20  byte are used.  
13c3d 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  The integer cons
13c3e 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79 74 65  ists of all byte
13c3f 73 20 74 68 61 74 20 68 61 76 65 20 62 69 74 20  s that have bit 
13c40 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74 68 65  8 set and.** the
13c41 20 66 69 72 73 74 20 62 79 74 65 20 77 69 74 68   first byte with
13c42 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20 20 54   bit 8 clear.  T
13c43 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66 69 63  he most signific
13c44 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68 65 20  ant byte of the 
13c45 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70 65 61  integer.** appea
13c46 72 73 20 66 69 72 73 74 2e 20 20 41 20 76 61 72  rs first.  A var
13c47 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
13c48 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62 65 20  eger may not be 
13c49 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79 74 65  more than 9 byte
13c4a 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 61 20  s long..** As a 
13c4b 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20 61 6c  special case, al
13c4c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74 68 65  l 8 bytes of the
13c4d 20 39 74 68 20 62 79 74 65 20 61 72 65 20 75 73   9th byte are us
13c4e 65 64 20 61 73 20 64 61 74 61 2e 20 20 54 68 69  ed as data.  Thi
13c4f 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 36 34  s.** allows a 64
13c50 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74 6f 20  -bit integer to 
13c51 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20 39 20  be encoded in 9 
13c52 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  bytes..**.**    
13c53 30 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20  0x00            
13c54 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
13c55 73 20 20 30 78 30 30 30 30 30 30 30 30 0a 2a 2a  s  0x00000000.**
13c56 20 20 20 20 30 78 37 66 20 20 20 20 20 20 20 20      0x7f        
13c57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
13c58 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 37  comes  0x0000007
13c59 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 78 30  f.**    0x81 0x0
13c5a 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
13c5b 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
13c5c 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78 38 32  00080.**    0x82
13c5d 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20 20   0x00           
13c5e 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
13c5f 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20 20 20  x00000100.**    
13c60 30 78 38 30 20 30 78 37 66 20 20 20 20 20 20 20  0x80 0x7f       
13c61 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
13c62 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a  s  0x0000007f.**
13c63 20 20 20 20 30 78 38 61 20 30 78 39 31 20 30 78      0x8a 0x91 0x
13c64 64 31 20 30 78 61 63 20 30 78 37 38 20 20 62 65  d1 0xac 0x78  be
13c65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35 36 37  comes  0x1234567
13c66 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30 78 38  8.**    0x81 0x8
13c67 31 20 30 78 38 31 20 30 78 38 31 20 30 78 30 31  1 0x81 0x81 0x01
13c68 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 30 32    becomes  0x102
13c69 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69  04081.**.** Vari
13c6a 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65  able length inte
13c6b 67 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f  gers are used fo
13c6c 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74 6f 20  r rowids and to 
13c6d 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65 72 20  hold the number 
13c6e 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 6b  of.** bytes of k
13c6f 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e 20 61  ey and data in a
13c70 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a   btree cell..**.
13c71 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ** The content o
13c72 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c  f a cell looks l
13c73 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
13c74 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
13c75 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
13c76 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
13c77 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63 68 69   of the left chi
13c78 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 6c  ld. Omitted if l
13c79 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 74 2e  eaf flag is set.
13c7a 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e  .**     var    N
13c7b 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
13c7c 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65 64 20  f data. Omitted 
13c7d 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74 61 20  if the zerodata 
13c7e 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20  flag is set..** 
13c7f 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65      var    Numbe
13c80 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6b 65  r of bytes of ke
13c81 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20 69 74  y. Or the key it
13c82 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79 20 66  self if intkey f
13c83 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20  lag is set..**  
13c84 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c 6f 61      *     Payloa
13c85 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20  d.**      4     
13c86 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68  First page of th
13c87 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e  e overflow chain
13c88 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20 6e 6f  .  Omitted if no
13c89 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20   overflow.**.** 
13c8a 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 66  Overflow pages f
13c8b 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  orm a linked lis
13c8c 74 2e 20 20 45 61 63 68 20 70 61 67 65 20 65 78  t.  Each page ex
13c8d 63 65 70 74 20 74 68 65 20 6c 61 73 74 20 69 73  cept the last is
13c8e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 66   completely.** f
13c8f 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74 61 20  illed with data 
13c90 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20 62 79  (pagesize - 4 by
13c91 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73 74 20  tes).  The last 
13c92 70 61 67 65 20 63 61 6e 20 68 61 76 65 20 61 73  page can have as
13c93 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20 31 20   little.** as 1 
13c94 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a 2a 2a  byte of data..**
13c95 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44  .**    SIZE    D
13c96 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20  ESCRIPTION.**   
13c97 20 20 20 34 20 20 20 20 20 50 61 67 65 20 6e 75     4     Page nu
13c98 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f 76 65  mber of next ove
13c99 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 20 20  rflow page.**   
13c9a 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a 2a 2a     *     Data.**
13c9b 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70 61 67  .** Freelist pag
13c9c 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20 73  es come in two s
13c9d 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b 20 70  ubtypes: trunk p
13c9e 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20 70 61  ages and leaf pa
13c9f 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 6c  ges.  The.** fil
13ca0 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74 73 20  e header points 
13ca1 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e 20  to the first in 
13ca2 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  a linked list of
13ca3 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20 45 61   trunk page.  Ea
13ca4 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 67 65  ch trunk.** page
13ca5 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c 74 69   points to multi
13ca6 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73 2e 20  ple leaf pages. 
13ca7 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   The content of 
13ca8 61 20 6c 65 61 66 20 70 61 67 65 20 69 73 0a 2a  a leaf page is.*
13ca9 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e 20 20  * unspecified.  
13caa 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c 6f 6f  A trunk page loo
13cab 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ks like this:.**
13cac 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44  .**    SIZE    D
13cad 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20  ESCRIPTION.**   
13cae 20 20 20 34 20 20 20 20 20 50 61 67 65 20 6e 75     4     Page nu
13caf 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74 72 75  mber of next tru
13cb0 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20  nk page.**      
13cb1 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20  4     Number of 
13cb2 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20 6f 6e  leaf pointers on
13cb3 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20   this page.**   
13cb4 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20 6f 72     *     zero or
13cb5 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75 6d 62   more pages numb
13cb6 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a 2a 2f  ers of leaves.*/
13cb7 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .../* The follow
13cb8 69 6e 67 20 76 61 6c 75 65 20 69 73 20 74 68 65  ing value is the
13cb9 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69   maximum cell si
13cba 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61  ze assuming a ma
13cbb 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69  ximum page.** si
13cbc 7a 65 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a  ze give above..*
13cbd 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c  /.#define MX_CEL
13cbe 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 28 70 42  L_SIZE(pBt)  (pB
13cbf 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 0a 0a  t->pageSize-8)..
13cc0 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  /* The maximum n
13cc1 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
13cc2 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20  n a single page 
13cc3 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
13cc4 20 20 54 68 69 73 0a 2a 2a 20 61 73 73 75 6d 65    This.** assume
13cc5 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c  s a minimum cell
13cc6 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74 65 73   size of 6 bytes
13cc7 20 20 28 34 20 62 79 74 65 73 20 66 6f 72 20 74    (4 bytes for t
13cc8 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 0a 2a  he cell itself.*
13cc9 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73 20 66  * plus 2 bytes f
13cca 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20  or the index to 
13ccb 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20  the cell in the 
13ccc 70 61 67 65 20 68 65 61 64 65 72 29 2e 20 20 53  page header).  S
13ccd 75 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c  uch.** small cel
13cce 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72 65 2c  ls will be rare,
13ccf 20 62 75 74 20 74 68 65 79 20 61 72 65 20 70 6f   but they are po
13cd0 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69  ssible..*/.#defi
13cd1 6e 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20  ne MX_CELL(pBt) 
13cd2 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d  ((pBt->pageSize-
13cd3 38 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 61 72  8)/6)../* Forwar
13cd4 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a  d declarations *
13cd5 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
13cd6 20 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61 67 65   MemPage MemPage
13cd7 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
13cd8 20 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a   BtLock BtLock;.
13cd9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ./*.** This is a
13cda 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 74 68   magic string th
13cdb 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68  at appears at th
13cdc 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65  e beginning of e
13cdd 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64  very.** SQLite d
13cde 61 74 61 62 61 73 65 20 69 6e 20 6f 72 64 65 72  atabase in order
13cdf 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65   to identify the
13ce0 20 66 69 6c 65 20 61 73 20 61 20 72 65 61 6c 20   file as a real 
13ce1 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
13ce2 59 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74  You can change t
13ce3 68 69 73 20 76 61 6c 75 65 20 61 74 20 63 6f 6d  his value at com
13ce4 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 73 70 65  pile-time by spe
13ce5 63 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d 44 53  cifying a.** -DS
13ce6 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45  QLITE_FILE_HEADE
13ce7 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 20 63  R="..." on the c
13ce8 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d  ompiler command-
13ce9 6c 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 68 65  line.  The.** he
13cea 61 64 65 72 20 6d 75 73 74 20 62 65 20 65 78 61  ader must be exa
13ceb 63 74 6c 79 20 31 36 20 62 79 74 65 73 20 69 6e  ctly 16 bytes in
13cec 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65 72 6f  cluding the zero
13ced 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a  -terminator so.*
13cee 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69 74 73  * the string its
13cef 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20 31 35  elf should be 15
13cf0 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67   characters long
13cf1 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e 67 65  .  If you change
13cf2 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20  .** the header, 
13cf3 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74 6f 6d  then your custom
13cf4 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 6e 6f   library will no
13cf5 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61  t be able to rea
13cf6 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20  d .** databases 
13cf7 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65  generated by the
13cf8 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 20   standard tools 
13cf9 61 6e 64 20 74 68 65 20 73 74 61 6e 64 61 72 64  and the standard
13cfa 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e   tools.** will n
13cfb 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65  ot be able to re
13cfc 61 64 20 64 61 74 61 62 61 73 65 73 20 63 72 65  ad databases cre
13cfd 61 74 65 64 20 62 79 20 79 6f 75 72 20 63 75 73  ated by your cus
13cfe 74 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  tom library..*/.
13cff 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 46  #ifndef SQLITE_F
13d00 49 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20 31 32  ILE_HEADER /* 12
13d01 33 34 35 36 37 38 39 20 31 32 33 34 35 36 20 2a  3456789 123456 *
13d02 2f 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49  /.#  define SQLI
13d03 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 22  TE_FILE_HEADER "
13d04 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 22  SQLite format 3"
13d05 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50  .#endif../*.** P
13d06 61 67 65 20 74 79 70 65 20 66 6c 61 67 73 2e 20  age type flags. 
13d07 20 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69 6e 61   An ORed combina
13d08 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 66 6c  tion of these fl
13d09 61 67 73 20 61 70 70 65 61 72 20 61 73 20 74 68  ags appear as th
13d0a 65 0a 2a 2a 20 66 69 72 73 74 20 62 79 74 65 20  e.** first byte 
13d0b 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 67 65  of on-disk image
13d0c 20 6f 66 20 65 76 65 72 79 20 42 54 72 65 65 20   of every BTree 
13d0d 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  page..*/.#define
13d0e 20 50 54 46 5f 49 4e 54 4b 45 59 20 20 20 20 30   PTF_INTKEY    0
13d0f 78 30 31 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  x01.#define PTF_
13d10 5a 45 52 4f 44 41 54 41 20 20 30 78 30 32 0a 23  ZERODATA  0x02.#
13d11 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 44  define PTF_LEAFD
13d12 41 54 41 20 20 30 78 30 34 0a 23 64 65 66 69 6e  ATA  0x04.#defin
13d13 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20 20 20  e PTF_LEAF      
13d14 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65  0x08../*.** As e
13d15 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20  ach page of the 
13d16 66 69 6c 65 20 69 73 20 6c 6f 61 64 65 64 20 69  file is loaded i
13d17 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e 20 69  nto memory, an i
13d18 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
13d19 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75  ollowing.** stru
13d1a 63 74 75 72 65 20 69 73 20 61 70 70 65 6e 64 65  cture is appende
13d1b 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  d and initialize
13d1c 64 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 69 73  d to zero.  This
13d1d 20 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65   structure store
13d1e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  s.** information
13d1f 20 61 62 6f 75 74 20 74 68 65 20 70 61 67 65 20   about the page 
13d20 74 68 61 74 20 69 73 20 64 65 63 6f 64 65 64 20  that is decoded 
13d21 66 72 6f 6d 20 74 68 65 20 72 61 77 20 66 69 6c  from the raw fil
13d22 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e page..**.** Th
13d23 65 20 70 50 61 72 65 6e 74 20 66 69 65 6c 64 20  e pParent field 
13d24 70 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f 20 74  points back to t
13d25 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20  he parent page. 
13d26 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 75 73 20   This allows us 
13d27 74 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 74 68  to.** walk up th
13d28 65 20 42 54 72 65 65 20 66 72 6f 6d 20 61 6e 79  e BTree from any
13d29 20 6c 65 61 66 20 74 6f 20 74 68 65 20 72 6f 6f   leaf to the roo
13d2a 74 2e 20 20 43 61 72 65 20 6d 75 73 74 20 62 65  t.  Care must be
13d2b 20 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72   taken to.** unr
13d2c 65 66 28 29 20 74 68 65 20 70 61 72 65 6e 74 20  ef() the parent 
13d2d 70 61 67 65 20 70 6f 69 6e 74 65 72 20 77 68 65  page pointer whe
13d2e 6e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 6e  n this page is n
13d2f 6f 20 6c 6f 6e 67 65 72 20 72 65 66 65 72 65 6e  o longer referen
13d30 63 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65  ced..** The page
13d31 44 65 73 74 72 75 63 74 6f 72 28 29 20 72 6f 75  Destructor() rou
13d32 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 74 68 61  tine handles tha
13d33 74 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41  t chore..**.** A
13d34 63 63 65 73 73 20 74 6f 20 61 6c 6c 20 66 69 65  ccess to all fie
13d35 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75  lds of this stru
13d36 63 74 75 72 65 20 69 73 20 63 6f 6e 74 72 6f 6c  cture is control
13d37 6c 65 64 20 62 79 20 74 68 65 20 6d 75 74 65 78  led by the mutex
13d38 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 4d 65  .** stored in Me
13d39 6d 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74 65 78  mPage.pBt->mutex
13d3a 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 50  ..*/.struct MemP
13d3b 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49 6e 69  age {.  u8 isIni
13d3c 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
13d3d 54 72 75 65 20 69 66 20 70 72 65 76 69 6f 75 73  True if previous
13d3e 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  ly initialized. 
13d3f 4d 55 53 54 20 42 45 20 46 49 52 53 54 21 20 2a  MUST BE FIRST! *
13d40 2f 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c 6f 77  /.  u8 nOverflow
13d41 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
13d42 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 63  er of overflow c
13d43 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20 61 43  ell bodies in aC
13d44 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 69 6e  ell[] */.  u8 in
13d45 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  tKey;           
13d46 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 6b 65  /* True if intke
13d47 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f  y flag is set */
13d48 0a 20 20 75 38 20 6c 65 61 66 3b 20 20 20 20 20  .  u8 leaf;     
13d49 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
13d4a 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20  if leaf flag is 
13d4b 73 65 74 20 2a 2f 0a 20 20 75 38 20 68 61 73 44  set */.  u8 hasD
13d4c 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ata;          /*
13d4d 20 54 72 75 65 20 69 66 20 74 68 69 73 20 70 61   True if this pa
13d4e 67 65 20 73 74 6f 72 65 73 20 64 61 74 61 20 2a  ge stores data *
13d4f 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73 65 74  /.  u8 hdrOffset
13d50 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30 30 20  ;        /* 100 
13d51 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30 20 6f  for page 1.  0 o
13d52 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20 75 38  therwise */.  u8
13d53 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 20 20   childPtrSize;  
13d54 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61 66 3d     /* 0 if leaf=
13d55 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66 3d 3d  =1.  4 if leaf==
13d56 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f  0 */.  u16 maxLo
13d57 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  cal;        /* C
13d58 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e  opy of BtShared.
13d59 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68  maxLocal or BtSh
13d5a 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a  ared.maxLeaf */.
13d5b 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20    u16 minLocal; 
13d5c 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
13d5d 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 6f  f BtShared.minLo
13d5e 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e  cal or BtShared.
13d5f 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36  minLeaf */.  u16
13d60 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20   cellOffset;    
13d61 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 44    /* Index in aD
13d62 61 74 61 20 6f 66 20 66 69 72 73 74 20 63 65 6c  ata of first cel
13d63 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75  l pointer */.  u
13d64 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20  16 nFree;       
13d65 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
13d66 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e 20 74   free bytes on t
13d67 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36  he page */.  u16
13d68 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20   nCell;         
13d69 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
13d6a 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67  ells on this pag
13d6b 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f 76 66  e, local and ovf
13d6c 6c 20 2a 2f 0a 20 20 75 31 36 20 6d 61 73 6b 50  l */.  u16 maskP
13d6d 61 67 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  age;        /* M
13d6e 61 73 6b 20 66 6f 72 20 70 61 67 65 20 6f 66 66  ask for page off
13d6f 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  set */.  struct 
13d70 5f 4f 76 66 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a  _OvflCell {   /*
13d71 20 43 65 6c 6c 73 20 74 68 61 74 20 77 69 6c 6c   Cells that will
13d72 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 44 61 74   not fit on aDat
13d73 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70  a[] */.    u8 *p
13d74 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f  Cell;          /
13d75 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  * Pointers to th
13d76 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 6f 76  e body of the ov
13d77 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20  erflow cell */. 
13d78 20 20 20 75 31 36 20 69 64 78 3b 20 20 20 20 20     u16 idx;     
13d79 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74         /* Insert
13d7a 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66 6f 72   this cell befor
13d7b 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65  e idx-th non-ove
13d7c 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20  rflow cell */.  
13d7d 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 42 74  } aOvfl[5];.  Bt
13d7e 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20  Shared *pBt;    
13d7f 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
13d80 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 74   BtShared that t
13d81 68 69 73 20 70 61 67 65 20 69 73 20 70 61 72 74  his page is part
13d82 20 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61   of */.  u8 *aDa
13d83 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ta;           /*
13d84 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69 73 6b   Pointer to disk
13d85 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 70 61   image of the pa
13d86 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 44 62 50  ge data */.  DbP
13d87 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20  age *pDbPage;   
13d88 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67 65 20    /* Pager page 
13d89 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  handle */.  Pgno
13d8a 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
13d8b 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
13d8c 66 6f 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f  for this page */
13d8d 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69  .};../*.** The i
13d8e 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f  n-memory image o
13d8f 66 20 61 20 64 69 73 6b 20 70 61 67 65 20 68 61  f a disk page ha
13d90 73 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  s the auxiliary 
13d91 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 65  information appe
13d92 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 65  nded.** to the e
13d93 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a 45 20  nd.  EXTRA_SIZE 
13d94 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
13d95 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
13d96 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 0a 2a  needed to hold.*
13d97 2a 20 74 68 61 74 20 65 78 74 72 61 20 69 6e 66  * that extra inf
13d98 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65  ormation..*/.#de
13d99 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a 45 20  fine EXTRA_SIZE 
13d9a 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 0a  sizeof(MemPage).
13d9b 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 6e 64  ./* A Btree hand
13d9c 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62  le.**.** A datab
13d9d 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
13d9e 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
13d9f 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
13da0 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65   of.** this obje
13da1 63 74 20 66 6f 72 20 65 76 65 72 79 20 64 61 74  ct for every dat
13da2 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20  abase file that 
13da3 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20 54 68  it has open.  Th
13da4 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
13da5 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74 68 65  is opaque to the
13da6 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
13da7 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74 61 62  tion.  The datab
13da8 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
13da9 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65  annot.** see the
13daa 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68   internals of th
13dab 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  is structure and
13dac 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68   only deals with
13dad 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
13dae 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a  this structure..
13daf 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64  **.** For some d
13db0 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c 20 74  atabase files, t
13db1 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c 79 69  he same underlyi
13db2 6e 67 20 64 61 74 61 62 61 73 65 20 63 61 63 68  ng database cach
13db3 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a 20 73  e might be .** s
13db4 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 6d 75  hared between mu
13db5 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f  ltiple connectio
13db6 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73  ns.  In that cas
13db7 65 2c 20 65 61 63 68 20 63 6f 6e 74 65 63 74 69  e, each contecti
13db8 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e  on.** has it own
13db9 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
13dba 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61   object.  But ea
13dbb 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ch instance of t
13dbc 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f  his object.** po
13dbd 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ints to the same
13dbe 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
13dbf 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20  .  The database 
13dc0 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a  cache and the.**
13dc1 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74   schema associat
13dc2 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ed with the data
13dc3 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c  base file are al
13dc4 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  l contained with
13dc5 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72  in.** the BtShar
13dc6 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  ed object..**.**
13dc7 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74   All fields in t
13dc8 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72  his structure ar
13dc9 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72  e accessed under
13dca 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a   sqlite3.mutex..
13dcb 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74  ** The pBt point
13dcc 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f  er itself may no
13dcd 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 69  t be changed whi
13dce 6c 65 20 74 68 65 72 65 20 65 78 69 73 74 73 20  le there exists 
13dcf 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74  cursors .** in t
13dd0 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42 74  he referenced Bt
13dd1 53 68 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e  Shared that poin
13dd2 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42  t back to this B
13dd3 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65  tree since those
13dd4 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65  .** cursors have
13dd5 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75 67   to do go throug
13dd6 68 20 74 68 69 73 20 42 74 72 65 65 20 74 6f 20  h this Btree to 
13dd7 66 69 6e 64 20 74 68 65 69 72 20 42 74 53 68 61  find their BtSha
13dd8 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 20  red and.** they 
13dd9 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 68  often do so with
13dda 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c 69  out holding sqli
13ddb 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74  te3.mutex..*/.st
13ddc 72 75 63 74 20 42 74 72 65 65 20 7b 0a 20 20 73  ruct Btree {.  s
13ddd 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
13dde 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
13ddf 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c  e connection hol
13de0 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 65 20  ding this btree 
13de1 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
13de2 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 61  Bt;     /* Shara
13de3 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  ble content of t
13de4 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75  his btree */.  u
13de5 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20  8 inTrans;      
13de6 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 2c    /* TRANS_NONE,
13de7 20 54 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 54   TRANS_READ or T
13de8 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 20  RANS_WRITE */.  
13de9 75 38 20 73 68 61 72 61 62 6c 65 3b 20 20 20 20  u8 sharable;    
13dea 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65     /* True if we
13deb 20 63 61 6e 20 73 68 61 72 65 20 70 42 74 20 77   can share pBt w
13dec 69 74 68 20 61 6e 6f 74 68 65 72 20 64 62 20 2a  ith another db *
13ded 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20  /.  u8 locked;  
13dee 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
13def 66 20 64 62 20 63 75 72 72 65 6e 74 6c 79 20 68  f db currently h
13df0 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a 2f  as pBt locked */
13df1 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f 63  .  int wantToLoc
13df2 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  k;    /* Number 
13df3 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20  of nested calls 
13df4 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  to sqlite3BtreeE
13df5 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69 6e 74 20  nter() */.  int 
13df6 6e 42 61 63 6b 75 70 3b 20 20 20 20 20 20 20 2f  nBackup;       /
13df7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b  * Number of back
13df8 75 70 20 6f 70 65 72 61 74 69 6f 6e 73 20 72 65  up operations re
13df9 61 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 65  ading this btree
13dfa 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4e 65   */.  Btree *pNe
13dfb 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  xt;      /* List
13dfc 20 6f 66 20 6f 74 68 65 72 20 73 68 61 72 61 62   of other sharab
13dfd 6c 65 20 42 74 72 65 65 73 20 66 72 6f 6d 20 74  le Btrees from t
13dfe 68 65 20 73 61 6d 65 20 64 62 20 2a 2f 0a 20 20  he same db */.  
13dff 42 74 72 65 65 20 2a 70 50 72 65 76 3b 20 20 20  Btree *pPrev;   
13e00 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f 69 6e 74     /* Back point
13e01 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c  er of the same l
13e02 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ist */.};../*.**
13e03 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20 6d   Btree.inTrans m
13e04 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20 74  ay take one of t
13e05 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  he following val
13e06 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ues..**.** If th
13e07 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 65 78  e shared-data ex
13e08 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62 6c  tension is enabl
13e09 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65  ed, there may be
13e0a 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73 0a   multiple users.
13e0b 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65 20  ** of the Btree 
13e0c 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d 6f  structure. At mo
13e0d 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20  st one of these 
13e0e 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74 65  may open a write
13e0f 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a   transaction,.**
13e10 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72 20   but any number 
13e11 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 65 20  may have active 
13e12 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e  read transaction
13e13 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52  s..*/.#define TR
13e14 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 66  ANS_NONE  0.#def
13e15 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 20  ine TRANS_READ  
13e16 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f  1.#define TRANS_
13e17 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41  WRITE 2../*.** A
13e18 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
13e19 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73  is object repres
13e1a 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64 61  ents a single da
13e1b 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
13e1c 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74  .** A single dat
13e1d 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 62  abase file can b
13e1e 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 65 20  e in use as the 
13e1f 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 6f  same time by two
13e20 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 61  .** or more data
13e21 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
13e22 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d  .  When two or m
13e23 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ore connections 
13e24 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 74  are.** sharing t
13e25 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
13e26 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e 6e   file, each conn
13e27 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f 77  ection has it ow
13e28 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74 72  n.** private Btr
13e29 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68  ee object for th
13e2a 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68 20  e file and each 
13e2b 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 73 20  of those Btrees 
13e2c 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 69  points.** to thi
13e2d 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20 6f  s one BtShared o
13e2e 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65 64  bject.  BtShared
13e2f 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75 6d  .nRef is the num
13e30 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63  ber of.** connec
13e31 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20  tions currently 
13e32 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 61 74  sharing this dat
13e33 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
13e34 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73  * Fields in this
13e35 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61   structure are a
13e36 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68  ccessed under th
13e37 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78  e BtShared.mutex
13e38 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65 70  .** mutex, excep
13e39 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20 70  t for nRef and p
13e3a 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 20 61  Next which are a
13e3b 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68  ccessed under th
13e3c 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c 49  e.** global SQLI
13e3d 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
13e3e 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 54  MASTER mutex.  T
13e3f 68 65 20 70 50 61 67 65 72 20 66 69 65 6c 64 0a  he pPager field.
13e40 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f  ** may not be mo
13e41 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20 69  dified once it i
13e42 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74 20  s initially set 
13e43 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e  as long as nRef>
13e44 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65 6d  0..** The pSchem
13e45 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20 73  a field may be s
13e46 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42 74  et once under Bt
13e47 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e 64  Shared.mutex and
13e48 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20 69  .** thereafter i
13e49 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20 6c  s unchanged as l
13e4a 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a  ong as nRef>0..*
13e4b 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 3a 0a  *.** isPending:.
13e4c 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 74 53  **.**   If a BtS
13e4d 68 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61 69  hared client fai
13e4e 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77  ls to obtain a w
13e4f 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20 64  rite-lock on a d
13e50 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 61 62  atabase.**   tab
13e51 6c 65 20 28 62 65 63 61 75 73 65 20 74 68 65 72  le (because ther
13e52 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 20  e exists one or 
13e53 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73 20  more read-locks 
13e54 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0a 2a  on the table),.*
13e55 2a 20 20 20 74 68 65 20 73 68 61 72 65 64 2d 63  *   the shared-c
13e56 61 63 68 65 20 65 6e 74 65 72 73 20 27 70 65 6e  ache enters 'pen
13e57 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65  ding-lock' state
13e58 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20 69   and isPending i
13e59 73 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 74 72  s.**   set to tr
13e5a 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20  ue..**.**   The 
13e5b 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 61  shared-cache lea
13e5c 76 65 73 20 74 68 65 20 27 70 65 6e 64 69 6e 67  ves the 'pending
13e5d 20 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 68 65   lock' state whe
13e5e 6e 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 20  n either of.**  
13e5f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f   the following o
13e60 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ccur:.**.**     
13e61 31 29 20 54 68 65 20 63 75 72 72 65 6e 74 20 77  1) The current w
13e62 72 69 74 65 72 20 28 42 74 53 68 61 72 65 64 2e  riter (BtShared.
13e63 70 57 72 69 74 65 72 29 20 63 6f 6e 63 6c 75 64  pWriter) conclud
13e64 65 73 20 69 74 73 20 74 72 61 6e 73 61 63 74 69  es its transacti
13e65 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 32 29  on, OR.**     2)
13e66 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   The number of l
13e67 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 6f 74 68  ocks held by oth
13e68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 64  er connections d
13e69 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a  rops to zero..**
13e6a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e 20 74  .**   while in t
13e6b 68 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b  he 'pending-lock
13e6c 27 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f 6e 6e  ' state, no conn
13e6d 65 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 72 74  ection may start
13e6e 20 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 61 6e   a new.**   tran
13e6f 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20  saction..**.**  
13e70 20 54 68 69 73 20 66 65 61 74 75 72 65 20 69 73   This feature is
13e71 20 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 65 6c   included to hel
13e72 70 20 70 72 65 76 65 6e 74 20 77 72 69 74 65 72  p prevent writer
13e73 2d 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a 2f 0a  -starvation..*/.
13e74 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20  struct BtShared 
13e75 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  {.  Pager *pPage
13e76 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r;        /* The
13e77 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a 20   page cache */. 
13e78 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
13e79 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
13e7a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75  se connection cu
13e7b 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74 68  rrently using th
13e7c 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 42 74  is Btree */.  Bt
13e7d 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b  Cursor *pCursor;
13e7e 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66      /* A list of
13e7f 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72   all open cursor
13e80 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  s */.  MemPage *
13e81 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20  pPage1;      /* 
13e82 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68  First page of th
13e83 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
13e84 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20  u8 readOnly;    
13e85 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
13e86 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
13e87 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c 79  file is readonly
13e88 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 69 7a   */.  u8 pageSiz
13e89 65 46 69 78 65 64 3b 20 20 20 20 20 2f 2a 20 54  eFixed;     /* T
13e8a 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20  rue if the page 
13e8b 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67  size can no long
13e8c 65 72 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f  er be changed */
13e8d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
13e8e 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
13e8f 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d 3b    u8 autoVacuum;
13e90 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
13e91 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69  if auto-vacuum i
13e92 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75  s enabled */.  u
13e93 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 20  8 incrVacuum;   
13e94 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
13e95 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 65  incr-vacuum is e
13e96 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69 66  nabled */.#endif
13e97 0a 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 3b  .  u16 pageSize;
13e98 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
13e99 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  l number of byte
13e9a 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20  s on a page */. 
13e9b 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 3b   u16 usableSize;
13e9c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
13e9d 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73   of usable bytes
13e9e 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f   on each page */
13e9f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b  .  u16 maxLocal;
13ea0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69           /* Maxi
13ea1 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61  mum local payloa
13ea2 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54  d in non-LEAFDAT
13ea3 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31  A tables */.  u1
13ea4 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20  6 minLocal;     
13ea5 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c      /* Minimum l
13ea6 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20  ocal payload in 
13ea7 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62  non-LEAFDATA tab
13ea8 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78  les */.  u16 max
13ea9 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f  Leaf;          /
13eaa 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20  * Maximum local 
13eab 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41  payload in a LEA
13eac 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20  FDATA table */. 
13ead 20 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20 20   u16 minLeaf;   
13eae 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75         /* Minimu
13eaf 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20  m local payload 
13eb0 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61  in a LEAFDATA ta
13eb1 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72  ble */.  u8 inTr
13eb2 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f  ansaction;     /
13eb3 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 73 74  * Transaction st
13eb4 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72  ate */.  int nTr
13eb5 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f  ansaction;     /
13eb6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e  * Number of open
13eb7 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 72   transactions (r
13eb8 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f 0a  ead + write) */.
13eb9 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 3b    void *pSchema;
13eba 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
13ebb 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c 6f  er to space allo
13ebc 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
13ebd 42 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a 2f  BtreeSchema() */
13ebe 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 53  .  void (*xFreeS
13ebf 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 20  chema)(void*);  
13ec0 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f  /* Destructor fo
13ec1 72 20 42 74 53 68 61 72 65 64 2e 70 53 63 68 65  r BtShared.pSche
13ec2 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ma */.  sqlite3_
13ec3 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f 2a  mutex *mutex; /*
13ec4 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d   Non-recursive m
13ec5 75 74 65 78 20 72 65 71 75 69 72 65 64 20 74 6f  utex required to
13ec6 20 61 63 63 65 73 73 20 74 68 69 73 20 73 74 72   access this str
13ec7 75 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 63 20  uct */.  Bitvec 
13ec8 2a 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 20 2f  *pHasContent;  /
13ec9 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 6d  * Set of pages m
13eca 6f 76 65 64 20 74 6f 20 66 72 65 65 2d 6c 69 73  oved to free-lis
13ecb 74 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  t this transacti
13ecc 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  on */.#ifndef SQ
13ecd 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
13ece 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52 65  _CACHE.  int nRe
13ecf 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  f;             /
13ed0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  * Number of refe
13ed1 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 73  rences to this s
13ed2 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74  tructure */.  Bt
13ed3 53 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20 20  Shared *pNext;  
13ed4 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 61      /* Next on a
13ed5 20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62 6c   list of sharabl
13ed6 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  e BtShared struc
13ed7 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a  ts */.  BtLock *
13ed8 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a  pLock;        /*
13ed9 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 68   List of locks h
13eda 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 72  eld on this shar
13edb 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 20  ed-btree struct 
13edc 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 72 69  */.  Btree *pWri
13edd 74 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 42 74  ter;       /* Bt
13ede 72 65 65 20 77 69 74 68 20 63 75 72 72 65 6e 74  ree with current
13edf 6c 79 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72  ly open write tr
13ee0 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75  ansaction */.  u
13ee1 38 20 69 73 45 78 63 6c 75 73 69 76 65 3b 20 20  8 isExclusive;  
13ee2 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
13ee3 70 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 45  pWriter has an E
13ee4 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e  XCLUSIVE lock on
13ee5 20 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 38 20   the db */.  u8 
13ee6 69 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20  isPending;      
13ee7 20 20 20 2f 2a 20 49 66 20 77 61 69 74 69 6e 67     /* If waiting
13ee8 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73 20   for read-locks 
13ee9 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 6e 64  to clear */.#end
13eea 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70 61  if.  u8 *pTmpSpa
13eeb 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74  ce;        /* Bt
13eec 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 20  Shared.pageSize 
13eed 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66  bytes of space f
13eee 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d 3b  or tmp use */.};
13eef 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
13ef0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
13ef1 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69  wing structure i
13ef2 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69  s used to hold i
13ef3 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62  nformation.** ab
13ef4 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 65  out a cell.  The
13ef5 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20   parseCellPtr() 
13ef6 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 69  function fills i
13ef7 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
13ef8 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e 66  .** based on inf
13ef9 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63 74  ormation extract
13efa 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64 69   from the raw di
13efb 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70 65  sk page..*/.type
13efc 64 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c 49  def struct CellI
13efd 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73 74  nfo CellInfo;.st
13efe 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b 0a  ruct CellInfo {.
13eff 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20    u8 *pCell;    
13f00 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
13f01 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c 6c  he start of cell
13f02 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 36   content */.  i6
13f03 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20  4 nKey;      /* 
13f04 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 4b  The key for INTK
13f05 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e 75  EY tables, or nu
13f06 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
13f07 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e 44   key */.  u32 nD
13f08 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ata;     /* Numb
13f09 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64  er of bytes of d
13f0a 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61  ata */.  u32 nPa
13f0b 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61 6c  yload;  /* Total
13f0c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f   amount of paylo
13f0d 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65 61  ad */.  u16 nHea
13f0e 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f  der;   /* Size o
13f0f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  f the cell conte
13f10 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 79 74  nt header in byt
13f11 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f 63  es */.  u16 nLoc
13f12 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74  al;    /* Amount
13f13 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
13f14 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75 31   locally */.  u1
13f15 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a 20  6 iOverflow; /* 
13f16 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66 6c  Offset to overfl
13f17 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20  ow page number. 
13f18 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65 72   Zero if no over
13f19 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e 53  flow */.  u16 nS
13f1a 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65  ize;     /* Size
13f1b 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   of the cell con
13f1c 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e  tent on the main
13f1d 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f 0a   b-tree page */.
13f1e 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75  };../*.** Maximu
13f1f 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53 51  m depth of an SQ
13f20 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72 75  Lite B-Tree stru
13f21 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72 65  cture. Any B-Tre
13f22 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a 2a  e deeper than.**
13f23 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64 65   this will be de
13f24 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e 20  clared corrupt. 
13f25 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63 61  This value is ca
13f26 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20 6f  lculated based o
13f27 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 64  n a.** maximum d
13f28 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66 20  atabase size of 
13f29 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69 6e  2^31 pages a min
13f2a 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20 32  imum fanout of 2
13f2b 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d 6e   for a.** root-n
13f2c 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61 6c  ode and 3 for al
13f2d 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61 6c  l other internal
13f2e 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   nodes..**.** If
13f2f 20 61 20 74 72 65 65 20 74 68 61 74 20 61 70 70   a tree that app
13f30 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c 65  ears to be talle
13f31 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 20 65  r than this is e
13f32 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20 69  ncountered, it i
13f33 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61  s.** assumed tha
13f34 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  t the database i
13f35 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23 64  s corrupt..*/.#d
13f36 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f 4d  efine BTCURSOR_M
13f37 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a 0a  AX_DEPTH 20../*.
13f38 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61  ** A cursor is a
13f39 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 61   pointer to a pa
13f3a 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20 77  rticular entry w
13f3b 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75 6c  ithin a particul
13f3c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69 74  ar.** b-tree wit
13f3d 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  hin a database f
13f3e 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  ile..**.** The e
13f3f 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66 69  ntry is identifi
13f40 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61 67  ed by its MemPag
13f41 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20  e and the index 
13f42 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 43  in.** MemPage.aC
13f43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e 74  ell[] of the ent
13f44 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  ry..**.** When a
13f45 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
13f46 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65 64   file can shared
13f47 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 74   by two more dat
13f48 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13f49 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f 72  s,.** but cursor
13f4a 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 72  s cannot be shar
13f4b 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f 72  ed.  Each cursor
13f4c 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77   is associated w
13f4d 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63 75  ith a.** particu
13f4e 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  lar database con
13f4f 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 69  nection identifi
13f50 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74 72  ed BtCursor.pBtr
13f51 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69 65  ee.db..**.** Fie
13f52 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75  lds in this stru
13f53 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73  cture are access
13f54 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 53  ed under the BtS
13f55 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 66  hared.mutex.** f
13f56 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 42  ound at self->pB
13f57 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 74  t->mutex. .*/.st
13f58 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b 0a  ruct BtCursor {.
13f59 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b    Btree *pBtree;
13f5a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
13f5b 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69 63  he Btree to whic
13f5c 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 65  h this cursor be
13f5d 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 61  longs */.  BtSha
13f5e 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20  red *pBt;       
13f5f 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53 68       /* The BtSh
13f60 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f 72  ared this cursor
13f61 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 20   points to */.  
13f62 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 2c  BtCursor *pNext,
13f63 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f 72   *pPrev;  /* For
13f64 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  ms a linked list
13f65 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20   of all cursors 
13f66 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49  */.  struct KeyI
13f67 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 2f  nfo *pKeyInfo; /
13f68 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73 65  * Argument passe
13f69 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20  d to comparison 
13f6a 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 67  function */.  Pg
13f6b 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 20  no pgnoRoot;    
13f6c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
13f6d 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 73  oot page of this
13f6e 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 74   tree */.  sqlit
13f6f 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 52  e3_int64 cachedR
13f70 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 6f  owid; /* Next ro
13f71 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d 65  wid cache.  0 me
13f72 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a 2f  ans not valid */
13f73 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f  .  CellInfo info
13f74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
13f75 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 63  A parse of the c
13f76 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e 74  ell we are point
13f77 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20 77  ing at */.  u8 w
13f78 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 20  rFlag;          
13f79 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
13f7a 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20 75   writable */.  u
13f7b 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20 20  8 atLast;       
13f7c 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
13f7d 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  or pointing to t
13f7e 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a 2f  he last entry */
13f7f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79 3b  .  u8 validNKey;
13f80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13f81 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b 65  True if info.nKe
13f82 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20  y is valid */.  
13f83 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 20  u8 eState;      
13f84 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
13f85 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f 58   of the CURSOR_X
13f86 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73 65  XX constants (se
13f87 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 6f  e below) */.  vo
13f88 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 2f  id *pKey;      /
13f89 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 61 74  * Saved key that
13f8a 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c 61   was cursor's la
13f8b 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 6f  st known positio
13f8c 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b  n */.  i64 nKey;
13f8d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
13f8e 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73 74  of pKey, or last
13f8f 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f 0a   integer key */.
13f90 20 20 69 6e 74 20 73 6b 69 70 3b 20 20 20 20 20    int skip;     
13f91 20 20 20 2f 2a 20 28 73 6b 69 70 3c 30 29 20 2d     /* (skip<0) -
13f92 3e 20 50 72 65 76 28 29 20 69 73 20 61 20 6e 6f  > Prev() is a no
13f93 2d 6f 70 2e 20 28 73 6b 69 70 3e 30 29 20 2d 3e  -op. (skip>0) ->
13f94 20 4e 65 78 74 28 29 20 69 73 20 2a 2f 0a 23 69   Next() is */.#i
13f95 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
13f96 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 75 38 20  T_INCRBLOB.  u8 
13f97 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65  isIncrblobHandle
13f98 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ;      /* True i
13f99 66 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73  f this cursor is
13f9a 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 68 61 6e   an incr. io han
13f9b 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a 61  dle */.  Pgno *a
13f9c 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 20  Overflow;       
13f9d 20 20 20 2f 2a 20 43 61 63 68 65 20 6f 66 20 6f     /* Cache of o
13f9e 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 6f 63  verflow page loc
13f9f 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 66  ations */.#endif
13fa0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
13fa1 20 20 75 38 20 70 61 67 65 73 53 68 75 66 66 6c    u8 pagesShuffl
13fa2 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ed;         /* T
13fa3 72 75 65 20 69 66 20 42 74 72 65 65 20 70 61 67  rue if Btree pag
13fa4 65 73 20 61 72 65 20 72 65 61 72 72 61 6e 67 65  es are rearrange
13fa5 64 20 62 79 20 62 61 6c 61 6e 63 65 28 29 2a 2f  d by balance()*/
13fa6 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 20 69 50  .#endif.  i16 iP
13fa7 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
13fa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fa9 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72  /* Index of curr
13faa 65 6e 74 20 70 61 67 65 20 69 6e 20 61 70 50 61  ent page in apPa
13fab 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ge */.  MemPage 
13fac 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53 4f 52  *apPage[BTCURSOR
13fad 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a  _MAX_DEPTH];  /*
13fae 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74   Pages from root
13faf 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61 67 65   to current page
13fb0 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 64 78 5b   */.  u16 aiIdx[
13fb1 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50  BTCURSOR_MAX_DEP
13fb2 54 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  TH];        /* C
13fb3 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69 6e 20  urrent index in 
13fb4 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a  apPage[i] */.};.
13fb5 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c  ./*.** Potential
13fb6 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 43 75   values for BtCu
13fb7 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a  rsor.eState..**.
13fb8 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a  ** CURSOR_VALID:
13fb9 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 70 6f 69  .**   Cursor poi
13fba 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 64 20 65  nts to a valid e
13fbb 6e 74 72 79 2e 20 67 65 74 50 61 79 6c 6f 61 64  ntry. getPayload
13fbc 28 29 20 65 74 63 2e 20 6d 61 79 20 62 65 20 63  () etc. may be c
13fbd 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52  alled..**.** CUR
13fbe 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20  SOR_INVALID:.** 
13fbf 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f    Cursor does no
13fc0 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c  t point to a val
13fc1 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63  id entry. This c
13fc2 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65  an happen (for e
13fc3 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65  xample) .**   be
13fc4 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20  cause the table 
13fc5 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65 63 61  is empty or beca
13fc6 75 73 65 20 42 74 72 65 65 43 75 72 73 6f 72 46  use BtreeCursorF
13fc7 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74 20 62  irst() has not b
13fc8 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e  een.**   called.
13fc9 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45  .**.** CURSOR_RE
13fca 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20  QUIRESEEK:.**   
13fcb 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 74  The table that t
13fcc 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f  his cursor was o
13fcd 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65  pened on still e
13fce 78 69 73 74 73 2c 20 62 75 74 20 68 61 73 20 62  xists, but has b
13fcf 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69  een .**   modifi
13fd0 65 64 20 73 69 6e 63 65 20 74 68 65 20 63 75 72  ed since the cur
13fd1 73 6f 72 20 77 61 73 20 6c 61 73 74 20 75 73 65  sor was last use
13fd2 64 2e 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f  d. The cursor po
13fd3 73 69 74 69 6f 6e 20 69 73 20 73 61 76 65 64 0a  sition is saved.
13fd4 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 62 6c 65  **   in variable
13fd5 73 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 20  s BtCursor.pKey 
13fd6 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65  and BtCursor.nKe
13fd7 79 2e 20 57 68 65 6e 20 61 20 63 75 72 73 6f 72  y. When a cursor
13fd8 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69   is in .**   thi
13fd9 73 20 73 74 61 74 65 2c 20 72 65 73 74 6f 72 65  s state, restore
13fda 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
13fdb 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 74   can be called t
13fdc 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20  o attempt to.** 
13fdd 20 20 73 65 65 6b 20 74 68 65 20 63 75 72 73 6f    seek the curso
13fde 72 20 74 6f 20 74 68 65 20 73 61 76 65 64 20 70  r to the saved p
13fdf 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43  osition..**.** C
13fe0 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20  URSOR_FAULT:.** 
13fe1 20 20 41 20 75 6e 72 65 63 6f 76 65 72 61 62 6c    A unrecoverabl
13fe2 65 20 65 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20  e error (an I/O 
13fe3 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f  error or a mallo
13fe4 63 20 66 61 69 6c 75 72 65 29 20 68 61 73 20 6f  c failure) has o
13fe5 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20  ccurred.**   on 
13fe6 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e  a different conn
13fe7 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72  ection that shar
13fe8 65 73 20 74 68 65 20 42 74 53 68 61 72 65 64 20  es the BtShared 
13fe9 63 61 63 68 65 20 77 69 74 68 20 74 68 69 73 0a  cache with this.
13fea 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 20 54 68  **   cursor.  Th
13feb 65 20 65 72 72 6f 72 20 68 61 73 20 6c 65 66 74  e error has left
13fec 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 61 6e   the cache in an
13fed 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74   inconsistent st
13fee 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74  ate..**   Do not
13fef 68 69 6e 67 20 65 6c 73 65 20 77 69 74 68 20 74  hing else with t
13ff0 68 69 73 20 63 75 72 73 6f 72 2e 20 20 41 6e 79  his cursor.  Any
13ff1 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
13ff2 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20  the cursor.**   
13ff3 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68  should return th
13ff4 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f  e error code sto
13ff5 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e  red in BtCursor.
13ff6 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  skip.*/.#define 
13ff7 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 20  CURSOR_INVALID  
13ff8 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69           0.#defi
13ff9 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  ne CURSOR_VALID 
13ffa 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64              1.#d
13ffb 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 52 45 51  efine CURSOR_REQ
13ffc 55 49 52 45 53 45 45 4b 20 20 20 20 20 20 20 32  UIRESEEK       2
13ffd 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f  .#define CURSOR_
13ffe 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 20 20  FAULT           
13fff 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20    3../* .** The 
14000 64 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68  database page th
14001 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6f  e PENDING_BYTE o
14002 63 63 75 70 69 65 73 2e 20 54 68 69 73 20 70 61  ccupies. This pa
14003 67 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64  ge is never used
14004 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 45  ..*/.# define PE
14005 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
14006 70 42 74 29 20 50 41 47 45 52 5f 4d 4a 5f 50 47  pBt) PAGER_MJ_PG
14007 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 41  NO(pBt)../*.** A
14008 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
14009 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
1400a 72 75 63 74 75 72 65 73 20 69 73 20 73 74 6f 72  ructures is stor
1400b 65 64 20 61 74 20 42 74 53 68 61 72 65 64 2e 70  ed at BtShared.p
1400c 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61  Lock..** Locks a
1400d 72 65 20 61 64 64 65 64 20 28 6f 72 20 75 70 67  re added (or upg
1400e 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41 44 5f  raded from READ_
1400f 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f  LOCK to WRITE_LO
14010 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72 73 6f  CK) when a curso
14011 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20  r .** is opened 
14012 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  on the table wit
14013 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74 53 68  h root page BtSh
14014 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63  ared.iTable. Loc
14015 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a  ks are removed.*
14016 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 73 74  * from this list
14017 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
14018 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
14019 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c   or rolled back,
1401a 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74   or when.** a bt
1401b 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c  ree handle is cl
1401c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  osed..*/.struct 
1401d 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65  BtLock {.  Btree
1401e 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20   *pBtree;       
1401f 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64 6c 65   /* Btree handle
14020 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f   holding this lo
14021 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61  ck */.  Pgno iTa
14022 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ble;          /*
14023 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61   Root page of ta
14024 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63  ble */.  u8 eLoc
14025 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
14026 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57  * READ_LOCK or W
14027 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42  RITE_LOCK */.  B
14028 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20  tLock *pNext;   
14029 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20       /* Next in 
1402a 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c  BtShared.pLock l
1402b 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61  ist */.};../* Ca
1402c 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20 66  ndidate values f
1402d 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20  or BtLock.eLock 
1402e 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f  */.#define READ_
1402f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69  LOCK     1.#defi
14030 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20  ne WRITE_LOCK   
14031 20 32 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20   2../*.** These 
14032 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 68  macros define th
14033 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68  e location of th
14034 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  e pointer-map en
14035 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64 61  try for a .** da
14036 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 65  tabase page. The
14037 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
14038 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 20 6e  to each is the n
14039 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 0a  umber of usable.
1403a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68  ** bytes on each
1403b 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74   page of the dat
1403c 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30 32  abase (often 102
1403d 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69  4). The second i
1403e 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e 75  s the.** page nu
1403f 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 20  mber to look up 
14040 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  in the pointer m
14041 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  ap..**.** PTRMAP
14042 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73 20  _PAGENO returns 
14043 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  the database pag
14044 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
14045 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 70  pointer-map.** p
14046 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 73 20  age that stores 
14047 74 68 65 20 72 65 71 75 69 72 65 64 20 70 6f 69  the required poi
14048 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54 52  nter. PTRMAP_PTR
14049 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a 2a  OFFSET returns.*
1404a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20  * the offset of 
1404b 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6d 61  the requested ma
1404c 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 49  p entry..**.** I
1404d 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75 6d  f the pgno argum
1404e 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50 54  ent passed to PT
1404f 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20 61  RMAP_PAGENO is a
14050 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
14051 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f 20  e,.** then pgno 
14052 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f 20  is returned. So 
14053 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41  (pgno==PTRMAP_PA
14054 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f 29  GENO(pgsz, pgno)
14055 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 64  ) can be.** used
14056 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e 6f   to test if pgno
14057 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61   is a pointer-ma
14058 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f 49  p page. PTRMAP_I
14059 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74 73  SPAGE implements
1405a 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a 2a  .** this test..*
1405b 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50  /.#define PTRMAP
1405c 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67 6e  _PAGENO(pBt, pgn
1405d 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28  o) ptrmapPageno(
1405e 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66 69  pBt, pgno).#defi
1405f 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46  ne PTRMAP_PTROFF
14060 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70 67  SET(pgptrmap, pg
14061 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67 70  no) (5*(pgno-pgp
14062 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69 6e  trmap-1)).#defin
14063 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28  e PTRMAP_ISPAGE(
14064 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 4d  pBt, pgno) (PTRM
14065 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 2c  AP_PAGENO((pBt),
14066 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 29  (pgno))==(pgno))
14067 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ../*.** The poin
14068 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f 6f  ter map is a loo
14069 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 69  kup table that i
1406a 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61  dentifies the pa
1406b 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a 2a  rent page for.**
1406c 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 65   each child page
1406d 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1406e 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 65   file.  The pare
1406f 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20 70  nt page is the p
14070 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e 74  age that.** cont
14071 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
14072 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 76  o the child.  Ev
14073 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
14074 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
14075 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 65  s.** 0 or 1 pare
14076 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 74  nt pages.  (In t
14077 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 74  his context 'dat
14078 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66 65  abase page' refe
14079 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 67  rs.** to any pag
1407a 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 61  e that is not pa
1407b 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65  rt of the pointe
1407c 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 20  r map itself.)  
1407d 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 70  Each pointer map
1407e 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 73  .** entry consis
1407f 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 62  ts of a single b
14080 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 61  yte 'type' and a
14081 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 70   4 byte parent p
14082 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 54  age number..** T
14083 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 64  he PTRMAP_XXX id
14084 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 20  entifiers below 
14085 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 79  are the valid ty
14086 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  pes..**.** The p
14087 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 6f  urpose of the po
14088 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f 20  inter map is to 
14089 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 20  facility moving 
1408a 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a 2a  pages from one.*
1408b 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68  * position in th
1408c 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 65  e file to anothe
1408d 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 74  r as part of aut
1408e 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 61  ovacuum.  When a
1408f 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 65   page.** is move
14090 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69  d, the pointer i
14091 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 73  n its parent mus
14092 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f 20  t be updated to 
14093 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20  point to the.** 
14094 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54  new location.  T
14095 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
14096 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  s used to locate
14097 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
14098 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20   quickly..**.** 
14099 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 3a  PTRMAP_ROOTPAGE:
1409a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
1409b 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67  ge is a root-pag
1409c 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62  e. The page-numb
1409d 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20  er is not.**    
1409e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73                us
1409f 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ed in this case.
140a0 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 52  .**.** PTRMAP_FR
140a1 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 61  EEPAGE: The data
140a2 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e 20  base page is an 
140a3 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 61  unused (free) pa
140a4 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d  ge. The page-num
140a5 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ber .**         
140a6 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 20           is not 
140a7 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73  used in this cas
140a8 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  e..**.** PTRMAP_
140a9 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 64  OVERFLOW1: The d
140aa 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20  atabase page is 
140ab 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  the first page i
140ac 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a 20  n a list of .** 
140ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140ae 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73    overflow pages
140af 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  . The page numbe
140b0 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  r identifies the
140b1 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 20   page that.**   
140b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140b3 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c  contains the cel
140b4 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72  l with a pointer
140b5 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   to this overflo
140b6 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54  w page..**.** PT
140b7 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 20  RMAP_OVERFLOW2: 
140b8 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  The database pag
140b9 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  e is the second 
140ba 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 6e  or later page in
140bb 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20   a list of.**   
140bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140bd 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
140be 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  The page-number 
140bf 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70  identifies the p
140c0 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 20  revious.**      
140c1 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
140c2 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f  e in the overflo
140c3 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 0a  w page list..**.
140c4 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a  ** PTRMAP_BTREE:
140c5 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
140c6 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f 74  ge is a non-root
140c7 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 65   btree page. The
140c8 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20   page number.** 
140c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 64                id
140ca 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72  entifies the par
140cb 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ent page in the 
140cc 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  btree..*/.#defin
140cd 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  e PTRMAP_ROOTPAG
140ce 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  E 1.#define PTRM
140cf 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 64  AP_FREEPAGE 2.#d
140d0 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45  efine PTRMAP_OVE
140d1 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e 65  RFLOW1 3.#define
140d2 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
140d3 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  2 4.#define PTRM
140d4 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 41  AP_BTREE 5../* A
140d5 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 74   bunch of assert
140d6 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  () statements to
140d7 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e 73   check the trans
140d8 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 72  action state var
140d9 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 6e  iables.** of han
140da 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 65  dle p (type Btre
140db 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 6c  e*) are internal
140dc 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a 2a  ly consistent..*
140dd 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 49  /.#define btreeI
140de 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 20  ntegrity(p) \.  
140df 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e  assert( p->pBt->
140e0 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d 54  inTransaction!=T
140e1 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e  RANS_NONE || p->
140e2 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f  pBt->nTransactio
140e3 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 65  n==0 ); \.  asse
140e4 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72  rt( p->pBt->inTr
140e5 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 6e  ansaction>=p->in
140e6 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a  Trans ); .../*.*
140e7 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 55  * The ISAUTOVACU
140e8 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  UM macro is used
140e9 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 5f   within balance_
140ea 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 74  nonroot() to det
140eb 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 65  ermine.** if the
140ec 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
140ed 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6f  ts auto-vacuum o
140ee 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 69  r not. Because i
140ef 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 74  t is used.** wit
140f0 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f  hin an expressio
140f1 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 67  n that is an arg
140f2 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 72  ument to another
140f3 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c 69   macro .** (sqli
140f4 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 74  teMallocRaw), it
140f5 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
140f6 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 6f   to use conditio
140f7 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e  nal compilation.
140f8 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 63  .** So, this mac
140f9 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e  ro is defined in
140fa 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  stead..*/.#ifnde
140fb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
140fc 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e 65  TOVACUUM.#define
140fd 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 70   ISAUTOVACUUM (p
140fe 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 0a  Bt->autoVacuum).
140ff 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 53  #else.#define IS
14100 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e  AUTOVACUUM 0.#en
14101 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  dif.../*.** This
14102 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61   structure is pa
14103 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 6f  ssed around thro
14104 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e 69  ugh all the sani
14105 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 74  ty checking rout
14106 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  ines.** in order
14107 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f   to keep track o
14108 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 74  f some global st
14109 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  ate information.
1410a 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
1410b 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 49  ct IntegrityCk I
1410c 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 75  ntegrityCk;.stru
1410d 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 7b  ct IntegrityCk {
1410e 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1410f 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 65  ;    /* The tree
14110 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 6f   being checked o
14111 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70  ut */.  Pager *p
14112 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 65  Pager;    /* The
14113 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 65   associated page
14114 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 69  r.  Also accessi
14115 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 67  ble by pBt->pPag
14116 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61  er */.  Pgno nPa
14117 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ge;       /* Num
14118 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
14119 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
1411a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 20    int *anRef;   
1411b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1411c 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 65   times each page
1411d 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a   is referenced *
1411e 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 20  /.  int mxErr;  
1411f 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 63        /* Stop ac
14120 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f 72  cumulating error
14121 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 63  s when this reac
14122 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e  hes zero */.  in
14123 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20  t nErr;         
14124 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 73  /* Number of mes
14125 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f  sages written to
14126 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72 20   zErrMsg so far 
14127 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63 46  */.  int mallocF
14128 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d 6f  ailed; /* A memo
14129 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
1412a 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
1412b 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20 65   */.  StrAccum e
1412c 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75 6d  rrMsg;  /* Accum
1412d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72 20  ulate the error 
1412e 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 65 72  message text her
1412f 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52  e */.};../*.** R
14130 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 74  ead or write a t
14131 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79 74  wo- and four-byt
14132 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74  e big-endian int
14133 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a  eger values..*/.
14134 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 65  #define get2byte
14135 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c 38  (x)   ((x)[0]<<8
14136 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66 69   | (x)[1]).#defi
14137 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c 76 29  ne put2byte(p,v)
14138 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29 28   ((p)[0] = (u8)(
14139 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d 20  (v)>>8), (p)[1] 
1413a 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 66 69  = (u8)(v)).#defi
1413b 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c 69  ne get4byte sqli
1413c 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65 66  te3Get4byte.#def
1413d 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71 6c  ine put4byte sql
1413e 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f 2a  ite3Put4byte../*
1413f 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 72 6f 75  .** Internal rou
14140 74 69 6e 65 73 20 74 68 61 74 20 73 68 6f 75 6c  tines that shoul
14141 64 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79  d be accessed by
14142 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   the btree layer
14143 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45   only..*/.SQLITE
14144 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
14145 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
14146 28 42 74 53 68 61 72 65 64 2a 2c 20 50 67 6e 6f  (BtShared*, Pgno
14147 2c 20 4d 65 6d 50 61 67 65 2a 2a 2c 20 69 6e 74  , MemPage**, int
14148 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
14149 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1414a 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d 50 61  eeInitPage(MemPa
1414b 67 65 20 2a 70 50 61 67 65 29 3b 0a 53 51 4c 49  ge *pPage);.SQLI
1414c 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1414d 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
1414e 65 43 65 6c 6c 50 74 72 28 4d 65 6d 50 61 67 65  eCellPtr(MemPage
1414f 2a 2c 20 75 38 2a 2c 20 43 65 6c 6c 49 6e 66 6f  *, u8*, CellInfo
14150 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
14151 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
14152 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 4d 65  treeParseCell(Me
14153 6d 50 61 67 65 2a 2c 20 69 6e 74 2c 20 43 65 6c  mPage*, int, Cel
14154 6c 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f  lInfo*);.SQLITE_
14155 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14156 74 65 33 42 74 72 65 65 52 65 73 74 6f 72 65 43  te3BtreeRestoreC
14157 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74  ursorPosition(Bt
14158 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a 53  Cursor *pCur);.S
14159 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1415a 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  id sqlite3BtreeG
1415b 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43  etTempCursor(BtC
1415c 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74 43  ursor *pCur, BtC
1415d 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72 29  ursor *pTempCur)
1415e 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
1415f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
14160 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72  eeReleaseTempCur
14161 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  sor(BtCursor *pC
14162 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ur);.SQLITE_PRIV
14163 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14164 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e  BtreeMoveToParen
14165 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t(BtCursor *pCur
14166 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  );../***********
14167 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65  *** End of btree
14168 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
14169 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1416a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1416b 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1416c 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
1416d 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
1416e 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a   in btmutex.c **
1416f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14170 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  **/.#ifndef SQLI
14171 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
14172 41 43 48 45 0a 23 69 66 20 53 51 4c 49 54 45 5f  ACHE.#if SQLITE_
14173 54 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a 0a 2a  THREADSAFE../*.*
14174 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 42 74 53  * Obtain the BtS
14175 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f  hared mutex asso
14176 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72  ciated with B-Tr
14177 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 41 6c 73  ee handle p. Als
14178 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68 61 72  o,.** set BtShar
14179 65 64 2e 64 62 20 74 6f 20 74 68 65 20 64 61 74  ed.db to the dat
1417a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 61 73 73  abase handle ass
1417b 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 20 61  ociated with p a
1417c 6e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c 6f 63  nd the.** p->loc
1417d 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f 20 74  ked boolean to t
1417e 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  rue..*/.static v
1417f 6f 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74  oid lockBtreeMut
14180 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  ex(Btree *p){.  
14181 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65  assert( p->locke
14182 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  d==0 );.  assert
14183 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
14184 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e  notheld(p->pBt->
14185 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
14186 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14187 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
14188 74 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74  tex) );..  sqlit
14189 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
1418a 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20  ->pBt->mutex);. 
1418b 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d   p->pBt->db = p-
1418c 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64  >db;.  p->locked
1418d 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   = 1;.}../*.** R
1418e 65 6c 65 61 73 65 20 74 68 65 20 42 74 53 68 61  elease the BtSha
1418f 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63 69  red mutex associ
14190 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 65 65  ated with B-Tree
14191 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a 2a 2a   handle p and.**
14192 20 63 6c 65 61 72 20 74 68 65 20 70 2d 3e 6c 6f   clear the p->lo
14193 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f  cked boolean..*/
14194 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c  .static void unl
14195 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74  ockBtreeMutex(Bt
14196 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
14197 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 20  t( p->locked==1 
14198 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
14199 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1419a 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  p->pBt->mutex) )
1419b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1419c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1419d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
1419e 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 3d    assert( p->db=
1419f 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 0a  =p->pBt->db );..
141a0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
141a1 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75  leave(p->pBt->mu
141a2 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65  tex);.  p->locke
141a3 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d = 0;.}../*.** 
141a4 45 6e 74 65 72 20 61 20 6d 75 74 65 78 20 6f 6e  Enter a mutex on
141a5 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 65 65   the given BTree
141a6 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49   object..**.** I
141a7 66 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20  f the object is 
141a8 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c 20 74 68  not sharable, th
141a9 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 65  en no mutex is e
141aa 76 65 72 20 72 65 71 75 69 72 65 64 0a 2a 2a 20  ver required.** 
141ab 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65  and this routine
141ac 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68   is a no-op.  Th
141ad 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 75 74  e underlying mut
141ae 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63 75 72 73  ex is non-recurs
141af 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77 65 20 6b  ive..** But we k
141b0 65 65 70 20 61 20 72 65 66 65 72 65 6e 63 65 20  eep a reference 
141b1 63 6f 75 6e 74 20 69 6e 20 42 74 72 65 65 2e 77  count in Btree.w
141b2 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 65  antToLock so the
141b3 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20   behavior.** of 
141b4 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69  this interface i
141b5 73 20 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a 0a  s recursive..**.
141b6 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64  ** To avoid dead
141b7 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 20  locks, multiple 
141b8 42 74 72 65 65 73 20 61 72 65 20 6c 6f 63 6b 65  Btrees are locke
141b9 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72  d in the same or
141ba 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 61  der.** by all da
141bb 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
141bc 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70 4e 65 78  ns.  The p->pNex
141bd 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6f  t is a list of o
141be 74 68 65 72 0a 2a 2a 20 42 74 72 65 65 73 20 62  ther.** Btrees b
141bf 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 20  elonging to the 
141c0 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
141c1 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 68 65 20  nnection as the 
141c2 70 20 42 74 72 65 65 0a 2a 2a 20 77 68 69 63 68  p Btree.** which
141c3 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b   need to be lock
141c4 65 64 20 61 66 74 65 72 20 70 2e 20 20 49 66 20  ed after p.  If 
141c5 77 65 20 63 61 6e 6e 6f 74 20 67 65 74 20 61 20  we cannot get a 
141c6 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 68  lock on.** p, th
141c7 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20  en first unlock 
141c8 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 72  all of the other
141c9 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74  s on p->pNext, t
141ca 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66 6f 72 20  hen wait.** for 
141cb 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f  the lock to beco
141cc 6d 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  me available on 
141cd 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61  p, then relock a
141ce 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62  ll of the.** sub
141cf 73 65 71 75 65 6e 74 20 42 74 72 65 65 73 20 74  sequent Btrees t
141d0 68 61 74 20 64 65 73 69 72 65 20 61 20 6c 6f 63  hat desire a loc
141d1 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  k..*/.SQLITE_PRI
141d2 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
141d3 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 65  3BtreeEnter(Btre
141d4 65 20 2a 70 29 7b 0a 20 20 42 74 72 65 65 20 2a  e *p){.  Btree *
141d5 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 6f  pLater;..  /* So
141d6 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20  me basic sanity 
141d7 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20  checking on the 
141d8 42 74 72 65 65 2e 20 20 54 68 65 20 6c 69 73 74  Btree.  The list
141d9 20 6f 66 20 42 74 72 65 65 73 0a 20 20 2a 2a 20   of Btrees.  ** 
141da 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 4e 65  connected by pNe
141db 78 74 20 61 6e 64 20 70 50 72 65 76 20 73 68 6f  xt and pPrev sho
141dc 75 6c 64 20 62 65 20 69 6e 20 73 6f 72 74 65 64  uld be in sorted
141dd 20 6f 72 64 65 72 20 62 79 0a 20 20 2a 2a 20 42   order by.  ** B
141de 74 72 65 65 2e 70 42 74 20 76 61 6c 75 65 2e 20  tree.pBt value. 
141df 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  All elements of 
141e0 74 68 65 20 6c 69 73 74 20 73 68 6f 75 6c 64 20  the list should 
141e1 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 74  belong to.  ** t
141e2 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69  he same connecti
141e3 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 65 64 20  on. Only shared 
141e4 42 74 72 65 65 73 20 61 72 65 20 6f 6e 20 74 68  Btrees are on th
141e5 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 61 73 73  e list. */.  ass
141e6 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30  ert( p->pNext==0
141e7 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 42   || p->pNext->pB
141e8 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73  t>p->pBt );.  as
141e9 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d  sert( p->pPrev==
141ea 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70  0 || p->pPrev->p
141eb 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61  Bt<p->pBt );.  a
141ec 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d  ssert( p->pNext=
141ed 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e  =0 || p->pNext->
141ee 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61  db==p->db );.  a
141ef 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d  ssert( p->pPrev=
141f0 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e  =0 || p->pPrev->
141f1 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61  db==p->db );.  a
141f2 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
141f3 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 3d  le || (p->pNext=
141f4 3d 30 20 26 26 20 70 2d 3e 70 50 72 65 76 3d 3d  =0 && p->pPrev==
141f5 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  0) );..  /* Chec
141f6 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f  k for locking co
141f7 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20 20 61  nsistency */.  a
141f8 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65  ssert( !p->locke
141f9 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  d || p->wantToLo
141fa 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ck>0 );.  assert
141fb 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c  ( p->sharable ||
141fc 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d   p->wantToLock==
141fd 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 68  0 );..  /* We sh
141fe 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c  ould already hol
141ff 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  d a lock on the 
14200 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14201 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ion */.  assert(
14202 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14203 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
14204 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73  ) );..  /* Unles
14205 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  s the database i
14206 73 20 73 68 61 72 61 62 6c 65 20 61 6e 64 20 75  s sharable and u
14207 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 42 74  nlocked, then Bt
14208 53 68 61 72 65 64 2e 64 62 0a 20 20 2a 2a 20 73  Shared.db.  ** s
14209 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65  hould already be
1420a 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20   set correctly. 
1420b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  */.  assert( (p-
1420c 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d  >locked==0 && p-
1420d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d  >sharable) || p-
1420e 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20  >pBt->db==p->db 
1420f 29 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73 68  );..  if( !p->sh
14210 61 72 61 62 6c 65 20 29 20 72 65 74 75 72 6e 3b  arable ) return;
14211 0a 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  .  p->wantToLock
14212 2b 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63  ++;.  if( p->loc
14213 6b 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  ked ) return;.. 
14214 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 65   /* In most case
14215 73 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65 20  s, we should be 
14216 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20  able to acquire 
14217 74 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a  the lock we.  **
14218 20 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61   want without ha
14219 76 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75  ving to go throu
1421a 67 68 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e  ght the ascendin
1421b 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63  g lock.  ** proc
1421c 65 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f  edure that follo
1421d 77 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72  ws.  Just be sur
1421e 65 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a  e not to block..
1421f 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
14220 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e  e3_mutex_try(p->
14221 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c  pBt->mutex)==SQL
14222 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d  ITE_OK ){.    p-
14223 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62  >pBt->db = p->db
14224 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20  ;.    p->locked 
14225 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  = 1;.    return;
14226 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76  .  }..  /* To av
14227 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69  oid deadlock, fi
14228 72 73 74 20 72 65 6c 65 61 73 65 20 61 6c 6c 20  rst release all 
14229 6c 6f 63 6b 73 20 77 69 74 68 20 61 20 6c 61 72  locks with a lar
1422a 67 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 72 65  ger.  ** BtShare
1422b 64 20 61 64 64 72 65 73 73 2e 20 20 54 68 65 6e  d address.  Then
1422c 20 61 63 71 75 69 72 65 20 6f 75 72 20 6c 6f 63   acquire our loc
1422d 6b 2e 20 20 54 68 65 6e 20 72 65 61 63 71 75 69  k.  Then reacqui
1422e 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65  re.  ** the othe
1422f 72 20 42 74 53 68 61 72 65 64 20 6c 6f 63 6b 73  r BtShared locks
14230 20 74 68 61 74 20 77 65 20 75 73 65 64 20 74 6f   that we used to
14231 20 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e 64 69   hold in ascendi
14232 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20  ng.  ** order.. 
14233 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 65 72   */.  for(pLater
14234 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65  =p->pNext; pLate
14235 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72  r; pLater=pLater
14236 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73  ->pNext){.    as
14237 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 73 68  sert( pLater->sh
14238 61 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73  arable );.    as
14239 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e  sert( pLater->pN
1423a 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72  ext==0 || pLater
1423b 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61  ->pNext->pBt>pLa
1423c 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20  ter->pBt );.    
1423d 61 73 73 65 72 74 28 20 21 70 4c 61 74 65 72 2d  assert( !pLater-
1423e 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65  >locked || pLate
1423f 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  r->wantToLock>0 
14240 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65  );.    if( pLate
14241 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20  r->locked ){.   
14242 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75     unlockBtreeMu
14243 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20  tex(pLater);.   
14244 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b 42 74 72   }.  }.  lockBtr
14245 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 66 6f  eeMutex(p);.  fo
14246 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78  r(pLater=p->pNex
14247 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65  t; pLater; pLate
14248 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29  r=pLater->pNext)
14249 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72  {.    if( pLater
1424a 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b 0a  ->wantToLock ){.
1424b 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65 4d        lockBtreeM
1424c 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20  utex(pLater);.  
1424d 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1424e 20 45 78 69 74 20 74 68 65 20 72 65 63 75 72 73   Exit the recurs
1424f 69 76 65 20 6d 75 74 65 78 20 6f 6e 20 61 20 42  ive mutex on a B
14250 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tree..*/.SQLITE_
14251 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
14252 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42  ite3BtreeLeave(B
14253 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  tree *p){.  if( 
14254 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20  p->sharable ){. 
14255 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61     assert( p->wa
14256 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20  ntToLock>0 );.  
14257 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d    p->wantToLock-
14258 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 61  -;.    if( p->wa
14259 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  ntToLock==0 ){. 
1425a 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65       unlockBtree
1425b 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a  Mutex(p);.    }.
1425c 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e    }.}..#ifndef N
1425d 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75  DEBUG./*.** Retu
1425e 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 42  rn true if the B
1425f 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 69 73  tShared mutex is
14260 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 62 74 72   held on the btr
14261 65 65 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a  ee, or if the.**
14262 20 42 2d 54 72 65 65 20 69 73 20 6e 6f 74 20 6d   B-Tree is not m
14263 61 72 6b 65 64 20 61 73 20 73 68 61 72 61 62 6c  arked as sharabl
14264 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
14265 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e  utine is used on
14266 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61  ly from within a
14267 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
14268 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ts..*/.SQLITE_PR
14269 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1426a 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
1426b 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73  (Btree *p){.  as
1426c 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
1426d 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65  e==0 || p->locke
1426e 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e 74 54  d==0 || p->wantT
1426f 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73  oLock>0 );.  ass
14270 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65  ert( p->sharable
14271 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64  ==0 || p->locked
14272 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d 70 2d  ==0 || p->db==p-
14273 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73  >pBt->db );.  as
14274 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
14275 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65  e==0 || p->locke
14276 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  d==0 || sqlite3_
14277 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42  mutex_held(p->pB
14278 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
14279 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
1427a 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b  le==0 || p->lock
1427b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  ed==0 || sqlite3
1427c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
1427d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
1427e 72 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 72 61  return (p->shara
1427f 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63  ble==0 || p->loc
14280 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ked);.}.#endif..
14281 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14282 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a  OMIT_INCRBLOB./*
14283 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 6c 65  .** Enter and le
14284 61 76 65 20 61 20 6d 75 74 65 78 20 6f 6e 20 61  ave a mutex on a
14285 20 42 74 72 65 65 20 67 69 76 65 6e 20 61 20 63   Btree given a c
14286 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 79 20 74  ursor owned by t
14287 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20 20 54  hat.** Btree.  T
14288 68 65 73 65 20 65 6e 74 72 79 20 70 6f 69 6e 74  hese entry point
14289 73 20 61 72 65 20 75 73 65 64 20 62 79 20 69 6e  s are used by in
1428a 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 61 6e  cremental I/O an
1428b 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d 69 74  d can be.** omit
1428c 74 65 64 20 69 66 20 74 68 61 74 20 6d 6f 64 75  ted if that modu
1428d 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a  le is not used..
1428e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1428f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14290 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42  reeEnterCursor(B
14291 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
14292 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
14293 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72 65 65  ter(pCur->pBtree
14294 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
14295 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14296 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72  BtreeLeaveCursor
14297 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
14298 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  {.  sqlite3Btree
14299 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 42 74 72  Leave(pCur->pBtr
1429a 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  ee);.}.#endif /*
1429b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
1429c 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  RBLOB */.../*.**
1429d 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78   Enter the mutex
1429e 20 6f 6e 20 65 76 65 72 79 20 42 74 72 65 65 20   on every Btree 
1429f 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
142a0 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  a database.** co
142a1 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20  nnection.  This 
142a2 69 73 20 6e 65 65 64 65 64 20 28 66 6f 72 20 65  is needed (for e
142a3 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 20 74 6f  xample) prior to
142a4 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20 73 74   parsing.** a st
142a5 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 20 77 65  atement since we
142a6 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61 72 69   will be compari
142a7 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c  ng table and col
142a8 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61 67 61  umn names.** aga
142a9 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d 61 73  inst all schemas
142aa 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77   and we do not w
142ab 61 6e 74 20 74 68 6f 73 65 20 73 63 68 65 6d 61  ant those schema
142ac 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73 65 74  s being.** reset
142ad 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20   out from under 
142ae 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  us..**.** There 
142af 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  is a correspondi
142b0 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 72 6f  ng leave-all pro
142b1 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 45  cedures..**.** E
142b2 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 73  nter the mutexes
142b3 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72   in accending or
142b4 64 65 72 20 62 79 20 42 74 53 68 61 72 65 64 20  der by BtShared 
142b5 70 6f 69 6e 74 65 72 20 61 64 64 72 65 73 73 0a  pointer address.
142b6 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20  ** to avoid the 
142b7 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 64  possibility of d
142b8 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74 77 6f  eadlock when two
142b9 20 74 68 72 65 61 64 73 20 77 69 74 68 0a 2a 2a   threads with.**
142ba 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 74 72   two or more btr
142bb 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 6f  ees in common bo
142bc 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b 20 61  th try to lock a
142bd 6c 6c 20 74 68 65 69 72 20 62 74 72 65 65 73 0a  ll their btrees.
142be 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65 20 69  ** at the same i
142bf 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  nstant..*/.SQLIT
142c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
142c1 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
142c2 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29  All(sqlite3 *db)
142c3 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 72  {.  int i;.  Btr
142c4 65 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72 3b 0a  ee *p, *pLater;.
142c5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
142c6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
142c7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
142c8 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
142c9 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64   i++){.    p = d
142ca 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
142cb 20 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c     assert( !p ||
142cc 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26   (p->locked==0 &
142cd 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c  & p->sharable) |
142ce 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d  | p->pBt->db==p-
142cf 3e 64 62 20 29 3b 0a 20 20 20 20 69 66 28 20 70  >db );.    if( p
142d0 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20   && p->sharable 
142d1 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74  ){.      p->want
142d2 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20  ToLock++;.      
142d3 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29  if( !p->locked )
142d4 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
142d5 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
142d6 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68  =1 );.        wh
142d7 69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 29 20  ile( p->pPrev ) 
142d8 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20  p = p->pPrev;.  
142d9 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e        while( p->
142da 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 70 4e 65  locked && p->pNe
142db 78 74 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78  xt ) p = p->pNex
142dc 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70  t;.        for(p
142dd 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74  Later = p->pNext
142de 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72  ; pLater; pLater
142df 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b  =pLater->pNext){
142e0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
142e1 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b  Later->locked ){
142e2 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c  .            unl
142e3 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c  ockBtreeMutex(pL
142e4 61 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  ater);.         
142e5 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
142e6 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b       while( p ){
142e7 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42  .          lockB
142e8 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
142e9 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70          p = p->p
142ea 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Next;.        }.
142eb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
142ec 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  }.}.SQLITE_PRIVA
142ed 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
142ee 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c  treeLeaveAll(sql
142ef 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
142f0 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a   i;.  Btree *p;.
142f1 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
142f2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
142f3 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
142f4 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
142f5 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64   i++){.    p = d
142f6 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
142f7 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73     if( p && p->s
142f8 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  harable ){.     
142f9 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74   assert( p->want
142fa 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20  ToLock>0 );.    
142fb 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d    p->wantToLock-
142fc 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  -;.      if( p->
142fd 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b  wantToLock==0 ){
142fe 0a 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42  .        unlockB
142ff 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
14300 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
14301 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
14302 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  G./*.** Return t
14303 72 75 65 20 69 66 20 74 68 65 20 63 75 72 72 65  rue if the curre
14304 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20  nt thread holds 
14305 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
14306 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78  nection.** mutex
14307 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65   and all require
14308 64 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  d BtShared mutex
14309 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  es..**.** This r
1430a 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69  outine is used i
1430b 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
1430c 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a  tatements only..
1430d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1430e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1430f 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
14310 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
14311 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21    int i;.  if( !
14312 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14313 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b  ld(db->mutex) ){
14314 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
14315 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
14316 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
14317 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20     Btree *p;.   
14318 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   p = db->aDb[i].
14319 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26  pBt;.    if( p &
1431a 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26  & p->sharable &&
1431b 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61  .         (p->wa
1431c 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21  ntToLock==0 || !
1431d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1431e 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ld(p->pBt->mutex
1431f 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  )) ){.      retu
14320 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 0;.    }.  }.
14321 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
14322 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
14323 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e  /../*.** Add a n
14324 65 77 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72  ew Btree pointer
14325 20 74 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78   to a BtreeMutex
14326 41 72 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68  Array. .** if th
14327 65 20 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f  e pointer can po
14328 73 73 69 62 6c 79 20 62 65 20 73 68 61 72 65 64  ssibly be shared
14329 20 77 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72   with.** another
1432a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1432b 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
1432c 70 6f 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70  pointers are kep
1432d 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  t in sorted orde
1432e 72 20 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74  r by pBtree->pBt
1432f 2e 20 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77  .  That.** way w
14330 68 65 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74  hen we go to ent
14331 65 72 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78  er all the mutex
14332 65 73 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72  es, we can enter
14333 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65   them.** in orde
14334 72 20 77 69 74 68 6f 75 74 20 65 76 65 72 79 20  r without every 
14335 68 61 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70  having to backup
14336 20 61 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77   and retry and w
14337 69 74 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69  ithout.** worryi
14338 6e 67 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63  ng about deadloc
14339 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d  k..**.** The num
1433a 62 65 72 20 6f 66 20 73 68 61 72 65 64 20 62 74  ber of shared bt
1433b 72 65 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73  rees will always
1433c 20 62 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c   be small (usual
1433d 6c 79 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f  ly 0 or 1).** so
1433e 20 61 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f   an insertion so
1433f 72 74 20 69 73 20 61 6e 20 61 64 65 71 75 61 74  rt is an adequat
14340 65 20 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65  e algorithm here
14341 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14342 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14343 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49  BtreeMutexArrayI
14344 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78  nsert(BtreeMutex
14345 41 72 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42  Array *pArray, B
14346 74 72 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20  tree *pBtree){. 
14347 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53   int i, j;.  BtS
14348 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66  hared *pBt;.  if
14349 28 20 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70  ( pBtree==0 || p
1434a 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d  Btree->sharable=
1434b 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66  =0 ) return;.#if
1434c 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a  ndef NDEBUG.  {.
1434d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1434e 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69  Array->nMutex; i
1434f 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
14350 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  t( pArray->aBtre
14351 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a  e[i]!=pBtree );.
14352 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
14353 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61  .  assert( pArra
14354 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a  y->nMutex>=0 );.
14355 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79    assert( pArray
14356 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69  ->nMutex<ArraySi
14357 7a 65 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65  ze(pArray->aBtre
14358 65 29 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20  e)-1 );.  pBt = 
14359 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66  pBtree->pBt;.  f
1435a 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79  or(i=0; i<pArray
1435b 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a  ->nMutex; i++){.
1435c 20 20 20 20 61 73 73 65 72 74 28 20 70 41 72 72      assert( pArr
1435d 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70  ay->aBtree[i]!=p
1435e 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28  Btree );.    if(
1435f 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14360 69 5d 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20  i]->pBt>pBt ){. 
14361 20 20 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61       for(j=pArra
14362 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20  y->nMutex; j>i; 
14363 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41  j--){.        pA
14364 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20  rray->aBtree[j] 
14365 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65  = pArray->aBtree
14366 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20  [j-1];.      }. 
14367 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74       pArray->aBt
14368 72 65 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b  ree[i] = pBtree;
14369 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e  .      pArray->n
1436a 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72  Mutex++;.      r
1436b 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
1436c 0a 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  .  pArray->aBtre
1436d 65 5b 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78  e[pArray->nMutex
1436e 2b 2b 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a  ++] = pBtree;.}.
1436f 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65  ./*.** Enter the
14370 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72 79 20   mutex of every 
14371 62 74 72 65 65 20 69 6e 20 74 68 65 20 61 72 72  btree in the arr
14372 61 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ay.  This routin
14373 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61  e is.** called a
14374 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
14375 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  of sqlite3VdbeEx
14376 65 63 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78  ec().  The mutex
14377 65 73 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64  es are.** exited
14378 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
14379 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  he same function
1437a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1437b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1437c 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45  BtreeMutexArrayE
1437d 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 41  nter(BtreeMutexA
1437e 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20  rray *pArray){. 
1437f 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
14380 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75  0; i<pArray->nMu
14381 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42  tex; i++){.    B
14382 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79  tree *p = pArray
14383 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20  ->aBtree[i];.   
14384 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73   /* Some basic s
14385 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
14386 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d  /.    assert( i=
14387 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42  =0 || pArray->aB
14388 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70  tree[i-1]->pBt<p
14389 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73  ->pBt );.    ass
1438a 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20  ert( !p->locked 
1438b 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  || p->wantToLock
1438c 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65  >0 );..    /* We
1438d 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20   should already 
1438e 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  hold a lock on t
1438f 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
14390 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73  ection */.    as
14391 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14392 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
14393 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 70  mutex) );..    p
14394 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a  ->wantToLock++;.
14395 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b      if( !p->lock
14396 65 64 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c  ed && p->sharabl
14397 65 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42  e ){.      lockB
14398 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
14399 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1439a 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74 65 78   Leave the mutex
1439b 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 65 20   of every btree 
1439c 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f  in the group..*/
1439d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1439e 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
1439f 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
143a0 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  (BtreeMutexArray
143a1 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74   *pArray){.  int
143a2 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
143a3 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b  <pArray->nMutex;
143a4 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65   i++){.    Btree
143a5 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42   *p = pArray->aB
143a6 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20  tree[i];.    /* 
143a7 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74  Some basic sanit
143a8 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20  y checking */.  
143a9 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c    assert( i==0 |
143aa 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65  | pArray->aBtree
143ab 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42  [i-1]->pBt<p->pB
143ac 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
143ad 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 21 70   p->locked || !p
143ae 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20  ->sharable );.  
143af 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e    assert( p->wan
143b0 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20  tToLock>0 );..  
143b1 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61    /* We should a
143b2 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f  lready hold a lo
143b3 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
143b4 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
143b5 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
143b6 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
143b7 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
143b8 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c  ..    p->wantToL
143b9 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  ock--;.    if( p
143ba 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
143bb 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a  && p->locked ){.
143bc 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65        unlockBtre
143bd 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d  eMutex(p);.    }
143be 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53 51  .  }.}..#else.SQ
143bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
143c0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
143c1 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ter(Btree *p){. 
143c2 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d   p->pBt->db = p-
143c3 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  >db;.}.SQLITE_PR
143c4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
143c5 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
143c6 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
143c7 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
143c8 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
143c9 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20  ){.    Btree *p 
143ca 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
143cb 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20  ;.    if( p ){. 
143cc 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20       p->pBt->db 
143cd 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a 20  = p->db;.    }. 
143ce 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69   }.}.#endif /* i
143cf 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
143d0 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  AFE */.#endif /*
143d1 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f   ifndef SQLITE_O
143d2 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
143d3 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
143d4 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d 75  **** End of btmu
143d5 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tex.c **********
143d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143d8 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
143d9 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
143da 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  btree.c ********
143db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143dd 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
143de 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68  April 6.**.** Th
143df 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
143e0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
143e1 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
143e2 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
143e3 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
143e4 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
143e5 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
143e6 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
143e7 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
143e8 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
143e9 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
143ea 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
143eb 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
143ec 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
143ed 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
143ee 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
143ef 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
143f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
143f4 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62  ******.** $Id: b
143f5 74 72 65 65 2e 63 2c 76 20 31 2e 36 30 38 20 32  tree.c,v 1.608 2
143f6 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 35 37 3a  009/05/06 18:57:
143f7 31 30 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  10 shane Exp $.*
143f8 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
143f9 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 65  mplements a exte
143fa 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 64  rnal (disk-based
143fb 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67  ) database using
143fc 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 20   BTrees..** See 
143fd 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65  the header comme
143fe 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 2e  nt on "btreeInt.
143ff 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  h" for additiona
14400 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
14401 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 65  * Including a de
14402 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 6c  scription of fil
14403 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e 20  e format and an 
14404 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 72  overview of oper
14405 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ation..*/../*.**
14406 20 54 68 65 20 68 65 61 64 65 72 20 73 74 72 69   The header stri
14407 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ng that appears 
14408 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
14409 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c   of every.** SQL
1440a 69 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  ite database..*/
1440b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
1440c 61 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72 5b  ar zMagicHeader[
1440d 5d 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f  ] = SQLITE_FILE_
1440e 48 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 53  HEADER;../*.** S
1440f 65 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76  et this global v
14410 61 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f 20  ariable to 1 to 
14411 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 75  enable tracing u
14412 73 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a 2a  sing the TRACE.*
14413 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20  * macro..*/.#if 
14414 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  0.int sqlite3Btr
14415 65 65 54 72 61 63 65 3d 30 3b 20 20 2f 2a 20 54  eeTrace=0;  /* T
14416 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 72  rue to enable tr
14417 61 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 69 6e  acing */.# defin
14418 65 20 54 52 41 43 45 28 58 29 20 20 69 66 28 73  e TRACE(X)  if(s
14419 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65  qlite3BtreeTrace
1441a 29 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c 75 73  ){printf X;fflus
1441b 68 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 6c 73  h(stdout);}.#els
1441c 65 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45  e.# define TRACE
1441d 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69  (X).#endif....#i
1441e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1441f 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
14420 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 42  *.** A list of B
14421 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73 20  tShared objects 
14422 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 62 6c  that are eligibl
14423 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 61 74  e for participat
14424 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65 64  ion.** in shared
14425 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 76 61   cache.  This va
14426 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65 20  riable has file 
14427 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f 72  scope during nor
14428 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20 62  mal builds,.** b
14429 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72 6e  ut the test harn
1442a 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63 63  ess needs to acc
1442b 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61 6b  ess it so we mak
1442c 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72 20  e it global for 
1442d 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73 2e  .** test builds.
1442e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f  .**.** Access to
1442f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69   this variable i
14430 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 53  s protected by S
14431 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
14432 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23 69  IC_MASTER..*/.#i
14433 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
14434 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14435 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45  BtShared *SQLITE
14436 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72  _WSD sqlite3Shar
14437 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b  edCacheList = 0;
14438 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42 74  .#else.static Bt
14439 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57  Shared *SQLITE_W
1443a 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 64  SD sqlite3Shared
1443b 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23  CacheList = 0;.#
1443c 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20  endif.#endif /* 
1443d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
1443e 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66  ED_CACHE */..#if
1443f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14440 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a  _SHARED_CACHE./*
14441 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69  .** Enable or di
14442 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 65 64  sable the shared
14443 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 65 6d   pager and schem
14444 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a  a features..**.*
14445 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68  * This routine h
14446 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20  as no effect on 
14447 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73  existing databas
14448 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a  e connections..*
14449 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 61 63  * The shared cac
1444a 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 65 63  he setting effec
1444b 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 63  ts only future c
1444c 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74  alls to.** sqlit
1444d 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 74  e3_open(), sqlit
1444e 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 20  e3_open16(), or 
1444f 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
14450 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
14451 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61   int sqlite3_ena
14452 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
14453 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 20  (int enable){.  
14454 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
14455 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 45  fig.sharedCacheE
14456 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 3b  nabled = enable;
14457 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
14458 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  _OK;.}.#endif...
14459 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65  /*.** Forward de
1445a 63 6c 61 72 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61  claration.*/.sta
1445b 74 69 63 20 69 6e 74 20 63 68 65 63 6b 46 6f 72  tic int checkFor
1445c 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 42 74  ReadConflicts(Bt
1445d 72 65 65 2a 2c 20 50 67 6e 6f 2c 20 42 74 43 75  ree*, Pgno, BtCu
1445e 72 73 6f 72 2a 2c 20 69 36 34 29 3b 0a 0a 0a 23  rsor*, i64);...#
1445f 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
14460 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
14461 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 75 6e   /*.  ** The fun
14462 63 74 69 6f 6e 73 20 71 75 65 72 79 53 68 61 72  ctions queryShar
14463 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
14464 28 29 2c 20 73 65 74 53 68 61 72 65 64 43 61 63  (), setSharedCac
14465 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 0a 20  heTableLock(),. 
14466 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 41 6c 6c   ** and clearAll
14467 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
14468 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 6d 61 6e  Locks().  ** man
14469 69 70 75 6c 61 74 65 20 65 6e 74 72 69 65 73 20  ipulate entries 
1446a 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  in the BtShared.
1446b 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c 69 73  pLock linked lis
1446c 74 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a  t used to store.
1446d 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68    ** shared-cach
1446e 65 20 74 61 62 6c 65 20 6c 65 76 65 6c 20 6c 6f  e table level lo
1446f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c 69 62 72  cks. If the libr
14470 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ary is compiled 
14471 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 68  with the.  ** sh
14472 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75  ared-cache featu
14473 72 65 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65  re disabled, the
14474 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20  n there is only 
14475 65 76 65 72 20 6f 6e 65 20 75 73 65 72 0a 20 20  ever one user.  
14476 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 53 68 61  ** of each BtSha
14477 72 65 64 20 73 74 72 75 63 74 75 72 65 20 61 6e  red structure an
14478 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 6b 69 6e  d so this lockin
14479 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  g is not necessa
1447a 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 64 65 66  ry. .  ** So def
1447b 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 72 65 6c  ine the lock rel
1447c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 61  ated functions a
1447d 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f 0a 20  s no-ops..  */. 
1447e 20 23 64 65 66 69 6e 65 20 71 75 65 72 79 53 68   #define querySh
1447f 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
14480 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45  ck(a,b,c) SQLITE
14481 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 73 65  _OK.  #define se
14482 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  tSharedCacheTabl
14483 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c  eLock(a,b,c) SQL
14484 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65  ITE_OK.  #define
14485 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43   clearAllSharedC
14486 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61  acheTableLocks(a
14487 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  ).#endif..#ifnde
14488 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
14489 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
1448a 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 66   Query to see if
1448b 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20   btree handle p 
1448c 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63  may obtain a loc
1448d 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20  k of type eLock 
1448e 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f  .** (READ_LOCK o
1448f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e  r WRITE_LOCK) on
14490 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20   the table with 
14491 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20  root-page iTab. 
14492 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  Return.** SQLITE
14493 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20  _OK if the lock 
14494 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20  may be obtained 
14495 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73  (by calling.** s
14496 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62  etSharedCacheTab
14497 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51  leLock()), or SQ
14498 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e  LITE_LOCKED if n
14499 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ot..*/.static in
1449a 74 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63  t querySharedCac
1449b 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65  heTableLock(Btre
1449c 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c  e *p, Pgno iTab,
1449d 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74   u8 eLock){.  Bt
1449e 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
1449f 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a  >pBt;.  BtLock *
144a0 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  pIter;..  assert
144a1 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
144a2 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
144a3 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d   assert( eLock==
144a4 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f  READ_LOCK || eLo
144a5 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  ck==WRITE_LOCK )
144a6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  ;.  assert( p->d
144a7 62 21 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20  b!=0 );.  .  /* 
144a8 49 66 20 72 65 71 75 65 73 74 69 6e 67 20 61 20  If requesting a 
144a9 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68 65 6e  write-lock, then
144aa 20 74 68 65 20 42 74 72 65 65 20 6d 75 73 74 20   the Btree must 
144ab 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 69  have an open wri
144ac 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  te.  ** transact
144ad 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69 6c 65  ion on this file
144ae 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73 6c 79  . And, obviously
144af 2c 20 66 6f 72 20 74 68 69 73 20 74 6f 20 62 65  , for this to be
144b0 20 73 6f 20 74 68 65 72 65 20 0a 20 20 2a 2a 20   so there .  ** 
144b1 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20  must be an open 
144b2 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
144b3 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 69 74  n on the file it
144b4 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  self..  */.  ass
144b5 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44  ert( eLock==READ
144b6 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70 42 74  _LOCK || (p==pBt
144b7 2d 3e 70 57 72 69 74 65 72 20 26 26 20 70 2d 3e  ->pWriter && p->
144b8 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
144b9 52 49 54 45 29 20 29 3b 0a 20 20 61 73 73 65 72  RITE) );.  asser
144ba 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c  t( eLock==READ_L
144bb 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72  OCK || pBt->inTr
144bc 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
144bd 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a 20 20 2f  _WRITE );.  .  /
144be 2a 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  * This is a no-o
144bf 70 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d  p if the shared-
144c0 63 61 63 68 65 20 69 73 20 6e 6f 74 20 65 6e 61  cache is not ena
144c1 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 20 21 70  bled */.  if( !p
144c2 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20  ->sharable ){.  
144c3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
144c4 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  OK;.  }..  /* If
144c5 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e   some other conn
144c6 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e  ection is holdin
144c7 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  g an exclusive l
144c8 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a 20 72 65  ock, the.  ** re
144c9 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 6d 61 79  quested lock may
144ca 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64   not be obtained
144cb 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74  ..  */.  if( pBt
144cc 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 26 26 20  ->pWriter!=p && 
144cd 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65  pBt->isExclusive
144ce 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43   ){.    sqlite3C
144cf 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64  onnectionBlocked
144d0 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 57 72  (p->db, pBt->pWr
144d1 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 20 20 72  iter->db);.    r
144d2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
144d3 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b  KED_SHAREDCACHE;
144d4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20  .  }..  /* This 
144d5 28 61 6c 6f 6e 67 20 77 69 74 68 20 73 65 74 53  (along with setS
144d6 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
144d7 6f 63 6b 28 29 29 20 69 73 20 77 68 65 72 65 0a  ock()) is where.
144d8 20 20 2a 2a 20 74 68 65 20 52 65 61 64 55 6e 63    ** the ReadUnc
144d9 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73  ommitted flag is
144da 20 64 65 61 6c 74 20 77 69 74 68 2e 0a 20 20 2a   dealt with..  *
144db 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
144dc 69 73 20 71 75 65 72 79 69 6e 67 20 66 6f 72 20  is querying for 
144dd 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 61  a read-lock on a
144de 6e 79 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 74  ny table.  ** ot
144df 68 65 72 20 74 68 61 6e 20 74 68 65 20 73 71 6c  her than the sql
144e0 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
144e1 20 28 74 61 62 6c 65 20 31 29 20 61 6e 64 20 69   (table 1) and i
144e2 66 20 74 68 65 20 52 65 61 64 55 6e 63 6f 6d 6d  f the ReadUncomm
144e3 69 74 74 65 64 0a 20 20 2a 2a 20 66 6c 61 67 20  itted.  ** flag 
144e4 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  is set, then the
144e5 20 6c 6f 63 6b 20 67 72 61 6e 74 65 64 20 65 76   lock granted ev
144e6 65 6e 20 69 66 20 74 68 65 72 65 20 61 72 65 20  en if there are 
144e7 77 72 69 74 65 2d 6c 6f 63 6b 73 0a 20 20 2a 2a  write-locks.  **
144e8 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49   on the table. I
144e9 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69  f a write-lock i
144ea 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 68 65  s requested, the
144eb 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64   ReadUncommitted
144ec 20 66 6c 61 67 0a 20 20 2a 2a 20 69 73 20 6e 6f   flag.  ** is no
144ed 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 20 20  t considered..  
144ee 2a 2a 0a 20 20 2a 2a 20 49 6e 20 66 75 6e 63 74  **.  ** In funct
144ef 69 6f 6e 20 73 65 74 53 68 61 72 65 64 43 61 63  ion setSharedCac
144f0 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 69  heTableLock(), i
144f1 66 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73  f a read-lock is
144f2 20 64 65 6d 61 6e 64 65 64 20 61 6e 64 20 74 68   demanded and th
144f3 65 20 0a 20 20 2a 2a 20 52 65 61 64 55 6e 63 6f  e .  ** ReadUnco
144f4 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 20  mmitted flag is 
144f5 73 65 74 2c 20 6e 6f 20 65 6e 74 72 79 20 69 73  set, no entry is
144f6 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 6f   added to the lo
144f7 63 6b 73 20 6c 69 73 74 20 0a 20 20 2a 2a 20 28  cks list .  ** (
144f8 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 29 2e  BtShared.pLock).
144f9 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 6f 20 73 75  .  **.  ** To su
144fa 6d 6d 61 72 69 7a 65 3a 20 49 66 20 74 68 65 20  mmarize: If the 
144fb 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
144fc 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
144fd 6e 20 72 65 61 64 20 63 75 72 73 6f 72 73 0a 20  n read cursors. 
144fe 20 2a 2a 20 6f 6e 20 6e 6f 6e 2d 73 63 68 65 6d   ** on non-schem
144ff 61 20 74 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20  a tables do not 
14500 63 72 65 61 74 65 20 6f 72 20 72 65 73 70 65 63  create or respec
14501 74 20 74 61 62 6c 65 20 6c 6f 63 6b 73 2e 20 54  t table locks. T
14502 68 65 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20  he locking.  ** 
14503 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20 61 20  procedure for a 
14504 77 72 69 74 65 2d 63 75 72 73 6f 72 20 64 6f 65  write-cursor doe
14505 73 20 6e 6f 74 20 63 68 61 6e 67 65 2e 0a 20 20  s not change..  
14506 2a 2f 0a 20 20 69 66 28 20 0a 20 20 20 20 30 3d  */.  if( .    0=
14507 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53  =(p->db->flags&S
14508 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
14509 69 74 74 65 64 29 20 7c 7c 20 0a 20 20 20 20 65  itted) || .    e
1450a 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
1450b 20 7c 7c 0a 20 20 20 20 69 54 61 62 3d 3d 4d 41   ||.    iTab==MA
1450c 53 54 45 52 5f 52 4f 4f 54 0a 20 20 29 7b 0a 20  STER_ROOT.  ){. 
1450d 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74     for(pIter=pBt
1450e 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20  ->pLock; pIter; 
1450f 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65  pIter=pIter->pNe
14510 78 74 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  xt){.      /* Th
14511 65 20 63 6f 6e 64 69 74 69 6f 6e 20 28 70 49 74  e condition (pIt
14512 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b  er->eLock!=eLock
14513 29 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ) in the followi
14514 6e 67 20 69 66 28 2e 2e 2e 29 20 0a 20 20 20 20  ng if(...) .    
14515 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69    ** statement i
14516 73 20 61 20 73 69 6d 70 6c 69 66 69 63 61 74 69  s a simplificati
14517 6f 6e 20 6f 66 3a 0a 20 20 20 20 20 20 2a 2a 0a  on of:.      **.
14518 20 20 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63        **   (eLoc
14519 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c  k==WRITE_LOCK ||
1451a 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57   pIter->eLock==W
1451b 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 20  RITE_LOCK).     
1451c 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 73 69 6e   **.      ** sin
1451d 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  ce we know that 
1451e 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  if eLock==WRITE_
1451f 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74  LOCK, then no ot
14520 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20  her connection. 
14521 20 20 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64       ** may hold
14522 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e   a WRITE_LOCK on
14523 20 61 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68   any table in th
14524 69 73 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74  is file (since t
14525 68 65 72 65 20 63 61 6e 0a 20 20 20 20 20 20 2a  here can.      *
14526 2a 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67  * only be a sing
14527 6c 65 20 77 72 69 74 65 72 29 2e 0a 20 20 20 20  le writer)..    
14528 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72    */.      asser
14529 74 28 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d  t( pIter->eLock=
1452a 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49  =READ_LOCK || pI
1452b 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54  ter->eLock==WRIT
1452c 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20  E_LOCK );.      
1452d 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52  assert( eLock==R
1452e 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 74 65  EAD_LOCK || pIte
1452f 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c 7c 20  r->pBtree==p || 
14530 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45  pIter->eLock==RE
14531 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  AD_LOCK);.      
14532 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65  if( pIter->pBtre
14533 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69  e!=p && pIter->i
14534 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70  Table==iTab && p
14535 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f  Iter->eLock!=eLo
14536 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ck ){.        sq
14537 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
14538 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49  locked(p->db, pI
14539 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29  ter->pBtree->db)
1453a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 4c  ;.        if( eL
1453b 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20  ock==WRITE_LOCK 
1453c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  ){.          ass
1453d 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72  ert( p==pBt->pWr
1453e 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20  iter );.        
1453f 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67    pBt->isPending
14540 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
14541 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
14542 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41  QLITE_LOCKED_SHA
14543 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 20 20  REDCACHE;.      
14544 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
14545 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
14546 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
14547 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14548 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
14549 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
1454a 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
1454b 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74   Add a lock on t
1454c 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  he table with ro
1454d 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74  ot-page iTable t
1454e 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  o the shared-btr
1454f 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74  ee used.** by Bt
14550 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61  ree handle p. Pa
14551 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75  rameter eLock mu
14552 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41  st be either REA
14553 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52  D_LOCK or .** WR
14554 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  ITE_LOCK..**.** 
14555 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
14556 75 72 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63  urned if the loc
14557 6b 20 69 73 20 61 64 64 65 64 20 73 75 63 63 65  k is added succe
14558 73 73 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f  ssfully. SQLITE_
14559 42 55 53 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49  BUSY and.** SQLI
1455a 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73  TE_NOMEM may als
1455b 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a  o be returned..*
1455c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74  /.static int set
1455d 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
1455e 4c 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50  Lock(Btree *p, P
1455f 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65  gno iTable, u8 e
14560 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65  Lock){.  BtShare
14561 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
14562 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b  .  BtLock *pLock
14563 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a   = 0;.  BtLock *
14564 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  pIter;..  assert
14565 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
14566 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
14567 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d   assert( eLock==
14568 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f  READ_LOCK || eLo
14569 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  ck==WRITE_LOCK )
1456a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  ;.  assert( p->d
1456b 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  b!=0 );..  /* Th
1456c 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  is is a no-op if
1456d 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
1456e 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64  e is not enabled
1456f 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68   */.  if( !p->sh
14570 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65  arable ){.    re
14571 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
14572 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 53    }..  assert( S
14573 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53  QLITE_OK==queryS
14574 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
14575 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65  ock(p, iTable, e
14576 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  Lock) );..  /* I
14577 66 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d  f the read-uncom
14578 6d 69 74 74 65 64 20 66 6c 61 67 20 69 73 20 73  mitted flag is s
14579 65 74 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f  et and a read-lo
1457a 63 6b 20 69 73 20 72 65 71 75 65 73 74 65 64 20  ck is requested 
1457b 6f 6e 0a 20 20 2a 2a 20 61 20 6e 6f 6e 2d 73 63  on.  ** a non-sc
1457c 68 65 6d 61 20 74 61 62 6c 65 2c 20 74 68 65 6e  hema table, then
1457d 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 6c 77   the lock is alw
1457e 61 79 73 20 67 72 61 6e 74 65 64 2e 20 20 52 65  ays granted.  Re
1457f 74 75 72 6e 20 65 61 72 6c 79 0a 20 20 2a 2a 20  turn early.  ** 
14580 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20 61  without adding a
14581 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 42  n entry to the B
14582 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69  tShared.pLock li
14583 73 74 2e 20 53 65 65 0a 20 20 2a 2a 20 63 6f 6d  st. See.  ** com
14584 6d 65 6e 74 20 69 6e 20 66 75 6e 63 74 69 6f 6e  ment in function
14585 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68   querySharedCach
14586 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 66 6f 72  eTableLock() for
14587 20 6d 6f 72 65 20 69 6e 66 6f 0a 20 20 2a 2a 20   more info.  ** 
14588 6f 6e 20 68 61 6e 64 6c 69 6e 67 20 74 68 65 20  on handling the 
14589 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
1458a 66 6c 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  flag..  */.  if(
1458b 20 0a 20 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c   .    (p->db->fl
1458c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55  ags&SQLITE_ReadU
1458d 6e 63 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a  ncommitted) && .
1458e 20 20 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44      (eLock==READ
1458f 5f 4c 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54  _LOCK) &&.    iT
14590 61 62 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f  able!=MASTER_ROO
14591 54 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  T.  ){.    retur
14592 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
14593 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61  ..  /* First sea
14594 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72  rch the list for
14595 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63   an existing loc
14596 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  k on this table.
14597 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d   */.  for(pIter=
14598 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65  pBt->pLock; pIte
14599 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e  r; pIter=pIter->
1459a 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
1459b 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69  pIter->iTable==i
1459c 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e  Table && pIter->
1459d 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20  pBtree==p ){.   
1459e 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72     pLock = pIter
1459f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
145a0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
145a1 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 61 72  f the above sear
145a2 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20  ch did not find 
145a3 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20  a BtLock struct 
145a4 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65  associating Btre
145a5 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61  e p.  ** with ta
145a6 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f  ble iTable, allo
145a7 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e  cate one and lin
145a8 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69  k it into the li
145a9 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  st..  */.  if( !
145aa 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f  pLock ){.    pLo
145ab 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73  ck = (BtLock *)s
145ac 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
145ad 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29  (sizeof(BtLock))
145ae 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b  ;.    if( !pLock
145af 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
145b0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
145b1 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e     }.    pLock->
145b2 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b  iTable = iTable;
145b3 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72  .    pLock->pBtr
145b4 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63  ee = p;.    pLoc
145b5 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e  k->pNext = pBt->
145b6 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e  pLock;.    pBt->
145b7 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20  pLock = pLock;. 
145b8 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
145b9 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61   BtLock.eLock va
145ba 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61  riable to the ma
145bb 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72  ximum of the cur
145bc 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61  rent lock.  ** a
145bd 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 65 64  nd the requested
145be 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e   lock. This mean
145bf 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63  s if a write-loc
145c0 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 68 65  k was already he
145c1 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65  ld.  ** and a re
145c2 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65  ad-lock requeste
145c3 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f  d, we don't inco
145c4 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64  rrectly downgrad
145c5 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f  e the lock..  */
145c6 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 54 45  .  assert( WRITE
145c7 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20  _LOCK>READ_LOCK 
145c8 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70  );.  if( eLock>p
145c9 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20  Lock->eLock ){. 
145ca 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20     pLock->eLock 
145cb 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20  = eLock;.  }..  
145cc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
145cd 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
145ce 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
145cf 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e  D_CACHE */..#ifn
145d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
145d1 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
145d2 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74  ** Release all t
145d3 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28  he table locks (
145d4 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76  locks obtained v
145d5 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 74  ia calls to.** t
145d6 68 65 20 73 65 74 53 68 61 72 65 64 43 61 63 68  he setSharedCach
145d7 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70 72 6f  eTableLock() pro
145d8 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20  cedure) held by 
145d9 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a  Btree handle p..
145da 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
145db 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74  ion assumes that
145dc 20 68 61 6e 64 6c 65 20 70 20 68 61 73 20 61 6e   handle p has an
145dd 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20 77 72   open read or wr
145de 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74  ite .** transact
145df 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65 73 20  ion. If it does 
145e0 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 42 74  not, then the Bt
145e1 53 68 61 72 65 64 2e 69 73 50 65 6e 64 69 6e 67  Shared.isPending
145e2 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 79   variable.** may
145e3 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c 79 20   be incorrectly 
145e4 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  cleared..*/.stat
145e5 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 6c 6c  ic void clearAll
145e6 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
145e7 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b  Locks(Btree *p){
145e8 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
145e9 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c   = p->pBt;.  BtL
145ea 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d 20 26  ock **ppIter = &
145eb 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 61  pBt->pLock;..  a
145ec 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
145ed 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29  reeHoldsMutex(p)
145ee 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
145ef 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 3d 3d  >sharable || 0==
145f0 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 73 73  *ppIter );.  ass
145f1 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e  ert( p->inTrans>
145f2 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a  0 );..  while( *
145f3 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42 74  ppIter ){.    Bt
145f4 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70  Lock *pLock = *p
145f5 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65 72  pIter;.    asser
145f6 74 28 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73  t( pBt->isExclus
145f7 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70  ive==0 || pBt->p
145f8 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d 3e 70  Writer==pLock->p
145f9 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61 73 73  Btree );.    ass
145fa 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72  ert( pLock->pBtr
145fb 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 4c 6f  ee->inTrans>=pLo
145fc 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 20 20  ck->eLock );.   
145fd 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72   if( pLock->pBtr
145fe 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a  ee==p ){.      *
145ff 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e  ppIter = pLock->
14600 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c  pNext;.      sql
14601 69 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29  ite3_free(pLock)
14602 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
14603 20 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f     ppIter = &pLo
14604 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  ck->pNext;.    }
14605 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
14606 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 3d 3d  pBt->isPending==
14607 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65  0 || pBt->pWrite
14608 72 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  r );.  if( pBt->
14609 70 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20  pWriter==p ){.  
1460a 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d    pBt->pWriter =
1460b 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 45   0;.    pBt->isE
1460c 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 20  xclusive = 0;.  
1460d 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67    pBt->isPending
1460e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
1460f 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74  ( pBt->nTransact
14610 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a  ion==2 ){.    /*
14611 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
14612 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 63 6f  s called when co
14613 6e 6e 65 63 74 69 6f 6e 20 70 20 69 73 20 63 6f  nnection p is co
14614 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20  ncluding its .  
14615 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
14616 2e 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65  . If there curre
14617 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72  ntly exists a wr
14618 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e  iter, and p is n
14619 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77  ot.    ** that w
1461a 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20  riter, then the 
1461b 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20  number of locks 
1461c 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69  held by connecti
1461d 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a  ons other.    **
1461e 20 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72   than the writer
1461f 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74   must be about t
14620 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20  o drop to zero. 
14621 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20  In this case.   
14622 20 2a 2a 20 73 65 74 20 74 68 65 20 69 73 50 65   ** set the isPe
14623 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e  nding flag to 0.
14624 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
14625 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63  f there is not c
14626 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65  urrently a write
14627 72 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64  r, then BtShared
14628 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a  .isPending must.
14629 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61      ** be zero a
1462a 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20  lready. So this 
1462b 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72  next line is har
1462c 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20 63 61  mless in that ca
1462d 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  se..    */.    p
1462e 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20  Bt->isPending = 
1462f 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  0;.  }.}.#endif 
14630 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
14631 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a  HARED_CACHE */..
14632 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65  static void rele
14633 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20  asePage(MemPage 
14634 2a 70 50 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72  *pPage);  /* For
14635 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a  ward reference *
14636 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 69 66 79 20  /../*.** Verify 
14637 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20  that the cursor 
14638 68 6f 6c 64 73 20 61 20 6d 75 74 65 78 20 6f 6e  holds a mutex on
14639 20 74 68 65 20 42 74 53 68 61 72 65 64 0a 2a 2f   the BtShared.*/
1463a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
1463b 73 74 61 74 69 63 20 69 6e 74 20 63 75 72 73 6f  static int curso
1463c 72 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 75  rHoldsMutex(BtCu
1463d 72 73 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 75  rsor *p){.  retu
1463e 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  rn sqlite3_mutex
1463f 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75  _held(p->pBt->mu
14640 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  tex);.}.#endif..
14641 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14642 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a  OMIT_INCRBLOB./*
14643 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74  .** Invalidate t
14644 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
14645 2d 6c 69 73 74 20 63 61 63 68 65 20 66 6f 72 20  -list cache for 
14646 63 75 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20  cursor pCur, if 
14647 61 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  any..*/.static v
14648 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76  oid invalidateOv
14649 65 72 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75  erflowCache(BtCu
1464a 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61  rsor *pCur){.  a
1464b 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
1464c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
1464d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
1464e 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29  pCur->aOverflow)
1464f 3b 0a 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66  ;.  pCur->aOverf
14650 6c 6f 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  low = 0;.}../*.*
14651 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65  * Invalidate the
14652 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c   overflow page-l
14653 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c  ist cache for al
14654 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64  l cursors opened
14655 0a 2a 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65  .** on the share
14656 64 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72  d btree structur
14657 65 20 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  e pBt..*/.static
14658 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65   void invalidate
14659 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
1465a 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b  (BtShared *pBt){
1465b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a  .  BtCursor *p;.
1465c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1465d 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
1465e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f  ->mutex) );.  fo
1465f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72  r(p=pBt->pCursor
14660 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
14661 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65  {.    invalidate
14662 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29  OverflowCache(p)
14663 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20  ;.  }.}.#else.  
14664 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61  #define invalida
14665 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  teOverflowCache(
14666 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76  x).  #define inv
14667 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c  alidateAllOverfl
14668 6f 77 43 61 63 68 65 28 78 29 0a 23 65 6e 64 69  owCache(x).#endi
14669 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62 69 74  f../*.** Set bit
1466a 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42 74 53   pgno of the BtS
1466b 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e  hared.pHasConten
1466c 74 20 62 69 74 76 65 63 2e 20 54 68 69 73 20 69  t bitvec. This i
1466d 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77 68 65  s called .** whe
1466e 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 70 72  n a page that pr
1466f 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e  eviously contain
14670 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65 73 20  ed data becomes 
14671 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66  a free-list leaf
14672 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   .** page..**.**
14673 20 54 68 65 20 42 74 53 68 61 72 65 64 2e 70 48   The BtShared.pH
14674 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63  asContent bitvec
14675 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72 6b 20   exists to work 
14676 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63 75 72  around an obscur
14677 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65 64 20  e.** bug caused 
14678 62 79 20 74 68 65 20 69 6e 74 65 72 61 63 74 69  by the interacti
14679 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66 75 6c  on of two useful
1467a 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   IO optimization
1467b 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a 2a 2a  s surrounding.**
1467c 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
1467d 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  pages:.**.**   1
1467e 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74 61 20  ) When all data 
1467f 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20  is deleted from 
14680 61 20 70 61 67 65 20 61 6e 64 20 74 68 65 20 70  a page and the p
14681 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 20  age becomes.**  
14682 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73 74 20      a free-list 
14683 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 20 70  leaf page, the p
14684 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69 74 74  age is not writt
14685 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  en to the databa
14686 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73 20 66  se.**      (as f
14687 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61  ree-list leaf pa
14688 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f 20 6d  ges contain no m
14689 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61 29 2e  eaningful data).
1468a 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 20 20   Sometimes.**   
1468b 20 20 20 73 75 63 68 20 61 20 70 61 67 65 20 69     such a page i
1468c 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75 72 6e  s not even journ
1468d 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77 69 6c  alled (as it wil
1468e 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65  l not be modifie
1468f 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79 20 62  d,.**      why b
14690 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c 69 6e  other journallin
14691 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20 20 20  g it?)..**.**   
14692 32 29 20 57 68 65 6e 20 61 20 66 72 65 65 2d 6c  2) When a free-l
14693 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69 73  ist leaf page is
14694 20 72 65 75 73 65 64 2c 20 69 74 73 20 63 6f 6e   reused, its con
14695 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65 61 64  tent is not read
14696 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 74 68  .**      from th
14697 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 77 72  e database or wr
14698 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
14699 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79 20 73  rnal file (why s
1469a 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20 20 20  hould it.**     
1469b 20 62 65 2c 20 69 66 20 69 74 20 69 73 20 6e 6f   be, if it is no
1469c 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69 6e 67  t at all meaning
1469d 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42 79 20  ful?)..**.** By 
1469e 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68 65 73  themselves, thes
1469f 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20  e optimizations 
146a0 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20 70 72  work fine and pr
146a1 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a 2a 2a  ovide a handy.**
146a2 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 6f 6f   performance boo
146a3 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c 65 74  st to bulk delet
146a4 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70 65 72  e or insert oper
146a5 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65 72 2c  ations. However,
146a6 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20 69 73   if.** a page is
146a7 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72   moved to the fr
146a8 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68 65 6e  ee-list and then
146a9 20 72 65 75 73 65 64 20 77 69 74 68 69 6e 20 74   reused within t
146aa 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61 6e 73  he same.** trans
146ab 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62 6c 65  action, a proble
146ac 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66 20 74  m comes up. If t
146ad 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 6a  he page is not j
146ae 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e 0a 2a  ournalled when.*
146af 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20 74 6f  * it is moved to
146b0 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61   the free-list a
146b1 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20 6e 6f  nd it is also no
146b2 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65  t journalled whe
146b3 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74 72 61  n it.** is extra
146b4 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72  cted from the fr
146b5 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65 75 73  ee-list and reus
146b6 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 72 69  ed, then the ori
146b7 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 6d 61  ginal data.** ma
146b8 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20 74 68  y be lost. In th
146b9 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 6f 6c  e event of a rol
146ba 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20 6e 6f  lback, it may no
146bb 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a 2a 2a  t be possible.**
146bc 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20   to restore the 
146bd 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73 20  database to its 
146be 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69 67 75  original configu
146bf 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ration..**.** Th
146c0 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 68  e solution is th
146c1 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43  e BtShared.pHasC
146c2 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 57  ontent bitvec. W
146c3 68 65 6e 65 76 65 72 20 61 20 70 61 67 65 20 69  henever a page i
146c4 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f 20 62  s .** moved to b
146c5 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c 69 73  ecome a free-lis
146c6 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65  t leaf page, the
146c7 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62   corresponding b
146c8 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69 6e 20  it is.** set in 
146c9 74 68 65 20 62 69 74 76 65 63 2e 20 57 68 65 6e  the bitvec. When
146ca 65 76 65 72 20 61 20 6c 65 61 66 20 70 61 67 65  ever a leaf page
146cb 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72   is extracted fr
146cc 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  om the free-list
146cd 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f  ,.** optimizatio
146ce 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f 6d 6d  n 2 above is omm
146cf 69 74 74 65 64 20 69 66 20 74 68 65 20 63 6f 72  itted if the cor
146d0 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69  responding bit i
146d1 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 65 74  s already.** set
146d2 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 48 61   in BtShared.pHa
146d3 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20 63 6f  sContent. The co
146d4 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 69  ntents of the bi
146d5 74 76 65 63 20 61 72 65 20 63 6c 65 61 72 65 64  tvec are cleared
146d6 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f  .** at the end o
146d7 66 20 65 76 65 72 79 20 74 72 61 6e 73 61 63 74  f every transact
146d8 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
146d9 6e 74 20 62 74 72 65 65 53 65 74 48 61 73 43 6f  nt btreeSetHasCo
146da 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a  ntent(BtShared *
146db 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b  pBt, Pgno pgno){
146dc 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
146dd 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70 42  TE_OK;.  if( !pB
146de 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 29  t->pHasContent )
146df 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b  {.    int nPage;
146e0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
146e1 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
146e2 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50  pBt->pPager, &nP
146e3 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
146e4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
146e5 20 20 20 20 20 70 42 74 2d 3e 70 48 61 73 43 6f       pBt->pHasCo
146e6 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 42  ntent = sqlite3B
146e7 69 74 76 65 63 43 72 65 61 74 65 28 28 75 33 32  itvecCreate((u32
146e8 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  )nPage);.      i
146e9 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e  f( !pBt->pHasCon
146ea 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  tent ){.        
146eb 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
146ec 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  M;.      }.    }
146ed 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
146ee 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 67 6e 6f  QLITE_OK && pgno
146ef 3c 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  <=sqlite3BitvecS
146f0 69 7a 65 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e  ize(pBt->pHasCon
146f1 74 65 6e 74 29 20 29 7b 0a 20 20 20 20 72 63 20  tent) ){.    rc 
146f2 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  = sqlite3BitvecS
146f3 65 74 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74  et(pBt->pHasCont
146f4 65 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a  ent, pgno);.  }.
146f5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
146f6 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20  /*.** Query the 
146f7 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e  BtShared.pHasCon
146f8 74 65 6e 74 20 76 65 63 74 6f 72 2e 0a 2a 2a 0a  tent vector..**.
146f9 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
146fa 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
146fb 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66  a free-list leaf
146fc 20 70 61 67 65 20 69 73 20 72 65 6d 6f 76 65 64   page is removed
146fd 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 66 72 65   from the.** fre
146fe 65 2d 6c 69 73 74 20 66 6f 72 20 72 65 75 73 65  e-list for reuse
146ff 2e 20 49 74 20 72 65 74 75 72 6e 73 20 66 61 6c  . It returns fal
14700 73 65 20 69 66 20 69 74 20 69 73 20 73 61 66 65  se if it is safe
14701 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65   to retrieve the
14702 0a 2a 2a 20 70 61 67 65 20 66 72 6f 6d 20 74 68  .** page from th
14703 65 20 70 61 67 65 72 20 6c 61 79 65 72 20 77 69  e pager layer wi
14704 74 68 20 74 68 65 20 27 6e 6f 2d 63 6f 6e 74 65  th the 'no-conte
14705 6e 74 27 20 66 6c 61 67 20 73 65 74 2e 20 54 72  nt' flag set. Tr
14706 75 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f  ue otherwise..*/
14707 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
14708 65 47 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42  eGetHasContent(B
14709 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67  tShared *pBt, Pg
1470a 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 42 69 74 76  no pgno){.  Bitv
1470b 65 63 20 2a 70 20 3d 20 70 42 74 2d 3e 70 48 61  ec *p = pBt->pHa
1470c 73 43 6f 6e 74 65 6e 74 3b 0a 20 20 72 65 74 75  sContent;.  retu
1470d 72 6e 20 28 70 20 26 26 20 28 70 67 6e 6f 3e 73  rn (p && (pgno>s
1470e 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a 65  qlite3BitvecSize
1470f 28 70 29 20 7c 7c 20 73 71 6c 69 74 65 33 42 69  (p) || sqlite3Bi
14710 74 76 65 63 54 65 73 74 28 70 2c 20 70 67 6e 6f  tvecTest(p, pgno
14711 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  )));.}../*.** Cl
14712 65 61 72 20 28 64 65 73 74 72 6f 79 29 20 74 68  ear (destroy) th
14713 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43  e BtShared.pHasC
14714 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 54  ontent bitvec. T
14715 68 69 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  his should be.**
14716 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 20   invoked at the 
14717 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 65 61  conclusion of ea
14718 63 68 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  ch write-transac
14719 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
1471a 76 6f 69 64 20 62 74 72 65 65 43 6c 65 61 72 48  void btreeClearH
1471b 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72  asContent(BtShar
1471c 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69  ed *pBt){.  sqli
1471d 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
1471e 28 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e  (pBt->pHasConten
1471f 74 29 3b 0a 20 20 70 42 74 2d 3e 70 48 61 73 43  t);.  pBt->pHasC
14720 6f 6e 74 65 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  ontent = 0;.}../
14721 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75  *.** Save the cu
14722 72 72 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73  rrent cursor pos
14723 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 61 72  ition in the var
14724 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e  iables BtCursor.
14725 6e 4b 65 79 20 0a 2a 2a 20 61 6e 64 20 42 74 43  nKey .** and BtC
14726 75 72 73 6f 72 2e 70 4b 65 79 2e 20 54 68 65 20  ursor.pKey. The 
14727 63 75 72 73 6f 72 27 73 20 73 74 61 74 65 20 69  cursor's state i
14728 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f 52 5f  s set to CURSOR_
14729 52 45 51 55 49 52 45 53 45 45 4b 2e 0a 2a 2f 0a  REQUIRESEEK..*/.
1472a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 43  static int saveC
1472b 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74  ursorPosition(Bt
1472c 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
1472d 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
1472e 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44  rt( CURSOR_VALID
1472f 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29  ==pCur->eState )
14730 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70  ;.  assert( 0==p
14731 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61  Cur->pKey );.  a
14732 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
14733 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
14734 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
14735 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75  BtreeKeySize(pCu
14736 72 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b  r, &pCur->nKey);
14737 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
14738 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c  s an intKey tabl
14739 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62 6f 76  e, then the abov
1473a 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65 4b  e call to BtreeK
1473b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20 73 74  eySize().  ** st
1473c 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67 65 72  ores the integer
1473d 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e 6e 4b   key in pCur->nK
1473e 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ey. In this case
1473f 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 0a 20   this value is. 
14740 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 73 20   ** all that is 
14741 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65 72 77  required. Otherw
14742 69 73 65 2c 20 69 66 20 70 43 75 72 20 69 73 20  ise, if pCur is 
14743 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69  not open on an i
14744 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62 6c 65  ntKey.  ** table
14745 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20 73 70  , then malloc sp
14746 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74 6f 72  ace for and stor
14747 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b 65 79  e the pCur->nKey
14748 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 0a 20   bytes of key . 
14749 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f 0a 20   ** data..  */. 
1474a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1474b 4f 4b 20 26 26 20 30 3d 3d 70 43 75 72 2d 3e 61  OK && 0==pCur->a
1474c 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79  pPage[0]->intKey
1474d 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4b 65  ){.    void *pKe
1474e 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  y = sqlite3Mallo
1474f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b  c( (int)pCur->nK
14750 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4b  ey );.    if( pK
14751 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ey ){.      rc =
14752 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
14753 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 70  (pCur, 0, (int)p
14754 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79 29  Cur->nKey, pKey)
14755 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
14756 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
14757 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20       pCur->pKey 
14758 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65  = pKey;.      }e
14759 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
1475a 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 29 3b  ite3_free(pKey);
1475b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
1475c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  se{.      rc = S
1475d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1475e 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
1475f 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30   !pCur->apPage[0
14760 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 70 43  ]->intKey || !pC
14761 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 20 69  ur->pKey );..  i
14762 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
14763 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
14764 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70     for(i=0; i<=p
14765 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29  Cur->iPage; i++)
14766 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  {.      releaseP
14767 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65  age(pCur->apPage
14768 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43 75 72  [i]);.      pCur
14769 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b  ->apPage[i] = 0;
1476a 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d  .    }.    pCur-
1476b 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 20  >iPage = -1;.   
1476c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
1476d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
1476e 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76 61 6c  EK;.  }..  inval
1476f 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63  idateOverflowCac
14770 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75  he(pCur);.  retu
14771 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
14772 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f  Save the positio
14773 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72  ns of all cursor
14774 73 20 65 78 63 65 70 74 20 70 45 78 63 65 70 74  s except pExcept
14775 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 61 62   open on the tab
14776 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f 6f 74  le .** with root
14777 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75  -page iRoot. Usu
14778 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61  ally, this is ca
14779 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65  lled just before
1477a 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65   cursor.** pExce
1477b 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f  pt is used to mo
1477c 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 20 28  dify the table (
1477d 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72  BtreeDelete() or
1477e 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e   BtreeInsert()).
1477f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
14780 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74  aveAllCursors(Bt
14781 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
14782 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f  o iRoot, BtCurso
14783 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42  r *pExcept){.  B
14784 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73  tCursor *p;.  as
14785 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14786 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
14787 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
14788 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20  ( pExcept==0 || 
14789 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42  pExcept->pBt==pB
1478a 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74  t );.  for(p=pBt
1478b 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d  ->pCursor; p; p=
1478c 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  p->pNext){.    i
1478d 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 26 26  f( p!=pExcept &&
1478e 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d   (0==iRoot || p-
1478f 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74  >pgnoRoot==iRoot
14790 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 2d  ) && .        p-
14791 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
14792 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 69  VALID ){.      i
14793 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 72 73  nt rc = saveCurs
14794 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20  orPosition(p);. 
14795 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f       if( SQLITE_
14796 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20  OK!=rc ){.      
14797 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
14798 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
14799 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1479a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  K;.}../*.** Clea
1479b 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75  r the current cu
1479c 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  rsor position..*
1479d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1479e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
1479f 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74  eeClearCursor(Bt
147a0 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
147a1 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
147a2 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
147a3 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
147a4 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20  e(pCur->pKey);. 
147a5 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b   pCur->pKey = 0;
147a6 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20  .  pCur->eState 
147a7 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
147a8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f  ;.}../*.** Resto
147a9 72 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  re the cursor to
147aa 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74   the position it
147ab 20 77 61 73 20 69 6e 20 28 6f 72 20 61 73 20 63   was in (or as c
147ac 6c 6f 73 65 20 74 6f 20 61 73 20 70 6f 73 73 69  lose to as possi
147ad 62 6c 65 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76  ble).** when sav
147ae 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
147af 29 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f  ) was called. No
147b0 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6c  te that this cal
147b1 6c 20 64 65 6c 65 74 65 73 20 74 68 65 20 0a 2a  l deletes the .*
147b2 2a 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e  * saved position
147b3 20 69 6e 66 6f 20 73 74 6f 72 65 64 20 62 79 20   info stored by 
147b4 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69  saveCursorPositi
147b5 6f 6e 28 29 2c 20 73 6f 20 74 68 65 72 65 20 63  on(), so there c
147b6 61 6e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74  an be.** at most
147b7 20 6f 6e 65 20 65 66 66 65 63 74 69 76 65 20 72   one effective r
147b8 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
147b9 74 69 6f 6e 28 29 20 63 61 6c 6c 20 61 66 74 65  tion() call afte
147ba 72 20 65 61 63 68 20 0a 2a 2a 20 73 61 76 65 43  r each .** saveC
147bb 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e  ursorPosition().
147bc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
147bd 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
147be 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72  reeRestoreCursor
147bf 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f  Position(BtCurso
147c0 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20  r *pCur){.  int 
147c1 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75  rc;.  assert( cu
147c2 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
147c3 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
147c4 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d  ( pCur->eState>=
147c5 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
147c6 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  EK );.  if( pCur
147c7 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
147c8 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65  _FAULT ){.    re
147c9 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b  turn pCur->skip;
147ca 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74  .  }.  pCur->eSt
147cb 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
147cc 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ALID;.  rc = sql
147cd 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28  ite3BtreeMoveto(
147ce 70 43 75 72 2c 20 70 43 75 72 2d 3e 70 4b 65 79  pCur, pCur->pKey
147cf 2c 20 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c  , pCur->nKey, 0,
147d0 20 26 70 43 75 72 2d 3e 73 6b 69 70 29 3b 0a 20   &pCur->skip);. 
147d1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
147d2 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
147d3 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b 65  3_free(pCur->pKe
147d4 79 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b  y);.    pCur->pK
147d5 65 79 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65  ey = 0;.    asse
147d6 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
147d7 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c  ==CURSOR_VALID |
147d8 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  | pCur->eState==
147d9 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29  CURSOR_INVALID )
147da 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
147db 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65  c;.}..#define re
147dc 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
147dd 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65  ion(p) \.  (p->e
147de 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45  State>=CURSOR_RE
147df 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20  QUIRESEEK ? \.  
147e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
147e1 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72  reeRestoreCursor
147e2 50 6f 73 69 74 69 6f 6e 28 70 29 20 3a 20 5c 0a  Position(p) : \.
147e3 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
147e4 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  OK)../*.** Deter
147e5 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20  mine whether or 
147e6 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68 61 73  not a cursor has
147e7 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20   moved from the 
147e8 70 6f 73 69 74 69 6f 6e 20 69 74 0a 2a 2a 20 77  position it.** w
147e9 61 73 20 6c 61 73 74 20 70 6c 61 63 65 64 20 61  as last placed a
147ea 74 2e 20 20 43 75 72 73 6f 72 73 20 63 61 6e 20  t.  Cursors can 
147eb 6d 6f 76 65 20 77 68 65 6e 20 74 68 65 20 72 6f  move when the ro
147ec 77 20 74 68 65 79 20 61 72 65 20 70 6f 69 6e 74  w they are point
147ed 69 6e 67 0a 2a 2a 20 61 74 20 69 73 20 64 65 6c  ing.** at is del
147ee 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e  eted out from un
147ef 64 65 72 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20  der them..**.** 
147f0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
147f1 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f  urns an error co
147f2 64 65 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20  de if something 
147f3 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 54 68 65  goes wrong.  The
147f4 0a 2a 2a 20 69 6e 74 65 67 65 72 20 2a 70 48 61  .** integer *pHa
147f5 73 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f  sMoved is set to
147f6 20 6f 6e 65 20 69 66 20 74 68 65 20 63 75 72 73   one if the curs
147f7 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 61 6e 64  or has moved and
147f8 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 53 51   0 if not..*/.SQ
147f9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
147fa 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
147fb 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75  sorHasMoved(BtCu
147fc 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
147fd 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a 20 20 69  *pHasMoved){.  i
147fe 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72  nt rc;..  rc = r
147ff 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
14800 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66  tion(pCur);.  if
14801 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 48 61  ( rc ){.    *pHa
14802 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 20 20  sMoved = 1;.    
14803 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
14804 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
14805 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e!=CURSOR_VALID 
14806 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 21 3d 30  || pCur->skip!=0
14807 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76   ){.    *pHasMov
14808 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  ed = 1;.  }else{
14809 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20  .    *pHasMoved 
1480a 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
1480b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1480c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1480d 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f  MIT_AUTOVACUUM./
1480e 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 70 61 67  *.** Given a pag
1480f 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 72 65  e number of a re
14810 67 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 70  gular database p
14811 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  age, return the 
14812 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66  page.** number f
14813 6f 72 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  or the pointer-m
14814 61 70 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e  ap page that con
14815 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72 79 20  tains the entry 
14816 66 6f 72 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74  for the.** input
14817 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f   page number..*/
14818 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 74 72  .static Pgno ptr
14819 6d 61 70 50 61 67 65 6e 6f 28 42 74 53 68 61 72  mapPageno(BtShar
1481a 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67  ed *pBt, Pgno pg
1481b 6e 6f 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65  no){.  int nPage
1481c 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 50  sPerMapPage;.  P
1481d 67 6e 6f 20 69 50 74 72 4d 61 70 2c 20 72 65 74  gno iPtrMap, ret
1481e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1481f 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14820 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
14821 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65  nPagesPerMapPage
14822 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53   = (pBt->usableS
14823 69 7a 65 2f 35 29 2b 31 3b 0a 20 20 69 50 74 72  ize/5)+1;.  iPtr
14824 4d 61 70 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e  Map = (pgno-2)/n
14825 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b  PagesPerMapPage;
14826 0a 20 20 72 65 74 20 3d 20 28 69 50 74 72 4d 61  .  ret = (iPtrMa
14827 70 2a 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61  p*nPagesPerMapPa
14828 67 65 29 20 2b 20 32 3b 20 0a 20 20 69 66 28 20  ge) + 2; .  if( 
14829 72 65 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  ret==PENDING_BYT
1482a 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
1482b 20 20 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20     ret++;.  }.  
1482c 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f  return ret;.}../
1482d 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e  *.** Write an en
1482e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69  try into the poi
1482f 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20  nter map..**.** 
14830 54 68 69 73 20 72 6f 75 74 69 6e 65 20 75 70 64  This routine upd
14831 61 74 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72  ates the pointer
14832 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70   map entry for p
14833 61 67 65 20 6e 75 6d 62 65 72 20 27 6b 65 79 27  age number 'key'
14834 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20 6d  .** so that it m
14835 61 70 73 20 74 6f 20 74 79 70 65 20 27 65 54 79  aps to type 'eTy
14836 70 65 27 20 61 6e 64 20 70 61 72 65 6e 74 20 70  pe' and parent p
14837 61 67 65 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f  age number 'pgno
14838 27 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63  '..** An error c
14839 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
1483a 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  if something goe
1483b 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69  s wrong, otherwi
1483c 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  se SQLITE_OK..*/
1483d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d  .static int ptrm
1483e 61 70 50 75 74 28 42 74 53 68 61 72 65 64 20 2a  apPut(BtShared *
1483f 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75  pBt, Pgno key, u
14840 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61  8 eType, Pgno pa
14841 72 65 6e 74 29 7b 0a 20 20 44 62 50 61 67 65 20  rent){.  DbPage 
14842 2a 70 44 62 50 61 67 65 3b 20 20 2f 2a 20 54 68  *pDbPage;  /* Th
14843 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  e pointer map pa
14844 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72  ge */.  u8 *pPtr
14845 6d 61 70 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  map;      /* The
14846 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 64 61 74   pointer map dat
14847 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72  a */.  Pgno iPtr
14848 6d 61 70 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  map;     /* The 
14849 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
1484a 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74   number */.  int
1484b 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 2f   offset;       /
1484c 2a 20 4f 66 66 73 65 74 20 69 6e 20 70 6f 69 6e  * Offset in poin
1484d 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a  ter map page */.
1484e 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
1484f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14850 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
14851 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20  ex) );.  /* The 
14852 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70  master-journal p
14853 61 67 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20  age number must 
14854 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61 73  never be used as
14855 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70   a pointer map p
14856 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  age */.  assert(
14857 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50 41 47   0==PTRMAP_ISPAG
14858 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42  E(pBt, PENDING_B
14859 59 54 45 5f 50 41 47 45 28 70 42 74 29 29 20 29  YTE_PAGE(pBt)) )
1485a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  ;..  assert( pBt
1485b 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a  ->autoVacuum );.
1485c 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a    if( key==0 ){.
1485d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1485e 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1485f 20 20 7d 0a 20 20 69 50 74 72 6d 61 70 20 3d 20    }.  iPtrmap = 
14860 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42  PTRMAP_PAGENO(pB
14861 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20  t, key);.  rc = 
14862 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
14863 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74  pBt->pPager, iPt
14864 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b  rmap, &pDbPage);
14865 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
14866 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
14867 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 6f 66 66  rn rc;.  }.  off
14868 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52  set = PTRMAP_PTR
14869 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20  OFFSET(iPtrmap, 
1486a 6b 65 79 29 3b 0a 20 20 70 50 74 72 6d 61 70 20  key);.  pPtrmap 
1486b 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50  = (u8 *)sqlite3P
1486c 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
1486d 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79  age);..  if( eTy
1486e 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73  pe!=pPtrmap[offs
1486f 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28  et] || get4byte(
14870 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b  &pPtrmap[offset+
14871 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20  1])!=parent ){. 
14872 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41     TRACE(("PTRMA
14873 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25  P_UPDATE: %d->(%
14874 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65  d,%d)\n", key, e
14875 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a  Type, parent));.
14876 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14877 50 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61  PagerWrite(pDbPa
14878 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ge);.    if( rc=
14879 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1487a 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 66 73      pPtrmap[offs
1487b 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20 20 20  et] = eType;.   
1487c 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 74     put4byte(&pPt
1487d 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20  rmap[offset+1], 
1487e 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20  parent);.    }. 
1487f 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67   }..  sqlite3Pag
14880 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29  erUnref(pDbPage)
14881 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
14882 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20  ../*.** Read an 
14883 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 70  entry from the p
14884 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a  ointer map..**.*
14885 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
14886 65 74 72 69 65 76 65 73 20 74 68 65 20 70 6f 69  etrieves the poi
14887 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 20 66  nter map entry f
14888 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c 20 77  or page 'key', w
14889 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79  riting.** the ty
1488a 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61  pe and parent pa
1488b 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45  ge number to *pE
1488c 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20  Type and *pPgno 
1488d 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a  respectively..**
1488e 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   An error code i
1488f 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 6f  s returned if so
14890 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
14891 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 53 51  ng, otherwise SQ
14892 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74  LITE_OK..*/.stat
14893 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47 65 74  ic int ptrmapGet
14894 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20  (BtShared *pBt, 
14895 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45  Pgno key, u8 *pE
14896 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e  Type, Pgno *pPgn
14897 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44  o){.  DbPage *pD
14898 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 65 20  bPage;   /* The 
14899 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
1489a 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72 6d 61   */.  int iPtrma
1489b 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  p;       /* Poin
1489c 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 6e 64  ter map page ind
1489d 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72  ex */.  u8 *pPtr
1489e 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f  map;       /* Po
1489f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 64  inter map page d
148a0 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66  ata */.  int off
148a1 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f  set;        /* O
148a2 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79 20 69  ffset of entry i
148a3 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f  n pointer map */
148a4 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
148a5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
148a6 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
148a7 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d  tex) );..  iPtrm
148a8 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45  ap = PTRMAP_PAGE
148a9 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20  NO(pBt, key);.  
148aa 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
148ab 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72  rGet(pBt->pPager
148ac 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50  , iPtrmap, &pDbP
148ad 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  age);.  if( rc!=
148ae 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
148af 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61  rc;.  }.  pPtrma
148b0 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  p = (u8 *)sqlite
148b1 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44  3PagerGetData(pD
148b2 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65  bPage);..  offse
148b3 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46  t = PTRMAP_PTROF
148b4 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65  FSET(iPtrmap, ke
148b5 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  y);.  assert( pE
148b6 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45  Type!=0 );.  *pE
148b7 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f  Type = pPtrmap[o
148b8 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20 70 50  ffset];.  if( pP
148b9 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67  gno ) *pPgno = g
148ba 65 74 34 62 79 74 65 28 26 70 50 74 72 6d 61 70  et4byte(&pPtrmap
148bb 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20  [offset+1]);..  
148bc 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
148bd 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66  f(pDbPage);.  if
148be 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a  ( *pEType<1 || *
148bf 70 45 54 79 70 65 3e 35 20 29 20 72 65 74 75 72  pEType>5 ) retur
148c0 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
148c1 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72 6e 20  _BKPT;.  return 
148c2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65  SQLITE_OK;.}..#e
148c3 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69 6e 65  lse /* if define
148c4 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  d SQLITE_OMIT_AU
148c5 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20 23 64  TOVACUUM */.  #d
148c6 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 28  efine ptrmapPut(
148c7 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f  w,x,y,z) SQLITE_
148c8 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72  OK.  #define ptr
148c9 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20  mapGet(w,x,y,z) 
148ca 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66  SQLITE_OK.  #def
148cb 69 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66  ine ptrmapPutOvf
148cc 6c 28 79 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b  l(y,z) SQLITE_OK
148cd 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47  .#endif../*.** G
148ce 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 67  iven a btree pag
148cf 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64  e and a cell ind
148d0 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 20  ex (0 means the 
148d1 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a  first cell on.**
148d2 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65 61   the page, 1 mea
148d3 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63 65  ns the second ce
148d4 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  ll, and so forth
148d5 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  ) return a point
148d6 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c  er.** to the cel
148d7 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a  l content..**.**
148d8 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f   This routine wo
148d9 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67  rks only for pag
148da 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 63  es that do not c
148db 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20  ontain overflow 
148dc 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  cells..*/.#defin
148dd 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20  e findCell(P,I) 
148de 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61 20  \.  ((P)->aData 
148df 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65  + ((P)->maskPage
148e0 20 26 20 67 65 74 32 62 79 74 65 28 26 28 50 29   & get2byte(&(P)
148e1 2d 3e 61 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c  ->aData[(P)->cel
148e2 6c 4f 66 66 73 65 74 2b 32 2a 28 49 29 5d 29 29  lOffset+2*(I)]))
148e3 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20  )../*.** This a 
148e4 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76 65 72  more complex ver
148e5 73 69 6f 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c  sion of findCell
148e6 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20 66 6f  () that works fo
148e7 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20  r.** pages that 
148e8 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66  do contain overf
148e9 6c 6f 77 20 63 65 6c 6c 73 2e 20 20 53 65 65 20  low cells.  See 
148ea 69 6e 73 65 72 74 0a 2a 2f 0a 73 74 61 74 69 63  insert.*/.static
148eb 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f   u8 *findOverflo
148ec 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70  wCell(MemPage *p
148ed 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29  Page, int iCell)
148ee 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  {.  int i;.  ass
148ef 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
148f0 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
148f1 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
148f2 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76  for(i=pPage->nOv
148f3 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20  erflow-1; i>=0; 
148f4 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b  i--){.    int k;
148f5 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76 66  .    struct _Ovf
148f6 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20  lCell *pOvfl;.  
148f7 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67 65    pOvfl = &pPage
148f8 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20  ->aOvfl[i];.    
148f9 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a  k = pOvfl->idx;.
148fa 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c      if( k<=iCell
148fb 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d   ){.      if( k=
148fc 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20  =iCell ){.      
148fd 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e    return pOvfl->
148fe 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  pCell;.      }. 
148ff 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20       iCell--;.  
14900 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
14901 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
14902 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   iCell);.}../*.*
14903 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 63  * Parse a cell c
14904 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64  ontent block and
14905 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c   fill in the Cel
14906 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  lInfo structure.
14907 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74    There.** are t
14908 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  wo versions of t
14909 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 73  his function.  s
1490a 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
1490b 43 65 6c 6c 28 29 20 74 61 6b 65 73 20 61 20 0a  Cell() takes a .
1490c 2a 2a 20 63 65 6c 6c 20 69 6e 64 65 78 20 61 73  ** cell index as
1490d 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
1490e 6d 65 6e 74 20 61 6e 64 20 73 71 6c 69 74 65 33  ment and sqlite3
1490f 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  BtreeParseCellPt
14910 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20  r() .** takes a 
14911 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62  pointer to the b
14912 6f 64 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  ody of the cell 
14913 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72  as its second ar
14914 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69  gument..**.** Wi
14915 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20  thin this file, 
14916 74 68 65 20 70 61 72 73 65 43 65 6c 6c 28 29 20  the parseCell() 
14917 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 63 61 6c  macro can be cal
14918 6c 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a  led instead of.*
14919 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  * sqlite3BtreePa
1491a 72 73 65 43 65 6c 6c 50 74 72 28 29 2e 20 55 73  rseCellPtr(). Us
1491b 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65  ing some compile
1491c 72 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65  rs, this will be
1491d 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49   faster..*/.SQLI
1491e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1491f 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
14920 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50  eCellPtr(.  MemP
14921 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20  age *pPage,     
14922 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74      /* Page cont
14923 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20  aining the cell 
14924 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20  */.  u8 *pCell, 
14925 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14926 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  Pointer to the c
14927 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43  ell text. */.  C
14928 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20  ellInfo *pInfo  
14929 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69         /* Fill i
1492a 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
1492b 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20   */.){.  u16 n; 
1492c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1492d 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74 65 73   /* Number bytes
1492e 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74   in cell content
1492f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32   header */.  u32
14930 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 20 20   nPayload;      
14931 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14932 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c 6c 20  f bytes of cell 
14933 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73  payload */..  as
14934 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14935 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
14936 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  pBt->mutex) );..
14937 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d    pInfo->pCell =
14938 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74   pCell;.  assert
14939 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30  ( pPage->leaf==0
1493a 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d   || pPage->leaf=
1493b 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67  =1 );.  n = pPag
1493c 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 3b  e->childPtrSize;
1493d 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d  .  assert( n==4-
1493e 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b  4*pPage->leaf );
1493f 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e  .  if( pPage->in
14940 74 4b 65 79 20 29 7b 0a 20 20 20 20 69 66 28 20  tKey ){.    if( 
14941 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29  pPage->hasData )
14942 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 67 65 74  {.      n += get
14943 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b  Varint32(&pCell[
14944 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20  n], nPayload);. 
14945 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
14946 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20  nPayload = 0;.  
14947 20 20 7d 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74    }.    n += get
14948 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d  Varint(&pCell[n]
14949 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e  , (u64*)&pInfo->
1494a 6e 4b 65 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f  nKey);.    pInfo
1494b 2d 3e 6e 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f  ->nData = nPaylo
1494c 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ad;.  }else{.   
1494d 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20   pInfo->nData = 
1494e 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56  0;.    n += getV
1494f 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e  arint32(&pCell[n
14950 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20  ], nPayload);.  
14951 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20    pInfo->nKey = 
14952 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20  nPayload;.  }.  
14953 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20  pInfo->nPayload 
14954 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49  = nPayload;.  pI
14955 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e  nfo->nHeader = n
14956 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 6e  ;.  if( likely(n
14957 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e  Payload<=pPage->
14958 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 20  maxLocal) ){.   
14959 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20   /* This is the 
1495a 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61  (easy) common ca
1495b 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e 74  se where the ent
1495c 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 73  ire payload fits
1495d 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c  .    ** on the l
1495e 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f  ocal page.  No o
1495f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 69  verflow is requi
14960 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  red..    */.    
14961 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20 20  int nSize;      
14962 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a      /* Total siz
14963 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e  e of cell conten
14964 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  t in bytes */.  
14965 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f    nSize = nPaylo
14966 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e 66  ad + n;.    pInf
14967 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36  o->nLocal = (u16
14968 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70  )nPayload;.    p
14969 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20  Info->iOverflow 
1496a 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e 53  = 0;.    if( (nS
1496b 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b 0a  ize & ~3)==0 ){.
1496c 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b        nSize = 4;
1496d 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
1496e 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20  um cell size is 
1496f 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 70  4 */.    }.    p
14970 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 75  Info->nSize = (u
14971 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c 73  16)nSize;.  }els
14972 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  e{.    /* If the
14973 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e 6f   payload will no
14974 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c 79  t fit completely
14975 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61   on the local pa
14976 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20  ge, we have.    
14977 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f 77  ** to decide how
14978 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 6c   much to store l
14979 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 6d  ocally and how m
1497a 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e 74  uch to spill ont
1497b 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f  o.    ** overflo
1497c 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 74  w pages.  The st
1497d 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 6e  rategy is to min
1497e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74  imize the amount
1497f 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 2a   of unused.    *
14980 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 66  * space on overf
14981 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 20  low pages while 
14982 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f 75  keeping the amou
14983 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f 72  nt of local stor
14984 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 65  age.    ** in be
14985 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61  tween minLocal a
14986 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 20  nd maxLocal..   
14987 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e 69   **.    ** Warni
14988 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 68  ng:  changing th
14989 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 70  e way overflow p
1498a 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 69  ayload is distri
1498b 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 20  buted in any.   
1498c 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 73   ** way will res
1498d 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70  ult in an incomp
1498e 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 6d  atible file form
1498f 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  at..    */.    i
14990 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a  nt minLocal;  /*
14991 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20   Minimum amount 
14992 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20  of payload held 
14993 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69  locally */.    i
14994 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a  nt maxLocal;  /*
14995 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20   Maximum amount 
14996 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20  of payload held 
14997 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69  locally */.    i
14998 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a  nt surplus;   /*
14999 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61   Overflow payloa
1499a 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20  d available for 
1499b 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f  local storage */
1499c 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d  ..    minLocal =
1499d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c   pPage->minLocal
1499e 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 3d  ;.    maxLocal =
1499f 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c   pPage->maxLocal
149a0 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d 20  ;.    surplus = 
149a1 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 79  minLocal + (nPay
149a2 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29  load - minLocal)
149a3 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  %(pPage->pBt->us
149a4 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20  ableSize - 4);. 
149a5 20 20 20 69 66 28 20 73 75 72 70 6c 75 73 20 3c     if( surplus <
149a6 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20  = maxLocal ){.  
149a7 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61      pInfo->nLoca
149a8 6c 20 3d 20 28 75 31 36 29 73 75 72 70 6c 75 73  l = (u16)surplus
149a9 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
149aa 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c     pInfo->nLocal
149ab 20 3d 20 28 75 31 36 29 6d 69 6e 4c 6f 63 61 6c   = (u16)minLocal
149ac 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66  ;.    }.    pInf
149ad 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 28  o->iOverflow = (
149ae 75 31 36 29 28 70 49 6e 66 6f 2d 3e 6e 4c 6f 63  u16)(pInfo->nLoc
149af 61 6c 20 2b 20 6e 29 3b 0a 20 20 20 20 70 49 6e  al + n);.    pIn
149b0 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70 49 6e 66  fo->nSize = pInf
149b1 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 2b 20 34  o->iOverflow + 4
149b2 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20  ;.  }.}.#define 
149b3 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c  parseCell(pPage,
149b4 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 20 5c   iCell, pInfo) \
149b5 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  .  sqlite3BtreeP
149b6 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 50 61  arseCellPtr((pPa
149b7 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 28 70  ge), findCell((p
149b8 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 29 2c  Page), (iCell)),
149b9 20 28 70 49 6e 66 6f 29 29 0a 53 51 4c 49 54 45   (pInfo)).SQLITE
149ba 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
149bb 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
149bc 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ell(.  MemPage *
149bd 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f  pPage,         /
149be 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
149bf 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20  g the cell */.  
149c0 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 20 20  int iCell,      
149c1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
149c2 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 72 73  ell index.  Firs
149c3 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f 0a 20  t cell is 0 */. 
149c4 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f   CellInfo *pInfo
149c5 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c           /* Fill
149c6 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75   in this structu
149c7 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72 73 65  re */.){.  parse
149c8 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c  Cell(pPage, iCel
149c9 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a  l, pInfo);.}../*
149ca 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20  .** Compute the 
149cb 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
149cc 62 79 74 65 73 20 74 68 61 74 20 61 20 43 65 6c  bytes that a Cel
149cd 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65 20 63  l needs in the c
149ce 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 65 61  ell.** data area
149cf 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d 70 61   of the btree-pa
149d0 67 65 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  ge.  The return 
149d1 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 73 20  number includes 
149d2 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61  the cell.** data
149d3 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 65 20   header and the 
149d4 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c 20 62  local payload, b
149d5 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 72 66  ut not any overf
149d6 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74  low page or.** t
149d7 68 65 20 73 70 61 63 65 20 75 73 65 64 20 62 79  he space used by
149d8 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65   the cell pointe
149d9 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31 36  r..*/.static u16
149da 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d   cellSizePtr(Mem
149db 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20  Page *pPage, u8 
149dc 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70  *pCell){.  u8 *p
149dd 49 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b 70 50  Iter = &pCell[pP
149de 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a  age->childPtrSiz
149df 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 7a 65 3b  e];.  u32 nSize;
149e0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
149e1 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 76  DEBUG.  /* The v
149e2 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
149e3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   this function s
149e4 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20  hould always be 
149e5 74 68 65 20 73 61 6d 65 20 61 73 0a 20 20 2a 2a  the same as.  **
149e6 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f 2e 6e   the (CellInfo.n
149e7 53 69 7a 65 29 20 76 61 6c 75 65 20 66 6f 75 6e  Size) value foun
149e8 64 20 62 79 20 64 6f 69 6e 67 20 61 20 66 75 6c  d by doing a ful
149e9 6c 20 70 61 72 73 65 20 6f 66 20 74 68 65 0a 20  l parse of the. 
149ea 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 51 4c   ** cell. If SQL
149eb 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66  ITE_DEBUG is def
149ec 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28  ined, an assert(
149ed 29 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20  ) at the bottom 
149ee 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 75 6e  of.  ** this fun
149ef 63 74 69 6f 6e 20 76 65 72 69 66 69 65 73 20 74  ction verifies t
149f0 68 61 74 20 74 68 69 73 20 69 6e 76 61 72 69 61  hat this invaria
149f1 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c 61 74  nt is not violat
149f2 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66  ed. */.  CellInf
149f3 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 20 73  o debuginfo;.  s
149f4 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
149f5 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
149f6 43 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e 66 6f  Cell, &debuginfo
149f7 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28  );.#endif..  if(
149f8 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
149f9 7b 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 0a  {.    u8 *pEnd;.
149fa 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68      if( pPage->h
149fb 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20  asData ){.      
149fc 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69  pIter += getVari
149fd 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a  nt32(pIter, nSiz
149fe 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
149ff 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 30 3b 0a       nSize = 0;.
14a00 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 49      }..    /* pI
14a01 74 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73 20 61  ter now points a
14a02 74 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74  t the 64-bit int
14a03 65 67 65 72 20 6b 65 79 20 76 61 6c 75 65 2c 20  eger key value, 
14a04 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74  a variable lengt
14a05 68 20 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65  h .    ** intege
14a06 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  r. The following
14a07 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70 49 74   block moves pIt
14a08 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  er to point at t
14a09 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20  he first byte.  
14a0a 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e    ** past the en
14a0b 64 20 6f 66 20 74 68 65 20 6b 65 79 20 76 61 6c  d of the key val
14a0c 75 65 2e 20 2a 2f 0a 20 20 20 20 70 45 6e 64 20  ue. */.    pEnd 
14a0d 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 20 20  = &pIter[9];.   
14a0e 20 77 68 69 6c 65 28 20 28 2a 70 49 74 65 72 2b   while( (*pIter+
14a0f 2b 29 26 30 78 38 30 20 26 26 20 70 49 74 65 72  +)&0x80 && pIter
14a10 3c 70 45 6e 64 20 29 3b 0a 20 20 7d 65 6c 73 65  <pEnd );.  }else
14a11 7b 0a 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67  {.    pIter += g
14a12 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 72  etVarint32(pIter
14a13 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20  , nSize);.  }.. 
14a14 20 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65   if( nSize>pPage
14a15 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20  ->maxLocal ){.  
14a16 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 20 3d    int minLocal =
14a17 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c   pPage->minLocal
14a18 3b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69  ;.    nSize = mi
14a19 6e 4c 6f 63 61 6c 20 2b 20 28 6e 53 69 7a 65 20  nLocal + (nSize 
14a1a 2d 20 6d 69 6e 4c 6f 63 61 6c 29 20 25 20 28 70  - minLocal) % (p
14a1b 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
14a1c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20  eSize - 4);.    
14a1d 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d  if( nSize>pPage-
14a1e 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20  >maxLocal ){.   
14a1f 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f     nSize = minLo
14a20 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  cal;.    }.    n
14a21 53 69 7a 65 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20  Size += 4;.  }. 
14a22 20 6e 53 69 7a 65 20 2b 3d 20 28 70 49 74 65 72   nSize += (pIter
14a23 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a   - pCell);..  /*
14a24 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a   The minimum siz
14a25 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69 73  e of any cell is
14a26 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69   4 bytes. */.  i
14a27 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 20  f( nSize<4 ){.  
14a28 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 7d    nSize = 4;.  }
14a29 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 7a  ..  assert( nSiz
14a2a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69  e==debuginfo.nSi
14a2b 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  ze );.  return (
14a2c 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 23 69 66  u16)nSize;.}.#if
14a2d 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74  ndef NDEBUG.stat
14a2e 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 28  ic u16 cellSize(
14a2f 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
14a30 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 72 65  int iCell){.  re
14a31 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72  turn cellSizePtr
14a32 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65 6c 6c  (pPage, findCell
14a33 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 29 3b  (pPage, iCell));
14a34 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
14a35 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
14a36 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
14a37 49 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65 6c  If the cell pCel
14a38 6c 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65 20  l, part of page 
14a39 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 61  pPage contains a
14a3a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61   pointer.** to a
14a3b 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c  n overflow page,
14a3c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 79   insert an entry
14a3d 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65   into the pointe
14a3e 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 65  r-map.** for the
14a3f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a   overflow page..
14a40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74  */.static int pt
14a41 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 4d  rmapPutOvflPtr(M
14a42 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75  emPage *pPage, u
14a43 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 43 65 6c  8 *pCell){.  Cel
14a44 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 61 73  lInfo info;.  as
14a45 73 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 29  sert( pCell!=0 )
14a46 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
14a47 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61  ParseCellPtr(pPa
14a48 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f  ge, pCell, &info
14a49 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e  );.  assert( (in
14a4a 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d  fo.nData+(pPage-
14a4b 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e  >intKey?0:info.n
14a4c 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79  Key))==info.nPay
14a4d 6c 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 28 69  load );.  if( (i
14a4e 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65  nfo.nData+(pPage
14a4f 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e  ->intKey?0:info.
14a50 6e 4b 65 79 29 29 3e 69 6e 66 6f 2e 6e 4c 6f 63  nKey))>info.nLoc
14a51 61 6c 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f  al ){.    Pgno o
14a52 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26  vfl = get4byte(&
14a53 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72  pCell[info.iOver
14a54 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 72 65 74 75  flow]);.    retu
14a55 72 6e 20 70 74 72 6d 61 70 50 75 74 28 70 50 61  rn ptrmapPut(pPa
14a56 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50  ge->pBt, ovfl, P
14a57 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c  TRMAP_OVERFLOW1,
14a58 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20   pPage->pgno);. 
14a59 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
14a5a 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 49  TE_OK;.}./*.** I
14a5b 66 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20  f the cell with 
14a5c 69 6e 64 65 78 20 69 43 65 6c 6c 20 6f 6e 20 70  index iCell on p
14a5d 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74 61 69  age pPage contai
14a5e 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  ns a pointer.** 
14a5f 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  to an overflow p
14a60 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 65  age, insert an e
14a61 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 6f  ntry into the po
14a62 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72  inter-map.** for
14a63 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
14a64 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ge..*/.static in
14a65 74 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28  t ptrmapPutOvfl(
14a66 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
14a67 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38  int iCell){.  u8
14a68 20 2a 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72   *pCell;.  asser
14a69 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14a6a 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
14a6b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43  ->mutex) );.  pC
14a6c 65 6c 6c 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c  ell = findOverfl
14a6d 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  owCell(pPage, iC
14a6e 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ell);.  return p
14a6f 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28  trmapPutOvflPtr(
14a70 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 7d  pPage, pCell);.}
14a71 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
14a72 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70  Defragment the p
14a73 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20  age given.  All 
14a74 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20  Cells are moved 
14a75 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66  to the.** end of
14a76 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 61 6c   the page and al
14a77 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 73 20  l free space is 
14a78 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f  collected into o
14a79 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 6c  ne.** big FreeBl
14a7a 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e  k that occurs in
14a7b 20 62 65 74 77 65 65 6e 20 74 68 65 20 68 65 61   between the hea
14a7c 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20  der and cell.** 
14a7d 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61 6e  pointer array an
14a7e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  d the cell conte
14a7f 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74  nt area..*/.stat
14a80 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 6e  ic int defragmen
14a81 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  tPage(MemPage *p
14a82 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20  Page){.  int i; 
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 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
14a85 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b  ter */.  int pc;
14a86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14a87 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
14a88 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f  f a i-th cell */
14a89 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20  .  int addr;    
14a8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14a8b 20 4f 66 66 73 65 74 20 6f 66 20 66 69 72 73 74   Offset of first
14a8c 20 62 79 74 65 20 61 66 74 65 72 20 63 65 6c 6c   byte after cell
14a8d 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a   pointer array *
14a8e 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20  /.  int hdr;    
14a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14a90 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20  * Offset to the 
14a91 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20  page header */. 
14a92 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20   int size;      
14a93 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
14a94 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f  ize of a cell */
14a95 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a  .  int usableSiz
14a96 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
14a97 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c   Number of usabl
14a98 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67  e bytes on a pag
14a99 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f  e */.  int cellO
14a9a 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
14a9b 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74    /* Offset to t
14a9c 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
14a9d 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63  array */.  int c
14a9e 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  brk;            
14a9f 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
14aa0 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  to the cell cont
14aa1 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e  ent area */.  in
14aa2 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  t nCell;        
14aa3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
14aa4 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74  er of cells on t
14aa5 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73  he page */.  uns
14aa6 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61  igned char *data
14aa7 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ;       /* The p
14aa8 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e  age data */.  un
14aa9 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d  signed char *tem
14aaa 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70  p;       /* Temp
14aab 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63   area for cell c
14aac 6f 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73  ontent */..  ass
14aad 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
14aae 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
14aaf 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
14ab0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
14ab1 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  >pBt!=0 );.  ass
14ab2 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d  ert( pPage->pBt-
14ab3 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53  >usableSize <= S
14ab4 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
14ab5 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  IZE );.  assert(
14ab6 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
14ab7 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  w==0 );.  assert
14ab8 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
14ab9 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
14aba 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d  >mutex) );.  tem
14abb 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  p = sqlite3Pager
14abc 54 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d  TempSpace(pPage-
14abd 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  >pBt->pPager);. 
14abe 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
14abf 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50  Data;.  hdr = pP
14ac0 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
14ac1 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70    cellOffset = p
14ac2 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74  Page->cellOffset
14ac3 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67  ;.  nCell = pPag
14ac4 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65  e->nCell;.  asse
14ac5 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62  rt( nCell==get2b
14ac6 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
14ac7 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a  ) );.  usableSiz
14ac8 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e  e = pPage->pBt->
14ac9 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62  usableSize;.  cb
14aca 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  rk = get2byte(&d
14acb 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d  ata[hdr+5]);.  m
14acc 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b  emcpy(&temp[cbrk
14acd 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20  ], &data[cbrk], 
14ace 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72  usableSize - cbr
14acf 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61  k);.  cbrk = usa
14ad0 62 6c 65 53 69 7a 65 3b 0a 20 20 66 6f 72 28 69  bleSize;.  for(i
14ad1 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b  =0; i<nCell; i++
14ad2 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72  ){.    u8 *pAddr
14ad3 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74  ;     /* The i-t
14ad4 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a  h cell pointer *
14ad5 2f 0a 20 20 20 20 70 41 64 64 72 20 3d 20 26 64  /.    pAddr = &d
14ad6 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b  ata[cellOffset +
14ad7 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20   i*2];.    pc = 
14ad8 67 65 74 32 62 79 74 65 28 70 41 64 64 72 29 3b  get2byte(pAddr);
14ad9 0a 20 20 20 20 69 66 28 20 70 63 3e 3d 75 73 61  .    if( pc>=usa
14ada 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  bleSize ){.     
14adb 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
14adc 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
14add 20 7d 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 65   }.    size = ce
14ade 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
14adf 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20   &temp[pc]);.   
14ae0 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 20   cbrk -= size;. 
14ae1 20 20 20 69 66 28 20 63 62 72 6b 3c 63 65 6c 6c     if( cbrk<cell
14ae2 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 7c  Offset+2*nCell |
14ae3 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65  | pc+size>usable
14ae4 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65  Size ){.      re
14ae5 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
14ae6 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
14ae7 20 20 20 20 61 73 73 65 72 74 28 20 63 62 72 6b      assert( cbrk
14ae8 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a  +size<=usableSiz
14ae9 65 20 26 26 20 63 62 72 6b 3e 3d 30 20 29 3b 0a  e && cbrk>=0 );.
14aea 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61      memcpy(&data
14aeb 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63  [cbrk], &temp[pc
14aec 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70 75  ], size);.    pu
14aed 74 32 62 79 74 65 28 70 41 64 64 72 2c 20 63 62  t2byte(pAddr, cb
14aee 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  rk);.  }.  asser
14aef 74 28 20 63 62 72 6b 3e 3d 63 65 6c 6c 4f 66 66  t( cbrk>=cellOff
14af0 73 65 74 2b 32 2a 6e 43 65 6c 6c 20 29 3b 0a 20  set+2*nCell );. 
14af1 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
14af2 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20  hdr+5], cbrk);. 
14af3 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30   data[hdr+1] = 0
14af4 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20  ;.  data[hdr+2] 
14af5 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b  = 0;.  data[hdr+
14af6 37 5d 20 3d 20 30 3b 0a 20 20 61 64 64 72 20 3d  7] = 0;.  addr =
14af7 20 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43   cellOffset+2*nC
14af8 65 6c 6c 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64  ell;.  memset(&d
14af9 61 74 61 5b 61 64 64 72 5d 2c 20 30 2c 20 63 62  ata[addr], 0, cb
14afa 72 6b 2d 61 64 64 72 29 3b 0a 20 20 61 73 73 65  rk-addr);.  asse
14afb 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
14afc 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
14afd 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
14afe 20 69 66 28 20 63 62 72 6b 2d 61 64 64 72 21 3d   if( cbrk-addr!=
14aff 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a  pPage->nFree ){.
14b00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
14b01 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
14b02 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
14b03 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
14b04 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20   Allocate nByte 
14b05 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66  bytes of space f
14b06 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 42  rom within the B
14b07 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 73 65  -Tree page passe
14b08 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73  d.** as the firs
14b09 74 20 61 72 67 75 6d 65 6e 74 2e 20 52 65 74 75  t argument. Retu
14b0a 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74  rn the index int
14b0b 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d  o pPage->aData[]
14b0c 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 69 72 73   of the .** firs
14b0d 74 20 62 79 74 65 20 6f 66 20 61 6c 6c 6f 63 61  t byte of alloca
14b0e 74 65 64 20 73 70 61 63 65 2e 20 0a 2a 2a 0a 2a  ted space. .**.*
14b0f 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61  * The caller gua
14b10 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65  rantees that the
14b11 20 73 70 61 63 65 20 62 65 74 77 65 65 6e 20 74   space between t
14b12 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 63 65  he end of the ce
14b13 6c 6c 2d 6f 66 66 73 65 74 20 0a 2a 2a 20 61 72  ll-offset .** ar
14b14 72 61 79 20 61 6e 64 20 74 68 65 20 73 74 61 72  ray and the star
14b15 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f  t of the cell-co
14b16 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 61 74  ntent area is at
14b17 20 6c 65 61 73 74 20 6e 42 79 74 65 20 62 79 74   least nByte byt
14b18 65 73 0a 2a 2a 20 69 6e 20 73 69 7a 65 2e 20 53  es.** in size. S
14b19 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63  o this routine c
14b1a 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 0a 2a  an never fail..*
14b1b 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72  *.** If there ar
14b1c 65 20 61 6c 72 65 61 64 79 20 36 30 20 6f 72 20  e already 60 or 
14b1d 6d 6f 72 65 20 62 79 74 65 73 20 6f 66 20 66 72  more bytes of fr
14b1e 61 67 6d 65 6e 74 73 20 77 69 74 68 69 6e 20 74  agments within t
14b1f 68 65 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 20  he page,.** the 
14b20 70 61 67 65 20 69 73 20 64 65 66 72 61 67 6d 65  page is defragme
14b21 6e 74 65 64 20 62 65 66 6f 72 65 20 72 65 74 75  nted before retu
14b22 72 6e 69 6e 67 2e 20 49 66 20 74 68 69 73 20 77  rning. If this w
14b23 65 72 65 20 6e 6f 74 20 64 6f 6e 65 20 74 68 65  ere not done the
14b24 72 65 0a 2a 2a 20 69 73 20 61 20 63 68 61 6e 63  re.** is a chanc
14b25 65 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65  e that the numbe
14b26 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20  r of fragmented 
14b27 62 79 74 65 73 20 63 6f 75 6c 64 20 65 76 65 6e  bytes could even
14b28 74 75 61 6c 6c 79 20 0a 2a 2a 20 6f 76 65 72 66  tually .** overf
14b29 6c 6f 77 20 74 68 65 20 73 69 6e 67 6c 65 2d 62  low the single-b
14b2a 79 74 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65  yte field of the
14b2b 20 70 61 67 65 2d 68 65 61 64 65 72 20 69 6e 20   page-header in 
14b2c 77 68 69 63 68 20 74 68 69 73 20 76 61 6c 75 65  which this value
14b2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e 0a 2a  .** is stored..*
14b2e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c  /.static int all
14b2f 6f 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61  ocateSpace(MemPa
14b30 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e  ge *pPage, int n
14b31 42 79 74 65 29 7b 0a 20 20 63 6f 6e 73 74 20 69  Byte){.  const i
14b32 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  nt hdr = pPage->
14b33 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a  hdrOffset;    /*
14b34 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20   Local cache of 
14b35 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
14b36 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74   */.  u8 * const
14b37 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
14b38 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  Data;      /* Lo
14b39 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50 61  cal cache of pPa
14b3a 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 69  ge->aData */.  i
14b3b 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20 20 20  nt nFrag;       
14b3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b3d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14b3e 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65   fragmented byte
14b3f 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20  s on pPage */.  
14b40 69 6e 74 20 74 6f 70 3b 0a 20 20 0a 20 20 61 73  int top;.  .  as
14b41 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
14b42 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
14b43 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
14b44 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
14b45 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72  ->pBt );.  asser
14b46 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14b47 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
14b48 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
14b49 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29  sert( nByte>=0 )
14b4a 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65  ;  /* Minimum ce
14b4b 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a  ll size is 4 */.
14b4c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
14b4d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b  >nFree>=nByte );
14b4e 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
14b4f 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29  ->nOverflow==0 )
14b50 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74  ;..  /* Assert t
14b51 68 61 74 20 74 68 65 20 73 70 61 63 65 20 62 65  hat the space be
14b52 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 2d 6f  tween the cell-o
14b53 66 66 73 65 74 20 61 72 72 61 79 20 61 6e 64 20  ffset array and 
14b54 74 68 65 20 0a 20 20 2a 2a 20 63 65 6c 6c 2d 63  the .  ** cell-c
14b55 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 67  ontent area is g
14b56 72 65 61 74 65 72 20 74 68 61 6e 20 6e 42 79 74  reater than nByt
14b57 65 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20  e bytes..  */.  
14b58 61 73 73 65 72 74 28 20 6e 42 79 74 65 20 3c 3d  assert( nByte <=
14b59 20 28 0a 20 20 20 20 20 20 67 65 74 32 62 79 74   (.      get2byt
14b5a 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 2d  e(&data[hdr+5])-
14b5b 28 68 64 72 2b 38 2b 28 70 50 61 67 65 2d 3e 6c  (hdr+8+(pPage->l
14b5c 65 61 66 3f 30 3a 34 29 2b 32 2a 67 65 74 32 62  eaf?0:4)+2*get2b
14b5d 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
14b5e 29 29 0a 20 20 29 29 3b 0a 0a 20 20 70 50 61 67  )).  ));..  pPag
14b5f 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 75 31 36  e->nFree -= (u16
14b60 29 6e 42 79 74 65 3b 0a 20 20 6e 46 72 61 67 20  )nByte;.  nFrag 
14b61 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a 20  = data[hdr+7];. 
14b62 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20 29   if( nFrag>=60 )
14b63 7b 0a 20 20 20 20 64 65 66 72 61 67 6d 65 6e 74  {.    defragment
14b64 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d  Page(pPage);.  }
14b65 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 65 61  else{.    /* Sea
14b66 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 73 74  rch the freelist
14b67 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66   looking for a f
14b68 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f  ree slot big eno
14b69 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a  ugh to satisfy .
14b6a 20 20 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65      ** the reque
14b6b 73 74 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69  st. The allocati
14b6c 6f 6e 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20  on is made from 
14b6d 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 73  the first free s
14b6e 6c 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74  lot in .    ** t
14b6f 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 73 20  he list that is 
14b70 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20  large enough to 
14b71 61 63 63 6f 6d 61 64 61 74 65 20 69 74 2e 0a 20  accomadate it.. 
14b72 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63     */.    int pc
14b73 2c 20 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28  , addr;.    for(
14b74 61 64 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20  addr=hdr+1; (pc 
14b75 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
14b76 5b 61 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72  [addr]))>0; addr
14b77 3d 70 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =pc){.      int 
14b78 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28  size = get2byte(
14b79 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 20 20 20  &data[pc+2]);   
14b7a 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66 72 65    /* Size of fre
14b7b 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 20 20  e slot */.      
14b7c 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20  if( size>=nByte 
14b7d 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78  ){.        int x
14b7e 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b   = size - nByte;
14b7f 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 3c 34  .        if( x<4
14b80 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
14b81 20 52 65 6d 6f 76 65 20 74 68 65 20 73 6c 6f 74   Remove the slot
14b82 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c   from the free-l
14b83 69 73 74 2e 20 55 70 64 61 74 65 20 74 68 65 20  ist. Update the 
14b84 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20  number of.      
14b85 20 20 20 20 2a 2a 20 66 72 61 67 6d 65 6e 74 65      ** fragmente
14b86 64 20 62 79 74 65 73 20 77 69 74 68 69 6e 20 74  d bytes within t
14b87 68 65 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20  he page. */.    
14b88 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61        memcpy(&da
14b89 74 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 61 5b  ta[addr], &data[
14b8a 70 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20  pc], 2);.       
14b8b 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d     data[hdr+7] =
14b8c 20 28 75 38 29 28 6e 46 72 61 67 20 2b 20 78 29   (u8)(nFrag + x)
14b8d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
14b8e 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
14b8f 65 20 73 6c 6f 74 20 72 65 6d 61 69 6e 73 20 6f  e slot remains o
14b90 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e  n the free-list.
14b91 20 52 65 64 75 63 65 20 69 74 73 20 73 69 7a 65   Reduce its size
14b92 20 74 6f 20 61 63 63 6f 75 6e 74 0a 20 20 20 20   to account.    
14b93 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65        ** for the
14b94 20 70 6f 72 74 69 6f 6e 20 75 73 65 64 20 62 79   portion used by
14b95 20 74 68 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74   the new allocat
14b96 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ion. */.        
14b97 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
14b98 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20  [pc+2], x);.    
14b99 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65      }.        re
14b9a 74 75 72 6e 20 70 63 20 2b 20 78 3b 0a 20 20 20  turn pc + x;.   
14b9b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
14b9c 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65    /* Allocate me
14b9d 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61  mory from the ga
14b9e 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65  p in between the
14b9f 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
14ba0 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65  ray.  ** and the
14ba1 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
14ba2 65 61 2e 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 3d  ea..  */.  top =
14ba3 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
14ba4 68 64 72 2b 35 5d 29 20 2d 20 6e 42 79 74 65 3b  hdr+5]) - nByte;
14ba5 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
14ba6 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a  a[hdr+5], top);.
14ba7 20 20 72 65 74 75 72 6e 20 74 6f 70 3b 0a 7d 0a    return top;.}.
14ba8 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
14ba9 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 70  section of the p
14baa 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f 20 74  Page->aData to t
14bab 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 20  he freelist..** 
14bac 54 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f  The first byte o
14bad 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 20 62  f the new free b
14bae 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d 3e 61  lock is pPage->a
14baf 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a 20 61  Disk[start].** a
14bb0 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  nd the size of t
14bb1 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 69 7a  he block is "siz
14bb2 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  e" bytes..**.** 
14bb3 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 66 6f  Most of the effo
14bb4 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 6f 6c  rt here is invol
14bb5 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 6e 67  ved in coalesing
14bb6 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 72 65   adjacent.** fre
14bb7 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 61 20  e blocks into a 
14bb8 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 65 20  single big free 
14bb9 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63  block..*/.static
14bba 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 28 4d   int freeSpace(M
14bbb 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
14bbc 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 73 69  nt start, int si
14bbd 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 2c  ze){.  int addr,
14bbe 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a 20 20   pbegin, hdr;.  
14bbf 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64  unsigned char *d
14bc0 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
14bc1 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  ta;..  assert( p
14bc2 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a  Page->pBt!=0 );.
14bc3 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14bc4 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
14bc5 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
14bc6 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
14bc7 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72  tart>=pPage->hdr
14bc8 4f 66 66 73 65 74 2b 36 2b 28 70 50 61 67 65 2d  Offset+6+(pPage-
14bc9 3e 6c 65 61 66 3f 30 3a 34 29 20 29 3b 0a 20 20  >leaf?0:4) );.  
14bca 61 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b  assert( (start +
14bcb 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70   size)<=pPage->p
14bcc 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  Bt->usableSize )
14bcd 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
14bce 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14bcf 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
14bd0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
14bd1 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d  ize>=0 );   /* M
14bd2 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  inimum cell size
14bd3 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66   is 4 */..#ifdef
14bd4 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
14bd5 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77  ELETE.  /* Overw
14bd6 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66  rite deleted inf
14bd7 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65  ormation with ze
14bd8 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53 45 43  ros when the SEC
14bd9 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a  URE_DELETE .  **
14bda 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c   option is enabl
14bdb 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ed at compile-ti
14bdc 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  me */.  memset(&
14bdd 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20  data[start], 0, 
14bde 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  size);.#endif.. 
14bdf 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61 63   /* Add the spac
14be0 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  e back into the 
14be1 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66  linked list of f
14be2 72 65 65 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 68  reeblocks */.  h
14be3 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
14be4 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20  ffset;.  addr = 
14be5 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65  hdr + 1;.  while
14be6 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32  ( (pbegin = get2
14be7 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d  byte(&data[addr]
14be8 29 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 67  ))<start && pbeg
14be9 69 6e 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65  in>0 ){.    asse
14bea 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 61 67  rt( pbegin<=pPag
14beb 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
14bec 7a 65 2d 34 20 29 3b 0a 20 20 20 20 69 66 28 20  ze-4 );.    if( 
14bed 70 62 65 67 69 6e 3c 3d 61 64 64 72 20 29 20 7b  pbegin<=addr ) {
14bee 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
14bef 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
14bf0 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64  T;.    }.    add
14bf1 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a  r = pbegin;.  }.
14bf2 20 20 69 66 20 28 20 70 62 65 67 69 6e 3e 70 50    if ( pbegin>pP
14bf3 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
14bf4 53 69 7a 65 2d 34 20 29 20 7b 0a 20 20 20 20 72  Size-4 ) {.    r
14bf5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
14bf6 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
14bf7 20 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3e   assert( pbegin>
14bf8 61 64 64 72 20 7c 7c 20 70 62 65 67 69 6e 3d 3d  addr || pbegin==
14bf9 30 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28  0 );.  put2byte(
14bfa 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 73 74 61  &data[addr], sta
14bfb 72 74 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28  rt);.  put2byte(
14bfc 26 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 70 62  &data[start], pb
14bfd 65 67 69 6e 29 3b 0a 20 20 70 75 74 32 62 79 74  egin);.  put2byt
14bfe 65 28 26 64 61 74 61 5b 73 74 61 72 74 2b 32 5d  e(&data[start+2]
14bff 2c 20 73 69 7a 65 29 3b 0a 20 20 70 50 61 67 65  , size);.  pPage
14c00 2d 3e 6e 46 72 65 65 20 2b 3d 20 28 75 31 36 29  ->nFree += (u16)
14c01 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 6f 61 6c  size;..  /* Coal
14c02 65 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72  esce adjacent fr
14c03 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a 20 20 61  ee blocks */.  a
14c04 64 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  ddr = pPage->hdr
14c05 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20 20 77 68  Offset + 1;.  wh
14c06 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67  ile( (pbegin = g
14c07 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64  et2byte(&data[ad
14c08 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20 20 69  dr]))>0 ){.    i
14c09 6e 74 20 70 6e 65 78 74 2c 20 70 73 69 7a 65 2c  nt pnext, psize,
14c0a 20 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   x;.    assert( 
14c0b 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20  pbegin>addr );. 
14c0c 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69     assert( pbegi
14c0d 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  n<=pPage->pBt->u
14c0e 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20  sableSize-4 );. 
14c0f 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62     pnext = get2b
14c10 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
14c11 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 3d 20  ]);.    psize = 
14c12 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
14c13 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69  begin+2]);.    i
14c14 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73 69 7a  f( pbegin + psiz
14c15 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 20 26  e + 3 >= pnext &
14c16 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20  & pnext>0 ){.   
14c17 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20 70 6e     int frag = pn
14c18 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b 70 73  ext - (pbegin+ps
14c19 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ize);.      if( 
14c1a 28 66 72 61 67 3c 30 29 20 7c 7c 20 28 66 72 61  (frag<0) || (fra
14c1b 67 3e 28 69 6e 74 29 64 61 74 61 5b 70 50 61 67  g>(int)data[pPag
14c1c 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d 29  e->hdrOffset+7])
14c1d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
14c1e 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
14c1f 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a  T_BKPT;.      }.
14c20 20 20 20 20 20 20 64 61 74 61 5b 70 50 61 67 65        data[pPage
14c21 2d 3e 68 64 72 4f 66 66 73 65 74 2b 37 5d 20 2d  ->hdrOffset+7] -
14c22 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20 20  = (u8)frag;.    
14c23 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28 26    x = get2byte(&
14c24 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20 20  data[pnext]);.  
14c25 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
14c26 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b 0a  ta[pbegin], x);.
14c27 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74 20        x = pnext 
14c28 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  + get2byte(&data
14c29 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62 65  [pnext+2]) - pbe
14c2a 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32 62  gin;.      put2b
14c2b 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
14c2c 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65 6c  +2], x);.    }el
14c2d 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  se{.      addr =
14c2e 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a 20   pbegin;.    }. 
14c2f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
14c30 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
14c31 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 20  a begins with a 
14c32 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76  freeblock, remov
14c33 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 64  e it. */.  if( d
14c34 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74 61  ata[hdr+1]==data
14c35 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61 5b  [hdr+5] && data[
14c36 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64 72  hdr+2]==data[hdr
14c37 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 74  +6] ){.    int t
14c38 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20 3d  op;.    pbegin =
14c39 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
14c3a 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d  hdr+1]);.    mem
14c3b 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  cpy(&data[hdr+1]
14c3c 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c  , &data[pbegin],
14c3d 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67   2);.    top = g
14c3e 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
14c3f 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74 65  r+5]) + get2byte
14c40 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d  (&data[pbegin+2]
14c41 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28  );.    put2byte(
14c42 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f  &data[hdr+5], to
14c43 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  p);.  }.  assert
14c44 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
14c45 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
14c46 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 72  >pDbPage) );.  r
14c47 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
14c48 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65  .}../*.** Decode
14c49 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20   the flags byte 
14c4a 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20  (the first byte 
14c4b 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 66  of the header) f
14c4c 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e 64  or a page.** and
14c4d 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 6c   initialize fiel
14c4e 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67  ds of the MemPag
14c4f 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 6f  e structure acco
14c50 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f  rdingly..**.** O
14c51 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  nly the followin
14c52 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 61  g combinations a
14c53 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 41  re supported.  A
14c54 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65 6e  nything differen
14c55 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 61  t.** indicates a
14c56 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73   corrupt databas
14c57 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  e files:.**.**  
14c58 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f 44         PTF_ZEROD
14c59 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 50  ATA.**         P
14c5a 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50 54  TF_ZERODATA | PT
14c5b 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20 20  F_LEAF.**       
14c5c 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c    PTF_LEAFDATA |
14c5d 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20   PTF_INTKEY.**  
14c5e 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44         PTF_LEAFD
14c5f 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59  ATA | PTF_INTKEY
14c60 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73   | PTF_LEAF.*/.s
14c61 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 65  tatic int decode
14c62 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a 70  Flags(MemPage *p
14c63 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 79  Page, int flagBy
14c64 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  te){.  BtShared 
14c65 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 63  *pBt;     /* A c
14c66 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 42  opy of pPage->pB
14c67 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
14c68 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
14c69 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d  ==(pPage->pgno==
14c6a 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b 0a  1 ? 100 : 0) );.
14c6b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14c6c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
14c6d 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
14c6e 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61 66  );.  pPage->leaf
14c6f 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74 65   = (u8)(flagByte
14c70 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20 50  >>3);  assert( P
14c71 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 20  TF_LEAF == 1<<3 
14c72 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26 3d  );.  flagByte &=
14c73 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 50   ~PTF_LEAF;.  pP
14c74 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a  age->childPtrSiz
14c75 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e 6c  e = 4-4*pPage->l
14c76 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 61  eaf;.  pBt = pPa
14c77 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66  ge->pBt;.  if( f
14c78 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c 45  lagByte==(PTF_LE
14c79 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54  AFDATA | PTF_INT
14c7a 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61 67  KEY) ){.    pPag
14c7b 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a 20  e->intKey = 1;. 
14c7c 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74     pPage->hasDat
14c7d 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 3b  a = pPage->leaf;
14c7e 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c  .    pPage->maxL
14c7f 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c  ocal = pBt->maxL
14c80 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  eaf;.    pPage->
14c81 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e  minLocal = pBt->
14c82 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65  minLeaf;.  }else
14c83 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 50   if( flagByte==P
14c84 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a 20  TF_ZERODATA ){. 
14c85 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79     pPage->intKey
14c86 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d   = 0;.    pPage-
14c87 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 20  >hasData = 0;.  
14c88 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61    pPage->maxLoca
14c89 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61  l = pBt->maxLoca
14c8a 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69  l;.    pPage->mi
14c8b 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69  nLocal = pBt->mi
14c8c 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b  nLocal;.  }else{
14c8d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
14c8e 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
14c8f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
14c90 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
14c91 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
14c92 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72   auxiliary infor
14c93 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 73  mation for a dis
14c94 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52  k block..**.** R
14c95 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
14c96 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20  on success.  If 
14c97 77 65 20 73 65 65 20 74 68 61 74 20 74 68 65 20  we see that the 
14c98 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  page does.** not
14c99 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c 2d   contain a well-
14c9a 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20  formed database 
14c9b 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72  page, then retur
14c9c 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52  n .** SQLITE_COR
14c9d 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 74  RUPT.  Note that
14c9e 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51 4c   a return of SQL
14c9f 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a  ITE_OK does not.
14ca0 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  ** guarantee tha
14ca1 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77 65  t the page is we
14ca2 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 6f  ll-formed.  It o
14ca3 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0a 2a  nly shows that.*
14ca4 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 64  * we failed to d
14ca5 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75 70  etect any corrup
14ca6 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
14ca7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14ca8 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65  te3BtreeInitPage
14ca9 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29  (MemPage *pPage)
14caa 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  {..  assert( pPa
14cab 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20  ge->pBt!=0 );.  
14cac 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14cad 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
14cae 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
14caf 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
14cb0 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50  ->pgno==sqlite3P
14cb1 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70  agerPagenumber(p
14cb2 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
14cb3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
14cb4 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65  e == sqlite3Page
14cb5 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d  rGetExtra(pPage-
14cb6 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61  >pDbPage) );.  a
14cb7 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44  ssert( pPage->aD
14cb8 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 50 61  ata == sqlite3Pa
14cb9 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65  gerGetData(pPage
14cba 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20  ->pDbPage) );.. 
14cbb 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 73 49   if( !pPage->isI
14cbc 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36 20 70  nit ){.    u16 p
14cbd 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c;            /*
14cbe 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 66 72   Address of a fr
14cbf 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70  eeblock within p
14cc0 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f  Page->aData[] */
14cc1 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20 20 20  .    u8 hdr;    
14cc2 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
14cc3 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f  t to beginning o
14cc4 66 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f  f page header */
14cc5 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b 20 20  .    u8 *data;  
14cc6 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 61 6c          /* Equal
14cc7 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61   to pPage->aData
14cc8 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72 65 64   */.    BtShared
14cc9 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 2f 2a   *pBt;        /*
14cca 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 65 20   The main btree 
14ccb 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20  structure */.   
14ccc 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 3b   u16 usableSize;
14ccd 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66      /* Amount of
14cce 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 6f 6e   usable space on
14ccf 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20   each page */.  
14cd0 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74    u16 cellOffset
14cd1 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66  ;    /* Offset f
14cd2 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 61 67  rom start of pag
14cd3 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c 6c 20  e to first cell 
14cd4 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75  pointer */.    u
14cd5 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20  16 nFree;       
14cd6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75    /* Number of u
14cd7 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e 20 74  nused bytes on t
14cd8 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75  he page */.    u
14cd9 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20  16 top;         
14cda 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
14cdb 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
14cdc 65 6e 74 20 61 72 65 61 20 2a 2f 0a 0a 20 20 20  ent area */..   
14cdd 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42   pBt = pPage->pB
14cde 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 50  t;..    hdr = pP
14cdf 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
14ce0 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67 65      data = pPage
14ce1 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28  ->aData;.    if(
14ce2 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61   decodeFlags(pPa
14ce3 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 29  ge, data[hdr]) )
14ce4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
14ce5 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
14ce6 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61   assert( pBt->pa
14ce7 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70  geSize>=512 && p
14ce8 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32  Bt->pageSize<=32
14ce9 37 36 38 20 29 3b 0a 20 20 20 20 70 50 61 67 65  768 );.    pPage
14cea 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70 42 74  ->maskPage = pBt
14ceb 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a  ->pageSize - 1;.
14cec 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72      pPage->nOver
14ced 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73  flow = 0;.    us
14cee 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e  ableSize = pBt->
14cef 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20  usableSize;.    
14cf0 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
14cf1 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d  t = cellOffset =
14cf2 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50   hdr + 12 - 4*pP
14cf3 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 74  age->leaf;.    t
14cf4 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  op = get2byte(&d
14cf5 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20  ata[hdr+5]);.   
14cf6 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20   pPage->nCell = 
14cf7 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
14cf8 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20  dr+3]);.    if( 
14cf9 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f  pPage->nCell>MX_
14cfa 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20  CELL(pBt) ){.   
14cfb 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65     /* To many ce
14cfc 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  lls for a single
14cfd 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65   page.  The page
14cfe 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74   must be corrupt
14cff 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
14d00 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
14d01 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  BKPT;.    }.  . 
14d02 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68     /* Compute th
14d03 65 20 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61  e total free spa
14d04 63 65 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a  ce on the page *
14d05 2f 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 62  /.    pc = get2b
14d06 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  yte(&data[hdr+1]
14d07 29 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 64  );.    nFree = d
14d08 61 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70  ata[hdr+7] + top
14d09 20 2d 20 28 63 65 6c 6c 4f 66 66 73 65 74 20 2b   - (cellOffset +
14d0a 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29   2*pPage->nCell)
14d0b 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 63 3e  ;.    while( pc>
14d0c 30 20 29 7b 0a 20 20 20 20 20 20 75 31 36 20 6e  0 ){.      u16 n
14d0d 65 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20  ext, size;.     
14d0e 20 69 66 28 20 70 63 3e 75 73 61 62 6c 65 53 69   if( pc>usableSi
14d0f 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20  ze-4 ){.        
14d10 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 20 69 73  /* Free block is
14d11 20 6f 66 66 20 74 68 65 20 70 61 67 65 20 2a 2f   off the page */
14d12 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
14d13 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
14d14 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20  KPT; .      }.  
14d15 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 32 62      next = get2b
14d16 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a  yte(&data[pc]);.
14d17 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74        size = get
14d18 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32  2byte(&data[pc+2
14d19 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65  ]);.      if( ne
14d1a 78 74 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63  xt>0 && next<=pc
14d1b 2b 73 69 7a 65 2b 33 20 29 7b 0a 20 20 20 20 20  +size+3 ){.     
14d1c 20 20 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b     /* Free block
14d1d 73 20 6d 75 73 74 20 62 65 20 69 6e 20 61 63 63  s must be in acc
14d1e 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a  ending order */.
14d1f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
14d20 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
14d21 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20  PT; .      }.   
14d22 20 20 20 6e 46 72 65 65 20 2b 3d 20 73 69 7a 65     nFree += size
14d23 3b 0a 20 20 20 20 20 20 70 63 20 3d 20 6e 65 78  ;.      pc = nex
14d24 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  t;.    }.    pPa
14d25 67 65 2d 3e 6e 46 72 65 65 20 3d 20 28 75 31 36  ge->nFree = (u16
14d26 29 6e 46 72 65 65 3b 0a 20 20 20 20 69 66 28 20  )nFree;.    if( 
14d27 6e 46 72 65 65 3e 3d 75 73 61 62 6c 65 53 69 7a  nFree>=usableSiz
14d28 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 72  e ){.      /* Fr
14d29 65 65 20 73 70 61 63 65 20 63 61 6e 6e 6f 74 20  ee space cannot 
14d2a 65 78 63 65 65 64 20 74 6f 74 61 6c 20 70 61 67  exceed total pag
14d2b 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20  e size */.      
14d2c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
14d2d 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20  RRUPT_BKPT; .   
14d2e 20 7d 0a 0a 23 69 66 20 30 0a 20 20 2f 2a 20 43   }..#if 0.  /* C
14d2f 68 65 63 6b 20 74 68 61 74 20 61 6c 6c 20 74 68  heck that all th
14d30 65 20 6f 66 66 73 65 74 73 20 69 6e 20 74 68 65  e offsets in the
14d31 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61 72 72   cell offset arr
14d32 61 79 20 61 72 65 20 77 69 74 68 69 6e 20 72 61  ay are within ra
14d33 6e 67 65 2e 20 0a 20 20 2a 2a 20 0a 20 20 2a 2a  nge. .  ** .  **
14d34 20 4f 6d 69 74 74 69 6e 67 20 74 68 69 73 20 63   Omitting this c
14d35 6f 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b  onsistency check
14d36 20 61 6e 64 20 75 73 69 6e 67 20 74 68 65 20 70   and using the p
14d37 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 6d  Page->maskPage m
14d38 61 73 6b 0a 20 20 2a 2a 20 74 6f 20 70 72 65 76  ask.  ** to prev
14d39 65 6e 74 20 6f 76 65 72 72 75 6e 6e 69 6e 67 20  ent overrunning 
14d3a 74 68 65 20 70 61 67 65 20 62 75 66 66 65 72 20  the page buffer 
14d3b 69 6e 20 66 69 6e 64 43 65 6c 6c 28 29 20 72 65  in findCell() re
14d3c 73 75 6c 74 73 20 69 6e 20 61 0a 20 20 2a 2a 20  sults in a.  ** 
14d3d 32 2e 35 25 20 70 65 72 66 6f 72 6d 61 6e 63 65  2.5% performance
14d3e 20 67 61 69 6e 2e 0a 20 20 2a 2f 0a 20 20 7b 0a   gain..  */.  {.
14d3f 20 20 20 20 75 38 20 2a 70 4f 66 66 3b 20 20 20      u8 *pOff;   
14d40 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
14d41 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 61   used to check a
14d42 6c 6c 20 63 65 6c 6c 20 6f 66 66 73 65 74 73 20  ll cell offsets 
14d43 61 72 65 20 69 6e 20 72 61 6e 67 65 20 2a 2f 0a  are in range */.
14d44 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 20 20 20      u8 *pEnd;   
14d45 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
14d46 74 6f 20 65 6e 64 20 6f 66 20 63 65 6c 6c 20 6f  to end of cell o
14d47 66 66 73 65 74 20 61 72 72 61 79 20 2a 2f 0a 20  ffset array */. 
14d48 20 20 20 75 38 20 6d 61 73 6b 3b 20 20 20 20 20     u8 mask;     
14d49 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 62      /* Mask of b
14d4a 69 74 73 20 74 68 61 74 20 6d 75 73 74 20 62 65  its that must be
14d4b 20 7a 65 72 6f 20 69 6e 20 4d 53 42 20 6f 66 20   zero in MSB of 
14d4c 63 65 6c 6c 20 6f 66 66 73 65 74 73 20 2a 2f 0a  cell offsets */.
14d4d 20 20 20 20 6d 61 73 6b 20 3d 20 7e 28 28 28 75      mask = ~(((u
14d4e 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  8)(pBt->pageSize
14d4f 3e 3e 38 29 29 2d 31 29 3b 0a 20 20 20 20 70 45  >>8))-1);.    pE
14d50 6e 64 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f  nd = &data[cellO
14d51 66 66 73 65 74 20 2b 20 70 50 61 67 65 2d 3e 6e  ffset + pPage->n
14d52 43 65 6c 6c 2a 32 5d 3b 0a 20 20 20 20 66 6f 72  Cell*2];.    for
14d53 28 70 4f 66 66 3d 26 64 61 74 61 5b 63 65 6c 6c  (pOff=&data[cell
14d54 4f 66 66 73 65 74 5d 3b 20 70 4f 66 66 21 3d 70  Offset]; pOff!=p
14d55 45 6e 64 20 26 26 20 21 28 28 2a 70 4f 66 66 29  End && !((*pOff)
14d56 26 6d 61 73 6b 29 3b 20 70 4f 66 66 2b 3d 32 29  &mask); pOff+=2)
14d57 3b 0a 20 20 20 20 69 66 28 20 70 4f 66 66 21 3d  ;.    if( pOff!=
14d58 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 72 65  pEnd ){.      re
14d59 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
14d5a 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
14d5b 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
14d5c 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
14d5d 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
14d5e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
14d5f 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77  .** Set up a raw
14d60 20 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74   page so that it
14d61 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61   looks like a da
14d62 74 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64  tabase page hold
14d63 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65  ing.** no entrie
14d64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
14d65 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61  d zeroPage(MemPa
14d66 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66  ge *pPage, int f
14d67 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65  lags){.  unsigne
14d68 64 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70  d char *data = p
14d69 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42  Page->aData;.  B
14d6a 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
14d6b 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20  Page->pBt;.  u8 
14d6c 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
14d6d 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 20 66 69  Offset;.  u16 fi
14d6e 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rst;..  assert( 
14d6f 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
14d70 6e 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44  number(pPage->pD
14d71 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70  bPage)==pPage->p
14d72 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28  gno );.  assert(
14d73 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
14d74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62  Extra(pPage->pDb
14d75 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29  Page) == (void*)
14d76 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72  pPage );.  asser
14d77 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  t( sqlite3PagerG
14d78 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44  etData(pPage->pD
14d79 62 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29  bPage) == data )
14d7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
14d7b 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
14d7c 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
14d7d 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
14d7e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14d7f 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
14d80 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 64  );.  /*memset(&d
14d81 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74  ata[hdr], 0, pBt
14d82 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68  ->usableSize - h
14d83 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 64  dr);*/.  data[hd
14d84 72 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 67 73  r] = (char)flags
14d85 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 64 72 20  ;.  first = hdr 
14d86 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 67 73 26  + 8 + 4*((flags&
14d87 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f 31 3a  PTF_LEAF)==0 ?1:
14d88 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61  0);.  memset(&da
14d89 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29  ta[hdr+1], 0, 4)
14d8a 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20  ;.  data[hdr+7] 
14d8b 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28  = 0;.  put2byte(
14d8c 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42  &data[hdr+5], pB
14d8d 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a  t->usableSize);.
14d8e 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d    pPage->nFree =
14d8f 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
14d90 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 63 6f   - first;.  deco
14d91 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66  deFlags(pPage, f
14d92 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e  lags);.  pPage->
14d93 68 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b  hdrOffset = hdr;
14d94 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66  .  pPage->cellOf
14d95 66 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20  fset = first;.  
14d96 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
14d97 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
14d98 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35  pBt->pageSize>=5
14d99 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53  12 && pBt->pageS
14d9a 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20  ize<=32768 );.  
14d9b 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20  pPage->maskPage 
14d9c 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  = pBt->pageSize 
14d9d 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43  - 1;.  pPage->nC
14d9e 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  ell = 0;.  pPage
14d9f 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a  ->isInit = 1;.}.
14da0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
14da1 61 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e 65  a DbPage obtaine
14da2 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72  d from the pager
14da3 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65 20   into a MemPage 
14da4 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 62  used by.** the b
14da5 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73  tree layer..*/.s
14da6 74 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62  tatic MemPage *b
14da7 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61  treePageFromDbPa
14da8 67 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61  ge(DbPage *pDbPa
14da9 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42  ge, Pgno pgno, B
14daa 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
14dab 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
14dac 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69  = (MemPage*)sqli
14dad 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61  te3PagerGetExtra
14dae 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61  (pDbPage);.  pPa
14daf 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69  ge->aData = sqli
14db0 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
14db1 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67  pDbPage);.  pPag
14db2 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 44 62  e->pDbPage = pDb
14db3 50 61 67 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70  Page;.  pPage->p
14db4 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 61 67  Bt = pBt;.  pPag
14db5 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a  e->pgno = pgno;.
14db6 20 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73    pPage->hdrOffs
14db7 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f  et = pPage->pgno
14db8 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20  ==1 ? 100 : 0;. 
14db9 20 72 65 74 75 72 6e 20 70 50 61 67 65 3b 20 0a   return pPage; .
14dba 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70  }../*.** Get a p
14dbb 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  age from the pag
14dbc 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20  er.  Initialize 
14dbd 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20  the MemPage.pBt 
14dbe 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61  and.** MemPage.a
14dbf 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66  Data elements if
14dc0 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49   needed..**.** I
14dc1 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20  f the noContent 
14dc2 66 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 20  flag is set, it 
14dc3 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f  means that we do
14dc4 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 0a   not care about.
14dc5 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ** the content o
14dc6 66 20 74 68 65 20 70 61 67 65 20 61 74 20 74 68  f the page at th
14dc7 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20  is time.  So do 
14dc8 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 69  not go to the di
14dc9 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 74  sk.** to fetch t
14dca 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73  he content.  Jus
14dcb 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f  t fill in the co
14dcc 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f 73  ntent with zeros
14dcd 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20   for now..** If 
14dce 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 65  in the future we
14dcf 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67   call sqlite3Pag
14dd0 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 69  erWrite() on thi
14dd1 73 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20  s page, that.** 
14dd2 6d 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 74  means we have st
14dd3 61 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e 63  arted to be conc
14dd4 65 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e 74  erned about cont
14dd5 65 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 6b  ent and the disk
14dd6 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 20  .** read should 
14dd7 6f 63 63 75 72 20 61 74 20 74 68 61 74 20 70 6f  occur at that po
14dd8 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  int..*/.SQLITE_P
14dd9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
14dda 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 0a  e3BtreeGetPage(.
14ddb 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c    BtShared *pBt,
14ddc 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74         /* The bt
14ddd 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ree */.  Pgno pg
14dde 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  no,           /*
14ddf 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70   Number of the p
14de0 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a  age to fetch */.
14de1 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61    MemPage **ppPa
14de2 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  ge,    /* Return
14de3 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69   the page in thi
14de4 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  s parameter */. 
14de5 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20   int noContent  
14de6 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
14de7 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e  load page conten
14de8 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  t if true */.){.
14de9 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61    int rc;.  DbPa
14dea 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20  ge *pDbPage;..  
14deb 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14dec 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
14ded 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
14dee 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
14def 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72  uire(pBt->pPager
14df0 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a  , pgno, (DbPage*
14df1 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f  *)&pDbPage, noCo
14df2 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63  ntent);.  if( rc
14df3 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
14df4 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50  *ppPage = btreeP
14df5 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44  ageFromDbPage(pD
14df6 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74  bPage, pgno, pBt
14df7 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
14df8 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
14df9 52 65 74 72 69 65 76 65 20 61 20 70 61 67 65 20  Retrieve a page 
14dfa 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63  from the pager c
14dfb 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 65 71  ache. If the req
14dfc 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e  uested page is n
14dfd 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e  ot.** already in
14dfe 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
14dff 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e   return NULL. In
14e00 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d  itialize the Mem
14e01 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20  Page.pBt and.** 
14e02 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c  MemPage.aData el
14e03 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64  ements if needed
14e04 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50  ..*/.static MemP
14e05 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f  age *btreePageLo
14e06 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 2a 70  okup(BtShared *p
14e07 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  Bt, Pgno pgno){.
14e08 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
14e09 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  e;.  assert( sql
14e0a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14e0b 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
14e0c 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74   pDbPage = sqlit
14e0d 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42  e3PagerLookup(pB
14e0e 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  t->pPager, pgno)
14e0f 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20  ;.  if( pDbPage 
14e10 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74  ){.    return bt
14e11 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
14e12 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c  e(pDbPage, pgno,
14e13 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74   pBt);.  }.  ret
14e14 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
14e15 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
14e16 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
14e17 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49  file in pages. I
14e18 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b  f there is any k
14e19 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c  ind of.** error,
14e1a 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e   return ((unsign
14e1b 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73  ed int)-1)..*/.s
14e1c 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72  tatic Pgno pager
14e1d 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72  Pagecount(BtShar
14e1e 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20  ed *pBt){.  int 
14e1f 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e  nPage = -1;.  in
14e20 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
14e21 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20  pBt->pPage1 );. 
14e22 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
14e23 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
14e24 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  >pPager, &nPage)
14e25 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
14e26 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61  SQLITE_OK || nPa
14e27 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75  ge==-1 );.  retu
14e28 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a  rn (Pgno)nPage;.
14e29 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70  }../*.** Get a p
14e2a 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  age from the pag
14e2b 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  er and initializ
14e2c 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74  e it.  This rout
14e2d 69 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74 20 61  ine.** is just a
14e2e 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61   convenience wra
14e2f 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61  pper around sepa
14e30 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a  rate calls to.**
14e31 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
14e32 50 61 67 65 28 29 20 61 6e 64 20 73 71 6c 69 74  Page() and sqlit
14e33 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28  e3BtreeInitPage(
14e34 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
14e35 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
14e36 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
14e37 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
14e38 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
14e39 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  */.  Pgno pgno, 
14e3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
14e3b 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20  ber of the page 
14e3c 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50  to get */.  MemP
14e3d 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20  age **ppPage    
14e3e 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61   /* Write the pa
14e3f 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20  ge pointer here 
14e40 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
14e41 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
14e42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
14e43 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14e44 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
14e45 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a   if( pgno==0 ){.
14e46 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
14e47 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20  E_CORRUPT_BKPT; 
14e48 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 69 73  .  }..  /* It is
14e49 20 6f 66 74 65 6e 20 74 68 65 20 63 61 73 65 20   often the case 
14e4a 74 68 61 74 20 74 68 65 20 70 61 67 65 20 77 65  that the page we
14e4b 20 77 61 6e 74 20 69 73 20 61 6c 72 65 61 64 79   want is already
14e4c 20 69 6e 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20   in cache..  ** 
14e4d 49 66 20 73 6f 2c 20 67 65 74 20 69 74 20 64 69  If so, get it di
14e4e 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 73 61  rectly.  This sa
14e4f 76 65 73 20 75 73 20 66 72 6f 6d 20 68 61 76 69  ves us from havi
14e50 6e 67 20 74 6f 20 63 61 6c 6c 0a 20 20 2a 2a 20  ng to call.  ** 
14e51 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29  pagerPagecount()
14e52 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 70 67   to make sure pg
14e53 6e 6f 20 69 73 20 77 69 74 68 69 6e 20 6c 69 6d  no is within lim
14e54 69 74 73 2c 20 77 68 69 63 68 20 72 65 73 75 6c  its, which resul
14e55 74 73 0a 20 20 2a 2a 20 69 6e 20 61 20 6d 65 61  ts.  ** in a mea
14e56 73 75 72 65 61 62 6c 65 20 70 65 72 66 6f 72 6d  sureable perform
14e57 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74  ance improvement
14e58 73 2e 0a 20 20 2a 2f 0a 20 20 2a 70 70 50 61 67  s..  */.  *ppPag
14e59 65 20 3d 20 70 50 61 67 65 20 3d 20 62 74 72 65  e = pPage = btre
14e5a 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 74 2c  ePageLookup(pBt,
14e5b 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 50   pgno);.  if( pP
14e5c 61 67 65 20 29 7b 0a 20 20 20 20 2f 2a 20 50 61  age ){.    /* Pa
14e5d 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ge is already in
14e5e 20 63 61 63 68 65 20 2a 2f 0a 20 20 20 20 72 63   cache */.    rc
14e5f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
14e60 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 50 61  }else{.    /* Pa
14e61 67 65 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e  ge not in cache.
14e62 20 20 41 63 71 75 69 72 65 20 69 74 2e 20 2a 2f    Acquire it. */
14e63 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 61  .    if( pgno>pa
14e64 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
14e65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
14e66 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
14e67 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20  _BKPT; .    }.  
14e68 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
14e69 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
14e6a 70 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 30 29  pgno, ppPage, 0)
14e6b 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
14e6c 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 50  eturn rc;.    pP
14e6d 61 67 65 20 3d 20 2a 70 70 50 61 67 65 3b 0a 20  age = *ppPage;. 
14e6e 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d   }.  if( !pPage-
14e6f 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 72  >isInit ){.    r
14e70 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
14e71 49 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b  InitPage(pPage);
14e72 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53  .  }.  if( rc!=S
14e73 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
14e74 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
14e75 65 29 3b 0a 20 20 20 20 2a 70 70 50 61 67 65 20  e);.    *ppPage 
14e76 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
14e77 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
14e78 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 67 65  elease a MemPage
14e79 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62  .  This should b
14e7a 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f  e called once fo
14e7b 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a 20  r each prior.** 
14e7c 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42  call to sqlite3B
14e7d 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f 0a  treeGetPage..*/.
14e7e 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65  static void rele
14e7f 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20  asePage(MemPage 
14e80 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20 70  *pPage){.  if( p
14e81 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65  Page ){.    asse
14e82 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  rt( pPage->nOver
14e83 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  flow==0 || sqlit
14e84 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f  e3PagerPageRefco
14e85 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 50 61  unt(pPage->pDbPa
14e86 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 73 73  ge)>1 );.    ass
14e87 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74  ert( pPage->aDat
14e88 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  a );.    assert(
14e89 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20   pPage->pBt );. 
14e8a 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
14e8b 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
14e8c 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
14e8d 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20  == (void*)pPage 
14e8e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
14e8f 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
14e90 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  ta(pPage->pDbPag
14e91 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 61  e)==pPage->aData
14e92 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
14e93 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14e94 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
14e95 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c  utex) );.    sql
14e96 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
14e97 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
14e98 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 72    }.}../*.** Dur
14e99 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20  ing a rollback, 
14e9a 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 72  when the pager r
14e9b 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 69  eloads informati
14e9c 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68  on into the cach
14e9d 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65  e.** so that the
14e9e 20 63 61 63 68 65 20 69 73 20 72 65 73 74 6f 72   cache is restor
14e9f 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e  ed to its origin
14ea0 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 65 20  al state at the 
14ea1 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20  start of.** the 
14ea2 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f 72  transaction, for
14ea3 20 65 61 63 68 20 70 61 67 65 20 72 65 73 74 6f   each page resto
14ea4 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65  red this routine
14ea5 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a   is called..**.*
14ea6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e  * This routine n
14ea7 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 74 68  eeds to reset th
14ea8 65 20 65 78 74 72 61 20 64 61 74 61 20 73 65 63  e extra data sec
14ea9 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64 20  tion at the end 
14eaa 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74  of the.** page t
14eab 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 68 65  o agree with the
14eac 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e 0a   restored data..
14ead 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
14eae 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 67 65  ageReinit(DbPage
14eaf 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d 50   *pData){.  MemP
14eb0 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 50  age *pPage;.  pP
14eb1 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 20 2a  age = (MemPage *
14eb2 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  )sqlite3PagerGet
14eb3 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a 20 20  Extra(pData);.  
14eb4 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
14eb5 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74  agerPageRefcount
14eb6 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 20 69  (pData)>0 );.  i
14eb7 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  f( pPage->isInit
14eb8 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
14eb9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14eba 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
14ebb 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 50 61  utex) );.    pPa
14ebc 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  ge->isInit = 0;.
14ebd 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50      if( sqlite3P
14ebe 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74  agerPageRefcount
14ebf 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 20 20  (pData)>1 ){.   
14ec0 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 67 68     /* pPage migh
14ec1 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72 65 65  t not be a btree
14ec2 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67 68 74   page;  it might
14ec3 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   be an overflow 
14ec4 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72  page.      ** or
14ec5 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f 72 20   ptrmap page or 
14ec6 61 20 66 72 65 65 20 70 61 67 65 2e 20 20 49 6e  a free page.  In
14ec7 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 74 68   those cases, th
14ec8 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20  e following.    
14ec9 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c    ** call to sql
14eca 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
14ecb 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20  e() will likely 
14ecc 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
14ecd 52 52 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a 20  RRUPT..      ** 
14ece 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 64  But no harm is d
14ecf 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 20 41 6e  one by this.  An
14ed0 64 20 69 74 20 69 73 20 76 65 72 79 20 69 6d 70  d it is very imp
14ed1 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 20 20 20  ortant that.    
14ed2 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65    ** sqlite3Btre
14ed3 65 49 6e 69 74 50 61 67 65 28 29 20 62 65 20 63  eInitPage() be c
14ed4 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 79 20 62  alled on every b
14ed5 74 72 65 65 20 70 61 67 65 20 73 6f 20 77 65 20  tree page so we 
14ed6 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a 20 74 68  make.      ** th
14ed7 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 65 72 79  e call for every
14ed8 20 70 61 67 65 20 74 68 61 74 20 63 6f 6d 65 73   page that comes
14ed9 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e 69 74 69   in for re-initi
14eda 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ng. */.      sql
14edb 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
14edc 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  e(pPage);.    }.
14edd 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76    }.}../*.** Inv
14ede 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e  oke the busy han
14edf 64 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65  dler for a btree
14ee0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
14ee1 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48  btreeInvokeBusyH
14ee2 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 41 72  andler(void *pAr
14ee3 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  g){.  BtShared *
14ee4 70 42 74 20 3d 20 28 42 74 53 68 61 72 65 64 2a  pBt = (BtShared*
14ee5 29 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28  )pArg;.  assert(
14ee6 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73   pBt->db );.  as
14ee7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14ee8 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62  tex_held(pBt->db
14ee9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65  ->mutex) );.  re
14eea 74 75 72 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f  turn sqlite3Invo
14eeb 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 26 70  keBusyHandler(&p
14eec 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64  Bt->db->busyHand
14eed 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ler);.}../*.** O
14eee 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  pen a database f
14eef 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c  ile..** .** zFil
14ef0 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  ename is the nam
14ef1 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
14ef2 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c  e file.  If zFil
14ef3 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a  ename is NULL.**
14ef4 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
14ef5 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61  with a random na
14ef6 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  me is created.  
14ef7 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61  This randomly na
14ef8 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  med.** database 
14ef9 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c  file will be del
14efa 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65  eted when sqlite
14efb 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73  3BtreeClose() is
14efc 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a   called..** If z
14efd 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65  Filename is ":me
14efe 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 69  mory:" then an i
14eff 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
14f00 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20  e is created.** 
14f01 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69  that is automati
14f02 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 20  cally destroyed 
14f03 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65  when it is close
14f04 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
14f05 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65  database is alre
14f06 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68  ady opened in th
14f07 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
14f08 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e  connection.** an
14f09 64 20 77 65 20 61 72 65 20 69 6e 20 73 68 61 72  d we are in shar
14f0a 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74  ed cache mode, t
14f0b 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c  hen the open wil
14f0c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a  l fail with an.*
14f0d 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  * SQLITE_CONSTRA
14f0e 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63  INT error.  We c
14f0f 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20  annot allow two 
14f10 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64  or more BtShared
14f11 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74  .** objects in t
14f12 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
14f13 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63   connection sinc
14f14 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20  e doing so will 
14f15 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c  lead.** to probl
14f16 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67  ems with locking
14f17 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14f18 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
14f19 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73  treeOpen(.  cons
14f1a 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
14f1b 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  e,  /* Name of t
14f1c 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69  he file containi
14f1d 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74  ng the BTree dat
14f1e 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74  abase */.  sqlit
14f1f 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
14f20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64     /* Associated
14f21 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
14f22 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70   */.  Btree **pp
14f23 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a  Btree,        /*
14f24 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20   Pointer to new 
14f25 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69  Btree object wri
14f26 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69  tten here */.  i
14f27 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
14f28 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e         /* Option
14f29 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c  s */.  int vfsFl
14f2a 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
14f2b 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74  * Flags passed t
14f2c 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65  hrough to sqlite
14f2d 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f  3_vfs.xOpen() */
14f2e 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  .){.  sqlite3_vf
14f2f 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
14f30 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20       /* The VFS 
14f31 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20  to use for this 
14f32 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61  btree */.  BtSha
14f33 72 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20  red *pBt = 0;   
14f34 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61            /* Sha
14f35 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65  red part of btre
14f36 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
14f37 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20   Btree *p;      
14f38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14f39 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74  /* Handle to ret
14f3a 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  urn */.  sqlite3
14f3b 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65  _mutex *mutexOpe
14f3c 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65  n = 0;  /* Preve
14f3d 6e 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69  nts a race condi
14f3e 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35  tion. Ticket #35
14f3f 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  37 */.  int rc =
14f40 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
14f41 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
14f42 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20   code from this 
14f43 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  function */.  u8
14f44 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20   nReserve;      
14f45 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14f46 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73  Byte of unused s
14f47 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67  pace on each pag
14f48 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
14f49 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31  char zDbHeader[1
14f4a 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73  00];  /* Databas
14f4b 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74  e header content
14f4c 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68   */..  /* Set th
14f4d 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 6d  e variable isMem
14f4e 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 61  db to true for a
14f4f 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
14f50 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66  base, or .  ** f
14f51 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 2d  alse for a file-
14f52 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e 20  based database. 
14f53 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f  This symbol is o
14f54 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 0a  nly required if.
14f55 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 74    ** either of t
14f56 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 6f  he shared-data o
14f57 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 61  r autovacuum fea
14f58 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 6c  tures are compil
14f59 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  ed .  ** into th
14f5a 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a  e library..  */.
14f5b 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
14f5c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14f5d 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 6e  CACHE) || !defin
14f5e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ed(SQLITE_OMIT_A
14f5f 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 66  UTOVACUUM).  #if
14f60 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14f61 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e  MEMORYDB.    con
14f62 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d  st int isMemdb =
14f63 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 20   0;.  #else.    
14f64 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64  const int isMemd
14f65 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26  b = zFilename &&
14f66 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61   !strcmp(zFilena
14f67 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b  me, ":memory:");
14f68 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  .  #endif.#endif
14f69 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d  ..  assert( db!=
14f6a 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
14f6b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14f6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
14f6d 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 56  .  pVfs = db->pV
14f6e 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  fs;.  p = sqlite
14f6f 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65  3MallocZero(size
14f70 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66  of(Btree));.  if
14f71 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75  ( !p ){.    retu
14f72 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
14f73 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e  .  }.  p->inTran
14f74 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  s = TRANS_NONE;.
14f75 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a 23    p->db = db;..#
14f76 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
14f77 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
14f78 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65  ACHE) && !define
14f79 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49  d(SQLITE_OMIT_DI
14f7a 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20  SKIO).  /*.  ** 
14f7b 49 66 20 74 68 69 73 20 42 74 72 65 65 20 69 73  If this Btree is
14f7c 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72   a candidate for
14f7d 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74   shared cache, t
14f7e 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20  ry to find an.  
14f7f 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 68  ** existing BtSh
14f80 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74  ared object that
14f81 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 69   we can share wi
14f82 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  th.  */.  if( is
14f83 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a 46 69 6c  Memdb==0 && zFil
14f84 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61  ename && zFilena
14f85 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 28  me[0] ){.    if(
14f86 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
14f87 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65  nfig.sharedCache
14f88 45 6e 61 62 6c 65 64 20 29 7b 0a 20 20 20 20 20  Enabled ){.     
14f89 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61   int nFullPathna
14f8a 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74  me = pVfs->mxPat
14f8b 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63  hname+1;.      c
14f8c 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61  har *zFullPathna
14f8d 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  me = sqlite3Mall
14f8e 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65  oc(nFullPathname
14f8f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
14f90 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61  _mutex *mutexSha
14f91 72 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68  red;.      p->sh
14f92 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20  arable = 1;.    
14f93 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
14f94 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68  QLITE_SharedCach
14f95 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46  e;.      if( !zF
14f96 75 6c 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20  ullPathname ){. 
14f97 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
14f98 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20  ree(p);.        
14f99 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
14f9a 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
14f9b 20 20 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c     sqlite3OsFull
14f9c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a  Pathname(pVfs, z
14f9d 46 69 6c 65 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50  Filename, nFullP
14f9e 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61  athname, zFullPa
14f9f 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 6d  thname);.      m
14fa0 75 74 65 78 4f 70 65 6e 20 3d 20 73 71 6c 69 74  utexOpen = sqlit
14fa1 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
14fa2 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
14fa3 5f 4f 50 45 4e 29 3b 0a 20 20 20 20 20 20 73 71  _OPEN);.      sq
14fa4 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
14fa5 72 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20  r(mutexOpen);.  
14fa6 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 20      mutexShared 
14fa7 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
14fa8 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
14fa9 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b  _STATIC_MASTER);
14faa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
14fab 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
14fac 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 66  Shared);.      f
14fad 6f 72 28 70 42 74 3d 47 4c 4f 42 41 4c 28 42 74  or(pBt=GLOBAL(Bt
14fae 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
14faf 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b  haredCacheList);
14fb0 20 70 42 74 3b 20 70 42 74 3d 70 42 74 2d 3e 70   pBt; pBt=pBt->p
14fb1 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 61  Next){.        a
14fb2 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 52 65 66  ssert( pBt->nRef
14fb3 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  >0 );.        if
14fb4 28 20 30 3d 3d 73 74 72 63 6d 70 28 7a 46 75 6c  ( 0==strcmp(zFul
14fb5 6c 50 61 74 68 6e 61 6d 65 2c 20 73 71 6c 69 74  lPathname, sqlit
14fb6 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28  e3PagerFilename(
14fb7 70 42 74 2d 3e 70 50 61 67 65 72 29 29 0a 20 20  pBt->pPager)).  
14fb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
14fb9 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 56 66  & sqlite3PagerVf
14fba 73 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d  s(pBt->pPager)==
14fbb 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 20 20  pVfs ){.        
14fbc 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
14fbd 20 20 20 20 20 66 6f 72 28 69 44 62 3d 64 62 2d       for(iDb=db-
14fbe 3e 6e 44 62 2d 31 3b 20 69 44 62 3e 3d 30 3b 20  >nDb-1; iDb>=0; 
14fbf 69 44 62 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20  iDb--){.        
14fc0 20 20 20 20 42 74 72 65 65 20 2a 70 45 78 69 73      Btree *pExis
14fc1 74 69 6e 67 20 3d 20 64 62 2d 3e 61 44 62 5b 69  ting = db->aDb[i
14fc2 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 20  Db].pBt;.       
14fc3 20 20 20 20 20 69 66 28 20 70 45 78 69 73 74 69       if( pExisti
14fc4 6e 67 20 26 26 20 70 45 78 69 73 74 69 6e 67 2d  ng && pExisting-
14fc5 3e 70 42 74 3d 3d 70 42 74 20 29 7b 0a 20 20 20  >pBt==pBt ){.   
14fc6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
14fc7 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
14fc8 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20  utexShared);.   
14fc9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
14fca 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
14fcb 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20  utexOpen);.     
14fcc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
14fcd 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e  _free(zFullPathn
14fce 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ame);.          
14fcf 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
14fd0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  (p);.           
14fd1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
14fd2 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20  _CONSTRAINT;.   
14fd3 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
14fd4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
14fd5 20 70 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20   p->pBt = pBt;. 
14fd6 20 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6e 52           pBt->nR
14fd7 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  ef++;.          
14fd8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
14fd9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
14fda 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
14fdb 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b  ve(mutexShared);
14fdc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
14fdd 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d  ree(zFullPathnam
14fde 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66  e);.    }.#ifdef
14fdf 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
14fe0 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a    else{.      /*
14fe1 20 49 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20   In debug mode, 
14fe2 77 65 20 6d 61 72 6b 20 61 6c 6c 20 70 65 72 73  we mark all pers
14fe3 69 73 74 65 6e 74 20 64 61 74 61 62 61 73 65 73  istent databases
14fe4 20 61 73 20 73 68 61 72 61 62 6c 65 0a 20 20 20   as sharable.   
14fe5 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20     ** even when 
14fe6 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 54  they are not.  T
14fe7 68 69 73 20 65 78 65 72 63 69 73 65 73 20 74 68  his exercises th
14fe8 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 64 65 20 61  e locking code a
14fe9 6e 64 0a 20 20 20 20 20 20 2a 2a 20 67 69 76 65  nd.      ** give
14fea 73 20 6d 6f 72 65 20 6f 70 70 6f 72 74 75 6e 69  s more opportuni
14feb 74 79 20 66 6f 72 20 61 73 73 65 72 74 73 28 73  ty for asserts(s
14fec 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14fed 64 28 29 29 0a 20 20 20 20 20 20 2a 2a 20 73 74  d()).      ** st
14fee 61 74 65 6d 65 6e 74 73 20 74 6f 20 66 69 6e 64  atements to find
14fef 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 62 6c 65 6d   locking problem
14ff0 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  s..      */.    
14ff1 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20    p->sharable = 
14ff2 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  1;.    }.#endif.
14ff3 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
14ff4 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f   pBt==0 ){.    /
14ff5 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c  *.    ** The fol
14ff6 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 73 20 6d  lowing asserts m
14ff7 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 73 74  ake sure that st
14ff8 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62 79  ructures used by
14ff9 20 74 68 65 20 62 74 72 65 65 20 61 72 65 0a 20   the btree are. 
14ffa 20 20 20 2a 2a 20 74 68 65 20 72 69 67 68 74 20     ** the right 
14ffb 73 69 7a 65 2e 20 20 54 68 69 73 20 69 73 20 74  size.  This is t
14ffc 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20  o guard against 
14ffd 73 69 7a 65 20 63 68 61 6e 67 65 73 20 74 68 61  size changes tha
14ffe 74 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20  t result.    ** 
14fff 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 6f  when compiling o
15000 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 61 72  n a different ar
15001 63 68 69 74 65 63 74 75 72 65 2e 0a 20 20 20 20  chitecture..    
15002 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  */.    assert( s
15003 69 7a 65 6f 66 28 69 36 34 29 3d 3d 38 20 7c 7c  izeof(i64)==8 ||
15004 20 73 69 7a 65 6f 66 28 69 36 34 29 3d 3d 34 20   sizeof(i64)==4 
15005 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
15006 69 7a 65 6f 66 28 75 36 34 29 3d 3d 38 20 7c 7c  izeof(u64)==8 ||
15007 20 73 69 7a 65 6f 66 28 75 36 34 29 3d 3d 34 20   sizeof(u64)==4 
15008 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
15009 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b  izeof(u32)==4 );
1500a 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a  .    assert( siz
1500b 65 6f 66 28 75 31 36 29 3d 3d 32 20 29 3b 0a 20  eof(u16)==2 );. 
1500c 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
1500d 66 28 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20  f(Pgno)==4 );.  
1500e 0a 20 20 20 20 70 42 74 20 3d 20 73 71 6c 69 74  .    pBt = sqlit
1500f 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69  e3MallocZero( si
15010 7a 65 6f 66 28 2a 70 42 74 29 20 29 3b 0a 20 20  zeof(*pBt) );.  
15011 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a    if( pBt==0 ){.
15012 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
15013 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67  E_NOMEM;.      g
15014 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f  oto btree_open_o
15015 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  ut;.    }.    rc
15016 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f   = sqlite3PagerO
15017 70 65 6e 28 70 56 66 73 2c 20 26 70 42 74 2d 3e  pen(pVfs, &pBt->
15018 70 50 61 67 65 72 2c 20 7a 46 69 6c 65 6e 61 6d  pPager, zFilenam
15019 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
1501a 20 20 20 20 20 20 20 20 20 20 20 20 20 45 58 54               EXT
1501b 52 41 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c 20  RA_SIZE, flags, 
1501c 76 66 73 46 6c 61 67 73 29 3b 0a 20 20 20 20 69  vfsFlags);.    i
1501d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1501e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
1501f 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46  qlite3PagerReadF
15020 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e 70  ileheader(pBt->p
15021 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44 62  Pager,sizeof(zDb
15022 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64 65  Header),zDbHeade
15023 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  r);.    }.    if
15024 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
15025 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74  ){.      goto bt
15026 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  ree_open_out;.  
15027 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 62 20    }.    pBt->db 
15028 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65  = db;.    sqlite
15029 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
1502a 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72  dler(pBt->pPager
1502b 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73  , btreeInvokeBus
1502c 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b 0a  yHandler, pBt);.
1502d 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 74      p->pBt = pBt
1502e 3b 0a 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33  ;.  .    sqlite3
1502f 50 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 72  PagerSetReiniter
15030 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 61  (pBt->pPager, pa
15031 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 20 70  geReinit);.    p
15032 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b  Bt->pCursor = 0;
15033 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31  .    pBt->pPage1
15034 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 72   = 0;.    pBt->r
15035 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65  eadOnly = sqlite
15036 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79  3PagerIsreadonly
15037 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
15038 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65     pBt->pageSize
15039 20 3d 20 67 65 74 32 62 79 74 65 28 26 7a 44 62   = get2byte(&zDb
1503a 48 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 20  Header[16]);.   
1503b 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69   if( pBt->pageSi
1503c 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70  ze<512 || pBt->p
1503d 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d  ageSize>SQLITE_M
1503e 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20  AX_PAGE_SIZE.   
1503f 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e        || ((pBt->
15040 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d  pageSize-1)&pBt-
15041 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b  >pageSize)!=0 ){
15042 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65  .      pBt->page
15043 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65  Size = 0;.#ifnde
15044 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
15045 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f  TOVACUUM.      /
15046 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e  * If the magic n
15047 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77  ame ":memory:" w
15048 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e  ill create an in
15049 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
1504a 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20  , then.      ** 
1504b 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61  leave the autoVa
1504c 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28  cuum mode at 0 (
1504d 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75  do not auto-vacu
1504e 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20  um), even if.   
1504f 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46     ** SQLITE_DEF
15050 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20  AULT_AUTOVACUUM 
15051 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20  is true. On the 
15052 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20  other hand, if. 
15053 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f       ** SQLITE_O
15054 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73  MIT_MEMORYDB has
15055 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74   been defined, t
15056 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69  hen ":memory:" i
15057 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a  s just a.      *
15058 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e  * regular file-n
15059 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ame. In this cas
1505a 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75  e the auto-vacuu
1505b 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72  m applies as per
1505c 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a   normal..      *
1505d 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c  /.      if( zFil
1505e 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64  ename && !isMemd
1505f 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74  b ){.        pBt
15060 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28  ->autoVacuum = (
15061 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41  SQLITE_DEFAULT_A
15062 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20  UTOVACUUM ? 1 : 
15063 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d  0);.        pBt-
15064 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53  >incrVacuum = (S
15065 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
15066 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20  TOVACUUM==2 ? 1 
15067 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  : 0);.      }.#e
15068 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65  ndif.      nRese
15069 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  rve = 0;.    }el
1506a 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72  se{.      nReser
1506b 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32  ve = zDbHeader[2
1506c 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70  0];.      pBt->p
1506d 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31  ageSizeFixed = 1
1506e 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1506f 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
15070 0a 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f  .      pBt->auto
15071 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79  Vacuum = (get4by
15072 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36  te(&zDbHeader[36
15073 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20   + 4*4])?1:0);. 
15074 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61       pBt->incrVa
15075 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65  cuum = (get4byte
15076 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b  (&zDbHeader[36 +
15077 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e   7*4])?1:0);.#en
15078 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  dif.    }.    rc
15079 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53   = sqlite3PagerS
1507a 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e  etPagesize(pBt->
1507b 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61  pPager, &pBt->pa
1507c 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28  geSize);.    if(
1507d 20 72 63 20 29 20 67 6f 74 6f 20 62 74 72 65 65   rc ) goto btree
1507e 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 70  _open_out;.    p
1507f 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d  Bt->usableSize =
15080 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d   pBt->pageSize -
15081 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 20 20 61   nReserve;.    a
15082 73 73 65 72 74 28 20 28 70 42 74 2d 3e 70 61 67  ssert( (pBt->pag
15083 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b  eSize & 7)==0 );
15084 20 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67    /* 8-byte alig
15085 6e 6d 65 6e 74 20 6f 66 20 70 61 67 65 53 69 7a  nment of pageSiz
15086 65 20 2a 2f 0a 20 20 20 0a 23 69 66 20 21 64 65  e */.   .#if !de
15087 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
15088 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20  T_SHARED_CACHE) 
15089 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
1508a 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a  TE_OMIT_DISKIO).
1508b 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e      /* Add the n
1508c 65 77 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  ew BtShared obje
1508d 63 74 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64  ct to the linked
1508e 20 6c 69 73 74 20 73 68 61 72 61 62 6c 65 20 42   list sharable B
1508f 74 53 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f  tShareds..    */
15090 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72  .    if( p->shar
15091 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71  able ){.      sq
15092 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
15093 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20  exShared;.      
15094 70 42 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  pBt->nRef = 1;. 
15095 20 20 20 20 20 6d 75 74 65 78 53 68 61 72 65 64       mutexShared
15096 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
15097 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
15098 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
15099 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  ;.      if( SQLI
1509a 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
1509b 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
1509c 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20  nfig.bCoreMutex 
1509d 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  ){.        pBt->
1509e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
1509f 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
150a0 5f 4d 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20  _MUTEX_FAST);.  
150a1 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d        if( pBt->m
150a2 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20  utex==0 ){.     
150a3 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
150a4 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
150a5 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
150a6 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ed = 0;.        
150a7 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65    goto btree_ope
150a8 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d  n_out;.        }
150a9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
150aa 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
150ab 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b  er(mutexShared);
150ac 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78  .      pBt->pNex
150ad 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61  t = GLOBAL(BtSha
150ae 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
150af 65 64 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20  edCacheList);.  
150b0 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61      GLOBAL(BtSha
150b1 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
150b2 65 64 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70  edCacheList) = p
150b3 42 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  Bt;.      sqlite
150b4 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
150b5 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20  texShared);.    
150b6 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69  }.#endif.  }..#i
150b7 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
150b8 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
150b9 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64  CHE) && !defined
150ba 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53  (SQLITE_OMIT_DIS
150bb 4b 49 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65  KIO).  /* If the
150bc 20 6e 65 77 20 42 74 72 65 65 20 75 73 65 73 20   new Btree uses 
150bd 61 20 73 68 61 72 61 62 6c 65 20 70 42 74 53 68  a sharable pBtSh
150be 61 72 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20  ared, then link 
150bf 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72  the new.  ** Btr
150c0 65 65 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74  ee into the list
150c1 20 6f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65   of all sharable
150c2 20 42 74 72 65 65 73 20 66 6f 72 20 74 68 65 20   Btrees for the 
150c3 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  same connection.
150c4 0a 20 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69  .  ** The list i
150c5 73 20 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64  s kept in ascend
150c6 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 42 74  ing order by pBt
150c7 20 61 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20   address..  */. 
150c8 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65   if( p->sharable
150c9 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
150ca 20 20 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a     Btree *pSib;.
150cb 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
150cc 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
150cd 20 20 20 20 69 66 28 20 28 70 53 69 62 20 3d 20      if( (pSib = 
150ce 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21  db->aDb[i].pBt)!
150cf 3d 30 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72  =0 && pSib->shar
150d0 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
150d1 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72  while( pSib->pPr
150d2 65 76 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69  ev ){ pSib = pSi
150d3 62 2d 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20  b->pPrev; }.    
150d4 20 20 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70      if( p->pBt<p
150d5 53 69 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20  Sib->pBt ){.    
150d6 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
150d7 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20   pSib;.         
150d8 20 70 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20   p->pPrev = 0;. 
150d9 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70           pSib->p
150da 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20  Prev = p;.      
150db 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
150dc 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e     while( pSib->
150dd 70 4e 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70  pNext && pSib->p
150de 4e 65 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74  Next->pBt<p->pBt
150df 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
150e0 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65  pSib = pSib->pNe
150e1 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  xt;.          }.
150e2 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65            p->pNe
150e3 78 74 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74  xt = pSib->pNext
150e4 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  ;.          p->p
150e5 50 72 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20  Prev = pSib;.   
150e6 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e         if( p->pN
150e7 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ext ){.         
150e8 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72     p->pNext->pPr
150e9 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  ev = p;.        
150ea 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53    }.          pS
150eb 69 62 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20  ib->pNext = p;. 
150ec 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
150ed 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
150ee 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
150ef 0a 20 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b  .  *ppBtree = p;
150f0 0a 0a 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74  ..btree_open_out
150f1 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  :.  if( rc!=SQLI
150f2 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
150f3 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61   pBt && pBt->pPa
150f4 67 65 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ger ){.      sql
150f5 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70  ite3PagerClose(p
150f6 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
150f7 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
150f8 72 65 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71  ree(pBt);.    sq
150f9 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
150fa 20 20 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b     *ppBtree = 0;
150fb 0a 20 20 7d 0a 20 20 69 66 28 20 6d 75 74 65 78  .  }.  if( mutex
150fc 4f 70 65 6e 20 29 7b 0a 20 20 20 20 61 73 73 65  Open ){.    asse
150fd 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
150fe 78 5f 68 65 6c 64 28 6d 75 74 65 78 4f 70 65 6e  x_held(mutexOpen
150ff 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) );.    sqlite3
15100 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
15101 65 78 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 20 72  exOpen);.  }.  r
15102 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
15103 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  ** Decrement the
15104 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63   BtShared.nRef c
15105 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 74  ounter.  When it
15106 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a 2a   reaches zero,.*
15107 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 74 53  * remove the BtS
15108 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20  hared structure 
15109 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 6e 67  from the sharing
1510a 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 0a 2a   list.  Return.*
1510b 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 42 74  * true if the Bt
1510c 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e  Shared.nRef coun
1510d 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 72 6f  ter reaches zero
1510e 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 66   and return.** f
1510f 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 74  alse if it is st
15110 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2f  ill positive..*/
15111 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f  .static int remo
15112 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73  veFromSharingLis
15113 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  t(BtShared *pBt)
15114 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
15115 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
15116 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  HE.  sqlite3_mut
15117 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 42  ex *pMaster;.  B
15118 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a  tShared *pList;.
15119 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20    int removed = 
1511a 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  0;..  assert( sq
1511b 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
1511c 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
1511d 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73  );.  pMaster = s
1511e 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
1511f 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
15120 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20  ATIC_MASTER);.  
15121 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
15122 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ter(pMaster);.  
15123 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69  pBt->nRef--;.  i
15124 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20  f( pBt->nRef<=0 
15125 29 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41  ){.    if( GLOBA
15126 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69  L(BtShared*,sqli
15127 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
15128 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20  st)==pBt ){.    
15129 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65    GLOBAL(BtShare
1512a 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
1512b 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74  CacheList) = pBt
1512c 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c  ->pNext;.    }el
1512d 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20  se{.      pList 
1512e 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65  = GLOBAL(BtShare
1512f 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
15130 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20  CacheList);.    
15131 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28    while( ALWAYS(
15132 70 4c 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d  pList) && pList-
15133 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20  >pNext!=pBt ){. 
15134 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69         pList=pLi
15135 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  st->pNext;.     
15136 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57   }.      if( ALW
15137 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20  AYS(pList) ){.  
15138 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65        pList->pNe
15139 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b  xt = pBt->pNext;
1513a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1513b 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48     if( SQLITE_TH
1513c 52 45 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20  READSAFE ){.    
1513d 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1513e 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29  free(pBt->mutex)
1513f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f  ;.    }.    remo
15140 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73  ved = 1;.  }.  s
15141 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
15142 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72  ve(pMaster);.  r
15143 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23  eturn removed;.#
15144 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b  else.  return 1;
15145 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
15146 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e   Make sure pBt->
15147 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73  pTmpSpace points
15148 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f   to an allocatio
15149 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c  n of .** MX_CELL
1514a 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74 65 73  _SIZE(pBt) bytes
1514b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1514c 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61   allocateTempSpa
1514d 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ce(BtShared *pBt
1514e 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70  ){.  if( !pBt->p
1514f 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20  TmpSpace ){.    
15150 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d  pBt->pTmpSpace =
15151 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c   sqlite3PageMall
15152 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  oc( pBt->pageSiz
15153 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  e );.  }.}../*.*
15154 2a 20 46 72 65 65 20 74 68 65 20 70 42 74 2d 3e  * Free the pBt->
15155 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 61  pTmpSpace alloca
15156 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tion.*/.static v
15157 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70 61 63  oid freeTempSpac
15158 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  e(BtShared *pBt)
15159 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46  {.  sqlite3PageF
1515a 72 65 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70  ree( pBt->pTmpSp
1515b 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e 70 54 6d  ace);.  pBt->pTm
1515c 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f  pSpace = 0;.}../
1515d 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70  *.** Close an op
1515e 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  en database and 
1515f 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 63  invalidate all c
15160 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ursors..*/.SQLIT
15161 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
15162 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
15163 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53  Btree *p){.  BtS
15164 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
15165 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  pBt;.  BtCursor 
15166 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f  *pCur;..  /* Clo
15167 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f  se all cursors o
15168 70 65 6e 65 64 20 76 69 61 20 74 68 69 73 20 68  pened via this h
15169 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73  andle.  */.  ass
1516a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1516b 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
1516c 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1516d 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
1516e 0a 20 20 70 43 75 72 20 3d 20 70 42 74 2d 3e 70  .  pCur = pBt->p
1516f 43 75 72 73 6f 72 3b 0a 20 20 77 68 69 6c 65 28  Cursor;.  while(
15170 20 70 43 75 72 20 29 7b 0a 20 20 20 20 42 74 43   pCur ){.    BtC
15171 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d 20 70 43  ursor *pTmp = pC
15172 75 72 3b 0a 20 20 20 20 70 43 75 72 20 3d 20 70  ur;.    pCur = p
15173 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Cur->pNext;.    
15174 69 66 28 20 70 54 6d 70 2d 3e 70 42 74 72 65 65  if( pTmp->pBtree
15175 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==p ){.      sql
15176 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75  ite3BtreeCloseCu
15177 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20 20 20 20  rsor(pTmp);.    
15178 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c  }.  }..  /* Roll
15179 62 61 63 6b 20 61 6e 79 20 61 63 74 69 76 65 20  back any active 
1517a 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20  transaction and 
1517b 66 72 65 65 20 74 68 65 20 68 61 6e 64 6c 65 20  free the handle 
1517c 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20  structure..  ** 
1517d 54 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  The call to sqli
1517e 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  te3BtreeRollback
1517f 28 29 20 64 72 6f 70 73 20 61 6e 79 20 74 61 62  () drops any tab
15180 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79  le-locks held by
15181 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 6e 64 6c  .  ** this handl
15182 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  e..  */.  sqlite
15183 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70  3BtreeRollback(p
15184 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
15185 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f 2a  eLeave(p);..  /*
15186 20 49 66 20 74 68 65 72 65 20 61 72 65 20 73 74   If there are st
15187 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 61  ill other outsta
15188 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73  nding references
15189 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62   to the shared-b
1518a 74 72 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 74  tree.  ** struct
1518b 75 72 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e  ure, return now.
1518c 20 54 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f   The remainder o
1518d 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  f this procedure
1518e 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 70   cleans .  ** up
1518f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65   the shared-btre
15190 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
15191 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
15192 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d  =0 && p->locked=
15193 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 2d 3e  =0 );.  if( !p->
15194 73 68 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d 6f  sharable || remo
15195 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73  veFromSharingLis
15196 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 2f 2a  t(pBt) ){.    /*
15197 20 54 68 65 20 70 42 74 20 69 73 20 6e 6f 20 6c   The pBt is no l
15198 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20 73 68 61  onger on the sha
15199 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 65  ring list, so we
1519a 20 63 61 6e 20 61 63 63 65 73 73 0a 20 20 20 20   can access.    
1519b 2a 2a 20 69 74 20 77 69 74 68 6f 75 74 20 68 61  ** it without ha
1519c 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 65  ving to hold the
1519d 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a 20   mutex..    **. 
1519e 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 20     ** Clean out 
1519f 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 42  and delete the B
151a0 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a  tShared object..
151a1 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
151a2 74 28 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72  t( !pBt->pCursor
151a3 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   );.    sqlite3P
151a4 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70  agerClose(pBt->p
151a5 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
151a6 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61  pBt->xFreeSchema
151a7 20 26 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61   && pBt->pSchema
151a8 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78   ){.      pBt->x
151a9 46 72 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e  FreeSchema(pBt->
151aa 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a  pSchema);.    }.
151ab 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
151ac 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  (pBt->pSchema);.
151ad 20 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63      freeTempSpac
151ae 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69  e(pBt);.    sqli
151af 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20  te3_free(pBt);. 
151b0 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
151b1 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
151b2 41 43 48 45 0a 20 20 61 73 73 65 72 74 28 20 70  ACHE.  assert( p
151b3 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
151b4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
151b5 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69  locked==0 );.  i
151b6 66 28 20 70 2d 3e 70 50 72 65 76 20 29 20 70 2d  f( p->pPrev ) p-
151b7 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
151b8 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69 66 28 20  p->pNext;.  if( 
151b9 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d 3e 70 4e  p->pNext ) p->pN
151ba 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e  ext->pPrev = p->
151bb 70 50 72 65 76 3b 0a 23 65 6e 64 69 66 0a 0a 20  pPrev;.#endif.. 
151bc 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
151bd 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
151be 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
151bf 68 61 6e 67 65 20 74 68 65 20 6c 69 6d 69 74 20  hange the limit 
151c0 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  on the number of
151c1 20 70 61 67 65 73 20 61 6c 6c 6f 77 65 64 20 69   pages allowed i
151c2 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a 0a  n the cache..**.
151c3 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
151c4 75 6d 62 65 72 20 6f 66 20 63 61 63 68 65 20 70  umber of cache p
151c5 61 67 65 73 20 69 73 20 73 65 74 20 74 6f 20 74  ages is set to t
151c6 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a 2a 20 76  he absolute.** v
151c7 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 2e 20  alue of mxPage. 
151c8 20 49 66 20 6d 78 50 61 67 65 20 69 73 20 6e 65   If mxPage is ne
151c9 67 61 74 69 76 65 2c 20 74 68 65 20 70 61 67 65  gative, the page
151ca 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65 72 61 74  r will.** operat
151cb 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 6c 79  e asynchronously
151cc 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 73   - it will not s
151cd 74 6f 70 20 74 6f 20 64 6f 20 66 73 79 6e 63 28  top to do fsync(
151ce 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75 72 65 20  )s.** to insure 
151cf 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20  data is written 
151d0 74 6f 20 74 68 65 20 64 69 73 6b 20 73 75 72 66  to the disk surf
151d1 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f  ace before.** co
151d2 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72 61 6e 73  ntinuing.  Trans
151d3 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c 20 77 6f  actions still wo
151d4 72 6b 20 69 66 20 73 79 6e 63 68 72 6f 6e 6f 75  rk if synchronou
151d5 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20 61 6e 64  s is off,.** and
151d6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61   the database ca
151d7 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 74 65  nnot be corrupte
151d8 64 20 69 66 20 74 68 69 73 20 70 72 6f 67 72 61  d if this progra
151d9 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e 20 20 42  m.** crashes.  B
151da 75 74 20 69 66 20 74 68 65 20 6f 70 65 72 61 74  ut if the operat
151db 69 6e 67 20 73 79 73 74 65 6d 20 63 72 61 73 68  ing system crash
151dc 65 73 20 6f 72 20 74 68 65 72 65 20 69 73 0a 2a  es or there is.*
151dd 2a 20 61 6e 20 61 62 72 75 70 74 20 70 6f 77 65  * an abrupt powe
151de 72 20 66 61 69 6c 75 72 65 20 77 68 65 6e 20 73  r failure when s
151df 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66  ynchronous is of
151e0 66 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  f, the database.
151e1 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c 65 66 74  ** could be left
151e2 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74   in an inconsist
151e3 65 6e 74 20 61 6e 64 20 75 6e 72 65 63 6f 76 65  ent and unrecove
151e4 72 61 62 6c 65 20 73 74 61 74 65 2e 0a 2a 2a 20  rable state..** 
151e5 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f  Synchronous is o
151e6 6e 20 62 79 20 64 65 66 61 75 6c 74 20 73 6f 20  n by default so 
151e7 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
151e8 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 6e 6f  ion is not.** no
151e9 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72 79 2e 0a  rmally a worry..
151ea 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
151eb 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
151ec 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42  eeSetCacheSize(B
151ed 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50  tree *p, int mxP
151ee 61 67 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64  age){.  BtShared
151ef 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
151f0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
151f1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
151f2 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
151f3 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
151f4 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  r(p);.  sqlite3P
151f5 61 67 65 72 53 65 74 43 61 63 68 65 73 69 7a 65  agerSetCachesize
151f6 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78  (pBt->pPager, mx
151f7 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Page);.  sqlite3
151f8 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
151f9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
151fa 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  K;.}../*.** Chan
151fb 67 65 20 74 68 65 20 77 61 79 20 64 61 74 61 20  ge the way data 
151fc 69 73 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73  is synced to dis
151fd 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e  k in order to in
151fe 63 72 65 61 73 65 20 6f 72 20 64 65 63 72 65 61  crease or decrea
151ff 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c 6c 20 74  se.** how well t
15200 68 65 20 64 61 74 61 62 61 73 65 20 72 65 73 69  he database resi
15201 73 74 73 20 64 61 6d 61 67 65 20 64 75 65 20 74  sts damage due t
15202 6f 20 4f 53 20 63 72 61 73 68 65 73 20 61 6e 64  o OS crashes and
15203 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72   power.** failur
15204 65 73 2e 20 20 4c 65 76 65 6c 20 31 20 69 73 20  es.  Level 1 is 
15205 74 68 65 20 73 61 6d 65 20 61 73 20 61 73 79 6e  the same as asyn
15206 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20 73 79 6e  chronous (no syn
15207 63 73 28 29 20 6f 63 63 75 72 20 61 6e 64 0a 2a  cs() occur and.*
15208 2a 20 74 68 65 72 65 20 69 73 20 61 20 68 69 67  * there is a hig
15209 68 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66  h probability of
1520a 20 64 61 6d 61 67 65 29 20 20 4c 65 76 65 6c 20   damage)  Level 
1520b 32 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  2 is the default
1520c 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69 73 20 61  .  There.** is a
1520d 20 76 65 72 79 20 6c 6f 77 20 62 75 74 20 6e 6f   very low but no
1520e 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69  n-zero probabili
1520f 74 79 20 6f 66 20 64 61 6d 61 67 65 2e 20 20 4c  ty of damage.  L
15210 65 76 65 6c 20 33 20 72 65 64 75 63 65 73 20 74  evel 3 reduces t
15211 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69 6c 69 74  he.** probabilit
15212 79 20 6f 66 20 64 61 6d 61 67 65 20 74 6f 20 6e  y of damage to n
15213 65 61 72 20 7a 65 72 6f 20 62 75 74 20 77 69 74  ear zero but wit
15214 68 20 61 20 77 72 69 74 65 20 70 65 72 66 6f 72  h a write perfor
15215 6d 61 6e 63 65 20 72 65 64 75 63 74 69 6f 6e 2e  mance reduction.
15216 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
15217 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
15218 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49  AGMAS.SQLITE_PRI
15219 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1521a 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65  BtreeSetSafetyLe
1521b 76 65 6c 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  vel(Btree *p, in
1521c 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 66 75 6c  t level, int ful
1521d 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53 68 61 72  lSync){.  BtShar
1521e 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1521f 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
15220 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
15221 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
15222 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
15223 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  ter(p);.  sqlite
15224 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c  3PagerSetSafetyL
15225 65 76 65 6c 28 70 42 74 2d 3e 70 50 61 67 65 72  evel(pBt->pPager
15226 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c 53 79 6e  , level, fullSyn
15227 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  c);.  sqlite3Btr
15228 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
15229 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1522a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1522b 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
1522c 68 65 20 67 69 76 65 6e 20 62 74 72 65 65 20 69  he given btree i
1522d 73 20 73 65 74 20 74 6f 20 73 61 66 65 74 79 20  s set to safety 
1522e 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68  level 1.  In oth
1522f 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74  er.** words, ret
15230 75 72 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 73  urn TRUE if no s
15231 79 6e 63 28 29 20 6f 63 63 75 72 73 20 6f 6e 20  ync() occurs on 
15232 74 68 65 20 64 69 73 6b 20 66 69 6c 65 73 2e 0a  the disk files..
15233 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
15234 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
15235 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42  eeSyncDisabled(B
15236 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68  tree *p){.  BtSh
15237 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
15238 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  Bt;.  int rc;.  
15239 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1523a 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
1523b 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20 0a 20 20  ->mutex) );  .  
1523c 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1523d 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r(p);.  assert( 
1523e 70 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67  pBt && pBt->pPag
1523f 65 72 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  er );.  rc = sql
15240 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28  ite3PagerNosync(
15241 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
15242 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
15243 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
15244 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e  c;.}..#if !defin
15245 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ed(SQLITE_OMIT_P
15246 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c  AGER_PRAGMAS) ||
15247 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
15248 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 0a 2f 2a  _OMIT_VACUUM)./*
15249 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 64  .** Change the d
1524a 65 66 61 75 6c 74 20 70 61 67 65 73 20 73 69 7a  efault pages siz
1524b 65 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72  e and the number
1524c 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 79 74   of reserved byt
1524d 65 73 20 70 65 72 20 70 61 67 65 2e 0a 2a 2a 20  es per page..** 
1524e 4f 72 2c 20 69 66 20 74 68 65 20 70 61 67 65 20  Or, if the page 
1524f 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61 64 79  size has already
15250 20 62 65 65 6e 20 66 69 78 65 64 2c 20 72 65 74   been fixed, ret
15251 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
15252 4e 4c 59 20 0a 2a 2a 20 77 69 74 68 6f 75 74 20  NLY .** without 
15253 63 68 61 6e 67 69 6e 67 20 61 6e 79 74 68 69 6e  changing anythin
15254 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67  g..**.** The pag
15255 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61  e size must be a
15256 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77   power of 2 betw
15257 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33  een 512 and 6553
15258 36 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 0a  6.  If the page.
15259 2a 2a 20 73 69 7a 65 20 73 75 70 70 6c 69 65 64  ** size supplied
1525a 20 64 6f 65 73 20 6e 6f 74 20 6d 65 65 74 20 74   does not meet t
1525b 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 74  his constraint t
1525c 68 65 6e 20 74 68 65 20 70 61 67 65 20 73 69 7a  hen the page siz
1525d 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e  e is not.** chan
1525e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20  ged..**.** Page 
1525f 73 69 7a 65 73 20 61 72 65 20 63 6f 6e 73 74 72  sizes are constr
15260 61 69 6e 65 64 20 74 6f 20 62 65 20 61 20 70 6f  ained to be a po
15261 77 65 72 20 6f 66 20 74 77 6f 20 73 6f 20 74 68  wer of two so th
15262 61 74 20 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a  at the region.**
15263 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
15264 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 6c   file used for l
15265 6f 63 6b 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e  ocking (beginnin
15266 67 20 61 74 20 50 45 4e 44 49 4e 47 5f 42 59 54  g at PENDING_BYT
15267 45 2c 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  E,.** the first 
15268 62 79 74 65 20 70 61 73 74 20 74 68 65 20 31 47  byte past the 1G
15269 42 20 62 6f 75 6e 64 61 72 79 2c 20 30 78 34 30  B boundary, 0x40
1526a 30 30 30 30 30 30 29 20 6e 65 65 64 73 20 74 6f  000000) needs to
1526b 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65   occur.** at the
1526c 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20   beginning of a 
1526d 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  page..**.** If p
1526e 61 72 61 6d 65 74 65 72 20 6e 52 65 73 65 72 76  arameter nReserv
1526f 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a  e is less than z
15270 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6e 75  ero, then the nu
15271 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64  mber of reserved
15272 0a 2a 2a 20 62 79 74 65 73 20 70 65 72 20 70 61  .** bytes per pa
15273 67 65 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61  ge is left uncha
15274 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  nged..**.** If t
15275 68 65 20 69 46 69 78 21 3d 30 20 74 68 65 6e 20  he iFix!=0 then 
15276 74 68 65 20 70 61 67 65 53 69 7a 65 46 69 78 65  the pageSizeFixe
15277 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 73 6f  d flag is set so
15278 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 73   that the page s
15279 69 7a 65 0a 2a 2a 20 61 6e 64 20 61 75 74 6f 76  ize.** and autov
1527a 61 63 75 75 6d 20 6d 6f 64 65 20 63 61 6e 20 6e  acuum mode can n
1527b 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e  o longer be chan
1527c 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ged..*/.SQLITE_P
1527d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1527e 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
1527f 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  ze(Btree *p, int
15280 20 70 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e   pageSize, int n
15281 52 65 73 65 72 76 65 2c 20 69 6e 74 20 69 46 69  Reserve, int iFi
15282 78 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  x){.  int rc = S
15283 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 53 68  QLITE_OK;.  BtSh
15284 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
15285 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 52  Bt;.  assert( nR
15286 65 73 65 72 76 65 3e 3d 2d 31 20 26 26 20 6e 52  eserve>=-1 && nR
15287 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20  eserve<=255 );. 
15288 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
15289 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74  er(p);.  if( pBt
1528a 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20  ->pageSizeFixed 
1528b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
1528c 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20  reeLeave(p);.   
1528d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
1528e 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69  EADONLY;.  }.  i
1528f 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29 7b  f( nReserve<0 ){
15290 0a 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20  .    nReserve = 
15291 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20  pBt->pageSize - 
15292 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
15293 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
15294 52 65 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52  Reserve>=0 && nR
15295 65 73 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20  eserve<=255 );. 
15296 20 69 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35   if( pageSize>=5
15297 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d  12 && pageSize<=
15298 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
15299 53 49 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20  SIZE &&.        
1529a 28 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61  ((pageSize-1)&pa
1529b 67 65 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20  geSize)==0 ){.  
1529c 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53    assert( (pageS
1529d 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20  ize & 7)==0 );. 
1529e 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d     assert( !pBt-
1529f 3e 70 50 61 67 65 31 20 26 26 20 21 70 42 74 2d  >pPage1 && !pBt-
152a0 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20  >pCursor );.    
152a1 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20  pBt->pageSize = 
152a2 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20  (u16)pageSize;. 
152a3 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65     freeTempSpace
152a4 28 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20  (pBt);.    rc = 
152a5 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
152a6 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61  agesize(pBt->pPa
152a7 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53  ger, &pBt->pageS
152a8 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 70 42 74 2d  ize);.  }.  pBt-
152a9 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42  >usableSize = pB
152aa 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 28 75  t->pageSize - (u
152ab 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 69  16)nReserve;.  i
152ac 66 28 20 69 46 69 78 20 29 20 70 42 74 2d 3e 70  f( iFix ) pBt->p
152ad 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31  ageSizeFixed = 1
152ae 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
152af 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
152b0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
152b1 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65  Return the curre
152b2 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67  ntly defined pag
152b3 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45  e size.*/.SQLITE
152b4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
152b5 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
152b6 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  Size(Btree *p){.
152b7 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d    return p->pBt-
152b8 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a  >pageSize;.}../*
152b9 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
152ba 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
152bb 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65  f space at the e
152bc 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 65  nd of every page
152bd 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74   that.** are int
152be 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e  entually left un
152bf 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 74  used.  This is t
152c0 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73 70  he "reserved" sp
152c1 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 73  ace that is.** s
152c2 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 79  ometimes used by
152c3 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
152c4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
152c5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
152c6 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 20  etReserve(Btree 
152c7 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  *p){.  int n;.  
152c8 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
152c9 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70  r(p);.  n = p->p
152ca 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70  Bt->pageSize - p
152cb 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
152cc 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  e;.  sqlite3Btre
152cd 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
152ce 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn n;.}../*.** 
152cf 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  Set the maximum 
152d0 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 61  page count for a
152d1 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 50   database if mxP
152d2 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e  age is positive.
152d3 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 61  .** No changes a
152d4 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 67  re made if mxPag
152d5 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 69  e is 0 or negati
152d6 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73  ve..** Regardles
152d7 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f  s of the value o
152d8 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e  f mxPage, return
152d9 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   the maximum pag
152da 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  e count..*/.SQLI
152db 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
152dc 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61  qlite3BtreeMaxPa
152dd 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70  geCount(Btree *p
152de 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
152df 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65   int n;.  sqlite
152e0 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
152e1 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 67    n = sqlite3Pag
152e2 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70  erMaxPageCount(p
152e3 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d  ->pBt->pPager, m
152e4 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65  xPage);.  sqlite
152e5 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
152e6 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65    return n;.}.#e
152e7 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
152e8 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47  (SQLITE_OMIT_PAG
152e9 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21  ER_PRAGMAS) || !
152ea 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
152eb 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a  MIT_VACUUM) */..
152ec 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
152ed 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70   'auto-vacuum' p
152ee 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 64  roperty of the d
152ef 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20  atabase. If the 
152f0 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20  'autoVacuum'.** 
152f1 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e  parameter is non
152f2 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f  -zero, then auto
152f3 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 20  -vacuum mode is 
152f4 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f  enabled. If zero
152f5 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 62  , it.** is disab
152f6 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c 74  led. The default
152f7 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 61   value for the a
152f8 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 65  uto-vacuum prope
152f9 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 72  rty is .** deter
152fa 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c  mined by the SQL
152fb 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
152fc 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f  VACUUM macro..*/
152fd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
152fe 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
152ff 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74  SetAutoVacuum(Bt
15300 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f  ree *p, int auto
15301 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 20  Vacuum){.#ifdef 
15302 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
15303 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20  VACUUM.  return 
15304 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
15305 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 65  .#else.  BtShare
15306 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
15307 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
15308 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d  TE_OK;.  u8 av =
15309 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b   (u8)autoVacuum;
1530a 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
1530b 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20  Enter(p);.  if( 
1530c 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78  pBt->pageSizeFix
1530d 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29 21  ed && (av ?1:0)!
1530e 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  =pBt->autoVacuum
1530f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
15310 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
15311 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e  }else{.    pBt->
15312 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 20  autoVacuum = av 
15313 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69  ?1:0;.    pBt->i
15314 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d  ncrVacuum = av==
15315 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71  2 ?1:0;.  }.  sq
15316 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15317 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
15318 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
15319 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75   Return the valu
1531a 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d 76  e of the 'auto-v
1531b 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 2e  acuum' property.
1531c 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   If auto-vacuum 
1531d 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31  is .** enabled 1
1531e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
1531f 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51  herwise 0..*/.SQ
15320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15321 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
15322 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65  AutoVacuum(Btree
15323 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c   *p){.#ifdef SQL
15324 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
15325 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 52  UUM.  return BTR
15326 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f  EE_AUTOVACUUM_NO
15327 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20  NE;.#else.  int 
15328 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  rc;.  sqlite3Btr
15329 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63  eeEnter(p);.  rc
1532a 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 42   = (.    (!p->pB
1532b 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42  t->autoVacuum)?B
1532c 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
1532d 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70  NONE:.    (!p->p
1532e 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f  Bt->incrVacuum)?
1532f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
15330 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45  _FULL:.    BTREE
15331 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52  _AUTOVACUUM_INCR
15332 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  .  );.  sqlite3B
15333 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
15334 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69  return rc;.#endi
15335 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  f.}.../*.** Get 
15336 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70  a reference to p
15337 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 74  Page1 of the dat
15338 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69  abase file.  Thi
15339 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61  s will.** also a
1533a 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f 63  cquire a readloc
1533b 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a  k on that file..
1533c 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  **.** SQLITE_OK 
1533d 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73  is returned on s
1533e 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 20  uccess.  If the 
1533f 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a  file is not a.**
15340 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74   well-formed dat
15341 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e  abase file, then
15342 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
15343 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
15344 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
15345 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 64  eturned if the d
15346 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65  atabase is locke
15347 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  d.  SQLITE_NOMEM
15348 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20  .** is returned 
15349 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66  if we run out of
1534a 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61   memory. .*/.sta
1534b 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65  tic int lockBtre
1534c 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  e(BtShared *pBt)
1534d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
1534e 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 20  mPage *pPage1;. 
1534f 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61   int nPage;..  a
15350 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15351 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
15352 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
15353 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d  t( pBt->pPage1==
15354 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  0 );.  rc = sqli
15355 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
15356 70 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c  pBt, 1, &pPage1,
15357 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   0);.  if( rc!=S
15358 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
15359 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73  n rc;..  /* Do s
1535a 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 74 6f 20  ome checking to 
1535b 68 65 6c 70 20 69 6e 73 75 72 65 20 74 68 65 20  help insure the 
1535c 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 64 20 72  file we opened r
1535d 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a 20 61 20  eally is.  ** a 
1535e 76 61 6c 69 64 20 64 61 74 61 62 61 73 65 20 66  valid database f
1535f 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 72 63 20  ile. .  */.  rc 
15360 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  = sqlite3PagerPa
15361 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  gecount(pBt->pPa
15362 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20  ger, &nPage);.  
15363 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15364 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 61  K ){.    goto pa
15365 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b  ge1_init_failed;
15366 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 50 61  .  }else if( nPa
15367 67 65 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  ge>0 ){.    int 
15368 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 69 6e  pageSize;.    in
15369 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  t usableSize;.  
1536a 20 20 75 38 20 2a 70 61 67 65 31 20 3d 20 70 50    u8 *page1 = pP
1536b 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20 20  age1->aData;.   
1536c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 54   rc = SQLITE_NOT
1536d 41 44 42 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d  ADB;.    if( mem
1536e 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d 61 67 69  cmp(page1, zMagi
1536f 63 48 65 61 64 65 72 2c 20 31 36 29 21 3d 30 20  cHeader, 16)!=0 
15370 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61  ){.      goto pa
15371 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b  ge1_init_failed;
15372 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
15373 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b 0a 20 20  age1[18]>1 ){.  
15374 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c      pBt->readOnl
15375 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  y = 1;.    }.   
15376 20 69 66 28 20 70 61 67 65 31 5b 31 39 5d 3e 31   if( page1[19]>1
15377 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
15378 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
15379 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1537a 54 68 65 20 6d 61 78 69 6d 75 6d 20 65 6d 62 65  The maximum embe
1537b 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75  dded fraction mu
1537c 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 32 35  st be exactly 25
1537d 25 2e 20 20 41 6e 64 20 74 68 65 20 6d 69 6e 69  %.  And the mini
1537e 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d 62 65 64  mum.    ** embed
1537f 64 65 64 20 66 72 61 63 74 69 6f 6e 20 6d 75 73  ded fraction mus
15380 74 20 62 65 20 31 32 2e 35 25 20 66 6f 72 20 62  t be 12.5% for b
15381 6f 74 68 20 6c 65 61 66 2d 64 61 74 61 20 61 6e  oth leaf-data an
15382 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 2e  d non-leaf-data.
15383 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 72 69 67  .    ** The orig
15384 69 6e 61 6c 20 64 65 73 69 67 6e 20 61 6c 6c 6f  inal design allo
15385 77 65 64 20 74 68 65 73 65 20 61 6d 6f 75 6e 74  wed these amount
15386 73 20 74 6f 20 76 61 72 79 2c 20 62 75 74 20 61  s to vary, but a
15387 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76 65 72 73  s of.    ** vers
15388 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 20 72 65  ion 3.6.0, we re
15389 71 75 69 72 65 20 74 68 65 6d 20 74 6f 20 62 65  quire them to be
1538a 20 66 69 78 65 64 2e 0a 20 20 20 20 2a 2f 0a 20   fixed..    */. 
1538b 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 26 70     if( memcmp(&p
1538c 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 30 30 5c  age1[21], "\100\
1538d 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 30 20 29  040\040",3)!=0 )
1538e 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67  {.      goto pag
1538f 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a  e1_init_failed;.
15390 20 20 20 20 7d 0a 20 20 20 20 70 61 67 65 53 69      }.    pageSi
15391 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 70  ze = get2byte(&p
15392 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20 20 20 69  age1[16]);.    i
15393 66 28 20 28 28 70 61 67 65 53 69 7a 65 2d 31 29  f( ((pageSize-1)
15394 26 70 61 67 65 53 69 7a 65 29 21 3d 30 20 7c 7c  &pageSize)!=0 ||
15395 20 70 61 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c   pageSize<512 ||
15396 0a 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45  .        (SQLITE
15397 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 3c 33  _MAX_PAGE_SIZE<3
15398 32 37 36 38 20 26 26 20 70 61 67 65 53 69 7a 65  2768 && pageSize
15399 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  >SQLITE_MAX_PAGE
1539a 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b 0a 20 20  _SIZE).    ){.  
1539b 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69      goto page1_i
1539c 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20  nit_failed;.    
1539d 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  }.    assert( (p
1539e 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 20  ageSize & 7)==0 
1539f 29 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a  );.    usableSiz
153a0 65 20 3d 20 70 61 67 65 53 69 7a 65 20 2d 20 70  e = pageSize - p
153a1 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 20 69 66  age1[20];.    if
153a2 28 20 70 61 67 65 53 69 7a 65 21 3d 70 42 74 2d  ( pageSize!=pBt-
153a3 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20  >pageSize ){.   
153a4 20 20 20 2f 2a 20 41 66 74 65 72 20 72 65 61 64     /* After read
153a5 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61  ing the first pa
153a6 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
153a7 73 65 20 61 73 73 75 6d 69 6e 67 20 61 20 70 61  se assuming a pa
153a8 67 65 20 73 69 7a 65 0a 20 20 20 20 20 20 2a 2a  ge size.      **
153a9 20 6f 66 20 42 74 53 68 61 72 65 64 2e 70 61 67   of BtShared.pag
153aa 65 53 69 7a 65 2c 20 77 65 20 68 61 76 65 20 64  eSize, we have d
153ab 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 74  iscovered that t
153ac 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 0a  he page-size is.
153ad 20 20 20 20 20 20 2a 2a 20 61 63 74 75 61 6c 6c        ** actuall
153ae 79 20 70 61 67 65 53 69 7a 65 2e 20 55 6e 6c 6f  y pageSize. Unlo
153af 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  ck the database,
153b0 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 50 61 67   leave pBt->pPag
153b1 65 31 20 61 74 0a 20 20 20 20 20 20 2a 2a 20 7a  e1 at.      ** z
153b2 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20 53  ero and return S
153b3 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 20 63 61  QLITE_OK. The ca
153b4 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c 6c 20 74  ller will call t
153b5 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20  his function.   
153b6 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 69 74 68     ** again with
153b7 20 74 68 65 20 63 6f 72 72 65 63 74 20 70 61 67   the correct pag
153b8 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 20 2a 2f  e-size..      */
153b9 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
153ba 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20  ge(pPage1);.    
153bb 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a    pBt->usableSiz
153bc 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53  e = (u16)usableS
153bd 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e  ize;.      pBt->
153be 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29  pageSize = (u16)
153bf 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20  pageSize;.      
153c0 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42  freeTempSpace(pB
153c1 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  t);.      rc = s
153c2 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
153c3 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67  gesize(pBt->pPag
153c4 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69  er, &pBt->pageSi
153c5 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ze);.      if( r
153c6 63 20 29 20 67 6f 74 6f 20 70 61 67 65 31 5f 69  c ) goto page1_i
153c7 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20  nit_failed;.    
153c8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
153c9 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  OK;.    }.    if
153ca 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 35 30 30  ( usableSize<500
153cb 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
153cc 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
153cd 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d  ;.    }.    pBt-
153ce 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36  >pageSize = (u16
153cf 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 70  )pageSize;.    p
153d0 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d  Bt->usableSize =
153d1 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a 65   (u16)usableSize
153d2 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
153d3 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
153d4 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61  .    pBt->autoVa
153d5 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65  cuum = (get4byte
153d6 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a 34  (&page1[36 + 4*4
153d7 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42 74  ])?1:0);.    pBt
153d8 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28  ->incrVacuum = (
153d9 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31 5b  get4byte(&page1[
153da 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b  36 + 7*4])?1:0);
153db 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f  .#endif.  }..  /
153dc 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74 68  * maxLocal is th
153dd 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74  e maximum amount
153de 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20 73   of payload to s
153df 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72  tore locally for
153e0 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d  .  ** a cell.  M
153e1 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 73  ake sure it is s
153e2 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74  mall enough so t
153e3 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69 6e  hat at least min
153e4 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c  Fanout.  ** cell
153e5 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f  s can will fit o
153e6 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65 20  n one page.  We 
153e7 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74 65  assume a 10-byte
153e8 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20 20   page header..  
153e9 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 20 70  ** Besides the p
153ea 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c  ayload, the cell
153eb 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a   must store:.  *
153ec 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f 69  *     2-byte poi
153ed 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c  nter to the cell
153ee 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65  .  **     4-byte
153ef 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20   child pointer. 
153f0 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20 6e   **     9-byte n
153f1 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20  Key value.  **  
153f2 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61 20     4-byte nData 
153f3 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34  value.  **     4
153f4 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20 70  -byte overflow p
153f5 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a  age pointer.  **
153f6 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69   So a cell consi
153f7 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65 20  sts of a 2-byte 
153f8 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 65 72  poiner, a header
153f9 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75 63   which is as muc
153fa 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79 74  h as.  ** 17 byt
153fb 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20  es long, 0 to N 
153fc 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 64  bytes of payload
153fd 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61  , and an optiona
153fe 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c 6f  l 4 byte overflo
153ff 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e  w.  ** page poin
15400 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d  ter..  */.  pBt-
15401 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74  >maxLocal = (pBt
15402 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29  ->usableSize-12)
15403 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20  *64/255 - 23;.  
15404 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20  pBt->minLocal = 
15405 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
15406 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33  -12)*32/255 - 23
15407 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66  ;.  pBt->maxLeaf
15408 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
15409 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e  ze - 35;.  pBt->
1540a 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e  minLeaf = (pBt->
1540b 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33  usableSize-12)*3
1540c 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 61 73  2/255 - 23;.  as
1540d 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65  sert( pBt->maxLe
1540e 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45  af + 23 <= MX_CE
1540f 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a  LL_SIZE(pBt) );.
15410 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20    pBt->pPage1 = 
15411 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 72 6e  pPage1;.  return
15412 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67   SQLITE_OK;..pag
15413 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a  e1_init_failed:.
15414 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
15415 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50  age1);.  pBt->pP
15416 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 74 75  age1 = 0;.  retu
15417 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
15418 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72  This routine wor
15419 6b 73 20 6c 69 6b 65 20 6c 6f 63 6b 42 74 72 65  ks like lockBtre
1541a 65 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20  e() except that 
1541b 69 74 20 61 6c 73 6f 20 69 6e 76 6f 6b 65 73 20  it also invokes 
1541c 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c  the.** busy call
1541d 62 61 63 6b 20 69 66 20 74 68 65 72 65 20 69 73  back if there is
1541e 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e   lock contention
1541f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15420 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52 65 74  lockBtreeWithRet
15421 72 79 28 42 74 72 65 65 20 2a 70 52 65 66 29 7b  ry(Btree *pRef){
15422 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
15423 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74  TE_OK;..  assert
15424 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
15425 6c 64 73 4d 75 74 65 78 28 70 52 65 66 29 20 29  ldsMutex(pRef) )
15426 3b 0a 20 20 69 66 28 20 70 52 65 66 2d 3e 69 6e  ;.  if( pRef->in
15427 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e  Trans==TRANS_NON
15428 45 20 29 7b 0a 20 20 20 20 75 38 20 69 6e 54 72  E ){.    u8 inTr
15429 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 52 65 66  ansaction = pRef
1542a 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ->pBt->inTransac
1542b 74 69 6f 6e 3b 0a 20 20 20 20 62 74 72 65 65 49  tion;.    btreeI
1542c 6e 74 65 67 72 69 74 79 28 70 52 65 66 29 3b 0a  ntegrity(pRef);.
1542d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1542e 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28  BtreeBeginTrans(
1542f 70 52 65 66 2c 20 30 29 3b 0a 20 20 20 20 70 52  pRef, 0);.    pR
15430 65 66 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73  ef->pBt->inTrans
15431 61 63 74 69 6f 6e 20 3d 20 69 6e 54 72 61 6e 73  action = inTrans
15432 61 63 74 69 6f 6e 3b 0a 20 20 20 20 70 52 65 66  action;.    pRef
15433 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e  ->inTrans = TRAN
15434 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 69 66 28 20  S_NONE;.    if( 
15435 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
15436 0a 20 20 20 20 20 20 70 52 65 66 2d 3e 70 42 74  .      pRef->pBt
15437 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d  ->nTransaction--
15438 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 74 72 65  ;.    }.    btre
15439 65 49 6e 74 65 67 72 69 74 79 28 70 52 65 66 29  eIntegrity(pRef)
1543a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1543b 63 3b 0a 7d 0a 20 20 20 20 20 20 20 0a 0a 2f 2a  c;.}.       ../*
1543c 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65  .** If there are
1543d 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
1543e 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65 20 61  cursors and we a
1543f 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69  re not in the mi
15440 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61  ddle.** of a tra
15441 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74 68 65  nsaction but the
15442 72 65 20 69 73 20 61 20 72 65 61 64 20 6c 6f 63  re is a read loc
15443 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
15444 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20  e, then.** this 
15445 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73 20 74  routine unrefs t
15446 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66  he first page of
15447 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
15448 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73  le which .** has
15449 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 72   the effect of r
1544a 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72 65 61  eleasing the rea
1544b 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  d lock..**.** If
1544c 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f   there are any o
1544d 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 73 6f  utstanding curso
1544e 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  rs, this routine
1544f 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
15450 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ** If there is a
15451 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20   transaction in 
15452 70 72 6f 67 72 65 73 73 2c 20 74 68 69 73 20 72  progress, this r
15453 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
15454 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  p..*/.static voi
15455 64 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55  d unlockBtreeIfU
15456 6e 75 73 65 64 28 42 74 53 68 61 72 65 64 20 2a  nused(BtShared *
15457 70 42 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20  pBt){.  assert( 
15458 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
15459 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
1545a 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54  ;.  if( pBt->inT
1545b 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
1545c 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 70  S_NONE && pBt->p
1545d 43 75 72 73 6f 72 3d 3d 30 20 26 26 20 70 42 74  Cursor==0 && pBt
1545e 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 7b 0a 20  ->pPage1!=0 ){. 
1545f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61     if( sqlite3Pa
15460 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d  gerRefcount(pBt-
15461 3e 70 50 61 67 65 72 29 3e 3d 31 20 29 7b 0a 20  >pPager)>=1 ){. 
15462 20 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74       assert( pBt
15463 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20  ->pPage1->aData 
15464 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  );.      release
15465 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 31  Page(pBt->pPage1
15466 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  );.    }.    pBt
15467 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20  ->pPage1 = 0;.  
15468 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  }.}../*.** Creat
15469 65 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65  e a new database
1546a 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67   by initializing
1546b 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
1546c 6f 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 2e 0a  of the.** file..
1546d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65  */.static int ne
1546e 77 44 61 74 61 62 61 73 65 28 42 74 53 68 61 72  wDatabase(BtShar
1546f 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50  ed *pBt){.  MemP
15470 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69  age *pP1;.  unsi
15471 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b  gned char *data;
15472 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
15473 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72   nPage;..  asser
15474 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15475 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
15476 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ) );.  rc = sqli
15477 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
15478 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26  t(pBt->pPager, &
15479 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  nPage);.  if( rc
1547a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e  !=SQLITE_OK || n
1547b 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 72 65  Page>0 ){.    re
1547c 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70  turn rc;.  }.  p
1547d 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31  P1 = pBt->pPage1
1547e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 31 21  ;.  assert( pP1!
1547f 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70  =0 );.  data = p
15480 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20  P1->aData;.  rc 
15481 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
15482 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61 67 65  ite(pP1->pDbPage
15483 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
15484 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 70  turn rc;.  memcp
15485 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 65  y(data, zMagicHe
15486 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d 61  ader, sizeof(zMa
15487 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20 20 61  gicHeader));.  a
15488 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 7a 4d  ssert( sizeof(zM
15489 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31 36 20  agicHeader)==16 
1548a 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64  );.  put2byte(&d
1548b 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70 61  ata[16], pBt->pa
1548c 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b  geSize);.  data[
1548d 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b  18] = 1;.  data[
1548e 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 72  19] = 1;.  asser
1548f 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  t( pBt->usableSi
15490 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a  ze<=pBt->pageSiz
15491 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c 65  e && pBt->usable
15492 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e 70  Size+255>=pBt->p
15493 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61  ageSize);.  data
15494 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 2d  [20] = (u8)(pBt-
15495 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d  >pageSize - pBt-
15496 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 20  >usableSize);.  
15497 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a 20  data[21] = 64;. 
15498 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b 0a   data[22] = 32;.
15499 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 3b    data[23] = 32;
1549a 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b  .  memset(&data[
1549b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 3b  24], 0, 100-24);
1549c 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 2c  .  zeroPage(pP1,
1549d 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f   PTF_INTKEY|PTF_
1549e 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 54  LEAF|PTF_LEAFDAT
1549f 41 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67 65  A );.  pBt->page
154a0 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23  SizeFixed = 1;.#
154a1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
154a2 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
154a3 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74  assert( pBt->aut
154a4 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42  oVacuum==1 || pB
154a5 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 30  t->autoVacuum==0
154a6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42   );.  assert( pB
154a7 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 31  t->incrVacuum==1
154a8 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63   || pBt->incrVac
154a9 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 34  uum==0 );.  put4
154aa 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20  byte(&data[36 + 
154ab 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 56  4*4], pBt->autoV
154ac 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 79  acuum);.  put4by
154ad 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37 2a  te(&data[36 + 7*
154ae 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 63  4], pBt->incrVac
154af 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72  uum);.#endif.  r
154b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
154b1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
154b2 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 77  t to start a new
154b3 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20   transaction. A 
154b4 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
154b5 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64 20  n.** is started 
154b6 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  if the second ar
154b7 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65 72  gument is nonzer
154b8 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20 72  o, otherwise a r
154b9 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63 74  ead-.** transact
154ba 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65 63  ion.  If the sec
154bb 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
154bc 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65 78  2 or more and ex
154bd 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e 73  clusive.** trans
154be 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65  action is starte
154bf 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20  d, meaning that 
154c0 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  no other process
154c1 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74   is allowed.** t
154c2 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74  o access the dat
154c3 61 62 61 73 65 2e 20 20 41 20 70 72 65 65 78 69  abase.  A preexi
154c4 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f  sting transactio
154c5 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a 20  n may not be.** 
154c6 75 70 67 72 61 64 65 64 20 74 6f 20 65 78 63 6c  upgraded to excl
154c7 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e 67  usive by calling
154c8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 20   this routine a 
154c9 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74 68  second time - th
154ca 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74 79  e.** exclusivity
154cb 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b 73   flag only works
154cc 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e 73   for a new trans
154cd 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20  action..**.** A 
154ce 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
154cf 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65  n must be starte
154d0 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74  d before attempt
154d1 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61 6e  ing any .** chan
154d2 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62  ges to the datab
154d3 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68  ase.  None of th
154d4 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
154d5 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77 6f  ines .** will wo
154d6 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61 6e  rk unless a tran
154d7 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
154d8 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a 20  ed first:.**.** 
154d9 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
154da 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a 2a  eCreateTable().*
154db 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  *      sqlite3Bt
154dc 72 65 65 43 72 65 61 74 65 49 6e 64 65 78 28 29  reeCreateIndex()
154dd 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
154de 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28  BtreeClearTable(
154df 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  ).**      sqlite
154e0 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28  3BtreeDropTable(
154e1 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  ).**      sqlite
154e2 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a 2a  3BtreeInsert().*
154e3 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  *      sqlite3Bt
154e4 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20 20  reeDelete().**  
154e5 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
154e6 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a 0a  UpdateMeta().**.
154e7 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61 6c  ** If an initial
154e8 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71 75   attempt to acqu
154e9 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61 69  ire the lock fai
154ea 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c 6f  ls because of lo
154eb 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a 2a  ck contention.**
154ec 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   and the databas
154ed 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  e was previously
154ee 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20   unlocked, then 
154ef 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  invoke the busy 
154f0 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74 68  handler.** if th
154f1 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75 74  ere is one.  But
154f2 20 69 66 20 74 68 65 72 65 20 77 61 73 20 70 72   if there was pr
154f3 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64 2d  eviously a read-
154f4 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20  lock, do not.** 
154f5 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  invoke the busy 
154f6 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20 72  handler - just r
154f7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
154f8 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59 20  Y.  SQLITE_BUSY 
154f9 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  is .** returned 
154fa 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6c  when there is al
154fb 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f 63  ready a read-loc
154fc 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76  k in order to av
154fd 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a  oid a deadlock..
154fe 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68  **.** Suppose th
154ff 65 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f 63  ere are two proc
15500 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20 20  esses A and B.  
15501 41 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63  A has a read loc
15502 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20 61  k and B has.** a
15503 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20   reserved lock. 
15504 20 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d   B tries to prom
15505 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76 65  ote to exclusive
15506 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 20   but is blocked 
15507 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41 27  because.** of A'
15508 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41 20  s read lock.  A 
15509 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65  tries to promote
1550a 20 74 6f 20 72 65 73 65 72 76 65 64 20 62 75 74   to reserved but
1550b 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 42   is blocked by B
1550c 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 20  ..** One or the 
1550d 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f  other of the two
1550e 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74 20   processes must 
1550f 67 69 76 65 20 77 61 79 20 6f 72 20 74 68 65 72  give way or ther
15510 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20 70  e can be.** no p
15511 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 74  rogress.  By ret
15512 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 55  urning SQLITE_BU
15513 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f 6b  SY and not invok
15514 69 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 6c  ing the busy cal
15515 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41 20  lback.** when A 
15516 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65  already has a re
15517 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 6f  ad lock, we enco
15518 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 20  urage A to give 
15519 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a 20  up and let B.** 
1551a 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c 49  proceed..*/.SQLI
1551b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1551c 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e  qlite3BtreeBegin
1551d 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c 20  Trans(Btree *p, 
1551e 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20 73  int wrflag){.  s
1551f 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20 3d  qlite3 *pBlock =
15520 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a   0;.  BtShared *
15521 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
15522 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
15523 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  OK;..  sqlite3Bt
15524 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 62  reeEnter(p);.  b
15525 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29  treeIntegrity(p)
15526 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62  ;..  /* If the b
15527 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 20  tree is already 
15528 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73  in a write-trans
15529 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 20  action, or it.  
1552a 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ** is already in
1552b 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74   a read-transact
1552c 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d 74  ion and a read-t
1552d 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20  ransaction.  ** 
1552e 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 68  is requested, th
1552f 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20  is is a no-op.. 
15530 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54   */.  if( p->inT
15531 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
15532 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e 73  E || (p->inTrans
15533 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 20  ==TRANS_READ && 
15534 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20 20  !wrflag) ){.    
15535 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e  goto trans_begun
15536 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74  ;.  }..  /* Writ
15537 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61  e transactions a
15538 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  re not possible 
15539 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 64  on a read-only d
1553a 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 28  atabase */.  if(
1553b 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26   pBt->readOnly &
1553c 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20  & wrflag ){.    
1553d 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44  rc = SQLITE_READ
1553e 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74  ONLY;.    goto t
1553f 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a  rans_begun;.  }.
15540 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15541 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
15542 45 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 65  E.  /* If anothe
15543 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  r database handl
15544 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f 70  e has already op
15545 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 61  ened a write tra
15546 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 6f  nsaction .  ** o
15547 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 74  n this shared-bt
15548 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e  ree structure an
15549 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 65  d a second write
1554a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a   transaction is.
1554b 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c 20    ** requested, 
1554c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
1554d 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  CKED..  */.  if(
1554e 20 28 77 72 66 6c 61 67 20 26 26 20 70 42 74 2d   (wrflag && pBt-
1554f 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
15550 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c 20  TRANS_WRITE) || 
15551 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 29  pBt->isPending )
15552 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 70  {.    pBlock = p
15553 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 3b  Bt->pWriter->db;
15554 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 66  .  }else if( wrf
15555 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 4c  lag>1 ){.    BtL
15556 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 20  ock *pIter;.    
15557 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70  for(pIter=pBt->p
15558 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74  Lock; pIter; pIt
15559 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29  er=pIter->pNext)
1555a 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  {.      if( pIte
1555b 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b 0a  r->pBtree!=p ){.
1555c 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 3d          pBlock =
1555d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e   pIter->pBtree->
1555e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  db;.        brea
1555f 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
15560 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f 63  .  }.  if( pBloc
15561 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  k ){.    sqlite3
15562 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65  ConnectionBlocke
15563 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b 29  d(p->db, pBlock)
15564 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
15565 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
15566 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 74  ACHE;.    goto t
15567 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a  rans_begun;.  }.
15568 23 65 6e 64 69 66 0a 0a 20 20 64 6f 20 7b 0a 20  #endif..  do {. 
15569 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b 42     /* Call lockB
1556a 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 69 74  tree() until eit
1556b 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 31 20  her pBt->pPage1 
1556c 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 72 0a  is populated or.
1556d 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 65      ** lockBtree
1556e 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 74  () returns somet
1556f 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20  hing other than 
15570 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b 42  SQLITE_OK. lockB
15571 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 6d 61  tree().    ** ma
15572 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  y return SQLITE_
15573 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 42 74  OK but leave pBt
15574 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 6f 20  ->pPage1 set to 
15575 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 20 2a  0 if after.    *
15576 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 31  * reading page 1
15577 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 74 68   it discovers th
15578 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  at the page-size
15579 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1557a 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 69 73   .    ** file is
1557b 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 53 69   not pBt->pageSi
1557c 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ze. In this case
1557d 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 69 6c   lockBtree() wil
1557e 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a 2a 20  l update.    ** 
1557f 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f  pBt->pageSize to
15580 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f   the page-size o
15581 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69  f the file on di
15582 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77  sk..    */.    w
15583 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 65  hile( pBt->pPage
15584 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f 4f  1==0 && SQLITE_O
15585 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72  K==(rc = lockBtr
15586 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 20  ee(pBt)) );..   
15587 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
15588 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a  OK && wrflag ){.
15589 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 72        if( pBt->r
1558a 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20  eadOnly ){.     
1558b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52     rc = SQLITE_R
1558c 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d  EADONLY;.      }
1558d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
1558e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42   = sqlite3PagerB
1558f 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 65 72  egin(pBt->pPager
15590 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69 74 65  ,wrflag>1,sqlite
15591 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 2d  3TempInMemory(p-
15592 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20 20 69  >db));.        i
15593 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15594 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
15595 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70   = newDatabase(p
15596 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  Bt);.        }. 
15597 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
15598 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
15599 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75  TE_OK ){.      u
1559a 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
1559b 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20  ed(pBt);.    }. 
1559c 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c   }while( rc==SQL
1559d 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74 2d  ITE_BUSY && pBt-
1559e 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d  >inTransaction==
1559f 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20  TRANS_NONE &&.  
155a0 20 20 20 20 20 20 20 20 62 74 72 65 65 49 6e 76          btreeInv
155a1 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 70  okeBusyHandler(p
155a2 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 63  Bt) );..  if( rc
155a3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
155a4 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e     if( p->inTran
155a5 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b  s==TRANS_NONE ){
155a6 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61  .      pBt->nTra
155a7 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 20 20 20 20  nsaction++;.    
155a8 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73  }.    p->inTrans
155a9 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 4e 53   = (wrflag?TRANS
155aa 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 45 41  _WRITE:TRANS_REA
155ab 44 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  D);.    if( p->i
155ac 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72  nTrans>pBt->inTr
155ad 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20  ansaction ){.   
155ae 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
155af 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61  ction = p->inTra
155b0 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ns;.    }.#ifnde
155b1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
155b2 41 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 69  ARED_CACHE.    i
155b3 66 28 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  f( wrflag ){.   
155b4 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d     assert( !pBt-
155b5 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20  >pWriter );.    
155b6 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d    pBt->pWriter =
155b7 20 70 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69   p;.      pBt->i
155b8 73 45 78 63 6c 75 73 69 76 65 20 3d 20 28 75 38  sExclusive = (u8
155b9 29 28 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20  )(wrflag>1);.   
155ba 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a   }.#endif.  }...
155bb 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69  trans_begun:.  i
155bc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
155bd 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20   && wrflag ){.  
155be 20 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d    /* This call m
155bf 61 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 74  akes sure that t
155c0 68 65 20 70 61 67 65 72 20 68 61 73 20 74 68 65  he pager has the
155c1 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20   correct number 
155c2 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73  of.    ** open s
155c3 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68  avepoints. If th
155c4 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
155c5 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68  er is greater th
155c6 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20  an 0 and.    ** 
155c7 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  the sub-journal 
155c8 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f  is not already o
155c9 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c  pen, then it wil
155ca 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65  l be opened here
155cb 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
155cc 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70  = sqlite3PagerOp
155cd 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d  enSavepoint(pBt-
155ce 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e  >pPager, p->db->
155cf 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d  nSavepoint);.  }
155d0 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  ..  btreeIntegri
155d1 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  ty(p);.  sqlite3
155d2 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
155d3 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
155d4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
155d5 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f  IT_AUTOVACUUM../
155d6 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69  *.** Set the poi
155d7 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
155d8 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65   for all childre
155d9 6e 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e  n of page pPage.
155da 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61   Also, if.** pPa
155db 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c  ge contains cell
155dc 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20  s that point to 
155dd 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20  overflow pages, 
155de 73 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a  set the pointer.
155df 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66  ** map entries f
155e0 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
155e1 70 61 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a  pages as well..*
155e2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74  /.static int set
155e3 43 68 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d  ChildPtrmaps(Mem
155e4 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20  Page *pPage){.  
155e5 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
155e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
155e7 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61     /* Counter va
155e8 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  riable */.  int 
155e9 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  nCell;          
155ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
155eb 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  * Number of cell
155ec 73 20 69 6e 20 70 61 67 65 20 70 50 61 67 65 20  s in page pPage 
155ed 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
155ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
155ef 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
155f0 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68  n code */.  BtSh
155f1 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67  ared *pBt = pPag
155f2 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49  e->pBt;.  u8 isI
155f3 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d  nitOrig = pPage-
155f4 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20  >isInit;.  Pgno 
155f5 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67  pgno = pPage->pg
155f6 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  no;..  assert( s
155f7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
155f8 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
155f9 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73  tex) );.  rc = s
155fa 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
155fb 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 69 66  age(pPage);.  if
155fc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
155fd 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 74 5f  ){.    goto set_
155fe 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75  child_ptrmaps_ou
155ff 74 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d  t;.  }.  nCell =
15600 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a   pPage->nCell;..
15601 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65    for(i=0; i<nCe
15602 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38  ll; i++){.    u8
15603 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65   *pCell = findCe
15604 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20  ll(pPage, i);.. 
15605 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
15606 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c 20  tOvflPtr(pPage, 
15607 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  pCell);.    if( 
15608 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
15609 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 65 74 5f  .      goto set_
1560a 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75  child_ptrmaps_ou
1560b 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  t;.    }..    if
1560c 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
1560d 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69  {.      Pgno chi
1560e 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74  ldPgno = get4byt
1560f 65 28 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20  e(pCell);.      
15610 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70  rc = ptrmapPut(p
15611 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50  Bt, childPgno, P
15612 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e  TRMAP_BTREE, pgn
15613 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  o);.      if( rc
15614 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
15615 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 72  to set_child_ptr
15616 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  maps_out;.    }.
15617 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67    }..  if( !pPag
15618 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 50  e->leaf ){.    P
15619 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20  gno childPgno = 
1561a 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
1561b 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
1561c 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20  rOffset+8]);.   
1561d 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28   rc = ptrmapPut(
1561e 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20  pBt, childPgno, 
1561f 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67  PTRMAP_BTREE, pg
15620 6e 6f 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 68  no);.  }..set_ch
15621 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3a  ild_ptrmaps_out:
15622 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  .  pPage->isInit
15623 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20   = isInitOrig;. 
15624 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
15625 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 6f  *.** Somewhere o
15626 6e 20 70 50 61 67 65 2c 20 77 68 69 63 68 20 69  n pPage, which i
15627 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  s guaranteed to 
15628 62 65 20 61 20 62 74 72 65 65 20 70 61 67 65 2c  be a btree page,
15629 20 6e 6f 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77   not an overflow
1562a 0a 2a 2a 20 70 61 67 65 2c 20 69 73 20 61 20 70  .** page, is a p
1562b 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69  ointer to page i
1562c 46 72 6f 6d 2e 20 4d 6f 64 69 66 79 20 74 68 69  From. Modify thi
1562d 73 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61  s pointer so tha
1562e 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a  t it points to.*
1562f 2a 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72  * iTo. Parameter
15630 20 65 54 79 70 65 20 64 65 73 63 72 69 62 65 73   eType describes
15631 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69   the type of poi
15632 6e 74 65 72 20 74 6f 20 62 65 20 6d 6f 64 69 66  nter to be modif
15633 69 65 64 2c 20 61 73 20 0a 2a 2a 20 66 6f 6c 6c  ied, as .** foll
15634 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  ows:.**.** PTRMA
15635 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50 61  P_BTREE:     pPa
15636 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61  ge is a btree-pa
15637 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  ge. The pointer 
15638 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c  points at a chil
15639 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  d .**           
1563a 20 20 20 20 20 20 20 20 70 61 67 65 20 6f 66 20          page of 
1563b 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52  pPage..**.** PTR
1563c 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70  MAP_OVERFLOW1: p
1563d 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d  Page is a btree-
1563e 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65  page. The pointe
1563f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f  r points at an o
15640 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20  verflow.**      
15641 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
15642 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  e pointed to by 
15643 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73  one of the cells
15644 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a   on pPage..**.**
15645 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
15646 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f  2: pPage is an o
15647 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68  verflow-page. Th
15648 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73  e pointer points
15649 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20   at the next.** 
1564a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1564b 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20    overflow page 
1564c 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a  in the list..*/.
1564d 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66  static int modif
1564e 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d  yPagePointer(Mem
1564f 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e  Page *pPage, Pgn
15650 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54  o iFrom, Pgno iT
15651 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20  o, u8 eType){.  
15652 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15653 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
15654 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
15655 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15656 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
15657 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
15658 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70  e) );.  if( eTyp
15659 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
1565a 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  OW2 ){.    /* Th
1565b 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77  e pointer is alw
1565c 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34 20  ays the first 4 
1565d 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67  bytes of the pag
1565e 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20  e in this case. 
1565f 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34   */.    if( get4
15660 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74  byte(pPage->aDat
15661 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  a)!=iFrom ){.   
15662 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15663 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
15664 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74     }.    put4byt
15665 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20  e(pPage->aData, 
15666 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  iTo);.  }else{. 
15667 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67     u8 isInitOrig
15668 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74   = pPage->isInit
15669 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
1566a 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20   int nCell;..   
1566b 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69   sqlite3BtreeIni
1566c 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  tPage(pPage);.  
1566d 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d    nCell = pPage-
1566e 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72  >nCell;..    for
1566f 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69  (i=0; i<nCell; i
15670 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70  ++){.      u8 *p
15671 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
15672 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20  pPage, i);.     
15673 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d   if( eType==PTRM
15674 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a  AP_OVERFLOW1 ){.
15675 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f          CellInfo
15676 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 73   info;.        s
15677 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
15678 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
15679 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
1567a 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69        if( info.i
1567b 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
1567c 20 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d        if( iFrom=
1567d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c  =get4byte(&pCell
1567e 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
1567f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
15680 20 70 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c   put4byte(&pCell
15681 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
15682 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20  , iTo);.        
15683 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
15684 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
15685 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
15686 20 20 20 20 20 20 69 66 28 20 67 65 74 34 62 79        if( get4by
15687 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d  te(pCell)==iFrom
15688 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75   ){.          pu
15689 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54  t4byte(pCell, iT
1568a 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  o);.          br
1568b 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
1568c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
1568d 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c      if( i==nCell
1568e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54   ){.      if( eT
1568f 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45  ype!=PTRMAP_BTRE
15690 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20  E || .          
15691 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
15692 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
15693 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72  rOffset+8])!=iFr
15694 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  om ){.        re
15695 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15696 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
15697 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  }.      put4byte
15698 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
15699 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
1569a 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a  8], iTo);.    }.
1569b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e  .    pPage->isIn
1569c 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b  it = isInitOrig;
1569d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
1569e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
1569f 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e  ** Move the open
156a0 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70   database page p
156a1 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69  DbPage to locati
156a2 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 6e 20  on iFreePage in 
156a3 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  the .** database
156a4 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 72 65  . The pDbPage re
156a5 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20  ference remains 
156a6 76 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  valid..*/.static
156a7 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67   int relocatePag
156a8 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
156a9 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Bt,           /*
156aa 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50   Btree */.  MemP
156ab 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20  age *pDbPage,   
156ac 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67       /* Open pag
156ad 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75  e to move */.  u
156ae 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20  8 eType,        
156af 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
156b0 65 72 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e  er map 'type' en
156b1 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20  try for pDbPage 
156b2 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61  */.  Pgno iPtrPa
156b3 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ge,           /*
156b4 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61   Pointer map 'pa
156b5 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72  ge-no' entry for
156b6 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67   pDbPage */.  Pg
156b7 6e 6f 20 69 46 72 65 65 50 61 67 65 2c 20 20 20  no iFreePage,   
156b8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f         /* The lo
156b9 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70  cation to move p
156ba 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69  DbPage to */.  i
156bb 6e 74 20 69 73 43 6f 6d 6d 69 74 0a 29 7b 0a 20  nt isCommit.){. 
156bc 20 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61   MemPage *pPtrPa
156bd 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67  ge;   /* The pag
156be 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
156bf 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62  a pointer to pDb
156c0 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  Page */.  Pgno i
156c1 44 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65  DbPage = pDbPage
156c2 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20  ->pgno;.  Pager 
156c3 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70  *pPager = pBt->p
156c4 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b  Pager;.  int rc;
156c5 0a 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70  ..  assert( eTyp
156c6 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
156c7 4f 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54  OW2 || eType==PT
156c8 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c  RMAP_OVERFLOW1 |
156c9 7c 20 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d  | .      eType==
156ca 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20  PTRMAP_BTREE || 
156cb 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f  eType==PTRMAP_RO
156cc 4f 54 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65  OTPAGE );.  asse
156cd 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
156ce 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
156cf 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
156d0 70 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42  pDbPage->pBt==pB
156d1 74 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20  t );..  /* Move 
156d2 70 61 67 65 20 69 44 62 50 61 67 65 20 66 72 6f  page iDbPage fro
156d3 6d 20 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f  m its current lo
156d4 63 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e  cation to page n
156d5 75 6d 62 65 72 20 69 46 72 65 65 50 61 67 65 20  umber iFreePage 
156d6 2a 2f 0a 20 20 54 52 41 43 45 28 28 22 41 55 54  */.  TRACE(("AUT
156d7 4f 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20  OVACUUM: Moving 
156d8 25 64 20 74 6f 20 66 72 65 65 20 70 61 67 65 20  %d to free page 
156d9 25 64 20 28 70 74 72 20 70 61 67 65 20 25 64 20  %d (ptr page %d 
156da 74 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20  type %d)\n", .  
156db 20 20 20 20 69 44 62 50 61 67 65 2c 20 69 46 72      iDbPage, iFr
156dc 65 65 50 61 67 65 2c 20 69 50 74 72 50 61 67 65  eePage, iPtrPage
156dd 2c 20 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20  , eType));.  rc 
156de 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f  = sqlite3PagerMo
156df 76 65 70 61 67 65 28 70 50 61 67 65 72 2c 20 70  vepage(pPager, p
156e0 44 62 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c  DbPage->pDbPage,
156e1 20 69 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f   iFreePage, isCo
156e2 6d 6d 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21  mmit);.  if( rc!
156e3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
156e4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
156e5 0a 20 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f  .  pDbPage->pgno
156e6 20 3d 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20   = iFreePage;.. 
156e7 20 2f 2a 20 49 66 20 70 44 62 50 61 67 65 20 77   /* If pDbPage w
156e8 61 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c  as a btree-page,
156e9 20 74 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76   then it may hav
156ea 65 20 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e  e child pages an
156eb 64 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20  d/or cells.  ** 
156ec 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76  that point to ov
156ed 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68  erflow pages. Th
156ee 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e  e pointer map en
156ef 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68  tries for all th
156f0 65 73 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e  ese.  ** pages n
156f1 65 65 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65  eed to be change
156f2 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  d..  **.  ** If 
156f3 70 44 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76  pDbPage is an ov
156f4 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65  erflow page, the
156f5 6e 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79  n the first 4 by
156f6 74 65 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a  tes may store a.
156f7 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20    ** pointer to 
156f8 61 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65  a subsequent ove
156f9 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74  rflow page. If t
156fa 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c  his is the case,
156fb 20 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70   then.  ** the p
156fc 6f 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73  ointer map needs
156fd 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 66   to be updated f
156fe 6f 72 20 74 68 65 20 73 75 62 73 65 71 75 65 6e  or the subsequen
156ff 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  t overflow page.
15700 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70  .  */.  if( eTyp
15701 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20  e==PTRMAP_BTREE 
15702 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  || eType==PTRMAP
15703 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20  _ROOTPAGE ){.   
15704 20 72 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74   rc = setChildPt
15705 72 6d 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a  rmaps(pDbPage);.
15706 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
15707 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
15708 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
15709 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e    }else{.    Pgn
1570a 6f 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74  o nextOvfl = get
1570b 34 62 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61  4byte(pDbPage->a
1570c 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e  Data);.    if( n
1570d 65 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20  extOvfl!=0 ){.  
1570e 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50      rc = ptrmapP
1570f 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c  ut(pBt, nextOvfl
15710 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  , PTRMAP_OVERFLO
15711 57 32 2c 20 69 46 72 65 65 50 61 67 65 29 3b 0a  W2, iFreePage);.
15712 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
15713 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15714 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
15715 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
15716 0a 20 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61  .  /* Fix the da
15717 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f  tabase pointer o
15718 6e 20 70 61 67 65 20 69 50 74 72 50 61 67 65 20  n page iPtrPage 
15719 74 68 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20  that pointed at 
1571a 69 44 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20  iDbPage so.  ** 
1571b 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61  that it points a
1571c 74 20 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73  t iFreePage. Als
1571d 6f 20 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65  o fix the pointe
1571e 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a  r map entry for.
1571f 20 20 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20    ** iPtrPage.. 
15720 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21   */.  if( eType!
15721 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45  =PTRMAP_ROOTPAGE
15722 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
15723 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
15724 28 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20  (pBt, iPtrPage, 
15725 26 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20  &pPtrPage, 0);. 
15726 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
15727 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
15728 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
15729 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1572a 61 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61  agerWrite(pPtrPa
1572b 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
1572c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1572d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c  _OK ){.      rel
1572e 65 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67  easePage(pPtrPag
1572f 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
15730 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72   rc;.    }.    r
15731 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f  c = modifyPagePo
15732 69 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20  inter(pPtrPage, 
15733 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61  iDbPage, iFreePa
15734 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20  ge, eType);.    
15735 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72  releasePage(pPtr
15736 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
15737 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
15738 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
15739 70 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 50  pPut(pBt, iFreeP
1573a 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72  age, eType, iPtr
1573b 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Page);.    }.  }
1573c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1573d 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
1573e 61 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64  aration required
1573f 20 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74   by incrVacuumSt
15740 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20  ep(). */.static 
15741 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  int allocateBtre
15742 65 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a  ePage(BtShared *
15743 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67  , MemPage **, Pg
15744 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b  no *, Pgno, u8);
15745 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20  ../*.** Perform 
15746 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66  a single step of
15747 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d   an incremental-
15748 76 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65  vacuum. If succe
15749 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e  ssful,.** return
1574a 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74   SQLITE_OK. If t
1574b 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20  here is no work 
1574c 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65  to do (and there
1574d 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74  fore no.** point
1574e 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73   in calling this
1574f 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29   function again)
15750 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
15751 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65  DONE..**.** More
15752 20 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69   specificly, thi
15753 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d  s function attem
15754 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69  pts to re-organi
15755 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62  ze the .** datab
15756 61 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20  ase so that the 
15757 6c 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  last page of the
15758 20 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20   file currently 
15759 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20  in use.** is no 
1575a 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a  longer in use..*
1575b 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e  *.** If the nFin
1575c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
1575d 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 69 6d 70 6c  n-zero, the impl
1575e 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d  ementation assum
1575f 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63  es.** that the c
15760 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20  aller will keep 
15761 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75  calling incrVacu
15762 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a  umStep() until.*
15763 2a 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c  * it returns SQL
15764 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65  ITE_DONE or an e
15765 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e  rror, and that n
15766 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75  Fin is the.** nu
15767 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68  mber of pages th
15768 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
15769 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74  will contain aft
1576a 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63  er this .** proc
1576b 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e  ess is complete.
1576c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
1576d 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 42 74  ncrVacuumStep(Bt
1576e 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
1576f 6f 20 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 4c 61  o nFin, Pgno iLa
15770 73 74 50 67 29 7b 0a 20 20 50 67 6e 6f 20 6e 46  stPg){.  Pgno nF
15771 72 65 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20  reeList;        
15772 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
15773 70 61 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74  pages still on t
15774 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a  he free-list */.
15775 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15776 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
15777 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
15778 73 73 65 72 74 28 20 69 4c 61 73 74 50 67 3e 6e  ssert( iLastPg>n
15779 46 69 6e 20 29 3b 0a 0a 20 20 69 66 28 20 21 50  Fin );..  if( !P
1577a 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
1577b 2c 20 69 4c 61 73 74 50 67 29 20 26 26 20 69 4c  , iLastPg) && iL
1577c 61 73 74 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42  astPg!=PENDING_B
1577d 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
1577e 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
1577f 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 50   u8 eType;.    P
15780 67 6e 6f 20 69 50 74 72 50 61 67 65 3b 0a 0a 20  gno iPtrPage;.. 
15781 20 20 20 6e 46 72 65 65 4c 69 73 74 20 3d 20 67     nFreeList = g
15782 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50  et4byte(&pBt->pP
15783 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29  age1->aData[36])
15784 3b 0a 20 20 20 20 69 66 28 20 6e 46 72 65 65 4c  ;.    if( nFreeL
15785 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ist==0 ){.      
15786 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
15787 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72  NE;.    }..    r
15788 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42  c = ptrmapGet(pB
15789 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 65 54 79  t, iLastPg, &eTy
1578a 70 65 2c 20 26 69 50 74 72 50 61 67 65 29 3b 0a  pe, &iPtrPage);.
1578b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1578c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
1578d 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1578e 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50      if( eType==P
1578f 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29  TRMAP_ROOTPAGE )
15790 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
15791 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15792 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  PT;.    }..    i
15793 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  f( eType==PTRMAP
15794 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20  _FREEPAGE ){.   
15795 20 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29     if( nFin==0 )
15796 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d  {.        /* Rem
15797 6f 76 65 20 74 68 65 20 70 61 67 65 20 66 72 6f  ove the page fro
15798 6d 20 74 68 65 20 66 69 6c 65 73 20 66 72 65 65  m the files free
15799 2d 6c 69 73 74 2e 20 54 68 69 73 20 69 73 20 6e  -list. This is n
1579a 6f 74 20 72 65 71 75 69 72 65 64 0a 20 20 20 20  ot required.    
1579b 20 20 20 20 2a 2a 20 69 66 20 6e 46 69 6e 20 69      ** if nFin i
1579c 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74  s non-zero. In t
1579d 68 61 74 20 63 61 73 65 2c 20 74 68 65 20 66 72  hat case, the fr
1579e 65 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 0a  ee-list will be.
1579f 20 20 20 20 20 20 20 20 2a 2a 20 74 72 75 6e 63          ** trunc
157a0 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 61 66 74  ated to zero aft
157a1 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  er this function
157a2 20 72 65 74 75 72 6e 73 2c 20 73 6f 20 69 74 20   returns, so it 
157a3 64 6f 65 73 6e 27 74 20 0a 20 20 20 20 20 20 20  doesn't .       
157a4 20 2a 2a 20 6d 61 74 74 65 72 20 69 66 20 69 74   ** matter if it
157a5 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20   still contains 
157a6 73 6f 6d 65 20 67 61 72 62 61 67 65 20 65 6e 74  some garbage ent
157a7 72 69 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f  ries..        */
157a8 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46  .        Pgno iF
157a9 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 4d  reePg;.        M
157aa 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b  emPage *pFreePg;
157ab 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c  .        rc = al
157ac 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
157ad 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26  pBt, &pFreePg, &
157ae 69 46 72 65 65 50 67 2c 20 69 4c 61 73 74 50 67  iFreePg, iLastPg
157af 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 1);.        if
157b0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
157b1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
157b2 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
157b3 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
157b4 28 20 69 46 72 65 65 50 67 3d 3d 69 4c 61 73 74  ( iFreePg==iLast
157b5 50 67 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65  Pg );.        re
157b6 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 65 50  leasePage(pFreeP
157b7 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
157b8 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 50  } else {.      P
157b9 67 6e 6f 20 69 46 72 65 65 50 67 3b 20 20 20 20  gno iFreePg;    
157ba 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
157bb 78 20 6f 66 20 66 72 65 65 20 70 61 67 65 20 74  x of free page t
157bc 6f 20 6d 6f 76 65 20 70 4c 61 73 74 50 67 20 74  o move pLastPg t
157bd 6f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61  o */.      MemPa
157be 67 65 20 2a 70 4c 61 73 74 50 67 3b 0a 0a 20 20  ge *pLastPg;..  
157bf 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
157c0 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
157c1 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73  , iLastPg, &pLas
157c2 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  tPg, 0);.      i
157c3 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
157c4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
157c5 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a  rn rc;.      }..
157c6 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e        /* If nFin
157c7 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c   is zero, this l
157c8 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74 6c 79  oop runs exactly
157c9 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65 20 70   once and page p
157ca 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a 2a 20  LastPg.      ** 
157cb 69 73 20 73 77 61 70 70 65 64 20 77 69 74 68 20  is swapped with 
157cc 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 70  the first free p
157cd 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 74  age pulled off t
157ce 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 20  he free list..  
157cf 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
157d0 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
157d1 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 20 67 72  d, if nFin is gr
157d2 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c  eater than zero,
157d3 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 20 20 20   then keep.     
157d4 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69   ** looping unti
157d5 6c 20 61 20 66 72 65 65 2d 70 61 67 65 20 6c 6f  l a free-page lo
157d6 63 61 74 65 64 20 77 69 74 68 69 6e 20 74 68 65  cated within the
157d7 20 66 69 72 73 74 20 6e 46 69 6e 20 70 61 67 65   first nFin page
157d8 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68  s.      ** of th
157d9 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e  e file is found.
157da 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
157db 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d  do {.        Mem
157dc 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20  Page *pFreePg;. 
157dd 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f         rc = allo
157de 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
157df 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 69 46  t, &pFreePg, &iF
157e0 72 65 65 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20  reePg, 0, 0);.  
157e1 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
157e2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
157e3 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
157e4 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20  (pLastPg);.     
157e5 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
157e6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
157e7 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 46    releasePage(pF
157e8 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77  reePg);.      }w
157e9 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 20 26 26  hile( nFin!=0 &&
157ea 20 69 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b   iFreePg>nFin );
157eb 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
157ec 46 72 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29  FreePg<iLastPg )
157ed 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20 20 72  ;.      .      r
157ee 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
157ef 57 72 69 74 65 28 70 4c 61 73 74 50 67 2d 3e 70  Write(pLastPg->p
157f0 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  DbPage);.      i
157f1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
157f2 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
157f3 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42   relocatePage(pB
157f4 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54 79 70  t, pLastPg, eTyp
157f5 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69 46 72  e, iPtrPage, iFr
157f6 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a  eePg, nFin!=0);.
157f7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
157f8 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73 74 50  leasePage(pLastP
157f9 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  g);.      if( rc
157fa 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
157fb 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
157fc 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
157fd 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 69 6e 3d    }..  if( nFin=
157fe 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 50  =0 ){.    iLastP
157ff 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  g--;.    while( 
15800 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47  iLastPg==PENDING
15801 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 7c  _BYTE_PAGE(pBt)|
15802 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70  |PTRMAP_ISPAGE(p
15803 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a  Bt, iLastPg) ){.
15804 20 20 20 20 20 20 69 66 28 20 50 54 52 4d 41 50        if( PTRMAP
15805 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61  _ISPAGE(pBt, iLa
15806 73 74 50 67 29 20 29 7b 0a 20 20 20 20 20 20 20  stPg) ){.       
15807 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 3b 0a 20   MemPage *pPg;. 
15808 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20         int rc = 
15809 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
1580a 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67  age(pBt, iLastPg
1580b 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20 20 20  , &pPg, 0);.    
1580c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1580d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1580e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1580f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
15810 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15811 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50  rWrite(pPg->pDbP
15812 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65  age);.        re
15813 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a  leasePage(pPg);.
15814 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
15815 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15816 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
15817 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
15818 20 20 7d 0a 20 20 20 20 20 20 69 4c 61 73 74 50    }.      iLastP
15819 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  g--;.    }.    s
1581a 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63  qlite3PagerTrunc
1581b 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50  ateImage(pBt->pP
1581c 61 67 65 72 2c 20 69 4c 61 73 74 50 67 29 3b 0a  ager, iLastPg);.
1581d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
1581e 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
1581f 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   A write-transac
15820 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65  tion must be ope
15821 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  ned before calli
15822 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ng this function
15823 2e 0a 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73  ..** It performs
15824 20 61 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f   a single unit o
15825 66 20 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61  f work towards a
15826 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61  n incremental va
15827 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  cuum..**.** If t
15828 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76  he incremental v
15829 61 63 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65  acuum is finishe
1582a 64 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e  d after this fun
1582b 63 74 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a  ction has run,.*
1582c 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73  * SQLITE_DONE is
1582d 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74   returned. If it
1582e 20 69 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64   is not finished
1582f 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f  , but no error o
15830 63 63 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49  ccurred,.** SQLI
15831 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
15832 64 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20  d. Otherwise an 
15833 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
15834 65 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  e. .*/.SQLITE_PR
15835 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15836 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d  3BtreeIncrVacuum
15837 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e  (Btree *p){.  in
15838 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64  t rc;.  BtShared
15839 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1583a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1583b 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72  nter(p);.  asser
1583c 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  t( pBt->inTransa
1583d 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
1583e 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73  TE && p->inTrans
1583f 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
15840 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74  .  if( !pBt->aut
15841 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72  oVacuum ){.    r
15842 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
15843 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
15844 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66  validateAllOverf
15845 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20  lowCache(pBt);. 
15846 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75     rc = incrVacu
15847 75 6d 53 74 65 70 28 70 42 74 2c 20 30 2c 20 70  umStep(pBt, 0, p
15848 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
15849 74 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  t));.  }.  sqlit
1584a 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1584b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1584c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1584d 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 70 72  ine is called pr
1584e 69 6f 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61  ior to sqlite3Pa
1584f 67 65 72 43 6f 6d 6d 69 74 20 77 68 65 6e 20 61  gerCommit when a
15850 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
15851 69 73 20 63 6f 6d 6d 69 74 65 64 20 66 6f 72 20  is commited for 
15852 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  an auto-vacuum d
15853 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  atabase..**.** I
15854 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  f SQLITE_OK is r
15855 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70  eturned, then *p
15856 6e 54 72 75 6e 63 20 69 73 20 73 65 74 20 74 6f  nTrunc is set to
15857 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
15858 61 67 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61  ages.** the data
15859 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64  base file should
1585a 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f   be truncated to
1585b 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 6d   during the comm
1585c 69 74 20 70 72 6f 63 65 73 73 2e 20 0a 2a 2a 20  it process. .** 
1585d 69 2e 65 2e 20 74 68 65 20 64 61 74 61 62 61 73  i.e. the databas
1585e 65 20 68 61 73 20 62 65 65 6e 20 72 65 6f 72 67  e has been reorg
1585f 61 6e 69 7a 65 64 20 73 6f 20 74 68 61 74 20 6f  anized so that o
15860 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 2a 70  nly the first *p
15861 6e 54 72 75 6e 63 0a 2a 2a 20 70 61 67 65 73 20  nTrunc.** pages 
15862 61 72 65 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73  are in use..*/.s
15863 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f 56 61  tatic int autoVa
15864 63 75 75 6d 43 6f 6d 6d 69 74 28 42 74 53 68 61  cuumCommit(BtSha
15865 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74  red *pBt){.  int
15866 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
15867 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
15868 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a   = pBt->pPager;.
15869 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20    VVA_ONLY( int 
1586a 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61  nRef = sqlite3Pa
1586b 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67  gerRefcount(pPag
1586c 65 72 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74  er) );..  assert
1586d 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1586e 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
1586f 20 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65   );.  invalidate
15870 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
15871 28 70 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28  (pBt);.  assert(
15872 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
15873 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e  ;.  if( !pBt->in
15874 63 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  crVacuum ){.    
15875 50 67 6e 6f 20 6e 46 69 6e 3b 0a 20 20 20 20 50  Pgno nFin;.    P
15876 67 6e 6f 20 6e 46 72 65 65 3b 0a 20 20 20 20 50  gno nFree;.    P
15877 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 0a 20 20 20  gno nPtrmap;.   
15878 20 50 67 6e 6f 20 69 46 72 65 65 3b 0a 20 20 20   Pgno iFree;.   
15879 20 63 6f 6e 73 74 20 69 6e 74 20 70 67 73 7a 20   const int pgsz 
1587a 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b  = pBt->pageSize;
1587b 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 67 20  .    Pgno nOrig 
1587c 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  = pagerPagecount
1587d 28 70 42 74 29 3b 0a 0a 20 20 20 20 69 66 28 20  (pBt);..    if( 
1587e 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42  PTRMAP_ISPAGE(pB
1587f 74 2c 20 6e 4f 72 69 67 29 20 7c 7c 20 6e 4f 72  t, nOrig) || nOr
15880 69 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  ig==PENDING_BYTE
15881 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20  _PAGE(pBt) ){.  
15882 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74      /* It is not
15883 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 72 65   possible to cre
15884 61 74 65 20 61 20 64 61 74 61 62 61 73 65 20 66  ate a database f
15885 6f 72 20 77 68 69 63 68 20 74 68 65 20 66 69 6e  or which the fin
15886 61 6c 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a  al page.      **
15887 20 69 73 20 65 69 74 68 65 72 20 61 20 70 6f 69   is either a poi
15888 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 6f 72  nter-map page or
15889 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 62 79 74   the pending-byt
1588a 65 20 70 61 67 65 2e 20 49 66 20 6f 6e 65 0a 20  e page. If one. 
1588b 20 20 20 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75       ** is encou
1588c 6e 74 65 72 65 64 2c 20 74 68 69 73 20 69 6e 64  ntered, this ind
1588d 69 63 61 74 65 73 20 63 6f 72 72 75 70 74 69 6f  icates corruptio
1588e 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n..      */.    
1588f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15890 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
15891 20 20 7d 0a 0a 20 20 20 20 6e 46 72 65 65 20 3d    }..    nFree =
15892 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e   get4byte(&pBt->
15893 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
15894 5d 29 3b 0a 20 20 20 20 6e 50 74 72 6d 61 70 20  ]);.    nPtrmap 
15895 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 2b 50  = (nFree-nOrig+P
15896 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
15897 2c 20 6e 4f 72 69 67 29 2b 70 67 73 7a 2f 35 29  , nOrig)+pgsz/5)
15898 2f 28 70 67 73 7a 2f 35 29 3b 0a 20 20 20 20 6e  /(pgsz/5);.    n
15899 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e 46  Fin = nOrig - nF
1589a 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a 20  ree - nPtrmap;. 
1589b 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 45 4e     if( nOrig>PEN
1589c 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1589d 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 4e 44  Bt) && nFin<PEND
1589e 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
1589f 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e  t) ){.      nFin
158a0 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68  --;.    }.    wh
158a1 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41  ile( PTRMAP_ISPA
158a2 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 7c 7c  GE(pBt, nFin) ||
158a3 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 5f 42   nFin==PENDING_B
158a4 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
158a5 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20  .      nFin--;. 
158a6 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 46     }..    for(iF
158a7 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 72 65 65  ree=nOrig; iFree
158a8 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d 53 51 4c  >nFin && rc==SQL
158a9 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 2d 2d 29  ITE_OK; iFree--)
158aa 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e 63  {.      rc = inc
158ab 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 74 2c  rVacuumStep(pBt,
158ac 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 3b 0a 20   nFin, iFree);. 
158ad 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 72 63     }.    if( (rc
158ae 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 7c 7c  ==SQLITE_DONE ||
158af 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20   rc==SQLITE_OK) 
158b0 26 26 20 6e 46 72 65 65 3e 30 20 29 7b 0a 20 20  && nFree>0 ){.  
158b1 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
158b2 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  OK;.      rc = s
158b3 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
158b4 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44  (pBt->pPage1->pD
158b5 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 75  bPage);.      pu
158b6 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61  t4byte(&pBt->pPa
158b7 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20  ge1->aData[32], 
158b8 30 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79  0);.      put4by
158b9 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
158ba 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 29 3b 0a  >aData[36], 0);.
158bb 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
158bc 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28  erTruncateImage(
158bd 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 46 69  pBt->pPager, nFi
158be 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  n);.    }.    if
158bf 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
158c0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
158c1 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50  PagerRollback(pP
158c2 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ager);.    }.  }
158c3 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 66  ..  assert( nRef
158c4 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  ==sqlite3PagerRe
158c5 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29  fcount(pPager) )
158c6 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
158c7 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64  ..#endif /* ifnd
158c8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
158c9 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a  UTOVACUUM */../*
158ca 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
158cb 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20   does the first 
158cc 70 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70  phase of a two-p
158cd 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68  hase commit.  Th
158ce 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61  is routine.** ca
158cf 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20  uses a rollback 
158d0 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72  journal to be cr
158d1 65 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65  eated (if it doe
158d2 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78  s not already ex
158d3 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75  ist).** and popu
158d4 6c 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67  lated with enoug
158d5 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f  h information so
158d6 20 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72   that if a power
158d7 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20   loss occurs.** 
158d8 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e  the database can
158d9 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20   be restored to 
158da 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61  its original sta
158db 74 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61  te by playing ba
158dc 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61  ck.** the journa
158dd 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e  l.  Then the con
158de 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75  tents of the jou
158df 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64  rnal are flushed
158e0 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64   out to.** the d
158e1 69 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20  isk.  After the 
158e2 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c  journal is safel
158e3 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20  y on oxide, the 
158e4 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a  changes to the.*
158e5 2a 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77  * database are w
158e6 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
158e7 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
158e8 64 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69  d flushed to oxi
158e9 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e  de..** At the en
158ea 64 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20  d of this call, 
158eb 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
158ec 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74  rnal still exist
158ed 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b  s on the.** disk
158ee 20 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c   and we are stil
158ef 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f  l holding all lo
158f0 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e  cks, so the tran
158f1 73 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a  saction has not.
158f2 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53  ** committed.  S
158f3 65 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  ee sqlite3BtreeC
158f4 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20  ommitPhaseTwo() 
158f5 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70  for the second p
158f6 68 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63  hase of the.** c
158f7 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a  ommit process..*
158f8 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69  *.** This call i
158f9 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20  s a no-op if no 
158fa 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
158fb 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  n is currently a
158fc 63 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a  ctive on pBt..**
158fd 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73  .** Otherwise, s
158fe 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65  ync the database
158ff 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74   file for the bt
15900 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72  ree pBt. zMaster
15901 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68   points to.** th
15902 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74  e name of a mast
15903 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
15904 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77  that should be w
15905 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a  ritten into the.
15906 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f  ** individual jo
15907 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69  urnal file, or i
15908 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69  s NULL, indicati
15909 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  ng no master jou
1590a 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73  rnal file .** (s
1590b 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74  ingle database t
1590c 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a  ransaction)..**.
1590d 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20  ** When this is 
1590e 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74  called, the mast
1590f 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c  er journal shoul
15910 64 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62  d already have b
15911 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20  een.** created, 
15912 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74  populated with t
15913 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  his journal poin
15914 74 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74  ter and synced t
15915 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  o disk..**.** On
15916 63 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69  ce this is routi
15917 6e 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c  ne has returned,
15918 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   the only thing 
15919 72 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d  required to comm
1591a 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d  it.** the write-
1591b 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20  transaction for 
1591c 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69  this database fi
1591d 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20  le is to delete 
1591e 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a  the journal..*/.
1591f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15920 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
15921 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74  ommitPhaseOne(Bt
15922 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  ree *p, const ch
15923 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
15924 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
15925 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54  OK;.  if( p->inT
15926 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
15927 45 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65  E ){.    BtShare
15928 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
15929 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1592a 65 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64  eEnter(p);.#ifnd
1592b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1592c 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
1592d 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
1592e 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  m ){.      rc = 
1592f 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74  autoVacuumCommit
15930 28 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28  (pBt);.      if(
15931 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15932 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
15933 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
15934 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
15935 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
15936 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d  .#endif.    rc =
15937 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
15938 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d  mitPhaseOne(pBt-
15939 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72  >pPager, zMaster
1593a 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
1593b 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1593c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1593d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74  .}../*.** Commit
1593e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1593f 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70 72   currently in pr
15940 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ogress..**.** Th
15941 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65  is routine imple
15942 6d 65 6e 74 73 20 74 68 65 20 73 65 63 6f 6e 64  ments the second
15943 20 70 68 61 73 65 20 6f 66 20 61 20 32 2d 70 68   phase of a 2-ph
15944 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 65  ase commit.  The
15945 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65  .** sqlite3Btree
15946 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29  CommitPhaseOne()
15947 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68   routine does th
15948 65 20 66 69 72 73 74 20 70 68 61 73 65 20 61 6e  e first phase an
15949 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 69  d should.** be i
1594a 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20  nvoked prior to 
1594b 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
1594c 74 69 6e 65 2e 20 20 54 68 65 20 73 71 6c 69 74  tine.  The sqlit
1594d 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
1594e 73 65 4f 6e 65 28 29 0a 2a 2a 20 72 6f 75 74 69  seOne().** routi
1594f 6e 65 20 64 69 64 20 61 6c 6c 20 74 68 65 20 77  ne did all the w
15950 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69  ork of writing i
15951 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74  nformation out t
15952 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68  o disk and flush
15953 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65  ing the.** conte
15954 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79  nts so that they
15955 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74   are written ont
15956 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74  o the disk platt
15957 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a  er.  All this.**
15958 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20   routine has to 
15959 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20  do is delete or 
1595a 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f  truncate or zero
1595b 20 74 68 65 20 68 65 61 64 65 72 20 69 6e 20 74   the header in t
1595c 68 65 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61  he.** the rollba
1595d 63 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68 69 63  ck journal (whic
1595e 68 20 63 61 75 73 65 73 20 74 68 65 20 74 72 61  h causes the tra
1595f 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d  nsaction to comm
15960 69 74 29 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 20  it) and.** drop 
15961 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  locks..**.** Thi
15962 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74  s will release t
15963 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e  he write lock on
15964 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
15965 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a  le.  If there.**
15966 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63   are no active c
15967 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20  ursors, it also 
15968 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61  releases the rea
15969 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54  d lock..*/.SQLIT
1596a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1596b 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
1596c 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 20 2a  PhaseTwo(Btree *
1596d 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  p){.  BtShared *
1596e 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20  pBt = p->pBt;.. 
1596f 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
15970 65 72 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e  er(p);.  btreeIn
15971 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f  tegrity(p);..  /
15972 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20  * If the handle 
15973 68 61 73 20 61 20 77 72 69 74 65 2d 74 72 61 6e  has a write-tran
15974 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 63 6f  saction open, co
15975 6d 6d 69 74 20 74 68 65 20 73 68 61 72 65 64 2d  mmit the shared-
15976 62 74 72 65 65 73 20 0a 20 20 2a 2a 20 74 72 61  btrees .  ** tra
15977 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 73 65 74  nsaction and set
15978 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74   the shared stat
15979 65 20 74 6f 20 54 52 41 4e 53 5f 52 45 41 44 2e  e to TRANS_READ.
1597a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69  .  */.  if( p->i
1597b 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
1597c 49 54 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ITE ){.    int r
1597d 63 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  c;.    assert( p
1597e 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
1597f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n==TRANS_WRITE )
15980 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
15981 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e  t->nTransaction>
15982 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  0 );.    rc = sq
15983 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
15984 50 68 61 73 65 54 77 6f 28 70 42 74 2d 3e 70 50  PhaseTwo(pBt->pP
15985 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ager);.    if( r
15986 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
15987 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
15988 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20  eeLeave(p);.    
15989 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1598a 20 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72   }.    pBt->inTr
1598b 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e  ansaction = TRAN
1598c 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 2f  S_READ;.  }..  /
1598d 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20  * If the handle 
1598e 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20  has any kind of 
1598f 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
15990 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20  , decrement the 
15991 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a  transaction.  **
15992 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 68   count of the sh
15993 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 74  ared btree. If t
15994 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63  he transaction c
15995 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30 2c 20  ount reaches 0, 
15996 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73 68 61  set.  ** the sha
15997 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41  red state to TRA
15998 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e 6c  NS_NONE. The unl
15999 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
1599a 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a 20 20  () call below.  
1599b 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74  ** will unlock t
1599c 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a 20  he pager..  */. 
1599d 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21   if( p->inTrans!
1599e 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20  =TRANS_NONE ){. 
1599f 20 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65     clearAllShare
159a0 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73  dCacheTableLocks
159a1 28 70 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 54  (p);.    pBt->nT
159a2 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20  ransaction--;.  
159a3 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54    if( 0==pBt->nT
159a4 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20  ransaction ){.  
159a5 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73      pBt->inTrans
159a6 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e  action = TRANS_N
159a7 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ONE;.    }.  }..
159a8 20 20 2f 2a 20 53 65 74 20 74 68 65 20 63 75 72    /* Set the cur
159a9 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
159aa 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f   state to TRANS_
159ab 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f 63 6b 0a  NONE and unlock.
159ac 20 20 2a 2a 20 74 68 65 20 70 61 67 65 72 20 69    ** the pager i
159ad 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f 73  f this call clos
159ae 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 64  ed the only read
159af 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61   or write transa
159b0 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 62 74  ction..  */.  bt
159b1 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65  reeClearHasConte
159b2 6e 74 28 70 42 74 29 3b 0a 20 20 70 2d 3e 69 6e  nt(pBt);.  p->in
159b3 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f  Trans = TRANS_NO
159b4 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65  NE;.  unlockBtre
159b5 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
159b6 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74  .  btreeIntegrit
159b7 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  y(p);.  sqlite3B
159b8 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
159b9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
159ba 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f  ;.}../*.** Do bo
159bb 74 68 20 70 68 61 73 65 73 20 6f 66 20 61 20 63  th phases of a c
159bc 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ommit..*/.SQLITE
159bd 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
159be 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
159bf 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74  Btree *p){.  int
159c0 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
159c1 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72  reeEnter(p);.  r
159c2 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
159c3 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70  CommitPhaseOne(p
159c4 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
159c5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
159c6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
159c7 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
159c8 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  (p);.  }.  sqlit
159c9 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
159ca 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
159cb 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
159cc 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
159cd 20 6e 75 6d 62 65 72 20 6f 66 20 77 72 69 74 65   number of write
159ce 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e  -cursors open on
159cf 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 54 68   this handle. Th
159d0 69 73 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a  is is for use.**
159d1 20 69 6e 20 61 73 73 65 72 74 28 29 20 65 78 70   in assert() exp
159d2 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20  ressions, so it 
159d3 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
159d4 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f   if NDEBUG is no
159d5 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a  t.** defined..**
159d6 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70  .** For the purp
159d7 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75  oses of this rou
159d8 74 69 6e 65 2c 20 61 20 77 72 69 74 65 2d 63 75  tine, a write-cu
159d9 72 73 6f 72 20 69 73 20 61 6e 79 20 63 75 72 73  rsor is any curs
159da 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 61  or that.** is ca
159db 70 61 62 6c 65 20 6f 66 20 77 72 69 74 69 6e 67  pable of writing
159dc 20 74 6f 20 74 68 65 20 64 61 74 61 62 73 65 2e   to the databse.
159dd 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68 65    That means the
159de 20 63 75 72 73 6f 72 20 77 61 73 0a 2a 2a 20 6f   cursor was.** o
159df 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65 6e 65 64  riginally opened
159e0 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 64   for writing and
159e1 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20   the cursor has 
159e2 6e 6f 74 20 62 65 20 64 69 73 61 62 6c 65 64 0a  not be disabled.
159e3 2a 2a 20 62 79 20 68 61 76 69 6e 67 20 69 74 73  ** by having its
159e4 20 73 74 61 74 65 20 63 68 61 6e 67 65 64 20 74   state changed t
159e5 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 2e 0a  o CURSOR_FAULT..
159e6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
159e7 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28  untWriteCursors(
159e8 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
159e9 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
159ea 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20  ;.  int r = 0;. 
159eb 20 66 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70   for(pCur=pBt->p
159ec 43 75 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43  Cursor; pCur; pC
159ed 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b  ur=pCur->pNext){
159ee 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 77  .    if( pCur->w
159ef 72 46 6c 61 67 20 26 26 20 70 43 75 72 2d 3e 65  rFlag && pCur->e
159f0 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 46 41  State!=CURSOR_FA
159f1 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a  ULT ) r++; .  }.
159f2 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 23 65    return r;.}.#e
159f3 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
159f4 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
159f5 65 20 73 74 61 74 65 20 74 6f 20 43 55 52 53 4f  e state to CURSO
159f6 52 5f 46 41 55 4c 54 20 61 6e 64 20 74 68 65 20  R_FAULT and the 
159f7 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 74 6f  error.** code to
159f8 20 65 72 72 43 6f 64 65 20 66 6f 72 20 65 76 65   errCode for eve
159f9 72 79 20 63 75 72 73 6f 72 20 6f 6e 20 42 74 53  ry cursor on BtS
159fa 68 61 72 65 64 20 74 68 61 74 20 70 42 74 72 65  hared that pBtre
159fb 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2e  e.** references.
159fc 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 63 75 72  .**.** Every cur
159fd 73 6f 72 20 69 73 20 74 72 69 70 70 65 64 2c 20  sor is tripped, 
159fe 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 73 6f 72  including cursor
159ff 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a  s that belong.**
15a00 20 74 6f 20 6f 74 68 65 72 20 64 61 74 61 62 61   to other databa
15a01 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  se connections t
15a02 68 61 74 20 68 61 70 70 65 6e 20 74 6f 20 62 65  hat happen to be
15a03 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74 68 65 20   sharing.** the 
15a04 63 61 63 68 65 20 77 69 74 68 20 70 42 74 72 65  cache with pBtre
15a05 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
15a06 75 74 69 6e 65 20 67 65 74 73 20 63 61 6c 6c 65  utine gets calle
15a07 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63  d when a rollbac
15a08 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c  k occurs..** All
15a09 20 63 75 72 73 6f 72 73 20 75 73 69 6e 67 20 74   cursors using t
15a0a 68 65 20 73 61 6d 65 20 63 61 63 68 65 20 6d 75  he same cache mu
15a0b 73 74 20 62 65 20 74 72 69 70 70 65 64 0a 2a 2a  st be tripped.**
15a0c 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 6d   to prevent them
15a0d 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20   from trying to 
15a0e 75 73 65 20 74 68 65 20 62 74 72 65 65 20 61 66  use the btree af
15a0f 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62  ter.** the rollb
15a10 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c 6c 62 61  ack.  The rollba
15a11 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65  ck may have dele
15a12 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 72  ted tables.** or
15a13 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70 61 67 65   moved root page
15a14 73 2c 20 73 6f 20 69 74 20 69 73 20 6e 6f 74 20  s, so it is not 
15a15 73 75 66 66 69 63 69 65 6e 74 20 74 6f 0a 2a 2a  sufficient to.**
15a16 20 73 61 76 65 20 74 68 65 20 73 74 61 74 65 20   save the state 
15a17 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20  of the cursor.  
15a18 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20  The cursor must 
15a19 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65  be.** invalidate
15a1a 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
15a1b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
15a1c 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72  3BtreeTripAllCur
15a1d 73 6f 72 73 28 42 74 72 65 65 20 2a 70 42 74 72  sors(Btree *pBtr
15a1e 65 65 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29  ee, int errCode)
15a1f 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  {.  BtCursor *p;
15a20 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
15a21 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20  nter(pBtree);.  
15a22 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42  for(p=pBtree->pB
15a23 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70  t->pCursor; p; p
15a24 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
15a25 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74  int i;.    sqlit
15a26 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
15a27 6f 72 28 70 29 3b 0a 20 20 20 20 70 2d 3e 65 53  or(p);.    p->eS
15a28 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 46 41  tate = CURSOR_FA
15a29 55 4c 54 3b 0a 20 20 20 20 70 2d 3e 73 6b 69 70  ULT;.    p->skip
15a2a 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 20   = errCode;.    
15a2b 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69  for(i=0; i<=p->i
15a2c 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Page; i++){.    
15a2d 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 2d    releasePage(p-
15a2e 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20  >apPage[i]);.   
15a2f 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d 20     p->apPage[i] 
15a30 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
15a31 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
15a32 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f  ve(pBtree);.}../
15a33 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68  *.** Rollback th
15a34 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e  e transaction in
15a35 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c 20   progress.  All 
15a36 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 0a  cursors will be.
15a37 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 20  ** invalided by 
15a38 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20  this operation. 
15a39 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
15a3a 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20  use a cursor.** 
15a3b 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74  that was open at
15a3c 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
15a3d 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  f this operation
15a3e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20   will result.** 
15a3f 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a  in an error..**.
15a40 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c  ** This will rel
15a41 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c  ease the write l
15a42 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
15a43 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68  ase file.  If th
15a44 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63  ere.** are no ac
15a45 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74  tive cursors, it
15a46 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74   also releases t
15a47 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f  he read lock..*/
15a48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15a49 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
15a4a 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 2a  Rollback(Btree *
15a4b 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
15a4c 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
15a4d 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67  p->pBt;.  MemPag
15a4e 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71  e *pPage1;..  sq
15a4f 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
15a50 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41  p);.  rc = saveA
15a51 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30  llCursors(pBt, 0
15a52 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  , 0);.#ifndef SQ
15a53 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
15a54 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21  _CACHE.  if( rc!
15a55 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15a56 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68    /* This is a h
15a57 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f  orrible situatio
15a58 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c  n. An IO or mall
15a59 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72  oc() error occur
15a5a 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a  red whilst.    *
15a5b 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65  * trying to save
15a5c 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e   cursor position
15a5d 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  s. If this is an
15a5e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62   automatic rollb
15a5f 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74  ack (as.    ** t
15a60 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63  he result of a c
15a61 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f  onstraint, mallo
15a62 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49  c() failure or I
15a63 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20  O error) then . 
15a64 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20     ** the cache 
15a65 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c  may be internall
15a66 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28  y inconsistent (
15a67 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69  not contain vali
15a68 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20  d trees) so.    
15a69 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d  ** we cannot sim
15a6a 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65  ply return the e
15a6b 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c  rror to the call
15a6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f  er. Instead, abo
15a6d 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71  rt .    ** all q
15a6e 75 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20  ueries that may 
15a6f 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20  be using any of 
15a70 74 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74  the cursors that
15a71 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e   failed to save.
15a72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
15a73 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43  te3BtreeTripAllC
15a74 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20  ursors(p, rc);. 
15a75 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65   }.#endif.  btre
15a76 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a  eIntegrity(p);..
15a77 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
15a78 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b  ==TRANS_WRITE ){
15a79 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20  .    int rc2;.. 
15a7a 20 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53     assert( TRANS
15a7b 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54  _WRITE==pBt->inT
15a7c 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20  ransaction );.  
15a7d 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50    rc2 = sqlite3P
15a7e 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74  agerRollback(pBt
15a7f 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  ->pPager);.    i
15a80 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f  f( rc2!=SQLITE_O
15a81 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
15a82 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  rc2;.    }..    
15a83 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20  /* The rollback 
15a84 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79  may have destroy
15a85 65 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61  ed the pPage1->a
15a86 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a  Data value.  So.
15a87 20 20 20 20 2a 2a 20 63 61 6c 6c 20 73 71 6c 69      ** call sqli
15a88 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
15a89 29 20 6f 6e 20 70 61 67 65 20 31 20 61 67 61 69  ) on page 1 agai
15a8a 6e 20 74 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a  n to make.    **
15a8b 20 73 75 72 65 20 70 50 61 67 65 31 2d 3e 61 44   sure pPage1->aD
15a8c 61 74 61 20 69 73 20 73 65 74 20 63 6f 72 72 65  ata is set corre
15a8d 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ctly. */.    if(
15a8e 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
15a8f 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50  Page(pBt, 1, &pP
15a90 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45  age1, 0)==SQLITE
15a91 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c  _OK ){.      rel
15a92 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29  easePage(pPage1)
15a93 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
15a94 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75  rt( countWriteCu
15a95 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b  rsors(pBt)==0 );
15a96 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e  .    pBt->inTran
15a97 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f  saction = TRANS_
15a98 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  READ;.  }..  if(
15a99 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41   p->inTrans!=TRA
15a9a 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 63  NS_NONE ){.    c
15a9b 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63  learAllSharedCac
15a9c 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b  heTableLocks(p);
15a9d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74  .    assert( pBt
15a9e 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30  ->nTransaction>0
15a9f 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 6e 54 72   );.    pBt->nTr
15aa0 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20  ansaction--;.   
15aa1 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72   if( 0==pBt->nTr
15aa2 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20  ansaction ){.   
15aa3 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
15aa4 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f  ction = TRANS_NO
15aa5 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  NE;.    }.  }.. 
15aa6 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f   btreeClearHasCo
15aa7 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 70 2d  ntent(pBt);.  p-
15aa8 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53  >inTrans = TRANS
15aa9 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f 63 6b 42  _NONE;.  unlockB
15aaa 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74  treeIfUnused(pBt
15aab 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67  );..  btreeInteg
15aac 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74  rity(p);.  sqlit
15aad 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
15aae 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15aaf 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73  ./*.** Start a s
15ab0 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e  tatement subtran
15ab1 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62  saction. The sub
15ab2 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20  transaction can 
15ab3 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a  can be rolled.**
15ab4 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e   back independen
15ab5 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  tly of the main 
15ab6 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75  transaction. You
15ab7 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 74 72   must start a tr
15ab8 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65  ansaction .** be
15ab9 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 20  fore starting a 
15aba 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  subtransaction. 
15abb 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69  The subtransacti
15abc 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f  on is ended auto
15abd 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66  matically .** if
15abe 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61   the main transa
15abf 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72  ction commits or
15ac0 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a   rolls back..**.
15ac1 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62  ** Statement sub
15ac2 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65  transactions are
15ac3 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64   used around ind
15ac4 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74  ividual SQL stat
15ac5 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61  ements.** that a
15ac6 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74  re contained wit
15ac7 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f  hin a BEGIN...CO
15ac8 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20  MMIT block.  If 
15ac9 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  a constraint.** 
15aca 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74  error occurs wit
15acb 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e  hin the statemen
15acc 74 2c 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  t, the effect of
15acd 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d   that one statem
15ace 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f  ent.** can be ro
15acf 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75  lled back withou
15ad0 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c  t having to roll
15ad1 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20  back the entire 
15ad2 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
15ad3 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73  ** A statement s
15ad4 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ub-transaction i
15ad5 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73  s implemented as
15ad6 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61   an anonymous sa
15ad7 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20  vepoint. The.** 
15ad8 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
15ad9 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
15ada 65 74 65 72 20 69 73 20 74 68 65 20 74 6f 74 61  eter is the tota
15adb 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65  l number of save
15adc 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75  points,.** inclu
15add 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f  ding the new ano
15ade 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74  nymous savepoint
15adf 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d  , open on the B-
15ae0 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68  Tree. i.e. if th
15ae1 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63  ere.** are no ac
15ae2 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20  tive savepoints 
15ae3 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61  and no other sta
15ae4 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69  tement-transacti
15ae5 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74  ons open,.** iSt
15ae6 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68  atement is 1. Th
15ae7 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76  is anonymous sav
15ae8 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65  epoint can be re
15ae9 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64  leased or rolled
15aea 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74   back.** using t
15aeb 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  he sqlite3BtreeS
15aec 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74  avepoint() funct
15aed 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
15aee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15aef 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74  e3BtreeBeginStmt
15af0 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69  (Btree *p, int i
15af1 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e  Statement){.  in
15af2 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64  t rc;.  BtShared
15af3 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
15af4 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
15af5 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74  ter(p);.  assert
15af6 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
15af7 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61  ANS_WRITE );.  a
15af8 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64  ssert( pBt->read
15af9 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Only==0 );.  ass
15afa 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e  ert( iStatement>
15afb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
15afc 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d  Statement>p->db-
15afd 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20  >nSavepoint );. 
15afe 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e   if( NEVER(p->in
15aff 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49  Trans!=TRANS_WRI
15b00 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f  TE || pBt->readO
15b01 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  nly) ){.    rc =
15b02 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c   SQLITE_INTERNAL
15b03 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
15b04 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72  ssert( pBt->inTr
15b05 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
15b06 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a  _WRITE );.    /*
15b07 20 41 74 20 74 68 65 20 70 61 67 65 72 20 6c 65   At the pager le
15b08 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74  vel, a statement
15b09 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
15b0a 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68  a savepoint with
15b0b 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78  .    ** an index
15b0c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c   greater than al
15b0d 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65  l savepoints cre
15b0e 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20  ated explicitly 
15b0f 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c  using.    ** SQL
15b10 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20   statements. It 
15b11 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70  is illegal to op
15b12 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72  en, release or r
15b13 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20  ollback any.    
15b14 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e  ** such savepoin
15b15 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 74 61  ts while the sta
15b16 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
15b17 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20  on savepoint is 
15b18 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20  active..    */. 
15b19 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
15b1a 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e  agerOpenSavepoin
15b1b 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
15b1c 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a  Statement);.  }.
15b1d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
15b1e 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
15b1f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
15b20 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
15b21 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
15b22 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79  on, op, is alway
15b23 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  s SAVEPOINT_ROLL
15b24 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50  BACK.** or SAVEP
15b25 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68  OINT_RELEASE. Th
15b26 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68  is function eith
15b27 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72  er releases or r
15b28 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a  olls back the.**
15b29 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74   savepoint ident
15b2a 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
15b2b 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64  er iSavepoint, d
15b2c 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
15b2d 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e  value .** of op.
15b2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c  .**.** Normally,
15b2f 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67   iSavepoint is g
15b30 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
15b31 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f  qual to zero. Ho
15b32 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a  wever, if op is.
15b33 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  ** SAVEPOINT_ROL
15b34 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76  LBACK, then iSav
15b35 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20  epoint may also 
15b36 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63  be -1. In this c
15b37 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74  ase the .** cont
15b38 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69  ents of the enti
15b39 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  re transaction a
15b3a 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  re rolled back. 
15b3b 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e  This is differen
15b3c 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d  t.** from a norm
15b3d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72  al transaction r
15b3e 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c  ollback, as no l
15b3f 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65  ocks are release
15b40 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61  d and the.** tra
15b41 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73  nsaction remains
15b42 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45   open..*/.SQLITE
15b43 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15b44 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
15b45 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  nt(Btree *p, int
15b46 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f   op, int iSavepo
15b47 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  int){.  int rc =
15b48 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
15b49 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e  ( p && p->inTran
15b4a 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
15b4b 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a  {.    BtShared *
15b4c 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
15b4d 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41    assert( op==SA
15b4e 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20  VEPOINT_RELEASE 
15b4f 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  || op==SAVEPOINT
15b50 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20  _ROLLBACK );.   
15b51 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f   assert( iSavepo
15b52 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65  int>=0 || (iSave
15b53 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d  point==-1 && op=
15b54 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
15b55 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  ACK) );.    sqli
15b56 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
15b57 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
15b58 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  e3PagerSavepoint
15b59 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70  (pBt->pPager, op
15b5a 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20  , iSavepoint);. 
15b5b 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
15b5c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
15b5d 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70   = newDatabase(p
15b5e 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  Bt);.    }.    s
15b5f 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
15b60 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  (p);.  }.  retur
15b61 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
15b62 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 72 73  reate a new curs
15b63 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 65 65  or for the BTree
15b64 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f   whose root is o
15b65 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54  n the page.** iT
15b66 61 62 6c 65 2e 20 20 54 68 65 20 61 63 74 20 6f  able.  The act o
15b67 66 20 61 63 71 75 69 72 69 6e 67 20 61 20 63 75  f acquiring a cu
15b68 72 73 6f 72 20 67 65 74 73 20 61 20 72 65 61 64  rsor gets a read
15b69 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74 68 65   lock on .** the
15b6a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
15b6b 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d  **.** If wrFlag=
15b6c 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  =0, then the cur
15b6d 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20  sor can only be 
15b6e 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67  used for reading
15b6f 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d  ..** If wrFlag==
15b70 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73  1, then the curs
15b71 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  or can be used f
15b72 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 66 6f  or reading or fo
15b73 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69 66 20  r.** writing if 
15b74 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73  other conditions
15b75 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 72 65   for writing are
15b76 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65 73   also met.  Thes
15b77 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63 6f 6e  e.** are the con
15b78 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 73  ditions that mus
15b79 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 65  t be met in orde
15b7a 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20 74 6f  r for writing to
15b7b 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a  .** be allowed:.
15b7c 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 75  **.** 1:  The cu
15b7d 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62  rsor must have b
15b7e 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 20  een opened with 
15b7f 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20  wrFlag==1.**.** 
15b80 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61 62 61  2:  Other databa
15b81 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  se connections t
15b82 68 61 74 20 73 68 61 72 65 20 74 68 65 20 73 61  hat share the sa
15b83 6d 65 20 70 61 67 65 72 20 63 61 63 68 65 0a 2a  me pager cache.*
15b84 2a 20 20 20 20 20 62 75 74 20 77 68 69 63 68 20  *     but which 
15b85 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 52  are not in the R
15b86 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20  EAD_UNCOMMITTED 
15b87 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20 68 61  state may not ha
15b88 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73 6f 72  ve.**     cursor
15b89 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72 46 6c  s open with wrFl
15b8a 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73 61 6d  ag==0 on the sam
15b8b 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65 72 77  e table.  Otherw
15b8c 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65 20 63  ise.**     the c
15b8d 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74  hanges made by t
15b8e 68 69 73 20 77 72 69 74 65 20 63 75 72 73 6f 72  his write cursor
15b8f 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69 62 6c   would be visibl
15b90 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20  e to.**     the 
15b91 72 65 61 64 20 63 75 72 73 6f 72 73 20 69 6e 20  read cursors in 
15b92 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62 61  the other databa
15b93 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
15b94 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64 61 74  *.** 3:  The dat
15b95 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 77 72  abase must be wr
15b96 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72  itable (not on r
15b97 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0a  ead-only media).
15b98 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65 20  **.** 4:  There 
15b99 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 69 76  must be an activ
15b9a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  e transaction..*
15b9b 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 6e 67  *.** No checking
15b9c 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65   is done to make
15b9d 20 73 75 72 65 20 74 68 61 74 20 70 61 67 65 20   sure that page 
15b9e 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20 69 73  iTable really is
15b9f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67   the.** root pag
15ba0 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e 20 20  e of a b-tree.  
15ba1 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68  If it is not, th
15ba2 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 61 63  en the cursor ac
15ba3 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e  quired.** will n
15ba4 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c  ot work correctl
15ba5 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61  y..**.** It is a
15ba6 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20  ssumed that the 
15ba7 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
15ba8 6f 72 53 69 7a 65 28 29 20 62 79 74 65 73 20 6f  orSize() bytes o
15ba9 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 6f 69  f memory .** poi
15baa 6e 74 65 64 20 74 6f 20 62 79 20 70 43 75 72 20  nted to by pCur 
15bab 68 61 76 65 20 62 65 65 6e 20 7a 65 72 6f 65 64  have been zeroed
15bac 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   by the caller..
15bad 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74  */.static int bt
15bae 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72  reeCursor(.  Btr
15baf 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ee *p,          
15bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15bb1 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65      /* The btree
15bb2 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
15bb3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
15bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15bb5 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62  Root page of tab
15bb6 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20  le to open */.  
15bb7 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20  int wrFlag,     
15bb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15bb9 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77         /* 1 to w
15bba 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c  rite. 0 read-onl
15bbb 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65  y */.  struct Ke
15bbc 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c  yInfo *pKeyInfo,
15bbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15bbe 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f   First arg to co
15bbf 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
15bc0 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  n */.  BtCursor 
15bc1 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20  *pCur           
15bc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15bc3 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63   Space for new c
15bc4 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ursor */.){.  in
15bc5 74 20 72 63 3b 0a 20 20 50 67 6e 6f 20 6e 50 61  t rc;.  Pgno nPa
15bc6 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge;.  BtShared *
15bc7 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20  pBt = p->pBt;.. 
15bc8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15bc9 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
15bca 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p) );.  assert( 
15bcb 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46  wrFlag==0 || wrF
15bcc 6c 61 67 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20  lag==1 );.  if( 
15bcd 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 61 73  wrFlag ){.    as
15bce 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64  sert( !pBt->read
15bcf 4f 6e 6c 79 20 29 3b 0a 20 20 20 20 69 66 28 20  Only );.    if( 
15bd0 4e 45 56 45 52 28 70 42 74 2d 3e 72 65 61 64 4f  NEVER(pBt->readO
15bd1 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 72 65  nly) ){.      re
15bd2 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44  turn SQLITE_READ
15bd3 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ONLY;.    }.    
15bd4 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 65 61  rc = checkForRea
15bd5 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54  dConflicts(p, iT
15bd6 61 62 6c 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20  able, 0, 0);.   
15bd7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
15bd8 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  OK ){.      asse
15bd9 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c  rt( rc==SQLITE_L
15bda 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
15bdb 45 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  E );.      retur
15bdc 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
15bdd 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67  .  if( pBt->pPag
15bde 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  e1==0 ){.    rc 
15bdf 3d 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52  = lockBtreeWithR
15be0 65 74 72 79 28 70 29 3b 0a 20 20 20 20 69 66 28  etry(p);.    if(
15be1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15be2 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
15be3 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  c;.    }.  }.  p
15be4 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20  Cur->pgnoRoot = 
15be5 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20  (Pgno)iTable;.  
15be6 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15be7 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e  rPagecount(pBt->
15be8 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a 29 26  pPager, (int *)&
15be9 6e 50 61 67 65 29 3b 20 0a 20 20 69 66 28 20 72  nPage); .  if( r
15bea 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
15beb 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
15bec 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3d   }.  if( iTable=
15bed 3d 31 20 26 26 20 6e 50 61 67 65 3d 3d 30 20 29  =1 && nPage==0 )
15bee 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
15bef 45 5f 45 4d 50 54 59 3b 0a 20 20 20 20 67 6f 74  E_EMPTY;.    got
15bf0 6f 20 63 72 65 61 74 65 5f 63 75 72 73 6f 72 5f  o create_cursor_
15bf1 65 78 63 65 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20  exception;.  }. 
15bf2 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74   rc = getAndInit
15bf3 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e  Page(pBt, pCur->
15bf4 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d  pgnoRoot, &pCur-
15bf5 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 69  >apPage[0]);.  i
15bf6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
15bf7 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65   ){.    goto cre
15bf8 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70  ate_cursor_excep
15bf9 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tion;.  }..  /* 
15bfa 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f 74 68 65  Now that no othe
15bfb 72 20 65 72 72 6f 72 73 20 63 61 6e 20 6f 63 63  r errors can occ
15bfc 75 72 2c 20 66 69 6e 69 73 68 20 66 69 6c 6c 69  ur, finish filli
15bfd 6e 67 20 69 6e 20 74 68 65 20 42 74 43 75 72 73  ng in the BtCurs
15bfe 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65  or.  ** variable
15bff 73 2c 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 73  s, link the curs
15c00 6f 72 20 69 6e 74 6f 20 74 68 65 20 42 74 53 68  or into the BtSh
15c01 61 72 65 64 20 6c 69 73 74 20 61 6e 64 20 73 65  ared list and se
15c02 74 20 2a 70 70 43 75 72 20 28 74 68 65 0a 20 20  t *ppCur (the.  
15c03 2a 2a 20 6f 75 74 70 75 74 20 61 72 67 75 6d 65  ** output argume
15c04 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
15c05 69 6f 6e 29 2e 0a 20 20 2a 2f 0a 20 20 70 43 75  ion)..  */.  pCu
15c06 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b  r->pKeyInfo = pK
15c07 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e  eyInfo;.  pCur->
15c08 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43  pBtree = p;.  pC
15c09 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20  ur->pBt = pBt;. 
15c0a 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20   pCur->wrFlag = 
15c0b 28 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 70 43  (u8)wrFlag;.  pC
15c0c 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d  ur->pNext = pBt-
15c0d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20  >pCursor;.  if( 
15c0e 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  pCur->pNext ){. 
15c0f 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e     pCur->pNext->
15c10 70 50 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20  pPrev = pCur;.  
15c11 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72  }.  pBt->pCursor
15c12 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d   = pCur;.  pCur-
15c13 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
15c14 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 75 72  _INVALID;.  pCur
15c15 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20  ->cachedRowid = 
15c16 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  0;..  return SQL
15c17 49 54 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74 65 5f  ITE_OK;..create_
15c18 63 75 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e  cursor_exception
15c19 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  :.  releasePage(
15c1a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29  pCur->apPage[0])
15c1b 3b 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49  ;.  unlockBtreeI
15c1c 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20  fUnused(pBt);.  
15c1d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
15c1e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15c1f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
15c20 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20  or(.  Btree *p, 
15c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c23 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a    /* The btree *
15c24 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
15c25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c27 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20  /* Root page of 
15c28 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f  table to open */
15c29 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20  .  int wrFlag,  
15c2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15c2c 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20  * 1 to write. 0 
15c2d 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73  read-only */.  s
15c2e 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70  truct KeyInfo *p
15c2f 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20  KeyInfo,        
15c30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
15c31 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70  rst arg to xComp
15c32 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72  are() */.  BtCur
15c33 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20  sor *pCur       
15c34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15c35 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
15c36 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72 65 20  new cursor here 
15c37 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
15c38 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
15c39 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62  ter(p);.  rc = b
15c3a 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20 69 54  treeCursor(p, iT
15c3b 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 70 4b  able, wrFlag, pK
15c3c 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b 0a 20  eyInfo, pCur);. 
15c3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
15c3e 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
15c3f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
15c40 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20  urn the size of 
15c41 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63  a BtCursor objec
15c42 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  t in bytes..**.*
15c43 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
15c44 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20 74  s is needed so t
15c45 68 61 74 20 75 73 65 72 73 20 6f 66 20 63 75 72  hat users of cur
15c46 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c 6c 6f  sors can preallo
15c47 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 69 65  cate.** sufficie
15c48 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20 68 6f  nt storage to ho
15c49 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68  ld a cursor.  Th
15c4a 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63  e BtCursor objec
15c4b 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a 20 74  t is opaque.** t
15c4c 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65 79 20  o users so they 
15c4d 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 73 69  cannot do the si
15c4e 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c 76 65  zeof() themselve
15c4f 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20 63 61  s - they must ca
15c50 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  ll.** this routi
15c51 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ne..*/.SQLITE_PR
15c52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15c53 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
15c54 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  (void){.  return
15c55 20 73 69 7a 65 6f 66 28 42 74 43 75 72 73 6f 72   sizeof(BtCursor
15c56 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
15c57 74 68 65 20 63 61 63 68 65 64 20 72 6f 77 69 64  the cached rowid
15c58 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79 20   value of every 
15c59 63 75 72 73 6f 72 20 69 6e 20 74 68 65 20 73 61  cursor in the sa
15c5a 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  me database file
15c5b 0a 2a 2a 20 61 73 20 70 43 75 72 20 61 6e 64 20  .** as pCur and 
15c5c 68 61 76 69 6e 67 20 74 68 65 20 73 61 6d 65 20  having the same 
15c5d 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
15c5e 20 61 73 20 70 43 75 72 2e 20 20 54 68 65 20 76   as pCur.  The v
15c5f 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74 20 74  alue is.** set t
15c60 6f 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20  o iRowid..**.** 
15c61 4f 6e 6c 79 20 70 6f 73 69 74 69 76 65 20 72 6f  Only positive ro
15c62 77 69 64 20 76 61 6c 75 65 73 20 61 72 65 20 63  wid values are c
15c63 6f 6e 73 69 64 65 72 65 64 20 76 61 6c 69 64 20  onsidered valid 
15c64 66 6f 72 20 74 68 69 73 20 63 61 63 68 65 2e 0a  for this cache..
15c65 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20  ** The cache is 
15c66 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a  initialized to z
15c67 65 72 6f 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  ero, indicating 
15c68 61 6e 20 69 6e 76 61 6c 69 64 20 63 61 63 68 65  an invalid cache
15c69 2e 0a 2a 2a 20 41 20 62 74 72 65 65 20 77 69 6c  ..** A btree wil
15c6a 6c 20 77 6f 72 6b 20 66 69 6e 65 20 77 69 74 68  l work fine with
15c6b 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76   zero or negativ
15c6c 65 20 72 6f 77 69 64 73 2e 20 20 57 65 20 6a 75  e rowids.  We ju
15c6d 73 74 20 63 61 6e 6e 6f 74 0a 2a 2a 20 63 61 63  st cannot.** cac
15c6e 68 65 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74  he zero or negat
15c6f 69 76 65 20 72 6f 77 69 64 73 2c 20 77 68 69 63  ive rowids, whic
15c70 68 20 6d 65 61 6e 73 20 74 61 62 6c 65 73 20 74  h means tables t
15c71 68 61 74 20 75 73 65 20 7a 65 72 6f 20 6f 72 0a  hat use zero or.
15c72 2a 2a 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69  ** negative rowi
15c73 64 73 20 6d 69 67 68 74 20 72 75 6e 20 61 20 6c  ds might run a l
15c74 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20 42  ittle slower.  B
15c75 75 74 20 69 6e 20 70 72 61 63 74 69 63 65 2c 20  ut in practice, 
15c76 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6e 65 67 61 74  zero.** or negat
15c77 69 76 65 20 72 6f 77 69 64 73 20 61 72 65 20 76  ive rowids are v
15c78 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20 73 6f 20  ery uncommon so 
15c79 74 68 69 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20  this should not 
15c7a 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f  be a problem..*/
15c7b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15c7c 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
15c7d 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28  eSetCachedRowid(
15c7e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
15c7f 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52  sqlite3_int64 iR
15c80 6f 77 69 64 29 7b 0a 20 20 42 74 43 75 72 73 6f  owid){.  BtCurso
15c81 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43  r *p;.  for(p=pC
15c82 75 72 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72  ur->pBt->pCursor
15c83 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
15c84 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e  {.    if( p->pgn
15c85 6f 52 6f 6f 74 3d 3d 70 43 75 72 2d 3e 70 67 6e  oRoot==pCur->pgn
15c86 6f 52 6f 6f 74 20 29 20 70 2d 3e 63 61 63 68 65  oRoot ) p->cache
15c87 64 52 6f 77 69 64 20 3d 20 69 52 6f 77 69 64 3b  dRowid = iRowid;
15c88 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
15c89 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64  Cur->cachedRowid
15c8a 3d 3d 69 52 6f 77 69 64 20 29 3b 0a 7d 0a 0a 2f  ==iRowid );.}../
15c8b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
15c8c 63 61 63 68 65 64 20 72 6f 77 69 64 20 66 6f 72  cached rowid for
15c8d 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f   the given curso
15c8e 72 2e 20 20 41 20 6e 65 67 61 74 69 76 65 20 6f  r.  A negative o
15c8f 72 20 7a 65 72 6f 0a 2a 2a 20 72 65 74 75 72 6e  r zero.** return
15c90 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
15c91 20 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20   that the rowid 
15c92 63 61 63 68 65 20 69 73 20 69 6e 76 61 6c 69 64  cache is invalid
15c93 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 0a 2a   and should be.*
15c94 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49 66 20 74  * ignored.  If t
15c95 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 68  he rowid cache h
15c96 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20  as never before 
15c97 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 61  been set, then a
15c98 0a 2a 2a 20 7a 65 72 6f 20 69 73 20 72 65 74 75  .** zero is retu
15c99 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
15c9a 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
15c9b 69 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74 72  int64 sqlite3Btr
15c9c 65 65 47 65 74 43 61 63 68 65 64 52 6f 77 69 64  eeGetCachedRowid
15c9d 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
15c9e 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d  {.  return pCur-
15c9f 3e 63 61 63 68 65 64 52 6f 77 69 64 3b 0a 7d 0a  >cachedRowid;.}.
15ca0 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63  ./*.** Close a c
15ca1 75 72 73 6f 72 2e 20 20 54 68 65 20 72 65 61 64  ursor.  The read
15ca2 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
15ca3 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 72 65  abase file is re
15ca4 6c 65 61 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74  leased.** when t
15ca5 68 65 20 6c 61 73 74 20 63 75 72 73 6f 72 20 69  he last cursor i
15ca6 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c  s closed..*/.SQL
15ca7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15ca8 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
15ca9 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  eCursor(BtCursor
15caa 20 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 65 65   *pCur){.  Btree
15cab 20 2a 70 42 74 72 65 65 20 3d 20 70 43 75 72 2d   *pBtree = pCur-
15cac 3e 70 42 74 72 65 65 3b 0a 20 20 69 66 28 20 70  >pBtree;.  if( p
15cad 42 74 72 65 65 20 29 7b 0a 20 20 20 20 69 6e 74  Btree ){.    int
15cae 20 69 3b 0a 20 20 20 20 42 74 53 68 61 72 65 64   i;.    BtShared
15caf 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42   *pBt = pCur->pB
15cb0 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  t;.    sqlite3Bt
15cb1 72 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29  reeEnter(pBtree)
15cb2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
15cb3 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43  eeClearCursor(pC
15cb4 75 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75  ur);.    if( pCu
15cb5 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20  r->pPrev ){.    
15cb6 20 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70    pCur->pPrev->p
15cb7 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65  Next = pCur->pNe
15cb8 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  xt;.    }else{. 
15cb9 20 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f       pBt->pCurso
15cba 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b  r = pCur->pNext;
15cbb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
15cbc 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  Cur->pNext ){.  
15cbd 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d      pCur->pNext-
15cbe 3e 70 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70  >pPrev = pCur->p
15cbf 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Prev;.    }.    
15cc0 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72  for(i=0; i<=pCur
15cc1 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20  ->iPage; i++){. 
15cc2 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
15cc3 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d  (pCur->apPage[i]
15cc4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c  );.    }.    unl
15cc5 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
15cc6 28 70 42 74 29 3b 0a 20 20 20 20 69 6e 76 61 6c  (pBt);.    inval
15cc7 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63  idateOverflowCac
15cc8 68 65 28 70 43 75 72 29 3b 0a 20 20 20 20 2f 2a  he(pCur);.    /*
15cc9 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
15cca 75 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ur); */.    sqli
15ccb 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42  te3BtreeLeave(pB
15ccc 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  tree);.  }.  ret
15ccd 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
15cce 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 74  ../*.** Make a t
15ccf 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20  emporary cursor 
15cd0 62 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68  by filling in th
15cd1 65 20 66 69 65 6c 64 73 20 6f 66 20 70 54 65 6d  e fields of pTem
15cd2 70 43 75 72 2e 0a 2a 2a 20 54 68 65 20 74 65 6d  pCur..** The tem
15cd3 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 69 73  porary cursor is
15cd4 20 6e 6f 74 20 6f 6e 20 74 68 65 20 63 75 72 73   not on the curs
15cd5 6f 72 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20  or list for the 
15cd6 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Btree..*/.SQLITE
15cd7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
15cd8 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 65 6d  lite3BtreeGetTem
15cd9 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  pCursor(BtCursor
15cda 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 6f 72   *pCur, BtCursor
15cdb 20 2a 70 54 65 6d 70 43 75 72 29 7b 0a 20 20 69   *pTempCur){.  i
15cdc 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
15cdd 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
15cde 28 70 43 75 72 29 20 29 3b 0a 20 20 6d 65 6d 63  (pCur) );.  memc
15cdf 70 79 28 70 54 65 6d 70 43 75 72 2c 20 70 43 75  py(pTempCur, pCu
15ce0 72 2c 20 73 69 7a 65 6f 66 28 42 74 43 75 72 73  r, sizeof(BtCurs
15ce1 6f 72 29 29 3b 0a 20 20 70 54 65 6d 70 43 75 72  or));.  pTempCur
15ce2 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70  ->pNext = 0;.  p
15ce3 54 65 6d 70 43 75 72 2d 3e 70 50 72 65 76 20 3d  TempCur->pPrev =
15ce4 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
15ce5 3c 3d 70 54 65 6d 70 43 75 72 2d 3e 69 50 61 67  <=pTempCur->iPag
15ce6 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  e; i++){.    sql
15ce7 69 74 65 33 50 61 67 65 72 52 65 66 28 70 54 65  ite3PagerRef(pTe
15ce8 6d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d  mpCur->apPage[i]
15ce9 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a  ->pDbPage);.  }.
15cea 20 20 61 73 73 65 72 74 28 20 70 54 65 6d 70 43    assert( pTempC
15ceb 75 72 2d 3e 70 4b 65 79 3d 3d 30 20 29 3b 0a 7d  ur->pKey==0 );.}
15cec 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
15ced 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f   temporary curso
15cee 72 20 73 75 63 68 20 61 73 20 77 61 73 20 6d 61  r such as was ma
15cef 64 65 20 62 79 20 74 68 65 20 43 72 65 61 74 65  de by the Create
15cf0 54 65 6d 70 6f 72 61 72 79 43 75 72 73 6f 72 28  TemporaryCursor(
15cf1 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 62  ).** function ab
15cf2 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ove..*/.SQLITE_P
15cf3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
15cf4 74 65 33 42 74 72 65 65 52 65 6c 65 61 73 65 54  te3BtreeReleaseT
15cf5 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72 73  empCursor(BtCurs
15cf6 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74  or *pCur){.  int
15cf7 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75   i;.  assert( cu
15cf8 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
15cf9 43 75 72 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d  Cur) );.  for(i=
15cfa 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67  0; i<=pCur->iPag
15cfb 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  e; i++){.    sql
15cfc 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
15cfd 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 2d 3e  Cur->apPage[i]->
15cfe 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20  pDbPage);.  }.  
15cff 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75  sqlite3_free(pCu
15d00 72 2d 3e 70 4b 65 79 29 3b 0a 7d 0a 0a 0a 0a 2f  r->pKey);.}..../
15d01 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  *.** Make sure t
15d02 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76  he BtCursor* giv
15d03 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65  en in the argume
15d04 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a  nt has a valid.*
15d05 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20  * BtCursor.info 
15d06 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 69  structure.  If i
15d07 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  t is not already
15d08 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20   valid, call.** 
15d09 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
15d0a 65 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c 6c 20  eCell() to fill 
15d0b 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 74 43  it in..**.** BtC
15d0c 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 61 20  ursor.info is a 
15d0d 63 61 63 68 65 20 6f 66 20 74 68 65 20 69 6e 66  cache of the inf
15d0e 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  ormation in the 
15d0f 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a 2a 2a  current cell..**
15d10 20 55 73 69 6e 67 20 74 68 69 73 20 63 61 63 68   Using this cach
15d11 65 20 72 65 64 75 63 65 73 20 74 68 65 20 6e 75  e reduces the nu
15d12 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f  mber of calls to
15d13 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
15d14 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20  seCell()..**.** 
15d15 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65  2007-06-25:  The
15d16 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73  re is a bug in s
15d17 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ome versions of 
15d18 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20  MSVC that cause 
15d19 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20  the.** compiler 
15d1a 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65  to crash when ge
15d1b 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69  tCellInfo() is i
15d1c 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20  mplemented as a 
15d1d 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68  macro..** But th
15d1e 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65  ere is a measure
15d1f 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e  able speed advan
15d20 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68  tage to using th
15d21 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a  e macro on gcc.*
15d22 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d  * (when less com
15d23 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  piler optimizati
15d24 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20  ons like -Os or 
15d25 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64  -O0 are used and
15d26 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72   the.** compiler
15d27 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67   is not doing ag
15d28 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67  ressive inlining
15d29 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20  .)  So we use a 
15d2a 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  real function.**
15d2b 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20   for MSVC and a 
15d2c 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74  macro for everyt
15d2d 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b  hing else.  Tick
15d2e 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66  et #2457..*/.#if
15d2f 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74  ndef NDEBUG.  st
15d30 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74  atic void assert
15d31 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f  CellInfo(BtCurso
15d32 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65  r *pCur){.    Ce
15d33 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20  llInfo info;.   
15d34 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75   int iPage = pCu
15d35 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65  r->iPage;.    me
15d36 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73  mset(&info, 0, s
15d37 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20  izeof(info));.  
15d38 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61    sqlite3BtreePa
15d39 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70  rseCell(pCur->ap
15d3a 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70 43 75  Page[iPage], pCu
15d3b 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c  r->aiIdx[iPage],
15d3c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73   &info);.    ass
15d3d 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69 6e 66  ert( memcmp(&inf
15d3e 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 2c 20  o, &pCur->info, 
15d3f 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d 3d 30  sizeof(info))==0
15d40 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20   );.  }.#else.  
15d41 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 43 65  #define assertCe
15d42 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64 69 66  llInfo(x).#endif
15d43 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56 45 52  .#ifdef _MSC_VER
15d44 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65 61 6c  .  /* Use a real
15d45 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d 53 56   function in MSV
15d46 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64  C to work around
15d47 20 62 75 67 73 20 69 6e 20 74 68 61 74 20 63 6f   bugs in that co
15d48 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73 74 61  mpiler. */.  sta
15d49 74 69 63 20 76 6f 69 64 20 67 65 74 43 65 6c 6c  tic void getCell
15d4a 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 2a 70  Info(BtCursor *p
15d4b 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20 70 43  Cur){.    if( pC
15d4c 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d  ur->info.nSize==
15d4d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  0 ){.      int i
15d4e 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61  Page = pCur->iPa
15d4f 67 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ge;.      sqlite
15d50 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28  3BtreeParseCell(
15d51 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61  pCur->apPage[iPa
15d52 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b  ge],pCur->aiIdx[
15d53 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e  iPage],&pCur->in
15d54 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d  fo);.      pCur-
15d55 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a  >validNKey = 1;.
15d56 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
15d57 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
15d58 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pCur);.    }.  }
15d59 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74  .#else /* if not
15d5a 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f   _MSC_VER */.  /
15d5b 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e  * Use a macro in
15d5c 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69   all other compi
15d5d 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65  lers so that the
15d5e 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c   function is inl
15d5f 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ined */.#define 
15d60 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  getCellInfo(pCur
15d61 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
15d62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d64 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 70         \.  if( p
15d65 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d  Cur->info.nSize=
15d66 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  =0 ){           
15d67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d69 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e          \.    in
15d6a 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e  t iPage = pCur->
15d6b 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  iPage;          
15d6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d6e 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 73           \.    s
15d6f 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
15d70 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67  Cell(pCur->apPag
15d71 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61  e[iPage],pCur->a
15d72 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75  iIdx[iPage],&pCu
15d73 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 20 20 20  r->info); \.    
15d74 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
15d75 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
15d76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d78 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
15d79 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20  else{           
15d7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d7d 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
15d7e 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f    assertCellInfo
15d7f 28 70 43 75 72 29 3b 20 20 20 20 20 20 20 20 20  (pCur);         
15d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d82 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
15d83 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 4d 53   }.#endif /* _MS
15d84 43 5f 56 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  C_VER */../*.** 
15d85 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68  Set *pSize to th
15d86 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75  e size of the bu
15d87 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68  ffer needed to h
15d88 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  old the value of
15d89 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20  .** the key for 
15d8a 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72  the current entr
15d8b 79 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f  y.  If the curso
15d8c 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e  r is not pointin
15d8d 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20  g.** to a valid 
15d8e 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73  entry, *pSize is
15d8f 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a   set to 0. .**.*
15d90 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69  * For a table wi
15d91 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c  th the INTKEY fl
15d92 61 67 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75  ag set, this rou
15d93 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
15d94 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20   key.** itself, 
15d95 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  not the number o
15d96 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b  f bytes in the k
15d97 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ey..*/.SQLITE_PR
15d98 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15d99 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74  3BtreeKeySize(Bt
15d9a 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36  Cursor *pCur, i6
15d9b 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74  4 *pSize){.  int
15d9c 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
15d9d 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
15d9e 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d  (pCur) );.  rc =
15d9f 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
15da0 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20  sition(pCur);.  
15da1 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
15da2 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  K ){.    assert(
15da3 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
15da4 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c  URSOR_INVALID ||
15da5 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
15da6 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
15da7 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74     if( pCur->eSt
15da8 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
15da9 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 2a 70 53  LID ){.      *pS
15daa 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ize = 0;.    }el
15dab 73 65 7b 0a 20 20 20 20 20 20 67 65 74 43 65 6c  se{.      getCel
15dac 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20  lInfo(pCur);.   
15dad 20 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72     *pSize = pCur
15dae 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20  ->info.nKey;.   
15daf 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
15db0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  rc;.}../*.** Set
15db1 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 6e   *pSize to the n
15db2 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
15db3 66 20 64 61 74 61 20 69 6e 20 74 68 65 20 65 6e  f data in the en
15db4 74 72 79 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f  try the.** curso
15db5 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  r currently poin
15db6 74 73 20 74 6f 2e 20 20 41 6c 77 61 79 73 20 72  ts to.  Always r
15db7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
15db8 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73 20 6e  .** Failure is n
15db9 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 49 66  ot possible.  If
15dba 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e   the cursor is n
15dbb 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  ot currently.** 
15dbc 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 6e 20 65  pointing to an e
15dbd 6e 74 72 79 20 28 77 68 69 63 68 20 63 61 6e 20  ntry (which can 
15dbe 68 61 70 70 65 6e 2c 20 66 6f 72 20 65 78 61 6d  happen, for exam
15dbf 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68 65 20 64  ple, if.** the d
15dc0 61 74 61 62 61 73 65 20 69 73 20 65 6d 70 74 79  atabase is empty
15dc1 29 20 74 68 65 6e 20 2a 70 53 69 7a 65 20 69 73  ) then *pSize is
15dc2 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a 53 51   set to 0..*/.SQ
15dc3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15dc4 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
15dc5 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20 2a  aSize(BtCursor *
15dc6 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69 7a 65  pCur, u32 *pSize
15dc7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
15dc8 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
15dc9 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
15dca 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65  ;.  rc = restore
15dcb 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
15dcc 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  Cur);.  if( rc==
15dcd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15dce 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
15dcf 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
15dd0 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65  VALID || pCur->e
15dd1 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
15dd2 4c 49 44 20 29 3b 0a 20 20 20 20 69 66 28 20 70  LID );.    if( p
15dd3 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
15dd4 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20  SOR_INVALID ){. 
15dd5 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70 6f 69 6e       /* Not poin
15dd6 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20  ting at a valid 
15dd7 65 6e 74 72 79 20 2d 20 73 65 74 20 2a 70 53 69  entry - set *pSi
15dd8 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20 20 20 20  ze to 0. */.    
15dd9 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20    *pSize = 0;.  
15dda 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 67    }else{.      g
15ddb 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29  etCellInfo(pCur)
15ddc 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d  ;.      *pSize =
15ddd 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74   pCur->info.nDat
15dde 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  a;.    }.  }.  r
15ddf 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
15de0 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67  ** Given the pag
15de1 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f  e number of an o
15de2 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20  verflow page in 
15de3 74 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61  the database (pa
15de4 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29  rameter.** ovfl)
15de5 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
15de6 66 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e  finds the page n
15de7 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78  umber of the nex
15de8 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a  t page in the .*
15de9 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  * linked list of
15dea 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
15deb 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74   If possible, it
15dec 20 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76   uses the auto-v
15ded 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72  acuum.** pointer
15dee 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61  -map data instea
15def 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65  d of reading the
15df0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65   content of page
15df1 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20   ovfl to do so. 
15df2 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
15df3 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c  or occurs an SQL
15df4 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ite error code i
15df5 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  s returned. Othe
15df6 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65  rwise:.**.** The
15df7 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
15df8 74 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f  the next overflo
15df9 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69  w page in the li
15dfa 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a  nked list is .**
15dfb 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67   written to *pPg
15dfc 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20  noNext. If page 
15dfd 6f 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74  ovfl is the last
15dfe 20 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e   page in its lin
15dff 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70  ked .** list, *p
15e00 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20  PgnoNext is set 
15e01 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20  to zero. .**.** 
15e02 49 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74  If ppPage is not
15e03 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66   NULL, and a ref
15e04 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65  erence to the Me
15e05 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72  mPage object cor
15e06 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f  responding.** to
15e07 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76   page number pOv
15e08 66 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c  fl was obtained,
15e09 20 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73   then *ppPage is
15e0a 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
15e0b 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e   that.** referen
15e0c 63 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65  ce. It is the re
15e0d 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
15e0e 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61  the caller to ca
15e0f 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29  ll releasePage()
15e10 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74  .** on *ppPage t
15e11 6f 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72  o free the refer
15e12 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65  ence. In no refe
15e13 72 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e  rence was obtain
15e14 65 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74  ed (because.** t
15e15 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77  he pointer-map w
15e16 61 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69  as used to obtai
15e17 6e 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20  n the value for 
15e18 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65  *pPgnoNext), the
15e19 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20  n.** *ppPage is 
15e1a 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a  set to zero..*/.
15e1b 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76  static int getOv
15e1c 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74  erflowPage(.  Bt
15e1d 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20  Shared *pBt, .  
15e1e 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20 20 20  Pgno ovfl,      
15e1f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15e20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 2a 2f  Overflow page */
15e21 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50  .  MemPage **ppP
15e22 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  age,            
15e23 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 65 20  /* OUT: MemPage 
15e24 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 20 4e  handle (may be N
15e25 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f 20 2a  ULL) */.  Pgno *
15e26 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 20 20  pPgnoNext       
15e27 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e         /* OUT: N
15e28 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ext overflow pag
15e29 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a 20  e number */.){. 
15e2a 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 3b 0a   Pgno next = 0;.
15e2b 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
15e2c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 0;.  int rc =
15e2d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61   SQLITE_OK;..  a
15e2e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15e2f 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
15e30 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
15e31 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a 23  t(pPgnoNext);..#
15e32 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
15e33 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
15e34 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 74  /* Try to find t
15e35 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
15e36 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73  the overflow lis
15e37 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a  t using the.  **
15e38 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69 6e   autovacuum poin
15e39 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20 47  ter-map pages. G
15e3a 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e 65  uess that the ne
15e3b 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a 2a  xt page in .  **
15e3c 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69   the overflow li
15e3d 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62 65  st is page numbe
15e3e 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20 74  r (ovfl+1). If t
15e3f 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73 20  hat guess turns 
15e40 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20  .  ** out to be 
15e41 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63 6b  wrong, fall back
15e42 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20   to loading the 
15e43 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20 20  data of page .  
15e44 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20 74  ** number ovfl t
15e45 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
15e46 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65 72  next page number
15e47 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42 74  ..  */.  if( pBt
15e48 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
15e49 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20      Pgno pgno;. 
15e4a 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20 3d     Pgno iGuess =
15e4b 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38 20   ovfl+1;.    u8 
15e4c 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69 6c  eType;..    whil
15e4d 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  e( PTRMAP_ISPAGE
15e4e 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c 7c  (pBt, iGuess) ||
15e4f 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e 47   iGuess==PENDING
15e50 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
15e51 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73 2b  ){.      iGuess+
15e52 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  +;.    }..    if
15e53 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 72 50  ( iGuess<=pagerP
15e54 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b  agecount(pBt) ){
15e55 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d  .      rc = ptrm
15e56 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 65 73  apGet(pBt, iGues
15e57 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 6e 6f  s, &eType, &pgno
15e58 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
15e59 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 54  =SQLITE_OK && eT
15e5a 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52  ype==PTRMAP_OVER
15e5b 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d 6f  FLOW2 && pgno==o
15e5c 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  vfl ){.        n
15e5d 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 20  ext = iGuess;.  
15e5e 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
15e5f 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a  E_DONE;.      }.
15e60 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
15e61 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
15e62 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
15e63 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
15e64 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 2c  tPage(pBt, ovfl,
15e65 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20   &pPage, 0);.   
15e66 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49   assert(rc==SQLI
15e67 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d  TE_OK || pPage==
15e68 30 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74  0);.    if( next
15e69 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  ==0 && rc==SQLIT
15e6a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 65  E_OK ){.      ne
15e6b 78 74 20 3d 20 67 65 74 34 62 79 74 65 28 70 50  xt = get4byte(pP
15e6c 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20  age->aData);.   
15e6d 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 67 6e 6f   }.  }..  *pPgno
15e6e 4e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 69  Next = next;.  i
15e6f 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20  f( ppPage ){.   
15e70 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65   *ppPage = pPage
15e71 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
15e72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
15e73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
15e74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  (rc==SQLITE_DONE
15e75 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72   ? SQLITE_OK : r
15e76 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70  c);.}../*.** Cop
15e77 79 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75  y data from a bu
15e78 66 66 65 72 20 74 6f 20 61 20 70 61 67 65 2c 20  ffer to a page, 
15e79 6f 72 20 66 72 6f 6d 20 61 20 70 61 67 65 20 74  or from a page t
15e7a 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a  o a buffer..**.*
15e7b 2a 20 70 50 61 79 6c 6f 61 64 20 69 73 20 61 20  * pPayload is a 
15e7c 70 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20  pointer to data 
15e7d 73 74 6f 72 65 64 20 6f 6e 20 64 61 74 61 62 61  stored on databa
15e7e 73 65 20 70 61 67 65 20 70 44 62 50 61 67 65 2e  se page pDbPage.
15e7f 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20  .** If argument 
15e80 65 4f 70 20 69 73 20 66 61 6c 73 65 2c 20 74 68  eOp is false, th
15e81 65 6e 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f  en nByte bytes o
15e82 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65  f data are copie
15e83 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79 6c 6f  d.** from pPaylo
15e84 61 64 20 74 6f 20 74 68 65 20 62 75 66 66 65 72  ad to the buffer
15e85 20 70 6f 69 6e 74 65 64 20 61 74 20 62 79 20 70   pointed at by p
15e86 42 75 66 2e 20 49 66 20 65 4f 70 20 69 73 20 74  Buf. If eOp is t
15e87 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c  rue,.** then sql
15e88 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
15e89 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 44   is called on pD
15e8a 62 50 61 67 65 20 61 6e 64 20 6e 42 79 74 65 20  bPage and nByte 
15e8b 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61  bytes.** of data
15e8c 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d   are copied from
15e8d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 75 66   the buffer pBuf
15e8e 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a   to pPayload..**
15e8f 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
15e90 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63   returned on suc
15e91 63 65 73 73 2c 20 6f 74 68 65 72 77 69 73 65 20  cess, otherwise 
15e92 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  an error code..*
15e93 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 70  /.static int cop
15e94 79 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f 69 64  yPayload(.  void
15e95 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20 20 20   *pPayload,     
15e96 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
15e97 20 74 6f 20 70 61 67 65 20 64 61 74 61 20 2a 2f   to page data */
15e98 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20  .  void *pBuf,  
15e99 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15e9a 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65  Pointer to buffe
15e9b 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  r */.  int nByte
15e9c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
15e9d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
15e9e 74 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20  tes to copy */. 
15e9f 20 69 6e 74 20 65 4f 70 2c 20 20 20 20 20 20 20   int eOp,       
15ea0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20             /* 0 
15ea1 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70 61 67  -> copy from pag
15ea2 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74 6f 20  e, 1 -> copy to 
15ea3 70 61 67 65 20 2a 2f 0a 20 20 44 62 50 61 67 65  page */.  DbPage
15ea4 20 2a 70 44 62 50 61 67 65 20 20 20 20 20 20 20   *pDbPage       
15ea5 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74      /* Page cont
15ea6 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61 64 20  aining pPayload 
15ea7 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f 70 20  */.){.  if( eOp 
15ea8 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64  ){.    /* Copy d
15ea9 61 74 61 20 66 72 6f 6d 20 62 75 66 66 65 72 20  ata from buffer 
15eaa 74 6f 20 70 61 67 65 20 28 61 20 77 72 69 74 65  to page (a write
15eab 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20   operation) */. 
15eac 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
15ead 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44  te3PagerWrite(pD
15eae 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
15eaf 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
15eb0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
15eb1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63  ;.    }.    memc
15eb2 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 42 75  py(pPayload, pBu
15eb3 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 65 6c  f, nByte);.  }el
15eb4 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20  se{.    /* Copy 
15eb5 64 61 74 61 20 66 72 6f 6d 20 70 61 67 65 20 74  data from page t
15eb6 6f 20 62 75 66 66 65 72 20 28 61 20 72 65 61 64  o buffer (a read
15eb7 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20   operation) */. 
15eb8 20 20 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20     memcpy(pBuf, 
15eb9 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74 65 29  pPayload, nByte)
15eba 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
15ebb 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
15ebc 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
15ebd 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64   is used to read
15ebe 20 6f 72 20 6f 76 65 72 77 72 69 74 65 20 70 61   or overwrite pa
15ebf 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f  yload informatio
15ec0 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 74  n.** for the ent
15ec1 72 79 20 74 68 61 74 20 74 68 65 20 70 43 75 72  ry that the pCur
15ec2 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74   cursor is point
15ec3 69 6e 67 20 74 6f 2e 20 49 66 20 74 68 65 20 65  ing to. If the e
15ec4 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  Op.** parameter 
15ec5 69 73 20 30 2c 20 74 68 69 73 20 69 73 20 61 20  is 0, this is a 
15ec6 72 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 20 28  read operation (
15ec7 64 61 74 61 20 63 6f 70 69 65 64 20 69 6e 74 6f  data copied into
15ec8 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 29  .** buffer pBuf)
15ec9 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 6e 2d 7a  . If it is non-z
15eca 65 72 6f 2c 20 61 20 77 72 69 74 65 20 28 64 61  ero, a write (da
15ecb 74 61 20 63 6f 70 69 65 64 20 66 72 6f 6d 0a 2a  ta copied from.*
15ecc 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 2e 0a  * buffer pBuf)..
15ecd 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f 66  **.** A total of
15ece 20 22 61 6d 74 22 20 62 79 74 65 73 20 61 72 65   "amt" bytes are
15ecf 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
15ed0 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 6f   beginning at "o
15ed1 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74 61 20  ffset"..** Data 
15ed2 69 73 20 72 65 61 64 20 74 6f 20 6f 72 20 66 72  is read to or fr
15ed3 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42  om the buffer pB
15ed4 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  uf..**.** This r
15ed5 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20  outine does not 
15ed6 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69  make a distincti
15ed7 6f 6e 20 62 65 74 77 65 65 6e 20 6b 65 79 20 61  on between key a
15ed8 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 49 74 20 6a  nd data..** It j
15ed9 75 73 74 20 72 65 61 64 73 20 6f 72 20 77 72 69  ust reads or wri
15eda 74 65 73 20 62 79 74 65 73 20 66 72 6f 6d 20 74  tes bytes from t
15edb 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 2e  he payload area.
15edc 20 20 44 61 74 61 20 6d 69 67 68 74 20 0a 2a 2a    Data might .**
15edd 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20 6d   appear on the m
15ede 61 69 6e 20 70 61 67 65 20 6f 72 20 62 65 20 73  ain page or be s
15edf 63 61 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20  cattered out on 
15ee0 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f  multiple overflo
15ee1 77 20 0a 2a 2a 20 70 61 67 65 73 2e 0a 2a 2a 0a  w .** pages..**.
15ee2 2a 2a 20 49 66 20 74 68 65 20 42 74 43 75 72 73  ** If the BtCurs
15ee3 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  or.isIncrblobHan
15ee4 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c  dle flag is set,
15ee5 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
15ee6 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e 74 72 79  .** cursor entry
15ee7 20 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72   uses one or mor
15ee8 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  e overflow pages
15ee9 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  , this function.
15eea 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61  ** allocates spa
15eeb 63 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a 69 6c  ce for and lazil
15eec 79 20 70 6f 70 6c 75 61 74 65 73 20 74 68 65 20  y popluates the 
15eed 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69  overflow page-li
15eee 73 74 20 0a 2a 2a 20 63 61 63 68 65 20 61 72 72  st .** cache arr
15eef 61 79 20 28 42 74 43 75 72 73 6f 72 2e 61 4f 76  ay (BtCursor.aOv
15ef0 65 72 66 6c 6f 77 29 2e 20 53 75 62 73 65 71 75  erflow). Subsequ
15ef1 65 6e 74 20 63 61 6c 6c 73 20 75 73 65 20 74 68  ent calls use th
15ef2 69 73 0a 2a 2a 20 63 61 63 68 65 20 74 6f 20 6d  is.** cache to m
15ef3 61 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f 20 74  ake seeking to t
15ef4 68 65 20 73 75 70 70 6c 69 65 64 20 6f 66 66 73  he supplied offs
15ef5 65 74 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e  et more efficien
15ef6 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e  t..**.** Once an
15ef7 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c   overflow page-l
15ef8 69 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65  ist cache has be
15ef9 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74  en allocated, it
15efa 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 76 61 6c   may be.** inval
15efb 69 64 61 74 65 64 20 69 66 20 73 6f 6d 65 20 6f  idated if some o
15efc 74 68 65 72 20 63 75 72 73 6f 72 20 77 72 69 74  ther cursor writ
15efd 65 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74  es to the same t
15efe 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 74  able, or if.** t
15eff 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d 6f 76  he cursor is mov
15f00 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e  ed to a differen
15f01 74 20 72 6f 77 2e 20 41 64 64 69 74 69 6f 6e 61  t row. Additiona
15f02 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d 76 61 63  lly, in auto-vac
15f03 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74 68 65  uum.** mode, the
15f04 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 65 6e 74   following event
15f05 73 20 6d 61 79 20 69 6e 76 61 6c 69 64 61 74 65  s may invalidate
15f06 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
15f07 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 0a 2a 2a  e-list cache..**
15f08 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63 72 65  .**   * An incre
15f09 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c 0a 2a  mental vacuum,.*
15f0a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74 20 69  *   * A commit i
15f0b 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d 22 66  n auto_vacuum="f
15f0c 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20  ull" mode,.**   
15f0d 2a 20 43 72 65 61 74 69 6e 67 20 61 20 74 61 62  * Creating a tab
15f0e 6c 65 20 28 6d 61 79 20 72 65 71 75 69 72 65 20  le (may require 
15f0f 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72 66 6c  moving an overfl
15f10 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a 73 74 61  ow page)..*/.sta
15f11 74 69 63 20 69 6e 74 20 61 63 63 65 73 73 50 61  tic int accessPa
15f12 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f  yload(.  BtCurso
15f13 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a  r *pCur,      /*
15f14 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   Cursor pointing
15f15 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61   to entry to rea
15f16 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20  d from */.  u32 
15f17 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  offset,         
15f18 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e   /* Begin readin
15f19 67 20 74 68 69 73 20 66 61 72 20 69 6e 74 6f 20  g this far into 
15f1a 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 33 32  payload */.  u32
15f1b 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20   amt,           
15f1c 20 20 2f 2a 20 52 65 61 64 20 74 68 69 73 20 6d    /* Read this m
15f1d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a 20 20 75  any bytes */.  u
15f1e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 42  nsigned char *pB
15f1f 75 66 2c 20 2f 2a 20 57 72 69 74 65 20 74 68 65  uf, /* Write the
15f20 20 62 79 74 65 73 20 69 6e 74 6f 20 74 68 69 73   bytes into this
15f21 20 62 75 66 66 65 72 20 2a 2f 20 0a 20 20 69 6e   buffer */ .  in
15f22 74 20 73 6b 69 70 4b 65 79 2c 20 20 20 20 20 20  t skipKey,      
15f23 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 62 65 67     /* offset beg
15f24 69 6e 73 20 61 74 20 64 61 74 61 20 69 66 20 74  ins at data if t
15f25 68 69 73 20 69 73 20 74 72 75 65 20 2a 2f 0a 20  his is true */. 
15f26 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 20 20   int eOp        
15f27 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74 6f        /* zero to
15f28 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f 20   read. non-zero 
15f29 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b 0a  to write. */.){.
15f2a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
15f2b 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e 74  *aPayload;.  int
15f2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
15f2d 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 69  .  u32 nKey;.  i
15f2e 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 4d  nt iIdx = 0;.  M
15f2f 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
15f30 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
15f31 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 42 74  r->iPage]; /* Bt
15f32 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 72 72  ree page of curr
15f33 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42  ent entry */.  B
15f34 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
15f35 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20 20 20  Cur->pBt;       
15f36 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74             /* Bt
15f37 72 65 65 20 74 68 69 73 20 63 75 72 73 6f 72 20  ree this cursor 
15f38 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20  belongs to */.. 
15f39 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 29   assert( pPage )
15f3a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
15f3b 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
15f3c 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65  _VALID );.  asse
15f3d 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
15f3e 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61  pCur->iPage]<pPa
15f3f 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61  ge->nCell );.  a
15f40 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
15f41 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
15f42 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28  ..  getCellInfo(
15f43 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f 61  pCur);.  aPayloa
15f44 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70  d = pCur->info.p
15f45 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e 66  Cell + pCur->inf
15f46 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e 4b 65  o.nHeader;.  nKe
15f47 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b  y = (pPage->intK
15f48 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29 70 43  ey ? 0 : (int)pC
15f49 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a  ur->info.nKey);.
15f4a 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29  .  if( skipKey )
15f4b 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20  {.    offset += 
15f4c 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nKey;.  }.  if( 
15f4d 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65  offset+amt > nKe
15f4e 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61  y+pCur->info.nDa
15f4f 74 61 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c  ta .   || &aPayl
15f50 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  oad[pCur->info.n
15f51 4c 6f 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d  Local] > &pPage-
15f52 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62  >aData[pBt->usab
15f53 6c 65 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20  leSize].  ){.   
15f54 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65   /* Trying to re
15f55 61 64 20 6f 72 20 77 72 69 74 65 20 70 61 73 74  ad or write past
15f56 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
15f57 64 61 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72  data is an error
15f58 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53   */.    return S
15f59 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
15f5a 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  PT;.  }..  /* Ch
15f5b 65 63 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74  eck if data must
15f5c 20 62 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e   be read/written
15f5d 20 74 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72   to/from the btr
15f5e 65 65 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20  ee page itself. 
15f5f 2a 2f 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c  */.  if( offset<
15f60 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
15f61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d  l ){.    int a =
15f62 20 61 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b   amt;.    if( a+
15f63 6f 66 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66  offset>pCur->inf
15f64 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20  o.nLocal ){.    
15f65 20 20 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f    a = pCur->info
15f66 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74  .nLocal - offset
15f67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
15f68 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50   copyPayload(&aP
15f69 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20  ayload[offset], 
15f6a 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50  pBuf, a, eOp, pP
15f6b 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
15f6c 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20     offset = 0;. 
15f6d 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20     pBuf += a;.  
15f6e 20 20 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65    amt -= a;.  }e
15f6f 6c 73 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20  lse{.    offset 
15f70 2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c  -= pCur->info.nL
15f71 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ocal;.  }..  if(
15f72 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
15f73 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63  & amt>0 ){.    c
15f74 6f 6e 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a  onst u32 ovflSiz
15f75 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e = pBt->usableS
15f76 69 7a 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74  ize - 4;  /* Byt
15f77 65 73 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f  es content per o
15f78 76 66 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20  vfl page */.    
15f79 50 67 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a  Pgno nextPage;..
15f7a 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67      nextPage = g
15f7b 65 74 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61  et4byte(&aPayloa
15f7c 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  d[pCur->info.nLo
15f7d 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20  cal]);..#ifndef 
15f7e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
15f7f 42 4c 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74  BLOB.    /* If t
15f80 68 65 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  he isIncrblobHan
15f81 64 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 20  dle flag is set 
15f82 61 6e 64 20 74 68 65 20 42 74 43 75 72 73 6f 72  and the BtCursor
15f83 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20  .aOverflow[].   
15f84 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e   ** has not been
15f85 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f   allocated, allo
15f86 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65  cate it now. The
15f87 20 61 72 72 61 79 20 69 73 20 73 69 7a 65 64 20   array is sized 
15f88 61 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e  at.    ** one en
15f89 74 72 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65  try for each ove
15f8a 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68  rflow page in th
15f8b 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e  e overflow chain
15f8c 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67  . The.    ** pag
15f8d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
15f8e 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70  first overflow p
15f8f 61 67 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  age is stored in
15f90 20 61 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20   aOverflow[0],. 
15f91 20 20 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c     ** etc. A val
15f92 75 65 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61  ue of 0 in the a
15f93 4f 76 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79  Overflow[] array
15f94 20 6d 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20   means "not yet 
15f95 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74  known".    ** (t
15f96 68 65 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69  he cache is lazi
15f97 6c 79 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20  ly populated).. 
15f98 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43     */.    if( pC
15f99 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61  ur->isIncrblobHa
15f9a 6e 64 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61  ndle && !pCur->a
15f9b 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
15f9c 20 20 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70    int nOvfl = (p
15f9d 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f  Cur->info.nPaylo
15f9e 61 64 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c  ad-pCur->info.nL
15f9f 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29  ocal+ovflSize-1)
15fa0 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20  /ovflSize;.     
15fa1 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
15fa2 20 3d 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74   = (Pgno *)sqlit
15fa3 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a  e3MallocZero(siz
15fa4 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29  eof(Pgno)*nOvfl)
15fa5 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66  ;.      if( nOvf
15fa6 6c 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65  l && !pCur->aOve
15fa7 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20  rflow ){.       
15fa8 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
15fa9 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EM;.      }.    
15faa 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
15fab 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c   overflow page-l
15fac 69 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65  ist cache has be
15fad 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  en allocated and
15fae 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72   the.    ** entr
15faf 79 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  y for the first 
15fb0 72 65 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f  required overflo
15fb1 77 20 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c  w page is valid,
15fb2 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72   skip.    ** dir
15fb3 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20  ectly to it..   
15fb4 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72   */.    if( pCur
15fb5 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70  ->aOverflow && p
15fb6 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f  Cur->aOverflow[o
15fb7 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20  ffset/ovflSize] 
15fb8 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20  ){.      iIdx = 
15fb9 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65  (offset/ovflSize
15fba 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67  );.      nextPag
15fbb 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66  e = pCur->aOverf
15fbc 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20  low[iIdx];.     
15fbd 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65   offset = (offse
15fbe 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20  t%ovflSize);.   
15fbf 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66   }.#endif..    f
15fc0 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45  or( ; rc==SQLITE
15fc1 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20  _OK && amt>0 && 
15fc2 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b  nextPage; iIdx++
15fc3 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ){..#ifndef SQLI
15fc4 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
15fc5 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71  .      /* If req
15fc6 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20  uired, populate 
15fc7 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
15fc8 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f  e-list cache. */
15fc9 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d  .      if( pCur-
15fca 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  >aOverflow ){.  
15fcb 20 20 20 20 20 20 61 73 73 65 72 74 28 21 70 43        assert(!pC
15fcc 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49  ur->aOverflow[iI
15fcd 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76  dx] || pCur->aOv
15fce 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65  erflow[iIdx]==ne
15fcf 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  xtPage);.       
15fd0 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
15fd1 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67  [iIdx] = nextPag
15fd2 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  e;.      }.#endi
15fd3 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66  f..      if( off
15fd4 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b  set>=ovflSize ){
15fd5 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
15fd6 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72  only reason to r
15fd7 65 61 64 20 74 68 69 73 20 70 61 67 65 20 69 73  ead this page is
15fd8 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70   to obtain the p
15fd9 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e  age.        ** n
15fda 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65  umber for the ne
15fdb 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f  xt page in the o
15fdc 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54  verflow chain. T
15fdd 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20  he page.        
15fde 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72  ** data is not r
15fdf 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73  equired. So firs
15fe0 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20  t try to lookup 
15fe1 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20  the overflow.   
15fe2 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73       ** page-lis
15fe3 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c  t cache, if any,
15fe4 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20   then fall back 
15fe5 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c  to the getOverfl
15fe6 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20 20 20  owPage().       
15fe7 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20   ** function..  
15fe8 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66        */.#ifndef
15fe9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
15fea 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66  RBLOB.        if
15feb 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  ( pCur->aOverflo
15fec 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72  w && pCur->aOver
15fed 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a  flow[iIdx+1] ){.
15fee 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61            nextPa
15fef 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72  ge = pCur->aOver
15ff0 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20  flow[iIdx+1];.  
15ff1 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65        } else .#e
15ff2 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 72  ndif.          r
15ff3 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50  c = getOverflowP
15ff4 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67  age(pBt, nextPag
15ff5 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29  e, 0, &nextPage)
15ff6 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65 74  ;.        offset
15ff7 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20   -= ovflSize;.  
15ff8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
15ff9 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65     /* Need to re
15ffa 61 64 20 74 68 69 73 20 70 61 67 65 20 70 72 6f  ad this page pro
15ffb 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69  perly. It contai
15ffc 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20  ns some of the. 
15ffd 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20         ** range 
15ffe 6f 66 20 64 61 74 61 20 74 68 61 74 20 69 73 20  of data that is 
15fff 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d  being read (eOp=
16000 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28  =0) or written (
16001 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20  eOp!=0)..       
16002 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61   */.        DbPa
16003 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20  ge *pDbPage;.   
16004 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74       int a = amt
16005 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
16006 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
16007 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74  Bt->pPager, next
16008 50 61 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b  Page, &pDbPage);
16009 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
1600a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1600b 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64          aPayload
1600c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
1600d 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b  etData(pDbPage);
1600e 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 50  .          nextP
1600f 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 61  age = get4byte(a
16010 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20  Payload);.      
16011 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73      if( a + offs
16012 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b  et > ovflSize ){
16013 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 20 3d  .            a =
16014 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73   ovflSize - offs
16015 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  et;.          }.
16016 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 63            rc = c
16017 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79  opyPayload(&aPay
16018 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20  load[offset+4], 
16019 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44  pBuf, a, eOp, pD
1601a 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
1601b 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
1601c 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ref(pDbPage);.  
1601d 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d          offset =
1601e 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d   0;.          am
1601f 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20  t -= a;.        
16020 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20    pBuf += a;.   
16021 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
16022 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
16023 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
16024 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65   amt>0 ){.    re
16025 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
16026 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20  UPT_BKPT;.  }.  
16027 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
16028 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66  .** Read part of
16029 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61   the key associa
1602a 74 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20  ted with cursor 
1602b 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a  pCur.  Exactly.*
1602c 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20 77 69  * "amt" bytes wi
1602d 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72 65 64  ll be transfered
1602e 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54   into pBuf[].  T
1602f 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62  he transfer.** b
16030 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74  egins at "offset
16031 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  "..**.** Return 
16032 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
16033 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72  cess or an error
16034 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e   code if anythin
16035 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e  g goes.** wrong.
16036 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65    An error is re
16037 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65  turned if "offse
16038 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72  t+amt" is larger
16039 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61   than.** the ava
1603a 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a  ilable payload..
1603b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1603c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1603d 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a  eeKey(BtCursor *
1603e 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74  pCur, u32 offset
1603f 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20  , u32 amt, void 
16040 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63  *pBuf){.  int rc
16041 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
16042 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
16043 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65  ur) );.  rc = re
16044 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
16045 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28  ion(pCur);.  if(
16046 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16047 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  {.    assert( pC
16048 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
16049 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20  OR_VALID );.    
1604a 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50  assert( pCur->iP
1604b 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e  age>=0 && pCur->
1604c 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1604d 67 65 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ge] );.    if( p
1604e 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e  Cur->apPage[0]->
1604f 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  intKey ){.      
16050 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
16051 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
16052 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  }.    assert( pC
16053 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
16054 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50  iPage]<pCur->apP
16055 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
16056 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72  ->nCell );.    r
16057 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61  c = accessPayloa
16058 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20  d(pCur, offset, 
16059 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  amt, (unsigned c
1605a 68 61 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29  har*)pBuf, 0, 0)
1605b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1605c 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  c;.}../*.** Read
1605d 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
1605e 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
1605f 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20  h cursor pCur.  
16060 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22  Exactly.** "amt"
16061 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74   bytes will be t
16062 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70  ransfered into p
16063 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e  Buf[].  The tran
16064 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61  sfer.** begins a
16065 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a  t "offset"..**.*
16066 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
16067 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72  OK on success or
16068 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
16069 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
1606a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72  ** wrong.  An er
1606b 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
1606c 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20  if "offset+amt" 
1606d 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a  is larger than.*
1606e 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  * the available 
1606f 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49  payload..*/.SQLI
16070 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16071 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
16072 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
16073 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
16074 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29  amt, void *pBuf)
16075 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66  {.  int rc;..#if
16076 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16077 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28  _INCRBLOB.  if (
16078 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16079 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
1607a 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1607b 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65  TE_ABORT;.  }.#e
1607c 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20  ndif..  assert( 
1607d 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1607e 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d  (pCur) );.  rc =
1607f 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
16080 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20  sition(pCur);.  
16081 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16082 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  K ){.    assert(
16083 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16084 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
16085 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
16086 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75  >iPage>=0 && pCu
16087 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16088 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73  iPage] );.    as
16089 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
1608a 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
1608b 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
1608c 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20  ->iPage]->nCell 
1608d 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65  );.    rc = acce
1608e 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  ssPayload(pCur, 
1608f 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75  offset, amt, pBu
16090 66 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  f, 1, 0);.  }.  
16091 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
16092 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
16093 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20  nter to payload 
16094 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
16095 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20   the entry that 
16096 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72  the .** pCur cur
16097 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  sor is pointing 
16098 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72  to.  The pointer
16099 20 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e   is to the begin
1609a 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b  ning of.** the k
1609b 65 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30  ey if skipKey==0
1609c 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74   and it points t
1609d 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
1609e 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b  of data if.** sk
1609f 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e  ipKey==1.  The n
160a0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
160a1 66 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f  f available key/
160a2 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a  data is written.
160a3 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20  ** into *pAmt.  
160a4 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65  If *pAmt==0, the
160a5 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  n the value retu
160a6 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65  rned will not be
160a7 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e  .** a valid poin
160a8 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ter..**.** This 
160a9 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70  routine is an op
160aa 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20  timization.  It 
160ab 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68  is common for th
160ac 65 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20  e entire key.** 
160ad 61 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20  and data to fit 
160ae 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67  on the local pag
160af 65 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20  e and for there 
160b0 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f  to be no overflo
160b1 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65  w.** pages.  Whe
160b2 6e 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68  n that is so, th
160b3 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62  is routine can b
160b4 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  e used to access
160b5 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20   the.** key and 
160b6 64 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b  data without mak
160b7 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20  ing a copy.  If 
160b8 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64  the key and/or d
160b9 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e  ata spills.** on
160ba 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
160bb 73 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61  s, then accessPa
160bc 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20  yload() must be 
160bd 75 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62  used to reassemb
160be 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61  le.** the key/da
160bf 74 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69  ta and copy it i
160c0 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74  nto a preallocat
160c1 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  ed buffer..**.**
160c2 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74   The pointer ret
160c3 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f  urned by this ro
160c4 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65  utine looks dire
160c5 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61  ctly into the ca
160c6 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20  ched.** page of 
160c7 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
160c8 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68  he data might ch
160c9 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65  ange or move the
160ca 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e   next time.** an
160cb 79 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20  y btree routine 
160cc 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74  is called..*/.st
160cd 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
160ce 6e 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50  ned char *fetchP
160cf 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73  ayload(.  BtCurs
160d0 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f  or *pCur,      /
160d1 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  * Cursor pointin
160d2 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65  g to entry to re
160d3 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74  ad from */.  int
160d4 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20   *pAmt,         
160d5 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e    /* Write the n
160d6 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62  umber of availab
160d7 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f  le bytes here */
160d8 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20  .  int skipKey  
160d9 20 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20          /* read 
160da 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74  beginning at dat
160db 61 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75  a if this is tru
160dc 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e  e */.){.  unsign
160dd 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61  ed char *aPayloa
160de 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  d;.  MemPage *pP
160df 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b  age;.  u32 nKey;
160e0 0a 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a  .  u32 nLocal;..
160e1 20 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d    assert( pCur!=
160e2 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65  0 && pCur->iPage
160e3 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50  >=0 && pCur->apP
160e4 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
160e5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
160e6 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
160e7 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73  R_VALID );.  ass
160e8 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
160e9 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
160ea 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
160eb 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
160ec 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  e];.  assert( pC
160ed 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
160ee 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43  iPage]<pPage->nC
160ef 65 6c 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c  ell );.  getCell
160f0 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50  Info(pCur);.  aP
160f1 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69  ayload = pCur->i
160f2 6e 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61  nfo.pCell;.  aPa
160f3 79 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69  yload += pCur->i
160f4 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69  nfo.nHeader;.  i
160f5 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  f( pPage->intKey
160f6 20 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30   ){.    nKey = 0
160f7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
160f8 4b 65 79 20 3d 20 28 69 6e 74 29 70 43 75 72 2d  Key = (int)pCur-
160f9 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a  >info.nKey;.  }.
160fa 20 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b    if( skipKey ){
160fb 0a 20 20 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d  .    aPayload +=
160fc 20 6e 4b 65 79 3b 0a 20 20 20 20 6e 4c 6f 63 61   nKey;.    nLoca
160fd 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  l = pCur->info.n
160fe 4c 6f 63 61 6c 20 2d 20 6e 4b 65 79 3b 0a 20 20  Local - nKey;.  
160ff 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4c 6f 63 61  }else{.    nLoca
16100 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  l = pCur->info.n
16101 4c 6f 63 61 6c 3b 0a 20 20 20 20 69 66 28 20 6e  Local;.    if( n
16102 4c 6f 63 61 6c 3e 6e 4b 65 79 20 29 7b 0a 20 20  Local>nKey ){.  
16103 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 6e 4b 65      nLocal = nKe
16104 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a  y;.    }.  }.  *
16105 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c 3b 0a 20  pAmt = nLocal;. 
16106 20 72 65 74 75 72 6e 20 61 50 61 79 6c 6f 61 64   return aPayload
16107 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  ;.}.../*.** For 
16108 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 63  the entry that c
16109 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 70 6f  ursor pCur is po
1610a 69 6e 74 20 74 6f 2c 20 72 65 74 75 72 6e 20 61  int to, return a
1610b 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74 65 73 20  s.** many bytes 
1610c 6f 66 20 74 68 65 20 6b 65 79 20 6f 72 20 64 61  of the key or da
1610d 74 61 20 61 73 20 61 72 65 20 61 76 61 69 6c 61  ta as are availa
1610e 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c  ble on the local
1610f 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61 67 65 2e  .** b-tree page.
16110 20 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62    Write the numb
16111 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20  er of available 
16112 62 79 74 65 73 20 69 6e 74 6f 20 2a 70 41 6d 74  bytes into *pAmt
16113 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ..**.** The poin
16114 74 65 72 20 72 65 74 75 72 6e 65 64 20 69 73 20  ter returned is 
16115 65 70 68 65 6d 65 72 61 6c 2e 20 20 54 68 65 20  ephemeral.  The 
16116 6b 65 79 2f 64 61 74 61 20 6d 61 79 20 6d 6f 76  key/data may mov
16117 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65 73 74 72  e.** or be destr
16118 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e 65 78 74  oyed on the next
16119 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20 42 74 72   call to any Btr
1611a 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 69  ee routine,.** i
1611b 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 20 66  ncluding calls f
1611c 72 6f 6d 20 6f 74 68 65 72 20 74 68 72 65 61 64  rom other thread
1611d 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 73 61  s against the sa
1611e 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20 48 65 6e  me cache..** Hen
1611f 63 65 2c 20 61 20 6d 75 74 65 78 20 6f 6e 20 74  ce, a mutex on t
16120 68 65 20 42 74 53 68 61 72 65 64 20 73 68 6f 75  he BtShared shou
16121 6c 64 20 62 65 20 68 65 6c 64 20 70 72 69 6f 72  ld be held prior
16122 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74   to calling.** t
16123 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a  his routine..**.
16124 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
16125 73 20 69 73 20 75 73 65 64 20 74 6f 20 67 65 74  s is used to get
16126 20 71 75 69 63 6b 20 61 63 63 65 73 73 20 74 6f   quick access to
16127 20 6b 65 79 20 61 6e 64 20 64 61 74 61 0a 2a 2a   key and data.**
16128 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63   in the common c
16129 61 73 65 20 77 68 65 72 65 20 6e 6f 20 6f 76 65  ase where no ove
1612a 72 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 20  rflow pages are 
1612b 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  used..*/.SQLITE_
1612c 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f  PRIVATE const vo
1612d 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
1612e 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 73 6f  KeyFetch(BtCurso
1612f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 41  r *pCur, int *pA
16130 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  mt){.  assert( c
16131 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16132 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 70  pCur) );.  if( p
16133 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
16134 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20  SOR_VALID ){.   
16135 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20 76   return (const v
16136 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61  oid*)fetchPayloa
16137 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 30 29  d(pCur, pAmt, 0)
16138 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
16139 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
1613a 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
1613b 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46  qlite3BtreeDataF
1613c 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70  etch(BtCursor *p
1613d 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b  Cur, int *pAmt){
1613e 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
1613f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
16140 29 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  ) );.  if( pCur-
16141 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16142 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74  VALID ){.    ret
16143 75 72 6e 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a  urn (const void*
16144 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43  )fetchPayload(pC
16145 75 72 2c 20 70 41 6d 74 2c 20 31 29 3b 0a 20 20  ur, pAmt, 1);.  
16146 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
16147 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65  ../*.** Move the
16148 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20   cursor down to 
16149 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65  a new child page
1614a 2e 20 20 54 68 65 20 6e 65 77 50 67 6e 6f 20 61  .  The newPgno a
1614b 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a  rgument is the.*
1614c 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  * page number of
1614d 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20   the child page 
1614e 74 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2f 0a 73  to move to..*/.s
1614f 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f  tatic int moveTo
16150 43 68 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a  Child(BtCursor *
16151 70 43 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e  pCur, u32 newPgn
16152 6f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  o){.  int rc;.  
16153 69 6e 74 20 69 20 3d 20 70 43 75 72 2d 3e 69 50  int i = pCur->iP
16154 61 67 65 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  age;.  MemPage *
16155 70 4e 65 77 50 61 67 65 3b 0a 20 20 42 74 53 68  pNewPage;.  BtSh
16156 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72  ared *pBt = pCur
16157 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74  ->pBt;..  assert
16158 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16159 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
1615a 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
1615b 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
1615c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
1615d 75 72 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53  ur->iPage<BTCURS
1615e 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a  OR_MAX_DEPTH );.
1615f 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67    if( pCur->iPag
16160 65 3e 3d 28 42 54 43 55 52 53 4f 52 5f 4d 41 58  e>=(BTCURSOR_MAX
16161 5f 44 45 50 54 48 2d 31 29 20 29 7b 0a 20 20 20  _DEPTH-1) ){.   
16162 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
16163 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
16164 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e  .  rc = getAndIn
16165 69 74 50 61 67 65 28 70 42 74 2c 20 6e 65 77 50  itPage(pBt, newP
16166 67 6e 6f 2c 20 26 70 4e 65 77 50 61 67 65 29 3b  gno, &pNewPage);
16167 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
16168 72 6e 20 72 63 3b 0a 20 20 70 43 75 72 2d 3e 61  rn rc;.  pCur->a
16169 70 50 61 67 65 5b 69 2b 31 5d 20 3d 20 70 4e 65  pPage[i+1] = pNe
1616a 77 50 61 67 65 3b 0a 20 20 70 43 75 72 2d 3e 61  wPage;.  pCur->a
1616b 69 49 64 78 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20  iIdx[i+1] = 0;. 
1616c 20 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a   pCur->iPage++;.
1616d 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53  .  pCur->info.nS
1616e 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d  ize = 0;.  pCur-
1616f 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
16170 20 20 69 66 28 20 70 4e 65 77 50 61 67 65 2d 3e    if( pNewPage->
16171 6e 43 65 6c 6c 3c 31 20 29 7b 0a 20 20 20 20 72  nCell<1 ){.    r
16172 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
16173 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
16174 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
16175 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44  K;.}..#ifndef ND
16176 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20  EBUG./*.** Page 
16177 70 50 61 72 65 6e 74 20 69 73 20 61 6e 20 69 6e  pParent is an in
16178 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 66  ternal (non-leaf
16179 29 20 74 72 65 65 20 70 61 67 65 2e 20 54 68 69  ) tree page. Thi
1617a 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 61  s function .** a
1617b 73 73 65 72 74 73 20 74 68 61 74 20 70 61 67 65  sserts that page
1617c 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69   number iChild i
1617d 73 20 74 68 65 20 6c 65 66 74 2d 63 68 69 6c 64  s the left-child
1617e 20 69 66 20 74 68 65 20 69 49 64 78 27 74 68 0a   if the iIdx'th.
1617f 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61 67 65 20  ** cell in page 
16180 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20 69 66 20  pParent. Or, if 
16181 69 49 64 78 20 69 73 20 65 71 75 61 6c 20 74 6f  iIdx is equal to
16182 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
16183 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 69 6e  r of.** cells in
16184 20 70 50 61 72 65 6e 74 2c 20 74 68 61 74 20 70   pParent, that p
16185 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c  age number iChil
16186 64 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 63  d is the right-c
16187 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 70  hild of.** the p
16188 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
16189 6f 69 64 20 61 73 73 65 72 74 50 61 72 65 6e 74  oid assertParent
1618a 49 6e 64 65 78 28 4d 65 6d 50 61 67 65 20 2a 70  Index(MemPage *p
1618b 50 61 72 65 6e 74 2c 20 69 6e 74 20 69 49 64 78  Parent, int iIdx
1618c 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64 29 7b 0a  , Pgno iChild){.
1618d 20 20 61 73 73 65 72 74 28 20 69 49 64 78 3c 3d    assert( iIdx<=
1618e 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29  pParent->nCell )
1618f 3b 0a 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50  ;.  if( iIdx==pP
16190 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  arent->nCell ){.
16191 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74 34      assert( get4
16192 62 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61  byte(&pParent->a
16193 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64  Data[pParent->hd
16194 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 69 43 68  rOffset+8])==iCh
16195 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ild );.  }else{.
16196 20 20 20 20 61 73 73 65 72 74 28 20 67 65 74 34      assert( get4
16197 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50  byte(findCell(pP
16198 61 72 65 6e 74 2c 20 69 49 64 78 29 29 3d 3d 69  arent, iIdx))==i
16199 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a 7d 0a 23  Child );.  }.}.#
1619a 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 61  else.#  define a
1619b 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78  ssertParentIndex
1619c 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64 69 66 0a  (x,y,z) .#endif.
1619d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  ./*.** Move the 
1619e 63 75 72 73 6f 72 20 75 70 20 74 6f 20 74 68 65  cursor up to the
1619f 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 2a 2a   parent page..**
161a0 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78 20 69 73  .** pCur->idx is
161a1 20 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c   set to the cell
161a2 20 69 6e 64 65 78 20 74 68 61 74 20 63 6f 6e 74   index that cont
161a3 61 69 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 72  ains the pointer
161a4 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 20  .** to the page 
161a5 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 72  we are coming fr
161a6 6f 6d 2e 20 20 49 66 20 77 65 20 61 72 65 20 63  om.  If we are c
161a7 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68 65 0a 2a  oming from the.*
161a8 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68 69  * right-most chi
161a9 6c 64 20 70 61 67 65 20 74 68 65 6e 20 70 43 75  ld page then pCu
161aa 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f  r->idx is set to
161ab 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61 6e 0a 2a   one more than.*
161ac 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20 63 65  * the largest ce
161ad 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c  ll index..*/.SQL
161ae 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
161af 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
161b0 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73  eToParent(BtCurs
161b1 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
161b2 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
161b3 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
161b4 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
161b5 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
161b6 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28  LID );.  assert(
161b7 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29   pCur->iPage>0 )
161b8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
161b9 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
161ba 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72  Page] );.  asser
161bb 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20  tParentIndex(.  
161bc 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70    pCur->apPage[p
161bd 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a  Cur->iPage-1], .
161be 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
161bf 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20  pCur->iPage-1], 
161c0 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67  .    pCur->apPag
161c1 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
161c2 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65  pgno.  );.  rele
161c3 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70  asePage(pCur->ap
161c4 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
161c5 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67  ]);.  pCur->iPag
161c6 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66  e--;.  pCur->inf
161c7 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70  o.nSize = 0;.  p
161c8 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
161c9 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76   0;.}../*.** Mov
161ca 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
161cb 74 68 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2f  the root page.*/
161cc 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65  .static int move
161cd 54 6f 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20  ToRoot(BtCursor 
161ce 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67  *pCur){.  MemPag
161cf 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20  e *pRoot;.  int 
161d0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
161d1 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75    Btree *p = pCu
161d2 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53  r->pBtree;.  BtS
161d3 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
161d4 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pBt;..  assert( 
161d5 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
161d6 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
161d7 72 74 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  rt( CURSOR_INVAL
161d8 49 44 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55  ID < CURSOR_REQU
161d9 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73  IRESEEK );.  ass
161da 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49  ert( CURSOR_VALI
161db 44 20 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51  D   < CURSOR_REQ
161dc 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73  UIRESEEK );.  as
161dd 73 65 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55  sert( CURSOR_FAU
161de 4c 54 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45  LT   > CURSOR_RE
161df 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69  QUIRESEEK );.  i
161e0 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e  f( pCur->eState>
161e1 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53  =CURSOR_REQUIRES
161e2 45 45 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70  EEK ){.    if( p
161e3 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
161e4 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20  SOR_FAULT ){.   
161e5 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e     return pCur->
161e6 73 6b 69 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20  skip;.    }.    
161e7 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61  sqlite3BtreeClea
161e8 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20  rCursor(pCur);. 
161e9 20 7d 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e   }..  if( pCur->
161ea 69 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20  iPage>=0 ){.    
161eb 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
161ec 3d 31 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61  =1; i<=pCur->iPa
161ed 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ge; i++){.      
161ee 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72  releasePage(pCur
161ef 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20  ->apPage[i]);.  
161f0 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
161f1 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49   if( .      SQLI
161f2 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 67 65 74  TE_OK!=(rc = get
161f3 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c  AndInitPage(pBt,
161f4 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
161f5 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30   &pCur->apPage[0
161f6 5d 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  ])).    ){.     
161f7 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
161f8 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
161f9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
161fa 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 52  .    }.  }..  pR
161fb 6f 6f 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  oot = pCur->apPa
161fc 67 65 5b 30 5d 3b 0a 20 20 61 73 73 65 72 74 28  ge[0];.  assert(
161fd 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43   pRoot->pgno==pC
161fe 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b 0a  ur->pgnoRoot );.
161ff 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20    pCur->iPage = 
16200 30 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78  0;.  pCur->aiIdx
16201 5b 30 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d  [0] = 0;.  pCur-
16202 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b  >info.nSize = 0;
16203 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20  .  pCur->atLast 
16204 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c  = 0;.  pCur->val
16205 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 69  idNKey = 0;..  i
16206 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d  f( pRoot->nCell=
16207 3d 30 20 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65  =0 && !pRoot->le
16208 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73  af ){.    Pgno s
16209 75 62 70 61 67 65 3b 0a 20 20 20 20 61 73 73 65  ubpage;.    asse
1620a 72 74 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d  rt( pRoot->pgno=
1620b 3d 31 20 29 3b 0a 20 20 20 20 73 75 62 70 61 67  =1 );.    subpag
1620c 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52  e = get4byte(&pR
1620d 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74  oot->aData[pRoot
1620e 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
1620f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 75 62  .    assert( sub
16210 70 61 67 65 3e 30 20 29 3b 0a 20 20 20 20 70 43  page>0 );.    pC
16211 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52  ur->eState = CUR
16212 53 4f 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72  SOR_VALID;.    r
16213 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  c = moveToChild(
16214 70 43 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a  pCur, subpage);.
16215 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75    }else{.    pCu
16216 72 2d 3e 65 53 74 61 74 65 20 3d 20 28 28 70 52  r->eState = ((pR
16217 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f 43 55  oot->nCell>0)?CU
16218 52 53 4f 52 5f 56 41 4c 49 44 3a 43 55 52 53 4f  RSOR_VALID:CURSO
16219 52 5f 49 4e 56 41 4c 49 44 29 3b 0a 20 20 7d 0a  R_INVALID);.  }.
1621a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1621b 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63  /*.** Move the c
1621c 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68  ursor down to th
1621d 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66  e left-most leaf
1621e 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74   entry beneath t
1621f 68 65 0a 2a 2a 20 65 6e 74 72 79 20 74 6f 20 77  he.** entry to w
16220 68 69 63 68 20 69 74 20 69 73 20 63 75 72 72 65  hich it is curre
16221 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a  ntly pointing..*
16222 2a 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f  *.** The left-mo
16223 73 74 20 6c 65 61 66 20 69 73 20 74 68 65 20 6f  st leaf is the o
16224 6e 65 20 77 69 74 68 20 74 68 65 20 73 6d 61 6c  ne with the smal
16225 6c 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 66  lest key - the f
16226 69 72 73 74 0a 2a 2a 20 69 6e 20 61 73 63 65 6e  irst.** in ascen
16227 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73  ding order..*/.s
16228 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f  tatic int moveTo
16229 4c 65 66 74 6d 6f 73 74 28 42 74 43 75 72 73 6f  Leftmost(BtCurso
1622a 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f  r *pCur){.  Pgno
1622b 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20   pgno;.  int rc 
1622c 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d  = SQLITE_OK;.  M
1622d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a  emPage *pPage;..
1622e 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
1622f 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
16230 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
16231 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
16232 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68  OR_VALID );.  wh
16233 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
16234 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d 20  OK && !(pPage = 
16235 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
16236 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66  r->iPage])->leaf
16237 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
16238 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
16239 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e  ->iPage]<pPage->
1623a 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 70 67 6e  nCell );.    pgn
1623b 6f 20 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e  o = get4byte(fin
1623c 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75  dCell(pPage, pCu
1623d 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
1623e 50 61 67 65 5d 29 29 3b 0a 20 20 20 20 72 63 20  Page]));.    rc 
1623f 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
16240 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20  ur, pgno);.  }. 
16241 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
16242 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  *.** Move the cu
16243 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65  rsor down to the
16244 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66   right-most leaf
16245 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74   entry beneath t
16246 68 65 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77 68  he.** page to wh
16247 69 63 68 20 69 74 20 69 73 20 63 75 72 72 65 6e  ich it is curren
16248 74 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 4e  tly pointing.  N
16249 6f 74 69 63 65 20 74 68 65 20 64 69 66 66 65 72  otice the differ
1624a 65 6e 63 65 0a 2a 2a 20 62 65 74 77 65 65 6e 20  ence.** between 
1624b 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29  moveToLeftmost()
1624c 20 61 6e 64 20 6d 6f 76 65 54 6f 52 69 67 68 74   and moveToRight
1624d 6d 6f 73 74 28 29 2e 20 20 6d 6f 76 65 54 6f 4c  most().  moveToL
1624e 65 66 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e  eftmost().** fin
1624f 64 73 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  ds the left-most
16250 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74   entry beneath t
16251 68 65 20 2a 65 6e 74 72 79 2a 20 77 68 65 72 65  he *entry* where
16252 61 73 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f  as moveToRightmo
16253 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68  st().** finds th
16254 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74  e right-most ent
16255 72 79 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a  ry beneath the *
16256 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  page*..**.** The
16257 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72   right-most entr
16258 79 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74  y is the one wit
16259 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65  h the largest ke
1625a 79 20 2d 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20  y - the last.** 
1625b 6b 65 79 20 69 6e 20 61 73 63 65 6e 64 69 6e 67  key in ascending
1625c 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69   order..*/.stati
1625d 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 69 67 68  c int moveToRigh
1625e 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a  tmost(BtCursor *
1625f 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67  pCur){.  Pgno pg
16260 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  no;.  int rc = S
16261 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50  QLITE_OK;.  MemP
16262 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  age *pPage = 0;.
16263 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
16264 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
16265 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
16266 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
16267 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77  SOR_VALID );.  w
16268 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
16269 5f 4f 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d  _OK && !(pPage =
1626a 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1626b 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61  ur->iPage])->lea
1626c 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20  f ){.    pgno = 
1626d 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
1626e 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
1626f 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20  rOffset+8]);.   
16270 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
16271 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67  r->iPage] = pPag
16272 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63  e->nCell;.    rc
16273 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70   = moveToChild(p
16274 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a  Cur, pgno);.  }.
16275 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
16276 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  _OK ){.    pCur-
16277 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
16278 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65  ge] = pPage->nCe
16279 6c 6c 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e  ll-1;.    pCur->
1627a 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a  info.nSize = 0;.
1627b 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e      pCur->validN
1627c 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  Key = 0;.  }.  r
1627d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
1627e 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
1627f 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74  to the first ent
16280 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ry in the table.
16281 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
16282 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73  OK.** on success
16283 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20  .  Set *pRes to 
16284 30 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20  0 if the cursor 
16285 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20  actually points 
16286 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20  to something.** 
16287 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20  or set *pRes to 
16288 31 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  1 if the table i
16289 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49  s empty..*/.SQLI
1628a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1628b 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 74  qlite3BtreeFirst
1628c 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
1628d 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69   int *pRes){.  i
1628e 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
1628f 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16290 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
16291 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
16292 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70  tex_held(pCur->p
16293 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78  Btree->db->mutex
16294 29 20 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65  ) );.  rc = move
16295 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20  ToRoot(pCur);.  
16296 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16297 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75  K ){.    if( pCu
16298 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
16299 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20  R_INVALID ){.   
1629a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
1629b 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1629c 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  age]->nCell==0 )
1629d 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  ;.      *pRes = 
1629e 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  1;.      rc = SQ
1629f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c  LITE_OK;.    }el
162a0 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
162a1 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  ( pCur->apPage[p
162a2 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65  Cur->iPage]->nCe
162a3 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 2a 70  ll>0 );.      *p
162a4 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  Res = 0;.      r
162a5 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  c = moveToLeftmo
162a6 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a  st(pCur);.    }.
162a7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
162a8 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20  .}../* Move the 
162a9 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61  cursor to the la
162aa 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
162ab 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53  table.  Return S
162ac 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73  QLITE_OK.** on s
162ad 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52  uccess.  Set *pR
162ae 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63  es to 0 if the c
162af 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70  ursor actually p
162b0 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69  oints to somethi
162b1 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52  ng.** or set *pR
162b2 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74  es to 1 if the t
162b3 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a  able is empty..*
162b4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
162b5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
162b6 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 20 2a  eLast(BtCursor *
162b7 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29  pCur, int *pRes)
162b8 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 0a 20 20  {.  int rc;. .  
162b9 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
162ba 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
162bb 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
162bc 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
162bd 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d  Cur->pBtree->db-
162be 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a  >mutex) );..  /*
162bf 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 61   If the cursor a
162c0 6c 72 65 61 64 79 20 70 6f 69 6e 74 73 20 74 6f  lready points to
162c1 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 2c   the last entry,
162c2 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   this is a no-op
162c3 2e 20 2a 2f 0a 20 20 69 66 28 20 43 55 52 53 4f  . */.  if( CURSO
162c4 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65  R_VALID==pCur->e
162c5 53 74 61 74 65 20 26 26 20 70 43 75 72 2d 3e 61  State && pCur->a
162c6 74 4c 61 73 74 20 29 7b 0a 23 69 66 64 65 66 20  tLast ){.#ifdef 
162c7 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
162c8 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73   /* This block s
162c9 65 72 76 65 73 20 74 6f 20 61 73 73 65 72 74 28  erves to assert(
162ca 29 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  ) that the curso
162cb 72 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 6f  r really does po
162cc 69 6e 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20 74  int .    ** to t
162cd 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
162ce 20 74 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f 0a   the b-tree. */.
162cf 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20      int ii;.    
162d0 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75  for(ii=0; ii<pCu
162d1 72 2d 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29 7b  r->iPage; ii++){
162d2 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
162d3 43 75 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d 3d  Cur->aiIdx[ii]==
162d4 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 69 5d  pCur->apPage[ii]
162d5 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 7d  ->nCell );.    }
162d6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
162d7 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
162d8 50 61 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 70 50  Page]==pCur->apP
162d9 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
162da 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20 20  ->nCell-1 );.   
162db 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
162dc 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
162dd 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 6e 64  e]->leaf );.#end
162de 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  if.    return SQ
162df 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
162e0 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28  rc = moveToRoot(
162e1 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d  pCur);.  if( rc=
162e2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
162e3 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56    if( CURSOR_INV
162e4 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61  ALID==pCur->eSta
162e5 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  te ){.      asse
162e6 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65  rt( pCur->apPage
162e7 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
162e8 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Cell==0 );.     
162e9 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20   *pRes = 1;.    
162ea 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
162eb 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
162ec 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
162ed 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  );.      *pRes =
162ee 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d   0;.      rc = m
162ef 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70  oveToRightmost(p
162f0 43 75 72 29 3b 0a 20 20 20 20 20 20 70 43 75 72  Cur);.      pCur
162f1 2d 3e 61 74 4c 61 73 74 20 3d 20 72 63 3d 3d 53  ->atLast = rc==S
162f2 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a 30 3b 0a 20  QLITE_OK ?1:0;. 
162f3 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
162f4 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65  n rc;.}../* Move
162f5 20 74 68 65 20 63 75 72 73 6f 72 20 73 6f 20 74   the cursor so t
162f6 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  hat it points to
162f7 20 61 6e 20 65 6e 74 72 79 20 6e 65 61 72 20 74   an entry near t
162f8 68 65 20 6b 65 79 20 0a 2a 2a 20 73 70 65 63 69  he key .** speci
162f9 66 69 65 64 20 62 79 20 70 49 64 78 4b 65 79 20  fied by pIdxKey 
162fa 6f 72 20 69 6e 74 4b 65 79 2e 20 20 20 52 65 74  or intKey.   Ret
162fb 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 63 6f  urn a success co
162fc 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 49 4e  de..**.** For IN
162fd 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 74 68 65  TKEY tables, the
162fe 20 69 6e 74 4b 65 79 20 70 61 72 61 6d 65 74 65   intKey paramete
162ff 72 20 69 73 20 75 73 65 64 2e 20 20 70 49 64 78  r is used.  pIdx
16300 4b 65 79 20 0a 2a 2a 20 6d 75 73 74 20 62 65 20  Key .** must be 
16301 4e 55 4c 4c 2e 20 20 46 6f 72 20 69 6e 64 65 78  NULL.  For index
16302 20 74 61 62 6c 65 73 2c 20 70 49 64 78 4b 65 79   tables, pIdxKey
16303 20 69 73 20 75 73 65 64 20 61 6e 64 20 69 6e 74   is used and int
16304 4b 65 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65  Key.** is ignore
16305 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  d..**.** If an e
16306 78 61 63 74 20 6d 61 74 63 68 20 69 73 20 6e 6f  xact match is no
16307 74 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68  t found, then th
16308 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61  e cursor is alwa
16309 79 73 0a 2a 2a 20 6c 65 66 74 20 70 6f 69 6e 74  ys.** left point
1630a 69 6e 67 20 61 74 20 61 20 6c 65 61 66 20 70 61  ing at a leaf pa
1630b 67 65 20 77 68 69 63 68 20 77 6f 75 6c 64 20 68  ge which would h
1630c 6f 6c 64 20 74 68 65 20 65 6e 74 72 79 20 69 66  old the entry if
1630d 20 69 74 0a 2a 2a 20 77 65 72 65 20 70 72 65 73   it.** were pres
1630e 65 6e 74 2e 20 20 54 68 65 20 63 75 72 73 6f 72  ent.  The cursor
1630f 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20   might point to 
16310 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 63 6f  an entry that co
16311 6d 65 73 0a 2a 2a 20 62 65 66 6f 72 65 20 6f 72  mes.** before or
16312 20 61 66 74 65 72 20 74 68 65 20 6b 65 79 2e 0a   after the key..
16313 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 67 65 72  **.** An integer
16314 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
16315 20 2a 70 52 65 73 20 77 68 69 63 68 20 69 73 20   *pRes which is 
16316 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
16317 20 63 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 6b   comparing the k
16318 65 79 20 77 69 74 68 20 74 68 65 20 65 6e 74 72  ey with the entr
16319 79 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 63  y to which the c
1631a 75 72 73 6f 72 20 69 73 20 0a 2a 2a 20 70 6f 69  ursor is .** poi
1631b 6e 74 69 6e 67 2e 20 20 54 68 65 20 6d 65 61 6e  nting.  The mean
1631c 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67  ing of the integ
1631d 65 72 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a  er written into.
1631e 2a 2a 20 2a 70 52 65 73 20 69 73 20 61 73 20 66  ** *pRes is as f
1631f 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
16320 20 20 2a 70 52 65 73 3c 30 20 20 20 20 20 20 54    *pRes<0      T
16321 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66  he cursor is lef
16322 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e  t pointing at an
16323 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20   entry that.**  
16324 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16325 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  is smaller than 
16326 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 20 6f  intKey/pIdxKey o
16327 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  r if the table i
16328 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20 20 20  s empty.**      
16329 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20              and 
1632a 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 74 68  the cursor is th
1632b 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70 6f 69  erefore left poi
1632c 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a  nt to nothing..*
1632d 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3d 3d  *.**     *pRes==
1632e 30 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72  0     The cursor
1632f 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
16330 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68  g at an entry th
16331 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  at.**           
16332 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 6d         exactly m
16333 61 74 63 68 65 73 20 69 6e 74 4b 65 79 2f 70 49  atches intKey/pI
16334 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  dxKey..**.**    
16335 20 2a 70 52 65 73 3e 30 20 20 20 20 20 20 54 68   *pRes>0      Th
16336 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
16337 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20   pointing at an 
16338 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20  entry that.**   
16339 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
1633a 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 6e  s larger than in
1633b 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a  tKey/pIdxKey..**
1633c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1633d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
1633e 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1633f 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  d(.  BtCursor *p
16340 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Cur,          /*
16341 20 54 68 65 20 63 75 72 73 6f 72 20 74 6f 20 62   The cursor to b
16342 65 20 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 6e 70  e moved */.  Unp
16343 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64  ackedRecord *pId
16344 78 4b 65 79 2c 20 2f 2a 20 55 6e 70 61 63 6b 65  xKey, /* Unpacke
16345 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20  d index key */. 
16346 20 69 36 34 20 69 6e 74 4b 65 79 2c 20 20 20 20   i64 intKey,    
16347 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
16348 20 74 61 62 6c 65 20 6b 65 79 20 2a 2f 0a 20 20   table key */.  
16349 69 6e 74 20 62 69 61 73 52 69 67 68 74 2c 20 20  int biasRight,  
1634a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
1634b 72 75 65 2c 20 62 69 61 73 20 74 68 65 20 73 65  rue, bias the se
1634c 61 72 63 68 20 74 6f 20 74 68 65 20 68 69 67 68  arch to the high
1634d 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70   end */.  int *p
1634e 52 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20  Res             
1634f 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72     /* Write sear
16350 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  ch results here 
16351 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
16352 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
16353 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
16354 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
16355 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
16356 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e  d(pCur->pBtree->
16357 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
16358 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f   /* If the curso
16359 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 73  r is already pos
1635a 69 74 69 6f 6e 65 64 20 61 74 20 74 68 65 20 70  itioned at the p
1635b 6f 69 6e 74 20 77 65 20 61 72 65 20 74 72 79 69  oint we are tryi
1635c 6e 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76 65 20  ng.  ** to move 
1635d 74 6f 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65  to, then just re
1635e 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69  turn without doi
1635f 6e 67 20 61 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20  ng any work */. 
16360 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
16361 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
16362 26 26 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b  && pCur->validNK
16363 65 79 20 0a 20 20 20 26 26 20 70 43 75 72 2d 3e  ey .   && pCur->
16364 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65  apPage[0]->intKe
16365 79 20 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20  y .  ){.    if( 
16366 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d  pCur->info.nKey=
16367 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20  =intKey ){.     
16368 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20   *pRes = 0;.    
16369 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1636a 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  OK;.    }.    if
1636b 28 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 26  ( pCur->atLast &
1636c 26 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65  & pCur->info.nKe
1636d 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  y<intKey ){.    
1636e 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20    *pRes = -1;.  
1636f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16370 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
16371 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f  .  rc = moveToRo
16372 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20  ot(pCur);.  if( 
16373 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  rc ){.    return
16374 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72   rc;.  }.  asser
16375 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
16376 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a  pCur->iPage] );.
16377 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16378 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
16379 67 65 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20  ge]->isInit );. 
1637a 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
1637b 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  e==CURSOR_INVALI
1637c 44 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d  D ){.    *pRes =
1637d 20 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28   -1;.    assert(
1637e 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1637f 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
16380 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75  l==0 );.    retu
16381 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
16382 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  }.  assert( pCur
16383 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74  ->apPage[0]->int
16384 4b 65 79 20 7c 7c 20 70 49 64 78 4b 65 79 20 29  Key || pIdxKey )
16385 3b 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20  ;.  for(;;){.   
16386 20 69 6e 74 20 6c 77 72 2c 20 75 70 72 3b 0a 20   int lwr, upr;. 
16387 20 20 20 50 67 6e 6f 20 63 68 6c 64 50 67 3b 0a     Pgno chldPg;.
16388 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61      MemPage *pPa
16389 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
1638a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
1638b 20 20 20 20 69 6e 74 20 63 20 3d 20 2d 31 3b 20      int c = -1; 
1638c 20 2f 2a 20 70 52 65 73 20 72 65 74 75 72 6e 20   /* pRes return 
1638d 69 66 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  if table is empt
1638e 79 20 6d 75 73 74 20 62 65 20 2d 31 20 2a 2f 0a  y must be -1 */.
1638f 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20 20 20      lwr = 0;.   
16390 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e 6e 43   upr = pPage->nC
16391 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 28  ell-1;.    if( (
16392 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26  !pPage->intKey &
16393 26 20 70 49 64 78 4b 65 79 3d 3d 30 29 20 7c 7c  & pIdxKey==0) ||
16394 20 75 70 72 3c 30 20 29 7b 0a 20 20 20 20 20 20   upr<0 ){.      
16395 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
16396 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
16397 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69  goto moveto_fini
16398 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  sh;.    }.    if
16399 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20  ( biasRight ){. 
1639a 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78       pCur->aiIdx
1639b 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20  [pCur->iPage] = 
1639c 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65  (u16)upr;.    }e
1639d 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d  lse{.      pCur-
1639e 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
1639f 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72  ge] = (u16)((upr
163a0 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a  +lwr)/2);.    }.
163a1 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20      for(;;){.   
163a2 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75     int idx = pCu
163a3 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
163a4 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20  Page]; /* Index 
163a5 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20  of current cell 
163a6 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20  in pPage */.    
163a7 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20    u8 *pCell;    
163a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
163a9 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
163aa 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c   to current cell
163ab 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20   in pPage */..  
163ac 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e      pCur->info.n
163ad 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Size = 0;.      
163ae 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
163af 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70  (pPage, idx) + p
163b0 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
163b1 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50  ze;.      if( pP
163b2 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
163b3 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c         i64 nCell
163b4 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Key;.        if(
163b5 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20   pPage->hasData 
163b6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32  ){.          u32
163b7 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20   dummy;.        
163b8 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61    pCell += getVa
163b9 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75  rint32(pCell, du
163ba 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  mmy);.        }.
163bb 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e          getVarin
163bc 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26  t(pCell, (u64*)&
163bd 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20  nCellKey);.     
163be 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d     if( nCellKey=
163bf 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20  =intKey ){.     
163c0 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20       c = 0;.    
163c1 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43      }else if( nC
163c2 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b  ellKey<intKey ){
163c3 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d  .          c = -
163c4 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
163c5 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
163c6 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74  rt( nCellKey>int
163c7 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20  Key );.         
163c8 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20   c = +1;.       
163c9 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d   }.        pCur-
163ca 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a  >validNKey = 1;.
163cb 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e          pCur->in
163cc 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b  fo.nKey = nCellK
163cd 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ey;.      }else{
163ce 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
163cf 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65  maximum supporte
163d0 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33  d page-size is 3
163d1 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 73  2768 bytes. This
163d2 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20   means that.    
163d3 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d      ** the maxim
163d4 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63  um number of rec
163d5 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64  ord bytes stored
163d6 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54   on an index B-T
163d7 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  ree.        ** p
163d8 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38  age is at most 8
163d9 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 68  198 bytes, which
163da 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61   may be stored a
163db 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20  s a 2-byte.     
163dc 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68     ** varint. Th
163dd 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  is information i
163de 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70  s used to attemp
163df 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69  t to avoid parsi
163e0 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ng .        ** t
163e1 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62  he entire cell b
163e2 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74  y checking for t
163e3 68 65 20 63 61 73 65 73 20 77 68 65 72 65 20 74  he cases where t
163e4 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20  he record is .  
163e5 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20        ** stored 
163e6 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20  entirely within 
163e7 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 20  the b-tree page 
163e8 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68  by inspecting th
163e9 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 20  e first .       
163ea 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74   ** 2 bytes of t
163eb 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20  he cell..       
163ec 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20   */.        int 
163ed 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d  nCell = pCell[0]
163ee 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 28  ;.        if( !(
163ef 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26  nCell & 0x80) &&
163f0 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d   nCell<=pPage->m
163f1 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20  axLocal ){.     
163f2 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
163f3 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 20  nch runs if the 
163f4 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c  record-size fiel
163f5 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73  d of the cell is
163f6 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20   a.          ** 
163f7 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69  single byte vari
163f8 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72  nt and the recor
163f9 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20  d fits entirely 
163fa 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20  on the main.    
163fb 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20        ** b-tree 
163fc 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20  page.  */.      
163fd 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
163fe 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
163ff 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26  (nCell, (void*)&
16400 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65  pCell[1], pIdxKe
16401 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  y);.        }els
16402 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d  e if( !(pCell[1]
16403 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20   & 0x80) .      
16404 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20      && (nCell = 
16405 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37  ((nCell&0x7f)<<7
16406 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70  ) + pCell[1])<=p
16407 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20  Page->maxLocal. 
16408 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
16409 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72      /* The recor
1640a 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20  d-size field is 
1640b 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20  a 2 byte varint 
1640c 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a  and the record .
1640d 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74            ** fit
1640e 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68  s entirely on th
1640f 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61  e main b-tree pa
16410 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  ge.  */.        
16411 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62    c = sqlite3Vdb
16412 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e  eRecordCompare(n
16413 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43  Cell, (void*)&pC
16414 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29  ell[2], pIdxKey)
16415 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
16416 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
16417 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f  e record flows o
16418 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20  ver onto one or 
16419 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  more overflow pa
1641a 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20  ges. In.        
1641b 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74    ** this case t
1641c 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65  he whole cell ne
1641d 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 64  eds to be parsed
1641e 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63  , a buffer alloc
1641f 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a  ated.          *
16420 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c  * and accessPayl
16421 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 65  oad() used to re
16422 74 72 69 65 76 65 20 74 68 65 20 72 65 63 6f 72  trieve the recor
16423 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20  d into the.     
16424 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62       ** buffer b
16425 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64  efore VdbeRecord
16426 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65  Compare() can be
16427 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20   called. */.    
16428 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c        void *pCel
16429 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20  lKey;.          
1642a 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c  u8 * const pCell
1642b 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70  Body = pCell - p
1642c 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
1642d 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  ze;.          sq
1642e 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
1642f 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
16430 65 6c 6c 42 6f 64 79 2c 20 26 70 43 75 72 2d 3e  ellBody, &pCur->
16431 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 20  info);.         
16432 20 6e 43 65 6c 6c 20 3d 20 28 69 6e 74 29 70 43   nCell = (int)pC
16433 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20  ur->info.nKey;. 
16434 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c 4b 65           pCellKe
16435 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  y = sqlite3Mallo
16436 63 28 20 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  c( nCell );.    
16437 20 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c 4b        if( pCellK
16438 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ey==0 ){.       
16439 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1643a 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
1643b 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f      goto moveto_
1643c 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20  finish;.        
1643d 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63    }.          rc
1643e 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64   = accessPayload
1643f 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 6c 6c 2c  (pCur, 0, nCell,
16440 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a   (unsigned char*
16441 29 70 43 65 6c 6c 4b 65 79 2c 20 30 2c 20 30 29  )pCellKey, 0, 0)
16442 3b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20  ;.          c = 
16443 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
16444 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20  dCompare(nCell, 
16445 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65  pCellKey, pIdxKe
16446 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  y);.          sq
16447 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c  lite3_free(pCell
16448 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Key);.          
16449 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f  if( rc ) goto mo
1644a 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20  veto_finish;.   
1644b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1644c 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b       if( c==0 ){
1644d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
1644e 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70  ge->intKey && !p
1644f 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
16450 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64          lwr = id
16451 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75 70 72  x;.          upr
16452 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20 20 20   = lwr - 1;.    
16453 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
16454 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
16455 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b        *pRes = 0;
16456 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
16457 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
16458 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f       goto moveto
16459 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20  _finish;.       
1645a 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1645b 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20   if( c<0 ){.    
1645c 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b      lwr = idx+1;
1645d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1645e 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d        upr = idx-
1645f 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
16460 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a   if( lwr>upr ){.
16461 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
16462 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75       }.      pCu
16463 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
16464 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 6c  Page] = (u16)((l
16465 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20 20  wr+upr)/2);.    
16466 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 77  }.    assert( lw
16467 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20 20  r==upr+1 );.    
16468 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
16469 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66 28  sInit );.    if(
1646a 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a   pPage->leaf ){.
1646b 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 30        chldPg = 0
1646c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1646d 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  lwr>=pPage->nCel
1646e 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50  l ){.      chldP
1646f 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  g = get4byte(&pP
16470 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
16471 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
16472 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
16473 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62    chldPg = get4b
16474 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61  yte(findCell(pPa
16475 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20 7d  ge, lwr));.    }
16476 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67 3d  .    if( chldPg=
16477 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
16478 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
16479 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75  pCur->iPage]<pCu
1647a 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
1647b 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b  iPage]->nCell );
1647c 0a 20 20 20 20 20 20 69 66 28 20 70 52 65 73 20  .      if( pRes 
1647d 29 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20 20 20  ) *pRes = c;.   
1647e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1647f 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f  K;.      goto mo
16480 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20  veto_finish;.   
16481 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49   }.    pCur->aiI
16482 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
16483 3d 20 28 75 31 36 29 6c 77 72 3b 0a 20 20 20 20  = (u16)lwr;.    
16484 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
16485 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e   = 0;.    pCur->
16486 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
16487 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
16488 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50 67  ild(pCur, chldPg
16489 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
1648a 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69  goto moveto_fini
1648b 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f 5f 66  sh;.  }.moveto_f
1648c 69 6e 69 73 68 3a 0a 20 20 72 65 74 75 72 6e 20  inish:.  return 
1648d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20  rc;.}../*.** In 
1648e 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  this version of 
1648f 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65  BtreeMoveto, pKe
16490 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e  y is a packed in
16491 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75  dex record.** su
16492 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 74  ch as is generat
16493 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b  ed by the OP_Mak
16494 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20  eRecord opcode. 
16495 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72   Unpack the.** r
16496 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63  ecord and then c
16497 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55  all BtreeMovetoU
16498 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20  npacked() to do 
16499 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 53 51 4c  the work..*/.SQL
1649a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1649b 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1649c 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  to(.  BtCursor *
1649d 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72  pCur,     /* Cur
1649e 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  sor open on the 
1649f 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 72  btree to be sear
164a0 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ched */.  const 
164a1 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a  void *pKey,   /*
164a2 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 74   Packed key if t
164a3 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 69  he btree is an i
164a4 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e 4b  ndex */.  i64 nK
164a5 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey,           /*
164a6 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72   Integer key for
164a7 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 6f   tables.  Size o
164a8 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 63  f pKey for indic
164a9 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73  es */.  int bias
164aa 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ,           /* B
164ab 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 68  ias search to th
164ac 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20  e high end */.  
164ad 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20  int *pRes       
164ae 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61      /* Write sea
164af 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65  rch results here
164b0 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
164b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164b2 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 6f      /* Status co
164b3 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64  de */.  Unpacked
164b4 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b  Record *pIdxKey;
164b5 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69     /* Unpacked i
164b6 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 68  ndex key */.  ch
164b7 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b 20  ar aSpace[150]; 
164b8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
164b9 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 4b   space for pIdxK
164ba 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 20  ey - to avoid a 
164bb 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 0a 20 20 69 66  malloc */...  if
164bc 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73  ( pKey ){.    as
164bd 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34  sert( nKey==(i64
164be 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20  )(int)nKey );.  
164bf 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69    pIdxKey = sqli
164c0 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
164c1 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e  ack(pCur->pKeyIn
164c2 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70  fo, (int)nKey, p
164c3 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
164c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164c5 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63             aSpac
164c6 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65  e, sizeof(aSpace
164c7 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78  ));.    if( pIdx
164c8 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Key==0 ) return 
164c9 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
164ca 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b  }else{.    pIdxK
164cb 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63  ey = 0;.  }.  rc
164cc 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
164cd 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43  ovetoUnpacked(pC
164ce 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65  ur, pIdxKey, nKe
164cf 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a  y, bias, pRes);.
164d0 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20    if( pKey ){.  
164d1 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
164d2 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
164d3 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a  d(pIdxKey);.  }.
164d4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
164d5 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
164d6 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72  UE if the cursor
164d7 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67   is not pointing
164d8 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20   at an entry of 
164d9 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  the table..**.**
164da 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65   TRUE will be re
164db 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63  turned after a c
164dc 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74  all to sqlite3Bt
164dd 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a  reeNext() moves.
164de 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74  ** past the last
164df 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61   entry in the ta
164e0 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74  ble or sqlite3Bt
164e1 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20  reePrev() moves 
164e2 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73  past.** the firs
164e3 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69  t entry.  TRUE i
164e4 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20  s also returned 
164e5 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
164e6 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  empty..*/.SQLITE
164e7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
164e8 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43  ite3BtreeEof(BtC
164e9 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
164ea 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66  /* TODO: What if
164eb 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69   the cursor is i
164ec 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  n CURSOR_REQUIRE
164ed 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62  SEEK but all tab
164ee 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20  le entries.  ** 
164ef 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65  have been delete
164f0 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c  d? This API will
164f1 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20   need to change 
164f2 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  to return an err
164f3 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20  or code.  ** as 
164f4 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c  well as the bool
164f5 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65  ean result value
164f6 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ..  */.  return 
164f7 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70  (CURSOR_VALID!=p
164f8 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a  Cur->eState);.}.
164f9 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
164fa 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
164fb 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72  ction handle for
164fc 20 61 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51   a cursor..*/.SQ
164fd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
164fe 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42 74 72  ite3 *sqlite3Btr
164ff 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e 73 74  eeCursorDb(const
16500 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29   BtCursor *pCur)
16501 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
16502 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
16503 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d  Cur->pBtree->db-
16504 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74  >mutex) );.  ret
16505 75 72 6e 20 70 43 75 72 2d 3e 70 42 74 72 65 65  urn pCur->pBtree
16506 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ->db;.}../*.** A
16507 64 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f  dvance the curso
16508 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e  r to the next en
16509 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
1650a 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63  ase.  If.** succ
1650b 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20  essful then set 
1650c 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65  *pRes=0.  If the
1650d 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61   cursor.** was a
1650e 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20  lready pointing 
1650f 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  to the last entr
16510 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
16511 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73  e before.** this
16512 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
16513 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70  led, then set *p
16514 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Res=1..*/.SQLITE
16515 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16516 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74  ite3BtreeNext(Bt
16517 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
16518 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20  t *pRes){.  int 
16519 72 63 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20  rc;.  int idx;. 
1651a 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
1651b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
1651c 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
1651d 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73  r) );.  rc = res
1651e 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
1651f 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20  on(pCur);.  if( 
16520 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
16521 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
16522 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 52    }.  assert( pR
16523 65 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 43  es!=0 );.  if( C
16524 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70  URSOR_INVALID==p
16525 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20  Cur->eState ){. 
16526 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20     *pRes = 1;.  
16527 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
16528 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  OK;.  }.  if( pC
16529 75 72 2d 3e 73 6b 69 70 3e 30 20 29 7b 0a 20 20  ur->skip>0 ){.  
1652a 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30    pCur->skip = 0
1652b 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b  ;.    *pRes = 0;
1652c 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1652d 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75  TE_OK;.  }.  pCu
1652e 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 0a 20 20  r->skip = 0;..  
1652f 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
16530 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
16531 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43 75  ];.  idx = ++pCu
16532 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
16533 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28  Page];.  assert(
16534 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
16535 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c  ;.  assert( idx<
16536 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b  =pPage->nCell );
16537 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  ..  pCur->info.n
16538 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72  Size = 0;.  pCur
16539 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
1653a 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61 67  .  if( idx>=pPag
1653b 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20  e->nCell ){.    
1653c 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
1653d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d   ){.      rc = m
1653e 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c  oveToChild(pCur,
1653f 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
16540 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
16541 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20  drOffset+8]));. 
16542 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
16543 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 72  turn rc;.      r
16544 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  c = moveToLeftmo
16545 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20  st(pCur);.      
16546 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20  *pRes = 0;.     
16547 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
16548 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20  }.    do{.      
16549 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d  if( pCur->iPage=
1654a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  =0 ){.        *p
1654b 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Res = 1;.       
1654c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
1654d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
1654e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
1654f 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
16550 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  }.      sqlite3B
16551 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74  treeMoveToParent
16552 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50  (pCur);.      pP
16553 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
16554 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
16555 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75  .    }while( pCu
16556 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
16557 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43  Page]>=pPage->nC
16558 65 6c 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73  ell );.    *pRes
16559 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50   = 0;.    if( pP
1655a 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
1655b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1655c 33 42 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c  3BtreeNext(pCur,
1655d 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73   pRes);.    }els
1655e 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  e{.      rc = SQ
1655f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
16560 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
16561 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20  }.  *pRes = 0;. 
16562 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66   if( pPage->leaf
16563 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16564 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
16565 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d  rc = moveToLeftm
16566 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74  ost(pCur);.  ret
16567 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
16568 2a 20 53 74 65 70 20 74 68 65 20 63 75 72 73 6f  * Step the curso
16569 72 20 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f  r to the back to
1656a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e   the previous en
1656b 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
1656c 61 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63  ase.  If.** succ
1656d 65 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20  essful then set 
1656e 2a 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65  *pRes=0.  If the
1656f 20 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61   cursor.** was a
16570 6c 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20  lready pointing 
16571 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74  to the first ent
16572 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ry in the databa
16573 73 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69  se before.** thi
16574 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61  s routine was ca
16575 6c 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a  lled, then set *
16576 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54  pRes=1..*/.SQLIT
16577 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
16578 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f  lite3BtreePrevio
16579 75 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  us(BtCursor *pCu
1657a 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20  r, int *pRes){. 
1657b 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61   int rc;.  MemPa
1657c 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73  ge *pPage;..  as
1657d 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
1657e 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
1657f 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75    rc = restoreCu
16580 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75  rsorPosition(pCu
16581 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  r);.  if( rc!=SQ
16582 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
16583 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
16584 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30  pCur->atLast = 0
16585 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49  ;.  if( CURSOR_I
16586 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53  NVALID==pCur->eS
16587 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65  tate ){.    *pRe
16588 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  s = 1;.    retur
16589 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1658a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69  .  if( pCur->ski
1658b 70 3c 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  p<0 ){.    pCur-
1658c 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20 20 20 2a  >skip = 0;.    *
1658d 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65  pRes = 0;.    re
1658e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1658f 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70    }.  pCur->skip
16590 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d   = 0;..  pPage =
16591 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
16592 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73  ur->iPage];.  as
16593 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
16594 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70 50  nit );.  if( !pP
16595 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
16596 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d   int idx = pCur-
16597 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
16598 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f  ge];.    rc = mo
16599 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
1659a 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c  get4byte(findCel
1659b 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29 3b  l(pPage, idx)));
1659c 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
1659d 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1659e 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d      }.    rc = m
1659f 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70  oveToRightmost(p
165a0 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Cur);.  }else{. 
165a1 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d 3e     while( pCur->
165a2 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
165a3 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  e]==0 ){.      i
165a4 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d  f( pCur->iPage==
165a5 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75  0 ){.        pCu
165a6 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
165a7 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20  OR_INVALID;.    
165a8 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20      *pRes = 1;. 
165a9 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
165aa 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
165ab 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
165ac 72 65 65 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28  reeMoveToParent(
165ad 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pCur);.    }.   
165ae 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
165af 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d  e = 0;.    pCur-
165b0 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
165b1 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78  .    pCur->aiIdx
165b2 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b  [pCur->iPage]--;
165b3 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75  .    pPage = pCu
165b4 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
165b5 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20  iPage];.    if( 
165b6 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26  pPage->intKey &&
165b7 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
165b8 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
165b9 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
165ba 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20  (pCur, pRes);.  
165bb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
165bc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
165bd 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73     }.  }.  *pRes
165be 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72   = 0;.  return r
165bf 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  c;.}../*.** Allo
165c0 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  cate a new page 
165c1 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
165c2 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
165c3 65 20 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61  e new page is ma
165c4 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20  rked as dirty.  
165c5 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
165c6 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
165c7 74 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65  te().** has alre
165c8 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  ady been called 
165c9 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e  on the new page.
165ca 29 20 20 54 68 65 20 6e 65 77 20 70 61 67 65 20  )  The new page 
165cb 68 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e  has also.** been
165cc 20 72 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20   referenced and 
165cd 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  the calling rout
165ce 69 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ine is responsib
165cf 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a  le for calling.*
165d0 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e  * sqlite3PagerUn
165d1 72 65 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77  ref() on the new
165d2 20 70 61 67 65 20 77 68 65 6e 20 69 74 20 69 73   page when it is
165d3 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   done..**.** SQL
165d4 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
165d5 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  ed on success.  
165d6 41 6e 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e  Any other return
165d7 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
165d8 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a  .** an error.  *
165d9 70 70 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e  ppPage and *pPgn
165da 6f 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20  o are undefined 
165db 69 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20  in the event of 
165dc 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20  an error..** Do 
165dd 6e 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  not invoke sqlit
165de 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f  e3PagerUnref() o
165df 6e 20 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20  n *ppPage if an 
165e0 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
165e1 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
165e2 22 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 74  "nearby" paramet
165e3 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65  er is not 0, the
165e4 6e 20 61 20 28 66 65 65 62 6c 65 29 20 65 66 66  n a (feeble) eff
165e5 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a  ort is made to .
165e6 2a 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65  ** locate a page
165e7 20 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61   close to the pa
165e8 67 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62  ge number "nearb
165e9 79 22 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  y".  This can be
165ea 20 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61   used in an.** a
165eb 74 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72  ttempt to keep r
165ec 65 6c 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f  elated pages clo
165ed 73 65 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72  se to each other
165ee 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
165ef 20 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20   file,.** which 
165f0 69 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65  in turn can make
165f1 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73   database access
165f2 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49   faster..**.** I
165f3 66 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61  f the "exact" pa
165f4 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30  rameter is not 0
165f5 2c 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e  , and the page-n
165f6 75 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69  umber nearby exi
165f7 73 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65  sts .** anywhere
165f8 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   on the free-lis
165f9 74 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75  t, then it is gu
165fa 61 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72  arenteed to be r
165fb 65 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a  eturned. This.**
165fc 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79   is only used by
165fd 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
165fe 61 62 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f  abases when allo
165ff 63 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62  cating a new tab
16600 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
16601 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  t allocateBtreeP
16602 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20  age(.  BtShared 
16603 2a 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65  *pBt, .  MemPage
16604 20 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67   **ppPage, .  Pg
16605 6e 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67  no *pPgno, .  Pg
16606 6e 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20  no nearby,.  u8 
16607 65 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61  exact.){.  MemPa
16608 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e  ge *pPage1;.  in
16609 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 20 20  t rc;.  int n;  
1660a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1660b 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65  pages on the fre
1660c 65 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6b  elist */.  int k
1660d 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
1660e 6f 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65  of leaves on the
1660f 20 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72   trunk of the fr
16610 65 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50  eelist */.  MemP
16611 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b  age *pTrunk = 0;
16612 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65  .  MemPage *pPre
16613 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61  vTrunk = 0;..  a
16614 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
16615 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
16616 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65  utex) );.  pPage
16617 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b  1 = pBt->pPage1;
16618 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28  .  n = get4byte(
16619 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
1661a 36 5d 29 3b 0a 20 20 69 66 28 20 6e 3e 30 20 29  6]);.  if( n>0 )
1661b 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61  {.    /* There a
1661c 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20  re pages on the 
1661d 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65  freelist.  Reuse
1661e 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61   one of those pa
1661f 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f  ges. */.    Pgno
16620 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20   iTrunk;.    u8 
16621 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20  searchList = 0; 
16622 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c  /* If the free-l
16623 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72  ist must be sear
16624 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79  ched for 'nearby
16625 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a  ' */.    .    /*
16626 20 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20   If the 'exact' 
16627 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72  parameter was tr
16628 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f  ue and a query o
16629 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  f the pointer-ma
1662a 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74  p.    ** shows t
1662b 68 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65  hat the page 'ne
1662c 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65  arby' is somewhe
1662d 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c  re on the free-l
1662e 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  ist, then.    **
1662f 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74   the entire-list
16630 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65   will be searche
16631 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e  d for that page.
16632 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  .    */.#ifndef 
16633 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
16634 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65  VACUUM.    if( e
16635 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d  xact && nearby<=
16636 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
16637 42 74 29 20 29 7b 0a 20 20 20 20 20 20 75 38 20  Bt) ){.      u8 
16638 65 54 79 70 65 3b 0a 20 20 20 20 20 20 61 73 73  eType;.      ass
16639 65 72 74 28 20 6e 65 61 72 62 79 3e 30 20 29 3b  ert( nearby>0 );
1663a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1663b 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
1663c 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  ;.      rc = ptr
1663d 6d 61 70 47 65 74 28 70 42 74 2c 20 6e 65 61 72  mapGet(pBt, near
1663e 62 79 2c 20 26 65 54 79 70 65 2c 20 30 29 3b 0a  by, &eType, 0);.
1663f 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
16640 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
16641 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
16642 50 5f 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20  P_FREEPAGE ){.  
16643 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 74        searchList
16644 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
16645 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 65 61      *pPgno = nea
16646 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  rby;.    }.#endi
16647 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d  f..    /* Decrem
16648 65 6e 74 20 74 68 65 20 66 72 65 65 2d 6c 69 73  ent the free-lis
16649 74 20 63 6f 75 6e 74 20 62 79 20 31 2e 20 53 65  t count by 1. Se
1664a 74 20 69 54 72 75 6e 6b 20 74 6f 20 74 68 65 20  t iTrunk to the 
1664b 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 20 20 20  index of the.   
1664c 20 2a 2a 20 66 69 72 73 74 20 66 72 65 65 2d 6c   ** first free-l
1664d 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20  ist trunk page. 
1664e 69 50 72 65 76 54 72 75 6e 6b 20 69 73 20 69 6e  iPrevTrunk is in
1664f 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a  itially 1..    *
16650 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
16651 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
16652 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge1->pDbPage);. 
16653 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
16654 72 6e 20 72 63 3b 0a 20 20 20 20 70 75 74 34 62  rn rc;.    put4b
16655 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
16656 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20  ta[36], n-1);.. 
16657 20 20 20 2f 2a 20 54 68 65 20 63 6f 64 65 20 77     /* The code w
16658 69 74 68 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20  ithin this loop 
16659 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65  is run only once
1665a 20 69 66 20 74 68 65 20 27 73 65 61 72 63 68 4c   if the 'searchL
1665b 69 73 74 27 20 76 61 72 69 61 62 6c 65 0a 20 20  ist' variable.  
1665c 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74 72 75 65    ** is not true
1665d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20  . Otherwise, it 
1665e 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61  runs once for ea
1665f 63 68 20 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e  ch trunk-page on
16660 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 72 65 65   the.    ** free
16661 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74 68 65 20  -list until the 
16662 70 61 67 65 20 27 6e 65 61 72 62 79 27 20 69 73  page 'nearby' is
16663 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20 20 2a 2f   located..    */
16664 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20  .    do {.      
16665 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 70 54 72  pPrevTrunk = pTr
16666 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 70  unk;.      if( p
16667 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20  PrevTrunk ){.   
16668 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65       iTrunk = ge
16669 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75  t4byte(&pPrevTru
1666a 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20  nk->aData[0]);. 
1666b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1666c 20 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74      iTrunk = get
1666d 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
1666e 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20  Data[32]);.     
1666f 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   }.      rc = sq
16670 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
16671 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26  e(pBt, iTrunk, &
16672 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20  pTrunk, 0);.    
16673 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
16674 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a      pTrunk = 0;.
16675 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
16676 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
16677 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 6b        }..      k
16678 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72   = get4byte(&pTr
16679 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a  unk->aData[4]);.
1667a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 26        if( k==0 &
1667b 26 20 21 73 65 61 72 63 68 4c 69 73 74 20 29 7b  & !searchList ){
1667c 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
1667d 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c 65 61  trunk has no lea
1667e 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69 73 74  ves and the list
1667f 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 73 65   is not being se
16680 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20 20 20  arched. .       
16681 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74 20 74   ** So extract t
16682 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 74  he trunk page it
16683 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69 74 20  self and use it 
16684 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a 20 20  as the newly .  
16685 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74        ** allocat
16686 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20  ed page */.     
16687 20 20 20 61 73 73 65 72 74 28 20 70 50 72 65 76     assert( pPrev
16688 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20 20 20  Trunk==0 );.    
16689 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1668a 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
1668b 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
1668c 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
1668d 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
1668e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
1668f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
16690 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54 72 75     *pPgno = iTru
16691 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  nk;.        memc
16692 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  py(&pPage1->aDat
16693 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  a[32], &pTrunk->
16694 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20  aData[0], 4);.  
16695 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20        *ppPage = 
16696 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20  pTrunk;.        
16697 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20  pTrunk = 0;.    
16698 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f      TRACE(("ALLO
16699 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d  CATE: %d trunk -
1669a 20 25 64 20 66 72 65 65 20 70 61 67 65 73 20 6c   %d free pages l
1669b 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20  eft\n", *pPgno, 
1669c 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c  n-1));.      }el
1669d 73 65 20 69 66 28 20 6b 3e 70 42 74 2d 3e 75 73  se if( k>pBt->us
1669e 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 29  ableSize/4 - 2 )
1669f 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c  {.        /* Val
166a0 75 65 20 6f 66 20 6b 20 69 73 20 6f 75 74 20 6f  ue of k is out o
166a1 66 20 72 61 6e 67 65 2e 20 20 44 61 74 61 62 61  f range.  Databa
166a2 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f  se corruption */
166a3 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
166a4 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
166a5 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  T;.        goto 
166a6 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
166a7 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e;.#ifndef SQLIT
166a8 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
166a9 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  M.      }else if
166aa 28 20 73 65 61 72 63 68 4c 69 73 74 20 26 26 20  ( searchList && 
166ab 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b 20 29  nearby==iTrunk )
166ac 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
166ad 20 6c 69 73 74 20 69 73 20 62 65 69 6e 67 20 73   list is being s
166ae 65 61 72 63 68 65 64 20 61 6e 64 20 74 68 69 73  earched and this
166af 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 74   trunk page is t
166b0 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20  he page.        
166b1 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2c 20  ** to allocate, 
166b2 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
166b3 65 74 68 65 72 20 69 74 20 68 61 73 20 6c 65 61  ether it has lea
166b4 76 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ves..        */.
166b5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
166b6 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29  *pPgno==iTrunk )
166b7 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67  ;.        *ppPag
166b8 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20  e = pTrunk;.    
166b9 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d      searchList =
166ba 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   0;.        rc =
166bb 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
166bc 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61  te(pTrunk->pDbPa
166bd 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ge);.        if(
166be 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20   rc ){.         
166bf 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
166c0 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20  te_page;.       
166c1 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6b   }.        if( k
166c2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
166c3 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b   if( !pPrevTrunk
166c4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
166c5 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e  memcpy(&pPage1->
166c6 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75  aData[32], &pTru
166c7 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29  nk->aData[0], 4)
166c8 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
166c9 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d  e{.            m
166ca 65 6d 63 70 79 28 26 70 50 72 65 76 54 72 75 6e  emcpy(&pPrevTrun
166cb 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54  k->aData[0], &pT
166cc 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20  runk->aData[0], 
166cd 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  4);.          }.
166ce 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
166cf 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
166d0 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65  trunk page is re
166d1 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63 61  quired by the ca
166d2 6c 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74  ller but it cont
166d3 61 69 6e 73 20 0a 20 20 20 20 20 20 20 20 20 20  ains .          
166d4 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66  ** pointers to f
166d5 72 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e  ree-list leaves.
166d6 20 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 20   The first leaf 
166d7 62 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0a  becomes a trunk.
166d8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67            ** pag
166d9 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a  e in this case..
166da 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
166db 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a         MemPage *
166dc 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 20  pNewTrunk;.     
166dd 20 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 72       Pgno iNewTr
166de 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26  unk = get4byte(&
166df 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d  pTrunk->aData[8]
166e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
166e1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
166e2 74 50 61 67 65 28 70 42 74 2c 20 69 4e 65 77 54  tPage(pBt, iNewT
166e3 72 75 6e 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b  runk, &pNewTrunk
166e4 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
166e5 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
166e6 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
166e7 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
166e8 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20  te_page;.       
166e9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72     }.          r
166ea 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
166eb 57 72 69 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d  Write(pNewTrunk-
166ec 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
166ed 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
166ee 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
166ef 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
166f0 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20  e(pNewTrunk);.  
166f1 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
166f2 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
166f3 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
166f4 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
166f5 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61  pNewTrunk->aData
166f6 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44  [0], &pTrunk->aD
166f7 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20  ata[0], 4);.    
166f8 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
166f9 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61  pNewTrunk->aData
166fa 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20  [4], k-1);.     
166fb 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65       memcpy(&pNe
166fc 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d  wTrunk->aData[8]
166fd 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  , &pTrunk->aData
166fe 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a  [12], (k-1)*4);.
166ff 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73            releas
16700 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29  ePage(pNewTrunk)
16701 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
16702 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20  !pPrevTrunk ){. 
16703 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
16704 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
16705 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
16706 31 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  1->pDbPage) );. 
16707 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62             put4b
16708 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
16709 74 61 5b 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e  ta[32], iNewTrun
1670a 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  k);.          }e
1670b 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
1670c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1670d 65 72 57 72 69 74 65 28 70 50 72 65 76 54 72 75  erWrite(pPrevTru
1670e 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  nk->pDbPage);.  
1670f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
16710 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
16711 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63    goto end_alloc
16712 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20  ate_page;.      
16713 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16714 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50      put4byte(&pP
16715 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  revTrunk->aData[
16716 30 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a  0], iNewTrunk);.
16717 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
16718 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 54      }.        pT
16719 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20  runk = 0;.      
1671a 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
1671b 54 45 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25  TE: %d trunk - %
1671c 64 20 66 72 65 65 20 70 61 67 65 73 20 6c 65 66  d free pages lef
1671d 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d  t\n", *pPgno, n-
1671e 31 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  1));.#endif.    
1671f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
16720 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65   /* Extract a le
16721 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e  af from the trun
16722 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74  k */.        int
16723 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20   closest;.      
16724 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20    Pgno iPage;.  
16725 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63        unsigned c
16726 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72  har *aData = pTr
16727 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  unk->aData;.    
16728 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16729 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
1672a 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
1672b 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
1672c 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
1672d 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
1672e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1672f 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20     if( nearby>0 
16730 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
16731 20 69 2c 20 64 69 73 74 3b 0a 20 20 20 20 20 20   i, dist;.      
16732 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b      closest = 0;
16733 0a 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20  .          dist 
16734 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74  = get4byte(&aDat
16735 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a  a[8]) - nearby;.
16736 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64 69            if( di
16737 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64  st<0 ) dist = -d
16738 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66  ist;.          f
16739 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b  or(i=1; i<k; i++
1673a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
1673b 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79 74 65  nt d2 = get4byte
1673c 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20  (&aData[8+i*4]) 
1673d 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20  - nearby;.      
1673e 20 20 20 20 20 20 69 66 28 20 64 32 3c 30 20 29        if( d2<0 )
1673f 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20   d2 = -d2;.     
16740 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 64 69         if( d2<di
16741 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  st ){.          
16742 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b      closest = i;
16743 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64  .              d
16744 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20  ist = d2;.      
16745 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16746 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
16747 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f  e{.          clo
16748 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  sest = 0;.      
16749 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61    }..        iPa
1674a 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61  ge = get4byte(&a
1674b 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34  Data[8+closest*4
1674c 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]);.        if( 
1674d 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69  !searchList || i
1674e 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a  Page==nearby ){.
1674f 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f            int no
16750 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20  Content;.       
16751 20 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 0a 20     Pgno nPage;. 
16752 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20           *pPgno 
16753 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20  = iPage;.       
16754 20 20 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72     nPage = pager
16755 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a  Pagecount(pBt);.
16756 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70            if( *p
16757 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b 0a 20 20  Pgno>nPage ){.  
16758 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65            /* Fre
16759 65 20 70 61 67 65 20 6f 66 66 20 74 68 65 20 65  e page off the e
1675a 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  nd of the file *
1675b 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  /.            rc
1675c 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
1675d 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20  T_BKPT;.        
1675e 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
1675f 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
16760 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16761 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
16762 54 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20  TE: %d was leaf 
16763 25 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e  %d of %d on trun
16764 6b 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20  k %d".          
16765 20 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72         ": %d mor
16766 65 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c  e free pages\n",
16767 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
16768 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73    *pPgno, closes
16769 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e  t+1, k, pTrunk->
1676a 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20  pgno, n-1));.   
1676b 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65         if( close
1676c 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20  st<k-1 ){.      
1676d 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44        memcpy(&aD
1676e 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d  ata[8+closest*4]
1676f 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c  , &aData[4+k*4],
16770 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   4);.          }
16771 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62  .          put4b
16772 79 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b  yte(&aData[4], k
16773 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  -1);.          a
16774 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
16775 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
16776 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 20  Trunk->pDbPage) 
16777 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43  );.          noC
16778 6f 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47  ontent = !btreeG
16779 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74  etHasContent(pBt
1677a 2c 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 20 20  , *pPgno);.     
1677b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1677c 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
1677d 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67  t, *pPgno, ppPag
1677e 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20  e, noContent);. 
1677f 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d           if( rc=
16780 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16781 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
16782 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
16783 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50  ((*ppPage)->pDbP
16784 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
16785 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16786 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
16787 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
16788 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20  (*ppPage);.     
16789 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1678a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73     }.          s
1678b 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20  earchList = 0;. 
1678c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1678d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1678e 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a  ge(pPrevTrunk);.
1678f 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b        pPrevTrunk
16790 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65   = 0;.    }while
16791 28 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a  ( searchList );.
16792 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
16793 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67  There are no pag
16794 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  es on the freeli
16795 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 20  st, so create a 
16796 6e 65 77 20 70 61 67 65 20 61 74 20 74 68 65 0a  new page at the.
16797 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68      ** end of th
16798 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 6e  e file */.    in
16799 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50  t nPage = pagerP
1679a 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20  agecount(pBt);. 
1679b 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 61 67     *pPgno = nPag
1679c 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66 28 20  e + 1;..    if( 
1679d 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f  *pPgno==PENDING_
1679e 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
1679f 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f 29  {.      (*pPgno)
167a0 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64  ++;.    }..#ifnd
167a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
167a2 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
167a3 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
167a4 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53 50 41  m && PTRMAP_ISPA
167a5 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f 29 20  GE(pBt, *pPgno) 
167a6 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 2a  ){.      /* If *
167a7 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74 6f 20  pPgno refers to 
167a8 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  a pointer-map pa
167a9 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74 77 6f  ge, allocate two
167aa 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20 20 20   new pages.     
167ab 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 6f   ** at the end o
167ac 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73 74 65  f the file inste
167ad 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65 20 66  ad of one. The f
167ae 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64 20 70  irst allocated p
167af 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 63  age.      ** bec
167b0 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69 6e 74  omes a new point
167b1 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74 68 65  er-map page, the
167b2 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65 64 20   second is used 
167b3 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20  by the caller.. 
167b4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4d 65       */.      Me
167b5 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30 3b 0a  mPage *pPg = 0;.
167b6 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c        TRACE(("AL
167b7 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20  LOCATE: %d from 
167b8 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70 6f 69  end of file (poi
167b9 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29 5c 6e  nter-map page)\n
167ba 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 20  ", *pPgno));.   
167bb 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e     assert( *pPgn
167bc 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  o!=PENDING_BYTE_
167bd 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20  PAGE(pBt) );.   
167be 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
167bf 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
167c0 20 2a 70 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30   *pPgno, &pPg, 0
167c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
167c2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
167c3 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
167c4 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 67  e3PagerWrite(pPg
167c5 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
167c6 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
167c7 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pPg);.      }.  
167c8 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
167c9 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 28 2a  urn rc;.      (*
167ca 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20  pPgno)++;.      
167cb 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44  if( *pPgno==PEND
167cc 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
167cd 74 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b  t) ){ (*pPgno)++
167ce 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ; }.    }.#endif
167cf 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70  ..    assert( *p
167d0 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno!=PENDING_BY
167d1 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a  TE_PAGE(pBt) );.
167d2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
167d3 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
167d4 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65  , *pPgno, ppPage
167d5 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
167d6 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
167d7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
167d8 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67  gerWrite((*ppPag
167d9 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  e)->pDbPage);.  
167da 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
167db 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c  _OK ){.      rel
167dc 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65  easePage(*ppPage
167dd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 52 41  );.    }.    TRA
167de 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25  CE(("ALLOCATE: %
167df 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69  d from end of fi
167e0 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b  le\n", *pPgno));
167e1 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
167e2 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f  *pPgno!=PENDING_
167e3 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
167e4 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  ;..end_allocate_
167e5 70 61 67 65 3a 0a 20 20 72 65 6c 65 61 73 65 50  page:.  releaseP
167e6 61 67 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72  age(pTrunk);.  r
167e7 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76  eleasePage(pPrev
167e8 54 72 75 6e 6b 29 3b 0a 20 20 69 66 28 20 72 63  Trunk);.  if( rc
167e9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
167ea 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61     if( sqlite3Pa
167eb 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
167ec 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61  (*ppPage)->pDbPa
167ed 67 65 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 72  ge)>1 ){.      r
167ee 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61  eleasePage(*ppPa
167ef 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ge);.      retur
167f0 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
167f1 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _BKPT;.    }.   
167f2 20 28 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 6e   (*ppPage)->isIn
167f3 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  it = 0;.  }.  re
167f4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
167f5 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
167f6 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70  is used to add p
167f7 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65  age iPage to the
167f8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
167f9 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74  ree-list. .** It
167fa 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
167fb 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
167fc 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20   already a part 
167fd 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  of the free-list
167fe 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ..**.** The valu
167ff 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
16800 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
16801 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
16802 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a   is optional..**
16803 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68   If the caller h
16804 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61  appens to have a
16805 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
16806 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a  MemPage object .
16807 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
16808 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68   to page iPage h
16809 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73  andy, it may pas
1680a 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f  s it as the seco
1680b 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74  nd value. .** Ot
1680c 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20  herwise, it may 
1680d 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  pass NULL..**.**
1680e 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   If a pointer to
1680f 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63   a MemPage objec
16810 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  t is passed as t
16811 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
16812 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72  nt,.** its refer
16813 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f  ence count is no
16814 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69  t altered by thi
16815 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  s function..*/.s
16816 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61  tatic int freePa
16817 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42  ge2(BtShared *pB
16818 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d  t, MemPage *pMem
16819 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65  Page, Pgno iPage
1681a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54  ){.  MemPage *pT
1681b 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20  runk = 0;       
1681c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65           /* Free
1681d 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
1681e 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e   */.  Pgno iTrun
1681f 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  k = 0;          
16820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
16821 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  e number of free
16822 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
16823 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a   */ .  MemPage *
16824 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50  pPage1 = pBt->pP
16825 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  age1;      /* Lo
16826 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f  cal reference to
16827 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d   page 1 */.  Mem
16828 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20  Page *pPage;    
16829 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1682a 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66   /* Page being f
1682b 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c  reed. May be NUL
1682c 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  L. */.  int rc; 
1682d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1682e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1682f 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20  eturn Code */.  
16830 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20  int nFree;      
16831 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16832 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e      /* Initial n
16833 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f  umber of pages o
16834 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a  n free-list */..
16835 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16836 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
16837 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
16838 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b  sert( iPage>1 );
16839 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d  .  assert( !pMem
1683a 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65  Page || pMemPage
1683b 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b  ->pgno==iPage );
1683c 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65  ..  if( pMemPage
1683d 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20   ){.    pPage = 
1683e 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71  pMemPage;.    sq
1683f 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50  lite3PagerRef(pP
16840 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
16841 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67   }else{.    pPag
16842 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f  e = btreePageLoo
16843 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b  kup(pBt, iPage);
16844 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65  .  }..  /* Incre
16845 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61  ment the free pa
16846 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67  ge count on pPag
16847 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  e1 */.  rc = sql
16848 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
16849 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b  Page1->pDbPage);
1684a 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f  .  if( rc ) goto
1684b 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20   freepage_out;. 
1684c 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74   nFree = get4byt
1684d 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
1684e 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74  [36]);.  put4byt
1684f 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
16850 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a  [36], nFree+1);.
16851 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
16852 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f  ECURE_DELETE.  /
16853 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f  * If the SQLITE_
16854 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f  SECURE_DELETE co
16855 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
16856 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  n is enabled, th
16857 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66  en.  ** always f
16858 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64  ully overwrite d
16859 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69  eleted informati
1685a 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20  on with zeros.. 
1685b 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67   */.  if( (!pPag
1685c 65 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74  e && (rc = sqlit
1685d 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
1685e 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61 67  Bt, iPage, &pPag
1685f 65 2c 20 30 29 29 29 0a 20 20 20 7c 7c 20 20 20  e, 0))).   ||   
16860 20 20 20 20 20 20 20 20 20 28 72 63 20 3d 20 73           (rc = s
16861 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
16862 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
16863 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ).  ){.    goto 
16864 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
16865 7d 0a 20 20 6d 65 6d 73 65 74 28 70 50 61 67 65  }.  memset(pPage
16866 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61 67  ->aData, 0, pPag
16867 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e->pBt->pageSize
16868 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
16869 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
1686a 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
1686b 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e 20 65  cuum, write an e
1686c 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e  ntry in the poin
1686d 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20  ter-map.  ** to 
1686e 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68  indicate that th
1686f 65 20 70 61 67 65 20 69 73 20 66 72 65 65 2e 0a  e page is free..
16870 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 54    */.  if( ISAUT
16871 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 72  OVACUUM ){.    r
16872 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42  c = ptrmapPut(pB
16873 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50  t, iPage, PTRMAP
16874 5f 46 52 45 45 50 41 47 45 2c 20 30 29 3b 0a 20  _FREEPAGE, 0);. 
16875 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
16876 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20   freepage_out;. 
16877 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e   }..  /* Now man
16878 69 70 75 6c 61 74 65 20 74 68 65 20 61 63 74 75  ipulate the actu
16879 61 6c 20 64 61 74 61 62 61 73 65 20 66 72 65 65  al database free
1687a 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72 65 2e  -list structure.
1687b 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 0a 20   There are two. 
1687c 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 69 65   ** possibilitie
1687d 73 2e 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c  s. If the free-l
1687e 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ist is currently
1687f 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20 74 68   empty, or if th
16880 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74 72 75  e first.  ** tru
16881 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 66  nk page in the f
16882 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c  ree-list is full
16883 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67 65  , then this page
16884 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 0a 20   will become a. 
16885 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c 69 73   ** new free-lis
16886 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 4f 74  t trunk page. Ot
16887 68 65 72 77 69 73 65 2c 20 69 74 20 77 69 6c 6c  herwise, it will
16888 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66 20 6f   become a leaf o
16889 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74  f the.  ** first
1688a 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74   trunk page in t
1688b 68 65 20 63 75 72 72 65 6e 74 20 66 72 65 65 2d  he current free-
1688c 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f 63 6b  list. This block
1688d 20 74 65 73 74 73 20 69 66 20 69 74 0a 20 20 2a   tests if it.  *
1688e 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  * is possible to
1688f 20 61 64 64 20 74 68 65 20 70 61 67 65 20 61 73   add the page as
16890 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 74   a new free-list
16891 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 66   leaf..  */.  if
16892 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a 20 20  ( nFree!=0 ){.  
16893 20 20 69 6e 74 20 6e 4c 65 61 66 3b 20 20 20 20    int nLeaf;    
16894 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
16895 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66  nitial number of
16896 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74   leaf cells on t
16897 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 20  runk page */..  
16898 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62    iTrunk = get4b
16899 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
1689a 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72 63 20  ta[32]);.    rc 
1689b 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
1689c 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e  tPage(pBt, iTrun
1689d 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a  k, &pTrunk, 0);.
1689e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1689f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67  TE_OK ){.      g
168a0 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74  oto freepage_out
168a1 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 65  ;.    }..    nLe
168a2 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  af = get4byte(&p
168a3 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29  Trunk->aData[4])
168a4 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 3c  ;.    if( nLeaf<
168a5 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
168a6 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
168a7 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
168a8 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
168a9 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61    }.    if( nLea
168aa 66 3c 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  f<pBt->usableSiz
168ab 65 2f 34 20 2d 20 38 20 29 7b 0a 20 20 20 20 20  e/4 - 8 ){.     
168ac 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   /* In this case
168ad 20 74 68 65 72 65 20 69 73 20 72 6f 6f 6d 20 6f   there is room o
168ae 6e 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65  n the trunk page
168af 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 70   to insert the p
168b0 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 69  age.      ** bei
168b1 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6e 65  ng freed as a ne
168b2 77 20 6c 65 61 66 2e 0a 20 20 20 20 20 20 2a 2a  w leaf..      **
168b3 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74  .      ** Note t
168b4 68 61 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61  hat the trunk pa
168b5 67 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79  ge is not really
168b6 20 66 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63   full until it c
168b7 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a  ontains.      **
168b8 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20   usableSize/4 - 
168b9 32 20 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75  2 entries, not u
168ba 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20  sableSize/4 - 8 
168bb 65 6e 74 72 69 65 73 20 61 73 20 77 65 20 68 61  entries as we ha
168bc 76 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 65  ve.      ** code
168bd 64 2e 20 20 42 75 74 20 64 75 65 20 74 6f 20 61  d.  But due to a
168be 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 6e   coding error in
168bf 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
168c0 69 74 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 20  ite prior to.   
168c1 20 20 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 74     ** 3.6.0, dat
168c2 61 62 61 73 65 73 20 77 69 74 68 20 66 72 65 65  abases with free
168c3 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 73  list trunk pages
168c4 20 68 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 68   holding more th
168c5 61 6e 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62  an.      ** usab
168c6 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74  leSize/4 - 8 ent
168c7 72 69 65 73 20 77 69 6c 6c 20 62 65 20 72 65 70  ries will be rep
168c8 6f 72 74 65 64 20 61 73 20 63 6f 72 72 75 70 74  orted as corrupt
168c9 2e 20 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 20  .  In order.    
168ca 20 20 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 6e    ** to maintain
168cb 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
168cc 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 6f 6c  tibility with ol
168cd 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  der versions of 
168ce 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a 2a  SQLite,.      **
168cf 20 77 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e   we will contain
168d0 20 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 65   to restrict the
168d1 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
168d2 65 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a 65  es to usableSize
168d3 2f 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a 20  /4 - 8.      ** 
168d4 66 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f 6d  for now.  At som
168d5 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66  e point in the f
168d6 75 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 72  uture (once ever
168d7 79 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 65  yone has upgrade
168d8 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 2e  d.      ** to 3.
168d9 36 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 65  6.0 or later) we
168da 20 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 72   should consider
168db 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 64   fixing the cond
168dc 69 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 20  itional above.  
168dd 20 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 22      ** to read "
168de 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 20  usableSize/4-2" 
168df 69 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 62  instead of "usab
168e0 6c 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 20  leSize/4-8"..   
168e1 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d     */.      rc =
168e2 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
168e3 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61  te(pTrunk->pDbPa
168e4 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
168e5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
168e6 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
168e7 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b  (&pTrunk->aData[
168e8 34 5d 2c 20 6e 4c 65 61 66 2b 31 29 3b 0a 20 20  4], nLeaf+1);.  
168e9 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
168ea 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b  pTrunk->aData[8+
168eb 6e 4c 65 61 66 2a 34 5d 2c 20 69 50 61 67 65 29  nLeaf*4], iPage)
168ec 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
168ed 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20  _SECURE_DELETE. 
168ee 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
168ef 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
168f0 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
168f1 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
168f2 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23  ge);.        }.#
168f3 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 63  endif.        rc
168f4 20 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f   = btreeSetHasCo
168f5 6e 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65  ntent(pBt, iPage
168f6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
168f7 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41   TRACE(("FREE-PA
168f8 47 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74  GE: %d leaf on t
168f9 72 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c  runk page %d\n",
168fa 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75  pPage->pgno,pTru
168fb 6e 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  nk->pgno));.    
168fc 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f    goto freepage_
168fd 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  out;.    }.  }..
168fe 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20    /* If control 
168ff 66 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f  flows to this po
16900 69 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73  int, then it was
16901 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
16902 20 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68   add the.  ** th
16903 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65  e page being fre
16904 65 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67  ed as a leaf pag
16905 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74  e of the first t
16906 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65  runk in the free
16907 2d 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73  -list..  ** Poss
16908 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65  ibly because the
16909 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d   free-list is em
1690a 70 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79  pty, or possibly
1690b 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20   because the .  
1690c 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69  ** first trunk i
1690d 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
1690e 69 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20  is full. Either 
1690f 77 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65  way, the page be
16910 69 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77  ing freed.  ** w
16911 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e  ill become the n
16912 65 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70  ew first trunk p
16913 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d  age in the free-
16914 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  list..  */.  if(
16915 20 20 20 28 28 21 70 50 61 67 65 29 20 26 26 20     ((!pPage) && 
16916 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69  (0 != (rc = sqli
16917 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
16918 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61  pBt, iPage, &pPa
16919 67 65 2c 20 30 29 29 29 29 0a 20 20 20 20 20 7c  ge, 0)))).     |
1691a 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71  | (0 != (rc = sq
1691b 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1691c 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29  pPage->pDbPage))
1691d 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ).  ){.    goto 
1691e 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
1691f 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 61  }.  put4byte(pPa
16920 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e  ge->aData, iTrun
16921 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26  k);.  put4byte(&
16922 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c  pPage->aData[4],
16923 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28   0);.  put4byte(
16924 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
16925 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52  2], iPage);.  TR
16926 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a  ACE(("FREE-PAGE:
16927 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61   %d new trunk pa
16928 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c  ge replacing %d\
16929 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  n", pPage->pgno,
1692a 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65   iTrunk));..free
1692b 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20  page_out:.  if( 
1692c 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61  pPage ){.    pPa
1692d 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  ge->isInit = 0;.
1692e 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67    }.  releasePag
1692f 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65  e(pPage);.  rele
16930 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b  asePage(pTrunk);
16931 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
16932 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50  static int freeP
16933 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
16934 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 66 72  ge){.  return fr
16935 65 65 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70  eePage2(pPage->p
16936 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65  Bt, pPage, pPage
16937 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->pgno);.}../*.*
16938 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66  * Free any overf
16939 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69  low pages associ
1693a 61 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69  ated with the gi
1693b 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  ven Cell..*/.sta
1693c 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c  tic int clearCel
1693d 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  l(MemPage *pPage
1693e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  , unsigned char 
1693f 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61  *pCell){.  BtSha
16940 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
16941 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66  ->pBt;.  CellInf
16942 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f  o info;.  Pgno o
16943 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72  vflPgno;.  int r
16944 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a  c;.  int nOvfl;.
16945 20 20 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69    u16 ovflPageSi
16946 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ze;..  assert( s
16947 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
16948 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
16949 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
1694a 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
1694b 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
1694c 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69   &info);.  if( i
1694d 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30  nfo.iOverflow==0
1694e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1694f 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f  QLITE_OK;  /* No
16950 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
16951 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20   Return without 
16952 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a  doing anything *
16953 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f  /.  }.  ovflPgno
16954 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65   = get4byte(&pCe
16955 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
16956 77 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  w]);.  assert( p
16957 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3e  Bt->usableSize >
16958 20 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65   4 );.  ovflPage
16959 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62  Size = pBt->usab
1695a 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f  leSize - 4;.  nO
1695b 76 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79  vfl = (info.nPay
1695c 6c 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63  load - info.nLoc
1695d 61 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a  al + ovflPageSiz
1695e 65 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53  e - 1)/ovflPageS
1695f 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  ize;.  assert( o
16960 76 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f  vflPgno==0 || nO
16961 76 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65  vfl>0 );.  while
16962 28 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20  ( nOvfl-- ){.   
16963 20 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 30 3b   Pgno iNext = 0;
16964 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f  .    MemPage *pO
16965 76 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  vfl = 0;.    if(
16966 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 7c 7c 20 6f   ovflPgno<2 || o
16967 76 66 6c 50 67 6e 6f 3e 70 61 67 65 72 50 61 67  vflPgno>pagerPag
16968 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20  ecount(pBt) ){. 
16969 20 20 20 20 20 2f 2a 20 30 20 69 73 20 6e 6f 74       /* 0 is not
1696a 20 61 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75   a legal page nu
1696b 6d 62 65 72 20 61 6e 64 20 70 61 67 65 20 31 20  mber and page 1 
1696c 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 0a 20 20  cannot be an .  
1696d 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20      ** overflow 
1696e 70 61 67 65 2e 20 54 68 65 72 65 66 6f 72 65 20  page. Therefore 
1696f 69 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 6f 72  if ovflPgno<2 or
16970 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
16971 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 66   the .      ** f
16972 69 6c 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ile the database
16973 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74   must be corrupt
16974 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  . */.      retur
16975 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
16976 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _BKPT;.    }.   
16977 20 69 66 28 20 6e 4f 76 66 6c 20 29 7b 0a 20 20   if( nOvfl ){.  
16978 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72      rc = getOver
16979 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6f 76  flowPage(pBt, ov
1697a 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20  flPgno, &pOvfl, 
1697b 26 69 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 69  &iNext);.      i
1697c 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1697d 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  c;.    }.    rc 
1697e 3d 20 66 72 65 65 50 61 67 65 32 28 70 42 74 2c  = freePage2(pBt,
1697f 20 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 67 6e 6f   pOvfl, ovflPgno
16980 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 76 66 6c  );.    if( pOvfl
16981 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
16982 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66  3PagerUnref(pOvf
16983 6c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  l->pDbPage);.   
16984 20 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20   }.    if( rc ) 
16985 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6f  return rc;.    o
16986 76 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 78 74 3b  vflPgno = iNext;
16987 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
16988 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
16989 2a 20 43 72 65 61 74 65 20 74 68 65 20 62 79 74  * Create the byt
1698a 65 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20  e sequence used 
1698b 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 63  to represent a c
1698c 65 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50 61 67  ell on page pPag
1698d 65 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74  e.** and write t
1698e 68 61 74 20 62 79 74 65 20 73 65 71 75 65 6e 63  hat byte sequenc
1698f 65 20 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20  e into pCell[]. 
16990 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   Overflow pages 
16991 61 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  are.** allocated
16992 20 61 6e 64 20 66 69 6c 6c 65 64 20 69 6e 20 61   and filled in a
16993 73 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68  s necessary.  Th
16994 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64  e calling proced
16995 75 72 65 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e  ure.** is respon
16996 73 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67  sible for making
16997 20 73 75 72 65 20 73 75 66 66 69 63 69 65 6e 74   sure sufficient
16998 20 73 70 61 63 65 20 68 61 73 20 62 65 65 6e 20   space has been 
16999 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72  allocated.** for
1699a 20 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20   pCell[]..**.** 
1699b 4e 6f 74 65 20 74 68 61 74 20 70 43 65 6c 6c 20  Note that pCell 
1699c 64 6f 65 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  does not necessa
1699d 72 79 20 6e 65 65 64 20 74 6f 20 70 6f 69 6e 74  ry need to point
1699e 20 74 6f 20 74 68 65 20 70 50 61 67 65 2d 3e 61   to the pPage->a
1699f 44 61 74 61 0a 2a 2a 20 61 72 65 61 2e 20 20 70  Data.** area.  p
169a0 43 65 6c 6c 20 6d 69 67 68 74 20 70 6f 69 6e 74  Cell might point
169a1 20 74 6f 20 73 6f 6d 65 20 74 65 6d 70 6f 72 61   to some tempora
169a2 72 79 20 73 74 6f 72 61 67 65 2e 20 20 54 68 65  ry storage.  The
169a3 20 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65   cell will.** be
169a4 20 63 6f 6e 73 74 72 75 63 74 65 64 20 69 6e 20   constructed in 
169a5 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 61  this temporary a
169a6 72 65 61 20 74 68 65 6e 20 63 6f 70 69 65 64 20  rea then copied 
169a7 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74  into pPage->aDat
169a8 61 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73  a.** later..*/.s
169a9 74 61 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e  tatic int fillIn
169aa 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20  Cell(.  MemPage 
169ab 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20  *pPage,         
169ac 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
169ad 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ge that contains
169ae 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75   the cell */.  u
169af 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43  nsigned char *pC
169b0 65 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ell,          /*
169b1 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f   Complete text o
169b2 66 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20  f the cell */.  
169b3 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
169b4 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f  , i64 nKey,    /
169b5 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 63  * The key */.  c
169b6 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61  onst void *pData
169b7 2c 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 2f 2a  ,int nData,   /*
169b8 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69   The data */.  i
169b9 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20  nt nZero,       
169ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
169bb 20 45 78 74 72 61 20 7a 65 72 6f 20 62 79 74 65   Extra zero byte
169bc 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 70  s to append to p
169bd 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  Data */.  int *p
169be 6e 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20  nSize           
169bf 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
169c0 65 20 63 65 6c 6c 20 73 69 7a 65 20 68 65 72 65  e cell size here
169c1 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61   */.){.  int nPa
169c2 79 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75  yload;.  const u
169c3 38 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e  8 *pSrc;.  int n
169c4 53 72 63 2c 20 6e 2c 20 72 63 3b 0a 20 20 69 6e  Src, n, rc;.  in
169c5 74 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d  t spaceLeft;.  M
169c6 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20  emPage *pOvfl = 
169c7 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54  0;.  MemPage *pT
169c8 6f 52 65 6c 65 61 73 65 20 3d 20 30 3b 0a 20 20  oRelease = 0;.  
169c9 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
169ca 50 72 69 6f 72 3b 0a 20 20 75 6e 73 69 67 6e 65  Prior;.  unsigne
169cb 64 20 63 68 61 72 20 2a 70 50 61 79 6c 6f 61 64  d char *pPayload
169cc 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
169cd 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
169ce 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20    Pgno pgnoOvfl 
169cf 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 65 61 64  = 0;.  int nHead
169d0 65 72 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69  er;.  CellInfo i
169d1 6e 66 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  nfo;..  assert( 
169d2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
169d3 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
169d4 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 70  utex) );..  /* p
169d5 50 61 67 65 20 69 73 20 6e 6f 74 20 6e 65 63 65  Page is not nece
169d6 73 73 61 72 69 6c 79 20 77 72 69 74 65 61 62 6c  ssarily writeabl
169d7 65 20 73 69 6e 63 65 20 70 43 65 6c 6c 20 6d 69  e since pCell mi
169d8 67 68 74 20 62 65 20 61 75 78 69 6c 69 61 72 79  ght be auxiliary
169d9 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 73 70 61  .  ** buffer spa
169da 63 65 20 74 68 61 74 20 69 73 20 73 65 70 61 72  ce that is separ
169db 61 74 65 20 66 72 6f 6d 20 74 68 65 20 70 50 61  ate from the pPa
169dc 67 65 20 62 75 66 66 65 72 20 61 72 65 61 20 2a  ge buffer area *
169dd 2f 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c  /.  assert( pCel
169de 6c 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c  l<pPage->aData |
169df 7c 20 70 43 65 6c 6c 3e 3d 26 70 50 61 67 65 2d  | pCell>=&pPage-
169e0 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65  >aData[pBt->page
169e1 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20  Size].          
169e2 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65    || sqlite3Page
169e3 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
169e4 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
169e5 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68  .  /* Fill in th
169e6 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 20 6e  e header. */.  n
169e7 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 69 66  Header = 0;.  if
169e8 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
169e9 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d  {.    nHeader +=
169ea 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50   4;.  }.  if( pP
169eb 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a  age->hasData ){.
169ec 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70      nHeader += p
169ed 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b  utVarint(&pCell[
169ee 6e 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 61 2b  nHeader], nData+
169ef 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b  nZero);.  }else{
169f0 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e 5a 65  .    nData = nZe
169f1 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 48  ro = 0;.  }.  nH
169f2 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69  eader += putVari
169f3 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65  nt(&pCell[nHeade
169f4 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b 65 79  r], *(u64*)&nKey
169f5 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
169f6 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
169f7 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
169f8 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e  o);.  assert( in
169f9 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61  fo.nHeader==nHea
169fa 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  der );.  assert(
169fb 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79   info.nKey==nKey
169fc 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e   );.  assert( in
169fd 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28  fo.nData==(u32)(
169fe 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a  nData+nZero) );.
169ff 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20    .  /* Fill in 
16a00 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20  the payload */. 
16a01 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74   nPayload = nDat
16a02 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28  a + nZero;.  if(
16a03 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
16a04 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61  {.    pSrc = pDa
16a05 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e  ta;.    nSrc = n
16a06 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20  Data;.    nData 
16a07 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20  = 0;.  }else{ . 
16a08 20 20 20 69 66 28 20 6e 4b 65 79 3e 30 78 37 66     if( nKey>0x7f
16a09 66 66 66 66 66 66 20 7c 7c 20 70 4b 65 79 3d 3d  ffffff || pKey==
16a0a 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
16a0b 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
16a0c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79  ;.    }.    nPay
16a0d 6c 6f 61 64 20 2b 3d 20 28 69 6e 74 29 6e 4b 65  load += (int)nKe
16a0e 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 4b  y;.    pSrc = pK
16a0f 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 28  ey;.    nSrc = (
16a10 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20  int)nKey;.  }.  
16a11 2a 70 6e 53 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e  *pnSize = info.n
16a12 53 69 7a 65 3b 0a 20 20 73 70 61 63 65 4c 65 66  Size;.  spaceLef
16a13 74 20 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b  t = info.nLocal;
16a14 0a 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70  .  pPayload = &p
16a15 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20  Cell[nHeader];. 
16a16 20 70 50 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c   pPrior = &pCell
16a17 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
16a18 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 50 61 79  ;..  while( nPay
16a19 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 20 20 69 66  load>0 ){.    if
16a1a 28 20 73 70 61 63 65 4c 65 66 74 3d 3d 30 20 29  ( spaceLeft==0 )
16a1b 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
16a1c 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
16a1d 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f  .      Pgno pgno
16a1e 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f 76 66  Ptrmap = pgnoOvf
16a1f 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70  l; /* Overflow p
16a20 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  age pointer-map 
16a21 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a 20 20  entry page */.  
16a22 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
16a23 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
16a24 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20     do{.         
16a25 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20   pgnoOvfl++;.   
16a26 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 0a 20       } while( . 
16a27 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 50 5f           PTRMAP_
16a28 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f  ISPAGE(pBt, pgno
16a29 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f 76 66  Ovfl) || pgnoOvf
16a2a 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  l==PENDING_BYTE_
16a2b 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 20 20  PAGE(pBt) .     
16a2c 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65     );.      }.#e
16a2d 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20  ndif.      rc = 
16a2e 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
16a2f 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26  e(pBt, &pOvfl, &
16a30 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76  pgnoOvfl, pgnoOv
16a31 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20  fl, 0);.#ifndef 
16a32 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
16a33 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20  VACUUM.      /* 
16a34 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
16a35 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
16a36 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 20 73 65  cuum, and the se
16a37 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 71 75 65  cond or subseque
16a38 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72  nt.      ** over
16a39 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62 65 69  flow page is bei
16a3a 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64  ng allocated, ad
16a3b 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68  d an entry to th
16a3c 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20  e pointer-map.  
16a3d 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61 74 20      ** for that 
16a3e 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 20 20 20  page now. .     
16a3f 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20   **.      ** If 
16a40 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
16a41 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c  t overflow page,
16a42 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 70 61   then write a pa
16a43 72 74 69 61 6c 20 65 6e 74 72 79 20 0a 20 20 20  rtial entry .   
16a44 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 6f 69     ** to the poi
16a45 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 77 65 20  nter-map. If we 
16a46 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 20 74 6f  write nothing to
16a47 20 74 68 69 73 20 70 6f 69 6e 74 65 72 2d 6d 61   this pointer-ma
16a48 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 20 2a 2a  p slot,.      **
16a49 20 74 68 65 6e 20 74 68 65 20 6f 70 74 69 6d 69   then the optimi
16a4a 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 20 63 68  stic overflow ch
16a4b 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69  ain processing i
16a4c 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 0a 20 20  n clearCell().  
16a4d 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 73 69 6e      ** may misin
16a4e 74 65 72 70 72 65 74 20 74 68 65 20 75 6e 69 6e  terpret the unin
16a4f 69 74 69 61 6c 69 73 65 64 20 76 61 6c 75 65 73  itialised values
16a50 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 0a   and delete the.
16a51 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e 67 20 70        ** wrong p
16a52 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 64 61  ages from the da
16a53 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f  tabase..      */
16a54 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e  .      if( pBt->
16a55 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 72 63  autoVacuum && rc
16a56 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
16a57 20 20 20 20 20 20 20 75 38 20 65 54 79 70 65 20         u8 eType 
16a58 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 3f 50 54  = (pgnoPtrmap?PT
16a59 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 50  RMAP_OVERFLOW2:P
16a5a 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 29  TRMAP_OVERFLOW1)
16a5b 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  ;.        rc = p
16a5c 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67  trmapPut(pBt, pg
16a5d 6e 6f 4f 76 66 6c 2c 20 65 54 79 70 65 2c 20 70  noOvfl, eType, p
16a5e 67 6e 6f 50 74 72 6d 61 70 29 3b 0a 20 20 20 20  gnoPtrmap);.    
16a5f 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
16a60 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
16a61 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20  age(pOvfl);.    
16a62 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65      }.      }.#e
16a63 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 72  ndif.      if( r
16a64 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  c ){.        rel
16a65 65 61 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65  easePage(pToRele
16a66 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65  ase);.        re
16a67 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
16a68 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 54  ..      /* If pT
16a69 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20  oRelease is not 
16a6a 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 69 6f 72  zero than pPrior
16a6b 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68 65   points into the
16a6c 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20 20   data area.     
16a6d 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73   ** of pToReleas
16a6e 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54  e.  Make sure pT
16a6f 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c  oRelease is stil
16a70 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a  l writeable. */.
16a71 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
16a72 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73  oRelease==0 || s
16a73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
16a74 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73  teable(pToReleas
16a75 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  e->pDbPage) );..
16a76 20 20 20 20 20 20 2f 2a 20 49 66 20 70 50 72 69        /* If pPri
16a77 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  or is part of th
16a78 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70  e data area of p
16a79 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20  Page, then make 
16a7a 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 20  sure pPage.     
16a7b 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69   ** is still wri
16a7c 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20  teable */.      
16a7d 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 3c 70  assert( pPrior<p
16a7e 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70  Page->aData || p
16a7f 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 2d 3e 61  Prior>=&pPage->a
16a80 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69  Data[pBt->pageSi
16a81 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ze].            
16a82 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  || sqlite3PagerI
16a83 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
16a84 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20  ->pDbPage) );.. 
16a85 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 50       put4byte(pP
16a86 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b  rior, pgnoOvfl);
16a87 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
16a88 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a  ge(pToRelease);.
16a89 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61 73 65        pToRelease
16a8a 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20   = pOvfl;.      
16a8b 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e  pPrior = pOvfl->
16a8c 61 44 61 74 61 3b 0a 20 20 20 20 20 20 70 75 74  aData;.      put
16a8d 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 30 29  4byte(pPrior, 0)
16a8e 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 6f 61 64  ;.      pPayload
16a8f 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61   = &pOvfl->aData
16a90 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 61 63 65  [4];.      space
16a91 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 73 61 62  Left = pBt->usab
16a92 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20  leSize - 4;.    
16a93 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 61 79 6c 6f  }.    n = nPaylo
16a94 61 64 3b 0a 20 20 20 20 69 66 28 20 6e 3e 73 70  ad;.    if( n>sp
16a95 61 63 65 4c 65 66 74 20 29 20 6e 20 3d 20 73 70  aceLeft ) n = sp
16a96 61 63 65 4c 65 66 74 3b 0a 0a 20 20 20 20 2f 2a  aceLeft;..    /*
16a97 20 49 66 20 70 54 6f 52 65 6c 65 61 73 65 20 69   If pToRelease i
16a98 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20  s not zero than 
16a99 70 50 61 79 6c 6f 61 64 20 70 6f 69 6e 74 73 20  pPayload points 
16a9a 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61 72  into the data ar
16a9b 65 61 0a 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f  ea.    ** of pTo
16a9c 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b 65 20 73  Release.  Make s
16a9d 75 72 65 20 70 54 6f 52 65 6c 65 61 73 65 20 69  ure pToRelease i
16a9e 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c  s still writeabl
16a9f 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  e. */.    assert
16aa0 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20  ( pToRelease==0 
16aa1 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  || sqlite3PagerI
16aa2 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65  swriteable(pToRe
16aa3 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20  lease->pDbPage) 
16aa4 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 50  );..    /* If pP
16aa5 61 79 6c 6f 61 64 20 69 73 20 70 61 72 74 20 6f  ayload is part o
16aa6 66 20 74 68 65 20 64 61 74 61 20 61 72 65 61 20  f the data area 
16aa7 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d  of pPage, then m
16aa8 61 6b 65 20 73 75 72 65 20 70 50 61 67 65 0a 20  ake sure pPage. 
16aa9 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77     ** is still w
16aaa 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20  riteable */.    
16aab 61 73 73 65 72 74 28 20 70 50 61 79 6c 6f 61 64  assert( pPayload
16aac 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c  <pPage->aData ||
16aad 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 50 61 67   pPayload>=&pPag
16aae 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61  e->aData[pBt->pa
16aaf 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20  geSize].        
16ab0 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61      || sqlite3Pa
16ab1 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
16ab2 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
16ab3 3b 0a 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3e  ;..    if( nSrc>
16ab4 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  0 ){.      if( n
16ab5 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 72 63  >nSrc ) n = nSrc
16ab6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
16ab7 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 6d 65  pSrc );.      me
16ab8 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70  mcpy(pPayload, p
16ab9 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c  Src, n);.    }el
16aba 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  se{.      memset
16abb 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 6e 29  (pPayload, 0, n)
16abc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79  ;.    }.    nPay
16abd 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 20 70  load -= n;.    p
16abe 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20  Payload += n;.  
16abf 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 20    pSrc += n;.   
16ac0 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 20   nSrc -= n;.    
16ac1 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a  spaceLeft -= n;.
16ac2 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d 30 20      if( nSrc==0 
16ac3 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 3d 20  ){.      nSrc = 
16ac4 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 53 72  nData;.      pSr
16ac5 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 7d  c = pData;.    }
16ac6 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61  .  }.  releasePa
16ac7 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a  ge(pToRelease);.
16ac8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
16ac9 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d  OK;.}../*.** Rem
16aca 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 65 6c  ove the i-th cel
16acb 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 20 54  l from pPage.  T
16acc 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 66 65  his routine effe
16acd 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 2e 0a  cts pPage only..
16ace 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f 6e 74  ** The cell cont
16acf 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 65 64  ent is not freed
16ad0 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e   or deallocated.
16ad1 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20    It is assumed 
16ad2 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 6c 6c  that.** the cell
16ad3 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 65 65   content has bee
16ad4 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 6c 61  n copied somepla
16ad5 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 20 72  ce else.  This r
16ad6 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a 20 72  outine just.** r
16ad7 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 65 72  emoves the refer
16ad8 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 6c 6c  ence to the cell
16ad9 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a   from pPage..**.
16ada 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 65 20  ** "sz" must be 
16adb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
16adc 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 2e  tes in the cell.
16add 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
16ade 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20  ropCell(MemPage 
16adf 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64 78 2c  *pPage, int idx,
16ae0 20 69 6e 74 20 73 7a 29 7b 0a 20 20 69 6e 74 20   int sz){.  int 
16ae1 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  i;          /* L
16ae2 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
16ae3 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20   int pc;        
16ae4 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 65   /* Offset to ce
16ae5 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 63 65  ll content of ce
16ae6 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  ll being deleted
16ae7 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20   */.  u8 *data; 
16ae8 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e        /* pPage->
16ae9 61 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a 70  aData */.  u8 *p
16aea 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73  tr;        /* Us
16aeb 65 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 65 73  ed to move bytes
16aec 20 61 72 6f 75 6e 64 20 77 69 74 68 69 6e 20 64   around within d
16aed 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72  ata[] */.  int r
16aee 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  c;         /* Th
16aef 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  e return code */
16af0 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e  ..  assert( idx>
16af1 3d 30 20 26 26 20 69 64 78 3c 70 50 61 67 65 2d  =0 && idx<pPage-
16af2 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65  >nCell );.  asse
16af3 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65  rt( sz==cellSize
16af4 28 70 50 61 67 65 2c 20 69 64 78 29 20 29 3b 0a  (pPage, idx) );.
16af5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16af6 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
16af7 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
16af8 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
16af9 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
16afa 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
16afb 74 65 78 29 20 29 3b 0a 20 20 64 61 74 61 20 3d  tex) );.  data =
16afc 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20   pPage->aData;. 
16afd 20 70 74 72 20 3d 20 26 64 61 74 61 5b 70 50 61   ptr = &data[pPa
16afe 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b  ge->cellOffset +
16aff 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d 20   2*idx];.  pc = 
16b00 67 65 74 32 62 79 74 65 28 70 74 72 29 3b 0a 20  get2byte(ptr);. 
16b01 20 69 66 28 20 28 70 63 3c 70 50 61 67 65 2d 3e   if( (pc<pPage->
16b02 68 64 72 4f 66 66 73 65 74 2b 36 2b 28 70 50 61  hdrOffset+6+(pPa
16b03 67 65 2d 3e 6c 65 61 66 3f 30 3a 34 29 29 0a 20  ge->leaf?0:4)). 
16b04 20 20 20 20 7c 7c 20 28 70 63 2b 73 7a 3e 70 50      || (pc+sz>pP
16b05 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
16b06 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 72 65 74  Size) ){.    ret
16b07 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
16b08 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72  PT_BKPT;.  }.  r
16b09 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70 50  c = freeSpace(pP
16b0a 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20 20  age, pc, sz);.  
16b0b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16b0c 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
16b0d 72 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  rc;.  }.  for(i=
16b0e 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 2d 3e  idx+1; i<pPage->
16b0f 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 72 2b  nCell; i++, ptr+
16b10 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 5d 20  =2){.    ptr[0] 
16b11 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 70 74  = ptr[2];.    pt
16b12 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b 0a 20  r[1] = ptr[3];. 
16b13 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c   }.  pPage->nCel
16b14 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 65 28  l--;.  put2byte(
16b15 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  &data[pPage->hdr
16b16 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67 65  Offset+3], pPage
16b17 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 67  ->nCell);.  pPag
16b18 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a 20  e->nFree += 2;. 
16b19 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
16b1a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65  K;.}../*.** Inse
16b1b 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 6f 6e  rt a new cell on
16b1c 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c 20 69   pPage at cell i
16b1d 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 6c 6c  ndex "i".  pCell
16b1e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a   points to the.*
16b1f 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  * content of the
16b20 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   cell..**.** If 
16b21 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
16b22 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68 65   will fit on the
16b23 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 74 20   page, then put 
16b24 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 69 74  it there.  If it
16b25 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  .** will not fit
16b26 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 63 6f  , then make a co
16b27 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  py of the cell c
16b28 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 65 6d  ontent into pTem
16b29 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 69 73  p if.** pTemp is
16b2a 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67 61   not null.  Rega
16b2b 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70 2c  rdless of pTemp,
16b2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
16b2d 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 61 67  entry.** in pPag
16b2e 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20 6d  e->aOvfl[] and m
16b2f 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f 20  ake it point to 
16b30 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
16b31 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e 20 70   (either.** in p
16b32 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69 67  Temp or the orig
16b33 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64 20  inal pCell) and 
16b34 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73 20  also record its 
16b35 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f 63  index. .** Alloc
16b36 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74 72  ating a new entr
16b37 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 65 6c  y in pPage->aCel
16b38 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 61 74  l[] implies that
16b39 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f 76 65   .** pPage->nOve
16b3a 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 6d 65  rflow is increme
16b3b 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  nted..**.** If n
16b3c 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  Skip is non-zero
16b3d 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63 6f  , then do not co
16b3e 70 79 20 74 68 65 20 66 69 72 73 74 20 6e 53 6b  py the first nSk
16b3f 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 65 0a  ip bytes of the.
16b40 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 61 6c  ** cell. The cal
16b41 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 72 69  ler will overwri
16b42 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 74 68  te them after th
16b43 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
16b44 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69 70  rns. If.** nSkip
16b45 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
16b46 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e 6f 74  en pCell may not
16b47 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 6e 76   point to an inv
16b48 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f 63 61  alid memory loca
16b49 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 70 43  tion .** (but pC
16b4a 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 6c 77  ell+nSkip is alw
16b4b 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a 73  ays valid)..*/.s
16b4c 74 61 74 69 63 20 69 6e 74 20 69 6e 73 65 72 74  tatic int insert
16b4d 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20  Cell(.  MemPage 
16b4e 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 61 67  *pPage,   /* Pag
16b4f 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20  e into which we 
16b50 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a 20  are copying */. 
16b51 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20   int i,         
16b52 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20 62     /* New cell b
16b53 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 68 20  ecomes the i-th 
16b54 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65  cell of the page
16b55 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c   */.  u8 *pCell,
16b56 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
16b57 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 63 65  nt of the new ce
16b58 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c 20  ll */.  int sz, 
16b59 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
16b5a 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69 6e  es of content in
16b5b 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a   pCell */.  u8 *
16b5c 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a  pTemp,        /*
16b5d 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 73 70   Temp storage sp
16b5e 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20 69  ace for pCell, i
16b5f 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 75 38  f needed */.  u8
16b60 20 6e 53 6b 69 70 20 20 20 20 20 20 20 20 20 20   nSkip          
16b61 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20  /* Do not write 
16b62 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20  the first nSkip 
16b63 62 79 74 65 73 20 6f 66 20 74 68 65 20 63 65 6c  bytes of the cel
16b64 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 64  l */.){.  int id
16b65 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  x;          /* W
16b66 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e 65  here to write ne
16b67 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  w cell content i
16b68 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e  n data[] */.  in
16b69 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  t j;            
16b6a 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
16b6b 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 20  */.  int top;   
16b6c 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
16b6d 62 79 74 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20  byte of content 
16b6e 66 6f 72 20 61 6e 79 20 63 65 6c 6c 20 69 6e 20  for any cell in 
16b6f 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  data[] */.  int 
16b70 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  end;          /*
16b71 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74   First byte past
16b72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70   the last cell p
16b73 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d  ointer in data[]
16b74 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20   */.  int ins;  
16b75 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
16b76 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65   in data[] where
16b77 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65   new cell pointe
16b78 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f  r is inserted */
16b79 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20  .  int hdr;     
16b7a 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
16b7b 6e 74 6f 20 64 61 74 61 5b 5d 20 6f 66 20 74 68  nto data[] of th
16b7c 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f  e page header */
16b7d 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65  .  int cellOffse
16b7e 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20  t;   /* Address 
16b7f 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f  of first cell po
16b80 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20  inter in data[] 
16b81 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20  */.  u8 *data;  
16b82 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
16b83 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f  ntent of the who
16b84 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20  le page */.  u8 
16b85 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f  *ptr;          /
16b86 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e  * Used for movin
16b87 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72  g information ar
16b88 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a  ound in data[] *
16b89 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  /..  assert( i>=
16b8a 30 20 26 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e  0 && i<=pPage->n
16b8b 43 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65  Cell+pPage->nOve
16b8c 72 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73 65 72  rflow );.  asser
16b8d 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c  t( pPage->nCell<
16b8e 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e  =MX_CELL(pPage->
16b8f 70 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28  pBt) && MX_CELL(
16b90 70 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36  pPage->pBt)<=546
16b91 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
16b92 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3c  Page->nOverflow<
16b93 3d 41 72 72 61 79 53 69 7a 65 28 70 50 61 67 65  =ArraySize(pPage
16b94 2d 3e 61 4f 76 66 6c 29 20 29 3b 0a 20 20 61 73  ->aOvfl) );.  as
16b95 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69  sert( sz==cellSi
16b96 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  zePtr(pPage, pCe
16b97 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ll) );.  assert(
16b98 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16b99 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
16b9a 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
16b9b 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
16b9c 20 7c 7c 20 73 7a 2b 32 3e 70 50 61 67 65 2d 3e   || sz+2>pPage->
16b9d 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 69 66 28  nFree ){.    if(
16b9e 20 70 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20   pTemp ){.      
16b9f 6d 65 6d 63 70 79 28 70 54 65 6d 70 2b 6e 53 6b  memcpy(pTemp+nSk
16ba0 69 70 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c  ip, pCell+nSkip,
16ba1 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20   sz-nSkip);.    
16ba2 20 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 3b    pCell = pTemp;
16ba3 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70  .    }.    j = p
16ba4 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b  Page->nOverflow+
16ba5 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a  +;.    assert( j
16ba6 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50  <(int)(sizeof(pP
16ba7 61 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65  age->aOvfl)/size
16ba8 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b  of(pPage->aOvfl[
16ba9 30 5d 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67  0])) );.    pPag
16baa 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c  e->aOvfl[j].pCel
16bab 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70  l = pCell;.    p
16bac 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69  Page->aOvfl[j].i
16bad 64 78 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 20  dx = (u16)i;.   
16bae 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20   pPage->nFree = 
16baf 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
16bb0 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
16bb1 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
16bb2 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
16bb3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16bb4 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
16bb5 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
16bb6 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
16bb7 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
16bb8 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
16bb9 29 3b 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50  );.    data = pP
16bba 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  age->aData;.    
16bbb 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
16bbc 4f 66 66 73 65 74 3b 0a 20 20 20 20 74 6f 70 20  Offset;.    top 
16bbd 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
16bbe 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 63 65  [hdr+5]);.    ce
16bbf 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65  llOffset = pPage
16bc0 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20  ->cellOffset;.  
16bc1 20 20 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73    end = cellOffs
16bc2 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43  et + 2*pPage->nC
16bc3 65 6c 6c 20 2b 20 32 3b 0a 20 20 20 20 69 6e 73  ell + 2;.    ins
16bc4 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20   = cellOffset + 
16bc5 32 2a 69 3b 0a 20 20 20 20 69 66 28 20 65 6e 64  2*i;.    if( end
16bc6 20 3e 20 74 6f 70 20 2d 20 73 7a 20 29 7b 0a 20   > top - sz ){. 
16bc7 20 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67       rc = defrag
16bc8 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b  mentPage(pPage);
16bc9 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
16bca 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16bcb 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
16bcc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 6f 70       }.      top
16bcd 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
16bce 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 20  a[hdr+5]);.     
16bcf 20 61 73 73 65 72 74 28 20 65 6e 64 20 2b 20 73   assert( end + s
16bd0 7a 20 3c 3d 20 74 6f 70 20 29 3b 0a 20 20 20 20  z <= top );.    
16bd1 7d 0a 20 20 20 20 69 64 78 20 3d 20 61 6c 6c 6f  }.    idx = allo
16bd2 63 61 74 65 53 70 61 63 65 28 70 50 61 67 65 2c  cateSpace(pPage,
16bd3 20 73 7a 29 3b 0a 20 20 20 20 61 73 73 65 72 74   sz);.    assert
16bd4 28 20 69 64 78 3e 30 20 29 3b 0a 20 20 20 20 61  ( idx>0 );.    a
16bd5 73 73 65 72 74 28 20 65 6e 64 20 3c 3d 20 67 65  ssert( end <= ge
16bd6 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
16bd7 2b 35 5d 29 20 29 3b 0a 20 20 20 20 69 66 20 28  +5]) );.    if (
16bd8 69 64 78 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e  idx+sz > pPage->
16bd9 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29  pBt->usableSize)
16bda 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
16bdb 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
16bdc 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  KPT;.    }.    p
16bdd 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20  Page->nCell++;. 
16bde 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20     pPage->nFree 
16bdf 2d 3d 20 32 3b 0a 20 20 20 20 6d 65 6d 63 70 79  -= 2;.    memcpy
16be0 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70  (&data[idx+nSkip
16be1 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20  ], pCell+nSkip, 
16be2 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 66  sz-nSkip);.    f
16be3 6f 72 28 6a 3d 65 6e 64 2d 32 2c 20 70 74 72 3d  or(j=end-2, ptr=
16be4 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b  &data[j]; j>ins;
16be5 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d 32 29 7b 0a   j-=2, ptr-=2){.
16be6 20 20 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70        ptr[0] = p
16be7 74 72 5b 2d 32 5d 3b 0a 20 20 20 20 20 20 70 74  tr[-2];.      pt
16be8 72 5b 31 5d 20 3d 20 70 74 72 5b 2d 31 5d 3b 0a  r[1] = ptr[-1];.
16be9 20 20 20 20 7d 0a 20 20 20 20 70 75 74 32 62 79      }.    put2by
16bea 74 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c 20 69  te(&data[ins], i
16beb 64 78 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74  dx);.    put2byt
16bec 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20  e(&data[hdr+3], 
16bed 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 23  pPage->nCell);.#
16bee 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16bef 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
16bf0 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42 74    if( pPage->pBt
16bf1 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
16bf2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 6c        /* The cel
16bf3 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 20  l may contain a 
16bf4 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 76  pointer to an ov
16bf5 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20  erflow page. If 
16bf6 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20 20  so, write.      
16bf7 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72  ** the entry for
16bf8 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61   the overflow pa
16bf9 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e  ge into the poin
16bfa 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20 2a  ter map..      *
16bfb 2f 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f  /.      CellInfo
16bfc 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 73 71 6c   info;.      sql
16bfd 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
16bfe 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
16bff 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  ll, &info);.    
16c00 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f 2e    assert( (info.
16c01 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e  nData+(pPage->in
16c02 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79  tKey?0:info.nKey
16c03 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61  ))==info.nPayloa
16c04 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  d );.      if( i
16c05 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b  nfo.iOverflow ){
16c06 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 70 67  .        Pgno pg
16c07 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74  noOvfl = get4byt
16c08 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f  e(&pCell[info.iO
16c09 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20  verflow]);.     
16c0a 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
16c0b 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 67  t(pPage->pBt, pg
16c0c 6e 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f  noOvfl, PTRMAP_O
16c0d 56 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d  VERFLOW1, pPage-
16c0e 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  >pgno);.        
16c0f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16c10 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
16c11 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
16c12 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  dif.  }..  retur
16c13 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
16c14 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 73 74  /*.** Add a list
16c15 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 20 70   of cells to a p
16c16 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 73  age.  The page s
16c17 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c  hould be initial
16c18 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65  ly empty..** The
16c19 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61 72 61   cells are guara
16c1a 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f 6e 20  nteed to fit on 
16c1b 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61  the page..*/.sta
16c1c 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62 6c  tic void assembl
16c1d 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 65  ePage(.  MemPage
16c1e 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54 68   *pPage,   /* Th
16c1f 65 20 70 61 67 65 20 74 6f 20 62 65 20 61 73 73  e page to be ass
16c20 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74  emblied */.  int
16c21 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f   nCell,        /
16c22 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
16c23 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f 20  cells to add to 
16c24 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 75  this page */.  u
16c25 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20  8 **apCell,     
16c26 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20   /* Pointers to 
16c27 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20  cell bodies */. 
16c28 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20 20 20   u16 *aSize     
16c29 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74     /* Sizes of t
16c2a 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20  he cells */.){. 
16c2b 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
16c2c 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
16c2d 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c  er */.  u8 *pCel
16c2e 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 41 64 64  lptr;     /* Add
16c2f 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c  ress of next cel
16c30 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69  l pointer */.  i
16c31 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20  nt cellbody;    
16c32 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e   /* Address of n
16c33 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f  ext cell body */
16c34 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 64 61  .  u8 * const da
16c35 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
16c36 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  a;             /
16c37 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74  * Pointer to dat
16c38 61 20 66 6f 72 20 70 50 61 67 65 20 2a 2f 0a 20  a for pPage */. 
16c39 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 3d   const int hdr =
16c3a 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
16c3b 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
16c3c 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 65 72  Offset of header
16c3d 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 63   on pPage */.  c
16c3e 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 62 6c 65  onst int nUsable
16c3f 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75   = pPage->pBt->u
16c40 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73  sableSize; /* Us
16c41 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 70 61 67  able size of pag
16c42 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
16c43 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
16c44 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
16c45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16c46 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
16c47 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
16c48 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26 26 20  rt( nCell>=0 && 
16c49 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70  nCell<=MX_CELL(p
16c4a 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d 58  Page->pBt) && MX
16c4b 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74  _CELL(pPage->pBt
16c4c 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 73  )<=5460 );.  ass
16c4d 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
16c4e 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
16c4f 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
16c50 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
16c51 20 74 68 65 20 70 61 67 65 20 68 61 73 20 6a 75   the page has ju
16c52 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 64 20 62  st been zeroed b
16c53 79 20 7a 65 72 6f 50 61 67 65 28 29 20 2a 2f 0a  y zeroPage() */.
16c54 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
16c55 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 61  >nCell==0 );.  a
16c56 73 73 65 72 74 28 20 67 65 74 32 62 79 74 65 28  ssert( get2byte(
16c57 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d 3d 6e  &data[hdr+5])==n
16c58 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70 43 65  Usable );..  pCe
16c59 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 5b 70 50  llptr = &data[pP
16c5a 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
16c5b 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20 63 65  + nCell*2];.  ce
16c5c 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62 6c 65  llbody = nUsable
16c5d 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d  ;.  for(i=nCell-
16c5e 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20  1; i>=0; i--){. 
16c5f 20 20 20 70 43 65 6c 6c 70 74 72 20 2d 3d 20 32     pCellptr -= 2
16c60 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 79 20 2d  ;.    cellbody -
16c61 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20 20 20  = aSize[i];.    
16c62 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c 70 74  put2byte(pCellpt
16c63 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20  r, cellbody);.  
16c64 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63    memcpy(&data[c
16c65 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65 6c 6c  ellbody], apCell
16c66 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d 29 3b 0a  [i], aSize[i]);.
16c67 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65 28 26    }.  put2byte(&
16c68 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e 43 65  data[hdr+3], nCe
16c69 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28  ll);.  put2byte(
16c6a 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 65  &data[hdr+5], ce
16c6b 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 67 65  llbody);.  pPage
16c6c 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43 65 6c  ->nFree -= (nCel
16c6d 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20 2d 20  l*2 + nUsable - 
16c6e 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61  cellbody);.  pPa
16c6f 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75 31 36  ge->nCell = (u16
16c70 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  )nCell;.}../*.**
16c71 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70   The following p
16c72 61 72 61 6d 65 74 65 72 73 20 64 65 74 65 72 6d  arameters determ
16c73 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61 64 6a  ine how many adj
16c74 61 63 65 6e 74 20 70 61 67 65 73 20 67 65 74 20  acent pages get 
16c75 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 61  involved.** in a
16c76 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61   balancing opera
16c77 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74 68 65  tion.  NN is the
16c78 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68   number of neigh
16c79 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73  bors on either s
16c7a 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61  ide.** of the pa
16c7b 67 65 20 74 68 61 74 20 70 61 72 74 69 63 69 70  ge that particip
16c7c 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e  ate in the balan
16c7d 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20  cing operation. 
16c7e 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20 74 6f   NB is the.** to
16c7f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
16c80 67 65 73 20 74 68 61 74 20 70 61 72 74 69 63 69  ges that partici
16c81 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20  pate, including 
16c82 74 68 65 20 74 61 72 67 65 74 20 70 61 67 65 20  the target page 
16c83 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 62  and.** NN neighb
16c84 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69  ors on either si
16c85 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69  de..**.** The mi
16c86 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e  nimum value of N
16c87 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75 72 73  N is 1 (of cours
16c88 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e 67 20  e).  Increasing 
16c89 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20 28 74  NN above 1.** (t
16c8a 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65 73 20  o 2 or 3) gives 
16c8b 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f 76 65  a modest improve
16c8c 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54 20 61  ment in SELECT a
16c8d 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66 6f 72  nd DELETE perfor
16c8e 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78 63 68  mance.** in exch
16c8f 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72 67 65  ange for a large
16c90 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20 69 6e  r degradation in
16c91 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50 44 41   INSERT and UPDA
16c92 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a  TE performance..
16c93 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
16c94 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20 67 69  NN appears to gi
16c95 76 65 20 74 68 65 20 62 65 73 74 20 72 65 73 75  ve the best resu
16c96 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a  lts overall..*/.
16c97 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20 20 20  #define NN 1    
16c98 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
16c99 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 20  er of neighbors 
16c9a 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f  on either side o
16c9b 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65 66 69  f pPage */.#defi
16c9c 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 20  ne NB (NN*2+1)  
16c9d 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70 61 67      /* Total pag
16c9e 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74  es involved in t
16c9f 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a 2f  he balance */../
16ca0 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65  * Forward refere
16ca1 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nce */.static in
16ca2 74 20 62 61 6c 61 6e 63 65 28 42 74 43 75 72 73  t balance(BtCurs
16ca3 6f 72 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 6e  or*, int);..#ifn
16ca4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
16ca5 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a  QUICKBALANCE./*.
16ca6 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
16ca7 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61 6e  of balance() han
16ca8 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20  dles the common 
16ca9 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65  special case whe
16caa 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72  re.** a new entr
16cab 79 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72  y is being inser
16cac 74 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72 65  ted on the extre
16cad 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66 20  me right-end of 
16cae 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20  the.** tree, in 
16caf 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68 65  other words, whe
16cb0 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  n the new entry 
16cb1 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20  will become the 
16cb2 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79  largest.** entry
16cb3 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a   in the tree..**
16cb4 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20 74  .** Instead of t
16cb5 72 79 69 6e 67 20 62 61 6c 61 6e 63 65 20 74 68  rying balance th
16cb6 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c  e 3 right-most l
16cb7 65 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74 20  eaf pages, just 
16cb8 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67  add.** a new pag
16cb9 65 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 68  e to the right-h
16cba 61 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75 74  and side and put
16cbb 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74   the one new ent
16cbc 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61  ry in.** that pa
16cbd 67 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73  ge.  This leaves
16cbe 20 74 68 65 20 72 69 67 68 74 20 73 69 64 65 20   the right side 
16cbf 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d 65  of the tree some
16cc0 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63  what.** unbalanc
16cc1 65 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61 72  ed.  But odds ar
16cc2 65 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62  e that we will b
16cc3 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20  e inserting new 
16cc4 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74 68  entries.** at th
16cc5 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72  e end soon after
16cc6 77 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65 61  wards so the nea
16cc7 72 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20 77  rly empty page w
16cc8 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66  ill quickly.** f
16cc9 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65 72  ill up.  On aver
16cca 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65  age..**.** pPage
16ccb 20 69 73 20 74 68 65 20 6c 65 61 66 20 70 61 67   is the leaf pag
16ccc 65 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72  e which is the r
16ccd 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20 69  ight-most page i
16cce 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70  n the tree..** p
16ccf 50 61 72 65 6e 74 20 69 73 20 69 74 73 20 70 61  Parent is its pa
16cd0 72 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75 73  rent.  pPage mus
16cd1 74 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20  t have a single 
16cd2 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a  overflow entry.*
16cd3 2a 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f 20  * which is also 
16cd4 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65  the right-most e
16cd5 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67 65  ntry on the page
16cd6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
16cd7 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 42 74  balance_quick(Bt
16cd8 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
16cd9 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61   int rc;.  MemPa
16cda 67 65 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20  ge *pNew = 0;.  
16cdb 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 0a 20 20  Pgno pgnoNew;.  
16cdc 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 75 31 36  u8 *pCell;.  u16
16cdd 20 73 7a 43 65 6c 6c 3b 0a 20 20 43 65 6c 6c 49   szCell;.  CellI
16cde 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 4d 65 6d 50  nfo info;.  MemP
16cdf 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75  age *pPage = pCu
16ce0 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16ce1 69 50 61 67 65 5d 3b 0a 20 20 4d 65 6d 50 61 67  iPage];.  MemPag
16ce2 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 43 75  e *pParent = pCu
16ce3 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16ce4 69 50 61 67 65 2d 31 5d 3b 0a 20 20 42 74 53 68  iPage-1];.  BtSh
16ce5 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67  ared *pBt = pPag
16ce6 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 70 61  e->pBt;.  int pa
16ce7 72 65 6e 74 49 64 78 20 3d 20 70 50 61 72 65 6e  rentIdx = pParen
16ce8 74 2d 3e 6e 43 65 6c 6c 3b 20 20 20 2f 2a 20 70  t->nCell;   /* p
16ce9 50 61 72 65 6e 74 20 6e 65 77 20 64 69 76 69 64  Parent new divid
16cea 65 72 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f  er cell index */
16ceb 0a 20 20 69 6e 74 20 70 61 72 65 6e 74 53 69 7a  .  int parentSiz
16cec 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
16ced 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
16cee 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c  new divider cell
16cef 20 2a 2f 0a 20 20 75 38 20 70 61 72 65 6e 74 43   */.  u8 parentC
16cf0 65 6c 6c 5b 36 34 5d 3b 20 20 20 20 20 20 20 20  ell[64];        
16cf1 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
16cf2 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 69 76   for the new div
16cf3 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20  ider cell */..  
16cf4 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16cf5 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
16cf6 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
16cf7 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
16cf8 61 20 6e 65 77 20 70 61 67 65 2e 20 49 6e 73 65  a new page. Inse
16cf9 72 74 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  rt the overflow 
16cfa 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 0a  cell from pPage.
16cfb 20 20 2a 2a 20 69 6e 74 6f 20 69 74 2e 20 54 68    ** into it. Th
16cfc 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 6f 76  en remove the ov
16cfd 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f 6d  erflow cell from
16cfe 20 70 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72   pPage..  */.  r
16cff 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  c = allocateBtre
16d00 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77  ePage(pBt, &pNew
16d01 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30  , &pgnoNew, 0, 0
16d02 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
16d03 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 43  ITE_OK ){.    pC
16d04 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76  ell = pPage->aOv
16d05 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20  fl[0].pCell;.   
16d06 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69   szCell = cellSi
16d07 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  zePtr(pPage, pCe
16d08 6c 6c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ll);.    assert(
16d09 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
16d0a 72 69 74 65 61 62 6c 65 28 70 4e 65 77 2d 3e 70  riteable(pNew->p
16d0b 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 7a  DbPage) );.    z
16d0c 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 50  eroPage(pNew, pP
16d0d 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 29 3b 0a  age->aData[0]);.
16d0e 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65      assemblePage
16d0f 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65 6c 6c  (pNew, 1, &pCell
16d10 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 20 20 20  , &szCell);.    
16d11 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
16d12 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 2f 2a 20   = 0;.  .    /* 
16d13 70 50 61 67 65 20 69 73 20 63 75 72 72 65 6e 74  pPage is current
16d14 6c 79 20 74 68 65 20 72 69 67 68 74 2d 63 68 69  ly the right-chi
16d15 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 2e 20 43  ld of pParent. C
16d16 68 61 6e 67 65 20 74 68 69 73 0a 20 20 20 20 2a  hange this.    *
16d17 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 69  * so that the ri
16d18 67 68 74 2d 63 68 69 6c 64 20 69 73 20 74 68 65  ght-child is the
16d19 20 6e 65 77 20 70 61 67 65 20 61 6c 6c 6f 63 61   new page alloca
16d1a 74 65 64 20 61 62 6f 76 65 20 61 6e 64 0a 20 20  ted above and.  
16d1b 20 20 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68    ** pPage is th
16d1c 65 20 6e 65 78 74 2d 74 6f 2d 72 69 67 68 74 20  e next-to-right 
16d1d 63 68 69 6c 64 2e 20 0a 20 20 20 20 2a 2a 0a 20  child. .    **. 
16d1e 20 20 20 2a 2a 20 49 67 6e 6f 72 65 20 74 68 65     ** Ignore the
16d1f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
16d20 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 66 69 6c   the call to fil
16d21 6c 49 6e 43 65 6c 6c 28 29 2e 20 66 69 6c 6c 49  lInCell(). fillI
16d22 6e 43 65 6c 6c 28 29 0a 20 20 20 20 2a 2a 20 6d  nCell().    ** m
16d23 61 79 20 6f 6e 6c 79 20 72 65 74 75 72 6e 20 6f  ay only return o
16d24 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
16d25 5f 4f 4b 20 69 66 20 69 74 20 69 73 20 72 65 71  _OK if it is req
16d26 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74  uired to allocat
16d27 65 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6f 72 20  e.    ** one or 
16d28 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  more overflow pa
16d29 67 65 73 2e 20 53 69 6e 63 65 20 61 6e 20 69 6e  ges. Since an in
16d2a 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 42 2d 54  ternal table B-T
16d2b 72 65 65 20 63 65 6c 6c 20 0a 20 20 20 20 2a 2a  ree cell .    **
16d2c 20 6d 61 79 20 6e 65 76 65 72 20 73 70 69 6c 6c   may never spill
16d2d 20 6f 76 65 72 20 6f 6e 74 6f 20 61 6e 20 6f 76   over onto an ov
16d2e 65 72 66 6c 6f 77 20 70 61 67 65 20 28 69 74 20  erflow page (it 
16d2f 69 73 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20  is a maximum of 
16d30 0a 20 20 20 20 2a 2a 20 31 33 20 62 79 74 65 73  .    ** 13 bytes
16d31 20 69 6e 20 73 69 7a 65 29 2c 20 69 74 20 69 73   in size), it is
16d32 20 6e 6f 74 20 6e 65 63 63 65 73 73 61 72 79 20   not neccessary 
16d33 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72 65 74  to check the ret
16d34 75 72 6e 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2a  urn code..    **
16d35 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c  .    ** Similarl
16d36 79 2c 20 74 68 65 20 69 6e 73 65 72 74 43 65 6c  y, the insertCel
16d37 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  l() function can
16d38 6e 6f 74 20 66 61 69 6c 20 69 66 20 74 68 65 20  not fail if the 
16d39 70 61 67 65 0a 20 20 20 20 2a 2a 20 62 65 69 6e  page.    ** bein
16d3a 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
16d3b 69 73 20 61 6c 72 65 61 64 79 20 77 72 69 74 61  is already writa
16d3c 62 6c 65 20 61 6e 64 20 74 68 65 20 63 65 6c 6c  ble and the cell
16d3d 20 64 6f 65 73 20 6e 6f 74 20 0a 20 20 20 20 2a   does not .    *
16d3e 2a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 6f 76 65  * contain an ove
16d3f 72 66 6c 6f 77 20 70 6f 69 6e 74 65 72 2e 20 53  rflow pointer. S
16d40 6f 20 69 67 6e 6f 72 65 20 74 68 69 73 20 72 65  o ignore this re
16d41 74 75 72 6e 20 63 6f 64 65 20 74 6f 6f 2e 0a 20  turn code too.. 
16d42 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
16d43 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30  ( pPage->nCell>0
16d44 20 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20   );.    pCell = 
16d45 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
16d46 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 29 3b  pPage->nCell-1);
16d47 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
16d48 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
16d49 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
16d4a 6f 29 3b 0a 20 20 20 20 66 69 6c 6c 49 6e 43 65  o);.    fillInCe
16d4b 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72 65  ll(pParent, pare
16d4c 6e 74 43 65 6c 6c 2c 20 30 2c 20 69 6e 66 6f 2e  ntCell, 0, info.
16d4d 6e 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c 20 26  nKey, 0, 0, 0, &
16d4e 70 61 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20  parentSize);.   
16d4f 20 61 73 73 65 72 74 28 20 70 61 72 65 6e 74 53   assert( parentS
16d50 69 7a 65 3c 36 34 20 29 3b 0a 20 20 20 20 61 73  ize<64 );.    as
16d51 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
16d52 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
16d53 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20  arent->pDbPage) 
16d54 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 43 65 6c  );.    insertCel
16d55 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72 65 6e  l(pParent, paren
16d56 74 49 64 78 2c 20 70 61 72 65 6e 74 43 65 6c 6c  tIdx, parentCell
16d57 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c 20 30 2c  , parentSize, 0,
16d58 20 34 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74   4);.    put4byt
16d59 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65  e(findOverflowCe
16d5a 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61 72 65 6e  ll(pParent,paren
16d5b 74 49 64 78 29 2c 20 70 50 61 67 65 2d 3e 70 67  tIdx), pPage->pg
16d5c 6e 6f 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74  no);.    put4byt
16d5d 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74  e(&pParent->aDat
16d5e 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66  a[pParent->hdrOf
16d5f 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77  fset+8], pgnoNew
16d60 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20  );.  .    /* If 
16d61 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d  this is an auto-
16d62 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c  vacuum database,
16d63 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e   update the poin
16d64 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77  ter map.    ** w
16d65 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20  ith entries for 
16d66 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e  the new page, an
16d67 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72  d any pointer fr
16d68 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  om the .    ** c
16d69 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20  ell on the page 
16d6a 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  to an overflow p
16d6b 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  age..    */.    
16d6c 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
16d6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
16d6e 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67  trmapPut(pBt, pg
16d6f 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42 54  noNew, PTRMAP_BT
16d70 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67  REE, pParent->pg
16d71 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  no);.      if( r
16d72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16d73 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72          rc = ptr
16d74 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c  mapPutOvfl(pNew,
16d75 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
16d76 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61   }..    /* Relea
16d77 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  se the reference
16d78 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65   to the new page
16d79 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61 73 65  . */.    release
16d7a 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 7d 0a  Page(pNew);.  }.
16d7b 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
16d7c 69 6e 74 20 74 68 65 20 70 50 61 67 65 2d 3e 6e  int the pPage->n
16d7d 46 72 65 65 20 76 61 72 69 61 62 6c 65 20 69 73  Free variable is
16d7e 20 6e 6f 74 20 73 65 74 20 63 6f 72 72 65 63 74   not set correct
16d7f 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 72 65 73  ly with.  ** res
16d80 70 65 63 74 20 74 6f 20 74 68 65 20 63 6f 6e 74  pect to the cont
16d81 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 20  ent of the page 
16d82 28 62 65 63 61 75 73 65 20 69 74 20 77 61 73 20  (because it was 
16d83 73 65 74 20 74 6f 20 30 20 62 79 20 0a 20 20 2a  set to 0 by .  *
16d84 2a 20 69 6e 73 65 72 74 43 65 6c 6c 29 2e 20 53  * insertCell). S
16d85 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 42 74  o call sqlite3Bt
16d86 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 74 6f  reeInitPage() to
16d87 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
16d88 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 2e 0a 20  .  ** correct.. 
16d89 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 68 61   **.  ** This ha
16d8a 73 20 74 6f 20 62 65 20 64 6f 6e 65 20 65 76 65  s to be done eve
16d8b 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20 77 69  n if an error wi
16d8c 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20  ll be returned. 
16d8d 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 0a 20 20 2a  Normally, if.  *
16d8e 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  * an error occur
16d8f 73 20 64 75 72 69 6e 67 20 74 72 65 65 20 62 61  s during tree ba
16d90 6c 61 6e 63 69 6e 67 2c 20 74 68 65 20 63 6f 6e  lancing, the con
16d91 74 65 6e 74 73 20 6f 66 20 4d 65 6d 50 61 67 65  tents of MemPage
16d92 20 61 72 65 0a 20 20 2a 2a 20 6e 6f 74 20 69 6d   are.  ** not im
16d93 70 6f 72 74 61 6e 74 2c 20 61 73 20 74 68 65 79  portant, as they
16d94 20 77 69 6c 6c 20 62 65 20 72 65 63 61 6c 63 75   will be recalcu
16d95 6c 61 74 65 64 20 77 68 65 6e 20 74 68 65 20 70  lated when the p
16d96 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 0a 20 20  age is rolled.  
16d97 2a 2a 20 62 61 63 6b 2e 20 42 75 74 20 68 65 72  ** back. But her
16d98 65 2c 20 69 6e 20 62 61 6c 61 6e 63 65 5f 71 75  e, in balance_qu
16d99 69 63 6b 28 29 2c 20 69 74 20 69 73 20 70 6f 73  ick(), it is pos
16d9a 73 69 62 6c 65 20 74 68 61 74 20 70 50 61 67 65  sible that pPage
16d9b 20 68 61 73 20 0a 20 20 2a 2a 20 6e 6f 74 20 79   has .  ** not y
16d9c 65 74 20 62 65 65 6e 20 6d 61 72 6b 65 64 20 64  et been marked d
16d9d 69 72 74 79 20 6f 72 20 77 72 69 74 74 65 6e 20  irty or written 
16d9e 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
16d9f 20 66 69 6c 65 2e 20 54 68 65 72 65 66 6f 72 65   file. Therefore
16da0 0a 20 20 2a 2a 20 69 74 20 77 69 6c 6c 20 6e 6f  .  ** it will no
16da1 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  t be rolled back
16da2 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20 69 6d   and so it is im
16da3 70 6f 72 74 61 6e 74 20 74 6f 20 6d 61 6b 65 20  portant to make 
16da4 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a 20 74  sure that.  ** t
16da5 68 65 20 70 61 67 65 20 64 61 74 61 20 61 6e 64  he page data and
16da6 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 6d   contents of Mem
16da7 50 61 67 65 20 61 72 65 20 63 6f 6e 73 69 73 74  Page are consist
16da8 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67  ent..  */.  pPag
16da9 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20  e->isInit = 0;. 
16daa 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69   sqlite3BtreeIni
16dab 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  tPage(pPage);.  
16dac 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
16dad 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a  Overflow==0 );..
16dae 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68 69    /* If everythi
16daf 6e 67 20 65 6c 73 65 20 73 75 63 63 65 65 64 65  ng else succeede
16db0 64 2c 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70  d, balance the p
16db1 61 72 65 6e 74 20 70 61 67 65 2c 20 69 6e 20 0a  arent page, in .
16db2 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 64 69    ** case the di
16db3 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 73 65 72  vider cell inser
16db4 74 65 64 20 63 61 75 73 65 64 20 69 74 20 74 6f  ted caused it to
16db5 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c   become overfull
16db6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ..  */.  if( rc=
16db7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
16db8 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
16db9 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e  age);.    pCur->
16dba 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 72 63 20  iPage--;.    rc 
16dbb 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c 20  = balance(pCur, 
16dbc 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
16dbd 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a   rc;.}.#endif /*
16dbe 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49   SQLITE_OMIT_QUI
16dbf 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 2f 2a  CKBALANCE */../*
16dc0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16dc1 20 72 65 64 69 73 74 72 69 62 75 74 65 73 20 43   redistributes C
16dc2 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 20 61 6e  ells on pPage an
16dc3 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20 73 69 62  d up to NN*2 sib
16dc4 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70 50 61 67  lings.** of pPag
16dc5 65 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70 61  e so that all pa
16dc6 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 20 74  ges have about t
16dc7 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f  he same amount o
16dc8 66 20 66 72 65 65 20 73 70 61 63 65 2e 0a 2a 2a  f free space..**
16dc9 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73 69 62 6c   Usually NN sibl
16dca 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 20 73  ings on either s
16dcb 69 64 65 20 6f 66 20 70 50 61 67 65 20 69 73 20  ide of pPage is 
16dcc 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61  used in the bala
16dcd 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f 75 67 68  ncing,.** though
16dce 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 6d   more siblings m
16dcf 69 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d 20 6f  ight come from o
16dd0 6e 65 20 73 69 64 65 20 69 66 20 70 50 61 67 65  ne side if pPage
16dd1 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   is the first.**
16dd2 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f   or last child o
16dd3 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 49  f its parent.  I
16dd4 66 20 70 50 61 67 65 20 68 61 73 20 66 65 77 65  f pPage has fewe
16dd5 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73 69 62 6c  r than 2*NN sibl
16dd6 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65 74 68 69  ings.** (somethi
16dd7 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c  ng which can onl
16dd8 79 20 68 61 70 70 65 6e 20 69 66 20 70 50 61 67  y happen if pPag
16dd9 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61  e is the root pa
16dda 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63 68 69 6c  ge or a .** chil
16ddb 64 20 6f 66 20 72 6f 6f 74 29 20 74 68 65 6e 20  d of root) then 
16ddc 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 69  all available si
16ddd 62 6c 69 6e 67 73 20 70 61 72 74 69 63 69 70 61  blings participa
16dde 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63  te in the balanc
16ddf 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ing..**.** The n
16de0 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67  umber of sibling
16de1 73 20 6f 66 20 70 50 61 67 65 20 6d 69 67 68 74  s of pPage might
16de2 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 6f 72   be increased or
16de3 20 64 65 63 72 65 61 73 65 64 20 62 79 20 6f 6e   decreased by on
16de4 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69 6e 20 61  e or.** two in a
16de5 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70  n effort to keep
16de6 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20 66 75   pages nearly fu
16de7 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 20  ll but not over 
16de8 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f 74 20 70  full. The root p
16de9 61 67 65 0a 2a 2a 20 69 73 20 73 70 65 63 69 61  age.** is specia
16dea 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f 77 65 64  l and is allowed
16deb 20 74 6f 20 62 65 20 6e 65 61 72 6c 79 20 65 6d   to be nearly em
16dec 70 74 79 2e 20 49 66 20 70 50 61 67 65 20 69 73  pty. If pPage is
16ded 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61   .** the root pa
16dee 67 65 2c 20 74 68 65 6e 20 74 68 65 20 64 65 70  ge, then the dep
16def 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 6d  th of the tree m
16df0 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65  ight be increase
16df1 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65 61 73 65  d.** or decrease
16df2 64 20 62 79 20 6f 6e 65 2c 20 61 73 20 6e 65 63  d by one, as nec
16df3 65 73 73 61 72 79 2c 20 74 6f 20 6b 65 65 70 20  essary, to keep 
16df4 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 66 72  the root page fr
16df5 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f 76 65 72  om being.** over
16df6 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c 65 74 65  full or complete
16df7 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20  ly empty..**.** 
16df8 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 74  Note that when t
16df9 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
16dfa 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 20 74  alled, some of t
16dfb 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67  he Cells on pPag
16dfc 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 61  e.** might not a
16dfd 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f 72 65  ctually be store
16dfe 64 20 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74  d in pPage->aDat
16dff 61 5b 5d 2e 20 20 54 68 69 73 20 63 61 6e 20 68  a[].  This can h
16e00 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 65 20  appen.** if the 
16e01 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c 6c  page is overfull
16e02 2e 20 20 50 61 72 74 20 6f 66 20 74 68 65 20 6a  .  Part of the j
16e03 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  ob of this routi
16e04 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d 61 6b 65  ne is to.** make
16e05 20 73 75 72 65 20 61 6c 6c 20 43 65 6c 6c 73 20   sure all Cells 
16e06 66 6f 72 20 70 50 61 67 65 20 6f 6e 63 65 20 61  for pPage once a
16e07 67 61 69 6e 20 66 69 74 20 69 6e 20 70 50 61 67  gain fit in pPag
16e08 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a 2a 0a 2a  e->aData[]..**.*
16e09 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 73 65 20  * In the course 
16e0a 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65  of balancing the
16e0b 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70 50 61   siblings of pPa
16e0c 67 65 2c 20 74 68 65 20 70 61 72 65 6e 74 20 6f  ge, the parent o
16e0d 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68 74  f pPage.** might
16e0e 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c   become overfull
16e0f 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 20   or underfull.  
16e10 49 66 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c  If that happens,
16e11 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
16e12 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  ne.** is called 
16e13 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20 74  recursively on t
16e14 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a  he parent..**.**
16e15 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
16e16 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72   fails for any r
16e17 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20  eason, it might 
16e18 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 61  leave the databa
16e19 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75  se.** in a corru
16e1a 70 74 65 64 20 73 74 61 74 65 2e 20 20 53 6f 20  pted state.  So 
16e1b 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  if this routine 
16e1c 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62  fails, the datab
16e1d 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  ase should.** be
16e1e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f   rolled back..*/
16e1f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61  .static int bala
16e20 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 42 74 43 75  nce_nonroot(BtCu
16e21 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d  rsor *pCur){.  M
16e22 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20  emPage *pPage;  
16e23 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
16e24 68 65 20 6f 76 65 72 20 6f 72 20 75 6e 64 65 72  he over or under
16e25 66 75 6c 6c 20 70 61 67 65 20 74 6f 20 62 61 6c  full page to bal
16e26 61 6e 63 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ance */.  MemPag
16e27 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20  e *pParent;     
16e28 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
16e29 72 65 6e 74 20 6f 66 20 70 50 61 67 65 20 2a 2f  rent of pPage */
16e2a 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
16e2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
16e2c 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61 74  /* The whole dat
16e2d 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  abase */.  int n
16e2e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20  Cell = 0;       
16e2f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
16e30 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70  r of cells in ap
16e31 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  Cell[] */.  int 
16e32 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20  nMaxCells = 0;  
16e33 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f           /* Allo
16e34 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 70  cated size of ap
16e35 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46  Cell, szCell, aF
16e36 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  rom. */.  int nO
16e37 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ld = 0;         
16e38 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
16e39 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 4f   of pages in apO
16e3a 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e  ld[] */.  int nN
16e3b 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ew = 0;         
16e3c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
16e3d 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 4e   of pages in apN
16e3e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  ew[] */.  int nD
16e3f 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  iv;             
16e40 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
16e41 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 44   of cells in apD
16e42 69 76 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c  iv[] */.  int i,
16e43 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 20   j, k;          
16e44 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
16e45 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  ounters */.  int
16e46 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20   idx;           
16e47 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
16e48 65 78 20 6f 66 20 70 50 61 67 65 20 69 6e 20 70  ex of pPage in p
16e49 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20  Parent->aCell[] 
16e4a 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b 20  */.  int nxDiv; 
16e4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16e4c 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 69 64 65    /* Next divide
16e4d 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e  r slot in pParen
16e4e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20  t->aCell[] */.  
16e4f 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
16e50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16e51 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  The return code 
16e52 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 43 6f 72  */.  int leafCor
16e53 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20  rection;        
16e54 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67 65 20    /* 4 if pPage 
16e55 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66  is a leaf.  0 if
16e56 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65   not */.  int le
16e57 61 66 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  afData;         
16e58 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
16e59 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61  f pPage is a lea
16e5a 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54 41 20  f of a LEAFDATA 
16e5b 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73  tree */.  int us
16e5c 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20 20 20  ableSpace;      
16e5d 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
16e5e 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e 64 20  in pPage beyond 
16e5f 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
16e60 69 6e 74 20 70 61 67 65 46 6c 61 67 73 3b 20 20  int pageFlags;  
16e61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16e62 56 61 6c 75 65 20 6f 66 20 70 50 61 67 65 2d 3e  Value of pPage->
16e63 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e  aData[0] */.  in
16e64 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 20 20 20  t subtotal;     
16e65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75             /* Su
16e66 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 65 73 20  btotal of bytes 
16e67 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20  in cells on one 
16e68 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53  page */.  int iS
16e69 70 61 63 65 31 20 3d 20 30 3b 20 20 20 20 20 20  pace1 = 0;      
16e6a 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
16e6b 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 61  unused byte of a
16e6c 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e  Space1[] */.  in
16e6d 74 20 69 53 70 61 63 65 32 20 3d 20 30 3b 20 20  t iSpace2 = 0;  
16e6e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
16e6f 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 20  rst unused byte 
16e70 6f 66 20 61 53 70 61 63 65 32 5b 5d 20 2a 2f 0a  of aSpace2[] */.
16e71 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b    int szScratch;
16e72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16e73 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74 63  * Size of scratc
16e74 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74  h memory request
16e75 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ed */.  MemPage 
16e76 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20  *apOld[NB];     
16e77 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e       /* pPage an
16e78 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c  d up to two sibl
16e79 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  ings */.  Pgno p
16e7a 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20  gnoOld[NB];     
16e7b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
16e7c 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68 20  umbers for each 
16e7d 70 61 67 65 20 69 6e 20 61 70 4f 6c 64 5b 5d 20  page in apOld[] 
16e7e 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70  */.  MemPage *ap
16e7f 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20  Copy[NB];       
16e80 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 6f 70    /* Private cop
16e81 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70  ies of apOld[] p
16e82 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ages */.  MemPag
16e83 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20  e *apNew[NB+2]; 
16e84 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20         /* pPage 
16e85 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 69 62  and up to NB sib
16e86 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 6c 61  lings after bala
16e87 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67 6e 6f 20  ncing */.  Pgno 
16e88 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20  pgnoNew[NB+2];  
16e89 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
16e8a 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68  numbers for each
16e8b 20 70 61 67 65 20 69 6e 20 61 70 4e 65 77 5b 5d   page in apNew[]
16e8c 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 76 5b   */.  u8 *apDiv[
16e8d 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  NB];            
16e8e 20 20 20 2f 2a 20 44 69 76 69 64 65 72 20 63 65     /* Divider ce
16e8f 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 2a  lls in pParent *
16e90 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b 4e  /.  int cntNew[N
16e91 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20  B+2];           
16e92 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 43 65   /* Index in aCe
16e93 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66 74  ll[] of cell aft
16e94 65 72 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a  er i-th page */.
16e95 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b 32    int szNew[NB+2
16e96 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ];             /
16e97 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a 65 20  * Combined size 
16e98 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 65 20 6f  of cells place o
16e99 6e 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20  n i-th page */. 
16e9a 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 30   u8 **apCell = 0
16e9b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
16e9c 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67 69 6e   All cells begin
16e9d 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 75   balanced */.  u
16e9e 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 20  16 *szCell;     
16e9f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
16ea0 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c  ocal size of all
16ea1 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c   cells in apCell
16ea2 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 43 6f 70  [] */.  u8 *aCop
16ea3 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 2f  y[NB];         /
16ea4 2a 20 53 70 61 63 65 20 66 6f 72 20 68 6f 6c 64  * Space for hold
16ea5 69 6e 67 20 64 61 74 61 20 6f 66 20 61 70 43 6f  ing data of apCo
16ea6 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53  py[] */.  u8 *aS
16ea7 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 20 20  pace1;          
16ea8 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 6f   /* Space for co
16ea9 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73  pies of dividers
16eaa 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 62 61   cells before ba
16eab 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a 61  lance */.  u8 *a
16eac 53 70 61 63 65 32 20 3d 20 30 3b 20 20 20 20 20  Space2 = 0;     
16ead 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6f    /* Space for o
16eae 76 65 72 66 6c 6f 77 20 64 69 76 69 64 65 72 73  verflow dividers
16eaf 20 63 65 6c 6c 73 20 61 66 74 65 72 20 62 61 6c   cells after bal
16eb0 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a 61 46  ance */.  u8 *aF
16eb1 72 6f 6d 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67  rom = 0;..  pPag
16eb2 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
16eb3 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
16eb4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16eb5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
16eb6 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
16eb7 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 43  ;.  VVA_ONLY( pC
16eb8 75 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65  ur->pagesShuffle
16eb9 64 20 3d 20 31 20 29 3b 0a 0a 20 20 2f 2a 20 0a  d = 1 );..  /* .
16eba 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 70 61    ** Find the pa
16ebb 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a  rent page..  */.
16ebc 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16ebd 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 73  iPage>0 );.  ass
16ebe 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e  ert( pPage->isIn
16ebf 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  it );.  assert( 
16ec0 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
16ec1 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
16ec2 44 62 50 61 67 65 29 20 7c 7c 20 70 50 61 67 65  DbPage) || pPage
16ec3 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29  ->nOverflow==1 )
16ec4 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d  ;.  pBt = pPage-
16ec5 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 6e 74 20  >pBt;.  pParent 
16ec6 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
16ec7 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a 20  Cur->iPage-1];. 
16ec8 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
16ec9 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45   );.  if( SQLITE
16eca 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK!=(rc = sqlit
16ecb 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
16ecc 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 29 20  rent->pDbPage)) 
16ecd 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61  ){.    goto bala
16ece 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  nce_cleanup;.  }
16ecf 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41  ..  TRACE(("BALA
16ed0 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 65 20  NCE: begin page 
16ed1 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e  %d child of %d\n
16ed2 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20  ", pPage->pgno, 
16ed3 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b  pParent->pgno));
16ed4 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
16ed5 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e  _OMIT_QUICKBALAN
16ed6 43 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 20 73  CE.  /*.  ** A s
16ed7 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 49 66  pecial case:  If
16ed8 20 61 20 6e 65 77 20 65 6e 74 72 79 20 68 61 73   a new entry has
16ed9 20 6a 75 73 74 20 62 65 65 6e 20 69 6e 73 65 72   just been inser
16eda 74 65 64 20 69 6e 74 6f 20 61 0a 20 20 2a 2a 20  ted into a.  ** 
16edb 74 61 62 6c 65 20 28 74 68 61 74 20 69 73 2c 20  table (that is, 
16edc 61 20 62 74 72 65 65 20 77 69 74 68 20 69 6e 74  a btree with int
16edd 65 67 65 72 20 6b 65 79 73 20 61 6e 64 20 61 6c  eger keys and al
16ede 6c 20 64 61 74 61 20 61 74 20 74 68 65 20 6c 65  l data at the le
16edf 61 76 65 73 29 0a 20 20 2a 2a 20 61 6e 64 20 74  aves).  ** and t
16ee0 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20  he new entry is 
16ee1 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65  the right-most e
16ee2 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 65 65  ntry in the tree
16ee3 20 28 69 74 20 68 61 73 20 74 68 65 0a 20 20 2a   (it has the.  *
16ee4 2a 20 6c 61 72 67 65 73 74 20 6b 65 79 29 20 74  * largest key) t
16ee5 68 65 6e 20 75 73 65 20 74 68 65 20 73 70 65 63  hen use the spec
16ee6 69 61 6c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63  ial balance_quic
16ee7 6b 28 29 20 72 6f 75 74 69 6e 65 20 66 6f 72 0a  k() routine for.
16ee8 20 20 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 2e 20    ** balancing. 
16ee9 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29   balance_quick()
16eea 20 69 73 20 6d 75 63 68 20 66 61 73 74 65 72 20   is much faster 
16eeb 61 6e 64 20 72 65 73 75 6c 74 73 20 69 6e 20 61  and results in a
16eec 20 74 69 67 68 74 65 72 0a 20 20 2a 2a 20 70 61   tighter.  ** pa
16eed 63 6b 69 6e 67 20 6f 66 20 64 61 74 61 20 69 6e  cking of data in
16eee 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65   the common case
16eef 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
16ef0 67 65 2d 3e 6c 65 61 66 20 26 26 0a 20 20 20 20  ge->leaf &&.    
16ef1 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20    pPage->intKey 
16ef2 26 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e  &&.      pPage->
16ef3 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 26 26 0a  nOverflow==1 &&.
16ef4 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76        pPage->aOv
16ef5 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65  fl[0].idx==pPage
16ef6 2d 3e 6e 43 65 6c 6c 20 26 26 0a 20 20 20 20 20  ->nCell &&.     
16ef7 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d   pParent->pgno!=
16ef8 31 20 26 26 0a 20 20 20 20 20 20 67 65 74 34 62  1 &&.      get4b
16ef9 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44  yte(&pParent->aD
16efa 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72  ata[pParent->hdr
16efb 4f 66 66 73 65 74 2b 38 5d 29 3d 3d 70 50 61 67  Offset+8])==pPag
16efc 65 2d 3e 70 67 6e 6f 0a 20 20 29 7b 0a 20 20 20  e->pgno.  ){.   
16efd 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
16efe 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 2f 2a  intKey );.    /*
16eff 0a 20 20 20 20 2a 2a 20 54 4f 44 4f 3a 20 43 68  .    ** TODO: Ch
16f00 65 63 6b 20 74 68 65 20 73 69 62 6c 69 6e 67 73  eck the siblings
16f01 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
16f02 70 50 61 67 65 2e 20 49 74 20 6d 61 79 20 62 65  pPage. It may be
16f03 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65   that.    ** the
16f04 79 20 61 72 65 20 6e 6f 74 20 66 75 6c 6c 20 61  y are not full a
16f05 6e 64 20 6e 6f 20 6e 65 77 20 70 61 67 65 20 69  nd no new page i
16f06 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  s required..    
16f07 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 62 61  */.    return ba
16f08 6c 61 6e 63 65 5f 71 75 69 63 6b 28 70 43 75 72  lance_quick(pCur
16f09 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
16f0a 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
16f0b 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
16f0c 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  erWrite(pPage->p
16f0d 44 62 50 61 67 65 29 29 20 29 7b 0a 20 20 20 20  DbPage)) ){.    
16f0e 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
16f0f 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  anup;.  }..  /*.
16f10 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 65    ** Find the ce
16f11 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  ll in the parent
16f12 20 70 61 67 65 20 77 68 6f 73 65 20 6c 65 66 74   page whose left
16f13 20 63 68 69 6c 64 20 70 6f 69 6e 74 73 20 62 61   child points ba
16f14 63 6b 0a 20 20 2a 2a 20 74 6f 20 70 50 61 67 65  ck.  ** to pPage
16f15 2e 20 20 54 68 65 20 22 69 64 78 22 20 76 61 72  .  The "idx" var
16f16 69 61 62 6c 65 20 69 73 20 74 68 65 20 69 6e 64  iable is the ind
16f17 65 78 20 6f 66 20 74 68 61 74 20 63 65 6c 6c 2e  ex of that cell.
16f18 20 20 49 66 20 70 50 61 67 65 0a 20 20 2a 2a 20    If pPage.  ** 
16f19 69 73 20 74 68 65 20 72 69 67 68 74 6d 6f 73 74  is the rightmost
16f1a 20 63 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e   child of pParen
16f1b 74 20 74 68 65 6e 20 73 65 74 20 69 64 78 20 74  t then set idx t
16f1c 6f 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  o pParent->nCell
16f1d 20 0a 20 20 2a 2f 0a 20 20 69 64 78 20 3d 20 70   .  */.  idx = p
16f1e 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
16f1f 3e 69 50 61 67 65 2d 31 5d 3b 0a 20 20 61 73 73  >iPage-1];.  ass
16f20 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 70  ertParentIndex(p
16f21 50 61 72 65 6e 74 2c 20 69 64 78 2c 20 70 50 61  Parent, idx, pPa
16f22 67 65 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a  ge->pgno);..  /*
16f23 0a 20 20 2a 2a 20 46 69 6e 64 20 73 69 62 6c 69  .  ** Find sibli
16f24 6e 67 20 70 61 67 65 73 20 74 6f 20 70 50 61 67  ng pages to pPag
16f25 65 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 73 20  e and the cells 
16f26 69 6e 20 70 50 61 72 65 6e 74 20 74 68 61 74 20  in pParent that 
16f27 64 69 76 69 64 65 0a 20 20 2a 2a 20 74 68 65 20  divide.  ** the 
16f28 73 69 62 6c 69 6e 67 73 2e 20 20 41 6e 20 61 74  siblings.  An at
16f29 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f  tempt is made to
16f2a 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67   find NN sibling
16f2b 73 20 6f 6e 20 65 69 74 68 65 72 0a 20 20 2a 2a  s on either.  **
16f2c 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 2e 20   side of pPage. 
16f2d 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 61   More siblings a
16f2e 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e  re taken from on
16f2f 65 20 73 69 64 65 2c 20 68 6f 77 65 76 65 72 2c  e side, however,
16f30 20 69 66 0a 20 20 2a 2a 20 70 50 61 67 65 20 74   if.  ** pPage t
16f31 68 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74  here are fewer t
16f32 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20  han NN siblings 
16f33 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 73 69 64  on the other sid
16f34 65 2e 20 20 49 66 20 70 50 61 72 65 6e 74 0a 20  e.  If pParent. 
16f35 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 20 66 65   ** has NB or fe
16f36 77 65 72 20 63 68 69 6c 64 72 65 6e 20 74 68 65  wer children the
16f37 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f  n all children o
16f38 66 20 70 50 61 72 65 6e 74 20 61 72 65 20 74 61  f pParent are ta
16f39 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 6e 78 44 69  ken..  */.  nxDi
16f3a 76 20 3d 20 69 64 78 20 2d 20 4e 4e 3b 0a 20 20  v = idx - NN;.  
16f3b 69 66 28 20 6e 78 44 69 76 20 2b 20 4e 42 20 3e  if( nxDiv + NB >
16f3c 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20   pParent->nCell 
16f3d 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d 20 70  ){.    nxDiv = p
16f3e 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 2d 20  Parent->nCell - 
16f3f 4e 42 20 2b 20 31 3b 0a 20 20 7d 0a 20 20 69 66  NB + 1;.  }.  if
16f40 28 20 6e 78 44 69 76 3c 30 20 29 7b 0a 20 20 20  ( nxDiv<0 ){.   
16f41 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 7d 0a   nxDiv = 0;.  }.
16f42 20 20 6e 44 69 76 20 3d 20 30 3b 0a 20 20 66 6f    nDiv = 0;.  fo
16f43 72 28 69 3d 30 2c 20 6b 3d 6e 78 44 69 76 3b 20  r(i=0, k=nxDiv; 
16f44 69 3c 4e 42 3b 20 69 2b 2b 2c 20 6b 2b 2b 29 7b  i<NB; i++, k++){
16f45 0a 20 20 20 20 69 66 28 20 6b 3c 70 50 61 72 65  .    if( k<pPare
16f46 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20  nt->nCell ){.   
16f47 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69     apDiv[i] = fi
16f48 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  ndCell(pParent, 
16f49 6b 29 3b 0a 20 20 20 20 20 20 6e 44 69 76 2b 2b  k);.      nDiv++
16f4a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
16f4b 21 70 50 61 72 65 6e 74 2d 3e 6c 65 61 66 20 29  !pParent->leaf )
16f4c 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b  ;.      pgnoOld[
16f4d 69 5d 20 3d 20 67 65 74 34 62 79 74 65 28 61 70  i] = get4byte(ap
16f4e 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 7d 65 6c  Div[i]);.    }el
16f4f 73 65 20 69 66 28 20 6b 3d 3d 70 50 61 72 65 6e  se if( k==pParen
16f50 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20  t->nCell ){.    
16f51 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20 3d 20 67    pgnoOld[i] = g
16f52 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74  et4byte(&pParent
16f53 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d  ->aData[pParent-
16f54 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a  >hdrOffset+8]);.
16f55 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
16f56 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
16f57 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69    rc = getAndIni
16f58 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 4f  tPage(pBt, pgnoO
16f59 6c 64 5b 69 5d 2c 20 26 61 70 4f 6c 64 5b 69 5d  ld[i], &apOld[i]
16f5a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
16f5b 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
16f5c 61 6e 75 70 3b 0a 20 20 20 20 2f 2a 20 61 70 4f  anup;.    /* apO
16f5d 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72 65 6e 74  ld[i]->idxParent
16f5e 20 3d 20 6b 3b 20 2a 2f 0a 20 20 20 20 61 70 43   = k; */.    apC
16f5f 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  opy[i] = 0;.    
16f60 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f 6c 64 20  assert( i==nOld 
16f61 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b 3b 0a 20  );.    nOld++;. 
16f62 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d 20     nMaxCells += 
16f63 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65 6c  1+apOld[i]->nCel
16f64 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76 65  l+apOld[i]->nOve
16f65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rflow;.  }..  /*
16f66 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20   Make nMaxCells 
16f67 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 34 20  a multiple of 4 
16f68 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73  in order to pres
16f69 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a  erve 8-byte.  **
16f6a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20   alignment */.  
16f6b 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61  nMaxCells = (nMa
16f6c 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33 3b 0a  xCells + 3)&~3;.
16f6d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63  .  /*.  ** Alloc
16f6e 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65  ate space for me
16f6f 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 0a  mory structures.
16f70 20 20 2a 2f 0a 20 20 73 7a 53 63 72 61 74 63 68    */.  szScratch
16f71 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78 43 65   =.       nMaxCe
16f72 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20  lls*sizeof(u8*) 
16f73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16f74 20 20 20 20 20 20 2f 2a 20 61 70 43 65 6c 6c 20        /* apCell 
16f75 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 43 65  */.     + nMaxCe
16f76 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 29 20  lls*sizeof(u16) 
16f77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16f78 20 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c 6c 20        /* szCell 
16f79 2a 2f 0a 20 20 20 20 20 2b 20 28 52 4f 55 4e 44  */.     + (ROUND
16f7a 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65  8(sizeof(MemPage
16f7b 29 29 2b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  ))+pBt->pageSize
16f7c 29 2a 4e 42 20 20 2f 2a 20 61 43 6f 70 79 20 2a  )*NB  /* aCopy *
16f7d 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e 70 61  /.     + pBt->pa
16f7e 67 65 53 69 7a 65 20 20 20 20 20 20 20 20 20 20  geSize          
16f7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16f80 20 20 20 20 20 2f 2a 20 61 53 70 61 63 65 31 20       /* aSpace1 
16f81 2a 2f 0a 20 20 20 20 20 2b 20 28 49 53 41 55 54  */.     + (ISAUT
16f82 4f 56 41 43 55 55 4d 20 3f 20 6e 4d 61 78 43 65  OVACUUM ? nMaxCe
16f83 6c 6c 73 20 3a 20 30 29 3b 20 20 20 20 20 20 20  lls : 0);       
16f84 20 20 20 20 20 20 2f 2a 20 61 46 72 6f 6d 20 2a        /* aFrom *
16f85 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c  /.  apCell = sql
16f86 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f  ite3ScratchMallo
16f87 63 28 20 73 7a 53 63 72 61 74 63 68 20 29 3b 20  c( szScratch ); 
16f88 0a 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30  .  if( apCell==0
16f89 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
16f8a 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
16f8b 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
16f8c 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43 65 6c  nup;.  }.  szCel
16f8d 6c 20 3d 20 28 75 31 36 2a 29 26 61 70 43 65 6c  l = (u16*)&apCel
16f8e 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20  l[nMaxCells];.  
16f8f 61 43 6f 70 79 5b 30 5d 20 3d 20 28 75 38 2a 29  aCopy[0] = (u8*)
16f90 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c  &szCell[nMaxCell
16f91 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49  s];.  assert( EI
16f92 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
16f93 4e 54 28 61 43 6f 70 79 5b 30 5d 29 20 29 3b 0a  NT(aCopy[0]) );.
16f94 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4e 42 3b    for(i=1; i<NB;
16f95 20 69 2b 2b 29 7b 0a 20 20 20 20 61 43 6f 70 79   i++){.    aCopy
16f96 5b 69 5d 20 3d 20 26 61 43 6f 70 79 5b 69 2d 31  [i] = &aCopy[i-1
16f97 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2b  ][pBt->pageSize+
16f98 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65  ROUND8(sizeof(Me
16f99 6d 50 61 67 65 29 29 5d 3b 0a 20 20 20 20 61 73  mPage))];.    as
16f9a 73 65 72 74 28 20 28 28 61 43 6f 70 79 5b 69 5d  sert( ((aCopy[i]
16f9b 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d   - (u8*)0) & 7)=
16f9c 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20  =0 ); /* 8-byte 
16f9d 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72  alignment requir
16f9e 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61 53 70 61  ed */.  }.  aSpa
16f9f 63 65 31 20 3d 20 26 61 43 6f 70 79 5b 4e 42 2d  ce1 = &aCopy[NB-
16fa0 31 5d 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  1][pBt->pageSize
16fa1 2b 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d  +ROUND8(sizeof(M
16fa2 65 6d 50 61 67 65 29 29 5d 3b 0a 20 20 61 73 73  emPage))];.  ass
16fa3 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
16fa4 41 4c 49 47 4e 4d 45 4e 54 28 61 53 70 61 63 65  ALIGNMENT(aSpace
16fa5 31 29 20 29 3b 0a 20 20 69 66 28 20 49 53 41 55  1) );.  if( ISAU
16fa6 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
16fa7 61 46 72 6f 6d 20 3d 20 26 61 53 70 61 63 65 31  aFrom = &aSpace1
16fa8 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 3b  [pBt->pageSize];
16fa9 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 32 20 3d  .  }.  aSpace2 =
16faa 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c   sqlite3PageMall
16fab 6f 63 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  oc(pBt->pageSize
16fac 29 3b 0a 20 20 69 66 28 20 61 53 70 61 63 65 32  );.  if( aSpace2
16fad 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
16fae 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
16faf 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63    goto balance_c
16fb0 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 0a 20  leanup;.  }.  . 
16fb1 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63 6f   /*.  ** Make co
16fb2 70 69 65 73 20 6f 66 20 74 68 65 20 63 6f 6e 74  pies of the cont
16fb3 65 6e 74 20 6f 66 20 70 50 61 67 65 20 61 6e 64  ent of pPage and
16fb4 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20 69 6e   its siblings in
16fb5 74 6f 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a 20  to aOld[]..  ** 
16fb6 54 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73  The rest of this
16fb7 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75   function will u
16fb8 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  se data from the
16fb9 20 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20   copies rather. 
16fba 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69   ** that the ori
16fbb 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63  ginal pages sinc
16fbc 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  e the original p
16fbd 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20  ages will be in 
16fbe 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73  the.  ** process
16fbf 20 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72   of being overwr
16fc0 69 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f  itten..  */.  fo
16fc1 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69  r(i=0; i<nOld; i
16fc2 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65  ++){.    MemPage
16fc3 20 2a 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20   *p = apCopy[i] 
16fc4 3d 20 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f 70  = (MemPage*)aCop
16fc5 79 5b 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79  y[i];.    memcpy
16fc6 28 70 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69  (p, apOld[i], si
16fc7 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a  zeof(MemPage));.
16fc8 20 20 20 20 70 2d 3e 61 44 61 74 61 20 3d 20 28      p->aData = (
16fc9 76 6f 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  void*)&p[1];.   
16fca 20 6d 65 6d 63 70 79 28 70 2d 3e 61 44 61 74 61   memcpy(p->aData
16fcb 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74  , apOld[i]->aDat
16fcc 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  a, pBt->pageSize
16fcd 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  );.  }..  /*.  *
16fce 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20  * Load pointers 
16fcf 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20  to all cells on 
16fd0 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e  sibling pages an
16fd1 64 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65  d the divider ce
16fd2 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  lls.  ** into th
16fd3 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d  e local apCell[]
16fd4 20 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f   array.  Make co
16fd5 70 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69  pies of the divi
16fd6 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69  der cells.  ** i
16fd7 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e  nto space obtain
16fd8 65 64 20 66 6f 72 6d 20 61 53 70 61 63 65 31 5b  ed form aSpace1[
16fd9 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65  ] and remove the
16fda 20 74 68 65 20 64 69 76 69 64 65 72 20 43 65 6c   the divider Cel
16fdb 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61  ls.  ** from pPa
16fdc 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rent..  **.  ** 
16fdd 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20  If the siblings 
16fde 61 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65  are on leaf page
16fdf 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c  s, then the chil
16fe0 64 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68  d pointers of th
16fe1 65 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63  e.  ** divider c
16fe2 65 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65  ells are strippe
16fe3 64 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73  d from the cells
16fe4 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65   before they are
16fe5 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74   copied.  ** int
16fe6 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e  o aSpace1[].  In
16fe7 20 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63   this way, all c
16fe8 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d  ells in apCell[]
16fe9 20 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a   are without.  *
16fea 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73  * child pointers
16feb 2e 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61  .  If siblings a
16fec 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74  re not leaves, t
16fed 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a  hen all cell in.
16fee 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e    ** apCell[] in
16fef 63 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e  clude child poin
16ff0 74 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61  ters.  Either wa
16ff1 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20  y, all cells in 
16ff2 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72  apCell[].  ** ar
16ff3 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20  e alike..  **.  
16ff4 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  ** leafCorrectio
16ff5 6e 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69  n:  4 if pPage i
16ff6 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20  s a leaf.  0 if 
16ff7 70 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c  pPage is not a l
16ff8 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20  eaf..  **       
16ff9 6c 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20  leafData:  1 if 
16ffa 70 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b  pPage holds key+
16ffb 64 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74  data and pParent
16ffc 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73   holds only keys
16ffd 2e 0a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d  ..  */.  nCell =
16ffe 20 30 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63   0;.  leafCorrec
16fff 74 69 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65  tion = pPage->le
17000 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61  af*4;.  leafData
17001 20 3d 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74   = pPage->hasDat
17002 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  a;.  for(i=0; i<
17003 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nOld; i++){.    
17004 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20  MemPage *pOld = 
17005 61 70 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69  apCopy[i];.    i
17006 6e 74 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d  nt limit = pOld-
17007 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76  >nCell+pOld->nOv
17008 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28  erflow;.    for(
17009 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b  j=0; j<limit; j+
1700a 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  +){.      assert
1700b 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c  ( nCell<nMaxCell
1700c 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c  s );.      apCel
1700d 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f  l[nCell] = findO
1700e 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64  verflowCell(pOld
1700f 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65  , j);.      szCe
17010 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c  ll[nCell] = cell
17011 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70  SizePtr(pOld, ap
17012 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 20  Cell[nCell]);.  
17013 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
17014 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 20  CUUM ){.        
17015 69 6e 74 20 61 3b 0a 20 20 20 20 20 20 20 20 61  int a;.        a
17016 46 72 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 28 75  From[nCell] = (u
17017 38 29 69 3b 20 20 20 61 73 73 65 72 74 28 20 69  8)i;   assert( i
17018 3e 3d 30 20 26 26 20 69 3c 36 20 29 3b 0a 20 20  >=0 && i<6 );.  
17019 20 20 20 20 20 20 66 6f 72 28 61 3d 30 3b 20 61        for(a=0; a
1701a 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77  <pOld->nOverflow
1701b 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; a++){.        
1701c 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f 76 66    if( pOld->aOvf
1701d 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 43 65  l[a].pCell==apCe
1701e 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 20 20  ll[nCell] ){.   
1701f 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e           aFrom[n
17020 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20  Cell] = 0xFF;.  
17021 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
17022 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
17023 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
17024 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20       nCell++;.  
17025 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f    }.    if( i<nO
17026 6c 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 75 31  ld-1 ){.      u1
17027 36 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50  6 sz = cellSizeP
17028 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69  tr(pParent, apDi
17029 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66 28  v[i]);.      if(
1702a 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20   leafData ){.   
1702b 20 20 20 20 20 2f 2a 20 57 69 74 68 20 74 68 65       /* With the
1702c 20 4c 45 41 46 44 41 54 41 20 66 6c 61 67 2c 20   LEAFDATA flag, 
1702d 70 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 68 6f  pParent cells ho
1702e 6c 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 73 20  ld only INTKEYs 
1702f 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  that.        ** 
17030 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 20 6f  are duplicates o
17031 66 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 63 68  f keys on the ch
17032 69 6c 64 20 70 61 67 65 73 2e 20 20 57 65 20 6e  ild pages.  We n
17033 65 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a 20 20  eed to remove.  
17034 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 69 76        ** the div
17035 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d 20  ider cells from 
17036 70 50 61 72 65 6e 74 2c 20 62 75 74 20 74 68 65  pParent, but the
17037 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20   dividers cells 
17038 61 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 20 20  are not.        
17039 2a 2a 20 61 64 64 65 64 20 74 6f 20 61 70 43 65  ** added to apCe
1703a 6c 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 68 65  ll[] because the
1703b 79 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73  y are duplicates
1703c 20 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c 73 2e   of child cells.
1703d 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1703e 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61      dropCell(pPa
1703f 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a 29  rent, nxDiv, sz)
17040 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
17041 20 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d 70         u8 *pTemp
17042 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
17043 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c  ( nCell<nMaxCell
17044 73 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43  s );.        szC
17045 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 3b  ell[nCell] = sz;
17046 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 20 3d  .        pTemp =
17047 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 65   &aSpace1[iSpace
17048 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 70 61  1];.        iSpa
17049 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20  ce1 += sz;.     
1704a 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70     assert( sz<=p
1704b 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29  Bt->pageSize/4 )
1704c 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
1704d 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e  ( iSpace1<=pBt->
1704e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  pageSize );.    
1704f 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70      memcpy(pTemp
17050 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b  , apDiv[i], sz);
17051 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b  .        apCell[
17052 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b 6c  nCell] = pTemp+l
17053 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20  eafCorrection;. 
17054 20 20 20 20 20 20 20 69 66 28 20 49 53 41 55 54         if( ISAUT
17055 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20  OVACUUM ){.     
17056 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c 6c       aFrom[nCell
17057 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 20 20  ] = 0xFF;.      
17058 20 20 7d 0a 20 20 20 20 20 20 20 20 64 72 6f 70    }.        drop
17059 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78  Cell(pParent, nx
1705a 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20  Div, sz);.      
1705b 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f    assert( leafCo
1705c 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c  rrection==0 || l
1705d 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34  eafCorrection==4
1705e 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a 43 65   );.        szCe
1705f 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 3d 20 28 75 31  ll[nCell] -= (u1
17060 36 29 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  6)leafCorrection
17061 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
17062 28 20 67 65 74 34 62 79 74 65 28 70 54 65 6d 70  ( get4byte(pTemp
17063 29 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20 29 3b  )==pgnoOld[i] );
17064 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 4f  .        if( !pO
17065 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ld->leaf ){.    
17066 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
17067 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20  afCorrection==0 
17068 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  );.          /* 
17069 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e 74 65  The right pointe
1706a 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70  r of the child p
1706b 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65 73  age pOld becomes
1706c 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20 20 20   the left.      
1706d 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 6f      ** pointer o
1706e 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65  f the divider ce
1706f 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ll */.          
17070 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43  memcpy(apCell[nC
17071 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61  ell], &pOld->aDa
17072 74 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73  ta[pOld->hdrOffs
17073 65 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20  et+8], 4);.     
17074 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17075 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66      assert( leaf
17076 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b  Correction==4 );
17077 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73  .          if( s
17078 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29  zCell[nCell]<4 )
17079 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {.            /*
1707a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e   Do not allow an
1707b 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20  y cells smaller 
1707c 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f  than 4 bytes. */
1707d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 7a 43  .            szC
1707e 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a  ell[nCell] = 4;.
1707f 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
17080 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 43      }.        nC
17081 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  ell++;.      }. 
17082 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20     }.  }..  /*. 
17083 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   ** Figure out t
17084 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
17085 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c  es needed to hol
17086 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c  d all nCell cell
17087 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20 74 68  s..  ** Store th
17088 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22  is number in "k"
17089 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20  .  Also compute 
1708a 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73  szNew[] which is
1708b 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20   the total.  ** 
1708c 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c  size of all cell
1708d 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 70 61  s on the i-th pa
1708e 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20  ge and cntNew[] 
1708f 77 68 69 63 68 20 69 73 20 74 68 65 20 69 6e 64  which is the ind
17090 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c  ex.  ** in apCel
17091 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  l[] of the cell 
17092 74 68 61 74 20 64 69 76 69 64 65 73 20 70 61 67  that divides pag
17093 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20 69 2b  e i from page i+
17094 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77  1.  .  ** cntNew
17095 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75 61 6c  [k] should equal
17096 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a   nCell..  **.  *
17097 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75 74 65  * Values compute
17098 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a  d by this block:
17099 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
1709a 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f 74 61       k: The tota
1709b 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c  l number of sibl
1709c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a 20 20  ing pages.  **  
1709d 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63    szNew[i]: Spac
1709e 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65 20 69  ed used on the i
1709f 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65  -th sibling page
170a0 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b  ..  **   cntNew[
170a1 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61 70 43  i]: Index in apC
170a2 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c  ell[] and szCell
170a3 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  [] for the first
170a4 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20   cell to.  **   
170a5 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72             the r
170a6 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d 74 68  ight of the i-th
170a7 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20   sibling page.. 
170a8 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63 65 3a   ** usableSpace:
170a9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
170aa 20 6f 66 20 73 70 61 63 65 20 61 76 61 69 6c 61   of space availa
170ab 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69 62 6c  ble on each sibl
170ac 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a  ing..  ** .  */.
170ad 20 20 75 73 61 62 6c 65 53 70 61 63 65 20 3d 20    usableSpace = 
170ae 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
170af 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72 72 65  - 12 + leafCorre
170b0 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62  ction;.  for(sub
170b1 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e  total=k=i=0; i<n
170b2 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Cell; i++){.    
170b3 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78 43 65  assert( i<nMaxCe
170b4 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62 74 6f  lls );.    subto
170b5 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d  tal += szCell[i]
170b6 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20 73 75   + 2;.    if( su
170b7 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65 53  btotal > usableS
170b8 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20 73 7a  pace ){.      sz
170b9 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61  New[k] = subtota
170ba 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20  l - szCell[i];. 
170bb 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d       cntNew[k] =
170bc 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65   i;.      if( le
170bd 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d  afData ){ i--; }
170be 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61 6c 20  .      subtotal 
170bf 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a  = 0;.      k++;.
170c0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65      }.  }.  szNe
170c1 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b  w[k] = subtotal;
170c2 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e  .  cntNew[k] = n
170c3 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20  Cell;.  k++;..  
170c4 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63 6b  /*.  ** The pack
170c5 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79 20  ing computed by 
170c6 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c 6f  the previous blo
170c7 63 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f 77  ck is biased tow
170c8 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73  ard the siblings
170c9 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 66  .  ** on the lef
170ca 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65 66  t side.  The lef
170cb 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 61  t siblings are a
170cc 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75 6c  lways nearly ful
170cd 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20 2a  l, while the.  *
170ce 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62  * right-most sib
170cf 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e 65  ling might be ne
170d0 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69  arly empty.  Thi
170d1 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20  s block of code 
170d2 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74 6f  attempts.  ** to
170d3 20 61 64 6a 75 73 74 20 74 68 65 20 70 61 63 6b   adjust the pack
170d4 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73 20  ing of siblings 
170d5 74 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 20  to get a better 
170d6 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20  balance..  **.  
170d7 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65  ** This adjustme
170d8 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20  nt is more than 
170d9 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  an optimization.
170da 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61 62    The packing ab
170db 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62  ove might.  ** b
170dc 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c 61  e so out of bala
170dd 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c 6c  nce as to be ill
170de 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d 70  egal.  For examp
170df 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d 6f  le, the right-mo
170e0 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20  st.  ** sibling 
170e1 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65 74  might be complet
170e2 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73  ely empty.  This
170e3 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6e   adjustment is n
170e4 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a  ot optional..  *
170e5 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 69  /.  for(i=k-1; i
170e6 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e  >0; i--){.    in
170e7 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e 65  t szRight = szNe
170e8 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 6f  w[i];  /* Size o
170e9 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65  f sibling on the
170ea 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69 6e   right */.    in
170eb 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77  t szLeft = szNew
170ec 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f  [i-1]; /* Size o
170ed 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65  f sibling on the
170ee 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e 74   left */.    int
170ef 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   r;             
170f0 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 67   /* Index of rig
170f1 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20  ht-most cell in 
170f2 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a  left sibling */.
170f3 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 20      int d;      
170f4 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
170f5 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 74   of first cell t
170f6 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 69  o the left of ri
170f7 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a  ght sibling */..
170f8 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69      r = cntNew[i
170f9 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20 3d  -1] - 1;.    d =
170fa 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74   r + 1 - leafDat
170fb 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  a;.    assert( d
170fc 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20  <nMaxCells );.  
170fd 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61 78    assert( r<nMax
170fe 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68 69  Cells );.    whi
170ff 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20 7c  le( szRight==0 |
17100 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 6c 6c  | szRight+szCell
17101 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28 73  [d]+2<=szLeft-(s
17102 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20  zCell[r]+2) ){. 
17103 20 20 20 20 20 73 7a 52 69 67 68 74 20 2b 3d 20       szRight += 
17104 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20  szCell[d] + 2;. 
17105 20 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d 20 73       szLeft -= s
17106 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20  zCell[r] + 2;.  
17107 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d      cntNew[i-1]-
17108 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 6e 74  -;.      r = cnt
17109 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20  New[i-1] - 1;.  
1710a 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d 20      d = r + 1 - 
1710b 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 7d 0a  leafData;.    }.
1710c 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 73      szNew[i] = s
1710d 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 7a 4e 65  zRight;.    szNe
1710e 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 3b  w[i-1] = szLeft;
1710f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 68 65  .  }..  /* Eithe
17110 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20 6f  r we found one o
17111 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63 6e  r more cells (cn
17112 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20 77  tnew[0])>0) or w
17113 65 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20 61  e are the.  ** a
17114 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61   virtual root pa
17115 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20 72  ge.  A virtual r
17116 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68 65 6e  oot page is when
17117 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0a 20   the real root. 
17118 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67 65   ** page is page
17119 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 74 68   1 and we are th
1711a 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66 20  e only child of 
1711b 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a  that page..  */.
1711c 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e 65 77    assert( cntNew
1711d 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 65 6e  [0]>0 || (pParen
1711e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 70 50  t->pgno==1 && pP
1711f 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 29  arent->nCell==0)
17120 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41   );..  /*.  ** A
17121 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 61  llocate k new pa
17122 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 20  ges.  Reuse old 
17123 70 61 67 65 73 20 77 68 65 72 65 20 70 6f 73 73  pages where poss
17124 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ible..  */.  ass
17125 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ert( pPage->pgno
17126 3e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c 61 67  >1 );.  pageFlag
17127 73 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  s = pPage->aData
17128 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  [0];.  for(i=0; 
17129 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d  i<k; i++){.    M
1712a 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20  emPage *pNew;.  
1712b 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a    if( i<nOld ){.
1712c 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e        pNew = apN
1712d 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 5d  ew[i] = apOld[i]
1712e 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b  ;.      pgnoNew[
1712f 69 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69 5d 3b  i] = pgnoOld[i];
17130 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20  .      apOld[i] 
17131 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
17132 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
17133 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29  e(pNew->pDbPage)
17134 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a  ;.      nNew++;.
17135 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67        if( rc ) g
17136 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
17137 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  nup;.    }else{.
17138 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e        assert( i>
17139 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 );.      rc = 
1713a 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
1713b 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26 70  e(pBt, &pNew, &p
1713c 67 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e 6f 4e  gnoNew[i], pgnoN
1713d 65 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20 20 20  ew[i-1], 0);.   
1713e 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
1713f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
17140 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d  ;.      apNew[i]
17141 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 6e   = pNew;.      n
17142 4e 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  New++;.    }.  }
17143 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20  ..  /* Free any 
17144 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74 20 77  old pages that w
17145 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64 20 61  ere not reused a
17146 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a  s new pages..  *
17147 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f 6c  /.  while( i<nOl
17148 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 72  d ){.    rc = fr
17149 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29  eePage(apOld[i])
1714a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
1714b 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
1714c 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61 73 65  nup;.    release
1714d 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a  Page(apOld[i]);.
1714e 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30      apOld[i] = 0
1714f 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a  ;.    i++;.  }..
17150 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74 68    /*.  ** Put th
17151 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e 20 61  e new pages in a
17152 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20  ccending order. 
17153 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f 0a 20   This helps to. 
17154 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69 65 73   ** keep entries
17155 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c   in the disk fil
17156 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20 74 68  e in order so th
17157 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20 6f  at a scan.  ** o
17158 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61  f the table is a
17159 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72   linear scan thr
1715a 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e 20 20  ough the file.  
1715b 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75 72  That.  ** in tur
1715c 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70 65 72  n helps the oper
1715d 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74 6f 20  ating system to 
1715e 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a 20 20  deliver pages.  
1715f 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b  ** from the disk
17160 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a 20   more rapidly.. 
17161 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e   **.  ** An O(n^
17162 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72  2) insertion sor
17163 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 75  t algorithm is u
17164 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65 0a 20  sed, but since. 
17165 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72 20 6d   ** n is never m
17166 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61 20 73  ore than NB (a s
17167 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c 20  mall constant), 
17168 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a 2a  that should.  **
17169 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65   not be a proble
1716a 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65  m..  **.  ** Whe
1716b 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f 6e  n NB==3, this on
1716c 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d  e optimization m
1716d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62 61 73  akes the databas
1716e 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32 35 25  e.  ** about 25%
1716f 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61 72 67   faster for larg
17170 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e 64  e insertions and
17171 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a 2f   deletions..  */
17172 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 2d  .  for(i=0; i<k-
17173 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  1; i++){.    int
17174 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 5b   minV = pgnoNew[
17175 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49  i];.    int minI
17176 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d   = i;.    for(j=
17177 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a  i+1; j<k; j++){.
17178 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 4e 65        if( pgnoNe
17179 77 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64 29 6d  w[j]<(unsigned)m
1717a 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  inV ){.        m
1717b 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20  inI = j;.       
1717c 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65 77 5b   minV = pgnoNew[
1717d 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  j];.      }.    
1717e 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 3e 69  }.    if( minI>i
1717f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 3b   ){.      int t;
17180 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  .      MemPage *
17181 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 70 67  pT;.      t = pg
17182 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20  noNew[i];.      
17183 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20  pT = apNew[i];. 
17184 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69 5d 20       pgnoNew[i] 
17185 3d 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49 5d 3b  = pgnoNew[minI];
17186 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20  .      apNew[i] 
17187 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20  = apNew[minI];. 
17188 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e       pgnoNew[min
17189 49 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20 61 70  I] = t;.      ap
1718a 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a  New[minI] = pT;.
1718b 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43      }.  }.  TRAC
1718c 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c 64  E(("BALANCE: old
1718d 3a 20 25 64 20 25 64 20 25 64 20 20 6e 65 77 3a  : %d %d %d  new:
1718e 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25   %d(%d) %d(%d) %
1718f 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28  d(%d) %d(%d) %d(
17190 25 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67 6e 6f  %d)\n",.    pgno
17191 4f 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e 4f 6c  Old[0], .    nOl
17192 64 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64 5b 31  d>=2 ? pgnoOld[1
17193 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e  ] : 0,.    nOld>
17194 3d 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32 5d 20  =3 ? pgnoOld[2] 
17195 3a 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e 65 77  : 0,.    pgnoNew
17196 5b 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20  [0], szNew[0],. 
17197 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70 67 6e     nNew>=2 ? pgn
17198 6f 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e 4e 65  oNew[1] : 0, nNe
17199 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20  w>=2 ? szNew[1] 
1719a 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33  : 0,.    nNew>=3
1719b 20 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20 3a 20   ? pgnoNew[2] : 
1719c 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a 4e  0, nNew>=3 ? szN
1719d 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20 6e  ew[2] : 0,.    n
1719e 4e 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e 65 77  New>=4 ? pgnoNew
1719f 5b 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 34  [3] : 0, nNew>=4
171a0 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30 2c   ? szNew[3] : 0,
171a1 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20 70  .    nNew>=5 ? p
171a2 67 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c 20 6e  gnoNew[4] : 0, n
171a3 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34  New>=5 ? szNew[4
171a4 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a 0a 20  ] : 0));..  /*. 
171a5 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72   ** Evenly distr
171a6 69 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69  ibute the data i
171a7 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73  n apCell[] acros
171a8 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e  s the new pages.
171a9 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76  .  ** Insert div
171aa 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20  ider cells into 
171ab 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73  pParent as neces
171ac 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d  sary..  */.  j =
171ad 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
171ae 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nNew; i++){.   
171af 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65   /* Assemble the
171b0 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67   new sibling pag
171b1 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67  e. */.    MemPag
171b2 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b  e *pNew = apNew[
171b3 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  i];.    assert( 
171b4 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  j<nMaxCells );. 
171b5 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d     assert( pNew-
171b6 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77 5b 69  >pgno==pgnoNew[i
171b7 5d 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67  ] );.    zeroPag
171b8 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c 61 67  e(pNew, pageFlag
171b9 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65  s);.    assemble
171ba 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e 65  Page(pNew, cntNe
171bb 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c 5b  w[i]-j, &apCell[
171bc 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 3b  j], &szCell[j]);
171bd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65  .    assert( pNe
171be 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 6e  w->nCell>0 || (n
171bf 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65 77  New==1 && cntNew
171c0 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 20 61  [0]==0) );.    a
171c1 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f 76  ssert( pNew->nOv
171c2 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20 20  erflow==0 );..  
171c3 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
171c4 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  an auto-vacuum d
171c5 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65 20  atabase, update 
171c6 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
171c7 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 74  entries.    ** t
171c8 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  hat point to the
171c9 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20 77   siblings that w
171ca 65 72 65 20 72 65 61 72 72 61 6e 67 65 64 2e 20  ere rearranged. 
171cb 54 68 65 73 65 20 63 61 6e 20 62 65 3a 20 6c 65  These can be: le
171cc 66 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 72  ft.    ** childr
171cd 65 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74 68 65  en of cells, the
171ce 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20   right-child of 
171cf 74 68 65 20 70 61 67 65 2c 20 6f 72 20 6f 76 65  the page, or ove
171d0 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 20  rflow pages.    
171d1 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ** pointed to by
171d2 20 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f 0a 20   cells..    */. 
171d3 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
171d4 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 66 6f 72  UUM ){.      for
171d5 28 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77 5b 69  (k=j; k<cntNew[i
171d6 5d 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ]; k++){.       
171d7 20 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61 78 43   assert( k<nMaxC
171d8 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20  ells );.        
171d9 69 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d 30 78  if( aFrom[k]==0x
171da 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61 46 72  FF || apCopy[aFr
171db 6f 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d 70 4e  om[k]]->pgno!=pN
171dc 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20  ew->pgno ){.    
171dd 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
171de 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c 20 6b  pPutOvfl(pNew, k
171df 2d 6a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  -j);.          i
171e0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
171e1 20 26 26 20 6c 65 61 66 43 6f 72 72 65 63 74 69   && leafCorrecti
171e2 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  on==0 ){.       
171e3 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
171e4 50 75 74 28 70 42 74 2c 20 67 65 74 34 62 79 74  Put(pBt, get4byt
171e5 65 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20 50 54  e(apCell[k]), PT
171e6 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77  RMAP_BTREE, pNew
171e7 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
171e8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
171e9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
171ea 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
171eb 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
171ec 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 20 20  anup;.          
171ed 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
171ee 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6a    }.    }..    j
171ef 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20   = cntNew[i];.. 
171f0 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 69 62     /* If the sib
171f1 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65 6d 62  ling page assemb
171f2 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20 6e 6f  led above was no
171f3 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  t the right-most
171f4 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a   sibling,.    **
171f5 20 69 6e 73 65 72 74 20 61 20 64 69 76 69 64 65   insert a divide
171f6 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20  r cell into the 
171f7 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20  parent page..   
171f8 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c 6e 4e   */.    if( i<nN
171f9 65 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c 6c 20  ew-1 && j<nCell 
171fa 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65  ){.      u8 *pCe
171fb 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54  ll;.      u8 *pT
171fc 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 73  emp;.      int s
171fd 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  z;..      assert
171fe 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( j<nMaxCells );
171ff 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 61  .      pCell = a
17200 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20  pCell[j];.      
17201 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20 2b  sz = szCell[j] +
17202 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b   leafCorrection;
17203 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26  .      pTemp = &
17204 61 53 70 61 63 65 32 5b 69 53 70 61 63 65 32 5d  aSpace2[iSpace2]
17205 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65  ;.      if( !pNe
17206 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  w->leaf ){.     
17207 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 2d     memcpy(&pNew-
17208 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65 6c 6c  >aData[8], pCell
17209 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 4);.        if
1720a 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 0a  ( ISAUTOVACUUM .
1720b 20 20 20 20 20 20 20 20 20 26 26 20 28 61 46 72           && (aFr
1720c 6f 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c 20 61  om[j]==0xFF || a
1720d 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d 5d 2d  pCopy[aFrom[j]]-
1720e 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e  >pgno!=pNew->pgn
1720f 6f 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20  o).        ){.  
17210 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72          rc = ptr
17211 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34  mapPut(pBt, get4
17212 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 50 54 52  byte(pCell), PTR
17213 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d  MAP_BTREE, pNew-
17214 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  >pgno);.        
17215 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
17216 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
17217 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f     goto balance_
17218 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20  cleanup;.       
17219 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1721a 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c       }else if( l
1721b 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 20  eafData ){.     
1721c 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 65     /* If the tre
1721d 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 61  e is a leaf-data
1721e 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 73   tree, and the s
1721f 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 76  iblings are leav
17220 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  es, .        ** 
17221 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f  then there is no
17222 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e   divider cell in
17223 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65   apCell[]. Inste
17224 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 20  ad, the divider 
17225 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c  .        ** cell
17226 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65   consists of the
17227 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72   integer key for
17228 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
17229 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 20  cell of .       
1722a 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 2d   ** the sibling-
1722b 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 61  page assembled a
1722c 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 20  bove only..     
1722d 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 65     */.        Ce
1722e 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20  llInfo info;.   
1722f 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 20       j--;.      
17230 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61    sqlite3BtreePa
17231 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c  rseCellPtr(pNew,
17232 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66   apCell[j], &inf
17233 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c  o);.        pCel
17234 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 20  l = pTemp;.     
17235 20 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65     rc = fillInCe
17236 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c  ll(pParent, pCel
17237 6c 2c 20 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79 2c  l, 0, info.nKey,
17238 20 30 2c 20 30 2c 20 30 2c 20 26 73 7a 29 3b 0a   0, 0, 0, &sz);.
17239 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1723a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1723b 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61         goto bala
1723c 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
1723d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
1723e 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  Temp = 0;.      
1723f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
17240 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20  Cell -= 4;.     
17241 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61     /* Obscure ca
17242 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d  se for non-leaf-
17243 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 74  data trees: If t
17244 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c  he cell at pCell
17245 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20   was.        ** 
17246 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65  previously store
17247 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65  d on a leaf node
17248 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74  , and its report
17249 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20  ed size was 4.  
1724a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20        ** bytes, 
1724b 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75  then it may actu
1724c 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20  ally be smaller 
1724d 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20  than this .     
1724e 20 20 20 2a 2a 20 28 73 65 65 20 73 71 6c 69 74     ** (see sqlit
1724f 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
17250 50 74 72 28 29 2c 20 34 20 62 79 74 65 73 20 69  Ptr(), 4 bytes i
17251 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69  s the minimum si
17252 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a  ze of.        **
17253 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75 74 20   any cell). But 
17254 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  it is important 
17255 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f 72 72  to pass the corr
17256 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20 20 20  ect size to .   
17257 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 43 65       ** insertCe
17258 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72 73 65  ll(), so reparse
17259 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a 20   the cell now.. 
1725a 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
1725b 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74    ** Note that t
1725c 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20 68 61  his can never ha
1725d 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c 69 74  ppen in an SQLit
1725e 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61 73 20  e data file, as 
1725f 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  all.        ** c
17260 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65 61 73  ells are at leas
17261 74 20 34 20 62 79 74 65 73 2e 20 49 74 20 6f 6e  t 4 bytes. It on
17262 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20 62 2d  ly happens in b-
17263 74 72 65 65 73 20 75 73 65 64 0a 20 20 20 20 20  trees used.     
17264 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75 61 74     ** to evaluat
17265 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e  e "IN (SELECT ..
17266 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61 72 20  .)" and similar 
17267 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20 20 20  clauses..       
17268 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
17269 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0a  szCell[j]==4 ){.
1726a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1726b 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d  (leafCorrection=
1726c 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  =4);.          s
1726d 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  z = cellSizePtr(
1726e 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b  pParent, pCell);
1726f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
17270 20 7d 0a 20 20 20 20 20 20 69 53 70 61 63 65 32   }.      iSpace2
17271 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 73   += sz;.      as
17272 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70  sert( sz<=pBt->p
17273 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20  ageSize/4 );.   
17274 20 20 20 61 73 73 65 72 74 28 20 69 53 70 61 63     assert( iSpac
17275 65 32 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a  e2<=pBt->pageSiz
17276 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  e );.      rc = 
17277 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65  insertCell(pPare
17278 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c  nt, nxDiv, pCell
17279 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 34 29 3b  , sz, pTemp, 4);
1727a 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1727b 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
1727c 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
1727d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
1727e 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1727f 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e  teable(pParent->
17280 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20  pDbPage) );.    
17281 20 20 70 75 74 34 62 79 74 65 28 66 69 6e 64 4f    put4byte(findO
17282 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61 72  verflowCell(pPar
17283 65 6e 74 2c 6e 78 44 69 76 29 2c 20 70 4e 65 77  ent,nxDiv), pNew
17284 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 20 20  ->pgno);..      
17285 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
17286 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74   auto-vacuum dat
17287 61 62 61 73 65 2c 20 61 6e 64 20 6e 6f 74 20 61  abase, and not a
17288 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 2c   leaf-data tree,
17289 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 75  .      ** then u
1728a 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65  pdate the pointe
1728b 72 20 6d 61 70 20 77 69 74 68 20 61 6e 20 65 6e  r map with an en
1728c 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65 72  try for the over
1728d 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20  flow page.      
1728e 2a 2a 20 74 68 61 74 20 74 68 65 20 63 65 6c 6c  ** that the cell
1728f 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20 70   just inserted p
17290 6f 69 6e 74 73 20 74 6f 20 28 69 66 20 61 6e 79  oints to (if any
17291 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
17292 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
17293 55 4d 20 26 26 20 21 6c 65 61 66 44 61 74 61 20  UM && !leafData 
17294 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
17295 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 50  ptrmapPutOvfl(pP
17296 61 72 65 6e 74 2c 20 6e 78 44 69 76 29 3b 0a 20  arent, nxDiv);. 
17297 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
17298 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17299 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e        goto balan
1729a 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  ce_cleanup;.    
1729b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1729c 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 6e      j++;.      n
1729d 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  xDiv++;.    }.. 
1729e 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 70 6f     /* Set the po
1729f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20  inter-map entry 
172a0 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 62 6c  for the new sibl
172a1 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20  ing page. */.   
172a2 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
172a3 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  M ){.      rc = 
172a4 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70  ptrmapPut(pBt, p
172a5 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41  New->pgno, PTRMA
172a6 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74  P_BTREE, pParent
172a7 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69  ->pgno);.      i
172a8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
172a9 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
172aa 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70   balance_cleanup
172ab 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
172ac 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 3d    }.  assert( j=
172ad 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65  =nCell );.  asse
172ae 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20  rt( nOld>0 );.  
172af 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 20 29  assert( nNew>0 )
172b0 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 6c 61  ;.  if( (pageFla
172b1 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d  gs & PTF_LEAF)==
172b2 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 68  0 ){.    u8 *zCh
172b3 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b 6e 4f  ild = &apCopy[nO
172b4 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 3b  ld-1]->aData[8];
172b5 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 70 4e  .    memcpy(&apN
172b6 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74  ew[nNew-1]->aDat
172b7 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 29  a[8], zChild, 4)
172b8 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f  ;.    if( ISAUTO
172b9 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20  VACUUM ){.      
172ba 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74 28 70  rc = ptrmapPut(p
172bb 42 74 2c 20 67 65 74 34 62 79 74 65 28 7a 43 68  Bt, get4byte(zCh
172bc 69 6c 64 29 2c 20 50 54 52 4d 41 50 5f 42 54 52  ild), PTRMAP_BTR
172bd 45 45 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31  EE, apNew[nNew-1
172be 5d 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  ]->pgno);.      
172bf 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
172c0 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
172c1 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
172c2 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  p;.      }.    }
172c3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73  .  }.  assert( s
172c4 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
172c5 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e  teable(pParent->
172c6 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66  pDbPage) );.  if
172c7 28 20 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74  ( nxDiv==pParent
172c8 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72 65 6e 74 2d  ->nCell+pParent-
172c9 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  >nOverflow ){.  
172ca 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20    /* Right-most 
172cb 73 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 72  sibling is the r
172cc 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20  ight-most child 
172cd 6f 66 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20  of pParent */.  
172ce 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 72    put4byte(&pPar
172cf 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
172d0 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
172d1 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d 31  , pgnoNew[nNew-1
172d2 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ]);.  }else{.   
172d3 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73   /* Right-most s
172d4 69 62 6c 69 6e 67 20 69 73 20 74 68 65 20 6c 65  ibling is the le
172d5 66 74 20 63 68 69 6c 64 20 6f 66 20 74 68 65 20  ft child of the 
172d6 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 70  first entry in p
172d7 50 61 72 65 6e 74 0a 20 20 20 20 2a 2a 20 70 61  Parent.    ** pa
172d8 73 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  st the right-mos
172d9 74 20 64 69 76 69 64 65 72 20 65 6e 74 72 79 20  t divider entry 
172da 2a 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65 28  */.    put4byte(
172db 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c  findOverflowCell
172dc 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 29  (pParent, nxDiv)
172dd 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e 65 77 2d 31  , pgnoNew[nNew-1
172de 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20  ]);.  }..  /*.  
172df 2a 2a 20 42 61 6c 61 6e 63 65 20 74 68 65 20 70  ** Balance the p
172e0 61 72 65 6e 74 20 70 61 67 65 2e 20 20 4e 6f 74  arent page.  Not
172e1 65 20 74 68 61 74 20 74 68 65 20 63 75 72 72 65  e that the curre
172e2 6e 74 20 70 61 67 65 20 28 70 50 61 67 65 29 20  nt page (pPage) 
172e3 6d 69 67 68 74 0a 20 20 2a 2a 20 68 61 76 65 20  might.  ** have 
172e4 62 65 65 6e 20 61 64 64 65 64 20 74 6f 20 74 68  been added to th
172e5 65 20 66 72 65 65 6c 69 73 74 20 73 6f 20 69 74  e freelist so it
172e6 20 6d 69 67 68 74 20 6e 6f 20 6c 6f 6e 67 65 72   might no longer
172e7 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
172e8 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20 70 61  .  ** But the pa
172e9 72 65 6e 74 20 70 61 67 65 20 77 69 6c 6c 20 61  rent page will a
172ea 6c 77 61 79 73 20 62 65 20 69 6e 69 74 69 61 6c  lways be initial
172eb 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ized..  */.  ass
172ec 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 69 73  ert( pParent->is
172ed 49 6e 69 74 20 29 3b 0a 20 20 73 71 6c 69 74 65  Init );.  sqlite
172ee 33 53 63 72 61 74 63 68 46 72 65 65 28 61 70 43  3ScratchFree(apC
172ef 65 6c 6c 29 3b 0a 20 20 61 70 43 65 6c 6c 20 3d  ell);.  apCell =
172f0 20 30 3b 0a 20 20 54 52 41 43 45 28 28 22 42 41   0;.  TRACE(("BA
172f1 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 64 20  LANCE: finished 
172f2 77 69 74 68 20 25 64 3a 20 6f 6c 64 3d 25 64 20  with %d: old=%d 
172f3 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c  new=%d cells=%d\
172f4 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 50  n",.          pP
172f5 61 67 65 2d 3e 70 67 6e 6f 2c 20 6e 4f 6c 64 2c  age->pgno, nOld,
172f6 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b 0a   nNew, nCell));.
172f7 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
172f8 6f 77 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61 73  ow = 0;.  releas
172f9 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
172fa 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20  pCur->iPage--;. 
172fb 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43   rc = balance(pC
172fc 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a 0a  ur, 0);.  .  /*.
172fd 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65 66    ** Cleanup bef
172fe 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 20  ore returning.. 
172ff 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 61   */.balance_clea
17300 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 50 61  nup:.  sqlite3Pa
17301 67 65 46 72 65 65 28 61 53 70 61 63 65 32 29 3b  geFree(aSpace2);
17302 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 74 63  .  sqlite3Scratc
17303 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20  hFree(apCell);. 
17304 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64   for(i=0; i<nOld
17305 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65  ; i++){.    rele
17306 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d  asePage(apOld[i]
17307 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  );.  }.  for(i=0
17308 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a  ; i<nNew; i++){.
17309 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1730a 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 20  apNew[i]);.  }. 
1730b 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1730c 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65  ur->iPage]->nOve
1730d 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 72 65  rflow = 0;..  re
1730e 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1730f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
17310 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 68 65  s called for the
17311 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20   root page of a 
17312 62 74 72 65 65 20 77 68 65 6e 20 74 68 65 20 72  btree when the r
17313 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74  oot.** page cont
17314 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 20  ains no cells.  
17315 54 68 69 73 20 69 73 20 61 6e 20 6f 70 70 6f 72  This is an oppor
17316 74 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 74  tunity to make t
17317 68 65 20 74 72 65 65 0a 2a 2a 20 73 68 61 6c 6c  he tree.** shall
17318 6f 77 65 72 20 62 79 20 6f 6e 65 20 6c 65 76 65  ower by one leve
17319 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
1731a 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f 77   balance_shallow
1731b 65 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  er(BtCursor *pCu
1731c 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  r){.  MemPage *p
1731d 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
1731e 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20     /* Root page 
1731f 6f 66 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 4d  of B-Tree */.  M
17320 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20  emPage *pChild; 
17321 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
17322 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 70 61  he only child pa
17323 67 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 20  ge of pPage */. 
17324 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 3b   Pgno pgnoChild;
17325 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17326 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72   Page number for
17327 20 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69 6e 74   pChild */.  int
17328 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
17329 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
1732a 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75  urn code from su
1732b 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20  bprocedures */. 
1732c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20   BtShared *pBt; 
1732d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1732e 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42 54 72   /* The main BTr
1732f 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  ee structure */.
17330 20 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65 72 50    int mxCellPerP
17331 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  age;           /
17332 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  * Maximum number
17333 20 6f 66 20 63 65 6c 6c 73 20 70 65 72 20 70 61   of cells per pa
17334 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43  ge */.  u8 **apC
17335 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
17336 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c       /* All cell
17337 73 20 66 72 6f 6d 20 70 61 67 65 73 20 62 65 69  s from pages bei
17338 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20  ng balanced */. 
17339 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20   u16 *szCell;   
1733a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1733b 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61   Local size of a
1733c 6c 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20 20 61  ll cells */..  a
1733d 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61  ssert( pCur->iPa
1733e 67 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67 65  ge==0 );.  pPage
1733f 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
17340 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  0];..  assert( p
17341 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  Page->nCell==0 )
17342 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
17343 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
17344 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
17345 29 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61  ) );.  pBt = pPa
17346 67 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 43 65 6c  ge->pBt;.  mxCel
17347 6c 50 65 72 50 61 67 65 20 3d 20 4d 58 5f 43 45  lPerPage = MX_CE
17348 4c 4c 28 70 42 74 29 3b 0a 20 20 61 70 43 65 6c  LL(pBt);.  apCel
17349 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  l = sqlite3Mallo
1734a 63 28 20 6d 78 43 65 6c 6c 50 65 72 50 61 67 65  c( mxCellPerPage
1734b 2a 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b 73 69  *(sizeof(u8*)+si
1734c 7a 65 6f 66 28 75 31 36 29 29 20 29 3b 0a 20 20  zeof(u16)) );.  
1734d 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 20  if( apCell==0 ) 
1734e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1734f 4d 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20 3d 20  MEM;.  szCell = 
17350 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6d 78  (u16*)&apCell[mx
17351 43 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a 20 20  CellPerPage];.  
17352 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  if( pPage->leaf 
17353 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61  ){.    /* The ta
17354 62 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c  ble is completel
17355 79 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 54  y empty */.    T
17356 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20  RACE(("BALANCE: 
17357 65 6d 70 74 79 20 74 61 62 6c 65 20 25 64 5c 6e  empty table %d\n
17358 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29  ", pPage->pgno))
17359 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
1735a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20  * The root page 
1735b 69 73 20 65 6d 70 74 79 20 62 75 74 20 68 61 73  is empty but has
1735c 20 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54 72 61   one child.  Tra
1735d 6e 73 66 65 72 20 74 68 65 0a 20 20 20 20 2a 2a  nsfer the.    **
1735e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
1735f 6d 20 74 68 61 74 20 6f 6e 65 20 63 68 69 6c 64  m that one child
17360 20 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74 20 70   into the root p
17361 61 67 65 20 69 66 20 69 74 20 0a 20 20 20 20 2a  age if it .    *
17362 2a 20 77 69 6c 6c 20 66 69 74 2e 20 20 54 68 69  * will fit.  Thi
17363 73 20 72 65 64 75 63 65 73 20 74 68 65 20 64 65  s reduces the de
17364 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20  pth of the tree 
17365 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20  by one..    **. 
17366 20 20 20 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f     ** If the roo
17367 74 20 70 61 67 65 20 69 73 20 70 61 67 65 20 31  t page is page 1
17368 2c 20 69 74 20 68 61 73 20 6c 65 73 73 20 73 70  , it has less sp
17369 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 74 68  ace available th
1736a 61 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20 63 68  an.    ** its ch
1736b 69 6c 64 20 28 64 75 65 20 74 6f 20 74 68 65 20  ild (due to the 
1736c 31 30 30 20 62 79 74 65 20 68 65 61 64 65 72 20  100 byte header 
1736d 74 68 61 74 20 6f 63 63 75 72 73 20 61 74 20 74  that occurs at t
1736e 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20  he beginning.   
1736f 20 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62   ** of the datab
17370 61 73 65 20 66 6c 65 29 2c 20 73 6f 20 69 74 20  ase fle), so it 
17371 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 62 6c  might not be abl
17372 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6f 66  e to hold all of
17373 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 69 6e 66   the .    ** inf
17374 6f 72 6d 61 74 69 6f 6e 20 63 75 72 72 65 6e 74  ormation current
17375 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ly contained in 
17376 74 68 65 20 63 68 69 6c 64 2e 20 20 49 66 20 74  the child.  If t
17377 68 69 73 20 69 73 20 74 68 65 20 0a 20 20 20 20  his is the .    
17378 2a 2a 20 63 61 73 65 2c 20 74 68 65 6e 20 64 6f  ** case, then do
17379 20 6e 6f 74 20 64 6f 20 74 68 65 20 74 72 61 6e   not do the tran
1737a 73 66 65 72 2e 20 20 4c 65 61 76 65 20 70 61 67  sfer.  Leave pag
1737b 65 20 31 20 65 6d 70 74 79 20 65 78 63 65 70 74  e 1 empty except
1737c 0a 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  .    ** for the 
1737d 72 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 74 6f  right-pointer to
1737e 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 2e   the child page.
1737f 20 20 54 68 65 20 63 68 69 6c 64 20 70 61 67 65    The child page
17380 20 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a 2a 20   becomes.    ** 
17381 74 68 65 20 76 69 72 74 75 61 6c 20 72 6f 6f 74  the virtual root
17382 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 20 20   of the tree..  
17383 20 20 2a 2f 0a 20 20 20 20 56 56 41 5f 4f 4e 4c    */.    VVA_ONL
17384 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68  Y( pCur->pagesSh
17385 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 20 20  uffled = 1 );.  
17386 20 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 67 65    pgnoChild = ge
17387 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
17388 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
17389 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 61  ffset+8]);.    a
1738a 73 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c 64  ssert( pgnoChild
1738b 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
1738c 28 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 70 61 67  ( pgnoChild<=pag
1738d 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
1738e 65 2d 3e 70 42 74 29 20 29 3b 0a 20 20 20 20 72  e->pBt) );.    r
1738f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
17390 47 65 74 50 61 67 65 28 70 50 61 67 65 2d 3e 70  GetPage(pPage->p
17391 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 26  Bt, pgnoChild, &
17392 70 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20 20  pChild, 0);.    
17393 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e  if( rc ) goto en
17394 64 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63  d_shallow_balanc
17395 65 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  e;.    if( pPage
17396 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20  ->pgno==1 ){.   
17397 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
17398 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 43 68  treeInitPage(pCh
17399 69 6c 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ild);.      if( 
1739a 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73 68  rc ) goto end_sh
1739b 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a 20  allow_balance;. 
1739c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 68       assert( pCh
1739d 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  ild->nOverflow==
1739e 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  0 );.      if( p
1739f 43 68 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d 31 30  Child->nFree>=10
173a0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
173a1 54 68 65 20 63 68 69 6c 64 20 69 6e 66 6f 72 6d  The child inform
173a2 61 74 69 6f 6e 20 77 69 6c 6c 20 66 69 74 20 6f  ation will fit o
173a3 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c  n the root page,
173a4 20 73 6f 20 64 6f 20 74 68 65 0a 20 20 20 20 20   so do the.     
173a5 20 20 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a 20 20     ** copy */.  
173a6 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
173a7 20 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50       zeroPage(pP
173a8 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61  age, pChild->aDa
173a9 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20  ta[0]);.        
173aa 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 69 6c  for(i=0; i<pChil
173ab 64 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a  d->nCell; i++){.
173ac 20 20 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c            apCell
173ad 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  [i] = findCell(p
173ae 43 68 69 6c 64 2c 69 29 3b 0a 20 20 20 20 20 20  Child,i);.      
173af 20 20 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 3d 20      szCell[i] = 
173b0 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 43 68 69  cellSizePtr(pChi
173b1 6c 64 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 3b 0a  ld, apCell[i]);.
173b2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
173b3 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70    assemblePage(p
173b4 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 6e 43  Page, pChild->nC
173b5 65 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 7a 43  ell, apCell, szC
173b6 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a  ell);.        /*
173b7 20 43 6f 70 79 20 74 68 65 20 72 69 67 68 74 2d   Copy the right-
173b8 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63  pointer of the c
173b9 68 69 6c 64 20 74 6f 20 74 68 65 20 70 61 72 65  hild to the pare
173ba 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  nt. */.        a
173bb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
173bc 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
173bd 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
173be 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79  ;.        put4by
173bf 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
173c0 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
173c1 74 2b 38 5d 2c 20 0a 20 20 20 20 20 20 20 20 20  t+8], .         
173c2 20 20 20 67 65 74 34 62 79 74 65 28 26 70 43 68     get4byte(&pCh
173c3 69 6c 64 2d 3e 61 44 61 74 61 5b 70 43 68 69 6c  ild->aData[pChil
173c4 64 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  d->hdrOffset+8])
173c5 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
173c6 66 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 29  freePage(pChild)
173c7 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28  ;.        TRACE(
173c8 28 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c 64  ("BALANCE: child
173c9 20 25 64 20 74 72 61 6e 73 66 65 72 20 74 6f 20   %d transfer to 
173ca 70 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 69 6c  page 1\n", pChil
173cb 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20  d->pgno));.     
173cc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
173cd 2f 2a 20 54 68 65 20 63 68 69 6c 64 20 68 61 73  /* The child has
173ce 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f   more informatio
173cf 6e 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20  n that will fit 
173d0 6f 6e 20 74 68 65 20 72 6f 6f 74 2e 0a 20 20 20  on the root..   
173d1 20 20 20 20 20 2a 2a 20 54 68 65 20 74 72 65 65       ** The tree
173d2 20 69 73 20 61 6c 72 65 61 64 79 20 62 61 6c 61   is already bala
173d3 6e 63 65 64 2e 20 20 44 6f 20 6e 6f 74 68 69 6e  nced.  Do nothin
173d4 67 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 54 52  g. */.        TR
173d5 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63  ACE(("BALANCE: c
173d6 68 69 6c 64 20 25 64 20 77 69 6c 6c 20 6e 6f 74  hild %d will not
173d7 20 66 69 74 20 6f 6e 20 70 61 67 65 20 31 5c 6e   fit on page 1\n
173d8 22 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29  ", pChild->pgno)
173d9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
173da 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
173db 70 79 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c  py(pPage->aData,
173dc 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 2c 20   pChild->aData, 
173dd 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
173de 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70  leSize);.      p
173df 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
173e0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
173e1 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67  ite3BtreeInitPag
173e2 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
173e3 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
173e4 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 66  TE_OK );.      f
173e5 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b  reePage(pChild);
173e6 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 42  .      TRACE(("B
173e7 41 4c 41 4e 43 45 3a 20 74 72 61 6e 73 66 65 72  ALANCE: transfer
173e8 20 63 68 69 6c 64 20 25 64 20 69 6e 74 6f 20 72   child %d into r
173e9 6f 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20  oot %d\n",.     
173ea 20 20 20 20 20 20 20 20 20 70 43 68 69 6c 64 2d           pChild-
173eb 3e 70 67 6e 6f 2c 20 70 50 61 67 65 2d 3e 70 67  >pgno, pPage->pg
173ec 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  no));.    }.    
173ed 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
173ee 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 23  Overflow==0 );.#
173ef 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
173f0 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
173f1 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
173f2 55 4d 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  UM && rc==SQLITE
173f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
173f4 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70  = setChildPtrmap
173f5 73 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  s(pPage);.    }.
173f6 23 65 6e 64 69 66 0a 20 20 20 20 72 65 6c 65 61  #endif.    relea
173f7 73 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a  sePage(pChild);.
173f8 20 20 7d 0a 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f    }.end_shallow_
173f9 62 61 6c 61 6e 63 65 3a 0a 20 20 73 71 6c 69 74  balance:.  sqlit
173fa 65 33 5f 66 72 65 65 28 61 70 43 65 6c 6c 29 3b  e3_free(apCell);
173fb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
173fc 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74  ../*.** The root
173fd 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c   page is overful
173fe 6c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  l.**.** When thi
173ff 73 20 68 61 70 70 65 6e 73 2c 20 43 72 65 61 74  s happens, Creat
17400 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61  e a new child pa
17401 67 65 20 61 6e 64 20 63 6f 70 79 20 74 68 65 0a  ge and copy the.
17402 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  ** contents of t
17403 68 65 20 72 6f 6f 74 20 69 6e 74 6f 20 74 68 65  he root into the
17404 20 63 68 69 6c 64 2e 20 20 54 68 65 6e 20 6d 61   child.  Then ma
17405 6b 65 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70  ke the root.** p
17406 61 67 65 20 61 6e 20 65 6d 70 74 79 20 70 61 67  age an empty pag
17407 65 20 77 69 74 68 20 72 69 67 68 74 43 68 69 6c  e with rightChil
17408 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  d pointing to th
17409 65 20 6e 65 77 0a 2a 2a 20 63 68 69 6c 64 2e 20  e new.** child. 
1740a 20 20 46 69 6e 61 6c 6c 79 2c 20 63 61 6c 6c 20    Finally, call 
1740b 62 61 6c 61 6e 63 65 5f 69 6e 74 65 72 6e 61 6c  balance_internal
1740c 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 63 68  () on the new ch
1740d 69 6c 64 0a 2a 2a 20 74 6f 20 63 61 75 73 65 20  ild.** to cause 
1740e 69 74 20 74 6f 20 73 70 6c 69 74 2e 0a 2a 2f 0a  it to split..*/.
1740f 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e  static int balan
17410 63 65 5f 64 65 65 70 65 72 28 42 74 43 75 72 73  ce_deeper(BtCurs
17411 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74  or *pCur){.  int
17412 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
17413 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65   /* Return value
17414 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75   from subprocedu
17415 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  res */.  MemPage
17416 20 2a 70 50 61 67 65 3b 20 20 20 20 20 2f 2a 20   *pPage;     /* 
17417 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  Pointer to the r
17418 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 4d 65  oot page */.  Me
17419 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 20  mPage *pChild;  
1741a 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
1741b 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65  a new child page
1741c 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43   */.  Pgno pgnoC
1741d 68 69 6c 64 3b 20 20 20 20 20 2f 2a 20 50 61 67  hild;     /* Pag
1741e 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
1741f 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a  new child page *
17420 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
17421 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  t;         /* Th
17422 65 20 42 54 72 65 65 20 2a 2f 0a 20 20 69 6e 74  e BTree */.  int
17423 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20   usableSize;    
17424 20 2f 2a 20 54 6f 74 61 6c 20 75 73 61 62 6c 65   /* Total usable
17425 20 73 69 7a 65 20 6f 66 20 61 20 70 61 67 65 20   size of a page 
17426 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20  */.  u8 *data;  
17427 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
17428 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 65 6e  ent of the paren
17429 74 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  t page */.  u8 *
1742a 63 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  cdata;          
1742b 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68  /* Content of th
1742c 65 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a  e child page */.
1742d 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20    int hdr;      
1742e 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1742f 74 6f 20 70 61 67 65 20 68 65 61 64 65 72 20 69  to page header i
17430 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e  n parent */.  in
17431 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20  t cbrk;         
17432 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63    /* Offset to c
17433 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20  ontent of first 
17434 63 65 6c 6c 20 69 6e 20 70 61 72 65 6e 74 20 2a  cell in parent *
17435 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  /..  assert( pCu
17436 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 3b 0a 20  r->iPage==0 );. 
17437 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
17438 70 50 61 67 65 5b 30 5d 2d 3e 6e 4f 76 65 72 66  pPage[0]->nOverf
17439 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 56 56 41 5f  low>0 );..  VVA_
1743a 4f 4e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65  ONLY( pCur->page
1743b 73 53 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b  sShuffled = 1 );
1743c 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
1743d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20 70 42  >apPage[0];.  pB
1743e 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
1743f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17440 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
17441 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
17442 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
17443 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
17444 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
17445 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65  .  rc = allocate
17446 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26  BtreePage(pBt, &
17447 70 43 68 69 6c 64 2c 20 26 70 67 6e 6f 43 68 69  pChild, &pgnoChi
17448 6c 64 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  ld, pPage->pgno,
17449 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20   0);.  if( rc ) 
1744a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73  return rc;.  ass
1744b 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
1744c 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 68  rIswriteable(pCh
1744d 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  ild->pDbPage) );
1744e 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20  .  usableSize = 
1744f 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b  pBt->usableSize;
17450 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  .  data = pPage-
17451 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20  >aData;.  hdr = 
17452 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
17453 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32 62  ;.  cbrk = get2b
17454 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
17455 29 3b 0a 20 20 63 64 61 74 61 20 3d 20 70 43 68  );.  cdata = pCh
17456 69 6c 64 2d 3e 61 44 61 74 61 3b 0a 20 20 6d 65  ild->aData;.  me
17457 6d 63 70 79 28 63 64 61 74 61 2c 20 26 64 61 74  mcpy(cdata, &dat
17458 61 5b 68 64 72 5d 2c 20 70 50 61 67 65 2d 3e 63  a[hdr], pPage->c
17459 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 70 50 61 67  ellOffset+2*pPag
1745a 65 2d 3e 6e 43 65 6c 6c 2d 68 64 72 29 3b 0a 20  e->nCell-hdr);. 
1745b 20 6d 65 6d 63 70 79 28 26 63 64 61 74 61 5b 63   memcpy(&cdata[c
1745c 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b  brk], &data[cbrk
1745d 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 62  ], usableSize-cb
1745e 72 6b 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rk);..  assert( 
1745f 70 43 68 69 6c 64 2d 3e 69 73 49 6e 69 74 3d 3d  pChild->isInit==
17460 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  0 );.  rc = sqli
17461 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65  te3BtreeInitPage
17462 28 70 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20  (pChild);.  if( 
17463 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
17464 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20 3d  .    int nCopy =
17465 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
17466 77 2a 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e  w*sizeof(pPage->
17467 61 4f 76 66 6c 5b 30 5d 29 3b 0a 20 20 20 20 6d  aOvfl[0]);.    m
17468 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e 61 4f  emcpy(pChild->aO
17469 76 66 6c 2c 20 70 50 61 67 65 2d 3e 61 4f 76 66  vfl, pPage->aOvf
1746a 6c 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 70  l, nCopy);.    p
1746b 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77  Child->nOverflow
1746c 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66   = pPage->nOverf
1746d 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 70 43 68  low;.    if( pCh
1746e 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29  ild->nOverflow )
1746f 7b 0a 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e  {.      pChild->
17470 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 7d  nFree = 0;.    }
17471 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 68  .    assert( pCh
17472 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 50 61 67  ild->nCell==pPag
17473 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  e->nCell );.    
17474 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
17475 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
17476 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
17477 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28  );.    zeroPage(
17478 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61  pPage, pChild->a
17479 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c  Data[0] & ~PTF_L
1747a 45 41 46 29 3b 0a 20 20 20 20 70 75 74 34 62 79  EAF);.    put4by
1747b 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
1747c 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
1747d 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29  t+8], pgnoChild)
1747e 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22 42 41  ;.    TRACE(("BA
1747f 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74  LANCE: copy root
17480 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20   %d into %d\n", 
17481 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 43 68  pPage->pgno, pCh
17482 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20  ild->pgno));.   
17483 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
17484 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  M ){.      rc = 
17485 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70  ptrmapPut(pBt, p
17486 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50 54 52  Child->pgno, PTR
17487 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 67 65  MAP_BTREE, pPage
17488 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 65 66  ->pgno);.#ifndef
17489 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1748a 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66  OVACUUM.      if
1748b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1748c 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1748d 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28  setChildPtrmaps(
1748e 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 7d  pChild);.      }
1748f 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
17490 0a 20 20 20 20 20 20 20 20 70 43 68 69 6c 64 2d  .        pChild-
17491 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a  >nOverflow = 0;.
17492 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
17493 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
17494 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
17495 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65  .    pCur->iPage
17496 2b 2b 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 70  ++;.    pCur->ap
17497 50 61 67 65 5b 31 5d 20 3d 20 70 43 68 69 6c 64  Page[1] = pChild
17498 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64  ;.    pCur->aiId
17499 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 72 63  x[0] = 0;.    rc
1749a 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f   = balance_nonro
1749b 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73  ot(pCur);.  }els
1749c 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  e{.    releasePa
1749d 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 7d 0a  ge(pChild);.  }.
1749e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1749f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  ./*.** The page 
174a0 74 68 61 74 20 70 43 75 72 20 63 75 72 72 65 6e  that pCur curren
174a1 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 68 61  tly points to ha
174a2 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64 69  s just been modi
174a3 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20  fied in.** some 
174a4 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69  way. This functi
174a5 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20 69  on figures out i
174a6 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61 74  f this modificat
174a7 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a  ion means the.**
174a8 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20 62   tree needs to b
174a9 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64 20  e balanced, and 
174aa 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65 20  if so calls the 
174ab 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c 61  appropriate bala
174ac 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 69 6e  ncing .** routin
174ad 65 2e 0a 2a 2a 20 0a 2a 2a 20 50 61 72 61 6d 65  e..** .** Parame
174ae 74 65 72 20 69 73 49 6e 73 65 72 74 20 69 73 20  ter isInsert is 
174af 74 72 75 65 20 69 66 20 61 20 6e 65 77 20 63 65  true if a new ce
174b0 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73 65  ll was just inse
174b1 72 74 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a 2a  rted into the.**
174b2 20 70 61 67 65 2c 20 6f 72 20 66 61 6c 73 65 20   page, or false 
174b3 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
174b4 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65  atic int balance
174b5 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
174b6 20 69 6e 74 20 69 73 49 6e 73 65 72 74 29 7b 0a   int isInsert){.
174b7 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
174b8 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20  E_OK;.  MemPage 
174b9 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  *pPage = pCur->a
174ba 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
174bb 65 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  e];..  assert( s
174bc 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
174bd 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
174be 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 43  tex) );.  if( pC
174bf 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a  ur->iPage==0 ){.
174c0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
174c1 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
174c2 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
174c3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
174c4 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65  K && pPage->nOve
174c5 72 66 6c 6f 77 3e 30 20 29 7b 0a 20 20 20 20 20  rflow>0 ){.     
174c6 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64 65   rc = balance_de
174c7 65 70 65 72 28 70 43 75 72 29 3b 0a 20 20 20 20  eper(pCur);.    
174c8 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
174c9 61 70 50 61 67 65 5b 30 5d 3d 3d 70 50 61 67 65  apPage[0]==pPage
174ca 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
174cb 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
174cc 6f 77 3d 3d 30 20 7c 7c 20 72 63 21 3d 53 51 4c  ow==0 || rc!=SQL
174cd 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a  ITE_OK );.    }.
174ce 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
174cf 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e  TE_OK && pPage->
174d0 6e 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  nCell==0 ){.    
174d1 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 73    rc = balance_s
174d2 68 61 6c 6c 6f 77 65 72 28 70 43 75 72 29 3b 0a  hallower(pCur);.
174d3 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43        assert( pC
174d4 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3d 3d 70  ur->apPage[0]==p
174d5 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 61 73  Page );.      as
174d6 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
174d7 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 72 63 21  erflow==0 || rc!
174d8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
174d9 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
174da 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65   if( pPage->nOve
174db 72 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20 20 20  rflow>0 || .    
174dc 20 20 20 20 28 21 69 73 49 6e 73 65 72 74 20 26      (!isInsert &
174dd 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e 70  & pPage->nFree>p
174de 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
174df 65 53 69 7a 65 2a 32 2f 33 29 20 29 7b 0a 20 20  eSize*2/3) ){.  
174e0 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
174e1 5f 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 29 3b 0a  _nonroot(pCur);.
174e2 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
174e3 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
174e4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
174e5 63 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72 73 20  cks all cursors 
174e6 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 61  that point to ta
174e7 62 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a  ble pgnoRoot..**
174e8 20 49 66 20 61 6e 79 20 6f 66 20 74 68 6f 73 65   If any of those
174e9 20 63 75 72 73 6f 72 73 20 77 65 72 65 20 6f 70   cursors were op
174ea 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 67  ened with wrFlag
174eb 3d 3d 30 20 69 6e 20 61 20 64 69 66 66 65 72 65  ==0 in a differe
174ec 6e 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  nt.** database c
174ed 6f 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64 61 74  onnection (a dat
174ee 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
174ef 20 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65   that shares the
174f0 20 70 61 67 65 72 0a 2a 2a 20 63 61 63 68 65 20   pager.** cache 
174f1 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  with the current
174f2 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61 6e 64   connection) and
174f3 20 74 68 61 74 20 6f 74 68 65 72 20 63 6f 6e 6e   that other conn
174f4 65 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 6e 6f  ection .** is no
174f5 74 20 69 6e 20 74 68 65 20 52 65 61 64 55 6e 63  t in the ReadUnc
174f6 6f 6d 6d 6d 69 74 74 65 64 20 73 74 61 74 65 2c  ommmitted state,
174f7 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
174f8 6e 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53  ne returns .** S
174f9 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2a  QLITE_LOCKED..**
174fa 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 63  .** As well as c
174fb 75 72 73 6f 72 73 20 77 69 74 68 20 77 72 46 6c  ursors with wrFl
174fc 61 67 3d 3d 30 2c 20 63 75 72 73 6f 72 73 20 77  ag==0, cursors w
174fd 69 74 68 20 0a 2a 2a 20 69 73 49 6e 63 72 62 6c  ith .** isIncrbl
174fe 6f 62 48 61 6e 64 6c 65 3d 3d 31 20 61 72 65 20  obHandle==1 are 
174ff 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 20  also considered 
17500 27 72 65 61 64 27 20 63 75 72 73 6f 72 73 20 62  'read' cursors b
17501 65 63 61 75 73 65 0a 2a 2a 20 69 6e 63 72 65 6d  ecause.** increm
17502 65 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f  ental blob curso
17503 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20  rs are used for 
17504 62 6f 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64  both reading and
17505 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   writing..**.** 
17506 57 68 65 6e 20 70 67 6e 6f 52 6f 6f 74 20 69 73  When pgnoRoot is
17507 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
17508 66 20 61 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c  f an intkey tabl
17509 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  e, this function
1750a 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 70   is also.** resp
1750b 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76 61  onsible for inva
1750c 6c 69 64 61 74 69 6e 67 20 69 6e 63 72 65 6d 65  lidating increme
1750d 6e 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72  ntal blob cursor
1750e 73 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65  s when the table
1750f 20 72 6f 77 0a 2a 2a 20 6f 6e 20 77 68 69 63 68   row.** on which
17510 20 74 68 65 79 20 61 72 65 20 6f 70 65 6e 65 64   they are opened
17511 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20 6d   is deleted or m
17512 6f 64 69 66 69 65 64 2e 20 43 75 72 73 6f 72 73  odified. Cursors
17513 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64   are invalidated
17514 0a 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  .** according to
17515 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   the following r
17516 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29  ules:.**.**   1)
17517 20 57 68 65 6e 20 42 74 72 65 65 43 6c 65 61 72   When BtreeClear
17518 54 61 62 6c 65 28 29 20 69 73 20 63 61 6c 6c 65  Table() is calle
17519 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20  d to completely 
1751a 64 65 6c 65 74 65 20 74 68 65 20 63 6f 6e 74 65  delete the conte
1751b 6e 74 73 0a 2a 2a 20 20 20 20 20 20 6f 66 20 61  nts.**      of a
1751c 20 42 2d 54 72 65 65 20 74 61 62 6c 65 2c 20 70   B-Tree table, p
1751d 45 78 63 6c 75 64 65 20 69 73 20 73 65 74 20 74  Exclude is set t
1751e 6f 20 7a 65 72 6f 20 61 6e 64 20 70 61 72 61 6d  o zero and param
1751f 65 74 65 72 20 69 52 6f 77 20 69 73 20 0a 2a 2a  eter iRow is .**
17520 20 20 20 20 20 20 73 65 74 20 74 6f 20 6e 6f 6e        set to non
17521 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 69 73 20 63  -zero. In this c
17522 61 73 65 20 61 6c 6c 20 69 6e 63 72 65 6d 65 6e  ase all incremen
17523 74 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 73  tal blob cursors
17524 20 6f 70 65 6e 0a 2a 2a 20 20 20 20 20 20 6f 6e   open.**      on
17525 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65   the table roote
17526 64 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 61 72  d at pgnoRoot ar
17527 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a  e invalidated..*
17528 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 42  *.**   2) When B
17529 74 72 65 65 49 6e 73 65 72 74 28 29 2c 20 42 74  treeInsert(), Bt
1752a 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42  reeDelete() or B
1752b 74 72 65 65 50 75 74 44 61 74 61 28 29 20 69 73  treePutData() is
1752c 20 63 61 6c 6c 65 64 20 74 6f 20 0a 2a 2a 20 20   called to .**  
1752d 20 20 20 20 6d 6f 64 69 66 79 20 61 20 74 61 62      modify a tab
1752e 6c 65 20 72 6f 77 20 76 69 61 20 61 6e 20 53 51  le row via an SQ
1752f 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 70 45 78  L statement, pEx
17530 63 6c 75 64 65 20 69 73 20 73 65 74 20 74 6f 20  clude is set to 
17531 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 77 72 69  the .**      wri
17532 74 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 74  te cursor used t
17533 6f 20 64 6f 20 74 68 65 20 6d 6f 64 69 66 69 63  o do the modific
17534 61 74 69 6f 6e 20 61 6e 64 20 70 61 72 61 6d 65  ation and parame
17535 74 65 72 20 69 52 6f 77 20 69 73 20 73 65 74 0a  ter iRow is set.
17536 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 69  **      to the i
17537 6e 74 65 67 65 72 20 72 6f 77 20 69 64 20 6f 66  nteger row id of
17538 20 74 68 65 20 42 2d 54 72 65 65 20 65 6e 74 72   the B-Tree entr
17539 79 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64  y being modified
1753a 2e 20 55 6e 6c 65 73 73 0a 2a 2a 20 20 20 20 20  . Unless.**     
1753b 20 70 45 78 63 6c 75 64 65 20 69 73 20 69 74 73   pExclude is its
1753c 65 6c 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74  elf an increment
1753d 61 6c 20 62 6c 6f 62 20 63 75 72 73 6f 72 2c 20  al blob cursor, 
1753e 74 68 65 6e 20 61 6c 6c 20 69 6e 63 72 65 6d 65  then all increme
1753f 6e 74 61 6c 0a 2a 2a 20 20 20 20 20 20 62 6c 6f  ntal.**      blo
17540 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  b cursors open o
17541 6e 20 72 6f 77 20 69 52 6f 77 20 6f 66 20 74 68  n row iRow of th
17542 65 20 42 2d 54 72 65 65 20 61 72 65 20 69 6e 76  e B-Tree are inv
17543 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  alidated..**.** 
17544 20 20 33 29 20 49 66 20 62 6f 74 68 20 70 45 78    3) If both pEx
17545 63 6c 75 64 65 20 61 6e 64 20 69 52 6f 77 20 61  clude and iRow a
17546 72 65 20 73 65 74 20 74 6f 20 7a 65 72 6f 2c 20  re set to zero, 
17547 6e 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62  no incremental b
17548 6c 6f 62 20 0a 2a 2a 20 20 20 20 20 20 63 75 72  lob .**      cur
17549 73 6f 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64  sors are invalid
1754a 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ated..*/.static 
1754b 69 6e 74 20 63 68 65 63 6b 46 6f 72 52 65 61 64  int checkForRead
1754c 43 6f 6e 66 6c 69 63 74 73 28 0a 20 20 42 74 72  Conflicts(.  Btr
1754d 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 20  ee *pBtree,     
1754e 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1754f 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68 65  base file to che
17550 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  ck */.  Pgno pgn
17551 6f 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20  oRoot,          
17552 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 72 65 61 64  /* Look for read
17553 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 69 73   cursors on this
17554 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 43 75   btree */.  BtCu
17555 72 73 6f 72 20 2a 70 45 78 63 6c 75 64 65 2c 20  rsor *pExclude, 
17556 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68      /* Ignore th
17557 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69  is cursor */.  i
17558 36 34 20 69 52 6f 77 20 20 20 20 20 20 20 20 20  64 iRow         
17559 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f         /* The ro
1755a 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 20 62  wid that might b
1755b 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 29 7b  e changing */.){
1755c 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a  .  BtCursor *p;.
1755d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1755e 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20  = pBtree->pBt;. 
1755f 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
17560 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Btree->db;.  ass
17561 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
17562 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 72  eHoldsMutex(pBtr
17563 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  ee) );.  for(p=p
17564 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
17565 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
17566 20 69 66 28 20 70 3d 3d 70 45 78 63 6c 75 64 65   if( p==pExclude
17567 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
17568 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74   if( p->pgnoRoot
17569 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63 6f 6e  !=pgnoRoot ) con
1756a 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53  tinue;.#ifndef S
1756b 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
1756c 4c 4f 42 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  LOB.    if( p->i
1756d 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
1756e 26 26 20 28 20 0a 20 20 20 20 20 20 20 20 20 28  && ( .         (
1756f 21 70 45 78 63 6c 75 64 65 20 26 26 20 69 52 6f  !pExclude && iRo
17570 77 29 0a 20 20 20 20 20 20 7c 7c 20 28 70 45 78  w).      || (pEx
17571 63 6c 75 64 65 20 26 26 20 21 70 45 78 63 6c 75  clude && !pExclu
17572 64 65 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61  de->isIncrblobHa
17573 6e 64 6c 65 20 26 26 20 70 2d 3e 69 6e 66 6f 2e  ndle && p->info.
17574 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20 20 20  nKey==iRow).    
17575 29 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53 74  )){.      p->eSt
17576 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
17577 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ALID;.    }.#end
17578 69 66 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 53  if.    if( p->eS
17579 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c  tate!=CURSOR_VAL
1757a 49 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ID ) continue;. 
1757b 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c 61 67     if( p->wrFlag
1757c 3d 3d 30 20 0a 23 69 66 6e 64 65 66 20 53 51 4c  ==0 .#ifndef SQL
1757d 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1757e 42 0a 20 20 20 20 20 7c 7c 20 70 2d 3e 69 73 49  B.     || p->isI
1757f 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 0a 23 65  ncrblobHandle.#e
17580 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20 20 20 20  ndif.    ){.    
17581 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f 74 68    sqlite3 *dbOth
17582 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65 2d 3e  er = p->pBtree->
17583 64 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  db;.      assert
17584 28 64 62 4f 74 68 65 72 29 3b 0a 20 20 20 20 20  (dbOther);.     
17585 20 69 66 28 20 64 62 4f 74 68 65 72 21 3d 64 62   if( dbOther!=db
17586 20 26 26 20 28 64 62 4f 74 68 65 72 2d 3e 66 6c   && (dbOther->fl
17587 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61  ags & SQLITE_Rea
17588 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 3d 3d 30  dUncommitted)==0
17589 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1758a 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
1758b 63 6b 65 64 28 64 62 2c 20 64 62 4f 74 68 65 72  cked(db, dbOther
1758c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1758d 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f  n SQLITE_LOCKED_
1758e 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20  SHAREDCACHE;.   
1758f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
17590 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
17591 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65  K;.}../*.** Inse
17592 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20  rt a new record 
17593 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e 20  into the BTree. 
17594 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 65   The key is give
17595 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 29  n by (pKey,nKey)
17596 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61  .** and the data
17597 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 44   is given by (pD
17598 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 65  ata,nData).  The
17599 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20   cursor is used 
1759a 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 6e  only to.** defin
1759b 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 65  e what table the
1759c 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 62   record should b
1759d 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e  e inserted into.
1759e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20    The cursor.** 
1759f 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
175a0 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f 63   at a random loc
175a1 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ation..**.** For
175a2 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65   an INTKEY table
175a3 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 20  , only the nKey 
175a4 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79  value of the key
175a5 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 20   is used.  pKey 
175a6 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20  is.** ignored.  
175a7 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 74  For a ZERODATA t
175a8 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 20  able, the pData 
175a9 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 6f  and nData are bo
175aa 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a  th ignored..**.*
175ab 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 73  * If the seekRes
175ac 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  ult parameter is
175ad 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
175ae 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  a successful cal
175af 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 42  l to.** sqlite3B
175b0 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 74 6f 20  treeMoveto() to 
175b1 73 65 65 6b 20 63 75 72 73 6f 72 20 70 43 75 72  seek cursor pCur
175b2 20 74 6f 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29   to (pKey, nKey)
175b3 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20   has already.** 
175b4 62 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20  been performed. 
175b5 73 65 65 6b 52 65 73 75 6c 74 20 69 73 20 74 68  seekResult is th
175b6 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 20  e search result 
175b7 72 65 74 75 72 6e 65 64 20 28 61 20 6e 65 67 61  returned (a nega
175b8 74 69 76 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69  tive.** number i
175b9 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74  f pCur points at
175ba 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 69   an entry that i
175bb 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28  s smaller than (
175bc 70 4b 65 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a  pKey, nKey), or.
175bd 2a 2a 20 61 20 70 6f 73 69 74 69 76 65 20 76 61  ** a positive va
175be 6c 75 65 20 69 66 20 70 43 75 72 20 70 6f 69 6e  lue if pCur poin
175bf 74 73 20 61 74 20 61 6e 20 65 74 72 79 20 74 68  ts at an etry th
175c0 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61  at is larger tha
175c1 6e 20 0a 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65  n .** (pKey, nKe
175c2 79 29 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  y)). .**.** If t
175c3 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61  he seekResult pa
175c4 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68  rameter is 0, th
175c5 65 6e 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d  en cursor pCur m
175c6 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 20  ay point to any 
175c7 0a 2a 2a 20 65 6e 74 72 79 20 6f 72 20 74 6f 20  .** entry or to 
175c8 6e 6f 20 65 6e 74 72 79 20 61 74 20 61 6c 6c 2e  no entry at all.
175c9 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
175ca 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20  is function has 
175cb 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 68 65 20 63  to seek.** the c
175cc 75 72 73 6f 72 20 62 65 66 6f 72 65 20 74 68 65  ursor before the
175cd 20 6e 65 77 20 6b 65 79 20 63 61 6e 20 62 65 20   new key can be 
175ce 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c  inserted..*/.SQL
175cf 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
175d0 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65  sqlite3BtreeInse
175d1 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  rt(.  BtCursor *
175d2 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20  pCur,           
175d3 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 64       /* Insert d
175d4 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74 61 62  ata into the tab
175d5 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f  le of this curso
175d6 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  r */.  const voi
175d7 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65  d *pKey, i64 nKe
175d8 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  y,    /* The key
175d9 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f   of the new reco
175da 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  rd */.  const vo
175db 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  id *pData, int n
175dc 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 64 61  Data,  /* The da
175dd 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65  ta of the new re
175de 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a  cord */.  int nZ
175df 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  ero,            
175e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
175e1 65 72 20 6f 66 20 65 78 74 72 61 20 30 20 62 79  er of extra 0 by
175e2 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f  tes to append to
175e3 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 61   data */.  int a
175e4 70 70 65 6e 64 42 69 61 73 2c 20 20 20 20 20 20  ppendBias,      
175e5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
175e6 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b  e if this is lik
175e7 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f  ely an append */
175e8 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c  .  int seekResul
175e9 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
175ea 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70    /* Result of p
175eb 72 69 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65  rior sqlite3Btre
175ec 65 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20 2a  eMoveto() call *
175ed 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
175ee 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52   int loc = seekR
175ef 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 73 7a 4e  esult;.  int szN
175f0 65 77 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20  ew;.  int idx;. 
175f1 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
175f2 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43  .  Btree *p = pC
175f3 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74  ur->pBtree;.  Bt
175f4 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
175f5 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64  >pBt;.  unsigned
175f6 20 63 68 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a   char *oldCell;.
175f7 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
175f8 2a 6e 65 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20  *newCell = 0;.. 
175f9 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
175fa 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
175fb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  );.  assert( pBt
175fc 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
175fd 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a  =TRANS_WRITE );.
175fe 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e    assert( !pBt->
175ff 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73  readOnly );.  as
17600 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c  sert( pCur->wrFl
17601 61 67 20 29 3b 0a 20 20 72 63 20 3d 20 63 68 65  ag );.  rc = che
17602 63 6b 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63  ckForReadConflic
17603 74 73 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2c  ts(pCur->pBtree,
17604 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
17605 20 70 43 75 72 2c 20 6e 4b 65 79 29 3b 0a 20 20   pCur, nKey);.  
17606 69 66 28 20 72 63 20 29 7b 20 20 20 20 20 20 20  if( rc ){       
17607 20 20 20 20 20 20 0a 20 20 20 20 2f 2a 20 54 68        .    /* Th
17608 65 20 74 61 62 6c 65 20 70 43 75 72 20 70 6f 69  e table pCur poi
17609 6e 74 73 20 74 6f 20 68 61 73 20 61 20 72 65 61  nts to has a rea
1760a 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 61 73  d lock */.    as
1760b 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1760c 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41  _LOCKED_SHAREDCA
1760d 43 48 45 20 29 3b 0a 20 20 20 20 72 65 74 75 72  CHE );.    retur
1760e 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20  n rc;.  }.  if( 
1760f 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
17610 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20  RSOR_FAULT ){.  
17611 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73    return pCur->s
17612 6b 69 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  kip;.  }..  /* S
17613 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  ave the position
17614 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 63  s of any other c
17615 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74  ursors open on t
17616 68 69 73 20 74 61 62 6c 65 2e 0a 20 20 2a 2a 0a  his table..  **.
17617 20 20 2a 2a 20 49 6e 20 73 6f 6d 65 20 63 61 73    ** In some cas
17618 65 73 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  es, the call to 
17619 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1761a 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 20  to() below is a 
1761b 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20  no-op. For.  ** 
1761c 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e  example, when in
1761d 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e 74  serting data int
1761e 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 61  o a table with a
1761f 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 6e  uto-generated in
17620 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c  teger.  ** keys,
17621 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20   the VDBE layer 
17622 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 42  invokes sqlite3B
17623 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 69  treeLast() to fi
17624 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 20  gure out the .  
17625 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 74  ** integer key t
17626 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 63  o use. It then c
17627 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69  alls this functi
17628 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 69  on to actually i
17629 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a 20  nsert the .  ** 
1762a 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 6e  data into the in
1762b 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e 20  tkey B-Tree. In 
1762c 74 68 69 73 20 63 61 73 65 20 73 71 6c 69 74 65  this case sqlite
1762d 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 72  3BtreeMoveto() r
1762e 65 63 6f 67 6e 69 7a 65 73 0a 20 20 2a 2a 20 74  ecognizes.  ** t
1762f 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69  hat the cursor i
17630 73 20 61 6c 72 65 61 64 79 20 77 68 65 72 65 20  s already where 
17631 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61  it needs to be a
17632 6e 64 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f  nd returns witho
17633 75 74 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 6e  ut.  ** doing an
17634 79 20 77 6f 72 6b 2e 20 54 6f 20 61 76 6f 69 64  y work. To avoid
17635 20 74 68 77 61 72 74 69 6e 67 20 74 68 65 73 65   thwarting these
17636 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2c 20   optimizations, 
17637 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 0a  it is important.
17638 20 20 2a 2a 20 6e 6f 74 20 74 6f 20 63 6c 65 61    ** not to clea
17639 72 20 74 68 65 20 63 75 72 73 6f 72 20 68 65 72  r the cursor her
1763a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 0a 20 20  e..  */.  if(.  
1763b 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63    SQLITE_OK!=(rc
1763c 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72   = saveAllCursor
1763d 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e  s(pBt, pCur->pgn
1763e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29 20 7c 7c  oRoot, pCur)) ||
1763f 20 28 21 6c 6f 63 20 26 26 0a 20 20 20 20 53 51   (!loc &&.    SQ
17640 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
17641 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
17642 6f 28 70 43 75 72 2c 20 70 4b 65 79 2c 20 6e 4b  o(pCur, pKey, nK
17643 65 79 2c 20 61 70 70 65 6e 64 42 69 61 73 2c 20  ey, appendBias, 
17644 26 6c 6f 63 29 29 0a 20 20 29 29 7b 0a 20 20 20  &loc)).  )){.   
17645 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
17646 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
17647 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
17648 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  age];.  assert( 
17649 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c  pPage->intKey ||
1764a 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73   nKey>=0 );.  as
1764b 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61  sert( pPage->lea
1764c 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74  f || !pPage->int
1764d 4b 65 79 20 29 3b 0a 20 20 54 52 41 43 45 28 28  Key );.  TRACE((
1764e 22 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25  "INSERT: table=%
1764f 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74  d nkey=%lld ndat
17650 61 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c  a=%d page=%d %s\
17651 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43  n",.          pC
17652 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b  ur->pgnoRoot, nK
17653 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65  ey, nData, pPage
17654 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20  ->pgno,.        
17655 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72    loc==0 ? "over
17656 77 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e  write" : "new en
17657 74 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74  try"));.  assert
17658 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ( pPage->isInit 
17659 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d  );.  allocateTem
1765a 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e  pSpace(pBt);.  n
1765b 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54  ewCell = pBt->pT
1765c 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e  mpSpace;.  if( n
1765d 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75  ewCell==0 ) retu
1765e 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1765f 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65  .  rc = fillInCe
17660 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c  ll(pPage, newCel
17661 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70  l, pKey, nKey, p
17662 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65  Data, nData, nZe
17663 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69  ro, &szNew);.  i
17664 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64  f( rc ) goto end
17665 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72  _insert;.  asser
17666 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69  t( szNew==cellSi
17667 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77  zePtr(pPage, new
17668 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72  Cell) );.  asser
17669 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c  t( szNew<=MX_CEL
1766a 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20  L_SIZE(pBt) );. 
1766b 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49   idx = pCur->aiI
1766c 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  dx[pCur->iPage];
1766d 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 26 26  .  if( loc==0 &&
1766e 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70   CURSOR_VALID==p
1766f 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20  Cur->eState ){. 
17670 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 20     u16 szOld;.  
17671 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 50    assert( idx<pP
17672 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
17673 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
17674 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e  gerWrite(pPage->
17675 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
17676 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 67 6f  ( rc ){.      go
17677 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20  to end_insert;. 
17678 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 6c 6c     }.    oldCell
17679 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67   = findCell(pPag
1767a 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 66 28  e, idx);.    if(
1767b 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
1767c 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e 65  .      memcpy(ne
1767d 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c 20  wCell, oldCell, 
1767e 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a  4);.    }.    sz
1767f 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  Old = cellSizePt
17680 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c  r(pPage, oldCell
17681 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61  );.    rc = clea
17682 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c 64  rCell(pPage, old
17683 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  Cell);.    if( r
17684 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73  c ) goto end_ins
17685 65 72 74 3b 0a 20 20 20 20 72 63 20 3d 20 64 72  ert;.    rc = dr
17686 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64  opCell(pPage, id
17687 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20 20 20 20 69  x, szOld);.    i
17688 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
17689 20 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20   ) {.      goto 
1768a 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20  end_insert;.    
1768b 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f  }.  }else if( lo
1768c 63 3c 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43  c<0 && pPage->nC
1768d 65 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73  ell>0 ){.    ass
1768e 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66  ert( pPage->leaf
1768f 20 29 3b 0a 20 20 20 20 69 64 78 20 3d 20 2b 2b   );.    idx = ++
17690 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
17691 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 70 43  ->iPage];.    pC
17692 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
17693 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61   0;.    pCur->va
17694 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d  lidNKey = 0;.  }
17695 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
17696 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b  ( pPage->leaf );
17697 0a 20 20 7d 0a 20 20 72 63 20 3d 20 69 6e 73 65  .  }.  rc = inse
17698 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64  rtCell(pPage, id
17699 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e 65  x, newCell, szNe
1769a 77 2c 20 30 2c 20 30 29 3b 0a 20 20 61 73 73 65  w, 0, 0);.  asse
1769b 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
1769c 4b 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 43 65 6c  K || pPage->nCel
1769d 6c 3e 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f  l>0 || pPage->nO
1769e 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20  verflow>0 );..  
1769f 2f 2a 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 68  /* If no error h
176a0 61 73 20 6f 63 63 75 72 65 64 2c 20 63 61 6c 6c  as occured, call
176a1 20 62 61 6c 61 6e 63 65 28 29 20 74 6f 20 64 65   balance() to de
176a2 61 6c 20 77 69 74 68 20 61 6e 79 20 6f 76 65 72  al with any over
176a3 66 6c 6f 77 20 61 6e 64 0a 20 20 2a 2a 20 6d 6f  flow and.  ** mo
176a4 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
176a5 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 72 6f   point at the ro
176a6 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ot of the table 
176a7 28 73 69 6e 63 65 20 62 61 6c 61 6e 63 65 20 6d  (since balance m
176a8 61 79 0a 20 20 2a 2a 20 68 61 76 65 20 72 65 61  ay.  ** have rea
176a9 72 72 61 6e 67 65 64 20 74 68 65 20 74 61 62 6c  rranged the tabl
176aa 65 20 69 6e 20 73 75 63 68 20 61 20 77 61 79 20  e in such a way 
176ab 61 73 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65  as to invalidate
176ac 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61 67 65   BtCursor.apPage
176ad 5b 5d 0a 20 20 2a 2a 20 6f 72 20 42 74 43 75 72  [].  ** or BtCur
176ae 73 6f 72 2e 61 69 49 64 78 5b 5d 29 2e 0a 20 20  sor.aiIdx[])..  
176af 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 2c 20  **.  ** Except, 
176b0 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f  if all of the fo
176b1 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65  llowing are true
176b2 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 3a 0a 20 20  , do nothing:.  
176b3 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 49 6e 73 65  **.  **   * Inse
176b4 72 74 69 6e 67 20 74 68 65 20 6e 65 77 20 63 65  rting the new ce
176b5 6c 6c 20 64 69 64 20 6e 6f 74 20 63 61 75 73 65  ll did not cause
176b6 20 6f 76 65 72 66 6c 6f 77 2c 0a 20 20 2a 2a 0a   overflow,.  **.
176b7 20 20 2a 2a 20 20 20 2a 20 42 65 66 6f 72 65 20    **   * Before 
176b8 69 6e 73 65 72 74 69 6e 67 20 74 68 65 20 6e 65  inserting the ne
176b9 77 20 63 65 6c 6c 20 74 68 65 20 63 75 72 73 6f  w cell the curso
176ba 72 20 77 61 73 20 70 6f 69 6e 74 69 6e 67 20 61  r was pointing a
176bb 74 20 74 68 65 20 0a 20 20 2a 2a 20 20 20 20 20  t the .  **     
176bc 6c 61 72 67 65 73 74 20 6b 65 79 20 69 6e 20 61  largest key in a
176bd 6e 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 2c  n intkey B-Tree,
176be 20 61 6e 64 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20   and.  **.  **  
176bf 20 2a 20 54 68 65 20 6b 65 79 20 76 61 6c 75 65   * The key value
176c0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
176c1 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 69 73   the new cell is
176c2 20 6e 6f 77 20 74 68 65 20 6c 61 72 67 65 73 74   now the largest
176c3 20 0a 20 20 2a 2a 20 20 20 20 20 69 6e 20 74 68   .  **     in th
176c4 65 20 42 2d 54 72 65 65 2e 0a 20 20 2a 2a 0a 20  e B-Tree..  **. 
176c5 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   ** In this case
176c6 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20   the cursor can 
176c7 62 65 20 73 61 66 65 6c 79 20 6c 65 66 74 20 70  be safely left p
176c8 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20 28  ointing at the (
176c9 6e 65 77 29 20 0a 20 20 2a 2a 20 6c 61 72 67 65  new) .  ** large
176ca 73 74 20 6b 65 79 20 76 61 6c 75 65 20 69 6e 20  st key value in 
176cb 74 68 65 20 42 2d 54 72 65 65 2e 20 44 6f 69 6e  the B-Tree. Doin
176cc 67 20 73 6f 20 73 70 65 65 64 73 20 75 70 20 69  g so speeds up i
176cd 6e 73 65 72 74 69 6e 67 20 61 20 73 65 74 0a 20  nserting a set. 
176ce 20 2a 2a 20 6f 66 20 65 6e 74 72 69 65 73 20 77   ** of entries w
176cf 69 74 68 20 69 6e 63 72 65 61 73 69 6e 67 20 69  ith increasing i
176d0 6e 74 65 67 65 72 20 6b 65 79 20 76 61 6c 75 65  nteger key value
176d1 73 20 76 69 61 20 61 20 73 69 6e 67 6c 65 20 63  s via a single c
176d2 75 72 73 6f 72 0a 20 20 2a 2a 20 28 63 6f 6d 65  ursor.  ** (come
176d3 73 20 75 70 20 77 69 74 68 20 22 49 4e 53 45 52  s up with "INSER
176d4 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43  T INTO ... SELEC
176d5 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74  T ..." statement
176d6 73 29 2c 20 61 73 20 0a 20 20 2a 2a 20 74 68 65  s), as .  ** the
176d7 20 6e 65 78 74 20 69 6e 73 65 72 74 20 6f 70 65   next insert ope
176d8 72 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65  ration is not re
176d9 71 75 69 72 65 64 20 74 6f 20 73 65 65 6b 20 74  quired to seek t
176da 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a  he cursor..  */.
176db 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
176dc 5f 4f 4b 20 0a 20 20 20 26 26 20 28 70 50 61 67  _OK .   && (pPag
176dd 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20  e->nOverflow || 
176de 21 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 7c 7c  !pCur->atLast ||
176df 20 6c 6f 63 3e 3d 30 20 7c 7c 20 21 70 43 75 72   loc>=0 || !pCur
176e0 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74  ->apPage[0]->int
176e1 4b 65 79 29 0a 20 20 29 7b 0a 20 20 20 20 72 63  Key).  ){.    rc
176e2 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c   = balance(pCur,
176e3 20 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d   1);.    if( rc=
176e4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
176e5 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70      moveToRoot(p
176e6 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Cur);.    }.  }.
176e7 20 20 0a 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b    .  /* Must mak
176e8 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77  e sure nOverflow
176e9 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72   is reset to zer
176ea 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20 62 61  o even if the ba
176eb 6c 61 6e 63 65 28 29 0a 20 20 2a 2a 20 66 61 69  lance().  ** fai
176ec 6c 73 2e 20 20 49 6e 74 65 72 6e 61 6c 20 64 61  ls.  Internal da
176ed 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72  ta structure cor
176ee 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73  ruption will res
176ef 75 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 2a  ult otherwise. *
176f0 2f 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65  /.  pCur->apPage
176f1 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
176f2 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 65  Overflow = 0;..e
176f3 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 74  nd_insert:.  ret
176f4 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
176f5 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72   Delete the entr
176f6 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  y that the curso
176f7 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
176f8 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a  .  The cursor.**
176f9 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
176fa 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72 79  g at a arbitrary
176fb 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   location..*/.SQ
176fc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
176fd 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c   sqlite3BtreeDel
176fe 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ete(BtCursor *pC
176ff 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ur){.  MemPage *
17700 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
17701 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
17702 5d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20  ];.  int idx;.  
17703 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
17704 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Cell;.  int rc;.
17705 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64    Pgno pgnoChild
17706 20 3d 20 30 3b 0a 20 20 42 74 72 65 65 20 2a 70   = 0;.  Btree *p
17707 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b   = pCur->pBtree;
17708 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
17709 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73   = p->pBt;..  as
1770a 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
1770b 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
1770c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1770d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73  >isInit );.  ass
1770e 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
1770f 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
17710 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74  RITE );.  assert
17711 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  ( !pBt->readOnly
17712 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e   );.  if( pCur->
17713 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46  eState==CURSOR_F
17714 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75  AULT ){.    retu
17715 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20  rn pCur->skip;. 
17716 20 7d 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70   }.  if( NEVER(p
17717 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
17718 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e  >iPage]>=pPage->
17719 6e 43 65 6c 6c 29 20 29 7b 0a 20 20 20 20 72 65  nCell) ){.    re
1771a 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1771b 52 3b 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f  R;  /* The curso
1771c 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e  r is not pointin
1771d 67 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 2a 2f  g to anything */
1771e 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1771f 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b 0a 20  Cur->wrFlag );. 
17720 20 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 65   rc = checkForRe
17721 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 70  adConflicts(p, p
17722 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70  Cur->pgnoRoot, p
17723 43 75 72 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  Cur, pCur->info.
17724 6e 4b 65 79 29 3b 0a 20 20 69 66 28 20 72 63 21  nKey);.  if( rc!
17725 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17726 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70    /* The table p
17727 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 68 61  Cur points to ha
17728 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f  s a read lock */
17729 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
1772a 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53  =SQLITE_LOCKED_S
1772b 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20  HAREDCACHE );.  
1772c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
1772d 0a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 74  ..  /* Restore t
1772e 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f  he current curso
1772f 72 20 70 6f 73 69 74 69 6f 6e 20 28 61 20 6e 6f  r position (a no
17730 2d 6f 70 20 69 66 20 74 68 65 20 63 75 72 73 6f  -op if the curso
17731 72 20 69 73 20 6e 6f 74 20 69 6e 20 0a 20 20 2a  r is not in .  *
17732 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  * CURSOR_REQUIRE
17733 53 45 45 4b 20 73 74 61 74 65 29 20 61 6e 64 20  SEEK state) and 
17734 73 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f  save the positio
17735 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20  ns of any other 
17736 63 75 72 73 6f 72 73 20 0a 20 20 2a 2a 20 6f 70  cursors .  ** op
17737 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74  en on the same t
17738 61 62 6c 65 2e 20 54 68 65 6e 20 63 61 6c 6c 20  able. Then call 
17739 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1773a 65 28 29 20 6f 6e 20 74 68 65 20 70 61 67 65 0a  e() on the page.
1773b 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 65 6e    ** that the en
1773c 74 72 79 20 77 69 6c 6c 20 62 65 20 64 65 6c 65  try will be dele
1773d 74 65 64 20 66 72 6f 6d 2e 0a 20 20 2a 2f 0a 20  ted from..  */. 
1773e 20 69 66 28 20 0a 20 20 20 20 28 72 63 20 3d 20   if( .    (rc = 
1773f 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
17740 69 74 69 6f 6e 28 70 43 75 72 29 29 21 3d 30 20  ition(pCur))!=0 
17741 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20 73 61 76  ||.    (rc = sav
17742 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c  eAllCursors(pBt,
17743 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
17744 20 70 43 75 72 29 29 21 3d 30 20 7c 7c 0a 20 20   pCur))!=0 ||.  
17745 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50    (rc = sqlite3P
17746 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
17747 3e 70 44 62 50 61 67 65 29 29 21 3d 30 0a 20 20  >pDbPage))!=0.  
17748 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
17749 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61  ;.  }..  /* Loca
1774a 74 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68  te the cell with
1774b 69 6e 20 69 74 73 20 70 61 67 65 20 61 6e 64 20  in its page and 
1774c 6c 65 61 76 65 20 70 43 65 6c 6c 20 70 6f 69 6e  leave pCell poin
1774d 74 69 6e 67 20 74 6f 20 74 68 65 0a 20 20 2a 2a  ting to the.  **
1774e 20 64 61 74 61 2e 20 54 68 65 20 63 6c 65 61 72   data. The clear
1774f 43 65 6c 6c 28 29 20 63 61 6c 6c 20 66 72 65 65  Cell() call free
17750 73 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70  s any overflow p
17751 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ages associated 
17752 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 63 65  with the.  ** ce
17753 6c 6c 2e 20 54 68 65 20 63 65 6c 6c 20 69 74 73  ll. The cell its
17754 65 6c 66 20 69 73 20 73 74 69 6c 6c 20 69 6e 74  elf is still int
17755 61 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 64 78 20  act..  */.  idx 
17756 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  = pCur->aiIdx[pC
17757 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 70 43  ur->iPage];.  pC
17758 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
17759 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 69 66  Page, idx);.  if
1775a 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
1775b 7b 0a 20 20 20 20 70 67 6e 6f 43 68 69 6c 64 20  {.    pgnoChild 
1775c 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c  = get4byte(pCell
1775d 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c  );.  }.  rc = cl
1775e 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70  earCell(pPage, p
1775f 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 72 63 20  Cell);.  if( rc 
17760 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
17761 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50  ;.  }..  if( !pP
17762 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
17763 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 65   /*.    ** The e
17764 6e 74 72 79 20 77 65 20 61 72 65 20 61 62 6f 75  ntry we are abou
17765 74 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e  t to delete is n
17766 6f 74 20 61 20 6c 65 61 66 20 73 6f 20 69 66 20  ot a leaf so if 
17767 77 65 20 64 6f 20 6e 6f 74 0a 20 20 20 20 2a 2a  we do not.    **
17768 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 65   do something we
17769 20 77 69 6c 6c 20 6c 65 61 76 65 20 61 20 68 6f   will leave a ho
1776a 6c 65 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61  le on an interna
1776b 6c 20 70 61 67 65 2e 0a 20 20 20 20 2a 2a 20 57  l page..    ** W
1776c 65 20 68 61 76 65 20 74 6f 20 66 69 6c 6c 20 74  e have to fill t
1776d 68 65 20 68 6f 6c 65 20 62 79 20 6d 6f 76 69 6e  he hole by movin
1776e 67 20 69 6e 20 61 20 63 65 6c 6c 20 66 72 6f 6d  g in a cell from
1776f 20 61 20 6c 65 61 66 2e 20 20 54 68 65 0a 20 20   a leaf.  The.  
17770 20 20 2a 2a 20 6e 65 78 74 20 43 65 6c 6c 20 61    ** next Cell a
17771 66 74 65 72 20 74 68 65 20 6f 6e 65 20 74 6f 20  fter the one to 
17772 62 65 20 64 65 6c 65 74 65 64 20 69 73 20 67 75  be deleted is gu
17773 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73  aranteed to exis
17774 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 6f 20  t and.    ** to 
17775 62 65 20 61 20 6c 65 61 66 20 73 6f 20 77 65 20  be a leaf so we 
17776 63 61 6e 20 75 73 65 20 69 74 2e 0a 20 20 20 20  can use it..    
17777 2a 2f 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20  */.    BtCursor 
17778 6c 65 61 66 43 75 72 3b 0a 20 20 20 20 4d 65 6d  leafCur;.    Mem
17779 50 61 67 65 20 2a 70 4c 65 61 66 50 61 67 65 20  Page *pLeafPage 
1777a 3d 20 30 3b 0a 0a 20 20 20 20 75 6e 73 69 67 6e  = 0;..    unsign
1777b 65 64 20 63 68 61 72 20 2a 70 4e 65 78 74 3b 0a  ed char *pNext;.
1777c 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b      int notUsed;
1777d 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
1777e 61 72 20 2a 74 65 6d 70 43 65 6c 6c 20 3d 20 30  ar *tempCell = 0
1777f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  ;.    assert( !p
17780 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a  Page->intKey );.
17781 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
17782 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 70 43  GetTempCursor(pC
17783 75 72 2c 20 26 6c 65 61 66 43 75 72 29 3b 0a 20  ur, &leafCur);. 
17784 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
17785 74 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75  treeNext(&leafCu
17786 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20  r, &notUsed);.  
17787 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
17788 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73  _OK ){.      ass
17789 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61 69 49  ert( leafCur.aiI
1778a 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65  dx[leafCur.iPage
1778b 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c  ]==0 );.      pL
1778c 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 43 75  eafPage = leafCu
1778d 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 75 72  r.apPage[leafCur
1778e 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 20 72  .iPage];.      r
1778f 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
17790 57 72 69 74 65 28 70 4c 65 61 66 50 61 67 65 2d  Write(pLeafPage-
17791 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 7d  >pDbPage);.    }
17792 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
17793 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17794 69 6e 74 20 6c 65 61 66 43 75 72 73 6f 72 49 6e  int leafCursorIn
17795 76 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20  valid = 0;.     
17796 20 75 31 36 20 73 7a 4e 65 78 74 3b 0a 20 20 20   u16 szNext;.   
17797 20 20 20 54 52 41 43 45 28 28 22 44 45 4c 45 54     TRACE(("DELET
17798 45 3a 20 74 61 62 6c 65 3d 25 64 20 64 65 6c 65  E: table=%d dele
17799 74 65 20 69 6e 74 65 72 6e 61 6c 20 66 72 6f 6d  te internal from
1779a 20 25 64 20 72 65 70 6c 61 63 65 20 66 72 6f 6d   %d replace from
1779b 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20   leaf %d\n",.   
1779c 20 20 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f        pCur->pgno
1779d 52 6f 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e  Root, pPage->pgn
1779e 6f 2c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67  o, pLeafPage->pg
1779f 6e 6f 29 29 3b 0a 20 20 20 20 20 20 64 72 6f 70  no));.      drop
177a0 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c  Cell(pPage, idx,
177a1 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
177a2 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20  ge, pCell));.   
177a3 20 20 20 70 4e 65 78 74 20 3d 20 66 69 6e 64 43     pNext = findC
177a4 65 6c 6c 28 70 4c 65 61 66 50 61 67 65 2c 20 30  ell(pLeafPage, 0
177a5 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 78 74 20  );.      szNext 
177a6 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c  = cellSizePtr(pL
177a7 65 61 66 50 61 67 65 2c 20 70 4e 65 78 74 29 3b  eafPage, pNext);
177a8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 4d  .      assert( M
177a9 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
177aa 3e 3d 73 7a 4e 65 78 74 2b 34 20 29 3b 0a 20 20  >=szNext+4 );.  
177ab 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70      allocateTemp
177ac 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20  Space(pBt);.    
177ad 20 20 74 65 6d 70 43 65 6c 6c 20 3d 20 70 42 74    tempCell = pBt
177ae 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20  ->pTmpSpace;.   
177af 20 20 20 69 66 28 20 74 65 6d 70 43 65 6c 6c 3d     if( tempCell=
177b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
177b1 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
177b2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
177b3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
177b4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
177b5 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67   insertCell(pPag
177b6 65 2c 20 69 64 78 2c 20 70 4e 65 78 74 2d 34 2c  e, idx, pNext-4,
177b7 20 73 7a 4e 65 78 74 2b 34 2c 20 74 65 6d 70 43   szNext+4, tempC
177b8 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d  ell, 0);.      }
177b9 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ...      /* The 
177ba 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69  "if" statement i
177bb 6e 20 74 68 65 20 6e 65 78 74 20 63 6f 64 65 20  n the next code 
177bc 62 6c 6f 63 6b 20 69 73 20 63 72 69 74 69 63 61  block is critica
177bd 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a  l.  The.      **
177be 20 73 6c 69 67 68 74 65 73 74 20 65 72 72 6f 72   slightest error
177bf 20 69 6e 20 74 68 61 74 20 73 74 61 74 65 6d 65   in that stateme
177c0 6e 74 20 77 6f 75 6c 64 20 61 6c 6c 6f 77 20 53  nt would allow S
177c1 51 4c 69 74 65 20 74 6f 20 6f 70 65 72 61 74 65  QLite to operate
177c2 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65 63  .      ** correc
177c3 74 6c 79 20 6d 6f 73 74 20 6f 66 20 74 68 65 20  tly most of the 
177c4 74 69 6d 65 20 62 75 74 20 70 72 6f 64 75 63 65  time but produce
177c5 20 76 65 72 79 20 72 61 72 65 20 66 61 69 6c 75   very rare failu
177c6 72 65 73 2e 20 20 54 6f 0a 20 20 20 20 20 20 2a  res.  To.      *
177c7 2a 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20  * guard against 
177c8 74 68 69 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  this, the follow
177c9 69 6e 67 20 6d 61 63 72 6f 73 20 68 65 6c 70 20  ing macros help 
177ca 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 0a 20  to verify that. 
177cb 20 20 20 20 20 2a 2a 20 74 68 65 20 22 69 66 22       ** the "if"
177cc 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 77 65   statement is we
177cd 6c 6c 20 74 65 73 74 65 64 2e 0a 20 20 20 20 20  ll tested..     
177ce 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
177cf 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  se( pPage->nOver
177d0 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65  flow==0 && pPage
177d1 2d 3e 6e 46 72 65 65 3c 70 42 74 2d 3e 75 73 61  ->nFree<pBt->usa
177d2 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20 20  bleSize*2/3 .   
177d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26                &&
177d4 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65   pLeafPage->nFre
177d5 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42 74  e+2+szNext > pBt
177d6 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33  ->usableSize*2/3
177d7 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
177d8 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  se( pPage->nOver
177d9 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65  flow==0 && pPage
177da 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75 73  ->nFree==pBt->us
177db 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 0a 20 20  ableSize*2/3 .  
177dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
177dd 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72  & pLeafPage->nFr
177de 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70 42  ee+2+szNext > pB
177df 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f  t->usableSize*2/
177e0 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  3 );.      testc
177e1 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ase( pPage->nOve
177e2 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67  rflow==0 && pPag
177e3 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74 2d 3e 75  e->nFree==pBt->u
177e4 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20  sableSize*2/3+1 
177e5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
177e6 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e    && pLeafPage->
177e7 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e  nFree+2+szNext >
177e8 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
177e9 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65  *2/3 );.      te
177ea 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e  stcase( pPage->n
177eb 4f 76 65 72 66 6c 6f 77 3e 30 20 26 26 20 70 50  Overflow>0 && pP
177ec 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 70 42 74 2d  age->nFree<=pBt-
177ed 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 0a  >usableSize*2/3.
177ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
177ef 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e 6e   && pLeafPage->n
177f0 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20  Free+2+szNext > 
177f1 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a  pBt->usableSize*
177f2 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65 73  2/3 );.      tes
177f3 74 63 61 73 65 28 20 28 70 50 61 67 65 2d 3e 6e  tcase( (pPage->n
177f4 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28 70  Overflow>0 || (p
177f5 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70 42  Page->nFree > pB
177f6 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f  t->usableSize*2/
177f7 33 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  3)).            
177f8 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 67       && pLeafPag
177f9 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78  e->nFree+2+szNex
177fa 74 20 3d 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  t == pBt->usable
177fb 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 0a 0a 20 20  Size*2/3 );...  
177fc 20 20 20 20 69 66 28 20 28 70 50 61 67 65 2d 3e      if( (pPage->
177fd 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 28  nOverflow>0 || (
177fe 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3e 20 70  pPage->nFree > p
177ff 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
17800 2f 33 29 29 20 26 26 0a 20 20 20 20 20 20 20 20  /3)) &&.        
17801 20 20 28 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46    (pLeafPage->nF
17802 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e 20 70  ree+2+szNext > p
17803 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32  Bt->usableSize*2
17804 2f 33 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  /3).      ){.   
17805 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
17806 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 69 66 20  nch is taken if 
17807 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  the internal nod
17808 65 20 69 73 20 6e 6f 77 20 65 69 74 68 65 72 20  e is now either 
17809 6f 76 65 72 66 6c 6f 77 69 6e 67 0a 20 20 20 20  overflowing.    
1780a 20 20 20 20 2a 2a 20 6f 72 20 75 6e 64 65 72 66      ** or underf
1780b 75 6c 6c 20 61 6e 64 20 74 68 65 20 6c 65 61 66  ull and the leaf
1780c 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65 20 75 6e   node will be un
1780d 64 65 72 66 75 6c 6c 20 61 66 74 65 72 20 74 68  derfull after th
1780e 65 20 6a 75 73 74 20 63 65 6c 6c 20 0a 20 20 20  e just cell .   
1780f 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 74       ** copied t
17810 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e  o the internal n
17811 6f 64 65 20 69 73 20 64 65 6c 65 74 65 64 20 66  ode is deleted f
17812 72 6f 6d 20 69 74 2e 20 54 68 69 73 20 69 73 20  rom it. This is 
17813 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20  a special.      
17814 20 20 2a 2a 20 63 61 73 65 20 62 65 63 61 75 73    ** case becaus
17815 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 61  e the call to ba
17816 6c 61 6e 63 65 28 29 20 74 6f 20 63 6f 72 72 65  lance() to corre
17817 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ct the internal 
17818 6e 6f 64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  node.        ** 
17819 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 74  may change the t
1781a 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 6e  ree structure an
1781b 64 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65  d invalidate the
1781c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 20 20 20   contents of.   
1781d 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 66       ** the leaf
1781e 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64  Cur.apPage[] and
1781f 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 5d   leafCur.aiIdx[]
17820 20 61 72 72 61 79 73 2c 20 77 68 69 63 68 20 77   arrays, which w
17821 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a  ill be.        *
17822 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 62 61  * used by the ba
17823 6c 61 6e 63 65 28 29 20 72 65 71 75 69 72 65 64  lance() required
17824 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 65 20   to correct the 
17825 75 6e 64 65 72 66 75 6c 6c 20 6c 65 61 66 0a 20  underfull leaf. 
17826 20 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 2e 0a         ** node..
17827 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
17828 20 20 20 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c     ** The formul
17829 61 20 75 73 65 64 20 69 6e 20 74 68 65 20 65 78  a used in the ex
1782a 70 72 65 73 73 69 6f 6e 20 61 62 6f 76 65 20 61  pression above a
1782b 72 65 20 62 61 73 65 64 20 6f 6e 20 66 61 63 65  re based on face
1782c 74 73 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a  ts of.        **
1782d 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65   the SQLite file
1782e 2d 66 6f 72 6d 61 74 20 74 68 61 74 20 64 6f 20  -format that do 
1782f 6e 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72 20  not change over 
17830 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  time..        */
17831 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
17832 65 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3d  e( pPage->nFree=
17833 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  =pBt->usableSize
17834 2a 32 2f 33 2b 31 20 29 3b 0a 20 20 20 20 20 20  *2/3+1 );.      
17835 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 61    testcase( pLea
17836 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73  fPage->nFree+2+s
17837 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e 75 73 61 62  zNext==pBt->usab
17838 6c 65 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a  leSize*2/3+1 );.
17839 20 20 20 20 20 20 20 20 6c 65 61 66 43 75 72 73          leafCurs
1783a 6f 72 49 6e 76 61 6c 69 64 20 3d 20 31 3b 0a 20  orInvalid = 1;. 
1783b 20 20 20 20 20 7d 20 20 20 20 20 20 20 20 0a 0a       }        ..
1783c 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1783d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1783e 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
1783f 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
17840 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
17841 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 70 75  e) );.        pu
17842 74 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66  t4byte(findOverf
17843 6c 6f 77 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  lowCell(pPage, i
17844 64 78 29 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b  dx), pgnoChild);
17845 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c  .        VVA_ONL
17846 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68  Y( pCur->pagesSh
17847 75 66 66 6c 65 64 20 3d 20 30 20 29 3b 0a 20 20  uffled = 0 );.  
17848 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e        rc = balan
17849 63 65 28 70 43 75 72 2c 20 30 29 3b 0a 20 20 20  ce(pCur, 0);.   
1784a 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
1784b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1784c 20 6c 65 61 66 43 75 72 73 6f 72 49 6e 76 61 6c   leafCursorInval
1784d 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  id ){.        /*
1784e 20 54 68 65 20 6c 65 61 66 2d 6e 6f 64 65 20 69   The leaf-node i
1784f 73 20 6e 6f 77 20 75 6e 64 65 72 66 75 6c 6c 20  s now underfull 
17850 61 6e 64 20 73 6f 20 74 68 65 20 74 72 65 65 20  and so the tree 
17851 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20 20  needs to be .   
17852 20 20 20 20 20 2a 2a 20 72 65 62 61 6c 61 6e 63       ** rebalanc
17853 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68 65  ed. However, the
17854 20 62 61 6c 61 6e 63 65 28 29 20 6f 70 65 72 61   balance() opera
17855 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e 74 65  tion on the inte
17856 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20  rnal.        ** 
17857 6e 6f 64 65 20 61 62 6f 76 65 20 6d 61 79 20 68  node above may h
17858 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74 68 65  ave modified the
17859 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 68   structure of th
1785a 65 20 42 2d 54 72 65 65 20 61 6e 64 0a 20 20 20  e B-Tree and.   
1785b 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 65 20 63       ** so the c
1785c 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20  urrent contents 
1785d 6f 66 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67  of leafCur.apPag
1785e 65 5b 5d 20 61 6e 64 20 6c 65 61 66 43 75 72 2e  e[] and leafCur.
1785f 61 69 49 64 78 5b 5d 0a 20 20 20 20 20 20 20 20  aiIdx[].        
17860 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 72  ** may not be tr
17861 75 73 74 65 64 2e 0a 20 20 20 20 20 20 20 20 2a  usted..        *
17862 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20  *.        ** It 
17863 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
17864 74 6f 20 63 6f 70 79 20 74 68 65 20 61 6e 63 65  to copy the ance
17865 73 74 72 79 20 66 72 6f 6d 20 70 43 75 72 2c 20  stry from pCur, 
17866 61 73 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20  as the same.    
17867 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 28 29      ** balance()
17868 20 63 61 6c 6c 20 68 61 73 20 69 6e 76 61 6c 69   call has invali
17869 64 61 74 65 64 20 74 68 65 20 70 43 75 72 2d 3e  dated the pCur->
1786a 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 61 69 49  apPage[] and aiI
1786b 64 78 5b 5d 0a 20 20 20 20 20 20 20 20 2a 2a 20  dx[].        ** 
1786c 61 72 72 61 79 73 2e 20 0a 20 20 20 20 20 20 20  arrays. .       
1786d 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54   **.        ** T
1786e 68 65 20 63 61 6c 6c 20 74 6f 20 73 61 76 65 43  he call to saveC
1786f 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20  ursorPosition() 
17870 62 65 6c 6f 77 20 69 6e 74 65 72 6e 61 6c 6c 79  below internally
17871 20 73 61 76 65 73 20 74 68 65 20 0a 20 20 20 20   saves the .    
17872 20 20 20 20 2a 2a 20 6b 65 79 20 74 68 61 74 20      ** key that 
17873 6c 65 61 66 43 75 72 20 69 73 20 63 75 72 72 65  leafCur is curre
17874 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
17875 2e 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65  . Currently, the
17876 72 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 72  re.        ** ar
17877 65 20 74 77 6f 20 63 6f 70 69 65 73 20 6f 66 20  e two copies of 
17878 74 68 61 74 20 6b 65 79 20 69 6e 20 74 68 65 20  that key in the 
17879 74 72 65 65 20 2d 20 6f 6e 65 20 68 65 72 65 20  tree - one here 
1787a 6f 6e 20 74 68 65 20 6c 65 61 66 0a 20 20 20 20  on the leaf.    
1787b 20 20 20 20 2a 2a 20 70 61 67 65 20 61 6e 64 20      ** page and 
1787c 6f 6e 65 20 6f 6e 20 73 6f 6d 65 20 69 6e 74 65  one on some inte
1787d 72 6e 61 6c 20 6e 6f 64 65 20 69 6e 20 74 68 65  rnal node in the
1787e 20 74 72 65 65 2e 20 54 68 65 20 63 6f 70 79 20   tree. The copy 
1787f 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  on.        ** th
17880 65 20 6c 65 61 66 20 6e 6f 64 65 20 69 73 20 61  e leaf node is a
17881 6c 77 61 79 73 20 74 68 65 20 6e 65 78 74 20 6b  lways the next k
17882 65 79 20 69 6e 20 74 72 65 65 2d 6f 72 64 65 72  ey in tree-order
17883 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 20 20   after the .    
17884 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 6e 20 74      ** copy on t
17885 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  he internal node
17886 2e 20 53 6f 2c 20 74 68 65 20 63 61 6c 6c 20 74  . So, the call t
17887 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  o sqlite3BtreeNe
17888 78 74 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20  xt().        ** 
17889 63 61 6c 6c 73 20 72 65 73 74 6f 72 65 43 75 72  calls restoreCur
1788a 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 74 6f  sorPosition() to
1788b 20 70 6f 69 6e 74 20 74 68 65 20 63 75 72 73 6f   point the curso
1788c 72 20 74 6f 20 74 68 65 20 63 6f 70 79 0a 20 20  r to the copy.  
1788d 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20        ** stored 
1788e 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  on the internal 
1788f 6e 6f 64 65 2c 20 74 68 65 6e 20 61 64 76 61 6e  node, then advan
17890 63 65 73 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ces to the next 
17891 65 6e 74 72 79 2c 0a 20 20 20 20 20 20 20 20 2a  entry,.        *
17892 2a 20 77 68 69 63 68 20 68 61 70 70 65 6e 73 20  * which happens 
17893 74 6f 20 62 65 20 74 68 65 20 63 6f 70 79 20 6f  to be the copy o
17894 66 20 74 68 65 20 6b 65 79 20 6f 6e 20 74 68 65  f the key on the
17895 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 0a   internal node..
17896 20 20 20 20 20 20 20 20 2a 2a 20 4e 65 74 20 65          ** Net e
17897 66 66 65 63 74 3a 20 6c 65 61 66 43 75 72 20 69  ffect: leafCur i
17898 73 20 70 6f 69 6e 74 69 6e 67 20 62 61 63 6b 20  s pointing back 
17899 74 6f 20 74 68 65 20 64 75 70 6c 69 63 61 74 65  to the duplicate
1789a 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a   cell.        **
1789b 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62   that needs to b
1789c 65 20 72 65 6d 6f 76 65 64 2c 20 61 6e 64 20 74  e removed, and t
1789d 68 65 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67  he leafCur.apPag
1789e 65 5b 5d 20 61 6e 64 0a 20 20 20 20 20 20 20 20  e[] and.        
1789f 2a 2a 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78  ** leafCur.aiIdx
178a0 5b 5d 20 61 72 72 61 79 73 20 61 72 65 20 63 6f  [] arrays are co
178a1 72 72 65 63 74 2e 0a 20 20 20 20 20 20 20 20 2a  rrect..        *
178a2 2f 0a 20 20 20 20 20 20 20 20 56 56 41 5f 4f 4e  /.        VVA_ON
178a3 4c 59 28 20 50 67 6e 6f 20 6c 65 61 66 50 67 6e  LY( Pgno leafPgn
178a4 6f 20 3d 20 70 4c 65 61 66 50 61 67 65 2d 3e 70  o = pLeafPage->p
178a5 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 20 72  gno );.        r
178a6 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50 6f  c = saveCursorPo
178a7 73 69 74 69 6f 6e 28 26 6c 65 61 66 43 75 72 29  sition(&leafCur)
178a8 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
178a9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
178aa 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
178ab 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 26  lite3BtreeNext(&
178ac 6c 65 61 66 43 75 72 2c 20 26 6e 6f 74 55 73 65  leafCur, &notUse
178ad 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  d);.        }.  
178ae 20 20 20 20 20 20 70 4c 65 61 66 50 61 67 65 20        pLeafPage 
178af 3d 20 6c 65 61 66 43 75 72 2e 61 70 50 61 67 65  = leafCur.apPage
178b0 5b 6c 65 61 66 43 75 72 2e 69 50 61 67 65 5d 3b  [leafCur.iPage];
178b1 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
178b2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
178b3 7c 20 70 4c 65 61 66 50 61 67 65 2d 3e 70 67 6e  | pLeafPage->pgn
178b4 6f 3d 3d 6c 65 61 66 50 67 6e 6f 20 29 3b 0a 20  o==leafPgno );. 
178b5 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
178b6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
178b7 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 6c 65  leafCur.aiIdx[le
178b8 61 66 43 75 72 2e 69 50 61 67 65 5d 3d 3d 30 20  afCur.iPage]==0 
178b9 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
178ba 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
178bb 3d 72 63 0a 20 20 20 20 20 20 20 26 26 20 53 51  =rc.       && SQ
178bc 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
178bd 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
178be 28 70 4c 65 61 66 50 61 67 65 2d 3e 70 44 62 50  (pLeafPage->pDbP
178bf 61 67 65 29 29 20 0a 20 20 20 20 20 20 29 7b 0a  age)) .      ){.
178c0 20 20 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c          dropCell
178c1 28 70 4c 65 61 66 50 61 67 65 2c 20 30 2c 20 73  (pLeafPage, 0, s
178c2 7a 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20  zNext);.        
178c3 56 56 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 43 75  VVA_ONLY( leafCu
178c4 72 2e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20  r.pagesShuffled 
178c5 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72  = 0 );.        r
178c6 63 20 3d 20 62 61 6c 61 6e 63 65 28 26 6c 65 61  c = balance(&lea
178c7 66 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  fCur, 0);.      
178c8 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 75    assert( leafCu
178c9 72 73 6f 72 49 6e 76 61 6c 69 64 20 7c 7c 20 21  rsorInvalid || !
178ca 6c 65 61 66 43 75 72 2e 70 61 67 65 73 53 68 75  leafCur.pagesShu
178cb 66 66 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20  ffled.          
178cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
178cd 20 20 20 20 20 20 20 20 20 7c 7c 20 21 70 43 75           || !pCu
178ce 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64  r->pagesShuffled
178cf 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
178d0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  }.    sqlite3Btr
178d1 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72  eeReleaseTempCur
178d2 73 6f 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20  sor(&leafCur);. 
178d3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43   }else{.    TRAC
178d4 45 28 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c  E(("DELETE: tabl
178d5 65 3d 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d  e=%d delete from
178d6 20 6c 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20   leaf %d\n",.   
178d7 20 20 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f      pCur->pgnoRo
178d8 6f 74 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29  ot, pPage->pgno)
178d9 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 72 6f 70  );.    rc = drop
178da 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c  Cell(pPage, idx,
178db 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
178dc 67 65 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20  ge, pCell));.   
178dd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
178de 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
178df 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 30   balance(pCur, 0
178e0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  );.    }.  }.  i
178e1 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
178e2 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f   ){.    moveToRo
178e3 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20  ot(pCur);.  }.  
178e4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
178e5 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
178e6 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57   BTree table.  W
178e7 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62  rite into *piTab
178e8 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e  le the page.** n
178e9 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f  umber for the ro
178ea 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e  ot page of the n
178eb 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ew table..**.** 
178ec 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65  The type of type
178ed 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
178ee 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61  y the flags para
178ef 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65  meter.  Only the
178f0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  .** following va
178f1 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72  lues of flags ar
178f2 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  e currently in u
178f3 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65  se.  Other value
178f4 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d  s for.** flags m
178f5 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a  ight not work:.*
178f6 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49  *.**     BTREE_I
178f7 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46  NTKEY|BTREE_LEAF
178f8 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f  DATA     Used fo
178f9 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74  r SQL tables wit
178fa 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20  h rowid keys.** 
178fb 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41      BTREE_ZERODA
178fc 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TA              
178fd 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c      Used for SQL
178fe 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74   indices.*/.stat
178ff 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61  ic int btreeCrea
17900 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  teTable(Btree *p
17901 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20  , int *piTable, 
17902 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74  int flags){.  Bt
17903 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
17904 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20  >pBt;.  MemPage 
17905 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70  *pRoot;.  Pgno p
17906 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72  gnoRoot;.  int r
17907 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  c;..  assert( sq
17908 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
17909 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73  utex(p) );.  ass
1790a 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
1790b 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
1790c 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74  RITE );.  assert
1790d 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  ( !pBt->readOnly
1790e 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   );..#ifdef SQLI
1790f 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
17910 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  UM.  rc = alloca
17911 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
17912 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f   &pRoot, &pgnoRo
17913 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28  ot, 1, 0);.  if(
17914 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72   rc ){.    retur
17915 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  n rc;.  }.#else.
17916 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
17917 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e  acuum ){.    Pgn
17918 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20  o pgnoMove;     
17919 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20   /* Move a page 
1791a 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f  here to make roo
1791b 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70  m for the root-p
1791c 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61  age */.    MemPa
1791d 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f  ge *pPageMove; /
1791e 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f  * The page to mo
1791f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f  ve to. */..    /
17920 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77  * Creating a new
17921 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61   table may proba
17922 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69  bly require movi
17923 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64  ng an existing d
17924 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74  atabase.    ** t
17925 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20  o make room for 
17926 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72  the new tables r
17927 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73  oot page. In cas
17928 65 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e  e this page turn
17929 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20  s.    ** out to 
1792a 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  be an overflow p
1792b 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20  age, delete all 
1792c 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61  overflow page-ma
1792d 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20  p caches.    ** 
1792e 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72  held by open cur
1792f 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  sors..    */.   
17930 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76   invalidateAllOv
17931 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29  erflowCache(pBt)
17932 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  ;..    /* Read t
17933 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61  he value of meta
17934 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74  [3] from the dat
17935 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69  abase to determi
17936 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20  ne where the.   
17937 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66   ** root page of
17938 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73   the new table s
17939 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33  hould go. meta[3
1793a 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74  ] is the largest
1793b 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a   root-page.    *
1793c 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72  * created so far
1793d 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f  , so the new roo
1793e 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b  t-page is (meta[
1793f 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  3]+1)..    */.  
17940 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
17941 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c  reeGetMeta(p, 4,
17942 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20   &pgnoRoot);.   
17943 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17944 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
17945 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
17946 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20   pgnoRoot++;..  
17947 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f    /* The new roo
17948 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62  t-page may not b
17949 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61  e allocated on a
1794a 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
1794b 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a  e, or the.    **
1794c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61   PENDING_BYTE pa
1794d 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77  ge..    */.    w
1794e 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d  hile( pgnoRoot==
1794f 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42  PTRMAP_PAGENO(pB
17950 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a  t, pgnoRoot) ||.
17951 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74          pgnoRoot
17952 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
17953 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
17954 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20    pgnoRoot++;.  
17955 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
17956 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a  pgnoRoot>=3 );..
17957 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20      /* Allocate 
17958 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65  a page. The page
17959 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20   that currently 
1795a 72 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52  resides at pgnoR
1795b 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20  oot will.    ** 
1795c 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20  be moved to the 
1795d 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28  allocated page (
1795e 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63  unless the alloc
1795f 61 74 65 64 20 70 61 67 65 20 68 61 70 70 65 6e  ated page happen
17960 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69  s.    ** to resi
17961 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e  de at pgnoRoot).
17962 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
17963 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
17964 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f  ge(pBt, &pPageMo
17965 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70  ve, &pgnoMove, p
17966 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20  gnoRoot, 1);.   
17967 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17968 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
17969 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20  rn rc;.    }..  
1796a 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d    if( pgnoMove!=
1796b 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20  pgnoRoot ){.    
1796c 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73    /* pgnoRoot is
1796d 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 77   the page that w
1796e 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20  ill be used for 
1796f 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66  the root-page of
17970 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65  .      ** the ne
17971 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e  w table (assumin
17972 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e  g an error did n
17973 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77  ot occur). But w
17974 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20  e were.      ** 
17975 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f  allocated pgnoMo
17976 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 20  ve. If required 
17977 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20  (i.e. if it was 
17978 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20  not allocated.  
17979 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64      ** by extend
1797a 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74  ing the file), t
1797b 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20  he current page 
1797c 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f  at position pgno
1797d 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73  Move.      ** is
1797e 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c   already journal
1797f 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
17980 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20     u8 eType;.   
17981 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65     Pgno iPtrPage
17982 3b 0a 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ;..      release
17983 50 61 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b  Page(pPageMove);
17984 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20  ..      /* Move 
17985 74 68 65 20 70 61 67 65 20 63 75 72 72 65 6e 74  the page current
17986 6c 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74  ly at pgnoRoot t
17987 6f 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20  o pgnoMove. */. 
17988 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
17989 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
1798a 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52  t, pgnoRoot, &pR
1798b 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  oot, 0);.      i
1798c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1798d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
1798e 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
1798f 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
17990 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f  Get(pBt, pgnoRoo
17991 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 72  t, &eType, &iPtr
17992 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
17993 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
17994 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  | eType==PTRMAP_
17995 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70  ROOTPAGE || eTyp
17996 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41  e==PTRMAP_FREEPA
17997 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  GE ){.        re
17998 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
17999 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1799a 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
1799b 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65     assert( eType
1799c 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  !=PTRMAP_ROOTPAG
1799d 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  E );.      asser
1799e 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50  t( eType!=PTRMAP
1799f 5f 46 52 45 45 50 41 47 45 20 29 3b 0a 20 20 20  _FREEPAGE );.   
179a0 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65     rc = relocate
179a1 50 61 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 2c  Page(pBt, pRoot,
179a2 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65   eType, iPtrPage
179a3 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b 0a  , pgnoMove, 0);.
179a4 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
179a5 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20  e(pRoot);..     
179a6 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70   /* Obtain the p
179a7 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20  age at pgnoRoot 
179a8 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  */.      if( rc!
179a9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
179aa 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
179ab 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
179ac 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
179ad 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e  GetPage(pBt, pgn
179ae 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30  oRoot, &pRoot, 0
179af 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
179b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
179b1 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
179b2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
179b3 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
179b4 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62  Write(pRoot->pDb
179b5 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
179b6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
179b7 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  {.        releas
179b8 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20  ePage(pRoot);.  
179b9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
179ba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
179bb 73 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20  se{.      pRoot 
179bc 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20  = pPageMove;.   
179bd 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61   } ..    /* Upda
179be 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  te the pointer-m
179bf 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61  ap and meta-data
179c0 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f   with the new ro
179c1 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20  ot-page number. 
179c2 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d  */.    rc = ptrm
179c3 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52  apPut(pBt, pgnoR
179c4 6f 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54  oot, PTRMAP_ROOT
179c5 50 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66  PAGE, 0);.    if
179c6 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
179c7 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
179c8 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
179c9 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  c;.    }.    rc 
179ca 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70  = sqlite3BtreeUp
179cb 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70  dateMeta(p, 4, p
179cc 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66  gnoRoot);.    if
179cd 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
179ce 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29  leasePage(pRoot)
179cf 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
179d0 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73  c;.    }..  }els
179d1 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f  e{.    rc = allo
179d2 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
179d3 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f  t, &pRoot, &pgno
179d4 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20  Root, 1, 0);.   
179d5 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
179d6 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a   rc;.  }.#endif.
179d7 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
179d8 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
179d9 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65  e(pRoot->pDbPage
179da 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28  ) );.  zeroPage(
179db 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50  pRoot, flags | P
179dc 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69  TF_LEAF);.  sqli
179dd 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52  te3PagerUnref(pR
179de 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  oot->pDbPage);. 
179df 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74   *piTable = (int
179e0 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74  )pgnoRoot;.  ret
179e1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
179e2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
179e3 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
179e4 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65  CreateTable(Btre
179e5 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62  e *p, int *piTab
179e6 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  le, int flags){.
179e7 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
179e8 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
179e9 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72  ;.  rc = btreeCr
179ea 65 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54  eateTable(p, piT
179eb 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20  able, flags);.  
179ec 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
179ed 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
179ee 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73  c;.}../*.** Eras
179ef 65 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61  e the given data
179f0 62 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c  base page and al
179f1 6c 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20  l its children. 
179f2 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70   Return.** the p
179f3 61 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c  age to the freel
179f4 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ist..*/.static i
179f5 6e 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65  nt clearDatabase
179f6 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64  Page(.  BtShared
179f7 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20   *pBt,          
179f8 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68   /* The BTree th
179f9 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
179fa 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  table */.  Pgno 
179fb 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  pgno,           
179fc 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
179fd 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 6e  to clear */.  in
179fe 74 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c 20  t freePageFlag, 
179ff 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74      /* Deallocat
17a00 65 20 70 61 67 65 20 69 66 20 74 72 75 65 20 2a  e page if true *
17a01 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67  /.  int *pnChang
17a02 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  e.){.  MemPage *
17a03 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74  pPage = 0;.  int
17a04 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20   rc;.  unsigned 
17a05 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69  char *pCell;.  i
17a06 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28  nt i;..  assert(
17a07 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
17a08 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
17a09 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e 70 61  );.  if( pgno>pa
17a0a 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
17a0b 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
17a0c 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
17a0d 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d  KPT;.  }..  rc =
17a0e 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
17a0f 70 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67  pBt, pgno, &pPag
17a10 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67  e);.  if( rc ) g
17a11 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73  oto cleardatabas
17a12 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 66 6f 72  epage_out;.  for
17a13 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e  (i=0; i<pPage->n
17a14 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Cell; i++){.    
17a15 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
17a16 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20  (pPage, i);.    
17a17 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
17a18 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63   ){.      rc = c
17a19 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65  learDatabasePage
17a1a 28 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70  (pBt, get4byte(p
17a1b 43 65 6c 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e  Cell), 1, pnChan
17a1c 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
17a1d 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61  c ) goto clearda
17a1e 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a  tabasepage_out;.
17a1f 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63      }.    rc = c
17a20 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20  learCell(pPage, 
17a21 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  pCell);.    if( 
17a22 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64  rc ) goto cleard
17a23 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b  atabasepage_out;
17a24 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67  .  }.  if( !pPag
17a25 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72  e->leaf ){.    r
17a26 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73  c = clearDatabas
17a27 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62  ePage(pBt, get4b
17a28 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
17a29 61 5b 38 5d 29 2c 20 31 2c 20 70 6e 43 68 61 6e  a[8]), 1, pnChan
17a2a 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ge);.    if( rc 
17a2b 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61  ) goto cleardata
17a2c 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  basepage_out;.  
17a2d 7d 65 6c 73 65 20 69 66 28 20 70 6e 43 68 61 6e  }else if( pnChan
17a2e 67 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ge ){.    assert
17a2f 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
17a30 29 3b 0a 20 20 20 20 2a 70 6e 43 68 61 6e 67 65  );.    *pnChange
17a31 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c   += pPage->nCell
17a32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 72 65 65  ;.  }.  if( free
17a33 50 61 67 65 46 6c 61 67 20 29 7b 0a 20 20 20 20  PageFlag ){.    
17a34 72 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50  rc = freePage(pP
17a35 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  age);.  }else if
17a36 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50  ( (rc = sqlite3P
17a37 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
17a38 3e 70 44 62 50 61 67 65 29 29 3d 3d 30 20 29 7b  >pDbPage))==0 ){
17a39 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50  .    zeroPage(pP
17a3a 61 67 65 2c 20 70 50 61 67 65 2d 3e 61 44 61 74  age, pPage->aDat
17a3b 61 5b 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29  a[0] | PTF_LEAF)
17a3c 3b 0a 20 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61  ;.  }..cleardata
17a3d 62 61 73 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20  basepage_out:.  
17a3e 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
17a3f 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
17a40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
17a41 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   all information
17a42 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74   from a single t
17a43 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61  able in the data
17a44 62 61 73 65 2e 20 20 69 54 61 62 6c 65 20 69 73  base.  iTable is
17a45 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d  .** the page num
17a46 62 65 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 20  ber of the root 
17a47 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 41  of the table.  A
17a48 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  fter this routin
17a49 65 20 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68  e returns,.** th
17a4a 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65  e root page is e
17a4b 6d 70 74 79 2c 20 62 75 74 20 73 74 69 6c 6c 20  mpty, but still 
17a4c 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  exists..**.** Th
17a4d 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
17a4e 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45  fail with SQLITE
17a4f 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65  _LOCKED if there
17a50 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a   are any open.**
17a51 20 72 65 61 64 20 63 75 72 73 6f 72 73 20 6f 6e   read cursors on
17a52 20 74 68 65 20 74 61 62 6c 65 2e 20 20 4f 70 65   the table.  Ope
17a53 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72 73 20  n write cursors 
17a54 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65  are moved to the
17a55 0a 2a 2a 20 72 6f 6f 74 20 6f 66 20 74 68 65 20  .** root of the 
17a56 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  table..**.** If 
17a57 70 6e 43 68 61 6e 67 65 20 69 73 20 6e 6f 74 20  pnChange is not 
17a58 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65  NULL, then table
17a59 20 69 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20   iTable must be 
17a5a 61 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e  an intkey table.
17a5b 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20   The.** integer 
17a5c 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f  value pointed to
17a5d 20 62 79 20 70 6e 43 68 61 6e 67 65 20 69 73 20   by pnChange is 
17a5e 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74  incremented by t
17a5f 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
17a60 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 74  entries in the t
17a61 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
17a62 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17a63 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62  te3BtreeClearTab
17a64 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  le(Btree *p, int
17a65 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e   iTable, int *pn
17a66 43 68 61 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72  Change){.  int r
17a67 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  c;.  BtShared *p
17a68 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73  Bt = p->pBt;.  s
17a69 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
17a6a 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
17a6b 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
17a6c 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20  _WRITE );.  if( 
17a6d 28 72 63 20 3d 20 63 68 65 63 6b 46 6f 72 52 65  (rc = checkForRe
17a6e 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69  adConflicts(p, i
17a6f 54 61 62 6c 65 2c 20 30 2c 20 31 29 29 21 3d 53  Table, 0, 1))!=S
17a70 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17a71 2f 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f  /* nothing to do
17a72 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
17a73 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
17a74 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28   saveAllCursors(
17a75 70 42 74 2c 20 69 54 61 62 6c 65 2c 20 30 29 29  pBt, iTable, 0))
17a76 20 29 7b 0a 20 20 20 20 2f 2a 20 6e 6f 74 68 69   ){.    /* nothi
17a77 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65  ng to do */.  }e
17a78 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c  lse{.    rc = cl
17a79 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28  earDatabasePage(
17a7a 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c  pBt, (Pgno)iTabl
17a7b 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65 29 3b  e, 0, pnChange);
17a7c 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
17a7d 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
17a7e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
17a7f 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 6e 66  ** Erase all inf
17a80 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 74 61  ormation in a ta
17a81 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68 65 20  ble and add the 
17a82 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c  root of the tabl
17a83 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72 65 65  e to.** the free
17a84 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20 74  list.  Except, t
17a85 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 70  he root of the p
17a86 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 20 28  rinciple table (
17a87 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61  the one on.** pa
17a88 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 20 61  ge 1) is never a
17a89 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 65 65  dded to the free
17a8a 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  list..**.** This
17a8b 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61   routine will fa
17a8c 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c  il with SQLITE_L
17a8d 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61  OCKED if there a
17a8e 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63  re any open.** c
17a8f 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 61  ursors on the ta
17a90 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55  ble..**.** If AU
17a91 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62  TOVACUUM is enab
17a92 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65  led and the page
17a93 20 61 74 20 69 54 61 62 6c 65 20 69 73 20 6e 6f   at iTable is no
17a94 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 72 6f  t the last.** ro
17a95 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ot page in the d
17a96 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
17a97 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74  en the last root
17a98 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74 68 65   page .** in the
17a99 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
17a9a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65  s moved into the
17a9b 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f   slot formerly o
17a9c 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20 69 54  ccupied by.** iT
17a9d 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20 6c 61  able and that la
17a9e 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79  st slot formerly
17a9f 20 6f 63 63 75 70 69 65 64 20 62 79 20 74 68 65   occupied by the
17aa0 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 0a   last root page.
17aa1 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74  ** is added to t
17aa2 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e 73 74  he freelist inst
17aa3 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e 20 20  ead of iTable.  
17aa4 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61 6c 6c  In this say, all
17aa5 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 61  .** root pages a
17aa6 72 65 20 6b 65 70 74 20 61 74 20 74 68 65 20 62  re kept at the b
17aa7 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20  eginning of the 
17aa8 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77  database file, w
17aa9 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63 65 73  hich.** is neces
17aaa 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56 41 43  sary for AUTOVAC
17aab 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69 67 68  UUM to work righ
17aac 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69 73 20  t.  *piMoved is 
17aad 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70  set to the .** p
17aae 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61 74 20  age number that 
17aaf 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c  used to be the l
17ab0 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69 6e  ast root page in
17ab1 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f 72 65   the file before
17ab2 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20 20 49  .** the move.  I
17ab3 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73 20 6d  f no page gets m
17ab4 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64 20 69  oved, *piMoved i
17ab5 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 54  s set to 0..** T
17ab6 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
17ab7 65 20 69 73 20 72 65 63 6f 72 64 65 64 20 69 6e  e is recorded in
17ab8 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 68 65   meta[3] and the
17ab9 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d 65 74   value of.** met
17aba 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 64 20  a[3] is updated 
17abb 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  by this procedur
17abc 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
17abd 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28   btreeDropTable(
17abe 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69  Btree *p, Pgno i
17abf 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f  Table, int *piMo
17ac0 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ved){.  int rc;.
17ac1 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
17ac2 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64   = 0;.  BtShared
17ac3 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
17ac4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17ac5 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
17ac6 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74  x(p) );.  assert
17ac7 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
17ac8 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a 20 20  ANS_WRITE );..  
17ac9 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c  /* It is illegal
17aca 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 6c 65   to drop a table
17acb 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 73 20   if any cursors 
17acc 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a  are open on the.
17acd 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54    ** database. T
17ace 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69  his is because i
17acf 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f  n auto-vacuum mo
17ad0 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 20 6d  de the backend m
17ad1 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20  ay.  ** need to 
17ad2 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 6f 6f  move another roo
17ad3 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c 20 61  t-page to fill a
17ad4 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65   gap left by the
17ad5 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72 6f   deleted.  ** ro
17ad6 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e 20 6f  ot page. If an o
17ad7 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 20 75  pen cursor was u
17ad8 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 61  sing this page a
17ad9 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 0a   problem would .
17ada 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a 2f    ** occur..  */
17adb 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 43 75 72  .  if( pBt->pCur
17adc 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  sor ){.    sqlit
17add 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63  e3ConnectionBloc
17ade 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e  ked(p->db, pBt->
17adf 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d  pCursor->pBtree-
17ae0 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  >db);.    return
17ae1 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53   SQLITE_LOCKED_S
17ae2 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a  HAREDCACHE;.  }.
17ae3 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
17ae4 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
17ae5 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 26   (Pgno)iTable, &
17ae6 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 28  pPage, 0);.  if(
17ae7 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
17ae8 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
17ae9 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 70  treeClearTable(p
17aea 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20  , iTable, 0);.  
17aeb 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
17aec 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
17aed 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
17aee 0a 20 20 7d 0a 0a 20 20 2a 70 69 4d 6f 76 65 64  .  }..  *piMoved
17aef 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 69 54 61   = 0;..  if( iTa
17af0 62 6c 65 3e 31 20 29 7b 0a 23 69 66 64 65 66 20  ble>1 ){.#ifdef 
17af1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
17af2 56 41 43 55 55 4d 0a 20 20 20 20 72 63 20 3d 20  VACUUM.    rc = 
17af3 66 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  freePage(pPage);
17af4 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
17af5 28 70 50 61 67 65 29 3b 0a 23 65 6c 73 65 0a 20  (pPage);.#else. 
17af6 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
17af7 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20  Vacuum ){.      
17af8 50 67 6e 6f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  Pgno maxRootPgno
17af9 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
17afa 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
17afb 28 70 2c 20 34 2c 20 26 6d 61 78 52 6f 6f 74 50  (p, 4, &maxRootP
17afc 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  gno);.      if( 
17afd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
17afe 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
17aff 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
17b00 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
17b01 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
17b02 66 28 20 69 54 61 62 6c 65 3d 3d 6d 61 78 52 6f  f( iTable==maxRo
17b03 6f 74 50 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20  otPgno ){.      
17b04 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c    /* If the tabl
17b05 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20  e being dropped 
17b06 69 73 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  is the table wit
17b07 68 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f  h the largest ro
17b08 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20  ot-page.        
17b09 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65  ** number in the
17b0a 20 64 61 74 61 62 61 73 65 2c 20 70 75 74 20 74   database, put t
17b0b 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 6e 20  he root page on 
17b0c 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 20 0a  the free list. .
17b0d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
17b0e 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65     rc = freePage
17b0f 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
17b10 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
17b11 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ge);.        if(
17b12 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
17b13 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
17b14 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
17b15 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
17b16 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
17b17 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64  le being dropped
17b18 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74   does not have t
17b19 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d  he largest root-
17b1a 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  page.        ** 
17b1b 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61  number in the da
17b1c 74 61 62 61 73 65 2e 20 53 6f 20 6d 6f 76 65 20  tabase. So move 
17b1d 74 68 65 20 70 61 67 65 20 74 68 61 74 20 64 6f  the page that do
17b1e 65 73 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20  es into the .   
17b1f 20 20 20 20 20 2a 2a 20 67 61 70 20 6c 65 66 74       ** gap left
17b20 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 20   by the deleted 
17b21 72 6f 6f 74 2d 70 61 67 65 2e 0a 20 20 20 20 20  root-page..     
17b22 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 4d 65     */.        Me
17b23 6d 50 61 67 65 20 2a 70 4d 6f 76 65 3b 0a 20 20  mPage *pMove;.  
17b24 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
17b25 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
17b26 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
17b27 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
17b28 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d  maxRootPgno, &pM
17b29 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ove, 0);.       
17b2a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17b2b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
17b2c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
17b2d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20     }.        rc 
17b2e 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70  = relocatePage(p
17b2f 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d 41  Bt, pMove, PTRMA
17b30 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 69  P_ROOTPAGE, 0, i
17b31 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 20 20  Table, 0);.     
17b32 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
17b33 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20 69  Move);.        i
17b34 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
17b35 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
17b36 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
17b37 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   }.        rc = 
17b38 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
17b39 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74  age(pBt, maxRoot
17b3a 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29  Pgno, &pMove, 0)
17b3b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
17b3c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17b3d 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
17b3e 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
17b3f 20 20 20 20 20 20 72 63 20 3d 20 66 72 65 65 50        rc = freeP
17b40 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20  age(pMove);.    
17b41 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
17b42 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20  pMove);.        
17b43 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
17b44 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
17b45 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
17b46 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 69 4d    }.        *piM
17b47 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 50 67  oved = maxRootPg
17b48 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  no;.      }..   
17b49 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e 65     /* Set the ne
17b4a 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 67 65  w 'max-root-page
17b4b 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 64  ' value in the d
17b4c 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e 20  atabase header. 
17b4d 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 73  This.      ** is
17b4e 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 6c   the old value l
17b4f 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 6f 6e  ess one, less on
17b50 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 20 68  e more if that h
17b51 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 20 20  appens to.      
17b52 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 61 67  ** be a root-pag
17b53 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20 6f  e number, less o
17b54 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 61 74  ne again if that
17b55 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a 2a   is the.      **
17b56 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41   PENDING_BYTE_PA
17b57 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  GE..      */.   
17b58 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d     maxRootPgno--
17b59 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 61 78 52  ;.      if( maxR
17b5a 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47  ootPgno==PENDING
17b5b 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
17b5c 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f  ){.        maxRo
17b5d 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20  otPgno--;.      
17b5e 7d 0a 20 20 20 20 20 20 69 66 28 20 6d 61 78 52  }.      if( maxR
17b5f 6f 6f 74 50 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f  ootPgno==PTRMAP_
17b60 50 41 47 45 4e 4f 28 70 42 74 2c 20 6d 61 78 52  PAGENO(pBt, maxR
17b61 6f 6f 74 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20  ootPgno) ){.    
17b62 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d      maxRootPgno-
17b63 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  -;.      }.     
17b64 20 61 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74   assert( maxRoot
17b65 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno!=PENDING_BY
17b66 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a  TE_PAGE(pBt) );.
17b67 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
17b68 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
17b69 74 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74  ta(p, 4, maxRoot
17b6a 50 67 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Pgno);.    }else
17b6b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 72 65  {.      rc = fre
17b6c 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
17b6d 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
17b6e 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65  pPage);.    }.#e
17b6f 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ndif.  }else{.  
17b70 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 42    /* If sqlite3B
17b71 74 72 65 65 44 72 6f 70 54 61 62 6c 65 20 77 61  treeDropTable wa
17b72 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65  s called on page
17b73 20 31 2e 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50   1. */.    zeroP
17b74 61 67 65 28 70 50 61 67 65 2c 20 50 54 46 5f 49  age(pPage, PTF_I
17b75 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20 29  NTKEY|PTF_LEAF )
17b76 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  ;.    releasePag
17b77 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20  e(pPage);.  }.  
17b78 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53  return rc;  .}.S
17b79 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
17b7a 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72  t sqlite3BtreeDr
17b7b 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  opTable(Btree *p
17b7c 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e  , int iTable, in
17b7d 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69  t *piMoved){.  i
17b7e 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
17b7f 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
17b80 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f 70 54   rc = btreeDropT
17b81 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20  able(p, iTable, 
17b82 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69  piMoved);.  sqli
17b83 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
17b84 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
17b85 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68  .../*.** Read th
17b86 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69  e meta-informati
17b87 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 74 61  on out of a data
17b88 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 61  base file.  Meta
17b89 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75  [0].** is the nu
17b8a 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 67  mber of free pag
17b8b 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  es currently in 
17b8c 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d  the database.  M
17b8d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67  eta[1].** throug
17b8e 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 61  h meta[15] are a
17b8f 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65  vailable for use
17b90 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 65 72   by higher layer
17b91 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69  s.  Meta[0].** i
17b92 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65  s read-only, the
17b93 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 61 64   others are read
17b94 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54  /write..** .** T
17b95 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20  he schema layer 
17b96 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 61 6c  numbers meta val
17b97 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 2e  ues differently.
17b98 20 20 41 74 20 74 68 65 20 73 63 68 65 6d 61 0a    At the schema.
17b99 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 74 68  ** layer (and th
17b9a 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20  e SetCookie and 
17b9b 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64  ReadCookie opcod
17b9c 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  es) the number o
17b9d 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 73 20  f.** free pages 
17b9e 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20  is not visible. 
17b9f 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73   So Cookie[0] is
17ba0 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d 65 74   the same as Met
17ba1 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  a[1]..*/.SQLITE_
17ba2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17ba3 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28  te3BtreeGetMeta(
17ba4 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64  Btree *p, int id
17ba5 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a  x, u32 *pMeta){.
17ba6 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
17ba7 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  e = 0;.  int rc;
17ba8 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
17ba9 20 2a 70 50 31 3b 0a 20 20 42 74 53 68 61 72 65   *pP1;.  BtShare
17baa 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
17bab 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
17bac 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 2f 2a 20  Enter(p);..  /* 
17bad 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61 2d 64  Reading a meta-d
17bae 61 74 61 20 76 61 6c 75 65 20 72 65 71 75 69 72  ata value requir
17baf 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  es a read-lock o
17bb0 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20 68 65  n page 1 (and he
17bb1 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c  nce.  ** the sql
17bb2 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
17bb3 2e 20 57 65 20 67 72 61 62 20 74 68 69 73 20 6c  . We grab this l
17bb4 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ock regardless o
17bb5 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20 20 2a  f whether or.  *
17bb6 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49 54 45  * not the SQLITE
17bb7 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64  _ReadUncommitted
17bb8 20 66 6c 61 67 20 69 73 20 73 65 74 20 28 74 68   flag is set (th
17bb9 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20 61  e table rooted a
17bba 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20 69 73  t page.  ** 1 is
17bbb 20 74 72 65 61 74 65 64 20 61 73 20 61 20 73 70   treated as a sp
17bbc 65 63 69 61 6c 20 63 61 73 65 20 62 79 20 71 75  ecial case by qu
17bbd 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61  erySharedCacheTa
17bbe 62 6c 65 4c 6f 63 6b 28 29 0a 20 20 2a 2a 20 61  bleLock().  ** a
17bbf 6e 64 20 73 65 74 53 68 61 72 65 64 43 61 63 68  nd setSharedCach
17bc0 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a 20  eTableLock()).. 
17bc1 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79   */.  rc = query
17bc2 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
17bc3 4c 6f 63 6b 28 70 2c 20 31 2c 20 52 45 41 44 5f  Lock(p, 1, READ_
17bc4 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 63 21  LOCK);.  if( rc!
17bc5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17bc6 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
17bc7 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75  ave(p);.    retu
17bc8 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73  rn rc;.  }..  as
17bc9 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20  sert( idx>=0 && 
17bca 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 69 66 28  idx<=15 );.  if(
17bcb 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 7b 0a   pBt->pPage1 ){.
17bcc 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74 72 65      /* The b-tre
17bcd 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 6f 6c  e is already hol
17bce 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65  ding a reference
17bcf 20 74 6f 20 70 61 67 65 20 31 20 6f 66 20 74 68   to page 1 of th
17bd0 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a  e database.    *
17bd1 2a 20 66 69 6c 65 2e 20 49 6e 20 74 68 69 73 20  * file. In this 
17bd2 63 61 73 65 20 74 68 65 20 72 65 71 75 69 72 65  case the require
17bd3 64 20 6d 65 74 61 2d 64 61 74 61 20 76 61 6c 75  d meta-data valu
17bd4 65 20 63 61 6e 20 62 65 20 72 65 61 64 20 64 69  e can be read di
17bd5 72 65 63 74 6c 79 0a 20 20 20 20 2a 2a 20 66 72  rectly.    ** fr
17bd6 6f 6d 20 74 68 65 20 70 61 67 65 20 64 61 74 61  om the page data
17bd7 20 6f 66 20 74 68 69 73 20 72 65 66 65 72 65 6e   of this referen
17bd8 63 65 2e 20 54 68 69 73 20 69 73 20 73 6c 69 67  ce. This is slig
17bd9 68 74 6c 79 20 66 61 73 74 65 72 20 74 68 61 6e  htly faster than
17bda 0a 20 20 20 20 2a 2a 20 72 65 71 75 65 73 74 69  .    ** requesti
17bdb 6e 67 20 61 20 6e 65 77 20 72 65 66 65 72 65 6e  ng a new referen
17bdc 63 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ce from the page
17bdd 72 20 6c 61 79 65 72 2e 0a 20 20 20 20 2a 2f 0a  r layer..    */.
17bde 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73 69 67      pP1 = (unsig
17bdf 6e 65 64 20 63 68 61 72 20 2a 29 70 42 74 2d 3e  ned char *)pBt->
17be0 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20  pPage1->aData;. 
17be1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
17be2 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73 20 6e  he b-tree does n
17be3 6f 74 20 68 61 76 65 20 61 20 72 65 66 65 72 65  ot have a refere
17be4 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 6f 66  nce to page 1 of
17be5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
17be6 6c 65 2e 0a 20 20 20 20 2a 2a 20 4f 62 74 61 69  le..    ** Obtai
17be7 6e 20 6f 6e 65 20 66 72 6f 6d 20 74 68 65 20 70  n one from the p
17be8 61 67 65 72 20 6c 61 79 65 72 2e 0a 20 20 20 20  ager layer..    
17be9 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  */.    rc = sqli
17bea 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d  te3PagerGet(pBt-
17beb 3e 70 50 61 67 65 72 2c 20 31 2c 20 26 70 44 62  >pPager, 1, &pDb
17bec 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
17bed 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  c ){.      sqlit
17bee 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
17bef 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
17bf0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 31 20  ;.    }.    pP1 
17bf1 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
17bf2 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47   *)sqlite3PagerG
17bf3 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b  etData(pDbPage);
17bf4 0a 20 20 7d 0a 20 20 2a 70 4d 65 74 61 20 3d 20  .  }.  *pMeta = 
17bf5 67 65 74 34 62 79 74 65 28 26 70 50 31 5b 33 36  get4byte(&pP1[36
17bf6 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 20 2f   + idx*4]);..  /
17bf7 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20  * If the b-tree 
17bf8 69 73 20 6e 6f 74 20 68 6f 6c 64 69 6e 67 20 61  is not holding a
17bf9 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61   reference to pa
17bfa 67 65 20 31 2c 20 74 68 65 6e 20 6f 6e 65 20 77  ge 1, then one w
17bfb 61 73 20 0a 20 20 2a 2a 20 72 65 71 75 65 73 74  as .  ** request
17bfc 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ed from the page
17bfd 72 20 6c 61 79 65 72 20 69 6e 20 74 68 65 20 61  r layer in the a
17bfe 62 6f 76 65 20 62 6c 6f 63 6b 2e 20 52 65 6c 65  bove block. Rele
17bff 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f  ase it now..  */
17c00 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 50 61  .  if( !pBt->pPa
17c01 67 65 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ge1 ){.    sqlit
17c02 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62  e3PagerUnref(pDb
17c03 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Page);.  }..  /*
17c04 20 49 66 20 61 75 74 6f 76 61 63 75 75 6d 65 64   If autovacuumed
17c05 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20   is disabled in 
17c06 74 68 69 73 20 62 75 69 6c 64 20 62 75 74 20 77  this build but w
17c07 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20  e are trying to 
17c08 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 61 6e 20  .  ** access an 
17c09 61 75 74 6f 76 61 63 75 75 6d 65 64 20 64 61 74  autovacuumed dat
17c0a 61 62 61 73 65 2c 20 74 68 65 6e 20 6d 61 6b 65  abase, then make
17c0b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65   the database re
17c0c 61 64 6f 6e 6c 79 2e 20 0a 20 20 2a 2f 0a 23 69  adonly. .  */.#i
17c0d 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
17c0e 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66  _AUTOVACUUM.  if
17c0f 28 20 69 64 78 3d 3d 34 20 26 26 20 2a 70 4d 65  ( idx==4 && *pMe
17c10 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 61 64  ta>0 ) pBt->read
17c11 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 69 66  Only = 1;.#endif
17c12 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
17c13 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20  is currently an 
17c14 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  open transaction
17c15 2c 20 67 72 61 62 20 61 20 72 65 61 64 2d 6c 6f  , grab a read-lo
17c16 63 6b 20 0a 20 20 2a 2a 20 6f 6e 20 70 61 67 65  ck .  ** on page
17c17 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61   1 of the databa
17c18 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73  se file. This is
17c19 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75   done to make su
17c1a 72 65 20 74 68 61 74 0a 20 20 2a 2a 20 6e 6f 20  re that.  ** no 
17c1b 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
17c1c 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 20   can modify the 
17c1d 6d 65 74 61 20 76 61 6c 75 65 20 6a 75 73 74 20  meta value just 
17c1e 72 65 61 64 20 66 72 6f 6d 0a 20 20 2a 2a 20 74  read from.  ** t
17c1f 68 65 20 64 61 74 61 62 61 73 65 20 75 6e 74 69  he database unti
17c20 6c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  l the transactio
17c21 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e 0a  n is concluded..
17c22 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e    */.  if( p->in
17c23 54 72 61 6e 73 3e 30 20 29 7b 0a 20 20 20 20 72  Trans>0 ){.    r
17c24 63 20 3d 20 73 65 74 53 68 61 72 65 64 43 61 63  c = setSharedCac
17c25 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 31  heTableLock(p, 1
17c26 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20  , READ_LOCK);.  
17c27 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
17c28 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
17c29 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
17c2a 57 72 69 74 65 20 6d 65 74 61 2d 69 6e 66 6f 72  Write meta-infor
17c2b 6d 61 74 69 6f 6e 20 62 61 63 6b 20 69 6e 74 6f  mation back into
17c2c 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
17c2d 4d 65 74 61 5b 30 5d 20 69 73 0a 2a 2a 20 72 65  Meta[0] is.** re
17c2e 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 6d 61 79 20  ad-only and may 
17c2f 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 2e 0a  not be written..
17c30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17c31 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
17c32 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 72  eeUpdateMeta(Btr
17c33 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20  ee *p, int idx, 
17c34 75 33 32 20 69 4d 65 74 61 29 7b 0a 20 20 42 74  u32 iMeta){.  Bt
17c35 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
17c36 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64  >pBt;.  unsigned
17c37 20 63 68 61 72 20 2a 70 50 31 3b 0a 20 20 69 6e   char *pP1;.  in
17c38 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
17c39 69 64 78 3e 3d 31 20 26 26 20 69 64 78 3c 3d 31  idx>=1 && idx<=1
17c3a 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  5 );.  sqlite3Bt
17c3b 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61  reeEnter(p);.  a
17c3c 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
17c3d 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
17c3e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ;.  assert( pBt-
17c3f 3e 70 50 61 67 65 31 21 3d 30 20 29 3b 0a 20 20  >pPage1!=0 );.  
17c40 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65  pP1 = pBt->pPage
17c41 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 3d  1->aData;.  rc =
17c42 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
17c43 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  te(pBt->pPage1->
17c44 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20  pDbPage);.  if( 
17c45 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
17c46 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70  .    put4byte(&p
17c47 50 31 5b 33 36 20 2b 20 69 64 78 2a 34 5d 2c 20  P1[36 + idx*4], 
17c48 69 4d 65 74 61 29 3b 0a 23 69 66 6e 64 65 66 20  iMeta);.#ifndef 
17c49 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
17c4a 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 69  VACUUM.    if( i
17c4b 64 78 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 61  dx==7 ){.      a
17c4c 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f  ssert( pBt->auto
17c4d 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65 74 61 3d  Vacuum || iMeta=
17c4e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  =0 );.      asse
17c4f 72 74 28 20 69 4d 65 74 61 3d 3d 30 20 7c 7c 20  rt( iMeta==0 || 
17c50 69 4d 65 74 61 3d 3d 31 20 29 3b 0a 20 20 20 20  iMeta==1 );.    
17c51 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75    pBt->incrVacuu
17c52 6d 20 3d 20 28 75 38 29 69 4d 65 74 61 3b 0a 20  m = (u8)iMeta;. 
17c53 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
17c54 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
17c55 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
17c56 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
17c57 74 75 72 6e 20 74 68 65 20 66 6c 61 67 20 62 79  turn the flag by
17c58 74 65 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  te at the beginn
17c59 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 20  ing of the page 
17c5a 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a  that the cursor.
17c5b 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ** is currently 
17c5c 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2f 0a  pointing to..*/.
17c5d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
17c5e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
17c5f 6c 61 67 73 28 42 74 43 75 72 73 6f 72 20 2a 70  lags(BtCursor *p
17c60 43 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 3a  Cur){.  /* TODO:
17c61 20 57 68 61 74 20 61 62 6f 75 74 20 43 55 52 53   What about CURS
17c62 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 73  OR_REQUIRESEEK s
17c63 74 61 74 65 3f 20 50 72 6f 62 61 62 6c 79 20 6e  tate? Probably n
17c64 65 65 64 20 74 6f 20 63 61 6c 6c 0a 20 20 2a 2a  eed to call.  **
17c65 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
17c66 73 69 74 69 6f 6e 28 29 20 68 65 72 65 2e 0a 20  sition() here.. 
17c67 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70   */.  MemPage *p
17c68 50 61 67 65 3b 0a 20 20 72 65 73 74 6f 72 65 43  Page;.  restoreC
17c69 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43  ursorPosition(pC
17c6a 75 72 29 3b 0a 20 20 70 50 61 67 65 20 3d 20 70  ur);.  pPage = p
17c6b 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
17c6c 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65  ->iPage];.  asse
17c6d 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
17c6e 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
17c6f 61 73 73 65 72 74 28 20 70 50 61 67 65 21 3d 30  assert( pPage!=0
17c70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
17c71 61 67 65 2d 3e 70 42 74 3d 3d 70 43 75 72 2d 3e  age->pBt==pCur->
17c72 70 42 74 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  pBt );.  return 
17c73 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
17c74 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 5d 3b 0a  ge->hdrOffset];.
17c75 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
17c76 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e  E_OMIT_BTREECOUN
17c77 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  T./*.** The firs
17c78 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43 75 72  t argument, pCur
17c79 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70  , is a cursor op
17c7a 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74  ened on some b-t
17c7b 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a  ree. Count the.*
17c7c 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * number of entr
17c7d 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74 72 65  ies in the b-tre
17c7e 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 20  e and write the 
17c7f 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74  result to *pnEnt
17c80 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  ry..**.** SQLITE
17c81 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
17c82 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  if the operation
17c83 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79   is successfully
17c84 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f   executed. .** O
17c85 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20  therwise, if an 
17c86 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
17c87 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f  ered (i.e. an IO
17c88 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61 62 61   error or databa
17c89 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e  se.** corruption
17c8a 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  ) an SQLite erro
17c8b 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
17c8c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
17c8d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
17c8e 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 43 75  3BtreeCount(BtCu
17c8f 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20  rsor *pCur, i64 
17c90 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69 36 34  *pnEntry){.  i64
17c91 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 20 20   nEntry = 0;    
17c92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c93 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
17c94 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79  turn in *pnEntry
17c95 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
17c96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c97 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
17c98 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72  turn code */.  r
17c99 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70  c = moveToRoot(p
17c9a 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65  Cur);..  /* Unle
17c9b 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  ss an error occu
17c9c 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  rs, the followin
17c9d 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20  g loop runs one 
17c9e 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65 61  iteration for ea
17c9f 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20  ch.  ** page in 
17ca0 74 68 65 20 42 2d 54 72 65 65 20 73 74 72 75 63  the B-Tree struc
17ca1 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64  ture (not includ
17ca2 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ing overflow pag
17ca3 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69  es). .  */.  whi
17ca4 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
17ca5 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 64  K ){.    int iId
17ca6 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
17ca7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
17ca8 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f  ndex of child no
17ca9 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a  de in parent */.
17caa 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61      MemPage *pPa
17cab 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
17cac 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
17cad 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d  t page of the b-
17cae 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  tree */..    /* 
17caf 49 66 20 74 68 69 73 20 69 73 20 61 20 6c 65 61  If this is a lea
17cb0 66 20 70 61 67 65 20 6f 72 20 74 68 65 20 74 72  f page or the tr
17cb1 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74  ee is not an int
17cb2 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65 6e 20  -key tree, then 
17cb3 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70 61 67  .    ** this pag
17cb4 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74  e contains count
17cb5 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 49 6e  able entries. In
17cb6 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e 74 72  crement the entr
17cb7 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a  y counter.    **
17cb8 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20   accordingly..  
17cb9 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 20 3d    */.    pPage =
17cba 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
17cbb 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
17cbc 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  if( pPage->leaf 
17cbd 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65  || !pPage->intKe
17cbe 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72  y ){.      nEntr
17cbf 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c  y += pPage->nCel
17cc0 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  l;.    }..    /*
17cc1 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66   pPage is a leaf
17cc2 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70   node. This loop
17cc3 20 6e 61 76 69 67 61 74 65 73 20 74 68 65 20 63   navigates the c
17cc4 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74  ursor so that it
17cc5 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20   .    ** points 
17cc6 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e 74  to the first int
17cc7 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61 74 20  erior cell that 
17cc8 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
17cc9 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a   parent of.    *
17cca 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  * the next page 
17ccb 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 74  in the tree that
17ccc 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
17ccd 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65 0a 20  n visited. The. 
17cce 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64     ** pCur->aiId
17ccf 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 76  x[pCur->iPage] v
17cd0 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f 20 74  alue is set to t
17cd1 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
17cd2 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20  parent cell.    
17cd3 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20  ** of the page, 
17cd4 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  or to the number
17cd5 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 68 65   of cells in the
17cd6 20 70 61 67 65 20 69 66 20 74 68 65 20 6e 65 78   page if the nex
17cd7 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f  t page.    ** to
17cd8 20 76 69 73 69 74 20 69 73 20 74 68 65 20 72 69   visit is the ri
17cd9 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69 74 73  ght-child of its
17cda 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a   parent..    **.
17cdb 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61      ** If all pa
17cdc 67 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 20  ges in the tree 
17cdd 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 65  have been visite
17cde 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  d, return SQLITE
17cdf 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20 20 2a  _OK to the.    *
17ce0 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f  * caller..    */
17ce1 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
17ce2 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 64 6f  leaf ){.      do
17ce3 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70   {.        if( p
17ce4 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b  Cur->iPage==0 ){
17ce5 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c  .          /* Al
17ce6 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 62  l pages of the b
17ce7 2d 74 72 65 65 20 68 61 76 65 20 62 65 65 6e 20  -tree have been 
17ce8 76 69 73 69 74 65 64 2e 20 52 65 74 75 72 6e 20  visited. Return 
17ce9 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f  successfully. */
17cea 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e 45 6e  .          *pnEn
17ceb 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20  try = nEntry;.  
17cec 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
17ced 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
17cee 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
17cef 74 65 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61  te3BtreeMoveToPa
17cf0 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20  rent(pCur);.    
17cf1 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 72 2d    }while ( pCur-
17cf2 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
17cf3 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 61 67  ge]>=pCur->apPag
17cf4 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
17cf5 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 20 20  nCell );..      
17cf6 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
17cf7 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 20 20  ->iPage]++;.    
17cf8 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
17cf9 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
17cfa 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ge];.    }..    
17cfb 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 74 68  /* Descend to th
17cfc 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f 66 20  e child node of 
17cfd 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 74 68  the cell that th
17cfe 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74  e cursor current
17cff 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74  ly .    ** point
17d00 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 74 68  s at. This is th
17d01 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69 66  e right-child if
17d02 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e   (iIdx==pPage->n
17d03 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  Cell)..    */.  
17d04 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e 61    iIdx = pCur->a
17d05 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
17d06 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 78 3d  ];.    if( iIdx=
17d07 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b  =pPage->nCell ){
17d08 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65  .      rc = move
17d09 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65  ToChild(pCur, ge
17d0a 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
17d0b 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
17d0c 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20  ffset+8]));.    
17d0d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
17d0e 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
17d0f 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e  ur, get4byte(fin
17d10 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49 64  dCell(pPage, iId
17d11 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  x)));.    }.  }.
17d12 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68  .  /* An error h
17d13 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 65 74  as occurred. Ret
17d14 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  urn an error cod
17d15 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  e. */.  return r
17d16 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  c;.}.#endif../*.
17d17 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61  ** Return the pa
17d18 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 77  ger associated w
17d19 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 54 68  ith a BTree.  Th
17d1a 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
17d1b 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e  ed for.** testin
17d1c 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20  g and debugging 
17d1d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  only..*/.SQLITE_
17d1e 50 52 49 56 41 54 45 20 50 61 67 65 72 20 2a 73  PRIVATE Pager *s
17d1f 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
17d20 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 65  (Btree *p){.  re
17d21 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 61  turn p->pBt->pPa
17d22 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ger;.}..#ifndef 
17d23 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
17d24 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a  GRITY_CHECK./*.*
17d25 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73 61  * Append a messa
17d26 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72 20  ge to the error 
17d27 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e 0a  message string..
17d28 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
17d29 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a 20  heckAppendMsg(. 
17d2a 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43   IntegrityCk *pC
17d2b 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a 4d  heck,.  char *zM
17d2c 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  sg1,.  const cha
17d2d 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e 2e  r *zFormat,.  ..
17d2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
17d2f 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63 6b  p;.  if( !pCheck
17d30 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72 6e  ->mxErr ) return
17d31 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72  ;.  pCheck->mxEr
17d32 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6e  r--;.  pCheck->n
17d33 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 72  Err++;.  va_star
17d34 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
17d35 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72    if( pCheck->er
17d36 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20 20  rMsg.nChar ){.  
17d37 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
17d38 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b 2d  mAppend(&pCheck-
17d39 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 31  >errMsg, "\n", 1
17d3a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d 73  );.  }.  if( zMs
17d3b 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  g1 ){.    sqlite
17d3c 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
17d3d 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c  &pCheck->errMsg,
17d3e 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20 7d   zMsg1, -1);.  }
17d3f 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  .  sqlite3VXPrin
17d40 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d  tf(&pCheck->errM
17d41 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20  sg, 1, zFormat, 
17d42 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
17d43 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d  );.  if( pCheck-
17d44 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 61  >errMsg.mallocFa
17d45 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68 65  iled ){.    pChe
17d46 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ck->mallocFailed
17d47 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64   = 1;.  }.}.#end
17d48 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
17d49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
17d4a 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  K */..#ifndef SQ
17d4b 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
17d4c 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20  ITY_CHECK./*.** 
17d4d 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 66  Add 1 to the ref
17d4e 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72  erence count for
17d4f 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 66   page iPage.  If
17d50 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65 63   this is the sec
17d51 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65  ond.** reference
17d52 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 64   to the page, ad
17d53 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  d an error messa
17d54 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a 45  ge to pCheck->zE
17d55 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 6e  rrMsg..** Return
17d56 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 20   1 if there are 
17d57 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 72  2 ore more refer
17d58 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67  ences to the pag
17d59 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 66  e and 0 if.** if
17d5a 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72   this is the fir
17d5b 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  st reference to 
17d5c 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  the page..**.** 
17d5d 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 20  Also check that 
17d5e 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
17d5f 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a 2f  is in bounds..*/
17d60 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63  .static int chec
17d61 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 6b  kRef(IntegrityCk
17d62 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 69   *pCheck, Pgno i
17d63 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e  Page, char *zCon
17d64 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50 61  text){.  if( iPa
17d65 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  ge==0 ) return 1
17d66 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70 43  ;.  if( iPage>pC
17d67 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a 20  heck->nPage ){. 
17d68 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
17d69 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65  g(pCheck, zConte
17d6a 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61 67  xt, "invalid pag
17d6b 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69 50  e number %d", iP
17d6c 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  age);.    return
17d6d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43   1;.  }.  if( pC
17d6e 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67  heck->anRef[iPag
17d6f 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68 65  e]==1 ){.    che
17d70 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
17d71 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 32  ck, zContext, "2
17d72 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  nd reference to 
17d73 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29  page %d", iPage)
17d74 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
17d75 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 28 70    }.  return  (p
17d76 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61  Check->anRef[iPa
17d77 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 66  ge]++)>1;.}..#if
17d78 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17d79 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a  _AUTOVACUUM./*.*
17d7a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
17d7b 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f   entry in the po
17d7c 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 70 61  inter-map for pa
17d7d 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 20 74  ge iChild maps t
17d7e 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 72 65  o .** page iPare
17d7f 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 70 65  nt, pointer type
17d80 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e 6f 74   ptrType. If not
17d81 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 72 6f  , append an erro
17d82 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20  r message.** to 
17d83 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69  pCheck..*/.stati
17d84 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 72 6d  c void checkPtrm
17d85 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43  ap(.  IntegrityC
17d86 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a 20  k *pCheck,   /* 
17d87 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 20  Integrity check 
17d88 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67 6e  context */.  Pgn
17d89 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20 20  o iChild,       
17d8a 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 61 67      /* Child pag
17d8b 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38  e number */.  u8
17d8c 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20   eType,         
17d8d 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64       /* Expected
17d8e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 79 70   pointer map typ
17d8f 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61 72  e */.  Pgno iPar
17d90 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ent,          /*
17d91 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 65   Expected pointe
17d92 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 61 67  r map parent pag
17d93 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68  e number */.  ch
17d94 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 20  ar *zContext    
17d95 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
17d96 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73 65  description (use
17d97 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67 29  d for error msg)
17d98 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
17d99 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 79 70  .  u8 ePtrmapTyp
17d9a 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d 61  e;.  Pgno iPtrma
17d9b 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20 3d  pParent;..  rc =
17d9c 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 65 63   ptrmapGet(pChec
17d9d 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c 20  k->pBt, iChild, 
17d9e 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 26 69  &ePtrmapType, &i
17d9f 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20  PtrmapParent);. 
17da0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
17da1 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63  OK ){.    if( rc
17da2 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c  ==SQLITE_NOMEM |
17da3 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  | rc==SQLITE_IOE
17da4 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65 63  RR_NOMEM ) pChec
17da5 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  k->mallocFailed 
17da6 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41 70  = 1;.    checkAp
17da7 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
17da8 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c 65  zContext, "Faile
17da9 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61 70  d to read ptrmap
17daa 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c 64   key=%d", iChild
17dab 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
17dac 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d 61   }..  if( ePtrma
17dad 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c 20  pType!=eType || 
17dae 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d 69  iPtrmapParent!=i
17daf 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 68  Parent ){.    ch
17db0 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
17db1 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a  eck, zContext, .
17db2 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20 6d        "Bad ptr m
17db3 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 20  ap entry key=%d 
17db4 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64 29  expected=(%d,%d)
17db5 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 0a   got=(%d,%d)", .
17db6 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65 54        iChild, eT
17db7 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 50  ype, iParent, eP
17db8 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72 6d  trmapType, iPtrm
17db9 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d  apParent);.  }.}
17dba 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
17dbb 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69  heck the integri
17dbc 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69  ty of the freeli
17dbd 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 72  st or of an over
17dbe 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a  flow page list..
17dbf 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  ** Verify that t
17dc0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
17dc1 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 69  es on the list i
17dc2 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  s N..*/.static v
17dc3 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a 20  oid checkList(. 
17dc4 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43   IntegrityCk *pC
17dc5 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 72  heck,  /* Integr
17dc6 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f 6e  ity checking con
17dc7 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73  text */.  int is
17dc8 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20 20  FreeList,       
17dc9 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66 72  /* True for a fr
17dca 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 66  eelist.  False f
17dcb 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  or overflow page
17dcc 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69   list */.  int i
17dcd 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  Page,           
17dce 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
17dcf 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20 69  for first page i
17dd0 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20  n the list */.  
17dd1 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20  int N,          
17dd2 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65        /* Expecte
17dd3 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  d number of page
17dd4 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f  s in the list */
17dd5 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78  .  char *zContex
17dd6 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74  t        /* Cont
17dd7 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65  ext for error me
17dd8 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 69  ssages */.){.  i
17dd9 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 65  nt i;.  int expe
17dda 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 20  cted = N;.  int 
17ddb 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b 0a  iFirst = iPage;.
17ddc 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30    while( N-- > 0
17ddd 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72   && pCheck->mxEr
17dde 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20  r ){.    DbPage 
17ddf 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 20  *pOvflPage;.    
17de0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
17de1 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 66  OvflData;.    if
17de2 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 20  ( iPage<1 ){.   
17de3 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
17de4 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65  g(pCheck, zConte
17de5 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25 64  xt,.         "%d
17de6 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69 73   of %d pages mis
17de7 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 6c  sing from overfl
17de8 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67  ow list starting
17de9 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20 20   at %d",.       
17dea 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 64     N+1, expected
17deb 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20 20  , iFirst);.     
17dec 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
17ded 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70    if( checkRef(p
17dee 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 43  Check, iPage, zC
17def 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b 3b  ontext) ) break;
17df0 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
17df1 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b 2d  PagerGet(pCheck-
17df2 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 69  >pPager, (Pgno)i
17df3 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 65  Page, &pOvflPage
17df4 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b  ) ){.      check
17df5 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
17df6 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 69  , zContext, "fai
17df7 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65 20  led to get page 
17df8 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20  %d", iPage);.   
17df9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
17dfa 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d 20      pOvflData = 
17dfb 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
17dfc 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  )sqlite3PagerGet
17dfd 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29 3b  Data(pOvflPage);
17dfe 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65 4c  .    if( isFreeL
17dff 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ist ){.      int
17e00 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70   n = get4byte(&p
17e01 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 69  OvflData[4]);.#i
17e02 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
17e03 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
17e04 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70     if( pCheck->p
17e05 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
17e06 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50  {.        checkP
17e07 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 50  trmap(pCheck, iP
17e08 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45  age, PTRMAP_FREE
17e09 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78  PAGE, 0, zContex
17e0a 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  t);.      }.#end
17e0b 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 70  if.      if( n>p
17e0c 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62  Check->pBt->usab
17e0d 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 20  leSize/4-2 ){.  
17e0e 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e        checkAppen
17e0f 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
17e10 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
17e11 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61 66    "freelist leaf
17e12 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f   count too big o
17e13 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67  n page %d", iPag
17e14 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b  e);.        N--;
17e15 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
17e16 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
17e17 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  <n; i++){.      
17e18 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 61      Pgno iFreePa
17e19 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  ge = get4byte(&p
17e1a 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d 29  OvflData[8+i*4])
17e1b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
17e1c 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
17e1d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
17e1e 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f  Check->pBt->auto
17e1f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20  Vacuum ){.      
17e20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
17e21 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50  p(pCheck, iFreeP
17e22 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 45  age, PTRMAP_FREE
17e23 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78  PAGE, 0, zContex
17e24 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  t);.          }.
17e25 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20  #endif.         
17e26 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b   checkRef(pCheck
17e27 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43 6f  , iFreePage, zCo
17e28 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20  ntext);.        
17e29 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 6e  }.        N -= n
17e2a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
17e2b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
17e2c 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
17e2d 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f     else{.      /
17e2e 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61  * If this databa
17e2f 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f  se supports auto
17e30 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61 67  -vacuum and iPag
17e31 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73  e is not the las
17e32 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20  t.      ** page 
17e33 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f 77  in this overflow
17e34 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68 61   list, check tha
17e35 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  t the pointer-ma
17e36 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 20  p entry for.    
17e37 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69    ** the followi
17e38 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73 20  ng page matches 
17e39 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  iPage..      */.
17e3a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63 6b        if( pCheck
17e3b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ->pBt->autoVacuu
17e3c 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 20  m && N>0 ){.    
17e3d 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74 65      i = get4byte
17e3e 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20  (pOvflData);.   
17e3f 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70       checkPtrmap
17e40 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 4d  (pCheck, i, PTRM
17e41 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 50  AP_OVERFLOW2, iP
17e42 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a  age, zContext);.
17e43 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
17e44 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20 3d  ndif.    iPage =
17e45 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c 44   get4byte(pOvflD
17e46 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ata);.    sqlite
17e47 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66  3PagerUnref(pOvf
17e48 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65  lPage);.  }.}.#e
17e49 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
17e4a 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
17e4b 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ECK */..#ifndef 
17e4c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
17e4d 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a  GRITY_CHECK./*.*
17e4e 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61 6e  * Do various san
17e4f 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 20  ity checks on a 
17e50 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 61  single page of a
17e51 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a 2a   tree.  Return.*
17e52 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74 68  * the tree depth
17e53 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72 65  .  Root pages re
17e54 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 73  turn 0.  Parents
17e55 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a 2a   of root pages.*
17e56 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 20  * return 1, and 
17e57 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a 2a  so forth..** .**
17e58 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61 72   These checks ar
17e59 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  e done:.**.**   
17e5a 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 65     1.  Make sure
17e5b 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 20   that cells and 
17e5c 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e 6f  freeblocks do no
17e5d 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 20  t overlap.**    
17e5e 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69 6e        but combin
17e5f 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20  e to completely 
17e60 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e 0a  cover the page..
17e61 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b 65  **  NO  2.  Make
17e62 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 20   sure cell keys 
17e63 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a  are in order..**
17e64 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 73    NO  3.  Make s
17e65 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c 65  ure no key is le
17e66 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
17e67 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 2e   to zLowerBound.
17e68 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 6b  .**  NO  4.  Mak
17e69 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73  e sure no key is
17e6a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
17e6b 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 72   equal to zUpper
17e6c 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 35  Bound..**      5
17e6d 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74  .  Check the int
17e6e 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66 6c  egrity of overfl
17e6f 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 20  ow pages..**    
17e70 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65 6c    6.  Recursivel
17e71 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 65  y call checkTree
17e72 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c  Page on all chil
17e73 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 2e  dren..**      7.
17e74 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68    Verify that th
17e75 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 63  e depth of all c
17e76 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20 73  hildren is the s
17e77 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e 20  ame..**      8. 
17e78 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20   Make sure this 
17e79 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73 74  page is at least
17e7a 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73   33% full or els
17e7b 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20  e it is.**      
17e7c 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 20      the root of 
17e7d 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61  the tree..*/.sta
17e7e 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72 65  tic int checkTre
17e7f 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72 69  ePage(.  Integri
17e80 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f  tyCk *pCheck,  /
17e81 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 68  * Context for th
17e82 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 2a  e sanity check *
17e83 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20 20  /.  int iPage,  
17e84 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
17e85 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
17e86 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f  page to check */
17e87 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e 74  .  char *zParent
17e88 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 65  Context  /* Pare
17e89 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 7b  nt context */.){
17e8a 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
17e8b 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c 20  e;.  int i, rc, 
17e8c 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f 2c  depth, d2, pgno,
17e8d 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 2c   cnt;.  int hdr,
17e8e 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 6e   cellStart;.  in
17e8f 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a 64  t nCell;.  u8 *d
17e90 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20  ata;.  BtShared 
17e91 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 62  *pBt;.  int usab
17e92 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 7a  leSize;.  char z
17e93 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 20  Context[100];.  
17e94 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a 0a  char *hit = 0;..
17e95 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
17e96 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65  tf(sizeof(zConte
17e97 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  xt), zContext, "
17e98 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61 67  Page %d: ", iPag
17e99 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  e);..  /* Check 
17e9a 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65 78  that the page ex
17e9b 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 20  ists.  */.  pBt 
17e9c 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a 20  = pCheck->pBt;. 
17e9d 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42   usableSize = pB
17e9e 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20  t->usableSize;. 
17e9f 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20   if( iPage==0 ) 
17ea0 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
17ea1 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c  checkRef(pCheck,
17ea2 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 43   iPage, zParentC
17ea3 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72 6e  ontext) ) return
17ea4 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20   0;.  if( (rc = 
17ea5 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
17ea6 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69  age(pBt, (Pgno)i
17ea7 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29  Page, &pPage, 0)
17ea8 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )!=0 ){.    if( 
17ea9 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
17eaa 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
17eab 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68  OERR_NOMEM ) pCh
17eac 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  eck->mallocFaile
17ead 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b  d = 1;.    check
17eae 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
17eaf 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20  , zContext,.    
17eb0 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65     "unable to ge
17eb1 74 20 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f  t the page. erro
17eb2 72 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b  r code=%d", rc);
17eb3 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
17eb4 20 7d 0a 20 20 69 66 28 20 28 72 63 20 3d 20 73   }.  if( (rc = s
17eb5 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50  qlite3BtreeInitP
17eb6 61 67 65 28 70 50 61 67 65 29 29 21 3d 30 20 29  age(pPage))!=0 )
17eb7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  {.    assert( rc
17eb8 3d 3d 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  ==SQLITE_CORRUPT
17eb9 20 29 3b 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79   );  /* The only
17eba 20 70 6f 73 73 69 62 6c 65 20 65 72 72 6f 72 20   possible error 
17ebb 66 72 6f 6d 20 49 6e 69 74 50 61 67 65 20 2a 2f  from InitPage */
17ebc 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64  .    checkAppend
17ebd 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e  Msg(pCheck, zCon
17ebe 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20  text, .         
17ebf 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 69 74            "sqlit
17ec0 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28  e3BtreeInitPage(
17ec1 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72 20  ) returns error 
17ec2 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a 20  code %d", rc);. 
17ec3 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
17ec4 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
17ec5 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  n 0;.  }..  /* C
17ec6 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68 65  heck out all the
17ec7 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 64   cells..  */.  d
17ec8 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72 28  epth = 0;.  for(
17ec9 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43  i=0; i<pPage->nC
17eca 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d  ell && pCheck->m
17ecb 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  xErr; i++){.    
17ecc 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 75  u8 *pCell;.    u
17ecd 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c 49  32 sz;.    CellI
17ece 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 2f  nfo info;..    /
17ecf 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64 20  * Check payload 
17ed0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20  overflow pages. 
17ed1 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
17ed2 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
17ed3 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f  f(zContext), zCo
17ed4 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
17ed5 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61 67      "On tree pag
17ed6 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22 2c  e %d cell %d: ",
17ed7 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20   iPage, i);.    
17ed8 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
17ed9 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20 73  (pPage,i);.    s
17eda 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
17edb 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
17edc 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
17edd 20 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74    sz = info.nDat
17ede 61 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  a;.    if( !pPag
17edf 65 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b  e->intKey ) sz +
17ee0 3d 20 28 69 6e 74 29 69 6e 66 6f 2e 6e 4b 65 79  = (int)info.nKey
17ee1 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a  ;.    assert( sz
17ee2 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20  ==info.nPayload 
17ee3 29 3b 0a 20 20 20 20 69 66 28 20 28 73 7a 3e 69  );.    if( (sz>i
17ee4 6e 66 6f 2e 6e 4c 6f 63 61 6c 29 20 0a 20 20 20  nfo.nLocal) .   
17ee5 20 20 26 26 20 28 26 70 43 65 6c 6c 5b 69 6e 66    && (&pCell[inf
17ee6 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3c 3d 26 70  o.iOverflow]<=&p
17ee7 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d  Page->aData[pBt-
17ee8 3e 75 73 61 62 6c 65 53 69 7a 65 5d 29 0a 20 20  >usableSize]).  
17ee9 20 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e    ){.      int n
17eea 50 61 67 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66  Page = (sz - inf
17eeb 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c  o.nLocal + usabl
17eec 65 53 69 7a 65 20 2d 20 35 29 2f 28 75 73 61 62  eSize - 5)/(usab
17eed 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20  leSize - 4);.   
17eee 20 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c     Pgno pgnoOvfl
17eef 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65   = get4byte(&pCe
17ef0 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
17ef1 77 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  w]);.#ifndef SQL
17ef2 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
17ef3 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42  UUM.      if( pB
17ef4 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
17ef5 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74  .        checkPt
17ef6 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e  rmap(pCheck, pgn
17ef7 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56  oOvfl, PTRMAP_OV
17ef8 45 52 46 4c 4f 57 31 2c 20 69 50 61 67 65 2c 20  ERFLOW1, iPage, 
17ef9 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20  zContext);.     
17efa 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
17efb 63 68 65 63 6b 4c 69 73 74 28 70 43 68 65 63 6b  checkList(pCheck
17efc 2c 20 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e  , 0, pgnoOvfl, n
17efd 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  Page, zContext);
17efe 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
17eff 68 65 63 6b 20 73 61 6e 69 74 79 20 6f 66 20 6c  heck sanity of l
17f00 65 66 74 20 63 68 69 6c 64 20 70 61 67 65 2e 0a  eft child page..
17f01 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
17f02 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
17f03 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34       pgno = get4
17f04 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 23 69 66  byte(pCell);.#if
17f05 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17f06 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
17f07 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
17f08 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20  acuum ){.       
17f09 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68   checkPtrmap(pCh
17f0a 65 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41  eck, pgno, PTRMA
17f0b 50 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20  P_BTREE, iPage, 
17f0c 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20  zContext);.     
17f0d 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
17f0e 64 32 20 3d 20 63 68 65 63 6b 54 72 65 65 50 61  d2 = checkTreePa
17f0f 67 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c  ge(pCheck, pgno,
17f10 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   zContext);.    
17f11 20 20 69 66 28 20 69 3e 30 20 26 26 20 64 32 21    if( i>0 && d2!
17f12 3d 64 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20  =depth ){.      
17f13 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
17f14 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
17f15 74 2c 20 22 43 68 69 6c 64 20 70 61 67 65 20 64  t, "Child page d
17f16 65 70 74 68 20 64 69 66 66 65 72 73 22 29 3b 0a  epth differs");.
17f17 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65        }.      de
17f18 70 74 68 20 3d 20 64 32 3b 0a 20 20 20 20 7d 0a  pth = d2;.    }.
17f19 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65    }.  if( !pPage
17f1a 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67  ->leaf ){.    pg
17f1b 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
17f1c 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
17f1d 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
17f1e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ;.    sqlite3_sn
17f1f 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43  printf(sizeof(zC
17f20 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78  ontext), zContex
17f21 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  t, .            
17f22 20 20 20 20 20 20 20 20 20 22 4f 6e 20 70 61 67           "On pag
17f23 65 20 25 64 20 61 74 20 72 69 67 68 74 20 63 68  e %d at right ch
17f24 69 6c 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a  ild: ", iPage);.
17f25 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
17f26 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
17f27 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
17f28 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20  Vacuum ){.      
17f29 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65  checkPtrmap(pChe
17f2a 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50  ck, pgno, PTRMAP
17f2b 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 30  _BTREE, iPage, 0
17f2c 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
17f2d 20 20 20 20 63 68 65 63 6b 54 72 65 65 50 61 67      checkTreePag
17f2e 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20  e(pCheck, pgno, 
17f2f 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20  zContext);.  }. 
17f30 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20  .  /* Check for 
17f31 63 6f 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67  complete coverag
17f32 65 20 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20  e of the page.  
17f33 2a 2f 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67  */.  data = pPag
17f34 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20  e->aData;.  hdr 
17f35 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
17f36 65 74 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69  et;.  hit = sqli
17f37 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70  te3PageMalloc( p
17f38 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a  Bt->pageSize );.
17f39 20 20 69 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a    if( hit==0 ){.
17f3a 20 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c      pCheck->mall
17f3b 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
17f3c 7d 65 6c 73 65 7b 0a 20 20 20 20 75 31 36 20 63  }else{.    u16 c
17f3d 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3d 20 67  ontentOffset = g
17f3e 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
17f3f 72 2b 35 5d 29 3b 0a 20 20 20 20 69 66 20 28 63  r+5]);.    if (c
17f40 6f 6e 74 65 6e 74 4f 66 66 73 65 74 20 3e 20 75  ontentOffset > u
17f41 73 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 20 20  sableSize) {.   
17f42 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
17f43 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20  g(pCheck, 0, .  
17f44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f45 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64     "Corruption d
17f46 65 74 65 63 74 65 64 20 69 6e 20 68 65 61 64 65  etected in heade
17f47 72 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 50  r on page %d",iP
17f48 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 67 6f  age,0);.      go
17f49 74 6f 20 63 68 65 63 6b 5f 70 61 67 65 5f 61 62  to check_page_ab
17f4a 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  ort;.    }.    m
17f4b 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74 65 6e  emset(hit+conten
17f4c 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73 61 62  tOffset, 0, usab
17f4d 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 4f 66  leSize-contentOf
17f4e 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d 73 65  fset);.    memse
17f4f 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74 65 6e  t(hit, 1, conten
17f50 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6e 43  tOffset);.    nC
17f51 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26  ell = get2byte(&
17f52 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20  data[hdr+3]);.  
17f53 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20 68 64    cellStart = hd
17f54 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65  r + 12 - 4*pPage
17f55 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f 72 28  ->leaf;.    for(
17f56 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b  i=0; i<nCell; i+
17f57 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 63  +){.      int pc
17f58 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
17f59 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a 32 5d  a[cellStart+i*2]
17f5a 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73 69 7a  );.      u16 siz
17f5b 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20 20 20  e = 1024;.      
17f5c 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28  int j;.      if(
17f5d 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20   pc<=usableSize 
17f5e 29 7b 0a 20 20 20 20 20 20 20 20 73 69 7a 65 20  ){.        size 
17f5f 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  = cellSizePtr(pP
17f60 61 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b  age, &data[pc]);
17f61 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
17f62 66 28 20 28 70 63 2b 73 69 7a 65 2d 31 29 3e 3d  f( (pc+size-1)>=
17f63 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 70 63  usableSize || pc
17f64 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  <0 ){.        ch
17f65 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
17f66 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20  eck, 0, .       
17f67 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e       "Corruption
17f68 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c   detected in cel
17f69 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22  l %d on page %d"
17f6a 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20  ,i,iPage,0);.   
17f6b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17f6c 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d    for(j=pc+size-
17f6d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68  1; j>=pc; j--) h
17f6e 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d  it[j]++;.      }
17f6f 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 63  .    }.    for(c
17f70 6e 74 3d 30 2c 20 69 3d 67 65 74 32 62 79 74 65  nt=0, i=get2byte
17f71 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 20  (&data[hdr+1]); 
17f72 69 3e 30 20 26 26 20 69 3c 75 73 61 62 6c 65 53  i>0 && i<usableS
17f73 69 7a 65 20 26 26 20 63 6e 74 3c 31 30 30 30 30  ize && cnt<10000
17f74 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 63 6e  ; .           cn
17f75 74 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  t++){.      int 
17f76 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28  size = get2byte(
17f77 26 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20 20 20  &data[i+2]);.   
17f78 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
17f79 69 66 28 20 28 69 2b 73 69 7a 65 2d 31 29 3e 3d  if( (i+size-1)>=
17f7a 75 73 61 62 6c 65 53 69 7a 65 20 7c 7c 20 69 3c  usableSize || i<
17f7b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65  0 ){.        che
17f7c 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
17f7d 63 6b 2c 20 30 2c 20 20 0a 20 20 20 20 20 20 20  ck, 0,  .       
17f7e 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e       "Corruption
17f7f 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c   detected in cel
17f80 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22  l %d on page %d"
17f81 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20  ,i,iPage,0);.   
17f82 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17f83 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31    for(j=i+size-1
17f84 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74  ; j>=i; j--) hit
17f85 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  [j]++;.      }. 
17f86 20 20 20 20 20 69 20 3d 20 67 65 74 32 62 79 74       i = get2byt
17f87 65 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20 20 20  e(&data[i]);.   
17f88 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74   }.    for(i=cnt
17f89 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a 65  =0; i<usableSize
17f8a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
17f8b 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20  ( hit[i]==0 ){. 
17f8c 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20         cnt++;.  
17f8d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 69      }else if( hi
17f8e 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  t[i]>1 ){.      
17f8f 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
17f90 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20  (pCheck, 0,.    
17f91 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65 20        "Multiple 
17f92 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 25 64  uses for byte %d
17f93 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69 2c   of page %d", i,
17f94 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20   iPage);.       
17f95 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
17f96 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e      }.    if( cn
17f97 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20 29  t!=data[hdr+7] )
17f98 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70  {.      checkApp
17f99 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30  endMsg(pCheck, 0
17f9a 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 46 72  , .          "Fr
17f9b 61 67 6d 65 6e 74 65 64 20 73 70 61 63 65 20 69  agmented space i
17f9c 73 20 25 64 20 62 79 74 65 20 72 65 70 6f 72 74  s %d byte report
17f9d 65 64 20 61 73 20 25 64 20 6f 6e 20 70 61 67 65  ed as %d on page
17f9e 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %d",.          
17f9f 63 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d  cnt, data[hdr+7]
17fa0 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  , iPage);.    }.
17fa1 20 20 7d 0a 63 68 65 63 6b 5f 70 61 67 65 5f 61    }.check_page_a
17fa2 62 6f 72 74 3a 0a 20 20 69 66 20 28 68 69 74 29  bort:.  if (hit)
17fa3 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65   sqlite3PageFree
17fa4 28 68 69 74 29 3b 0a 0a 20 20 72 65 6c 65 61 73  (hit);..  releas
17fa5 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
17fa6 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a  return depth+1;.
17fa7 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
17fa8 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
17fa9 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e  Y_CHECK */..#ifn
17faa 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
17fab 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a  INTEGRITY_CHECK.
17fac 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
17fad 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65  ne does a comple
17fae 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20  te check of the 
17faf 67 69 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65  given BTree file
17fb0 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a  .  aRoot[] is.**
17fb1 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67   an array of pag
17fb2 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20  es numbers were 
17fb3 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72  each page number
17fb4 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67   is the root pag
17fb5 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e  e of.** a table.
17fb6 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e    nRoot is the n
17fb7 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
17fb8 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a   in aRoot..**.**
17fb9 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65   Write the numbe
17fba 72 20 6f 66 20 65 72 72 6f 72 20 73 65 65 6e 20  r of error seen 
17fbb 69 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78 63 65  in *pnErr.  Exce
17fbc 70 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f  pt for some memo
17fbd 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  ry.** allocation
17fbe 20 65 72 72 6f 72 73 2c 20 20 61 6e 20 65 72 72   errors,  an err
17fbf 6f 72 20 6d 65 73 73 61 67 65 20 68 65 6c 64 20  or message held 
17fc0 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  in memory obtain
17fc1 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f  ed from.** mallo
17fc2 63 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  c is returned if
17fc3 20 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a   *pnErr is non-z
17fc4 65 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d  ero.  If *pnErr=
17fc5 3d 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a  =0 then NULL is.
17fc6 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66  ** returned.  If
17fc7 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
17fc8 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
17fc9 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  s, NULL is retur
17fca 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
17fcb 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
17fcc 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69  ite3BtreeIntegri
17fcd 74 79 43 68 65 63 6b 28 0a 20 20 42 74 72 65 65  tyCheck(.  Btree
17fce 20 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68 65 20   *p,     /* The 
17fcf 62 74 72 65 65 20 74 6f 20 62 65 20 63 68 65 63  btree to be chec
17fd0 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52  ked */.  int *aR
17fd1 6f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72  oot,   /* An arr
17fd2 61 79 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73  ay of root pages
17fd3 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 69 6e 64   numbers for ind
17fd4 69 76 69 64 75 61 6c 20 74 72 65 65 73 20 2a 2f  ividual trees */
17fd5 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20  .  int nRoot,   
17fd6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
17fd7 74 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 5b 5d  tries in aRoot[]
17fd8 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 2c   */.  int mxErr,
17fd9 20 20 20 20 2f 2a 20 53 74 6f 70 20 72 65 70 6f      /* Stop repo
17fda 72 74 69 6e 67 20 65 72 72 6f 72 73 20 61 66 74  rting errors aft
17fdb 65 72 20 74 68 69 73 20 6d 61 6e 79 20 2a 2f 0a  er this many */.
17fdc 20 20 69 6e 74 20 2a 70 6e 45 72 72 20 20 20 20    int *pnErr    
17fdd 2f 2a 20 57 72 69 74 65 20 6e 75 6d 62 65 72 20  /* Write number 
17fde 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e 20 74  of errors seen t
17fdf 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  o this variable 
17fe0 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a  */.){.  Pgno i;.
17fe1 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20 49 6e    int nRef;.  In
17fe2 74 65 67 72 69 74 79 43 6b 20 73 43 68 65 63 6b  tegrityCk sCheck
17fe3 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
17fe4 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 63 68  t = p->pBt;.  ch
17fe5 61 72 20 7a 45 72 72 5b 31 30 30 5d 3b 0a 0a 20  ar zErr[100];.. 
17fe6 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
17fe7 65 72 28 70 29 3b 0a 20 20 6e 52 65 66 20 3d 20  er(p);.  nRef = 
17fe8 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
17fe9 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
17fea 29 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 42 74 72  );.  if( lockBtr
17feb 65 65 57 69 74 68 52 65 74 72 79 28 70 29 21 3d  eeWithRetry(p)!=
17fec 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
17fed 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20   *pnErr = 1;.   
17fee 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
17fef 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72  ve(p);.    retur
17ff0 6e 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  n sqlite3DbStrDu
17ff1 70 28 30 2c 20 22 63 61 6e 6e 6f 74 20 61 63 71  p(0, "cannot acq
17ff2 75 69 72 65 20 61 20 72 65 61 64 20 6c 6f 63 6b  uire a read lock
17ff3 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
17ff4 22 29 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b  ");.  }.  sCheck
17ff5 2e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 73 43  .pBt = pBt;.  sC
17ff6 68 65 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 42  heck.pPager = pB
17ff7 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73 43 68  t->pPager;.  sCh
17ff8 65 63 6b 2e 6e 50 61 67 65 20 3d 20 70 61 67 65  eck.nPage = page
17ff9 72 50 61 67 65 63 6f 75 6e 74 28 73 43 68 65 63  rPagecount(sChec
17ffa 6b 2e 70 42 74 29 3b 0a 20 20 73 43 68 65 63 6b  k.pBt);.  sCheck
17ffb 2e 6d 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a  .mxErr = mxErr;.
17ffc 20 20 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20    sCheck.nErr = 
17ffd 30 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c  0;.  sCheck.mall
17ffe 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20  ocFailed = 0;.  
17fff 2a 70 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66  *pnErr = 0;.  if
18000 28 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d  ( sCheck.nPage==
18001 30 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42  0 ){.    unlockB
18002 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74  treeIfUnused(pBt
18003 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
18004 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20  reeLeave(p);.   
18005 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
18006 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20   sCheck.anRef = 
18007 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28  sqlite3Malloc( (
18008 73 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a  sCheck.nPage+1)*
18009 73 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e  sizeof(sCheck.an
1800a 52 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  Ref[0]) );.  if(
1800b 20 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29   !sCheck.anRef )
1800c 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65  {.    unlockBtre
1800d 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
1800e 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a      *pnErr = 1;.
1800f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
18010 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65  Leave(p);.    re
18011 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f  turn 0;.  }.  fo
18012 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b  r(i=0; i<=sCheck
18013 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43  .nPage; i++){ sC
18014 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20  heck.anRef[i] = 
18015 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49  0; }.  i = PENDI
18016 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
18017 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 65  );.  if( i<=sChe
18018 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20  ck.nPage ){.    
18019 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20  sCheck.anRef[i] 
1801a 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 1;.  }.  sqlit
1801b 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26  e3StrAccumInit(&
1801c 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a  sCheck.errMsg, z
1801d 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72  Err, sizeof(zErr
1801e 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a  ), 20000);..  /*
1801f 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67   Check the integ
18020 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65  rity of the free
18021 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63  list.  */.  chec
18022 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 31  kList(&sCheck, 1
18023 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d  , get4byte(&pBt-
18024 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
18025 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  2]),.           
18026 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e   get4byte(&pBt->
18027 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
18028 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 69  ]), "Main freeli
18029 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68  st: ");..  /* Ch
1802a 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 6c  eck all the tabl
1802b 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  es..  */.  for(i
1802c 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74  =0; (int)i<nRoot
1802d 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72   && sCheck.mxErr
1802e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
1802f 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f  aRoot[i]==0 ) co
18030 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20  ntinue;.#ifndef 
18031 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
18032 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70  VACUUM.    if( p
18033 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26  Bt->autoVacuum &
18034 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a  & aRoot[i]>1 ){.
18035 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
18036 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74  p(&sCheck, aRoot
18037 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54  [i], PTRMAP_ROOT
18038 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20  PAGE, 0, 0);.   
18039 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68   }.#endif.    ch
1803a 65 63 6b 54 72 65 65 50 61 67 65 28 26 73 43 68  eckTreePage(&sCh
1803b 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22  eck, aRoot[i], "
1803c 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f 6f  List of tree roo
1803d 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f  ts: ");.  }..  /
1803e 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 72  * Make sure ever
1803f 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 69  y page in the fi
18040 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64  le is referenced
18041 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b  .  */.  for(i=1;
18042 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65   i<=sCheck.nPage
18043 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 72   && sCheck.mxErr
18044 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53  ; i++){.#ifdef S
18045 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
18046 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 43  ACUUM.    if( sC
18047 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30  heck.anRef[i]==0
18048 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41   ){.      checkA
18049 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b  ppendMsg(&sCheck
1804a 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 73  , 0, "Page %d is
1804b 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 29   never used", i)
1804c 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20  ;.    }.#else.  
1804d 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61    /* If the data
1804e 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75  base supports au
1804f 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20  to-vacuum, make 
18050 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 63  sure no tables c
18051 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65  ontain.    ** re
18052 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e  ferences to poin
18053 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20  ter-map pages.. 
18054 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 43     */.    if( sC
18055 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30  heck.anRef[i]==0
18056 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 52   && .       (PTR
18057 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20  MAP_PAGENO(pBt, 
18058 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61  i)!=i || !pBt->a
18059 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 20  utoVacuum) ){.  
1805a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
1805b 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22  sg(&sCheck, 0, "
1805c 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72  Page %d is never
1805d 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20   used", i);.    
1805e 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b  }.    if( sCheck
1805f 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20  .anRef[i]!=0 && 
18060 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f  .       (PTRMAP_
18061 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d  PAGENO(pBt, i)==
18062 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 61  i && pBt->autoVa
18063 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63  cuum) ){.      c
18064 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73  heckAppendMsg(&s
18065 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74  Check, 0, "Point
18066 65 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 69  er map page %d i
18067 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 69  s referenced", i
18068 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
18069 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
1806a 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 69  ure this analysi
1806b 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 20  s did not leave 
1806c 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 65  any unref() page
1806d 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20  s..  ** This is 
1806e 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73  an internal cons
1806f 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 61  istency check; a
18070 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63  n integrity chec
18071 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e  k.  ** of the in
18072 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 0a 20  tegrity check.. 
18073 20 2a 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65   */.  unlockBtre
18074 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
18075 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65 66    if( NEVER(nRef
18076 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72   != sqlite3Pager
18077 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50  Refcount(pBt->pP
18078 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63 68  ager)) ){.    ch
18079 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43  eckAppendMsg(&sC
1807a 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20  heck, 0, .      
1807b 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67  "Outstanding pag
1807c 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72 6f  e count goes fro
1807d 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 69 6e  m %d to %d durin
1807e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 22  g this analysis"
1807f 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73 71  ,.      nRef, sq
18080 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
18081 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 0a  nt(pBt->pPager).
18082 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a      );.  }..  /*
18083 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20 72   Clean  up and r
18084 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20 20  eport errors..  
18085 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  */.  sqlite3Btre
18086 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71 6c  eLeave(p);.  sql
18087 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 63 6b  ite3_free(sCheck
18088 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20 73  .anRef);.  if( s
18089 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c  Check.mallocFail
1808a 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
1808b 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 26  3StrAccumReset(&
1808c 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a  sCheck.errMsg);.
1808d 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 68      *pnErr = sCh
1808e 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 20  eck.nErr+1;.    
1808f 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
18090 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b 2e  *pnErr = sCheck.
18091 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68 65  nErr;.  if( sChe
18092 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71 6c  ck.nErr==0 ) sql
18093 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65  ite3StrAccumRese
18094 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67  t(&sCheck.errMsg
18095 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
18096 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73  te3StrAccumFinis
18097 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67  h(&sCheck.errMsg
18098 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
18099 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
1809a 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f  RITY_CHECK */../
1809b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1809c 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66  full pathname of
1809d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
1809e 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1809f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 66  *.** The pager f
180a0 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72  ilename is invar
180a1 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20  iant as long as 
180a2 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20  the pager is.** 
180a3 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61  open so it is sa
180a4 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 74  fe to access wit
180a5 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 65  hout the BtShare
180a6 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49  d mutex..*/.SQLI
180a7 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
180a8 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
180a9 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 42  reeGetFilename(B
180aa 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
180ab 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67  rt( p->pBt->pPag
180ac 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  er!=0 );.  retur
180ad 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69  n sqlite3PagerFi
180ae 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70  lename(p->pBt->p
180af 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Pager);.}../*.**
180b0 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 74 68   Return the path
180b1 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  name of the jour
180b2 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 68 69  nal file for thi
180b3 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  s database. The 
180b4 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20  return.** value 
180b5 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
180b6 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 61  is the same rega
180b7 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
180b8 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  r the journal fi
180b9 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63  le.** has been c
180ba 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a 2a  reated or not..*
180bb 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6a  *.** The pager j
180bc 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20  ournal filename 
180bd 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73 20  is invariant as 
180be 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67 65  long as the page
180bf 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20  r is.** open so 
180c0 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 63  it is safe to ac
180c1 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68 65  cess without the
180c2 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 2e   BtShared mutex.
180c3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
180c4 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
180c5 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f  qlite3BtreeGetJo
180c6 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20  urnalname(Btree 
180c7 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
180c8 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d 30  ->pBt->pPager!=0
180c9 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   );.  return sql
180ca 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
180cb 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61  name(p->pBt->pPa
180cc 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ger);.}../*.** R
180cd 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
180ce 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
180cf 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51  is active..*/.SQ
180d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
180d1 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
180d2 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29  nTrans(Btree *p)
180d3 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30  {.  assert( p==0
180d4 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
180d5 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
180d6 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  tex) );.  return
180d7 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72 61   (p && (p->inTra
180d8 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 29  ns==TRANS_WRITE)
180d9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
180da 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61  rn non-zero if a
180db 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65 29   read (or write)
180dc 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
180dd 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54  active..*/.SQLIT
180de 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
180df 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65  lite3BtreeIsInRe
180e0 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70  adTrans(Btree *p
180e1 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29  ){.  assert( p )
180e2 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
180e3 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
180e4 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
180e5 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54 72    return p->inTr
180e6 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 3b  ans!=TRANS_NONE;
180e7 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
180e8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
180e9 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 74  reeIsInBackup(Bt
180ea 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
180eb 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74  t( p );.  assert
180ec 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
180ed 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
180ee 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  x) );.  return p
180ef 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d 0a  ->nBackup!=0;.}.
180f0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
180f1 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70  tion returns a p
180f2 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f 62  ointer to a blob
180f3 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63   of memory assoc
180f4 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 20  iated with.** a 
180f5 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 74  single shared-bt
180f6 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 20  ree. The memory 
180f7 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65 6e  is used by clien
180f8 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20 6f  t code for its o
180f9 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 28  wn.** purposes (
180fa 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20  for example, to 
180fb 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 76  store a high-lev
180fc 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69  el schema associ
180fd 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68  ated with .** th
180fe 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29 2e  e shared-btree).
180ff 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   The btree layer
18100 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65 6e   manages referen
18101 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 75  ce counting issu
18102 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  es..**.** The fi
18103 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73  rst time this is
18104 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61   called on a sha
18105 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 65  red-btree, nByte
18106 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  s bytes of memor
18107 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74  y.** are allocat
18108 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20  ed, zeroed, and 
18109 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
1810a 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 68  caller. For each
1810b 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20   subsequent .** 
1810c 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 20  call the nBytes 
1810d 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67 6e  parameter is ign
1810e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e 74  ored and a point
1810f 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62  er to the same b
18110 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79  lob.** of memory
18111 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a   returned. .**.*
18112 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73 20  * If the nBytes 
18113 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 61  parameter is 0 a
18114 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d  nd the blob of m
18115 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 65  emory has not ye
18116 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61  t been.** alloca
18117 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 6e  ted, a null poin
18118 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
18119 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61 73   If the blob has
1811a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a   already been.**
1811b 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 69   allocated, it i
1811c 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e 6f  s returned as no
1811d 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 74  rmal..**.** Just
1811e 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61 72   before the shar
1811f 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f 73  ed-btree is clos
18120 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  ed, the function
18121 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 0a   passed as the .
18122 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65 6e  ** xFree argumen
18123 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72  t when the memor
18124 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73  y allocation was
18125 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 64   made is invoked
18126 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62   on the .** blob
18127 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65   of allocated me
18128 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63 74  mory. This funct
18129 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63  ion should not c
1812a 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 65  all sqlite3_free
1812b 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 6d  ().** on the mem
1812c 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20 6c  ory, the btree l
1812d 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e 0a  ayer does that..
1812e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1812f 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42  E void *sqlite3B
18130 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 65  treeSchema(Btree
18131 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c   *p, int nBytes,
18132 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76 6f   void(*xFree)(vo
18133 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61 72  id *)){.  BtShar
18134 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
18135 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
18136 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20  Enter(p);.  if( 
18137 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 26 26  !pBt->pSchema &&
18138 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 70   nBytes ){.    p
18139 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 71  Bt->pSchema = sq
1813a 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
1813b 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 42 74  nBytes);.    pBt
1813c 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 3d 20  ->xFreeSchema = 
1813d 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71 6c  xFree;.  }.  sql
1813e 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1813f 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 74 2d  );.  return pBt-
18140 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a 0a  >pSchema;.}../*.
18141 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
18142 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41  _LOCKED_SHAREDCA
18143 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 20 75  CHE if another u
18144 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ser of the same 
18145 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 65 65  shared .** btree
18146 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74   as the argument
18147 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 6e   handle holds an
18148 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
18149 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  on the .** sqlit
1814a 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  e_master table. 
1814b 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 45  Otherwise SQLITE
1814c 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  _OK..*/.SQLITE_P
1814d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1814e 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63  e3BtreeSchemaLoc
1814f 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ked(Btree *p){. 
18150 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
18151 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
18152 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
18153 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
18154 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
18155 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65   rc = queryShare
18156 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
18157 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  p, MASTER_ROOT, 
18158 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61 73  READ_LOCK);.  as
18159 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1815a 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  _OK || rc==SQLIT
1815b 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
1815c 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 74 65  ACHE );.  sqlite
1815d 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1815e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1815f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18160 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
18161 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61  E./*.** Obtain a
18162 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62   lock on the tab
18163 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61  le whose root pa
18164 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68 65  ge is iTab.  The
18165 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77 72  .** lock is a wr
18166 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57 72  ite lock if isWr
18167 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65 20  itelock is true 
18168 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a 2a  or a read lock.*
18169 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73 65  * if it is false
1816a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1816b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1816c 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74  treeLockTable(Bt
1816d 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62  ree *p, int iTab
1816e 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b  , u8 isWriteLock
1816f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
18170 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
18171 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20  ->sharable ){.  
18172 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d 20    u8 lockType = 
18173 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57 72  READ_LOCK + isWr
18174 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 73 73  iteLock;.    ass
18175 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b 2b 31  ert( READ_LOCK+1
18176 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a  ==WRITE_LOCK );.
18177 20 20 20 20 61 73 73 65 72 74 28 20 69 73 57 72      assert( isWr
18178 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73  iteLock==0 || is
18179 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a  WriteLock==1 );.
1817a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1817b 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72 63  Enter(p);.    rc
1817c 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61   = querySharedCa
1817d 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  cheTableLock(p, 
1817e 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 3b  iTab, lockType);
1817f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
18180 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18181 72 63 20 3d 20 73 65 74 53 68 61 72 65 64 43 61  rc = setSharedCa
18182 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  cheTableLock(p, 
18183 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65 29 3b  iTab, lockType);
18184 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
18185 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
18186 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
18187 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
18188 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18189 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 41  INCRBLOB./*.** A
1818a 72 67 75 6d 65 6e 74 20 70 43 73 72 20 6d 75 73  rgument pCsr mus
1818b 74 20 62 65 20 61 20 63 75 72 73 6f 72 20 6f 70  t be a cursor op
1818c 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67  ened for writing
1818d 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54 4b 45   on an .** INTKE
1818e 59 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c  Y table currentl
1818f 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20  y pointing at a 
18190 76 61 6c 69 64 20 74 61 62 6c 65 20 65 6e 74 72  valid table entr
18191 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  y. .** This func
18192 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 74 68  tion modifies th
18193 65 20 64 61 74 61 20 73 74 6f 72 65 64 20 61 73  e data stored as
18194 20 70 61 72 74 20 6f 66 20 74 68 61 74 20 65 6e   part of that en
18195 74 72 79 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65  try..** Only the
18196 20 64 61 74 61 20 63 6f 6e 74 65 6e 74 20 6d 61   data content ma
18197 79 20 6f 6e 6c 79 20 62 65 20 6d 6f 64 69 66 69  y only be modifi
18198 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f  ed, it is not po
18199 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 63 68 61  ssible.** to cha
1819a 6e 67 65 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  nge the length o
1819b 66 20 74 68 65 20 64 61 74 61 20 73 74 6f 72 65  f the data store
1819c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1819d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1819e 42 74 72 65 65 50 75 74 44 61 74 61 28 42 74 43  BtreePutData(BtC
1819f 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32  ursor *pCsr, u32
181a0 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74   offset, u32 amt
181a1 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e  , void *z){.  in
181a2 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
181a3 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
181a4 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 73 73  x(pCsr) );.  ass
181a5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
181a6 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70 42  ex_held(pCsr->pB
181a7 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
181a8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 43 73   );.  assert(pCs
181a9 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  r->isIncrblobHan
181aa 64 6c 65 29 3b 0a 0a 20 20 72 65 73 74 6f 72 65  dle);..  restore
181ab 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
181ac 43 73 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Csr);.  assert( 
181ad 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55  pCsr->eState!=CU
181ae 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
181af 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e   );.  if( pCsr->
181b0 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56  eState!=CURSOR_V
181b1 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75  ALID ){.    retu
181b2 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b  rn SQLITE_ABORT;
181b3 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
181b4 20 73 6f 6d 65 20 70 72 65 63 6f 6e 64 69 74 69   some preconditi
181b5 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29  ons: .  **   (a)
181b6 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f   the cursor is o
181b7 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2c  pen for writing,
181b8 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65 72  .  **   (b) ther
181b9 65 20 69 73 20 6e 6f 20 72 65 61 64 2d 6c 6f 63  e is no read-loc
181ba 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 62  k on the table b
181bb 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20 61 6e  eing modified an
181bc 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65  d.  **   (c) the
181bd 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 61   cursor points a
181be 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20 6f 66  t a valid row of
181bf 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65   an intKey table
181c0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43  ..  */.  if( !pC
181c1 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20  sr->wrFlag ){.  
181c2 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
181c3 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20  READONLY;.  }.  
181c4 61 73 73 65 72 74 28 20 21 70 43 73 72 2d 3e 70  assert( !pCsr->p
181c5 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 0a 20 20  Bt->readOnly .  
181c6 20 20 20 20 20 20 20 20 26 26 20 70 43 73 72 2d          && pCsr-
181c7 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  >pBt->inTransact
181c8 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
181c9 20 29 3b 0a 20 20 72 63 20 3d 20 63 68 65 63 6b   );.  rc = check
181ca 46 6f 72 52 65 61 64 43 6f 6e 66 6c 69 63 74 73  ForReadConflicts
181cb 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c 20 70  (pCsr->pBtree, p
181cc 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70  Csr->pgnoRoot, p
181cd 43 73 72 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  Csr, 0);.  if( r
181ce 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
181cf 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
181d0 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20   pCur points to 
181d1 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  has a read lock 
181d2 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72  */.    assert( r
181d3 63 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  c==SQLITE_LOCKED
181d4 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a  _SHAREDCACHE );.
181d5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
181d6 20 7d 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65   }.  if( pCsr->e
181d7 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e  State==CURSOR_IN
181d8 56 41 4c 49 44 20 7c 7c 20 21 70 43 73 72 2d 3e  VALID || !pCsr->
181d9 61 70 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61  apPage[pCsr->iPa
181da 67 65 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  ge]->intKey ){. 
181db 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
181dc 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72  _ERROR;.  }..  r
181dd 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79 6c  eturn accessPayl
181de 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73 65 74  oad(pCsr, offset
181df 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64  , amt, (unsigned
181e0 20 63 68 61 72 20 2a 29 7a 2c 20 30 2c 20 31 29   char *)z, 0, 1)
181e1 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20  ;.}../* .** Set 
181e2 61 20 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63  a flag on this c
181e3 75 72 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74  ursor to cache t
181e4 68 65 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20  he locations of 
181e5 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a  pages from the .
181e6 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74  ** overflow list
181e7 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
181e8 20 72 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73   row. This is us
181e9 65 64 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70  ed by cursors op
181ea 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72  ened.** for incr
181eb 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20  emental blob IO 
181ec 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  only..**.** This
181ed 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61   function sets a
181ee 20 66 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20   flag only. The 
181ef 61 63 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61  actual page loca
181f0 74 69 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73  tion cache.** (s
181f1 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f  tored in BtCurso
181f2 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69  r.aOverflow[]) i
181f3 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  s allocated and 
181f4 75 73 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e  used by function
181f5 0a 2a 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61  .** accessPayloa
181f6 64 28 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20  d() (the worker 
181f7 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c  function for sql
181f8 69 74 65 33 42 74 72 65 65 44 61 74 61 28 29 20  ite3BtreeData() 
181f9 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74  and.** sqlite3Bt
181fa 72 65 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a  reePutData())..*
181fb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
181fc 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
181fd 65 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28  eeCacheOverflow(
181fe 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
181ff 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
18200 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
18201 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
18202 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
18203 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e  d(pCur->pBtree->
18204 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
18205 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73  assert(!pCur->is
18206 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b  IncrblobHandle);
18207 0a 20 20 61 73 73 65 72 74 28 21 70 43 75 72 2d  .  assert(!pCur-
18208 3e 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70  >aOverflow);.  p
18209 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48  Cur->isIncrblobH
1820a 61 6e 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e  andle = 1;.}.#en
1820b 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
1820c 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65  **** End of btre
1820d 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1820e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1820f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18210 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
18211 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
18212 62 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a  backup.c *******
18213 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18214 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18215 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20  ***/./*.** 2009 
18216 4a 61 6e 75 61 72 79 20 32 38 0a 2a 2a 0a 2a 2a  January 28.**.**
18217 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
18218 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
18219 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1821a 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1821b 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1821c 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1821d 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1821e 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1821f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
18220 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
18221 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
18222 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
18223 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
18224 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
18225 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
18226 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
18227 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
18228 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18229 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1822a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1822b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1822c 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
1822d 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1822e 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
1822f 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
18230 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 0a  3_backup_XXX() .
18231 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73  ** API functions
18232 20 61 6e 64 20 74 68 65 20 72 65 6c 61 74 65 64   and the related
18233 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a   features..**.**
18234 20 24 49 64 3a 20 62 61 63 6b 75 70 2e 63 2c 76   $Id: backup.c,v
18235 20 31 2e 31 33 20 32 30 30 39 2f 30 33 2f 31 36   1.13 2009/03/16
18236 20 31 33 3a 31 39 3a 33 36 20 64 61 6e 69 65 6c   13:19:36 daniel
18237 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
18238 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64  /* Macro to find
18239 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20   the minimum of 
1823a 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75  two numeric valu
1823b 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d  es..*/.#ifndef M
1823c 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28  IN.# define MIN(
1823d 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78  x,y) ((x)<(y)?(x
1823e 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f  ):(y)).#endif../
1823f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61  *.** Structure a
18240 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63  llocated for eac
18241 68 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  h backup operati
18242 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  on..*/.struct sq
18243 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 7b 0a 20  lite3_backup {. 
18244 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44   sqlite3* pDestD
18245 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73  b;        /* Des
18246 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
18247 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74  e handle */.  Bt
18248 72 65 65 20 2a 70 44 65 73 74 3b 20 20 20 20 20  ree *pDest;     
18249 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e         /* Destin
1824a 61 74 69 6f 6e 20 62 2d 74 72 65 65 20 66 69 6c  ation b-tree fil
1824b 65 20 2a 2f 0a 20 20 75 33 32 20 69 44 65 73 74  e */.  u32 iDest
1824c 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 20  Schema;         
1824d 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73 63 68 65  /* Original sche
1824e 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20 64 65 73  ma cookie in des
1824f 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  tination */.  in
18250 74 20 62 44 65 73 74 4c 6f 63 6b 65 64 3b 20 20  t bDestLocked;  
18251 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f         /* True o
18252 6e 63 65 20 61 20 77 72 69 74 65 2d 74 72 61 6e  nce a write-tran
18253 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20  saction is open 
18254 6f 6e 20 70 44 65 73 74 20 2a 2f 0a 0a 20 20 50  on pDest */..  P
18255 67 6e 6f 20 69 4e 65 78 74 3b 20 20 20 20 20 20  gno iNext;      
18256 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
18257 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
18258 78 74 20 73 6f 75 72 63 65 20 70 61 67 65 20 74  xt source page t
18259 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 73 71 6c 69  o copy */.  sqli
1825a 74 65 33 2a 20 70 53 72 63 44 62 3b 20 20 20 20  te3* pSrcDb;    
1825b 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64       /* Source d
1825c 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
1825d 2f 0a 20 20 42 74 72 65 65 20 2a 70 53 72 63 3b  /.  Btree *pSrc;
1825e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1825f 53 6f 75 72 63 65 20 62 2d 74 72 65 65 20 66 69  Source b-tree fi
18260 6c 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 3b  le */..  int rc;
18261 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18262 20 20 2f 2a 20 42 61 63 6b 75 70 20 70 72 6f 63    /* Backup proc
18263 65 73 73 20 65 72 72 6f 72 20 63 6f 64 65 20 2a  ess error code *
18264 2f 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20 74 77  /..  /* These tw
18265 6f 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  o variables are 
18266 73 65 74 20 62 79 20 65 76 65 72 79 20 63 61 6c  set by every cal
18267 6c 20 74 6f 20 62 61 63 6b 75 70 5f 73 74 65 70  l to backup_step
18268 28 29 2e 20 54 68 65 79 20 61 72 65 0a 20 20 2a  (). They are.  *
18269 2a 20 72 65 61 64 20 62 79 20 63 61 6c 6c 73 20  * read by calls 
1826a 74 6f 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  to backup_remain
1826b 69 6e 67 28 29 20 61 6e 64 20 62 61 63 6b 75 70  ing() and backup
1826c 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a 20 20  _pagecount()..  
1826d 2a 2f 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69  */.  Pgno nRemai
1826e 6e 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  ning;         /*
1826f 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
18270 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f   left to copy */
18271 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75  .  Pgno nPagecou
18272 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  nt;         /* T
18273 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
18274 61 67 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a  ages to copy */.
18275 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
18276 70 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e  p *pNext;   /* N
18277 65 78 74 20 62 61 63 6b 75 70 20 61 73 73 6f 63  ext backup assoc
18278 69 61 74 65 64 20 77 69 74 68 20 73 6f 75 72 63  iated with sourc
18279 65 20 70 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f  e pager */.};../
1827a 2a 0a 2a 2a 20 54 48 52 45 41 44 20 53 41 46 45  *.** THREAD SAFE
1827b 54 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20  TY NOTES:.**.** 
1827c 20 20 4f 6e 63 65 20 69 74 20 68 61 73 20 62 65    Once it has be
1827d 65 6e 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  en created using
1827e 20 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20   backup_init(), 
1827f 61 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33  a single sqlite3
18280 5f 62 61 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72  _backup.**   str
18281 75 63 74 75 72 65 20 6d 61 79 20 62 65 20 61 63  ucture may be ac
18282 63 65 73 73 65 64 20 76 69 61 20 74 77 6f 20 67  cessed via two g
18283 72 6f 75 70 73 20 6f 66 20 74 68 72 65 61 64 2d  roups of thread-
18284 73 61 66 65 20 65 6e 74 72 79 20 70 6f 69 6e 74  safe entry point
18285 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56  s:.**.**     * V
18286 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  ia the sqlite3_b
18287 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20  ackup_XXX() API 
18288 66 75 6e 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f  function backup_
18289 73 74 65 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20  step() and .**  
1828a 20 20 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69       backup_fini
1828b 73 68 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65  sh(). Both these
1828c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69   functions obtai
1828d 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74  n the source dat
1828e 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 68  abase.**       h
1828f 61 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20  andle mutex and 
18290 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69  the mutex associ
18291 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f  ated with the so
18292 75 72 63 65 20 42 74 53 68 61 72 65 64 20 0a 2a  urce BtShared .*
18293 2a 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72  *       structur
18294 65 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72  e, in that order
18295 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69  ..**.**     * Vi
18296 61 20 74 68 65 20 42 61 63 6b 75 70 55 70 64 61  a the BackupUpda
18297 74 65 28 29 20 61 6e 64 20 42 61 63 6b 75 70 52  te() and BackupR
18298 65 73 74 61 72 74 28 29 20 66 75 6e 63 74 69 6f  estart() functio
18299 6e 73 2c 20 77 68 69 63 68 20 61 72 65 0a 2a 2a  ns, which are.**
1829a 20 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 62         invoked b
1829b 79 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65  y the pager laye
1829c 72 20 74 6f 20 72 65 70 6f 72 74 20 76 61 72 69  r to report vari
1829d 6f 75 73 20 73 74 61 74 65 20 63 68 61 6e 67 65  ous state change
1829e 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68  s in.**       th
1829f 65 20 70 61 67 65 20 63 61 63 68 65 20 61 73 73  e page cache ass
182a0 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
182a1 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
182a2 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20  . The mutex.**  
182a3 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20       associated 
182a4 77 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20  with the source 
182a5 64 61 74 61 62 61 73 65 20 42 74 53 68 61 72 65  database BtShare
182a6 64 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c  d structure will
182a7 20 61 6c 77 61 79 73 20 0a 2a 2a 20 20 20 20 20   always .**     
182a8 20 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 65    be held when e
182a9 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 66  ither of these f
182aa 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76  unctions are inv
182ab 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68  oked..**.**   Th
182ac 65 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f  e other sqlite3_
182ad 62 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49  backup_XXX() API
182ae 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b   functions, back
182af 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61  up_remaining() a
182b0 6e 64 0a 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70  nd.**   backup_p
182b1 61 67 65 63 6f 75 6e 74 28 29 20 61 72 65 20 6e  agecount() are n
182b2 6f 74 20 74 68 72 65 61 64 2d 73 61 66 65 20 66  ot thread-safe f
182b3 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65  unctions. If the
182b4 79 20 61 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20  y are called.** 
182b5 20 20 77 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68    while some oth
182b6 65 72 20 74 68 72 65 61 64 20 69 73 20 63 61 6c  er thread is cal
182b7 6c 69 6e 67 20 62 61 63 6b 75 70 5f 73 74 65 70  ling backup_step
182b8 28 29 20 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e  () or backup_fin
182b9 69 73 68 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20  ish(),.**   the 
182ba 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20  values returned 
182bb 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 64 2e 20  may be invalid. 
182bc 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  There is no way 
182bd 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a  for a call to.**
182be 20 20 20 42 61 63 6b 75 70 55 70 64 61 74 65 28     BackupUpdate(
182bf 29 20 6f 72 20 42 61 63 6b 75 70 52 65 73 74 61  ) or BackupResta
182c0 72 74 28 29 20 74 6f 20 69 6e 74 65 72 66 65 72  rt() to interfer
182c1 65 20 77 69 74 68 20 62 61 63 6b 75 70 5f 72 65  e with backup_re
182c2 6d 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f  maining().**   o
182c3 72 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75  r backup_pagecou
182c4 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65  nt()..**.**   De
182c5 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53  pending on the S
182c6 51 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74  QLite configurat
182c7 69 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73  ion, the databas
182c8 65 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72  e handles and/or
182c9 0a 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20  .**   the Btree 
182ca 6f 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65  objects may have
182cb 20 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78   their own mutex
182cc 65 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20  es that require 
182cd 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f  locking..**   No
182ce 6e 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65  n-sharable Btree
182cf 73 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  s (in-memory dat
182d0 61 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d 70  abases for examp
182d1 6c 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65  le), do not have
182d2 0a 2a 2a 20 20 20 61 73 73 6f 63 69 61 74 65 64  .**   associated
182d3 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a   mutexes..*/../*
182d4 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
182d5 6e 74 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69  nter correspondi
182d6 6e 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 7a  ng to database z
182d7 44 62 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c  Db (i.e. "main",
182d8 20 22 74 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63   "temp").** in c
182d9 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
182da 20 70 44 62 2e 20 49 66 20 73 75 63 68 20 61 20   pDb. If such a 
182db 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20  database cannot 
182dc 62 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e  be found, return
182dd 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  .** a NULL point
182de 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20  er and write an 
182df 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f  error message to
182e0 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a   pErrorDb..**.**
182e1 20 49 66 20 74 68 65 20 22 74 65 6d 70 22 20 64   If the "temp" d
182e2 61 74 61 62 61 73 65 20 69 73 20 72 65 71 75 65  atabase is reque
182e3 73 74 65 64 2c 20 69 74 20 6d 61 79 20 6e 65 65  sted, it may nee
182e4 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 62  d to be opened b
182e5 79 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74  y this .** funct
182e6 69 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ion. If an error
182e7 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f   occurs while do
182e8 69 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e 20 30  ing so, return 0
182e9 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 0a 2a   and write an .*
182ea 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  * error message 
182eb 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a  to pErrorDb..*/.
182ec 73 74 61 74 69 63 20 42 74 72 65 65 20 2a 66 69  static Btree *fi
182ed 6e 64 42 74 72 65 65 28 73 71 6c 69 74 65 33 20  ndBtree(sqlite3 
182ee 2a 70 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74  *pErrorDb, sqlit
182ef 65 33 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63  e3 *pDb, const c
182f0 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74  har *zDb){.  int
182f1 20 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   i = sqlite3Find
182f2 44 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29  DbName(pDb, zDb)
182f3 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 31 20 29 7b  ;..  if( i==1 ){
182f4 0a 20 20 20 20 50 61 72 73 65 20 73 50 61 72 73  .    Parse sPars
182f5 65 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73  e;.    memset(&s
182f6 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66  Parse, 0, sizeof
182f7 28 73 50 61 72 73 65 29 29 3b 0a 20 20 20 20 73  (sParse));.    s
182f8 50 61 72 73 65 2e 64 62 20 3d 20 70 44 62 3b 0a  Parse.db = pDb;.
182f9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
182fa 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28  penTempDatabase(
182fb 26 73 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  &sParse) ){.    
182fc 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c    sqlite3ErrorCl
182fd 65 61 72 28 26 73 50 61 72 73 65 29 3b 0a 20 20  ear(&sParse);.  
182fe 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
182ff 28 70 45 72 72 6f 72 44 62 2c 20 73 50 61 72 73  (pErrorDb, sPars
18300 65 2e 72 63 2c 20 22 25 73 22 2c 20 73 50 61 72  e.rc, "%s", sPar
18301 73 65 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  se.zErrMsg);.   
18302 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
18303 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73   }.    assert( s
18304 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 3d 3d 30  Parse.zErrMsg==0
18305 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69   );.  }..  if( i
18306 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <0 ){.    sqlite
18307 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c  3Error(pErrorDb,
18308 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22   SQLITE_ERROR, "
18309 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65  unknown database
1830a 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20   %s", zDb);.    
1830b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
1830c 20 72 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62   return pDb->aDb
1830d 5b 69 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  [i].pBt;.}../*.*
1830e 2a 20 43 72 65 61 74 65 20 61 6e 20 73 71 6c 69  * Create an sqli
1830f 74 65 33 5f 62 61 63 6b 75 70 20 70 72 6f 63 65  te3_backup proce
18310 73 73 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63  ss to copy the c
18311 6f 6e 74 65 6e 74 73 20 6f 66 20 7a 53 72 63 44  ontents of zSrcD
18312 62 20 66 72 6f 6d 0a 2a 2a 20 63 6f 6e 6e 65 63  b from.** connec
18313 74 69 6f 6e 20 68 61 6e 64 6c 65 20 70 53 72 63  tion handle pSrc
18314 44 62 20 74 6f 20 7a 44 65 73 74 44 62 20 69 6e  Db to zDestDb in
18315 20 70 44 65 73 74 44 62 2e 20 49 66 20 73 75 63   pDestDb. If suc
18316 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a  cessful, return.
18317 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
18318 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65 33 5f  the new sqlite3_
18319 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 0a 2a  backup object..*
1831a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
1831b 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73   occurs, NULL is
1831c 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e   returned and an
1831d 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
1831e 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a  error message.**
1831f 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62   stored in datab
18320 61 73 65 20 68 61 6e 64 6c 65 20 70 44 65 73 74  ase handle pDest
18321 44 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  Db..*/.SQLITE_AP
18322 49 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  I sqlite3_backup
18323 20 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70   *sqlite3_backup
18324 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33  _init(.  sqlite3
18325 2a 20 70 44 65 73 74 44 62 2c 20 20 20 20 20 20  * pDestDb,      
18326 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18327 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 77 72  * Database to wr
18328 69 74 65 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73  ite to */.  cons
18329 74 20 63 68 61 72 20 2a 7a 44 65 73 74 44 62 2c  t char *zDestDb,
1832a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1832b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
1832c 61 62 61 73 65 20 77 69 74 68 69 6e 20 70 44 65  abase within pDe
1832d 73 74 44 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65  stDb */.  sqlite
1832e 33 2a 20 70 53 72 63 44 62 2c 20 20 20 20 20 20  3* pSrcDb,      
1832f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18330 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
18331 65 63 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 66  ection to read f
18332 72 6f 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  rom */.  const c
18333 68 61 72 20 2a 7a 53 72 63 44 62 20 20 20 20 20  har *zSrcDb     
18334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18335 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
18336 73 65 20 77 69 74 68 69 6e 20 70 53 72 63 44 62  se within pSrcDb
18337 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
18338 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20  _backup *p;     
18339 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1833a 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
1833b 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20  n */..  /* Lock 
1833c 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
1833d 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20  ase handle. The 
1833e 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
1833f 62 61 73 65 0a 20 20 2a 2a 20 68 61 6e 64 6c 65  base.  ** handle
18340 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 69   is not locked i
18341 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  n this routine, 
18342 62 75 74 20 69 74 20 69 73 20 6c 6f 63 6b 65 64  but it is locked
18343 20 69 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33   in.  ** sqlite3
18344 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
18345 54 68 65 20 75 73 65 72 20 69 73 20 72 65 71 75  The user is requ
18346 69 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74  ired to ensure t
18347 68 61 74 20 6e 6f 0a 20 20 2a 2a 20 6f 74 68 65  hat no.  ** othe
18348 72 20 74 68 72 65 61 64 20 61 63 63 65 73 73 65  r thread accesse
18349 73 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  s the destinatio
1834a 6e 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65  n handle for the
1834b 20 64 75 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 6f   duration.  ** o
1834c 66 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65  f the backup ope
1834d 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74  ration.  Any att
1834e 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20  empt to use the 
1834f 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a  destination.  **
18350 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
18351 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63  tion while a bac
18352 6b 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65  kup is in progre
18353 73 73 20 6d 61 79 20 63 61 75 73 65 0a 20 20 2a  ss may cause.  *
18354 2a 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20  * a malfunction 
18355 6f 72 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 20  or a deadlock.. 
18356 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
18357 74 65 78 5f 65 6e 74 65 72 28 70 53 72 63 44 62  tex_enter(pSrcDb
18358 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69  ->mutex);.  sqli
18359 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1835a 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b  pDestDb->mutex);
1835b 0a 0a 20 20 69 66 28 20 70 53 72 63 44 62 3d 3d  ..  if( pSrcDb==
1835c 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73  pDestDb ){.    s
1835d 71 6c 69 74 65 33 45 72 72 6f 72 28 0a 20 20 20  qlite3Error(.   
1835e 20 20 20 20 20 70 44 65 73 74 44 62 2c 20 53 51       pDestDb, SQ
1835f 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f 75  LITE_ERROR, "sou
18360 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74  rce and destinat
18361 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 69 73 74  ion must be dist
18362 69 6e 63 74 22 0a 20 20 20 20 29 3b 0a 20 20 20  inct".    );.   
18363 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20   p = 0;.  }else 
18364 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  {.    /* Allocat
18365 65 20 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65  e space for a ne
18366 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  w sqlite3_backup
18367 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70   object */.    p
18368 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 61 63 6b   = (sqlite3_back
18369 75 70 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  up *)sqlite3_mal
1836a 6c 6f 63 28 73 69 7a 65 6f 66 28 73 71 6c 69 74  loc(sizeof(sqlit
1836b 65 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20  e3_backup));.   
1836c 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20   if( !p ){.     
1836d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 44   sqlite3Error(pD
1836e 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f  estDb, SQLITE_NO
1836f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  MEM, 0);.    }. 
18370 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
18371 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63 65  allocation succe
18372 65 64 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74  eded, populate t
18373 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 2a  he new object. *
18374 2f 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  /.  if( p ){.   
18375 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69   memset(p, 0, si
18376 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63  zeof(sqlite3_bac
18377 6b 75 70 29 29 3b 0a 20 20 20 20 70 2d 3e 70 53  kup));.    p->pS
18378 72 63 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70  rc = findBtree(p
18379 44 65 73 74 44 62 2c 20 70 53 72 63 44 62 2c 20  DestDb, pSrcDb, 
1837a 7a 53 72 63 44 62 29 3b 0a 20 20 20 20 70 2d 3e  zSrcDb);.    p->
1837b 70 44 65 73 74 20 3d 20 66 69 6e 64 42 74 72 65  pDest = findBtre
1837c 65 28 70 44 65 73 74 44 62 2c 20 70 44 65 73 74  e(pDestDb, pDest
1837d 44 62 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 20  Db, zDestDb);.  
1837e 20 20 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 70    p->pDestDb = p
1837f 44 65 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e 70  DestDb;.    p->p
18380 53 72 63 44 62 20 3d 20 70 53 72 63 44 62 3b 0a  SrcDb = pSrcDb;.
18381 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31      p->iNext = 1
18382 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 2d  ;..    if( 0==p-
18383 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e 70  >pSrc || 0==p->p
18384 44 65 73 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Dest ){.      /*
18385 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29 20 6f   One (or both) o
18386 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61  f the named data
18387 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20 65 78  bases did not ex
18388 69 73 74 2e 20 41 6e 20 65 72 72 6f 72 20 68 61  ist. An error ha
18389 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61  s.      ** alrea
1838a 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  dy been written 
1838b 69 6e 74 6f 20 74 68 65 20 70 44 65 73 74 44 62  into the pDestDb
1838c 20 68 61 6e 64 6c 65 2e 20 41 6c 6c 20 74 68 61   handle. All tha
1838d 74 20 69 73 20 6c 65 66 74 0a 20 20 20 20 20 20  t is left.      
1838e 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69 73  ** to do here is
1838f 20 66 72 65 65 20 74 68 65 20 73 71 6c 69 74 65   free the sqlite
18390 33 5f 62 61 63 6b 75 70 20 73 74 72 75 63 74 75  3_backup structu
18391 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  re..      */.   
18392 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
18393 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 30 3b  p);.      p = 0;
18394 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
18395 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 68   If everything h
18396 61 73 20 67 6f 6e 65 20 61 73 20 70 6c 61 6e 6e  as gone as plann
18397 65 64 2c 20 61 74 74 61 63 68 20 74 68 65 20 62  ed, attach the b
18398 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 74 6f 20  ackup object to 
18399 74 68 65 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20  the.  ** source 
1839a 70 61 67 65 72 2e 20 54 68 65 20 73 6f 75 72 63  pager. The sourc
1839b 65 20 70 61 67 65 72 20 63 61 6c 6c 73 20 42 61  e pager calls Ba
1839c 63 6b 75 70 55 70 64 61 74 65 28 29 20 61 6e 64  ckupUpdate() and
1839d 20 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 29   BackupRestart()
1839e 0a 20 20 2a 2a 20 74 6f 20 6e 6f 74 69 66 79 20  .  ** to notify 
1839f 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 66 20 74  this module if t
183a0 68 65 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69  he source file i
183a1 73 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 62  s modified mid-b
183a2 61 63 6b 75 70 2e 0a 20 20 2a 2f 0a 20 20 69 66  ackup..  */.  if
183a3 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
183a4 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 20  e3_backup **pp; 
183a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
183a6 6f 69 6e 74 65 72 20 74 6f 20 68 65 61 64 20 6f  ointer to head o
183a7 66 20 70 61 67 65 72 73 20 62 61 63 6b 75 70 20  f pagers backup 
183a8 6c 69 73 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69  list */.    sqli
183a9 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d  te3BtreeEnter(p-
183aa 3e 70 53 72 63 29 3b 0a 20 20 20 20 70 70 20 3d  >pSrc);.    pp =
183ab 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63   sqlite3PagerBac
183ac 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 42 74  kupPtr(sqlite3Bt
183ad 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63  reePager(p->pSrc
183ae 29 29 3b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74  ));.    p->pNext
183af 20 3d 20 2a 70 70 3b 0a 20 20 20 20 2a 70 70 20   = *pp;.    *pp 
183b0 3d 20 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = p;.    sqlite3
183b1 42 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53  BtreeLeave(p->pS
183b2 72 63 29 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63  rc);.    p->pSrc
183b3 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d  ->nBackup++;.  }
183b4 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
183b5 78 5f 6c 65 61 76 65 28 70 44 65 73 74 44 62 2d  x_leave(pDestDb-
183b6 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74  >mutex);.  sqlit
183b7 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
183b8 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  SrcDb->mutex);. 
183b9 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
183ba 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 72 63 20  .** Argument rc 
183bb 69 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  is an SQLite err
183bc 6f 72 20 63 6f 64 65 2e 20 52 65 74 75 72 6e 20  or code. Return 
183bd 74 72 75 65 20 69 66 20 74 68 69 73 20 65 72 72  true if this err
183be 6f 72 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64  or is .** consid
183bf 65 72 65 64 20 66 61 74 61 6c 20 69 66 20 65 6e  ered fatal if en
183c0 63 6f 75 6e 74 65 72 65 64 20 64 75 72 69 6e 67  countered during
183c1 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74   a backup operat
183c2 69 6f 6e 2e 20 41 6c 6c 20 65 72 72 6f 72 73 0a  ion. All errors.
183c3 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  ** are considere
183c4 64 20 66 61 74 61 6c 20 65 78 63 65 70 74 20 66  d fatal except f
183c5 6f 72 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61  or SQLITE_BUSY a
183c6 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  nd SQLITE_LOCKED
183c7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
183c8 69 73 46 61 74 61 6c 45 72 72 6f 72 28 69 6e 74  isFatalError(int
183c9 20 72 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28   rc){.  return (
183ca 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc!=SQLITE_OK &&
183cb 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc!=SQLITE_BUSY
183cc 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c   && rc!=SQLITE_L
183cd 4f 43 4b 45 44 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  OCKED);.}../*.**
183ce 20 50 61 72 61 6d 65 74 65 72 20 7a 53 72 63 44   Parameter zSrcD
183cf 61 74 61 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  ata points to a 
183d0 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
183d1 67 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 0a  g the data for .
183d2 2a 2a 20 70 61 67 65 20 69 53 72 63 50 67 20 66  ** page iSrcPg f
183d3 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64  rom the source d
183d4 61 74 61 62 61 73 65 2e 20 43 6f 70 79 20 74 68  atabase. Copy th
183d5 69 73 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65  is data into the
183d6 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e   .** destination
183d7 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74   database..*/.st
183d8 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75 70 4f  atic int backupO
183d9 6e 65 50 61 67 65 28 73 71 6c 69 74 65 33 5f 62  nePage(sqlite3_b
183da 61 63 6b 75 70 20 2a 70 2c 20 50 67 6e 6f 20 69  ackup *p, Pgno i
183db 53 72 63 50 67 2c 20 63 6f 6e 73 74 20 75 38 20  SrcPg, const u8 
183dc 2a 7a 53 72 63 44 61 74 61 29 7b 0a 20 20 50 61  *zSrcData){.  Pa
183dd 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73  ger * const pDes
183de 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  tPager = sqlite3
183df 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44  BtreePager(p->pD
183e0 65 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e  est);.  const in
183e1 74 20 6e 53 72 63 50 67 73 7a 20 3d 20 73 71 6c  t nSrcPgsz = sql
183e2 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
183e3 53 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20  Size(p->pSrc);. 
183e4 20 69 6e 74 20 6e 44 65 73 74 50 67 73 7a 20 3d   int nDestPgsz =
183e5 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
183e6 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65 73  PageSize(p->pDes
183e7 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  t);.  const int 
183e8 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 53 72 63  nCopy = MIN(nSrc
183e9 50 67 73 7a 2c 20 6e 44 65 73 74 50 67 73 7a 29  Pgsz, nDestPgsz)
183ea 3b 0a 20 20 63 6f 6e 73 74 20 69 36 34 20 69 45  ;.  const i64 iE
183eb 6e 64 20 3d 20 28 69 36 34 29 69 53 72 63 50 67  nd = (i64)iSrcPg
183ec 2a 28 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 0a  *(i64)nSrcPgsz;.
183ed 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
183ee 54 45 5f 4f 4b 3b 0a 20 20 69 36 34 20 69 4f 66  TE_OK;.  i64 iOf
183ef 66 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d  f;..  assert( p-
183f0 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 29 3b 0a  >bDestLocked );.
183f1 20 20 61 73 73 65 72 74 28 20 21 69 73 46 61 74    assert( !isFat
183f2 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 29  alError(p->rc) )
183f3 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 72 63  ;.  assert( iSrc
183f4 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  Pg!=PENDING_BYTE
183f5 5f 50 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70  _PAGE(p->pSrc->p
183f6 42 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Bt) );.  assert(
183f7 20 7a 53 72 63 44 61 74 61 20 29 3b 0a 0a 20 20   zSrcData );..  
183f8 2f 2a 20 43 61 74 63 68 20 74 68 65 20 63 61 73  /* Catch the cas
183f9 65 20 77 68 65 72 65 20 74 68 65 20 64 65 73 74  e where the dest
183fa 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ination is an in
183fb 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
183fc 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 70 61   and the.  ** pa
183fd 67 65 20 73 69 7a 65 73 20 6f 66 20 74 68 65 20  ge sizes of the 
183fe 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69  source and desti
183ff 6e 61 74 69 6f 6e 20 64 69 66 66 65 72 2e 20 0a  nation differ. .
18400 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 72 63 50    */.  if( nSrcP
18401 67 73 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20 26  gsz!=nDestPgsz &
18402 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  & sqlite3PagerIs
18403 4d 65 6d 64 62 28 73 71 6c 69 74 65 33 42 74 72  Memdb(sqlite3Btr
18404 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74  eePager(p->pDest
18405 29 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  )) ){.    rc = S
18406 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a  QLITE_READONLY;.
18407 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c    }..  /* This l
18408 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f  oop runs once fo
18409 72 20 65 61 63 68 20 64 65 73 74 69 6e 61 74 69  r each destinati
1840a 6f 6e 20 70 61 67 65 20 73 70 61 6e 6e 65 64 20  on page spanned 
1840b 62 79 20 74 68 65 20 73 6f 75 72 63 65 20 0a 20  by the source . 
1840c 20 2a 2a 20 70 61 67 65 2e 20 46 6f 72 20 65 61   ** page. For ea
1840d 63 68 20 69 74 65 72 61 74 69 6f 6e 2c 20 76 61  ch iteration, va
1840e 72 69 61 62 6c 65 20 69 4f 66 66 20 69 73 20 73  riable iOff is s
1840f 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f  et to the byte o
18410 66 66 73 65 74 0a 20 20 2a 2a 20 6f 66 20 74 68  ffset.  ** of th
18411 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61  e destination pa
18412 67 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ge..  */.  for(i
18413 4f 66 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e 53  Off=iEnd-(i64)nS
18414 72 63 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c 49  rcPgsz; rc==SQLI
18415 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45  TE_OK && iOff<iE
18416 6e 64 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74 50  nd; iOff+=nDestP
18417 67 73 7a 29 7b 0a 20 20 20 20 44 62 50 61 67 65  gsz){.    DbPage
18418 20 2a 70 44 65 73 74 50 67 20 3d 20 30 3b 0a 20   *pDestPg = 0;. 
18419 20 20 20 50 67 6e 6f 20 69 44 65 73 74 20 3d 20     Pgno iDest = 
1841a 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65 73  (Pgno)(iOff/nDes
1841b 74 50 67 73 7a 29 2b 31 3b 0a 20 20 20 20 69 66  tPgsz)+1;.    if
1841c 28 20 69 44 65 73 74 3d 3d 50 45 4e 44 49 4e 47  ( iDest==PENDING
1841d 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44  _BYTE_PAGE(p->pD
1841e 65 73 74 2d 3e 70 42 74 29 20 29 20 63 6f 6e 74  est->pBt) ) cont
1841f 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 53 51  inue;.    if( SQ
18420 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
18421 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
18422 44 65 73 74 50 61 67 65 72 2c 20 69 44 65 73 74  DestPager, iDest
18423 2c 20 26 70 44 65 73 74 50 67 29 29 0a 20 20 20  , &pDestPg)).   
18424 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
18425 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
18426 65 72 57 72 69 74 65 28 70 44 65 73 74 50 67 29  erWrite(pDestPg)
18427 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63  ).    ){.      c
18428 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 20 3d 20 26  onst u8 *zIn = &
18429 7a 53 72 63 44 61 74 61 5b 69 4f 66 66 25 6e 53  zSrcData[iOff%nS
1842a 72 63 50 67 73 7a 5d 3b 0a 20 20 20 20 20 20 75  rcPgsz];.      u
1842b 38 20 2a 7a 44 65 73 74 44 61 74 61 20 3d 20 73  8 *zDestData = s
1842c 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
1842d 74 61 28 70 44 65 73 74 50 67 29 3b 0a 20 20 20  ta(pDestPg);.   
1842e 20 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 26 7a     u8 *zOut = &z
1842f 44 65 73 74 44 61 74 61 5b 69 4f 66 66 25 6e 44  DestData[iOff%nD
18430 65 73 74 50 67 73 7a 5d 3b 0a 0a 20 20 20 20 20  estPgsz];..     
18431 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 64 61 74   /* Copy the dat
18432 61 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63  a from the sourc
18433 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  e page into the 
18434 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65  destination page
18435 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20  ..      ** Then 
18436 63 6c 65 61 72 20 74 68 65 20 42 74 72 65 65 20  clear the Btree 
18437 6c 61 79 65 72 20 4d 65 6d 50 61 67 65 2e 69 73  layer MemPage.is
18438 49 6e 69 74 20 66 6c 61 67 2e 20 42 6f 74 68 20  Init flag. Both 
18439 74 68 69 73 20 6d 6f 64 75 6c 65 0a 20 20 20 20  this module.    
1843a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 70 61 67    ** and the pag
1843b 65 72 20 63 6f 64 65 20 75 73 65 20 74 68 69 73  er code use this
1843c 20 74 72 69 63 6b 20 28 63 6c 65 61 72 69 6e 67   trick (clearing
1843d 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a   the first byte.
1843e 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
1843f 70 61 67 65 20 27 65 78 74 72 61 27 20 73 70 61  page 'extra' spa
18440 63 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65  ce to invalidate
18441 20 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72   the Btree layer
18442 73 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65  s.      ** cache
18443 64 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 70  d parse of the p
18444 61 67 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69 73  age). MemPage.is
18445 49 6e 69 74 20 69 73 20 6d 61 72 6b 65 64 20 0a  Init is marked .
18446 20 20 20 20 20 20 2a 2a 20 22 4d 55 53 54 20 42        ** "MUST B
18447 45 20 46 49 52 53 54 22 20 66 6f 72 20 74 68 69  E FIRST" for thi
18448 73 20 70 75 72 70 6f 73 65 2e 0a 20 20 20 20 20  s purpose..     
18449 20 2a 2f 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   */.      memcpy
1844a 28 7a 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f 70  (zOut, zIn, nCop
1844b 79 29 3b 0a 20 20 20 20 20 20 28 28 75 38 20 2a  y);.      ((u8 *
1844c 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  )sqlite3PagerGet
1844d 45 78 74 72 61 28 70 44 65 73 74 50 67 29 29 5b  Extra(pDestPg))[
1844e 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  0] = 0;.    }.  
1844f 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
18450 72 65 66 28 70 44 65 73 74 50 67 29 3b 0a 20 20  ref(pDestPg);.  
18451 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
18452 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c  }../*.** If pFil
18453 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6c  e is currently l
18454 61 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65  arger than iSize
18455 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74 72 75   bytes, then tru
18456 6e 63 61 74 65 20 69 74 20 74 6f 0a 2a 2a 20 65  ncate it to.** e
18457 78 61 63 74 6c 79 20 69 53 69 7a 65 20 62 79 74  xactly iSize byt
18458 65 73 2e 20 49 66 20 70 46 69 6c 65 20 69 73 20  es. If pFile is 
18459 6e 6f 74 20 6c 61 72 67 65 72 20 74 68 61 6e 20  not larger than 
1845a 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 65  iSize bytes, the
1845b 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69  n.** this functi
1845c 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
1845d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
1845e 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68  TE_OK if everyth
1845f 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75  ing is successfu
18460 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20  l, or an SQLite 
18461 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 69  error .** code i
18462 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
18463 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
18464 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46   backupTruncateF
18465 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ile(sqlite3_file
18466 20 2a 70 46 69 6c 65 2c 20 69 36 34 20 69 53 69   *pFile, i64 iSi
18467 7a 65 29 7b 0a 20 20 69 36 34 20 69 43 75 72 72  ze){.  i64 iCurr
18468 65 6e 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ent;.  int rc = 
18469 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
1846a 65 28 70 46 69 6c 65 2c 20 26 69 43 75 72 72 65  e(pFile, &iCurre
1846b 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  nt);.  if( rc==S
1846c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 43 75 72  QLITE_OK && iCur
1846d 72 65 6e 74 3e 69 53 69 7a 65 20 29 7b 0a 20 20  rent>iSize ){.  
1846e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1846f 54 72 75 6e 63 61 74 65 28 70 46 69 6c 65 2c 20  Truncate(pFile, 
18470 69 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 72 65  iSize);.  }.  re
18471 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
18472 2a 20 43 6f 70 79 20 6e 50 61 67 65 20 70 61 67  * Copy nPage pag
18473 65 73 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72  es from the sour
18474 63 65 20 62 2d 74 72 65 65 20 74 6f 20 74 68 65  ce b-tree to the
18475 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f   destination..*/
18476 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
18477 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
18478 74 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  tep(sqlite3_back
18479 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65  up *p, int nPage
1847a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
1847b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1847c 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d  ter(p->pSrcDb->m
1847d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  utex);.  sqlite3
1847e 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 53  BtreeEnter(p->pS
1847f 72 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44  rc);.  if( p->pD
18480 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c  estDb ){.    sql
18481 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
18482 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74  (p->pDestDb->mut
18483 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d  ex);.  }..  rc =
18484 20 70 2d 3e 72 63 3b 0a 20 20 69 66 28 20 21 69   p->rc;.  if( !i
18485 73 46 61 74 61 6c 45 72 72 6f 72 28 72 63 29 20  sFatalError(rc) 
18486 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 20 63  ){.    Pager * c
18487 6f 6e 73 74 20 70 53 72 63 50 61 67 65 72 20 3d  onst pSrcPager =
18488 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
18489 65 72 28 70 2d 3e 70 53 72 63 29 3b 20 20 20 20  er(p->pSrc);    
1848a 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 72   /* Source pager
1848b 20 2a 2f 0a 20 20 20 20 50 61 67 65 72 20 2a 20   */.    Pager * 
1848c 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72  const pDestPager
1848d 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
1848e 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 20  ager(p->pDest); 
1848f 20 20 2f 2a 20 44 65 73 74 20 70 61 67 65 72 20    /* Dest pager 
18490 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20  */.    int ii;  
18491 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18492 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
18493 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
18494 2f 0a 20 20 20 20 69 6e 74 20 6e 53 72 63 50 61  /.    int nSrcPa
18495 67 65 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20  ge = -1;        
18496 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
18497 20 6f 66 20 73 6f 75 72 63 65 20 64 62 20 69 6e   of source db in
18498 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 69 6e   pages */.    in
18499 74 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20  t bCloseTrans = 
1849a 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
1849b 20 2f 2a 20 54 72 75 65 20 69 66 20 73 72 63 20   /* True if src 
1849c 64 62 20 72 65 71 75 69 72 65 73 20 75 6e 6c 6f  db requires unlo
1849d 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 2f 2a  cking */..    /*
1849e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70   If the source p
1849f 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c  ager is currentl
184a0 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61  y in a write-tra
184a1 6e 73 61 63 74 69 6f 6e 2c 20 72 65 74 75 72 6e  nsaction, return
184a2 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42  .    ** SQLITE_B
184a3 55 53 59 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  USY immediately.
184a4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
184a5 70 2d 3e 70 44 65 73 74 44 62 20 26 26 20 70 2d  p->pDestDb && p-
184a6 3e 70 53 72 63 2d 3e 70 42 74 2d 3e 69 6e 54 72  >pSrc->pBt->inTr
184a7 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
184a8 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20  _WRITE ){.      
184a9 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
184aa 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
184ab 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
184ac 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  K;.    }..    /*
184ad 20 4c 6f 63 6b 20 74 68 65 20 64 65 73 74 69 6e   Lock the destin
184ae 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2c 20  ation database, 
184af 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f 63  if it is not loc
184b0 6b 65 64 20 61 6c 72 65 61 64 79 2e 20 2a 2f 0a  ked already. */.
184b1 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
184b2 4b 3d 3d 72 63 20 26 26 20 70 2d 3e 62 44 65 73  K==rc && p->bDes
184b3 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20 20 20 20 20  tLocked==0.     
184b4 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
184b5 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
184b6 42 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 44  BeginTrans(p->pD
184b7 65 73 74 2c 20 32 29 29 20 0a 20 20 20 20 29 7b  est, 2)) .    ){
184b8 0a 20 20 20 20 20 20 70 2d 3e 62 44 65 73 74 4c  .      p->bDestL
184b9 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ocked = 1;.     
184ba 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
184bb 65 65 47 65 74 4d 65 74 61 28 70 2d 3e 70 44 65  eeGetMeta(p->pDe
184bc 73 74 2c 20 31 2c 20 26 70 2d 3e 69 44 65 73 74  st, 1, &p->iDest
184bd 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 0a  Schema);.    }..
184be 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
184bf 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d  is no open read-
184c0 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
184c1 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
184c2 73 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a 2a 20  se, open.    ** 
184c3 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72  one now. If a tr
184c4 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
184c5 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 69  ned here, then i
184c6 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64  t will be closed
184c7 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 74  .    ** before t
184c8 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 78 69  his function exi
184c9 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ts..    */.    i
184ca 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
184cb 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 74   && 0==sqlite3Bt
184cc 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
184cd 28 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20  (p->pSrc) ){.   
184ce 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
184cf 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70  treeBeginTrans(p
184d0 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20  ->pSrc, 0);.    
184d1 20 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20    bCloseTrans = 
184d2 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  1;.    }.  .    
184d3 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68 65 72  /* Now that ther
184d4 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b  e is a read-lock
184d5 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   on the source d
184d6 61 74 61 62 61 73 65 2c 20 71 75 65 72 79 20 74  atabase, query t
184d7 68 65 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63 65  he.    ** source
184d8 20 70 61 67 65 72 20 66 6f 72 20 74 68 65 20 6e   pager for the n
184d9 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
184da 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
184db 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
184dc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
184dd 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
184de 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
184df 28 70 53 72 63 50 61 67 65 72 2c 20 26 6e 53 72  (pSrcPager, &nSr
184e0 63 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  cPage);.    }.  
184e1 20 20 66 6f 72 28 69 69 3d 30 3b 20 28 6e 50 61    for(ii=0; (nPa
184e2 67 65 3c 30 20 7c 7c 20 69 69 3c 6e 50 61 67 65  ge<0 || ii<nPage
184e3 29 20 26 26 20 70 2d 3e 69 4e 65 78 74 3c 3d 28  ) && p->iNext<=(
184e4 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 26 26  Pgno)nSrcPage &&
184e5 20 21 72 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20   !rc; ii++){.   
184e6 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53     const Pgno iS
184e7 72 63 50 67 20 3d 20 70 2d 3e 69 4e 65 78 74 3b  rcPg = p->iNext;
184e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
184e9 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 20   /* Source page 
184ea 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20  number */.      
184eb 69 66 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44  if( iSrcPg!=PEND
184ec 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d  ING_BYTE_PAGE(p-
184ed 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 7b 0a 20  >pSrc->pBt) ){. 
184ee 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70         DbPage *p
184ef 53 72 63 50 67 3b 20 20 20 20 20 20 20 20 20 20  SrcPg;          
184f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
184f1 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67     /* Source pag
184f2 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20  e object */.    
184f3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
184f4 50 61 67 65 72 47 65 74 28 70 53 72 63 50 61 67  PagerGet(pSrcPag
184f5 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72  er, iSrcPg, &pSr
184f6 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66  cPg);.        if
184f7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
184f8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
184f9 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28  = backupOnePage(
184fa 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c 69 74  p, iSrcPg, sqlit
184fb 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
184fc 53 72 63 50 67 29 29 3b 0a 20 20 20 20 20 20 20  SrcPg));.       
184fd 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
184fe 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a 20 20  nref(pSrcPg);.  
184ff 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
18500 20 20 20 20 20 20 70 2d 3e 69 4e 65 78 74 2b 2b        p->iNext++
18501 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
18502 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
18503 0a 20 20 20 20 20 20 70 2d 3e 6e 50 61 67 65 63  .      p->nPagec
18504 6f 75 6e 74 20 3d 20 6e 53 72 63 50 61 67 65 3b  ount = nSrcPage;
18505 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 6d 61 69  .      p->nRemai
18506 6e 69 6e 67 20 3d 20 6e 53 72 63 50 61 67 65 2b  ning = nSrcPage+
18507 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a 20 20 20 20  1-p->iNext;.    
18508 20 20 69 66 28 20 70 2d 3e 69 4e 65 78 74 3e 28    if( p->iNext>(
18509 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 29 7b  Pgno)nSrcPage ){
1850a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1850b 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20  LITE_DONE;.     
1850c 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
1850d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
1850e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  ONE ){.      con
1850f 73 74 20 69 6e 74 20 6e 53 72 63 50 61 67 65 73  st int nSrcPages
18510 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ize = sqlite3Btr
18511 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d  eeGetPageSize(p-
18512 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 63 6f  >pSrc);.      co
18513 6e 73 74 20 69 6e 74 20 6e 44 65 73 74 50 61 67  nst int nDestPag
18514 65 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42  esize = sqlite3B
18515 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28  treeGetPageSize(
18516 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 20  p->pDest);.     
18517 20 69 6e 74 20 6e 44 65 73 74 54 72 75 6e 63 61   int nDestTrunca
18518 74 65 3b 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20  te;.  .      /* 
18519 55 70 64 61 74 65 20 74 68 65 20 73 63 68 65 6d  Update the schem
1851a 61 20 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 20  a version field 
1851b 69 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  in the destinati
1851c 6f 6e 20 64 61 74 61 62 61 73 65 2e 20 54 68 69  on database. Thi
1851d 73 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 6f  s.      ** is to
1851e 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
1851f 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69  the schema-versi
18520 6f 6e 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 63  on really does c
18521 68 61 6e 67 65 20 69 6e 0a 20 20 20 20 20 20 2a  hange in.      *
18522 2a 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65  * the case where
18523 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20   the source and 
18524 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
18525 62 61 73 65 73 20 68 61 76 65 20 74 68 65 0a 20  bases have the. 
18526 20 20 20 20 20 2a 2a 20 73 61 6d 65 20 73 63 68       ** same sch
18527 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20  ema version..   
18528 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
18529 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
1852a 74 61 28 70 2d 3e 70 44 65 73 74 2c 20 31 2c 20  ta(p->pDest, 1, 
1852b 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 2b 31  p->iDestSchema+1
1852c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  );.      if( p->
1852d 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 20  pDestDb ){.     
1852e 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49     sqlite3ResetI
1852f 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 70 2d  nternalSchema(p-
18530 3e 70 44 65 73 74 44 62 2c 20 30 29 3b 0a 20 20  >pDestDb, 0);.  
18531 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
18532 53 65 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74  Set nDestTruncat
18533 65 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 6e  e to the final n
18534 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
18535 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  n the destinatio
18536 6e 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62  n.      ** datab
18537 61 73 65 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63  ase. The complic
18538 61 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68  ation here is th
18539 61 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  at the destinati
1853a 6f 6e 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a  on page.      **
1853b 20 73 69 7a 65 20 6d 61 79 20 62 65 20 64 69 66   size may be dif
1853c 66 65 72 65 6e 74 20 74 6f 20 74 68 65 20 73 6f  ferent to the so
1853d 75 72 63 65 20 70 61 67 65 20 73 69 7a 65 2e 20  urce page size. 
1853e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1853f 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65  ** If the source
18540 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 6d   page size is sm
18541 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64  aller than the d
18542 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20  estination page 
18543 73 69 7a 65 2c 20 0a 20 20 20 20 20 20 2a 2a 20  size, .      ** 
18544 72 6f 75 6e 64 20 75 70 2e 20 49 6e 20 74 68 69  round up. In thi
18545 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20  s case the call 
18546 74 6f 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e  to sqlite3OsTrun
18547 63 61 74 65 28 29 20 62 65 6c 6f 77 20 77 69 6c  cate() below wil
18548 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69 78 20 74  l.      ** fix t
18549 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  he size of the f
1854a 69 6c 65 2e 20 48 6f 77 65 76 65 72 20 69 74 20  ile. However it 
1854b 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  is important to 
1854c 63 61 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 73 71  call.      ** sq
1854d 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
1854e 74 65 49 6d 61 67 65 28 29 20 68 65 72 65 20 73  teImage() here s
1854f 6f 20 74 68 61 74 20 61 6e 79 20 70 61 67 65 73  o that any pages
18550 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a   in the .      *
18551 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 69  * destination fi
18552 6c 65 20 74 68 61 74 20 6c 69 65 20 62 65 79 6f  le that lie beyo
18553 6e 64 20 74 68 65 20 6e 44 65 73 74 54 72 75 6e  nd the nDestTrun
18554 63 61 74 65 20 70 61 67 65 20 6d 61 72 6b 20 61  cate page mark a
18555 72 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72  re.      ** jour
18556 6e 61 6c 6c 65 64 20 62 79 20 50 61 67 65 72 43  nalled by PagerC
18557 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20  ommitPhaseOne() 
18558 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20  before they are 
18559 64 65 73 74 72 6f 79 65 64 0a 20 20 20 20 20 20  destroyed.      
1855a 2a 2a 20 62 79 20 74 68 65 20 66 69 6c 65 20 74  ** by the file t
1855b 72 75 6e 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20  runcation..     
1855c 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 53   */.      if( nS
1855d 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74  rcPagesize<nDest
1855e 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20  Pagesize ){.    
1855f 20 20 20 20 69 6e 74 20 72 61 74 69 6f 20 3d 20      int ratio = 
18560 6e 44 65 73 74 50 61 67 65 73 69 7a 65 2f 6e 53  nDestPagesize/nS
18561 72 63 50 61 67 65 73 69 7a 65 3b 0a 20 20 20 20  rcPagesize;.    
18562 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74      nDestTruncat
18563 65 20 3d 20 28 6e 53 72 63 50 61 67 65 2b 72 61  e = (nSrcPage+ra
18564 74 69 6f 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20  tio-1)/ratio;.  
18565 20 20 20 20 20 20 69 66 28 20 6e 44 65 73 74 54        if( nDestT
18566 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 50 45  runcate==(int)PE
18567 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
18568 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29  p->pDest->pBt) )
18569 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 65 73  {.          nDes
1856a 74 54 72 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 20  tTruncate--;.   
1856b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1856c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 44 65 73  se{.        nDes
1856d 74 54 72 75 6e 63 61 74 65 20 3d 20 6e 53 72 63  tTruncate = nSrc
1856e 50 61 67 65 20 2a 20 28 6e 53 72 63 50 61 67 65  Page * (nSrcPage
1856f 73 69 7a 65 2f 6e 44 65 73 74 50 61 67 65 73 69  size/nDestPagesi
18570 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ze);.      }.   
18571 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54     sqlite3PagerT
18572 72 75 6e 63 61 74 65 49 6d 61 67 65 28 70 44 65  runcateImage(pDe
18573 73 74 50 61 67 65 72 2c 20 6e 44 65 73 74 54 72  stPager, nDestTr
18574 75 6e 63 61 74 65 29 3b 0a 0a 20 20 20 20 20 20  uncate);..      
18575 69 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65  if( nSrcPagesize
18576 3c 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 29  <nDestPagesize )
18577 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
18578 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 2d  the source page-
18579 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20  size is smaller 
1857a 74 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61  than the destina
1857b 74 69 6f 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a  tion page-size,.
1857c 20 20 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 65          ** two e
1857d 78 74 72 61 20 74 68 69 6e 67 73 20 6d 61 79 20  xtra things may 
1857e 6e 65 65 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a  need to happen:.
1857f 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
18580 20 20 20 2a 2a 20 20 20 2a 20 54 68 65 20 64 65     **   * The de
18581 73 74 69 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65  stination may ne
18582 65 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74  ed to be truncat
18583 65 64 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20  ed, and.        
18584 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  **.        **   
18585 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 6f 6e  * Data stored on
18586 20 74 68 65 20 70 61 67 65 73 20 69 6d 6d 65 64   the pages immed
18587 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67  iately following
18588 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a   the .        **
18589 20 20 20 20 20 70 65 6e 64 69 6e 67 2d 62 79 74       pending-byt
1858a 65 20 70 61 67 65 20 69 6e 20 74 68 65 20 73 6f  e page in the so
1858b 75 72 63 65 20 64 61 74 61 62 61 73 65 20 6d 61  urce database ma
1858c 79 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 20  y need to be.   
1858d 20 20 20 20 20 2a 2a 20 20 20 20 20 63 6f 70 69       **     copi
1858e 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74  ed into the dest
1858f 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
18590 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
18591 20 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69       const i64 i
18592 53 69 7a 65 20 3d 20 28 69 36 34 29 6e 53 72 63  Size = (i64)nSrc
18593 50 61 67 65 73 69 7a 65 20 2a 20 28 69 36 34 29  Pagesize * (i64)
18594 6e 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20 20  nSrcPage;.      
18595 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
18596 20 63 6f 6e 73 74 20 70 46 69 6c 65 20 3d 20 73   const pFile = s
18597 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28  qlite3PagerFile(
18598 70 44 65 73 74 50 61 67 65 72 29 3b 0a 0a 20 20  pDestPager);..  
18599 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46        assert( pF
1859a 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ile );.        a
1859b 73 73 65 72 74 28 20 28 69 36 34 29 6e 44 65 73  ssert( (i64)nDes
1859c 74 54 72 75 6e 63 61 74 65 2a 28 69 36 34 29 6e  tTruncate*(i64)n
1859d 44 65 73 74 50 61 67 65 73 69 7a 65 20 3e 3d 20  DestPagesize >= 
1859e 69 53 69 7a 65 20 7c 7c 20 28 0a 20 20 20 20 20  iSize || (.     
1859f 20 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72           nDestTr
185a0 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 28 50 45  uncate==(int)(PE
185a1 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
185a2 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 2d 31  p->pDest->pBt)-1
185a3 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  ).           && 
185a4 69 53 69 7a 65 3e 3d 50 45 4e 44 49 4e 47 5f 42  iSize>=PENDING_B
185a5 59 54 45 20 26 26 20 69 53 69 7a 65 3c 3d 50 45  YTE && iSize<=PE
185a6 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 44 65 73 74  NDING_BYTE+nDest
185a7 50 61 67 65 73 69 7a 65 0a 20 20 20 20 20 20 20  Pagesize.       
185a8 20 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   ));.        if(
185a9 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
185aa 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  = sqlite3PagerCo
185ab 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65  mmitPhaseOne(pDe
185ac 73 74 50 61 67 65 72 2c 20 30 2c 20 31 29 29 0a  stPager, 0, 1)).
185ad 20 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49           && SQLI
185ae 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 62 61 63  TE_OK==(rc = bac
185af 6b 75 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28  kupTruncateFile(
185b0 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 29 0a 20  pFile, iSize)). 
185b1 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54          && SQLIT
185b2 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
185b3 74 65 33 50 61 67 65 72 53 79 6e 63 28 70 44 65  te3PagerSync(pDe
185b4 73 74 50 61 67 65 72 29 29 0a 20 20 20 20 20 20  stPager)).      
185b5 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69    ){.          i
185b6 36 34 20 69 4f 66 66 3b 0a 20 20 20 20 20 20 20  64 iOff;.       
185b7 20 20 20 69 36 34 20 69 45 6e 64 20 3d 20 4d 49     i64 iEnd = MI
185b8 4e 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b  N(PENDING_BYTE +
185b9 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 2c 20   nDestPagesize, 
185ba 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20  iSize);.        
185bb 20 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 20    for(.         
185bc 20 20 20 69 4f 66 66 3d 50 45 4e 44 49 4e 47 5f     iOff=PENDING_
185bd 42 59 54 45 2b 6e 53 72 63 50 61 67 65 73 69 7a  BYTE+nSrcPagesiz
185be 65 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e; .            
185bf 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
185c0 20 69 4f 66 66 3c 69 45 6e 64 3b 20 0a 20 20 20   iOff<iEnd; .   
185c1 20 20 20 20 20 20 20 20 20 69 4f 66 66 2b 3d 6e           iOff+=n
185c2 53 72 63 50 61 67 65 73 69 7a 65 0a 20 20 20 20  SrcPagesize.    
185c3 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
185c4 20 20 20 20 20 50 67 48 64 72 20 2a 70 53 72 63       PgHdr *pSrc
185c5 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  Pg = 0;.        
185c6 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69      const Pgno i
185c7 53 72 63 50 67 20 3d 20 28 50 67 6e 6f 29 28 28  SrcPg = (Pgno)((
185c8 69 4f 66 66 2f 6e 53 72 63 50 61 67 65 73 69 7a  iOff/nSrcPagesiz
185c9 65 29 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  e)+1);.         
185ca 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
185cb 61 67 65 72 47 65 74 28 70 53 72 63 50 61 67 65  agerGet(pSrcPage
185cc 72 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72 63  r, iSrcPg, &pSrc
185cd 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Pg);.           
185ce 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
185cf 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
185d0 20 20 20 20 75 38 20 2a 7a 44 61 74 61 20 3d 20      u8 *zData = 
185d1 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
185d2 61 74 61 28 70 53 72 63 50 67 29 3b 0a 20 20 20  ata(pSrcPg);.   
185d3 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
185d4 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
185d5 46 69 6c 65 2c 20 7a 44 61 74 61 2c 20 6e 53 72  File, zData, nSr
185d6 63 50 61 67 65 73 69 7a 65 2c 20 69 4f 66 66 29  cPagesize, iOff)
185d7 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
185d8 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
185d9 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 53  te3PagerUnref(pS
185da 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20  rcPg);.         
185db 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
185dc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
185dd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
185de 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  gerCommitPhaseOn
185df 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c  e(pDestPager, 0,
185e0 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a   0);.      }.  .
185e1 20 20 20 20 20 20 2f 2a 20 46 69 6e 69 73 68 20        /* Finish 
185e2 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 20 74  committing the t
185e3 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 74 68  ransaction to th
185e4 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
185e5 74 61 62 61 73 65 2e 20 2a 2f 0a 20 20 20 20 20  tabase. */.     
185e6 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
185e7 72 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c  rc.       && SQL
185e8 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
185e9 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
185ea 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73  PhaseTwo(p->pDes
185eb 74 29 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  t)).      ){.   
185ec 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
185ed 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20  _DONE;.      }. 
185ee 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
185ef 66 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 69 73  f bCloseTrans is
185f0 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 73   true, then this
185f1 20 66 75 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64   function opened
185f2 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74   a read transact
185f3 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68  ion.    ** on th
185f4 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
185f5 65 2e 20 43 6c 6f 73 65 20 74 68 65 20 72 65 61  e. Close the rea
185f6 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 65  d transaction he
185f7 72 65 2e 20 54 68 65 72 65 20 69 73 0a 20 20 20  re. There is.   
185f8 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63   ** no need to c
185f9 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
185fa 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 62 74  values of the bt
185fb 72 65 65 20 6d 65 74 68 6f 64 73 20 68 65 72 65  ree methods here
185fc 2c 20 61 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d  , as.    ** "com
185fd 6d 69 74 74 69 6e 67 22 20 61 20 72 65 61 64 2d  mitting" a read-
185fe 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e  only transaction
185ff 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20   cannot fail..  
18600 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 43 6c    */.    if( bCl
18601 6f 73 65 54 72 61 6e 73 20 29 7b 0a 20 20 20 20  oseTrans ){.    
18602 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20    TESTONLY( int 
18603 72 63 32 20 29 3b 0a 20 20 20 20 20 20 54 45 53  rc2 );.      TES
18604 54 4f 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20  TONLY( rc2  = ) 
18605 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
18606 69 74 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53  itPhaseOne(p->pS
18607 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 54 45  rc, 0);.      TE
18608 53 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29  STONLY( rc2 |= )
18609 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1860a 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70  mitPhaseTwo(p->p
1860b 53 72 63 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Src);.      asse
1860c 72 74 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f  rt( rc2==SQLITE_
1860d 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  OK );.    }.  . 
1860e 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20     p->rc = rc;. 
1860f 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73   }.  if( p->pDes
18610 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tDb ){.    sqlit
18611 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
18612 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78  ->pDestDb->mutex
18613 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
18614 42 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53  BtreeLeave(p->pS
18615 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
18616 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53  utex_leave(p->pS
18617 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  rcDb->mutex);.  
18618 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
18619 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20  .** Release all 
1861a 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69  resources associ
1861b 61 74 65 64 20 77 69 74 68 20 61 6e 20 73 71 6c  ated with an sql
1861c 69 74 65 33 5f 62 61 63 6b 75 70 2a 20 68 61 6e  ite3_backup* han
1861d 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
1861e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1861f 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c  ackup_finish(sql
18620 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b  ite3_backup *p){
18621 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
18622 70 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20  p **pp;         
18623 20 20 20 20 20 20 20 20 2f 2a 20 50 74 72 20 74          /* Ptr t
18624 6f 20 68 65 61 64 20 6f 66 20 70 61 67 65 72 73  o head of pagers
18625 20 62 61 63 6b 75 70 20 6c 69 73 74 20 2a 2f 0a   backup list */.
18626 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
18627 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  *mutex;         
18628 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20         /* Mutex 
18629 74 6f 20 70 72 6f 74 65 63 74 20 73 6f 75 72 63  to protect sourc
1862a 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
1862b 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
1862c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1862d 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
1862e 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a   return */..  /*
1862f 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78   Enter the mutex
18630 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  es */.  sqlite3_
18631 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70  mutex_enter(p->p
18632 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  SrcDb->mutex);. 
18633 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
18634 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 6d  er(p->pSrc);.  m
18635 75 74 65 78 20 3d 20 70 2d 3e 70 53 72 63 44 62  utex = p->pSrcDb
18636 2d 3e 6d 75 74 65 78 3b 0a 20 20 69 66 28 20 70  ->mutex;.  if( p
18637 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20  ->pDestDb ){.   
18638 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
18639 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d  nter(p->pDestDb-
1863a 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20  >mutex);.  }..  
1863b 2f 2a 20 44 65 74 61 63 68 20 74 68 69 73 20 62  /* Detach this b
1863c 61 63 6b 75 70 20 66 72 6f 6d 20 74 68 65 20 73  ackup from the s
1863d 6f 75 72 63 65 20 70 61 67 65 72 2e 20 2a 2f 0a  ource pager. */.
1863e 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62    if( p->pDestDb
1863f 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 73 71 6c   ){.    pp = sql
18640 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50  ite3PagerBackupP
18641 74 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50  tr(sqlite3BtreeP
18642 61 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a  ager(p->pSrc));.
18643 20 20 20 20 77 68 69 6c 65 28 20 2a 70 70 21 3d      while( *pp!=
18644 70 20 29 7b 0a 20 20 20 20 20 20 70 70 20 3d 20  p ){.      pp = 
18645 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20  &(*pp)->pNext;. 
18646 20 20 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70     }.    *pp = p
18647 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 2d 3e  ->pNext;.    p->
18648 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b  pSrc->nBackup--;
18649 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20  .  }..  /* If a 
1864a 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
1864b 74 69 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65  till open on the
1864c 20 42 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20   Btree, roll it 
1864d 62 61 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  back. */.  sqlit
1864e 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28  e3BtreeRollback(
1864f 70 2d 3e 70 44 65 73 74 29 3b 0a 0a 20 20 2f 2a  p->pDest);..  /*
18650 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 63   Set the error c
18651 6f 64 65 20 6f 66 20 74 68 65 20 64 65 73 74 69  ode of the desti
18652 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
18653 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 20  handle. */.  rc 
18654 3d 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  = (p->rc==SQLITE
18655 5f 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f  _DONE) ? SQLITE_
18656 4f 4b 20 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 71  OK : p->rc;.  sq
18657 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 70 44  lite3Error(p->pD
18658 65 73 74 44 62 2c 20 72 63 2c 20 30 29 3b 0a 0a  estDb, rc, 0);..
18659 20 20 2f 2a 20 45 78 69 74 20 74 68 65 20 6d 75    /* Exit the mu
1865a 74 65 78 65 73 20 61 6e 64 20 66 72 65 65 20 74  texes and free t
1865b 68 65 20 62 61 63 6b 75 70 20 63 6f 6e 74 65 78  he backup contex
1865c 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a  t structure. */.
1865d 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62    if( p->pDestDb
1865e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1865f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70  mutex_leave(p->p
18660 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  DestDb->mutex);.
18661 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
18662 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 29  eeLeave(p->pSrc)
18663 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74  ;.  if( p->pDest
18664 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Db ){.    sqlite
18665 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20  3_free(p);.  }. 
18666 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
18667 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72  eave(mutex);.  r
18668 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
18669 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1866a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74  mber of pages st
1866b 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64  ill to be backed
1866c 20 75 70 20 61 73 20 6f 66 20 74 68 65 20 6d 6f   up as of the mo
1866d 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c  st recent.** cal
1866e 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
1866f 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53  kup_step()..*/.S
18670 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
18671 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d  lite3_backup_rem
18672 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62  aining(sqlite3_b
18673 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74  ackup *p){.  ret
18674 75 72 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e  urn p->nRemainin
18675 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  g;.}../*.** Retu
18676 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  rn the total num
18677 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
18678 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
18679 61 73 65 20 61 73 20 6f 66 20 74 68 65 20 6d 6f  ase as of the mo
1867a 73 74 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61  st .** recent ca
1867b 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
1867c 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a  ckup_step()..*/.
1867d 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1867e 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61  qlite3_backup_pa
1867f 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  gecount(sqlite3_
18680 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65  backup *p){.  re
18681 74 75 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75  turn p->nPagecou
18682 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  nt;.}../*.** Thi
18683 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
18684 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 63  lled after the c
18685 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20  ontents of page 
18686 69 50 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20  iPage of the.** 
18687 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
18688 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69  have been modifi
18689 65 64 2e 20 49 66 20 70 61 67 65 20 69 50 61 67  ed. If page iPag
1868a 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  e has already be
1868b 65 6e 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e  en .** copied in
1868c 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  to the destinati
1868d 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  on database, the
1868e 6e 20 74 68 65 20 64 61 74 61 20 77 72 69 74 74  n the data writt
1868f 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73  en to the.** des
18690 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20  tination is now 
18691 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 54 68 65  invalidated. The
18692 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70   destination cop
18693 79 20 6f 66 20 69 50 61 67 65 20 6e 65 65 64 73  y of iPage needs
18694 0a 2a 2a 20 74 6f 20 62 65 20 75 70 64 61 74 65  .** to be update
18695 64 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 64  d with the new d
18696 61 74 61 20 62 65 66 6f 72 65 20 74 68 65 20 62  ata before the b
18697 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20  ackup operation 
18698 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a  is.** complete..
18699 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75  **.** It is assu
1869a 6d 65 64 20 74 68 61 74 20 74 68 65 20 6d 75 74  med that the mut
1869b 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
1869c 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 20  th the BtShared 
1869d 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73  object.** corres
1869e 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73  ponding to the s
1869f 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69  ource database i
186a0 73 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73  s held when this
186a1 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20   function is.** 
186a2 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  called..*/.SQLIT
186a3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
186a4 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61  qlite3BackupUpda
186a5 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  te(sqlite3_backu
186a6 70 20 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f  p *pBackup, Pgno
186a7 20 69 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 38   iPage, const u8
186a8 20 2a 61 44 61 74 61 29 7b 0a 20 20 73 71 6c 69   *aData){.  sqli
186a9 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20  te3_backup *p;  
186aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
186ab 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
186ac 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70  iable */.  for(p
186ad 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70  =pBackup; p; p=p
186ae 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73  ->pNext){.    as
186af 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
186b0 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63  tex_held(p->pSrc
186b1 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
186b2 0a 20 20 20 20 69 66 28 20 21 69 73 46 61 74 61  .    if( !isFata
186b3 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 26 26  lError(p->rc) &&
186b4 20 69 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20   iPage<p->iNext 
186b5 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
186b6 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 70  backup process p
186b7 20 68 61 73 20 61 6c 72 65 61 64 79 20 63 6f 70   has already cop
186b8 69 65 64 20 70 61 67 65 20 69 50 61 67 65 2e 20  ied page iPage. 
186b9 42 75 74 20 6e 6f 77 20 69 74 0a 20 20 20 20 20  But now it.     
186ba 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64   ** has been mod
186bb 69 66 69 65 64 20 62 79 20 61 20 74 72 61 6e 73  ified by a trans
186bc 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f  action on the so
186bd 75 72 63 65 20 70 61 67 65 72 2e 20 43 6f 70 79  urce pager. Copy
186be 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65  .      ** the ne
186bf 77 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20  w data into the 
186c0 62 61 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f  backup..      */
186c1 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20  .      int rc = 
186c2 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c  backupOnePage(p,
186c3 20 69 50 61 67 65 2c 20 61 44 61 74 61 29 3b 0a   iPage, aData);.
186c4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
186c5 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26  !=SQLITE_BUSY &&
186c6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b   rc!=SQLITE_LOCK
186c7 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ED );.      if( 
186c8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
186c9 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d  .        p->rc =
186ca 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
186cb 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
186cc 52 65 73 74 61 72 74 20 74 68 65 20 62 61 63 6b  Restart the back
186cd 75 70 20 70 72 6f 63 65 73 73 2e 20 54 68 69 73  up process. This
186ce 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
186cf 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 0a  the pager layer.
186d0 2a 2a 20 64 65 74 65 63 74 73 20 74 68 61 74 20  ** detects that 
186d1 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  the database has
186d2 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62   been modified b
186d3 79 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 64 61  y an external da
186d4 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
186d5 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61  tion. In this ca
186d6 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77  se there is no w
186d7 61 79 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68  ay of knowing wh
186d8 69 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61  ich of the.** pa
186d9 67 65 73 20 74 68 61 74 20 68 61 76 65 20 62 65  ges that have be
186da 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  en copied into t
186db 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
186dc 61 74 61 62 61 73 65 20 61 72 65 20 73 74 69 6c  atabase are stil
186dd 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20  l .** valid and 
186de 77 68 69 63 68 20 61 72 65 20 6e 6f 74 2c 20 73  which are not, s
186df 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 72 6f  o the entire pro
186e0 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 65  cess needs to be
186e1 20 72 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a   restarted..**.*
186e2 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20  * It is assumed 
186e3 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 61  that the mutex a
186e4 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
186e5 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65  he BtShared obje
186e6 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  ct.** correspond
186e7 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 63  ing to the sourc
186e8 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68 65  e database is he
186e9 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ld when this fun
186ea 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c  ction is.** call
186eb 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
186ec 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
186ed 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28  e3BackupRestart(
186ee 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
186ef 70 42 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69  pBackup){.  sqli
186f0 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20  te3_backup *p;  
186f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
186f2 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
186f3 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70  iable */.  for(p
186f4 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70  =pBackup; p; p=p
186f5 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73  ->pNext){.    as
186f6 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
186f7 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63  tex_held(p->pSrc
186f8 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
186f9 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20  .    p->iNext = 
186fa 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65  1;.  }.}..#ifnde
186fb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  f SQLITE_OMIT_VA
186fc 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20  CUUM./*.** Copy 
186fd 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e  the complete con
186fe 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d 20  tent of pBtFrom 
186ff 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 20 74  into pBtTo.  A t
18700 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75  ransaction.** mu
18701 73 74 20 62 65 20 61 63 74 69 76 65 20 66 6f 72  st be active for
18702 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a   both files..**.
18703 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 66  ** The size of f
18704 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 20 72  ile pTo may be r
18705 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20 6f  educed by this o
18706 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79  peration. If any
18707 74 68 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77  thing .** goes w
18708 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 61  rong, the transa
18709 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20  ction on pTo is 
1870a 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20  rolled back. If 
1870b 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 20  successful, the 
1870c 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
1870d 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 66  is committed bef
1870e 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
1870f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18710 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
18711 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20  eCopyFile(Btree 
18712 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 72  *pTo, Btree *pFr
18713 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  om){.  int rc;. 
18714 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
18715 62 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  b;.  sqlite3Btre
18716 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20 20 73  eEnter(pTo);.  s
18717 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
18718 28 70 46 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 53  (pFrom);..  /* S
18719 65 74 20 75 70 20 61 6e 20 73 71 6c 69 74 65 33  et up an sqlite3
1871a 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 20  _backup object. 
1871b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2e 70  sqlite3_backup.p
1871c 44 65 73 74 44 62 20 6d 75 73 74 20 62 65 20 73  DestDb must be s
1871d 65 74 0a 20 20 2a 2a 20 74 6f 20 30 2e 20 54 68  et.  ** to 0. Th
1871e 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68  is is used by th
1871f 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
18720 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 62 61 63  s of sqlite3_bac
18721 6b 75 70 5f 73 74 65 70 28 29 0a 20 20 2a 2a 20  kup_step().  ** 
18722 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  and sqlite3_back
18723 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 64  up_finish() to d
18724 65 74 65 63 74 20 74 68 61 74 20 74 68 65 79 20  etect that they 
18725 61 72 65 20 62 65 69 6e 67 20 63 61 6c 6c 65 64  are being called
18726 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20  .  ** from this 
18727 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 69  function, not di
18728 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 75 73  rectly by the us
18729 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65  er..  */.  memse
1872a 74 28 26 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28  t(&b, 0, sizeof(
1872b 62 29 29 3b 0a 20 20 62 2e 70 53 72 63 44 62 20  b));.  b.pSrcDb 
1872c 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 62  = pFrom->db;.  b
1872d 2e 70 53 72 63 20 3d 20 70 46 72 6f 6d 3b 0a 20  .pSrc = pFrom;. 
1872e 20 62 2e 70 44 65 73 74 20 3d 20 70 54 6f 3b 0a   b.pDest = pTo;.
1872f 20 20 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a    b.iNext = 1;..
18730 20 20 2f 2a 20 30 78 37 46 46 46 46 46 46 46 20    /* 0x7FFFFFFF 
18731 69 73 20 74 68 65 20 68 61 72 64 20 6c 69 6d 69  is the hard limi
18732 74 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72  t for the number
18733 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 20 64   of pages in a d
18734 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c  atabase.  ** fil
18735 65 2e 20 42 79 20 70 61 73 73 69 6e 67 20 74 68  e. By passing th
18736 69 73 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72  is as the number
18737 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f 70   of pages to cop
18738 79 20 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65  y to.  ** sqlite
18739 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c  3_backup_step(),
1873a 20 77 65 20 63 61 6e 20 67 75 61 72 61 6e 74 65   we can guarante
1873b 65 20 74 68 61 74 20 74 68 65 20 63 6f 70 79 20  e that the copy 
1873c 66 69 6e 69 73 68 65 73 20 0a 20 20 2a 2a 20 77  finishes .  ** w
1873d 69 74 68 69 6e 20 61 20 73 69 6e 67 6c 65 20 63  ithin a single c
1873e 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61 6e 20 65  all (unless an e
1873f 72 72 6f 72 20 6f 63 63 75 72 73 29 2e 20 54 68  rror occurs). Th
18740 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  e assert() state
18741 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 65 63 6b 73  ment.  ** checks
18742 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e   this assumption
18743 20 2d 20 28 70 2d 3e 72 63 29 20 73 68 6f 75 6c   - (p->rc) shoul
18744 64 20 62 65 20 73 65 74 20 74 6f 20 65 69 74 68  d be set to eith
18745 65 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a  er SQLITE_DONE .
18746 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72    ** or an error
18747 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71   code..  */.  sq
18748 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
18749 70 28 26 62 2c 20 30 78 37 46 46 46 46 46 46 46  p(&b, 0x7FFFFFFF
1874a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 62 2e 72  );.  assert( b.r
1874b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c!=SQLITE_OK );.
1874c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
1874d 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 26 62 29  ackup_finish(&b)
1874e 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1874f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 54 6f  TE_OK ){.    pTo
18750 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46  ->pBt->pageSizeF
18751 69 78 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  ixed = 0;.  }.. 
18752 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
18753 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c  ve(pFrom);.  sql
18754 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
18755 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  To);.  return rc
18756 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
18757 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
18758 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
18759 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63 6b  **** End of back
1875a 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  up.c ***********
1875b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1875c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1875d 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1875e 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1875f 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a  vdbemem.c ******
18760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18761 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18762 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
18763 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65  May 26.**.** The
18764 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
18765 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
18766 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
18767 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
18768 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
18769 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1876a 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1876b 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1876c 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1876d 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1876e 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1876f 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
18770 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
18771 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
18772 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
18773 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
18774 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
18775 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18778 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18779 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1877a 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
1877b 6f 64 65 20 75 73 65 20 74 6f 20 6d 61 6e 69 70  ode use to manip
1877c 75 6c 61 74 65 20 22 4d 65 6d 22 20 73 74 72 75  ulate "Mem" stru
1877d 63 74 75 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a  cture.  A "Mem".
1877e 2a 2a 20 73 74 6f 72 65 73 20 61 20 73 69 6e 67  ** stores a sing
1877f 6c 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  le value in the 
18780 56 44 42 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e  VDBE.  Mem is an
18781 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72   opaque structur
18782 65 20 76 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c  e visible.** onl
18783 79 20 77 69 74 68 69 6e 20 74 68 65 20 56 44 42  y within the VDB
18784 45 2e 20 20 49 6e 74 65 72 66 61 63 65 20 72 6f  E.  Interface ro
18785 75 74 69 6e 65 73 20 72 65 66 65 72 20 74 6f 20  utines refer to 
18786 61 20 4d 65 6d 20 75 73 69 6e 67 20 74 68 65 0a  a Mem using the.
18787 2a 2a 20 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76  ** name sqlite_v
18788 61 6c 75 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  alue.**.** $Id: 
18789 76 64 62 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 34  vdbemem.c,v 1.14
1878a 34 20 32 30 30 39 2f 30 35 2f 30 35 20 31 32 3a  4 2009/05/05 12:
1878b 35 34 3a 35 30 20 64 72 68 20 45 78 70 20 24 0a  54:50 drh Exp $.
1878c 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73  */../*.** Call s
1878d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
1878e 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65  andBlob() on the
1878f 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20   supplied value 
18790 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50  (type Mem*).** P
18791 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f   if required..*/
18792 0a 23 64 65 66 69 6e 65 20 65 78 70 61 6e 64 42  .#define expandB
18793 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c  lob(P) (((P)->fl
18794 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71  ags&MEM_Zero)?sq
18795 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
18796 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a  ndBlob(P):0)../*
18797 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 61  .** If pMem is a
18798 6e 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61 20  n object with a 
18799 76 61 6c 69 64 20 73 74 72 69 6e 67 20 72 65 70  valid string rep
1879a 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69  resentation, thi
1879b 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73  s routine.** ens
1879c 75 72 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61  ures the interna
1879d 6c 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74  l encoding for t
1879e 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  he string repres
1879f 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27  entation is.** '
187a0 64 65 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65  desiredEnc', one
187a1 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   of SQLITE_UTF8,
187a2 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20   SQLITE_UTF16LE 
187a3 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  or SQLITE_UTF16B
187a4 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d  E..**.** If pMem
187a5 20 69 73 20 6e 6f 74 20 61 20 73 74 72 69 6e 67   is not a string
187a6 20 6f 62 6a 65 63 74 2c 20 6f 72 20 74 68 65 20   object, or the 
187a7 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20  encoding of the 
187a8 73 74 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73  string.** repres
187a9 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 6c 72 65  entation is alre
187aa 61 64 79 20 73 74 6f 72 65 64 20 75 73 69 6e 67  ady stored using
187ab 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 65   the requested e
187ac 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68  ncoding, then th
187ad 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73  is.** routine is
187ae 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
187af 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
187b0 75 72 6e 65 64 20 69 66 20 74 68 65 20 63 6f 6e  urned if the con
187b1 76 65 72 73 69 6f 6e 20 69 73 20 73 75 63 63 65  version is succe
187b2 73 73 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65  ssful (or not re
187b3 71 75 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49  quired)..** SQLI
187b4 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20  TE_NOMEM may be 
187b5 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d 61  returned if a ma
187b6 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64 75 72  lloc() fails dur
187b7 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a  ing conversion.*
187b8 2a 20 62 65 74 77 65 65 6e 20 66 6f 72 6d 61 74  * between format
187b9 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
187ba 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
187bb 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
187bc 6e 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e  ng(Mem *pMem, in
187bd 74 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a 20  t desiredEnc){. 
187be 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72   int rc;.  asser
187bf 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
187c0 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
187c1 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69  ;.  assert( desi
187c2 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
187c3 54 46 38 20 7c 7c 20 64 65 73 69 72 65 64 45 6e  TF8 || desiredEn
187c4 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
187c5 45 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  E.           || 
187c6 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49  desiredEnc==SQLI
187c7 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20  TE_UTF16BE );.  
187c8 69 66 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67  if( !(pMem->flag
187c9 73 26 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d  s&MEM_Str) || pM
187ca 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64  em->enc==desired
187cb 45 6e 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Enc ){.    retur
187cc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
187cd 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d  .  assert( pMem-
187ce 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  >db==0 || sqlite
187cf 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65  3_mutex_held(pMe
187d0 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
187d1 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
187d2 4d 49 54 5f 55 54 46 31 36 0a 20 20 72 65 74 75  MIT_UTF16.  retu
187d3 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
187d4 0a 23 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d  .#else..  /* Mem
187d5 54 72 61 6e 73 6c 61 74 65 28 29 20 6d 61 79 20  Translate() may 
187d6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
187d7 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   or SQLITE_NOMEM
187d8 2e 20 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65  . If NOMEM is re
187d9 74 75 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65  turned,.  ** the
187da 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f  n the encoding o
187db 66 20 74 68 65 20 76 61 6c 75 65 20 6d 61 79 20  f the value may 
187dc 6e 6f 74 20 68 61 76 65 20 63 68 61 6e 67 65 64  not have changed
187dd 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ..  */.  rc = sq
187de 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e  lite3VdbeMemTran
187df 73 6c 61 74 65 28 70 4d 65 6d 2c 20 28 75 38 29  slate(pMem, (u8)
187e0 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61  desiredEnc);.  a
187e1 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45  ssert(rc==SQLITE
187e2 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51  _OK    || rc==SQ
187e3 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61  LITE_NOMEM);.  a
187e4 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45  ssert(rc==SQLITE
187e5 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e  _OK    || pMem->
187e6 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 29  enc!=desiredEnc)
187e7 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53  ;.  assert(rc==S
187e8 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70  QLITE_NOMEM || p
187e9 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65  Mem->enc==desire
187ea 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  dEnc);.  return 
187eb 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  rc;.#endif.}../*
187ec 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 4d  .** Make sure pM
187ed 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20  em->z points to 
187ee 61 20 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63  a writable alloc
187ef 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73  ation of at leas
187f0 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a  t .** n bytes..*
187f1 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f  *.** If the memo
187f2 72 79 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c  ry cell currentl
187f3 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e  y contains strin
187f4 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a  g or blob data.*
187f5 2a 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20  * and the third 
187f6 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
187f7 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
187f8 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 0a 2a   is true, the .*
187f9 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e  * current conten
187fa 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73  t of the cell is
187fb 20 70 72 65 73 65 72 76 65 64 2e 20 4f 74 68 65   preserved. Othe
187fc 72 77 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a  rwise, it may.**
187fd 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20 20   be discarded.  
187fe 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
187ff 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 4d 45  tion sets the ME
18800 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 63  M_Dyn flag and c
18801 6c 65 61 72 73 20 61 6e 79 20 78 44 65 6c 20 63  lears any xDel c
18802 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61  allback..** It a
18803 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45  lso clears MEM_E
18804 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61  phem and MEM_Sta
18805 74 69 63 2e 20 49 66 20 74 68 65 20 70 72 65 73  tic. If the pres
18806 65 72 76 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a  erve flag is .**
18807 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20   not set, Mem.n 
18808 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51  is zeroed..*/.SQ
18809 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1880a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
1880b 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  row(Mem *pMem, i
1880c 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72  nt n, int preser
1880d 76 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 31  ve){.  assert( 1
1880e 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e   >=.    ((pMem->
1880f 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d  zMalloc && pMem-
18810 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e  >zMalloc==pMem->
18811 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20  z) ? 1 : 0) +.  
18812 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73    (((pMem->flags
18813 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d  &MEM_Dyn)&&pMem-
18814 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 20  >xDel) ? 1 : 0) 
18815 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66  + .    ((pMem->f
18816 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20  lags&MEM_Ephem) 
18817 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20  ? 1 : 0) + .    
18818 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ((pMem->flags&ME
18819 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 20 3a 20  M_Static) ? 1 : 
1881a 30 29 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74  0).  );.  assert
1881b 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  ( (pMem->flags&M
1881c 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
1881d 0a 0a 20 20 69 66 28 20 6e 3c 33 32 20 29 20 6e  ..  if( n<32 ) n
1881e 20 3d 20 33 32 3b 0a 20 20 69 66 28 20 73 71 6c   = 32;.  if( sql
1881f 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
18820 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d  (pMem->db, pMem-
18821 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20  >zMalloc)<n ){. 
18822 20 20 20 69 66 28 20 70 72 65 73 65 72 76 65 20     if( preserve 
18823 26 26 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d  && pMem->z==pMem
18824 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20  ->zMalloc ){.   
18825 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65     pMem->z = pMe
18826 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c  m->zMalloc = sql
18827 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
18828 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d  ree(pMem->db, pM
18829 65 6d 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20  em->z, n);.     
1882a 20 70 72 65 73 65 72 76 65 20 3d 20 30 3b 0a 20   preserve = 0;. 
1882b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1882c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d  sqlite3DbFree(pM
1882d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d  em->db, pMem->zM
1882e 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d  alloc);.      pM
1882f 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71  em->zMalloc = sq
18830 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
18831 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20  (pMem->db, n);. 
18832 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
18833 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 6d  preserve && pMem
18834 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61  ->z && pMem->zMa
18835 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21  lloc && pMem->z!
18836 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29  =pMem->zMalloc )
18837 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65  {.    memcpy(pMe
18838 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d  m->zMalloc, pMem
18839 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20  ->z, pMem->n);. 
1883a 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66   }.  if( pMem->f
1883b 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20  lags&MEM_Dyn && 
1883c 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20  pMem->xDel ){.  
1883d 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f    pMem->xDel((vo
1883e 69 64 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b  id *)(pMem->z));
1883f 0a 20 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20  .  }..  pMem->z 
18840 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b  = pMem->zMalloc;
18841 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d  .  if( pMem->z==
18842 30 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66  0 ){.    pMem->f
18843 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
18844 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d  .  }else{.    pM
18845 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  em->flags &= ~(M
18846 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61  EM_Ephem|MEM_Sta
18847 74 69 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d  tic);.  }.  pMem
18848 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65  ->xDel = 0;.  re
18849 74 75 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20  turn (pMem->z ? 
1884a 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
1884b 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a  TE_NOMEM);.}../*
1884c 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 76  .** Make the giv
1884d 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45  en Mem object ME
1884e 4d 5f 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72  M_Dyn.  In other
1884f 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74 20   words, make it 
18850 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20 54  so.** that any T
18851 45 58 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74  EXT or BLOB cont
18852 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ent is stored in
18853 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
18854 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28   from.** malloc(
18855 29 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c  ).  In this way,
18856 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68   we know that th
18857 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 61 66 65  e memory is safe
18858 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72   to be.** overwr
18859 69 74 74 65 6e 20 6f 72 20 61 6c 74 65 72 65 64  itten or altered
1885a 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
1885b 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
1885c 65 73 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f  ess or SQLITE_NO
1885d 4d 45 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61  MEM if malloc fa
1885e 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
1885f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18860 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69  e3VdbeMemMakeWri
18861 74 65 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d  teable(Mem *pMem
18862 29 7b 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61 73  ){.  int f;.  as
18863 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
18864 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
18865 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
18866 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
18867 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
18868 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d  gs&MEM_RowSet)==
18869 30 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f  0 );.  expandBlo
1886a 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70  b(pMem);.  f = p
1886b 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66  Mem->flags;.  if
1886c 28 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45  ( (f&(MEM_Str|ME
1886d 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d  M_Blob)) && pMem
1886e 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  ->z!=pMem->zMall
1886f 6f 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  oc ){.    if( sq
18870 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
18871 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b  (pMem, pMem->n +
18872 20 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20   2, 1) ){.      
18873 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
18874 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  MEM;.    }.    p
18875 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20  Mem->z[pMem->n] 
18876 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  = 0;.    pMem->z
18877 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b  [pMem->n+1] = 0;
18878 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
18879 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20   |= MEM_Term;.  
1887a 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
1887b 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1887c 49 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d  If the given Mem
1887d 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c  * has a zero-fil
1887e 6c 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69  led tail, turn i
1887f 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61  t into an ordina
18880 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65  ry.** blob store
18881 64 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79  d in dynamically
18882 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
18883 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
18884 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
18885 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  B.SQLITE_PRIVATE
18886 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
18887 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65  MemExpandBlob(Me
18888 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20  m *pMem){.  if( 
18889 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
1888a 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e  M_Zero ){.    in
1888b 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73  t nByte;.    ass
1888c 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
1888d 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20  &MEM_Blob );.   
1888e 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
1888f 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74  flags&MEM_RowSet
18890 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  )==0 );.    asse
18891 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
18892 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
18893 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
18894 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f  mutex) );..    /
18895 2a 20 53 65 74 20 6e 42 79 74 65 20 74 6f 20 74  * Set nByte to t
18896 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
18897 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73  es required to s
18898 74 6f 72 65 20 74 68 65 20 65 78 70 61 6e 64 65  tore the expande
18899 64 20 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e  d blob. */.    n
1889a 42 79 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b  Byte = pMem->n +
1889b 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a   pMem->u.nZero;.
1889c 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30      if( nByte<=0
1889d 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20   ){.      nByte 
1889e 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 1;.    }.    i
1889f 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
188a0 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74  mGrow(pMem, nByt
188a1 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72  e, 1) ){.      r
188a2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
188a3 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d  EM;.    }..    m
188a4 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70  emset(&pMem->z[p
188a5 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d  Mem->n], 0, pMem
188a6 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20  ->u.nZero);.    
188a7 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d  pMem->n += pMem-
188a8 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d  >u.nZero;.    pM
188a9 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  em->flags &= ~(M
188aa 45 4d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d  EM_Zero|MEM_Term
188ab 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
188ac 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
188ad 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  dif.../*.** Make
188ae 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e 20   sure the given 
188af 4d 65 6d 20 69 73 20 5c 75 30 30 30 30 20 74 65  Mem is \u0000 te
188b0 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  rminated..*/.SQL
188b1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
188b2 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
188b3 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a  lTerminate(Mem *
188b4 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  pMem){.  assert(
188b5 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
188b6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
188b7 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
188b8 65 78 29 20 29 3b 0a 20 20 69 66 28 20 28 70 4d  ex) );.  if( (pM
188b9 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
188ba 54 65 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65  Term)!=0 || (pMe
188bb 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53  m->flags & MEM_S
188bc 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  tr)==0 ){.    re
188bd 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
188be 20 20 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20    /* Nothing to 
188bf 64 6f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  do */.  }.  if( 
188c0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
188c1 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e  ow(pMem, pMem->n
188c2 2b 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 72 65  +2, 1) ){.    re
188c3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
188c4 4d 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a  M;.  }.  pMem->z
188c5 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20  [pMem->n] = 0;. 
188c6 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e   pMem->z[pMem->n
188c7 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d  +1] = 0;.  pMem-
188c8 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65  >flags |= MEM_Te
188c9 72 6d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  rm;.  return SQL
188ca 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
188cb 20 41 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20   Add MEM_Str to 
188cc 74 68 65 20 73 65 74 20 6f 66 20 72 65 70 72 65  the set of repre
188cd 73 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74  sentations for t
188ce 68 65 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e  he given Mem.  N
188cf 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f  umbers.** are co
188d0 6e 76 65 72 74 65 64 20 75 73 69 6e 67 20 73 71  nverted using sq
188d1 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
188d2 2e 20 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 20  .  Converting a 
188d3 42 4c 4f 42 20 74 6f 20 61 20 73 74 72 69 6e 67  BLOB to a string
188d4 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  .** is a no-op..
188d5 2a 2a 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 72  **.** Existing r
188d6 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d  epresentations M
188d7 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52  EM_Int and MEM_R
188d8 65 61 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e  eal are *not* in
188d9 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  validated..**.**
188da 20 41 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75   A MEM_Null valu
188db 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  e will never be 
188dc 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
188dd 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 75  unction. This fu
188de 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65  nction is.** use
188df 64 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67  d for converting
188e0 20 76 61 6c 75 65 73 20 74 6f 20 74 65 78 74 20   values to text 
188e1 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 74 6f  for returning to
188e2 20 74 68 65 20 75 73 65 72 20 28 69 2e 65 2e 20   the user (i.e. 
188e3 76 69 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  via.** sqlite3_v
188e4 61 6c 75 65 5f 74 65 78 74 28 29 29 2c 20 6f 72  alue_text()), or
188e5 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68   for ensuring th
188e6 61 74 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20  at values to be 
188e7 75 73 65 64 20 61 73 20 62 74 72 65 65 0a 2a 2a  used as btree.**
188e8 20 6b 65 79 73 20 61 72 65 20 73 74 72 69 6e 67   keys are string
188e9 73 2e 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 72  s. In the former
188ea 20 63 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69   case a NULL poi
188eb 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
188ec 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64   the.** user and
188ed 20 74 68 65 20 6c 61 74 65 72 20 69 73 20 61 6e   the later is an
188ee 20 69 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 61   internal progra
188ef 6d 6d 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a  mming error..*/.
188f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
188f1 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
188f2 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a  mStringify(Mem *
188f3 70 4d 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a  pMem, int enc){.
188f4 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
188f5 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d  E_OK;.  int fg =
188f6 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20   pMem->flags;.  
188f7 63 6f 6e 73 74 20 69 6e 74 20 6e 42 79 74 65 20  const int nByte 
188f8 3d 20 33 32 3b 0a 0a 20 20 61 73 73 65 72 74 28  = 32;..  assert(
188f9 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
188fa 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
188fb 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
188fc 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
188fd 20 21 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20   !(fg&MEM_Zero) 
188fe 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 66  );.  assert( !(f
188ff 67 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  g&(MEM_Str|MEM_B
18900 6c 6f 62 29 29 20 29 3b 0a 20 20 61 73 73 65 72  lob)) );.  asser
18901 74 28 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d  t( fg&(MEM_Int|M
18902 45 4d 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61 73  EM_Real) );.  as
18903 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
18904 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d  gs&MEM_RowSet)==
18905 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45  0 );.  assert( E
18906 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
18907 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20  ENT(pMem) );... 
18908 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
18909 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42  MemGrow(pMem, nB
1890a 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72  yte, 0) ){.    r
1890b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1890c 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f  EM;.  }..  /* Fo
1890d 72 20 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 65  r a Real or Inte
1890e 67 65 72 2c 20 75 73 65 20 73 71 6c 69 74 65 33  ger, use sqlite3
1890f 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20 70 72  _mprintf() to pr
18910 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d 38 0a  oduce the UTF-8.
18911 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72    ** string repr
18912 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
18913 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69  e value. Then, i
18914 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 65  f the required e
18915 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20  ncoding.  ** is 
18916 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d  UTF-16le or UTF-
18917 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e 73 6c  16be do a transl
18918 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a  ation..  ** .  *
18919 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77 6f 75  * FIX ME: It wou
1891a 6c 64 20 62 65 20 62 65 74 74 65 72 20 69 66 20  ld be better if 
1891b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1891c 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d  () could do UTF-
1891d 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66  16..  */.  if( f
1891e 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  g & MEM_Int ){. 
1891f 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
18920 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d  ntf(nByte, pMem-
18921 3e 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d  >z, "%lld", pMem
18922 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b  ->u.i);.  }else{
18923 0a 20 20 20 20 61 73 73 65 72 74 28 20 66 67 20  .    assert( fg 
18924 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20  & MEM_Real );.  
18925 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
18926 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e  tf(nByte, pMem->
18927 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 4d 65  z, "%!.15g", pMe
18928 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65  m->r);.  }.  pMe
18929 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  m->n = sqlite3St
1892a 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b  rlen30(pMem->z);
1892b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53  .  pMem->enc = S
1892c 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d  QLITE_UTF8;.  pM
1892d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  em->flags |= MEM
1892e 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20  _Str|MEM_Term;. 
1892f 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
18930 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c  geEncoding(pMem,
18931 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20   enc);.  return 
18932 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d  rc;.}../*.** Mem
18933 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f  ory cell pMem co
18934 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65  ntains the conte
18935 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61  xt of an aggrega
18936 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20  te function..** 
18937 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c  This routine cal
18938 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20  ls the finalize 
18939 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20  method for that 
1893a 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a  function.  The.*
1893b 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  * result of the 
1893c 61 67 67 72 65 67 61 74 65 20 69 73 20 73 74 6f  aggregate is sto
1893d 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d  red back into pM
1893e 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  em..**.** Return
1893f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66   SQLITE_ERROR if
18940 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72   the finalizer r
18941 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e  eports an error.
18942 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f    SQLITE_OK.** o
18943 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c  therwise..*/.SQL
18944 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
18945 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69  sqlite3VdbeMemFi
18946 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d  nalize(Mem *pMem
18947 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63  , FuncDef *pFunc
18948 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
18949 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
1894a 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78  Func && pFunc->x
1894b 46 69 6e 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20  Finalize ){.    
1894c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1894d 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ctx;.    assert(
1894e 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
1894f 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20  MEM_Null)!=0 || 
18950 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70  pFunc==pMem->u.p
18951 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65 72  Def );.    asser
18952 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
18953 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
18954 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
18955 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d 65 6d  utex) );.    mem
18956 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69 7a  set(&ctx, 0, siz
18957 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 20 63  eof(ctx));.    c
18958 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d  tx.s.flags = MEM
18959 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73  _Null;.    ctx.s
1895a 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a  .db = pMem->db;.
1895b 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70      ctx.pMem = p
1895c 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46 75  Mem;.    ctx.pFu
1895d 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20  nc = pFunc;.    
1895e 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65  pFunc->xFinalize
1895f 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 73 65  (&ctx);.    asse
18960 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c  rt( 0==(pMem->fl
18961 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20  ags&MEM_Dyn) && 
18962 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20  !pMem->xDel );. 
18963 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
18964 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d  (pMem->db, pMem-
18965 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 6d  >zMalloc);.    m
18966 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74 78  emcpy(pMem, &ctx
18967 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e 73  .s, sizeof(ctx.s
18968 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 28 63 74  ));.    rc = (ct
18969 78 2e 69 73 45 72 72 6f 72 3f 53 51 4c 49 54 45  x.isError?SQLITE
1896a 5f 45 52 52 4f 52 3a 53 51 4c 49 54 45 5f 4f 4b  _ERROR:SQLITE_OK
1896b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1896c 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  rc;.}../*.** If 
1896d 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
1896e 63 6f 6e 74 61 69 6e 73 20 61 20 73 74 72 69 6e  contains a strin
1896f 67 20 76 61 6c 75 65 20 74 68 61 74 20 6d 75 73  g value that mus
18970 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a 2a  t be freed by.**
18971 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74   invoking an ext
18972 65 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20  ernal callback, 
18973 66 72 65 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c  free it now. Cal
18974 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69  ling this functi
18975 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66  on.** does not f
18976 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c  ree any Mem.zMal
18977 6c 6f 63 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53  loc buffer..*/.S
18978 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
18979 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
1897a 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c  mReleaseExternal
1897b 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65  (Mem *p){.  asse
1897c 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20  rt( p->db==0 || 
1897d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1897e 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
1897f 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61   );.  if( p->fla
18980 67 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f  gs&(MEM_Agg|MEM_
18981 44 79 6e 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20  Dyn|MEM_RowSet) 
18982 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c  ){.    if( p->fl
18983 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a 20  ags&MEM_Agg ){. 
18984 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
18985 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 70  MemFinalize(p, p
18986 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 20  ->u.pDef);.     
18987 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61   assert( (p->fla
18988 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30  gs & MEM_Agg)==0
18989 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1898a 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1898b 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  p);.    }else if
1898c 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44  ( p->flags&MEM_D
1898d 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b  yn && p->xDel ){
1898e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
1898f 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77  p->flags&MEM_Row
18990 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Set)==0 );.     
18991 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a   p->xDel((void *
18992 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 2d  )p->z);.      p-
18993 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 7d  >xDel = 0;.    }
18994 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67  else if( p->flag
18995 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a  s&MEM_RowSet ){.
18996 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77        sqlite3Row
18997 53 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 52  SetClear(p->u.pR
18998 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20  owSet);.    }.  
18999 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  }.}../*.** Relea
1899a 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79 20 68 65  se any memory he
1899b 6c 64 20 62 79 20 74 68 65 20 4d 65 6d 2e 20 54  ld by the Mem. T
1899c 68 69 73 20 6d 61 79 20 6c 65 61 76 65 20 74 68  his may leave th
1899d 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20 69  e Mem in an.** i
1899e 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74  nconsistent stat
1899f 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 77  e, for example w
189a0 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20 61  ith (Mem.z==0) a
189a1 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 3d  nd.** (Mem.type=
189a2 3d 53 51 4c 49 54 45 5f 54 45 58 54 29 2e 0a 2a  =SQLITE_TEXT)..*
189a3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
189a4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
189a5 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20  eMemRelease(Mem 
189a6 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64  *p){.  sqlite3Vd
189a7 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65  beMemReleaseExte
189a8 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71 6c 69 74  rnal(p);.  sqlit
189a9 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20  e3DbFree(p->db, 
189aa 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 70  p->zMalloc);.  p
189ab 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a 4d  ->z = 0;.  p->zM
189ac 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 70 2d 3e  alloc = 0;.  p->
189ad 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  xDel = 0;.}../*.
189ae 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 36 34 2d  ** Convert a 64-
189af 62 69 74 20 49 45 45 45 20 64 6f 75 62 6c 65 20  bit IEEE double 
189b0 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69  into a 64-bit si
189b1 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a  gned integer..**
189b2 20 49 66 20 74 68 65 20 64 6f 75 62 6c 65 20 69   If the double i
189b3 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 72 65 74  s too large, ret
189b4 75 72 6e 20 30 78 38 30 30 30 30 30 30 30 30 30  urn 0x8000000000
189b5 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 2a 20 4d 6f  000000..**.** Mo
189b6 73 74 20 73 79 73 74 65 6d 73 20 61 70 70 65 61  st systems appea
189b7 72 20 74 6f 20 64 6f 20 74 68 69 73 20 73 69 6d  r to do this sim
189b8 70 6c 79 20 62 79 20 61 73 73 69 67 6e 69 6e 67  ply by assigning
189b9 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e  .** variables an
189ba 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 65 78  d without the ex
189bb 74 72 61 20 72 61 6e 67 65 20 74 65 73 74 73 2e  tra range tests.
189bc 20 20 42 75 74 0a 2a 2a 20 74 68 65 72 65 20 61    But.** there a
189bd 72 65 20 72 65 70 6f 72 74 73 20 74 68 61 74 20  re reports that 
189be 77 69 6e 64 6f 77 73 20 74 68 72 6f 77 73 20 61  windows throws a
189bf 6e 20 65 78 70 65 63 74 69 6f 6e 0a 2a 2a 20 69  n expection.** i
189c0 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70  f the floating p
189c1 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 6f 75  oint value is ou
189c2 74 20 6f 66 20 72 61 6e 67 65 2e 20 28 53 65 65  t of range. (See
189c3 20 74 69 63 6b 65 74 20 23 32 38 38 30 2e 29 0a   ticket #2880.).
189c4 2a 2a 20 42 65 63 61 75 73 65 20 77 65 20 64 6f  ** Because we do
189c5 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20   not completely 
189c6 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 70  understand the p
189c7 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69 6c 6c 0a  roblem, we will.
189c8 2a 2a 20 74 61 6b 65 20 74 68 65 20 63 6f 6e 73  ** take the cons
189c9 65 72 76 61 74 69 76 65 20 61 70 70 72 6f 61 63  ervative approac
189ca 68 20 61 6e 64 20 61 6c 77 61 79 73 20 64 6f 20  h and always do 
189cb 72 61 6e 67 65 20 74 65 73 74 73 0a 2a 2a 20 62  range tests.** b
189cc 65 66 6f 72 65 20 61 74 74 65 6d 70 74 69 6e 67  efore attempting
189cd 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e   the conversion.
189ce 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 64  .*/.static i64 d
189cf 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 64 6f 75  oubleToInt64(dou
189d0 62 6c 65 20 72 29 7b 0a 20 20 2f 2a 0a 20 20 2a  ble r){.  /*.  *
189d1 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 6c 65 72 73  * Many compilers
189d2 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 20 64 6f   we encounter do
189d3 20 6e 6f 74 20 64 65 66 69 6e 65 20 63 6f 6e 73   not define cons
189d4 74 61 6e 74 73 20 66 6f 72 20 74 68 65 0a 20 20  tants for the.  
189d5 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 6e 64 20 6d  ** minimum and m
189d6 61 78 69 6d 75 6d 20 36 34 2d 62 69 74 20 69 6e  aximum 64-bit in
189d7 74 65 67 65 72 73 2c 20 6f 72 20 74 68 65 79 20  tegers, or they 
189d8 64 65 66 69 6e 65 20 74 68 65 6d 0a 20 20 2a 2a  define them.  **
189d9 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e   inconsistently.
189da 20 20 41 6e 64 20 6d 61 6e 79 20 64 6f 20 6e 6f    And many do no
189db 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65  t understand the
189dc 20 22 4c 4c 22 20 6e 6f 74 61 74 69 6f 6e 2e 0a   "LL" notation..
189dd 20 20 2a 2a 20 53 6f 20 77 65 20 64 65 66 69 6e    ** So we defin
189de 65 20 6f 75 72 20 6f 77 6e 20 73 74 61 74 69 63  e our own static
189df 20 63 6f 6e 73 74 61 6e 74 73 20 68 65 72 65 20   constants here 
189e0 75 73 69 6e 67 20 6e 6f 74 68 69 6e 67 0a 20 20  using nothing.  
189e1 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 61  ** larger than a
189e2 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
189e3 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 2a 2f 0a 20  constant..  */. 
189e4 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36   static const i6
189e5 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41 52 47 45  4 maxInt = LARGE
189e6 53 54 5f 49 4e 54 36 34 3b 0a 20 20 73 74 61 74  ST_INT64;.  stat
189e7 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d 69 6e  ic const i64 min
189e8 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53 54 5f 49  Int = SMALLEST_I
189e9 4e 54 36 34 3b 0a 0a 20 20 69 66 28 20 72 3c 28  NT64;..  if( r<(
189ea 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74 20 29 7b  double)minInt ){
189eb 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49  .    return minI
189ec 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  nt;.  }else if( 
189ed 72 3e 28 64 6f 75 62 6c 65 29 6d 61 78 49 6e 74  r>(double)maxInt
189ee 20 29 7b 0a 20 20 20 20 2f 2a 20 6d 69 6e 49 6e   ){.    /* minIn
189ef 74 20 69 73 20 63 6f 72 72 65 63 74 20 68 65 72  t is correct her
189f0 65 20 2d 20 6e 6f 74 20 6d 61 78 49 6e 74 2e 20  e - not maxInt. 
189f1 20 49 74 20 74 75 72 6e 73 20 6f 75 74 20 74 68   It turns out th
189f2 61 74 20 61 73 73 69 67 6e 69 6e 67 0a 20 20 20  at assigning.   
189f3 20 2a 2a 20 61 20 76 65 72 79 20 6c 61 72 67 65   ** a very large
189f4 20 70 6f 73 69 74 69 76 65 20 6e 75 6d 62 65 72   positive number
189f5 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 72   to an integer r
189f6 65 73 75 6c 74 73 20 69 6e 20 61 20 76 65 72 79  esults in a very
189f7 20 6c 61 72 67 65 0a 20 20 20 20 2a 2a 20 6e 65   large.    ** ne
189f8 67 61 74 69 76 65 20 69 6e 74 65 67 65 72 2e 20  gative integer. 
189f9 20 54 68 69 73 20 6d 61 6b 65 73 20 6e 6f 20 73   This makes no s
189fa 65 6e 73 65 2c 20 62 75 74 20 69 74 20 69 73 20  ense, but it is 
189fb 77 68 61 74 20 78 38 36 20 68 61 72 64 77 61 72  what x86 hardwar
189fc 65 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 73 6f  e.    ** does so
189fd 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69   for compatibili
189fe 74 79 20 77 65 20 77 69 6c 6c 20 64 6f 20 74 68  ty we will do th
189ff 65 20 73 61 6d 65 20 69 6e 20 73 6f 66 74 77 61  e same in softwa
18a00 72 65 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  re. */.    retur
18a01 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73  n minInt;.  }els
18a02 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 69  e{.    return (i
18a03 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  64)r;.  }.}../*.
18a04 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d 65 20 6b  ** Return some k
18a05 69 6e 64 20 6f 66 20 69 6e 74 65 67 65 72 20 76  ind of integer v
18a06 61 6c 75 65 20 77 68 69 63 68 20 69 73 20 74 68  alue which is th
18a07 65 20 62 65 73 74 20 77 65 20 63 61 6e 20 64 6f  e best we can do
18a08 0a 2a 2a 20 61 74 20 72 65 70 72 65 73 65 6e 74  .** at represent
18a09 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 74 68  ing the value th
18a0a 61 74 20 2a 70 4d 65 6d 20 64 65 73 63 72 69 62  at *pMem describ
18a0b 65 73 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72  es as an integer
18a0c 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20  ..** If pMem is 
18a0d 61 6e 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e  an integer, then
18a0e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 65 78   the value is ex
18a0f 61 63 74 2e 20 20 49 66 20 70 4d 65 6d 20 69 73  act.  If pMem is
18a10 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70  .** a floating-p
18a11 6f 69 6e 74 20 74 68 65 6e 20 74 68 65 20 76 61  oint then the va
18a12 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
18a13 74 68 65 20 69 6e 74 65 67 65 72 20 70 61 72 74  the integer part
18a14 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20  ..** If pMem is 
18a15 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  a string or blob
18a16 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b 65 20 61  , then we make a
18a17 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e  n attempt to con
18a18 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e 74 6f 20  vert.** it into 
18a19 61 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72 65  a integer and re
18a1a 74 75 72 6e 20 74 68 61 74 2e 20 20 49 66 20 70  turn that.  If p
18a1b 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61  Mem represents a
18a1c 6e 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e 55 4c 4c  n.** an SQL-NULL
18a1d 20 76 61 6c 75 65 2c 20 72 65 74 75 72 6e 20 30   value, return 0
18a1e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20  ..**.** If pMem 
18a1f 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 74 72  represents a str
18a20 69 6e 67 20 76 61 6c 75 65 2c 20 69 74 73 20 65  ing value, its e
18a21 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 62 65  ncoding might be
18a22 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c   changed..*/.SQL
18a23 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20  ITE_PRIVATE i64 
18a24 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
18a25 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  lue(Mem *pMem){.
18a26 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 61    int flags;.  a
18a27 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
18a28 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
18a29 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
18a2a 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
18a2b 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54  ssert( EIGHT_BYT
18a2c 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d  E_ALIGNMENT(pMem
18a2d 29 20 29 3b 0a 20 20 66 6c 61 67 73 20 3d 20 70  ) );.  flags = p
18a2e 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66  Mem->flags;.  if
18a2f 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ( flags & MEM_In
18a30 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
18a31 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c  pMem->u.i;.  }el
18a32 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  se if( flags & M
18a33 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  EM_Real ){.    r
18a34 65 74 75 72 6e 20 64 6f 75 62 6c 65 54 6f 49 6e  eturn doubleToIn
18a35 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20  t64(pMem->r);.  
18a36 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20  }else if( flags 
18a37 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
18a38 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 36 34 20  lob) ){.    i64 
18a39 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d 65 6d 2d  value;.    pMem-
18a3a 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
18a3b 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  r;.    if( sqlit
18a3c 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
18a3d 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54  ding(pMem, SQLIT
18a3e 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c  E_UTF8).       |
18a3f 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  | sqlite3VdbeMem
18a40 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65  NulTerminate(pMe
18a41 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  m) ){.      retu
18a42 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
18a43 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20  assert( pMem->z 
18a44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74  );.    sqlite3At
18a45 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c 20 26 76  oi64(pMem->z, &v
18a46 61 6c 75 65 29 3b 0a 20 20 20 20 72 65 74 75 72  alue);.    retur
18a47 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65 6c 73 65  n value;.  }else
18a48 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
18a49 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
18a4a 75 72 6e 20 74 68 65 20 62 65 73 74 20 72 65 70  urn the best rep
18a4b 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70  resentation of p
18a4c 4d 65 6d 20 74 68 61 74 20 77 65 20 63 61 6e 20  Mem that we can 
18a4d 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a 20 64 6f  get into a.** do
18a4e 75 62 6c 65 2e 20 20 49 66 20 70 4d 65 6d 20 69  uble.  If pMem i
18a4f 73 20 61 6c 72 65 61 64 79 20 61 20 64 6f 75 62  s already a doub
18a50 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65 67 65 72  le or an integer
18a51 2c 20 72 65 74 75 72 6e 20 69 74 73 0a 2a 2a 20  , return its.** 
18a52 76 61 6c 75 65 2e 20 20 49 66 20 69 74 20 69 73  value.  If it is
18a53 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
18a54 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72  b, try to conver
18a55 74 20 69 74 20 74 6f 20 61 20 64 6f 75 62 6c 65  t it to a double
18a56 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 61 20  ..** If it is a 
18a57 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 30  NULL, return 0.0
18a58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18a59 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  ATE double sqlit
18a5a 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
18a5b 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
18a5c 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
18a5d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
18a5e 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
18a5f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
18a60 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
18a61 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29  _ALIGNMENT(pMem)
18a62 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e   );.  if( pMem->
18a63 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c  flags & MEM_Real
18a64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
18a65 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65 20  Mem->r;.  }else 
18a66 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
18a67 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
18a68 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29   return (double)
18a69 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c  pMem->u.i;.  }el
18a6a 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  se if( pMem->fla
18a6b 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45  gs & (MEM_Str|ME
18a6c 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 2f  M_Blob) ){.    /
18a6d 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63  * (double)0 In c
18a6e 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
18a6f 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
18a70 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 64 6f 75 62  T... */.    doub
18a71 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75 62 6c 65  le val = (double
18a72 29 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  )0;.    pMem->fl
18a73 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a  ags |= MEM_Str;.
18a74 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
18a75 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
18a76 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55  g(pMem, SQLITE_U
18a77 54 46 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 73  TF8).       || s
18a78 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c  qlite3VdbeMemNul
18a79 54 65 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20  Terminate(pMem) 
18a7a 29 7b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75  ){.      /* (dou
18a7b 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66  ble)0 In case of
18a7c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
18a7d 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a  ATING_POINT... *
18a7e 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28  /.      return (
18a7f 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 7d 0a  double)0;.    }.
18a80 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
18a81 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ->z );.    sqlit
18a82 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e 7a 2c 20  e3AtoF(pMem->z, 
18a83 26 76 61 6c 29 3b 0a 20 20 20 20 72 65 74 75 72  &val);.    retur
18a84 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a  n val;.  }else{.
18a85 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30      /* (double)0
18a86 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
18a87 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
18a88 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20  _POINT... */.   
18a89 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29   return (double)
18a8a 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
18a8b 54 68 65 20 4d 45 4d 20 73 74 72 75 63 74 75 72  The MEM structur
18a8c 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 4d  e is already a M
18a8d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79 20 74 6f  EM_Real.  Try to
18a8e 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74 20 61 0a   also make it a.
18a8f 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66 20 77 65  ** MEM_Int if we
18a90 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   can..*/.SQLITE_
18a91 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
18a92 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41  ite3VdbeIntegerA
18a93 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 4d 65  ffinity(Mem *pMe
18a94 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
18a95 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
18a96 52 65 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  Real );.  assert
18a97 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
18a98 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
18a99 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
18a9a 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
18a9b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
18a9c 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
18a9d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  );.  assert( EIG
18a9e 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
18a9f 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20 70 4d  T(pMem) );..  pM
18aa0 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75 62 6c 65  em->u.i = double
18aa1 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29  ToInt64(pMem->r)
18aa2 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d  ;.  if( pMem->r=
18aa3 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75  =(double)pMem->u
18aa4 2e 69 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  .i ){.    pMem->
18aa5 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74  flags |= MEM_Int
18aa6 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
18aa7 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20 74  onvert pMem to t
18aa8 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49 6e  ype integer.  In
18aa9 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69  validate any pri
18aaa 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  or representatio
18aab 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ns..*/.SQLITE_PR
18aac 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18aad 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
18aae 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  fy(Mem *pMem){. 
18aaf 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
18ab0 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
18ab1 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
18ab2 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
18ab3 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
18ab4 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
18ab5 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  et)==0 );.  asse
18ab6 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
18ab7 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29  LIGNMENT(pMem) )
18ab8 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  ;..  pMem->u.i =
18ab9 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
18aba 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65  alue(pMem);.  Me
18abb 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65  mSetTypeFlag(pMe
18abc 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72  m, MEM_Int);.  r
18abd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
18abe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
18abf 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69  t pMem so that i
18ac0 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45 4d  t is of type MEM
18ac1 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69  _Real..** Invali
18ac2 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72  date any prior r
18ac3 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a  epresentations..
18ac4 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18ac5 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
18ac6 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20  eMemRealify(Mem 
18ac7 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74  *pMem){.  assert
18ac8 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
18ac9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
18aca 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
18acb 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
18acc 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
18acd 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a  GNMENT(pMem) );.
18ace 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c  .  pMem->r = sql
18acf 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
18ad0 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65  e(pMem);.  MemSe
18ad1 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20  tTypeFlag(pMem, 
18ad2 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74  MEM_Real);.  ret
18ad3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
18ad4 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
18ad5 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20  pMem so that it 
18ad6 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 65  has types MEM_Re
18ad7 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72  al or MEM_Int or
18ad8 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69   both..** Invali
18ad9 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72  date any prior r
18ada 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a  epresentations..
18adb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18adc 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
18add 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d  eMemNumerify(Mem
18ade 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c   *pMem){.  doubl
18adf 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 20  e r1, r2;.  i64 
18ae0 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  i;.  assert( (pM
18ae1 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  em->flags & (MEM
18ae2 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45  _Int|MEM_Real|ME
18ae3 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20  M_Null))==0 );. 
18ae4 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
18ae5 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f  flags & (MEM_Blo
18ae6 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 29  b|MEM_Str))!=0 )
18ae7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ;.  assert( pMem
18ae8 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
18ae9 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
18aea 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
18aeb 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33  ;.  r1 = sqlite3
18aec 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d  VdbeRealValue(pM
18aed 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c  em);.  i = doubl
18aee 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 20  eToInt64(r1);.  
18aef 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a  r2 = (double)i;.
18af0 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a    if( r1==r2 ){.
18af1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
18af2 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d 65  emIntegerify(pMe
18af3 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  m);.  }else{.   
18af4 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20   pMem->r = r1;. 
18af5 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
18af6 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c  g(pMem, MEM_Real
18af7 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
18af8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
18af9 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70  .** Delete any p
18afa 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e  revious value an
18afb 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  d set the value 
18afc 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20  stored in *pMem 
18afd 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49  to NULL..*/.SQLI
18afe 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
18aff 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
18b00 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29  tNull(Mem *pMem)
18b01 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c  {.  if( pMem->fl
18b02 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
18b03 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52   ){.    sqlite3R
18b04 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d  owSetClear(pMem-
18b05 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 7d  >u.pRowSet);.  }
18b06 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
18b07 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c  g(pMem, MEM_Null
18b08 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  );.  pMem->type 
18b09 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d  = SQLITE_NULL;.}
18b0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
18b0b 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75  ny previous valu
18b0c 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61  e and set the va
18b0d 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42  lue to be a BLOB
18b0e 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20   of length.** n 
18b0f 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a  containing all z
18b10 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eros..*/.SQLITE_
18b11 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
18b12 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65  ite3VdbeMemSetZe
18b13 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d  roBlob(Mem *pMem
18b14 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69  , int n){.  sqli
18b15 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
18b16 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d  e(pMem);.  pMem-
18b17 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f  >flags = MEM_Blo
18b18 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d  b|MEM_Zero;.  pM
18b19 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
18b1a 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e  E_BLOB;.  pMem->
18b1b 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30  n = 0;.  if( n<0
18b1c 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d   ) n = 0;.  pMem
18b1d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a 20  ->u.nZero = n;. 
18b1e 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
18b1f 49 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a 0a  ITE_UTF8;.}../*.
18b20 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72  ** Delete any pr
18b21 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64  evious value and
18b22 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73   set the value s
18b23 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74  tored in *pMem t
18b24 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65  o val,.** manife
18b25 73 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e  st type INTEGER.
18b26 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18b27 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
18b28 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d  dbeMemSetInt64(M
18b29 65 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61  em *pMem, i64 va
18b2a 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  l){.  sqlite3Vdb
18b2b 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
18b2c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  );.  pMem->u.i =
18b2d 20 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c   val;.  pMem->fl
18b2e 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
18b2f 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
18b30 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a  LITE_INTEGER;.}.
18b31 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
18b32 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
18b33 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
18b34 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d  ue stored in *pM
18b35 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61  em to val,.** ma
18b36 6e 69 66 65 73 74 20 74 79 70 65 20 52 45 41 4c  nifest type REAL
18b37 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18b38 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
18b39 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65  VdbeMemSetDouble
18b3a 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62  (Mem *pMem, doub
18b3b 6c 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 73  le val){.  if( s
18b3c 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 29  qlite3IsNaN(val)
18b3d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
18b3e 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d  dbeMemSetNull(pM
18b3f 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  em);.  }else{.  
18b40 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
18b41 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
18b42 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c     pMem->r = val
18b43 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
18b44 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20  s = MEM_Real;.  
18b45 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
18b46 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d  QLITE_FLOAT;.  }
18b47 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
18b48 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61   any previous va
18b49 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20  lue and set the 
18b4a 76 61 6c 75 65 20 6f 66 20 70 4d 65 6d 20 74 6f  value of pMem to
18b4b 20 62 65 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 20   be an.** empty 
18b4c 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 2e 0a 2a  boolean index..*
18b4d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18b4e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
18b4f 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65  eMemSetRowSet(Me
18b50 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 73 71 6c 69  m *pMem){.  sqli
18b51 74 65 33 20 2a 64 62 20 3d 20 70 4d 65 6d 2d 3e  te3 *db = pMem->
18b52 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62  db;.  assert( db
18b53 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4d 65  !=0 );.  if( pMe
18b54 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  m->flags & MEM_R
18b55 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c  owSet ){.    sql
18b56 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28  ite3RowSetClear(
18b57 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29  pMem->u.pRowSet)
18b58 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
18b59 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
18b5a 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
18b5b 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pMem->zMalloc = 
18b5c 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
18b5d 61 77 28 64 62 2c 20 36 34 29 3b 0a 20 20 7d 0a  aw(db, 64);.  }.
18b5e 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
18b5f 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 4d  Failed ){.    pM
18b60 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
18b61 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Null;.  }else{. 
18b62 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
18b63 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20  >zMalloc );.    
18b64 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 20  pMem->u.pRowSet 
18b65 3d 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49  = sqlite3RowSetI
18b66 6e 69 74 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d  nit(db, pMem->zM
18b67 61 6c 6c 6f 63 2c 20 0a 20 20 20 20 20 20 20 20  alloc, .        
18b68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
18b6a 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
18b6b 7a 65 28 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61  ze(db, pMem->zMa
18b6c 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 61 73 73 65  lloc));.    asse
18b6d 72 74 28 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77  rt( pMem->u.pRow
18b6e 53 65 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 4d  Set!=0 );.    pM
18b6f 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
18b70 52 6f 77 53 65 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f  RowSet;.  }.}../
18b71 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
18b72 20 69 66 20 74 68 65 20 4d 65 6d 20 6f 62 6a 65   if the Mem obje
18b73 63 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 54 45  ct contains a TE
18b74 58 54 20 6f 72 20 42 4c 4f 42 20 74 68 61 74 20  XT or BLOB that 
18b75 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61 72 67 65 20  is.** too large 
18b76 2d 20 77 68 6f 73 65 20 73 69 7a 65 20 65 78 63  - whose size exc
18b77 65 65 64 73 20 53 51 4c 49 54 45 5f 4d 41 58 5f  eeds SQLITE_MAX_
18b78 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54  LENGTH..*/.SQLIT
18b79 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
18b7a 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42  lite3VdbeMemTooB
18b7b 69 67 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73  ig(Mem *p){.  as
18b7c 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29  sert( p->db!=0 )
18b7d 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  ;.  if( p->flags
18b7e 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f   & (MEM_Str|MEM_
18b7f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 6e 74  Blob) ){.    int
18b80 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 20 20 69   n = p->n;.    i
18b81 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45  f( p->flags & ME
18b82 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
18b83 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b  n += p->u.nZero;
18b84 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
18b85 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d 69  n n>p->db->aLimi
18b86 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
18b87 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72 65  ENGTH];.  }.  re
18b88 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a  turn 0; .}../*.*
18b89 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63 74  * Size of struct
18b8a 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 69   Mem not includi
18b8b 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c  ng the Mem.zMall
18b8c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64  oc member..*/.#d
18b8d 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 5a  efine MEMCELLSIZ
18b8e 45 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28 4d  E (size_t)(&(((M
18b8f 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63  em *)0)->zMalloc
18b90 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61  ))../*.** Make a
18b91 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f  n shallow copy o
18b92 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f  f pFrom into pTo
18b93 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74  .  Prior content
18b94 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20  s of.** pTo are 
18b95 66 72 65 65 64 2e 20 20 54 68 65 20 70 46 72 6f  freed.  The pFro
18b96 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e 6f  m->z field is no
18b97 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20 49  t duplicated.  I
18b98 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73  f.** pFrom->z is
18b99 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d   used, then pTo-
18b9a 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  >z points to the
18b9b 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 70   same thing as p
18b9c 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66  From->z.** and f
18b9d 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79 70  lags gets srcTyp
18b9e 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 70  e (either MEM_Ep
18b9f 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69  hem or MEM_Stati
18ba0 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  c)..*/.SQLITE_PR
18ba1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
18ba2 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
18ba3 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63  Copy(Mem *pTo, c
18ba4 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c  onst Mem *pFrom,
18ba5 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a 20   int srcType){. 
18ba6 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d   assert( (pFrom-
18ba7 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
18ba8 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  Set)==0 );.  sql
18ba9 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
18baa 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b  seExternal(pTo);
18bab 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70  .  memcpy(pTo, p
18bac 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a  From, MEMCELLSIZ
18bad 45 29 3b 0a 20 20 70 54 6f 2d 3e 78 44 65 6c 20  E);.  pTo->xDel 
18bae 3d 20 30 3b 0a 20 20 69 66 28 20 28 70 46 72 6f  = 0;.  if( (pFro
18baf 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e  m->flags&MEM_Dyn
18bb0 29 21 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 7a  )!=0 || pFrom->z
18bb1 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63  ==pFrom->zMalloc
18bb2 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61   ){.    pTo->fla
18bb3 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 44 79 6e 7c  gs &= ~(MEM_Dyn|
18bb4 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45  MEM_Static|MEM_E
18bb5 70 68 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72  phem);.    asser
18bb6 74 28 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f  t( srcType==MEM_
18bb7 45 70 68 65 6d 20 7c 7c 20 73 72 63 54 79 70 65  Ephem || srcType
18bb8 3d 3d 4d 45 4d 5f 53 74 61 74 69 63 20 29 3b 0a  ==MEM_Static );.
18bb9 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c      pTo->flags |
18bba 3d 20 73 72 63 54 79 70 65 3b 0a 20 20 7d 0a 7d  = srcType;.  }.}
18bbb 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 66  ../*.** Make a f
18bbc 75 6c 6c 20 63 6f 70 79 20 6f 66 20 70 46 72 6f  ull copy of pFro
18bbd 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69  m into pTo.  Pri
18bbe 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70  or contents of p
18bbf 54 6f 20 61 72 65 0a 2a 2a 20 66 72 65 65 64 20  To are.** freed 
18bc0 62 65 66 6f 72 65 20 74 68 65 20 63 6f 70 79 20  before the copy 
18bc1 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49  is made..*/.SQLI
18bc2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18bc3 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70  qlite3VdbeMemCop
18bc4 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73  y(Mem *pTo, cons
18bc5 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20  t Mem *pFrom){. 
18bc6 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
18bc7 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  _OK;..  assert( 
18bc8 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20  (pFrom->flags & 
18bc9 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
18bca 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
18bcb 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
18bcc 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79  l(pTo);.  memcpy
18bcd 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d  (pTo, pFrom, MEM
18bce 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f  CELLSIZE);.  pTo
18bcf 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f  ->flags &= ~MEM_
18bd0 44 79 6e 3b 0a 0a 20 20 69 66 28 20 70 54 6f 2d  Dyn;..  if( pTo-
18bd1 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c  >flags&(MEM_Str|
18bd2 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20  MEM_Blob) ){.   
18bd3 20 69 66 28 20 30 3d 3d 28 70 46 72 6f 6d 2d 3e   if( 0==(pFrom->
18bd4 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63  flags&MEM_Static
18bd5 29 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 2d 3e  ) ){.      pTo->
18bd6 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68  flags |= MEM_Eph
18bd7 65 6d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  em;.      rc = s
18bd8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b  qlite3VdbeMemMak
18bd9 65 57 72 69 74 65 61 62 6c 65 28 70 54 6f 29 3b  eWriteable(pTo);
18bda 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
18bdb 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
18bdc 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 63  * Transfer the c
18bdd 6f 6e 74 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d  ontents of pFrom
18bde 20 74 6f 20 70 54 6f 2e 20 41 6e 79 20 65 78 69   to pTo. Any exi
18bdf 73 74 69 6e 67 20 76 61 6c 75 65 20 69 6e 20 70  sting value in p
18be0 54 6f 20 69 73 0a 2a 2a 20 66 72 65 65 64 2e 20  To is.** freed. 
18be1 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e  If pFrom contain
18be2 73 20 65 70 68 65 6d 65 72 61 6c 20 64 61 74 61  s ephemeral data
18be3 2c 20 61 20 63 6f 70 79 20 69 73 20 6d 61 64 65  , a copy is made
18be4 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f 6d 20 63 6f  ..**.** pFrom co
18be5 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55  ntains an SQL NU
18be6 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  LL when this rou
18be7 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f  tine returns..*/
18be8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18be9 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
18bea 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20 2a 70 54 6f  MemMove(Mem *pTo
18beb 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20  , Mem *pFrom){. 
18bec 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e   assert( pFrom->
18bed 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
18bee 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 46 72 6f  _mutex_held(pFro
18bef 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  m->db->mutex) );
18bf0 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e  .  assert( pTo->
18bf1 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
18bf2 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 54 6f 2d  _mutex_held(pTo-
18bf3 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
18bf4 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e   assert( pFrom->
18bf5 64 62 3d 3d 30 20 7c 7c 20 70 54 6f 2d 3e 64 62  db==0 || pTo->db
18bf6 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 64 62  ==0 || pFrom->db
18bf7 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b 0a 0a 20 20  ==pTo->db );..  
18bf8 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
18bf9 6c 65 61 73 65 28 70 54 6f 29 3b 0a 20 20 6d 65  lease(pTo);.  me
18bfa 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c  mcpy(pTo, pFrom,
18bfb 20 73 69 7a 65 6f 66 28 4d 65 6d 29 29 3b 0a 20   sizeof(Mem));. 
18bfc 20 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 3d 20   pFrom->flags = 
18bfd 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 70 46 72 6f  MEM_Null;.  pFro
18bfe 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 70  m->xDel = 0;.  p
18bff 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  From->zMalloc = 
18c00 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  0;.}../*.** Chan
18c01 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
18c02 61 20 4d 65 6d 20 74 6f 20 62 65 20 61 20 73 74  a Mem to be a st
18c03 72 69 6e 67 20 6f 72 20 61 20 42 4c 4f 42 2e 0a  ring or a BLOB..
18c04 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79  **.** The memory
18c05 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 61   management stra
18c06 74 65 67 79 20 64 65 70 65 6e 64 73 20 6f 6e 20  tegy depends on 
18c07 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
18c08 20 78 44 65 6c 0a 2a 2a 20 70 61 72 61 6d 65 74   xDel.** paramet
18c09 65 72 2e 20 49 66 20 74 68 65 20 76 61 6c 75 65  er. If the value
18c0a 20 70 61 73 73 65 64 20 69 73 20 53 51 4c 49 54   passed is SQLIT
18c0b 45 5f 54 52 41 4e 53 49 45 4e 54 2c 20 74 68 65  E_TRANSIENT, the
18c0c 6e 20 74 68 65 20 0a 2a 2a 20 73 74 72 69 6e 67  n the .** string
18c0d 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20   is copied into 
18c0e 61 20 28 70 6f 73 73 69 62 6c 79 20 65 78 69 73  a (possibly exis
18c0f 74 69 6e 67 29 20 62 75 66 66 65 72 20 6d 61 6e  ting) buffer man
18c10 61 67 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20  aged by the .** 
18c11 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 4f  Mem structure. O
18c12 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 65 78  therwise, any ex
18c13 69 73 74 69 6e 67 20 62 75 66 66 65 72 20 69 73  isting buffer is
18c14 20 66 72 65 65 64 20 61 6e 64 20 74 68 65 0a 2a   freed and the.*
18c15 2a 20 70 6f 69 6e 74 65 72 20 63 6f 70 69 65 64  * pointer copied
18c16 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18c17 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
18c18 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 20  dbeMemSetStr(.  
18c19 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 20  Mem *pMem,      
18c1a 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
18c1b 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 72  ll to set to str
18c1c 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63  ing value */.  c
18c1d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20  onst char *z,   
18c1e 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69     /* String poi
18c1f 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c  nter */.  int n,
18c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18c21 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67   Bytes in string
18c22 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f  , or negative */
18c23 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
18c24 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69         /* Encodi
18c25 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20  ng of z.  0 for 
18c26 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20  BLOBs */.  void 
18c27 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f  (*xDel)(void*) /
18c28 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e  * Destructor fun
18c29 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ction */.){.  in
18c2a 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20  t nByte = n;    
18c2b 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66    /* New value f
18c2c 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 20  or pMem->n */.  
18c2d 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20  int iLimit;     
18c2e 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
18c2f 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72  llowed string or
18c30 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 20   blob size */.  
18c31 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 20  u16 flags = 0;  
18c32 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65      /* New value
18c33 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   for pMem->flags
18c34 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
18c35 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
18c36 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
18c37 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
18c38 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
18c39 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
18c3a 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a  M_RowSet)==0 );.
18c3b 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20  .  /* If z is a 
18c3c 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65  NULL pointer, se
18c3d 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69  t pMem to contai
18c3e 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a  n an SQL NULL. *
18c3f 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20  /.  if( !z ){.  
18c40 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
18c41 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20  SetNull(pMem);. 
18c42 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
18c43 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  _OK;.  }..  if( 
18c44 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20  pMem->db ){.    
18c45 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64  iLimit = pMem->d
18c46 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
18c47 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a  _LIMIT_LENGTH];.
18c48 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69    }else{.    iLi
18c49 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58  mit = SQLITE_MAX
18c4a 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66  _LENGTH;.  }.  f
18c4b 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d  lags = (enc==0?M
18c4c 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29  EM_Blob:MEM_Str)
18c4d 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20  ;.  if( nByte<0 
18c4e 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65  ){.    assert( e
18c4f 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  nc!=0 );.    if(
18c50 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46   enc==SQLITE_UTF
18c51 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e  8 ){.      for(n
18c52 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69  Byte=0; nByte<=i
18c53 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65  Limit && z[nByte
18c54 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20  ]; nByte++){}.  
18c55 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
18c56 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74  or(nByte=0; nByt
18c57 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b  e<=iLimit && (z[
18c58 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65  nByte] | z[nByte
18c59 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b  +1]); nByte+=2){
18c5a 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67  }.    }.    flag
18c5b 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20  s |= MEM_Term;. 
18c5c 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c   }..  /* The fol
18c5d 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74  lowing block set
18c5e 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73  s the new values
18c5f 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65   of Mem.z and Me
18c60 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20  m.xDel. It.  ** 
18c61 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67  also sets a flag
18c62 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   in local variab
18c63 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e  le "flags" to in
18c64 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72  dicate the memor
18c65 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e  y.  ** managemen
18c66 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79  t (one of MEM_Dy
18c67 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29  n or MEM_Static)
18c68 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65  ..  */.  if( xDe
18c69 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  l==SQLITE_TRANSI
18c6a 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ENT ){.    int n
18c6b 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20  Alloc = nByte;. 
18c6c 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d     if( flags&MEM
18c6d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e  _Term ){.      n
18c6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53  Alloc += (enc==S
18c6f 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b  QLITE_UTF8?1:2);
18c70 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
18c71 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20  Byte>iLimit ){. 
18c72 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
18c73 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d  TE_TOOBIG;.    }
18c74 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
18c75 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
18c76 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a  , nAlloc, 0) ){.
18c77 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
18c78 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
18c79 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d  .    memcpy(pMem
18c7a 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b  ->z, z, nAlloc);
18c7b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65  .  }else if( xDe
18c7c 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  l==SQLITE_DYNAMI
18c7d 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  C ){.    sqlite3
18c7e 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
18c7f 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  Mem);.    pMem->
18c80 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e  zMalloc = pMem->
18c81 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20  z = (char *)z;. 
18c82 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20     pMem->xDel = 
18c83 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
18c84 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
18c85 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20  lease(pMem);.   
18c86 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72   pMem->z = (char
18c87 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e   *)z;.    pMem->
18c88 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20  xDel = xDel;.   
18c89 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c   flags |= ((xDel
18c8a 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  ==SQLITE_STATIC)
18c8b 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f  ?MEM_Static:MEM_
18c8c 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Dyn);.  }.  if( 
18c8d 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a  nByte>iLimit ){.
18c8e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
18c8f 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20  E_TOOBIG;.  }.. 
18c90 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65   pMem->n = nByte
18c91 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
18c92 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d  = flags;.  pMem-
18c93 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f  >enc = (enc==0 ?
18c94 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65   SQLITE_UTF8 : e
18c95 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70  nc);.  pMem->typ
18c96 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51  e = (enc==0 ? SQ
18c97 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49  LITE_BLOB : SQLI
18c98 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64  TE_TEXT);..#ifnd
18c99 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
18c9a 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d  TF16.  if( pMem-
18c9b 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46  >enc!=SQLITE_UTF
18c9c 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65  8 && sqlite3Vdbe
18c9d 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65  MemHandleBom(pMe
18c9e 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  m) ){.    return
18c9f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
18ca0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74   }.#endif..  ret
18ca1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
18ca2 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ../*.** Compare 
18ca3 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74 61  the values conta
18ca4 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f 20  ined by the two 
18ca5 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72 65  memory cells, re
18ca6 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 74  turning.** negat
18ca7 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73  ive, zero or pos
18ca8 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20 69  itive if pMem1 i
18ca9 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75  s less than, equ
18caa 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65  al to, or greate
18cab 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e  r.** than pMem2.
18cac 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20 69   Sorting order i
18cad 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c 20  s NULL's first, 
18cae 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62  followed by numb
18caf 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a 2a  ers (integers.**
18cb0 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72 74   and reals) sort
18cb1 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20  ed numerically, 
18cb2 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78 74  followed by text
18cb3 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65 20   ordered by the 
18cb4 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71  collating.** seq
18cb5 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20  uence pColl and 
18cb6 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f  finally blob's o
18cb7 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d 70  rdered by memcmp
18cb8 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e 55  ()..**.** Two NU
18cb9 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f  LL values are co
18cba 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20 62  nsidered equal b
18cbb 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
18cbc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18cbd 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  TE int sqlite3Me
18cbe 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d  mCompare(const M
18cbf 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 74  em *pMem1, const
18cc0 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f 6e   Mem *pMem2, con
18cc1 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  st CollSeq *pCol
18cc2 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  l){.  int rc;.  
18cc3 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69 6e  int f1, f2;.  in
18cc4 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73  t combined_flags
18cc5 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68 61  ;..  /* Intercha
18cc6 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70 4d  nge pMem1 and pM
18cc7 65 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c 61  em2 if the colla
18cc8 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73 70  ting sequence sp
18cc9 65 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45 53  ecifies.  ** DES
18cca 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20  C order..  */.  
18ccb 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67  f1 = pMem1->flag
18ccc 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d  s;.  f2 = pMem2-
18ccd 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69 6e  >flags;.  combin
18cce 65 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66 32  ed_flags = f1|f2
18ccf 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f 6d  ;.  assert( (com
18cd0 62 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d 45  bined_flags & ME
18cd1 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a  M_RowSet)==0 );.
18cd2 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61   .  /* If one va
18cd3 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20  lue is NULL, it 
18cd4 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65  is less than the
18cd5 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 20   other. If both 
18cd6 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 20  values.  ** are 
18cd7 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a  NULL, return 0..
18cd8 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69    */.  if( combi
18cd9 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  ned_flags&MEM_Nu
18cda 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ll ){.    return
18cdb 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 2d   (f2&MEM_Null) -
18cdc 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a   (f1&MEM_Null);.
18cdd 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65    }..  /* If one
18cde 20 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d 62   value is a numb
18cdf 65 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72  er and the other
18ce0 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 6d   is not, the num
18ce1 62 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a  ber is less..  *
18ce2 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e 75  * If both are nu
18ce3 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 61  mbers, compare a
18ce4 73 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 69  s reals if one i
18ce5 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 20  s a real, or as 
18ce6 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 66  integers.  ** if
18ce7 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65   both values are
18ce8 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a   integers..  */.
18ce9 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66    if( combined_f
18cea 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45  lags&(MEM_Int|ME
18ceb 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 69  M_Real) ){.    i
18cec 66 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74  f( !(f1&(MEM_Int
18ced 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20  |MEM_Real)) ){. 
18cee 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
18cef 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 66     }.    if( !(f
18cf0 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52  2&(MEM_Int|MEM_R
18cf1 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72  eal)) ){.      r
18cf2 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a  eturn -1;.    }.
18cf3 20 20 20 20 69 66 28 20 28 66 31 20 26 20 66 32      if( (f1 & f2
18cf4 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29   & MEM_Int)==0 )
18cf5 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72  {.      double r
18cf6 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 28  1, r2;.      if(
18cf7 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d   (f1&MEM_Real)==
18cf8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20  0 ){.        r1 
18cf9 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 31 2d  = (double)pMem1-
18cfa 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73  >u.i;.      }els
18cfb 65 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20  e{.        r1 = 
18cfc 70 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20 20  pMem1->r;.      
18cfd 7d 0a 20 20 20 20 20 20 69 66 28 20 28 66 32 26  }.      if( (f2&
18cfe 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a  MEM_Real)==0 ){.
18cff 20 20 20 20 20 20 20 20 72 32 20 3d 20 28 64 6f          r2 = (do
18d00 75 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 2e 69 3b  uble)pMem2->u.i;
18d01 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
18d02 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32        r2 = pMem2
18d03 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ->r;.      }.   
18d04 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72     if( r1<r2 ) r
18d05 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20  eturn -1;.      
18d06 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 75  if( r1>r2 ) retu
18d07 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75  rn 1;.      retu
18d08 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rn 0;.    }else{
18d09 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66  .      assert( f
18d0a 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20  1&MEM_Int );.   
18d0b 20 20 20 61 73 73 65 72 74 28 20 66 32 26 4d 45     assert( f2&ME
18d0c 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 69  M_Int );.      i
18d0d 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c 20  f( pMem1->u.i < 
18d0e 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74  pMem2->u.i ) ret
18d0f 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 66  urn -1;.      if
18d10 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 70  ( pMem1->u.i > p
18d11 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75  Mem2->u.i ) retu
18d12 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75  rn 1;.      retu
18d13 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 0;.    }.  }.
18d14 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c  .  /* If one val
18d15 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 61  ue is a string a
18d16 6e 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20  nd the other is 
18d17 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 69  a blob, the stri
18d18 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a  ng is less..  **
18d19 20 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 72   If both are str
18d1a 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 73  ings, compare us
18d1b 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  ing the collatin
18d1c 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  g functions..  *
18d1d 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64  /.  if( combined
18d1e 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29  _flags&MEM_Str )
18d1f 7b 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20  {.    if( (f1 & 
18d20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20  MEM_Str)==0 ){. 
18d21 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
18d22 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 32     }.    if( (f2
18d23 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29   & MEM_Str)==0 )
18d24 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d  {.      return -
18d25 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73  1;.    }..    as
18d26 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63  sert( pMem1->enc
18d27 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b 0a  ==pMem2->enc );.
18d28 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
18d29 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  1->enc==SQLITE_U
18d2a 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  TF8 || .        
18d2b 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d      pMem1->enc==
18d2c 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c  SQLITE_UTF16LE |
18d2d 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51  | pMem1->enc==SQ
18d2e 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a  LITE_UTF16BE );.
18d2f 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c  .    /* The coll
18d30 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6d  ation sequence m
18d31 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61  ust be defined a
18d32 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 76  t this point, ev
18d33 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 65  en if.    ** the
18d34 20 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 68   user deletes th
18d35 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
18d36 65 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 76  ence after the v
18d37 64 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a 20  dbe program is. 
18d38 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28     ** compiled (
18d39 74 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c 77  this was not alw
18d3a 61 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a 20  ays the case).. 
18d3b 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
18d3c 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c  ( !pColl || pCol
18d3d 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 20  l->xCmp );..    
18d3e 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
18d3f 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 6e     if( pMem1->en
18d40 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b  c==pColl->enc ){
18d41 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
18d42 73 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 65  strings are alre
18d43 61 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 65  ady in the corre
18d44 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 61  ct encoding.  Ca
18d45 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ll the.        *
18d46 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  * comparison fun
18d47 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 2a  ction directly *
18d48 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
18d49 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f   pColl->xCmp(pCo
18d4a 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 2d  ll->pUser,pMem1-
18d4b 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 6d  >n,pMem1->z,pMem
18d4c 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b 0a  2->n,pMem2->z);.
18d4d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
18d4e 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20       const void 
18d4f 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 20  *v1, *v2;.      
18d50 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 20    int n1, n2;.  
18d51 20 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 20        Mem c1;.  
18d52 20 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 20        Mem c2;.  
18d53 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 31        memset(&c1
18d54 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 29  , 0, sizeof(c1))
18d55 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  ;.        memset
18d56 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (&c2, 0, sizeof(
18d57 63 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71  c2));.        sq
18d58 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
18d59 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d 65  lowCopy(&c1, pMe
18d5a 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  m1, MEM_Ephem);.
18d5b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
18d5c 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
18d5d 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45  y(&c2, pMem2, ME
18d5e 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20  M_Ephem);.      
18d5f 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61    v1 = sqlite3Va
18d60 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33  lueText((sqlite3
18d61 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f  _value*)&c1, pCo
18d62 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20  ll->enc);.      
18d63 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30    n1 = v1==0 ? 0
18d64 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 20   : c1.n;.       
18d65 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c   v2 = sqlite3Val
18d66 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f  ueText((sqlite3_
18d67 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f 6c  value*)&c2, pCol
18d68 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20  l->enc);.       
18d69 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20   n2 = v2==0 ? 0 
18d6a 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 20  : c2.n;.        
18d6b 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  rc = pColl->xCmp
18d6c 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e  (pColl->pUser, n
18d6d 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a  1, v1, n2, v2);.
18d6e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
18d6f 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63  dbeMemRelease(&c
18d70 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
18d71 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
18d72 65 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 20  e(&c2);.        
18d73 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
18d74 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20   }.    }.    /* 
18d75 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  If a NULL pointe
18d76 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20  r was passed as 
18d77 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63  the collate func
18d78 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75  tion, fall throu
18d79 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  gh.    ** to the
18d7a 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75   blob case and u
18d7b 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f  se memcmp().  */
18d7c 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68  .  }. .  /* Both
18d7d 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20   values must be 
18d7e 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20  blobs.  Compare 
18d7f 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20  using memcmp(). 
18d80 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d   */.  rc = memcm
18d81 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d  p(pMem1->z, pMem
18d82 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e  2->z, (pMem1->n>
18d83 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d  pMem2->n)?pMem2-
18d84 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20  >n:pMem1->n);.  
18d85 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
18d86 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d   rc = pMem1->n -
18d87 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20   pMem2->n;.  }. 
18d88 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
18d89 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f  *.** Move data o
18d8a 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65  ut of a btree ke
18d8b 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20  y or data field 
18d8c 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73  and into a Mem s
18d8d 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65  tructure..** The
18d8e 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20   data or key is 
18d8f 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65  taken from the e
18d90 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20 69  ntry that pCur i
18d91 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
18d92 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66  ting.** to.  off
18d93 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65  set and amt dete
18d94 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69  rmine what porti
18d95 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f  on of the data o
18d96 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76  r key to retriev
18d97 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75  e..** key is tru
18d98 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79  e to get the key
18d99 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74   or false to get
18d9a 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 75   data.  The resu
18d9b 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  lt is written.**
18d9c 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65   into the pMem e
18d9d 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  lement..**.** Th
18d9e 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65  e pMem structure
18d9f 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62   is assumed to b
18da0 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e  e uninitialized.
18da1 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74    Any prior cont
18da2 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72  ent.** is overwr
18da3 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65  itten without be
18da4 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a  ing freed..**.**
18da5 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
18da6 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72   fails for any r
18da7 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65  eason (malloc re
18da8 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e  turns NULL or un
18da9 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20  able.** to read 
18daa 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74  from the disk) t
18dab 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20  hen the pMem is 
18dac 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e  left in an incon
18dad 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a  sistent state..*
18dae 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18daf 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
18db0 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20  MemFromBtree(.  
18db1 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
18db2 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e    /* Cursor poin
18db3 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74  ting at record t
18db4 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20  o retrieve. */. 
18db5 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20   int offset,    
18db6 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f     /* Offset fro
18db7 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 64  m the start of d
18db8 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79  ata to return by
18db9 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69  tes from. */.  i
18dba 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
18dbb 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
18dbc 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a  tes to return. *
18dbd 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20  /.  int key,    
18dbe 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
18dbf 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20  , retrieve from 
18dc0 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e  the btree key, n
18dc1 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65  ot data. */.  Me
18dc2 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20  m *pMem         
18dc3 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64  /* OUT: Return d
18dc4 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20  ata in this Mem 
18dc5 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b  structure. */.){
18dc6 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20  .  char *zData; 
18dc7 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72        /* Data fr
18dc8 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 79  om the btree lay
18dc9 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69  er */.  int avai
18dca 6c 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75  lable = 0; /* Nu
18dcb 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76  mber of bytes av
18dcc 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c  ailable on the l
18dcd 6f 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 20  ocal btree page 
18dce 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
18dcf 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62  ;       /* Datab
18dd0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
18dd1 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
18dd2 49 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20  ITE_OK;..  db = 
18dd3 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
18dd4 6f 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73  orDb(pCur);.  as
18dd5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
18dd6 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
18dd7 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
18dd8 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
18dd9 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
18dda 3b 0a 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20  ;.  if( key ){. 
18ddb 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72     zData = (char
18ddc 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b   *)sqlite3BtreeK
18ddd 65 79 46 65 74 63 68 28 70 43 75 72 2c 20 26 61  eyFetch(pCur, &a
18dde 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 65 6c  vailable);.  }el
18ddf 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20  se{.    zData = 
18de0 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42  (char *)sqlite3B
18de1 74 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43  treeDataFetch(pC
18de2 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b  ur, &available);
18de3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a  .  }.  assert( z
18de4 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20 20 69 66  Data!=0 );..  if
18de5 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 61 76  ( offset+amt<=av
18de6 61 69 6c 61 62 6c 65 20 26 26 20 28 28 70 4d 65  ailable && ((pMe
18de7 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e  m->flags&MEM_Dyn
18de8 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d 2d 3e 78 44  )==0 || pMem->xD
18de9 65 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  el) ){.    sqlit
18dea 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
18deb 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d  (pMem);.    pMem
18dec 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b 6f 66 66  ->z = &zData[off
18ded 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  set];.    pMem->
18dee 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62  flags = MEM_Blob
18def 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65  |MEM_Ephem;.  }e
18df0 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  lse if( SQLITE_O
18df1 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
18df2 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
18df3 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20 29 7b 0a  , amt+2, 0)) ){.
18df4 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
18df5 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 44  = MEM_Blob|MEM_D
18df6 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  yn|MEM_Term;.   
18df7 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b 0a   pMem->enc = 0;.
18df8 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
18df9 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20   SQLITE_BLOB;.  
18dfa 20 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20 20    if( key ){.   
18dfb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
18dfc 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 6f 66  treeKey(pCur, of
18dfd 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d  fset, amt, pMem-
18dfe 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  >z);.    }else{.
18dff 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
18e00 65 33 42 74 72 65 65 44 61 74 61 28 70 43 75 72  e3BtreeData(pCur
18e01 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70  , offset, amt, p
18e02 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a 20  Mem->z);.    }. 
18e03 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 5d 20     pMem->z[amt] 
18e04 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  = 0;.    pMem->z
18e05 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a 20 20 20  [amt+1] = 0;.   
18e06 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18e07 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
18e08 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
18e09 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20  e(pMem);.    }. 
18e0a 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 61   }.  pMem->n = a
18e0b 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63  mt;..  return rc
18e0c 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e  ;.}../* This fun
18e0d 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 76  ction is only av
18e0e 61 69 6c 61 62 6c 65 20 69 6e 74 65 72 6e 61 6c  ailable internal
18e0f 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 61  ly, it is not pa
18e10 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 74  rt of the.** ext
18e11 65 72 6e 61 6c 20 41 50 49 2e 20 49 74 20 77 6f  ernal API. It wo
18e12 72 6b 73 20 69 6e 20 61 20 73 69 6d 69 6c 61 72  rks in a similar
18e13 20 77 61 79 20 74 6f 20 73 71 6c 69 74 65 33 5f   way to sqlite3_
18e14 76 61 6c 75 65 5f 74 65 78 74 28 29 2c 0a 2a 2a  value_text(),.**
18e15 20 65 78 63 65 70 74 20 74 68 65 20 64 61 74 61   except the data
18e16 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e 20   returned is in 
18e17 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 73 70 65  the encoding spe
18e18 63 69 66 69 65 64 20 62 79 20 74 68 65 20 73 65  cified by the se
18e19 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65  cond.** paramete
18e1a 72 2c 20 77 68 69 63 68 20 6d 75 73 74 20 62 65  r, which must be
18e1b 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55   one of SQLITE_U
18e1c 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55  TF16BE, SQLITE_U
18e1d 54 46 31 36 4c 45 20 6f 72 0a 2a 2a 20 53 51 4c  TF16LE or.** SQL
18e1e 49 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a 2a 2a 20  ITE_UTF8..**.** 
18e1f 28 32 30 30 36 2d 30 32 2d 31 36 3a 29 20 20 54  (2006-02-16:)  T
18e20 68 65 20 65 6e 63 20 76 61 6c 75 65 20 63 61 6e  he enc value can
18e21 20 62 65 20 6f 72 2d 65 64 20 77 69 74 68 20 53   be or-ed with S
18e22 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
18e23 4e 45 44 2e 0a 2a 2a 20 49 66 20 74 68 61 74 20  NED..** If that 
18e24 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65  is the case, the
18e25 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6d 75 73  n the result mus
18e26 74 20 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20  t be aligned on 
18e27 61 6e 20 65 76 65 6e 20 62 79 74 65 0a 2a 2a 20  an even byte.** 
18e28 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 53 51 4c  boundary..*/.SQL
18e29 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
18e2a 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 56  t void *sqlite3V
18e2b 61 6c 75 65 54 65 78 74 28 73 71 6c 69 74 65 33  alueText(sqlite3
18e2c 5f 76 61 6c 75 65 2a 20 70 56 61 6c 2c 20 75 38  _value* pVal, u8
18e2d 20 65 6e 63 29 7b 0a 20 20 69 66 28 20 21 70 56   enc){.  if( !pV
18e2e 61 6c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a  al ) return 0;..
18e2f 20 20 61 73 73 65 72 74 28 20 70 56 61 6c 2d 3e    assert( pVal->
18e30 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
18e31 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 56 61 6c  _mutex_held(pVal
18e32 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
18e33 20 20 61 73 73 65 72 74 28 20 28 65 6e 63 26 33    assert( (enc&3
18e34 29 3d 3d 28 65 6e 63 26 7e 53 51 4c 49 54 45 5f  )==(enc&~SQLITE_
18e35 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29  UTF16_ALIGNED) )
18e36 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 56 61  ;.  assert( (pVa
18e37 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  l->flags & MEM_R
18e38 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20  owSet)==0 );..  
18e39 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26  if( pVal->flags&
18e3a 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
18e3b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
18e3c 61 73 73 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f  assert( (MEM_Blo
18e3d 62 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72  b>>3) == MEM_Str
18e3e 20 29 3b 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67   );.  pVal->flag
18e3f 73 20 7c 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67  s |= (pVal->flag
18e40 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33  s & MEM_Blob)>>3
18e41 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70  ;.  expandBlob(p
18e42 56 61 6c 29 3b 0a 20 20 69 66 28 20 70 56 61 6c  Val);.  if( pVal
18e43 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20  ->flags&MEM_Str 
18e44 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
18e45 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
18e46 28 70 56 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51  (pVal, enc & ~SQ
18e47 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
18e48 45 44 29 3b 0a 20 20 20 20 69 66 28 20 28 65 6e  ED);.    if( (en
18e49 63 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c & SQLITE_UTF16
18e4a 5f 41 4c 49 47 4e 45 44 29 21 3d 30 20 26 26 20  _ALIGNED)!=0 && 
18e4b 31 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52  1==(1&SQLITE_PTR
18e4c 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29  _TO_INT(pVal->z)
18e4d 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ) ){.      asser
18e4e 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20  t( (pVal->flags 
18e4f 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d  & (MEM_Ephem|MEM
18e50 5f 53 74 61 74 69 63 29 29 21 3d 30 20 29 3b 0a  _Static))!=0 );.
18e51 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
18e52 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
18e53 65 61 62 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c  eable(pVal)!=SQL
18e54 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18e55 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
18e56 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
18e57 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54  lite3VdbeMemNulT
18e58 65 72 6d 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a  erminate(pVal);.
18e59 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
18e5a 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67  ert( (pVal->flag
18e5b 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29  s&MEM_Blob)==0 )
18e5c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
18e5d 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 56  eMemStringify(pV
18e5e 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 61 73  al, enc);.    as
18e5f 73 65 72 74 28 20 30 3d 3d 28 31 26 53 51 4c 49  sert( 0==(1&SQLI
18e60 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56  TE_PTR_TO_INT(pV
18e61 61 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20 7d 0a 20  al->z)) );.  }. 
18e62 20 61 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e   assert(pVal->en
18e63 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54  c==(enc & ~SQLIT
18e64 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
18e65 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a   || pVal->db==0.
18e66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
18e67 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f   pVal->db->mallo
18e68 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28  cFailed );.  if(
18e69 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63   pVal->enc==(enc
18e6a 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36   & ~SQLITE_UTF16
18e6b 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20  _ALIGNED) ){.   
18e6c 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b   return pVal->z;
18e6d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
18e6e 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
18e6f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
18e70 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  w sqlite3_value 
18e71 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54  object..*/.SQLIT
18e72 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
18e73 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33  3_value *sqlite3
18e74 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33  ValueNew(sqlite3
18e75 20 2a 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20   *db){.  Mem *p 
18e76 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
18e77 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
18e78 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29  (*p));.  if( p )
18e79 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d  {.    p->flags =
18e7a 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70   MEM_Null;.    p
18e7b 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
18e7c 4e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20  NULL;.    p->db 
18e7d 3d 20 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75  = db;.  }.  retu
18e7e 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rn p;.}../*.** C
18e7f 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69  reate a new sqli
18e80 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
18e81 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  , containing the
18e82 20 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e   value of pExpr.
18e83 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79  .**.** This only
18e84 20 77 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20   works for very 
18e85 73 69 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f  simple expressio
18e86 6e 73 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20  ns that consist 
18e87 6f 66 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a  of one constant.
18e88 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22  ** token (i.e. "
18e89 35 22 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73  5", "5.1", "'a s
18e8a 74 72 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65  tring'"). If the
18e8b 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a   expression can.
18e8c 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20  ** be converted 
18e8d 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20  directly into a 
18e8e 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20  value, then the 
18e8f 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74  value is allocat
18e90 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e  ed and.** a poin
18e91 74 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a  ter written to *
18e92 70 70 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65  ppVal. The calle
18e93 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  r is responsible
18e94 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e   for deallocatin
18e95 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62  g.** the value b
18e96 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20  y passing it to 
18e97 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
18e98 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20  () later on. If 
18e99 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  the expression.*
18e9a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76  * cannot be conv
18e9b 65 72 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65  erted to a value
18e9c 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73  , then *ppVal is
18e9d 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f   set to NULL..*/
18e9e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18e9f 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65  int sqlite3Value
18ea0 46 72 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69  FromExpr(.  sqli
18ea1 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
18ea2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
18ea3 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
18ea4 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
18ea5 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
18ea6 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
18ea7 6e 20 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f  n to evaluate */
18ea8 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
18ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18eaa 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20  Encoding to use 
18eab 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79  */.  u8 affinity
18eac 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
18ead 2a 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73  * Affinity to us
18eae 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  e */.  sqlite3_v
18eaf 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20  alue **ppVal    
18eb0 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65   /* Write the ne
18eb1 77 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a  w value here */.
18eb2 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63  ){.  int op;.  c
18eb3 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20  har *zVal = 0;. 
18eb4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
18eb5 70 56 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28  pVal = 0;..  if(
18eb6 20 21 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a   !pExpr ){.    *
18eb7 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72  ppVal = 0;.    r
18eb8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
18eb9 0a 20 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70  .  }.  op = pExp
18eba 72 2d 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70  r->op;..  if( op
18ebb 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f  ==TK_STRING || o
18ebc 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f  p==TK_FLOAT || o
18ebd 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b  p==TK_INTEGER ){
18ebe 0a 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69  .    zVal = sqli
18ebf 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
18ec0 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74   (char*)pExpr->t
18ec1 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74  oken.z, pExpr->t
18ec2 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61  oken.n);.    pVa
18ec3 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  l = sqlite3Value
18ec4 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28  New(db);.    if(
18ec5 20 21 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20   !zVal || !pVal 
18ec6 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
18ec7 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53     sqlite3ValueS
18ec8 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20  etStr(pVal, -1, 
18ec9 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  zVal, SQLITE_UTF
18eca 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  8, SQLITE_DYNAMI
18ecb 43 29 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 3d  C);.    if( (op=
18ecc 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f  =TK_INTEGER || o
18ecd 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26  p==TK_FLOAT ) &&
18ece 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54   affinity==SQLIT
18ecf 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20  E_AFF_NONE ){.  
18ed0 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
18ed1 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56  ApplyAffinity(pV
18ed2 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  al, SQLITE_AFF_N
18ed3 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55  UMERIC, SQLITE_U
18ed4 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  TF8);.    }else{
18ed5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
18ed6 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79  lueApplyAffinity
18ed7 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c  (pVal, affinity,
18ed8 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20   SQLITE_UTF8);. 
18ed9 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63     }.    if( enc
18eda 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b  !=SQLITE_UTF8 ){
18edb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
18edc 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
18edd 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20  (pVal, enc);.   
18ede 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f   }.  }else if( o
18edf 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b  p==TK_UMINUS ) {
18ee0 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
18ee1 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65  OK==sqlite3Value
18ee2 46 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 70  FromExpr(db,pExp
18ee3 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 66  r->pLeft,enc,aff
18ee4 69 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 7b 0a  inity,&pVal) ){.
18ee5 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20        pVal->u.i 
18ee6 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e 69  = -1 * pVal->u.i
18ee7 3b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62  ;.      /* (doub
18ee8 6c 65 29 2d 31 20 49 6e 20 63 61 73 65 20 6f 66  le)-1 In case of
18ee9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
18eea 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a  ATING_POINT... *
18eeb 2f 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 20  /.      pVal->r 
18eec 3d 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 70  = (double)-1 * p
18eed 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20  Val->r;.    }.  
18eee 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
18eef 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52  _OMIT_BLOB_LITER
18ef0 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70  AL.  else if( op
18ef1 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20  ==TK_BLOB ){.   
18ef2 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61   int nVal;.    a
18ef3 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f  ssert( pExpr->to
18ef4 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20  ken.n>=3 );.    
18ef5 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74  assert( pExpr->t
18ef6 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20 7c  oken.z[0]=='x' |
18ef7 7c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  | pExpr->token.z
18ef8 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20  [0]=='X' );.    
18ef9 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74  assert( pExpr->t
18efa 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20  oken.z[1]=='\'' 
18efb 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
18efc 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45  Expr->token.z[pE
18efd 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d  xpr->token.n-1]=
18efe 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 61  ='\'' );.    pVa
18eff 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  l = sqlite3Value
18f00 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28  New(db);.    if(
18f01 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f   !pVal ) goto no
18f02 5f 6d 65 6d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d  _mem;.    nVal =
18f03 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20   pExpr->token.n 
18f04 2d 20 33 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20  - 3;.    zVal = 
18f05 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f  (char*)pExpr->to
18f06 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 73  ken.z + 2;.    s
18f07 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
18f08 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 65  Str(pVal, sqlite
18f09 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a  3HexToBlob(db, z
18f0a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c  Val, nVal), nVal
18f0b 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  /2,.            
18f0c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
18f0d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b  SQLITE_DYNAMIC);
18f0e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2a  .  }.#endif..  *
18f0f 70 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20  ppVal = pVal;.  
18f10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
18f11 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d  ;..no_mem:.  db-
18f12 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
18f13 31 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  1;.  sqlite3DbFr
18f14 65 65 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 20  ee(db, zVal);.  
18f15 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
18f16 28 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c  (pVal);.  *ppVal
18f17 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53   = 0;.  return S
18f18 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a  QLITE_NOMEM;.}..
18f19 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
18f1a 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f 66   string value of
18f1b 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   an sqlite3_valu
18f1c 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49  e object.*/.SQLI
18f1d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
18f1e 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
18f1f 74 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  tr(.  sqlite3_va
18f20 6c 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56  lue *v,     /* V
18f21 61 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20 2a  alue to be set *
18f22 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20  /.  int n,      
18f23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
18f24 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a 20  gth of string z 
18f25 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
18f26 2a 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65  *z,        /* Te
18f27 78 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73 74  xt of the new st
18f28 72 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63  ring */.  u8 enc
18f29 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
18f2a 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75  /* Encoding to u
18f2b 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  se */.  void (*x
18f2c 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a  Del)(void*)   /*
18f2d 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   Destructor for 
18f2e 74 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b  the string */.){
18f2f 0a 20 20 69 66 28 20 76 20 29 20 73 71 6c 69 74  .  if( v ) sqlit
18f30 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
18f31 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20  (Mem *)v, z, n, 
18f32 65 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f  enc, xDel);.}../
18f33 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71 6c  *.** Free an sql
18f34 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
18f35 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  t.*/.SQLITE_PRIV
18f36 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
18f37 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65  ValueFree(sqlite
18f38 33 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69  3_value *v){.  i
18f39 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a  f( !v ) return;.
18f3a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
18f3b 52 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29 76  Release((Mem *)v
18f3c 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
18f3d 65 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 62  ee(((Mem*)v)->db
18f3e 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  , v);.}../*.** R
18f3f 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
18f40 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
18f41 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
18f42 62 6a 65 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a  bject assuming.*
18f43 2a 20 74 68 61 74 20 69 74 20 75 73 65 73 20 74  * that it uses t
18f44 68 65 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63  he encoding "enc
18f45 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ".*/.SQLITE_PRIV
18f46 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
18f47 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74 65  alueBytes(sqlite
18f48 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75  3_value *pVal, u
18f49 38 20 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70  8 enc){.  Mem *p
18f4a 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20   = (Mem*)pVal;. 
18f4b 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26   if( (p->flags &
18f4c 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c   MEM_Blob)!=0 ||
18f4d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78   sqlite3ValueTex
18f4e 74 28 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a  t(pVal, enc) ){.
18f4f 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73      if( p->flags
18f50 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20   & MEM_Zero ){. 
18f51 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e       return p->n
18f52 20 2b 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20   + p->u.nZero;. 
18f53 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
18f54 72 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20  return p->n;.   
18f55 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
18f56 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  0;.}../*********
18f57 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
18f58 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  emem.c *********
18f59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f5b 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
18f5c 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
18f5d 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a   vdbeaux.c *****
18f5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f60 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
18f61 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a   September 6.**.
18f62 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
18f63 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
18f64 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
18f65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
18f66 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
18f67 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
18f68 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
18f69 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
18f6a 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
18f6b 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
18f6c 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
18f6d 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
18f6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
18f6f 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
18f70 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
18f71 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
18f72 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
18f73 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
18f74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
18f78 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
18f79 73 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20  s code used for 
18f7a 63 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f  creating, destro
18f7b 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61  ying, and popula
18f7c 74 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28  ting.** a VDBE (
18f7d 6f 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73  or an "sqlite3_s
18f7e 74 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e  tmt" as it is kn
18f7f 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69  own to the outsi
18f80 64 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f  de world.)  Prio
18f81 72 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20  r.** to version 
18f82 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20  2.8.7, all this 
18f83 63 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65  code was combine
18f84 64 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e  d into the vdbe.
18f85 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a  c source file..*
18f86 2a 20 42 75 74 20 74 68 61 74 20 66 69 6c 65 20  * But that file 
18f87 77 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20  was getting too 
18f88 62 69 67 20 73 6f 20 74 68 69 73 20 73 75 62 72  big so this subr
18f89 6f 75 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c  outines were spl
18f8a 69 74 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49  it out..**.** $I
18f8b 64 3a 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31  d: vdbeaux.c,v 1
18f8c 2e 34 35 37 20 32 30 30 39 2f 30 35 2f 30 36 20  .457 2009/05/06 
18f8d 31 38 3a 35 37 3a 31 30 20 73 68 61 6e 65 20 45  18:57:10 shane E
18f8e 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a  xp $.*/..../*.**
18f8f 20 57 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20   When debugging 
18f90 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  the code generat
18f91 6f 72 20 69 6e 20 61 20 73 79 6d 62 6f 6c 69 63  or in a symbolic
18f92 20 64 65 62 75 67 67 65 72 2c 20 6f 6e 65 20 63   debugger, one c
18f93 61 6e 0a 2a 2a 20 73 65 74 20 74 68 65 20 73 71  an.** set the sq
18f94 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72  lite3VdbeAddopTr
18f95 61 63 65 20 74 6f 20 31 20 61 6e 64 20 61 6c 6c  ace to 1 and all
18f96 20 6f 70 63 6f 64 65 73 20 77 69 6c 6c 20 62 65   opcodes will be
18f97 20 70 72 69 6e 74 65 64 0a 2a 2a 20 61 73 20 74   printed.** as t
18f98 68 65 79 20 61 72 65 20 61 64 64 65 64 20 74 6f  hey are added to
18f99 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e   the instruction
18f9a 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a 23 69 66 64   stream..*/.#ifd
18f9b 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
18f9c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18f9d 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
18f9e 64 6f 70 54 72 61 63 65 20 3d 20 30 3b 0a 23 65  dopTrace = 0;.#e
18f9f 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  ndif.../*.** Cre
18fa0 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61  ate a new virtua
18fa1 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
18fa2 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
18fa3 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74  VATE Vdbe *sqlit
18fa4 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71 6c  e3VdbeCreate(sql
18fa5 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62  ite3 *db){.  Vdb
18fa6 65 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  e *p;.  p = sqli
18fa7 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
18fa8 64 62 2c 20 73 69 7a 65 6f 66 28 56 64 62 65 29  db, sizeof(Vdbe)
18fa9 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   );.  if( p==0 )
18faa 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 2d 3e   return 0;.  p->
18fab 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20 64  db = db;.  if( d
18fac 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20  b->pVdbe ){.    
18fad 64 62 2d 3e 70 56 64 62 65 2d 3e 70 50 72 65 76  db->pVdbe->pPrev
18fae 20 3d 20 70 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70   = p;.  }.  p->p
18faf 4e 65 78 74 20 3d 20 64 62 2d 3e 70 56 64 62 65  Next = db->pVdbe
18fb0 3b 0a 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 30  ;.  p->pPrev = 0
18fb1 3b 0a 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20  ;.  db->pVdbe = 
18fb2 70 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20  p;.  p->magic = 
18fb3 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 3b  VDBE_MAGIC_INIT;
18fb4 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
18fb5 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74  /*.** Remember t
18fb6 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 66 6f  he SQL string fo
18fb7 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61  r a prepared sta
18fb8 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  tement..*/.SQLIT
18fb9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
18fba 71 6c 69 74 65 33 56 64 62 65 53 65 74 53 71 6c  qlite3VdbeSetSql
18fbb 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20  (Vdbe *p, const 
18fbc 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20  char *z, int n, 
18fbd 69 6e 74 20 69 73 50 72 65 70 61 72 65 56 32 29  int isPrepareV2)
18fbe 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
18fbf 65 74 75 72 6e 3b 0a 23 69 66 64 65 66 20 53 51  eturn;.#ifdef SQ
18fc0 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a  LITE_OMIT_TRACE.
18fc1 20 20 69 66 28 20 21 69 73 50 72 65 70 61 72 65    if( !isPrepare
18fc2 56 32 20 29 20 72 65 74 75 72 6e 3b 0a 23 65 6e  V2 ) return;.#en
18fc3 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 2d  dif.  assert( p-
18fc4 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20 70 2d  >zSql==0 );.  p-
18fc5 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 44  >zSql = sqlite3D
18fc6 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20  bStrNDup(p->db, 
18fc7 7a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73 50 72  z, n);.  p->isPr
18fc8 65 70 61 72 65 56 32 20 3d 20 69 73 50 72 65 70  epareV2 = isPrep
18fc9 61 72 65 56 32 20 3f 20 31 20 3a 20 30 3b 0a 7d  areV2 ? 1 : 0;.}
18fca 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
18fcb 68 65 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65  he SQL associate
18fcc 64 20 77 69 74 68 20 61 20 70 72 65 70 61 72 65  d with a prepare
18fcd 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53  d statement.*/.S
18fce 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
18fcf 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71  char *sqlite3_sq
18fd0 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  l(sqlite3_stmt *
18fd1 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a  pStmt){.  Vdbe *
18fd2 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  p = (Vdbe *)pStm
18fd3 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e  t;.  return (p->
18fd4 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 70 2d  isPrepareV2 ? p-
18fd5 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a 2f  >zSql : 0);.}../
18fd6 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f  *.** Swap all co
18fd7 6e 74 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77  ntent between tw
18fd8 6f 20 56 44 42 45 20 73 74 72 75 63 74 75 72 65  o VDBE structure
18fd9 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
18fda 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
18fdb 33 56 64 62 65 53 77 61 70 28 56 64 62 65 20 2a  3VdbeSwap(Vdbe *
18fdc 70 41 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a 20  pA, Vdbe *pB){. 
18fdd 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70   Vdbe tmp, *pTmp
18fde 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a  ;.  char *zTmp;.
18fdf 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a    tmp = *pA;.  *
18fe0 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20  pA = *pB;.  *pB 
18fe1 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20  = tmp;.  pTmp = 
18fe2 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d  pA->pNext;.  pA-
18fe3 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65  >pNext = pB->pNe
18fe4 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20  xt;.  pB->pNext 
18fe5 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d  = pTmp;.  pTmp =
18fe6 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41   pA->pPrev;.  pA
18fe7 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50  ->pPrev = pB->pP
18fe8 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76  rev;.  pB->pPrev
18fe9 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20   = pTmp;.  zTmp 
18fea 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41  = pA->zSql;.  pA
18feb 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71  ->zSql = pB->zSq
18fec 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20  l;.  pB->zSql = 
18fed 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 72  zTmp;.  pB->isPr
18fee 65 70 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73  epareV2 = pA->is
18fef 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 69  PrepareV2;.}..#i
18ff0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
18ff1 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61  G./*.** Turn tra
18ff2 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a  cing on or off.*
18ff3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18ff4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
18ff5 65 54 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20  eTrace(Vdbe *p, 
18ff6 46 49 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20  FILE *trace){.  
18ff7 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65  p->trace = trace
18ff8 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
18ff9 2a 20 52 65 73 69 7a 65 20 74 68 65 20 56 64 62  * Resize the Vdb
18ffa 65 2e 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74  e.aOp array so t
18ffb 68 61 74 20 69 74 20 69 73 20 61 74 20 6c 65 61  hat it is at lea
18ffc 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72  st one op larger
18ffd 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73   than .** it was
18ffe 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75  ..**.** If an ou
18fff 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f  t-of-memory erro
19000 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72  r occurs while r
19001 65 73 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61  esizing the arra
19002 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  y, return.** SQL
19003 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68  ITE_NOMEM. In th
19004 69 73 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70  is case Vdbe.aOp
19005 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c   and Vdbe.nOpAll
19006 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e  oc remain .** un
19007 63 68 61 6e 67 65 64 20 28 74 68 69 73 20 69 73  changed (this is
19008 20 73 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63   so that any opc
19009 6f 64 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c  odes already all
1900a 6f 63 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a  ocated can be .*
1900b 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c  * correctly deal
1900c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69  located along wi
1900d 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  th the rest of t
1900e 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61  he Vdbe)..*/.sta
1900f 74 69 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72  tic int growOpAr
19010 72 61 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ray(Vdbe *p){.  
19011 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20  VdbeOp *pNew;.  
19012 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e  int nNew = (p->n
19013 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70  OpAlloc ? p->nOp
19014 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28  Alloc*2 : (int)(
19015 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29  1024/sizeof(Op))
19016 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  );.  pNew = sqli
19017 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e  te3DbRealloc(p->
19018 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77  db, p->aOp, nNew
19019 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20  *sizeof(Op));.  
1901a 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20  if( pNew ){.    
1901b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 71  p->nOpAlloc = sq
1901c 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
1901d 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f 73  e(p->db, pNew)/s
1901e 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 70  izeof(Op);.    p
1901f 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 20  ->aOp = pNew;.  
19020 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 4e 65 77  }.  return (pNew
19021 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
19022 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a  QLITE_NOMEM);.}.
19023 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
19024 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20   instruction to 
19025 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 74  the list of inst
19026 72 75 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74  ructions current
19027 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e   in the.** VDBE.
19028 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 64    Return the add
19029 72 65 73 73 20 6f 66 20 74 68 65 20 6e 65 77 20  ress of the new 
1902a 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a  instruction..**.
1902b 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 3a 0a 2a  ** Parameters:.*
1902c 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 20 20 20  *.**    p       
1902d 20 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20          Pointer 
1902e 74 6f 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a 2a  to the VDBE.**.*
1902f 2a 20 20 20 20 6f 70 20 20 20 20 20 20 20 20 20  *    op         
19030 20 20 20 20 20 54 68 65 20 6f 70 63 6f 64 65 20       The opcode 
19031 66 6f 72 20 74 68 69 73 20 69 6e 73 74 72 75 63  for this instruc
19032 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 31  tion.**.**    p1
19033 2c 20 70 32 2c 20 70 33 20 20 20 20 20 20 4f 70  , p2, p3      Op
19034 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 65  erands.**.** Use
19035 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65   the sqlite3Vdbe
19036 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 66  ResolveLabel() f
19037 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 61  unction to fix a
19038 6e 20 61 64 64 72 65 73 73 20 61 6e 64 0a 2a 2a  n address and.**
19039 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65   the sqlite3Vdbe
1903a 43 68 61 6e 67 65 50 34 28 29 20 66 75 6e 63 74  ChangeP4() funct
1903b 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ion to change th
1903c 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
1903d 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a 2f  4.** operand..*/
1903e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1903f 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
19040 64 64 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69  ddOp3(Vdbe *p, i
19041 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69  nt op, int p1, i
19042 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0a  nt p2, int p3){.
19043 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f    int i;.  VdbeO
19044 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70  p *pOp;..  i = p
19045 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28  ->nOp;.  assert(
19046 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
19047 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
19048 61 73 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20  assert( op>0 && 
19049 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28  op<0xff );.  if(
1904a 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20   p->nOpAlloc<=i 
1904b 29 7b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 4f  ){.    if( growO
1904c 70 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20  pArray(p) ){.   
1904d 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
1904e 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b   }.  }.  p->nOp+
1904f 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61  +;.  pOp = &p->a
19050 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70  Op[i];.  pOp->op
19051 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a 20  code = (u8)op;. 
19052 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20   pOp->p5 = 0;.  
19053 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20  pOp->p1 = p1;.  
19054 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20  pOp->p2 = p2;.  
19055 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20  pOp->p3 = p3;.  
19056 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20  pOp->p4.p = 0;. 
19057 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
19058 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d 3e  4_NOTUSED;.  p->
19059 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 66  expired = 0;.#if
1905a 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1905b 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74  .  pOp->zComment
1905c 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69   = 0;.  if( sqli
1905d 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63  te3VdbeAddopTrac
1905e 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 50  e ) sqlite3VdbeP
1905f 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d  rintOp(0, i, &p-
19060 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66  >aOp[i]);.#endif
19061 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
19062 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c  FILE.  pOp->cycl
19063 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63  es = 0;.  pOp->c
19064 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20  nt = 0;.#endif. 
19065 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c   return i;.}.SQL
19066 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19067 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19068 30 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f  0(Vdbe *p, int o
19069 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  p){.  return sql
1906a 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70  ite3VdbeAddOp3(p
1906b 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  , op, 0, 0, 0);.
1906c 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
1906d 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1906e 41 64 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20  AddOp1(Vdbe *p, 
1906f 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b  int op, int p1){
19070 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
19071 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f  3VdbeAddOp3(p, o
19072 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a  p, p1, 0, 0);.}.
19073 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19074 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
19075 64 4f 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e  dOp2(Vdbe *p, in
19076 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e  t op, int p1, in
19077 74 20 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20  t p2){.  return 
19078 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19079 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c  3(p, op, p1, p2,
1907a 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41   0);.}.../*.** A
1907b 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61  dd an opcode tha
1907c 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70  t includes the p
1907d 34 20 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69  4 value as a poi
1907e 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nter..*/.SQLITE_
1907f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19080 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20  te3VdbeAddOp4(. 
19081 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20   Vdbe *p,       
19082 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20       /* Add the 
19083 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56  opcode to this V
19084 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  M */.  int op,  
19085 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
19086 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a  e new opcode */.
19087 20 20 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20    int p1,       
19088 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20        /* The P1 
19089 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74  operand */.  int
1908a 20 70 32 2c 20 20 20 20 20 20 20 20 20 20 20 20   p2,            
1908b 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61   /* The P2 opera
1908c 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20  nd */.  int p3, 
1908d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1908e 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f  he P3 operand */
1908f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
19090 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34  P4,    /* The P4
19091 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
19092 74 20 70 34 74 79 70 65 20 20 20 20 20 20 20 20  t p4type        
19093 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20    /* P4 operand 
19094 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  type */.){.  int
19095 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
19096 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c  dbeAddOp3(p, op,
19097 20 70 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20   p1, p2, p3);.  
19098 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
19099 65 50 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34  eP4(p, addr, zP4
1909a 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74  , p4type);.  ret
1909b 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a  urn addr;.}../*.
1909c 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
1909d 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66  symbolic label f
1909e 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f  or an instructio
1909f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74  n that has yet t
190a0 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20  o be.** coded.  
190a1 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62  The symbolic lab
190a2 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73  el is really jus
190a3 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  t a negative num
190a4 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62  ber.  The.** lab
190a5 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 61  el can be used a
190a6 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f  s the P2 value o
190a7 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20  f an operation. 
190a8 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20   Later, when.** 
190a9 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73  the label is res
190aa 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 69  olved to a speci
190ab 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 65  fic address, the
190ac 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a   VDBE will scan.
190ad 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f  ** through its o
190ae 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e  peration list an
190af 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c  d change all val
190b0 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 20  ues of P2 which 
190b1 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62  match.** the lab
190b2 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f  el into the reso
190b3 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a  lved address..**
190b4 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f  .** The VDBE kno
190b5 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61 6c  ws that a P2 val
190b6 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65  ue is a label be
190b7 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65  cause labels are
190b8 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74  .** always negat
190b9 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65  ive and P2 value
190ba 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f  s are suppose to
190bb 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   be non-negative
190bc 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65  ..** Hence, a ne
190bd 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 20  gative P2 value 
190be 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20  is a label that 
190bf 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 65  has yet to be re
190c0 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65  solved..**.** Ze
190c1 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ro is returned i
190c2 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
190c3 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ls..*/.SQLITE_PR
190c4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
190c5 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56  3VdbeMakeLabel(V
190c6 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  dbe *p){.  int i
190c7 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65  ;.  i = p->nLabe
190c8 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70  l++;.  assert( p
190c9 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
190ca 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66  GIC_INIT );.  if
190cb 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c  ( i>=p->nLabelAl
190cc 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  loc ){.    int n
190cd 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f   = p->nLabelAllo
190ce 63 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d 3e  c*2 + 5;.    p->
190cf 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33  aLabel = sqlite3
190d0 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
190d1 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c  p->db, p->aLabel
190d2 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
190d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
190d4 20 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 6f           n*sizeo
190d5 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 29  f(p->aLabel[0]))
190d6 3b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c 41  ;.    p->nLabelA
190d7 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
190d8 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62  MallocSize(p->db
190d9 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 7a  , p->aLabel)/siz
190da 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d  eof(p->aLabel[0]
190db 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
190dc 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d  aLabel ){.    p-
190dd 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b  >aLabel[i] = -1;
190de 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31  .  }.  return -1
190df 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73  -i;.}../*.** Res
190e0 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 74  olve label "x" t
190e1 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73 73  o be the address
190e2 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73   of the next ins
190e3 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62  truction to.** b
190e4 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 65  e inserted.  The
190e5 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 6d   parameter "x" m
190e6 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62  ust have been ob
190e7 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 61  tained from.** a
190e8 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73   prior call to s
190e9 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
190ea 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  bel()..*/.SQLITE
190eb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
190ec 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
190ed 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 69  Label(Vdbe *p, i
190ee 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 3d  nt x){.  int j =
190ef 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 28   -1-x;.  assert(
190f0 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
190f1 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20  MAGIC_INIT );.  
190f2 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20  assert( j>=0 && 
190f3 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20  j<p->nLabel );. 
190f4 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29   if( p->aLabel )
190f5 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b  {.    p->aLabel[
190f6 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d  j] = p->nOp;.  }
190f7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74  .}../*.** Loop t
190f8 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72  hrough the progr
190f9 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50  am looking for P
190fa 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72  2 values that ar
190fb 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e  e negative.** on
190fc 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f   jump instructio
190fd 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20 76  ns.  Each such v
190fe 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 2e  alue is a label.
190ff 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a    Resolve the.**
19100 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e   label by settin
19101 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 74  g the P2 value t
19102 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e 6f  o its correct no
19103 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a  n-zero value..**
19104 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
19105 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   is called once 
19106 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65  after all opcode
19107 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65  s have been inse
19108 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69  rted..**.** Vari
19109 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72  able *pMaxFuncAr
1910a 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  gs is set to the
1910b 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f   maximum value o
1910c 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e  f any P2 argumen
1910d 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46  t .** to an OP_F
1910e 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53  unction, OP_AggS
1910f 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65  tep or OP_VFilte
19110 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 69  r opcode. This i
19111 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 71  s used by .** sq
19112 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
19113 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68 65  dy() to size the
19114 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72   Vdbe.apArg[] ar
19115 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ray..**.** This 
19116 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65  routine also doe
19117 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  s the following 
19118 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 3a 20 20 49  optimization:  I
19119 74 20 73 63 61 6e 73 20 66 6f 72 0a 2a 2a 20 69  t scans for.** i
1911a 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 61 74  nstructions that
1911b 20 6d 69 67 68 74 20 63 61 75 73 65 20 61 20 73   might cause a s
1911c 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 62 61 63  tatement rollbac
1911d 6b 2e 20 20 53 75 63 68 20 69 6e 73 74 72 75 63  k.  Such instruc
1911e 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 3a 0a 2a 2a  tions.** are:.**
1911f 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74  .**   *  OP_Halt
19120 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f   with P1=SQLITE_
19121 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50  CONSTRAINT and P
19122 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20  2=OE_Abort..**  
19123 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a   *  OP_Destroy.*
19124 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74  *   *  OP_VUpdat
19125 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65  e.**   *  OP_VRe
19126 6e 61 6d 65 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  name.**.** If no
19127 20 73 75 63 68 20 69 6e 73 74 72 75 63 74 69 6f   such instructio
19128 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 6e  n is found, then
19129 20 65 76 65 72 79 20 53 74 61 74 65 6d 65 6e 74   every Statement
1912a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a 2a 2a   instruction .**
1912b 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 61   is changed to a
1912c 20 4e 6f 6f 70 2e 20 20 49 6e 20 74 68 69 73 20   Noop.  In this 
1912d 77 61 79 2c 20 77 65 20 61 76 6f 69 64 20 63 72  way, we avoid cr
1912e 65 61 74 69 6e 67 20 74 68 65 20 73 74 61 74 65  eating the state
1912f 6d 65 6e 74 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  ment .** journal
19130 20 66 69 6c 65 20 75 6e 6e 65 63 65 73 73 61 72   file unnecessar
19131 69 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ily..*/.static v
19132 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c  oid resolveP2Val
19133 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ues(Vdbe *p, int
19134 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 7b   *pMaxFuncArgs){
19135 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
19136 6e 4d 61 78 41 72 67 73 20 3d 20 30 3b 0a 20 20  nMaxArgs = 0;.  
19137 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20 2a  Op *pOp;.  int *
19138 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61 62  aLabel = p->aLab
19139 65 6c 3b 0a 20 20 69 6e 74 20 64 6f 65 73 53 74  el;.  int doesSt
1913a 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20  atementRollback 
1913b 3d 20 30 3b 0a 20 20 69 6e 74 20 68 61 73 53 74  = 0;.  int hasSt
1913c 61 74 65 6d 65 6e 74 42 65 67 69 6e 20 3d 20 30  atementBegin = 0
1913d 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20  ;.  p->readOnly 
1913e 3d 20 31 3b 0a 20 20 70 2d 3e 75 73 65 73 53 74  = 1;.  p->usesSt
1913f 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20  mtJournal = 0;. 
19140 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c   for(pOp=p->aOp,
19141 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d   i=p->nOp-1; i>=
19142 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a  0; i--, pOp++){.
19143 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d 20      u8 opcode = 
19144 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 20  pOp->opcode;..  
19145 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50    if( opcode==OP
19146 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 63  _Function || opc
19147 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70 20  ode==OP_AggStep 
19148 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  ){.      if( pOp
19149 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29 20  ->p5>nMaxArgs ) 
1914a 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e  nMaxArgs = pOp->
1914b 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  p5;.#ifndef SQLI
1914c 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1914d 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69  ABLE.    }else i
1914e 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55  f( opcode==OP_VU
1914f 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 69  pdate ){.      i
19150 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 41  f( pOp->p2>nMaxA
19151 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d  rgs ) nMaxArgs =
19152 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 66   pOp->p2;.#endif
19153 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f  .    }.    if( o
19154 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 29  pcode==OP_Halt )
19155 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  {.      if( pOp-
19156 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53  >p1==SQLITE_CONS
19157 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e 70  TRAINT && pOp->p
19158 32 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20  2==OE_Abort ){. 
19159 20 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65         doesState
1915a 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31  mentRollback = 1
1915b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
1915c 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d  lse if( opcode==
1915d 4f 50 5f 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a  OP_Statement ){.
1915e 20 20 20 20 20 20 68 61 73 53 74 61 74 65 6d 65        hasStateme
1915f 6e 74 42 65 67 69 6e 20 3d 20 31 3b 0a 20 20 20  ntBegin = 1;.   
19160 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f     p->usesStmtJo
19161 75 72 6e 61 6c 20 3d 20 31 3b 0a 20 20 20 20 7d  urnal = 1;.    }
19162 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d  else if( opcode=
19163 3d 4f 50 5f 44 65 73 74 72 6f 79 20 29 7b 0a 20  =OP_Destroy ){. 
19164 20 20 20 20 20 64 6f 65 73 53 74 61 74 65 6d 65       doesStateme
19165 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31 3b 0a  ntRollback = 1;.
19166 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70      }else if( op
19167 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63  code==OP_Transac
19168 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21  tion && pOp->p2!
19169 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72  =0 ){.      p->r
1916a 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66  eadOnly = 0;.#if
1916b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1916c 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1916d 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f    }else if( opco
1916e 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 7c  de==OP_VUpdate |
1916f 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 65  | opcode==OP_VRe
19170 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 64 6f  name ){.      do
19171 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62  esStatementRollb
19172 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  ack = 1;.    }el
19173 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f  se if( opcode==O
19174 50 5f 56 46 69 6c 74 65 72 20 29 7b 0a 20 20 20  P_VFilter ){.   
19175 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20     int n;.      
19176 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 20 2d  assert( p->nOp -
19177 20 69 20 3e 3d 20 33 20 29 3b 0a 20 20 20 20 20   i >= 3 );.     
19178 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d   assert( pOp[-1]
19179 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 74 65  .opcode==OP_Inte
1917a 67 65 72 20 29 3b 0a 20 20 20 20 20 20 6e 20 3d  ger );.      n =
1917b 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b 0a 20 20 20   pOp[-1].p1;.   
1917c 20 20 20 69 66 28 20 6e 3e 6e 4d 61 78 41 72 67     if( n>nMaxArg
1917d 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 6e  s ) nMaxArgs = n
1917e 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a  ;.#endif.    }..
1917f 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
19180 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70  dbeOpcodeHasProp
19181 65 72 74 79 28 6f 70 63 6f 64 65 2c 20 4f 50 46  erty(opcode, OPF
19182 4c 47 5f 4a 55 4d 50 29 20 26 26 20 70 4f 70 2d  LG_JUMP) && pOp-
19183 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 61  >p2<0 ){.      a
19184 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d 3e 70  ssert( -1-pOp->p
19185 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20  2<p->nLabel );. 
19186 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 61       pOp->p2 = a
19187 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e 70 32  Label[-1-pOp->p2
19188 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  ];.    }.  }.  s
19189 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e  qlite3DbFree(p->
1918a 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a  db, p->aLabel);.
1918b 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 30 3b    p->aLabel = 0;
1918c 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41 72 67  ..  *pMaxFuncArg
1918d 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a 0a 20  s = nMaxArgs;.. 
1918e 20 2f 2a 20 49 66 20 77 65 20 6e 65 76 65 72 20   /* If we never 
1918f 72 6f 6c 6c 62 61 63 6b 20 61 20 73 74 61 74 65  rollback a state
19190 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
19191 2c 20 74 68 65 6e 20 73 74 61 74 65 6d 65 6e 74  , then statement
19192 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
19193 6e 73 20 61 72 65 20 6e 6f 74 20 6e 65 65 64 65  ns are not neede
19194 64 2e 20 20 53 6f 20 63 68 61 6e 67 65 20 65 76  d.  So change ev
19195 65 72 79 20 4f 50 5f 53 74 61 74 65 6d 65 6e 74  ery OP_Statement
19196 0a 20 20 2a 2a 20 6f 70 63 6f 64 65 20 69 6e 74  .  ** opcode int
19197 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 20 54  o an OP_Noop.  T
19198 68 69 73 20 61 76 6f 69 64 20 61 20 63 61 6c 6c  his avoid a call
19199 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4f 70 65   to sqlite3OsOpe
1919a 6e 45 78 63 6c 75 73 69 76 65 28 29 0a 20 20 2a  nExclusive().  *
1919b 2a 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 65  * which can be e
1919c 78 70 65 6e 73 69 76 65 20 6f 6e 20 73 6f 6d 65  xpensive on some
1919d 20 70 6c 61 74 66 6f 72 6d 73 2e 0a 20 20 2a 2f   platforms..  */
1919e 0a 20 20 69 66 28 20 68 61 73 53 74 61 74 65 6d  .  if( hasStatem
1919f 65 6e 74 42 65 67 69 6e 20 26 26 20 21 64 6f 65  entBegin && !doe
191a0 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62 61  sStatementRollba
191a1 63 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 75 73 65  ck ){.    p->use
191a2 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 30  sStmtJournal = 0
191a3 3b 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 70 2d  ;.    for(pOp=p-
191a4 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31  >aOp, i=p->nOp-1
191a5 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70  ; i>=0; i--, pOp
191a6 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
191a7 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53  Op->opcode==OP_S
191a8 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20  tatement ){.    
191a9 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20      pOp->opcode 
191aa 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20  = OP_Noop;.     
191ab 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
191ac 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
191ad 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   address of the 
191ae 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
191af 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e   to be inserted.
191b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
191b1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
191b2 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 64  beCurrentAddr(Vd
191b3 62 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  be *p){.  assert
191b4 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
191b5 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
191b6 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f 70 3b 0a   return p->nOp;.
191b7 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77  }../*.** Add a w
191b8 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 65  hole list of ope
191b9 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f  rations to the o
191ba 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20  peration stack. 
191bb 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61   Return the.** a
191bc 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66 69  ddress of the fi
191bd 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 64  rst operation ad
191be 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ded..*/.SQLITE_P
191bf 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
191c0 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
191c1 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70  Vdbe *p, int nOp
191c2 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e  , VdbeOpList con
191c3 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20  st *aOp){.  int 
191c4 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28 20  addr;.  assert( 
191c5 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
191c6 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
191c7 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20  f( p->nOp + nOp 
191c8 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26  > p->nOpAlloc &&
191c9 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 20   growOpArray(p) 
191ca 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
191cb 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d  .  }.  addr = p-
191cc 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3e  >nOp;.  if( nOp>
191cd 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
191ce 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 63      VdbeOpList c
191cf 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 3b  onst *pIn = aOp;
191d0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
191d1 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b 29  nOp; i++, pIn++)
191d2 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 3d  {.      int p2 =
191d3 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 20   pIn->p2;.      
191d4 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 26  VdbeOp *pOut = &
191d5 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b 0a  p->aOp[i+addr];.
191d6 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 6f        pOut->opco
191d7 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 65  de = pIn->opcode
191d8 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 31  ;.      pOut->p1
191d9 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 20   = pIn->p1;.    
191da 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 73 71    if( p2<0 && sq
191db 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48  lite3VdbeOpcodeH
191dc 61 73 50 72 6f 70 65 72 74 79 28 70 4f 75 74 2d  asProperty(pOut-
191dd 3e 6f 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a  >opcode, OPFLG_J
191de 55 4d 50 29 20 29 7b 0a 20 20 20 20 20 20 20 20  UMP) ){.        
191df 70 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 20  pOut->p2 = addr 
191e0 2b 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20 20  + ADDR(p2);.    
191e1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
191e2 20 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b 0a   pOut->p2 = p2;.
191e3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f        }.      pO
191e4 75 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70 33  ut->p3 = pIn->p3
191e5 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 34  ;.      pOut->p4
191e6 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45  type = P4_NOTUSE
191e7 44 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  D;.      pOut->p
191e8 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  4.p = 0;.      p
191e9 4f 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69 66  Out->p5 = 0;.#if
191ea 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
191eb 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43 6f  .      pOut->zCo
191ec 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  mment = 0;.     
191ed 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
191ee 41 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20 20  AddopTrace ){.  
191ef 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
191f0 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 64  ePrintOp(0, i+ad
191f1 64 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64  dr, &p->aOp[i+ad
191f2 64 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  dr]);.      }.#e
191f3 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70  ndif.    }.    p
191f4 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20 20  ->nOp += nOp;.  
191f5 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b  }.  return addr;
191f6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
191f7 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
191f8 65 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f 72  e P1 operand for
191f9 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74   a specific inst
191fa 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73  ruction..** This
191fb 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66   routine is usef
191fc 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20  ul when a large 
191fd 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65  program is loade
191fe 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74  d from a.** stat
191ff 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73  ic array using s
19200 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
19201 69 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20  ist but we want 
19202 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77  to make a.** few
19203 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74   minor changes t
19204 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a  o the program..*
19205 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19206 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
19207 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 20 2a  eChangeP1(Vdbe *
19208 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74  p, int addr, int
19209 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28   val){.  assert(
1920a 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69   p==0 || p->magi
1920b 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
1920c 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26  IT );.  if( p &&
1920d 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e   addr>=0 && p->n
1920e 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 4f  Op>addr && p->aO
1920f 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b  p ){.    p->aOp[
19210 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a  addr].p1 = val;.
19211 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
19212 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
19213 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20   the P2 operand 
19214 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69  for a specific i
19215 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54  nstruction..** T
19216 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
19217 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 69 6e  seful for settin
19218 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 6e 61  g a jump destina
19219 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
1921a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1921b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
1921c 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
1921d 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20  dr, int val){.  
1921e 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20  assert( p==0 || 
1921f 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
19220 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
19221 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d 30 20  f( p && addr>=0 
19222 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26  && p->nOp>addr &
19223 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20  & p->aOp ){.    
19224 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20  p->aOp[addr].p2 
19225 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  = val;.  }.}../*
19226 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76  .** Change the v
19227 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20 6f  alue of the P3 o
19228 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65  perand for a spe
19229 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f  cific instructio
1922a 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1922b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1922c 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56 64  3VdbeChangeP3(Vd
1922d 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c  be *p, int addr,
1922e 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73   int val){.  ass
1922f 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e  ert( p==0 || p->
19230 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
19231 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20  C_INIT );.  if( 
19232 70 20 26 26 20 61 64 64 72 3e 3d 30 20 26 26 20  p && addr>=0 && 
19233 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 26 26 20 70  p->nOp>addr && p
19234 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 70 2d 3e  ->aOp ){.    p->
19235 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 76  aOp[addr].p3 = v
19236 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  al;.  }.}../*.**
19237 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
19238 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72  e of the P5 oper
19239 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74  and for the most
1923a 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 64   recently.** add
1923b 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f  ed operation..*/
1923c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1923d 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1923e 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a 70  ChangeP5(Vdbe *p
1923f 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 73  , u8 val){.  ass
19240 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e  ert( p==0 || p->
19241 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
19242 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20  C_INIT );.  if( 
19243 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20  p && p->aOp ){. 
19244 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f     assert( p->nO
19245 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f  p>0 );.    p->aO
19246 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d  p[p->nOp-1].p5 =
19247 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   val;.  }.}../*.
19248 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 32  ** Change the P2
19249 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 74   operand of inst
1924a 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f 20  ruction addr so 
1924b 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
1924c 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73  o.** the address
1924d 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73   of the next ins
1924e 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 63  truction to be c
1924f 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oded..*/.SQLITE_
19250 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19251 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
19252 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
19253 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64  dr){.  sqlite3Vd
19254 62 65 43 68 61 6e 67 65 50 32 28 70 2c 20 61 64  beChangeP2(p, ad
19255 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a  dr, p->nOp);.}..
19256 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  ./*.** If the in
19257 70 75 74 20 46 75 6e 63 44 65 66 20 73 74 72 75  put FuncDef stru
19258 63 74 75 72 65 20 69 73 20 65 70 68 65 6d 65 72  cture is ephemer
19259 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 20 69 74  al, then free it
1925a 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 46 75 6e  .  If.** the Fun
1925b 63 44 65 66 20 69 73 20 6e 6f 74 20 65 70 68 65  cDef is not ephe
1925c 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  rmal, then do no
1925d 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  thing..*/.static
1925e 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d 65   void freeEpheme
1925f 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  ralFunction(sqli
19260 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 66  te3 *db, FuncDef
19261 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 28 20 70   *pDef){.  if( p
19262 44 65 66 20 26 26 20 28 70 44 65 66 2d 3e 66 6c  Def && (pDef->fl
19263 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e  ags & SQLITE_FUN
19264 43 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a 20  C_EPHEM)!=0 ){. 
19265 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
19266 28 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d 0a  (db, pDef);.  }.
19267 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
19268 61 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e 65  a P4 value if ne
19269 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74  cessary..*/.stat
1926a 69 63 20 76 6f 69 64 20 66 72 65 65 50 34 28 73  ic void freeP4(s
1926b 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
1926c 70 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70 34  p4type, void *p4
1926d 29 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a 20  ){.  if( p4 ){. 
1926e 20 20 20 73 77 69 74 63 68 28 20 70 34 74 79 70     switch( p4typ
1926f 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
19270 50 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20 63  P4_REAL:.      c
19271 61 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20 20  ase P4_INT64:.  
19272 20 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52 49      case P4_MPRI
19273 4e 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65 20  NTF:.      case 
19274 50 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 20  P4_DYNAMIC:.    
19275 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46    case P4_KEYINF
19276 4f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34  O:.      case P4
19277 5f 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 20  _INTARRAY:.     
19278 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f   case P4_KEYINFO
19279 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 20  _HANDOFF: {.    
1927a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1927b 65 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 20  e(db, p4);.     
1927c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1927d 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  }.      case P4_
1927e 56 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20 20  VDBEFUNC: {.    
1927f 20 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56      VdbeFunc *pV
19280 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 46  dbeFunc = (VdbeF
19281 75 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20 20  unc *)p4;.      
19282 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46    freeEphemeralF
19283 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64 62  unction(db, pVdb
19284 65 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a 20  eFunc->pFunc);. 
19285 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19286 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28  beDeleteAuxData(
19287 70 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a 20  pVdbeFunc, 0);. 
19288 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
19289 46 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 75  Free(db, pVdbeFu
1928a 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  nc);.        bre
1928b 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1928c 20 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 45    case P4_FUNCDE
1928d 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 65  F: {.        fre
1928e 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69  eEphemeralFuncti
1928f 6f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 2a  on(db, (FuncDef*
19290 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72  )p4);.        br
19291 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
19292 20 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20     case P4_MEM: 
19293 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
19294 33 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c 69  3ValueFree((sqli
19295 74 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b 0a  te3_value*)p4);.
19296 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
19297 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
19298 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  .}.../*.** Chang
19299 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 72  e N opcodes star
1929a 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f 20  ting at addr to 
1929b 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54  No-ops..*/.SQLIT
1929c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1929d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1929e 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c 20  ToNoop(Vdbe *p, 
1929f 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29  int addr, int N)
192a0 7b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e  {.  if( p && p->
192a1 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65 4f  aOp ){.    VdbeO
192a2 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70  p *pOp = &p->aOp
192a3 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69  [addr];.    sqli
192a4 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
192a5 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20  .    while( N-- 
192a6 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28  ){.      freeP4(
192a7 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c  db, pOp->p4type,
192a8 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 20   pOp->p4.p);.   
192a9 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30     memset(pOp, 0
192aa 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29  , sizeof(pOp[0])
192ab 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70  );.      pOp->op
192ac 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a  code = OP_Noop;.
192ad 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 20        pOp++;.   
192ae 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
192af 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
192b0 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72 61   of the P4 opera
192b1 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69  nd for a specifi
192b2 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  c instruction..*
192b3 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
192b4 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20  s useful when a 
192b5 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73  large program is
192b6 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a   loaded from a.*
192b7 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75  * static array u
192b8 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65  sing sqlite3Vdbe
192b9 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65  AddOpList but we
192ba 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a   want to make a.
192bb 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61  ** few minor cha
192bc 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67  nges to the prog
192bd 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e  ram..**.** If n>
192be 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 20 6f  =0 then the P4 o
192bf 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 6d 69  perand is dynami
192c0 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20  c, meaning that 
192c1 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65  a copy of.** the
192c2 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 65 20   string is made 
192c3 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
192c4 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
192c5 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41  3_malloc()..** A
192c6 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 6d   value of n==0 m
192c7 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 73 20  eans copy bytes 
192c8 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 6e 64  of zP4 up to and
192c9 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a 2a   including the.*
192ca 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 79 74  * first null byt
192cb 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 6e 20  e.  If n>0 then 
192cc 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 20 6f  copy n+1 bytes o
192cd 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  f zP4..**.** If 
192ce 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69 74  n==P4_KEYINFO it
192cf 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 34 20   means that zP4 
192d0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
192d1 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  a KeyInfo struct
192d2 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 69  ure..** A copy i
192d3 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 4b 65  s made of the Ke
192d4 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
192d5 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
192d6 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c  ined from.** sql
192d7 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20  ite3_malloc, to 
192d8 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 74 68  be freed when th
192d9 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69  e Vdbe is finali
192da 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45  zed..** n==P4_KE
192db 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 6e  YINFO_HANDOFF in
192dc 64 69 63 61 74 65 73 20 74 68 61 74 20 7a 50 34  dicates that zP4
192dd 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79   points to a Key
192de 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a  Info structure.*
192df 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f  * stored in memo
192e0 72 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  ry that the call
192e1 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20  er has obtained 
192e2 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
192e3 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61 6c  loc. The .** cal
192e4 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66  ler should not f
192e5 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  ree the allocati
192e6 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 66  on, it will be f
192e7 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64  reed when the Vd
192e8 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a  be is.** finaliz
192e9 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72  ed..** .** Other
192ea 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50 34   values of n (P4
192eb 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4c  _STATIC, P4_COLL
192ec 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63 61  SEQ etc.) indica
192ed 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e  te that zP4 poin
192ee 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69 6e  ts.** to a strin
192ef 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20 74  g or structure t
192f0 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65 65  hat is guarantee
192f1 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20 74  d to exist for t
192f2 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a  he lifetime of.*
192f3 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20 74  * the Vdbe. In t
192f4 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63 61  hese cases we ca
192f5 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65 20  n just copy the 
192f6 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  pointer..**.** I
192f7 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63 68  f addr<0 then ch
192f8 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 20 6d  ange P4 on the m
192f9 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73  ost recently ins
192fa 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69 6f  erted instructio
192fb 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
192fc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
192fd 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 64  3VdbeChangeP4(Vd
192fe 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c  be *p, int addr,
192ff 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34   const char *zP4
19300 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 2a  , int n){.  Op *
19301 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  pOp;.  sqlite3 *
19302 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 21  db;.  assert( p!
19303 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d 3e  =0 );.  db = p->
19304 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  db;.  assert( p-
19305 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
19306 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
19307 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62   p->aOp==0 || db
19308 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
19309 7b 0a 20 20 20 20 69 66 20 28 6e 20 21 3d 20 50  {.    if (n != P
1930a 34 5f 4b 45 59 49 4e 46 4f 29 20 7b 0a 20 20 20  4_KEYINFO) {.   
1930b 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 6e 2c     freeP4(db, n,
1930c 20 28 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a 2a   (void*)*(char**
1930d 29 26 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20 20  )&zP4);.    }.  
1930e 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1930f 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d 3e  assert( addr<p->
19310 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64 64  nOp );.  if( add
19311 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72 20  r<0 ){.    addr 
19312 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20  = p->nOp - 1;.  
19313 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 20 72    if( addr<0 ) r
19314 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 4f 70  eturn;.  }.  pOp
19315 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d   = &p->aOp[addr]
19316 3b 0a 20 20 66 72 65 65 50 34 28 64 62 2c 20 70  ;.  freeP4(db, p
19317 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d  Op->p4type, pOp-
19318 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d 3e 70  >p4.p);.  pOp->p
19319 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e  4.p = 0;.  if( n
1931a 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20  ==P4_INT32 ){.  
1931b 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69 73 20    /* Note: this 
1931c 63 61 73 74 20 69 73 20 73 61 66 65 2c 20 62 65  cast is safe, be
1931d 63 61 75 73 65 20 74 68 65 20 6f 72 69 67 69 6e  cause the origin
1931e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61 73 20   data point was 
1931f 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20 74 68  an int.    ** th
19320 61 74 20 77 61 73 20 63 61 73 74 20 74 6f 20 61  at was cast to a
19321 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 2e   (const char *).
19322 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e   */.    pOp->p4.
19323 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54  i = SQLITE_PTR_T
19324 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 20 20 20  O_INT(zP4);.    
19325 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
19326 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 6c 73 65 20  _INT32;.  }else 
19327 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20  if( zP4==0 ){.  
19328 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b    pOp->p4.p = 0;
19329 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65  .    pOp->p4type
1932a 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20   = P4_NOTUSED;. 
1932b 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34   }else if( n==P4
1932c 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20  _KEYINFO ){.    
1932d 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
1932e 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 65 6c  o;.    int nFiel
1932f 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20 20 6e  d, nByte;..    n
19330 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49 6e 66  Field = ((KeyInf
19331 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c 64 3b  o*)zP4)->nField;
19332 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73 69 7a  .    nByte = siz
19333 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29 20 2b  eof(*pKeyInfo) +
19334 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69 7a 65   (nField-1)*size
19335 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  of(pKeyInfo->aCo
19336 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c 64 3b  ll[0]) + nField;
19337 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20  .    pKeyInfo = 
19338 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e  sqlite3Malloc( n
19339 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f 70 2d  Byte );.    pOp-
1933a 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70  >p4.pKeyInfo = p
1933b 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 66 28  KeyInfo;.    if(
1933c 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20   pKeyInfo ){.   
1933d 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 65     u8 *aSortOrde
1933e 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  r;.      memcpy(
1933f 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c 20 6e  pKeyInfo, zP4, n
19340 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 53 6f  Byte);.      aSo
19341 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79 49 6e  rtOrder = pKeyIn
19342 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 3b 0a  fo->aSortOrder;.
19343 20 20 20 20 20 20 69 66 28 20 61 53 6f 72 74 4f        if( aSortO
19344 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20  rder ){.        
19345 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f  pKeyInfo->aSortO
19346 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e 65 64  rder = (unsigned
19347 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e 66 6f   char*)&pKeyInfo
19348 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64 5d 3b  ->aColl[nField];
19349 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
1934a 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f  pKeyInfo->aSortO
1934b 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64 65 72  rder, aSortOrder
1934c 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20 20  , nField);.     
1934d 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 34   }.      pOp->p4
1934e 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 46  type = P4_KEYINF
1934f 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  O;.    }else{.  
19350 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f      p->db->mallo
19351 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
19352 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d     pOp->p4type =
19353 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20   P4_NOTUSED;.   
19354 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e   }.  }else if( n
19355 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  ==P4_KEYINFO_HAN
19356 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f 70 2d  DOFF ){.    pOp-
19357 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a  >p4.p = (void*)z
19358 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74  P4;.    pOp->p4t
19359 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e 46 4f  ype = P4_KEYINFO
1935a 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3c  ;.  }else if( n<
1935b 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  0 ){.    pOp->p4
1935c 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b  .p = (void*)zP4;
1935d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65  .    pOp->p4type
1935e 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 29   = (signed char)
1935f 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  n;.  }else{.    
19360 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20 73  if( n==0 ) n = s
19361 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
19362 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  P4);.    pOp->p4
19363 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  .z = sqlite3DbSt
19364 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50 34  rNDup(p->db, zP4
19365 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  , n);.    pOp->p
19366 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d  4type = P4_DYNAM
19367 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64  IC;.  }.}..#ifnd
19368 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ef NDEBUG./*.** 
19369 43 68 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d 65  Change the comme
1936a 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 20 6d 6f  nt on the the mo
1936b 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 65  st recently code
1936c 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20  d instruction.  
1936d 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20 4e  Or.** insert a N
1936e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68 65  o-op and add the
1936f 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61 74   comment to that
19370 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e   new instruction
19371 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 6b 65 73  .  This.** makes
19372 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65 72   the code easier
19373 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67 20   to read during 
19374 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e 65  debugging.  None
19375 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e 73   of this happens
19376 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63 74  .** in a product
19377 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f 0a 53 51  ion build..*/.SQ
19378 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19379 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d  d sqlite3VdbeCom
1937a 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63 6f  ment(Vdbe *p, co
1937b 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
1937c 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  t, ...){.  va_li
1937d 73 74 20 61 70 3b 0a 20 20 61 73 73 65 72 74 28  st ap;.  assert(
1937e 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e   p->nOp>0 || p->
1937f 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  aOp==0 );.  asse
19380 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c  rt( p->aOp==0 ||
19381 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31   p->aOp[p->nOp-1
19382 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c  ].zComment==0 ||
19383 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
19384 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 2d  iled );.  if( p-
19385 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61 72  >nOp ){.    char
19386 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b   **pz = &p->aOp[
19387 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65  p->nOp-1].zComme
19388 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72 74  nt;.    va_start
19389 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
1938a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1938b 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 20  (p->db, *pz);.  
1938c 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33 56    *pz = sqlite3V
1938d 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a  MPrintf(p->db, z
1938e 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20  Format, ap);.   
1938f 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7d   va_end(ap);.  }
19390 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
19391 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
19392 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64  beNoopComment(Vd
19393 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  be *p, const cha
19394 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
19395 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
19396 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
19397 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b  Op0(p, OP_Noop);
19398 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f  .  assert( p->nO
19399 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30  p>0 || p->aOp==0
1939a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1939b 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f  >aOp==0 || p->aO
1939c 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d  p[p->nOp-1].zCom
1939d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62  ment==0 || p->db
1939e 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1939f 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29  ;.  if( p->nOp )
193a0 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20  {.    char **pz 
193a1 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70  = &p->aOp[p->nOp
193a2 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20  -1].zComment;.  
193a3 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
193a4 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c  Format);.    sql
193a5 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62  ite3DbFree(p->db
193a6 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20  , *pz);.    *pz 
193a7 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
193a8 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74  f(p->db, zFormat
193a9 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e  , ap);.    va_en
193aa 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  d(ap);.  }.}.#en
193ab 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a  dif  /* NDEBUG *
193ac 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
193ad 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 61  the opcode for a
193ae 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e 0a   given address..
193af 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
193b0 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65  E VdbeOp *sqlite
193b1 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 20  3VdbeGetOp(Vdbe 
193b2 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20  *p, int addr){. 
193b3 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
193b4 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
193b5 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  IT );.  assert( 
193b6 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64 72  (addr>=0 && addr
193b7 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e 64  <p->nOp) || p->d
193b8 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
193b9 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 61 64  );.  return ((ad
193ba 64 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70 2d  dr>=0 && addr<p-
193bb 3e 6e 4f 70 29 3f 28 26 70 2d 3e 61 4f 70 5b 61  >nOp)?(&p->aOp[a
193bc 64 64 72 5d 29 3a 30 29 3b 0a 7d 0a 0a 23 69 66  ddr]):0);.}..#if
193bd 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
193be 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c  _OMIT_EXPLAIN) |
193bf 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  | !defined(NDEBU
193c0 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65 66  G) \.     || def
193c1 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c  ined(VDBE_PROFIL
193c2 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
193c3 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
193c4 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69  * Compute a stri
193c5 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62 65  ng that describe
193c6 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74  s the P4 paramet
193c7 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64 65  er for an opcode
193c8 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20 66  ..** Use zTemp f
193c9 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64 20  or any required 
193ca 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 72  temporary buffer
193cb 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69   space..*/.stati
193cc 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79 50  c char *displayP
193cd 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72 20  4(Op *pOp, char 
193ce 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65 6d  *zTemp, int nTem
193cf 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34 20  p){.  char *zP4 
193d0 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65 72  = zTemp;.  asser
193d1 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b 0a  t( nTemp>=20 );.
193d2 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 70    switch( pOp->p
193d3 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  4type ){.    cas
193d4 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41  e P4_KEYINFO_STA
193d5 54 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50 34  TIC:.    case P4
193d6 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20 20  _KEYINFO: {.    
193d7 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20    int i, j;.    
193d8 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
193d9 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b  nfo = pOp->p4.pK
193da 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 71  eyInfo;.      sq
193db 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
193dc 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b 65  Temp, zTemp, "ke
193dd 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79 49  yinfo(%d", pKeyI
193de 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20 20  nfo->nField);.  
193df 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33 53      i = sqlite3S
193e0 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b 0a  trlen30(zTemp);.
193e1 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
193e2 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c  <pKeyInfo->nFiel
193e3 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  d; j++){.       
193e4 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
193e5 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  = pKeyInfo->aCol
193e6 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  l[j];.        if
193e7 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20  ( pColl ){.     
193e8 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c       int n = sql
193e9 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43 6f  ite3Strlen30(pCo
193ea 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ll->zName);.    
193eb 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e 54        if( i+n>nT
193ec 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20  emp-6 ){.       
193ed 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65       memcpy(&zTe
193ee 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29 3b  mp[i],",...",4);
193ef 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
193f0 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
193f1 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b            zTemp[
193f2 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20 20  i++] = ',';.    
193f3 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e        if( pKeyIn
193f4 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26  fo->aSortOrder &
193f5 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  & pKeyInfo->aSor
193f6 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20 20  tOrder[j] ){.   
193f7 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 69           zTemp[i
193f8 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20  ++] = '-';.     
193f9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
193fa 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69   memcpy(&zTemp[i
193fb 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c  ], pColl->zName,
193fc 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  n+1);.          
193fd 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20  i += n;.        
193fe 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e 54  }else if( i+4<nT
193ff 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20 20  emp-6 ){.       
19400 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70     memcpy(&zTemp
19401 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a 20  [i],",nil",4);. 
19402 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34 3b           i += 4;
19403 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
19404 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69   }.      zTemp[i
19405 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20 20  ++] = ')';.     
19406 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a 20   zTemp[i] = 0;. 
19407 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e       assert( i<n
19408 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62 72  Temp );.      br
19409 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1940a 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a 20  ase P4_COLLSEQ: 
1940b 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  {.      CollSeq 
1940c 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70 34  *pColl = pOp->p4
1940d 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73 71  .pColl;.      sq
1940e 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
1940f 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63 6f  Temp, zTemp, "co
19410 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20 70  llseq(%.20s)", p
19411 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Coll->zName);.  
19412 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
19413 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 4e  .    case P4_FUN
19414 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46 75  CDEF: {.      Fu
19415 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70 4f  ncDef *pDef = pO
19416 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 20  p->p4.pFunc;.   
19417 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
19418 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
19419 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65 66  , "%s(%d)", pDef
1941a 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 6e  ->zName, pDef->n
1941b 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61  Arg);.      brea
1941c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1941d 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20 20  e P4_INT64: {.  
1941e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1941f 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
19420 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70 2d  p, "%lld", *pOp-
19421 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20 20  >p4.pI64);.     
19422 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
19423 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32 3a    case P4_INT32:
19424 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
19425 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c  _snprintf(nTemp,
19426 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70 4f   zTemp, "%d", pO
19427 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20 20  p->p4.i);.      
19428 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
19429 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20 7b   case P4_REAL: {
1942a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1942b 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a  nprintf(nTemp, z
1942c 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20 2a  Temp, "%.16g", *
1942d 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b 0a  pOp->p4.pReal);.
1942e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1942f 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4d   }.    case P4_M
19430 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20  EM: {.      Mem 
19431 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e  *pMem = pOp->p4.
19432 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65  pMem;.      asse
19433 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
19434 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20   & MEM_Null)==0 
19435 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65  );.      if( pMe
19436 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53  m->flags & MEM_S
19437 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 50  tr ){.        zP
19438 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20  4 = pMem->z;.   
19439 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65     }else if( pMe
1943a 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  m->flags & MEM_I
1943b 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  nt ){.        sq
1943c 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
1943d 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c  Temp, zTemp, "%l
1943e 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b  ld", pMem->u.i);
1943f 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
19440 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
19441 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20  EM_Real ){.     
19442 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
19443 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
19444 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65 6d 2d  , "%.16g", pMem-
19445 3e 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  >r);.      }.   
19446 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
19447 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19448 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
19449 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 54 41  .    case P4_VTA
1944a 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  B: {.      sqlit
1944b 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d  e3_vtab *pVtab =
1944c 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a   pOp->p4.pVtab;.
1944d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1944e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1944f 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a 25 70  emp, "vtab:%p:%p
19450 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61 62 2d  ", pVtab, pVtab-
19451 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 20  >pModule);.     
19452 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
19453 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 50 34  ndif.    case P4
19454 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20 20  _INTARRAY: {.   
19455 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
19456 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
19457 2c 20 22 69 6e 74 61 72 72 61 79 22 29 3b 0a 20  , "intarray");. 
19458 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
19459 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
1945a 0a 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4f 70  .      zP4 = pOp
1945b 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69 66  ->p4.z;.      if
1945c 28 20 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20  ( zP4==0 ){.    
1945d 20 20 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70 3b      zP4 = zTemp;
1945e 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 30  .        zTemp[0
1945f 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  ] = 0;.      }. 
19460 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
19461 74 28 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20 72  t( zP4!=0 );.  r
19462 65 74 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65 6e  eturn zP4;.}.#en
19463 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61  dif../*.** Decla
19464 72 65 20 74 6f 20 74 68 65 20 56 64 62 65 20 74  re to the Vdbe t
19465 68 61 74 20 74 68 65 20 42 54 72 65 65 20 6f 62  hat the BTree ob
19466 6a 65 63 74 20 61 74 20 64 62 2d 3e 61 44 62 5b  ject at db->aDb[
19467 69 5d 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a  i] is used..**.*
19468 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19469 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1946a 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 20  eUsesBtree(Vdbe 
1946b 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e  *p, int i){.  in
1946c 74 20 6d 61 73 6b 3b 0a 20 20 61 73 73 65 72 74  t mask;.  assert
1946d 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 64  ( i>=0 && i<p->d
1946e 62 2d 3e 6e 44 62 20 26 26 20 69 3c 73 69 7a 65  b->nDb && i<size
1946f 6f 66 28 75 33 32 29 2a 38 20 29 3b 0a 20 20 61  of(u32)*8 );.  a
19470 73 73 65 72 74 28 20 69 3c 28 69 6e 74 29 73 69  ssert( i<(int)si
19471 7a 65 6f 66 28 70 2d 3e 62 74 72 65 65 4d 61 73  zeof(p->btreeMas
19472 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 73 6b 20 3d  k)*8 );.  mask =
19473 20 28 28 75 33 32 29 31 29 3c 3c 69 3b 0a 20 20   ((u32)1)<<i;.  
19474 69 66 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  if( (p->btreeMas
19475 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a  k & mask)==0 ){.
19476 20 20 20 20 70 2d 3e 62 74 72 65 65 4d 61 73 6b      p->btreeMask
19477 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 73 71   |= mask;.    sq
19478 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
19479 72 72 61 79 49 6e 73 65 72 74 28 26 70 2d 3e 61  rrayInsert(&p->a
1947a 4d 75 74 65 78 2c 20 70 2d 3e 64 62 2d 3e 61 44  Mutex, p->db->aD
1947b 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 7d 0a 7d  b[i].pBt);.  }.}
1947c 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 56  ...#if defined(V
1947d 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20  DBE_PROFILE) || 
1947e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
1947f 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e  EBUG)./*.** Prin
19480 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 63 6f 64  t a single opcod
19481 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
19482 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62   is used for deb
19483 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ugging only..*/.
19484 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19485 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50  oid sqlite3VdbeP
19486 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a 70 4f 75  rintOp(FILE *pOu
19487 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 20 2a 70  t, int pc, Op *p
19488 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34  Op){.  char *zP4
19489 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 5b 35 30  ;.  char zPtr[50
1948a 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ];.  static cons
1948b 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 31  t char *zFormat1
1948c 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20 25 34   = "%4d %-13s %4
1948d 64 20 25 34 64 20 25 34 64 20 25 2d 34 73 20 25  d %4d %4d %-4s %
1948e 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 69 66 28  .2X %s\n";.  if(
1948f 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f 75 74 20   pOut==0 ) pOut 
19490 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a 50 34 20  = stdout;.  zP4 
19491 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 2c  = displayP4(pOp,
19492 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 28 7a 50   zPtr, sizeof(zP
19493 74 72 29 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  tr));.  fprintf(
19494 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 31 2c 20  pOut, zFormat1, 
19495 70 63 2c 20 0a 20 20 20 20 20 20 73 71 6c 69 74  pc, .      sqlit
19496 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70  e3OpcodeName(pOp
19497 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f 70 2d 3e  ->opcode), pOp->
19498 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f 70  p1, pOp->p2, pOp
19499 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f 70 2d 3e  ->p3, zP4, pOp->
1949a 70 35 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54  p5,.#ifdef SQLIT
1949b 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f  E_DEBUG.      pO
1949c 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f 20 70 4f  p->zComment ? pO
1949d 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a 20 22 22  p->zComment : ""
1949e 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 22 22 0a  .#else.      "".
1949f 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 66 66  #endif.  );.  ff
194a0 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a 23 65  lush(pOut);.}.#e
194a1 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  ndif../*.** Rele
194a2 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20  ase an array of 
194a3 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 0a 2a  N Mem elements.*
194a4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
194a5 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 4d 65  leaseMemArray(Me
194a6 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  m *p, int N){.  
194a7 69 66 28 20 70 20 26 26 20 4e 20 29 7b 0a 20 20  if( p && N ){.  
194a8 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a 20 20 20    Mem *pEnd;.   
194a9 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
194aa 2d 3e 64 62 3b 0a 20 20 20 20 75 38 20 6d 61 6c  ->db;.    u8 mal
194ab 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 64 62 2d  loc_failed = db-
194ac 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 0a 20  >mallocFailed;. 
194ad 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 5b 4e     for(pEnd=&p[N
194ae 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b 29 7b  ]; p<pEnd; p++){
194af 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
194b0 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 20 7c 7c 20  &p[1])==pEnd || 
194b1 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64 62  p[0].db==p[1].db
194b2 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68   );..      /* Th
194b3 69 73 20 62 6c 6f 63 6b 20 69 73 20 72 65 61 6c  is block is real
194b4 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 64 20 76 65  ly an inlined ve
194b5 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  rsion of sqlite3
194b6 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 29  VdbeMemRelease()
194b7 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 74  .      ** that t
194b8 61 6b 65 73 20 61 64 76 61 6e 74 61 67 65 20 6f  akes advantage o
194b9 66 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20  f the fact that 
194ba 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
194bb 76 61 6c 75 65 20 69 73 20 0a 20 20 20 20 20 20  value is .      
194bc 2a 2a 20 62 65 69 6e 67 20 73 65 74 20 74 6f 20  ** being set to 
194bd 4e 55 4c 4c 20 61 66 74 65 72 20 72 65 6c 65 61  NULL after relea
194be 73 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d 69 63  sing any dynamic
194bf 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 20 20 20   resources..    
194c0 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
194c1 65 20 6a 75 73 74 69 66 69 63 61 74 69 6f 6e 20  e justification 
194c2 66 6f 72 20 64 75 70 6c 69 63 61 74 69 6e 67 20  for duplicating 
194c3 63 6f 64 65 20 69 73 20 74 68 61 74 20 61 63 63  code is that acc
194c4 6f 72 64 69 6e 67 20 74 6f 20 0a 20 20 20 20 20  ording to .     
194c5 20 2a 2a 20 63 61 6c 6c 67 72 69 6e 64 2c 20 74   ** callgrind, t
194c6 68 69 73 20 63 61 75 73 65 73 20 61 20 63 65 72  his causes a cer
194c7 74 61 69 6e 20 74 65 73 74 20 63 61 73 65 20 74  tain test case t
194c8 6f 20 68 69 74 20 74 68 65 20 43 50 55 20 34 2e  o hit the CPU 4.
194c9 37 20 0a 20 20 20 20 20 20 2a 2a 20 70 65 72 63  7 .      ** perc
194ca 65 6e 74 20 6c 65 73 73 20 28 78 38 36 20 6c 69  ent less (x86 li
194cb 6e 75 78 2c 20 67 63 63 20 76 65 72 73 69 6f 6e  nux, gcc version
194cc 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 20 74 68 61   4.1.2, -O6) tha
194cd 6e 20 69 66 20 0a 20 20 20 20 20 20 2a 2a 20 73  n if .      ** s
194ce 71 6c 69 74 65 33 4d 65 6d 52 65 6c 65 61 73 65  qlite3MemRelease
194cf 28 29 20 77 65 72 65 20 63 61 6c 6c 65 64 20 66  () were called f
194d0 72 6f 6d 20 68 65 72 65 2e 20 57 69 74 68 20 2d  rom here. With -
194d1 4f 32 2c 20 74 68 69 73 20 6a 75 6d 70 73 0a 20  O2, this jumps. 
194d2 20 20 20 20 20 2a 2a 20 74 6f 20 36 2e 36 20 70       ** to 6.6 p
194d3 65 72 63 65 6e 74 2e 20 54 68 65 20 74 65 73 74  ercent. The test
194d4 20 63 61 73 65 20 69 73 20 69 6e 73 65 72 74 69   case is inserti
194d5 6e 67 20 31 30 30 30 20 72 6f 77 73 20 69 6e 74  ng 1000 rows int
194d6 6f 20 61 20 74 61 62 6c 65 20 0a 20 20 20 20 20  o a table .     
194d7 20 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e 64 65   ** with no inde
194d8 78 65 73 20 75 73 69 6e 67 20 61 20 73 69 6e 67  xes using a sing
194d9 6c 65 20 70 72 65 70 61 72 65 64 20 49 4e 53 45  le prepared INSE
194da 52 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 69  RT statement, bi
194db 6e 64 28 29 20 0a 20 20 20 20 20 20 2a 2a 20 61  nd() .      ** a
194dc 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e 73 65  nd reset(). Inse
194dd 72 74 73 20 61 72 65 20 67 72 6f 75 70 65 64 20  rts are grouped 
194de 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63 74 69  into a transacti
194df 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  on..      */.   
194e0 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26     if( p->flags&
194e1 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e  (MEM_Agg|MEM_Dyn
194e2 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
194e3 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
194e4 73 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c  se(p);.      }el
194e5 73 65 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f  se if( p->zMallo
194e6 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  c ){.        sql
194e7 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
194e8 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20  ->zMalloc);.    
194e9 20 20 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d      p->zMalloc =
194ea 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   0;.      }..   
194eb 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45     p->flags = ME
194ec 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20  M_Null;.    }.  
194ed 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
194ee 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c  ed = malloc_fail
194ef 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65  ed;.  }.}..#ifde
194f0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
194f1 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e  MEMORY_MANAGEMEN
194f2 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
194f3 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
194f4 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28 56  ReleaseBuffers(V
194f5 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  dbe *p){.  int i
194f6 69 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d  i;.  int nFree =
194f7 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   0;.  assert( sq
194f8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
194f9 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
194fa 3b 0a 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69  ;.  for(ii=1; ii
194fb 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b 29  <=p->nMem; ii++)
194fc 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20  {.    Mem *pMem 
194fd 3d 20 26 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b 0a  = &p->aMem[ii];.
194fe 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c      if( pMem->fl
194ff 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
19500 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
19501 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65  3RowSetClear(pMe
19502 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20  m->u.pRowSet);. 
19503 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4d 65     }.    if( pMe
19504 6d 2d 3e 7a 20 26 26 20 70 4d 65 6d 2d 3e 66 6c  m->z && pMem->fl
19505 61 67 73 26 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20  ags&MEM_Dyn ){. 
19506 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 4d       assert( !pM
19507 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20  em->xDel );.    
19508 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74    nFree += sqlit
19509 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  e3DbMallocSize(p
1950a 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
1950b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1950c 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
1950d 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Mem);.    }.  }.
1950e 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a    return nFree;.
1950f 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
19510 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  f SQLITE_OMIT_EX
19511 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65  PLAIN./*.** Give
19512 20 61 20 6c 69 73 74 69 6e 67 20 6f 66 20 74 68   a listing of th
19513 65 20 70 72 6f 67 72 61 6d 20 69 6e 20 74 68 65  e program in the
19514 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
19515 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ..**.** The inte
19516 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d  rface is the sam
19517 65 20 61 73 20 73 71 6c 69 74 65 33 56 64 62 65  e as sqlite3Vdbe
19518 45 78 65 63 28 29 2e 20 20 42 75 74 20 69 6e 73  Exec().  But ins
19519 74 65 61 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69  tead of.** runni
1951a 6e 67 20 74 68 65 20 63 6f 64 65 2c 20 69 74 20  ng the code, it 
1951b 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 61 6c 6c  invokes the call
1951c 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61  back once for ea
1951d 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ch instruction..
1951e 2a 2a 20 54 68 69 73 20 66 65 61 74 75 72 65 20  ** This feature 
1951f 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  is used to imple
19520 6d 65 6e 74 20 22 45 58 50 4c 41 49 4e 22 2e 0a  ment "EXPLAIN"..
19521 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 78  **.** When p->ex
19522 70 6c 61 69 6e 3d 3d 31 2c 20 65 61 63 68 20 69  plain==1, each i
19523 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6c 69  nstruction is li
19524 73 74 65 64 2e 20 20 57 68 65 6e 0a 2a 2a 20 70  sted.  When.** p
19525 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e  ->explain==2, on
19526 6c 79 20 4f 50 5f 45 78 70 6c 61 69 6e 20 69 6e  ly OP_Explain in
19527 73 74 72 75 63 74 69 6f 6e 73 20 61 72 65 20 6c  structions are l
19528 69 73 74 65 64 20 61 6e 64 20 74 68 65 73 65 0a  isted and these.
19529 2a 2a 20 61 72 65 20 73 68 6f 77 6e 20 69 6e 20  ** are shown in 
1952a 61 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 6d  a different form
1952b 61 74 2e 20 20 70 2d 3e 65 78 70 6c 61 69 6e 3d  at.  p->explain=
1952c 3d 32 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d  =2 is used to im
1952d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c 41  plement.** EXPLA
1952e 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 2e 0a 2a  IN QUERY PLAN..*
1952f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19530 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
19531 4c 69 73 74 28 0a 20 20 56 64 62 65 20 2a 70 20  List(.  Vdbe *p 
19532 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19533 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f    /* The VDBE */
19534 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
19535 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74  b = p->db;.  int
19536 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   i;.  int rc = S
19537 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20  QLITE_OK;.  Mem 
19538 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75  *pMem = p->pResu
19539 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d  ltSet = &p->aMem
1953a 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  [1];..  assert( 
1953b 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20  p->explain );.  
1953c 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44  if( p->magic!=VD
1953d 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29 20 72  BE_MAGIC_RUN ) r
1953e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1953f 55 53 45 3b 0a 20 20 61 73 73 65 72 74 28 20 64  USE;.  assert( d
19540 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45  b->magic==SQLITE
19541 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a 20  _MAGIC_BUSY );. 
19542 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d   assert( p->rc==
19543 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
19544 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
19545 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  || p->rc==SQLITE
19546 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20  _NOMEM );..  /* 
19547 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 69 73  Even though this
19548 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74   opcode does not
19549 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73 74 72   use dynamic str
1954a 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68  ings for.  ** th
1954b 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75 6c 74  e result, result
1954c 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63   columns may bec
1954d 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 20 74  ome dynamic if t
1954e 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a 20 20  he user calls.  
1954f 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
19550 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61 75 73  n_text16(), caus
19551 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 69 6f  ing a translatio
19552 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f  n to UTF-16 enco
19553 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c  ding..  */.  rel
19554 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 4d 65  easeMemArray(pMe
19555 6d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 0a 20 20  m, p->nMem);..  
19556 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  if( p->rc==SQLIT
19557 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f  E_NOMEM ){.    /
19558 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 69  * This happens i
19559 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73  f a malloc() ins
1955a 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ide a call to sq
1955b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
1955c 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71  t() or.    ** sq
1955d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
1955e 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 2a  t16() failed.  *
1955f 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  /.    db->malloc
19560 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
19561 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
19562 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a  ROR;.  }..  do{.
19563 20 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b      i = p->pc++;
19564 0a 20 20 7d 77 68 69 6c 65 28 20 69 3c 70 2d 3e  .  }while( i<p->
19565 6e 4f 70 20 26 26 20 70 2d 3e 65 78 70 6c 61 69  nOp && p->explai
19566 6e 3d 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69  n==2 && p->aOp[i
19567 5d 2e 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70  ].opcode!=OP_Exp
19568 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e  lain );.  if( i>
19569 3d 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 70  =p->nOp ){.    p
1956a 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
1956b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1956c 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20  E_DONE;.  }else 
1956d 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  if( db->u1.isInt
1956e 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20  errupted ){.    
1956f 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 49  p->rc = SQLITE_I
19570 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 72 63  NTERRUPT;.    rc
19571 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
19572 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
19573 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
19574 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
19575 69 74 65 33 45 72 72 53 74 72 28 70 2d 3e 72 63  ite3ErrStr(p->rc
19576 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ));.  }else{.   
19577 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 4f 70   char *z;.    Op
19578 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b   *pOp = &p->aOp[
19579 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 65  i];.    if( p->e
1957a 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20  xplain==1 ){.   
1957b 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1957c 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1957d 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1957e 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  ITE_INTEGER;.   
1957f 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69     pMem->u.i = i
19580 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19581 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19582 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e   /* Program coun
19583 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ter */.      pMe
19584 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d  m++;.  .      pM
19585 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
19586 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d  Static|MEM_Str|M
19587 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  EM_Term;.      p
19588 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29  Mem->z = (char*)
19589 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d  sqlite3OpcodeNam
1958a 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20  e(pOp->opcode); 
1958b 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20   /* Opcode */.  
1958c 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
1958d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  ->z!=0 );.      
1958e 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65  pMem->n = sqlite
1958f 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e  3Strlen30(pMem->
19590 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  z);.      pMem->
19591 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45  type = SQLITE_TE
19592 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  XT;.      pMem->
19593 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
19594 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  8;.      pMem++;
19595 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d  .    }..    pMem
19596 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
19597 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  t;.    pMem->u.i
19598 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20   = pOp->p1;     
19599 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1959a 20 20 20 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20       /* P1 */.  
1959b 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
1959c 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20  QLITE_INTEGER;. 
1959d 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20     pMem++;..    
1959e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1959f 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d  M_Int;.    pMem-
195a0 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20  >u.i = pOp->p2; 
195a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
195a2 20 20 20 20 20 20 20 20 20 2f 2a 20 50 32 20 2a           /* P2 *
195a3 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65  /.    pMem->type
195a4 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45   = SQLITE_INTEGE
195a5 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a  R;.    pMem++;..
195a6 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61      if( p->expla
195a7 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70  in==1 ){.      p
195a8 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
195a9 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d  _Int;.      pMem
195aa 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b  ->u.i = pOp->p3;
195ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
195ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 33 20            /* P3 
195ad 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74  */.      pMem->t
195ae 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
195af 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d  EGER;.      pMem
195b0 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
195b1 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
195b2 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 33 32 2c 20  mGrow(pMem, 32, 
195b3 30 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  0) ){           
195b4 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 20 20 20 20   /* P4 */.      
195b5 61 73 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d  assert( p->db->m
195b6 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
195b7 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
195b8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
195b9 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
195ba 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74  = MEM_Dyn|MEM_St
195bb 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  r|MEM_Term;.    
195bc 7a 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f  z = displayP4(pO
195bd 70 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29 3b  p, pMem->z, 32);
195be 0a 20 20 20 20 69 66 28 20 7a 21 3d 70 4d 65 6d  .    if( z!=pMem
195bf 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ->z ){.      sql
195c0 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
195c1 72 28 70 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20 53  r(pMem, z, -1, S
195c2 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a  QLITE_UTF8, 0);.
195c3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
195c4 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a   assert( pMem->z
195c5 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d 65  !=0 );.      pMe
195c6 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  m->n = sqlite3St
195c7 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b  rlen30(pMem->z);
195c8 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63  .      pMem->enc
195c9 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
195ca 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e      }.    pMem->
195cb 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45  type = SQLITE_TE
195cc 58 54 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  XT;.    pMem++;.
195cd 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c  .    if( p->expl
195ce 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ain==1 ){.      
195cf 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
195d0 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 34 2c 20  emGrow(pMem, 4, 
195d1 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  0) ){.        as
195d2 73 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c  sert( p->db->mal
195d3 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
195d4 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
195d5 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
195d6 7d 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  }.      pMem->fl
195d7 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45  ags = MEM_Dyn|ME
195d8 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
195d9 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20        pMem->n = 
195da 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  2;.      sqlite3
195db 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 70 4d 65  _snprintf(3, pMe
195dc 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22 2c 20 70 4f  m->z, "%.2x", pO
195dd 70 2d 3e 70 35 29 3b 20 20 20 2f 2a 20 50 35 20  p->p5);   /* P5 
195de 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74  */.      pMem->t
195df 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58  ype = SQLITE_TEX
195e0 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65  T;.      pMem->e
195e1 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
195e2 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  ;.      pMem++;.
195e3 20 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45    .#ifdef SQLITE
195e4 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 28  _DEBUG.      if(
195e5 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 29   pOp->zComment )
195e6 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
195e7 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c  flags = MEM_Str|
195e8 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20  MEM_Term;.      
195e9 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 2d    pMem->z = pOp-
195ea 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 20  >zComment;.     
195eb 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c     pMem->n = sql
195ec 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65  ite3Strlen30(pMe
195ed 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20 20 70  m->z);.        p
195ee 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
195ef 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 20 20  E_UTF8;.        
195f0 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
195f1 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20  ITE_TEXT;.      
195f2 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
195f3 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 4d 65     {.        pMe
195f4 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  m->flags = MEM_N
195f5 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ull;            
195f6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
195f7 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20  mment */.       
195f8 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
195f9 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20  LITE_NULL;.     
195fa 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 2d   }.    }..    p-
195fb 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 38 20  >nResColumn = 8 
195fc 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61 69 6e 2d  - 5*(p->explain-
195fd 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20  1);.    p->rc = 
195fe 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72  SQLITE_OK;.    r
195ff 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a  c = SQLITE_ROW;.
19600 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
19601 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
19602 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
19603 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
19604 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50  TE_DEBUG./*.** P
19605 72 69 6e 74 20 74 68 65 20 53 51 4c 20 74 68 61  rint the SQL tha
19606 74 20 77 61 73 20 75 73 65 64 20 74 6f 20 67 65  t was used to ge
19607 6e 65 72 61 74 65 20 61 20 56 44 42 45 20 70 72  nerate a VDBE pr
19608 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ogram..*/.SQLITE
19609 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1960a 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71  lite3VdbePrintSq
1960b 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  l(Vdbe *p){.  in
1960c 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a  t nOp = p->nOp;.
1960d 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
1960e 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 74   if( nOp<1 ) ret
1960f 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d  urn;.  pOp = &p-
19610 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 70  >aOp[0];.  if( p
19611 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54  Op->opcode==OP_T
19612 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e  race && pOp->p4.
19613 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73  z!=0 ){.    cons
19614 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d  t char *z = pOp-
19615 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68 69 6c 65  >p4.z;.    while
19616 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ( sqlite3Isspace
19617 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20  (*z) ) z++;.    
19618 70 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25 73  printf("SQL: [%s
19619 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a  ]\n", z);.  }.}.
1961a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66  #endif..#if !def
1961b 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1961c 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e  _TRACE) && defin
1961d 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
1961e 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20  _IOTRACE)./*.** 
1961f 50 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43 45  Print an IOTRACE
19620 20 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e 67   message showing
19621 20 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f   SQL content..*/
19622 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19623 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
19624 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 20  IOTraceSql(Vdbe 
19625 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d  *p){.  int nOp =
19626 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f   p->nOp;.  VdbeO
19627 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73 71  p *pOp;.  if( sq
19628 6c 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30 20  lite3IoTrace==0 
19629 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
1962a 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a  nOp<1 ) return;.
1962b 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b    pOp = &p->aOp[
1962c 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f  0];.  if( pOp->o
1962d 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20  pcode==OP_Trace 
1962e 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20  && pOp->p4.z!=0 
1962f 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b  ){.    int i, j;
19630 0a 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30 30  .    char z[1000
19631 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ];.    sqlite3_s
19632 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
19633 29 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d  ), z, "%s", pOp-
19634 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 28  >p4.z);.    for(
19635 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73 73 70  i=0; sqlite3Issp
19636 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  ace(z[i]); i++){
19637 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 7a  }.    for(j=0; z
19638 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  [i]; i++){.     
19639 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 73 70   if( sqlite3Issp
1963a 61 63 65 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20  ace(z[i]) ){.   
1963b 20 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21       if( z[i-1]!
1963c 3d 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20 20  =' ' ){.        
1963d 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a    z[j++] = ' ';.
1963e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1963f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a  }else{.        z
19640 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20  [j++] = z[i];.  
19641 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
19642 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71  z[j] = 0;.    sq
19643 6c 69 74 65 33 49 6f 54 72 61 63 65 28 22 53 51  lite3IoTrace("SQ
19644 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d  L %s\n", z);.  }
19645 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51  .}.#endif /* !SQ
19646 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20  LITE_OMIT_TRACE 
19647 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
19648 5f 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 0a  _IOTRACE */../*.
19649 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  ** Allocate spac
1964a 65 20 66 72 6f 6d 20 61 20 66 69 78 65 64 20 73  e from a fixed s
1964b 69 7a 65 20 62 75 66 66 65 72 2e 20 20 4d 61 6b  ize buffer.  Mak
1964c 65 20 2a 70 70 20 70 6f 69 6e 74 20 74 6f 20 74  e *pp point to t
1964d 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  he.** allocated 
1964e 73 70 61 63 65 2e 20 20 28 4e 6f 74 65 3a 20 20  space.  (Note:  
1964f 70 70 20 69 73 20 61 20 63 68 61 72 2a 20 72 61  pp is a char* ra
19650 74 68 65 72 20 74 68 61 6e 20 61 20 76 6f 69 64  ther than a void
19651 2a 2a 20 74 6f 0a 2a 2a 20 77 6f 72 6b 20 61 72  ** to.** work ar
19652 6f 75 6e 64 20 74 68 65 20 70 6f 69 6e 74 65 72  ound the pointer
19653 20 61 6c 69 61 73 69 6e 67 20 72 75 6c 65 73 20   aliasing rules 
19654 6f 66 20 43 2e 29 20 20 2a 70 70 20 73 68 6f 75  of C.)  *pp shou
19655 6c 64 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20  ld initially.** 
19656 62 65 20 7a 65 72 6f 2e 20 20 49 66 20 2a 70 70  be zero.  If *pp
19657 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68   is not zero, th
19658 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  at means that th
19659 65 20 73 70 61 63 65 20 68 61 73 20 61 6c 72 65  e space has alre
1965a 61 64 79 0a 2a 2a 20 62 65 65 6e 20 61 6c 6c 6f  ady.** been allo
1965b 63 61 74 65 64 20 61 6e 64 20 74 68 69 73 20 72  cated and this r
1965c 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 6f 70  outine is a noop
1965d 2e 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 20 69 73  ..**.** nByte is
1965e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
1965f 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65  ytes of space ne
19660 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 46  eded..**.** *ppF
19661 72 6f 6d 20 70 6f 69 6e 74 20 74 6f 20 61 76 61  rom point to ava
19662 69 6c 61 62 6c 65 20 73 70 61 63 65 20 61 6e 64  ilable space and
19663 20 70 45 6e 64 20 70 6f 69 6e 74 73 20 74 6f 20   pEnd points to 
19664 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a  the end of the.*
19665 2a 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63  * available spac
19666 65 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 42 79 74 65  e..**.** *pnByte
19667 20 69 73 20 61 20 63 6f 75 6e 74 65 72 20 6f 66   is a counter of
19668 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
19669 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 74 68  ytes of space th
1966a 61 74 20 68 61 76 65 20 66 61 69 6c 65 64 0a 2a  at have failed.*
1966b 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2e 20 20  * to allocate.  
1966c 49 66 20 74 68 65 72 65 20 69 73 20 69 6e 73 75  If there is insu
1966d 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 69  fficient space i
1966e 6e 20 2a 70 70 46 72 6f 6d 20 74 6f 20 73 61 74  n *ppFrom to sat
1966f 69 73 66 79 20 74 68 65 0a 2a 2a 20 72 65 71 75  isfy the.** requ
19670 65 73 74 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d  est, then increm
19671 65 6e 74 20 2a 70 6e 42 79 74 65 20 62 79 20 74  ent *pnByte by t
19672 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65  he amount of the
19673 20 72 65 71 75 65 73 74 2e 0a 2a 2f 0a 73 74 61   request..*/.sta
19674 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 53 70  tic void allocSp
19675 61 63 65 28 0a 20 20 63 68 61 72 20 2a 70 70 2c  ace(.  char *pp,
19676 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
19677 4e 2f 4f 55 54 3a 20 53 65 74 20 2a 70 70 20 74  N/OUT: Set *pp t
19678 6f 20 70 6f 69 6e 74 20 74 6f 20 61 6c 6c 6f 63  o point to alloc
19679 61 74 65 64 20 62 75 66 66 65 72 20 2a 2f 0a 20  ated buffer */. 
1967a 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20   int nByte,     
1967b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1967c 6f 66 20 62 79 74 65 73 20 74 6f 20 61 6c 6c 6f  of bytes to allo
1967d 63 61 74 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 70  cate */.  u8 **p
1967e 70 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 2f  pFrom,         /
1967f 2a 20 49 4e 2f 4f 55 54 3a 20 41 6c 6c 6f 63 61  * IN/OUT: Alloca
19680 74 65 20 66 72 6f 6d 20 2a 70 70 46 72 6f 6d 20  te from *ppFrom 
19681 2a 2f 0a 20 20 75 38 20 2a 70 45 6e 64 2c 20 20  */.  u8 *pEnd,  
19682 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
19683 6e 74 65 72 20 74 6f 20 31 20 62 79 74 65 20 70  nter to 1 byte p
19684 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 2a  ast the end of *
19685 70 70 46 72 6f 6d 20 62 75 66 66 65 72 20 2a 2f  ppFrom buffer */
19686 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 20 20  .  int *pnByte  
19687 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6c          /* If al
19688 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  location cannot 
19689 62 65 20 6d 61 64 65 2c 20 69 6e 63 72 65 6d 65  be made, increme
1968a 6e 74 20 2a 70 6e 42 79 74 65 20 2a 2f 0a 29 7b  nt *pnByte */.){
1968b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54  .  assert( EIGHT
1968c 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
1968d 2a 70 70 46 72 6f 6d 29 20 29 3b 0a 20 20 69 66  *ppFrom) );.  if
1968e 28 20 28 2a 28 76 6f 69 64 2a 2a 29 70 70 29 3d  ( (*(void**)pp)=
1968f 3d 30 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20  =0 ){.    nByte 
19690 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b  = ROUND8(nByte);
19691 0a 20 20 20 20 69 66 28 20 28 70 45 6e 64 20 2d  .    if( (pEnd -
19692 20 2a 70 70 46 72 6f 6d 29 3e 3d 6e 42 79 74 65   *ppFrom)>=nByte
19693 20 29 7b 0a 20 20 20 20 20 20 2a 28 76 6f 69 64   ){.      *(void
19694 2a 2a 29 70 70 20 3d 20 28 76 6f 69 64 20 2a 29  **)pp = (void *)
19695 2a 70 70 46 72 6f 6d 3b 0a 20 20 20 20 20 20 2a  *ppFrom;.      *
19696 70 70 46 72 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b  ppFrom += nByte;
19697 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
19698 20 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79    *pnByte += nBy
19699 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  te;.    }.  }.}.
1969a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61  ./*.** Prepare a
1969b 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1969c 20 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 2e 20   for execution. 
1969d 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 74   This involves t
1969e 68 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61 73  hings such.** as
1969f 20 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 63   allocating stac
196a0 6b 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69 74  k space and init
196a1 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 6f  ializing the pro
196a2 67 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a  gram counter..**
196a3 20 41 66 74 65 72 20 74 68 65 20 56 44 42 45 20   After the VDBE 
196a4 68 61 73 20 62 65 20 70 72 65 70 70 65 64 2c 20  has be prepped, 
196a5 69 74 20 63 61 6e 20 62 65 20 65 78 65 63 75 74  it can be execut
196a6 65 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72  ed by one or mor
196a7 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71  e.** calls to sq
196a8 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e  lite3VdbeExec().
196a9 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73    .**.** This is
196aa 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f   the only way to
196ab 20 6d 6f 76 65 20 61 20 56 44 42 45 20 66 72 6f   move a VDBE fro
196ac 6d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  m VDBE_MAGIC_INI
196ad 54 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47  T to.** VDBE_MAG
196ae 49 43 5f 52 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68  IC_RUN..**.** Th
196af 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20  is function may 
196b0 62 65 20 63 61 6c 6c 65 64 20 6d 6f 72 65 20 74  be called more t
196b1 68 61 6e 20 6f 6e 63 65 20 6f 6e 20 61 20 73 69  han once on a si
196b2 6e 67 6c 65 20 76 69 72 74 75 61 6c 20 6d 61 63  ngle virtual mac
196b3 68 69 6e 65 2e 0a 2a 2a 20 54 68 65 20 66 69 72  hine..** The fir
196b4 73 74 20 63 61 6c 6c 20 69 73 20 6d 61 64 65 20  st call is made 
196b5 77 68 69 6c 65 20 63 6f 6d 70 69 6c 69 6e 67 20  while compiling 
196b6 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
196b7 74 2e 20 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a  t. Subsequent.**
196b8 20 63 61 6c 6c 73 20 61 72 65 20 6d 61 64 65 20   calls are made 
196b9 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 70  as part of the p
196ba 72 6f 63 65 73 73 20 6f 66 20 72 65 73 65 74 74  rocess of resett
196bb 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20  ing a statement 
196bc 74 6f 20 62 65 0a 2a 2a 20 72 65 2d 65 78 65 63  to be.** re-exec
196bd 75 74 65 64 20 28 66 72 6f 6d 20 61 20 63 61 6c  uted (from a cal
196be 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73  l to sqlite3_res
196bf 65 74 28 29 29 2e 20 54 68 65 20 6e 56 61 72 2c  et()). The nVar,
196c0 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 0a   nMem, nCursor .
196c1 2a 2a 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e  ** and isExplain
196c2 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20   parameters are 
196c3 6f 6e 6c 79 20 70 61 73 73 65 64 20 63 6f 72 72  only passed corr
196c4 65 63 74 20 76 61 6c 75 65 73 20 74 68 65 20 66  ect values the f
196c5 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 65  irst time.** the
196c6 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
196c7 6c 65 64 2e 20 4f 6e 20 73 75 62 73 65 71 75 65  led. On subseque
196c8 6e 74 20 63 61 6c 6c 73 2c 20 66 72 6f 6d 20 73  nt calls, from s
196c9 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 20  qlite3_reset(), 
196ca 6e 56 61 72 0a 2a 2a 20 69 73 20 70 61 73 73 65  nVar.** is passe
196cb 64 20 2d 31 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e  d -1 and nMem, n
196cc 43 75 72 73 6f 72 20 61 6e 64 20 69 73 45 78 70  Cursor and isExp
196cd 6c 61 69 6e 20 61 72 65 20 61 6c 6c 20 70 61 73  lain are all pas
196ce 73 65 64 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c  sed zero..*/.SQL
196cf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
196d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
196d1 52 65 61 64 79 28 0a 20 20 56 64 62 65 20 2a 70  Ready(.  Vdbe *p
196d2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
196d3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
196d4 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61  DBE */.  int nVa
196d5 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
196d6 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
196d7 72 20 6f 66 20 27 3f 27 20 73 65 65 20 69 6e 20  r of '?' see in 
196d8 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
196d9 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c  t */.  int nMem,
196da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
196db 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
196dc 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  of memory cells 
196dd 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20  to allocate */. 
196de 20 69 6e 74 20 6e 43 75 72 73 6f 72 2c 20 20 20   int nCursor,   
196df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
196e0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72  /* Number of cur
196e1 73 6f 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65  sors to allocate
196e2 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 78 70 6c   */.  int isExpl
196e3 61 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ain             
196e4 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
196e5 74 68 65 20 45 58 50 4c 41 49 4e 20 6b 65 79 77  the EXPLAIN keyw
196e6 6f 72 64 73 20 69 73 20 70 72 65 73 65 6e 74 20  ords is present 
196e7 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20  */.){.  int n;. 
196e8 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
196e9 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  ->db;..  assert(
196ea 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   p!=0 );.  asser
196eb 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
196ec 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
196ed 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75  .  /* There shou
196ee 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f  ld be at least o
196ef 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a  ne opcode..  */.
196f0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70    assert( p->nOp
196f1 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  >0 );..  /* Set 
196f2 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42  the magic to VDB
196f3 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e  E_MAGIC_RUN soon
196f4 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c  er rather than l
196f5 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61  ater. */.  p->ma
196f6 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43  gic = VDBE_MAGIC
196f7 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20  _RUN;..  /* For 
196f8 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 71 75  each cursor requ
196f9 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63  ired, also alloc
196fa 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c  ate a memory cel
196fb 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63  l. Memory.  ** c
196fc 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75  ells (nMem+1-nCu
196fd 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63  rsor)..nMem, inc
196fe 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76  lusive, will nev
196ff 65 72 20 62 65 20 75 73 65 64 20 62 79 0a 20 20  er be used by.  
19700 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  ** the vdbe prog
19701 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 65  ram. Instead the
19702 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 6c  y are used to al
19703 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
19704 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72  .  ** VdbeCursor
19705 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 63 74  /BtCursor struct
19706 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f  ures. The blob o
19707 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  f memory associa
19708 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 63  ted with .  ** c
19709 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f 72 65  ursor 0 is store
1970a 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  d in memory cell
1970b 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65   nMem. Memory ce
1970c 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a  ll (nMem-1).  **
1970d 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c 6f 62   stores the blob
1970e 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63   of memory assoc
1970f 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f  iated with curso
19710 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20  r 1, etc..  **. 
19711 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c   ** See also: al
19712 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0a  locateCursor()..
19713 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e    */.  nMem += n
19714 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c  Cursor;..  /* Al
19715 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
19716 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72   memory register
19717 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73  s, SQL variables
19718 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 61  , VDBE cursors a
19719 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61  nd .  ** an arra
1971a 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c  y to marshal SQL
1971b 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
1971c 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 73 20  nts in. This is 
1971d 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 20  only done the.  
1971e 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  ** first time th
1971f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
19720 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 76 65  alled for a give
19721 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e  n VDBE, not when
19722 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e   it is.  ** bein
19723 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71  g called from sq
19724 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 74 6f  lite3_reset() to
19725 20 72 65 73 65 74 20 74 68 65 20 76 69 72 74 75   reset the virtu
19726 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f  al machine..  */
19727 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 26  .  if( nVar>=0 &
19728 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  & !db->mallocFai
19729 6c 65 64 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a  led ){.    u8 *z
1972a 43 73 72 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e  Csr = (u8 *)&p->
1972b 61 4f 70 5b 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 20  aOp[p->nOp];.   
1972c 20 75 38 20 2a 7a 45 6e 64 20 3d 20 28 75 38 20   u8 *zEnd = (u8 
1972d 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70  *)&p->aOp[p->nOp
1972e 41 6c 6c 6f 63 5d 3b 0a 20 20 20 20 69 6e 74 20  Alloc];.    int 
1972f 6e 42 79 74 65 3b 0a 20 20 20 20 69 6e 74 20 6e  nByte;.    int n
19730 41 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61  Arg;       /* Ma
19731 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
19732 61 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61  args passed to a
19733 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20   user function. 
19734 2a 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32  */.    resolveP2
19735 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29  Values(p, &nArg)
19736 3b 0a 20 20 20 20 69 66 28 20 69 73 45 78 70 6c  ;.    if( isExpl
19737 61 69 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29  ain && nMem<10 )
19738 7b 0a 20 20 20 20 20 20 6e 4d 65 6d 20 3d 20 31  {.      nMem = 1
19739 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 73  0;.    }.    zCs
1973a 72 20 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75 38  r += (zCsr - (u8
1973b 2a 29 30 29 26 37 3b 0a 20 20 20 20 61 73 73 65  *)0)&7;.    asse
1973c 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
1973d 4c 49 47 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29  LIGNMENT(zCsr) )
1973e 3b 0a 20 20 20 20 69 66 28 20 7a 45 6e 64 3c 7a  ;.    if( zEnd<z
1973f 43 73 72 20 29 20 7a 45 6e 64 20 3d 20 7a 43 73  Csr ) zEnd = zCs
19740 72 3b 0a 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20  r;..    do {.   
19741 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 2c 20     memset(zCsr, 
19742 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b 0a 20  0, zEnd-zCsr);. 
19743 20 20 20 20 20 6e 42 79 74 65 20 3d 20 30 3b 0a       nByte = 0;.
19744 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65        allocSpace
19745 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 4d 65 6d  ((char*)&p->aMem
19746 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d 65  , nMem*sizeof(Me
19747 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c  m), &zCsr, zEnd,
19748 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20   &nByte);.      
19749 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72  allocSpace((char
1974a 2a 29 26 70 2d 3e 61 56 61 72 2c 20 6e 56 61 72  *)&p->aVar, nVar
1974b 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a  *sizeof(Mem), &z
1974c 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74  Csr, zEnd, &nByt
1974d 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53  e);.      allocS
1974e 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e  pace((char*)&p->
1974f 61 70 41 72 67 2c 20 6e 41 72 67 2a 73 69 7a 65  apArg, nArg*size
19750 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a 43 73 72 2c  of(Mem*), &zCsr,
19751 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a   zEnd, &nByte);.
19752 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65        allocSpace
19753 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 7a 56 61  ((char*)&p->azVa
19754 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 28 63  r, nVar*sizeof(c
19755 68 61 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45  har*), &zCsr, zE
19756 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20  nd, &nByte);.   
19757 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63     allocSpace((c
19758 68 61 72 2a 29 26 70 2d 3e 61 70 43 73 72 2c 20  har*)&p->apCsr, 
19759 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1975a 20 20 6e 43 75 72 73 6f 72 2a 73 69 7a 65 6f 66    nCursor*sizeof
1975b 28 56 64 62 65 43 75 72 73 6f 72 2a 29 2c 20 26  (VdbeCursor*), &
1975c 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79  zCsr, zEnd, &nBy
1975d 74 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  te.      );.    
1975e 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a 20    if( nByte ){. 
1975f 20 20 20 20 20 20 20 70 2d 3e 70 46 72 65 65 20         p->pFree 
19760 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
19761 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 29 3b  cRaw(db, nByte);
19762 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
19763 43 73 72 20 3d 20 70 2d 3e 70 46 72 65 65 3b 0a  Csr = p->pFree;.
19764 20 20 20 20 20 20 7a 45 6e 64 20 3d 20 26 7a 43        zEnd = &zC
19765 73 72 5b 6e 42 79 74 65 5d 3b 0a 20 20 20 20 7d  sr[nByte];.    }
19766 77 68 69 6c 65 28 20 6e 42 79 74 65 20 26 26 20  while( nByte && 
19767 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
19768 64 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 6e 43 75  d );..    p->nCu
19769 72 73 6f 72 20 3d 20 6e 43 75 72 73 6f 72 3b 0a  rsor = nCursor;.
1976a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20      if( p->aVar 
1976b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72  ){.      p->nVar
1976c 20 3d 20 6e 56 61 72 3b 0a 20 20 20 20 20 20 66   = nVar;.      f
1976d 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20  or(n=0; n<nVar; 
1976e 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  n++){.        p-
1976f 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d  >aVar[n].flags =
19770 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20   MEM_Null;.     
19771 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62     p->aVar[n].db
19772 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20   = db;.      }. 
19773 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e     }.    if( p->
19774 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 2d  aMem ){.      p-
19775 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20  >aMem--;        
19776 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19777 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72 6f   aMem[] goes fro
19778 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 20 20  m 1..nMem */.   
19779 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d 65     p->nMem = nMe
1977a 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
1977b 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20     /*       not 
1977c 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a  from 0..nMem-1 *
1977d 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 31 3b  /.      for(n=1;
1977e 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a   n<=nMem; n++){.
1977f 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b          p->aMem[
19780 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  n].flags = MEM_N
19781 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ull;.        p->
19782 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b  aMem[n].db = db;
19783 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
19784 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
19785 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d 31  _DEBUG.  for(n=1
19786 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b  ; n<p->nMem; n++
19787 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
19788 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62  ->aMem[n].db==db
19789 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a   );.  }.#endif..
1978a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20    p->pc = -1;.  
1978b 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  p->rc = SQLITE_O
1978c 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74  K;.  p->errorAct
1978d 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ion = OE_Abort;.
1978e 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d 20    p->explain |= 
1978f 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d 3e  isExplain;.  p->
19790 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47  magic = VDBE_MAG
19791 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43 68  IC_RUN;.  p->nCh
19792 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 63  ange = 0;.  p->c
19793 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20 70  acheCtr = 1;.  p
19794 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f  ->minWriteFileFo
19795 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70 2d  rmat = 255;.  p-
19796 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b  >iStatement = 0;
19797 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
19798 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e 74  FILE.  {.    int
19799 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   i;.    for(i=0;
1979a 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
1979b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d  .      p->aOp[i]
1979c 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  .cnt = 0;.      
1979d 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73  p->aOp[i].cycles
1979e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
1979f 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
197a0 43 6c 6f 73 65 20 61 20 56 44 42 45 20 63 75 72  Close a VDBE cur
197a1 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 73 65 20  sor and release 
197a2 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 72 63 65  all the resource
197a3 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 0a 2a  s that cursor .*
197a4 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 68 6f 6c  * happens to hol
197a5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
197a6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
197a7 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28  3VdbeFreeCursor(
197a8 56 64 62 65 20 2a 70 2c 20 56 64 62 65 43 75 72  Vdbe *p, VdbeCur
197a9 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69 66 28  sor *pCx){.  if(
197aa 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72   pCx==0 ){.    r
197ab 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
197ac 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20 20   pCx->pBt ){.   
197ad 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
197ae 73 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20  se(pCx->pBt);.  
197af 20 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70 43    /* The pCx->pC
197b0 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 63 6c  ursor will be cl
197b1 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ose automaticall
197b2 79 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2c  y, if it exists,
197b3 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 63   by.    ** the c
197b4 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20  all above. */.  
197b5 7d 65 6c 73 65 20 69 66 28 20 70 43 78 2d 3e 70  }else if( pCx->p
197b6 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71  Cursor ){.    sq
197b7 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43  lite3BtreeCloseC
197b8 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 72 73  ursor(pCx->pCurs
197b9 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  or);.  }.#ifndef
197ba 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
197bb 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
197bc 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  pCx->pVtabCursor
197bd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
197be 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74  vtab_cursor *pVt
197bf 61 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d 3e  abCursor = pCx->
197c0 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20  pVtabCursor;.   
197c1 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
197c2 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d  odule *pModule =
197c3 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20   pCx->pModule;. 
197c4 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68     p->inVtabMeth
197c5 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 76 6f 69  od = 1;.    (voi
197c6 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
197c7 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 70  ff(p->db);.    p
197c8 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70  Module->xClose(p
197c9 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20  VtabCursor);.   
197ca 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
197cb 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3b 0a 20  fetyOn(p->db);. 
197cc 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68     p->inVtabMeth
197cd 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64  od = 0;.  }.#end
197ce 69 66 0a 20 20 69 66 28 20 21 70 43 78 2d 3e 65  if.  if( !pCx->e
197cf 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20  phemPseudoTable 
197d0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
197d1 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 78 2d  Free(p->db, pCx-
197d2 3e 70 44 61 74 61 29 3b 0a 20 20 7d 0a 7d 0a 0a  >pData);.  }.}..
197d3 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20  /*.** Close all 
197d4 63 75 72 73 6f 72 73 20 65 78 63 65 70 74 20 66  cursors except f
197d5 6f 72 20 56 54 61 62 20 63 75 72 73 6f 72 73 20  or VTab cursors 
197d6 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74  that are current
197d7 6c 79 0a 2a 2a 20 69 6e 20 75 73 65 2e 0a 2a 2f  ly.** in use..*/
197d8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f  .static void clo
197d9 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65  seAllCursorsExce
197da 70 74 41 63 74 69 76 65 56 74 61 62 73 28 56 64  ptActiveVtabs(Vd
197db 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  be *p){.  int i;
197dc 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 3d  .  if( p->apCsr=
197dd 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  =0 ) return;.  f
197de 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75  or(i=0; i<p->nCu
197df 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  rsor; i++){.    
197e0 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d  VdbeCursor *pC =
197e1 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20   p->apCsr[i];.  
197e2 20 20 69 66 28 20 70 43 20 26 26 20 28 21 70 2d    if( pC && (!p-
197e3 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 7c 7c  >inVtabMethod ||
197e4 20 21 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f   !pC->pVtabCurso
197e5 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  r) ){.      sqli
197e6 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f  te3VdbeFreeCurso
197e7 72 28 70 2c 20 70 43 29 3b 0a 20 20 20 20 20 20  r(p, pC);.      
197e8 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b  p->apCsr[i] = 0;
197e9 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
197ea 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74 68 65  .** Clean up the
197eb 20 56 4d 20 61 66 74 65 72 20 65 78 65 63 75 74   VM after execut
197ec 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
197ed 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 75 74  routine will aut
197ee 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65  omatically close
197ef 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20 6c 69   any cursors, li
197f0 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73  sts, and/or.** s
197f1 6f 72 74 65 72 73 20 74 68 61 74 20 77 65 72 65  orters that were
197f2 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49 74 20   left open.  It 
197f3 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74 68 65  also deletes the
197f4 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 76 61   values of.** va
197f5 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 20 61  riables in the a
197f6 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a  Var[] array..*/.
197f7 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c 65 61  static void Clea
197f8 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  nup(Vdbe *p){.  
197f9 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33  int i;.  sqlite3
197fa 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20   *db = p->db;.  
197fb 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 63 6c 6f  Mem *pMem;.  clo
197fc 73 65 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65  seAllCursorsExce
197fd 70 74 41 63 74 69 76 65 56 74 61 62 73 28 70 29  ptActiveVtabs(p)
197fe 3b 0a 20 20 66 6f 72 28 70 4d 65 6d 3d 26 70 2d  ;.  for(pMem=&p-
197ff 3e 61 4d 65 6d 5b 31 5d 2c 20 69 3d 31 3b 20 69  >aMem[1], i=1; i
19800 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b 2c 20  <=p->nMem; i++, 
19801 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  pMem++){.    if(
19802 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
19803 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 20  EM_RowSet ){.   
19804 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74     sqlite3RowSet
19805 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 52  Clear(pMem->u.pR
19806 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 20  owSet);.    }.  
19807 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
19808 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  (pMem, MEM_Null)
19809 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d  ;.  }.  releaseM
1980a 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d  emArray(&p->aMem
1980b 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20  [1], p->nMem);. 
1980c 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53   if( p->contextS
1980d 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69  tack ){.    sqli
1980e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1980f 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 29 3b 0a  >contextStack);.
19810 20 20 7d 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 74    }.  p->context
19811 53 74 61 63 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e  Stack = 0;.  p->
19812 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70 74  contextStackDept
19813 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74  h = 0;.  p->cont
19814 65 78 74 53 74 61 63 6b 54 6f 70 20 3d 20 30 3b  extStackTop = 0;
19815 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
19816 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
19817 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d  ;.  p->zErrMsg =
19818 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74   0;.  p->pResult
19819 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Set = 0;.}../*.*
1981a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72  * Set the number
1981b 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
1981c 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ns that will be 
1981d 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73  returned by this
1981e 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e   SQL.** statemen
1981f 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 20 73  t. This is now s
19820 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  et at compile ti
19821 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61 6e 20  me, rather than 
19822 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75 74  during.** execut
19823 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 65 20  ion of the vdbe 
19824 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 74 20  program so that 
19825 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
19826 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62 65  ount() can.** be
19827 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53 51   called on an SQ
19828 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 66 6f  L statement befo
19829 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  re sqlite3_step(
1982a 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
1982b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1982c 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
1982d 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52 65  Vdbe *p, int nRe
1982e 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d 20  sColumn){.  Mem 
1982f 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e 74  *pColName;.  int
19830 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   n;.  sqlite3 *d
19831 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 72 65  b = p->db;..  re
19832 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d  leaseMemArray(p-
19833 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52  >aColName, p->nR
19834 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45  esColumn*COLNAME
19835 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  _N);.  sqlite3Db
19836 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f 6c  Free(db, p->aCol
19837 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e 52 65  Name);.  n = nRe
19838 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f  sColumn*COLNAME_
19839 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75  N;.  p->nResColu
1983a 6d 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b  mn = nResColumn;
1983b 0a 20 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 20 3d  .  p->aColName =
1983c 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 28 4d 65 6d   pColName = (Mem
1983d 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
1983e 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
1983f 28 4d 65 6d 29 2a 6e 20 29 3b 0a 20 20 69 66 28  (Mem)*n );.  if(
19840 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 3d 3d 30 20   p->aColName==0 
19841 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c  ) return;.  whil
19842 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20 20  e( n-- > 0 ){.  
19843 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67    pColName->flag
19844 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
19845 20 20 70 43 6f 6c 4e 61 6d 65 2d 3e 64 62 20 3d    pColName->db =
19846 20 70 2d 3e 64 62 3b 0a 20 20 20 20 70 43 6f 6c   p->db;.    pCol
19847 4e 61 6d 65 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Name++;.  }.}../
19848 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 61 6d  *.** Set the nam
19849 65 20 6f 66 20 74 68 65 20 69 64 78 27 74 68 20  e of the idx'th 
1984a 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 72 65 74  column to be ret
1984b 75 72 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c  urned by the SQL
1984c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 7a   statement..** z
1984d 4e 61 6d 65 20 6d 75 73 74 20 62 65 20 61 20 70  Name must be a p
1984e 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 75 6c 20  ointer to a nul 
1984f 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
19850 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61  g..**.** This ca
19851 6c 6c 20 6d 75 73 74 20 62 65 20 6d 61 64 65 20  ll must be made 
19852 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20  after a call to 
19853 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
19854 6d 43 6f 6c 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  mCols()..**.** T
19855 68 65 20 66 69 6e 61 6c 20 70 61 72 61 6d 65 74  he final paramet
19856 65 72 2c 20 78 44 65 6c 2c 20 6d 75 73 74 20 62  er, xDel, must b
19857 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  e one of SQLITE_
19858 44 59 4e 41 4d 49 43 2c 20 53 51 4c 49 54 45 5f  DYNAMIC, SQLITE_
19859 53 54 41 54 49 43 0a 2a 2a 20 6f 72 20 53 51 4c  STATIC.** or SQL
1985a 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 20 49  ITE_TRANSIENT. I
1985b 66 20 69 74 20 69 73 20 53 51 4c 49 54 45 5f 44  f it is SQLITE_D
1985c 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 74 68 65  YNAMIC, then the
1985d 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 0a   buffer pointed.
1985e 2a 2a 20 74 6f 20 62 79 20 7a 4e 61 6d 65 20 77  ** to by zName w
1985f 69 6c 6c 20 62 65 20 66 72 65 65 64 20 62 79 20  ill be freed by 
19860 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20  sqlite3DbFree() 
19861 77 68 65 6e 20 74 68 65 20 76 64 62 65 20 69 73  when the vdbe is
19862 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 53   destroyed..*/.S
19863 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19864 74 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  t sqlite3VdbeSet
19865 43 6f 6c 4e 61 6d 65 28 0a 20 20 56 64 62 65 20  ColName(.  Vdbe 
19866 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
19867 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
19868 64 62 65 20 62 65 69 6e 67 20 63 6f 6e 66 69 67  dbe being config
19869 75 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64  ured */.  int id
1986a 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
1986b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1986c 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 7a 4e  dex of column zN
1986d 61 6d 65 20 61 70 70 6c 69 65 73 20 74 6f 20 2a  ame applies to *
1986e 2f 0a 20 20 69 6e 74 20 76 61 72 2c 20 20 20 20  /.  int var,    
1986f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19870 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
19871 68 65 20 43 4f 4c 4e 41 4d 45 5f 2a 20 63 6f 6e  he COLNAME_* con
19872 73 74 61 6e 74 73 20 2a 2f 0a 20 20 63 6f 6e 73  stants */.  cons
19873 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
19874 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19875 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65  Pointer to buffe
19876 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 61 6d  r containing nam
19877 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44  e */.  void (*xD
19878 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 20 20  el)(void*)      
19879 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
1987a 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20 73 74 72  y management str
1987b 61 74 65 67 79 20 66 6f 72 20 7a 4e 61 6d 65 20  ategy for zName 
1987c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
1987d 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b    Mem *pColName;
1987e 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70  .  assert( idx<p
1987f 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 29 3b 0a  ->nResColumn );.
19880 20 20 61 73 73 65 72 74 28 20 76 61 72 3c 43 4f    assert( var<CO
19881 4c 4e 41 4d 45 5f 4e 20 29 3b 0a 20 20 69 66 28  LNAME_N );.  if(
19882 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
19883 69 6c 65 64 20 29 7b 0a 20 20 20 20 61 73 73 65  iled ){.    asse
19884 72 74 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 78 44  rt( !zName || xD
19885 65 6c 21 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d  el!=SQLITE_DYNAM
19886 49 43 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  IC );.    return
19887 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
19888 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   }.  assert( p->
19889 61 43 6f 6c 4e 61 6d 65 21 3d 30 20 29 3b 0a 20  aColName!=0 );. 
1988a 20 70 43 6f 6c 4e 61 6d 65 20 3d 20 26 28 70 2d   pColName = &(p-
1988b 3e 61 43 6f 6c 4e 61 6d 65 5b 69 64 78 2b 76 61  >aColName[idx+va
1988c 72 2a 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 5d  r*p->nResColumn]
1988d 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1988e 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
1988f 43 6f 6c 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20  ColName, zName, 
19890 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  -1, SQLITE_UTF8,
19891 20 78 44 65 6c 29 3b 0a 20 20 61 73 73 65 72 74   xDel);.  assert
19892 28 20 72 63 21 3d 30 20 7c 7c 20 21 7a 4e 61 6d  ( rc!=0 || !zNam
19893 65 20 7c 7c 20 28 70 43 6f 6c 4e 61 6d 65 2d 3e  e || (pColName->
19894 66 6c 61 67 73 26 4d 45 4d 5f 54 65 72 6d 29 21  flags&MEM_Term)!
19895 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  =0 );.  return r
19896 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 65  c;.}../*.** A re
19897 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e  ad or write tran
19898 73 61 63 74 69 6f 6e 20 6d 61 79 20 6f 72 20 6d  saction may or m
19899 61 79 20 6e 6f 74 20 62 65 20 61 63 74 69 76 65  ay not be active
1989a 20 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e   on database han
1989b 64 6c 65 0a 2a 2a 20 64 62 2e 20 49 66 20 61 20  dle.** db. If a 
1989c 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
1989d 63 74 69 76 65 2c 20 63 6f 6d 6d 69 74 20 69 74  ctive, commit it
1989e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 0a  . If there is a.
1989f 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  ** write-transac
198a0 74 69 6f 6e 20 73 70 61 6e 6e 69 6e 67 20 6d 6f  tion spanning mo
198a1 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61  re than one data
198a2 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20  base file, this 
198a3 72 6f 75 74 69 6e 65 0a 2a 2a 20 74 61 6b 65 73  routine.** takes
198a4 20 63 61 72 65 20 6f 66 20 74 68 65 20 6d 61 73   care of the mas
198a5 74 65 72 20 6a 6f 75 72 6e 61 6c 20 74 72 69 63  ter journal tric
198a6 6b 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  kery..*/.static 
198a7 69 6e 74 20 76 64 62 65 43 6f 6d 6d 69 74 28 73  int vdbeCommit(s
198a8 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 64 62 65  qlite3 *db, Vdbe
198a9 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
198aa 20 69 6e 74 20 6e 54 72 61 6e 73 20 3d 20 30 3b   int nTrans = 0;
198ab 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64    /* Number of d
198ac 61 74 61 62 61 73 65 73 20 77 69 74 68 20 61 6e  atabases with an
198ad 20 61 63 74 69 76 65 20 77 72 69 74 65 2d 74 72   active write-tr
198ae 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 69  ansaction */.  i
198af 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
198b0 4b 3b 0a 20 20 69 6e 74 20 6e 65 65 64 58 63 6f  K;.  int needXco
198b1 6d 6d 69 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  mmit = 0;..  /* 
198b2 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79  Before doing any
198b3 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c 6c  thing else, call
198b4 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 61 6c   the xSync() cal
198b5 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 20  lback for any.  
198b6 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c  ** virtual modul
198b7 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 65 6e  e tables written
198b8 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63   in this transac
198b9 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 74  tion. This has t
198ba 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 62  o.  ** be done b
198bb 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 6e  efore determinin
198bc 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 73 74  g whether a mast
198bd 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
198be 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 65  is .  ** require
198bf 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 28 29  d, as an xSync()
198c0 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 64   callback may ad
198c1 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  d an attached da
198c2 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 74  tabase.  ** to t
198c3 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  he transaction..
198c4 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
198c5 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 2c 20  te3VtabSync(db, 
198c6 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  &p->zErrMsg);.  
198c7 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
198c8 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
198c9 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  rc;.  }..  /* Th
198ca 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69 6e  is loop determin
198cb 65 73 20 28 61 29 20 69 66 20 74 68 65 20 63 6f  es (a) if the co
198cc 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c 64  mmit hook should
198cd 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64 0a   be invoked and.
198ce 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61 6e    ** (b) how man
198cf 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  y database files
198d0 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 74 65   have open write
198d1 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20 6e   transactions, n
198d2 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64 69  ot .  ** includi
198d3 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  ng the temp data
198d4 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 6d 70  base. (b) is imp
198d5 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 20 69  ortant because i
198d6 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20 2a  f more than .  *
198d7 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66  * one database f
198d8 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 6e 20  ile has an open 
198d9 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
198da 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  n, a master jour
198db 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 73  nal.  ** file is
198dc 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 6e   required for an
198dd 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e 0a   atomic commit..
198de 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30 3b    */ .  for(i=0;
198df 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
198e0 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42  { .    Btree *pB
198e1 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
198e2 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  Bt;.    if( sqli
198e3 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
198e4 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  s(pBt) ){.      
198e5 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 3b  needXcommit = 1;
198e6 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 31 20  .      if( i!=1 
198e7 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20 20  ) nTrans++;.    
198e8 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
198e9 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 72 69  here are any wri
198ea 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  te-transactions 
198eb 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74  at all, invoke t
198ec 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a  he commit hook *
198ed 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d  /.  if( needXcom
198ee 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d  mit && db->xComm
198ef 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20  itCallback ){.  
198f0 20 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e 66    assert( (db->f
198f1 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f  lags & SQLITE_Co
198f2 6d 6d 69 74 42 75 73 79 29 3d 3d 30 20 29 3b 0a  mmitBusy)==0 );.
198f3 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
198f4 20 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 74 42 75   SQLITE_CommitBu
198f5 73 79 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  sy;.    (void)sq
198f6 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
198f7 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 2d  b);.    rc = db-
198f8 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b  >xCommitCallback
198f9 28 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29  (db->pCommitArg)
198fa 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
198fb 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
198fc 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  .    db->flags &
198fd 3d 20 7e 53 51 4c 49 54 45 5f 43 6f 6d 6d 69 74  = ~SQLITE_Commit
198fe 42 75 73 79 3b 0a 20 20 20 20 69 66 28 20 72 63  Busy;.    if( rc
198ff 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19900 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
19901 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  NT;.    }.  }.. 
19902 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 63   /* The simple c
19903 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68  ase - no more th
19904 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  an one database 
19905 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69  file (not counti
19906 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d 50  ng the.  ** TEMP
19907 20 64 61 74 61 62 61 73 65 29 20 68 61 73 20 61   database) has a
19908 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74   transaction act
19909 69 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 20  ive.   There is 
1990a 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0a  no need for the.
1990b 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72    ** master-jour
1990c 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  nal..  **.  ** I
1990d 66 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  f the return val
1990e 75 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72  ue of sqlite3Btr
1990f 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 20  eeGetFilename() 
19910 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 68  is a zero length
19911 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 74  .  ** string, it
19912 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20   means the main 
19913 64 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 6d  database is :mem
19914 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 66  ory: or a temp f
19915 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 74  ile.  In .  ** t
19916 68 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 6e  hat case we do n
19917 6f 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d 69  ot support atomi
19918 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f 6d  c multi-file com
19919 6d 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 65  mits, so use the
1991a 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 61   .  ** simple ca
1991b 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 2a  se then too..  *
1991c 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74  /.  if( 0==sqlit
1991d 65 33 53 74 72 6c 65 6e 33 30 28 73 71 6c 69 74  e3Strlen30(sqlit
1991e 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
1991f 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42  me(db->aDb[0].pB
19920 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72 61 6e 73  t)).   || nTrans
19921 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72  <=1.  ){.    for
19922 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
19923 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62  _OK && i<db->nDb
19924 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74  ; i++){.      Bt
19925 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
19926 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
19927 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
19928 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
19929 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
1992a 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20 20 20  One(pBt, 0);.   
1992b 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
1992c 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d 69 74  /* Do the commit
1992d 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64 61 74   only if all dat
1992e 61 62 61 73 65 73 20 73 75 63 63 65 73 73 66 75  abases successfu
1992f 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70 68 61  lly complete pha
19930 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20 49 66  se 1. .    ** If
19931 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74 72 65   one of the Btre
19932 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  eCommitPhaseOne(
19933 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c 20 74  ) calls fails, t
19934 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 61 6e  his indicates an
19935 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72 6f 72  .    ** IO error
19936 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e 67 20   while deleting 
19937 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 61 20  or truncating a 
19938 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74  journal file. It
19939 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a 20 20   is unlikely,.  
1993a 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64 20 68    ** but could h
1993b 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73 20 63  appen. In this c
1993c 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63  ase abandon proc
1993d 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74 75 72  essing and retur
1993e 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20 20  n the error..   
1993f 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
19940 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
19941 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  & i<db->nDb; i++
19942 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a  ){.      Btree *
19943 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
19944 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20  .pBt;.      if( 
19945 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  pBt ){.        r
19946 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
19947 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
19948 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Bt);.      }.   
19949 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
1994a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1994b 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d    sqlite3VtabCom
1994c 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20  mit(db);.    }. 
1994d 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63 6f 6d   }..  /* The com
1994e 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68 65 72  plex case - Ther
1994f 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66 69 6c  e is a multi-fil
19950 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  e write-transact
19951 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20 2a 2a  ion active..  **
19952 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20 61   This requires a
19953 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
19954 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65 20 74  file to ensure t
19955 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
19956 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74 65 64  s.  ** committed
19957 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a 2f 0a   atomicly..  */.
19958 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19959 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65 6c 73  MIT_DISKIO.  els
1995a 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  e{.    sqlite3_v
1995b 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70  fs *pVfs = db->p
1995c 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e 65 65  Vfs;.    int nee
1995d 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 63  dSync = 0;.    c
1995e 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30  har *zMaster = 0
1995f 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61 6d 65  ;   /* File-name
19960 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65 72 20   for the master 
19961 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 63  journal */.    c
19962 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 69 6e  har const *zMain
19963 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74  File = sqlite3Bt
19964 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64  reeGetFilename(d
19965 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a  b->aDb[0].pBt);.
19966 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65      sqlite3_file
19967 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20   *pMaster = 0;. 
19968 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20     i64 offset = 
19969 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  0;.    int res;.
1996a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 61  .    /* Select a
1996b 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1996c 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  file name */.   
1996d 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33 32 20   do {.      u32 
1996e 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 20 73  iRandom;.      s
1996f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
19970 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20   zMaster);.     
19971 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
19972 65 73 73 28 73 69 7a 65 6f 66 28 69 52 61 6e 64  ess(sizeof(iRand
19973 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d 29 3b 0a  om), &iRandom);.
19974 20 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20        zMaster = 
19975 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
19976 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20  b, "%s-mj%08X", 
19977 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52 61 6e 64  zMainFile, iRand
19978 6f 6d 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a  om&0x7fffffff);.
19979 20 20 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74        if( !zMast
1997a 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  er ){.        re
1997b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
1997c 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
1997d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
1997e 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73  ccess(pVfs, zMas
1997f 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  ter, SQLITE_ACCE
19980 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29  SS_EXISTS, &res)
19981 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63  ;.    }while( rc
19982 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  ==SQLITE_OK && r
19983 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  es );.    if( rc
19984 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
19985 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65       /* Open the
19986 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e   master journal.
19987 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73   */.      rc = s
19988 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c  qlite3OsOpenMall
19989 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  oc(pVfs, zMaster
1998a 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20  , &pMaster, .   
1998b 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
1998c 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c  EN_READWRITE|SQL
1998d 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c  ITE_OPEN_CREATE|
1998e 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
1998f 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
19990 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  |SQLITE_OPEN_MAS
19991 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20  TER_JOURNAL, 0. 
19992 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20       );.    }.  
19993 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
19994 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
19995 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
19996 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72  Master);.      r
19997 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
19998 20 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74   .    /* Write t
19999 68 65 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20  he name of each 
1999a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
1999b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1999c 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20   into the new.  
1999d 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72    ** master jour
1999e 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20  nal file. If an 
1999f 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20  error occurs at 
199a0 74 68 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65  this point close
199a1 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65  .    ** and dele
199a2 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
199a3 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20  urnal file. All 
199a4 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a  the individual j
199a5 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20  ournal files.   
199a6 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27   ** still have '
199a7 6e 75 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73  null' as the mas
199a8 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e  ter journal poin
199a9 74 65 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c  ter, so they wil
199aa 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61  l roll.    ** ba
199ab 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79  ck independently
199ac 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63   if a failure oc
199ad 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  curs..    */.   
199ae 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
199af 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
199b0 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
199b1 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
199b2 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63      if( i==1 ) c
199b3 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 67  ontinue;   /* Ig
199b4 6e 6f 72 65 20 74 68 65 20 54 45 4d 50 20 64 61  nore the TEMP da
199b5 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20  tabase */.      
199b6 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  if( sqlite3Btree
199b7 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29  IsInTrans(pBt) )
199b8 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63  {.        char c
199b9 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71  onst *zFile = sq
199ba 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75  lite3BtreeGetJou
199bb 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a 20  rnalname(pBt);. 
199bc 20 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65         if( zFile
199bd 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  [0]==0 ) continu
199be 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a 6d  e;  /* Ignore :m
199bf 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73  emory: databases
199c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
199c1 21 6e 65 65 64 53 79 6e 63 20 26 26 20 21 73 71  !needSync && !sq
199c2 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69  lite3BtreeSyncDi
199c3 73 61 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20  sabled(pBt) ){. 
199c4 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e           needSyn
199c5 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  c = 1;.        }
199c6 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
199c7 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61  lite3OsWrite(pMa
199c8 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20 73 71 6c  ster, zFile, sql
199c9 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69  ite3Strlen30(zFi
199ca 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74 29 3b 0a  le)+1, offset);.
199cb 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2b          offset +
199cc 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
199cd 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20 20 20 20  0(zFile)+1;.    
199ce 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
199cf 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
199d0 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
199d1 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a  eFree(pMaster);.
199d2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
199d3 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20  3OsDelete(pVfs, 
199d4 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20  zMaster, 0);.   
199d5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
199d6 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  Free(db, zMaster
199d7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
199d8 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
199d9 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
199da 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65  .    /* Sync the
199db 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
199dc 66 69 6c 65 2e 20 49 66 20 74 68 65 20 49 4f 43  file. If the IOC
199dd 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 64 65  AP_SEQUENTIAL de
199de 76 69 63 65 0a 20 20 20 20 2a 2a 20 66 6c 61 67  vice.    ** flag
199df 20 69 73 20 73 65 74 20 74 68 69 73 20 69 73 20   is set this is 
199e0 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20  not required..  
199e1 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 65    */.    if( nee
199e2 64 53 79 6e 63 20 0a 20 20 20 20 20 26 26 20 30  dSync .     && 0
199e3 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69  ==(sqlite3OsDevi
199e4 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
199e5 73 28 70 4d 61 73 74 65 72 29 26 53 51 4c 49 54  s(pMaster)&SQLIT
199e6 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
199e7 41 4c 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49  AL).     && SQLI
199e8 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
199e9 69 74 65 33 4f 73 53 79 6e 63 28 70 4d 61 73 74  ite3OsSync(pMast
199ea 65 72 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  er, SQLITE_SYNC_
199eb 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20 29 7b 0a  NORMAL)).    ){.
199ec 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
199ed 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72  loseFree(pMaster
199ee 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
199ef 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a  OsDelete(pVfs, z
199f0 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20  Master, 0);.    
199f1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
199f2 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  db, zMaster);.  
199f3 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
199f4 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e     }..    /* Syn
199f5 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66 69 6c  c all the db fil
199f6 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74  es involved in t
199f7 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
199f8 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a 20 20  The same call.  
199f9 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20 6d 61    ** sets the ma
199fa 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69  ster journal poi
199fb 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69 6e 64  nter in each ind
199fc 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 2e  ividual journal.
199fd 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72   If.    ** an er
199fe 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72 65 2c  ror occurs here,
199ff 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74   do not delete t
19a00 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
19a01 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20  l file..    **. 
19a02 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65 72 72     ** If the err
19a03 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
19a04 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
19a05 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  to.    ** sqlite
19a06 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
19a07 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74 68 65  eOne(), then the
19a08 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20 74  re is a chance t
19a09 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 6d  hat the.    ** m
19a0a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
19a0b 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70 68 61  le will be orpha
19a0c 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61 6e 6e  ned. But we cann
19a0d 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a 20 20  ot delete it,.  
19a0e 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74 68 65    ** in case the
19a0f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
19a10 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20 77 72  file name was wr
19a11 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
19a12 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69  ournal.    ** fi
19a13 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20 66 61  le before the fa
19a14 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 2e 0a  ilure occurred..
19a15 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
19a16 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
19a17 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  K && i<db->nDb; 
19a18 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72  i++){ .      Btr
19a19 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
19a1a 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20  b[i].pBt;.      
19a1b 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20  if( pBt ){.     
19a1c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
19a1d 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
19a1e 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 72 29  ne(pBt, zMaster)
19a1f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
19a20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
19a21 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29 3b  seFree(pMaster);
19a22 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
19a23 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
19a24 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
19a25 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
19a26 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
19a27 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74   }..    /* Delet
19a28 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
19a29 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20  rnal file. This 
19a2a 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61 6e  commits the tran
19a2b 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a 20  saction. After. 
19a2c 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69 73     ** doing this
19a2d 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 69   the directory i
19a2e 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20 62  s synced again b
19a2f 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76 69  efore any indivi
19a30 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61 6e  dual.    ** tran
19a31 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61 72  saction files ar
19a32 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  e deleted..    *
19a33 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
19a34 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c  e3OsDelete(pVfs,
19a35 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20 20   zMaster, 1);.  
19a36 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
19a37 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  db, zMaster);.  
19a38 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20    zMaster = 0;. 
19a39 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
19a3a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
19a3b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 20    }..    /* All 
19a3c 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 63 74  files and direct
19a3d 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 65 61  ories have alrea
19a3e 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2c 20  dy been synced, 
19a3f 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  so the following
19a40 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f  .    ** calls to
19a41 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
19a42 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61 72  mitPhaseTwo() ar
19a43 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20 66  e only closing f
19a44 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20  iles and.    ** 
19a45 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e  deleting or trun
19a46 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73 2e  cating journals.
19a47 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   If something go
19a48 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a 20  es wrong while. 
19a49 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 61     ** this is ha
19a4a 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27 74  ppening we don't
19a4b 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 54 68   really care. Th
19a4c 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74  e integrity of t
19a4d 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61  he.    ** transa
19a4e 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79  ction is already
19a4f 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 75 74   guaranteed, but
19a50 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 6f 6c   some stray 'col
19a51 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20 20  d' journals.    
19a52 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e 67 20  ** may be lying 
19a53 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69 6e  around. Returnin
19a54 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  g an error code 
19a55 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 74 65  won't help matte
19a56 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64  rs..    */.    d
19a57 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64  isable_simulated
19a58 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 20  _io_errors();.  
19a59 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
19a5a 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
19a5b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
19a5c 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20  >nDb; i++){ .   
19a5d 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
19a5e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
19a5f 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b        if( pBt ){
19a60 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
19a61 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
19a62 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20  Two(pBt);.      
19a63 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
19a64 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
19a65 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c 65  oc();.    enable
19a66 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
19a67 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71 6c  rors();..    sql
19a68 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64  ite3VtabCommit(d
19a69 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  b);.  }.#endif..
19a6a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
19a6b 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  /* .** This rout
19a6c 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74 20  ine checks that 
19a6d 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74 69  the sqlite3.acti
19a6e 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74 20  veVdbeCnt count 
19a6f 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74 63  variable.** matc
19a70 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  hes the number o
19a71 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65 20  f vdbe's in the 
19a72 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56 64  list sqlite3.pVd
19a73 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63  be that are.** c
19a74 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 2e  urrently active.
19a75 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
19a76 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20 63  ils if the two c
19a77 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61 74  ounts do not mat
19a78 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ch..** This is a
19a79 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66 2d  n internal self-
19a7a 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74 20  check only - it 
19a7b 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e 74  is not an essent
19a7c 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a  ial processing.*
19a7d 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  * step..**.** Th
19a7e 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  is is a no-op if
19a7f 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e   NDEBUG is defin
19a80 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  ed..*/.#ifndef N
19a81 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f 69  DEBUG.static voi
19a82 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62  d checkActiveVdb
19a83 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62  eCnt(sqlite3 *db
19a84 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20  ){.  Vdbe *p;.  
19a85 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69  int cnt = 0;.  i
19a86 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a 20  nt nWrite = 0;. 
19a87 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a   p = db->pVdbe;.
19a88 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20    while( p ){.  
19a89 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d    if( p->magic==
19a8a 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26  VDBE_MAGIC_RUN &
19a8b 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  & p->pc>=0 ){.  
19a8c 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20      cnt++;.     
19a8d 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79   if( p->readOnly
19a8e 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b 0a  ==0 ) nWrite++;.
19a8f 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d      }.    p = p-
19a90 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61 73  >pNext;.  }.  as
19a91 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e 61  sert( cnt==db->a
19a92 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b 0a  ctiveVdbeCnt );.
19a93 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74 65    assert( nWrite
19a94 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  ==db->writeVdbeC
19a95 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64  nt );.}.#else.#d
19a96 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69 76  efine checkActiv
19a97 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e 64  eVdbeCnt(x).#end
19a98 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 76  if../*.** For ev
19a99 65 72 79 20 42 74 72 65 65 20 74 68 61 74 20 69  ery Btree that i
19a9a 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  n database conne
19a9b 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20 0a  ction db which .
19a9c 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69  ** has been modi
19a9d 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72 20  fied, "trip" or 
19a9e 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68 20  invalidate each 
19a9f 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68 61  cursor in.** tha
19aa0 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 68 61  t Btree might ha
19aa1 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  ve been modified
19aa2 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 75 72   so that the cur
19aa3 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65 72  sor.** can never
19aa4 20 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e 20   be used again. 
19aa5 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68   This happens wh
19aa6 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a  en a rollback.**
19aa7 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68 61  * occurs.  We ha
19aa8 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20 74  ve to trip all t
19aa9 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73  he other cursors
19aaa 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f 72  , even.** cursor
19aab 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73 20   from other VMs 
19aac 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61 74  in different dat
19aad 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
19aae 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f  s,.** so that no
19aaf 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20 74  ne of them try t
19ab0 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 20 61  o use the data a
19ab1 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20  t which they.** 
19ab2 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 6e  were pointing an
19ab3 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79 20  d which now may 
19ab4 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67 65  have been change
19ab5 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65 20  d due.** to the 
19ab6 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  rollback..**.** 
19ab7 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61 20  Remember that a 
19ab8 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65 6c  rollback can del
19ab9 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70 6c  ete tables compl
19aba 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72 64  ete and.** reord
19abb 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20 53  er rootpages.  S
19abc 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66  o it is not suff
19abd 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20 73  icient just to s
19abe 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65  ave.** the state
19abf 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20   of the cursor. 
19ac0 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76 61   We have to inva
19ac1 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f  lidate the curso
19ac2 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20  r.** so that it 
19ac3 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61 67  is never used ag
19ac4 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ain..*/.static v
19ac5 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43 75  oid invalidateCu
19ac6 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42  rsorsOnModifiedB
19ac7 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a 64  trees(sqlite3 *d
19ac8 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  b){.  int i;.  f
19ac9 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
19aca 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  b; i++){.    Btr
19acb 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b  ee *p = db->aDb[
19acc 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20  i].pBt;.    if( 
19acd 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65  p && sqlite3Btre
19ace 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 29 7b  eIsInTrans(p) ){
19acf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
19ad0 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72  reeTripAllCursor
19ad1 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52  s(p, SQLITE_ABOR
19ad2 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  T);.    }.  }.}.
19ad3 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 56 64  ./*.** If the Vd
19ad4 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  be passed as the
19ad5 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
19ad6 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d 65  opened a stateme
19ad7 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a  nt-transaction,.
19ad8 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e 6f 77 2e  ** close it now.
19ad9 20 41 72 67 75 6d 65 6e 74 20 65 4f 70 20 6d 75   Argument eOp mu
19ada 73 74 20 62 65 20 65 69 74 68 65 72 20 53 41 56  st be either SAV
19adb 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
19adc 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f  or.** SAVEPOINT_
19add 52 45 4c 45 41 53 45 2e 20 49 66 20 69 74 20 69  RELEASE. If it i
19ade 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  s SAVEPOINT_ROLL
19adf 42 41 43 4b 2c 20 74 68 65 6e 20 74 68 65 20 73  BACK, then the s
19ae0 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e  tatement.** tran
19ae1 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
19ae2 64 20 62 61 63 6b 2e 20 49 66 20 65 4f 70 20 69  d back. If eOp i
19ae3 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  s SAVEPOINT_RELE
19ae4 41 53 45 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a  ASE, then the .*
19ae5 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  * statement tran
19ae6 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 74  saction is commt
19ae7 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ted..**.** If an
19ae8 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
19ae9 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  , an SQLITE_IOER
19aea 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65  R_XXX error code
19aeb 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a   is returned. .*
19aec 2a 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49  * Otherwise SQLI
19aed 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TE_OK..*/.SQLITE
19aee 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
19aef 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61  ite3VdbeCloseSta
19af0 74 65 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20  tement(Vdbe *p, 
19af1 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73 71 6c 69  int eOp){.  sqli
19af2 74 65 33 20 2a 63 6f 6e 73 74 20 64 62 20 3d 20  te3 *const db = 
19af3 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 20  p->db;.  int rc 
19af4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
19af5 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74  f( p->iStatement
19af6 20 26 26 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65   && db->nStateme
19af7 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  nt ){.    int i;
19af8 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69  .    const int i
19af9 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 2d 3e 69  Savepoint = p->i
19afa 53 74 61 74 65 6d 65 6e 74 2d 31 3b 0a 0a 20 20  Statement-1;..  
19afb 20 20 61 73 73 65 72 74 28 20 65 4f 70 3d 3d 53    assert( eOp==S
19afc 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
19afd 4b 20 7c 7c 20 65 4f 70 3d 3d 53 41 56 45 50 4f  K || eOp==SAVEPO
19afe 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a 20 20  INT_RELEASE);.  
19aff 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53    assert( db->nS
19b00 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 20  tatement>0 );.  
19b01 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 53 74    assert( p->iSt
19b02 61 74 65 6d 65 6e 74 3d 3d 28 64 62 2d 3e 6e 53  atement==(db->nS
19b03 74 61 74 65 6d 65 6e 74 2b 64 62 2d 3e 6e 53 61  tatement+db->nSa
19b04 76 65 70 6f 69 6e 74 29 20 29 3b 0a 0a 20 20 20  vepoint) );..   
19b05 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
19b06 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20  nDb; i++){ .    
19b07 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49    int rc2 = SQLI
19b08 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 42 74 72  TE_OK;.      Btr
19b09 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
19b0a 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20  b[i].pBt;.      
19b0b 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20  if( pBt ){.     
19b0c 20 20 20 69 66 28 20 65 4f 70 3d 3d 53 41 56 45     if( eOp==SAVE
19b0d 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29  POINT_ROLLBACK )
19b0e 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 32 20  {.          rc2 
19b0f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61  = sqlite3BtreeSa
19b10 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 56  vepoint(pBt, SAV
19b11 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c  EPOINT_ROLLBACK,
19b12 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20   iSavepoint);.  
19b13 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
19b14 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f  if( rc2==SQLITE_
19b15 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
19b16 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74 72  rc2 = sqlite3Btr
19b17 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2c  eeSavepoint(pBt,
19b18 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41   SAVEPOINT_RELEA
19b19 53 45 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b  SE, iSavepoint);
19b1a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
19b1b 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
19b1c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
19b1d 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20    rc = rc2;.    
19b1e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
19b1f 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6e 53 74 61    }.    db->nSta
19b20 74 65 6d 65 6e 74 2d 2d 3b 0a 20 20 20 20 70 2d  tement--;.    p-
19b21 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b  >iStatement = 0;
19b22 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
19b23 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51  ;.}../*.** If SQ
19b24 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
19b25 20 74 6f 20 73 75 70 70 6f 72 74 20 73 68 61 72   to support shar
19b26 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 61 6e  ed-cache mode an
19b27 64 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61  d to be threadsa
19b28 66 65 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  fe,.** this rout
19b29 69 6e 65 20 6f 62 74 61 69 6e 73 20 74 68 65 20  ine obtains the 
19b2a 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64  mutex associated
19b2b 20 77 69 74 68 20 65 61 63 68 20 42 74 53 68 61   with each BtSha
19b2c 72 65 64 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  red structure.**
19b2d 20 74 68 61 74 20 6d 61 79 20 62 65 20 61 63 63   that may be acc
19b2e 65 73 73 65 64 20 62 79 20 74 68 65 20 56 4d 20  essed by the VM 
19b2f 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
19b30 75 6d 65 6e 74 2e 20 49 6e 20 64 6f 69 6e 67 20  ument. In doing 
19b31 73 6f 20 69 74 0a 2a 2a 20 73 65 74 73 20 74 68  so it.** sets th
19b32 65 20 42 74 53 68 61 72 65 64 2e 64 62 20 6d 65  e BtShared.db me
19b33 6d 62 65 72 20 6f 66 20 65 61 63 68 20 6f 66 20  mber of each of 
19b34 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74 72  the BtShared str
19b35 75 63 74 75 72 65 73 2c 20 65 6e 73 75 72 69 6e  uctures, ensurin
19b36 67 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 6f  g.** that the co
19b37 72 72 65 63 74 20 62 75 73 79 2d 68 61 6e 64 6c  rrect busy-handl
19b38 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  er callback is i
19b39 6e 76 6f 6b 65 64 20 69 66 20 72 65 71 75 69 72  nvoked if requir
19b3a 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c  ed..**.** If SQL
19b3b 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61  ite is not threa
19b3c 64 73 61 66 65 20 62 75 74 20 64 6f 65 73 20 73  dsafe but does s
19b3d 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61  upport shared-ca
19b3e 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 0a 2a  che mode, then.*
19b3f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  * sqlite3BtreeEn
19b40 74 65 72 41 6c 6c 28 29 20 69 73 20 69 6e 76 6f  terAll() is invo
19b41 6b 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 42  ked to set the B
19b42 74 53 68 61 72 65 64 2e 64 62 20 76 61 72 69 61  tShared.db varia
19b43 62 6c 65 73 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f  bles.** of all o
19b44 66 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  f BtShared struc
19b45 74 75 72 65 73 20 61 63 63 65 73 73 69 62 6c 65  tures accessible
19b46 20 76 69 61 20 74 68 65 20 64 61 74 61 62 61 73   via the databas
19b47 65 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 61 73 73  e handle .** ass
19b48 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
19b49 20 56 4d 2e 20 4f 66 20 63 6f 75 72 73 65 20 6f   VM. Of course o
19b4a 6e 6c 79 20 61 20 73 75 62 73 65 74 20 6f 66 20  nly a subset of 
19b4b 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73  these structures
19b4c 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 61 63 63 65  .** will be acce
19b4d 73 73 65 64 20 62 79 20 74 68 65 20 56 4d 2c 20  ssed by the VM, 
19b4e 61 6e 64 20 77 65 20 63 6f 75 6c 64 20 75 73 65  and we could use
19b4f 20 56 64 62 65 2e 62 74 72 65 65 4d 61 73 6b 20   Vdbe.btreeMask 
19b50 74 6f 20 66 69 67 75 72 65 0a 2a 2a 20 74 68 61  to figure.** tha
19b51 74 20 73 75 62 73 65 74 20 6f 75 74 2c 20 62 75  t subset out, bu
19b52 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 61 64  t there is no ad
19b53 76 61 6e 74 61 67 65 20 74 6f 20 64 6f 69 6e 67  vantage to doing
19b54 20 73 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51   so..**.** If SQ
19b55 4c 69 74 65 20 69 73 20 6e 6f 74 20 74 68 72 65  Lite is not thre
19b56 61 64 73 61 66 65 20 61 6e 64 20 64 6f 65 73 20  adsafe and does 
19b57 6e 6f 74 20 73 75 70 70 6f 72 74 20 73 68 61 72  not support shar
19b58 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 74  ed-cache mode, t
19b59 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
19b5a 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 23  is a no-op..*/.#
19b5b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
19b5c 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
19b5d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19b5e 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
19b5f 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 56  utexArrayEnter(V
19b60 64 62 65 20 2a 70 29 7b 0a 23 69 66 20 53 51 4c  dbe *p){.#if SQL
19b61 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20  ITE_THREADSAFE. 
19b62 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
19b63 65 78 41 72 72 61 79 45 6e 74 65 72 28 26 70 2d  exArrayEnter(&p-
19b64 3e 61 4d 75 74 65 78 29 3b 0a 23 65 6c 73 65 0a  >aMutex);.#else.
19b65 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
19b66 74 65 72 41 6c 6c 28 70 2d 3e 64 62 29 3b 0a 23  terAll(p->db);.#
19b67 65 6e 64 69 66 0a 7d 0a 23 65 6e 64 69 66 0a 0a  endif.}.#endif..
19b68 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
19b69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65  ne is called the
19b6a 20 77 68 65 6e 20 61 20 56 44 42 45 20 74 72 69   when a VDBE tri
19b6b 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 49 66 20  es to halt.  If 
19b6c 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 61 73 20  the VDBE.** has 
19b6d 6d 61 64 65 20 63 68 61 6e 67 65 73 20 61 6e 64  made changes and
19b6e 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69   is in autocommi
19b6f 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 63 6f 6d  t mode, then com
19b70 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 63 68 61  mit those.** cha
19b71 6e 67 65 73 2e 20 20 49 66 20 61 20 72 6f 6c 6c  nges.  If a roll
19b72 62 61 63 6b 20 69 73 20 6e 65 65 64 65 64 2c 20  back is needed, 
19b73 74 68 65 6e 20 64 6f 20 74 68 65 20 72 6f 6c 6c  then do the roll
19b74 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  back..**.** This
19b75 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
19b76 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65  only way to move
19b77 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 61 20   the state of a 
19b78 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 4c 49 54  VM from.** SQLIT
19b79 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 6f 20 53  E_MAGIC_RUN to S
19b7a 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c 54  QLITE_MAGIC_HALT
19b7b 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c 65 73  .  It is harmles
19b7c 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 74 68 69  s to.** call thi
19b7d 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 74 20 69  s on a VM that i
19b7e 73 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45 5f  s in the SQLITE_
19b7f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 61 74 65  MAGIC_HALT state
19b80 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ..**.** Return a
19b81 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 49  n error code.  I
19b82 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 6f 75  f the commit cou
19b83 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 20  ld not complete 
19b84 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f  because of.** lo
19b85 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c 20 72  ck contention, r
19b86 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
19b87 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f 42 55  Y.  If SQLITE_BU
19b88 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  SY is returned, 
19b89 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 65 20  it.** means the 
19b8a 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20 68 61  close did not ha
19b8b 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73 20 74  ppen and needs t
19b8c 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e 0a 2a  o be repeated..*
19b8d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19b8e 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
19b8f 48 61 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20  Halt(Vdbe *p){. 
19b90 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
19b91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19b92 20 2f 2a 20 55 73 65 64 20 74 6f 20 73 74 6f 72   /* Used to stor
19b93 65 20 74 72 61 6e 73 69 65 6e 74 20 72 65 74 75  e transient retu
19b94 72 6e 20 63 6f 64 65 73 20 2a 2f 0a 20 20 73 71  rn codes */.  sq
19b95 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
19b96 62 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75  b;..  /* This fu
19b97 6e 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  nction contains 
19b98 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74 20 64  the logic that d
19b99 65 74 65 72 6d 69 6e 65 73 20 69 66 20 61 20 73  etermines if a s
19b9a 74 61 74 65 6d 65 6e 74 20 6f 72 0a 20 20 2a 2a  tatement or.  **
19b9b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c   transaction wil
19b9c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f  l be committed o
19b9d 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73  r rolled back as
19b9e 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65   a result of the
19b9f 0a 20 20 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20  .  ** execution 
19ba0 6f 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20  of this virtual 
19ba1 6d 61 63 68 69 6e 65 2e 20 0a 20 20 2a 2a 0a 20  machine. .  **. 
19ba2 20 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68   ** If any of th
19ba3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f  e following erro
19ba4 72 73 20 6f 63 63 75 72 3a 0a 20 20 2a 2a 0a 20  rs occur:.  **. 
19ba5 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4e   **     SQLITE_N
19ba6 4f 4d 45 4d 0a 20 20 2a 2a 20 20 20 20 20 53 51  OMEM.  **     SQ
19ba7 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 2a 2a 20  LITE_IOERR.  ** 
19ba8 20 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a      SQLITE_FULL.
19ba9 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f    **     SQLITE_
19baa 49 4e 54 45 52 52 55 50 54 0a 20 20 2a 2a 0a 20  INTERRUPT.  **. 
19bab 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 69 6e 74   ** Then the int
19bac 65 72 6e 61 6c 20 63 61 63 68 65 20 6d 69 67 68  ernal cache migh
19bad 74 20 68 61 76 65 20 62 65 65 6e 20 6c 65 66 74  t have been left
19bae 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74   in an inconsist
19baf 65 6e 74 0a 20 20 2a 2a 20 73 74 61 74 65 2e 20  ent.  ** state. 
19bb0 20 57 65 20 6e 65 65 64 20 74 6f 20 72 6f 6c 6c   We need to roll
19bb1 62 61 63 6b 20 74 68 65 20 73 74 61 74 65 6d 65  back the stateme
19bb2 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  nt transaction, 
19bb3 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 2a 2a  if there is.  **
19bb4 20 6f 6e 65 2c 20 6f 72 20 74 68 65 20 63 6f 6d   one, or the com
19bb5 70 6c 65 74 65 20 74 72 61 6e 73 61 63 74 69 6f  plete transactio
19bb6 6e 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  n if there is no
19bb7 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
19bb8 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20 20  action..  */..  
19bb9 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  if( p->db->mallo
19bba 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70  cFailed ){.    p
19bbb 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
19bbc 4d 45 4d 3b 0a 20 20 7d 0a 20 20 63 6c 6f 73 65  MEM;.  }.  close
19bbd 41 6c 6c 43 75 72 73 6f 72 73 45 78 63 65 70 74  AllCursorsExcept
19bbe 41 63 74 69 76 65 56 74 61 62 73 28 70 29 3b 0a  ActiveVtabs(p);.
19bbf 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d    if( p->magic!=
19bc0 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29  VDBE_MAGIC_RUN )
19bc1 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
19bc2 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 63 68  ITE_OK;.  }.  ch
19bc3 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74  eckActiveVdbeCnt
19bc4 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 63  (db);..  /* No c
19bc5 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63  ommit or rollbac
19bc6 6b 20 6e 65 65 64 65 64 20 69 66 20 74 68 65 20  k needed if the 
19bc7 70 72 6f 67 72 61 6d 20 6e 65 76 65 72 20 73 74  program never st
19bc8 61 72 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 70  arted */.  if( p
19bc9 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 69  ->pc>=0 ){.    i
19bca 6e 74 20 6d 72 63 3b 20 20 20 2f 2a 20 50 72 69  nt mrc;   /* Pri
19bcb 6d 61 72 79 20 65 72 72 6f 72 20 63 6f 64 65 20  mary error code 
19bcc 66 72 6f 6d 20 70 2d 3e 72 63 20 2a 2f 0a 20 20  from p->rc */.  
19bcd 20 20 69 6e 74 20 65 53 74 61 74 65 6d 65 6e 74    int eStatement
19bce 4f 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  Op = 0;.    int 
19bcf 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 3b 20  isSpecialError; 
19bd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
19bd1 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 20 27  t to true if a '
19bd2 73 70 65 63 69 61 6c 27 20 65 72 72 6f 72 20 2a  special' error *
19bd3 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 61  /..    /* Lock a
19bd4 6c 6c 20 62 74 72 65 65 73 20 75 73 65 64 20 62  ll btrees used b
19bd5 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  y the statement 
19bd6 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
19bd7 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65  beMutexArrayEnte
19bd8 72 28 70 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68  r(p);..    /* Ch
19bd9 65 63 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74  eck for one of t
19bda 68 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72  he special error
19bdb 73 20 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 70  s */.    mrc = p
19bdc 2d 3e 72 63 20 26 20 30 78 66 66 3b 0a 20 20 20  ->rc & 0xff;.   
19bdd 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 20   isSpecialError 
19bde 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  = mrc==SQLITE_NO
19bdf 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49  MEM || mrc==SQLI
19be0 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 20  TE_IOERR.       
19be1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
19be2 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54   mrc==SQLITE_INT
19be3 45 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53  ERRUPT || mrc==S
19be4 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20  QLITE_FULL;.    
19be5 69 66 28 20 69 73 53 70 65 63 69 61 6c 45 72 72  if( isSpecialErr
19be6 6f 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  or ){.      /* I
19be7 66 20 74 68 65 20 71 75 65 72 79 20 77 61 73 20  f the query was 
19be8 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e 65  read-only, we ne
19be9 65 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63  ed do no rollbac
19bea 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 77  k at all. Otherw
19beb 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 72  ise,.      ** pr
19bec 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20 73  oceed with the s
19bed 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e  pecial handling.
19bee 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
19bef 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79  if( !p->readOnly
19bf0 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f   || mrc!=SQLITE_
19bf1 49 4e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20  INTERRUPT ){.   
19bf2 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d       if( p->rc==
19bf3 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f  SQLITE_IOERR_BLO
19bf4 43 4b 45 44 20 26 26 20 70 2d 3e 75 73 65 73 53  CKED && p->usesS
19bf5 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  tmtJournal ){.  
19bf6 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65          eStateme
19bf7 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54  ntOp = SAVEPOINT
19bf8 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 20  _ROLLBACK;.     
19bf9 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c       p->rc = SQL
19bfa 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
19bfb 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6d 72 63    }else if( (mrc
19bfc 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c  ==SQLITE_NOMEM |
19bfd 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55  | mrc==SQLITE_FU
19bfe 4c 4c 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  LL).            
19bff 20 20 20 20 20 20 20 26 26 20 70 2d 3e 75 73 65         && p->use
19c00 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a  sStmtJournal ){.
19c01 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65            eState
19c02 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49  mentOp = SAVEPOI
19c03 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20  NT_ROLLBACK;.   
19c04 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
19c05 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20        /* We are 
19c06 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62  forced to roll b
19c07 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 74  ack the active t
19c08 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f  ransaction. Befo
19c09 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 20  re doing.       
19c0a 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20     ** so, abort 
19c0b 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 6d  any other statem
19c0c 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c 65  ents this handle
19c0d 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61   currently has a
19c0e 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20  ctive..         
19c0f 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e   */.          in
19c10 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f  validateCursorsO
19c11 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28  nModifiedBtrees(
19c12 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  db);.          s
19c13 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
19c14 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20  l(db);.         
19c15 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76   sqlite3CloseSav
19c16 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20  epoints(db);.   
19c17 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
19c18 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  ommit = 1;.     
19c19 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
19c1a 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20   }.  .    /* If 
19c1b 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  the auto-commit 
19c1c 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20  flag is set and 
19c1d 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79  this is the only
19c1e 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20 0a   active writer .
19c1f 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e 20      ** VM, then 
19c20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 63  we do either a c
19c21 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63  ommit or rollbac
19c22 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  k of the current
19c23 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20   transaction. . 
19c24 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74     **.    ** Not
19c25 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c  e: This block al
19c26 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f  so runs if one o
19c27 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72  f the special er
19c28 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 20  rors handled .  
19c29 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 6f    ** above has o
19c2a 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a 2f  ccurred. .    */
19c2b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
19c2c 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20  3VtabInSync(db) 
19c2d 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 74  .     && db->aut
19c2e 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 26  oCommit .     &&
19c2f 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e   db->writeVdbeCn
19c30 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d  t==(p->readOnly=
19c31 3d 30 29 20 0a 20 20 20 20 20 26 26 20 28 64 62  =0) .     && (db
19c32 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
19c33 5f 43 6f 6d 6d 69 74 42 75 73 79 29 3d 3d 30 0a  _CommitBusy)==0.
19c34 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28      ){.      if(
19c35 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
19c36 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63  K || (p->errorAc
19c37 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26  tion==OE_Fail &&
19c38 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72   !isSpecialError
19c39 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
19c3a 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  The auto-commit 
19c3b 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 61 6e  flag is true, an
19c3c 64 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  d the vdbe progr
19c3d 61 6d 20 77 61 73 20 0a 20 20 20 20 20 20 20 20  am was .        
19c3e 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6f 72  ** successful or
19c3f 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c   hit an 'OR FAIL
19c40 27 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 54 68  ' constraint. Th
19c41 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d 6d 69  is means a commi
19c42 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 73  t .        ** is
19c43 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 20   required..     
19c44 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63     */.        rc
19c45 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62   = vdbeCommit(db
19c46 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , p);.        if
19c47 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
19c48 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  Y ){.          s
19c49 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
19c4a 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61  ArrayLeave(&p->a
19c4b 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20  Mutex);.        
19c4c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19c4d 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65  BUSY;.        }e
19c4e 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49  lse if( rc!=SQLI
19c4f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
19c50 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20     p->rc = rc;. 
19c51 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
19c52 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b  RollbackAll(db);
19c53 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
19c54 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
19c55 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43  3CommitInternalC
19c56 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20  hanges(db);.    
19c57 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
19c58 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
19c59 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
19c5a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
19c5b 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20   db->nStatement 
19c5c 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  = 0;.    }else i
19c5d 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d  f( eStatementOp=
19c5e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
19c5f 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->rc==SQLITE_OK
19c60 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69   || p->errorActi
19c61 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20  on==OE_Fail ){. 
19c62 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e         eStatemen
19c63 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f  tOp = SAVEPOINT_
19c64 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d  RELEASE;.      }
19c65 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f  else if( p->erro
19c66 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72  rAction==OE_Abor
19c67 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74  t ){.        eSt
19c68 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45  atementOp = SAVE
19c69 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a  POINT_ROLLBACK;.
19c6a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19c6b 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43       invalidateC
19c6c 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64  ursorsOnModified
19c6d 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20  Btrees(db);.    
19c6e 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62      sqlite3Rollb
19c6f 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  ackAll(db);.    
19c70 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65      sqlite3Close
19c71 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a  Savepoints(db);.
19c72 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f          db->auto
19c73 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  Commit = 1;.    
19c74 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
19c75 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e   /* If eStatemen
19c76 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  tOp is non-zero,
19c77 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e   then a statemen
19c78 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65  t transaction ne
19c79 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65  eds to.    ** be
19c7a 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f   committed or ro
19c7b 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20  lled back. Call 
19c7c 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65  sqlite3VdbeClose
19c7d 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20  Statement() to. 
19c7e 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20     ** do so. If 
19c7f 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72  this operation r
19c80 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c  eturns an error,
19c81 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
19c82 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a   statement.    *
19c83 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  * error code is 
19c84 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c  SQLITE_OK or SQL
19c85 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20  ITE_CONSTRAINT, 
19c86 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 72  then set the err
19c87 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74  or.    ** code t
19c88 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e  o the new value.
19c89 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
19c8a 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a  eStatementOp ){.
19c8b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
19c8c 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65  e3VdbeCloseState
19c8d 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65  ment(p, eStateme
19c8e 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28  ntOp);.      if(
19c8f 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53   rc && (p->rc==S
19c90 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72  QLITE_OK || p->r
19c91 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  c==SQLITE_CONSTR
19c92 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20  AINT) ){.       
19c93 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20   p->rc = rc;.   
19c94 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
19c95 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
19c96 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a  g);.        p->z
19c97 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20  ErrMsg = 0;.    
19c98 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
19c99 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20   /* If this was 
19c9a 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  an INSERT, UPDAT
19c9b 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20  E or DELETE and 
19c9c 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  no statement tra
19c9d 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  nsaction.    ** 
19c9e 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20  has been rolled 
19c9f 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65  back, update the
19ca0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
19ca1 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e  tion change-coun
19ca2 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  ter. .    */.   
19ca3 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e   if( p->changeCn
19ca4 74 4f 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20  tOn && p->pc>=0 
19ca5 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53 74  ){.      if( eSt
19ca6 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45 50  atementOp!=SAVEP
19ca7 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 7b  OINT_ROLLBACK ){
19ca8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
19ca9 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64  VdbeSetChanges(d
19caa 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a  b, p->nChange);.
19cab 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19cac 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
19cad 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 30  SetChanges(db, 0
19cae 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
19caf 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b   p->nChange = 0;
19cb0 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
19cb1 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 6f 6d   Rollback or com
19cb2 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 20 63  mit any schema c
19cb3 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 63 75  hanges that occu
19cb4 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28  rred. */.    if(
19cb5 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f   p->rc!=SQLITE_O
19cb6 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53  K && db->flags&S
19cb7 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
19cb8 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ges ){.      sql
19cb9 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
19cba 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
19cbb 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20        db->flags 
19cbc 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20 53  = (db->flags | S
19cbd 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
19cbe 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ges);.    }..   
19cbf 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20   /* Release the 
19cc0 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 71 6c  locks */.    sql
19cc1 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
19cc2 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75  rayLeave(&p->aMu
19cc3 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tex);.  }..  /* 
19cc4 57 65 20 68 61 76 65 20 73 75 63 63 65 73 73 66  We have successf
19cc5 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e 64 20  ully halted and 
19cc6 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e 20 20  closed the VM.  
19cc7 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 63 74  Record this fact
19cc8 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63  . */.  if( p->pc
19cc9 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 61  >=0 ){.    db->a
19cca 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d 3b 0a  ctiveVdbeCnt--;.
19ccb 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 64      if( !p->read
19ccc 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 64 62  Only ){.      db
19ccd 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2d 2d  ->writeVdbeCnt--
19cce 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
19ccf 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  rt( db->activeVd
19cd0 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74 65  beCnt>=db->write
19cd1 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d 0a 20  VdbeCnt );.  }. 
19cd2 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45   p->magic = VDBE
19cd3 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 20 63  _MAGIC_HALT;.  c
19cd4 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e  heckActiveVdbeCn
19cd5 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 2d 3e  t(db);.  if( p->
19cd6 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
19cd7 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
19cd8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
19cd9 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  }..  /* If the a
19cda 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20  uto-commit flag 
19cdb 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 2c 20  is set to true, 
19cdc 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 20 74  then any locks t
19cdd 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a 20 20  hat were held.  
19cde 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e  ** by connection
19cdf 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 65 65   db have now bee
19ce0 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 6c 6c  n released. Call
19ce1 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
19ce2 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a 20 20  onUnlocked() .  
19ce3 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 6e 79  ** to invoke any
19ce4 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f 63 6b   required unlock
19ce5 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
19ce6 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  s..  */.  if( db
19ce7 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
19ce8 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65      sqlite3Conne
19ce9 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62  ctionUnlocked(db
19cea 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
19ceb 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ( db->activeVdbe
19cec 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74  Cnt>0 || db->aut
19ced 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62  oCommit==0 || db
19cee 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20  ->nStatement==0 
19cef 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
19cf0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
19cf1 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64 73   Each VDBE holds
19cf2 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
19cf3 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 73  he most recent s
19cf4 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63 61  qlite3_step() ca
19cf5 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e 20  ll.** in p->rc. 
19cf6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
19cf7 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20 62  ts that result b
19cf8 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b  ack to SQLITE_OK
19cf9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19cfa 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
19cfb 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73  VdbeResetStepRes
19cfc 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ult(Vdbe *p){.  
19cfd 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  p->rc = SQLITE_O
19cfe 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  K;.}../*.** Clea
19cff 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74 65  n up a VDBE afte
19d00 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74 20  r execution but 
19d01 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68  do not delete th
19d02 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74 2e  e VDBE just yet.
19d03 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65 72  .** Write any er
19d04 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e 74  ror messages int
19d05 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52 65  o *pzErrMsg.  Re
19d06 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 20  turn the result 
19d07 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  code..**.** Afte
19d08 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  r this routine i
19d09 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45 20  s run, the VDBE 
19d0a 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79 20  should be ready 
19d0b 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a 2a  to be executed.*
19d0c 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54  * again..**.** T
19d0d 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e 6f  o look at it ano
19d0e 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20 72  ther way, this r
19d0f 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74 68  outine resets th
19d10 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a 2a  e state of the.*
19d11 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  * virtual machin
19d12 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47 49  e from VDBE_MAGI
19d13 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d 41  C_RUN or VDBE_MA
19d14 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74 6f  GIC_HALT back to
19d15 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  .** VDBE_MAGIC_I
19d16 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  NIT..*/.SQLITE_P
19d17 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19d18 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62 65  e3VdbeReset(Vdbe
19d19 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20   *p){.  sqlite3 
19d1a 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64  *db;.  db = p->d
19d1b 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  b;..  /* If the 
19d1c 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20 74  VM did not run t
19d1d 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72 20  o completion or 
19d1e 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72 65  if it encountere
19d1f 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 2c  d an.  ** error,
19d20 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e   then it might n
19d21 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61 6c  ot have been hal
19d22 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20 53  ted properly.  S
19d23 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20 6e  o halt.  ** it n
19d24 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64  ow..  */.  (void
19d25 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
19d26 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  (db);.  sqlite3V
19d27 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 28 76  dbeHalt(p);.  (v
19d28 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
19d29 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20  yOff(db);..  /* 
19d2a 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20  If the VDBE has 
19d2b 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72 74  be run even part
19d2c 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 6e  ially, then tran
19d2d 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20 63  sfer the error c
19d2e 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 72  ode.  ** and err
19d2f 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20  or message from 
19d30 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74 68  the VDBE into th
19d31 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
19d32 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74 0a  structure.  But.
19d33 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42 45    ** if the VDBE
19d34 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 73   has just been s
19d35 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68 61  et to run but ha
19d36 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 65  s not actually e
19d37 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a 2a  xecuted any.  **
19d38 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 65   instructions ye
19d39 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61 69  t, leave the mai
19d3a 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72  n database error
19d3b 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e 63   information unc
19d3c 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  hanged..  */.  i
19d3d 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  f( p->pc>=0 ){. 
19d3e 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d 73     if( p->zErrMs
19d3f 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  g ){.      sqlit
19d40 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
19d41 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71 6c  loc();.      sql
19d42 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
19d43 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e 7a  db->pErr,-1,p->z
19d44 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55 54  ErrMsg,SQLITE_UT
19d45 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  F8,SQLITE_TRANSI
19d46 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ENT);.      sqli
19d47 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
19d48 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  oc();.      db->
19d49 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b  errCode = p->rc;
19d4a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
19d4b 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72  Free(db, p->zErr
19d4c 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d 3e 7a  Msg);.      p->z
19d4d 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20  ErrMsg = 0;.    
19d4e 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20  }else if( p->rc 
19d4f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
19d50 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c  Error(db, p->rc,
19d51 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
19d52 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
19d53 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  or(db, SQLITE_OK
19d54 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  , 0);.    }.  }e
19d55 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 26 26  lse if( p->rc &&
19d56 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20   p->expired ){. 
19d57 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 72 65     /* The expire
19d58 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 20 6f  d flag was set o
19d59 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 6f 72  n the VDBE befor
19d5a 65 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  e the first call
19d5b 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74  .    ** to sqlit
19d5c 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 20 63  e3_step(). For c
19d5d 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 6e 63  onsistency (sinc
19d5e 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  e sqlite3_step()
19d5f 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c 6c   was.    ** call
19d60 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 61 74  ed), set the dat
19d61 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 20 74  abase error in t
19d62 68 69 73 20 63 61 73 65 20 61 73 20 77 65 6c 6c  his case as well
19d63 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
19d64 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d  ite3Error(db, p-
19d65 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  >rc, 0);.    sql
19d66 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
19d67 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 70 2d  db->pErr, -1, p-
19d68 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 54 45  >zErrMsg, SQLITE
19d69 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52  _UTF8, SQLITE_TR
19d6a 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71  ANSIENT);.    sq
19d6b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
19d6c 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  p->zErrMsg);.   
19d6d 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b   p->zErrMsg = 0;
19d6e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c 61  .  }..  /* Recla
19d6f 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73  im all memory us
19d70 65 64 20 62 79 20 74 68 65 20 56 44 42 45 0a 20  ed by the VDBE. 
19d71 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 70 29   */.  Cleanup(p)
19d72 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 72 6f  ;..  /* Save pro
19d73 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  filing informati
19d74 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 44 42  on from this VDB
19d75 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 66 64  E run..  */.#ifd
19d76 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a  ef VDBE_PROFILE.
19d77 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a 6f 75    {.    FILE *ou
19d78 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 65 5f  t = fopen("vdbe_
19d79 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 22 61  profile.out", "a
19d7a 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 20  ");.    if( out 
19d7b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
19d7c 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
19d7d 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 20 20  t, "---- ");.   
19d7e 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
19d7f 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nOp; i++){.    
19d80 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
19d81 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f 70 5b   "%02x", p->aOp[
19d82 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 20 20  i].opcode);.    
19d83 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 6e 74    }.      fprint
19d84 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20  f(out, "\n");.  
19d85 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
19d86 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nOp; i++){.   
19d87 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
19d88 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 25 38  , "%6d %10lld %8
19d89 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 20 20  lld ",.         
19d8a 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 2c    p->aOp[i].cnt,
19d8b 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61  .           p->a
19d8c 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a 20 20  Op[i].cycles,.  
19d8d 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b           p->aOp[
19d8e 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e 61 4f  i].cnt>0 ? p->aO
19d8f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d 3e 61  p[i].cycles/p->a
19d90 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a 20 20  Op[i].cnt : 0.  
19d91 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
19d92 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
19d93 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 2d 3e  tOp(out, i, &p->
19d94 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d  aOp[i]);.      }
19d95 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f 75  .      fclose(ou
19d96 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  t);.    }.  }.#e
19d97 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 63 20  ndif.  p->magic 
19d98 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  = VDBE_MAGIC_INI
19d99 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 72  T;.  return p->r
19d9a 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b  c & db->errMask;
19d9b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e  .}. ./*.** Clean
19d9c 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20 61   up and delete a
19d9d 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63   VDBE after exec
19d9e 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 61  ution.  Return a
19d9f 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
19da0 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  is.** the result
19da1 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61 6e   code.  Write an
19da2 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  y error message 
19da3 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72 72  text into *pzErr
19da4 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Msg..*/.SQLITE_P
19da5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19da6 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56  e3VdbeFinalize(V
19da7 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  dbe *p){.  int r
19da8 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
19da9 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56   if( p->magic==V
19daa 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c 7c  DBE_MAGIC_RUN ||
19dab 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
19dac 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20 20  MAGIC_HALT ){.  
19dad 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
19dae 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20  beReset(p);.    
19daf 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70 2d  assert( (rc & p-
19db0 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72  >db->errMask)==r
19db1 63 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  c );.  }else if(
19db2 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f   p->magic!=VDBE_
19db3 4d 41 47 49 43 5f 49 4e 49 54 20 29 7b 0a 20 20  MAGIC_INIT ){.  
19db4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19db5 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71  MISUSE;.  }.  sq
19db6 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28  lite3VdbeDelete(
19db7 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
19db8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74  .}../*.** Call t
19db9 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f  he destructor fo
19dba 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20 65  r each auxdata e
19dbb 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 6e  ntry in pVdbeFun
19dbc 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74  c for which.** t
19dbd 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
19dbe 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 20   bit in mask is 
19dbf 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 20  clear.  Auxdata 
19dc0 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 33  entries beyond 3
19dc1 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 20  1.** are always 
19dc2 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 64  destroyed.  To d
19dc3 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 61  estroy all auxda
19dc4 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c  ta entries, call
19dc5 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
19dc6 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a   with mask==0..*
19dc7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19dc8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
19dc9 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 56  eDeleteAuxData(V
19dca 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75  dbeFunc *pVdbeFu
19dcb 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20  nc, int mask){. 
19dcc 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
19dcd 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e  0; i<pVdbeFunc->
19dce 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nAux; i++){.    
19dcf 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 2a  struct AuxData *
19dd0 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 6e  pAux = &pVdbeFun
19dd1 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20  c->apAux[i];.   
19dd2 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 28   if( (i>31 || !(
19dd3 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c 3c  mask&(((u32)1)<<
19dd4 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41  i))) && pAux->pA
19dd5 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ux ){.      if( 
19dd6 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b  pAux->xDelete ){
19dd7 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78  .        pAux->x
19dd8 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75  Delete(pAux->pAu
19dd9 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  x);.      }.    
19dda 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30    pAux->pAux = 0
19ddb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
19ddc 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  *.** Delete an e
19ddd 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53  ntire VDBE..*/.S
19dde 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
19ddf 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  id sqlite3VdbeDe
19de0 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20  lete(Vdbe *p){. 
19de1 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65   int i;.  sqlite
19de2 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d  3 *db;..  if( p=
19de3 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64  =0 ) return;.  d
19de4 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28  b = p->db;.  if(
19de5 20 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20   p->pPrev ){.   
19de6 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74   p->pPrev->pNext
19de7 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d   = p->pNext;.  }
19de8 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
19de9 28 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29  ( db->pVdbe==p )
19dea 3b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20  ;.    db->pVdbe 
19deb 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  = p->pNext;.  }.
19dec 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29    if( p->pNext )
19ded 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e  {.    p->pNext->
19dee 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76  pPrev = p->pPrev
19def 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61  ;.  }.  if( p->a
19df0 4f 70 20 29 7b 0a 20 20 20 20 4f 70 20 2a 70 4f  Op ){.    Op *pO
19df1 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20  p = p->aOp;.    
19df2 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f  for(i=0; i<p->nO
19df3 70 3b 20 69 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a  p; i++, pOp++){.
19df4 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c        freeP4(db,
19df5 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f   pOp->p4type, pO
19df6 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66  p->p4.p);.#ifdef
19df7 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
19df8 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
19df9 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d  e(db, pOp->zComm
19dfa 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 20  ent);.#endif    
19dfb 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65   .    }.  }.  re
19dfc 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d  leaseMemArray(p-
19dfd 3e 61 56 61 72 2c 20 70 2d 3e 6e 56 61 72 29 3b  >aVar, p->nVar);
19dfe 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
19dff 28 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b  (db, p->aLabel);
19e00 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  .  releaseMemArr
19e01 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20  ay(p->aColName, 
19e02 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f  p->nResColumn*CO
19e03 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69  LNAME_N);.  sqli
19e04 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
19e05 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71  >aColName);.  sq
19e06 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
19e07 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d  p->zSql);.  p->m
19e08 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
19e09 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65  C_DEAD;.  sqlite
19e0a 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
19e0b 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  Op);.  sqlite3Db
19e0c 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72 65  Free(db, p->pFre
19e0d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
19e0e 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f  ree(db, p);.}../
19e0f 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f  *.** If a MoveTo
19e10 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 65   operation is pe
19e11 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 76  nding on the giv
19e12 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20  en cursor, then 
19e13 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54  do that.** MoveT
19e14 6f 20 6e 6f 77 2e 20 20 52 65 74 75 72 6e 20 61  o now.  Return a
19e15 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 49  n error code.  I
19e16 66 20 6e 6f 20 4d 6f 76 65 54 6f 20 69 73 20 70  f no MoveTo is p
19e17 65 6e 64 69 6e 67 2c 20 74 68 69 73 0a 2a 2a 20  ending, this.** 
19e18 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74  routine does not
19e19 68 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73  hing and returns
19e1a 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53   SQLITE_OK..*/.S
19e1b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19e1c 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  t sqlite3VdbeCur
19e1d 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75  sorMoveto(VdbeCu
19e1e 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  rsor *p){.  if( 
19e1f 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  p->deferredMovet
19e20 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73  o ){.    int res
19e21 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c  , rc;.#ifdef SQL
19e22 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74  ITE_TEST.    ext
19e23 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  ern int sqlite3_
19e24 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65  search_count;.#e
19e25 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28  ndif.    assert(
19e26 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20   p->isTable );. 
19e27 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
19e28 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
19e29 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30  ed(p->pCursor, 0
19e2a 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65  , p->movetoTarge
19e2b 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20  t, 0, &res);.   
19e2c 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
19e2d 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74   rc;.    p->last
19e2e 52 6f 77 69 64 20 3d 20 6b 65 79 54 6f 49 6e 74  Rowid = keyToInt
19e2f 28 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74  (p->movetoTarget
19e30 29 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 49  );.    p->rowidI
19e31 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53 28  sValid = ALWAYS(
19e32 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 20  res==0) ?1:0;.  
19e33 20 20 69 66 28 20 4e 45 56 45 52 28 72 65 73 3c    if( NEVER(res<
19e34 30 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  0) ){.      rc =
19e35 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
19e36 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72  t(p->pCursor, &r
19e37 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  es);.      if( r
19e38 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
19e39 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
19e3a 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69  TE_TEST.    sqli
19e3b 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74  te3_search_count
19e3c 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70  ++;.#endif.    p
19e3d 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
19e3e 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 63   = 0;.    p->cac
19e3f 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
19e40 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65 20  _STALE;.  }else 
19e41 69 66 28 20 70 2d 3e 70 43 75 72 73 6f 72 20 29  if( p->pCursor )
19e42 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 4d 6f 76  {.    int hasMov
19e43 65 64 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  ed;.    int rc =
19e44 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
19e45 73 6f 72 48 61 73 4d 6f 76 65 64 28 70 2d 3e 70  sorHasMoved(p->p
19e46 43 75 72 73 6f 72 2c 20 26 68 61 73 4d 6f 76 65  Cursor, &hasMove
19e47 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  d);.    if( rc )
19e48 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
19e49 69 66 28 20 68 61 73 4d 6f 76 65 64 20 29 7b 0a  if( hasMoved ){.
19e4a 20 20 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74        p->cacheSt
19e4b 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
19e4c 4c 45 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 75 6c  LE;.      p->nul
19e4d 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  lRow = 1;.    }.
19e4e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
19e4f 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
19e50 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
19e51 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20  unctions:.**.** 
19e52 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
19e53 6c 54 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69 74  lType().** sqlit
19e54 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
19e55 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33  Len().** sqlite3
19e56 56 64 62 65 53 65 72 69 61 6c 4c 65 6e 28 29 0a  VdbeSerialLen().
19e57 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  ** sqlite3VdbeSe
19e58 72 69 61 6c 50 75 74 28 29 0a 2a 2a 20 73 71 6c  rialPut().** sql
19e59 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
19e5a 74 28 29 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73  t().**.** encaps
19e5b 75 6c 61 74 65 20 74 68 65 20 63 6f 64 65 20 74  ulate the code t
19e5c 68 61 74 20 73 65 72 69 61 6c 69 7a 65 73 20 76  hat serializes v
19e5d 61 6c 75 65 73 20 66 6f 72 20 73 74 6f 72 61 67  alues for storag
19e5e 65 20 69 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 64  e in SQLite.** d
19e5f 61 74 61 20 61 6e 64 20 69 6e 64 65 78 20 72 65  ata and index re
19e60 63 6f 72 64 73 2e 20 45 61 63 68 20 73 65 72 69  cords. Each seri
19e61 61 6c 69 7a 65 64 20 76 61 6c 75 65 20 63 6f 6e  alized value con
19e62 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 27 73  sists of a.** 's
19e63 65 72 69 61 6c 2d 74 79 70 65 27 20 61 6e 64 20  erial-type' and 
19e64 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e 20  a blob of data. 
19e65 54 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20  The serial type 
19e66 69 73 20 61 6e 20 38 2d 62 79 74 65 20 75 6e 73  is an 8-byte uns
19e67 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72  igned.** integer
19e68 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 76 61  , stored as a va
19e69 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61  rint..**.** In a
19e6a 6e 20 53 51 4c 69 74 65 20 69 6e 64 65 78 20 72  n SQLite index r
19e6b 65 63 6f 72 64 2c 20 74 68 65 20 73 65 72 69 61  ecord, the seria
19e6c 6c 20 74 79 70 65 20 69 73 20 73 74 6f 72 65 64  l type is stored
19e6d 20 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65   directly before
19e6e 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66 20  .** the blob of 
19e6f 64 61 74 61 20 74 68 61 74 20 69 74 20 63 6f 72  data that it cor
19e70 72 65 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e 20  responds to. In 
19e71 61 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2c 20  a table record, 
19e72 61 6c 6c 20 73 65 72 69 61 6c 0a 2a 2a 20 74 79  all serial.** ty
19e73 70 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 61  pes are stored a
19e74 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
19e75 68 65 20 72 65 63 6f 72 64 2c 20 61 6e 64 20 74  he record, and t
19e76 68 65 20 62 6c 6f 62 73 20 6f 66 20 64 61 74 61  he blobs of data
19e77 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 2e 20   at.** the end. 
19e78 48 65 6e 63 65 20 74 68 65 73 65 20 66 75 6e 63  Hence these func
19e79 74 69 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65 20  tions allow the 
19e7a 63 61 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c 65  caller to handle
19e7b 20 74 68 65 0a 2a 2a 20 73 65 72 69 61 6c 2d 74   the.** serial-t
19e7c 79 70 65 20 61 6e 64 20 64 61 74 61 20 62 6c 6f  ype and data blo
19e7d 62 20 73 65 70 65 72 61 74 65 6c 79 2e 0a 2a 2a  b seperately..**
19e7e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
19e7f 67 20 74 61 62 6c 65 20 64 65 73 63 72 69 62 65  g table describe
19e80 73 20 74 68 65 20 76 61 72 69 6f 75 73 20 73 74  s the various st
19e81 6f 72 61 67 65 20 63 6c 61 73 73 65 73 20 66 6f  orage classes fo
19e82 72 20 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20  r data:.**.**   
19e83 73 65 72 69 61 6c 20 74 79 70 65 20 20 20 20 20  serial type     
19e84 20 20 20 62 79 74 65 73 20 6f 66 20 64 61 74 61     bytes of data
19e85 20 20 20 20 20 20 74 79 70 65 0a 2a 2a 20 20 20        type.**   
19e86 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
19e87 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
19e88 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --    ----------
19e89 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 30 20  -----.**      0 
19e8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e8b 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
19e8c 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 31 20   NULL.**      1 
19e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e8e 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
19e8f 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a   signed integer.
19e90 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 20 20  **      2       
19e91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 20                2 
19e92 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
19e93 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  d integer.**    
19e94 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 20    3             
19e95 20 20 20 20 20 20 20 20 33 20 20 20 20 20 20 20          3       
19e96 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
19e97 67 65 72 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  ger.**      4   
19e98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e99 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20 73    4            s
19e9a 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
19e9b 20 20 20 20 20 20 35 20 20 20 20 20 20 20 20 20        5         
19e9c 20 20 20 20 20 20 20 20 20 20 20 20 36 20 20 20              6   
19e9d 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20           signed 
19e9e 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
19e9f 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  6               
19ea0 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20        8         
19ea1 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65     signed intege
19ea2 72 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20  r.**      7     
19ea3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ea4 38 20 20 20 20 20 20 20 20 20 20 20 20 49 45 45  8            IEE
19ea5 45 20 66 6c 6f 61 74 0a 2a 2a 20 20 20 20 20 20  E float.**      
19ea6 38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  8               
19ea7 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20        0         
19ea8 20 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74     Integer const
19ea9 61 6e 74 20 30 0a 2a 2a 20 20 20 20 20 20 39 20  ant 0.**      9 
19eaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19eab 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
19eac 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   Integer constan
19ead 74 20 31 0a 2a 2a 20 20 20 20 20 31 30 2c 31 31  t 1.**     10,11
19eae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19eaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
19eb0 65 73 65 72 76 65 64 20 66 6f 72 20 65 78 70 61  eserved for expa
19eb1 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31  nsion.**    N>=1
19eb2 32 20 61 6e 64 20 65 76 65 6e 20 20 20 20 20 20  2 and even      
19eb3 20 28 4e 2d 31 32 29 2f 32 20 20 20 20 20 20 20   (N-12)/2       
19eb4 20 42 4c 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31   BLOB.**    N>=1
19eb5 33 20 61 6e 64 20 6f 64 64 20 20 20 20 20 20 20  3 and odd       
19eb6 20 28 4e 2d 31 33 29 2f 32 20 20 20 20 20 20 20   (N-13)/2       
19eb7 20 74 65 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20   text.**.** The 
19eb8 38 20 61 6e 64 20 39 20 74 79 70 65 73 20 77 65  8 and 9 types we
19eb9 72 65 20 61 64 64 65 64 20 69 6e 20 33 2e 33 2e  re added in 3.3.
19eba 30 2c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 34  0, file format 4
19ebb 2e 20 20 50 72 69 6f 72 20 76 65 72 73 69 6f 6e  .  Prior version
19ebc 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 77  s.** of SQLite w
19ebd 69 6c 6c 20 6e 6f 74 20 75 6e 64 65 72 73 74 61  ill not understa
19ebe 6e 64 20 74 68 6f 73 65 20 73 65 72 69 61 6c 20  nd those serial 
19ebf 74 79 70 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  types..*/../*.**
19ec0 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 72 69   Return the seri
19ec1 61 6c 2d 74 79 70 65 20 66 6f 72 20 74 68 65 20  al-type for the 
19ec2 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20  value stored in 
19ec3 70 4d 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  pMem..*/.SQLITE_
19ec4 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69  PRIVATE u32 sqli
19ec5 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
19ec6 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74  e(Mem *pMem, int
19ec7 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20   file_format){. 
19ec8 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d 65   int flags = pMe
19ec9 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20  m->flags;.  int 
19eca 6e 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 26  n;..  if( flags&
19ecb 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
19ecc 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
19ecd 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 49 6e  if( flags&MEM_In
19ece 74 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75  t ){.    /* Figu
19ecf 72 65 20 6f 75 74 20 77 68 65 74 68 65 72 20 74  re out whether t
19ed0 6f 20 75 73 65 20 31 2c 20 32 2c 20 34 2c 20 36  o use 1, 2, 4, 6
19ed1 20 6f 72 20 38 20 62 79 74 65 73 2e 20 2a 2f 0a   or 8 bytes. */.
19ed2 23 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 36  #   define MAX_6
19ed3 42 59 54 45 20 28 28 28 28 69 36 34 29 30 78 30  BYTE ((((i64)0x0
19ed4 30 30 30 38 30 30 30 29 3c 3c 33 32 29 2d 31 29  0008000)<<32)-1)
19ed5 0a 20 20 20 20 69 36 34 20 69 20 3d 20 70 4d 65  .    i64 i = pMe
19ed6 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 36 34 20  m->u.i;.    u64 
19ed7 75 3b 0a 20 20 20 20 69 66 28 20 66 69 6c 65 5f  u;.    if( file_
19ed8 66 6f 72 6d 61 74 3e 3d 34 20 26 26 20 28 69 26  format>=4 && (i&
19ed9 31 29 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 72  1)==i ){.      r
19eda 65 74 75 72 6e 20 38 2b 28 75 33 32 29 69 3b 0a  eturn 8+(u32)i;.
19edb 20 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69 3c      }.    u = i<
19edc 30 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20 20  0 ? -i : i;.    
19edd 69 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65 74  if( u<=127 ) ret
19ede 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 75  urn 1;.    if( u
19edf 3c 3d 33 32 37 36 37 20 29 20 72 65 74 75 72 6e  <=32767 ) return
19ee0 20 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 38   2;.    if( u<=8
19ee1 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e 20  388607 ) return 
19ee2 33 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32 31  3;.    if( u<=21
19ee3 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75 72  47483647 ) retur
19ee4 6e 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d  n 4;.    if( u<=
19ee5 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65 74 75  MAX_6BYTE ) retu
19ee6 72 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72 6e  rn 5;.    return
19ee7 20 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c   6;.  }.  if( fl
19ee8 61 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a  ags&MEM_Real ){.
19ee9 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20      return 7;.  
19eea 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  }.  assert( pMem
19eeb 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
19eec 65 64 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45 4d  ed || flags&(MEM
19eed 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29  _Str|MEM_Blob) )
19eee 3b 0a 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b  ;.  n = pMem->n;
19eef 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d  .  if( flags & M
19ef0 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 6e  EM_Zero ){.    n
19ef1 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72   += pMem->u.nZer
19ef2 6f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  o;.  }.  assert(
19ef3 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72   n>=0 );.  retur
19ef4 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b 20  n ((n*2) + 12 + 
19ef5 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29  ((flags&MEM_Str)
19ef6 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  !=0));.}../*.** 
19ef7 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74  Return the lengt
19ef8 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 63 6f  h of the data co
19ef9 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74  rresponding to t
19efa 68 65 20 73 75 70 70 6c 69 65 64 20 73 65 72 69  he supplied seri
19efb 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49  al-type..*/.SQLI
19efc 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19efd 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
19efe 54 79 70 65 4c 65 6e 28 75 33 32 20 73 65 72 69  TypeLen(u32 seri
19eff 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69 66 28 20  al_type){.  if( 
19f00 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20  serial_type>=12 
19f01 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 73  ){.    return (s
19f02 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32  erial_type-12)/2
19f03 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
19f04 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61  tatic const u8 a
19f05 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c  Size[] = { 0, 1,
19f06 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c 20   2, 3, 4, 6, 8, 
19f07 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b  8, 0, 0, 0, 0 };
19f08 0a 20 20 20 20 72 65 74 75 72 6e 20 61 53 69 7a  .    return aSiz
19f09 65 5b 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b 0a  e[serial_type];.
19f0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20    }.}../*.** If 
19f0b 77 65 20 61 72 65 20 6f 6e 20 61 6e 20 61 72 63  we are on an arc
19f0c 68 69 74 65 63 74 75 72 65 20 77 69 74 68 20 6d  hitecture with m
19f0d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f 61  ixed-endian floa
19f0e 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73 20  ting .** points 
19f0f 28 65 78 3a 20 41 52 4d 37 29 20 74 68 65 6e 20  (ex: ARM7) then 
19f10 73 77 61 70 20 74 68 65 20 6c 6f 77 65 72 20 34  swap the lower 4
19f11 20 62 79 74 65 73 20 77 69 74 68 20 74 68 65 20   bytes with the 
19f12 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 79 74 65  .** upper 4 byte
19f13 73 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72  s.  Return the r
19f14 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  esult..**.** For
19f15 20 6d 6f 73 74 20 61 72 63 68 69 74 65 63 74 75   most architectu
19f16 72 65 73 2c 20 74 68 69 73 20 69 73 20 61 20 6e  res, this is a n
19f17 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74  o-op..**.** (lat
19f18 65 72 29 3a 20 20 49 74 20 69 73 20 72 65 70 6f  er):  It is repo
19f19 72 74 65 64 20 74 6f 20 6d 65 20 74 68 61 74 20  rted to me that 
19f1a 74 68 65 20 6d 69 78 65 64 2d 65 6e 64 69 61 6e  the mixed-endian
19f1b 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41   problem.** on A
19f1c 52 4d 37 20 69 73 20 61 6e 20 69 73 73 75 65 20  RM7 is an issue 
19f1d 77 69 74 68 20 47 43 43 2c 20 6e 6f 74 20 77 69  with GCC, not wi
19f1e 74 68 20 74 68 65 20 41 52 4d 37 20 63 68 69 70  th the ARM7 chip
19f1f 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20 74  .  It seems.** t
19f20 68 61 74 20 65 61 72 6c 79 20 76 65 72 73 69 6f  hat early versio
19f21 6e 73 20 6f 66 20 47 43 43 20 73 74 6f 72 65 64  ns of GCC stored
19f22 20 74 68 65 20 74 77 6f 20 77 6f 72 64 73 20 6f   the two words o
19f23 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66 6c  f a 64-bit.** fl
19f24 6f 61 74 20 69 6e 20 74 68 65 20 77 72 6f 6e 67  oat in the wrong
19f25 20 6f 72 64 65 72 2e 20 20 41 6e 64 20 74 68 61   order.  And tha
19f26 74 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e  t error has been
19f27 20 70 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20 65   propagated.** e
19f28 76 65 72 20 73 69 6e 63 65 2e 20 20 54 68 65 20  ver since.  The 
19f29 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65 63  blame is not nec
19f2a 65 73 73 61 72 69 6c 79 20 77 69 74 68 20 47 43  essarily with GC
19f2b 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47 43  C, though..** GC
19f2c 43 20 6d 69 67 68 74 20 68 61 76 65 20 6a 75 73  C might have jus
19f2d 74 20 63 6f 70 79 69 6e 67 20 74 68 65 20 70 72  t copying the pr
19f2e 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72 69  oblem from a pri
19f2f 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20  or compiler..** 
19f30 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20 74  I am also told t
19f31 68 61 74 20 6e 65 77 65 72 20 76 65 72 73 69 6f  hat newer versio
19f32 6e 73 20 6f 66 20 47 43 43 20 74 68 61 74 20 66  ns of GCC that f
19f33 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65 72 65 6e  ollow a differen
19f34 74 0a 2a 2a 20 41 42 49 20 67 65 74 20 74 68 65  t.** ABI get the
19f35 20 62 79 74 65 20 6f 72 64 65 72 20 72 69 67 68   byte order righ
19f36 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70  t..**.** Develop
19f37 65 72 73 20 75 73 69 6e 67 20 53 51 4c 69 74 65  ers using SQLite
19f38 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f 75   on an ARM7 shou
19f39 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72  ld compile and r
19f3a 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61 70 70 6c  un their.** appl
19f3b 69 63 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d 44  ication using -D
19f3c 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 20 61  SQLITE_DEBUG=1 a
19f3d 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 57  t least once.  W
19f3e 69 74 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e 61  ith DEBUG.** ena
19f3f 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73 73 65 72  bled, some asser
19f40 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65 6e  ts below will en
19f41 73 75 72 65 20 74 68 61 74 20 74 68 65 20 62 79  sure that the by
19f42 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 66  te order of.** f
19f43 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
19f44 6c 75 65 73 20 69 73 20 63 6f 72 72 65 63 74 2e  lues is correct.
19f45 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38 2d  .**.** (2007-08-
19f46 33 30 29 20 20 46 72 61 6e 6b 20 76 61 6e 20 56  30)  Frank van V
19f47 75 67 74 20 68 61 73 20 73 74 75 64 69 65 64 20  ugt has studied 
19f48 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c 6f  this problem clo
19f49 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73 20  sely.** and has 
19f4a 73 65 6e 64 20 68 69 73 20 66 69 6e 64 69 6e 67  send his finding
19f4b 73 20 74 6f 20 74 68 65 20 53 51 4c 69 74 65 20  s to the SQLite 
19f4c 64 65 76 65 6c 6f 70 65 72 73 2e 20 20 46 72 61  developers.  Fra
19f4d 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20 74 68 61  nk.** writes tha
19f4e 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65 72  t some Linux ker
19f4f 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61 74  nels offer float
19f50 69 6e 67 20 70 6f 69 6e 74 20 68 61 72 64 77 61  ing point hardwa
19f51 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e 20  re.** emulation 
19f52 74 68 61 74 20 75 73 65 73 20 6f 6e 6c 79 20 33  that uses only 3
19f53 32 2d 62 69 74 20 6d 61 6e 74 69 73 73 61 73 20  2-bit mantissas 
19f54 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 75 6c  instead of a ful
19f55 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61 73  l .** 48-bits as
19f56 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
19f57 20 49 45 45 45 20 73 74 61 6e 64 61 72 64 2e 20   IEEE standard. 
19f58 20 28 54 68 69 73 20 69 73 20 74 68 65 0a 2a 2a   (This is the.**
19f59 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53 54   CONFIG_FPE_FAST
19f5a 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e  FPE option.)  On
19f5b 20 73 75 63 68 20 73 79 73 74 65 6d 73 2c 20 66   such systems, f
19f5c 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a  loating point.**
19f5d 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20 62   byte swapping b
19f5e 65 63 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d 70  ecomes very comp
19f5f 6c 69 63 61 74 65 64 2e 20 20 54 6f 20 61 76 6f  licated.  To avo
19f60 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20  id problems,.** 
19f61 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 62 79  the necessary by
19f62 74 65 20 73 77 61 70 70 69 6e 67 20 69 73 20 63  te swapping is c
19f63 61 72 72 69 65 64 20 6f 75 74 20 75 73 69 6e 67  arried out using
19f64 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65   a 64-bit intege
19f65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74 68 61 6e  r.** rather than
19f66 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 2e   a 64-bit float.
19f67 20 20 46 72 61 6e 6b 20 61 73 73 75 72 65 73 20    Frank assures 
19f68 75 73 20 74 68 61 74 20 74 68 65 20 63 6f 64 65  us that the code
19f69 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20 66   here.** works f
19f6a 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20 74 68 65  or him.  We, the
19f6b 20 64 65 76 65 6c 6f 70 65 72 73 2c 20 68 61 76   developers, hav
19f6c 65 20 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64 65  e no way to inde
19f6d 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65 72  pendently.** ver
19f6e 69 66 79 20 74 68 69 73 2c 20 62 75 74 20 46 72  ify this, but Fr
19f6f 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e 6f  ank seems to kno
19f70 77 20 77 68 61 74 20 68 65 20 69 73 20 74 61 6c  w what he is tal
19f71 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73 6f  king about.** so
19f72 20 77 65 20 74 72 75 73 74 20 68 69 6d 2e 0a 2a   we trust him..*
19f73 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
19f74 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42  MIXED_ENDIAN_64B
19f75 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63 20  IT_FLOAT.static 
19f76 75 36 34 20 66 6c 6f 61 74 53 77 61 70 28 75 36  u64 floatSwap(u6
19f77 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b  4 in){.  union {
19f78 0a 20 20 20 20 75 36 34 20 72 3b 0a 20 20 20 20  .    u64 r;.    
19f79 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b  u32 i[2];.  } u;
19f7a 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e 72  .  u32 t;..  u.r
19f7b 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e 69   = in;.  t = u.i
19f7c 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d 20  [0];.  u.i[0] = 
19f7d 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d  u.i[1];.  u.i[1]
19f7e 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 6e 20 75   = t;.  return u
19f7f 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20 73  .r;.}.# define s
19f80 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c  wapMixedEndianFl
19f81 6f 61 74 28 58 29 20 20 58 20 3d 20 66 6c 6f 61  oat(X)  X = floa
19f82 74 53 77 61 70 28 58 29 0a 23 65 6c 73 65 0a 23  tSwap(X).#else.#
19f83 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65   define swapMixe
19f84 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 0a  dEndianFloat(X).
19f85 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72  #endif../*.** Wr
19f86 69 74 65 20 74 68 65 20 73 65 72 69 61 6c 69 7a  ite the serializ
19f87 65 64 20 64 61 74 61 20 62 6c 6f 62 20 66 6f 72  ed data blob for
19f88 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   the value store
19f89 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20 0a  d in pMem into .
19f8a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 20 61 73  ** buf. It is as
19f8b 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 63  sumed that the c
19f8c 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63 61  aller has alloca
19f8d 74 65 64 20 73 75 66 66 69 63 69 65 6e 74 20 73  ted sufficient s
19f8e 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  pace..** Return 
19f8f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
19f90 74 65 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a  tes written..**.
19f91 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 65 20 61  ** nBuf is the a
19f92 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 6c  mount of space l
19f93 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20 6e  eft in buf[].  n
19f94 42 75 66 20 6d 75 73 74 20 61 6c 77 61 79 73 20  Buf must always 
19f95 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f 75  be.** large enou
19f96 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 65  gh to hold the e
19f97 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 20 45 78  ntire field.  Ex
19f98 63 65 70 74 2c 20 69 66 20 74 68 65 20 66 69 65  cept, if the fie
19f99 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62 20  ld is.** a blob 
19f9a 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c 6c  with a zero-fill
19f9b 65 64 20 74 61 69 6c 2c 20 74 68 65 6e 20 62 75  ed tail, then bu
19f9c 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 6a 75 73  f[] might be jus
19f9d 74 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 73  t the right.** s
19f9e 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 76 65 72  ize to hold ever
19f9f 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 66 6f  ything except fo
19fa0 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65  r the zero-fille
19fa1 64 20 74 61 69 6c 2e 20 20 49 66 20 62 75 66 5b  d tail.  If buf[
19fa2 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69 67  ].** is only big
19fa3 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20   enough to hold 
19fa4 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65  the non-zero pre
19fa5 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 77  fix, then only w
19fa6 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 70 72 65  rite that.** pre
19fa7 66 69 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20  fix into buf[]. 
19fa8 20 42 75 74 20 69 66 20 62 75 66 5b 5d 20 69 73   But if buf[] is
19fa9 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f   large enough to
19faa 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a 2a   hold both the.*
19fab 2a 20 70 72 65 66 69 78 20 61 6e 64 20 74 68 65  * prefix and the
19fac 20 74 61 69 6c 20 74 68 65 6e 20 77 72 69 74 65   tail then write
19fad 20 74 68 65 20 70 72 65 66 69 78 20 61 6e 64 20   the prefix and 
19fae 73 65 74 20 74 68 65 20 74 61 69 6c 20 74 6f 20  set the tail to 
19faf 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a  all.** zeros..**
19fb0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
19fb1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61  umber of bytes a
19fb2 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20  ctually written 
19fb3 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68 65  into buf[].  The
19fb4 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79   number.** of by
19fb5 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d  tes in the zero-
19fb6 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73 20 69  filled tail is i
19fb7 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 72  ncluded in the r
19fb8 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c 79  eturn value only
19fb9 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62 79 74  .** if those byt
19fba 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64 20 69  es were zeroed i
19fbb 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c  n buf[]..*/ .SQL
19fbc 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19fbd 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
19fbe 6c 50 75 74 28 75 38 20 2a 62 75 66 2c 20 69 6e  lPut(u8 *buf, in
19fbf 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d 65  t nBuf, Mem *pMe
19fc0 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d  m, int file_form
19fc1 61 74 29 7b 0a 20 20 75 33 32 20 73 65 72 69 61  at){.  u32 seria
19fc2 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33  l_type = sqlite3
19fc3 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70  VdbeSerialType(p
19fc4 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74  Mem, file_format
19fc5 29 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20  );.  int len;.. 
19fc6 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 6e 64 20   /* Integer and 
19fc7 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73 65  Real */.  if( se
19fc8 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26 20  rial_type<=7 && 
19fc9 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 20 29 7b  serial_type>0 ){
19fca 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 20 20 20  .    u64 v;.    
19fcb 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28 20 73  int i;.    if( s
19fcc 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29 7b  erial_type==7 ){
19fcd 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
19fce 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f 66  izeof(v)==sizeof
19fcf 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20 20  (pMem->r) );.   
19fd0 20 20 20 6d 65 6d 63 70 79 28 26 76 2c 20 26 70     memcpy(&v, &p
19fd1 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 76  Mem->r, sizeof(v
19fd2 29 29 3b 0a 20 20 20 20 20 20 73 77 61 70 4d 69  ));.      swapMi
19fd3 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 76  xedEndianFloat(v
19fd4 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
19fd5 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e      v = pMem->u.
19fd6 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 6e  i;.    }.    len
19fd7 20 3d 20 69 20 3d 20 73 71 6c 69 74 65 33 56 64   = i = sqlite3Vd
19fd8 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
19fd9 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20  serial_type);.  
19fda 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d 6e    assert( len<=n
19fdb 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65  Buf );.    while
19fdc 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62  ( i-- ){.      b
19fdd 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30  uf[i] = (u8)(v&0
19fde 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e  xFF);.      v >>
19fdf 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  = 8;.    }.    r
19fe0 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a  eturn len;.  }..
19fe1 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62    /* String or b
19fe2 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72  lob */.  if( ser
19fe3 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a  ial_type>=12 ){.
19fe4 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
19fe5 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c  ->n + ((pMem->fl
19fe6 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f  ags & MEM_Zero)?
19fe7 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29  pMem->u.nZero:0)
19fe8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d  .             ==
19fe9 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
19fea 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c  alTypeLen(serial
19feb 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 20 61 73  _type) );.    as
19fec 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e  sert( pMem->n<=n
19fed 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 6e 20 3d  Buf );.    len =
19fee 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 20 6d 65   pMem->n;.    me
19fef 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 6d 2d 3e  mcpy(buf, pMem->
19ff0 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 69 66 28  z, len);.    if(
19ff1 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
19ff2 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20  EM_Zero ){.     
19ff3 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e   len += pMem->u.
19ff4 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20 69 66 28  nZero;.      if(
19ff5 20 6c 65 6e 3e 6e 42 75 66 20 29 7b 0a 20 20 20   len>nBuf ){.   
19ff6 20 20 20 20 20 6c 65 6e 20 3d 20 6e 42 75 66 3b       len = nBuf;
19ff7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
19ff8 65 6d 73 65 74 28 26 62 75 66 5b 70 4d 65 6d 2d  emset(&buf[pMem-
19ff9 3e 6e 5d 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65 6d  >n], 0, len-pMem
19ffa 2d 3e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ->n);.    }.    
19ffb 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a  return len;.  }.
19ffc 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 6f  .  /* NULL or co
19ffd 6e 73 74 61 6e 74 73 20 30 20 6f 72 20 31 20 2a  nstants 0 or 1 *
19ffe 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  /.  return 0;.}.
19fff 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69  ./*.** Deseriali
1a000 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c 6f 62  ze the data blob
1a001 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 62   pointed to by b
1a002 75 66 20 61 73 20 73 65 72 69 61 6c 20 74 79 70  uf as serial typ
1a003 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0a 2a 2a  e serial_type.**
1a004 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
1a005 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d 2e 20 20  esult in pMem.  
1a006 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1a007 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e  r of bytes read.
1a008 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/ .SQLITE_PRIV
1a009 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1a00a 64 62 65 53 65 72 69 61 6c 47 65 74 28 0a 20 20  dbeSerialGet(.  
1a00b 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1a00c 68 61 72 20 2a 62 75 66 2c 20 20 20 20 20 2f 2a  har *buf,     /*
1a00d 20 42 75 66 66 65 72 20 74 6f 20 64 65 73 65 72   Buffer to deser
1a00e 69 61 6c 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a 20  ialize from */. 
1a00f 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
1a010 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1a011 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 74 6f  * Serial type to
1a012 20 64 65 73 65 72 69 61 6c 69 7a 65 20 2a 2f 0a   deserialize */.
1a013 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20    Mem *pMem     
1a014 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a015 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74  /* Memory cell t
1a016 6f 20 77 72 69 74 65 20 76 61 6c 75 65 20 69 6e  o write value in
1a017 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63  to */.){.  switc
1a018 68 28 20 73 65 72 69 61 6c 5f 74 79 70 65 20 29  h( serial_type )
1a019 7b 0a 20 20 20 20 63 61 73 65 20 31 30 3a 20 20  {.    case 10:  
1a01a 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66 6f 72   /* Reserved for
1a01b 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f 0a 20   future use */. 
1a01c 20 20 20 63 61 73 65 20 31 31 3a 20 20 20 2f 2a     case 11:   /*
1a01d 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 75   Reserved for fu
1a01e 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20  ture use */.    
1a01f 63 61 73 65 20 30 3a 20 7b 20 20 2f 2a 20 4e 55  case 0: {  /* NU
1a020 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  LL */.      pMem
1a021 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  ->flags = MEM_Nu
1a022 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ll;.      break;
1a023 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1a024 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20 73  1: { /* 1-byte s
1a025 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
1a026 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
1a027 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 29   = (signed char)
1a028 62 75 66 5b 30 5d 3b 0a 20 20 20 20 20 20 70 4d  buf[0];.      pM
1a029 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1a02a 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Int;.      retur
1a02b 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
1a02c 61 73 65 20 32 3a 20 7b 20 2f 2a 20 32 2d 62 79  ase 2: { /* 2-by
1a02d 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
1a02e 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d  r */.      pMem-
1a02f 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64  >u.i = (((signed
1a030 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38   char)buf[0])<<8
1a031 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20  ) | buf[1];.    
1a032 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1a033 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72  MEM_Int;.      r
1a034 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a 20  eturn 2;.    }. 
1a035 20 20 20 63 61 73 65 20 33 3a 20 7b 20 2f 2a 20     case 3: { /* 
1a036 33 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  3-byte signed in
1a037 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70  teger */.      p
1a038 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69  Mem->u.i = (((si
1a039 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d  gned char)buf[0]
1a03a 29 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 31 5d  )<<16) | (buf[1]
1a03b 3c 3c 38 29 20 7c 20 62 75 66 5b 32 5d 3b 0a 20  <<8) | buf[2];. 
1a03c 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1a03d 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1a03e 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20    return 3;.    
1a03f 7d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b 20  }.    case 4: { 
1a040 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 4-byte signed
1a041 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
1a042 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 62    pMem->u.i = (b
1a043 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75  uf[0]<<24) | (bu
1a044 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66  f[1]<<16) | (buf
1a045 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d  [2]<<8) | buf[3]
1a046 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
1a047 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1a048 20 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20       return 4;. 
1a049 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 35 3a     }.    case 5:
1a04a 20 7b 20 2f 2a 20 36 2d 62 79 74 65 20 73 69 67   { /* 6-byte sig
1a04b 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20  ned integer */. 
1a04c 20 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28       u64 x = (((
1a04d 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b  signed char)buf[
1a04e 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d  0])<<8) | buf[1]
1a04f 3b 0a 20 20 20 20 20 20 75 33 32 20 79 20 3d 20  ;.      u32 y = 
1a050 28 62 75 66 5b 32 5d 3c 3c 32 34 29 20 7c 20 28  (buf[2]<<24) | (
1a051 62 75 66 5b 33 5d 3c 3c 31 36 29 20 7c 20 28 62  buf[3]<<16) | (b
1a052 75 66 5b 34 5d 3c 3c 38 29 20 7c 20 62 75 66 5b  uf[4]<<8) | buf[
1a053 35 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 78  5];.      x = (x
1a054 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 20  <<32) | y;.     
1a055 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69   pMem->u.i = *(i
1a056 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 70 4d  64*)&x;.      pM
1a057 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1a058 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Int;.      retur
1a059 6e 20 36 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 6;.    }.    c
1a05a 61 73 65 20 36 3a 20 20 20 2f 2a 20 38 2d 62 79  ase 6:   /* 8-by
1a05b 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
1a05c 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 37 3a  r */.    case 7:
1a05d 20 7b 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61 74   { /* IEEE float
1a05e 69 6e 67 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20  ing point */.   
1a05f 20 20 20 75 36 34 20 78 3b 0a 20 20 20 20 20 20     u64 x;.      
1a060 75 33 32 20 79 3b 0a 23 69 66 20 21 64 65 66 69  u32 y;.#if !defi
1a061 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21  ned(NDEBUG) && !
1a062 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1a063 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
1a064 4e 54 29 0a 20 20 20 20 20 20 2f 2a 20 56 65 72  NT).      /* Ver
1a065 69 66 79 20 74 68 61 74 20 69 6e 74 65 67 65 72  ify that integer
1a066 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67 20 70  s and floating p
1a067 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73 65 20  oint values use 
1a068 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 20 2a  the same.      *
1a069 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 4f  * byte order.  O
1a06a 72 2c 20 74 68 61 74 20 69 66 20 53 51 4c 49 54  r, that if SQLIT
1a06b 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36  E_MIXED_ENDIAN_6
1a06c 34 42 49 54 5f 46 4c 4f 41 54 20 69 73 0a 20 20  4BIT_FLOAT is.  
1a06d 20 20 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 74      ** defined t
1a06e 68 61 74 20 36 34 2d 62 69 74 20 66 6c 6f 61 74  hat 64-bit float
1a06f 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73  ing point values
1a070 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 69 78 65   really are mixe
1a071 64 0a 20 20 20 20 20 20 2a 2a 20 65 6e 64 69 61  d.      ** endia
1a072 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n..      */.    
1a073 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
1a074 36 34 20 74 31 20 3d 20 28 28 75 36 34 29 30 78  64 t1 = ((u64)0x
1a075 33 66 66 30 30 30 30 30 29 3c 3c 33 32 3b 0a 20  3ff00000)<<32;. 
1a076 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
1a077 74 20 64 6f 75 62 6c 65 20 72 31 20 3d 20 31 2e  t double r1 = 1.
1a078 30 3b 0a 20 20 20 20 20 20 75 36 34 20 74 32 20  0;.      u64 t2 
1a079 3d 20 74 31 3b 0a 20 20 20 20 20 20 73 77 61 70  = t1;.      swap
1a07a 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74  MixedEndianFloat
1a07b 28 74 32 29 3b 0a 20 20 20 20 20 20 61 73 73 65  (t2);.      asse
1a07c 72 74 28 20 73 69 7a 65 6f 66 28 72 31 29 3d 3d  rt( sizeof(r1)==
1a07d 73 69 7a 65 6f 66 28 74 32 29 20 26 26 20 6d 65  sizeof(t2) && me
1a07e 6d 63 6d 70 28 26 72 31 2c 20 26 74 32 2c 20 73  mcmp(&r1, &t2, s
1a07f 69 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29 3b  izeof(r1))==0 );
1a080 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 78  .#endif..      x
1a081 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20   = (buf[0]<<24) 
1a082 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c  | (buf[1]<<16) |
1a083 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62   (buf[2]<<8) | b
1a084 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20 3d  uf[3];.      y =
1a085 20 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c 20   (buf[4]<<24) | 
1a086 28 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 28  (buf[5]<<16) | (
1a087 62 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 66  buf[6]<<8) | buf
1a088 5b 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28  [7];.      x = (
1a089 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20  x<<32) | y;.    
1a08a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
1a08b 65 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20 20  e==6 ){.        
1a08c 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36  pMem->u.i = *(i6
1a08d 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20 70  4*)&x;.        p
1a08e 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1a08f 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73  _Int;.      }els
1a090 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e{.        asser
1a091 74 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38 20  t( sizeof(x)==8 
1a092 26 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e  && sizeof(pMem->
1a093 72 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20 20  r)==8 );.       
1a094 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e   swapMixedEndian
1a095 46 6c 6f 61 74 28 78 29 3b 0a 20 20 20 20 20 20  Float(x);.      
1a096 20 20 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d 3e    memcpy(&pMem->
1a097 72 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29  r, &x, sizeof(x)
1a098 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  );.        pMem-
1a099 3e 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33  >flags = sqlite3
1a09a 49 73 4e 61 4e 28 70 4d 65 6d 2d 3e 72 29 20 3f  IsNaN(pMem->r) ?
1a09b 20 4d 45 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d 5f   MEM_Null : MEM_
1a09c 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Real;.      }.  
1a09d 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20      return 8;.  
1a09e 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38 3a 20    }.    case 8: 
1a09f 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 30 20     /* Integer 0 
1a0a0 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a 20 7b  */.    case 9: {
1a0a1 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31 20 2a    /* Integer 1 *
1a0a2 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  /.      pMem->u.
1a0a3 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70 65 2d  i = serial_type-
1a0a4 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  8;.      pMem->f
1a0a5 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1a0a6 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1a0a7 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
1a0a8 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6c  t: {.      int l
1a0a9 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74 79 70  en = (serial_typ
1a0aa 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20 20 70  e-12)/2;.      p
1a0ab 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a  Mem->z = (char *
1a0ac 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d 65 6d  )buf;.      pMem
1a0ad 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  ->n = len;.     
1a0ae 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b   pMem->xDel = 0;
1a0af 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69 61  .      if( seria
1a0b0 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b 0a 20  l_type&0x01 ){. 
1a0b1 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61         pMem->fla
1a0b2 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 4d  gs = MEM_Str | M
1a0b3 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20  EM_Ephem;.      
1a0b4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
1a0b5 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1a0b6 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70 68 65  _Blob | MEM_Ephe
1a0b7 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  m;.      }.     
1a0b8 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 20   return len;.   
1a0b9 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1a0ba 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 76  0;.}.../*.** Giv
1a0bb 65 6e 20 74 68 65 20 6e 4b 65 79 2d 62 79 74 65  en the nKey-byte
1a0bc 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72   encoding of a r
1a0bd 65 63 6f 72 64 20 69 6e 20 70 4b 65 79 5b 5d 2c  ecord in pKey[],
1a0be 20 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 72 65   parse the.** re
1a0bf 63 6f 72 64 20 69 6e 74 6f 20 61 20 55 6e 70 61  cord into a Unpa
1a0c0 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72 75 63  ckedRecord struc
1a0c1 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 61 20  ture.  Return a 
1a0c2 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68  pointer to.** th
1a0c3 61 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  at structure..**
1a0c4 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  .** The calling 
1a0c5 66 75 6e 63 74 69 6f 6e 20 6d 69 67 68 74 20 70  function might p
1a0c6 72 6f 76 69 64 65 20 73 7a 53 70 61 63 65 20 62  rovide szSpace b
1a0c7 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a  ytes of memory.*
1a0c8 2a 20 73 70 61 63 65 20 61 74 20 70 53 70 61 63  * space at pSpac
1a0c9 65 2e 20 20 54 68 69 73 20 73 70 61 63 65 20 63  e.  This space c
1a0ca 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 68 6f  an be used to ho
1a0cb 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 0a  ld the returned.
1a0cc 2a 2a 20 56 44 62 65 50 61 72 73 65 64 52 65 63  ** VDbeParsedRec
1a0cd 6f 72 64 20 73 74 72 75 63 74 75 72 65 20 69 66  ord structure if
1a0ce 20 69 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f   it is large eno
1a0cf 75 67 68 2e 20 20 49 66 20 69 74 20 69 73 0a 2a  ugh.  If it is.*
1a0d0 2a 20 6e 6f 74 20 62 69 67 20 65 6e 6f 75 67 68  * not big enough
1a0d1 2c 20 73 70 61 63 65 20 69 73 20 6f 62 74 61 69  , space is obtai
1a0d2 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
1a0d3 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a  _malloc()..**.**
1a0d4 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   The returned st
1a0d5 72 75 63 74 75 72 65 20 73 68 6f 75 6c 64 20 62  ructure should b
1a0d6 65 20 63 6c 6f 73 65 64 20 62 79 20 61 20 63 61  e closed by a ca
1a0d7 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ll to.** sqlite3
1a0d8 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b  VdbeDeleteUnpack
1a0d9 65 64 52 65 63 6f 72 64 28 29 2e 0a 2a 2f 20 0a  edRecord()..*/ .
1a0da 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55  SQLITE_PRIVATE U
1a0db 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73  npackedRecord *s
1a0dc 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64  qlite3VdbeRecord
1a0dd 55 6e 70 61 63 6b 28 0a 20 20 4b 65 79 49 6e 66  Unpack(.  KeyInf
1a0de 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20  o *pKeyInfo,    
1a0df 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
1a0e0 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f 72 64  about the record
1a0e1 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 69 6e 74   format */.  int
1a0e2 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20   nKey,          
1a0e3 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
1a0e4 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72 64  he binary record
1a0e5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
1a0e6 20 2a 70 4b 65 79 2c 20 20 20 20 20 20 2f 2a 20   *pKey,      /* 
1a0e7 54 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72  The binary recor
1a0e8 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 53 70  d */.  char *pSp
1a0e9 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ace,          /*
1a0ea 20 55 6e 61 6c 69 67 6e 65 64 20 73 70 61 63 65   Unaligned space
1a0eb 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f   available to ho
1a0ec 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f  ld the object */
1a0ed 0a 20 20 69 6e 74 20 73 7a 53 70 61 63 65 20 20  .  int szSpace  
1a0ee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
1a0ef 65 20 6f 66 20 70 53 70 61 63 65 5b 5d 20 69 6e  e of pSpace[] in
1a0f0 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63   bytes */.){.  c
1a0f1 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1a0f2 61 72 20 2a 61 4b 65 79 20 3d 20 28 63 6f 6e 73  ar *aKey = (cons
1a0f3 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1a0f4 2a 29 70 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b  *)pKey;.  Unpack
1a0f5 65 64 52 65 63 6f 72 64 20 2a 70 3b 20 20 2f 2a  edRecord *p;  /*
1a0f6 20 54 68 65 20 75 6e 70 61 63 6b 65 64 20 72 65   The unpacked re
1a0f7 63 6f 72 64 20 74 68 61 74 20 77 65 20 77 69 6c  cord that we wil
1a0f8 6c 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e  l return */.  in
1a0f9 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
1a0fa 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 73 70 61 63    /* Memory spac
1a0fb 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64  e needed to hold
1a0fc 20 70 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a   p, in bytes */.
1a0fd 20 20 69 6e 74 20 64 3b 0a 20 20 75 33 32 20 69    int d;.  u32 i
1a0fe 64 78 3b 0a 20 20 75 31 36 20 75 3b 20 20 20 20  dx;.  u16 u;    
1a0ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73            /* Uns
1a100 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74  igned loop count
1a101 65 72 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64  er */.  u32 szHd
1a102 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a  r;.  Mem *pMem;.
1a103 20 20 69 6e 74 20 6e 4f 66 66 3b 20 20 20 20 20    int nOff;     
1a104 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 61 73        /* Increas
1a105 65 20 70 53 70 61 63 65 20 62 79 20 74 68 69 73  e pSpace by this
1a106 20 6d 75 63 68 20 74 6f 20 38 2d 62 79 74 65 20   much to 8-byte 
1a107 61 6c 69 67 6e 20 69 74 20 2a 2f 0a 20 20 0a 20  align it */.  . 
1a108 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 77 61 6e 74   /*.  ** We want
1a109 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 70 6f   to shift the po
1a10a 69 6e 74 65 72 20 70 53 70 61 63 65 20 75 70 20  inter pSpace up 
1a10b 73 75 63 68 20 74 68 61 74 20 69 74 20 69 73 20  such that it is 
1a10c 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 2e 0a  8-byte aligned..
1a10d 20 20 2a 2a 20 54 68 75 73 2c 20 77 65 20 6e 65    ** Thus, we ne
1a10e 65 64 20 74 6f 20 63 61 6c 63 75 6c 61 74 65 20  ed to calculate 
1a10f 61 20 76 61 6c 75 65 2c 20 6e 4f 66 66 2c 20 62  a value, nOff, b
1a110 65 74 77 65 65 6e 20 30 20 61 6e 64 20 37 2c 20  etween 0 and 7, 
1a111 74 6f 20 73 68 69 66 74 20 0a 20 20 2a 2a 20 69  to shift .  ** i
1a112 74 20 62 79 2e 20 20 49 66 20 70 53 70 61 63 65  t by.  If pSpace
1a113 20 69 73 20 61 6c 72 65 61 64 79 20 38 2d 62 79   is already 8-by
1a114 74 65 20 61 6c 69 67 6e 65 64 2c 20 6e 4f 66 66  te aligned, nOff
1a115 20 73 68 6f 75 6c 64 20 62 65 20 7a 65 72 6f 2e   should be zero.
1a116 0a 20 20 2a 2f 0a 20 20 6e 4f 66 66 20 3d 20 28  .  */.  nOff = (
1a117 38 20 2d 20 28 53 51 4c 49 54 45 5f 50 54 52 5f  8 - (SQLITE_PTR_
1a118 54 4f 5f 49 4e 54 28 70 53 70 61 63 65 29 20 26  TO_INT(pSpace) &
1a119 20 37 29 29 20 26 20 37 3b 0a 20 20 70 53 70 61   7)) & 7;.  pSpa
1a11a 63 65 20 2b 3d 20 6e 4f 66 66 3b 0a 20 20 73 7a  ce += nOff;.  sz
1a11b 53 70 61 63 65 20 2d 3d 20 6e 4f 66 66 3b 0a 20  Space -= nOff;. 
1a11c 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28   nByte = ROUND8(
1a11d 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52  sizeof(UnpackedR
1a11e 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66  ecord)) + sizeof
1a11f 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f 2d  (Mem)*(pKeyInfo-
1a120 3e 6e 46 69 65 6c 64 2b 31 29 3b 0a 20 20 69 66  >nField+1);.  if
1a121 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65 20  ( nByte>szSpace 
1a122 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  ){.    p = sqlit
1a123 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4b  e3DbMallocRaw(pK
1a124 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 74  eyInfo->db, nByt
1a125 65 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30  e);.    if( p==0
1a126 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
1a127 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41   p->flags = UNPA
1a128 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 7c  CKED_NEED_FREE |
1a129 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44   UNPACKED_NEED_D
1a12a 45 53 54 52 4f 59 3b 0a 20 20 7d 65 6c 73 65 7b  ESTROY;.  }else{
1a12b 0a 20 20 20 20 70 20 3d 20 28 55 6e 70 61 63 6b  .    p = (Unpack
1a12c 65 64 52 65 63 6f 72 64 2a 29 70 53 70 61 63 65  edRecord*)pSpace
1a12d 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d  ;.    p->flags =
1a12e 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44   UNPACKED_NEED_D
1a12f 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 2d  ESTROY;.  }.  p-
1a130 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79  >pKeyInfo = pKey
1a131 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c  Info;.  p->nFiel
1a132 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  d = pKeyInfo->nF
1a133 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 2d 3e 61  ield + 1;.  p->a
1a134 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 28 4d 65  Mem = pMem = (Me
1a135 6d 2a 29 26 28 28 63 68 61 72 2a 29 70 29 5b 52  m*)&((char*)p)[R
1a136 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70  OUND8(sizeof(Unp
1a137 61 63 6b 65 64 52 65 63 6f 72 64 29 29 5d 3b 0a  ackedRecord))];.
1a138 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
1a139 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70  BYTE_ALIGNMENT(p
1a13a 4d 65 6d 29 20 29 3b 0a 20 20 69 64 78 20 3d 20  Mem) );.  idx = 
1a13b 67 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79  getVarint32(aKey
1a13c 2c 20 73 7a 48 64 72 29 3b 0a 20 20 64 20 3d 20  , szHdr);.  d = 
1a13d 73 7a 48 64 72 3b 0a 20 20 75 20 3d 20 30 3b 0a  szHdr;.  u = 0;.
1a13e 20 20 77 68 69 6c 65 28 20 69 64 78 3c 73 7a 48    while( idx<szH
1a13f 64 72 20 26 26 20 75 3c 70 2d 3e 6e 46 69 65 6c  dr && u<p->nFiel
1a140 64 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 72  d ){.    u32 ser
1a141 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20 20 20 69  ial_type;..    i
1a142 64 78 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33  dx += getVarint3
1a143 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73 65  2(&aKey[idx], se
1a144 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20  rial_type);.    
1a145 69 66 28 20 64 3e 3d 6e 4b 65 79 20 26 26 20 73  if( d>=nKey && s
1a146 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1a147 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74  TypeLen(serial_t
1a148 79 70 65 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a  ype)>0 ) break;.
1a149 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
1a14a 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20  pKeyInfo->enc;. 
1a14b 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b     pMem->db = pK
1a14c 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20  eyInfo->db;.    
1a14d 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b  pMem->flags = 0;
1a14e 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  .    pMem->zMall
1a14f 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d  oc = 0;.    d +=
1a150 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1a151 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20  alGet(&aKey[d], 
1a152 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65  serial_type, pMe
1a153 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a  m);.    pMem++;.
1a154 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61      u++;.  }.  a
1a155 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e  ssert( u<=pKeyIn
1a156 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29  fo->nField + 1 )
1a157 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20  ;.  p->nField = 
1a158 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69  u;.  return (voi
1a159 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  d*)p;.}../*.** T
1a15a 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74  his routine dest
1a15b 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52  roys a UnpackedR
1a15c 65 63 6f 72 64 20 6f 62 6a 65 63 74 0a 2a 2f 0a  ecord object.*/.
1a15d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a15e 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
1a15f 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
1a160 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f  ord(UnpackedReco
1a161 72 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  rd *p){.  if( p 
1a162 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c  ){.    if( p->fl
1a163 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e  ags & UNPACKED_N
1a164 45 45 44 5f 44 45 53 54 52 4f 59 20 29 7b 0a 20  EED_DESTROY ){. 
1a165 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
1a166 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20    Mem *pMem;.   
1a167 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d     for(i=0, pMem
1a168 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e  =p->aMem; i<p->n
1a169 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d  Field; i++, pMem
1a16a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
1a16b 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29   pMem->zMalloc )
1a16c 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
1a16d 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1a16e 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20  e(pMem);.       
1a16f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1a170 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67  .    if( p->flag
1a171 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  s & UNPACKED_NEE
1a172 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 20 20  D_FREE ){.      
1a173 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d  sqlite3DbFree(p-
1a174 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70  >pKeyInfo->db, p
1a175 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
1a176 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1a177 69 6f 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 65  ion compares the
1a178 20 74 77 6f 20 74 61 62 6c 65 20 72 6f 77 73 20   two table rows 
1a179 6f 72 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73  or index records
1a17a 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79  .** specified by
1a17b 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20   {nKey1, pKey1} 
1a17c 61 6e 64 20 70 50 4b 65 79 32 2e 20 20 49 74 20  and pPKey2.  It 
1a17d 72 65 74 75 72 6e 73 20 61 20 6e 65 67 61 74 69  returns a negati
1a17e 76 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70  ve, zero.** or p
1a17f 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 20  ositive integer 
1a180 69 66 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20  if key1 is less 
1a181 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f  than, equal to o
1a182 72 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68  r .** greater th
1a183 61 6e 20 6b 65 79 32 2e 20 20 54 68 65 20 7b 6e  an key2.  The {n
1a184 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65 79  Key1, pKey1} key
1a185 20 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62 0a   must be a blob.
1a186 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 74 68  ** created by th
1a187 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f   OP_MakeRecord o
1a188 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42  pcode of the VDB
1a189 45 2e 20 20 54 68 65 20 70 50 4b 65 79 32 0a 2a  E.  The pPKey2.*
1a18a 2a 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20  * key must be a 
1a18b 70 61 72 73 65 64 20 6b 65 79 20 73 75 63 68 20  parsed key such 
1a18c 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  as obtained from
1a18d 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 50  .** sqlite3VdbeP
1a18e 61 72 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a 2a  arseRecord..**.*
1a18f 2a 20 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20  * Key1 and Key2 
1a190 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 63  do not have to c
1a191 6f 6e 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20  ontain the same 
1a192 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73  number of fields
1a193 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 74  ..** The key wit
1a194 68 20 66 65 77 65 72 20 66 69 65 6c 64 73 20 69  h fewer fields i
1a195 73 20 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 72  s usually compar
1a196 65 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65  es less than the
1a197 20 0a 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 2e   .** longer key.
1a198 20 20 48 6f 77 65 76 65 72 20 69 66 20 74 68 65    However if the
1a199 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45   UNPACKED_INCRKE
1a19a 59 20 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 79  Y flags in pPKey
1a19b 32 20 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64 20  2 is set.** and 
1a19c 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69  the common prefi
1a19d 78 65 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74  xes are equal, t
1a19e 68 65 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 73  hen key1 is less
1a19f 20 74 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20 4f   than key2..** O
1a1a0 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45  r if the UNPACKE
1a1a1 44 5f 4d 41 54 43 48 5f 50 52 45 46 49 58 20 66  D_MATCH_PREFIX f
1a1a2 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 74  lag is set and t
1a1a3 68 65 20 70 72 65 66 69 78 65 73 20 61 72 65 0a  he prefixes are.
1a1a4 2a 2a 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 74  ** equal, then t
1a1a5 68 65 20 6b 65 79 73 20 61 72 65 20 63 6f 6e 73  he keys are cons
1a1a6 69 64 65 72 65 64 20 74 6f 20 62 65 20 65 71 75  idered to be equ
1a1a7 61 6c 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61  al and.** the pa
1a1a8 72 74 73 20 62 65 79 6f 6e 64 20 74 68 65 20 63  rts beyond the c
1a1a9 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20 61 72 65  ommon prefix are
1a1aa 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   ignored..**.** 
1a1ab 49 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f  If the UNPACKED_
1a1ac 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 66 6c 61  IGNORE_ROWID fla
1a1ad 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74  g is set, then t
1a1ae 68 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a  he last byte of.
1a1af 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 6f 66  ** the header of
1a1b0 20 70 4b 65 79 31 20 69 73 20 69 67 6e 6f 72 65   pKey1 is ignore
1a1b1 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65  d.  It is assume
1a1b2 64 20 74 68 61 74 20 70 4b 65 79 31 20 69 73 0a  d that pKey1 is.
1a1b3 2a 2a 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 2c  ** an index key,
1a1b4 20 61 6e 64 20 74 68 75 73 20 65 6e 64 73 20 77   and thus ends w
1a1b5 69 74 68 20 61 20 72 6f 77 69 64 20 76 61 6c 75  ith a rowid valu
1a1b6 65 2e 20 20 54 68 65 20 6c 61 73 74 20 62 79 74  e.  The last byt
1a1b7 65 0a 2a 2a 20 6f 66 20 74 68 65 20 68 65 61 64  e.** of the head
1a1b8 65 72 20 77 69 6c 6c 20 74 68 65 72 65 66 6f 72  er will therefor
1a1b9 65 20 62 65 20 74 68 65 20 73 65 72 69 61 6c 20  e be the serial 
1a1ba 74 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69  type of the rowi
1a1bb 64 3a 0a 2a 2a 20 6f 6e 65 20 6f 66 20 31 2c 20  d:.** one of 1, 
1a1bc 32 2c 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 38  2, 3, 4, 5, 6, 8
1a1bd 2c 20 6f 72 20 39 20 2d 20 74 68 65 20 69 6e 74  , or 9 - the int
1a1be 65 67 65 72 20 73 65 72 69 61 6c 20 74 79 70 65  eger serial type
1a1bf 73 2e 0a 2a 2a 20 54 68 65 20 73 65 72 69 61 6c  s..** The serial
1a1c0 20 74 79 70 65 20 6f 66 20 74 68 65 20 66 69 6e   type of the fin
1a1c1 61 6c 20 72 6f 77 69 64 20 77 69 6c 6c 20 61 6c  al rowid will al
1a1c2 77 61 79 73 20 62 65 20 61 20 73 69 6e 67 6c 65  ways be a single
1a1c3 20 62 79 74 65 2e 0a 2a 2a 20 42 79 20 69 67 6e   byte..** By ign
1a1c4 6f 72 69 6e 67 20 74 68 69 73 20 6c 61 73 74 20  oring this last 
1a1c5 62 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64  byte of the head
1a1c6 65 72 2c 20 77 65 20 66 6f 72 63 65 20 74 68 65  er, we force the
1a1c7 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 74   comparison.** t
1a1c8 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 72 6f 77  o ignore the row
1a1c9 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  id at the end of
1a1ca 20 6b 65 79 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45   key1..*/.SQLITE
1a1cb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1a1cc 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f  ite3VdbeRecordCo
1a1cd 6d 70 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b 65  mpare(.  int nKe
1a1ce 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  y1, const void *
1a1cf 70 4b 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20 6b  pKey1, /* Left k
1a1d0 65 79 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64  ey */.  Unpacked
1a1d1 52 65 63 6f 72 64 20 2a 70 50 4b 65 79 32 20 20  Record *pPKey2  
1a1d2 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6b        /* Right k
1a1d3 65 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 64  ey */.){.  int d
1a1d4 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  1;            /*
1a1d5 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65   Offset into aKe
1a1d6 79 5b 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 61  y[] of next data
1a1d7 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33   element */.  u3
1a1d8 32 20 69 64 78 31 3b 20 20 20 20 20 20 20 20 20  2 idx1;         
1a1d9 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20   /* Offset into 
1a1da 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 68  aKey[] of next h
1a1db 65 61 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a 2f  eader element */
1a1dc 0a 20 20 75 33 32 20 73 7a 48 64 72 31 3b 20 20  .  u32 szHdr1;  
1a1dd 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1a1de 6f 66 20 62 79 74 65 73 20 69 6e 20 68 65 61 64  of bytes in head
1a1df 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20  er */.  int i = 
1a1e0 30 3b 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b  0;.  int nField;
1a1e1 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20  .  int rc = 0;. 
1a1e2 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1a1e3 63 68 61 72 20 2a 61 4b 65 79 31 20 3d 20 28 63  char *aKey1 = (c
1a1e4 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1a1e5 61 72 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b 65  ar *)pKey1;.  Ke
1a1e6 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
1a1e7 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20 20  .  Mem mem1;..  
1a1e8 70 4b 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65 79  pKeyInfo = pPKey
1a1e9 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 6d  2->pKeyInfo;.  m
1a1ea 65 6d 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49 6e  em1.enc = pKeyIn
1a1eb 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31 2e  fo->enc;.  mem1.
1a1ec 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64  db = pKeyInfo->d
1a1ed 62 3b 0a 20 20 6d 65 6d 31 2e 66 6c 61 67 73 20  b;.  mem1.flags 
1a1ee 3d 20 30 3b 0a 20 20 6d 65 6d 31 2e 75 2e 69 20  = 0;.  mem1.u.i 
1a1ef 3d 20 30 3b 20 20 2f 2a 20 6e 6f 74 20 6e 65 65  = 0;  /* not nee
1a1f0 64 65 64 2c 20 68 65 72 65 20 74 6f 20 73 69 6c  ded, here to sil
1a1f1 65 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 61  ence compiler wa
1a1f2 72 6e 69 6e 67 20 2a 2f 0a 20 20 6d 65 6d 31 2e  rning */.  mem1.
1a1f3 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 0a  zMalloc = 0;.  .
1a1f4 20 20 69 64 78 31 20 3d 20 67 65 74 56 61 72 69    idx1 = getVari
1a1f5 6e 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64  nt32(aKey1, szHd
1a1f6 72 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64  r1);.  d1 = szHd
1a1f7 72 31 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32  r1;.  if( pPKey2
1a1f8 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b  ->flags & UNPACK
1a1f9 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20  ED_IGNORE_ROWID 
1a1fa 29 7b 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b  ){.    szHdr1--;
1a1fb 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20  .  }.  nField = 
1a1fc 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1a1fd 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c  ;.  while( idx1<
1a1fe 73 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65  szHdr1 && i<pPKe
1a1ff 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20  y2->nField ){.  
1a200 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
1a201 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64  e1;..    /* Read
1a202 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   the serial type
1a203 73 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65  s for the next e
1a204 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b  lement in each k
1a205 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20  ey. */.    idx1 
1a206 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 20  += getVarint32( 
1a207 61 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69  aKey1+idx1, seri
1a208 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20  al_type1 );.    
1a209 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26  if( d1>=nKey1 &&
1a20a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1a20b 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c  alTypeLen(serial
1a20c 5f 74 79 70 65 31 29 3e 30 20 29 20 62 72 65 61  _type1)>0 ) brea
1a20d 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61  k;..    /* Extra
1a20e 63 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f  ct the values to
1a20f 20 62 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20   be compared..  
1a210 20 20 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73    */.    d1 += s
1a211 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1a212 47 65 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20  Get(&aKey1[d1], 
1a213 73 65 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d  serial_type1, &m
1a214 65 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f  em1);..    /* Do
1a215 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a   the comparison.
1a216 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
1a217 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72  sqlite3MemCompar
1a218 65 28 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32  e(&mem1, &pPKey2
1a219 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20  ->aMem[i],.     
1a21a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a21b 20 20 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f        i<nField ?
1a21c 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c   pKeyInfo->aColl
1a21d 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66  [i] : 0);.    if
1a21e 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20  ( rc!=0 ){.     
1a21f 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1a220 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28    i++;.  }.  if(
1a221 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 29 20   mem1.zMalloc ) 
1a222 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1a223 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b 0a 0a 20  lease(&mem1);.. 
1a224 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 46 49   /* If the PREFI
1a225 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 69 73  X_SEARCH flag is
1a226 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 69 65   set and all fie
1a227 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 20 66  lds except the f
1a228 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 69 64 20  inal.  ** rowid 
1a229 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61 6c  field were equal
1a22a 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68 65  , then clear the
1a22b 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 66   PREFIX_SEARCH f
1a22c 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20 2a  lag and set .  *
1a22d 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64 20  * pPKey2->rowid 
1a22e 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  to the value of 
1a22f 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c 64 20  the rowid field 
1a230 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b 65 79 31  in (pKey1, nKey1
1a231 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20  )..  ** This is 
1a232 75 73 65 64 20 62 79 20 74 68 65 20 4f 50 5f 49  used by the OP_I
1a233 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e 0a  sUnique opcode..
1a234 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 4b 65    */.  if( (pPKe
1a235 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41  y2->flags & UNPA
1a236 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52  CKED_PREFIX_SEAR
1a237 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65 79  CH) && i==(pPKey
1a238 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b 0a  2->nField-1) ){.
1a239 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 31      assert( idx1
1a23a 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 63 20 29  ==szHdr1 && rc )
1a23b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  ;.    assert( me
1a23c 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  m1.flags & MEM_I
1a23d 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b 65 79 32  nt );.    pPKey2
1a23e 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e 50 41  ->flags &= ~UNPA
1a23f 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52  CKED_PREFIX_SEAR
1a240 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 32 2d 3e  CH;.    pPKey2->
1a241 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e 69  rowid = mem1.u.i
1a242 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
1a243 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72 63 3d  =0 ){.    /* rc=
1a244 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20 74 68  =0 here means th
1a245 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 6b 65  at one of the ke
1a246 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20 66 69  ys ran out of fi
1a247 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a 2a 20  elds and.    ** 
1a248 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73 20 75  all the fields u
1a249 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e 74 20  p to that point 
1a24a 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66 20 74  were equal. If t
1a24b 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52  he UNPACKED_INCR
1a24c 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20  KEY.    ** flag 
1a24d 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62 72 65  is set, then bre
1a24e 61 6b 20 74 68 65 20 74 69 65 20 62 79 20 74 72  ak the tie by tr
1a24f 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73 20 6c  eating key2 as l
1a250 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20 49 66  arger..    ** If
1a251 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50 52 45   the UPACKED_PRE
1a252 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67 20 69  FIX_MATCH flag i
1a253 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65 79 73  s set, then keys
1a254 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70 72 65   with common pre
1a255 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61 72 65  fixes.    ** are
1a256 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62   considered to b
1a257 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72 77  e equal.  Otherw
1a258 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72 20  ise, the longer 
1a259 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 20 20  key is the .    
1a25a 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73 20 69  ** larger.  As i
1a25b 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20 70  t happens, the p
1a25c 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77 61 79  PKey2 will alway
1a25d 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65 72 0a  s be the longer.
1a25e 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20      ** if there 
1a25f 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 2e  is a difference.
1a260 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1a261 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20  pPKey2->flags & 
1a262 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
1a263 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 2d   ){.      rc = -
1a264 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  1;.    }else if(
1a265 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26   pPKey2->flags &
1a266 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58   UNPACKED_PREFIX
1a267 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20 20 20  _MATCH ){.      
1a268 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30 20 2a  /* Leave rc==0 *
1a269 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  /.    }else if( 
1a26a 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b 0a 20  idx1<szHdr1 ){. 
1a26b 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20       rc = 1;.   
1a26c 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70   }.  }else if( p
1a26d 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72  KeyInfo->aSortOr
1a26e 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49 6e 66  der && i<pKeyInf
1a26f 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20 20 20  o->nField.      
1a270 20 20 20 20 20 20 20 20 20 26 26 20 70 4b 65 79           && pKey
1a271 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
1a272 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  [i] ){.    rc = 
1a273 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  -rc;.  }..  retu
1a274 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a  rn rc;.}. ../*.*
1a275 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74  * pCur points at
1a276 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72 79 20   an index entry 
1a277 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68  created using th
1a278 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20  e OP_MakeRecord 
1a279 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61 64 20  opcode..** Read 
1a27a 74 68 65 20 72 6f 77 69 64 20 28 74 68 65 20 6c  the rowid (the l
1a27b 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74 68 65  ast field in the
1a27c 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73 74 6f   record) and sto
1a27d 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69 64 2e  re it in *rowid.
1a27e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
1a27f 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69  E_OK if everythi
1a280 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61 6e 20  ng works, or an 
1a281 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68 65 72  error code other
1a282 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72  wise..**.** pCur
1a283 20 6d 69 67 68 74 20 62 65 20 70 6f 69 6e 74 69   might be pointi
1a284 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 74 61 69  ng to text obtai
1a285 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f 72 72 75  ned from a corru
1a286 70 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  pt database file
1a287 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 6f 6e 74  ..** So the cont
1a288 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 74 72  ent cannot be tr
1a289 75 73 74 65 64 2e 20 20 44 6f 20 61 70 70 72 6f  usted.  Do appro
1a28a 70 72 69 61 74 65 20 63 68 65 63 6b 73 20 6f 6e  priate checks on
1a28b 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f   the content..*/
1a28c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a28d 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
1a28e 64 78 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72  dxRowid(BtCursor
1a28f 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77   *pCur, i64 *row
1a290 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c  id){.  i64 nCell
1a291 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  Key = 0;.  int r
1a292 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20  c;.  u32 szHdr; 
1a293 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1a294 66 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  f the header */.
1a295 20 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b    u32 typeRowid;
1a296 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79      /* Serial ty
1a297 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20  pe of the rowid 
1a298 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69  */.  u32 lenRowi
1a299 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f  d;     /* Size o
1a29a 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20  f the rowid */. 
1a29b 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 2f 2a   Mem m, v;..  /*
1a29c 20 47 65 74 20 74 68 65 20 73 69 7a 65 20 6f 66   Get the size of
1a29d 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79   the index entry
1a29e 2e 20 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73 20  .  Only indices 
1a29f 65 6e 74 72 69 65 73 20 6f 66 20 6c 65 73 73 0a  entries of less.
1a2a0 20 20 2a 2a 20 74 68 61 6e 20 32 47 69 42 20 61    ** than 2GiB a
1a2a1 72 65 20 73 75 70 70 6f 72 74 20 2d 20 61 6e 79  re support - any
1a2a2 74 68 69 6e 67 20 6c 61 72 67 65 20 6d 75 73 74  thing large must
1a2a3 20 62 65 20 64 61 74 61 62 61 73 65 20 63 6f 72   be database cor
1a2a4 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  ruption */.  sql
1a2a5 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
1a2a6 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79  (pCur, &nCellKey
1a2a7 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c  );.  if( unlikel
1a2a8 79 28 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c  y(nCellKey<=0 ||
1a2a9 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66   nCellKey>0x7fff
1a2aa 66 66 66 66 29 20 29 7b 0a 20 20 20 20 72 65 74  ffff) ){.    ret
1a2ab 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1a2ac 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20  PT_BKPT;.  }..  
1a2ad 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 63  /* Read in the c
1a2ae 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20  omplete content 
1a2af 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74  of the index ent
1a2b0 72 79 20 2a 2f 0a 20 20 6d 2e 66 6c 61 67 73 20  ry */.  m.flags 
1a2b1 3d 20 30 3b 0a 20 20 6d 2e 64 62 20 3d 20 30 3b  = 0;.  m.db = 0;
1a2b2 0a 20 20 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30  .  m.zMalloc = 0
1a2b3 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1a2b4 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65  VdbeMemFromBtree
1a2b5 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 29 6e  (pCur, 0, (int)n
1a2b6 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b  CellKey, 1, &m);
1a2b7 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
1a2b8 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
1a2b9 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20  .  /* The index 
1a2ba 65 6e 74 72 79 20 6d 75 73 74 20 62 65 67 69 6e  entry must begin
1a2bb 20 77 69 74 68 20 61 20 68 65 61 64 65 72 20 73   with a header s
1a2bc 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67  ize */.  (void)g
1a2bd 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29  etVarint32((u8*)
1a2be 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 20 20 74  m.z, szHdr);.  t
1a2bf 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d 3d  estcase( szHdr==
1a2c0 32 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  2 );.  testcase(
1a2c1 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a 20   szHdr==m.n );. 
1a2c2 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 73 7a   if( unlikely(sz
1a2c3 48 64 72 3c 32 20 7c 7c 20 28 69 6e 74 29 73 7a  Hdr<2 || (int)sz
1a2c4 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20 20  Hdr>m.n) ){.    
1a2c5 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63  goto idx_rowid_c
1a2c6 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a  orruption;.  }..
1a2c7 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 20 66 69    /* The last fi
1a2c8 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78  eld of the index
1a2c9 20 73 68 6f 75 6c 64 20 62 65 20 61 6e 20 69 6e   should be an in
1a2ca 74 65 67 65 72 20 2d 20 74 68 65 20 52 4f 57 49  teger - the ROWI
1a2cb 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 79 20 74  D..  ** Verify t
1a2cc 68 61 74 20 74 68 65 20 6c 61 73 74 20 65 6e 74  hat the last ent
1a2cd 72 79 20 72 65 61 6c 6c 79 20 69 73 20 61 6e 20  ry really is an 
1a2ce 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 28 76  integer. */.  (v
1a2cf 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32 28  oid)getVarint32(
1a2d0 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d  (u8*)&m.z[szHdr-
1a2d1 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 29 3b 0a  1], typeRowid);.
1a2d2 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65    testcase( type
1a2d3 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 20 74 65  Rowid==1 );.  te
1a2d4 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69  stcase( typeRowi
1a2d5 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 74 63 61  d==2 );.  testca
1a2d6 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 33  se( typeRowid==3
1a2d7 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
1a2d8 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 29 3b 0a  typeRowid==4 );.
1a2d9 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65    testcase( type
1a2da 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 20 74 65  Rowid==5 );.  te
1a2db 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69  stcase( typeRowi
1a2dc 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 74 63 61  d==6 );.  testca
1a2dd 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 38  se( typeRowid==8
1a2de 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
1a2df 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 29 3b 0a  typeRowid==9 );.
1a2e0 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 74    if( unlikely(t
1a2e1 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c 20 74 79  ypeRowid<1 || ty
1a2e2 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 74 79 70  peRowid>9 || typ
1a2e3 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b 0a 20 20  eRowid==7) ){.  
1a2e4 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64    goto idx_rowid
1a2e5 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d  _corruption;.  }
1a2e6 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71  .  lenRowid = sq
1a2e7 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1a2e8 79 70 65 4c 65 6e 28 74 79 70 65 52 6f 77 69 64  ypeLen(typeRowid
1a2e9 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6d  );.  testcase( m
1a2ea 2e 6e 2d 6c 65 6e 52 6f 77 69 64 3d 3d 73 7a 48  .n-lenRowid==szH
1a2eb 64 72 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69  dr );.  if( unli
1a2ec 6b 65 6c 79 28 6d 2e 6e 2d 6c 65 6e 52 6f 77 69  kely(m.n-lenRowi
1a2ed 64 3c 73 7a 48 64 72 29 20 29 7b 0a 20 20 20 20  d<szHdr) ){.    
1a2ee 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63  goto idx_rowid_c
1a2ef 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a  orruption;.  }..
1a2f0 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69    /* Fetch the i
1a2f1 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65 20 65  nteger off the e
1a2f2 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  nd of the index 
1a2f3 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69  record */.  sqli
1a2f4 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74  te3VdbeSerialGet
1a2f5 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c  ((u8*)&m.z[m.n-l
1a2f6 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 65 52 6f  enRowid], typeRo
1a2f7 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a 72 6f 77  wid, &v);.  *row
1a2f8 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 71  id = v.u.i;.  sq
1a2f9 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1a2fa 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72  ase(&m);.  retur
1a2fb 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  n SQLITE_OK;..  
1a2fc 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20  /* Jump here if 
1a2fd 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
1a2fe 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 20  ion is detected 
1a2ff 61 66 74 65 72 20 6d 20 68 61 73 20 62 65 65 6e  after m has been
1a300 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2e  .  ** allocated.
1a301 20 20 46 72 65 65 20 74 68 65 20 6d 20 6f 62 6a    Free the m obj
1a302 65 63 74 20 61 6e 64 20 72 65 74 75 72 6e 20 53  ect and return S
1a303 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20 2a  QLITE_CORRUPT. *
1a304 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72  /.idx_rowid_corr
1a305 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 74 63 61  uption:.  testca
1a306 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d 30  se( m.zMalloc!=0
1a307 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1a308 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b  eMemRelease(&m);
1a309 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1a30a 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 7d  _CORRUPT_BKPT;.}
1a30b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ../*.** Compare 
1a30c 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 69  the key of the i
1a30d 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74 20  ndex entry that 
1a30e 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f 69  cursor pC is poi
1a30f 6e 74 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a 2a  nt to against.**
1a310 20 74 68 65 20 6b 65 79 20 73 74 72 69 6e 67 20   the key string 
1a311 69 6e 20 70 4b 65 79 20 28 6f 66 20 6c 65 6e 67  in pKey (of leng
1a312 74 68 20 6e 4b 65 79 29 2e 20 20 57 72 69 74 65  th nKey).  Write
1a313 20 69 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e 75   into *pRes a nu
1a314 6d 62 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20  mber.** that is 
1a315 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20  negative, zero, 
1a316 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70  or positive if p
1a317 43 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20  C is less than, 
1a318 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20  equal to,.** or 
1a319 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 4b 65  greater than pKe
1a31a 79 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  y.  Return SQLIT
1a31b 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
1a31c 0a 2a 2a 0a 2a 2a 20 70 4b 65 79 20 69 73 20 65  .**.** pKey is e
1a31d 69 74 68 65 72 20 63 72 65 61 74 65 64 20 77 69  ither created wi
1a31e 74 68 6f 75 74 20 61 20 72 6f 77 69 64 20 6f 72  thout a rowid or
1a31f 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 73 6f   is truncated so
1a320 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69 74   that it.** omit
1a321 73 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74  s the rowid at t
1a322 68 65 20 65 6e 64 2e 20 20 54 68 65 20 72 6f 77  he end.  The row
1a323 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  id at the end of
1a324 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79   the index entry
1a325 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 20 61  .** is ignored a
1a326 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c 20  s well.  Hence, 
1a327 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c  this routine onl
1a328 79 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 70  y compares the p
1a329 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20 74  refixes .** of t
1a32a 68 65 20 6b 65 79 73 20 70 72 69 6f 72 20 74 6f  he keys prior to
1a32b 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64   the final rowid
1a32c 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 69 72 65  , not the entire
1a32d 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70   key..**.** pUnp
1a32e 61 63 6b 65 64 20 6d 61 79 20 62 65 20 61 6e 20  acked may be an 
1a32f 75 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f 6e  unpacked version
1a330 20 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20   of pKey,nKey.  
1a331 49 66 20 70 55 6e 70 61 63 6b 65 64 20 69 73 0a  If pUnpacked is.
1a332 2a 2a 20 73 75 70 70 6c 69 65 64 20 69 74 20 69  ** supplied it i
1a333 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20  s used in place 
1a334 6f 66 20 70 4b 65 79 2c 6e 4b 65 79 2e 0a 2a 2f  of pKey,nKey..*/
1a335 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a336 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49  int sqlite3VdbeI
1a337 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20  dxKeyCompare(.  
1a338 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c 20  VdbeCursor *pC, 
1a339 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1a33a 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d  he cursor to com
1a33b 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a  pare against */.
1a33c 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1a33d 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f 2a   *pUnpacked,  /*
1a33e 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f   Unpacked versio
1a33f 6e 20 6f 66 20 70 4b 65 79 20 61 6e 64 20 6e 4b  n of pKey and nK
1a340 65 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 73  ey */.  int *res
1a341 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a342 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
1a343 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73 75   comparison resu
1a344 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  lt here */.){.  
1a345 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30  i64 nCellKey = 0
1a346 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74  ;.  int rc;.  Bt
1a347 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70  Cursor *pCur = p
1a348 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 4d 65  C->pCursor;.  Me
1a349 6d 20 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  m m;..  sqlite3B
1a34a 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72  treeKeySize(pCur
1a34b 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20  , &nCellKey);.  
1a34c 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20  if( nCellKey<=0 
1a34d 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 66  || nCellKey>0x7f
1a34e 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 2a 72  ffffff ){.    *r
1a34f 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  es = 0;.    retu
1a350 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1a351 7d 0a 20 20 6d 2e 64 62 20 3d 20 30 3b 0a 20 20  }.  m.db = 0;.  
1a352 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d  m.flags = 0;.  m
1a353 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  .zMalloc = 0;.  
1a354 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1a355 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 2d  MemFromBtree(pC-
1a356 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 69 6e  >pCursor, 0, (in
1a357 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26  t)nCellKey, 1, &
1a358 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  m);.  if( rc ){.
1a359 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1a35a 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e   }.  assert( pUn
1a35b 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20  packed->flags & 
1a35c 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f  UNPACKED_IGNORE_
1a35d 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 20  ROWID );.  *res 
1a35e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
1a35f 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20  ordCompare(m.n, 
1a360 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b  m.z, pUnpacked);
1a361 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1a362 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20  mRelease(&m);.  
1a363 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1a364 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
1a365 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
1a366 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74   value to be ret
1a367 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75  urned by subsequ
1a368 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20  ent calls to.** 
1a369 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
1a36a 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  ) on the databas
1a36b 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a  e handle 'db'. .
1a36c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a36d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a36e 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c  beSetChanges(sql
1a36f 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43  ite3 *db, int nC
1a370 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74  hange){.  assert
1a371 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1a372 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
1a373 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65  );.  db->nChange
1a374 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62   = nChange;.  db
1a375 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b  ->nTotalChange +
1a376 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a  = nChange;.}../*
1a377 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69  .** Set a flag i
1a378 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 70  n the vdbe to up
1a379 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 20  date the change 
1a37a 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20  counter when it 
1a37b 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20  is finalised.** 
1a37c 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c  or reset..*/.SQL
1a37d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1a37e 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e   sqlite3VdbeCoun
1a37f 74 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76  tChanges(Vdbe *v
1a380 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e  ){.  v->changeCn
1a381 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  tOn = 1;.}../*.*
1a382 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65  * Mark every pre
1a383 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
1a384 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1a385 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
1a386 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69  ction.** as expi
1a387 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78  red..**.** An ex
1a388 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pired statement 
1a389 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d  means that recom
1a38a 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  pilation of the 
1a38b 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20  statement is.** 
1a38c 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74  recommend.  Stat
1a38d 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68  ements expire wh
1a38e 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e  en things happen
1a38f 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72   that make their
1a390 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73  .** programs obs
1a391 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67  olete.  Removing
1a392 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75   user-defined fu
1a393 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61  nctions or colla
1a394 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ting.** sequence
1a395 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61  s, or changing a
1a396 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  n authorization 
1a397 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65  function are the
1a398 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69   types of.** thi
1a399 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72  ngs that make pr
1a39a 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1a39b 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53  s obsolete..*/.S
1a39c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1a39d 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65  id sqlite3Expire
1a39e 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
1a39f 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ts(sqlite3 *db){
1a3a0 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f  .  Vdbe *p;.  fo
1a3a1 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b  r(p = db->pVdbe;
1a3a2 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
1a3a3 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20  .    p->expired 
1a3a4 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 1;.  }.}../*.*
1a3a5 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74  * Return the dat
1a3a6 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 64  abase associated
1a3a7 20 77 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a   with the Vdbe..
1a3a8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a3a9 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  E sqlite3 *sqlit
1a3aa 65 33 56 64 62 65 44 62 28 56 64 62 65 20 2a 76  e3VdbeDb(Vdbe *v
1a3ab 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64  ){.  return v->d
1a3ac 62 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  b;.}../*********
1a3ad 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
1a3ae 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eaux.c *********
1a3af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3b1 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1a3b2 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1a3b3 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a   vdbeapi.c *****
1a3b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3b6 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
1a3b7 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68   May 26.**.** Th
1a3b8 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1a3b9 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1a3ba 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1a3bb 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1a3bc 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1a3bd 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1a3be 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1a3bf 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1a3c0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1a3c1 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1a3c2 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1a3c3 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1a3c4 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1a3c5 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1a3c6 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1a3c7 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1a3c8 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1a3c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a3cd 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
1a3ce 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1a3cf 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c  code use to impl
1a3d0 65 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74 20  ement APIs that 
1a3d1 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a  are part of the.
1a3d2 2a 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24  ** VDBE..**.** $
1a3d3 49 64 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 20  Id: vdbeapi.c,v 
1a3d4 31 2e 31 36 34 20 32 30 30 39 2f 30 34 2f 32 37  1.164 2009/04/27
1a3d5 20 31 38 3a 34 36 3a 30 36 20 64 72 68 20 45 78   18:46:06 drh Ex
1a3d6 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 30 20 26 26  p $.*/..#if 0 &&
1a3d7 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1a3d8 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
1a3d9 4e 41 47 45 4d 45 4e 54 29 0a 2f 2a 0a 2a 2a 20  NAGEMENT)./*.** 
1a3da 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  The following st
1a3db 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
1a3dc 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
1a3dd 20 65 6e 64 20 70 6f 69 6e 74 73 20 6f 66 20 61   end points of a
1a3de 0a 2a 2a 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65  .** doubly-linke
1a3df 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f  d list of all co
1a3e0 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65  mpiled SQL state
1a3e1 6d 65 6e 74 73 20 74 68 61 74 20 6d 61 79 20 62  ments that may b
1a3e2 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 62 75 66  e holding.** buf
1a3e3 66 65 72 73 20 65 6c 69 67 69 62 6c 65 20 66 6f  fers eligible fo
1a3e4 72 20 72 65 6c 65 61 73 65 20 77 68 65 6e 20 74  r release when t
1a3e5 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  he sqlite3_relea
1a3e6 73 65 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74 65  se_memory() inte
1a3e7 72 66 61 63 65 20 69 73 0a 2a 2a 20 69 6e 76 6f  rface is.** invo
1a3e8 6b 65 64 2e 20 41 63 63 65 73 73 20 74 6f 20 74  ked. Access to t
1a3e9 68 69 73 20 6c 69 73 74 20 69 73 20 70 72 6f 74  his list is prot
1a3ea 65 63 74 65 64 20 62 79 20 74 68 65 20 53 51 4c  ected by the SQL
1a3eb 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
1a3ec 5f 4c 52 55 32 0a 2a 2a 20 6d 75 74 65 78 2e 0a  _LRU2.** mutex..
1a3ed 2a 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74 73  **.** Statements
1a3ee 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 68   are added to th
1a3ef 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 6c 69  e end of this li
1a3f0 73 74 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f  st when sqlite3_
1a3f1 72 65 73 65 74 28 29 20 69 73 0a 2a 2a 20 63 61  reset() is.** ca
1a3f2 6c 6c 65 64 2e 20 54 68 65 79 20 61 72 65 20 72  lled. They are r
1a3f3 65 6d 6f 76 65 64 20 65 69 74 68 65 72 20 77 68  emoved either wh
1a3f4 65 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  en sqlite3_step(
1a3f5 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 66 69 6e  ) or sqlite3_fin
1a3f6 61 6c 69 7a 65 28 29 0a 2a 2a 20 69 73 20 63 61  alize().** is ca
1a3f7 6c 6c 65 64 2e 20 57 68 65 6e 20 73 74 61 74 65  lled. When state
1a3f8 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64 20  ments are added 
1a3f9 74 6f 20 74 68 69 73 20 6c 69 73 74 2c 20 74 68  to this list, th
1a3fa 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a  e associated .**
1a3fb 20 72 65 67 69 73 74 65 72 20 61 72 72 61 79 20   register array 
1a3fc 28 70 2d 3e 61 4d 65 6d 5b 31 2e 2e 70 2d 3e 6e  (p->aMem[1..p->n
1a3fd 4d 65 6d 5d 29 20 6d 61 79 20 63 6f 6e 74 61 69  Mem]) may contai
1a3fe 6e 20 64 79 6e 61 6d 69 63 20 62 75 66 66 65 72  n dynamic buffer
1a3ff 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65  s that.** can be
1a400 20 66 72 65 65 64 20 75 73 69 6e 67 20 73 71 6c   freed using sql
1a401 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 4d  ite3VdbeReleaseM
1a402 65 6d 6f 72 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 57  emory()..**.** W
1a403 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 73 20 61  hen statements a
1a404 72 65 20 61 64 64 65 64 20 6f 72 20 72 65 6d 6f  re added or remo
1a405 76 65 64 20 66 72 6f 6d 20 74 68 69 73 20 6c 69  ved from this li
1a406 73 74 2c 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a  st, the mutex.**
1a407 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1a408 20 74 68 65 20 56 64 62 65 20 62 65 69 6e 67 20   the Vdbe being 
1a409 61 64 64 65 64 20 6f 72 20 72 65 6d 6f 76 65 64  added or removed
1a40a 20 28 56 64 62 65 2e 64 62 2d 3e 6d 75 74 65 78   (Vdbe.db->mutex
1a40b 29 20 69 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20  ) is.** already 
1a40c 68 65 6c 64 2e 20 54 68 65 20 4c 52 55 32 20 6d  held. The LRU2 m
1a40d 75 74 65 78 20 69 73 20 74 68 65 6e 20 6f 62 74  utex is then obt
1a40e 61 69 6e 65 64 2c 20 62 6c 6f 63 6b 69 6e 67 20  ained, blocking 
1a40f 69 66 20 6e 65 63 65 73 73 61 72 79 2c 0a 2a 2a  if necessary,.**
1a410 20 74 68 65 20 6c 69 6e 6b 65 64 2d 6c 69 73 74   the linked-list
1a411 20 70 6f 69 6e 74 65 72 73 20 6d 61 6e 69 70 75   pointers manipu
1a412 6c 61 74 65 64 20 61 6e 64 20 74 68 65 20 4c 52  lated and the LR
1a413 55 32 20 6d 75 74 65 78 20 72 65 6c 69 6e 71 75  U2 mutex relinqu
1a414 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  ished..*/.struct
1a415 20 53 74 61 74 65 6d 65 6e 74 4c 72 75 4c 69 73   StatementLruLis
1a416 74 20 7b 0a 20 20 56 64 62 65 20 2a 70 46 69 72  t {.  Vdbe *pFir
1a417 73 74 3b 0a 20 20 56 64 62 65 20 2a 70 4c 61 73  st;.  Vdbe *pLas
1a418 74 3b 0a 7d 3b 0a 73 74 61 74 69 63 20 73 74 72  t;.};.static str
1a419 75 63 74 20 53 74 61 74 65 6d 65 6e 74 4c 72 75  uct StatementLru
1a41a 4c 69 73 74 20 73 71 6c 69 74 65 33 4c 72 75 53  List sqlite3LruS
1a41b 74 61 74 65 6d 65 6e 74 73 3b 0a 0a 2f 2a 0a 2a  tatements;../*.*
1a41c 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
1a41d 20 6c 69 73 74 20 6c 6f 6f 6b 73 20 74 6f 20 62   list looks to b
1a41e 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e  e internally con
1a41f 73 69 73 74 65 6e 74 2e 20 54 68 69 73 20 69 73  sistent. This is
1a420 20 75 73 65 64 0a 2a 2a 20 61 73 20 70 61 72 74   used.** as part
1a421 20 6f 66 20 61 6e 20 61 73 73 65 72 74 28 29 20   of an assert() 
1a422 73 74 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c  statement as fol
1a423 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73  lows:.**.**   as
1a424 73 65 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65  sert( stmtLruChe
1a425 63 6b 28 29 20 29 3b 0a 2a 2f 0a 23 69 66 6e 64  ck() );.*/.#ifnd
1a426 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63  ef NDEBUG.static
1a427 20 69 6e 74 20 73 74 6d 74 4c 72 75 43 68 65 63   int stmtLruChec
1a428 6b 28 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a  k(){.  Vdbe *p;.
1a429 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 4c    for(p=sqlite3L
1a42a 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69  ruStatements.pFi
1a42b 72 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4c 72  rst; p; p=p->pLr
1a42c 75 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65  uNext){.    asse
1a42d 72 74 28 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c  rt(p->pLruNext |
1a42e 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 53  | p==sqlite3LruS
1a42f 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 29  tatements.pLast)
1a430 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 70 2d  ;.    assert(!p-
1a431 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 2d 3e  >pLruNext || p->
1a432 70 4c 72 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72  pLruNext->pLruPr
1a433 65 76 3d 3d 70 29 3b 0a 20 20 20 20 61 73 73 65  ev==p);.    asse
1a434 72 74 28 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c  rt(p->pLruPrev |
1a435 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c 72 75 53  | p==sqlite3LruS
1a436 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74  tatements.pFirst
1a437 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21 70  );.    assert(!p
1a438 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d  ->pLruPrev || p-
1a439 3e 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e  >pLruPrev->pLruN
1a43a 65 78 74 3d 3d 70 29 3b 0a 20 20 7d 0a 20 20 72  ext==p);.  }.  r
1a43b 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
1a43c 66 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 76 64 62  f../*.** Add vdb
1a43d 65 20 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  e p to the end o
1a43e 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
1a43f 6c 72 75 20 6c 69 73 74 2e 20 49 74 20 69 73 20  lru list. It is 
1a440 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20  assumed that.** 
1a441 70 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  p is not already
1a442 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73   part of the lis
1a443 74 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 63  t when this is c
1a444 61 6c 6c 65 64 2e 20 54 68 65 20 6c 72 75 20 6c  alled. The lru l
1a445 69 73 74 0a 2a 2a 20 69 73 20 70 72 6f 74 65 63  ist.** is protec
1a446 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54  ted by the SQLIT
1a447 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
1a448 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61  RU mutex..*/.sta
1a449 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c 72 75  tic void stmtLru
1a44a 41 64 64 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  Add(Vdbe *p){.  
1a44b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1a44c 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ter(sqlite3Mutex
1a44d 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
1a44e 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29 29  EX_STATIC_LRU2))
1a44f 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 70 4c 72 75  ;..  if( p->pLru
1a450 50 72 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e  Prev || p->pLruN
1a451 65 78 74 20 7c 7c 20 73 71 6c 69 74 65 33 4c 72  ext || sqlite3Lr
1a452 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72  uStatements.pFir
1a453 73 74 3d 3d 70 20 29 7b 0a 20 20 20 20 73 71 6c  st==p ){.    sql
1a454 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1a455 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
1a456 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
1a457 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20  STATIC_LRU2));. 
1a458 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
1a459 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72    assert( stmtLr
1a45a 75 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20 20 69  uCheck() );..  i
1a45b 66 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53 74  f( !sqlite3LruSt
1a45c 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20  atements.pFirst 
1a45d 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
1a45e 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
1a45f 65 6e 74 73 2e 70 4c 61 73 74 20 29 3b 0a 20 20  ents.pLast );.  
1a460 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74    sqlite3LruStat
1a461 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 3d 20  ements.pFirst = 
1a462 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72  p;.    sqlite3Lr
1a463 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73  uStatements.pLas
1a464 74 20 3d 20 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a  t = p;.  }else{.
1a465 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c      assert( !sql
1a466 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
1a467 73 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e 65 78  s.pLast->pLruNex
1a468 74 20 29 3b 0a 20 20 20 20 70 2d 3e 70 4c 72 75  t );.    p->pLru
1a469 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33 4c 72  Prev = sqlite3Lr
1a46a 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73  uStatements.pLas
1a46b 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72  t;.    sqlite3Lr
1a46c 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73  uStatements.pLas
1a46d 74 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 3b  t->pLruNext = p;
1a46e 0a 20 20 20 20 73 71 6c 69 74 65 33 4c 72 75 53  .    sqlite3LruS
1a46f 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 20  tatements.pLast 
1a470 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  = p;.  }..  asse
1a471 72 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b  rt( stmtLruCheck
1a472 28 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  () );..  sqlite3
1a473 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
1a474 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
1a475 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
1a476 49 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a 0a 2f 2a  IC_LRU2));.}../*
1a477 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65  .** Assuming the
1a478 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
1a479 41 54 49 43 5f 4c 52 55 32 20 6d 75 74 65 78 74  ATIC_LRU2 mutext
1a47a 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64   is already held
1a47b 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61 74  , remove.** stat
1a47c 65 6d 65 6e 74 20 70 20 66 72 6f 6d 20 74 68 65  ement p from the
1a47d 20 6c 65 61 73 74 2d 72 65 63 65 6e 74 6c 79 2d   least-recently-
1a47e 75 73 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6c  used statement l
1a47f 69 73 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20  ist. If the .** 
1a480 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74  statement is not
1a481 20 63 75 72 72 65 6e 74 6c 79 20 70 61 72 74 20   currently part 
1a482 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69  of the list, thi
1a483 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
1a484 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  p..*/.static voi
1a485 64 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e  d stmtLruRemoveN
1a486 6f 6d 75 74 65 78 28 56 64 62 65 20 2a 70 29 7b  omutex(Vdbe *p){
1a487 0a 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 50 72  .  if( p->pLruPr
1a488 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e 65 78  ev || p->pLruNex
1a489 74 20 7c 7c 20 70 3d 3d 73 71 6c 69 74 65 33 4c  t || p==sqlite3L
1a48a 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69  ruStatements.pFi
1a48b 72 73 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  rst ){.    asser
1a48c 74 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28  t( stmtLruCheck(
1a48d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ) );.    if( p->
1a48e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 20  pLruNext ){.    
1a48f 20 20 70 2d 3e 70 4c 72 75 4e 65 78 74 2d 3e 70    p->pLruNext->p
1a490 4c 72 75 50 72 65 76 20 3d 20 70 2d 3e 70 4c 72  LruPrev = p->pLr
1a491 75 50 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65  uPrev;.    }else
1a492 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c  {.      sqlite3L
1a493 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61  ruStatements.pLa
1a494 73 74 20 3d 20 70 2d 3e 70 4c 72 75 50 72 65 76  st = p->pLruPrev
1a495 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1a496 70 2d 3e 70 4c 72 75 50 72 65 76 20 29 7b 0a 20  p->pLruPrev ){. 
1a497 20 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65 76       p->pLruPrev
1a498 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 2d 3e  ->pLruNext = p->
1a499 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d 65  pLruNext;.    }e
1a49a 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
1a49b 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e  e3LruStatements.
1a49c 70 46 69 72 73 74 20 3d 20 70 2d 3e 70 4c 72 75  pFirst = p->pLru
1a49d 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
1a49e 70 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 30 3b  p->pLruNext = 0;
1a49f 0a 20 20 20 20 70 2d 3e 70 4c 72 75 50 72 65 76  .    p->pLruPrev
1a4a0 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
1a4a1 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 29  ( stmtLruCheck()
1a4a2 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   );.  }.}../*.**
1a4a3 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 53 51   Assuming the SQ
1a4a4 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
1a4a5 43 5f 4c 52 55 32 20 6d 75 74 65 78 74 20 69 73  C_LRU2 mutext is
1a4a6 20 6e 6f 74 20 68 65 6c 64 2c 20 72 65 6d 6f 76   not held, remov
1a4a7 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70  e.** statement p
1a4a8 20 66 72 6f 6d 20 74 68 65 20 6c 65 61 73 74 2d   from the least-
1a4a9 72 65 63 65 6e 74 6c 79 2d 75 73 65 64 20 73 74  recently-used st
1a4aa 61 74 65 6d 65 6e 74 20 6c 69 73 74 2e 20 49 66  atement list. If
1a4ab 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65   the .** stateme
1a4ac 6e 74 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  nt is not curren
1a4ad 74 6c 79 20 70 61 72 74 20 6f 66 20 74 68 65 20  tly part of the 
1a4ae 6c 69 73 74 2c 20 74 68 69 73 20 63 61 6c 6c 20  list, this call 
1a4af 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73  is a no-op..*/.s
1a4b0 74 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c  tatic void stmtL
1a4b1 72 75 52 65 6d 6f 76 65 28 56 64 62 65 20 2a 70  ruRemove(Vdbe *p
1a4b2 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
1a4b3 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
1a4b4 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
1a4b5 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
1a4b6 52 55 32 29 29 3b 0a 20 20 73 74 6d 74 4c 72 75  RU2));.  stmtLru
1a4b7 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 70 29  RemoveNomutex(p)
1a4b8 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1a4b9 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d  x_leave(sqlite3M
1a4ba 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
1a4bb 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
1a4bc 55 32 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  U2));.}../*.** T
1a4bd 72 79 20 74 6f 20 72 65 6c 65 61 73 65 20 6e 20  ry to release n 
1a4be 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
1a4bf 62 79 20 66 72 65 65 69 6e 67 20 62 75 66 66 65  by freeing buffe
1a4c0 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a  rs associated .*
1a4c1 2a 20 77 69 74 68 20 74 68 65 20 6d 65 6d 6f 72  * with the memor
1a4c2 79 20 72 65 67 69 73 74 65 72 73 20 6f 66 20 63  y registers of c
1a4c3 75 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 20  urrently unused 
1a4c4 76 64 62 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  vdbes..*/.SQLITE
1a4c5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1a4c6 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 4d  ite3VdbeReleaseM
1a4c7 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 20 20  emory(int n){.  
1a4c8 56 64 62 65 20 2a 70 3b 0a 20 20 56 64 62 65 20  Vdbe *p;.  Vdbe 
1a4c9 2a 70 4e 65 78 74 3b 0a 20 20 69 6e 74 20 6e 46  *pNext;.  int nF
1a4ca 72 65 65 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69  ree = 0;..  sqli
1a4cb 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1a4cc 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
1a4cd 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
1a4ce 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20 20  TATIC_LRU2));.  
1a4cf 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 4c 72 75  for(p=sqlite3Lru
1a4d0 53 74 61 74 65 6d 65 6e 74 73 2e 70 46 69 72 73  Statements.pFirs
1a4d1 74 3b 20 70 20 26 26 20 6e 46 72 65 65 3c 6e 3b  t; p && nFree<n;
1a4d2 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 70   p=pNext){.    p
1a4d3 4e 65 78 74 20 3d 20 70 2d 3e 70 4c 72 75 4e 65  Next = p->pLruNe
1a4d4 78 74 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f 72 20  xt;..    /* For 
1a4d5 65 61 63 68 20 73 74 61 74 65 6d 65 6e 74 20 68  each statement h
1a4d6 61 6e 64 6c 65 20 69 6e 20 74 68 65 20 6c 72 75  andle in the lru
1a4d7 20 6c 69 73 74 2c 20 61 74 74 65 6d 70 74 20 74   list, attempt t
1a4d8 6f 20 6f 62 74 61 69 6e 20 74 68 65 0a 20 20 20  o obtain the.   
1a4d9 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 64   ** associated d
1a4da 61 74 61 62 61 73 65 20 6d 75 74 65 78 2e 20 49  atabase mutex. I
1a4db 66 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 6f  f it cannot be o
1a4dc 62 74 61 69 6e 65 64 2c 20 63 6f 6e 74 69 6e 75  btained, continu
1a4dd 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  e.    ** to the 
1a4de 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20 68  next statement h
1a4df 61 6e 64 6c 65 2e 20 49 74 20 69 73 20 6e 6f 74  andle. It is not
1a4e0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 62 6c 6f   possible to blo
1a4e1 63 6b 20 6f 6e 0a 20 20 20 20 2a 2a 20 74 68 65  ck on.    ** the
1a4e2 20 64 61 74 61 62 61 73 65 20 6d 75 74 65 78 20   database mutex 
1a4e3 2d 20 74 68 61 74 20 63 6f 75 6c 64 20 63 61 75  - that could cau
1a4e4 73 65 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 20  se deadlock..   
1a4e5 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49   */.    if( SQLI
1a4e6 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 5f 6d  TE_OK==sqlite3_m
1a4e7 75 74 65 78 5f 74 72 79 28 70 2d 3e 64 62 2d 3e  utex_try(p->db->
1a4e8 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 20 20  mutex) ){.      
1a4e9 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33  nFree += sqlite3
1a4ea 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 65  VdbeReleaseBuffe
1a4eb 72 73 28 70 29 3b 0a 20 20 20 20 20 20 73 74 6d  rs(p);.      stm
1a4ec 74 4c 72 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65  tLruRemoveNomute
1a4ed 78 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  x(p);.      sqli
1a4ee 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1a4ef 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1a4f0 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
1a4f1 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73  e3_mutex_leave(s
1a4f2 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
1a4f3 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
1a4f4 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 0a 20 20  ATIC_LRU2));..  
1a4f5 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a  return nFree;.}.
1a4f6 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69  ./*.** Call sqli
1a4f7 74 65 33 52 65 70 72 65 70 61 72 65 28 29 20 6f  te3Reprepare() o
1a4f8 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e  n the statement.
1a4f9 20 52 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d 20   Remove it from 
1a4fa 74 68 65 0a 2a 2a 20 6c 72 75 20 6c 69 73 74 20  the.** lru list 
1a4fb 62 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c  before doing so,
1a4fc 20 61 73 20 52 65 70 72 65 70 61 72 65 28 29 20   as Reprepare() 
1a4fd 77 69 6c 6c 20 66 72 65 65 20 61 6c 6c 20 74 68  will free all th
1a4fe 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 65 67 69  e.** memory regi
1a4ff 73 74 65 72 20 62 75 66 66 65 72 73 20 61 6e 79  ster buffers any
1a500 77 61 79 2e 0a 2a 2f 0a 69 6e 74 20 76 64 62 65  way..*/.int vdbe
1a501 52 65 70 72 65 70 61 72 65 28 56 64 62 65 20 2a  Reprepare(Vdbe *
1a502 70 29 7b 0a 20 20 73 74 6d 74 4c 72 75 52 65 6d  p){.  stmtLruRem
1a503 6f 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ove(p);.  return
1a504 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72   sqlite3Reprepar
1a505 65 28 70 29 3b 0a 7d 0a 0a 23 65 6c 73 65 20 20  e(p);.}..#else  
1a506 20 20 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 5f       /* !SQLITE_
1a507 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
1a508 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 20 20 23 64  NAGEMENT */.  #d
1a509 65 66 69 6e 65 20 73 74 6d 74 4c 72 75 52 65 6d  efine stmtLruRem
1a50a 6f 76 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65  ove(x).  #define
1a50b 20 73 74 6d 74 4c 72 75 41 64 64 28 78 29 0a 20   stmtLruAdd(x). 
1a50c 20 23 64 65 66 69 6e 65 20 76 64 62 65 52 65 70   #define vdbeRep
1a50d 72 65 70 61 72 65 28 78 29 20 73 71 6c 69 74 65  repare(x) sqlite
1a50e 33 52 65 70 72 65 70 61 72 65 28 78 29 0a 23 65  3Reprepare(x).#e
1a50f 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53  ndif...#ifndef S
1a510 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
1a511 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75  CATED./*.** Retu
1a512 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72  rn TRUE (non-zer
1a513 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  o) of the statem
1a514 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20  ent supplied as 
1a515 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64  an argument need
1a516 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d  s.** to be recom
1a517 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d  piled.  A statem
1a518 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ent needs to be 
1a519 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65  recompiled whene
1a51a 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75  ver the.** execu
1a51b 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74  tion environment
1a51c 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61   changes in a wa
1a51d 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74  y that would alt
1a51e 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a  er the program.*
1a51f 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70  * that sqlite3_p
1a520 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74  repare() generat
1a521 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  es.  For example
1a522 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f  , if new functio
1a523 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69  ns or.** collati
1a524 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65  ng sequences are
1a525 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69   registered or i
1a526 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20  f an authorizer 
1a527 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61  function is.** a
1a528 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e  dded or changed.
1a529 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1a52a 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  nt sqlite3_expir
1a52b 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ed(sqlite3_stmt 
1a52c 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
1a52d 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *p = (Vdbe*)pStm
1a52e 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  t;.  return p==0
1a52f 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a   || p->expired;.
1a530 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1a531 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
1a532 75 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61  utine destroys a
1a533 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1a534 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64   that is created
1a535 20 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74   by.** the sqlit
1a536 65 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75  e3_compile() rou
1a537 74 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65  tine. The intege
1a538 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e  r returned is an
1a539 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63   SQLITE_.** succ
1a53a 65 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65  ess/failure code
1a53b 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
1a53c 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78  the result of ex
1a53d 65 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 74  ecuting the virt
1a53e 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a  ual.** machine..
1a53f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1a540 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f  ne sets the erro
1a541 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e  r code and strin
1a542 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  g returned by.**
1a543 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
1a544 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  (), sqlite3_errm
1a545 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  sg() and sqlite3
1a546 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a  _errmsg16()..*/.
1a547 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1a548 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1a549 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1a54a 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  tmt){.  int rc;.
1a54b 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29    if( pStmt==0 )
1a54c 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1a54d 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
1a54e 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64     Vdbe *v = (Vd
1a54f 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73  be*)pStmt;.    s
1a550 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e  qlite3 *db = v->
1a551 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  db;.#if SQLITE_T
1a552 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71  HREADSAFE.    sq
1a553 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
1a554 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65  ex = v->db->mute
1a555 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71  x;.#endif.    sq
1a556 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1a557 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 74  r(mutex);.    st
1a558 6d 74 4c 72 75 52 65 6d 6f 76 65 28 76 29 3b 0a  mtLruRemove(v);.
1a559 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1a55a 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b  VdbeFinalize(v);
1a55b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1a55c 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
1a55d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1a55e 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
1a55f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1a560 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72 6d  c;.}../*.** Term
1a561 69 6e 61 74 65 20 74 68 65 20 63 75 72 72 65 6e  inate the curren
1a562 74 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 61  t execution of a
1a563 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
1a564 61 6e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a 20  and reset it.** 
1a565 62 61 63 6b 20 74 6f 20 69 74 73 20 73 74 61 72  back to its star
1a566 74 69 6e 67 20 73 74 61 74 65 20 73 6f 20 74 68  ting state so th
1a567 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 75  at it can be reu
1a568 73 65 64 2e 20 41 20 73 75 63 63 65 73 73 20 63  sed. A success c
1a569 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ode from.** the 
1a56a 70 72 69 6f 72 20 65 78 65 63 75 74 69 6f 6e 20  prior execution 
1a56b 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
1a56c 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1a56d 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 63  sets the error c
1a56e 6f 64 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72  ode and string r
1a56f 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71  eturned by.** sq
1a570 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c  lite3_errcode(),
1a571 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
1a572 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72  ) and sqlite3_er
1a573 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c  rmsg16()..*/.SQL
1a574 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1a575 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65  te3_reset(sqlite
1a576 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
1a577 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20    int rc;.  if( 
1a578 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pStmt==0 ){.    
1a579 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1a57a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62    }else{.    Vdb
1a57b 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53  e *v = (Vdbe*)pS
1a57c 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  tmt;.    sqlite3
1a57d 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e  _mutex_enter(v->
1a57e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
1a57f 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1a580 52 65 73 65 74 28 76 29 3b 0a 20 20 20 20 73 74  Reset(v);.    st
1a581 6d 74 4c 72 75 41 64 64 28 76 29 3b 0a 20 20 20  mtLruAdd(v);.   
1a582 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1a583 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c 20  Ready(v, -1, 0, 
1a584 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72  0, 0);.    asser
1a585 74 28 20 28 72 63 20 26 20 28 76 2d 3e 64 62 2d  t( (rc & (v->db-
1a586 3e 65 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29  >errMask))==rc )
1a587 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1a588 65 33 41 70 69 45 78 69 74 28 76 2d 3e 64 62 2c  e3ApiExit(v->db,
1a589 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   rc);.    sqlite
1a58a 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 76 2d  3_mutex_leave(v-
1a58b 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
1a58c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1a58d 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74  ./*.** Set all t
1a58e 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e  he parameters in
1a58f 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 53 51   the compiled SQ
1a590 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e  L statement to N
1a591 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ULL..*/.SQLITE_A
1a592 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
1a593 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71  lear_bindings(sq
1a594 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1a595 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  t){.  int i;.  i
1a596 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1a597 4b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  K;.  Vdbe *p = (
1a598 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66  Vdbe*)pStmt;.#if
1a599 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
1a59a 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  FE.  sqlite3_mut
1a59b 65 78 20 2a 6d 75 74 65 78 20 3d 20 28 28 56 64  ex *mutex = ((Vd
1a59c 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e  be*)pStmt)->db->
1a59d 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20  mutex;.#endif.  
1a59e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1a59f 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f  ter(mutex);.  fo
1a5a0 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72  r(i=0; i<p->nVar
1a5a1 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
1a5a2 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1a5a3 65 28 26 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a  e(&p->aVar[i]);.
1a5a4 20 20 20 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66      p->aVar[i].f
1a5a5 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1a5a6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
1a5a7 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
1a5a8 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1a5a9 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.../***********
1a5aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a5ab 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  * sqlite3_value_
1a5ac 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
1a5ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a5ae 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1a5af 6e 67 20 72 6f 75 74 69 6e 65 73 20 65 78 74 72  ng routines extr
1a5b0 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  act information 
1a5b1 66 72 6f 6d 20 61 20 4d 65 6d 20 6f 72 20 73 71  from a Mem or sq
1a5b2 6c 69 74 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 73  lite3_value.** s
1a5b3 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c  tructure..*/.SQL
1a5b4 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1a5b5 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75  id *sqlite3_valu
1a5b6 65 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76  e_blob(sqlite3_v
1a5b7 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d  alue *pVal){.  M
1a5b8 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56  em *p = (Mem*)pV
1a5b9 61 6c 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61  al;.  if( p->fla
1a5ba 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d  gs & (MEM_Blob|M
1a5bb 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 73  EM_Str) ){.    s
1a5bc 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
1a5bd 61 6e 64 42 6c 6f 62 28 70 29 3b 0a 20 20 20 20  andBlob(p);.    
1a5be 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d  p->flags &= ~MEM
1a5bf 5f 53 74 72 3b 0a 20 20 20 20 70 2d 3e 66 6c 61  _Str;.    p->fla
1a5c0 67 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a  gs |= MEM_Blob;.
1a5c1 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 7a 3b      return p->z;
1a5c2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
1a5c3 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  turn sqlite3_val
1a5c4 75 65 5f 74 65 78 74 28 70 56 61 6c 29 3b 0a 20  ue_text(pVal);. 
1a5c5 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20   }.}.SQLITE_API 
1a5c6 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  int sqlite3_valu
1a5c7 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f  e_bytes(sqlite3_
1a5c8 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
1a5c9 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61  return sqlite3Va
1a5ca 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53  lueBytes(pVal, S
1a5cb 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 53  QLITE_UTF8);.}.S
1a5cc 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1a5cd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
1a5ce 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  s16(sqlite3_valu
1a5cf 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
1a5d0 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42  rn sqlite3ValueB
1a5d1 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54  ytes(pVal, SQLIT
1a5d2 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a  E_UTF16NATIVE);.
1a5d3 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75  }.SQLITE_API dou
1a5d4 62 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ble sqlite3_valu
1a5d5 65 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  e_double(sqlite3
1a5d6 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
1a5d7 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1a5d8 64 62 65 52 65 61 6c 56 61 6c 75 65 28 28 4d 65  dbeRealValue((Me
1a5d9 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49  m*)pVal);.}.SQLI
1a5da 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1a5db 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c  e3_value_int(sql
1a5dc 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1a5dd 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  ){.  return (int
1a5de 29 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56  )sqlite3VdbeIntV
1a5df 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29  alue((Mem*)pVal)
1a5e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  ;.}.SQLITE_API s
1a5e1 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69  qlite_int64 sqli
1a5e2 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
1a5e3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1a5e4 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
1a5e5 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1a5e6 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a  ue((Mem*)pVal);.
1a5e7 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  }.SQLITE_API con
1a5e8 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1a5e9 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   *sqlite3_value_
1a5ea 74 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c  text(sqlite3_val
1a5eb 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue *pVal){.  ret
1a5ec 75 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 69 67  urn (const unsig
1a5ed 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 74  ned char *)sqlit
1a5ee 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c  e3ValueText(pVal
1a5ef 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
1a5f0 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1a5f1 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1a5f2 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1a5f3 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  d *sqlite3_value
1a5f4 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f  _text16(sqlite3_
1a5f5 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20  value* pVal){.  
1a5f6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61  return sqlite3Va
1a5f7 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51  lueText(pVal, SQ
1a5f8 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
1a5f9 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1a5fa 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
1a5fb 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1a5fc 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  be(sqlite3_value
1a5fd 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72   *pVal){.  retur
1a5fe 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  n sqlite3ValueTe
1a5ff 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  xt(pVal, SQLITE_
1a600 55 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49  UTF16BE);.}.SQLI
1a601 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1a602 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  d *sqlite3_value
1a603 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65  _text16le(sqlite
1a604 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
1a605 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1a606 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
1a607 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 3b  SQLITE_UTF16LE);
1a608 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1a609 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1a60a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1a60b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1a60c 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ype(sqlite3_valu
1a60d 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e* pVal){.  retu
1a60e 72 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d  rn pVal->type;.}
1a60f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1a610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
1a611 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 20  sqlite3_result_ 
1a612 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1a613 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a614 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1a615 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  g routines are u
1a616 73 65 64 20 62 79 20 75 73 65 72 2d 64 65 66 69  sed by user-defi
1a617 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f  ned functions to
1a618 20 73 70 65 63 69 66 79 0a 2a 2a 20 74 68 65 20   specify.** the 
1a619 66 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e  function result.
1a61a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
1a61b 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1a61c 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74  lt_blob(.  sqlit
1a61d 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1a61e 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
1a61f 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
1a620 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1a621 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72  id *).){.  asser
1a622 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73  t( n>=0 );.  ass
1a623 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1a624 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1a625 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1a626 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1a627 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a  tStr(&pCtx->s, z
1a628 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d  , n, 0, xDel);.}
1a629 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1a62a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a62b 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63  double(sqlite3_c
1a62c 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 64 6f  ontext *pCtx, do
1a62d 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61 73  uble rVal){.  as
1a62e 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1a62f 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1a630 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1a631 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a632 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e  etDouble(&pCtx->
1a633 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49  s, rVal);.}.SQLI
1a634 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1a635 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
1a636 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1a637 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 63 68   *pCtx, const ch
1a638 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *z, int n){. 
1a639 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1a63a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
1a63b 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
1a63c 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f  ;.  pCtx->isErro
1a63d 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  r = SQLITE_ERROR
1a63e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1a63f 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
1a640 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  s, z, n, SQLITE_
1a641 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 41  UTF8, SQLITE_TRA
1a642 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64  NSIENT);.}.#ifnd
1a643 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
1a644 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
1a645 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1a646 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69  ult_error16(sqli
1a647 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1a648 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  x, const void *z
1a649 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65  , int n){.  asse
1a64a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1a64b 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1a64c 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
1a64d 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53  Ctx->isError = S
1a64e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73  QLITE_ERROR;.  s
1a64f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1a650 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c  Str(&pCtx->s, z,
1a651 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36   n, SQLITE_UTF16
1a652 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 54  NATIVE, SQLITE_T
1a653 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e  RANSIENT);.}.#en
1a654 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  dif.SQLITE_API v
1a655 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1a656 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63  lt_int(sqlite3_c
1a657 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e  ontext *pCtx, in
1a658 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72  t iVal){.  asser
1a659 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1a65a 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1a65b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1a65c 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
1a65d 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 28  nt64(&pCtx->s, (
1a65e 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c  i64)iVal);.}.SQL
1a65f 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1a660 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
1a661 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  4(sqlite3_contex
1a662 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 56 61  t *pCtx, i64 iVa
1a663 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  l){.  assert( sq
1a664 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1a665 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1a666 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
1a667 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28  VdbeMemSetInt64(
1a668 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b  &pCtx->s, iVal);
1a669 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
1a66a 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1a66b 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63  t_null(sqlite3_c
1a66c 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20  ontext *pCtx){. 
1a66d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1a66e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
1a66f 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
1a670 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1a671 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d  emSetNull(&pCtx-
1a672 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  >s);.}.SQLITE_AP
1a673 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1a674 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 73 71  esult_text(.  sq
1a675 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1a676 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  Ctx, .  const ch
1a677 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  ar *z, .  int n,
1a678 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
1a679 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73  void *).){.  ass
1a67a 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1a67b 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1a67c 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1a67d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1a67e 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a  tStr(&pCtx->s, z
1a67f 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , n, SQLITE_UTF8
1a680 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e 64  , xDel);.}.#ifnd
1a681 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
1a682 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
1a683 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1a684 75 6c 74 5f 74 65 78 74 31 36 28 0a 20 20 73 71  ult_text16(.  sq
1a685 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1a686 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f  Ctx, .  const vo
1a687 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  id *z, .  int n,
1a688 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29   .  void (*xDel)
1a689 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73  (void *).){.  as
1a68a 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1a68b 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1a68c 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1a68d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a68e 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1a68f 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1a690 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b  16NATIVE, xDel);
1a691 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
1a692 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1a693 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73 71  t_text16be(.  sq
1a694 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1a695 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f  Ctx, .  const vo
1a696 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  id *z, .  int n,
1a697 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29   .  void (*xDel)
1a698 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73  (void *).){.  as
1a699 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1a69a 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1a69b 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1a69c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a69d 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1a69e 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1a69f 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53  16BE, xDel);.}.S
1a6a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1a6a1 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
1a6a2 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 65  xt16le(.  sqlite
1a6a3 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
1a6a4 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
1a6a5 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20  z, .  int n, .  
1a6a6 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
1a6a7 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74  d *).){.  assert
1a6a8 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1a6a9 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1a6aa 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
1a6ab 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
1a6ac 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e  r(&pCtx->s, z, n
1a6ad 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
1a6ae 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69  , xDel);.}.#endi
1a6af 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1a6b0 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45  _UTF16 */.SQLITE
1a6b1 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1a6b2 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 73  3_result_value(s
1a6b3 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a6b4 70 43 74 78 2c 20 73 71 6c 69 74 65 33 5f 76 61  pCtx, sqlite3_va
1a6b5 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20  lue *pValue){.  
1a6b6 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1a6b7 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1a6b8 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1a6b9 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1a6ba 6d 43 6f 70 79 28 26 70 43 74 78 2d 3e 73 2c 20  mCopy(&pCtx->s, 
1a6bb 70 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54  pValue);.}.SQLIT
1a6bc 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1a6bd 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c  e3_result_zerobl
1a6be 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ob(sqlite3_conte
1a6bf 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 6e 29  xt *pCtx, int n)
1a6c0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1a6c1 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1a6c2 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1a6c3 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
1a6c4 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62  beMemSetZeroBlob
1a6c5 28 26 70 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d  (&pCtx->s, n);.}
1a6c6 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1a6c7 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1a6c8 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74  error_code(sqlit
1a6c9 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1a6ca 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a  , int errCode){.
1a6cb 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20    pCtx->isError 
1a6cc 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 69 66 28  = errCode;.  if(
1a6cd 20 70 43 74 78 2d 3e 73 2e 66 6c 61 67 73 20 26   pCtx->s.flags &
1a6ce 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1a6cf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a6d0 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1a6d1 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 65 72  sqlite3ErrStr(er
1a6d2 72 43 6f 64 65 29 2c 20 2d 31 2c 20 0a 20 20 20  rCode), -1, .   
1a6d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6d4 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46        SQLITE_UTF
1a6d5 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
1a6d6 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 6f 72  );.  }.}../* For
1a6d7 63 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f  ce an SQLITE_TOO
1a6d8 42 49 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51  BIG error. */.SQ
1a6d9 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1a6da 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1a6db 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65  or_toobig(sqlite
1a6dc 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29  3_context *pCtx)
1a6dd 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1a6de 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1a6df 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1a6e0 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45  ) );.  pCtx->isE
1a6e1 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 54 4f  rror = SQLITE_TO
1a6e2 4f 42 49 47 3b 0a 20 20 73 71 6c 69 74 65 33 56  OBIG;.  sqlite3V
1a6e3 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43  dbeMemSetStr(&pC
1a6e4 74 78 2d 3e 73 2c 20 22 73 74 72 69 6e 67 20 6f  tx->s, "string o
1a6e5 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c  r blob too big",
1a6e6 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20   -1, .          
1a6e7 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
1a6e8 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
1a6e9 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20  _STATIC);.}../* 
1a6ea 41 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  An SQLITE_NOMEM 
1a6eb 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45  error. */.SQLITE
1a6ec 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1a6ed 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e  3_result_error_n
1a6ee 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f 6e  omem(sqlite3_con
1a6ef 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61  text *pCtx){.  a
1a6f0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1a6f1 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1a6f2 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1a6f3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1a6f4 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73  SetNull(&pCtx->s
1a6f5 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72  );.  pCtx->isErr
1a6f6 6f 72 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  or = SQLITE_NOME
1a6f7 4d 3b 0a 20 20 70 43 74 78 2d 3e 73 2e 64 62 2d  M;.  pCtx->s.db-
1a6f8 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1a6f9 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  1;.}../*.** Exec
1a6fa 75 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ute the statemen
1a6fb 74 20 70 53 74 6d 74 2c 20 65 69 74 68 65 72 20  t pStmt, either 
1a6fc 75 6e 74 69 6c 20 61 20 72 6f 77 20 6f 66 20 64  until a row of d
1a6fd 61 74 61 20 69 73 20 72 65 61 64 79 2c 20 74 68  ata is ready, th
1a6fe 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69  e.** statement i
1a6ff 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 65  s completely exe
1a700 63 75 74 65 64 20 6f 72 20 61 6e 20 65 72 72 6f  cuted or an erro
1a701 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20  r occurs..**.** 
1a702 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
1a703 6c 65 6d 65 6e 74 73 20 74 68 65 20 62 75 6c 6b  lements the bulk
1a704 20 6f 66 20 74 68 65 20 6c 6f 67 69 63 20 62 65   of the logic be
1a705 68 69 6e 64 20 74 68 65 20 73 71 6c 69 74 65 5f  hind the sqlite_
1a706 73 74 65 70 28 29 0a 2a 2a 20 41 50 49 2e 20 20  step().** API.  
1a707 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 6f  The only thing o
1a708 6d 69 74 74 65 64 20 69 73 20 74 68 65 20 61 75  mitted is the au
1a709 74 6f 6d 61 74 69 63 20 72 65 63 6f 6d 70 69 6c  tomatic recompil
1a70a 65 20 69 66 20 61 20 0a 2a 2a 20 73 63 68 65 6d  e if a .** schem
1a70b 61 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63  a change has occ
1a70c 75 72 72 65 64 2e 20 20 54 68 61 74 20 64 65 74  urred.  That det
1a70d 61 69 6c 20 69 73 20 68 61 6e 64 6c 65 64 20 62  ail is handled b
1a70e 79 20 74 68 65 0a 2a 2a 20 6f 75 74 65 72 20 73  y the.** outer s
1a70f 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 77 72  qlite3_step() wr
1a710 61 70 70 65 72 20 70 72 6f 63 65 64 75 72 65 2e  apper procedure.
1a711 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
1a712 71 6c 69 74 65 33 53 74 65 70 28 56 64 62 65 20  qlite3Step(Vdbe 
1a713 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  *p){.  sqlite3 *
1a714 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  db;.  int rc;.. 
1a715 20 61 73 73 65 72 74 28 70 29 3b 0a 20 20 69 66   assert(p);.  if
1a716 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45  ( p->magic!=VDBE
1a717 5f 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20  _MAGIC_RUN ){.  
1a718 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a719 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f  MISUSE;.  }..  /
1a71a 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 6d 61  * Assert that ma
1a71b 6c 6c 6f 63 28 29 20 68 61 73 20 6e 6f 74 20 66  lloc() has not f
1a71c 61 69 6c 65 64 20 2a 2f 0a 20 20 64 62 20 3d 20  ailed */.  db = 
1a71d 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d  p->db;.  if( db-
1a71e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
1a71f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1a720 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
1a721 20 69 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26 26   if( p->pc<=0 &&
1a722 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20   p->expired ){. 
1a723 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d     if( ALWAYS(p-
1a724 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20  >rc==SQLITE_OK) 
1a725 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d  ){.      p->rc =
1a726 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a   SQLITE_SCHEMA;.
1a727 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 53      }.    rc = S
1a728 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1a729 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65   goto end_of_ste
1a72a 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  p;.  }.  if( sql
1a72b 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1a72c 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1a72d 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1a72e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1a72f 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69  _MISUSE;.  }.  i
1a730 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20  f( p->pc<0 ){.  
1a731 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
1a732 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65  e no other state
1a733 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20  ments currently 
1a734 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20  running, then.  
1a735 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20 69    ** reset the i
1a736 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20 20  nterrupt flag.  
1a737 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20  This prevents a 
1a738 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
1a739 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a 2a  interrupt.    **
1a73a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74 69   from interrupti
1a73b 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  ng a statement t
1a73c 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20  hat has not yet 
1a73d 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  started..    */.
1a73e 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69      if( db->acti
1a73f 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a  veVdbeCnt==0 ){.
1a740 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49        db->u1.isI
1a741 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a  nterrupted = 0;.
1a742 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
1a743 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
1a744 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72  .    if( db->xPr
1a745 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e  ofile && !db->in
1a746 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20  it.busy ){.     
1a747 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20   double rNow;.  
1a748 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72      sqlite3OsCur
1a749 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66  rentTime(db->pVf
1a74a 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20  s, &rNow);.     
1a74b 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d 20   p->startTime = 
1a74c 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69  (u64)((rNow - (i
1a74d 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a  nt)rNow)*3600.0*
1a74e 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e  24.0*1000000000.
1a74f 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  0);.    }.#endif
1a750 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65  ..    db->active
1a751 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 69  VdbeCnt++;.    i
1a752 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d  f( p->readOnly==
1a753 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 64 62  0 ) db->writeVdb
1a754 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70  eCnt++;.    p->p
1a755 63 20 3d 20 30 3b 0a 20 20 20 20 73 74 6d 74 4c  c = 0;.    stmtL
1a756 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a 20 20 7d  ruRemove(p);.  }
1a757 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a758 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69  OMIT_EXPLAIN.  i
1a759 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b  f( p->explain ){
1a75a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1a75b 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20  3VdbeList(p);.  
1a75c 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
1a75d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
1a75e 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72  AIN */.  {.    r
1a75f 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45  c = sqlite3VdbeE
1a760 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69  xec(p);.  }..  i
1a761 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1a762 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72  Off(db) ){.    r
1a763 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
1a764 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  E;.  }..#ifndef 
1a765 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1a766 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  E.  /* Invoke th
1a767 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61  e profile callba
1a768 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ck if there is o
1a769 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  ne.  */.  if( rc
1a76a 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20  !=SQLITE_ROW && 
1a76b 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20  db->xProfile && 
1a76c 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26  !db->init.busy &
1a76d 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20  & p->zSql ){.   
1a76e 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20   double rNow;.  
1a76f 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65    u64 elapseTime
1a770 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ;..    sqlite3Os
1a771 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e  CurrentTime(db->
1a772 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20  pVfs, &rNow);.  
1a773 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28    elapseTime = (
1a774 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e  u64)((rNow - (in
1a775 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32  t)rNow)*3600.0*2
1a776 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30  4.0*1000000000.0
1a777 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d  );.    elapseTim
1a778 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d  e -= p->startTim
1a779 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66  e;.    db->xProf
1a77a 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65  ile(db->pProfile
1a77b 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c  Arg, p->zSql, el
1a77c 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23  apseTime);.  }.#
1a77d 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72  endif..  db->err
1a77e 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28  Code = rc;.  if(
1a77f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73   SQLITE_NOMEM==s
1a780 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d  qlite3ApiExit(p-
1a781 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20  >db, p->rc) ){. 
1a782 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
1a783 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64  E_NOMEM;.  }.end
1a784 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41  _of_step:.  /* A
1a785 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63  t this point loc
1a786 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68  al variable rc h
1a787 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74  olds the value t
1a788 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20  hat should be . 
1a789 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20   ** returned if 
1a78a 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77  this statement w
1a78b 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e  as compiled usin
1a78c 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20  g the legacy .  
1a78d 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ** sqlite3_prepa
1a78e 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20  re() interface. 
1a78f 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  According to the
1a790 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20   docs, this can 
1a791 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65  only.  ** be one
1a792 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69   of the values i
1a793 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 65  n the first asse
1a794 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69  rt() below. Vari
1a795 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a  able p->rc .  **
1a796 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61   contains the va
1a797 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62  lue that would b
1a798 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 71  e returned if sq
1a799 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
1a79a 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c   .  ** were call
1a79b 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20  ed on statement 
1a79c 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  p..  */.  assert
1a79d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
1a79e 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f    || rc==SQLITE_
1a79f 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51  DONE   || rc==SQ
1a7a0 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20  LITE_ERROR .    
1a7a1 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45     || rc==SQLITE
1a7a2 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c  _BUSY || rc==SQL
1a7a3 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a  ITE_MISUSE.  );.
1a7a4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21    assert( p->rc!
1a7a5 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70  =SQLITE_ROW && p
1a7a6 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ->rc!=SQLITE_DON
1a7a7 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73  E );.  if( p->is
1a7a8 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 21  PrepareV2 && rc!
1a7a9 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72  =SQLITE_ROW && r
1a7aa 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
1a7ab 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
1a7ac 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70   statement was p
1a7ad 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 71  repared using sq
1a7ae 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a7af 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a  (), and an.    *
1a7b0 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  * error has occu
1a7b1 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  red, then return
1a7b2 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
1a7b3 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a  in p->rc to the.
1a7b4 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53      ** caller. S
1a7b5 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  et the error cod
1a7b6 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
1a7b7 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20  e handle to the 
1a7b8 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20  same value..    
1a7b9 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d  */ .    rc = db-
1a7ba 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63  >errCode = p->rc
1a7bb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
1a7bc 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b  rc&db->errMask);
1a7bd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
1a7be 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20  s the top-level 
1a7bf 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1a7c0 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  f sqlite3_step()
1a7c1 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74  .  Call.** sqlit
1a7c2 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d  e3Step() to do m
1a7c3 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e  ost of the work.
1a7c4 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72    If a schema er
1a7c5 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63  ror occurs,.** c
1a7c6 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65  all sqlite3Repre
1a7c7 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61  pare() and try a
1a7c8 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  gain..*/.#ifdef 
1a7c9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53  SQLITE_OMIT_PARS
1a7ca 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ER.SQLITE_API in
1a7cb 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  t sqlite3_step(s
1a7cc 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1a7cd 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  mt){.  int rc = 
1a7ce 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1a7cf 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20   if( pStmt ){.  
1a7d0 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 76    Vdbe *v;.    v
1a7d1 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
1a7d2 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1a7d3 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e  ex_enter(v->db->
1a7d4 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d  mutex);.    rc =
1a7d5 20 73 71 6c 69 74 65 33 53 74 65 70 28 76 29 3b   sqlite3Step(v);
1a7d6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1a7d7 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e  ex_leave(v->db->
1a7d8 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65  mutex);.  }.  re
1a7d9 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73 65  turn rc;.}.#else
1a7da 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1a7db 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c  sqlite3_step(sql
1a7dc 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1a7dd 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1a7de 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69  LITE_MISUSE;.  i
1a7df 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20  f( pStmt ){.    
1a7e0 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20  int cnt = 0;.   
1a7e1 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65   Vdbe *v = (Vdbe
1a7e2 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c  *)pStmt;.    sql
1a7e3 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62  ite3 *db = v->db
1a7e4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1a7e5 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
1a7e6 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28  tex);.    while(
1a7e7 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74   (rc = sqlite3St
1a7e8 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53  ep(v))==SQLITE_S
1a7e9 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20  CHEMA.          
1a7ea 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20   && cnt++ < 5.  
1a7eb 20 20 20 20 20 20 20 20 20 26 26 20 28 72 63 20           && (rc 
1a7ec 3d 20 76 64 62 65 52 65 70 72 65 70 61 72 65 28  = vdbeReprepare(
1a7ed 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  v))==SQLITE_OK )
1a7ee 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1a7ef 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20  reset(pStmt);.  
1a7f0 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20 3d      v->expired =
1a7f1 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
1a7f2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48  ( rc==SQLITE_SCH
1a7f3 45 4d 41 20 26 26 20 41 4c 57 41 59 53 28 76 2d  EMA && ALWAYS(v-
1a7f4 3e 69 73 50 72 65 70 61 72 65 56 32 29 20 26 26  >isPrepareV2) &&
1a7f5 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45 72 72   ALWAYS(db->pErr
1a7f6 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  ) ){.      /* Th
1a7f7 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 61  is case occurs a
1a7f8 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f 20  fter failing to 
1a7f9 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71 6c  recompile an sql
1a7fa 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20 20   statement. .   
1a7fb 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 20     ** The error 
1a7fc 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65  message from the
1a7fd 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 61   SQL compiler ha
1a7fe 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c  s already been l
1a7ff 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a 20  oaded .      ** 
1a800 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
1a801 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 62  e handle. This b
1a802 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 20  lock copies the 
1a803 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a 20  error message . 
1a804 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65       ** from the
1a805 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1a806 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65 6d   into the statem
1a807 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68 65  ent and sets the
1a808 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20   statement.     
1a809 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e   ** program coun
1a80a 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73 75  ter to 0 to ensu
1a80b 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 65  re that when the
1a80c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a 20   statement is . 
1a80d 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a 65       ** finalize
1a80e 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 70  d or reset the p
1a80f 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73 73  arser error mess
1a810 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65  age is available
1a811 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73 71   via.      ** sq
1a812 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61  lite3_errmsg() a
1a813 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nd sqlite3_errco
1a814 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  de()..      */. 
1a815 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1a816 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20 63  *zErr = (const c
1a817 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
1a818 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72  lue_text(db->pEr
1a819 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69 74  r); .      sqlit
1a81a 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d 3e  e3DbFree(db, v->
1a81b 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
1a81c 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
1a81d 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
1a81e 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71   v->zErrMsg = sq
1a81f 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
1a820 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7d  , zErr);.      }
1a821 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20   else {.        
1a822 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  v->zErrMsg = 0;.
1a823 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20          v->rc = 
1a824 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1a825 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1a826 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
1a827 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20  xit(db, rc);.   
1a828 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1a829 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
1a82a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1a82b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1a82c 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73  * Extract the us
1a82d 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73  er data from a s
1a82e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73  qlite3_context s
1a82f 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74  tructure and ret
1a830 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72  urn a.** pointer
1a831 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54   to it..*/.SQLIT
1a832 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
1a833 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71  te3_user_data(sq
1a834 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1a835 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  ){.  assert( p &
1a836 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20  & p->pFunc );.  
1a837 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d  return p->pFunc-
1a838 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f  >pUserData;.}../
1a839 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
1a83a 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20   user data from 
1a83b 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a sqlite3_contex
1a83c 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  t structure and 
1a83d 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e  return a.** poin
1a83e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51  ter to it..*/.SQ
1a83f 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
1a840 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   *sqlite3_contex
1a841 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69  t_db_handle(sqli
1a842 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b  te3_context *p){
1a843 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20  .  assert( p && 
1a844 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65  p->pFunc );.  re
1a845 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a  turn p->s.db;.}.
1a846 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
1a847 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c  wing is the impl
1a848 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e  ementation of an
1a849 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68   SQL function th
1a84a 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69  at always.** fai
1a84b 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ls with an error
1a84c 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67   message stating
1a84d 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69   that the functi
1a84e 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68  on is used in th
1a84f 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65  e.** wrong conte
1a850 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  xt.  The sqlite3
1a851 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
1a852 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63  on() API might c
1a853 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20  onstruct.** SQL 
1a854 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73  function that us
1a855 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  e this routine s
1a856 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74  o that the funct
1a857 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a  ions will exist.
1a858 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f  ** for name reso
1a859 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61  lution but are a
1a85a 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64  ctually overload
1a85b 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46  ed by the xFindF
1a85c 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f  unction.** metho
1a85d 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62  d of virtual tab
1a85e 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  les..*/.SQLITE_P
1a85f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a860 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69  te3InvalidFuncti
1a861 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  on(.  sqlite3_co
1a862 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
1a863 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e   /* The function
1a864 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74   calling context
1a865 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65   */.  int NotUse
1a866 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
1a867 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
1a868 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66  guments to the f
1a869 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  unction */.  sql
1a86a 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74  ite3_value **Not
1a86b 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65  Used2   /* Value
1a86c 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e   of each argumen
1a86d 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  t */.){.  const 
1a86e 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f  char *zName = co
1a86f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e  ntext->pFunc->zN
1a870 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72  ame;.  char *zEr
1a871 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  r;.  UNUSED_PARA
1a872 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
1a873 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72  NotUsed2);.  zEr
1a874 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  r = sqlite3_mpri
1a875 6e 74 66 28 0a 20 20 20 20 20 20 22 75 6e 61 62  ntf(.      "unab
1a876 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69  le to use functi
1a877 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71  on %s in the req
1a878 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c  uested context",
1a879 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74   zName);.  sqlit
1a87a 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
1a87b 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d  context, zErr, -
1a87c 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  1);.  sqlite3_fr
1a87d 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a  ee(zErr);.}../*.
1a87e 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72  ** Allocate or r
1a87f 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67  eturn the aggreg
1a880 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ate context for 
1a881 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e  a user function.
1a882 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65    A new.** conte
1a883 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  xt is allocated 
1a884 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  on the first cal
1a885 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63  l.  Subsequent c
1a886 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a  alls return the.
1a887 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20  ** same context 
1a888 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65  that was returne
1a889 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73  d on prior calls
1a88a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1a88b 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67  void *sqlite3_ag
1a88c 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
1a88d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1a88e 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a  *p, int nByte){.
1a88f 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61    Mem *pMem;.  a
1a890 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70  ssert( p && p->p
1a891 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63  Func && p->pFunc
1a892 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73  ->xStep );.  ass
1a893 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1a894 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d  ex_held(p->s.db-
1a895 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65  >mutex) );.  pMe
1a896 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69  m = p->pMem;.  i
1a897 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  f( (pMem->flags 
1a898 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b  & MEM_Agg)==0 ){
1a899 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d  .    if( nByte==
1a89a 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1a89b 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1a89c 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a  External(pMem);.
1a89d 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1a89e 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1a89f 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b      pMem->z = 0;
1a8a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1a8a1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1a8a2 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65  Grow(pMem, nByte
1a8a3 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d  , 0);.      pMem
1a8a4 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67  ->flags = MEM_Ag
1a8a5 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  g;.      pMem->u
1a8a6 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63  .pDef = p->pFunc
1a8a7 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d  ;.      if( pMem
1a8a8 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  ->z ){.        m
1a8a9 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30  emset(pMem->z, 0
1a8aa 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
1a8ab 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
1a8ac 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d  turn (void*)pMem
1a8ad 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ->z;.}../*.** Re
1a8ae 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72  turn the auxilar
1a8af 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20  y data pointer, 
1a8b0 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20  if any, for the 
1a8b1 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74  iArg'th argument
1a8b2 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d   to.** the user-
1a8b3 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64  function defined
1a8b4 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c   by pCtx..*/.SQL
1a8b5 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
1a8b6 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
1a8b7 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a(sqlite3_contex
1a8b8 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72  t *pCtx, int iAr
1a8b9 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a  g){.  VdbeFunc *
1a8ba 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73  pVdbeFunc;..  as
1a8bb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1a8bc 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1a8bd 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1a8be 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74   pVdbeFunc = pCt
1a8bf 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20  x->pVdbeFunc;.  
1a8c0 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c  if( !pVdbeFunc |
1a8c1 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e  | iArg>=pVdbeFun
1a8c2 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c  c->nAux || iArg<
1a8c3 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
1a8c4 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
1a8c5 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78  pVdbeFunc->apAux
1a8c6 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a  [iArg].pAux;.}..
1a8c7 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75  /*.** Set the au
1a8c8 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e  xilary data poin
1a8c9 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66  ter and delete f
1a8ca 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65  unction, for the
1a8cb 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75   iArg'th.** argu
1a8cc 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72  ment to the user
1a8cd 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65  -function define
1a8ce 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70  d by pCtx. Any p
1a8cf 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73  revious value is
1a8d0 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63  .** deleted by c
1a8d1 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74  alling the delet
1a8d2 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69  e function speci
1a8d3 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 73  fied when it was
1a8d4 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   set..*/.SQLITE_
1a8d5 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1a8d6 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20  _set_auxdata(.  
1a8d7 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1a8d8 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41  *pCtx, .  int iA
1a8d9 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75  rg, .  void *pAu
1a8da 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  x, .  void (*xDe
1a8db 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  lete)(void*).){.
1a8dc 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61    struct AuxData
1a8dd 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64   *pAuxData;.  Vd
1a8de 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e  beFunc *pVdbeFun
1a8df 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20  c;.  if( iArg<0 
1a8e0 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a  ) goto failed;..
1a8e1 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1a8e2 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1a8e3 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1a8e4 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d  );.  pVdbeFunc =
1a8e5 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63   pCtx->pVdbeFunc
1a8e6 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75  ;.  if( !pVdbeFu
1a8e7 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d  nc || pVdbeFunc-
1a8e8 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20  >nAux<=iArg ){. 
1a8e9 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70     int nAux = (p
1a8ea 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65  VdbeFunc ? pVdbe
1a8eb 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b  Func->nAux : 0);
1a8ec 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63  .    int nMalloc
1a8ed 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75   = sizeof(VdbeFu
1a8ee 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72  nc) + sizeof(str
1a8ef 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 72  uct AuxData)*iAr
1a8f0 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63  g;.    pVdbeFunc
1a8f1 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
1a8f2 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20  loc(pCtx->s.db, 
1a8f3 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c  pVdbeFunc, nMall
1a8f4 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56  oc);.    if( !pV
1a8f5 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20  dbeFunc ){.     
1a8f6 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20   goto failed;.  
1a8f7 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56    }.    pCtx->pV
1a8f8 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46  dbeFunc = pVdbeF
1a8f9 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  unc;.    memset(
1a8fa 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75  &pVdbeFunc->apAu
1a8fb 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65  x[nAux], 0, size
1a8fc 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74  of(struct AuxDat
1a8fd 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29  a)*(iArg+1-nAux)
1a8fe 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63  );.    pVdbeFunc
1a8ff 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b  ->nAux = iArg+1;
1a900 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e  .    pVdbeFunc->
1a901 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46  pFunc = pCtx->pF
1a902 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78  unc;.  }..  pAux
1a903 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e  Data = &pVdbeFun
1a904 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a  c->apAux[iArg];.
1a905 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e    if( pAuxData->
1a906 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74 61  pAux && pAuxData
1a907 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20  ->xDelete ){.   
1a908 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65   pAuxData->xDele
1a909 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75  te(pAuxData->pAu
1a90a 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61  x);.  }.  pAuxDa
1a90b 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b  ta->pAux = pAux;
1a90c 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65  .  pAuxData->xDe
1a90d 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a  lete = xDelete;.
1a90e 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65    return;..faile
1a90f 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65  d:.  if( xDelete
1a910 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28   ){.    xDelete(
1a911 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  pAux);.  }.}..#i
1a912 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1a913 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a  T_DEPRECATED./*.
1a914 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1a915 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
1a916 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20  e Step function 
1a917 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68  of a aggregate h
1a918 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c  as been .** call
1a919 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ed..**.** This f
1a91a 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65  unction is depre
1a91b 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75  cated.  Do not u
1a91c 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f  se it for new co
1a91d 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72  de.  It is.** pr
1a91e 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76  ovide only to av
1a91f 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67  oid breaking leg
1a920 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61  acy code.  New a
1a921 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
1a922 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  n.** implementat
1a923 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70  ions should keep
1a924 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74   their own count
1a925 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 61  s within their a
1a926 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74  ggregate.** cont
1a927 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ext..*/.SQLITE_A
1a928 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61  PI int sqlite3_a
1a929 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73  ggregate_count(s
1a92a 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1a92b 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
1a92c 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d  && p->pMem && p-
1a92d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75  >pFunc && p->pFu
1a92e 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72  nc->xStep );.  r
1a92f 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e  eturn p->pMem->n
1a930 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1a931 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1a932 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
1a933 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
1a934 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65   for the stateme
1a935 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c  nt pStmt..*/.SQL
1a936 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1a937 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
1a938 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1a939 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70  Stmt){.  Vdbe *p
1a93a 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  Vm = (Vdbe *)pSt
1a93b 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d  mt;.  return pVm
1a93c 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75   ? pVm->nResColu
1a93d 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  mn : 0;.}../*.**
1a93e 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1a93f 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61  er of values ava
1a940 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20  ilable from the 
1a941 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74  current row of t
1a942 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  he.** currently 
1a943 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d  executing statem
1a944 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51  ent pStmt..*/.SQ
1a945 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1a946 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28  ite3_data_count(
1a947 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1a948 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56  tmt){.  Vdbe *pV
1a949 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  m = (Vdbe *)pStm
1a94a 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20  t;.  if( pVm==0 
1a94b 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53  || pVm->pResultS
1a94c 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  et==0 ) return 0
1a94d 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e  ;.  return pVm->
1a94e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a  nResColumn;.}...
1a94f 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
1a950 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f  ee if column iCo
1a951 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73  l of the given s
1a952 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69  tatement is vali
1a953 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c  d.  If.** it is,
1a954 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
1a955 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72  r to the Mem for
1a956 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1a957 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66  at column..** If
1a958 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c   iCol is not val
1a959 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69  id, return a poi
1a95a 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68  nter to a Mem wh
1a95b 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a  ich has a value.
1a95c 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73  ** of NULL..*/.s
1a95d 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d  tatic Mem *colum
1a95e 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d  nMem(sqlite3_stm
1a95f 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1a960 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20  {.  Vdbe *pVm;. 
1a961 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d   int vals;.  Mem
1a962 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d   *pOut;..  pVm =
1a963 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a   (Vdbe *)pStmt;.
1a964 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d    if( pVm && pVm
1a965 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20  ->pResultSet!=0 
1a966 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f  && i<pVm->nResCo
1a967 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a  lumn && i>=0 ){.
1a968 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1a969 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d  x_enter(pVm->db-
1a96a 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c  >mutex);.    val
1a96b 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61  s = sqlite3_data
1a96c 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20  _count(pStmt);. 
1a96d 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e     pOut = &pVm->
1a96e 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20  pResultSet[i];. 
1a96f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28   }else{.    /* (
1a970 28 64 6f 75 62 6c 65 29 30 29 20 49 6e 20 63 61  (double)0) In ca
1a971 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
1a972 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
1a973 2e 2e 2e 20 2a 2f 0a 20 20 20 20 73 74 61 74 69  ... */.    stati
1a974 63 20 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c  c const Mem null
1a975 4d 65 6d 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f 75  Mem = {{0}, (dou
1a976 62 6c 65 29 30 2c 20 30 2c 20 22 22 2c 20 30 2c  ble)0, 0, "", 0,
1a977 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54   MEM_Null, SQLIT
1a978 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20  E_NULL, 0, 0, 0 
1a979 7d 3b 0a 20 20 20 20 69 66 28 20 70 56 6d 20 26  };.    if( pVm &
1a97a 26 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e 64 62  & ALWAYS(pVm->db
1a97b 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
1a97c 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
1a97d 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  Vm->db->mutex);.
1a97e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1a97f 6f 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49  or(pVm->db, SQLI
1a980 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20  TE_RANGE, 0);.  
1a981 20 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 28    }.    pOut = (
1a982 4d 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20  Mem*)&nullMem;. 
1a983 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74   }.  return pOut
1a984 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
1a985 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1a986 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e  ed after invokin
1a987 67 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  g an sqlite3_val
1a988 75 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20  ue_XXX function 
1a989 6f 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  on a .** column 
1a98a 76 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61  value (i.e. a va
1a98b 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
1a98c 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51  evaluating an SQ
1a98d 4c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  L expression in 
1a98e 74 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69  the.** select li
1a98f 73 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73  st of a SELECT s
1a990 74 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d  tatement) that m
1a991 61 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f  ay cause a mallo
1a992 63 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20  c() failure. If 
1a993 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73  .** malloc() has
1a994 20 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72   failed, the thr
1a995 65 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65  eads mallocFaile
1a996 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65  d flag is cleare
1a997 64 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74  d and the result
1a998 0a 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74  .** code of stat
1a999 65 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20  ement pStmt set 
1a99a 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e  to SQLITE_NOMEM.
1a99b 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61  .**.** Specifica
1a99c 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c  lly, this is cal
1a99d 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a  led from within:
1a99e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  .**.**     sqlit
1a99f 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a  e3_column_int().
1a9a0 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
1a9a1 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a  olumn_int64().**
1a9a2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
1a9a3 75 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20  umn_text().**   
1a9a4 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1a9a5 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20  _text16().**    
1a9a6 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1a9a7 72 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71  real().**     sq
1a9a8 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
1a9a9 65 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  es().**     sqli
1a9aa 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
1a9ab 31 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e  16().**.** But n
1a9ac 6f 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63  ot for sqlite3_c
1a9ad 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68  olumn_blob(), wh
1a9ae 69 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20  ich never calls 
1a9af 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61  malloc()..*/.sta
1a9b0 74 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d  tic void columnM
1a9b1 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c  allocFailure(sql
1a9b2 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1a9b3 29 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c  ).{.  /* If mall
1a9b4 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69  oc() failed duri
1a9b5 6e 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63  ng an encoding c
1a9b6 6f 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e  onversion within
1a9b7 20 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33   an.  ** sqlite3
1a9b8 5f 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c  _column_XXX API,
1a9b9 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 65   then set the re
1a9ba 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65  turn code of the
1a9bb 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20   statement to.  
1a9bc 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e  ** SQLITE_NOMEM.
1a9bd 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74   The next call t
1a9be 6f 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e  o _step() (if an
1a9bf 79 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53  y) will return S
1a9c0 51 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a  QLITE_ERROR.  **
1a9c1 20 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29   and _finalize()
1a9c2 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d   will return NOM
1a9c3 45 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20  EM..  */.  Vdbe 
1a9c4 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
1a9c5 6d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  mt;.  if( p ){. 
1a9c6 20 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74     p->rc = sqlit
1a9c7 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c  e3ApiExit(p->db,
1a9c8 20 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c   p->rc);.    sql
1a9c9 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1a9ca 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1a9cb 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
1a9cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9cd 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c  **** sqlite3_col
1a9ce 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  umn_  **********
1a9cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1a9d0 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  *****.** The fol
1a9d1 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20  lowing routines 
1a9d2 61 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 65  are used to acce
1a9d3 73 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  ss elements of t
1a9d4 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a  he current row.*
1a9d5 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  * in the result 
1a9d6 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  set..*/.SQLITE_A
1a9d7 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1a9d8 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
1a9d9 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ob(sqlite3_stmt 
1a9da 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1a9db 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61    const void *va
1a9dc 6c 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 74  l;.  val = sqlit
1a9dd 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63  e3_value_blob( c
1a9de 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
1a9df 29 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74  ) );.  /* Even t
1a9e0 68 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 6e  hough there is n
1a9e1 6f 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65  o encoding conve
1a9e2 72 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f  rsion, value_blo
1a9e3 62 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e  b() might.  ** n
1a9e4 65 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c  eed to call mall
1a9e5 6f 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 74  oc() to expand t
1a9e6 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 7a  he result of a z
1a9e7 65 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20  eroblob() .  ** 
1a9e8 65 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a  expression. .  *
1a9e9 2f 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  /.  columnMalloc
1a9ea 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
1a9eb 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
1a9ec 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1a9ed 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
1a9ee 74 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  tes(sqlite3_stmt
1a9ef 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b   *pStmt, int i){
1a9f0 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c  .  int val = sql
1a9f1 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
1a9f2 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  ( columnMem(pStm
1a9f3 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e  t,i) );.  column
1a9f4 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53  MallocFailure(pS
1a9f5 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76  tmt);.  return v
1a9f6 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  al;.}.SQLITE_API
1a9f7 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
1a9f8 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69  umn_bytes16(sqli
1a9f9 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1a9fa 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76   int i){.  int v
1a9fb 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
1a9fc 75 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c 75  ue_bytes16( colu
1a9fd 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
1a9fe 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
1a9ff 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
1aa00 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
1aa01 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c  SQLITE_API doubl
1aa02 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
1aa03 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
1aa04 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1aa05 20 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61   i){.  double va
1aa06 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
1aa07 65 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e  e_double( column
1aa08 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a  Mem(pStmt,i) );.
1aa09 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61    columnMallocFa
1aa0a 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20  ilure(pStmt);.  
1aa0b 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51  return val;.}.SQ
1aa0c 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1aa0d 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  ite3_column_int(
1aa0e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1aa0f 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69  tmt, int i){.  i
1aa10 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33  nt val = sqlite3
1aa11 5f 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75  _value_int( colu
1aa12 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
1aa13 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
1aa14 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
1aa15 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
1aa16 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
1aa17 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  e_int64 sqlite3_
1aa18 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c  column_int64(sql
1aa19 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1aa1a 2c 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69  , int i){.  sqli
1aa1b 74 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73  te_int64 val = s
1aa1c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
1aa1d 36 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  64( columnMem(pS
1aa1e 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75  tmt,i) );.  colu
1aa1f 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
1aa20 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
1aa21 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41   val;.}.SQLITE_A
1aa22 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  PI const unsigne
1aa23 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  d char *sqlite3_
1aa24 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69  column_text(sqli
1aa25 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1aa26 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74   int i){.  const
1aa27 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
1aa28 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
1aa29 6c 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e  lue_text( column
1aa2a 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a  Mem(pStmt,i) );.
1aa2b 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61    columnMallocFa
1aa2c 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20  ilure(pStmt);.  
1aa2d 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51  return val;.}.SQ
1aa2e 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
1aa2f 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f  _value *sqlite3_
1aa30 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c  column_value(sql
1aa31 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1aa32 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20  , int i){.  Mem 
1aa33 2a 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65  *pOut = columnMe
1aa34 6d 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69  m(pStmt, i);.  i
1aa35 66 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d  f( pOut->flags&M
1aa36 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20  EM_Static ){.   
1aa37 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20   pOut->flags &= 
1aa38 7e 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20  ~MEM_Static;.   
1aa39 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20   pOut->flags |= 
1aa3a 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20  MEM_Ephem;.  }. 
1aa3b 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1aa3c 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1aa3d 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 76  eturn (sqlite3_v
1aa3e 61 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23  alue *)pOut;.}.#
1aa3f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1aa40 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1aa41 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1aa42 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
1aa43 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ext16(sqlite3_st
1aa44 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1aa45 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  ){.  const void 
1aa46 2a 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76  *val = sqlite3_v
1aa47 61 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f 6c  alue_text16( col
1aa48 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20  umnMem(pStmt,i) 
1aa49 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f  );.  columnMallo
1aa4a 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b  cFailure(pStmt);
1aa4b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d  .  return val;.}
1aa4c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1aa4d 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
1aa4e 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1aa4f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
1aa50 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  pe(sqlite3_stmt 
1aa51 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1aa52 20 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71    int iType = sq
1aa53 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
1aa54 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  ( columnMem(pStm
1aa55 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e  t,i) );.  column
1aa56 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53  MallocFailure(pS
1aa57 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  tmt);.  return i
1aa58 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20  Type;.}../* The 
1aa59 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69  following functi
1aa5a 6f 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74  on is experiment
1aa5b 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74  al and subject t
1aa5c 6f 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72  o change or.** r
1aa5d 65 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20  emoval */./*int 
1aa5e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
1aa5f 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69  umeric_type(sqli
1aa60 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1aa61 20 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74   int i){.**  ret
1aa62 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  urn sqlite3_valu
1aa63 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 20  e_numeric_type( 
1aa64 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c  columnMem(pStmt,
1aa65 69 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a  i) );.**}.*/../*
1aa66 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20  .** Convert the 
1aa67 4e 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20  N-th element of 
1aa68 70 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b  pStmt->pColName[
1aa69 5d 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20  ] into a string 
1aa6a 75 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29  using.** xFunc()
1aa6b 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61   then return tha
1aa6c 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20  t string.  If N 
1aa6d 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c  is out of range,
1aa6e 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a   return 0..**.**
1aa6f 20 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f   There are up to
1aa70 20 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63   5 names for eac
1aa71 68 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79  h column.  useTy
1aa72 70 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  pe determines wh
1aa73 69 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72  ich.** name is r
1aa74 65 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 61  eturned.  Here a
1aa75 72 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a  re the names:.**
1aa76 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 68  .**    0      Th
1aa77 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73  e column name as
1aa78 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 69   it should be di
1aa79 73 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 70  splayed for outp
1aa7a 75 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 20  ut.**    1      
1aa7b 54 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 6d  The datatype nam
1aa7c 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e  e for the column
1aa7d 0a 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 68  .**    2      Th
1aa7e 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  e name of the da
1aa7f 74 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20  tabase that the 
1aa80 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66  column derives f
1aa81 72 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 20  rom.**    3     
1aa82 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
1aa83 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20   table that the 
1aa84 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66  column derives f
1aa85 72 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 20  rom.**    4     
1aa86 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
1aa87 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68   table column th
1aa88 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  at the result co
1aa89 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f  lumn derives fro
1aa8a 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  m.**.** If the r
1aa8b 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 73  esult is not a s
1aa8c 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66  imple column ref
1aa8d 65 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 73  erence (if it is
1aa8e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a   an expression.*
1aa8f 2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 29  * or a constant)
1aa90 20 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 32   then useTypes 2
1aa91 2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 72  , 3, and 4 retur
1aa92 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69  n NULL..*/.stati
1aa93 63 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f  c const void *co
1aa94 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69  lumnName(.  sqli
1aa95 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1aa96 0a 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73  .  int N,.  cons
1aa97 74 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29  t void *(*xFunc)
1aa98 28 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73  (Mem*),.  int us
1aa99 65 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74  eType.){.  const
1aa9a 20 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a   void *ret = 0;.
1aa9b 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
1aa9c 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74  e *)pStmt;.  int
1aa9d 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   n;.  sqlite3 *d
1aa9e 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 20  b = p->db;.  .  
1aa9f 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
1aaa0 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63  .  n = sqlite3_c
1aaa1 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d  olumn_count(pStm
1aaa2 74 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20 26 26  t);.  if( N<n &&
1aaa3 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e 20 2b   N>=0 ){.    N +
1aaa4 3d 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20  = useType*n;.   
1aaa5 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1aaa6 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
1aaa7 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d  .    assert( db-
1aaa8 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30  >mallocFailed==0
1aaa9 20 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 78 46   );.    ret = xF
1aaaa 75 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65  unc(&p->aColName
1aaab 5b 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20 41 20  [N]);.     /* A 
1aaac 6d 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20  malloc may have 
1aaad 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66  failed inside of
1aaae 20 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c   the xFunc() cal
1aaaf 6c 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 2a  l. If this.    *
1aab0 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 63  * is the case, c
1aab1 6c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46  lear the mallocF
1aab2 61 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 72  ailed flag and r
1aab3 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20  eturn NULL..    
1aab4 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  */.    if( db->m
1aab5 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1aab6 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
1aab7 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20  ailed = 0;.     
1aab8 20 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a   ret = 0;.    }.
1aab9 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1aaba 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
1aabb 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  x);.  }.  return
1aabc 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   ret;.}../*.** R
1aabd 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f  eturn the name o
1aabe 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
1aabf 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
1aac0 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 53  et returned by S
1aac1 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  QL.** statement 
1aac2 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pStmt..*/.SQLITE
1aac3 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
1aac4 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1aac5 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
1aac6 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
1aac7 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
1aac8 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
1aac9 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
1aaca 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
1aacb 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c  ite3_value_text,
1aacc 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a   COLNAME_NAME);.
1aacd 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1aace 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1aacf 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1aad0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
1aad1 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33  n_name16(sqlite3
1aad2 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1aad3 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  t N){.  return c
1aad4 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20  olumnName(.     
1aad5 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73   pStmt, N, (cons
1aad6 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29  t void*(*)(Mem*)
1aad7 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1aad8 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  ext16, COLNAME_N
1aad9 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  AME);.}.#endif..
1aada 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74  /*.** Constraint
1aadb 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 45  :  If you have E
1aadc 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54  NABLE_COLUMN_MET
1aadd 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 6d  ADATA then you m
1aade 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e  ust.** not defin
1aadf 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e  e OMIT_DECLTYPE.
1aae0 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
1aae1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
1aae2 54 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 64  TYPE) && defined
1aae3 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43  (SQLITE_ENABLE_C
1aae4 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a  OLUMN_METADATA).
1aae5 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f  # error "Must no
1aae6 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51  t define both SQ
1aae7 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59  LITE_OMIT_DECLTY
1aae8 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 6e  PE \.         an
1aae9 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d SQLITE_ENABLE_
1aaea 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22  COLUMN_METADATA"
1aaeb 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1aaec 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
1aaed 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75  LTYPE./*.** Retu
1aaee 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65  rn the column de
1aaef 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 28  claration type (
1aaf0 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 6f  if applicable) o
1aaf1 66 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c 75  f the 'i'th colu
1aaf2 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73  mn.** of the res
1aaf3 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 73  ult set of SQL s
1aaf4 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a  tatement pStmt..
1aaf5 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1aaf6 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1aaf7 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
1aaf8 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1aaf9 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1aafa 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1aafb 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1aafc 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1aafd 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1aafe 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f  3_value_text, CO
1aaff 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b  LNAME_DECLTYPE);
1ab00 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1ab01 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1ab02 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1ab03 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
1ab04 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71  mn_decltype16(sq
1ab05 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1ab06 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1ab07 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1ab08 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1ab09 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1ab0a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1ab0b 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e  lue_text16, COLN
1ab0c 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d  AME_DECLTYPE);.}
1ab0d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1ab0e 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
1ab0f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1ab10 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a  _OMIT_DECLTYPE *
1ab11 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
1ab12 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
1ab13 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65  ETADATA./*.** Re
1ab14 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
1ab15 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72   the database fr
1ab16 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c  om which a resul
1ab17 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73  t column derives
1ab18 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74  ..** NULL is ret
1ab19 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73  urned if the res
1ab1a 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e  ult column is an
1ab1b 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63   expression or c
1ab1c 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e  onstant or.** an
1ab1d 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63  ything else whic
1ab1e 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62  h is not an unab
1ab1f 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65  iguous reference
1ab20 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63   to a database c
1ab21 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  olumn..*/.SQLITE
1ab22 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
1ab23 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1ab24 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71  database_name(sq
1ab25 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1ab26 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1ab27 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1ab28 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1ab29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1ab2a 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1ab2b 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d  lue_text, COLNAM
1ab2c 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23  E_DATABASE);.}.#
1ab2d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1ab2e 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1ab2f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1ab30 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
1ab31 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73  atabase_name16(s
1ab32 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1ab33 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1ab34 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1ab35 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1ab36 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1ab37 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1ab38 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
1ab39 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a  NAME_DATABASE);.
1ab3a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1ab3b 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1ab3c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1ab3d 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
1ab3e 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20  able from which 
1ab3f 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20  a result column 
1ab40 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c  derives..** NULL
1ab41 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
1ab42 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  the result colum
1ab43 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69  n is an expressi
1ab44 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f  on or constant o
1ab45 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c  r.** anything el
1ab46 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20  se which is not 
1ab47 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65  an unabiguous re
1ab48 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74  ference to a dat
1ab49 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f  abase column..*/
1ab4a 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1ab4b 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
1ab4c 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
1ab4d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1ab4e 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1ab4f 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1ab50 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1ab51 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1ab52 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1ab53 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f  3_value_text, CO
1ab54 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a  LNAME_TABLE);.}.
1ab55 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ab56 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
1ab57 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
1ab58 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
1ab59 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c  table_name16(sql
1ab5a 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1ab5b 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1ab5c 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1ab5d 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1ab5e 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1ab5f 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1ab60 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41  ue_text16, COLNA
1ab61 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e  ME_TABLE);.}.#en
1ab62 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1ab63 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a  IT_UTF16 */../*.
1ab64 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61  ** Return the na
1ab65 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1ab66 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63  column from whic
1ab67 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  h a result colum
1ab68 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55  n derives..** NU
1ab69 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  LL is returned i
1ab6a 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c  f the result col
1ab6b 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73  umn is an expres
1ab6c 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74  sion or constant
1ab6d 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20   or.** anything 
1ab6e 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f  else which is no
1ab6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20  t an unabiguous 
1ab70 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64  reference to a d
1ab71 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a  atabase column..
1ab72 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1ab73 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1ab74 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  3_column_origin_
1ab75 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
1ab76 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
1ab77 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
1ab78 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
1ab79 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
1ab7a 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
1ab7b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c  ite3_value_text,
1ab7c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29   COLNAME_COLUMN)
1ab7d 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1ab7e 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1ab7f 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1ab80 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
1ab81 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
1ab82 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
1ab83 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1ab84 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1ab85 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1ab86 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1ab87 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1ab88 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20  3_value_text16, 
1ab89 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b  COLNAME_COLUMN);
1ab8a 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1ab8b 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1ab8c 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  /.#endif /* SQLI
1ab8d 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
1ab8e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f  _METADATA */.../
1ab8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ab90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
1ab91 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a  sqlite3_bind_  *
1ab92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ab93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a  **********.** .*
1ab94 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20  * Routines used 
1ab95 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 73  to attach values
1ab96 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 6e   to wildcards in
1ab97 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20   a compiled SQL 
1ab98 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a  statement..*/./*
1ab99 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 76  .** Unbind the v
1ab9a 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 61  alue bound to va
1ab9b 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 74  riable i in virt
1ab9c 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 54  ual machine p. T
1ab9d 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 74  his is the .** t
1ab9e 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 69  he same as bindi
1ab9f 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20  ng a NULL value 
1aba0 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49  to the column. I
1aba1 66 20 74 68 65 20 22 69 22 20 70 61 72 61 6d 65  f the "i" parame
1aba2 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66  ter is.** out of
1aba3 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c   range, then SQL
1aba4 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 74  ITE_RANGE is ret
1aba5 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 20  urned. Othewise 
1aba6 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a  SQLITE_OK..**.**
1aba7 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76   A successful ev
1aba8 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  aluation of this
1aba9 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65   routine acquire
1abaa 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70  s the mutex on p
1abab 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69  ..** the mutex i
1abac 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e  s released if an
1abad 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20  y kind of error 
1abae 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  occurs..**.** Th
1abaf 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f  e error code sto
1abb0 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20  red in database 
1abb1 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69  p->db is overwri
1abb2 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65  tten with the re
1abb3 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e  turn.** value in
1abb4 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74   any case..*/.st
1abb5 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62  atic int vdbeUnb
1abb6 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ind(Vdbe *p, int
1abb7 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72   i){.  Mem *pVar
1abb8 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
1abb9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1abba 55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  USE;.  sqlite3_m
1abbb 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62  utex_enter(p->db
1abbc 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
1abbd 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  p->magic!=VDBE_M
1abbe 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70  AGIC_RUN || p->p
1abbf 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  c>=0 ){.    sqli
1abc0 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20  te3Error(p->db, 
1abc1 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30  SQLITE_MISUSE, 0
1abc2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1abc3 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
1abc4 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65  ->mutex);.    re
1abc5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
1abc6 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c  SE;.  }.  if( i<
1abc7 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29  1 || i>p->nVar )
1abc8 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1abc9 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45  or(p->db, SQLITE
1abca 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20  _RANGE, 0);.    
1abcb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1abcc 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
1abcd 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
1abce 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a  LITE_RANGE;.  }.
1abcf 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20    i--;.  pVar = 
1abd0 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73  &p->aVar[i];.  s
1abd1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1abd2 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56  ease(pVar);.  pV
1abd3 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ar->flags = MEM_
1abd4 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45  Null;.  sqlite3E
1abd5 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49  rror(p->db, SQLI
1abd6 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74  TE_OK, 0);.  ret
1abd7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1abd8 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74  ../*.** Bind a t
1abd9 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75  ext or BLOB valu
1abda 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1abdb 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 6c   bindText(.  sql
1abdc 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1abdd 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65  ,   /* The state
1abde 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 61  ment to bind aga
1abdf 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c  inst */.  int i,
1abe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1abe1 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
1abe2 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69   parameter to bi
1abe3 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nd */.  const vo
1abe4 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f  id *zData,     /
1abe5 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
1abe6 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e   data to be boun
1abe7 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61  d */.  int nData
1abe8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
1abe9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1abea 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62   of data to be b
1abeb 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28  ound */.  void (
1abec 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20  *xDel)(void*),  
1abed 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
1abee 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20  or the data */. 
1abef 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 20 20   u8 encoding    
1abf0 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64          /* Encod
1abf1 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61  ing for the data
1abf2 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70   */.){.  Vdbe *p
1abf3 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
1abf4 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20  ;.  Mem *pVar;. 
1abf5 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d   int rc;..  rc =
1abf6 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
1abf7 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1abf8 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
1abf9 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20 20  ( zData!=0 ){.  
1abfa 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61      pVar = &p->a
1abfb 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20  Var[i-1];.      
1abfc 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1abfd 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c 20  MemSetStr(pVar, 
1abfe 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e  zData, nData, en
1abff 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20  coding, xDel);. 
1ac00 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1ac01 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69  ITE_OK && encodi
1ac02 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ng!=0 ){.       
1ac03 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1ac04 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1ac05 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29  pVar, ENC(p->db)
1ac06 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1ac07 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d   sqlite3Error(p-
1ac08 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20  >db, rc, 0);.   
1ac09 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
1ac0a 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72 63  piExit(p->db, rc
1ac0b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
1ac0c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1ac0d 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1ac0e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1ac0f 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20  .}.../*.** Bind 
1ac10 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f 20  a blob value to 
1ac11 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
1ac12 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 51   variable..*/.SQ
1ac13 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1ac14 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 0a  ite3_bind_blob(.
1ac15 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1ac16 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c  pStmt, .  int i,
1ac17 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
1ac18 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44  zData, .  int nD
1ac19 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  ata, .  void (*x
1ac1a 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20  Del)(void*).){. 
1ac1b 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74   return bindText
1ac1c 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61  (pStmt, i, zData
1ac1d 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 30  , nData, xDel, 0
1ac1e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1ac1f 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1ac20 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
1ac21 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1ac22 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 75   i, double rValu
1ac23 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e){.  int rc;.  
1ac24 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
1ac25 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20  *)pStmt;.  rc = 
1ac26 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29  vdbeUnbind(p, i)
1ac27 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1ac28 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
1ac29 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f  ite3VdbeMemSetDo
1ac2a 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69 2d  uble(&p->aVar[i-
1ac2b 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20 20  1], rValue);.   
1ac2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1ac2d 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
1ac2e 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  x);.  }.  return
1ac2f 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   rc;.}.SQLITE_AP
1ac30 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1ac31 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73  nd_int(sqlite3_s
1ac32 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20 69  tmt *p, int i, i
1ac33 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72 65  nt iValue){.  re
1ac34 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e  turn sqlite3_bin
1ac35 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28 69  d_int64(p, i, (i
1ac36 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 51  64)iValue);.}.SQ
1ac37 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1ac38 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
1ac39 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1ac3a 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69  tmt, int i, sqli
1ac3b 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 29  te_int64 iValue)
1ac3c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64  {.  int rc;.  Vd
1ac3d 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
1ac3e 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64  pStmt;.  rc = vd
1ac3f 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
1ac40 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1ac41 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
1ac42 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1ac43 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c  4(&p->aVar[i-1],
1ac44 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71   iValue);.    sq
1ac45 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1ac46 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1ac47 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1ac48 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
1ac49 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1ac4a 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d  null(sqlite3_stm
1ac4b 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1ac4c 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64  {.  int rc;.  Vd
1ac4d 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70  be *p = (Vdbe*)p
1ac4e 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62  Stmt;.  rc = vdb
1ac4f 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20  eUnbind(p, i);. 
1ac50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1ac51 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1ac52 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
1ac53 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
1ac54 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1ac55 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1ac56 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
1ac57 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ( .  sqlite3_stm
1ac58 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74  t *pStmt, .  int
1ac59 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61   i, .  const cha
1ac5a 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74  r *zData, .  int
1ac5b 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20   nData, .  void 
1ac5c 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
1ac5d 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54  {.  return bindT
1ac5e 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44  ext(pStmt, i, zD
1ac5f 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c  ata, nData, xDel
1ac60 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
1ac61 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1ac62 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1ac63 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1ac64 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 0a  e3_bind_text16(.
1ac65 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1ac66 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c  pStmt, .  int i,
1ac67 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
1ac68 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44  zData, .  int nD
1ac69 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  ata, .  void (*x
1ac6a 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20  Del)(void*).){. 
1ac6b 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74   return bindText
1ac6c 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61  (pStmt, i, zData
1ac6d 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53  , nData, xDel, S
1ac6e 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
1ac6f 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  E);.}.#endif /* 
1ac70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1ac71 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  6 */.SQLITE_API 
1ac72 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1ac73 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73  _value(sqlite3_s
1ac74 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1ac75 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  i, const sqlite3
1ac76 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b  _value *pValue){
1ac77 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 77 69  .  int rc;.  swi
1ac78 74 63 68 28 20 70 56 61 6c 75 65 2d 3e 74 79 70  tch( pValue->typ
1ac79 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  e ){.    case SQ
1ac7a 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a  LITE_INTEGER: {.
1ac7b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1ac7c 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53  e3_bind_int64(pS
1ac7d 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e  tmt, i, pValue->
1ac7e 75 2e 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61  u.i);.      brea
1ac7f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1ac80 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20  e SQLITE_FLOAT: 
1ac81 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1ac82 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65  ite3_bind_double
1ac83 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75  (pStmt, i, pValu
1ac84 65 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62 72 65  e->r);.      bre
1ac85 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1ac86 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20  se SQLITE_BLOB: 
1ac87 7b 0a 20 20 20 20 20 20 69 66 28 20 70 56 61 6c  {.      if( pVal
1ac88 75 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ue->flags & MEM_
1ac89 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  Zero ){.        
1ac8a 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
1ac8b 64 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74 6d 74  d_zeroblob(pStmt
1ac8c 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 6e  , i, pValue->u.n
1ac8d 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Zero);.      }el
1ac8e 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  se{.        rc =
1ac8f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
1ac90 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61  ob(pStmt, i, pVa
1ac91 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e  lue->z, pValue->
1ac92 6e 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  n,SQLITE_TRANSIE
1ac93 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  NT);.      }.   
1ac94 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1ac95 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
1ac96 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 72 63  TEXT: {.      rc
1ac97 20 3d 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d   = bindText(pStm
1ac98 74 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e 7a 2c  t,i,  pValue->z,
1ac99 20 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51 4c 49   pValue->n, SQLI
1ac9a 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a 20 20  TE_TRANSIENT,.  
1ac9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ac9c 20 20 20 20 20 20 20 20 20 20 20 20 70 56 61 6c              pVal
1ac9d 75 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20  ue->enc);.      
1ac9e 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1ac9f 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
1aca0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
1aca1 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74 2c 20  ind_null(pStmt, 
1aca2 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  i);.      break;
1aca3 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1aca4 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
1aca5 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1aca6 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73  _bind_zeroblob(s
1aca7 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1aca8 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e  mt, int i, int n
1aca9 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56  ){.  int rc;.  V
1acaa 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
1acab 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76  )pStmt;.  rc = v
1acac 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b  dbeUnbind(p, i);
1acad 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1acae 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
1acaf 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72  te3VdbeMemSetZer
1acb0 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72 5b 69  oBlob(&p->aVar[i
1acb1 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c  -1], n);.    sql
1acb2 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1acb3 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1acb4 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1acb5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1acb6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77   the number of w
1acb7 69 6c 64 63 61 72 64 73 20 74 68 61 74 20 63 61  ildcards that ca
1acb8 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c 6c 79  n be potentially
1acb9 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68   bound to..** Th
1acba 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 64  is routine is ad
1acbb 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 44  ded to support D
1acbc 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a 2a 2f  BD::SQLite.  .*/
1acbd 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1acbe 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
1acbf 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c  ameter_count(sql
1acc0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1acc1 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
1acc2 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72  Vdbe*)pStmt;.  r
1acc3 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61  eturn p ? p->nVa
1acc4 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r : 0;.}../*.** 
1acc5 43 72 65 61 74 65 20 61 20 6d 61 70 70 69 6e 67  Create a mapping
1acc6 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e   from variable n
1acc7 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69 61 62  umbers to variab
1acc8 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74  le names.** in t
1acc9 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20  he Vdbe.azVar[] 
1acca 61 72 72 61 79 2c 20 69 66 20 73 75 63 68 20 61  array, if such a
1accb 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f   mapping does no
1accc 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69  t already.** exi
1accd 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  st..*/.static vo
1acce 69 64 20 63 72 65 61 74 65 56 61 72 4d 61 70 28  id createVarMap(
1accf 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  Vdbe *p){.  if( 
1acd0 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20  !p->okVar ){.   
1acd1 20 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70 20 2a   int j;.    Op *
1acd2 70 4f 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  pOp;.    sqlite3
1acd3 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e  _mutex_enter(p->
1acd4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
1acd5 2f 2a 20 54 68 65 20 72 61 63 65 20 63 6f 6e 64  /* The race cond
1acd6 69 74 69 6f 6e 20 68 65 72 65 20 69 73 20 68 61  ition here is ha
1acd7 72 6d 6c 65 73 73 2e 20 20 49 66 20 74 77 6f 20  rmless.  If two 
1acd8 74 68 72 65 61 64 73 20 63 61 6c 6c 20 74 68 69  threads call thi
1acd9 73 0a 20 20 20 20 2a 2a 20 72 6f 75 74 69 6e 65  s.    ** routine
1acda 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 56 64 62   on the same Vdb
1acdb 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  e at the same ti
1acdc 6d 65 2c 20 74 68 65 79 20 62 6f 74 68 20 6d 69  me, they both mi
1acdd 67 68 74 20 65 6e 64 0a 20 20 20 20 2a 2a 20 75  ght end.    ** u
1acde 70 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  p initializing t
1acdf 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20  he Vdbe.azVar[] 
1ace0 61 72 72 61 79 2e 20 20 54 68 61 74 20 69 73 20  array.  That is 
1ace1 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 0a 20  a little extra. 
1ace2 20 20 20 2a 2a 20 77 6f 72 6b 20 62 75 74 20 69     ** work but i
1ace3 74 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65  t results in the
1ace4 20 73 61 6d 65 20 61 6e 73 77 65 72 2e 0a 20 20   same answer..  
1ace5 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30    */.    for(j=0
1ace6 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c  , pOp=p->aOp; j<
1ace7 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70  p->nOp; j++, pOp
1ace8 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
1ace9 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56  Op->opcode==OP_V
1acea 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  ariable ){.     
1aceb 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1acec 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  p1>0 && pOp->p1<
1aced 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 20 20  =p->nVar );.    
1acee 20 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70 4f 70      p->azVar[pOp
1acef 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70  ->p1-1] = pOp->p
1acf0 34 2e 7a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  4.z;.      }.   
1acf1 20 7d 0a 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20   }.    p->okVar 
1acf2 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 1;.    sqlite3
1acf3 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
1acf4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  db->mutex);.  }.
1acf5 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1acf6 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77 69  the name of a wi
1acf7 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72  ldcard parameter
1acf8 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
1acf9 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 69  f the index.** i
1acfa 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f  s out of range o
1acfb 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 61 72  r if the wildcar
1acfc 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a 2a  d is unnamed..**
1acfd 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69  .** The result i
1acfe 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e 0a  s always UTF-8..
1acff 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1ad00 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1ad01 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
1ad02 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
1ad03 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1ad04 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
1ad05 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69  Vdbe*)pStmt;.  i
1ad06 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c  f( p==0 || i<1 |
1ad07 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20  | i>p->nVar ){. 
1ad08 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1ad09 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28  .  createVarMap(
1ad0a 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p);.  return p->
1ad0b 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f  azVar[i-1];.}../
1ad0c 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69 6c  *.** Given a wil
1ad0d 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 20  dcard parameter 
1ad0e 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68 65  name, return the
1ad0f 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 61   index of the va
1ad10 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20 74  riable.** with t
1ad11 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74 68  hat name.  If th
1ad12 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61 62  ere is no variab
1ad13 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76 65  le with the give
1ad14 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 72  n name,.** retur
1ad15 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  n 0..*/.SQLITE_A
1ad16 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1ad17 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
1ad18 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  dex(sqlite3_stmt
1ad19 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63   *pStmt, const c
1ad1a 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56  har *zName){.  V
1ad1b 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29  dbe *p = (Vdbe*)
1ad1c 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a  pStmt;.  int i;.
1ad1d 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20    if( p==0 ){.  
1ad1e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1ad1f 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 70    createVarMap(p
1ad20 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20  ); .  if( zName 
1ad21 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
1ad22 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b  i<p->nVar; i++){
1ad23 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
1ad24 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 5b  r *z = p->azVar[
1ad25 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20  i];.      if( z 
1ad26 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 6d  && strcmp(z,zNam
1ad27 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)==0 ){.       
1ad28 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20   return i+1;.   
1ad29 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1ad2a 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
1ad2b 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c  .** Transfer all
1ad2c 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74   bindings from t
1ad2d 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65  he first stateme
1ad2e 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 73  nt over to the s
1ad2f 65 63 6f 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  econd..*/.SQLITE
1ad30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1ad31 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64  ite3TransferBind
1ad32 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
1ad33 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71  t *pFromStmt, sq
1ad34 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53  lite3_stmt *pToS
1ad35 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46  tmt){.  Vdbe *pF
1ad36 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72  rom = (Vdbe*)pFr
1ad37 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a  omStmt;.  Vdbe *
1ad38 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f  pTo = (Vdbe*)pTo
1ad39 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Stmt;.  int i;. 
1ad3a 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 64 62   assert( pTo->db
1ad3b 3d 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 3b 0a 20  ==pFrom->db );. 
1ad3c 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 6e 56   assert( pTo->nV
1ad3d 61 72 3d 3d 70 46 72 6f 6d 2d 3e 6e 56 61 72 20  ar==pFrom->nVar 
1ad3e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
1ad3f 65 78 5f 65 6e 74 65 72 28 70 54 6f 2d 3e 64 62  ex_enter(pTo->db
1ad40 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28  ->mutex);.  for(
1ad41 69 3d 30 3b 20 69 3c 70 46 72 6f 6d 2d 3e 6e 56  i=0; i<pFrom->nV
1ad42 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  ar; i++){.    sq
1ad43 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
1ad44 28 26 70 54 6f 2d 3e 61 56 61 72 5b 69 5d 2c 20  (&pTo->aVar[i], 
1ad45 26 70 46 72 6f 6d 2d 3e 61 56 61 72 5b 69 5d 29  &pFrom->aVar[i])
1ad46 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1ad47 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 54 6f 2d  mutex_leave(pTo-
1ad48 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  >db->mutex);.  r
1ad49 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1ad4a 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
1ad4b 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
1ad4c 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61  ED./*.** Depreca
1ad4d 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74  ted external int
1ad4e 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61  erface.  Interna
1ad4f 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f  l/core SQLite co
1ad50 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c  de.** should cal
1ad51 6c 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65  l sqlite3Transfe
1ad52 72 42 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a  rBindings..**.**
1ad53 20 49 73 20 69 73 20 6d 69 73 75 73 65 20 74 6f   Is is misuse to
1ad54 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
1ad55 6e 65 20 77 69 74 68 20 73 74 61 74 65 6d 65 6e  ne with statemen
1ad56 74 73 20 66 72 6f 6d 20 64 69 66 66 65 72 65 6e  ts from differen
1ad57 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  t.** database co
1ad58 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20  nnections.  But 
1ad59 61 73 20 74 68 69 73 20 69 73 20 61 20 64 65 70  as this is a dep
1ad5a 72 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 63  recated interfac
1ad5b 65 2c 20 77 65 0a 2a 2a 20 77 69 6c 6c 20 6e 6f  e, we.** will no
1ad5c 74 20 62 6f 74 68 65 72 20 74 6f 20 63 68 65 63  t bother to chec
1ad5d 6b 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 64 69  k for that condi
1ad5e 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  tion..**.** If t
1ad5f 68 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74  he two statement
1ad60 73 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66  s contain a diff
1ad61 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  erent number of 
1ad62 62 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a  bindings, then.*
1ad63 2a 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  * an SQLITE_ERRO
1ad64 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  R is returned.  
1ad65 4e 6f 74 68 69 6e 67 20 65 6c 73 65 20 63 61 6e  Nothing else can
1ad66 20 67 6f 20 77 72 6f 6e 67 2c 20 73 6f 20 6f 74   go wrong, so ot
1ad67 68 65 72 77 69 73 65 0a 2a 2a 20 53 51 4c 49 54  herwise.** SQLIT
1ad68 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
1ad69 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1ad6a 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e  int sqlite3_tran
1ad6b 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71  sfer_bindings(sq
1ad6c 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f  lite3_stmt *pFro
1ad6d 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73  mStmt, sqlite3_s
1ad6e 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20  tmt *pToStmt){. 
1ad6f 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28   Vdbe *pFrom = (
1ad70 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b  Vdbe*)pFromStmt;
1ad71 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28  .  Vdbe *pTo = (
1ad72 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20  Vdbe*)pToStmt;. 
1ad73 20 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72   if( pFrom->nVar
1ad74 21 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20  !=pTo->nVar ){. 
1ad75 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1ad76 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65  _ERROR;.  }.  re
1ad77 74 75 72 6e 20 73 71 6c 69 74 65 33 54 72 61 6e  turn sqlite3Tran
1ad78 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 46 72  sferBindings(pFr
1ad79 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29  omStmt, pToStmt)
1ad7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1ad7b 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 71 6c  * Return the sql
1ad7c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68  ite3* database h
1ad7d 61 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20 74  andle to which t
1ad7e 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
1ad7f 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69  ement given.** i
1ad80 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 62  n the argument b
1ad81 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 73  elongs.  This is
1ad82 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
1ad83 73 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77  se handle that w
1ad84 61 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  as.** the first 
1ad85 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
1ad86 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
1ad87 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20  ) that was used 
1ad88 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 65  to create.** the
1ad89 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68   statement in th
1ad8a 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a  e first place..*
1ad8b 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
1ad8c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62  ite3 *sqlite3_db
1ad8d 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f  _handle(sqlite3_
1ad8e 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
1ad8f 72 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 28  return pStmt ? (
1ad90 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64  (Vdbe*)pStmt)->d
1ad91 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  b : 0;.}../*.** 
1ad92 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1ad93 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 72 65   to the next pre
1ad94 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
1ad95 61 66 74 65 72 20 70 53 74 6d 74 20 61 73 73 6f  after pStmt asso
1ad96 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 64  ciated.** with d
1ad97 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1ad98 6f 6e 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d  on pDb.  If pStm
1ad99 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72  t is NULL, retur
1ad9a 6e 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70  n the first.** p
1ad9b 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1ad9c 74 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61  t for the databa
1ad9d 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20  se connection.  
1ad9e 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74  Return NULL if t
1ad9f 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d  here.** are no m
1ada0 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ore..*/.SQLITE_A
1ada1 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  PI sqlite3_stmt 
1ada2 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74  *sqlite3_next_st
1ada3 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c  mt(sqlite3 *pDb,
1ada4 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
1ada5 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  Stmt){.  sqlite3
1ada6 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20  _stmt *pNext;.  
1ada7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1ada8 74 65 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b  ter(pDb->mutex);
1ada9 0a 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20  .  if( pStmt==0 
1adaa 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28  ){.    pNext = (
1adab 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 44  sqlite3_stmt*)pD
1adac 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73  b->pVdbe;.  }els
1adad 65 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28  e{.    pNext = (
1adae 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 28  sqlite3_stmt*)((
1adaf 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e  Vdbe*)pStmt)->pN
1adb0 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ext;.  }.  sqlit
1adb1 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1adb2 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  Db->mutex);.  re
1adb3 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f  turn pNext;.}../
1adb4 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1adb5 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75  value of a statu
1adb6 73 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 20  s counter for a 
1adb7 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1adb8 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  nt.*/.SQLITE_API
1adb9 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d   int sqlite3_stm
1adba 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33  t_status(sqlite3
1adbb 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1adbc 74 20 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 46  t op, int resetF
1adbd 6c 61 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 56  lag){.  Vdbe *pV
1adbe 64 62 65 20 3d 20 28 56 64 62 65 2a 29 70 53 74  dbe = (Vdbe*)pSt
1adbf 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 56  mt;.  int v = pV
1adc0 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70  dbe->aCounter[op
1adc1 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74  -1];.  if( reset
1adc2 46 6c 61 67 20 29 20 70 56 64 62 65 2d 3e 61 43  Flag ) pVdbe->aC
1adc3 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30  ounter[op-1] = 0
1adc4 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a  ;.  return v;.}.
1adc5 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1adc6 20 45 6e 64 20 6f 66 20 76 64 62 65 61 70 69 2e   End of vdbeapi.
1adc7 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1adc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1adca 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1adcb 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65   Begin file vdbe
1adcc 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1adcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1adce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1adcf 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
1add0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
1add1 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1add2 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1add3 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1add4 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1add5 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1add6 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1add7 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1add8 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1add9 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1adda 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1addb 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1addc 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1addd 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1adde 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1addf 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1ade0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1ade1 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1ade2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ade3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ade4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ade5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ade6 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63  *******.** The c
1ade7 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
1ade8 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63   implements exec
1ade9 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20  ution method of 
1adea 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20  the .** Virtual 
1adeb 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20  Database Engine 
1adec 28 56 44 42 45 29 2e 20 20 41 20 73 65 70 61 72  (VDBE).  A separ
1aded 61 74 65 20 66 69 6c 65 20 28 22 76 64 62 65 61  ate file ("vdbea
1adee 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65  ux.c").** handle
1adef 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64  s housekeeping d
1adf0 65 74 61 69 6c 73 20 73 75 63 68 20 61 73 20 63  etails such as c
1adf1 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65  reating and dele
1adf2 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73  ting.** VDBE ins
1adf3 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69  tances.  This fi
1adf4 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74  le is solely int
1adf5 65 72 65 73 74 65 64 20 69 6e 20 65 78 65 63 75  erested in execu
1adf6 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45  ting.** the VDBE
1adf7 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20   program..**.** 
1adf8 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20  In the external 
1adf9 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73  interface, an "s
1adfa 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73  qlite3_stmt*" is
1adfb 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74   an opaque point
1adfc 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e  er.** to a VDBE.
1adfd 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70  .**.** The SQL p
1adfe 61 72 73 65 72 20 67 65 6e 65 72 61 74 65 73 20  arser generates 
1adff 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20  a program which 
1ae00 69 73 20 74 68 65 6e 20 65 78 65 63 75 74 65 64  is then executed
1ae01 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20   by.** the VDBE 
1ae02 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f  to do the work o
1ae03 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  f the SQL statem
1ae04 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72  ent.  VDBE progr
1ae05 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69  ams are .** simi
1ae06 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61  lar in form to a
1ae07 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65  ssembly language
1ae08 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63  .  The program c
1ae09 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20  onsists of.** a 
1ae0a 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20  linear sequence 
1ae0b 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20  of operations.  
1ae0c 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68  Each operation h
1ae0d 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a  as an opcode .**
1ae0e 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e   and 5 operands.
1ae0f 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50    Operands P1, P
1ae10 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e  2, and P3 are in
1ae11 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64  tegers.  Operand
1ae12 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c   P4 .** is a nul
1ae13 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  l-terminated str
1ae14 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35  ing.  Operand P5
1ae15 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20   is an unsigned 
1ae16 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65  character..** Fe
1ae17 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c  w opcodes use al
1ae18 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a  l 5 operands..**
1ae19 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20  .** Computation 
1ae1a 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72  results are stor
1ae1b 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72  ed on a set of r
1ae1c 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65  egisters numbere
1ae1d 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77  d beginning.** w
1ae1e 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20  ith 1 and going 
1ae1f 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e  up to Vdbe.nMem.
1ae20 20 20 45 61 63 68 20 72 65 67 69 73 74 65 72 20    Each register 
1ae21 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74  can store.** eit
1ae22 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20  her an integer, 
1ae23 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
1ae24 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61  d string, a floa
1ae25 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75  ting point.** nu
1ae26 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c  mber, or the SQL
1ae27 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20   "NULL" value.  
1ae28 41 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76  An implicit conv
1ae29 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a  ersion from one.
1ae2a 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f  ** type to the o
1ae2b 74 68 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e  ther occurs as n
1ae2c 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a  ecessary..** .**
1ae2d 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64   Most of the cod
1ae2e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
1ae2f 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68  s taken up by th
1ae30 65 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  e sqlite3VdbeExe
1ae31 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  c().** function 
1ae32 77 68 69 63 68 20 64 6f 65 73 20 74 68 65 20 77  which does the w
1ae33 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74  ork of interpret
1ae34 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72  ing a VDBE progr
1ae35 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72  am..** But other
1ae36 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c   routines are al
1ae37 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68  so provided to h
1ae38 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20  elp in building 
1ae39 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20  up.** a program 
1ae3a 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69  instruction by i
1ae3b 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
1ae3c 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69 70 74  * Various script
1ae3d 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72  s scan this sour
1ae3e 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72  ce file in order
1ae3f 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d   to generate HTM
1ae40 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69  L.** documentati
1ae41 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65  on, headers file
1ae42 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69  s, or other deri
1ae43 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20  ved files.  The 
1ae44 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66  formatting.** of
1ae45 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   the code in thi
1ae46 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65  s file is, there
1ae47 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e  fore, important.
1ae48 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d    See other comm
1ae49 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20  ents.** in this 
1ae4a 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73  file for details
1ae4b 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20  .  If in doubt, 
1ae4c 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66  do not deviate f
1ae4d 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20  rom existing.** 
1ae4e 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69  commenting and i
1ae4f 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74  ndentation pract
1ae50 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69  ices when changi
1ae51 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64  ng or adding cod
1ae52 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64  e..**.** $Id: vd
1ae53 62 65 2e 63 2c 76 20 31 2e 38 34 32 20 32 30 30  be.c,v 1.842 200
1ae54 39 2f 30 35 2f 30 36 20 31 38 3a 35 37 3a 31 30  9/05/06 18:57:10
1ae55 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
1ae56 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
1ae57 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69  wing global vari
1ae58 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e  able is incremen
1ae59 74 65 64 20 65 76 65 72 79 20 74 69 6d 65 20 61  ted every time a
1ae5a 20 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73   cursor.** moves
1ae5b 2c 20 65 69 74 68 65 72 20 62 79 20 74 68 65 20  , either by the 
1ae5c 4f 50 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65  OP_SeekXX, OP_Ne
1ae5d 78 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f  xt, or OP_Prev o
1ae5e 70 63 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73  pcodes.  The tes
1ae5f 74 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20  t.** procedures 
1ae60 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61  use this informa
1ae61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72  tion to make sur
1ae62 65 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61  e that indices a
1ae63 72 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f  re.** working co
1ae64 72 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76  rrectly.  This v
1ae65 61 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66  ariable has no f
1ae66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68  unction other th
1ae67 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65  an to.** help ve
1ae68 72 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74  rify the correct
1ae69 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   operation of th
1ae6a 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69  e library..*/.#i
1ae6b 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1ae6c 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1ae6d 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63  sqlite3_search_c
1ae6e 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
1ae6f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ../*.** When thi
1ae70 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  s global variabl
1ae71 65 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69  e is positive, i
1ae72 74 20 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74  t gets decrement
1ae73 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a  ed once before.*
1ae74 2a 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69  * each instructi
1ae75 6f 6e 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20  on in the VDBE. 
1ae76 20 57 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65   When reaches ze
1ae77 72 6f 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74  ro, the u1.isInt
1ae78 65 72 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c  errupted.** fiel
1ae79 64 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  d of the sqlite3
1ae7a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 65   structure is se
1ae7b 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69  t in order to si
1ae7c 6d 75 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72  mulate and inter
1ae7d 72 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rupt..**.** This
1ae7e 20 66 61 63 69 6c 69 74 79 20 69 73 20 75 73 65   facility is use
1ae7f 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75  d for testing pu
1ae80 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74  rposes only.  It
1ae81 20 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69   does not functi
1ae82 6f 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69  on.** in an ordi
1ae83 6e 61 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23  nary build..*/.#
1ae84 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1ae85 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
1ae86 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1ae87 70 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  pt_count = 0;.#e
1ae88 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1ae89 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69  next global vari
1ae8a 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e  able is incremen
1ae8b 74 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68  ted each type th
1ae8c 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65  e OP_Sort opcode
1ae8d 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e  .** is executed.
1ae8e 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65    The test proce
1ae8f 64 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69  dures use this i
1ae90 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61  nformation to ma
1ae91 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20  ke sure that.** 
1ae92 73 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72  sorting is occur
1ae93 72 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75  ring or not occu
1ae94 72 72 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72  rring at appropr
1ae95 69 61 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68  iate times.   Th
1ae96 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68  is variable.** h
1ae97 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f  as no function o
1ae98 74 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c  ther than to hel
1ae99 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72  p verify the cor
1ae9a 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f  rect operation o
1ae9b 66 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79  f the.** library
1ae9c 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
1ae9d 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
1ae9e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
1ae9f 6f 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ort_count = 0;.#
1aea0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
1aea1 20 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72   next global var
1aea2 69 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68  iable records th
1aea3 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61  e size of the la
1aea4 72 67 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a  rgest MEM_Blob.*
1aea5 2a 20 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61  * or MEM_Str tha
1aea6 74 20 68 61 73 20 62 65 65 6e 20 75 73 65 64 20  t has been used 
1aea7 62 79 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65  by a VDBE opcode
1aea8 2e 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63  .  The test proc
1aea9 65 64 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68  edures.** use th
1aeaa 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  is information t
1aeab 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
1aeac 20 74 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66   the zero-blob f
1aead 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20  unctionality.** 
1aeae 69 73 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65  is working corre
1aeaf 63 74 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72  ctly.   This var
1aeb0 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e  iable has no fun
1aeb1 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e  ction other than
1aeb2 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69   to.** help veri
1aeb3 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  fy the correct o
1aeb4 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  peration of the 
1aeb5 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64  library..*/.#ifd
1aeb6 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
1aeb7 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1aeb8 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69  lite3_max_blobsi
1aeb9 7a 65 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76  ze = 0;.static v
1aeba 6f 69 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f  oid updateMaxBlo
1aebb 62 73 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20  bsize(Mem *p){. 
1aebc 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26   if( (p->flags &
1aebd 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c   (MEM_Str|MEM_Bl
1aebe 6f 62 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e  ob))!=0 && p->n>
1aebf 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62  sqlite3_max_blob
1aec0 73 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  size ){.    sqli
1aec1 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65  te3_max_blobsize
1aec2 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23   = p->n;.  }.}.#
1aec3 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73  endif../*.** Tes
1aec4 74 20 61 20 72 65 67 69 73 74 65 72 20 74 6f 20  t a register to 
1aec5 73 65 65 20 69 66 20 69 74 20 65 78 63 65 65 64  see if it exceed
1aec6 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61  s the current ma
1aec7 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e  ximum blob size.
1aec8 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20  .** If it does, 
1aec9 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d  record the new m
1aeca 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65  aximum blob size
1aecb 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
1aecc 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26  (SQLITE_TEST) &&
1aecd 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1aece 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
1aecf 53 54 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44  ST).# define UPD
1aed0 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1aed1 28 50 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c  (P)  updateMaxBl
1aed2 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a  obsize(P).#else.
1aed3 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f  # define UPDATE_
1aed4 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a  MAX_BLOBSIZE(P).
1aed5 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f  #endif../*.** Co
1aed6 6e 76 65 72 74 20 74 68 65 20 67 69 76 65 6e 20  nvert the given 
1aed7 72 65 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20  register into a 
1aed8 73 74 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e  string if it isn
1aed9 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64  't one.** alread
1aeda 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  y. Return non-ze
1aedb 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  ro if a malloc()
1aedc 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69   fails..*/.#defi
1aedd 6e 65 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20  ne Stringify(P, 
1aede 65 6e 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50  enc) \.   if(((P
1aedf 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74  )->flags&(MEM_St
1aee0 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20  r|MEM_Blob))==0 
1aee1 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  && sqlite3VdbeMe
1aee2 6d 53 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63  mStringify(P,enc
1aee3 29 29 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f  )) \.     { goto
1aee4 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a   no_mem; }../*.*
1aee5 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73  * An ephemeral s
1aee6 74 72 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67  tring value (sig
1aee7 6e 69 66 69 65 64 20 62 79 20 74 68 65 20 4d 45  nified by the ME
1aee8 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f  M_Ephem flag) co
1aee9 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e  ntains.** a poin
1aeea 74 65 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63  ter to a dynamic
1aeeb 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ally allocated s
1aeec 74 72 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65  tring where some
1aeed 20 6f 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a   other entity.**
1aeee 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
1aeef 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67  for deallocating
1aef0 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42   that string.  B
1aef1 65 63 61 75 73 65 20 74 68 65 20 72 65 67 69 73  ecause the regis
1aef2 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  ter.** does not 
1aef3 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69  control the stri
1aef4 6e 67 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20  ng, it might be 
1aef5 64 65 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20  deleted without 
1aef6 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20  the register.** 
1aef7 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a  knowing it..**.*
1aef8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
1aef9 6f 6e 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d  onverts an ephem
1aefa 65 72 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f  eral string into
1aefb 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61   a dynamically a
1aefc 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69  llocated.** stri
1aefd 6e 67 20 74 68 61 74 20 74 68 65 20 72 65 67 69  ng that the regi
1aefe 73 74 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74  ster itself cont
1aeff 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20  rols.  In other 
1af00 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e  words, it.** con
1af01 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68  verts an MEM_Eph
1af02 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61  em string into a
1af03 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67  n MEM_Dyn string
1af04 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65  ..*/.#define Dee
1af05 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c  phemeralize(P) \
1af06 0a 20 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c  .   if( ((P)->fl
1af07 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d  ags&MEM_Ephem)!=
1af08 30 20 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71  0 \.       && sq
1af09 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
1af0a 57 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20  Writeable(P) ){ 
1af0b 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f  goto no_mem;}../
1af0c 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
1af0d 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
1af0e 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70  ob() on the supp
1af0f 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65  lied value (type
1af10 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72   Mem*).** P if r
1af11 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66  equired..*/.#def
1af12 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50  ine ExpandBlob(P
1af13 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d  ) (((P)->flags&M
1af14 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33  EM_Zero)?sqlite3
1af15 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
1af16 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41  b(P):0)../*.** A
1af17 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69  rgument pMem poi
1af18 6e 74 73 20 61 74 20 61 20 72 65 67 69 73 74 65  nts at a registe
1af19 72 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70  r that will be p
1af1a 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73  assed to a.** us
1af1b 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er-defined funct
1af1c 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20  ion or returned 
1af1d 74 6f 20 74 68 65 20 75 73 65 72 20 61 73 20 74  to the user as t
1af1e 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71  he result of a q
1af1f 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63  uery..** The sec
1af20 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 27 64  ond argument, 'd
1af21 62 5f 65 6e 63 27 20 69 73 20 74 68 65 20 74 65  b_enc' is the te
1af22 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64  xt encoding used
1af23 20 62 79 20 74 68 65 20 76 64 62 65 20 66 6f 72   by the vdbe for
1af24 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 76 61 72  .** register var
1af25 69 61 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f  iables.  This ro
1af26 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70  utine sets the p
1af27 4d 65 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65  Mem->enc and pMe
1af28 6d 2d 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61  m->type.** varia
1af29 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 65  bles used by the
1af2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a   sqlite3_value_*
1af2b 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  () routines..*/.
1af2c 23 64 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70  #define storeTyp
1af2d 65 49 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72  eInfo(A,B) _stor
1af2e 65 54 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61  eTypeInfo(A).sta
1af2f 74 69 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54  tic void _storeT
1af30 79 70 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65  ypeInfo(Mem *pMe
1af31 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20  m){.  int flags 
1af32 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20  = pMem->flags;. 
1af33 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
1af34 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65  _Null ){.    pMe
1af35 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1af36 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73  _NULL;.  }.  els
1af37 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45  e if( flags & ME
1af38 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65  M_Int ){.    pMe
1af39 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1af3a 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20  _INTEGER;.  }.  
1af3b 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26  else if( flags &
1af3c 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20   MEM_Real ){.   
1af3d 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
1af3e 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a  LITE_FLOAT;.  }.
1af3f 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73    else if( flags
1af40 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20   & MEM_Str ){.  
1af41 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
1af42 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65  QLITE_TEXT;.  }e
1af43 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74  lse{.    pMem->t
1af44 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f  ype = SQLITE_BLO
1af45 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  B;.  }.}../*.** 
1af46 50 72 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70  Properties of op
1af47 63 6f 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c  codes.  The OPFL
1af48 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61  G_INITIALIZER ma
1af49 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65  cro is.** create
1af4a 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61  d by mkopcodeh.a
1af4b 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c  wk during compil
1af4c 61 74 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20  ation.  Data is 
1af4d 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d  obtained.** from
1af4e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f   the comments fo
1af4f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73  llowing the "cas
1af50 65 20 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74  e OP_xxxx:" stat
1af51 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 69  ements in.** thi
1af52 73 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61  s file.  .*/.sta
1af53 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
1af54 65 64 20 63 68 61 72 20 6f 70 63 6f 64 65 50 72  ed char opcodePr
1af55 6f 70 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47  operty[] = OPFLG
1af56 5f 49 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f  _INITIALIZER;../
1af57 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
1af58 20 69 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 61   if an opcode ha
1af59 73 20 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 46  s any of the OPF
1af5a 4c 47 5f 78 78 78 20 70 72 6f 70 65 72 74 69 65  LG_xxx propertie
1af5b 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62  s.** specified b
1af5c 79 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54  y mask..*/.SQLIT
1af5d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1af5e 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48  lite3VdbeOpcodeH
1af5f 61 73 50 72 6f 70 65 72 74 79 28 69 6e 74 20 6f  asProperty(int o
1af60 70 63 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b 29  pcode, int mask)
1af61 7b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f  {.  assert( opco
1af62 64 65 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c 28  de>0 && opcode<(
1af63 69 6e 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f 64  int)sizeof(opcod
1af64 65 50 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 20  eProperty) );.  
1af65 72 65 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 72  return (opcodePr
1af66 6f 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 6d  operty[opcode]&m
1af67 61 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  ask)!=0;.}../*.*
1af68 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43  * Allocate VdbeC
1af69 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75  ursor number iCu
1af6a 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  r.  Return a poi
1af6b 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74  nter to it.  Ret
1af6c 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77  urn NULL.** if w
1af6d 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d  e run out of mem
1af6e 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56  ory..*/.static V
1af6f 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63  dbeCursor *alloc
1af70 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62  ateCursor(.  Vdb
1af71 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
1af72 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
1af73 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69  l machine */.  i
1af74 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20  nt iCur,        
1af75 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1af76 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 72   the new VdbeCur
1af77 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69  sor */.  int nFi
1af78 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f  eld,           /
1af79 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * Number of fiel
1af7a 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ds in the table 
1af7b 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  or index */.  in
1af7c 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20  t iDb,          
1af7d 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 61      /* When data
1af7e 62 61 73 65 20 74 68 65 20 63 75 72 73 6f 72 20  base the cursor 
1af7f 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d  belongs to, or -
1af80 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 72  1 */.  int isBtr
1af81 65 65 43 75 72 73 6f 72 20 20 20 20 20 2f 2a 20  eeCursor     /* 
1af82 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20  */.){.  /* Find 
1af83 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
1af84 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65  that will be use
1af85 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62  d to store the b
1af86 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20  lob of memory.  
1af87 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  ** required for 
1af88 74 68 69 73 20 56 64 62 65 43 75 72 73 6f 72 20  this VdbeCursor 
1af89 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 69 73  structure. It is
1af8a 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75   convenient to u
1af8b 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62 65 20  se a .  ** vdbe 
1af8c 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d  memory cell to m
1af8d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f 72 79  anage the memory
1af8e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75   allocation requ
1af8f 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20  ired for a.  ** 
1af90 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 75 63  VdbeCursor struc
1af91 74 75 72 65 20 66 6f 72 20 74 68 65 20 66 6f 6c  ture for the fol
1af92 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a  lowing reasons:.
1af93 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f    **.  **   * So
1af94 6d 65 74 69 6d 65 73 20 63 75 72 73 6f 72 20 6e  metimes cursor n
1af95 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 20  umbers are used 
1af96 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 6f 66 20  for a couple of 
1af97 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20  different.  **  
1af98 20 20 20 70 75 72 70 6f 73 65 73 20 69 6e 20 61     purposes in a
1af99 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 54   vdbe program. T
1af9a 68 65 20 64 69 66 66 65 72 65 6e 74 20 75 73 65  he different use
1af9b 73 20 6d 69 67 68 74 20 72 65 71 75 69 72 65 0a  s might require.
1af9c 20 20 2a 2a 20 20 20 20 20 64 69 66 66 65 72 65    **     differe
1af9d 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74  nt sized allocat
1af9e 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c  ions. Memory cel
1af9f 6c 73 20 70 72 6f 76 69 64 65 20 67 72 6f 77 61  ls provide growa
1afa0 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c  ble.  **     all
1afa1 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20  ocations..  **. 
1afa2 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 69   **   * When usi
1afa3 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  ng ENABLE_MEMORY
1afa4 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d  _MANAGEMENT, mem
1afa5 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65 72 73  ory cell buffers
1afa6 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65   can.  **     be
1afa7 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20 76 69   freed lazily vi
1afa8 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  a the sqlite3_re
1afa9 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41  lease_memory() A
1afaa 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20 20  PI. This.  **   
1afab 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65 20    minimizes the 
1afac 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63  number of malloc
1afad 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 74   calls made by t
1afae 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a  he system..  **.
1afaf 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c    ** Memory cell
1afb0 73 20 66 6f 72 20 63 75 72 73 6f 72 73 20 61 72  s for cursors ar
1afb1 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 74  e allocated at t
1afb2 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 61 64  he top of the ad
1afb3 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 61 63 65  dress.  ** space
1afb4 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70  . Memory cell (p
1afb5 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f  ->nMem) correspo
1afb6 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 20 30 2e  nds to cursor 0.
1afb7 20 53 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20   Space for.  ** 
1afb8 63 75 72 73 6f 72 20 31 20 69 73 20 6d 61 6e 61  cursor 1 is mana
1afb9 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 20 63 65  ged by memory ce
1afba 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20  ll (p->nMem-1), 
1afbb 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20  etc..  */.  Mem 
1afbc 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d  *pMem = &p->aMem
1afbd 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a  [p->nMem-iCur];.
1afbe 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20  .  int nByte;.  
1afbf 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 20  VdbeCursor *pCx 
1afc0 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 0a  = 0;.  nByte = .
1afc1 20 20 20 20 20 20 73 69 7a 65 6f 66 28 56 64 62        sizeof(Vdb
1afc2 65 43 75 72 73 6f 72 29 20 2b 20 0a 20 20 20 20  eCursor) + .    
1afc3 20 20 28 69 73 42 74 72 65 65 43 75 72 73 6f 72    (isBtreeCursor
1afc4 3f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72  ?sqlite3BtreeCur
1afc5 73 6f 72 53 69 7a 65 28 29 3a 30 29 20 2b 20 0a  sorSize():0) + .
1afc6 20 20 20 20 20 20 32 2a 6e 46 69 65 6c 64 2a 73        2*nField*s
1afc7 69 7a 65 6f 66 28 75 33 32 29 3b 0a 0a 20 20 61  izeof(u32);..  a
1afc8 73 73 65 72 74 28 20 69 43 75 72 3c 70 2d 3e 6e  ssert( iCur<p->n
1afc9 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20  Cursor );.  if( 
1afca 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 29  p->apCsr[iCur] )
1afcb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1afcc 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70  eFreeCursor(p, p
1afcd 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 29 3b 0a  ->apCsr[iCur]);.
1afce 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 75      p->apCsr[iCu
1afcf 72 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  r] = 0;.  }.  if
1afd0 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c  ( SQLITE_OK==sql
1afd1 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1afd2 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20  pMem, nByte, 0) 
1afd3 29 7b 0a 20 20 20 20 70 2d 3e 61 70 43 73 72 5b  ){.    p->apCsr[
1afd4 69 43 75 72 5d 20 3d 20 70 43 78 20 3d 20 28 56  iCur] = pCx = (V
1afd5 64 62 65 43 75 72 73 6f 72 2a 29 70 4d 65 6d 2d  dbeCursor*)pMem-
1afd6 3e 7a 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  >z;.    memset(p
1afd7 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 65  Mem->z, 0, nByte
1afd8 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 62 20  );.    pCx->iDb 
1afd9 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78 2d 3e  = iDb;.    pCx->
1afda 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b  nField = nField;
1afdb 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c 64 20  .    if( nField 
1afdc 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 61 54  ){.      pCx->aT
1afdd 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26 70 4d  ype = (u32 *)&pM
1afde 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 62  em->z[sizeof(Vdb
1afdf 65 43 75 72 73 6f 72 29 5d 3b 0a 20 20 20 20 7d  eCursor)];.    }
1afe0 0a 20 20 20 20 69 66 28 20 69 73 42 74 72 65 65  .    if( isBtree
1afe1 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20  Cursor ){.      
1afe2 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 3d 20 28  pCx->pCursor = (
1afe3 42 74 43 75 72 73 6f 72 2a 29 0a 20 20 20 20 20  BtCursor*).     
1afe4 20 20 20 20 20 26 70 4d 65 6d 2d 3e 7a 5b 73 69       &pMem->z[si
1afe5 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29  zeof(VdbeCursor)
1afe6 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66  +2*nField*sizeof
1afe7 28 75 33 32 29 5d 3b 0a 20 20 20 20 7d 0a 20 20  (u32)];.    }.  
1afe8 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a  }.  return pCx;.
1afe9 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20  }../*.** Try to 
1afea 63 6f 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20  convert a value 
1afeb 69 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72  into a numeric r
1afec 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66  epresentation if
1afed 20 77 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f   we can.** do so
1afee 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66   without loss of
1afef 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49   information.  I
1aff0 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
1aff1 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20  f the string.** 
1aff2 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d  looks like a num
1aff3 62 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20  ber, convert it 
1aff4 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20  into a number.  
1aff5 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a  If it does not.*
1aff6 2a 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75  * look like a nu
1aff7 6d 62 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61  mber, leave it a
1aff8 6c 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lone..*/.static 
1aff9 76 6f 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69  void applyNumeri
1affa 63 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70  cAffinity(Mem *p
1affb 52 65 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65  Rec){.  if( (pRe
1affc 63 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  c->flags & (MEM_
1affd 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d  Real|MEM_Int))==
1affe 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61  0 ){.    int rea
1afff 6c 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  lnum;.    sqlite
1b000 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
1b001 6e 61 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20  nate(pRec);.    
1b002 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73  if( (pRec->flags
1b003 26 4d 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20  &MEM_Str).      
1b004 20 20 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e     && sqlite3IsN
1b005 75 6d 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26  umber(pRec->z, &
1b006 72 65 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65  realnum, pRec->e
1b007 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34  nc) ){.      i64
1b008 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71   value;.      sq
1b009 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
1b00a 6e 63 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51  ncoding(pRec, SQ
1b00b 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20  LITE_UTF8);.    
1b00c 20 20 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26    if( !realnum &
1b00d 26 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28  & sqlite3Atoi64(
1b00e 70 52 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29  pRec->z, &value)
1b00f 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65 63   ){.        pRec
1b010 2d 3e 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a 20  ->u.i = value;. 
1b011 20 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70         MemSetTyp
1b012 65 46 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d 5f  eFlag(pRec, MEM_
1b013 49 6e 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Int);.      }els
1b014 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
1b015 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79  e3VdbeMemRealify
1b016 28 70 52 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a  (pRec);.      }.
1b017 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1b018 2a 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73  ** Processing is
1b019 20 64 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68   determine by th
1b01a 65 20 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d  e affinity param
1b01b 65 74 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  eter:.**.** SQLI
1b01c 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a  TE_AFF_INTEGER:.
1b01d 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45  ** SQLITE_AFF_RE
1b01e 41 4c 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46  AL:.** SQLITE_AF
1b01f 46 5f 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20  F_NUMERIC:.**   
1b020 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   Try to convert 
1b021 70 52 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 67  pRec to an integ
1b022 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  er representatio
1b023 6e 20 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c  n or a .**    fl
1b024 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70  oating-point rep
1b025 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 61  resentation if a
1b026 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73  n integer repres
1b027 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69  entation.**    i
1b028 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20  s not possible. 
1b029 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 69   Note that the i
1b02a 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74  nteger represent
1b02b 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61  ation is.**    a
1b02c 6c 77 61 79 73 20 70 72 65 66 65 72 72 65 64 2c  lways preferred,
1b02d 20 65 76 65 6e 20 69 66 20 74 68 65 20 61 66 66   even if the aff
1b02e 69 6e 69 74 79 20 69 73 20 52 45 41 4c 2c 20 62  inity is REAL, b
1b02f 65 63 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20  ecause.**    an 
1b030 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e  integer represen
1b031 74 61 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73  tation is more s
1b032 70 61 63 65 20 65 66 66 69 63 69 65 6e 74 20 6f  pace efficient o
1b033 6e 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51  n disk..**.** SQ
1b034 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a  LITE_AFF_TEXT:.*
1b035 2a 20 20 20 20 43 6f 6e 76 65 72 74 20 70 52 65  *    Convert pRe
1b036 63 20 74 6f 20 61 20 74 65 78 74 20 72 65 70 72  c to a text repr
1b037 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  esentation..**.*
1b038 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  * SQLITE_AFF_NON
1b039 45 3a 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20  E:.**    No-op. 
1b03a 20 70 52 65 63 20 69 73 20 75 6e 63 68 61 6e 67   pRec is unchang
1b03b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
1b03c 69 64 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79  id applyAffinity
1b03d 28 0a 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20  (.  Mem *pRec,  
1b03e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
1b03f 61 6c 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66  alue to apply af
1b040 66 69 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 63  finity to */.  c
1b041 68 61 72 20 61 66 66 69 6e 69 74 79 2c 20 20 20  har affinity,   
1b042 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69     /* The affini
1b043 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64  ty to be applied
1b044 20 2a 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 20   */.  u8 enc    
1b045 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
1b046 20 74 68 69 73 20 74 65 78 74 20 65 6e 63 6f 64   this text encod
1b047 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  ing */.){.  if( 
1b048 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
1b049 5f 41 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 20  _AFF_TEXT ){.   
1b04a 20 2f 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 74   /* Only attempt
1b04b 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
1b04c 74 6f 20 54 45 58 54 20 69 66 20 74 68 65 72 65  to TEXT if there
1b04d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f   is an integer o
1b04e 72 20 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 65  r real.    ** re
1b04f 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 6c  presentation (bl
1b050 6f 62 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e  ob and NULL do n
1b051 6f 74 20 67 65 74 20 63 6f 6e 76 65 72 74 65 64  ot get converted
1b052 29 20 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 0a  ) but no string.
1b053 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74      ** represent
1b054 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
1b055 20 20 69 66 28 20 30 3d 3d 28 70 52 65 63 2d 3e    if( 0==(pRec->
1b056 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 26  flags&MEM_Str) &
1b057 26 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 28  & (pRec->flags&(
1b058 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74  MEM_Real|MEM_Int
1b059 29 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  )) ){.      sqli
1b05a 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67  te3VdbeMemString
1b05b 69 66 79 28 70 52 65 63 2c 20 65 6e 63 29 3b 0a  ify(pRec, enc);.
1b05c 20 20 20 20 7d 0a 20 20 20 20 70 52 65 63 2d 3e      }.    pRec->
1b05d 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 52  flags &= ~(MEM_R
1b05e 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  eal|MEM_Int);.  
1b05f 7d 65 6c 73 65 20 69 66 28 20 61 66 66 69 6e 69  }else if( affini
1b060 74 79 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ty!=SQLITE_AFF_N
1b061 4f 4e 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ONE ){.    asser
1b062 74 28 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  t( affinity==SQL
1b063 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20  ITE_AFF_INTEGER 
1b064 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  || affinity==SQL
1b065 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20  ITE_AFF_REAL.   
1b066 20 20 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66            || aff
1b067 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46  inity==SQLITE_AF
1b068 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20  F_NUMERIC );.   
1b069 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66   applyNumericAff
1b06a 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20  inity(pRec);.   
1b06b 20 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73   if( pRec->flags
1b06c 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
1b06d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b06e 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28  IntegerAffinity(
1b06f 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pRec);.    }.  }
1b070 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f  .}../*.** Try to
1b071 20 63 6f 6e 76 65 72 74 20 74 68 65 20 74 79 70   convert the typ
1b072 65 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20  e of a function 
1b073 61 72 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 65  argument or a re
1b074 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69  sult column.** i
1b075 6e 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65  nto a numeric re
1b076 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 55  presentation.  U
1b077 73 65 20 65 69 74 68 65 72 20 49 4e 54 45 47 45  se either INTEGE
1b078 52 20 6f 72 20 52 45 41 4c 20 77 68 69 63 68 65  R or REAL whiche
1b079 76 65 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f 70  ver.** is approp
1b07a 72 69 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c 79  riate.  But only
1b07b 20 64 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69   do the conversi
1b07c 6f 6e 20 69 66 20 69 74 20 69 73 20 70 6f 73 73  on if it is poss
1b07d 69 62 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20  ible without.** 
1b07e 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74  loss of informat
1b07f 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ion and return t
1b080 68 65 20 72 65 76 69 73 65 64 20 74 79 70 65 20  he revised type 
1b081 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e  of the argument.
1b082 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  .**.** This is a
1b083 6e 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 61  n EXPERIMENTAL a
1b084 70 69 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63  pi and is subjec
1b085 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 72  t to change or r
1b086 65 6d 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54  emoval..*/.SQLIT
1b087 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b088 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
1b089 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  type(sqlite3_val
1b08a 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d  ue *pVal){.  Mem
1b08b 20 2a 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 70   *pMem = (Mem*)p
1b08c 56 61 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65  Val;.  applyNume
1b08d 72 69 63 41 66 66 69 6e 69 74 79 28 70 4d 65 6d  ricAffinity(pMem
1b08e 29 3b 0a 20 20 73 74 6f 72 65 54 79 70 65 49 6e  );.  storeTypeIn
1b08f 66 6f 28 70 4d 65 6d 2c 20 30 29 3b 0a 20 20 72  fo(pMem, 0);.  r
1b090 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70 65  eturn pMem->type
1b091 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72  ;.}../*.** Expor
1b092 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 61  ted version of a
1b093 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29 2e 20  pplyAffinity(). 
1b094 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20 6f  This one works o
1b095 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  n sqlite3_value*
1b096 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69 6e  , .** not the in
1b097 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70 65  ternal Mem* type
1b098 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b099 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1b09a 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69  ValueApplyAffini
1b09b 74 79 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ty(.  sqlite3_va
1b09c 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75 38  lue *pVal, .  u8
1b09d 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 20 75 38   affinity, .  u8
1b09e 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79 41   enc.){.  applyA
1b09f 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29 70  ffinity((Mem *)p
1b0a0 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65  Val, affinity, e
1b0a1 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  nc);.}..#ifdef S
1b0a2 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a  QLITE_DEBUG./*.*
1b0a3 2a 20 57 72 69 74 65 20 61 20 6e 69 63 65 20 73  * Write a nice s
1b0a4 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
1b0a5 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e 74  tion of the cont
1b0a6 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d 65  ents of cell pMe
1b0a7 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65 72  m.** into buffer
1b0a8 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e 42   zBuf, length nB
1b0a9 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  uf..*/.SQLITE_PR
1b0aa 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1b0ab 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50  e3VdbeMemPrettyP
1b0ac 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  rint(Mem *pMem, 
1b0ad 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 63  char *zBuf){.  c
1b0ae 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75 66  har *zCsr = zBuf
1b0af 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d 65 6d  ;.  int f = pMem
1b0b0 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61 74  ->flags;..  stat
1b0b1 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
1b0b2 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d 20  onst encnames[] 
1b0b3 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 29 22 2c  = {"(X)", "(8)",
1b0b4 20 22 28 31 36 4c 45 29 22 2c 20 22 28 31 36 42   "(16LE)", "(16B
1b0b5 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26 4d  E)"};..  if( f&M
1b0b6 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20 69  EM_Blob ){.    i
1b0b7 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 63  nt i;.    char c
1b0b8 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45  ;.    if( f & ME
1b0b9 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 63  M_Dyn ){.      c
1b0ba 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73   = 'z';.      as
1b0bb 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f  sert( (f & (MEM_
1b0bc 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d  Static|MEM_Ephem
1b0bd 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c  ))==0 );.    }el
1b0be 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53  se if( f & MEM_S
1b0bf 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 63  tatic ){.      c
1b0c0 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73   = 't';.      as
1b0c1 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f  sert( (f & (MEM_
1b0c2 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d  Dyn|MEM_Ephem))=
1b0c3 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  =0 );.    }else 
1b0c4 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65  if( f & MEM_Ephe
1b0c5 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27  m ){.      c = '
1b0c6 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e';.      assert
1b0c7 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74  ( (f & (MEM_Stat
1b0c8 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20  ic|MEM_Dyn))==0 
1b0c9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1b0ca 20 20 20 20 63 20 3d 20 27 73 27 3b 0a 20 20 20      c = 's';.   
1b0cb 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   }..    sqlite3_
1b0cc 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43  snprintf(100, zC
1b0cd 73 72 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20 20  sr, "%c", c);.  
1b0ce 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65    zCsr += sqlite
1b0cf 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b  3Strlen30(zCsr);
1b0d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1b0d1 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c  rintf(100, zCsr,
1b0d2 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29   "%d[", pMem->n)
1b0d3 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71  ;.    zCsr += sq
1b0d4 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43  lite3Strlen30(zC
1b0d5 73 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  sr);.    for(i=0
1b0d6 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d  ; i<16 && i<pMem
1b0d7 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ->n; i++){.     
1b0d8 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1b0d9 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30  f(100, zCsr, "%0
1b0da 32 58 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d  2X", ((int)pMem-
1b0db 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a  >z[i] & 0xFF));.
1b0dc 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71        zCsr += sq
1b0dd 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43  lite3Strlen30(zC
1b0de 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  sr);.    }.    f
1b0df 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20  or(i=0; i<16 && 
1b0e0 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b  i<pMem->n; i++){
1b0e1 0a 20 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20  .      char z = 
1b0e2 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20  pMem->z[i];.    
1b0e3 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e    if( z<32 || z>
1b0e4 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20  126 ) *zCsr++ = 
1b0e5 27 2e 27 3b 0a 20 20 20 20 20 20 65 6c 73 65 20  '.';.      else 
1b0e6 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20  *zCsr++ = z;.   
1b0e7 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   }..    sqlite3_
1b0e8 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43  snprintf(100, zC
1b0e9 73 72 2c 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61  sr, "]%s", encna
1b0ea 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b  mes[pMem->enc]);
1b0eb 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c  .    zCsr += sql
1b0ec 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73  ite3Strlen30(zCs
1b0ed 72 29 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20  r);.    if( f & 
1b0ee 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20  MEM_Zero ){.    
1b0ef 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1b0f0 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25  tf(100, zCsr,"+%
1b0f1 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72  dz",pMem->u.nZer
1b0f2 6f 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b  o);.      zCsr +
1b0f3 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1b0f4 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20  0(zCsr);.    }. 
1b0f5 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c 30 27 3b     *zCsr = '\0';
1b0f6 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26  .  }else if( f &
1b0f7 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20   MEM_Str ){.    
1b0f8 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 7a 42  int j, k;.    zB
1b0f9 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20  uf[0] = ' ';.   
1b0fa 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e   if( f & MEM_Dyn
1b0fb 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31   ){.      zBuf[1
1b0fc 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61  ] = 'z';.      a
1b0fd 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
1b0fe 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65  _Static|MEM_Ephe
1b0ff 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65  m))==0 );.    }e
1b100 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f  lse if( f & MEM_
1b101 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20  Static ){.      
1b102 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 3b 0a 20  zBuf[1] = 't';. 
1b103 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20       assert( (f 
1b104 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45  & (MEM_Dyn|MEM_E
1b105 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20  phem))==0 );.   
1b106 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d   }else if( f & M
1b107 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20  EM_Ephem ){.    
1b108 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 65 27 3b    zBuf[1] = 'e';
1b109 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
1b10a 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c  f & (MEM_Static|
1b10b 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b 0a  MEM_Dyn))==0 );.
1b10c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1b10d 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 27 3b 0a   zBuf[1] = 's';.
1b10e 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d 20 32 3b      }.    k = 2;
1b10f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
1b110 72 69 6e 74 66 28 31 30 30 2c 20 26 7a 42 75 66  rintf(100, &zBuf
1b111 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d 65 6d 2d  [k], "%d", pMem-
1b112 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71  >n);.    k += sq
1b113 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a  lite3Strlen30(&z
1b114 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75  Buf[k]);.    zBu
1b115 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20  f[k++] = '[';.  
1b116 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 20    for(j=0; j<15 
1b117 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b  && j<pMem->n; j+
1b118 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 63 20 3d  +){.      u8 c =
1b119 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20   pMem->z[j];.   
1b11a 20 20 20 69 66 28 20 63 3e 3d 30 78 32 30 20 26     if( c>=0x20 &
1b11b 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 20 20 20  & c<0x7f ){.    
1b11c 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
1b11d 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  c;.      }else{.
1b11e 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b          zBuf[k++
1b11f 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 7d  ] = '.';.      }
1b120 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b  .    }.    zBuf[
1b121 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20 20  k++] = ']';.    
1b122 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1b123 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65  (100,&zBuf[k], e
1b124 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e  ncnames[pMem->en
1b125 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71  c]);.    k += sq
1b126 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a  lite3Strlen30(&z
1b127 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75  Buf[k]);.    zBu
1b128 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a  f[k++] = 0;.  }.
1b129 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  }.#endif..#ifdef
1b12a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
1b12b 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61  .** Print the va
1b12c 6c 75 65 20 6f 66 20 61 20 72 65 67 69 73 74 65  lue of a registe
1b12d 72 20 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75  r for tracing pu
1b12e 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 74 69  rposes:.*/.stati
1b12f 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 63 65 50  c void memTraceP
1b130 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 74 2c 20  rint(FILE *out, 
1b131 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  Mem *p){.  if( p
1b132 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1b133 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  ll ){.    fprint
1b134 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 29 3b  f(out, " NULL");
1b135 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d  .  }else if( (p-
1b136 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e  >flags & (MEM_In
1b137 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45  t|MEM_Str))==(ME
1b138 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29  M_Int|MEM_Str) )
1b139 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  {.    fprintf(ou
1b13a 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70  t, " si:%lld", p
1b13b 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20  ->u.i);.  }else 
1b13c 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  if( p->flags & M
1b13d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 66 70  EM_Int ){.    fp
1b13e 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 69 3a 25  rintf(out, " i:%
1b13f 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20  lld", p->u.i);. 
1b140 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c   }else if( p->fl
1b141 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29  ags & MEM_Real )
1b142 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  {.    fprintf(ou
1b143 74 2c 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72  t, " r:%g", p->r
1b144 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
1b145 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
1b146 77 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69  wSet ){.    fpri
1b147 6e 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73  ntf(out, " (rows
1b148 65 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  et)");.  }else{.
1b149 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30      char zBuf[20
1b14a 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0];.    sqlite3V
1b14b 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e  dbeMemPrettyPrin
1b14c 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20  t(p, zBuf);.    
1b14d 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22  fprintf(out, " "
1b14e 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  );.    fprintf(o
1b14f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b  ut, "%s", zBuf);
1b150 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
1b151 69 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65  id registerTrace
1b152 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20  (FILE *out, int 
1b153 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20  iReg, Mem *p){. 
1b154 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52   fprintf(out, "R
1b155 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67  EG[%d] = ", iReg
1b156 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69  );.  memTracePri
1b157 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70  nt(out, p);.  fp
1b158 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29  rintf(out, "\n")
1b159 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  ;.}.#endif..#ifd
1b15a 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1b15b 23 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54  #  define REGIST
1b15c 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66  ER_TRACE(R,M) if
1b15d 28 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74  (p->trace)regist
1b15e 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65  erTrace(p->trace
1b15f 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64  ,R,M).#else.#  d
1b160 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54  efine REGISTER_T
1b161 52 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66  RACE(R,M).#endif
1b162 0a 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50  ...#ifdef VDBE_P
1b163 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68  ROFILE../* .** h
1b164 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73  wtime.h contains
1b165 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65   inline assemble
1b166 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65  r code for imple
1b167 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68  menting .** high
1b168 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d  -performance tim
1b169 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f  ing routines..*/
1b16a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1b16b 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e   Include hwtime.
1b16c 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
1b16d 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a  of vdbe.c ******
1b16e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1b16f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1b170 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69   Begin file hwti
1b171 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  me.h ***********
1b172 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b173 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1b174 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20  ./*.** 2008 May 
1b175 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  27.**.** The aut
1b176 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1b177 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1b178 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1b179 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1b17a 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1b17b 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1b17c 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1b17d 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1b17e 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1b17f 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1b180 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1b181 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1b182 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1b183 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1b184 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1b185 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1b186 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1b187 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b188 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b189 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b18a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b18b 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
1b18c 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1b18d 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20  inline asm code 
1b18e 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22  for retrieving "
1b18f 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65  high-performance
1b190 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f  ".** counters fo
1b191 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73  r x86 class CPUs
1b192 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74  ..**.** $Id: hwt
1b193 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38  ime.h,v 1.3 2008
1b194 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20  /08/01 14:33:15 
1b195 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23  shane Exp $.*/.#
1b196 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48  ifndef _HWTIME_H
1b197 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d  _.#define _HWTIM
1b198 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  E_H_../*.** The 
1b199 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
1b19a 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20  e only works on 
1b19b 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f  pentium-class (o
1b19c 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73  r newer) process
1b19d 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20  ors..** It uses 
1b19e 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65  the RDTSC opcode
1b19f 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63   to read the cyc
1b1a0 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f  le count value o
1b1a1 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f  ut of the.** pro
1b1a2 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72  cessor and retur
1b1a3 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20  ns that value.  
1b1a4 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
1b1a5 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a   for high-res.**
1b1a6 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23   profiling..*/.#
1b1a7 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
1b1a8 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  UC__) || defined
1b1a9 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c  (_MSC_VER)) && \
1b1aa 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28  .      (defined(
1b1ab 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64  i386) || defined
1b1ac 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65  (__i386__) || de
1b1ad 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a  fined(_M_IX86)).
1b1ae 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f  .  #if defined(_
1b1af 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e  _GNUC__)..  __in
1b1b0 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
1b1b1 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
1b1b2 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75  me(void){.     u
1b1b3 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20  nsigned int lo, 
1b1b4 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f  hi;.     __asm__
1b1b5 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
1b1b6 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c  rdtsc" : "=a" (l
1b1b7 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a  o), "=d" (hi));.
1b1b8 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c       return (sql
1b1b9 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c  ite_uint64)hi <<
1b1ba 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20   32 | lo;.  }.. 
1b1bb 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f   #elif defined(_
1b1bc 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65  MSC_VER)..  __de
1b1bd 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f  clspec(naked) __
1b1be 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69  inline sqlite_ui
1b1bf 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c  nt64 __cdecl sql
1b1c0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
1b1c1 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20  {.     __asm {. 
1b1c2 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20         rdtsc.   
1b1c3 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b       ret       ;
1b1c4 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74   return value at
1b1c5 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a   EDX:EAX.     }.
1b1c6 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23    }..  #endif..#
1b1c7 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
1b1c8 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
1b1c9 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a  ed(__x86_64__)).
1b1ca 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71  .  __inline__ sq
1b1cb 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
1b1cc 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
1b1cd 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
1b1ce 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20  long val;.      
1b1cf 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69  __asm__ __volati
1b1d0 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20  le__ ("rdtsc" : 
1b1d1 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20  "=A" (val));.   
1b1d2 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20     return val;. 
1b1d3 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69   }. .#elif (defi
1b1d4 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
1b1d5 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f   defined(__ppc__
1b1d6 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
1b1d7 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
1b1d8 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
1b1d9 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
1b1da 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74  ed long long ret
1b1db 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67  val;.      unsig
1b1dc 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20  ned long junk;. 
1b1dd 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
1b1de 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a  olatile__ ("\n\.
1b1df 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20            1:    
1b1e0 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a    mftbu   %1\n\.
1b1e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b1e2 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c    mftb    %L0\n\
1b1e3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b1e4 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c     mftbu   %0\n\
1b1e5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1b1e6 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31     cmpw    %0,%1
1b1e7 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
1b1e8 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62        bne     1b
1b1e9 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
1b1ea 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76      : "=r" (retv
1b1eb 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29  al), "=r" (junk)
1b1ec 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1b1ed 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c  retval;.  }..#el
1b1ee 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65  se..  #error Nee
1b1ef 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  d implementation
1b1f0 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d   of sqlite3Hwtim
1b1f1 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61  e() for your pla
1b1f2 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a  tform...  /*.  *
1b1f3 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74  * To compile wit
1b1f4 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e  hout implementin
1b1f5 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  g sqlite3Hwtime(
1b1f6 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66  ) for your platf
1b1f7 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61  orm,.  ** you ca
1b1f8 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f  n remove the abo
1b1f9 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73  ve #error and us
1b1fa 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  e the following.
1b1fb 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69    ** stub functi
1b1fc 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f  on.  You will lo
1b1fd 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72  se timing suppor
1b1fe 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20  t for many.  ** 
1b1ff 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67  of the debugging
1b200 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69   and testing uti
1b201 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73  lities, but it s
1b202 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65  hould at.  ** le
1b203 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20  ast compile and 
1b204 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45  run..  */.SQLITE
1b205 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74  _PRIVATE   sqlit
1b206 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
1b207 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65  Hwtime(void){ re
1b208 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69  turn ((sqlite_ui
1b209 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64  nt64)0); }..#end
1b20a 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  if..#endif /* !d
1b20b 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48  efined(_HWTIME_H
1b20c 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _) */../********
1b20d 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77  ****** End of hw
1b20e 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  time.h *********
1b20f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b211 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1b212 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
1b213 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
1b214 6f 66 66 20 69 6e 20 76 64 62 65 2e 63 20 2a 2a  off in vdbe.c **
1b215 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b216 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 0a 0a  *****/..#endif..
1b217 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f  /*.** The CHECK_
1b218 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 6d 61  FOR_INTERRUPT ma
1b219 63 72 6f 20 64 65 66 69 6e 65 64 20 68 65 72 65  cro defined here
1b21a 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 20 69 66   looks to see if
1b21b 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
1b21c 69 6e 74 65 72 72 75 70 74 28 29 20 72 6f 75 74  interrupt() rout
1b21d 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c  ine has been cal
1b21e 6c 65 64 2e 20 20 49 66 20 69 74 20 68 61 73 20  led.  If it has 
1b21f 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70 72  been, then.** pr
1b220 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68 65 20  ocessing of the 
1b221 56 44 42 45 20 70 72 6f 67 72 61 6d 20 69 73 20  VDBE program is 
1b222 69 6e 74 65 72 72 75 70 74 65 64 2e 0a 2a 2a 0a  interrupted..**.
1b223 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 61 64  ** This macro ad
1b224 64 65 64 20 74 6f 20 65 76 65 72 79 20 69 6e 73  ded to every ins
1b225 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 64 6f  truction that do
1b226 65 73 20 61 20 6a 75 6d 70 20 69 6e 20 6f 72 64  es a jump in ord
1b227 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65  er to.** impleme
1b228 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54 68 69 73  nt a loop.  This
1b229 20 74 65 73 74 20 75 73 65 64 20 74 6f 20 62 65   test used to be
1b22a 20 6f 6e 20 65 76 65 72 79 20 73 69 6e 67 6c 65   on every single
1b22b 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 0a 2a 2a   instruction,.**
1b22c 20 62 75 74 20 74 68 61 74 20 6d 65 61 6e 74 20   but that meant 
1b22d 77 65 20 6d 6f 72 65 20 74 65 73 74 69 6e 67 20  we more testing 
1b22e 74 68 61 74 20 77 65 20 6e 65 65 64 65 64 2e 20  that we needed. 
1b22f 20 42 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e 67   By only testing
1b230 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20   the.** flag on 
1b231 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e  jump instruction
1b232 73 2c 20 77 65 20 67 65 74 20 61 20 28 73 6d 61  s, we get a (sma
1b233 6c 6c 29 20 73 70 65 65 64 20 69 6d 70 72 6f 76  ll) speed improv
1b234 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ement..*/.#defin
1b235 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45  e CHECK_FOR_INTE
1b236 52 52 55 50 54 20 5c 0a 20 20 20 69 66 28 20 64  RRUPT \.   if( d
1b237 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
1b238 74 65 64 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  ted ) goto abort
1b239 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70  _due_to_interrup
1b23a 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  t;..#ifdef SQLIT
1b23b 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69  E_DEBUG.static i
1b23c 6e 74 20 66 69 6c 65 45 78 69 73 74 73 28 73 71  nt fileExists(sq
1b23d 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
1b23e 20 63 68 61 72 20 2a 7a 46 69 6c 65 29 7b 0a 20   char *zFile){. 
1b23f 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20   int res = 0;.  
1b240 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1b241 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  OK;.#ifdef SQLIT
1b242 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 66 20 77  E_TEST.  /* If w
1b243 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20  e are currently 
1b244 74 65 73 74 69 6e 67 20 49 4f 20 65 72 72 6f 72  testing IO error
1b245 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63  s, then do not c
1b246 61 6c 6c 20 4f 73 41 63 63 65 73 73 28 29 20 74  all OsAccess() t
1b247 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f 72 20  o.  ** test for 
1b248 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20  the presence of 
1b249 7a 46 69 6c 65 2e 20 54 68 69 73 20 69 73 20 62  zFile. This is b
1b24a 65 63 61 75 73 65 20 61 6e 79 20 49 4f 20 65 72  ecause any IO er
1b24b 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a 20 6f 63  ror that.  ** oc
1b24c 63 75 72 73 20 68 65 72 65 20 77 69 6c 6c 20 6e  curs here will n
1b24d 6f 74 20 62 65 20 72 65 70 6f 72 74 65 64 2c 20  ot be reported, 
1b24e 63 61 75 73 69 6e 67 20 74 68 65 20 74 65 73 74  causing the test
1b24f 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20   to fail..  */. 
1b250 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69   extern int sqli
1b251 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
1b252 64 69 6e 67 3b 0a 20 20 69 66 28 20 73 71 6c 69  ding;.  if( sqli
1b253 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
1b254 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 69 66  ding<=0 ).#endif
1b255 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1b256 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56  3OsAccess(db->pV
1b257 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51 4c 49 54  fs, zFile, SQLIT
1b258 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c  E_ACCESS_EXISTS,
1b259 20 26 72 65 73 29 3b 0a 20 20 72 65 74 75 72 6e   &res);.  return
1b25a 20 28 72 65 73 20 26 26 20 72 63 3d 3d 53 51 4c   (res && rc==SQL
1b25b 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 69  ITE_OK);.}.#endi
1b25c 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  f..#ifndef NDEBU
1b25d 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  G./*.** This fun
1b25e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61  ction is only ca
1b25f 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
1b260 20 61 6e 20 61 73 73 65 72 74 28 29 20 65 78 70   an assert() exp
1b261 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a 20 63  ression. It.** c
1b262 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 73  hecks that the s
1b263 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73 61 63 74  qlite3.nTransact
1b264 69 6f 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20  ion variable is 
1b265 63 6f 72 72 65 63 74 6c 79 20 73 65 74 20 74 6f  correctly set to
1b266 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
1b267 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f  f non-transactio
1b268 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 63 75 72  n savepoints cur
1b269 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 0a 2a  rently in the .*
1b26a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74  * linked list st
1b26b 61 72 74 69 6e 67 20 61 74 20 73 71 6c 69 74 65  arting at sqlite
1b26c 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a  3.pSavepoint..**
1b26d 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a   .** Usage:.**.*
1b26e 2a 20 20 20 20 20 61 73 73 65 72 74 28 20 63 68  *     assert( ch
1b26f 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e  eckSavepointCoun
1b270 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 61 74  t(db) );.*/.stat
1b271 69 63 20 69 6e 74 20 63 68 65 63 6b 53 61 76 65  ic int checkSave
1b272 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c 69 74  pointCount(sqlit
1b273 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 6e  e3 *db){.  int n
1b274 20 3d 20 30 3b 0a 20 20 53 61 76 65 70 6f 69 6e   = 0;.  Savepoin
1b275 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 64 62  t *p;.  for(p=db
1b276 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 70 3b  ->pSavepoint; p;
1b277 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20 6e 2b 2b   p=p->pNext) n++
1b278 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 28  ;.  assert( n==(
1b279 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b  db->nSavepoint +
1b27a 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69   db->isTransacti
1b27b 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b 0a  onSavepoint) );.
1b27c 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
1b27d 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  ndif../*.** Exec
1b27e 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20 61  ute as much of a
1b27f 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 73   VDBE program as
1b280 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65 74   we can then ret
1b281 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74  urn..**.** sqlit
1b282 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28  e3VdbeMakeReady(
1b283 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  ) must be called
1b284 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75   before this rou
1b285 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  tine in order to
1b286 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70 72  .** close the pr
1b287 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66 69 6e  ogram with a fin
1b288 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20 74  al OP_Halt and t
1b289 6f 20 73 65 74 20 75 70 20 74 68 65 20 63 61 6c  o set up the cal
1b28a 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74 68  lbacks.** and th
1b28b 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
1b28c 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57  pointer..**.** W
1b28d 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f 72  henever a row or
1b28e 20 72 65 73 75 6c 74 20 64 61 74 61 20 69 73 20   result data is 
1b28f 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20  available, this 
1b290 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69 74  routine will eit
1b291 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  her.** invoke th
1b292 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61 63  e result callbac
1b293 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20 6f  k (if there is o
1b294 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77 69  ne) or return wi
1b295 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57  th.** SQLITE_ROW
1b296 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61 74  ..**.** If an at
1b297 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f  tempt is made to
1b298 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 64   open a locked d
1b299 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 68  atabase, then th
1b29a 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 69  is routine.** wi
1b29b 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b 65  ll either invoke
1b29c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61   the busy callba
1b29d 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20  ck (if there is 
1b29e 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c 0a  one) or it will.
1b29f 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ** return SQLITE
1b2a0 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  _BUSY..**.** If 
1b2a1 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
1b2a2 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1b2a3 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  e is written to 
1b2a4 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a  memory obtained.
1b2a5 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ** from sqlite3_
1b2a6 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d 3e  malloc() and p->
1b2a7 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65 20  zErrMsg is made 
1b2a8 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74  to point to that
1b2a9 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20   memory..** The 
1b2aa 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 74  error code is st
1b2ab 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61 6e  ored in p->rc an
1b2ac 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  d this routine r
1b2ad 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 52  eturns SQLITE_ER
1b2ae 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ROR..**.** If th
1b2af 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72 20  e callback ever 
1b2b0 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
1b2b1 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72  , then the progr
1b2b2 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d 65  am exits.** imme
1b2b3 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65 20  diately.  There 
1b2b4 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f 72  will be no error
1b2b5 20 6d 65 73 73 61 67 65 20 62 75 74 20 74 68 65   message but the
1b2b6 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73 0a   p->rc field is.
1b2b7 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  ** set to SQLITE
1b2b8 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73 20  _ABORT and this 
1b2b9 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74  routine will ret
1b2ba 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1b2bb 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79  ..**.** A memory
1b2bc 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
1b2bd 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20 74  r causes p->rc t
1b2be 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49  o be set to SQLI
1b2bf 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68 69  TE_NOMEM and thi
1b2c0 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20  s.** routine to 
1b2c1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1b2c2 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72  ROR..**.** Other
1b2c3 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72 65   fatal errors re
1b2c4 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1b2c5 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74  R..**.** After t
1b2c6 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20  his routine has 
1b2c7 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 65  finished, sqlite
1b2c8 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 20  3VdbeFinalize() 
1b2c9 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73 65  should be.** use
1b2ca 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68  d to clean up th
1b2cb 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 73 20  e mess that was 
1b2cc 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f 0a  left behind..*/.
1b2cd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1b2ce 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  nt sqlite3VdbeEx
1b2cf 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20  ec(.  Vdbe *p   
1b2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b2d1 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a   /* The VDBE */.
1b2d2 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20  ){.  int pc;    
1b2d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b2d4 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63  /* The program c
1b2d5 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a  ounter */.  Op *
1b2d6 70 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  pOp;            
1b2d7 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
1b2d8 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20  t operation */. 
1b2d9 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1b2da 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 56  _OK;        /* V
1b2db 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a  alue to return *
1b2dc 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
1b2dd 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f  = p->db;       /
1b2de 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 2a  * The database *
1b2df 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20  /.  u8 encoding 
1b2e0 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f  = ENC(db);     /
1b2e1 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 65  * The database e
1b2e2 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65 6d  ncoding */.  Mem
1b2e3 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20 20 20 20   *pIn1 = 0;     
1b2e4 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 69          /* 1st i
1b2e5 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  nput operand */.
1b2e6 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 30 3b    Mem *pIn2 = 0;
1b2e7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b2e8 32 6e 64 20 69 6e 70 75 74 20 6f 70 65 72 61 6e  2nd input operan
1b2e9 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 33  d */.  Mem *pIn3
1b2ea 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1b2eb 20 20 2f 2a 20 33 72 64 20 69 6e 70 75 74 20 6f    /* 3rd input o
1b2ec 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20  perand */.  Mem 
1b2ed 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 20  *pOut = 0;      
1b2ee 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74         /* Output
1b2ef 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 75 38   operand */.  u8
1b2f0 20 6f 70 50 72 6f 70 65 72 74 79 3b 0a 20 20 69   opProperty;.  i
1b2f1 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 30 3b  nt iCompare = 0;
1b2f2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
1b2f3 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f 43  ult of last OP_C
1b2f4 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 69 6f 6e  ompare operation
1b2f5 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d   */.  int *aPerm
1b2f6 75 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ute = 0;        
1b2f7 20 2f 2a 20 50 65 72 6d 75 74 61 74 69 6f 6e 20   /* Permutation 
1b2f8 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 4f  of columns for O
1b2f9 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69 66  P_Compare */.#if
1b2fa 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45  def VDBE_PROFILE
1b2fb 0a 20 20 75 36 34 20 73 74 61 72 74 3b 20 20 20  .  u64 start;   
1b2fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b2fd 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e 74   CPU clock count
1b2fe 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63   at start of opc
1b2ff 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 69  ode */.  int ori
1b300 67 50 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  gPc;            
1b301 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63      /* Program c
1b302 6f 75 6e 74 65 72 20 61 74 20 73 74 61 72 74 20  ounter at start 
1b303 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 6e  of opcode */.#en
1b304 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
1b305 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53  TE_OMIT_PROGRESS
1b306 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20  _CALLBACK.  int 
1b307 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30  nProgressOps = 0
1b308 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65  ;      /* Opcode
1b309 73 20 65 78 65 63 75 74 65 64 20 73 69 6e 63 65  s executed since
1b30a 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
1b30b 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20  ck. */.#endif.. 
1b30c 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 70   /* Temporary sp
1b30d 61 63 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74  ace into which t
1b30e 6f 20 75 6e 70 61 63 6b 20 61 20 72 65 63 6f 72  o unpack a recor
1b30f 64 2e 20 2a 2f 0a 20 20 63 68 61 72 20 61 54 65  d. */.  char aTe
1b310 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 7a  mpRec[ROUND8(siz
1b311 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f  eof(UnpackedReco
1b312 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65  rd)) + sizeof(Me
1b313 6d 29 2a 33 20 2b 20 37 5d 3b 0a 0a 20 20 61 73  m)*3 + 7];..  as
1b314 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
1b315 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29  VDBE_MAGIC_RUN )
1b316 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74  ;  /* sqlite3_st
1b317 65 70 28 29 20 76 65 72 69 66 69 65 73 20 74 68  ep() verifies th
1b318 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  is */.  assert( 
1b319 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
1b31a 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a  E_MAGIC_BUSY );.
1b31b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74    sqlite3VdbeMut
1b31c 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b  exArrayEnter(p);
1b31d 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51  .  if( p->rc==SQ
1b31e 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
1b31f 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e    /* This happen
1b320 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  s if a malloc() 
1b321 69 6e 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f  inside a call to
1b322 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1b323 74 65 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a  text() or.    **
1b324 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1b325 74 65 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e  text16() failed.
1b326 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f    */.    goto no
1b327 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65  _mem;.  }.  asse
1b328 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  rt( p->rc==SQLIT
1b329 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53  E_OK || p->rc==S
1b32a 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20  QLITE_BUSY );.  
1b32b 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  p->rc = SQLITE_O
1b32c 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  K;.  assert( p->
1b32d 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20  explain==0 );.  
1b32e 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20  p->pResultSet = 
1b32f 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e  0;.  db->busyHan
1b330 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a  dler.nBusy = 0;.
1b331 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
1b332 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33  RRUPT;.  sqlite3
1b333 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70  VdbeIOTraceSql(p
1b334 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
1b335 5f 44 45 42 55 47 0a 20 20 73 71 6c 69 74 65 33  _DEBUG.  sqlite3
1b336 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
1b337 63 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 63  c();.  if( p->pc
1b338 3d 3d 30 20 0a 20 20 20 26 26 20 28 28 70 2d 3e  ==0 .   && ((p->
1b339 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
1b33a 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 20  TE_VdbeListing) 
1b33b 7c 7c 20 66 69 6c 65 45 78 69 73 74 73 28 64 62  || fileExists(db
1b33c 2c 20 22 76 64 62 65 5f 65 78 70 6c 61 69 6e 22  , "vdbe_explain"
1b33d 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  )).  ){.    int 
1b33e 69 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 56  i;.    printf("V
1b33f 44 42 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 74  DBE Program List
1b340 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 71  ing:\n");.    sq
1b341 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71  lite3VdbePrintSq
1b342 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  l(p);.    for(i=
1b343 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b  0; i<p->nOp; i++
1b344 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b345 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f  VdbePrintOp(stdo
1b346 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69  ut, i, &p->aOp[i
1b347 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ]);.    }.  }.  
1b348 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 28 64  if( fileExists(d
1b349 62 2c 20 22 76 64 62 65 5f 74 72 61 63 65 22 29  b, "vdbe_trace")
1b34a 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65   ){.    p->trace
1b34b 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20   = stdout;.  }. 
1b34c 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
1b34d 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69  nMalloc();.#endi
1b34e 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63  f.  for(pc=p->pc
1b34f 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  ; rc==SQLITE_OK;
1b350 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65   pc++){.    asse
1b351 72 74 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c  rt( pc>=0 && pc<
1b352 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66  p->nOp );.    if
1b353 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
1b354 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  ed ) goto no_mem
1b355 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ;.#ifdef VDBE_PR
1b356 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63  OFILE.    origPc
1b357 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74   = pc;.    start
1b358 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65   = sqlite3Hwtime
1b359 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70  ();.#endif.    p
1b35a 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63 5d  Op = &p->aOp[pc]
1b35b 3b 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61  ;..    /* Only a
1b35c 6c 6c 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20  llow tracing if 
1b35d 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20  SQLITE_DEBUG is 
1b35e 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a  defined..    */.
1b35f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1b360 42 55 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74  BUG.    if( p->t
1b361 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66  race ){.      if
1b362 28 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ( pc==0 ){.     
1b363 20 20 20 70 72 69 6e 74 66 28 22 56 44 42 45 20     printf("VDBE 
1b364 45 78 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a  Execution Trace:
1b365 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71  \n");.        sq
1b366 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71  lite3VdbePrintSq
1b367 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  l(p);.      }.  
1b368 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
1b369 72 69 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c  rintOp(p->trace,
1b36a 20 70 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d   pc, pOp);.    }
1b36b 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63  .    if( p->trac
1b36c 65 3d 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b  e==0 && pc==0 ){
1b36d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65  .      sqlite3Be
1b36e 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
1b36f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c  );.      if( fil
1b370 65 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62  eExists(db, "vdb
1b371 65 5f 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a  e_sqltrace") ){.
1b372 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1b373 64 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a  dbePrintSql(p);.
1b374 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
1b375 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
1b376 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65  lloc();.    }.#e
1b377 6e 64 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20  ndif.      ..   
1b378 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   /* Check to see
1b379 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73   if we need to s
1b37a 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72  imulate an inter
1b37b 72 75 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79  rupt.  This only
1b37c 20 68 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20   happens.    ** 
1b37d 69 66 20 77 65 20 68 61 76 65 20 61 20 73 70 65  if we have a spe
1b37e 63 69 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e  cial test build.
1b37f 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  .    */.#ifdef S
1b380 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69  QLITE_TEST.    i
1b381 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  f( sqlite3_inter
1b382 72 75 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a  rupt_count>0 ){.
1b383 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
1b384 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b  terrupt_count--;
1b385 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1b386 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75  e3_interrupt_cou
1b387 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  nt==0 ){.       
1b388 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1b389 70 74 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a  pt(db);.      }.
1b38a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69      }.#endif..#i
1b38b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1b38c 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
1b38d 41 43 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20  ACK.    /* Call 
1b38e 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
1b38f 6c 62 61 63 6b 20 69 66 20 69 74 20 69 73 20 63  lback if it is c
1b390 6f 6e 66 69 67 75 72 65 64 20 61 6e 64 20 74 68  onfigured and th
1b391 65 20 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65  e required numbe
1b392 72 0a 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45  r.    ** of VDBE
1b393 20 6f 70 73 20 68 61 76 65 20 62 65 65 6e 20 65   ops have been e
1b394 78 65 63 75 74 65 64 20 28 65 69 74 68 65 72 20  xecuted (either 
1b395 73 69 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63  since this invoc
1b396 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20  ation of.    ** 
1b397 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
1b398 29 20 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20  ) or since last 
1b399 74 69 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73  time the progres
1b39a 73 20 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63  s callback was c
1b39b 61 6c 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49  alled)..    ** I
1b39c 66 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63  f the progress c
1b39d 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
1b39e 6e 6f 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74  non-zero, exit t
1b39f 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
1b3a0 6e 65 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61  ne with.    ** a
1b3a1 20 72 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c   return code SQL
1b3a2 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a  ITE_ABORT..    *
1b3a3 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50  /.    if( db->xP
1b3a4 72 6f 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20  rogress ){.     
1b3a5 20 69 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65   if( db->nProgre
1b3a6 73 73 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73  ssOps==nProgress
1b3a7 4f 70 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69  Ops ){.        i
1b3a8 6e 74 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20  nt prc;.        
1b3a9 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1b3aa 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20  yOff(db) ) goto 
1b3ab 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1b3ac 75 73 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63  use;.        prc
1b3ad 20 3d 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28   =db->xProgress(
1b3ae 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67  db->pProgressArg
1b3af 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  );.        if( s
1b3b0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1b3b1 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1b3b2 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1b3b3 20 20 20 20 20 20 20 69 66 28 20 70 72 63 21 3d         if( prc!=
1b3b4 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
1b3b5 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52  c = SQLITE_INTER
1b3b6 52 55 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20  RUPT;.          
1b3b7 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f  goto vdbe_error_
1b3b8 68 61 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  halt;.        }.
1b3b9 20 20 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73          nProgres
1b3ba 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  sOps = 0;.      
1b3bb 7d 0a 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73  }.      nProgres
1b3bc 73 4f 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65  sOps++;.    }.#e
1b3bd 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 6f 20  ndif..    /* Do 
1b3be 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20 70 72 6f  common setup pro
1b3bf 63 65 73 73 69 6e 67 20 66 6f 72 20 61 6e 79 20  cessing for any 
1b3c0 6f 70 63 6f 64 65 20 74 68 61 74 20 69 73 20 6d  opcode that is m
1b3c1 61 72 6b 65 64 0a 20 20 20 20 2a 2a 20 77 69 74  arked.    ** wit
1b3c2 68 20 74 68 65 20 22 6f 75 74 32 2d 70 72 65 72  h the "out2-prer
1b3c3 65 6c 65 61 73 65 22 20 74 61 67 2e 20 20 53 75  elease" tag.  Su
1b3c4 63 68 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20  ch opcodes have 
1b3c5 61 20 73 69 6e 67 6c 65 0a 20 20 20 20 2a 2a 20  a single.    ** 
1b3c6 6f 75 74 70 75 74 20 77 68 69 63 68 20 69 73 20  output which is 
1b3c7 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
1b3c8 20 50 32 20 70 61 72 61 6d 65 74 65 72 2e 20 20   P2 parameter.  
1b3c9 54 68 65 20 50 32 20 72 65 67 69 73 74 65 72 0a  The P2 register.
1b3ca 20 20 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61      ** is initia
1b3cb 6c 69 7a 65 64 20 74 6f 20 61 20 4e 55 4c 4c 2e  lized to a NULL.
1b3cc 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6f 70 50 72  .    */.    opPr
1b3cd 6f 70 65 72 74 79 20 3d 20 6f 70 63 6f 64 65 50  operty = opcodeP
1b3ce 72 6f 70 65 72 74 79 5b 70 4f 70 2d 3e 6f 70 63  roperty[pOp->opc
1b3cf 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28 20 28 6f  ode];.    if( (o
1b3d0 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
1b3d1 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53  G_OUT2_PRERELEAS
1b3d2 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  E)!=0 ){.      a
1b3d3 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
1b3d4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1b3d5 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d  ( pOp->p2<=p->nM
1b3d6 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 4f 75 74  em );.      pOut
1b3d7 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1b3d8 3e 70 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69  >p2];.      sqli
1b3d9 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1b3da 65 45 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b  eExternal(pOut);
1b3db 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  .      pOut->fla
1b3dc 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1b3dd 20 20 20 7d 65 6c 73 65 0a 20 0a 20 20 20 20 2f     }else. .    /
1b3de 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75  * Do common setu
1b3df 70 20 66 6f 72 20 6f 70 63 6f 64 65 73 20 6d 61  p for opcodes ma
1b3e0 72 6b 65 64 20 77 69 74 68 20 6f 6e 65 20 6f 66  rked with one of
1b3e1 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
1b3e2 20 20 20 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 6f     ** combinatio
1b3e3 6e 73 20 6f 66 20 70 72 6f 70 65 72 74 69 65 73  ns of properties
1b3e4 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1b3e5 20 20 20 20 20 20 20 20 20 20 69 6e 31 0a 20 20            in1.  
1b3e6 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69    **           i
1b3e7 6e 31 20 69 6e 32 0a 20 20 20 20 2a 2a 20 20 20  n1 in2.    **   
1b3e8 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32 20          in1 in2 
1b3e9 6f 75 74 33 0a 20 20 20 20 2a 2a 20 20 20 20 20  out3.    **     
1b3ea 20 20 20 20 20 20 69 6e 31 20 69 6e 33 0a 20 20        in1 in3.  
1b3eb 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 56 61 72 69    **.    ** Vari
1b3ec 61 62 6c 65 73 20 70 49 6e 31 2c 20 70 49 6e 32  ables pIn1, pIn2
1b3ed 2c 20 61 6e 64 20 70 49 6e 33 20 61 72 65 20 6d  , and pIn3 are m
1b3ee 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ade to point to 
1b3ef 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 20  appropriate.    
1b3f0 2a 2a 20 72 65 67 69 73 74 65 72 73 20 66 6f 72  ** registers for
1b3f1 20 69 6e 70 75 74 73 2e 20 20 56 61 72 69 61 62   inputs.  Variab
1b3f2 6c 65 20 70 4f 75 74 20 70 6f 69 6e 74 73 20 74  le pOut points t
1b3f3 6f 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67  o the output reg
1b3f4 69 73 74 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  ister..    */.  
1b3f5 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74    if( (opPropert
1b3f6 79 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 3d  y & OPFLG_IN1)!=
1b3f7 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
1b3f8 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a  t( pOp->p1>0 );.
1b3f9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1b3fa 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p1<=p->nMem )
1b3fb 3b 0a 20 20 20 20 20 20 70 49 6e 31 20 3d 20 26  ;.      pIn1 = &
1b3fc 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  p->aMem[pOp->p1]
1b3fd 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52  ;.      REGISTER
1b3fe 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20  _TRACE(pOp->p1, 
1b3ff 70 49 6e 31 29 3b 0a 20 20 20 20 20 20 69 66 28  pIn1);.      if(
1b400 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f   (opProperty & O
1b401 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a  PFLG_IN2)!=0 ){.
1b402 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1b403 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20  pOp->p2>0 );.   
1b404 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1b405 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p2<=p->nMem );
1b406 0a 20 20 20 20 20 20 20 20 70 49 6e 32 20 3d 20  .        pIn2 = 
1b407 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1b408 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53  ];.        REGIS
1b409 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1b40a 32 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 20 20  2, pIn2);.      
1b40b 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74    if( (opPropert
1b40c 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21  y & OPFLG_OUT3)!
1b40d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1b40e 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1b40f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  0 );.          a
1b410 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d  ssert( pOp->p3<=
1b411 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
1b412 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e       pOut = &p->
1b413 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1b414 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1b415 65 6c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70  else if( (opProp
1b416 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33  erty & OPFLG_IN3
1b417 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
1b418 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1b419 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  0 );.        ass
1b41a 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  ert( pOp->p3<=p-
1b41b 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20  >nMem );.       
1b41c 20 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn3 = &p->aMem
1b41d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20  [pOp->p3];.     
1b41e 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
1b41f 45 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29  E(pOp->p3, pIn3)
1b420 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
1b421 6c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65  lse if( (opPrope
1b422 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29  rty & OPFLG_IN2)
1b423 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  !=0 ){.      ass
1b424 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29  ert( pOp->p2>0 )
1b425 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1b426 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p2<=p->nMem
1b427 20 29 3b 0a 20 20 20 20 20 20 70 49 6e 32 20 3d   );.      pIn2 =
1b428 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1b429 32 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54  2];.      REGIST
1b42a 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32  ER_TRACE(pOp->p2
1b42b 2c 20 70 49 6e 32 29 3b 0a 20 20 20 20 7d 65 6c  , pIn2);.    }el
1b42c 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72  se if( (opProper
1b42d 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21  ty & OPFLG_IN3)!
1b42e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
1b42f 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b  rt( pOp->p3>0 );
1b430 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1b431 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
1b432 29 3b 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20  );.      pIn3 = 
1b433 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1b434 5d 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45  ];.      REGISTE
1b435 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
1b436 20 70 49 6e 33 29 3b 0a 20 20 20 20 7d 0a 0a 20   pIn3);.    }.. 
1b437 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e     switch( pOp->
1b438 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a  opcode ){../****
1b439 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b43a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b43b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b43c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b43d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61  *********.** Wha
1b43e 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d  t follows is a m
1b43f 61 73 73 69 76 65 20 73 77 69 74 63 68 20 73 74  assive switch st
1b440 61 74 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61  atement where ea
1b441 63 68 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e  ch case implemen
1b442 74 73 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65  ts a.** separate
1b443 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20   instruction in 
1b444 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
1b445 69 6e 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c  ine.  If we foll
1b446 6f 77 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20  ow the usual.** 
1b447 69 6e 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76  indentation conv
1b448 65 6e 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61  entions, each ca
1b449 73 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64  se should be ind
1b44a 65 6e 74 65 64 20 62 79 20 36 20 73 70 61 63 65  ented by 6 space
1b44b 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20  s.  But.** that 
1b44c 69 73 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74  is a lot of wast
1b44d 65 64 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20  ed space on the 
1b44e 6c 65 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f  left margin.  So
1b44f 20 74 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e   the code within
1b450 0a 2a 2a 20 74 68 65 20 73 77 69 74 63 68 20 73  .** the switch s
1b451 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72  tatement will br
1b452 65 61 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74  eak with convent
1b453 69 6f 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68  ion and be flush
1b454 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a  -left. Another.*
1b455 2a 20 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73  * big comment (s
1b456 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f  imilar to this o
1b457 6e 65 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68  ne) will mark th
1b458 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63  e point in the c
1b459 6f 64 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20  ode where.** we 
1b45a 74 72 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20  transition back 
1b45b 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74  to normal indent
1b45c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
1b45d 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65   formatting of e
1b45e 61 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f  ach case is impo
1b45f 72 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65  rtant.  The make
1b460 66 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a  file for SQLite.
1b461 2a 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f  ** generates two
1b462 20 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65   C files "opcode
1b463 73 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65  s.h" and "opcode
1b464 73 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67  s.c" by scanning
1b465 20 74 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f   this.** file lo
1b466 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20  oking for lines 
1b467 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20  that begin with 
1b468 22 63 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65  "case OP_".  The
1b469 20 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73   opcodes.h files
1b46a 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c  .** will be fill
1b46b 65 64 20 77 69 74 68 20 23 64 65 66 69 6e 65 73  ed with #defines
1b46c 20 74 68 61 74 20 67 69 76 65 20 75 6e 69 71 75   that give uniqu
1b46d 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  e integer values
1b46e 20 74 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f   to each.** opco
1b46f 64 65 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64  de and the opcod
1b470 65 73 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c  es.c file is fil
1b471 6c 65 64 20 77 69 74 68 20 61 6e 20 61 72 72 61  led with an arra
1b472 79 20 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65  y of strings whe
1b473 72 65 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e  re.** each strin
1b474 67 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69  g is the symboli
1b475 63 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63  c name for the c
1b476 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63  orresponding opc
1b477 6f 64 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ode.  If the.** 
1b478 63 61 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69  case statement i
1b479 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20  s followed by a 
1b47a 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66  comment of the f
1b47b 6f 72 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20  orm "/# same as 
1b47c 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20  ... #/".** that 
1b47d 63 6f 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20  comment is used 
1b47e 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
1b47f 20 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75   particular valu
1b480 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e  e of the opcode.
1b481 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79  .**.** Other key
1b482 77 6f 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d  words in the com
1b483 6d 65 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ment that follow
1b484 73 20 65 61 63 68 20 63 61 73 65 20 61 72 65 20  s each case are 
1b485 75 73 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74  used to.** const
1b486 72 75 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49  ruct the OPFLG_I
1b487 4e 49 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65  NITIALIZER value
1b488 20 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65   that initialize
1b489 73 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79  s opcodeProperty
1b48a 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20  []..** Keywords 
1b48b 69 6e 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e  include: in1, in
1b48c 32 2c 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65  2, in3, out2_pre
1b48d 72 65 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f  release, out2, o
1b48e 75 74 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65  ut3.  See.** the
1b48f 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73   mkopcodeh.awk s
1b490 63 72 69 70 74 20 66 6f 72 20 61 64 64 69 74 69  cript for additi
1b491 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1b492 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74  ..**.** Document
1b493 61 74 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45  ation about VDBE
1b494 20 6f 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65   opcodes is gene
1b495 72 61 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e  rated by scannin
1b496 67 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66  g this file.** f
1b497 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74  or lines of that
1b498 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65   contain "Opcode
1b499 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61  :".  That line a
1b49a 6e 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  nd all subsequen
1b49b 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e  t.** comment lin
1b49c 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74  es are used in t
1b49d 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66  he generation of
1b49e 20 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c   the opcode.html
1b49f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a   documentation.*
1b4a0 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55  * file..**.** SU
1b4a1 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  MMARY:.**.**    
1b4a2 20 46 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69   Formatting is i
1b4a3 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69  mportant to scri
1b4a4 70 74 73 20 74 68 61 74 20 73 63 61 6e 20 74 68  pts that scan th
1b4a5 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20  is file..**     
1b4a6 44 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66  Do not deviate f
1b4a7 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69  rom the formatti
1b4a8 6e 67 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74  ng style current
1b4a9 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a  ly in use..**.**
1b4aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
1b4af 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a   Opcode:  Goto *
1b4b0 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1b4b1 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c  An unconditional
1b4b2 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73   jump to address
1b4b3 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74   P2..** The next
1b4b4 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65   instruction exe
1b4b5 63 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a  cuted will be .*
1b4b6 2a 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64  * the one at ind
1b4b7 65 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62  ex P2 from the b
1b4b8 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74  eginning of.** t
1b4b9 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63  he program..*/.c
1b4ba 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20  ase OP_Goto: {  
1b4bb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
1b4bc 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f  mp */.  CHECK_FO
1b4bd 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70  R_INTERRUPT;.  p
1b4be 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1b4bf 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1b4c0 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50  Opcode:  Gosub P
1b4c1 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1b4c2 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65   Write the curre
1b4c3 6e 74 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20  nt address onto 
1b4c4 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61  register P1.** a
1b4c5 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  nd then jump to 
1b4c6 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63  address P2..*/.c
1b4c7 61 73 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20  ase OP_Gosub: { 
1b4c8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
1b4c9 6d 70 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  mp */.  assert( 
1b4ca 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61  pOp->p1>0 );.  a
1b4cb 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d  ssert( pOp->p1<=
1b4cc 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e  p->nMem );.  pIn
1b4cd 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  1 = &p->aMem[pOp
1b4ce 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1b4cf 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1b4d0 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20  MEM_Dyn)==0 );. 
1b4d1 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d   pIn1->flags = M
1b4d2 45 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e  EM_Int;.  pIn1->
1b4d3 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49  u.i = pc;.  REGI
1b4d4 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1b4d5 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20  p1, pIn1);.  pc 
1b4d6 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1b4d7 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1b4d8 63 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31  code:  Return P1
1b4d9 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a   * * * *.**.** J
1b4da 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ump to the next 
1b4db 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65  instruction afte
1b4dc 72 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e  r the address in
1b4dd 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f   register P1..*/
1b4de 0a 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a  .case OP_Return:
1b4df 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1b4e0 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  in1 */.  assert(
1b4e1 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1b4e2 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 63 20 3d  EM_Int );.  pc =
1b4e3 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b   (int)pIn1->u.i;
1b4e4 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1b4e5 4f 70 63 6f 64 65 3a 20 20 59 69 65 6c 64 20 50  Opcode:  Yield P
1b4e6 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1b4e7 53 77 61 70 20 74 68 65 20 70 72 6f 67 72 61 6d  Swap the program
1b4e8 20 63 6f 75 6e 74 65 72 20 77 69 74 68 20 74 68   counter with th
1b4e9 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1b4ea 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20  ter P1..*/.case 
1b4eb 4f 50 5f 59 69 65 6c 64 3a 20 7b 20 20 20 20 20  OP_Yield: {     
1b4ec 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f         /* in1 */
1b4ed 0a 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a 20  .  int pcDest;. 
1b4ee 20 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e   assert( (pIn1->
1b4ef 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29  flags & MEM_Dyn)
1b4f0 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66  ==0 );.  pIn1->f
1b4f1 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1b4f2 20 20 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29    pcDest = (int)
1b4f3 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e  pIn1->u.i;.  pIn
1b4f4 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52  1->u.i = pc;.  R
1b4f5 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1b4f6 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20  p->p1, pIn1);.  
1b4f7 70 63 20 3d 20 70 63 44 65 73 74 3b 0a 20 20 62  pc = pcDest;.  b
1b4f8 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1b4f9 64 65 3a 20 20 48 61 6c 74 49 66 4e 75 6c 6c 20  de:  HaltIfNull 
1b4fa 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1b4fb 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76  *.** Check the v
1b4fc 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1b4fd 20 50 33 2e 20 20 49 66 20 69 73 20 69 73 20 4e   P3.  If is is N
1b4fe 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74 20 75 73  ULL then Halt us
1b4ff 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ing.** parameter
1b500 20 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 34 20   P1, P2, and P4 
1b501 61 73 20 69 66 20 74 68 69 73 20 77 65 72 65 20  as if this were 
1b502 61 20 48 61 6c 74 20 69 6e 73 74 72 75 63 74 69  a Halt instructi
1b503 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 76  on.  If the.** v
1b504 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1b505 20 50 33 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   P3 is not NULL,
1b506 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
1b507 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
1b508 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 49 66  /.case OP_HaltIf
1b509 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 2f 2a 20  Null: {      /* 
1b50a 69 6e 33 20 2a 2f 0a 20 20 69 66 28 20 28 70 49  in3 */.  if( (pI
1b50b 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n3->flags & MEM_
1b50c 4e 75 6c 6c 29 3d 3d 30 20 29 20 62 72 65 61 6b  Null)==0 ) break
1b50d 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f  ;.  /* Fall thro
1b50e 75 67 68 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74  ugh into OP_Halt
1b50f 20 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65   */.}../* Opcode
1b510 3a 20 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20  :  Halt P1 P2 * 
1b511 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20  P4 *.**.** Exit 
1b512 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 41 6c  immediately.  Al
1b513 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2c 20  l open cursors, 
1b514 65 74 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a  etc are closed.*
1b515 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e  * automatically.
1b516 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65  .**.** P1 is the
1b517 20 72 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74   result code ret
1b518 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
1b519 5f 65 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33  _exec(), sqlite3
1b51a 5f 72 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20  _reset(),.** or 
1b51b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
1b51c 28 29 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61  ().  For a norma
1b51d 6c 20 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f  l halt, this sho
1b51e 75 6c 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b  uld be SQLITE_OK
1b51f 20 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72   (0)..** For err
1b520 6f 72 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73  ors, it can be s
1b521 6f 6d 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e  ome other value.
1b522 20 20 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20    If P1!=0 then 
1b523 50 32 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e  P2 will determin
1b524 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20  e.** whether or 
1b525 6e 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20  not to rollback 
1b526 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  the current tran
1b527 73 61 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74  saction.  Do not
1b528 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20   rollback.** if 
1b529 50 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20  P2==OE_Fail. Do 
1b52a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20  the rollback if 
1b52b 50 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e  P2==OE_Rollback.
1b52c 20 20 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72    If P2==OE_Abor
1b52d 74 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20  t,.** then back 
1b52e 6f 75 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20  out all changes 
1b52f 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72  that have occurr
1b530 65 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 65  ed during this e
1b531 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a  xecution of the.
1b532 2a 2a 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20  ** VDBE, but do 
1b533 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65  not rollback the
1b534 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a   transaction. .*
1b535 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f  *.** If P4 is no
1b536 74 20 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69  t null then it i
1b537 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  s an error messa
1b538 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  ge string..**.**
1b539 20 54 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70   There is an imp
1b53a 6c 69 65 64 20 22 48 61 6c 74 20 30 20 30 20 30  lied "Halt 0 0 0
1b53b 22 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e  " instruction in
1b53c 73 65 72 74 65 64 20 61 74 20 74 68 65 20 76 65  serted at the ve
1b53d 72 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65  ry end of.** eve
1b53e 72 79 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20  ry program.  So 
1b53f 61 20 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20  a jump past the 
1b540 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  last instruction
1b541 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a   of the program.
1b542 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ** is the same a
1b543 73 20 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74  s executing Halt
1b544 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c  ..*/.case OP_Hal
1b545 74 3a 20 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 70  t: {.  p->rc = p
1b546 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 70 63 20  Op->p1;.  p->pc 
1b547 3d 20 70 63 3b 0a 20 20 70 2d 3e 65 72 72 6f 72  = pc;.  p->error
1b548 41 63 74 69 6f 6e 20 3d 20 70 4f 70 2d 3e 70 32  Action = pOp->p2
1b549 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e  ;.  if( pOp->p4.
1b54a 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  z ){.    sqlite3
1b54b 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1b54c 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
1b54d 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 7d   pOp->p4.z);.  }
1b54e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
1b54f 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 61 73  dbeHalt(p);.  as
1b550 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1b551 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c  _BUSY || rc==SQL
1b552 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20  ITE_OK );.  if( 
1b553 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
1b554 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72  ){.    p->rc = r
1b555 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
1b556 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
1b557 20 3d 20 70 2d 3e 72 63 20 3f 20 53 51 4c 49 54   = p->rc ? SQLIT
1b558 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c 49 54 45  E_ERROR : SQLITE
1b559 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 67 6f 74  _DONE;.  }.  got
1b55a 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d  o vdbe_return;.}
1b55b 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74  ../* Opcode: Int
1b55c 65 67 65 72 20 50 31 20 50 32 20 2a 20 2a 20 2a  eger P1 P2 * * *
1b55d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32 2d 62 69  .**.** The 32-bi
1b55e 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  t integer value 
1b55f 50 31 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  P1 is written in
1b560 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1b561 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67  */.case OP_Integ
1b562 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a  er: {         /*
1b563 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1b564 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67   */.  pOut->flag
1b565 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70  s = MEM_Int;.  p
1b566 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e  Out->u.i = pOp->
1b567 70 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  p1;.  break;.}..
1b568 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34  /* Opcode: Int64
1b569 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a   * P2 * P4 *.**.
1b56a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1b56b 65 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 69  er to a 64-bit i
1b56c 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2a  nteger value..**
1b56d 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75   Write that valu
1b56e 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
1b56f 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  P2..*/.case OP_I
1b570 6e 74 36 34 3a 20 7b 20 20 20 20 20 20 20 20 20  nt64: {         
1b571 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1b572 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  ease */.  assert
1b573 28 20 70 4f 70 2d 3e 70 34 2e 70 49 36 34 21 3d  ( pOp->p4.pI64!=
1b574 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61  0 );.  pOut->fla
1b575 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1b576 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a 70 4f 70  pOut->u.i = *pOp
1b577 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20 62 72 65  ->p4.pI64;.  bre
1b578 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1b579 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a 20 50 34  : Real * P2 * P4
1b57a 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61   *.**.** P4 is a
1b57b 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34   pointer to a 64
1b57c 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  -bit floating po
1b57d 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72  int value..** Wr
1b57e 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69  ite that value i
1b57f 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
1b580 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c  .*/.case OP_Real
1b581 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1b582 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f  * same as TK_FLO
1b583 41 54 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c 65  AT, out2-prerele
1b584 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66  ase */.  pOut->f
1b585 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b  lags = MEM_Real;
1b586 0a 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69  .  assert( !sqli
1b587 74 65 33 49 73 4e 61 4e 28 2a 70 4f 70 2d 3e 70  te3IsNaN(*pOp->p
1b588 34 2e 70 52 65 61 6c 29 20 29 3b 0a 20 20 70 4f  4.pReal) );.  pO
1b589 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d 3e 70 34  ut->r = *pOp->p4
1b58a 2e 70 52 65 61 6c 3b 0a 20 20 62 72 65 61 6b 3b  .pReal;.  break;
1b58b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1b58c 74 72 69 6e 67 38 20 2a 20 50 32 20 2a 20 50 34  tring8 * P2 * P4
1b58d 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e   *.**.** P4 poin
1b58e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d  ts to a nul term
1b58f 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72  inated UTF-8 str
1b590 69 6e 67 2e 20 54 68 69 73 20 6f 70 63 6f 64 65  ing. This opcode
1b591 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20   is transformed 
1b592 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 53  .** into an OP_S
1b593 74 72 69 6e 67 20 62 65 66 6f 72 65 20 69 74 20  tring before it 
1b594 69 73 20 65 78 65 63 75 74 65 64 20 66 6f 72 20  is executed for 
1b595 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 2e 0a  the first time..
1b596 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e  */.case OP_Strin
1b597 67 38 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a  g8: {         /*
1b598 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49   same as TK_STRI
1b599 4e 47 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c 65  NG, out2-prerele
1b59a 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ase */.  assert(
1b59b 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b   pOp->p4.z!=0 );
1b59c 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d  .  pOp->opcode =
1b59d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a 20 20 70 4f   OP_String;.  pO
1b59e 70 2d 3e 70 31 20 3d 20 73 71 6c 69 74 65 33 53  p->p1 = sqlite3S
1b59f 74 72 6c 65 6e 33 30 28 70 4f 70 2d 3e 70 34 2e  trlen30(pOp->p4.
1b5a0 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  z);..#ifndef SQL
1b5a1 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20  ITE_OMIT_UTF16. 
1b5a2 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d 53   if( encoding!=S
1b5a3 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20  QLITE_UTF8 ){.  
1b5a4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b5a5 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70  SetStr(pOut, pOp
1b5a6 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49  ->p4.z, -1, SQLI
1b5a7 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
1b5a8 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69 66 28  STATIC);.    if(
1b5a9 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
1b5aa 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1b5ab 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63 6f  oding(pOut, enco
1b5ac 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 6e 6f 5f  ding) ) goto no_
1b5ad 6d 65 6d 3b 0a 20 20 20 20 69 66 28 20 53 51 4c  mem;.    if( SQL
1b5ae 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56  ITE_OK!=sqlite3V
1b5af 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
1b5b0 62 6c 65 28 70 4f 75 74 29 20 29 20 67 6f 74 6f  ble(pOut) ) goto
1b5b1 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 70 4f 75   no_mem;.    pOu
1b5b2 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  t->zMalloc = 0;.
1b5b3 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20      pOut->flags 
1b5b4 7c 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20  |= MEM_Static;. 
1b5b5 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26     pOut->flags &
1b5b6 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20  = ~MEM_Dyn;.    
1b5b7 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  if( pOp->p4type=
1b5b8 3d 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20  =P4_DYNAMIC ){. 
1b5b9 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1b5ba 65 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34 2e 7a  ee(db, pOp->p4.z
1b5bb 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 70  );.    }.    pOp
1b5bc 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59  ->p4type = P4_DY
1b5bd 4e 41 4d 49 43 3b 0a 20 20 20 20 70 4f 70 2d 3e  NAMIC;.    pOp->
1b5be 70 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e 7a 3b 0a  p4.z = pOut->z;.
1b5bf 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4f      pOp->p1 = pO
1b5c0 75 74 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70  ut->n;.    if( p
1b5c1 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d 69  Op->p1>db->aLimi
1b5c2 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1b5c3 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20  ENGTH] ){.      
1b5c4 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20  goto too_big;.  
1b5c5 20 20 7d 0a 20 20 20 20 55 50 44 41 54 45 5f 4d    }.    UPDATE_M
1b5c6 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74  AX_BLOBSIZE(pOut
1b5c7 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  );.    break;.  
1b5c8 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  }.#endif.  if( p
1b5c9 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d 69  Op->p1>db->aLimi
1b5ca 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1b5cb 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f  ENGTH] ){.    go
1b5cc 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1b5cd 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
1b5ce 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61  h to the next ca
1b5cf 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a 2f  se, OP_String */
1b5d0 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a  .}.  ./* Opcode:
1b5d1 20 53 74 72 69 6e 67 20 50 31 20 50 32 20 2a 20   String P1 P2 * 
1b5d2 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  P4 *.**.** The s
1b5d3 74 72 69 6e 67 20 76 61 6c 75 65 20 50 34 20 6f  tring value P4 o
1b5d4 66 20 6c 65 6e 67 74 68 20 50 31 20 28 62 79 74  f length P1 (byt
1b5d5 65 73 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e  es) is stored in
1b5d6 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f   register P2..*/
1b5d7 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 3a  .case OP_String:
1b5d8 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f   {          /* o
1b5d9 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1b5da 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
1b5db 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f  >p4.z!=0 );.  pO
1b5dc 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
1b5dd 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d  Str|MEM_Static|M
1b5de 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d  EM_Term;.  pOut-
1b5df 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  >z = pOp->p4.z;.
1b5e0 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d    pOut->n = pOp-
1b5e1 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63  >p1;.  pOut->enc
1b5e2 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55   = encoding;.  U
1b5e3 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1b5e4 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61  ZE(pOut);.  brea
1b5e5 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1b5e6 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 2a 20 2a   Null * P2 * * *
1b5e7 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 4e  .**.** Write a N
1b5e8 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ULL into registe
1b5e9 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P2..*/.case OP
1b5ea 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20  _Null: {        
1b5eb 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1b5ec 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72 65 61 6b  lease */.  break
1b5ed 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ;.}.../* Opcode:
1b5ee 20 42 6c 6f 62 20 50 31 20 50 32 20 2a 20 50 34   Blob P1 P2 * P4
1b5ef 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73  .**.** P4 points
1b5f0 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 64 61   to a blob of da
1b5f1 74 61 20 50 31 20 62 79 74 65 73 20 6c 6f 6e 67  ta P1 bytes long
1b5f2 2e 20 20 53 74 6f 72 65 20 74 68 69 73 0a 2a 2a  .  Store this.**
1b5f3 20 62 6c 6f 62 20 69 6e 20 72 65 67 69 73 74 65   blob in registe
1b5f4 72 20 50 32 2e 20 54 68 69 73 20 69 6e 73 74 72  r P2. This instr
1b5f5 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f  uction is not co
1b5f6 64 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20  ded directly.** 
1b5f7 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e  by the compiler.
1b5f8 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 63 6f   Instead, the co
1b5f9 6d 70 69 6c 65 72 20 6c 61 79 65 72 20 73 70 65  mpiler layer spe
1b5fa 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f  cifies.** an OP_
1b5fb 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20  HexBlob opcode, 
1b5fc 77 69 74 68 20 74 68 65 20 68 65 78 20 73 74 72  with the hex str
1b5fd 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
1b5fe 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f  on of.** the blo
1b5ff 62 20 61 73 20 50 34 2e 20 54 68 69 73 20 6f 70  b as P4. This op
1b600 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72  code is transfor
1b601 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f  med to an OP_Blo
1b602 62 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74  b.** the first t
1b603 69 6d 65 20 69 74 20 69 73 20 65 78 65 63 75 74  ime it is execut
1b604 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42  ed..*/.case OP_B
1b605 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20  lob: {          
1b606 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1b607 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73  erelease */.  as
1b608 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20 3c 3d  sert( pOp->p1 <=
1b609 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
1b60a 54 48 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  TH );.  sqlite3V
1b60b 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75  dbeMemSetStr(pOu
1b60c 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f  t, pOp->p4.z, pO
1b60d 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20  p->p1, 0, 0);.  
1b60e 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  pOut->enc = enco
1b60f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d  ding;.  UPDATE_M
1b610 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74  AX_BLOBSIZE(pOut
1b611 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1b612 2a 20 4f 70 63 6f 64 65 3a 20 56 61 72 69 61 62  * Opcode: Variab
1b613 6c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  le P1 P2 P3 P4 *
1b614 0a 2a 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20  .**.** Transfer 
1b615 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 62 6f  the values of bo
1b616 75 6e 64 20 70 61 72 61 6d 65 74 65 72 73 20 50  und parameters P
1b617 31 2e 2e 50 31 2b 50 33 2d 31 20 69 6e 74 6f 20  1..P1+P3-1 into 
1b618 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 50 32 2e  registers.** P2.
1b619 2e 50 32 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2a 20  .P2+P3-1..**.** 
1b61a 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  If the parameter
1b61b 20 69 73 20 6e 61 6d 65 64 2c 20 74 68 65 6e 20   is named, then 
1b61c 69 74 73 20 6e 61 6d 65 20 61 70 70 65 61 72 73  its name appears
1b61d 20 69 6e 20 50 34 20 61 6e 64 20 50 33 3d 3d 31   in P4 and P3==1
1b61e 2e 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75  ..** The P4 valu
1b61f 65 20 69 73 20 75 73 65 64 20 62 79 20 73 71 6c  e is used by sql
1b620 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
1b621 74 65 72 5f 6e 61 6d 65 28 29 2e 0a 2a 2f 0a 63  ter_name()..*/.c
1b622 61 73 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 3a  ase OP_Variable:
1b623 20 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 70 4f 70   {.  int j = pOp
1b624 2d 3e 70 31 20 2d 20 31 3b 0a 20 20 69 6e 74 20  ->p1 - 1;.  int 
1b625 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 4d  k = pOp->p2;.  M
1b626 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 6e 74 20  em *pVar;.  int 
1b627 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61  n = pOp->p3;.  a
1b628 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a  ssert( j>=0 && j
1b629 2b 6e 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20  +n<=p->nVar );. 
1b62a 20 61 73 73 65 72 74 28 20 6b 3e 3d 31 20 26 26   assert( k>=1 &&
1b62b 20 6b 2b 6e 2d 31 3c 3d 70 2d 3e 6e 4d 65 6d 20   k+n-1<=p->nMem 
1b62c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1b62d 2d 3e 70 34 2e 7a 3d 3d 30 20 7c 7c 20 70 4f 70  ->p4.z==0 || pOp
1b62e 2d 3e 70 33 3d 3d 31 20 29 3b 0a 0a 20 20 77 68  ->p3==1 );..  wh
1b62f 69 6c 65 28 20 6e 2d 2d 20 3e 20 30 20 29 7b 0a  ile( n-- > 0 ){.
1b630 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61      pVar = &p->a
1b631 56 61 72 5b 6a 2b 2b 5d 3b 0a 20 20 20 20 69 66  Var[j++];.    if
1b632 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
1b633 54 6f 6f 42 69 67 28 70 56 61 72 29 20 29 7b 0a  TooBig(pVar) ){.
1b634 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62        goto too_b
1b635 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f  ig;.    }.    pO
1b636 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 6b 2b  ut = &p->aMem[k+
1b637 2b 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  +];.    sqlite3V
1b638 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74  dbeMemReleaseExt
1b639 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20 20 20  ernal(pOut);.   
1b63a 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1b63b 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 73 71 6c  EM_Null;.    sql
1b63c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c  ite3VdbeMemShall
1b63d 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 70 56 61  owCopy(pOut, pVa
1b63e 72 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a  r, MEM_Static);.
1b63f 20 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42      UPDATE_MAX_B
1b640 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20  LOBSIZE(pOut);. 
1b641 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1b642 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 50  * Opcode: Move P
1b643 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1b644 2a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 65  * Move the value
1b645 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  s in register P1
1b646 2e 2e 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 69  ..P1+P3-1 over i
1b647 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73  nto.** registers
1b648 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 52   P2..P2+P3-1.  R
1b649 65 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b  egisters P1..P1+
1b64a 50 31 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 74  P1-1 are.** left
1b64b 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c 2e   holding a NULL.
1b64c 20 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72    It is an error
1b64d 20 66 6f 72 20 72 65 67 69 73 74 65 72 20 72 61   for register ra
1b64e 6e 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b 50  nges.** P1..P1+P
1b64f 33 2d 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b 50  3-1 and P2..P2+P
1b650 33 2d 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e 0a  3-1 to overlap..
1b651 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a  */.case OP_Move:
1b652 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c   {.  char *zMall
1b653 6f 63 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f  oc;.  int n = pO
1b654 70 2d 3e 70 33 3b 0a 20 20 69 6e 74 20 70 31 20  p->p3;.  int p1 
1b655 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74  = pOp->p1;.  int
1b656 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20   p2 = pOp->p2;. 
1b657 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20   assert( n>0 && 
1b658 70 31 3e 30 20 26 26 20 70 32 3e 30 20 29 3b 0a  p1>0 && p2>0 );.
1b659 20 20 61 73 73 65 72 74 28 20 70 31 2b 6e 3c 3d    assert( p1+n<=
1b65a 70 32 20 7c 7c 20 70 32 2b 6e 3c 3d 70 31 20 29  p2 || p2+n<=p1 )
1b65b 3b 0a 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e  ;..  pIn1 = &p->
1b65c 61 4d 65 6d 5b 70 31 5d 3b 0a 20 20 70 4f 75 74  aMem[p1];.  pOut
1b65d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 32 5d 3b   = &p->aMem[p2];
1b65e 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 7b  .  while( n-- ){
1b65f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 75  .    assert( pOu
1b660 74 3c 3d 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e  t<=&p->aMem[p->n
1b661 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 61 73 73 65  Mem] );.    asse
1b662 72 74 28 20 70 49 6e 31 3c 3d 26 70 2d 3e 61 4d  rt( pIn1<=&p->aM
1b663 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20  em[p->nMem] );. 
1b664 20 20 20 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75     zMalloc = pOu
1b665 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20  t->zMalloc;.    
1b666 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pOut->zMalloc = 
1b667 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  0;.    sqlite3Vd
1b668 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20  beMemMove(pOut, 
1b669 70 49 6e 31 29 3b 0a 20 20 20 20 70 49 6e 31 2d  pIn1);.    pIn1-
1b66a 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4d 61 6c 6c  >zMalloc = zMall
1b66b 6f 63 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52  oc;.    REGISTER
1b66c 5f 54 52 41 43 45 28 70 32 2b 2b 2c 20 70 4f 75  _TRACE(p2++, pOu
1b66d 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a  t);.    pIn1++;.
1b66e 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a      pOut++;.  }.
1b66f 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1b670 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50  pcode: Copy P1 P
1b671 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61  2 * * *.**.** Ma
1b672 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72 65 67  ke a copy of reg
1b673 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65  ister P1 into re
1b674 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a  gister P2..**.**
1b675 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1b676 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70 20 63  n makes a deep c
1b677 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65  opy of the value
1b678 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65 0a 2a  .  A duplicate.*
1b679 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61 6e 79  * is made of any
1b67a 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
1b67b 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 20 61  constant.  See a
1b67c 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f  lso OP_SCopy..*/
1b67d 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b  .case OP_Copy: {
1b67e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b67f 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  in1 */.  assert(
1b680 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20   pOp->p2>0 );.  
1b681 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c  assert( pOp->p2<
1b682 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f  =p->nMem );.  pO
1b683 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1b684 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74  p->p2];.  assert
1b685 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a  ( pOut!=pIn1 );.
1b686 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b687 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74  ShallowCopy(pOut
1b688 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65  , pIn1, MEM_Ephe
1b689 6d 29 3b 0a 20 20 44 65 65 70 68 65 6d 65 72 61  m);.  Deephemera
1b68a 6c 69 7a 65 28 70 4f 75 74 29 3b 0a 20 20 52 45  lize(pOut);.  RE
1b68b 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1b68c 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62  ->p2, pOut);.  b
1b68d 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1b68e 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50 32 20  de: SCopy P1 P2 
1b68f 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65  * * *.**.** Make
1b690 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20   a shallow copy 
1b691 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69  of register P1 i
1b692 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
1b693 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
1b694 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20  ruction makes a 
1b695 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20  shallow copy of 
1b696 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66 20 74  the value.  If t
1b697 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 61  he value.** is a
1b698 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 2c   string or blob,
1b699 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79 20 69   then the copy i
1b69a 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72  s only a pointer
1b69b 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69   to the.** origi
1b69c 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20 69 66  nal and hence if
1b69d 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 68   the original ch
1b69e 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20 74 68  anges so will th
1b69f 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72 73 65  e copy..** Worse
1b6a0 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61  , if the origina
1b6a1 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74 65 64  l is deallocated
1b6a2 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63 6f 6d  , the copy becom
1b6a3 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 20 54  es invalid..** T
1b6a4 68 75 73 20 74 68 65 20 70 72 6f 67 72 61 6d 20  hus the program 
1b6a5 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74  must guarantee t
1b6a6 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  hat the original
1b6a7 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e 67 65   will not change
1b6a8 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 6c  .** during the l
1b6a9 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 63  ifetime of the c
1b6aa 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43 6f 70  opy.  Use OP_Cop
1b6ab 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 6d 70  y to make a comp
1b6ac 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a 2a 2f  lete.** copy..*/
1b6ad 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79 3a 20  .case OP_SCopy: 
1b6ae 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1b6af 69 6e 31 20 2a 2f 0a 20 20 52 45 47 49 53 54 45  in1 */.  REGISTE
1b6b0 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
1b6b1 20 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74   pIn1);.  assert
1b6b2 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20  ( pOp->p2>0 );. 
1b6b3 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1b6b4 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
1b6b5 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
1b6b6 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72  Op->p2];.  asser
1b6b7 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b  t( pOut!=pIn1 );
1b6b8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1b6b9 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75  mShallowCopy(pOu
1b6ba 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68  t, pIn1, MEM_Eph
1b6bb 65 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  em);.  REGISTER_
1b6bc 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
1b6bd 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1b6be 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73  ../* Opcode: Res
1b6bf 75 6c 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a  ultRow P1 P2 * *
1b6c0 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67   *.**.** The reg
1b6c1 69 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67  isters P1 throug
1b6c2 68 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69  h P1+P2-1 contai
1b6c3 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  n a single row o
1b6c4 66 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68  f.** results. Th
1b6c5 69 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73  is opcode causes
1b6c6 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65   the sqlite3_ste
1b6c7 70 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d  p() call to term
1b6c8 69 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e  inate.** with an
1b6c9 20 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75   SQLITE_ROW retu
1b6ca 72 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73  rn code and it s
1b6cb 65 74 73 20 75 70 20 74 68 65 20 73 71 6c 69 74  ets up the sqlit
1b6cc 65 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63  e3_stmt.** struc
1b6cd 74 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20  ture to provide 
1b6ce 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f  access to the to
1b6cf 70 20 50 31 20 76 61 6c 75 65 73 20 61 73 20 74  p P1 values as t
1b6d0 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77  he result.** row
1b6d1 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73  ..*/.case OP_Res
1b6d2 75 6c 74 52 6f 77 3a 20 7b 0a 20 20 4d 65 6d 20  ultRow: {.  Mem 
1b6d3 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a  *pMem;.  int i;.
1b6d4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
1b6d5 73 43 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70 32  sColumn==pOp->p2
1b6d6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1b6d7 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73  p->p1>0 );.  ass
1b6d8 65 72 74 28 20 70 4f 70 2d 3e 70 31 2b 70 4f 70  ert( pOp->p1+pOp
1b6d9 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20  ->p2<=p->nMem+1 
1b6da 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
1b6db 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
1b6dc 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20   flag is set in 
1b6dd 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20 6d 61  sqlite3.flags ma
1b6de 73 6b 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 44  sk, then .  ** D
1b6df 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  ML statements in
1b6e0 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65  voke this opcode
1b6e1 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 6e   to return the n
1b6e2 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 0a 20  umber of rows . 
1b6e3 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f 20   ** modified to 
1b6e4 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 69  the user. This i
1b6e5 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74  s the only way t
1b6e6 68 61 74 20 61 20 56 4d 20 74 68 61 74 0a 20 20  hat a VM that.  
1b6e7 2a 2a 20 6f 70 65 6e 73 20 61 20 73 74 61 74 65  ** opens a state
1b6e8 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1b6e9 20 6d 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 73   may invoke this
1b6ea 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20   opcode..  **.  
1b6eb 2a 2a 20 49 6e 20 63 61 73 65 20 74 68 69 73 20  ** In case this 
1b6ec 69 73 20 73 75 63 68 20 61 20 73 74 61 74 65 6d  is such a statem
1b6ed 65 6e 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73  ent, close any s
1b6ee 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1b6ef 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 64  tion.  ** opened
1b6f0 20 62 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f   by this VM befo
1b6f1 72 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e  re returning con
1b6f2 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72  trol to the user
1b6f3 2e 20 54 68 69 73 20 69 73 20 74 6f 0a 20 20 2a  . This is to.  *
1b6f4 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73 74  * ensure that st
1b6f5 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74  atement-transact
1b6f6 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20  ions are always 
1b6f7 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72  nested, not over
1b6f8 6c 61 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 66  lapping..  ** If
1b6f9 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65 6d   the open statem
1b6fa 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent-transaction 
1b6fb 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 65  is not closed he
1b6fc 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65  re, then the use
1b6fd 72 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 20  r.  ** may step 
1b6fe 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61 74 20  another VM that 
1b6ff 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 74  opens its own st
1b700 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1b701 69 6f 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d  ion. This.  ** m
1b702 61 79 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 6c  ay lead to overl
1b703 61 70 70 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  apping statement
1b704 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20   transactions.. 
1b705 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
1b706 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 7c  >iStatement==0 |
1b707 7c 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49  | db->flags&SQLI
1b708 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 3b 0a  TE_CountRows );.
1b709 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
1b70a 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64  =(rc = sqlite3Vd
1b70b 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74  beCloseStatement
1b70c 28 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45  (p, SAVEPOINT_RE
1b70d 4c 45 41 53 45 29 29 20 29 7b 0a 20 20 20 20 62  LEASE)) ){.    b
1b70e 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
1b70f 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 65  Invalidate all e
1b710 70 68 65 6d 65 72 61 6c 20 63 75 72 73 6f 72 20  phemeral cursor 
1b711 72 6f 77 20 63 61 63 68 65 73 20 2a 2f 0a 20 20  row caches */.  
1b712 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 28 70  p->cacheCtr = (p
1b713 2d 3e 63 61 63 68 65 43 74 72 20 2b 20 32 29 7c  ->cacheCtr + 2)|
1b714 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  1;..  /* Make su
1b715 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  re the results o
1b716 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  f the current ro
1b717 77 20 61 72 65 20 5c 30 30 30 20 74 65 72 6d 69  w are \000 termi
1b718 6e 61 74 65 64 0a 20 20 2a 2a 20 61 6e 64 20 68  nated.  ** and h
1b719 61 76 65 20 61 6e 20 61 73 73 69 67 6e 65 64 20  ave an assigned 
1b71a 74 79 70 65 2e 20 20 54 68 65 20 72 65 73 75 6c  type.  The resul
1b71b 74 73 20 61 72 65 20 64 65 2d 65 70 68 65 6d 65  ts are de-epheme
1b71c 72 61 6c 69 7a 65 64 20 61 73 0a 20 20 2a 2a 20  ralized as.  ** 
1b71d 61 73 20 73 69 64 65 20 65 66 66 65 63 74 2e 0a  as side effect..
1b71e 20 20 2a 2f 0a 20 20 70 4d 65 6d 20 3d 20 70 2d    */.  pMem = p-
1b71f 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70  >pResultSet = &p
1b720 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b  ->aMem[pOp->p1];
1b721 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f  .  for(i=0; i<pO
1b722 70 2d 3e 70 32 3b 20 69 2b 2b 29 7b 0a 20 20 20  p->p2; i++){.   
1b723 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
1b724 75 6c 54 65 72 6d 69 6e 61 74 65 28 26 70 4d 65  ulTerminate(&pMe
1b725 6d 5b 69 5d 29 3b 0a 20 20 20 20 73 74 6f 72 65  m[i]);.    store
1b726 54 79 70 65 49 6e 66 6f 28 26 70 4d 65 6d 5b 69  TypeInfo(&pMem[i
1b727 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  ], encoding);.  
1b728 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1b729 28 70 4f 70 2d 3e 70 31 2b 69 2c 20 26 70 4d 65  (pOp->p1+i, &pMe
1b72a 6d 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28  m[i]);.  }.  if(
1b72b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1b72c 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  d ) goto no_mem;
1b72d 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 53 51  ..  /* Return SQ
1b72e 4c 49 54 45 5f 52 4f 57 0a 20 20 2a 2f 0a 20 20  LITE_ROW.  */.  
1b72f 70 2d 3e 70 63 20 3d 20 70 63 20 2b 20 31 3b 0a  p->pc = pc + 1;.
1b730 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f    rc = SQLITE_RO
1b731 57 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72  W;.  goto vdbe_r
1b732 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  eturn;.}../* Opc
1b733 6f 64 65 3a 20 43 6f 6e 63 61 74 20 50 31 20 50  ode: Concat P1 P
1b734 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  2 P3 * *.**.** A
1b735 64 64 20 74 68 65 20 74 65 78 74 20 69 6e 20 72  dd the text in r
1b736 65 67 69 73 74 65 72 20 50 31 20 6f 6e 74 6f 20  egister P1 onto 
1b737 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74  the end of the t
1b738 65 78 74 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74  ext in.** regist
1b739 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20  er P2 and store 
1b73a 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
1b73b 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66  gister P3..** If
1b73c 20 65 69 74 68 65 72 20 74 68 65 20 50 31 20 6f   either the P1 o
1b73d 72 20 50 32 20 74 65 78 74 20 61 72 65 20 4e 55  r P2 text are NU
1b73e 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 4e 55  LL then store NU
1b73f 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a 0a 2a 2a 20  LL in P3..**.** 
1b740 20 20 50 33 20 3d 20 50 32 20 7c 7c 20 50 31 0a    P3 = P2 || P1.
1b741 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65  **.** It is ille
1b742 67 61 6c 20 66 6f 72 20 50 31 20 61 6e 64 20 50  gal for P1 and P
1b743 33 20 74 6f 20 62 65 20 74 68 65 20 73 61 6d 65  3 to be the same
1b744 20 72 65 67 69 73 74 65 72 2e 20 53 6f 6d 65 74   register. Somet
1b745 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 50 33 20 69  imes,.** if P3 i
1b746 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73  s the same regis
1b747 74 65 72 20 61 73 20 50 32 2c 20 74 68 65 20 69  ter as P2, the i
1b748 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
1b749 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 76 6f 69   able.** to avoi
1b74a 64 20 61 20 6d 65 6d 63 70 79 28 29 2e 0a 2a 2f  d a memcpy()..*/
1b74b 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 63 61 74 3a  .case OP_Concat:
1b74c 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1b74d 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41  same as TK_CONCA
1b74e 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74  T, in1, in2, out
1b74f 33 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65  3 */.  i64 nByte
1b750 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e  ;..  assert( pIn
1b751 31 21 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28  1!=pOut );.  if(
1b752 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20   (pIn1->flags | 
1b753 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d  pIn2->flags) & M
1b754 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73  EM_Null ){.    s
1b755 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1b756 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20  Null(pOut);.    
1b757 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 45 78 70  break;.  }.  Exp
1b758 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20  andBlob(pIn1);. 
1b759 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 31 2c   Stringify(pIn1,
1b75a 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 45 78   encoding);.  Ex
1b75b 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a  pandBlob(pIn2);.
1b75c 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32    Stringify(pIn2
1b75d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 6e  , encoding);.  n
1b75e 42 79 74 65 20 3d 20 70 49 6e 31 2d 3e 6e 20 2b  Byte = pIn1->n +
1b75f 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 69 66 28 20   pIn2->n;.  if( 
1b760 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74  nByte>db->aLimit
1b761 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
1b762 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74  NGTH] ){.    got
1b763 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20  o too_big;.  }. 
1b764 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1b765 70 4f 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b 0a  pOut, MEM_Str);.
1b766 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1b767 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28  eMemGrow(pOut, (
1b768 69 6e 74 29 6e 42 79 74 65 2b 32 2c 20 70 4f 75  int)nByte+2, pOu
1b769 74 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20  t==pIn2) ){.    
1b76a 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
1b76b 0a 20 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e  .  if( pOut!=pIn
1b76c 32 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  2 ){.    memcpy(
1b76d 70 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a  pOut->z, pIn2->z
1b76e 2c 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a  , pIn2->n);.  }.
1b76f 20 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e    memcpy(&pOut->
1b770 7a 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31  z[pIn2->n], pIn1
1b771 2d 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20  ->z, pIn1->n);. 
1b772 20 70 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65 5d 20   pOut->z[nByte] 
1b773 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 6e  = 0;.  pOut->z[n
1b774 42 79 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 20 70  Byte+1] = 0;.  p
1b775 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Out->flags |= ME
1b776 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e  M_Term;.  pOut->
1b777 6e 20 3d 20 28 69 6e 74 29 6e 42 79 74 65 3b 0a  n = (int)nByte;.
1b778 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e    pOut->enc = en
1b779 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45  coding;.  UPDATE
1b77a 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f  _MAX_BLOBSIZE(pO
1b77b 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
1b77c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20  ./* Opcode: Add 
1b77d 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1b77e 2a 2a 20 41 64 64 20 74 68 65 20 76 61 6c 75 65  ** Add the value
1b77f 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1b780 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  to the value in 
1b781 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61  register P2.** a
1b782 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1b783 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1b784 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1b785 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
1b786 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1b787 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
1b788 3a 20 4d 75 6c 74 69 70 6c 79 20 50 31 20 50 32  : Multiply P1 P2
1b789 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a   P3 * *.**.**.**
1b78a 20 4d 75 6c 74 69 70 6c 79 20 74 68 65 20 76 61   Multiply the va
1b78b 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1b78c 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65 20  P1 by the value 
1b78d 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a  in register P2.*
1b78e 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  * and store the 
1b78f 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1b790 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1b791 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1b792 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1b793 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1b794 6f 64 65 3a 20 53 75 62 74 72 61 63 74 20 50 31  ode: Subtract P1
1b795 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1b796 20 53 75 62 74 72 61 63 74 20 74 68 65 20 76 61   Subtract the va
1b797 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1b798 50 31 20 66 72 6f 6d 20 74 68 65 20 76 61 6c 75  P1 from the valu
1b799 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
1b79a 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68  .** and store th
1b79b 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
1b79c 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
1b79d 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
1b79e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
1b79f 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
1b7a0 70 63 6f 64 65 3a 20 44 69 76 69 64 65 20 50 31  pcode: Divide P1
1b7a1 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1b7a2 20 44 69 76 69 64 65 20 74 68 65 20 76 61 6c 75   Divide the valu
1b7a3 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1b7a4 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e   by the value in
1b7a5 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20   register P2.** 
1b7a6 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
1b7a7 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72  sult in register
1b7a8 20 50 33 2e 20 20 49 66 20 74 68 65 20 76 61 6c   P3.  If the val
1b7a9 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1b7aa 32 0a 2a 2a 20 69 73 20 7a 65 72 6f 2c 20 74 68  2.** is zero, th
1b7ab 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
1b7ac 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 74   NULL..** If eit
1b7ad 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1b7ae 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1b7af 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1b7b0 6f 64 65 3a 20 52 65 6d 61 69 6e 64 65 72 20 50  ode: Remainder P
1b7b1 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1b7b2 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 72 65  * Compute the re
1b7b3 6d 61 69 6e 64 65 72 20 61 66 74 65 72 20 69 6e  mainder after in
1b7b4 74 65 67 65 72 20 64 69 76 69 73 69 6f 6e 20 6f  teger division o
1b7b5 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 0a 2a  f the value in.*
1b7b6 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79  * register P1 by
1b7b7 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1b7b8 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74  gister P2 and st
1b7b9 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1b7ba 6e 20 50 33 2e 20 0a 2a 2a 20 49 66 20 74 68 65  n P3. .** If the
1b7bb 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1b7bc 65 72 20 50 32 20 69 73 20 7a 65 72 6f 20 74 68  er P2 is zero th
1b7bd 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1b7be 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f  ..** If either o
1b7bf 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20  perand is NULL, 
1b7c0 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1b7c1 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41  LL..*/.case OP_A
1b7c2 64 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  dd:             
1b7c3 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1b7c4 20 54 4b 5f 50 4c 55 53 2c 20 69 6e 31 2c 20 69   TK_PLUS, in1, i
1b7c5 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1b7c6 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20   OP_Subtract:   
1b7c7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1b7c8 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 2c 20  me as TK_MINUS, 
1b7c9 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1b7ca 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70  /.case OP_Multip
1b7cb 6c 79 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  ly:             
1b7cc 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53   /* same as TK_S
1b7cd 54 41 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  TAR, in1, in2, o
1b7ce 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 44  ut3 */.case OP_D
1b7cf 69 76 69 64 65 3a 20 20 20 20 20 20 20 20 20 20  ivide:          
1b7d0 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1b7d1 20 54 4b 5f 53 4c 41 53 48 2c 20 69 6e 31 2c 20   TK_SLASH, in1, 
1b7d2 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
1b7d3 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 3a 20  e OP_Remainder: 
1b7d4 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  {           /* s
1b7d5 61 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c 20 69  ame as TK_REM, i
1b7d6 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1b7d7 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20  .  int flags;.  
1b7d8 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69  applyNumericAffi
1b7d9 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20 20 61 70  nity(pIn1);.  ap
1b7da 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  plyNumericAffini
1b7db 74 79 28 70 49 6e 32 29 3b 0a 20 20 66 6c 61 67  ty(pIn2);.  flag
1b7dc 73 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  s = pIn1->flags 
1b7dd 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20  | pIn2->flags;. 
1b7de 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 4d 45   if( (flags & ME
1b7df 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 67 6f 74  M_Null)!=0 ) got
1b7e0 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73  o arithmetic_res
1b7e1 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69  ult_is_null;.  i
1b7e2 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
1b7e3 26 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20  & pIn2->flags & 
1b7e4 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e  MEM_Int)==MEM_In
1b7e5 74 20 29 7b 0a 20 20 20 20 69 36 34 20 61 2c 20  t ){.    i64 a, 
1b7e6 62 3b 0a 20 20 20 20 61 20 3d 20 70 49 6e 31 2d  b;.    a = pIn1-
1b7e7 3e 75 2e 69 3b 0a 20 20 20 20 62 20 3d 20 70 49  >u.i;.    b = pI
1b7e8 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69  n2->u.i;.    swi
1b7e9 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  tch( pOp->opcode
1b7ea 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f   ){.      case O
1b7eb 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 62  P_Add:         b
1b7ec 20 2b 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65   += a;       bre
1b7ed 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1b7ee 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20 62  P_Subtract:    b
1b7ef 20 2d 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65   -= a;       bre
1b7f0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1b7f1 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 62  P_Multiply:    b
1b7f2 20 2a 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65   *= a;       bre
1b7f3 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ak;.      case O
1b7f4 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20  P_Divide: {.    
1b7f5 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 20 67      if( a==0 ) g
1b7f6 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72  oto arithmetic_r
1b7f7 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20  esult_is_null;. 
1b7f8 20 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 69         /* Dividi
1b7f9 6e 67 20 74 68 65 20 6c 61 72 67 65 73 74 20 70  ng the largest p
1b7fa 6f 73 73 69 62 6c 65 20 6e 65 67 61 74 69 76 65  ossible negative
1b7fb 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20   64-bit integer 
1b7fc 28 31 3c 3c 36 33 29 20 62 79 20 0a 20 20 20 20  (1<<63) by .    
1b7fd 20 20 20 20 2a 2a 20 2d 31 20 72 65 74 75 72 6e      ** -1 return
1b7fe 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 6f  s an integer too
1b7ff 20 6c 61 72 67 65 20 74 6f 20 73 74 6f 72 65 20   large to store 
1b800 69 6e 20 61 20 36 34 2d 62 69 74 20 64 61 74 61  in a 64-bit data
1b801 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 20 20 20 20  -type. On.      
1b802 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68 69 74    ** some archit
1b803 65 63 74 75 72 65 73 2c 20 74 68 65 20 76 61 6c  ectures, the val
1b804 75 65 20 6f 76 65 72 66 6c 6f 77 73 20 74 6f 20  ue overflows to 
1b805 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 6f 74 68 65  (1<<63). On othe
1b806 72 73 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  rs,.        ** a
1b807 20 53 49 47 46 50 45 20 69 73 20 69 73 73 75 65   SIGFPE is issue
1b808 64 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  d. The following
1b809 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 72 6d 61   statement norma
1b80a 6c 69 7a 65 73 20 74 68 69 73 0a 20 20 20 20 20  lizes this.     
1b80b 20 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 20 73     ** behavior s
1b80c 6f 20 74 68 61 74 20 61 6c 6c 20 61 72 63 68 69  o that all archi
1b80d 74 65 63 74 75 72 65 73 20 62 65 68 61 76 65 20  tectures behave 
1b80e 61 73 20 69 66 20 69 6e 74 65 67 65 72 20 0a 20  as if integer . 
1b80f 20 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c         ** overfl
1b810 6f 77 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 20  ow occurred..   
1b811 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1b812 69 66 28 20 61 3d 3d 2d 31 20 26 26 20 62 3d 3d  if( a==-1 && b==
1b813 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29  SMALLEST_INT64 )
1b814 20 61 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20   a = 1;.        
1b815 62 20 2f 3d 20 61 3b 0a 20 20 20 20 20 20 20 20  b /= a;.        
1b816 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1b817 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
1b818 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d 30          if( a==0
1b819 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74   ) goto arithmet
1b81a 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
1b81b 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  l;.        if( a
1b81c 3d 3d 2d 31 20 29 20 61 20 3d 20 31 3b 0a 20 20  ==-1 ) a = 1;.  
1b81d 20 20 20 20 20 20 62 20 25 3d 20 61 3b 0a 20 20        b %= a;.  
1b81e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1b81f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
1b820 4f 75 74 2d 3e 75 2e 69 20 3d 20 62 3b 0a 20 20  Out->u.i = b;.  
1b821 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1b822 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1b823 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f  .  }else{.    do
1b824 75 62 6c 65 20 61 2c 20 62 3b 0a 20 20 20 20 61  uble a, b;.    a
1b825 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1b826 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20  alValue(pIn1);. 
1b827 20 20 20 62 20 3d 20 73 71 6c 69 74 65 33 56 64     b = sqlite3Vd
1b828 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 32  beRealValue(pIn2
1b829 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  );.    switch( p
1b82a 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20  Op->opcode ){.  
1b82b 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a      case OP_Add:
1b82c 20 20 20 20 20 20 20 20 20 62 20 2b 3d 20 61 3b           b += a;
1b82d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1b82e 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74      case OP_Subt
1b82f 72 61 63 74 3a 20 20 20 20 62 20 2d 3d 20 61 3b  ract:    b -= a;
1b830 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1b831 20 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74      case OP_Mult
1b832 69 70 6c 79 3a 20 20 20 20 62 20 2a 3d 20 61 3b  iply:    b *= a;
1b833 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1b834 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 69      case OP_Divi
1b835 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a  de: {.        /*
1b836 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61   (double)0 In ca
1b837 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
1b838 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
1b839 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ... */.        i
1b83a 66 28 20 61 3d 3d 28 64 6f 75 62 6c 65 29 30 20  f( a==(double)0 
1b83b 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69  ) goto arithmeti
1b83c 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c  c_result_is_null
1b83d 3b 0a 20 20 20 20 20 20 20 20 62 20 2f 3d 20 61  ;.        b /= a
1b83e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1b83f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
1b840 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
1b841 20 20 69 36 34 20 69 61 20 3d 20 28 69 36 34 29    i64 ia = (i64)
1b842 61 3b 0a 20 20 20 20 20 20 20 20 69 36 34 20 69  a;.        i64 i
1b843 62 20 3d 20 28 69 36 34 29 62 3b 0a 20 20 20 20  b = (i64)b;.    
1b844 20 20 20 20 69 66 28 20 69 61 3d 3d 30 20 29 20      if( ia==0 ) 
1b845 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
1b846 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a  result_is_null;.
1b847 20 20 20 20 20 20 20 20 69 66 28 20 69 61 3d 3d          if( ia==
1b848 2d 31 20 29 20 69 61 20 3d 20 31 3b 0a 20 20 20  -1 ) ia = 1;.   
1b849 20 20 20 20 20 62 20 3d 20 28 64 6f 75 62 6c 65       b = (double
1b84a 29 28 69 62 20 25 20 69 61 29 3b 0a 20 20 20 20  )(ib % ia);.    
1b84b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1b84c 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
1b84d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 62 29   sqlite3IsNaN(b)
1b84e 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61   ){.      goto a
1b84f 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
1b850 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a  _is_null;.    }.
1b851 20 20 20 20 70 4f 75 74 2d 3e 72 20 3d 20 62 3b      pOut->r = b;
1b852 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
1b853 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 65  lag(pOut, MEM_Re
1b854 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 66 6c  al);.    if( (fl
1b855 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d  ags & MEM_Real)=
1b856 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1b857 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66  te3VdbeIntegerAf
1b858 66 69 6e 69 74 79 28 70 4f 75 74 29 3b 0a 20 20  finity(pOut);.  
1b859 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
1b85a 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73  ..arithmetic_res
1b85b 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a 20 20 73  ult_is_null:.  s
1b85c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1b85d 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 62 72  Null(pOut);.  br
1b85e 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1b85f 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a 20 50  e: CollSeq * * P
1b860 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  4.**.** P4 is a 
1b861 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c  pointer to a Col
1b862 6c 53 65 71 20 73 74 72 75 63 74 2e 20 49 66 20  lSeq struct. If 
1b863 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  the next call to
1b864 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
1b865 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 61 74 65  .** or aggregate
1b866 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 47 65   calls sqlite3Ge
1b867 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 2c 20  tFuncCollSeq(), 
1b868 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  this collation s
1b869 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a 2a 20  equence will.** 
1b86a 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  be returned. Thi
1b86b 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  s is used by the
1b86c 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28 29 2c   built-in min(),
1b86d 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c 6c 69   max() and nulli
1b86e 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  f().** functions
1b86f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ..**.** The inte
1b870 72 66 61 63 65 20 75 73 65 64 20 62 79 20 74 68  rface used by th
1b871 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1b872 20 6f 66 20 74 68 65 20 61 66 6f 72 65 6d 65 6e   of the aforemen
1b873 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  tioned functions
1b874 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 76 65 20  .** to retrieve 
1b875 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
1b876 71 75 65 6e 63 65 20 73 65 74 20 62 79 20 74 68  quence set by th
1b877 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6e 6f 74  is opcode is not
1b878 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 70 75   available.** pu
1b879 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 74 6f 20  blicly, only to 
1b87a 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 64  user functions d
1b87b 65 66 69 6e 65 64 20 69 6e 20 66 75 6e 63 2e 63  efined in func.c
1b87c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6c  ..*/.case OP_Col
1b87d 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73 65 72 74  lSeq: {.  assert
1b87e 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1b87f 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 62  4_COLLSEQ );.  b
1b880 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1b881 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 50 31 20  de: Function P1 
1b882 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1b883 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73 65 72 20  * Invoke a user 
1b884 66 75 6e 63 74 69 6f 6e 20 28 50 34 20 69 73 20  function (P4 is 
1b885 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 46  a pointer to a F
1b886 75 6e 63 74 69 6f 6e 20 73 74 72 75 63 74 75 72  unction structur
1b887 65 20 74 68 61 74 0a 2a 2a 20 64 65 66 69 6e 65  e that.** define
1b888 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 29 20  s the function) 
1b889 77 69 74 68 20 50 35 20 61 72 67 75 6d 65 6e 74  with P5 argument
1b88a 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67  s taken from reg
1b88b 69 73 74 65 72 20 50 32 20 61 6e 64 0a 2a 2a 20  ister P2 and.** 
1b88c 73 75 63 63 65 73 73 6f 72 73 2e 20 20 54 68 65  successors.  The
1b88d 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66   result of the f
1b88e 75 6e 63 74 69 6f 6e 20 69 73 20 73 74 6f 72 65  unction is store
1b88f 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
1b890 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33  ..** Register P3
1b891 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 6f 6e 65   must not be one
1b892 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
1b893 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a 2a 20 50   inputs..**.** P
1b894 31 20 69 73 20 61 20 33 32 2d 62 69 74 20 62 69  1 is a 32-bit bi
1b895 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67  tmask indicating
1b896 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1b897 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  each argument to
1b898 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f   the .** functio
1b899 6e 20 77 61 73 20 64 65 74 65 72 6d 69 6e 65 64  n was determined
1b89a 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20   to be constant 
1b89b 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e  at compile time.
1b89c 20 49 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   If the first.**
1b89d 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 63 6f   argument was co
1b89e 6e 73 74 61 6e 74 20 74 68 65 6e 20 62 69 74 20  nstant then bit 
1b89f 30 20 6f 66 20 50 31 20 69 73 20 73 65 74 2e 20  0 of P1 is set. 
1b8a0 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
1b8a1 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65  determine.** whe
1b8a2 74 68 65 72 20 6d 65 74 61 20 64 61 74 61 20 61  ther meta data a
1b8a3 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
1b8a4 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 61   user function a
1b8a5 72 67 75 6d 65 6e 74 20 75 73 69 6e 67 20 74 68  rgument using th
1b8a6 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 65 74  e.** sqlite3_set
1b8a7 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 20 6d  _auxdata() API m
1b8a8 61 79 20 62 65 20 73 61 66 65 6c 79 20 72 65 74  ay be safely ret
1b8a9 61 69 6e 65 64 20 75 6e 74 69 6c 20 74 68 65 20  ained until the 
1b8aa 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  next.** invocati
1b8ab 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70 63 6f 64  on of this opcod
1b8ac 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  e..**.** See als
1b8ad 6f 3a 20 41 67 67 53 74 65 70 20 61 6e 64 20 41  o: AggStep and A
1b8ae 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61 73 65 20  ggFinal.*/.case 
1b8af 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 7b 0a 20  OP_Function: {. 
1b8b0 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70   int i;.  Mem *p
1b8b1 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63  Arg;.  sqlite3_c
1b8b2 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71  ontext ctx;.  sq
1b8b3 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
1b8b4 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 70  Val;.  int n = p
1b8b5 4f 70 2d 3e 70 35 3b 0a 0a 20 20 61 70 56 61 6c  Op->p5;..  apVal
1b8b6 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61   = p->apArg;.  a
1b8b7 73 73 65 72 74 28 20 61 70 56 61 6c 20 7c 7c 20  ssert( apVal || 
1b8b8 6e 3d 3d 30 20 29 3b 0a 0a 20 20 61 73 73 65 72  n==0 );..  asser
1b8b9 74 28 20 6e 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d  t( n==0 || (pOp-
1b8ba 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  >p2>0 && pOp->p2
1b8bb 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29  +n<=p->nMem+1) )
1b8bc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1b8bd 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 70  >p3<pOp->p2 || p
1b8be 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 2b  Op->p3>=pOp->p2+
1b8bf 6e 20 29 3b 0a 20 20 70 41 72 67 20 3d 20 26 70  n );.  pArg = &p
1b8c0 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b  ->aMem[pOp->p2];
1b8c1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b  .  for(i=0; i<n;
1b8c2 20 69 2b 2b 2c 20 70 41 72 67 2b 2b 29 7b 0a 20   i++, pArg++){. 
1b8c3 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 41     apVal[i] = pA
1b8c4 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70  rg;.    storeTyp
1b8c5 65 49 6e 66 6f 28 70 41 72 67 2c 20 65 6e 63 6f  eInfo(pArg, enco
1b8c6 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 47 49 53  ding);.    REGIS
1b8c7 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1b8c8 32 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a 0a 20  2, pArg);.  }.. 
1b8c9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1b8ca 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46  type==P4_FUNCDEF
1b8cb 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d   || pOp->p4type=
1b8cc 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 29 3b 0a  =P4_VDBEFUNC );.
1b8cd 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70    if( pOp->p4typ
1b8ce 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 29 7b  e==P4_FUNCDEF ){
1b8cf 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20 3d  .    ctx.pFunc =
1b8d0 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a   pOp->p4.pFunc;.
1b8d1 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75 6e      ctx.pVdbeFun
1b8d2 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  c = 0;.  }else{.
1b8d3 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75 6e      ctx.pVdbeFun
1b8d4 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a 29 70  c = (VdbeFunc*)p
1b8d5 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63  Op->p4.pVdbeFunc
1b8d6 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20  ;.    ctx.pFunc 
1b8d7 3d 20 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2d  = ctx.pVdbeFunc-
1b8d8 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 61  >pFunc;.  }..  a
1b8d9 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1b8da 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e   && pOp->p3<=p->
1b8db 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d  nMem );.  pOut =
1b8dc 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1b8dd 33 5d 3b 0a 20 20 63 74 78 2e 73 2e 66 6c 61 67  3];.  ctx.s.flag
1b8de 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1b8df 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a 20  ctx.s.db = db;. 
1b8e0 20 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b   ctx.s.xDel = 0;
1b8e1 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63  .  ctx.s.zMalloc
1b8e2 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20   = 0;..  /* The 
1b8e3 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20  output cell may 
1b8e4 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62  already have a b
1b8e5 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e  uffer allocated.
1b8e6 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 70   Move.  ** the p
1b8e7 6f 69 6e 74 65 72 20 74 6f 20 63 74 78 2e 73 20  ointer to ctx.s 
1b8e8 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20 75  so in case the u
1b8e9 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 63 61 6e  ser-function can
1b8ea 20 75 73 65 0a 20 20 2a 2a 20 74 68 65 20 61 6c   use.  ** the al
1b8eb 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20  ready allocated 
1b8ec 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f  buffer instead o
1b8ed 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e  f allocating a n
1b8ee 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73  ew one..  */.  s
1b8ef 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1b8f0 65 28 26 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b  e(&ctx.s, pOut);
1b8f1 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1b8f2 67 28 26 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e 75  g(&ctx.s, MEM_Nu
1b8f3 6c 6c 29 3b 0a 0a 20 20 63 74 78 2e 69 73 45 72  ll);..  ctx.isEr
1b8f4 72 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 63  ror = 0;.  if( c
1b8f5 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20  tx.pFunc->flags 
1b8f6 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  & SQLITE_FUNC_NE
1b8f7 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73  EDCOLL ){.    as
1b8f8 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70  sert( pOp>p->aOp
1b8f9 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1b8fa 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d  pOp[-1].p4type==
1b8fb 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20  P4_COLLSEQ );.  
1b8fc 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31    assert( pOp[-1
1b8fd 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c  ].opcode==OP_Col
1b8fe 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78 2e  lSeq );.    ctx.
1b8ff 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e  pColl = pOp[-1].
1b900 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20  p4.pColl;.  }.  
1b901 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1b902 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20  yOff(db) ) goto 
1b903 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1b904 75 73 65 3b 0a 20 20 28 2a 63 74 78 2e 70 46 75  use;.  (*ctx.pFu
1b905 6e 63 2d 3e 78 46 75 6e 63 29 28 26 63 74 78 2c  nc->xFunc)(&ctx,
1b906 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66   n, apVal);.  if
1b907 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1b908 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 71 6c  n(db) ){.    sql
1b909 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1b90a 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 20 20  se(&ctx.s);.    
1b90b 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1b90c 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20  o_misuse;.  }.  
1b90d 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1b90e 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 45  iled ){.    /* E
1b90f 76 65 6e 20 74 68 6f 75 67 68 20 61 20 6d 61 6c  ven though a mal
1b910 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64  loc() has failed
1b911 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  , the implementa
1b912 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20 20  tion of the.    
1b913 2a 2a 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  ** user function
1b914 20 6d 61 79 20 68 61 76 65 20 63 61 6c 6c 65 64   may have called
1b915 20 61 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 75   an sqlite3_resu
1b916 6c 74 5f 58 58 58 28 29 20 66 75 6e 63 74 69 6f  lt_XXX() functio
1b917 6e 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 74 75  n.    ** to retu
1b918 72 6e 20 61 20 76 61 6c 75 65 2e 20 54 68 65 20  rn a value. The 
1b919 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 72  following call r
1b91a 65 6c 65 61 73 65 73 20 61 6e 79 20 72 65 73 6f  eleases any reso
1b91b 75 72 63 65 73 0a 20 20 20 20 2a 2a 20 61 73 73  urces.    ** ass
1b91c 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 75 63  ociated with suc
1b91d 68 20 61 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a  h a value..    *
1b91e 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 4d  *.    ** Note: M
1b91f 61 79 62 65 20 4d 65 6d 52 65 6c 65 61 73 65 28  aybe MemRelease(
1b920 29 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c  ) should be call
1b921 65 64 20 69 66 20 73 71 6c 69 74 65 33 53 61 66  ed if sqlite3Saf
1b922 65 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a 20 66  etyOn().    ** f
1b923 61 69 6c 73 20 61 6c 73 6f 20 28 74 68 65 20 69  ails also (the i
1b924 66 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65 6e 74  f(...) statement
1b925 20 61 62 6f 76 65 29 2e 20 42 75 74 20 69 66 20   above). But if 
1b926 70 65 6f 70 6c 65 20 61 72 65 0a 20 20 20 20 2a  people are.    *
1b927 2a 20 6d 69 73 75 73 69 6e 67 20 73 71 6c 69 74  * misusing sqlit
1b928 65 2c 20 74 68 65 79 20 68 61 76 65 20 62 69 67  e, they have big
1b929 67 65 72 20 70 72 6f 62 6c 65 6d 73 20 74 68 61  ger problems tha
1b92a 6e 20 61 20 6c 65 61 6b 65 64 20 76 61 6c 75 65  n a leaked value
1b92b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
1b92c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1b92d 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 20 20  se(&ctx.s);.    
1b92e 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
1b92f 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61 75  ..  /* If any au
1b930 78 69 6c 69 61 72 79 20 64 61 74 61 20 66 75 6e  xiliary data fun
1b931 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e  ctions have been
1b932 20 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73 20   called by this 
1b933 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20  user function,. 
1b934 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   ** immediately 
1b935 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63  call the destruc
1b936 74 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d  tor for any non-
1b937 73 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a 20  static values.. 
1b938 20 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e 70 56   */.  if( ctx.pV
1b939 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 73  dbeFunc ){.    s
1b93a 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
1b93b 41 75 78 44 61 74 61 28 63 74 78 2e 70 56 64 62  AuxData(ctx.pVdb
1b93c 65 46 75 6e 63 2c 20 70 4f 70 2d 3e 70 31 29 3b  eFunc, pOp->p1);
1b93d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 56 64  .    pOp->p4.pVd
1b93e 62 65 46 75 6e 63 20 3d 20 63 74 78 2e 70 56 64  beFunc = ctx.pVd
1b93f 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70 2d  beFunc;.    pOp-
1b940 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 44 42  >p4type = P4_VDB
1b941 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  EFUNC;.  }..  /*
1b942 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   If the function
1b943 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72   returned an err
1b944 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63  or, throw an exc
1b945 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20  eption */.  if( 
1b946 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20  ctx.isError ){. 
1b947 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1b948 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1b949 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
1b94a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 63  e3_value_text(&c
1b94b 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d  tx.s));.    rc =
1b94c 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20   ctx.isError;.  
1b94d 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65  }..  /* Copy the
1b94e 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66   result of the f
1b94f 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72 65 67  unction into reg
1b950 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 73 71  ister P3 */.  sq
1b951 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
1b952 6e 63 6f 64 69 6e 67 28 26 63 74 78 2e 73 2c 20  ncoding(&ctx.s, 
1b953 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c  encoding);.  sql
1b954 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
1b955 70 4f 75 74 2c 20 26 63 74 78 2e 73 29 3b 0a 20  pOut, &ctx.s);. 
1b956 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
1b957 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74 29 20  MemTooBig(pOut) 
1b958 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  ){.    goto too_
1b959 62 69 67 3b 0a 20 20 7d 0a 20 20 52 45 47 49 53  big;.  }.  REGIS
1b95a 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1b95b 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 44 41  3, pOut);.  UPDA
1b95c 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1b95d 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pOut);.  break;.
1b95e 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69  }../* Opcode: Bi
1b95f 74 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20  tAnd P1 P2 P3 * 
1b960 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65  *.**.** Take the
1b961 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20 6f 66   bit-wise AND of
1b962 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72   the values in r
1b963 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50  egister P1 and P
1b964 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74  2 and.** store t
1b965 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
1b966 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20  ister P3..** If 
1b967 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
1b968 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
1b969 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
1b96a 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20 50 31  Opcode: BitOr P1
1b96b 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1b96c 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77 69   Take the bit-wi
1b96d 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c  se OR of the val
1b96e 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20  ues in register 
1b96f 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a  P1 and P2 and.**
1b970 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1b971 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1b972 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1b973 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1b974 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1b975 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1b976 53 68 69 66 74 4c 65 66 74 20 50 31 20 50 32 20  ShiftLeft P1 P2 
1b977 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69  P3 * *.**.** Shi
1b978 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  ft the integer v
1b979 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1b97a 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66 74 20   P2 to the left 
1b97b 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  by the.** number
1b97c 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 69   of bits specifi
1b97d 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 65  ed by the intege
1b97e 72 20 69 6e 20 72 65 67 69 73 65 72 20 50 31 2e  r in regiser P1.
1b97f 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65  .** Store the re
1b980 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72  sult in register
1b981 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65   P3..** If eithe
1b982 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c  r input is NULL,
1b983 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
1b984 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ULL..*/./* Opcod
1b985 65 3a 20 53 68 69 66 74 52 69 67 68 74 20 50 31  e: ShiftRight P1
1b986 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1b987 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 67   Shift the integ
1b988 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  er value in regi
1b989 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 72  ster P2 to the r
1b98a 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e  ight by the.** n
1b98b 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70  umber of bits sp
1b98c 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69  ecified by the i
1b98d 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 74  nteger in regist
1b98e 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20  er P1..** Store 
1b98f 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
1b990 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66  gister P3..** If
1b991 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73   either input is
1b992 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
1b993 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  t is NULL..*/.ca
1b994 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20  se OP_BitAnd:   
1b995 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b996 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41   same as TK_BITA
1b997 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  ND, in1, in2, ou
1b998 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69  t3 */.case OP_Bi
1b999 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20 20 20  tOr:            
1b99a 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1b99b 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31 2c 20   TK_BITOR, in1, 
1b99c 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73  in2, out3 */.cas
1b99d 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a 20  e OP_ShiftLeft: 
1b99e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b99f 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46  same as TK_LSHIF
1b9a0 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74  T, in1, in2, out
1b9a1 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69  3 */.case OP_Shi
1b9a2 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20 20 20  ftRight: {      
1b9a3 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1b9a4 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31 2c 20  TK_RSHIFT, in1, 
1b9a5 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 69  in2, out3 */.  i
1b9a6 36 34 20 61 2c 20 62 3b 0a 0a 20 20 69 66 28 20  64 a, b;..  if( 
1b9a7 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70  (pIn1->flags | p
1b9a8 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45  In2->flags) & ME
1b9a9 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71  M_Null ){.    sq
1b9aa 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1b9ab 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62  ull(pOut);.    b
1b9ac 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 61 20 3d 20  reak;.  }.  a = 
1b9ad 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1b9ae 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 62 20 3d  lue(pIn2);.  b =
1b9af 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
1b9b0 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 73 77  alue(pIn1);.  sw
1b9b1 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  itch( pOp->opcod
1b9b2 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50  e ){.    case OP
1b9b3 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20 61 20  _BitAnd:      a 
1b9b4 26 3d 20 62 3b 20 20 20 20 20 62 72 65 61 6b 3b  &= b;     break;
1b9b5 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74  .    case OP_Bit
1b9b6 4f 72 3a 20 20 20 20 20 20 20 61 20 7c 3d 20 62  Or:       a |= b
1b9b7 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
1b9b8 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65   case OP_ShiftLe
1b9b9 66 74 3a 20 20 20 61 20 3c 3c 3d 20 62 3b 20 20  ft:   a <<= b;  
1b9ba 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66    break;.    def
1b9bb 61 75 6c 74 3a 20 20 61 73 73 65 72 74 28 20 70  ault:  assert( p
1b9bc 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53  Op->opcode==OP_S
1b9bd 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20 20  hiftRight );.   
1b9be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b9bf 20 20 20 20 20 20 61 20 3e 3e 3d 20 62 3b 20 20        a >>= b;  
1b9c0 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70    break;.  }.  p
1b9c1 4f 75 74 2d 3e 75 2e 69 20 3d 20 61 3b 0a 20 20  Out->u.i = a;.  
1b9c2 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1b9c3 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Out, MEM_Int);. 
1b9c4 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1b9c5 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20 50 31  code: AddImm  P1
1b9c6 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a   P2 * * *.** .**
1b9c7 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74 61 6e   Add the constan
1b9c8 74 20 50 32 20 74 6f 20 74 68 65 20 76 61 6c 75  t P2 to the valu
1b9c9 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1b9ca 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20  ..** The result 
1b9cb 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 6e 74  is always an int
1b9cc 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 66  eger..**.** To f
1b9cd 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73 74 65  orce any registe
1b9ce 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67  r to be an integ
1b9cf 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30 2e 0a  er, just add 0..
1b9d0 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 49 6d  */.case OP_AddIm
1b9d1 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  m: {            
1b9d2 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71 6c 69  /* in1 */.  sqli
1b9d3 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
1b9d4 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 49  rify(pIn1);.  pI
1b9d5 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e  n1->u.i += pOp->
1b9d6 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  p2;.  break;.}..
1b9d7 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 74 42  /* Opcode: MustB
1b9d8 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  eInt P1 P2 * * *
1b9d9 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 74 68  .** .** Force th
1b9da 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1b9db 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 6e 20  ter P1 to be an 
1b9dc 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74 68 65  integer.  If the
1b9dd 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1b9de 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65  is not an intege
1b9df 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 65 20  r and cannot be 
1b9e0 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61  converted into a
1b9e1 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 69 74  n integer.** wit
1b9e2 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 2c 20  hout data loss, 
1b9e3 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69  then jump immedi
1b9e4 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f 72 20  ately to P2, or 
1b9e5 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 69 73  if P2==0.** rais
1b9e6 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 4d  e an SQLITE_MISM
1b9e7 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e 2e 0a  ATCH exception..
1b9e8 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 74 42  */.case OP_MustB
1b9e9 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20  eInt: {         
1b9ea 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20     /* jump, in1 
1b9eb 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69  */.  applyAffini
1b9ec 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f  ty(pIn1, SQLITE_
1b9ed 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63  AFF_NUMERIC, enc
1b9ee 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 20 28 70  oding);.  if( (p
1b9ef 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1b9f0 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20  _Int)==0 ){.    
1b9f1 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20 29  if( pOp->p2==0 )
1b9f2 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1b9f3 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b 0a 20 20  ITE_MISMATCH;.  
1b9f4 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1b9f5 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1b9f6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63   }else{.      pc
1b9f7 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1b9f8 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1b9f9 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1b9fa 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29  g(pIn1, MEM_Int)
1b9fb 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1b9fc 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61  ../* Opcode: Rea
1b9fd 6c 41 66 66 69 6e 69 74 79 20 50 31 20 2a 20 2a  lAffinity P1 * *
1b9fe 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65   * *.**.** If re
1b9ff 67 69 73 74 65 72 20 50 31 20 68 6f 6c 64 73 20  gister P1 holds 
1ba00 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 76 65  an integer conve
1ba01 72 74 20 69 74 20 74 6f 20 61 20 72 65 61 6c 20  rt it to a real 
1ba02 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  value..**.** Thi
1ba03 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64  s opcode is used
1ba04 20 77 68 65 6e 20 65 78 74 72 61 63 74 69 6e 67   when extracting
1ba05 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
1ba06 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a  m a column that.
1ba07 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 66 66 69  ** has REAL affi
1ba08 6e 69 74 79 2e 20 20 53 75 63 68 20 63 6f 6c 75  nity.  Such colu
1ba09 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 20 73 74  mn values may st
1ba0a 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 61 73  ill be stored as
1ba0b 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c 20 66 6f  .** integers, fo
1ba0c 72 20 73 70 61 63 65 20 65 66 66 69 63 69 65 6e  r space efficien
1ba0d 63 79 2c 20 62 75 74 20 61 66 74 65 72 20 65 78  cy, but after ex
1ba0e 74 72 61 63 74 69 6f 6e 20 77 65 20 77 61 6e 74  traction we want
1ba0f 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 61 76 65   them.** to have
1ba10 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 76 61 6c   only a real val
1ba11 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  ue..*/.case OP_R
1ba12 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 7b 20 20  ealAffinity: {  
1ba13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ba14 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20  /* in1 */.  if( 
1ba15 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1ba16 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c  M_Int ){.    sql
1ba17 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69  ite3VdbeMemReali
1ba18 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20  fy(pIn1);.  }.  
1ba19 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  break;.}..#ifnde
1ba1a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
1ba1b 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  ST./* Opcode: To
1ba1c 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  Text P1 * * * *.
1ba1d 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20  **.** Force the 
1ba1e 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1ba1f 72 20 50 31 20 74 6f 20 62 65 20 74 65 78 74 2e  r P1 to be text.
1ba20 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1ba21 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f 6e   is numeric, con
1ba22 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 72  vert it to a str
1ba23 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  ing using the.**
1ba24 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 70   equivalent of p
1ba25 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62 20 76  rintf().  Blob v
1ba26 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68 61 6e  alues are unchan
1ba27 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61  ged and.** are a
1ba28 66 74 65 72 77 61 72 64 73 20 73 69 6d 70 6c 79  fterwards simply
1ba29 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
1ba2a 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  text..**.** A NU
1ba2b 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20  LL value is not 
1ba2c 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
1ba2d 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d  routine.  It rem
1ba2e 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  ains NULL..*/.ca
1ba2f 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20 7b 20  se OP_ToText: { 
1ba30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ba31 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
1ba32 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f 0a 20  O_TEXT, in1 */. 
1ba33 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73   if( pIn1->flags
1ba34 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72   & MEM_Null ) br
1ba35 65 61 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 4d  eak;.  assert( M
1ba36 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f  EM_Str==(MEM_Blo
1ba37 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49 6e 31 2d  b>>3) );.  pIn1-
1ba38 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49 6e 31 2d  >flags |= (pIn1-
1ba39 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29  >flags&MEM_Blob)
1ba3a 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41 66 66 69  >>3;.  applyAffi
1ba3b 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54  nity(pIn1, SQLIT
1ba3c 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f  E_AFF_TEXT, enco
1ba3d 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d 20 45 78  ding);.  rc = Ex
1ba3e 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a  pandBlob(pIn1);.
1ba3f 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
1ba40 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20  flags & MEM_Str 
1ba41 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
1ba42 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66  led );.  pIn1->f
1ba43 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e  lags &= ~(MEM_In
1ba44 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42  t|MEM_Real|MEM_B
1ba45 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0a 20  lob|MEM_Zero);. 
1ba46 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1ba47 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72  SIZE(pIn1);.  br
1ba48 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ba49 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a 20 2a  e: ToBlob P1 * *
1ba4a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65   * *.**.** Force
1ba4b 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1ba4c 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20  gister P1 to be 
1ba4d 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 68  a BLOB..** If th
1ba4e 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d 65 72  e value is numer
1ba4f 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 74  ic, convert it t
1ba50 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72 73 74  o a string first
1ba51 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61 72 65  ..** Strings are
1ba52 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65 72 70   simply reinterp
1ba53 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73 20 77  reted as blobs w
1ba54 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a  ith no change.**
1ba55 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79 69   to the underlyi
1ba56 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41  ng data..**.** A
1ba57 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
1ba58 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
1ba59 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
1ba5a 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
1ba5b 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a  .case OP_ToBlob:
1ba5c 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
1ba5d 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1ba5e 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a  K_TO_BLOB, in1 *
1ba5f 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c  /.  if( pIn1->fl
1ba60 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
1ba61 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20 28 70   break;.  if( (p
1ba62 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1ba63 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 20 20  _Blob)==0 ){.   
1ba64 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70   applyAffinity(p
1ba65 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  In1, SQLITE_AFF_
1ba66 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  TEXT, encoding);
1ba67 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e  .    assert( pIn
1ba68 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53  1->flags & MEM_S
1ba69 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  tr || db->malloc
1ba6a 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 4d 65  Failed );.    Me
1ba6b 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e  mSetTypeFlag(pIn
1ba6c 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20  1, MEM_Blob);.  
1ba6d 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 31 2d  }else{.    pIn1-
1ba6e 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
1ba6f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d 5f 42 6c  TypeMask&~MEM_Bl
1ba70 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54  ob);.  }.  UPDAT
1ba71 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1ba72 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  In1);.  break;.}
1ba73 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 4e  ../* Opcode: ToN
1ba74 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20 2a 20  umeric P1 * * * 
1ba75 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68  *.**.** Force th
1ba76 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1ba77 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e 75 6d  ter P1 to be num
1ba78 65 72 69 63 20 28 65 69 74 68 65 72 20 61 6e 0a  eric (either an.
1ba79 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 61 20  ** integer or a 
1ba7a 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 6e  floating-point n
1ba7b 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 74 68  umber.).** If th
1ba7c 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20  e value is text 
1ba7d 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20  or blob, try to 
1ba7e 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e  convert it to an
1ba7f 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71   using the.** eq
1ba80 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69  uivalent of atoi
1ba81 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 6e 64  () or atof() and
1ba82 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73   store 0 if no s
1ba83 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 0a  uch conversion .
1ba84 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a  ** is possible..
1ba85 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c  **.** A NULL val
1ba86 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  ue is not change
1ba87 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  d by this routin
1ba88 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e  e.  It remains N
1ba89 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
1ba8a 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20 20  ToNumeric: {    
1ba8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ba8c 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 4e   same as TK_TO_N
1ba8d 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f 0a 20  UMERIC, in1 */. 
1ba8e 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
1ba8f 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45  s & (MEM_Null|ME
1ba90 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29  M_Int|MEM_Real))
1ba91 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1ba92 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66  e3VdbeMemNumerif
1ba93 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62  y(pIn1);.  }.  b
1ba94 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1ba95 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  * SQLITE_OMIT_CA
1ba96 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ST */../* Opcode
1ba97 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20 2a  : ToInt P1 * * *
1ba98 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74   *.**.** Force t
1ba99 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1ba9a 73 74 65 72 20 50 31 20 62 65 20 61 6e 20 69 6e  ster P1 be an in
1ba9b 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 68  teger.  If.** Th
1ba9c 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 65  e value is curre
1ba9d 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d 62  ntly a real numb
1ba9e 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 61  er, drop its fra
1ba9f 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a  ctional part..**
1baa0 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73   If the value is
1baa1 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74   text or blob, t
1baa2 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74  ry to convert it
1baa3 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 75   to an integer u
1baa4 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69  sing the.** equi
1baa5 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29  valent of atoi()
1baa6 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20   and store 0 if 
1baa7 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69  no such conversi
1baa8 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a  on is possible..
1baa9 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c  **.** A NULL val
1baaa 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  ue is not change
1baab 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  d by this routin
1baac 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e  e.  It remains N
1baad 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
1baae 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20  ToInt: {        
1baaf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1bab0 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20  e as TK_TO_INT, 
1bab1 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49  in1 */.  if( (pI
1bab2 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1bab3 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Null)==0 ){.    
1bab4 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
1bab5 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a  tegerify(pIn1);.
1bab6 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1bab7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1bab8 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f  MIT_CAST./* Opco
1bab9 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 20 2a 20  de: ToReal P1 * 
1baba 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63  * * *.**.** Forc
1babb 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1babc 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65  egister P1 to be
1babd 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
1babe 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49 66 20  t number..** If 
1babf 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72  The value is cur
1bac0 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65 67 65  rently an intege
1bac1 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e 0a 2a  r, convert it..*
1bac2 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69  * If the value i
1bac3 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20  s text or blob, 
1bac4 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69  try to convert i
1bac5 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  t to an integer 
1bac6 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75  using the.** equ
1bac7 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28  ivalent of atoi(
1bac8 29 20 61 6e 64 20 73 74 6f 72 65 20 30 2e 30 20  ) and store 0.0 
1bac9 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65  if no such conve
1baca 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c  rsion is possibl
1bacb 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20  e..**.** A NULL 
1bacc 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61  value is not cha
1bacd 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  nged by this rou
1bace 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e  tine.  It remain
1bacf 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  s NULL..*/.case 
1bad0 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 20 20 20  OP_ToReal: {    
1bad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bad2 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52   same as TK_TO_R
1bad3 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66  EAL, in1 */.  if
1bad4 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1bad5 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b   MEM_Null)==0 ){
1bad6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1bad7 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 29  MemRealify(pIn1)
1bad8 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1bad9 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1bada 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a  E_OMIT_CAST */..
1badb 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 31  /* Opcode: Lt P1
1badc 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1badd 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76  ** Compare the v
1bade 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
1badf 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49 66  r P1 and P3.  If
1bae0 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31 29   reg(P3)<reg(P1)
1bae1 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f   then.** jump to
1bae2 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a 2a   address P2.  .*
1bae3 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49  *.** If the SQLI
1bae4 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69  TE_JUMPIFNULL bi
1bae5 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 61  t of P5 is set a
1bae6 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50 31  nd either reg(P1
1bae7 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 20  ) or.** reg(P3) 
1bae8 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1bae9 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 20  e the jump.  If 
1baea 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  the SQLITE_JUMPI
1baeb 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 73  FNULL .** bit is
1baec 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c 6c   clear then fall
1baed 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72 20   thru if either 
1baee 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e  operand is NULL.
1baef 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  .**.** The SQLIT
1baf0 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74 69  E_AFF_MASK porti
1baf1 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62 65  on of P5 must be
1baf2 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61   an affinity cha
1baf3 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c 49  racter -.** SQLI
1baf4 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 4c  TE_AFF_TEXT, SQL
1baf5 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 2c  ITE_AFF_INTEGER,
1baf6 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 41   and so forth. A
1baf7 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64  n attempt is mad
1baf8 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 20  e .** to coerce 
1baf9 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63 6f  both inputs acco
1bafa 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61 66  rding to this af
1bafb 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74 68  finity before th
1bafc 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20  e.** comparison 
1bafd 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65 20  is made. If the 
1bafe 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20  SQLITE_AFF_MASK 
1baff 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e 75  is 0x00, then nu
1bb00 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69 74  meric.** affinit
1bb01 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65 20  y is used. Note 
1bb02 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 74  that the affinit
1bb03 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 72  y conversions ar
1bb04 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63 6b  e stored.** back
1bb05 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 20   into the input 
1bb06 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64  registers P1 and
1bb07 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f 70   P3.  So this op
1bb08 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a 2a  code can cause.*
1bb09 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68 61  * persistent cha
1bb0a 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65 72  nges to register
1bb0b 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a 0a  s P1 and P3..**.
1bb0c 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e 76  ** Once any conv
1bb0d 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61 6b  ersions have tak
1bb0e 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e 65  en place, and ne
1bb0f 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20 4e  ither value is N
1bb10 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c  ULL, .** the val
1bb11 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65 64  ues are compared
1bb12 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73  . If both values
1bb13 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e 20   are blobs then 
1bb14 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20 75  memcmp() is.** u
1bb15 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
1bb16 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   the results of 
1bb17 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20  the comparison. 
1bb18 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a   If both values.
1bb19 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68 65  ** are text, the
1bb1a 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
1bb1b 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63  e collating func
1bb1c 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69  tion specified i
1bb1d 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65 64  n.** P4 is  used
1bb1e 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 61   to do the compa
1bb1f 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69 73  rison.  If P4 is
1bb20 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 74   not specified t
1bb21 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29 20  hen.** memcmp() 
1bb22 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61  is used to compa
1bb23 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e 20  re text string. 
1bb24 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20   If both values 
1bb25 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c 20  are.** numeric, 
1bb26 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20 63  then a numeric c
1bb27 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73 65  omparison is use
1bb28 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76 61  d. If the two va
1bb29 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 64  lues.** are of d
1bb2a 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c 20  ifferent types, 
1bb2b 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72 65  then numbers are
1bb2c 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73   considered less
1bb2d 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67 73   than.** strings
1bb2e 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65   and strings are
1bb2f 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73   considered less
1bb30 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a 0a   than blobs..**.
1bb31 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45  ** If the SQLITE
1bb32 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66 20  _STOREP2 bit of 
1bb33 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  P5 is set, then 
1bb34 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 6e  do not jump.  In
1bb35 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65 20  stead,.** store 
1bb36 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c 74  a boolean result
1bb37 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20 31   (either 0, or 1
1bb38 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72 65  , or NULL) in re
1bb39 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f 2a  gister P2..*/./*
1bb3a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 50   Opcode: Ne P1 P
1bb3b 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
1bb3c 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74   This works just
1bb3d 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63   like the Lt opc
1bb3e 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20  ode except that 
1bb3f 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65  the jump is take
1bb40 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72  n if.** the oper
1bb41 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 72  ands in register
1bb42 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 20  s P1 and P3 are 
1bb43 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65 20  not equal.  See 
1bb44 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f  the Lt opcode fo
1bb45 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20  r.** additional 
1bb46 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
1bb47 2f 2a 20 4f 70 63 6f 64 65 3a 20 45 71 20 50 31  /* Opcode: Eq P1
1bb48 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1bb49 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75  ** This works ju
1bb4a 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f  st like the Lt o
1bb4b 70 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61  pcode except tha
1bb4c 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61  t the jump is ta
1bb4d 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70  ken if.** the op
1bb4e 65 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74  erands in regist
1bb4f 65 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72  ers P1 and P3 ar
1bb50 65 20 65 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 20  e equal..** See 
1bb51 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f  the Lt opcode fo
1bb52 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
1bb53 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20  ormation..*/./* 
1bb54 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20 50 32  Opcode: Le P1 P2
1bb55 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1bb56 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20  This works just 
1bb57 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f  like the Lt opco
1bb58 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74  de except that t
1bb59 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1bb5a 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65   if.** the conte
1bb5b 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  nt of register P
1bb5c 33 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f  3 is less than o
1bb5d 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63  r equal to the c
1bb5e 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67  ontent of.** reg
1bb5f 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74  ister P1.  See t
1bb60 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72  he Lt opcode for
1bb61 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
1bb62 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f  rmation..*/./* O
1bb63 70 63 6f 64 65 3a 20 47 74 20 50 31 20 50 32 20  pcode: Gt P1 P2 
1bb64 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54  P3 P4 P5.**.** T
1bb65 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c  his works just l
1bb66 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64  ike the Lt opcod
1bb67 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  e except that th
1bb68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20  e jump is taken 
1bb69 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  if.** the conten
1bb6a 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33  t of register P3
1bb6b 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
1bb6c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a   the content of.
1bb6d 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20  ** register P1. 
1bb6e 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f   See the Lt opco
1bb6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  de for additiona
1bb70 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
1bb71 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 65 20  /./* Opcode: Ge 
1bb72 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1bb73 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
1bb74 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74  just like the Lt
1bb75 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
1bb76 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
1bb77 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20  taken if.** the 
1bb78 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73  content of regis
1bb79 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74 65  ter P3 is greate
1bb7a 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
1bb7b 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  to the content o
1bb7c 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31  f.** register P1
1bb7d 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70  .  See the Lt op
1bb7e 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f  code for additio
1bb7f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
1bb80 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71 3a 20  .*/.case OP_Eq: 
1bb81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bb82 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 2c 20   same as TK_EQ, 
1bb83 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
1bb84 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20  /.case OP_Ne:   
1bb85 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1bb86 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20 6a 75  ame as TK_NE, ju
1bb87 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
1bb88 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20 20  case OP_Lt:     
1bb89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1bb8a 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a 75 6d 70  e as TK_LT, jump
1bb8b 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61  , in1, in3 */.ca
1bb8c 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 20 20 20  se OP_Le:       
1bb8d 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1bb8e 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d 70 2c 20  as TK_LE, jump, 
1bb8f 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65  in1, in3 */.case
1bb90 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 20 20 20   OP_Gt:         
1bb91 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1bb92 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c 20 69 6e   TK_GT, jump, in
1bb93 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  1, in3 */.case O
1bb94 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Ge: {         
1bb95 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1bb96 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c  K_GE, jump, in1,
1bb97 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c   in3 */.  int fl
1bb98 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a  ags;.  int res;.
1bb99 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b    char affinity;
1bb9a 0a 0a 20 20 66 6c 61 67 73 20 3d 20 70 49 6e 31  ..  flags = pIn1
1bb9b 2d 3e 66 6c 61 67 73 7c 70 49 6e 33 2d 3e 66 6c  ->flags|pIn3->fl
1bb9c 61 67 73 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67  ags;..  if( flag
1bb9d 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  s&MEM_Null ){.  
1bb9e 20 20 2f 2a 20 49 66 20 65 69 74 68 65 72 20 6f    /* If either o
1bb9f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74  perand is NULL t
1bba0 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69  hen the result i
1bba1 73 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a 20  s always NULL.. 
1bba2 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69     ** The jump i
1bba3 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53  s taken if the S
1bba4 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1bba5 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20   bit is set..   
1bba6 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   */.    if( pOp-
1bba7 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f  >p5 & SQLITE_STO
1bba8 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20 70 4f  REP2 ){.      pO
1bba9 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1bbaa 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 4d 65  p->p2];.      Me
1bbab 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1bbac 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20  t, MEM_Null);.  
1bbad 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1bbae 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74  CE(pOp->p2, pOut
1bbaf 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1bbb0 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54   pOp->p5 & SQLIT
1bbb1 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29 7b 0a  E_JUMPIFNULL ){.
1bbb2 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1bbb3 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  p2-1;.    }.    
1bbb4 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 61 66  break;.  }..  af
1bbb5 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 35  finity = pOp->p5
1bbb6 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41   & SQLITE_AFF_MA
1bbb7 53 4b 3b 0a 20 20 69 66 28 20 61 66 66 69 6e 69  SK;.  if( affini
1bbb8 74 79 20 29 7b 0a 20 20 20 20 61 70 70 6c 79 41  ty ){.    applyA
1bbb9 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 61 66  ffinity(pIn1, af
1bbba 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67  finity, encoding
1bbbb 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69  );.    applyAffi
1bbbc 6e 69 74 79 28 70 49 6e 33 2c 20 61 66 66 69 6e  nity(pIn3, affin
1bbbd 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  ity, encoding);.
1bbbe 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
1bbbf 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20  ocFailed ) goto 
1bbc0 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61  no_mem;.  }..  a
1bbc1 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1bbc2 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c  pe==P4_COLLSEQ |
1bbc3 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d  | pOp->p4.pColl=
1bbc4 3d 30 20 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c  =0 );.  ExpandBl
1bbc5 6f 62 28 70 49 6e 31 29 3b 0a 20 20 45 78 70 61  ob(pIn1);.  Expa
1bbc6 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20  ndBlob(pIn3);.  
1bbc7 72 65 73 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d  res = sqlite3Mem
1bbc8 43 6f 6d 70 61 72 65 28 70 49 6e 33 2c 20 70 49  Compare(pIn3, pI
1bbc9 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c  n1, pOp->p4.pCol
1bbca 6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f  l);.  switch( pO
1bbcb 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20  p->opcode ){.   
1bbcc 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20   case OP_Eq:    
1bbcd 72 65 73 20 3d 20 72 65 73 3d 3d 30 3b 20 20 20  res = res==0;   
1bbce 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
1bbcf 65 20 4f 50 5f 4e 65 3a 20 20 20 20 72 65 73 20  e OP_Ne:    res 
1bbd0 3d 20 72 65 73 21 3d 30 3b 20 20 20 20 20 62 72  = res!=0;     br
1bbd1 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50  eak;.    case OP
1bbd2 5f 4c 74 3a 20 20 20 20 72 65 73 20 3d 20 72 65  _Lt:    res = re
1bbd3 73 3c 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b  s<0;      break;
1bbd4 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a  .    case OP_Le:
1bbd5 20 20 20 20 72 65 73 20 3d 20 72 65 73 3c 3d 30      res = res<=0
1bbd6 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
1bbd7 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20   case OP_Gt:    
1bbd8 72 65 73 20 3d 20 72 65 73 3e 30 3b 20 20 20 20  res = res>0;    
1bbd9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66    break;.    def
1bbda 61 75 6c 74 3a 20 20 20 20 20 20 20 72 65 73 20  ault:       res 
1bbdb 3d 20 72 65 73 3e 3d 30 3b 20 20 20 20 20 62 72  = res>=0;     br
1bbdc 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  eak;.  }..  if( 
1bbdd 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45  pOp->p5 & SQLITE
1bbde 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20  _STOREP2 ){.    
1bbdf 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1bbe0 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 4d 65  pOp->p2];.    Me
1bbe1 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1bbe2 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20  t, MEM_Int);.   
1bbe3 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 72 65 73   pOut->u.i = res
1bbe4 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54  ;.    REGISTER_T
1bbe5 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f  RACE(pOp->p2, pO
1bbe6 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ut);.  }else if(
1bbe7 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d   res ){.    pc =
1bbe8 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a   pOp->p2-1;.  }.
1bbe9 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1bbea 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69  pcode: Permutati
1bbeb 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  on * * * P4 *.**
1bbec 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d  .** Set the perm
1bbed 75 74 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20  utation used by 
1bbee 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f  the OP_Compare o
1bbef 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68  perator to be th
1bbf0 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e  e array.** of in
1bbf1 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a  tegers in P4..**
1bbf2 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74  .** The permutat
1bbf3 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69  ion is only vali
1bbf4 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74  d until the next
1bbf5 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c   OP_Permutation,
1bbf6 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20   OP_Compare,.** 
1bbf7 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52  OP_Halt, or OP_R
1bbf8 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63  esultRow.  Typic
1bbf9 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d  ally the OP_Perm
1bbfa 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f  utation should o
1bbfb 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74  ccur.** immediat
1bbfc 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65  ely prior to the
1bbfd 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a   OP_Compare..*/.
1bbfe 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74  case OP_Permutat
1bbff 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28  ion: {.  assert(
1bc00 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
1bc01 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61  _INTARRAY );.  a
1bc02 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61  ssert( pOp->p4.a
1bc03 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20  i );.  aPermute 
1bc04 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20  = pOp->p4.ai;.  
1bc05 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1bc06 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20  ode: Compare P1 
1bc07 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  P2 P3 P4 *.**.**
1bc08 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74   Compare to vect
1bc09 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65 72 73  ors of registers
1bc0a 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67   in reg(P1)..reg
1bc0b 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74  (P1+P3-1) (all t
1bc0c 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20  his.** one "A") 
1bc0d 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e  and in reg(P2)..
1bc0e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42  reg(P2+P3-1) ("B
1bc0f 22 29 2e 20 20 53 61 76 65 20 74 68 65 20 72 65  ").  Save the re
1bc10 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63  sult of.** the c
1bc11 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73  omparison for us
1bc12 65 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50  e by the next OP
1bc13 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a  _Jump instruct..
1bc14 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65  **.** P4 is a Ke
1bc15 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
1bc16 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c  that defines col
1bc17 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
1bc18 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64   and sort.** ord
1bc19 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70  ers for the comp
1bc1a 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72  arison.  The per
1bc1b 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73  mutation applies
1bc1c 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a   to registers.**
1bc1d 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49   only.  The KeyI
1bc1e 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65  nfo elements are
1bc1f 20 75 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c   used sequential
1bc20 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ly..**.** The co
1bc21 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f  mparison is a so
1bc22 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73  rt comparison, s
1bc23 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20  o NULLs compare 
1bc24 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20  equal,.** NULLs 
1bc25 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75  are less than nu
1bc26 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61  mbers, numbers a
1bc27 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72  re less than str
1bc28 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72  ings,.** and str
1bc29 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74 68  ings are less th
1bc2a 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73  an blobs..*/.cas
1bc2b 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a  e OP_Compare: {.
1bc2c 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70    int n = pOp->p
1bc2d 33 3b 0a 20 20 69 6e 74 20 69 2c 20 70 31 2c 20  3;.  int i, p1, 
1bc2e 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49  p2;.  const KeyI
1bc2f 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20  nfo *pKeyInfo = 
1bc30 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f  pOp->p4.pKeyInfo
1bc31 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20  ;.  assert( n>0 
1bc32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65  );.  assert( pKe
1bc33 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 70 31  yInfo!=0 );.  p1
1bc34 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73   = pOp->p1;.  as
1bc35 73 65 72 74 28 20 70 31 3e 30 20 26 26 20 70 31  sert( p1>0 && p1
1bc36 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b  +n<=p->nMem+1 );
1bc37 0a 20 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b  .  p2 = pOp->p2;
1bc38 0a 20 20 61 73 73 65 72 74 28 20 70 32 3e 30 20  .  assert( p2>0 
1bc39 26 26 20 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d  && p2+n<=p->nMem
1bc3a 2b 31 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  +1 );.  for(i=0;
1bc3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<n; i++){.    
1bc3c 69 6e 74 20 69 64 78 20 3d 20 61 50 65 72 6d 75  int idx = aPermu
1bc3d 74 65 20 3f 20 61 50 65 72 6d 75 74 65 5b 69 5d  te ? aPermute[i]
1bc3e 20 3a 20 69 3b 0a 20 20 20 20 43 6f 6c 6c 53 65   : i;.    CollSe
1bc3f 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20  q *pColl;    /* 
1bc40 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
1bc41 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69  ce to use on thi
1bc42 73 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e  s term */.    in
1bc43 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20  t bRev;         
1bc44 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53   /* True for DES
1bc45 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64  CENDING sort ord
1bc46 65 72 20 2a 2f 0a 20 20 20 20 52 45 47 49 53 54  er */.    REGIST
1bc47 45 52 5f 54 52 41 43 45 28 70 31 2b 69 64 78 2c  ER_TRACE(p1+idx,
1bc48 20 26 70 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78   &p->aMem[p1+idx
1bc49 5d 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52  ]);.    REGISTER
1bc4a 5f 54 52 41 43 45 28 70 32 2b 69 64 78 2c 20 26  _TRACE(p2+idx, &
1bc4b 70 2d 3e 61 4d 65 6d 5b 70 32 2b 69 64 78 5d 29  p->aMem[p2+idx])
1bc4c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c  ;.    assert( i<
1bc4d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1bc4e 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20   );.    pColl = 
1bc4f 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b  pKeyInfo->aColl[
1bc50 69 5d 3b 0a 20 20 20 20 62 52 65 76 20 3d 20 70  i];.    bRev = p
1bc51 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72  KeyInfo->aSortOr
1bc52 64 65 72 5b 69 5d 3b 0a 20 20 20 20 69 43 6f 6d  der[i];.    iCom
1bc53 70 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65  pare = sqlite3Me
1bc54 6d 43 6f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65  mCompare(&p->aMe
1bc55 6d 5b 70 31 2b 69 64 78 5d 2c 20 26 70 2d 3e 61  m[p1+idx], &p->a
1bc56 4d 65 6d 5b 70 32 2b 69 64 78 5d 2c 20 70 43 6f  Mem[p2+idx], pCo
1bc57 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f  ll);.    if( iCo
1bc58 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20 69  mpare ){.      i
1bc59 66 28 20 62 52 65 76 20 29 20 69 43 6f 6d 70 61  f( bRev ) iCompa
1bc5a 72 65 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a  re = -iCompare;.
1bc5b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1bc5c 20 7d 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 74   }.  }.  aPermut
1bc5d 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a  e = 0;.  break;.
1bc5e 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75  }../* Opcode: Ju
1bc5f 6d 70 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  mp P1 P2 P3 * *.
1bc60 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68  **.** Jump to th
1bc61 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74  e instruction at
1bc62 20 61 64 64 72 65 73 73 20 50 31 2c 20 50 32 2c   address P1, P2,
1bc63 20 6f 72 20 50 33 20 64 65 70 65 6e 64 69 6e 67   or P3 depending
1bc64 20 6f 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 69   on whether.** i
1bc65 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  n the most recen
1bc66 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73  t OP_Compare ins
1bc67 74 72 75 63 74 69 6f 6e 20 74 68 65 20 50 31 20  truction the P1 
1bc68 76 65 63 74 6f 72 20 77 61 73 20 6c 65 73 73 20  vector was less 
1bc69 74 68 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f  than.** equal to
1bc6a 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61  , or greater tha
1bc6b 6e 20 74 68 65 20 50 32 20 76 65 63 74 6f 72 2c  n the P2 vector,
1bc6c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a   respectively..*
1bc6d 2f 0a 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20  /.case OP_Jump: 
1bc6e 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {             /*
1bc6f 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69   jump */.  if( i
1bc70 43 6f 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 20  Compare<0 ){.   
1bc71 20 70 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20   pc = pOp->p1 - 
1bc72 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69  1;.  }else if( i
1bc73 43 6f 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20  Compare==0 ){.  
1bc74 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1bc75 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
1bc76 20 70 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20   pc = pOp->p3 - 
1bc77 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1bc78 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e  }../* Opcode: An
1bc79 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  d P1 P2 P3 * *.*
1bc7a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f  *.** Take the lo
1bc7b 67 69 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65  gical AND of the
1bc7c 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73   values in regis
1bc7d 74 65 72 73 20 50 31 20 61 6e 64 20 50 32 20 61  ters P1 and P2 a
1bc7e 6e 64 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20  nd.** write the 
1bc7f 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69  result into regi
1bc80 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49  ster P3..**.** I
1bc81 66 20 65 69 74 68 65 72 20 50 31 20 6f 72 20 50  f either P1 or P
1bc82 32 20 69 73 20 30 20 28 66 61 6c 73 65 29 20 74  2 is 0 (false) t
1bc83 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69  hen the result i
1bc84 73 20 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74  s 0 even if.** t
1bc85 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74 20 69  he other input i
1bc86 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20  s NULL.  A NULL 
1bc87 61 6e 64 20 74 72 75 65 20 6f 72 20 74 77 6f 20  and true or two 
1bc88 4e 55 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20  NULLs give.** a 
1bc89 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a  NULL output..*/.
1bc8a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31  /* Opcode: Or P1
1bc8b 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1bc8c 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61   Take the logica
1bc8d 6c 20 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75  l OR of the valu
1bc8e 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  es in register P
1bc8f 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20  1 and P2 and.** 
1bc90 73 74 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72  store the answer
1bc91 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1bc92 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  .**.** If either
1bc93 20 50 31 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e   P1 or P2 is non
1bc94 7a 65 72 6f 20 28 74 72 75 65 29 20 74 68 65 6e  zero (true) then
1bc95 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 31   the result is 1
1bc96 20 28 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20   (true).** even 
1bc97 69 66 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70  if the other inp
1bc98 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e  ut is NULL.  A N
1bc99 55 4c 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f 72  ULL and false or
1bc9a 20 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69   two NULLs.** gi
1bc9b 76 65 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74  ve a NULL output
1bc9c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64  ..*/.case OP_And
1bc9d 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  :              /
1bc9e 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44  * same as TK_AND
1bc9f 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
1bca0 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20   */.case OP_Or: 
1bca1 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {             /*
1bca2 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20   same as TK_OR, 
1bca3 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1bca4 2f 0a 20 20 69 6e 74 20 76 31 2c 20 76 32 3b 20  /.  int v1, v2; 
1bca5 20 20 20 2f 2a 20 30 3d 3d 46 41 4c 53 45 2c 20     /* 0==FALSE, 
1bca6 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e  1==TRUE, 2==UNKN
1bca7 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 0a  OWN or NULL */..
1bca8 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67    if( pIn1->flag
1bca9 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  s & MEM_Null ){.
1bcaa 20 20 20 20 76 31 20 3d 20 32 3b 0a 20 20 7d 65      v1 = 2;.  }e
1bcab 6c 73 65 7b 0a 20 20 20 20 76 31 20 3d 20 73 71  lse{.    v1 = sq
1bcac 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1bcad 65 28 70 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a  e(pIn1)!=0;.  }.
1bcae 20 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67    if( pIn2->flag
1bcaf 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  s & MEM_Null ){.
1bcb0 20 20 20 20 76 32 20 3d 20 32 3b 0a 20 20 7d 65      v2 = 2;.  }e
1bcb1 6c 73 65 7b 0a 20 20 20 20 76 32 20 3d 20 73 71  lse{.    v2 = sq
1bcb2 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1bcb3 65 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a  e(pIn2)!=0;.  }.
1bcb4 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
1bcb5 65 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20  e==OP_And ){.   
1bcb6 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
1bcb7 73 69 67 6e 65 64 20 63 68 61 72 20 61 6e 64 5f  signed char and_
1bcb8 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30  logic[] = { 0, 0
1bcb9 2c 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c  , 0, 0, 1, 2, 0,
1bcba 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20   2, 2 };.    v1 
1bcbb 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b 76 31 2a 33  = and_logic[v1*3
1bcbc 2b 76 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  +v2];.  }else{. 
1bcbd 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
1bcbe 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72  unsigned char or
1bcbf 5f 6c 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20  _logic[] = { 0, 
1bcc0 31 2c 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32  1, 2, 1, 1, 1, 2
1bcc1 2c 20 31 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31  , 1, 2 };.    v1
1bcc2 20 3d 20 6f 72 5f 6c 6f 67 69 63 5b 76 31 2a 33   = or_logic[v1*3
1bcc3 2b 76 32 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  +v2];.  }.  if( 
1bcc4 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d 65 6d  v1==2 ){.    Mem
1bcc5 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1bcc6 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d  , MEM_Null);.  }
1bcc7 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74 2d 3e  else{.    pOut->
1bcc8 75 2e 69 20 3d 20 76 31 3b 0a 20 20 20 20 4d 65  u.i = v1;.    Me
1bcc9 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1bcca 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d  t, MEM_Int);.  }
1bccb 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1bccc 4f 70 63 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50  Opcode: Not P1 P
1bccd 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e  2 * * *.**.** In
1bcce 74 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75  terpret the valu
1bccf 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1bcd0 20 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61   as a boolean va
1bcd1 6c 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0a  lue.  Store the.
1bcd2 2a 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c  ** boolean compl
1bcd3 65 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65  ement in registe
1bcd4 72 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61  r P2.  If the va
1bcd5 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1bcd6 50 31 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20  P1 is .** NULL, 
1bcd7 74 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20 73  then a NULL is s
1bcd8 74 6f 72 65 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a  tored in P2..*/.
1bcd9 63 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20  case OP_Not: {  
1bcda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bcdb 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c   same as TK_NOT,
1bcdc 20 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d   in1 */.  pOut =
1bcdd 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1bcde 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e  2];.  if( pIn1->
1bcdf 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1bce0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1bce1 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f  dbeMemSetNull(pO
1bce2 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ut);.  }else{.  
1bce3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1bce4 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 21  SetInt64(pOut, !
1bce5 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1bce6 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a  lue(pIn1));.  }.
1bce7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1bce8 70 63 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31  pcode: BitNot P1
1bce9 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1bcea 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 63 6f  Interpret the co
1bceb 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
1bcec 72 20 50 31 20 61 73 20 61 6e 20 69 6e 74 65 67  r P1 as an integ
1bced 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a  er.  Store the.*
1bcee 2a 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e  * ones-complemen
1bcef 74 20 6f 66 20 74 68 65 20 50 31 20 76 61 6c 75  t of the P1 valu
1bcf0 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
1bcf1 50 32 2e 20 20 49 66 20 50 31 20 68 6f 6c 64 73  P2.  If P1 holds
1bcf2 0a 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20  .** a NULL then 
1bcf3 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20  store a NULL in 
1bcf4 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42  P2..*/.case OP_B
1bcf5 69 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20  itNot: {        
1bcf6 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1bcf7 54 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a  TK_BITNOT, in1 *
1bcf8 2f 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  /.  pOut = &p->a
1bcf9 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1bcfa 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  if( pIn1->flags 
1bcfb 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1bcfc 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1bcfd 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20  SetNull(pOut);. 
1bcfe 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
1bcff 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
1bd00 36 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65  64(pOut, ~sqlite
1bd01 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
1bd02 6e 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  n1));.  }.  brea
1bd03 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1bd04 20 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a   If P1 P2 P3 * *
1bd05 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50  .**.** Jump to P
1bd06 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69  2 if the value i
1bd07 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  n register P1 is
1bd08 20 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75   true.  The valu
1bd09 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69  e is.** is consi
1bd0a 64 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74  dered true if it
1bd0b 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20   is numeric and 
1bd0c 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68  non-zero.  If th
1bd0d 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31  e value.** in P1
1bd0e 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61   is NULL then ta
1bd0f 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50  ke the jump if P
1bd10 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a  3 is true..*/./*
1bd11 20 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50   Opcode: IfNot P
1bd12 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1bd13 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20  * Jump to P2 if 
1bd14 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1bd15 69 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73  ister P1 is Fals
1bd16 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  e.  The value is
1bd17 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65  .** is considere
1bd18 64 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73  d true if it has
1bd19 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65   a numeric value
1bd1a 20 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68   of zero.  If th
1bd1b 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31  e value.** in P1
1bd1c 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61   is NULL then ta
1bd1d 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50  ke the jump if P
1bd1e 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61  3 is true..*/.ca
1bd1f 73 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20  se OP_If:       
1bd20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d            /* jum
1bd21 70 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f  p, in1 */.case O
1bd22 50 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20  P_IfNot: {      
1bd23 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69        /* jump, i
1bd24 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 20  n1 */.  int c;. 
1bd25 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73   if( pIn1->flags
1bd26 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1bd27 20 20 20 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a     c = pOp->p3;.
1bd28 20 20 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20    }else{.#ifdef 
1bd29 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
1bd2a 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 63  TING_POINT.    c
1bd2b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
1bd2c 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b  tValue(pIn1)!=0;
1bd2d 0a 23 65 6c 73 65 0a 20 20 20 20 63 20 3d 20 73  .#else.    c = s
1bd2e 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
1bd2f 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 3b 0a  lue(pIn1)!=0.0;.
1bd30 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70  #endif.    if( p
1bd31 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49  Op->opcode==OP_I
1bd32 66 4e 6f 74 20 29 20 63 20 3d 20 21 63 3b 0a 20  fNot ) c = !c;. 
1bd33 20 7d 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20   }.  if( c ){.  
1bd34 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31    pc = pOp->p2-1
1bd35 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1bd36 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 4e  ../* Opcode: IsN
1bd37 75 6c 6c 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ull P1 P2 P3 * *
1bd38 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50  .**.** Jump to P
1bd39 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69  2 if the value i
1bd3a 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  n register P1 is
1bd3b 20 4e 55 4c 4c 2e 20 20 49 66 20 50 33 20 69 73   NULL.  If P3 is
1bd3c 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e   greater.** than
1bd3d 20 7a 65 72 6f 2c 20 74 68 65 6e 20 63 68 65 63   zero, then chec
1bd3e 6b 20 61 6c 6c 20 76 61 6c 75 65 73 20 72 65 67  k all values reg
1bd3f 28 50 31 29 2c 20 72 65 67 28 50 31 2b 31 29 2c  (P1), reg(P1+1),
1bd40 20 0a 2a 2a 20 72 65 67 28 50 31 2b 32 29 2c 20   .** reg(P1+2), 
1bd41 2e 2e 2e 2c 20 72 65 67 28 50 31 2b 50 33 2d 31  ..., reg(P1+P3-1
1bd42 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73  )..*/.case OP_Is
1bd43 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20  Null: {         
1bd44 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1bd45 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69  _ISNULL, jump, i
1bd46 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 6e 20 3d 20  n1 */.  int n = 
1bd47 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72  pOp->p3;.  asser
1bd48 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 7c 7c  t( pOp->p3==0 ||
1bd49 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20   pOp->p1>0 );.  
1bd4a 64 6f 7b 0a 20 20 20 20 69 66 28 20 28 70 49 6e  do{.    if( (pIn
1bd4b 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  1->flags & MEM_N
1bd4c 75 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ull)!=0 ){.     
1bd4d 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1bd4e 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  1;.      break;.
1bd4f 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 31 2b 2b      }.    pIn1++
1bd50 3b 0a 20 20 7d 77 68 69 6c 65 28 20 2d 2d 6e 20  ;.  }while( --n 
1bd51 3e 20 30 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  > 0 );.  break;.
1bd52 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  }../* Opcode: No
1bd53 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20  tNull P1 P2 * * 
1bd54 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20  *.**.** Jump to 
1bd55 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  P2 if the value 
1bd56 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69  in register P1 i
1bd57 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f  s not NULL.  .*/
1bd58 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c  .case OP_NotNull
1bd59 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1bd5a 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54  * same as TK_NOT
1bd5b 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20  NULL, jump, in1 
1bd5c 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  */.  if( (pIn1->
1bd5d 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1bd5e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d  )==0 ){.    pc =
1bd5f 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1bd60 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1bd61 20 4f 70 63 6f 64 65 3a 20 53 65 74 4e 75 6d 43   Opcode: SetNumC
1bd62 6f 6c 75 6d 6e 73 20 2a 20 50 32 20 2a 20 2a 20  olumns * P2 * * 
1bd63 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  *.**.** This opc
1bd64 6f 64 65 20 73 65 74 73 20 74 68 65 20 6e 75 6d  ode sets the num
1bd65 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66  ber of columns f
1bd66 6f 72 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70  or the cursor op
1bd67 65 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 66  ened by the.** f
1bd68 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63  ollowing instruc
1bd69 74 69 6f 6e 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  tion to P2..**.*
1bd6a 2a 20 41 6e 20 4f 50 5f 53 65 74 4e 75 6d 43 6f  * An OP_SetNumCo
1bd6b 6c 75 6d 6e 73 20 69 73 20 6f 6e 6c 79 20 75 73  lumns is only us
1bd6c 65 66 75 6c 20 69 66 20 69 74 20 6f 63 63 75 72  eful if it occur
1bd6d 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 65  s immediately be
1bd6e 66 6f 72 65 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20  fore .** one of 
1bd6f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70  the following op
1bd70 63 6f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  codes:.**.**    
1bd71 20 4f 70 65 6e 52 65 61 64 0a 2a 2a 20 20 20 20   OpenRead.**    
1bd72 20 4f 70 65 6e 57 72 69 74 65 0a 2a 2a 20 20 20   OpenWrite.**   
1bd73 20 20 4f 70 65 6e 50 73 65 75 64 6f 0a 2a 2a 0a    OpenPseudo.**.
1bd74 2a 2a 20 49 66 20 74 68 65 20 4f 50 5f 43 6f 6c  ** If the OP_Col
1bd75 75 6d 6e 20 6f 70 63 6f 64 65 20 69 73 20 74 6f  umn opcode is to
1bd76 20 62 65 20 65 78 65 63 75 74 65 64 20 6f 6e 20   be executed on 
1bd77 61 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 0a 2a  a cursor, then.*
1bd78 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 20 6d 75  * this opcode mu
1bd79 73 74 20 62 65 20 70 72 65 73 65 6e 74 20 69 6d  st be present im
1bd7a 6d 65 64 69 61 74 65 6c 79 20 62 65 66 6f 72 65  mediately before
1bd7b 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74   the opcode that
1bd7c 0a 2a 2a 20 6f 70 65 6e 73 20 74 68 65 20 63 75  .** opens the cu
1bd7d 72 73 6f 72 2e 0a 2a 2f 0a 23 69 66 20 30 0a 63  rsor..*/.#if 0.c
1bd7e 61 73 65 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c  ase OP_SetNumCol
1bd7f 75 6d 6e 73 3a 20 7b 0a 20 20 62 72 65 61 6b 3b  umns: {.  break;
1bd80 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70  .}.#endif../* Op
1bd81 63 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20 50 31 20  code: Column P1 
1bd82 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  P2 P3 P4 *.**.**
1bd83 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 64   Interpret the d
1bd84 61 74 61 20 74 68 61 74 20 63 75 72 73 6f 72 20  ata that cursor 
1bd85 50 31 20 70 6f 69 6e 74 73 20 74 6f 20 61 73 20  P1 points to as 
1bd86 61 20 73 74 72 75 63 74 75 72 65 20 62 75 69 6c  a structure buil
1bd87 74 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 4d  t using.** the M
1bd88 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75  akeRecord instru
1bd89 63 74 69 6f 6e 2e 20 20 28 53 65 65 20 74 68 65  ction.  (See the
1bd8a 20 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f   MakeRecord opco
1bd8b 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  de for additiona
1bd8c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  l.** information
1bd8d 20 61 62 6f 75 74 20 74 68 65 20 66 6f 72 6d 61   about the forma
1bd8e 74 20 6f 66 20 74 68 65 20 64 61 74 61 2e 29 20  t of the data.) 
1bd8f 20 45 78 74 72 61 63 74 20 74 68 65 20 50 32 2d   Extract the P2-
1bd90 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f  th column.** fro
1bd91 6d 20 74 68 69 73 20 72 65 63 6f 72 64 2e 20 20  m this record.  
1bd92 49 66 20 74 68 65 72 65 20 61 72 65 20 6c 65 73  If there are les
1bd93 73 20 74 68 61 74 20 28 50 32 2b 31 29 20 0a 2a  s that (P2+1) .*
1bd94 2a 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20  * values in the 
1bd95 72 65 63 6f 72 64 2c 20 65 78 74 72 61 63 74 20  record, extract 
1bd96 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68  a NULL..**.** Th
1bd97 65 20 76 61 6c 75 65 20 65 78 74 72 61 63 74 65  e value extracte
1bd98 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  d is stored in r
1bd99 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a  egister P3..**.*
1bd9a 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  * If the column 
1bd9b 63 6f 6e 74 61 69 6e 73 20 66 65 77 65 72 20 74  contains fewer t
1bd9c 68 61 6e 20 50 32 20 66 69 65 6c 64 73 2c 20 74  han P2 fields, t
1bd9d 68 65 6e 20 65 78 74 72 61 63 74 20 61 20 4e 55  hen extract a NU
1bd9e 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a 20 69 66 20 74  LL.  Or,.** if t
1bd9f 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69  he P4 argument i
1bda0 73 20 61 20 50 34 5f 4d 45 4d 20 75 73 65 20 74  s a P4_MEM use t
1bda1 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1bda2 50 34 20 61 72 67 75 6d 65 6e 74 20 61 73 0a 2a  P4 argument as.*
1bda3 2a 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f  * the result..*/
1bda4 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a  .case OP_Column:
1bda5 20 7b 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64   {.  int payload
1bda6 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65  Size;   /* Numbe
1bda7 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
1bda8 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  e record */.  in
1bda9 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20  t p1 = pOp->p1; 
1bdaa 20 2f 2a 20 50 31 20 76 61 6c 75 65 20 6f 66 20   /* P1 value of 
1bdab 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  the opcode */.  
1bdac 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32  int p2 = pOp->p2
1bdad 3b 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d  ;  /* column num
1bdae 62 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 20  ber to retrieve 
1bdaf 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
1bdb0 2a 70 43 20 3d 20 30 3b 2f 2a 20 54 68 65 20 56  *pC = 0;/* The V
1bdb1 44 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  DBE cursor */.  
1bdb2 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20  char *zRec;     
1bdb3 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1bdb4 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64   complete record
1bdb5 2d 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75 72  -data */.  BtCur
1bdb6 73 6f 72 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a  sor *pCrsr;   /*
1bdb7 20 54 68 65 20 42 54 72 65 65 20 63 75 72 73 6f   The BTree curso
1bdb8 72 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70  r */.  u32 *aTyp
1bdb9 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79  e;        /* aTy
1bdba 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20  pe[i] holds the 
1bdbb 6e 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20  numeric type of 
1bdbc 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20  the i-th column 
1bdbd 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65  */.  u32 *aOffse
1bdbe 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66 66 73  t;      /* aOffs
1bdbf 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 65 74 20  et[i] is offset 
1bdc0 74 6f 20 73 74 61 72 74 20 6f 66 20 64 61 74 61  to start of data
1bdc1 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75 6d 6e   for i-th column
1bdc2 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   */.  int nField
1bdc3 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62  ;        /* numb
1bdc4 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
1bdc5 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1bdc6 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20  int len;        
1bdc7 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68     /* The length
1bdc8 20 6f 66 20 74 68 65 20 73 65 72 69 61 6c 69 7a   of the serializ
1bdc9 65 64 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  ed data for the 
1bdca 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20  column */.  int 
1bdcb 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  i;             /
1bdcc 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1bdcd 2f 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b  /.  char *zData;
1bdce 20 20 20 20 20 20 20 2f 2a 20 50 61 72 74 20 6f         /* Part o
1bdcf 66 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69  f the record bei
1bdd0 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20  ng decoded */.  
1bdd1 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 20 20 20  Mem *pDest;     
1bdd2 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77     /* Where to w
1bdd3 72 69 74 65 20 74 68 65 20 65 78 74 72 61 63 74  rite the extract
1bdd4 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20 4d 65  ed value */.  Me
1bdd5 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  m sMem;         
1bdd6 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 20   /* For storing 
1bdd7 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67  the record being
1bdd8 20 64 65 63 6f 64 65 64 20 2a 2f 0a 0a 20 20 6d   decoded */..  m
1bdd9 65 6d 73 65 74 28 26 73 4d 65 6d 2c 20 30 2c 20  emset(&sMem, 0, 
1bdda 73 69 7a 65 6f 66 28 73 4d 65 6d 29 29 3b 0a 20  sizeof(sMem));. 
1bddb 20 61 73 73 65 72 74 28 20 70 31 3c 70 2d 3e 6e   assert( p1<p->n
1bddc 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
1bddd 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26  rt( pOp->p3>0 &&
1bdde 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65   pOp->p3<=p->nMe
1bddf 6d 20 29 3b 0a 20 20 70 44 65 73 74 20 3d 20 26  m );.  pDest = &
1bde0 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1bde1 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  ;.  MemSetTypeFl
1bde2 61 67 28 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75  ag(pDest, MEM_Nu
1bde3 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20  ll);..  /* This 
1bde4 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76  block sets the v
1bde5 61 72 69 61 62 6c 65 20 70 61 79 6c 6f 61 64 53  ariable payloadS
1bde6 69 7a 65 20 74 6f 20 62 65 20 74 68 65 20 74 6f  ize to be the to
1bde7 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20  tal number of.  
1bde8 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  ** bytes in the 
1bde9 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a 20 20 2a  record..  **.  *
1bdea 2a 20 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f  * zRec is set to
1bdeb 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65   be the complete
1bdec 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63   text of the rec
1bded 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61  ord if it is ava
1bdee 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 65  ilable..  ** The
1bdef 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64   complete record
1bdf0 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20   text is always 
1bdf1 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73  available for ps
1bdf2 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a 2a  eudo-tables.  **
1bdf3 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69   If the record i
1bdf4 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 75  s stored in a cu
1bdf5 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c 65  rsor, the comple
1bdf6 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a 20  te record text. 
1bdf7 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 61   ** might be ava
1bdf8 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 70  ilable in the  p
1bdf9 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
1bdfa 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20  Or it might not 
1bdfb 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20  be..  ** If the 
1bdfc 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61  data is unavaila
1bdfd 62 6c 65 2c 20 20 7a 52 65 63 20 69 73 20 73 65  ble,  zRec is se
1bdfe 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2a 0a  t to NULL..  **.
1bdff 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 6f 6d    ** We also com
1be00 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  pute the number 
1be01 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
1be02 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 20 63  e record.  For c
1be03 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 68 65  ursors,.  ** the
1be04 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1be05 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ns is stored in 
1be06 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 6e  the VdbeCursor.n
1be07 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e 0a 20  Field element.. 
1be08 20 2a 2f 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70   */.  pC = p->ap
1be09 43 73 72 5b 70 31 5d 3b 0a 20 20 61 73 73 65 72  Csr[p1];.  asser
1be0a 74 28 20 70 43 21 3d 30 20 29 3b 0a 23 69 66 6e  t( pC!=0 );.#ifn
1be0b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1be0c 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 61  VIRTUALTABLE.  a
1be0d 73 73 65 72 74 28 20 70 43 2d 3e 70 56 74 61 62  ssert( pC->pVtab
1be0e 43 75 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e  Cursor==0 );.#en
1be0f 64 69 66 0a 20 20 69 66 28 20 70 43 2d 3e 70 43  dif.  if( pC->pC
1be10 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20  ursor!=0 ){.    
1be11 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 20 69 73  /* The record is
1be12 20 73 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54   stored in a B-T
1be13 72 65 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ree */.    rc = 
1be14 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1be15 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 20  rMoveto(pC);.   
1be16 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61   if( rc ) goto a
1be17 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1be18 72 3b 0a 20 20 20 20 7a 52 65 63 20 3d 20 30 3b  r;.    zRec = 0;
1be19 0a 20 20 20 20 70 43 72 73 72 20 3d 20 70 43 2d  .    pCrsr = pC-
1be1a 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66  >pCursor;.    if
1be1b 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b  ( pC->nullRow ){
1be1c 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69  .      payloadSi
1be1d 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ze = 0;.    }els
1be1e 65 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65 53  e if( pC->cacheS
1be1f 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43  tatus==p->cacheC
1be20 74 72 20 29 7b 0a 20 20 20 20 20 20 70 61 79 6c  tr ){.      payl
1be21 6f 61 64 53 69 7a 65 20 3d 20 70 43 2d 3e 70 61  oadSize = pC->pa
1be22 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20  yloadSize;.     
1be23 20 7a 52 65 63 20 3d 20 28 63 68 61 72 2a 29 70   zRec = (char*)p
1be24 43 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c  C->aRow;.    }el
1be25 73 65 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64  se if( pC->isInd
1be26 65 78 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20  ex ){.      i64 
1be27 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20  payloadSize64;. 
1be28 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1be29 65 4b 65 79 53 69 7a 65 28 70 43 72 73 72 2c 20  eKeySize(pCrsr, 
1be2a 26 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29 3b  &payloadSize64);
1be2b 0a 20 20 20 20 20 20 70 61 79 6c 6f 61 64 53 69  .      payloadSi
1be2c 7a 65 20 3d 20 28 69 6e 74 29 70 61 79 6c 6f 61  ze = (int)payloa
1be2d 64 53 69 7a 65 36 34 3b 0a 20 20 20 20 7d 65 6c  dSize64;.    }el
1be2e 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
1be2f 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 70  3BtreeDataSize(p
1be30 43 72 73 72 2c 20 28 75 33 32 20 2a 29 26 70 61  Crsr, (u32 *)&pa
1be31 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 20 20  yloadSize);.    
1be32 7d 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 70  }.    nField = p
1be33 43 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 7d 65 6c  C->nField;.  }el
1be34 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
1be35 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20  pC->pseudoTable 
1be36 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65  );.    /* The re
1be37 63 6f 72 64 20 69 73 20 74 68 65 20 73 6f 6c 65  cord is the sole
1be38 20 65 6e 74 72 79 20 6f 66 20 61 20 70 73 65 75   entry of a pseu
1be39 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  do-table */.    
1be3a 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 43  payloadSize = pC
1be3b 2d 3e 6e 44 61 74 61 3b 0a 20 20 20 20 7a 52 65  ->nData;.    zRe
1be3c 63 20 3d 20 70 43 2d 3e 70 44 61 74 61 3b 0a 20  c = pC->pData;. 
1be3d 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74     pC->cacheStat
1be3e 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1be3f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61  ;.    assert( pa
1be40 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 7c 7c 20  yloadSize==0 || 
1be41 7a 52 65 63 21 3d 30 20 29 3b 0a 20 20 20 20 6e  zRec!=0 );.    n
1be42 46 69 65 6c 64 20 3d 20 70 43 2d 3e 6e 46 69 65  Field = pC->nFie
1be43 6c 64 3b 0a 20 20 20 20 70 43 72 73 72 20 3d 20  ld;.    pCrsr = 
1be44 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  0;.  }..  /* If 
1be45 70 61 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30  payloadSize is 0
1be46 2c 20 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72  , then just stor
1be47 65 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66  e a NULL */.  if
1be48 28 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30  ( payloadSize==0
1be49 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1be4a 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d  pDest->flags&MEM
1be4b 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 67 6f 74  _Null );.    got
1be4c 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b  o op_column_out;
1be4d 0a 20 20 7d 0a 20 20 69 66 28 20 70 61 79 6c 6f  .  }.  if( paylo
1be4e 61 64 53 69 7a 65 3e 64 62 2d 3e 61 4c 69 6d 69  adSize>db->aLimi
1be4f 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1be50 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f  ENGTH] ){.    go
1be51 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1be52 0a 20 20 61 73 73 65 72 74 28 20 70 32 3c 6e 46  .  assert( p2<nF
1be53 69 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65  ield );..  /* Re
1be54 61 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65  ad and parse the
1be55 20 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20   table header.  
1be56 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  Store the result
1be57 73 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20  s of the parse. 
1be58 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63   ** into the rec
1be59 6f 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65  ord header cache
1be5a 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63   fields of the c
1be5b 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 61 54  ursor..  */.  aT
1be5c 79 70 65 20 3d 20 70 43 2d 3e 61 54 79 70 65 3b  ype = pC->aType;
1be5d 0a 20 20 69 66 28 20 70 43 2d 3e 63 61 63 68 65  .  if( pC->cache
1be5e 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65  Status==p->cache
1be5f 43 74 72 20 29 7b 0a 20 20 20 20 61 4f 66 66 73  Ctr ){.    aOffs
1be60 65 74 20 3d 20 70 43 2d 3e 61 4f 66 66 73 65 74  et = pC->aOffset
1be61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
1be62 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20  8 *zIdx;        
1be63 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65  /* Index into he
1be64 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a  ader */.    u8 *
1be65 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 2f 2a 20  zEndHdr;     /* 
1be66 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74  Pointer to first
1be67 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20   byte after the 
1be68 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 6e  header */.    in
1be69 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 2f  t offset;      /
1be6a 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68  * Offset into th
1be6b 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 69 6e  e data */.    in
1be6c 74 20 73 7a 48 64 72 53 7a 3b 20 20 20 20 20 2f  t szHdrSz;     /
1be6d 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65  * Size of the he
1be6e 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20  ader size field 
1be6f 61 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f  at start of reco
1be70 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 61 76  rd */.    int av
1be71 61 69 6c 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75  ail = 0;   /* Nu
1be72 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1be73 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20   available data 
1be74 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 61  */..    assert(a
1be75 54 79 70 65 29 3b 0a 20 20 20 20 70 43 2d 3e 61  Type);.    pC->a
1be76 4f 66 66 73 65 74 20 3d 20 61 4f 66 66 73 65 74  Offset = aOffset
1be77 20 3d 20 26 61 54 79 70 65 5b 6e 46 69 65 6c 64   = &aType[nField
1be78 5d 3b 0a 20 20 20 20 70 43 2d 3e 70 61 79 6c 6f  ];.    pC->paylo
1be79 61 64 53 69 7a 65 20 3d 20 70 61 79 6c 6f 61 64  adSize = payload
1be7a 53 69 7a 65 3b 0a 20 20 20 20 70 43 2d 3e 63 61  Size;.    pC->ca
1be7b 63 68 65 53 74 61 74 75 73 20 3d 20 70 2d 3e 63  cheStatus = p->c
1be7c 61 63 68 65 43 74 72 3b 0a 0a 20 20 20 20 2f 2a  acheCtr;..    /*
1be7d 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
1be7e 6d 61 6e 79 20 62 79 74 65 73 20 61 72 65 20 69  many bytes are i
1be7f 6e 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  n the header */.
1be80 20 20 20 20 69 66 28 20 7a 52 65 63 20 29 7b 0a      if( zRec ){.
1be81 20 20 20 20 20 20 7a 44 61 74 61 20 3d 20 7a 52        zData = zR
1be82 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ec;.    }else{. 
1be83 20 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73 49       if( pC->isI
1be84 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20  ndex ){.        
1be85 7a 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29 73  zData = (char*)s
1be86 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65  qlite3BtreeKeyFe
1be87 74 63 68 28 70 43 72 73 72 2c 20 26 61 76 61 69  tch(pCrsr, &avai
1be88 6c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  l);.      }else{
1be89 0a 20 20 20 20 20 20 20 20 7a 44 61 74 61 20 3d  .        zData =
1be8a 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42   (char*)sqlite3B
1be8b 74 72 65 65 44 61 74 61 46 65 74 63 68 28 70 43  treeDataFetch(pC
1be8c 72 73 72 2c 20 26 61 76 61 69 6c 29 3b 0a 20 20  rsr, &avail);.  
1be8d 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49      }.      /* I
1be8e 66 20 4b 65 79 46 65 74 63 68 28 29 2f 44 61 74  f KeyFetch()/Dat
1be8f 61 46 65 74 63 68 28 29 20 6d 61 6e 61 67 65 64  aFetch() managed
1be90 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 74 69   to get the enti
1be91 72 65 20 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20  re payload,.    
1be92 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20 70 61    ** save the pa
1be93 79 6c 6f 61 64 20 69 6e 20 74 68 65 20 70 43 2d  yload in the pC-
1be94 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 54 68  >aRow cache.  Th
1be95 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75 73 20  at will save us 
1be96 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 68 61  from.      ** ha
1be97 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 64 64  ving to make add
1be98 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 74 6f  itional calls to
1be99 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65   fetch the conte
1be9a 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a 20 20  nt portion of.  
1be9b 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63 6f 72      ** the recor
1be9c 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
1be9d 20 20 69 66 28 20 61 76 61 69 6c 3e 3d 70 61 79    if( avail>=pay
1be9e 6c 6f 61 64 53 69 7a 65 20 29 7b 0a 20 20 20 20  loadSize ){.    
1be9f 20 20 20 20 7a 52 65 63 20 3d 20 7a 44 61 74 61      zRec = zData
1bea0 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 52  ;.        pC->aR
1bea1 6f 77 20 3d 20 28 75 38 2a 29 7a 44 61 74 61 3b  ow = (u8*)zData;
1bea2 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1bea3 20 20 20 20 20 20 70 43 2d 3e 61 52 6f 77 20 3d        pC->aRow =
1bea4 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1bea5 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c  }.    /* The fol
1bea6 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 69 73  lowing assert is
1bea7 20 74 72 75 65 20 69 6e 20 61 6c 6c 20 63 61 73   true in all cas
1bea8 65 73 20 61 63 63 65 70 74 20 77 68 65 6e 0a 20  es accept when. 
1bea9 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61     ** the databa
1beaa 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e  se file has been
1beab 20 63 6f 72 72 75 70 74 65 64 20 65 78 74 65 72   corrupted exter
1beac 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a 20 20 20  nally..    **   
1bead 20 61 73 73 65 72 74 28 20 7a 52 65 63 21 3d 30   assert( zRec!=0
1beae 20 7c 7c 20 61 76 61 69 6c 3e 3d 70 61 79 6c 6f   || avail>=paylo
1beaf 61 64 53 69 7a 65 20 7c 7c 20 61 76 61 69 6c 3e  adSize || avail>
1beb0 3d 39 20 29 3b 20 2a 2f 0a 20 20 20 20 73 7a 48  =9 ); */.    szH
1beb1 64 72 53 7a 20 3d 20 67 65 74 56 61 72 69 6e 74  drSz = getVarint
1beb2 33 32 28 28 75 38 2a 29 7a 44 61 74 61 2c 20 6f  32((u8*)zData, o
1beb3 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 2f 2a 20  ffset);..    /* 
1beb4 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20 6f  The KeyFetch() o
1beb5 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61 62  r DataFetch() ab
1beb6 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e 64  ove are fast and
1beb7 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65 6e   will get the en
1beb8 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f  tire.    ** reco
1beb9 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f 73  rd header in mos
1beba 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74 68  t cases.  But th
1bebb 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f 20  ey will fail to 
1bebc 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  get the complete
1bebd 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 68  .    ** record h
1bebe 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65 63  eader if the rec
1bebf 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73 20  ord header does 
1bec0 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69 6e  not fit on a sin
1bec1 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20  gle page.    ** 
1bec2 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 20  in the B-Tree.  
1bec3 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e  When that happen
1bec4 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56 64  s, use sqlite3Vd
1bec5 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29  beMemFromBtree()
1bec6 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 69   to.    ** acqui
1bec7 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  re the complete 
1bec8 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20 20  header text..   
1bec9 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 7a 52 65   */.    if( !zRe
1beca 63 20 26 26 20 61 76 61 69 6c 3c 6f 66 66 73 65  c && avail<offse
1becb 74 20 29 7b 0a 20 20 20 20 20 20 73 4d 65 6d 2e  t ){.      sMem.
1becc 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20  flags = 0;.     
1becd 20 73 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20   sMem.db = 0;.  
1bece 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1becf 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65  VdbeMemFromBtree
1bed0 28 70 43 72 73 72 2c 20 30 2c 20 6f 66 66 73 65  (pCrsr, 0, offse
1bed1 74 2c 20 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20  t, pC->isIndex, 
1bed2 26 73 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66  &sMem);.      if
1bed3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1bed4 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
1bed5 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20  op_column_out;. 
1bed6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 44 61       }.      zDa
1bed7 74 61 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20 20 20  ta = sMem.z;.   
1bed8 20 7d 0a 20 20 20 20 7a 45 6e 64 48 64 72 20 3d   }.    zEndHdr =
1bed9 20 28 75 38 20 2a 29 26 7a 44 61 74 61 5b 6f 66   (u8 *)&zData[of
1beda 66 73 65 74 5d 3b 0a 20 20 20 20 7a 49 64 78 20  fset];.    zIdx 
1bedb 3d 20 28 75 38 20 2a 29 26 7a 44 61 74 61 5b 73  = (u8 *)&zData[s
1bedc 7a 48 64 72 53 7a 5d 3b 0a 0a 20 20 20 20 2f 2a  zHdrSz];..    /*
1bedd 20 53 63 61 6e 20 74 68 65 20 68 65 61 64 65 72   Scan the header
1bede 20 61 6e 64 20 75 73 65 20 69 74 20 74 6f 20 66   and use it to f
1bedf 69 6c 6c 20 69 6e 20 74 68 65 20 61 54 79 70 65  ill in the aType
1bee0 5b 5d 20 61 6e 64 20 61 4f 66 66 73 65 74 5b 5d  [] and aOffset[]
1bee1 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 73 2e 20  .    ** arrays. 
1bee2 20 61 54 79 70 65 5b 69 5d 20 77 69 6c 6c 20 63   aType[i] will c
1bee3 6f 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20  ontain the type 
1bee4 69 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20  integer for the 
1bee5 69 2d 74 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75  i-th.    ** colu
1bee6 6d 6e 20 61 6e 64 20 61 4f 66 66 73 65 74 5b 69  mn and aOffset[i
1bee7 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74  ] will contain t
1bee8 68 65 20 6f 66 66 73 65 74 20 66 72 6f 6d 20 74  he offset from t
1bee9 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20  he beginning.   
1beea 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72   ** of the recor
1beeb 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  d to the start o
1beec 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  f the data for t
1beed 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20  he i-th column. 
1beee 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d     */.    for(i=
1beef 30 3b 20 69 3c 6e 46 69 65 6c 64 3b 20 69 2b 2b  0; i<nField; i++
1bef0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 49 64  ){.      if( zId
1bef1 78 3c 7a 45 6e 64 48 64 72 20 29 7b 0a 20 20 20  x<zEndHdr ){.   
1bef2 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69 5d 20       aOffset[i] 
1bef3 3d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  = offset;.      
1bef4 20 20 7a 49 64 78 20 2b 3d 20 67 65 74 56 61 72    zIdx += getVar
1bef5 69 6e 74 33 32 28 7a 49 64 78 2c 20 61 54 79 70  int32(zIdx, aTyp
1bef6 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 6f  e[i]);.        o
1bef7 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33  ffset += sqlite3
1bef8 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1bef9 6e 28 61 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20  n(aType[i]);.   
1befa 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1befb 20 20 2f 2a 20 49 66 20 69 20 69 73 20 6c 65 73    /* If i is les
1befc 73 20 74 68 61 74 20 6e 46 69 65 6c 64 2c 20 74  s that nField, t
1befd 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6c 65  hen there are le
1befe 73 73 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69  ss fields in thi
1beff 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 63  s.        ** rec
1bf00 6f 72 64 20 74 68 61 6e 20 53 65 74 4e 75 6d 43  ord than SetNumC
1bf01 6f 6c 75 6d 6e 73 20 69 6e 64 69 63 61 74 65 64  olumns indicated
1bf02 20 74 68 65 72 65 20 61 72 65 20 63 6f 6c 75 6d   there are colum
1bf03 6e 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20  ns in the.      
1bf04 20 20 2a 2a 20 74 61 62 6c 65 2e 20 53 65 74 20    ** table. Set 
1bf05 74 68 65 20 6f 66 66 73 65 74 20 66 6f 72 20 61  the offset for a
1bf06 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 73  ny extra columns
1bf07 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 0a   not present in.
1bf08 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72          ** the r
1bf09 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 73  ecord to 0. This
1bf0a 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c 6f   tells code belo
1bf0b 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 4c  w to store a NUL
1bf0c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73  L.        ** ins
1bf0d 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61 6c  tead of deserial
1bf0e 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66 72  izing a value fr
1bf0f 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20  om the record.. 
1bf10 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1bf11 20 20 61 4f 66 66 73 65 74 5b 69 5d 20 3d 20 30    aOffset[i] = 0
1bf12 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1bf13 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1bf14 65 6d 52 65 6c 65 61 73 65 28 26 73 4d 65 6d 29  emRelease(&sMem)
1bf15 3b 0a 20 20 20 20 73 4d 65 6d 2e 66 6c 61 67 73  ;.    sMem.flags
1bf16 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20   = MEM_Null;..  
1bf17 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20    /* If we have 
1bf18 72 65 61 64 20 6d 6f 72 65 20 68 65 61 64 65 72  read more header
1bf19 20 64 61 74 61 20 74 68 61 6e 20 77 61 73 20 63   data than was c
1bf1a 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
1bf1b 68 65 61 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f  header,.    ** o
1bf1c 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20  r if the end of 
1bf1d 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61  the last field a
1bf1e 70 70 65 61 72 73 20 74 6f 20 62 65 20 70 61 73  ppears to be pas
1bf1f 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
1bf20 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20  .    ** record, 
1bf21 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66  or if the end of
1bf22 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20   the last field 
1bf23 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 62 65  appears to be be
1bf24 66 6f 72 65 20 74 68 65 20 65 6e 64 0a 20 20 20  fore the end.   
1bf25 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72   ** of the recor
1bf26 64 20 28 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c  d (when all fiel
1bf27 64 73 20 70 72 65 73 65 6e 74 29 2c 20 74 68 65  ds present), the
1bf28 6e 20 77 65 20 6d 75 73 74 20 62 65 20 64 65 61  n we must be dea
1bf29 6c 69 6e 67 20 0a 20 20 20 20 2a 2a 20 77 69 74  ling .    ** wit
1bf2a 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61  h a corrupt data
1bf2b 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  base..    */.   
1bf2c 20 69 66 28 20 7a 49 64 78 3e 7a 45 6e 64 48 64   if( zIdx>zEndHd
1bf2d 72 20 7c 7c 20 6f 66 66 73 65 74 3e 70 61 79 6c  r || offset>payl
1bf2e 6f 61 64 53 69 7a 65 20 0a 20 20 20 20 20 7c 7c  oadSize .     ||
1bf2f 20 28 7a 49 64 78 3d 3d 7a 45 6e 64 48 64 72 20   (zIdx==zEndHdr 
1bf30 26 26 20 6f 66 66 73 65 74 21 3d 70 61 79 6c 6f  && offset!=paylo
1bf31 61 64 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 20  adSize) ){.     
1bf32 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
1bf33 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
1bf34 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f   goto op_column_
1bf35 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  out;.    }.  }..
1bf36 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c    /* Get the col
1bf37 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  umn information.
1bf38 20 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d 20   If aOffset[p2] 
1bf39 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1bf3a 6e 20 0a 20 20 2a 2a 20 64 65 73 65 72 69 61 6c  n .  ** deserial
1bf3b 69 7a 65 20 74 68 65 20 76 61 6c 75 65 20 66 72  ize the value fr
1bf3c 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 20 49  om the record. I
1bf3d 66 20 61 4f 66 66 73 65 74 5b 70 32 5d 20 69 73  f aOffset[p2] is
1bf3e 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e   zero,.  ** then
1bf3f 20 74 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65   there are not e
1bf40 6e 6f 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20  nough fields in 
1bf41 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61  the record to sa
1bf42 74 69 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72  tisfy the.  ** r
1bf43 65 71 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73  equest.  In this
1bf44 20 63 61 73 65 2c 20 73 65 74 20 74 68 65 20 76   case, set the v
1bf45 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20  alue NULL or to 
1bf46 50 34 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a  P4 if P4 is.  **
1bf47 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1bf48 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f  Mem object..  */
1bf49 0a 20 20 69 66 28 20 61 4f 66 66 73 65 74 5b 70  .  if( aOffset[p
1bf4a 32 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  2] ){.    assert
1bf4b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1bf4c 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 65 63 20  );.    if( zRec 
1bf4d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1bf4e 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78  VdbeMemReleaseEx
1bf4f 74 65 72 6e 61 6c 28 70 44 65 73 74 29 3b 0a 20  ternal(pDest);. 
1bf50 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1bf51 53 65 72 69 61 6c 47 65 74 28 28 75 38 20 2a 29  SerialGet((u8 *)
1bf52 26 7a 52 65 63 5b 61 4f 66 66 73 65 74 5b 70 32  &zRec[aOffset[p2
1bf53 5d 5d 2c 20 61 54 79 70 65 5b 70 32 5d 2c 20 70  ]], aType[p2], p
1bf54 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Dest);.    }else
1bf55 7b 0a 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71  {.      len = sq
1bf56 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1bf57 79 70 65 4c 65 6e 28 61 54 79 70 65 5b 70 32 5d  ypeLen(aType[p2]
1bf58 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1bf59 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 73 4d 65  VdbeMemMove(&sMe
1bf5a 6d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 20  m, pDest);.     
1bf5b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1bf5c 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43  eMemFromBtree(pC
1bf5d 72 73 72 2c 20 61 4f 66 66 73 65 74 5b 70 32 5d  rsr, aOffset[p2]
1bf5e 2c 20 6c 65 6e 2c 20 70 43 2d 3e 69 73 49 6e 64  , len, pC->isInd
1bf5f 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 20  ex, &sMem);.    
1bf60 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1bf61 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
1bf62 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75  oto op_column_ou
1bf63 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
1bf64 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a 3b   zData = sMem.z;
1bf65 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1bf66 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a  beSerialGet((u8*
1bf67 29 7a 44 61 74 61 2c 20 61 54 79 70 65 5b 70 32  )zData, aType[p2
1bf68 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 7d  ], pDest);.    }
1bf69 0a 20 20 20 20 70 44 65 73 74 2d 3e 65 6e 63 20  .    pDest->enc 
1bf6a 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 7d 65  = encoding;.  }e
1bf6b 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 4f 70  lse{.    if( pOp
1bf6c 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d 45 4d  ->p4type==P4_MEM
1bf6d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1bf6e 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43  3VdbeMemShallowC
1bf6f 6f 70 79 28 70 44 65 73 74 2c 20 70 4f 70 2d 3e  opy(pDest, pOp->
1bf70 70 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61  p4.pMem, MEM_Sta
1bf71 74 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  tic);.    }else{
1bf72 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1bf73 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  Dest->flags&MEM_
1bf74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 20  Null );.    }.  
1bf75 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 64 79  }..  /* If we dy
1bf76 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
1bf77 74 65 64 20 73 70 61 63 65 20 74 6f 20 68 6f 6c  ted space to hol
1bf78 64 20 74 68 65 20 64 61 74 61 20 28 69 6e 20 74  d the data (in t
1bf79 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56  he.  ** sqlite3V
1bf7a 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1bf7b 29 20 63 61 6c 6c 20 61 62 6f 76 65 29 20 74 68  ) call above) th
1bf7c 65 6e 20 74 72 61 6e 73 66 65 72 20 63 6f 6e 74  en transfer cont
1bf7d 72 6f 6c 20 6f 66 20 74 68 61 74 0a 20 20 2a 2a  rol of that.  **
1bf7e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
1bf7f 6f 63 61 74 65 64 20 73 70 61 63 65 20 6f 76 65  ocated space ove
1bf80 72 20 74 6f 20 74 68 65 20 70 44 65 73 74 20 73  r to the pDest s
1bf81 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54  tructure..  ** T
1bf82 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 6d  his prevents a m
1bf83 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a 2f  emory copy..  */
1bf84 0a 20 20 69 66 28 20 73 4d 65 6d 2e 7a 4d 61 6c  .  if( sMem.zMal
1bf85 6c 6f 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72  loc ){.    asser
1bf86 74 28 20 73 4d 65 6d 2e 7a 3d 3d 73 4d 65 6d 2e  t( sMem.z==sMem.
1bf87 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 61  zMalloc );.    a
1bf88 73 73 65 72 74 28 20 21 28 70 44 65 73 74 2d 3e  ssert( !(pDest->
1bf89 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29  flags & MEM_Dyn)
1bf8a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1bf8b 21 28 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26  !(pDest->flags &
1bf8c 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53   (MEM_Blob|MEM_S
1bf8d 74 72 29 29 20 7c 7c 20 70 44 65 73 74 2d 3e 7a  tr)) || pDest->z
1bf8e 3d 3d 73 4d 65 6d 2e 7a 20 29 3b 0a 20 20 20 20  ==sMem.z );.    
1bf8f 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d 20  pDest->flags &= 
1bf90 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f  ~(MEM_Ephem|MEM_
1bf91 53 74 61 74 69 63 29 3b 0a 20 20 20 20 70 44 65  Static);.    pDe
1bf92 73 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  st->flags |= MEM
1bf93 5f 54 65 72 6d 3b 0a 20 20 20 20 70 44 65 73 74  _Term;.    pDest
1bf94 2d 3e 7a 20 3d 20 73 4d 65 6d 2e 7a 3b 0a 20 20  ->z = sMem.z;.  
1bf95 20 20 70 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f 63    pDest->zMalloc
1bf96 20 3d 20 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 3b   = sMem.zMalloc;
1bf97 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c  .  }..  rc = sql
1bf98 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
1bf99 72 69 74 65 61 62 6c 65 28 70 44 65 73 74 29 3b  riteable(pDest);
1bf9a 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a  ..op_column_out:
1bf9b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
1bf9c 4f 42 53 49 5a 45 28 70 44 65 73 74 29 3b 0a 20  OBSIZE(pDest);. 
1bf9d 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1bf9e 70 4f 70 2d 3e 70 33 2c 20 70 44 65 73 74 29 3b  pOp->p3, pDest);
1bf9f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1bfa0 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74 79  Opcode: Affinity
1bfa1 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a   P1 P2 * P4 *.**
1bfa2 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e 69  .** Apply affini
1bfa3 74 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65 20  ties to a range 
1bfa4 6f 66 20 50 32 20 72 65 67 69 73 74 65 72 73 20  of P2 registers 
1bfa5 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 50 31  starting with P1
1bfa6 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  ..**.** P4 is a 
1bfa7 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50  string that is P
1bfa8 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e  2 characters lon
1bfa9 67 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72 61  g. The nth chara
1bfaa 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73  cter of the.** s
1bfab 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 20  tring indicates 
1bfac 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  the column affin
1bfad 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ity that should 
1bfae 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  be used for the 
1bfaf 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65  nth.** memory ce
1bfb0 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e  ll in the range.
1bfb1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66 69  .*/.case OP_Affi
1bfb2 6e 69 74 79 3a 20 7b 0a 20 20 63 68 61 72 20 2a  nity: {.  char *
1bfb3 7a 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d  zAffinity = pOp-
1bfb4 3e 70 34 2e 7a 3b 0a 20 20 4d 65 6d 20 2a 70 44  >p4.z;.  Mem *pD
1bfb5 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  ata0 = &p->aMem[
1bfb6 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 4d 65 6d 20  pOp->p1];.  Mem 
1bfb7 2a 70 4c 61 73 74 20 3d 20 26 70 44 61 74 61 30  *pLast = &pData0
1bfb8 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a 20 20 4d  [pOp->p2-1];.  M
1bfb9 65 6d 20 2a 70 52 65 63 3b 0a 0a 20 20 66 6f 72  em *pRec;..  for
1bfba 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52  (pRec=pData0; pR
1bfbb 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b  ec<=pLast; pRec+
1bfbc 2b 29 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c  +){.    ExpandBl
1bfbd 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20 61 70  ob(pRec);.    ap
1bfbe 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 63  plyAffinity(pRec
1bfbf 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63  , zAffinity[pRec
1bfc0 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69  -pData0], encodi
1bfc1 6e 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  ng);.  }.  break
1bfc2 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1bfc3 4d 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32  MakeRecord P1 P2
1bfc4 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43   P3 P4 *.**.** C
1bfc5 6f 6e 76 65 72 74 20 50 32 20 72 65 67 69 73 74  onvert P2 regist
1bfc6 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ers beginning wi
1bfc7 74 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69 6e  th P1 into a sin
1bfc8 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75 69  gle entry.** sui
1bfc9 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73  table for use as
1bfca 20 61 20 64 61 74 61 20 72 65 63 6f 72 64 20 69   a data record i
1bfcb 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62  n a database tab
1bfcc 6c 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a 2a  le or as a key.*
1bfcd 2a 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20  * in an index.  
1bfce 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74  The details of t
1bfcf 68 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69 72  he format are ir
1bfd0 72 65 6c 65 76 61 6e 74 20 61 73 20 6c 6f 6e 67  relevant as long
1bfd1 20 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43 6f   as.** the OP_Co
1bfd2 6c 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20  lumn opcode can 
1bfd3 64 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72  decode the recor
1bfd4 64 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66 65  d later..** Refe
1bfd5 72 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64 65  r to source code
1bfd6 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74 68   comments for th
1bfd7 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65  e details of the
1bfd8 20 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61   record.** forma
1bfd9 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20  t..**.** P4 may 
1bfda 62 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  be a string that
1bfdb 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72   is P2 character
1bfdc 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68  s long.  The nth
1bfdd 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68   character of th
1bfde 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69  e.** string indi
1bfdf 63 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e  cates the column
1bfe0 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73   affinity that s
1bfe1 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f  hould be used fo
1bfe2 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65  r the nth.** fie
1bfe3 6c 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ld of the index 
1bfe4 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  key..**.** The m
1bfe5 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72  apping from char
1bfe6 61 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74  acter to affinit
1bfe7 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68  y is given by th
1bfe8 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a  e SQLITE_AFF_.**
1bfe9 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20   macros defined 
1bfea 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a  in sqliteInt.h..
1bfeb 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 4e  **.** If P4 is N
1bfec 55 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64  ULL then all ind
1bfed 65 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74  ex fields have t
1bfee 68 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45  he affinity NONE
1bfef 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61 6b  ..*/.case OP_Mak
1bff0 65 52 65 63 6f 72 64 3a 20 7b 0a 20 20 2f 2a 20  eRecord: {.  /* 
1bff1 41 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65 63  Assuming the rec
1bff2 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20 66  ord contains N f
1bff3 69 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f 72  ields, the recor
1bff4 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20  d format looks. 
1bff5 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20   ** like this:. 
1bff6 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d   **.  ** -------
1bff7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1bff8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1bff9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1bffa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1bffb 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a  -.  ** | hdr-siz
1bffc 65 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79 70  e | type 0 | typ
1bffd 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65  e 1 | ... | type
1bffe 20 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20 2e   N-1 | data0 | .
1bfff 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c 20  .. | data N-1 | 
1c000 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .  ** ----------
1c001 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c002 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c003 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c004 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20  --------------. 
1c005 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30 29   **.  ** Data(0)
1c006 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72   is taken from r
1c007 65 67 69 73 74 65 72 20 50 31 2e 20 20 44 61 74  egister P1.  Dat
1c008 61 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d 20  a(1) comes from 
1c009 72 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20 20  register P1+1.  
1c00a 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68 2e  ** and so froth.
1c00b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20  .  **.  ** Each 
1c00c 74 79 70 65 20 66 69 65 6c 64 20 69 73 20 61 20  type field is a 
1c00d 76 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e 74  varint represent
1c00e 69 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20 74  ing the serial t
1c00f 79 70 65 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a  ype of the .  **
1c010 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64   corresponding d
1c011 61 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65 65  ata element (see
1c012 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1c013 61 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a 20  alType()). The. 
1c014 20 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69 65   ** hdr-size fie
1c015 6c 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61 72  ld is also a var
1c016 69 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65  int which is the
1c017 20 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65   offset from the
1c018 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20   beginning.  ** 
1c019 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  of the record to
1c01a 20 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20 75   data0..  */.  u
1c01b 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b 20 20  8 *zNewRecord;  
1c01c 20 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66 65        /* A buffe
1c01d 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 64 61  r to hold the da
1c01e 74 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72  ta for the new r
1c01f 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a  ecord */.  Mem *
1c020 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20  pRec;           
1c021 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65 63    /* The new rec
1c022 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e 44 61  ord */.  u64 nDa
1c023 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ta = 0;         
1c024 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1c025 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65  es of data space
1c026 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72 20 3d   */.  int nHdr =
1c027 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
1c028 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1c029 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20  of header space 
1c02a 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 20 3d  */.  i64 nByte =
1c02b 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44   0;         /* D
1c02c 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72  ata space requir
1c02d 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f  ed for this reco
1c02e 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72  rd */.  int nZer
1c02f 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  o = 0;         /
1c030 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f  * Number of zero
1c031 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65 6e   bytes at the en
1c032 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  d of the record 
1c033 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69 6e 74  */.  int nVarint
1c034 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1c035 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
1c036 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20  n a varint */.  
1c037 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b  u32 serial_type;
1c038 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 66         /* Type f
1c039 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  ield */.  Mem *p
1c03a 44 61 74 61 30 3b 20 20 20 20 20 20 20 20 20 20  Data0;          
1c03b 20 2f 2a 20 46 69 72 73 74 20 66 69 65 6c 64 20   /* First field 
1c03c 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64 20 69  to be combined i
1c03d 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 2a  nto the record *
1c03e 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20  /.  Mem *pLast; 
1c03f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
1c040 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  st field of the 
1c041 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20  record */.  int 
1c042 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20  nField;         
1c043 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1c044 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65  fields in the re
1c045 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  cord */.  char *
1c046 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20  zAffinity;      
1c047 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
1c048 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20   string for the 
1c049 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20  record */.  int 
1c04a 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20  file_format;    
1c04b 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61     /* File forma
1c04c 74 20 74 6f 20 75 73 65 20 66 6f 72 20 65 6e 63  t to use for enc
1c04d 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69  oding */.  int i
1c04e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1c04f 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64 20    /* Space used 
1c050 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d 20  in zNewRecord[] 
1c051 2a 2f 0a 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70  */..  nField = p
1c052 4f 70 2d 3e 70 31 3b 0a 20 20 7a 41 66 66 69 6e  Op->p1;.  zAffin
1c053 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b  ity = pOp->p4.z;
1c054 0a 20 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c  .  assert( nFiel
1c055 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30  d>0 && pOp->p2>0
1c056 20 26 26 20 70 4f 70 2d 3e 70 32 2b 6e 46 69 65   && pOp->p2+nFie
1c057 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b  ld<=p->nMem+1 );
1c058 0a 20 20 70 44 61 74 61 30 20 3d 20 26 70 2d 3e  .  pData0 = &p->
1c059 61 4d 65 6d 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20  aMem[nField];.  
1c05a 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 32  nField = pOp->p2
1c05b 3b 0a 20 20 70 4c 61 73 74 20 3d 20 26 70 44 61  ;.  pLast = &pDa
1c05c 74 61 30 5b 6e 46 69 65 6c 64 2d 31 5d 3b 0a 20  ta0[nField-1];. 
1c05d 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70   file_format = p
1c05e 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f  ->minWriteFileFo
1c05f 72 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70  rmat;..  /* Loop
1c060 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c 65   through the ele
1c061 6d 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c 20  ments that will 
1c062 6d 61 6b 65 20 75 70 20 74 68 65 20 72 65 63 6f  make up the reco
1c063 72 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20 2a  rd to figure.  *
1c064 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73  * out how much s
1c065 70 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64  pace is required
1c066 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63   for the new rec
1c067 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ord..  */.  for(
1c068 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 65  pRec=pData0; pRe
1c069 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b  c<=pLast; pRec++
1c06a 29 7b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a  ){.    int len;.
1c06b 20 20 20 20 69 66 28 20 7a 41 66 66 69 6e 69 74      if( zAffinit
1c06c 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79  y ){.      apply
1c06d 41 66 66 69 6e 69 74 79 28 70 52 65 63 2c 20 7a  Affinity(pRec, z
1c06e 41 66 66 69 6e 69 74 79 5b 70 52 65 63 2d 70 44  Affinity[pRec-pD
1c06f 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29  ata0], encoding)
1c070 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1c071 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pRec->flags&MEM_
1c072 5a 65 72 6f 20 26 26 20 70 52 65 63 2d 3e 6e 3e  Zero && pRec->n>
1c073 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
1c074 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
1c075 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20 7d  lob(pRec);.    }
1c076 0a 20 20 20 20 73 65 72 69 61 6c 5f 74 79 70 65  .    serial_type
1c077 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1c078 72 69 61 6c 54 79 70 65 28 70 52 65 63 2c 20 66  rialType(pRec, f
1c079 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20  ile_format);.   
1c07a 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64   len = sqlite3Vd
1c07b 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1c07c 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20  serial_type);.  
1c07d 20 20 6e 44 61 74 61 20 2b 3d 20 6c 65 6e 3b 0a    nData += len;.
1c07e 20 20 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c 69      nHdr += sqli
1c07f 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 73 65 72  te3VarintLen(ser
1c080 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69  ial_type);.    i
1c081 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26  f( pRec->flags &
1c082 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
1c083 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20     /* Only pure 
1c084 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42  zero-filled BLOB
1c085 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74  s can be input t
1c086 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20  o this Opcode.. 
1c087 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f       ** We do no
1c088 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69  t allow blobs wi
1c089 74 68 20 61 20 70 72 65 66 69 78 20 61 6e 64 20  th a prefix and 
1c08a 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  a zero-filled ta
1c08b 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 5a 65  il. */.      nZe
1c08c 72 6f 20 2b 3d 20 70 52 65 63 2d 3e 75 2e 6e 5a  ro += pRec->u.nZ
1c08d 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ero;.    }else i
1c08e 66 28 20 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20  f( len ){.      
1c08f 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d  nZero = 0;.    }
1c090 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74  .  }..  /* Add t
1c091 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64 65  he initial heade
1c092 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f 74  r varint and tot
1c093 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20  al the size */. 
1c094 20 6e 48 64 72 20 2b 3d 20 6e 56 61 72 69 6e 74   nHdr += nVarint
1c095 20 3d 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74   = sqlite3Varint
1c096 4c 65 6e 28 6e 48 64 72 29 3b 0a 20 20 69 66 28  Len(nHdr);.  if(
1c097 20 6e 56 61 72 69 6e 74 3c 73 71 6c 69 74 65 33   nVarint<sqlite3
1c098 56 61 72 69 6e 74 4c 65 6e 28 6e 48 64 72 29 20  VarintLen(nHdr) 
1c099 29 7b 0a 20 20 20 20 6e 48 64 72 2b 2b 3b 0a 20  ){.    nHdr++;. 
1c09a 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 6e 48 64   }.  nByte = nHd
1c09b 72 2b 6e 44 61 74 61 2d 6e 5a 65 72 6f 3b 0a 20  r+nData-nZero;. 
1c09c 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61   if( nByte>db->a
1c09d 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1c09e 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1c09f 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1c0a0 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
1c0a1 75 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 72  ure the output r
1c0a2 65 67 69 73 74 65 72 20 68 61 73 20 61 20 62 75  egister has a bu
1c0a3 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67  ffer large enoug
1c0a4 68 20 74 6f 20 73 74 6f 72 65 20 0a 20 20 2a 2a  h to store .  **
1c0a5 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e   the new record.
1c0a6 20 54 68 65 20 6f 75 74 70 75 74 20 72 65 67 69   The output regi
1c0a7 73 74 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69  ster (pOp->p3) i
1c0a8 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f  s not allowed to
1c0a9 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20  .  ** be one of 
1c0aa 74 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74  the input regist
1c0ab 65 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65  ers (because the
1c0ac 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20   following call 
1c0ad 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56  to.  ** sqlite3V
1c0ae 64 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75  dbeMemGrow() cou
1c0af 6c 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76  ld clobber the v
1c0b0 61 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69  alue before it i
1c0b1 73 20 75 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20  s used)..  */.  
1c0b2 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c  assert( pOp->p3<
1c0b3 70 4f 70 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e  pOp->p1 || pOp->
1c0b4 70 33 3e 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d  p3>=pOp->p1+pOp-
1c0b5 3e 70 32 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20  >p2 );.  pOut = 
1c0b6 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1c0b7 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ];.  if( sqlite3
1c0b8 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74  VdbeMemGrow(pOut
1c0b9 2c 20 28 69 6e 74 29 6e 42 79 74 65 2c 20 30 29  , (int)nByte, 0)
1c0ba 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f   ){.    goto no_
1c0bb 6d 65 6d 3b 0a 20 20 7d 0a 20 20 7a 4e 65 77 52  mem;.  }.  zNewR
1c0bc 65 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70 4f  ecord = (u8 *)pO
1c0bd 75 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69  ut->z;..  /* Wri
1c0be 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  te the record */
1c0bf 0a 20 20 69 20 3d 20 70 75 74 56 61 72 69 6e 74  .  i = putVarint
1c0c0 33 32 28 7a 4e 65 77 52 65 63 6f 72 64 2c 20 6e  32(zNewRecord, n
1c0c1 48 64 72 29 3b 0a 20 20 66 6f 72 28 70 52 65 63  Hdr);.  for(pRec
1c0c2 3d 70 44 61 74 61 30 3b 20 70 52 65 63 3c 3d 70  =pData0; pRec<=p
1c0c3 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29 7b 0a 20  Last; pRec++){. 
1c0c4 20 20 20 73 65 72 69 61 6c 5f 74 79 70 65 20 3d     serial_type =
1c0c5 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1c0c6 61 6c 54 79 70 65 28 70 52 65 63 2c 20 66 69 6c  alType(pRec, fil
1c0c7 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 69  e_format);.    i
1c0c8 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 33 32 28   += putVarint32(
1c0c9 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69 5d 2c 20  &zNewRecord[i], 
1c0ca 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 20 20 20  serial_type);   
1c0cb 20 20 20 2f 2a 20 73 65 72 69 61 6c 20 74 79 70     /* serial typ
1c0cc 65 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 70  e */.  }.  for(p
1c0cd 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52 65 63  Rec=pData0; pRec
1c0ce 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b 2b 29  <=pLast; pRec++)
1c0cf 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20 64 61 74  {  /* serial dat
1c0d0 61 20 2a 2f 0a 20 20 20 20 69 20 2b 3d 20 73 71  a */.    i += sq
1c0d1 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50  lite3VdbeSerialP
1c0d2 75 74 28 26 7a 4e 65 77 52 65 63 6f 72 64 5b 69  ut(&zNewRecord[i
1c0d3 5d 2c 20 28 69 6e 74 29 28 6e 42 79 74 65 2d 69  ], (int)(nByte-i
1c0d4 29 2c 20 70 52 65 63 2c 66 69 6c 65 5f 66 6f 72  ), pRec,file_for
1c0d5 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  mat);.  }.  asse
1c0d6 72 74 28 20 69 3d 3d 6e 42 79 74 65 20 29 3b 0a  rt( i==nByte );.
1c0d7 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1c0d8 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
1c0d9 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f  =p->nMem );.  pO
1c0da 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 6e 42 79  ut->n = (int)nBy
1c0db 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67  te;.  pOut->flag
1c0dc 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d  s = MEM_Blob | M
1c0dd 45 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e  EM_Dyn;.  pOut->
1c0de 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20  xDel = 0;.  if( 
1c0df 6e 5a 65 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75  nZero ){.    pOu
1c0e0 74 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 5a 65  t->u.nZero = nZe
1c0e1 72 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c  ro;.    pOut->fl
1c0e2 61 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b  ags |= MEM_Zero;
1c0e3 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63  .  }.  pOut->enc
1c0e4 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20   = SQLITE_UTF8; 
1c0e5 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20   /* In case the 
1c0e6 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f 6e  blob is ever con
1c0e7 76 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 2a  verted to text *
1c0e8 2f 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  /.  REGISTER_TRA
1c0e9 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74  CE(pOp->p3, pOut
1c0ea 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  );.  UPDATE_MAX_
1c0eb 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1c0ec 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c0ed 70 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31 20  pcode: Count P1 
1c0ee 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  P2 * * *.**.** S
1c0ef 74 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20  tore the number 
1c0f0 6f 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20 69  of entries (an i
1c0f1 6e 74 65 67 65 72 20 76 61 6c 75 65 29 20 69 6e  nteger value) in
1c0f2 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
1c0f3 64 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 62  dex .** opened b
1c0f4 79 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20 72  y cursor P1 in r
1c0f5 65 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23 69  egister P2.*/.#i
1c0f6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1c0f7 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61 73  T_BTREECOUNT.cas
1c0f8 65 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20 20  e OP_Count: {   
1c0f9 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1c0fa 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 36  erelease */.  i6
1c0fb 34 20 6e 45 6e 74 72 79 3b 0a 20 20 42 74 43 75  4 nEntry;.  BtCu
1c0fc 72 73 6f 72 20 2a 70 43 72 73 72 20 3d 20 70 2d  rsor *pCrsr = p-
1c0fd 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 2d  >apCsr[pOp->p1]-
1c0fe 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20  >pCursor;.  if( 
1c0ff 70 43 72 73 72 20 29 7b 0a 20 20 20 20 72 63 20  pCrsr ){.    rc 
1c100 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
1c101 75 6e 74 28 70 43 72 73 72 2c 20 26 6e 45 6e 74  unt(pCrsr, &nEnt
1c102 72 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ry);.  }else{.  
1c103 20 20 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20    nEntry = 0;.  
1c104 7d 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20  }.  pOut->flags 
1c105 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75  = MEM_Int;.  pOu
1c106 74 2d 3e 75 2e 69 20 3d 20 6e 45 6e 74 72 79 3b  t->u.i = nEntry;
1c107 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1c108 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  if../* Opcode: S
1c109 74 61 74 65 6d 65 6e 74 20 50 31 20 2a 20 2a 20  tatement P1 * * 
1c10a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20  * *.**.** Begin 
1c10b 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 73 74  an individual st
1c10c 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1c10d 69 6f 6e 20 77 68 69 63 68 20 69 73 20 70 61 72  ion which is par
1c10e 74 20 6f 66 20 61 20 6c 61 72 67 65 72 0a 2a 2a  t of a larger.**
1c10f 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54   transaction.  T
1c110 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f  his is needed so
1c111 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d   that the statem
1c112 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f  ent.** can be ro
1c113 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 20  lled back after 
1c114 61 6e 20 65 72 72 6f 72 20 77 69 74 68 6f 75 74  an error without
1c115 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 20   having to roll 
1c116 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65 6e 74 69  back the.** enti
1c117 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  re transaction. 
1c118 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74   The statement t
1c119 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20  ransaction will 
1c11a 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a  automatically.**
1c11b 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 65   commit when the
1c11c 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a 0a   VDBE halts..**.
1c11d 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  ** If the databa
1c11e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
1c11f 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 75   currently in au
1c120 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 28 74  tocommit mode (t
1c121 68 61 74 20 0a 2a 2a 20 69 73 20 74 6f 20 73 61  hat .** is to sa
1c122 79 2c 20 69 66 20 69 74 20 69 73 20 69 6e 20 62  y, if it is in b
1c123 65 74 77 65 65 6e 20 42 45 47 49 4e 20 61 6e 64  etween BEGIN and
1c124 20 43 4f 4d 4d 49 54 29 0a 2a 2a 20 61 6e 64 20   COMMIT).** and 
1c125 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  if there are no 
1c126 6f 74 68 65 72 20 61 63 74 69 76 65 20 73 74 61  other active sta
1c127 74 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 73  tements on the s
1c128 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ame database.** 
1c129 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 6e  connection, then
1c12a 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
1c12b 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 4e 6f 20  is a no-op.  No 
1c12c 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1c12d 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 65 65 64  ction.** is need
1c12e 65 64 20 73 69 6e 63 65 20 61 6e 79 20 65 72 72  ed since any err
1c12f 6f 72 20 63 61 6e 20 75 73 65 20 74 68 65 20 6e  or can use the n
1c130 6f 72 6d 61 6c 20 52 4f 4c 4c 42 41 43 4b 20 70  ormal ROLLBACK p
1c131 72 6f 63 65 73 73 20 74 6f 0a 2a 2a 20 75 6e 64  rocess to.** und
1c132 6f 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a  o changes..**.**
1c133 20 49 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   If a statement 
1c134 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
1c135 74 61 72 74 65 64 2c 20 74 68 65 6e 20 61 20 73  tarted, then a s
1c136 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
1c137 20 66 69 6c 65 0a 2a 2a 20 77 69 6c 6c 20 62 65   file.** will be
1c138 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69   allocated and i
1c139 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a  nitialized..**.*
1c13a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * The statement 
1c13b 69 73 20 62 65 67 75 6e 20 6f 6e 20 74 68 65 20  is begun on the 
1c13c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69  database file wi
1c13d 74 68 20 69 6e 64 65 78 20 50 31 2e 20 20 54 68  th index P1.  Th
1c13e 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61  e main.** databa
1c13f 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 69  se file has an i
1c140 6e 64 65 78 20 6f 66 20 30 20 61 6e 64 20 74 68  ndex of 0 and th
1c141 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20  e file used for 
1c142 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1c143 0a 2a 2a 20 68 61 73 20 61 6e 20 69 6e 64 65 78  .** has an index
1c144 20 6f 66 20 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f   of 1..*/.case O
1c145 50 5f 53 74 61 74 65 6d 65 6e 74 3a 20 7b 0a 20  P_Statement: {. 
1c146 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d   if( db->autoCom
1c147 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 63  mit==0 || db->ac
1c148 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b  tiveVdbeCnt>1 ){
1c149 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4f 70  .    int i = pOp
1c14a 2d 3e 70 31 3b 0a 20 20 20 20 42 74 72 65 65 20  ->p1;.    Btree 
1c14b 2a 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74  *pBt;.    assert
1c14c 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d 3e  ( i>=0 && i<db->
1c14d 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72  nDb );.    asser
1c14e 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  t( db->aDb[i].pB
1c14f 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 20  t!=0 );.    pBt 
1c150 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1c151 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
1c152 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
1c153 61 6e 73 28 70 42 74 29 20 29 3b 0a 20 20 20 20  ans(pBt) );.    
1c154 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
1c155 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29 21  eMask & (1<<i))!
1c156 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  =0 );.    if( p-
1c157 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29  >iStatement==0 )
1c158 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1c159 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e 3d  db->nStatement>=
1c15a 30 20 26 26 20 64 62 2d 3e 6e 53 61 76 65 70 6f  0 && db->nSavepo
1c15b 69 6e 74 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20  int>=0 );.      
1c15c 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 2b  db->nStatement++
1c15d 3b 20 0a 20 20 20 20 20 20 70 2d 3e 69 53 74 61  ; .      p->iSta
1c15e 74 65 6d 65 6e 74 20 3d 20 64 62 2d 3e 6e 53 61  tement = db->nSa
1c15f 76 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 6e 53  vepoint + db->nS
1c160 74 61 74 65 6d 65 6e 74 3b 0a 20 20 20 20 7d 0a  tatement;.    }.
1c161 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1c162 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 70  BtreeBeginStmt(p
1c163 42 74 2c 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  Bt, p->iStatemen
1c164 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  t);.  }.  break;
1c165 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1c166 61 76 65 70 6f 69 6e 74 20 50 31 20 2a 20 2a 20  avepoint P1 * * 
1c167 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 2c  P4 *.**.** Open,
1c168 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c   release or roll
1c169 62 61 63 6b 20 74 68 65 20 73 61 76 65 70 6f 69  back the savepoi
1c16a 6e 74 20 6e 61 6d 65 64 20 62 79 20 70 61 72 61  nt named by para
1c16b 6d 65 74 65 72 20 50 34 2c 20 64 65 70 65 6e 64  meter P4, depend
1c16c 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 76 61  ing.** on the va
1c16d 6c 75 65 20 6f 66 20 50 31 2e 20 54 6f 20 6f 70  lue of P1. To op
1c16e 65 6e 20 61 20 6e 65 77 20 73 61 76 65 70 6f 69  en a new savepoi
1c16f 6e 74 2c 20 50 31 3d 3d 30 2e 20 54 6f 20 72 65  nt, P1==0. To re
1c170 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61  lease (commit) a
1c171 6e 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 73 61  n.** existing sa
1c172 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 31 2c 20  vepoint, P1==1, 
1c173 6f 72 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61  or to rollback a
1c174 6e 20 65 78 69 73 74 69 6e 67 20 73 61 76 65 70  n existing savep
1c175 6f 69 6e 74 20 50 31 3d 3d 32 2e 0a 2a 2f 0a 63  oint P1==2..*/.c
1c176 61 73 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74  ase OP_Savepoint
1c177 3a 20 7b 0a 20 20 69 6e 74 20 70 31 20 3d 20 70  : {.  int p1 = p
1c178 4f 70 2d 3e 70 31 3b 0a 20 20 63 68 61 72 20 2a  Op->p1;.  char *
1c179 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34 2e  zName = pOp->p4.
1c17a 7a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  z;         /* Na
1c17b 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20  me of savepoint 
1c17c 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20  */..  /* Assert 
1c17d 74 68 61 74 20 74 68 65 20 70 31 20 70 61 72 61  that the p1 para
1c17e 6d 65 74 65 72 20 69 73 20 76 61 6c 69 64 2e 20  meter is valid. 
1c17f 41 6c 73 6f 20 74 68 61 74 20 69 66 20 74 68 65  Also that if the
1c180 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 0a 20 20  re is no open.  
1c181 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  ** transaction, 
1c182 74 68 65 6e 20 74 68 65 72 65 20 63 61 6e 6e 6f  then there canno
1c183 74 20 62 65 20 61 6e 79 20 73 61 76 65 70 6f 69  t be any savepoi
1c184 6e 74 73 2e 20 0a 20 20 2a 2f 0a 20 20 61 73 73  nts. .  */.  ass
1c185 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65 70 6f  ert( db->pSavepo
1c186 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75  int==0 || db->au
1c187 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20  toCommit==0 );. 
1c188 20 61 73 73 65 72 74 28 20 70 31 3d 3d 53 41 56   assert( p1==SAV
1c189 45 50 4f 49 4e 54 5f 42 45 47 49 4e 7c 7c 70 31  EPOINT_BEGIN||p1
1c18a 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  ==SAVEPOINT_RELE
1c18b 41 53 45 7c 7c 70 31 3d 3d 53 41 56 45 50 4f 49  ASE||p1==SAVEPOI
1c18c 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20  NT_ROLLBACK );. 
1c18d 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61   assert( db->pSa
1c18e 76 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d 3e 69  vepoint || db->i
1c18f 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65  sTransactionSave
1c190 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73  point==0 );.  as
1c191 73 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70  sert( checkSavep
1c192 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b  ointCount(db) );
1c193 0a 0a 20 20 69 66 28 20 70 31 3d 3d 53 41 56 45  ..  if( p1==SAVE
1c194 50 4f 49 4e 54 5f 42 45 47 49 4e 20 29 7b 0a 20  POINT_BEGIN ){. 
1c195 20 20 20 69 66 28 20 64 62 2d 3e 77 72 69 74 65     if( db->write
1c196 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20  VdbeCnt>0 ){.   
1c197 20 20 20 2f 2a 20 41 20 6e 65 77 20 73 61 76 65     /* A new save
1c198 70 6f 69 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20  point cannot be 
1c199 63 72 65 61 74 65 64 20 69 66 20 74 68 65 72 65  created if there
1c19a 20 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74   are active writ
1c19b 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74  e .      ** stat
1c19c 65 6d 65 6e 74 73 20 28 69 2e 65 2e 20 6f 70 65  ements (i.e. ope
1c19d 6e 20 72 65 61 64 2f 77 72 69 74 65 20 69 6e 63  n read/write inc
1c19e 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 68 61  remental blob ha
1c19f 6e 64 6c 65 73 29 2e 0a 20 20 20 20 20 20 2a 2f  ndles)..      */
1c1a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
1c1a1 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1c1a2 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74  Msg, db, "cannot
1c1a3 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 20   open savepoint 
1c1a4 2d 20 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c  - ".        "SQL
1c1a5 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70   statements in p
1c1a6 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 20 20 20  rogress");.     
1c1a7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1c1a8 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  Y;.    }else{.  
1c1a9 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20      int nName = 
1c1aa 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1c1ab 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 53 61  zName);.      Sa
1c1ac 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 0a  vepoint *pNew;..
1c1ad 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20        /* Create 
1c1ae 61 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20  a new savepoint 
1c1af 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20  structure. */.  
1c1b0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
1c1b1 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
1c1b2 2c 20 73 69 7a 65 6f 66 28 53 61 76 65 70 6f 69  , sizeof(Savepoi
1c1b3 6e 74 29 2b 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20  nt)+nName+1);.  
1c1b4 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a      if( pNew ){.
1c1b5 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 7a 4e          pNew->zN
1c1b6 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70  ame = (char *)&p
1c1b7 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  New[1];.        
1c1b8 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 4e 61  memcpy(pNew->zNa
1c1b9 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  me, zName, nName
1c1ba 2b 31 29 3b 0a 20 20 20 20 0a 20 20 20 20 20 20  +1);.    .      
1c1bb 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
1c1bc 20 6e 6f 20 6f 70 65 6e 20 74 72 61 6e 73 61 63   no open transac
1c1bd 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 61 72 6b 20  tion, then mark 
1c1be 74 68 69 73 20 61 73 20 61 20 73 70 65 63 69 61  this as a specia
1c1bf 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 74 72  l.        ** "tr
1c1c0 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f  ansaction savepo
1c1c1 69 6e 74 22 2e 20 2a 2f 0a 20 20 20 20 20 20 20  int". */.       
1c1c2 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d   if( db->autoCom
1c1c3 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  mit ){.         
1c1c4 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
1c1c5 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64  = 0;.          d
1c1c6 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  b->isTransaction
1c1c7 53 61 76 65 70 6f 69 6e 74 20 3d 20 31 3b 0a 20  Savepoint = 1;. 
1c1c8 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1c1c9 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76          db->nSav
1c1ca 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20  epoint++;.      
1c1cb 20 20 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 20    }.    .       
1c1cc 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77   /* Link the new
1c1cd 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74 6f 20   savepoint into 
1c1ce 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
1c1cf 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f 0a 20  dle's list. */. 
1c1d0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65         pNew->pNe
1c1d1 78 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f  xt = db->pSavepo
1c1d2 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d  int;.        db-
1c1d3 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 4e  >pSavepoint = pN
1c1d4 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ew;.      }.    
1c1d5 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 53  }.  }else{.    S
1c1d6 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70  avepoint *pSavep
1c1d7 6f 69 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 53  oint;.    int iS
1c1d8 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20  avepoint = 0;.. 
1c1d9 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e     /* Find the n
1c1da 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20  amed savepoint. 
1c1db 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73  If there is no s
1c1dc 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c 20 74  uch savepoint, t
1c1dd 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e  hen an.    ** an
1c1de 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
1c1df 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ed to the user. 
1c1e0 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20 20   */.    for(.   
1c1e1 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 64 62     pSavepoint=db
1c1e2 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 0a 20  ->pSavepoint; . 
1c1e3 20 20 20 20 20 70 53 61 76 65 70 6f 69 6e 74 20       pSavepoint 
1c1e4 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
1c1e5 70 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e  p(pSavepoint->zN
1c1e6 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  ame, zName);.   
1c1e7 20 20 20 70 53 61 76 65 70 6f 69 6e 74 3d 70 53     pSavepoint=pS
1c1e8 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a  avepoint->pNext.
1c1e9 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 53 61      ){.      iSa
1c1ea 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d  vepoint++;.    }
1c1eb 0a 20 20 20 20 69 66 28 20 21 70 53 61 76 65 70  .    if( !pSavep
1c1ec 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  oint ){.      sq
1c1ed 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1c1ee 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1c1ef 22 6e 6f 20 73 75 63 68 20 73 61 76 65 70 6f 69  "no such savepoi
1c1f0 6e 74 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  nt: %s", zName);
1c1f1 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1c1f2 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65  TE_ERROR;.    }e
1c1f3 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20 20 20  lse if( .       
1c1f4 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e   db->writeVdbeCn
1c1f5 74 3e 30 20 7c 7c 20 28 70 31 3d 3d 53 41 56 45  t>0 || (p1==SAVE
1c1f6 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26  POINT_ROLLBACK &
1c1f7 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  & db->activeVdbe
1c1f8 43 6e 74 3e 31 29 20 0a 20 20 20 20 29 7b 0a 20  Cnt>1) .    ){. 
1c1f9 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f       /* It is no
1c1fa 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65  t possible to re
1c1fb 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61  lease (commit) a
1c1fc 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68   savepoint if th
1c1fd 65 72 65 20 61 72 65 20 0a 20 20 20 20 20 20 2a  ere are .      *
1c1fe 2a 20 61 63 74 69 76 65 20 77 72 69 74 65 20 73  * active write s
1c1ff 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73  tatements. It is
1c200 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
1c201 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 65   rollback a save
1c202 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69  point.      ** i
1c203 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
1c204 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74  active statement
1c205 73 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20  s at all..      
1c206 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1c207 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1c208 72 72 4d 73 67 2c 20 64 62 2c 20 0a 20 20 20 20  rrMsg, db, .    
1c209 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73      "cannot %s s
1c20a 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73  avepoint - SQL s
1c20b 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f  tatements in pro
1c20c 67 72 65 73 73 22 2c 0a 20 20 20 20 20 20 20 20  gress",.        
1c20d 28 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  (p1==SAVEPOINT_R
1c20e 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62  OLLBACK ? "rollb
1c20f 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29  ack": "release")
1c210 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
1c211 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
1c212 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20  ;.    }else{..  
1c213 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
1c214 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1c215 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61  this is a transa
1c216 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e  ction savepoint.
1c217 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a   If so,.      **
1c218 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 52   and this is a R
1c219 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20  ELEASE command, 
1c21a 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74  then the current
1c21b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20   transaction .  
1c21c 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74      ** is commit
1c21d 74 65 64 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20  ted. .      */. 
1c21e 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73       int isTrans
1c21f 61 63 74 69 6f 6e 20 3d 20 70 53 61 76 65 70 6f  action = pSavepo
1c220 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26  int->pNext==0 &&
1c221 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69   db->isTransacti
1c222 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20  onSavepoint;.   
1c223 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61 63     if( isTransac
1c224 74 69 6f 6e 20 26 26 20 70 31 3d 3d 53 41 56 45  tion && p1==SAVE
1c225 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b  POINT_RELEASE ){
1c226 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74  .        db->aut
1c227 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20  oCommit = 1;.   
1c228 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1c229 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c  VdbeHalt(p)==SQL
1c22a 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20  ITE_BUSY ){.    
1c22b 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
1c22c 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e  ;.          db->
1c22d 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a  autoCommit = 0;.
1c22e 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20            p->rc 
1c22f 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55  = rc = SQLITE_BU
1c230 53 59 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  SY;.          go
1c231 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1c232 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1c233 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74    db->isTransact
1c234 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30  ionSavepoint = 0
1c235 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  ;.        rc = p
1c236 2d 3e 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ->rc;.      }els
1c237 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  e{.        int i
1c238 69 3b 0a 20 20 20 20 20 20 20 20 69 53 61 76 65  i;.        iSave
1c239 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76  point = db->nSav
1c23a 65 70 6f 69 6e 74 20 2d 20 69 53 61 76 65 70 6f  epoint - iSavepo
1c23b 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  int - 1;.       
1c23c 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 64 62   for(ii=0; ii<db
1c23d 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20  ->nDb; ii++){.  
1c23e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1c23f 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
1c240 6e 74 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70  nt(db->aDb[ii].p
1c241 42 74 2c 20 70 31 2c 20 69 53 61 76 65 70 6f 69  Bt, p1, iSavepoi
1c242 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  nt);.          i
1c243 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1c244 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1c245 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1c246 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1c247 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1c248 20 20 20 20 20 20 20 69 66 28 20 70 31 3d 3d 53         if( p1==S
1c249 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
1c24a 4b 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 26  K && (db->flags&
1c24b 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
1c24c 6e 67 65 73 29 21 3d 30 20 29 7b 0a 20 20 20 20  nges)!=0 ){.    
1c24d 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1c24e 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
1c24f 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 20  ments(db);.     
1c250 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65       sqlite3Rese
1c251 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
1c252 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  db, 0);.        
1c253 7d 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20  }.      }.  .   
1c254 20 20 20 2f 2a 20 52 65 67 61 72 64 6c 65 73 73     /* Regardless
1c255 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 69 73   of whether this
1c256 20 69 73 20 61 20 52 45 4c 45 41 53 45 20 6f 72   is a RELEASE or
1c257 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65 73 74 72   ROLLBACK, destr
1c258 6f 79 20 61 6c 6c 20 0a 20 20 20 20 20 20 2a 2a  oy all .      **
1c259 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65 73 74   savepoints nest
1c25a 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65  ed inside of the
1c25b 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67   savepoint being
1c25c 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20 2a 2f   operated on. */
1c25d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 64 62  .      while( db
1c25e 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d 70 53  ->pSavepoint!=pS
1c25f 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20  avepoint ){.    
1c260 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70      Savepoint *p
1c261 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 70  Tmp = db->pSavep
1c262 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62  oint;.        db
1c263 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70  ->pSavepoint = p
1c264 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Tmp->pNext;.    
1c265 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1c266 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20 20  e(db, pTmp);.   
1c267 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f       db->nSavepo
1c268 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a  int--;.      }..
1c269 20 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69        /* If it i
1c26a 73 20 61 20 52 45 4c 45 41 53 45 2c 20 74 68 65  s a RELEASE, the
1c26b 6e 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 61  n destroy the sa
1c26c 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70  vepoint being op
1c26d 65 72 61 74 65 64 20 6f 6e 20 74 6f 6f 20 2a 2f  erated on too */
1c26e 0a 20 20 20 20 20 20 69 66 28 20 70 31 3d 3d 53  .      if( p1==S
1c26f 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
1c270 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1c271 72 74 28 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d  rt( pSavepoint==
1c272 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29  db->pSavepoint )
1c273 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53  ;.        db->pS
1c274 61 76 65 70 6f 69 6e 74 20 3d 20 70 53 61 76 65  avepoint = pSave
1c275 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  point->pNext;.  
1c276 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1c277 72 65 65 28 64 62 2c 20 70 53 61 76 65 70 6f 69  ree(db, pSavepoi
1c278 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  nt);.        if(
1c279 20 21 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20   !isTransaction 
1c27a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  ){.          db-
1c27b 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20  >nSavepoint--;. 
1c27c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1c27d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72  .    }.  }..  br
1c27e 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1c27f 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31  e: AutoCommit P1
1c280 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1c281 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  Set the database
1c282 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61   auto-commit fla
1c283 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29  g to P1 (1 or 0)
1c284 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c  . If P2 is true,
1c285 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e   roll.** back an
1c286 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69  y currently acti
1c287 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63  ve btree transac
1c288 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20  tions. If there 
1c289 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a  are any active.*
1c28a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f  * VMs (apart fro
1c28b 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65  m this one), the
1c28c 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69  n a ROLLBACK fai
1c28d 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61  ls.  A COMMIT fa
1c28e 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20  ils if.** there 
1c28f 61 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69  are active writi
1c290 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65  ng VMs or active
1c291 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68   VMs that use sh
1c292 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a  ared cache..**.*
1c293 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
1c294 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d  on causes the VM
1c295 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73   to halt..*/.cas
1c296 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a  e OP_AutoCommit:
1c297 20 7b 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64   {.  int desired
1c298 41 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70  AutoCommit = pOp
1c299 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 72 6f 6c 6c  ->p1;.  int roll
1c29a 62 61 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  back = pOp->p2;.
1c29b 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 20 3d    int turnOnAC =
1c29c 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d   desiredAutoComm
1c29d 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43  it && !db->autoC
1c29e 6f 6d 6d 69 74 3b 0a 0a 20 20 61 73 73 65 72 74  ommit;..  assert
1c29f 28 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  ( desiredAutoCom
1c2a0 6d 69 74 3d 3d 31 20 7c 7c 20 64 65 73 69 72 65  mit==1 || desire
1c2a1 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29  dAutoCommit==0 )
1c2a2 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 69  ;.  assert( desi
1c2a3 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31  redAutoCommit==1
1c2a4 20 7c 7c 20 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20   || rollback==0 
1c2a5 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  );..  assert( db
1c2a6 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
1c2a7 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73  0 );  /* At leas
1c2a8 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73  t this one VM is
1c2a9 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66   active */..  if
1c2aa 28 20 74 75 72 6e 4f 6e 41 43 20 26 26 20 72 6f  ( turnOnAC && ro
1c2ab 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63  llback && db->ac
1c2ac 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b  tiveVdbeCnt>1 ){
1c2ad 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
1c2ae 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c  instruction impl
1c2af 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43  ements a ROLLBAC
1c2b0 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20  K and other VMs 
1c2b1 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c  are.    ** still
1c2b2 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20   running, and a 
1c2b3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
1c2b4 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e  ctive, return an
1c2b5 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e   error indicatin
1c2b6 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68  g.    ** that th
1c2b7 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74  e other VMs must
1c2b8 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e   complete first.
1c2b9 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c   .    */.    sql
1c2ba 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1c2bb 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1c2bc 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  cannot rollback 
1c2bd 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a  transaction - ".
1c2be 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61          "SQL sta
1c2bf 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72  tements in progr
1c2c0 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20  ess");.    rc = 
1c2c1 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
1c2c2 65 6c 73 65 20 69 66 28 20 74 75 72 6e 4f 6e 41  else if( turnOnA
1c2c3 43 20 26 26 20 21 72 6f 6c 6c 62 61 63 6b 20 26  C && !rollback &
1c2c4 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  & db->writeVdbeC
1c2c5 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49  nt>1 ){.    /* I
1c2c6 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  f this instructi
1c2c7 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  on implements a 
1c2c8 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 74 68 65 72  COMMIT and other
1c2c9 20 56 4d 73 20 61 72 65 20 77 72 69 74 69 6e 67   VMs are writing
1c2ca 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61  .    ** return a
1c2cb 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69  n error indicati
1c2cc 6e 67 20 74 68 61 74 20 74 68 65 20 6f 74 68 65  ng that the othe
1c2cd 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c  r VMs must compl
1c2ce 65 74 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20  ete first. .    
1c2cf 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  */.    sqlite3Se
1c2d0 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1c2d1 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74  Msg, db, "cannot
1c2d2 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74   commit transact
1c2d3 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20  ion - ".        
1c2d4 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20  "SQL statements 
1c2d5 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20  in progress");. 
1c2d6 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
1c2d7 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  USY;.  }else if(
1c2d8 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d   desiredAutoComm
1c2d9 69 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  it!=db->autoComm
1c2da 69 74 20 29 7b 0a 20 20 20 20 69 66 28 20 72 6f  it ){.    if( ro
1c2db 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20  llback ){.      
1c2dc 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 41  assert( desiredA
1c2dd 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a  utoCommit==1 );.
1c2de 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c        sqlite3Rol
1c2df 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20  lbackAll(db);.  
1c2e0 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1c2e1 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  it = 1;.    }els
1c2e2 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74  e{.      db->aut
1c2e3 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 64 65  oCommit = (u8)de
1c2e4 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b  siredAutoCommit;
1c2e5 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1c2e6 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53  e3VdbeHalt(p)==S
1c2e7 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
1c2e8 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63        p->pc = pc
1c2e9 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75  ;.        db->au
1c2ea 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28  toCommit = (u8)(
1c2eb 31 2d 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  1-desiredAutoCom
1c2ec 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  mit);.        p-
1c2ed 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54  >rc = rc = SQLIT
1c2ee 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20  E_BUSY;.        
1c2ef 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1c2f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1c2f1 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1c2f2 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 3b  nStatement==0 );
1c2f3 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73  .    sqlite3Clos
1c2f4 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b  eSavepoints(db);
1c2f5 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d  .    if( p->rc==
1c2f6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1c2f7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
1c2f8 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ONE;.    }else{.
1c2f9 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1c2fa 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
1c2fb 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
1c2fc 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  urn;.  }else{.  
1c2fd 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1c2fe 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1c2ff 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21 64 65  db,.        (!de
1c300 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 29  siredAutoCommit)
1c301 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 74 20 61  ?"cannot start a
1c302 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 74   transaction wit
1c303 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  hin a transactio
1c304 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 28 72 6f  n":(.        (ro
1c305 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20  llback)?"cannot 
1c306 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72  rollback - no tr
1c307 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
1c308 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20  ive":.          
1c309 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74           "cannot
1c30a 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61   commit - no tra
1c30b 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
1c30c 76 65 22 29 29 3b 0a 20 20 20 20 20 20 20 20 20  ve"));.         
1c30d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1c30e 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 62 72  _ERROR;.  }.  br
1c30f 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1c310 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 50  e: Transaction P
1c311 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1c312 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63   Begin a transac
1c313 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 6e 73  tion.  The trans
1c314 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 65 6e  action ends when
1c315 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 6f 6c   a Commit or Rol
1c316 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64 65 20  lback.** opcode 
1c317 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20  is encountered. 
1c318 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   Depending on th
1c319 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 73 65  e ON CONFLICT se
1c31a 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20 74 72  tting, the.** tr
1c31b 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20  ansaction might 
1c31c 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  also be rolled b
1c31d 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ack if an error 
1c31e 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  is encountered..
1c31f 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
1c320 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
1c321 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 77 68  abase file on wh
1c322 69 63 68 20 74 68 65 20 74 72 61 6e 73 61 63 74  ich the transact
1c323 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72 74 65  ion is.** starte
1c324 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73 20 74  d.  Index 0 is t
1c325 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1c326 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65 78 20   file and index 
1c327 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65  1 is the.** file
1c328 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72   used for tempor
1c329 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49 6e 64  ary tables.  Ind
1c32a 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d 6f 72  ices of 2 or mor
1c32b 65 20 61 72 65 20 75 73 65 64 20 66 6f 72 0a 2a  e are used for.*
1c32c 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  * attached datab
1c32d 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  ases..**.** If P
1c32e 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  2 is non-zero, t
1c32f 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e  hen a write-tran
1c330 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
1c331 65 64 2e 20 20 41 20 52 45 53 45 52 56 45 44 20  ed.  A RESERVED 
1c332 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69  lock is.** obtai
1c333 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ned on the datab
1c334 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 61 20  ase file when a 
1c335 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1c336 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 4e  n is started.  N
1c337 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65  o.** other proce
1c338 73 73 20 63 61 6e 20 73 74 61 72 74 20 61 6e 6f  ss can start ano
1c339 74 68 65 72 20 77 72 69 74 65 20 74 72 61 6e 73  ther write trans
1c33a 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69  action while thi
1c33b 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  s transaction is
1c33c 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20 20 53  .** underway.  S
1c33d 74 61 72 74 69 6e 67 20 61 20 77 72 69 74 65 20  tarting a write 
1c33e 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 73 6f  transaction also
1c33f 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c 6c 62   creates a rollb
1c340 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 0a 2a  ack journal. A.*
1c341 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  * write transact
1c342 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72  ion must be star
1c343 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63  ted before any c
1c344 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20 6d 61  hanges can be ma
1c345 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74  de to the.** dat
1c346 61 62 61 73 65 2e 20 20 49 66 20 50 32 20 69 73  abase.  If P2 is
1c347 20 32 20 6f 72 20 67 72 65 61 74 65 72 20 74 68   2 or greater th
1c348 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  en an EXCLUSIVE 
1c349 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f 62 74  lock is also obt
1c34a 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  ained.** on the 
1c34b 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  file..**.** If P
1c34c 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  2 is zero, then 
1c34d 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f  a read-lock is o
1c34e 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64  btained on the d
1c34f 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f  atabase file..*/
1c350 0a 63 61 73 65 20 4f 50 5f 54 72 61 6e 73 61 63  .case OP_Transac
1c351 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74 20 69 20  tion: {.  int i 
1c352 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 42 74 72  = pOp->p1;.  Btr
1c353 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73 73 65  ee *pBt;..  asse
1c354 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62  rt( i>=0 && i<db
1c355 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1c356 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1c357 20 26 20 28 31 3c 3c 69 29 29 21 3d 30 20 29 3b   & (1<<i))!=0 );
1c358 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  .  pBt = db->aDb
1c359 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20  [i].pBt;..  if( 
1c35a 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  pBt ){.    rc = 
1c35b 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
1c35c 6e 54 72 61 6e 73 28 70 42 74 2c 20 70 4f 70 2d  nTrans(pBt, pOp-
1c35d 3e 70 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63  >p2);.    if( rc
1c35e 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b  ==SQLITE_BUSY ){
1c35f 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70  .      p->pc = p
1c360 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d  c;.      p->rc =
1c361 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1c362 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64  Y;.      goto vd
1c363 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d  be_return;.    }
1c364 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1c365 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
1c366 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 2f 2a  LITE_READONLY /*
1c367 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42   && rc!=SQLITE_B
1c368 55 53 59 20 2a 2f 20 29 7b 0a 20 20 20 20 20 20  USY */ ){.      
1c369 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1c36a 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  o_error;.    }. 
1c36b 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1c36c 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 6f  * Opcode: ReadCo
1c36d 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20  okie P1 P2 P3 * 
1c36e 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f  *.**.** Read coo
1c36f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72  kie number P3 fr
1c370 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61  om database P1 a
1c371 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f  nd write it into
1c372 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a   register P2..**
1c373 20 50 33 3d 3d 30 20 69 73 20 74 68 65 20 73 63   P3==0 is the sc
1c374 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50  hema version.  P
1c375 33 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61  3==1 is the data
1c376 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20  base format..** 
1c377 50 33 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63  P3==2 is the rec
1c378 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63  ommended pager c
1c379 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73  ache size, and s
1c37a 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20  o forth.  P1==0 
1c37b 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  is.** the main d
1c37c 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1c37d 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P1==1 is the da
1c37e 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64  tabase file used
1c37f 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d   to store.** tem
1c380 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
1c381 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 65  *.** If P1 is ne
1c382 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 69  gative, then thi
1c383 73 20 69 73 20 61 20 72 65 71 75 65 73 74 20 74  s is a request t
1c384 6f 20 72 65 61 64 20 74 68 65 20 73 69 7a 65 20  o read the size 
1c385 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65  of a.** database
1c386 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 50 33 20  s free-list. P3 
1c387 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 31  must be set to 1
1c388 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 54   in this case. T
1c389 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 64 61 74  he actual.** dat
1c38a 61 62 61 73 65 20 61 63 63 65 73 73 65 64 20 69  abase accessed i
1c38b 73 20 28 28 50 31 2b 31 29 2a 2d 31 29 2e 20 46  s ((P1+1)*-1). F
1c38c 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 20 50 31  or example, a P1
1c38d 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 2d 31   parameter of -1
1c38e 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  .** corresponds 
1c38f 74 6f 20 64 61 74 61 62 61 73 65 20 30 20 28 22  to database 0 ("
1c390 6d 61 69 6e 22 29 2c 20 61 20 50 31 20 6f 66 20  main"), a P1 of 
1c391 2d 32 20 69 73 20 64 61 74 61 62 61 73 65 20 31  -2 is database 1
1c392 20 28 22 74 65 6d 70 22 29 2e 0a 2a 2a 0a 2a 2a   ("temp")..**.**
1c393 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
1c394 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
1c395 65 20 64 61 74 61 62 61 73 65 20 28 65 69 74 68  e database (eith
1c396 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  er a transaction
1c397 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 61 72  .** must be star
1c398 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d 75 73  ted or there mus
1c399 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72  t be an open cur
1c39a 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a 20 65  sor) before.** e
1c39b 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e  xecuting this in
1c39c 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
1c39d 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65  se OP_ReadCookie
1c39e 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1c39f 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1c3a0 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d  ease */.  int iM
1c3a1 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d  eta;.  int iDb =
1c3a2 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20   pOp->p1;.  int 
1c3a3 69 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70  iCookie = pOp->p
1c3a4 33 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  3;..  assert( pO
1c3a5 70 2d 3e 70 33 3c 53 51 4c 49 54 45 5f 4e 5f 42  p->p3<SQLITE_N_B
1c3a6 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 69  TREE_META );.  i
1c3a7 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20  f( iDb<0 ){.    
1c3a8 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62 2b 31  iDb = (-1*(iDb+1
1c3a9 29 29 3b 0a 20 20 20 20 69 43 6f 6f 6b 69 65 20  ));.    iCookie 
1c3aa 2a 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 61 73 73  *= -1;.  }.  ass
1c3ab 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
1c3ac 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
1c3ad 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
1c3ae 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20  iDb].pBt!=0 );. 
1c3af 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1c3b0 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62  eeMask & (1<<iDb
1c3b1 29 29 21 3d 30 20 29 3b 0a 20 20 2f 2a 20 54 68  ))!=0 );.  /* Th
1c3b2 65 20 69 6e 64 65 78 69 6e 67 20 6f 66 20 6d 65  e indexing of me
1c3b3 74 61 20 76 61 6c 75 65 73 20 61 74 20 74 68 65  ta values at the
1c3b4 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20 69 73   schema layer is
1c3b5 20 6f 66 66 20 62 79 20 6f 6e 65 20 66 72 6f 6d   off by one from
1c3b6 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 69  .  ** the indexi
1c3b7 6e 67 20 69 6e 20 74 68 65 20 62 74 72 65 65 20  ng in the btree 
1c3b8 6c 61 79 65 72 2e 20 20 54 68 65 20 62 74 72 65  layer.  The btre
1c3b9 65 20 63 6f 6e 73 69 64 65 72 73 20 6d 65 74 61  e considers meta
1c3ba 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20 62 65 20 74  [0] to.  ** be t
1c3bb 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65  he number of fre
1c3bc 65 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  e pages in the d
1c3bd 61 74 61 62 61 73 65 20 28 61 20 72 65 61 64 2d  atabase (a read-
1c3be 6f 6e 6c 79 20 76 61 6c 75 65 29 0a 20 20 2a 2a  only value).  **
1c3bf 20 61 6e 64 20 6d 65 74 61 5b 31 5d 20 74 6f 20   and meta[1] to 
1c3c0 62 65 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  be the schema co
1c3c1 6f 6b 69 65 2e 20 20 54 68 65 20 73 63 68 65 6d  okie.  The schem
1c3c2 61 20 6c 61 79 65 72 20 63 6f 6e 73 69 64 65 72  a layer consider
1c3c3 73 0a 20 20 2a 2a 20 6d 65 74 61 5b 31 5d 20 74  s.  ** meta[1] t
1c3c4 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d 61 20  o be the schema 
1c3c5 63 6f 6f 6b 69 65 2e 20 20 53 6f 20 77 65 20 68  cookie.  So we h
1c3c6 61 76 65 20 74 6f 20 73 68 69 66 74 20 74 68 65  ave to shift the
1c3c7 20 69 6e 64 65 78 0a 20 20 2a 2a 20 62 79 20 6f   index.  ** by o
1c3c8 6e 65 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  ne in the follow
1c3c9 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20  ing statement.. 
1c3ca 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
1c3cb 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 64  e3BtreeGetMeta(d
1c3cc 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 2c  b->aDb[iDb].pBt,
1c3cd 20 31 20 2b 20 69 43 6f 6f 6b 69 65 2c 20 28 75   1 + iCookie, (u
1c3ce 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20 20  32 *)&iMeta);.  
1c3cf 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 69 4d 65 74  pOut->u.i = iMet
1c3d0 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46  a;.  MemSetTypeF
1c3d1 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1c3d2 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1c3d3 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f  /* Opcode: SetCo
1c3d4 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20  okie P1 P2 P3 * 
1c3d5 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  *.**.** Write th
1c3d6 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67  e content of reg
1c3d7 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 72 70  ister P3 (interp
1c3d8 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65  reted as an inte
1c3d9 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f  ger).** into coo
1c3da 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66  kie number P2 of
1c3db 20 64 61 74 61 62 61 73 65 20 50 31 2e 0a 2a 2a   database P1..**
1c3dc 20 50 32 3d 3d 30 20 69 73 20 74 68 65 20 73 63   P2==0 is the sc
1c3dd 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50  hema version.  P
1c3de 32 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61  2==1 is the data
1c3df 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20  base format..** 
1c3e0 50 32 3d 3d 32 20 69 73 20 74 68 65 20 72 65 63  P2==2 is the rec
1c3e1 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63  ommended pager c
1c3e2 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73  ache size, and s
1c3e3 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20  o forth.  P1==0 
1c3e4 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64  is.** the main d
1c3e5 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1c3e6 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P1==1 is the da
1c3e7 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64  tabase file used
1c3e8 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d   to store.** tem
1c3e9 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
1c3ea 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69  *.** A transacti
1c3eb 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74  on must be start
1c3ec 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74  ed before execut
1c3ed 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
1c3ee 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43  .*/.case OP_SetC
1c3ef 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f  ookie: {       /
1c3f0 2a 20 69 6e 33 20 2a 2f 0a 20 20 44 62 20 2a 70  * in3 */.  Db *p
1c3f1 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  Db;.  assert( pO
1c3f2 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42  p->p2<SQLITE_N_B
1c3f3 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61  TREE_META );.  a
1c3f4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1c3f5 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d  0 && pOp->p1<db-
1c3f6 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1c3f7 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1c3f8 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21  & (1<<pOp->p1))!
1c3f9 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  =0 );.  pDb = &d
1c3fa 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b  b->aDb[pOp->p1];
1c3fb 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 2d 3e  .  assert( pDb->
1c3fc 70 42 74 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69  pBt!=0 );.  sqli
1c3fd 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
1c3fe 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a  rify(pIn3);.  /*
1c3ff 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20   See note about 
1c400 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f  index shifting o
1c401 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20  n OP_ReadCookie 
1c402 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
1c403 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
1c404 28 70 44 62 2d 3e 70 42 74 2c 20 31 2b 70 4f 70  (pDb->pBt, 1+pOp
1c405 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d  ->p2, (int)pIn3-
1c406 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70  >u.i);.  if( pOp
1c407 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  ->p2==0 ){.    /
1c408 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68 65 6d  * When the schem
1c409 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73  a cookie changes
1c40a 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77  , record the new
1c40b 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e 61 6c   cookie internal
1c40c 6c 79 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70  ly */.    pDb->p
1c40d 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
1c40e 6f 6f 6b 69 65 20 3d 20 28 69 6e 74 29 70 49 6e  ookie = (int)pIn
1c40f 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e  3->u.i;.    db->
1c410 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
1c411 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
1c412 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
1c413 70 32 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  p2==1 ){.    /* 
1c414 52 65 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69  Record changes i
1c415 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61  n the file forma
1c416 74 20 2a 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53  t */.    pDb->pS
1c417 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
1c418 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75  at = (u8)pIn3->u
1c419 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f  .i;.  }.  if( pO
1c41a 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20  p->p1==1 ){.    
1c41b 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c  /* Invalidate al
1c41c 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  l prepared state
1c41d 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74  ments whenever t
1c41e 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
1c41f 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69  .    ** schema i
1c420 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b  s changed.  Tick
1c421 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20  et #1644 */.    
1c422 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
1c423 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
1c424 64 62 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  db);.  }.  break
1c425 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1c426 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20  VerifyCookie P1 
1c427 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b  P2 *.**.** Check
1c428 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c   the value of gl
1c429 6f 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 61  obal database pa
1c42a 72 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30  rameter number 0
1c42b 20 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20   (the.** schema 
1c42c 76 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b  version) and mak
1c42d 65 20 73 75 72 65 20 69 74 20 69 73 20 65 71 75  e sure it is equ
1c42e 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50  al to P2.  .** P
1c42f 31 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  1 is the databas
1c430 65 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69  e number which i
1c431 73 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  s 0 for the main
1c432 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
1c433 2a 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20  * and 1 for the 
1c434 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d  file holding tem
1c435 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e  porary tables an
1c436 64 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75  d some higher nu
1c437 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69  mber.** for auxi
1c438 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e  liary databases.
1c439 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69  .**.** The cooki
1c43a 65 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 61  e changes its va
1c43b 6c 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 65  lue whenever the
1c43c 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1c43d 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69   changes..** Thi
1c43e 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75  s operation is u
1c43f 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 68  sed to detect wh
1c440 65 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b  en that the cook
1c441 69 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a  ie has changed.*
1c442 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63  * and that the c
1c443 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e  urrent process n
1c444 65 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 74  eeds to reread t
1c445 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a  he schema..**.**
1c446 20 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 61   Either a transa
1c447 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68  ction needs to h
1c448 61 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 64  ave been started
1c449 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e   or an OP_Open n
1c44a 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78  eeds.** to be ex
1c44b 65 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 62  ecuted (to estab
1c44c 6c 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b  lish a read lock
1c44d 29 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70  ) before this op
1c44e 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b  code is.** invok
1c44f 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  ed..*/.case OP_V
1c450 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 20  erifyCookie: {. 
1c451 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 74   int iMeta;.  Bt
1c452 72 65 65 20 2a 70 42 74 3b 0a 20 20 61 73 73 65  ree *pBt;.  asse
1c453 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1c454 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
1c455 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1c456 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1c457 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20  1<<pOp->p1))!=0 
1c458 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61  );.  pBt = db->a
1c459 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b  Db[pOp->p1].pBt;
1c45a 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20  .  if( pBt ){.  
1c45b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1c45c 72 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c 20  reeGetMeta(pBt, 
1c45d 31 2c 20 28 75 33 32 20 2a 29 26 69 4d 65 74 61  1, (u32 *)&iMeta
1c45e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1c45f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1c460 20 20 20 20 69 4d 65 74 61 20 3d 20 30 3b 0a 20      iMeta = 0;. 
1c461 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
1c462 49 54 45 5f 4f 4b 20 26 26 20 69 4d 65 74 61 21  ITE_OK && iMeta!
1c463 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20  =pOp->p2 ){.    
1c464 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1c465 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1c466 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
1c467 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1c468 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 73 63  db, "database sc
1c469 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64  hema has changed
1c46a 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  ");.    /* If th
1c46b 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20  e schema-cookie 
1c46c 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1c46d 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74  e file matches t
1c46e 68 65 20 63 6f 6f 6b 69 65 20 0a 20 20 20 20 2a  he cookie .    *
1c46f 2a 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68  * stored with th
1c470 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72  e in-memory repr
1c471 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
1c472 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20  e schema, do.   
1c473 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74   ** not reload t
1c474 68 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74  he schema from t
1c475 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1c476 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1c477 49 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  If virtual-table
1c478 73 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68  s are in use, th
1c479 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61  is is not just a
1c47a 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a  n optimization..
1c47b 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d      ** Often, v-
1c47c 74 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65  tables store the
1c47d 69 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72  ir data in other
1c47e 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20   SQLite tables, 
1c47f 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65  which.    ** are
1c480 20 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69   queried from wi
1c481 74 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64  thin xNext() and
1c482 20 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d   other v-table m
1c483 65 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20  ethods using.   
1c484 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65   ** prepared que
1c485 72 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20  ries. If such a 
1c486 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d  query is out-of-
1c487 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20  date, we do not 
1c488 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64  want to.    ** d
1c489 69 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62  iscard the datab
1c48a 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74  ase schema, as t
1c48b 68 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70  he user code imp
1c48c 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20  lementing the.  
1c48d 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75    ** v-table wou
1c48e 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65  ld have to be re
1c48f 61 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ady for the sqli
1c490 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
1c491 72 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a  re itself.    **
1c492 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74   to be invalidat
1c493 65 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69  ed whenever sqli
1c494 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61  te3_step() is ca
1c495 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
1c496 20 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62   .    ** a v-tab
1c497 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a  le method..    *
1c498 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44  /.    if( db->aD
1c499 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65  b[pOp->p1].pSche
1c49a 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
1c49b 65 21 3d 69 4d 65 74 61 20 29 7b 0a 20 20 20 20  e!=iMeta ){.    
1c49c 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
1c49d 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
1c49e 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d   pOp->p1);.    }
1c49f 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ..    sqlite3Exp
1c4a0 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
1c4a1 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 72  ments(db);.    r
1c4a2 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  c = SQLITE_SCHEM
1c4a3 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  A;.  }.  break;.
1c4a4 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  }../* Opcode: Op
1c4a5 65 6e 52 65 61 64 20 50 31 20 50 32 20 50 33 20  enRead P1 P2 P3 
1c4a6 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e  P4 P5.**.** Open
1c4a7 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72   a read-only cur
1c4a8 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61  sor for the data
1c4a9 62 61 73 65 20 74 61 62 6c 65 20 77 68 6f 73 65  base table whose
1c4aa 20 72 6f 6f 74 20 70 61 67 65 20 69 73 0a 2a 2a   root page is.**
1c4ab 20 50 32 20 69 6e 20 61 20 64 61 74 61 62 61 73   P2 in a databas
1c4ac 65 20 66 69 6c 65 2e 20 20 54 68 65 20 64 61 74  e file.  The dat
1c4ad 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 64 65  abase file is de
1c4ae 74 65 72 6d 69 6e 65 64 20 62 79 20 50 33 2e 20  termined by P3. 
1c4af 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61 6e 73 20  .** P3==0 means 
1c4b0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1c4b1 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e 73 20 74  e, P3==1 means t
1c4b2 68 65 20 64 61 74 61 62 61 73 65 20 75 73 65 64  he database used
1c4b3 20 66 6f 72 20 0a 2a 2a 20 74 65 6d 70 6f 72 61   for .** tempora
1c4b4 72 79 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 50  ry tables, and P
1c4b5 33 3e 31 20 6d 65 61 6e 73 20 75 73 65 64 20 74  3>1 means used t
1c4b6 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
1c4b7 20 61 74 74 61 63 68 65 64 0a 2a 2a 20 64 61 74   attached.** dat
1c4b8 61 62 61 73 65 2e 20 20 47 69 76 65 20 74 68 65  abase.  Give the
1c4b9 20 6e 65 77 20 63 75 72 73 6f 72 20 61 6e 20 69   new cursor an i
1c4ba 64 65 6e 74 69 66 69 65 72 20 6f 66 20 50 31 2e  dentifier of P1.
1c4bb 20 20 54 68 65 20 50 31 0a 2a 2a 20 76 61 6c 75    The P1.** valu
1c4bc 65 73 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 63  es need not be c
1c4bd 6f 6e 74 69 67 75 6f 75 73 20 62 75 74 20 61 6c  ontiguous but al
1c4be 6c 20 50 31 20 76 61 6c 75 65 73 20 73 68 6f 75  l P1 values shou
1c4bf 6c 64 20 62 65 20 73 6d 61 6c 6c 20 69 6e 74 65  ld be small inte
1c4c0 67 65 72 73 2e 0a 2a 2a 20 49 74 20 69 73 20 61  gers..** It is a
1c4c1 6e 20 65 72 72 6f 72 20 66 6f 72 20 50 31 20 74  n error for P1 t
1c4c2 6f 20 62 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a  o be negative..*
1c4c3 2a 0a 2a 2a 20 49 66 20 50 35 21 3d 30 20 74 68  *.** If P5!=0 th
1c4c4 65 6e 20 75 73 65 20 74 68 65 20 63 6f 6e 74 65  en use the conte
1c4c5 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  nt of register P
1c4c6 32 20 61 73 20 74 68 65 20 72 6f 6f 74 20 70 61  2 as the root pa
1c4c7 67 65 2c 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 76  ge, not.** the v
1c4c8 61 6c 75 65 20 6f 66 20 50 32 20 69 74 73 65 6c  alue of P2 itsel
1c4c9 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 77  f..**.** There w
1c4ca 69 6c 6c 20 62 65 20 61 20 72 65 61 64 20 6c 6f  ill be a read lo
1c4cb 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
1c4cc 73 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 72  se whenever ther
1c4cd 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70 65 6e 20  e is an.** open 
1c4ce 63 75 72 73 6f 72 2e 20 20 49 66 20 74 68 65 20  cursor.  If the 
1c4cf 64 61 74 61 62 61 73 65 20 77 61 73 20 75 6e 6c  database was unl
1c4d0 6f 63 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 74  ocked prior to t
1c4d1 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a  his instruction.
1c4d2 2a 2a 20 74 68 65 6e 20 61 20 72 65 61 64 20 6c  ** then a read l
1c4d3 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65 64 20  ock is acquired 
1c4d4 61 73 20 70 61 72 74 20 6f 66 20 74 68 69 73 20  as part of this 
1c4d5 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 41 20  instruction.  A 
1c4d6 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20 61 6c 6c  read.** lock all
1c4d7 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ows other proces
1c4d8 73 65 73 20 74 6f 20 72 65 61 64 20 74 68 65 20  ses to read the 
1c4d9 64 61 74 61 62 61 73 65 20 62 75 74 20 70 72 6f  database but pro
1c4da 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79 20 6f 74  hibits.** any ot
1c4db 68 65 72 20 70 72 6f 63 65 73 73 20 66 72 6f 6d  her process from
1c4dc 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 64   modifying the d
1c4dd 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 72 65  atabase.  The re
1c4de 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 72 65  ad lock is.** re
1c4df 6c 65 61 73 65 64 20 77 68 65 6e 20 61 6c 6c 20  leased when all 
1c4e0 63 75 72 73 6f 72 73 20 61 72 65 20 63 6c 6f 73  cursors are clos
1c4e1 65 64 2e 20 20 49 66 20 74 68 69 73 20 69 6e 73  ed.  If this ins
1c4e2 74 72 75 63 74 69 6f 6e 20 61 74 74 65 6d 70 74  truction attempt
1c4e3 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61 20 72 65  s.** to get a re
1c4e4 61 64 20 6c 6f 63 6b 20 62 75 74 20 66 61 69 6c  ad lock but fail
1c4e5 73 2c 20 74 68 65 20 73 63 72 69 70 74 20 74 65  s, the script te
1c4e6 72 6d 69 6e 61 74 65 73 20 77 69 74 68 20 61 6e  rminates with an
1c4e7 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20  .** SQLITE_BUSY 
1c4e8 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  error code..**.*
1c4e9 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 6d  * The P4 value m
1c4ea 61 79 20 62 65 20 65 69 74 68 65 72 20 61 6e 20  ay be either an 
1c4eb 69 6e 74 65 67 65 72 20 28 50 34 5f 49 4e 54 33  integer (P4_INT3
1c4ec 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 20  2) or a pointer 
1c4ed 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e 66 6f 20  to.** a KeyInfo 
1c4ee 73 74 72 75 63 74 75 72 65 20 28 50 34 5f 4b 45  structure (P4_KE
1c4ef 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 20 69 73  YINFO). If it is
1c4f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1c4f1 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75  KeyInfo .** stru
1c4f2 63 74 75 72 65 2c 20 74 68 65 6e 20 73 61 69 64  cture, then said
1c4f3 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69 6e   structure defin
1c4f4 65 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 61  es the content a
1c4f5 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 0a 2a 2a  nd collating .**
1c4f6 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 65   sequence of the
1c4f7 20 69 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65   index being ope
1c4f8 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
1c4f9 69 66 20 50 34 20 69 73 20 61 6e 20 69 6e 74 65  if P4 is an inte
1c4fa 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 2c 20 69  ger .** value, i
1c4fb 74 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  t is set to the 
1c4fc 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
1c4fd 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  s in the table..
1c4fe 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f  **.** See also O
1c4ff 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a 2f 2a 20  penWrite..*/./* 
1c500 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57 72 69 74  Opcode: OpenWrit
1c501 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  e P1 P2 P3 P4 P5
1c502 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65  .**.** Open a re
1c503 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20  ad/write cursor 
1c504 6e 61 6d 65 64 20 50 31 20 6f 6e 20 74 68 65 20  named P1 on the 
1c505 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77  table or index w
1c506 68 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67  hose root.** pag
1c507 65 20 69 73 20 50 32 2e 20 20 4f 72 20 69 66 20  e is P2.  Or if 
1c508 50 35 21 3d 30 20 75 73 65 20 74 68 65 20 63 6f  P5!=0 use the co
1c509 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
1c50a 72 20 50 32 20 74 6f 20 66 69 6e 64 20 74 68 65  r P2 to find the
1c50b 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 0a 2a  .** root page..*
1c50c 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75  *.** The P4 valu
1c50d 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20  e may be either 
1c50e 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49  an integer (P4_I
1c50f 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74  NT32) or a point
1c510 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e  er to.** a KeyIn
1c511 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 50 34  fo structure (P4
1c512 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74  _KEYINFO). If it
1c513 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1c514 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73   a KeyInfo .** s
1c515 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73  tructure, then s
1c516 61 69 64 20 73 74 72 75 63 74 75 72 65 20 64 65  aid structure de
1c517 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e  fines the conten
1c518 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20  t and collating 
1c519 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20  .** sequence of 
1c51a 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20  the index being 
1c51b 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  opened. Otherwis
1c51c 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e 20 69  e, if P4 is an i
1c51d 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65  nteger .** value
1c51e 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74  , it is set to t
1c51f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1c520 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
1c521 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  e..**.** This in
1c522 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20  struction works 
1c523 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65  just like OpenRe
1c524 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ad except that i
1c525 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73  t opens the curs
1c526 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72  or.** in read/wr
1c527 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61  ite mode.  For a
1c528 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68   given table, th
1c529 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f  ere can be one o
1c52a 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79  r more read-only
1c52b 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61  .** cursors or a
1c52c 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69   single read/wri
1c52d 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f  te cursor but no
1c52e 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65  t both..**.** Se
1c52f 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e  e also OpenRead.
1c530 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e  .*/.case OP_Open
1c531 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f 4f 70  Read:.case OP_Op
1c532 65 6e 57 72 69 74 65 3a 20 7b 0a 20 20 69 6e 74  enWrite: {.  int
1c533 20 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 4b   nField = 0;.  K
1c534 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
1c535 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 20 3d 20   = 0;.  int i = 
1c536 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e 74 20 70  pOp->p1;.  int p
1c537 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69  2 = pOp->p2;.  i
1c538 6e 74 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33  nt iDb = pOp->p3
1c539 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a  ;.  int wrFlag;.
1c53a 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56    Btree *pX;.  V
1c53b 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  dbeCursor *pCur;
1c53c 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 0a 20  .  Db *pDb;.  . 
1c53d 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
1c53e 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
1c53f 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
1c540 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c  btreeMask & (1<<
1c541 69 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 70 44  iDb))!=0 );.  pD
1c542 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
1c543 5d 3b 0a 20 20 70 58 20 3d 20 70 44 62 2d 3e 70  ];.  pX = pDb->p
1c544 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 58  Bt;.  assert( pX
1c545 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70  !=0 );.  if( pOp
1c546 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65  ->opcode==OP_Ope
1c547 6e 57 72 69 74 65 20 29 7b 0a 20 20 20 20 77 72  nWrite ){.    wr
1c548 46 6c 61 67 20 3d 20 31 3b 0a 20 20 20 20 69 66  Flag = 1;.    if
1c549 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
1c54a 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d  file_format < p-
1c54b 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72  >minWriteFileFor
1c54c 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  mat ){.      p->
1c54d 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d  minWriteFileForm
1c54e 61 74 20 3d 20 70 44 62 2d 3e 70 53 63 68 65 6d  at = pDb->pSchem
1c54f 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a  a->file_format;.
1c550 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1c551 20 20 20 77 72 46 6c 61 67 20 3d 20 30 3b 0a 20     wrFlag = 0;. 
1c552 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35   }.  if( pOp->p5
1c553 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1c554 70 32 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  p2>0 );.    asse
1c555 72 74 28 20 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  rt( p2<=p->nMem 
1c556 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 70  );.    pIn2 = &p
1c557 2d 3e 61 4d 65 6d 5b 70 32 5d 3b 0a 20 20 20 20  ->aMem[p2];.    
1c558 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
1c559 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a  tegerify(pIn2);.
1c55a 20 20 20 20 70 32 20 3d 20 28 69 6e 74 29 70 49      p2 = (int)pI
1c55b 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 69 66 28  n2->u.i;.    if(
1c55c 20 70 32 3c 32 20 29 20 7b 0a 20 20 20 20 20 20   p2<2 ) {.      
1c55d 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
1c55e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
1c55f 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1c560 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  o_error;.    }. 
1c561 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d   }.  assert( i>=
1c562 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  0 );.  if( pOp->
1c563 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e  p4type==P4_KEYIN
1c564 46 4f 20 29 7b 0a 20 20 20 20 70 4b 65 79 49 6e  FO ){.    pKeyIn
1c565 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65  fo = pOp->p4.pKe
1c566 79 49 6e 66 6f 3b 0a 20 20 20 20 70 4b 65 79 49  yInfo;.    pKeyI
1c567 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70  nfo->enc = ENC(p
1c568 2d 3e 64 62 29 3b 0a 20 20 20 20 6e 46 69 65 6c  ->db);.    nFiel
1c569 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  d = pKeyInfo->nF
1c56a 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73 65 20  ield+1;.  }else 
1c56b 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  if( pOp->p4type=
1c56c 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20 20  =P4_INT32 ){.   
1c56d 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70   nField = pOp->p
1c56e 34 2e 69 3b 0a 20 20 7d 0a 20 20 70 43 75 72 20  4.i;.  }.  pCur 
1c56f 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72  = allocateCursor
1c570 28 70 2c 20 69 2c 20 6e 46 69 65 6c 64 2c 20 69  (p, i, nField, i
1c571 44 62 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 43  Db, 1);.  if( pC
1c572 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  ur==0 ) goto no_
1c573 6d 65 6d 3b 0a 20 20 70 43 75 72 2d 3e 6e 75 6c  mem;.  pCur->nul
1c574 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d  lRow = 1;.  rc =
1c575 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
1c576 73 6f 72 28 70 58 2c 20 70 32 2c 20 77 72 46 6c  sor(pX, p2, wrFl
1c577 61 67 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43  ag, pKeyInfo, pC
1c578 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  ur->pCursor);.  
1c579 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d  pCur->pKeyInfo =
1c57a 20 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 20 73 77   pKeyInfo;..  sw
1c57b 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20  itch( rc ){.    
1c57c 63 61 73 65 20 53 51 4c 49 54 45 5f 42 55 53 59  case SQLITE_BUSY
1c57d 3a 20 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20  : {.      p->pc 
1c57e 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72  = pc;.      p->r
1c57f 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  c = rc = SQLITE_
1c580 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BUSY;.      goto
1c581 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20   vdbe_return;.  
1c582 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
1c583 49 54 45 5f 4f 4b 3a 20 7b 0a 20 20 20 20 20 20  ITE_OK: {.      
1c584 69 6e 74 20 66 6c 61 67 73 20 3d 20 73 71 6c 69  int flags = sqli
1c585 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43  te3BtreeFlags(pC
1c586 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  ur->pCursor);.  
1c587 20 20 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68      /* Sanity ch
1c588 65 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 74 68  ecking.  Only th
1c589 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74  e lower four bit
1c58a 73 20 6f 66 20 74 68 65 20 66 6c 61 67 73 20 62  s of the flags b
1c58b 79 74 65 20 73 68 6f 75 6c 64 0a 20 20 20 20 20  yte should.     
1c58c 20 2a 2a 20 62 65 20 75 73 65 64 2e 20 20 42 69   ** be used.  Bi
1c58d 74 20 33 20 28 6d 61 73 6b 20 30 78 30 38 29 20  t 3 (mask 0x08) 
1c58e 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65  is unpredictable
1c58f 2e 20 20 54 68 65 20 6c 6f 77 65 72 20 33 20 62  .  The lower 3 b
1c590 69 74 73 0a 20 20 20 20 20 20 2a 2a 20 28 6d 61  its.      ** (ma
1c591 73 6b 20 30 78 30 37 29 20 73 68 6f 75 6c 64 20  sk 0x07) should 
1c592 62 65 20 65 69 74 68 65 72 20 35 20 28 69 6e 74  be either 5 (int
1c593 6b 65 79 2b 6c 65 61 66 64 61 74 61 20 66 6f 72  key+leafdata for
1c594 20 74 61 62 6c 65 73 29 20 6f 72 0a 20 20 20 20   tables) or.    
1c595 20 20 2a 2a 20 32 20 28 7a 65 72 6f 64 61 74 61    ** 2 (zerodata
1c596 20 66 6f 72 20 69 6e 64 69 63 65 73 29 2e 20 20   for indices).  
1c597 49 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69  If these conditi
1c598 6f 6e 73 20 61 72 65 20 6e 6f 74 20 6d 65 74 20  ons are not met 
1c599 69 74 20 63 61 6e 0a 20 20 20 20 20 20 2a 2a 20  it can.      ** 
1c59a 6f 6e 6c 79 20 6d 65 61 6e 20 74 68 61 74 20 77  only mean that w
1c59b 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
1c59c 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  th a corrupt dat
1c59d 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20  abase file.     
1c59e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 66   */.      if( (f
1c59f 6c 61 67 73 20 26 20 30 78 66 30 29 21 3d 30 20  lags & 0xf0)!=0 
1c5a0 7c 7c 20 28 28 66 6c 61 67 73 20 26 20 30 78 30  || ((flags & 0x0
1c5a1 37 29 21 3d 35 20 26 26 20 28 66 6c 61 67 73 20  7)!=5 && (flags 
1c5a2 26 20 30 78 30 37 29 21 3d 32 29 20 29 7b 0a 20  & 0x07)!=2) ){. 
1c5a3 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1c5a4 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1c5a5 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
1c5a6 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1c5a7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1c5a8 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20  pCur->isTable = 
1c5a9 28 66 6c 61 67 73 20 26 20 42 54 52 45 45 5f 49  (flags & BTREE_I
1c5aa 4e 54 4b 45 59 29 21 3d 30 20 3f 31 3a 30 3b 0a  NTKEY)!=0 ?1:0;.
1c5ab 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e        pCur->isIn
1c5ac 64 65 78 20 3d 20 28 66 6c 61 67 73 20 26 20 42  dex = (flags & B
1c5ad 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29 21 3d  TREE_ZERODATA)!=
1c5ae 30 20 3f 31 3a 30 3b 0a 20 20 20 20 20 20 2f 2a  0 ?1:0;.      /*
1c5af 20 49 66 20 50 34 3d 3d 30 20 69 74 20 6d 65 61   If P4==0 it mea
1c5b0 6e 73 20 77 65 20 61 72 65 20 65 78 70 65 63 74  ns we are expect
1c5b1 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74 61 62  ed to open a tab
1c5b2 6c 65 2e 20 20 49 66 20 50 34 21 3d 30 20 74 68  le.  If P4!=0 th
1c5b3 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 65  en.      ** we e
1c5b4 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70 65 6e  xpect to be open
1c5b5 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20 20 49  ing an index.  I
1c5b6 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 77 68  f this is not wh
1c5b7 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20 20 20  at happened,.   
1c5b8 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 64     ** then the d
1c5b9 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75  atabase is corru
1c5ba 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  pt.      */.    
1c5bb 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69 73 54    if( (pCur->isT
1c5bc 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70 34 74  able && pOp->p4t
1c5bd 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 29  ype==P4_KEYINFO)
1c5be 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43 75 72  .       || (pCur
1c5bf 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70 4f 70  ->isIndex && pOp
1c5c0 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 59  ->p4type!=P4_KEY
1c5c1 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20 20 20  INFO) ){.       
1c5c2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
1c5c3 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
1c5c4 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1c5c5 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1c5c6 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1c5c7 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1c5c8 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20 7b 0a  SQLITE_EMPTY: {.
1c5c9 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 54 61        pCur->isTa
1c5ca 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70  ble = pOp->p4typ
1c5cb 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20  e!=P4_KEYINFO;. 
1c5cc 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e 64       pCur->isInd
1c5cd 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73 54 61  ex = !pCur->isTa
1c5ce 62 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d  ble;.      pCur-
1c5cf 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  >pCursor = 0;.  
1c5d0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1c5d1 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OK;.      break;
1c5d2 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
1c5d3 6c 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f  lt: {.      goto
1c5d4 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1c5d5 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ror;.    }.  }. 
1c5d6 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1c5d7 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65  code: OpenEpheme
1c5d8 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a  ral P1 P2 * P4 *
1c5d9 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65  .**.** Open a ne
1c5da 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61  w cursor P1 to a
1c5db 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65   transient table
1c5dc 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20  ..** The cursor 
1c5dd 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64  is always opened
1c5de 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e   read/write even
1c5df 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e   if .** the main
1c5e0 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 61   database is rea
1c5e1 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 61  d-only.  The tra
1c5e2 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 75 61  nsient or virtua
1c5e3 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65  l.** table is de
1c5e4 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61  leted automatica
1c5e5 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75 72  lly when the cur
1c5e6 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a  sor is closed..*
1c5e7 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e  *.** P2 is the n
1c5e8 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1c5e9 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20   in the virtual 
1c5ea 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75  table..** The cu
1c5eb 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61  rsor points to a
1c5ec 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 66 20   BTree table if 
1c5ed 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 42  P4==0 and to a B
1c5ee 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66  Tree index.** if
1c5ef 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 49   P4 is not 0.  I
1c5f0 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f P4 is not NULL
1c5f1 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  , it points to a
1c5f2 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
1c5f3 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 6e  re.** that defin
1c5f4 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66  es the format of
1c5f5 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 6e 64   keys in the ind
1c5f6 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  ex..**.** This o
1c5f7 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 63  pcode was once c
1c5f8 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20  alled OpenTemp. 
1c5f9 20 42 75 74 20 74 68 61 74 20 63 72 65 61 74 65   But that create
1c5fa 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62  d.** confusion b
1c5fb 65 63 61 75 73 65 20 74 68 65 20 74 65 72 6d 20  ecause the term 
1c5fc 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69  "temp table", mi
1c5fd 67 68 74 20 72 65 66 65 72 20 65 69 74 68 65 72  ght refer either
1c5fe 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 61  .** to a TEMP ta
1c5ff 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c 20 6c  ble at the SQL l
1c600 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61  evel, or to a ta
1c601 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a  ble opened by.**
1c602 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 54   this opcode.  T
1c603 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20  hen this opcode 
1c604 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72  was call OpenVir
1c605 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68  tual.  But.** th
1c606 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e 66 75  at created confu
1c607 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 77 68  sion with the wh
1c608 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  ole virtual-tabl
1c609 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20  e idea..*/.case 
1c60a 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
1c60b 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f  : {.  int i = pO
1c60c 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72  p->p1;.  VdbeCur
1c60d 73 6f 72 20 2a 70 43 78 3b 0a 20 20 73 74 61 74  sor *pCx;.  stat
1c60e 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70 65  ic const int ope
1c60f 6e 46 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20  nFlags = .      
1c610 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
1c611 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51  WRITE |.      SQ
1c612 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
1c613 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f   |.      SQLITE_
1c614 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c  OPEN_EXCLUSIVE |
1c615 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
1c616 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
1c617 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f   |.      SQLITE_
1c618 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44  OPEN_TRANSIENT_D
1c619 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  B;..  assert( i>
1c61a 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61 6c  =0 );.  pCx = al
1c61b 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20  locateCursor(p, 
1c61c 69 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c 20  i, pOp->p2, -1, 
1c61d 31 29 3b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30  1);.  if( pCx==0
1c61e 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1c61f 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d    pCx->nullRow =
1c620 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   1;.  rc = sqlit
1c621 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64  e3BtreeFactory(d
1c622 62 2c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f  b, 0, 1, SQLITE_
1c623 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43  DEFAULT_TEMP_CAC
1c624 48 45 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61  HE_SIZE, openFla
1c625 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  gs,.            
1c626 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
1c627 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28  pCx->pBt);.  if(
1c628 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1c629 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1c62a 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
1c62b 73 28 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a  s(pCx->pBt, 1);.
1c62c 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
1c62d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
1c62e 2a 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74  * If a transient
1c62f 20 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72   index is requir
1c630 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79  ed, create it by
1c631 20 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20   calling.    ** 
1c632 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
1c633 74 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74  teTable() with t
1c634 68 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54  he BTREE_ZERODAT
1c635 41 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20  A flag before.  
1c636 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e    ** opening it.
1c637 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20   If a transient 
1c638 74 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65  table is require
1c639 64 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a  d, just use the.
1c63a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63      ** automatic
1c63b 61 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62  ally created tab
1c63c 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  le with root-pag
1c63d 65 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74  e 1 (an INTKEY t
1c63e 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  able)..    */.  
1c63f 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b    if( pOp->p4.pK
1c640 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20  eyInfo ){.      
1c641 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20  int pgno;.      
1c642 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1c643 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20  ype==P4_KEYINFO 
1c644 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
1c645 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
1c646 54 61 62 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20  Table(pCx->pBt, 
1c647 26 70 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52  &pgno, BTREE_ZER
1c648 4f 44 41 54 41 29 3b 20 0a 20 20 20 20 20 20 69  ODATA); .      i
1c649 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1c64a 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1c64b 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52  rt( pgno==MASTER
1c64c 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20  _ROOT+1 );.     
1c64d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1c64e 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d 3e  treeCursor(pCx->
1c64f 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20  pBt, pgno, 1, . 
1c650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c651 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1c652 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34  KeyInfo*)pOp->p4
1c653 2e 7a 2c 20 70 43 78 2d 3e 70 43 75 72 73 6f 72  .z, pCx->pCursor
1c654 29 3b 0a 20 20 20 20 20 20 20 20 70 43 78 2d 3e  );.        pCx->
1c655 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e  pKeyInfo = pOp->
1c656 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  p4.pKeyInfo;.   
1c657 20 20 20 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e       pCx->pKeyIn
1c658 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d  fo->enc = ENC(p-
1c659 3e 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  >db);.      }.  
1c65a 20 20 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65      pCx->isTable
1c65b 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
1c65c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1c65d 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 70  te3BtreeCursor(p
1c65e 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f  Cx->pBt, MASTER_
1c65f 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 70 43 78 2d  ROOT, 1, 0, pCx-
1c660 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >pCursor);.     
1c661 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20   pCx->isTable = 
1c662 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
1c663 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 70  Cx->isIndex = !p
1c664 43 78 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62  Cx->isTable;.  b
1c665 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1c666 64 65 3a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50  de: OpenPseudo P
1c667 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1c668 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72  * Open a new cur
1c669 73 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20  sor that points 
1c66a 74 6f 20 61 20 66 61 6b 65 20 74 61 62 6c 65 20  to a fake table 
1c66b 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20  that contains a 
1c66c 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66  single.** row of
1c66d 20 64 61 74 61 2e 20 20 41 6e 79 20 61 74 74 65   data.  Any atte
1c66e 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 73  mpt to write a s
1c66f 65 63 6f 6e 64 20 72 6f 77 20 6f 66 20 64 61 74  econd row of dat
1c670 61 20 63 61 75 73 65 73 20 74 68 65 0a 2a 2a 20  a causes the.** 
1c671 66 69 72 73 74 20 72 6f 77 20 74 6f 20 62 65 20  first row to be 
1c672 64 65 6c 65 74 65 64 2e 20 20 41 6c 6c 20 64 61  deleted.  All da
1c673 74 61 20 69 73 20 64 65 6c 65 74 65 64 20 77 68  ta is deleted wh
1c674 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  en the cursor is
1c675 0a 2a 2a 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a  .** closed..**.*
1c676 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  * A pseudo-table
1c677 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73   created by this
1c678 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 66 75   opcode is usefu
1c679 6c 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68  l for holding th
1c67a 65 0a 2a 2a 20 4e 45 57 20 6f 72 20 4f 4c 44 20  e.** NEW or OLD 
1c67b 74 61 62 6c 65 73 20 69 6e 20 61 20 74 72 69 67  tables in a trig
1c67c 67 65 72 2e 20 20 41 6c 73 6f 20 75 73 65 64 20  ger.  Also used 
1c67d 74 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73 69  to hold the a si
1c67e 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70  ngle.** row outp
1c67f 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74  ut from the sort
1c680 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 72  er so that the r
1c681 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70  ow can be decomp
1c682 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64  osed into.** ind
1c683 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20  ividual columns 
1c684 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c  using the OP_Col
1c685 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a  umn opcode..**.*
1c686 2a 20 57 68 65 6e 20 4f 50 5f 49 6e 73 65 72 74  * When OP_Insert
1c687 20 69 73 20 65 78 65 63 75 74 65 64 20 74 6f 20   is executed to 
1c688 69 6e 73 65 72 74 20 61 20 72 6f 77 20 69 6e 20  insert a row in 
1c689 74 6f 20 74 68 65 20 70 73 65 75 64 6f 20 74 61  to the pseudo ta
1c68a 62 6c 65 2c 0a 2a 2a 20 74 68 65 20 70 73 65 75  ble,.** the pseu
1c68b 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f 72 20  do-table cursor 
1c68c 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 6d  may or may not m
1c68d 61 6b 65 20 69 74 27 73 20 6f 77 6e 20 63 6f 70  ake it's own cop
1c68e 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67  y of the.** orig
1c68f 69 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e 20 49  inal row data. I
1c690 66 20 50 32 20 69 73 20 30 2c 20 74 68 65 6e 20  f P2 is 0, then 
1c691 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  the pseudo-table
1c692 20 77 69 6c 6c 20 63 6f 70 79 20 74 68 65 0a 2a   will copy the.*
1c693 2a 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 20 64  * original row d
1c694 61 74 61 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ata. Otherwise, 
1c695 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1c696 20 6f 72 69 67 69 6e 61 6c 20 6d 65 6d 6f 72 79   original memory
1c697 20 63 65 6c 6c 0a 2a 2a 20 69 73 20 73 74 6f 72   cell.** is stor
1c698 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
1c699 2c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  , the vdbe progr
1c69a 61 6d 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  am must ensure t
1c69b 68 61 74 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f  hat the .** memo
1c69c 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69  ry cell containi
1c69d 6e 67 20 74 68 65 20 72 6f 77 20 64 61 74 61 20  ng the row data 
1c69e 69 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 74  is not overwritt
1c69f 65 6e 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20  en until the.** 
1c6a0 70 73 65 75 64 6f 20 74 61 62 6c 65 20 69 73 20  pseudo table is 
1c6a1 63 6c 6f 73 65 64 20 28 6f 72 20 61 20 6e 65 77  closed (or a new
1c6a2 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64   row is inserted
1c6a3 20 69 6e 74 6f 20 69 74 29 2e 0a 2a 2a 0a 2a 2a   into it)..**.**
1c6a4 20 50 33 20 69 73 20 74 68 65 20 6e 75 6d 62 65   P3 is the numbe
1c6a5 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
1c6a6 68 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 20  he records that 
1c6a7 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 62  will be stored b
1c6a8 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d  y.** the pseudo-
1c6a9 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
1c6aa 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a  P_OpenPseudo: {.
1c6ab 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1c6ac 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  1;.  VdbeCursor 
1c6ad 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28 20  *pCx;.  assert( 
1c6ae 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20  i>=0 );.  pCx = 
1c6af 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70  allocateCursor(p
1c6b0 2c 20 69 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31  , i, pOp->p3, -1
1c6b1 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 78 3d  , 0);.  if( pCx=
1c6b2 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
1c6b3 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77  ;.  pCx->nullRow
1c6b4 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 70 73 65   = 1;.  pCx->pse
1c6b5 75 64 6f 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20  udoTable = 1;.  
1c6b6 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f  pCx->ephemPseudo
1c6b7 54 61 62 6c 65 20 3d 20 28 75 38 29 70 4f 70 2d  Table = (u8)pOp-
1c6b8 3e 70 32 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61  >p2;.  pCx->isTa
1c6b9 62 6c 65 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e  ble = 1;.  pCx->
1c6ba 69 73 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62  isIndex = 0;.  b
1c6bb 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1c6bc 64 65 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a  de: Close P1 * *
1c6bd 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65   * *.**.** Close
1c6be 20 61 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f   a cursor previo
1c6bf 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50  usly opened as P
1c6c0 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74  1.  If P1 is not
1c6c1 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  .** currently op
1c6c2 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63  en, this instruc
1c6c3 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
1c6c4 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73  .*/.case OP_Clos
1c6c5 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70  e: {.  int i = p
1c6c6 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74  Op->p1;.  assert
1c6c7 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
1c6c8 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69  Cursor );.  sqli
1c6c9 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f  te3VdbeFreeCurso
1c6ca 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d  r(p, p->apCsr[i]
1c6cb 29 3b 0a 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d  );.  p->apCsr[i]
1c6cc 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1c6cd 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65  ../* Opcode: See
1c6ce 6b 47 65 20 50 31 20 50 32 20 50 33 20 50 34 20  kGe P1 P2 P3 P4 
1c6cf 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f  *.**.** If curso
1c6d0 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61  r P1 refers to a
1c6d1 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54  n SQL table (B-T
1c6d2 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e  ree that uses in
1c6d3 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a  teger keys), .**
1c6d4 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69   use the value i
1c6d5 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73  n register P3 as
1c6d6 20 74 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75   the key.  If cu
1c6d7 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a  rsor P1 refers .
1c6d8 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64  ** to an SQL ind
1c6d9 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74  ex, then P3 is t
1c6da 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61  he first in an a
1c6db 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73  rray of P4 regis
1c6dc 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72  ters .** that ar
1c6dd 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70  e used as an unp
1c6de 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e  acked index key.
1c6df 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69   .**.** Repositi
1c6e0 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20  on cursor P1 so 
1c6e1 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20  that  it points 
1c6e2 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20  to the smallest 
1c6e3 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69  entry that .** i
1c6e4 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
1c6e5 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b  r equal to the k
1c6e6 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  ey value. If the
1c6e7 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
1c6e8 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68  s .** greater th
1c6e9 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
1c6ea 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73  he key and P2 is
1c6eb 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20   not zero, then 
1c6ec 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
1c6ed 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
1c6ee 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73  d, NotFound, Dis
1c6ef 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53  tinct, SeekLt, S
1c6f0 65 65 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f  eekGt, SeekLe.*/
1c6f1 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b  ./* Opcode: Seek
1c6f2 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  Gt P1 P2 P3 P4 *
1c6f3 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  .**.** If cursor
1c6f4 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e   P1 refers to an
1c6f5 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72   SQL table (B-Tr
1c6f6 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74  ee that uses int
1c6f7 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20  eger keys), .** 
1c6f8 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  use the value in
1c6f9 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
1c6fa 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
1c6fb 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
1c6fc 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
1c6fd 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
1c6fe 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
1c6ff 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1c700 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
1c701 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
1c702 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
1c703 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
1c704 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1c705 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74    it points to t
1c706 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72  he smallest entr
1c707 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72  y that .** is gr
1c708 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b  eater than the k
1c709 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65  ey value. If the
1c70a 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
1c70b 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a  s greater than .
1c70c 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50  ** the key and P
1c70d 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74  2 is not zero, t
1c70e 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a  hen jump to P2..
1c70f 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1c710 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
1c711 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c   Distinct, SeekL
1c712 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c  t, SeekGe, SeekL
1c713 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  e.*/./* Opcode: 
1c714 53 65 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20  SeekLt P1 P2 P3 
1c715 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63  P4 * .**.** If c
1c716 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
1c717 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20  to an SQL table 
1c718 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 65  (B-Tree that use
1c719 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c  s integer keys),
1c71a 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c   .** use the val
1c71b 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1c71c 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63  3 as a key. If c
1c71d 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
1c71e 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e  .** to an SQL in
1c71f 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20  dex, then P3 is 
1c720 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
1c721 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69  array of P4 regi
1c722 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61  sters .** that a
1c723 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e  re used as an un
1c724 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
1c725 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74  . .**.** Reposit
1c726 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f  ion cursor P1 so
1c727 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73   that  it points
1c728 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20   to the largest 
1c729 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69  entry that .** i
1c72a 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
1c72b 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68  key value. If th
1c72c 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72  ere are no recor
1c72d 64 73 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a  ds less than .**
1c72e 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
1c72f 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
1c730 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1c731 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1c732 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44  und, NotFound, D
1c733 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c  istinct, SeekGt,
1c734 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a   SeekGe, SeekLe.
1c735 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  */./* Opcode: Se
1c736 65 6b 4c 65 20 50 31 20 50 32 20 50 33 20 50 34  ekLe P1 P2 P3 P4
1c737 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73   *.**.** If curs
1c738 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20  or P1 refers to 
1c739 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d  an SQL table (B-
1c73a 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69  Tree that uses i
1c73b 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a  nteger keys), .*
1c73c 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20  * use the value 
1c73d 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61  in register P3 a
1c73e 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73  s a key. If curs
1c73f 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a  or P1 refers .**
1c740 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78   to an SQL index
1c741 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65  , then P3 is the
1c742 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72   first in an arr
1c743 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65  ay of P4 registe
1c744 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  rs .** that are 
1c745 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63  used as an unpac
1c746 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a  ked index key. .
1c747 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e  **.** Reposition
1c748 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
1c749 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
1c74a 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72  the largest entr
1c74b 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65  y that .** is le
1c74c 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
1c74d 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75   to the key valu
1c74e 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20  e. If there are 
1c74f 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c  no records .** l
1c750 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
1c751 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64  l to the key and
1c752 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c   P2 is not zero,
1c753 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32   then jump to P2
1c754 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1c755 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e  : Found, NotFoun
1c756 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65  d, Distinct, See
1c757 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65  kGt, SeekGe, See
1c758 6b 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  kLt.*/.case OP_S
1c759 65 65 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f  eekLt:         /
1c75a 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63  * jump, in3 */.c
1c75b 61 73 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20  ase OP_SeekLe:  
1c75c 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1c75d 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  in3 */.case OP_S
1c75e 65 65 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f  eekGe:         /
1c75f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63  * jump, in3 */.c
1c760 61 73 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b  ase OP_SeekGt: {
1c761 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1c762 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  in3 */.  int i =
1c763 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65   pOp->p1;.  Vdbe
1c764 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61  Cursor *pC;..  a
1c765 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
1c766 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1c767 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1c768 21 3d 30 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d  !=0 );.  pC = p-
1c769 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73  >apCsr[i];.  ass
1c76a 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20  ert( pC!=0 );.  
1c76b 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21  if( pC->pCursor!
1c76c 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  =0 ){.    int re
1c76d 73 2c 20 6f 63 3b 0a 20 20 20 20 6f 63 20 3d 20  s, oc;.    oc = 
1c76e 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20  pOp->opcode;.   
1c76f 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30   pC->nullRow = 0
1c770 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 69 73  ;.    if( pC->is
1c771 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69  Table ){.      i
1c772 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a  64 iKey;      /*
1c773 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61 72   The rowid we ar
1c774 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a  e to seek to */.
1c775 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e  .      /* The in
1c776 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20  put value in P3 
1c777 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20  might be of any 
1c778 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72  type: integer, r
1c779 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20  eal, string,.   
1c77a 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e     ** blob, or N
1c77b 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65  ULL.  But it nee
1c77c 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65  ds to be an inte
1c77d 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 61  ger before we ca
1c77e 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  n do.      ** th
1c77f 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72  e seek, so cover
1c780 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 61  t it. */.      a
1c781 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
1c782 69 74 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 20  ity(pIn3);.     
1c783 20 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56   iKey = sqlite3V
1c784 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33  dbeIntValue(pIn3
1c785 29 3b 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77  );.      pC->row
1c786 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 0a  idIsValid = 0;..
1c787 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
1c788 50 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 6e  P3 value could n
1c789 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20  ot be converted 
1c78a 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  into an integer 
1c78b 77 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a 2a  without.      **
1c78c 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61   loss of informa
1c78d 74 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 69  tion, then speci
1c78e 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73  al processing is
1c78f 20 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f 0a   required... */.
1c790 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d        if( (pIn3-
1c791 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1c792 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1c793 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73  if( (pIn3->flags
1c794 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20   & MEM_Real)==0 
1c795 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
1c796 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20  If the P3 value 
1c797 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72  cannot be conver
1c798 74 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 6e  ted into any kin
1c799 64 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a 20  d of a number,. 
1c79a 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e           ** then
1c79b 20 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f 74   the seek is not
1c79c 20 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a 75   possible, so ju
1c79d 6d 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20  mp to P2 */.    
1c79e 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1c79f 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20  p2 - 1;.        
1c7a0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1c7a1 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66   }.        /* If
1c7a2 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70   we reach this p
1c7a3 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 50  oint, then the P
1c7a4 33 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20  3 value must be 
1c7a5 61 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 20  a floating.     
1c7a6 20 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62     ** point numb
1c7a7 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  er. */.        a
1c7a8 73 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c  ssert( (pIn3->fl
1c7a9 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21  ags & MEM_Real)!
1c7aa 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  =0 );..        i
1c7ab 66 28 20 69 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53  f( iKey==SMALLES
1c7ac 54 5f 49 4e 54 36 34 20 26 26 20 28 70 49 6e 33  T_INT64 && (pIn3
1c7ad 2d 3e 72 3c 28 64 6f 75 62 6c 65 29 69 4b 65 79  ->r<(double)iKey
1c7ae 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29   || pIn3->r>0) )
1c7af 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
1c7b0 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 74  he P3 value is t
1c7b1 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69  o large in magni
1c7b2 74 75 64 65 20 74 6f 20 62 65 20 65 78 70 72 65  tude to be expre
1c7b3 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20  ssed as an.     
1c7b4 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e       ** integer.
1c7b5 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65   */.          re
1c7b6 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  s = 1;.         
1c7b7 20 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 29   if( pIn3->r<0 )
1c7b8 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
1c7b9 28 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20  ( oc==OP_SeekGt 
1c7ba 7c 7c 20 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65  || oc==OP_SeekGe
1c7bb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1c7bc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1c7bd 72 65 65 46 69 72 73 74 28 70 43 2d 3e 70 43 75  reeFirst(pC->pCu
1c7be 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20  rsor, &res);.   
1c7bf 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72             if( r
1c7c0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
1c7c1 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1c7c2 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
1c7c3 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1c7c4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1c7c5 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65     if( oc==OP_Se
1c7c6 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53  ekLt || oc==OP_S
1c7c7 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20  eekLe ){.       
1c7c8 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1c7c9 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d  te3BtreeLast(pC-
1c7ca 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b  >pCursor, &res);
1c7cb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
1c7cc 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1c7cd 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1c7ce 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1c7cf 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1c7d0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1c7d1 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20  if( res ){.     
1c7d2 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d         pc = pOp-
1c7d3 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  >p2 - 1;.       
1c7d4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
1c7d5 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65  reak;.        }e
1c7d6 6c 73 65 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53  lse if( oc==OP_S
1c7d7 65 65 6b 4c 74 20 7c 7c 20 6f 63 3d 3d 4f 50 5f  eekLt || oc==OP_
1c7d8 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20  SeekGe ){.      
1c7d9 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 63      /* Use the c
1c7da 65 69 6c 69 6e 67 28 29 20 66 75 6e 63 74 69 6f  eiling() functio
1c7db 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61  n to convert rea
1c7dc 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20  l->int */.      
1c7dd 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20      if( pIn3->r 
1c7de 3e 20 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29  > (double)iKey )
1c7df 20 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20   iKey++;.       
1c7e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1c7e1 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f    /* Use the flo
1c7e2 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  or() function to
1c7e3 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69   convert real->i
1c7e4 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  nt */.          
1c7e5 61 73 73 65 72 74 28 20 6f 63 3d 3d 4f 50 5f 53  assert( oc==OP_S
1c7e6 65 65 6b 4c 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f  eekLe || oc==OP_
1c7e7 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20  SeekGt );.      
1c7e8 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20      if( pIn3->r 
1c7e9 3c 20 28 64 6f 75 62 6c 65 29 69 4b 65 79 20 29  < (double)iKey )
1c7ea 20 69 4b 65 79 2d 2d 3b 0a 20 20 20 20 20 20 20   iKey--;.       
1c7eb 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20   }.      } .    
1c7ec 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1c7ed 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1c7ee 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30  d(pC->pCursor, 0
1c7ef 2c 20 28 75 36 34 29 69 4b 65 79 2c 20 30 2c 20  , (u64)iKey, 0, 
1c7f0 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28  &res);.      if(
1c7f1 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1c7f2 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61  {.        goto a
1c7f3 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1c7f4 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  r;.      }.     
1c7f5 20 69 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20   if( res==0 ){. 
1c7f6 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64         pC->rowid
1c7f7 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20  IsValid = 1;.   
1c7f8 20 20 20 20 20 70 43 2d 3e 6c 61 73 74 52 6f 77       pC->lastRow
1c7f9 69 64 20 3d 20 69 4b 65 79 3b 0a 20 20 20 20 20  id = iKey;.     
1c7fa 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
1c7fb 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f      UnpackedReco
1c7fc 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e 74 20  rd r;.      int 
1c7fd 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34  nField = pOp->p4
1c7fe 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  .i;.      assert
1c7ff 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1c800 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20  4_INT32 );.     
1c801 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c 64 3e   assert( nField>
1c802 30 20 29 3b 0a 20 20 20 20 20 20 72 2e 70 4b 65  0 );.      r.pKe
1c803 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65 79  yInfo = pC->pKey
1c804 49 6e 66 6f 3b 0a 20 20 20 20 20 20 72 2e 6e 46  Info;.      r.nF
1c805 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 46 69 65  ield = (u16)nFie
1c806 6c 64 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 63  ld;.      if( oc
1c807 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 6f  ==OP_SeekGt || o
1c808 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a  c==OP_SeekLe ){.
1c809 20 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20          r.flags 
1c80a 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b  = UNPACKED_INCRK
1c80b 45 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EY;.      }else{
1c80c 0a 20 20 20 20 20 20 20 20 72 2e 66 6c 61 67 73  .        r.flags
1c80d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
1c80e 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26 70 2d      r.aMem = &p-
1c80f 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1c810 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1c811 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1c812 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f  acked(pC->pCurso
1c813 72 2c 20 26 72 2c 20 30 2c 20 30 2c 20 26 72 65  r, &r, 0, 0, &re
1c814 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
1c815 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1c816 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
1c817 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1c818 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
1c819 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1c81a 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43   0;.    }.    pC
1c81b 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1c81c 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61   = 0;.    pC->ca
1c81d 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1c81e 45 5f 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 20  E_STALE;.#ifdef 
1c81f 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20  SQLITE_TEST.    
1c820 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63  sqlite3_search_c
1c821 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
1c822 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50 5f 53 65     if( oc==OP_Se
1c823 65 6b 47 65 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 53  ekGe || oc==OP_S
1c824 65 65 6b 47 74 20 29 7b 0a 20 20 20 20 20 20 69  eekGt ){.      i
1c825 66 28 20 72 65 73 3c 30 20 7c 7c 20 28 72 65 73  f( res<0 || (res
1c826 3d 3d 30 20 26 26 20 6f 63 3d 3d 4f 50 5f 53 65  ==0 && oc==OP_Se
1c827 65 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20  ekGt) ){.       
1c828 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1c829 65 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73  eeNext(pC->pCurs
1c82a 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  or, &res);.     
1c82b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1c82c 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72  E_OK ) goto abor
1c82d 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1c82e 20 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69          pC->rowi
1c82f 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1c830 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1c831 20 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20     res = 0;.    
1c832 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
1c833 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d       assert( oc=
1c834 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 6f 63  =OP_SeekLt || oc
1c835 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20  ==OP_SeekLe );. 
1c836 20 20 20 20 20 69 66 28 20 72 65 73 3e 30 20 7c       if( res>0 |
1c837 7c 20 28 72 65 73 3d 3d 30 20 26 26 20 6f 63 3d  | (res==0 && oc=
1c838 3d 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20  =OP_SeekLt) ){. 
1c839 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1c83a 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
1c83b 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72  (pC->pCursor, &r
1c83c 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  es);.        if(
1c83d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1c83e 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1c83f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1c840 20 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c    pC->rowidIsVal
1c841 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  id = 0;.      }e
1c842 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
1c843 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67  res might be neg
1c844 61 74 69 76 65 20 62 65 63 61 75 73 65 20 74 68  ative because th
1c845 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79  e table is empty
1c846 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20  .  Check to.    
1c847 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68      ** see if th
1c848 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a  is is the case..
1c849 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1c84a 20 20 20 72 65 73 20 3d 20 73 71 6c 69 74 65 33     res = sqlite3
1c84b 42 74 72 65 65 45 6f 66 28 70 43 2d 3e 70 43 75  BtreeEof(pC->pCu
1c84c 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rsor);.      }. 
1c84d 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1c84e 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20   pOp->p2>0 );.  
1c84f 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
1c850 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1c851 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  - 1;.    }.  }el
1c852 73 65 20 69 66 28 20 21 70 43 2d 3e 70 73 65 75  se if( !pC->pseu
1c853 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f  doTable ){.    /
1c854 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77  * This happens w
1c855 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 74  hen attempting t
1c856 6f 20 6f 70 65 6e 20 74 68 65 20 73 71 6c 69 74  o open the sqlit
1c857 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a  e3_master table.
1c858 20 20 20 20 2a 2a 20 66 6f 72 20 72 65 61 64 20      ** for read 
1c859 61 63 63 65 73 73 20 72 65 74 75 72 6e 73 20 53  access returns S
1c85a 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20  QLITE_EMPTY. In 
1c85b 74 68 69 73 20 63 61 73 65 20 61 6c 77 61 79 73  this case always
1c85c 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20 74 68 65  .    ** take the
1c85d 20 6a 75 6d 70 20 28 73 69 6e 63 65 20 74 68 65   jump (since the
1c85e 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64  re are no record
1c85f 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 29 2e  s in the table).
1c860 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d  .    */.    pc =
1c861 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1c862 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1c863 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 20 50 31   Opcode: Seek P1
1c864 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1c865 50 31 20 69 73 20 61 6e 20 6f 70 65 6e 20 74 61  P1 is an open ta
1c866 62 6c 65 20 63 75 72 73 6f 72 20 61 6e 64 20 50  ble cursor and P
1c867 32 20 69 73 20 61 20 72 6f 77 69 64 20 69 6e 74  2 is a rowid int
1c868 65 67 65 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a  eger.  Arrange.*
1c869 2a 20 66 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65  * for P1 to move
1c86a 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e   so that it poin
1c86b 74 73 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20  ts to the rowid 
1c86c 67 69 76 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a  given by P2..**.
1c86d 2a 2a 20 54 68 69 73 20 69 73 20 61 63 74 75 61  ** This is actua
1c86e 6c 6c 79 20 61 20 64 65 66 65 72 72 65 64 20 73  lly a deferred s
1c86f 65 65 6b 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63  eek.  Nothing ac
1c870 74 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 75  tually happens u
1c871 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73  ntil.** the curs
1c872 6f 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  or is used to re
1c873 61 64 20 61 20 72 65 63 6f 72 64 2e 20 20 54 68  ad a record.  Th
1c874 61 74 20 77 61 79 2c 20 69 66 20 6e 6f 20 72 65  at way, if no re
1c875 61 64 73 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f  ads.** occur, no
1c876 20 75 6e 6e 65 63 65 73 73 61 72 79 20 49 2f 4f   unnecessary I/O
1c877 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73   happens..*/.cas
1c878 65 20 4f 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20  e OP_Seek: {    
1c879 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 69 6e 74 20  /* in2 */.  int 
1c87a 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56  i = pOp->p1;.  V
1c87b 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a  dbeCursor *pC;..
1c87c 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1c87d 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1c87e 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73  ;.  pC = p->apCs
1c87f 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  r[i];.  assert( 
1c880 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  pC!=0 );.  if( p
1c881 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b  C->pCursor!=0 ){
1c882 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1c883 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
1c884 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b  pC->nullRow = 0;
1c885 0a 20 20 20 20 70 43 2d 3e 6d 6f 76 65 74 6f 54  .    pC->movetoT
1c886 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56  arget = sqlite3V
1c887 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32  dbeIntValue(pIn2
1c888 29 3b 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64  );.    pC->rowid
1c889 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1c88a 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
1c88b 65 74 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62  eto = 1;.  }.  b
1c88c 72 65 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f  reak;.}.  ../* O
1c88d 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20  pcode: Found P1 
1c88e 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1c88f 52 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64  Register P3 hold
1c890 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75  s a blob constru
1c891 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f  cted by MakeReco
1c892 72 64 2e 20 20 50 31 20 69 73 20 61 6e 20 69 6e  rd.  P1 is an in
1c893 64 65 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 6e  dex..** If an en
1c894 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 73  try that matches
1c895 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1c896 67 69 73 74 65 72 20 70 33 20 65 78 69 73 74 73  gister p3 exists
1c897 20 69 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20 6a   in P1 then.** j
1c898 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20 74  ump to P2.  If t
1c899 68 65 20 50 33 20 76 61 6c 75 65 20 64 6f 65 73  he P3 value does
1c89a 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 65   not match any e
1c89b 6e 74 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74 68  ntry in P1.** th
1c89c 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54  en fall thru.  T
1c89d 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 20  he P1 cursor is 
1c89e 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
1c89f 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e   the matching en
1c8a0 74 72 79 0a 2a 2a 20 69 66 20 69 74 20 65 78 69  try.** if it exi
1c8a1 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  sts..**.** This 
1c8a2 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 75  instruction is u
1c8a3 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
1c8a4 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
1c8a5 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c 65   where the.** le
1c8a6 66 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20  ft-hand side is 
1c8a7 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
1c8a8 6e 74 2e 20 20 50 31 20 6d 61 79 20 62 65 20 61  nt.  P1 may be a
1c8a9 20 74 72 75 65 20 69 6e 64 65 78 2c 20 6f 72 20   true index, or 
1c8aa 69 74 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20 74  it.** may be a t
1c8ab 65 6d 70 6f 72 61 72 79 20 69 6e 64 65 78 20 74  emporary index t
1c8ac 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 72 65  hat holds the re
1c8ad 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c  sults of the SEL
1c8ae 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ECT.** statement
1c8af 2e 20 20 20 54 68 69 73 20 69 6e 73 74 72 75 63  .   This instruc
1c8b0 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65  tion is also use
1c8b1 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
1c8b2 68 65 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20 6b  he.** DISTINCT k
1c8b3 65 79 77 6f 72 64 20 69 6e 20 53 45 4c 45 43 54  eyword in SELECT
1c8b4 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
1c8b5 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
1c8b6 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 69 6e  ion checks if in
1c8b7 64 65 78 20 50 31 20 63 6f 6e 74 61 69 6e 73 20  dex P1 contains 
1c8b8 61 20 72 65 63 6f 72 64 20 66 6f 72 20 77 68 69  a record for whi
1c8b9 63 68 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  ch .** the first
1c8ba 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61   N serialized va
1c8bb 6c 75 65 73 20 65 78 61 63 74 6c 79 20 6d 61 74  lues exactly mat
1c8bc 63 68 20 74 68 65 20 4e 20 73 65 72 69 61 6c 69  ch the N seriali
1c8bd 7a 65 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e  zed values.** in
1c8be 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72   the record in r
1c8bf 65 67 69 73 74 65 72 20 50 33 2c 20 77 68 65 72  egister P3, wher
1c8c0 65 20 4e 20 69 73 20 74 68 65 20 74 6f 74 61 6c  e N is the total
1c8c1 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65   number of value
1c8c2 73 20 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20 72  s in.** the P3 r
1c8c3 65 63 6f 72 64 20 28 74 68 65 20 50 33 20 72 65  ecord (the P3 re
1c8c4 63 6f 72 64 20 69 73 20 61 20 70 72 65 66 69 78  cord is a prefix
1c8c5 20 6f 66 20 74 68 65 20 50 31 20 72 65 63 6f 72   of the P1 recor
1c8c6 64 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  d). .**.** See a
1c8c7 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 49  lso: NotFound, I
1c8c8 73 55 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69 73  sUnique, NotExis
1c8c9 74 73 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ts.*/./* Opcode:
1c8ca 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20   NotFound P1 P2 
1c8cb 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67  P3 * *.**.** Reg
1c8cc 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61  ister P3 holds a
1c8cd 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65   blob constructe
1c8ce 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e  d by MakeRecord.
1c8cf 20 20 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e    P1 is.** an in
1c8d0 64 65 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74 72  dex.  If no entr
1c8d1 79 20 65 78 69 73 74 73 20 69 6e 20 50 31 20 74  y exists in P1 t
1c8d2 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20  hat matches the 
1c8d3 62 6c 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a 2a  blob then jump.*
1c8d4 2a 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e 20  * to P2.  If an 
1c8d5 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69 73 74  entry does exist
1c8d6 69 6e 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  ing, fall throug
1c8d7 68 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 69  h.  The cursor i
1c8d8 73 20 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74 69  s left.** pointi
1c8d9 6e 67 20 74 6f 20 74 68 65 20 65 6e 74 72 79 20  ng to the entry 
1c8da 74 68 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a 2a  that matches..**
1c8db 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1c8dc 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20  und, NotExists, 
1c8dd 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65  IsUnique.*/.case
1c8de 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20   OP_NotFound:   
1c8df 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
1c8e0 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e   */.case OP_Foun
1c8e1 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  d: {        /* j
1c8e2 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e  ump, in3 */.  in
1c8e3 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
1c8e4 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73   int alreadyExis
1c8e5 74 73 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75  ts = 0;.  VdbeCu
1c8e6 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
1c8e7 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1c8e8 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
1c8e9 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
1c8ea 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  ]!=0 );.  if( (p
1c8eb 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
1c8ec 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a  ->pCursor!=0 ){.
1c8ed 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1c8ee 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1c8ef 2a 70 49 64 78 4b 65 79 3b 0a 0a 20 20 20 20 61  *pIdxKey;..    a
1c8f0 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62  ssert( pC->isTab
1c8f1 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  le==0 );.    ass
1c8f2 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73  ert( pIn3->flags
1c8f3 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20   & MEM_Blob );. 
1c8f4 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c     pIdxKey = sql
1c8f5 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e  ite3VdbeRecordUn
1c8f6 70 61 63 6b 28 70 43 2d 3e 70 4b 65 79 49 6e 66  pack(pC->pKeyInf
1c8f7 6f 2c 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33  o, pIn3->n, pIn3
1c8f8 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->z,.           
1c8f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c8fa 20 20 20 20 20 20 20 20 20 20 20 61 54 65 6d 70             aTemp
1c8fb 52 65 63 2c 20 73 69 7a 65 6f 66 28 61 54 65 6d  Rec, sizeof(aTem
1c8fc 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20  pRec));.    if( 
1c8fd 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20  pIdxKey==0 ){.  
1c8fe 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1c8ff 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1c900 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46  Op->opcode==OP_F
1c901 6f 75 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 49  ound ){.      pI
1c902 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20  dxKey->flags |= 
1c903 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f  UNPACKED_PREFIX_
1c904 4d 41 54 43 48 3b 0a 20 20 20 20 7d 0a 20 20 20  MATCH;.    }.   
1c905 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1c906 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
1c907 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 70 49  (pC->pCursor, pI
1c908 64 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 72 65  dxKey, 0, 0, &re
1c909 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  s);.    sqlite3V
1c90a 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65  dbeDeleteUnpacke
1c90b 64 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29  dRecord(pIdxKey)
1c90c 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1c90d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1c90e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1c90f 20 20 61 6c 72 65 61 64 79 45 78 69 73 74 73 20    alreadyExists 
1c910 3d 20 28 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20  = (res==0);.    
1c911 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1c912 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e  to = 0;.    pC->
1c913 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1c914 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20  CHE_STALE;.  }. 
1c915 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1c916 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20  ==OP_Found ){.  
1c917 20 20 69 66 28 20 61 6c 72 65 61 64 79 45 78 69    if( alreadyExi
1c918 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e  sts ) pc = pOp->
1c919 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  p2 - 1;.  }else{
1c91a 0a 20 20 20 20 69 66 28 20 21 61 6c 72 65 61 64  .    if( !alread
1c91b 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20 70  yExists ) pc = p
1c91c 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
1c91d 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c91e 70 63 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20  pcode: IsUnique 
1c91f 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
1c920 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73  .** Cursor P1 is
1c921 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 64 65   open on an inde
1c922 78 2e 20 20 53 6f 20 69 74 20 68 61 73 20 6e 6f  x.  So it has no
1c923 20 64 61 74 61 20 61 6e 64 20 69 74 73 20 6b 65   data and its ke
1c924 79 20 63 6f 6e 73 69 73 74 73 20 0a 2a 2a 20 6f  y consists .** o
1c925 66 20 61 20 72 65 63 6f 72 64 20 67 65 6e 65 72  f a record gener
1c926 61 74 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52  ated by OP_MakeR
1c927 65 63 6f 72 64 20 77 68 65 72 65 20 74 68 65 20  ecord where the 
1c928 6c 61 73 74 20 66 69 65 6c 64 20 69 73 20 74 68  last field is th
1c929 65 20 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74  e .** rowid of t
1c92a 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68  he entry that th
1c92b 65 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74  e index refers t
1c92c 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20  o..**.** The P3 
1c92d 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e  register contain
1c92e 73 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 63  s an integer rec
1c92f 6f 72 64 20 6e 75 6d 62 65 72 2e 20 43 61 6c 6c  ord number. Call
1c930 20 74 68 69 73 20 72 65 63 6f 72 64 20 0a 2a 2a   this record .**
1c931 20 6e 75 6d 62 65 72 20 52 2e 20 52 65 67 69 73   number R. Regis
1c932 74 65 72 20 50 34 20 69 73 20 74 68 65 20 66 69  ter P4 is the fi
1c933 72 73 74 20 69 6e 20 61 20 73 65 74 20 6f 66 20  rst in a set of 
1c934 4e 20 63 6f 6e 74 69 67 75 6f 75 73 20 72 65 67  N contiguous reg
1c935 69 73 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 6d  isters.** that m
1c936 61 6b 65 20 75 70 20 61 6e 20 75 6e 70 61 63 6b  ake up an unpack
1c937 65 64 20 69 6e 64 65 78 20 6b 65 79 20 74 68 61  ed index key tha
1c938 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69  t can be used wi
1c939 74 68 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a  th cursor P1..**
1c93a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 20   The value of N 
1c93b 63 61 6e 20 62 65 20 69 6e 66 65 72 72 65 64 20  can be inferred 
1c93c 66 72 6f 6d 20 74 68 65 20 63 75 72 73 6f 72 2e  from the cursor.
1c93d 20 4e 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20   N includes the 
1c93e 72 6f 77 69 64 0a 2a 2a 20 76 61 6c 75 65 20 61  rowid.** value a
1c93f 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65  ppended to the e
1c940 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  nd of the index 
1c941 72 65 63 6f 72 64 2e 20 54 68 69 73 20 72 6f 77  record. This row
1c942 69 64 20 76 61 6c 75 65 20 6d 61 79 0a 2a 2a 20  id value may.** 
1c943 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68  or may not be th
1c944 65 20 73 61 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a  e same as R..**.
1c945 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65  ** If any of the
1c946 20 4e 20 72 65 67 69 73 74 65 72 73 20 62 65 67   N registers beg
1c947 69 6e 6e 69 6e 67 20 77 69 74 68 20 72 65 67 69  inning with regi
1c948 73 74 65 72 20 50 34 20 63 6f 6e 74 61 69 6e 73  ster P4 contains
1c949 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65   a NULL.** value
1c94a 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  , jump immediate
1c94b 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ly to P2..**.** 
1c94c 4f 74 68 65 72 77 69 73 65 2c 20 74 68 69 73 20  Otherwise, this 
1c94d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63  instruction chec
1c94e 6b 73 20 69 66 20 63 75 72 73 6f 72 20 50 31 20  ks if cursor P1 
1c94f 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72  contains an entr
1c950 79 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 66  y.** where the f
1c951 69 72 73 74 20 28 4e 2d 31 29 20 66 69 65 6c 64  irst (N-1) field
1c952 73 20 6d 61 74 63 68 20 62 75 74 20 74 68 65 20  s match but the 
1c953 72 6f 77 69 64 20 76 61 6c 75 65 20 61 74 20 74  rowid value at t
1c954 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65  he end.** of the
1c955 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20   index entry is 
1c956 6e 6f 74 20 52 2e 20 49 66 20 74 68 65 72 65 20  not R. If there 
1c957 69 73 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79  is no such entry
1c958 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a  , control jumps.
1c959 2a 2a 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  ** to instructio
1c95a 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c  n P2. Otherwise,
1c95b 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
1c95c 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 69 6e  e conflicting in
1c95d 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20  dex.** entry is 
1c95e 63 6f 70 69 65 64 20 74 6f 20 72 65 67 69 73 74  copied to regist
1c95f 65 72 20 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f  er P3 and contro
1c960 6c 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20  l falls through 
1c961 74 6f 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69  to the next.** i
1c962 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
1c963 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46  * See also: NotF
1c964 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c  ound, NotExists,
1c965 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f   Found.*/.case O
1c966 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20  P_IsUnique: {   
1c967 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1c968 33 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a 20  3 */.  u16 ii;. 
1c969 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1c96a 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ;.  BtCursor *pC
1c96b 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65 6c  rsr;.  u16 nFiel
1c96c 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d  d;.  Mem *aMem =
1c96d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1c96e 34 2e 69 5d 3b 0a 0a 20 20 2f 2a 20 41 73 73 65  4.i];..  /* Asse
1c96f 72 74 20 74 68 61 74 20 74 68 65 20 76 61 6c 75  rt that the valu
1c970 65 73 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73  es of parameters
1c971 20 50 31 20 61 6e 64 20 50 34 20 61 72 65 20 69   P1 and P4 are i
1c972 6e 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73  n range. */.  as
1c973 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70  sert( pOp->p4typ
1c974 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20  e==P4_INT32 );. 
1c975 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1c976 2e 69 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e  .i>0 && pOp->p4.
1c977 69 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  i<=p->nMem );.  
1c978 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1c979 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
1c97a 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 0a 20 20 2f  >nCursor );..  /
1c97b 2a 20 46 69 6e 64 20 74 68 65 20 69 6e 64 65 78  * Find the index
1c97c 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 70 43   cursor. */.  pC
1c97d 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  x = p->apCsr[pOp
1c97e 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1c97f 20 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f   pCx->deferredMo
1c980 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 70 43 78  veto==0 );.  pCx
1c981 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 30  ->seekResult = 0
1c982 3b 0a 20 20 70 43 78 2d 3e 63 61 63 68 65 53 74  ;.  pCx->cacheSt
1c983 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1c984 4c 45 3b 0a 20 20 70 43 72 73 72 20 3d 20 70 43  LE;.  pCrsr = pC
1c985 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 0a 20 20 2f  x->pCursor;..  /
1c986 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20  * If any of the 
1c987 76 61 6c 75 65 73 20 61 72 65 20 4e 55 4c 4c 2c  values are NULL,
1c988 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20   take the jump. 
1c989 2a 2f 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 43  */.  nField = pC
1c98a 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69  x->pKeyInfo->nFi
1c98b 65 6c 64 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  eld;.  for(ii=0;
1c98c 20 69 69 3c 6e 46 69 65 6c 64 3b 20 69 69 2b 2b   ii<nField; ii++
1c98d 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 65 6d 5b  ){.    if( aMem[
1c98e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ii].flags & MEM_
1c98f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 63  Null ){.      pc
1c990 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1c991 20 20 20 20 20 20 70 43 72 73 72 20 3d 20 30 3b        pCrsr = 0;
1c992 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1c993 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
1c994 28 20 28 61 4d 65 6d 5b 6e 46 69 65 6c 64 5d 2e  ( (aMem[nField].
1c995 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1c996 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70  )==0 );..  if( p
1c997 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 55  Crsr!=0 ){.    U
1c998 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b  npackedRecord r;
1c999 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c99a 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65    /* B-Tree inde
1c99b 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a  x search key */.
1c99c 20 20 20 20 69 36 34 20 52 3b 20 20 20 20 20 20      i64 R;      
1c99d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c99e 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20         /* Rowid 
1c99f 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1c9a0 65 72 20 50 33 20 2a 2f 0a 0a 20 20 20 20 2f 2a  er P3 */..    /*
1c9a1 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e   Populate the in
1c9a2 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20  dex search key. 
1c9a3 2a 2f 0a 20 20 20 20 72 2e 70 4b 65 79 49 6e 66  */.    r.pKeyInf
1c9a4 6f 20 3d 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66  o = pCx->pKeyInf
1c9a5 6f 3b 0a 20 20 20 20 72 2e 6e 46 69 65 6c 64 20  o;.    r.nField 
1c9a6 3d 20 6e 46 69 65 6c 64 20 2b 20 31 3b 0a 20 20  = nField + 1;.  
1c9a7 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41    r.flags = UNPA
1c9a8 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52  CKED_PREFIX_SEAR
1c9a9 43 48 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d  CH;.    r.aMem =
1c9aa 20 61 4d 65 6d 3b 0a 0a 20 20 20 20 2f 2a 20 45   aMem;..    /* E
1c9ab 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65  xtract the value
1c9ac 20 6f 66 20 52 20 66 72 6f 6d 20 72 65 67 69 73   of R from regis
1c9ad 74 65 72 20 50 33 2e 20 2a 2f 0a 20 20 20 20 73  ter P3. */.    s
1c9ae 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1c9af 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20  egerify(pIn3);. 
1c9b0 20 20 20 52 20 3d 20 70 49 6e 33 2d 3e 75 2e 69     R = pIn3->u.i
1c9b1 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68  ;..    /* Search
1c9b2 20 74 68 65 20 42 2d 54 72 65 65 20 69 6e 64 65   the B-Tree inde
1c9b3 78 2e 20 49 66 20 6e 6f 20 63 6f 6e 66 6c 69 63  x. If no conflic
1c9b4 74 69 6e 67 20 72 65 63 6f 72 64 20 69 73 20 66  ting record is f
1c9b5 6f 75 6e 64 2c 20 6a 75 6d 70 0a 20 20 20 20 2a  ound, jump.    *
1c9b6 2a 20 74 6f 20 50 32 2e 20 4f 74 68 65 72 77 69  * to P2. Otherwi
1c9b7 73 65 2c 20 63 6f 70 79 20 74 68 65 20 72 6f 77  se, copy the row
1c9b8 69 64 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69  id of the confli
1c9b9 63 74 69 6e 67 20 72 65 63 6f 72 64 20 74 6f 0a  cting record to.
1c9ba 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20      ** register 
1c9bb 50 33 20 61 6e 64 20 66 61 6c 6c 20 74 68 72 6f  P3 and fall thro
1c9bc 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1c9bd 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 2a 2f  instruction.  */
1c9be 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1c9bf 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
1c9c0 63 6b 65 64 28 70 43 72 73 72 2c 20 26 72 2c 20  cked(pCrsr, &r, 
1c9c1 30 2c 20 30 2c 20 26 70 43 78 2d 3e 73 65 65 6b  0, 0, &pCx->seek
1c9c2 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 69 66 28  Result);.    if(
1c9c3 20 28 72 2e 66 6c 61 67 73 20 26 20 55 4e 50 41   (r.flags & UNPA
1c9c4 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52  CKED_PREFIX_SEAR
1c9c5 43 48 29 20 7c 7c 20 72 2e 72 6f 77 69 64 3d 3d  CH) || r.rowid==
1c9c6 52 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  R ){.      pc = 
1c9c7 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
1c9c8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49   }else{.      pI
1c9c9 6e 33 2d 3e 75 2e 69 20 3d 20 72 2e 72 6f 77 69  n3->u.i = r.rowi
1c9ca 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  d;.    }.  }.  b
1c9cb 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1c9cc 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31  de: NotExists P1
1c9cd 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1c9ce 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74   Use the content
1c9cf 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20   of register P3 
1c9d0 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79  as a integer key
1c9d1 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a  .  If a record .
1c9d2 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79  ** with that key
1c9d3 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
1c9d4 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20  in table of P1, 
1c9d5 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1c9d6 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f   .** If the reco
1c9d7 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74  rd does exist, t
1c9d8 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20  hen fall thru.  
1c9d9 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
1c9da 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20  ft .** pointing 
1c9db 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66  to the record if
1c9dc 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a   it exists..**.*
1c9dd 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  * The difference
1c9de 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70   between this op
1c9df 65 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46  eration and NotF
1c9e0 6f 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69  ound is that thi
1c9e1 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61  s.** operation a
1c9e2 73 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69  ssumes the key i
1c9e3 73 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64  s an integer and
1c9e4 20 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61   that P1 is a ta
1c9e5 62 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e  ble whereas.** N
1c9e6 6f 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20  otFound assumes 
1c9e7 6b 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f  key is a blob co
1c9e8 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d  nstructed from M
1c9e9 61 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a  akeRecord and.**
1c9ea 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e   P1 is an index.
1c9eb 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1c9ec 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64   Found, NotFound
1c9ed 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61  , IsUnique.*/.ca
1c9ee 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a  se OP_NotExists:
1c9ef 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1c9f0 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20  p, in3 */.  int 
1c9f1 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56  i = pOp->p1;.  V
1c9f2 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1c9f3 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1c9f4 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ;.  assert( i>=0
1c9f5 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72   && i<p->nCursor
1c9f6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1c9f7 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a  >apCsr[i]!=0 );.
1c9f8 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 28    if( (pCrsr = (
1c9f9 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1c9fa 29 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29  )->pCursor)!=0 )
1c9fb 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20  {.    int res = 
1c9fc 30 3b 0a 20 20 20 20 75 36 34 20 69 4b 65 79 3b  0;.    u64 iKey;
1c9fd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e  .    assert( pIn
1c9fe 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  3->flags & MEM_I
1c9ff 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nt );.    assert
1ca00 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d 3e 69  ( p->apCsr[i]->i
1ca01 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 69 4b  sTable );.    iK
1ca02 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70 49  ey = intToKey(pI
1ca03 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 72 63  n3->u.i);.    rc
1ca04 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
1ca05 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43  ovetoUnpacked(pC
1ca06 72 73 72 2c 20 30 2c 20 69 4b 65 79 2c 20 30 2c  rsr, 0, iKey, 0,
1ca07 20 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d 3e   &res);.    pC->
1ca08 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33  lastRowid = pIn3
1ca09 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 43 2d 3e 72  ->u.i;.    pC->r
1ca0a 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65  owidIsValid = re
1ca0b 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70  s==0 ?1:0;.    p
1ca0c 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a  C->nullRow = 0;.
1ca0d 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61      pC->cacheSta
1ca0e 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1ca0f 45 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72  E;.    pC->defer
1ca10 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
1ca11 20 20 20 69 66 28 20 72 65 73 21 3d 30 20 29 7b     if( res!=0 ){
1ca12 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
1ca13 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 61  >p2 - 1;.      a
1ca14 73 73 65 72 74 28 20 70 43 2d 3e 72 6f 77 69 64  ssert( pC->rowid
1ca15 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20  IsValid==0 );.  
1ca16 20 20 7d 0a 20 20 20 20 70 43 2d 3e 73 65 65 6b    }.    pC->seek
1ca17 52 65 73 75 6c 74 20 3d 20 72 65 73 3b 0a 20 20  Result = res;.  
1ca18 7d 65 6c 73 65 20 69 66 28 20 21 70 43 2d 3e 70  }else if( !pC->p
1ca19 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20  seudoTable ){.  
1ca1a 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e    /* This happen
1ca1b 73 20 77 68 65 6e 20 61 6e 20 61 74 74 65 6d 70  s when an attemp
1ca1c 74 20 74 6f 20 6f 70 65 6e 20 61 20 72 65 61 64  t to open a read
1ca1d 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 0a   cursor on the .
1ca1e 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61      ** sqlite_ma
1ca1f 73 74 65 72 20 74 61 62 6c 65 20 72 65 74 75 72  ster table retur
1ca20 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e  ns SQLITE_EMPTY.
1ca21 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
1ca22 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20  rt( pC->isTable 
1ca23 29 3b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  );.    pc = pOp-
1ca24 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73  >p2 - 1;.    ass
1ca25 65 72 74 28 20 70 43 2d 3e 72 6f 77 69 64 49 73  ert( pC->rowidIs
1ca26 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20  Valid==0 );.    
1ca27 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d  pC->seekResult =
1ca28 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   0;.  }.  break;
1ca29 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1ca2a 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a 20  equence P1 P2 * 
1ca2b 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74  * *.**.** Find t
1ca2c 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c  he next availabl
1ca2d 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  e sequence numbe
1ca2e 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e  r for cursor P1.
1ca2f 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 65  .** Write the se
1ca30 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e  quence number in
1ca31 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1ca32 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 20  ** The sequence 
1ca33 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 75  number on the cu
1ca34 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 6e  rsor is incremen
1ca35 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a 2a  ted after this.*
1ca36 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20  * instruction.  
1ca37 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75  .*/.case OP_Sequ
1ca38 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20  ence: {         
1ca39 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1ca3a 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20  ease */.  int i 
1ca3b 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73  = pOp->p1;.  ass
1ca3c 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
1ca3d 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1ca3e 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b  ssert( p->apCsr[
1ca3f 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d  i]!=0 );.  pOut-
1ca40 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b  >u.i = p->apCsr[
1ca41 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a  i]->seqCount++;.
1ca42 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1ca43 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1ca44 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a  .  break;.}.../*
1ca45 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69   Opcode: NewRowi
1ca46 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  d P1 P2 P3 * *.*
1ca47 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69  *.** Get a new i
1ca48 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75  nteger record nu
1ca49 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77  mber (a.k.a "row
1ca4a 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65  id") used as the
1ca4b 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e   key to a table.
1ca4c 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e  .** The record n
1ca4d 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65  umber is not pre
1ca4e 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20  viously used as 
1ca4f 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74  a key in the dat
1ca50 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74  abase.** table t
1ca51 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f  hat cursor P1 po
1ca52 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65  ints to.  The ne
1ca53 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  w record number 
1ca54 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72  is written.** wr
1ca55 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65  itten to registe
1ca56 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  r P2..**.** If P
1ca57 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61  3>0 then P3 is a
1ca58 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 68   register that h
1ca59 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 73 74  olds the largest
1ca5a 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 67   previously.** g
1ca5b 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20  enerated record 
1ca5c 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 77 20  number.  No new 
1ca5d 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61  record numbers a
1ca5e 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  re allowed to be
1ca5f 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68   less.** than th
1ca60 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 6e 20  is value.  When 
1ca61 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 68  this value reach
1ca62 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20  es its maximum, 
1ca63 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a  a SQLITE_FULL.**
1ca64 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61   error is genera
1ca65 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 65 67  ted.  The P3 reg
1ca66 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65 64  ister is updated
1ca67 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 72 61   with the genera
1ca68 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 6e 75  ted.** record nu
1ca69 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 20 6d  mber.  This P3 m
1ca6a 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64  echanism is used
1ca6b 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65   to help impleme
1ca6c 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e  nt the.** AUTOIN
1ca6d 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e  CREMENT feature.
1ca6e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52  .*/.case OP_NewR
1ca6f 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20  owid: {         
1ca70 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1ca71 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20  ease */.  int i 
1ca72 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34  = pOp->p1;.  i64
1ca73 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75   v = 0;.  VdbeCu
1ca74 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
1ca75 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1ca76 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
1ca77 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
1ca78 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  ]!=0 );.  if( (p
1ca79 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
1ca7a 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b 0a  ->pCursor==0 ){.
1ca7b 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20      /* The zero 
1ca7c 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61  initialization a
1ca7d 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74  bove is all that
1ca7e 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20   is needed */.  
1ca7f 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
1ca80 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20  e next rowid or 
1ca81 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64  record number (d
1ca82 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66  ifferent terms f
1ca83 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20  or the same.    
1ca84 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74  ** thing) is obt
1ca85 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73  ained in a two-s
1ca86 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20  tep algorithm.. 
1ca87 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72     **.    ** Fir
1ca88 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f  st we attempt to
1ca89 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73   find the larges
1ca8a 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
1ca8b 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20   and add one.   
1ca8c 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75   ** to that.  Bu
1ca8d 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 74  t if the largest
1ca8e 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20   existing rowid 
1ca8f 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d  is already the m
1ca90 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f  aximum.    ** po
1ca91 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20  sitive integer, 
1ca92 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20  we have to fall 
1ca93 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73  through to the s
1ca94 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f  econd.    ** pro
1ca95 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72  babilistic algor
1ca96 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ithm.    **.    
1ca97 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c  ** The second al
1ca98 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65  gorithm is to se
1ca99 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 20  lect a rowid at 
1ca9a 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69  random and see i
1ca9b 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65  f.    ** it alre
1ca9c 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 68  ady exists in th
1ca9d 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20  e table.  If it 
1ca9e 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
1ca9f 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73  we have.    ** s
1caa0 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 68  ucceeded.  If th
1caa1 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64  e random rowid d
1caa2 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 65  oes exist, we se
1caa3 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20  lect a new one. 
1caa4 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67     ** and try ag
1caa5 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30 20  ain, up to 1000 
1caa6 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20  times..    **.  
1caa7 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65    ** For a table
1caa8 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e 20   with less than 
1caa9 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69 65  2 billion entrie
1caaa 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69  s, the probabili
1caab 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f 74  ty.    ** of not
1caac 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73 65   finding a unuse
1caad 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75 74  d rowid is about
1caae 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69 73   1.0e-300.  This
1caaf 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e 6f   is a .    ** no
1cab0 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69  n-zero probabili
1cab1 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73 74  ty, but it is st
1cab2 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79 20  ill vanishingly 
1cab3 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c 64  small and should
1cab4 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63 61  .    ** never ca
1cab5 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20 20  use a problem.  
1cab6 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d 75  You are much, mu
1cab7 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74  ch more likely t
1cab8 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a 20  o have a.    ** 
1cab9 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72 65  hardware failure
1caba 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20 61   than for this a
1cabb 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69 6c  lgorithm to fail
1cabc 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1cabd 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e 20  The analysis in 
1cabe 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72  the previous par
1cabf 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20 74  agraph assumes t
1cac0 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20 67  hat you have a g
1cac1 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63  ood.    ** sourc
1cac2 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62  e of random numb
1cac3 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72 61  ers.  Is a libra
1cac4 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65  ry function like
1cac5 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20 2a   lrand48().    *
1cac6 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20 20  * good enough?  
1cac7 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f 74  Maybe. Maybe not
1cac8 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20 6b  . It's hard to k
1cac9 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65 72  now whether ther
1caca 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 62  e.    ** might b
1cacb 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69 73  e subtle bugs is
1cacc 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61   some implementa
1cacd 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34 38  tions of lrand48
1cace 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 63  () that.    ** c
1cacf 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c  ould cause probl
1cad0 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75 6e  ems. To avoid un
1cad1 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69 74  certainty, SQLit
1cad2 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20 0a  e uses its own .
1cad3 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e 75      ** random nu
1cad4 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62  mber generator b
1cad5 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34 20  ased on the RC4 
1cad6 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a  algorithm..    *
1cad7 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f 6d  *.    ** To prom
1cad8 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66 20  ote locality of 
1cad9 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72 65  reference for re
1cada 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74 73  petitive inserts
1cadb 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72  , the.    ** fir
1cadc 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73 20  st few attempts 
1cadd 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 72 61  at choosing a ra
1cade 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b 20  ndom rowid pick 
1cadf 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c 69  values just a li
1cae0 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67  ttle.    ** larg
1cae1 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 76  er than the prev
1cae2 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68 69  ious rowid.  Thi
1cae3 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77 6e  s has been shown
1cae4 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79 0a   experimentally.
1cae5 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c 65      ** to double
1cae6 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74 68   the speed of th
1cae7 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f 6e  e COPY operation
1cae8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
1cae9 20 72 65 73 3d 30 2c 20 72 78 3d 53 51 4c 49 54   res=0, rx=SQLIT
1caea 45 5f 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69  E_OK, cnt;.    i
1caeb 36 34 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20  64 x;.    cnt = 
1caec 30 3b 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69  0;.    if( (sqli
1caed 74 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43  te3BtreeFlags(pC
1caee 2d 3e 70 43 75 72 73 6f 72 29 26 28 42 54 52 45  ->pCursor)&(BTRE
1caef 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a  E_INTKEY|BTREE_Z
1caf0 45 52 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20  ERODATA)) !=.   
1caf1 20 20 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54         BTREE_INT
1caf2 4b 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20  KEY ){.      rc 
1caf3 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
1caf4 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74  _BKPT;.      got
1caf5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1caf6 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rror;.    }.    
1caf7 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33  assert( (sqlite3
1caf8 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70  BtreeFlags(pC->p
1caf9 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f  Cursor) & BTREE_
1cafa 49 4e 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20  INTKEY)!=0 );.  
1cafb 20 20 61 73 73 65 72 74 28 20 28 73 71 6c 69 74    assert( (sqlit
1cafc 65 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d  e3BtreeFlags(pC-
1cafd 3e 70 43 75 72 73 6f 72 29 20 26 20 42 54 52 45  >pCursor) & BTRE
1cafe 45 5f 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29  E_ZERODATA)==0 )
1caff 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1cb00 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20  _32BIT_ROWID.#  
1cb01 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49   define MAX_ROWI
1cb02 44 20 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c  D 0x7fffffff.#el
1cb03 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63  se.    /* Some c
1cb04 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69  ompilers complai
1cb05 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74  n about constant
1cb06 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78  s of the form 0x
1cb07 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  7fffffffffffffff
1cb08 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20  ..    ** Others 
1cb09 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30  complain about 0
1cb0a 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 66  x7ffffffffffffff
1cb0b 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  ffLL.  The follo
1cb0c 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73  wing macro seems
1cb0d 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69  .    ** to provi
1cb0e 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20  de the constant 
1cb0f 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c  while making all
1cb10 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79   compilers happy
1cb11 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66  ..    */.#   def
1cb12 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28  ine MAX_ROWID  (
1cb13 69 36 34 29 28 20 28 28 28 75 36 34 29 30 78 37  i64)( (((u64)0x7
1cb14 66 66 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20  fffffff)<<32) | 
1cb15 28 75 36 34 29 30 78 66 66 66 66 66 66 66 66 20  (u64)0xffffffff 
1cb16 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66  ).#endif..    if
1cb17 28 20 21 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d  ( !pC->useRandom
1cb18 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 76  Rowid ){.      v
1cb19 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
1cb1a 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 70 43  etCachedRowid(pC
1cb1b 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->pCursor);.    
1cb1c 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20    if( v==0 ){.  
1cb1d 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1cb1e 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d 3e  e3BtreeLast(pC->
1cb1f 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a  pCursor, &res);.
1cb20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1cb21 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1cb22 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
1cb23 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1cb24 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1cb25 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
1cb26 20 20 20 20 20 20 20 76 20 3d 20 31 3b 0a 20 20         v = 1;.  
1cb27 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1cb28 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
1cb29 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e 70  reeKeySize(pC->p
1cb2a 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20 20  Cursor, &v);.   
1cb2b 20 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54 6f         v = keyTo
1cb2c 49 6e 74 28 76 29 3b 0a 20 20 20 20 20 20 20 20  Int(v);.        
1cb2d 20 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52 4f 57    if( v==MAX_ROW
1cb2e 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ID ){.          
1cb2f 20 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52    pC->useRandomR
1cb30 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  owid = 1;.      
1cb31 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1cb32 20 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20         v++;.    
1cb33 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1cb34 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64  }.      }..#ifnd
1cb35 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1cb36 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20  UTOINCREMENT.   
1cb37 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29     if( pOp->p3 )
1cb38 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 2a 70  {.        Mem *p
1cb39 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 61 73 73  Mem;.        ass
1cb3a 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26  ert( pOp->p3>0 &
1cb3b 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  & pOp->p3<=p->nM
1cb3c 65 6d 20 29 3b 20 2f 2a 20 50 33 20 69 73 20 61  em ); /* P3 is a
1cb3d 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65   valid memory ce
1cb3e 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d  ll */.        pM
1cb3f 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  em = &p->aMem[pO
1cb40 70 2d 3e 70 33 5d 3b 0a 09 52 45 47 49 53 54 45  p->p3];..REGISTE
1cb41 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
1cb42 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20   pMem);.        
1cb43 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
1cb44 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a  tegerify(pMem);.
1cb45 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1cb46 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d  (pMem->flags & M
1cb47 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20 2f  EM_Int)!=0 );  /
1cb48 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 73 20  * mem(P3) holds 
1cb49 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20  an integer */.  
1cb4a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e        if( pMem->
1cb4b 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c  u.i==MAX_ROWID |
1cb4c 7c 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52  | pC->useRandomR
1cb4d 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20  owid ){.        
1cb4e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55    rc = SQLITE_FU
1cb4f 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  LL;.          go
1cb50 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1cb51 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d  error;.        }
1cb52 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3c 70  .        if( v<p
1cb53 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20  Mem->u.i+1 ){.  
1cb54 20 20 20 20 20 20 20 20 76 20 3d 20 70 4d 65 6d          v = pMem
1cb55 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20  ->u.i + 1;.     
1cb56 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 65     }.        pMe
1cb57 6d 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20  m->u.i = v;.    
1cb58 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
1cb59 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
1cb5a 74 43 61 63 68 65 64 52 6f 77 69 64 28 70 43 2d  tCachedRowid(pC-
1cb5b 3e 70 43 75 72 73 6f 72 2c 20 76 3c 4d 41 58 5f  >pCursor, v<MAX_
1cb5c 52 4f 57 49 44 20 3f 20 76 2b 31 20 3a 20 30 29  ROWID ? v+1 : 0)
1cb5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1cb5e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77  pC->useRandomRow
1cb5f 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  id ){.      asse
1cb60 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29  rt( pOp->p3==0 )
1cb61 3b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 55 4c  ;  /* SQLITE_FUL
1cb62 4c 20 6d 75 73 74 20 68 61 76 65 20 6f 63 63 75  L must have occu
1cb63 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68  rred prior to th
1cb64 69 73 20 2a 2f 0a 20 20 20 20 20 20 76 20 3d 20  is */.      v = 
1cb65 64 62 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69  db->priorNewRowi
1cb66 64 3b 0a 20 20 20 20 20 20 63 6e 74 20 3d 20 30  d;.      cnt = 0
1cb67 3b 0a 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20  ;.      do{.    
1cb68 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26      if( cnt==0 &
1cb69 26 20 28 76 26 30 78 66 66 66 66 66 66 29 3d 3d  & (v&0xffffff)==
1cb6a 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 76  v ){.          v
1cb6b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ++;.        }els
1cb6c 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  e{.          sql
1cb6d 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
1cb6e 73 69 7a 65 6f 66 28 76 29 2c 20 26 76 29 3b 0a  sizeof(v), &v);.
1cb6f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6e            if( cn
1cb70 74 3c 35 20 29 20 76 20 26 3d 20 30 78 66 66 66  t<5 ) v &= 0xfff
1cb71 66 66 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  fff;.        }. 
1cb72 20 20 20 20 20 20 20 69 66 28 20 76 3d 3d 30 20         if( v==0 
1cb73 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
1cb74 20 20 20 20 78 20 3d 20 69 6e 74 54 6f 4b 65 79      x = intToKey
1cb75 28 76 29 3b 0a 20 20 20 20 20 20 20 20 72 78 20  (v);.        rx 
1cb76 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
1cb77 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 2d  vetoUnpacked(pC-
1cb78 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 36  >pCursor, 0, (u6
1cb79 34 29 78 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20  4)x, 0, &res);. 
1cb7a 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20         cnt++;.  
1cb7b 20 20 20 20 7d 77 68 69 6c 65 28 20 63 6e 74 3c      }while( cnt<
1cb7c 31 30 30 20 26 26 20 72 78 3d 3d 53 51 4c 49 54  100 && rx==SQLIT
1cb7d 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20 29  E_OK && res==0 )
1cb7e 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 72 69 6f  ;.      db->prio
1cb7f 72 4e 65 77 52 6f 77 69 64 20 3d 20 76 3b 0a 20  rNewRowid = v;. 
1cb80 20 20 20 20 20 69 66 28 20 72 78 3d 3d 53 51 4c       if( rx==SQL
1cb81 49 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30  ITE_OK && res==0
1cb82 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1cb83 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20   SQLITE_FULL;.  
1cb84 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
1cb85 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1cb86 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1cb87 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69   pC->rowidIsVali
1cb88 64 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d 3e 64  d = 0;.    pC->d
1cb89 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1cb8a 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65  0;.    pC->cache
1cb8b 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1cb8c 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53  TALE;.  }.  MemS
1cb8d 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1cb8e 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70 4f 75   MEM_Int);.  pOu
1cb8f 74 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 62 72  t->u.i = v;.  br
1cb90 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cb91 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 32 20  e: Insert P1 P2 
1cb92 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57  P3 P4 P5.**.** W
1cb93 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  rite an entry in
1cb94 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20  to the table of 
1cb95 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 6e 65  cursor P1.  A ne
1cb96 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 63 72  w entry is.** cr
1cb97 65 61 74 65 64 20 69 66 20 69 74 20 64 6f 65 73  eated if it does
1cb98 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  n't already exis
1cb99 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 66 6f  t or the data fo
1cb9a 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a  r an existing.**
1cb9b 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 77 72   entry is overwr
1cb9c 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 74 61  itten.  The data
1cb9d 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 73 74   is the value st
1cb9e 6f 72 65 64 20 72 65 67 69 73 74 65 72 0a 2a 2a  ored register.**
1cb9f 20 6e 75 6d 62 65 72 20 50 32 2e 20 54 68 65 20   number P2. The 
1cba0 6b 65 79 20 69 73 20 73 74 6f 72 65 64 20 69 6e  key is stored in
1cba1 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 54 68   register P3. Th
1cba2 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a 20 62 65  e key must.** be
1cba3 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a   an integer..**.
1cba4 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47  ** If the OPFLAG
1cba5 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 6f 66  _NCHANGE flag of
1cba6 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e   P5 is set, then
1cba7 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20   the row change 
1cba8 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72  count is.** incr
1cba9 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 77 69  emented (otherwi
1cbaa 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74 68 65  se not).  If the
1cbab 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49   OPFLAG_LASTROWI
1cbac 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69 73 20  D flag of P5 is 
1cbad 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 6f 77  set,.** then row
1cbae 69 64 20 69 73 20 73 74 6f 72 65 64 20 66 6f 72  id is stored for
1cbaf 20 73 75 62 73 65 71 75 65 6e 74 20 72 65 74 75   subsequent retu
1cbb0 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73 71 6c  rn by the.** sql
1cbb1 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
1cbb2 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74 69 6f  _rowid() functio
1cbb3 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69 74 20  n (otherwise it 
1cbb4 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29 2e 0a  is unmodified)..
1cbb5 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20  **.** Parameter 
1cbb6 50 34 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20  P4 may point to 
1cbb7 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  a string contain
1cbb8 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2d 6e 61  ing the table-na
1cbb9 6d 65 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20 62 65  me, or.** may be
1cbba 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20   NULL. If it is 
1cbbb 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  not NULL, then t
1cbbc 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 0a  he update-hook .
1cbbd 2a 2a 20 28 73 71 6c 69 74 65 33 2e 78 55 70 64  ** (sqlite3.xUpd
1cbbe 61 74 65 43 61 6c 6c 62 61 63 6b 29 20 69 73 20  ateCallback) is 
1cbbf 69 6e 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e  invoked followin
1cbc0 67 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 69  g a successful i
1cbc1 6e 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41  nsert..**.** (WA
1cbc2 52 4e 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 50  RNING/TODO: If P
1cbc3 31 20 69 73 20 61 20 70 73 65 75 64 6f 2d 63 75  1 is a pseudo-cu
1cbc4 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73 20 64  rsor and P2 is d
1cbc5 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c  ynamically.** al
1cbc6 6c 6f 63 61 74 65 64 2c 20 74 68 65 6e 20 6f 77  located, then ow
1cbc7 6e 65 72 73 68 69 70 20 6f 66 20 50 32 20 69 73  nership of P2 is
1cbc8 20 74 72 61 6e 73 66 65 72 72 65 64 20 74 6f 20   transferred to 
1cbc9 74 68 65 20 70 73 65 75 64 6f 2d 63 75 72 73 6f  the pseudo-curso
1cbca 72 0a 2a 2a 20 61 6e 64 20 72 65 67 69 73 74 65  r.** and registe
1cbcb 72 20 50 32 20 62 65 63 6f 6d 65 73 20 65 70 68  r P2 becomes eph
1cbcc 65 6d 65 72 61 6c 2e 20 20 49 66 20 74 68 65 20  emeral.  If the 
1cbcd 63 75 72 73 6f 72 20 69 73 20 63 68 61 6e 67 65  cursor is change
1cbce 64 2c 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20  d, the.** value 
1cbcf 6f 66 20 72 65 67 69 73 74 65 72 20 50 32 20 77  of register P2 w
1cbd0 69 6c 6c 20 74 68 65 6e 20 63 68 61 6e 67 65 2e  ill then change.
1cbd1 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73    Make sure this
1cbd2 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 61 75   does not.** cau
1cbd3 73 65 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e  se any problems.
1cbd4 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73  ).**.** This ins
1cbd5 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f  truction only wo
1cbd6 72 6b 73 20 6f 6e 20 74 61 62 6c 65 73 2e 20 20  rks on tables.  
1cbd7 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 69  The equivalent i
1cbd8 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f  nstruction.** fo
1cbd9 72 20 69 6e 64 69 63 65 73 20 69 73 20 4f 50 5f  r indices is OP_
1cbda 49 64 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61  IdxInsert..*/.ca
1cbdb 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 7b 0a  se OP_Insert: {.
1cbdc 20 20 4d 65 6d 20 2a 70 44 61 74 61 20 3d 20 26    Mem *pData = &
1cbdd 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  p->aMem[pOp->p2]
1cbde 3b 0a 20 20 4d 65 6d 20 2a 70 4b 65 79 20 3d 20  ;.  Mem *pKey = 
1cbdf 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1cbe0 5d 3b 0a 0a 20 20 69 36 34 20 69 4b 65 79 3b 20  ];..  i64 iKey; 
1cbe1 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72    /* The integer
1cbe2 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f   ROWID or key fo
1cbe3 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20  r the record to 
1cbe4 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  be inserted */. 
1cbe5 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31   int i = pOp->p1
1cbe6 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
1cbe7 70 43 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e  pC;.  assert( i>
1cbe8 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
1cbe9 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e  or );.  pC = p->
1cbea 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65  apCsr[i];.  asse
1cbeb 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 61  rt( pC!=0 );.  a
1cbec 73 73 65 72 74 28 20 70 43 2d 3e 70 43 75 72 73  ssert( pC->pCurs
1cbed 6f 72 21 3d 30 20 7c 7c 20 70 43 2d 3e 70 73 65  or!=0 || pC->pse
1cbee 75 64 6f 54 61 62 6c 65 20 29 3b 0a 20 20 61 73  udoTable );.  as
1cbef 73 65 72 74 28 20 70 4b 65 79 2d 3e 66 6c 61 67  sert( pKey->flag
1cbf0 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  s & MEM_Int );. 
1cbf1 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54   assert( pC->isT
1cbf2 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49 53 54  able );.  REGIST
1cbf3 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32  ER_TRACE(pOp->p2
1cbf4 2c 20 70 44 61 74 61 29 3b 0a 20 20 52 45 47 49  , pData);.  REGI
1cbf5 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1cbf6 70 33 2c 20 70 4b 65 79 29 3b 0a 0a 20 20 69 4b  p3, pKey);..  iK
1cbf7 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28 70 4b  ey = intToKey(pK
1cbf8 65 79 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20  ey->u.i);.  if( 
1cbf9 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47  pOp->p5 & OPFLAG
1cbfa 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43  _NCHANGE ) p->nC
1cbfb 68 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 20 70  hange++;.  if( p
1cbfc 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
1cbfd 4c 41 53 54 52 4f 57 49 44 20 29 20 64 62 2d 3e  LASTROWID ) db->
1cbfe 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 4b 65 79  lastRowid = pKey
1cbff 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 70 44 61  ->u.i;.  if( pDa
1cc00 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ta->flags & MEM_
1cc01 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 44 61 74  Null ){.    pDat
1cc02 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 70 44  a->z = 0;.    pD
1cc03 61 74 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65  ata->n = 0;.  }e
1cc04 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
1cc05 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20   pData->flags & 
1cc06 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74  (MEM_Blob|MEM_St
1cc07 72 29 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  r) );.  }.  if( 
1cc08 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20  pC->pseudoTable 
1cc09 29 7b 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e  ){.    if( !pC->
1cc0a 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65  ephemPseudoTable
1cc0b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1cc0c 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 2d 3e  3DbFree(db, pC->
1cc0d 70 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20  pData);.    }.  
1cc0e 20 20 70 43 2d 3e 69 4b 65 79 20 3d 20 69 4b 65    pC->iKey = iKe
1cc0f 79 3b 0a 20 20 20 20 70 43 2d 3e 6e 44 61 74 61  y;.    pC->nData
1cc10 20 3d 20 70 44 61 74 61 2d 3e 6e 3b 0a 20 20 20   = pData->n;.   
1cc11 20 69 66 28 20 70 44 61 74 61 2d 3e 7a 3d 3d 70   if( pData->z==p
1cc12 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 7c 7c  Data->zMalloc ||
1cc13 20 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f   pC->ephemPseudo
1cc14 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70  Table ){.      p
1cc15 43 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61  C->pData = pData
1cc16 2d 3e 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ->z;.      if( !
1cc17 70 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54  pC->ephemPseudoT
1cc18 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
1cc19 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 3d 20  pData->flags &= 
1cc1a 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 20 20  ~MEM_Dyn;.      
1cc1b 20 20 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 7c    pData->flags |
1cc1c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20  = MEM_Ephem;.   
1cc1d 20 20 20 20 20 70 44 61 74 61 2d 3e 7a 4d 61 6c       pData->zMal
1cc1e 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  loc = 0;.      }
1cc1f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1cc20 20 20 70 43 2d 3e 70 44 61 74 61 20 3d 20 73 71    pC->pData = sq
1cc21 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 2d  lite3Malloc( pC-
1cc22 3e 6e 44 61 74 61 2b 32 20 29 3b 0a 20 20 20 20  >nData+2 );.    
1cc23 20 20 69 66 28 20 21 70 43 2d 3e 70 44 61 74 61    if( !pC->pData
1cc24 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1cc25 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43 2d        memcpy(pC-
1cc26 3e 70 44 61 74 61 2c 20 70 44 61 74 61 2d 3e 7a  >pData, pData->z
1cc27 2c 20 70 43 2d 3e 6e 44 61 74 61 29 3b 0a 20 20  , pC->nData);.  
1cc28 20 20 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43      pC->pData[pC
1cc29 2d 3e 6e 44 61 74 61 5d 20 3d 20 30 3b 0a 20 20  ->nData] = 0;.  
1cc2a 20 20 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43      pC->pData[pC
1cc2b 2d 3e 6e 44 61 74 61 2b 31 5d 20 3d 20 30 3b 0a  ->nData+1] = 0;.
1cc2c 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e 6e 75      }.    pC->nu
1cc2d 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c  llRow = 0;.  }el
1cc2e 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72  se{.    int nZer
1cc2f 6f 3b 0a 20 20 20 20 69 6e 74 20 73 65 65 6b 52  o;.    int seekR
1cc30 65 73 75 6c 74 20 3d 20 28 28 70 4f 70 2d 3e 70  esult = ((pOp->p
1cc31 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45 53 45  5 & OPFLAG_USESE
1cc32 45 4b 52 45 53 55 4c 54 29 20 3f 20 70 43 2d 3e  EKRESULT) ? pC->
1cc33 73 65 65 6b 52 65 73 75 6c 74 20 3a 20 30 29 3b  seekResult : 0);
1cc34 0a 20 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e  .    if( pData->
1cc35 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
1cc36 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20   ){.      nZero 
1cc37 3d 20 70 44 61 74 61 2d 3e 75 2e 6e 5a 65 72 6f  = pData->u.nZero
1cc38 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1cc39 20 20 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20     nZero = 0;.  
1cc3a 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42    }.    sqlite3B
1cc3b 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77  treeSetCachedRow
1cc3c 69 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  id(pC->pCursor, 
1cc3d 30 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  0);.    rc = sql
1cc3e 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28  ite3BtreeInsert(
1cc3f 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  pC->pCursor, 0, 
1cc40 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20  iKey,.          
1cc41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cc42 20 20 70 44 61 74 61 2d 3e 7a 2c 20 70 44 61 74    pData->z, pDat
1cc43 61 2d 3e 6e 2c 20 6e 5a 65 72 6f 2c 0a 20 20 20  a->n, nZero,.   
1cc44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cc45 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 35           pOp->p5
1cc46 20 26 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44   & OPFLAG_APPEND
1cc47 2c 20 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 20  , seekResult.   
1cc48 20 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 43 2d   );.  }.  .  pC-
1cc49 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1cc4a 30 3b 0a 20 20 70 43 2d 3e 64 65 66 65 72 72 65  0;.  pC->deferre
1cc4b 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 70  dMoveto = 0;.  p
1cc4c 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1cc4d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20   CACHE_STALE;.. 
1cc4e 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75   /* Invoke the u
1cc4f 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65  pdate-hook if re
1cc50 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28  quired. */.  if(
1cc51 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1cc52 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c  & db->xUpdateCal
1cc53 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34  lback && pOp->p4
1cc54 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  .z ){.    const 
1cc55 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e  char *zDb = db->
1cc56 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61  aDb[pC->iDb].zNa
1cc57 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  me;.    const ch
1cc58 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e  ar *zTbl = pOp->
1cc59 70 34 2e 7a 3b 0a 20 20 20 20 69 6e 74 20 6f 70  p4.z;.    int op
1cc5a 20 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f   = ((pOp->p5 & O
1cc5b 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 29 20  PFLAG_ISUPDATE) 
1cc5c 3f 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20  ? SQLITE_UPDATE 
1cc5d 3a 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 29  : SQLITE_INSERT)
1cc5e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ;.    assert( pC
1cc5f 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20  ->isTable );.   
1cc60 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c   db->xUpdateCall
1cc61 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65  back(db->pUpdate
1cc62 41 72 67 2c 20 6f 70 2c 20 7a 44 62 2c 20 7a 54  Arg, op, zDb, zT
1cc63 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20 20 20 61  bl, iKey);.    a
1cc64 73 73 65 72 74 28 20 70 43 2d 3e 69 44 62 3e 3d  ssert( pC->iDb>=
1cc65 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  0 );.  }.  break
1cc66 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cc67 44 65 6c 65 74 65 20 50 31 20 50 32 20 2a 20 50  Delete P1 P2 * P
1cc68 34 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65  4 *.**.** Delete
1cc69 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 20 77   the record at w
1cc6a 68 69 63 68 20 74 68 65 20 50 31 20 63 75 72 73  hich the P1 curs
1cc6b 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  or is currently 
1cc6c 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  pointing..**.** 
1cc6d 54 68 65 20 63 75 72 73 6f 72 20 77 69 6c 6c 20  The cursor will 
1cc6e 62 65 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  be left pointing
1cc6f 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 6e   at either the n
1cc70 65 78 74 20 6f 72 20 74 68 65 20 70 72 65 76 69  ext or the previ
1cc71 6f 75 73 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e  ous.** record in
1cc72 20 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 69   the table. If i
1cc73 74 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  t is left pointi
1cc74 6e 67 20 61 74 20 74 68 65 20 6e 65 78 74 20 72  ng at the next r
1cc75 65 63 6f 72 64 2c 20 74 68 65 6e 0a 2a 2a 20 74  ecord, then.** t
1cc76 68 65 20 6e 65 78 74 20 4e 65 78 74 20 69 6e 73  he next Next ins
1cc77 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65  truction will be
1cc78 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 65 6e 63 65   a no-op.  Hence
1cc79 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 64 65 6c   it is OK to del
1cc7a 65 74 65 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20  ete.** a record 
1cc7b 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 4e  from within an N
1cc7c 65 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20  ext loop..**.** 
1cc7d 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43  If the OPFLAG_NC
1cc7e 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 32  HANGE flag of P2
1cc7f 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68   is set, then th
1cc80 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75  e row change cou
1cc81 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65  nt is.** increme
1cc82 6e 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20  nted (otherwise 
1cc83 6e 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d  not)..**.** P1 m
1cc84 75 73 74 20 6e 6f 74 20 62 65 20 70 73 65 75 64  ust not be pseud
1cc85 6f 2d 74 61 62 6c 65 2e 20 20 49 74 20 68 61 73  o-table.  It has
1cc86 20 74 6f 20 62 65 20 61 20 72 65 61 6c 20 74 61   to be a real ta
1cc87 62 6c 65 20 77 69 74 68 0a 2a 2a 20 6d 75 6c 74  ble with.** mult
1cc88 69 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a  iple rows..**.**
1cc89 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55   If P4 is not NU
1cc8a 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74  LL, then it is t
1cc8b 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
1cc8c 61 62 6c 65 20 74 68 61 74 20 50 31 20 69 73 0a  able that P1 is.
1cc8d 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  ** pointing to. 
1cc8e 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   The update hook
1cc8f 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64   will be invoked
1cc90 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a  , if it exists..
1cc91 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20  ** If P4 is not 
1cc92 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 50 31  NULL then the P1
1cc93 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76   cursor must hav
1cc94 65 20 62 65 65 6e 20 70 6f 73 69 74 69 6f 6e 65  e been positione
1cc95 64 0a 2a 2a 20 75 73 69 6e 67 20 4f 50 5f 4e 6f  d.** using OP_No
1cc96 74 46 6f 75 6e 64 20 70 72 69 6f 72 20 74 6f 20  tFound prior to 
1cc97 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 6f 70  invoking this op
1cc98 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  code..*/.case OP
1cc99 5f 44 65 6c 65 74 65 3a 20 7b 0a 20 20 69 6e 74  _Delete: {.  int
1cc9a 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20   i = pOp->p1;.  
1cc9b 69 36 34 20 69 4b 65 79 20 3d 20 30 3b 0a 20 20  i64 iKey = 0;.  
1cc9c 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1cc9d 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1cc9e 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  && i<p->nCursor 
1cc9f 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43  );.  pC = p->apC
1cca0 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28  sr[i];.  assert(
1cca1 20 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65   pC!=0 );.  asse
1cca2 72 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21  rt( pC->pCursor!
1cca3 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76  =0 );  /* Only v
1cca4 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74 61  alid for real ta
1cca5 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74  bles, no pseudot
1cca6 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49  ables */..  /* I
1cca7 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f  f the update-hoo
1cca8 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65  k will be invoke
1cca9 64 2c 20 73 65 74 20 69 4b 65 79 20 74 6f 20 74  d, set iKey to t
1ccaa 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 0a  he rowid of the.
1ccab 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e 67 20 64    ** row being d
1ccac 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  eleted..  */.  i
1ccad 66 28 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61  f( db->xUpdateCa
1ccae 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70  llback && pOp->p
1ccaf 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 73 65 72  4.z ){.    asser
1ccb0 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 20 29  t( pC->isTable )
1ccb1 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ;.    assert( pC
1ccb2 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29  ->rowidIsValid )
1ccb3 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20  ;  /* lastRowid 
1ccb4 73 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20  set by previous 
1ccb5 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20  OP_NotFound */. 
1ccb6 20 20 20 69 4b 65 79 20 3d 20 70 43 2d 3e 6c 61     iKey = pC->la
1ccb7 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a 0a 20 20  stRowid;.  }..  
1ccb8 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1ccb9 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29  CursorMoveto(pC)
1ccba 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74  ;.  if( rc ) got
1ccbb 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1ccbc 72 72 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 42  rror;.  sqlite3B
1ccbd 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77  treeSetCachedRow
1ccbe 69 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  id(pC->pCursor, 
1ccbf 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  0);.  rc = sqlit
1ccc0 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 70 43  e3BtreeDelete(pC
1ccc1 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 70 43  ->pCursor);.  pC
1ccc2 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1ccc3 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20  CACHE_STALE;..  
1ccc4 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70  /* Invoke the up
1ccc5 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71  date-hook if req
1ccc6 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20  uired. */.  if( 
1ccc7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1ccc8 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c   db->xUpdateCall
1ccc9 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e  back && pOp->p4.
1ccca 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  z ){.    const c
1cccb 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
1cccc 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d  Db[pC->iDb].zNam
1cccd 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
1ccce 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70  r *zTbl = pOp->p
1cccf 34 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70  4.z;.    db->xUp
1ccd0 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d  dateCallback(db-
1ccd1 3e 70 55 70 64 61 74 65 41 72 67 2c 20 53 51 4c  >pUpdateArg, SQL
1ccd2 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 44 62 2c  ITE_DELETE, zDb,
1ccd3 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b 0a 20 20   zTbl, iKey);.  
1ccd4 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 44    assert( pC->iD
1ccd5 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66  b>=0 );.  }.  if
1ccd6 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46 4c  ( pOp->p2 & OPFL
1ccd7 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e  AG_NCHANGE ) p->
1ccd8 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72 65  nChange++;.  bre
1ccd9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1ccda 3a 20 52 65 73 65 74 43 6f 75 6e 74 20 50 31 20  : ResetCount P1 
1ccdb 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  * *.**.** This o
1ccdc 70 63 6f 64 65 20 72 65 73 65 74 73 20 74 68 65  pcode resets the
1ccdd 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68   VMs internal ch
1ccde 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20  ange counter to 
1ccdf 30 2e 20 49 66 20 50 31 20 69 73 20 74 72 75 65  0. If P1 is true
1cce0 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61  ,.** then the va
1cce1 6c 75 65 20 6f 66 20 74 68 65 20 63 68 61 6e 67  lue of the chang
1cce2 65 20 63 6f 75 6e 74 65 72 20 69 73 20 63 6f 70  e counter is cop
1cce3 69 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62  ied to the datab
1cce4 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 63 68  ase handle.** ch
1cce5 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 28 72 65  ange counter (re
1cce6 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71  turned by subseq
1cce7 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71  uent calls to sq
1cce8 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 29  lite3_changes())
1cce9 0a 2a 2a 20 62 65 66 6f 72 65 20 69 74 20 69 73  .** before it is
1ccea 20 72 65 73 65 74 2e 20 54 68 69 73 20 69 73 20   reset. This is 
1cceb 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20  used by trigger 
1ccec 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73  programs..*/.cas
1cced 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 3a  e OP_ResetCount:
1ccee 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31   {.  if( pOp->p1
1ccef 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1ccf0 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62  dbeSetChanges(db
1ccf1 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20  , p->nChange);. 
1ccf2 20 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20   }.  p->nChange 
1ccf3 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  = 0;.  break;.}.
1ccf4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44  ./* Opcode: RowD
1ccf5 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ata P1 P2 * * *.
1ccf6 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f  **.** Write into
1ccf7 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68 65   register P2 the
1ccf8 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 61   complete row da
1ccf9 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31  ta for cursor P1
1ccfa 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f  ..** There is no
1ccfb 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20   interpretation 
1ccfc 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a  of the data.  .*
1ccfd 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70  * It is just cop
1ccfe 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 20  ied onto the P2 
1ccff 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79  register exactly
1cd00 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f   as .** it is fo
1cd01 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  und in the datab
1cd02 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
1cd03 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72  If the P1 cursor
1cd04 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e   must be pointin
1cd05 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77  g to a valid row
1cd06 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77   (not a NULL row
1cd07 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74  ).** of a real t
1cd08 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75  able, not a pseu
1cd09 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20  do-table..*/./* 
1cd0a 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50  Opcode: RowKey P
1cd0b 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1cd0c 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69   Write into regi
1cd0d 73 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70  ster P2 the comp
1cd0e 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72  lete row key for
1cd0f 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54   cursor P1..** T
1cd10 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72  here is no inter
1cd11 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  pretation of the
1cd12 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20   data.  .** The 
1cd13 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e  key is copied on
1cd14 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74  to the P3 regist
1cd15 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a  er exactly as .*
1cd16 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e  * it is found in
1cd17 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1cd18 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le..**.** If the
1cd19 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20   P1 cursor must 
1cd1a 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61  be pointing to a
1cd1b 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20   valid row (not 
1cd1c 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f  a NULL row).** o
1cd1d 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20  f a real table, 
1cd1e 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62  not a pseudo-tab
1cd1f 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  le..*/.case OP_R
1cd20 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52  owKey:.case OP_R
1cd21 6f 77 44 61 74 61 3a 20 7b 0a 20 20 69 6e 74 20  owData: {.  int 
1cd22 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56  i = pOp->p1;.  V
1cd23 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1cd24 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1cd25 3b 0a 20 20 75 33 32 20 6e 3b 0a 0a 20 20 70 4f  ;.  u32 n;..  pO
1cd26 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1cd27 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f  p->p2];..  /* No
1cd28 74 65 20 74 68 61 74 20 52 6f 77 4b 65 79 20 61  te that RowKey a
1cd29 6e 64 20 52 6f 77 44 61 74 61 20 61 72 65 20 72  nd RowData are r
1cd2a 65 61 6c 6c 79 20 65 78 61 63 74 6c 79 20 74 68  eally exactly th
1cd2b 65 20 73 61 6d 65 20 69 6e 73 74 72 75 63 74 69  e same instructi
1cd2c 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  on */.  assert( 
1cd2d 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1cd2e 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70  rsor );.  pC = p
1cd2f 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73  ->apCsr[i];.  as
1cd30 73 65 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c  sert( pC->isTabl
1cd31 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  e || pOp->opcode
1cd32 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20  ==OP_RowKey );. 
1cd33 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 49   assert( pC->isI
1cd34 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63  ndex || pOp->opc
1cd35 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20  ode==OP_RowData 
1cd36 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21  );.  assert( pC!
1cd37 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1cd38 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29  pC->nullRow==0 )
1cd39 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e  ;.  assert( pC->
1cd3a 70 73 65 75 64 6f 54 61 62 6c 65 3d 3d 30 20 29  pseudoTable==0 )
1cd3b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e  ;.  assert( pC->
1cd3c 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20  pCursor!=0 );.  
1cd3d 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72  pCrsr = pC->pCur
1cd3e 73 6f 72 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  sor;.  rc = sqli
1cd3f 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
1cd40 65 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72  eto(pC);.  if( r
1cd41 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  c ) goto abort_d
1cd42 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 69  ue_to_error;.  i
1cd43 66 28 20 70 43 2d 3e 69 73 49 6e 64 65 78 20 29  f( pC->isIndex )
1cd44 7b 0a 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20  {.    i64 n64;. 
1cd45 20 20 20 61 73 73 65 72 74 28 20 21 70 43 2d 3e     assert( !pC->
1cd46 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 73  isTable );.    s
1cd47 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
1cd48 7a 65 28 70 43 72 73 72 2c 20 26 6e 36 34 29 3b  ze(pCrsr, &n64);
1cd49 0a 20 20 20 20 69 66 28 20 6e 36 34 3e 64 62 2d  .    if( n64>db-
1cd4a 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1cd4b 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
1cd4c 20 20 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62        goto too_b
1cd4d 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 20  ig;.    }.    n 
1cd4e 3d 20 28 69 6e 74 29 6e 36 34 3b 0a 20 20 7d 65  = (int)n64;.  }e
1cd4f 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
1cd50 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 70 43  BtreeDataSize(pC
1cd51 72 73 72 2c 20 26 6e 29 3b 0a 20 20 20 20 69 66  rsr, &n);.    if
1cd52 28 20 28 69 6e 74 29 6e 3e 64 62 2d 3e 61 4c 69  ( (int)n>db->aLi
1cd53 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
1cd54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
1cd55 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1cd56 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
1cd57 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1cd58 6f 77 28 70 4f 75 74 2c 20 6e 2c 20 30 29 20 29  ow(pOut, n, 0) )
1cd59 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
1cd5a 6d 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e  m;.  }.  pOut->n
1cd5b 20 3d 20 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79   = n;.  MemSetTy
1cd5c 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1cd5d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20 70 43  _Blob);.  if( pC
1cd5e 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20  ->isIndex ){.   
1cd5f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1cd60 65 65 4b 65 79 28 70 43 72 73 72 2c 20 30 2c 20  eeKey(pCrsr, 0, 
1cd61 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d  n, pOut->z);.  }
1cd62 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73  else{.    rc = s
1cd63 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
1cd64 70 43 72 73 72 2c 20 30 2c 20 6e 2c 20 70 4f 75  pCrsr, 0, n, pOu
1cd65 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20 20 70 4f 75  t->z);.  }.  pOu
1cd66 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  t->enc = SQLITE_
1cd67 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73  UTF8;  /* In cas
1cd68 65 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76  e the blob is ev
1cd69 65 72 20 63 61 73 74 20 74 6f 20 74 65 78 74 20  er cast to text 
1cd6a 2a 2f 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  */.  UPDATE_MAX_
1cd6b 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1cd6c 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1cd6d 70 63 6f 64 65 3a 20 52 6f 77 69 64 20 50 31 20  pcode: Rowid P1 
1cd6e 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  P2 * * *.**.** S
1cd6f 74 6f 72 65 20 69 6e 20 72 65 67 69 73 74 65 72  tore in register
1cd70 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77   P2 an integer w
1cd71 68 69 63 68 20 69 73 20 74 68 65 20 6b 65 79 20  hich is the key 
1cd72 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74  of the table ent
1cd73 72 79 20 74 68 61 74 0a 2a 2a 20 50 31 20 69 73  ry that.** P1 is
1cd74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1cd75 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 63 61   to..**.** P1 ca
1cd76 6e 20 62 65 20 65 69 74 68 65 72 20 61 6e 20 6f  n be either an o
1cd77 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f 72  rdinary table or
1cd78 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1cd79 2e 20 20 54 68 65 72 65 20 75 73 65 64 20 74 6f  .  There used to
1cd7a 0a 2a 2a 20 62 65 20 61 20 73 65 70 61 72 61 74  .** be a separat
1cd7b 65 20 4f 50 5f 56 52 6f 77 69 64 20 6f 70 63 6f  e OP_VRowid opco
1cd7c 64 65 20 66 6f 72 20 75 73 65 20 77 69 74 68 20  de for use with 
1cd7d 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2c 20  virtual tables, 
1cd7e 62 75 74 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 20  but this.** one 
1cd7f 6f 70 63 6f 64 65 20 6e 6f 77 20 77 6f 72 6b 73  opcode now works
1cd80 20 66 6f 72 20 62 6f 74 68 20 74 61 62 6c 65 20   for both table 
1cd81 74 79 70 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f  types..*/.case O
1cd82 50 5f 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20  P_Rowid: {      
1cd83 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
1cd84 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
1cd85 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
1cd86 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p1;.  VdbeCursor
1cd87 20 2a 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a 0a   *pC;.  i64 v;..
1cd88 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1cd89 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1cd8a 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73  ;.  pC = p->apCs
1cd8b 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  r[i];.  assert( 
1cd8c 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  pC!=0 );.  if( p
1cd8d 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20  C->nullRow ){.  
1cd8e 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20    /* Do nothing 
1cd8f 73 6f 20 74 68 61 74 20 72 65 67 5b 50 32 5d 20  so that reg[P2] 
1cd90 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 20 2a 2f 0a  remains NULL */.
1cd91 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 65 6c      break;.  }el
1cd92 73 65 20 69 66 28 20 70 43 2d 3e 64 65 66 65 72  se if( pC->defer
1cd93 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 20  redMoveto ){.   
1cd94 20 76 20 3d 20 70 43 2d 3e 6d 6f 76 65 74 6f 54   v = pC->movetoT
1cd95 61 72 67 65 74 3b 0a 20 20 7d 65 6c 73 65 20 69  arget;.  }else i
1cd96 66 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  f( pC->pseudoTab
1cd97 6c 65 20 29 7b 0a 20 20 20 20 76 20 3d 20 6b 65  le ){.    v = ke
1cd98 79 54 6f 49 6e 74 28 70 43 2d 3e 69 4b 65 79 29  yToInt(pC->iKey)
1cd99 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1cd9a 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1cd9b 4c 45 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  LE.  }else if( p
1cd9c 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29  C->pVtabCursor )
1cd9d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74  {.    sqlite3_vt
1cd9e 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 63  ab *pVtab;.    c
1cd9f 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1cda0 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20  ule *pModule;.  
1cda1 20 20 70 56 74 61 62 20 3d 20 70 43 2d 3e 70 56    pVtab = pC->pV
1cda2 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1cda3 3b 0a 20 20 20 20 70 4d 6f 64 75 6c 65 20 3d 20  ;.    pModule = 
1cda4 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  pVtab->pModule;.
1cda5 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 6f 64      assert( pMod
1cda6 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b 0a 20  ule->xRowid );. 
1cda7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1cda8 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1cda9 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1cdaa 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d  misuse;.    rc =
1cdab 20 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64   pModule->xRowid
1cdac 28 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  (pC->pVtabCursor
1cdad 2c 20 26 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  , &v);.    sqlit
1cdae 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1cdaf 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d  zErrMsg);.    p-
1cdb0 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61 62  >zErrMsg = pVtab
1cdb1 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70  ->zErrMsg;.    p
1cdb2 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
1cdb3 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  0;.    if( sqlit
1cdb4 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1cdb5 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1cdb6 74 6f 5f 6d 69 73 75 73 65 3b 0a 23 65 6e 64 69  to_misuse;.#endi
1cdb7 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1cdb8 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1cdb9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
1cdba 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
1cdbb 72 73 6f 72 4d 6f 76 65 74 6f 28 70 43 29 3b 0a  rsorMoveto(pC);.
1cdbc 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
1cdbd 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1cdbe 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 43  rror;.    if( pC
1cdbf 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29  ->rowidIsValid )
1cdc0 7b 0a 20 20 20 20 20 20 76 20 3d 20 70 43 2d 3e  {.      v = pC->
1cdc1 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 7d  lastRowid;.    }
1cdc2 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
1cdc3 72 74 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21  rt( pC->pCursor!
1cdc4 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
1cdc5 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
1cdc6 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 76 29  pC->pCursor, &v)
1cdc7 3b 0a 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54  ;.      v = keyT
1cdc8 6f 49 6e 74 28 76 29 3b 0a 20 20 20 20 7d 0a 20  oInt(v);.    }. 
1cdc9 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d   }.  pOut->u.i =
1cdca 20 76 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65   v;.  MemSetType
1cdcb 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1cdcc 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  nt);.  break;.}.
1cdcd 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c  ./* Opcode: Null
1cdce 52 6f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  Row P1 * * * *.*
1cdcf 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  *.** Move the cu
1cdd0 72 73 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c  rsor P1 to a nul
1cdd1 6c 20 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43  l row.  Any OP_C
1cdd2 6f 6c 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73  olumn operations
1cdd3 0a 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20 77  .** that occur w
1cdd4 68 69 6c 65 20 74 68 65 20 63 75 72 73 6f 72 20  hile the cursor 
1cdd5 69 73 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72  is on the null r
1cdd6 6f 77 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a  ow will always.*
1cdd7 2a 20 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a  * write a NULL..
1cdd8 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52  */.case OP_NullR
1cdd9 6f 77 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20  ow: {.  int i = 
1cdda 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43  pOp->p1;.  VdbeC
1cddb 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73  ursor *pC;..  as
1cddc 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1cddd 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1cdde 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1cddf 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
1cde0 30 20 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52  0 );.  pC->nullR
1cde1 6f 77 20 3d 20 31 3b 0a 20 20 70 43 2d 3e 72 6f  ow = 1;.  pC->ro
1cde2 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1cde3 20 20 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f    if( pC->pCurso
1cde4 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
1cde5 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72  BtreeClearCursor
1cde6 28 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20  (pC->pCursor);. 
1cde7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1cde8 2a 20 4f 70 63 6f 64 65 3a 20 4c 61 73 74 20 50  * Opcode: Last P
1cde9 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1cdea 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66   The next use of
1cdeb 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f   the Rowid or Co
1cdec 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73  lumn or Next ins
1cded 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20  truction for P1 
1cdee 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74  .** will refer t
1cdef 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
1cdf0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1cdf1 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e   table or index.
1cdf2 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65  .** If the table
1cdf3 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70   or index is emp
1cdf4 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65  ty and P2>0, the
1cdf5 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  n jump immediate
1cdf6 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20  ly to P2..** If 
1cdf7 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68  P2 is 0 or if th
1cdf8 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
1cdf9 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66   is not empty, f
1cdfa 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  all through.** t
1cdfb 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
1cdfc 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
1cdfd 63 61 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20  case OP_Last: { 
1cdfe 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
1cdff 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
1ce00 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  >p1;.  VdbeCurso
1ce01 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f  r *pC;.  BtCurso
1ce02 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20  r *pCrsr;.  int 
1ce03 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  res;..  assert( 
1ce04 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1ce05 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70  rsor );.  pC = p
1ce06 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73  ->apCsr[i];.  as
1ce07 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20  sert( pC!=0 );. 
1ce08 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75   pCrsr = pC->pCu
1ce09 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20  rsor;.  assert( 
1ce0a 70 43 72 73 72 21 3d 30 20 29 3b 0a 20 20 72 63  pCrsr!=0 );.  rc
1ce0b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c   = sqlite3BtreeL
1ce0c 61 73 74 28 70 43 72 73 72 2c 20 26 72 65 73 29  ast(pCrsr, &res)
1ce0d 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  ;.  pC->nullRow 
1ce0e 3d 20 28 75 38 29 72 65 73 3b 0a 20 20 70 43 2d  = (u8)res;.  pC-
1ce0f 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1ce10 3d 20 30 3b 0a 20 20 70 43 2d 3e 72 6f 77 69 64  = 0;.  pC->rowid
1ce11 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 70  IsValid = 0;.  p
1ce12 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1ce13 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1ce14 69 66 28 20 72 65 73 20 26 26 20 70 4f 70 2d 3e  if( res && pOp->
1ce15 70 32 3e 30 20 29 7b 0a 20 20 20 20 70 63 20 3d  p2>0 ){.    pc =
1ce16 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1ce17 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f  }.  break;.}.../
1ce18 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74 20 50  * Opcode: Sort P
1ce19 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1ce1a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65   This opcode doe
1ce1b 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61  s exactly the sa
1ce1c 6d 65 20 74 68 69 6e 67 20 61 73 20 4f 50 5f 52  me thing as OP_R
1ce1d 65 77 69 6e 64 20 65 78 63 65 70 74 20 74 68 61  ewind except tha
1ce1e 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d 65 6e  t.** it incremen
1ce1f 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74  ts an undocument
1ce20 65 64 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  ed global variab
1ce21 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  le used for test
1ce22 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 74 69  ing..**.** Sorti
1ce23 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68  ng is accomplish
1ce24 65 64 20 62 79 20 77 72 69 74 69 6e 67 20 72 65  ed by writing re
1ce25 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 73 6f 72  cords into a sor
1ce26 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a 2a 20 74  ting index,.** t
1ce27 68 65 6e 20 72 65 77 69 6e 64 69 6e 67 20 74 68  hen rewinding th
1ce28 61 74 20 69 6e 64 65 78 20 61 6e 64 20 70 6c 61  at index and pla
1ce29 79 69 6e 67 20 69 74 20 62 61 63 6b 20 66 72 6f  ying it back fro
1ce2a 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a  m beginning to.*
1ce2b 2a 20 65 6e 64 2e 20 20 57 65 20 75 73 65 20 74  * end.  We use t
1ce2c 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64  he OP_Sort opcod
1ce2d 65 20 69 6e 73 74 65 61 64 20 6f 66 20 4f 50 5f  e instead of OP_
1ce2e 52 65 77 69 6e 64 20 74 6f 20 64 6f 20 74 68 65  Rewind to do the
1ce2f 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67 20 73 6f  .** rewinding so
1ce30 20 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c   that the global
1ce31 20 76 61 72 69 61 62 6c 65 20 77 69 6c 6c 20 62   variable will b
1ce32 65 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 6e  e incremented an
1ce33 64 0a 2a 2a 20 72 65 67 72 65 73 73 69 6f 6e 20  d.** regression 
1ce34 74 65 73 74 73 20 63 61 6e 20 64 65 74 65 72 6d  tests can determ
1ce35 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
1ce36 6f 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72  ot the optimizer
1ce37 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79   is.** correctly
1ce38 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f 75 74 20   optimizing out 
1ce39 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f  sorts..*/.case O
1ce3a 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20 20 20 20  P_Sort: {       
1ce3b 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 64   /* jump */.#ifd
1ce3c 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
1ce3d 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f   sqlite3_sort_co
1ce3e 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  unt++;.  sqlite3
1ce3f 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d 2d 3b  _search_count--;
1ce40 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 61 43 6f  .#endif.  p->aCo
1ce41 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f 53 54 4d  unter[SQLITE_STM
1ce42 54 53 54 41 54 55 53 5f 53 4f 52 54 2d 31 5d 2b  TSTATUS_SORT-1]+
1ce43 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72  +;.  /* Fall thr
1ce44 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 52 65 77  ough into OP_Rew
1ce45 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f  ind */.}./* Opco
1ce46 64 65 3a 20 52 65 77 69 6e 64 20 50 31 20 50 32  de: Rewind P1 P2
1ce47 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65   * * *.**.** The
1ce48 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68 65   next use of the
1ce49 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e   Rowid or Column
1ce4a 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75 63   or Next instruc
1ce4b 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20  tion for P1 .** 
1ce4c 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74 68  will refer to th
1ce4d 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e  e first entry in
1ce4e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
1ce4f 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a  ble or index..**
1ce50 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   If the table or
1ce51 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20   index is empty 
1ce52 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a  and P2>0, then j
1ce53 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
1ce54 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20  to P2..** If P2 
1ce55 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74  is 0 or if the t
1ce56 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73  able or index is
1ce57 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c   not empty, fall
1ce58 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74   through.** to t
1ce59 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73  he following ins
1ce5a 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  truction..*/.cas
1ce5b 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20 7b 20 20  e OP_Rewind: {  
1ce5c 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1ce5d 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
1ce5e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p1;.  VdbeCursor
1ce5f 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
1ce60 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72   *pCrsr;.  int r
1ce61 65 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  es;..  assert( i
1ce62 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
1ce63 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d  sor );.  pC = p-
1ce64 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73  >apCsr[i];.  ass
1ce65 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20  ert( pC!=0 );.  
1ce66 69 66 28 20 28 70 43 72 73 72 20 3d 20 70 43 2d  if( (pCrsr = pC-
1ce67 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a  >pCursor)!=0 ){.
1ce68 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ce69 42 74 72 65 65 46 69 72 73 74 28 70 43 72 73 72  BtreeFirst(pCrsr
1ce6a 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d  , &res);.    pC-
1ce6b 3e 61 74 46 69 72 73 74 20 3d 20 72 65 73 3d 3d  >atFirst = res==
1ce6c 30 20 3f 31 3a 30 3b 0a 20 20 20 20 70 43 2d 3e  0 ?1:0;.    pC->
1ce6d 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d  deferredMoveto =
1ce6e 20 30 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68   0;.    pC->cach
1ce6f 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1ce70 53 54 41 4c 45 3b 0a 20 20 20 20 70 43 2d 3e 72  STALE;.    pC->r
1ce71 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
1ce72 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
1ce73 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 43 2d  s = 1;.  }.  pC-
1ce74 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72  >nullRow = (u8)r
1ce75 65 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  es;.  assert( pO
1ce76 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p2>0 && pOp->
1ce77 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69  p2<p->nOp );.  i
1ce78 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63  f( res ){.    pc
1ce79 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1ce7a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1ce7b 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20  /* Opcode: Next 
1ce7c 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1ce7d 2a 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f 72  * Advance cursor
1ce7e 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70   P1 so that it p
1ce7f 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65 78  oints to the nex
1ce80 74 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20  t key/data pair 
1ce81 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20  in its.** table 
1ce82 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68  or index.  If th
1ce83 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20  ere are no more 
1ce84 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20  key/value pairs 
1ce85 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67  then fall throug
1ce86 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c  h.** to the foll
1ce87 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f  owing instructio
1ce88 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63  n.  But if the c
1ce89 75 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77 61  ursor advance wa
1ce8a 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a  s successful,.**
1ce8b 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c   jump immediatel
1ce8c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54  y to P2..**.** T
1ce8d 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73  he P1 cursor mus
1ce8e 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20  t be for a real 
1ce8f 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65  table, not a pse
1ce90 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  udo-table..**.**
1ce91 20 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a   See also: Prev.
1ce92 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72  */./* Opcode: Pr
1ce93 65 76 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ev P1 P2 * * *.*
1ce94 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75 72  *.** Back up cur
1ce95 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69  sor P1 so that i
1ce96 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
1ce97 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61 74  previous key/dat
1ce98 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a  a pair in its.**
1ce99 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e   table or index.
1ce9a 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
1ce9b 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76 61   previous key/va
1ce9c 6c 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66  lue pairs then f
1ce9d 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  all through.** t
1ce9e 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
1ce9f 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75  instruction.  Bu
1cea0 74 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20  t if the cursor 
1cea1 62 61 63 6b 75 70 20 77 61 73 20 73 75 63 63 65  backup was succe
1cea2 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69  ssful,.** jump i
1cea3 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32  mmediately to P2
1cea4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63  ..**.** The P1 c
1cea5 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f  ursor must be fo
1cea6 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20  r a real table, 
1cea7 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62  not a pseudo-tab
1cea8 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50  le..*/.case OP_P
1cea9 72 65 76 3a 20 20 20 20 20 20 20 20 20 20 2f 2a  rev:          /*
1ceaa 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50   jump */.case OP
1ceab 5f 4e 65 78 74 3a 20 7b 20 20 20 20 20 20 20 20  _Next: {        
1ceac 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 56 64 62  /* jump */.  Vdb
1cead 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1ceae 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1ceaf 20 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 43 48    int res;..  CH
1ceb0 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50  ECK_FOR_INTERRUP
1ceb1 54 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  T;.  assert( pOp
1ceb2 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1ceb3 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1ceb4 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  .  pC = p->apCsr
1ceb5 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28  [pOp->p1];.  if(
1ceb6 20 70 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72   pC==0 ){.    br
1ceb7 65 61 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69 63  eak;  /* See tic
1ceb8 6b 65 74 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d  ket #2273 */.  }
1ceb9 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70  .  pCrsr = pC->p
1ceba 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74  Cursor;.  assert
1cebb 28 20 70 43 72 73 72 20 29 3b 0a 20 20 72 65 73  ( pCrsr );.  res
1cebc 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20   = 1;.  assert( 
1cebd 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1cebe 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20  to==0 );.  rc = 
1cebf 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
1cec0 4e 65 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74  Next ? sqlite3Bt
1cec1 72 65 65 4e 65 78 74 28 70 43 72 73 72 2c 20 26  reeNext(pCrsr, &
1cec2 72 65 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20  res) :.         
1cec3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cec4 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1cec5 65 50 72 65 76 69 6f 75 73 28 70 43 72 73 72 2c  ePrevious(pCrsr,
1cec6 20 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75   &res);.  pC->nu
1cec7 6c 6c 52 6f 77 20 3d 20 28 75 38 29 72 65 73 3b  llRow = (u8)res;
1cec8 0a 20 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74  .  pC->cacheStat
1cec9 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1ceca 3b 0a 20 20 69 66 28 20 72 65 73 3d 3d 30 20 29  ;.  if( res==0 )
1cecb 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  {.    pc = pOp->
1cecc 70 32 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20  p2 - 1;.    if( 
1cecd 70 4f 70 2d 3e 70 35 20 29 20 70 2d 3e 61 43 6f  pOp->p5 ) p->aCo
1cece 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 35 2d 31 5d  unter[pOp->p5-1]
1cecf 2b 2b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ++;.#ifdef SQLIT
1ced0 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
1ced1 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b  e3_search_count+
1ced2 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  +;.#endif.  }.  
1ced3 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1ced4 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1ced5 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78  ../* Opcode: Idx
1ced6 49 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 20  Insert P1 P2 P3 
1ced7 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73  * P5.**.** Regis
1ced8 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61 20 53  ter P2 holds a S
1ced9 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61 64  QL index key mad
1ceda 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 4d  e using the.** M
1cedb 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75  akeRecord instru
1cedc 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f 70  ctions.  This op
1cedd 63 6f 64 65 20 77 72 69 74 65 73 20 74 68 61 74  code writes that
1cede 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68 65   key.** into the
1cedf 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74 61   index P1.  Data
1cee0 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 69   for the entry i
1cee1 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33 20  s nil..**.** P3 
1cee2 69 73 20 61 20 66 6c 61 67 20 74 68 61 74 20 70  is a flag that p
1cee3 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20 74  rovides a hint t
1cee4 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 79  o the b-tree lay
1cee5 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20  er that this.** 
1cee6 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c 79  insert is likely
1cee7 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64   to be an append
1cee8 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73  ..**.** This ins
1cee9 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f  truction only wo
1ceea 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73 2e  rks for indices.
1ceeb 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e 74    The equivalent
1ceec 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20   instruction.** 
1ceed 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f 50  for tables is OP
1ceee 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 65  _Insert..*/.case
1ceef 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20 7b   OP_IdxInsert: {
1cef0 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a          /* in2 *
1cef1 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  /.  int i = pOp-
1cef2 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  >p1;.  VdbeCurso
1cef3 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f  r *pC;.  BtCurso
1cef4 72 20 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65  r *pCrsr;.  asse
1cef5 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1cef6 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
1cef7 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
1cef8 5d 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ]!=0 );.  assert
1cef9 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20  ( pIn2->flags & 
1cefa 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 69 66  MEM_Blob );.  if
1cefb 28 20 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d  ( (pCrsr = (pC =
1cefc 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70   p->apCsr[i])->p
1cefd 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20  Cursor)!=0 ){.  
1cefe 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73    assert( pC->is
1ceff 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20  Table==0 );.    
1cf00 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28  rc = ExpandBlob(
1cf01 70 49 6e 32 29 3b 0a 20 20 20 20 69 66 28 20 72  pIn2);.    if( r
1cf02 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1cf03 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20 3d        int nKey =
1cf04 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 20   pIn2->n;.      
1cf05 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79  const char *zKey
1cf06 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20 20   = pIn2->z;.    
1cf07 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1cf08 72 65 65 49 6e 73 65 72 74 28 70 43 72 73 72 2c  reeInsert(pCrsr,
1cf09 20 7a 4b 65 79 2c 20 6e 4b 65 79 2c 20 22 22 2c   zKey, nKey, "",
1cf0a 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 2c 20   0, 0, pOp->p3, 
1cf0b 0a 20 20 20 20 20 20 20 20 20 20 28 28 70 4f 70  .          ((pOp
1cf0c 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53  ->p5 & OPFLAG_US
1cf0d 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 70  ESEEKRESULT) ? p
1cf0e 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3a 20  C->seekResult : 
1cf0f 30 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  0).      );.    
1cf10 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65    assert( pC->de
1cf11 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20  ferredMoveto==0 
1cf12 29 3b 0a 20 20 20 20 20 20 70 43 2d 3e 63 61 63  );.      pC->cac
1cf13 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1cf14 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20  _STALE;.    }.  
1cf15 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1cf16 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65   Opcode: IdxDele
1cf17 74 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  te P1 P2 P3 * *.
1cf18 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e  **.** The conten
1cf19 74 20 6f 66 20 50 33 20 72 65 67 69 73 74 65 72  t of P3 register
1cf1a 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65  s starting at re
1cf1b 67 69 73 74 65 72 20 50 32 20 66 6f 72 6d 0a 2a  gister P2 form.*
1cf1c 2a 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e  * an unpacked in
1cf1d 64 65 78 20 6b 65 79 2e 20 54 68 69 73 20 6f 70  dex key. This op
1cf1e 63 6f 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61  code removes tha
1cf1f 74 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  t entry from the
1cf20 20 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e 65   .** index opene
1cf21 64 20 62 79 20 63 75 72 73 6f 72 20 50 31 2e 0a  d by cursor P1..
1cf22 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 65  */.case OP_IdxDe
1cf23 6c 65 74 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20  lete: {.  int i 
1cf24 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62  = pOp->p1;.  Vdb
1cf25 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1cf26 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1cf27 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1cf28 33 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  3>0 );.  assert(
1cf29 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f   pOp->p2>0 && pO
1cf2a 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3c 3d 70  p->p2+pOp->p3<=p
1cf2b 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 61 73  ->nMem+1 );.  as
1cf2c 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1cf2d 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1cf2e 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
1cf2f 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  [i]!=0 );.  if( 
1cf30 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20 70  (pCrsr = (pC = p
1cf31 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75  ->apCsr[i])->pCu
1cf32 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20  rsor)!=0 ){.    
1cf33 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 55 6e 70  int res;.    Unp
1cf34 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20  ackedRecord r;. 
1cf35 20 20 20 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20     r.pKeyInfo = 
1cf36 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  pC->pKeyInfo;.  
1cf37 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31    r.nField = (u1
1cf38 36 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 72  6)pOp->p3;.    r
1cf39 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20  .flags = 0;.    
1cf3a 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65  r.aMem = &p->aMe
1cf3b 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1cf3c 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1cf3d 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1cf3e 70 43 72 73 72 2c 20 26 72 2c 20 30 2c 20 30 2c  pCrsr, &r, 0, 0,
1cf3f 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20   &res);.    if( 
1cf40 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1cf41 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20   res==0 ){.     
1cf42 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1cf43 65 65 44 65 6c 65 74 65 28 70 43 72 73 72 29 3b  eeDelete(pCrsr);
1cf44 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
1cf45 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  t( pC->deferredM
1cf46 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20  oveto==0 );.    
1cf47 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1cf48 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1cf49 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1cf4a 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 52 6f 77  * Opcode: IdxRow
1cf4b 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  id P1 P2 * * *.*
1cf4c 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20  *.** Write into 
1cf4d 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69  register P2 an i
1cf4e 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20  nteger which is 
1cf4f 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
1cf50 6e 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a  n the record at.
1cf51 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  ** the end of th
1cf52 65 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e  e index key poin
1cf53 74 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72  ted to by cursor
1cf54 20 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67   P1.  This integ
1cf55 65 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  er should be.** 
1cf56 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
1cf57 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20   table entry to 
1cf58 77 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78  which this index
1cf59 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a   entry points..*
1cf5a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52  *.** See also: R
1cf5b 6f 77 69 64 2c 20 4d 61 6b 65 52 65 63 6f 72 64  owid, MakeRecord
1cf5c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78  ..*/.case OP_Idx
1cf5d 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20  Rowid: {        
1cf5e 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1cf5f 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e  erelease */.  in
1cf60 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
1cf61 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1cf62 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
1cf63 70 43 3b 0a 0a 0a 20 20 61 73 73 65 72 74 28 20  pC;...  assert( 
1cf64 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75  i>=0 && i<p->nCu
1cf65 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74  rsor );.  assert
1cf66 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30  ( p->apCsr[i]!=0
1cf67 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72 73 72   );.  if( (pCrsr
1cf68 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73   = (pC = p->apCs
1cf69 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 29 21  r[i])->pCursor)!
1cf6a 3d 30 20 29 7b 0a 20 20 20 20 69 36 34 20 72 6f  =0 ){.    i64 ro
1cf6b 77 69 64 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  wid;.    rc = sq
1cf6c 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
1cf6d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 20 20 69  oveto(pC);.    i
1cf6e 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f  f( rc ) goto abo
1cf6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1cf70 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1cf71 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d  >deferredMoveto=
1cf72 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
1cf73 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30  ( pC->isTable==0
1cf74 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 2d   );.    if( !pC-
1cf75 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20  >nullRow ){.    
1cf76 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1cf77 62 65 49 64 78 52 6f 77 69 64 28 70 43 72 73 72  beIdxRowid(pCrsr
1cf78 2c 20 26 72 6f 77 69 64 29 3b 0a 20 20 20 20 20  , &rowid);.     
1cf79 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1cf7a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
1cf7b 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1cf7c 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20  error;.      }. 
1cf7d 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46       MemSetTypeF
1cf7e 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1cf7f 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  t);.      pOut->
1cf80 75 2e 69 20 3d 20 72 6f 77 69 64 3b 0a 20 20 20  u.i = rowid;.   
1cf81 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a   }.  }.  break;.
1cf82 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  }../* Opcode: Id
1cf83 78 47 45 20 50 31 20 50 32 20 50 33 20 50 34 20  xGE P1 P2 P3 P4 
1cf84 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20  P5.**.** The P4 
1cf85 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20  register values 
1cf86 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50  beginning with P
1cf87 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b  3 form an unpack
1cf88 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79  ed index .** key
1cf89 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20   that omits the 
1cf8a 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20  ROWID.  Compare 
1cf8b 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61  this key value a
1cf8c 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78  gainst the index
1cf8d 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20   .** that P1 is 
1cf8e 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
1cf8f 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20  ng to, ignoring 
1cf90 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65  the ROWID on the
1cf91 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a   P1 index..**.**
1cf92 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78   If the P1 index
1cf93 20 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65   entry is greate
1cf94 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
1cf95 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  to the key value
1cf96 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  .** then jump to
1cf97 20 50 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20   P2.  Otherwise 
1cf98 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20  fall through to 
1cf99 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
1cf9a 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  tion..**.** If P
1cf9b 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68  5 is non-zero th
1cf9c 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  en the key value
1cf9d 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79   is increased by
1cf9e 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20   an epsilon .** 
1cf9f 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d  prior to the com
1cfa0 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d  parison.  This m
1cfa1 61 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77  ake the opcode w
1cfa2 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65  ork like IdxGT e
1cfa3 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66  xcept.** that if
1cfa4 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65   the key from re
1cfa5 67 69 73 74 65 72 20 50 33 20 69 73 20 61 20 70  gister P3 is a p
1cfa6 72 65 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79  refix of the key
1cfa7 20 69 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a   in the cursor,.
1cfa8 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  ** the result is
1cfa9 20 66 61 6c 73 65 20 77 68 65 72 65 61 73 20 69   false whereas i
1cfaa 74 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20  t would be true 
1cfab 77 69 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f  with IdxGT..*/./
1cfac 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20  * Opcode: IdxLT 
1cfad 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a  P1 P2 P3 * P5.**
1cfae 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73  .** The P4 regis
1cfaf 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e  ter values begin
1cfb0 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72  ning with P3 for
1cfb1 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e  m an unpacked in
1cfb2 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74  dex .** key that
1cfb3 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44   omits the ROWID
1cfb4 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20  .  Compare this 
1cfb5 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73  key value agains
1cfb6 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20  t the index .** 
1cfb7 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65  that P1 is curre
1cfb8 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  ntly pointing to
1cfb9 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52  , ignoring the R
1cfba 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69  OWID on the P1 i
1cfbb 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ndex..**.** If t
1cfbc 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72  he P1 index entr
1cfbd 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74  y is less than t
1cfbe 68 65 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65  he key value the
1cfbf 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1cfc0 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20   Otherwise fall 
1cfc1 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
1cfc2 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
1cfc3 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20  .**.** If P5 is 
1cfc4 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68  non-zero then th
1cfc5 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69  e key value is i
1cfc6 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65  ncreased by an e
1cfc7 70 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a  psilon prior .**
1cfc8 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73   to the comparis
1cfc9 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20  on.  This makes 
1cfca 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20  the opcode work 
1cfcb 6c 69 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63  like IdxLE..*/.c
1cfcc 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20  ase OP_IdxLT:   
1cfcd 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1cfce 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  in3 */.case OP_I
1cfcf 64 78 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f  dxGE: {        /
1cfd0 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20  * jump, in3 */. 
1cfd1 20 69 6e 74 20 69 3d 20 70 4f 70 2d 3e 70 31 3b   int i= pOp->p1;
1cfd2 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1cfd3 43 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  C;..  assert( i>
1cfd4 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
1cfd5 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  or );.  assert( 
1cfd6 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29  p->apCsr[i]!=0 )
1cfd7 3b 0a 20 20 69 66 28 20 28 70 43 20 3d 20 70 2d  ;.  if( (pC = p-
1cfd8 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72  >apCsr[i])->pCur
1cfd9 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  sor!=0 ){.    in
1cfda 74 20 72 65 73 3b 0a 20 20 20 20 55 6e 70 61 63  t res;.    Unpac
1cfdb 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20  kedRecord r;.   
1cfdc 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66   assert( pC->def
1cfdd 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
1cfde 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ;.    assert( pO
1cfdf 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d  p->p5==0 || pOp-
1cfe0 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73  >p5==1 );.    as
1cfe1 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70  sert( pOp->p4typ
1cfe2 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20  e==P4_INT32 );. 
1cfe3 20 20 20 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20     r.pKeyInfo = 
1cfe4 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  pC->pKeyInfo;.  
1cfe5 20 20 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31    r.nField = (u1
1cfe6 36 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20  6)pOp->p4.i;.   
1cfe7 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a   if( pOp->p5 ){.
1cfe8 20 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20        r.flags = 
1cfe9 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
1cfea 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f   | UNPACKED_IGNO
1cfeb 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65  RE_ROWID;.    }e
1cfec 6c 73 65 7b 0a 20 20 20 20 20 20 72 2e 66 6c 61  lse{.      r.fla
1cfed 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 47  gs = UNPACKED_IG
1cfee 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20  NORE_ROWID;.    
1cfef 7d 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d 20 26  }.    r.aMem = &
1cff0 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1cff1 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1cff2 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70  e3VdbeIdxKeyComp
1cff3 61 72 65 28 70 43 2c 20 26 72 2c 20 26 72 65 73  are(pC, &r, &res
1cff4 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  );.    if( pOp->
1cff5 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54  opcode==OP_IdxLT
1cff6 20 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20   ){.      res = 
1cff7 2d 72 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  -res;.    }else{
1cff8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1cff9 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49  Op->opcode==OP_I
1cffa 64 78 47 45 20 29 3b 0a 20 20 20 20 20 20 72 65  dxGE );.      re
1cffb 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  s++;.    }.    i
1cffc 66 28 20 72 65 73 3e 30 20 29 7b 0a 20 20 20 20  f( res>0 ){.    
1cffd 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1cffe 20 31 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   1 ;.    }.  }. 
1cfff 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d000 63 6f 64 65 3a 20 44 65 73 74 72 6f 79 20 50 31  code: Destroy P1
1d001 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1d002 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72   Delete an entir
1d003 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  e database table
1d004 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20   or index whose 
1d005 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65  root page in the
1d006 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
1d007 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 31  e is given by P1
1d008 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c  ..**.** The tabl
1d009 65 20 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65  e being destroye
1d00a 64 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e  d is in the main
1d00b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1d00c 66 20 50 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20  f P3==0.  If.** 
1d00d 50 33 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74  P3==1 then the t
1d00e 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72  able to be clear
1d00f 20 69 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c   is in the auxil
1d010 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69  iary database fi
1d011 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73  le.** that is us
1d012 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c  ed to store tabl
1d013 65 73 20 63 72 65 61 74 65 20 75 73 69 6e 67 20  es create using 
1d014 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59  CREATE TEMPORARY
1d015 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66   TABLE..**.** If
1d016 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65   AUTOVACUUM is e
1d017 6e 61 62 6c 65 64 20 74 68 65 6e 20 69 74 20 69  nabled then it i
1d018 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20  s possible that 
1d019 61 6e 6f 74 68 65 72 20 72 6f 6f 74 20 70 61 67  another root pag
1d01a 65 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f  e.** might be mo
1d01b 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6e 65 77  ved into the new
1d01c 6c 79 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 20  ly deleted root 
1d01d 70 61 67 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  page in order to
1d01e 20 6b 65 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f   keep all.** roo
1d01f 74 20 70 61 67 65 73 20 63 6f 6e 74 69 67 75 6f  t pages contiguo
1d020 75 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  us at the beginn
1d021 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62  ing of the datab
1d022 61 73 65 2e 20 20 54 68 65 20 66 6f 72 6d 65 72  ase.  The former
1d023 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65  .** value of the
1d024 20 72 6f 6f 74 20 70 61 67 65 20 74 68 61 74 20   root page that 
1d025 6d 6f 76 65 64 20 2d 20 69 74 73 20 76 61 6c 75  moved - its valu
1d026 65 20 62 65 66 6f 72 65 20 74 68 65 20 6d 6f 76  e before the mov
1d027 65 20 6f 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20  e occurred -.** 
1d028 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
1d029 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 6e 6f  ister P2.  If no
1d02a 20 70 61 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65   page .** moveme
1d02b 6e 74 20 77 61 73 20 72 65 71 75 69 72 65 64 20  nt was required 
1d02c 28 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62  (because the tab
1d02d 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64  le being dropped
1d02e 20 77 61 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a   was already .**
1d02f 20 74 68 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e   the last one in
1d030 20 74 68 65 20 64 61 74 61 62 61 73 65 29 20 74   the database) t
1d031 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74  hen a zero is st
1d032 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1d033 20 50 32 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56   P2..** If AUTOV
1d034 41 43 55 55 4d 20 69 73 20 64 69 73 61 62 6c 65  ACUUM is disable
1d035 64 20 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 73  d then a zero is
1d036 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1d037 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65  ter P2..**.** Se
1d038 65 20 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f  e also: Clear.*/
1d039 0a 63 61 73 65 20 4f 50 5f 44 65 73 74 72 6f 79  .case OP_Destroy
1d03a 3a 20 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d  : {     /* out2-
1d03b 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1d03c 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 69 6e  int iMoved;.  in
1d03d 74 20 69 43 6e 74 3b 0a 23 69 66 6e 64 65 66 20  t iCnt;.#ifndef 
1d03e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1d03f 55 41 4c 54 41 42 4c 45 0a 20 20 56 64 62 65 20  UALTABLE.  Vdbe 
1d040 2a 70 56 64 62 65 3b 0a 20 20 69 43 6e 74 20 3d  *pVdbe;.  iCnt =
1d041 20 30 3b 0a 20 20 66 6f 72 28 70 56 64 62 65 3d   0;.  for(pVdbe=
1d042 64 62 2d 3e 70 56 64 62 65 3b 20 70 56 64 62 65  db->pVdbe; pVdbe
1d043 3b 20 70 56 64 62 65 3d 70 56 64 62 65 2d 3e 70  ; pVdbe=pVdbe->p
1d044 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
1d045 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  Vdbe->magic==VDB
1d046 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70  E_MAGIC_RUN && p
1d047 56 64 62 65 2d 3e 69 6e 56 74 61 62 4d 65 74 68  Vdbe->inVtabMeth
1d048 6f 64 3c 32 20 26 26 20 70 56 64 62 65 2d 3e 70  od<2 && pVdbe->p
1d049 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 43  c>=0 ){.      iC
1d04a 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  nt++;.    }.  }.
1d04b 23 65 6c 73 65 0a 20 20 69 43 6e 74 20 3d 20 64  #else.  iCnt = d
1d04c 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
1d04d 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 69  ;.#endif.  if( i
1d04e 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 72 63 20  Cnt>1 ){.    rc 
1d04f 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b  = SQLITE_LOCKED;
1d050 0a 20 20 20 20 70 2d 3e 65 72 72 6f 72 41 63 74  .    p->errorAct
1d051 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ion = OE_Abort;.
1d052 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
1d053 20 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a   iDb = pOp->p3;.
1d054 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6e 74      assert( iCnt
1d055 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
1d056 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1d057 20 26 20 28 31 3c 3c 69 44 62 29 29 21 3d 30 20   & (1<<iDb))!=0 
1d058 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1d059 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c  te3BtreeDropTabl
1d05a 65 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  e(db->aDb[iDb].p
1d05b 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 26 69 4d  Bt, pOp->p1, &iM
1d05c 6f 76 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65  oved);.    MemSe
1d05d 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1d05e 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f  MEM_Int);.    pO
1d05f 75 74 2d 3e 75 2e 69 20 3d 20 69 4d 6f 76 65 64  ut->u.i = iMoved
1d060 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1d061 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1d062 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1d063 49 54 45 5f 4f 4b 20 26 26 20 69 4d 6f 76 65 64  ITE_OK && iMoved
1d064 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=0 ){.      sql
1d065 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65  ite3RootPageMove
1d066 64 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2c  d(&db->aDb[iDb],
1d067 20 69 4d 6f 76 65 64 2c 20 70 4f 70 2d 3e 70 31   iMoved, pOp->p1
1d068 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
1d069 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1d06a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61 72  /* Opcode: Clear
1d06b 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a 20   P1 P2 P3.**.** 
1d06c 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74 65  Delete all conte
1d06d 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  nts of the datab
1d06e 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  ase table or ind
1d06f 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61  ex whose root pa
1d070 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74  ge.** in the dat
1d071 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 67 69  abase file is gi
1d072 76 65 6e 20 62 79 20 50 31 2e 20 20 42 75 74 2c  ven by P1.  But,
1d073 20 75 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79 2c   unlike Destroy,
1d074 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f 76   do not.** remov
1d075 65 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  e the table or i
1d076 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 64 61  ndex from the da
1d077 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1d078 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69  ** The table bei
1d079 6e 67 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74  ng clear is in t
1d07a 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1d07b 20 66 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e 20   file if P2==0. 
1d07c 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68 65   If.** P2==1 the
1d07d 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  n the table to b
1d07e 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68  e clear is in th
1d07f 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  e auxiliary data
1d080 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61  base file.** tha
1d081 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f  t is used to sto
1d082 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65  re tables create
1d083 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45   using CREATE TE
1d084 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a  MPORARY TABLE..*
1d085 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 33 20 76  *.** If the P3 v
1d086 61 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  alue is non-zero
1d087 2c 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65  , then the table
1d088 20 72 65 66 65 72 72 65 64 20 74 6f 20 6d 75 73   referred to mus
1d089 74 20 62 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65  t be an.** intke
1d08a 79 20 74 61 62 6c 65 20 28 61 6e 20 53 51 4c 20  y table (an SQL 
1d08b 74 61 62 6c 65 2c 20 6e 6f 74 20 61 6e 20 69 6e  table, not an in
1d08c 64 65 78 29 2e 20 49 6e 20 74 68 69 73 20 63 61  dex). In this ca
1d08d 73 65 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  se the row chang
1d08e 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 69 73 20 69  e .** count is i
1d08f 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68  ncremented by th
1d090 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
1d091 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65   in the table be
1d092 69 6e 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2a  ing cleared. .**
1d093 20 49 66 20 50 33 20 69 73 20 67 72 65 61 74 65   If P3 is greate
1d094 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  r than zero, the
1d095 6e 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  n the value stor
1d096 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1d097 33 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 69 6e 63  3 is.** also inc
1d098 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20  remented by the 
1d099 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
1d09a 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  n the table bein
1d09b 67 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a  g cleared..**.**
1d09c 20 53 65 65 20 61 6c 73 6f 3a 20 44 65 73 74 72   See also: Destr
1d09d 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c  oy.*/.case OP_Cl
1d09e 65 61 72 3a 20 7b 0a 20 20 69 6e 74 20 6e 43 68  ear: {.  int nCh
1d09f 61 6e 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65  ange = 0;.  asse
1d0a0 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
1d0a1 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29  k & (1<<pOp->p2)
1d0a2 29 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73  )!=0 );.  rc = s
1d0a3 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72  qlite3BtreeClear
1d0a4 54 61 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d  Table(.      db-
1d0a5 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42  >aDb[pOp->p2].pB
1d0a6 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70  t, pOp->p1, (pOp
1d0a7 2d 3e 70 33 20 3f 20 26 6e 43 68 61 6e 67 65 20  ->p3 ? &nChange 
1d0a8 3a 20 30 29 0a 20 20 29 3b 0a 20 20 69 66 28 20  : 0).  );.  if( 
1d0a9 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 70  pOp->p3 ){.    p
1d0aa 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68  ->nChange += nCh
1d0ab 61 6e 67 65 3b 0a 20 20 20 20 69 66 28 20 70 4f  ange;.    if( pO
1d0ac 70 2d 3e 70 33 3e 30 20 29 7b 0a 20 20 20 20 20  p->p3>0 ){.     
1d0ad 20 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   p->aMem[pOp->p3
1d0ae 5d 2e 75 2e 69 20 2b 3d 20 6e 43 68 61 6e 67 65  ].u.i += nChange
1d0af 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1d0b0 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d0b1 65 3a 20 43 72 65 61 74 65 54 61 62 6c 65 20 50  e: CreateTable P
1d0b2 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1d0b3 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
1d0b4 74 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69  table in the mai
1d0b5 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
1d0b6 69 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74  if P1==0 or in t
1d0b7 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20  he.** auxiliary 
1d0b8 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66  database file if
1d0b9 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20   P1==1 or in an 
1d0ba 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1d0bb 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57  e if.** P1>1.  W
1d0bc 72 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61  rite the root pa
1d0bd 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
1d0be 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a   new table into.
1d0bf 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a  ** register P2.*
1d0c0 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65  *.** The differe
1d0c1 6e 63 65 20 62 65 74 77 65 65 6e 20 61 20 74 61  nce between a ta
1d0c2 62 6c 65 20 61 6e 64 20 61 6e 20 69 6e 64 65 78  ble and an index
1d0c3 20 69 73 20 74 68 69 73 3a 20 20 41 20 74 61 62   is this:  A tab
1d0c4 6c 65 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20  le must.** have 
1d0c5 61 20 34 2d 62 79 74 65 20 69 6e 74 65 67 65 72  a 4-byte integer
1d0c6 20 6b 65 79 20 61 6e 64 20 63 61 6e 20 68 61 76   key and can hav
1d0c7 65 20 61 72 62 69 74 72 61 72 79 20 64 61 74 61  e arbitrary data
1d0c8 2e 20 20 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68  .  An index.** h
1d0c9 61 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20  as an arbitrary 
1d0ca 6b 65 79 20 62 75 74 20 6e 6f 20 64 61 74 61 2e  key but no data.
1d0cb 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1d0cc 20 43 72 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a   CreateIndex.*/.
1d0cd 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74  /* Opcode: Creat
1d0ce 65 49 6e 64 65 78 20 50 31 20 50 32 20 2a 20 2a  eIndex P1 P2 * *
1d0cf 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74   *.**.** Allocat
1d0d0 65 20 61 20 6e 65 77 20 69 6e 64 65 78 20 69 6e  e a new index in
1d0d1 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1d0d2 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30  se file if P1==0
1d0d3 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75   or in the.** au
1d0d4 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
1d0d5 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f   file if P1==1 o
1d0d6 72 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64  r in an attached
1d0d7 20 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20   database if.** 
1d0d8 50 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65  P1>1.  Write the
1d0d9 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
1d0da 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  r of the new tab
1d0db 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73  le into.** regis
1d0dc 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65  ter P2..**.** Se
1d0dd 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  e documentation 
1d0de 6f 6e 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c  on OP_CreateTabl
1d0df 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  e for additional
1d0e0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
1d0e1 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 49  .case OP_CreateI
1d0e2 6e 64 65 78 3a 20 20 20 20 20 20 20 20 20 20 20  ndex:           
1d0e3 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1d0e4 61 73 65 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  ase */.case OP_C
1d0e5 72 65 61 74 65 54 61 62 6c 65 3a 20 7b 20 20 20  reateTable: {   
1d0e6 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1d0e7 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69  rerelease */.  i
1d0e8 6e 74 20 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 69  nt pgno = 0;.  i
1d0e9 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62 20 2a  nt flags;.  Db *
1d0ea 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  pDb;.  assert( p
1d0eb 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1d0ec 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  ->p1<db->nDb );.
1d0ed 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1d0ee 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1d0ef 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20  p->p1))!=0 );.  
1d0f0 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70  pDb = &db->aDb[p
1d0f1 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1d0f2 74 28 20 70 44 62 2d 3e 70 42 74 21 3d 30 20 29  t( pDb->pBt!=0 )
1d0f3 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63  ;.  if( pOp->opc
1d0f4 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54 61  ode==OP_CreateTa
1d0f5 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 66 6c  ble ){.    /* fl
1d0f6 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b  ags = BTREE_INTK
1d0f7 45 59 3b 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73  EY; */.    flags
1d0f8 20 3d 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54   = BTREE_LEAFDAT
1d0f9 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 0a  A|BTREE_INTKEY;.
1d0fa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6c 61    }else{.    fla
1d0fb 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44  gs = BTREE_ZEROD
1d0fc 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ATA;.  }.  rc = 
1d0fd 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
1d0fe 74 65 54 61 62 6c 65 28 70 44 62 2d 3e 70 42 74  teTable(pDb->pBt
1d0ff 2c 20 26 70 67 6e 6f 2c 20 66 6c 61 67 73 29 3b  , &pgno, flags);
1d100 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70  .  pOut->u.i = p
1d101 67 6e 6f 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70  gno;.  MemSetTyp
1d102 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1d103 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Int);.  break;.}
1d104 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72  ../* Opcode: Par
1d105 73 65 53 63 68 65 6d 61 20 50 31 20 50 32 20 2a  seSchema P1 P2 *
1d106 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64   P4 *.**.** Read
1d107 20 61 6e 64 20 70 61 72 73 65 20 61 6c 6c 20 65   and parse all e
1d108 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 20  ntries from the 
1d109 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
1d10a 62 6c 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ble of database 
1d10b 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68  P1.** that match
1d10c 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1d10d 65 20 50 34 2e 20 20 50 32 20 69 73 20 74 68 65  e P4.  P2 is the
1d10e 20 22 66 6f 72 63 65 22 20 66 6c 61 67 2e 20 20   "force" flag.  
1d10f 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74 68   Always do.** th
1d110 65 20 70 61 72 73 69 6e 67 20 69 66 20 50 32 20  e parsing if P2 
1d111 69 73 20 74 72 75 65 2e 20 20 49 66 20 50 32 20  is true.  If P2 
1d112 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74  is false, then t
1d113 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
1d114 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65  .** no-op if the
1d115 20 73 63 68 65 6d 61 20 69 73 20 6e 6f 74 20 63   schema is not c
1d116 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64 2e  urrently loaded.
1d117 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
1d118 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 20 66 61  , if P2.** is fa
1d119 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 54 45 5f  lse, the SQLITE_
1d11a 4d 41 53 54 45 52 20 74 61 62 6c 65 20 69 73 20  MASTER table is 
1d11b 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 66 20 74  only parsed if t
1d11c 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 0a 2a  he rest of the.*
1d11d 2a 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65  * schema is alre
1d11e 61 64 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20  ady loaded into 
1d11f 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65  the symbol table
1d120 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
1d121 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ode invokes the 
1d122 70 61 72 73 65 72 20 74 6f 20 63 72 65 61 74 65  parser to create
1d123 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d   a new virtual m
1d124 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20  achine,.** then 
1d125 72 75 6e 73 20 74 68 65 20 6e 65 77 20 76 69 72  runs the new vir
1d126 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49  tual machine.  I
1d127 74 20 69 73 20 74 68 75 73 20 61 20 72 65 2d 65  t is thus a re-e
1d128 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a  ntrant opcode..*
1d129 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 65 53  /.case OP_ParseS
1d12a 63 68 65 6d 61 3a 20 7b 0a 20 20 69 6e 74 20 69  chema: {.  int i
1d12b 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  Db = pOp->p1;.  
1d12c 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
1d12d 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
1d12e 0a 0a 20 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70  ..  /* If pOp->p
1d12f 32 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69  2 is 0, then thi
1d130 73 20 6f 70 63 6f 64 65 20 69 73 20 62 65 69 6e  s opcode is bein
1d131 67 20 65 78 65 63 75 74 65 64 20 74 6f 20 72 65  g executed to re
1d132 61 64 20 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65  ad a.  ** single
1d133 20 72 6f 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c   row, for exampl
1d134 65 20 74 68 65 20 72 6f 77 20 63 6f 72 72 65 73  e the row corres
1d135 70 6f 6e 64 69 6e 67 20 74 6f 20 61 20 6e 65 77  ponding to a new
1d136 20 69 6e 64 65 78 0a 20 20 2a 2a 20 63 72 65 61   index.  ** crea
1d137 74 65 64 20 62 79 20 74 68 69 73 20 56 44 42 45  ted by this VDBE
1d138 2c 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74  , from the sqlit
1d139 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  e_master table. 
1d13a 49 74 20 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65  It only.  ** doe
1d13b 73 20 74 68 69 73 20 69 66 20 74 68 65 20 63 6f  s this if the co
1d13c 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d  rresponding in-m
1d13d 65 6d 6f 72 79 20 73 63 68 65 6d 61 20 69 73 20  emory schema is 
1d13e 63 75 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c  currently.  ** l
1d13f 6f 61 64 65 64 2e 20 4f 74 68 65 72 77 69 73 65  oaded. Otherwise
1d140 2c 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20  , the new index 
1d141 64 65 66 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62  definition can b
1d142 65 20 6c 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20  e loaded along. 
1d143 20 2a 2a 20 77 69 74 68 20 74 68 65 20 72 65 73   ** with the res
1d144 74 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  t of the schema 
1d145 77 68 65 6e 20 69 74 20 69 73 20 72 65 71 75 69  when it is requi
1d146 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  red..  **.  ** A
1d147 6c 74 68 6f 75 67 68 20 74 68 65 20 6d 75 74 65  lthough the mute
1d148 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65  x on the BtShare
1d149 64 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f  d object that co
1d14a 72 72 65 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a  rresponds to.  *
1d14b 2a 20 64 61 74 61 62 61 73 65 20 69 44 62 20 28  * database iDb (
1d14c 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
1d14d 74 61 69 6e 69 6e 67 20 74 68 65 20 73 71 6c 69  taining the sqli
1d14e 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a  te_master table.
1d14f 20 20 2a 2a 20 72 65 61 64 20 62 79 20 74 68 69    ** read by thi
1d150 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 29 20 69  s instruction) i
1d151 73 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64  s currently held
1d152 2c 20 69 74 20 69 73 20 6e 65 63 65 73 73 61 72  , it is necessar
1d153 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 6e  y to.  ** obtain
1d154 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20   the mutexes on 
1d155 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
1d156 61 62 61 73 65 73 20 62 65 66 6f 72 65 20 63 68  abases before ch
1d157 65 63 6b 69 6e 67 20 69 66 0a 20 20 2a 2a 20 74  ecking if.  ** t
1d158 68 65 20 73 63 68 65 6d 61 20 6f 66 20 69 44 62  he schema of iDb
1d159 20 69 73 20 6c 6f 61 64 65 64 2e 20 54 68 69 73   is loaded. This
1d15a 20 69 73 20 62 65 63 61 75 73 65 2c 20 61 74 20   is because, at 
1d15b 74 68 65 20 73 74 61 72 74 20 6f 66 0a 20 20 2a  the start of.  *
1d15c 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78  * the sqlite3_ex
1d15d 65 63 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 2c  ec() call below,
1d15e 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76   SQLite will inv
1d15f 6f 6b 65 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65  oke .  ** sqlite
1d160 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29  3BtreeEnterAll()
1d161 2e 20 49 66 20 61 6c 6c 20 6d 75 74 65 78 65 73  . If all mutexes
1d162 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79   are not already
1d163 20 68 65 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20   held, the.  ** 
1d164 69 44 62 20 6d 75 74 65 78 20 6d 61 79 20 62 65  iDb mutex may be
1d165 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 72 65 6c   temporarily rel
1d166 65 61 73 65 64 20 74 6f 20 61 76 6f 69 64 20 64  eased to avoid d
1d167 65 61 64 6c 6f 63 6b 2e 20 49 66 20 0a 20 20 2a  eadlock. If .  *
1d168 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20  * this happens, 
1d169 74 68 65 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20  then some other 
1d16a 74 68 72 65 61 64 20 6d 61 79 20 64 65 6c 65 74  thread may delet
1d16b 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  e the in-memory 
1d16c 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6f 66 20  .  ** schema of 
1d16d 64 61 74 61 62 61 73 65 20 69 44 62 20 62 65 66  database iDb bef
1d16e 6f 72 65 20 74 68 65 20 53 51 4c 20 73 74 61 74  ore the SQL stat
1d16f 65 6d 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20  ement runs. The 
1d170 73 63 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c  schema.  ** will
1d171 20 6e 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64   not be reloaded
1d172 20 62 65 63 75 61 73 65 20 74 68 65 20 64 62 2d   becuase the db-
1d173 3e 69 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20  >init.busy flag 
1d174 69 73 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a  is set. This.  *
1d175 2a 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20  * can result in 
1d176 61 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65  a "no such table
1d177 3a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22  : sqlite_master"
1d178 20 6f 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20   or "malformed. 
1d179 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68   ** database sch
1d17a 65 6d 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67  ema" error being
1d17b 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
1d17c 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73   user..  */.  as
1d17d 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
1d17e 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d  eeHoldsMutex(db-
1d17f 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20 29  >aDb[iDb].pBt) )
1d180 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1d181 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20  EnterAll(db);.  
1d182 69 66 28 20 70 4f 70 2d 3e 70 32 20 7c 7c 20 44  if( pOp->p2 || D
1d183 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c  bHasProperty(db,
1d184 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c   iDb, DB_SchemaL
1d185 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 63 6f  oaded) ){.    co
1d186 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65  nst char *zMaste
1d187 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  r = SCHEMA_TABLE
1d188 28 69 44 62 29 3b 0a 20 20 20 20 63 68 61 72 20  (iDb);.    char 
1d189 2a 7a 53 71 6c 3b 0a 20 20 20 20 49 6e 69 74 44  *zSql;.    InitD
1d18a 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20  ata initData;.  
1d18b 20 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20    initData.db = 
1d18c 64 62 3b 0a 20 20 20 20 69 6e 69 74 44 61 74 61  db;.    initData
1d18d 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  .iDb = pOp->p1;.
1d18e 20 20 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45      initData.pzE
1d18f 72 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72  rrMsg = &p->zErr
1d190 4d 73 67 3b 0a 20 20 20 20 7a 53 71 6c 20 3d 20  Msg;.    zSql = 
1d191 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1d192 62 2c 0a 20 20 20 20 20 20 20 22 53 45 4c 45 43  b,.       "SELEC
1d193 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65  T name, rootpage
1d194 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e  , sql FROM '%q'.
1d195 25 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20 20  %s WHERE %s",.  
1d196 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
1d197 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65 72  ].zName, zMaster
1d198 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20  , pOp->p4.z);.  
1d199 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b    if( zSql==0 ){
1d19a 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1d19b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
1d19c 6c 73 65 7b 0a 20 20 20 20 20 20 28 76 6f 69 64  lse{.      (void
1d19d 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
1d19e 66 28 64 62 29 3b 0a 20 20 20 20 20 20 61 73 73  f(db);.      ass
1d19f 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75  ert( db->init.bu
1d1a0 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64  sy==0 );.      d
1d1a1 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31  b->init.busy = 1
1d1a2 3b 0a 20 20 20 20 20 20 69 6e 69 74 44 61 74 61  ;.      initData
1d1a3 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  .rc = SQLITE_OK;
1d1a4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
1d1a5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1d1a6 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   );.      rc = s
1d1a7 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
1d1a8 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69  zSql, sqlite3Ini
1d1a9 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74  tCallback, &init
1d1aa 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 20 20  Data, 0);.      
1d1ab 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1d1ac 4b 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61 74  K ) rc = initDat
1d1ad 61 2e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69  a.rc;.      sqli
1d1ae 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
1d1af 71 6c 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69  ql);.      db->i
1d1b0 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20  nit.busy = 0;.  
1d1b1 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1d1b2 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
1d1b3 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
1d1b4 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
1d1b5 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  db);.  if( rc==S
1d1b6 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
1d1b7 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
1d1b8 20 20 7d 0a 20 20 62 72 65 61 6b 3b 20 20 0a 7d    }.  break;  .}
1d1b9 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1d1ba 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59  QLITE_OMIT_ANALY
1d1bb 5a 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  ZE) && !defined(
1d1bc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53  SQLITE_OMIT_PARS
1d1bd 45 52 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c  ER)./* Opcode: L
1d1be 6f 61 64 41 6e 61 6c 79 73 69 73 20 50 31 20 2a  oadAnalysis P1 *
1d1bf 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61   * * *.**.** Rea
1d1c0 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  d the sqlite_sta
1d1c1 74 31 20 74 61 62 6c 65 20 66 6f 72 20 64 61 74  t1 table for dat
1d1c2 61 62 61 73 65 20 50 31 20 61 6e 64 20 6c 6f 61  abase P1 and loa
1d1c3 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a  d the content.**
1d1c4 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 69   of that table i
1d1c5 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  nto the internal
1d1c6 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c   index hash tabl
1d1c7 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61  e.  This will ca
1d1c8 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e 61 6c 79  use.** the analy
1d1c9 73 69 73 20 74 6f 20 62 65 20 75 73 65 64 20 77  sis to be used w
1d1ca 68 65 6e 20 70 72 65 70 61 72 69 6e 67 20 61 6c  hen preparing al
1d1cb 6c 20 73 75 62 73 65 71 75 65 6e 74 20 71 75 65  l subsequent que
1d1cc 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  ries..*/.case OP
1d1cd 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 3a 20 7b  _LoadAnalysis: {
1d1ce 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f 70  .  int iDb = pOp
1d1cf 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p1;.  assert( 
1d1d0 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
1d1d1 2d 3e 6e 44 62 20 29 3b 0a 20 20 72 63 20 3d 20  ->nDb );.  rc = 
1d1d2 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c  sqlite3AnalysisL
1d1d3 6f 61 64 28 64 62 2c 20 69 44 62 29 3b 0a 20 20  oad(db, iDb);.  
1d1d4 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69  break;  .}.#endi
1d1d5 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
1d1d6 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a  LITE_OMIT_ANALYZ
1d1d7 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  E) && !defined(S
1d1d8 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45  QLITE_OMIT_PARSE
1d1d9 52 29 20 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  R)  */../* Opcod
1d1da 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20  e: DropTable P1 
1d1db 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52  * * P4 *.**.** R
1d1dc 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e  emove the intern
1d1dd 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64  al (in-memory) d
1d1de 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74  ata structures t
1d1df 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20  hat describe.** 
1d1e0 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20  the table named 
1d1e1 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  P4 in database P
1d1e2 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c  1.  This is call
1d1e3 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65  ed after a table
1d1e4 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69  .** is dropped i
1d1e5 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
1d1e6 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
1d1e7 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
1d1e8 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e  he.** schema con
1d1e9 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61  sistent with wha
1d1ea 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f  t is on disk..*/
1d1eb 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62  .case OP_DropTab
1d1ec 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55  le: {.  sqlite3U
1d1ed 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61  nlinkAndDeleteTa
1d1ee 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c  ble(db, pOp->p1,
1d1ef 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62   pOp->p4.z);.  b
1d1f0 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d1f1 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31  de: DropIndex P1
1d1f2 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1d1f3 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72  Remove the inter
1d1f4 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20  nal (in-memory) 
1d1f5 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
1d1f6 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a  that describe.**
1d1f7 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64   the index named
1d1f8 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20   P4 in database 
1d1f9 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c  P1.  This is cal
1d1fa 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64  led after an ind
1d1fb 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64  ex.** is dropped
1d1fc 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65   in order to kee
1d1fd 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72  p the internal r
1d1fe 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
1d1ff 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63   the.** schema c
1d200 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77  onsistent with w
1d201 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a  hat is on disk..
1d202 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49  */.case OP_DropI
1d203 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65  ndex: {.  sqlite
1d204 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
1d205 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70  Index(db, pOp->p
1d206 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  1, pOp->p4.z);. 
1d207 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d208 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65  code: DropTrigge
1d209 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  r P1 * * P4 *.**
1d20a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69  .** Remove the i
1d20b 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f  nternal (in-memo
1d20c 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74 75  ry) data structu
1d20d 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62  res that describ
1d20e 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72  e.** the trigger
1d20f 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74   named P4 in dat
1d210 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20  abase P1.  This 
1d211 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
1d212 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20  a trigger.** is 
1d213 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72  dropped in order
1d214 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74   to keep the int
1d215 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
1d216 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73  tion of the.** s
1d217 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74  chema consistent
1d218 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e   with what is on
1d219 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f   disk..*/.case O
1d21a 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b  P_DropTrigger: {
1d21b 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  .  sqlite3Unlink
1d21c 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72  AndDeleteTrigger
1d21d 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  (db, pOp->p1, pO
1d21e 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61  p->p4.z);.  brea
1d21f 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  k;.}...#ifndef S
1d220 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
1d221 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70  RITY_CHECK./* Op
1d222 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43  code: IntegrityC
1d223 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a  k P1 P2 P3 * P5.
1d224 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c  **.** Do an anal
1d225 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 72  ysis of the curr
1d226 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62  ently open datab
1d227 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a  ase.  Store in.*
1d228 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 68  * register P1 th
1d229 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72  e text of an err
1d22a 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 72  or message descr
1d22b 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65  ibing any proble
1d22c 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f  ms..** If no pro
1d22d 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c  blems are found,
1d22e 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e   store a NULL in
1d22f 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1d230 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 72  .** The register
1d231 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65   P3 contains the
1d232 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1d233 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72  of allowed error
1d234 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65  s..** At most re
1d235 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c  g(P3) errors wil
1d236 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a  l be reported..*
1d237 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
1d238 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73  , the analysis s
1d239 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20  tops as soon as 
1d23a 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 61  reg(P1) errors a
1d23b 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65  re .** seen.  Re
1d23c 67 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64  g(P1) is updated
1d23d 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72   with the number
1d23e 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69   of errors remai
1d23f 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ning..**.** The 
1d240 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
1d241 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20  s of all tables 
1d242 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1d243 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73  are integer.** s
1d244 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 29  tored in reg(P1)
1d245 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67  , reg(P1+1), reg
1d246 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68  (P1+2), ....  Th
1d247 65 72 65 20 61 72 65 20 50 32 20 74 61 62 6c 65  ere are P2 table
1d248 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a  s.** total..**.*
1d249 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a  * If P5 is not z
1d24a 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69  ero, the check i
1d24b 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75  s done on the au
1d24c 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
1d24d 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68  .** file, not th
1d24e 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1d24f 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  file..**.** This
1d250 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20   opcode is used 
1d251 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1d252 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b   integrity_check
1d253 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65   pragma..*/.case
1d254 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a   OP_IntegrityCk:
1d255 20 7b 0a 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20   {.  int nRoot; 
1d256 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d257 66 20 74 61 62 6c 65 73 20 74 6f 20 63 68 65 63  f tables to chec
1d258 6b 2e 20 20 28 4e 75 6d 62 65 72 20 6f 66 20 72  k.  (Number of r
1d259 6f 6f 74 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20  oot pages.) */. 
1d25a 20 69 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20   int *aRoot;    
1d25b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f   /* Array of roo
1d25c 74 70 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f  tpage numbers fo
1d25d 72 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63  r tables to be c
1d25e 68 65 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20  hecked */.  int 
1d25f 6a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  j;          /* L
1d260 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1d261 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20   int nErr;      
1d262 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72   /* Number of er
1d263 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20 2a 2f  rors reported */
1d264 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20  .  char *z;     
1d265 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68     /* Text of th
1d266 65 20 65 72 72 6f 72 20 72 65 70 6f 72 74 20 2a  e error report *
1d267 2f 0a 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20  /.  Mem *pnErr; 
1d268 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
1d269 6b 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66  keeping track of
1d26a 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e   errors remainin
1d26b 67 20 2a 2f 0a 20 20 0a 20 20 6e 52 6f 6f 74 20  g */.  .  nRoot 
1d26c 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73  = pOp->p2;.  ass
1d26d 65 72 74 28 20 6e 52 6f 6f 74 3e 30 20 29 3b 0a  ert( nRoot>0 );.
1d26e 20 20 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65    aRoot = sqlite
1d26f 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
1d270 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 52   sizeof(int)*(nR
1d271 6f 6f 74 2b 31 29 20 29 3b 0a 20 20 69 66 28 20  oot+1) );.  if( 
1d272 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f 20  aRoot==0 ) goto 
1d273 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72 74  no_mem;.  assert
1d274 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70  ( pOp->p3>0 && p
1d275 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
1d276 29 3b 0a 20 20 70 6e 45 72 72 20 3d 20 26 70 2d  );.  pnErr = &p-
1d277 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1d278 20 20 61 73 73 65 72 74 28 20 28 70 6e 45 72 72    assert( (pnErr
1d279 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1d27a 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  t)!=0 );.  asser
1d27b 74 28 20 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73  t( (pnErr->flags
1d27c 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f   & (MEM_Str|MEM_
1d27d 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70  Blob))==0 );.  p
1d27e 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In1 = &p->aMem[p
1d27f 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 6a  Op->p1];.  for(j
1d280 3d 30 3b 20 6a 3c 6e 52 6f 6f 74 3b 20 6a 2b 2b  =0; j<nRoot; j++
1d281 29 7b 0a 20 20 20 20 61 52 6f 6f 74 5b 6a 5d 20  ){.    aRoot[j] 
1d282 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64  = (int)sqlite3Vd
1d283 62 65 49 6e 74 56 61 6c 75 65 28 26 70 49 6e 31  beIntValue(&pIn1
1d284 5b 6a 5d 29 3b 0a 20 20 7d 0a 20 20 61 52 6f 6f  [j]);.  }.  aRoo
1d285 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65  t[j] = 0;.  asse
1d286 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e  rt( pOp->p5<db->
1d287 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
1d288 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1d289 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 29 21 3d   (1<<pOp->p5))!=
1d28a 30 20 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74  0 );.  z = sqlit
1d28b 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 79  e3BtreeIntegrity
1d28c 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b 70 4f  Check(db->aDb[pO
1d28d 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 61 52 6f 6f  p->p5].pBt, aRoo
1d28e 74 2c 20 6e 52 6f 6f 74 2c 0a 20 20 20 20 20 20  t, nRoot,.      
1d28f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d290 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29             (int)
1d291 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 6e 45 72  pnErr->u.i, &nEr
1d292 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  r);.  sqlite3DbF
1d293 72 65 65 28 64 62 2c 20 61 52 6f 6f 74 29 3b 0a  ree(db, aRoot);.
1d294 20 20 70 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d 20    pnErr->u.i -= 
1d295 6e 45 72 72 3b 0a 20 20 73 71 6c 69 74 65 33 56  nErr;.  sqlite3V
1d296 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49  dbeMemSetNull(pI
1d297 6e 31 29 3b 0a 20 20 69 66 28 20 6e 45 72 72 3d  n1);.  if( nErr=
1d298 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
1d299 28 20 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73  ( z==0 );.  }els
1d29a 65 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20  e if( z==0 ){.  
1d29b 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1d29c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
1d29d 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
1d29e 28 70 49 6e 31 2c 20 7a 2c 20 2d 31 2c 20 53 51  (pIn1, z, -1, SQ
1d29f 4c 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74  LITE_UTF8, sqlit
1d2a0 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 20 20  e3_free);.  }.  
1d2a1 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1d2a2 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c  IZE(pIn1);.  sql
1d2a3 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
1d2a4 63 6f 64 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63  coding(pIn1, enc
1d2a5 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b  oding);.  break;
1d2a6 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1d2a7 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
1d2a8 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20  TY_CHECK */../* 
1d2a9 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 41 64  Opcode: RowSetAd
1d2aa 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  d P1 P2 * * *.**
1d2ab 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 69  .** Insert the i
1d2ac 6e 74 65 67 65 72 20 76 61 6c 75 65 20 68 65 6c  nteger value hel
1d2ad 64 20 62 79 20 72 65 67 69 73 74 65 72 20 50 32  d by register P2
1d2ae 20 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 61 6e 20   into a boolean 
1d2af 69 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 20 69 6e  index.** held in
1d2b0 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1d2b1 0a 2a 2a 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  .** An assertion
1d2b2 20 66 61 69 6c 73 20 69 66 20 50 32 20 69 73 20   fails if P2 is 
1d2b3 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a  not an integer..
1d2b4 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65  */.case OP_RowSe
1d2b5 74 41 64 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a  tAdd: {       /*
1d2b6 20 69 6e 32 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70   in2 */.  Mem *p
1d2b7 49 64 78 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 6c  Idx;.  Mem *pVal
1d2b8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1d2b9 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31  >p1>0 && pOp->p1
1d2ba 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
1d2bb 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Idx = &p->aMem[p
1d2bc 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1d2bd 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20  t( pOp->p2>0 && 
1d2be 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p2<=p->nMem
1d2bf 20 29 3b 0a 20 20 70 56 61 6c 20 3d 20 26 70 2d   );.  pVal = &p-
1d2c0 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
1d2c1 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d    assert( (pVal-
1d2c2 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1d2c3 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  )!=0 );.  if( (p
1d2c4 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Idx->flags & MEM
1d2c5 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20  _RowSet)==0 ){. 
1d2c6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1d2c7 6d 53 65 74 52 6f 77 53 65 74 28 70 49 64 78 29  mSetRowSet(pIdx)
1d2c8 3b 0a 20 20 20 20 69 66 28 20 28 70 49 64 78 2d  ;.    if( (pIdx-
1d2c9 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
1d2ca 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  Set)==0 ) goto n
1d2cb 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c  o_mem;.  }.  sql
1d2cc 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74  ite3RowSetInsert
1d2cd 28 70 49 64 78 2d 3e 75 2e 70 52 6f 77 53 65 74  (pIdx->u.pRowSet
1d2ce 2c 20 70 56 61 6c 2d 3e 75 2e 69 29 3b 0a 20 20  , pVal->u.i);.  
1d2cf 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1d2d0 6f 64 65 3a 20 52 6f 77 53 65 74 52 65 61 64 20  ode: RowSetRead 
1d2d1 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1d2d2 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 73  ** Extract the s
1d2d3 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20 66 72  mallest value fr
1d2d4 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78  om boolean index
1d2d5 20 50 31 20 61 6e 64 20 70 75 74 20 74 68 61 74   P1 and put that
1d2d6 20 76 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a 20 72   value into.** r
1d2d7 65 67 69 73 74 65 72 20 50 33 2e 20 20 4f 72 2c  egister P3.  Or,
1d2d8 20 69 66 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65   if boolean inde
1d2d9 78 20 50 31 20 69 73 20 69 6e 69 74 69 61 6c 6c  x P1 is initiall
1d2da 79 20 65 6d 70 74 79 2c 20 6c 65 61 76 65 20 50  y empty, leave P
1d2db 33 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 61  3.** unchanged a
1d2dc 6e 64 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72  nd jump to instr
1d2dd 75 63 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a 63 61  uction P2..*/.ca
1d2de 73 65 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64  se OP_RowSetRead
1d2df 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  : {       /* jum
1d2e0 70 2c 20 6f 75 74 33 20 2a 2f 0a 20 20 4d 65 6d  p, out3 */.  Mem
1d2e1 20 2a 70 49 64 78 3b 0a 20 20 69 36 34 20 76 61   *pIdx;.  i64 va
1d2e2 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  l;.  assert( pOp
1d2e3 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p1>0 && pOp->p
1d2e4 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  1<=p->nMem );.  
1d2e5 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
1d2e6 55 50 54 3b 0a 20 20 70 49 64 78 20 3d 20 26 70  UPT;.  pIdx = &p
1d2e7 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b  ->aMem[pOp->p1];
1d2e8 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1d2e9 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69  em[pOp->p3];.  i
1d2ea 66 28 20 28 70 49 64 78 2d 3e 66 6c 61 67 73 20  f( (pIdx->flags 
1d2eb 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
1d2ec 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 52   .   || sqlite3R
1d2ed 6f 77 53 65 74 4e 65 78 74 28 70 49 64 78 2d 3e  owSetNext(pIdx->
1d2ee 75 2e 70 52 6f 77 53 65 74 2c 20 26 76 61 6c 29  u.pRowSet, &val)
1d2ef 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20  ==0.  ){.    /* 
1d2f0 54 68 65 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65  The boolean inde
1d2f1 78 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20  x is empty */.  
1d2f2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d2f3 53 65 74 4e 75 6c 6c 28 70 49 64 78 29 3b 0a 20  SetNull(pIdx);. 
1d2f4 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1d2f5 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  - 1;.  }else{.  
1d2f6 20 20 2f 2a 20 41 20 76 61 6c 75 65 20 77 61 73    /* A value was
1d2f7 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   pulled from the
1d2f8 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 61 73   index */.    as
1d2f9 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20  sert( pOp->p3>0 
1d2fa 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e  && pOp->p3<=p->n
1d2fb 4d 65 6d 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  Mem );.    sqlit
1d2fc 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1d2fd 34 28 70 4f 75 74 2c 20 76 61 6c 29 3b 0a 20 20  4(pOut, val);.  
1d2fe 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1d2ff 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 54   Opcode: RowSetT
1d300 65 73 74 20 50 31 20 50 32 20 50 33 20 50 34 0a  est P1 P2 P3 P4.
1d301 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50  **.** Register P
1d302 33 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  3 is assumed to 
1d303 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20 69 6e  hold a 64-bit in
1d304 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49 66 20  teger value. If 
1d305 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 63  register P1.** c
1d306 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53 65 74  ontains a RowSet
1d307 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61 74   object and that
1d308 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 63   RowSet object c
1d309 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 76  ontains.** the v
1d30a 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50 33 2c  alue held in P3,
1d30b 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73 74 65   jump to registe
1d30c 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c  r P2. Otherwise,
1d30d 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a 20 69   insert the.** i
1d30e 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69 6e 74  nteger in P3 int
1d30f 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61 6e 64  o the RowSet and
1d310 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f 20   continue on to 
1d311 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70 63 6f  the.** next opco
1d312 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f  de..**.** The Ro
1d313 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73 20 6f  wSet object is o
1d314 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74 68 65  ptimized for the
1d315 20 63 61 73 65 20 77 68 65 72 65 20 73 75 63 63   case where succ
1d316 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a 20 6f  essive sets.** o
1d317 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68 65 72  f integers, wher
1d318 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e 74 61  e each set conta
1d319 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61 74 65  ins no duplicate
1d31a 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a 20 6f  s. Each set.** o
1d31b 66 20 76 61 6c 75 65 73 20 69 73 20 69 64 65 6e  f values is iden
1d31c 74 69 66 69 65 64 20 62 79 20 61 20 75 6e 69 71  tified by a uniq
1d31d 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54 68 65  ue P4 value. The
1d31e 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20 6d 75   first set.** mu
1d31f 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c 20 74  st have P4==0, t
1d320 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50 34 3d  he final set P4=
1d321 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62 65 20  -1.  P4 must be 
1d322 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a 2a 20  either -1 or.** 
1d323 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 46  non-negative.  F
1d324 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  or non-negative 
1d325 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f 6e 6c  values of P4 onl
1d326 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a 2a 2a  y the lower 4.**
1d327 20 62 69 74 73 20 61 72 65 20 73 69 67 6e 69 66   bits are signif
1d328 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  icant..**.** Thi
1d329 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d 69 7a  s allows optimiz
1d32a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68 65 6e  ations: (a) when
1d32b 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69 73 20   P4==0 there is 
1d32c 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74 0a  no need to test.
1d32d 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20 6f 62  ** the rowset ob
1d32e 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61 73 20  ject for P3, as 
1d32f 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  it is guaranteed
1d330 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20   not to contain 
1d331 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65 6e 20  it,.** (b) when 
1d332 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69 73 20  P4==-1 there is 
1d333 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73 65 72  no need to inser
1d334 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61 73 20  t the value, as 
1d335 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72  it will.** never
1d336 20 62 65 20 74 65 73 74 65 64 20 66 6f 72 2c 20   be tested for, 
1d337 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61 20 76  and (c) when a v
1d338 61 6c 75 65 20 74 68 61 74 20 69 73 20 70 61 72  alue that is par
1d339 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a 2a 2a  t of set X is.**
1d33a 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65 72 65   inserted, there
1d33b 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 73   is no need to s
1d33c 65 61 72 63 68 20 74 6f 20 73 65 65 20 69 66 20  earch to see if 
1d33d 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 77  the same value w
1d33e 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  as.** previously
1d33f 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61 72   inserted as par
1d340 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e 6c 79  t of set X (only
1d341 20 69 66 20 69 74 20 77 61 73 20 70 72 65 76 69   if it was previ
1d342 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65  ously.** inserte
1d343 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 6f 6d  d as part of som
1d344 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a 2a 2f  e other set)..*/
1d345 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 54  .case OP_RowSetT
1d346 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  est: {          
1d347 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
1d348 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
1d349 20 20 69 6e 74 20 69 53 65 74 20 3d 20 70 4f 70    int iSet = pOp
1d34a 2d 3e 70 34 2e 69 3b 0a 20 20 61 73 73 65 72 74  ->p4.i;.  assert
1d34b 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d 45  ( pIn3->flags&ME
1d34c 4d 5f 49 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 49  M_Int );..  /* I
1d34d 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 74 68  f there is anyth
1d34e 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61  ing other than a
1d34f 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 69   rowset object i
1d350 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 31  n memory cell P1
1d351 2c 0a 20 20 2a 2a 20 64 65 6c 65 74 65 20 69 74  ,.  ** delete it
1d352 20 6e 6f 77 20 61 6e 64 20 69 6e 69 74 69 61 6c   now and initial
1d353 69 7a 65 20 50 31 20 77 69 74 68 20 61 6e 20 65  ize P1 with an e
1d354 6d 70 74 79 20 72 6f 77 73 65 74 0a 20 20 2a 2f  mpty rowset.  */
1d355 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
1d356 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
1d357 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
1d358 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77  te3VdbeMemSetRow
1d359 53 65 74 28 70 49 6e 31 29 3b 0a 20 20 20 20 69  Set(pIn1);.    i
1d35a 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
1d35b 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
1d35c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1d35d 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
1d35e 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49  Op->p4type==P4_I
1d35f 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74  NT32 );.  assert
1d360 28 20 69 53 65 74 3d 3d 2d 31 20 7c 7c 20 69 53  ( iSet==-1 || iS
1d361 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 69  et>=0 );.  if( i
1d362 53 65 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 65  Set ){.    int e
1d363 78 69 73 74 73 3b 0a 20 20 20 20 65 78 69 73 74  xists;.    exist
1d364 73 20 3d 20 73 71 6c 69 74 65 33 52 6f 77 53 65  s = sqlite3RowSe
1d365 74 54 65 73 74 28 70 49 6e 31 2d 3e 75 2e 70 52  tTest(pIn1->u.pR
1d366 6f 77 53 65 74 2c 20 0a 20 20 20 20 20 20 20 20  owSet, .        
1d367 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d368 20 20 20 20 20 20 20 28 75 38 29 28 69 53 65 74         (u8)(iSet
1d369 3e 3d 30 20 3f 20 69 53 65 74 20 26 20 30 78 66  >=0 ? iSet & 0xf
1d36a 20 3a 20 30 78 66 66 29 2c 0a 20 20 20 20 20 20   : 0xff),.      
1d36b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d36c 20 20 20 20 20 20 20 20 20 70 49 6e 33 2d 3e 75           pIn3->u
1d36d 2e 69 29 3b 0a 20 20 20 20 69 66 28 20 65 78 69  .i);.    if( exi
1d36e 73 74 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20  sts ){.      pc 
1d36f 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1d370 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1d371 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 53 65 74  }.  }.  if( iSet
1d372 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
1d373 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70  e3RowSetInsert(p
1d374 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20  In1->u.pRowSet, 
1d375 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a  pIn3->u.i);.  }.
1d376 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66    break;.}...#if
1d377 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d378 5f 54 52 49 47 47 45 52 0a 2f 2a 20 4f 70 63 6f  _TRIGGER./* Opco
1d379 64 65 3a 20 43 6f 6e 74 65 78 74 50 75 73 68 20  de: ContextPush 
1d37a 2a 20 2a 20 2a 20 0a 2a 2a 0a 2a 2a 20 53 61 76  * * * .**.** Sav
1d37b 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64  e the current Vd
1d37c 62 65 20 63 6f 6e 74 65 78 74 20 73 75 63 68 20  be context such 
1d37d 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72  that it can be r
1d37e 65 73 74 6f 72 65 64 20 62 79 20 61 20 43 6f 6e  estored by a Con
1d37f 74 65 78 74 50 6f 70 0a 2a 2a 20 6f 70 63 6f 64  textPop.** opcod
1d380 65 2e 20 54 68 65 20 63 6f 6e 74 65 78 74 20 73  e. The context s
1d381 74 6f 72 65 73 20 74 68 65 20 6c 61 73 74 20 69  tores the last i
1d382 6e 73 65 72 74 20 72 6f 77 20 69 64 2c 20 74 68  nsert row id, th
1d383 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74  e last statement
1d384 20 63 68 61 6e 67 65 0a 2a 2a 20 63 6f 75 6e 74   change.** count
1d385 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  , and the curren
1d386 74 20 73 74 61 74 65 6d 65 6e 74 20 63 68 61 6e  t statement chan
1d387 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 63 61 73  ge count..*/.cas
1d388 65 20 4f 50 5f 43 6f 6e 74 65 78 74 50 75 73 68  e OP_ContextPush
1d389 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 2d  : {.  int i = p-
1d38a 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 54 6f 70  >contextStackTop
1d38b 2b 2b 3b 0a 20 20 43 6f 6e 74 65 78 74 20 2a 70  ++;.  Context *p
1d38c 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65  Context;..  asse
1d38d 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 2f 2a  rt( i>=0 );.  /*
1d38e 20 46 49 58 20 4d 45 3a 20 54 68 69 73 20 73 68   FIX ME: This sh
1d38f 6f 75 6c 64 20 62 65 20 61 6c 6c 6f 63 61 74 65  ould be allocate
1d390 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  d as part of the
1d391 20 76 64 62 65 20 61 74 20 63 6f 6d 70 69 6c 65   vdbe at compile
1d392 2d 74 69 6d 65 20 2a 2f 0a 20 20 69 66 28 20 69  -time */.  if( i
1d393 3e 3d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  >=p->contextStac
1d394 6b 44 65 70 74 68 20 29 7b 0a 20 20 20 20 70 2d  kDepth ){.    p-
1d395 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70  >contextStackDep
1d396 74 68 20 3d 20 69 2b 31 3b 0a 20 20 20 20 70 2d  th = i+1;.    p-
1d397 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 20 3d 20  >contextStack = 
1d398 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
1d399 4f 72 46 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f  OrFree(db, p->co
1d39a 6e 74 65 78 74 53 74 61 63 6b 2c 0a 20 20 20 20  ntextStack,.    
1d39b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d39c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d39d 20 20 20 20 20 20 73 69 7a 65 6f 66 28 43 6f 6e        sizeof(Con
1d39e 74 65 78 74 29 2a 28 69 2b 31 29 29 3b 0a 20 20  text)*(i+1));.  
1d39f 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74 65 78 74    if( p->context
1d3a0 53 74 61 63 6b 3d 3d 30 20 29 20 67 6f 74 6f 20  Stack==0 ) goto 
1d3a1 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 43  no_mem;.  }.  pC
1d3a2 6f 6e 74 65 78 74 20 3d 20 26 70 2d 3e 63 6f 6e  ontext = &p->con
1d3a3 74 65 78 74 53 74 61 63 6b 5b 69 5d 3b 0a 20 20  textStack[i];.  
1d3a4 70 43 6f 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f  pContext->lastRo
1d3a5 77 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f  wid = db->lastRo
1d3a6 77 69 64 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d  wid;.  pContext-
1d3a7 3e 6e 43 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43  >nChange = p->nC
1d3a8 68 61 6e 67 65 3b 0a 20 20 62 72 65 61 6b 3b 0a  hange;.  break;.
1d3a9 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  }../* Opcode: Co
1d3aa 6e 74 65 78 74 50 6f 70 20 2a 20 2a 20 2a 20 0a  ntextPop * * * .
1d3ab 2a 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68  **.** Restore th
1d3ac 65 20 56 64 62 65 20 63 6f 6e 74 65 78 74 20 74  e Vdbe context t
1d3ad 6f 20 74 68 65 20 73 74 61 74 65 20 69 74 20 77  o the state it w
1d3ae 61 73 20 69 6e 20 77 68 65 6e 20 63 6f 6e 74 65  as in when conte
1d3af 78 74 50 75 73 68 20 77 61 73 20 6c 61 73 74 0a  xtPush was last.
1d3b0 2a 2a 20 65 78 65 63 75 74 65 64 2e 20 54 68 65  ** executed. The
1d3b1 20 63 6f 6e 74 65 78 74 20 73 74 6f 72 65 73 20   context stores 
1d3b2 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20  the last insert 
1d3b3 72 6f 77 20 69 64 2c 20 74 68 65 20 6c 61 73 74  row id, the last
1d3b4 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63 68   statement.** ch
1d3b5 61 6e 67 65 20 63 6f 75 6e 74 2c 20 61 6e 64 20  ange count, and 
1d3b6 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
1d3b7 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f 75  ement change cou
1d3b8 6e 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  nt..*/.case OP_C
1d3b9 6f 6e 74 65 78 74 50 6f 70 3a 20 7b 0a 20 20 43  ontextPop: {.  C
1d3ba 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74  ontext *pContext
1d3bb 20 3d 20 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74   = &p->contextSt
1d3bc 61 63 6b 5b 2d 2d 70 2d 3e 63 6f 6e 74 65 78 74  ack[--p->context
1d3bd 53 74 61 63 6b 54 6f 70 5d 3b 0a 20 20 61 73 73  StackTop];.  ass
1d3be 65 72 74 28 20 70 2d 3e 63 6f 6e 74 65 78 74 53  ert( p->contextS
1d3bf 74 61 63 6b 54 6f 70 3e 3d 30 20 29 3b 0a 20 20  tackTop>=0 );.  
1d3c0 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  db->lastRowid = 
1d3c1 70 43 6f 6e 74 65 78 74 2d 3e 6c 61 73 74 52 6f  pContext->lastRo
1d3c2 77 69 64 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67  wid;.  p->nChang
1d3c3 65 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 6e 43  e = pContext->nC
1d3c4 68 61 6e 67 65 3b 0a 20 20 62 72 65 61 6b 3b 0a  hange;.  break;.
1d3c5 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e  }.#endif /* #ifn
1d3c6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1d3c7 54 52 49 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e  TRIGGER */..#ifn
1d3c8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1d3c9 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a  AUTOINCREMENT./*
1d3ca 20 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20   Opcode: MemMax 
1d3cb 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1d3cc 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 20  * Set the value 
1d3cd 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 74  of register P1 t
1d3ce 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66  o the maximum of
1d3cf 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 61 6c   its current val
1d3d0 75 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76 61  ue.** and the va
1d3d1 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1d3d2 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  P2..**.** This i
1d3d3 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77  nstruction throw
1d3d4 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68  s an error if th
1d3d5 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73  e memory cell is
1d3d6 20 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a   not initially.*
1d3d7 2a 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f  * an integer..*/
1d3d8 0a 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a  .case OP_MemMax:
1d3d9 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31   {        /* in1
1d3da 2c 20 69 6e 32 20 2a 2f 0a 20 20 73 71 6c 69 74  , in2 */.  sqlit
1d3db 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1d3dc 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c  ify(pIn1);.  sql
1d3dd 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
1d3de 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 69  erify(pIn2);.  i
1d3df 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e  f( pIn1->u.i<pIn
1d3e0 32 2d 3e 75 2e 69 29 7b 0a 20 20 20 20 70 49 6e  2->u.i){.    pIn
1d3e1 31 2d 3e 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75  1->u.i = pIn2->u
1d3e2 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  .i;.  }.  break;
1d3e3 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1d3e4 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
1d3e5 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70  REMENT */../* Op
1d3e6 63 6f 64 65 3a 20 49 66 50 6f 73 20 50 31 20 50  code: IfPos P1 P
1d3e7 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  2 * * *.**.** If
1d3e8 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65   the value of re
1d3e9 67 69 73 74 65 72 20 50 31 20 69 73 20 31 20 6f  gister P1 is 1 o
1d3ea 72 20 67 72 65 61 74 65 72 2c 20 6a 75 6d 70 20  r greater, jump 
1d3eb 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  to P2..**.** It 
1d3ec 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73  is illegal to us
1d3ed 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  e this instructi
1d3ee 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72  on on a register
1d3ef 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f   that does.** no
1d3f0 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74  t contain an int
1d3f1 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74  eger.  An assert
1d3f2 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72  ion fault will r
1d3f3 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79  esult if you try
1d3f4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50  ..*/.case OP_IfP
1d3f5 6f 73 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  os: {        /* 
1d3f6 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61  jump, in1 */.  a
1d3f7 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61  ssert( pIn1->fla
1d3f8 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20  gs&MEM_Int );.  
1d3f9 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20  if( pIn1->u.i>0 
1d3fa 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70  ){.     pc = pOp
1d3fb 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20  ->p2 - 1;.  }.  
1d3fc 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1d3fd 6f 64 65 3a 20 49 66 4e 65 67 20 50 31 20 50 32  ode: IfNeg P1 P2
1d3fe 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20   * * *.**.** If 
1d3ff 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67  the value of reg
1d400 69 73 74 65 72 20 50 31 20 69 73 20 6c 65 73 73  ister P1 is less
1d401 20 74 68 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70   than zero, jump
1d402 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49   to P2. .**.** I
1d403 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20  t is illegal to 
1d404 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 63  use this instruc
1d405 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74  tion on a regist
1d406 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20  er that does.** 
1d407 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69  not contain an i
1d408 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65  nteger.  An asse
1d409 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c  rtion fault will
1d40a 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74   result if you t
1d40b 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  ry..*/.case OP_I
1d40c 66 4e 65 67 3a 20 7b 20 20 20 20 20 20 20 20 2f  fNeg: {        /
1d40d 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20  * jump, in1 */. 
1d40e 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66   assert( pIn1->f
1d40f 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a  lags&MEM_Int );.
1d410 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c    if( pIn1->u.i<
1d411 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 70  0 ){.     pc = p
1d412 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
1d413 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d414 70 63 6f 64 65 3a 20 49 66 5a 65 72 6f 20 50 31  pcode: IfZero P1
1d415 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1d416 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  If the value of 
1d417 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 65  register P1 is e
1d418 78 61 63 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74  xactly 0, jump t
1d419 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20  o P2. .**.** It 
1d41a 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73  is illegal to us
1d41b 65 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  e this instructi
1d41c 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72  on on a register
1d41d 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f   that does.** no
1d41e 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74  t contain an int
1d41f 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74  eger.  An assert
1d420 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72  ion fault will r
1d421 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79  esult if you try
1d422 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a  ..*/.case OP_IfZ
1d423 65 72 6f 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  ero: {        /*
1d424 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20   jump, in1 */.  
1d425 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c  assert( pIn1->fl
1d426 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  ags&MEM_Int );. 
1d427 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d   if( pIn1->u.i==
1d428 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 70  0 ){.     pc = p
1d429 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a  Op->p2 - 1;.  }.
1d42a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d42b 70 63 6f 64 65 3a 20 41 67 67 53 74 65 70 20 2a  pcode: AggStep *
1d42c 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1d42d 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 73  ** Execute the s
1d42e 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  tep function for
1d42f 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e 20 20   an aggregate.  
1d430 54 68 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  The.** function 
1d431 68 61 73 20 50 35 20 61 72 67 75 6d 65 6e 74 73  has P5 arguments
1d432 2e 20 20 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .   P4 is a poin
1d433 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44  ter to the FuncD
1d434 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  ef.** structure 
1d435 74 68 61 74 20 73 70 65 63 69 66 69 65 73 20 74  that specifies t
1d436 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73  he function.  Us
1d437 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33  e register.** P3
1d438 20 61 73 20 74 68 65 20 61 63 63 75 6d 75 6c 61   as the accumula
1d439 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50  tor..**.** The P
1d43a 35 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  5 arguments are 
1d43b 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73  taken from regis
1d43c 74 65 72 20 50 32 20 61 6e 64 20 69 74 73 0a 2a  ter P2 and its.*
1d43d 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f  * successors..*/
1d43e 0a 63 61 73 65 20 4f 50 5f 41 67 67 53 74 65 70  .case OP_AggStep
1d43f 3a 20 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f  : {.  int n = pO
1d440 70 2d 3e 70 35 3b 0a 20 20 69 6e 74 20 69 3b 0a  p->p5;.  int i;.
1d441 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 2a 70 52    Mem *pMem, *pR
1d442 65 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ec;.  sqlite3_co
1d443 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71 6c  ntext ctx;.  sql
1d444 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56  ite3_value **apV
1d445 61 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e  al;..  assert( n
1d446 3e 3d 30 20 29 3b 0a 20 20 70 52 65 63 20 3d 20  >=0 );.  pRec = 
1d447 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1d448 5d 3b 0a 20 20 61 70 56 61 6c 20 3d 20 70 2d 3e  ];.  apVal = p->
1d449 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28  apArg;.  assert(
1d44a 20 61 70 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29   apVal || n==0 )
1d44b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
1d44c 3b 20 69 2b 2b 2c 20 70 52 65 63 2b 2b 29 7b 0a  ; i++, pRec++){.
1d44d 20 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70      apVal[i] = p
1d44e 52 65 63 3b 0a 20 20 20 20 73 74 6f 72 65 54 79  Rec;.    storeTy
1d44f 70 65 49 6e 66 6f 28 70 52 65 63 2c 20 65 6e 63  peInfo(pRec, enc
1d450 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 63 74  oding);.  }.  ct
1d451 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70  x.pFunc = pOp->p
1d452 34 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73 65 72  4.pFunc;.  asser
1d453 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
1d454 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1d455 20 29 3b 0a 20 20 63 74 78 2e 70 4d 65 6d 20 3d   );.  ctx.pMem =
1d456 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d   pMem = &p->aMem
1d457 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 70 4d 65  [pOp->p3];.  pMe
1d458 6d 2d 3e 6e 2b 2b 3b 0a 20 20 63 74 78 2e 73 2e  m->n++;.  ctx.s.
1d459 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1d45a 3b 0a 20 20 63 74 78 2e 73 2e 7a 20 3d 20 30 3b  ;.  ctx.s.z = 0;
1d45b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63  .  ctx.s.zMalloc
1d45c 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 78 44   = 0;.  ctx.s.xD
1d45d 65 6c 20 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e  el = 0;.  ctx.s.
1d45e 64 62 20 3d 20 64 62 3b 0a 20 20 63 74 78 2e 69  db = db;.  ctx.i
1d45f 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 63 74  sError = 0;.  ct
1d460 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69  x.pColl = 0;.  i
1d461 66 28 20 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c  f( ctx.pFunc->fl
1d462 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e  ags & SQLITE_FUN
1d463 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20  C_NEEDCOLL ){.  
1d464 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d    assert( pOp>p-
1d465 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65  >aOp );.    asse
1d466 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79  rt( pOp[-1].p4ty
1d467 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29  pe==P4_COLLSEQ )
1d468 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ;.    assert( pO
1d469 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50  p[-1].opcode==OP
1d46a 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20  _CollSeq );.    
1d46b 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b  ctx.pColl = pOp[
1d46c 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20  -1].p4.pColl;.  
1d46d 7d 0a 20 20 28 63 74 78 2e 70 46 75 6e 63 2d 3e  }.  (ctx.pFunc->
1d46e 78 53 74 65 70 29 28 26 63 74 78 2c 20 6e 2c 20  xStep)(&ctx, n, 
1d46f 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 63 74  apVal);.  if( ct
1d470 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20  x.isError ){.   
1d471 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1d472 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1d473 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33  b, "%s", sqlite3
1d474 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 63 74 78  _value_text(&ctx
1d475 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 63  .s));.    rc = c
1d476 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a  tx.isError;.  }.
1d477 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1d478 52 65 6c 65 61 73 65 28 26 63 74 78 2e 73 29 3b  Release(&ctx.s);
1d479 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d47a 4f 70 63 6f 64 65 3a 20 41 67 67 46 69 6e 61 6c  Opcode: AggFinal
1d47b 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a   P1 P2 * P4 *.**
1d47c 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65 20  .** Execute the 
1d47d 66 69 6e 61 6c 69 7a 65 72 20 66 75 6e 63 74 69  finalizer functi
1d47e 6f 6e 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67  on for an aggreg
1d47f 61 74 65 2e 20 20 50 31 20 69 73 0a 2a 2a 20 74  ate.  P1 is.** t
1d480 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
1d481 6f 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 61  on that is the a
1d482 63 63 75 6d 75 6c 61 74 6f 72 20 66 6f 72 20 74  ccumulator for t
1d483 68 65 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a  he aggregate..**
1d484 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75  .** P2 is the nu
1d485 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
1d486 73 20 74 68 61 74 20 74 68 65 20 73 74 65 70 20  s that the step 
1d487 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61  function takes a
1d488 6e 64 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f  nd.** P4 is a po
1d489 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e  inter to the Fun
1d48a 63 44 65 66 20 66 6f 72 20 74 68 69 73 20 66 75  cDef for this fu
1d48b 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 50 32 0a  nction.  The P2.
1d48c 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  ** argument is n
1d48d 6f 74 20 75 73 65 64 20 62 79 20 74 68 69 73 20  ot used by this 
1d48e 6f 70 63 6f 64 65 2e 20 20 49 74 20 69 73 20 6f  opcode.  It is o
1d48f 6e 6c 79 20 74 68 65 72 65 20 74 6f 20 64 69 73  nly there to dis
1d490 61 6d 62 69 67 75 61 74 65 0a 2a 2a 20 66 75 6e  ambiguate.** fun
1d491 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20  ctions that can 
1d492 74 61 6b 65 20 76 61 72 79 69 6e 67 20 6e 75 6d  take varying num
1d493 62 65 72 73 20 6f 66 20 61 72 67 75 6d 65 6e 74  bers of argument
1d494 73 2e 20 20 54 68 65 0a 2a 2a 20 50 34 20 61 72  s.  The.** P4 ar
1d495 67 75 6d 65 6e 74 20 69 73 20 6f 6e 6c 79 20 6e  gument is only n
1d496 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64 65  eeded for the de
1d497 67 65 6e 65 72 61 74 65 20 63 61 73 65 20 77 68  generate case wh
1d498 65 72 65 0a 2a 2a 20 74 68 65 20 73 74 65 70 20  ere.** the step 
1d499 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74  function was not
1d49a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c   previously call
1d49b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41  ed..*/.case OP_A
1d49c 67 67 46 69 6e 61 6c 3a 20 7b 0a 20 20 4d 65 6d  ggFinal: {.  Mem
1d49d 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74   *pMem;.  assert
1d49e 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70  ( pOp->p1>0 && p
1d49f 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1d4a0 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e  );.  pMem = &p->
1d4a1 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  aMem[pOp->p1];. 
1d4a2 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
1d4a3 66 6c 61 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75  flags & ~(MEM_Nu
1d4a4 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20  ll|MEM_Agg))==0 
1d4a5 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1d4a6 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65  3VdbeMemFinalize
1d4a7 28 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70  (pMem, pOp->p4.p
1d4a8 46 75 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 3d  Func);.  if( rc=
1d4a9 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b  =SQLITE_ERROR ){
1d4aa 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1d4ab 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1d4ac 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
1d4ad 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1d4ae 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71  pMem));.  }.  sq
1d4af 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
1d4b0 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e  ncoding(pMem, en
1d4b1 63 6f 64 69 6e 67 29 3b 0a 20 20 55 50 44 41 54  coding);.  UPDAT
1d4b2 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1d4b3 4d 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Mem);.  if( sqli
1d4b4 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1d4b5 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f  (pMem) ){.    go
1d4b6 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1d4b7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66    break;.}...#if
1d4b8 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1d4b9 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 26 26  _OMIT_VACUUM) &&
1d4ba 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1d4bb 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a  _OMIT_ATTACH)./*
1d4bc 20 4f 70 63 6f 64 65 3a 20 56 61 63 75 75 6d 20   Opcode: Vacuum 
1d4bd 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  * * * * *.**.** 
1d4be 56 61 63 75 75 6d 20 74 68 65 20 65 6e 74 69 72  Vacuum the entir
1d4bf 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
1d4c0 73 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 63 61  s opcode will ca
1d4c1 75 73 65 20 6f 74 68 65 72 20 76 69 72 74 75 61  use other virtua
1d4c2 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f  l.** machines to
1d4c3 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64 20   be created and 
1d4c4 72 75 6e 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74  run.  It may not
1d4c5 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
1d4c6 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e  within.** a tran
1d4c7 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  saction..*/.case
1d4c8 20 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 0a 20 20   OP_Vacuum: {.  
1d4c9 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1d4ca 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20  yOff(db) ) goto 
1d4cb 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1d4cc 75 73 65 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c  use; .  rc = sql
1d4cd 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 26 70  ite3RunVacuum(&p
1d4ce 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a  ->zErrMsg, db);.
1d4cf 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1d4d0 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
1d4d1 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1d4d2 73 75 73 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  suse;.  break;.}
1d4d3 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65  .#endif..#if !de
1d4d4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1d4d5 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 2f 2a  T_AUTOVACUUM)./*
1d4d6 20 4f 70 63 6f 64 65 3a 20 49 6e 63 72 56 61 63   Opcode: IncrVac
1d4d7 75 75 6d 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  uum P1 P2 * * *.
1d4d8 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20  **.** Perform a 
1d4d9 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 74  single step of t
1d4da 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76  he incremental v
1d4db 61 63 75 75 6d 20 70 72 6f 63 65 64 75 72 65 20  acuum procedure 
1d4dc 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 20 64 61 74  on.** the P1 dat
1d4dd 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 76 61  abase. If the va
1d4de 63 75 75 6d 20 68 61 73 20 66 69 6e 69 73 68 65  cuum has finishe
1d4df 64 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72  d, jump to instr
1d4e0 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f 74  uction.** P2. Ot
1d4e1 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68  herwise, fall th
1d4e2 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1d4e3 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
1d4e4 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 63 72 56 61  /.case OP_IncrVa
1d4e5 63 75 75 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f  cuum: {        /
1d4e6 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 42 74 72 65  * jump */.  Btre
1d4e7 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73 73 65 72  e *pBt;..  asser
1d4e8 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1d4e9 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62   pOp->p1<db->nDb
1d4ea 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
1d4eb 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1d4ec 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29  <<pOp->p1))!=0 )
1d4ed 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44  ;.  pBt = db->aD
1d4ee 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a  b[pOp->p1].pBt;.
1d4ef 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1d4f0 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 70 42  reeIncrVacuum(pB
1d4f1 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
1d4f2 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
1d4f3 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1d4f4 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  1;.    rc = SQLI
1d4f5 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65  TE_OK;.  }.  bre
1d4f6 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ak;.}.#endif../*
1d4f7 20 4f 70 63 6f 64 65 3a 20 45 78 70 69 72 65 20   Opcode: Expire 
1d4f8 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1d4f9 20 43 61 75 73 65 20 70 72 65 63 6f 6d 70 69 6c   Cause precompil
1d4fa 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ed statements to
1d4fb 20 62 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e   become expired.
1d4fc 20 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 74   An expired stat
1d4fd 65 6d 65 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77  ement.** fails w
1d4fe 69 74 68 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  ith an error cod
1d4ff 65 20 6f 66 20 53 51 4c 49 54 45 5f 53 43 48 45  e of SQLITE_SCHE
1d500 4d 41 20 69 66 20 69 74 20 69 73 20 65 76 65 72  MA if it is ever
1d501 20 65 78 65 63 75 74 65 64 20 0a 2a 2a 20 28 76   executed .** (v
1d502 69 61 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ia sqlite3_step(
1d503 29 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31  ))..** .** If P1
1d504 20 69 73 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20   is 0, then all 
1d505 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62  SQL statements b
1d506 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 49  ecome expired. I
1d507 66 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  f P1 is non-zero
1d508 2c 0a 2a 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74  ,.** then only t
1d509 68 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  he currently exe
1d50a 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  cuting statement
1d50b 20 69 73 20 61 66 66 65 63 74 65 64 2e 20 0a 2a   is affected. .*
1d50c 2f 0a 63 61 73 65 20 4f 50 5f 45 78 70 69 72 65  /.case OP_Expire
1d50d 3a 20 7b 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e  : {.  if( !pOp->
1d50e 70 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  p1 ){.    sqlite
1d50f 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
1d510 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20  tatements(db);. 
1d511 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65   }else{.    p->e
1d512 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  xpired = 1;.  }.
1d513 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e    break;.}..#ifn
1d514 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1d515 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20  SHARED_CACHE./* 
1d516 4f 70 63 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63  Opcode: TableLoc
1d517 6b 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  k P1 P2 P3 P4 *.
1d518 2a 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c  **.** Obtain a l
1d519 6f 63 6b 20 6f 6e 20 61 20 70 61 72 74 69 63 75  ock on a particu
1d51a 6c 61 72 20 74 61 62 6c 65 2e 20 54 68 69 73 20  lar table. This 
1d51b 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f  instruction is o
1d51c 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a  nly used when.**
1d51d 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
1d51e 65 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61  e feature is ena
1d51f 62 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  bled. .**.** If 
1d520 50 31 20 69 73 20 20 74 68 65 20 69 6e 64 65 78  P1 is  the index
1d521 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1d522 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b   in sqlite3.aDb[
1d523 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  ] of the databas
1d524 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20 74 68  e.** on which th
1d525 65 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72  e lock is acquir
1d526 65 64 2e 20 20 41 20 72 65 61 64 6c 6f 63 6b 20  ed.  A readlock 
1d527 69 73 20 6f 62 74 61 69 6e 65 64 20 69 66 20 50  is obtained if P
1d528 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 20 77 72 69  3==0 or.** a wri
1d529 74 65 20 6c 6f 63 6b 20 69 66 20 50 33 3d 3d 31  te lock if P3==1
1d52a 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74 61  ..**.** P2 conta
1d52b 69 6e 73 20 74 68 65 20 72 6f 6f 74 2d 70 61 67  ins the root-pag
1d52c 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
1d52d 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34  o lock..**.** P4
1d52e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e   contains a poin
1d52f 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20  ter to the name 
1d530 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  of the table bei
1d531 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 68 69 73 20  ng locked. This 
1d532 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64 20  is only.** used 
1d533 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  to generate an e
1d534 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 66 20  rror message if 
1d535 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20  the lock cannot 
1d536 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a  be obtained..*/.
1d537 63 61 73 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63  case OP_TableLoc
1d538 6b 3a 20 7b 0a 20 20 69 6e 74 20 70 31 20 3d 20  k: {.  int p1 = 
1d539 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 75 38 20 69  pOp->p1; .  u8 i
1d53a 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38  sWriteLock = (u8
1d53b 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65  )pOp->p3;.  asse
1d53c 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 31 3c  rt( p1>=0 && p1<
1d53d 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
1d53e 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
1d53f 73 6b 20 26 20 28 31 3c 3c 70 31 29 29 21 3d 30  sk & (1<<p1))!=0
1d540 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73   );.  assert( is
1d541 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20  WriteLock==0 || 
1d542 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29  isWriteLock==1 )
1d543 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1d544 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64  BtreeLockTable(d
1d545 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20  b->aDb[p1].pBt, 
1d546 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 69 74 65  pOp->p2, isWrite
1d547 4c 6f 63 6b 29 3b 0a 20 20 69 66 28 20 28 72 63  Lock);.  if( (rc
1d548 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c  &0xFF)==SQLITE_L
1d549 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 63 6f 6e  OCKED ){.    con
1d54a 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70  st char *z = pOp
1d54b 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 73 71 6c 69  ->p4.z;.    sqli
1d54c 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1d54d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 64  >zErrMsg, db, "d
1d54e 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 69 73  atabase table is
1d54f 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29   locked: %s", z)
1d550 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d551 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1d552 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
1d553 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  CHE */..#ifndef 
1d554 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1d555 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
1d556 64 65 3a 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a  de: VBegin * * *
1d557 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d   P4 *.**.** P4 m
1d558 61 79 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20  ay be a pointer 
1d559 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  to an sqlite3_vt
1d55a 61 62 20 73 74 72 75 63 74 75 72 65 2e 20 49 66  ab structure. If
1d55b 20 73 6f 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a   so, call the .*
1d55c 2a 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20  * xBegin method 
1d55d 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a  for that table..
1d55e 2a 2a 0a 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74  **.** Also, whet
1d55f 68 65 72 20 6f 72 20 6e 6f 74 20 50 34 20 69 73  her or not P4 is
1d560 20 73 65 74 2c 20 63 68 65 63 6b 20 74 68 61 74   set, check that
1d561 20 74 68 69 73 20 69 73 20 6e 6f 74 20 62 65 69   this is not bei
1d562 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a  ng called from.*
1d563 2a 20 77 69 74 68 69 6e 20 61 20 63 61 6c 6c 62  * within a callb
1d564 61 63 6b 20 74 6f 20 61 20 76 69 72 74 75 61 6c  ack to a virtual
1d565 20 74 61 62 6c 65 20 78 53 79 6e 63 28 29 20 6d   table xSync() m
1d566 65 74 68 6f 64 2e 20 49 66 20 69 74 20 69 73 2c  ethod. If it is,
1d567 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f   the error.** co
1d568 64 65 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74  de will be set t
1d569 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e  o SQLITE_LOCKED.
1d56a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65 67  .*/.case OP_VBeg
1d56b 69 6e 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  in: {.  sqlite3_
1d56c 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f  vtab *pVtab = pO
1d56d 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 72  p->p4.pVtab;.  r
1d56e 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 42  c = sqlite3VtabB
1d56f 65 67 69 6e 28 64 62 2c 20 70 56 74 61 62 29 3b  egin(db, pVtab);
1d570 0a 20 20 69 66 28 20 70 56 74 61 62 20 29 7b 0a  .  if( pVtab ){.
1d571 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1d572 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1d573 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73  );.    p->zErrMs
1d574 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  g = pVtab->zErrM
1d575 73 67 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e 7a  sg;.    pVtab->z
1d576 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a  ErrMsg = 0;.  }.
1d577 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1d578 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1d579 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1d57a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1d57b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1d57c 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43  LE./* Opcode: VC
1d57d 72 65 61 74 65 20 50 31 20 2a 20 2a 20 50 34 20  reate P1 * * P4 
1d57e 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68  *.**.** P4 is th
1d57f 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74  e name of a virt
1d580 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74  ual table in dat
1d581 61 62 61 73 65 20 50 31 2e 20 43 61 6c 6c 20 74  abase P1. Call t
1d582 68 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f  he xCreate metho
1d583 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20 74 61  d.** for that ta
1d584 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ble..*/.case OP_
1d585 56 43 72 65 61 74 65 3a 20 7b 0a 20 20 72 63 20  VCreate: {.  rc 
1d586 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  = sqlite3VtabCal
1d587 6c 43 72 65 61 74 65 28 64 62 2c 20 70 4f 70 2d  lCreate(db, pOp-
1d588 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20  >p1, pOp->p4.z, 
1d589 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  &p->zErrMsg);.  
1d58a 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1d58b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1d58c 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1d58d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1d58e 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1d58f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 44 65 73  ./* Opcode: VDes
1d590 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 34 20 2a  troy P1 * * P4 *
1d591 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65  .**.** P4 is the
1d592 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75   name of a virtu
1d593 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61  al table in data
1d594 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c 20 74  base P1.  Call t
1d595 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68  he xDestroy meth
1d596 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 61  od.** of that ta
1d597 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ble..*/.case OP_
1d598 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d  VDestroy: {.  p-
1d599 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
1d59a 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  2;.  rc = sqlite
1d59b 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79  3VtabCallDestroy
1d59c 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  (db, pOp->p1, pO
1d59d 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69  p->p4.z);.  p->i
1d59e 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b  nVtabMethod = 0;
1d59f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1d5a0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1d5a1 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1d5a2 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1d5a3 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1d5a4 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1d5a5 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a  Open P1 * * P4 *
1d5a6 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  .**.** P4 is a p
1d5a7 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74  ointer to a virt
1d5a8 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
1d5a9 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  , an sqlite3_vta
1d5aa 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  b structure..** 
1d5ab 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20 6e  P1 is a cursor n
1d5ac 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f 70 63  umber.  This opc
1d5ad 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75 72 73  ode opens a curs
1d5ae 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74 75 61  or to the virtua
1d5af 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 73  l.** table and s
1d5b0 74 6f 72 65 73 20 74 68 61 74 20 63 75 72 73 6f  tores that curso
1d5b1 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65  r in P1..*/.case
1d5b2 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a 20 20 56   OP_VOpen: {.  V
1d5b3 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 20  dbeCursor *pCur 
1d5b4 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  = 0;.  sqlite3_v
1d5b5 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61  tab_cursor *pVta
1d5b6 62 43 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20  bCursor = 0;..  
1d5b7 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1d5b8 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56  tab = pOp->p4.pV
1d5b9 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  tab;.  sqlite3_m
1d5ba 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d  odule *pModule =
1d5bb 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65   (sqlite3_module
1d5bc 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   *)pVtab->pModul
1d5bd 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 56 74  e;..  assert(pVt
1d5be 61 62 20 26 26 20 70 4d 6f 64 75 6c 65 29 3b 0a  ab && pModule);.
1d5bf 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1d5c0 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
1d5c1 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1d5c2 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70 4d  isuse;.  rc = pM
1d5c3 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 56 74  odule->xOpen(pVt
1d5c4 61 62 2c 20 26 70 56 74 61 62 43 75 72 73 6f 72  ab, &pVtabCursor
1d5c5 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1d5c6 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1d5c7 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67  g);.  p->zErrMsg
1d5c8 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73   = pVtab->zErrMs
1d5c9 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72  g;.  pVtab->zErr
1d5ca 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  Msg = 0;.  if( s
1d5cb 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1d5cc 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1d5cd 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1d5ce 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d   if( SQLITE_OK==
1d5cf 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 69  rc ){.    /* Ini
1d5d0 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33 5f  tialize sqlite3_
1d5d1 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65  vtab_cursor base
1d5d2 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 70 56   class */.    pV
1d5d3 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1d5d4 20 3d 20 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f   = pVtab;..    /
1d5d5 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62  * Initialise vdb
1d5d6 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20  e cursor object 
1d5d7 2a 2f 0a 20 20 20 20 70 43 75 72 20 3d 20 61 6c  */.    pCur = al
1d5d8 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20  locateCursor(p, 
1d5d9 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20  pOp->p1, 0, -1, 
1d5da 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72  0);.    if( pCur
1d5db 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e   ){.      pCur->
1d5dc 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70 56  pVtabCursor = pV
1d5dd 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  tabCursor;.     
1d5de 20 70 43 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d   pCur->pModule =
1d5df 20 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56   pVtabCursor->pV
1d5e0 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  tab->pModule;.  
1d5e1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
1d5e2 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1d5e3 3d 20 31 3b 0a 20 20 20 20 20 20 70 4d 6f 64 75  = 1;.      pModu
1d5e4 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74 61 62  le->xClose(pVtab
1d5e5 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20  Cursor);.    }. 
1d5e6 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1d5e7 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1d5e8 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1d5e9 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1d5ea 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1d5eb 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
1d5ec 20 56 46 69 6c 74 65 72 20 50 31 20 50 32 20 50   VFilter P1 P2 P
1d5ed 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20  3 P4 *.**.** P1 
1d5ee 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e  is a cursor open
1d5ef 65 64 20 75 73 69 6e 67 20 56 4f 70 65 6e 2e 20  ed using VOpen. 
1d5f0 20 50 32 20 69 73 20 61 6e 20 61 64 64 72 65 73   P2 is an addres
1d5f1 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 0a  s to jump to if.
1d5f2 2a 2a 20 74 68 65 20 66 69 6c 74 65 72 65 64 20  ** the filtered 
1d5f3 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d  result set is em
1d5f4 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  pty..**.** P4 is
1d5f5 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20   either NULL or 
1d5f6 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 77 61  a string that wa
1d5f7 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74  s generated by t
1d5f8 68 65 20 78 42 65 73 74 49 6e 64 65 78 0a 2a 2a  he xBestIndex.**
1d5f9 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d   method of the m
1d5fa 6f 64 75 6c 65 2e 20 20 54 68 65 20 69 6e 74 65  odule.  The inte
1d5fb 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68  rpretation of th
1d5fc 65 20 50 34 20 73 74 72 69 6e 67 20 69 73 20 6c  e P4 string is l
1d5fd 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6d 6f  eft.** to the mo
1d5fe 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
1d5ff 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
1d600 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74  opcode invokes t
1d601 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f  he xFilter metho
1d602 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61 6c  d on the virtual
1d603 20 74 61 62 6c 65 20 73 70 65 63 69 66 69 65 64   table specified
1d604 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54 68 65 20  .** by P1.  The 
1d605 69 6e 74 65 67 65 72 20 71 75 65 72 79 20 70 6c  integer query pl
1d606 61 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  an parameter to 
1d607 78 46 69 6c 74 65 72 20 69 73 20 73 74 6f 72 65  xFilter is store
1d608 64 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a  d in register.**
1d609 20 50 33 2e 20 52 65 67 69 73 74 65 72 20 50 33   P3. Register P3
1d60a 2b 31 20 73 74 6f 72 65 73 20 74 68 65 20 61 72  +1 stores the ar
1d60b 67 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gc parameter to 
1d60c 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  be passed to the
1d60d 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d 65 74 68  .** xFilter meth
1d60e 6f 64 2e 20 52 65 67 69 73 74 65 72 73 20 50 33  od. Registers P3
1d60f 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63 20 61 72  +2..P3+1+argc ar
1d610 65 20 74 68 65 20 61 72 67 63 0a 2a 2a 20 61 64  e the argc.** ad
1d611 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74  ditional paramet
1d612 65 72 73 20 77 68 69 63 68 20 61 72 65 20 70 61  ers which are pa
1d613 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46 69 6c 74  ssed to.** xFilt
1d614 65 72 20 61 73 20 61 72 67 76 2e 20 52 65 67 69  er as argv. Regi
1d615 73 74 65 72 20 50 33 2b 32 20 62 65 63 6f 6d 65  ster P3+2 become
1d616 73 20 61 72 67 76 5b 30 5d 20 77 68 65 6e 20 70  s argv[0] when p
1d617 61 73 73 65 64 20 74 6f 20 78 46 69 6c 74 65 72  assed to xFilter
1d618 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20 69  ..**.** A jump i
1d619 73 20 6d 61 64 65 20 74 6f 20 50 32 20 69 66 20  s made to P2 if 
1d61a 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 61  the result set a
1d61b 66 74 65 72 20 66 69 6c 74 65 72 69 6e 67 20 77  fter filtering w
1d61c 6f 75 6c 64 20 62 65 20 65 6d 70 74 79 2e 0a 2a  ould be empty..*
1d61d 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69 6c 74 65  /.case OP_VFilte
1d61e 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a  r: {   /* jump *
1d61f 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20  /.  int nArg;.  
1d620 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 63 6f  int iQuery;.  co
1d621 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
1d622 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d  le *pModule;.  M
1d623 65 6d 20 2a 70 51 75 65 72 79 20 3d 20 26 70 2d  em *pQuery = &p-
1d624 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1d625 20 20 4d 65 6d 20 2a 70 41 72 67 63 20 3d 20 26    Mem *pArgc = &
1d626 70 51 75 65 72 79 5b 31 5d 3b 0a 20 20 73 71 6c  pQuery[1];.  sql
1d627 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
1d628 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20   *pVtabCursor;. 
1d629 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1d62a 56 74 61 62 3b 0a 0a 20 20 56 64 62 65 43 75 72  Vtab;..  VdbeCur
1d62b 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61  sor *pCur = p->a
1d62c 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 0a  pCsr[pOp->p1];..
1d62d 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1d62e 28 70 4f 70 2d 3e 70 33 2c 20 70 51 75 65 72 79  (pOp->p3, pQuery
1d62f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
1d630 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29  r->pVtabCursor )
1d631 3b 0a 20 20 70 56 74 61 62 43 75 72 73 6f 72 20  ;.  pVtabCursor 
1d632 3d 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  = pCur->pVtabCur
1d633 73 6f 72 3b 0a 20 20 70 56 74 61 62 20 3d 20 70  sor;.  pVtab = p
1d634 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61  VtabCursor->pVta
1d635 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70  b;.  pModule = p
1d636 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a  Vtab->pModule;..
1d637 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20 69 6e    /* Grab the in
1d638 64 65 78 20 6e 75 6d 62 65 72 20 61 6e 64 20 61  dex number and a
1d639 72 67 63 20 70 61 72 61 6d 65 74 65 72 73 20 2a  rgc parameters *
1d63a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 51 75  /.  assert( (pQu
1d63b 65 72 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49  ery->flags&MEM_I
1d63c 6e 74 29 21 3d 30 20 26 26 20 70 41 72 67 63 2d  nt)!=0 && pArgc-
1d63d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20  >flags==MEM_Int 
1d63e 29 3b 0a 20 20 6e 41 72 67 20 3d 20 28 69 6e 74  );.  nArg = (int
1d63f 29 70 41 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 69  )pArgc->u.i;.  i
1d640 51 75 65 72 79 20 3d 20 28 69 6e 74 29 70 51 75  Query = (int)pQu
1d641 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20  ery->u.i;..  /* 
1d642 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c 74  Invoke the xFilt
1d643 65 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b  er method */.  {
1d644 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20 30  .    int res = 0
1d645 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
1d646 20 4d 65 6d 20 2a 2a 61 70 41 72 67 20 3d 20 70   Mem **apArg = p
1d647 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 66 6f 72  ->apArg;.    for
1d648 28 69 20 3d 20 30 3b 20 69 3c 6e 41 72 67 3b 20  (i = 0; i<nArg; 
1d649 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 70 41 72  i++){.      apAr
1d64a 67 5b 69 5d 20 3d 20 26 70 41 72 67 63 5b 69 2b  g[i] = &pArgc[i+
1d64b 31 5d 3b 0a 20 20 20 20 20 20 73 74 6f 72 65 54  1];.      storeT
1d64c 79 70 65 49 6e 66 6f 28 61 70 41 72 67 5b 69 5d  ypeInfo(apArg[i]
1d64d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  , 0);.    }..   
1d64e 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1d64f 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1d650 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1d651 73 75 73 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  suse;.    sqlite
1d652 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29  3VtabLock(pVtab)
1d653 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d  ;.    p->inVtabM
1d654 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 72  ethod = 1;.    r
1d655 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 46 69  c = pModule->xFi
1d656 6c 74 65 72 28 70 56 74 61 62 43 75 72 73 6f 72  lter(pVtabCursor
1d657 2c 20 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70  , iQuery, pOp->p
1d658 34 2e 7a 2c 20 6e 41 72 67 2c 20 61 70 41 72 67  4.z, nArg, apArg
1d659 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62  );.    p->inVtab
1d65a 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20 20  Method = 0;.    
1d65b 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1d65c 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1d65d 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
1d65e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a  pVtab->zErrMsg;.
1d65f 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d      pVtab->zErrM
1d660 73 67 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  sg = 0;.    sqli
1d661 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62  te3VtabUnlock(db
1d662 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66  , pVtab);.    if
1d663 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1d664 29 7b 0a 20 20 20 20 20 20 72 65 73 20 3d 20 70  ){.      res = p
1d665 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 70 56 74  Module->xEof(pVt
1d666 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d  abCursor);.    }
1d667 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1d668 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1d669 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1d66a 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 20 69 66  _misuse;..    if
1d66b 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70  ( res ){.      p
1d66c 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1d66d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75  .    }.  }.  pCu
1d66e 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a  r->nullRow = 0;.
1d66f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1d670 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1d671 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1d672 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1d673 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1d674 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1d675 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20  Column P1 P2 P3 
1d676 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20  * *.**.** Store 
1d677 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1d678 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66   P2-th column of
1d679 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66 20 74  .** the row of t
1d67a 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
1d67b 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 50 31   that the .** P1
1d67c 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74   cursor is point
1d67d 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 65 67 69  ing to into regi
1d67e 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65  ster P3..*/.case
1d67f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 20   OP_VColumn: {. 
1d680 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1d681 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71  Vtab;.  const sq
1d682 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1d683 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44  odule;.  Mem *pD
1d684 65 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63  est;.  sqlite3_c
1d685 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b  ontext sContext;
1d686 0a 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ..  VdbeCursor *
1d687 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b  pCur = p->apCsr[
1d688 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1d689 72 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43  rt( pCur->pVtabC
1d68a 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72  ursor );.  asser
1d68b 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
1d68c 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1d68d 20 29 3b 0a 20 20 70 44 65 73 74 20 3d 20 26 70   );.  pDest = &p
1d68e 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1d68f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c  .  if( pCur->nul
1d690 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  lRow ){.    sqli
1d691 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1d692 6c 28 70 44 65 73 74 29 3b 0a 20 20 20 20 62 72  l(pDest);.    br
1d693 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 56 74 61 62  eak;.  }.  pVtab
1d694 20 3d 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75   = pCur->pVtabCu
1d695 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 70  rsor->pVtab;.  p
1d696 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e  Module = pVtab->
1d697 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72  pModule;.  asser
1d698 74 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c  t( pModule->xCol
1d699 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28  umn );.  memset(
1d69a 26 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69  &sContext, 0, si
1d69b 7a 65 6f 66 28 73 43 6f 6e 74 65 78 74 29 29 3b  zeof(sContext));
1d69c 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75  ..  /* The outpu
1d69d 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61  t cell may alrea
1d69e 64 79 20 68 61 76 65 20 61 20 62 75 66 66 65 72  dy have a buffer
1d69f 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65   allocated. Move
1d6a0 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e  .  ** the curren
1d6a1 74 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 73 43  t contents to sC
1d6a2 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 6e 20 63  ontext.s so in c
1d6a3 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 75 6e  ase the user-fun
1d6a4 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 63 61 6e 20  ction .  ** can 
1d6a5 75 73 65 20 74 68 65 20 61 6c 72 65 61 64 79 20  use the already 
1d6a6 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
1d6a7 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f   instead of allo
1d6a8 63 61 74 69 6e 67 20 61 20 0a 20 20 2a 2a 20 6e  cating a .  ** n
1d6a9 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73  ew one..  */.  s
1d6aa 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1d6ab 65 28 26 73 43 6f 6e 74 65 78 74 2e 73 2c 20 70  e(&sContext.s, p
1d6ac 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54  Dest);.  MemSetT
1d6ad 79 70 65 46 6c 61 67 28 26 73 43 6f 6e 74 65 78  ypeFlag(&sContex
1d6ae 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a  t.s, MEM_Null);.
1d6af 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1d6b0 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1d6b1 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1d6b2 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 70  misuse;.  rc = p
1d6b3 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 28  Module->xColumn(
1d6b4 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
1d6b5 72 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 4f  r, &sContext, pO
1d6b6 70 2d 3e 70 32 29 3b 0a 20 20 73 71 6c 69 74 65  p->p2);.  sqlite
1d6b7 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1d6b8 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
1d6b9 72 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a  rrMsg = pVtab->z
1d6ba 45 72 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d  ErrMsg;.  pVtab-
1d6bb 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20  >zErrMsg = 0;.. 
1d6bc 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73   /* Copy the res
1d6bd 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ult of the funct
1d6be 69 6f 6e 20 74 6f 20 74 68 65 20 50 33 20 72 65  ion to the P3 re
1d6bf 67 69 73 74 65 72 2e 20 57 65 0a 20 20 2a 2a 20  gister. We.  ** 
1d6c0 64 6f 20 74 68 69 73 20 72 65 67 61 72 64 6c 65  do this regardle
1d6c1 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72  ss of whether or
1d6c2 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f 63   not an error oc
1d6c3 63 75 72 72 65 64 20 74 6f 20 65 6e 73 75 72 65  curred to ensure
1d6c4 20 61 6e 79 0a 20 20 2a 2a 20 64 79 6e 61 6d 69   any.  ** dynami
1d6c5 63 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20  c allocation in 
1d6c6 73 43 6f 6e 74 65 78 74 2e 73 20 28 61 20 4d 65  sContext.s (a Me
1d6c7 6d 20 73 74 72 75 63 74 29 20 69 73 20 20 72 65  m struct) is  re
1d6c8 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73  leased..  */.  s
1d6c9 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1d6ca 45 6e 63 6f 64 69 6e 67 28 26 73 43 6f 6e 74 65  Encoding(&sConte
1d6cb 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  xt.s, encoding);
1d6cc 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1d6cd 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44 65 73 74  E(pOp->p3, pDest
1d6ce 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1d6cf 4d 65 6d 4d 6f 76 65 28 70 44 65 73 74 2c 20 26  MemMove(pDest, &
1d6d0 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a 20 20 55  sContext.s);.  U
1d6d1 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1d6d2 5a 45 28 70 44 65 73 74 29 3b 0a 0a 20 20 69 66  ZE(pDest);..  if
1d6d3 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1d6d4 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 67 6f 74  n(db) ){.    got
1d6d5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1d6d6 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28  isuse;.  }.  if(
1d6d7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
1d6d8 6f 6f 42 69 67 28 70 44 65 73 74 29 20 29 7b 0a  ooBig(pDest) ){.
1d6d9 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
1d6da 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d6db 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1d6dc 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1d6dd 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
1d6de 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1d6df 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
1d6e0 64 65 3a 20 56 4e 65 78 74 20 50 31 20 50 32 20  de: VNext P1 P2 
1d6e1 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61  * * *.**.** Adva
1d6e2 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  nce virtual tabl
1d6e3 65 20 50 31 20 74 6f 20 74 68 65 20 6e 65 78 74  e P1 to the next
1d6e4 20 72 6f 77 20 69 6e 20 69 74 73 20 72 65 73 75   row in its resu
1d6e5 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a 75  lt set and.** ju
1d6e6 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  mp to instructio
1d6e7 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66 20 74 68  n P2.  Or, if th
1d6e8 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
1d6e9 68 61 73 20 72 65 61 63 68 65 64 0a 2a 2a 20 74  has reached.** t
1d6ea 68 65 20 65 6e 64 20 6f 66 20 69 74 73 20 72 65  he end of its re
1d6eb 73 75 6c 74 20 73 65 74 2c 20 74 68 65 6e 20 66  sult set, then f
1d6ec 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
1d6ed 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
1d6ee 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
1d6ef 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6a 75  VNext: {   /* ju
1d6f0 6d 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  mp */.  sqlite3_
1d6f1 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63  vtab *pVtab;.  c
1d6f2 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1d6f3 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20  ule *pModule;.  
1d6f4 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 0a 20 20  int res = 0;..  
1d6f5 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72  VdbeCursor *pCur
1d6f6 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1d6f7 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1d6f8 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
1d6f9 72 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  r );.  if( pCur-
1d6fa 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20  >nullRow ){.    
1d6fb 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 56 74  break;.  }.  pVt
1d6fc 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 74 61 62  ab = pCur->pVtab
1d6fd 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20  Cursor->pVtab;. 
1d6fe 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74 61 62   pModule = pVtab
1d6ff 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73  ->pModule;.  ass
1d700 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e  ert( pModule->xN
1d701 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76  ext );..  /* Inv
1d702 6f 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29 20  oke the xNext() 
1d703 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f  method of the mo
1d704 64 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e  dule. There is n
1d705 6f 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20  o way for the.  
1d706 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d  ** underlying im
1d707 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20  plementation to 
1d708 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1d709 69 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75  if one occurs du
1d70a 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28  ring.  ** xNext(
1d70b 29 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61  ). Instead, if a
1d70c 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
1d70d 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  true is returned
1d70e 20 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61   (indicating tha
1d70f 74 20 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20  t .  ** data is 
1d710 61 76 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74  available) and t
1d711 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65  he error code re
1d712 74 75 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c  turned when xCol
1d713 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65  umn or.  ** some
1d714 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73   other method is
1d715 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e   next invoked on
1d716 20 74 68 65 20 73 61 76 65 20 76 69 72 74 75 61   the save virtua
1d717 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a  l table cursor..
1d718 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
1d719 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1d71a 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1d71b 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 73 71  _to_misuse;.  sq
1d71c 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56  lite3VtabLock(pV
1d71d 74 61 62 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61  tab);.  p->inVta
1d71e 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 72  bMethod = 1;.  r
1d71f 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65  c = pModule->xNe
1d720 78 74 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75  xt(pCur->pVtabCu
1d721 72 73 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74  rsor);.  p->inVt
1d722 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1d723 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1d724 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1d725 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56   p->zErrMsg = pV
1d726 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  tab->zErrMsg;.  
1d727 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1d728 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 74 61   0;.  sqlite3Vta
1d729 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61  bUnlock(db, pVta
1d72a 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  b);.  if( rc==SQ
1d72b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
1d72c 65 73 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 45  es = pModule->xE
1d72d 6f 66 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75  of(pCur->pVtabCu
1d72e 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28  rsor);.  }.  if(
1d72f 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
1d730 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1d731 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1d732 0a 0a 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a  ..  if( !res ){.
1d733 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
1d734 69 73 20 64 61 74 61 2c 20 6a 75 6d 70 20 74 6f  is data, jump to
1d735 20 50 32 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20   P2 */.    pc = 
1d736 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1d737 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1d738 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1d739 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1d73a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1d73b 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1d73c 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1d73d 52 65 6e 61 6d 65 20 50 31 20 2a 20 2a 20 50 34  Rename P1 * * P4
1d73e 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61   *.**.** P4 is a
1d73f 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69   pointer to a vi
1d740 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65  rtual table obje
1d741 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76  ct, an sqlite3_v
1d742 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  tab structure..*
1d743 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e  * This opcode in
1d744 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73  vokes the corres
1d745 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d 65 20  ponding xRename 
1d746 6d 65 74 68 6f 64 2e 20 54 68 65 20 76 61 6c 75  method. The valu
1d747 65 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72  e.** in register
1d748 20 50 31 20 69 73 20 70 61 73 73 65 64 20 61 73   P1 is passed as
1d749 20 74 68 65 20 7a 4e 61 6d 65 20 61 72 67 75 6d   the zName argum
1d74a 65 6e 74 20 74 6f 20 74 68 65 20 78 52 65 6e 61  ent to the xRena
1d74b 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61  me method..*/.ca
1d74c 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65 3a 20 7b  se OP_VRename: {
1d74d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
1d74e 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34  *pVtab = pOp->p4
1d74f 2e 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a 70  .pVtab;.  Mem *p
1d750 4e 61 6d 65 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  Name = &p->aMem[
1d751 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1d752 72 74 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75  rt( pVtab->pModu
1d753 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20  le->xRename );. 
1d754 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1d755 70 4f 70 2d 3e 70 31 2c 20 70 4e 61 6d 65 29 3b  pOp->p1, pName);
1d756 0a 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 4e  ..  Stringify(pN
1d757 61 6d 65 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  ame, encoding);.
1d758 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1d759 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1d75a 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1d75b 6d 69 73 75 73 65 3b 0a 20 20 73 71 6c 69 74 65  misuse;.  sqlite
1d75c 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29  3VtabLock(pVtab)
1d75d 3b 0a 20 20 72 63 20 3d 20 70 56 74 61 62 2d 3e  ;.  rc = pVtab->
1d75e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65  pModule->xRename
1d75f 28 70 56 74 61 62 2c 20 70 4e 61 6d 65 2d 3e 7a  (pVtab, pName->z
1d760 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1d761 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1d762 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67  g);.  p->zErrMsg
1d763 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73   = pVtab->zErrMs
1d764 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72  g;.  pVtab->zErr
1d765 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  Msg = 0;.  sqlit
1d766 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c  e3VtabUnlock(db,
1d767 20 70 56 74 61 62 29 3b 0a 20 20 69 66 28 20 73   pVtab);.  if( s
1d768 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1d769 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1d76a 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a  due_to_misuse;..
1d76b 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1d76c 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
1d76d 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1d76e 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1d76f 55 70 64 61 74 65 20 50 31 20 50 32 20 50 33 20  Update P1 P2 P3 
1d770 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  P4 *.**.** P4 is
1d771 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1d772 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62  virtual table ob
1d773 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33  ject, an sqlite3
1d774 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e  _vtab structure.
1d775 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1d776 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72  invokes the corr
1d777 65 73 70 6f 6e 64 69 6e 67 20 78 55 70 64 61 74  esponding xUpdat
1d778 65 20 6d 65 74 68 6f 64 2e 20 50 32 20 76 61 6c  e method. P2 val
1d779 75 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69  ues.** are conti
1d77a 67 75 6f 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c  guous memory cel
1d77b 6c 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 50  ls starting at P
1d77c 33 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65  3 to pass to the
1d77d 20 78 55 70 64 61 74 65 20 0a 2a 2a 20 69 6e 76   xUpdate .** inv
1d77e 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c  ocation. The val
1d77f 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 28  ue in register (
1d780 50 33 2b 50 32 2d 31 29 20 63 6f 72 72 65 73 70  P3+P2-1) corresp
1d781 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20  onds to the .** 
1d782 70 32 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20  p2th element of 
1d783 74 68 65 20 61 72 67 76 20 61 72 72 61 79 20 70  the argv array p
1d784 61 73 73 65 64 20 74 6f 20 78 55 70 64 61 74 65  assed to xUpdate
1d785 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64  ..**.** The xUpd
1d786 61 74 65 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20  ate method will 
1d787 64 6f 20 61 20 44 45 4c 45 54 45 20 6f 72 20 61  do a DELETE or a
1d788 6e 20 49 4e 53 45 52 54 20 6f 72 20 62 6f 74 68  n INSERT or both
1d789 2e 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b 30 5d  ..** The argv[0]
1d78a 20 65 6c 65 6d 65 6e 74 20 28 77 68 69 63 68 20   element (which 
1d78b 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 6d  corresponds to m
1d78c 65 6d 6f 72 79 20 63 65 6c 6c 20 50 33 29 0a 2a  emory cell P3).*
1d78d 2a 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f  * is the rowid o
1d78e 66 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74  f a row to delet
1d78f 65 2e 20 20 49 66 20 61 72 67 76 5b 30 5d 20 69  e.  If argv[0] i
1d790 73 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a  s NULL then no .
1d791 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75  ** deletion occu
1d792 72 73 2e 20 20 54 68 65 20 61 72 67 76 5b 31 5d  rs.  The argv[1]
1d793 20 65 6c 65 6d 65 6e 74 20 69 73 20 74 68 65 20   element is the 
1d794 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77  rowid of the new
1d795 20 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 69 73 20   .** row.  This 
1d796 63 61 6e 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68  can be NULL to h
1d797 61 76 65 20 74 68 65 20 76 69 72 74 75 61 6c 20  ave the virtual 
1d798 74 61 62 6c 65 20 73 65 6c 65 63 74 20 74 68 65  table select the
1d799 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 64 20 66   new .** rowid f
1d79a 6f 72 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20  or itself.  The 
1d79b 73 75 62 73 65 71 75 65 6e 74 20 65 6c 65 6d 65  subsequent eleme
1d79c 6e 74 73 20 69 6e 20 74 68 65 20 61 72 72 61 79  nts in the array
1d79d 20 61 72 65 20 0a 2a 2a 20 74 68 65 20 76 61 6c   are .** the val
1d79e 75 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ues of columns i
1d79f 6e 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a  n the new row..*
1d7a0 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 20 74 68  *.** If P2==1 th
1d7a1 65 6e 20 6e 6f 20 69 6e 73 65 72 74 20 69 73 20  en no insert is 
1d7a2 70 65 72 66 6f 72 6d 65 64 2e 20 20 61 72 67 76  performed.  argv
1d7a3 5b 30 5d 20 69 73 20 74 68 65 20 72 6f 77 69 64  [0] is the rowid
1d7a4 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74 6f 20   of.** a row to 
1d7a5 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31  delete..**.** P1
1d7a6 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c   is a boolean fl
1d7a7 61 67 2e 20 49 66 20 69 74 20 69 73 20 73 65 74  ag. If it is set
1d7a8 20 74 6f 20 74 72 75 65 20 61 6e 64 20 74 68 65   to true and the
1d7a9 20 78 55 70 64 61 74 65 20 63 61 6c 6c 0a 2a 2a   xUpdate call.**
1d7aa 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
1d7ab 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72  then the value r
1d7ac 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
1d7ad 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
1d7ae 6f 77 69 64 28 29 20 0a 2a 2a 20 69 73 20 73 65  owid() .** is se
1d7af 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f  t to the value o
1d7b0 66 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20  f the rowid for 
1d7b1 74 68 65 20 72 6f 77 20 6a 75 73 74 20 69 6e 73  the row just ins
1d7b2 65 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f  erted..*/.case O
1d7b3 50 5f 56 55 70 64 61 74 65 3a 20 7b 0a 20 20 73  P_VUpdate: {.  s
1d7b4 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
1d7b5 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74  ab = pOp->p4.pVt
1d7b6 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f  ab;.  sqlite3_mo
1d7b7 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20  dule *pModule = 
1d7b8 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20  (sqlite3_module 
1d7b9 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  *)pVtab->pModule
1d7ba 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20 3d 20 70  ;.  int nArg = p
1d7bb 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74  Op->p2;.  assert
1d7bc 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1d7bd 34 5f 56 54 41 42 20 29 3b 0a 20 20 69 66 28 20  4_VTAB );.  if( 
1d7be 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65  pModule->xUpdate
1d7bf 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1d7c0 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1d7c1 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 72 65  zErrMsg, db, "re
1d7c2 61 64 2d 6f 6e 6c 79 20 74 61 62 6c 65 22 29 3b  ad-only table");
1d7c3 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1d7c4 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b  _ERROR;.  }else{
1d7c5 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1d7c6 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77  sqlite_int64 row
1d7c7 69 64 3b 0a 20 20 20 20 4d 65 6d 20 2a 2a 61 70  id;.    Mem **ap
1d7c8 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a  Arg = p->apArg;.
1d7c9 20 20 20 20 4d 65 6d 20 2a 70 58 20 3d 20 26 70      Mem *pX = &p
1d7ca 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1d7cb 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1d7cc 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nArg; i++){.    
1d7cd 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28    storeTypeInfo(
1d7ce 70 58 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 70  pX, 0);.      ap
1d7cf 41 72 67 5b 69 5d 20 3d 20 70 58 3b 0a 20 20 20  Arg[i] = pX;.   
1d7d0 20 20 20 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a 20     pX++;.    }. 
1d7d1 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1d7d2 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1d7d3 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1d7d4 6d 69 73 75 73 65 3b 0a 20 20 20 20 73 71 6c 69  misuse;.    sqli
1d7d5 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56 74 61  te3VtabLock(pVta
1d7d6 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f  b);.    rc = pMo
1d7d7 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 28 70 56  dule->xUpdate(pV
1d7d8 74 61 62 2c 20 6e 41 72 67 2c 20 61 70 41 72 67  tab, nArg, apArg
1d7d9 2c 20 26 72 6f 77 69 64 29 3b 0a 20 20 20 20 73  , &rowid);.    s
1d7da 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1d7db 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1d7dc 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70    p->zErrMsg = p
1d7dd 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20  Vtab->zErrMsg;. 
1d7de 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73     pVtab->zErrMs
1d7df 67 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  g = 0;.    sqlit
1d7e0 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c  e3VtabUnlock(db,
1d7e1 20 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28   pVtab);.    if(
1d7e2 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
1d7e3 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1d7e4 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1d7e5 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 31  .    if( pOp->p1
1d7e6 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
1d7e7 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  K ){.      asser
1d7e8 74 28 20 6e 41 72 67 3e 31 20 26 26 20 61 70 41  t( nArg>1 && apA
1d7e9 72 67 5b 30 5d 20 26 26 20 28 61 70 41 72 67 5b  rg[0] && (apArg[
1d7ea 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  0]->flags&MEM_Nu
1d7eb 6c 6c 29 20 29 3b 0a 20 20 20 20 20 20 64 62 2d  ll) );.      db-
1d7ec 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 72 6f 77  >lastRowid = row
1d7ed 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  id;.    }.    p-
1d7ee 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a  >nChange++;.  }.
1d7ef 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1d7f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1d7f1 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1d7f2 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 4c 49 54  ..#ifndef  SQLIT
1d7f3 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
1d7f4 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  GMAS./* Opcode: 
1d7f5 50 61 67 65 63 6f 75 6e 74 20 50 31 20 50 32 20  Pagecount P1 P2 
1d7f6 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  * * *.**.** Writ
1d7f7 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75  e the current nu
1d7f8 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
1d7f9 20 64 61 74 61 62 61 73 65 20 50 31 20 74 6f 20   database P1 to 
1d7fa 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32 2e 0a  memory cell P2..
1d7fb 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 67 65 63  */.case OP_Pagec
1d7fc 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20  ount: {         
1d7fd 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1d7fe 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 70  lease */.  int p
1d7ff 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20  1 = pOp->p1; .  
1d800 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 50 61 67  int nPage;.  Pag
1d801 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c  er *pPager = sql
1d802 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 64  ite3BtreePager(d
1d803 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 29 3b  b->aDb[p1].pBt);
1d804 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
1d805 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
1d806 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a  Pager, &nPage);.
1d807 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1d808 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d  _OK ){.    pOut-
1d809 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1d80a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20  ;.    pOut->u.i 
1d80b 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 62  = nPage;.  }.  b
1d80c 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  reak;.}.#endif..
1d80d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1d80e 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63  MIT_TRACE./* Opc
1d80f 6f 64 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a  ode: Trace * * *
1d810 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74   P4 *.**.** If t
1d811 72 61 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65  racing is enable
1d812 64 20 28 62 79 20 74 68 65 20 73 71 6c 69 74 65  d (by the sqlite
1d813 33 5f 74 72 61 63 65 28 29 29 20 69 6e 74 65 72  3_trace()) inter
1d814 66 61 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  face, then.** th
1d815 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63  e UTF-8 string c
1d816 6f 6e 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69  ontained in P4 i
1d817 73 20 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65  s emitted on the
1d818 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e   trace callback.
1d819 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63  .*/.case OP_Trac
1d81a 65 3a 20 7b 0a 20 20 63 68 61 72 20 2a 7a 54 72  e: {.  char *zTr
1d81b 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 34 2e 7a  ace = (pOp->p4.z
1d81c 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a 20 70   ? pOp->p4.z : p
1d81d 2d 3e 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 7a  ->zSql);.  if( z
1d81e 54 72 61 63 65 20 29 7b 0a 20 20 20 20 69 66 28  Trace ){.    if(
1d81f 20 64 62 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20   db->xTrace ){. 
1d820 20 20 20 20 20 64 62 2d 3e 78 54 72 61 63 65 28       db->xTrace(
1d821 64 62 2d 3e 70 54 72 61 63 65 41 72 67 2c 20 7a  db->pTraceArg, z
1d822 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 69  Trace);.    }.#i
1d823 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1d824 47 0a 20 20 20 20 69 66 28 20 28 64 62 2d 3e 66  G.    if( (db->f
1d825 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 71  lags & SQLITE_Sq
1d826 6c 54 72 61 63 65 29 21 3d 30 20 29 7b 0a 20 20  lTrace)!=0 ){.  
1d827 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
1d828 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 72 61 63  Printf("SQL-trac
1d829 65 3a 20 25 73 5c 6e 22 2c 20 7a 54 72 61 63 65  e: %s\n", zTrace
1d82a 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20  );.    }.#endif 
1d82b 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
1d82c 2a 2f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  */.  }.  break;.
1d82d 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4f 70  }.#endif.../* Op
1d82e 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a  code: Noop * * *
1d82f 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f   * *.**.** Do no
1d830 74 68 69 6e 67 2e 20 20 54 68 69 73 20 69 6e 73  thing.  This ins
1d831 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 66 74 65  truction is ofte
1d832 6e 20 75 73 65 66 75 6c 20 61 73 20 61 20 6a 75  n useful as a ju
1d833 6d 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f  mp.** destinatio
1d834 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20  n..*/./*.** The 
1d835 6d 61 67 69 63 20 45 78 70 6c 61 69 6e 20 6f 70  magic Explain op
1d836 63 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e  code are only in
1d837 73 65 72 74 65 64 20 77 68 65 6e 20 65 78 70 6c  serted when expl
1d838 61 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a  ain==2 (which.**
1d839 20 69 73 20 74 6f 20 73 61 79 20 77 68 65 6e 20   is to say when 
1d83a 74 68 65 20 45 58 50 4c 41 49 4e 20 51 55 45 52  the EXPLAIN QUER
1d83b 59 20 50 4c 41 4e 20 73 79 6e 74 61 78 20 69 73  Y PLAN syntax is
1d83c 20 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20   used.).** This 
1d83d 6f 70 63 6f 64 65 20 72 65 63 6f 72 64 73 20 69  opcode records i
1d83e 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
1d83f 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20  the optimizer.  
1d840 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 65  It is the.** the
1d841 20 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70   same as a no-op
1d842 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 73 6e  .  This opcodesn
1d843 65 76 65 72 20 61 70 70 65 61 72 73 20 69 6e 20  ever appears in 
1d844 61 20 72 65 61 6c 20 56 4d 20 70 72 6f 67 72 61  a real VM progra
1d845 6d 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a 20 7b  m..*/.default: {
1d846 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69            /* Thi
1d847 73 20 69 73 20 72 65 61 6c 6c 79 20 4f 50 5f 4e  s is really OP_N
1d848 6f 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70 6c 61  oop and OP_Expla
1d849 69 6e 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d  in */.  break;.}
1d84a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1d84b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d84c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d84d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d84e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d84f 0a 2a 2a 20 54 68 65 20 63 61 73 65 73 20 6f 66  .** The cases of
1d850 20 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74   the switch stat
1d851 65 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 69 73  ement above this
1d852 20 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c   line should all
1d853 20 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20   be indented.** 
1d854 62 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75  by 6 spaces.  Bu
1d855 74 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  t the left-most 
1d856 36 20 73 70 61 63 65 73 20 68 61 76 65 20 62 65  6 spaces have be
1d857 65 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d  en removed to im
1d858 70 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 65 61  prove the.** rea
1d859 64 61 62 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20  dability.  From 
1d85a 74 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f  this point on do
1d85b 77 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69  wn, the normal i
1d85c 6e 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73  ndentation rules
1d85d 20 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64   are.** restored
1d85e 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**************
1d85f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d862 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1d863 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 56  .    }..#ifdef V
1d864 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20  DBE_PROFILE.    
1d865 7b 0a 20 20 20 20 20 20 75 36 34 20 65 6c 61 70  {.      u64 elap
1d866 73 65 64 20 3d 20 73 71 6c 69 74 65 33 48 77 74  sed = sqlite3Hwt
1d867 69 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a 20  ime() - start;. 
1d868 20 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73       pOp->cycles
1d869 20 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20 20 20   += elapsed;.   
1d86a 20 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23     pOp->cnt++;.#
1d86b 69 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72  if 0.        fpr
1d86c 69 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31  intf(stdout, "%1
1d86d 30 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65 64 29  0llu ", elapsed)
1d86e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1d86f 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64  3VdbePrintOp(std
1d870 6f 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d  out, origPc, &p-
1d871 3e 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23  >aOp[origPc]);.#
1d872 65 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64  endif.    }.#end
1d873 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66  if..    /* The f
1d874 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 64  ollowing code ad
1d875 64 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68  ds nothing to th
1d876 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f  e actual functio
1d877 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66  nality.    ** of
1d878 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 49   the program.  I
1d879 74 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 66  t is only here f
1d87a 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  or testing and d
1d87b 65 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a  ebugging..    **
1d87c 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61   On the other ha
1d87d 6e 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 6e  nd, it does burn
1d87e 20 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 72   CPU cycles ever
1d87f 79 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a 20  y time through. 
1d880 20 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 61     ** the evalua
1d881 74 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65  tor loop.  So we
1d882 20 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f 75   can leave it ou
1d883 74 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 73  t when NDEBUG is
1d884 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f   defined..    */
1d885 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
1d886 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d      assert( pc>=
1d887 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20  -1 && pc<p->nOp 
1d888 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
1d889 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20  E_DEBUG.    if( 
1d88a 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20  p->trace ){.    
1d88b 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 66 70    if( rc!=0 ) fp
1d88c 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 22  rintf(p->trace,"
1d88d 72 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20  rc=%d\n",rc);.  
1d88e 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72      if( opProper
1d88f 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f  ty & OPFLG_OUT2_
1d890 50 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 20 20  PRERELEASE ){.  
1d891 20 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72        registerTr
1d892 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f  ace(p->trace, pO
1d893 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p2, pOut);.  
1d894 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1d895 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1d896 4c 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20  LG_OUT3 ){.     
1d897 20 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65     registerTrace
1d898 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e  (p->trace, pOp->
1d899 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20  p3, pOut);.     
1d89a 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20   }.    }.#endif 
1d89b 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47   /* SQLITE_DEBUG
1d89c 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e   */.#endif  /* N
1d89d 44 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a  DEBUG */.  }  /*
1d89e 20 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   The end of the 
1d89f 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65  for(;;) loop the
1d8a0 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 6f   loops through o
1d8a1 70 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20  pcodes */..  /* 
1d8a2 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73  If we reach this
1d8a3 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73   point, it means
1d8a4 20 74 68 61 74 20 65 78 65 63 75 74 69 6f 6e 20   that execution 
1d8a5 69 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68  is finished with
1d8a6 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f  .  ** an error o
1d8a7 66 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a  f some kind..  *
1d8a8 2f 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c  /.vdbe_error_hal
1d8a9 74 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 20  t:.  assert( rc 
1d8aa 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b  );.  p->rc = rc;
1d8ab 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61  .  sqlite3VdbeHa
1d8ac 6c 74 28 70 29 3b 0a 20 20 69 66 28 20 72 63 3d  lt(p);.  if( rc=
1d8ad 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
1d8ae 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63  MEM ) db->malloc
1d8af 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63  Failed = 1;.  rc
1d8b0 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
1d8b1 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74  ..  /* This is t
1d8b2 68 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20  he only way out 
1d8b3 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  of this procedur
1d8b4 65 2e 20 20 57 65 20 68 61 76 65 20 74 6f 0a 20  e.  We have to. 
1d8b5 20 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20   ** release the 
1d8b6 6d 75 74 65 78 65 73 20 6f 6e 20 62 74 72 65 65  mutexes on btree
1d8b7 73 20 74 68 61 74 20 77 65 72 65 20 61 63 71 75  s that were acqu
1d8b8 69 72 65 64 20 61 74 20 74 68 65 0a 20 20 2a 2a  ired at the.  **
1d8b9 20 74 6f 70 2e 20 2a 2f 0a 76 64 62 65 5f 72 65   top. */.vdbe_re
1d8ba 74 75 72 6e 3a 0a 20 20 73 71 6c 69 74 65 33 42  turn:.  sqlite3B
1d8bb 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
1d8bc 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b  ave(&p->aMutex);
1d8bd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20  .  return rc;.. 
1d8be 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65   /* Jump to here
1d8bf 20 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20   if a string or 
1d8c0 62 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e  blob larger than
1d8c1 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
1d8c2 54 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75  TH.  ** is encou
1d8c3 6e 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f  ntered..  */.too
1d8c4 5f 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 33 53  _big:.  sqlite3S
1d8c5 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1d8c6 72 4d 73 67 2c 20 64 62 2c 20 22 73 74 72 69 6e  rMsg, db, "strin
1d8c7 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69  g or blob too bi
1d8c8 67 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49  g");.  rc = SQLI
1d8c9 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74  TE_TOOBIG;.  got
1d8ca 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c  o vdbe_error_hal
1d8cb 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  t;..  /* Jump to
1d8cc 20 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f   here if a mallo
1d8cd 63 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a  c() fails..  */.
1d8ce 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61  no_mem:.  db->ma
1d8cf 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
1d8d0 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d8d1 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1d8d2 64 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  db, "out of memo
1d8d3 72 79 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c  ry");.  rc = SQL
1d8d4 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74  ITE_NOMEM;.  got
1d8d5 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c  o vdbe_error_hal
1d8d6 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  t;..  /* Jump to
1d8d7 20 68 65 72 65 20 66 6f 72 20 61 6e 20 53 51 4c   here for an SQL
1d8d8 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72  ITE_MISUSE error
1d8d9 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65  ..  */.abort_due
1d8da 5f 74 6f 5f 6d 69 73 75 73 65 3a 0a 20 20 72 63  _to_misuse:.  rc
1d8db 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45   = SQLITE_MISUSE
1d8dc 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75  ;.  /* Fall thru
1d8dd 20 69 6e 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   into abort_due_
1d8de 74 6f 5f 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 2f  to_error */..  /
1d8df 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66  * Jump to here f
1d8e0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e  or any other kin
1d8e1 64 20 6f 66 20 66 61 74 61 6c 20 65 72 72 6f 72  d of fatal error
1d8e2 2e 20 20 54 68 65 20 22 72 63 22 20 76 61 72 69  .  The "rc" vari
1d8e3 61 62 6c 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  able.  ** should
1d8e4 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20   hold the error 
1d8e5 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 61 62 6f  number..  */.abo
1d8e6 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3a  rt_due_to_error:
1d8e7 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 45  .  assert( p->zE
1d8e8 72 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20 69 66  rrMsg==0 );.  if
1d8e9 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
1d8ea 65 64 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45  ed ) rc = SQLITE
1d8eb 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63  _NOMEM;.  if( rc
1d8ec 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  !=SQLITE_IOERR_N
1d8ed 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69  OMEM ){.    sqli
1d8ee 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1d8ef 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
1d8f0 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  s", sqlite3ErrSt
1d8f1 72 28 72 63 29 29 3b 0a 20 20 7d 0a 20 20 67 6f  r(rc));.  }.  go
1d8f2 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
1d8f3 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74  lt;..  /* Jump t
1d8f4 6f 20 68 65 72 65 20 69 66 20 74 68 65 20 73 71  o here if the sq
1d8f5 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
1d8f6 29 20 41 50 49 20 73 65 74 73 20 74 68 65 20 69  ) API sets the i
1d8f7 6e 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c  nterrupt.  ** fl
1d8f8 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64  ag..  */.abort_d
1d8f9 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a  ue_to_interrupt:
1d8fa 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 75  .  assert( db->u
1d8fb 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20  1.isInterrupted 
1d8fc 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  );.  rc = SQLITE
1d8fd 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 2d  _INTERRUPT;.  p-
1d8fe 3e 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69  >rc = rc;.  sqli
1d8ff 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1d900 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
1d901 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74  s", sqlite3ErrSt
1d902 72 28 72 63 29 29 3b 0a 20 20 67 6f 74 6f 20 76  r(rc));.  goto v
1d903 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a  dbe_error_halt;.
1d904 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
1d905 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 63  ** End of vdbe.c
1d906 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1d907 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d908 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d909 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1d90a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
1d90b 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a  beblob.c *******
1d90c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d90d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d90e 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61  */./*.** 2007 Ma
1d90f 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  y 1.**.** The au
1d910 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1d911 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1d912 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1d913 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1d914 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1d915 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1d916 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1d917 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1d918 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
1d919 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
1d91a 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
1d91b 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
1d91c 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
1d91d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
1d91e 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
1d91f 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1d920 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1d921 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d924 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1d925 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
1d926 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
1d927 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
1d928 6e 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42  nt incremental B
1d929 4c 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24  LOB I/O..**.** $
1d92a 49 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76  Id: vdbeblob.c,v
1d92b 20 31 2e 33 31 20 32 30 30 39 2f 30 33 2f 32 34   1.31 2009/03/24
1d92c 20 31 35 3a 30 38 3a 31 30 20 64 72 68 20 45 78   15:08:10 drh Ex
1d92d 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66  p $.*/...#ifndef
1d92e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
1d92f 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c  RBLOB../*.** Val
1d930 69 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a  id sqlite3_blob*
1d931 20 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74   handles point t
1d932 6f 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 63  o Incrblob struc
1d933 74 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  tures..*/.typede
1d934 66 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f  f struct Incrblo
1d935 62 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75  b Incrblob;.stru
1d936 63 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20  ct Incrblob {.  
1d937 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
1d938 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
1d939 6f 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 65  of "flags" passe
1d93a 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f  d to sqlite3_blo
1d93b 62 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  b_open() */.  in
1d93c 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
1d93d 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1d93e 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62   open blob, in b
1d93f 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f  ytes */.  int iO
1d940 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
1d941 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74    /* Byte offset
1d942 20 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 73   of blob in curs
1d943 6f 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 43  or data */.  BtC
1d944 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20  ursor *pCsr;    
1d945 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70       /* Cursor p
1d946 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20  ointing at blob 
1d947 72 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  row */.  sqlite3
1d948 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20  _stmt *pStmt;   
1d949 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f   /* Statement ho
1d94a 6c 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65  lding cursor ope
1d94b 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  n */.  sqlite3 *
1d94c 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  db;            /
1d94d 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64  * The associated
1d94e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a   database */.};.
1d94f 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c  ./*.** Open a bl
1d950 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  ob handle..*/.SQ
1d951 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1d952 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a  ite3_blob_open(.
1d953 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20    sqlite3* db,  
1d954 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1d955 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1d956 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
1d957 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20  char *zDb,      
1d958 20 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65    /* The attache
1d959 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  d database conta
1d95a 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a  ining the blob *
1d95b 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1d95c 7a 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54  zTable,     /* T
1d95d 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
1d95e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a  ing the blob */.
1d95f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
1d960 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65  olumn,    /* The
1d961 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69   column containi
1d962 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20  ng the blob */. 
1d963 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52   sqlite_int64 iR
1d964 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ow,      /* The 
1d965 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  row containing t
1d966 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74  he glob */.  int
1d967 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
1d968 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20       /* True -> 
1d969 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73  read/write acces
1d96a 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 64  s, false -> read
1d96b 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74  -only */.  sqlit
1d96c 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62  e3_blob **ppBlob
1d96d 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72     /* Handle for
1d96e 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 62   accessing the b
1d96f 6c 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 72  lob returned her
1d970 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41  e */.){.  int nA
1d971 74 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e  ttempt = 0;.  in
1d972 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t iCol;         
1d973 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1d974 66 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77  f zColumn in row
1d975 2d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a  -record */..  /*
1d976 20 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 72   This VDBE progr
1d977 61 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 65  am seeks a btree
1d978 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69   cursor to the i
1d979 64 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a 20  dentified .  ** 
1d97a 64 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74  db/table/row ent
1d97b 72 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66  ry. The reason f
1d97c 6f 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 20  or using a vdbe 
1d97d 70 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 0a  program instead.
1d97e 20 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 20    ** of writing 
1d97f 63 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 20  code to use the 
1d980 62 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 72  b-tree layer dir
1d981 65 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 68  ectly is that th
1d982 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67  e.  ** vdbe prog
1d983 72 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 64  ram will take ad
1d984 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 76  vantage of the v
1d985 61 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 69  arious transacti
1d986 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67  on,.  ** locking
1d987 20 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 6c   and error handl
1d988 69 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 75  ing infrastructu
1d989 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  re built into th
1d98a 65 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a  e vdbe..  **.  *
1d98b 2a 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20  * After seeking 
1d98c 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20  the cursor, the 
1d98d 76 64 62 65 20 65 78 65 63 75 74 65 73 20 61 6e  vdbe executes an
1d98e 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20   OP_ResultRow.. 
1d98f 20 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61   ** Code externa
1d990 6c 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68  l to the Vdbe th
1d991 65 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65  en "borrows" the
1d992 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61   b-tree cursor a
1d993 6e 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20  nd.  ** uses it 
1d994 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1d995 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c   blob_read(), bl
1d996 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0a  ob_write() and .
1d997 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28    ** blob_bytes(
1d998 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  ) functions..  *
1d999 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74  *.  ** The sqlit
1d99a 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20  e3_blob_close() 
1d99b 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a  function finaliz
1d99c 65 73 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  es the vdbe prog
1d99d 72 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20  ram,.  ** which 
1d99e 63 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 65  closes the b-tre
1d99f 65 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 6f  e cursor and (po
1d9a0 73 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20  ssibly) commits 
1d9a1 74 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61  the .  ** transa
1d9a2 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74  ction..  */.  st
1d9a3 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
1d9a4 70 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d  pList openBlob[]
1d9a5 20 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61   = {.    {OP_Tra
1d9a6 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20  nsaction, 0, 0, 
1d9a7 30 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 74  0},     /* 0: St
1d9a8 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  art a transactio
1d9a9 6e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72  n */.    {OP_Ver
1d9aa 69 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c  ifyCookie, 0, 0,
1d9ab 20 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68   0},    /* 1: Ch
1d9ac 65 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 63  eck the schema c
1d9ad 6f 6f 6b 69 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a  ookie */..    /*
1d9ae 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   One of the foll
1d9af 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74 72 75  owing two instru
1d9b0 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c 61 63  ctions is replac
1d9b1 65 64 20 62 79 20 61 6e 0a 20 20 20 20 2a 2a 20  ed by an.    ** 
1d9b2 4f 50 5f 4e 6f 6f 70 20 62 65 66 6f 72 65 20 65  OP_Noop before e
1d9b3 78 65 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  xection..    */.
1d9b4 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64      {OP_OpenRead
1d9b5 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20  , 0, 0, 0},     
1d9b6 20 20 20 2f 2a 20 32 3a 20 4f 70 65 6e 20 63 75     /* 2: Open cu
1d9b7 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 69  rsor 0 for readi
1d9b8 6e 67 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70  ng */.    {OP_Op
1d9b9 65 6e 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30  enWrite, 0, 0, 0
1d9ba 7d 2c 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 4f  },       /* 3: O
1d9bb 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72  pen cursor 0 for
1d9bc 20 72 65 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a   read/write */..
1d9bd 20 20 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65      {OP_Variable
1d9be 2c 20 31 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20  , 1, 1, 1},     
1d9bf 20 20 20 2f 2a 20 34 3a 20 50 75 73 68 20 74 68     /* 4: Push th
1d9c0 65 20 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73  e rowid to the s
1d9c1 74 61 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  tack */.    {OP_
1d9c2 4e 6f 74 45 78 69 73 74 73 2c 20 30 2c 20 38 2c  NotExists, 0, 8,
1d9c3 20 31 7d 2c 20 20 20 20 20 20 20 2f 2a 20 35 3a   1},       /* 5:
1d9c4 20 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72   Seek the cursor
1d9c5 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75   */.    {OP_Colu
1d9c6 6d 6e 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20  mn, 0, 0, 1},   
1d9c7 20 20 20 20 20 20 20 2f 2a 20 36 20 20 2a 2f 0a         /* 6  */.
1d9c8 20 20 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f      {OP_ResultRo
1d9c9 77 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20  w, 1, 0, 0},    
1d9ca 20 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 20 20 20     /* 7  */.    
1d9cb 7b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c  {OP_Close, 0, 0,
1d9cc 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f   0},           /
1d9cd 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  * 8  */.    {OP_
1d9ce 48 61 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  Halt, 0, 0, 0}, 
1d9cf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 39 20             /* 9 
1d9d0 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 65 20  */.  };..  Vdbe 
1d9d1 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  *v = 0;.  int rc
1d9d2 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1d9d3 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a  char zErr[128];.
1d9d4 0a 20 20 7a 45 72 72 5b 30 5d 20 3d 20 30 3b 0a  .  zErr[0] = 0;.
1d9d5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1d9d6 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
1d9d7 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 50 61 72  ;.  do {.    Par
1d9d8 73 65 20 73 50 61 72 73 65 3b 0a 20 20 20 20 54  se sParse;.    T
1d9d9 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20 20 20  able *pTab;..   
1d9da 20 6d 65 6d 73 65 74 28 26 73 50 61 72 73 65 2c   memset(&sParse,
1d9db 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65   0, sizeof(Parse
1d9dc 29 29 3b 0a 20 20 20 20 73 50 61 72 73 65 2e 64  ));.    sParse.d
1d9dd 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66 28  b = db;..    if(
1d9de 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
1d9df 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71  (db) ){.      sq
1d9e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1d9e1 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1d9e2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1d9e3 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 7d 0a  E_MISUSE;.    }.
1d9e4 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1d9e5 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20  eEnterAll(db);. 
1d9e6 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
1d9e7 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 26 73 50  3LocateTable(&sP
1d9e8 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c 65 2c  arse, 0, zTable,
1d9e9 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
1d9ea 54 61 62 20 26 26 20 49 73 56 69 72 74 75 61 6c  Tab && IsVirtual
1d9eb 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
1d9ec 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20  pTab = 0;.      
1d9ed 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1d9ee 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  &sParse, "cannot
1d9ef 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 74 61   open virtual ta
1d9f0 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65  ble: %s", zTable
1d9f1 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  );.    }.#ifndef
1d9f2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
1d9f3 57 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26  W.    if( pTab &
1d9f4 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  & pTab->pSelect 
1d9f5 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  ){.      pTab = 
1d9f6 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
1d9f7 45 72 72 6f 72 4d 73 67 28 26 73 50 61 72 73 65  ErrorMsg(&sParse
1d9f8 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76  , "cannot open v
1d9f9 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65  iew: %s", zTable
1d9fa 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
1d9fb 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 7b      if( !pTab ){
1d9fc 0a 20 20 20 20 20 20 69 66 28 20 73 50 61 72 73  .      if( sPars
1d9fd 65 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20  e.zErrMsg ){.   
1d9fe 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1d9ff 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72  rintf(sizeof(zEr
1da00 72 29 2c 20 7a 45 72 72 2c 20 22 25 73 22 2c 20  r), zErr, "%s", 
1da01 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 29 3b  sParse.zErrMsg);
1da02 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1da03 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1da04 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67 29   sParse.zErrMsg)
1da05 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
1da06 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
1da07 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1da08 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
1da09 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
1da0a 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20  eaveAll(db);.   
1da0b 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65     goto blob_ope
1da0c 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  n_out;.    }..  
1da0d 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63 68 20    /* Now search 
1da0e 70 54 61 62 20 66 6f 72 20 74 68 65 20 65 78 61  pTab for the exa
1da0f 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20  ct column. */.  
1da10 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43    for(iCol=0; iC
1da11 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b  ol < pTab->nCol;
1da12 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20 20 20   iCol++) {.     
1da13 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1da14 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  Cmp(pTab->aCol[i
1da15 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  Col].zName, zCol
1da16 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  umn)==0 ){.     
1da17 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1da18 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
1da19 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c  iCol==pTab->nCol
1da1a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1da1b 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1da1c 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22  f(zErr), zErr, "
1da1d 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20  no such column: 
1da1e 5c 22 25 73 5c 22 22 2c 20 7a 43 6f 6c 75 6d 6e  \"%s\"", zColumn
1da1f 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
1da20 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1da21 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1da22 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
1da23 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1da24 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20  LeaveAll(db);.  
1da25 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1da26 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20  en_out;.    }.. 
1da27 20 20 20 2f 2a 20 49 66 20 74 68 65 20 76 61 6c     /* If the val
1da28 75 65 20 69 73 20 62 65 69 6e 67 20 6f 70 65 6e  ue is being open
1da29 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 20  ed for writing, 
1da2a 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20  check that the. 
1da2b 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20     ** column is 
1da2c 6e 6f 74 20 69 6e 64 65 78 65 64 2e 20 49 74 20  not indexed. It 
1da2d 69 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 72  is against the r
1da2e 75 6c 65 73 20 74 6f 20 6f 70 65 6e 20 61 6e 0a  ules to open an.
1da2f 20 20 20 20 2a 2a 20 69 6e 64 65 78 65 64 20 63      ** indexed c
1da30 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e  olumn for writin
1da31 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  g..    */.    if
1da32 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20  ( flags ){.     
1da33 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
1da34 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
1da35 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
1da36 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
1da37 74 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  t){.        int 
1da38 6a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a  j;.        for(j
1da39 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; j<pIdx->nCol
1da3a 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  umn; j++){.     
1da3b 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
1da3c 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 43 6f 6c  iColumn[j]==iCol
1da3d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1da3e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1da3f 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1da40 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Err,.           
1da41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da42 20 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 69    "cannot open i
1da43 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 6f  ndexed column fo
1da44 72 20 77 72 69 74 69 6e 67 22 29 3b 0a 20 20 20  r writing");.   
1da45 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
1da46 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1da47 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 73 71          (void)sq
1da48 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1da49 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  b);.            
1da4a 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1da4b 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  eAll(db);.      
1da4c 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
1da4d 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  open_out;.      
1da4e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
1da4f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
1da50 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 56 64     v = sqlite3Vd
1da51 62 65 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20  beCreate(db);.  
1da52 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20    if( v ){.     
1da53 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74   int iDb = sqlit
1da54 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1da55 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1da56 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  a);.      sqlite
1da57 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
1da58 2c 20 73 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f  , sizeof(openBlo
1da59 62 29 2f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70  b)/sizeof(VdbeOp
1da5a 4c 69 73 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29  List), openBlob)
1da5b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66  ;..      /* Conf
1da5c 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61  igure the OP_Tra
1da5d 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  nsaction */.    
1da5e 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1da5f 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29  ngeP1(v, 0, iDb)
1da60 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1da61 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30  dbeChangeP2(v, 0
1da62 2c 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30  , (flags ? 1 : 0
1da63 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  ));..      /* Co
1da64 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 56  nfigure the OP_V
1da65 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a 2f 0a 20  erifyCookie */. 
1da66 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1da67 43 68 61 6e 67 65 50 31 28 76 2c 20 31 2c 20 69  ChangeP1(v, 1, i
1da68 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
1da69 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
1da6a 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  , 1, pTab->pSche
1da6b 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
1da6c 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61  e);..      /* Ma
1da6d 6b 65 20 73 75 72 65 20 61 20 6d 75 74 65 78 20  ke sure a mutex 
1da6e 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 74  is held on the t
1da6f 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73  able to be acces
1da70 73 65 64 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  sed */.      sql
1da71 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
1da72 65 28 76 2c 20 69 44 62 29 3b 20 0a 0a 20 20 20  e(v, iDb); ..   
1da73 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74     /* Remove eit
1da74 68 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57  her the OP_OpenW
1da75 72 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64  rite or OpenRead
1da76 2e 20 53 65 74 20 74 68 65 20 50 32 20 0a 20 20  . Set the P2 .  
1da77 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72      ** parameter
1da78 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f   of the other to
1da79 20 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 0a 20 20   pTab->tnum. .  
1da7a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
1da7b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
1da7c 4e 6f 6f 70 28 76 2c 20 28 66 6c 61 67 73 20 3f  Noop(v, (flags ?
1da7d 20 32 20 3a 20 33 29 2c 20 31 29 3b 0a 20 20 20   2 : 3), 1);.   
1da7e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1da7f 61 6e 67 65 50 32 28 76 2c 20 28 66 6c 61 67 73  angeP2(v, (flags
1da80 20 3f 20 33 20 3a 20 32 29 2c 20 70 54 61 62 2d   ? 3 : 2), pTab-
1da81 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71  >tnum);.      sq
1da82 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1da83 33 28 76 2c 20 28 66 6c 61 67 73 20 3f 20 33 20  3(v, (flags ? 3 
1da84 3a 20 32 29 2c 20 69 44 62 29 3b 0a 0a 20 20 20  : 2), iDb);..   
1da85 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20     /* Configure 
1da86 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
1da87 6c 75 6d 6e 73 2e 20 43 6f 6e 66 69 67 75 72 65  lumns. Configure
1da88 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 0a 20   the cursor to. 
1da89 20 20 20 20 20 2a 2a 20 74 68 69 6e 6b 20 74 68       ** think th
1da8a 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61 73  at the table has
1da8b 20 6f 6e 65 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e   one more column
1da8c 20 74 68 61 6e 20 69 74 20 72 65 61 6c 6c 79 0a   than it really.
1da8d 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 2e 20 41        ** does. A
1da8e 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72  n OP_Column to r
1da8f 65 74 72 69 65 76 65 20 74 68 69 73 20 69 6d 61  etrieve this ima
1da90 67 69 6e 61 72 79 20 63 6f 6c 75 6d 6e 20 77 69  ginary column wi
1da91 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6c 77 61  ll.      ** alwa
1da92 79 73 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c  ys return an SQL
1da93 20 4e 55 4c 4c 2e 20 54 68 69 73 20 69 73 20 75   NULL. This is u
1da94 73 65 66 75 6c 20 62 65 63 61 75 73 65 20 69 74  seful because it
1da95 20 6d 65 61 6e 73 0a 20 20 20 20 20 20 2a 2a 20   means.      ** 
1da96 77 65 20 63 61 6e 20 69 6e 76 6f 6b 65 20 4f 50  we can invoke OP
1da97 5f 43 6f 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c 20  _Column to fill 
1da98 69 6e 20 74 68 65 20 76 64 62 65 20 63 75 72 73  in the vdbe curs
1da99 6f 72 73 20 74 79 70 65 20 0a 20 20 20 20 20 20  ors type .      
1da9a 2a 2a 20 61 6e 64 20 6f 66 66 73 65 74 20 63 61  ** and offset ca
1da9b 63 68 65 20 77 69 74 68 6f 75 74 20 63 61 75 73  che without caus
1da9c 69 6e 67 20 61 6e 79 20 49 4f 2e 0a 20 20 20 20  ing any IO..    
1da9d 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
1da9e 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
1da9f 2c 20 66 6c 61 67 73 20 3f 20 33 20 3a 20 32 2c  , flags ? 3 : 2,
1daa0 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50   SQLITE_INT_TO_P
1daa1 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29  TR(pTab->nCol+1)
1daa2 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20  , P4_INT32);.   
1daa3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1daa4 61 6e 67 65 50 32 28 76 2c 20 36 2c 20 70 54 61  angeP2(v, 6, pTa
1daa5 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  b->nCol);.      
1daa6 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
1daa7 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
1daa8 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1daa9 52 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31  Ready(v, 1, 1, 1
1daaa 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
1daab 20 20 7d 0a 20 20 20 0a 20 20 20 20 73 71 6c 69    }.   .    sqli
1daac 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
1daad 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  (db);.    rc = s
1daae 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1daaf 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  db);.    if( rc!
1dab0 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62  =SQLITE_OK || db
1dab1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1dab2 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f  {.      goto blo
1dab3 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  b_open_out;.    
1dab4 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62  }..    sqlite3_b
1dab5 69 6e 64 5f 69 6e 74 36 34 28 28 73 71 6c 69 74  ind_int64((sqlit
1dab6 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20  e3_stmt *)v, 1, 
1dab7 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d 20  iRow);.    rc = 
1dab8 73 71 6c 69 74 65 33 5f 73 74 65 70 28 28 73 71  sqlite3_step((sq
1dab9 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b  lite3_stmt *)v);
1daba 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1dabb 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
1dabc 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20   nAttempt++;.   
1dabd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
1dabe 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c 69 74 65  finalize((sqlite
1dabf 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20  3_stmt *)v);.   
1dac0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1dac1 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
1dac2 2c 20 7a 45 72 72 2c 20 73 71 6c 69 74 65 33 5f  , zErr, sqlite3_
1dac3 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20  errmsg(db));.   
1dac4 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d 0a     v = 0;.    }.
1dac5 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74 65    } while( nAtte
1dac6 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51 4c  mpt<5 && rc==SQL
1dac7 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a 20  ITE_SCHEMA );.. 
1dac8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1dac9 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ROW ){.    /* Th
1daca 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61 73  e row-record has
1dacb 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75 63   been opened suc
1dacc 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63 6b  cessfully. Check
1dacd 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a   that the.    **
1dace 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73 74   column in quest
1dacf 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65 78  ion contains tex
1dad0 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66 20  t or a blob. If 
1dad1 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20  it contains.    
1dad2 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20 75  ** text, it is u
1dad3 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20  p to the caller 
1dad4 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f 64  to get the encod
1dad5 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20 2a  ing right..    *
1dad6 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20 2a  /.    Incrblob *
1dad7 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20 74  pBlob;.    u32 t
1dad8 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30  ype = v->apCsr[0
1dad9 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b 0a  ]->aType[iCol];.
1dada 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31 32  .    if( type<12
1dadb 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1dadc 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1dadd 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 22  f(zErr), zErr, "
1dade 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 61 6c 75  cannot open valu
1dadf 65 20 6f 66 20 74 79 70 65 20 25 73 22 2c 0a 20  e of type %s",. 
1dae0 20 20 20 20 20 20 20 20 20 74 79 70 65 3d 3d 30           type==0
1dae1 3f 22 6e 75 6c 6c 22 3a 20 74 79 70 65 3d 3d 37  ?"null": type==7
1dae2 3f 22 72 65 61 6c 22 3a 20 22 69 6e 74 65 67 65  ?"real": "intege
1dae3 72 22 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  r".      );.    
1dae4 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1dae5 52 4f 52 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ROR;.      goto 
1dae6 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  blob_open_out;. 
1dae7 20 20 20 7d 0a 20 20 20 20 70 42 6c 6f 62 20 3d     }.    pBlob =
1dae8 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 73 71 6c   (Incrblob *)sql
1dae9 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
1daea 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e 63 72  (db, sizeof(Incr
1daeb 62 6c 6f 62 29 29 3b 0a 20 20 20 20 69 66 28 20  blob));.    if( 
1daec 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1daed 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1daee 33 44 62 46 72 65 65 28 64 62 2c 20 70 42 6c 6f  3DbFree(db, pBlo
1daef 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62  b);.      goto b
1daf0 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  lob_open_out;.  
1daf1 20 20 7d 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 66    }.    pBlob->f
1daf2 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20  lags = flags;.  
1daf3 20 20 70 42 6c 6f 62 2d 3e 70 43 73 72 20 3d 20    pBlob->pCsr = 
1daf4 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 70 43   v->apCsr[0]->pC
1daf5 75 72 73 6f 72 3b 0a 20 20 20 20 73 71 6c 69 74  ursor;.    sqlit
1daf6 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73  e3BtreeEnterCurs
1daf7 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b  or(pBlob->pCsr);
1daf8 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1daf9 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 70  eCacheOverflow(p
1dafa 42 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20  Blob->pCsr);.   
1dafb 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1dafc 76 65 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e  veCursor(pBlob->
1dafd 70 43 73 72 29 3b 0a 20 20 20 20 70 42 6c 6f 62  pCsr);.    pBlob
1dafe 2d 3e 70 53 74 6d 74 20 3d 20 28 73 71 6c 69 74  ->pStmt = (sqlit
1daff 65 33 5f 73 74 6d 74 20 2a 29 76 3b 0a 20 20 20  e3_stmt *)v;.   
1db00 20 70 42 6c 6f 62 2d 3e 69 4f 66 66 73 65 74 20   pBlob->iOffset 
1db01 3d 20 76 2d 3e 61 70 43 73 72 5b 30 5d 2d 3e 61  = v->apCsr[0]->a
1db02 4f 66 66 73 65 74 5b 69 43 6f 6c 5d 3b 0a 20 20  Offset[iCol];.  
1db03 20 20 70 42 6c 6f 62 2d 3e 6e 42 79 74 65 20 3d    pBlob->nByte =
1db04 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1db05 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65 29 3b  alTypeLen(type);
1db06 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 64 62 20 3d  .    pBlob->db =
1db07 20 64 62 3b 0a 20 20 20 20 2a 70 70 42 6c 6f 62   db;.    *ppBlob
1db08 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62   = (sqlite3_blob
1db09 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 20 20 72 63   *)pBlob;.    rc
1db0a 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1db0b 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
1db0c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
1db0d 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1db0e 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45  sizeof(zErr), zE
1db0f 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 72 6f 77  rr, "no such row
1db10 69 64 3a 20 25 6c 6c 64 22 2c 20 69 52 6f 77 29  id: %lld", iRow)
1db11 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1db12 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c  E_ERROR;.  }..bl
1db13 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 7a  ob_open_out:.  z
1db14 45 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72 29  Err[sizeof(zErr)
1db15 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66  -1] = '\0';.  if
1db16 28 20 76 20 26 26 20 28 72 63 21 3d 53 51 4c 49  ( v && (rc!=SQLI
1db17 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c  TE_OK || db->mal
1db18 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b 0a 20 20  locFailed) ){.  
1db19 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
1db1a 61 6c 69 7a 65 28 76 29 3b 0a 20 20 7d 0a 20 20  alize(v);.  }.  
1db1b 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
1db1c 20 72 63 2c 20 28 72 63 3d 3d 53 51 4c 49 54 45   rc, (rc==SQLITE
1db1d 5f 4f 4b 3f 30 3a 7a 45 72 72 29 29 3b 0a 20 20  _OK?0:zErr));.  
1db1e 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
1db1f 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
1db20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1db21 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
1db22 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1db23 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c 6f  *.** Close a blo
1db24 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61  b handle that wa
1db25 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 65  s previously cre
1db26 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71  ated using.** sq
1db27 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
1db28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
1db29 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f   int sqlite3_blo
1db2a 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  b_close(sqlite3_
1db2b 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20  blob *pBlob){.  
1db2c 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49  Incrblob *p = (I
1db2d 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b  ncrblob *)pBlob;
1db2e 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
1db2f 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 64 62 20  ite3 *db;..  db 
1db30 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74  = p->db;.  sqlit
1db31 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1db32 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20  b->mutex);.  rc 
1db33 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  = sqlite3_finali
1db34 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  ze(p->pStmt);.  
1db35 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1db36 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  , p);.  sqlite3_
1db37 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
1db38 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
1db39 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65   rc;.}../*.** Pe
1db3a 72 66 6f 72 6d 20 61 20 72 65 61 64 20 6f 72 20  rform a read or 
1db3b 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20  write operation 
1db3c 6f 6e 20 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61  on a blob.*/.sta
1db3d 74 69 63 20 69 6e 74 20 62 6c 6f 62 52 65 61 64  tic int blobRead
1db3e 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33  Write(.  sqlite3
1db3f 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a 20  _blob *pBlob, . 
1db40 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74   void *z, .  int
1db41 20 6e 2c 20 0a 20 20 69 6e 74 20 69 4f 66 66 73   n, .  int iOffs
1db42 65 74 2c 20 0a 20 20 69 6e 74 20 28 2a 78 43 61  et, .  int (*xCa
1db43 6c 6c 29 28 42 74 43 75 72 73 6f 72 2a 2c 20 75  ll)(BtCursor*, u
1db44 33 32 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 0a  32, u32, void*).
1db45 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 49  ){.  int rc;.  I
1db46 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e  ncrblob *p = (In
1db47 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a  crblob *)pBlob;.
1db48 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c    Vdbe *v;.  sql
1db49 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
1db4a 3b 20 20 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ;  ..  sqlite3_m
1db4b 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
1db4c 75 74 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64  utex);.  v = (Vd
1db4d 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20  be*)p->pStmt;.. 
1db4e 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66   if( n<0 || iOff
1db4f 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65  set<0 || (iOffse
1db50 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b  t+n)>p->nByte ){
1db51 0a 20 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20  .    /* Request 
1db52 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e  is out of range.
1db53 20 52 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69   Return a transi
1db54 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20  ent error. */.  
1db55 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1db56 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ROR;.    sqlite3
1db57 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
1db58 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20  _ERROR, 0);.  } 
1db59 65 6c 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b  else if( v==0 ){
1db5a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
1db5b 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74   is no statement
1db5c 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68   handle, then th
1db5d 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61  e blob-handle ha
1db5e 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79  s.    ** already
1db5f 20 62 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65   been invalidate
1db60 64 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  d. Return SQLITE
1db61 5f 41 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63  _ABORT in this c
1db62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
1db63 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52  rc = SQLITE_ABOR
1db64 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  T;.  }else{.    
1db65 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42  /* Call either B
1db66 74 72 65 65 44 61 74 61 28 29 20 6f 72 20 42 74  treeData() or Bt
1db67 72 65 65 50 75 74 44 61 74 61 28 29 2e 20 49 66  reePutData(). If
1db68 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73   SQLITE_ABORT is
1db69 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64  .    ** returned
1db6a 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73  , clean-up the s
1db6b 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e  tatement handle.
1db6c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
1db6d 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20  rt( db == v->db 
1db6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
1db6f 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70  reeEnterCursor(p
1db70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20  ->pCsr);.    rc 
1db71 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c  = xCall(p->pCsr,
1db72 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66   iOffset+p->iOff
1db73 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20  set, n, z);.    
1db74 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1db75 65 43 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29  eCursor(p->pCsr)
1db76 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1db77 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20  LITE_ABORT ){.  
1db78 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
1db79 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20  inalize(v);.    
1db7a 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a    p->pStmt = 0;.
1db7b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1db7c 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72   db->errCode = r
1db7d 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d  c;.      v->rc =
1db7e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   rc;.    }.  }. 
1db7f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1db80 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
1db81 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1db82 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
1db83 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1db84 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  /*.** Read data 
1db85 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64  from a blob hand
1db86 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
1db87 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1db88 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f  ob_read(sqlite3_
1db89 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69  blob *pBlob, voi
1db8a 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  d *z, int n, int
1db8b 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74   iOffset){.  ret
1db8c 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74  urn blobReadWrit
1db8d 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69  e(pBlob, z, n, i
1db8e 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42  Offset, sqlite3B
1db8f 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a  treeData);.}../*
1db90 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 74  .** Write data t
1db91 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e  o a blob handle.
1db92 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1db93 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
1db94 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c  write(sqlite3_bl
1db95 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74  ob *pBlob, const
1db96 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c   void *z, int n,
1db97 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20   int iOffset){. 
1db98 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64   return blobRead
1db99 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f  Write(pBlob, (vo
1db9a 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73  id *)z, n, iOffs
1db9b 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65  et, sqlite3Btree
1db9c 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a  PutData);.}../*.
1db9d 2a 2a 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20  ** Query a blob 
1db9e 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73  handle for the s
1db9f 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e  ize of the data.
1dba0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62  .**.** The Incrb
1dba1 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20  lob.nByte field 
1dba2 69 73 20 66 69 78 65 64 20 66 6f 72 20 74 68 65  is fixed for the
1dba3 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65   lifetime of the
1dba4 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20   Incrblob.** so 
1dba5 6e 6f 20 6d 75 74 65 78 20 69 73 20 72 65 71 75  no mutex is requ
1dba6 69 72 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e  ired for access.
1dba7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1dba8 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  nt sqlite3_blob_
1dba9 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c  bytes(sqlite3_bl
1dbaa 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e  ob *pBlob){.  In
1dbab 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63  crblob *p = (Inc
1dbac 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20  rblob *)pBlob;. 
1dbad 20 72 65 74 75 72 6e 20 70 2d 3e 6e 42 79 74 65   return p->nByte
1dbae 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 23  ;.}..#endif /* #
1dbaf 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1dbb0 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a  IT_INCRBLOB */..
1dbb1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1dbb2 45 6e 64 20 6f 66 20 76 64 62 65 62 6c 6f 62 2e  End of vdbeblob.
1dbb3 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1dbb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1dbb6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1dbb7 42 65 67 69 6e 20 66 69 6c 65 20 6a 6f 75 72 6e  Begin file journ
1dbb8 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  al.c ***********
1dbb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1dbbb 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
1dbbc 74 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 22.**.** The a
1dbbd 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
1dbbe 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
1dbbf 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
1dbc0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
1dbc1 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
1dbc2 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
1dbc3 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
1dbc4 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
1dbc5 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
1dbc6 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
1dbc7 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
1dbc8 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
1dbc9 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
1dbca 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
1dbcb 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
1dbcc 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
1dbcd 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
1dbce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbcf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbd1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dbd2 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  ***.**.** @(#) $
1dbd3 49 64 3a 20 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20  Id: journal.c,v 
1dbd4 31 2e 39 20 32 30 30 39 2f 30 31 2f 32 30 20 31  1.9 2009/01/20 1
1dbd5 37 3a 30 36 3a 32 37 20 64 61 6e 69 65 6c 6b 31  7:06:27 danielk1
1dbd6 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  977 Exp $.*/..#i
1dbd7 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
1dbd8 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a  LE_ATOMIC_WRITE.
1dbd9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ./*.** This file
1dbda 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 73 70   implements a sp
1dbdb 65 63 69 61 6c 20 6b 69 6e 64 20 6f 66 20 73 71  ecial kind of sq
1dbdc 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63  lite3_file objec
1dbdd 74 20 75 73 65 64 0a 2a 2a 20 62 79 20 53 51 4c  t used.** by SQL
1dbde 69 74 65 20 74 6f 20 63 72 65 61 74 65 20 6a 6f  ite to create jo
1dbdf 75 72 6e 61 6c 20 66 69 6c 65 73 20 69 66 20 74  urnal files if t
1dbe0 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20  he atomic-write 
1dbe1 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20  optimization.** 
1dbe2 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a  is enabled..**.*
1dbe3 2a 20 54 68 65 20 64 69 73 74 69 6e 63 74 69 76  * The distinctiv
1dbe4 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
1dbe5 20 6f 66 20 74 68 69 73 20 73 71 6c 69 74 65 33   of this sqlite3
1dbe6 5f 66 69 6c 65 20 69 73 20 74 68 61 74 20 74 68  _file is that th
1dbe7 65 0a 2a 2a 20 61 63 74 75 61 6c 20 6f 6e 20 64  e.** actual on d
1dbe8 69 73 6b 20 66 69 6c 65 20 69 73 20 63 72 65 61  isk file is crea
1dbe9 74 65 64 20 6c 61 7a 69 6c 79 2e 20 57 68 65 6e  ted lazily. When
1dbea 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 72 65   the file is cre
1dbeb 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 63 61 6c  ated,.** the cal
1dbec 6c 65 72 20 73 70 65 63 69 66 69 65 73 20 61 20  ler specifies a 
1dbed 62 75 66 66 65 72 20 73 69 7a 65 20 66 6f 72 20  buffer size for 
1dbee 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 62 75 66  an in-memory buf
1dbef 66 65 72 20 74 6f 0a 2a 2a 20 62 65 20 75 73 65  fer to.** be use
1dbf0 64 20 74 6f 20 73 65 72 76 69 63 65 20 72 65 61  d to service rea
1dbf1 64 28 29 20 61 6e 64 20 77 72 69 74 65 28 29 20  d() and write() 
1dbf2 72 65 71 75 65 73 74 73 2e 20 54 68 65 20 61 63  requests. The ac
1dbf3 74 75 61 6c 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20  tual file.** on 
1dbf4 64 69 73 6b 20 69 73 20 6e 6f 74 20 63 72 65 61  disk is not crea
1dbf5 74 65 64 20 6f 72 20 70 6f 70 75 6c 61 74 65 64  ted or populated
1dbf6 20 75 6e 74 69 6c 20 65 69 74 68 65 72 3a 0a 2a   until either:.*
1dbf7 2a 0a 2a 2a 20 20 20 31 29 20 54 68 65 20 69 6e  *.**   1) The in
1dbf8 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e  -memory represen
1dbf9 74 61 74 69 6f 6e 20 67 72 6f 77 73 20 74 6f 6f  tation grows too
1dbfa 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20 61   large for the a
1dbfb 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 20 20 20  llocated .**    
1dbfc 20 20 62 75 66 66 65 72 2c 20 6f 72 0a 2a 2a 20    buffer, or.** 
1dbfd 20 20 32 29 20 54 68 65 20 73 71 6c 69 74 65 33    2) The sqlite3
1dbfe 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 20  JournalCreate() 
1dbff 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1dc00 65 64 2e 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  ed..*/..../*.** 
1dc01 41 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 6f 62  A JournalFile ob
1dc02 6a 65 63 74 20 69 73 20 61 20 73 75 62 63 6c 61  ject is a subcla
1dc03 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69  ss of sqlite3_fi
1dc04 6c 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 61 73  le used by.** as
1dc05 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 68 61   an open file ha
1dc06 6e 64 6c 65 20 66 6f 72 20 6a 6f 75 72 6e 61 6c  ndle for journal
1dc07 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63   files..*/.struc
1dc08 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 7b 0a  t JournalFile {.
1dc09 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74    sqlite3_io_met
1dc0a 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20  hods *pMethod;  
1dc0b 20 20 2f 2a 20 49 2f 4f 20 6d 65 74 68 6f 64 73    /* I/O methods
1dc0c 20 6f 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   on journal file
1dc0d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 3b  s */.  int nBuf;
1dc0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dc0f 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1dc10 66 20 7a 42 75 66 5b 5d 20 69 6e 20 62 79 74 65  f zBuf[] in byte
1dc11 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 75  s */.  char *zBu
1dc12 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
1dc13 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20         /* Space 
1dc14 74 6f 20 62 75 66 66 65 72 20 6a 6f 75 72 6e 61  to buffer journa
1dc15 6c 20 77 72 69 74 65 73 20 2a 2f 0a 20 20 69 6e  l writes */.  in
1dc16 74 20 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20  t iSize;        
1dc17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1dc18 20 41 6d 6f 75 6e 74 20 6f 66 20 7a 42 75 66 5b   Amount of zBuf[
1dc19 5d 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64  ] currently used
1dc1a 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b   */.  int flags;
1dc1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dc1c 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 66        /* xOpen f
1dc1d 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  lags */.  sqlite
1dc1e 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20  3_vfs *pVfs;    
1dc1f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1dc20 20 22 72 65 61 6c 22 20 75 6e 64 65 72 6c 79 69   "real" underlyi
1dc21 6e 67 20 56 46 53 20 2a 2f 0a 20 20 73 71 6c 69  ng VFS */.  sqli
1dc22 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 61 6c 3b  te3_file *pReal;
1dc23 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1dc24 68 65 20 22 72 65 61 6c 22 20 75 6e 64 65 72 6c  he "real" underl
1dc25 79 69 6e 67 20 66 69 6c 65 20 64 65 73 63 72 69  ying file descri
1dc26 70 74 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ptor */.  const 
1dc27 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20  char *zJournal; 
1dc28 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
1dc29 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
1dc2a 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65   file */.};.type
1dc2b 64 65 66 20 73 74 72 75 63 74 20 4a 6f 75 72 6e  def struct Journ
1dc2c 61 6c 46 69 6c 65 20 4a 6f 75 72 6e 61 6c 46 69  alFile JournalFi
1dc2d 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 69 74  le;../*.** If it
1dc2e 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64   does not alread
1dc2f 79 20 65 78 69 73 74 73 2c 20 63 72 65 61 74 65  y exists, create
1dc30 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 74 68   and populate th
1dc31 65 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 20 0a  e on-disk file .
1dc32 2a 2a 20 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69  ** for JournalFi
1dc33 6c 65 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  le p..*/.static 
1dc34 69 6e 74 20 63 72 65 61 74 65 46 69 6c 65 28 4a  int createFile(J
1dc35 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 29 7b 0a  ournalFile *p){.
1dc36 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1dc37 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 70 2d 3e  E_OK;.  if( !p->
1dc38 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c  pReal ){.    sql
1dc39 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 65 61 6c  ite3_file *pReal
1dc3a 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65   = (sqlite3_file
1dc3b 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 72 63   *)&p[1];.    rc
1dc3c 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e   = sqlite3OsOpen
1dc3d 28 70 2d 3e 70 56 66 73 2c 20 70 2d 3e 7a 4a 6f  (p->pVfs, p->zJo
1dc3e 75 72 6e 61 6c 2c 20 70 52 65 61 6c 2c 20 70 2d  urnal, pReal, p-
1dc3f 3e 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 20 20  >flags, 0);.    
1dc40 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1dc41 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 52  K ){.      p->pR
1dc42 65 61 6c 20 3d 20 70 52 65 61 6c 3b 0a 20 20 20  eal = pReal;.   
1dc43 20 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3e     if( p->iSize>
1dc44 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  0 ){.        ass
1dc45 65 72 74 28 70 2d 3e 69 53 69 7a 65 3c 3d 70 2d  ert(p->iSize<=p-
1dc46 3e 6e 42 75 66 29 3b 0a 20 20 20 20 20 20 20 20  >nBuf);.        
1dc47 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
1dc48 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 70 2d  ite(p->pReal, p-
1dc49 3e 7a 42 75 66 2c 20 70 2d 3e 69 53 69 7a 65 2c  >zBuf, p->iSize,
1dc4a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1dc4b 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1dc4c 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  rc;.}../*.** Clo
1dc4d 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a  se the file..*/.
1dc4e 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 43  static int jrnlC
1dc4f 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
1dc50 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4a 6f 75 72  e *pJfd){.  Jour
1dc51 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1dc52 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1dc53 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ;.  if( p->pReal
1dc54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f   ){.    sqlite3O
1dc55 73 43 6c 6f 73 65 28 70 2d 3e 70 52 65 61 6c 29  sClose(p->pReal)
1dc56 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1dc57 66 72 65 65 28 70 2d 3e 7a 42 75 66 29 3b 0a 20  free(p->zBuf);. 
1dc58 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1dc59 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  K;.}../*.** Read
1dc5a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 66   data from the f
1dc5b 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
1dc5c 6e 74 20 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73  nt jrnlRead(.  s
1dc5d 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1dc5e 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75  d,    /* The jou
1dc5f 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77  rnal file from w
1dc60 68 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a  hich to read */.
1dc61 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20    void *zBuf,   
1dc62 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20           /* Put 
1dc63 74 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65  the results here
1dc64 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20   */.  int iAmt, 
1dc65 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1dc66 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1dc67 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c  to read */.  sql
1dc68 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20  ite_int64 iOfst 
1dc69 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61      /* Begin rea
1dc6a 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ding at this off
1dc6b 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  set */.){.  int 
1dc6c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1dc6d 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1dc6e 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1dc6f 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d  *)pJfd;.  if( p-
1dc70 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63  >pReal ){.    rc
1dc71 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
1dc72 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c  (p->pReal, zBuf,
1dc73 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20   iAmt, iOfst);. 
1dc74 20 7d 65 6c 73 65 20 69 66 28 20 28 69 41 6d 74   }else if( (iAmt
1dc75 2b 69 4f 66 73 74 29 3e 70 2d 3e 69 53 69 7a 65  +iOfst)>p->iSize
1dc76 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
1dc77 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
1dc78 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  READ;.  }else{. 
1dc79 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20     memcpy(zBuf, 
1dc7a 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c  &p->zBuf[iOfst],
1dc7b 20 69 41 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65   iAmt);.  }.  re
1dc7c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1dc7d 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20  * Write data to 
1dc7e 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1dc7f 74 69 63 20 69 6e 74 20 6a 72 6e 6c 57 72 69 74  tic int jrnlWrit
1dc80 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  e(.  sqlite3_fil
1dc81 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54  e *pJfd,    /* T
1dc82 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1dc83 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  into which to wr
1dc84 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ite */.  const v
1dc85 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20  oid *zBuf,      
1dc86 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20  /* Take data to 
1dc87 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20  be written from 
1dc88 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41  here */.  int iA
1dc89 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
1dc8a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1dc8b 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
1dc8c 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
1dc8d 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69  Ofst     /* Begi
1dc8e 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69  n writing at thi
1dc8f 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68  s offset into th
1dc90 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69  e file */.){.  i
1dc91 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1dc92 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  K;.  JournalFile
1dc93 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69   *p = (JournalFi
1dc94 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28  le *)pJfd;.  if(
1dc95 20 21 70 2d 3e 70 52 65 61 6c 20 26 26 20 28 69   !p->pReal && (i
1dc96 4f 66 73 74 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42  Ofst+iAmt)>p->nB
1dc97 75 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63  uf ){.    rc = c
1dc98 72 65 61 74 65 46 69 6c 65 28 70 29 3b 0a 20 20  reateFile(p);.  
1dc99 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
1dc9a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
1dc9b 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20   p->pReal ){.   
1dc9c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1dc9d 73 57 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c  sWrite(p->pReal,
1dc9e 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66   zBuf, iAmt, iOf
1dc9f 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  st);.    }else{.
1dca0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d        memcpy(&p-
1dca1 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 7a 42  >zBuf[iOfst], zB
1dca2 75 66 2c 20 69 41 6d 74 29 3b 0a 20 20 20 20 20  uf, iAmt);.     
1dca3 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 28 69   if( p->iSize<(i
1dca4 4f 66 73 74 2b 69 41 6d 74 29 20 29 7b 0a 20 20  Ofst+iAmt) ){.  
1dca5 20 20 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d        p->iSize =
1dca6 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 3b 0a 20   (iOfst+iAmt);. 
1dca7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1dca8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1dca9 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
1dcaa 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1dcab 74 69 63 20 69 6e 74 20 6a 72 6e 6c 54 72 75 6e  tic int jrnlTrun
1dcac 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  cate(sqlite3_fil
1dcad 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f  e *pJfd, sqlite_
1dcae 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 69  int64 size){.  i
1dcaf 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1dcb0 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  K;.  JournalFile
1dcb1 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69   *p = (JournalFi
1dcb2 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28  le *)pJfd;.  if(
1dcb3 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20   p->pReal ){.   
1dcb4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
1dcb5 72 75 6e 63 61 74 65 28 70 2d 3e 70 52 65 61 6c  runcate(p->pReal
1dcb6 2c 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65  , size);.  }else
1dcb7 20 69 66 28 20 73 69 7a 65 3c 70 2d 3e 69 53 69   if( size<p->iSi
1dcb8 7a 65 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69  ze ){.    p->iSi
1dcb9 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 7d 0a 20  ze = size;.  }. 
1dcba 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1dcbb 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69  *.** Sync the fi
1dcbc 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1dcbd 74 20 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74  t jrnlSync(sqlit
1dcbe 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 69  e3_file *pJfd, i
1dcbf 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74  nt flags){.  int
1dcc0 20 72 63 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69   rc;.  JournalFi
1dcc1 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
1dcc2 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
1dcc3 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
1dcc4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1dcc5 73 53 79 6e 63 28 70 2d 3e 70 52 65 61 6c 2c 20  sSync(p->pReal, 
1dcc6 66 6c 61 67 73 29 3b 0a 20 20 7d 65 6c 73 65 7b  flags);.  }else{
1dcc7 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1dcc8 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  _OK;.  }.  retur
1dcc9 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  n rc;.}../*.** Q
1dcca 75 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66  uery the size of
1dccb 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74   the file in byt
1dccc 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
1dccd 74 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73  t jrnlFileSize(s
1dcce 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1dccf 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  d, sqlite_int64 
1dcd0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  *pSize){.  int r
1dcd1 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1dcd2 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
1dcd3 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
1dcd4 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e  )pJfd;.  if( p->
1dcd5 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20  pReal ){.    rc 
1dcd6 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
1dcd7 69 7a 65 28 70 2d 3e 70 52 65 61 6c 2c 20 70 53  ize(p->pReal, pS
1dcd8 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ize);.  }else{. 
1dcd9 20 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c     *pSize = (sql
1dcda 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 69 53  ite_int64) p->iS
1dcdb 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ize;.  }.  retur
1dcdc 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
1dcdd 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20  able of methods 
1dcde 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20  for JournalFile 
1dcdf 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a  sqlite3_file obj
1dce0 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ect..*/.static s
1dce1 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f  truct sqlite3_io
1dce2 5f 6d 65 74 68 6f 64 73 20 4a 6f 75 72 6e 61 6c  _methods Journal
1dce3 46 69 6c 65 4d 65 74 68 6f 64 73 20 3d 20 7b 0a  FileMethods = {.
1dce4 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
1dce5 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
1dce6 20 20 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20    jrnlClose,    
1dce7 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20   /* xClose */.  
1dce8 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20 2f  jrnlRead,      /
1dce9 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6a 72 6e  * xRead */.  jrn
1dcea 6c 57 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78  lWrite,     /* x
1dceb 57 72 69 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54  Write */.  jrnlT
1dcec 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72  runcate,  /* xTr
1dced 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c  uncate */.  jrnl
1dcee 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53  Sync,      /* xS
1dcef 79 6e 63 20 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c  ync */.  jrnlFil
1dcf0 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65  eSize,  /* xFile
1dcf1 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Size */.  0,    
1dcf2 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
1dcf3 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  k */.  0,       
1dcf4 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
1dcf5 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1dcf6 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
1dcf7 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20  servedLock */.  
1dcf8 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1dcf9 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a  * xFileControl *
1dcfa 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
1dcfb 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a     /* xSectorSiz
1dcfc 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20  e */.  0        
1dcfd 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65        /* xDevice
1dcfe 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  Characteristics 
1dcff 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70  */.};../* .** Op
1dd00 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  en a journal fil
1dd01 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1dd02 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1dd03 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 73  JournalOpen(.  s
1dd04 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1dd05 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
1dd06 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20   VFS to use for 
1dd07 61 63 74 75 61 6c 20 66 69 6c 65 20 49 2f 4f 20  actual file I/O 
1dd08 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1dd09 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
1dd0a 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a  /* Name of the j
1dd0b 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20  ournal file */. 
1dd0c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
1dd0d 4a 66 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  Jfd,        /* P
1dd0e 72 65 61 6c 6c 6f 63 61 74 65 64 2c 20 62 6c 61  reallocated, bla
1dd0f 6e 6b 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a  nk file handle *
1dd10 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
1dd11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1dd12 2a 20 4f 70 65 6e 69 6e 67 20 66 6c 61 67 73 20  * Opening flags 
1dd13 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 20 20 20  */.  int nBuf   
1dd14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd15 2f 2a 20 42 79 74 65 73 20 62 75 66 66 65 72 65  /* Bytes buffere
1dd16 64 20 62 65 66 6f 72 65 20 6f 70 65 6e 69 6e 67  d before opening
1dd17 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a   the file */.){.
1dd18 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1dd19 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1dd1a 2a 29 70 4a 66 64 3b 0a 20 20 6d 65 6d 73 65 74  *)pJfd;.  memset
1dd1b 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4a 6f  (p, 0, sqlite3Jo
1dd1c 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 29  urnalSize(pVfs))
1dd1d 3b 0a 20 20 69 66 28 20 6e 42 75 66 3e 30 20 29  ;.  if( nBuf>0 )
1dd1e 7b 0a 20 20 20 20 70 2d 3e 7a 42 75 66 20 3d 20  {.    p->zBuf = 
1dd1f 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
1dd20 6f 28 6e 42 75 66 29 3b 0a 20 20 20 20 69 66 28  o(nBuf);.    if(
1dd21 20 21 70 2d 3e 7a 42 75 66 20 29 7b 0a 20 20 20   !p->zBuf ){.   
1dd22 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1dd23 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
1dd24 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
1dd25 6e 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  n sqlite3OsOpen(
1dd26 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66  pVfs, zName, pJf
1dd27 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20  d, flags, 0);.  
1dd28 7d 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d  }.  p->pMethod =
1dd29 20 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74   &JournalFileMet
1dd2a 68 6f 64 73 3b 0a 20 20 70 2d 3e 6e 42 75 66 20  hods;.  p->nBuf 
1dd2b 3d 20 6e 42 75 66 3b 0a 20 20 70 2d 3e 66 6c 61  = nBuf;.  p->fla
1dd2c 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 2d  gs = flags;.  p-
1dd2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d  >zJournal = zNam
1dd2e 65 3b 0a 20 20 70 2d 3e 70 56 66 73 20 3d 20 70  e;.  p->pVfs = p
1dd2f 56 66 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Vfs;.  return SQ
1dd30 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1dd31 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e  * If the argumen
1dd32 74 20 70 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  t p points to a 
1dd33 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 74 72 75  JournalFile stru
1dd34 63 74 75 72 65 2c 20 61 6e 64 20 74 68 65 20 75  cture, and the u
1dd35 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c  nderlying.** fil
1dd36 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  e has not yet be
1dd37 65 6e 20 63 72 65 61 74 65 64 2c 20 63 72 65 61  en created, crea
1dd38 74 65 20 69 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51  te it now..*/.SQ
1dd39 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1dd3a 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43   sqlite3JournalC
1dd3b 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69  reate(sqlite3_fi
1dd3c 6c 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  le *p){.  if( p-
1dd3d 3e 70 4d 65 74 68 6f 64 73 21 3d 26 4a 6f 75 72  >pMethods!=&Jour
1dd3e 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 20 29  nalFileMethods )
1dd3f 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1dd40 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65  ITE_OK;.  }.  re
1dd41 74 75 72 6e 20 63 72 65 61 74 65 46 69 6c 65 28  turn createFile(
1dd42 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1dd43 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74  );.}../* .** Ret
1dd44 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1dd45 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
1dd46 20 74 6f 20 73 74 6f 72 65 20 61 20 4a 6f 75 72   to store a Jour
1dd47 6e 61 6c 46 69 6c 65 20 74 68 61 74 20 75 73 65  nalFile that use
1dd48 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f  s vfs.** pVfs to
1dd49 20 63 72 65 61 74 65 20 74 68 65 20 75 6e 64 65   create the unde
1dd4a 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66  rlying on-disk f
1dd4b 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  iles..*/.SQLITE_
1dd4c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1dd4d 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73  te3JournalSize(s
1dd4e 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1dd4f 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 56 66  ){.  return (pVf
1dd50 73 2d 3e 73 7a 4f 73 46 69 6c 65 2b 73 69 7a 65  s->szOsFile+size
1dd51 6f 66 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 29 29  of(JournalFile))
1dd52 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
1dd53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1dd54 6f 66 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a  of journal.c ***
1dd55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1dd58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1dd59 6e 20 66 69 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61  n file memjourna
1dd5a 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
1dd5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1dd5d 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 37  * 2008 October 7
1dd5e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1dd5f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1dd60 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1dd61 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1dd62 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1dd63 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1dd64 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1dd65 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1dd66 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1dd67 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1dd68 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1dd69 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1dd6a 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1dd6b 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1dd6c 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1dd6d 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1dd6e 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1dd6f 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1dd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1dd73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1dd74 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
1dd75 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
1dd76 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  e to implement a
1dd77 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c  n in-memory roll
1dd78 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a  back journal..**
1dd79 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72   The in-memory r
1dd7a 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
1dd7b 69 73 20 75 73 65 64 20 74 6f 20 6a 6f 75 72 6e  is used to journ
1dd7c 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  al transactions 
1dd7d 66 6f 72 0a 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a  for.** ":memory:
1dd7e 22 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20  " databases and 
1dd7f 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  when the journal
1dd80 5f 6d 6f 64 65 3d 4d 45 4d 4f 52 59 20 70 72 61  _mode=MEMORY pra
1dd81 67 6d 61 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a  gma is used..**.
1dd82 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6d 65 6d  ** @(#) $Id: mem
1dd83 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 31 32  journal.c,v 1.12
1dd84 20 32 30 30 39 2f 30 35 2f 30 34 20 31 31 3a 34   2009/05/04 11:4
1dd85 32 3a 33 30 20 64 61 6e 69 65 6c 6b 31 39 37 37  2:30 danielk1977
1dd86 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f   Exp $.*/../* Fo
1dd87 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73  rward references
1dd88 20 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72   to internal str
1dd89 75 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64  uctures */.typed
1dd8a 65 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75  ef struct MemJou
1dd8b 72 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b  rnal MemJournal;
1dd8c 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1dd8d 46 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f  FilePoint FilePo
1dd8e 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  int;.typedef str
1dd8f 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69  uct FileChunk Fi
1dd90 6c 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61  leChunk;../* Spa
1dd91 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
1dd92 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
1dd93 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  is allocated in 
1dd94 69 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a  increments of.**
1dd95 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73   this many bytes
1dd96 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65  ..**.** The size
1dd97 20 63 68 6f 73 65 6e 20 69 73 20 61 20 6c 69 74   chosen is a lit
1dd98 74 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 61 20  tle less than a 
1dd99 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 54  power of two.  T
1dd9a 68 61 74 20 77 61 79 2c 0a 2a 2a 20 74 68 65 20  hat way,.** the 
1dd9b 46 69 6c 65 43 68 75 6e 6b 20 6f 62 6a 65 63 74  FileChunk object
1dd9c 20 77 69 6c 6c 20 68 61 76 65 20 61 20 73 69 7a   will have a siz
1dd9d 65 20 74 68 61 74 20 61 6c 6d 6f 73 74 20 65 78  e that almost ex
1dd9e 61 63 74 6c 79 20 66 69 6c 6c 73 0a 2a 2a 20 61  actly fills.** a
1dd9f 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c   power-of-two al
1dda0 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20  location.  This 
1dda1 6d 69 6d 69 6d 69 7a 65 73 20 77 61 73 74 65 64  mimimizes wasted
1dda2 20 73 70 61 63 65 20 69 6e 20 70 6f 77 65 72 2d   space in power-
1dda3 6f 66 2d 74 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79  of-two.** memory
1dda4 20 61 6c 6c 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a   allocators..*/.
1dda5 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f  #define JOURNAL_
1dda6 43 48 55 4e 4b 53 49 5a 45 20 28 28 69 6e 74 29  CHUNKSIZE ((int)
1dda7 28 31 30 32 34 2d 73 69 7a 65 6f 66 28 46 69 6c  (1024-sizeof(Fil
1dda8 65 43 68 75 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d  eChunk*)))../* M
1dda9 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65  acro to find the
1ddaa 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20   minimum of two 
1ddab 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a  numeric values..
1ddac 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23  */.#ifndef MIN.#
1ddad 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29   define MIN(x,y)
1ddae 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79   ((x)<(y)?(x):(y
1ddaf 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  )).#endif../*.**
1ddb0 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   The rollback jo
1ddb1 75 72 6e 61 6c 20 69 73 20 63 6f 6d 70 6f 73 65  urnal is compose
1ddb2 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d of a linked li
1ddb3 73 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75  st of these stru
1ddb4 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63  ctures..*/.struc
1ddb5 74 20 46 69 6c 65 43 68 75 6e 6b 20 7b 0a 20 20  t FileChunk {.  
1ddb6 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 78 74  FileChunk *pNext
1ddb7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1ddb8 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 69 6e  /* Next chunk in
1ddb9 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   the journal */.
1ddba 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52    u8 zChunk[JOUR
1ddbb 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 5d 3b 20  NAL_CHUNKSIZE]; 
1ddbc 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20    /* Content of 
1ddbd 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b  this chunk */.};
1ddbe 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
1ddbf 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
1ddc0 63 74 20 73 65 72 76 65 73 20 61 73 20 61 20 63  ct serves as a c
1ddc1 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 72  ursor into the r
1ddc2 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
1ddc3 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63  .** The cursor c
1ddc4 61 6e 20 62 65 20 65 69 74 68 65 72 20 66 6f 72  an be either for
1ddc5 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74   reading or writ
1ddc6 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46  ing..*/.struct F
1ddc7 69 6c 65 50 6f 69 6e 74 20 7b 0a 20 20 73 71 6c  ilePoint {.  sql
1ddc8 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73  ite3_int64 iOffs
1ddc9 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  et;          /* 
1ddca 4f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20  Offset from the 
1ddcb 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
1ddcc 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 43   file */.  FileC
1ddcd 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20  hunk *pChunk;   
1ddce 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
1ddcf 65 63 69 66 69 63 20 63 68 75 6e 6b 20 69 6e 74  ecific chunk int
1ddd0 6f 20 77 68 69 63 68 20 63 75 72 73 6f 72 20 70  o which cursor p
1ddd1 6f 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  oints */.};../*.
1ddd2 2a 2a 20 54 68 69 73 20 73 75 62 63 6c 61 73 73  ** This subclass
1ddd3 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f   is a subclass o
1ddd4 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 20  f sqlite3_file. 
1ddd5 20 45 61 63 68 20 6f 70 65 6e 20 6d 65 6d 6f 72   Each open memor
1ddd6 79 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 73 20  y-journal.** is 
1ddd7 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
1ddd8 68 69 73 20 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74  his class..*/.st
1ddd9 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ruct MemJournal 
1ddda 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  {.  sqlite3_io_m
1dddb 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b  ethods *pMethod;
1dddc 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 63 6c      /* Parent cl
1dddd 61 73 73 2e 20 4d 55 53 54 20 42 45 20 46 49 52  ass. MUST BE FIR
1ddde 53 54 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e  ST */.  FileChun
1dddf 6b 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20  k *pFirst;      
1dde0 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 64 20          /* Head 
1dde1 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 68 75  of in-memory chu
1dde2 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 46 69 6c  nk-list */.  Fil
1dde3 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69 6e 74 3b  ePoint endpoint;
1dde4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1dde5 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65  Pointer to the e
1dde6 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  nd of the file *
1dde7 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 72 65  /.  FilePoint re
1dde8 61 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20  adpoint;        
1dde9 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1ddea 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
1ddeb 20 6c 61 73 74 20 78 52 65 61 64 28 29 20 2a 2f   last xRead() */
1ddec 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  .};../*.** Read 
1dded 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 69 6e  data from the in
1ddee 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20  -memory journal 
1ddef 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 74  file.  This is t
1ddf0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
1ddf1 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69  n.** of the sqli
1ddf2 74 65 33 5f 76 66 73 2e 78 52 65 61 64 20 6d 65  te3_vfs.xRead me
1ddf3 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
1ddf4 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 28  int memjrnlRead(
1ddf5 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
1ddf6 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65  *pJfd,    /* The
1ddf7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72   journal file fr
1ddf8 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 64  om which to read
1ddf9 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66   */.  void *zBuf
1ddfa 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
1ddfb 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 20  Put the results 
1ddfc 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41  here */.  int iA
1ddfd 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
1ddfe 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1ddff 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  tes to read */. 
1de00 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f   sqlite_int64 iO
1de01 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e  fst     /* Begin
1de02 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73   reading at this
1de03 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20   offset */.){.  
1de04 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20  MemJournal *p = 
1de05 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a  (MemJournal *)pJ
1de06 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 74 20 3d  fd;.  u8 *zOut =
1de07 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 6e 52 65   zBuf;.  int nRe
1de08 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20 69 6e 74  ad = iAmt;.  int
1de09 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20   iChunkOffset;. 
1de0a 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75   FileChunk *pChu
1de0b 6e 6b 3b 0a 0a 20 20 2f 2a 20 53 51 4c 69 74 65  nk;..  /* SQLite
1de0c 20 6e 65 76 65 72 20 74 72 69 65 73 20 74 6f 20   never tries to 
1de0d 72 65 61 64 20 70 61 73 74 20 74 68 65 20 65 6e  read past the en
1de0e 64 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20  d of a rollback 
1de0f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
1de10 20 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 2b    assert( iOfst+
1de11 69 41 6d 74 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e  iAmt<=p->endpoin
1de12 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 0a 20 20  t.iOffset );..  
1de13 69 66 28 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74  if( p->readpoint
1de14 2e 69 4f 66 66 73 65 74 21 3d 69 4f 66 73 74 20  .iOffset!=iOfst 
1de15 7c 7c 20 69 4f 66 73 74 3d 3d 30 20 29 7b 0a 20  || iOfst==0 ){. 
1de16 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
1de17 20 69 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 66   iOff = 0;.    f
1de18 6f 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 46 69  or(pChunk=p->pFi
1de19 72 73 74 3b 20 0a 20 20 20 20 20 20 20 20 41 4c  rst; .        AL
1de1a 57 41 59 53 28 70 43 68 75 6e 6b 29 20 26 26 20  WAYS(pChunk) && 
1de1b 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48  (iOff+JOURNAL_CH
1de1c 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b  UNKSIZE)<=iOfst;
1de1d 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d  .        pChunk=
1de1e 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20  pChunk->pNext.  
1de1f 20 20 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20    ){.      iOff 
1de20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  += JOURNAL_CHUNK
1de21 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  SIZE;.    }.  }e
1de22 6c 73 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20  lse{.    pChunk 
1de23 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70  = p->readpoint.p
1de24 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43  Chunk;.  }..  iC
1de25 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e  hunkOffset = (in
1de26 74 29 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c  t)(iOfst%JOURNAL
1de27 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64  _CHUNKSIZE);.  d
1de28 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61  o {.    int iSpa
1de29 63 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55  ce = JOURNAL_CHU
1de2a 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f  NKSIZE - iChunkO
1de2b 66 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e  ffset;.    int n
1de2c 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64  Copy = MIN(nRead
1de2d 2c 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  , (JOURNAL_CHUNK
1de2e 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66  SIZE - iChunkOff
1de2f 73 65 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70  set));.    memcp
1de30 79 28 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d  y(zOut, &pChunk-
1de31 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66  >zChunk[iChunkOf
1de32 66 73 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20  fset], nCopy);. 
1de33 20 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79     zOut += nCopy
1de34 3b 0a 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69  ;.    nRead -= i
1de35 53 70 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e  Space;.    iChun
1de36 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d  kOffset = 0;.  }
1de37 20 77 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30   while( nRead>=0
1de38 20 26 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75   && (pChunk=pChu
1de39 6e 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26  nk->pNext)!=0 &&
1de3a 20 6e 52 65 61 64 3e 30 20 29 3b 0a 20 20 70 2d   nRead>0 );.  p-
1de3b 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73  >readpoint.iOffs
1de3c 65 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b  et = iOfst+iAmt;
1de3d 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e  .  p->readpoint.
1de3e 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b  pChunk = pChunk;
1de3f 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
1de40 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  E_OK;.}../*.** W
1de41 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65  rite data to the
1de42 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1de43 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 72 69 74   int memjrnlWrit
1de44 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  e(.  sqlite3_fil
1de45 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54  e *pJfd,    /* T
1de46 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1de47 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  into which to wr
1de48 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ite */.  const v
1de49 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20  oid *zBuf,      
1de4a 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20  /* Take data to 
1de4b 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20  be written from 
1de4c 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41  here */.  int iA
1de4d 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
1de4e 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1de4f 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
1de50 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
1de51 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69  Ofst     /* Begi
1de52 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69  n writing at thi
1de53 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68  s offset into th
1de54 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d  e file */.){.  M
1de55 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28  emJournal *p = (
1de56 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66  MemJournal *)pJf
1de57 64 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20  d;.  int nWrite 
1de58 3d 20 69 41 6d 74 3b 0a 20 20 75 38 20 2a 7a 57  = iAmt;.  u8 *zW
1de59 72 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75  rite = (u8 *)zBu
1de5a 66 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d  f;..  /* An in-m
1de5b 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69  emory journal fi
1de5c 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 65  le should only e
1de5d 76 65 72 20 62 65 20 61 70 70 65 6e 64 65 64 20  ver be appended 
1de5e 74 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20  to. Random.  ** 
1de5f 61 63 63 65 73 73 20 77 72 69 74 65 73 20 61 72  access writes ar
1de60 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 62  e not required b
1de61 79 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20  y sqlite..  */. 
1de62 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 3d 3d   assert( iOfst==
1de63 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66  p->endpoint.iOff
1de64 73 65 74 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  set );.  UNUSED_
1de65 50 41 52 41 4d 45 54 45 52 28 69 4f 66 73 74 29  PARAMETER(iOfst)
1de66 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 57 72 69  ;..  while( nWri
1de67 74 65 3e 30 20 29 7b 0a 20 20 20 20 46 69 6c 65  te>0 ){.    File
1de68 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 20 3d 20  Chunk *pChunk = 
1de69 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75  p->endpoint.pChu
1de6a 6e 6b 3b 0a 20 20 20 20 69 6e 74 20 69 43 68 75  nk;.    int iChu
1de6b 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29  nkOffset = (int)
1de6c 28 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66  (p->endpoint.iOf
1de6d 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55  fset%JOURNAL_CHU
1de6e 4e 4b 53 49 5a 45 29 3b 0a 20 20 20 20 69 6e 74  NKSIZE);.    int
1de6f 20 69 53 70 61 63 65 20 3d 20 4d 49 4e 28 6e 57   iSpace = MIN(nW
1de70 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48  rite, JOURNAL_CH
1de71 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b  UNKSIZE - iChunk
1de72 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 69 66  Offset);..    if
1de73 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d  ( iChunkOffset==
1de74 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 65  0 ){.      /* Ne
1de75 77 20 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69  w chunk is requi
1de76 72 65 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68  red to extend th
1de77 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20  e file. */.     
1de78 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77   FileChunk *pNew
1de79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
1de7a 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75  c(sizeof(FileChu
1de7b 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nk));.      if( 
1de7c 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20  !pNew ){.       
1de7d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
1de7e 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  OERR_NOMEM;.    
1de7f 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e    }.      pNew->
1de80 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20  pNext = 0;.     
1de81 20 69 66 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20   if( pChunk ){. 
1de82 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1de83 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20  ->pFirst );.    
1de84 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78      pChunk->pNex
1de85 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20  t = pNew;.      
1de86 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
1de87 73 73 65 72 74 28 20 21 70 2d 3e 70 46 69 72 73  ssert( !p->pFirs
1de88 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  t );.        p->
1de89 70 46 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20  pFirst = pNew;. 
1de8a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e       }.      p->
1de8b 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20  endpoint.pChunk 
1de8c 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 0a 20  = pNew;.    }.. 
1de8d 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e     memcpy(&p->en
1de8e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a  dpoint.pChunk->z
1de8f 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73  Chunk[iChunkOffs
1de90 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70  et], zWrite, iSp
1de91 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 69 74 65  ace);.    zWrite
1de92 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20   += iSpace;.    
1de93 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65  nWrite -= iSpace
1de94 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e  ;.    p->endpoin
1de95 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 53 70  t.iOffset += iSp
1de96 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ace;.  }..  retu
1de97 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1de98 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
1de99 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1de9a 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54  tic int memjrnlT
1de9b 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f  runcate(sqlite3_
1de9c 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69  file *pJfd, sqli
1de9d 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a  te_int64 size){.
1de9e 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20    MemJournal *p 
1de9f 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29  = (MemJournal *)
1dea0 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68 75 6e  pJfd;.  FileChun
1dea1 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 73 73  k *pChunk;.  ass
1dea2 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a 20 20  ert(size==0);.  
1dea3 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1dea4 28 73 69 7a 65 29 3b 0a 20 20 70 43 68 75 6e 6b  (size);.  pChunk
1dea5 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0a 20 20   = p->pFirst;.  
1dea6 77 68 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b  while( pChunk ){
1dea7 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a  .    FileChunk *
1dea8 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0a 20  pTmp = pChunk;. 
1dea9 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 43 68 75     pChunk = pChu
1deaa 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73  nk->pNext;.    s
1deab 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70  qlite3_free(pTmp
1deac 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1dead 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70  MemJournalOpen(p
1deae 4a 66 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Jfd);.  return S
1deaf 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1deb0 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c  ** Close the fil
1deb1 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1deb2 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 73 71   memjrnlClose(sq
1deb3 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1deb4 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e  ){.  memjrnlTrun
1deb5 63 61 74 65 28 70 4a 66 64 2c 20 30 29 3b 0a 20  cate(pJfd, 0);. 
1deb6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1deb7 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e  K;.}.../*.** Syn
1deb8 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  c the file..**.*
1deb9 2a 20 53 79 6e 63 69 6e 67 20 61 6e 20 69 6e 2d  * Syncing an in-
1deba 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 69  memory journal i
1debb 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c  s a no-op.  And,
1debc 20 69 6e 20 66 61 63 74 2c 20 74 68 69 73 20 72   in fact, this r
1debd 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6e 65 76  outine.** is nev
1debe 65 72 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 77  er called in a w
1debf 6f 72 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74  orking implement
1dec0 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6d 70  ation.  This imp
1dec1 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65  lementation.** e
1dec2 78 69 73 74 73 20 70 75 72 65 6c 79 20 61 73 20  xists purely as 
1dec3 61 20 63 6f 6e 74 69 6e 67 65 6e 63 79 2c 20 69  a contingency, i
1dec4 6e 20 63 61 73 65 20 73 6f 6d 65 20 6d 61 6c 66  n case some malf
1dec5 75 6e 63 74 69 6f 6e 20 69 6e 20 73 6f 6d 65 20  unction in some 
1dec6 6f 74 68 65 72 0a 2a 2a 20 70 61 72 74 20 6f 66  other.** part of
1dec7 20 53 51 4c 69 74 65 20 63 61 75 73 65 73 20 53   SQLite causes S
1dec8 79 6e 63 20 74 6f 20 62 65 20 63 61 6c 6c 65 64  ync to be called
1dec9 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a   by mistake..*/.
1deca 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72  static int memjr
1decb 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  nlSync(sqlite3_f
1decc 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  ile *NotUsed, in
1decd 74 20 4e 6f 74 55 73 65 64 32 29 7b 20 20 20 2f  t NotUsed2){   /
1dece 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 55 4e 55  *NO_TEST*/.  UNU
1decf 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
1ded0 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
1ded1 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
1ded2 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54            /*NO_T
1ded3 45 53 54 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  EST*/.  assert( 
1ded4 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  0 );            
1ded5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ded6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ded7 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f       /*NO_TEST*/
1ded8 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1ded9 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
1deda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dedb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dedc 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 7d 20 20 20  /*NO_TEST*/.}   
1dedd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dede 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dedf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dee0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
1dee1 54 45 53 54 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75  TEST*/../*.** Qu
1dee2 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ery the size of 
1dee3 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65  the file in byte
1dee4 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
1dee5 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65   memjrnlFileSize
1dee6 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1dee7 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  Jfd, sqlite_int6
1dee8 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d  4 *pSize){.  Mem
1dee9 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65  Journal *p = (Me
1deea 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b  mJournal *)pJfd;
1deeb 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c  .  *pSize = (sql
1deec 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e  ite_int64) p->en
1deed 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a  dpoint.iOffset;.
1deee 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1deef 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62  OK;.}../*.** Tab
1def0 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f  le of methods fo
1def1 72 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c  r MemJournal sql
1def2 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74  ite3_file object
1def3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
1def4 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  ct sqlite3_io_me
1def5 74 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c  thods MemJournal
1def6 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c  Methods = {.  1,
1def7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1def8 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
1def9 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20   memjrnlClose,  
1defa 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a     /* xClose */.
1defb 20 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 20    memjrnlRead,  
1defc 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a      /* xRead */.
1defd 20 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 2c 20    memjrnlWrite, 
1defe 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f      /* xWrite */
1deff 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61  .  memjrnlTrunca
1df00 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74  te,  /* xTruncat
1df01 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79  e */.  memjrnlSy
1df02 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e  nc,      /* xSyn
1df03 63 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69  c */.  memjrnlFi
1df04 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c  leSize,  /* xFil
1df05 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20  eSize */.  0,   
1df06 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1df07 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20  xLock */.  0,   
1df08 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1df09 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  xUnlock */.  0, 
1df0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1df0b 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
1df0c 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Lock */.  0,    
1df0d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1df0e 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20  FileControl */. 
1df0f 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1df10 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a     /* xSectorSiz
1df11 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20  e */.  0        
1df12 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76           /* xDev
1df13 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
1df14 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a  cs */.};../* .**
1df15 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20   Open a journal 
1df16 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  file..*/.SQLITE_
1df17 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1df18 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70  ite3MemJournalOp
1df19 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  en(sqlite3_file 
1df1a 2a 70 4a 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75  *pJfd){.  MemJou
1df1b 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f  rnal *p = (MemJo
1df1c 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20  urnal *)pJfd;.  
1df1d 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
1df1e 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20  TE_ALIGNMENT(p) 
1df1f 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  );.  memset(p, 0
1df20 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72  , sqlite3MemJour
1df21 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 2d  nalSize());.  p-
1df22 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d 4a  >pMethod = &MemJ
1df23 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d  ournalMethods;.}
1df24 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1df25 72 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 2d  rue if the file-
1df26 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73  handle passed as
1df27 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20   an argument is 
1df28 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  .** an in-memory
1df29 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c   journal .*/.SQL
1df2a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1df2b 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72  sqlite3IsMemJour
1df2c 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  nal(sqlite3_file
1df2d 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 72   *pJfd){.  retur
1df2e 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73  n pJfd->pMethods
1df2f 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74  ==&MemJournalMet
1df30 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  hods;.}../* .** 
1df31 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1df32 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69  r of bytes requi
1df33 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4d  red to store a M
1df34 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 75  emJournal that u
1df35 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20  ses vfs.** pVfs 
1df36 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e  to create the un
1df37 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b  derlying on-disk
1df38 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
1df39 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1df3a 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53  lite3MemJournalS
1df3b 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74  ize(void){.  ret
1df3c 75 72 6e 20 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f  urn sizeof(MemJo
1df3d 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  urnal);.}../****
1df3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1df3f 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a  f memjournal.c *
1df40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1df43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1df44 20 66 69 6c 65 20 77 61 6c 6b 65 72 2e 63 20 2a   file walker.c *
1df45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1df48 20 32 30 30 38 20 41 75 67 75 73 74 20 31 36 0a   2008 August 16.
1df49 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1df4a 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1df4b 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1df4c 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1df4d 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1df4e 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1df4f 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1df50 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1df51 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1df52 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1df53 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1df54 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1df55 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1df56 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1df57 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1df58 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1df59 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1df5a 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1df5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1df5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1df5f 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1df60 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73  ains routines us
1df61 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 74  ed for walking t
1df62 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20 66  he parser tree f
1df63 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 73 74 61  or.** an SQL sta
1df64 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49  tement..**.** $I
1df65 64 3a 20 77 61 6c 6b 65 72 2e 63 2c 76 20 31 2e  d: walker.c,v 1.
1df66 34 20 32 30 30 39 2f 30 34 2f 30 38 20 31 33 3a  4 2009/04/08 13:
1df67 35 31 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a  51:52 drh Exp $.
1df68 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20  */.../*.** Walk 
1df69 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
1df6a 65 65 2e 20 20 49 6e 76 6f 6b 65 20 74 68 65 20  ee.  Invoke the 
1df6b 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f  callback once fo
1df6c 72 20 65 61 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f  r each node.** o
1df6d 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1df6e 2c 20 77 68 69 6c 65 20 64 65 63 65 6e 64 69 6e  , while decendin
1df6f 67 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f  g.  (In other wo
1df70 72 64 73 2c 20 74 68 65 20 63 61 6c 6c 62 61 63  rds, the callbac
1df71 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20  k.** is invoked 
1df72 62 65 66 6f 72 65 20 76 69 73 69 74 69 6e 67 20  before visiting 
1df73 63 68 69 6c 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a  children.).**.**
1df74 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
1df75 65 20 66 72 6f 6d 20 74 68 65 20 63 61 6c 6c 62  e from the callb
1df76 61 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e  ack should be on
1df77 65 20 6f 66 20 74 68 65 20 57 52 43 5f 2a 0a 2a  e of the WRC_*.*
1df78 2a 20 63 6f 6e 73 74 61 6e 74 73 20 74 6f 20 73  * constants to s
1df79 70 65 63 69 66 79 20 68 6f 77 20 74 6f 20 70 72  pecify how to pr
1df7a 6f 63 65 65 64 20 77 69 74 68 20 74 68 65 20 77  oceed with the w
1df7b 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52  alk..**.**    WR
1df7c 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 20 20  C_Continue      
1df7d 43 6f 6e 74 69 6e 75 65 20 64 65 73 63 65 6e 64  Continue descend
1df7e 69 6e 67 20 64 6f 77 6e 20 74 68 65 20 74 72 65  ing down the tre
1df7f 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f  e..**.**    WRC_
1df80 50 72 75 6e 65 20 20 20 20 20 20 20 20 20 44 6f  Prune         Do
1df81 20 6e 6f 74 20 64 65 73 63 65 6e 64 20 69 6e 74   not descend int
1df82 6f 20 63 68 69 6c 64 20 6e 6f 64 65 73 2e 20 20  o child nodes.  
1df83 42 75 74 20 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20  But allow.**    
1df84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1df85 20 20 74 68 65 20 77 61 6c 6b 20 74 6f 20 63 6f    the walk to co
1df86 6e 74 69 6e 75 65 20 77 69 74 68 20 73 69 62 6c  ntinue with sibl
1df87 69 6e 67 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a  ing nodes..**.**
1df88 20 20 20 20 57 52 43 5f 41 62 6f 72 74 20 20 20      WRC_Abort   
1df89 20 20 20 20 20 20 44 6f 20 6e 6f 20 6d 6f 72 65        Do no more
1df8a 20 63 61 6c 6c 62 61 63 6b 73 2e 20 20 55 6e 77   callbacks.  Unw
1df8b 69 6e 64 20 74 68 65 20 73 74 61 63 6b 20 61 6e  ind the stack an
1df8c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
1df8d 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
1df8e 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 77   the top-level w
1df8f 61 6c 6b 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  alk call..**.** 
1df90 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
1df91 20 66 72 6f 6d 20 74 68 69 73 20 72 6f 75 74 69   from this routi
1df92 6e 65 20 69 73 20 57 52 43 5f 41 62 6f 72 74 20  ne is WRC_Abort 
1df93 74 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 74  to abandon the t
1df94 72 65 65 20 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20  ree walk.** and 
1df95 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 74 6f 20  WRC_Continue to 
1df96 63 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c  continue..*/.SQL
1df97 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1df98 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1df99 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
1df9a 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
1df9b 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70   int rc;.  if( p
1df9c 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  Expr==0 ) return
1df9d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20   WRC_Continue;. 
1df9e 20 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48   testcase( ExprH
1df9f 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
1dfa0 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20  , EP_TokenOnly) 
1dfa1 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 45  );.  testcase( E
1dfa2 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
1dfa3 45 78 70 72 2c 20 45 50 5f 53 70 61 6e 54 6f 6b  Expr, EP_SpanTok
1dfa4 65 6e 29 20 29 3b 0a 20 20 74 65 73 74 63 61 73  en) );.  testcas
1dfa5 65 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  e( ExprHasProper
1dfa6 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 64  ty(pExpr, EP_Red
1dfa7 75 63 65 64 29 20 29 3b 0a 20 20 72 63 20 3d 20  uced) );.  rc = 
1dfa8 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72 43 61  pWalker->xExprCa
1dfa9 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20  llback(pWalker, 
1dfaa 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 72 63  pExpr);.  if( rc
1dfab 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65 0a 20  ==WRC_Continue. 
1dfac 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
1dfad 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
1dfae 72 74 79 28 70 45 78 70 72 2c 45 50 5f 54 6f 6b  rty(pExpr,EP_Tok
1dfaf 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 6f  enOnly|EP_SpanTo
1dfb0 6b 65 6e 29 20 29 7b 0a 20 20 20 20 69 66 28 20  ken) ){.    if( 
1dfb1 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1dfb2 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e  pWalker, pExpr->
1dfb3 70 4c 65 66 74 29 20 29 20 72 65 74 75 72 6e 20  pLeft) ) return 
1dfb4 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69  WRC_Abort;.    i
1dfb5 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  f( sqlite3WalkEx
1dfb6 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70  pr(pWalker, pExp
1dfb7 72 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65 74  r->pRight) ) ret
1dfb8 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1dfb9 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
1dfba 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1dfbb 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
1dfbc 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1dfbd 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b  WalkSelect(pWalk
1dfbe 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  er, pExpr->x.pSe
1dfbf 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 57  lect) ) return W
1dfc0 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 65  RC_Abort;.    }e
1dfc1 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 73  lse{.      if( s
1dfc2 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69  qlite3WalkExprLi
1dfc3 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70  st(pWalker, pExp
1dfc4 72 2d 3e 78 2e 70 4c 69 73 74 29 20 29 20 72 65  r->x.pList) ) re
1dfc5 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1dfc6 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1dfc7 72 6e 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72  rn rc & WRC_Abor
1dfc8 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  t;.}../*.** Call
1dfc9 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
1dfca 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70  () for every exp
1dfcb 72 65 73 73 69 6f 6e 20 69 6e 20 6c 69 73 74 20  ression in list 
1dfcc 70 20 6f 72 20 75 6e 74 69 6c 0a 2a 2a 20 61 6e  p or until.** an
1dfcd 20 61 62 6f 72 74 20 72 65 71 75 65 73 74 20 69   abort request i
1dfce 73 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  s seen..*/.SQLIT
1dfcf 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1dfd0 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1dfd1 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  t(Walker *pWalke
1dfd2 72 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b  r, ExprList *p){
1dfd3 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 57  .  int i, rc = W
1dfd4 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 73  RC_Continue;.  s
1dfd5 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
1dfd6 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66  tem *pItem;.  if
1dfd7 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ( p ){.    for(i
1dfd8 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d  =p->nExpr, pItem
1dfd9 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c  =p->a; i>0; i--,
1dfda 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1dfdb 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1dfdc 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 49  Expr(pWalker, pI
1dfdd 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 72 65  tem->pExpr) ) re
1dfde 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1dfdf 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1dfe0 72 6e 20 72 63 20 26 20 57 52 43 5f 43 6f 6e 74  rn rc & WRC_Cont
1dfe1 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  inue;.}../*.** W
1dfe2 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69  alk all expressi
1dfe3 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ons associated w
1dfe4 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65  ith SELECT state
1dfe5 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e  ment p.  Do.** n
1dfe6 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45  ot invoke the SE
1dfe7 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e  LECT callback on
1dfe8 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63   p, but do (of c
1dfe9 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a  ourse) invoke.**
1dfea 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61   any expr callba
1dfeb 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63  cks and SELECT c
1dfec 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f  allbacks that co
1dfed 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69  me from subqueri
1dfee 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52  es..** Return WR
1dfef 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43  C_Abort or WRC_C
1dff0 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49  ontinue..*/.SQLI
1dff1 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1dff2 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1dff3 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  Expr(Walker *pWa
1dff4 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
1dff5 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  {.  if( sqlite3W
1dff6 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1dff7 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20  ker, p->pEList) 
1dff8 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1dff9 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1dffa 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1dffb 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20  r, p->pWhere) ) 
1dffc 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1dffd 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ;.  if( sqlite3W
1dffe 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1dfff 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79  ker, p->pGroupBy
1e000 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1e001 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
1e002 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1e003 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29  ker, p->pHaving)
1e004 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1e005 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ort;.  if( sqlit
1e006 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
1e007 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65  Walker, p->pOrde
1e008 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52  rBy) ) return WR
1e009 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73  C_Abort;.  if( s
1e00a 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1e00b 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69  Walker, p->pLimi
1e00c 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
1e00d 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
1e00e 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1e00f 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74  lker, p->pOffset
1e010 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1e011 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57  bort;.  return W
1e012 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a  RC_Continue;.}..
1e013 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70  /*.** Walk the p
1e014 61 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63  arse trees assoc
1e015 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73  iated with all s
1e016 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65  ubqueries in the
1e017 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  .** FROM clause 
1e018 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  of SELECT statem
1e019 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69  ent p.  Do not i
1e01a 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74  nvoke the select
1e01b 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20  .** callback on 
1e01c 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65  p, but do invoke
1e01d 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d   it on each FROM
1e01e 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79   clause subquery
1e01f 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73  .** and on any s
1e020 75 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65  ubqueries furthe
1e021 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72  r down in the tr
1e022 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20  ee.  Return .** 
1e023 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43  WRC_Abort or WRC
1e024 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51  _Continue;.*/.SQ
1e025 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1e026 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
1e027 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70  ctFrom(Walker *p
1e028 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
1e029 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  p){.  SrcList *p
1e02a 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Src;.  int i;.  
1e02b 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
1e02c 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70  tem *pItem;..  p
1e02d 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  Src = p->pSrc;. 
1e02e 20 69 66 28 20 70 53 72 63 20 29 7b 0a 20 20 20   if( pSrc ){.   
1e02f 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72   for(i=pSrc->nSr
1e030 63 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61  c, pItem=pSrc->a
1e031 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
1e032 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  m++){.      if( 
1e033 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1e034 74 28 70 57 61 6c 6b 65 72 2c 20 70 49 74 65 6d  t(pWalker, pItem
1e035 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  ->pSelect) ){.  
1e036 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1e037 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a  _Abort;.      }.
1e038 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1e039 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
1e03a 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  .} ../*.** Call 
1e03b 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1e03c 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 72  ) for every expr
1e03d 65 73 73 69 6f 6e 20 69 6e 20 53 65 6c 65 63 74  ession in Select
1e03e 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 2a 2a   statement p..**
1e03f 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 57   Invoke sqlite3W
1e040 61 6c 6b 53 65 6c 65 63 74 28 29 20 66 6f 72 20  alkSelect() for 
1e041 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68  subqueries in th
1e042 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e  e FROM clause an
1e043 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70  d.** on the comp
1e044 6f 75 6e 64 20 73 65 6c 65 63 74 20 63 68 61 69  ound select chai
1e045 6e 2c 20 70 2d 3e 70 50 72 69 6f 72 2e 0a 2a 2a  n, p->pPrior..**
1e046 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 43  .** Return WRC_C
1e047 6f 6e 74 69 6e 75 65 20 75 6e 64 65 72 20 6e 6f  ontinue under no
1e048 72 6d 61 6c 20 63 6f 6e 64 69 74 69 6f 6e 73 2e  rmal conditions.
1e049 20 20 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    Return WRC_Abo
1e04a 72 74 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 69  rt if.** there i
1e04b 73 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75 65  s an abort reque
1e04c 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  st..**.** If the
1e04d 20 57 61 6c 6b 65 72 20 64 6f 65 73 20 6e 6f 74   Walker does not
1e04e 20 68 61 76 65 20 61 6e 20 78 53 65 6c 65 63 74   have an xSelect
1e04f 43 61 6c 6c 62 61 63 6b 28 29 20 74 68 65 6e 20  Callback() then 
1e050 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1e051 69 73 20 61 20 6e 6f 2d 6f 70 20 72 65 74 75 72  is a no-op retur
1e052 6e 69 6e 67 20 57 52 43 5f 43 6f 6e 74 69 6e 75  ning WRC_Continu
1e053 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1e054 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1e055 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65  WalkSelect(Walke
1e056 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
1e057 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  ct *p){.  int rc
1e058 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
1e059 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74  pWalker->xSelect
1e05a 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65  Callback==0 ) re
1e05b 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1e05c 65 3b 0a 20 20 72 63 20 3d 20 57 52 43 5f 43 6f  e;.  rc = WRC_Co
1e05d 6e 74 69 6e 75 65 3b 0a 20 20 77 68 69 6c 65 28  ntinue;.  while(
1e05e 20 70 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20   p  ){.    rc = 
1e05f 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74  pWalker->xSelect
1e060 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72  Callback(pWalker
1e061 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , p);.    if( rc
1e062 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66   ) break;.    if
1e063 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  ( sqlite3WalkSel
1e064 65 63 74 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  ectExpr(pWalker,
1e065 20 70 29 20 29 20 72 65 74 75 72 6e 20 57 52 43   p) ) return WRC
1e066 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20  _Abort;.    if( 
1e067 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1e068 74 46 72 6f 6d 28 70 57 61 6c 6b 65 72 2c 20 70  tFrom(pWalker, p
1e069 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1e06a 62 6f 72 74 3b 0a 20 20 20 20 70 20 3d 20 70 2d  bort;.    p = p-
1e06b 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72  >pPrior;.  }.  r
1e06c 65 74 75 72 6e 20 72 63 20 26 20 57 52 43 5f 41  eturn rc & WRC_A
1e06d 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  bort;.}../******
1e06e 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1e06f 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  walker.c *******
1e070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e071 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e072 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1e073 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1e074 69 6c 65 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a  ile resolve.c **
1e075 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e076 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e077 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1e078 30 30 38 20 41 75 67 75 73 74 20 31 38 0a 2a 2a  008 August 18.**
1e079 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1e07a 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1e07b 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1e07c 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1e07d 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1e07e 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1e07f 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1e080 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1e081 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1e082 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1e083 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1e084 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1e085 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1e086 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1e087 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1e088 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1e089 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1e08a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1e08b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e08c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e08d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e08e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
1e08f 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1e090 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75  tains routines u
1e091 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20  sed for walking 
1e092 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20  the parser tree 
1e093 61 6e 64 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 61  and.** resolve a
1e094 6c 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20 62  ll identifiers b
1e095 79 20 61 73 73 6f 63 69 61 74 69 6e 67 20 74 68  y associating th
1e096 65 6d 20 77 69 74 68 20 61 20 70 61 72 74 69 63  em with a partic
1e097 75 6c 61 72 0a 2a 2a 20 74 61 62 6c 65 20 61 6e  ular.** table an
1e098 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  d column..**.** 
1e099 24 49 64 3a 20 72 65 73 6f 6c 76 65 2e 63 2c 76  $Id: resolve.c,v
1e09a 20 31 2e 32 32 20 32 30 30 39 2f 30 35 2f 30 35   1.22 2009/05/05
1e09b 20 31 35 3a 34 36 3a 34 33 20 64 72 68 20 45 78   15:46:43 drh Ex
1e09c 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75  p $.*/../*.** Tu
1e09d 72 6e 20 74 68 65 20 70 45 78 70 72 20 65 78 70  rn the pExpr exp
1e09e 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20  ression into an 
1e09f 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 69 43  alias for the iC
1e0a0 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20  ol-th column of 
1e0a1 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  the.** result se
1e0a2 74 20 69 6e 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a  t in pEList..**.
1e0a3 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74  ** If the result
1e0a4 20 73 65 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61   set column is a
1e0a5 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72   simple column r
1e0a6 65 66 65 72 65 6e 63 65 2c 20 74 68 65 6e 20 74  eference, then t
1e0a7 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d  his routine.** m
1e0a8 61 6b 65 73 20 61 6e 20 65 78 61 63 74 20 63 6f  akes an exact co
1e0a9 70 79 2e 20 20 42 75 74 20 66 6f 72 20 61 6e 79  py.  But for any
1e0aa 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 65   other kind of e
1e0ab 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 69 73 0a  xpression, this.
1e0ac 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20  ** routine make 
1e0ad 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65  a copy of the re
1e0ae 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20  sult set column 
1e0af 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  as the argument 
1e0b0 74 6f 20 74 68 65 0a 2a 2a 20 54 4b 5f 41 53 20  to the.** TK_AS 
1e0b1 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 54  operator.  The T
1e0b2 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72 20 63 61  K_AS operator ca
1e0b3 75 73 65 73 20 74 68 65 20 65 78 70 72 65 73 73  uses the express
1e0b4 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 76 61  ion to be.** eva
1e0b5 6c 75 61 74 65 64 20 6a 75 73 74 20 6f 6e 63 65  luated just once
1e0b6 20 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64   and then reused
1e0b7 20 66 6f 72 20 65 61 63 68 20 61 6c 69 61 73 2e   for each alias.
1e0b8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 61 73 6f  .**.** The reaso
1e0b9 6e 20 66 6f 72 20 73 75 70 70 72 65 73 73 69 6e  n for suppressin
1e0ba 67 20 74 68 65 20 54 4b 5f 41 53 20 74 65 72 6d  g the TK_AS term
1e0bb 20 77 68 65 6e 20 74 68 65 20 65 78 70 72 65 73   when the expres
1e0bc 73 69 6f 6e 20 69 73 20 61 20 73 69 6d 70 6c 65  sion is a simple
1e0bd 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  .** column refer
1e0be 65 6e 63 65 20 69 73 20 73 6f 20 74 68 61 74 20  ence is so that 
1e0bf 74 68 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  the column refer
1e0c0 65 6e 63 65 20 77 69 6c 6c 20 62 65 20 72 65 63  ence will be rec
1e0c1 6f 67 6e 69 7a 65 64 20 61 73 0a 2a 2a 20 75 73  ognized as.** us
1e0c2 61 62 6c 65 20 62 79 20 69 6e 64 69 63 65 73 20  able by indices 
1e0c3 77 69 74 68 69 6e 20 74 68 65 20 57 48 45 52 45  within the WHERE
1e0c4 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69   clause processi
1e0c5 6e 67 20 6c 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a  ng logic. .**.**
1e0c6 20 48 61 63 6b 3a 20 20 54 68 65 20 54 4b 5f 41   Hack:  The TK_A
1e0c7 53 20 6f 70 65 72 61 74 6f 72 20 69 73 20 69 6e  S operator is in
1e0c8 68 69 62 69 74 65 64 20 69 66 20 7a 54 79 70 65  hibited if zType
1e0c9 5b 30 5d 3d 3d 27 47 27 2e 20 20 54 68 69 73 20  [0]=='G'.  This 
1e0ca 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 6e  means.** that in
1e0cb 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75   a GROUP BY clau
1e0cc 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69  se, the expressi
1e0cd 6f 6e 20 69 73 20 65 76 61 6c 75 61 74 65 64 20  on is evaluated 
1e0ce 74 77 69 63 65 2e 20 20 48 65 6e 63 65 3a 0a 2a  twice.  Hence:.*
1e0cf 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
1e0d0 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c  random()%5 AS x,
1e0d1 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74   count(*) FROM t
1e0d2 61 62 20 47 52 4f 55 50 20 42 59 20 78 0a 2a 2a  ab GROUP BY x.**
1e0d3 0a 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e  .** Is equivalen
1e0d4 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  t to:.**.**     
1e0d5 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29 25  SELECT random()%
1e0d6 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29  5 AS x, count(*)
1e0d7 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50 20   FROM tab GROUP 
1e0d8 42 59 20 72 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a  BY random()%5.**
1e0d9 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f  .** The result o
1e0da 66 20 72 61 6e 64 6f 6d 28 29 25 35 20 69 6e 20  f random()%5 in 
1e0db 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  the GROUP BY cla
1e0dc 75 73 65 20 69 73 20 70 72 6f 62 61 62 6c 79 20  use is probably 
1e0dd 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f  different.** fro
1e0de 6d 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  m the result in 
1e0df 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 2e 20  the result-set. 
1e0e0 20 57 65 20 6d 69 67 68 74 20 66 69 78 20 74 68   We might fix th
1e0e1 69 73 20 73 6f 6d 65 64 61 79 2e 20 20 4f 72 0a  is someday.  Or.
1e0e2 2a 2a 20 74 68 65 6e 20 61 67 61 69 6e 2c 20 77  ** then again, w
1e0e3 65 20 6d 69 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a  e might not....*
1e0e4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
1e0e5 73 6f 6c 76 65 41 6c 69 61 73 28 0a 20 20 50 61  solveAlias(.  Pa
1e0e6 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1e0e7 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
1e0e8 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
1e0e9 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
1e0ea 20 20 20 20 2f 2a 20 41 20 72 65 73 75 6c 74 20      /* A result 
1e0eb 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  set */.  int iCo
1e0ec 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
1e0ed 2f 2a 20 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  /* A column in t
1e0ee 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20  he result set.  
1e0ef 30 2e 2e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  0..pEList->nExpr
1e0f0 2d 31 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  -1 */.  Expr *pE
1e0f1 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  xpr,           /
1e0f2 2a 20 54 72 61 6e 73 66 6f 72 6d 20 74 68 69 73  * Transform this
1e0f3 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 74   into an alias t
1e0f4 6f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  o the result set
1e0f5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1e0f6 20 2a 7a 54 79 70 65 20 20 20 20 20 20 2f 2a 20   *zType      /* 
1e0f7 22 47 52 4f 55 50 22 20 6f 72 20 22 4f 52 44 45  "GROUP" or "ORDE
1e0f8 52 22 20 6f 72 20 22 22 20 2a 2f 0a 29 7b 0a 20  R" or "" */.){. 
1e0f9 20 45 78 70 72 20 2a 70 4f 72 69 67 3b 20 20 20   Expr *pOrig;   
1e0fa 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
1e0fb 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  Col-th column of
1e0fc 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1e0fd 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 75 70 3b  */.  Expr *pDup;
1e0fe 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1e0ff 6f 70 79 20 6f 66 20 70 4f 72 69 67 20 2a 2f 0a  opy of pOrig */.
1e100 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
1e101 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1e102 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1e103 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ion */..  assert
1e104 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f  ( iCol>=0 && iCo
1e105 6c 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  l<pEList->nExpr 
1e106 29 3b 0a 20 20 70 4f 72 69 67 20 3d 20 70 45 4c  );.  pOrig = pEL
1e107 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78  ist->a[iCol].pEx
1e108 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  pr;.  assert( pO
1e109 72 69 67 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  rig!=0 );.  asse
1e10a 72 74 28 20 70 4f 72 69 67 2d 3e 66 6c 61 67 73  rt( pOrig->flags
1e10b 20 26 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 29   & EP_Resolved )
1e10c 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
1e10d 3e 64 62 3b 0a 20 20 70 44 75 70 20 3d 20 73 71  >db;.  pDup = sq
1e10e 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
1e10f 20 70 4f 72 69 67 2c 20 30 29 3b 0a 20 20 69 66   pOrig, 0);.  if
1e110 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74 75  ( pDup==0 ) retu
1e111 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 54 6f 6b  rn;.  sqlite3Tok
1e112 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 44 75 70  enCopy(db, &pDup
1e113 2d 3e 74 6f 6b 65 6e 2c 20 26 70 4f 72 69 67 2d  ->token, &pOrig-
1e114 3e 74 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 70  >token);.  if( p
1e115 44 75 70 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  Dup->op!=TK_COLU
1e116 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d 21 3d  MN && zType[0]!=
1e117 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75 70 20  'G' ){.    pDup 
1e118 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
1e119 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 44  Parse, TK_AS, pD
1e11a 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69  up, 0, 0);.    i
1e11b 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74  f( pDup==0 ) ret
1e11c 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 4c  urn;.    if( pEL
1e11d 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c  ist->a[iCol].iAl
1e11e 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ias==0 ){.      
1e11f 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e  pEList->a[iCol].
1e120 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 28 2b  iAlias = (u16)(+
1e121 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 29  +pParse->nAlias)
1e122 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 75 70  ;.    }.    pDup
1e123 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c 69 73  ->iTable = pELis
1e124 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61  t->a[iCol].iAlia
1e125 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 78  s;.  }.  if( pEx
1e126 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  pr->flags & EP_E
1e127 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20  xpCollate ){.   
1e128 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d 20 70   pDup->pColl = p
1e129 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  Expr->pColl;.   
1e12a 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c 3d 20   pDup->flags |= 
1e12b 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20  EP_ExpCollate;. 
1e12c 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
1e12d 43 6c 65 61 72 28 64 62 2c 20 70 45 78 70 72 29  Clear(db, pExpr)
1e12e 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 78 70 72  ;.  memcpy(pExpr
1e12f 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66 28 2a  , pDup, sizeof(*
1e130 70 45 78 70 72 29 29 3b 0a 20 20 73 71 6c 69 74  pExpr));.  sqlit
1e131 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 75  e3DbFree(db, pDu
1e132 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76  p);.}../*.** Giv
1e133 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  en the name of a
1e134 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66   column of the f
1e135 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a  orm X.Y.Z or Y.Z
1e136 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f 6f 6b   or just Z, look
1e137 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61 6d 65   up.** that name
1e138 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66 20 73   in the set of s
1e139 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69 6e 20  ource tables in 
1e13a 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d 61 6b  pSrcList and mak
1e13b 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a 2a 20  e the pExpr .** 
1e13c 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
1e13d 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20 74 68  refer back to th
1e13e 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75 6d 6e  at source column
1e13f 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
1e140 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20   changes.** are 
1e141 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a 0a 2a  made to pExpr:.*
1e142 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69  *.**    pExpr->i
1e143 44 62 20 20 20 20 20 20 20 20 20 20 20 53 65 74  Db           Set
1e144 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 64 62   the index in db
1e145 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64  ->aDb[] of the d
1e146 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 20 20 20  atabase X.**    
1e147 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e148 20 20 20 20 20 28 65 76 65 6e 20 69 66 20 58 20       (even if X 
1e149 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a 2a 2a 20  is implied)..** 
1e14a 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1e14b 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20 74          Set to t
1e14c 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  he cursor number
1e14d 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 6f   for the table o
1e14e 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20 20  btained.**      
1e14f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e150 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69 73 74     from pSrcList
1e151 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70  ..**    pExpr->p
1e152 54 61 62 20 20 20 20 20 20 20 20 20 20 50 6f 69  Tab          Poi
1e153 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65  nts to the Table
1e154 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 58 2e   structure of X.
1e155 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a 20 20 20  Y (even if.**   
1e156 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e157 20 20 20 20 20 20 58 20 61 6e 64 2f 6f 72 20 59        X and/or Y
1e158 20 61 72 65 20 69 6d 70 6c 69 65 64 2e 29 0a 2a   are implied.).*
1e159 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c  *    pExpr->iCol
1e15a 75 6d 6e 20 20 20 20 20 20 20 53 65 74 20 74 6f  umn       Set to
1e15b 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62   the column numb
1e15c 65 72 20 77 69 74 68 69 6e 20 74 68 65 20 74 61  er within the ta
1e15d 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72  ble..**    pExpr
1e15e 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 20  ->op            
1e15f 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e  Set to TK_COLUMN
1e160 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 70  ..**    pExpr->p
1e161 4c 65 66 74 20 20 20 20 20 20 20 20 20 41 6e 79  Left         Any
1e162 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69 73   expression this
1e163 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64 65   points to is de
1e164 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 45 78 70  leted.**    pExp
1e165 72 2d 3e 70 52 69 67 68 74 20 20 20 20 20 20 20  r->pRight       
1e166 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20   Any expression 
1e167 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69  this points to i
1e168 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a  s deleted..**.**
1e169 20 54 68 65 20 70 44 62 54 6f 6b 65 6e 20 69 73   The pDbToken is
1e16a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1e16b 20 64 61 74 61 62 61 73 65 20 28 74 68 65 20 22   database (the "
1e16c 58 22 29 2e 20 20 54 68 69 73 20 76 61 6c 75 65  X").  This value
1e16d 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 20   may be.** NULL 
1e16e 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 6e 61 6d  meaning that nam
1e16f 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  e is of the form
1e170 20 59 2e 5a 20 6f 72 20 5a 2e 20 20 41 6e 79 20   Y.Z or Z.  Any 
1e171 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 62 61  available databa
1e172 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  se.** can be use
1e173 64 2e 20 20 54 68 65 20 70 54 61 62 6c 65 54 6f  d.  The pTableTo
1e174 6b 65 6e 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ken is the name 
1e175 6f 66 20 74 68 65 20 74 61 62 6c 65 20 28 74 68  of the table (th
1e176 65 20 22 59 22 29 2e 20 20 54 68 69 73 0a 2a 2a  e "Y").  This.**
1e177 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 4e 55   value can be NU
1e178 4c 4c 20 69 66 20 70 44 62 54 6f 6b 65 6e 20 69  LL if pDbToken i
1e179 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 20 49 66  s also NULL.  If
1e17a 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20   pTableToken is 
1e17b 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73  NULL it.** means
1e17c 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 6f   that the form o
1e17d 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a 20  f the name is Z 
1e17e 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73  and that columns
1e17f 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 0a   from any table.
1e180 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a  ** can be used..
1e181 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 6d  **.** If the nam
1e182 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 6f  e cannot be reso
1e183 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 73  lved unambiguous
1e184 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72  ly, leave an err
1e185 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e  or message.** in
1e186 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75   pParse and retu
1e187 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65  rn non-zero.  Re
1e188 74 75 72 6e 20 7a 65 72 6f 20 6f 6e 20 73 75 63  turn zero on suc
1e189 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cess..*/.static 
1e18a 69 6e 74 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 0a  int lookupName(.
1e18b 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1e18c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
1e18d 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1e18e 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 62 54 6f 6b  .  Token *pDbTok
1e18f 65 6e 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  en,     /* Name 
1e190 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1e191 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65  containing table
1e192 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54  , or NULL */.  T
1e193 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 54 6f 6b 65  oken *pTableToke
1e194 6e 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  n,  /* Name of t
1e195 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
1e196 63 6f 6c 75 6d 6e 2c 20 6f 72 20 4e 55 4c 4c 20  column, or NULL 
1e197 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c  */.  Token *pCol
1e198 75 6d 6e 54 6f 6b 65 6e 2c 20 2f 2a 20 4e 61 6d  umnToken, /* Nam
1e199 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e  e of the column.
1e19a 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
1e19b 74 20 2a 70 4e 43 2c 20 20 20 20 2f 2a 20 54 68  t *pNC,    /* Th
1e19c 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 75  e name context u
1e19d 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74  sed to resolve t
1e19e 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70  he name */.  Exp
1e19f 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
1e1a0 20 20 2f 2a 20 4d 61 6b 65 20 74 68 69 73 20 45    /* Make this E
1e1a1 58 50 52 20 6e 6f 64 65 20 70 6f 69 6e 74 20 74  XPR node point t
1e1a2 6f 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 63  o the selected c
1e1a3 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b 0a 20 20 63 68  olumn */.){.  ch
1e1a4 61 72 20 2a 7a 44 62 20 3d 20 30 3b 20 20 20 20  ar *zDb = 0;    
1e1a5 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
1e1a6 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
1e1a7 20 22 58 22 20 69 6e 20 58 2e 59 2e 5a 20 2a 2f   "X" in X.Y.Z */
1e1a8 0a 20 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20  .  char *zTab = 
1e1a9 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  0;      /* Name 
1e1aa 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54  of the table.  T
1e1ab 68 65 20 22 59 22 20 69 6e 20 58 2e 59 2e 5a 20  he "Y" in X.Y.Z 
1e1ac 6f 72 20 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72  or Y.Z */.  char
1e1ad 20 2a 7a 43 6f 6c 20 3d 20 30 3b 20 20 20 20 20   *zCol = 0;     
1e1ae 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1e1af 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 22 5a 22  column.  The "Z"
1e1b0 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20   */.  int i, j; 
1e1b1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
1e1b2 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
1e1b3 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20   int cnt = 0;   
1e1b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e1b5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1e1b6 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20  matching column 
1e1b7 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63  names */.  int c
1e1b8 6e 74 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20  ntTab = 0;      
1e1b9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e1ba 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69  Number of matchi
1e1bb 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a  ng table names *
1e1bc 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
1e1bd 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
1e1be 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
1e1bf 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1e1c0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
1e1c1 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1e1c2 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66  ;       /* Use f
1e1c3 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
1e1c4 70 53 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a  pSrcList items *
1e1c5 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
1e1c6 73 74 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20  st_item *pMatch 
1e1c7 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74  = 0;  /* The mat
1e1c8 63 68 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69  ching pSrcList i
1e1c9 74 65 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  tem */.  NameCon
1e1ca 74 65 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70  text *pTopNC = p
1e1cb 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69  NC;        /* Fi
1e1cc 72 73 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20  rst namecontext 
1e1cd 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  in the list */. 
1e1ce 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
1e1cf 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
1e1d0 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20     /* Schema of 
1e1d1 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  the expression *
1e1d2 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 43  /..  assert( pNC
1e1d3 20 29 3b 20 2f 2a 20 74 68 65 20 6e 61 6d 65 20   ); /* the name 
1e1d4 63 6f 6e 74 65 78 74 20 63 61 6e 6e 6f 74 20 62  context cannot b
1e1d5 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73  e NULL. */.  ass
1e1d6 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65  ert( pColumnToke
1e1d7 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65  n && pColumnToke
1e1d8 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68 65 20 5a  n->z ); /* The Z
1e1d9 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74   in X.Y.Z cannot
1e1da 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20 2f   be NULL */..  /
1e1db 2a 20 44 65 71 75 6f 74 65 20 61 6e 64 20 7a 65  * Dequote and ze
1e1dc 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 74 68 65  ro-terminate the
1e1dd 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 7a 44 62 20   names */.  zDb 
1e1de 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
1e1df 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 62 54 6f  mToken(db, pDbTo
1e1e0 6b 65 6e 29 3b 0a 20 20 7a 54 61 62 20 3d 20 73  ken);.  zTab = s
1e1e1 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1e1e2 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 54 6f  ken(db, pTableTo
1e1e3 6b 65 6e 29 3b 0a 20 20 7a 43 6f 6c 20 3d 20 73  ken);.  zCol = s
1e1e4 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1e1e5 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 54  ken(db, pColumnT
1e1e6 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 64 62 2d  oken);.  if( db-
1e1e7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
1e1e8 0a 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70  .    goto lookup
1e1e9 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20  name_end;.  }.. 
1e1ea 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
1e1eb 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61  he node to no-ma
1e1ec 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e  tch */.  pExpr->
1e1ed 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70  iTable = -1;.  p
1e1ee 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a  Expr->pTab = 0;.
1e1ef 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20 74  .  /* Start at t
1e1f0 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f  he inner-most co
1e1f1 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 6f  ntext and move o
1e1f2 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20 6d  utward until a m
1e1f3 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a 2f  atch is found */
1e1f4 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26  .  while( pNC &&
1e1f5 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 45   cnt==0 ){.    E
1e1f6 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
1e1f7 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53  .    SrcList *pS
1e1f8 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  rcList = pNC->pS
1e1f9 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 28  rcList;..    if(
1e1fa 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20   pSrcList ){.   
1e1fb 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
1e1fc 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 69  m=pSrcList->a; i
1e1fd 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pSrcList->nSrc;
1e1fe 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
1e1ff 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70          Table *p
1e200 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  Tab;.        int
1e201 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43 6f   iDb;.        Co
1e202 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20  lumn *pCol;.  . 
1e203 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 49         pTab = pI
1e204 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20  tem->pTab;.     
1e205 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21     assert( pTab!
1e206 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 6d  =0 && pTab->zNam
1e207 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  e!=0 );.        
1e208 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1e209 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
1e20a 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
1e20b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1e20c 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20  Tab->nCol>0 );. 
1e20d 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 20         if( zTab 
1e20e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
1e20f 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29   pItem->zAlias )
1e210 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  {.            ch
1e211 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70  ar *zTabName = p
1e212 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20  Item->zAlias;.  
1e213 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
1e214 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61  lite3StrICmp(zTa
1e215 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20  bName, zTab)!=0 
1e216 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
1e217 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1e218 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
1e219 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e  TabName = pTab->
1e21a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
1e21b 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65 3d     if( zTabName=
1e21c 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72  =0 || sqlite3Str
1e21d 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20 7a  ICmp(zTabName, z
1e21e 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e  Tab)!=0 ) contin
1e21f 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
1e220 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71  if( zDb!=0 && sq
1e221 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d  lite3StrICmp(db-
1e222 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
1e223 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20   zDb)!=0 ){.    
1e224 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
1e225 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
1e226 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
1e227 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e228 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b  if( 0==(cntTab++
1e229 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
1e22a 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
1e22b 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20  Item->iCursor;. 
1e22c 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1e22d 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20  pTab = pTab;.   
1e22e 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d         pSchema =
1e22f 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
1e230 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
1e231 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20   = pItem;.      
1e232 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28    }.        for(
1e233 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e  j=0, pCol=pTab->
1e234 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  aCol; j<pTab->nC
1e235 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29  ol; j++, pCol++)
1e236 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1e237 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
1e238 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  Col->zName, zCol
1e239 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1e23a 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69      IdList *pUsi
1e23b 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ng;.            
1e23c 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  cnt++;.         
1e23d 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1e23e 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f   = pItem->iCurso
1e23f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  r;.            p
1e240 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61  Expr->pTab = pTa
1e241 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  b;.            p
1e242 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20  Match = pItem;. 
1e243 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65             pSche
1e244 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
1e245 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ma;.            
1e246 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68  /* Substitute th
1e247 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20  e rowid (column 
1e248 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45  -1) for the INTE
1e249 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
1e24a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  */.            p
1e24b 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  Expr->iColumn = 
1e24c 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f  j==pTab->iPKey ?
1e24d 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20 20   -1 : j;.       
1e24e 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63 4c       if( i<pSrcL
1e24f 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20  ist->nSrc-1 ){. 
1e250 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
1e251 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74 79   pItem[1].jointy
1e252 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20  pe & JT_NATURAL 
1e253 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1e254 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d 61     /* If this ma
1e255 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e 20  tch occurred in 
1e256 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f  the left table o
1e257 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e  f a natural join
1e258 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1e259 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20 74    ** then skip t
1e25a 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 74  he right table t
1e25b 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69 63  o avoid a duplic
1e25c 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20  ate match */.   
1e25d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74               pIt
1e25e 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  em++;.          
1e25f 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
1e260 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
1e261 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49 74  f( (pUsing = pIt
1e262 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d 30  em[1].pUsing)!=0
1e263 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1e264 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d      /* If this m
1e265 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20 61  atch occurs on a
1e266 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   column that is 
1e267 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61  in the USING cla
1e268 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  use.            
1e269 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69 6e      ** of a join
1e26a 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72 63  , skip the searc
1e26b 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20 74  h of the right t
1e26c 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e  able of the join
1e26d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1e26e 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 64   ** to avoid a d
1e26f 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 74  uplicate match t
1e270 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20  here. */.       
1e271 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a           int k;.
1e272 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e273 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69 6e  for(k=0; k<pUsin
1e274 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20 20  g->nId; k++){.  
1e275 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e276 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1e277 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d 2e  mp(pUsing->a[k].
1e278 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20  zName, zCol)==0 
1e279 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1e27a 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a         pItem++;.
1e27b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e27c 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20      i++;.       
1e27d 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
1e27e 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
1e27f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e280 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e281 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e282 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1e283 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1e284 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1e285 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1e286 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1e287 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20  MIT_TRIGGER.    
1e288 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e 6f  /* If we have no
1e289 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c 76  t already resolv
1e28a 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68 65  ed the name, the
1e28b 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a 20  n maybe .    ** 
1e28c 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f 72  it is a new.* or
1e28d 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20 61   old.* trigger a
1e28e 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e 63  rgument referenc
1e28f 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  e.    */.    if(
1e290 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62 21   zDb==0 && zTab!
1e291 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26 20  =0 && cnt==0 && 
1e292 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
1e293 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54 72  k!=0 ){.      Tr
1e294 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72 69  iggerStack *pTri
1e295 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61 72  ggerStack = pPar
1e296 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a 20  se->trigStack;. 
1e297 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
1e298 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 33 32 20   = 0;.      u32 
1e299 2a 70 69 43 6f 6c 4d 61 73 6b 20 3d 20 30 3b 0a  *piColMask = 0;.
1e29a 20 20 20 20 20 20 69 66 28 20 70 54 72 69 67 67        if( pTrigg
1e29b 65 72 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20  erStack->newIdx 
1e29c 21 3d 20 2d 31 20 26 26 20 73 71 6c 69 74 65 33  != -1 && sqlite3
1e29d 53 74 72 49 43 6d 70 28 22 6e 65 77 22 2c 20 7a  StrICmp("new", z
1e29e 54 61 62 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20  Tab) == 0 ){.   
1e29f 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1e2a0 6c 65 20 3d 20 70 54 72 69 67 67 65 72 53 74 61  le = pTriggerSta
1e2a1 63 6b 2d 3e 6e 65 77 49 64 78 3b 0a 20 20 20 20  ck->newIdx;.    
1e2a2 20 20 20 20 61 73 73 65 72 74 28 20 70 54 72 69      assert( pTri
1e2a3 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 20  ggerStack->pTab 
1e2a4 29 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  );.        pTab 
1e2a5 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  = pTriggerStack-
1e2a6 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 70  >pTab;.        p
1e2a7 69 43 6f 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72  iColMask = &(pTr
1e2a8 69 67 67 65 72 53 74 61 63 6b 2d 3e 6e 65 77 43  iggerStack->newC
1e2a9 6f 6c 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d  olMask);.      }
1e2aa 65 6c 73 65 20 69 66 28 20 70 54 72 69 67 67 65  else if( pTrigge
1e2ab 72 53 74 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 21  rStack->oldIdx !
1e2ac 3d 20 2d 31 20 26 26 20 73 71 6c 69 74 65 33 53  = -1 && sqlite3S
1e2ad 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c 20 7a 54  trICmp("old", zT
1e2ae 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ab)==0 ){.      
1e2af 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1e2b0 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  = pTriggerStack-
1e2b1 3e 6f 6c 64 49 64 78 3b 0a 20 20 20 20 20 20 20  >oldIdx;.       
1e2b2 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65   assert( pTrigge
1e2b3 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a  rStack->pTab );.
1e2b4 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
1e2b5 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54  TriggerStack->pT
1e2b6 61 62 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f  ab;.        piCo
1e2b7 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67  lMask = &(pTrigg
1e2b8 65 72 53 74 61 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d  erStack->oldColM
1e2b9 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  ask);.      }.. 
1e2ba 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b       if( pTab ){
1e2bb 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43   .        int iC
1e2bc 6f 6c 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75  ol;.        Colu
1e2bd 6d 6e 20 2a 70 43 6f 6c 20 3d 20 70 54 61 62 2d  mn *pCol = pTab-
1e2be 3e 61 43 6f 6c 3b 0a 0a 20 20 20 20 20 20 20 20  >aCol;..        
1e2bf 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e  pSchema = pTab->
1e2c0 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20  pSchema;.       
1e2c1 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20   cntTab++;.     
1e2c2 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
1e2c3 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c  Col < pTab->nCol
1e2c4 3b 20 69 43 6f 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b  ; iCol++, pCol++
1e2c5 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ) {.          if
1e2c6 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1e2c7 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43  (pCol->zName, zC
1e2c8 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
1e2c9 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20        cnt++;.   
1e2ca 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1e2cb 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d 3d  iColumn = iCol==
1e2cc 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31  pTab->iPKey ? -1
1e2cd 20 3a 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20   : iCol;.       
1e2ce 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62       pExpr->pTab
1e2cf 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 20   = pTab;.       
1e2d0 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30       if( iCol>=0
1e2d1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1e2d2 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c    testcase( iCol
1e2d3 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20  ==31 );.        
1e2d4 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1e2d5 69 43 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20  iCol==32 );.    
1e2d6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43            if( iC
1e2d7 6f 6c 3e 3d 33 32 20 29 7b 0a 20 20 20 20 20 20  ol>=32 ){.      
1e2d8 20 20 20 20 20 20 20 20 20 20 2a 70 69 43 6f 6c            *piCol
1e2d9 4d 61 73 6b 20 3d 20 30 78 66 66 66 66 66 66 66  Mask = 0xfffffff
1e2da 66 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  f;.             
1e2db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1e2dc 20 20 20 20 20 20 20 20 2a 70 69 43 6f 6c 4d 61          *piColMa
1e2dd 73 6b 20 7c 3d 20 28 28 75 33 32 29 31 29 3c 3c  sk |= ((u32)1)<<
1e2de 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20  iCol;.          
1e2df 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1e2e0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1e2e1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1e2e2 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1e2e3 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
1e2e4 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
1e2e5 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1e2e6 52 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 0a 20 20  R) */..    /*.  
1e2e7 20 20 2a 2a 20 50 65 72 68 61 70 73 20 74 68 65    ** Perhaps the
1e2e8 20 6e 61 6d 65 20 69 73 20 61 20 72 65 66 65 72   name is a refer
1e2e9 65 6e 63 65 20 74 6f 20 74 68 65 20 52 4f 57 49  ence to the ROWI
1e2ea 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  D.    */.    if(
1e2eb 20 63 6e 74 3d 3d 30 20 26 26 20 63 6e 74 54 61   cnt==0 && cntTa
1e2ec 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33 49  b==1 && sqlite3I
1e2ed 73 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a  sRowid(zCol) ){.
1e2ee 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20        cnt = 1;. 
1e2ef 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
1e2f0 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  umn = -1;.      
1e2f1 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20  pExpr->affinity 
1e2f2 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  = SQLITE_AFF_INT
1e2f3 45 47 45 52 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  EGER;.    }..   
1e2f4 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68   /*.    ** If th
1e2f5 65 20 69 6e 70 75 74 20 69 73 20 6f 66 20 74 68  e input is of th
1e2f6 65 20 66 6f 72 6d 20 5a 20 28 6e 6f 74 20 59 2e  e form Z (not Y.
1e2f7 5a 20 6f 72 20 58 2e 59 2e 5a 29 20 74 68 65 6e  Z or X.Y.Z) then
1e2f8 20 74 68 65 20 6e 61 6d 65 20 5a 0a 20 20 20 20   the name Z.    
1e2f9 2a 2a 20 6d 69 67 68 74 20 72 65 66 65 72 20 74  ** might refer t
1e2fa 6f 20 61 6e 20 72 65 73 75 6c 74 2d 73 65 74 20  o an result-set 
1e2fb 61 6c 69 61 73 2e 20 20 54 68 69 73 20 68 61 70  alias.  This hap
1e2fc 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c  pens, for exampl
1e2fd 65 2c 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 77  e, when.    ** w
1e2fe 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e 67 20  e are resolving 
1e2ff 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 57 48 45  names in the WHE
1e300 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  RE clause of the
1e301 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 6d 61   following comma
1e302 6e 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  nd:.    **.    *
1e303 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62  *     SELECT a+b
1e304 20 41 53 20 78 20 46 52 4f 4d 20 74 61 62 6c 65   AS x FROM table
1e305 20 57 48 45 52 45 20 78 3c 31 30 3b 0a 20 20 20   WHERE x<10;.   
1e306 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 6e 20 63 61   **.    ** In ca
1e307 73 65 73 20 6c 69 6b 65 20 74 68 69 73 2c 20 72  ses like this, r
1e308 65 70 6c 61 63 65 20 70 45 78 70 72 20 77 69 74  eplace pExpr wit
1e309 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  h a copy of the 
1e30a 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 0a  expression that.
1e30b 20 20 20 20 2a 2a 20 66 6f 72 6d 73 20 74 68 65      ** forms the
1e30c 20 72 65 73 75 6c 74 20 73 65 74 20 65 6e 74 72   result set entr
1e30d 79 20 28 22 61 2b 62 22 20 69 6e 20 74 68 65 20  y ("a+b" in the 
1e30e 65 78 61 6d 70 6c 65 29 20 61 6e 64 20 72 65 74  example) and ret
1e30f 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  urn immediately.
1e310 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61  .    ** Note tha
1e311 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  t the expression
1e312 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1e313 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61  et should have a
1e314 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20 20 20  lready been.    
1e315 2a 2a 20 72 65 73 6f 6c 76 65 64 20 62 79 20 74  ** resolved by t
1e316 68 65 20 74 69 6d 65 20 74 68 65 20 57 48 45 52  he time the WHER
1e317 45 20 63 6c 61 75 73 65 20 69 73 20 72 65 73 6f  E clause is reso
1e318 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  lved..    */.   
1e319 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 28   if( cnt==0 && (
1e31a 70 45 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 45  pEList = pNC->pE
1e31b 4c 69 73 74 29 21 3d 30 20 26 26 20 7a 54 61 62  List)!=0 && zTab
1e31c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72  ==0 ){.      for
1e31d 28 6a 3d 30 3b 20 6a 3c 70 45 4c 69 73 74 2d 3e  (j=0; j<pEList->
1e31e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20  nExpr; j++){.   
1e31f 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20 3d       char *zAs =
1e320 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e   pEList->a[j].zN
1e321 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ame;.        if(
1e322 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74   zAs!=0 && sqlit
1e323 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a  e3StrICmp(zAs, z
1e324 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Col)==0 ){.     
1e325 20 20 20 20 20 45 78 70 72 20 2a 70 4f 72 69 67       Expr *pOrig
1e326 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
1e327 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  rt( pExpr->pLeft
1e328 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 70 52  ==0 && pExpr->pR
1e329 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ight==0 );.     
1e32a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
1e32b 70 72 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 29  pr->x.pList==0 )
1e32c 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
1e32d 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  rt( pExpr->x.pSe
1e32e 6c 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lect==0 );.     
1e32f 20 20 20 20 20 70 4f 72 69 67 20 3d 20 70 45 4c       pOrig = pEL
1e330 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 3b  ist->a[j].pExpr;
1e331 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
1e332 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 26 26  pNC->allowAgg &&
1e333 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1e334 28 70 4f 72 69 67 2c 20 45 50 5f 41 67 67 29 20  (pOrig, EP_Agg) 
1e335 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
1e336 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1e337 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 6f  Parse, "misuse o
1e338 66 20 61 6c 69 61 73 65 64 20 61 67 67 72 65 67  f aliased aggreg
1e339 61 74 65 20 25 73 22 2c 20 7a 41 73 29 3b 0a 20  ate %s", zAs);. 
1e33a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1e33b 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
1e33c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  l);.            
1e33d 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
1e33e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1e33f 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61  resolveAlias(pPa
1e340 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 6a 2c 20  rse, pEList, j, 
1e341 70 45 78 70 72 2c 20 22 22 29 3b 0a 20 20 20 20  pExpr, "");.    
1e342 20 20 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20        cnt = 1;. 
1e343 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20           pMatch 
1e344 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61  = 0;.          a
1e345 73 73 65 72 74 28 20 7a 54 61 62 3d 3d 30 20 26  ssert( zTab==0 &
1e346 26 20 7a 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20  & zDb==0 );.    
1e347 20 20 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75        goto looku
1e348 70 6e 61 6d 65 5f 65 6e 64 5f 32 3b 0a 20 20 20  pname_end_2;.   
1e349 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 0a       }.      } .
1e34a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64      }..    /* Ad
1e34b 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78  vance to the nex
1e34c 74 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20  t name context. 
1e34d 20 54 68 65 20 6c 6f 6f 70 20 77 69 6c 6c 20 65   The loop will e
1e34e 78 69 74 20 77 68 65 6e 20 65 69 74 68 65 72 0a  xit when either.
1e34f 20 20 20 20 2a 2a 20 77 65 20 68 61 76 65 20 61      ** we have a
1e350 20 6d 61 74 63 68 20 28 63 6e 74 3e 30 29 20 6f   match (cnt>0) o
1e351 72 20 77 68 65 6e 20 77 65 20 72 75 6e 20 6f 75  r when we run ou
1e352 74 20 6f 66 20 6e 61 6d 65 20 63 6f 6e 74 65 78  t of name contex
1e353 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ts..    */.    i
1e354 66 28 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  f( cnt==0 ){.   
1e355 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d 3e 70 4e     pNC = pNC->pN
1e356 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ext;.    }.  }..
1e357 20 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 58 20 61    /*.  ** If X a
1e358 6e 64 20 59 20 61 72 65 20 4e 55 4c 4c 20 28 69  nd Y are NULL (i
1e359 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66  n other words if
1e35a 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e   only the column
1e35b 20 6e 61 6d 65 20 5a 20 69 73 0a 20 20 2a 2a 20   name Z is.  ** 
1e35c 73 75 70 70 6c 69 65 64 29 20 61 6e 64 20 74 68  supplied) and th
1e35d 65 20 76 61 6c 75 65 20 6f 66 20 5a 20 69 73 20  e value of Z is 
1e35e 65 6e 63 6c 6f 73 65 64 20 69 6e 20 64 6f 75 62  enclosed in doub
1e35f 6c 65 2d 71 75 6f 74 65 73 2c 20 74 68 65 6e 0a  le-quotes, then.
1e360 20 20 2a 2a 20 5a 20 69 73 20 61 20 73 74 72 69    ** Z is a stri
1e361 6e 67 20 6c 69 74 65 72 61 6c 20 69 66 20 69 74  ng literal if it
1e362 20 64 6f 65 73 6e 27 74 20 6d 61 74 63 68 20 61   doesn't match a
1e363 6e 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e  ny column names.
1e364 20 20 49 6e 20 74 68 61 74 0a 20 20 2a 2a 20 63    In that.  ** c
1e365 61 73 65 2c 20 77 65 20 6e 65 65 64 20 74 6f 20  ase, we need to 
1e366 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 77 61  return right awa
1e367 79 20 61 6e 64 20 6e 6f 74 20 6d 61 6b 65 20 61  y and not make a
1e368 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 20  ny changes to.  
1e369 2a 2a 20 70 45 78 70 72 2e 0a 20 20 2a 2a 0a 20  ** pExpr..  **. 
1e36a 20 2a 2a 20 42 65 63 61 75 73 65 20 6e 6f 20 72   ** Because no r
1e36b 65 66 65 72 65 6e 63 65 20 77 61 73 20 6d 61 64  eference was mad
1e36c 65 20 74 6f 20 6f 75 74 65 72 20 63 6f 6e 74 65  e to outer conte
1e36d 78 74 73 2c 20 74 68 65 20 70 4e 43 2d 3e 6e 52  xts, the pNC->nR
1e36e 65 66 0a 20 20 2a 2a 20 66 69 65 6c 64 73 20 61  ef.  ** fields a
1e36f 72 65 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 69  re not changed i
1e370 6e 20 61 6e 79 20 63 6f 6e 74 65 78 74 2e 0a 20  n any context.. 
1e371 20 2a 2f 0a 20 20 69 66 28 20 63 6e 74 3d 3d 30   */.  if( cnt==0
1e372 20 26 26 20 7a 54 61 62 3d 3d 30 20 26 26 20 45   && zTab==0 && E
1e373 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
1e374 45 78 70 72 2c 45 50 5f 44 62 6c 51 75 6f 74 65  Expr,EP_DblQuote
1e375 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  d) ){.    sqlite
1e376 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c  3DbFree(db, zCol
1e377 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  );.    pExpr->op
1e378 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20   = TK_STRING;.  
1e379 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
1e37a 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  0;.    return 0;
1e37b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
1e37c 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65  cnt==0 means the
1e37d 72 65 20 77 61 73 20 6e 6f 74 20 6d 61 74 63 68  re was not match
1e37e 2e 20 20 63 6e 74 3e 31 20 6d 65 61 6e 73 20 74  .  cnt>1 means t
1e37f 68 65 72 65 20 77 65 72 65 20 74 77 6f 20 6f 72  here were two or
1e380 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d 61 74 63 68  .  ** more match
1e381 65 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c  es.  Either way,
1e382 20 77 65 20 68 61 76 65 20 61 6e 20 65 72 72 6f   we have an erro
1e383 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6e  r..  */.  if( cn
1e384 74 21 3d 31 20 29 7b 0a 20 20 20 20 63 6f 6e 73  t!=1 ){.    cons
1e385 74 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20  t char *zErr;.  
1e386 20 20 7a 45 72 72 20 3d 20 63 6e 74 3d 3d 30 20    zErr = cnt==0 
1e387 3f 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d  ? "no such colum
1e388 6e 22 20 3a 20 22 61 6d 62 69 67 75 6f 75 73 20  n" : "ambiguous 
1e389 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22 3b 0a 20 20  column name";.  
1e38a 20 20 69 66 28 20 7a 44 62 20 29 7b 0a 20 20 20    if( zDb ){.   
1e38b 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1e38c 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20  sg(pParse, "%s: 
1e38d 25 73 2e 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c  %s.%s.%s", zErr,
1e38e 20 7a 44 62 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c   zDb, zTab, zCol
1e38f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1e390 20 7a 54 61 62 20 29 7b 0a 20 20 20 20 20 20 73   zTab ){.      s
1e391 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1e392 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25  Parse, "%s: %s.%
1e393 73 22 2c 20 7a 45 72 72 2c 20 7a 54 61 62 2c 20  s", zErr, zTab, 
1e394 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65  zCol);.    }else
1e395 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1e396 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1e397 22 25 73 3a 20 25 73 22 2c 20 7a 45 72 72 2c 20  "%s: %s", zErr, 
1e398 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  zCol);.    }.   
1e399 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b   pTopNC->nErr++;
1e39a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20  .  }..  /* If a 
1e39b 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 20 74 61  column from a ta
1e39c 62 6c 65 20 69 6e 20 70 53 72 63 4c 69 73 74 20  ble in pSrcList 
1e39d 69 73 20 72 65 66 65 72 65 6e 63 65 64 2c 20 74  is referenced, t
1e39e 68 65 6e 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20  hen record.  ** 
1e39f 74 68 69 73 20 66 61 63 74 20 69 6e 20 74 68 65  this fact in the
1e3a0 20 70 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 63 6f   pSrcList.a[].co
1e3a1 6c 55 73 65 64 20 62 69 74 6d 61 73 6b 2e 20 20  lUsed bitmask.  
1e3a2 43 6f 6c 75 6d 6e 20 30 20 63 61 75 73 65 73 0a  Column 0 causes.
1e3a3 20 20 2a 2a 20 62 69 74 20 30 20 74 6f 20 62 65    ** bit 0 to be
1e3a4 20 73 65 74 2e 20 20 43 6f 6c 75 6d 6e 20 31 20   set.  Column 1 
1e3a5 73 65 74 73 20 62 69 74 20 31 2e 20 20 41 6e 64  sets bit 1.  And
1e3a6 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74   so forth.  If t
1e3a7 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e  he.  ** column n
1e3a8 75 6d 62 65 72 20 69 73 20 67 72 65 61 74 65 72  umber is greater
1e3a9 20 74 68 61 6e 20 74 68 65 20 6e 75 6d 62 65 72   than the number
1e3aa 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20   of bits in the 
1e3ab 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20 74 68 65  bitmask.  ** the
1e3ac 6e 20 73 65 74 20 74 68 65 20 68 69 67 68 2d 6f  n set the high-o
1e3ad 72 64 65 72 20 62 69 74 20 6f 66 20 74 68 65 20  rder bit of the 
1e3ae 62 69 74 6d 61 73 6b 2e 0a 20 20 2a 2f 0a 20 20  bitmask..  */.  
1e3af 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  if( pExpr->iColu
1e3b0 6d 6e 3e 3d 30 20 26 26 20 70 4d 61 74 63 68 21  mn>=0 && pMatch!
1e3b1 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20  =0 ){.    int n 
1e3b2 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
1e3b3 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
1e3b4 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20  n==BMS-1 );.    
1e3b5 69 66 28 20 6e 3e 3d 42 4d 53 20 29 7b 0a 20 20  if( n>=BMS ){.  
1e3b6 20 20 20 20 6e 20 3d 20 42 4d 53 2d 31 3b 0a 20      n = BMS-1;. 
1e3b7 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1e3b8 20 70 4d 61 74 63 68 2d 3e 69 43 75 72 73 6f 72   pMatch->iCursor
1e3b9 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20  ==pExpr->iTable 
1e3ba 29 3b 0a 20 20 20 20 70 4d 61 74 63 68 2d 3e 63  );.    pMatch->c
1e3bb 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d  olUsed |= ((Bitm
1e3bc 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20 7d 0a 0a  ask)1)<<n;.  }..
1e3bd 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a  lookupname_end:.
1e3be 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e    /* Clean up an
1e3bf 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20  d return.  */.  
1e3c0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1e3c1 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69 74 65  , zDb);.  sqlite
1e3c2 33 44 62 46 72 65 65 28 64 62 2c 20 7a 54 61 62  3DbFree(db, zTab
1e3c3 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
1e3c4 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72  Delete(db, pExpr
1e3c5 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70  ->pLeft);.  pExp
1e3c6 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20  r->pLeft = 0;.  
1e3c7 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1e3c8 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69  e(db, pExpr->pRi
1e3c9 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70  ght);.  pExpr->p
1e3ca 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78  Right = 0;.  pEx
1e3cb 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55  pr->op = TK_COLU
1e3cc 4d 4e 3b 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65  MN;.lookupname_e
1e3cd 6e 64 5f 32 3a 0a 20 20 73 71 6c 69 74 65 33 44  nd_2:.  sqlite3D
1e3ce 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b  bFree(db, zCol);
1e3cf 0a 20 20 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b  .  if( cnt==1 ){
1e3d0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 43  .    assert( pNC
1e3d1 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  !=0 );.    sqlit
1e3d2 65 33 41 75 74 68 52 65 61 64 28 70 50 61 72 73  e3AuthRead(pPars
1e3d3 65 2c 20 70 45 78 70 72 2c 20 70 53 63 68 65 6d  e, pExpr, pSchem
1e3d4 61 2c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74  a, pNC->pSrcList
1e3d5 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d  );.    /* Increm
1e3d6 65 6e 74 20 74 68 65 20 6e 52 65 66 20 76 61 6c  ent the nRef val
1e3d7 75 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63  ue on all name c
1e3d8 6f 6e 74 65 78 74 73 20 66 72 6f 6d 20 54 6f 70  ontexts from Top
1e3d9 4e 43 20 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20  NC up to.    ** 
1e3da 74 68 65 20 70 6f 69 6e 74 20 77 68 65 72 65 20  the point where 
1e3db 74 68 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 64  the name matched
1e3dc 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29  . */.    for(;;)
1e3dd 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1e3de 70 54 6f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20  pTopNC!=0 );.   
1e3df 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b     pTopNC->nRef+
1e3e0 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f  +;.      if( pTo
1e3e1 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b  pNC==pNC ) break
1e3e2 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 20 3d  ;.      pTopNC =
1e3e3 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a   pTopNC->pNext;.
1e3e4 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
1e3e5 20 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20   0;.  } else {. 
1e3e6 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
1e3e7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1e3e8 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 62 61  outine is callba
1e3e9 63 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 57 61  ck for sqlite3Wa
1e3ea 6c 6b 45 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20  lkExpr()..**.** 
1e3eb 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63  Resolve symbolic
1e3ec 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43   names into TK_C
1e3ed 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20  OLUMN operators 
1e3ee 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 0a  for the current.
1e3ef 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 65  ** node in the e
1e3f0 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
1e3f1 20 52 65 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e   Return 0 to con
1e3f2 74 69 6e 75 65 20 74 68 65 20 73 65 61 72 63 68  tinue the search
1e3f3 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65   down.** the tre
1e3f4 65 20 6f 72 20 32 20 74 6f 20 61 62 6f 72 74 20  e or 2 to abort 
1e3f5 74 68 65 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a  the tree walk..*
1e3f6 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1e3f7 65 20 61 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f  e also does erro
1e3f8 72 20 63 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e  r checking and n
1e3f9 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66  ame resolution f
1e3fa 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e  or.** function n
1e3fb 61 6d 65 73 2e 20 20 54 68 65 20 6f 70 65 72 61  ames.  The opera
1e3fc 74 6f 72 20 66 6f 72 20 61 67 67 72 65 67 61 74  tor for aggregat
1e3fd 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63  e functions is c
1e3fe 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f  hanged.** to TK_
1e3ff 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f  AGG_FUNCTION..*/
1e400 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
1e401 6c 76 65 45 78 70 72 53 74 65 70 28 57 61 6c 6b  lveExprStep(Walk
1e402 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
1e403 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d  r *pExpr){.  Nam
1e404 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 0a 20  eContext *pNC;. 
1e405 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a   Parse *pParse;.
1e406 0a 20 20 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72  .  pNC = pWalker
1e407 2d 3e 75 2e 70 4e 43 3b 0a 20 20 61 73 73 65 72  ->u.pNC;.  asser
1e408 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 70  t( pNC!=0 );.  p
1e409 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61  Parse = pNC->pPa
1e40a 72 73 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  rse;.  assert( p
1e40b 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e  Parse==pWalker->
1e40c 70 50 61 72 73 65 20 29 3b 0a 0a 20 20 69 66 28  pParse );..  if(
1e40d 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65   ExprHasAnyPrope
1e40e 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65  rty(pExpr, EP_Re
1e40f 73 6f 6c 76 65 64 29 20 29 20 72 65 74 75 72 6e  solved) ) return
1e410 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 45 78   WRC_Prune;.  Ex
1e411 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 45  prSetProperty(pE
1e412 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64  xpr, EP_Resolved
1e413 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  );.#ifndef NDEBU
1e414 47 0a 20 20 69 66 28 20 70 4e 43 2d 3e 70 53 72  G.  if( pNC->pSr
1e415 63 4c 69 73 74 20 26 26 20 70 4e 43 2d 3e 70 53  cList && pNC->pS
1e416 72 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30  rcList->nAlloc>0
1e417 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20   ){.    SrcList 
1e418 2a 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d  *pSrcList = pNC-
1e419 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 69  >pSrcList;.    i
1e41a 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
1e41b 30 3b 20 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69  0; i<pNC->pSrcLi
1e41c 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  st->nSrc; i++){.
1e41d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53        assert( pS
1e41e 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  rcList->a[i].iCu
1e41f 72 73 6f 72 3e 3d 30 20 26 26 20 70 53 72 63 4c  rsor>=0 && pSrcL
1e420 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f  ist->a[i].iCurso
1e421 72 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b  r<pParse->nTab);
1e422 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
1e423 66 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70  f.  switch( pExp
1e424 72 2d 3e 6f 70 20 29 7b 0a 0a 23 69 66 20 64 65  r->op ){..#if de
1e425 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
1e426 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54  BLE_UPDATE_DELET
1e427 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66  E_LIMIT) && !def
1e428 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1e429 5f 53 55 42 51 55 45 52 59 29 0a 20 20 20 20 2f  _SUBQUERY).    /
1e42a 2a 20 54 68 65 20 73 70 65 63 69 61 6c 20 6f 70  * The special op
1e42b 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57 20 6d 65  erator TK_ROW me
1e42c 61 6e 73 20 75 73 65 20 74 68 65 20 72 6f 77 69  ans use the rowi
1e42d 64 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 0a  d for the first.
1e42e 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e      ** column in
1e42f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
1e430 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
1e431 62 79 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64  by the LIMIT and
1e432 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20 2a 2a   ORDER BY.    **
1e433 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69   clause processi
1e434 6e 67 20 6f 6e 20 55 50 44 41 54 45 20 61 6e 64  ng on UPDATE and
1e435 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e   DELETE statemen
1e436 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ts..    */.    c
1e437 61 73 65 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20  ase TK_ROW: {.  
1e438 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72      SrcList *pSr
1e439 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  cList = pNC->pSr
1e43a 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 73 74 72  cList;.      str
1e43b 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
1e43c 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 61   *pItem;.      a
1e43d 73 73 65 72 74 28 20 70 53 72 63 4c 69 73 74 20  ssert( pSrcList 
1e43e 26 26 20 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72  && pSrcList->nSr
1e43f 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 49  c==1 );.      pI
1e440 74 65 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e  tem = pSrcList->
1e441 61 3b 20 0a 20 20 20 20 20 20 70 45 78 70 72 2d  a; .      pExpr-
1e442 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b  >op = TK_COLUMN;
1e443 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54  .      pExpr->pT
1e444 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62  ab = pItem->pTab
1e445 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69  ;.      pExpr->i
1e446 54 61 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69  Table = pItem->i
1e447 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 45  Cursor;.      pE
1e448 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d  xpr->iColumn = -
1e449 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  1;.      pExpr->
1e44a 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54  affinity = SQLIT
1e44b 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20  E_AFF_INTEGER;. 
1e44c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1e44d 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  }.#endif /* defi
1e44e 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
1e44f 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f  E_UPDATE_DELETE_
1e450 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e  LIMIT) && !defin
1e451 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
1e452 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 20 20 20  UBQUERY) */..   
1e453 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74   /* A lone ident
1e454 69 66 69 65 72 20 69 73 20 74 68 65 20 6e 61 6d  ifier is the nam
1e455 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20  e of a column.. 
1e456 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54     */.    case T
1e457 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20 20 6c 6f  K_ID: {.      lo
1e458 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c  okupName(pParse,
1e459 20 30 2c 20 30 2c 20 26 70 45 78 70 72 2d 3e 74   0, 0, &pExpr->t
1e45a 6f 6b 65 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72  oken, pNC, pExpr
1e45b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1e45c 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d  WRC_Prune;.    }
1e45d 0a 20 20 0a 20 20 20 20 2f 2a 20 41 20 74 61 62  .  .    /* A tab
1e45e 6c 65 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75  le name and colu
1e45f 6d 6e 20 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e  mn name:     ID.
1e460 49 44 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20 64  ID.    ** Or a d
1e461 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61  atabase, table a
1e462 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49  nd column:  ID.I
1e463 44 2e 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20  D.ID.    */.    
1e464 63 61 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20  case TK_DOT: {. 
1e465 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c       Token *pCol
1e466 75 6d 6e 3b 0a 20 20 20 20 20 20 54 6f 6b 65 6e  umn;.      Token
1e467 20 2a 70 54 61 62 6c 65 3b 0a 20 20 20 20 20 20   *pTable;.      
1e468 54 6f 6b 65 6e 20 2a 70 44 62 3b 0a 20 20 20 20  Token *pDb;.    
1e469 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 0a    Expr *pRight;.
1e46a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28 20 70 53  .      /* if( pS
1e46b 72 63 4c 69 73 74 3d 3d 30 20 29 20 62 72 65 61  rcList==0 ) brea
1e46c 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70 52 69 67  k; */.      pRig
1e46d 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67  ht = pExpr->pRig
1e46e 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52  ht;.      if( pR
1e46f 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20  ight->op==TK_ID 
1e470 29 7b 0a 20 20 20 20 20 20 20 20 70 44 62 20 3d  ){.        pDb =
1e471 20 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62   0;.        pTab
1e472 6c 65 20 3d 20 26 70 45 78 70 72 2d 3e 70 4c 65  le = &pExpr->pLe
1e473 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 20  ft->token;.     
1e474 20 20 20 70 43 6f 6c 75 6d 6e 20 3d 20 26 70 52     pColumn = &pR
1e475 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20  ight->token;.   
1e476 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e477 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74    assert( pRight
1e478 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0a  ->op==TK_DOT );.
1e479 20 20 20 20 20 20 20 20 70 44 62 20 3d 20 26 70          pDb = &p
1e47a 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b  Expr->pLeft->tok
1e47b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62  en;.        pTab
1e47c 6c 65 20 3d 20 26 70 52 69 67 68 74 2d 3e 70 4c  le = &pRight->pL
1e47d 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20  eft->token;.    
1e47e 20 20 20 20 70 43 6f 6c 75 6d 6e 20 3d 20 26 70      pColumn = &p
1e47f 52 69 67 68 74 2d 3e 70 52 69 67 68 74 2d 3e 74  Right->pRight->t
1e480 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  oken;.      }.  
1e481 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70      lookupName(p
1e482 50 61 72 73 65 2c 20 70 44 62 2c 20 70 54 61 62  Parse, pDb, pTab
1e483 6c 65 2c 20 70 43 6f 6c 75 6d 6e 2c 20 70 4e 43  le, pColumn, pNC
1e484 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  , pExpr);.      
1e485 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65  return WRC_Prune
1e486 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1e487 52 65 73 6f 6c 76 65 20 66 75 6e 63 74 69 6f 6e  Resolve function
1e488 20 6e 61 6d 65 73 0a 20 20 20 20 2a 2f 0a 20 20   names.    */.  
1e489 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f    case TK_CONST_
1e48a 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 54  FUNC:.    case T
1e48b 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20  K_FUNCTION: {.  
1e48c 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
1e48d 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ist = pExpr->x.p
1e48e 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  List;    /* The 
1e48f 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 2a 2f  argument list */
1e490 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 70  .      int n = p
1e491 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45  List ? pList->nE
1e492 78 70 72 20 3a 20 30 3b 20 20 20 20 2f 2a 20 4e  xpr : 0;    /* N
1e493 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
1e494 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ts */.      int 
1e495 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 30  no_such_func = 0
1e496 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
1e497 69 66 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74  if no such funct
1e498 69 6f 6e 20 65 78 69 73 74 73 20 2a 2f 0a 20 20  ion exists */.  
1e499 20 20 20 20 69 6e 74 20 77 72 6f 6e 67 5f 6e 75      int wrong_nu
1e49a 6d 5f 61 72 67 73 20 3d 20 30 3b 20 20 20 20 20  m_args = 0;     
1e49b 2f 2a 20 54 72 75 65 20 69 66 20 77 72 6f 6e 67  /* True if wrong
1e49c 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
1e49d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ents */.      in
1e49e 74 20 69 73 5f 61 67 67 20 3d 20 30 3b 20 20 20  t is_agg = 0;   
1e49f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1e4a0 65 20 69 66 20 69 73 20 61 6e 20 61 67 67 72 65  e if is an aggre
1e4a1 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  gate function */
1e4a2 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74 68 3b  .      int auth;
1e4a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e4a4 20 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74     /* Authorizat
1e4a5 69 6f 6e 20 74 6f 20 75 73 65 20 74 68 65 20 66  ion to use the f
1e4a6 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  unction */.     
1e4a7 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 20   int nId;       
1e4a8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e4a9 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  Number of charac
1e4aa 74 65 72 73 20 69 6e 20 66 75 6e 63 74 69 6f 6e  ters in function
1e4ab 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63   name */.      c
1e4ac 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20  onst char *zId; 
1e4ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1e4ae 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e  e function name.
1e4af 20 2a 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65   */.      FuncDe
1e4b0 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20  f *pDef;        
1e4b1 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
1e4b2 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66  tion about the f
1e4b3 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  unction */.     
1e4b4 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50   u8 enc = ENC(pP
1e4b5 61 72 73 65 2d 3e 64 62 29 3b 20 20 20 2f 2a 20  arse->db);   /* 
1e4b6 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63  The database enc
1e4b7 6f 64 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 20 20  oding */..      
1e4b8 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
1e4b9 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
1e4ba 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
1e4bb 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63 68  .      zId = (ch
1e4bc 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
1e4bd 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20  .z;.      nId = 
1e4be 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a  pExpr->token.n;.
1e4bf 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c        pDef = sql
1e4c0 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
1e4c1 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64  (pParse->db, zId
1e4c2 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20 30  , nId, n, enc, 0
1e4c3 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65  );.      if( pDe
1e4c4 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  f==0 ){.        
1e4c5 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69  pDef = sqlite3Fi
1e4c6 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73  ndFunction(pPars
1e4c7 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c  e->db, zId, nId,
1e4c8 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20   -1, enc, 0);.  
1e4c9 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d        if( pDef==
1e4ca 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  0 ){.          n
1e4cb 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 3b  o_such_func = 1;
1e4cc 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1e4cd 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67 5f            wrong_
1e4ce 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20 20  num_args = 1;.  
1e4cf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
1e4d0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 73 5f  lse{.        is_
1e4d1 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 75 6e  agg = pDef->xFun
1e4d2 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69  c==0;.      }.#i
1e4d3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1e4d4 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
1e4d5 20 20 20 20 20 20 69 66 28 20 70 44 65 66 20 29        if( pDef )
1e4d6 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 20 3d  {.        auth =
1e4d7 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
1e4d8 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
1e4d9 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70 44  _FUNCTION, 0, pD
1e4da 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ef->zName, 0);. 
1e4db 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68 21         if( auth!
1e4dc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1e4dd 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68          if( auth
1e4de 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
1e4df 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1e4e0 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1e4e1 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72  rse, "not author
1e4e2 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 6e 63  ized to use func
1e4e3 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20 20  tion: %s",.     
1e4e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e4e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1e4e6 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Def->zName);.   
1e4e7 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45           pNC->nE
1e4e8 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  rr++;.          
1e4e9 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70  }.          pExp
1e4ea 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b  r->op = TK_NULL;
1e4eb 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1e4ec 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20  n WRC_Prune;.   
1e4ed 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23       }.      }.#
1e4ee 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20  endif.      if( 
1e4ef 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43 2d 3e  is_agg && !pNC->
1e4f0 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20 20 20  allowAgg ){.    
1e4f1 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1e4f2 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73  Msg(pParse, "mis
1e4f3 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 65  use of aggregate
1e4f4 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29   function %.*s()
1e4f5 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20 20  ", nId,zId);.   
1e4f6 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b       pNC->nErr++
1e4f7 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67 67  ;.        is_agg
1e4f8 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
1e4f9 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66 75  e if( no_such_fu
1e4fa 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  nc ){.        sq
1e4fb 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1e4fc 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 66  arse, "no such f
1e4fd 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c 20  unction: %.*s", 
1e4fe 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20  nId, zId);.     
1e4ff 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a     pNC->nErr++;.
1e500 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1e501 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 29  wrong_num_args )
1e502 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1e503 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1e504 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f  ,"wrong number o
1e505 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 66  f arguments to f
1e506 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c  unction %.*s()",
1e507 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 49  .             nI
1e508 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20  d, zId);.       
1e509 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20   pNC->nErr++;.  
1e50a 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1e50b 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20 20 20  is_agg ){.      
1e50c 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
1e50d 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20  _AGG_FUNCTION;. 
1e50e 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 73 41         pNC->hasA
1e50f 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  gg = 1;.      }.
1e510 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67        if( is_agg
1e511 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67   ) pNC->allowAgg
1e512 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 0;.      sqli
1e513 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28  te3WalkExprList(
1e514 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74 29 3b  pWalker, pList);
1e515 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67  .      if( is_ag
1e516 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67  g ) pNC->allowAg
1e517 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20  g = 1;.      /* 
1e518 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74 65  FIX ME:  Compute
1e519 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
1e51a 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 65 78   based on the ex
1e51b 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a 20 20  pected return.  
1e51c 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 20 74      ** type of t
1e51d 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 20  he function .   
1e51e 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75     */.      retu
1e51f 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
1e520 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
1e521 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
1e522 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c  .    case TK_SEL
1e523 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  ECT:.    case TK
1e524 5f 45 58 49 53 54 53 3a 0a 23 65 6e 64 69 66 0a  _EXISTS:.#endif.
1e525 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20      case TK_IN: 
1e526 7b 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 72  {.      if( Expr
1e527 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
1e528 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
1e529 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
1e52a 6e 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 65 66  nRef = pNC->nRef
1e52b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1e52c 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20  _OMIT_CHECK.    
1e52d 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43      if( pNC->isC
1e52e 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  heck ){.        
1e52f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1e530 67 28 70 50 61 72 73 65 2c 22 73 75 62 71 75 65  g(pParse,"subque
1e531 72 69 65 73 20 70 72 6f 68 69 62 69 74 65 64 20  ries prohibited 
1e532 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  in CHECK constra
1e533 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 20 20  ints");.        
1e534 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  }.#endif.       
1e535 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
1e536 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70  ct(pWalker, pExp
1e537 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20  r->x.pSelect);. 
1e538 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1e539 4e 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29  NC->nRef>=nRef )
1e53a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 52  ;.        if( nR
1e53b 65 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b  ef!=pNC->nRef ){
1e53c 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53  .          ExprS
1e53d 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  etProperty(pExpr
1e53e 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 3b  , EP_VarSelect);
1e53f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1e540 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1e541 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1e542 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
1e543 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
1e544 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 66  ABLE: {.      if
1e545 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29  ( pNC->isCheck )
1e546 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1e547 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1e548 2c 22 70 61 72 61 6d 65 74 65 72 73 20 70 72 6f  ,"parameters pro
1e549 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b  hibited in CHECK
1e54a 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a   constraints");.
1e54b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1e54c 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
1e54d 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  f.  }.  return (
1e54e 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
1e54f 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
1e550 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f  ocFailed) ? WRC_
1e551 41 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74  Abort : WRC_Cont
1e552 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  inue;.}../*.** p
1e553 45 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20  EList is a list 
1e554 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77  of expressions w
1e555 68 69 63 68 20 61 72 65 20 72 65 61 6c 6c 79 20  hich are really 
1e556 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
1e557 66 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c 45 43  f the.** a SELEC
1e558 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 45  T statement.  pE
1e559 20 69 73 20 61 20 74 65 72 6d 20 69 6e 20 61 6e   is a term in an
1e55a 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
1e55b 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a  UP BY clause..**
1e55c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
1e55d 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 70  ecks to see if p
1e55e 45 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69 64  E is a simple id
1e55f 65 6e 74 69 66 69 65 72 20 77 68 69 63 68 20 63  entifier which c
1e560 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 6f  orresponds.** to
1e561 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 20   the AS-name of 
1e562 6f 6e 65 20 6f 66 20 74 68 65 20 74 65 72 6d 73  one of the terms
1e563 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
1e564 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 69 74 20  on list.  If it 
1e565 69 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  is,.** this rout
1e566 69 6e 65 20 72 65 74 75 72 6e 20 61 6e 20 69 6e  ine return an in
1e567 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20  teger between 1 
1e568 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73  and N where N is
1e569 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
1e56a 2a 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 45  * elements in pE
1e56b 4c 69 73 74 2c 20 63 6f 72 72 65 73 70 6f 6e 64  List, correspond
1e56c 69 6e 67 20 74 6f 20 74 68 65 20 6d 61 74 63 68  ing to the match
1e56d 69 6e 67 20 65 6e 74 72 79 2e 20 20 49 66 20 74  ing entry.  If t
1e56e 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61  here is.** no ma
1e56f 74 63 68 2c 20 6f 72 20 69 66 20 70 45 20 69 73  tch, or if pE is
1e570 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 69 64   not a simple id
1e571 65 6e 74 69 66 69 65 72 2c 20 74 68 65 6e 20 74  entifier, then t
1e572 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72  his routine.** r
1e573 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 70  eturn 0..**.** p
1e574 45 4c 69 73 74 20 68 61 73 20 62 65 65 6e 20 72  EList has been r
1e575 65 73 6f 6c 76 65 64 2e 20 20 70 45 20 68 61 73  esolved.  pE has
1e576 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   not..*/.static 
1e577 69 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d  int resolveAsNam
1e578 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
1e579 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
1e57a 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65  ng context for e
1e57b 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f  rror messages */
1e57c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
1e57d 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ist,  /* List of
1e57e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20   expressions to 
1e57f 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  scan */.  Expr *
1e580 70 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pE           /* 
1e581 45 78 70 72 65 73 73 69 6f 6e 20 77 65 20 61 72  Expression we ar
1e582 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 74 63  e trying to matc
1e583 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  h */.){.  int i;
1e584 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e585 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
1e586 0a 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54  .  if( pE->op==T
1e587 4b 5f 49 44 20 7c 7c 20 28 70 45 2d 3e 6f 70 3d  K_ID || (pE->op=
1e588 3d 54 4b 5f 53 54 52 49 4e 47 20 26 26 20 70 45  =TK_STRING && pE
1e589 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 27 5c  ->token.z[0]!='\
1e58a 27 27 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  '') ){.    sqlit
1e58b 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
1e58c 3e 64 62 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  >db;.    char *z
1e58d 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  Col = sqlite3Nam
1e58e 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26  eFromToken(db, &
1e58f 70 45 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  pE->token);.    
1e590 69 66 28 20 7a 43 6f 6c 3d 3d 30 20 29 7b 0a 20  if( zCol==0 ){. 
1e591 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a       return -1;.
1e592 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
1e593 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; i<pEList->nEx
1e594 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
1e595 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69  char *zAs = pELi
1e596 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a  st->a[i].zName;.
1e597 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30        if( zAs!=0
1e598 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1e599 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30  mp(zAs, zCol)==0
1e59a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1e59b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43  te3DbFree(db, zC
1e59c 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ol);.        ret
1e59d 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d  urn i+1;.      }
1e59e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1e59f 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
1e5a0 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
1e5a1 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 20   0;.}../*.** pE 
1e5a2 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1e5a3 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68  an expression wh
1e5a4 69 63 68 20 69 73 20 61 20 73 69 6e 67 6c 65 20  ich is a single 
1e5a5 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 4f  term in the.** O
1e5a6 52 44 45 52 20 42 59 20 6f 66 20 61 20 63 6f 6d  RDER BY of a com
1e5a7 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20 20 54  pound SELECT.  T
1e5a8 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61  he expression ha
1e5a9 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e 61  s not been.** na
1e5aa 6d 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a  me resolved..**.
1e5ab 2a 2a 20 41 74 20 74 68 65 20 70 6f 69 6e 74 20  ** At the point 
1e5ac 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
1e5ad 63 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72 65 61  called, we alrea
1e5ae 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65  dy know that the
1e5af 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72  .** ORDER BY ter
1e5b0 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  m is not an inte
1e5b1 67 65 72 20 69 6e 64 65 78 20 69 6e 74 6f 20 74  ger index into t
1e5b2 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20  he result set.  
1e5b3 54 68 61 74 0a 2a 2a 20 63 61 73 65 20 69 73 20  That.** case is 
1e5b4 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 63  handled by the c
1e5b5 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a  alling routine..
1e5b6 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  **.** Attempt to
1e5b7 20 6d 61 74 63 68 20 70 45 20 61 67 61 69 6e 73   match pE agains
1e5b8 74 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c  t result set col
1e5b9 75 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65 66 74  umns in the left
1e5ba 2d 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43 54 20  -most.** SELECT 
1e5bb 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75  statement.  Retu
1e5bc 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 20 6f  rn the index i o
1e5bd 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 63  f the matching c
1e5be 6f 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e 20  olumn,.** as an 
1e5bf 69 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74 68  indication to th
1e5c0 65 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69 74  e caller that it
1e5c1 20 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79 20   should sort by 
1e5c2 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e  the i-th column.
1e5c3 0a 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73  .** The left-mos
1e5c4 74 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20  t column is 1.  
1e5c5 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1e5c6 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
1e5c7 65 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d  ed is the.** sam
1e5c8 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  e integer value 
1e5c9 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75 73  that would be us
1e5ca 65 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74  ed in the SQL st
1e5cb 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69 63  atement to indic
1e5cc 61 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d  ate.** the colum
1e5cd 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  n..**.** If ther
1e5ce 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 72  e is no match, r
1e5cf 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e  eturn 0.  Return
1e5d0 20 2d 31 20 69 66 20 61 6e 20 65 72 72 6f 72 20   -1 if an error 
1e5d1 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  occurs..*/.stati
1e5d2 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64  c int resolveOrd
1e5d3 65 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69  erByTermToExprLi
1e5d4 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
1e5d5 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
1e5d6 69 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ing context for 
1e5d7 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a  error messages *
1e5d8 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
1e5d9 65 63 74 2c 20 20 20 2f 2a 20 54 68 65 20 53 45  ect,   /* The SE
1e5da 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 77  LECT statement w
1e5db 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59  ith the ORDER BY
1e5dc 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70   clause */.  Exp
1e5dd 72 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20  r *pE           
1e5de 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20  /* The specific 
1e5df 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 2a 2f  ORDER BY term */
1e5e0 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
1e5e1 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1e5e2 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78   counter */.  Ex
1e5e3 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
1e5e4 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20   /* The columns 
1e5e5 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
1e5e6 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  t */.  NameConte
1e5e7 78 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d  xt nc;    /* Nam
1e5e8 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 72 65  e context for re
1e5e9 73 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a 0a 20  solving pE */.. 
1e5ea 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1e5eb 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45  ExprIsInteger(pE
1e5ec 2c 20 26 69 29 3d 3d 30 20 29 3b 0a 20 20 70 45  , &i)==0 );.  pE
1e5ed 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  List = pSelect->
1e5ee 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65  pEList;..  /* Re
1e5ef 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20  solve all names 
1e5f0 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  in the ORDER BY 
1e5f1 74 65 72 6d 20 65 78 70 72 65 73 73 69 6f 6e 0a  term expression.
1e5f2 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6e    */.  memset(&n
1e5f3 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e 63 29  c, 0, sizeof(nc)
1e5f4 29 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65 20 3d  );.  nc.pParse =
1e5f5 20 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e 70 53   pParse;.  nc.pS
1e5f6 72 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74  rcList = pSelect
1e5f7 2d 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70 45 4c  ->pSrc;.  nc.pEL
1e5f8 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20  ist = pEList;.  
1e5f9 6e 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b  nc.allowAgg = 1;
1e5fa 0a 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a  .  nc.nErr = 0;.
1e5fb 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
1e5fc 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 6e  olveExprNames(&n
1e5fd 63 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 73 71  c, pE) ){.    sq
1e5fe 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28  lite3ErrorClear(
1e5ff 70 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65 74  pParse);.    ret
1e600 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 0;.  }..  /*
1e601 20 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68   Try to match th
1e602 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65  e ORDER BY expre
1e603 73 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e  ssion against an
1e604 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a   expression.  **
1e605 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1e606 65 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 31  et.  Return an 1
1e607 2d 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66 20  -based index of 
1e608 74 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a  the matching.  *
1e609 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e 74  * result-set ent
1e60a 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ry..  */.  for(i
1e60b 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; i<pEList->nE
1e60c 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  xpr; i++){.    i
1e60d 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  f( sqlite3ExprCo
1e60e 6d 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b  mpare(pEList->a[
1e60f 69 5d 2e 70 45 78 70 72 2c 20 70 45 29 20 29 7b  i].pExpr, pE) ){
1e610 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b  .      return i+
1e611 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  1;.    }.  }..  
1e612 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c 20  /* If no match, 
1e613 72 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72  return 0. */.  r
1e614 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
1e615 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f 52  * Generate an OR
1e616 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
1e617 42 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d 72  BY term out-of-r
1e618 61 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73  ange error..*/.s
1e619 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c  tatic void resol
1e61a 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f  veOutOfRangeErro
1e61b 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
1e61c 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  se,         /* T
1e61d 68 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78 74  he error context
1e61e 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77   into which to w
1e61f 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 20 2a  rite the error *
1e620 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1e621 7a 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f  zType,     /* "O
1e622 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22  RDER" or "GROUP"
1e623 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20   */.  int i,    
1e624 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e625 54 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61 73  The index (1-bas
1e626 65 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d 20  ed) of the term 
1e627 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a  out of range */.
1e628 20 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20 20    int mx        
1e629 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67           /* Larg
1e62a 65 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65 20  est permissible 
1e62b 76 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29 7b  value of i */.){
1e62c 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
1e62d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
1e62e 22 25 72 20 25 73 20 42 59 20 74 65 72 6d 20 6f  "%r %s BY term o
1e62f 75 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68  ut of range - sh
1e630 6f 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22 62  ould be ".    "b
1e631 65 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22  etween 1 and %d"
1e632 2c 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29 3b  , i, zType, mx);
1e633 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a  .}../*.** Analyz
1e634 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  e the ORDER BY c
1e635 6c 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70 6f  lause in a compo
1e636 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65  und SELECT state
1e637 6d 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a 2a  ment.   Modify.*
1e638 2a 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74  * each term of t
1e639 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
1e63a 73 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74  se is a constant
1e63b 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e   integer between
1e63c 20 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65 72   1.** and N wher
1e63d 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
1e63e 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1e63f 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  the compound SEL
1e640 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52  ECT..**.** ORDER
1e641 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61   BY terms that a
1e642 72 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e  re already an in
1e643 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20  teger between 1 
1e644 61 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e 6d  and N are.** unm
1e645 6f 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52 20  odified.  ORDER 
1e646 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72  BY terms that ar
1e647 65 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73 69  e integers outsi
1e648 64 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66 0a  de the range of.
1e649 2a 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20 67  ** 1 through N g
1e64a 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72  enerate an error
1e64b 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  .  ORDER BY term
1e64c 73 20 74 68 61 74 20 61 72 65 20 65 78 70 72 65  s that are expre
1e64d 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61  ssions.** are ma
1e64e 74 63 68 65 64 20 61 67 61 69 6e 73 74 20 72 65  tched against re
1e64f 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73  sult set express
1e650 69 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64  ions of compound
1e651 20 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69 6e   SELECT.** begin
1e652 6e 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c 65  ning with the le
1e653 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 61  ft-most SELECT a
1e654 6e 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72  nd working towar
1e655 64 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a 20  d the right..** 
1e656 41 74 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  At the first mat
1e657 63 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42 59  ch, the ORDER BY
1e658 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
1e659 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0a  ransformed into.
1e65a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63  ** the integer c
1e65b 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a  olumn number..**
1e65c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1e65d 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
1e65e 73 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  seen..*/.static 
1e65f 69 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f  int resolveCompo
1e660 75 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50 61  undOrderBy(.  Pa
1e661 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1e662 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1e663 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65  ontext.  Leave e
1e664 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65  rror messages he
1e665 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  re */.  Select *
1e666 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f 2a  pSelect       /*
1e667 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
1e668 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67  ement containing
1e669 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a 2f   the ORDER BY */
1e66a 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45  .){.  int i;.  E
1e66b 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
1e66c 79 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  y;.  ExprList *p
1e66d 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33  EList;.  sqlite3
1e66e 20 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 65   *db;.  int more
1e66f 54 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f 72  ToDo = 1;..  pOr
1e670 64 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74 2d  derBy = pSelect-
1e671 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28  >pOrderBy;.  if(
1e672 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72   pOrderBy==0 ) r
1e673 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20  eturn 0;.  db = 
1e674 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 20  pParse->db;.#if 
1e675 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
1e676 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  N.  if( pOrderBy
1e677 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d  ->nExpr>db->aLim
1e678 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1e679 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73  COLUMN] ){.    s
1e67a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1e67b 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
1e67c 20 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52 20   terms in ORDER 
1e67d 42 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20  BY clause");.   
1e67e 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23   return 1;.  }.#
1e67f 65 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b  endif.  for(i=0;
1e680 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   i<pOrderBy->nEx
1e681 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4f  pr; i++){.    pO
1e682 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e  rderBy->a[i].don
1e683 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53 65  e = 0;.  }.  pSe
1e684 6c 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  lect->pNext = 0;
1e685 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63  .  while( pSelec
1e686 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  t->pPrior ){.   
1e687 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72   pSelect->pPrior
1e688 2d 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65 63  ->pNext = pSelec
1e689 74 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d  t;.    pSelect =
1e68a 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72   pSelect->pPrior
1e68b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70  ;.  }.  while( p
1e68c 53 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54 6f  Select && moreTo
1e68d 44 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  Do ){.    struct
1e68e 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1e68f 70 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 54  pItem;.    moreT
1e690 6f 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45 4c  oDo = 0;.    pEL
1e691 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ist = pSelect->p
1e692 45 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72  EList;.    asser
1e693 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
1e694 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
1e695 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
1e696 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
1e697 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
1e698 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  {.      int iCol
1e699 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78 70   = -1;.      Exp
1e69a 72 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20 20  r *pE, *pDup;.  
1e69b 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 64      if( pItem->d
1e69c 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  one ) continue;.
1e69d 20 20 20 20 20 20 70 45 20 3d 20 70 49 74 65 6d        pE = pItem
1e69e 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69  ->pExpr;.      i
1e69f 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  f( sqlite3ExprIs
1e6a0 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f  Integer(pE, &iCo
1e6a1 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  l) ){.        if
1e6a2 28 20 69 43 6f 6c 3c 30 20 7c 7c 20 69 43 6f 6c  ( iCol<0 || iCol
1e6a3 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  >pEList->nExpr )
1e6a4 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f  {.          reso
1e6a5 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72  lveOutOfRangeErr
1e6a6 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45  or(pParse, "ORDE
1e6a7 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d  R", i+1, pEList-
1e6a8 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20  >nExpr);.       
1e6a9 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1e6aa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1e6ab 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c  se{.        iCol
1e6ac 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65   = resolveAsName
1e6ad 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c  (pParse, pEList,
1e6ae 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66   pE);.        if
1e6af 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20  ( iCol==0 ){.   
1e6b0 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71         pDup = sq
1e6b1 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
1e6b2 20 70 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20   pE, 0);.       
1e6b3 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c     if( !db->mall
1e6b4 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
1e6b5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70          assert(p
1e6b6 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Dup);.          
1e6b7 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65    iCol = resolve
1e6b8 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70  OrderByTermToExp
1e6b9 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53  rList(pParse, pS
1e6ba 65 6c 65 63 74 2c 20 70 44 75 70 29 3b 0a 20 20  elect, pDup);.  
1e6bb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e6bc 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
1e6bd 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b  elete(db, pDup);
1e6be 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1e6bf 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
1e6c0 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1e6c1 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 1;.        }. 
1e6c2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
1e6c3 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20   iCol>0 ){.     
1e6c4 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
1e6c5 6c 20 3d 20 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20  l = pE->pColl;. 
1e6c6 20 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73         int flags
1e6c7 20 3d 20 70 45 2d 3e 66 6c 61 67 73 20 26 20 45   = pE->flags & E
1e6c8 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20  P_ExpCollate;.  
1e6c9 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1e6ca 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 29 3b  rDelete(db, pE);
1e6cb 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  .        pItem->
1e6cc 70 45 78 70 72 20 3d 20 70 45 20 3d 20 73 71 6c  pExpr = pE = sql
1e6cd 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
1e6ce 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 2c 20 30  INTEGER, 0, 0, 0
1e6cf 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
1e6d0 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  E==0 ) return 1;
1e6d1 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f  .        pE->pCo
1e6d2 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  ll = pColl;.    
1e6d3 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d      pE->flags |=
1e6d4 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66   EP_IntValue | f
1e6d5 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 45  lags;.        pE
1e6d6 2d 3e 69 54 61 62 6c 65 20 3d 20 69 43 6f 6c 3b  ->iTable = iCol;
1e6d7 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  .        pItem->
1e6d8 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c  iCol = (u16)iCol
1e6d9 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d  ;.        pItem-
1e6da 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20  >done = 1;.     
1e6db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1e6dc 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 20 20  moreToDo = 1;.  
1e6dd 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1e6de 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
1e6df 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  t->pNext;.  }.  
1e6e0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65  for(i=0; i<pOrde
1e6e1 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  rBy->nExpr; i++)
1e6e2 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65 72  {.    if( pOrder
1e6e3 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d 3d 30  By->a[i].done==0
1e6e4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1e6e5 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1e6e6 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59 20 74  , "%r ORDER BY t
1e6e7 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  erm does not mat
1e6e8 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20 20 20  ch any ".       
1e6e9 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69 6e 20       "column in 
1e6ea 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 22 2c  the result set",
1e6eb 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72 65 74   i+1);.      ret
1e6ec 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
1e6ed 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1e6ee 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76 65 72  /*.** Check ever
1e6ef 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 4f 52  y term in the OR
1e6f0 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
1e6f1 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72  BY clause pOrder
1e6f2 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53 45 4c  By of.** the SEL
1e6f3 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 53  ECT statement pS
1e6f4 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79 20 74  elect.  If any t
1e6f5 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e 63 65  erm is reference
1e6f6 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c 74 20   to a.** result 
1e6f7 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 28  set expression (
1e6f8 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  as determined by
1e6f9 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61 2e   the ExprList.a.
1e6fa 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a 20 74  iCol field).** t
1e6fb 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 61 74  hen convert that
1e6fc 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63 6f 70   term into a cop
1e6fd 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70  y of the corresp
1e6fe 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65  onding result se
1e6ff 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a  t.** column..**.
1e700 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73  ** If any errors
1e701 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20 61   are detected, a
1e702 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  dd an error mess
1e703 61 67 65 20 74 6f 20 70 50 61 72 73 65 20 61 6e  age to pParse an
1e704 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d  d.** return non-
1e705 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65  zero.  Return ze
1e706 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72 73 20  ro if no errors 
1e707 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c  are seen..*/.SQL
1e708 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1e709 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
1e70a 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 50 61  derGroupBy(.  Pa
1e70b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1e70c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1e70d 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65  ontext.  Leave e
1e70e 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65  rror messages he
1e70f 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  re */.  Select *
1e710 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a  pSelect,      /*
1e711 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
1e712 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67  ement containing
1e713 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a 20   the clause */. 
1e714 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
1e715 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20 4f 52  rBy,   /* The OR
1e716 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
1e717 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20  BY clause to be 
1e718 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20 63  processed */.  c
1e719 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65  onst char *zType
1e71a 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20       /* "ORDER" 
1e71b 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 29 7b  or "GROUP" */.){
1e71c 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69  .  int i;.  sqli
1e71d 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1e71e 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69 73 74  ->db;.  ExprList
1e71f 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 75   *pEList;.  stru
1e720 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
1e721 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20   *pItem;..  if( 
1e722 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 70  pOrderBy==0 || p
1e723 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
1e724 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
1e725 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d   0;.#if SQLITE_M
1e726 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20  AX_COLUMN.  if( 
1e727 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e  pOrderBy->nExpr>
1e728 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1e729 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20  E_LIMIT_COLUMN] 
1e72a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1e72b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1e72c 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69  too many terms i
1e72d 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65 22 2c  n %s BY clause",
1e72e 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72 65 74   zType);.    ret
1e72f 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69  urn 1;.  }.#endi
1e730 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65  f.  pEList = pSe
1e731 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20  lect->pEList;.  
1e732 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d  assert( pEList!=
1e733 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33  0 );  /* sqlite3
1e734 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75 61 72  SelectNew() guar
1e735 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f 0a 20  antees this */. 
1e736 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
1e737 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70  pOrderBy->a; i<p
1e738 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20  OrderBy->nExpr; 
1e739 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1e73a 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43     if( pItem->iC
1e73b 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ol ){.      if( 
1e73c 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45 4c 69  pItem->iCol>pELi
1e73d 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
1e73e 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f       resolveOutO
1e73f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72  fRangeError(pPar
1e740 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20  se, zType, i+1, 
1e741 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a  pEList->nExpr);.
1e742 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
1e743 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1e744 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61  resolveAlias(pPa
1e745 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 49 74  rse, pEList, pIt
1e746 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49 74 65  em->iCol-1, pIte
1e747 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 70 65 29  m->pExpr, zType)
1e748 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1e749 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
1e74a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20   pOrderBy is an 
1e74b 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
1e74c 50 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20 53  P BY clause in S
1e74d 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1e74e 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 68 65 20  pSelect..** The 
1e74f 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20  Name context of 
1e750 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
1e751 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 20 7a 54  ment is pNC.  zT
1e752 79 70 65 20 69 73 20 65 69 74 68 65 72 0a 2a 2a  ype is either.**
1e753 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f   "ORDER" or "GRO
1e754 55 50 22 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  UP" depending on
1e755 20 77 68 69 63 68 20 74 79 70 65 20 6f 66 20 63   which type of c
1e756 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 20 69  lause pOrderBy i
1e757 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
1e758 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73 20 65  utine resolves e
1e759 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ach term of the 
1e75a 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e 20 65  clause into an e
1e75b 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 49 66  xpression..** If
1e75c 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74 65   the order-by te
1e75d 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  rm is an integer
1e75e 20 49 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64   I between 1 and
1e75f 20 4e 20 28 77 68 65 72 65 20 4e 20 69 73 20 74   N (where N is t
1e760 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
1e761 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
1e762 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
1e763 20 53 45 4c 45 43 54 29 20 74 68 65 6e 20 74 68   SELECT) then th
1e764 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  e expression.** 
1e765 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 74 69 6f  in the resolutio
1e766 6e 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  n is a copy of t
1e767 68 65 20 49 2d 74 68 20 72 65 73 75 6c 74 2d 73  he I-th result-s
1e768 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  et expression.  
1e769 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 65 72 2d  If.** the order-
1e76a 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69 64  by term is an id
1e76b 65 6e 74 69 66 79 20 74 68 61 74 20 63 6f 72 72  entify that corr
1e76c 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 41  esponds to the A
1e76d 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61 20 72  S-name of.** a r
1e76e 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73  esult-set expres
1e76f 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74  sion, then the t
1e770 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 74 6f 20  erm resolves to 
1e771 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a  a copy of the.**
1e772 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72   result-set expr
1e773 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69  ession.  Otherwi
1e774 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 69  se, the expressi
1e775 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 64 20 69  on is resolved i
1e776 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c 20 77  n.** the usual w
1e777 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c 69 74  ay - using sqlit
1e778 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
1e779 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  es()..**.** This
1e77a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
1e77b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1e77c 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72 6f 72  rrors.  If error
1e77d 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0a 2a 2a  s occur, then.**
1e77e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
1e77f 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6d 69  error message mi
1e780 67 68 74 20 62 65 20 6c 65 66 74 20 69 6e 20 70  ght be left in p
1e781 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 65 72 72  Parse.  (OOM err
1e782 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 65 64 2e  ors.** excepted.
1e783 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ).*/.static int 
1e784 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75  resolveOrderGrou
1e785 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  pBy(.  NameConte
1e786 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 2f 2a 20  xt *pNC,     /* 
1e787 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74  The name context
1e788 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
1e789 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65  tatement */.  Se
1e78a 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
1e78b 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
1e78c 54 20 73 74 61 74 65 6d 65 6e 74 20 68 6f 6c 64  T statement hold
1e78d 69 6e 67 20 70 4f 72 64 65 72 42 79 20 2a 2f 0a  ing pOrderBy */.
1e78e 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
1e78f 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20 4f 52  erBy,   /* An OR
1e790 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
1e791 42 59 20 63 6c 61 75 73 65 20 74 6f 20 72 65 73  BY clause to res
1e792 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  olve */.  const 
1e793 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 20  char *zType     
1e794 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44 45 52  /* Either "ORDER
1e795 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20 61 73  " or "GROUP", as
1e796 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a   appropriate */.
1e797 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
1e798 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e799 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1e79a 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  ter */.  int iCo
1e79b 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
1e79c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
1e79d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 74  n number */.  st
1e79e 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
1e79f 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 2f 2a 20  em *pItem;   /* 
1e7a0 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52  A term of the OR
1e7a1 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
1e7a2 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1e7a3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1e7a4 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
1e7a5 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  text */.  int nR
1e7a6 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20  esult;          
1e7a7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1e7a8 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74  er of terms in t
1e7a9 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
1e7aa 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  ..  if( pOrderBy
1e7ab 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1e7ac 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 53 65 6c    nResult = pSel
1e7ad 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  ect->pEList->nEx
1e7ae 70 72 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70  pr;.  pParse = p
1e7af 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 66 6f  NC->pParse;.  fo
1e7b0 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72  r(i=0, pItem=pOr
1e7b1 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64  derBy->a; i<pOrd
1e7b2 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  erBy->nExpr; i++
1e7b3 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
1e7b4 45 78 70 72 20 2a 70 45 20 3d 20 70 49 74 65 6d  Expr *pE = pItem
1e7b5 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 43 6f  ->pExpr;.    iCo
1e7b6 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d  l = resolveAsNam
1e7b7 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  e(pParse, pSelec
1e7b8 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a  t->pEList, pE);.
1e7b9 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29      if( iCol<0 )
1e7ba 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1e7bb 3b 20 20 2f 2a 20 4f 4f 4d 20 65 72 72 6f 72 20  ;  /* OOM error 
1e7bc 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  */.    }.    if(
1e7bd 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20   iCol>0 ){.     
1e7be 20 2f 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d   /* If an AS-nam
1e7bf 65 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  e match is found
1e7c0 2c 20 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 45  , mark this ORDE
1e7c1 52 20 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62  R BY column as b
1e7c2 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 20  eing.      ** a 
1e7c3 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c  copy of the iCol
1e7c4 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 63  -th result-set c
1e7c5 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 73 75 62 73  olumn.  The subs
1e7c6 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20  equent call to. 
1e7c7 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 52       ** sqlite3R
1e7c8 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
1e7c9 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72  By() will conver
1e7ca 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  t the expression
1e7cb 20 74 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20 63   to a.      ** c
1e7cc 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d  opy of the iCol-
1e7cd 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78  th result-set ex
1e7ce 70 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20  pression. */.   
1e7cf 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d     pItem->iCol =
1e7d0 20 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20   (u16)iCol;.    
1e7d1 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
1e7d2 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
1e7d3 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70  3ExprIsInteger(p
1e7d4 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20  E, &iCol) ){.   
1e7d5 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
1e7d6 42 59 20 74 65 72 6d 20 69 73 20 61 6e 20 69 6e  BY term is an in
1e7d7 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 20  teger constant. 
1e7d8 20 41 67 61 69 6e 2c 20 73 65 74 20 74 68 65 20   Again, set the 
1e7d9 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20  column.      ** 
1e7da 6e 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 73  number so that s
1e7db 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64  qlite3ResolveOrd
1e7dc 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c  erGroupBy() will
1e7dd 20 63 6f 6e 76 65 72 74 20 74 68 65 0a 20 20 20   convert the.   
1e7de 20 20 20 2a 2a 20 6f 72 64 65 72 2d 62 79 20 74     ** order-by t
1e7df 65 72 6d 20 74 6f 20 61 20 63 6f 70 79 20 6f 66  erm to a copy of
1e7e0 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20   the result-set 
1e7e1 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
1e7e2 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 31 20 29      if( iCol<1 )
1e7e3 7b 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76  {.        resolv
1e7e4 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72  eOutOfRangeError
1e7e5 28 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c 20  (pParse, zType, 
1e7e6 69 2b 31 2c 20 6e 52 65 73 75 6c 74 29 3b 0a 20  i+1, nResult);. 
1e7e7 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b         return 1;
1e7e8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1e7e9 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31  Item->iCol = (u1
1e7ea 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f  6)iCol;.      co
1e7eb 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20  ntinue;.    }.. 
1e7ec 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c     /* Otherwise,
1e7ed 20 74 72 65 61 74 20 74 68 65 20 4f 52 44 45 52   treat the ORDER
1e7ee 20 42 59 20 74 65 72 6d 20 61 73 20 61 6e 20 6f   BY term as an o
1e7ef 72 64 69 6e 61 72 79 20 65 78 70 72 65 73 73 69  rdinary expressi
1e7f0 6f 6e 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d  on */.    pItem-
1e7f1 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 69  >iCol = 0;.    i
1e7f2 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
1e7f3 65 45 78 70 72 4e 61 6d 65 73 28 70 4e 43 2c 20  eExprNames(pNC, 
1e7f4 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  pE) ){.      ret
1e7f5 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
1e7f6 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1e7f7 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f  3ResolveOrderGro
1e7f8 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 53 65  upBy(pParse, pSe
1e7f9 6c 65 63 74 2c 20 70 4f 72 64 65 72 42 79 2c 20  lect, pOrderBy, 
1e7fa 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  zType);.}../*.**
1e7fb 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69   Resolve names i
1e7fc 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  n the SELECT sta
1e7fd 74 65 6d 65 6e 74 20 70 20 61 6e 64 20 61 6c 6c  tement p and all
1e7fe 20 6f 66 20 69 74 73 20 64 65 73 63 65 6e 64 65   of its descende
1e7ff 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nts..*/.static i
1e800 6e 74 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74  nt resolveSelect
1e801 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61  Step(Walker *pWa
1e802 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
1e803 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  {.  NameContext 
1e804 2a 70 4f 75 74 65 72 4e 43 3b 20 20 2f 2a 20 43  *pOuterNC;  /* C
1e805 6f 6e 74 65 78 74 20 74 68 61 74 20 63 6f 6e 74  ontext that cont
1e806 61 69 6e 73 20 74 68 69 73 20 53 45 4c 45 43 54  ains this SELECT
1e807 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
1e808 74 20 73 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a  t sNC;        /*
1e809 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66   Name context of
1e80a 20 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a   this SELECT */.
1e80b 20 20 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64    int isCompound
1e80c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
1e80d 65 20 69 66 20 70 20 69 73 20 61 20 63 6f 6d 70  e if p is a comp
1e80e 6f 75 6e 64 20 73 65 6c 65 63 74 20 2a 2f 0a 20  ound select */. 
1e80f 20 69 6e 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b 20   int nCompound; 
1e810 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1e811 65 72 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 74  er of compound t
1e812 65 72 6d 73 20 70 72 6f 63 65 73 73 65 64 20 73  erms processed s
1e813 6f 20 66 61 72 20 2a 2f 0a 20 20 50 61 72 73 65  o far */.  Parse
1e814 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
1e815 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
1e816 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
1e817 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20  ist *pEList;    
1e818 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 73 65 74     /* Result set
1e819 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1e81a 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
1e81b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e81c 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
1e81d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72  .  ExprList *pGr
1e81e 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a 20 54 68  oupBy;     /* Th
1e81f 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  e GROUP BY claus
1e820 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  e */.  Select *p
1e821 4c 65 66 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f  Leftmost;      /
1e822 2a 20 4c 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53  * Left-most of S
1e823 45 4c 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f  ELECT of a compo
1e824 75 6e 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  und */.  sqlite3
1e825 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
1e826 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
1e827 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a 20  nection */.  .. 
1e828 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
1e829 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61  .  if( p->selFla
1e82a 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64  gs & SF_Resolved
1e82b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
1e82c 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20  RC_Prune;.  }.  
1e82d 70 4f 75 74 65 72 4e 43 20 3d 20 70 57 61 6c 6b  pOuterNC = pWalk
1e82e 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 61  er->u.pNC;.  pPa
1e82f 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70  rse = pWalker->p
1e830 50 61 72 73 65 3b 0a 20 20 64 62 20 3d 20 70 50  Parse;.  db = pP
1e831 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20  arse->db;..  /* 
1e832 4e 6f 72 6d 61 6c 6c 79 20 73 71 6c 69 74 65 33  Normally sqlite3
1e833 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 77  SelectExpand() w
1e834 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 66 69  ill be called fi
1e835 72 73 74 20 61 6e 64 20 77 69 6c 6c 20 68 61 76  rst and will hav
1e836 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65  e.  ** already e
1e837 78 70 61 6e 64 65 64 20 74 68 69 73 20 53 45 4c  xpanded this SEL
1e838 45 43 54 2e 20 20 48 6f 77 65 76 65 72 2c 20 69  ECT.  However, i
1e839 66 20 74 68 69 73 20 69 73 20 61 20 73 75 62 71  f this is a subq
1e83a 75 65 72 79 20 77 69 74 68 69 6e 0a 20 20 2a 2a  uery within.  **
1e83b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20   an expression, 
1e83c 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
1e83d 70 72 4e 61 6d 65 73 28 29 20 77 69 6c 6c 20 62  prNames() will b
1e83e 65 20 63 61 6c 6c 65 64 20 77 69 74 68 6f 75 74  e called without
1e83f 20 61 0a 20 20 2a 2a 20 70 72 69 6f 72 20 63 61   a.  ** prior ca
1e840 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 53 65 6c  ll to sqlite3Sel
1e841 65 63 74 45 78 70 61 6e 64 28 29 2e 20 20 57 68  ectExpand().  Wh
1e842 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c  en that happens,
1e843 20 6c 65 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65   let.  ** sqlite
1e844 33 53 65 6c 65 63 74 50 72 65 70 28 29 20 64 6f  3SelectPrep() do
1e845 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 72 6f 63   all of the proc
1e846 65 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20  essing for this 
1e847 53 45 4c 45 43 54 2e 0a 20 20 2a 2a 20 73 71 6c  SELECT..  ** sql
1e848 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 29  ite3SelectPrep()
1e849 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74   will invoke bot
1e84a 68 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  h sqlite3SelectE
1e84b 78 70 61 6e 64 28 29 20 61 6e 64 0a 20 20 2a 2a  xpand() and.  **
1e84c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e   this routine in
1e84d 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64   the correct ord
1e84e 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  er..  */.  if( (
1e84f 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
1e850 5f 45 78 70 61 6e 64 65 64 29 3d 3d 30 20 29 7b  _Expanded)==0 ){
1e851 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .    sqlite3Sele
1e852 63 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70  ctPrep(pParse, p
1e853 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20  , pOuterNC);.   
1e854 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d   return (pParse-
1e855 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
1e856 6c 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43  locFailed) ? WRC
1e857 5f 41 62 6f 72 74 20 3a 20 57 52 43 5f 50 72 75  _Abort : WRC_Pru
1e858 6e 65 3b 0a 20 20 7d 0a 0a 20 20 69 73 43 6f 6d  ne;.  }..  isCom
1e859 70 6f 75 6e 64 20 3d 20 70 2d 3e 70 50 72 69 6f  pound = p->pPrio
1e85a 72 21 3d 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 6e  r!=0;.  nCompoun
1e85b 64 20 3d 20 30 3b 0a 20 20 70 4c 65 66 74 6d 6f  d = 0;.  pLeftmo
1e85c 73 74 20 3d 20 70 3b 0a 20 20 77 68 69 6c 65 28  st = p;.  while(
1e85d 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74   p ){.    assert
1e85e 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  ( (p->selFlags &
1e85f 20 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30   SF_Expanded)!=0
1e860 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1e861 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
1e862 46 5f 52 65 73 6f 6c 76 65 64 29 3d 3d 30 20 29  F_Resolved)==0 )
1e863 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67  ;.    p->selFlag
1e864 73 20 7c 3d 20 53 46 5f 52 65 73 6f 6c 76 65 64  s |= SF_Resolved
1e865 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76  ;..    /* Resolv
1e866 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  e the expression
1e867 73 20 69 6e 20 74 68 65 20 4c 49 4d 49 54 20 61  s in the LIMIT a
1e868 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  nd OFFSET clause
1e869 73 2e 20 54 68 65 73 65 0a 20 20 20 20 2a 2a 20  s. These.    ** 
1e86a 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20  are not allowed 
1e86b 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20  to refer to any 
1e86c 6e 61 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 61  names, so pass a
1e86d 6e 20 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74  n empty NameCont
1e86e 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ext..    */.    
1e86f 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
1e870 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
1e871 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
1e872 50 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73  Parse;.    if( s
1e873 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
1e874 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e  rNames(&sNC, p->
1e875 70 4c 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20  pLimit) ||.     
1e876 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76     sqlite3Resolv
1e877 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
1e878 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a   p->pOffset) ){.
1e879 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1e87a 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
1e87b 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 70 20 74  .    /* Set up t
1e87c 68 65 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f  he local name-co
1e87d 6e 74 65 78 74 20 74 6f 20 70 61 73 73 20 74 6f  ntext to pass to
1e87e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
1e87f 78 70 72 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20  xprNames() to.  
1e880 20 20 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68 65    ** resolve the
1e881 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72   result-set expr
1e882 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 20  ession list..   
1e883 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f   */.    sNC.allo
1e884 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 73 4e  wAgg = 1;.    sN
1e885 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e  C.pSrcList = p->
1e886 70 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e  pSrc;.    sNC.pN
1e887 65 78 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a  ext = pOuterNC;.
1e888 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76    .    /* Resolv
1e889 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72  e names in the r
1e88a 65 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20  esult set. */.  
1e88b 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45    pEList = p->pE
1e88c 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74  List;.    assert
1e88d 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20  ( pEList!=0 );. 
1e88e 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45     for(i=0; i<pE
1e88f 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
1e890 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
1e891 58 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d  X = pEList->a[i]
1e892 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66  .pExpr;.      if
1e893 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
1e894 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
1e895 70 58 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  pX) ){.        r
1e896 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1e897 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1e898 20 0a 20 20 20 20 2f 2a 20 52 65 63 75 72 73 69   .    /* Recursi
1e899 76 65 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d  vely resolve nam
1e89a 65 73 20 69 6e 20 61 6c 6c 20 73 75 62 71 75 65  es in all subque
1e89b 72 69 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ries.    */.    
1e89c 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53  for(i=0; i<p->pS
1e89d 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  rc->nSrc; i++){.
1e89e 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63        struct Src
1e89f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1e8a0 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69   = &p->pSrc->a[i
1e8a1 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  ];.      if( pIt
1e8a2 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  em->pSelect ){. 
1e8a3 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
1e8a4 72 20 2a 7a 53 61 76 65 64 43 6f 6e 74 65 78 74  r *zSavedContext
1e8a5 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68   = pParse->zAuth
1e8a6 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20  Context;.       
1e8a7 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d   if( pItem->zNam
1e8a8 65 20 29 20 70 50 61 72 73 65 2d 3e 7a 41 75 74  e ) pParse->zAut
1e8a9 68 43 6f 6e 74 65 78 74 20 3d 20 70 49 74 65 6d  hContext = pItem
1e8aa 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ->zName;.       
1e8ab 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53   sqlite3ResolveS
1e8ac 65 6c 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73  electNames(pPars
1e8ad 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  e, pItem->pSelec
1e8ae 74 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20  t, pOuterNC);.  
1e8af 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41        pParse->zA
1e8b0 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61  uthContext = zSa
1e8b1 76 65 64 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  vedContext;.    
1e8b2 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
1e8b3 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
1e8b4 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
1e8b5 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
1e8b6 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
1e8b7 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
1e8b8 65 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66  e no aggregate f
1e8b9 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20  unctions in the 
1e8ba 72 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20  result-set, and 
1e8bb 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20 20  no GROUP BY .   
1e8bc 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20   ** expression, 
1e8bd 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67  do not allow agg
1e8be 72 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 6f  regates in any o
1e8bf 66 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72  f the other expr
1e8c0 65 73 73 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a  essions..    */.
1e8c1 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e      assert( (p->
1e8c2 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67  selFlags & SF_Ag
1e8c3 67 72 65 67 61 74 65 29 3d 3d 30 20 29 3b 0a 20  gregate)==0 );. 
1e8c4 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d     pGroupBy = p-
1e8c5 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 69  >pGroupBy;.    i
1e8c6 66 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20 73  f( pGroupBy || s
1e8c7 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20 20  NC.hasAgg ){.   
1e8c8 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c     p->selFlags |
1e8c9 3d 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b 0a  = SF_Aggregate;.
1e8ca 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e8cb 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20   sNC.allowAgg = 
1e8cc 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  0;.    }.  .    
1e8cd 2f 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20 63  /* If a HAVING c
1e8ce 6c 61 75 73 65 20 69 73 20 70 72 65 73 65 6e 74  lause is present
1e8cf 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73  , then there mus
1e8d0 74 20 62 65 20 61 20 47 52 4f 55 50 20 42 59 20  t be a GROUP BY 
1e8d1 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  clause..    */. 
1e8d2 20 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e     if( p->pHavin
1e8d3 67 20 26 26 20 21 70 47 72 6f 75 70 42 79 20 29  g && !pGroupBy )
1e8d4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1e8d5 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1e8d6 22 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  "a GROUP BY clau
1e8d7 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62  se is required b
1e8d8 65 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b 0a  efore HAVING");.
1e8d9 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1e8da 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
1e8db 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20  .    /* Add the 
1e8dc 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  expression list 
1e8dd 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74  to the name-cont
1e8de 65 78 74 20 62 65 66 6f 72 65 20 70 61 72 73 69  ext before parsi
1e8df 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74  ng the.    ** ot
1e8e0 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20  her expressions 
1e8e1 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  in the SELECT st
1e8e2 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  atement. This is
1e8e3 20 73 6f 20 74 68 61 74 0a 20 20 20 20 2a 2a 20   so that.    ** 
1e8e4 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74  expressions in t
1e8e5 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1e8e6 28 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65 72  (etc.) can refer
1e8e7 20 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20   to expressions 
1e8e8 62 79 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73 65  by.    ** aliase
1e8e9 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
1e8ea 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  set..    **.    
1e8eb 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a 20  ** Minor point: 
1e8ec 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
1e8ed 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78  ase, then the ex
1e8ee 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62 65  pression will be
1e8ef 0a 20 20 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75  .    ** re-evalu
1e8f0 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 65  ated for each re
1e8f1 66 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20  ference to it.. 
1e8f2 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 45     */.    sNC.pE
1e8f3 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74  List = p->pEList
1e8f4 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1e8f5 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
1e8f6 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72  s(&sNC, p->pWher
1e8f7 65 29 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c  e) ||.       sql
1e8f8 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
1e8f9 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48  ames(&sNC, p->pH
1e8fa 61 76 69 6e 67 29 0a 20 20 20 20 29 7b 0a 20 20  aving).    ){.  
1e8fb 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
1e8fc 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  bort;.    }..   
1e8fd 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
1e8fe 20 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 6c   and GROUP BY cl
1e8ff 61 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72 65  auses may not re
1e900 66 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e 0a  fer to terms in.
1e901 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65      ** outer que
1e902 72 69 65 73 20 0a 20 20 20 20 2a 2f 0a 20 20 20  ries .    */.   
1e903 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a   sNC.pNext = 0;.
1e904 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67      sNC.allowAgg
1e905 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 72   = 1;..    /* Pr
1e906 6f 63 65 73 73 20 74 68 65 20 4f 52 44 45 52 20  ocess the ORDER 
1e907 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73 69  BY clause for si
1e908 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20 73  ngleton SELECT s
1e909 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a  tatements..    *
1e90a 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
1e90b 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f 75  lause for compou
1e90c 6e 64 73 20 53 45 4c 45 43 54 20 73 74 61 74 65  nds SELECT state
1e90d 6d 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65 64  ments is handled
1e90e 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 61  .    ** below, a
1e90f 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20  fter all of the 
1e910 72 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72 20  result-sets for 
1e911 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d 65  all of the eleme
1e912 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68  nts of.    ** th
1e913 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20  e compound have 
1e914 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20  been resolved.. 
1e915 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69     */.    if( !i
1e916 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 73  sCompound && res
1e917 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
1e918 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f 72  (&sNC, p, p->pOr
1e919 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 20  derBy, "ORDER") 
1e91a 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1e91b 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
1e91c 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
1e91d 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1e91e 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1e91f 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  ort;.    }.  .  
1e920 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65    /* Resolve the
1e921 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1e922 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74  .  At the same t
1e923 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 0a  ime, make sure .
1e924 20 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55 50      ** the GROUP
1e925 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20   BY clause does 
1e926 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67 72  not contain aggr
1e927 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e  egate functions.
1e928 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1e929 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20  pGroupBy ){.    
1e92a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1e92b 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
1e92c 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 65     .      if( re
1e92d 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
1e92e 79 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f 75  y(&sNC, p, pGrou
1e92f 70 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c 7c  pBy, "GROUP") ||
1e930 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1e931 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  d ){.        ret
1e932 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1e933 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
1e934 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f  (i=0, pItem=pGro
1e935 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75  upBy->a; i<pGrou
1e936 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  pBy->nExpr; i++,
1e937 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1e938 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
1e939 6f 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70 45  operty(pItem->pE
1e93a 78 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a  xpr, EP_Agg) ){.
1e93b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1e93c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1e93d 2c 20 22 61 67 67 72 65 67 61 74 65 20 66 75 6e  , "aggregate fun
1e93e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61  ctions are not a
1e93f 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20 20  llowed in ".    
1e940 20 20 20 20 20 20 20 20 20 20 22 74 68 65 20 47            "the G
1e941 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22 29  ROUP BY clause")
1e942 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
1e943 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1e944 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1e945 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64      }..    /* Ad
1e946 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78  vance to the nex
1e947 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6f  t term of the co
1e948 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20  mpound.    */.  
1e949 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b    p = p->pPrior;
1e94a 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b 2b  .    nCompound++
1e94b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f  ;.  }..  /* Reso
1e94c 6c 76 65 20 74 68 65 20 4f 52 44 45 52 20 42 59  lve the ORDER BY
1e94d 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53   on a compound S
1e94e 45 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c 20  ELECT after all 
1e94f 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 68  terms of.  ** th
1e950 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20  e compound have 
1e951 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20  been resolved.. 
1e952 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d 70   */.  if( isComp
1e953 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 43  ound && resolveC
1e954 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 70  ompoundOrderBy(p
1e955 50 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74  Parse, pLeftmost
1e956 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
1e957 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a  WRC_Abort;.  }..
1e958 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
1e959 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ne;.}../*.** Thi
1e95a 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20  s routine walks 
1e95b 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
1e95c 65 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73 20  ee and resolves 
1e95d 72 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a  references to.**
1e95e 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 61   table columns a
1e95f 6e 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f  nd result-set co
1e960 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20 73  lumns.  At the s
1e961 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72 72  ame time, do err
1e962 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 6f  or.** checking o
1e963 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67 65  n function usage
1e964 20 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67 20   and set a flag 
1e965 69 66 20 61 6e 79 20 61 67 67 72 65 67 61 74 65  if any aggregate
1e966 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 72   functions.** ar
1e967 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f  e seen..**.** To
1e968 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 63   resolve table c
1e969 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65  olumns reference
1e96a 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e 6f  s we look for no
1e96b 64 65 73 20 28 6f 72 20 73 75 62 74 72 65 65 73  des (or subtrees
1e96c 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 72  ) of the .** for
1e96d 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f  m X.Y.Z or Y.Z o
1e96e 72 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a 2a  r just Z where.*
1e96f 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20 54  *.**      X:   T
1e970 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74  he name of a dat
1e971 61 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d 61  abase.  Ex:  "ma
1e972 69 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f 72  in" or "temp" or
1e973 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68  .**           th
1e974 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20  e symbolic name 
1e975 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20 41  assigned to an A
1e976 54 54 41 43 48 2d 65 64 20 64 61 74 61 62 61 73  TTACH-ed databas
1e977 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 3a  e..**.**      Y:
1e978 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61     The name of a
1e979 20 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d   table in a FROM
1e97a 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e 20   clause.  Or in 
1e97b 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20 20  a trigger.**    
1e97c 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74 68         one of th
1e97d 65 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73 20  e special names 
1e97e 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e 0a  "old" or "new"..
1e97f 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20 20  **.**      Z:   
1e980 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f  The name of a co
1e981 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59 2e  lumn in table Y.
1e982 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 20  .**.** The node 
1e983 61 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  at the root of t
1e984 68 65 20 73 75 62 74 72 65 65 20 69 73 20 6d 6f  he subtree is mo
1e985 64 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f 77  dified as follow
1e986 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 72  s:.**.**    Expr
1e987 2e 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e 67  .op        Chang
1e988 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 0a  ed to TK_COLUMN.
1e989 2a 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62 20  **    Expr.pTab 
1e98a 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74       Points to t
1e98b 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20  he Table object 
1e98c 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45 78  for X.Y.**    Ex
1e98d 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68 65  pr.iColumn   The
1e98e 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 6e   column index in
1e98f 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74 68   X.Y.  -1 for th
1e990 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 45  e rowid..**    E
1e991 78 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54 68  xpr.iTable    Th
1e992 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
1e993 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a 0a  mber for X.Y.**.
1e994 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65  **.** To resolve
1e995 20 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66 65   result-set refe
1e996 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72  rences, look for
1e997 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65   expression node
1e998 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d  s of the.** form
1e999 20 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61 6e   Z (with no X an
1e99a 64 20 59 20 70 72 65 66 69 78 29 20 77 68 65 72  d Y prefix) wher
1e99b 65 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73 20  e the Z matches 
1e99c 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a  the right-hand.*
1e99d 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53 20  * size of an AS 
1e99e 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72 65  clause in the re
1e99f 73 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53 45  sult-set of a SE
1e9a0 4c 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78 70  LECT.  The Z exp
1e9a1 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 65  ression.** is re
1e9a2 70 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70 79  placed by a copy
1e9a3 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   of the left-han
1e9a4 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65  d side of the re
1e9a5 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
1e9a6 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e 61  ion..** Table-na
1e9a7 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20  me and function 
1e9a8 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 72  resolution occur
1e9a9 73 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69 74  s on the substit
1e9aa 75 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 0a  uted expression.
1e9ab 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65 78  ** tree.  For ex
1e9ac 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a  ample, in:.**.**
1e9ad 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62        SELECT a+b
1e9ae 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 20   AS x, c+d AS y 
1e9af 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59  FROM t1 ORDER BY
1e9b0 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 78   x;.**.** The "x
1e9b1 22 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f 72  " term of the or
1e9b2 64 65 72 20 62 79 20 69 73 20 72 65 70 6c 61 63  der by is replac
1e9b3 65 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20 72  ed by "a+b" to r
1e9b4 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ender:.**.**    
1e9b5 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20    SELECT a+b AS 
1e9b6 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d  x, c+d AS y FROM
1e9b7 20 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b 62   t1 ORDER BY a+b
1e9b8 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ;.**.** Function
1e9b9 20 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63 6b   calls are check
1e9ba 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
1e9bb 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f  that the functio
1e9bc 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65 64  n is .** defined
1e9bd 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 6f   and that the co
1e9be 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrect number of 
1e9bf 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73 70  arguments are sp
1e9c0 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20 74  ecified..** If t
1e9c1 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  he function is a
1e9c2 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  n aggregate func
1e9c3 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 70  tion, then the p
1e9c4 4e 43 2d 3e 68 61 73 41 67 67 20 69 73 0a 2a 2a  NC->hasAgg is.**
1e9c5 20 73 65 74 20 61 6e 64 20 74 68 65 20 6f 70 63   set and the opc
1e9c6 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 20 66  ode is changed f
1e9c7 72 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20  rom TK_FUNCTION 
1e9c8 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  to TK_AGG_FUNCTI
1e9c9 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70  ON..** If an exp
1e9ca 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  ression contains
1e9cb 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1e9cc 69 6f 6e 73 20 74 68 65 6e 20 74 68 65 20 45 50  ions then the EP
1e9cd 5f 41 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79  _Agg.** property
1e9ce 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   on the expressi
1e9cf 6f 6e 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  on is set..**.**
1e9d0 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   An error messag
1e9d1 65 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 61  e is left in pPa
1e9d2 72 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20  rse if anything 
1e9d3 69 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20 6e  is amiss.  The n
1e9d4 75 6d 62 65 72 0a 2a 2a 20 69 66 20 65 72 72 6f  umber.** if erro
1e9d5 72 73 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  rs is returned..
1e9d6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e9d7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73  E int sqlite3Res
1e9d8 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 20 0a  olveExprNames( .
1e9d9 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1e9da 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  NC,       /* Nam
1e9db 65 73 70 61 63 65 20 74 6f 20 72 65 73 6f 6c 76  espace to resolv
1e9dc 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  e expressions in
1e9dd 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  . */.  Expr *pEx
1e9de 70 72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  pr             /
1e9df 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
1e9e0 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e   to be analyzed.
1e9e1 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76   */.){.  int sav
1e9e2 65 64 48 61 73 41 67 67 3b 0a 20 20 57 61 6c 6b  edHasAgg;.  Walk
1e9e3 65 72 20 77 3b 0a 0a 20 20 69 66 28 20 70 45 78  er w;..  if( pEx
1e9e4 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  pr==0 ) return 0
1e9e5 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ;.#if SQLITE_MAX
1e9e6 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20  _EXPR_DEPTH>0.  
1e9e7 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61  {.    Parse *pPa
1e9e8 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73  rse = pNC->pPars
1e9e9 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  e;.    if( sqlit
1e9ea 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68  e3ExprCheckHeigh
1e9eb 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  t(pParse, pExpr-
1e9ec 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50  >nHeight+pNC->pP
1e9ed 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20 29  arse->nHeight) )
1e9ee 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1e9ef 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
1e9f0 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70  se->nHeight += p
1e9f1 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20  Expr->nHeight;. 
1e9f2 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76 65   }.#endif.  save
1e9f3 64 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68  dHasAgg = pNC->h
1e9f4 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61  asAgg;.  pNC->ha
1e9f5 73 41 67 67 20 3d 20 30 3b 0a 20 20 77 2e 78 45  sAgg = 0;.  w.xE
1e9f6 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65  xprCallback = re
1e9f7 73 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20  solveExprStep;. 
1e9f8 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
1e9f9 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65  ck = resolveSele
1e9fa 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72  ctStep;.  w.pPar
1e9fb 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
1e9fc 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e  ;.  w.u.pNC = pN
1e9fd 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  C;.  sqlite3Walk
1e9fe 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b  Expr(&w, pExpr);
1e9ff 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
1ea00 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 70  EXPR_DEPTH>0.  p
1ea01 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69  NC->pParse->nHei
1ea02 67 68 74 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48  ght -= pExpr->nH
1ea03 65 69 67 68 74 3b 0a 23 65 6e 64 69 66 0a 20 20  eight;.#endif.  
1ea04 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20  if( pNC->nErr>0 
1ea05 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72  ){.    ExprSetPr
1ea06 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1ea07 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69  _Error);.  }.  i
1ea08 66 28 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 29  f( pNC->hasAgg )
1ea09 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f  {.    ExprSetPro
1ea0a 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1ea0b 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Agg);.  }else if
1ea0c 28 20 73 61 76 65 64 48 61 73 41 67 67 20 29 7b  ( savedHasAgg ){
1ea0d 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67  .    pNC->hasAgg
1ea0e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
1ea0f 72 6e 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  rn ExprHasProper
1ea10 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72  ty(pExpr, EP_Err
1ea11 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  or);.}.../*.** R
1ea12 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73  esolve all names
1ea13 20 69 6e 20 61 6c 6c 20 65 78 70 72 65 73 73 69   in all expressi
1ea14 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20  ons of a SELECT 
1ea15 61 6e 64 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65  and in all.** de
1ea16 63 65 6e 64 65 6e 74 73 20 6f 66 20 74 68 65 20  cendents of the 
1ea17 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e  SELECT, includin
1ea18 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20  g compounds off 
1ea19 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a  of p->pPrior,.**
1ea1a 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 65   subqueries in e
1ea1b 78 70 72 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20  xpressions, and 
1ea1c 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64 20  subqueries used 
1ea1d 61 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a  as FROM clause.*
1ea1e 2a 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53  * terms..**.** S
1ea1f 65 65 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  ee sqlite3Resolv
1ea20 65 45 78 70 72 4e 61 6d 65 73 28 29 20 66 6f 72  eExprNames() for
1ea21 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f   a description o
1ea22 66 20 74 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a  f the kinds of.*
1ea23 2a 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e  * transformation
1ea24 73 20 74 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a  s that occur..**
1ea25 0a 2a 2a 20 41 6c 6c 20 53 45 4c 45 43 54 20 73  .** All SELECT s
1ea26 74 61 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64  tatements should
1ea27 20 68 61 76 65 20 62 65 65 6e 20 65 78 70 61 6e   have been expan
1ea28 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c  ded using.** sql
1ea29 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64  ite3SelectExpand
1ea2a 28 29 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f  () prior to invo
1ea2b 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  king this routin
1ea2c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1ea2d 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1ea2e 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61  3ResolveSelectNa
1ea2f 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50  mes(.  Parse *pP
1ea30 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  arse,         /*
1ea31 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
1ea32 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
1ea33 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
1ea34 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
1ea35 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f  atement being co
1ea36 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  ded. */.  NameCo
1ea37 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20  ntext *pOuterNC 
1ea38 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74   /* Name context
1ea39 20 66 6f 72 20 70 61 72 65 6e 74 20 53 45 4c 45   for parent SELE
1ea3a 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  CT statement */.
1ea3b 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a  ){.  Walker w;..
1ea3c 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
1ea3d 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
1ea3e 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70  ack = resolveExp
1ea3f 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65  rStep;.  w.xSele
1ea40 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73  ctCallback = res
1ea41 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a  olveSelectStep;.
1ea42 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61    w.pParse = pPa
1ea43 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d  rse;.  w.u.pNC =
1ea44 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c   pOuterNC;.  sql
1ea45 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26  ite3WalkSelect(&
1ea46 77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  w, p);.}../*****
1ea47 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1ea48 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a   resolve.c *****
1ea49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea4b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1ea4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1ea4d 66 69 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a  file expr.c ****
1ea4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea50 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1ea51 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
1ea52 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
1ea53 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1ea54 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1ea55 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1ea56 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1ea57 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1ea58 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1ea59 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1ea5a 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1ea5b 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1ea5c 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1ea5d 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1ea5e 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1ea5f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1ea60 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1ea61 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1ea62 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1ea63 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1ea64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ea68 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
1ea69 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20  ntains routines 
1ea6a 75 73 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69  used for analyzi
1ea6b 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  ng expressions a
1ea6c 6e 64 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61  nd.** for genera
1ea6d 74 69 6e 67 20 56 44 42 45 20 63 6f 64 65 20 74  ting VDBE code t
1ea6e 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 65 78  hat evaluates ex
1ea6f 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c  pressions in SQL
1ea70 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ite..**.** $Id: 
1ea71 65 78 70 72 2e 63 2c 76 20 31 2e 34 33 32 20 32  expr.c,v 1.432 2
1ea72 30 30 39 2f 30 35 2f 30 36 20 31 38 3a 35 37 3a  009/05/06 18:57:
1ea73 31 30 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  10 shane Exp $.*
1ea74 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
1ea75 74 68 65 20 27 61 66 66 69 6e 69 74 79 27 20 6f  the 'affinity' o
1ea76 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1ea77 20 70 45 78 70 72 20 69 66 20 61 6e 79 2e 0a 2a   pExpr if any..*
1ea78 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73  *.** If pExpr is
1ea79 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20 72 65 66   a column, a ref
1ea7a 65 72 65 6e 63 65 20 74 6f 20 61 20 63 6f 6c 75  erence to a colu
1ea7b 6d 6e 20 76 69 61 20 61 6e 20 27 41 53 27 20 61  mn via an 'AS' a
1ea7c 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61 20 73 75  lias,.** or a su
1ea7d 62 2d 73 65 6c 65 63 74 20 77 69 74 68 20 61 20  b-select with a 
1ea7e 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 65  column as the re
1ea7f 74 75 72 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e  turn value, then
1ea80 20 74 68 65 20 0a 2a 2a 20 61 66 66 69 6e 69 74   the .** affinit
1ea81 79 20 6f 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e  y of that column
1ea82 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
1ea83 68 65 72 77 69 73 65 2c 20 30 78 30 30 20 69 73  herwise, 0x00 is
1ea84 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 69 6e   returned,.** in
1ea85 64 69 63 61 74 69 6e 67 20 6e 6f 20 61 66 66 69  dicating no affi
1ea86 6e 69 74 79 20 66 6f 72 20 74 68 65 20 65 78 70  nity for the exp
1ea87 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 69  ression..**.** i
1ea88 2e 65 2e 20 74 68 65 20 57 48 45 52 45 20 63 6c  .e. the WHERE cl
1ea89 61 75 73 65 20 65 78 70 72 65 73 73 73 69 6f 6e  ause expresssion
1ea8a 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s in the followi
1ea8b 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6c  ng statements al
1ea8c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20 61 66 66  l.** have an aff
1ea8d 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20 43 52 45  inity:.**.** CRE
1ea8e 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 29 3b  ATE TABLE t1(a);
1ea8f 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  .** SELECT * FRO
1ea90 4d 20 74 31 20 57 48 45 52 45 20 61 3b 0a 2a 2a  M t1 WHERE a;.**
1ea91 20 53 45 4c 45 43 54 20 61 20 41 53 20 62 20 46   SELECT a AS b F
1ea92 52 4f 4d 20 74 31 20 57 48 45 52 45 20 62 3b 0a  ROM t1 WHERE b;.
1ea93 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  ** SELECT * FROM
1ea94 20 74 31 20 57 48 45 52 45 20 28 73 65 6c 65 63   t1 WHERE (selec
1ea95 74 20 61 20 66 72 6f 6d 20 74 31 29 3b 0a 2a 2f  t a from t1);.*/
1ea96 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1ea97 63 68 61 72 20 73 71 6c 69 74 65 33 45 78 70 72  char sqlite3Expr
1ea98 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70  Affinity(Expr *p
1ea99 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 6f 70 20  Expr){.  int op 
1ea9a 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69  = pExpr->op;.  i
1ea9b 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54  f( op==TK_SELECT
1ea9c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1ea9d 70 45 78 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f  pExpr->flags&EP_
1ea9e 78 49 73 53 65 6c 65 63 74 20 29 3b 0a 20 20 20  xIsSelect );.   
1ea9f 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45   return sqlite3E
1eaa0 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70  xprAffinity(pExp
1eaa1 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45  r->x.pSelect->pE
1eaa2 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
1eaa3 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  );.  }.#ifndef S
1eaa4 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a  QLITE_OMIT_CAST.
1eaa5 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 43 41 53    if( op==TK_CAS
1eaa6 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  T ){.    return 
1eaa7 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54  sqlite3AffinityT
1eaa8 79 70 65 28 26 70 45 78 70 72 2d 3e 74 6f 6b 65  ype(&pExpr->toke
1eaa9 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  n);.  }.#endif. 
1eaaa 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47   if( (op==TK_AGG
1eaab 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54  _COLUMN || op==T
1eaac 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d  K_COLUMN || op==
1eaad 54 4b 5f 52 45 47 49 53 54 45 52 29 20 0a 20 20  TK_REGISTER) .  
1eaae 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21   && pExpr->pTab!
1eaaf 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 6f  =0.  ){.    /* o
1eab0 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26  p==TK_REGISTER &
1eab1 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d 30  & pExpr->pTab!=0
1eab2 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70 45   happens when pE
1eab3 78 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61 6c  xpr was original
1eab4 6c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43  ly.    ** a TK_C
1eab5 4f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70 72  OLUMN but was pr
1eab6 65 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 74  eviously evaluat
1eab7 65 64 20 61 6e 64 20 63 61 63 68 65 64 20 69 6e  ed and cached in
1eab8 20 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20   a register */. 
1eab9 20 20 20 69 6e 74 20 6a 20 3d 20 70 45 78 70 72     int j = pExpr
1eaba 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69  ->iColumn;.    i
1eabb 66 28 20 6a 3c 30 20 29 20 72 65 74 75 72 6e 20  f( j<0 ) return 
1eabc 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47  SQLITE_AFF_INTEG
1eabd 45 52 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ER;.    assert( 
1eabe 70 45 78 70 72 2d 3e 70 54 61 62 20 26 26 20 6a  pExpr->pTab && j
1eabf 3c 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 6e 43  <pExpr->pTab->nC
1eac0 6f 6c 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ol );.    return
1eac1 20 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43   pExpr->pTab->aC
1eac2 6f 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79 3b 0a  ol[j].affinity;.
1eac3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45 78    }.  return pEx
1eac4 70 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 7d 0a  pr->affinity;.}.
1eac5 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63  ./*.** Set the c
1eac6 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1eac7 65 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e  e for expression
1eac8 20 70 45 78 70 72 20 74 6f 20 62 65 20 74 68 65   pExpr to be the
1eac9 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65   collating.** se
1eaca 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 62 79 20  quence named by 
1eacb 70 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75 72 6e  pToken.   Return
1eacc 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1eacd 65 20 72 65 76 69 73 65 64 20 65 78 70 72 65 73  e revised expres
1eace 73 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c  sion..** The col
1eacf 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
1ead0 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78  is marked as "ex
1ead1 70 6c 69 63 69 74 22 20 75 73 69 6e 67 20 74 68  plicit" using th
1ead2 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 0a  e EP_ExpCollate.
1ead3 2a 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 78 70  ** flag.  An exp
1ead4 6c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e 67 20  licit collating 
1ead5 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 6f 76  sequence will ov
1ead6 65 72 72 69 64 65 20 69 6d 70 6c 69 63 69 74 0a  erride implicit.
1ead7 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ** collating seq
1ead8 75 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  uences..*/.SQLIT
1ead9 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
1eada 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f  sqlite3ExprSetCo
1eadb 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ll(Parse *pParse
1eadc 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54  , Expr *pExpr, T
1eadd 6f 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 6d 65 29  oken *pCollName)
1eade 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20  {.  char *zColl 
1eadf 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
1eae0 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65  /* Dequoted name
1eae1 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65   of collation se
1eae2 71 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c  quence */.  Coll
1eae3 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73 71  Seq *pColl;.  sq
1eae4 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
1eae5 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20  se->db;.  zColl 
1eae6 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
1eae7 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 6c  mToken(db, pColl
1eae8 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 45 78  Name);.  if( pEx
1eae9 70 72 20 26 26 20 7a 43 6f 6c 6c 20 29 7b 0a 20  pr && zColl ){. 
1eaea 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
1eaeb 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
1eaec 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d  pParse, zColl, -
1eaed 31 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c  1);.    if( pCol
1eaee 6c 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72  l ){.      pExpr
1eaef 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b  ->pColl = pColl;
1eaf0 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 66 6c  .      pExpr->fl
1eaf1 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c  ags |= EP_ExpCol
1eaf2 6c 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  late;.    }.  }.
1eaf3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1eaf4 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 72 65  db, zColl);.  re
1eaf5 74 75 72 6e 20 70 45 78 70 72 3b 0a 7d 0a 0a 2f  turn pExpr;.}../
1eaf6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1eaf7 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f  default collatio
1eaf8 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  n sequence for t
1eaf9 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  he expression pE
1eafa 78 70 72 2e 20 49 66 0a 2a 2a 20 74 68 65 72 65  xpr. If.** there
1eafb 20 69 73 20 6e 6f 20 64 65 66 61 75 6c 74 20 63   is no default c
1eafc 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 2c 20 72  ollation type, r
1eafd 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49  eturn 0..*/.SQLI
1eafe 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
1eaff 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43  eq *sqlite3ExprC
1eb00 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50  ollSeq(Parse *pP
1eb01 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
1eb02 72 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  r){.  CollSeq *p
1eb03 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 45 78 70 72  Coll = 0;.  Expr
1eb04 20 2a 70 20 3d 20 70 45 78 70 72 3b 0a 20 20 77   *p = pExpr;.  w
1eb05 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 69  hile( p ){.    i
1eb06 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c  nt op;.    pColl
1eb07 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20   = p->pColl;.   
1eb08 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 62 72 65   if( pColl ) bre
1eb09 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20 70 2d 3e  ak;.    op = p->
1eb0a 6f 70 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 3d  op;.    if( (op=
1eb0b 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 7c  =TK_AGG_COLUMN |
1eb0c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  | op==TK_COLUMN 
1eb0d 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54  || op==TK_REGIST
1eb0e 45 52 29 20 26 26 20 70 2d 3e 70 54 61 62 21 3d  ER) && p->pTab!=
1eb0f 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 70  0 ){.      /* op
1eb10 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26  ==TK_REGISTER &&
1eb11 20 70 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70   p->pTab!=0 happ
1eb12 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20 77  ens when pExpr w
1eb13 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20  as originally.  
1eb14 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55      ** a TK_COLU
1eb15 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65 76 69  MN but was previ
1eb16 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65 64 20  ously evaluated 
1eb17 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20 61 20  and cached in a 
1eb18 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
1eb19 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
1eb1a 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  oll;.      int j
1eb1b 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20   = p->iColumn;. 
1eb1c 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b       if( j>=0 ){
1eb1d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1eb1e 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
1eb1f 62 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c  b;.        zColl
1eb20 20 3d 20 70 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c   = p->pTab->aCol
1eb21 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20  [j].zColl;.     
1eb22 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
1eb23 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
1eb24 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c  , ENC(db), zColl
1eb25 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  , -1, 0);.      
1eb26 20 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d    pExpr->pColl =
1eb27 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a   pColl;.      }.
1eb28 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1eb29 20 7d 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 54   }.    if( op!=T
1eb2a 4b 5f 43 41 53 54 20 26 26 20 6f 70 21 3d 54 4b  K_CAST && op!=TK
1eb2b 5f 55 50 4c 55 53 20 29 7b 0a 20 20 20 20 20 20  _UPLUS ){.      
1eb2c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1eb2d 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20   p = p->pLeft;. 
1eb2e 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1eb2f 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61  CheckCollSeq(pPa
1eb30 72 73 65 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a  rse, pColl) ){ .
1eb31 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20      pColl = 0;. 
1eb32 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c   }.  return pCol
1eb33 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70  l;.}../*.** pExp
1eb34 72 20 69 73 20 61 6e 20 6f 70 65 72 61 6e 64 20  r is an operand 
1eb35 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  of a comparison 
1eb36 6f 70 65 72 61 74 6f 72 2e 20 20 61 66 66 32 20  operator.  aff2 
1eb37 69 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61  is the.** type a
1eb38 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 6f  ffinity of the o
1eb39 74 68 65 72 20 6f 70 65 72 61 6e 64 2e 20 20 54  ther operand.  T
1eb3a 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
1eb3b 72 6e 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20  rns the.** type 
1eb3c 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
1eb3d 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
1eb3e 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1eb3f 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c  operator..*/.SQL
1eb40 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
1eb41 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
1eb42 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45  ffinity(Expr *pE
1eb43 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 7b  xpr, char aff2){
1eb44 0a 20 20 63 68 61 72 20 61 66 66 31 20 3d 20 73  .  char aff1 = s
1eb45 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
1eb46 74 79 28 70 45 78 70 72 29 3b 0a 20 20 69 66 28  ty(pExpr);.  if(
1eb47 20 61 66 66 31 20 26 26 20 61 66 66 32 20 29 7b   aff1 && aff2 ){
1eb48 0a 20 20 20 20 2f 2a 20 42 6f 74 68 20 73 69 64  .    /* Both sid
1eb49 65 73 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72  es of the compar
1eb4a 69 73 6f 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73  ison are columns
1eb4b 2e 20 49 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d  . If one has num
1eb4c 65 72 69 63 0a 20 20 20 20 2a 2a 20 61 66 66 69  eric.    ** affi
1eb4d 6e 69 74 79 2c 20 75 73 65 20 74 68 61 74 2e 20  nity, use that. 
1eb4e 4f 74 68 65 72 77 69 73 65 20 75 73 65 20 6e 6f  Otherwise use no
1eb4f 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a   affinity..    *
1eb50 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
1eb51 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  3IsNumericAffini
1eb52 74 79 28 61 66 66 31 29 20 7c 7c 20 73 71 6c 69  ty(aff1) || sqli
1eb53 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69  te3IsNumericAffi
1eb54 6e 69 74 79 28 61 66 66 32 29 20 29 7b 0a 20 20  nity(aff2) ){.  
1eb55 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1eb56 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20  E_AFF_NUMERIC;. 
1eb57 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1eb58 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46  return SQLITE_AF
1eb59 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  F_NONE;.    }.  
1eb5a 7d 65 6c 73 65 20 69 66 28 20 21 61 66 66 31 20  }else if( !aff1 
1eb5b 26 26 20 21 61 66 66 32 20 29 7b 0a 20 20 20 20  && !aff2 ){.    
1eb5c 2f 2a 20 4e 65 69 74 68 65 72 20 73 69 64 65 20  /* Neither side 
1eb5d 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
1eb5e 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20  n is a column.  
1eb5f 43 6f 6d 70 61 72 65 20 74 68 65 0a 20 20 20 20  Compare the.    
1eb60 2a 2a 20 72 65 73 75 6c 74 73 20 64 69 72 65 63  ** results direc
1eb61 74 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  tly..    */.    
1eb62 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46  return SQLITE_AF
1eb63 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b  F_NONE;.  }else{
1eb64 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65  .    /* One side
1eb65 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68   is a column, th
1eb66 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2e 20  e other is not. 
1eb67 55 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20  Use the columns 
1eb68 61 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20  affinity. */.   
1eb69 20 61 73 73 65 72 74 28 20 61 66 66 31 3d 3d 30   assert( aff1==0
1eb6a 20 7c 7c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20   || aff2==0 );. 
1eb6b 20 20 20 72 65 74 75 72 6e 20 28 61 66 66 31 20     return (aff1 
1eb6c 2b 20 61 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a  + aff2);.  }.}..
1eb6d 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61  /*.** pExpr is a
1eb6e 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72   comparison oper
1eb6f 61 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 74 68  ator.  Return th
1eb70 65 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20  e type affinity 
1eb71 74 68 61 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62  that should.** b
1eb72 65 20 61 70 70 6c 69 65 64 20 74 6f 20 62 6f 74  e applied to bot
1eb73 68 20 6f 70 65 72 61 6e 64 73 20 70 72 69 6f 72  h operands prior
1eb74 20 74 6f 20 64 6f 69 6e 67 20 74 68 65 20 63 6f   to doing the co
1eb75 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61  mparison..*/.sta
1eb76 74 69 63 20 63 68 61 72 20 63 6f 6d 70 61 72 69  tic char compari
1eb77 73 6f 6e 41 66 66 69 6e 69 74 79 28 45 78 70 72  sonAffinity(Expr
1eb78 20 2a 70 45 78 70 72 29 7b 0a 20 20 63 68 61 72   *pExpr){.  char
1eb79 20 61 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20   aff;.  assert( 
1eb7a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51  pExpr->op==TK_EQ
1eb7b 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54   || pExpr->op==T
1eb7c 4b 5f 49 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f  K_IN || pExpr->o
1eb7d 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20  p==TK_LT ||.    
1eb7e 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d        pExpr->op=
1eb7f 3d 54 4b 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d  =TK_GT || pExpr-
1eb80 3e 6f 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45  >op==TK_GE || pE
1eb81 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c  xpr->op==TK_LE |
1eb82 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70  |.          pExp
1eb83 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a  r->op==TK_NE );.
1eb84 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1eb85 3e 70 4c 65 66 74 20 29 3b 0a 20 20 61 66 66 20  >pLeft );.  aff 
1eb86 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  = sqlite3ExprAff
1eb87 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 4c 65  inity(pExpr->pLe
1eb88 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72  ft);.  if( pExpr
1eb89 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ->pRight ){.    
1eb8a 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d  aff = sqlite3Com
1eb8b 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 78  pareAffinity(pEx
1eb8c 70 72 2d 3e 70 52 69 67 68 74 2c 20 61 66 66 29  pr->pRight, aff)
1eb8d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 45 78  ;.  }else if( Ex
1eb8e 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
1eb8f 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
1eb90 74 29 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20  t) ){.    aff = 
1eb91 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
1eb92 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 78 2e  finity(pExpr->x.
1eb93 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d  pSelect->pEList-
1eb94 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 61 66 66  >a[0].pExpr, aff
1eb95 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21  );.  }else if( !
1eb96 61 66 66 20 29 7b 0a 20 20 20 20 61 66 66 20 3d  aff ){.    aff =
1eb97 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
1eb98 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61  ;.  }.  return a
1eb99 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78  ff;.}../*.** pEx
1eb9a 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69 73  pr is a comparis
1eb9b 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 65  on expression, e
1eb9c 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20 49 4e 28  g. '=', '<', IN(
1eb9d 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20 69 64 78  ...) etc..** idx
1eb9e 5f 61 66 66 69 6e 69 74 79 20 69 73 20 74 68 65  _affinity is the
1eb9f 20 61 66 66 69 6e 69 74 79 20 6f 66 20 61 6e 20   affinity of an 
1eba0 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 2e 20  indexed column. 
1eba1 52 65 74 75 72 6e 20 74 72 75 65 0a 2a 2a 20 69  Return true.** i
1eba2 66 20 74 68 65 20 69 6e 64 65 78 20 77 69 74 68  f the index with
1eba3 20 61 66 66 69 6e 69 74 79 20 69 64 78 5f 61 66   affinity idx_af
1eba4 66 69 6e 69 74 79 20 6d 61 79 20 62 65 20 75 73  finity may be us
1eba5 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a  ed to implement.
1eba6 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ** the compariso
1eba7 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a 2f 0a 53  n in pExpr..*/.S
1eba8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1eba9 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66  t sqlite3IndexAf
1ebaa 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70  finityOk(Expr *p
1ebab 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61  Expr, char idx_a
1ebac 66 66 69 6e 69 74 79 29 7b 0a 20 20 63 68 61 72  ffinity){.  char
1ebad 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f   aff = compariso
1ebae 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 29  nAffinity(pExpr)
1ebaf 3b 0a 20 20 73 77 69 74 63 68 28 20 61 66 66 20  ;.  switch( aff 
1ebb0 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
1ebb1 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 20 20 20  TE_AFF_NONE:.   
1ebb2 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1ebb3 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46   case SQLITE_AFF
1ebb4 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 72 65 74  _TEXT:.      ret
1ebb5 75 72 6e 20 69 64 78 5f 61 66 66 69 6e 69 74 79  urn idx_affinity
1ebb6 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  ==SQLITE_AFF_TEX
1ebb7 54 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a  T;.    default:.
1ebb8 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c        return sql
1ebb9 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66  ite3IsNumericAff
1ebba 69 6e 69 74 79 28 69 64 78 5f 61 66 66 69 6e 69  inity(idx_affini
1ebbb 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ty);.  }.}../*.*
1ebbc 2a 20 52 65 74 75 72 6e 20 74 68 65 20 50 35 20  * Return the P5 
1ebbd 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f 75 6c  value that shoul
1ebbe 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 61 20  d be used for a 
1ebbf 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f  binary compariso
1ebc0 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28 4f 50 5f  n.** opcode (OP_
1ebc1 45 71 2c 20 4f 50 5f 47 65 20 65 74 63 2e 29 20  Eq, OP_Ge etc.) 
1ebc2 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20  used to compare 
1ebc3 70 45 78 70 72 31 20 61 6e 64 20 70 45 78 70 72  pExpr1 and pExpr
1ebc4 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20  2..*/.static u8 
1ebc5 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28  binaryCompareP5(
1ebc6 45 78 70 72 20 2a 70 45 78 70 72 31 2c 20 45 78  Expr *pExpr1, Ex
1ebc7 70 72 20 2a 70 45 78 70 72 32 2c 20 69 6e 74 20  pr *pExpr2, int 
1ebc8 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 75  jumpIfNull){.  u
1ebc9 38 20 61 66 66 20 3d 20 28 63 68 61 72 29 73 71  8 aff = (char)sq
1ebca 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
1ebcb 79 28 70 45 78 70 72 32 29 3b 0a 20 20 61 66 66  y(pExpr2);.  aff
1ebcc 20 3d 20 28 75 38 29 73 71 6c 69 74 65 33 43 6f   = (u8)sqlite3Co
1ebcd 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 45  mpareAffinity(pE
1ebce 78 70 72 31 2c 20 61 66 66 29 20 7c 20 28 75 38  xpr1, aff) | (u8
1ebcf 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a 20 20 72  )jumpIfNull;.  r
1ebd0 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a  eturn aff;.}../*
1ebd1 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
1ebd2 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c 6c  nter to the coll
1ebd3 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74  ation sequence t
1ebd4 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73  hat should be us
1ebd5 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e 61 72  ed by.** a binar
1ebd6 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  y comparison ope
1ebd7 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e 67 20  rator comparing 
1ebd8 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74  pLeft and pRight
1ebd9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
1ebda 65 66 74 20 68 61 6e 64 20 65 78 70 72 65 73 73  eft hand express
1ebdb 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74  ion has a collat
1ebdc 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 79 70  ing sequence typ
1ebdd 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a  e, then it is.**
1ebde 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69 73 65   used. Otherwise
1ebdf 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
1ebe0 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20  equence for the 
1ebe1 72 69 67 68 74 20 68 61 6e 64 20 65 78 70 72 65  right hand expre
1ebe2 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73 65 64  ssion.** is used
1ebe3 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75 6c 74  , or the default
1ebe4 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e 65 69   (BINARY) if nei
1ebe5 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20  ther expression 
1ebe6 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67 0a  has a collating.
1ebe7 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 41  ** type..**.** A
1ebe8 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74 20 28  rgument pRight (
1ebe9 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29 20 6d  but not pLeft) m
1ebea 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69  ay be a null poi
1ebeb 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20 63 61  nter. In this ca
1ebec 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74  se,.** it is not
1ebed 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a 2f 0a   considered..*/.
1ebee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
1ebef 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 42  ollSeq *sqlite3B
1ebf0 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
1ebf1 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a 70 50  Seq(.  Parse *pP
1ebf2 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 2a 70  arse, .  Expr *p
1ebf3 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20 2a 70  Left, .  Expr *p
1ebf4 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c 6c 53  Right.){.  CollS
1ebf5 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61 73 73  eq *pColl;.  ass
1ebf6 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 20  ert( pLeft );.  
1ebf7 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73  if( pLeft->flags
1ebf8 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
1ebf9 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1ebfa 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a  pLeft->pColl );.
1ebfb 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c 65 66      pColl = pLef
1ebfc 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c 73  t->pColl;.  }els
1ebfd 65 20 69 66 28 20 70 52 69 67 68 74 20 26 26 20  e if( pRight && 
1ebfe 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20  pRight->flags & 
1ebff 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b  EP_ExpCollate ){
1ec00 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52 69  .    assert( pRi
1ec01 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20 20  ght->pColl );.  
1ec02 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74    pColl = pRight
1ec03 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c 73 65  ->pColl;.  }else
1ec04 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71  {.    pColl = sq
1ec05 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
1ec06 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29 3b  (pParse, pLeft);
1ec07 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20  .    if( !pColl 
1ec08 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d  ){.      pColl =
1ec09 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
1ec0a 53 65 71 28 70 50 61 72 73 65 2c 20 70 52 69 67  Seq(pParse, pRig
1ec0b 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ht);.    }.  }. 
1ec0c 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d   return pColl;.}
1ec0d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1ec0e 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 66 6f   the operands fo
1ec0f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  r a comparison o
1ec10 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 66 6f 72  peration.  Befor
1ec11 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20  e.** generating 
1ec12 74 68 65 20 63 6f 64 65 20 66 6f 72 20 65 61 63  the code for eac
1ec13 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 74 20 74  h operand, set t
1ec14 68 65 20 45 50 5f 41 6e 79 41 66 66 0a 2a 2a 20  he EP_AnyAff.** 
1ec15 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 78 70 72  flag on the expr
1ec16 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 20 69  ession so that i
1ec17 74 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74  t will be able t
1ec18 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 61 63 68  o used a.** cach
1ec19 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  ed column value 
1ec1a 74 68 61 74 20 68 61 73 20 70 72 65 76 69 6f 75  that has previou
1ec1b 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 20 61 6e  sly undergone an
1ec1c 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 63 68 61  .** affinity cha
1ec1d 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  nge..*/.static v
1ec1e 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f  oid codeCompareO
1ec1f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 72 73 65  perands(.  Parse
1ec20 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20   *pParse,    /* 
1ec21 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65  Parsing and code
1ec22 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74   generating cont
1ec23 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ext */.  Expr *p
1ec24 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68  Left,      /* Th
1ec25 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  e left operand *
1ec26 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 4c 65 66  /.  int *pRegLef
1ec27 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65  t,    /* Registe
1ec28 72 20 77 68 65 72 65 20 6c 65 66 74 20 6f 70 65  r where left ope
1ec29 72 61 6e 64 20 69 73 20 73 74 6f 72 65 64 20 2a  rand is stored *
1ec2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 4c 65  /.  int *pFreeLe
1ec2b 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 20 74 68  ft,   /* Free th
1ec2c 69 73 20 72 65 67 69 73 74 65 72 20 77 68 65 6e  is register when
1ec2d 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 70 72 20   done */.  Expr 
1ec2e 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20  *pRight,     /* 
1ec2f 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  The right operan
1ec30 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67  d */.  int *pReg
1ec31 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 65 67 69  Right,   /* Regi
1ec32 73 74 65 72 20 77 68 65 72 65 20 72 69 67 68 74  ster where right
1ec33 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72   operand is stor
1ec34 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72  ed */.  int *pFr
1ec35 65 65 52 69 67 68 74 20 20 20 2f 2a 20 57 72 69  eeRight   /* Wri
1ec36 74 65 20 74 65 6d 70 20 72 65 67 69 73 74 65 72  te temp register
1ec37 20 66 6f 72 20 72 69 67 68 74 20 6f 70 65 72 61   for right opera
1ec38 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  nd there */.){. 
1ec39 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d 3e 6f   while( pLeft->o
1ec3a 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 4c  p==TK_UPLUS ) pL
1ec3b 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 4c 65  eft = pLeft->pLe
1ec3c 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e 66 6c 61  ft;.  pLeft->fla
1ec3d 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 66 66 3b  gs |= EP_AnyAff;
1ec3e 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 3d 20 73  .  *pRegLeft = s
1ec3f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
1ec40 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74  mp(pParse, pLeft
1ec41 2c 20 70 46 72 65 65 4c 65 66 74 29 3b 0a 20 20  , pFreeLeft);.  
1ec42 77 68 69 6c 65 28 20 70 52 69 67 68 74 2d 3e 6f  while( pRight->o
1ec43 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70 52  p==TK_UPLUS ) pR
1ec44 69 67 68 74 20 3d 20 70 52 69 67 68 74 2d 3e 70  ight = pRight->p
1ec45 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 74 2d 3e  Left;.  pRight->
1ec46 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41  flags |= EP_AnyA
1ec47 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 67 68 74  ff;.  *pRegRight
1ec48 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1ec49 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
1ec4a 52 69 67 68 74 2c 20 70 46 72 65 65 52 69 67 68  Right, pFreeRigh
1ec4b 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  t);.}../*.** Gen
1ec4c 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
1ec4d 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72   comparison oper
1ec4e 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ator..*/.static 
1ec4f 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72 65 28  int codeCompare(
1ec50 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1ec51 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73  ,    /* The pars
1ec52 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20 67 65  ing (and code ge
1ec53 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74 65 78  nerating) contex
1ec54 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65  t */.  Expr *pLe
1ec55 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ft,      /* The 
1ec56 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  left operand */.
1ec57 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20    Expr *pRight, 
1ec58 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
1ec59 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
1ec5a 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20 20 20  t opcode,       
1ec5b 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69 73 6f  /* The compariso
1ec5c 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  n opcode */.  in
1ec5d 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e 32 2c 20  t in1, int in2, 
1ec5e 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
1ec5f 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 2a 2f 0a  ing operands */.
1ec60 20 20 69 6e 74 20 64 65 73 74 2c 20 20 20 20 20    int dest,     
1ec61 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
1ec62 20 69 66 20 74 72 75 65 2e 20 20 2a 2f 0a 20 20   if true.  */.  
1ec63 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 20  int jumpIfNull  
1ec64 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 6a 75    /* If true, ju
1ec65 6d 70 20 69 66 20 65 69 74 68 65 72 20 6f 70 65  mp if either ope
1ec66 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a  rand is NULL */.
1ec67 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a 20 20 69  ){.  int p5;.  i
1ec68 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f 6c 6c 53  nt addr;.  CollS
1ec69 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 20 3d 20  eq *p4;..  p4 = 
1ec6a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
1ec6b 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  pareCollSeq(pPar
1ec6c 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  se, pLeft, pRigh
1ec6d 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 6e 61 72  t);.  p5 = binar
1ec6e 79 43 6f 6d 70 61 72 65 50 35 28 70 4c 65 66 74  yCompareP5(pLeft
1ec6f 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70 49 66  , pRight, jumpIf
1ec70 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 20 3d 20  Null);.  addr = 
1ec71 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ec72 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  4(pParse->pVdbe,
1ec73 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c 20 64 65   opcode, in2, de
1ec74 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 20 20 20  st, in1,.       
1ec75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec76 20 20 20 20 28 76 6f 69 64 2a 29 70 34 2c 20 50      (void*)p4, P
1ec77 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 73 71  4_COLLSEQ);.  sq
1ec78 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1ec79 35 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  5(pParse->pVdbe,
1ec7a 20 28 75 38 29 70 35 29 3b 0a 20 20 69 66 28 20   (u8)p5);.  if( 
1ec7b 28 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46  (p5 & SQLITE_AFF
1ec7c 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 54 45 5f 41  _MASK)!=SQLITE_A
1ec7d 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 73  FF_NONE ){.    s
1ec7e 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
1ec7f 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
1ec80 61 72 73 65 2c 20 69 6e 31 2c 20 31 29 3b 0a 20  arse, in1, 1);. 
1ec81 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
1ec82 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
1ec83 65 28 70 50 61 72 73 65 2c 20 69 6e 32 2c 20 31  e(pParse, in2, 1
1ec84 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1ec85 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c  addr;.}..#if SQL
1ec86 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
1ec87 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  TH>0./*.** Check
1ec88 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20 6e   that argument n
1ec89 48 65 69 67 68 74 20 69 73 20 6c 65 73 73 20 74  Height is less t
1ec8a 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
1ec8b 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 65  the maximum.** e
1ec8c 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74 68 20  xpression depth 
1ec8d 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74 20 69  allowed. If it i
1ec8e 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61 6e 20  s not, leave an 
1ec8f 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
1ec90 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53  .** pParse..*/.S
1ec91 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1ec92 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 65  t sqlite3ExprChe
1ec93 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 20 2a  ckHeight(Parse *
1ec94 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 48 65 69  pParse, int nHei
1ec95 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ght){.  int rc =
1ec96 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
1ec97 74 20 6d 78 48 65 69 67 68 74 20 3d 20 70 50 61  t mxHeight = pPa
1ec98 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  rse->db->aLimit[
1ec99 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50  SQLITE_LIMIT_EXP
1ec9a 52 5f 44 45 50 54 48 5d 3b 0a 20 20 69 66 28 20  R_DEPTH];.  if( 
1ec9b 6e 48 65 69 67 68 74 3e 6d 78 48 65 69 67 68 74  nHeight>mxHeight
1ec9c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1ec9d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1ec9e 0a 20 20 20 20 20 20 20 22 45 78 70 72 65 73 73  .       "Express
1ec9f 69 6f 6e 20 74 72 65 65 20 69 73 20 74 6f 6f 20  ion tree is too 
1eca0 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 6d 20 64  large (maximum d
1eca1 65 70 74 68 20 25 64 29 22 2c 20 6d 78 48 65 69  epth %d)", mxHei
1eca2 67 68 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 72  ght.    );.    r
1eca3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1eca4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1eca5 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c  c;.}../* The fol
1eca6 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66 75 6e  lowing three fun
1eca7 63 74 69 6f 6e 73 2c 20 68 65 69 67 68 74 4f 66  ctions, heightOf
1eca8 45 78 70 72 28 29 2c 20 68 65 69 67 68 74 4f 66  Expr(), heightOf
1eca9 45 78 70 72 4c 69 73 74 28 29 0a 2a 2a 20 61 6e  ExprList().** an
1ecaa 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74  d heightOfSelect
1ecab 28 29 2c 20 61 72 65 20 75 73 65 64 20 74 6f 20  (), are used to 
1ecac 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 61  determine the ma
1ecad 78 69 6d 75 6d 20 68 65 69 67 68 74 0a 2a 2a 20  ximum height.** 
1ecae 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f  of any expressio
1ecaf 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  n tree reference
1ecb0 64 20 62 79 20 74 68 65 20 73 74 72 75 63 74 75  d by the structu
1ecb1 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  re passed as the
1ecb2 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75 6d 65  .** first argume
1ecb3 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69  nt..**.** If thi
1ecb4 73 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74  s maximum height
1ecb5 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
1ecb6 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   the current val
1ecb7 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  ue pointed.** to
1ecb8 20 62 79 20 70 6e 48 65 69 67 68 74 2c 20 74 68   by pnHeight, th
1ecb9 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
1ecba 65 72 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 6e  er, then set *pn
1ecbb 48 65 69 67 68 74 20 74 6f 20 74 68 61 74 0a 2a  Height to that.*
1ecbc 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  * value..*/.stat
1ecbd 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66  ic void heightOf
1ecbe 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e  Expr(Expr *p, in
1ecbf 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20  t *pnHeight){.  
1ecc0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28  if( p ){.    if(
1ecc1 20 70 2d 3e 6e 48 65 69 67 68 74 3e 2a 70 6e 48   p->nHeight>*pnH
1ecc2 65 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 2a  eight ){.      *
1ecc3 70 6e 48 65 69 67 68 74 20 3d 20 70 2d 3e 6e 48  pnHeight = p->nH
1ecc4 65 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  eight;.    }.  }
1ecc5 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68  .}.static void h
1ecc6 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28  eightOfExprList(
1ecc7 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74  ExprList *p, int
1ecc8 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69   *pnHeight){.  i
1ecc9 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20  f( p ){.    int 
1ecca 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  i;.    for(i=0; 
1eccb 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  i<p->nExpr; i++)
1eccc 7b 0a 20 20 20 20 20 20 68 65 69 67 68 74 4f 66  {.      heightOf
1eccd 45 78 70 72 28 70 2d 3e 61 5b 69 5d 2e 70 45 78  Expr(p->a[i].pEx
1ecce 70 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20  pr, pnHeight);. 
1eccf 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69     }.  }.}.stati
1ecd0 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 53  c void heightOfS
1ecd1 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a 70 2c  elect(Select *p,
1ecd2 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b   int *pnHeight){
1ecd3 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
1ecd4 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e  heightOfExpr(p->
1ecd5 70 57 68 65 72 65 2c 20 70 6e 48 65 69 67 68 74  pWhere, pnHeight
1ecd6 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45  );.    heightOfE
1ecd7 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20  xpr(p->pHaving, 
1ecd8 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
1ecd9 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
1ecda 4c 69 6d 69 74 2c 20 70 6e 48 65 69 67 68 74 29  Limit, pnHeight)
1ecdb 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  ;.    heightOfEx
1ecdc 70 72 28 70 2d 3e 70 4f 66 66 73 65 74 2c 20 70  pr(p->pOffset, p
1ecdd 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
1ecde 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70  ightOfExprList(p
1ecdf 2d 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65 69 67  ->pEList, pnHeig
1ece0 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
1ece1 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72  fExprList(p->pGr
1ece2 6f 75 70 42 79 2c 20 70 6e 48 65 69 67 68 74 29  oupBy, pnHeight)
1ece3 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  ;.    heightOfEx
1ece4 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65 72  prList(p->pOrder
1ece5 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20  By, pnHeight);. 
1ece6 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63     heightOfSelec
1ece7 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70 6e 48  t(p->pPrior, pnH
1ece8 65 69 67 68 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  eight);.  }.}../
1ece9 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70  *.** Set the Exp
1ecea 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62  r.nHeight variab
1eceb 6c 65 20 69 6e 20 74 68 65 20 73 74 72 75 63 74  le in the struct
1ecec 75 72 65 20 70 61 73 73 65 64 20 61 73 20 61 6e  ure passed as an
1eced 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 41   .** argument. A
1ecee 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69 74  n expression wit
1ecef 68 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2c 20 45  h no children, E
1ecf0 78 70 72 2e 70 4c 69 73 74 20 6f 72 20 0a 2a 2a  xpr.pList or .**
1ecf1 20 45 78 70 72 2e 70 53 65 6c 65 63 74 20 6d 65   Expr.pSelect me
1ecf2 6d 62 65 72 20 68 61 73 20 61 20 68 65 69 67 68  mber has a heigh
1ecf3 74 20 6f 66 20 31 2e 20 41 6e 79 20 6f 74 68 65  t of 1. Any othe
1ecf4 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  r expression.** 
1ecf5 68 61 73 20 61 20 68 65 69 67 68 74 20 65 71 75  has a height equ
1ecf6 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  al to the maximu
1ecf7 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e 79 20  m height of any 
1ecf8 6f 74 68 65 72 20 0a 2a 2a 20 72 65 66 65 72 65  other .** refere
1ecf9 6e 63 65 64 20 45 78 70 72 20 70 6c 75 73 20 6f  nced Expr plus o
1ecfa 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ne..*/.static vo
1ecfb 69 64 20 65 78 70 72 53 65 74 48 65 69 67 68 74  id exprSetHeight
1ecfc 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 6e 74  (Expr *p){.  int
1ecfd 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20   nHeight = 0;.  
1ecfe 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e  heightOfExpr(p->
1ecff 70 4c 65 66 74 2c 20 26 6e 48 65 69 67 68 74 29  pLeft, &nHeight)
1ed00 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72  ;.  heightOfExpr
1ed01 28 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e 48 65  (p->pRight, &nHe
1ed02 69 67 68 74 29 3b 0a 20 20 69 66 28 20 45 78 70  ight);.  if( Exp
1ed03 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
1ed04 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
1ed05 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c  .    heightOfSel
1ed06 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c 65 63 74  ect(p->x.pSelect
1ed07 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d  , &nHeight);.  }
1ed08 65 6c 73 65 7b 0a 20 20 20 20 68 65 69 67 68 74  else{.    height
1ed09 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 78 2e  OfExprList(p->x.
1ed0a 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68 74 29  pList, &nHeight)
1ed0b 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 48 65 69 67  ;.  }.  p->nHeig
1ed0c 68 74 20 3d 20 6e 48 65 69 67 68 74 20 2b 20 31  ht = nHeight + 1
1ed0d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
1ed0e 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20  he Expr.nHeight 
1ed0f 76 61 72 69 61 62 6c 65 20 75 73 69 6e 67 20 74  variable using t
1ed10 68 65 20 65 78 70 72 53 65 74 48 65 69 67 68 74  he exprSetHeight
1ed11 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 0a  () function. If.
1ed12 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 69 73  ** the height is
1ed13 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
1ed14 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65  e maximum allowe
1ed15 64 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70  d expression dep
1ed16 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20  th,.** leave an 
1ed17 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e  error in pParse.
1ed18 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1ed19 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
1ed1a 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 72  xprSetHeight(Par
1ed1b 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
1ed1c 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 65 74 48   *p){.  exprSetH
1ed1d 65 69 67 68 74 28 70 29 3b 0a 20 20 73 71 6c 69  eight(p);.  sqli
1ed1e 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67  te3ExprCheckHeig
1ed1f 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48  ht(pParse, p->nH
1ed20 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  eight);.}../*.**
1ed21 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69   Return the maxi
1ed22 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e  mum height of an
1ed23 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  y expression tre
1ed24 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20  e referenced.** 
1ed25 62 79 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  by the select st
1ed26 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61  atement passed a
1ed27 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  s an argument..*
1ed28 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ed29 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65   int sqlite3Sele
1ed2a 63 74 45 78 70 72 48 65 69 67 68 74 28 53 65 6c  ctExprHeight(Sel
1ed2b 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  ect *p){.  int n
1ed2c 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65  Height = 0;.  he
1ed2d 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20  ightOfSelect(p, 
1ed2e 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74  &nHeight);.  ret
1ed2f 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 23  urn nHeight;.}.#
1ed30 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 65  else.  #define e
1ed31 78 70 72 53 65 74 48 65 69 67 68 74 28 79 29 0a  xprSetHeight(y).
1ed32 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1ed33 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e  _MAX_EXPR_DEPTH>
1ed34 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73  0 */../*.** Cons
1ed35 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72  truct a new expr
1ed36 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20  ession node and 
1ed37 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
1ed38 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a   to it.  Memory.
1ed39 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65  ** for this node
1ed3a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
1ed3b 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
1ed3c 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  ().  The calling
1ed3d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20   function.** is 
1ed3e 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
1ed3f 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20  making sure the 
1ed40 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20  node eventually 
1ed41 67 65 74 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 53  gets freed..*/.S
1ed42 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
1ed43 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28  pr *sqlite3Expr(
1ed44 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
1ed45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
1ed46 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33  ndle for sqlite3
1ed47 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28  DbMallocZero() (
1ed48 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a  may be null) */.
1ed49 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
1ed4a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
1ed4b 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a  ression opcode *
1ed4c 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c  /.  Expr *pLeft,
1ed4d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1ed4e 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  eft operand */. 
1ed4f 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20   Expr *pRight,  
1ed50 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68           /* Righ
1ed51 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63  t operand */.  c
1ed52 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  onst Token *pTok
1ed53 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65  en     /* Argume
1ed54 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  nt token */.){. 
1ed55 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 70   Expr *pNew;.  p
1ed56 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
1ed57 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
1ed58 7a 65 6f 66 28 45 78 70 72 29 29 3b 0a 20 20 69  zeof(Expr));.  i
1ed59 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( pNew==0 ){.  
1ed5a 20 20 2f 2a 20 57 68 65 6e 20 6d 61 6c 6c 6f 63    /* When malloc
1ed5b 20 66 61 69 6c 73 2c 20 64 65 6c 65 74 65 20 70   fails, delete p
1ed5c 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e  Left and pRight.
1ed5d 20 45 78 70 72 65 73 73 69 6f 6e 73 20 70 61 73   Expressions pas
1ed5e 73 65 64 20 74 6f 20 0a 20 20 20 20 2a 2a 20 74  sed to .    ** t
1ed5f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  his function mus
1ed60 74 20 61 6c 77 61 79 73 20 62 65 20 61 6c 6c 6f  t always be allo
1ed61 63 61 74 65 64 20 77 69 74 68 20 73 71 6c 69 74  cated with sqlit
1ed62 65 33 45 78 70 72 28 29 20 66 6f 72 20 74 68 69  e3Expr() for thi
1ed63 73 20 0a 20 20 20 20 2a 2a 20 72 65 61 73 6f 6e  s .    ** reason
1ed64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  . .    */.    sq
1ed65 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1ed66 64 62 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  db, pLeft);.    
1ed67 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1ed68 65 28 64 62 2c 20 70 52 69 67 68 74 29 3b 0a 20  e(db, pRight);. 
1ed69 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1ed6a 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 28 75  .  pNew->op = (u
1ed6b 38 29 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c  8)op;.  pNew->pL
1ed6c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 70  eft = pLeft;.  p
1ed6d 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 70 52  New->pRight = pR
1ed6e 69 67 68 74 3b 0a 20 20 70 4e 65 77 2d 3e 69 41  ight;.  pNew->iA
1ed6f 67 67 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d  gg = -1;.  pNew-
1ed70 3e 73 70 61 6e 2e 7a 20 3d 20 28 75 38 2a 29 22  >span.z = (u8*)"
1ed71 22 3b 0a 20 20 69 66 28 20 70 54 6f 6b 65 6e 20  ";.  if( pToken 
1ed72 29 7b 0a 20 20 20 20 69 6e 74 20 63 3b 0a 20 20  ){.    int c;.  
1ed73 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e    assert( pToken
1ed74 2d 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20  ->dyn==0 );.    
1ed75 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 2a 70 54  pNew->span = *pT
1ed76 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 54  oken;.    if( pT
1ed77 6f 6b 65 6e 2d 3e 6e 3e 3d 32 20 0a 20 20 20 20  oken->n>=2 .    
1ed78 20 20 20 20 20 26 26 20 28 28 63 20 3d 20 70 54       && ((c = pT
1ed79 6f 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27  oken->z[0])=='\'
1ed7a 27 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63  ' || c=='"' || c
1ed7b 3d 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29  =='[' || c=='`')
1ed7c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1ed7d 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26  3TokenCopy(db, &
1ed7e 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2c 20 70 54 6f  pNew->token, pTo
1ed7f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ken);.      if( 
1ed80 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b  pNew->token.z ){
1ed81 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 74  .        pNew->t
1ed82 6f 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33  oken.n = sqlite3
1ed83 44 65 71 75 6f 74 65 28 28 63 68 61 72 2a 29 70  Dequote((char*)p
1ed84 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20  New->token.z);. 
1ed85 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1ed86 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 6e 3d 3d 28 75  New->token.n==(u
1ed87 6e 73 69 67 6e 65 64 29 73 71 6c 69 74 65 33 53  nsigned)sqlite3S
1ed88 74 72 6c 65 6e 33 30 28 28 63 68 61 72 2a 29 70  trlen30((char*)p
1ed89 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 7a 29 20 29 3b  New->token.z) );
1ed8a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1ed8b 66 28 20 63 3d 3d 27 22 27 20 29 20 70 4e 65 77  f( c=='"' ) pNew
1ed8c 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44 62  ->flags |= EP_Db
1ed8d 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20 7d 65 6c  lQuoted;.    }el
1ed8e 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  se{.      pNew->
1ed8f 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b  token = *pToken;
1ed90 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d  .    }.    pNew-
1ed91 3e 74 6f 6b 65 6e 2e 71 75 6f 74 65 64 20 3d 20  >token.quoted = 
1ed92 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  0;.  }else if( p
1ed93 4c 65 66 74 20 29 7b 0a 20 20 20 20 69 66 28 20  Left ){.    if( 
1ed94 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  pRight ){.      
1ed95 69 66 28 20 70 52 69 67 68 74 2d 3e 73 70 61 6e  if( pRight->span
1ed96 2e 64 79 6e 3d 3d 30 20 26 26 20 70 4c 65 66 74  .dyn==0 && pLeft
1ed97 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d 30 20 29 7b  ->span.dyn==0 ){
1ed98 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ed99 45 78 70 72 53 70 61 6e 28 70 4e 65 77 2c 20 26  ExprSpan(pNew, &
1ed9a 70 4c 65 66 74 2d 3e 73 70 61 6e 2c 20 26 70 52  pLeft->span, &pR
1ed9b 69 67 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  ight->span);.   
1ed9c 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
1ed9d 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45  Right->flags & E
1ed9e 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a  P_ExpCollate ){.
1ed9f 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c          pNew->fl
1eda0 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c  ags |= EP_ExpCol
1eda1 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 4e  late;.        pN
1eda2 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67  ew->pColl = pRig
1eda3 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  ht->pColl;.     
1eda4 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
1eda5 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20   pLeft->flags & 
1eda6 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b  EP_ExpCollate ){
1eda7 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61  .      pNew->fla
1eda8 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c  gs |= EP_ExpColl
1eda9 61 74 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  ate;.      pNew-
1edaa 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e  >pColl = pLeft->
1edab 70 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d  pColl;.    }.  }
1edac 0a 0a 20 20 65 78 70 72 53 65 74 48 65 69 67 68  ..  exprSetHeigh
1edad 74 28 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72  t(pNew);.  retur
1edae 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
1edaf 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69   Works like sqli
1edb0 74 65 33 45 78 70 72 28 29 20 65 78 63 65 70 74  te3Expr() except
1edb1 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61   that it takes a
1edb2 6e 20 65 78 74 72 61 20 50 61 72 73 65 2a 0a 2a  n extra Parse*.*
1edb3 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 6e  * argument and n
1edb4 6f 74 69 66 69 65 73 20 74 68 65 20 61 73 73 6f  otifies the asso
1edb5 63 69 61 74 65 64 20 63 6f 6e 6e 65 63 74 69 6f  ciated connectio
1edb6 6e 20 6f 62 6a 65 63 74 20 69 66 20 6d 61 6c 6c  n object if mall
1edb7 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c  oc fails..*/.SQL
1edb8 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
1edb9 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 0a   *sqlite3PExpr(.
1edba 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1edbb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
1edbc 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1edbd 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
1edbe 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
1edbf 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a  ression opcode *
1edc0 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c  /.  Expr *pLeft,
1edc1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1edc2 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  eft operand */. 
1edc3 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20   Expr *pRight,  
1edc4 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68           /* Righ
1edc5 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63  t operand */.  c
1edc6 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  onst Token *pTok
1edc7 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65  en     /* Argume
1edc8 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  nt token */.){. 
1edc9 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74   Expr *p = sqlit
1edca 65 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64  e3Expr(pParse->d
1edcb 62 2c 20 6f 70 2c 20 70 4c 65 66 74 2c 20 70 52  b, op, pLeft, pR
1edcc 69 67 68 74 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  ight, pToken);. 
1edcd 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71   if( p ){.    sq
1edce 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65  lite3ExprCheckHe
1edcf 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e  ight(pParse, p->
1edd0 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20  nHeight);.  }.  
1edd1 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
1edd2 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 61 20  ** When doing a 
1edd3 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20 79 6f  nested parse, yo
1edd4 75 20 63 61 6e 20 69 6e 63 6c 75 64 65 20 74 65  u can include te
1edd5 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72 65 73  rms in an expres
1edd6 73 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c 6f 6f  sion.** that loo
1edd7 6b 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23  k like this:   #
1edd8 31 20 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20  1 #2 ...  These 
1edd9 74 65 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72  terms refer to r
1edda 65 67 69 73 74 65 72 73 0a 2a 2a 20 69 6e 20 74  egisters.** in t
1eddb 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
1eddc 6e 65 2e 20 20 23 4e 20 69 73 20 74 68 65 20 4e  ne.  #N is the N
1eddd 2d 74 68 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a  -th register..**
1edde 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1eddf 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
1ede0 65 20 70 61 72 73 65 72 20 74 6f 20 64 65 61 6c  e parser to deal
1ede1 20 77 69 74 68 20 6f 6e 20 6f 66 20 74 68 6f 73   with on of thos
1ede2 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 49 74 20 69  e terms..** It i
1ede3 6d 6d 65 64 69 61 74 65 6c 79 20 67 65 6e 65 72  mmediately gener
1ede4 61 74 65 73 20 63 6f 64 65 20 74 6f 20 73 74 6f  ates code to sto
1ede5 72 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  re the value in 
1ede6 61 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  a memory locatio
1ede7 6e 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  n..** The return
1ede8 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s an expression 
1ede9 74 68 61 74 20 77 69 6c 6c 20 63 6f 64 65 20 74  that will code t
1edea 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 76 61  o extract the va
1edeb 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 61 74  lue from.** that
1edec 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
1eded 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53   as needed..*/.S
1edee 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
1edef 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73  pr *sqlite3Regis
1edf0 74 65 72 45 78 70 72 28 50 61 72 73 65 20 2a 70  terExpr(Parse *p
1edf1 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54  Parse, Token *pT
1edf2 6f 6b 65 6e 29 7b 0a 20 20 56 64 62 65 20 2a 76  oken){.  Vdbe *v
1edf3 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
1edf4 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 69  ;.  Expr *p;.  i
1edf5 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
1edf6 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  d==0 ){.    sqli
1edf7 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1edf8 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22  se, "near \"%T\"
1edf9 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c  : syntax error",
1edfa 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 72 65   pToken);.    re
1edfb 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70  turn sqlite3PExp
1edfc 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c  r(pParse, TK_NUL
1edfd 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  L, 0, 0, 0);.  }
1edfe 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
1edff 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 73 71  turn 0;.  p = sq
1ee00 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
1ee01 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20  e, TK_REGISTER, 
1ee02 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  0, 0, pToken);. 
1ee03 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20   if( p==0 ){.   
1ee04 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4d   return 0;  /* M
1ee05 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a  alloc failed */.
1ee06 20 20 7d 0a 20 20 70 2d 3e 69 54 61 62 6c 65 20    }.  p->iTable 
1ee07 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70  = atoi((char*)&p
1ee08 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20  Token->z[1]);.  
1ee09 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
1ee0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72  ** Join two expr
1ee0b 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e  essions using an
1ee0c 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20   AND operator.  
1ee0d 49 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73  If either expres
1ee0e 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c  sion is.** NULL,
1ee0f 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72   then just retur
1ee10 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72  n the other expr
1ee11 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ession..*/.SQLIT
1ee12 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
1ee13 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73  sqlite3ExprAnd(s
1ee14 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
1ee15 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70   *pLeft, Expr *p
1ee16 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c  Right){.  if( pL
1ee17 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  eft==0 ){.    re
1ee18 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d  turn pRight;.  }
1ee19 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d  else if( pRight=
1ee1a 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1ee1b 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b   pLeft;.  }else{
1ee1c 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
1ee1d 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 41  te3Expr(db, TK_A
1ee1e 4e 44 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  ND, pLeft, pRigh
1ee1f 74 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  t, 0);.  }.}../*
1ee20 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72  .** Set the Expr
1ee21 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74  .span field of t
1ee22 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
1ee23 69 6f 6e 20 74 6f 20 73 70 61 6e 20 61 6c 6c 0a  ion to span all.
1ee24 2a 2a 20 74 65 78 74 20 62 65 74 77 65 65 6e 20  ** text between 
1ee25 74 68 65 20 74 77 6f 20 67 69 76 65 6e 20 74 6f  the two given to
1ee26 6b 65 6e 73 2e 20 20 42 6f 74 68 20 74 6f 6b 65  kens.  Both toke
1ee27 6e 73 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74  ns must be point
1ee28 69 6e 67 0a 2a 2a 20 61 74 20 74 68 65 20 73 61  ing.** at the sa
1ee29 6d 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51  me string..*/.SQ
1ee2a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1ee2b 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61  d sqlite3ExprSpa
1ee2c 6e 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54  n(Expr *pExpr, T
1ee2d 6f 6b 65 6e 20 2a 70 4c 65 66 74 2c 20 54 6f 6b  oken *pLeft, Tok
1ee2e 65 6e 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61  en *pRight){.  a
1ee2f 73 73 65 72 74 28 20 70 52 69 67 68 74 21 3d 30  ssert( pRight!=0
1ee30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c   );.  assert( pL
1ee31 65 66 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  eft!=0 );.  if( 
1ee32 70 45 78 70 72 20 29 7b 0a 20 20 20 20 70 45 78  pExpr ){.    pEx
1ee33 70 72 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4c 65  pr->span.z = pLe
1ee34 66 74 2d 3e 7a 3b 0a 20 20 20 20 2f 2a 20 54 68  ft->z;.    /* Th
1ee35 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
1ee36 72 74 28 29 20 6d 61 79 20 66 61 69 6c 20 77 68  rt() may fail wh
1ee37 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  en this is calle
1ee38 64 20 0a 20 20 20 20 2a 2a 20 76 69 61 20 73 71  d .    ** via sq
1ee39 6c 69 74 65 33 50 45 78 70 72 28 29 2f 73 71 6c  lite3PExpr()/sql
1ee3a 69 74 65 33 45 78 70 72 28 29 20 66 72 6f 6d 20  ite3Expr() from 
1ee3b 61 64 64 57 68 65 72 65 54 65 72 6d 28 29 2e 20  addWhereTerm(). 
1ee3c 2a 2f 0a 20 20 20 20 2f 2a 20 61 73 73 65 72 74  */.    /* assert
1ee3d 28 70 52 69 67 68 74 2d 3e 7a 20 3e 3d 20 70 4c  (pRight->z >= pL
1ee3e 65 66 74 2d 3e 7a 29 3b 20 2a 2f 0a 20 20 20 20  eft->z); */.    
1ee3f 70 45 78 70 72 2d 3e 73 70 61 6e 2e 6e 20 3d 20  pExpr->span.n = 
1ee40 70 52 69 67 68 74 2d 3e 6e 20 2b 20 28 75 6e 73  pRight->n + (uns
1ee41 69 67 6e 65 64 29 28 70 52 69 67 68 74 2d 3e 7a  igned)(pRight->z
1ee42 20 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b 0a 20 20   - pLeft->z);.  
1ee43 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74  }.}../*.** Const
1ee44 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65  ruct a new expre
1ee45 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61  ssion node for a
1ee46 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d   function with m
1ee47 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d  ultiple.** argum
1ee48 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
1ee49 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
1ee4a 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f  lite3ExprFunctio
1ee4b 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
1ee4c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1ee4d 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29  , Token *pToken)
1ee4e 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a  {.  Expr *pNew;.
1ee4f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1ee50 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
1ee51 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a  sert( pToken );.
1ee52 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
1ee53 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
1ee54 20 73 69 7a 65 6f 66 28 45 78 70 72 29 20 29 3b   sizeof(Expr) );
1ee55 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
1ee56 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
1ee57 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
1ee58 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69 64  pList); /* Avoid
1ee59 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77 68 65   memory leak whe
1ee5a 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a  n malloc fails *
1ee5b 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
1ee5c 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d    }.  pNew->op =
1ee5d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20   TK_FUNCTION;.  
1ee5e 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20  pNew->x.pList = 
1ee5f 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28  pList;.  assert(
1ee60 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
1ee61 79 28 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65  y(pNew, EP_xIsSe
1ee62 6c 65 63 74 29 20 29 3b 0a 20 20 61 73 73 65 72  lect) );.  asser
1ee63 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d  t( pToken->dyn==
1ee64 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 73 70 61  0 );.  pNew->spa
1ee65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 73  n = *pToken;.  s
1ee66 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28  qlite3TokenCopy(
1ee67 64 62 2c 20 26 70 4e 65 77 2d 3e 74 6f 6b 65 6e  db, &pNew->token
1ee68 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c  , pToken);.  sql
1ee69 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
1ee6a 74 28 70 50 61 72 73 65 2c 20 70 4e 65 77 29 3b  t(pParse, pNew);
1ee6b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
1ee6c 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20  }../*.** Assign 
1ee6d 61 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65  a variable numbe
1ee6e 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69  r to an expressi
1ee6f 6f 6e 20 74 68 61 74 20 65 6e 63 6f 64 65 73 20  on that encodes 
1ee70 61 20 77 69 6c 64 63 61 72 64 0a 2a 2a 20 69 6e  a wildcard.** in
1ee71 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
1ee72 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a  L statement.  .*
1ee73 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63  *.** Wildcards c
1ee74 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73  onsisting of a s
1ee75 69 6e 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73  ingle "?" are as
1ee76 73 69 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20  signed the next 
1ee77 73 65 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 76 61  sequential.** va
1ee78 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a  riable number..*
1ee79 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f  *.** Wildcards o
1ee7a 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e  f the form "?nnn
1ee7b 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74  " are assigned t
1ee7c 68 65 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22 2e  he number "nnn".
1ee7d 20 20 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 75 72    We make.** sur
1ee7e 65 20 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 20 74  e "nnn" is not t
1ee7f 6f 6f 20 62 65 20 74 6f 20 61 76 6f 69 64 20 61  oo be to avoid a
1ee80 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69   denial of servi
1ee81 63 65 20 61 74 74 61 63 6b 20 77 68 65 6e 0a 2a  ce attack when.*
1ee82 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  * the SQL statem
1ee83 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61  ent comes from a
1ee84 6e 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63  n external sourc
1ee85 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72  e..**.** Wildcar
1ee86 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  ds of the form "
1ee87 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 22 20  :aaa" or "$aaa" 
1ee88 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65  are assigned the
1ee89 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20   same number.** 
1ee8a 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  as the previous 
1ee8b 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
1ee8c 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20  same wildcard.  
1ee8d 4f 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68  Or if this is th
1ee8e 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61  e first.** insta
1ee8f 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63  nce of the wildc
1ee90 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65  ard, the next se
1ee91 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65  quenial variable
1ee92 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73   number is.** as
1ee93 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  signed..*/.SQLIT
1ee94 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1ee95 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e  qlite3ExprAssign
1ee96 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20  VarNumber(Parse 
1ee97 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1ee98 45 78 70 72 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a  Expr){.  Token *
1ee99 70 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65  pToken;.  sqlite
1ee9a 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1ee9b 64 62 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72  db;..  if( pExpr
1ee9c 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1ee9d 70 54 6f 6b 65 6e 20 3d 20 26 70 45 78 70 72 2d  pToken = &pExpr-
1ee9e 3e 74 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74  >token;.  assert
1ee9f 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31 20 29  ( pToken->n>=1 )
1eea0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b  ;.  assert( pTok
1eea1 65 6e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 61 73  en->z!=0 );.  as
1eea2 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b  sert( pToken->z[
1eea3 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  0]!=0 );.  if( p
1eea4 54 6f 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b 0a 20  Token->n==1 ){. 
1eea5 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f     /* Wildcard o
1eea6 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20  f the form "?". 
1eea7 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74   Assign the next
1eea8 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72   variable number
1eea9 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d 3e 69   */.    pExpr->i
1eeaa 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65  Table = ++pParse
1eeab 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73 65 20  ->nVar;.  }else 
1eeac 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d  if( pToken->z[0]
1eead 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20  =='?' ){.    /* 
1eeae 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20  Wildcard of the 
1eeaf 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f  form "?nnn".  Co
1eeb0 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61  nvert "nnn" to a
1eeb1 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a 20 20  n integer and.  
1eeb2 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73 20 74    ** use it as t
1eeb3 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  he variable numb
1eeb4 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b  er */.    int i;
1eeb5 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  .    pExpr->iTab
1eeb6 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28 28 63  le = i = atoi((c
1eeb7 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e 7a 5b  har*)&pToken->z[
1eeb8 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  1]);.    testcas
1eeb9 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20 74  e( i==0 );.    t
1eeba 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b  estcase( i==1 );
1eebb 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69  .    testcase( i
1eebc 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
1eebd 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42  ITE_LIMIT_VARIAB
1eebe 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a  LE_NUMBER]-1 );.
1eebf 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
1eec0 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  =db->aLimit[SQLI
1eec1 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c  TE_LIMIT_VARIABL
1eec2 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20  E_NUMBER] );.   
1eec3 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 64 62   if( i<1 || i>db
1eec4 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1eec5 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
1eec6 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 20 20  UMBER] ){.      
1eec7 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1eec8 70 50 61 72 73 65 2c 20 22 76 61 72 69 61 62 6c  pParse, "variabl
1eec9 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 62 65  e number must be
1eeca 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e 64 20   between ?1 and 
1eecb 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20  ?%d",.          
1eecc 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1eecd 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
1eece 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20 20 7d  _NUMBER]);.    }
1eecf 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73  .    if( i>pPars
1eed0 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 20  e->nVar ){.     
1eed1 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20   pParse->nVar = 
1eed2 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  i;.    }.  }else
1eed3 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72  {.    /* Wildcar
1eed4 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  ds of the form "
1eed5 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 22 2e  :aaa" or "$aaa".
1eed6 20 20 52 65 75 73 65 20 74 68 65 20 73 61 6d 65    Reuse the same
1eed7 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a   variable.    **
1eed8 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65 20 70   number as the p
1eed9 72 69 6f 72 20 61 70 70 65 61 72 61 6e 63 65 20  rior appearance 
1eeda 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  of the same name
1eedb 2c 20 6f 72 20 69 66 20 74 68 65 20 6e 61 6d 65  , or if the name
1eedc 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 65 76 65  .    ** has neve
1eedd 72 20 61 70 70 65 61 72 65 64 20 62 65 66 6f 72  r appeared befor
1eede 65 2c 20 72 65 75 73 65 20 74 68 65 20 73 61 6d  e, reuse the sam
1eedf 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65  e variable numbe
1eee0 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  r.    */.    int
1eee1 20 69 3b 0a 20 20 20 20 75 33 32 20 6e 3b 0a 20   i;.    u32 n;. 
1eee2 20 20 20 6e 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e     n = pToken->n
1eee3 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
1eee4 3c 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70  <pParse->nVarExp
1eee5 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45  r; i++){.      E
1eee6 78 70 72 20 2a 70 45 3b 0a 20 20 20 20 20 20 69  xpr *pE;.      i
1eee7 66 28 20 28 70 45 20 3d 20 70 50 61 72 73 65 2d  f( (pE = pParse-
1eee8 3e 61 70 56 61 72 45 78 70 72 5b 69 5d 29 21 3d  >apVarExpr[i])!=
1eee9 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 70  0.          && p
1eeea 45 2d 3e 74 6f 6b 65 6e 2e 6e 3d 3d 6e 0a 20 20  E->token.n==n.  
1eeeb 20 20 20 20 20 20 20 20 26 26 20 6d 65 6d 63 6d          && memcm
1eeec 70 28 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70  p(pE->token.z, p
1eeed 54 6f 6b 65 6e 2d 3e 7a 2c 20 6e 29 3d 3d 30 20  Token->z, n)==0 
1eeee 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  ){.        pExpr
1eeef 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 2d 3e 69  ->iTable = pE->i
1eef0 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 62  Table;.        b
1eef1 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1eef2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d 70    }.    if( i>=p
1eef3 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 20  Parse->nVarExpr 
1eef4 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  ){.      pExpr->
1eef5 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73  iTable = ++pPars
1eef6 65 2d 3e 6e 56 61 72 3b 0a 20 20 20 20 20 20 69  e->nVar;.      i
1eef7 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  f( pParse->nVarE
1eef8 78 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61  xpr>=pParse->nVa
1eef9 72 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a  rExprAlloc-1 ){.
1eefa 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
1eefb 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d  nVarExprAlloc +=
1eefc 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70   pParse->nVarExp
1eefd 72 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20  rAlloc + 10;.   
1eefe 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56       pParse->apV
1eeff 61 72 45 78 70 72 20 3d 0a 20 20 20 20 20 20 20  arExpr =.       
1ef00 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65       sqlite3DbRe
1ef01 61 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 20  allocOrFree(.   
1ef02 20 20 20 20 20 20 20 20 20 20 20 64 62 2c 0a 20             db,. 
1ef03 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
1ef04 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a  rse->apVarExpr,.
1ef05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
1ef06 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c  arse->nVarExprAl
1ef07 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 73  loc*sizeof(pPars
1ef08 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 5d 29  e->apVarExpr[0])
1ef09 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a  .            );.
1ef0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1ef0b 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
1ef0c 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61  led ){.        a
1ef0d 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61  ssert( pParse->a
1ef0e 70 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a 20  pVarExpr!=0 );. 
1ef0f 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61         pParse->a
1ef10 70 56 61 72 45 78 70 72 5b 70 50 61 72 73 65 2d  pVarExpr[pParse-
1ef11 3e 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70  >nVarExpr++] = p
1ef12 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Expr;.      }.  
1ef13 20 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 21    }.  } .  if( !
1ef14 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 20  pParse->nErr && 
1ef15 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62 2d  pParse->nVar>db-
1ef16 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1ef17 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55  IMIT_VARIABLE_NU
1ef18 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 71 6c  MBER] ){.    sql
1ef19 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1ef1a 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53  rse, "too many S
1ef1b 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b 0a  QL variables");.
1ef1c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65    }.}../*.** Cle
1ef1d 61 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ar an expression
1ef1e 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 6f   structure witho
1ef1f 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20  ut deleting the 
1ef20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
1ef21 2e 0a 2a 2a 20 53 75 62 73 74 72 75 63 74 75 72  ..** Substructur
1ef22 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f  e is deleted..*/
1ef23 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1ef24 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1ef25 43 6c 65 61 72 28 73 71 6c 69 74 65 33 20 2a 64  Clear(sqlite3 *d
1ef26 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69  b, Expr *p){.  i
1ef27 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20  f( p->token.dyn 
1ef28 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  ) sqlite3DbFree(
1ef29 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 74 6f  db, (char*)p->to
1ef2a 6b 65 6e 2e 7a 29 3b 0a 20 20 69 66 28 20 21 45  ken.z);.  if( !E
1ef2b 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
1ef2c 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  y(p, EP_TokenOnl
1ef2d 79 7c 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20  y|EP_SpanToken) 
1ef2e 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 73 70  ){.    if( p->sp
1ef2f 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33  an.dyn ) sqlite3
1ef30 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61 72  DbFree(db, (char
1ef31 2a 29 70 2d 3e 73 70 61 6e 2e 7a 29 3b 0a 20 20  *)p->span.z);.  
1ef32 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
1ef33 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75  perty(p, EP_Redu
1ef34 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  ced) ){.      /*
1ef35 20 53 75 62 74 72 65 65 73 20 61 72 65 20 70 61   Subtrees are pa
1ef36 72 74 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6d  rt of the same m
1ef37 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
1ef38 20 77 68 65 6e 20 45 50 5f 52 65 64 75 63 65 64   when EP_Reduced
1ef39 20 73 65 74 20 2a 2f 0a 20 20 20 20 20 20 69 66   set */.      if
1ef3a 28 20 70 2d 3e 70 4c 65 66 74 20 29 20 73 71 6c  ( p->pLeft ) sql
1ef3b 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 62  ite3ExprClear(db
1ef3c 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20  , p->pLeft);.   
1ef3d 20 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74     if( p->pRight
1ef3e 20 29 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c   ) sqlite3ExprCl
1ef3f 65 61 72 28 64 62 2c 20 70 2d 3e 70 52 69 67 68  ear(db, p->pRigh
1ef40 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
1ef41 20 20 20 20 20 2f 2a 20 53 75 62 74 72 65 65 73       /* Subtrees
1ef42 20 61 72 65 20 73 65 70 61 72 61 74 65 20 61 6c   are separate al
1ef43 6c 6f 63 61 74 69 6f 6e 73 20 77 68 65 6e 20 45  locations when E
1ef44 50 5f 52 65 64 75 63 65 64 20 69 73 20 63 6c 65  P_Reduced is cle
1ef45 61 72 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ar */.      sqli
1ef46 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1ef47 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20  , p->pLeft);.   
1ef48 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
1ef49 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 67  lete(db, p->pRig
1ef4a 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  ht);.    }.    /
1ef4b 2a 20 78 2e 70 53 65 6c 65 63 74 20 61 6e 64 20  * x.pSelect and 
1ef4c 78 2e 70 4c 69 73 74 20 61 72 65 20 61 6c 77 61  x.pList are alwa
1ef4d 79 73 20 73 65 70 61 72 61 74 65 6c 79 20 61 6c  ys separately al
1ef4e 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20 20 69  located */.    i
1ef4f 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
1ef50 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65  ty(p, EP_xIsSele
1ef51 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ct) ){.      sql
1ef52 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
1ef53 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63  (db, p->x.pSelec
1ef54 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
1ef55 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1ef56 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
1ef57 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20  ->x.pList);.    
1ef58 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
1ef59 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74  ecursively delet
1ef5a 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  e an expression 
1ef5b 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tree..*/.SQLITE_
1ef5c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1ef5d 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 73  ite3ExprDelete(s
1ef5e 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
1ef5f 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30   *p){.  if( p==0
1ef60 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c   ) return;.  sql
1ef61 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 62  ite3ExprClear(db
1ef62 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  , p);.  sqlite3D
1ef63 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a  bFree(db, p);.}.
1ef64 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1ef65 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1ef66 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
1ef67 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73  the expression s
1ef68 74 72 75 63 74 75 72 65 20 0a 2a 2a 20 70 61 73  tructure .** pas
1ef69 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
1ef6a 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20   argument. This 
1ef6b 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66  is always one of
1ef6c 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 0a   EXPR_FULLSIZE,.
1ef6d 2a 2a 20 45 58 50 52 5f 52 45 44 55 43 45 44 53  ** EXPR_REDUCEDS
1ef6e 49 5a 45 20 6f 72 20 45 58 50 52 5f 54 4f 4b 45  IZE or EXPR_TOKE
1ef6f 4e 4f 4e 4c 59 53 49 5a 45 2e 0a 2a 2f 0a 73 74  NONLYSIZE..*/.st
1ef70 61 74 69 63 20 69 6e 74 20 65 78 70 72 53 74 72  atic int exprStr
1ef71 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 29  uctSize(Expr *p)
1ef72 7b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  {.  if( ExprHasP
1ef73 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f  roperty(p, EP_To
1ef74 6b 65 6e 4f 6e 6c 79 29 20 29 20 72 65 74 75 72  kenOnly) ) retur
1ef75 6e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59  n EXPR_TOKENONLY
1ef76 53 49 5a 45 3b 0a 20 20 69 66 28 20 45 78 70 72  SIZE;.  if( Expr
1ef77 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
1ef78 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 29 20 72  P_SpanToken) ) r
1ef79 65 74 75 72 6e 20 45 58 50 52 5f 53 50 41 4e 54  eturn EXPR_SPANT
1ef7a 4f 4b 45 4e 53 49 5a 45 3b 0a 20 20 69 66 28 20  OKENSIZE;.  if( 
1ef7b 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1ef7c 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29  p, EP_Reduced) )
1ef7d 20 72 65 74 75 72 6e 20 45 58 50 52 5f 52 45 44   return EXPR_RED
1ef7e 55 43 45 44 53 49 5a 45 3b 0a 20 20 72 65 74 75  UCEDSIZE;.  retu
1ef7f 72 6e 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45  rn EXPR_FULLSIZE
1ef80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74  ;.}../*.** sqlit
1ef81 65 33 45 78 70 72 44 75 70 28 29 20 68 61 73 20  e3ExprDup() has 
1ef82 62 65 65 6e 20 63 61 6c 6c 65 64 20 74 6f 20 63  been called to c
1ef83 72 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20  reate a copy of 
1ef84 65 78 70 72 65 73 73 69 6f 6e 20 70 20 77 69 74  expression p wit
1ef85 68 0a 2a 2a 20 74 68 65 20 45 58 50 52 44 55 50  h.** the EXPRDUP
1ef86 5f 58 58 58 20 66 6c 61 67 73 20 70 61 73 73 65  _XXX flags passe
1ef87 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
1ef88 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 66  argument. This f
1ef89 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 72 65 74 75  unction .** retu
1ef8a 72 6e 73 20 74 68 65 20 73 70 61 63 65 20 72 65  rns the space re
1ef8b 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 63  quired for the c
1ef8c 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 20  opy of the Expr 
1ef8d 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 2e 0a  structure only..
1ef8e 2a 2a 20 54 68 69 73 20 69 73 20 61 6c 77 61 79  ** This is alway
1ef8f 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55  s one of EXPR_FU
1ef90 4c 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52 45 44  LLSIZE, EXPR_RED
1ef91 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50 52  UCEDSIZE or EXPR
1ef92 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 0a  _TOKENONLYSIZE..
1ef93 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75  */.static int du
1ef94 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a  pedExprStructSiz
1ef95 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66  e(Expr *p, int f
1ef96 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 53 69  lags){.  int nSi
1ef97 7a 65 3b 0a 20 20 69 66 28 20 30 3d 3d 28 66 6c  ze;.  if( 0==(fl
1ef98 61 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55  ags&EXPRDUP_REDU
1ef99 43 45 29 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65  CE) ){.    nSize
1ef9a 20 3d 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45   = EXPR_FULLSIZE
1ef9b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
1ef9c 3e 70 4c 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69  >pLeft || p->pRi
1ef9d 67 68 74 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20  ght || p->pColl 
1ef9e 7c 7c 20 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b  || p->x.pList ){
1ef9f 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50  .    nSize = EXP
1efa0 52 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20  R_REDUCEDSIZE;. 
1efa1 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73   }else if( flags
1efa2 26 45 58 50 52 44 55 50 5f 53 50 41 4e 20 29 7b  &EXPRDUP_SPAN ){
1efa3 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50  .    nSize = EXP
1efa4 52 5f 53 50 41 4e 54 4f 4b 45 4e 53 49 5a 45 3b  R_SPANTOKENSIZE;
1efa5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 53  .  }else{.    nS
1efa6 69 7a 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e  ize = EXPR_TOKEN
1efa7 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 7d 0a 20 20  ONLYSIZE;.  }.  
1efa8 72 65 74 75 72 6e 20 6e 53 69 7a 65 3b 0a 7d 0a  return nSize;.}.
1efa9 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 45 78  ./*.** sqlite3Ex
1efaa 70 72 44 75 70 28 29 20 68 61 73 20 62 65 65 6e  prDup() has been
1efab 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
1efac 65 20 61 20 63 6f 70 79 20 6f 66 20 65 78 70 72  e a copy of expr
1efad 65 73 73 69 6f 6e 20 70 20 77 69 74 68 0a 2a 2a  ession p with.**
1efae 20 74 68 65 20 45 58 50 52 44 55 50 5f 58 58 58   the EXPRDUP_XXX
1efaf 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
1efb0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20  econd argument. 
1efb1 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
1efb2 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 73 70 61  turns.** the spa
1efb3 63 65 20 69 6e 20 62 79 74 65 73 20 72 65 71 75  ce in bytes requ
1efb4 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  ired to store th
1efb5 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78  e copy of the Ex
1efb6 70 72 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  pr structure.** 
1efb7 61 6e 64 20 74 68 65 20 63 6f 70 69 65 73 20 6f  and the copies o
1efb8 66 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e  f the Expr.token
1efb9 2e 7a 20 61 6e 64 20 45 78 70 72 2e 73 70 61 6e  .z and Expr.span
1efba 2e 7a 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c  .z (if applicabl
1efbb 65 29 0a 2a 2a 20 73 74 72 69 6e 67 20 62 75 66  e).** string buf
1efbc 66 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  fers..*/.static 
1efbd 69 6e 74 20 64 75 70 65 64 45 78 70 72 4e 6f 64  int dupedExprNod
1efbe 65 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69  eSize(Expr *p, i
1efbf 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74  nt flags){.  int
1efc0 20 6e 42 79 74 65 20 3d 20 64 75 70 65 64 45 78   nByte = dupedEx
1efc1 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20  prStructSize(p, 
1efc2 66 6c 61 67 73 29 20 2b 20 28 70 2d 3e 74 6f 6b  flags) + (p->tok
1efc3 65 6e 2e 7a 20 3f 20 70 2d 3e 74 6f 6b 65 6e 2e  en.z ? p->token.
1efc4 6e 20 2b 20 31 20 3a 20 30 29 3b 0a 20 20 69 66  n + 1 : 0);.  if
1efc5 28 20 28 66 6c 61 67 73 26 45 58 50 52 44 55 50  ( (flags&EXPRDUP
1efc6 5f 53 50 41 4e 29 21 3d 30 0a 20 20 20 26 26 20  _SPAN)!=0.   && 
1efc7 28 70 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 70 2d 3e  (p->token.z!=p->
1efc8 73 70 61 6e 2e 7a 20 7c 7c 20 70 2d 3e 74 6f 6b  span.z || p->tok
1efc9 65 6e 2e 6e 21 3d 70 2d 3e 73 70 61 6e 2e 6e 29  en.n!=p->span.n)
1efca 0a 20 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20  .  ){.    nByte 
1efcb 2b 3d 20 70 2d 3e 73 70 61 6e 2e 6e 3b 0a 20 20  += p->span.n;.  
1efcc 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44  }.  return ROUND
1efcd 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a  8(nByte);.}../*.
1efce 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1efcf 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
1efd0 71 75 69 72 65 64 20 74 6f 20 63 72 65 61 74 65  quired to create
1efd1 20 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 20   a duplicate of 
1efd2 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69  the .** expressi
1efd3 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65  on passed as the
1efd4 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
1efd5 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
1efd6 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20 6d 61 73  ment is a.** mas
1efd7 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50  k containing EXP
1efd8 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a  RDUP_XXX flags..
1efd9 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
1efda 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65  returned include
1efdb 73 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74  s space to creat
1efdc 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  e a copy of the 
1efdd 45 78 70 72 20 73 74 72 75 63 74 0a 2a 2a 20 69  Expr struct.** i
1efde 74 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62 75  tself and the bu
1efdf 66 66 65 72 20 72 65 66 65 72 72 65 64 20 74 6f  ffer referred to
1efe0 20 62 79 20 45 78 70 72 2e 74 6f 6b 65 6e 2c 20   by Expr.token, 
1efe1 69 66 20 61 6e 79 2e 20 49 66 20 74 68 65 20 0a  if any. If the .
1efe2 2a 2a 20 45 58 50 52 44 55 50 5f 53 50 41 4e 20  ** EXPRDUP_SPAN 
1efe3 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
1efe4 6e 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74  n space to creat
1efe5 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  e a copy of the 
1efe6 62 75 66 66 65 72 0a 2a 2a 20 72 65 66 65 72 65  buffer.** refere
1efe7 64 20 74 6f 20 62 79 20 45 78 70 72 2e 73 70 61  d to by Expr.spa
1efe8 6e 20 69 73 20 61 6c 73 6f 20 69 6e 63 6c 75 64  n is also includ
1efe9 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
1efea 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20   EXPRDUP_REDUCE 
1efeb 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
1efec 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  n the return val
1efed 75 65 20 69 6e 63 6c 75 64 65 73 20 0a 2a 2a 20  ue includes .** 
1efee 73 70 61 63 65 20 74 6f 20 64 75 70 6c 69 63 61  space to duplica
1efef 74 65 20 61 6c 6c 20 45 78 70 72 20 6e 6f 64 65  te all Expr node
1eff0 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 66 6f  s in the tree fo
1eff1 72 6d 65 64 20 62 79 20 45 78 70 72 2e 70 4c 65  rmed by Expr.pLe
1eff2 66 74 20 0a 2a 2a 20 61 6e 64 20 45 78 70 72 2e  ft .** and Expr.
1eff3 70 52 69 67 68 74 20 76 61 72 69 61 62 6c 65 73  pRight variables
1eff4 20 28 62 75 74 20 6e 6f 74 20 66 6f 72 20 61 6e   (but not for an
1eff5 79 20 73 74 72 75 63 74 75 72 65 73 20 70 6f 69  y structures poi
1eff6 6e 74 65 64 20 74 6f 20 6f 72 20 0a 2a 2a 20 64  nted to or .** d
1eff7 65 73 63 65 6e 64 65 64 20 66 72 6f 6d 20 74 68  escended from th
1eff8 65 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f  e Expr.x.pList o
1eff9 72 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74  r Expr.x.pSelect
1effa 20 76 61 72 69 61 62 6c 65 73 29 2e 0a 2a 2f 0a   variables)..*/.
1effb 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 64  static int duped
1effc 45 78 70 72 53 69 7a 65 28 45 78 70 72 20 2a 70  ExprSize(Expr *p
1effd 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
1effe 69 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20  int nByte = 0;. 
1efff 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 42   if( p ){.    nB
1f000 79 74 65 20 3d 20 64 75 70 65 64 45 78 70 72 4e  yte = dupedExprN
1f001 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 73  odeSize(p, flags
1f002 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  );.    if( flags
1f003 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20  &EXPRDUP_REDUCE 
1f004 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 20 3d  ){.      int f =
1f005 20 66 6c 61 67 73 26 28 7e 45 58 50 52 44 55 50   flags&(~EXPRDUP
1f006 5f 53 50 41 4e 29 3b 0a 20 20 20 20 20 20 6e 42  _SPAN);.      nB
1f007 79 74 65 20 2b 3d 20 64 75 70 65 64 45 78 70 72  yte += dupedExpr
1f008 53 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20 66  Size(p->pLeft, f
1f009 29 20 2b 20 64 75 70 65 64 45 78 70 72 53 69 7a  ) + dupedExprSiz
1f00a 65 28 70 2d 3e 70 52 69 67 68 74 2c 20 66 29 3b  e(p->pRight, f);
1f00b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1f00c 75 72 6e 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a  urn nByte;.}../*
1f00d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1f00e 6e 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20  n is similar to 
1f00f 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29  sqlite3ExprDup()
1f010 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 69 66  , except that if
1f011 20 70 7a 42 75 66 66 65 72 20 0a 2a 2a 20 69 73   pzBuffer .** is
1f012 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a   not NULL then *
1f013 70 7a 42 75 66 66 65 72 20 69 73 20 61 73 73 75  pzBuffer is assu
1f014 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  med to point to 
1f015 61 20 62 75 66 66 65 72 20 6c 61 72 67 65 20 65  a buffer large e
1f016 6e 6f 75 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f  nough .** to sto
1f017 72 65 20 74 68 65 20 63 6f 70 79 20 6f 66 20 65  re the copy of e
1f018 78 70 72 65 73 73 69 6f 6e 20 70 2c 20 74 68 65  xpression p, the
1f019 20 63 6f 70 69 65 73 20 6f 66 20 70 2d 3e 74 6f   copies of p->to
1f01a 6b 65 6e 20 61 6e 64 20 70 2d 3e 73 70 61 6e 20  ken and p->span 
1f01b 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 62  .** (if applicab
1f01c 6c 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f 70  le), and the cop
1f01d 69 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c  ies of the p->pL
1f01e 65 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 68  eft and p->pRigh
1f01f 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a  t expressions,.*
1f020 2a 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 65  * if any. Before
1f021 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42   returning, *pzB
1f022 75 66 66 65 72 20 69 73 20 73 65 74 20 74 6f 20  uffer is set to 
1f023 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70  the first byte p
1f024 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72  assed the.** por
1f025 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 66  tion of the buff
1f026 65 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62  er copied into b
1f027 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
1f028 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20  .*/.static Expr 
1f029 2a 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 33  *exprDup(sqlite3
1f02a 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69   *db, Expr *p, i
1f02b 6e 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70  nt flags, u8 **p
1f02c 7a 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 72  zBuffer){.  Expr
1f02d 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 20   *pNew = 0;     
1f02e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f02f 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74   /* Value to ret
1f030 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29  urn */.  if( p )
1f031 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  {.    const int 
1f032 69 73 52 65 71 75 69 72 65 53 70 61 6e 20 3d 20  isRequireSpan = 
1f033 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 53  (flags&EXPRDUP_S
1f034 50 41 4e 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20  PAN);.    const 
1f035 69 6e 74 20 69 73 52 65 64 75 63 65 64 20 3d 20  int isReduced = 
1f036 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52  (flags&EXPRDUP_R
1f037 45 44 55 43 45 29 3b 0a 20 20 20 20 75 38 20 2a  EDUCE);.    u8 *
1f038 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 61 73 73  zAlloc;..    ass
1f039 65 72 74 28 20 70 7a 42 75 66 66 65 72 3d 3d 30  ert( pzBuffer==0
1f03a 20 7c 7c 20 69 73 52 65 64 75 63 65 64 20 29 3b   || isReduced );
1f03b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  ..    /* Figure 
1f03c 6f 75 74 20 77 68 65 72 65 20 74 6f 20 77 72 69  out where to wri
1f03d 74 65 20 74 68 65 20 6e 65 77 20 45 78 70 72 20  te the new Expr 
1f03e 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20  structure. */.  
1f03f 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20 29    if( pzBuffer )
1f040 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d  {.      zAlloc =
1f041 20 2a 70 7a 42 75 66 66 65 72 3b 0a 20 20 20 20   *pzBuffer;.    
1f042 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 41 6c  }else{.      zAl
1f043 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  loc = sqlite3DbM
1f044 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 64 75 70  allocRaw(db, dup
1f045 65 64 45 78 70 72 53 69 7a 65 28 70 2c 20 66 6c  edExprSize(p, fl
1f046 61 67 73 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ags));.    }.   
1f047 20 70 4e 65 77 20 3d 20 28 45 78 70 72 20 2a 29   pNew = (Expr *)
1f048 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 69 66 28  zAlloc;..    if(
1f049 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 2f   pNew ){.      /
1f04a 2a 20 53 65 74 20 6e 4e 65 77 53 69 7a 65 20 74  * Set nNewSize t
1f04b 6f 20 74 68 65 20 73 69 7a 65 20 61 6c 6c 6f 63  o the size alloc
1f04c 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 74 72  ated for the str
1f04d 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74  ucture pointed t
1f04e 6f 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 70 4e  o.      ** by pN
1f04f 65 77 2e 20 54 68 69 73 20 69 73 20 65 69 74 68  ew. This is eith
1f050 65 72 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45  er EXPR_FULLSIZE
1f051 2c 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49  , EXPR_REDUCEDSI
1f052 5a 45 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 45  ZE or.      ** E
1f053 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a  XPR_TOKENONLYSIZ
1f054 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73 65 74  E. nToken is set
1f055 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
1f056 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64  f bytes consumed
1f057 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 65  .      ** by the
1f058 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 2d 3e   copy of the p->
1f059 74 6f 6b 65 6e 2e 7a 20 73 74 72 69 6e 67 20 28  token.z string (
1f05a 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 20 20 2a  if any)..      *
1f05b 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e  /.      const in
1f05c 74 20 6e 4e 65 77 53 69 7a 65 20 3d 20 64 75 70  t nNewSize = dup
1f05d 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65  edExprStructSize
1f05e 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20  (p, flags);.    
1f05f 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 6f 6b    const int nTok
1f060 65 6e 20 3d 20 28 70 2d 3e 74 6f 6b 65 6e 2e 7a  en = (p->token.z
1f061 20 3f 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 20 2b 20   ? p->token.n + 
1f062 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 69 66  1 : 0);.      if
1f063 28 20 69 73 52 65 64 75 63 65 64 20 29 7b 0a 20  ( isReduced ){. 
1f064 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 45         assert( E
1f065 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
1f066 2c 20 45 50 5f 52 65 64 75 63 65 64 29 3d 3d 30  , EP_Reduced)==0
1f067 20 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63   );.        memc
1f068 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 4e  py(zAlloc, p, nN
1f069 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d  ewSize);.      }
1f06a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
1f06b 74 20 6e 53 69 7a 65 20 3d 20 65 78 70 72 53 74  t nSize = exprSt
1f06c 72 75 63 74 53 69 7a 65 28 70 29 3b 0a 20 20 20  ructSize(p);.   
1f06d 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c       memcpy(zAll
1f06e 6f 63 2c 20 70 2c 20 6e 53 69 7a 65 29 3b 0a 20  oc, p, nSize);. 
1f06f 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 7a         memset(&z
1f070 41 6c 6c 6f 63 5b 6e 53 69 7a 65 5d 2c 20 30 2c  Alloc[nSize], 0,
1f071 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2d 6e   EXPR_FULLSIZE-n
1f072 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  Size);.      }..
1f073 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65        /* Set the
1f074 20 45 50 5f 52 65 64 75 63 65 64 20 61 6e 64 20   EP_Reduced and 
1f075 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61  EP_TokenOnly fla
1f076 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  gs appropriately
1f077 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 2d  . */.      pNew-
1f078 3e 66 6c 61 67 73 20 26 3d 20 7e 28 45 50 5f 52  >flags &= ~(EP_R
1f079 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f  educed|EP_TokenO
1f07a 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 6f 6b 65 6e  nly|EP_SpanToken
1f07b 29 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  );.      switch(
1f07c 20 6e 4e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20   nNewSize ){.   
1f07d 20 20 20 20 20 63 61 73 65 20 45 58 50 52 5f 52       case EXPR_R
1f07e 45 44 55 43 45 44 53 49 5a 45 3a 20 20 20 70 4e  EDUCEDSIZE:   pN
1f07f 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ew->flags |= EP_
1f080 52 65 64 75 63 65 64 3b 20 62 72 65 61 6b 3b 0a  Reduced; break;.
1f081 20 20 20 20 20 20 20 20 63 61 73 65 20 45 58 50          case EXP
1f082 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3a  R_TOKENONLYSIZE:
1f083 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20   pNew->flags |= 
1f084 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 3b 20 62 72  EP_TokenOnly; br
1f085 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
1f086 65 20 45 58 50 52 5f 53 50 41 4e 54 4f 4b 45 4e  e EXPR_SPANTOKEN
1f087 53 49 5a 45 3a 20 70 4e 65 77 2d 3e 66 6c 61 67  SIZE: pNew->flag
1f088 73 20 7c 3d 20 45 50 5f 53 70 61 6e 54 6f 6b 65  s |= EP_SpanToke
1f089 6e 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  n; break;.      
1f08a 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79  }..      /* Copy
1f08b 20 74 68 65 20 70 2d 3e 74 6f 6b 65 6e 20 73 74   the p->token st
1f08c 72 69 6e 67 2c 20 69 66 20 61 6e 79 2e 20 2a 2f  ring, if any. */
1f08d 0a 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65  .      if( nToke
1f08e 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 73  n ){.        uns
1f08f 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 6f 6b  igned char *zTok
1f090 65 6e 20 3d 20 26 7a 41 6c 6c 6f 63 5b 6e 4e 65  en = &zAlloc[nNe
1f091 77 53 69 7a 65 5d 3b 0a 20 20 20 20 20 20 20 20  wSize];.        
1f092 6d 65 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c 20 70  memcpy(zToken, p
1f093 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 6e 54 6f 6b 65  ->token.z, nToke
1f094 6e 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 7a 54  n-1);.        zT
1f095 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 2d 31 5d 20 3d  oken[nToken-1] =
1f096 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 70   '\0';.        p
1f097 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 3d  New->token.dyn =
1f098 20 30 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77   0;.        pNew
1f099 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 7a 54 6f 6b  ->token.z = zTok
1f09a 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  en;.      }..   
1f09b 20 20 20 69 66 28 20 30 3d 3d 28 28 70 2d 3e 66     if( 0==((p->f
1f09c 6c 61 67 73 7c 70 4e 65 77 2d 3e 66 6c 61 67 73  lags|pNew->flags
1f09d 29 20 26 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  ) & EP_TokenOnly
1f09e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
1f09f 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 4e 65 77  Fill in the pNew
1f0a0 2d 3e 73 70 61 6e 20 74 6f 6b 65 6e 2c 20 69 66  ->span token, if
1f0a1 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20   required. */.  
1f0a2 20 20 20 20 20 20 69 66 28 20 69 73 52 65 71 75        if( isRequ
1f0a3 69 72 65 53 70 61 6e 20 29 7b 0a 20 20 20 20 20  ireSpan ){.     
1f0a4 20 20 20 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65       if( p->toke
1f0a5 6e 2e 7a 21 3d 70 2d 3e 73 70 61 6e 2e 7a 20 7c  n.z!=p->span.z |
1f0a6 7c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 70 2d  | p->token.n!=p-
1f0a7 3e 73 70 61 6e 2e 6e 20 29 7b 0a 20 20 20 20 20  >span.n ){.     
1f0a8 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 73 70 61         pNew->spa
1f0a9 6e 2e 7a 20 3d 20 26 7a 41 6c 6c 6f 63 5b 6e 4e  n.z = &zAlloc[nN
1f0aa 65 77 53 69 7a 65 2b 6e 54 6f 6b 65 6e 5d 3b 0a  ewSize+nToken];.
1f0ab 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63              memc
1f0ac 70 79 28 28 63 68 61 72 20 2a 29 70 4e 65 77 2d  py((char *)pNew-
1f0ad 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e  >span.z, p->span
1f0ae 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e 29 3b 0a  .z, p->span.n);.
1f0af 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77              pNew
1f0b0 2d 3e 73 70 61 6e 2e 64 79 6e 20 3d 20 30 3b 0a  ->span.dyn = 0;.
1f0b1 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
1f0b2 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65  .            pNe
1f0b3 77 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4e 65 77  w->span.z = pNew
1f0b4 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20  ->token.z;.     
1f0b5 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 73 70 61         pNew->spa
1f0b6 6e 2e 6e 20 3d 20 70 4e 65 77 2d 3e 74 6f 6b 65  n.n = pNew->toke
1f0b7 6e 2e 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  n.n;.          }
1f0b8 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1f0b9 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
1f0ba 73 70 61 6e 2e 7a 20 3d 20 30 3b 0a 20 20 20 20  span.z = 0;.    
1f0bb 20 20 20 20 20 20 70 4e 65 77 2d 3e 73 70 61 6e        pNew->span
1f0bc 2e 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  .n = 0;.        
1f0bd 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
1f0be 20 69 66 28 20 30 3d 3d 28 28 70 2d 3e 66 6c 61   if( 0==((p->fla
1f0bf 67 73 7c 70 4e 65 77 2d 3e 66 6c 61 67 73 29 20  gs|pNew->flags) 
1f0c0 26 20 28 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  & (EP_TokenOnly|
1f0c1 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 29 20 29  EP_SpanToken)) )
1f0c2 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c  {.        /* Fil
1f0c3 6c 20 69 6e 20 74 68 65 20 70 4e 65 77 2d 3e 78  l in the pNew->x
1f0c4 2e 70 53 65 6c 65 63 74 20 6f 72 20 70 4e 65 77  .pSelect or pNew
1f0c5 2d 3e 78 2e 70 4c 69 73 74 20 6d 65 6d 62 65 72  ->x.pList member
1f0c6 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  . */.        if(
1f0c7 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1f0c8 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  (p, EP_xIsSelect
1f0c9 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
1f0ca 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d  New->x.pSelect =
1f0cb 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
1f0cc 70 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c 65  p(db, p->x.pSele
1f0cd 63 74 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a  ct, isReduced);.
1f0ce 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1f0cf 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78           pNew->x
1f0d0 2e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  .pList = sqlite3
1f0d1 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
1f0d2 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 69 73 52 65  p->x.pList, isRe
1f0d3 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20  duced);.        
1f0d4 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
1f0d5 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 70 4e 65 77   /* Fill in pNew
1f0d6 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 4e 65 77  ->pLeft and pNew
1f0d7 2d 3e 70 52 69 67 68 74 2e 20 2a 2f 0a 20 20 20  ->pRight. */.   
1f0d8 20 20 20 69 66 28 20 45 78 70 72 48 61 73 41 6e     if( ExprHasAn
1f0d9 79 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20  yProperty(pNew, 
1f0da 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f  EP_Reduced|EP_To
1f0db 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54  kenOnly|EP_SpanT
1f0dc 6f 6b 65 6e 29 20 29 7b 0a 20 20 20 20 20 20 20  oken) ){.       
1f0dd 20 7a 41 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64   zAlloc += duped
1f0de 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20  ExprNodeSize(p, 
1f0df 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20  flags);.        
1f0e0 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
1f0e1 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64  rty(pNew, EP_Red
1f0e2 75 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20  uced) ){.       
1f0e3 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d     pNew->pLeft =
1f0e4 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e   exprDup(db, p->
1f0e5 70 4c 65 66 74 2c 20 45 58 50 52 44 55 50 5f 52  pLeft, EXPRDUP_R
1f0e6 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b  EDUCE, &zAlloc);
1f0e7 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d  .          pNew-
1f0e8 3e 70 52 69 67 68 74 20 3d 20 65 78 70 72 44 75  >pRight = exprDu
1f0e9 70 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c  p(db, p->pRight,
1f0ea 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c   EXPRDUP_REDUCE,
1f0eb 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20   &zAlloc);.     
1f0ec 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1f0ed 20 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20   pzBuffer ){.   
1f0ee 20 20 20 20 20 20 20 2a 70 7a 42 75 66 66 65 72         *pzBuffer
1f0ef 20 3d 20 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20   = zAlloc;.     
1f0f0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1f0f1 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79   if( !ExprHasAny
1f0f2 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54  Property(p, EP_T
1f0f3 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e  okenOnly|EP_Span
1f0f4 54 6f 6b 65 6e 29 20 29 7b 0a 20 20 20 20 20 20  Token) ){.      
1f0f5 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20    pNew->pLeft = 
1f0f6 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
1f0f7 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 30 29 3b  b, p->pLeft, 0);
1f0f8 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  .        pNew->p
1f0f9 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45  Right = sqlite3E
1f0fa 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52  xprDup(db, p->pR
1f0fb 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  ight, 0);.      
1f0fc 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
1f0fd 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
1f0fe 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1f0ff 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69  g group of routi
1f100 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f  nes make deep co
1f101 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73 69  pies of expressi
1f102 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69  ons,.** expressi
1f103 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 73  on lists, ID lis
1f104 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 73  ts, and select s
1f105 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20  tatements.  The 
1f106 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65  copies can.** be
1f107 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 65 69   deleted (by bei
1f108 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  ng passed to the
1f109 69 72 20 72 65 73 70 65 63 74 69 76 65 20 2e 2e  ir respective ..
1f10a 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 69 6e  .Delete() routin
1f10b 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65  es).** without e
1f10c 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f 72 69  ffecting the ori
1f10d 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ginals..**.** Th
1f10e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
1f10f 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 63  t, ID, and sourc
1f110 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e 20 62  e lists return b
1f111 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  y sqlite3ExprLis
1f112 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74  tDup(),.** sqlit
1f113 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c 20 61  e3IdListDup(), a
1f114 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  nd sqlite3SrcLis
1f115 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 62  tDup() can not b
1f116 65 20 66 75 72 74 68 65 72 20 65 78 70 61 6e 64  e further expand
1f117 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 71  ed .** by subseq
1f118 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71  uent calls to sq
1f119 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 28  lite*ListAppend(
1f11a 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a  ) routines..**.*
1f11b 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 68 61  * Any tables tha
1f11c 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 6d 69  t the SrcList mi
1f11d 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 65  ght point to are
1f11e 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e   not duplicated.
1f11f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  .**.** The flags
1f120 20 70 61 72 61 6d 65 74 65 72 20 63 6f 6e 74 61   parameter conta
1f121 69 6e 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f  ins a combinatio
1f122 6e 20 6f 66 20 74 68 65 20 45 58 50 52 44 55 50  n of the EXPRDUP
1f123 5f 58 58 58 20 66 6c 61 67 73 2e 20 49 66 0a 2a  _XXX flags. If.*
1f124 2a 20 74 68 65 20 45 58 50 52 44 55 50 5f 53 50  * the EXPRDUP_SP
1f125 41 4e 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  AN flag is set i
1f126 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70  n the argument p
1f127 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 74  arameter, then t
1f128 68 65 20 0a 2a 2a 20 45 78 70 72 2e 73 70 61 6e  he .** Expr.span
1f129 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e   field of the in
1f12a 70 75 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69  put expression i
1f12b 73 20 63 6f 70 69 65 64 2e 20 49 66 20 45 58 50  s copied. If EXP
1f12c 52 44 55 50 5f 53 50 41 4e 20 69 73 0a 2a 2a 20  RDUP_SPAN is.** 
1f12d 63 6c 65 61 72 2c 20 74 68 65 6e 20 74 68 65 20  clear, then the 
1f12e 45 78 70 72 2e 73 70 61 6e 20 66 69 65 6c 64 20  Expr.span field 
1f12f 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  of the returned 
1f130 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63  expression struc
1f131 74 75 72 65 0a 2a 2a 20 69 73 20 7a 65 72 6f 65  ture.** is zeroe
1f132 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1f133 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 66  EXPRDUP_REDUCE f
1f134 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e  lag is set, then
1f135 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 72   the structure r
1f136 65 74 75 72 6e 65 64 20 69 73 20 61 0a 2a 2a 20  eturned is a.** 
1f137 74 72 75 6e 63 61 74 65 64 20 76 65 72 73 69 6f  truncated versio
1f138 6e 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20 45  n of the usual E
1f139 78 70 72 20 73 74 72 75 63 74 75 72 65 20 74 68  xpr structure th
1f13a 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65  at will be store
1f13b 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  d as.** part of 
1f13c 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  the in-memory re
1f13d 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1f13e 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
1f13f 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ema..*/.SQLITE_P
1f140 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
1f141 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c 69  ite3ExprDup(sqli
1f142 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70  te3 *db, Expr *p
1f143 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
1f144 72 65 74 75 72 6e 20 65 78 70 72 44 75 70 28 64  return exprDup(d
1f145 62 2c 20 70 2c 20 66 6c 61 67 73 2c 20 30 29 3b  b, p, flags, 0);
1f146 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
1f147 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 6f  E void sqlite3To
1f148 6b 65 6e 43 6f 70 79 28 73 71 6c 69 74 65 33 20  kenCopy(sqlite3 
1f149 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 2c  *db, Token *pTo,
1f14a 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 46   const Token *pF
1f14b 72 6f 6d 29 7b 0a 20 20 69 66 28 20 70 54 6f 2d  rom){.  if( pTo-
1f14c 3e 64 79 6e 20 29 20 73 71 6c 69 74 65 33 44 62  >dyn ) sqlite3Db
1f14d 46 72 65 65 28 64 62 2c 20 28 63 68 61 72 2a 29  Free(db, (char*)
1f14e 70 54 6f 2d 3e 7a 29 3b 0a 20 20 69 66 28 20 70  pTo->z);.  if( p
1f14f 46 72 6f 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 70  From->z ){.    p
1f150 54 6f 2d 3e 6e 20 3d 20 70 46 72 6f 6d 2d 3e 6e  To->n = pFrom->n
1f151 3b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d 20 28  ;.    pTo->z = (
1f152 75 38 2a 29 73 71 6c 69 74 65 33 44 62 53 74 72  u8*)sqlite3DbStr
1f153 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
1f154 70 46 72 6f 6d 2d 3e 7a 2c 20 70 46 72 6f 6d 2d  pFrom->z, pFrom-
1f155 3e 6e 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 64 79  >n);.    pTo->dy
1f156 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  n = 1;.  }else{.
1f157 20 20 20 20 70 54 6f 2d 3e 7a 20 3d 20 30 3b 0a      pTo->z = 0;.
1f158 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49    }.}.SQLITE_PRI
1f159 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73  VATE ExprList *s
1f15a 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
1f15b 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45  p(sqlite3 *db, E
1f15c 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20  xprList *p, int 
1f15d 66 6c 61 67 73 29 7b 0a 20 20 45 78 70 72 4c 69  flags){.  ExprLi
1f15e 73 74 20 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75  st *pNew;.  stru
1f15f 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
1f160 20 2a 70 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74   *pItem, *pOldIt
1f161 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  em;.  int i;.  i
1f162 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
1f163 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c   0;.  pNew = sql
1f164 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
1f165 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77  db, sizeof(*pNew
1f166 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  ) );.  if( pNew=
1f167 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
1f168 20 70 4e 65 77 2d 3e 69 45 43 75 72 73 6f 72 20   pNew->iECursor 
1f169 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78  = 0;.  pNew->nEx
1f16a 70 72 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f  pr = pNew->nAllo
1f16b 63 20 3d 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20  c = p->nExpr;.  
1f16c 70 4e 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20  pNew->a = pItem 
1f16d 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1f16e 63 52 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78  cRaw(db,  p->nEx
1f16f 70 72 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30  pr*sizeof(p->a[0
1f170 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 49 74 65  ]) );.  if( pIte
1f171 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  m==0 ){.    sqli
1f172 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e  te3DbFree(db, pN
1f173 65 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ew);.    return 
1f174 30 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c 64 49 74  0;.  } .  pOldIt
1f175 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f 72  em = p->a;.  for
1f176 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72  (i=0; i<p->nExpr
1f177 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c 20  ; i++, pItem++, 
1f178 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  pOldItem++){.   
1f179 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b   Expr *pNewExpr;
1f17a 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6c 64 45  .    Expr *pOldE
1f17b 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  xpr = pOldItem->
1f17c 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d  pExpr;.    pItem
1f17d 2d 3e 70 45 78 70 72 20 3d 20 70 4e 65 77 45 78  ->pExpr = pNewEx
1f17e 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  pr = sqlite3Expr
1f17f 44 75 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72  Dup(db, pOldExpr
1f180 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 49  , flags);.    pI
1f181 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c  tem->zName = sql
1f182 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
1f183 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pOldItem->zName
1f184 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f  );.    pItem->so
1f185 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 74  rtOrder = pOldIt
1f186 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20  em->sortOrder;. 
1f187 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d     pItem->done =
1f188 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69   0;.    pItem->i
1f189 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  Col = pOldItem->
1f18a 69 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d  iCol;.    pItem-
1f18b 3e 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74  >iAlias = pOldIt
1f18c 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a  em->iAlias;.  }.
1f18d 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
1f18e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f  ../*.** If curso
1f18f 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69  rs, triggers, vi
1f190 65 77 73 20 61 6e 64 20 73 75 62 71 75 65 72 69  ews and subqueri
1f191 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74  es are all omitt
1f192 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62  ed from.** the b
1f193 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20  uild, then none 
1f194 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1f195 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70   routines, excep
1f196 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65  t for .** sqlite
1f197 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61  3SelectDup(), ca
1f198 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c  n be called. sql
1f199 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 20  ite3SelectDup() 
1f19a 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  is sometimes.** 
1f19b 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55  called with a NU
1f19c 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  LL argument..*/.
1f19d 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
1f19e 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
1f19f 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
1f1a0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20  E_OMIT_TRIGGER) 
1f1a1 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  \. || !defined(S
1f1a2 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
1f1a3 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  ERY).SQLITE_PRIV
1f1a4 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
1f1a5 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 73  ite3SrcListDup(s
1f1a6 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c  qlite3 *db, SrcL
1f1a7 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  ist *p, int flag
1f1a8 73 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  s){.  SrcList *p
1f1a9 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  New;.  int i;.  
1f1aa 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28  int nByte;.  if(
1f1ab 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
1f1ac 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65  ;.  nByte = size
1f1ad 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72  of(*p) + (p->nSr
1f1ae 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e  c>0 ? sizeof(p->
1f1af 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63  a[0]) * (p->nSrc
1f1b0 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77  -1) : 0);.  pNew
1f1b1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1f1b2 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20  ocRaw(db, nByte 
1f1b3 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
1f1b4 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
1f1b5 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77  New->nSrc = pNew
1f1b6 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53  ->nAlloc = p->nS
1f1b7 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rc;.  for(i=0; i
1f1b8 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  <p->nSrc; i++){.
1f1b9 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
1f1ba 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65  st_item *pNewIte
1f1bb 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b  m = &pNew->a[i];
1f1bc 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
1f1bd 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74  ist_item *pOldIt
1f1be 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20  em = &p->a[i];. 
1f1bf 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
1f1c0 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44      pNewItem->zD
1f1c1 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65  atabase = sqlite
1f1c2 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f  3DbStrDup(db, pO
1f1c3 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73  ldItem->zDatabas
1f1c4 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  e);.    pNewItem
1f1c5 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ->zName = sqlite
1f1c6 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f  3DbStrDup(db, pO
1f1c7 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a  ldItem->zName);.
1f1c8 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41      pNewItem->zA
1f1c9 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62  lias = sqlite3Db
1f1ca 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  StrDup(db, pOldI
1f1cb 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20  tem->zAlias);.  
1f1cc 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e    pNewItem->join
1f1cd 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  type = pOldItem-
1f1ce 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 70  >jointype;.    p
1f1cf 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  NewItem->iCursor
1f1d0 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75   = pOldItem->iCu
1f1d1 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 74  rsor;.    pNewIt
1f1d2 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20  em->isPopulated 
1f1d3 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f  = pOldItem->isPo
1f1d4 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e 65  pulated;.    pNe
1f1d5 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20  wItem->zIndex = 
1f1d6 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1f1d7 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49  db, pOldItem->zI
1f1d8 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 49  ndex);.    pNewI
1f1d9 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20  tem->notIndexed 
1f1da 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49  = pOldItem->notI
1f1db 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 77  ndexed;.    pNew
1f1dc 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70  Item->pIndex = p
1f1dd 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b  OldItem->pIndex;
1f1de 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77  .    pTab = pNew
1f1df 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c  Item->pTab = pOl
1f1e0 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  dItem->pTab;.   
1f1e1 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
1f1e2 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b     pTab->nRef++;
1f1e3 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 49  .    }.    pNewI
1f1e4 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73  tem->pSelect = s
1f1e5 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
1f1e6 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53  db, pOldItem->pS
1f1e7 65 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b 0a 20  elect, flags);. 
1f1e8 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e     pNewItem->pOn
1f1e9 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
1f1ea 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
1f1eb 70 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20  pOn, flags);.   
1f1ec 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e   pNewItem->pUsin
1f1ed 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  g = sqlite3IdLis
1f1ee 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  tDup(db, pOldIte
1f1ef 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20 20  m->pUsing);.    
1f1f0 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65  pNewItem->colUse
1f1f1 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f  d = pOldItem->co
1f1f2 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74  lUsed;.  }.  ret
1f1f3 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49  urn pNew;.}.SQLI
1f1f4 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73  TE_PRIVATE IdLis
1f1f5 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74  t *sqlite3IdList
1f1f6 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Dup(sqlite3 *db,
1f1f7 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49   IdList *p){.  I
1f1f8 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69  dList *pNew;.  i
1f1f9 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30  nt i;.  if( p==0
1f1fa 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
1f1fb 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
1f1fc 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
1f1fd 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20  eof(*pNew) );.  
1f1fe 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65  if( pNew==0 ) re
1f1ff 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  turn 0;.  pNew->
1f200 6e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c  nId = pNew->nAll
1f201 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70  oc = p->nId;.  p
1f202 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33  New->a = sqlite3
1f203 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
1f204 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d  p->nId*sizeof(p-
1f205 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  >a[0]) );.  if( 
1f206 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20  pNew->a==0 ){.  
1f207 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f208 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72  db, pNew);.    r
1f209 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66  eturn 0;.  }.  f
1f20a 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64  or(i=0; i<p->nId
1f20b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
1f20c 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a  ct IdList_item *
1f20d 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77  pNewItem = &pNew
1f20e 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75  ->a[i];.    stru
1f20f 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a  ct IdList_item *
1f210 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61  pOldItem = &p->a
1f211 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  [i];.    pNewIte
1f212 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  m->zName = sqlit
1f213 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
1f214 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  OldItem->zName);
1f215 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69  .    pNewItem->i
1f216 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69  dx = pOldItem->i
1f217 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dx;.  }.  return
1f218 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f   pNew;.}.SQLITE_
1f219 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a  PRIVATE Select *
1f21a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
1f21b 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65  (sqlite3 *db, Se
1f21c 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61  lect *p, int fla
1f21d 67 73 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70  gs){.  Select *p
1f21e 4e 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  New;.  if( p==0 
1f21f 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
1f220 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
1f221 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
1f222 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20  of(*p) );.  if( 
1f223 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  pNew==0 ) return
1f224 20 30 3b 0a 20 20 2f 2a 20 41 6c 77 61 79 73 20   0;.  /* Always 
1f225 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  make a copy of t
1f226 68 65 20 73 70 61 6e 20 66 6f 72 20 74 6f 70 2d  he span for top-
1f227 6c 65 76 65 6c 20 65 78 70 72 65 73 73 69 6f 6e  level expression
1f228 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 65 78  s in the.  ** ex
1f229 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20  pression list.  
1f22a 54 68 65 20 6c 6f 67 69 63 20 69 6e 20 53 45 4c  The logic in SEL
1f22b 45 43 54 20 70 72 6f 63 65 73 73 69 6e 67 20 74  ECT processing t
1f22c 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 20  hat determines. 
1f22d 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 6f 66   ** the names of
1f22e 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
1f22f 72 65 73 75 6c 74 20 73 65 74 20 6e 65 65 64 73  result set needs
1f230 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   this informatio
1f231 6e 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 70 45 4c  n */.  pNew->pEL
1f232 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
1f233 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
1f234 70 45 4c 69 73 74 2c 20 66 6c 61 67 73 7c 45 58  pEList, flags|EX
1f235 50 52 44 55 50 5f 53 50 41 4e 29 3b 0a 20 20 70  PRDUP_SPAN);.  p
1f236 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69  New->pSrc = sqli
1f237 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 64 62  te3SrcListDup(db
1f238 2c 20 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73  , p->pSrc, flags
1f239 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72  );.  pNew->pWher
1f23a 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  e = sqlite3ExprD
1f23b 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65  up(db, p->pWhere
1f23c 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77  , flags);.  pNew
1f23d 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c  ->pGroupBy = sql
1f23e 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
1f23f 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c  db, p->pGroupBy,
1f240 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d   flags);.  pNew-
1f241 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74  >pHaving = sqlit
1f242 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d  e3ExprDup(db, p-
1f243 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73 29  >pHaving, flags)
1f244 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72  ;.  pNew->pOrder
1f245 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  By = sqlite3Expr
1f246 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  ListDup(db, p->p
1f247 4f 72 64 65 72 42 79 2c 20 66 6c 61 67 73 29 3b  OrderBy, flags);
1f248 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d  .  pNew->op = p-
1f249 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72  >op;.  pNew->pPr
1f24a 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ior = sqlite3Sel
1f24b 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 50  ectDup(db, p->pP
1f24c 72 69 6f 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20  rior, flags);.  
1f24d 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73  pNew->pLimit = s
1f24e 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
1f24f 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61  , p->pLimit, fla
1f250 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66  gs);.  pNew->pOf
1f251 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78  fset = sqlite3Ex
1f252 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66  prDup(db, p->pOf
1f253 66 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20  fset, flags);.  
1f254 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30  pNew->iLimit = 0
1f255 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65  ;.  pNew->iOffse
1f256 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73  t = 0;.  pNew->s
1f257 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c  elFlags = p->sel
1f258 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73  Flags & ~SF_Uses
1f259 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65  Ephemeral;.  pNe
1f25a 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  w->pRightmost = 
1f25b 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  0;.  pNew->addrO
1f25c 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b  penEphm[0] = -1;
1f25d 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65  .  pNew->addrOpe
1f25e 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20  nEphm[1] = -1;. 
1f25f 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45   pNew->addrOpenE
1f260 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72  phm[2] = -1;.  r
1f261 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65  eturn pNew;.}.#e
1f262 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  lse.SQLITE_PRIVA
1f263 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
1f264 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69  e3SelectDup(sqli
1f265 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20  te3 *db, Select 
1f266 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  *p, int flags){.
1f267 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29    assert( p==0 )
1f268 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
1f269 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41  #endif.../*.** A
1f26a 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74  dd a new element
1f26b 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61   to the end of a
1f26c 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  n expression lis
1f26d 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a  t.  If pList is.
1f26e 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c  ** initially NUL
1f26f 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  L, then create a
1f270 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20   new expression 
1f271 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  list..*/.SQLITE_
1f272 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74  PRIVATE ExprList
1f273 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73   *sqlite3ExprLis
1f274 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65  tAppend(.  Parse
1f275 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
1f276 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
1f277 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
1f278 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
1f279 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68     /* List to wh
1f27a 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d  ich to append. M
1f27b 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  ight be NULL */.
1f27c 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20    Expr *pExpr,  
1f27d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
1f27e 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70  ression to be ap
1f27f 70 65 6e 64 65 64 20 2a 2f 0a 20 20 54 6f 6b 65  pended */.  Toke
1f280 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20  n *pName        
1f281 20 20 20 20 2f 2a 20 41 53 20 6b 65 79 77 6f 72      /* AS keywor
1f282 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73  d for the expres
1f283 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  sion */.){.  sql
1f284 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1f285 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69  e->db;.  if( pLi
1f286 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69  st==0 ){.    pLi
1f287 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  st = sqlite3DbMa
1f288 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
1f289 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b  eof(ExprList) );
1f28a 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  .    if( pList==
1f28b 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
1f28c 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20  no_mem;.    }.  
1f28d 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
1f28e 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20  >nAlloc==0 );.  
1f28f 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e  }.  if( pList->n
1f290 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45  Alloc<=pList->nE
1f291 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63  xpr ){.    struc
1f292 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
1f293 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  *a;.    int n = 
1f294 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20  pList->nAlloc*2 
1f295 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c  + 4;.    a = sql
1f296 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
1f297 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69  , pList->a, n*si
1f298 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d  zeof(pList->a[0]
1f299 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30  ));.    if( a==0
1f29a 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e   ){.      goto n
1f29b 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  o_mem;.    }.   
1f29c 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20   pList->a = a;. 
1f29d 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63     pList->nAlloc
1f29e 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1f29f 6f 63 53 69 7a 65 28 64 62 2c 20 61 29 2f 73 69  ocSize(db, a)/si
1f2a0 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a  zeof(a[0]);.  }.
1f2a1 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
1f2a2 3e 61 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  >a!=0 );.  if( p
1f2a3 45 78 70 72 20 7c 7c 20 70 4e 61 6d 65 20 29 7b  Expr || pName ){
1f2a4 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72  .    struct Expr
1f2a5 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1f2a6 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69   = &pList->a[pLi
1f2a7 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0a 20 20  st->nExpr++];.  
1f2a8 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c 20    memset(pItem, 
1f2a9 30 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74 65 6d  0, sizeof(*pItem
1f2aa 29 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a  ));.    pItem->z
1f2ab 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
1f2ac 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
1f2ad 70 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65  pName);.    pIte
1f2ae 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72  m->pExpr = pExpr
1f2af 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c  ;.    pItem->iAl
1f2b0 69 61 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ias = 0;.  }.  r
1f2b1 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 0a 6e 6f  eturn pList;..no
1f2b2 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 20 2f 2a 20  _mem:     .  /* 
1f2b3 41 76 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65  Avoid leaking me
1f2b4 6d 6f 72 79 20 69 66 20 6d 61 6c 6c 6f 63 20 68  mory if malloc h
1f2b5 61 73 20 66 61 69 6c 65 64 2e 20 2a 2f 0a 20 20  as failed. */.  
1f2b6 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1f2b7 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20  e(db, pExpr);.  
1f2b8 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
1f2b9 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
1f2ba 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
1f2bb 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  ./*.** If the ex
1f2bc 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 70 45  pression list pE
1f2bd 4c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 6d 6f  List contains mo
1f2be 72 65 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 65  re than iLimit e
1f2bf 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20 6c 65 61 76  lements,.** leav
1f2c0 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
1f2c1 67 65 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f  ge in pParse..*/
1f2c2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f2c3 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1f2c4 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28  ListCheckLength(
1f2c5 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1f2c6 2c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ,.  ExprList *pE
1f2c7 4c 69 73 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68  List,.  const ch
1f2c8 61 72 20 2a 7a 4f 62 6a 65 63 74 0a 29 7b 0a 20  ar *zObject.){. 
1f2c9 20 69 6e 74 20 6d 78 20 3d 20 70 50 61 72 73 65   int mx = pParse
1f2ca 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ->db->aLimit[SQL
1f2cb 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
1f2cc 5d 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  ];.  testcase( p
1f2cd 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d  EList && pEList-
1f2ce 3e 6e 45 78 70 72 3d 3d 6d 78 20 29 3b 0a 20 20  >nExpr==mx );.  
1f2cf 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74  testcase( pEList
1f2d0 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
1f2d1 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20 20 69 66 28  r==mx+1 );.  if(
1f2d2 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73   pEList && pELis
1f2d3 74 2d 3e 6e 45 78 70 72 3e 6d 78 20 29 7b 0a 20  t->nExpr>mx ){. 
1f2d4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f2d5 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
1f2d6 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  many columns in 
1f2d7 25 73 22 2c 20 7a 4f 62 6a 65 63 74 29 3b 0a 20  %s", zObject);. 
1f2d8 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65   }.}../*.** Dele
1f2d9 74 65 20 61 6e 20 65 6e 74 69 72 65 20 65 78 70  te an entire exp
1f2da 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f  ression list..*/
1f2db 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f2dc 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1f2dd 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
1f2de 65 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74  e3 *db, ExprList
1f2df 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20   *pList){.  int 
1f2e0 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  i;.  struct Expr
1f2e1 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1f2e2 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
1f2e3 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
1f2e4 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30  ert( pList->a!=0
1f2e5 20 7c 7c 20 28 70 4c 69 73 74 2d 3e 6e 45 78 70   || (pList->nExp
1f2e6 72 3d 3d 30 20 26 26 20 70 4c 69 73 74 2d 3e 6e  r==0 && pList->n
1f2e7 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b 0a 20 20 61  Alloc==0) );.  a
1f2e8 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45  ssert( pList->nE
1f2e9 78 70 72 3c 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c  xpr<=pList->nAll
1f2ea 6f 63 20 29 3b 0a 20 20 66 6f 72 28 70 49 74 65  oc );.  for(pIte
1f2eb 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b  m=pList->a, i=0;
1f2ec 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
1f2ed 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
1f2ee 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
1f2ef 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d  elete(db, pItem-
1f2f0 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 73 71 6c  >pExpr);.    sql
1f2f1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1f2f2 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Item->zName);.  
1f2f3 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
1f2f4 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b  e(db, pList->a);
1f2f5 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1f2f6 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a  (db, pList);.}..
1f2f7 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  /*.** These rout
1f2f8 69 6e 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20  ines are Walker 
1f2f9 63 61 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b  callbacks.  Walk
1f2fa 65 72 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69  er.u.pi is a poi
1f2fb 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e  nter.** to an in
1f2fc 74 65 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f  teger.  These ro
1f2fd 75 74 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b  utines are check
1f2fe 69 6e 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ing an expressio
1f2ff 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66 20 69  n to see.** if i
1f300 74 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e  t is a constant.
1f301 20 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e    Set *Walker.u.
1f302 70 69 20 74 6f 20 30 20 69 66 20 74 68 65 20 65  pi to 0 if the e
1f303 78 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20  xpression is.** 
1f304 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a  not constant..**
1f305 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 62 61  .** These callba
1f306 63 6b 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  ck routines are 
1f307 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
1f308 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  t the following:
1f309 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  .**.**     sqlit
1f30a 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
1f30b 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
1f30c 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
1f30d 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 20 20  otJoin().**     
1f30e 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
1f30f 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28  stantOrFunction(
1f310 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ).**.*/.static i
1f311 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e  nt exprNodeIsCon
1f312 73 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57  stant(Walker *pW
1f313 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
1f314 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 70 57  pr){..  /* If pW
1f315 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 20 33 20  alker->u.i is 3 
1f316 74 68 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66  then any term of
1f317 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1f318 74 68 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a  that comes from.
1f319 20 20 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55    ** the ON or U
1f31a 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20  SING clauses of 
1f31b 61 20 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66  a join disqualif
1f31c 69 65 73 20 74 68 65 20 65 78 70 72 65 73 73 69  ies the expressi
1f31d 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69  on.  ** from bei
1f31e 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f  ng considered co
1f31f 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28  nstant. */.  if(
1f320 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33   pWalker->u.i==3
1f321 20 26 26 20 45 78 70 72 48 61 73 41 6e 79 50 72   && ExprHasAnyPr
1f322 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1f323 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20  _FromJoin) ){.  
1f324 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d    pWalker->u.i =
1f325 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   0;.    return W
1f326 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20  RC_Abort;.  }.. 
1f327 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
1f328 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e  op ){.    /* Con
1f329 73 69 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  sider functions 
1f32a 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 69  to be constant i
1f32b 66 20 61 6c 6c 20 74 68 65 69 72 20 61 72 67 75  f all their argu
1f32c 6d 65 6e 74 73 20 61 72 65 20 63 6f 6e 73 74 61  ments are consta
1f32d 6e 74 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 57  nt.    ** and pW
1f32e 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f  alker->u.i==2 */
1f32f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e  .    case TK_FUN
1f330 43 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28  CTION:.      if(
1f331 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32   pWalker->u.i==2
1f332 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
1f333 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75     /* Fall throu
1f334 67 68 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54  gh */.    case T
1f335 4b 5f 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54  K_ID:.    case T
1f336 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61  K_COLUMN:.    ca
1f337 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  se TK_AGG_FUNCTI
1f338 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ON:.    case TK_
1f339 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 23 69 66 6e  AGG_COLUMN:.#ifn
1f33a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f33b 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
1f33c 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20  e TK_SELECT:.   
1f33d 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a   case TK_EXISTS:
1f33e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1f33f 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53   pExpr->op==TK_S
1f340 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 74  ELECT );.      t
1f341 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
1f342 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b  op==TK_EXISTS );
1f343 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 74 65  .#endif.      te
1f344 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
1f345 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20 20 20 20  p==TK_ID );.    
1f346 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
1f347 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op==TK_COLUMN
1f348 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1f349 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
1f34a 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29  K_AGG_FUNCTION )
1f34b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1f34c 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
1f34d 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  AGG_COLUMN );.  
1f34e 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69      pWalker->u.i
1f34f 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   = 0;.      retu
1f350 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1f351 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
1f352 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
1f353 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  inue;.  }.}.stat
1f354 69 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64  ic int selectNod
1f355 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b  eIsConstant(Walk
1f356 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c  er *pWalker, Sel
1f357 65 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  ect *NotUsed){. 
1f358 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1f359 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 70 57  R(NotUsed);.  pW
1f35a 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a  alker->u.i = 0;.
1f35b 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
1f35c 72 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  rt;.}.static int
1f35d 20 65 78 70 72 49 73 43 6f 6e 73 74 28 45 78 70   exprIsConst(Exp
1f35e 72 20 2a 70 2c 20 69 6e 74 20 69 6e 69 74 46 6c  r *p, int initFl
1f35f 61 67 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b  ag){.  Walker w;
1f360 0a 20 20 77 2e 75 2e 69 20 3d 20 69 6e 69 74 46  .  w.u.i = initF
1f361 6c 61 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  lag;.  w.xExprCa
1f362 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 4e 6f 64  llback = exprNod
1f363 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 77  eIsConstant;.  w
1f364 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b  .xSelectCallback
1f365 20 3d 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43   = selectNodeIsC
1f366 6f 6e 73 74 61 6e 74 3b 0a 20 20 73 71 6c 69 74  onstant;.  sqlit
1f367 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70  e3WalkExpr(&w, p
1f368 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 2e 75 2e  );.  return w.u.
1f369 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b  i;.}../*.** Walk
1f36a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
1f36b 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69  ree.  Return 1 i
1f36c 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1f36d 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20   is constant.** 
1f36e 61 6e 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f  and 0 if it invo
1f36f 6c 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f  lves variables o
1f370 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  r function calls
1f371 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ..**.** For the 
1f372 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73  purposes of this
1f373 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75   function, a dou
1f374 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e  ble-quoted strin
1f375 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a  g (ex: "abc").**
1f376 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61   is considered a
1f377 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20   variable but a 
1f378 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74  single-quoted st
1f379 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29  ring (ex: 'abc')
1f37a 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e   is.** a constan
1f37b 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1f37c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1f37d 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45  ExprIsConstant(E
1f37e 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  xpr *p){.  retur
1f37f 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c  n exprIsConst(p,
1f380 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61   1);.}../*.** Wa
1f381 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  lk an expression
1f382 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31   tree.  Return 1
1f383 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
1f384 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a  on is constant.*
1f385 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f  * that does no o
1f386 72 69 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68  riginate from th
1f387 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  e ON or USING cl
1f388 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e  auses of a join.
1f389 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20  .** Return 0 if 
1f38a 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69  it involves vari
1f38b 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f  ables or functio
1f38c 6e 20 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73  n calls or terms
1f38d 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f   from.** an ON o
1f38e 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a  r USING clause..
1f38f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f390 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
1f391 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f  rIsConstantNotJo
1f392 69 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72  in(Expr *p){.  r
1f393 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73  eturn exprIsCons
1f394 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  t(p, 3);.}../*.*
1f395 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73  * Walk an expres
1f396 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75  sion tree.  Retu
1f397 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72  rn 1 if the expr
1f398 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61  ession is consta
1f399 6e 74 0a 2a 2a 20 6f 72 20 61 20 66 75 6e 63 74  nt.** or a funct
1f39a 69 6f 6e 20 63 61 6c 6c 20 77 69 74 68 20 63 6f  ion call with co
1f39b 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 73  nstant arguments
1f39c 2e 20 20 52 65 74 75 72 6e 20 61 6e 64 20 30 20  .  Return and 0 
1f39d 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20  if there.** are 
1f39e 61 6e 79 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a  any variables..*
1f39f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72  *.** For the pur
1f3a0 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 75  poses of this fu
1f3a1 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65  nction, a double
1f3a2 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28  -quoted string (
1f3a3 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73  ex: "abc").** is
1f3a4 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61   considered a va
1f3a5 72 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e  riable but a sin
1f3a6 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e  gle-quoted strin
1f3a7 67 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 73  g (ex: 'abc') is
1f3a8 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a  .** a constant..
1f3a9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f3aa 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
1f3ab 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e  rIsConstantOrFun
1f3ac 63 74 69 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0a  ction(Expr *p){.
1f3ad 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43    return exprIsC
1f3ae 6f 6e 73 74 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f  onst(p, 2);.}../
1f3af 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  *.** If the expr
1f3b0 65 73 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61  ession p codes a
1f3b1 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65   constant intege
1f3b2 72 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20  r that is small 
1f3b3 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74  enough.** to fit
1f3b4 20 69 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74   in a 32-bit int
1f3b5 65 67 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61  eger, return 1 a
1f3b6 6e 64 20 70 75 74 20 74 68 65 20 76 61 6c 75 65  nd put the value
1f3b7 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a   of the integer.
1f3b8 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20  ** in *pValue.  
1f3b9 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
1f3ba 6e 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  n is not an inte
1f3bb 67 65 72 20 6f 72 20 69 66 20 69 74 20 69 73 20  ger or if it is 
1f3bc 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69  too big.** to fi
1f3bd 74 20 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32  t in a signed 32
1f3be 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65  -bit integer, re
1f3bf 74 75 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65  turn 0 and leave
1f3c0 20 2a 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67   *pValue unchang
1f3c1 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1f3c2 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1f3c3 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45  3ExprIsInteger(E
1f3c4 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61  xpr *p, int *pVa
1f3c5 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  lue){.  int rc =
1f3c6 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61   0;.  if( p->fla
1f3c7 67 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65  gs & EP_IntValue
1f3c8 20 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20   ){.    *pValue 
1f3c9 3d 20 70 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20  = p->iTable;.   
1f3ca 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
1f3cb 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
1f3cc 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  {.    case TK_IN
1f3cd 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 72  TEGER: {.      r
1f3ce 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 49 6e  c = sqlite3GetIn
1f3cf 74 33 32 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f  t32((char*)p->to
1f3d0 6b 65 6e 2e 7a 2c 20 70 56 61 6c 75 65 29 3b 0a  ken.z, pValue);.
1f3d1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1f3d2 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55   }.    case TK_U
1f3d3 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 72 63  PLUS: {.      rc
1f3d4 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 49 73   = sqlite3ExprIs
1f3d5 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74  Integer(p->pLeft
1f3d6 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20  , pValue);.     
1f3d7 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1f3d8 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53    case TK_UMINUS
1f3d9 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 3b  : {.      int v;
1f3da 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1f3db 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
1f3dc 70 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 29 7b  p->pLeft, &v) ){
1f3dd 0a 20 20 20 20 20 20 20 20 2a 70 56 61 6c 75 65  .        *pValue
1f3de 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 72   = -v;.        r
1f3df 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 1;.      }. 
1f3e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1f3e1 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 62  }.    default: b
1f3e2 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  reak;.  }.  if( 
1f3e3 72 63 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 70 20  rc ){.    p->op 
1f3e4 3d 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20  = TK_INTEGER;.  
1f3e5 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50    p->flags |= EP
1f3e6 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 70  _IntValue;.    p
1f3e7 2d 3e 69 54 61 62 6c 65 20 3d 20 2a 70 56 61 6c  ->iTable = *pVal
1f3e8 75 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ue;.  }.  return
1f3e9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
1f3ea 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
1f3eb 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73   given string is
1f3ec 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e   a row-id column
1f3ed 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   name..*/.SQLITE
1f3ee 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1f3ef 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73  ite3IsRowid(cons
1f3f0 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66  t char *z){.  if
1f3f1 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1f3f2 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d  (z, "_ROWID_")==
1f3f3 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
1f3f4 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1f3f5 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d  mp(z, "ROWID")==
1f3f6 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
1f3f7 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1f3f8 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20  mp(z, "OID")==0 
1f3f9 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65  ) return 1;.  re
1f3fa 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
1f3fb 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
1f3fc 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20  the IN operator 
1f3fd 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20  optimization is 
1f3fe 65 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20 74  enabled and.** t
1f3ff 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
1f400 65 6e 74 20 70 20 65 78 69 73 74 73 20 61 6e 64  ent p exists and
1f401 20 69 73 20 6f 66 20 74 68 65 0a 2a 2a 20 73 69   is of the.** si
1f402 6d 70 6c 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a  mple form:.**.**
1f403 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c       SELECT <col
1f404 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65  umn> FROM <table
1f405 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20  >.**.** If this 
1f406 69 73 20 74 68 65 20 63 61 73 65 2c 20 69 74 20  is the case, it 
1f407 6d 61 79 20 62 65 20 70 6f 73 73 69 62 6c 65 20  may be possible 
1f408 74 6f 20 75 73 65 20 61 6e 20 65 78 69 73 74 69  to use an existi
1f409 6e 67 20 74 61 62 6c 65 0a 2a 2a 20 6f 72 20 69  ng table.** or i
1f40a 6e 64 65 78 20 69 6e 73 74 65 61 64 20 6f 66 20  ndex instead of 
1f40b 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 65 70  generating an ep
1f40c 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e 0a 2a  heremal table..*
1f40d 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1f40e 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 73  _OMIT_SUBQUERY.s
1f40f 74 61 74 69 63 20 69 6e 74 20 69 73 43 61 6e 64  tatic int isCand
1f410 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 53 65  idateForInOpt(Se
1f411 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c  lect *p){.  SrcL
1f412 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 45 78 70  ist *pSrc;.  Exp
1f413 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20  rList *pEList;. 
1f414 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
1f415 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1f416 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  n 0;            
1f417 20 20 20 20 20 20 20 2f 2a 20 72 69 67 68 74 2d         /* right-
1f418 68 61 6e 64 20 73 69 64 65 20 6f 66 20 49 4e 20  hand side of IN 
1f419 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69  is SELECT */.  i
1f41a 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 20 72  f( p->pPrior ) r
1f41b 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
1f41c 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63        /* Not a c
1f41d 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a  ompound SELECT *
1f41e 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c  /.  if( p->selFl
1f41f 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e  ags & (SF_Distin
1f420 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29  ct|SF_Aggregate)
1f421 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1f422 20 30 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e   0; /* No DISTIN
1f423 43 54 20 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e  CT keyword and n
1f424 6f 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  o aggregate func
1f425 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 69  tions */.  }.  i
1f426 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 29  f( p->pGroupBy )
1f427 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
1f428 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20        /* Has no 
1f429 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
1f42a 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d  */.  if( p->pLim
1f42b 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  it ) return 0;  
1f42c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
1f42d 61 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75  as no LIMIT clau
1f42e 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  se */.  if( p->p
1f42f 4f 66 66 73 65 74 20 29 20 72 65 74 75 72 6e 20  Offset ) return 
1f430 30 3b 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65  0;.  if( p->pWhe
1f431 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  re ) return 0;  
1f432 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
1f433 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75  as no WHERE clau
1f434 73 65 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70  se */.  pSrc = p
1f435 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74  ->pSrc;.  assert
1f436 28 20 70 53 72 63 21 3d 30 20 29 3b 0a 20 20 69  ( pSrc!=0 );.  i
1f437 66 28 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31  f( pSrc->nSrc!=1
1f438 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
1f439 20 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 20        /* Single 
1f43a 74 65 72 6d 20 69 6e 20 46 52 4f 4d 20 63 6c 61  term in FROM cla
1f43b 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 72  use */.  if( pSr
1f43c 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20  c->a[0].pSelect 
1f43d 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
1f43e 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69  /* FROM clause i
1f43f 73 20 6e 6f 74 20 61 20 73 75 62 71 75 65 72 79  s not a subquery
1f440 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 70 53 72   */.  pTab = pSr
1f441 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20  c->a[0].pTab;.  
1f442 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65  if( pTab==0 ) re
1f443 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 54  turn 0;.  if( pT
1f444 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 72 65  ab->pSelect ) re
1f445 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
1f446 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   /* FROM clause 
1f447 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a 2f  is not a view */
1f448 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
1f449 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20  (pTab) ) return 
1f44a 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f  0;        /* FRO
1f44b 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 20 76  M clause not a v
1f44c 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
1f44d 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45    pEList = p->pE
1f44e 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c 69  List;.  if( pELi
1f44f 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72  st->nExpr!=1 ) r
1f450 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 2f  eturn 0;       /
1f451 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e 20  * One column in 
1f452 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
1f453 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e  /.  if( pEList->
1f454 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d  a[0].pExpr->op!=
1f455 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75  TK_COLUMN ) retu
1f456 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c 74 20  rn 0; /* Result 
1f457 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  is a column */. 
1f458 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e   return 1;.}.#en
1f459 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1f45a 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a  IT_SUBQUERY */..
1f45b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1f45c 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74  ion is used by t
1f45d 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
1f45e 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e 2e 2e  n of the IN (...
1f45f 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 49  ) operator..** I
1f460 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 66 69  t's job is to fi
1f461 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 20 62  nd or create a b
1f462 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  -tree structure 
1f463 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64  that may be used
1f464 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 74 65  .** either to te
1f465 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69  st for membershi
1f466 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 20 73  p of the (...) s
1f467 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 74 65  et or to iterate
1f468 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 73 20   through.** its 
1f469 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 6e  members, skippin
1f46a 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a  g duplicates..**
1f46b 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 6f  .** The cursor o
1f46c 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 73 74 72  pened on the str
1f46d 75 63 74 75 72 65 20 28 64 61 74 61 62 61 73 65  ucture (database
1f46e 20 74 61 62 6c 65 2c 20 64 61 74 61 62 61 73 65   table, database
1f46f 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 72 20 65 70   index .** or ep
1f470 68 65 72 6d 61 6c 20 74 61 62 6c 65 29 20 69 73  hermal table) is
1f471 20 73 74 6f 72 65 64 20 69 6e 20 70 58 2d 3e 69   stored in pX->i
1f472 54 61 62 6c 65 20 62 65 66 6f 72 65 20 74 68 69  Table before thi
1f473 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
1f474 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ns..** The retur
1f475 6e 65 64 20 76 61 6c 75 65 20 69 6e 64 69 63 61  ned value indica
1f476 74 65 73 20 74 68 65 20 73 74 72 75 63 74 75 72  tes the structur
1f477 65 20 74 79 70 65 2c 20 61 73 20 66 6f 6c 6c 6f  e type, as follo
1f478 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 5f 49  ws:.**.**   IN_I
1f479 4e 44 45 58 5f 52 4f 57 49 44 20 2d 20 54 68 65  NDEX_ROWID - The
1f47a 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e   cursor was open
1f47b 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  ed on a database
1f47c 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 49 4e 5f   table..**   IN_
1f47d 49 4e 44 45 58 5f 49 4e 44 45 58 20 2d 20 54 68  INDEX_INDEX - Th
1f47e 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65  e cursor was ope
1f47f 6e 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73  ned on a databas
1f480 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 20 20 49 4e  e index..**   IN
1f481 5f 49 4e 44 45 58 5f 45 50 48 20 2d 20 20 20 54  _INDEX_EPH -   T
1f482 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70  he cursor was op
1f483 65 6e 65 64 20 6f 6e 20 61 20 73 70 65 63 69 61  ened on a specia
1f484 6c 6c 79 20 63 72 65 61 74 65 64 20 61 6e 64 0a  lly created and.
1f485 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1f486 20 20 20 20 20 20 70 6f 70 75 6c 61 74 65 64 20        populated 
1f487 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e  epheremal table.
1f488 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 69 73 74 69  .**.** An existi
1f489 6e 67 20 73 74 72 75 63 74 75 72 65 20 6d 61 79  ng structure may
1f48a 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69 66   only be used if
1f48b 20 74 68 65 20 53 45 4c 45 43 54 20 69 73 20 6f   the SELECT is o
1f48c 66 20 74 68 65 20 73 69 6d 70 6c 65 0a 2a 2a 20  f the simple.** 
1f48d 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  form:.**.**     
1f48e 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20  SELECT <column> 
1f48f 46 52 4f 4d 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a  FROM <table>.**.
1f490 2a 2a 20 49 66 20 70 72 4e 6f 74 46 6f 75 6e 64  ** If prNotFound
1f491 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c   parameter is 0,
1f492 20 74 68 65 6e 20 74 68 65 20 73 74 72 75 63 74   then the struct
1f493 75 72 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64  ure will be used
1f494 20 74 6f 20 69 74 65 72 61 74 65 0a 2a 2a 20 74   to iterate.** t
1f495 68 72 6f 75 67 68 20 74 68 65 20 73 65 74 20 6d  hrough the set m
1f496 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67  embers, skipping
1f497 20 61 6e 79 20 64 75 70 6c 69 63 61 74 65 73 2e   any duplicates.
1f498 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e   In this case an
1f499 0a 2a 2a 20 65 70 68 65 72 65 6d 61 6c 20 74 61  .** epheremal ta
1f49a 62 6c 65 20 6d 75 73 74 20 62 65 20 75 73 65 64  ble must be used
1f49b 20 75 6e 6c 65 73 73 20 74 68 65 20 73 65 6c 65   unless the sele
1f49c 63 74 65 64 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73  cted <column> is
1f49d 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74   guaranteed.** t
1f49e 6f 20 62 65 20 75 6e 69 71 75 65 20 2d 20 65 69  o be unique - ei
1f49f 74 68 65 72 20 62 65 63 61 75 73 65 20 69 74 20  ther because it 
1f4a0 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  is an INTEGER PR
1f4a1 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 69 74 0a  IMARY KEY or it.
1f4a2 2a 2a 20 69 73 20 75 6e 69 71 75 65 20 62 79 20  ** is unique by 
1f4a3 76 69 72 74 75 65 20 6f 66 20 61 20 63 6f 6e 73  virtue of a cons
1f4a4 74 72 61 69 6e 74 20 6f 72 20 69 6d 70 6c 69 63  traint or implic
1f4a5 69 74 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  it index..**.** 
1f4a6 49 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e  If the prNotFoun
1f4a7 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  d parameter is n
1f4a8 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 20 73  ot 0, then the s
1f4a9 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 62 65  tructure will be
1f4aa 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 66 61   used .** for fa
1f4ab 73 74 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69  st set membershi
1f4ac 70 20 74 65 73 74 73 2e 20 49 6e 20 74 68 69 73  p tests. In this
1f4ad 20 63 61 73 65 20 61 6e 20 65 70 68 65 72 65 6d   case an epherem
1f4ae 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 0a 2a  al table must .*
1f4af 2a 20 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73  * be used unless
1f4b0 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 61 6e 20   <column> is an 
1f4b1 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
1f4b2 4b 45 59 20 6f 72 20 61 6e 20 69 6e 64 65 78 20  KEY or an index 
1f4b3 63 61 6e 20 0a 2a 2a 20 62 65 20 66 6f 75 6e 64  can .** be found
1f4b4 20 77 69 74 68 20 3c 63 6f 6c 75 6d 6e 3e 20 61   with <column> a
1f4b5 73 20 69 74 73 20 6c 65 66 74 2d 6d 6f 73 74 20  s its left-most 
1f4b6 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68  column..**.** Wh
1f4b7 65 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65  en the structure
1f4b8 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66   is being used f
1f4b9 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 73 68 69  or set membershi
1f4ba 70 20 74 65 73 74 73 2c 20 74 68 65 20 75 73 65  p tests, the use
1f4bb 72 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 6b 6e  r.** needs to kn
1f4bc 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ow whether or no
1f4bd 74 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  t the structure 
1f4be 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c 20  contains an SQL 
1f4bf 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c 75 65 20 69  NULL .** value i
1f4c0 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 72 72 65  n order to corre
1f4c1 63 74 6c 79 20 65 76 61 6c 75 61 74 65 20 65 78  ctly evaluate ex
1f4c2 70 72 65 73 73 69 6f 6e 73 20 6c 69 6b 65 20 22  pressions like "
1f4c3 58 20 49 4e 20 28 59 2c 20 5a 29 22 2e 0a 2a 2a  X IN (Y, Z)"..**
1f4c4 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 63   If there is a c
1f4c5 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 73  hance that the s
1f4c6 74 72 75 63 74 75 72 65 20 6d 61 79 20 63 6f 6e  tructure may con
1f4c7 74 61 69 6e 20 61 20 4e 55 4c 4c 20 76 61 6c 75  tain a NULL valu
1f4c8 65 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2c  e at.** runtime,
1f4c9 20 74 68 65 6e 20 61 20 72 65 67 69 73 74 65 72   then a register
1f4ca 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
1f4cb 64 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e  d the register n
1f4cc 75 6d 62 65 72 20 77 72 69 74 74 65 6e 0a 2a 2a  umber written.**
1f4cd 20 74 6f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2e   to *prNotFound.
1f4ce 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
1f4cf 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20  chance that the 
1f4d0 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
1f4d1 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c 20 76 61 6c  ns a.** NULL val
1f4d2 75 65 2c 20 74 68 65 6e 20 2a 70 72 4e 6f 74 46  ue, then *prNotF
1f4d3 6f 75 6e 64 20 69 73 20 6c 65 66 74 20 75 6e 63  ound is left unc
1f4d4 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  hanged..**.** If
1f4d5 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61   a register is a
1f4d6 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69 74 73  llocated and its
1f4d7 20 6c 6f 63 61 74 69 6f 6e 20 73 74 6f 72 65 64   location stored
1f4d8 20 69 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2c   in *prNotFound,
1f4d9 20 74 68 65 6e 0a 2a 2a 20 69 74 73 20 69 6e 69   then.** its ini
1f4da 74 69 61 6c 20 76 61 6c 75 65 20 69 73 20 4e 55  tial value is NU
1f4db 4c 4c 2e 20 49 66 20 74 68 65 20 73 74 72 75 63  LL. If the struc
1f4dc 74 75 72 65 20 64 6f 65 73 20 6e 6f 74 20 72 65  ture does not re
1f4dd 6d 61 69 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a  main constant.**
1f4de 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f   for the duratio
1f4df 6e 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 28  n of the query (
1f4e0 69 2e 65 2e 20 74 68 65 20 73 65 74 20 69 73 20  i.e. the set is 
1f4e1 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75 62  a correlated sub
1f4e2 2d 73 65 6c 65 63 74 29 2c 20 0a 2a 2a 20 74 68  -select), .** th
1f4e3 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61  e value of the a
1f4e4 6c 6c 6f 63 61 74 65 64 20 72 65 67 69 73 74 65  llocated registe
1f4e5 72 20 69 73 20 72 65 73 65 74 20 74 6f 20 4e 55  r is reset to NU
1f4e6 4c 4c 20 65 61 63 68 20 74 69 6d 65 20 74 68 65  LL each time the
1f4e7 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69   .** structure i
1f4e8 73 20 72 65 70 6f 70 75 6c 61 74 65 64 2e 20 54  s repopulated. T
1f4e9 68 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 20 63  his allows the c
1f4ea 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76 64 62  aller to use vdb
1f4eb 65 20 63 6f 64 65 20 0a 2a 2a 20 65 71 75 69 76  e code .** equiv
1f4ec 61 6c 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c  alent to the fol
1f4ed 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
1f4ee 69 66 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55  if( register==NU
1f4ef 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73  LL ){.**     has
1f4f0 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66  _null = <test if
1f4f1 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
1f4f2 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a  contains null>.*
1f4f3 2a 20 20 20 20 20 72 65 67 69 73 74 65 72 20 3d  *     register =
1f4f4 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20   1.**   }.**.** 
1f4f5 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69  in order to avoi
1f4f6 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74  d running the <t
1f4f7 65 73 74 20 69 66 20 64 61 74 61 20 73 74 72 75  est if data stru
1f4f8 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e  cture contains n
1f4f9 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72  ull>.** test mor
1f4fa 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 69 73 20  e often than is 
1f4fb 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69  necessary..*/.#i
1f4fc 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f4fd 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54  T_SUBQUERY.SQLIT
1f4fe 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1f4ff 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78  lite3FindInIndex
1f500 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1f501 45 78 70 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70  Expr *pX, int *p
1f502 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65  rNotFound){.  Se
1f503 6c 65 63 74 20 2a 70 3b 0a 20 20 69 6e 74 20 65  lect *p;.  int e
1f504 54 79 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Type = 0;.  int 
1f505 69 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iTab = pParse->n
1f506 54 61 62 2b 2b 3b 0a 20 20 69 6e 74 20 6d 75 73  Tab++;.  int mus
1f507 74 42 65 55 6e 69 71 75 65 20 3d 20 21 70 72 4e  tBeUnique = !prN
1f508 6f 74 46 6f 75 6e 64 3b 0a 0a 20 20 2f 2a 20 54  otFound;..  /* T
1f509 68 65 20 66 6f 6c 6c 77 69 6e 67 20 69 66 28 2e  he follwing if(.
1f50a 2e 2e 29 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ..) expression i
1f50b 73 20 74 72 75 65 20 69 66 20 74 68 65 20 53 45  s true if the SE
1f50c 4c 45 43 54 20 69 73 20 6f 66 20 74 68 65 20 0a  LECT is of the .
1f50d 20 20 2a 2a 20 73 69 6d 70 6c 65 20 66 6f 72 6d    ** simple form
1f50e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
1f50f 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20  SELECT <column> 
1f510 46 52 4f 4d 20 3c 74 61 62 6c 65 3e 0a 20 20 2a  FROM <table>.  *
1f511 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69  *.  ** If this i
1f512 73 20 74 68 65 20 63 61 73 65 2c 20 69 74 20 6d  s the case, it m
1f513 61 79 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74  ay be possible t
1f514 6f 20 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e  o use an existin
1f515 67 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 72 20  g table.  ** or 
1f516 69 6e 64 65 78 20 69 6e 73 74 65 61 64 20 6f 66  index instead of
1f517 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 65   generating an e
1f518 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e 0a  pheremal table..
1f519 20 20 2a 2f 0a 20 20 70 20 3d 20 28 45 78 70 72    */.  p = (Expr
1f51a 48 61 73 50 72 6f 70 65 72 74 79 28 70 58 2c 20  HasProperty(pX, 
1f51b 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 3f 20  EP_xIsSelect) ? 
1f51c 70 58 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3a 20  pX->x.pSelect : 
1f51d 30 29 3b 0a 20 20 69 66 28 20 69 73 43 61 6e 64  0);.  if( isCand
1f51e 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70 29  idateForInOpt(p)
1f51f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   ){.    sqlite3 
1f520 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1f521 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1f522 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
1f523 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70  ction */.    Exp
1f524 72 20 2a 70 45 78 70 72 20 3d 20 70 2d 3e 70 45  r *pExpr = p->pE
1f525 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
1f526 3b 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f  ;   /* Expressio
1f527 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20  n <column> */.  
1f528 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78    int iCol = pEx
1f529 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  pr->iColumn;    
1f52a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
1f52b 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 3c 63 6f 6c  x of column <col
1f52c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20 56 64 62 65  umn> */.    Vdbe
1f52d 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
1f52e 56 64 62 65 28 70 50 61 72 73 65 29 3b 20 20 20  Vdbe(pParse);   
1f52f 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61     /* Virtual ma
1f530 63 68 69 6e 65 20 62 65 69 6e 67 20 63 6f 64 65  chine being code
1f531 64 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a  d */.    Table *
1f532 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e  pTab = p->pSrc->
1f533 61 5b 30 5d 2e 70 54 61 62 3b 20 20 20 20 20 20  a[0].pTab;      
1f534 2f 2a 20 54 61 62 6c 65 20 3c 74 61 62 6c 65 3e  /* Table <table>
1f535 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62  . */.    int iDb
1f536 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1f537 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f538 2f 2a 20 44 61 74 61 62 61 73 65 20 69 64 78 20  /* Database idx 
1f539 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 20 0a  for pTab */.   .
1f53a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f      /* Code an O
1f53b 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 61  P_VerifyCookie a
1f53c 6e 64 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20  nd OP_TableLock 
1f53d 66 6f 72 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a  for <table>. */.
1f53e 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
1f53f 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
1f540 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
1f541 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  );.    sqlite3Co
1f542 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70  deVerifySchema(p
1f543 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20  Parse, iDb);.   
1f544 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
1f545 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  k(pParse, iDb, p
1f546 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54  Tab->tnum, 0, pT
1f547 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20  ab->zName);..   
1f548 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f   /* This functio
1f549 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  n is only called
1f54a 20 66 72 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73   from two places
1f54b 2e 20 49 6e 20 62 6f 74 68 20 63 61 73 65 73 20  . In both cases 
1f54c 74 68 65 20 76 64 62 65 0a 20 20 20 20 2a 2a 20  the vdbe.    ** 
1f54d 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
1f54e 20 61 6c 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61   allocated. So a
1f54f 73 73 75 6d 65 20 73 71 6c 69 74 65 33 47 65 74  ssume sqlite3Get
1f550 56 64 62 65 28 29 20 69 73 20 61 6c 77 61 79 73  Vdbe() is always
1f551 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 73 73 66  .    ** successf
1f552 75 6c 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a  ul here..    */.
1f553 20 20 20 20 61 73 73 65 72 74 28 76 29 3b 0a 20      assert(v);. 
1f554 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
1f555 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20  .      int iMem 
1f556 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
1f557 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 41 64 64  ;.      int iAdd
1f558 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
1f559 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
1f55a 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 69 41   iDb);..      iA
1f55b 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1f55c 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
1f55d 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73  , iMem);.      s
1f55e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1f55f 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1f560 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 20  1, iMem);..     
1f561 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
1f562 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20  e(pParse, iTab, 
1f563 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70  iDb, pTab, OP_Op
1f564 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 65  enRead);.      e
1f565 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f  Type = IN_INDEX_
1f566 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 71  ROWID;..      sq
1f567 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1f568 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20  e(v, iAddr);.   
1f569 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e   }else{.      In
1f56a 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
1f56b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f56c 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
1f56d 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20  ariable */..    
1f56e 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69    /* The collati
1f56f 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65 64  on sequence used
1f570 20 62 79 20 74 68 65 20 63 6f 6d 70 61 72 69 73   by the comparis
1f571 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 65 78 20  on. If an index 
1f572 69 73 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20  is to .      ** 
1f573 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  be used in place
1f574 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 6c 65   of a temp-table
1f575 2c 20 69 74 20 6d 75 73 74 20 62 65 20 6f 72 64  , it must be ord
1f576 65 72 65 64 20 61 63 63 6f 72 64 69 6e 67 0a 20  ered according. 
1f577 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 73 20       ** to this 
1f578 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
1f579 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f  ce.  */.      Co
1f57a 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71  llSeq *pReq = sq
1f57b 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61  lite3BinaryCompa
1f57c 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  reCollSeq(pParse
1f57d 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78  , pX->pLeft, pEx
1f57e 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43  pr);..      /* C
1f57f 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66  heck that the af
1f580 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c  finity that will
1f581 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66   be used to perf
1f582 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 2a  orm the .      *
1f583 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  * comparison is 
1f584 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
1f585 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20  affinity of the 
1f586 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20  column. If.     
1f587 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 69   ** it is not, i
1f588 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
1f589 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e 64  e to use any ind
1f58a 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ex..      */.   
1f58b 20 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 6f     char aff = co
1f58c 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79  mparisonAffinity
1f58d 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  (pX);.      int 
1f58e 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20 28 70  affinity_ok = (p
1f58f 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
1f590 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c 7c 61  affinity==aff||a
1f591 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
1f592 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72  ONE);..      for
1f593 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
1f594 65 78 3b 20 70 49 64 78 20 26 26 20 65 54 79 70  ex; pIdx && eTyp
1f595 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69 74 79  e==0 && affinity
1f596 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  _ok; pIdx=pIdx->
1f597 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
1f598 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43 6f 6c  if( (pIdx->aiCol
1f599 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20  umn[0]==iCol).  
1f59a 20 20 20 20 20 20 20 26 26 20 28 70 52 65 71 3d         && (pReq=
1f59b 3d 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c  =sqlite3FindColl
1f59c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c  Seq(db, ENC(db),
1f59d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d   pIdx->azColl[0]
1f59e 2c 20 2d 31 2c 20 30 29 29 0a 20 20 20 20 20 20  , -1, 0)).      
1f59f 20 20 20 26 26 20 28 21 6d 75 73 74 42 65 55 6e     && (!mustBeUn
1f5a0 69 71 75 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e  ique || (pIdx->n
1f5a1 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64  Column==1 && pId
1f5a2 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
1f5a3 6f 6e 65 29 29 0a 20 20 20 20 20 20 20 20 29 7b  one)).        ){
1f5a4 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
1f5a5 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Mem = ++pParse->
1f5a6 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  nMem;.          
1f5a7 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20  int iAddr;.     
1f5a8 20 20 20 20 20 63 68 61 72 20 2a 70 4b 65 79 3b       char *pKey;
1f5a9 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 70 4b  .  .          pK
1f5aa 65 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ey = (char *)sql
1f5ab 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
1f5ac 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a  (pParse, pIdx);.
1f5ad 20 20 20 20 20 20 20 20 20 20 69 44 62 20 3d 20            iDb = 
1f5ae 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1f5af 6e 64 65 78 28 64 62 2c 20 70 49 64 78 2d 3e 70  ndex(db, pIdx->p
1f5b0 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20  Schema);.       
1f5b1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
1f5b2 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
1f5b3 0a 0a 20 20 20 20 20 20 20 20 20 20 69 41 64 64  ..          iAdd
1f5b4 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
1f5b5 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp1(v, OP_If, 
1f5b6 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 20  iMem);.         
1f5b7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f5b8 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
1f5b9 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 20 20 0a 20  , 1, iMem);.  . 
1f5ba 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1f5bb 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1f5bc 5f 4f 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c  _OpenRead, iTab,
1f5bd 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62   pIdx->tnum, iDb
1f5be 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1f5bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f5c0 20 70 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f   pKey,P4_KEYINFO
1f5c1 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20  _HANDOFF);.     
1f5c2 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
1f5c3 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d  ((v, "%s", pIdx-
1f5c4 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20  >zName));.      
1f5c5 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49      eType = IN_I
1f5c6 4e 44 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20  NDEX_INDEX;..   
1f5c7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1f5c8 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41  beJumpHere(v, iA
1f5c9 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ddr);.          
1f5ca 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26  if( prNotFound &
1f5cb 26 20 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43  & !pTab->aCol[iC
1f5cc 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20  ol].notNull ){. 
1f5cd 20 20 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f             *prNo
1f5ce 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73  tFound = ++pPars
1f5cf 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20  e->nMem;.       
1f5d0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1f5d1 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1f5d2 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30  ..  if( eType==0
1f5d3 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 4d 61 79   ){.    int rMay
1f5d4 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20  HaveNull = 0;.  
1f5d5 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44    eType = IN_IND
1f5d6 45 58 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20  EX_EPH;.    if( 
1f5d7 70 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20  prNotFound ){.  
1f5d8 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20      *prNotFound 
1f5d9 3d 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d  = rMayHaveNull =
1f5da 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
1f5db 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
1f5dc 58 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d  X->pLeft->iColum
1f5dd 6e 3c 30 20 26 26 20 21 45 78 70 72 48 61 73 41  n<0 && !ExprHasA
1f5de 6e 79 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45  nyProperty(pX, E
1f5df 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a  P_xIsSelect) ){.
1f5e0 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e        eType = IN
1f5e1 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20  _INDEX_ROWID;.  
1f5e2 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43    }.    sqlite3C
1f5e3 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 61  odeSubselect(pPa
1f5e4 72 73 65 2c 20 70 58 2c 20 72 4d 61 79 48 61 76  rse, pX, rMayHav
1f5e5 65 4e 75 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e  eNull, eType==IN
1f5e6 5f 49 4e 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20  _INDEX_ROWID);. 
1f5e7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e   }else{.    pX->
1f5e8 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20  iTable = iTab;. 
1f5e9 20 7d 0a 20 20 72 65 74 75 72 6e 20 65 54 79 70   }.  return eTyp
1f5ea 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e;.}.#endif../*.
1f5eb 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1f5ec 20 66 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71   for scalar subq
1f5ed 75 65 72 69 65 73 20 75 73 65 64 20 61 73 20 61  ueries used as a
1f5ee 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  n expression.** 
1f5ef 61 6e 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73  and IN operators
1f5f0 2e 20 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a  .  Examples:.**.
1f5f1 2a 2a 20 20 20 20 20 28 53 45 4c 45 43 54 20 61  **     (SELECT a
1f5f2 20 46 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20   FROM b)        
1f5f3 20 20 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a    -- subquery.**
1f5f4 20 20 20 20 20 45 58 49 53 54 53 20 28 53 45 4c       EXISTS (SEL
1f5f5 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20  ECT a FROM b)   
1f5f6 2d 2d 20 45 58 49 53 54 53 20 73 75 62 71 75 65  -- EXISTS subque
1f5f7 72 79 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28  ry.**     x IN (
1f5f8 34 2c 35 2c 31 31 29 20 20 20 20 20 20 20 20 20  4,5,11)         
1f5f9 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61       -- IN opera
1f5fa 74 6f 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e  tor with list on
1f5fb 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
1f5fc 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45  .**     x IN (SE
1f5fd 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20  LECT a FROM b)  
1f5fe 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f     -- IN operato
1f5ff 72 20 77 69 74 68 20 73 75 62 71 75 65 72 79 20  r with subquery 
1f600 6f 6e 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a  on the right.**.
1f601 2a 2a 20 54 68 65 20 70 45 78 70 72 20 70 61 72  ** The pExpr par
1f602 61 6d 65 74 65 72 20 64 65 73 63 72 69 62 65 73  ameter describes
1f603 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1f604 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
1f605 65 20 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72  e IN.** operator
1f606 20 6f 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a   or subquery..**
1f607 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72  .** If parameter
1f608 20 69 73 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d   isRowid is non-
1f609 7a 65 72 6f 2c 20 74 68 65 6e 20 65 78 70 72 65  zero, then expre
1f60a 73 73 69 6f 6e 20 70 45 78 70 72 20 69 73 20 67  ssion pExpr is g
1f60b 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20  uaranteed.** to 
1f60c 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  be of the form "
1f60d 3c 72 6f 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f  <rowid> IN (?, ?
1f60e 2c 20 3f 29 22 2c 20 77 68 65 72 65 20 3c 72 6f  , ?)", where <ro
1f60f 77 69 64 3e 20 69 73 20 61 20 72 65 66 65 72 65  wid> is a refere
1f610 6e 63 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69  nce.** to some i
1f611 6e 74 65 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d  nteger key colum
1f612 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 42 2d 54  n of a table B-T
1f613 72 65 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ree. In this cas
1f614 65 2c 20 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74  e, use an.** int
1f615 6b 65 79 20 42 2d 54 72 65 65 20 74 6f 20 73 74  key B-Tree to st
1f616 6f 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 49  ore the set of I
1f617 4e 28 2e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e  N(...) values in
1f618 73 74 65 61 64 20 6f 66 20 74 68 65 20 75 73 75  stead of the usu
1f619 61 6c 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76  al.** (slower) v
1f61a 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b  ariable length k
1f61b 65 79 73 20 42 2d 54 72 65 65 2e 0a 2a 2f 0a 23  eys B-Tree..*/.#
1f61c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f61d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49  IT_SUBQUERY.SQLI
1f61e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1f61f 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65  sqlite3CodeSubse
1f620 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70  lect(.  Parse *p
1f621 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 2a  Parse, .  Expr *
1f622 70 45 78 70 72 2c 20 0a 20 20 69 6e 74 20 72 4d  pExpr, .  int rM
1f623 61 79 48 61 76 65 4e 75 6c 6c 2c 0a 20 20 69 6e  ayHaveNull,.  in
1f624 74 20 69 73 52 6f 77 69 64 0a 29 7b 0a 20 20 69  t isRowid.){.  i
1f625 6e 74 20 74 65 73 74 41 64 64 72 20 3d 20 30 3b  nt testAddr = 0;
1f626 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f627 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 2d 74 69         /* One-ti
1f628 6d 65 20 74 65 73 74 20 61 64 64 72 65 73 73 20  me test address 
1f629 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  */.  Vdbe *v = s
1f62a 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1f62b 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
1f62c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  0 ) return;.  sq
1f62d 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75  lite3ExprCachePu
1f62e 73 68 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 2f  sh(pParse);..  /
1f62f 2a 20 54 68 69 73 20 63 6f 64 65 20 6d 75 73 74  * This code must
1f630 20 62 65 20 72 75 6e 20 69 6e 20 69 74 73 20 65   be run in its e
1f631 6e 74 69 72 65 74 79 20 65 76 65 72 79 20 74 69  ntirety every ti
1f632 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74  me it is encount
1f633 65 72 65 64 0a 20 20 2a 2a 20 69 66 20 61 6e 79  ered.  ** if any
1f634 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
1f635 67 20 69 73 20 74 72 75 65 3a 0a 20 20 2a 2a 0a  g is true:.  **.
1f636 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72    **    *  The r
1f637 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69  ight-hand side i
1f638 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73  s a correlated s
1f639 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 20 20 20  ubquery.  **    
1f63a 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e  *  The right-han
1f63b 64 20 73 69 64 65 20 69 73 20 61 6e 20 65 78 70  d side is an exp
1f63c 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e  ression list con
1f63d 74 61 69 6e 69 6e 67 20 76 61 72 69 61 62 6c 65  taining variable
1f63e 73 0a 20 20 2a 2a 20 20 20 20 2a 20 20 57 65 20  s.  **    *  We 
1f63f 61 72 65 20 69 6e 73 69 64 65 20 61 20 74 72 69  are inside a tri
1f640 67 67 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  gger.  **.  ** I
1f641 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f  f all of the abo
1f642 76 65 20 61 72 65 20 66 61 6c 73 65 2c 20 74 68  ve are false, th
1f643 65 6e 20 77 65 20 63 61 6e 20 72 75 6e 20 74 68  en we can run th
1f644 69 73 20 63 6f 64 65 20 6a 75 73 74 20 6f 6e 63  is code just onc
1f645 65 0a 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20  e.  ** save the 
1f646 72 65 73 75 6c 74 73 2c 20 61 6e 64 20 72 65 75  results, and reu
1f647 73 65 20 74 68 65 20 73 61 6d 65 20 72 65 73 75  se the same resu
1f648 6c 74 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74  lt on subsequent
1f649 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 20 20   invocations..  
1f64a 2a 2f 0a 20 20 69 66 28 20 21 45 78 70 72 48 61  */.  if( !ExprHa
1f64b 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78  sAnyProperty(pEx
1f64c 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74  pr, EP_VarSelect
1f64d 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72  ) && !pParse->tr
1f64e 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 69  igStack ){.    i
1f64f 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  nt mem = ++pPars
1f650 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
1f651 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
1f652 2c 20 4f 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20  , OP_If, mem);. 
1f653 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 73 71     testAddr = sq
1f654 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1f655 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
1f656 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 61 73 73 65  , mem);.    asse
1f657 72 74 28 20 74 65 73 74 41 64 64 72 3e 30 20 7c  rt( testAddr>0 |
1f658 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
1f659 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
1f65a 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  }..  switch( pEx
1f65b 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61  pr->op ){.    ca
1f65c 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20  se TK_IN: {.    
1f65d 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b    char affinity;
1f65e 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b  .      KeyInfo k
1f65f 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e  eyInfo;.      in
1f660 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f  t addr;        /
1f661 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f  * Address of OP_
1f662 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e  OpenEphemeral in
1f663 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20  struction */.   
1f664 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
1f665 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a   pExpr->pLeft;..
1f666 20 20 20 20 20 20 69 66 28 20 72 4d 61 79 48 61        if( rMayHa
1f667 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20  veNull ){.      
1f668 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f669 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
1f66a 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29  0, rMayHaveNull)
1f66b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
1f66c 20 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69   affinity = sqli
1f66d 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
1f66e 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f  pLeft);..      /
1f66f 2a 20 57 68 65 74 68 65 72 20 74 68 69 73 20 69  * Whether this i
1f670 73 20 61 6e 20 27 78 20 49 4e 28 53 45 4c 45 43  s an 'x IN(SELEC
1f671 54 2e 2e 2e 29 27 20 6f 72 20 61 6e 20 27 78 20  T...)' or an 'x 
1f672 49 4e 28 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a  IN(<exprlist>)'.
1f673 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73        ** express
1f674 69 6f 6e 20 69 74 20 69 73 20 68 61 6e 64 6c 65  ion it is handle
1f675 64 20 74 68 65 20 73 61 6d 65 20 77 61 79 2e 20  d the same way. 
1f676 41 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  A virtual table 
1f677 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c  is .      ** fil
1f678 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65 2d  led with single-
1f679 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79 73  field index keys
1f67a 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
1f67b 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20  e results.      
1f67c 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45  ** from the SELE
1f67d 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72 6c  CT or the <exprl
1f67e 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  ist>..      **. 
1f67f 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 27       ** If the '
1f680 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  x' expression is
1f681 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c   a column value,
1f682 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e   or the SELECT..
1f683 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65  ..      ** state
1f684 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20 63  ment returns a c
1f685 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65  olumn value, the
1f686 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f  n the affinity o
1f687 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20  f that.      ** 
1f688 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 74  column is used t
1f689 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64 65  o build the inde
1f68a 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68 20  x keys. If both 
1f68b 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20 20  'x' and the.    
1f68c 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73    ** SELECT... s
1f68d 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c  tatement are col
1f68e 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72  umns, then numer
1f68f 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20 75  ic affinity is u
1f690 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66 20  sed.      ** if 
1f691 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61  either column ha
1f692 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e 54  s NUMERIC or INT
1f693 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20 49  EGER affinity. I
1f694 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20 20  f neither.      
1f695 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20 53  ** 'x' nor the S
1f696 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65  ELECT... stateme
1f697 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20  nt are columns, 
1f698 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66  then numeric aff
1f699 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20 69  inity.      ** i
1f69a 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f  s used..      */
1f69b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54  .      pExpr->iT
1f69c 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e  able = pParse->n
1f69d 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64  Tab++;.      add
1f69e 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
1f69f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
1f6a0 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70 72  Ephemeral, pExpr
1f6a1 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f 77  ->iTable, !isRow
1f6a2 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65  id);.      memse
1f6a3 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73  t(&keyInfo, 0, s
1f6a4 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b  izeof(keyInfo));
1f6a5 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e  .      keyInfo.n
1f6a6 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20  Field = 1;..    
1f6a7 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
1f6a8 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1f6a9 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
1f6aa 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a        /* Case 1:
1f6ab 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 53 45       expr IN (SE
1f6ac 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20  LECT ...).      
1f6ad 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
1f6ae 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1f6af 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   write the resul
1f6b0 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ts of the select
1f6b1 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f 72   into the tempor
1f6b2 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ary.        ** t
1f6b3 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20 61  able allocated a
1f6b4 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e  nd opened above.
1f6b5 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1f6b6 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64      SelectDest d
1f6b7 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 78 70  est;.        Exp
1f6b8 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a  rList *pEList;..
1f6b9 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1f6ba 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 20 20  !isRowid );.    
1f6bb 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
1f6bc 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c  tDestInit(&dest,
1f6bd 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 72 2d   SRT_Set, pExpr-
1f6be 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 20  >iTable);.      
1f6bf 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 79 20    dest.affinity 
1f6c0 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 3b 0a  = (u8)affinity;.
1f6c1 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1f6c2 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 26 30  (pExpr->iTable&0
1f6c3 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 78 70  x0000FFFF)==pExp
1f6c4 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20  r->iTable );.   
1f6c5 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1f6c6 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
1f6c7 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c  Expr->x.pSelect,
1f6c8 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20   &dest) ){.     
1f6c9 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
1f6ca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
1f6cb 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78  EList = pExpr->x
1f6cc 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74  .pSelect->pEList
1f6cd 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45  ;.        if( pE
1f6ce 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e  List && pEList->
1f6cf 6e 45 78 70 72 3e 30 20 29 7b 20 0a 20 20 20 20  nExpr>0 ){ .    
1f6d0 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43        keyInfo.aC
1f6d1 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33  oll[0] = sqlite3
1f6d2 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
1f6d3 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78  lSeq(pParse, pEx
1f6d4 70 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20  pr->pLeft,.     
1f6d5 20 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d           pEList-
1f6d6 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20  >a[0].pExpr);.  
1f6d7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
1f6d8 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78  lse if( pExpr->x
1f6d9 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20  .pList ){.      
1f6da 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 20    /* Case 2:    
1f6db 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c 69   expr IN (exprli
1f6dc 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  st).        **. 
1f6dd 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65 61         ** For ea
1f6de 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62  ch expression, b
1f6df 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b 65  uild an index ke
1f6e0 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c 75  y from the evalu
1f6e1 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20  ation and.      
1f6e2 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e    ** store it in
1f6e3 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
1f6e4 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20  able. If <expr> 
1f6e5 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65  is a column, the
1f6e6 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  n use.        **
1f6e7 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66   that columns af
1f6e8 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69 6c  finity when buil
1f6e9 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 2e  ding index keys.
1f6ea 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f   If <expr> is no
1f6eb 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63  t.        ** a c
1f6ec 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72  olumn, use numer
1f6ed 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20  ic affinity..   
1f6ee 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1f6ef 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 45  int i;.        E
1f6f0 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
1f6f1 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b   pExpr->x.pList;
1f6f2 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
1f6f3 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
1f6f4 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e  Item;.        in
1f6f5 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20  t r1, r2, r3;.. 
1f6f6 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 69         if( !affi
1f6f7 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20  nity ){.        
1f6f8 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c    affinity = SQL
1f6f9 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
1f6fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1f6fb 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d  keyInfo.aColl[0]
1f6fc 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1f6fd 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
1f6fe 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20  xpr->pLeft);..  
1f6ff 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68        /* Loop th
1f700 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65  rough each expre
1f701 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69  ssion in <exprli
1f702 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  st>. */.        
1f703 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
1f704 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
1f705 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c          r2 = sql
1f706 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1f707 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
1f708 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f709 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
1f70a 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 6f   r2);.        fo
1f70b 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72  r(i=pList->nExpr
1f70c 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
1f70d 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
1f70e 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
1f70f 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65  Expr *pE2 = pIte
1f710 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20  m->pExpr;..     
1f711 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65       /* If the e
1f712 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74  xpression is not
1f713 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77   constant then w
1f714 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20  e will need to. 
1f715 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61           ** disa
1f716 62 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61  ble the test tha
1f717 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20  t was generated 
1f718 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73  above that makes
1f719 20 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20   sure.          
1f71a 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c  ** this code onl
1f71b 79 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e  y executes once.
1f71c 20 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20    Because for a 
1f71d 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20  non-constant.   
1f71e 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73         ** expres
1f71f 73 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20  sion we need to 
1f720 72 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20  rerun this code 
1f721 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20  each time..     
1f722 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1f723 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 26    if( testAddr &
1f724 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73  & !sqlite3ExprIs
1f725 43 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b  Constant(pE2) ){
1f726 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1f727 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
1f728 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72  Noop(v, testAddr
1f729 2d 31 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20  -1, 2);.        
1f72a 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 30      testAddr = 0
1f72b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20  ;.          }.. 
1f72c 20 20 20 20 20 20 20 20 20 2f 2a 20 45 76 61 6c           /* Eval
1f72d 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73  uate the express
1f72e 69 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74 20 69  ion and insert i
1f72f 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 20  t into the temp 
1f730 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  table */.       
1f731 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45     r3 = sqlite3E
1f732 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
1f733 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a  arse, pE2, r1);.
1f734 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73            if( is
1f735 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
1f736 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f737 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73  AddOp2(v, OP_Mus
1f738 74 42 65 49 6e 74 2c 20 72 33 2c 20 73 71 6c 69  tBeInt, r3, sqli
1f739 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
1f73a 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 20  dr(v)+2);.      
1f73b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f73c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
1f73d 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 61  sert, pExpr->iTa
1f73e 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a 20 20  ble, r2, r3);.  
1f73f 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1f740 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f741 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
1f742 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
1f743 33 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69 6e  3, 1, r2, &affin
1f744 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  ity, 1);.       
1f745 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1f746 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
1f747 6e 67 65 28 70 50 61 72 73 65 2c 20 72 33 2c 20  nge(pParse, r3, 
1f748 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
1f749 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f74a 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
1f74b 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  t, pExpr->iTable
1f74c 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  , r2);.         
1f74d 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1f74e 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
1f74f 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
1f750 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  e, r1);.        
1f751 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
1f752 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 32  mpReg(pParse, r2
1f753 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1f754 20 69 66 28 20 21 69 73 52 6f 77 69 64 20 29 7b   if( !isRowid ){
1f755 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1f756 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
1f757 61 64 64 72 2c 20 28 76 6f 69 64 20 2a 29 26 6b  addr, (void *)&k
1f758 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e  eyInfo, P4_KEYIN
1f759 46 4f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  FO);.      }.   
1f75a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1f75b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49  .    case TK_EXI
1f75c 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  STS:.    case TK
1f75d 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20  _SELECT: {.     
1f75e 20 2f 2a 20 54 68 69 73 20 68 61 73 20 74 6f 20   /* This has to 
1f75f 62 65 20 61 20 73 63 61 6c 61 72 20 53 45 4c 45  be a scalar SELE
1f760 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f  CT.  Generate co
1f761 64 65 20 74 6f 20 70 75 74 20 74 68 65 0a 20 20  de to put the.  
1f762 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20      ** value of 
1f763 74 68 69 73 20 73 65 6c 65 63 74 20 69 6e 20 61  this select in a
1f764 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64   memory cell and
1f765 20 72 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62   record the numb
1f766 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  er.      ** of t
1f767 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69  he memory cell i
1f768 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20  n iColumn..     
1f769 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63   */.      static
1f76a 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e 65   const Token one
1f76b 20 3d 20 7b 20 28 75 38 2a 29 22 31 22 2c 20 30   = { (u8*)"1", 0
1f76c 2c 20 30 2c 20 31 20 7d 3b 0a 20 20 20 20 20 20  , 0, 1 };.      
1f76d 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 0a 20 20  Select *pSel;.  
1f76e 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64      SelectDest d
1f76f 65 73 74 3b 0a 0a 20 20 20 20 20 20 61 73 73 65  est;..      asse
1f770 72 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  rt( ExprHasPrope
1f771 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
1f772 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20  sSelect) );.    
1f773 20 20 70 53 65 6c 20 3d 20 70 45 78 70 72 2d 3e    pSel = pExpr->
1f774 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20  x.pSelect;.     
1f775 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
1f776 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 30 2c  stInit(&dest, 0,
1f777 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29   ++pParse->nMem)
1f778 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  ;.      if( pExp
1f779 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54  r->op==TK_SELECT
1f77a 20 29 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74   ){.        dest
1f77b 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 4d 65 6d  .eDest = SRT_Mem
1f77c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1f77d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1f77e 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 74 2e  P_Null, 0, dest.
1f77f 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20  iParm);.        
1f780 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
1f781 22 49 6e 69 74 20 73 75 62 71 75 65 72 79 20 72  "Init subquery r
1f782 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20  esult"));.      
1f783 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64  }else{.        d
1f784 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f  est.eDest = SRT_
1f785 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20  Exists;.        
1f786 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f787 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
1f788 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b   0, dest.iParm);
1f789 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d  .        VdbeCom
1f78a 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 45  ment((v, "Init E
1f78b 58 49 53 54 53 20 72 65 73 75 6c 74 22 29 29 3b  XISTS result"));
1f78c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1f78d 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1f78e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65  (pParse->db, pSe
1f78f 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20  l->pLimit);.    
1f790 20 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d    pSel->pLimit =
1f791 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
1f792 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52  arse, TK_INTEGER
1f793 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29 3b 0a 20  , 0, 0, &one);. 
1f794 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1f795 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
1f796 53 65 6c 2c 20 26 64 65 73 74 29 20 29 7b 0a 20  Sel, &dest) ){. 
1f797 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
1f798 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 45 78       }.      pEx
1f799 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 64 65  pr->iColumn = de
1f79a 73 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20  st.iParm;.      
1f79b 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
1f79c 0a 0a 20 20 69 66 28 20 74 65 73 74 41 64 64 72  ..  if( testAddr
1f79d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1f79e 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74  dbeJumpHere(v, t
1f79f 65 73 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d 0a  estAddr-1);.  }.
1f7a0 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
1f7a1 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
1f7a2 3b 0a 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23  ;..  return;.}.#
1f7a3 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1f7a4 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
1f7a5 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74  ../*.** Duplicat
1f7a6 65 20 61 6e 20 38 2d 62 79 74 65 20 76 61 6c 75  e an 8-byte valu
1f7a7 65 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  e.*/.static char
1f7a8 20 2a 64 75 70 38 62 79 74 65 73 28 56 64 62 65   *dup8bytes(Vdbe
1f7a9 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *v, const char 
1f7aa 2a 69 6e 29 7b 0a 20 20 63 68 61 72 20 2a 6f 75  *in){.  char *ou
1f7ab 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
1f7ac 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 56 64  locRaw(sqlite3Vd
1f7ad 62 65 44 62 28 76 29 2c 20 38 29 3b 0a 20 20 69  beDb(v), 8);.  i
1f7ae 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 6d 65  f( out ){.    me
1f7af 6d 63 70 79 28 6f 75 74 2c 20 69 6e 2c 20 38 29  mcpy(out, in, 8)
1f7b0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f  ;.  }.  return o
1f7b1 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  ut;.}../*.** Gen
1f7b2 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63  erate an instruc
1f7b3 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70  tion that will p
1f7b4 75 74 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20  ut the floating 
1f7b5 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 64  point.** value d
1f7b6 65 73 63 72 69 62 65 64 20 62 79 20 7a 5b 30 2e  escribed by z[0.
1f7b7 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73  .n-1] into regis
1f7b8 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20  ter iMem..**.** 
1f7b9 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77  The z[] string w
1f7ba 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74  ill probably not
1f7bb 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   be zero-termina
1f7bc 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a  ted.  But the .*
1f7bd 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72  * z[n] character
1f7be 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
1f7bf 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74  o be something t
1f7c0 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f  hat does not loo
1f7c1 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f  k.** like the co
1f7c2 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68  ntinuation of th
1f7c3 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
1f7c4 74 69 63 20 76 6f 69 64 20 63 6f 64 65 52 65 61  tic void codeRea
1f7c5 6c 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74  l(Vdbe *v, const
1f7c6 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c   char *z, int n,
1f7c7 20 69 6e 74 20 6e 65 67 61 74 65 46 6c 61 67 2c   int negateFlag,
1f7c8 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 61 73   int iMem){.  as
1f7c9 73 65 72 74 28 20 7a 20 7c 7c 20 76 3d 3d 30 20  sert( z || v==0 
1f7ca 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 44 62  || sqlite3VdbeDb
1f7cb 28 76 29 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  (v)->mallocFaile
1f7cc 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  d );.  assert( !
1f7cd 7a 20 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 64  z || !sqlite3Isd
1f7ce 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20 20  igit(z[n]) );.  
1f7cf 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1f7d0 28 6e 29 3b 0a 20 20 69 66 28 20 7a 20 29 7b 0a  (n);.  if( z ){.
1f7d1 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 65      double value
1f7d2 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a  ;.    char *zV;.
1f7d3 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28      sqlite3AtoF(
1f7d4 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20  z, &value);.    
1f7d5 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  if( sqlite3IsNaN
1f7d6 28 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20  (value) ){.     
1f7d7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f7d8 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
1f7d9 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65 6c  , iMem);.    }el
1f7da 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65  se{.      if( ne
1f7db 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 65  gateFlag ) value
1f7dc 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 20   = -value;.     
1f7dd 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 28   zV = dup8bytes(
1f7de 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65  v, (char*)&value
1f7df 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f7e0 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1f7e1 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20  _Real, 0, iMem, 
1f7e2 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b  0, zV, P4_REAL);
1f7e3 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f  .    }.  }.}.../
1f7e4 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
1f7e5 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
1f7e6 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 69  t will put the i
1f7e7 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65 20  nteger describe 
1f7e8 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e 2e  by.** text z[0..
1f7e9 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 74  n-1] into regist
1f7ea 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54  er iMem..**.** T
1f7eb 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69  he z[] string wi
1f7ec 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20  ll probably not 
1f7ed 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  be zero-terminat
1f7ee 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a 2a  ed.  But the .**
1f7ef 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 20   z[n] character 
1f7f0 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
1f7f1 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 68   be something th
1f7f2 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b  at does not look
1f7f3 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e  .** like the con
1f7f4 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 65  tinuation of the
1f7f5 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
1f7f6 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74 65  ic void codeInte
1f7f7 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78 70  ger(Vdbe *v, Exp
1f7f8 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e 65  r *pExpr, int ne
1f7f9 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29  gFlag, int iMem)
1f7fa 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
1f7fb 7a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  z;.  if( pExpr->
1f7fc 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61  flags & EP_IntVa
1f7fd 6c 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  lue ){.    int i
1f7fe 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65   = pExpr->iTable
1f7ff 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61  ;.    if( negFla
1f800 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 20  g ) i = -i;.    
1f801 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f802 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
1f803 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 65 6c   i, iMem);.  }el
1f804 73 65 20 69 66 28 20 28 7a 20 3d 20 28 63 68 61  se if( (z = (cha
1f805 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  r*)pExpr->token.
1f806 7a 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  z)!=0 ){.    int
1f807 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20   i;.    int n = 
1f808 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a  pExpr->token.n;.
1f809 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c      assert( !sql
1f80a 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 6e 5d  ite3Isdigit(z[n]
1f80b 29 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ) );.    if( sql
1f80c 69 74 65 33 47 65 74 49 6e 74 33 32 28 7a 2c 20  ite3GetInt32(z, 
1f80d 26 69 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  &i) ){.      if(
1f80e 20 6e 65 67 46 6c 61 67 20 29 20 69 20 3d 20 2d   negFlag ) i = -
1f80f 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  i;.      sqlite3
1f810 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1f811 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d 65  _Integer, i, iMe
1f812 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  m);.    }else if
1f813 28 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36  ( sqlite3FitsIn6
1f814 34 42 69 74 73 28 7a 2c 20 6e 65 67 46 6c 61 67  4Bits(z, negFlag
1f815 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76  ) ){.      i64 v
1f816 61 6c 75 65 3b 0a 20 20 20 20 20 20 63 68 61 72  alue;.      char
1f817 20 2a 7a 56 3b 0a 20 20 20 20 20 20 73 71 6c 69   *zV;.      sqli
1f818 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 26 76 61  te3Atoi64(z, &va
1f819 6c 75 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  lue);.      if( 
1f81a 6e 65 67 46 6c 61 67 20 29 20 76 61 6c 75 65 20  negFlag ) value 
1f81b 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  = -value;.      
1f81c 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 76  zV = dup8bytes(v
1f81d 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29  , (char*)&value)
1f81e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1f81f 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1f820 49 6e 74 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20  Int64, 0, iMem, 
1f821 30 2c 20 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29  0, zV, P4_INT64)
1f822 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1f823 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a     codeReal(v, z
1f824 2c 20 6e 2c 20 6e 65 67 46 6c 61 67 2c 20 69 4d  , n, negFlag, iM
1f825 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  em);.    }.  }.}
1f826 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 20  ../*.** Clear a 
1f827 63 61 63 68 65 20 65 6e 74 72 79 2e 0a 2a 2f 0a  cache entry..*/.
1f828 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 63 68  static void cach
1f829 65 45 6e 74 72 79 43 6c 65 61 72 28 50 61 72 73  eEntryClear(Pars
1f82a 65 20 2a 70 50 61 72 73 65 2c 20 73 74 72 75 63  e *pParse, struc
1f82b 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 29 7b  t yColCache *p){
1f82c 0a 20 20 69 66 28 20 70 2d 3e 74 65 6d 70 52 65  .  if( p->tempRe
1f82d 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  g ){.    if( pPa
1f82e 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 72  rse->nTempReg<Ar
1f82f 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e  raySize(pParse->
1f830 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20 20  aTempReg) ){.   
1f831 20 20 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70     pParse->aTemp
1f832 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d  Reg[pParse->nTem
1f833 70 52 65 67 2b 2b 5d 20 3d 20 70 2d 3e 69 52 65  pReg++] = p->iRe
1f834 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  g;.    }.    p->
1f835 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 7d  tempReg = 0;.  }
1f836 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72  .}.../*.** Recor
1f837 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20  d in the column 
1f838 63 61 63 68 65 20 74 68 61 74 20 61 20 70 61 72  cache that a par
1f839 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 20 66  ticular column f
1f83a 72 6f 6d 20 61 0a 2a 2a 20 70 61 72 74 69 63 75  rom a.** particu
1f83b 6c 61 72 20 74 61 62 6c 65 20 69 73 20 73 74 6f  lar table is sto
1f83c 72 65 64 20 69 6e 20 61 20 70 61 72 74 69 63 75  red in a particu
1f83d 6c 61 72 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f  lar register..*/
1f83e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f83f 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1f840 43 61 63 68 65 53 74 6f 72 65 28 50 61 72 73 65  CacheStore(Parse
1f841 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 54   *pParse, int iT
1f842 61 62 2c 20 69 6e 74 20 69 43 6f 6c 2c 20 69 6e  ab, int iCol, in
1f843 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t iReg){.  int i
1f844 3b 0a 20 20 69 6e 74 20 6d 69 6e 4c 72 75 3b 0a  ;.  int minLru;.
1f845 20 20 69 6e 74 20 69 64 78 4c 72 75 3b 0a 20 20    int idxLru;.  
1f846 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
1f847 20 2a 70 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74   *p;..  /* First
1f848 20 72 65 70 6c 61 63 65 20 61 6e 79 20 65 78 69   replace any exi
1f849 73 74 69 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20  sting entry */. 
1f84a 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
1f84b 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
1f84c 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
1f84d 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
1f84e 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26     if( p->iReg &
1f84f 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61  & p->iTable==iTa
1f850 62 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d  b && p->iColumn=
1f851 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 63  =iCol ){.      c
1f852 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70  acheEntryClear(p
1f853 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20  Parse, p);.     
1f854 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61   p->iLevel = pPa
1f855 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c  rse->iCacheLevel
1f856 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20  ;.      p->iReg 
1f857 3d 20 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d  = iReg;.      p-
1f858 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a  >affChange = 0;.
1f859 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70        p->lru = p
1f85a 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74  Parse->iCacheCnt
1f85b 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ++;.      return
1f85c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1f85d 28 20 69 52 65 67 3c 3d 30 20 29 20 72 65 74 75  ( iReg<=0 ) retu
1f85e 72 6e 3b 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 61  rn;..  /* Find a
1f85f 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20 61 6e 64  n empty slot and
1f860 20 72 65 70 6c 61 63 65 20 69 74 20 2a 2f 0a 20   replace it */. 
1f861 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
1f862 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
1f863 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
1f864 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
1f865 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d     if( p->iReg==
1f866 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  0 ){.      p->iL
1f867 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69  evel = pParse->i
1f868 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20  CacheLevel;.    
1f869 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54    p->iTable = iT
1f86a 61 62 3b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f  ab;.      p->iCo
1f86b 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lumn = iCol;.   
1f86c 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65     p->iReg = iRe
1f86d 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 43  g;.      p->affC
1f86e 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 20  hange = 0;.     
1f86f 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b   p->tempReg = 0;
1f870 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20  .      p->lru = 
1f871 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e  pParse->iCacheCn
1f872 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72  t++;.      retur
1f873 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
1f874 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 6c  /* Replace the l
1f875 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 73 65  ast recently use
1f876 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 75 20 3d 20  d */.  minLru = 
1f877 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 69 64  0x7fffffff;.  id
1f878 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 20 66 6f 72  xLru = -1;.  for
1f879 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
1f87a 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
1f87b 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
1f87c 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
1f87d 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 6e 4c 72 75  f( p->lru<minLru
1f87e 20 29 7b 0a 20 20 20 20 20 20 69 64 78 4c 72 75   ){.      idxLru
1f87f 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d 69 6e 4c   = i;.      minL
1f880 72 75 20 3d 20 70 2d 3e 6c 72 75 3b 0a 20 20 20  ru = p->lru;.   
1f881 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 64 78   }.  }.  if( idx
1f882 4c 72 75 3e 3d 30 20 29 7b 0a 20 20 20 20 70 20  Lru>=0 ){.    p 
1f883 3d 20 26 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43  = &pParse->aColC
1f884 61 63 68 65 5b 69 64 78 4c 72 75 5d 3b 0a 20 20  ache[idxLru];.  
1f885 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50    p->iLevel = pP
1f886 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65  arse->iCacheLeve
1f887 6c 3b 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65  l;.    p->iTable
1f888 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e   = iTab;.    p->
1f889 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a  iColumn = iCol;.
1f88a 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52      p->iReg = iR
1f88b 65 67 3b 0a 20 20 20 20 70 2d 3e 61 66 66 43 68  eg;.    p->affCh
1f88c 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d  ange = 0;.    p-
1f88d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20  >tempReg = 0;.  
1f88e 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73    p->lru = pPars
1f88f 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a  e->iCacheCnt++;.
1f890 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
1f891 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74  }../*.** Indicat
1f892 65 20 74 68 61 74 20 61 20 72 65 67 69 73 74 65  e that a registe
1f893 72 20 69 73 20 62 65 69 6e 67 20 6f 76 65 72 77  r is being overw
1f894 72 69 74 74 65 6e 2e 20 20 50 75 72 67 65 20 74  ritten.  Purge t
1f895 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 66  he register.** f
1f896 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63  rom the column c
1f897 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ache..*/.SQLITE_
1f898 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1f899 69 74 65 33 45 78 70 72 43 61 63 68 65 52 65 6d  ite3ExprCacheRem
1f89a 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ove(Parse *pPars
1f89b 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20  e, int iReg){.  
1f89c 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
1f89d 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20  yColCache *p;.  
1f89e 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73  for(i=0, p=pPars
1f89f 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c  e->aColCache; i<
1f8a0 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
1f8a1 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20  E; i++, p++){.  
1f8a2 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69    if( p->iReg==i
1f8a3 52 65 67 20 29 7b 0a 20 20 20 20 20 20 63 61 63  Reg ){.      cac
1f8a4 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61  heEntryClear(pPa
1f8a5 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70  rse, p);.      p
1f8a6 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20  ->iReg = 0;.    
1f8a7 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
1f8a8 65 6d 65 6d 62 65 72 20 74 68 65 20 63 75 72 72  emember the curr
1f8a9 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  ent column cache
1f8aa 20 63 6f 6e 74 65 78 74 2e 20 20 41 6e 79 20 6e   context.  Any n
1f8ab 65 77 20 65 6e 74 72 69 65 73 20 61 64 64 65 64  ew entries added
1f8ac 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65  .** added to the
1f8ad 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 61 66   column cache af
1f8ae 74 65 72 20 74 68 69 73 20 63 61 6c 6c 20 61 72  ter this call ar
1f8af 65 20 72 65 6d 6f 76 65 64 20 77 68 65 6e 20 74  e removed when t
1f8b0 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  he.** correspond
1f8b1 69 6e 67 20 70 6f 70 20 6f 63 63 75 72 73 2e 0a  ing pop occurs..
1f8b2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1f8b3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
1f8b4 70 72 43 61 63 68 65 50 75 73 68 28 50 61 72 73  prCachePush(Pars
1f8b5 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 70 50  e *pParse){.  pP
1f8b6 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65  arse->iCacheLeve
1f8b7 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  l++;.}../*.** Re
1f8b8 6d 6f 76 65 20 66 72 6f 6d 20 74 68 65 20 63 6f  move from the co
1f8b9 6c 75 6d 6e 20 63 61 63 68 65 20 61 6e 79 20 65  lumn cache any e
1f8ba 6e 74 72 69 65 73 20 74 68 61 74 20 77 65 72 65  ntries that were
1f8bb 20 61 64 64 65 64 20 73 69 6e 63 65 20 74 68 65   added since the
1f8bc 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73  .** the previous
1f8bd 20 4e 20 50 75 73 68 20 6f 70 65 72 61 74 69 6f   N Push operatio
1f8be 6e 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ns.  In other wo
1f8bf 72 64 73 2c 20 72 65 73 74 6f 72 65 20 74 68 65  rds, restore the
1f8c0 20 63 61 63 68 65 0a 2a 2a 20 74 6f 20 74 68 65   cache.** to the
1f8c1 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69 6e   state it was in
1f8c2 20 4e 20 50 75 73 68 65 73 20 61 67 6f 2e 0a 2a   N Pushes ago..*
1f8c3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f8c4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
1f8c5 72 43 61 63 68 65 50 6f 70 28 50 61 72 73 65 20  rCachePop(Parse 
1f8c6 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 4e 29 7b  *pParse, int N){
1f8c7 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
1f8c8 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b  ct yColCache *p;
1f8c9 0a 20 20 61 73 73 65 72 74 28 20 4e 3e 30 20 29  .  assert( N>0 )
1f8ca 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
1f8cb 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e  se->iCacheLevel>
1f8cc 3d 4e 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  =N );.  pParse->
1f8cd 69 43 61 63 68 65 4c 65 76 65 6c 20 2d 3d 20 4e  iCacheLevel -= N
1f8ce 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70  ;.  for(i=0, p=p
1f8cf 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
1f8d0 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  ; i<SQLITE_N_COL
1f8d1 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29  CACHE; i++, p++)
1f8d2 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65  {.    if( p->iRe
1f8d3 67 20 26 26 20 70 2d 3e 69 4c 65 76 65 6c 3e 70  g && p->iLevel>p
1f8d4 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76  Parse->iCacheLev
1f8d5 65 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68  el ){.      cach
1f8d6 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72  eEntryClear(pPar
1f8d7 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d  se, p);.      p-
1f8d8 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d  >iReg = 0;.    }
1f8d9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
1f8da 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
1f8db 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68   will extract th
1f8dc 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c  e iColumn-th col
1f8dd 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c  umn from.** tabl
1f8de 65 20 70 54 61 62 20 61 6e 64 20 73 74 6f 72 65  e pTab and store
1f8df 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75   the column valu
1f8e0 65 20 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e  e in a register.
1f8e1 20 20 41 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69    An effort.** i
1f8e2 73 20 6d 61 64 65 20 74 6f 20 73 74 6f 72 65 20  s made to store 
1f8e3 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  the column value
1f8e4 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65   in register iRe
1f8e5 67 2c 20 62 75 74 20 74 68 69 73 20 69 73 0a 2a  g, but this is.*
1f8e6 2a 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64  * not guaranteed
1f8e7 2e 20 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20  .  The location 
1f8e8 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61  of the column va
1f8e9 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  lue is returned.
1f8ea 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
1f8eb 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72  t be an open cur
1f8ec 73 6f 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69  sor to pTab in i
1f8ed 54 61 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20  Table when this 
1f8ee 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61  routine.** is ca
1f8ef 6c 6c 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d  lled.  If iColum
1f8f0 6e 3c 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73  n<0 then code is
1f8f1 20 67 65 6e 65 72 61 74 65 64 20 74 68 61 74 20   generated that 
1f8f2 65 78 74 72 61 63 74 73 20 74 68 65 20 72 6f 77  extracts the row
1f8f3 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  id..**.** This r
1f8f4 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 61 74 74  outine might att
1f8f5 65 6d 70 74 20 74 6f 20 72 65 75 73 65 20 74 68  empt to reuse th
1f8f6 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63  e value of the c
1f8f7 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61  olumn that.** ha
1f8f8 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c  s already been l
1f8f9 6f 61 64 65 64 20 69 6e 74 6f 20 61 20 72 65 67  oaded into a reg
1f8fa 69 73 74 65 72 2e 20 20 54 68 65 20 76 61 6c 75  ister.  The valu
1f8fb 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a  e will always.**
1f8fc 20 62 65 20 75 73 65 64 20 69 66 20 69 74 20 68   be used if it h
1f8fd 61 73 20 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65  as not undergone
1f8fe 20 61 6e 79 20 61 66 66 69 6e 69 74 79 20 63 68   any affinity ch
1f8ff 61 6e 67 65 73 2e 20 20 42 75 74 20 69 66 0a 2a  anges.  But if.*
1f900 2a 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68  * an affinity ch
1f901 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65  ange has occurre
1f902 64 2c 20 74 68 65 6e 20 74 68 65 20 63 61 63 68  d, then the cach
1f903 65 64 20 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e  ed value will on
1f904 6c 79 20 62 65 0a 2a 2a 20 75 73 65 64 20 69 66  ly be.** used if
1f905 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 69 73   allowAffChng is
1f906 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   true..*/.SQLITE
1f907 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1f908 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
1f909 6f 6c 75 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a  olumn(.  Parse *
1f90a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72  pParse,   /* Par
1f90b 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65  sing and code ge
1f90c 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74  nerating context
1f90d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
1f90e 62 2c 20 20 20 20 20 2f 2a 20 44 65 73 63 72 69  b,     /* Descri
1f90f 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62  ption of the tab
1f910 6c 65 20 77 65 20 61 72 65 20 72 65 61 64 69 6e  le we are readin
1f911 67 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20  g from */.  int 
1f912 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20  iColumn,     /* 
1f913 49 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61 62  Index of the tab
1f914 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  le column */.  i
1f915 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20  nt iTable,      
1f916 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f  /* The cursor po
1f917 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 74 61  inting to the ta
1f918 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65  ble */.  int iRe
1f919 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f  g,        /* Sto
1f91a 72 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  re results here 
1f91b 2a 2f 0a 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66  */.  int allowAf
1f91c 66 43 68 6e 67 20 2f 2a 20 54 72 75 65 20 69 66  fChng /* True if
1f91d 20 70 72 69 6f 72 20 61 66 66 69 6e 69 74 79 20   prior affinity 
1f91e 63 68 61 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a  changes are OK *
1f91f 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
1f920 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
1f921 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
1f922 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
1f923 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50  .  for(i=0, p=pP
1f924 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b  arse->aColCache;
1f925 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43   i<SQLITE_N_COLC
1f926 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b  ACHE; i++, p++){
1f927 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67  .    if( p->iReg
1f928 3e 30 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d  >0 && p->iTable=
1f929 3d 69 54 61 62 6c 65 20 26 26 20 70 2d 3e 69 43  =iTable && p->iC
1f92a 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20  olumn==iColumn. 
1f92b 20 20 20 20 20 20 20 20 20 20 26 26 20 28 21 70            && (!p
1f92c 2d 3e 61 66 66 43 68 61 6e 67 65 20 7c 7c 20 61  ->affChange || a
1f92d 6c 6c 6f 77 41 66 66 43 68 6e 67 29 20 29 7b 0a  llowAffChng) ){.
1f92e 23 69 66 20 30 0a 20 20 20 20 20 20 73 71 6c 69  #if 0.      sqli
1f92f 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
1f930 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 20 20 20   OP_Noop);.     
1f931 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
1f932 20 22 4f 50 54 3a 20 74 61 62 25 64 2e 63 6f 6c   "OPT: tab%d.col
1f933 25 64 20 2d 3e 20 72 25 64 22 2c 20 69 54 61 62  %d -> r%d", iTab
1f934 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70 2d 3e  le, iColumn, p->
1f935 69 52 65 67 29 29 3b 0a 23 65 6e 64 69 66 0a 20  iReg));.#endif. 
1f936 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50       p->lru = pP
1f937 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b  arse->iCacheCnt+
1f938 2b 3b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70  +;.      p->temp
1f939 52 65 67 20 3d 20 30 3b 20 20 2f 2a 20 54 68 69  Reg = 0;  /* Thi
1f93a 73 20 70 69 6e 73 20 74 68 65 20 72 65 67 69 73  s pins the regis
1f93b 74 65 72 2c 20 62 75 74 20 61 6c 73 6f 20 6c 65  ter, but also le
1f93c 61 6b 73 20 69 74 20 2a 2f 0a 20 20 20 20 20 20  aks it */.      
1f93d 72 65 74 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a  return p->iReg;.
1f93e 20 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73      }.  }  .  as
1f93f 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20  sert( v!=0 );.  
1f940 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b  if( iColumn<0 ){
1f941 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1f942 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
1f943 69 64 2c 20 69 54 61 62 6c 65 2c 20 69 52 65 67  id, iTable, iReg
1f944 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  );.  }else if( p
1f945 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Tab==0 ){.    sq
1f946 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1f947 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54  v, OP_Column, iT
1f948 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69  able, iColumn, i
1f949 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Reg);.  }else{. 
1f94a 20 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69     int op = IsVi
1f94b 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50  rtual(pTab) ? OP
1f94c 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f  _VColumn : OP_Co
1f94d 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65  lumn;.    sqlite
1f94e 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f  3VdbeAddOp3(v, o
1f94f 70 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75  p, iTable, iColu
1f950 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73  mn, iReg);.    s
1f951 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
1f952 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f  ult(v, pTab, iCo
1f953 6c 75 6d 6e 29 3b 0a 23 69 66 6e 64 65 66 20 53  lumn);.#ifndef S
1f954 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
1f955 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 69 66  ING_POINT.    if
1f956 28 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  ( pTab->aCol[iCo
1f957 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d  lumn].affinity==
1f958 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20  SQLITE_AFF_REAL 
1f959 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1f95a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
1f95b 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20 69  _RealAffinity, i
1f95c 52 65 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  Reg);.    }.#end
1f95d 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  if.  }.  sqlite3
1f95e 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70  ExprCacheStore(p
1f95f 50 61 72 73 65 2c 20 69 54 61 62 6c 65 2c 20 69  Parse, iTable, i
1f960 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20  Column, iReg);. 
1f961 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a   return iReg;.}.
1f962 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c 6c  ./*.** Clear all
1f963 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e   column cache en
1f964 74 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tries..*/.SQLITE
1f965 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1f966 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
1f967 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  ear(Parse *pPars
1f968 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  e){.  int i;.  s
1f969 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
1f96a 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20  *p;..  for(i=0, 
1f96b 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
1f96c 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
1f96d 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
1f96e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ++){.    if( p->
1f96f 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 63 61  iReg ){.      ca
1f970 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50  cheEntryClear(pP
1f971 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20  arse, p);.      
1f972 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20  p->iReg = 0;.   
1f973 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
1f974 52 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20  Record the fact 
1f975 74 68 61 74 20 61 6e 20 61 66 66 69 6e 69 74 79  that an affinity
1f976 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75   change has occu
1f977 72 72 65 64 20 6f 6e 20 69 43 6f 75 6e 74 0a 2a  rred on iCount.*
1f978 2a 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  * registers star
1f979 74 69 6e 67 20 77 69 74 68 20 69 53 74 61 72 74  ting with iStart
1f97a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f97b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f97c 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
1f97d 79 43 68 61 6e 67 65 28 50 61 72 73 65 20 2a 70  yChange(Parse *p
1f97e 50 61 72 73 65 2c 20 69 6e 74 20 69 53 74 61 72  Parse, int iStar
1f97f 74 2c 20 69 6e 74 20 69 43 6f 75 6e 74 29 7b 0a  t, int iCount){.
1f980 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 69 53 74    int iEnd = iSt
1f981 61 72 74 20 2b 20 69 43 6f 75 6e 74 20 2d 20 31  art + iCount - 1
1f982 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  ;.  int i;.  str
1f983 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70  uct yColCache *p
1f984 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70  ;.  for(i=0, p=p
1f985 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
1f986 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  ; i<SQLITE_N_COL
1f987 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29  CACHE; i++, p++)
1f988 7b 0a 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d  {.    int r = p-
1f989 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72  >iReg;.    if( r
1f98a 3e 3d 69 53 74 61 72 74 20 26 26 20 72 3c 3d 69  >=iStart && r<=i
1f98b 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  End ){.      p->
1f98c 61 66 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20  affChange = 1;. 
1f98d 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
1f98e 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1f98f 74 6f 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20  to move content 
1f990 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69  from registers i
1f991 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65  From...iFrom+nRe
1f992 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69  g-1.** over to i
1f993 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20  To..iTo+nReg-1. 
1f994 4b 65 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20  Keep the column 
1f995 63 61 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65  cache up-to-date
1f996 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f997 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f998 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72  ExprCodeMove(Par
1f999 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1f99a 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20  iFrom, int iTo, 
1f99b 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74  int nReg){.  int
1f99c 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f   i;.  struct yCo
1f99d 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 69 66 28  lCache *p;.  if(
1f99e 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29 20 72 65   iFrom==iTo ) re
1f99f 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56  turn;.  sqlite3V
1f9a0 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65  dbeAddOp3(pParse
1f9a1 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65  ->pVdbe, OP_Move
1f9a2 2c 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52  , iFrom, iTo, nR
1f9a3 65 67 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  eg);.  for(i=0, 
1f9a4 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
1f9a5 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
1f9a6 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
1f9a7 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d  ++){.    int x =
1f9a8 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66   p->iReg;.    if
1f9a9 28 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c  ( x>=iFrom && x<
1f9aa 69 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20  iFrom+nReg ){.  
1f9ab 20 20 20 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69      p->iReg += i
1f9ac 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a  To-iFrom;.    }.
1f9ad 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e    }.}../*.** Gen
1f9ae 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 6f  erate code to co
1f9af 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20  py content from 
1f9b0 72 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e  registers iFrom.
1f9b1 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a  ..iFrom+nReg-1.*
1f9b2 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69  * over to iTo..i
1f9b3 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51  To+nReg-1..*/.SQ
1f9b4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1f9b5 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  d sqlite3ExprCod
1f9b6 65 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61  eCopy(Parse *pPa
1f9b7 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  rse, int iFrom, 
1f9b8 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65  int iTo, int nRe
1f9b9 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  g){.  int i;.  i
1f9ba 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29 20  f( iFrom==iTo ) 
1f9bb 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
1f9bc 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b 29 7b  0; i<nReg; i++){
1f9bd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1f9be 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70  AddOp2(pParse->p
1f9bf 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20 69  Vdbe, OP_Copy, i
1f9c0 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b 0a  From+i, iTo+i);.
1f9c1 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
1f9c2 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79 20  urn true if any 
1f9c3 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 20  register in the 
1f9c4 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54 6f  range iFrom..iTo
1f9c5 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a 20   (inclusive).** 
1f9c6 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74 20  is used as part 
1f9c7 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61  of the column ca
1f9c8 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  che..*/.static i
1f9c9 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43  nt usedAsColumnC
1f9ca 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61 72  ache(Parse *pPar
1f9cb 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69  se, int iFrom, i
1f9cc 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20 69  nt iTo){.  int i
1f9cd 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
1f9ce 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69  ache *p;.  for(i
1f9cf 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
1f9d0 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
1f9d1 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
1f9d2 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  +, p++){.    int
1f9d3 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20   r = p->iReg;.  
1f9d4 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d 20 26    if( r>=iFrom &
1f9d5 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74 75 72  & r<=iTo ) retur
1f9d6 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
1f9d7 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  n 0;.}../*.** If
1f9d8 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75   the last instru
1f9d9 63 74 69 6f 6e 20 63 6f 64 65 64 20 69 73 20 61  ction coded is a
1f9da 6e 20 65 70 68 65 6d 65 72 61 6c 20 63 6f 70 79  n ephemeral copy
1f9db 20 6f 66 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 68   of any of.** th
1f9dc 65 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 74  e registers in t
1f9dd 68 65 20 6e 52 65 67 20 72 65 67 69 73 74 65 72  he nReg register
1f9de 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  s beginning with
1f9df 20 69 52 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 63   iReg, then.** c
1f9e0 6f 6e 76 65 72 74 20 74 68 65 20 6c 61 73 74 20  onvert the last 
1f9e1 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 72 6f 6d  instruction from
1f9e2 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 4f 50 5f   OP_SCopy to OP_
1f9e3 43 6f 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Copy..*/.SQLITE_
1f9e4 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1f9e5 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79  ite3ExprHardCopy
1f9e6 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1f9e7 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52  int iReg, int nR
1f9e8 65 67 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b  eg){.  int addr;
1f9e9 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a  .  VdbeOp *pOp;.
1f9ea 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 76 20    Vdbe *v;..  v 
1f9eb 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
1f9ec 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  .  addr = sqlite
1f9ed 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
1f9ee 28 76 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c  (v);.  pOp = sql
1f9ef 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
1f9f0 20 61 64 64 72 2d 31 29 3b 0a 20 20 61 73 73 65   addr-1);.  asse
1f9f1 72 74 28 20 70 4f 70 20 7c 7c 20 70 50 61 72 73  rt( pOp || pPars
1f9f2 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
1f9f3 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 4f 70  led );.  if( pOp
1f9f4 20 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d   && pOp->opcode=
1f9f5 3d 4f 50 5f 53 43 6f 70 79 20 26 26 20 70 4f 70  =OP_SCopy && pOp
1f9f6 2d 3e 70 31 3e 3d 69 52 65 67 20 26 26 20 70 4f  ->p1>=iReg && pO
1f9f7 70 2d 3e 70 31 3c 69 52 65 67 2b 6e 52 65 67 20  p->p1<iReg+nReg 
1f9f8 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f  ){.    pOp->opco
1f9f9 64 65 20 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20  de = OP_Copy;.  
1f9fa 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
1f9fb 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 6f 72  ate code to stor
1f9fc 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
1f9fd 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69  he iAlias-th ali
1f9fe 61 73 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a  as in register.*
1f9ff 2a 20 74 61 72 67 65 74 2e 20 20 54 68 65 20 66  * target.  The f
1fa00 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69  irst time this i
1fa01 73 20 63 61 6c 6c 65 64 2c 20 70 45 78 70 72 20  s called, pExpr 
1fa02 69 73 20 65 76 61 6c 75 61 74 65 64 20 74 6f 20  is evaluated to 
1fa03 63 6f 6d 70 75 74 65 0a 2a 2a 20 74 68 65 20 76  compute.** the v
1fa04 61 6c 75 65 20 6f 66 20 74 68 65 20 61 6c 69 61  alue of the alia
1fa05 73 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  s.  The value is
1fa06 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 20 61 75   stored in an au
1fa07 78 69 6c 69 61 72 79 20 72 65 67 69 73 74 65 72  xiliary register
1fa08 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 75 6d 62  .** and the numb
1fa09 65 72 20 6f 66 20 74 68 61 74 20 72 65 67 69 73  er of that regis
1fa0a 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
1fa0b 20 20 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20    On subsequent 
1fa0c 63 61 6c 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65  calls,.** the re
1fa0d 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 69 73  gister number is
1fa0e 20 72 65 74 75 72 6e 65 64 20 77 69 74 68 6f 75   returned withou
1fa0f 74 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 79  t generating any
1fa10 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74   code..**.** Not
1fa11 65 20 74 68 61 74 20 69 6e 20 6f 72 64 65 72 20  e that in order 
1fa12 66 6f 72 20 74 68 69 73 20 74 6f 20 77 6f 72 6b  for this to work
1fa13 2c 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 67  , code must be g
1fa14 65 6e 65 72 61 74 65 64 20 69 6e 20 74 68 65 0a  enerated in the.
1fa15 2a 2a 20 73 61 6d 65 20 6f 72 64 65 72 20 74 68  ** same order th
1fa16 61 74 20 69 74 20 69 73 20 65 78 65 63 75 74 65  at it is execute
1fa17 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73  d..**.** Aliases
1fa18 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74   are numbered st
1fa19 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e 20 20  arting with 1.  
1fa1a 53 6f 20 69 41 6c 69 61 73 20 69 73 20 69 6e 20  So iAlias is in 
1fa1b 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20  the range.** of 
1fa1c 31 20 74 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c  1 to pParse->nAl
1fa1d 69 61 73 20 69 6e 63 6c 75 73 69 76 65 2e 20 20  ias inclusive.  
1fa1e 0a 2a 2a 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61  .**.** pParse->a
1fa1f 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20  Alias[iAlias-1] 
1fa20 72 65 63 6f 72 64 73 20 74 68 65 20 72 65 67 69  records the regi
1fa21 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72  ster number wher
1fa22 65 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f  e the value.** o
1fa23 66 20 74 68 65 20 69 41 6c 69 61 73 2d 74 68 20  f the iAlias-th 
1fa24 61 6c 69 61 73 20 69 73 20 73 74 6f 72 65 64 2e  alias is stored.
1fa25 20 20 49 66 20 7a 65 72 6f 2c 20 74 68 61 74 20    If zero, that 
1fa26 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a  means that the.*
1fa27 2a 20 61 6c 69 61 73 20 68 61 73 20 6e 6f 74 20  * alias has not 
1fa28 79 65 74 20 62 65 65 6e 20 63 6f 6d 70 75 74 65  yet been compute
1fa29 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
1fa2a 20 63 6f 64 65 41 6c 69 61 73 28 50 61 72 73 65   codeAlias(Parse
1fa2b 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 41   *pParse, int iA
1fa2c 6c 69 61 73 2c 20 45 78 70 72 20 2a 70 45 78 70  lias, Expr *pExp
1fa2d 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a  r, int target){.
1fa2e 23 69 66 20 30 0a 20 20 73 71 6c 69 74 65 33 20  #if 0.  sqlite3 
1fa2f 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1fa30 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b 0a 20 20  ;.  int iReg;.  
1fa31 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69  if( pParse->nAli
1fa32 61 73 41 6c 6c 6f 63 3c 70 50 61 72 73 65 2d 3e  asAlloc<pParse->
1fa33 6e 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 70 50  nAlias ){.    pP
1fa34 61 72 73 65 2d 3e 61 41 6c 69 61 73 20 3d 20 73  arse->aAlias = s
1fa35 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
1fa36 72 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65  rFree(db, pParse
1fa37 2d 3e 61 41 6c 69 61 73 2c 0a 20 20 20 20 20 20  ->aAlias,.      
1fa38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa39 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
1fa3a 66 28 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73  f(pParse->aAlias
1fa3b 5b 30 5d 29 2a 70 50 61 72 73 65 2d 3e 6e 41 6c  [0])*pParse->nAl
1fa3c 69 61 73 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ias );.    testc
1fa3d 61 73 65 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ase( db->mallocF
1fa3e 61 69 6c 65 64 20 26 26 20 70 50 61 72 73 65 2d  ailed && pParse-
1fa3f 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3e 30 20 29  >nAliasAlloc>0 )
1fa40 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
1fa41 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
1fa42 75 72 6e 20 30 3b 0a 20 20 20 20 6d 65 6d 73 65  urn 0;.    memse
1fa43 74 28 26 70 50 61 72 73 65 2d 3e 61 41 6c 69 61  t(&pParse->aAlia
1fa44 73 5b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  s[pParse->nAlias
1fa45 41 6c 6c 6f 63 5d 2c 20 30 2c 0a 20 20 20 20 20  Alloc], 0,.     
1fa46 20 20 20 20 20 20 28 70 50 61 72 73 65 2d 3e 6e        (pParse->n
1fa47 41 6c 69 61 73 2d 70 50 61 72 73 65 2d 3e 6e 41  Alias-pParse->nA
1fa48 6c 69 61 73 41 6c 6c 6f 63 29 2a 73 69 7a 65 6f  liasAlloc)*sizeo
1fa49 66 28 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73  f(pParse->aAlias
1fa4a 5b 30 5d 29 29 3b 0a 20 20 20 20 70 50 61 72 73  [0]));.    pPars
1fa4b 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 20 3d  e->nAliasAlloc =
1fa4c 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 3b   pParse->nAlias;
1fa4d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
1fa4e 41 6c 69 61 73 3e 30 20 26 26 20 69 41 6c 69 61  Alias>0 && iAlia
1fa4f 73 3c 3d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61  s<=pParse->nAlia
1fa50 73 20 29 3b 0a 20 20 69 52 65 67 20 3d 20 70 50  s );.  iReg = pP
1fa51 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c  arse->aAlias[iAl
1fa52 69 61 73 2d 31 5d 3b 0a 20 20 69 66 28 20 69 52  ias-1];.  if( iR
1fa53 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  eg==0 ){.    if(
1fa54 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c   pParse->iCacheL
1fa55 65 76 65 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20  evel>0 ){.      
1fa56 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  iReg = sqlite3Ex
1fa57 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
1fa58 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67  rse, pExpr, targ
1fa59 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  et);.    }else{.
1fa5a 20 20 20 20 20 20 69 52 65 67 20 3d 20 2b 2b 70        iReg = ++p
1fa5b 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
1fa5c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
1fa5d 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
1fa5e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 20 70  , iReg);.      p
1fa5f 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41  Parse->aAlias[iA
1fa60 6c 69 61 73 2d 31 5d 20 3d 20 69 52 65 67 3b 0a  lias-1] = iReg;.
1fa61 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1fa62 72 6e 20 69 52 65 67 3b 0a 23 65 6c 73 65 0a 20  rn iReg;.#else. 
1fa63 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1fa64 52 28 69 41 6c 69 61 73 29 3b 0a 20 20 72 65 74  R(iAlias);.  ret
1fa65 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 43  urn sqlite3ExprC
1fa66 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65  odeTarget(pParse
1fa67 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29  , pExpr, target)
1fa68 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
1fa69 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1fa6a 69 6e 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  into the current
1fa6b 20 56 64 62 65 20 74 6f 20 65 76 61 6c 75 61 74   Vdbe to evaluat
1fa6c 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65  e the given.** e
1fa6d 78 70 72 65 73 73 69 6f 6e 2e 20 20 41 74 74 65  xpression.  Atte
1fa6e 6d 70 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65  mpt to store the
1fa6f 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69   results in regi
1fa70 73 74 65 72 20 22 74 61 72 67 65 74 22 2e 0a 2a  ster "target"..*
1fa71 2a 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 67  * Return the reg
1fa72 69 73 74 65 72 20 77 68 65 72 65 20 72 65 73 75  ister where resu
1fa73 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 2e 0a  lts are stored..
1fa74 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 69 73 20  **.** With this 
1fa75 72 6f 75 74 69 6e 65 2c 20 74 68 65 72 65 20 69  routine, there i
1fa76 73 20 6e 6f 20 67 75 61 72 61 6e 74 65 65 20 74  s no guarantee t
1fa77 68 61 74 20 72 65 73 75 6c 74 73 20 77 69 6c 6c  hat results will
1fa78 0a 2a 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e  .** be stored in
1fa79 20 74 61 72 67 65 74 2e 20 20 54 68 65 20 72 65   target.  The re
1fa7a 73 75 6c 74 20 6d 69 67 68 74 20 62 65 20 73 74  sult might be st
1fa7b 6f 72 65 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68  ored in some oth
1fa7c 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 69  er.** register i
1fa7d 66 20 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65  f it is convenie
1fa7e 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68  nt to do so.  Th
1fa7f 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
1fa80 6f 6e 0a 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b  on.** must check
1fa81 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65   the return code
1fa82 20 61 6e 64 20 6d 6f 76 65 20 74 68 65 20 72 65   and move the re
1fa83 73 75 6c 74 73 20 74 6f 20 74 68 65 20 64 65 73  sults to the des
1fa84 69 72 65 64 0a 2a 2a 20 72 65 67 69 73 74 65 72  ired.** register
1fa85 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1fa86 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
1fa87 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61  xprCodeTarget(Pa
1fa88 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
1fa89 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61  r *pExpr, int ta
1fa8a 72 67 65 74 29 7b 0a 20 20 56 64 62 65 20 2a 76  rget){.  Vdbe *v
1fa8b 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
1fa8c 3b 20 20 2f 2a 20 54 68 65 20 56 4d 20 75 6e 64  ;  /* The VM und
1fa8d 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
1fa8e 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20  */.  int op;    
1fa8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fa90 2a 20 54 68 65 20 6f 70 63 6f 64 65 20 62 65 69  * The opcode bei
1fa91 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e  ng coded */.  in
1fa92 74 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74  t inReg = target
1fa93 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c  ;       /* Resul
1fa94 74 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  ts stored in reg
1fa95 69 73 74 65 72 20 69 6e 52 65 67 20 2a 2f 0a 20  ister inReg */. 
1fa96 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20   int regFree1 = 
1fa97 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  0;         /* If
1fa98 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74   non-zero free t
1fa99 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65  his temporary re
1fa9a 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
1fa9b 72 65 67 46 72 65 65 32 20 3d 20 30 3b 20 20 20  regFree2 = 0;   
1fa9c 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d        /* If non-
1fa9d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 20 74  zero free this t
1fa9e 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
1fa9f 72 20 2a 2f 0a 20 20 69 6e 74 20 72 31 2c 20 72  r */.  int r1, r
1faa0 32 2c 20 72 33 2c 20 72 34 3b 20 20 20 20 20 20  2, r3, r4;      
1faa1 20 2f 2a 20 56 61 72 69 6f 75 73 20 72 65 67 69   /* Various regi
1faa2 73 74 65 72 20 6e 75 6d 62 65 72 73 20 2a 2f 0a  ster numbers */.
1faa3 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
1faa4 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
1faa5 62 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  b;.  assert( v!=
1faa6 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
1faa7 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72  ailed );.  asser
1faa8 74 28 20 74 61 72 67 65 74 3e 30 20 26 26 20 74  t( target>0 && t
1faa9 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e  arget<=pParse->n
1faaa 4d 65 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d  Mem );.  if( v==
1faab 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  0 ) return 0;.. 
1faac 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b   if( pExpr==0 ){
1faad 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c  .    op = TK_NUL
1faae 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  L;.  }else{.    
1faaf 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a  op = pExpr->op;.
1fab0 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f 70    }.  switch( op
1fab1 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
1fab2 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20  AGG_COLUMN: {.  
1fab3 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67      AggInfo *pAg
1fab4 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70  gInfo = pExpr->p
1fab5 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73  AggInfo;.      s
1fab6 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f  truct AggInfo_co
1fab7 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 49  l *pCol = &pAggI
1fab8 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d  nfo->aCol[pExpr-
1fab9 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 66  >iAgg];.      if
1faba 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72  ( !pAggInfo->dir
1fabb 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20  ectMode ){.     
1fabc 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d     assert( pCol-
1fabd 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 20  >iMem>0 );.     
1fabe 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d     inReg = pCol-
1fabf 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 62  >iMem;.        b
1fac0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  reak;.      }els
1fac1 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e  e if( pAggInfo->
1fac2 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29 7b  useSortingIdx ){
1fac3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1fac4 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1fac5 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e 66  _Column, pAggInf
1fac6 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20  o->sortingIdx,. 
1fac7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fac8 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
1fac9 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
1faca 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1facb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1facc 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72  }.      /* Other
1facd 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 20  wise, fall thru 
1face 69 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55  into the TK_COLU
1facf 4d 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d  MN case */.    }
1fad0 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c  .    case TK_COL
1fad1 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  UMN: {.      if(
1fad2 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30   pExpr->iTable<0
1fad3 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
1fad4 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73  his only happens
1fad5 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65   when coding che
1fad6 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  ck constraints *
1fad7 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
1fad8 28 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65  ( pParse->ckBase
1fad9 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e  >0 );.        in
1fada 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  Reg = pExpr->iCo
1fadb 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63  lumn + pParse->c
1fadc 6b 42 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 6c  kBase;.      }el
1fadd 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74  se{.        test
1fade 63 61 73 65 28 20 28 70 45 78 70 72 2d 3e 66 6c  case( (pExpr->fl
1fadf 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29  ags & EP_AnyAff)
1fae0 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69  !=0 );.        i
1fae1 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  nReg = sqlite3Ex
1fae2 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28  prCodeGetColumn(
1fae3 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1fae4 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Tab,.           
1fae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fae6 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f        pExpr->iCo
1fae7 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61  lumn, pExpr->iTa
1fae8 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 20  ble, target,.   
1fae9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1faea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1faeb 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
1faec 41 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 20 7d  AnyAff);.      }
1faed 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1faee 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1faef 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20  INTEGER: {.     
1faf0 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20   codeInteger(v, 
1faf1 70 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74  pExpr, 0, target
1faf2 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1faf3 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
1faf4 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20  K_FLOAT: {.     
1faf5 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28 63 68   codeReal(v, (ch
1faf6 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
1faf7 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  .z, pExpr->token
1faf8 2e 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a  .n, 0, target);.
1faf9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1fafa 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53   }.    case TK_S
1fafb 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 73  TRING: {.      s
1fafc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1fafd 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
1fafe 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 0a 20 20  0, target, 0,.  
1faff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fb00 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 45 78        (char*)pEx
1fb01 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78  pr->token.z, pEx
1fb02 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20  pr->token.n);.  
1fb03 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1fb04 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c  .    case TK_NUL
1fb05 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L: {.      sqlit
1fb06 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1fb07 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67  OP_Null, 0, targ
1fb08 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  et);.      break
1fb09 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
1fb0a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
1fb0b 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73  _LITERAL.    cas
1fb0c 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20  e TK_BLOB: {.   
1fb0d 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20     int n;.      
1fb0e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20  const char *z;. 
1fb0f 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f 62       char *zBlob
1fb10 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1fb11 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d  pExpr->token.n>=
1fb12 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  3 );.      asser
1fb13 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  t( pExpr->token.
1fb14 7a 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78  z[0]=='x' || pEx
1fb15 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d  pr->token.z[0]==
1fb16 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73  'X' );.      ass
1fb17 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65  ert( pExpr->toke
1fb18 6e 2e 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a  n.z[1]=='\'' );.
1fb19 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
1fb1a 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 70 45 78  xpr->token.z[pEx
1fb1b 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2d 31 5d 3d 3d  pr->token.n-1]==
1fb1c 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 6e 20  '\'' );.      n 
1fb1d 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  = pExpr->token.n
1fb1e 20 2d 20 33 3b 0a 20 20 20 20 20 20 7a 20 3d 20   - 3;.      z = 
1fb1f 28 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f  (char*)pExpr->to
1fb20 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20 20 20 20 20  ken.z + 2;.     
1fb21 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33   zBlob = sqlite3
1fb22 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65  HexToBlob(sqlite
1fb23 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 6e  3VdbeDb(v), z, n
1fb24 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1fb25 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1fb26 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 67  _Blob, n/2, targ
1fb27 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 34  et, 0, zBlob, P4
1fb28 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
1fb29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
1fb2a 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b  ndif.    case TK
1fb2b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20  _VARIABLE: {.   
1fb2c 20 20 20 69 6e 74 20 69 50 72 69 6f 72 3b 0a 20     int iPrior;. 
1fb2d 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70       VdbeOp *pOp
1fb2e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  ;.      if( pExp
1fb2f 72 2d 3e 74 6f 6b 65 6e 2e 6e 3c 3d 31 0a 20 20  r->token.n<=1.  
1fb30 20 20 20 20 20 20 20 26 26 20 28 69 50 72 69 6f         && (iPrio
1fb31 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  r = sqlite3VdbeC
1fb32 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d 31 29  urrentAddr(v)-1)
1fb33 3e 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20  >=0.         && 
1fb34 28 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  (pOp = sqlite3Vd
1fb35 62 65 47 65 74 4f 70 28 76 2c 20 69 50 72 69 6f  beGetOp(v, iPrio
1fb36 72 29 29 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  r))->opcode==OP_
1fb37 56 61 72 69 61 62 6c 65 0a 20 20 20 20 20 20 20  Variable.       
1fb38 20 20 26 26 20 70 4f 70 2d 3e 70 31 2b 70 4f 70    && pOp->p1+pOp
1fb39 2d 3e 70 33 3d 3d 70 45 78 70 72 2d 3e 69 54 61  ->p3==pExpr->iTa
1fb3a 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26 26 20  ble.         && 
1fb3b 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3d  pOp->p2+pOp->p3=
1fb3c 3d 74 61 72 67 65 74 0a 20 20 20 20 20 20 20 20  =target.        
1fb3d 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30   && pOp->p4.z==0
1fb3e 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
1fb3f 20 20 2f 2a 20 49 66 20 74 68 65 20 70 72 65 76    /* If the prev
1fb40 69 6f 75 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  ious instruction
1fb41 20 77 61 73 20 61 20 63 6f 70 79 20 6f 66 20 74   was a copy of t
1fb42 68 65 20 70 72 65 76 69 6f 75 73 20 75 6e 6e 61  he previous unna
1fb43 6d 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  med.        ** p
1fb44 61 72 61 6d 65 74 65 72 20 69 6e 74 6f 20 74 68  arameter into th
1fb45 65 20 70 72 65 76 69 6f 75 73 20 72 65 67 69 73  e previous regis
1fb46 74 65 72 2c 20 74 68 65 6e 20 73 69 6d 70 6c 79  ter, then simply
1fb47 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 0a 20   increment the. 
1fb48 20 20 20 20 20 20 20 2a 2a 20 72 65 70 65 61 74         ** repeat
1fb49 20 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20 70 72   count on the pr
1fb4a 69 6f 72 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ior instruction 
1fb4b 72 61 74 68 65 72 20 74 68 61 6e 20 6d 61 6b 69  rather than maki
1fb4c 6e 67 20 61 20 6e 65 77 0a 20 20 20 20 20 20 20  ng a new.       
1fb4d 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e   ** instruction.
1fb4e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1fb4f 20 20 20 20 70 4f 70 2d 3e 70 33 2b 2b 3b 0a 20      pOp->p3++;. 
1fb50 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1fb51 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fb52 64 64 4f 70 33 28 76 2c 20 4f 50 5f 56 61 72 69  ddOp3(v, OP_Vari
1fb53 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 69 54 61  able, pExpr->iTa
1fb54 62 6c 65 2c 20 74 61 72 67 65 74 2c 20 31 29 3b  ble, target, 1);
1fb55 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78  .        if( pEx
1fb56 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 31 20 29 7b  pr->token.n>1 ){
1fb57 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1fb58 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
1fb59 2c 20 2d 31 2c 20 28 63 68 61 72 2a 29 70 45 78  , -1, (char*)pEx
1fb5a 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78  pr->token.z, pEx
1fb5b 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20  pr->token.n);.  
1fb5c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1fb5d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1fb5e 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52   }.    case TK_R
1fb5f 45 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20  EGISTER: {.     
1fb60 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e   inReg = pExpr->
1fb61 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72  iTable;.      br
1fb62 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1fb63 61 73 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20  ase TK_AS: {.   
1fb64 20 20 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41     inReg = codeA
1fb65 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 78  lias(pParse, pEx
1fb66 70 72 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70  pr->iTable, pExp
1fb67 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74  r->pLeft, target
1fb68 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1fb69 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1fb6a 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20  LITE_OMIT_CAST. 
1fb6b 20 20 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a     case TK_CAST:
1fb6c 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72   {.      /* Expr
1fb6d 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66  essions of the f
1fb6e 6f 72 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66  orm:   CAST(pLef
1fb6f 74 20 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20  t AS token) */. 
1fb70 20 20 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f       int aff, to
1fb71 5f 6f 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67  _op;.      inReg
1fb72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1fb73 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
1fb74 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74   pExpr->pLeft, t
1fb75 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 61 66  arget);.      af
1fb76 66 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e  f = sqlite3Affin
1fb77 69 74 79 54 79 70 65 28 26 70 45 78 70 72 2d 3e  ityType(&pExpr->
1fb78 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 74 6f  token);.      to
1fb79 5f 6f 70 20 3d 20 61 66 66 20 2d 20 53 51 4c 49  _op = aff - SQLI
1fb7a 54 45 5f 41 46 46 5f 54 45 58 54 20 2b 20 4f 50  TE_AFF_TEXT + OP
1fb7b 5f 54 6f 54 65 78 74 3b 0a 20 20 20 20 20 20 61  _ToText;.      a
1fb7c 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50  ssert( to_op==OP
1fb7d 5f 54 6f 54 65 78 74 20 20 20 20 7c 7c 20 61 66  _ToText    || af
1fb7e 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  f!=SQLITE_AFF_TE
1fb7f 58 54 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61  XT    );.      a
1fb80 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50  ssert( to_op==OP
1fb81 5f 54 6f 42 6c 6f 62 20 20 20 20 7c 7c 20 61 66  _ToBlob    || af
1fb82 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  f!=SQLITE_AFF_NO
1fb83 4e 45 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61  NE    );.      a
1fb84 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50  ssert( to_op==OP
1fb85 5f 54 6f 4e 75 6d 65 72 69 63 20 7c 7c 20 61 66  _ToNumeric || af
1fb86 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  f!=SQLITE_AFF_NU
1fb87 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 20 20 61  MERIC );.      a
1fb88 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50  ssert( to_op==OP
1fb89 5f 54 6f 49 6e 74 20 20 20 20 20 7c 7c 20 61 66  _ToInt     || af
1fb8a 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  f!=SQLITE_AFF_IN
1fb8b 54 45 47 45 52 20 29 3b 0a 20 20 20 20 20 20 61  TEGER );.      a
1fb8c 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50  ssert( to_op==OP
1fb8d 5f 54 6f 52 65 61 6c 20 20 20 20 7c 7c 20 61 66  _ToReal    || af
1fb8e 66 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45  f!=SQLITE_AFF_RE
1fb8f 41 4c 20 20 20 20 29 3b 0a 20 20 20 20 20 20 74  AL    );.      t
1fb90 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d  estcase( to_op==
1fb91 4f 50 5f 54 6f 54 65 78 74 20 29 3b 0a 20 20 20  OP_ToText );.   
1fb92 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f     testcase( to_
1fb93 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b  op==OP_ToBlob );
1fb94 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fb95 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d   to_op==OP_ToNum
1fb96 65 72 69 63 20 29 3b 0a 20 20 20 20 20 20 74 65  eric );.      te
1fb97 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f  stcase( to_op==O
1fb98 50 5f 54 6f 49 6e 74 20 29 3b 0a 20 20 20 20 20  P_ToInt );.     
1fb99 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70   testcase( to_op
1fb9a 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 29 3b 0a 20  ==OP_ToReal );. 
1fb9b 20 20 20 20 20 69 66 28 20 69 6e 52 65 67 21 3d       if( inReg!=
1fb9c 74 61 72 67 65 74 20 29 7b 0a 20 20 20 20 20 20  target ){.      
1fb9d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fb9e 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
1fb9f 20 69 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b   inReg, target);
1fba0 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d  .        inReg =
1fba1 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 7d   target;.      }
1fba2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1fba3 62 65 41 64 64 4f 70 31 28 76 2c 20 74 6f 5f 6f  beAddOp1(v, to_o
1fba4 70 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20 20  p, inReg);.     
1fba5 20 74 65 73 74 63 61 73 65 28 20 75 73 65 64 41   testcase( usedA
1fba6 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61  sColumnCache(pPa
1fba7 72 73 65 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65  rse, inReg, inRe
1fba8 67 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  g) );.      sqli
1fba9 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
1fbaa 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
1fbab 65 2c 20 69 6e 52 65 67 2c 20 31 29 3b 0a 20 20  e, inReg, 1);.  
1fbac 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1fbad 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1fbae 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 20  E_OMIT_CAST */. 
1fbaf 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20     case TK_LT:. 
1fbb0 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20     case TK_LE:. 
1fbb1 20 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20     case TK_GT:. 
1fbb2 20 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20     case TK_GE:. 
1fbb3 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20     case TK_NE:. 
1fbb4 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b     case TK_EQ: {
1fbb5 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1fbb6 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20  K_LT==OP_Lt );. 
1fbb7 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1fbb8 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20  LE==OP_Le );.   
1fbb9 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54     assert( TK_GT
1fbba 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 20 20 20  ==OP_Gt );.     
1fbbb 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d   assert( TK_GE==
1fbbc 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20 20 20 61  OP_Ge );.      a
1fbbd 73 73 65 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50  ssert( TK_EQ==OP
1fbbe 5f 45 71 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Eq );.      ass
1fbbf 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e  ert( TK_NE==OP_N
1fbc0 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  e );.      testc
1fbc1 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29  ase( op==TK_LT )
1fbc2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fbc3 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20  ( op==TK_LE );. 
1fbc4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1fbc5 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20  p==TK_GT );.    
1fbc6 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1fbc7 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74  TK_GE );.      t
1fbc8 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1fbc9 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  EQ );.      test
1fbca 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20  case( op==TK_NE 
1fbcb 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
1fbcc 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61  pareOperands(pPa
1fbcd 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1fbce 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65  t, &r1, &regFree
1fbcf 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
1fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbd1 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67       pExpr->pRig
1fbd2 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65  ht, &r2, &regFre
1fbd3 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43  e2);.      codeC
1fbd4 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70  ompare(pParse, p
1fbd5 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78  Expr->pLeft, pEx
1fbd6 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a  pr->pRight, op,.
1fbd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbd8 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c    r1, r2, inReg,
1fbd9 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29   SQLITE_STOREP2)
1fbda 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fbdb 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
1fbdc 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fbdd 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a   regFree2==0 );.
1fbde 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1fbdf 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41   }.    case TK_A
1fbe0 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ND:.    case TK_
1fbe1 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  OR:.    case TK_
1fbe2 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54  PLUS:.    case T
1fbe3 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65  K_STAR:.    case
1fbe4 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63   TK_MINUS:.    c
1fbe5 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20  ase TK_REM:.    
1fbe6 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a  case TK_BITAND:.
1fbe7 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f      case TK_BITO
1fbe8 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  R:.    case TK_S
1fbe9 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54  LASH:.    case T
1fbea 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61  K_LSHIFT:.    ca
1fbeb 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20  se TK_RSHIFT: . 
1fbec 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41     case TK_CONCA
1fbed 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  T: {.      asser
1fbee 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e  t( TK_AND==OP_An
1fbef 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  d );.      asser
1fbf0 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20  t( TK_OR==OP_Or 
1fbf1 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1fbf2 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64   TK_PLUS==OP_Add
1fbf3 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1fbf4 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53  ( TK_MINUS==OP_S
1fbf5 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20  ubtract );.     
1fbf6 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d   assert( TK_REM=
1fbf7 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b  =OP_Remainder );
1fbf8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1fbf9 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74  K_BITAND==OP_Bit
1fbfa 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73  And );.      ass
1fbfb 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f  ert( TK_BITOR==O
1fbfc 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20  P_BitOr );.     
1fbfd 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53   assert( TK_SLAS
1fbfe 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a  H==OP_Divide );.
1fbff 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1fc00 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66  _LSHIFT==OP_Shif
1fc01 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61  tLeft );.      a
1fc02 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54  ssert( TK_RSHIFT
1fc03 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20  ==OP_ShiftRight 
1fc04 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1fc05 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43   TK_CONCAT==OP_C
1fc06 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20 20 74  oncat );.      t
1fc07 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1fc08 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73  AND );.      tes
1fc09 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52  tcase( op==TK_OR
1fc0a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1fc0b 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20  se( op==TK_PLUS 
1fc0c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1fc0d 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20  e( op==TK_MINUS 
1fc0e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1fc0f 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b  e( op==TK_REM );
1fc10 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fc11 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29   op==TK_BITAND )
1fc12 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fc13 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29  ( op==TK_BITOR )
1fc14 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fc15 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29  ( op==TK_SLASH )
1fc16 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fc17 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20  ( op==TK_LSHIFT 
1fc18 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1fc19 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54  e( op==TK_RSHIFT
1fc1a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1fc1b 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41  se( op==TK_CONCA
1fc1c 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  T );.      r1 = 
1fc1d 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1fc1e 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
1fc1f 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
1fc20 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d  ee1);.      r2 =
1fc21 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1fc22 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
1fc23 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67  pr->pRight, &reg
1fc24 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 73 71  Free2);.      sq
1fc25 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1fc26 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74  v, op, r2, r1, t
1fc27 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65  arget);.      te
1fc28 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
1fc29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
1fc2a 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
1fc2b 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
1fc2c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1fc2d 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20  e TK_UMINUS: {. 
1fc2e 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74       Expr *pLeft
1fc2f 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b   = pExpr->pLeft;
1fc30 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1fc31 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 69 66  Left );.      if
1fc32 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pLeft->op==TK_
1fc33 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20  FLOAT ){.       
1fc34 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28 63 68   codeReal(v, (ch
1fc35 61 72 2a 29 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e  ar*)pLeft->token
1fc36 2e 7a 2c 20 70 4c 65 66 74 2d 3e 74 6f 6b 65 6e  .z, pLeft->token
1fc37 2e 6e 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a  .n, 1, target);.
1fc38 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1fc39 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  pLeft->op==TK_IN
1fc3a 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20  TEGER ){.       
1fc3b 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20   codeInteger(v, 
1fc3c 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74  pLeft, 1, target
1fc3d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1fc3e 20 20 20 20 20 20 20 20 72 65 67 46 72 65 65 31          regFree1
1fc3f 20 3d 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47   = r1 = sqlite3G
1fc40 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
1fc41 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1fc42 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1fc43 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72  OP_Integer, 0, r
1fc44 31 29 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d  1);.        r2 =
1fc45 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1fc46 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
1fc47 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
1fc48 72 65 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73  ree2);.        s
1fc49 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1fc4a 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c  (v, OP_Subtract,
1fc4b 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29   r2, r1, target)
1fc4c 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
1fc4d 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
1fc4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1fc4f 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b   inReg = target;
1fc50 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1fc51 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1fc52 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65  BITNOT:.    case
1fc53 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20   TK_NOT: {.     
1fc54 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e   assert( TK_BITN
1fc55 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b  OT==OP_BitNot );
1fc56 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1fc57 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b  K_NOT==OP_Not );
1fc58 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fc59 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29   op==TK_BITNOT )
1fc5a 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1fc5b 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a  ( op==TK_NOT );.
1fc5c 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
1fc5d 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
1fc5e 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1fc5f 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b  eft, &regFree1);
1fc60 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1fc61 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
1fc62 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61        inReg = ta
1fc63 72 67 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  rget;.      sqli
1fc64 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1fc65 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b   op, r1, inReg);
1fc66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1fc67 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1fc68 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65  ISNULL:.    case
1fc69 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20   TK_NOTNULL: {. 
1fc6a 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20       int addr;. 
1fc6b 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1fc6c 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c  ISNULL==OP_IsNul
1fc6d 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  l );.      asser
1fc6e 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f  t( TK_NOTNULL==O
1fc6f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20  P_NotNull );.   
1fc70 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1fc71 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20  =TK_ISNULL );.  
1fc72 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1fc73 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a  ==TK_NOTNULL );.
1fc74 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1fc75 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
1fc76 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74  teger, 1, target
1fc77 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71  );.      r1 = sq
1fc78 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
1fc79 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
1fc7a 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65  >pLeft, &regFree
1fc7b 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  1);.      testca
1fc7c 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
1fc7d 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  );.      addr = 
1fc7e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fc7f 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b 0a 20 20  1(v, op, r1);.  
1fc80 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fc81 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
1fc82 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b  mm, target, -1);
1fc83 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1fc84 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
1fc85 64 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  dr);.      break
1fc86 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1fc87 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e   TK_AGG_FUNCTION
1fc88 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66  : {.      AggInf
1fc89 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45 78 70 72  o *pInfo = pExpr
1fc8a 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20  ->pAggInfo;.    
1fc8b 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29    if( pInfo==0 )
1fc8c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1fc8d 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1fc8e 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 67 67  , "misuse of agg
1fc8f 72 65 67 61 74 65 3a 20 25 54 22 2c 0a 20 20 20  regate: %T",.   
1fc90 20 20 20 20 20 20 20 20 20 26 70 45 78 70 72 2d           &pExpr-
1fc91 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 7d 65  >span);.      }e
1fc92 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 52  lse{.        inR
1fc93 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 46 75 6e  eg = pInfo->aFun
1fc94 63 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e 69  c[pExpr->iAgg].i
1fc95 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Mem;.      }.   
1fc96 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1fc97 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53      case TK_CONS
1fc98 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65  T_FUNC:.    case
1fc99 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a   TK_FUNCTION: {.
1fc9a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
1fc9b 70 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a 20  pFarg;       /* 
1fc9c 4c 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f 6e  List of function
1fc9d 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
1fc9e 20 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20 20      int nFarg;  
1fc9f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1fca0 6d 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f 6e  mber of function
1fca1 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
1fca2 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65      FuncDef *pDe
1fca3 66 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  f;         /* Th
1fca4 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  e function defin
1fca5 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a  ition object */.
1fca6 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20        int nId;  
1fca7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fca8 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 75  Length of the fu
1fca9 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e 20 62  nction name in b
1fcaa 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 63 6f  ytes */.      co
1fcab 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20  nst char *zId;  
1fcac 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63       /* The func
1fcad 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  tion name */.   
1fcae 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d 61 73 6b     int constMask
1fcaf 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4d 61 73   = 0;     /* Mas
1fcb0 6b 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72  k of function ar
1fcb1 67 75 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65  guments that are
1fcb2 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 20 20 20   constant */.   
1fcb3 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
1fcb4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1fcb5 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20  p counter */.   
1fcb6 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28     u8 enc = ENC(
1fcb7 64 62 29 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  db);      /* The
1fcb8 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75   text encoding u
1fcb9 73 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61  sed by this data
1fcba 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 6f  base */.      Co
1fcbb 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30  llSeq *pColl = 0
1fcbc 3b 20 20 20 20 2f 2a 20 41 20 63 6f 6c 6c 61 74  ;    /* A collat
1fcbd 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
1fcbe 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
1fcbf 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1fcc0 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
1fcc1 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 74 65  ect) );.      te
1fcc2 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43  stcase( op==TK_C
1fcc3 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20  ONST_FUNC );.   
1fcc4 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1fcc5 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a  =TK_FUNCTION );.
1fcc6 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
1fcc7 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78  sAnyProperty(pEx
1fcc8 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  pr, EP_TokenOnly
1fcc9 7c 45 50 5f 53 70 61 6e 54 6f 6b 65 6e 29 20 29  |EP_SpanToken) )
1fcca 7b 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20  {.        pFarg 
1fccb 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
1fccc 7b 0a 20 20 20 20 20 20 20 20 70 46 61 72 67 20  {.        pFarg 
1fccd 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
1fcce 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1fccf 6e 46 61 72 67 20 3d 20 70 46 61 72 67 20 3f 20  nFarg = pFarg ? 
1fcd0 70 46 61 72 67 2d 3e 6e 45 78 70 72 20 3a 20 30  pFarg->nExpr : 0
1fcd1 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 28 63  ;.      zId = (c
1fcd2 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
1fcd3 6e 2e 7a 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d  n.z;.      nId =
1fcd4 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b   pExpr->token.n;
1fcd5 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71  .      pDef = sq
1fcd6 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
1fcd7 6e 28 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20  n(db, zId, nId, 
1fcd8 6e 46 61 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a  nFarg, enc, 0);.
1fcd9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 44        assert( pD
1fcda 65 66 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  ef!=0 );.      i
1fcdb 66 28 20 70 46 61 72 67 20 29 7b 0a 20 20 20 20  f( pFarg ){.    
1fcdc 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
1fcdd 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
1fcde 72 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20  rse, nFarg);.   
1fcdf 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1fce0 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61  CodeExprList(pPa
1fce1 72 73 65 2c 20 70 46 61 72 67 2c 20 72 31 2c 20  rse, pFarg, r1, 
1fce2 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  1);.      }else{
1fce3 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 30 3b  .        r1 = 0;
1fce4 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  .      }.#ifndef
1fce5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1fce6 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20  TUALTABLE.      
1fce7 2f 2a 20 50 6f 73 73 69 62 6c 79 20 6f 76 65 72  /* Possibly over
1fce8 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f  load the functio
1fce9 6e 20 69 66 20 74 68 65 20 66 69 72 73 74 20 61  n if the first a
1fcea 72 67 75 6d 65 6e 74 20 69 73 0a 20 20 20 20 20  rgument is.     
1fceb 20 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 74 61   ** a virtual ta
1fcec 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20  ble column..    
1fced 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f    **.      ** Fo
1fcee 72 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e  r infix function
1fcef 73 20 28 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52  s (LIKE, GLOB, R
1fcf0 45 47 45 58 50 2c 20 61 6e 64 20 4d 41 54 43 48  EGEXP, and MATCH
1fcf1 29 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20  ) use the.      
1fcf2 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  ** second argume
1fcf3 6e 74 2c 20 6e 6f 74 20 74 68 65 20 66 69 72 73  nt, not the firs
1fcf4 74 2c 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  t, as the argume
1fcf5 6e 74 20 74 6f 20 74 65 73 74 20 74 6f 0a 20 20  nt to test to.  
1fcf6 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74      ** see if it
1fcf7 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   is a column in 
1fcf8 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
1fcf9 20 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62    This is done b
1fcfa 65 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20  ecause.      ** 
1fcfb 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  the left operand
1fcfc 20 6f 66 20 69 6e 66 69 78 20 66 75 6e 63 74 69   of infix functi
1fcfd 6f 6e 73 20 28 74 68 65 20 6f 70 65 72 61 6e 64  ons (the operand
1fcfe 20 77 65 20 77 61 6e 74 20 74 6f 0a 20 20 20 20   we want to.    
1fcff 20 20 2a 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65    ** control ove
1fd00 72 6c 6f 61 64 69 6e 67 29 20 65 6e 64 73 20 75  rloading) ends u
1fd01 70 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  p as the second 
1fd02 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a  argument to the.
1fd03 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f        ** functio
1fd04 6e 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 69  n.  The expressi
1fd05 6f 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 69 73  on "A glob B" is
1fd06 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a   equivalent to .
1fd07 20 20 20 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42        ** "glob(B
1fd08 2c 41 29 2e 20 20 57 65 20 77 61 6e 74 20 74 6f  ,A).  We want to
1fd09 20 75 73 65 20 74 68 65 20 41 20 69 6e 20 22 41   use the A in "A
1fd0a 20 67 6c 6f 62 20 42 22 20 74 6f 20 74 65 73 74   glob B" to test
1fd0b 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 66 75  .      ** for fu
1fd0c 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69  nction overloadi
1fd0d 6e 67 2e 20 20 42 75 74 20 77 65 20 75 73 65 20  ng.  But we use 
1fd0e 74 68 65 20 42 20 74 65 72 6d 20 69 6e 20 22 67  the B term in "g
1fd0f 6c 6f 62 28 42 2c 41 29 22 2e 0a 20 20 20 20 20  lob(B,A)"..     
1fd10 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 46   */.      if( nF
1fd11 61 72 67 3e 3d 32 20 26 26 20 28 70 45 78 70 72  arg>=2 && (pExpr
1fd12 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 66  ->flags & EP_Inf
1fd13 69 78 46 75 6e 63 29 20 29 7b 0a 20 20 20 20 20  ixFunc) ){.     
1fd14 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65     pDef = sqlite
1fd15 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e  3VtabOverloadFun
1fd16 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20  ction(db, pDef, 
1fd17 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b  nFarg, pFarg->a[
1fd18 31 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  1].pExpr);.     
1fd19 20 7d 65 6c 73 65 20 69 66 28 20 6e 46 61 72 67   }else if( nFarg
1fd1a 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  >0 ){.        pD
1fd1b 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62  ef = sqlite3Vtab
1fd1c 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e  OverloadFunction
1fd1d 28 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67  (db, pDef, nFarg
1fd1e 2c 20 70 46 61 72 67 2d 3e 61 5b 30 5d 2e 70 45  , pFarg->a[0].pE
1fd1f 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  xpr);.      }.#e
1fd20 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72 28 69  ndif.      for(i
1fd21 3d 30 3b 20 69 3c 6e 46 61 72 67 20 26 26 20 69  =0; i<nFarg && i
1fd22 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  <32; i++){.     
1fd23 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
1fd24 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 46 61  prIsConstant(pFa
1fd25 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20  rg->a[i].pExpr) 
1fd26 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  ){.          con
1fd27 73 74 4d 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29  stMask |= (1<<i)
1fd28 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1fd29 20 20 20 20 69 66 28 20 28 70 44 65 66 2d 3e 66      if( (pDef->f
1fd2a 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
1fd2b 4e 43 5f 4e 45 45 44 43 4f 4c 4c 29 21 3d 30 20  NC_NEEDCOLL)!=0 
1fd2c 26 26 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  && !pColl ){.   
1fd2d 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73         pColl = s
1fd2e 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
1fd2f 71 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d  q(pParse, pFarg-
1fd30 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20  >a[i].pExpr);.  
1fd31 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1fd32 20 20 20 20 20 20 69 66 28 20 70 44 65 66 2d 3e        if( pDef->
1fd33 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
1fd34 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a  UNC_NEEDCOLL ){.
1fd35 20 20 20 20 20 20 20 20 69 66 28 20 21 70 43 6f          if( !pCo
1fd36 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d  ll ) pColl = db-
1fd37 3e 70 44 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20  >pDfltColl; .   
1fd38 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1fd39 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp4(v, OP_Col
1fd3a 6c 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28  lSeq, 0, 0, 0, (
1fd3b 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34  char *)pColl, P4
1fd3c 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20  _COLLSEQ);.     
1fd3d 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1fd3e 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
1fd3f 5f 46 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74  _Function, const
1fd40 4d 61 73 6b 2c 20 72 31 2c 20 74 61 72 67 65 74  Mask, r1, target
1fd41 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1fd42 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
1fd43 29 70 44 65 66 2c 20 50 34 5f 46 55 4e 43 44 45  )pDef, P4_FUNCDE
1fd44 46 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  F);.      sqlite
1fd45 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
1fd46 20 28 75 38 29 6e 46 61 72 67 29 3b 0a 20 20 20   (u8)nFarg);.   
1fd47 20 20 20 69 66 28 20 6e 46 61 72 67 20 29 7b 0a     if( nFarg ){.
1fd48 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1fd49 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
1fd4a 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 46 61 72  pParse, r1, nFar
1fd4b 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
1fd4c 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
1fd4d 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
1fd4e 28 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 46 61  (pParse, r1, nFa
1fd4f 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  rg);.      break
1fd50 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
1fd51 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1fd52 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b  UERY.    case TK
1fd53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 73  _EXISTS:.    cas
1fd54 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20  e TK_SELECT: {. 
1fd55 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1fd56 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a  p==TK_EXISTS );.
1fd57 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1fd58 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b  op==TK_SELECT );
1fd59 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72  .      if( pExpr
1fd5a 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a  ->iColumn==0 ){.
1fd5b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
1fd5c 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 61  odeSubselect(pPa
1fd5d 72 73 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30  rse, pExpr, 0, 0
1fd5e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1fd5f 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e   inReg = pExpr->
1fd60 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 62  iColumn;.      b
1fd61 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1fd62 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20  case TK_IN: {.  
1fd63 20 20 20 20 69 6e 74 20 72 4e 6f 74 46 6f 75 6e      int rNotFoun
1fd64 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74  d = 0;.      int
1fd65 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20   rMayHaveNull = 
1fd66 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 32 2c  0;.      int j2,
1fd67 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20 20   j3, j4, j5;.   
1fd68 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79     char affinity
1fd69 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 54 79 70  ;.      int eTyp
1fd6a 65 3b 0a 0a 20 20 20 20 20 20 56 64 62 65 4e 6f  e;..      VdbeNo
1fd6b 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62  opComment((v, "b
1fd6c 65 67 69 6e 20 49 4e 20 65 78 70 72 20 72 25 64  egin IN expr r%d
1fd6d 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20 20  ", target));.   
1fd6e 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74     eType = sqlit
1fd6f 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70 50  e3FindInIndex(pP
1fd70 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 4d  arse, pExpr, &rM
1fd71 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20  ayHaveNull);.   
1fd72 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e     if( rMayHaveN
1fd73 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ull ){.        r
1fd74 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61  NotFound = ++pPa
1fd75 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
1fd76 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 67   }..      /* Fig
1fd77 75 72 65 20 6f 75 74 20 74 68 65 20 61 66 66 69  ure out the affi
1fd78 6e 69 74 79 20 74 6f 20 75 73 65 20 74 6f 20 63  nity to use to c
1fd79 72 65 61 74 65 20 61 20 6b 65 79 20 66 72 6f 6d  reate a key from
1fd7a 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20   the results.   
1fd7b 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 78 70     ** of the exp
1fd7c 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 6e 69 74  ression. affinit
1fd7d 79 53 74 72 20 73 74 6f 72 65 73 20 61 20 73 74  yStr stores a st
1fd7e 61 74 69 63 20 73 74 72 69 6e 67 20 73 75 69 74  atic string suit
1fd7f 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 20 20 2a  able for.      *
1fd80 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52  * P4 of OP_MakeR
1fd81 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  ecord..      */.
1fd82 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 3d        affinity =
1fd83 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e   comparisonAffin
1fd84 69 74 79 28 70 45 78 70 72 29 3b 0a 0a 0a 20 20  ity(pExpr);...  
1fd85 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20      /* Code the 
1fd86 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22 3c 65 78  <expr> from "<ex
1fd87 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22 2e 20 54  pr> IN (...)". T
1fd88 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
1fd89 6c 65 0a 20 20 20 20 20 20 2a 2a 20 70 45 78 70  le.      ** pExp
1fd8a 72 2d 3e 69 54 61 62 6c 65 20 63 6f 6e 74 61 69  r->iTable contai
1fd8b 6e 73 20 74 68 65 20 76 61 6c 75 65 73 20 74 68  ns the values th
1fd8c 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 28  at make up the (
1fd8d 2e 2e 2e 29 20 73 65 74 2e 0a 20 20 20 20 20 20  ...) set..      
1fd8e 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1fd8f 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
1fd90 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  arse);.      sql
1fd91 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
1fd92 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1fd93 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
1fd94 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64    j2 = sqlite3Vd
1fd95 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
1fd96 73 4e 75 6c 6c 2c 20 74 61 72 67 65 74 29 3b 0a  sNull, target);.
1fd97 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d        if( eType=
1fd98 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20  =IN_INDEX_ROWID 
1fd99 29 7b 0a 20 20 20 20 20 20 20 20 6a 33 20 3d 20  ){.        j3 = 
1fd9a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1fd9b 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e  1(v, OP_MustBeIn
1fd9c 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
1fd9d 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 33      j4 = sqlite3
1fd9e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1fd9f 5f 4e 6f 74 45 78 69 73 74 73 2c 20 70 45 78 70  _NotExists, pExp
1fda0 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 74 61  r->iTable, 0, ta
1fda1 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73  rget);.        s
1fda2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1fda3 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1fda4 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  1, target);.    
1fda5 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 33      j5 = sqlite3
1fda6 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
1fda7 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20 20  _Goto);.        
1fda8 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
1fda9 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20  ere(v, j3);.    
1fdaa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
1fdab 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a  umpHere(v, j4);.
1fdac 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1fdad 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1fdae 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72 67  Integer, 0, targ
1fdaf 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  et);.      }else
1fdb0 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 72  {.        r2 = r
1fdb1 65 67 46 72 65 65 32 20 3d 20 73 71 6c 69 74 65  egFree2 = sqlite
1fdb2 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
1fdb3 73 65 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  se);..        /*
1fdb4 20 43 72 65 61 74 65 20 61 20 72 65 63 6f 72 64   Create a record
1fdb5 20 61 6e 64 20 74 65 73 74 20 66 6f 72 20 73 65   and test for se
1fdb6 74 20 6d 65 6d 62 65 72 73 68 69 70 2e 20 49 66  t membership. If
1fdb7 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 69 6e   the set contain
1fdb8 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  s.        ** the
1fdb9 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 6a 75 6d   value, then jum
1fdba 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  p to the end of 
1fdbb 74 68 65 20 74 65 73 74 20 63 6f 64 65 2e 20 54  the test code. T
1fdbc 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 20 20  he target.      
1fdbd 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20 73 74    ** register st
1fdbe 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ill contains the
1fdbf 20 74 72 75 65 20 28 31 29 20 76 61 6c 75 65 20   true (1) value 
1fdc0 77 72 69 74 74 65 6e 20 74 6f 20 69 74 20 65 61  written to it ea
1fdc1 72 6c 69 65 72 2e 0a 20 20 20 20 20 20 20 20 2a  rlier..        *
1fdc2 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
1fdc3 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1fdc4 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 74 61  P_MakeRecord, ta
1fdc5 72 67 65 74 2c 20 31 2c 20 72 32 2c 20 26 61 66  rget, 1, r2, &af
1fdc6 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20  finity, 1);.    
1fdc7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1fdc8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
1fdc9 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b  ger, 1, target);
1fdca 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73 71  .        j5 = sq
1fdcb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1fdcc 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78  v, OP_Found, pEx
1fdcd 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72  pr->iTable, 0, r
1fdce 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  2);..        /* 
1fdcf 49 66 20 74 68 65 20 73 65 74 20 6d 65 6d 62 65  If the set membe
1fdd0 72 73 68 69 70 20 74 65 73 74 20 66 61 69 6c 73  rship test fails
1fdd1 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  , then the resul
1fdd2 74 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 20  t of the .      
1fdd3 20 20 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e 29    ** "x IN (...)
1fdd4 22 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73  " expression mus
1fdd5 74 20 62 65 20 65 69 74 68 65 72 20 30 20 6f 72  t be either 0 or
1fdd6 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 65   NULL. If the se
1fdd7 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e  t.        ** con
1fdd8 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61  tains no NULL va
1fdd9 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72  lues, then the r
1fdda 65 73 75 6c 74 20 69 73 20 30 2e 20 49 66 20 74  esult is 0. If t
1fddb 68 65 20 73 65 74 20 0a 20 20 20 20 20 20 20 20  he set .        
1fddc 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20  ** contains one 
1fddd 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c  or more NULL val
1fdde 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ues, then the re
1fddf 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20  sult of the.    
1fde0 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f      ** expressio
1fde1 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a  n is also NULL..
1fde2 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1fde3 20 20 20 69 66 28 20 72 4e 6f 74 46 6f 75 6e 64     if( rNotFound
1fde4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1fde5 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20   /* This branch 
1fde6 72 75 6e 73 20 69 66 20 69 74 20 69 73 20 6b 6e  runs if it is kn
1fde7 6f 77 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20 74  own at compile t
1fde8 69 6d 65 20 28 6e 6f 77 29 20 74 68 61 74 20 0a  ime (now) that .
1fde9 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65            ** the
1fdea 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f   set contains no
1fdeb 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54 68   NULL values. Th
1fdec 69 73 20 68 61 70 70 65 6e 73 20 61 73 20 74 68  is happens as th
1fded 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 20  e result.       
1fdee 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20     ** of a "NOT 
1fdef 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74  NULL" constraint
1fdf0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1fdf1 20 73 63 68 65 6d 61 2e 20 4e 6f 20 6e 65 65 64   schema. No need
1fdf2 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  .          ** to
1fdf3 20 74 65 73 74 20 74 68 65 20 64 61 74 61 20 73   test the data s
1fdf4 74 72 75 63 74 75 72 65 20 61 74 20 72 75 6e 74  tructure at runt
1fdf5 69 6d 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  ime in this case
1fdf6 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
1fdf7 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1fdf8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1fdf9 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72  _Integer, 0, tar
1fdfa 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  get);.        }e
1fdfb 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
1fdfc 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 70 6f 70  * This block pop
1fdfd 75 6c 61 74 65 73 20 74 68 65 20 72 4e 6f 74 46  ulates the rNotF
1fdfe 6f 75 6e 64 20 72 65 67 69 73 74 65 72 20 77 69  ound register wi
1fdff 74 68 20 65 69 74 68 65 72 20 4e 55 4c 4c 0a 20  th either NULL. 
1fe00 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 30           ** or 0
1fe01 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c   (an integer val
1fe02 75 65 29 2e 20 49 66 20 74 68 65 20 64 61 74 61  ue). If the data
1fe03 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
1fe04 69 6e 73 20 6f 6e 65 0a 20 20 20 20 20 20 20 20  ins one.        
1fe05 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 20 4e 55 4c    ** or more NUL
1fe06 4c 73 2c 20 74 68 65 6e 20 73 65 74 20 72 4e 6f  Ls, then set rNo
1fe07 74 46 6f 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e 20  tFound to NULL. 
1fe08 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74 20 69  Otherwise, set i
1fe09 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74  t.          ** t
1fe0a 6f 20 30 2e 20 49 66 20 72 65 67 69 73 74 65 72  o 0. If register
1fe0b 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73   rMayHaveNull is
1fe0c 20 61 6c 72 65 61 64 79 20 73 65 74 20 74 6f 20   already set to 
1fe0d 73 6f 6d 65 20 76 61 6c 75 65 0a 20 20 20 20 20  some value.     
1fe0e 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 74 68       ** other th
1fe0f 61 6e 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68  an NULL, then th
1fe10 65 20 74 65 73 74 20 68 61 73 20 61 6c 72 65 61  e test has alrea
1fe11 64 79 20 62 65 65 6e 20 72 75 6e 20 61 6e 64 20  dy been run and 
1fe12 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 4e  .          ** rN
1fe13 6f 74 46 6f 75 6e 64 20 69 73 20 61 6c 72 65 61  otFound is alrea
1fe14 64 79 20 70 6f 70 75 6c 61 74 65 64 2e 0a 20 20  dy populated..  
1fe15 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1fe16 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
1fe17 74 20 63 68 61 72 20 6e 75 6c 6c 52 65 63 6f 72  t char nullRecor
1fe18 64 5b 5d 20 3d 20 7b 20 30 78 30 32 2c 20 30 78  d[] = { 0x02, 0x
1fe19 30 30 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20  00 };.          
1fe1a 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j3 = sqlite3Vdbe
1fe1b 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
1fe1c 4e 75 6c 6c 2c 20 72 4d 61 79 48 61 76 65 4e 75  Null, rMayHaveNu
1fe1d 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ll);.          s
1fe1e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1fe1f 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
1fe20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 20  rNotFound);.    
1fe21 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1fe22 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c  eAddOp4(v, OP_Bl
1fe23 6f 62 2c 20 32 2c 20 72 4d 61 79 48 61 76 65 4e  ob, 2, rMayHaveN
1fe24 75 6c 6c 2c 20 30 2c 20 0a 20 20 20 20 20 20 20  ull, 0, .       
1fe25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fe26 20 20 20 20 20 20 6e 75 6c 6c 52 65 63 6f 72 64        nullRecord
1fe27 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
1fe28 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 71 6c          j4 = sql
1fe29 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1fe2a 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70  , OP_Found, pExp
1fe2b 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 4d  r->iTable, 0, rM
1fe2c 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20  ayHaveNull);.   
1fe2d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1fe2e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
1fe2f 6e 74 65 67 65 72 2c 20 30 2c 20 72 4e 6f 74 46  nteger, 0, rNotF
1fe30 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20  ound);.         
1fe31 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
1fe32 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20  Here(v, j4);.   
1fe33 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1fe34 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33  beJumpHere(v, j3
1fe35 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  );..          /*
1fe36 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 20   Copy the value 
1fe37 6f 66 20 72 65 67 69 73 74 65 72 20 72 4e 6f 74  of register rNot
1fe38 46 6f 75 6e 64 20 28 77 68 69 63 68 20 69 73 20  Found (which is 
1fe39 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 30  either NULL or 0
1fe3a 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69  ).          ** i
1fe3b 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 72  nto the target r
1fe3c 65 67 69 73 74 65 72 2e 20 54 68 69 73 20 77 69  egister. This wi
1fe3d 6c 6c 20 62 65 20 74 68 65 20 72 65 73 75 6c 74  ll be the result
1fe3e 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20   of the.        
1fe3f 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e    ** expression.
1fe40 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
1fe41 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1fe42 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1fe43 43 6f 70 79 2c 20 72 4e 6f 74 46 6f 75 6e 64 2c  Copy, rNotFound,
1fe44 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
1fe45 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1fe46 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1fe47 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20  pHere(v, j2);.  
1fe48 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
1fe49 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a  umpHere(v, j5);.
1fe4a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1fe4b 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65  rCachePop(pParse
1fe4c 2c 20 31 29 3b 0a 20 20 20 20 20 20 56 64 62 65  , 1);.      Vdbe
1fe4d 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64  Comment((v, "end
1fe4e 20 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 74   IN expr r%d", t
1fe4f 61 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 62  arget));.      b
1fe50 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
1fe51 69 66 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  if.    /*.    **
1fe52 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 20      x BETWEEN y 
1fe53 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 20  AND z.    **.   
1fe54 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75 69   ** This is equi
1fe55 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a 2a  valent to.    **
1fe56 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20  .    **    x>=y 
1fe57 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a 0a  AND x<=z.    **.
1fe58 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 6f 72      ** X is stor
1fe59 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65  ed in pExpr->pLe
1fe5a 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 20  ft..    ** Y is 
1fe5b 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d  stored in pExpr-
1fe5c 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  >pList->a[0].pEx
1fe5d 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73 20  pr..    ** Z is 
1fe5e 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d  stored in pExpr-
1fe5f 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78  >pList->a[1].pEx
1fe60 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  pr..    */.    c
1fe61 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20  ase TK_BETWEEN: 
1fe62 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c  {.      Expr *pL
1fe63 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65  eft = pExpr->pLe
1fe64 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74  ft;.      struct
1fe65 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1fe66 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 2d 3e  pLItem = pExpr->
1fe67 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20  x.pList->a;.    
1fe68 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d    Expr *pRight =
1fe69 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a   pLItem->pExpr;.
1fe6a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
1fe6b 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73  reOperands(pPars
1fe6c 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 26  e, pLeft, &r1, &
1fe6d 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20  regFree1,.      
1fe6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fe6f 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67              pRig
1fe70 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65  ht, &r2, &regFre
1fe71 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  e2);.      testc
1fe72 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
1fe73 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1fe74 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
1fe75 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73 71  );.      r3 = sq
1fe76 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
1fe77 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72  pParse);.      r
1fe78 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  4 = sqlite3GetTe
1fe79 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
1fe7a 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
1fe7b 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20  (pParse, pLeft, 
1fe7c 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a 20  pRight, OP_Ge,. 
1fe7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fe7e 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 4c   r1, r2, r3, SQL
1fe7f 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20  ITE_STOREP2);.  
1fe80 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20      pLItem++;.  
1fe81 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c 49      pRight = pLI
1fe82 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  tem->pExpr;.    
1fe83 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
1fe84 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
1fe85 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
1fe86 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r2 = sqlite3Exp
1fe87 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
1fe88 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 46 72  , pRight, &regFr
1fe89 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ee2);.      test
1fe8a 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
1fe8b 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43  0 );.      codeC
1fe8c 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70  ompare(pParse, p
1fe8d 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50  Left, pRight, OP
1fe8e 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 2c  _Le, r1, r2, r4,
1fe8f 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29   SQLITE_STOREP2)
1fe90 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1fe91 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1fe92 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 72  And, r3, r4, tar
1fe93 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  get);.      sqli
1fe94 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
1fe95 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a 20  g(pParse, r3);. 
1fe96 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
1fe97 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
1fe98 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62 72  e, r4);.      br
1fe99 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1fe9a 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a  ase TK_UPLUS: {.
1fe9b 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71        inReg = sq
1fe9c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
1fe9d 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  get(pParse, pExp
1fe9e 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74  r->pLeft, target
1fe9f 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1fea0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20      }..    /*.  
1fea1 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 20    ** Form A:.   
1fea2 20 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48 45   **   CASE x WHE
1fea3 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 45  N e1 THEN r1 WHE
1fea4 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e  N e2 THEN r2 ...
1fea5 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e   WHEN eN THEN rN
1fea6 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20   ELSE y END.    
1fea7 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 42  **.    ** Form B
1fea8 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20  :.    **   CASE 
1fea9 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20  WHEN e1 THEN r1 
1feaa 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20  WHEN e2 THEN r2 
1feab 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e  ... WHEN eN THEN
1feac 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20   rN ELSE y END. 
1fead 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72     **.    ** For
1feae 6d 20 41 20 69 73 20 63 61 6e 20 62 65 20 74 72  m A is can be tr
1feaf 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74  ansformed into t
1feb0 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f  he equivalent fo
1feb1 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  rm B as follows:
1feb2 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57  .    **   CASE W
1feb3 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31  HEN x=e1 THEN r1
1feb4 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20   WHEN x=e2 THEN 
1feb5 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20  r2 ....    **   
1feb6 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 54       WHEN x=eN T
1feb7 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e  HEN rN ELSE y EN
1feb8 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  D.    **.    ** 
1feb9 58 20 28 69 66 20 69 74 20 65 78 69 73 74 73 29  X (if it exists)
1feba 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c   is in pExpr->pL
1febb 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73  eft..    ** Y is
1febc 20 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 68   in pExpr->pRigh
1febd 74 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c 73  t.  The Y is als
1febe 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20  o optional.  If 
1febf 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20  there is no.    
1fec0 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20 61  ** ELSE clause a
1fec1 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 6d  nd no other term
1fec2 20 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20 74   matches, then t
1fec3 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1fec4 0a 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69 6f  .    ** exprssio
1fec5 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a  n is NULL..    *
1fec6 2a 20 45 69 20 69 73 20 69 6e 20 70 45 78 70 72  * Ei is in pExpr
1fec7 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d 20  ->pList->a[i*2] 
1fec8 61 6e 64 20 52 69 20 69 73 20 70 45 78 70 72 2d  and Ri is pExpr-
1fec9 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 5d  >pList->a[i*2+1]
1feca 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1fecb 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  The result of th
1fecc 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
1fecd 74 68 65 20 52 69 20 66 6f 72 20 74 68 65 20 66  the Ri for the f
1fece 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45 69  irst matching Ei
1fecf 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74  ,.    ** or if t
1fed0 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
1fed1 69 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53 45  ing Ei, the ELSE
1fed2 20 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 74   term Y, or if t
1fed3 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e  here is.    ** n
1fed4 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 4c  o ELSE term, NUL
1fed5 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  L..    */.    ca
1fed6 73 65 20 54 4b 5f 43 41 53 45 3a 20 7b 0a 20 20  se TK_CASE: {.  
1fed7 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c      int endLabel
1fed8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1fed9 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61        /* GOTO la
1feda 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20 43  bel for end of C
1fedb 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 20 20  ASE stmt */.    
1fedc 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b 20    int nextCase; 
1fedd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fede 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65      /* GOTO labe
1fedf 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e 20  l for next WHEN 
1fee0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
1fee1 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20  int nExpr;      
1fee2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fee3 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 20 6f    /* 2x number o
1fee4 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a  f WHEN terms */.
1fee5 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20        int i;    
1fee6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fee7 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1fee8 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20  counter */.     
1fee9 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
1feea 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
1feeb 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 57 48     /* List of WH
1feec 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20  EN terms */.    
1feed 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1feee 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 6c 65  t_item *aListele
1feef 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20  m;  /* Array of 
1fef0 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20  WHEN terms */.  
1fef1 20 20 20 20 45 78 70 72 20 6f 70 43 6f 6d 70 61      Expr opCompa
1fef2 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  re;             
1fef3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 3d 3d        /* The X==
1fef4 45 69 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  Ei expression */
1fef5 0a 20 20 20 20 20 20 45 78 70 72 20 63 61 63 68  .      Expr cach
1fef6 65 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  eX;             
1fef7 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68           /* Cach
1fef8 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 58 20  ed expression X 
1fef9 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  */.      Expr *p
1fefa 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  X;              
1fefb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1fefc 65 20 58 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  e X expression *
1fefd 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 54  /.      Expr *pT
1fefe 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  est = 0;        
1feff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 3d 3d            /* X==
1ff00 45 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 20 6a  Ei (form A) or j
1ff01 75 73 74 20 45 69 20 28 66 6f 72 6d 20 42 29 20  ust Ei (form B) 
1ff02 2a 2f 0a 20 20 20 20 20 20 56 56 41 5f 4f 4e 4c  */.      VVA_ONL
1ff03 59 28 20 69 6e 74 20 69 43 61 63 68 65 4c 65 76  Y( int iCacheLev
1ff04 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61  el = pParse->iCa
1ff05 63 68 65 4c 65 76 65 6c 3b 20 29 0a 0a 20 20 20  cheLevel; )..   
1ff06 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
1ff07 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
1ff08 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
1ff09 20 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69   && pExpr->x.pLi
1ff0a 73 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  st );.      asse
1ff0b 72 74 28 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69  rt((pExpr->x.pLi
1ff0c 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29 20 3d  st->nExpr % 2) =
1ff0d 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65  = 0);.      asse
1ff0e 72 74 28 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  rt(pExpr->x.pLis
1ff0f 74 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b 0a 20  t->nExpr > 0);. 
1ff10 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 45       pEList = pE
1ff11 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20  xpr->x.pList;.  
1ff12 20 20 20 20 61 4c 69 73 74 65 6c 65 6d 20 3d 20      aListelem = 
1ff13 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20  pEList->a;.     
1ff14 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d   nExpr = pEList-
1ff15 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 65 6e  >nExpr;.      en
1ff16 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33  dLabel = sqlite3
1ff17 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
1ff18 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 58 20  ;.      if( (pX 
1ff19 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 21  = pExpr->pLeft)!
1ff1a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61  =0 ){.        ca
1ff1b 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20 20 20 20  cheX = *pX;.    
1ff1c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 58      testcase( pX
1ff1d 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
1ff1e 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45  || pX->op==TK_RE
1ff1f 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 20  GISTER );.      
1ff20 20 20 63 61 63 68 65 58 2e 69 54 61 62 6c 65 20    cacheX.iTable 
1ff21 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
1ff22 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 58  eTemp(pParse, pX
1ff23 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20  , &regFree1);.  
1ff24 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ff25 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
1ff26 20 20 20 20 20 20 20 63 61 63 68 65 58 2e 6f 70         cacheX.op
1ff27 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a   = TK_REGISTER;.
1ff28 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72          opCompar
1ff29 65 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20  e.op = TK_EQ;.  
1ff2a 20 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e        opCompare.
1ff2b 70 4c 65 66 74 20 3d 20 26 63 61 63 68 65 58 3b  pLeft = &cacheX;
1ff2c 0a 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d  .        pTest =
1ff2d 20 26 6f 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20   &opCompare;.   
1ff2e 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69     }.      for(i
1ff2f 3d 30 3b 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69  =0; i<nExpr; i=i
1ff30 2b 32 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  +2){.        sql
1ff31 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73  ite3ExprCachePus
1ff32 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  h(pParse);.     
1ff33 20 20 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20     if( pX ){.   
1ff34 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1ff35 54 65 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 20  Test!=0 );.     
1ff36 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70       opCompare.p
1ff37 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65  Right = aListele
1ff38 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  m[i].pExpr;.    
1ff39 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ff3a 20 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69       pTest = aLi
1ff3b 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b  stelem[i].pExpr;
1ff3c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1ff3d 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71     nextCase = sq
1ff3e 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1ff3f 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74  el(v);.        t
1ff40 65 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e  estcase( pTest->
1ff41 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
1ff42 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 52   pTest->op==TK_R
1ff43 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20  EGISTER );.     
1ff44 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
1ff45 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54  False(pParse, pT
1ff46 65 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53  est, nextCase, S
1ff47 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1ff48 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
1ff49 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69  ase( aListelem[i
1ff4a 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  +1].pExpr->op==T
1ff4b 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  K_COLUMN );.    
1ff4c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c      testcase( aL
1ff4d 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78  istelem[i+1].pEx
1ff4e 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  pr->op==TK_REGIS
1ff4f 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73  TER );.        s
1ff50 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1ff51 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d  Parse, aListelem
1ff52 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72  [i+1].pExpr, tar
1ff53 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  get);.        sq
1ff54 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1ff55 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65  v, OP_Goto, 0, e
1ff56 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20  ndLabel);.      
1ff57 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
1ff58 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
1ff59 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1ff5a 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1ff5b 6c 28 76 2c 20 6e 65 78 74 43 61 73 65 29 3b 0a  l(v, nextCase);.
1ff5c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1ff5d 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20  ( pExpr->pRight 
1ff5e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1ff5f 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28  e3ExprCachePush(
1ff60 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20  pParse);.       
1ff61 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1ff62 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
1ff63 70 52 69 67 68 74 2c 20 74 61 72 67 65 74 29 3b  pRight, target);
1ff64 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ff65 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61  ExprCachePop(pPa
1ff66 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d  rse, 1);.      }
1ff67 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
1ff68 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1ff69 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74  v, OP_Null, 0, t
1ff6a 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a  arget);.      }.
1ff6b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62        assert( db
1ff6c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
1ff6d 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30  | pParse->nErr>0
1ff6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20   .           || 
1ff6f 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65  pParse->iCacheLe
1ff70 76 65 6c 3d 3d 69 43 61 63 68 65 4c 65 76 65 6c  vel==iCacheLevel
1ff71 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1ff72 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1ff73 6c 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a  l(v, endLabel);.
1ff74 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1ff75 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
1ff76 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
1ff77 20 20 20 63 61 73 65 20 54 4b 5f 52 41 49 53 45     case TK_RAISE
1ff78 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70  : {.      if( !p
1ff79 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
1ff7a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1ff7b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1ff7c 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  se,.            
1ff7d 20 20 20 20 20 20 20 20 20 20 20 22 52 41 49 53             "RAIS
1ff7e 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  E() may only be 
1ff7f 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 74 72  used within a tr
1ff80 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22 29 3b  igger-program");
1ff81 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1ff82 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
1ff83 20 69 66 28 20 70 45 78 70 72 2d 3e 61 66 66 69   if( pExpr->affi
1ff84 6e 69 74 79 21 3d 4f 45 5f 49 67 6e 6f 72 65 20  nity!=OE_Ignore 
1ff85 29 7b 0a 20 20 20 20 20 20 20 20 20 61 73 73 65  ){.         asse
1ff86 72 74 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e  rt( pExpr->affin
1ff87 69 74 79 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b  ity==OE_Rollback
1ff88 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
1ff89 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69       pExpr->affi
1ff8a 6e 69 74 79 20 3d 3d 20 4f 45 5f 41 62 6f 72 74  nity == OE_Abort
1ff8b 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
1ff8c 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69       pExpr->affi
1ff8d 6e 69 74 79 20 3d 3d 20 4f 45 5f 46 61 69 6c 20  nity == OE_Fail 
1ff8e 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  );.         sqli
1ff8f 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1ff90 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45   OP_Halt, SQLITE
1ff91 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 70 45 78  _CONSTRAINT, pEx
1ff92 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c 20 30 2c  pr->affinity, 0,
1ff93 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1ff94 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
1ff95 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20  pExpr->token.z, 
1ff96 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b  pExpr->token.n);
1ff97 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  .      } else {.
1ff98 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
1ff99 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
1ff9a 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72 65 20 29 3b   == OE_Ignore );
1ff9b 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
1ff9c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1ff9d 50 5f 43 6f 6e 74 65 78 74 50 6f 70 2c 20 30 2c  P_ContextPop, 0,
1ff9e 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71   0);.         sq
1ff9f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1ffa0 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70  v, OP_Goto, 0, p
1ffa1 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
1ffa2 2d 3e 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a 20  ->ignoreJump);. 
1ffa3 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
1ffa4 65 6e 74 28 28 76 2c 20 22 72 61 69 73 65 28 49  ent((v, "raise(I
1ffa5 47 4e 4f 52 45 29 22 29 29 3b 0a 20 20 20 20 20  GNORE)"));.     
1ffa6 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1ffa7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
1ffa8 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
1ffa9 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
1ffaa 20 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71   regFree1);.  sq
1ffab 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
1ffac 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46  Reg(pParse, regF
1ffad 72 65 65 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  ree2);.  return 
1ffae 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  inReg;.}../*.** 
1ffaf 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1ffb0 20 65 76 61 6c 75 61 74 65 20 61 6e 20 65 78 70   evaluate an exp
1ffb1 72 65 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f 72  ression and stor
1ffb2 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a  e the results.**
1ffb3 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72   into a register
1ffb4 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65  .  Return the re
1ffb5 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 68  gister number wh
1ffb6 65 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a  ere the results.
1ffb7 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a  ** are stored..*
1ffb8 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 67 69  *.** If the regi
1ffb9 73 74 65 72 20 69 73 20 61 20 74 65 6d 70 6f 72  ster is a tempor
1ffba 61 72 79 20 72 65 67 69 73 74 65 72 20 74 68 61  ary register tha
1ffbb 74 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63  t can be dealloc
1ffbc 61 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77 72  ated,.** then wr
1ffbd 69 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20 69  ite its number i
1ffbe 6e 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20 74  nto *pReg.  If t
1ffbf 68 65 20 72 65 73 75 6c 74 20 72 65 67 69 73 74  he result regist
1ffc0 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 74  er is not.** a t
1ffc1 65 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20 73  emporary, then s
1ffc2 65 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72 6f  et *pReg to zero
1ffc3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1ffc4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
1ffc5 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73  xprCodeTemp(Pars
1ffc6 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1ffc7 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 52 65  *pExpr, int *pRe
1ffc8 67 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73  g){.  int r1 = s
1ffc9 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
1ffca 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20  (pParse);.  int 
1ffcb 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r2 = sqlite3Expr
1ffcc 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
1ffcd 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a 20  e, pExpr, r1);. 
1ffce 20 69 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a 20   if( r2==r1 ){. 
1ffcf 20 20 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a 20     *pReg = r1;. 
1ffd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
1ffd1 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
1ffd2 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
1ffd3 20 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20 20     *pReg = 0;.  
1ffd4 7d 0a 20 20 72 65 74 75 72 6e 20 72 32 3b 0a 7d  }.  return r2;.}
1ffd5 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1ffd6 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
1ffd7 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73  evaluate express
1ffd8 69 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74  ion pExpr and st
1ffd9 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c  ore the.** resul
1ffda 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 74  ts in register t
1ffdb 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75  arget.  The resu
1ffdc 6c 74 73 20 61 72 65 20 67 75 61 72 61 6e 74 65  lts are guarante
1ffdd 65 64 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a 20  ed to appear.** 
1ffde 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67  in register targ
1ffdf 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  et..*/.SQLITE_PR
1ffe0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1ffe1 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65 20  3ExprCode(Parse 
1ffe2 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1ffe3 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74  Expr, int target
1ffe4 29 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b 0a  ){.  int inReg;.
1ffe5 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65  .  assert( targe
1ffe6 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d 70  t>0 && target<=p
1ffe7 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  Parse->nMem );. 
1ffe8 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
1ffe9 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
1ffea 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61  Parse, pExpr, ta
1ffeb 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74 28  rget);.  assert(
1ffec 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 7c   pParse->pVdbe |
1ffed 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
1ffee 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
1ffef 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67 65  if( inReg!=targe
1fff0 74 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56 64  t && pParse->pVd
1fff1 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  be ){.    sqlite
1fff2 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72  3VdbeAddOp2(pPar
1fff3 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53 43  se->pVdbe, OP_SC
1fff4 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67  opy, inReg, targ
1fff5 65 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  et);.  }.  retur
1fff6 6e 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a 0a  n target;.}../*.
1fff7 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1fff8 20 74 68 61 74 20 65 76 61 6c 75 74 65 73 20 74   that evalutes t
1fff9 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
1fffa 69 6f 6e 20 61 6e 64 20 70 75 74 73 20 74 68 65  ion and puts the
1fffb 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72 65   result.** in re
1fffc 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a  gister target..*
1fffd 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20 61  *.** Also make a
1fffe 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70   copy of the exp
1ffff 72 65 73 73 69 6f 6e 20 72 65 73 75 6c 74 73 20  ression results 
20000 69 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22 63 61  into another "ca
20001 63 68 65 22 20 72 65 67 69 73 74 65 72 0a 2a 2a  che" register.**
20002 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20   and modify the 
20003 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68  expression so th
20004 61 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65  at the next time
20005 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64   it is evaluated
20006 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20  ,.** the result 
20007 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  is a copy of the
20008 20 63 61 63 68 65 20 72 65 67 69 73 74 65 72 2e   cache register.
20009 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2000a 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ine is used for 
2000b 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74  expressions that
2000c 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69 70   are used multip
2000d 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20 54  le .** times.  T
2000e 68 65 79 20 61 72 65 20 65 76 61 6c 75 61 74 65  hey are evaluate
2000f 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 20 72  d once and the r
20010 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 65 78  esults of the ex
20011 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65 20  pression.** are 
20012 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  reused..*/.SQLIT
20013 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20014 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e 64  lite3ExprCodeAnd
20015 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61  Cache(Parse *pPa
20016 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
20017 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20  , int target){. 
20018 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
20019 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
2001a 69 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67 20 3d  inReg;.  inReg =
2001b 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2001c 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
2001d 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72  target);.  asser
2001e 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20  t( target>0 );. 
2001f 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
20020 54 4b 5f 52 45 47 49 53 54 45 52 20 29 7b 20 20  TK_REGISTER ){  
20021 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 20  .    int iMem;. 
20022 20 20 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72     iMem = ++pPar
20023 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
20024 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
20025 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 65  v, OP_Copy, inRe
20026 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 45  g, iMem);.    pE
20027 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 4d  xpr->iTable = iM
20028 65 6d 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f  em;.    pExpr->o
20029 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  p = TK_REGISTER;
2002a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e  .  }.  return in
2002b 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  Reg;.}../*.** Re
2002c 74 75 72 6e 20 54 52 55 45 20 69 66 20 70 45 78  turn TRUE if pEx
2002d 70 72 20 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e  pr is an constan
2002e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  t expression tha
2002f 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  t is appropriate
20030 0a 2a 2a 20 66 6f 72 20 66 61 63 74 6f 72 69 6e  .** for factorin
20031 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e  g out of a loop.
20032 20 20 41 70 70 72 6f 70 72 69 61 74 65 20 65 78    Appropriate ex
20033 70 72 65 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a  pressions are:.*
20034 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 65  *.**    *  Any e
20035 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65  xpression that e
20036 76 61 6c 75 61 74 65 73 20 74 6f 20 74 77 6f 20  valuates to two 
20037 6f 72 20 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e  or more opcodes.
20038 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79  .**.**    *  Any
20039 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f   OP_Integer, OP_
2003a 52 65 61 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c  Real, OP_String,
2003b 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c   OP_Blob, OP_Nul
2003c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20  l, .**       or 
2003d 4f 50 5f 56 61 72 69 61 62 6c 65 20 74 68 61 74  OP_Variable that
2003e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
2003f 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61  o be placed in a
20040 20 0a 2a 2a 20 20 20 20 20 20 20 73 70 65 63 69   .**       speci
20041 66 69 63 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a  fic register..**
20042 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20  .** There is no 
20043 70 6f 69 6e 74 20 69 6e 20 66 61 63 74 6f 72 69  point in factori
20044 6e 67 20 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e  ng out single-in
20045 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61  struction consta
20046 6e 74 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  nt.** expression
20047 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62  s that need to b
20048 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 70 61  e placed in a pa
20049 72 74 69 63 75 6c 61 72 20 72 65 67 69 73 74 65  rticular registe
2004a 72 2e 20 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64  r.  .** We could
2004b 20 66 61 63 74 6f 72 20 74 68 65 6d 20 6f 75 74   factor them out
2004c 2c 20 62 75 74 20 74 68 65 6e 20 77 65 20 77 6f  , but then we wo
2004d 75 6c 64 20 65 6e 64 20 75 70 20 61 64 64 69 6e  uld end up addin
2004e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79  g an.** OP_SCopy
2004f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20   instruction to 
20050 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 69  move the value i
20051 6e 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20  nto the correct 
20052 72 65 67 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65  register.** late
20053 72 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 20  r.  We might as 
20054 77 65 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68  well just use th
20055 65 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72  e original instr
20056 75 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76  uction and.** av
20057 6f 69 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79  oid the OP_SCopy
20058 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
20059 69 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72  isAppropriateFor
2005a 46 61 63 74 6f 72 69 6e 67 28 45 78 70 72 20 2a  Factoring(Expr *
2005b 70 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74  p){.  if( !sqlit
2005c 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
2005d 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20  NotJoin(p) ){.  
2005e 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20    return 0;  /* 
2005f 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e 74 20 65 78  Only constant ex
20060 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20 61 70  pressions are ap
20061 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 66 61  propriate for fa
20062 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20  ctoring */.  }. 
20063 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26   if( (p->flags &
20064 20 45 50 5f 46 69 78 65 64 44 65 73 74 29 3d 3d   EP_FixedDest)==
20065 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
20066 31 3b 20 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74  1;  /* Any const
20067 61 6e 74 20 77 69 74 68 6f 75 74 20 61 20 66 69  ant without a fi
20068 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  xed destination 
20069 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a  is appropriate *
2006a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70  /.  }.  while( p
2006b 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29  ->op==TK_UPLUS )
2006c 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20   p = p->pLeft;. 
2006d 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
2006e 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
2006f 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52  _OMIT_BLOB_LITER
20070 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42  AL.    case TK_B
20071 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20  LOB:.#endif.    
20072 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45  case TK_VARIABLE
20073 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  :.    case TK_IN
20074 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20  TEGER:.    case 
20075 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61  TK_FLOAT:.    ca
20076 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20  se TK_NULL:.    
20077 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20  case TK_STRING: 
20078 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
20079 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42  ( p->op==TK_BLOB
2007a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2007b 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41  se( p->op==TK_VA
2007c 52 49 41 42 4c 45 20 29 3b 0a 20 20 20 20 20 20  RIABLE );.      
2007d 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d  testcase( p->op=
2007e 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20  =TK_INTEGER );. 
2007f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
20080 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29  ->op==TK_FLOAT )
20081 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
20082 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c  ( p->op==TK_NULL
20083 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
20084 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54  se( p->op==TK_ST
20085 52 49 4e 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a  RING );.      /*
20086 20 53 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74   Single-instruct
20087 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 73 20 77 69  ion constants wi
20088 74 68 20 61 20 66 69 78 65 64 20 64 65 73 74 69  th a fixed desti
20089 6e 61 74 69 6f 6e 20 61 72 65 0a 20 20 20 20 20  nation are.     
2008a 20 2a 2a 20 62 65 74 74 65 72 20 64 6f 6e 65 20   ** better done 
2008b 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 65 20  in-line.  If we 
2008c 66 61 63 74 6f 72 20 74 68 65 6d 2c 20 74 68 65  factor them, the
2008d 79 20 77 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a  y will just end.
2008e 20 20 20 20 20 20 2a 2a 20 75 70 20 67 65 6e 65        ** up gene
2008f 72 61 74 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f  rating an OP_SCo
20090 70 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76  py to move the v
20091 61 6c 75 65 20 74 6f 20 74 68 65 20 64 65 73 74  alue to the dest
20092 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  ination.      **
20093 20 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20   register. */.  
20094 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
20095 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
20096 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20  UMINUS: {.      
20097 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f   if( p->pLeft->o
20098 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70  p==TK_FLOAT || p
20099 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ->pLeft->op==TK_
2009a 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20  INTEGER ){.     
2009b 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2009c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 62 72       }.       br
2009d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
2009e 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
2009f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
200a0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
200a1 2f 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69  /*.** If pExpr i
200a2 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70  s a constant exp
200a3 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20  ression that is 
200a4 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a  appropriate for.
200a5 2a 2a 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74  ** factoring out
200a6 20 6f 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e   of a loop, then
200a7 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 65 78   evaluate the ex
200a8 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f  pression.** into
200a9 20 61 20 72 65 67 69 73 74 65 72 20 61 6e 64 20   a register and 
200aa 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72  convert the expr
200ab 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b  ession into a TK
200ac 5f 52 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70  _REGISTER.** exp
200ad 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  ression..*/.stat
200ae 69 63 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74  ic int evalConst
200af 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  Expr(Walker *pWa
200b0 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70  lker, Expr *pExp
200b1 72 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  r){.  Parse *pPa
200b2 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70  rse = pWalker->p
200b3 50 61 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28  Parse;.  switch(
200b4 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
200b5 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54    case TK_REGIST
200b6 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75  ER: {.      retu
200b7 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
200b8 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  case TK_FUNCTION
200b9 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  :.    case TK_AG
200ba 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20  G_FUNCTION:.    
200bb 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55  case TK_CONST_FU
200bc 4e 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54  NC: {.      /* T
200bd 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  he arguments to 
200be 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20  a function have 
200bf 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74  a fixed destinat
200c0 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 61  ion..      ** Ma
200c1 72 6b 20 74 68 65 6d 20 74 68 69 73 20 77 61 79  rk them this way
200c2 20 74 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 61   to avoid genera
200c3 74 65 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 5f  ted unneeded OP_
200c4 53 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 69  SCopy.      ** i
200c5 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 20  nstructions. .  
200c6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70      */.      Exp
200c7 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70  rList *pList = p
200c8 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20  Expr->x.pList;. 
200c9 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
200ca 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
200cb 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
200cc 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  t) );.      if( 
200cd 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20  pList ){.       
200ce 20 69 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d 3e   int i = pList->
200cf 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 73  nExpr;.        s
200d0 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
200d1 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 4c 69  tem *pItem = pLi
200d2 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66  st->a;.        f
200d3 6f 72 28 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70  or(; i>0; i--, p
200d4 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
200d5 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 45     if( pItem->pE
200d6 78 70 72 20 29 20 70 49 74 65 6d 2d 3e 70 45 78  xpr ) pItem->pEx
200d7 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  pr->flags |= EP_
200d8 46 69 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20  FixedDest;.     
200d9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
200da 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
200db 20 20 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72    }.  if( isAppr
200dc 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72  opriateForFactor
200dd 69 6e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20  ing(pExpr) ){.  
200de 20 20 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61    int r1 = ++pPa
200df 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69  rse->nMem;.    i
200e0 6e 74 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20  nt r2;.    r2 = 
200e1 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
200e2 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45  arget(pParse, pE
200e3 78 70 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66  xpr, r1);.    if
200e4 28 20 72 31 21 3d 72 32 20 29 20 73 71 6c 69 74  ( r1!=r2 ) sqlit
200e5 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
200e6 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
200e7 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
200e8 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 70  _REGISTER;.    p
200e9 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 72  Expr->iTable = r
200ea 32 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  2;.    return WR
200eb 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 72  C_Prune;.  }.  r
200ec 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
200ed 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65  ue;.}../*.** Pre
200ee 65 76 61 6c 75 61 74 65 20 63 6f 6e 73 74 61 6e  evaluate constan
200ef 74 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  t subexpressions
200f0 20 77 69 74 68 69 6e 20 70 45 78 70 72 20 61 6e   within pExpr an
200f1 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72  d store the.** r
200f2 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74  esults in regist
200f3 65 72 73 2e 20 20 4d 6f 64 69 66 79 20 70 45 78  ers.  Modify pEx
200f4 70 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  pr so that the c
200f5 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72 65  onstant subexpre
200f6 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 54 4b 5f  sions.** are TK_
200f7 52 45 47 49 53 54 45 52 20 6f 70 63 6f 64 65 73  REGISTER opcodes
200f8 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
200f9 68 65 20 70 72 65 63 6f 6d 70 75 74 65 64 20 76  he precomputed v
200fa 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  alues..*/.SQLITE
200fb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
200fc 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e  lite3ExprCodeCon
200fd 73 74 61 6e 74 73 28 50 61 72 73 65 20 2a 70 50  stants(Parse *pP
200fe 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
200ff 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a  r){.  Walker w;.
20100 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
20101 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78 70  k = evalConstExp
20102 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  r;.  w.xSelectCa
20103 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77 2e  llback = 0;.  w.
20104 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
20105 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  .  sqlite3WalkEx
20106 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d  pr(&w, pExpr);.}
20107 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  .../*.** Generat
20108 65 20 63 6f 64 65 20 74 68 61 74 20 70 75 73 68  e code that push
20109 65 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  es the value of 
2010a 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f 66  every element of
2010b 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78   the given.** ex
2010c 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 69 6e  pression list in
2010d 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  to a sequence of
2010e 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e   registers begin
2010f 6e 69 6e 67 20 61 74 20 74 61 72 67 65 74 2e 0a  ning at target..
20110 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  **.** Return the
20111 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   number of eleme
20112 6e 74 73 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a  nts evaluated..*
20113 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20114 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
20115 43 6f 64 65 45 78 70 72 4c 69 73 74 28 0a 20 20  CodeExprList(.  
20116 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
20117 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
20118 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
20119 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a  ist *pList,   /*
2011a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
2011b 6c 69 73 74 20 74 6f 20 62 65 20 63 6f 64 65 64  list to be coded
2011c 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72 67 65 74   */.  int target
2011d 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72  ,        /* Wher
2011e 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c  e to write resul
2011f 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48 61  ts */.  int doHa
20120 72 64 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d 61  rdCopy     /* Ma
20121 6b 65 20 61 20 68 61 72 64 20 63 6f 70 79 20 6f  ke a hard copy o
20122 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20  f every element 
20123 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 45  */.){.  struct E
20124 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
20125 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  tem;.  int i, n;
20126 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
20127 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
20128 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 6e   target>0 );.  n
20129 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
2012a 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69  .  for(pItem=pLi
2012b 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e 3b  st->a, i=0; i<n;
2012c 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
2012d 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
2012e 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 69  Alias ){.      i
2012f 6e 74 20 69 52 65 67 20 3d 20 63 6f 64 65 41 6c  nt iReg = codeAl
20130 69 61 73 28 70 50 61 72 73 65 2c 20 70 49 74 65  ias(pParse, pIte
20131 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49 74 65 6d  m->iAlias, pItem
20132 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 2b  ->pExpr, target+
20133 69 29 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a  i);.      Vdbe *
20134 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
20135 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
20136 20 20 69 66 28 20 69 52 65 67 21 3d 74 61 72 67    if( iReg!=targ
20137 65 74 2b 69 20 29 7b 0a 20 20 20 20 20 20 20 20  et+i ){.        
20138 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20139 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69  2(v, OP_SCopy, i
2013a 52 65 67 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a  Reg, target+i);.
2013b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2013c 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2013d 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2013e 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74   pItem->pExpr, t
2013f 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 7d 0a  arget+i);.    }.
20140 20 20 20 20 69 66 28 20 64 6f 48 61 72 64 43 6f      if( doHardCo
20141 70 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  py ){.      sqli
20142 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28  te3ExprHardCopy(
20143 70 50 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20  pParse, target, 
20144 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  n);.    }.  }.  
20145 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a  return n;.}../*.
20146 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
20147 20 66 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65   for a boolean e
20148 78 70 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74  xpression such t
20149 68 61 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61  hat a jump is ma
2014a 64 65 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62  de.** to the lab
2014b 65 6c 20 22 64 65 73 74 22 20 69 66 20 74 68 65  el "dest" if the
2014c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
2014d 72 75 65 20 62 75 74 20 65 78 65 63 75 74 69 6f  rue but executio
2014e 6e 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73  n.** continues s
2014f 74 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20  traight thru if 
20150 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
20151 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  s false..**.** I
20152 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
20153 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55   evaluates to NU
20154 4c 4c 20 28 6e 65 69 74 68 65 72 20 74 72 75 65  LL (neither true
20155 20 6e 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65   nor false), the
20156 6e 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75  n.** take the ju
20157 6d 70 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66  mp if the jumpIf
20158 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 53 51 4c  Null flag is SQL
20159 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a  ITE_JUMPIFNULL..
2015a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20  **.** This code 
2015b 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 66  depends on the f
2015c 61 63 74 20 74 68 61 74 20 63 65 72 74 61 69 6e  act that certain
2015d 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28 65   token values (e
2015e 78 3a 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65  x: TK_EQ).** are
2015f 20 74 68 65 20 73 61 6d 65 20 61 73 20 6f 70 63   the same as opc
20160 6f 64 65 20 76 61 6c 75 65 73 20 28 65 78 3a 20  ode values (ex: 
20161 4f 50 5f 45 71 29 20 74 68 61 74 20 69 6d 70 6c  OP_Eq) that impl
20162 65 6d 65 6e 74 20 74 68 65 20 63 6f 72 72 65 73  ement the corres
20163 70 6f 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61  ponding.** opera
20164 74 69 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20 63  tion.  Special c
20165 6f 6d 6d 65 6e 74 73 20 69 6e 20 76 64 62 65 2e  omments in vdbe.
20166 63 20 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f  c and the mkopco
20167 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 69  deh.awk script i
20168 6e 0a 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70 72  n.** the make pr
20169 6f 63 65 73 73 20 63 61 75 73 65 20 74 68 65 73  ocess cause thes
2016a 65 20 76 61 6c 75 65 73 20 74 6f 20 61 6c 69 67  e values to alig
2016b 6e 2e 20 20 41 73 73 65 72 74 28 29 73 20 69 6e  n.  Assert()s in
2016c 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c   the code.** bel
2016d 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 74  ow verify that t
2016e 68 65 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61  he numbers are a
2016f 6c 69 67 6e 65 64 20 63 6f 72 72 65 63 74 6c 79  ligned correctly
20170 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20171 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20172 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73 65  ExprIfTrue(Parse
20173 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
20174 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c  pExpr, int dest,
20175 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29   int jumpIfNull)
20176 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
20177 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
20178 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74  nt op = 0;.  int
20179 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20   regFree1 = 0;. 
2017a 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20   int regFree2 = 
2017b 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b  0;.  int r1, r2;
2017c 0a 0a 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70  ..  assert( jump
2017d 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a  IfNull==SQLITE_J
2017e 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d  UMPIFNULL || jum
2017f 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
20180 69 66 28 20 76 3d 3d 30 20 7c 7c 20 70 45 78 70  if( v==0 || pExp
20181 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  r==0 ) return;. 
20182 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b   op = pExpr->op;
20183 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
20184 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44  .    case TK_AND
20185 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32  : {.      int d2
20186 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
20187 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
20188 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
20189 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
2018a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2018b 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b  chePush(pParse);
2018c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2018d 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
2018e 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
2018f 64 32 2c 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51  d2,jumpIfNull^SQ
20190 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
20191 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
20192 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
20193 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
20194 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
20195 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l);.      sqlite
20196 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
20197 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 20  l(v, d2);.      
20198 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
20199 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a  Pop(pParse, 1);.
2019a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2019b 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f   }.    case TK_O
2019c 52 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  R: {.      testc
2019d 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
2019e 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
2019f 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50  te3ExprIfTrue(pP
201a0 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
201a1 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ft, dest, jumpIf
201a2 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  Null);.      sql
201a3 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
201a4 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
201a5 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  ight, dest, jump
201a6 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62  IfNull);.      b
201a7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
201a8 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20  case TK_NOT: {. 
201a9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
201aa 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
201ab 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
201ac 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
201ad 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64   pExpr->pLeft, d
201ae 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
201af 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
201b0 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
201b1 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LT:.    case TK
201b2 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _LE:.    case TK
201b3 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GT:.    case TK
201b4 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _GE:.    case TK
201b5 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _NE:.    case TK
201b6 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  _EQ: {.      ass
201b7 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c  ert( TK_LT==OP_L
201b8 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
201b9 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20  t( TK_LE==OP_Le 
201ba 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
201bb 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b   TK_GT==OP_Gt );
201bc 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
201bd 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20  K_GE==OP_Ge );. 
201be 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
201bf 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20  EQ==OP_Eq );.   
201c0 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45     assert( TK_NE
201c1 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20  ==OP_Ne );.     
201c2 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
201c3 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65  K_LT );.      te
201c4 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c  stcase( op==TK_L
201c5 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
201c6 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29  ase( op==TK_GT )
201c7 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
201c8 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20  ( op==TK_GE );. 
201c9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
201ca 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20  p==TK_EQ );.    
201cb 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
201cc 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 74  TK_NE );.      t
201cd 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e  estcase( jumpIfN
201ce 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ull==0 );.      
201cf 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61  codeCompareOpera
201d0 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70  nds(pParse, pExp
201d1 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26  r->pLeft, &r1, &
201d2 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20  regFree1,.      
201d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
201d4 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
201d5 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20  r->pRight, &r2, 
201d6 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
201d7 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50    codeCompare(pP
201d8 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
201d9 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ft, pExpr->pRigh
201da 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20  t, op,.         
201db 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c           r1, r2,
201dc 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
201dd 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  l);.      testca
201de 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
201df 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
201e0 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29  e( regFree2==0 )
201e1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
201e2 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
201e3 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73  _ISNULL:.    cas
201e4 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a  e TK_NOTNULL: {.
201e5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
201e6 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75  _ISNULL==OP_IsNu
201e7 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ll );.      asse
201e8 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d  rt( TK_NOTNULL==
201e9 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20  OP_NotNull );.  
201ea 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
201eb 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  ==TK_ISNULL );. 
201ec 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
201ed 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b  p==TK_NOTNULL );
201ee 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
201ef 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
201f0 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
201f1 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29  Left, &regFree1)
201f2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
201f3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c  dbeAddOp2(v, op,
201f4 20 72 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20   r1, dest);.    
201f5 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
201f6 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree1==0 );.     
201f7 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
201f8 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45    case TK_BETWEE
201f9 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20  N: {.      /*   
201fa 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44   x BETWEEN y AND
201fb 20 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20   z.      **.    
201fc 20 20 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65    ** Is equivale
201fd 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 0a  nt to .      **.
201fe 20 20 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79        **    x>=y
201ff 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 20   AND x<=z.      
20200 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 65  **.      ** Code
20201 20 69 74 20 61 73 20 73 75 63 68 2c 20 74 61 6b   it as such, tak
20202 69 6e 67 20 63 61 72 65 20 74 6f 20 64 6f 20 74  ing care to do t
20203 68 65 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 70  he common subexp
20204 72 65 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  ression.      **
20205 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66   elementation of
20206 20 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20   x..      */.   
20207 20 20 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b     Expr exprAnd;
20208 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70  .      Expr comp
20209 4c 65 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72  Left;.      Expr
2020a 20 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20   compRight;.    
2020b 20 20 45 78 70 72 20 65 78 70 72 58 3b 0a 0a 20    Expr exprX;.. 
2020c 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
2020d 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2020e 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2020f 74 29 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72  t) );.      expr
20210 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66  X = *pExpr->pLef
20211 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64  t;.      exprAnd
20212 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20  .op = TK_AND;.  
20213 20 20 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66      exprAnd.pLef
20214 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20  t = &compLeft;. 
20215 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69       exprAnd.pRi
20216 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74  ght = &compRight
20217 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74  ;.      compLeft
20218 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20  .op = TK_GE;.   
20219 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66     compLeft.pLef
2021a 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20  t = &exprX;.    
2021b 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68    compLeft.pRigh
2021c 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2021d 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
2021e 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e        compRight.
2021f 6f 70 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20  op = TK_LE;.    
20220 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66    compRight.pLef
20221 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20  t = &exprX;.    
20222 20 20 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67    compRight.pRig
20223 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ht = pExpr->x.pL
20224 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b  ist->a[1].pExpr;
20225 0a 20 20 20 20 20 20 65 78 70 72 58 2e 69 54 61  .      exprX.iTa
20226 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ble = sqlite3Exp
20227 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
20228 2c 20 26 65 78 70 72 58 2c 20 26 72 65 67 46 72  , &exprX, &regFr
20229 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ee1);.      test
2022a 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
2022b 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 58  0 );.      exprX
2022c 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45  .op = TK_REGISTE
2022d 52 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  R;.      testcas
2022e 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
2022f 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
20230 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
20231 73 65 2c 20 26 65 78 70 72 41 6e 64 2c 20 64 65  se, &exprAnd, de
20232 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
20233 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
20234 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
20235 20 7b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71   {.      r1 = sq
20236 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
20237 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  p(pParse, pExpr,
20238 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20   &regFree1);.   
20239 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2023a 64 4f 70 33 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp3(v, OP_If, r
2023b 31 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  1, dest, jumpIfN
2023c 75 6c 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74  ull!=0);.      t
2023d 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
2023e 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
2023f 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
20240 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  ll==0 );.      b
20241 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
20242 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
20243 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
20244 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c  regFree1);.  sql
20245 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
20246 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72  eg(pParse, regFr
20247 65 65 32 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  ee2);  .}../*.**
20248 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
20249 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70  or a boolean exp
2024a 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61  ression such tha
2024b 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65  t a jump is made
2024c 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c  .** to the label
2024d 20 22 64 65 73 74 22 20 69 66 20 74 68 65 20 65   "dest" if the e
2024e 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c  xpression is fal
2024f 73 65 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e  se but execution
20250 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74  .** continues st
20251 72 61 69 67 68 74 20 74 68 72 75 20 69 66 20 74  raight thru if t
20252 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
20253 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   true..**.** If 
20254 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65  the expression e
20255 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c  valuates to NULL
20256 20 28 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e   (neither true n
20257 6f 72 20 66 61 6c 73 65 29 20 74 68 65 6e 0a 2a  or false) then.*
20258 2a 20 6a 75 6d 70 20 69 66 20 6a 75 6d 70 49 66  * jump if jumpIf
20259 4e 75 6c 6c 20 69 73 20 53 51 4c 49 54 45 5f 4a  Null is SQLITE_J
2025a 55 4d 50 49 46 4e 55 4c 4c 20 6f 72 20 66 61 6c  UMPIFNULL or fal
2025b 6c 20 74 68 72 6f 75 67 68 20 69 66 20 6a 75 6d  l through if jum
2025c 70 49 66 4e 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e  pIfNull.** is 0.
2025d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2025e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
2025f 78 70 72 49 66 46 61 6c 73 65 28 50 61 72 73 65  xprIfFalse(Parse
20260 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
20261 70 45 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c  pExpr, int dest,
20262 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29   int jumpIfNull)
20263 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
20264 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
20265 6e 74 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74  nt op = 0;.  int
20266 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20   regFree1 = 0;. 
20267 20 69 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20   int regFree2 = 
20268 30 3b 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b  0;.  int r1, r2;
20269 0a 0a 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70  ..  assert( jump
2026a 49 66 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a  IfNull==SQLITE_J
2026b 55 4d 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d  UMPIFNULL || jum
2026c 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
2026d 69 66 28 20 76 3d 3d 30 20 7c 7c 20 70 45 78 70  if( v==0 || pExp
2026e 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a  r==0 ) return;..
2026f 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f    /* The value o
20270 66 20 70 45 78 70 72 2d 3e 6f 70 20 61 6e 64 20  f pExpr->op and 
20271 6f 70 20 61 72 65 20 72 65 6c 61 74 65 64 20 61  op are related a
20272 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a  s follows:.  **.
20273 20 20 2a 2a 20 20 20 20 20 20 20 70 45 78 70 72    **       pExpr
20274 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 20  ->op            
20275 6f 70 0a 20 20 2a 2a 20 20 20 20 20 20 20 2d 2d  op.  **       --
20276 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 20 20 20 20  -------         
20277 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a   ----------.  **
20278 20 20 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c         TK_ISNULL
20279 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 6f 74            OP_Not
2027a 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20  Null.  **       
2027b 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20  TK_NOTNULL      
2027c 20 20 20 4f 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a     OP_IsNull.  *
2027d 2a 20 20 20 20 20 20 20 54 4b 5f 4e 45 20 20 20  *       TK_NE   
2027e 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 45 71             OP_Eq
2027f 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 45  .  **       TK_E
20280 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  Q              O
20281 50 5f 4e 65 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Ne.  **       
20282 54 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20  TK_GT           
20283 20 20 20 4f 50 5f 4c 65 0a 20 20 2a 2a 20 20 20     OP_Le.  **   
20284 20 20 20 20 54 4b 5f 4c 45 20 20 20 20 20 20 20      TK_LE       
20285 20 20 20 20 20 20 20 4f 50 5f 47 74 0a 20 20 2a         OP_Gt.  *
20286 2a 20 20 20 20 20 20 20 54 4b 5f 47 45 20 20 20  *       TK_GE   
20287 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 74             OP_Lt
20288 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c  .  **       TK_L
20289 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  T              O
2028a 50 5f 47 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46  P_Ge.  **.  ** F
2028b 6f 72 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20  or other values 
2028c 6f 66 20 70 45 78 70 72 2d 3e 6f 70 2c 20 6f 70  of pExpr->op, op
2028d 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 6e   is undefined an
2028e 64 20 75 6e 75 73 65 64 2e 0a 20 20 2a 2a 20 54  d unused..  ** T
2028f 68 65 20 76 61 6c 75 65 20 6f 66 20 54 4b 5f 20  he value of TK_ 
20290 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74  and OP_ constant
20291 73 20 61 72 65 20 61 72 72 61 6e 67 65 64 20 73  s are arranged s
20292 75 63 68 20 74 68 61 74 20 77 65 0a 20 20 2a 2a  uch that we.  **
20293 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65   can compute the
20294 20 6d 61 70 70 69 6e 67 20 61 62 6f 76 65 20 75   mapping above u
20295 73 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69  sing the followi
20296 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20  ng expression.. 
20297 20 2a 2a 20 41 73 73 65 72 74 28 29 73 20 76 65   ** Assert()s ve
20298 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63 6f  rify that the co
20299 6d 70 75 74 61 74 69 6f 6e 20 69 73 20 63 6f 72  mputation is cor
2029a 72 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20  rect..  */.  op 
2029b 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70 2b 28 54  = ((pExpr->op+(T
2029c 4b 5f 49 53 4e 55 4c 4c 26 31 29 29 5e 31 29 2d  K_ISNULL&1))^1)-
2029d 28 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a  (TK_ISNULL&1);..
2029e 20 20 2f 2a 20 56 65 72 69 66 79 20 63 6f 72 72    /* Verify corr
2029f 65 63 74 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66  ect alignment of
202a0 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e   TK_ and OP_ con
202a1 73 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73  stants.  */.  as
202a2 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
202a3 3d 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70  =TK_ISNULL || op
202a4 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a  ==OP_NotNull );.
202a5 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
202a6 3e 6f 70 21 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20  >op!=TK_NOTNULL 
202a7 7c 7c 20 6f 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c  || op==OP_IsNull
202a8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
202a9 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c  xpr->op!=TK_NE |
202aa 7c 20 6f 70 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20  | op==OP_Eq );. 
202ab 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
202ac 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d  op!=TK_EQ || op=
202ad 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 61 73 73 65  =OP_Ne );.  asse
202ae 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
202af 4b 5f 4c 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47  K_LT || op==OP_G
202b0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
202b1 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20  Expr->op!=TK_LE 
202b2 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a  || op==OP_Gt );.
202b3 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
202b4 3e 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70  >op!=TK_GT || op
202b5 3d 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 61 73 73  ==OP_Le );.  ass
202b6 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
202b7 54 4b 5f 47 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TK_GE || op==OP_
202b8 4c 74 20 29 3b 0a 0a 20 20 73 77 69 74 63 68 28  Lt );..  switch(
202b9 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
202ba 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b    case TK_AND: {
202bb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
202bc 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
202bd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
202be 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
202bf 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
202c0 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
202c1 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l);.      sqlite
202c2 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
202c3 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
202c4 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ht, dest, jumpIf
202c5 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Null);.      bre
202c6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
202c7 73 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20  se TK_OR: {.    
202c8 20 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74    int d2 = sqlit
202c9 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
202ca 76 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  v);.      testca
202cb 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
202cc 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
202cd 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28  e3ExprCachePush(
202ce 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
202cf 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65  qlite3ExprIfTrue
202d0 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
202d1 70 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70 49  pLeft, d2, jumpI
202d2 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d  fNull^SQLITE_JUM
202d3 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20  PIFNULL);.      
202d4 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c  sqlite3ExprIfFal
202d5 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  se(pParse, pExpr
202d6 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20  ->pRight, dest, 
202d7 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
202d8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
202d9 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32  solveLabel(v, d2
202da 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
202db 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61  ExprCachePop(pPa
202dc 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 62  rse, 1);.      b
202dd 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
202de 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20  case TK_NOT: {. 
202df 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
202e0 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70  IfTrue(pParse, p
202e1 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73  Expr->pLeft, des
202e2 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
202e3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
202e4 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c   }.    case TK_L
202e5 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c  T:.    case TK_L
202e6 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  E:.    case TK_G
202e7 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  T:.    case TK_G
202e8 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  E:.    case TK_N
202e9 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45  E:.    case TK_E
202ea 51 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  Q: {.      testc
202eb 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29  ase( op==TK_LT )
202ec 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
202ed 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20  ( op==TK_LE );. 
202ee 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
202ef 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20  p==TK_GT );.    
202f0 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
202f1 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74  TK_GE );.      t
202f2 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
202f3 45 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  EQ );.      test
202f4 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20  case( op==TK_NE 
202f5 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
202f6 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
202f7 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f   );.      codeCo
202f8 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50  mpareOperands(pP
202f9 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
202fa 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65  ft, &r1, &regFre
202fb 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  e1,.            
202fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
202fd 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69        pExpr->pRi
202fe 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72  ght, &r2, &regFr
202ff 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  ee2);.      code
20300 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
20301 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45  pExpr->pLeft, pE
20302 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c  xpr->pRight, op,
20303 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
20304 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c     r1, r2, dest,
20305 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
20306 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
20307 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
20308 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
20309 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20  Free2==0 );.    
2030a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2030b 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c     case TK_ISNUL
2030c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  L:.    case TK_N
2030d 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  OTNULL: {.      
2030e 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
2030f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  _ISNULL );.     
20310 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
20311 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20  K_NOTNULL );.   
20312 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
20313 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
20314 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
20315 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20  , &regFree1);.  
20316 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20317 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c  ddOp2(v, op, r1,
20318 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65   dest);.      te
20319 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
2031a 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
2031b 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2031c 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b  se TK_BETWEEN: {
2031d 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42  .      /*    x B
2031e 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20  ETWEEN y AND z. 
2031f 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
20320 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   Is equivalent t
20321 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  o .      **.    
20322 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44    **    x>=y AND
20323 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20   x<=z.      **. 
20324 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20       ** Code it 
20325 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20  as such, taking 
20326 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63  care to do the c
20327 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73  ommon subexpress
20328 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65  ion.      ** ele
20329 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a  mentation of x..
2032a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45        */.      E
2032b 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20  xpr exprAnd;.   
2032c 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74     Expr compLeft
2032d 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d  ;.      Expr com
2032e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78  pRight;.      Ex
2032f 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20  pr exprX;..     
20330 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
20331 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
20332 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
20333 3b 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20  ;.      exprX = 
20334 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20  *pExpr->pLeft;. 
20335 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20       exprAnd.op 
20336 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20  = TK_AND;.      
20337 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20  exprAnd.pLeft = 
20338 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20  &compLeft;.     
20339 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20   exprAnd.pRight 
2033a 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20  = &compRight;.  
2033b 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20      compLeft.op 
2033c 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63  = TK_GE;.      c
2033d 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20  ompLeft.pLeft = 
2033e 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f  &exprX;.      co
2033f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20  mpLeft.pRight = 
20340 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
20341 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[0].pExpr;.    
20342 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d    compRight.op =
20343 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f   TK_LE;.      co
20344 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20  mpRight.pLeft = 
20345 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f  &exprX;.      co
20346 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d  mpRight.pRight =
20347 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d   pExpr->x.pList-
20348 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[1].pExpr;.   
20349 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20     exprX.iTable 
2034a 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
2034b 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65  eTemp(pParse, &e
2034c 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29  xprX, &regFree1)
2034d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2034e 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
2034f 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20  .      exprX.op 
20350 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20  = TK_REGISTER;. 
20351 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
20352 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
20353 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
20354 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
20355 20 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c   &exprAnd, dest,
20356 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
20357 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
20358 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
20359 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
2035a 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
2035b 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72  Parse, pExpr, &r
2035c 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
2035d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2035e 33 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72  3(v, OP_IfNot, r
2035f 31 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  1, dest, jumpIfN
20360 75 6c 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74  ull!=0);.      t
20361 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
20362 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
20363 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
20364 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  ll==0 );.      b
20365 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
20366 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
20367 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
20368 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c  regFree1);.  sql
20369 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2036a 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72  eg(pParse, regFr
2036b 65 65 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ee2);.}../*.** D
2036c 6f 20 61 20 64 65 65 70 20 63 6f 6d 70 61 72 69  o a deep compari
2036d 73 6f 6e 20 6f 66 20 74 77 6f 20 65 78 70 72 65  son of two expre
2036e 73 73 69 6f 6e 20 74 72 65 65 73 2e 20 20 52 65  ssion trees.  Re
2036f 74 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a  turn TRUE (non-z
20370 65 72 6f 29 0a 2a 2a 20 69 66 20 74 68 65 79 20  ero).** if they 
20371 61 72 65 20 69 64 65 6e 74 69 63 61 6c 20 61 6e  are identical an
20372 64 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 69  d return FALSE i
20373 66 20 74 68 65 79 20 64 69 66 66 65 72 20 69 6e  f they differ in
20374 20 61 6e 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20   any way..**.** 
20375 53 6f 6d 65 74 69 6d 65 73 20 74 68 69 73 20 72  Sometimes this r
20376 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75  outine will retu
20377 72 6e 20 46 41 4c 53 45 20 65 76 65 6e 20 69 66  rn FALSE even if
20378 20 74 68 65 20 74 77 6f 20 65 78 70 72 65 73 73   the two express
20379 69 6f 6e 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61  ions.** really a
2037a 72 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20  re equivalent.  
2037b 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f  If we cannot pro
2037c 76 65 20 74 68 61 74 20 74 68 65 20 65 78 70 72  ve that the expr
2037d 65 73 73 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69  essions are.** i
2037e 64 65 6e 74 69 63 61 6c 2c 20 77 65 20 72 65 74  dentical, we ret
2037f 75 72 6e 20 46 41 4c 53 45 20 6a 75 73 74 20 74  urn FALSE just t
20380 6f 20 62 65 20 73 61 66 65 2e 20 20 53 6f 20 69  o be safe.  So i
20381 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  f this routine.*
20382 2a 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 2c  * returns false,
20383 20 74 68 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74   then you do not
20384 20 72 65 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72   really know for
20385 20 63 65 72 74 61 69 6e 20 69 66 20 74 68 65 20   certain if the 
20386 74 77 6f 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  two.** expressio
20387 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e  ns are the same.
20388 20 20 42 75 74 20 69 66 20 79 6f 75 20 67 65 74    But if you get
20389 20 61 20 54 52 55 45 20 72 65 74 75 72 6e 2c 20   a TRUE return, 
2038a 74 68 65 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20  then you.** can 
2038b 62 65 20 73 75 72 65 20 74 68 65 20 65 78 70 72  be sure the expr
2038c 65 73 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20  essions are the 
2038d 73 61 6d 65 2e 20 20 49 6e 20 74 68 65 20 70 6c  same.  In the pl
2038e 61 63 65 73 20 77 68 65 72 65 0a 2a 2a 20 74 68  aces where.** th
2038f 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
20390 65 64 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20  ed, it does not 
20391 68 75 72 74 20 74 6f 20 67 65 74 20 61 6e 20 65  hurt to get an e
20392 78 74 72 61 20 46 41 4c 53 45 20 2d 20 74 68 61  xtra FALSE - tha
20393 74 0a 2a 2a 20 6a 75 73 74 20 6d 69 67 68 74 20  t.** just might 
20394 72 65 73 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73  result in some s
20395 6c 69 67 68 74 6c 79 20 73 6c 6f 77 65 72 20 63  lightly slower c
20396 6f 64 65 2e 20 20 42 75 74 20 72 65 74 75 72 6e  ode.  But return
20397 69 6e 67 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72  ing.** an incorr
20398 65 63 74 20 54 52 55 45 20 63 6f 75 6c 64 20 6c  ect TRUE could l
20399 65 61 64 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63  ead to a malfunc
2039a 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
2039b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2039c 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45  te3ExprCompare(E
2039d 78 70 72 20 2a 70 41 2c 20 45 78 70 72 20 2a 70  xpr *pA, Expr *p
2039e 42 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  B){.  int i;.  i
2039f 66 28 20 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20  f( pA==0||pB==0 
203a0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42  ){.    return pB
203a1 3d 3d 70 41 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ==pA;.  }.  if( 
203a2 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
203a3 70 41 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pA, EP_xIsSelect
203a4 29 20 7c 7c 20 45 78 70 72 48 61 73 50 72 6f 70  ) || ExprHasProp
203a5 65 72 74 79 28 70 42 2c 20 45 50 5f 78 49 73 53  erty(pB, EP_xIsS
203a6 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 72 65  elect) ){.    re
203a7 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
203a8 28 20 28 70 41 2d 3e 66 6c 61 67 73 20 26 20 45  ( (pA->flags & E
203a9 50 5f 44 69 73 74 69 6e 63 74 29 21 3d 28 70 42  P_Distinct)!=(pB
203aa 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73  ->flags & EP_Dis
203ab 74 69 6e 63 74 29 20 29 20 72 65 74 75 72 6e 20  tinct) ) return 
203ac 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e 6f 70 21  0;.  if( pA->op!
203ad 3d 70 42 2d 3e 6f 70 20 29 20 72 65 74 75 72 6e  =pB->op ) return
203ae 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74   0;.  if( !sqlit
203af 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 41  e3ExprCompare(pA
203b0 2d 3e 70 4c 65 66 74 2c 20 70 42 2d 3e 70 4c 65  ->pLeft, pB->pLe
203b1 66 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ft) ) return 0;.
203b2 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78    if( !sqlite3Ex
203b3 70 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 52  prCompare(pA->pR
203b4 69 67 68 74 2c 20 70 42 2d 3e 70 52 69 67 68 74  ight, pB->pRight
203b5 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  ) ) return 0;.. 
203b6 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74   if( pA->x.pList
203b7 20 26 26 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20   && pB->x.pList 
203b8 29 7b 0a 20 20 20 20 69 66 28 20 70 41 2d 3e 78  ){.    if( pA->x
203b9 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70  .pList->nExpr!=p
203ba 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70  B->x.pList->nExp
203bb 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  r ) return 0;.  
203bc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 2d    for(i=0; i<pA-
203bd 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  >x.pList->nExpr;
203be 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70   i++){.      Exp
203bf 72 20 2a 70 45 78 70 72 41 20 3d 20 70 41 2d 3e  r *pExprA = pA->
203c0 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  x.pList->a[i].pE
203c1 78 70 72 3b 0a 20 20 20 20 20 20 45 78 70 72 20  xpr;.      Expr 
203c2 2a 70 45 78 70 72 42 20 3d 20 70 42 2d 3e 78 2e  *pExprB = pB->x.
203c3 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
203c4 72 3b 0a 20 20 20 20 20 20 69 66 28 20 21 73 71  r;.      if( !sq
203c5 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
203c6 28 70 45 78 70 72 41 2c 20 70 45 78 70 72 42 29  (pExprA, pExprB)
203c7 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
203c8 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70   }.  }else if( p
203c9 41 2d 3e 78 2e 70 4c 69 73 74 20 7c 7c 20 70 42  A->x.pList || pB
203ca 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20  ->x.pList ){.   
203cb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
203cc 20 20 69 66 28 20 70 41 2d 3e 69 54 61 62 6c 65    if( pA->iTable
203cd 21 3d 70 42 2d 3e 69 54 61 62 6c 65 20 7c 7c 20  !=pB->iTable || 
203ce 70 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70 42 2d  pA->iColumn!=pB-
203cf 3e 69 43 6f 6c 75 6d 6e 20 29 20 72 65 74 75 72  >iColumn ) retur
203d0 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 2d 3e 6f  n 0;.  if( pA->o
203d1 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20  p!=TK_COLUMN && 
203d2 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b 0a 20  pA->token.z ){. 
203d3 20 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b 65 6e     if( pB->token
203d4 2e 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  .z==0 ) return 0
203d5 3b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 74 6f  ;.    if( pB->to
203d6 6b 65 6e 2e 6e 21 3d 70 41 2d 3e 74 6f 6b 65 6e  ken.n!=pA->token
203d7 2e 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  .n ) return 0;. 
203d8 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
203d9 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 70 41  rNICmp((char*)pA
203da 2d 3e 74 6f 6b 65 6e 2e 7a 2c 28 63 68 61 72 2a  ->token.z,(char*
203db 29 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 2c 70 42 2d  )pB->token.z,pB-
203dc 3e 74 6f 6b 65 6e 2e 6e 29 21 3d 30 20 29 7b 0a  >token.n)!=0 ){.
203dd 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
203de 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
203df 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn 1;.}.../*.** 
203e0 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e  Add a new elemen
203e1 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66  t to the pAggInf
203e2 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e  o->aCol[] array.
203e3 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64    Return the ind
203e4 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77  ex of.** the new
203e5 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72   element.  Retur
203e6 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  n a negative num
203e7 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61  ber if malloc fa
203e8 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ils..*/.static i
203e9 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c  nt addAggInfoCol
203ea 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  umn(sqlite3 *db,
203eb 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29   AggInfo *pInfo)
203ec 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e  {.  int i;.  pIn
203ed 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74  fo->aCol = sqlit
203ee 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28  e3ArrayAllocate(
203ef 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20  .       db,.    
203f0 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a     pInfo->aCol,.
203f1 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49         sizeof(pI
203f2 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20  nfo->aCol[0]),. 
203f3 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20        3,.       
203f4 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c  &pInfo->nColumn,
203f5 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e  .       &pInfo->
203f6 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20  nColumnAlloc,.  
203f7 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 72       &i.  );.  r
203f8 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a  eturn i;.}    ..
203f9 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20  /*.** Add a new 
203fa 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 70  element to the p
203fb 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d  AggInfo->aFunc[]
203fc 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e 20   array.  Return 
203fd 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20  the index of.** 
203fe 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e  the new element.
203ff 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61 74    Return a negat
20400 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d 61  ive number if ma
20401 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73  lloc fails..*/.s
20402 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 67 67  tatic int addAgg
20403 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74 65 33  InfoFunc(sqlite3
20404 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70   *db, AggInfo *p
20405 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  Info){.  int i;.
20406 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d    pInfo->aFunc =
20407 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c   sqlite3ArrayAll
20408 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20 64 62  ocate(.       db
20409 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66 6f 2d  , .       pInfo-
2040a 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 73  >aFunc,.       s
2040b 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 46 75  izeof(pInfo->aFu
2040c 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33  nc[0]),.       3
2040d 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d  ,.       &pInfo-
2040e 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 26  >nFunc,.       &
2040f 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f  pInfo->nFuncAllo
20410 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29  c,.       &i.  )
20411 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20  ;.  return i;.} 
20412 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20     ../*.** This 
20413 69 73 20 74 68 65 20 78 45 78 70 72 43 61 6c 6c  is the xExprCall
20414 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65 65 20  back for a tree 
20415 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73 20 75  walker.  It is u
20416 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d  sed to.** implem
20417 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 41  ent sqlite3ExprA
20418 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
20419 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33  ().  See sqlite3
2041a 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65  ExprAnalyzeAggre
2041b 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61 64 64  gates.** for add
2041c 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2041d 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
2041e 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67  nt analyzeAggreg
2041f 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  ate(Walker *pWal
20420 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72  ker, Expr *pExpr
20421 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61  ){.  int i;.  Na
20422 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d  meContext *pNC =
20423 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b   pWalker->u.pNC;
20424 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20425 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a   = pNC->pParse;.
20426 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c    SrcList *pSrcL
20427 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c  ist = pNC->pSrcL
20428 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f 20 2a  ist;.  AggInfo *
20429 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e  pAggInfo = pNC->
2042a 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73 77 69  pAggInfo;..  swi
2042b 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
2042c 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  {.    case TK_AG
2042d 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61  G_COLUMN:.    ca
2042e 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a  se TK_COLUMN: {.
2042f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
20430 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47  pExpr->op==TK_AG
20431 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  G_COLUMN );.    
20432 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
20433 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op==TK_COLUMN
20434 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43 68 65   );.      /* Che
20435 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
20436 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20 6f 6e   column is in on
20437 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20  e of the tables 
20438 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20 20 20  in the FROM.    
20439 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66 20 74    ** clause of t
2043a 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65  he aggregate que
2043b 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ry */.      if( 
2043c 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20 20  pSrcList ){.    
2043d 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
2043e 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2043f 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 0a 20 20   pSrcList->a;.  
20440 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
20441 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pSrcList->nSrc;
20442 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
20443 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74            struct
20444 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43   AggInfo_col *pC
20445 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ol;.          if
20446 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d  ( pExpr->iTable=
20447 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20  =pItem->iCursor 
20448 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  ){.            /
20449 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68  * If we reach th
2044a 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61  is point, it mea
2044b 6e 73 20 74 68 61 74 20 70 45 78 70 72 20 72 65  ns that pExpr re
2044c 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 0a  fers to a table.
2044d 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
2044e 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 46 52  hat is in the FR
2044f 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
20450 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79   aggregate query
20451 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  .  .            
20452 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  **.            *
20453 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 20  * Make an entry 
20454 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  for the column i
20455 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  n pAggInfo->aCol
20456 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20 20 20  [] if there.    
20457 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f          ** is no
20458 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65 72 65  t an entry there
20459 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 20 20   already..      
2045a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2045b 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20       int k;.    
2045c 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 70          pCol = p
2045d 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 0a 20  AggInfo->aCol;. 
2045e 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b             for(k
2045f 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f 2d 3e  =0; k<pAggInfo->
20460 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 70 43  nColumn; k++, pC
20461 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  ol++){.         
20462 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69       if( pCol->i
20463 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54  Table==pExpr->iT
20464 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20  able &&.        
20465 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
20466 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e  iColumn==pExpr->
20467 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  iColumn ){.     
20468 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
20469 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2046a 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  }.            }.
2046b 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
2046c 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e 43  (k>=pAggInfo->nC
2046d 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20 20 20  olumn).         
2046e 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64 64 41      && (k = addA
2046f 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70 50 61  ggInfoColumn(pPa
20470 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e 66  rse->db, pAggInf
20471 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20 20 20  o))>=0 .        
20472 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
20473 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 41 67       pCol = &pAg
20474 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b 0a  gInfo->aCol[k];.
20475 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
20476 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78 70 72  ol->pTab = pExpr
20477 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20  ->pTab;.        
20478 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54 61 62        pCol->iTab
20479 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62  le = pExpr->iTab
2047a 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  le;.            
2047b 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 20    pCol->iColumn 
2047c 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
2047d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2047e 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70  pCol->iMem = ++p
2047f 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
20480 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
20481 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d  >iSorterColumn =
20482 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20   -1;.           
20483 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 20 3d     pCol->pExpr =
20484 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20   pExpr;.        
20485 20 20 20 20 20 20 69 66 28 20 70 41 67 67 49 6e        if( pAggIn
20486 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a  fo->pGroupBy ){.
20487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20488 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20 20 20  int j, n;.      
20489 20 20 20 20 20 20 20 20 20 20 45 78 70 72 4c 69            ExprLi
2048a 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67 49 6e  st *pGB = pAggIn
2048b 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20  fo->pGroupBy;.  
2048c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
2048d 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
2048e 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47 42 2d  em *pTerm = pGB-
2048f 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  >a;.            
20490 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e 45 78      n = pGB->nEx
20491 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr;.            
20492 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e      for(j=0; j<n
20493 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; j++, pTerm++){
20494 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
20495 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 54     Expr *pE = pT
20496 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  erm->pExpr;.    
20497 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
20498 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  ( pE->op==TK_COL
20499 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61 62 6c  UMN && pE->iTabl
2049a 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  e==pExpr->iTable
2049b 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20   &&.            
2049c 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e 69 43            pE->iC
2049d 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69 43  olumn==pExpr->iC
2049e 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
2049f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
204a0 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
204a1 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20   = j;.          
204a2 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
204a3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
204a4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
204a5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
204a6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
204a7 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69       if( pCol->i
204a8 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 20 29  SorterColumn<0 )
204a9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
204aa 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
204ab 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e 66 6f  olumn = pAggInfo
204ac 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e  ->nSortingColumn
204ad 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
204ae 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
204af 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  }.            /*
204b0 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20 61 6e   There is now an
204b1 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78 70 72   entry for pExpr
204b2 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43   in pAggInfo->aC
204b3 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20 20 20  ol[] (either.   
204b4 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63 61           ** beca
204b5 75 73 65 20 69 74 20 77 61 73 20 74 68 65 72 65  use it was there
204b6 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63 61 75   before or becau
204b7 73 65 20 77 65 20 6a 75 73 74 20 63 72 65 61 74  se we just creat
204b8 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20 20 20  ed it)..        
204b9 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74 20 74      ** Convert t
204ba 68 65 20 70 45 78 70 72 20 74 6f 20 62 65 20 61  he pExpr to be a
204bb 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 72   TK_AGG_COLUMN r
204bc 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68 61 74  eferring to that
204bd 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
204be 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
204bf 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20 20 20   entry..        
204c0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
204c1 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e     pExpr->pAggIn
204c2 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20  fo = pAggInfo;. 
204c3 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
204c4 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 43 4f  ->op = TK_AGG_CO
204c5 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20 20 20  LUMN;.          
204c6 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20    pExpr->iAgg = 
204c7 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  k;.            b
204c8 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
204c9 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78 70 72  } /* endif pExpr
204ca 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d  ->iTable==pItem-
204cb 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  >iCursor */.    
204cc 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f      } /* end loo
204cd 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 20  p over pSrcList 
204ce 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  */.      }.     
204cf 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
204d0 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  e;.    }.    cas
204d1 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  e TK_AGG_FUNCTIO
204d2 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  N: {.      /* Th
204d3 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30  e pNC->nDepth==0
204d4 20 74 65 73 74 20 63 61 75 73 65 73 20 61 67 67   test causes agg
204d5 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
204d6 20 69 6e 20 73 75 62 71 75 65 72 69 65 73 0a 20   in subqueries. 
204d7 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 67       ** to be ig
204d8 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 69  nored */.      i
204d9 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d  f( pNC->nDepth==
204da 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
204db 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
204dc 70 45 78 70 72 20 69 73 20 61 20 64 75 70 6c 69  pExpr is a dupli
204dd 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65 72 20  cate of another 
204de 61 67 67 72 65 67 61 74 65 20 0a 20 20 20 20 20  aggregate .     
204df 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74     ** function t
204e0 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 20 69  hat is already i
204e1 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f 20 73  n the pAggInfo s
204e2 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 20 20  tructure.       
204e3 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74 72 75   */.        stru
204e4 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20  ct AggInfo_func 
204e5 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49 6e 66  *pItem = pAggInf
204e6 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20 20 20  o->aFunc;.      
204e7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67    for(i=0; i<pAg
204e8 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b  gInfo->nFunc; i+
204e9 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
204ea 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
204eb 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 49  e3ExprCompare(pI
204ec 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45 78 70  tem->pExpr, pExp
204ed 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  r) ){.          
204ee 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
204ef 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
204f0 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 70 41         if( i>=pA
204f1 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b  ggInfo->nFunc ){
204f2 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 45  .          /* pE
204f3 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61 6c 2e  xpr is original.
204f4 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 6e 74    Make a new ent
204f5 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  ry in pAggInfo->
204f6 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20 20 20  aFunc[].        
204f7 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75    */.          u
204f8 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72  8 enc = ENC(pPar
204f9 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 20  se->db);.       
204fa 20 20 20 69 20 3d 20 61 64 64 41 67 67 49 6e 66     i = addAggInf
204fb 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e 64 62  oFunc(pParse->db
204fc 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20  , pAggInfo);.   
204fd 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 30 20         if( i>=0 
204fe 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
204ff 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
20500 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
20501 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
20502 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65              pIte
20503 6d 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61  m = &pAggInfo->a
20504 46 75 6e 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20  Func[i];.       
20505 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70       pItem->pExp
20506 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20  r = pExpr;.     
20507 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d         pItem->iM
20508 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
20509 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Mem;.           
2050a 20 70 49 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20   pItem->pFunc = 
2050b 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
2050c 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a  ion(pParse->db,.
2050d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2050e 20 20 20 28 63 68 61 72 2a 29 70 45 78 70 72 2d     (char*)pExpr-
2050f 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72 2d  >token.z, pExpr-
20510 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20 20 20 20 20 20  >token.n,.      
20511 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
20512 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3f 20 70 45  pr->x.pList ? pE
20513 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45  xpr->x.pList->nE
20514 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c 20 30 29  xpr : 0, enc, 0)
20515 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
20516 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26  ( pExpr->flags &
20517 20 45 50 5f 44 69 73 74 69 6e 63 74 20 29 7b 0a   EP_Distinct ){.
20518 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
20519 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d  tem->iDistinct =
2051a 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
2051b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
2051c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
2051d 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69 6e    pItem->iDistin
2051e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20  ct = -1;.       
2051f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
20520 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
20521 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 70 45 78       /* Make pEx
20522 70 72 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  pr point to the 
20523 61 70 70 72 6f 70 72 69 61 74 65 20 70 41 67 67  appropriate pAgg
20524 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 65 6e  Info->aFunc[] en
20525 74 72 79 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  try.        */. 
20526 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41         pExpr->iA
20527 67 67 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20  gg = i;.        
20528 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20  pExpr->pAggInfo 
20529 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20  = pAggInfo;.    
2052a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
2052b 72 75 6e 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rune;.      }.  
2052c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2052d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
2052e 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61 6c  .static int anal
2052f 79 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e 53  yzeAggregatesInS
20530 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57  elect(Walker *pW
20531 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70  alker, Select *p
20532 53 65 6c 65 63 74 29 7b 0a 20 20 4e 61 6d 65 43  Select){.  NameC
20533 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 57  ontext *pNC = pW
20534 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20  alker->u.pNC;.  
20535 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d  if( pNC->nDepth=
20536 3d 30 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e 6e  =0 ){.    pNC->n
20537 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 73 71 6c  Depth++;.    sql
20538 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70  ite3WalkSelect(p
20539 57 61 6c 6b 65 72 2c 20 70 53 65 6c 65 63 74 29  Walker, pSelect)
2053a 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74  ;.    pNC->nDept
2053b 68 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  h--;.    return 
2053c 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 65 6c  WRC_Prune;.  }el
2053d 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57  se{.    return W
2053e 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d  RC_Continue;.  }
2053f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a  .}../*.** Analyz
20540 65 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72  e the given expr
20541 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67 20 66  ession looking f
20542 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  or aggregate fun
20543 63 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66 6f  ctions and.** fo
20544 72 20 76 61 72 69 61 62 6c 65 73 20 74 68 61 74  r variables that
20545 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64 65   need to be adde
20546 64 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 2d  d to the pParse-
20547 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 2e 0a 2a  >aAgg[] array..*
20548 2a 20 4d 61 6b 65 20 61 64 64 69 74 69 6f 6e 61  * Make additiona
20549 6c 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65  l entries to the
2054a 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20   pParse->aAgg[] 
2054b 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73 61  array as necessa
2054c 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ry..**.** This r
2054d 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f 6e  outine should on
2054e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74  ly be called aft
2054f 65 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  er the expressio
20550 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 6e  n has been.** an
20551 61 6c 79 7a 65 64 20 62 79 20 73 71 6c 69 74 65  alyzed by sqlite
20552 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
20553 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  s()..*/.SQLITE_P
20554 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20555 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
20556 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f 6e  gregates(NameCon
20557 74 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72 20  text *pNC, Expr 
20558 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65  *pExpr){.  Walke
20559 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  r w;.  w.xExprCa
2055a 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65  llback = analyze
2055b 41 67 67 72 65 67 61 74 65 3b 0a 20 20 77 2e 78  Aggregate;.  w.x
2055c 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d  SelectCallback =
2055d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74   analyzeAggregat
2055e 65 73 49 6e 53 65 6c 65 63 74 3b 0a 20 20 77 2e  esInSelect;.  w.
2055f 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73  u.pNC = pNC;.  s
20560 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26  qlite3WalkExpr(&
20561 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a  w, pExpr);.}../*
20562 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  .** Call sqlite3
20563 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65  ExprAnalyzeAggre
20564 67 61 74 65 73 28 29 20 66 6f 72 20 65 76 65 72  gates() for ever
20565 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  y expression in 
20566 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  an.** expression
20567 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 20 74   list.  Return t
20568 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  he number of err
20569 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ors..**.** If an
2056a 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e 64 2c   error is found,
2056b 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 69 73   the analysis is
2056c 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f 0a 53   cut short..*/.S
2056d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2056e 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e  id sqlite3ExprAn
2056f 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61 6d  alyzeAggList(Nam
20570 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45  eContext *pNC, E
20571 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  xprList *pList){
20572 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
20573 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
20574 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
20575 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28  List ){.    for(
20576 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20  pItem=pList->a, 
20577 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
20578 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
20579 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
2057a 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72  3ExprAnalyzeAggr
2057b 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49 74 65  egates(pNC, pIte
2057c 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  m->pExpr);.    }
2057d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  .  }.}../*.** Al
2057e 6c 6f 63 61 74 65 20 61 20 73 69 6e 67 6c 65 20  locate a single 
2057f 6e 65 77 20 72 65 67 69 73 74 65 72 20 66 6f 72  new register for
20580 20 75 73 65 20 74 6f 20 68 6f 6c 64 20 73 6f 6d   use to hold som
20581 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72  e intermediate r
20582 65 73 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  esult..*/.SQLITE
20583 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
20584 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50  ite3GetTempReg(P
20585 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
20586 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65   if( pParse->nTe
20587 6d 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20  mpReg==0 ){.    
20588 72 65 74 75 72 6e 20 2b 2b 70 50 61 72 73 65 2d  return ++pParse-
20589 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74  >nMem;.  }.  ret
2058a 75 72 6e 20 70 50 61 72 73 65 2d 3e 61 54 65 6d  urn pParse->aTem
2058b 70 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e  pReg[--pParse->n
2058c 54 65 6d 70 52 65 67 5d 3b 0a 7d 0a 0a 2f 2a 0a  TempReg];.}../*.
2058d 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 20  ** Deallocate a 
2058e 72 65 67 69 73 74 65 72 2c 20 6d 61 6b 69 6e 67  register, making
2058f 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72   available for r
20590 65 75 73 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74  euse for some ot
20591 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65 2e 0a  her.** purpose..
20592 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73  **.** If a regis
20593 74 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ter is currently
20594 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74   being used by t
20595 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2c  he column cache,
20596 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 6c   then.** the dal
20597 6c 6f 63 61 74 69 6f 6e 20 69 73 20 64 65 66 65  location is defe
20598 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65 20 63  rred until the c
20599 6f 6c 75 6d 6e 20 63 61 63 68 65 20 6c 69 6e 65  olumn cache line
2059a 20 74 68 61 74 20 75 73 65 73 0a 2a 2a 20 74 68   that uses.** th
2059b 65 20 72 65 67 69 73 74 65 72 20 62 65 63 6f 6d  e register becom
2059c 65 73 20 73 74 61 6c 65 2e 0a 2a 2f 0a 53 51 4c  es stale..*/.SQL
2059d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2059e 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2059f 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a 70 50  empReg(Parse *pP
205a0 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b  arse, int iReg){
205a1 0a 20 20 69 66 28 20 69 52 65 67 20 26 26 20 70  .  if( iReg && p
205a2 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c  Parse->nTempReg<
205a3 41 72 72 61 79 53 69 7a 65 28 70 50 61 72 73 65  ArraySize(pParse
205a4 2d 3e 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20  ->aTempReg) ){. 
205a5 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 74     int i;.    st
205a6 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a  ruct yColCache *
205a7 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  p;.    for(i=0, 
205a8 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
205a9 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
205aa 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
205ab 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
205ac 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a  ->iReg==iReg ){.
205ad 20 20 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52          p->tempR
205ae 65 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  eg = 1;.        
205af 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
205b0 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
205b1 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72 73  ->aTempReg[pPars
205b2 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d  e->nTempReg++] =
205b3 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a   iReg;.  }.}../*
205b4 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20  .** Allocate or 
205b5 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f  deallocate a blo
205b6 63 6b 20 6f 66 20 6e 52 65 67 20 63 6f 6e 73 65  ck of nReg conse
205b7 63 75 74 69 76 65 20 72 65 67 69 73 74 65 72 73  cutive registers
205b8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
205b9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
205ba 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65  tTempRange(Parse
205bb 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 52   *pParse, int nR
205bc 65 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  eg){.  int i, n;
205bd 0a 20 20 69 20 3d 20 70 50 61 72 73 65 2d 3e 69  .  i = pParse->i
205be 52 61 6e 67 65 52 65 67 3b 0a 20 20 6e 20 3d 20  RangeReg;.  n = 
205bf 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65  pParse->nRangeRe
205c0 67 3b 0a 20 20 69 66 28 20 6e 52 65 67 3c 3d 6e  g;.  if( nReg<=n
205c1 20 26 26 20 21 75 73 65 64 41 73 43 6f 6c 75 6d   && !usedAsColum
205c2 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69  nCache(pParse, i
205c3 2c 20 69 2b 6e 2d 31 29 20 29 7b 0a 20 20 20 20  , i+n-1) ){.    
205c4 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65  pParse->iRangeRe
205c5 67 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70  g += nReg;.    p
205c6 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67  Parse->nRangeReg
205c7 20 2d 3d 20 6e 52 65 67 3b 0a 20 20 7d 65 6c 73   -= nReg;.  }els
205c8 65 7b 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73  e{.    i = pPars
205c9 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70  e->nMem+1;.    p
205ca 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e  Parse->nMem += n
205cb 52 65 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Reg;.  }.  retur
205cc 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  n i;.}.SQLITE_PR
205cd 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
205ce 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
205cf 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ge(Parse *pParse
205d0 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20  , int iReg, int 
205d1 6e 52 65 67 29 7b 0a 20 20 69 66 28 20 6e 52 65  nReg){.  if( nRe
205d2 67 3e 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65  g>pParse->nRange
205d3 52 65 67 20 29 7b 0a 20 20 20 20 70 50 61 72 73  Reg ){.    pPars
205d4 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 3d 20 6e  e->nRangeReg = n
205d5 52 65 67 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  Reg;.    pParse-
205d6 3e 69 52 61 6e 67 65 52 65 67 20 3d 20 69 52 65  >iRangeReg = iRe
205d7 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  g;.  }.}../*****
205d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
205d9 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   expr.c ********
205da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205dc 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
205dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
205de 66 69 6c 65 20 61 6c 74 65 72 2e 63 20 2a 2a 2a  file alter.c ***
205df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205e1 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
205e2 32 30 30 35 20 46 65 62 72 75 61 72 79 20 31 35  2005 February 15
205e3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
205e4 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
205e5 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
205e6 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
205e7 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
205e8 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
205e9 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
205ea 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
205eb 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
205ec 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
205ed 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
205ee 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
205ef 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
205f0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
205f1 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
205f2 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
205f3 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
205f4 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
205f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
205f9 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
205fa 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75  tains C code rou
205fb 74 69 6e 65 73 20 74 68 61 74 20 75 73 65 64 20  tines that used 
205fc 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44 42 45  to generate VDBE
205fd 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 69 6d   code.** that im
205fe 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 41 4c 54  plements the ALT
205ff 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64  ER TABLE command
20600 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 6c 74  ..**.** $Id: alt
20601 65 72 2e 63 2c 76 20 31 2e 35 37 20 32 30 30 39  er.c,v 1.57 2009
20602 2f 30 34 2f 31 36 20 31 36 3a 33 30 3a 31 38 20  /04/16 16:30:18 
20603 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
20604 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
20605 74 68 69 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65  this file only e
20606 78 69 73 74 73 20 69 66 20 77 65 20 61 72 65 20  xists if we are 
20607 6e 6f 74 20 6f 6d 69 74 74 69 6e 67 20 74 68 65  not omitting the
20608 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20  .** ALTER TABLE 
20609 6c 6f 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62  logic from the b
2060a 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  uild..*/.#ifndef
2060b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54   SQLITE_OMIT_ALT
2060c 45 52 54 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20  ERTABLE.../*.** 
2060d 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2060e 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e   used by SQL gen
2060f 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d  erated to implem
20610 65 6e 74 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45  ent the .** ALTE
20611 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e  R TABLE command.
20612 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
20613 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20  ent is the text 
20614 6f 66 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  of a CREATE TABL
20615 45 20 6f 72 0a 2a 2a 20 43 52 45 41 54 45 20 49  E or.** CREATE I
20616 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68  NDEX command. Th
20617 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61  e second is a ta
20618 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61  ble name. The ta
20619 62 6c 65 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20  ble name in .** 
2061a 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
2061b 20 6f 72 20 43 52 45 41 54 45 20 49 4e 44 45 58   or CREATE INDEX
2061c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65   statement is re
2061d 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20  placed with the 
2061e 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e  third.** argumen
2061f 74 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74  t and the result
20620 20 72 65 74 75 72 6e 65 64 2e 20 45 78 61 6d 70   returned. Examp
20621 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74  les:.**.** sqlit
20622 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27  e_rename_table('
20623 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63  CREATE TABLE abc
20624 28 61 2c 20 62 2c 20 63 29 27 2c 20 27 64 65 66  (a, b, c)', 'def
20625 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52  ').**     -> 'CR
20626 45 41 54 45 20 54 41 42 4c 45 20 64 65 66 28 61  EATE TABLE def(a
20627 2c 20 62 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73  , b, c)'.**.** s
20628 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62  qlite_rename_tab
20629 6c 65 28 27 43 52 45 41 54 45 20 49 4e 44 45 58  le('CREATE INDEX
2062a 20 69 20 4f 4e 20 61 62 63 28 61 29 27 2c 20 27   i ON abc(a)', '
2062b 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20  def').**     -> 
2062c 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20  'CREATE INDEX i 
2062d 4f 4e 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27  ON def(a, b, c)'
2062e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2062f 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28  renameTableFunc(
20630 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
20631 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
20632 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
20633 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
20634 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  gv.){.  unsigned
20635 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71   char const *zSq
20636 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
20637 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
20638 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
20639 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61   const *zTableNa
2063a 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  me = sqlite3_val
2063b 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
2063c 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a  ;..  int token;.
2063d 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20    Token tname;. 
2063e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
2063f 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71  onst *zCsr = zSq
20640 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30  l;.  int len = 0
20641 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a  ;.  char *zRet;.
20642 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
20643 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
20644 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
20645 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50  xt);..  UNUSED_P
20646 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
20647 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69  );..  /* The pri
20648 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c  nciple used to l
20649 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  ocate the table 
2064a 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41  name in the CREA
2064b 54 45 20 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73  TE TABLE .  ** s
2064c 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74  tatement is that
2064d 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
2064e 69 73 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e  is the first non
2064f 2d 73 70 61 63 65 20 74 6f 6b 65 6e 20 74 68 61  -space token tha
20650 74 0a 20 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69  t.  ** is immedi
20651 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62  ately followed b
20652 79 20 61 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f  y a TK_LP or TK_
20653 55 53 49 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a  USING token..  *
20654 2f 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a  /.  if( zSql ){.
20655 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 69      do {.      i
20656 66 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20  f( !*zCsr ){.   
20657 20 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20       /* Ran out 
20658 6f 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20  of input before 
20659 66 69 6e 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69  finding an openi
2065a 6e 67 20 62 72 61 63 6b 65 74 2e 20 52 65 74 75  ng bracket. Retu
2065b 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20  rn NULL. */.    
2065c 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2065d 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74    }..      /* St
2065e 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 68  ore the token th
2065f 61 74 20 7a 43 73 72 20 70 6f 69 6e 74 73 20 74  at zCsr points t
20660 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20  o in tname. */. 
20661 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 7a       tname.z = z
20662 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65  Csr;.      tname
20663 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20  .n = len;..     
20664 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73 72   /* Advance zCsr
20665 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 6f 6b   to the next tok
20666 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74 20 74  en. Store that t
20667 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27 74 6f  oken type in 'to
20668 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61  ken',.      ** a
20669 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20 69 6e  nd its length in
2066a 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75 73   'len' (to be us
2066b 65 64 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f  ed next iteratio
2066c 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e  n of this loop).
2066d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2066e 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43 73  do {.        zCs
2066f 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20  r += len;.      
20670 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 47    len = sqlite3G
20671 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74  etToken(zCsr, &t
20672 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 20 77  oken);.      } w
20673 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f  hile( token==TK_
20674 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20 61  SPACE );.      a
20675 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a  ssert( len>0 );.
20676 20 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b      } while( tok
20677 65 6e 21 3d 54 4b 5f 4c 50 20 26 26 20 74 6f 6b  en!=TK_LP && tok
20678 65 6e 21 3d 54 4b 5f 55 53 49 4e 47 20 29 3b 0a  en!=TK_USING );.
20679 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c 69  .    zRet = sqli
2067a 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2067b 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20 74  %.*s\"%w\"%s", t
2067c 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c 2c 20 7a  name.z - zSql, z
2067d 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61  Sql, .       zTa
2067e 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a  bleName, tname.z
2067f 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73  +tname.n);.    s
20680 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
20681 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74  xt(context, zRet
20682 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e  , -1, SQLITE_DYN
20683 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  AMIC);.  }.}..#i
20684 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
20685 54 5f 54 52 49 47 47 45 52 0a 2f 2a 20 54 68 69  T_TRIGGER./* Thi
20686 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
20687 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61  ed by SQL genera
20688 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ted to implement
20689 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41   the.** ALTER TA
2068a 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65  BLE command. The
2068b 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
2068c 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61  is the text of a
2068d 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
2068e 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  .** statement. T
2068f 68 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74  he second is a t
20690 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74  able name. The t
20691 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65  able name in the
20692 20 43 52 45 41 54 45 20 0a 2a 2a 20 54 52 49 47   CREATE .** TRIG
20693 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20 69 73  GER statement is
20694 20 72 65 70 6c 61 63 65 64 20 77 69 74 68 20 74   replaced with t
20695 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
20696 74 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74  t and the result
20697 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54   .** returned. T
20698 68 69 73 20 69 73 20 61 6e 61 6c 61 67 6f 75 73  his is analagous
20699 20 74 6f 20 72 65 6e 61 6d 65 54 61 62 6c 65 46   to renameTableF
2069a 75 6e 63 28 29 20 61 62 6f 76 65 2c 20 65 78 63  unc() above, exc
2069b 65 70 74 20 66 6f 72 20 43 52 45 41 54 45 0a 2a  ept for CREATE.*
2069c 2a 20 54 52 49 47 47 45 52 2c 20 6e 6f 74 20 43  * TRIGGER, not C
2069d 52 45 41 54 45 20 49 4e 44 45 58 20 61 6e 64 20  REATE INDEX and 
2069e 43 52 45 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f  CREATE TABLE..*/
2069f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e  .static void ren
206a0 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 28 0a  ameTriggerFunc(.
206a1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
206a2 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
206a3 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
206a4 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
206a5 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  v.){.  unsigned 
206a6 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c  char const *zSql
206a7 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
206a8 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
206a9 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
206aa 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d  const *zTableNam
206ab 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
206ac 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
206ad 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20  ..  int token;. 
206ae 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20   Token tname;.  
206af 69 6e 74 20 64 69 73 74 20 3d 20 33 3b 0a 20 20  int dist = 3;.  
206b0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
206b1 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c  nst *zCsr = zSql
206b2 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b  ;.  int len = 0;
206b3 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 20  .  char *zRet;. 
206b4 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
206b5 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
206b6 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
206b7 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  );..  UNUSED_PAR
206b8 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
206b9 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63  ..  /* The princ
206ba 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63  iple used to loc
206bb 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ate the table na
206bc 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  me in the CREATE
206bd 20 54 52 49 47 47 45 52 20 0a 20 20 2a 2a 20 73   TRIGGER .  ** s
206be 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74  tatement is that
206bf 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
206c0 69 73 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b  is the first tok
206c1 65 6e 20 74 68 61 74 20 69 73 20 69 6d 6d 65 64  en that is immed
206c2 69 61 74 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65  iatedly.  ** pre
206c3 63 65 64 65 64 20 62 79 20 65 69 74 68 65 72 20  ceded by either 
206c4 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20  TK_ON or TK_DOT 
206c5 61 6e 64 20 69 6d 6d 65 64 69 61 74 65 64 6c 79  and immediatedly
206c6 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65   followed by one
206c7 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e  .  ** of TK_WHEN
206c8 2c 20 54 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b  , TK_BEGIN or TK
206c9 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  _FOR..  */.  if(
206ca 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20   zSql ){.    do 
206cb 7b 0a 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a  {..      if( !*z
206cc 43 73 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  Csr ){.        /
206cd 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70  * Ran out of inp
206ce 75 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e  ut before findin
206cf 67 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  g the table name
206d0 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a  . Return NULL. *
206d1 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
206d2 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
206d3 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f   /* Store the to
206d4 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f  ken that zCsr po
206d5 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65  ints to in tname
206d6 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65  . */.      tname
206d7 2e 7a 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20  .z = zCsr;.     
206d8 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a   tname.n = len;.
206d9 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63  .      /* Advanc
206da 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65  e zCsr to the ne
206db 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20  xt token. Store 
206dc 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20  that token type 
206dd 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20  in 'token',.    
206de 20 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e    ** and its len
206df 67 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f  gth in 'len' (to
206e0 20 62 65 20 75 73 65 64 20 6e 65 78 74 20 69 74   be used next it
206e1 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  eration of this 
206e2 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  loop)..      */.
206e3 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20        do {.     
206e4 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a     zCsr += len;.
206e5 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71          len = sq
206e6 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43  lite3GetToken(zC
206e7 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20  sr, &token);.   
206e8 20 20 20 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e     }while( token
206e9 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20  ==TK_SPACE );.  
206ea 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e      assert( len>
206eb 30 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 56  0 );..      /* V
206ec 61 72 69 61 62 6c 65 20 27 64 69 73 74 27 20 73  ariable 'dist' s
206ed 74 6f 72 65 73 20 74 68 65 20 6e 75 6d 62 65 72  tores the number
206ee 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65 61 64 20   of tokens read 
206ef 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 0a 20  since the most. 
206f0 20 20 20 20 20 2a 2a 20 72 65 63 65 6e 74 20 54       ** recent T
206f1 4b 5f 44 4f 54 20 6f 72 20 54 4b 5f 4f 4e 2e 20  K_DOT or TK_ON. 
206f2 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20  This means that 
206f3 77 68 65 6e 20 61 20 57 48 45 4e 2c 20 46 4f 52  when a WHEN, FOR
206f4 20 6f 72 20 42 45 47 49 4e 20 0a 20 20 20 20 20   or BEGIN .     
206f5 20 2a 2a 20 74 6f 6b 65 6e 20 69 73 20 72 65 61   ** token is rea
206f6 64 20 61 6e 64 20 27 64 69 73 74 27 20 65 71 75  d and 'dist' equ
206f7 61 6c 73 20 32 2c 20 74 68 65 20 63 6f 6e 64 69  als 2, the condi
206f8 74 69 6f 6e 20 73 74 61 74 65 64 20 61 62 6f 76  tion stated abov
206f9 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65  e.      ** to be
206fa 20 6d 65 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20   met..      **. 
206fb 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61       ** Note tha
206fc 74 20 4f 4e 20 63 61 6e 6e 6f 74 20 62 65 20 61  t ON cannot be a
206fd 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
206fe 20 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2c   or column name,
206ff 20 73 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   so.      ** the
20700 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
20701 20 77 6f 72 72 79 20 61 62 6f 75 74 20 73 79 6e   worry about syn
20702 74 61 78 20 6c 69 6b 65 20 0a 20 20 20 20 20 20  tax like .      
20703 2a 2a 20 22 43 52 45 41 54 45 20 54 52 49 47 47  ** "CREATE TRIGG
20704 45 52 20 2e 2e 2e 20 4f 4e 20 4f 4e 2e 4f 4e 20  ER ... ON ON.ON 
20705 42 45 47 49 4e 20 2e 2e 2e 22 20 65 74 63 2e 0a  BEGIN ..." etc..
20706 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64        */.      d
20707 69 73 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28  ist++;.      if(
20708 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 44 4f 54 20 7c   token==TK_DOT |
20709 7c 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 4f 4e 20 29  | token==TK_ON )
2070a 7b 0a 20 20 20 20 20 20 20 20 64 69 73 74 20 3d  {.        dist =
2070b 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2070c 7d 20 77 68 69 6c 65 28 20 64 69 73 74 21 3d 32  } while( dist!=2
2070d 20 7c 7c 20 28 74 6f 6b 65 6e 21 3d 54 4b 5f 57   || (token!=TK_W
2070e 48 45 4e 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b  HEN && token!=TK
2070f 5f 46 4f 52 20 26 26 20 74 6f 6b 65 6e 21 3d 54  _FOR && token!=T
20710 4b 5f 42 45 47 49 4e 29 20 29 3b 0a 0a 20 20 20  K_BEGIN) );..   
20711 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6e 61   /* Variable tna
20712 6d 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20  me now contains 
20713 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 69  the token that i
20714 73 20 74 68 65 20 6f 6c 64 20 74 61 62 6c 65 2d  s the old table-
20715 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74  name.    ** in t
20716 68 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45  he CREATE TRIGGE
20717 52 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  R statement..   
20718 20 2a 2f 0a 20 20 20 20 7a 52 65 74 20 3d 20 73   */.    zRet = s
20719 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2071a 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22  , "%.*s\"%w\"%s"
2071b 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71 6c  , tname.z - zSql
2071c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20 20  , zSql, .       
2071d 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d  zTableName, tnam
2071e 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20  e.z+tname.n);.  
2071f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
20720 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
20721 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  Ret, -1, SQLITE_
20722 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a  DYNAMIC);.  }.}.
20723 23 65 6e 64 69 66 20 20 20 2f 2a 20 21 53 51 4c  #endif   /* !SQL
20724 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
20725 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73   */../*.** Regis
20726 74 65 72 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e  ter built-in fun
20727 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 68  ctions used to h
20728 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 41 4c  elp implement AL
20729 54 45 52 20 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c  TER TABLE.*/.SQL
2072a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2072b 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e   sqlite3AlterFun
2072c 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a  ctions(sqlite3 *
2072d 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 72  db){.  sqlite3Cr
2072e 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71  eateFunc(db, "sq
2072f 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  lite_rename_tabl
20730 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  e", 2, SQLITE_UT
20731 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  F8, 0,.         
20732 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20733 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 2c  renameTableFunc,
20734 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20   0, 0);.#ifndef 
20735 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
20736 47 45 52 0a 20 20 73 71 6c 69 74 65 33 43 72 65  GER.  sqlite3Cre
20737 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c  ateFunc(db, "sql
20738 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67  ite_rename_trigg
20739 65 72 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55  er", 2, SQLITE_U
2073a 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  TF8, 0,.        
2073b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2073c 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75   renameTriggerFu
2073d 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69  nc, 0, 0);.#endi
2073e 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  f.}../*.** Gener
2073f 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20  ate the text of 
20740 61 20 57 48 45 52 45 20 65 78 70 72 65 73 73 69  a WHERE expressi
20741 6f 6e 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  on which can be 
20742 75 73 65 64 20 74 6f 20 73 65 6c 65 63 74 20 61  used to select a
20743 6c 6c 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20  ll.** temporary 
20744 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c  triggers on tabl
20745 65 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20  e pTab from the 
20746 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
20747 65 72 20 74 61 62 6c 65 2e 20 49 66 0a 2a 2a 20  er table. If.** 
20748 74 61 62 6c 65 20 70 54 61 62 20 68 61 73 20 6e  table pTab has n
20749 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67  o temporary trig
2074a 67 65 72 73 2c 20 6f 72 20 69 73 20 69 74 73 65  gers, or is itse
2074b 6c 66 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  lf stored in the
2074c 20 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 64   .** temporary d
2074d 61 74 61 62 61 73 65 2c 20 4e 55 4c 4c 20 69 73  atabase, NULL is
2074e 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
2074f 61 74 69 63 20 63 68 61 72 20 2a 77 68 65 72 65  atic char *where
20750 54 65 6d 70 54 72 69 67 67 65 72 73 28 50 61 72  TempTriggers(Par
20751 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
20752 65 20 2a 70 54 61 62 29 7b 0a 20 20 54 72 69 67  e *pTab){.  Trig
20753 67 65 72 20 2a 70 54 72 69 67 3b 0a 20 20 63 68  ger *pTrig;.  ch
20754 61 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b 0a  ar *zWhere = 0;.
20755 20 20 63 68 61 72 20 2a 74 6d 70 20 3d 20 30 3b    char *tmp = 0;
20756 0a 20 20 63 6f 6e 73 74 20 53 63 68 65 6d 61 20  .  const Schema 
20757 2a 70 54 65 6d 70 53 63 68 65 6d 61 20 3d 20 70  *pTempSchema = p
20758 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31  Parse->db->aDb[1
20759 5d 2e 70 53 63 68 65 6d 61 3b 20 2f 2a 20 54 65  ].pSchema; /* Te
2075a 6d 70 20 64 62 20 73 63 68 65 6d 61 20 2a 2f 0a  mp db schema */.
2075b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62  .  /* If the tab
2075c 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 61 74 65  le is not locate
2075d 64 20 69 6e 20 74 68 65 20 74 65 6d 70 2d 64 62  d in the temp-db
2075e 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
2075f 4e 55 4c 4c 20 69 73 20 0a 20 20 2a 2a 20 72 65  NULL is .  ** re
20760 74 75 72 6e 65 64 2c 20 6c 6f 6f 70 20 74 68 72  turned, loop thr
20761 6f 75 67 68 20 74 68 65 20 74 61 62 6c 65 73 20  ough the tables 
20762 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73  list of triggers
20763 2e 20 46 6f 72 20 65 61 63 68 20 74 72 69 67 67  . For each trigg
20764 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 69 73 20  er.  ** that is 
20765 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20  not part of the 
20766 74 65 6d 70 2d 64 62 20 73 63 68 65 6d 61 2c 20  temp-db schema, 
20767 61 64 64 20 61 20 63 6c 61 75 73 65 20 74 6f 20  add a clause to 
20768 74 68 65 20 57 48 45 52 45 20 0a 20 20 2a 2a 20  the WHERE .  ** 
20769 65 78 70 72 65 73 73 69 6f 6e 20 62 65 69 6e 67  expression being
2076a 20 62 75 69 6c 74 20 75 70 20 69 6e 20 7a 57 68   built up in zWh
2076b 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ere..  */.  if( 
2076c 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 21 3d 70  pTab->pSchema!=p
2076d 54 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  TempSchema ){.  
2076e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2076f 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20  pParse->db;.    
20770 66 6f 72 28 70 54 72 69 67 3d 73 71 6c 69 74 65  for(pTrig=sqlite
20771 33 54 72 69 67 67 65 72 4c 69 73 74 28 70 50 61  3TriggerList(pPa
20772 72 73 65 2c 20 70 54 61 62 29 3b 20 70 54 72 69  rse, pTab); pTri
20773 67 3b 20 70 54 72 69 67 3d 70 54 72 69 67 2d 3e  g; pTrig=pTrig->
20774 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66  pNext){.      if
20775 28 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61  ( pTrig->pSchema
20776 3d 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 29 7b  ==pTempSchema ){
20777 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 57  .        if( !zW
20778 68 65 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20  here ){.        
20779 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74    zWhere = sqlit
2077a 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
2077b 61 6d 65 3d 25 51 22 2c 20 70 54 72 69 67 2d 3e  ame=%Q", pTrig->
2077c 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  name);.        }
2077d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2077e 74 6d 70 20 3d 20 7a 57 68 65 72 65 3b 0a 20 20  tmp = zWhere;.  
2077f 20 20 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d          zWhere =
20780 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
20781 64 62 2c 20 22 25 73 20 4f 52 20 6e 61 6d 65 3d  db, "%s OR name=
20782 25 51 22 2c 20 7a 57 68 65 72 65 2c 20 70 54 72  %Q", zWhere, pTr
20783 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20  ig->name);.     
20784 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
20785 65 65 28 64 62 2c 20 74 6d 70 29 3b 0a 20 20 20  ee(db, tmp);.   
20786 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
20787 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
20788 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a  n zWhere;.}../*.
20789 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2078a 20 74 6f 20 64 72 6f 70 20 61 6e 64 20 72 65 6c   to drop and rel
2078b 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  oad the internal
2078c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
2078d 6f 66 20 74 61 62 6c 65 0a 2a 2a 20 70 54 61 62  of table.** pTab
2078e 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
2078f 73 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 72  se, including tr
20790 69 67 67 65 72 73 20 61 6e 64 20 74 65 6d 70 6f  iggers and tempo
20791 72 61 72 79 20 74 72 69 67 67 65 72 73 2e 0a 2a  rary triggers..*
20792 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65  * Argument zName
20793 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
20794 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  the table in the
20795 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
20796 20 61 74 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20   at.** the time 
20797 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f  the generated co
20798 64 65 20 69 73 20 65 78 65 63 75 74 65 64 2e 20  de is executed. 
20799 54 68 69 73 20 63 61 6e 20 62 65 20 64 69 66 66  This can be diff
2079a 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 70 54  erent from.** pT
2079b 61 62 2d 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69  ab->zName if thi
2079c 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  s function is be
2079d 69 6e 67 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f  ing called to co
2079e 64 65 20 70 61 72 74 20 6f 66 20 61 6e 20 0a 2a  de part of an .*
2079f 2a 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 52  * "ALTER TABLE R
207a0 45 4e 41 4d 45 20 54 4f 22 20 73 74 61 74 65 6d  ENAME TO" statem
207a1 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ent..*/.static v
207a2 6f 69 64 20 72 65 6c 6f 61 64 54 61 62 6c 65 53  oid reloadTableS
207a3 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61  chema(Parse *pPa
207a4 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
207a5 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
207a6 61 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b  ame){.  Vdbe *v;
207a7 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 3b  .  char *zWhere;
207a8 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
207a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
207aa 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61   Index of databa
207ab 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54  se containing pT
207ac 61 62 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ab */.#ifndef SQ
207ad 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
207ae 52 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  R.  Trigger *pTr
207af 69 67 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20  ig;.#endif..  v 
207b0 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
207b1 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
207b2 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65  NEVER(v==0) ) re
207b3 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
207b4 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
207b5 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72  sAllMutexes(pPar
207b6 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44 62  se->db) );.  iDb
207b7 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
207b8 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
207b9 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
207ba 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  a);.  assert( iD
207bb 62 3e 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66  b>=0 );..#ifndef
207bc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
207bd 47 47 45 52 0a 20 20 2f 2a 20 44 72 6f 70 20 61  GGER.  /* Drop a
207be 6e 79 20 74 61 62 6c 65 20 74 72 69 67 67 65 72  ny table trigger
207bf 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72  s from the inter
207c0 6e 61 6c 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20  nal schema. */. 
207c1 20 66 6f 72 28 70 54 72 69 67 3d 73 71 6c 69 74   for(pTrig=sqlit
207c2 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70 50  e3TriggerList(pP
207c3 61 72 73 65 2c 20 70 54 61 62 29 3b 20 70 54 72  arse, pTab); pTr
207c4 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69 67 2d  ig; pTrig=pTrig-
207c5 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74  >pNext){.    int
207c6 20 69 54 72 69 67 44 62 20 3d 20 73 71 6c 69 74   iTrigDb = sqlit
207c7 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
207c8 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69  pParse->db, pTri
207c9 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  g->pSchema);.   
207ca 20 61 73 73 65 72 74 28 20 69 54 72 69 67 44 62   assert( iTrigDb
207cb 3d 3d 69 44 62 20 7c 7c 20 69 54 72 69 67 44 62  ==iDb || iTrigDb
207cc 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ==1 );.    sqlit
207cd 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
207ce 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 2c 20  OP_DropTrigger, 
207cf 69 54 72 69 67 44 62 2c 20 30 2c 20 30 2c 20 70  iTrigDb, 0, 0, p
207d0 54 72 69 67 2d 3e 6e 61 6d 65 2c 20 30 29 3b 0a  Trig->name, 0);.
207d1 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
207d2 20 44 72 6f 70 20 74 68 65 20 74 61 62 6c 65 20   Drop the table 
207d3 61 6e 64 20 69 6e 64 65 78 20 66 72 6f 6d 20 74  and index from t
207d4 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
207d5 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  ma */.  sqlite3V
207d6 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
207d7 44 72 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20  DropTable, iDb, 
207d8 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  0, 0, pTab->zNam
207d9 65 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c  e, 0);..  /* Rel
207da 6f 61 64 20 74 68 65 20 74 61 62 6c 65 2c 20 69  oad the table, i
207db 6e 64 65 78 20 61 6e 64 20 70 65 72 6d 61 6e 65  ndex and permane
207dc 6e 74 20 74 72 69 67 67 65 72 20 73 63 68 65 6d  nt trigger schem
207dd 61 73 2e 20 2a 2f 0a 20 20 7a 57 68 65 72 65 20  as. */.  zWhere 
207de 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
207df 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 74 62  (pParse->db, "tb
207e0 6c 5f 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d  l_name=%Q", zNam
207e1 65 29 3b 0a 20 20 69 66 28 20 21 7a 57 68 65 72  e);.  if( !zWher
207e2 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  e ) return;.  sq
207e3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
207e4 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  v, OP_ParseSchem
207e5 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 7a 57  a, iDb, 0, 0, zW
207e6 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43  here, P4_DYNAMIC
207e7 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
207e8 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
207e9 20 20 2f 2a 20 4e 6f 77 2c 20 69 66 20 74 68 65    /* Now, if the
207ea 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 73 74   table is not st
207eb 6f 72 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70  ored in the temp
207ec 20 64 61 74 61 62 61 73 65 2c 20 72 65 6c 6f 61   database, reloa
207ed 64 20 61 6e 79 20 74 65 6d 70 20 0a 20 20 2a 2a  d any temp .  **
207ee 20 74 72 69 67 67 65 72 73 2e 20 44 6f 6e 27 74   triggers. Don't
207ef 20 75 73 65 20 49 4e 28 2e 2e 2e 29 20 69 6e 20   use IN(...) in 
207f0 63 61 73 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54  case SQLITE_OMIT
207f1 5f 53 55 42 51 55 45 52 59 20 69 73 20 64 65 66  _SUBQUERY is def
207f2 69 6e 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 69 66  ined. .  */.  if
207f3 28 20 28 7a 57 68 65 72 65 3d 77 68 65 72 65 54  ( (zWhere=whereT
207f4 65 6d 70 54 72 69 67 67 65 72 73 28 70 50 61 72  empTriggers(pPar
207f5 73 65 2c 20 70 54 61 62 29 29 21 3d 30 20 29 7b  se, pTab))!=0 ){
207f6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
207f7 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72  AddOp4(v, OP_Par
207f8 73 65 53 63 68 65 6d 61 2c 20 31 2c 20 30 2c 20  seSchema, 1, 0, 
207f9 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59  0, zWhere, P4_DY
207fa 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64  NAMIC);.  }.#end
207fb 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  if.}../*.** Gene
207fc 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70  rate code to imp
207fd 6c 65 6d 65 6e 74 20 74 68 65 20 22 41 4c 54 45  lement the "ALTE
207fe 52 20 54 41 42 4c 45 20 78 78 78 20 52 45 4e 41  R TABLE xxx RENA
207ff 4d 45 20 54 4f 20 79 79 79 22 20 0a 2a 2a 20 63  ME TO yyy" .** c
20800 6f 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a 53 51 4c 49  ommand. .*/.SQLI
20801 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
20802 73 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61  sqlite3AlterRena
20803 6d 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  meTable(.  Parse
20804 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
20805 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63       /* Parser c
20806 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53 72 63  ontext. */.  Src
20807 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
20808 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
20809 62 6c 65 20 74 6f 20 72 65 6e 61 6d 65 2e 20 2a  ble to rename. *
2080a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
2080b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2080c 20 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   The new table n
2080d 61 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ame. */.){.  int
2080e 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
2080f 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
20810 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  se that contains
20811 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
20812 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
20813 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
20814 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 44  e of database iD
20815 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
20816 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
20817 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20   /* Table being 
20818 72 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 63 68 61  renamed */.  cha
20819 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20  r *zName = 0;   
2081a 20 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74         /* NULL-t
2081b 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f  erminated versio
2081c 6e 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20 0a 20  n of pName */ . 
2081d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2081e 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61  Parse->db; /* Da
2081f 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
20820 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 4e  n */.  int nTabN
20821 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
20822 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 54   /* Number of UT
20823 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 20 69  F-8 characters i
20824 6e 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a 20 20  n zTabName */.  
20825 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
20826 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f 72 69  Name;     /* Ori
20827 67 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20 74 68  ginal name of th
20828 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62  e table */.  Vdb
20829 65 20 2a 76 3b 0a 23 69 66 6e 64 65 66 20 53 51  e *v;.#ifndef SQ
2082a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2082b 52 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65  R.  char *zWhere
2082c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
2082d 20 57 68 65 72 65 20 63 6c 61 75 73 65 20 74 6f   Where clause to
2082e 20 6c 6f 63 61 74 65 20 74 65 6d 70 20 74 72 69   locate temp tri
2082f 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  ggers */.#endif.
20830 20 20 69 6e 74 20 69 73 56 69 72 74 75 61 6c 52    int isVirtualR
20831 65 6e 61 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 54  ename = 0;  /* T
20832 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61  rue if this is a
20833 20 76 2d 74 61 62 6c 65 20 77 69 74 68 20 61 6e   v-table with an
20834 20 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a 20 20   xRename() */.  
20835 0a 20 20 69 66 28 20 4e 45 56 45 52 28 64 62 2d  .  if( NEVER(db-
20836 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29  >mallocFailed) )
20837 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
20838 65 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73 65 72  e_table;.  asser
20839 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31  t( pSrc->nSrc==1
2083a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
2083b 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
2083c 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65  llMutexes(pParse
2083d 2d 3e 64 62 29 20 29 3b 0a 0a 20 20 70 54 61 62  ->db) );..  pTab
2083e 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
2083f 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
20840 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d   pSrc->a[0].zNam
20841 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44  e, pSrc->a[0].zD
20842 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20  atabase);.  if( 
20843 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69  !pTab ) goto exi
20844 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
20845 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
20846 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
20847 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
20848 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d  Schema);.  zDb =
20849 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2084a 61 6d 65 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61  ame;..  /* Get a
2084b 20 4e 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64   NULL terminated
2084c 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
2084d 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  new table name. 
2084e 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  */.  zName = sql
2084f 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
20850 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  n(db, pName);.  
20851 69 66 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74  if( !zName ) got
20852 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
20853 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ble;..  /* Check
20854 20 74 68 61 74 20 61 20 74 61 62 6c 65 20 6f 72   that a table or
20855 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e   index named 'zN
20856 61 6d 65 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c  ame' does not al
20857 72 65 61 64 79 20 65 78 69 73 74 0a 20 20 2a 2a  ready exist.  **
20858 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
20859 2e 20 49 66 20 73 6f 2c 20 74 68 69 73 20 69 73  . If so, this is
2085a 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a   an error..  */.
2085b 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
2085c 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  dTable(db, zName
2085d 2c 20 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74 65  , zDb) || sqlite
2085e 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a  3FindIndex(db, z
2085f 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20  Name, zDb) ){.  
20860 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
20861 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  g(pParse, .     
20862 20 20 20 22 74 68 65 72 65 20 69 73 20 61 6c 72     "there is alr
20863 65 61 64 79 20 61 6e 6f 74 68 65 72 20 74 61 62  eady another tab
20864 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 69 74 68  le or index with
20865 20 74 68 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c   this name: %s",
20866 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74   zName);.    got
20867 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
20868 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  ble;.  }..  /* M
20869 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6e  ake sure it is n
2086a 6f 74 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c  ot a system tabl
2086b 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c  e being altered,
2086c 20 6f 72 20 61 20 72 65 73 65 72 76 65 64 20 6e   or a reserved n
2086d 61 6d 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68  ame.  ** that th
2086e 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67  e table is being
2086f 20 72 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a   renamed to..  *
20870 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
20871 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e  trlen30(pTab->zN
20872 61 6d 65 29 3e 36 20 0a 20 20 20 26 26 20 30 3d  ame)>6 .   && 0=
20873 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70  =sqlite3StrNICmp
20874 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73  (pTab->zName, "s
20875 71 6c 69 74 65 5f 22 2c 20 37 29 0a 20 20 29 7b  qlite_", 7).  ){
20876 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
20877 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
20878 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62  ble %s may not b
20879 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62  e altered", pTab
2087a 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
2087b 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
2087c 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  able;.  }.  if( 
2087d 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
2087e 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
2087f 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29  e(pParse, zName)
20880 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
20881 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
20882 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
20883 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
20884 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
20885 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
20886 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
20887 20 22 76 69 65 77 20 25 73 20 6d 61 79 20 6e 6f   "view %s may no
20888 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70  t be altered", p
20889 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
2088a 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
2088b 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e  e_table;.  }.#en
2088c 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
2088d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
2088e 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f  ZATION.  /* Invo
2088f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61  ke the authoriza
20890 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a  tion callback. *
20891 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  /.  if( sqlite3A
20892 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
20893 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41   SQLITE_ALTER_TA
20894 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e  BLE, zDb, pTab->
20895 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20  zName, 0) ){.   
20896 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
20897 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e  e_table;.  }.#en
20898 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
20899 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2089a 54 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69  TABLE.  if( sqli
2089b 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
2089c 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
2089d 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ab) ){.    goto 
2089e 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
2089f 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56  e;.  }.  if( IsV
208a0 69 72 74 75 61 6c 28 70 54 61 62 29 20 26 26 20  irtual(pTab) && 
208a1 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64  pTab->pMod->pMod
208a2 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 7b 0a  ule->xRename ){.
208a3 20 20 20 20 69 73 56 69 72 74 75 61 6c 52 65 6e      isVirtualRen
208a4 61 6d 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 6e  ame = 1;.  }.#en
208a5 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20  dif..  /* Begin 
208a6 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e  a transaction an
208a7 64 20 63 6f 64 65 20 74 68 65 20 56 65 72 69 66  d code the Verif
208a8 79 43 6f 6f 6b 69 65 20 66 6f 72 20 64 61 74 61  yCookie for data
208a9 62 61 73 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20  base iDb. .  ** 
208aa 54 68 65 6e 20 6d 6f 64 69 66 79 20 74 68 65 20  Then modify the 
208ab 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73  schema cookie (s
208ac 69 6e 63 65 20 74 68 65 20 41 4c 54 45 52 20 54  ince the ALTER T
208ad 41 42 4c 45 20 6d 6f 64 69 66 69 65 73 20 74 68  ABLE modifies th
208ae 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20  e.  ** schema). 
208af 4f 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74  Open a statement
208b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20   transaction if 
208b1 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 76  the table is a v
208b2 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c  irtual.  ** tabl
208b3 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71  e..  */.  v = sq
208b4 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
208b5 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  rse);.  if( v==0
208b6 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
208b7 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
208b8 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 65 67    }.  sqlite3Beg
208b9 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e  inWriteOperation
208ba 28 70 50 61 72 73 65 2c 20 69 73 56 69 72 74 75  (pParse, isVirtu
208bb 61 6c 52 65 6e 61 6d 65 2c 20 69 44 62 29 3b 0a  alRename, iDb);.
208bc 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
208bd 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44  ookie(pParse, iD
208be 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69  b);..  /* If thi
208bf 73 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74  s is a virtual t
208c0 61 62 6c 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65  able, invoke the
208c1 20 78 52 65 6e 61 6d 65 28 29 20 66 75 6e 63 74   xRename() funct
208c2 69 6f 6e 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20  ion if.  ** one 
208c3 69 73 20 64 65 66 69 6e 65 64 2e 20 54 68 65 20  is defined. The 
208c4 78 52 65 6e 61 6d 65 28 29 20 63 61 6c 6c 62 61  xRename() callba
208c5 63 6b 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74  ck will modify t
208c6 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66  he names.  ** of
208c7 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 75   any resources u
208c8 73 65 64 20 62 79 20 74 68 65 20 76 2d 74 61 62  sed by the v-tab
208c9 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
208ca 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 6f 74 68  n (including oth
208cb 65 72 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 74  er.  ** SQLite t
208cc 61 62 6c 65 73 29 20 74 68 61 74 20 61 72 65 20  ables) that are 
208cd 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68  identified by th
208ce 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69  e name of the vi
208cf 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a  rtual table..  *
208d0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
208d1 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
208d2 4c 45 0a 20 20 69 66 28 20 69 73 56 69 72 74 75  LE.  if( isVirtu
208d3 61 6c 52 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20  alRename ){.    
208d4 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61 72 73 65  int i = ++pParse
208d5 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
208d6 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
208d7 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
208d8 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b  i, 0, zName, 0);
208d9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
208da 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65  AddOp4(v, OP_VRe
208db 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63  name, i, 0, 0,(c
208dc 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 61 62 2d  onst char*)pTab-
208dd 3e 70 56 74 61 62 2c 20 50 34 5f 56 54 41 42 29  >pVtab, P4_VTAB)
208de 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
208df 2f 2a 20 66 69 67 75 72 65 20 6f 75 74 20 68 6f  /* figure out ho
208e0 77 20 6d 61 6e 79 20 55 54 46 2d 38 20 63 68 61  w many UTF-8 cha
208e1 72 61 63 74 65 72 73 20 61 72 65 20 69 6e 20 7a  racters are in z
208e2 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54 61 62 4e 61  Name */.  zTabNa
208e3 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  me = pTab->zName
208e4 3b 0a 20 20 6e 54 61 62 4e 61 6d 65 20 3d 20 73  ;.  nTabName = s
208e5 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
208e6 6e 28 7a 54 61 62 4e 61 6d 65 2c 20 2d 31 29 3b  n(zTabName, -1);
208e7 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68  ..  /* Modify th
208e8 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
208e9 74 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 65  table to use the
208ea 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e   new table name.
208eb 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73   */.  sqlite3Nes
208ec 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
208ed 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25  .      "UPDATE %
208ee 51 2e 25 73 20 53 45 54 20 22 0a 23 69 66 64 65  Q.%s SET ".#ifde
208ef 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
208f0 49 47 47 45 52 0a 20 20 20 20 20 20 20 20 20 20  IGGER.          
208f1 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65  "sql = sqlite_re
208f2 6e 61 6d 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20  name_table(sql, 
208f3 25 51 29 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20  %Q), ".#else.   
208f4 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 43 41         "sql = CA
208f5 53 45 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  SE ".           
208f6 20 22 57 48 45 4e 20 74 79 70 65 20 3d 20 27 74   "WHEN type = 't
208f7 72 69 67 67 65 72 27 20 54 48 45 4e 20 73 71 6c  rigger' THEN sql
208f8 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67  ite_rename_trigg
208f9 65 72 28 73 71 6c 2c 20 25 51 29 22 0a 20 20 20  er(sql, %Q)".   
208fa 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 73           "ELSE s
208fb 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62  qlite_rename_tab
208fc 6c 65 28 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c  le(sql, %Q) END,
208fd 20 22 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   ".#endif.      
208fe 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20      "tbl_name = 
208ff 25 51 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20  %Q, ".          
20900 22 6e 61 6d 65 20 3d 20 43 41 53 45 20 22 0a 20  "name = CASE ". 
20901 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e             "WHEN
20902 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 54 48   type='table' TH
20903 45 4e 20 25 51 20 22 0a 20 20 20 20 20 20 20 20  EN %Q ".        
20904 20 20 20 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c      "WHEN name L
20905 49 4b 45 20 27 73 71 6c 69 74 65 5f 61 75 74 6f  IKE 'sqlite_auto
20906 69 6e 64 65 78 25 25 27 20 41 4e 44 20 74 79 70  index%%' AND typ
20907 65 3d 27 69 6e 64 65 78 27 20 54 48 45 4e 20 22  e='index' THEN "
20908 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 22 27  .             "'
20909 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78  sqlite_autoindex
2090a 5f 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73  _' || %Q || subs
2090b 74 72 28 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22  tr(name,%d+18) "
2090c 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c  .            "EL
2090d 53 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20  SE name END ".  
2090e 20 20 20 20 22 57 48 45 52 45 20 74 62 6c 5f 6e      "WHERE tbl_n
2090f 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a 20 20 20  ame=%Q AND ".   
20910 20 20 20 20 20 20 20 22 28 74 79 70 65 3d 27 74         "(type='t
20911 61 62 6c 65 27 20 4f 52 20 74 79 70 65 3d 27 69  able' OR type='i
20912 6e 64 65 78 27 20 4f 52 20 74 79 70 65 3d 27 74  ndex' OR type='t
20913 72 69 67 67 65 72 27 29 3b 22 2c 20 0a 20 20 20  rigger');", .   
20914 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54     zDb, SCHEMA_T
20915 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65  ABLE(iDb), zName
20916 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20  , zName, zName, 
20917 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20918 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
20919 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66     zName,.#endif
2091a 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54  .      zName, nT
2091b 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65  abName, zTabName
2091c 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  .  );..#ifndef S
2091d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
2091e 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66  NCREMENT.  /* If
2091f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
20920 65 6e 63 65 20 74 61 62 6c 65 20 65 78 69 73 74  ence table exist
20921 73 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 61  s in this databa
20922 73 65 2c 20 74 68 65 6e 20 75 70 64 61 74 65 20  se, then update 
20923 0a 20 20 2a 2a 20 69 74 20 77 69 74 68 20 74 68  .  ** it with th
20924 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65  e new table name
20925 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
20926 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
20927 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  , "sqlite_sequen
20928 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  ce", zDb) ){.   
20929 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
2092a 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
2092b 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77      "UPDATE \"%w
2092c 5c 22 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  \".sqlite_sequen
2092d 63 65 20 73 65 74 20 6e 61 6d 65 20 3d 20 25 51  ce set name = %Q
2092e 20 57 48 45 52 45 20 6e 61 6d 65 20 3d 20 25 51   WHERE name = %Q
2092f 22 2c 0a 20 20 20 20 20 20 20 20 7a 44 62 2c 20  ",.        zDb, 
20930 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  zName, pTab->zNa
20931 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  me);.  }.#endif.
20932 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20933 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f  OMIT_TRIGGER.  /
20934 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 54  * If there are T
20935 45 4d 50 20 74 72 69 67 67 65 72 73 20 6f 6e 20  EMP triggers on 
20936 74 68 69 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69  this table, modi
20937 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65  fy the sqlite_te
20938 6d 70 5f 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74  mp_master.  ** t
20939 61 62 6c 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74  able. Don't do t
2093a 68 69 73 20 69 66 20 74 68 65 20 74 61 62 6c 65  his if the table
2093b 20 62 65 69 6e 67 20 41 4c 54 45 52 65 64 20 69   being ALTERed i
2093c 73 20 69 74 73 65 6c 66 20 6c 6f 63 61 74 65 64  s itself located
2093d 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d   in.  ** the tem
2093e 70 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  p database..  */
2093f 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77  .  if( (zWhere=w
20940 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73  hereTempTriggers
20941 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21  (pParse, pTab))!
20942 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
20943 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
20944 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55  rse, .        "U
20945 50 44 41 54 45 20 73 71 6c 69 74 65 5f 74 65 6d  PDATE sqlite_tem
20946 70 5f 6d 61 73 74 65 72 20 53 45 54 20 22 0a 20  p_master SET ". 
20947 20 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20             "sql 
20948 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f  = sqlite_rename_
20949 74 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29  trigger(sql, %Q)
2094a 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ".            
2094b 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22  "tbl_name = %Q "
2094c 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48  .            "WH
2094d 45 52 45 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c  ERE %s;", zName,
2094e 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b   zName, zWhere);
2094f 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
20950 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a  ee(db, zWhere);.
20951 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
20952 20 44 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64   Drop and reload
20953 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 61   the internal ta
20954 62 6c 65 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20  ble schema. */. 
20955 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65   reloadTableSche
20956 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ma(pParse, pTab,
20957 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78 69 74 5f 72   zName);..exit_r
20958 65 6e 61 6d 65 5f 74 61 62 6c 65 3a 0a 20 20 73  ename_table:.  s
20959 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
2095a 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20  ete(db, pSrc);. 
2095b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2095c 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f  b, zName);.}.../
2095d 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2095e 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74  on is called aft
2095f 65 72 20 61 6e 20 22 41 4c 54 45 52 20 54 41 42  er an "ALTER TAB
20960 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73 74 61 74  LE ... ADD" stat
20961 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65  ement.** has bee
20962 6e 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d 65  n parsed. Argume
20963 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f 6e 74 61  nt pColDef conta
20964 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66 20  ins the text of 
20965 74 68 65 20 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d  the new.** colum
20966 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a  n definition..**
20967 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74  .** The Table st
20968 72 75 63 74 75 72 65 20 70 50 61 72 73 65 2d 3e  ructure pParse->
20969 70 4e 65 77 54 61 62 6c 65 20 77 61 73 20 65 78  pNewTable was ex
2096a 74 65 6e 64 65 64 20 74 6f 20 69 6e 63 6c 75 64  tended to includ
2096b 65 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 6f 6c  e.** the new col
2096c 75 6d 6e 20 64 75 72 69 6e 67 20 70 61 72 73 69  umn during parsi
2096d 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
2096e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2096f 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64  e3AlterFinishAdd
20970 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50  Column(Parse *pP
20971 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f  arse, Token *pCo
20972 6c 44 65 66 29 7b 0a 20 20 54 61 62 6c 65 20 2a  lDef){.  Table *
20973 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  pNew;           
20974 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50     /* Copy of pP
20975 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
20976 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
20977 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
20978 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 61 6c  * Table being al
20979 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  tered */.  int i
2097a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
2097b 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2097c 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 6f 6e   number */.  con
2097d 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  st char *zDb;   
2097e 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2097f 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e  se name */.  con
20980 73 74 20 63 68 61 72 20 2a 7a 54 61 62 3b 20 20  st char *zTab;  
20981 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
20982 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  name */.  char *
20983 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  zCol;           
20984 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d      /* Null-term
20985 69 6e 61 74 65 64 20 63 6f 6c 75 6d 6e 20 64 65  inated column de
20986 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f  finition */.  Co
20987 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20 20 20 20  lumn *pCol;     
20988 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
20989 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45  ew column */.  E
2098a 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20  xpr *pDflt;     
2098b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
2098c 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
2098d 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  e new column */.
2098e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
2098f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
20990 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
20991 65 63 74 69 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62  ection; */..  db
20992 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
20993 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
20994 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
20995 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
20996 20 20 70 4e 65 77 20 3d 20 70 50 61 72 73 65 2d    pNew = pParse-
20997 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 61 73  >pNewTable;.  as
20998 73 65 72 74 28 20 70 4e 65 77 20 29 3b 0a 0a 20  sert( pNew );.. 
20999 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2099a 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
2099b 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69 44  exes(db) );.  iD
2099c 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2099d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 4e 65  aToIndex(db, pNe
2099e 77 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a  w->pSchema);.  z
2099f 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
209a0 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7a 54 61 62 20  ].zName;.  zTab 
209a1 3d 20 26 70 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31  = &pNew->zName[1
209a2 36 5d 3b 20 20 2f 2a 20 53 6b 69 70 20 74 68 65  6];  /* Skip the
209a3 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74 61   "sqlite_alterta
209a4 62 5f 22 20 70 72 65 66 69 78 20 6f 6e 20 74 68  b_" prefix on th
209a5 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c  e name */.  pCol
209a6 20 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70   = &pNew->aCol[p
209a7 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20  New->nCol-1];.  
209a8 70 44 66 6c 74 20 3d 20 70 43 6f 6c 2d 3e 70 44  pDflt = pCol->pD
209a9 66 6c 74 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  flt;.  pTab = sq
209aa 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
209ab 62 2c 20 7a 54 61 62 2c 20 7a 44 62 29 3b 0a 20  b, zTab, zDb);. 
209ac 20 61 73 73 65 72 74 28 20 70 54 61 62 20 29 3b   assert( pTab );
209ad 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
209ae 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
209af 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ION.  /* Invoke 
209b0 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  the authorizatio
209b1 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20  n callback. */. 
209b2 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
209b3 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
209b4 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45  LITE_ALTER_TABLE
209b5 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61  , zDb, pTab->zNa
209b6 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65  me, 0) ){.    re
209b7 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
209b8 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65  ..  /* If the de
209b9 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
209ba 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 77  the new column w
209bb 61 73 20 73 70 65 63 69 66 69 65 64 20 77 69 74  as specified wit
209bc 68 20 61 20 0a 20 20 2a 2a 20 6c 69 74 65 72 61  h a .  ** litera
209bd 6c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74  l NULL, then set
209be 20 70 44 66 6c 74 20 74 6f 20 30 2e 20 54 68 69   pDflt to 0. Thi
209bf 73 20 73 69 6d 70 6c 69 66 69 65 73 20 63 68 65  s simplifies che
209c0 63 6b 69 6e 67 0a 20 20 2a 2a 20 66 6f 72 20 61  cking.  ** for a
209c1 6e 20 53 51 4c 20 4e 55 4c 4c 20 64 65 66 61 75  n SQL NULL defau
209c2 6c 74 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20  lt below..  */. 
209c3 20 69 66 28 20 70 44 66 6c 74 20 26 26 20 70 44   if( pDflt && pD
209c4 66 6c 74 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c  flt->op==TK_NULL
209c5 20 29 7b 0a 20 20 20 20 70 44 66 6c 74 20 3d 20   ){.    pDflt = 
209c6 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  0;.  }..  /* Che
209c7 63 6b 20 74 68 61 74 20 74 68 65 20 6e 65 77 20  ck that the new 
209c8 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 73 70  column is not sp
209c9 65 63 69 66 69 65 64 20 61 73 20 50 52 49 4d 41  ecified as PRIMA
209ca 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45  RY KEY or UNIQUE
209cb 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20  ..  ** If there 
209cc 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  is a NOT NULL co
209cd 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 6e 20 74  nstraint, then t
209ce 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
209cf 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 63 6f   for the.  ** co
209d0 6c 75 6d 6e 20 6d 75 73 74 20 6e 6f 74 20 62 65  lumn must not be
209d1 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66   NULL..  */.  if
209d2 28 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65  ( pCol->isPrimKe
209d3 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  y ){.    sqlite3
209d4 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
209d5 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 50   "Cannot add a P
209d6 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
209d7 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  n");.    return;
209d8 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e 65 77 2d  .  }.  if( pNew-
209d9 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73  >pIndex ){.    s
209da 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
209db 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61  Parse, "Cannot a
209dc 64 64 20 61 20 55 4e 49 51 55 45 20 63 6f 6c 75  dd a UNIQUE colu
209dd 6d 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  mn");.    return
209de 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c  ;.  }.  if( pCol
209df 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44  ->notNull && !pD
209e0 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  flt ){.    sqlit
209e1 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
209e2 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61 6e  e, .        "Can
209e3 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e 55  not add a NOT NU
209e4 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64  LL column with d
209e5 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c  efault value NUL
209e6 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  L");.    return;
209e7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72  .  }..  /* Ensur
209e8 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65 78  e the default ex
209e9 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65  pression is some
209ea 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69 74  thing that sqlit
209eb 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
209ec 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c  ).  ** can handl
209ed 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52  e (i.e. not CURR
209ee 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20  ENT_TIME etc.). 
209ef 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20   */.  if( pDflt 
209f0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
209f1 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20  alue *pVal;.    
209f2 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75 65  if( sqlite3Value
209f3 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 66  FromExpr(db, pDf
209f4 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  lt, SQLITE_UTF8,
209f5 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
209f6 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20  , &pVal) ){.    
209f7 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
209f8 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65  ed = 1;.      re
209f9 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
209fa 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20 20  if( !pVal ){.   
209fb 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
209fc 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e  sg(pParse, "Cann
209fd 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20  ot add a column 
209fe 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e  with non-constan
209ff 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20 20  t default");.   
20a00 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
20a01 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
20a02 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d  eFree(pVal);.  }
20a03 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68  ..  /* Modify th
20a04 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  e CREATE TABLE s
20a05 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a  tatement. */.  z
20a06 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Col = sqlite3DbS
20a07 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
20a08 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43  *)pColDef->z, pC
20a09 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28  olDef->n);.  if(
20a0a 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61   zCol ){.    cha
20a0b 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b  r *zEnd = &zCol[
20a0c 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20  pColDef->n-1];. 
20a0d 20 20 20 77 68 69 6c 65 28 20 7a 45 6e 64 3e 7a     while( zEnd>z
20a0e 43 6f 6c 20 26 26 20 28 2a 7a 45 6e 64 3d 3d 27  Col && (*zEnd=='
20a0f 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 73  ;' || sqlite3Iss
20a10 70 61 63 65 28 2a 7a 45 6e 64 29 29 20 29 7b 0a  pace(*zEnd)) ){.
20a11 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20        *zEnd-- = 
20a12 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20  '\0';.    }.    
20a13 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
20a14 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  se(pParse, .    
20a15 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77      "UPDATE \"%w
20a16 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20 20 20  \".%s SET ".    
20a17 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 75 62        "sql = sub
20a18 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c  str(sql,1,%d) ||
20a19 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73   ', ' || %Q || s
20a1a 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20 22 0a  ubstr(sql,%d) ".
20a1b 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20 74          "WHERE t
20a1c 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e  ype = 'table' AN
20a1d 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20 0a 20  D name = %Q", . 
20a1e 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41       zDb, SCHEMA
20a1f 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 4e 65  _TABLE(iDb), pNe
20a20 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 2c  w->addColOffset,
20a21 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64 64   zCol, pNew->add
20a22 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20 20 20  ColOffset+1,.   
20a23 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b 0a 20     zTab.    );. 
20a24 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
20a25 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a  (db, zCol);.  }.
20a26 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66  .  /* If the def
20a27 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68  ault value of th
20a28 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20  e new column is 
20a29 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 74  NULL, then set t
20a2a 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72  he file.  ** for
20a2b 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 74 68 65  mat to 2. If the
20a2c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f   default value o
20a2d 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  f the new column
20a2e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20   is not NULL,.  
20a2f 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d  ** the file form
20a30 61 74 20 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20  at becomes 3..  
20a31 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d 69 6e 69  */.  sqlite3Mini
20a32 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 70 50  mumFileFormat(pP
20a33 61 72 73 65 2c 20 69 44 62 2c 20 70 44 66 6c 74  arse, iDb, pDflt
20a34 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a   ? 3 : 2);..  /*
20a35 20 52 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65   Reload the sche
20a36 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 69 66 69  ma of the modifi
20a37 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72  ed table. */.  r
20a38 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61  eloadTableSchema
20a39 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
20a3a 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a  Tab->zName);.}..
20a3b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
20a3c 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
20a3d 20 74 68 65 20 70 61 72 73 65 72 20 61 66 74 65   the parser afte
20a3e 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65  r the table-name
20a3f 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52   in.** an "ALTER
20a40 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61   TABLE <table-na
20a41 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65 6d 65  me> ADD" stateme
20a42 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20 41 72  nt is parsed. Ar
20a43 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20  gument .** pSrc 
20a44 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65  is the full-name
20a45 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   of the table be
20a46 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a  ing altered..**.
20a47 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
20a48 6d 61 6b 65 73 20 61 20 28 70 61 72 74 69 61 6c  makes a (partial
20a49 29 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61  ) copy of the Ta
20a4a 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  ble structure.**
20a4b 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 62   for the table b
20a4c 65 69 6e 67 20 61 6c 74 65 72 65 64 20 61 6e 64  eing altered and
20a4d 20 73 65 74 73 20 50 61 72 73 65 2e 70 4e 65 77   sets Parse.pNew
20a4e 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a  Table to point.*
20a4f 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65  * to it. Routine
20a50 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
20a51 70 61 72 73 65 72 20 61 73 20 74 68 65 20 63 6f  parser as the co
20a52 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a  lumn definition.
20a53 2a 2a 20 69 73 20 70 61 72 73 65 64 20 28 69 2e  ** is parsed (i.
20a54 65 2e 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  e. sqlite3AddCol
20a55 75 6d 6e 28 29 29 20 61 64 64 20 74 68 65 20 6e  umn()) add the n
20a56 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74  ew Column data t
20a57 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20  o .** the copy. 
20a58 54 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20  The copy of the 
20a59 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
20a5a 69 73 20 64 65 6c 65 74 65 64 20 62 79 20 74 6f  is deleted by to
20a5b 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74  kenize.c .** aft
20a5c 65 72 20 70 61 72 73 69 6e 67 20 69 73 20 66 69  er parsing is fi
20a5d 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f  nished..**.** Ro
20a5e 75 74 69 6e 65 20 73 71 6c 69 74 65 33 41 6c 74  utine sqlite3Alt
20a5f 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d  erFinishAddColum
20a60 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  n() will be call
20a61 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a  ed to complete.*
20a62 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 22 41 4c  * coding the "AL
20a63 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44  TER TABLE ... AD
20a64 44 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  D" statement..*/
20a65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20a66 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
20a67 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28  rBeginAddColumn(
20a68 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
20a69 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20  rcList *pSrc){. 
20a6a 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20   Table *pNew;.  
20a6b 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56  Table *pTab;.  V
20a6c 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44  dbe *v;.  int iD
20a6d 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  b;.  int i;.  in
20a6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69  t nAlloc;.  sqli
20a6f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
20a70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b  ->db;..  /* Look
20a71 20 75 70 20 74 68 65 20 74 61 62 6c 65 20 62 65   up the table be
20a72 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a  ing altered. */.
20a73 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
20a74 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29  ->pNewTable==0 )
20a75 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
20a76 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
20a77 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20  Mutexes(db) );. 
20a78 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
20a79 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65 78 69  ailed ) goto exi
20a7a 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75  t_begin_add_colu
20a7b 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c  mn;.  pTab = sql
20a7c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
20a7d 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63 2d  pParse, 0, pSrc-
20a7e 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72  >a[0].zName, pSr
20a7f 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73  c->a[0].zDatabas
20a80 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20  e);.  if( !pTab 
20a81 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69  ) goto exit_begi
20a82 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23  n_add_column;..#
20a83 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20a84 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
20a85 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
20a86 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c  pTab) ){.    sql
20a87 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
20a88 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61  rse, "virtual ta
20a89 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  bles may not be 
20a8a 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20 20 67  altered");.    g
20a8b 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
20a8c 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23  dd_column;.  }.#
20a8d 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65  endif..  /* Make
20a8e 20 73 75 72 65 20 74 68 69 73 20 69 73 20 6e 6f   sure this is no
20a8f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  t an attempt to 
20a90 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20 2a 2f  ALTER a view. */
20a91 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
20a92 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
20a93 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
20a94 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20  se, "Cannot add 
20a95 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69  a column to a vi
20a96 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ew");.    goto e
20a97 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
20a98 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  lumn;.  }..  ass
20a99 65 72 74 28 20 70 54 61 62 2d 3e 61 64 64 43 6f  ert( pTab->addCo
20a9a 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20 20 69  lOffset>0 );.  i
20a9b 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
20a9c 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
20a9d 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20  ab->pSchema);.. 
20a9e 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79 20 6f   /* Put a copy o
20a9f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  f the Table stru
20aa0 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e 65 77  ct in Parse.pNew
20aa1 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a 20 20  Table for the.  
20aa2 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  ** sqlite3AddCol
20aa3 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 61  umn() function a
20aa4 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f  nd friends to mo
20aa5 64 69 66 79 2e 20 20 42 75 74 20 6d 6f 64 69 66  dify.  But modif
20aa6 79 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20  y.  ** the name 
20aa7 62 79 20 61 64 64 69 6e 67 20 61 6e 20 22 73 71  by adding an "sq
20aa8 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20  lite_altertab_" 
20aa9 70 72 65 66 69 78 2e 20 20 42 79 20 61 64 64 69  prefix.  By addi
20aaa 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65  ng this.  ** pre
20aab 66 69 78 2c 20 77 65 20 69 6e 73 75 72 65 20 74  fix, we insure t
20aac 68 61 74 20 74 68 65 20 6e 61 6d 65 20 77 69 6c  hat the name wil
20aad 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69  l not collide wi
20aae 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20  th an existing. 
20aaf 20 2a 2a 20 74 61 62 6c 65 20 62 65 63 61 75 73   ** table becaus
20ab0 65 20 75 73 65 72 20 74 61 62 6c 65 20 61 72 65  e user table are
20ab1 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20   not allowed to 
20ab2 68 61 76 65 20 74 68 65 20 22 73 71 6c 69 74 65  have the "sqlite
20ab3 5f 22 0a 20 20 2a 2a 20 70 72 65 66 69 78 20 6f  _".  ** prefix o
20ab4 6e 20 74 68 65 69 72 20 6e 61 6d 65 2e 0a 20 20  n their name..  
20ab5 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54 61 62  */.  pNew = (Tab
20ab6 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  le*)sqlite3DbMal
20ab7 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
20ab8 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 66  of(Table));.  if
20ab9 28 20 21 70 4e 65 77 20 29 20 67 6f 74 6f 20 65  ( !pNew ) goto e
20aba 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
20abb 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e  lumn;.  pParse->
20abc 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e 65 77  pNewTable = pNew
20abd 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d  ;.  pNew->nRef =
20abe 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 64 62 4d 65   1;.  pNew->dbMe
20abf 6d 20 3d 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3b  m = pTab->dbMem;
20ac0 0a 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20  .  pNew->nCol = 
20ac1 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73  pTab->nCol;.  as
20ac2 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c  sert( pNew->nCol
20ac3 3e 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d  >0 );.  nAlloc =
20ac4 20 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31   (((pNew->nCol-1
20ac5 29 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73  )/8)*8)+8;.  ass
20ac6 65 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65  ert( nAlloc>=pNe
20ac7 77 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f  w->nCol && nAllo
20ac8 63 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63  c%8==0 && nAlloc
20ac9 2d 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b  -pNew->nCol<8 );
20aca 0a 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20  .  pNew->aCol = 
20acb 28 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33  (Column*)sqlite3
20acc 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
20acd 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a   sizeof(Column)*
20ace 6e 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d  nAlloc);.  pNew-
20acf 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
20ad0 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c  MPrintf(db, "sql
20ad1 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 25 73 22  ite_altertab_%s"
20ad2 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
20ad3 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f    if( !pNew->aCo
20ad4 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d  l || !pNew->zNam
20ad5 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  e ){.    db->mal
20ad6 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
20ad7 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67     goto exit_beg
20ad8 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20  in_add_column;. 
20ad9 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77   }.  memcpy(pNew
20ada 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43  ->aCol, pTab->aC
20adb 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d  ol, sizeof(Colum
20adc 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a  n)*pNew->nCol);.
20add 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65    for(i=0; i<pNe
20ade 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  w->nCol; i++){. 
20adf 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20     Column *pCol 
20ae0 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d  = &pNew->aCol[i]
20ae1 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  ;.    pCol->zNam
20ae2 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
20ae3 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e  Dup(db, pCol->zN
20ae4 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e  ame);.    pCol->
20ae5 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70  zColl = 0;.    p
20ae6 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a  Col->zType = 0;.
20ae7 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20      pCol->pDflt 
20ae8 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d  = 0;.  }.  pNew-
20ae9 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61  >pSchema = db->a
20aea 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b  Db[iDb].pSchema;
20aeb 0a 20 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f  .  pNew->addColO
20aec 66 66 73 65 74 20 3d 20 70 54 61 62 2d 3e 61 64  ffset = pTab->ad
20aed 64 43 6f 6c 4f 66 66 73 65 74 3b 0a 20 20 70 4e  dColOffset;.  pN
20aee 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 0a 20  ew->nRef = 1;.. 
20aef 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e   /* Begin a tran
20af0 73 61 63 74 69 6f 6e 20 61 6e 64 20 69 6e 63 72  saction and incr
20af1 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61  ement the schema
20af2 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a 20 20 73   cookie.  */.  s
20af3 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
20af4 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
20af5 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 76 20 3d  , 0, iDb);.  v =
20af6 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
20af7 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21  pParse);.  if( !
20af8 76 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65  v ) goto exit_be
20af9 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
20afa 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
20afb 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44  ookie(pParse, iD
20afc 62 29 3b 0a 0a 65 78 69 74 5f 62 65 67 69 6e 5f  b);..exit_begin_
20afd 61 64 64 5f 63 6f 6c 75 6d 6e 3a 0a 20 20 73 71  add_column:.  sq
20afe 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
20aff 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20  te(db, pSrc);.  
20b00 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66  return;.}.#endif
20b01 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c 54 45    /* SQLITE_ALTE
20b02 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a  R_TABLE */../***
20b03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
20b04 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a  of alter.c *****
20b05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
20b08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
20b09 6e 20 66 69 6c 65 20 61 6e 61 6c 79 7a 65 2e 63  n file analyze.c
20b0a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
20b0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
20b0d 2a 20 32 30 30 35 20 4a 75 6c 79 20 38 0a 2a 2a  * 2005 July 8.**
20b0e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
20b0f 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
20b10 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
20b11 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
20b12 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
20b13 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
20b14 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
20b15 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
20b16 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
20b17 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
20b18 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
20b19 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
20b1a 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
20b1b 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
20b1c 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
20b1d 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
20b1e 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
20b1f 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
20b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
20b24 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
20b25 6e 73 20 63 6f 64 65 20 61 73 73 6f 63 69 61 74  ns code associat
20b26 65 64 20 77 69 74 68 20 74 68 65 20 41 4e 41 4c  ed with the ANAL
20b27 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  YZE command..**.
20b28 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 61 6e 61  ** @(#) $Id: ana
20b29 6c 79 7a 65 2e 63 2c 76 20 31 2e 35 32 20 32 30  lyze.c,v 1.52 20
20b2a 30 39 2f 30 34 2f 31 36 20 31 37 3a 34 35 3a 34  09/04/16 17:45:4
20b2b 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23  8 drh Exp $.*/.#
20b2c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20b2d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a 0a 2a  IT_ANALYZE../*.*
20b2e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
20b2f 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 68  enerates code th
20b30 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73 71 6c  at opens the sql
20b31 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
20b32 6f 6e 20 63 75 72 73 6f 72 0a 2a 2a 20 69 53 74  on cursor.** iSt
20b33 61 74 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  atCur..**.** If 
20b34 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
20b35 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74   tables does not
20b36 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73   previously exis
20b37 74 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64  t, it is created
20b38 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20  ..** If it does 
20b39 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74  previously exist
20b3a 2c 20 61 6c 6c 20 65 6e 74 69 72 65 73 20 61 73  , all entires as
20b3b 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 61  sociated with ta
20b3c 62 6c 65 20 7a 57 68 65 72 65 0a 2a 2a 20 61 72  ble zWhere.** ar
20b3d 65 20 72 65 6d 6f 76 65 64 2e 20 20 49 66 20 7a  e removed.  If z
20b3e 57 68 65 72 65 3d 3d 30 20 74 68 65 6e 20 61 6c  Where==0 then al
20b3f 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20 72 65  l entries are re
20b40 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  moved..*/.static
20b41 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74 54 61   void openStatTa
20b42 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
20b43 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
20b44 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
20b45 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20  t */.  int iDb, 
20b46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
20b47 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 77  * The database w
20b48 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 69 6e  e are looking in
20b49 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 43   */.  int iStatC
20b4a 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ur,           /*
20b4b 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65   Open the sqlite
20b4c 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f 6e 20  _stat1 table on 
20b4d 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20  this cursor */. 
20b4e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57 68   const char *zWh
20b4f 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65 6c 65  ere      /* Dele
20b50 74 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63  te entries assoc
20b51 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
20b52 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 71  table */.){.  sq
20b53 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
20b54 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44  se->db;.  Db *pD
20b55 62 3b 0a 20 20 69 6e 74 20 69 52 6f 6f 74 50 61  b;.  int iRootPa
20b56 67 65 3b 0a 20 20 75 38 20 63 72 65 61 74 65 53  ge;.  u8 createS
20b57 74 61 74 31 20 3d 20 30 3b 0a 20 20 54 61 62 6c  tat1 = 0;.  Tabl
20b58 65 20 2a 70 53 74 61 74 3b 0a 20 20 56 64 62 65  e *pStat;.  Vdbe
20b59 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
20b5a 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 0a 20  Vdbe(pParse);.. 
20b5b 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
20b5c 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  rn;.  assert( sq
20b5d 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
20b5e 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b  llMutexes(db) );
20b5f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
20b60 65 33 56 64 62 65 44 62 28 76 29 3d 3d 64 62 20  e3VdbeDb(v)==db 
20b61 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  );.  pDb = &db->
20b62 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66 28 20  aDb[iDb];.  if( 
20b63 28 70 53 74 61 74 20 3d 20 73 71 6c 69 74 65 33  (pStat = sqlite3
20b64 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73  FindTable(db, "s
20b65 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 70 44  qlite_stat1", pD
20b66 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30 20 29 7b  b->zName))==0 ){
20b67 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69  .    /* The sqli
20b68 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 73 20  te_stat1 tables 
20b69 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20  does not exist. 
20b6a 20 43 72 65 61 74 65 20 69 74 2e 20 20 0a 20 20   Create it.  .  
20b6b 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 61    ** Note that a
20b6c 20 73 69 64 65 2d 65 66 66 65 63 74 20 6f 66 20   side-effect of 
20b6d 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
20b6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 6f   statement is to
20b6f 20 6c 65 61 76 65 0a 20 20 20 20 2a 2a 20 74 68   leave.    ** th
20b70 65 20 72 6f 6f 74 70 61 67 65 20 6f 66 20 74 68  e rootpage of th
20b71 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 72  e new table in r
20b72 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e  egister pParse->
20b73 72 65 67 52 6f 6f 74 2e 20 20 54 68 69 73 20 69  regRoot.  This i
20b74 73 0a 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 61  s.    ** importa
20b75 6e 74 20 62 65 63 61 75 73 65 20 74 68 65 20 4f  nt because the O
20b76 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64 65 20  penWrite opcode 
20b77 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20 6e 65  below will be ne
20b78 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 20  eding it. */.   
20b79 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
20b7a 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
20b7b 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20    "CREATE TABLE 
20b7c 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 28  %Q.sqlite_stat1(
20b7d 74 62 6c 2c 69 64 78 2c 73 74 61 74 29 22 2c 0a  tbl,idx,stat)",.
20b7e 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65        pDb->zName
20b7f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 52 6f 6f  .    );.    iRoo
20b80 74 50 61 67 65 20 3d 20 70 50 61 72 73 65 2d 3e  tPage = pParse->
20b81 72 65 67 52 6f 6f 74 3b 0a 20 20 20 20 63 72 65  regRoot;.    cre
20b82 61 74 65 53 74 61 74 31 20 3d 20 31 3b 20 20 2f  ateStat1 = 1;  /
20b83 2a 20 43 61 75 73 65 20 72 6f 6f 74 70 61 67 65  * Cause rootpage
20b84 20 74 6f 20 62 65 20 74 61 6b 65 6e 20 66 72 6f   to be taken fro
20b85 6d 20 74 6f 70 20 6f 66 20 73 74 61 63 6b 20 2a  m top of stack *
20b86 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 57  /.  }else if( zW
20b87 68 65 72 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54  here ){.    /* T
20b88 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
20b89 74 61 62 6c 65 20 65 78 69 73 74 73 2e 20 20 44  table exists.  D
20b8a 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72 69 65  elete all entrie
20b8b 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
20b8c 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61 62  h.    ** the tab
20b8d 6c 65 20 7a 57 68 65 72 65 2e 20 2a 2f 0a 20 20  le zWhere. */.  
20b8e 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
20b8f 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
20b90 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
20b91 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31   %Q.sqlite_stat1
20b92 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22 2c 0a   WHERE tbl=%Q",.
20b93 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d         pDb->zNam
20b94 65 2c 20 7a 57 68 65 72 65 0a 20 20 20 20 29 3b  e, zWhere.    );
20b95 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 65 20 3d  .    iRootPage =
20b96 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20   pStat->tnum;.  
20b97 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
20b98 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
20b99 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69  able already exi
20b9a 73 74 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c  sts.  Delete all
20b9b 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 20 69 52   rows. */.    iR
20b9c 6f 6f 74 50 61 67 65 20 3d 20 70 53 74 61 74 2d  ootPage = pStat-
20b9d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >tnum;.    sqlit
20b9e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
20b9f 4f 50 5f 43 6c 65 61 72 2c 20 70 53 74 61 74 2d  OP_Clear, pStat-
20ba0 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d  >tnum, iDb);.  }
20ba1 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
20ba2 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
20ba3 6c 65 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20  le for writing. 
20ba4 55 6e 6c 65 73 73 20 69 74 20 77 61 73 20 63 72  Unless it was cr
20ba5 65 61 74 65 64 0a 20 20 2a 2a 20 62 79 20 74 68  eated.  ** by th
20ba6 69 73 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c  is vdbe program,
20ba7 20 6c 6f 63 6b 20 69 74 20 66 6f 72 20 77 72 69   lock it for wri
20ba8 74 69 6e 67 20 61 74 20 74 68 65 20 73 68 61 72  ting at the shar
20ba9 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c 2e 20  ed-cache level. 
20baa 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 76 64  .  ** If this vd
20bab 62 65 20 64 69 64 20 63 72 65 61 74 65 20 74 68  be did create th
20bac 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
20bad 61 62 6c 65 2c 20 74 68 65 6e 20 69 74 20 6d 75  able, then it mu
20bae 73 74 20 68 61 76 65 20 0a 20 20 2a 2a 20 61 6c  st have .  ** al
20baf 72 65 61 64 79 20 6f 62 74 61 69 6e 65 64 20 61  ready obtained a
20bb0 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 2c 20 6d 61   schema-lock, ma
20bb1 6b 69 6e 67 20 74 68 65 20 77 72 69 74 65 2d 6c  king the write-l
20bb2 6f 63 6b 20 72 65 64 75 6e 64 61 6e 74 2e 0a 20  ock redundant.. 
20bb3 20 2a 2f 0a 20 20 69 66 28 20 21 63 72 65 61 74   */.  if( !creat
20bb4 65 53 74 61 74 31 20 29 7b 0a 20 20 20 20 73 71  eStat1 ){.    sq
20bb5 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70  lite3TableLock(p
20bb6 50 61 72 73 65 2c 20 69 44 62 2c 20 69 52 6f 6f  Parse, iDb, iRoo
20bb7 74 50 61 67 65 2c 20 31 2c 20 22 73 71 6c 69 74  tPage, 1, "sqlit
20bb8 65 5f 73 74 61 74 31 22 29 3b 0a 20 20 7d 0a 20  e_stat1");.  }. 
20bb9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20bba 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p3(v, OP_OpenWri
20bbb 74 65 2c 20 69 53 74 61 74 43 75 72 2c 20 69 52  te, iStatCur, iR
20bbc 6f 6f 74 50 61 67 65 2c 20 69 44 62 29 3b 0a 20  ootPage, iDb);. 
20bbd 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
20bbe 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61  geP4(v, -1, (cha
20bbf 72 20 2a 29 33 2c 20 50 34 5f 49 4e 54 33 32 29  r *)3, P4_INT32)
20bc0 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
20bc1 68 61 6e 67 65 50 35 28 76 2c 20 63 72 65 61 74  hangeP5(v, creat
20bc2 65 53 74 61 74 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  eStat1);.}../*.*
20bc3 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
20bc4 74 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69  to do an analysi
20bc5 73 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73  s of all indices
20bc6 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
20bc7 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62  .** a single tab
20bc8 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
20bc9 69 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62  id analyzeOneTab
20bca 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
20bcb 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72  rse,   /* Parser
20bcc 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
20bcd 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f  ble *pTab,     /
20bce 2a 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e  * Table whose in
20bcf 64 69 63 65 73 20 61 72 65 20 74 6f 20 62 65 20  dices are to be 
20bd0 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e  analyzed */.  in
20bd1 74 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f  t iStatCur,    /
20bd2 2a 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43  * Index of VdbeC
20bd3 75 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65  ursor that write
20bd4 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  s the sqlite_sta
20bd5 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  t1 table */.  in
20bd6 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f  t iMem         /
20bd7 2a 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f  * Available memo
20bd8 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67  ry locations beg
20bd9 69 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  in here */.){.  
20bda 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
20bdb 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20   /* An index to 
20bdc 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a  being analyzed *
20bdd 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b  /.  int iIdxCur;
20bde 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
20bdf 20 56 64 62 65 43 75 72 73 6f 72 20 66 6f 72 20   VdbeCursor for 
20be0 69 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61 6c  index being anal
20be1 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  yzed */.  int nC
20be2 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ol;        /* Nu
20be3 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
20be4 69 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  in the index */.
20be5 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
20be6 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
20be7 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20  l machine being 
20be8 62 75 69 6c 74 20 75 70 20 2a 2f 0a 20 20 69 6e  built up */.  in
20be9 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f  t i;           /
20bea 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
20beb 2f 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f  /.  int topOfLoo
20bec 70 3b 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20  p;   /* The top 
20bed 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
20bee 20 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20   int endOfLoop; 
20bef 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20    /* The end of 
20bf0 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
20bf1 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f  t addr;        /
20bf2 2a 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66  * The address of
20bf3 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   an instruction 
20bf4 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
20bf5 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
20bf6 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  f database conta
20bf7 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 0a 20  ining pTab */.. 
20bf8 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
20bf9 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
20bfa 66 28 20 76 3d 3d 30 20 7c 7c 20 4e 45 56 45 52  f( v==0 || NEVER
20bfb 28 70 54 61 62 3d 3d 30 29 20 7c 7c 20 70 54 61  (pTab==0) || pTa
20bfc 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a  b->pIndex==0 ){.
20bfd 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61      /* Do no ana
20bfe 6c 79 73 69 73 20 66 6f 72 20 74 61 62 6c 65 73  lysis for tables
20bff 20 74 68 61 74 20 68 61 76 65 20 6e 6f 20 69 6e   that have no in
20c00 64 69 63 65 73 20 2a 2f 0a 20 20 20 20 72 65 74  dices */.    ret
20c01 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
20c02 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
20c03 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70  oldsAllMutexes(p
20c04 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20  Parse->db) );.  
20c05 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
20c06 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
20c07 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
20c08 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28  hema);.  assert(
20c09 20 69 44 62 3e 3d 30 20 29 3b 0a 23 69 66 6e 64   iDb>=0 );.#ifnd
20c0a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
20c0b 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69  UTHORIZATION.  i
20c0c 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
20c0d 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
20c0e 54 45 5f 41 4e 41 4c 59 5a 45 2c 20 70 54 61 62  TE_ANALYZE, pTab
20c0f 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20  ->zName, 0,.    
20c10 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44    pParse->db->aD
20c11 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29  b[iDb].zName ) )
20c12 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
20c13 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45  }.#endif..  /* E
20c14 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 2d  stablish a read-
20c15 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c  lock on the tabl
20c16 65 20 61 74 20 74 68 65 20 73 68 61 72 65 64 2d  e at the shared-
20c17 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a  cache level. */.
20c18 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
20c19 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  ck(pParse, iDb, 
20c1a 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70  pTab->tnum, 0, p
20c1b 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20  Tab->zName);..  
20c1c 69 49 64 78 43 75 72 20 3d 20 70 50 61 72 73 65  iIdxCur = pParse
20c1d 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28  ->nTab++;.  for(
20c1e 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
20c1f 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
20c20 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
20c21 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20  KeyInfo *pKey = 
20c22 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
20c23 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78  nfo(pParse, pIdx
20c24 29 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 46 69  );.    int regFi
20c25 65 6c 64 73 3b 20 20 20 20 2f 2a 20 52 65 67 69  elds;    /* Regi
20c26 73 74 65 72 20 62 6c 6f 63 6b 20 66 6f 72 20 62  ster block for b
20c27 75 69 6c 64 69 6e 67 20 72 65 63 6f 72 64 73 20  uilding records 
20c28 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 52 65  */.    int regRe
20c29 63 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  c;       /* Regi
20c2a 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6d  ster holding com
20c2b 70 6c 65 74 65 64 20 72 65 63 6f 72 64 20 2a 2f  pleted record */
20c2c 0a 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70  .    int regTemp
20c2d 3b 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72  ;      /* Tempor
20c2e 61 72 79 20 75 73 65 20 72 65 67 69 73 74 65 72  ary use register
20c2f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 43   */.    int regC
20c30 6f 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  ol;       /* Con
20c31 74 65 6e 74 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  tent of a column
20c32 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20   from the table 
20c33 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a  being analyzed *
20c34 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 52 6f 77  /.    int regRow
20c35 69 64 3b 20 20 20 20 20 2f 2a 20 52 6f 77 69 64  id;     /* Rowid
20c36 20 66 6f 72 20 74 68 65 20 69 6e 73 65 72 74 65   for the inserte
20c37 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  d record */.    
20c38 69 6e 74 20 72 65 67 46 32 3b 0a 0a 20 20 20 20  int regF2;..    
20c39 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72 73 6f 72  /* Open a cursor
20c3a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 74 6f   to the index to
20c3b 20 62 65 20 61 6e 61 6c 79 7a 65 64 0a 20 20 20   be analyzed.   
20c3c 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
20c3d 69 44 62 3d 3d 73 71 6c 69 74 65 33 53 63 68 65  iDb==sqlite3Sche
20c3e 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
20c3f 2d 3e 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68  ->db, pIdx->pSch
20c40 65 6d 61 29 20 29 3b 0a 20 20 20 20 6e 43 6f 6c  ema) );.    nCol
20c41 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   = pIdx->nColumn
20c42 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
20c43 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70  eAddOp4(v, OP_Op
20c44 65 6e 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c  enRead, iIdxCur,
20c45 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62   pIdx->tnum, iDb
20c46 2c 0a 20 20 20 20 20 20 20 20 28 63 68 61 72 20  ,.        (char 
20c47 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
20c48 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
20c49 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
20c4a 20 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61   "%s", pIdx->zNa
20c4b 6d 65 29 29 3b 0a 20 20 20 20 72 65 67 46 69 65  me));.    regFie
20c4c 6c 64 73 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a  lds = iMem+nCol*
20c4d 32 3b 0a 20 20 20 20 72 65 67 54 65 6d 70 20 3d  2;.    regTemp =
20c4e 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65 67 43   regRowid = regC
20c4f 6f 6c 20 3d 20 72 65 67 46 69 65 6c 64 73 2b 33  ol = regFields+3
20c50 3b 0a 20 20 20 20 72 65 67 52 65 63 20 3d 20 72  ;.    regRec = r
20c51 65 67 43 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28  egCol+1;.    if(
20c52 20 72 65 67 52 65 63 3e 70 50 61 72 73 65 2d 3e   regRec>pParse->
20c53 6e 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 50  nMem ){.      pP
20c54 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67  arse->nMem = reg
20c55 52 65 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  Rec;.    }..    
20c56 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  /* Memory cells 
20c57 61 72 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c  are used as foll
20c58 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ows:.    **.    
20c59 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a  **    mem[iMem]:
20c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65               The
20c5b 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
20c5c 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62   rows in the tab
20c5d 6c 65 2e 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65  le..    **    me
20c5e 6d 5b 69 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20  m[iMem+1]:      
20c5f 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64       Number of d
20c60 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 20 69  istinct values i
20c61 6e 20 63 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a  n column 1.    *
20c62 2a 20 20 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20  *    ....    ** 
20c63 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c     mem[iMem+nCol
20c64 5d 3a 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72  ]:        Number
20c65 20 6f 66 20 64 69 73 74 69 6e 63 74 20 76 61 6c   of distinct val
20c66 75 65 73 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a  ues in column N.
20c67 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d      **    mem[iM
20c68 65 6d 2b 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20  em+nCol+1]      
20c69 20 4c 61 73 74 20 6f 62 73 65 72 76 65 64 20 76   Last observed v
20c6a 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31  alue of column 1
20c6b 0a 20 20 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20  .    **    .... 
20c6c 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65     **    mem[iMe
20c6d 6d 2b 6e 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20  m+nCol+nCol]:   
20c6e 4c 61 73 74 20 6f 62 73 65 72 76 65 64 20 76 61  Last observed va
20c6f 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a  lue of column N.
20c70 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65      **.    ** Ce
20c71 6c 6c 73 20 69 4d 65 6d 20 74 68 72 6f 75 67 68  lls iMem through
20c72 20 69 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69   iMem+nCol are i
20c73 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e  nitialized to 0.
20c74 20 20 54 68 65 20 6f 74 68 65 72 73 0a 20 20 20    The others.   
20c75 20 2a 2a 20 61 72 65 20 69 6e 69 74 69 61 6c 69   ** are initiali
20c76 7a 65 64 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20  zed to NULL..   
20c77 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
20c78 20 69 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a   i<=nCol; i++){.
20c79 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
20c7a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
20c7b 74 65 67 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69  teger, 0, iMem+i
20c7c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
20c7d 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
20c7e 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
20c7f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
20c80 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b  P_Null, 0, iMem+
20c81 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d  nCol+i+1);.    }
20c82 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20  ..    /* Do the 
20c83 61 6e 61 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f  analysis..    */
20c84 0a 20 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d  .    endOfLoop =
20c85 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
20c86 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 73 71  Label(v);.    sq
20c87 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
20c88 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49  v, OP_Rewind, iI
20c89 64 78 43 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70  dxCur, endOfLoop
20c8a 29 3b 0a 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70  );.    topOfLoop
20c8b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
20c8c 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
20c8d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20c8e 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
20c8f 2c 20 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20  , iMem, 1);.    
20c90 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
20c91 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
20c92 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
20c93 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64  , OP_Column, iId
20c94 78 43 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29  xCur, i, regCol)
20c95 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
20c96 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
20c97 4e 65 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69  Ne, regCol, 0, i
20c98 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20  Mem+nCol+i+1);. 
20c99 20 20 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a       /**** TODO:
20c9a 20 20 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20    add collating 
20c9b 73 65 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a  sequence *****/.
20c9c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
20c9d 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c  eChangeP5(v, SQL
20c9e 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
20c9f 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
20ca0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
20ca1 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f  OP_Goto, 0, endO
20ca2 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28  fLoop);.    for(
20ca3 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
20ca4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
20ca5 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
20ca6 74 6f 70 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69  topOfLoop + 2*(i
20ca7 20 2b 20 31 29 29 3b 0a 20 20 20 20 20 20 73 71   + 1));.      sq
20ca8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
20ca9 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d  v, OP_AddImm, iM
20caa 65 6d 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20  em+i+1, 1);.    
20cab 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20cac 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
20cad 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d  , iIdxCur, i, iM
20cae 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20  em+nCol+i+1);.  
20caf 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
20cb0 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
20cb1 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20  v, endOfLoop);. 
20cb2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20cb3 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
20cb4 20 69 49 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c   iIdxCur, topOfL
20cb5 6f 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oop);.    sqlite
20cb6 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
20cb7 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 43 75 72  P_Close, iIdxCur
20cb8 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65  );..    /* Store
20cb9 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 0a   the results.  .
20cba 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
20cbb 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 73 69  e result is a si
20cbc 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20  ngle row of the 
20cbd 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
20cbe 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20  le.  The first. 
20cbf 20 20 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e     ** two column
20cc0 73 20 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20  s are the names 
20cc1 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  of the table and
20cc2 20 69 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69   index.  The thi
20cc3 72 64 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a  rd column.    **
20cc4 20 69 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d   is a string com
20cc5 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20  posed of a list 
20cc6 6f 66 20 69 6e 74 65 67 65 72 20 73 74 61 74 69  of integer stati
20cc7 73 74 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a  stics about the.
20cc8 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54      ** index.  T
20cc9 68 65 20 66 69 72 73 74 20 69 6e 74 65 67 65 72  he first integer
20cca 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20   in the list is 
20ccb 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
20ccc 20 6f 66 20 65 6e 74 72 69 65 73 0a 20 20 20 20   of entries.    
20ccd 2a 2a 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  ** in the index.
20cce 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61    There is one a
20ccf 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65  dditional intege
20cd0 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f  r in the list fo
20cd1 72 20 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f  r each.    ** co
20cd2 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c  lumn of the tabl
20cd3 65 2e 20 20 54 68 69 73 20 61 64 64 69 74 69 6f  e.  This additio
20cd4 6e 61 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61  nal integer is a
20cd5 20 67 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61   guess of how ma
20cd6 6e 79 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f  ny.    ** rows o
20cd7 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 65 20  f the table the 
20cd8 69 6e 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63  index will selec
20cd9 74 2e 20 20 49 66 20 44 20 69 73 20 74 68 65 20  t.  If D is the 
20cda 63 6f 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63  count of distinc
20cdb 74 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20  t.    ** values 
20cdc 61 6e 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74  and K is the tot
20cdd 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  al number of row
20cde 73 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65  s, then the inte
20cdf 67 65 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a  ger is computed.
20ce0 20 20 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a      ** as:.    *
20ce1 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
20ce2 49 20 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20  I = (K+D-1)/D.  
20ce3 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b    **.    ** If K
20ce4 3d 3d 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72  ==0 then no entr
20ce5 79 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74  y is made into t
20ce6 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
20ce7 74 61 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20  table.  .    ** 
20ce8 49 66 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69  If K>0 then it i
20ce9 73 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73  s always the cas
20cea 65 20 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76  e the D>0 so div
20ceb 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20  ision by zero.  
20cec 20 20 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f    ** is never po
20ced 73 73 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20  ssible..    */. 
20cee 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
20cef 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
20cf0 50 5f 49 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a  P_IfNot, iMem);.
20cf1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20cf2 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
20cf3 6e 67 38 2c 20 30 2c 20 72 65 67 46 69 65 6c 64  ng8, 0, regField
20cf4 73 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  s, 0, pTab->zNam
20cf5 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  e, 0);.    sqlit
20cf6 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
20cf7 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72  OP_String8, 0, r
20cf8 65 67 46 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70  egFields+1, 0, p
20cf9 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Idx->zName, 0);.
20cfa 20 20 20 20 72 65 67 46 32 20 3d 20 72 65 67 46      regF2 = regF
20cfb 69 65 6c 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c  ields+2;.    sql
20cfc 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
20cfd 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d  , OP_SCopy, iMem
20cfe 2c 20 72 65 67 46 32 29 3b 0a 20 20 20 20 66 6f  , regF2);.    fo
20cff 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
20d00 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
20d01 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
20d02 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72  OP_String8, 0, r
20d03 65 67 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20  egTemp, 0, " ", 
20d04 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
20d05 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
20d06 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d  P_Concat, regTem
20d07 70 2c 20 72 65 67 46 32 2c 20 72 65 67 46 32 29  p, regF2, regF2)
20d08 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
20d09 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
20d0a 41 64 64 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b  Add, iMem, iMem+
20d0b 69 2b 31 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20  i+1, regTemp);. 
20d0c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20d0d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
20d0e 49 6d 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31  Imm, regTemp, -1
20d0f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
20d10 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
20d11 5f 44 69 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b  _Divide, iMem+i+
20d12 31 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54  1, regTemp, regT
20d13 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  emp);.      sqli
20d14 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
20d15 20 4f 50 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65   OP_ToInt, regTe
20d16 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  mp);.      sqlit
20d17 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
20d18 4f 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65  OP_Concat, regTe
20d19 6d 70 2c 20 72 65 67 46 32 2c 20 72 65 67 46 32  mp, regF2, regF2
20d1a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
20d1b 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
20d1c 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
20d1d 20 72 65 67 46 69 65 6c 64 73 2c 20 33 2c 20 72   regFields, 3, r
20d1e 65 67 52 65 63 2c 20 22 61 61 61 22 2c 20 30 29  egRec, "aaa", 0)
20d1f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
20d20 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
20d21 77 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72  wRowid, iStatCur
20d22 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
20d23 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20d24 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
20d25 20 69 53 74 61 74 43 75 72 2c 20 72 65 67 52 65   iStatCur, regRe
20d26 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  c, regRowid);.  
20d27 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
20d28 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f  ngeP5(v, OPFLAG_
20d29 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c  APPEND);.    sql
20d2a 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
20d2b 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d  (v, addr);.  }.}
20d2c 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
20d2d 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
20d2e 63 61 75 73 65 20 74 68 65 20 6d 6f 73 74 20 72  cause the most r
20d2f 65 63 65 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c  ecent index anal
20d30 79 73 69 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61  ysis to.** be la
20d31 6f 64 65 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e  oded into intern
20d32 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 20 77  al hash tables w
20d33 68 65 72 65 20 69 73 20 63 61 6e 20 62 65 20 75  here is can be u
20d34 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  sed..*/.static v
20d35 6f 69 64 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73  oid loadAnalysis
20d36 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
20d37 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65  int iDb){.  Vdbe
20d38 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
20d39 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
20d3a 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
20d3b 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
20d3c 2c 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69  , OP_LoadAnalysi
20d3d 73 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a  s, iDb);.  }.}..
20d3e 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
20d3f 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f  ode that will do
20d40 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20   an analysis of 
20d41 61 6e 20 65 6e 74 69 72 65 20 64 61 74 61 62 61  an entire databa
20d42 73 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  se.*/.static voi
20d43 64 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73  d analyzeDatabas
20d44 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
20d45 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c   int iDb){.  sql
20d46 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
20d47 65 2d 3e 64 62 3b 0a 20 20 53 63 68 65 6d 61 20  e->db;.  Schema 
20d48 2a 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61  *pSchema = db->a
20d49 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b  Db[iDb].pSchema;
20d4a 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66      /* Schema of
20d4b 20 64 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f   database iDb */
20d4c 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a  .  HashElem *k;.
20d4d 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a    int iStatCur;.
20d4e 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73    int iMem;..  s
20d4f 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
20d50 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
20d51 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74  , 0, iDb);.  iSt
20d52 61 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e  atCur = pParse->
20d53 6e 54 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74  nTab++;.  openSt
20d54 61 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  atTable(pParse, 
20d55 69 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 30  iDb, iStatCur, 0
20d56 29 3b 0a 20 20 69 4d 65 6d 20 3d 20 70 50 61 72  );.  iMem = pPar
20d57 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f  se->nMem+1;.  fo
20d58 72 28 6b 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(k=sqliteHashFi
20d59 72 73 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62  rst(&pSchema->tb
20d5a 6c 48 61 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c  lHash); k; k=sql
20d5b 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b  iteHashNext(k)){
20d5c 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
20d5d 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74   = (Table*)sqlit
20d5e 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a 20 20  eHashData(k);.  
20d5f 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c    analyzeOneTabl
20d60 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
20d61 69 53 74 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b  iStatCur, iMem);
20d62 0a 20 20 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79  .  }.  loadAnaly
20d63 73 69 73 28 70 50 61 72 73 65 2c 20 69 44 62 29  sis(pParse, iDb)
20d64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
20d65 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
20d66 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69  ll do an analysi
20d67 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61  s of a single ta
20d68 62 6c 65 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61  ble in.** a data
20d69 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
20d6a 76 6f 69 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c  void analyzeTabl
20d6b 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
20d6c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
20d6d 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20   int iDb;.  int 
20d6e 69 53 74 61 74 43 75 72 3b 0a 0a 20 20 61 73 73  iStatCur;..  ass
20d6f 65 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a  ert( pTab!=0 );.
20d70 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
20d71 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
20d72 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62  texes(pParse->db
20d73 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  ) );.  iDb = sql
20d74 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
20d75 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
20d76 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
20d77 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
20d78 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
20d79 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53  e, 0, iDb);.  iS
20d7a 74 61 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d  tatCur = pParse-
20d7b 3e 6e 54 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53  >nTab++;.  openS
20d7c 74 61 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c  tatTable(pParse,
20d7d 20 69 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20   iDb, iStatCur, 
20d7e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
20d7f 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28  analyzeOneTable(
20d80 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53  pParse, pTab, iS
20d81 74 61 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e  tatCur, pParse->
20d82 6e 4d 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41  nMem+1);.  loadA
20d83 6e 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20  nalysis(pParse, 
20d84 69 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  iDb);.}../*.** G
20d85 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
20d86 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d   the ANALYZE com
20d87 6d 61 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65  mand.  The parse
20d88 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
20d89 74 69 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20  tine.** when it 
20d8a 72 65 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e  recognizes an AN
20d8b 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ALYZE command..*
20d8c 2a 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c  *.**        ANAL
20d8d 59 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20  YZE             
20d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
20d8f 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e  - 1.**        AN
20d90 41 4c 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65  ALYZE  <database
20d91 3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  >               
20d92 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20   -- 2.**        
20d93 41 4e 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62  ANALYZE  ?<datab
20d94 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65  ase>.?<tablename
20d95 3e 20 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f  >  -- 3.**.** Fo
20d96 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20  rm 1 causes all 
20d97 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61  indices in all a
20d98 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
20d99 73 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64  s to be analyzed
20d9a 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c  ..** Form 2 anal
20d9b 79 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73  yzes all indices
20d9c 20 74 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61   the single data
20d9d 62 61 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46  base named..** F
20d9e 6f 72 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61  orm 3 analyzes a
20d9f 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63  ll indices assoc
20da0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e  iated with the n
20da1 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  amed table..*/.S
20da2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
20da3 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a  id sqlite3Analyz
20da4 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
20da5 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
20da6 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a  Token *pName2){.
20da7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
20da8 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
20da9 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a  t iDb;.  int i;.
20daa 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b    char *z, *zDb;
20dab 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
20dac 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e    Token *pTableN
20dad 61 6d 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20  ame;..  /* Read 
20dae 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
20daf 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ema. If an error
20db0 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61   occurs, leave a
20db1 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a  n error message.
20db2 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e    ** and code in
20db3 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75   pParse and retu
20db4 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73  rn NULL. */.  as
20db5 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
20db6 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
20db7 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b  s(pParse->db) );
20db8 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
20db9 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  !=sqlite3ReadSch
20dba 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
20dbb 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
20dbc 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32    assert( pName2
20dbd 21 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 3d 3d 30  !=0 || pName1==0
20dbe 20 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 31   );.  if( pName1
20dbf 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f  ==0 ){.    /* Fo
20dc0 72 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20 65  rm 1:  Analyze e
20dc1 76 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20 20  verything */.   
20dc2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
20dc3 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
20dc4 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74   if( i==1 ) cont
20dc5 69 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74  inue;  /* Do not
20dc6 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45 4d   analyze the TEM
20dc7 50 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  P database */.  
20dc8 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62      analyzeDatab
20dc9 61 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b 0a  ase(pParse, i);.
20dca 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
20dcb 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29  ( pName2->n==0 )
20dcc 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32 3a  {.    /* Form 2:
20dcd 20 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 64 61    Analyze the da
20dce 74 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20  tabase or table 
20dcf 6e 61 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44 62  named */.    iDb
20dd0 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62   = sqlite3FindDb
20dd1 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20  (db, pName1);.  
20dd2 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a    if( iDb>=0 ){.
20dd3 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74        analyzeDat
20dd4 61 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 44  abase(pParse, iD
20dd5 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  b);.    }else{. 
20dd6 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33       z = sqlite3
20dd7 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
20dd8 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20  , pName1);.     
20dd9 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20   if( z ){.      
20dda 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
20ddb 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72  LocateTable(pPar
20ddc 73 65 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a 20 20  se, 0, z, 0);.  
20ddd 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
20dde 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20  ree(db, z);.    
20ddf 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a      if( pTab ){.
20de0 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a            analyz
20de1 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  eTable(pParse, p
20de2 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Tab);.        }.
20de3 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
20de4 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 6f  }else{.    /* Fo
20de5 72 6d 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74 68  rm 3: Analyze th
20de6 65 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65  e fully qualifie
20de7 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  d table name */.
20de8 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
20de9 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61  3TwoPartName(pPa
20dea 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61  rse, pName1, pNa
20deb 6d 65 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d 65  me2, &pTableName
20dec 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d  );.    if( iDb>=
20ded 30 20 29 7b 0a 20 20 20 20 20 20 7a 44 62 20 3d  0 ){.      zDb =
20dee 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
20def 61 6d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73  ame;.      z = s
20df0 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
20df1 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e 61  ken(db, pTableNa
20df2 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  me);.      if( z
20df3 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62   ){.        pTab
20df4 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
20df5 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
20df6 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20   z, zDb);.      
20df7 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
20df8 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20  db, z);.        
20df9 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
20dfa 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62        analyzeTab
20dfb 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  le(pParse, pTab)
20dfc 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
20dfd 20 20 7d 0a 20 20 20 20 7d 20 20 20 0a 20 20 7d    }.    }   .  }
20dfe 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20 74  .}../*.** Used t
20dff 6f 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69  o pass informati
20e00 6f 6e 20 66 72 6f 6d 20 74 68 65 20 61 6e 61 6c  on from the anal
20e01 79 7a 65 72 20 72 65 61 64 65 72 20 74 68 72 6f  yzer reader thro
20e02 75 67 68 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61  ugh to the.** ca
20e03 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 0a  llback routine..
20e04 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
20e05 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 61  t analysisInfo a
20e06 6e 61 6c 79 73 69 73 49 6e 66 6f 3b 0a 73 74 72  nalysisInfo;.str
20e07 75 63 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f  uct analysisInfo
20e08 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
20e09 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
20e0a 7a 44 61 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f  zDatabase;.};../
20e0b 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 62 61  *.** This callba
20e0c 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  ck is invoked on
20e0d 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  ce for each inde
20e0e 78 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 74  x when reading t
20e0f 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61  he.** sqlite_sta
20e10 74 31 20 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a  t1 table.  .**.*
20e11 2a 20 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20  *     argv[0] = 
20e12 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  name of the inde
20e13 78 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d  x.**     argv[1]
20e14 20 3d 20 72 65 73 75 6c 74 73 20 6f 66 20 61 6e   = results of an
20e15 61 6c 79 73 69 73 20 2d 20 6f 6e 20 69 6e 74 65  alysis - on inte
20e16 67 65 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  ger for each col
20e17 75 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  umn.*/.static in
20e18 74 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72  t analysisLoader
20e19 28 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e  (void *pData, in
20e1a 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61  t argc, char **a
20e1b 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55  rgv, char **NotU
20e1c 73 65 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73  sed){.  analysis
20e1d 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61  Info *pInfo = (a
20e1e 6e 61 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61  nalysisInfo*)pDa
20e1f 74 61 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e  ta;.  Index *pIn
20e20 64 65 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b  dex;.  int i, c;
20e21 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
20e22 76 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  v;.  const char 
20e23 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61  *z;..  assert( a
20e24 72 67 63 3d 3d 32 20 29 3b 0a 20 20 55 4e 55 53  rgc==2 );.  UNUS
20e25 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
20e26 74 55 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a 20  tUsed, argc);.. 
20e27 20 69 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c 20   if( argv==0 || 
20e28 61 72 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61 72  argv[0]==0 || ar
20e29 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  gv[1]==0 ){.    
20e2a 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
20e2b 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
20e2c 46 69 6e 64 49 6e 64 65 78 28 70 49 6e 66 6f 2d  FindIndex(pInfo-
20e2d 3e 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70 49  >db, argv[0], pI
20e2e 6e 66 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  nfo->zDatabase);
20e2f 0a 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30  .  if( pIndex==0
20e30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
20e31 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 61 72 67 76  ;.  }.  z = argv
20e32 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  [1];.  for(i=0; 
20e33 2a 7a 20 26 26 20 69 3c 3d 70 49 6e 64 65 78 2d  *z && i<=pIndex-
20e34 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  >nColumn; i++){.
20e35 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 77      v = 0;.    w
20e36 68 69 6c 65 28 20 28 63 3d 7a 5b 30 5d 29 3e 3d  hile( (c=z[0])>=
20e37 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b  '0' && c<='9' ){
20e38 0a 20 20 20 20 20 20 76 20 3d 20 76 2a 31 30 20  .      v = v*10 
20e39 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  + c - '0';.     
20e3a 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20   z++;.    }.    
20e3b 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74  pIndex->aiRowEst
20e3c 5b 69 5d 20 3d 20 76 3b 0a 20 20 20 20 69 66 28  [i] = v;.    if(
20e3d 20 2a 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b 0a   *z==' ' ) z++;.
20e3e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
20e3f 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 74 68  }../*.** Load th
20e40 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
20e41 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
20e42 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69 6e 64  ble into the ind
20e43 65 78 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a  ex hash tables..
20e44 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20e45 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 6e 61  E int sqlite3Ana
20e46 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c 69 74 65  lysisLoad(sqlite
20e47 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b  3 *db, int iDb){
20e48 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20  .  analysisInfo 
20e49 73 49 6e 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65  sInfo;.  HashEle
20e4a 6d 20 2a 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53  m *i;.  char *zS
20e4b 71 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  ql;.  int rc;.. 
20e4c 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
20e4d 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
20e4e 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
20e4f 61 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20  aDb[iDb].pBt!=0 
20e50 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
20e51 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
20e52 74 65 78 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d  tex(db->aDb[iDb]
20e53 2e 70 42 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 43  .pBt) );..  /* C
20e54 6c 65 61 72 20 61 6e 79 20 70 72 69 6f 72 20 73  lear any prior s
20e55 74 61 74 69 73 74 69 63 73 20 2a 2f 0a 20 20 66  tatistics */.  f
20e56 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46  or(i=sqliteHashF
20e57 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 44  irst(&db->aDb[iD
20e58 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  b].pSchema->idxH
20e59 61 73 68 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48  ash);i;i=sqliteH
20e5a 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20 20  ashNext(i)){.   
20e5b 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73   Index *pIdx = s
20e5c 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
20e5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 66  ;.    sqlite3Def
20e5e 61 75 6c 74 52 6f 77 45 73 74 28 70 49 64 78 29  aultRowEst(pIdx)
20e5f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
20e60 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  k to make sure t
20e61 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
20e62 74 61 62 6c 65 20 65 78 69 73 74 73 73 20 2a 2f  table existss */
20e63 0a 20 20 73 49 6e 66 6f 2e 64 62 20 3d 20 64 62  .  sInfo.db = db
20e64 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62  ;.  sInfo.zDatab
20e65 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  ase = db->aDb[iD
20e66 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20  b].zName;.  if( 
20e67 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
20e68 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61  (db, "sqlite_sta
20e69 74 31 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61  t1", sInfo.zData
20e6a 62 61 73 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  base)==0 ){.    
20e6b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
20e6c 52 52 4f 52 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  RROR;.  }...  /*
20e6d 20 4c 6f 61 64 20 6e 65 77 20 73 74 61 74 69 73   Load new statis
20e6e 74 69 63 73 20 6f 75 74 20 6f 66 20 74 68 65 20  tics out of the 
20e6f 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
20e70 6c 65 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73  le */.  zSql = s
20e71 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
20e72 2c 20 22 53 45 4c 45 43 54 20 69 64 78 2c 20 73  , "SELECT idx, s
20e73 74 61 74 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69  tat FROM %Q.sqli
20e74 74 65 5f 73 74 61 74 31 22 2c 0a 20 20 20 20 20  te_stat1",.     
20e75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20e76 20 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61     sInfo.zDataba
20e77 73 65 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d  se);.  if( zSql=
20e78 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
20e79 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
20e7a 65 6c 73 65 7b 0a 20 20 20 20 28 76 6f 69 64 29  else{.    (void)
20e7b 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
20e7c 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  (db);.    rc = s
20e7d 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
20e7e 7a 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f  zSql, analysisLo
20e7f 61 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29  ader, &sInfo, 0)
20e80 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
20e81 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
20e82 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
20e83 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20  ee(db, zSql);.  
20e84 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
20e85 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c  _NOMEM ) db->mal
20e86 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
20e87 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
20e88 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  }...#endif /* SQ
20e89 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a  LITE_OMIT_ANALYZ
20e8a 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
20e8b 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6e 61  ***** End of ana
20e8c 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  lyze.c *********
20e8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20e8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20e8f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
20e90 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
20e91 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a   attach.c ******
20e92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20e93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20e94 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
20e95 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54   April 6.**.** T
20e96 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
20e97 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
20e98 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
20e99 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
20e9a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
20e9b 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
20e9c 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
20e9d 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
20e9e 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
20e9f 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
20ea0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
20ea1 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
20ea2 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
20ea3 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
20ea4 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
20ea5 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
20ea6 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
20ea7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ea8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20ea9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20eaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20eab 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
20eac 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
20ead 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
20eae 6d 65 6e 74 20 74 68 65 20 41 54 54 41 43 48 20  ment the ATTACH 
20eaf 61 6e 64 20 44 45 54 41 43 48 20 63 6f 6d 6d 61  and DETACH comma
20eb0 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  nds..**.** $Id: 
20eb1 61 74 74 61 63 68 2e 63 2c 76 20 31 2e 39 30 20  attach.c,v 1.90 
20eb2 32 30 30 39 2f 30 35 2f 30 31 20 30 36 3a 31 39  2009/05/01 06:19
20eb3 3a 32 31 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :21 danielk1977 
20eb4 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
20eb5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54  f SQLITE_OMIT_AT
20eb6 54 41 43 48 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c  TACH./*.** Resol
20eb7 76 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ve an expression
20eb8 20 74 68 61 74 20 77 61 73 20 70 61 72 74 20 6f   that was part o
20eb9 66 20 61 6e 20 41 54 54 41 43 48 20 6f 72 20 44  f an ATTACH or D
20eba 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e  ETACH statement.
20ebb 20 54 68 69 73 0a 2a 2a 20 69 73 20 73 6c 69 67   This.** is slig
20ebc 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 20 66  htly different f
20ebd 72 6f 6d 20 72 65 73 6f 6c 76 69 6e 67 20 61 20  rom resolving a 
20ebe 6e 6f 72 6d 61 6c 20 53 51 4c 20 65 78 70 72 65  normal SQL expre
20ebf 73 73 69 6f 6e 2c 20 62 65 63 61 75 73 65 20 73  ssion, because s
20ec0 69 6d 70 6c 65 0a 2a 2a 20 69 64 65 6e 74 69 66  imple.** identif
20ec1 69 65 72 73 20 61 72 65 20 74 72 65 61 74 65 64  iers are treated
20ec2 20 61 73 20 73 74 72 69 6e 67 73 2c 20 6e 6f 74   as strings, not
20ec3 20 70 6f 73 73 69 62 6c 65 20 63 6f 6c 75 6d 6e   possible column
20ec4 20 6e 61 6d 65 73 20 6f 72 20 61 6c 69 61 73 65   names or aliase
20ec5 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 69 66  s..**.** i.e. if
20ec6 20 74 68 65 20 70 61 72 73 65 72 20 73 65 65 73   the parser sees
20ec7 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41  :.**.**     ATTA
20ec8 43 48 20 44 41 54 41 42 41 53 45 20 61 62 63 20  CH DATABASE abc 
20ec9 41 53 20 64 65 66 0a 2a 2a 0a 2a 2a 20 69 74 20  AS def.**.** it 
20eca 74 72 65 61 74 73 20 74 68 65 20 74 77 6f 20 65  treats the two e
20ecb 78 70 72 65 73 73 69 6f 6e 73 20 61 73 20 6c 69  xpressions as li
20ecc 74 65 72 61 6c 20 73 74 72 69 6e 67 73 20 27 61  teral strings 'a
20ecd 62 63 27 20 61 6e 64 20 27 64 65 66 27 20 69 6e  bc' and 'def' in
20ece 73 74 65 61 64 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b  stead of.** look
20ecf 69 6e 67 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20  ing for columns 
20ed0 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  of the same name
20ed1 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c  ..**.** This onl
20ed2 79 20 61 70 70 6c 69 65 73 20 74 6f 20 74 68 65  y applies to the
20ed3 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 70 45   root node of pE
20ed4 78 70 72 2c 20 73 6f 20 74 68 65 20 73 74 61 74  xpr, so the stat
20ed5 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ement:.**.**    
20ed6 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45   ATTACH DATABASE
20ed7 20 61 62 63 7c 7c 64 65 66 20 41 53 20 27 64 62   abc||def AS 'db
20ed8 32 27 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 66 61  2'.**.** will fa
20ed9 69 6c 20 62 65 63 61 75 73 65 20 6e 65 69 74 68  il because neith
20eda 65 72 20 61 62 63 20 6f 72 20 64 65 66 20 63 61  er abc or def ca
20edb 6e 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  n be resolved..*
20edc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
20edd 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 4e  olveAttachExpr(N
20ede 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 61 6d  ameContext *pNam
20edf 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 0a  e, Expr *pExpr).
20ee0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
20ee1 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 45  ITE_OK;.  if( pE
20ee2 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70  xpr ){.    if( p
20ee3 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 44 20  Expr->op!=TK_ID 
20ee4 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
20ee5 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
20ee6 4e 61 6d 65 73 28 70 4e 61 6d 65 2c 20 70 45 78  Names(pName, pEx
20ee7 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  pr);.      if( r
20ee8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
20ee9 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f  !sqlite3ExprIsCo
20eea 6e 73 74 61 6e 74 28 70 45 78 70 72 29 20 29 7b  nstant(pExpr) ){
20eeb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
20eec 45 72 72 6f 72 4d 73 67 28 70 4e 61 6d 65 2d 3e  ErrorMsg(pName->
20eed 70 50 61 72 73 65 2c 20 22 69 6e 76 61 6c 69 64  pParse, "invalid
20eee 20 6e 61 6d 65 3a 20 5c 22 25 54 5c 22 22 2c 20   name: \"%T\"", 
20eef 26 70 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20  &pExpr->span);. 
20ef0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
20ef1 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
20ef2 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
20ef3 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
20ef4 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20   TK_STRING;.    
20ef5 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
20ef6 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53  c;.}../*.** An S
20ef7 51 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  QL user-function
20ef8 20 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64   registered to d
20ef9 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e  o the work of an
20efa 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e   ATTACH statemen
20efb 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20  t. The.** three 
20efc 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
20efd 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64   function come d
20efe 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 6e 20  irectly from an 
20eff 61 74 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74  attach statement
20f00 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41  :.**.**     ATTA
20f01 43 48 20 44 41 54 41 42 41 53 45 20 78 20 41 53  CH DATABASE x AS
20f02 20 79 20 4b 45 59 20 7a 0a 2a 2a 0a 2a 2a 20 20   y KEY z.**.**  
20f03 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65     SELECT sqlite
20f04 5f 61 74 74 61 63 68 28 78 2c 20 79 2c 20 7a 29  _attach(x, y, z)
20f05 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
20f06 74 69 6f 6e 61 6c 20 22 4b 45 59 20 7a 22 20 73  tional "KEY z" s
20f07 79 6e 74 61 78 20 69 73 20 6f 6d 69 74 74 65 64  yntax is omitted
20f08 2c 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 69 73  , an SQL NULL is
20f09 20 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a   passed as the.*
20f0a 2a 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  * third argument
20f0b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
20f0c 20 61 74 74 61 63 68 46 75 6e 63 28 0a 20 20 73   attachFunc(.  s
20f0d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
20f0e 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
20f0f 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
20f10 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
20f11 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
20f12 20 72 63 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74   rc = 0;.  sqlit
20f13 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
20f14 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
20f15 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63  le(context);.  c
20f16 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
20f17 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
20f18 7a 46 69 6c 65 3b 0a 20 20 44 62 20 2a 61 4e 65  zFile;.  Db *aNe
20f19 77 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 44  w;.  char *zErrD
20f1a 79 6e 20 3d 20 30 3b 0a 0a 20 20 55 4e 55 53 45  yn = 0;..  UNUSE
20f1b 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
20f1c 73 65 64 29 3b 0a 0a 20 20 7a 46 69 6c 65 20 3d  sed);..  zFile =
20f1d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
20f1e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
20f1f 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e  t(argv[0]);.  zN
20f20 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ame = (const cha
20f21 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
20f22 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
20f23 0a 20 20 69 66 28 20 7a 46 69 6c 65 3d 3d 30 20  .  if( zFile==0 
20f24 29 20 7a 46 69 6c 65 20 3d 20 22 22 3b 0a 20 20  ) zFile = "";.  
20f25 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a  if( zName==0 ) z
20f26 4e 61 6d 65 20 3d 20 22 22 3b 0a 0a 20 20 2f 2a  Name = "";..  /*
20f27 20 43 68 65 63 6b 20 66 6f 72 20 74 68 65 20 66   Check for the f
20f28 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73 3a  ollowing errors:
20f29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 2a  .  **.  **     *
20f2a 20 54 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68   Too many attach
20f2b 65 64 20 64 61 74 61 62 61 73 65 73 2c 0a 20 20  ed databases,.  
20f2c 2a 2a 20 20 20 20 20 2a 20 54 72 61 6e 73 61 63  **     * Transac
20f2d 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 6f  tion currently o
20f2e 70 65 6e 0a 20 20 2a 2a 20 20 20 20 20 2a 20 53  pen.  **     * S
20f2f 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73  pecified databas
20f30 65 20 6e 61 6d 65 20 61 6c 72 65 61 64 79 20 62  e name already b
20f31 65 69 6e 67 20 75 73 65 64 2e 0a 20 20 2a 2f 0a  eing used..  */.
20f32 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 3d 64    if( db->nDb>=d
20f33 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
20f34 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d  _LIMIT_ATTACHED]
20f35 2b 32 20 29 7b 0a 20 20 20 20 7a 45 72 72 44 79  +2 ){.    zErrDy
20f36 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  n = sqlite3MPrin
20f37 74 66 28 64 62 2c 20 22 74 6f 6f 20 6d 61 6e 79  tf(db, "too many
20f38 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
20f39 73 65 73 20 2d 20 6d 61 78 20 25 64 22 2c 20 0a  ses - max %d", .
20f3a 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74        db->aLimit
20f3b 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54  [SQLITE_LIMIT_AT
20f3c 54 41 43 48 45 44 5d 0a 20 20 20 20 29 3b 0a 20  TACHED].    );. 
20f3d 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
20f3e 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rror;.  }.  if( 
20f3f 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20  !db->autoCommit 
20f40 29 7b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20 3d  ){.    zErrDyn =
20f41 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
20f42 64 62 2c 20 22 63 61 6e 6e 6f 74 20 41 54 54 41  db, "cannot ATTA
20f43 43 48 20 64 61 74 61 62 61 73 65 20 77 69 74 68  CH database with
20f44 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29  in transaction")
20f45 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63  ;.    goto attac
20f46 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 66  h_error;.  }.  f
20f47 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
20f48 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61  b; i++){.    cha
20f49 72 20 2a 7a 20 3d 20 64 62 2d 3e 61 44 62 5b 69  r *z = db->aDb[i
20f4a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 61 73 73  ].zName;.    ass
20f4b 65 72 74 28 20 7a 20 26 26 20 7a 4e 61 6d 65 20  ert( z && zName 
20f4c 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
20f4d 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 7a 4e 61  e3StrICmp(z, zNa
20f4e 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
20f4f 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
20f50 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 64 61  3MPrintf(db, "da
20f51 74 61 62 61 73 65 20 25 73 20 69 73 20 61 6c 72  tabase %s is alr
20f52 65 61 64 79 20 69 6e 20 75 73 65 22 2c 20 7a 4e  eady in use", zN
20f53 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ame);.      goto
20f54 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
20f55 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
20f56 6c 6c 6f 63 61 74 65 20 74 68 65 20 6e 65 77 20  llocate the new 
20f57 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 62 2d  entry in the db-
20f58 3e 61 44 62 5b 5d 20 61 72 72 61 79 20 61 6e 64  >aDb[] array and
20f59 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68 65 20   initialise the 
20f5a 73 63 68 65 6d 61 0a 20 20 2a 2a 20 68 61 73 68  schema.  ** hash
20f5b 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
20f5c 69 66 28 20 64 62 2d 3e 61 44 62 3d 3d 64 62 2d  if( db->aDb==db-
20f5d 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20  >aDbStatic ){.  
20f5e 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    aNew = sqlite3
20f5f 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
20f60 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30  sizeof(db->aDb[0
20f61 5d 29 2a 33 20 29 3b 0a 20 20 20 20 69 66 28 20  ])*3 );.    if( 
20f62 61 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  aNew==0 ) return
20f63 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 4e 65  ;.    memcpy(aNe
20f64 77 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65  w, db->aDb, size
20f65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 32  of(db->aDb[0])*2
20f66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
20f67 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  aNew = sqlite3Db
20f68 52 65 61 6c 6c 6f 63 28 64 62 2c 20 64 62 2d 3e  Realloc(db, db->
20f69 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e  aDb, sizeof(db->
20f6a 61 44 62 5b 30 5d 29 2a 28 64 62 2d 3e 6e 44 62  aDb[0])*(db->nDb
20f6b 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28 20 61  +1) );.    if( a
20f6c 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  New==0 ) return;
20f6d 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 20 3d  .  }.  db->aDb =
20f6e 20 61 4e 65 77 3b 0a 20 20 61 4e 65 77 20 3d 20   aNew;.  aNew = 
20f6f 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62  &db->aDb[db->nDb
20f70 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 4e 65 77  ];.  memset(aNew
20f71 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 61 4e 65  , 0, sizeof(*aNe
20f72 77 29 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  w));..  /* Open 
20f73 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
20f74 65 2e 20 49 66 20 74 68 65 20 62 74 72 65 65 20  e. If the btree 
20f75 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  is successfully 
20f76 6f 70 65 6e 65 64 2c 20 75 73 65 0a 20 20 2a 2a  opened, use.  **
20f77 20 69 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68   it to obtain th
20f78 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
20f79 61 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  a. At this point
20f7a 20 74 68 65 20 73 63 68 65 6d 61 20 6d 61 79 0a   the schema may.
20f7b 20 20 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20    ** or may not 
20f7c 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a  be initialised..
20f7d 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
20f7e 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
20f7f 64 62 2c 20 7a 46 69 6c 65 2c 20 30 2c 20 53 51  db, zFile, 0, SQ
20f80 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
20f81 48 45 5f 53 49 5a 45 2c 0a 20 20 20 20 20 20 20  HE_SIZE,.       
20f82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f83 20 20 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67      db->openFlag
20f84 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s | SQLITE_OPEN_
20f85 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20 20  MAIN_DB,.       
20f86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f87 20 20 20 20 26 61 4e 65 77 2d 3e 70 42 74 29 3b      &aNew->pBt);
20f88 0a 20 20 64 62 2d 3e 6e 44 62 2b 2b 3b 0a 20 20  .  db->nDb++;.  
20f89 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43  if( rc==SQLITE_C
20f8a 4f 4e 53 54 52 41 49 4e 54 20 29 7b 0a 20 20 20  ONSTRAINT ){.   
20f8b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
20f8c 4f 52 3b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20  OR;.    zErrDyn 
20f8d 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
20f8e 28 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 69  (db, "database i
20f8f 73 20 61 6c 72 65 61 64 79 20 61 74 74 61 63 68  s already attach
20f90 65 64 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ed");.  }else if
20f91 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
20f92 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50  ){.    Pager *pP
20f93 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d 3e  ager;.    aNew->
20f94 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65  pSchema = sqlite
20f95 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 61  3SchemaGet(db, a
20f96 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69  New->pBt);.    i
20f97 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65 6d  f( !aNew->pSchem
20f98 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  a ){.      rc = 
20f99 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
20f9a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65 77    }else if( aNew
20f9b 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
20f9c 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d 3e  format && aNew->
20f9d 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e  pSchema->enc!=EN
20f9e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 7a  C(db) ){.      z
20f9f 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33  ErrDyn = sqlite3
20fa0 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20  MPrintf(db, .   
20fa1 20 20 20 20 20 22 61 74 74 61 63 68 65 64 20 64       "attached d
20fa2 61 74 61 62 61 73 65 73 20 6d 75 73 74 20 75 73  atabases must us
20fa3 65 20 74 68 65 20 73 61 6d 65 20 74 65 78 74 20  e the same text 
20fa4 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e  encoding as main
20fa5 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20   database");.   
20fa6 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
20fa7 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20  RROR;.    }.    
20fa8 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
20fa9 42 74 72 65 65 50 61 67 65 72 28 61 4e 65 77 2d  BtreePager(aNew-
20faa 3e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  >pBt);.    sqlit
20fab 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
20fac 64 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64  de(pPager, db->d
20fad 66 6c 74 4c 6f 63 6b 4d 6f 64 65 29 3b 0a 20 20  fltLockMode);.  
20fae 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f    sqlite3PagerJo
20faf 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72  urnalMode(pPager
20fb0 2c 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61  , db->dfltJourna
20fb1 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 61 4e  lMode);.  }.  aN
20fb2 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  ew->zName = sqli
20fb3 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
20fb4 7a 4e 61 6d 65 29 3b 0a 20 20 61 4e 65 77 2d 3e  zName);.  aNew->
20fb5 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33  safety_level = 3
20fb6 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41  ;..#if SQLITE_HA
20fb7 53 5f 43 4f 44 45 43 0a 20 20 7b 0a 20 20 20 20  S_CODEC.  {.    
20fb8 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
20fb9 65 33 43 6f 64 65 63 41 74 74 61 63 68 28 73 71  e3CodecAttach(sq
20fba 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e  lite3*, int, con
20fbb 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a  st void*, int);.
20fbc 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20      extern void 
20fbd 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b  sqlite3CodecGetK
20fbe 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ey(sqlite3*, int
20fbf 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b  , void**, int*);
20fc0 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20  .    int nKey;. 
20fc1 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20     char *zKey;. 
20fc2 20 20 20 69 6e 74 20 74 20 3d 20 73 71 6c 69 74     int t = sqlit
20fc3 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
20fc4 67 76 5b 32 5d 29 3b 0a 20 20 20 20 73 77 69 74  gv[2]);.    swit
20fc5 63 68 28 20 74 20 29 7b 0a 20 20 20 20 20 20 63  ch( t ){.      c
20fc6 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  ase SQLITE_INTEG
20fc7 45 52 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53  ER:.      case S
20fc8 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 0a 20 20 20  QLITE_FLOAT:.   
20fc9 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73       zErrDyn = s
20fca 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
20fcb 62 2c 20 22 49 6e 76 61 6c 69 64 20 6b 65 79 20  b, "Invalid key 
20fcc 76 61 6c 75 65 22 29 3b 0a 20 20 20 20 20 20 20  value");.       
20fcd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
20fce 4f 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  OR;.        brea
20fcf 6b 3b 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20  k;.        .    
20fd0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
20fd1 58 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53  XT:.      case S
20fd2 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20  QLITE_BLOB:.    
20fd3 20 20 20 20 6e 4b 65 79 20 3d 20 73 71 6c 69 74      nKey = sqlit
20fd4 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
20fd5 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20  rgv[2]);.       
20fd6 20 7a 4b 65 79 20 3d 20 28 63 68 61 72 20 2a 29   zKey = (char *)
20fd7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
20fd8 6f 62 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  ob(argv[2]);.   
20fd9 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65       sqlite3Code
20fda 63 41 74 74 61 63 68 28 64 62 2c 20 64 62 2d 3e  cAttach(db, db->
20fdb 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65  nDb-1, zKey, nKe
20fdc 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  y);.        brea
20fdd 6b 3b 0a 0a 20 20 20 20 20 20 63 61 73 65 20 53  k;..      case S
20fde 51 4c 49 54 45 5f 4e 55 4c 4c 3a 0a 20 20 20 20  QLITE_NULL:.    
20fdf 20 20 20 20 2f 2a 20 4e 6f 20 6b 65 79 20 73 70      /* No key sp
20fe0 65 63 69 66 69 65 64 2e 20 20 55 73 65 20 74 68  ecified.  Use th
20fe1 65 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 6d  e key from the m
20fe2 61 69 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ain database */.
20fe3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
20fe4 6f 64 65 63 47 65 74 4b 65 79 28 64 62 2c 20 30  odecGetKey(db, 0
20fe5 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b 65 79 2c  , (void**)&zKey,
20fe6 20 26 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20   &nKey);.       
20fe7 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74   sqlite3CodecAtt
20fe8 61 63 68 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d  ach(db, db->nDb-
20fe9 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a  1, zKey, nKey);.
20fea 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
20feb 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
20fec 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c  .  /* If the fil
20fed 65 20 77 61 73 20 6f 70 65 6e 65 64 20 73 75 63  e was opened suc
20fee 63 65 73 73 66 75 6c 6c 79 2c 20 72 65 61 64 20  cessfully, read 
20fef 74 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74  the schema for t
20ff0 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 2e  he new database.
20ff1 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 66 61  .  ** If this fa
20ff2 69 6c 73 2c 20 6f 72 20 69 66 20 6f 70 65 6e 69  ils, or if openi
20ff3 6e 67 20 74 68 65 20 66 69 6c 65 20 66 61 69 6c  ng the file fail
20ff4 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74  ed, then close t
20ff5 68 65 20 66 69 6c 65 20 61 6e 64 20 0a 20 20 2a  he file and .  *
20ff6 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74  * remove the ent
20ff7 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e  ry from the db->
20ff8 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 69 2e 65  aDb[] array. i.e
20ff9 2e 20 70 75 74 20 65 76 65 72 79 74 68 69 6e 67  . put everything
20ffa 20 62 61 63 6b 20 74 68 65 20 77 61 79 0a 20 20   back the way.  
20ffb 2a 2a 20 77 65 20 66 6f 75 6e 64 20 69 74 2e 0a  ** we found it..
20ffc 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53    */.  if( rc==S
20ffd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
20ffe 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
20fff 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73  etyOn(db);.    s
21000 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
21001 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 72 63 20  All(db);.    rc 
21002 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62  = sqlite3Init(db
21003 2c 20 26 7a 45 72 72 44 79 6e 29 3b 0a 20 20 20  , &zErrDyn);.   
21004 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
21005 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 28  veAll(db);.    (
21006 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
21007 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 7d 0a 20  tyOff(db);.  }. 
21008 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69   if( rc ){.    i
21009 6e 74 20 69 44 62 20 3d 20 64 62 2d 3e 6e 44 62  nt iDb = db->nDb
2100a 20 2d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74   - 1;.    assert
2100b 28 20 69 44 62 3e 3d 32 20 29 3b 0a 20 20 20 20  ( iDb>=2 );.    
2100c 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  if( db->aDb[iDb]
2100d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 73 71  .pBt ){.      sq
2100e 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
2100f 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
21010 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62  );.      db->aDb
21011 5b 69 44 62 5d 2e 70 42 74 20 3d 20 30 3b 0a 20  [iDb].pBt = 0;. 
21012 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
21013 5d 2e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20  ].pSchema = 0;. 
21014 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
21015 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
21016 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20  ema(db, 0);.    
21017 64 62 2d 3e 6e 44 62 20 3d 20 69 44 62 3b 0a 20  db->nDb = iDb;. 
21018 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
21019 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53  E_NOMEM || rc==S
2101a 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
2101b 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  M ){.      db->m
2101c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2101d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2101e 46 72 65 65 28 64 62 2c 20 7a 45 72 72 44 79 6e  Free(db, zErrDyn
2101f 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 44 79 6e  );.      zErrDyn
21020 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
21021 66 28 64 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65  f(db, "out of me
21022 6d 6f 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73  mory");.    }els
21023 65 20 69 66 28 20 7a 45 72 72 44 79 6e 3d 3d 30  e if( zErrDyn==0
21024 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 44 79   ){.      zErrDy
21025 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  n = sqlite3MPrin
21026 74 66 28 64 62 2c 20 22 75 6e 61 62 6c 65 20 74  tf(db, "unable t
21027 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 3a  o open database:
21028 20 25 73 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20   %s", zFile);.  
21029 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 61 74 74    }.    goto att
2102a 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  ach_error;.  }. 
2102b 20 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 61 74 74   .  return;..att
2102c 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20 2f 2a 20  ach_error:.  /* 
2102d 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
2102e 69 66 20 77 65 20 67 65 74 20 68 65 72 65 20 2a  if we get here *
2102f 2f 0a 20 20 69 66 28 20 7a 45 72 72 44 79 6e 20  /.  if( zErrDyn 
21030 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
21031 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
21032 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c 20 2d 31  ext, zErrDyn, -1
21033 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
21034 46 72 65 65 28 64 62 2c 20 7a 45 72 72 44 79 6e  Free(db, zErrDyn
21035 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20  );.  }.  if( rc 
21036 29 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  ) sqlite3_result
21037 5f 65 72 72 6f 72 5f 63 6f 64 65 28 63 6f 6e 74  _error_code(cont
21038 65 78 74 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a  ext, rc);.}../*.
21039 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66  ** An SQL user-f
2103a 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72  unction register
2103b 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72  ed to do the wor
2103c 6b 20 6f 66 20 61 6e 20 44 45 54 41 43 48 20 73  k of an DETACH s
2103d 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a  tatement. The.**
2103e 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73   three arguments
2103f 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
21040 20 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66   come directly f
21041 72 6f 6d 20 61 20 64 65 74 61 63 68 20 73 74 61  rom a detach sta
21042 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  tement:.**.**   
21043 20 20 44 45 54 41 43 48 20 44 41 54 41 42 41 53    DETACH DATABAS
21044 45 20 78 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  E x.**.**     SE
21045 4c 45 43 54 20 73 71 6c 69 74 65 5f 64 65 74 61  LECT sqlite_deta
21046 63 68 28 78 29 0a 2a 2f 0a 73 74 61 74 69 63 20  ch(x).*/.static 
21047 76 6f 69 64 20 64 65 74 61 63 68 46 75 6e 63 28  void detachFunc(
21048 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
21049 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
2104a 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
2104b 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2104c 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  gv.){.  const ch
2104d 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e  ar *zName = (con
2104e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
2104f 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
21050 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33  v[0]);.  sqlite3
21051 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
21052 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
21053 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69 6e 74  (context);.  int
21054 20 69 3b 0a 20 20 44 62 20 2a 70 44 62 20 3d 20   i;.  Db *pDb = 
21055 30 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31  0;.  char zErr[1
21056 32 38 5d 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50  28];..  UNUSED_P
21057 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
21058 29 3b 0a 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d  );..  if( zName=
21059 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b  =0 ) zName = "";
2105a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
2105b 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
2105c 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
2105d 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d  i];.    if( pDb-
2105e 3e 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  >pBt==0 ) contin
2105f 75 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ue;.    if( sqli
21060 74 65 33 53 74 72 49 43 6d 70 28 70 44 62 2d 3e  te3StrICmp(pDb->
21061 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30  zName, zName)==0
21062 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20   ) break;.  }.. 
21063 20 69 66 28 20 69 3e 3d 64 62 2d 3e 6e 44 62 20   if( i>=db->nDb 
21064 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
21065 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
21066 45 72 72 29 2c 7a 45 72 72 2c 20 22 6e 6f 20 73  Err),zErr, "no s
21067 75 63 68 20 64 61 74 61 62 61 73 65 3a 20 25 73  uch database: %s
21068 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  ", zName);.    g
21069 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72  oto detach_error
2106a 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 32 20  ;.  }.  if( i<2 
2106b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
2106c 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
2106d 45 72 72 29 2c 7a 45 72 72 2c 20 22 63 61 6e 6e  Err),zErr, "cann
2106e 6f 74 20 64 65 74 61 63 68 20 64 61 74 61 62 61  ot detach databa
2106f 73 65 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  se %s", zName);.
21070 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f      goto detach_
21071 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28  error;.  }.  if(
21072 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
21073 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
21074 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
21075 7a 45 72 72 29 2c 20 7a 45 72 72 2c 0a 20 20 20  zErr), zErr,.   
21076 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21077 20 20 22 63 61 6e 6e 6f 74 20 44 45 54 41 43 48    "cannot DETACH
21078 20 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e   database within
21079 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a   transaction");.
2107a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f      goto detach_
2107b 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28  error;.  }.  if(
2107c 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
2107d 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e  nReadTrans(pDb->
2107e 70 42 74 29 20 7c 7c 20 73 71 6c 69 74 65 33 42  pBt) || sqlite3B
2107f 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 70  treeIsInBackup(p
21080 44 62 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20  Db->pBt) ){.    
21081 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
21082 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45  (sizeof(zErr),zE
21083 72 72 2c 20 22 64 61 74 61 62 61 73 65 20 25 73  rr, "database %s
21084 20 69 73 20 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61   is locked", zNa
21085 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65  me);.    goto de
21086 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
21087 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  .  sqlite3BtreeC
21088 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a  lose(pDb->pBt);.
21089 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a    pDb->pBt = 0;.
2108a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d    pDb->pSchema =
2108b 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73   0;.  sqlite3Res
2108c 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2108d 28 64 62 2c 20 30 29 3b 0a 20 20 72 65 74 75 72  (db, 0);.  retur
2108e 6e 3b 0a 0a 64 65 74 61 63 68 5f 65 72 72 6f 72  n;..detach_error
2108f 3a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  :.  sqlite3_resu
21090 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
21091 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a  , zErr, -1);.}..
21092 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65  /*.** This proce
21093 64 75 72 65 20 67 65 6e 65 72 61 74 65 73 20 56  dure generates V
21094 44 42 45 20 63 6f 64 65 20 66 6f 72 20 61 20 73  DBE code for a s
21095 69 6e 67 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e  ingle invocation
21096 20 6f 66 20 65 69 74 68 65 72 20 74 68 65 0a 2a   of either the.*
21097 2a 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28  * sqlite_detach(
21098 29 20 6f 72 20 73 71 6c 69 74 65 5f 61 74 74 61  ) or sqlite_atta
21099 63 68 28 29 20 53 51 4c 20 75 73 65 72 20 66 75  ch() SQL user fu
2109a 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74  nctions..*/.stat
2109b 69 63 20 76 6f 69 64 20 63 6f 64 65 41 74 74 61  ic void codeAtta
2109c 63 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ch(.  Parse *pPa
2109d 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  rse,       /* Th
2109e 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2109f 20 2a 2f 0a 20 20 69 6e 74 20 74 79 70 65 2c 20   */.  int type, 
210a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69             /* Ei
210a1 74 68 65 72 20 53 51 4c 49 54 45 5f 41 54 54 41  ther SQLITE_ATTA
210a2 43 48 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 54  CH or SQLITE_DET
210a3 41 43 48 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66  ACH */.  FuncDef
210a4 20 2a 70 46 75 6e 63 2c 20 20 20 20 20 20 2f 2a   *pFunc,      /*
210a5 20 46 75 6e 63 44 65 66 20 77 72 61 70 70 65 72   FuncDef wrapper
210a6 20 66 6f 72 20 64 65 74 61 63 68 46 75 6e 63 28   for detachFunc(
210a7 29 20 6f 72 20 61 74 74 61 63 68 46 75 6e 63 28  ) or attachFunc(
210a8 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 41 75  ) */.  Expr *pAu
210a9 74 68 41 72 67 2c 20 20 20 20 20 20 2f 2a 20 45  thArg,      /* E
210aa 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61 73  xpression to pas
210ab 73 20 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69  s to authorizati
210ac 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  on callback */. 
210ad 20 45 78 70 72 20 2a 70 46 69 6c 65 6e 61 6d 65   Expr *pFilename
210ae 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ,     /* Name of
210af 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
210b0 2f 0a 20 20 45 78 70 72 20 2a 70 44 62 6e 61 6d  /.  Expr *pDbnam
210b1 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  e,       /* Name
210b2 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
210b3 20 74 6f 20 75 73 65 20 69 6e 74 65 72 6e 61 6c   to use internal
210b4 6c 79 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4b  ly */.  Expr *pK
210b5 65 79 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ey           /* 
210b6 44 61 74 61 62 61 73 65 20 6b 65 79 20 66 6f 72  Database key for
210b7 20 65 6e 63 72 79 70 74 69 6f 6e 20 65 78 74 65   encryption exte
210b8 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  nsion */.){.  in
210b9 74 20 72 63 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74  t rc;.  NameCont
210ba 65 78 74 20 73 4e 61 6d 65 3b 0a 20 20 56 64 62  ext sName;.  Vdb
210bb 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 2a  e *v;.  sqlite3*
210bc 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
210bd 3b 0a 20 20 69 6e 74 20 72 65 67 41 72 67 73 3b  ;.  int regArgs;
210be 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
210bf 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
210c0 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 64 62  ION.  assert( db
210c1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
210c2 7c 20 70 41 75 74 68 41 72 67 20 29 3b 0a 20 20  | pAuthArg );.  
210c3 69 66 28 20 70 41 75 74 68 41 72 67 20 29 7b 0a  if( pAuthArg ){.
210c4 20 20 20 20 63 68 61 72 20 2a 7a 41 75 74 68 41      char *zAuthA
210c5 72 67 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  rg = sqlite3Name
210c6 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 26 70  FromToken(db, &p
210c7 41 75 74 68 41 72 67 2d 3e 73 70 61 6e 29 3b 0a  AuthArg->span);.
210c8 20 20 20 20 69 66 28 20 21 7a 41 75 74 68 41 72      if( !zAuthAr
210c9 67 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  g ){.      goto 
210ca 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20  attach_end;.    
210cb 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
210cc 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
210cd 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74 68 41  se, type, zAuthA
210ce 72 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  rg, 0, 0);.    s
210cf 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
210d0 20 7a 41 75 74 68 41 72 67 29 3b 0a 20 20 20 20   zAuthArg);.    
210d1 69 66 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  if(rc!=SQLITE_OK
210d2 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61   ){.      goto a
210d3 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 7d  ttach_end;.    }
210d4 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
210d5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
210d6 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 6d  RIZATION */..  m
210d7 65 6d 73 65 74 28 26 73 4e 61 6d 65 2c 20 30 2c  emset(&sName, 0,
210d8 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e 74   sizeof(NameCont
210d9 65 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 2e 70  ext));.  sName.p
210da 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
210db 0a 20 20 69 66 28 20 0a 20 20 20 20 20 20 53 51  .  if( .      SQ
210dc 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
210dd 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72  esolveAttachExpr
210de 28 26 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e 61  (&sName, pFilena
210df 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51  me)) ||.      SQ
210e0 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
210e1 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72  esolveAttachExpr
210e2 28 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d 65  (&sName, pDbname
210e3 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49  )) ||.      SQLI
210e4 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73  TE_OK!=(rc = res
210e5 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26  olveAttachExpr(&
210e6 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0a 20 20  sName, pKey)).  
210e7 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ){.    pParse->n
210e8 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20  Err++;.    goto 
210e9 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 7d 0a  attach_end;.  }.
210ea 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
210eb 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
210ec 20 72 65 67 41 72 67 73 20 3d 20 73 71 6c 69 74   regArgs = sqlit
210ed 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
210ee 50 61 72 73 65 2c 20 34 29 3b 0a 20 20 73 71 6c  Parse, 4);.  sql
210ef 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
210f0 72 73 65 2c 20 70 46 69 6c 65 6e 61 6d 65 2c 20  rse, pFilename, 
210f1 72 65 67 41 72 67 73 29 3b 0a 20 20 73 71 6c 69  regArgs);.  sqli
210f2 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
210f3 73 65 2c 20 70 44 62 6e 61 6d 65 2c 20 72 65 67  se, pDbname, reg
210f4 41 72 67 73 2b 31 29 3b 0a 20 20 73 71 6c 69 74  Args+1);.  sqlit
210f5 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
210f6 65 2c 20 70 4b 65 79 2c 20 72 65 67 41 72 67 73  e, pKey, regArgs
210f7 2b 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  +2);..  assert( 
210f8 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  v || db->mallocF
210f9 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 76  ailed );.  if( v
210fa 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
210fb 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
210fc 46 75 6e 63 74 69 6f 6e 2c 20 30 2c 20 72 65 67  Function, 0, reg
210fd 41 72 67 73 2b 33 2d 70 46 75 6e 63 2d 3e 6e 41  Args+3-pFunc->nA
210fe 72 67 2c 20 72 65 67 41 72 67 73 2b 33 29 3b 0a  rg, regArgs+3);.
210ff 20 20 20 20 61 73 73 65 72 74 28 20 70 46 75 6e      assert( pFun
21100 63 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 28  c->nArg==-1 || (
21101 70 46 75 6e 63 2d 3e 6e 41 72 67 26 30 78 66 66  pFunc->nArg&0xff
21102 29 3d 3d 70 46 75 6e 63 2d 3e 6e 41 72 67 20 29  )==pFunc->nArg )
21103 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21104 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38  eChangeP5(v, (u8
21105 29 28 70 46 75 6e 63 2d 3e 6e 41 72 67 29 29 3b  )(pFunc->nArg));
21106 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21107 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
21108 28 63 68 61 72 20 2a 29 70 46 75 6e 63 2c 20 50  (char *)pFunc, P
21109 34 5f 46 55 4e 43 44 45 46 29 3b 0a 0a 20 20 20  4_FUNCDEF);..   
2110a 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45   /* Code an OP_E
2110b 78 70 69 72 65 2e 20 46 6f 72 20 61 6e 20 41 54  xpire. For an AT
2110c 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2c 20  TACH statement, 
2110d 73 65 74 20 50 31 20 74 6f 20 74 72 75 65 20 28  set P1 to true (
2110e 65 78 70 69 72 65 20 74 68 69 73 0a 20 20 20 20  expire this.    
2110f 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 6c  ** statement onl
21110 79 29 2e 20 46 6f 72 20 44 45 54 41 43 48 2c 20  y). For DETACH, 
21111 73 65 74 20 69 74 20 74 6f 20 66 61 6c 73 65 20  set it to false 
21112 28 65 78 70 69 72 65 20 61 6c 6c 20 65 78 69 73  (expire all exis
21113 74 69 6e 67 0a 20 20 20 20 2a 2a 20 73 74 61 74  ting.    ** stat
21114 65 6d 65 6e 74 73 29 2e 0a 20 20 20 20 2a 2f 0a  ements)..    */.
21115 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21116 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 69  ddOp1(v, OP_Expi
21117 72 65 2c 20 28 74 79 70 65 3d 3d 53 51 4c 49 54  re, (type==SQLIT
21118 45 5f 41 54 54 41 43 48 29 29 3b 0a 20 20 7d 0a  E_ATTACH));.  }.
21119 20 20 0a 61 74 74 61 63 68 5f 65 6e 64 3a 0a 20    .attach_end:. 
2111a 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2111b 74 65 28 64 62 2c 20 70 46 69 6c 65 6e 61 6d 65  te(db, pFilename
2111c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2111d 44 65 6c 65 74 65 28 64 62 2c 20 70 44 62 6e 61  Delete(db, pDbna
2111e 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  me);.  sqlite3Ex
2111f 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4b 65  prDelete(db, pKe
21120 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c  y);.}../*.** Cal
21121 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
21122 72 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 20 44  r to compile a D
21123 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e  ETACH statement.
21124 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43  .**.**     DETAC
21125 48 20 70 44 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c  H pDbname.*/.SQL
21126 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21127 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 50   sqlite3Detach(P
21128 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
21129 70 72 20 2a 70 44 62 6e 61 6d 65 29 7b 0a 20 20  pr *pDbname){.  
2112a 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20 64  static FuncDef d
2112b 65 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a 20  etach_func = {. 
2112c 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20     1,           
2112d 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a       /* nArg */.
2112e 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c      SQLITE_UTF8,
2112f 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e        /* iPrefEn
21130 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  c */.    0,     
21131 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c             /* fl
21132 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ags */.    0,   
21133 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21134 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20 20 20  pUserData */.   
21135 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
21136 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20     /* pNext */. 
21137 20 20 20 64 65 74 61 63 68 46 75 6e 63 2c 20 20     detachFunc,  
21138 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a 2f       /* xFunc */
21139 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
2113a 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65 70 20         /* xStep 
2113b 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
2113c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6e           /* xFin
2113d 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73 71  alize */.    "sq
2113e 6c 69 74 65 5f 64 65 74 61 63 68 22 2c 20 20 2f  lite_detach",  /
2113f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30  * zName */.    0
21140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21141 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20 7d   /* pHash */.  }
21142 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28 70  ;.  codeAttach(p
21143 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45  Parse, SQLITE_DE
21144 54 41 43 48 2c 20 26 64 65 74 61 63 68 5f 66 75  TACH, &detach_fu
21145 6e 63 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c 20  nc, pDbname, 0, 
21146 30 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a  0, pDbname);.}..
21147 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20  /*.** Called by 
21148 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f  the parser to co
21149 6d 70 69 6c 65 20 61 6e 20 41 54 54 41 43 48 20  mpile an ATTACH 
2114a 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
2114b 20 20 20 20 20 41 54 54 41 43 48 20 70 20 41 53       ATTACH p AS
2114c 20 70 44 62 6e 61 6d 65 20 4b 45 59 20 70 4b 65   pDbname KEY pKe
2114d 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  y.*/.SQLITE_PRIV
2114e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2114f 41 74 74 61 63 68 28 50 61 72 73 65 20 2a 70 50  Attach(Parse *pP
21150 61 72 73 65 2c 20 45 78 70 72 20 2a 70 2c 20 45  arse, Expr *p, E
21151 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 45 78  xpr *pDbname, Ex
21152 70 72 20 2a 70 4b 65 79 29 7b 0a 20 20 73 74 61  pr *pKey){.  sta
21153 74 69 63 20 46 75 6e 63 44 65 66 20 61 74 74 61  tic FuncDef atta
21154 63 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20 20  ch_func = {.    
21155 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  3,              
21156 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20 20    /* nArg */.   
21157 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
21158 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a     /* iPrefEnc *
21159 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
2115a 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73          /* flags
2115b 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
2115c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73            /* pUs
2115d 65 72 44 61 74 61 20 2a 2f 0a 20 20 20 20 30 2c  erData */.    0,
2115e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2115f 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20  /* pNext */.    
21160 61 74 74 61 63 68 46 75 6e 63 2c 20 20 20 20 20  attachFunc,     
21161 20 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20 20    /* xFunc */.  
21162 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
21163 20 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0a      /* xStep */.
21164 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
21165 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69        /* xFinali
21166 7a 65 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69 74  ze */.    "sqlit
21167 65 5f 61 74 74 61 63 68 22 2c 20 20 2f 2a 20 7a  e_attach",  /* z
21168 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20 20  Name */.    0   
21169 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2116a 20 70 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20   pHash */.  };. 
2116b 20 63 6f 64 65 41 74 74 61 63 68 28 70 50 61 72   codeAttach(pPar
2116c 73 65 2c 20 53 51 4c 49 54 45 5f 41 54 54 41 43  se, SQLITE_ATTAC
2116d 48 2c 20 26 61 74 74 61 63 68 5f 66 75 6e 63 2c  H, &attach_func,
2116e 20 70 2c 20 70 2c 20 70 44 62 6e 61 6d 65 2c 20   p, p, pDbname, 
2116f 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  pKey);.}.#endif 
21170 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  /* SQLITE_OMIT_A
21171 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  TTACH */../*.** 
21172 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 44 62 46  Initialize a DbF
21173 69 78 65 72 20 73 74 72 75 63 74 75 72 65 2e 20  ixer structure. 
21174 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75   This routine mu
21175 73 74 20 62 65 20 63 61 6c 6c 65 64 20 70 72 69  st be called pri
21176 6f 72 0a 2a 2a 20 74 6f 20 70 61 73 73 69 6e 67  or.** to passing
21177 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 74   the structure t
21178 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 71 6c  o one of the sql
21179 69 74 65 46 69 78 41 41 41 41 28 29 20 72 6f 75  iteFixAAAA() rou
2117a 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 0a  tines below..**.
2117b 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
2117c 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 77 68  lue indicates wh
2117d 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 66 69 78  ether or not fix
2117e 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65  ation is require
2117f 64 2e 20 20 54 52 55 45 0a 2a 2a 20 6d 65 61 6e  d.  TRUE.** mean
21180 73 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20  s we do need to 
21181 66 69 78 20 74 68 65 20 64 61 74 61 62 61 73 65  fix the database
21182 20 72 65 66 65 72 65 6e 63 65 73 2c 20 46 41 4c   references, FAL
21183 53 45 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e  SE means we do n
21184 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ot..*/.SQLITE_PR
21185 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21186 33 46 69 78 49 6e 69 74 28 0a 20 20 44 62 46 69  3FixInit(.  DbFi
21187 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20  xer *pFix,      
21188 2f 2a 20 54 68 65 20 66 69 78 65 72 20 74 6f 20  /* The fixer to 
21189 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  be initialized *
2118a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  /.  Parse *pPars
2118b 65 2c 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72  e,      /* Error
2118c 20 6d 65 73 73 61 67 65 73 20 77 69 6c 6c 20 62   messages will b
2118d 65 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a  e written here *
2118e 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20  /.  int iDb,    
2118f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
21190 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
21191 74 68 61 74 20 6d 75 73 74 20 62 65 20 75 73 65  that must be use
21192 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
21193 72 20 2a 7a 54 79 70 65 2c 20 20 2f 2a 20 22 76  r *zType,  /* "v
21194 69 65 77 22 2c 20 22 74 72 69 67 67 65 72 22 2c  iew", "trigger",
21195 20 6f 72 20 22 69 6e 64 65 78 22 20 2a 2f 0a 20   or "index" */. 
21196 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e   const Token *pN
21197 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ame  /* Name of 
21198 74 68 65 20 76 69 65 77 2c 20 74 72 69 67 67 65  the view, trigge
21199 72 2c 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 29  r, or index */.)
2119a 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
2119b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 44  ..  if( NEVER(iD
2119c 62 3c 30 29 20 7c 7c 20 69 44 62 3d 3d 31 20 29  b<0) || iDb==1 )
2119d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20   return 0;.  db 
2119e 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2119f 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e  assert( db->nDb>
211a0 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70  iDb );.  pFix->p
211a1 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
211a2 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62    pFix->zDb = db
211a3 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
211a4 3b 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20  ;.  pFix->zType 
211a5 3d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 2d  = zType;.  pFix-
211a6 3e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a  >pName = pName;.
211a7 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
211a8 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
211a9 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e  ng set of routin
211aa 65 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 20  es walk through 
211ab 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 61  the parse tree a
211ac 6e 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73  nd assign.** a s
211ad 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73 65  pecific database
211ae 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 65   to all table re
211af 66 65 72 65 6e 63 65 73 20 77 68 65 72 65 20 74  ferences where t
211b0 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
211b1 0a 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e 73  .** was left uns
211b2 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
211b3 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61  original SQL sta
211b4 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69  tement.  The pFi
211b5 78 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d  x structure.** m
211b6 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ust have been in
211b7 69 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20 70  itialized by a p
211b8 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c  rior call to sql
211b9 69 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a  ite3FixInit()..*
211ba 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
211bb 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  nes are used to 
211bc 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61  make sure that a
211bd 6e 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65 72  n index, trigger
211be 2c 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20  , or.** view in 
211bf 6f 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f 65  one database doe
211c0 73 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 6f  s not refer to o
211c1 62 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66 66  bjects in a diff
211c2 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e 0a  erent database..
211c3 2a 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 69  ** (Exception: i
211c4 6e 64 69 63 65 73 2c 20 74 72 69 67 67 65 72 73  ndices, triggers
211c5 2c 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20 74  , and views in t
211c6 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
211c7 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20   are.** allowed 
211c8 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 74  to refer to anyt
211c9 68 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65 66  hing.)  If a ref
211ca 65 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69 63  erence is explic
211cb 69 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20  itly made.** to 
211cc 61 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 64  an object in a d
211cd 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61 73  ifferent databas
211ce 65 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  e, an error mess
211cf 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a  age is added to.
211d0 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  ** pParse->zErrM
211d1 73 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75  sg and these rou
211d2 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e  tines return non
211d3 2d 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79  -zero.  If every
211d4 74 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20  thing.** checks 
211d5 6f 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69  out, these routi
211d6 6e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f  nes return 0..*/
211d7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
211d8 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72  int sqlite3FixSr
211d9 63 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72  cList(.  DbFixer
211da 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a   *pFix,       /*
211db 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   Context of the 
211dc 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72  fixation */.  Sr
211dd 63 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20  cList *pList    
211de 20 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 65     /* The Source
211df 20 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20 61   list to check a
211e0 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a  nd modify */.){.
211e1 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74    int i;.  const
211e2 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74   char *zDb;.  st
211e3 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
211e4 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28  m *pItem;..  if(
211e5 20 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d 30 29   NEVER(pList==0)
211e6 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a   ) return 0;.  z
211e7 44 62 20 3d 20 70 46 69 78 2d 3e 7a 44 62 3b 0a  Db = pFix->zDb;.
211e8 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d    for(i=0, pItem
211e9 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69  =pList->a; i<pLi
211ea 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
211eb 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Item++){.    if(
211ec 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
211ed 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49  e==0 ){.      pI
211ee 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d  tem->zDatabase =
211ef 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
211f0 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2d 3e 64  (pFix->pParse->d
211f1 62 2c 20 7a 44 62 29 3b 0a 20 20 20 20 7d 65 6c  b, zDb);.    }el
211f2 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  se if( sqlite3St
211f3 72 49 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 44 61  rICmp(pItem->zDa
211f4 74 61 62 61 73 65 2c 7a 44 62 29 21 3d 30 20 29  tabase,zDb)!=0 )
211f5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
211f6 72 72 6f 72 4d 73 67 28 70 46 69 78 2d 3e 70 50  rrorMsg(pFix->pP
211f7 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 22  arse,.         "
211f8 25 73 20 25 54 20 63 61 6e 6e 6f 74 20 72 65 66  %s %T cannot ref
211f9 65 72 65 6e 63 65 20 6f 62 6a 65 63 74 73 20 69  erence objects i
211fa 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 0a  n database %s",.
211fb 20 20 20 20 20 20 20 20 20 70 46 69 78 2d 3e 7a           pFix->z
211fc 54 79 70 65 2c 20 70 46 69 78 2d 3e 70 4e 61 6d  Type, pFix->pNam
211fd 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62  e, pItem->zDatab
211fe 61 73 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ase);.      retu
211ff 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 69 66 20  rn 1;.    }.#if 
21200 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
21201 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
21202 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
21203 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20  IT_TRIGGER).    
21204 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53 65  if( sqlite3FixSe
21205 6c 65 63 74 28 70 46 69 78 2c 20 70 49 74 65 6d  lect(pFix, pItem
21206 2d 3e 70 53 65 6c 65 63 74 29 20 29 20 72 65 74  ->pSelect) ) ret
21207 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 73  urn 1;.    if( s
21208 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46  qlite3FixExpr(pF
21209 69 78 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 20  ix, pItem->pOn) 
2120a 29 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64  ) return 1;.#end
2120b 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
2120c 30 3b 0a 7d 0a 23 69 66 20 21 64 65 66 69 6e 65  0;.}.#if !define
2120d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
2120e 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  EW) || !defined(
2120f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
21210 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  GER).SQLITE_PRIV
21211 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
21212 69 78 53 65 6c 65 63 74 28 0a 20 20 44 62 46 69  ixSelect(.  DbFi
21213 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20  xer *pFix,      
21214 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74   /* Context of t
21215 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20  he fixation */. 
21216 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
21217 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
21218 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
21219 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65   be fixed to one
2121a 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a   database */.){.
2121b 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74    while( pSelect
2121c 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
2121d 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70  te3FixExprList(p
2121e 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45  Fix, pSelect->pE
2121f 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72  List) ){.      r
21220 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
21221 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
21222 78 53 72 63 4c 69 73 74 28 70 46 69 78 2c 20 70  xSrcList(pFix, p
21223 53 65 6c 65 63 74 2d 3e 70 53 72 63 29 20 29 7b  Select->pSrc) ){
21224 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
21225 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
21226 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46  qlite3FixExpr(pF
21227 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 57 68  ix, pSelect->pWh
21228 65 72 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ere) ){.      re
21229 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2122a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
2122b 45 78 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65  Expr(pFix, pSele
2122c 63 74 2d 3e 70 48 61 76 69 6e 67 29 20 29 7b 0a  ct->pHaving) ){.
2122d 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2122e 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63      }.    pSelec
2122f 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72  t = pSelect->pPr
21230 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ior;.  }.  retur
21231 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  n 0;.}.SQLITE_PR
21232 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21233 33 46 69 78 45 78 70 72 28 0a 20 20 44 62 46 69  3FixExpr(.  DbFi
21234 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f  xer *pFix,     /
21235 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
21236 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45   fixation */.  E
21237 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20  xpr *pExpr      
21238 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
21239 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20  ion to be fixed 
2123a 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
2123b 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  */.){.  while( p
2123c 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20  Expr ){.    if( 
2123d 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
2123e 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b  ty(pExpr, EP_Tok
2123f 65 6e 4f 6e 6c 79 7c 45 50 5f 53 70 61 6e 54 6f  enOnly|EP_SpanTo
21240 6b 65 6e 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  ken) ) break;.  
21241 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
21242 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21243 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
21244 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
21245 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70  ixSelect(pFix, p
21246 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29  Expr->x.pSelect)
21247 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
21248 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
21249 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
2124a 4c 69 73 74 28 70 46 69 78 2c 20 70 45 78 70 72  List(pFix, pExpr
2124b 2d 3e 78 2e 70 4c 69 73 74 29 20 29 20 72 65 74  ->x.pList) ) ret
2124c 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
2124d 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
2124e 78 70 72 28 70 46 69 78 2c 20 70 45 78 70 72 2d  xpr(pFix, pExpr-
2124f 3e 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20  >pRight) ){.    
21250 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
21251 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70 45  }.    pExpr = pE
21252 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a  xpr->pLeft;.  }.
21253 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51    return 0;.}.SQ
21254 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
21255 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c   sqlite3FixExprL
21256 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a  ist(.  DbFixer *
21257 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e  pFix,     /* Con
21258 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61  text of the fixa
21259 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69  tion */.  ExprLi
2125a 73 74 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a 20  st *pList    /* 
2125b 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  The expression t
2125c 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e  o be fixed to on
2125d 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b  e database */.){
2125e 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
2125f 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
21260 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70   *pItem;.  if( p
21261 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
21262 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   0;.  for(i=0, p
21263 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69  Item=pList->a; i
21264 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  <pList->nExpr; i
21265 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
21266 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
21267 45 78 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d  Expr(pFix, pItem
21268 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ->pExpr) ){.    
21269 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2126a 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
2126b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
2126c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2126d 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f 50  TRIGGER.SQLITE_P
2126e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2126f 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70  e3FixTriggerStep
21270 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69  (.  DbFixer *pFi
21271 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78  x,     /* Contex
21272 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f  t of the fixatio
21273 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74  n */.  TriggerSt
21274 65 70 20 2a 70 53 74 65 70 20 2f 2a 20 54 68 65  ep *pStep /* The
21275 20 74 72 69 67 67 65 72 20 73 74 65 70 20 62 65   trigger step be
21276 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61   fixed to one da
21277 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77  tabase */.){.  w
21278 68 69 6c 65 28 20 70 53 74 65 70 20 29 7b 0a 20  hile( pStep ){. 
21279 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
2127a 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 53  xSelect(pFix, pS
2127b 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b  tep->pSelect) ){
2127c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2127d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
2127e 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46  qlite3FixExpr(pF
2127f 69 78 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72  ix, pStep->pWher
21280 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  e) ){.      retu
21281 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
21282 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
21283 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53 74  prList(pFix, pSt
21284 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 20 29  ep->pExprList) )
21285 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
21286 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74 65  ;.    }.    pSte
21287 70 20 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78 74  p = pStep->pNext
21288 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
21289 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
2128a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2128b 6f 66 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a  of attach.c ****
2128c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2128d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2128e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2128f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
21290 6e 20 66 69 6c 65 20 61 75 74 68 2e 63 20 2a 2a  n file auth.c **
21291 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21292 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21293 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
21294 2a 20 32 30 30 33 20 4a 61 6e 75 61 72 79 20 31  * 2003 January 1
21295 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  1.**.** The auth
21296 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
21297 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
21298 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
21299 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2129a 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2129b 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2129c 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2129d 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2129e 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2129f 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
212a0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
212a1 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
212a2 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
212a3 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
212a4 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
212a5 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
212a6 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
212a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
212ab 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
212ac 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
212ad 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
212ae 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  e sqlite3_set_au
212af 74 68 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41 50  thorizer().** AP
212b0 49 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69 74  I.  This facilit
212b1 79 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c  y is an optional
212b2 20 66 65 61 74 75 72 65 20 6f 66 20 74 68 65 20   feature of the 
212b3 6c 69 62 72 61 72 79 2e 20 20 45 6d 62 65 64 64  library.  Embedd
212b4 65 64 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74 68  ed.** systems th
212b5 61 74 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74  at do not need t
212b6 68 69 73 20 66 61 63 69 6c 69 74 79 20 6d 61 79  his facility may
212b7 20 6f 6d 69 74 20 69 74 20 62 79 20 72 65 63 6f   omit it by reco
212b8 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6c  mpiling.** the l
212b9 69 62 72 61 72 79 20 77 69 74 68 20 2d 44 53 51  ibrary with -DSQ
212ba 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
212bb 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a 20  IZATION=1.**.** 
212bc 24 49 64 3a 20 61 75 74 68 2e 63 2c 76 20 31 2e  $Id: auth.c,v 1.
212bd 33 31 20 32 30 30 39 2f 30 35 2f 30 34 20 31 38  31 2009/05/04 18
212be 3a 30 31 3a 34 30 20 64 72 68 20 45 78 70 20 24  :01:40 drh Exp $
212bf 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f  .*/../*.** All o
212c0 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68  f the code in th
212c1 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20 6f  is file may be o
212c2 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e 69  mitted by defini
212c3 6e 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d  ng a single.** m
212c4 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  acro..*/.#ifndef
212c5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
212c6 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a 2a  HORIZATION../*.*
212c7 2a 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20 74  * Set or clear t
212c8 68 65 20 61 63 63 65 73 73 20 61 75 74 68 6f 72  he access author
212c9 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ization function
212ca 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63 65  ..**.** The acce
212cb 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ss authorization
212cc 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 20   function is be 
212cd 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 68  called during th
212ce 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a 2a  e compilation.**
212cf 20 70 68 61 73 65 20 74 6f 20 76 65 72 69 66 79   phase to verify
212d0 20 74 68 61 74 20 74 68 65 20 75 73 65 72 20 68   that the user h
212d1 61 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20 77  as read and/or w
212d2 72 69 74 65 20 61 63 63 65 73 73 20 70 65 72 6d  rite access perm
212d3 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61 72  ission on.** var
212d4 69 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20 74  ious fields of t
212d5 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
212d6 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
212d7 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
212d8 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63 6f  ction.** is a co
212d9 70 79 20 6f 66 20 74 68 65 20 33 72 64 20 61 72  py of the 3rd ar
212da 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 72  gument to this r
212db 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65 63  outine.  The sec
212dc 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ond argument.** 
212dd 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  to the auth func
212de 74 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20 74  tion is one of t
212df 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a 0a  hese constants:.
212e0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  **.**       SQLI
212e1 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 0a  TE_CREATE_INDEX.
212e2 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
212e3 43 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a 20  CREATE_TABLE.** 
212e4 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45        SQLITE_CRE
212e5 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a  ATE_TEMP_INDEX.*
212e6 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43  *       SQLITE_C
212e7 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45  REATE_TEMP_TABLE
212e8 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
212e9 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49  _CREATE_TEMP_TRI
212ea 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
212eb 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
212ec 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53  _VIEW.**       S
212ed 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49  QLITE_CREATE_TRI
212ee 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
212ef 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57  LITE_CREATE_VIEW
212f0 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
212f1 5f 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 20  _DELETE.**      
212f2 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44   SQLITE_DROP_IND
212f3 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  EX.**       SQLI
212f4 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a 2a  TE_DROP_TABLE.**
212f5 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
212f6 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a  OP_TEMP_INDEX.**
212f7 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
212f8 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a  OP_TEMP_TABLE.**
212f9 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
212fa 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a  OP_TEMP_TRIGGER.
212fb 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
212fc 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a 2a  DROP_TEMP_VIEW.*
212fd 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
212fe 52 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20  ROP_TRIGGER.**  
212ff 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
21300 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53  _VIEW.**       S
21301 51 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a 20  QLITE_INSERT.** 
21302 20 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52 41        SQLITE_PRA
21303 47 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  GMA.**       SQL
21304 49 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20 20  ITE_READ.**     
21305 20 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 0a    SQLITE_SELECT.
21306 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
21307 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20  TRANSACTION.**  
21308 20 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44 41       SQLITE_UPDA
21309 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  TE.**.** The thi
2130a 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72  rd and fourth ar
2130b 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 61  guments to the a
2130c 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72 65  uth function are
2130d 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20   the name of.** 
2130e 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68  the table and th
2130f 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 72  e column that ar
21310 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65 64  e being accessed
21311 2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e 63  .  The auth func
21312 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 72  tion.** should r
21313 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c  eturn either SQL
21314 49 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 44  ITE_OK, SQLITE_D
21315 45 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f 49  ENY, or SQLITE_I
21316 47 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53 51  GNORE.  If.** SQ
21317 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
21318 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  ned, it means th
21319 61 74 20 61 63 63 65 73 73 20 69 73 20 61 6c 6c  at access is all
2131a 6f 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44 45  owed.  SQLITE_DE
2131b 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74  NY.** means that
2131c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
2131d 6e 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72 75  nt will never-ru
2131e 6e 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 5f  n - the sqlite3_
2131f 65 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20 77  exec() call.** w
21320 69 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68 20  ill return with 
21321 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54  an error.  SQLIT
21322 45 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74  E_IGNORE means t
21323 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 74  hat the SQL stat
21324 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64 20  ement.** should 
21325 72 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74 73  run but attempts
21326 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 70 65   to read the spe
21327 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77 69  cified column wi
21328 6c 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a  ll return NULL.*
21329 2a 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20 74  * and attempts t
2132a 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c 75  o write the colu
2132b 6d 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f 72  mn will be ignor
2132c 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e  ed..**.** Settin
2132d 67 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  g the auth funct
2132e 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73 61  ion to NULL disa
2132f 62 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e 20  bles this hook. 
21330 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20   The default.** 
21331 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 61  setting of the a
21332 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  uth function is 
21333 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
21334 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
21335 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a  set_authorizer(.
21336 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
21337 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
21338 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
21339 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
2133a 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
2133b 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64  t char*),.  void
2133c 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c 69   *pArg.){.  sqli
2133d 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2133e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62  db->mutex);.  db
2133f 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b  ->xAuth = xAuth;
21340 0a 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67 20  .  db->pAuthArg 
21341 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  = pArg;.  sqlite
21342 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
21343 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20  tatements(db);. 
21344 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
21345 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
21346 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
21347 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72  _OK;.}../*.** Wr
21348 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
21349 73 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73 65  sage into pParse
2134a 2d 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20 65  ->zErrMsg that e
2134b 78 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68 65  xplains that the
2134c 0a 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69 65  .** user-supplie
2134d 64 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  d authorization 
2134e 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65  function returne
2134f 64 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c  d an illegal val
21350 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ue..*/.static vo
21351 69 64 20 73 71 6c 69 74 65 41 75 74 68 42 61 64  id sqliteAuthBad
21352 52 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73 65  ReturnCode(Parse
21353 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
21354 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
21355 72 73 65 2c 20 22 61 75 74 68 6f 72 69 7a 65 72  rse, "authorizer
21356 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 22 29 3b 0a   malfunction");.
21357 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
21358 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a  QLITE_ERROR;.}..
21359 2f 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 20  /*.** The pExpr 
2135a 73 68 6f 75 6c 64 20 62 65 20 61 20 54 4b 5f 43  should be a TK_C
2135b 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e  OLUMN expression
2135c 2e 20 20 54 68 65 20 74 61 62 6c 65 20 72 65 66  .  The table ref
2135d 65 72 72 65 64 20 74 6f 0a 2a 2a 20 69 73 20 69  erred to.** is i
2135e 6e 20 70 54 61 62 4c 69 73 74 20 6f 72 20 65 6c  n pTabList or el
2135f 73 65 20 69 74 20 69 73 20 74 68 65 20 4e 45 57  se it is the NEW
21360 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 20 6f 66   or OLD table of
21361 20 61 20 74 72 69 67 67 65 72 2e 20 20 0a 2a 2a   a trigger.  .**
21362 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
21363 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 72 65 61   it is OK to rea
21364 64 20 74 68 69 73 20 70 61 72 74 69 63 75 6c 61  d this particula
21365 72 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  r column..**.** 
21366 49 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  If the auth func
21367 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c  tion returns SQL
21368 49 54 45 5f 49 47 4e 4f 52 45 2c 20 63 68 61 6e  ITE_IGNORE, chan
21369 67 65 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e  ge the TK_COLUMN
2136a 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e   .** instruction
2136b 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c 4c 2e   into a TK_NULL.
2136c 20 20 49 66 20 74 68 65 20 61 75 74 68 20 66 75    If the auth fu
2136d 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53  nction returns S
2136e 51 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a 2a 20 74  QLITE_DENY,.** t
2136f 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e 20  hen generate an 
21370 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  error..*/.SQLITE
21371 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
21372 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 0a 20  lite3AuthRead(. 
21373 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
21374 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
21375 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
21376 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20    Expr *pExpr,  
21377 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
21378 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 63 68 65  xpression to che
21379 63 6b 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ck authorization
2137a 20 6f 6e 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20   on */.  Schema 
2137b 2a 70 53 63 68 65 6d 61 2c 20 20 20 20 20 20 2f  *pSchema,      /
2137c 2a 20 54 68 65 20 73 63 68 65 6d 61 20 6f 66 20  * The schema of 
2137d 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  the expression *
2137e 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
2137f 62 4c 69 73 74 20 20 20 20 20 2f 2a 20 41 6c 6c  bList     /* All
21380 20 74 61 62 6c 65 20 74 68 61 74 20 70 45 78 70   table that pExp
21381 72 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f  r might refer to
21382 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
21383 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
21384 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54  b;.  int rc;.  T
21385 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20  able *pTab = 0; 
21386 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
21387 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a  e being read */.
21388 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
21389 6f 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ol;     /* Name 
2138a 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66  of the column of
2138b 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
2138c 69 6e 74 20 69 53 72 63 3b 20 20 20 20 20 20 20  int iSrc;       
2138d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
2138e 6e 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 20  n pTabList->a[] 
2138f 6f 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72  of table being r
21390 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ead */.  const c
21391 68 61 72 20 2a 7a 44 42 61 73 65 3b 20 20 20 2f  har *zDBase;   /
21392 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
21393 73 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65  se being accesse
21394 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74  d */.  TriggerSt
21395 61 63 6b 20 2a 70 53 74 61 63 6b 3b 20 2f 2a 20  ack *pStack; /* 
21396 54 68 65 20 73 74 61 63 6b 20 6f 66 20 63 75 72  The stack of cur
21397 72 65 6e 74 20 74 72 69 67 67 65 72 73 20 2a 2f  rent triggers */
21398 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
21399 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2139a 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
2139b 61 62 61 73 65 20 74 68 65 20 65 78 70 72 65 73  abase the expres
2139c 73 69 6f 6e 20 72 65 66 65 72 73 20 74 6f 20 2a  sion refers to *
2139d 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75  /..  if( db->xAu
2139e 74 68 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  th==0 ) return;.
2139f 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
213a0 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29  >op==TK_COLUMN )
213a1 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
213a2 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
213a3 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 63 68 65  Parse->db, pSche
213a4 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30  ma);.  if( iDb<0
213a5 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74   ){.    /* An at
213a6 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 61 20  tempt to read a 
213a7 63 6f 6c 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20  column out of a 
213a8 73 75 62 71 75 65 72 79 20 6f 72 20 6f 74 68 65  subquery or othe
213a9 72 0a 20 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61  r.    ** tempora
213aa 72 79 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20  ry table. */.   
213ab 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
213ac 66 28 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20  f( pTabList ){. 
213ad 20 20 20 66 6f 72 28 69 53 72 63 3d 30 3b 20 41     for(iSrc=0; A
213ae 4c 57 41 59 53 28 69 53 72 63 3c 70 54 61 62 4c  LWAYS(iSrc<pTabL
213af 69 73 74 2d 3e 6e 53 72 63 29 3b 20 69 53 72 63  ist->nSrc); iSrc
213b0 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
213b1 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 54  Expr->iTable==pT
213b2 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e  abList->a[iSrc].
213b3 69 43 75 72 73 6f 72 20 29 20 62 72 65 61 6b 3b  iCursor ) break;
213b4 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
213b5 74 28 20 69 53 72 63 3c 70 54 61 62 4c 69 73 74  t( iSrc<pTabList
213b6 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 70 54  ->nSrc );.    pT
213b7 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  ab = pTabList->a
213b8 5b 69 53 72 63 5d 2e 70 54 61 62 3b 0a 20 20 7d  [iSrc].pTab;.  }
213b9 65 6c 73 65 7b 0a 20 20 20 20 70 53 74 61 63 6b  else{.    pStack
213ba 20 3d 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53   = pParse->trigS
213bb 74 61 63 6b 3b 0a 20 20 20 20 69 66 28 20 41 4c  tack;.    if( AL
213bc 57 41 59 53 28 70 53 74 61 63 6b 29 20 29 7b 0a  WAYS(pStack) ){.
213bd 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6d 75        /* This mu
213be 73 74 20 62 65 20 61 6e 20 61 74 74 65 6d 70 74  st be an attempt
213bf 20 74 6f 20 72 65 61 64 20 74 68 65 20 4e 45 57   to read the NEW
213c0 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74   or OLD pseudo-t
213c1 61 62 6c 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f  ables.      ** o
213c2 66 20 61 20 74 72 69 67 67 65 72 2e 0a 20 20 20  f a trigger..   
213c3 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65     */.      asse
213c4 72 74 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  rt( pExpr->iTabl
213c5 65 3d 3d 70 53 74 61 63 6b 2d 3e 6e 65 77 49 64  e==pStack->newId
213c6 78 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62  x || pExpr->iTab
213c7 6c 65 3d 3d 70 53 74 61 63 6b 2d 3e 6f 6c 64 49  le==pStack->oldI
213c8 64 78 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62  dx );.      pTab
213c9 20 3d 20 70 53 74 61 63 6b 2d 3e 70 54 61 62 3b   = pStack->pTab;
213ca 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
213cb 20 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20   NEVER(pTab==0) 
213cc 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
213cd 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d  pExpr->iColumn>=
213ce 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
213cf 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c   pExpr->iColumn<
213d0 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
213d1 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
213d2 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75  Col[pExpr->iColu
213d3 6d 6e 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c  mn].zName;.  }el
213d4 73 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b  se if( pTab->iPK
213d5 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  ey>=0 ){.    ass
213d6 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79  ert( pTab->iPKey
213d7 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  <pTab->nCol );. 
213d8 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     zCol = pTab->
213d9 61 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79  aCol[pTab->iPKey
213da 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65  ].zName;.  }else
213db 7b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f  {.    zCol = "RO
213dc 57 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65  WID";.  }.  asse
213dd 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
213de 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a  b<db->nDb );.  z
213df 44 42 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b  DBase = db->aDb[
213e0 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63  iDb].zName;.  rc
213e1 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d   = db->xAuth(db-
213e2 3e 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54  >pAuthArg, SQLIT
213e3 45 5f 52 45 41 44 2c 20 70 54 61 62 2d 3e 7a 4e  E_READ, pTab->zN
213e4 61 6d 65 2c 20 7a 43 6f 6c 2c 20 7a 44 42 61 73  ame, zCol, zDBas
213e5 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
213e6 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75       pParse->zAu
213e7 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66  thContext);.  if
213e8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e  ( rc==SQLITE_IGN
213e9 4f 52 45 20 29 7b 0a 20 20 20 20 70 45 78 70 72  ORE ){.    pExpr
213ea 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a  ->op = TK_NULL;.
213eb 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d    }else if( rc==
213ec 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
213ed 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32     if( db->nDb>2
213ee 20 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20 20   || iDb!=0 ){.  
213ef 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
213f0 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63  Msg(pParse, "acc
213f1 65 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73 20  ess to %s.%s.%s 
213f2 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20  is prohibited", 
213f3 0a 20 20 20 20 20 20 20 20 20 7a 44 42 61 73 65  .         zDBase
213f4 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  , pTab->zName, z
213f5 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Col);.    }else{
213f6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
213f7 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
213f8 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20  access to %s.%s 
213f9 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 70  is prohibited",p
213fa 54 61 62 2d 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29  Tab->zName,zCol)
213fb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
213fc 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
213fd 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66  AUTH;.  }else if
213fe 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
213ff 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74  ){.    sqliteAut
21400 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70  hBadReturnCode(p
21401 50 61 72 73 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Parse);.  }.}../
21402 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68 6f  *.** Do an autho
21403 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20 75  rization check u
21404 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20 61 6e  sing the code an
21405 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69 76 65  d arguments give
21406 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 65 69  n.  Return.** ei
21407 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28  ther SQLITE_OK (
21408 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54 45 5f  zero) or SQLITE_
21409 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54 45  IGNORE or SQLITE
2140a 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c 49 54  _DENY.  If SQLIT
2140b 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72 65 74  E_DENY.** is ret
2140c 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20  urned, then the 
2140d 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64 20  error count and 
2140e 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
2140f 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a 20 6d   pParse are.** m
21410 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70 72 69  odified appropri
21411 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ately..*/.SQLITE
21412 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
21413 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 0a 20  ite3AuthCheck(. 
21414 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a   Parse *pParse,.
21415 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20 63 6f    int code,.  co
21416 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 31 2c  nst char *zArg1,
21417 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
21418 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Arg2,.  const ch
21419 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20 20 73  ar *zArg3.){.  s
2141a 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2141b 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72  rse->db;.  int r
2141c 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 64  c;..  /* Don't d
2141d 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a 61 74  o any authorizat
2141e 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74 68  ion checks if th
2141f 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e  e database is in
21420 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a 2a 20  itialising.  ** 
21421 6f 72 20 69 66 20 74 68 65 20 70 61 72 73 65 72  or if the parser
21422 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f 6b 65   is being invoke
21423 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 73 71  d from within sq
21424 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
21425 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ab..  */.  if( d
21426 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20  b->init.busy || 
21427 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
21428 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
21429 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
2142a 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30  if( db->xAuth==0
2142b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2142c 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
2142d 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64  rc = db->xAuth(d
2142e 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 63 6f 64  b->pAuthArg, cod
2142f 65 2c 20 7a 41 72 67 31 2c 20 7a 41 72 67 32 2c  e, zArg1, zArg2,
21430 20 7a 41 72 67 33 2c 20 70 50 61 72 73 65 2d 3e   zArg3, pParse->
21431 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20  zAuthContext);. 
21432 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
21433 44 45 4e 59 20 29 7b 0a 20 20 20 20 73 71 6c 69  DENY ){.    sqli
21434 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
21435 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 69  se, "not authori
21436 7a 65 64 22 29 3b 0a 20 20 20 20 70 50 61 72 73  zed");.    pPars
21437 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41  e->rc = SQLITE_A
21438 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  UTH;.  }else if(
21439 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
2143a 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 47 4e  & rc!=SQLITE_IGN
2143b 4f 52 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ORE ){.    rc = 
2143c 53 51 4c 49 54 45 5f 44 45 4e 59 3b 0a 20 20 20  SQLITE_DENY;.   
2143d 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65   sqliteAuthBadRe
2143e 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73 65 29  turnCode(pParse)
2143f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
21440 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68  c;.}../*.** Push
21441 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   an authorizatio
21442 6e 20 63 6f 6e 74 65 78 74 2e 20 20 41 66 74 65  n context.  Afte
21443 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  r this routine i
21444 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a 2a  s called, the.**
21445 20 7a 41 72 67 33 20 61 72 67 75 6d 65 6e 74 20   zArg3 argument 
21446 74 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  to authorization
21447 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20   callbacks will 
21448 62 65 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74 69  be zContext unti
21449 6c 0a 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f 72  l.** popped.  Or
2144a 20 69 66 20 70 50 61 72 73 65 3d 3d 30 2c 20 74   if pParse==0, t
2144b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
2144c 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
2144d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2144e 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
2144f 74 50 75 73 68 28 0a 20 20 50 61 72 73 65 20 2a  tPush(.  Parse *
21450 70 50 61 72 73 65 2c 0a 20 20 41 75 74 68 43 6f  pParse,.  AuthCo
21451 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
21452 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
21453 7a 43 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 61 73  zContext.){.  as
21454 73 65 72 74 28 20 70 50 61 72 73 65 20 29 3b 0a  sert( pParse );.
21455 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72    pContext->pPar
21456 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70  se = pParse;.  p
21457 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f  Context->zAuthCo
21458 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e  ntext = pParse->
21459 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20  zAuthContext;.  
2145a 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
2145b 74 65 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b  text = zContext;
2145c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 61 6e  .}../*.** Pop an
2145d 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
2145e 6f 6e 74 65 78 74 20 74 68 61 74 20 77 61 73 20  ontext that was 
2145f 70 72 65 76 69 6f 75 73 6c 79 20 70 75 73 68 65  previously pushe
21460 64 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 41  d.** by sqlite3A
21461 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 0a 2a  uthContextPush.*
21462 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21463 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
21464 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68  hContextPop(Auth
21465 43 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  Context *pContex
21466 74 29 7b 0a 20 20 69 66 28 20 70 43 6f 6e 74 65  t){.  if( pConte
21467 78 74 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20  xt->pParse ){.  
21468 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72    pContext->pPar
21469 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
2146a 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75   = pContext->zAu
2146b 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70  thContext;.    p
2146c 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20  Context->pParse 
2146d 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64  = 0;.  }.}..#end
2146e 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2146f 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20  T_AUTHORIZATION 
21470 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
21471 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 75 74 68 2e  *** End of auth.
21472 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
21473 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21474 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21475 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
21476 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
21477 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  uild.c *********
21478 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21479 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2147a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
2147b 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
2147c 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2147d 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2147e 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2147f 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
21480 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
21481 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
21482 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
21483 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
21484 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
21485 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
21486 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
21487 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
21488 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
21489 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2148a 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2148b 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2148c 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2148d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2148e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2148f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21491 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
21492 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
21493 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73   C code routines
21494 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64   that are called
21495 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 70   by the SQLite p
21496 61 72 73 65 72 0a 2a 2a 20 77 68 65 6e 20 73 79  arser.** when sy
21497 6e 74 61 78 20 72 75 6c 65 73 20 61 72 65 20 72  ntax rules are r
21498 65 64 75 63 65 64 2e 20 20 54 68 65 20 72 6f 75  educed.  The rou
21499 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
2149a 6c 65 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a  le handle the.**
2149b 20 66 6f 6c 6c 6f 77 69 6e 67 20 6b 69 6e 64 73   following kinds
2149c 20 6f 66 20 53 51 4c 20 73 79 6e 74 61 78 3a 0a   of SQL syntax:.
2149d 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45  **.**     CREATE
2149e 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 44 52   TABLE.**     DR
2149f 4f 50 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20  OP TABLE.**     
214a0 43 52 45 41 54 45 20 49 4e 44 45 58 0a 2a 2a 20  CREATE INDEX.** 
214a1 20 20 20 20 44 52 4f 50 20 49 4e 44 45 58 0a 2a      DROP INDEX.*
214a2 2a 20 20 20 20 20 63 72 65 61 74 69 6e 67 20 49  *     creating I
214a3 44 20 6c 69 73 74 73 0a 2a 2a 20 20 20 20 20 42  D lists.**     B
214a4 45 47 49 4e 20 54 52 41 4e 53 41 43 54 49 4f 4e  EGIN TRANSACTION
214a5 0a 2a 2a 20 20 20 20 20 43 4f 4d 4d 49 54 0a 2a  .**     COMMIT.*
214a6 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 0a 2a  *     ROLLBACK.*
214a7 2a 0a 2a 2a 20 24 49 64 3a 20 62 75 69 6c 64 2e  *.** $Id: build.
214a8 63 2c 76 20 31 2e 35 33 37 20 32 30 30 39 2f 30  c,v 1.537 2009/0
214a9 35 2f 30 36 20 31 38 3a 34 32 3a 32 31 20 64 72  5/06 18:42:21 dr
214aa 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
214ab 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
214ac 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20  s called when a 
214ad 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  new SQL statemen
214ae 74 20 69 73 20 62 65 67 69 6e 6e 69 6e 67 20 74  t is beginning t
214af 6f 0a 2a 2a 20 62 65 20 70 61 72 73 65 64 2e 20  o.** be parsed. 
214b0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
214b1 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65  pParse structure
214b2 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53   as needed..*/.S
214b3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
214b4 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50  id sqlite3BeginP
214b5 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 61 72  arse(Parse *pPar
214b6 73 65 2c 20 69 6e 74 20 65 78 70 6c 61 69 6e 46  se, int explainF
214b7 6c 61 67 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e  lag){.  pParse->
214b8 65 78 70 6c 61 69 6e 20 3d 20 28 75 38 29 65 78  explain = (u8)ex
214b9 70 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61  plainFlag;.  pPa
214ba 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d  rse->nVar = 0;.}
214bb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
214bc 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
214bd 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62  HE./*.** The Tab
214be 6c 65 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65  leLock structure
214bf 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79   is only used by
214c0 20 74 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c   the sqlite3Tabl
214c1 65 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63  eLock() and.** c
214c2 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20  odeTableLocks() 
214c3 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  functions..*/.st
214c4 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b  ruct TableLock {
214c5 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
214c6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
214c7 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
214c8 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  ng the table to 
214c9 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69  be locked */.  i
214ca 6e 74 20 69 54 61 62 3b 20 20 20 20 20 20 20 20  nt iTab;        
214cb 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20      /* The root 
214cc 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c  page of the tabl
214cd 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
214ce 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f  /.  u8 isWriteLo
214cf 63 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ck;      /* True
214d0 20 66 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e   for write lock.
214d1 20 20 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65    False for a re
214d2 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e  ad lock */.  con
214d3 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  st char *zName; 
214d4 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
214d5 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   table */.};../*
214d6 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66  .** Record the f
214d7 61 63 74 20 74 68 61 74 20 77 65 20 77 61 6e 74  act that we want
214d8 20 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65   to lock a table
214d9 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a   at run-time.  .
214da 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
214db 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73  to be locked has
214dc 20 72 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20   root page iTab 
214dd 61 6e 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20  and is found in 
214de 64 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a  database iDb..**
214df 20 41 20 72 65 61 64 20 6f 72 20 61 20 77 72 69   A read or a wri
214e0 74 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74  te lock can be t
214e1 61 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f  aken depending o
214e2 6e 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a  n isWritelock..*
214e3 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
214e4 65 20 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74  e just records t
214e5 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
214e6 20 6c 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64   lock is desired
214e7 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74  .  The.** code t
214e8 6f 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20  o make the lock 
214e9 6f 63 63 75 72 20 69 73 20 67 65 6e 65 72 61 74  occur is generat
214ea 65 64 20 62 79 20 61 20 6c 61 74 65 72 20 63 61  ed by a later ca
214eb 6c 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62  ll to.** codeTab
214ec 6c 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20  leLocks() which 
214ed 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71  occurs during sq
214ee 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e  lite3FinishCodin
214ef 67 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  g()..*/.SQLITE_P
214f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
214f1 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20  te3TableLock(.  
214f2 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
214f3 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
214f4 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
214f5 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Db,           /*
214f6 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
214f7 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
214f8 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c  g the table to l
214f9 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ock */.  int iTa
214fa 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  b,          /* R
214fb 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
214fc 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
214fd 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75  be locked */.  u
214fe 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20  8 isWriteLock,  
214ff 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
21500 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  write lock */.  
21501 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
21502 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  e  /* Name of th
21503 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f  e table to be lo
21504 63 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  cked */.){.  int
21505 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   i;.  int nBytes
21506 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70  ;.  TableLock *p
21507 3b 0a 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29  ;..  if( iDb<0 )
21508 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
21509 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  }..  for(i=0; i<
2150a 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
2150b 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20  ck; i++){.    p 
2150c 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c  = &pParse->aTabl
2150d 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66  eLock[i];.    if
2150e 28 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26  ( p->iDb==iDb &&
2150f 20 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62 20 29   p->iTab==iTab )
21510 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 57 72 69  {.      p->isWri
21511 74 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57  teLock = (p->isW
21512 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72  riteLock || isWr
21513 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20  iteLock);.      
21514 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
21515 7d 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20 73 69  }..  nBytes = si
21516 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20  zeof(TableLock) 
21517 2a 20 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c  * (pParse->nTabl
21518 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 50 61 72  eLock+1);.  pPar
21519 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d  se->aTableLock =
2151a 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44   .      sqlite3D
2151b 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70  bReallocOrFree(p
2151c 50 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73  Parse->db, pPars
2151d 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e  e->aTableLock, n
2151e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 50  Bytes);.  if( pP
2151f 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
21520 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61   ){.    p = &pPa
21521 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b  rse->aTableLock[
21522 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
21523 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44  ck++];.    p->iD
21524 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e  b = iDb;.    p->
21525 69 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20  iTab = iTab;.   
21526 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20   p->isWriteLock 
21527 3d 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20  = isWriteLock;. 
21528 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e     p->zName = zN
21529 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ame;.  }else{.  
2152a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65    pParse->nTable
2152b 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50  Lock = 0;.    pP
2152c 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
2152d 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
2152e 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e  }../*.** Code an
2152f 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e   OP_TableLock in
21530 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61  struction for ea
21531 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20  ch table locked 
21532 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d  by the.** statem
21533 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20  ent (configured 
21534 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  by calls to sqli
21535 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e  te3TableLock()).
21536 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
21537 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50  codeTableLocks(P
21538 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
21539 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a   int i;.  Vdbe *
2153a 70 56 64 62 65 3b 20 0a 0a 20 20 69 66 28 20 30  pVdbe; ..  if( 0
2153b 3d 3d 28 70 56 64 62 65 20 3d 20 73 71 6c 69 74  ==(pVdbe = sqlit
2153c 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2153d 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
2153e 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30  ;.  }..  for(i=0
2153f 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62  ; i<pParse->nTab
21540 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20  leLock; i++){.  
21541 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d    TableLock *p =
21542 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65   &pParse->aTable
21543 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74  Lock[i];.    int
21544 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20   p1 = p->iDb;.  
21545 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21546 4f 70 34 28 70 56 64 62 65 2c 20 4f 50 5f 54 61  Op4(pVdbe, OP_Ta
21547 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e  bleLock, p1, p->
21548 69 54 61 62 2c 20 70 2d 3e 69 73 57 72 69 74 65  iTab, p->isWrite
21549 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20  Lock,.          
2154a 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 7a              p->z
2154b 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
2154c 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20  ;.  }.}.#else.  
2154d 23 64 65 66 69 6e 65 20 63 6f 64 65 54 61 62 6c  #define codeTabl
2154e 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e 64 69 66  eLocks(x).#endif
2154f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
21550 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61  tine is called a
21551 66 74 65 72 20 61 20 73 69 6e 67 6c 65 20 53 51  fter a single SQ
21552 4c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  L statement has 
21553 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61  been.** parsed a
21554 6e 64 20 61 20 56 44 42 45 20 70 72 6f 67 72 61  nd a VDBE progra
21555 6d 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 61  m to execute tha
21556 74 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  t statement has 
21557 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64  been.** prepared
21558 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
21559 70 75 74 73 20 74 68 65 20 66 69 6e 69 73 68 69  puts the finishi
2155a 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e 20 74 68  ng touches on th
2155b 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f 67 72 61  e.** VDBE progra
2155c 6d 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65  m and resets the
2155d 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72   pParse structur
2155e 65 20 66 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a  e for the next.*
2155f 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e  * parse..**.** N
21560 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 20 65  ote that if an e
21561 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 69  rror occurred, i
21562 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63  t might be the c
21563 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56  ase that.** no V
21564 44 42 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e  DBE code was gen
21565 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  erated..*/.SQLIT
21566 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21567 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
21568 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ng(Parse *pParse
21569 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2156a 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  ;.  Vdbe *v;..  
2156b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2156c 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2156d 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
2156e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2156f 6e 65 73 74 65 64 20 29 20 72 65 74 75 72 6e 3b  nested ) return;
21570 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
21571 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  Err ) return;.. 
21572 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 67 65 6e   /* Begin by gen
21573 65 72 61 74 69 6e 67 20 73 6f 6d 65 20 74 65 72  erating some ter
21574 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 20 61 74  mination code at
21575 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a   the end of the.
21576 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72 61    ** vdbe progra
21577 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  m.  */.  v = sql
21578 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
21579 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
2157a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2157b 64 64 4f 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp0(v, OP_Halt
2157c 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
2157d 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61  ookie mask conta
2157e 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20  ins one bit for 
2157f 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69  each database fi
21580 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20  le open..    ** 
21581 28 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61  (Bit 0 is for ma
21582 69 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72  in, bit 1 is for
21583 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f   temp, and so fo
21584 72 74 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a  rth.)  Bits are.
21585 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65      ** set for e
21586 61 63 68 20 64 61 74 61 62 61 73 65 20 74 68 61  ach database tha
21587 74 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65  t is used.  Gene
21588 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61  rate code to sta
21589 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e  rt a.    ** tran
2158a 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20  saction on each 
2158b 75 73 65 64 20 64 61 74 61 62 61 73 65 20 61 6e  used database an
2158c 64 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 20  d to verify the 
2158d 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20  schema cookie.  
2158e 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65    ** on each use
2158f 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  d database..    
21590 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73  */.    if( pPars
21591 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20  e->cookieGoto>0 
21592 29 7b 0a 20 20 20 20 20 20 75 33 32 20 6d 61 73  ){.      u32 mas
21593 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  k;.      int iDb
21594 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21595 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70  dbeJumpHere(v, p
21596 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
21597 6f 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  o-1);.      for(
21598 69 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69  iDb=0, mask=1; i
21599 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b  Db<db->nDb; mask
2159a 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20  <<=1, iDb++){.  
2159b 20 20 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20        if( (mask 
2159c 26 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  & pParse->cookie
2159d 4d 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69  Mask)==0 ) conti
2159e 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nue;.        sql
2159f 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
215a0 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  e(v, iDb);.     
215a1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
215a2 64 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61  dOp2(v,OP_Transa
215a3 63 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73  ction, iDb, (mas
215a4 6b 20 26 20 70 50 61 72 73 65 2d 3e 77 72 69 74  k & pParse->writ
215a5 65 4d 61 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20  eMask)!=0);.    
215a6 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74      if( db->init
215a7 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20  .busy==0 ){.    
215a8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
215a9 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 56 65 72  eAddOp2(v,OP_Ver
215aa 69 66 79 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  ifyCookie, iDb, 
215ab 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61  pParse->cookieVa
215ac 6c 75 65 5b 69 44 62 5d 29 3b 0a 20 20 20 20 20  lue[iDb]);.     
215ad 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66     }.      }.#if
215ae 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
215af 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
215b0 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69 6e      {.        in
215b1 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t i;.        for
215b2 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e  (i=0; i<pParse->
215b3 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b  nVtabLock; i++){
215b4 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20  .          char 
215b5 2a 76 74 61 62 20 3d 20 28 63 68 61 72 20 2a 29  *vtab = (char *)
215b6 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f  pParse->apVtabLo
215b7 63 6b 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20 20  ck[i]->pVtab;.  
215b8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
215b9 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
215ba 56 42 65 67 69 6e 2c 20 30 2c 20 30 2c 20 30 2c  VBegin, 0, 0, 0,
215bb 20 76 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b   vtab, P4_VTAB);
215bc 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
215bd 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 74 61 62     pParse->nVtab
215be 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20  Lock = 0;.      
215bf 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20  }.#endif..      
215c0 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20  /* Once all the 
215c1 63 6f 6f 6b 69 65 73 20 68 61 76 65 20 62 65 65  cookies have bee
215c2 6e 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 74  n verified and t
215c3 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 65 6e  ransactions open
215c4 65 64 2c 20 0a 20 20 20 20 20 20 2a 2a 20 6f 62  ed, .      ** ob
215c5 74 61 69 6e 20 74 68 65 20 72 65 71 75 69 72 65  tain the require
215c6 64 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 2e 20 54  d table-locks. T
215c7 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 75  his is a no-op u
215c8 6e 6c 65 73 73 20 74 68 65 20 0a 20 20 20 20 20  nless the .     
215c9 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65   ** shared-cache
215ca 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62   feature is enab
215cb 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  led..      */.  
215cc 20 20 20 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63      codeTableLoc
215cd 6b 73 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ks(pParse);.    
215ce 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
215cf 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
215d0 30 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  0, pParse->cooki
215d1 65 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 20 20  eGoto);.    }.  
215d2 7d 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65  }...  /* Get the
215d3 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72 65   VDBE program re
215d4 61 64 79 20 66 6f 72 20 65 78 65 63 75 74 69 6f  ady for executio
215d5 6e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76 20 26  n.  */.  if( v &
215d6 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  & pParse->nErr==
215d7 30 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  0 && !db->malloc
215d8 46 61 69 6c 65 64 20 29 7b 0a 23 69 66 64 65 66  Failed ){.#ifdef
215d9 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
215da 20 20 46 49 4c 45 20 2a 74 72 61 63 65 20 3d 20    FILE *trace = 
215db 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
215dc 49 54 45 5f 56 64 62 65 54 72 61 63 65 29 21 3d  ITE_VdbeTrace)!=
215dd 30 20 3f 20 73 74 64 6f 75 74 20 3a 20 30 3b 0a  0 ? stdout : 0;.
215de 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 54      sqlite3VdbeT
215df 72 61 63 65 28 76 2c 20 74 72 61 63 65 29 3b 0a  race(v, trace);.
215e0 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72  #endif.    asser
215e1 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68  t( pParse->iCach
215e2 65 4c 65 76 65 6c 3d 3d 30 20 29 3b 20 20 2f 2a  eLevel==0 );  /*
215e3 20 44 69 73 61 62 6c 65 73 20 61 6e 64 20 72 65   Disables and re
215e4 2d 65 6e 61 62 6c 65 73 20 6d 61 74 63 68 20 2a  -enables match *
215e5 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
215e6 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 70 50  eMakeReady(v, pP
215e7 61 72 73 65 2d 3e 6e 56 61 72 2c 20 70 50 61 72  arse->nVar, pPar
215e8 73 65 2d 3e 6e 4d 65 6d 2c 0a 20 20 20 20 20 20  se->nMem,.      
215e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
215ea 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2c     pParse->nTab,
215eb 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
215ec 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  );.    pParse->r
215ed 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
215ee 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c  .    pParse->col
215ef 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20 20  NamesSet = 0;.  
215f0 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73 65  }else if( pParse
215f1 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
215f2 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  ){.    pParse->r
215f3 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
215f4 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e  ;.  }.  pParse->
215f5 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50 61 72  nTab = 0;.  pPar
215f6 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20 20  se->nMem = 0;.  
215f7 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20 30  pParse->nSet = 0
215f8 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  ;.  pParse->nVar
215f9 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e   = 0;.  pParse->
215fa 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b 0a  cookieMask = 0;.
215fb 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65    pParse->cookie
215fc 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  Goto = 0;.}../*.
215fd 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65  ** Run the parse
215fe 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  r and code gener
215ff 61 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c 79  ator recursively
21600 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e   in order to gen
21601 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f  erate.** code fo
21602 72 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  r the SQL statem
21603 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20 74  ent given onto t
21604 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 50  he end of the pP
21605 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a 20  arse context.** 
21606 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20  currently under 
21607 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 57  construction.  W
21608 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 69  hen the parser i
21609 73 20 72 75 6e 20 72 65 63 75 72 73 69 76 65 6c  s run recursivel
2160a 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20 74  y.** this way, t
2160b 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74  he final OP_Halt
2160c 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65 64   is not appended
2160d 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74 69   and other initi
2160e 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64  alization.** and
2160f 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73 74   finalization st
21610 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20  eps are omitted 
21611 62 65 63 61 75 73 65 20 74 68 6f 73 65 20 61 72  because those ar
21612 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 68  e handling by th
21613 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20 70  e.** outermost p
21614 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  arser..**.** Not
21615 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 6e   everything is n
21616 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20 66  estable.  This f
21617 61 63 69 6c 69 74 79 20 69 73 20 64 65 73 69 67  acility is desig
21618 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a 2a  ned to permit.**
21619 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
2161a 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 72   and DELETE oper
2161b 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 53  ations against S
2161c 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 55  QLITE_MASTER.  U
2161d 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 6f  se.** care if yo
2161e 75 20 64 65 63 69 64 65 20 74 6f 20 74 72 79 20  u decide to try 
2161f 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75 74  to use this rout
21620 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68  ine for some oth
21621 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a  er purposes..*/.
21622 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21623 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65  oid sqlite3Neste
21624 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 50  dParse(Parse *pP
21625 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
21626 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
21627 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
21628 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 63   char *zSql;.  c
21629 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30  har *zErrMsg = 0
2162a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2162b 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 20  = pParse->db;.# 
2162c 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20 20  define SAVE_SZ  
2162d 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20 2d  (sizeof(Parse) -
2162e 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65 2c   offsetof(Parse,
2162f 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73 61  nVar)).  char sa
21630 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b 0a  veBuf[SAVE_SZ];.
21631 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
21632 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  Err ) return;.  
21633 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
21634 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f 2a  nested<10 );  /*
21635 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64 20   Nesting should 
21636 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69 74  only be of limit
21637 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76 61  ed depth */.  va
21638 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
21639 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71  at);.  zSql = sq
2163a 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62  lite3VMPrintf(db
2163b 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
2163c 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
2163d 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20  if( zSql==0 ){. 
2163e 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a 20     return;   /* 
2163f 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61  A malloc must ha
21640 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d  ve failed */.  }
21641 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  .  pParse->neste
21642 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73 61  d++;.  memcpy(sa
21643 76 65 42 75 66 2c 20 26 70 50 61 72 73 65 2d 3e  veBuf, &pParse->
21644 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b 0a  nVar, SAVE_SZ);.
21645 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65    memset(&pParse
21646 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56 45 5f  ->nVar, 0, SAVE_
21647 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 75  SZ);.  sqlite3Ru
21648 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20  nParser(pParse, 
21649 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b  zSql, &zErrMsg);
2164a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2164b 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20  (db, zErrMsg);. 
2164c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2164d 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d 63  b, zSql);.  memc
2164e 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61 72  py(&pParse->nVar
2164f 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45 5f  , saveBuf, SAVE_
21650 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  SZ);.  pParse->n
21651 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a  ested--;.}../*.*
21652 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d  * Locate the in-
21653 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
21654 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
21655 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74  a particular dat
21656 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67  abase.** table g
21657 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  iven the name of
21658 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20   that table and 
21659 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65  (optionally) the
2165a 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20   name of the.** 
2165b 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
2165c 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20  ing the table.  
2165d 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e  Return NULL if n
2165e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20  ot found..**.** 
2165f 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20  If zDatabase is 
21660 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  0, all databases
21661 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f   are searched fo
21662 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  r the table and 
21663 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61 74  the.** first mat
21664 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20 72  ching table is r
21665 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68  eturned.  (No ch
21666 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c 69  ecking for dupli
21667 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e 61  cate table.** na
21668 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54  mes is done.)  T
21669 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20  he search order 
2166a 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74  is TEMP first, t
2166b 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61  hen MAIN, then a
2166c 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20  ny.** auxiliary 
2166d 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 20  databases added 
2166e 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48  using the ATTACH
2166f 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
21670 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33  See also sqlite3
21671 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a 2a  LocateTable()..*
21672 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21673 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46   Table *sqlite3F
21674 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33  indTable(sqlite3
21675 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
21676 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63   *zName, const c
21677 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 29 7b  har *zDatabase){
21678 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 30 3b  .  Table *p = 0;
21679 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2167a 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28  nName;.  assert(
2167b 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 6e   zName!=0 );.  n
2167c 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
2167d 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
2167e 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50   for(i=OMIT_TEMP
2167f 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  DB; i<db->nDb; i
21680 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d  ++){.    int j =
21681 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69   (i<2) ? i^1 : i
21682 3b 20 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45  ;   /* Search TE
21683 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a  MP before MAIN *
21684 2f 0a 20 20 20 20 69 66 28 20 7a 44 61 74 61 62  /.    if( zDatab
21685 61 73 65 21 3d 30 20 26 26 20 73 71 6c 69 74 65  ase!=0 && sqlite
21686 33 53 74 72 49 43 6d 70 28 7a 44 61 74 61 62 61  3StrICmp(zDataba
21687 73 65 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a  se, db->aDb[j].z
21688 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65  Name) ) continue
21689 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ;.    p = sqlite
2168a 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61  3HashFind(&db->a
2168b 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  Db[j].pSchema->t
2168c 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  blHash, zName, n
2168d 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Name);.    if( p
2168e 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20   ) break;.  }.  
2168f 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
21690 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e  ** Locate the in
21691 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72  -memory structur
21692 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
21693 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
21694 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20  tabase.** table 
21695 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  given the name o
21696 66 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64  f that table and
21697 20 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68   (optionally) th
21698 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a  e name of the.**
21699 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
2169a 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20  ning the table. 
2169b 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
2169c 6e 6f 74 20 66 6f 75 6e 64 2e 20 20 41 6c 73 6f  not found.  Also
2169d 20 6c 65 61 76 65 20 61 6e 0a 2a 2a 20 65 72 72   leave an.** err
2169e 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
2169f 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a  arse->zErrMsg..*
216a0 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65  *.** The differe
216a1 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73  nce between this
216a2 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 73 71 6c   routine and sql
216a3 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29 20  ite3FindTable() 
216a4 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20  is that this.** 
216a5 72 6f 75 74 69 6e 65 20 6c 65 61 76 65 73 20 61  routine leaves a
216a6 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
216a7 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  in pParse->zErrM
216a8 73 67 20 77 68 65 72 65 0a 2a 2a 20 73 71 6c 69  sg where.** sqli
216a9 74 65 33 46 69 6e 64 54 61 62 6c 65 28 29 20 64  te3FindTable() d
216aa 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49  oes not..*/.SQLI
216ab 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65  TE_PRIVATE Table
216ac 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54   *sqlite3LocateT
216ad 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
216ae 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
216af 2a 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69  * context in whi
216b0 63 68 20 74 6f 20 72 65 70 6f 72 74 20 65 72 72  ch to report err
216b1 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56  ors */.  int isV
216b2 69 65 77 2c 20 20 20 20 20 20 20 20 20 20 20 20  iew,            
216b3 2f 2a 20 54 72 75 65 20 69 66 20 6c 6f 6f 6b 69  /* True if looki
216b4 6e 67 20 66 6f 72 20 61 20 56 49 45 57 20 72 61  ng for a VIEW ra
216b5 74 68 65 72 20 74 68 61 6e 20 61 20 54 41 42 4c  ther than a TABL
216b6 45 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  E */.  const cha
216b7 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 2f 2a  r *zName,     /*
216b8 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   Name of the tab
216b9 6c 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e  le we are lookin
216ba 67 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  g for */.  const
216bb 20 63 68 61 72 20 2a 7a 44 62 61 73 65 20 20 20   char *zDbase   
216bc 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
216bd 20 64 61 74 61 62 61 73 65 2e 20 20 4d 69 67 68   database.  Migh
216be 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a  t be NULL */.){.
216bf 20 20 54 61 62 6c 65 20 2a 70 3b 0a 0a 20 20 2f    Table *p;..  /
216c0 2a 20 52 65 61 64 20 74 68 65 20 64 61 74 61 62  * Read the datab
216c1 61 73 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61  ase schema. If a
216c2 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
216c3 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
216c4 65 73 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20  essage.  ** and 
216c5 63 6f 64 65 20 69 6e 20 70 50 61 72 73 65 20 61  code in pParse a
216c6 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  nd return NULL. 
216c7 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  */.  if( SQLITE_
216c8 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53  OK!=sqlite3ReadS
216c9 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
216ca 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
216cb 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69 74 65   }..  p = sqlite
216cc 33 46 69 6e 64 54 61 62 6c 65 28 70 50 61 72 73  3FindTable(pPars
216cd 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44  e->db, zName, zD
216ce 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d  base);.  if( p==
216cf 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  0 ){.    const c
216d0 68 61 72 20 2a 7a 4d 73 67 20 3d 20 69 73 56 69  har *zMsg = isVi
216d1 65 77 20 3f 20 22 6e 6f 20 73 75 63 68 20 76 69  ew ? "no such vi
216d2 65 77 22 20 3a 20 22 6e 6f 20 73 75 63 68 20 74  ew" : "no such t
216d3 61 62 6c 65 22 3b 0a 20 20 20 20 69 66 28 20 7a  able";.    if( z
216d4 44 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 73  Dbase ){.      s
216d5 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
216d6 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25  Parse, "%s: %s.%
216d7 73 22 2c 20 7a 4d 73 67 2c 20 7a 44 62 61 73 65  s", zMsg, zDbase
216d8 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65  , zName);.    }e
216d9 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
216da 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
216db 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 4d 73  e, "%s: %s", zMs
216dc 67 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  g, zName);.    }
216dd 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65  .    pParse->che
216de 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20  ckSchema = 1;.  
216df 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
216e0 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68  ./*.** Locate th
216e1 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75  e in-memory stru
216e2 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72  cture that descr
216e3 69 62 65 73 20 0a 2a 2a 20 61 20 70 61 72 74 69  ibes .** a parti
216e4 63 75 6c 61 72 20 69 6e 64 65 78 20 67 69 76 65  cular index give
216e5 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
216e6 61 74 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20  at index.** and 
216e7 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
216e8 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63 6f  database that co
216e9 6e 74 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78  ntains the index
216ea 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c  ..** Return NULL
216eb 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a   if not found..*
216ec 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73  *.** If zDatabas
216ed 65 20 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61  e is 0, all data
216ee 62 61 73 65 73 20 61 72 65 20 73 65 61 72 63 68  bases are search
216ef 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74 61  ed for the.** ta
216f0 62 6c 65 20 61 6e 64 20 74 68 65 20 66 69 72 73  ble and the firs
216f1 74 20 6d 61 74 63 68 69 6e 67 20 69 6e 64 65 78  t matching index
216f2 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28   is returned.  (
216f3 4e 6f 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 66  No checking.** f
216f4 6f 72 20 64 75 70 6c 69 63 61 74 65 20 69 6e 64  or duplicate ind
216f5 65 78 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65  ex names is done
216f6 2e 29 20 20 54 68 65 20 73 65 61 72 63 68 20 6f  .)  The search o
216f7 72 64 65 72 20 69 73 0a 2a 2a 20 54 45 4d 50 20  rder is.** TEMP 
216f8 66 69 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e  first, then MAIN
216f9 2c 20 74 68 65 6e 20 61 6e 79 20 61 75 78 69 6c  , then any auxil
216fa 69 61 72 79 20 64 61 74 61 62 61 73 65 73 20 61  iary databases a
216fb 64 64 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68  dded.** using th
216fc 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64  e ATTACH command
216fd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
216fe 41 54 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74  ATE Index *sqlit
216ff 65 33 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69  e3FindIndex(sqli
21700 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
21701 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73  har *zName, cons
21702 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20  t char *zDb){.  
21703 49 6e 64 65 78 20 2a 70 20 3d 20 30 3b 0a 20 20  Index *p = 0;.  
21704 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4e 61  int i;.  int nNa
21705 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
21706 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 66  en30(zName);.  f
21707 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42  or(i=OMIT_TEMPDB
21708 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
21709 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28  ){.    int j = (
2170a 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20  i<2) ? i^1 : i; 
2170b 20 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20   /* Search TEMP 
2170c 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20  before MAIN */. 
2170d 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65     Schema *pSche
2170e 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e  ma = db->aDb[j].
2170f 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 69 66 28  pSchema;.    if(
21710 20 7a 44 62 20 26 26 20 73 71 6c 69 74 65 33 53   zDb && sqlite3S
21711 74 72 49 43 6d 70 28 7a 44 62 2c 20 64 62 2d 3e  trICmp(zDb, db->
21712 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20  aDb[j].zName) ) 
21713 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73  continue;.    as
21714 73 65 72 74 28 20 70 53 63 68 65 6d 61 20 7c 7c  sert( pSchema ||
21715 20 28 6a 3d 3d 31 20 26 26 20 21 64 62 2d 3e 61   (j==1 && !db->a
21716 44 62 5b 31 5d 2e 70 42 74 29 20 29 3b 0a 20 20  Db[1].pBt) );.  
21717 20 20 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b    if( pSchema ){
21718 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74  .      p = sqlit
21719 65 33 48 61 73 68 46 69 6e 64 28 26 70 53 63 68  e3HashFind(&pSch
2171a 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e  ema->idxHash, zN
2171b 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
2171c 20 7d 0a 20 20 20 20 69 66 28 20 70 20 29 20 62   }.    if( p ) b
2171d 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  reak;.  }.  retu
2171e 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
2171f 65 63 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72  eclaim the memor
21720 79 20 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64  y used by an ind
21721 65 78 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ex.*/.static voi
21722 64 20 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65  d freeIndex(Inde
21723 78 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  x *p){.  sqlite3
21724 20 2a 64 62 20 3d 20 70 2d 3e 70 54 61 62 6c 65   *db = p->pTable
21725 2d 3e 64 62 4d 65 6d 3b 0a 20 20 73 71 6c 69 74  ->dbMem;.  sqlit
21726 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
21727 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69  zColAff);.  sqli
21728 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
21729 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76  ;.}../*.** Remov
2172a 65 20 74 68 65 20 67 69 76 65 6e 20 69 6e 64 65  e the given inde
2172b 78 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78  x from the index
2172c 20 68 61 73 68 20 74 61 62 6c 65 2c 20 61 6e 64   hash table, and
2172d 20 66 72 65 65 0a 2a 2a 20 69 74 73 20 6d 65 6d   free.** its mem
2172e 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 2e 0a  ory structures..
2172f 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20  **.** The index 
21730 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20  is removed from 
21731 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  the database has
21732 68 20 74 61 62 6c 65 73 20 62 75 74 0a 2a 2a 20  h tables but.** 
21733 69 74 20 69 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b  it is not unlink
21734 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c  ed from the Tabl
21735 65 20 74 68 61 74 20 69 74 20 69 6e 64 65 78 65  e that it indexe
21736 73 2e 0a 2a 2a 20 55 6e 6c 69 6e 6b 69 6e 67 20  s..** Unlinking 
21737 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 6d  from the Table m
21738 75 73 74 20 62 65 20 64 6f 6e 65 20 62 79 20 74  ust be done by t
21739 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
2173a 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
2173b 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
2173c 65 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29  eIndex(Index *p)
2173d 7b 0a 20 20 49 6e 64 65 78 20 2a 70 4f 6c 64 3b  {.  Index *pOld;
2173e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2173f 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b  Name = p->zName;
21740 0a 0a 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74  ..  pOld = sqlit
21741 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d  e3HashInsert(&p-
21742 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  >pSchema->idxHas
21743 68 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  h, zName,.      
21744 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21745 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c       sqlite3Strl
21746 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b  en30(zName), 0);
21747 0a 20 20 61 73 73 65 72 74 28 20 70 4f 6c 64 3d  .  assert( pOld=
21748 3d 30 20 7c 7c 20 70 4f 6c 64 3d 3d 70 20 29 3b  =0 || pOld==p );
21749 0a 20 20 66 72 65 65 49 6e 64 65 78 28 70 29 3b  .  freeIndex(p);
2174a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68  .}../*.** For th
2174b 65 20 69 6e 64 65 78 20 63 61 6c 6c 65 64 20 7a  e index called z
2174c 49 64 78 4e 61 6d 65 20 77 68 69 63 68 20 69 73  IdxName which is
2174d 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61   found in the da
2174e 74 61 62 61 73 65 20 69 44 62 2c 0a 2a 2a 20 75  tabase iDb,.** u
2174f 6e 6c 69 6b 65 20 74 68 61 74 20 69 6e 64 65 78  nlike that index
21750 20 66 72 6f 6d 20 69 74 73 20 54 61 62 6c 65 20   from its Table 
21751 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 20  then remove the 
21752 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 68  index from.** th
21753 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62  e index hash tab
21754 6c 65 20 61 6e 64 20 66 72 65 65 20 61 6c 6c 20  le and free all 
21755 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
21756 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  s associated.** 
21757 77 69 74 68 20 74 68 65 20 69 6e 64 65 78 2e 0a  with the index..
21758 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21759 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e  E void sqlite3Un
2175a 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64  linkAndDeleteInd
2175b 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ex(sqlite3 *db, 
2175c 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63  int iDb, const c
2175d 68 61 72 20 2a 7a 49 64 78 4e 61 6d 65 29 7b 0a  har *zIdxName){.
2175e 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
2175f 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 48 61  .  int len;.  Ha
21760 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64 62 2d  sh *pHash = &db-
21761 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
21762 61 2d 3e 69 64 78 48 61 73 68 3b 0a 0a 20 20 6c  a->idxHash;..  l
21763 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  en = sqlite3Strl
21764 65 6e 33 30 28 7a 49 64 78 4e 61 6d 65 29 3b 0a  en30(zIdxName);.
21765 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74    pIndex = sqlit
21766 65 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  e3HashInsert(pHa
21767 73 68 2c 20 7a 49 64 78 4e 61 6d 65 2c 20 6c 65  sh, zIdxName, le
21768 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 49 6e  n, 0);.  if( pIn
21769 64 65 78 20 29 7b 0a 20 20 20 20 69 66 28 20 70  dex ){.    if( p
2176a 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70  Index->pTable->p
2176b 49 6e 64 65 78 3d 3d 70 49 6e 64 65 78 20 29 7b  Index==pIndex ){
2176c 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70  .      pIndex->p
2176d 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 20 3d 20  Table->pIndex = 
2176e 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20  pIndex->pNext;. 
2176f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21770 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20 20 20  Index *p;.      
21771 66 6f 72 28 70 3d 70 49 6e 64 65 78 2d 3e 70 54  for(p=pIndex->pT
21772 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 20  able->pIndex; p 
21773 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 49 6e  && p->pNext!=pIn
21774 64 65 78 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  dex; p=p->pNext)
21775 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 70 20 26  {}.      if( p &
21776 26 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64  & p->pNext==pInd
21777 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  ex ){.        p-
21778 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 2d  >pNext = pIndex-
21779 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  >pNext;.      }.
2177a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65 49 6e      }.    freeIn
2177b 64 65 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d  dex(pIndex);.  }
2177c 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20  .  db->flags |= 
2177d 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
2177e 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  nges;.}../*.** E
2177f 72 61 73 65 20 61 6c 6c 20 73 63 68 65 6d 61 20  rase all schema 
21780 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
21781 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68   the in-memory h
21782 61 73 68 20 74 61 62 6c 65 73 20 6f 66 0a 2a 2a  ash tables of.**
21783 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
21784 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  se.  This routin
21785 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72  e is called to r
21786 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 0a 2a 2a  eclaim memory.**
21787 20 62 65 66 6f 72 65 20 74 68 65 20 64 61 74 61   before the data
21788 62 61 73 65 20 63 6c 6f 73 65 73 2e 20 20 49 74  base closes.  It
21789 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20   is also called 
2178a 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63  during a rollbac
2178b 6b 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 77 65  k.** if there we
2178c 72 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  re schema change
2178d 73 20 64 75 72 69 6e 67 20 74 68 65 20 74 72 61  s during the tra
2178e 6e 73 61 63 74 69 6f 6e 20 6f 72 20 69 66 20 61  nsaction or if a
2178f 0a 2a 2a 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69  .** schema-cooki
21790 65 20 6d 69 73 6d 61 74 63 68 20 6f 63 63 75 72  e mismatch occur
21791 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3d  s..**.** If iDb=
21792 3d 30 20 74 68 65 6e 20 72 65 73 65 74 20 74 68  =0 then reset th
21793 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  e internal schem
21794 61 20 74 61 62 6c 65 73 20 66 6f 72 20 61 6c 6c  a tables for all
21795 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
21796 65 73 2e 20 20 49 66 20 69 44 62 3e 3d 31 20 74  es.  If iDb>=1 t
21797 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69 6e  hen reset the in
21798 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 66 6f  ternal schema fo
21799 72 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 73 69  r only the.** si
2179a 6e 67 6c 65 20 66 69 6c 65 20 69 6e 64 69 63 61  ngle file indica
2179b 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
2179c 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2179d 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
2179e 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 20 2a  Schema(sqlite3 *
2179f 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  db, int iDb){.  
217a0 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 73 73 65  int i, j;.  asse
217a1 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
217a2 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20  b<db->nDb );..  
217a3 69 66 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20  if( iDb==0 ){.  
217a4 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
217a5 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 7d 0a  terAll(db);.  }.
217a6 20 20 66 6f 72 28 69 3d 69 44 62 3b 20 69 3c 64    for(i=iDb; i<d
217a7 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
217a8 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d    Db *pDb = &db-
217a9 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28  >aDb[i];.    if(
217aa 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b   pDb->pSchema ){
217ab 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 69 3d  .      assert(i=
217ac 3d 31 20 7c 7c 20 28 70 44 62 2d 3e 70 42 74 20  =1 || (pDb->pBt 
217ad 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  && sqlite3BtreeH
217ae 6f 6c 64 73 4d 75 74 65 78 28 70 44 62 2d 3e 70  oldsMutex(pDb->p
217af 42 74 29 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  Bt)));.      sql
217b0 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 70  ite3SchemaFree(p
217b1 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  Db->pSchema);.  
217b2 20 20 7d 0a 20 20 20 20 69 66 28 20 69 44 62 3e    }.    if( iDb>
217b3 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a  0 ) return;.  }.
217b4 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 30    assert( iDb==0
217b5 20 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20   );.  db->flags 
217b6 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72  &= ~SQLITE_Inter
217b7 6e 43 68 61 6e 67 65 73 3b 0a 20 20 73 71 6c 69  nChanges;.  sqli
217b8 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
217b9 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f  (db);..  /* If o
217ba 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
217bb 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  e auxiliary data
217bc 62 61 73 65 20 66 69 6c 65 73 20 68 61 73 20 62  base files has b
217bd 65 65 6e 20 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a  een closed,.  **
217be 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65   then remove the
217bf 6d 20 66 72 6f 6d 20 74 68 65 20 61 75 78 69 6c  m from the auxil
217c0 69 61 72 79 20 64 61 74 61 62 61 73 65 20 6c 69  iary database li
217c1 73 74 2e 20 20 57 65 20 74 61 6b 65 20 74 68 65  st.  We take the
217c2 0a 20 20 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74  .  ** opportunit
217c3 79 20 74 6f 20 64 6f 20 74 68 69 73 20 68 65 72  y to do this her
217c4 65 20 73 69 6e 63 65 20 77 65 20 68 61 76 65 20  e since we have 
217c5 6a 75 73 74 20 64 65 6c 65 74 65 64 20 61 6c 6c  just deleted all
217c6 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 63 68   of the.  ** sch
217c7 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73 20  ema hash tables 
217c8 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 64 6f  and therefore do
217c9 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6d 61 6b   not have to mak
217ca 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 20 20  e any changes.  
217cb 2a 2a 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 6f  ** to any of tho
217cc 73 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a  se tables..  */.
217cd 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
217ce 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
217cf 73 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d  struct Db *pDb =
217d0 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20   &db->aDb[i];.  
217d1 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d    if( pDb->pBt==
217d2 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
217d3 44 62 2d 3e 70 41 75 78 20 26 26 20 70 44 62 2d  Db->pAux && pDb-
217d4 3e 78 46 72 65 65 41 75 78 20 29 20 70 44 62 2d  >xFreeAux ) pDb-
217d5 3e 78 46 72 65 65 41 75 78 28 70 44 62 2d 3e 70  >xFreeAux(pDb->p
217d6 41 75 78 29 3b 0a 20 20 20 20 20 20 70 44 62 2d  Aux);.      pDb-
217d7 3e 70 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d  >pAux = 0;.    }
217d8 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 32  .  }.  for(i=j=2
217d9 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
217da 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62  ){.    struct Db
217db 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
217dc 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62  [i];.    if( pDb
217dd 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ->pBt==0 ){.    
217de 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
217df 64 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 3b  db, pDb->zName);
217e0 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d  .      pDb->zNam
217e1 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 6f 6e  e = 0;.      con
217e2 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20  tinue;.    }.   
217e3 20 69 66 28 20 6a 3c 69 20 29 7b 0a 20 20 20 20   if( j<i ){.    
217e4 20 20 64 62 2d 3e 61 44 62 5b 6a 5d 20 3d 20 64    db->aDb[j] = d
217e5 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 7d  b->aDb[i];.    }
217e6 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20  .    j++;.  }.  
217e7 6d 65 6d 73 65 74 28 26 64 62 2d 3e 61 44 62 5b  memset(&db->aDb[
217e8 6a 5d 2c 20 30 2c 20 28 64 62 2d 3e 6e 44 62 2d  j], 0, (db->nDb-
217e9 6a 29 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44  j)*sizeof(db->aD
217ea 62 5b 6a 5d 29 29 3b 0a 20 20 64 62 2d 3e 6e 44  b[j]));.  db->nD
217eb 62 20 3d 20 6a 3b 0a 20 20 69 66 28 20 64 62 2d  b = j;.  if( db-
217ec 3e 6e 44 62 3c 3d 32 20 26 26 20 64 62 2d 3e 61  >nDb<=2 && db->a
217ed 44 62 21 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db!=db->aDbStati
217ee 63 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  c ){.    memcpy(
217ef 64 62 2d 3e 61 44 62 53 74 61 74 69 63 2c 20 64  db->aDbStatic, d
217f0 62 2d 3e 61 44 62 2c 20 32 2a 73 69 7a 65 6f 66  b->aDb, 2*sizeof
217f1 28 64 62 2d 3e 61 44 62 5b 30 5d 29 29 3b 0a 20  (db->aDb[0]));. 
217f2 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
217f3 28 64 62 2c 20 64 62 2d 3e 61 44 62 29 3b 0a 20  (db, db->aDb);. 
217f4 20 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d     db->aDb = db-
217f5 3e 61 44 62 53 74 61 74 69 63 3b 0a 20 20 7d 0a  >aDbStatic;.  }.
217f6 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
217f7 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
217f8 77 68 65 6e 20 61 20 63 6f 6d 6d 69 74 20 6f 63  when a commit oc
217f9 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  curs..*/.SQLITE_
217fa 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
217fb 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e  ite3CommitIntern
217fc 61 6c 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65  alChanges(sqlite
217fd 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 66 6c  3 *db){.  db->fl
217fe 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49  ags &= ~SQLITE_I
217ff 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a  nternChanges;.}.
21800 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
21801 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72   column names fr
21802 6f 6d 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69  om a table or vi
21803 65 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ew..*/.static vo
21804 69 64 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f  id sqliteResetCo
21805 6c 75 6d 6e 4e 61 6d 65 73 28 54 61 62 6c 65 20  lumnNames(Table 
21806 2a 70 54 61 62 6c 65 29 7b 0a 20 20 69 6e 74 20  *pTable){.  int 
21807 69 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  i;.  Column *pCo
21808 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  l;.  sqlite3 *db
21809 20 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d   = pTable->dbMem
2180a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
2180b 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  le!=0 );.  if( (
2180c 70 43 6f 6c 20 3d 20 70 54 61 62 6c 65 2d 3e 61  pCol = pTable->a
2180d 43 6f 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 66  Col)!=0 ){.    f
2180e 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 6c 65  or(i=0; i<pTable
2180f 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  ->nCol; i++, pCo
21810 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  l++){.      sqli
21811 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
21812 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ol->zName);.    
21813 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
21814 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44  ete(db, pCol->pD
21815 66 6c 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  flt);.      sqli
21816 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
21817 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 20 20 20  ol->zType);.    
21818 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
21819 64 62 2c 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29  db, pCol->zColl)
2181a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2181b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
2181c 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a 20 20 7d  able->aCol);.  }
2181d 0a 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20  .  pTable->aCol 
2181e 3d 20 30 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e  = 0;.  pTable->n
2181f 43 6f 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Col = 0;.}../*.*
21820 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 6d 65 6d  * Remove the mem
21821 6f 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75  ory data structu
21822 72 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  res associated w
21823 69 74 68 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a  ith the given.**
21824 20 54 61 62 6c 65 2e 20 20 4e 6f 20 63 68 61 6e   Table.  No chan
21825 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20  ges are made to 
21826 64 69 73 6b 20 62 79 20 74 68 69 73 20 72 6f 75  disk by this rou
21827 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tine..**.** This
21828 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 64 65   routine just de
21829 6c 65 74 65 73 20 74 68 65 20 64 61 74 61 20 73  letes the data s
2182a 74 72 75 63 74 75 72 65 2e 20 20 49 74 20 64 6f  tructure.  It do
2182b 65 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 0a 2a 2a  es not unlink.**
2182c 20 74 68 65 20 74 61 62 6c 65 20 64 61 74 61 20   the table data 
2182d 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74  structure from t
2182e 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20  he hash table.  
2182f 42 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74  But it does dest
21830 72 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74  roy.** memory st
21831 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20  ructures of the 
21832 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65  indices and fore
21833 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61  ign keys associa
21834 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65  ted with .** the
21835 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
21836 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21837 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
21838 65 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29  e(Table *pTable)
21839 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  {.  Index *pInde
2183a 78 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 46 4b 65  x, *pNext;.  FKe
2183b 79 20 2a 70 46 4b 65 79 2c 20 2a 70 4e 65 78 74  y *pFKey, *pNext
2183c 46 4b 65 79 3b 0a 20 20 73 71 6c 69 74 65 33 20  FKey;.  sqlite3 
2183d 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 54 61 62  *db;..  if( pTab
2183e 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  le==0 ) return;.
2183f 20 20 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e 64    db = pTable->d
21840 62 4d 65 6d 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e  bMem;..  /* Do n
21841 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 74 61  ot delete the ta
21842 62 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 72 65  ble until the re
21843 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65  ference count re
21844 61 63 68 65 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20  aches zero. */. 
21845 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b   pTable->nRef--;
21846 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e  .  if( pTable->n
21847 52 65 66 3e 30 20 29 7b 0a 20 20 20 20 72 65 74  Ref>0 ){.    ret
21848 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
21849 74 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3d  t( pTable->nRef=
2184a 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65  =0 );..  /* Dele
2184b 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61  te all indices a
2184c 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2184d 68 69 73 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20  his table.  */. 
2184e 20 66 6f 72 28 70 49 6e 64 65 78 20 3d 20 70 54   for(pIndex = pT
2184f 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49  able->pIndex; pI
21850 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 4e 65  ndex; pIndex=pNe
21851 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d  xt){.    pNext =
21852 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a   pIndex->pNext;.
21853 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64      assert( pInd
21854 65 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  ex->pSchema==pTa
21855 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  ble->pSchema );.
21856 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
21857 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 3b 0a  eIndex(pIndex);.
21858 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
21859 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
2185a 5f 4b 45 59 0a 20 20 2f 2a 20 44 65 6c 65 74 65  _KEY.  /* Delete
2185b 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79   all foreign key
2185c 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2185d 68 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f  h this table. */
2185e 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61  .  for(pFKey=pTa
2185f 62 6c 65 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65  ble->pFKey; pFKe
21860 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74 46 4b  y; pFKey=pNextFK
21861 65 79 29 7b 0a 20 20 20 20 70 4e 65 78 74 46 4b  ey){.    pNextFK
21862 65 79 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65 78  ey = pFKey->pNex
21863 74 46 72 6f 6d 3b 0a 20 20 20 20 73 71 6c 69 74  tFrom;.    sqlit
21864 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b  e3DbFree(db, pFK
21865 65 79 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ey);.  }.#endif.
21866 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65  .  /* Delete the
21867 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
21868 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20   itself..  */.  
21869 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d  sqliteResetColum
2186a 6e 4e 61 6d 65 73 28 70 54 61 62 6c 65 29 3b 0a  nNames(pTable);.
2186b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2186c 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d  db, pTable->zNam
2186d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
2186e 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ree(db, pTable->
2186f 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71 6c 69  zColAff);.  sqli
21870 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
21871 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c  db, pTable->pSel
21872 65 63 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ect);.#ifndef SQ
21873 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
21874 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
21875 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ete(db, pTable->
21876 70 43 68 65 63 6b 29 3b 0a 23 65 6e 64 69 66 0a  pCheck);.#endif.
21877 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65    sqlite3VtabCle
21878 61 72 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71  ar(pTable);.  sq
21879 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2187a 70 54 61 62 6c 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pTable);.}../*.*
2187b 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 67 69 76  * Unlink the giv
2187c 65 6e 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68  en table from th
2187d 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e  e hash tables an
2187e 64 20 74 68 65 20 64 65 6c 65 74 65 20 74 68 65  d the delete the
2187f 0a 2a 2a 20 74 61 62 6c 65 20 73 74 72 75 63 74  .** table struct
21880 75 72 65 20 77 69 74 68 20 61 6c 6c 20 69 74 73  ure with all its
21881 20 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72   indices and for
21882 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2f 0a 53 51  eign keys..*/.SQ
21883 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
21884 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
21885 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28 73 71  ndDeleteTable(sq
21886 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
21887 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  Db, const char *
21888 7a 54 61 62 4e 61 6d 65 29 7b 0a 20 20 54 61 62  zTabName){.  Tab
21889 6c 65 20 2a 70 3b 0a 20 20 44 62 20 2a 70 44 62  le *p;.  Db *pDb
2188a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21  ;..  assert( db!
2188b 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2188c 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
2188d 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
2188e 74 28 20 7a 54 61 62 4e 61 6d 65 20 26 26 20 7a  t( zTabName && z
2188f 54 61 62 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20 20  TabName[0] );.  
21890 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
21891 44 62 5d 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74  Db];.  p = sqlit
21892 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 44  e3HashInsert(&pD
21893 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  b->pSchema->tblH
21894 61 73 68 2c 20 7a 54 61 62 4e 61 6d 65 2c 0a 20  ash, zTabName,. 
21895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21896 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
21897 72 6c 65 6e 33 30 28 7a 54 61 62 4e 61 6d 65 29  rlen30(zTabName)
21898 2c 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  ,0);.  sqlite3De
21899 6c 65 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20  leteTable(p);.  
2189a 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
2189b 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
2189c 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  s;.}../*.** Give
2189d 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74 75 72  n a token, retur
2189e 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  n a string that 
2189f 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
218a0 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a 2a 20  text of that.** 
218a1 74 6f 6b 65 6e 2e 20 20 53 70 61 63 65 20 74 6f  token.  Space to
218a2 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
218a3 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20  ed string.** is 
218a4 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
218a5 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64  liteMalloc() and
218a6 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62   must be freed b
218a7 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a  y the calling.**
218a8 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
218a9 20 41 6e 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d   Any quotation m
218aa 61 72 6b 73 20 28 65 78 3a 20 20 22 6e 61 6d 65  arks (ex:  "name
218ab 22 2c 20 27 6e 61 6d 65 27 2c 20 5b 6e 61 6d 65  ", 'name', [name
218ac 5d 2c 20 6f 72 20 60 6e 61 6d 65 60 29 20 74 68  ], or `name`) th
218ad 61 74 0a 2a 2a 20 73 75 72 72 6f 75 6e 64 20 74  at.** surround t
218ae 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 74  he body of the t
218af 6f 6b 65 6e 20 61 72 65 20 72 65 6d 6f 76 65 64  oken are removed
218b0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b 65 6e 73 20 61  ..**.** Tokens a
218b1 72 65 20 6f 66 74 65 6e 20 6a 75 73 74 20 70 6f  re often just po
218b2 69 6e 74 65 72 73 20 69 6e 74 6f 20 74 68 65 20  inters into the 
218b3 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78  original SQL tex
218b4 74 20 61 6e 64 20 73 6f 0a 2a 2a 20 61 72 65 20  t and so.** are 
218b5 6e 6f 74 20 5c 30 30 30 20 74 65 72 6d 69 6e 61  not \000 termina
218b6 74 65 64 20 61 6e 64 20 61 72 65 20 6e 6f 74 20  ted and are not 
218b7 70 65 72 73 69 73 74 65 6e 74 2e 20 20 54 68 65  persistent.  The
218b8 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67   returned string
218b9 0a 2a 2a 20 69 73 20 5c 30 30 30 20 74 65 72 6d  .** is \000 term
218ba 69 6e 61 74 65 64 20 61 6e 64 20 69 73 20 70 65  inated and is pe
218bb 72 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  rsistent..*/.SQL
218bc 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
218bd 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f   *sqlite3NameFro
218be 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 20 2a  mToken(sqlite3 *
218bf 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  db, Token *pName
218c0 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  ){.  char *zName
218c1 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b  ;.  if( pName ){
218c2 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  .    zName = sql
218c3 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
218c4 2c 20 28 63 68 61 72 2a 29 70 4e 61 6d 65 2d 3e  , (char*)pName->
218c5 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20  z, pName->n);.  
218c6 20 20 69 66 28 20 70 4e 61 6d 65 2d 3e 71 75 6f    if( pName->quo
218c7 74 65 64 20 29 20 73 71 6c 69 74 65 33 44 65 71  ted ) sqlite3Deq
218c8 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 7d  uote(zName);.  }
218c9 65 6c 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65 20  else{.    zName 
218ca 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
218cb 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n zName;.}../*.*
218cc 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74  * Open the sqlit
218cd 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 73  e_master table s
218ce 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73  tored in databas
218cf 65 20 6e 75 6d 62 65 72 20 69 44 62 20 66 6f 72  e number iDb for
218d0 0a 2a 2a 20 77 72 69 74 69 6e 67 2e 20 54 68 65  .** writing. The
218d1 20 74 61 62 6c 65 20 69 73 20 6f 70 65 6e 65 64   table is opened
218d2 20 75 73 69 6e 67 20 63 75 72 73 6f 72 20 30 2e   using cursor 0.
218d3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
218d4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f  TE void sqlite3O
218d5 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50  penMasterTable(P
218d6 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69 44 62  arse *p, int iDb
218d7 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  ){.  Vdbe *v = s
218d8 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29  qlite3GetVdbe(p)
218d9 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ;.  sqlite3Table
218da 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41 53  Lock(p, iDb, MAS
218db 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 53 43 48  TER_ROOT, 1, SCH
218dc 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 29 3b  EMA_TABLE(iDb));
218dd 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
218de 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp3(v, OP_OpenW
218df 72 69 74 65 2c 20 30 2c 20 4d 41 53 54 45 52 5f  rite, 0, MASTER_
218e0 52 4f 4f 54 2c 20 69 44 62 29 3b 0a 20 20 73 71  ROOT, iDb);.  sq
218e1 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
218e2 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a  4(v, -1, (char *
218e3 29 35 2c 20 50 34 5f 49 4e 54 33 32 29 3b 20 20  )5, P4_INT32);  
218e4 2f 2a 20 35 20 63 6f 6c 75 6d 6e 20 74 61 62 6c  /* 5 column tabl
218e5 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 54  e */.  if( p->nT
218e6 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  ab==0 ){.    p->
218e7 6e 54 61 62 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a  nTab = 1;.  }.}.
218e8 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  ./*.** Parameter
218e9 20 7a 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f   zName points to
218ea 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65   a nul-terminate
218eb 64 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e  d buffer contain
218ec 69 6e 67 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20  ing the name.** 
218ed 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 28 22  of a database ("
218ee 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f 72  main", "temp" or
218ef 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20   the name of an 
218f0 61 74 74 61 63 68 65 64 20 64 62 29 2e 20 54 68  attached db). Th
218f1 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72  is.** function r
218f2 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78  eturns the index
218f3 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61   of the named da
218f4 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44  tabase in db->aD
218f5 62 5b 5d 2c 20 6f 72 0a 2a 2a 20 2d 31 20 69 66  b[], or.** -1 if
218f6 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20 63 61   the named db ca
218f7 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a  nnot be found..*
218f8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
218f9 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64   int sqlite3Find
218fa 44 62 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a  DbName(sqlite3 *
218fb 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
218fc 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20  zName){.  int i 
218fd 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a  = -1;         /*
218fe 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72   Database number
218ff 20 2a 2f 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20   */.  if( zName 
21900 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 3b 0a  ){.    Db *pDb;.
21901 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69      int n = sqli
21902 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
21903 65 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 28 64  e);.    for(i=(d
21904 62 2d 3e 6e 44 62 2d 31 29 2c 20 70 44 62 3d 26  b->nDb-1), pDb=&
21905 64 62 2d 3e 61 44 62 5b 69 5d 3b 20 69 3e 3d 30  db->aDb[i]; i>=0
21906 3b 20 69 2d 2d 2c 20 70 44 62 2d 2d 29 7b 0a 20  ; i--, pDb--){. 
21907 20 20 20 20 20 69 66 28 20 28 21 4f 4d 49 54 5f       if( (!OMIT_
21908 54 45 4d 50 44 42 20 7c 7c 20 69 21 3d 31 20 29  TEMPDB || i!=1 )
21909 20 26 26 20 6e 3d 3d 73 71 6c 69 74 65 33 53 74   && n==sqlite3St
2190a 72 6c 65 6e 33 30 28 70 44 62 2d 3e 7a 4e 61 6d  rlen30(pDb->zNam
2190b 65 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 20  e) && .         
2190c 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
2190d 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  mp(pDb->zName, z
2190e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Name) ){.       
2190f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
21910 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
21911 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn i;.}../*.** T
21912 68 65 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20  he token *pName 
21913 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d  contains the nam
21914 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  e of a database 
21915 28 65 69 74 68 65 72 20 22 6d 61 69 6e 22 20 6f  (either "main" o
21916 72 0a 2a 2a 20 22 74 65 6d 70 22 20 6f 72 20 74  r.** "temp" or t
21917 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74  he name of an at
21918 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 69 73  tached db). This
21919 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2191a 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66   the.** index of
2191b 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62   the named datab
2191c 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d  ase in db->aDb[]
2191d 2c 20 6f 72 20 2d 31 20 69 66 20 74 68 65 20 6e  , or -1 if the n
2191e 61 6d 65 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73  amed db .** does
2191f 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 53   not exist..*/.S
21920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
21921 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28  t sqlite3FindDb(
21922 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b  sqlite3 *db, Tok
21923 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e  en *pName){.  in
21924 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
21925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21926 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
21927 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  umber */.  char 
21928 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
21929 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2192a 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72 65 20 73  /* Name we are s
2192b 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a  earching for */.
2192c 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
2192d 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
2192e 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 20 3d  b, pName);.  i =
2192f 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61   sqlite3FindDbNa
21930 6d 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  me(db, zName);. 
21931 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
21932 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74  b, zName);.  ret
21933 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65  urn i;.}../* The
21934 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6f   table or view o
21935 72 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69  r trigger name i
21936 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  s passed to this
21937 20 72 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b   routine via tok
21938 65 6e 73 0a 2a 2a 20 70 4e 61 6d 65 31 20 61 6e  ens.** pName1 an
21939 64 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65  d pName2. If the
2193a 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20   table name was 
2193b 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c  fully qualified,
2193c 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a   for example:.**
2193d 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45  .** CREATE TABLE
2193e 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a   xxx.yyy (...);.
2193f 2a 2a 20 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d  ** .** Then pNam
21940 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 78 78  e1 is set to "xx
21941 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79  x" and pName2 "y
21942 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65  yy". On the othe
21943 72 20 68 61 6e 64 20 69 66 0a 2a 2a 20 74 68 65  r hand if.** the
21944 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e   table name is n
21945 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69  ot fully qualifi
21946 65 64 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20  ed, i.e.:.**.** 
21947 43 52 45 41 54 45 20 54 41 42 4c 45 20 79 79 79  CREATE TABLE yyy
21948 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65  (...);.**.** The
21949 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20  n pName1 is set 
2194a 74 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61  to "yyy" and pNa
2194b 6d 65 32 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a  me2 is ""..**.**
2194c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
2194d 74 73 20 74 68 65 20 2a 70 70 55 6e 71 75 61 6c  ts the *ppUnqual
2194e 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e   pointer to poin
2194f 74 20 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28  t at the token (
21950 70 4e 61 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e 61  pName1 or.** pNa
21951 6d 65 32 29 20 74 68 61 74 20 73 74 6f 72 65 73  me2) that stores
21952 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64   the unqualified
21953 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 20 54 68   table name.  Th
21954 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a  e index of the.*
21955 2a 20 64 61 74 61 62 61 73 65 20 22 78 78 78 22  * database "xxx"
21956 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
21957 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21958 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61  int sqlite3TwoPa
21959 72 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20  rtName(.  Parse 
2195a 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
2195b 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
2195c 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
2195d 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  text */.  Token 
2195e 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a  *pName1,      /*
2195f 20 54 68 65 20 22 78 78 78 22 20 69 6e 20 74 68   The "xxx" in th
21960 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22  e name "xxx.yyy"
21961 20 6f 72 20 22 78 78 78 22 20 2a 2f 0a 20 20 54   or "xxx" */.  T
21962 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
21963 20 20 20 2f 2a 20 54 68 65 20 22 79 79 79 22 20     /* The "yyy" 
21964 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78  in the name "xxx
21965 2e 79 79 79 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e  .yyy" */.  Token
21966 20 2a 2a 70 55 6e 71 75 61 6c 20 20 20 20 20 2f   **pUnqual     /
21967 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e 71 75  * Write the unqu
21968 61 6c 69 66 69 65 64 20 6f 62 6a 65 63 74 20 6e  alified object n
21969 61 6d 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ame here */.){. 
2196a 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
2196b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2196c 44 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  Database holding
2196d 20 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20   the object */. 
2196e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2196f 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66  Parse->db;..  if
21970 28 20 70 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d  ( pName2 && pNam
21971 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 69  e2->n>0 ){.    i
21972 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
21973 20 29 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74   ) {.      sqlit
21974 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
21975 65 2c 20 22 63 6f 72 72 75 70 74 20 64 61 74 61  e, "corrupt data
21976 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20 70 50  base");.      pP
21977 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
21978 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20      return -1;. 
21979 20 20 20 7d 0a 20 20 20 20 2a 70 55 6e 71 75 61     }.    *pUnqua
2197a 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 20 20 20  l = pName2;.    
2197b 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  iDb = sqlite3Fin
2197c 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b  dDb(db, pName1);
2197d 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29  .    if( iDb<0 )
2197e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2197f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
21980 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73  "unknown databas
21981 65 20 25 54 22 2c 20 70 4e 61 6d 65 31 29 3b 0a  e %T", pName1);.
21982 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45        pParse->nE
21983 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75  rr++;.      retu
21984 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn -1;.    }.  }
21985 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
21986 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d  ( db->init.iDb==
21987 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e 62 75  0 || db->init.bu
21988 73 79 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20  sy );.    iDb = 
21989 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20 20  db->init.iDb;.  
2198a 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61    *pUnqual = pNa
2198b 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  me1;.  }.  retur
2198c 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n iDb;.}../*.** 
2198d 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2198e 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66  used to check if
2198f 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e   the UTF-8 strin
21990 67 20 7a 4e 61 6d 65 20 69 73 20 61 20 6c 65 67  g zName is a leg
21991 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 66 69 65  al.** unqualifie
21992 64 20 6e 61 6d 65 20 66 6f 72 20 61 20 6e 65 77  d name for a new
21993 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 28   schema object (
21994 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 76 69  table, index, vi
21995 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72  ew or.** trigger
21996 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 61 72 65  ). All names are
21997 20 6c 65 67 61 6c 20 65 78 63 65 70 74 20 74 68   legal except th
21998 6f 73 65 20 74 68 61 74 20 62 65 67 69 6e 20 77  ose that begin w
21999 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 0a 2a  ith the string.*
2199a 2a 20 22 73 71 6c 69 74 65 5f 22 20 28 69 6e 20  * "sqlite_" (in 
2199b 75 70 70 65 72 2c 20 6c 6f 77 65 72 20 6f 72 20  upper, lower or 
2199c 6d 69 78 65 64 20 63 61 73 65 29 2e 20 54 68 69  mixed case). Thi
2199d 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65  s portion of the
2199e 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a 20 69 73   namespace.** is
2199f 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e   reserved for in
219a0 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f 0a 53  ternal use..*/.S
219a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
219a2 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62  t sqlite3CheckOb
219a3 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a  jectName(Parse *
219a4 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
219a5 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 66  ar *zName){.  if
219a6 28 20 21 70 50 61 72 73 65 2d 3e 64 62 2d 3e 69  ( !pParse->db->i
219a7 6e 69 74 2e 62 75 73 79 20 26 26 20 70 50 61 72  nit.busy && pPar
219a8 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 0a 20  se->nested==0 . 
219a9 20 20 20 20 20 20 20 20 20 26 26 20 28 70 50 61           && (pPa
219aa 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26  rse->db->flags &
219ab 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
219ac 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  ema)==0.        
219ad 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53    && 0==sqlite3S
219ae 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 20 22  trNICmp(zName, "
219af 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a  sqlite_", 7) ){.
219b0 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
219b1 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f 62 6a  Msg(pParse, "obj
219b2 65 63 74 20 6e 61 6d 65 20 72 65 73 65 72 76 65  ect name reserve
219b3 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75  d for internal u
219b4 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  se: %s", zName);
219b5 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
219b6 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
219b7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
219b8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e  ;.}../*.** Begin
219b9 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20   constructing a 
219ba 6e 65 77 20 74 61 62 6c 65 20 72 65 70 72 65 73  new table repres
219bb 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d 65 6d 6f  entation in memo
219bc 72 79 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20  ry.  This is.** 
219bd 74 68 65 20 66 69 72 73 74 20 6f 66 20 73 65 76  the first of sev
219be 65 72 61 6c 20 61 63 74 69 6f 6e 20 72 6f 75 74  eral action rout
219bf 69 6e 65 73 20 74 68 61 74 20 67 65 74 20 63 61  ines that get ca
219c0 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65  lled in response
219c1 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41 54 45 20  .** to a CREATE 
219c2 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
219c3 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c    In particular,
219c4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
219c5 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 74 65 72   called.** after
219c6 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e 73 20 22   seeing tokens "
219c7 43 52 45 41 54 45 22 20 61 6e 64 20 22 54 41 42  CREATE" and "TAB
219c8 4c 45 22 20 61 6e 64 20 74 68 65 20 74 61 62 6c  LE" and the tabl
219c9 65 20 6e 61 6d 65 2e 20 54 68 65 20 69 73 54 65  e name. The isTe
219ca 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74 72  mp.** flag is tr
219cb 75 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ue if the table 
219cc 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65 64  should be stored
219cd 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72   in the auxiliar
219ce 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  y database.** fi
219cf 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e  le instead of in
219d0 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
219d1 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69  se file.  This i
219d2 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 65 20 63  s normally the c
219d3 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ase.** when the 
219d4 22 54 45 4d 50 22 20 6f 72 20 22 54 45 4d 50 4f  "TEMP" or "TEMPO
219d5 52 41 52 59 22 20 6b 65 79 77 6f 72 64 20 6f 63  RARY" keyword oc
219d6 63 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 0a  curs in between.
219d7 2a 2a 20 43 52 45 41 54 45 20 61 6e 64 20 54 41  ** CREATE and TA
219d8 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  BLE..**.** The n
219d9 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20  ew table record 
219da 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61  is initialized a
219db 6e 64 20 70 75 74 20 69 6e 20 70 50 61 72 73 65  nd put in pParse
219dc 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a 2a 2a 20  ->pNewTable..** 
219dd 41 73 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 43  As more of the C
219de 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
219df 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c  ement is parsed,
219e0 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 63 74 69   additional acti
219e1 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77  on.** routines w
219e2 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f  ill be called to
219e3 20 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f 72 6d   add more inform
219e4 61 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 72 65  ation to this re
219e5 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 68 65 20  cord..** At the 
219e6 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54  end of the CREAT
219e7 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
219e8 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33 45 6e  t, the sqlite3En
219e9 64 54 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65  dTable() routine
219ea 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  .** is called to
219eb 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 63 6f   complete the co
219ec 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68  nstruction of th
219ed 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f  e new table reco
219ee 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rd..*/.SQLITE_PR
219ef 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
219f0 65 33 53 74 61 72 74 54 61 62 6c 65 28 0a 20 20  e3StartTable(.  
219f1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
219f2 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
219f3 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
219f4 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46 69 72 73  Name1,   /* Firs
219f5 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61  t part of the na
219f6 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
219f7 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b  or view */.  Tok
219f8 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 2f 2a  en *pName2,   /*
219f9 20 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20   Second part of 
219fa 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
219fb 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f  table or view */
219fc 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20  .  int isTemp,  
219fd 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
219fe 68 69 73 20 69 73 20 61 20 54 45 4d 50 20 74 61  his is a TEMP ta
219ff 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56  ble */.  int isV
21a00 69 65 77 2c 20 20 20 20 20 20 2f 2a 20 54 72 75  iew,      /* Tru
21a01 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56  e if this is a V
21a02 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56  IEW */.  int isV
21a03 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20 54 72 75  irtual,   /* Tru
21a04 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56  e if this is a V
21a05 49 52 54 55 41 4c 20 74 61 62 6c 65 20 2a 2f 0a  IRTUAL table */.
21a06 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20    int noErr     
21a07 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67     /* Do nothing
21a08 20 69 66 20 74 61 62 6c 65 20 61 6c 72 65 61 64   if table alread
21a09 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
21a0a 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 0a   Table *pTable;.
21a0b 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
21a0c 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f  0; /* The name o
21a0d 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
21a0e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
21a0f 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
21a10 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
21a11 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  iDb;         /* 
21a12 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20  Database number 
21a13 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 61  to create the ta
21a14 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ble in */.  Toke
21a15 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 2f 2a 20  n *pName;    /* 
21a16 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65  Unqualified name
21a17 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
21a18 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a   create */..  /*
21a19 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69   The table or vi
21a1a 65 77 20 6e 61 6d 65 20 74 6f 20 63 72 65 61 74  ew name to creat
21a1b 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74  e is passed to t
21a1c 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69 61 20  his routine via 
21a1d 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70 4e 61 6d  tokens.  ** pNam
21a1e 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49  e1 and pName2. I
21a1f 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  f the table name
21a20 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69   was fully quali
21a21 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c  fied, for exampl
21a22 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45  e:.  **.  ** CRE
21a23 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79  ATE TABLE xxx.yy
21a24 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 20 0a 20  y (...);.  ** . 
21a25 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20   ** Then pName1 
21a26 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22 20  is set to "xxx" 
21a27 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22  and pName2 "yyy"
21a28 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  . On the other h
21a29 61 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 65 20  and if.  ** the 
21a2a 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f  table name is no
21a2b 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65  t fully qualifie
21a2c 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a 0a 20 20  d, i.e.:.  **.  
21a2d 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
21a2e 79 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a 20  yyy(...);.  **. 
21a2f 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20   ** Then pName1 
21a30 69 73 20 73 65 74 20 74 6f 20 22 79 79 79 22 20  is set to "yyy" 
21a31 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22  and pName2 is ""
21a32 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
21a33 63 61 6c 6c 20 62 65 6c 6f 77 20 73 65 74 73 20  call below sets 
21a34 74 68 65 20 70 4e 61 6d 65 20 70 6f 69 6e 74 65  the pName pointe
21a35 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  r to point at th
21a36 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20  e token (pName1 
21a37 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d 65 32 29 20  or.  ** pName2) 
21a38 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20  that stores the 
21a39 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c  unqualified tabl
21a3a 65 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 72 69  e name. The vari
21a3b 61 62 6c 65 20 69 44 62 20 69 73 0a 20 20 2a 2a  able iDb is.  **
21a3c 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65   set to the inde
21a3d 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
21a3e 65 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  e that the table
21a3f 20 6f 72 20 76 69 65 77 20 69 73 20 74 6f 20 62   or view is to b
21a40 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69  e.  ** created i
21a41 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44 62 20 3d 20  n..  */.  iDb = 
21a42 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
21a43 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  me(pParse, pName
21a44 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d  1, pName2, &pNam
21a45 65 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20  e);.  if( iDb<0 
21a46 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
21a47 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
21a48 69 73 54 65 6d 70 20 26 26 20 69 44 62 3e 31 20  isTemp && iDb>1 
21a49 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 63 72 65  ){.    /* If cre
21a4a 61 74 69 6e 67 20 61 20 74 65 6d 70 20 74 61 62  ating a temp tab
21a4b 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20 6d 61 79  le, the name may
21a4c 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65   not be qualifie
21a4d 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  d */.    sqlite3
21a4e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
21a4f 20 22 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c   "temporary tabl
21a50 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 75  e name must be u
21a51 6e 71 75 61 6c 69 66 69 65 64 22 29 3b 0a 20 20  nqualified");.  
21a52 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
21a53 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
21a54 20 26 26 20 69 73 54 65 6d 70 20 29 20 69 44 62   && isTemp ) iDb
21a55 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72 73 65 2d   = 1;..  pParse-
21a56 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70  >sNameToken = *p
21a57 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  Name;.  zName = 
21a58 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
21a59 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b  oken(db, pName);
21a5a 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20  .  if( zName==0 
21a5b 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
21a5c 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
21a5d 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
21a5e 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29  e(pParse, zName)
21a5f 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67   ){.    goto beg
21a60 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
21a61 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e    }.  if( db->in
21a62 69 74 2e 69 44 62 3d 3d 31 20 29 20 69 73 54 65  it.iDb==1 ) isTe
21a63 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20  mp = 1;.#ifndef 
21a64 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
21a65 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65  ORIZATION.  asse
21a66 72 74 28 20 28 69 73 54 65 6d 70 20 26 20 31 29  rt( (isTemp & 1)
21a67 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20 20 7b 0a  ==isTemp );.  {.
21a68 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20      int code;.  
21a69 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62    char *zDb = db
21a6a 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
21a6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
21a6c 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
21a6d 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  e, SQLITE_INSERT
21a6e 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
21a6f 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29 20  sTemp), 0, zDb) 
21a70 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65  ){.      goto be
21a71 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
21a72 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
21a73 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69  sView ){.      i
21a74 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
21a75 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20  && isTemp ){.   
21a76 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
21a77 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56  TE_CREATE_TEMP_V
21a78 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  IEW;.      }else
21a79 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
21a7a 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56   SQLITE_CREATE_V
21a7b 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  IEW;.      }.   
21a7c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
21a7d 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
21a7e 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20  & isTemp ){.    
21a7f 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
21a80 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41  E_CREATE_TEMP_TA
21a81 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  BLE;.      }else
21a82 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
21a83 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
21a84 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ABLE;.      }.  
21a85 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 56    }.    if( !isV
21a86 69 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74 65  irtual && sqlite
21a87 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
21a88 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20  e, code, zName, 
21a89 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  0, zDb) ){.     
21a8a 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
21a8b 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  e_error;.    }. 
21a8c 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
21a8d 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e 65  Make sure the ne
21a8e 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f 65  w table name doe
21a8f 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69  s not collide wi
21a90 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20  th an existing. 
21a91 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62   ** index or tab
21a92 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73  le name in the s
21a93 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20 20 49  ame database.  I
21a94 73 73 75 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  ssue an error me
21a95 73 73 61 67 65 20 69 66 0a 20 20 2a 2a 20 69 74  ssage if.  ** it
21a96 20 64 6f 65 73 2e 20 54 68 65 20 65 78 63 65 70   does. The excep
21a97 74 69 6f 6e 20 69 73 20 69 66 20 74 68 65 20 73  tion is if the s
21a98 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 70  tatement being p
21a99 61 72 73 65 64 20 77 61 73 20 70 61 73 73 65 64  arsed was passed
21a9a 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c 69  .  ** to an sqli
21a9b 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
21a9c 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61 74  () call. In that
21a9d 20 63 61 73 65 20 6f 6e 6c 79 20 74 68 65 20 63   case only the c
21a9e 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a 2a  olumn names.  **
21a9f 20 61 6e 64 20 74 79 70 65 73 20 77 69 6c 6c 20   and types will 
21aa0 62 65 20 75 73 65 64 2c 20 73 6f 20 74 68 65 72  be used, so ther
21aa1 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20  e is no need to 
21aa2 74 65 73 74 20 66 6f 72 20 6e 61 6d 65 73 70 61  test for namespa
21aa3 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69 6f  ce.  ** collisio
21aa4 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ns..  */.  if( !
21aa5 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
21aa6 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  ){.    if( SQLIT
21aa7 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
21aa8 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
21aa9 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65  ){.      goto be
21aaa 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
21aab 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 6c  .    }.    pTabl
21aac 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  e = sqlite3FindT
21aad 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  able(db, zName, 
21aae 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
21aaf 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  me);.    if( pTa
21ab0 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ble ){.      if(
21ab1 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20   !noErr ){.     
21ab2 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
21ab3 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c  sg(pParse, "tabl
21ab4 65 20 25 54 20 61 6c 72 65 61 64 79 20 65 78 69  e %T already exi
21ab5 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  sts", pName);.  
21ab6 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
21ab7 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
21ab8 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  or;.    }.    if
21ab9 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  ( sqlite3FindInd
21aba 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 30 29  ex(db, zName, 0)
21abb 21 3d 30 20 26 26 20 28 69 44 62 3d 3d 30 20 7c  !=0 && (iDb==0 |
21abc 7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  | !db->init.busy
21abd 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
21abe 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
21abf 65 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72  e, "there is alr
21ac0 65 61 64 79 20 61 6e 20 69 6e 64 65 78 20 6e 61  eady an index na
21ac1 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  med %s", zName);
21ac2 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69  .      goto begi
21ac3 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20  n_table_error;. 
21ac4 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 54 61 62     }.  }..  pTab
21ac5 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  le = sqlite3DbMa
21ac6 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
21ac7 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69  eof(Table));.  i
21ac8 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 7b 0a  f( pTable==0 ){.
21ac9 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
21aca 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 70 50  iled = 1;.    pP
21acb 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
21acc 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 70 50 61  E_NOMEM;.    pPa
21acd 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  rse->nErr++;.   
21ace 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
21acf 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70  e_error;.  }.  p
21ad0 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d 20 7a  Table->zName = z
21ad1 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c 65 2d 3e  Name;.  pTable->
21ad2 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 70 54  iPKey = -1;.  pT
21ad3 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 3d 20  able->pSchema = 
21ad4 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
21ad5 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c 65 2d 3e  hema;.  pTable->
21ad6 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62  nRef = 1;.  pTab
21ad7 6c 65 2d 3e 64 62 4d 65 6d 20 3d 20 64 62 2d 3e  le->dbMem = db->
21ad8 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
21ad9 65 64 20 3f 20 64 62 20 3a 20 30 3b 0a 20 20 69  ed ? db : 0;.  i
21ada 66 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  f( pParse->pNewT
21adb 61 62 6c 65 20 29 20 73 71 6c 69 74 65 33 44 65  able ) sqlite3De
21adc 6c 65 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  leteTable(pParse
21add 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20  ->pNewTable);.  
21ade 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
21adf 65 20 3d 20 70 54 61 62 6c 65 3b 0a 0a 20 20 2f  e = pTable;..  /
21ae0 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  * If this is the
21ae1 20 6d 61 67 69 63 20 73 71 6c 69 74 65 5f 73 65   magic sqlite_se
21ae2 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65  quence table use
21ae3 64 20 62 79 20 61 75 74 6f 69 6e 63 72 65 6d 65  d by autoincreme
21ae4 6e 74 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65  nt,.  ** then re
21ae5 63 6f 72 64 20 61 20 70 6f 69 6e 74 65 72 20 74  cord a pointer t
21ae6 6f 20 74 68 69 73 20 74 61 62 6c 65 20 69 6e 20  o this table in 
21ae7 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
21ae8 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a  e structure.  **
21ae9 20 73 6f 20 74 68 61 74 20 49 4e 53 45 52 54 20   so that INSERT 
21aea 63 61 6e 20 66 69 6e 64 20 74 68 65 20 74 61 62  can find the tab
21aeb 6c 65 20 65 61 73 69 6c 79 2e 0a 20 20 2a 2f 0a  le easily..  */.
21aec 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
21aed 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
21aee 54 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d  T.  if( !pParse-
21aef 3e 6e 65 73 74 65 64 20 26 26 20 73 74 72 63 6d  >nested && strcm
21af0 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  p(zName, "sqlite
21af1 5f 73 65 71 75 65 6e 63 65 22 29 3d 3d 30 20 29  _sequence")==0 )
21af2 7b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53  {.    pTable->pS
21af3 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 20 3d  chema->pSeqTab =
21af4 20 70 54 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e   pTable;.  }.#en
21af5 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20  dif..  /* Begin 
21af6 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63  generating the c
21af7 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e  ode that will in
21af8 73 65 72 74 20 74 68 65 20 74 61 62 6c 65 20 72  sert the table r
21af9 65 63 6f 72 64 20 69 6e 74 6f 0a 20 20 2a 2a 20  ecord into.  ** 
21afa 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45  the SQLITE_MASTE
21afb 52 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 20 69  R table.  Note i
21afc 6e 20 70 61 72 74 69 63 75 6c 61 72 20 74 68 61  n particular tha
21afd 74 20 77 65 20 6d 75 73 74 20 67 6f 20 61 68 65  t we must go ahe
21afe 61 64 0a 20 20 2a 2a 20 61 6e 64 20 61 6c 6c 6f  ad.  ** and allo
21aff 63 61 74 65 20 74 68 65 20 72 65 63 6f 72 64 20  cate the record 
21b00 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
21b01 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f 77 2e 20  able entry now. 
21b02 20 42 65 66 6f 72 65 20 61 6e 79 0a 20 20 2a 2a   Before any.  **
21b03 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
21b04 55 4e 49 51 55 45 20 6b 65 79 77 6f 72 64 73 20  UNIQUE keywords 
21b05 61 72 65 20 70 61 72 73 65 64 2e 20 20 54 68 6f  are parsed.  Tho
21b06 73 65 20 6b 65 79 77 6f 72 64 73 20 77 69 6c 6c  se keywords will
21b07 20 63 61 75 73 65 0a 20 20 2a 2a 20 69 6e 64 69   cause.  ** indi
21b08 63 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65  ces to be create
21b09 64 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20  d and the table 
21b0a 72 65 63 6f 72 64 20 6d 75 73 74 20 63 6f 6d 65  record must come
21b0b 20 62 65 66 6f 72 65 20 74 68 65 20 0a 20 20 2a   before the .  *
21b0c 2a 20 69 6e 64 69 63 65 73 2e 20 20 48 65 6e 63  * indices.  Henc
21b0d 65 2c 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  e, the record nu
21b0e 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62  mber for the tab
21b0f 6c 65 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63  le must be alloc
21b10 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 77 2e 0a 20  ated.  ** now.. 
21b11 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69   */.  if( !db->i
21b12 6e 69 74 2e 62 75 73 79 20 26 26 20 28 76 20 3d  nit.busy && (v =
21b13 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
21b14 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a 20  pParse))!=0 ){. 
21b15 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 69     int j1;.    i
21b16 6e 74 20 66 69 6c 65 46 6f 72 6d 61 74 3b 0a 20  nt fileFormat;. 
21b17 20 20 20 69 6e 74 20 72 65 67 31 2c 20 72 65 67     int reg1, reg
21b18 32 2c 20 72 65 67 33 3b 0a 20 20 20 20 73 71 6c  2, reg3;.    sql
21b19 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
21b1a 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
21b1b 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65  0, iDb);..#ifnde
21b1c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
21b1d 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
21b1e 66 28 20 69 73 56 69 72 74 75 61 6c 20 29 7b 0a  f( isVirtual ){.
21b1f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21b20 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42  eAddOp0(v, OP_VB
21b21 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  egin);.    }.#en
21b22 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  dif..    /* If t
21b23 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61  he file format a
21b24 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69 6e 20 74  nd encoding in t
21b25 68 65 20 64 61 74 61 62 61 73 65 20 68 61 76 65  he database have
21b26 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c 20 0a   not been set, .
21b27 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65 6d 20      ** set them 
21b28 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  now..    */.    
21b29 72 65 67 31 20 3d 20 70 50 61 72 73 65 2d 3e 72  reg1 = pParse->r
21b2a 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72  egRowid = ++pPar
21b2b 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65  se->nMem;.    re
21b2c 67 32 20 3d 20 70 50 61 72 73 65 2d 3e 72 65 67  g2 = pParse->reg
21b2d 52 6f 6f 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  Root = ++pParse-
21b2e 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 33 20  >nMem;.    reg3 
21b2f 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
21b30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21b31 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65  eAddOp3(v, OP_Re
21b32 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72  adCookie, iDb, r
21b33 65 67 33 2c 20 31 29 3b 20 20 20 2f 2a 20 66 69  eg3, 1);   /* fi
21b34 6c 65 5f 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20  le_format */.   
21b35 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
21b36 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20  Btree(v, iDb);. 
21b37 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
21b38 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
21b39 49 66 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 66  If, reg3);.    f
21b3a 69 6c 65 46 6f 72 6d 61 74 20 3d 20 28 64 62 2d  ileFormat = (db-
21b3b 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
21b3c 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 29 21 3d  LegacyFileFmt)!=
21b3d 30 20 3f 0a 20 20 20 20 20 20 20 20 20 20 20 20  0 ?.            
21b3e 20 20 20 20 20 20 31 20 3a 20 53 51 4c 49 54 45        1 : SQLITE
21b3f 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  _MAX_FILE_FORMAT
21b40 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21b41 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
21b42 74 65 67 65 72 2c 20 66 69 6c 65 46 6f 72 6d 61  teger, fileForma
21b43 74 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71  t, reg3);.    sq
21b44 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21b45 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c  v, OP_SetCookie,
21b46 20 69 44 62 2c 20 31 2c 20 72 65 67 33 29 3b 0a   iDb, 1, reg3);.
21b47 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21b48 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
21b49 67 65 72 2c 20 45 4e 43 28 64 62 29 2c 20 72 65  ger, ENC(db), re
21b4a 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g3);.    sqlite3
21b4b 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21b4c 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  _SetCookie, iDb,
21b4d 20 34 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73   4, reg3);.    s
21b4e 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
21b4f 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20  re(v, j1);..    
21b50 2f 2a 20 54 68 69 73 20 6a 75 73 74 20 63 72 65  /* This just cre
21b51 61 74 65 73 20 61 20 70 6c 61 63 65 2d 68 6f 6c  ates a place-hol
21b52 64 65 72 20 72 65 63 6f 72 64 20 69 6e 20 74 68  der record in th
21b53 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
21b54 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 54 68  table..    ** Th
21b55 65 20 72 65 63 6f 72 64 20 63 72 65 61 74 65 64  e record created
21b56 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69   does not contai
21b57 6e 20 61 6e 79 74 68 69 6e 67 20 79 65 74 2e 20  n anything yet. 
21b58 20 49 74 20 77 69 6c 6c 20 62 65 20 72 65 70 6c   It will be repl
21b59 61 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74  aced.    ** by t
21b5a 68 65 20 72 65 61 6c 20 65 6e 74 72 79 20 69 6e  he real entry in
21b5b 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
21b5c 61 74 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  at sqlite3EndTab
21b5d 6c 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  le()..    **.   
21b5e 20 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 66 6f   ** The rowid fo
21b5f 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  r the new entry 
21b60 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67 69 73  is left in regis
21b61 74 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52  ter pParse->regR
21b62 6f 77 69 64 2e 0a 20 20 20 20 2a 2a 20 54 68 65  owid..    ** The
21b63 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
21b64 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  r of the new tab
21b65 6c 65 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65  le is left in re
21b66 67 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f  g pParse->regRoo
21b67 74 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f  t..    ** The ro
21b68 77 69 64 20 61 6e 64 20 72 6f 6f 74 20 70 61 67  wid and root pag
21b69 65 20 6e 75 6d 62 65 72 20 76 61 6c 75 65 73 20  e number values 
21b6a 61 72 65 20 6e 65 65 64 65 64 20 62 79 20 74 68  are needed by th
21b6b 65 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20 20  e code that.    
21b6c 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  ** sqlite3EndTab
21b6d 6c 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74 65  le will generate
21b6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 20 21 64 65  ..    */.#if !de
21b6f 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
21b70 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69  T_VIEW) || !defi
21b71 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
21b72 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20  VIRTUALTABLE).  
21b73 20 20 69 66 28 20 69 73 56 69 65 77 20 7c 7c 20    if( isView || 
21b74 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20  isVirtual ){.   
21b75 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21b76 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
21b77 65 72 2c 20 30 2c 20 72 65 67 32 29 3b 0a 20 20  er, 0, reg2);.  
21b78 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
21b79 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74     {.      sqlit
21b7a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21b7b 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 2c 20  OP_CreateTable, 
21b7c 69 44 62 2c 20 72 65 67 32 29 3b 0a 20 20 20 20  iDb, reg2);.    
21b7d 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65  }.    sqlite3Ope
21b7e 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70 50 61  nMasterTable(pPa
21b7f 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  rse, iDb);.    s
21b80 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21b81 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
21b82 20 30 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73   0, reg1);.    s
21b83 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21b84 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
21b85 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74  reg3);.    sqlit
21b86 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
21b87 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20 72 65  OP_Insert, 0, re
21b88 67 33 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73  g3, reg1);.    s
21b89 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
21b8a 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
21b8b 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  END);.    sqlite
21b8c 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
21b8d 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 0a 20  P_Close);.  }.. 
21b8e 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e 6f 6e 2d   /* Normal (non-
21b8f 65 72 72 6f 72 29 20 72 65 74 75 72 6e 2e 20 2a  error) return. *
21b90 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f  /.  return;..  /
21b91 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
21b92 63 75 72 73 2c 20 77 65 20 6a 75 6d 70 20 68 65  curs, we jump he
21b93 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 61 62 6c  re */.begin_tabl
21b94 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74  e_error:.  sqlit
21b95 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61  e3DbFree(db, zNa
21b96 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  me);.  return;.}
21b97 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63  ../*.** This mac
21b98 72 6f 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  ro is used to co
21b99 6d 70 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67  mpare two string
21b9a 73 20 69 6e 20 61 20 63 61 73 65 2d 69 6e 73 65  s in a case-inse
21b9b 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65 72 2e 0a  nsitive manner..
21b9c 2a 2a 20 49 74 20 69 73 20 73 6c 69 67 68 74 6c  ** It is slightl
21b9d 79 20 66 61 73 74 65 72 20 74 68 61 6e 20 63 61  y faster than ca
21b9e 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 53 74 72  lling sqlite3Str
21b9f 49 43 6d 70 28 29 20 64 69 72 65 63 74 6c 79 2c  ICmp() directly,
21ba0 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75 63 65 73   but.** produces
21ba1 20 6c 61 72 67 65 72 20 63 6f 64 65 2e 0a 2a 2a   larger code..**
21ba2 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 54 68 69  .** WARNING: Thi
21ba3 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 63  s macro is not c
21ba4 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 74  ompatible with t
21ba5 68 65 20 73 74 72 63 6d 70 28 29 20 66 61 6d 69  he strcmp() fami
21ba6 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74 75 72 6e  ly. It.** return
21ba7 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74 77  s true if the tw
21ba8 6f 20 73 74 72 69 6e 67 73 20 61 72 65 20 65 71  o strings are eq
21ba9 75 61 6c 2c 20 6f 74 68 65 72 77 69 73 65 20 66  ual, otherwise f
21baa 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  alse..*/.#define
21bab 20 53 54 52 49 43 4d 50 28 78 2c 20 79 29 20 28   STRICMP(x, y) (
21bac 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  \.sqlite3UpperTo
21bad 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64  Lower[*(unsigned
21bae 20 63 68 61 72 20 2a 29 28 78 29 5d 3d 3d 20 20   char *)(x)]==  
21baf 20 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54   \.sqlite3UpperT
21bb0 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65  oLower[*(unsigne
21bb1 64 20 63 68 61 72 20 2a 29 28 79 29 5d 20 20 20  d char *)(y)]   
21bb2 20 20 5c 0a 26 26 20 73 71 6c 69 74 65 33 53 74    \.&& sqlite3St
21bb3 72 49 43 6d 70 28 28 78 29 2b 31 2c 28 79 29 2b  rICmp((x)+1,(y)+
21bb4 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a 20 41  1)==0 )../*.** A
21bb5 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20  dd a new column 
21bb6 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
21bb7 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e  rently being con
21bb8 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  structed..**.** 
21bb9 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
21bba 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   this routine on
21bbb 63 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  ce for each colu
21bbc 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a  mn declaration.*
21bbd 2a 20 69 6e 20 61 20 43 52 45 41 54 45 20 54 41  * in a CREATE TA
21bbe 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  BLE statement.  
21bbf 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
21bc0 65 28 29 20 67 65 74 73 20 63 61 6c 6c 65 64 0a  e() gets called.
21bc1 2a 2a 20 66 69 72 73 74 20 74 6f 20 67 65 74 20  ** first to get 
21bc2 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20 20 54  things going.  T
21bc3 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
21bc4 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 65   is called for e
21bc5 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a  ach.** column..*
21bc6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21bc7 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
21bc8 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50  Column(Parse *pP
21bc9 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
21bca 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  me){.  Table *p;
21bcb 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72  .  int i;.  char
21bcc 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70   *z;.  Column *p
21bcd 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Col;.  sqlite3 *
21bce 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
21bcf 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72  .  if( (p = pPar
21bd0 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d  se->pNewTable)==
21bd1 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 20  0 ) return;.#if 
21bd2 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
21bd3 4e 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b  N.  if( p->nCol+
21bd4 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  1>db->aLimit[SQL
21bd5 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
21bd6 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
21bd7 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
21bd8 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d   "too many colum
21bd9 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e 7a 4e  ns on %s", p->zN
21bda 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
21bdb 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7a  ;.  }.#endif.  z
21bdc 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
21bdd 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d  omToken(db, pNam
21bde 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29  e);.  if( z==0 )
21bdf 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
21be0 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69  =0; i<p->nCol; i
21be1 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 54 52  ++){.    if( STR
21be2 49 43 4d 50 28 7a 2c 20 70 2d 3e 61 43 6f 6c 5b  ICMP(z, p->aCol[
21be3 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  i].zName) ){.   
21be4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
21be5 73 67 28 70 50 61 72 73 65 2c 20 22 64 75 70 6c  sg(pParse, "dupl
21be6 69 63 61 74 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  icate column nam
21be7 65 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20  e: %s", z);.    
21be8 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
21be9 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 65  db, z);.      re
21bea 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
21beb 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f 6c 20 26    if( (p->nCol &
21bec 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20   0x7)==0 ){.    
21bed 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20  Column *aNew;.  
21bee 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    aNew = sqlite3
21bef 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e  DbRealloc(db,p->
21bf0 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29  aCol,(p->nCol+8)
21bf1 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b  *sizeof(p->aCol[
21bf2 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 4e  0]));.    if( aN
21bf3 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ew==0 ){.      s
21bf4 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
21bf5 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72   z);.      retur
21bf6 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  n;.    }.    p->
21bf7 61 43 6f 6c 20 3d 20 61 4e 65 77 3b 0a 20 20 7d  aCol = aNew;.  }
21bf8 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43  .  pCol = &p->aC
21bf9 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d  ol[p->nCol];.  m
21bfa 65 6d 73 65 74 28 70 43 6f 6c 2c 20 30 2c 20 73  emset(pCol, 0, s
21bfb 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d  izeof(p->aCol[0]
21bfc 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  ));.  pCol->zNam
21bfd 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f 2a 20 49 66  e = z;. .  /* If
21bfe 20 74 68 65 72 65 20 69 73 20 6e 6f 20 74 79 70   there is no typ
21bff 65 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f 6c  e specified, col
21c00 75 6d 6e 73 20 68 61 76 65 20 74 68 65 20 64 65  umns have the de
21c01 66 61 75 6c 74 20 61 66 66 69 6e 69 74 79 0a 20  fault affinity. 
21c02 20 2a 2a 20 27 4e 4f 4e 45 27 2e 20 49 66 20 74   ** 'NONE'. If t
21c03 68 65 72 65 20 69 73 20 61 20 74 79 70 65 20 73  here is a type s
21c04 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 73  pecified, then s
21c05 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54  qlite3AddColumnT
21c06 79 70 65 28 29 20 77 69 6c 6c 0a 20 20 2a 2a 20  ype() will.  ** 
21c07 62 65 20 63 61 6c 6c 65 64 20 6e 65 78 74 20 74  be called next t
21c08 6f 20 73 65 74 20 70 43 6f 6c 2d 3e 61 66 66 69  o set pCol->affi
21c09 6e 69 74 79 20 63 6f 72 72 65 63 74 6c 79 2e 0a  nity correctly..
21c0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d 3e 61 66 66    */.  pCol->aff
21c0b 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
21c0c 46 46 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 6e 43  FF_NONE;.  p->nC
21c0d 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ol++;.}../*.** T
21c0e 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
21c0f 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
21c10 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65  ser while in the
21c11 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61   middle of.** pa
21c12 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54  rsing a CREATE T
21c13 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
21c14 20 41 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f   A "NOT NULL" co
21c15 6e 73 74 72 61 69 6e 74 20 68 61 73 0a 2a 2a 20  nstraint has.** 
21c16 62 65 65 6e 20 73 65 65 6e 20 6f 6e 20 61 20 63  been seen on a c
21c17 6f 6c 75 6d 6e 2e 20 20 54 68 69 73 20 72 6f 75  olumn.  This rou
21c18 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 6e 6f  tine sets the no
21c19 74 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a  tNull flag on.**
21c1a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 75 72 72   the column curr
21c1b 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
21c1c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  truction..*/.SQL
21c1d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21c1e 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75   sqlite3AddNotNu
21c1f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ll(Parse *pParse
21c20 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a  , int onError){.
21c21 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e    Table *p;.  in
21c22 74 20 69 3b 0a 20 20 69 66 28 20 28 70 20 3d 20  t i;.  if( (p = 
21c23 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
21c24 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  e)==0 ) return;.
21c25 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b    i = p->nCol-1;
21c26 0a 20 20 69 66 28 20 69 3e 3d 30 20 29 20 70 2d  .  if( i>=0 ) p-
21c27 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c  >aCol[i].notNull
21c28 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a   = (u8)onError;.
21c29 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68  }../*.** Scan th
21c2a 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6e 61  e column type na
21c2b 6d 65 20 7a 54 79 70 65 20 28 6c 65 6e 67 74 68  me zType (length
21c2c 20 6e 54 79 70 65 29 20 61 6e 64 20 72 65 74 75   nType) and retu
21c2d 72 6e 20 74 68 65 0a 2a 2a 20 61 73 73 6f 63 69  rn the.** associ
21c2e 61 74 65 64 20 61 66 66 69 6e 69 74 79 20 74 79  ated affinity ty
21c2f 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  pe..**.** This r
21c30 6f 75 74 69 6e 65 20 64 6f 65 73 20 61 20 63 61  outine does a ca
21c31 73 65 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 73  se-independent s
21c32 65 61 72 63 68 20 6f 66 20 7a 54 79 70 65 20 66  earch of zType f
21c33 6f 72 20 74 68 65 20 0a 2a 2a 20 73 75 62 73 74  or the .** subst
21c34 72 69 6e 67 73 20 69 6e 20 74 68 65 20 66 6f 6c  rings in the fol
21c35 6c 6f 77 69 6e 67 20 74 61 62 6c 65 2e 20 49 66  lowing table. If
21c36 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73   one of the subs
21c37 74 72 69 6e 67 73 20 69 73 0a 2a 2a 20 66 6f 75  trings is.** fou
21c38 6e 64 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f  nd, the correspo
21c39 6e 64 69 6e 67 20 61 66 66 69 6e 69 74 79 20 69  nding affinity i
21c3a 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 7a  s returned. If z
21c3b 54 79 70 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  Type contains.**
21c3c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f   more than one o
21c3d 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73  f the substrings
21c3e 2c 20 65 6e 74 72 69 65 73 20 74 6f 77 61 72 64  , entries toward
21c3f 20 74 68 65 20 74 6f 70 20 6f 66 20 0a 2a 2a 20   the top of .** 
21c40 74 68 65 20 74 61 62 6c 65 20 74 61 6b 65 20 70  the table take p
21c41 72 69 6f 72 69 74 79 2e 20 46 6f 72 20 65 78 61  riority. For exa
21c42 6d 70 6c 65 2c 20 69 66 20 7a 54 79 70 65 20 69  mple, if zType i
21c43 73 20 27 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a  s 'BLOBINT', .**
21c44 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
21c45 47 45 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e  GER is returned.
21c46 0a 2a 2a 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67  .**.** Substring
21c47 20 20 20 20 20 7c 20 41 66 66 69 6e 69 74 79 0a       | Affinity.
21c48 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ** -------------
21c49 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
21c4a 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 20 20  ---.** 'INT'    
21c4b 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
21c4c 46 5f 49 4e 54 45 47 45 52 0a 2a 2a 20 27 43 48  F_INTEGER.** 'CH
21c4d 41 52 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c  AR'        | SQL
21c4e 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20  ITE_AFF_TEXT.** 
21c4f 27 43 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20  'CLOB'        | 
21c50 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a  SQLITE_AFF_TEXT.
21c51 2a 2a 20 27 54 45 58 54 27 20 20 20 20 20 20 20  ** 'TEXT'       
21c52 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   | SQLITE_AFF_TE
21c53 58 54 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 20 20  XT.** 'BLOB'    
21c54 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46      | SQLITE_AFF
21c55 5f 4e 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c 27 20  _NONE.** 'REAL' 
21c56 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
21c57 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f  AFF_REAL.** 'FLO
21c58 41 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49  A'        | SQLI
21c59 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27  TE_AFF_REAL.** '
21c5a 44 4f 55 42 27 20 20 20 20 20 20 20 20 7c 20 53  DOUB'        | S
21c5b 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a  QLITE_AFF_REAL.*
21c5c 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20  *.** If none of 
21c5d 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 20 69  the substrings i
21c5e 6e 20 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c  n the above tabl
21c5f 65 20 61 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20  e are found,.** 
21c60 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
21c61 49 43 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  IC is returned..
21c62 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21c63 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 41 66  E char sqlite3Af
21c64 66 69 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74  finityType(const
21c65 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a   Token *pType){.
21c66 20 20 75 33 32 20 68 20 3d 20 30 3b 0a 20 20 63    u32 h = 0;.  c
21c67 68 61 72 20 61 66 66 20 3d 20 53 51 4c 49 54 45  har aff = SQLITE
21c68 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20  _AFF_NUMERIC;.  
21c69 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
21c6a 68 61 72 20 2a 7a 49 6e 20 3d 20 70 54 79 70 65  har *zIn = pType
21c6b 2d 3e 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  ->z;.  const uns
21c6c 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 6e 64  igned char *zEnd
21c6d 20 3d 20 26 70 54 79 70 65 2d 3e 7a 5b 70 54 79   = &pType->z[pTy
21c6e 70 65 2d 3e 6e 5d 3b 0a 0a 20 20 77 68 69 6c 65  pe->n];..  while
21c6f 28 20 7a 49 6e 21 3d 7a 45 6e 64 20 29 7b 0a 20  ( zIn!=zEnd ){. 
21c70 20 20 20 68 20 3d 20 28 68 3c 3c 38 29 20 2b 20     h = (h<<8) + 
21c71 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
21c72 77 65 72 5b 2a 7a 49 6e 5d 3b 0a 20 20 20 20 7a  wer[*zIn];.    z
21c73 49 6e 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3d  In++;.    if( h=
21c74 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 68 27  =(('c'<<24)+('h'
21c75 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27  <<16)+('a'<<8)+'
21c76 72 27 29 20 29 7b 20 20 20 20 20 20 20 20 20 20  r') ){          
21c77 20 20 20 2f 2a 20 43 48 41 52 20 2a 2f 0a 20 20     /* CHAR */.  
21c78 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
21c79 5f 41 46 46 5f 54 45 58 54 3b 20 0a 20 20 20 20  _AFF_TEXT; .    
21c7a 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
21c7b 63 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36  c'<<24)+('l'<<16
21c7c 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20  )+('o'<<8)+'b') 
21c7d 29 7b 20 20 20 20 20 20 20 2f 2a 20 43 4c 4f 42  ){       /* CLOB
21c7e 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20   */.      aff = 
21c7f 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b  SQLITE_AFF_TEXT;
21c80 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68  .    }else if( h
21c81 3d 3d 28 28 27 74 27 3c 3c 32 34 29 2b 28 27 65  ==(('t'<<24)+('e
21c82 27 3c 3c 31 36 29 2b 28 27 78 27 3c 3c 38 29 2b  '<<16)+('x'<<8)+
21c83 27 74 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a  't') ){       /*
21c84 20 54 45 58 54 20 2a 2f 0a 20 20 20 20 20 20 61   TEXT */.      a
21c85 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
21c86 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20  TEXT;.    }else 
21c87 69 66 28 20 68 3d 3d 28 28 27 62 27 3c 3c 32 34  if( h==(('b'<<24
21c88 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27  )+('l'<<16)+('o'
21c89 3c 3c 38 29 2b 27 62 27 29 20 20 20 20 20 20 20  <<8)+'b')       
21c8a 20 20 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20     /* BLOB */.  
21c8b 20 20 20 20 20 20 26 26 20 28 61 66 66 3d 3d 53        && (aff==S
21c8c 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
21c8d 43 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45  C || aff==SQLITE
21c8e 5f 41 46 46 5f 52 45 41 4c 29 20 29 7b 0a 20 20  _AFF_REAL) ){.  
21c8f 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
21c90 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64  _AFF_NONE;.#ifnd
21c91 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
21c92 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
21c93 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28    }else if( h==(
21c94 28 27 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c  ('r'<<24)+('e'<<
21c95 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c 27  16)+('a'<<8)+'l'
21c96 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45  )          /* RE
21c97 41 4c 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26  AL */.        &&
21c98 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46   aff==SQLITE_AFF
21c99 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20  _NUMERIC ){.    
21c9a 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
21c9b 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c  FF_REAL;.    }el
21c9c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 66 27 3c  se if( h==(('f'<
21c9d 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28  <24)+('l'<<16)+(
21c9e 27 6f 27 3c 3c 38 29 2b 27 61 27 29 20 20 20 20  'o'<<8)+'a')    
21c9f 20 20 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f        /* FLOA */
21ca0 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d  .        && aff=
21ca1 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
21ca2 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66  RIC ){.      aff
21ca3 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45   = SQLITE_AFF_RE
21ca4 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  AL;.    }else if
21ca5 28 20 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29 2b  ( h==(('d'<<24)+
21ca6 28 27 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c 3c  ('o'<<16)+('u'<<
21ca7 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20 20  8)+'b')         
21ca8 20 2f 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20 20   /* DOUB */.    
21ca9 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49      && aff==SQLI
21caa 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29  TE_AFF_NUMERIC )
21cab 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  {.      aff = SQ
21cac 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 23  LITE_AFF_REAL;.#
21cad 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20  endif.    }else 
21cae 69 66 28 20 28 68 26 30 78 30 30 46 46 46 46 46  if( (h&0x00FFFFF
21caf 46 29 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b 28  F)==(('i'<<16)+(
21cb0 27 6e 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20  'n'<<8)+'t') ){ 
21cb1 20 20 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20 20     /* INT */.   
21cb2 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
21cb3 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
21cb4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21cb5 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 66    }..  return af
21cb6 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  f;.}../*.** This
21cb7 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
21cb8 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
21cb9 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69   while in the mi
21cba 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69  ddle of.** parsi
21cbb 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  ng a CREATE TABL
21cbc 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  E statement.  Th
21cbd 65 20 70 46 69 72 73 74 20 74 6f 6b 65 6e 20 69  e pFirst token i
21cbe 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74  s the first.** t
21cbf 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 65 71 75  oken in the sequ
21cc0 65 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 74  ence of tokens t
21cc1 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65  hat describe the
21cc2 20 74 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a 20   type of the.** 
21cc3 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79  column currently
21cc4 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
21cc5 69 6f 6e 2e 20 20 20 70 4c 61 73 74 20 69 73 20  ion.   pLast is 
21cc6 74 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a  the last token.*
21cc7 2a 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63  * in the sequenc
21cc8 65 2e 20 20 55 73 65 20 74 68 69 73 20 69 6e 66  e.  Use this inf
21cc9 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 73  ormation to cons
21cca 74 72 75 63 74 20 61 20 73 74 72 69 6e 67 0a 2a  truct a string.*
21ccb 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  * that contains 
21ccc 74 68 65 20 74 79 70 65 6e 61 6d 65 20 6f 66 20  the typename of 
21ccd 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 73  the column and s
21cce 74 6f 72 65 20 74 68 61 74 20 73 74 72 69 6e 67  tore that string
21ccf 0a 2a 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f  .** in zType..*/
21cd0 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
21cd1 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
21cd2 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65  ColumnType(Parse
21cd3 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
21cd4 2a 70 54 79 70 65 29 7b 0a 20 20 54 61 62 6c 65  *pType){.  Table
21cd5 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20   *p;.  int i;.  
21cd6 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20  Column *pCol;.  
21cd7 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20  sqlite3 *db;..  
21cd8 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d  if( (p = pParse-
21cd9 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29  >pNewTable)==0 )
21cda 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70   return;.  i = p
21cdb 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66 28 20  ->nCol-1;.  if( 
21cdc 69 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  i<0 ) return;.  
21cdd 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b  pCol = &p->aCol[
21cde 69 5d 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73  i];.  db = pPars
21cdf 65 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 33  e->db;.  sqlite3
21ce0 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d  DbFree(db, pCol-
21ce1 3e 7a 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d  >zType);.  pCol-
21ce2 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33  >zType = sqlite3
21ce3 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
21ce4 2c 20 70 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c  , pType);.  pCol
21ce5 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c  ->affinity = sql
21ce6 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
21ce7 28 70 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (pType);.}../*.*
21ce8 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
21ce9 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20   is the default 
21cea 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6d 6f  value for the mo
21ceb 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65  st recently adde
21cec 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74  d column.** of t
21ced 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74  he table current
21cee 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
21cef 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66  ction..**.** Def
21cf0 61 75 6c 74 20 76 61 6c 75 65 20 65 78 70 72 65  ault value expre
21cf1 73 73 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 63  ssions must be c
21cf2 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69 73 65 20  onstant.  Raise 
21cf3 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66 20  an exception if 
21cf4 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f 74 20 74  this.** is not t
21cf5 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  he case..**.** T
21cf6 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
21cf7 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
21cf8 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65  ser while in the
21cf9 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61   middle of.** pa
21cfa 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54  rsing a CREATE T
21cfb 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
21cfc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21cfd 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
21cfe 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 50 61  dDefaultValue(Pa
21cff 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
21d00 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 54 61 62  r *pExpr){.  Tab
21d01 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20  le *p;.  Column 
21d02 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33  *pCol;.  sqlite3
21d03 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
21d04 62 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50  b;.  if( (p = pP
21d05 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29  arse->pNewTable)
21d06 21 3d 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 20  !=0 ){.    pCol 
21d07 3d 20 26 28 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e  = &(p->aCol[p->n
21d08 43 6f 6c 2d 31 5d 29 3b 0a 20 20 20 20 69 66 28  Col-1]);.    if(
21d09 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43   !sqlite3ExprIsC
21d0a 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f  onstantOrFunctio
21d0b 6e 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  n(pExpr) ){.    
21d0c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
21d0d 67 28 70 50 61 72 73 65 2c 20 22 64 65 66 61 75  g(pParse, "defau
21d0e 6c 74 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75  lt value of colu
21d0f 6d 6e 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63  mn [%s] is not c
21d10 6f 6e 73 74 61 6e 74 22 2c 0a 20 20 20 20 20 20  onstant",.      
21d11 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29      pCol->zName)
21d12 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
21d13 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 20     /* A copy of 
21d14 70 45 78 70 72 20 69 73 20 75 73 65 64 20 69 6e  pExpr is used in
21d15 73 74 65 61 64 20 6f 66 20 74 68 65 20 6f 72 69  stead of the ori
21d16 67 69 6e 61 6c 2c 20 61 73 20 70 45 78 70 72 20  ginal, as pExpr 
21d17 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a  contains.      *
21d18 2a 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 70 6f  * tokens that po
21d19 69 6e 74 20 74 6f 20 76 6f 6c 61 74 69 6c 65 20  int to volatile 
21d1a 6d 65 6d 6f 72 79 2e 20 54 68 65 20 27 73 70 61  memory. The 'spa
21d1b 6e 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  n' of the expres
21d1c 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73  sion.      ** is
21d1d 20 72 65 71 75 69 72 65 64 20 62 79 20 70 72 61   required by pra
21d1e 67 6d 61 20 74 61 62 6c 65 5f 69 6e 66 6f 2e 0a  gma table_info..
21d1f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
21d20 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
21d21 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  (db, pCol->pDflt
21d22 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70  );.      pCol->p
21d23 44 66 6c 74 20 3d 20 73 71 6c 69 74 65 33 45 78  Dflt = sqlite3Ex
21d24 70 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2c  prDup(db, pExpr,
21d25 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 7c   EXPRDUP_REDUCE|
21d26 45 58 50 52 44 55 50 5f 53 50 41 4e 29 3b 0a 20  EXPRDUP_SPAN);. 
21d27 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
21d28 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
21d29 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   pExpr);.}../*.*
21d2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74 68 65 20  * Designate the 
21d2b 50 52 49 4d 41 52 59 20 4b 45 59 20 66 6f 72 20  PRIMARY KEY for 
21d2c 74 68 65 20 74 61 62 6c 65 2e 20 20 70 4c 69 73  the table.  pLis
21d2d 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6e  t is a list of n
21d2e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c 75  ames .** of colu
21d2f 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68  mns that form th
21d30 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20  e primary key.  
21d31 49 66 20 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c  If pList is NULL
21d32 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 6f  , then the.** mo
21d33 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65  st recently adde
21d34 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  d column of the 
21d35 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 72 69  table is the pri
21d36 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20  mary key..**.** 
21d37 41 20 74 61 62 6c 65 20 63 61 6e 20 68 61 76 65  A table can have
21d38 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 70 72 69   at most one pri
21d39 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20 74 68  mary key.  If th
21d3a 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  e table already 
21d3b 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d 61 72 79  has.** a primary
21d3c 20 6b 65 79 20 28 61 6e 64 20 74 68 69 73 20 69   key (and this i
21d3d 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 69  s the second pri
21d3e 6d 61 72 79 20 6b 65 79 29 20 74 68 65 6e 20 63  mary key) then c
21d3f 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65 72 72 6f  reate an.** erro
21d40 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
21d41 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 6f  PRIMARY KEY is o
21d42 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  n a single colum
21d43 6e 20 77 68 6f 73 65 20 64 61 74 61 74 79 70 65  n whose datatype
21d44 20 69 73 20 49 4e 54 45 47 45 52 2c 0a 2a 2a 20   is INTEGER,.** 
21d45 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 74 72 79  then we will try
21d46 20 74 6f 20 75 73 65 20 74 68 61 74 20 63 6f 6c   to use that col
21d47 75 6d 6e 20 61 73 20 74 68 65 20 72 6f 77 69 64  umn as the rowid
21d48 2e 20 20 53 65 74 20 74 68 65 20 54 61 62 6c 65  .  Set the Table
21d49 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65 6c 64 20  .iPKey.** field 
21d4a 6f 66 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64  of the table und
21d4b 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
21d4c 74 6f 20 62 65 20 74 68 65 20 69 6e 64 65 78 20  to be the index 
21d4d 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54 45 47 45  of the.** INTEGE
21d4e 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  R PRIMARY KEY co
21d4f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e 69 50 4b  lumn.  Table.iPK
21d50 65 79 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20  ey is set to -1 
21d51 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e  if there is.** n
21d52 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  o INTEGER PRIMAR
21d53 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  Y KEY..**.** If 
21d54 74 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 61  the key is not a
21d55 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  n INTEGER PRIMAR
21d56 59 20 4b 45 59 2c 20 74 68 65 6e 20 63 72 65 61  Y KEY, then crea
21d57 74 65 20 61 20 75 6e 69 71 75 65 0a 2a 2a 20 69  te a unique.** i
21d58 6e 64 65 78 20 66 6f 72 20 74 68 65 20 6b 65 79  ndex for the key
21d59 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69 73 20 63  .  No index is c
21d5a 72 65 61 74 65 64 20 66 6f 72 20 49 4e 54 45 47  reated for INTEG
21d5b 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 73 2e  ER PRIMARY KEYs.
21d5c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
21d5d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
21d5e 64 64 50 72 69 6d 61 72 79 4b 65 79 28 0a 20 20  ddPrimaryKey(.  
21d5f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
21d60 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
21d61 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
21d62 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a 20 4c  st *pList,  /* L
21d63 69 73 74 20 6f 66 20 66 69 65 6c 64 20 6e 61 6d  ist of field nam
21d64 65 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  es to be indexed
21d65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f   */.  int onErro
21d66 72 2c 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20  r,      /* What 
21d67 74 6f 20 64 6f 20 77 69 74 68 20 61 20 75 6e 69  to do with a uni
21d68 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74  queness conflict
21d69 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74 6f 49 6e   */.  int autoIn
21d6a 63 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  c,      /* True 
21d6b 69 66 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45  if the AUTOINCRE
21d6c 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20 69 73 20  MENT keyword is 
21d6d 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  present */.  int
21d6e 20 73 6f 72 74 4f 72 64 65 72 20 20 20 20 20 2f   sortOrder     /
21d6f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20  * SQLITE_SO_ASC 
21d70 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53  or SQLITE_SO_DES
21d71 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20  C */.){.  Table 
21d72 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  *pTab = pParse->
21d73 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 63 68 61  pNewTable;.  cha
21d74 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20  r *zType = 0;.  
21d75 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 2c 20 69  int iCol = -1, i
21d76 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
21d77 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  || IN_DECLARE_VT
21d78 41 42 20 29 20 67 6f 74 6f 20 70 72 69 6d 61 72  AB ) goto primar
21d79 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 69 66  y_key_exit;.  if
21d7a 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73  ( pTab->tabFlags
21d7b 20 26 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79   & TF_HasPrimary
21d7c 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Key ){.    sqlit
21d7d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
21d7e 65 2c 20 0a 20 20 20 20 20 20 22 74 61 62 6c 65  e, .      "table
21d7f 20 5c 22 25 73 5c 22 20 68 61 73 20 6d 6f 72 65   \"%s\" has more
21d80 20 74 68 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72   than one primar
21d81 79 20 6b 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e  y key", pTab->zN
21d82 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 70  ame);.    goto p
21d83 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b  rimary_key_exit;
21d84 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e 74 61 62  .  }.  pTab->tab
21d85 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61 73 50  Flags |= TF_HasP
21d86 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20 69 66 28  rimaryKey;.  if(
21d87 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20   pList==0 ){.   
21d88 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43   iCol = pTab->nC
21d89 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70 54 61 62  ol - 1;.    pTab
21d8a 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50  ->aCol[iCol].isP
21d8b 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 7d 65  rimKey = 1;.  }e
21d8c 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  lse{.    for(i=0
21d8d 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ; i<pList->nExpr
21d8e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f  ; i++){.      fo
21d8f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70  r(iCol=0; iCol<p
21d90 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b  Tab->nCol; iCol+
21d91 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
21d92 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
21d93 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
21d94 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  , pTab->aCol[iCo
21d95 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  l].zName)==0 ){.
21d96 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
21d97 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
21d98 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f   }.      if( iCo
21d99 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a  l<pTab->nCol ){.
21d9a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43          pTab->aC
21d9b 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b  ol[iCol].isPrimK
21d9c 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ey = 1;.      }.
21d9d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c      }.    if( pL
21d9e 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20 29 20 69  ist->nExpr>1 ) i
21d9f 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20  Col = -1;.  }.  
21da0 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69  if( iCol>=0 && i
21da1 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  Col<pTab->nCol )
21da2 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54  {.    zType = pT
21da3 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
21da4 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Type;.  }.  if( 
21da5 7a 54 79 70 65 20 26 26 20 73 71 6c 69 74 65 33  zType && sqlite3
21da6 53 74 72 49 43 6d 70 28 7a 54 79 70 65 2c 20 22  StrICmp(zType, "
21da7 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a 20 20 20  INTEGER")==0.   
21da8 20 20 20 20 20 26 26 20 73 6f 72 74 4f 72 64 65       && sortOrde
21da9 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43  r==SQLITE_SO_ASC
21daa 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 69 50   ){.    pTab->iP
21dab 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20  Key = iCol;.    
21dac 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d 20  pTab->keyConf = 
21dad 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 20  (u8)onError;.   
21dae 20 61 73 73 65 72 74 28 20 61 75 74 6f 49 6e 63   assert( autoInc
21daf 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63 3d 3d  ==0 || autoInc==
21db0 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d 3e 74  1 );.    pTab->t
21db1 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74 6f 49  abFlags |= autoI
21db2 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d  nc*TF_Autoincrem
21db3 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ent;.  }else if(
21db4 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69 66 6e   autoInc ){.#ifn
21db5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
21db6 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
21db7 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
21db8 67 28 70 50 61 72 73 65 2c 20 22 41 55 54 4f 49  g(pParse, "AUTOI
21db9 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e 6c 79  NCREMENT is only
21dba 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e 20 22   allowed on an "
21dbb 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47 45 52  .       "INTEGER
21dbc 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29 3b 0a   PRIMARY KEY");.
21dbd 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a  #endif.  }else{.
21dbe 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74      sqlite3Creat
21dbf 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 30  eIndex(pParse, 0
21dc0 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c 20 6f  , 0, 0, pList, o
21dc1 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20 73 6f  nError, 0, 0, so
21dc2 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20 20 20  rtOrder, 0);.   
21dc3 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d 0a   pList = 0;.  }.
21dc4 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69  .primary_key_exi
21dc5 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  t:.  sqlite3Expr
21dc6 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
21dc7 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  e->db, pList);. 
21dc8 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
21dc9 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 45 43  * Add a new CHEC
21dca 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20  K constraint to 
21dcb 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e  the table curren
21dcc 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
21dcd 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  uction..*/.SQLIT
21dce 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21dcf 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f  qlite3AddCheckCo
21dd0 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72 73  nstraint(.  Pars
21dd1 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a  e *pParse,    /*
21dd2 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
21dd3 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 68 65   */.  Expr *pChe
21dd4 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20 63  ckExpr  /* The c
21dd5 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e 20  heck expression 
21dd6 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
21dd7 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
21dd8 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
21dd9 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54 61  _OMIT_CHECK.  Ta
21dda 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72  ble *pTab = pPar
21ddb 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
21ddc 20 69 66 28 20 70 54 61 62 20 26 26 20 21 49 4e   if( pTab && !IN
21ddd 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b  _DECLARE_VTAB ){
21dde 0a 20 20 20 20 2f 2a 20 54 68 65 20 43 48 45 43  .    /* The CHEC
21ddf 4b 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73  K expression mus
21de0 74 20 62 65 20 64 75 70 6c 69 63 61 74 65 64 20  t be duplicated 
21de1 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e 73 20 72  so that tokens r
21de2 65 66 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20 6d  efer.    ** to m
21de3 61 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 61 6e  alloced space an
21de4 64 20 6e 6f 74 20 74 68 65 20 28 65 70 68 65 6d  d not the (ephem
21de5 65 72 61 6c 29 20 74 65 78 74 20 6f 66 20 74 68  eral) text of th
21de6 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a 20  e CREATE TABLE. 
21de7 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20     ** statement 
21de8 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e 70 43 68  */.    pTab->pCh
21de9 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78 70  eck = sqlite3Exp
21dea 72 41 6e 64 28 64 62 2c 20 70 54 61 62 2d 3e 70  rAnd(db, pTab->p
21deb 43 68 65 63 6b 2c 20 0a 20 20 20 20 20 20 20 20  Check, .        
21dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ded 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
21dee 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 43 68  3ExprDup(db, pCh
21def 65 63 6b 45 78 70 72 2c 20 30 29 29 3b 0a 20 20  eckExpr, 0));.  
21df0 7d 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  }.#endif.  sqlit
21df1 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
21df2 20 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 7d 0a   pCheckExpr);.}.
21df3 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63  ./*.** Set the c
21df4 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  ollation functio
21df5 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  n of the most re
21df6 63 65 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61  cently parsed ta
21df7 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f  ble column.** to
21df8 20 74 68 65 20 43 6f 6c 6c 53 65 71 20 67 69 76   the CollSeq giv
21df9 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  en..*/.SQLITE_PR
21dfa 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21dfb 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65  e3AddCollateType
21dfc 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21dfd 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a  Token *pToken){.
21dfe 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e    Table *p;.  in
21dff 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f  t i;.  char *zCo
21e00 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
21e01 20 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d   /* Dequoted nam
21e02 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  e of collation s
21e03 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c  equence */.  sql
21e04 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28  ite3 *db;..  if(
21e05 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e   (p = pParse->pN
21e06 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65  ewTable)==0 ) re
21e07 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e  turn;.  i = p->n
21e08 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50  Col-1;.  db = pP
21e09 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c  arse->db;.  zCol
21e0a 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  l = sqlite3NameF
21e0b 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f  romToken(db, pTo
21e0c 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f  ken);.  if( !zCo
21e0d 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  ll ) return;..  
21e0e 69 66 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  if( sqlite3Locat
21e0f 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
21e10 20 7a 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20   zColl, -1) ){. 
21e11 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
21e12 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a      p->aCol[i].z
21e13 43 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20  Coll = zColl;.  
21e14 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63  .    /* If the c
21e15 6f 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65  olumn is declare
21e16 64 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49  d as "<name> PRI
21e17 4d 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45  MARY KEY COLLATE
21e18 20 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a   <type>",.    **
21e19 20 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d   then an index m
21e1a 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65  ay have been cre
21e1b 61 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c  ated on this col
21e1c 75 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20  umn before the. 
21e1d 20 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20     ** collation 
21e1e 74 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20  type was added. 
21e1f 43 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20  Correct this if 
21e20 69 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a  it is the case..
21e21 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70      */.    for(p
21e22 49 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70  Idx=p->pIndex; p
21e23 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
21e24 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73  pNext){.      as
21e25 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c  sert( pIdx->nCol
21e26 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
21e27 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  if( pIdx->aiColu
21e28 6d 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20  mn[0]==i ){.    
21e29 20 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c      pIdx->azColl
21e2a 5b 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d  [0] = p->aCol[i]
21e2b 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  .zColl;.      }.
21e2c 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
21e2d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
21e2e 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d  (db, zColl);.  }
21e2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
21e30 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
21e31 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
21e32 71 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62  quence for datab
21e33 61 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a  ase native text.
21e34 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e  ** encoding iden
21e35 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74  tified by the st
21e36 72 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67  ring zName, leng
21e37 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  th nName..**.** 
21e38 49 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  If the requested
21e39 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
21e3a 6e 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  nce is not avail
21e3b 61 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61  able, or not ava
21e3c 69 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65  ilable.** in the
21e3d 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65   database native
21e3e 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63   encoding, the c
21e3f 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79  ollation factory
21e40 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a   is invoked to.*
21e41 2a 20 72 65 71 75 65 73 74 20 69 74 2e 20 49 66  * request it. If
21e42 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
21e43 61 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20  actory does not 
21e44 73 75 70 70 6c 79 20 73 75 63 68 20 61 20 73 65  supply such a se
21e45 71 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74  quence,.** and t
21e46 68 65 20 73 65 71 75 65 6e 63 65 20 69 73 20 61  he sequence is a
21e47 76 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74  vailable in anot
21e48 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  her text encodin
21e49 67 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a  g, then that is.
21e4a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74  ** returned inst
21e4b 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  ead..**.** If no
21e4c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
21e4d 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61   requested colla
21e4e 74 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61  tions sequence a
21e4f 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72  re available, or
21e50 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f  .** another erro
21e51 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69  r occurs, NULL i
21e52 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61  s returned and a
21e53 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
21e54 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20  written into.** 
21e55 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  pParse..**.** Th
21e56 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
21e57 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73  wrapper around s
21e58 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
21e59 71 28 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69  q().  This routi
21e5a 6e 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68  ne.** invokes th
21e5b 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74  e collation fact
21e5c 6f 72 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64  ory if the named
21e5d 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f   collation canno
21e5e 74 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e  t be found.** an
21e5f 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65  d generates an e
21e60 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f  rror message..*/
21e61 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21e62 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
21e63 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61  LocateCollSeq(Pa
21e64 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
21e65 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
21e66 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 73 71  int nName){.  sq
21e67 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
21e68 73 65 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63  se->db;.  u8 enc
21e69 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38   = ENC(db);.  u8
21e6a 20 69 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e   initbusy = db->
21e6b 69 6e 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c  init.busy;.  Col
21e6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20  lSeq *pColl;..  
21e6d 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  pColl = sqlite3F
21e6e 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65  indCollSeq(db, e
21e6f 6e 63 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  nc, zName, nName
21e70 2c 20 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69  , initbusy);.  i
21e71 66 28 20 21 69 6e 69 74 62 75 73 79 20 26 26 20  f( !initbusy && 
21e72 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c  (!pColl || !pCol
21e73 6c 2d 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20  l->xCmp) ){.    
21e74 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
21e75 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70 43  etCollSeq(db, pC
21e76 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  oll, zName, nNam
21e77 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f  e);.    if( !pCo
21e78 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
21e79 6e 4e 61 6d 65 3c 30 20 29 7b 0a 20 20 20 20 20  nName<0 ){.     
21e7a 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74     nName = sqlit
21e7b 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
21e7c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21e7d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
21e7e 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
21e7f 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  h collation sequ
21e80 65 6e 63 65 3a 20 25 2e 2a 73 22 2c 20 6e 4e 61  ence: %.*s", nNa
21e81 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  me, zName);.    
21e82 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20    pColl = 0;.   
21e83 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
21e84 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a   pColl;.}.../*.*
21e85 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
21e86 74 68 61 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d  that will increm
21e87 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63  ent the schema c
21e88 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ookie..**.** The
21e89 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69   schema cookie i
21e8a 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
21e8b 69 6e 65 20 77 68 65 6e 20 74 68 65 20 73 63 68  ine when the sch
21e8c 65 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64  ema for the.** d
21e8d 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e  atabase changes.
21e8e 20 20 41 66 74 65 72 20 65 61 63 68 20 73 63 68    After each sch
21e8f 65 6d 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20  ema change, the 
21e90 63 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20  cookie value.** 
21e91 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61  changes.  When a
21e92 20 70 72 6f 63 65 73 73 20 66 69 72 73 74 20 72   process first r
21e93 65 61 64 73 20 74 68 65 20 73 63 68 65 6d 61 20  eads the schema 
21e94 69 74 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a  it records the.*
21e95 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65  * cookie.  There
21e96 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65 72 20  after, whenever 
21e97 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63 65 73  it goes to acces
21e98 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a  s the database,.
21e99 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74 68 65  ** it checks the
21e9a 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20   cookie to make 
21e9b 73 75 72 65 20 74 68 65 20 73 63 68 65 6d 61 20  sure the schema 
21e9c 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a  has not changed.
21e9d 2a 2a 20 73 69 6e 63 65 20 69 74 20 77 61 73 20  ** since it was 
21e9e 6c 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  last read..**.**
21e9f 20 54 68 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f   This plan is no
21ea0 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c  t completely bul
21ea1 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69  let-proof.  It i
21ea2 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a  s possible for.*
21ea3 2a 20 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20  * the schema to 
21ea4 63 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20  change multiple 
21ea5 74 69 6d 65 73 20 61 6e 64 20 66 6f 72 20 74 68  times and for th
21ea6 65 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a  e cookie to be.*
21ea7 2a 20 73 65 74 20 62 61 63 6b 20 74 6f 20 70 72  * set back to pr
21ea8 69 6f 72 20 76 61 6c 75 65 2e 20 20 42 75 74 20  ior value.  But 
21ea9 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61  schema changes a
21eaa 72 65 20 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a  re infrequent.**
21eab 20 61 6e 64 20 74 68 65 20 70 72 6f 62 61 62 69   and the probabi
21eac 6c 69 74 79 20 6f 66 20 68 69 74 74 69 6e 67 20  lity of hitting 
21ead 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20  the same cookie 
21eae 76 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a  value is only.**
21eaf 20 31 20 63 68 61 6e 63 65 20 69 6e 20 32 5e 33   1 chance in 2^3
21eb0 32 2e 20 20 53 6f 20 77 65 27 72 65 20 73 61 66  2.  So we're saf
21eb1 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c  e enough..*/.SQL
21eb2 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21eb3 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
21eb4 6f 6b 69 65 28 50 61 72 73 65 20 2a 70 50 61 72  okie(Parse *pPar
21eb5 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  se, int iDb){.  
21eb6 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
21eb7 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
21eb8 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e);.  sqlite3 *d
21eb9 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
21eba 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
21ebb 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c  se->pVdbe;.  sql
21ebc 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21ebd 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 64 62  , OP_Integer, db
21ebe 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
21ebf 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
21ec0 65 2b 31 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69  e+1, r1);.  sqli
21ec1 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
21ec2 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69   OP_SetCookie, i
21ec3 44 62 2c 20 30 2c 20 72 31 29 3b 0a 20 20 73 71  Db, 0, r1);.  sq
21ec4 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
21ec5 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
21ec6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 75 72  .}../*.** Measur
21ec7 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
21ec8 63 68 61 72 61 63 74 65 72 73 20 6e 65 65 64 65  characters neede
21ec9 64 20 74 6f 20 6f 75 74 70 75 74 20 74 68 65 20  d to output the 
21eca 67 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 69 66  given.** identif
21ecb 69 65 72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  ier.  The number
21ecc 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64   returned includ
21ecd 65 73 20 61 6e 79 20 71 75 6f 74 65 73 20 75 73  es any quotes us
21ece 65 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73 20 6e  ed.** but does n
21ecf 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e  ot include the n
21ed0 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a  ull terminator..
21ed1 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61  **.** The estima
21ed2 74 65 20 69 73 20 63 6f 6e 73 65 72 76 61 74 69  te is conservati
21ed3 76 65 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65  ve.  It might be
21ed4 20 6c 61 72 67 65 72 20 74 68 61 74 20 77 68 61   larger that wha
21ed5 74 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e  t is.** really n
21ed6 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  eeded..*/.static
21ed7 20 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68   int identLength
21ed8 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
21ed9 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28  .  int n;.  for(
21eda 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b  n=0; *z; n++, z+
21edb 2b 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d  +){.    if( *z==
21edc 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a 20 20  '"' ){ n++; }.  
21edd 7d 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 32  }.  return n + 2
21ede 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
21edf 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72  function is a wr
21ee0 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c  apper around sql
21ee1 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 29 20 75  ite3GetToken() u
21ee2 73 65 64 20 62 79 20 0a 2a 2a 20 69 73 56 61 6c  sed by .** isVal
21ee3 69 64 44 69 6d 65 6e 73 69 6f 6e 28 29 2e 20 54  idDimension(). T
21ee4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 66  his function dif
21ee5 66 65 72 73 20 66 72 6f 6d 20 73 71 6c 69 74 65  fers from sqlite
21ee6 33 47 65 74 54 6f 6b 65 6e 28 29 20 69 6e 0a 2a  3GetToken() in.*
21ee7 2a 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  * that:.**.**   
21ee8 2a 20 57 68 69 74 65 73 70 61 63 65 20 69 73 20  * Whitespace is 
21ee9 69 67 6e 6f 72 65 64 2c 20 61 6e 64 0a 2a 2a 20  ignored, and.** 
21eea 20 20 2a 20 54 68 65 20 6f 75 74 70 75 74 20 76    * The output v
21eeb 61 72 69 61 62 6c 65 20 2a 70 65 54 6f 6b 65 6e  ariable *peToken
21eec 20 69 73 20 73 65 74 20 74 6f 20 30 20 69 66 20   is set to 0 if 
21eed 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a  the end of the.*
21eee 2a 20 20 20 20 20 6e 75 6c 2d 74 65 72 6d 69 6e  *     nul-termin
21eef 61 74 65 64 20 69 6e 70 75 74 20 73 74 72 69 6e  ated input strin
21ef0 67 20 69 73 20 72 65 61 63 68 65 64 2e 0a 2a 2f  g is reached..*/
21ef1 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54  .static int getT
21ef2 6f 6b 65 6e 4e 6f 53 70 61 63 65 28 75 6e 73 69  okenNoSpace(unsi
21ef3 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e  gned char *z, in
21ef4 74 20 2a 70 65 54 6f 6b 65 6e 29 7b 0a 20 20 69  t *peToken){.  i
21ef5 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 77 68 69 6c  nt n = 0;.  whil
21ef6 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
21ef7 65 28 7a 5b 6e 5d 29 20 29 20 6e 2b 2b 3b 0a 20  e(z[n]) ) n++;. 
21ef8 20 69 66 28 20 21 7a 5b 6e 5d 20 29 7b 0a 20 20   if( !z[n] ){.  
21ef9 20 20 2a 70 65 54 6f 6b 65 6e 20 3d 20 30 3b 0a    *peToken = 0;.
21efa 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
21efb 7d 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 73  }.  return n + s
21efc 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 26  qlite3GetToken(&
21efd 7a 5b 6e 5d 2c 20 70 65 54 6f 6b 65 6e 29 3b 0a  z[n], peToken);.
21efe 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  }../*.** Paramet
21eff 65 72 20 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61  er z points to a
21f00 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
21f01 73 74 72 69 6e 67 2e 20 52 65 74 75 72 6e 20 74  string. Return t
21f02 72 75 65 20 69 66 2c 20 77 68 65 6e 0a 2a 2a 20  rue if, when.** 
21f03 77 68 69 74 65 73 70 61 63 65 20 69 73 20 69 67  whitespace is ig
21f04 6e 6f 72 65 64 2c 20 74 68 65 20 63 6f 6e 74 65  nored, the conte
21f05 6e 74 73 20 6f 66 20 74 68 69 73 20 73 74 72 69  nts of this stri
21f06 6e 67 20 6d 61 74 63 68 65 73 20 6f 6e 65 20 6f  ng matches one o
21f07 66 20 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77  f .** the follow
21f08 69 6e 67 20 70 61 74 74 65 72 6e 73 3a 0a 2a 2a  ing patterns:.**
21f09 0a 2a 2a 20 20 20 20 20 22 22 0a 2a 2a 20 20 20  .**     "".**   
21f0a 20 20 22 28 6e 75 6d 62 65 72 29 22 0a 2a 2a 20    "(number)".** 
21f0b 20 20 20 20 22 28 6e 75 6d 62 65 72 2c 6e 75 6d      "(number,num
21f0c 62 65 72 29 22 0a 2a 2f 0a 73 74 61 74 69 63 20  ber)".*/.static 
21f0d 69 6e 74 20 69 73 56 61 6c 69 64 44 69 6d 65 6e  int isValidDimen
21f0e 73 69 6f 6e 28 75 6e 73 69 67 6e 65 64 20 63 68  sion(unsigned ch
21f0f 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 65 54  ar *z){.  int eT
21f10 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  oken;.  int n = 
21f11 30 3b 0a 20 20 6e 20 2b 3d 20 67 65 74 54 6f 6b  0;.  n += getTok
21f12 65 6e 4e 6f 53 70 61 63 65 28 26 7a 5b 6e 5d 2c  enNoSpace(&z[n],
21f13 20 26 65 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28   &eToken);.  if(
21f14 20 65 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69   eToken ){.    i
21f15 66 28 20 65 54 6f 6b 65 6e 21 3d 54 4b 5f 4c 50  f( eToken!=TK_LP
21f16 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
21f17 20 6e 20 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e 6f   n += getTokenNo
21f18 53 70 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54  Space(&z[n], &eT
21f19 6f 6b 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 65  oken);.    if( e
21f1a 54 6f 6b 65 6e 3d 3d 54 4b 5f 50 4c 55 53 20 7c  Token==TK_PLUS |
21f1b 7c 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f 4d 49 4e  | eToken==TK_MIN
21f1c 55 53 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d  US ){.      n +=
21f1d 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65   getTokenNoSpace
21f1e 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f 6b 65 6e 29  (&z[n], &eToken)
21f1f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
21f20 65 54 6f 6b 65 6e 21 3d 54 4b 5f 49 4e 54 45 47  eToken!=TK_INTEG
21f21 45 52 20 26 26 20 65 54 6f 6b 65 6e 21 3d 54 4b  ER && eToken!=TK
21f22 5f 46 4c 4f 41 54 20 29 20 72 65 74 75 72 6e 20  _FLOAT ) return 
21f23 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 54  0;.    n += getT
21f24 6f 6b 65 6e 4e 6f 53 70 61 63 65 28 26 7a 5b 6e  okenNoSpace(&z[n
21f25 5d 2c 20 26 65 54 6f 6b 65 6e 29 3b 0a 20 20 20  ], &eToken);.   
21f26 20 69 66 28 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f   if( eToken==TK_
21f27 43 4f 4d 4d 41 20 29 7b 0a 20 20 20 20 20 20 6e  COMMA ){.      n
21f28 20 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 70   += getTokenNoSp
21f29 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f 6b  ace(&z[n], &eTok
21f2a 65 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65  en);.      if( e
21f2b 54 6f 6b 65 6e 3d 3d 54 4b 5f 50 4c 55 53 20 7c  Token==TK_PLUS |
21f2c 7c 20 65 54 6f 6b 65 6e 3d 3d 54 4b 5f 4d 49 4e  | eToken==TK_MIN
21f2d 55 53 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 20  US ){.        n 
21f2e 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61  += getTokenNoSpa
21f2f 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f 6b 65  ce(&z[n], &eToke
21f30 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
21f31 20 20 69 66 28 20 65 54 6f 6b 65 6e 21 3d 54 4b    if( eToken!=TK
21f32 5f 49 4e 54 45 47 45 52 20 26 26 20 65 54 6f 6b  _INTEGER && eTok
21f33 65 6e 21 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 72  en!=TK_FLOAT ) r
21f34 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 6e  eturn 0;.      n
21f35 20 2b 3d 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 70   += getTokenNoSp
21f36 61 63 65 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f 6b  ace(&z[n], &eTok
21f37 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  en);.    }.    i
21f38 66 28 20 65 54 6f 6b 65 6e 21 3d 54 4b 5f 52 50  f( eToken!=TK_RP
21f39 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
21f3a 20 67 65 74 54 6f 6b 65 6e 4e 6f 53 70 61 63 65   getTokenNoSpace
21f3b 28 26 7a 5b 6e 5d 2c 20 26 65 54 6f 6b 65 6e 29  (&z[n], &eToken)
21f3c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 65 54 6f 6b  ;.  }.  if( eTok
21f3d 65 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  en ) return 0;. 
21f3e 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
21f3f 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
21f40 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69  rameter is a poi
21f41 6e 74 65 72 20 74 6f 20 61 6e 20 6f 75 74 70 75  nter to an outpu
21f42 74 20 62 75 66 66 65 72 2e 20 54 68 65 20 73 65  t buffer. The se
21f43 63 6f 6e 64 20 0a 2a 2a 20 70 61 72 61 6d 65 74  cond .** paramet
21f44 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  er is a pointer 
21f45 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  to an integer th
21f46 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
21f47 6f 66 66 73 65 74 20 61 74 0a 2a 2a 20 77 68 69  offset at.** whi
21f48 63 68 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f  ch to write into
21f49 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
21f4a 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  er. This functio
21f4b 6e 20 63 6f 70 69 65 73 20 74 68 65 0a 2a 2a 20  n copies the.** 
21f4c 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  nul-terminated s
21f4d 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f  tring pointed to
21f4e 20 62 79 20 74 68 65 20 74 68 69 72 64 20 70 61   by the third pa
21f4f 72 61 6d 65 74 65 72 2c 20 7a 53 69 67 6e 65 64  rameter, zSigned
21f50 49 64 65 6e 74 2c 0a 2a 2a 20 74 6f 20 74 68 65  Ident,.** to the
21f51 20 73 70 65 63 69 66 69 65 64 20 6f 66 66 73 65   specified offse
21f52 74 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20  t in the buffer 
21f53 61 6e 64 20 75 70 64 61 74 65 73 20 2a 70 49 64  and updates *pId
21f54 78 20 74 6f 20 72 65 66 65 72 0a 2a 2a 20 74 6f  x to refer.** to
21f55 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
21f56 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 62  after the last b
21f57 79 74 65 20 77 72 69 74 74 65 6e 20 62 65 66 6f  yte written befo
21f58 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a  re returning..**
21f59 20 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69   .** If the stri
21f5a 6e 67 20 7a 53 69 67 6e 65 64 49 64 65 6e 74 20  ng zSignedIdent 
21f5b 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c  consists entirel
21f5c 79 20 6f 66 20 61 6c 70 68 61 2d 6e 75 6d 65 72  y of alpha-numer
21f5d 69 63 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  ic.** characters
21f5e 2c 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e  , does not begin
21f5f 20 77 69 74 68 20 61 20 64 69 67 69 74 20 61 6e   with a digit an
21f60 64 20 69 73 20 6e 6f 74 20 61 6e 20 53 51 4c 20  d is not an SQL 
21f61 6b 65 79 77 6f 72 64 2c 0a 2a 2a 20 74 68 65 6e  keyword,.** then
21f62 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 6f   it is copied to
21f63 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
21f64 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 69 74  er exactly as it
21f65 20 69 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a   is. Otherwise,.
21f66 2a 2a 20 69 74 20 69 73 20 71 75 6f 74 65 64 20  ** it is quoted 
21f67 75 73 69 6e 67 20 64 6f 75 62 6c 65 2d 71 75 6f  using double-quo
21f68 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tes..*/.static v
21f69 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 68 61  oid identPut(cha
21f6a 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 2c  r *z, int *pIdx,
21f6b 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 64   char *zSignedId
21f6c 65 6e 74 2c 20 69 6e 74 20 69 73 54 79 70 65 6e  ent, int isTypen
21f6d 61 6d 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ame){.  unsigned
21f6e 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20   char *zIdent = 
21f6f 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
21f70 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 20  zSignedIdent;.  
21f71 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75  int i, j, needQu
21f72 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 78  ote;.  i = *pIdx
21f73 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49  ;..  for(j=0; zI
21f74 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20  dent[j]; j++){. 
21f75 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
21f76 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 6a 5d  salnum(zIdent[j]
21f77 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d  ) && zIdent[j]!=
21f78 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  '_' ) break;.  }
21f79 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 73  .  needQuote = s
21f7a 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 49  qlite3Isdigit(zI
21f7b 64 65 6e 74 5b 30 5d 29 20 7c 7c 20 73 71 6c 69  dent[0]) || sqli
21f7c 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 7a  te3KeywordCode(z
21f7d 49 64 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f 49 44  Ident, j)!=TK_ID
21f7e 3b 0a 20 20 69 66 28 20 21 6e 65 65 64 51 75 6f  ;.  if( !needQuo
21f7f 74 65 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73  te ){.    if( is
21f80 54 79 70 65 6e 61 6d 65 20 29 7b 0a 20 20 20 20  Typename ){.    
21f81 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
21f82 61 20 74 79 70 65 2d 6e 61 6d 65 2c 20 61 6c 6c  a type-name, all
21f83 6f 77 20 61 20 6c 69 74 74 6c 65 20 6d 6f 72 65  ow a little more
21f84 20 66 6c 65 78 69 62 69 6c 69 74 79 2e 20 49 6e   flexibility. In
21f85 20 53 51 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a   SQLite,.      *
21f86 2a 20 61 20 74 79 70 65 2d 6e 61 6d 65 20 69 73  * a type-name is
21f87 20 73 70 65 63 69 66 69 65 64 20 61 73 3a 0a 20   specified as:. 
21f88 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
21f89 20 20 20 69 64 73 20 5b 69 64 73 5d 20 5b 28 6e     ids [ids] [(n
21f8a 75 6d 62 65 72 20 5b 2c 20 6e 75 6d 62 65 72 5d  umber [, number]
21f8b 29 5d 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  )].      **.    
21f8c 20 20 2a 2a 20 77 68 65 72 65 20 22 69 64 73 22    ** where "ids"
21f8d 20 69 73 20 65 69 74 68 65 72 20 61 20 71 75 6f   is either a quo
21f8e 74 65 64 20 73 74 72 69 6e 67 20 6f 72 20 61 20  ted string or a 
21f8f 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65  simple identifie
21f90 72 20 28 69 6e 20 74 68 65 0a 20 20 20 20 20 20  r (in the.      
21f91 2a 2a 20 61 62 6f 76 65 20 6e 6f 74 61 74 69 6f  ** above notatio
21f92 6e 2c 20 5b 5d 20 6d 65 61 6e 73 20 6f 70 74 69  n, [] means opti
21f93 6f 6e 61 6c 29 2e 20 49 74 20 69 73 20 61 20 62  onal). It is a b
21f94 69 74 20 74 72 69 63 6b 79 20 74 6f 20 63 68 65  it tricky to che
21f95 63 6b 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20  ck.      ** for 
21f96 61 6c 6c 20 63 61 73 65 73 2c 20 62 75 74 20 69  all cases, but i
21f97 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 61 76 6f  t is good to avo
21f98 69 64 20 75 6e 6e 65 63 65 73 73 61 72 69 6c 79  id unnecessarily
21f99 20 71 75 6f 74 69 6e 67 20 63 6f 6d 6d 6f 6e 0a   quoting common.
21f9a 20 20 20 20 20 20 2a 2a 20 74 79 70 65 6e 61 6d        ** typenam
21f9b 65 73 20 6c 69 6b 65 20 56 41 52 43 48 41 52 28  es like VARCHAR(
21f9c 31 30 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  10)..      */.  
21f9d 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20      needQuote = 
21f9e 21 69 73 56 61 6c 69 64 44 69 6d 65 6e 73 69 6f  !isValidDimensio
21f9f 6e 28 26 7a 49 64 65 6e 74 5b 6a 5d 29 3b 0a 20  n(&zIdent[j]);. 
21fa0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
21fa1 6e 65 65 64 51 75 6f 74 65 20 3d 20 7a 49 64 65  needQuote = zIde
21fa2 6e 74 5b 6a 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  nt[j];.    }.  }
21fa3 0a 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74  ..  if( needQuot
21fa4 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27  e ) z[i++] = '"'
21fa5 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64  ;.  for(j=0; zId
21fa6 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20  ent[j]; j++){.  
21fa7 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e    z[i++] = zIden
21fa8 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20 7a 49  t[j];.    if( zI
21fa9 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a  dent[j]=='"' ) z
21faa 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7d  [i++] = '"';.  }
21fab 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65  .  if( needQuote
21fac 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b   ) z[i++] = '"';
21fad 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a  .  z[i] = 0;.  *
21fae 70 49 64 78 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a  pIdx = i;.}../*.
21faf 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 20 43 52  ** Generate a CR
21fb0 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
21fb1 6d 65 6e 74 20 61 70 70 72 6f 70 72 69 61 74 65  ment appropriate
21fb2 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a   for the given.*
21fb3 2a 20 74 61 62 6c 65 2e 20 20 4d 65 6d 6f 72 79  * table.  Memory
21fb4 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 74 65 78   to hold the tex
21fb5 74 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  t of the stateme
21fb6 6e 74 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a  nt is obtained.*
21fb7 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c  * from sqliteMal
21fb8 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62  loc() and must b
21fb9 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  e freed by the c
21fba 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e  alling function.
21fbb 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
21fbc 2a 63 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74  *createTableStmt
21fbd 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61  (sqlite3 *db, Ta
21fbe 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  ble *p){.  int i
21fbf 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a  , k, n;.  char *
21fc0 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a  zStmt;.  char *z
21fc1 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45  Sep, *zSep2, *zE
21fc2 6e 64 2c 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e  nd, *z;.  Column
21fc3 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b   *pCol;.  n = 0;
21fc4 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d  .  for(pCol = p-
21fc5 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d  >aCol, i=0; i<p-
21fc6 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c  >nCol; i++, pCol
21fc7 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64  ++){.    n += id
21fc8 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e  entLength(pCol->
21fc9 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7a 20 3d 20  zName);.    z = 
21fca 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20 20  pCol->zType;.   
21fcb 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20   if( z ){.      
21fcc 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68  n += identLength
21fcd 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  (z);.    }.  }. 
21fce 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74   n += identLengt
21fcf 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69  h(p->zName);.  i
21fd0 66 28 20 6e 3c 35 30 20 29 7b 0a 20 20 20 20 7a  f( n<50 ){.    z
21fd1 53 65 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a 53  Sep = "";.    zS
21fd2 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20 7a  ep2 = ",";.    z
21fd3 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65 6c  End = ")";.  }el
21fd4 73 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22  se{.    zSep = "
21fd5 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65 70 32  \n  ";.    zSep2
21fd6 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20 20   = ",\n  ";.    
21fd7 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20 20  zEnd = "\n)";.  
21fd8 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36 2a  }.  n += 35 + 6*
21fd9 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74  p->nCol;.  zStmt
21fda 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
21fdb 28 20 6e 20 29 3b 0a 20 20 69 66 28 20 7a 53 74  ( n );.  if( zSt
21fdc 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d  mt==0 ){.    db-
21fdd 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
21fde 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  1;.    return 0;
21fdf 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73  .  }.  sqlite3_s
21fe0 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74 6d 74  nprintf(n, zStmt
21fe1 2c 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20  , "CREATE TABLE 
21fe2 22 29 3b 0a 20 20 6b 20 3d 20 73 71 6c 69 74 65  ");.  k = sqlite
21fe3 33 53 74 72 6c 65 6e 33 30 28 7a 53 74 6d 74 29  3Strlen30(zStmt)
21fe4 3b 0a 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74  ;.  identPut(zSt
21fe5 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61 6d 65  mt, &k, p->zName
21fe6 2c 20 30 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b 2b  , 0);.  zStmt[k+
21fe7 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72 28  +] = '(';.  for(
21fe8 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69 3d  pCol=p->aCol, i=
21fe9 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b  0; i<p->nCol; i+
21fea 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
21feb 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
21fec 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c  (n-k, &zStmt[k],
21fed 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20 2b 3d   zSep);.    k +=
21fee 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
21fef 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20 20 20  (&zStmt[k]);.   
21ff0 20 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b 0a 20   zSep = zSep2;. 
21ff1 20 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d     identPut(zStm
21ff2 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a 4e 61  t, &k, pCol->zNa
21ff3 6d 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  me, 0);.    if( 
21ff4 28 7a 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65  (z = pCol->zType
21ff5 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 53  )!=0 ){.      zS
21ff6 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 20 27 3b 0a  tmt[k++] = ' ';.
21ff7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69        assert( (i
21ff8 6e 74 29 28 73 71 6c 69 74 65 33 53 74 72 6c 65  nt)(sqlite3Strle
21ff9 6e 33 30 28 7a 29 2b 6b 2b 31 29 3c 3d 6e 20 29  n30(z)+k+1)<=n )
21ffa 3b 0a 20 20 20 20 20 20 69 64 65 6e 74 50 75 74  ;.      identPut
21ffb 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 7a 2c 20 31  (zStmt, &k, z, 1
21ffc 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
21ffd 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
21ffe 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20  n-k, &zStmt[k], 
21fff 22 25 73 22 2c 20 7a 45 6e 64 29 3b 0a 20 20 72  "%s", zEnd);.  r
22000 65 74 75 72 6e 20 7a 53 74 6d 74 3b 0a 7d 0a 0a  eturn zStmt;.}..
22001 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
22002 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
22003 72 65 70 6f 72 74 20 74 68 65 20 66 69 6e 61 6c  report the final
22004 20 22 29 22 20 74 68 61 74 20 74 65 72 6d 69 6e   ")" that termin
22005 61 74 65 73 0a 2a 2a 20 61 20 43 52 45 41 54 45  ates.** a CREATE
22006 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
22007 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c  ..**.** The tabl
22008 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  e structure that
22009 20 6f 74 68 65 72 20 61 63 74 69 6f 6e 20 72 6f   other action ro
2200a 75 74 69 6e 65 73 20 68 61 76 65 20 62 65 65 6e  utines have been
2200b 20 62 75 69 6c 64 69 6e 67 0a 2a 2a 20 69 73 20   building.** is 
2200c 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 74  added to the int
2200d 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65  ernal hash table
2200e 73 2c 20 61 73 73 75 6d 69 6e 67 20 6e 6f 20 65  s, assuming no e
2200f 72 72 6f 72 73 20 68 61 76 65 0a 2a 2a 20 6f 63  rrors have.** oc
22010 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e  curred..**.** An
22011 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 74   entry for the t
22012 61 62 6c 65 20 69 73 20 6d 61 64 65 20 69 6e 20  able is made in 
22013 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65  the master table
22014 20 6f 6e 20 64 69 73 6b 2c 20 75 6e 6c 65 73 73   on disk, unless
22015 0a 2a 2a 20 74 68 69 73 20 69 73 20 61 20 74 65  .** this is a te
22016 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 6f 72  mporary table or
22017 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d   db->init.busy==
22018 31 2e 20 20 57 68 65 6e 20 64 62 2d 3e 69 6e 69  1.  When db->ini
22019 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a 20 69 74 20  t.busy==1.** it 
2201a 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 61  means we are rea
2201b 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f  ding the sqlite_
2201c 6d 61 73 74 65 72 20 74 61 62 6c 65 20 62 65 63  master table bec
2201d 61 75 73 65 20 77 65 20 6a 75 73 74 0a 2a 2a 20  ause we just.** 
2201e 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20 74 68 65  connected to the
2201f 20 64 61 74 61 62 61 73 65 20 6f 72 20 62 65 63   database or bec
22020 61 75 73 65 20 74 68 65 20 73 71 6c 69 74 65 5f  ause the sqlite_
22021 6d 61 73 74 65 72 20 74 61 62 6c 65 20 68 61 73  master table has
22022 0a 2a 2a 20 72 65 63 65 6e 74 6c 79 20 63 68 61  .** recently cha
22023 6e 67 65 64 2c 20 73 6f 20 74 68 65 20 65 6e 74  nged, so the ent
22024 72 79 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c  ry for this tabl
22025 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  e already exists
22026 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74   in.** the sqlit
22027 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  e_master table. 
22028 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   We do not want 
22029 74 6f 20 63 72 65 61 74 65 20 69 74 20 61 67 61  to create it aga
2202a 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  in..**.** If the
2202b 20 70 53 65 6c 65 63 74 20 61 72 67 75 6d 65 6e   pSelect argumen
2202c 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69  t is not NULL, i
2202d 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69  t means that thi
2202e 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 61 73  s routine.** was
2202f 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
22030 65 20 61 20 74 61 62 6c 65 20 67 65 6e 65 72 61  e a table genera
22031 74 65 64 20 66 72 6f 6d 20 61 20 0a 2a 2a 20 22  ted from a .** "
22032 43 52 45 41 54 45 20 54 41 42 4c 45 20 2e 2e 2e  CREATE TABLE ...
22033 20 41 53 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20   AS SELECT ..." 
22034 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
22035 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 66 0a  column names of.
22036 2a 2a 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  ** the new table
22037 20 77 69 6c 6c 20 6d 61 74 63 68 20 74 68 65 20   will match the 
22038 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
22039 65 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c  e SELECT..*/.SQL
2203a 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2203b 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
2203c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2203d 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
2203e 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  arse context */.
2203f 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6e 73 2c 20    Token *pCons, 
22040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22041 20 27 2c 27 20 74 6f 6b 65 6e 20 61 66 74 65 72   ',' token after
22042 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e   the last column
22043 20 64 65 66 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65   defn. */.  Toke
22044 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20  n *pEnd,        
22045 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6e 61 6c      /* The final
22046 20 27 29 27 20 74 6f 6b 65 6e 20 69 6e 20 74 68   ')' token in th
22047 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 2a  e CREATE TABLE *
22048 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
22049 65 63 74 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ect         /* S
2204a 65 6c 65 63 74 20 66 72 6f 6d 20 61 20 22 43 52  elect from a "CR
2204b 45 41 54 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45  EATE ... AS SELE
2204c 43 54 22 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  CT" */.){.  Tabl
2204d 65 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 20  e *p;.  sqlite3 
2204e 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2204f 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20  ;.  int iDb;..  
22050 69 66 28 20 28 70 45 6e 64 3d 3d 30 20 26 26 20  if( (pEnd==0 && 
22051 70 53 65 6c 65 63 74 3d 3d 30 29 20 7c 7c 20 70  pSelect==0) || p
22052 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
22053 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22054 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ) {.    return;.
22055 20 20 7d 0a 20 20 70 20 3d 20 70 50 61 72 73 65    }.  p = pParse
22056 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69  ->pNewTable;.  i
22057 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
22058 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  ;..  assert( !db
22059 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 21  ->init.busy || !
2205a 70 53 65 6c 65 63 74 20 29 3b 0a 0a 20 20 69 44  pSelect );..  iD
2205b 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2205c 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e  aToIndex(db, p->
2205d 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64  pSchema);..#ifnd
2205e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
2205f 48 45 43 4b 0a 20 20 2f 2a 20 52 65 73 6f 6c 76  HECK.  /* Resolv
22060 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 43  e names in all C
22061 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20  HECK constraint 
22062 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20 20 2a  expressions..  *
22063 2f 0a 20 20 69 66 28 20 70 2d 3e 70 43 68 65 63  /.  if( p->pChec
22064 6b 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74  k ){.    SrcList
22065 20 73 53 72 63 3b 20 20 20 20 20 20 20 20 20 20   sSrc;          
22066 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6b 65           /* Fake
22067 20 53 72 63 4c 69 73 74 20 66 6f 72 20 70 50 61   SrcList for pPa
22068 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a  rse->pNewTable *
22069 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78  /.    NameContex
2206a 74 20 73 4e 43 3b 20 20 20 20 20 20 20 20 20 20  t sNC;          
2206b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f        /* Name co
2206c 6e 74 65 78 74 20 66 6f 72 20 70 50 61 72 73 65  ntext for pParse
2206d 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 0a  ->pNewTable */..
2206e 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c      memset(&sNC,
2206f 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
22070 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 53  ;.    memset(&sS
22071 72 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 53  rc, 0, sizeof(sS
22072 72 63 29 29 3b 0a 20 20 20 20 73 53 72 63 2e 6e  rc));.    sSrc.n
22073 53 72 63 20 3d 20 31 3b 0a 20 20 20 20 73 53 72  Src = 1;.    sSr
22074 63 2e 61 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 70  c.a[0].zName = p
22075 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 73 53 72  ->zName;.    sSr
22076 63 2e 61 5b 30 5d 2e 70 54 61 62 20 3d 20 70 3b  c.a[0].pTab = p;
22077 0a 20 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 69  .    sSrc.a[0].i
22078 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 20  Cursor = -1;.   
22079 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
2207a 61 72 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53  arse;.    sNC.pS
2207b 72 63 4c 69 73 74 20 3d 20 26 73 53 72 63 3b 0a  rcList = &sSrc;.
2207c 20 20 20 20 73 4e 43 2e 69 73 43 68 65 63 6b 20      sNC.isCheck 
2207d 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  = 1;.    if( sql
2207e 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
2207f 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 43  ames(&sNC, p->pC
22080 68 65 63 6b 29 20 29 7b 0a 20 20 20 20 20 20 72  heck) ){.      r
22081 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
22082 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
22083 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
22084 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20  CHECK) */..  /* 
22085 49 66 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e  If the db->init.
22086 62 75 73 79 20 69 73 20 31 20 69 74 20 6d 65 61  busy is 1 it mea
22087 6e 73 20 77 65 20 61 72 65 20 72 65 61 64 69 6e  ns we are readin
22088 67 20 74 68 65 20 53 51 4c 20 6f 66 66 20 74 68  g the SQL off th
22089 65 0a 20 20 2a 2a 20 22 73 71 6c 69 74 65 5f 6d  e.  ** "sqlite_m
2208a 61 73 74 65 72 22 20 6f 72 20 22 73 71 6c 69 74  aster" or "sqlit
2208b 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 20 74  e_temp_master" t
2208c 61 62 6c 65 20 6f 6e 20 74 68 65 20 64 69 73 6b  able on the disk
2208d 2e 0a 20 20 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74  ..  ** So do not
2208e 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 69   write to the di
2208f 73 6b 20 61 67 61 69 6e 2e 20 20 45 78 74 72 61  sk again.  Extra
22090 63 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  ct the root page
22091 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72   number.  ** for
22092 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   the table from 
22093 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77  the db->init.new
22094 54 6e 75 6d 20 66 69 65 6c 64 2e 20 20 28 54 68  Tnum field.  (Th
22095 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20  e page number.  
22096 2a 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62  ** should have b
22097 65 65 6e 20 70 75 74 20 74 68 65 72 65 20 62 79  een put there by
22098 20 74 68 65 20 73 71 6c 69 74 65 4f 70 65 6e 43   the sqliteOpenC
22099 62 20 72 6f 75 74 69 6e 65 2e 29 0a 20 20 2a 2f  b routine.).  */
2209a 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e  .  if( db->init.
2209b 62 75 73 79 20 29 7b 0a 20 20 20 20 70 2d 3e 74  busy ){.    p->t
2209c 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e  num = db->init.n
2209d 65 77 54 6e 75 6d 3b 0a 20 20 7d 0a 0a 20 20 2f  ewTnum;.  }..  /
2209e 2a 20 49 66 20 6e 6f 74 20 69 6e 69 74 69 61 6c  * If not initial
2209f 69 7a 69 6e 67 2c 20 74 68 65 6e 20 63 72 65 61  izing, then crea
220a0 74 65 20 61 20 72 65 63 6f 72 64 20 66 6f 72 20  te a record for 
220a1 74 68 65 20 6e 65 77 20 74 61 62 6c 65 0a 20 20  the new table.  
220a2 2a 2a 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45  ** in the SQLITE
220a3 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66  _MASTER table of
220a4 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20   the database.. 
220a5 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73   **.  ** If this
220a6 20 69 73 20 61 20 54 45 4d 50 4f 52 41 52 59 20   is a TEMPORARY 
220a7 74 61 62 6c 65 2c 20 77 72 69 74 65 20 74 68 65  table, write the
220a8 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20   entry into the 
220a9 61 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 66  auxiliary.  ** f
220aa 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69  ile instead of i
220ab 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74  nto the main dat
220ac 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2f  abase file..  */
220ad 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74  .  if( !db->init
220ae 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 69 6e 74  .busy ){.    int
220af 20 6e 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b   n;.    Vdbe *v;
220b0 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65  .    char *zType
220b1 3b 20 20 20 20 2f 2a 20 22 76 69 65 77 22 20 6f  ;    /* "view" o
220b2 72 20 22 74 61 62 6c 65 22 20 2a 2f 0a 20 20 20  r "table" */.   
220b3 20 63 68 61 72 20 2a 7a 54 79 70 65 32 3b 20 20   char *zType2;  
220b4 20 2f 2a 20 22 56 49 45 57 22 20 6f 72 20 22 54   /* "VIEW" or "T
220b5 41 42 4c 45 22 20 2a 2f 0a 20 20 20 20 63 68 61  ABLE" */.    cha
220b6 72 20 2a 7a 53 74 6d 74 3b 20 20 20 20 2f 2a 20  r *zStmt;    /* 
220b7 54 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41  Text of the CREA
220b8 54 45 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41  TE TABLE or CREA
220b9 54 45 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e  TE VIEW statemen
220ba 74 20 2a 2f 0a 0a 20 20 20 20 76 20 3d 20 73 71  t */..    v = sq
220bb 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
220bc 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d  rse);.    if( v=
220bd 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  =0 ) return;..  
220be 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
220bf 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
220c0 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 0a 20 20   0);..    /* .  
220c1 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    ** Initialize 
220c2 7a 54 79 70 65 20 66 6f 72 20 74 68 65 20 6e 65  zType for the ne
220c3 77 20 76 69 65 77 20 6f 72 20 74 61 62 6c 65 2e  w view or table.
220c4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
220c5 70 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b  p->pSelect==0 ){
220c6 0a 20 20 20 20 20 20 2f 2a 20 41 20 72 65 67 75  .      /* A regu
220c7 6c 61 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  lar table */.   
220c8 20 20 20 7a 54 79 70 65 20 3d 20 22 74 61 62 6c     zType = "tabl
220c9 65 22 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32  e";.      zType2
220ca 20 3d 20 22 54 41 42 4c 45 22 3b 0a 23 69 66 6e   = "TABLE";.#ifn
220cb 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
220cc 56 49 45 57 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  VIEW.    }else{.
220cd 20 20 20 20 20 20 2f 2a 20 41 20 76 69 65 77 20        /* A view 
220ce 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  */.      zType =
220cf 20 22 76 69 65 77 22 3b 0a 20 20 20 20 20 20 7a   "view";.      z
220d0 54 79 70 65 32 20 3d 20 22 56 49 45 57 22 3b 0a  Type2 = "VIEW";.
220d1 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20  #endif.    }..  
220d2 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
220d3 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78  a CREATE TABLE x
220d4 78 20 41 53 20 53 45 4c 45 43 54 20 2e 2e 2e 2c  x AS SELECT ...,
220d5 20 65 78 65 63 75 74 65 20 74 68 65 20 53 45 4c   execute the SEL
220d6 45 43 54 0a 20 20 20 20 2a 2a 20 73 74 61 74 65  ECT.    ** state
220d7 6d 65 6e 74 20 74 6f 20 70 6f 70 75 6c 61 74 65  ment to populate
220d8 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 20   the new table. 
220d9 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75  The root-page nu
220da 6d 62 65 72 20 66 6f 72 20 74 68 65 0a 20 20 20  mber for the.   
220db 20 2a 2a 20 6e 65 77 20 74 61 62 6c 65 20 69 73   ** new table is
220dc 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 50 61   in register pPa
220dd 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20  rse->regRoot..  
220de 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 63 65    **.    ** Once
220df 20 74 68 65 20 53 45 4c 45 43 54 20 68 61 73 20   the SELECT has 
220e0 62 65 65 6e 20 63 6f 64 65 64 20 62 79 20 73 71  been coded by sq
220e1 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2c 20 69  lite3Select(), i
220e2 74 20 69 73 20 69 6e 20 61 0a 20 20 20 20 2a 2a  t is in a.    **
220e3 20 73 75 69 74 61 62 6c 65 20 73 74 61 74 65 20   suitable state 
220e4 74 6f 20 71 75 65 72 79 20 66 6f 72 20 74 68 65  to query for the
220e5 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e   column names an
220e6 64 20 74 79 70 65 73 20 74 6f 20 62 65 20 75 73  d types to be us
220e7 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 65  ed.    ** by the
220e8 20 6e 65 77 20 74 61 62 6c 65 2e 0a 20 20 20 20   new table..    
220e9 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 73 68 61 72  **.    ** A shar
220ea 65 64 2d 63 61 63 68 65 20 77 72 69 74 65 2d 6c  ed-cache write-l
220eb 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 69  ock is not requi
220ec 72 65 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20  red to write to 
220ed 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2c 0a 20  the new table,. 
220ee 20 20 20 2a 2a 20 61 73 20 61 20 73 63 68 65 6d     ** as a schem
220ef 61 2d 6c 6f 63 6b 20 6d 75 73 74 20 68 61 76 65  a-lock must have
220f0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6f 62   already been ob
220f1 74 61 69 6e 65 64 20 74 6f 20 63 72 65 61 74 65  tained to create
220f2 20 69 74 2e 20 53 69 6e 63 65 0a 20 20 20 20 2a   it. Since.    *
220f3 2a 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20  * a schema-lock 
220f4 65 78 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68  excludes all oth
220f5 65 72 20 64 61 74 61 62 61 73 65 20 75 73 65 72  er database user
220f6 73 2c 20 74 68 65 20 77 72 69 74 65 2d 6c 6f 63  s, the write-loc
220f7 6b 20 77 6f 75 6c 64 0a 20 20 20 20 2a 2a 20 62  k would.    ** b
220f8 65 20 72 65 64 75 6e 64 61 6e 74 2e 0a 20 20 20  e redundant..   
220f9 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c   */.    if( pSel
220fa 65 63 74 20 29 7b 0a 20 20 20 20 20 20 53 65 6c  ect ){.      Sel
220fb 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20  ectDest dest;.  
220fc 20 20 20 20 54 61 62 6c 65 20 2a 70 53 65 6c 54      Table *pSelT
220fd 61 62 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72  ab;..      asser
220fe 74 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 3d 3d  t(pParse->nTab==
220ff 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
22100 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22101 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 31 2c 20  P_OpenWrite, 1, 
22102 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c  pParse->regRoot,
22103 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
22104 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
22105 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 50  (v, 1);.      pP
22106 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a  arse->nTab = 2;.
22107 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
22108 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73  ectDestInit(&des
22109 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c 20 31 29  t, SRT_Table, 1)
2210a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
2210b 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
2210c 65 6c 65 63 74 2c 20 26 64 65 73 74 29 3b 0a 20  elect, &dest);. 
2210d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2210e 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
2210f 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66  se, 1);.      if
22110 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d  ( pParse->nErr==
22111 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 53 65  0 ){.        pSe
22112 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65  lTab = sqlite3Re
22113 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28  sultSetOfSelect(
22114 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 29  pParse, pSelect)
22115 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 53  ;.        if( pS
22116 65 6c 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72  elTab==0 ) retur
22117 6e 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  n;.        asser
22118 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b  t( p->aCol==0 );
22119 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 43 6f 6c  .        p->nCol
2211a 20 3d 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c   = pSelTab->nCol
2211b 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 43 6f  ;.        p->aCo
2211c 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f  l = pSelTab->aCo
2211d 6c 3b 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54  l;.        pSelT
2211e 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20  ab->nCol = 0;.  
2211f 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 61        pSelTab->a
22120 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Col = 0;.       
22121 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
22122 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a 20 20  ble(pSelTab);.  
22123 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
22124 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
22125 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
22126 20 74 68 65 20 43 52 45 41 54 45 20 73 74 61 74   the CREATE stat
22127 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28  ement */.    if(
22128 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
22129 20 20 7a 53 74 6d 74 20 3d 20 63 72 65 61 74 65    zStmt = create
2212a 54 61 62 6c 65 53 74 6d 74 28 64 62 2c 20 70 29  TableStmt(db, p)
2212b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2212c 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e     n = (int)(pEn
2212d 64 2d 3e 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73  d->z - pParse->s
2212e 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 20 2b 20 31  NameToken.z) + 1
2212f 3b 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20  ;.      zStmt = 
22130 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
22131 62 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 43  b, .          "C
22132 52 45 41 54 45 20 25 73 20 25 2e 2a 73 22 2c 20  REATE %s %.*s", 
22133 7a 54 79 70 65 32 2c 20 6e 2c 20 70 50 61 72 73  zType2, n, pPars
22134 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 0a  e->sNameToken.z.
22135 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a        );.    }..
22136 20 20 20 20 2f 2a 20 41 20 73 6c 6f 74 20 66 6f      /* A slot fo
22137 72 20 74 68 65 20 72 65 63 6f 72 64 20 68 61 73  r the record has
22138 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c   already been al
22139 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 0a  located in the .
2213a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4d 41      ** SQLITE_MA
2213b 53 54 45 52 20 74 61 62 6c 65 2e 20 20 57 65 20  STER table.  We 
2213c 6a 75 73 74 20 6e 65 65 64 20 74 6f 20 75 70 64  just need to upd
2213d 61 74 65 20 74 68 61 74 20 73 6c 6f 74 20 77 69  ate that slot wi
2213e 74 68 20 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 68  th all.    ** th
2213f 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 65  e information we
22140 27 76 65 20 63 6f 6c 6c 65 63 74 65 64 2e 0a 20  've collected.. 
22141 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
22142 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
22143 72 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41  rse,.      "UPDA
22144 54 45 20 25 51 2e 25 73 20 22 0a 20 20 20 20 20  TE %Q.%s ".     
22145 20 20 20 20 22 53 45 54 20 74 79 70 65 3d 27 25      "SET type='%
22146 73 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c  s', name=%Q, tbl
22147 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61  _name=%Q, rootpa
22148 67 65 3d 23 25 64 2c 20 73 71 6c 3d 25 51 20 22  ge=#%d, sql=%Q "
22149 0a 20 20 20 20 20 20 20 22 57 48 45 52 45 20 72  .       "WHERE r
2214a 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20 20 20 20  owid=#%d",.     
2214b 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2214c 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ame, SCHEMA_TABL
2214d 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20 7a 54  E(iDb),.      zT
2214e 79 70 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e  ype,.      p->zN
2214f 61 6d 65 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e  ame,.      p->zN
22150 61 6d 65 2c 0a 20 20 20 20 20 20 70 50 61 72 73  ame,.      pPars
22151 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a 20 20 20 20  e->regRoot,.    
22152 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70    zStmt,.      p
22153 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a  Parse->regRowid.
22154 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74      );.    sqlit
22155 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74  e3DbFree(db, zSt
22156 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  mt);.    sqlite3
22157 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
22158 72 73 65 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e  rse, iDb);..#ifn
22159 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2215a 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
2215b 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65    /* Check to se
2215c 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20  e if we need to 
2215d 63 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65  create an sqlite
2215e 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
2215f 66 6f 72 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69  for.    ** keepi
22160 6e 67 20 74 72 61 63 6b 20 6f 66 20 61 75 74 6f  ng track of auto
22161 69 6e 63 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a  increment keys..
22162 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
22163 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
22164 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b  Autoincrement ){
22165 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 20 3d  .      Db *pDb =
22166 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
22167 20 20 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70        if( pDb->p
22168 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 3d  Schema->pSeqTab=
22169 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
2216a 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2216b 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
2216c 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45     "CREATE TABLE
2216d 20 25 51 2e 73 71 6c 69 74 65 5f 73 65 71 75 65   %Q.sqlite_seque
2216e 6e 63 65 28 6e 61 6d 65 2c 73 65 71 29 22 2c 0a  nce(name,seq)",.
2216f 20 20 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a            pDb->z
22170 4e 61 6d 65 0a 20 20 20 20 20 20 20 20 29 3b 0a  Name.        );.
22171 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
22172 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 52 65 70  ndif..    /* Rep
22173 61 72 73 65 20 65 76 65 72 79 74 68 69 6e 67 20  arse everything 
22174 74 6f 20 75 70 64 61 74 65 20 6f 75 72 20 69 6e  to update our in
22175 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75  ternal data stru
22176 63 74 75 72 65 73 20 2a 2f 0a 20 20 20 20 73 71  ctures */.    sq
22177 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
22178 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  v, OP_ParseSchem
22179 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 0a 20 20  a, iDb, 0, 0,.  
2217a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72        sqlite3MPr
2217b 69 6e 74 66 28 64 62 2c 20 22 74 62 6c 5f 6e 61  intf(db, "tbl_na
2217c 6d 65 3d 27 25 71 27 22 2c 70 2d 3e 7a 4e 61 6d  me='%q'",p->zNam
2217d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  e), P4_DYNAMIC);
2217e 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 20  .  }...  /* Add 
2217f 74 68 65 20 74 61 62 6c 65 20 74 6f 20 74 68 65  the table to the
22180 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65   in-memory repre
22181 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
22182 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a   database..  */.
22183 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
22184 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  usy && pParse->n
22185 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 54 61  Err==0 ){.    Ta
22186 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 53  ble *pOld;.    S
22187 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
22188 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20   p->pSchema;.   
22189 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48   pOld = sqlite3H
2218a 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68 65  ashInsert(&pSche
2218b 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 2d 3e  ma->tblHash, p->
2218c 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
2218d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2218e 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65      sqlite3Strle
2218f 6e 33 30 28 70 2d 3e 7a 4e 61 6d 65 29 2c 70 29  n30(p->zName),p)
22190 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20 29  ;.    if( pOld )
22191 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
22192 70 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d  p==pOld );  /* M
22193 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20  alloc must have 
22194 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48 61  failed inside Ha
22195 73 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20  shInsert() */.  
22196 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
22197 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
22198 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
22199 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
2219a 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  ble = 0;.    db-
2219b 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20 20 20 64  >nTable++;.    d
2219c 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
2219d 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
2219e 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
2219f 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c  E_OMIT_ALTERTABL
221a0 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 70 53  E.    if( !p->pS
221a1 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 63  elect ){.      c
221a2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
221a3 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
221a4 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f  )pParse->sNameTo
221a5 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69 6e 74  ken.z;.      int
221a6 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 61 73   nName;.      as
221a7 73 65 72 74 28 20 21 70 53 65 6c 65 63 74 20 26  sert( !pSelect &
221a8 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e 64 20  & pCons && pEnd 
221a9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  );.      if( pCo
221aa 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  ns->z==0 ){.    
221ab 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45 6e 64      pCons = pEnd
221ac 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
221ad 6e 4e 61 6d 65 20 3d 20 28 69 6e 74 29 28 28 63  nName = (int)((c
221ae 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 43 6f 6e  onst char *)pCon
221af 73 2d 3e 7a 20 2d 20 7a 4e 61 6d 65 29 3b 0a 20  s->z - zName);. 
221b0 20 20 20 20 20 70 2d 3e 61 64 64 43 6f 6c 4f 66       p->addColOf
221b1 66 73 65 74 20 3d 20 31 33 20 2b 20 73 71 6c 69  fset = 13 + sqli
221b2 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a  te3Utf8CharLen(z
221b3 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
221b4 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
221b5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
221b6 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a 2a  _OMIT_VIEW./*.**
221b7 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
221b8 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  s this routine i
221b9 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74  n order to creat
221ba 65 20 61 20 6e 65 77 20 56 49 45 57 0a 2a 2f 0a  e a new VIEW.*/.
221bb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
221bc 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
221bd 65 56 69 65 77 28 0a 20 20 50 61 72 73 65 20 2a  eView(.  Parse *
221be 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 54  pParse,     /* T
221bf 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
221c0 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
221c1 42 65 67 69 6e 2c 20 20 20 20 20 2f 2a 20 54 68  Begin,     /* Th
221c2 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20 74  e CREATE token t
221c3 68 61 74 20 62 65 67 69 6e 73 20 74 68 65 20 73  hat begins the s
221c4 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f  tatement */.  To
221c5 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20  ken *pName1,    
221c6 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68   /* The token th
221c7 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d  at holds the nam
221c8 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f  e of the view */
221c9 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32  .  Token *pName2
221ca 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b  ,     /* The tok
221cb 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  en that holds th
221cc 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69  e name of the vi
221cd 65 77 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  ew */.  Select *
221ce 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 41 20  pSelect,   /* A 
221cf 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
221d0 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d   that will becom
221d1 65 20 74 68 65 20 6e 65 77 20 76 69 65 77 20 2a  e the new view *
221d2 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20  /.  int isTemp, 
221d3 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 66         /* TRUE f
221d4 6f 72 20 61 20 54 45 4d 50 4f 52 41 52 59 20 76  or a TEMPORARY v
221d5 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45  iew */.  int noE
221d6 72 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  rr          /* S
221d7 75 70 70 72 65 73 73 20 65 72 72 6f 72 20 6d 65  uppress error me
221d8 73 73 61 67 65 73 20 69 66 20 56 49 45 57 20 61  ssages if VIEW a
221d9 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f  lready exists */
221da 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  .){.  Table *p;.
221db 20 20 69 6e 74 20 6e 3b 0a 20 20 63 6f 6e 73 74    int n;.  const
221dc 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
221dd 7a 3b 0a 20 20 54 6f 6b 65 6e 20 73 45 6e 64 3b  z;.  Token sEnd;
221de 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b  .  DbFixer sFix;
221df 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b  .  Token *pName;
221e0 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73 71  .  int iDb;.  sq
221e1 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
221e2 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70  se->db;..  if( p
221e3 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30 20 29 7b  Parse->nVar>0 ){
221e4 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
221e5 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 61  rMsg(pParse, "pa
221e6 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 6f 74  rameters are not
221e7 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76 69 65 77   allowed in view
221e8 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  s");.    sqlite3
221e9 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
221ea 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72   pSelect);.    r
221eb 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c  eturn;.  }.  sql
221ec 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70  ite3StartTable(p
221ed 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
221ee 4e 61 6d 65 32 2c 20 69 73 54 65 6d 70 2c 20 31  Name2, isTemp, 1
221ef 2c 20 30 2c 20 6e 6f 45 72 72 29 3b 0a 20 20 70  , 0, noErr);.  p
221f0 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
221f1 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30  able;.  if( p==0
221f2 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
221f3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
221f4 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
221f5 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65  pSelect);.    re
221f6 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  turn;.  }.  sqli
221f7 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70  te3TwoPartName(p
221f8 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
221f9 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a  Name2, &pName);.
221fa 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
221fb 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
221fc 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20   p->pSchema);.  
221fd 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e  if( sqlite3FixIn
221fe 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73 65  it(&sFix, pParse
221ff 2c 20 69 44 62 2c 20 22 76 69 65 77 22 2c 20 70  , iDb, "view", p
22200 4e 61 6d 65 29 0a 20 20 20 20 26 26 20 73 71 6c  Name).    && sql
22201 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 26 73  ite3FixSelect(&s
22202 46 69 78 2c 20 70 53 65 6c 65 63 74 29 0a 20 20  Fix, pSelect).  
22203 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
22204 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
22205 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74  Select);.    ret
22206 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  urn;.  }..  /* M
22207 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
22208 65 20 65 6e 74 69 72 65 20 53 45 4c 45 43 54 20  e entire SELECT 
22209 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64  statement that d
2220a 65 66 69 6e 65 73 20 74 68 65 20 76 69 65 77 2e  efines the view.
2220b 0a 20 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20  .  ** This will 
2220c 66 6f 72 63 65 20 61 6c 6c 20 74 68 65 20 45 78  force all the Ex
2220d 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65  pr.token.z value
2220e 73 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 61  s to be dynamica
2220f 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  lly.  ** allocat
22210 65 64 20 72 61 74 68 65 72 20 74 68 61 6e 20 70  ed rather than p
22211 6f 69 6e 74 20 74 6f 20 74 68 65 20 69 6e 70 75  oint to the inpu
22212 74 20 73 74 72 69 6e 67 20 2d 20 77 68 69 63 68  t string - which
22213 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 2a 2a   means that.  **
22214 20 74 68 65 79 20 77 69 6c 6c 20 70 65 72 73 69   they will persi
22215 73 74 20 61 66 74 65 72 20 74 68 65 20 63 75 72  st after the cur
22216 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65  rent sqlite3_exe
22217 63 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73  c() call returns
22218 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 53 65 6c  ..  */.  p->pSel
22219 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
2221a 65 63 74 44 75 70 28 64 62 2c 20 70 53 65 6c 65  ectDup(db, pSele
2221b 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  ct, EXPRDUP_REDU
2221c 43 45 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  CE);.  sqlite3Se
2221d 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2221e 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28 20 64  Select);.  if( d
2221f 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22220 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
22221 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e   }.  if( !db->in
22222 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 73  it.busy ){.    s
22223 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
22224 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
22225 20 70 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c   p);.  }..  /* L
22226 6f 63 61 74 65 20 74 68 65 20 65 6e 64 20 6f 66  ocate the end of
22227 20 74 68 65 20 43 52 45 41 54 45 20 56 49 45 57   the CREATE VIEW
22228 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4d 61 6b   statement.  Mak
22229 65 20 73 45 6e 64 20 70 6f 69 6e 74 20 74 6f 0a  e sEnd point to.
2222a 20 20 2a 2a 20 74 68 65 20 65 6e 64 2e 0a 20 20    ** the end..  
2222b 2a 2f 0a 20 20 73 45 6e 64 20 3d 20 70 50 61 72  */.  sEnd = pPar
2222c 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 3b 0a  se->sLastToken;.
2222d 20 20 69 66 28 20 73 45 6e 64 2e 7a 5b 30 5d 21    if( sEnd.z[0]!
2222e 3d 30 20 26 26 20 73 45 6e 64 2e 7a 5b 30 5d 21  =0 && sEnd.z[0]!
2222f 3d 27 3b 27 20 29 7b 0a 20 20 20 20 73 45 6e 64  =';' ){.    sEnd
22230 2e 7a 20 2b 3d 20 73 45 6e 64 2e 6e 3b 0a 20 20  .z += sEnd.n;.  
22231 7d 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 30 3b 0a  }.  sEnd.n = 0;.
22232 20 20 6e 20 3d 20 28 69 6e 74 29 28 73 45 6e 64    n = (int)(sEnd
22233 2e 7a 20 2d 20 70 42 65 67 69 6e 2d 3e 7a 29 3b  .z - pBegin->z);
22234 0a 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20 75 6e  .  z = (const un
22235 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70 42 65  signed char*)pBe
22236 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69 6c 65 28  gin->z;.  while(
22237 20 6e 3e 30 20 26 26 20 28 7a 5b 6e 2d 31 5d 3d   n>0 && (z[n-1]=
22238 3d 27 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33 49  =';' || sqlite3I
22239 73 73 70 61 63 65 28 7a 5b 6e 2d 31 5d 29 29 20  sspace(z[n-1])) 
2223a 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64  ){ n--; }.  sEnd
2223b 2e 7a 20 3d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20  .z = &z[n-1];.  
2223c 73 45 6e 64 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f  sEnd.n = 1;..  /
2223d 2a 20 55 73 65 20 73 71 6c 69 74 65 33 45 6e 64  * Use sqlite3End
2223e 54 61 62 6c 65 28 29 20 74 6f 20 61 64 64 20 74  Table() to add t
2223f 68 65 20 76 69 65 77 20 74 6f 20 74 68 65 20 53  he view to the S
22240 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
22241 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45  le */.  sqlite3E
22242 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  ndTable(pParse, 
22243 30 2c 20 26 73 45 6e 64 2c 20 30 29 3b 0a 20 20  0, &sEnd, 0);.  
22244 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66  return;.}.#endif
22245 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
22246 56 49 45 57 20 2a 2f 0a 0a 23 69 66 20 21 64 65  VIEW */..#if !de
22247 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
22248 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69  T_VIEW) || !defi
22249 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2224a 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 2f 2a  VIRTUALTABLE)./*
2224b 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74  .** The Table st
2224c 72 75 63 74 75 72 65 20 70 54 61 62 6c 65 20 69  ructure pTable i
2224d 73 20 72 65 61 6c 6c 79 20 61 20 56 49 45 57 2e  s really a VIEW.
2224e 20 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 6e 61    Fill in the na
2224f 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  mes of.** the co
22250 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 76 69 65  lumns of the vie
22251 77 20 69 6e 20 74 68 65 20 70 54 61 62 6c 65 20  w in the pTable 
22252 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 74 75  structure.  Retu
22253 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a  rn the number.**
22254 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20   of errors.  If 
22255 61 6e 20 65 72 72 6f 72 20 69 73 20 73 65 65 6e  an error is seen
22256 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20   leave an error 
22257 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73  message in pPars
22258 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53  e->zErrMsg..*/.S
22259 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2225a 74 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  t sqlite3ViewGet
2225b 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73  ColumnNames(Pars
2225c 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
2225d 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 54 61 62   *pTable){.  Tab
2225e 6c 65 20 2a 70 53 65 6c 54 61 62 3b 20 20 20 2f  le *pSelTab;   /
2225f 2a 20 41 20 66 61 6b 65 20 74 61 62 6c 65 20 66  * A fake table f
22260 72 6f 6d 20 77 68 69 63 68 20 77 65 20 67 65 74  rom which we get
22261 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
22262 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
22263 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f  l;     /* Copy o
22264 66 20 74 68 65 20 53 45 4c 45 43 54 20 74 68 61  f the SELECT tha
22265 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
22266 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e   view */.  int n
22267 45 72 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  Err = 0;     /* 
22268 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
22269 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a   encountered */.
2226a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
2226b 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 69      /* Temporari
2226c 6c 79 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d  ly holds the num
2226d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 61  ber of cursors a
2226e 73 73 69 67 6e 65 64 20 2a 2f 0a 20 20 73 71 6c  ssigned */.  sql
2226f 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
22270 65 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62  e->db;  /* Datab
22271 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66  ase connection f
22272 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  or malloc errors
22273 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 41 75 74   */.  int (*xAut
22274 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  h)(void*,int,con
22275 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
22276 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
22277 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a  ,const char*);..
22278 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
22279 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   );..#ifndef SQL
2227a 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2227b 54 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69  TABLE.  if( sqli
2227c 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65  te3VtabCallConne
2227d 63 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c  ct(pParse, pTabl
2227e 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  e) ){.    return
2227f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
22280 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75   }.  if( IsVirtu
22281 61 6c 28 70 54 61 62 6c 65 29 20 29 20 72 65 74  al(pTable) ) ret
22282 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23  urn 0;.#endif..#
22283 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
22284 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 41 20 70  IT_VIEW.  /* A p
22285 6f 73 69 74 69 76 65 20 6e 43 6f 6c 20 6d 65 61  ositive nCol mea
22286 6e 73 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6e  ns the columns n
22287 61 6d 65 73 20 66 6f 72 20 74 68 69 73 20 76 69  ames for this vi
22288 65 77 20 61 72 65 0a 20 20 2a 2a 20 61 6c 72 65  ew are.  ** alre
22289 61 64 79 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a  ady known..  */.
2228a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43    if( pTable->nC
2228b 6f 6c 3e 30 20 29 20 72 65 74 75 72 6e 20 30 3b  ol>0 ) return 0;
2228c 0a 0a 20 20 2f 2a 20 41 20 6e 65 67 61 74 69 76  ..  /* A negativ
2228d 65 20 6e 43 6f 6c 20 69 73 20 61 20 73 70 65 63  e nCol is a spec
2228e 69 61 6c 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69  ial marker meani
2228f 6e 67 20 74 68 61 74 20 77 65 20 61 72 65 20 63  ng that we are c
22290 75 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 74 72  urrently.  ** tr
22291 79 69 6e 67 20 74 6f 20 63 6f 6d 70 75 74 65 20  ying to compute 
22292 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
22293 2e 20 20 49 66 20 77 65 20 65 6e 74 65 72 20 74  .  If we enter t
22294 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68  his routine with
22295 0a 20 20 2a 2a 20 61 20 6e 65 67 61 74 69 76 65  .  ** a negative
22296 20 6e 43 6f 6c 2c 20 69 74 20 6d 65 61 6e 73 20   nCol, it means 
22297 74 77 6f 20 6f 72 20 6d 6f 72 65 20 76 69 65 77  two or more view
22298 73 20 66 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c  s form a loop, l
22299 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20  ike this:.  **. 
2229a 20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56   **     CREATE V
2229b 49 45 57 20 6f 6e 65 20 41 53 20 53 45 4c 45 43  IEW one AS SELEC
2229c 54 20 2a 20 46 52 4f 4d 20 74 77 6f 3b 0a 20 20  T * FROM two;.  
2229d 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49  **     CREATE VI
2229e 45 57 20 74 77 6f 20 41 53 20 53 45 4c 45 43 54  EW two AS SELECT
2229f 20 2a 20 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a   * FROM one;.  *
222a0 2a 0a 20 20 2a 2a 20 41 63 74 75 61 6c 6c 79 2c  *.  ** Actually,
222a1 20 74 68 69 73 20 65 72 72 6f 72 20 69 73 20 63   this error is c
222a2 61 75 67 68 74 20 70 72 65 76 69 6f 75 73 6c 79  aught previously
222a3 20 61 6e 64 20 73 6f 20 74 68 65 20 66 6f 6c 6c   and so the foll
222a4 6f 77 69 6e 67 20 74 65 73 74 0a 20 20 2a 2a 20  owing test.  ** 
222a5 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 66 61  should always fa
222a6 69 6c 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c  il.  But we will
222a7 20 6c 65 61 76 65 20 69 74 20 69 6e 20 70 6c 61   leave it in pla
222a8 63 65 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61  ce just to be sa
222a9 66 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  fe..  */.  if( p
222aa 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b  Table->nCol<0 ){
222ab 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
222ac 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69  rMsg(pParse, "vi
222ad 65 77 20 25 73 20 69 73 20 63 69 72 63 75 6c 61  ew %s is circula
222ae 72 6c 79 20 64 65 66 69 6e 65 64 22 2c 20 70 54  rly defined", pT
222af 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  able->zName);.  
222b0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
222b1 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
222b2 2d 3e 6e 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20  ->nCol>=0 );..  
222b3 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69  /* If we get thi
222b4 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20  s far, it means 
222b5 77 65 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 75  we need to compu
222b6 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  te the table nam
222b7 65 73 2e 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68  es..  ** Note th
222b8 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73  at the call to s
222b9 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f  qlite3ResultSetO
222ba 66 53 65 6c 65 63 74 28 29 20 77 69 6c 6c 20 65  fSelect() will e
222bb 78 70 61 6e 64 20 61 6e 79 0a 20 20 2a 2a 20 22  xpand any.  ** "
222bc 2a 22 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  *" elements in t
222bd 68 65 20 72 65 73 75 6c 74 73 20 73 65 74 20 6f  he results set o
222be 66 20 74 68 65 20 76 69 65 77 20 61 6e 64 20 77  f the view and w
222bf 69 6c 6c 20 61 73 73 69 67 6e 20 63 75 72 73 6f  ill assign curso
222c0 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 65  rs.  ** to the e
222c1 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 46  lements of the F
222c2 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 42 75 74  ROM clause.  But
222c3 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   we do not want 
222c4 74 68 65 73 65 20 63 68 61 6e 67 65 73 0a 20 20  these changes.  
222c5 2a 2a 20 74 6f 20 62 65 20 70 65 72 6d 61 6e 65  ** to be permane
222c6 6e 74 2e 20 20 53 6f 20 74 68 65 20 63 6f 6d 70  nt.  So the comp
222c7 75 74 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20  utation is done 
222c8 6f 6e 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  on a copy of the
222c9 20 53 45 4c 45 43 54 0a 20 20 2a 2a 20 73 74 61   SELECT.  ** sta
222ca 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69  tement that defi
222cb 6e 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20  nes the view..  
222cc 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  */.  assert( pTa
222cd 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20 29 3b 0a  ble->pSelect );.
222ce 20 20 70 53 65 6c 20 3d 20 73 71 6c 69 74 65 33    pSel = sqlite3
222cf 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54  SelectDup(db, pT
222d0 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 2c 20 30  able->pSelect, 0
222d1 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 20 29 7b  );.  if( pSel ){
222d2 0a 20 20 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f  .    u8 enableLo
222d3 6f 6b 61 73 69 64 65 20 3d 20 64 62 2d 3e 6c 6f  okaside = db->lo
222d4 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
222d5 3b 0a 20 20 20 20 6e 20 3d 20 70 50 61 72 73 65  ;.    n = pParse
222d6 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 73 71 6c 69  ->nTab;.    sqli
222d7 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e  te3SrcListAssign
222d8 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20  Cursors(pParse, 
222d9 70 53 65 6c 2d 3e 70 53 72 63 29 3b 0a 20 20 20  pSel->pSrc);.   
222da 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
222db 2d 31 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  -1;.    db->look
222dc 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d  aside.bEnabled =
222dd 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   0;.#ifndef SQLI
222de 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
222df 41 54 49 4f 4e 0a 20 20 20 20 78 41 75 74 68 20  ATION.    xAuth 
222e0 3d 20 64 62 2d 3e 78 41 75 74 68 3b 0a 20 20 20  = db->xAuth;.   
222e1 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a   db->xAuth = 0;.
222e2 20 20 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71      pSelTab = sq
222e3 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66  lite3ResultSetOf
222e4 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
222e5 53 65 6c 29 3b 0a 20 20 20 20 64 62 2d 3e 78 41  Sel);.    db->xA
222e6 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 23 65 6c  uth = xAuth;.#el
222e7 73 65 0a 20 20 20 20 70 53 65 6c 54 61 62 20 3d  se.    pSelTab =
222e8 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65   sqlite3ResultSe
222e9 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 65  tOfSelect(pParse
222ea 2c 20 70 53 65 6c 29 3b 0a 23 65 6e 64 69 66 0a  , pSel);.#endif.
222eb 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
222ec 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61  e.bEnabled = ena
222ed 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a 20 20  bleLookaside;.  
222ee 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d    pParse->nTab =
222ef 20 6e 3b 0a 20 20 20 20 69 66 28 20 70 53 65 6c   n;.    if( pSel
222f0 54 61 62 20 29 7b 0a 20 20 20 20 20 20 61 73 73  Tab ){.      ass
222f1 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 61 43 6f  ert( pTable->aCo
222f2 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 54  l==0 );.      pT
222f3 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65  able->nCol = pSe
222f4 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20  lTab->nCol;.    
222f5 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d    pTable->aCol =
222f6 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a   pSelTab->aCol;.
222f7 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e        pSelTab->n
222f8 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  Col = 0;.      p
222f9 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30  SelTab->aCol = 0
222fa 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
222fb 65 6c 65 74 65 54 61 62 6c 65 28 70 53 65 6c 54  eleteTable(pSelT
222fc 61 62 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c  ab);.      pTabl
222fd 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67  e->pSchema->flag
222fe 73 20 7c 3d 20 44 42 5f 55 6e 72 65 73 65 74 56  s |= DB_UnresetV
222ff 69 65 77 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  iews;.    }else{
22300 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e  .      pTable->n
22301 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6e  Col = 0;.      n
22302 45 72 72 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  Err++;.    }.   
22303 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
22304 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 29 3b 0a  lete(db, pSel);.
22305 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 6e    } else {.    n
22306 45 72 72 2b 2b 3b 0a 20 20 7d 0a 23 65 6e 64 69  Err++;.  }.#endi
22307 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
22308 5f 56 49 45 57 20 2a 2f 0a 20 20 72 65 74 75 72  _VIEW */.  retur
22309 6e 20 6e 45 72 72 3b 20 20 0a 7d 0a 23 65 6e 64  n nErr;  .}.#end
2230a 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
2230b 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
2230c 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
2230d 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2230e 54 41 42 4c 45 29 20 2a 2f 0a 0a 23 69 66 6e 64  TABLE) */..#ifnd
2230f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
22310 49 45 57 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  IEW./*.** Clear 
22311 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
22312 20 66 72 6f 6d 20 65 76 65 72 79 20 56 49 45 57   from every VIEW
22313 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 64 78   in database idx
22314 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
22315 20 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74   sqliteViewReset
22316 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  All(sqlite3 *db,
22317 20 69 6e 74 20 69 64 78 29 7b 0a 20 20 48 61 73   int idx){.  Has
22318 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 66 28 20  hElem *i;.  if( 
22319 21 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64  !DbHasProperty(d
2231a 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73  b, idx, DB_Unres
2231b 65 74 56 69 65 77 73 29 20 29 20 72 65 74 75 72  etViews) ) retur
2231c 6e 3b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74  n;.  for(i=sqlit
2231d 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e  eHashFirst(&db->
2231e 61 44 62 5b 69 64 78 5d 2e 70 53 63 68 65 6d 61  aDb[idx].pSchema
2231f 2d 3e 74 62 6c 48 61 73 68 29 3b 20 69 3b 69 3d  ->tblHash); i;i=
22320 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69  sqliteHashNext(i
22321 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  )){.    Table *p
22322 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Tab = sqliteHash
22323 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28  Data(i);.    if(
22324 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
22325 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 52 65  {.      sqliteRe
22326 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  setColumnNames(p
22327 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Tab);.    }.  }.
22328 20 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74    DbClearPropert
22329 79 28 64 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e  y(db, idx, DB_Un
2232a 72 65 73 65 74 56 69 65 77 73 29 3b 0a 7d 0a 23  resetViews);.}.#
2232b 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
2232c 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c  liteViewResetAll
2232d 28 41 2c 42 29 0a 23 65 6e 64 69 66 20 2f 2a 20  (A,B).#endif /* 
2232e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2232f 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   */../*.** This 
22330 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
22331 65 64 20 62 79 20 74 68 65 20 56 44 42 45 20 74  ed by the VDBE t
22332 6f 20 61 64 6a 75 73 74 20 74 68 65 20 69 6e 74  o adjust the int
22333 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a 20  ernal schema.** 
22334 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 77  used by SQLite w
22335 68 65 6e 20 74 68 65 20 62 74 72 65 65 20 6c 61  hen the btree la
22336 79 65 72 20 6d 6f 76 65 73 20 61 20 74 61 62 6c  yer moves a tabl
22337 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 54 68 65  e root page. The
22338 0a 2a 2a 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66  .** root-page of
22339 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65   a table or inde
2233a 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44  x in database iD
2233b 62 20 68 61 73 20 63 68 61 6e 67 65 64 20 66 72  b has changed fr
2233c 6f 6d 20 69 46 72 6f 6d 0a 2a 2a 20 74 6f 20 69  om iFrom.** to i
2233d 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74  To..**.** Ticket
2233e 20 23 31 37 32 38 3a 20 20 54 68 65 20 73 79 6d   #1728:  The sym
2233f 62 6f 6c 20 74 61 62 6c 65 20 6d 69 67 68 74 20  bol table might 
22340 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 69 6e  still contain in
22341 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20  formation.** on 
22342 74 61 62 6c 65 73 20 61 6e 64 2f 6f 72 20 69 6e  tables and/or in
22343 64 69 63 65 73 20 74 68 61 74 20 61 72 65 20 74  dices that are t
22344 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62 65  he process of be
22345 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20  ing deleted..** 
22346 49 66 20 79 6f 75 20 61 72 65 20 75 6e 6c 75 63  If you are unluc
22347 6b 79 2c 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65  ky, one of those
22348 20 64 65 6c 65 74 65 64 20 69 6e 64 69 63 65 73   deleted indices
22349 20 6f 72 20 74 61 62 6c 65 73 20 6d 69 67 68 74   or tables might
2234a 0a 2a 2a 20 68 61 76 65 20 74 68 65 20 73 61 6d  .** have the sam
2234b 65 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65  e rootpage numbe
2234c 72 20 61 73 20 74 68 65 20 72 65 61 6c 20 74 61  r as the real ta
2234d 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 68 61  ble or index tha
2234e 74 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 6d 6f  t is.** being mo
2234f 76 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e 6e  ved.  So we cann
22350 6f 74 20 73 74 6f 70 20 73 65 61 72 63 68 69 6e  ot stop searchin
22351 67 20 61 66 74 65 72 20 74 68 65 20 66 69 72 73  g after the firs
22352 74 20 6d 61 74 63 68 20 0a 2a 2a 20 62 65 63 61  t match .** beca
22353 75 73 65 20 74 68 65 20 66 69 72 73 74 20 6d 61  use the first ma
22354 74 63 68 20 6d 69 67 68 74 20 62 65 20 66 6f 72  tch might be for
22355 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 6c 65   one of the dele
22356 74 65 64 20 69 6e 64 69 63 65 73 0a 2a 2a 20 6f  ted indices.** o
22357 72 20 74 61 62 6c 65 73 20 61 6e 64 20 6e 6f 74  r tables and not
22358 20 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65 78   the table/index
22359 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c   that is actuall
2235a 79 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 0a 2a  y being moved..*
2235b 2a 20 57 65 20 6d 75 73 74 20 63 6f 6e 74 69 6e  * We must contin
2235c 75 65 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c  ue looping until
2235d 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64 20   all tables and 
2235e 69 6e 64 69 63 65 73 20 77 69 74 68 0a 2a 2a 20  indices with.** 
2235f 72 6f 6f 74 70 61 67 65 3d 3d 69 46 72 6f 6d 20  rootpage==iFrom 
22360 68 61 76 65 20 62 65 65 6e 20 63 6f 6e 76 65 72  have been conver
22361 74 65 64 20 74 6f 20 68 61 76 65 20 61 20 72 6f  ted to have a ro
22362 6f 74 70 61 67 65 20 6f 66 20 69 54 6f 0a 2a 2a  otpage of iTo.**
22363 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20   in order to be 
22364 63 65 72 74 61 69 6e 20 74 68 61 74 20 77 65 20  certain that we 
22365 67 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f 6e  got the right on
22366 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
22367 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
22368 43 55 55 4d 0a 53 51 4c 49 54 45 5f 50 52 49 56  CUUM.SQLITE_PRIV
22369 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2236a 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62  RootPageMoved(Db
2236b 20 2a 70 44 62 2c 20 69 6e 74 20 69 46 72 6f 6d   *pDb, int iFrom
2236c 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 48 61  , int iTo){.  Ha
2236d 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20  shElem *pElem;. 
2236e 20 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 0a 20   Hash *pHash;.. 
2236f 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70   pHash = &pDb->p
22370 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b  Schema->tblHash;
22371 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
22372 69 74 65 48 61 73 68 46 69 72 73 74 28 70 48 61  iteHashFirst(pHa
22373 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65  sh); pElem; pEle
22374 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  m=sqliteHashNext
22375 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61  (pElem)){.    Ta
22376 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
22377 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d  teHashData(pElem
22378 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  );.    if( pTab-
22379 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a  >tnum==iFrom ){.
2237a 20 20 20 20 20 20 70 54 61 62 2d 3e 74 6e 75 6d        pTab->tnum
2237b 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20   = iTo;.    }.  
2237c 7d 0a 20 20 70 48 61 73 68 20 3d 20 26 70 44 62  }.  pHash = &pDb
2237d 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  ->pSchema->idxHa
2237e 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  sh;.  for(pElem=
2237f 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
22380 70 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70  pHash); pElem; p
22381 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
22382 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
22383 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73   Index *pIdx = s
22384 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45  qliteHashData(pE
22385 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 49  lem);.    if( pI
22386 64 78 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20  dx->tnum==iFrom 
22387 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 74  ){.      pIdx->t
22388 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d  num = iTo;.    }
22389 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
2238a 2a 0a 2a 2a 20 57 72 69 74 65 20 63 6f 64 65 20  *.** Write code 
2238b 74 6f 20 65 72 61 73 65 20 74 68 65 20 74 61 62  to erase the tab
2238c 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  le with root-pag
2238d 65 20 69 54 61 62 6c 65 20 66 72 6f 6d 20 64 61  e iTable from da
2238e 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41  tabase iDb..** A
2238f 6c 73 6f 20 77 72 69 74 65 20 63 6f 64 65 20 74  lso write code t
22390 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c  o modify the sql
22391 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
22392 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73 63   and internal sc
22393 68 65 6d 61 0a 2a 2a 20 69 66 20 61 20 72 6f 6f  hema.** if a roo
22394 74 2d 70 61 67 65 20 6f 66 20 61 6e 6f 74 68 65  t-page of anothe
22395 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64  r table is moved
22396 20 62 79 20 74 68 65 20 62 74 72 65 65 2d 6c 61   by the btree-la
22397 79 65 72 20 77 68 69 6c 73 74 0a 2a 2a 20 65 72  yer whilst.** er
22398 61 73 69 6e 67 20 69 54 61 62 6c 65 20 28 74 68  asing iTable (th
22399 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69  is can happen wi
2239a 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  th an auto-vacuu
2239b 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 20  m database)..*/ 
2239c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 73  .static void des
2239d 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 50 61 72  troyRootPage(Par
2239e 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
2239f 69 54 61 62 6c 65 2c 20 69 6e 74 20 69 44 62 29  iTable, int iDb)
223a0 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
223a1 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
223a2 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 31 20 3d  rse);.  int r1 =
223a3 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
223a4 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
223a5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
223a6 76 2c 20 4f 50 5f 44 65 73 74 72 6f 79 2c 20 69  v, OP_Destroy, i
223a7 54 61 62 6c 65 2c 20 72 31 2c 20 69 44 62 29 3b  Table, r1, iDb);
223a8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
223a9 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
223aa 20 20 2f 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20    /* OP_Destroy 
223ab 73 74 6f 72 65 73 20 61 6e 20 69 6e 20 69 6e 74  stores an in int
223ac 65 67 65 72 20 72 31 2e 20 49 66 20 74 68 69 73  eger r1. If this
223ad 20 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 69 73   integer.  ** is
223ae 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
223af 69 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70  it is the root p
223b0 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20  age number of a 
223b1 74 61 62 6c 65 20 6d 6f 76 65 64 20 74 6f 0a 20  table moved to. 
223b2 20 2a 2a 20 6c 6f 63 61 74 69 6f 6e 20 69 54 61   ** location iTa
223b3 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ble. The followi
223b4 6e 67 20 63 6f 64 65 20 6d 6f 64 69 66 69 65 73  ng code modifies
223b5 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
223b6 65 72 20 74 61 62 6c 65 20 74 6f 0a 20 20 2a 2a  er table to.  **
223b7 20 72 65 66 6c 65 63 74 20 74 68 69 73 2e 0a 20   reflect this.. 
223b8 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 22 23 4e   **.  ** The "#N
223b9 4e 4e 22 20 69 6e 20 74 68 65 20 53 51 4c 20 69  NN" in the SQL i
223ba 73 20 61 20 73 70 65 63 69 61 6c 20 63 6f 6e 73  s a special cons
223bb 74 61 6e 74 20 74 68 61 74 20 6d 65 61 6e 73 20  tant that means 
223bc 77 68 61 74 65 76 65 72 20 76 61 6c 75 65 0a 20  whatever value. 
223bd 20 2a 2a 20 69 73 20 69 6e 20 72 65 67 69 73 74   ** is in regist
223be 65 72 20 4e 4e 4e 2e 20 20 53 65 65 20 73 71 6c  er NNN.  See sql
223bf 69 74 65 33 52 65 67 69 73 74 65 72 45 78 70 72  ite3RegisterExpr
223c0 28 29 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ()..  */.  sqlit
223c1 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
223c2 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55 50 44  arse, .     "UPD
223c3 41 54 45 20 25 51 2e 25 73 20 53 45 54 20 72 6f  ATE %Q.%s SET ro
223c4 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52 45 20  otpage=%d WHERE 
223c5 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61 67 65  #%d AND rootpage
223c6 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50 61 72  =#%d",.     pPar
223c7 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d  se->db->aDb[iDb]
223c8 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54  .zName, SCHEMA_T
223c9 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61 62 6c  ABLE(iDb), iTabl
223ca 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65 6e 64  e, r1, r1);.#end
223cb 69 66 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  if.  sqlite3Rele
223cc 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
223cd 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  e, r1);.}../*.**
223ce 20 57 72 69 74 65 20 56 44 42 45 20 63 6f 64 65   Write VDBE code
223cf 20 74 6f 20 65 72 61 73 65 20 74 61 62 6c 65 20   to erase table 
223d0 70 54 61 62 20 61 6e 64 20 61 6c 6c 20 61 73 73  pTab and all ass
223d1 6f 63 69 61 74 65 64 20 69 6e 64 69 63 65 73 20  ociated indices 
223d2 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f 64 65  on disk..** Code
223d3 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 73   to update the s
223d4 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
223d5 6c 65 73 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c  les and internal
223d6 20 73 63 68 65 6d 61 20 64 65 66 69 6e 69 74 69   schema definiti
223d7 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65 20 61  ons.** in case a
223d8 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c 6f 6e   root-page belon
223d9 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65 72 20  ging to another 
223da 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 20 62  table is moved b
223db 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65  y the btree laye
223dc 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61 64 64  r.** is also add
223dd 65 64 20 28 74 68 69 73 20 63 61 6e 20 68 61 70  ed (this can hap
223de 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75 74 6f  pen with an auto
223df 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
223e0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
223e1 64 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28 50  d destroyTable(P
223e2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
223e3 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69 66 64  ble *pTab){.#ifd
223e4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
223e5 55 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e 64 65  UTOVACUUM.  Inde
223e6 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 69  x *pIdx;.  int i
223e7 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
223e8 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
223e9 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
223ea 65 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f 79 52  ema);.  destroyR
223eb 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c 20  ootPage(pParse, 
223ec 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pTab->tnum, iDb)
223ed 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61  ;.  for(pIdx=pTa
223ee 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
223ef 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
223f0 74 29 7b 0a 20 20 20 20 64 65 73 74 72 6f 79 52  t){.    destroyR
223f1 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c 20  ootPage(pParse, 
223f2 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pIdx->tnum, iDb)
223f3 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a  ;.  }.#else.  /*
223f4 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
223f5 20 6d 61 79 20 62 65 20 61 75 74 6f 2d 76 61 63   may be auto-vac
223f6 75 75 6d 20 63 61 70 61 62 6c 65 20 28 69 66 20  uum capable (if 
223f7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
223f8 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73 20 6e  VACUUM.  ** is n
223f9 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 74 68 65  ot defined), the
223fa 6e 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e  n it is importan
223fb 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44 65 73  t to call OP_Des
223fc 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20 2a 2a  troy on the.  **
223fd 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78   table and index
223fe 20 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e 20 6f   root-pages in o
223ff 72 64 65 72 2c 20 73 74 61 72 74 69 6e 67 20 77  rder, starting w
22400 69 74 68 20 74 68 65 20 6e 75 6d 65 72 69 63 61  ith the numerica
22401 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67 65 73  lly .  ** larges
22402 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62  t root-page numb
22403 65 72 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74  er. This guarant
22404 65 65 73 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66  ees that none of
22405 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 73 0a   the root-pages.
22406 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73 74 72    ** to be destr
22407 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61 74 65  oyed is relocate
22408 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65 72 20  d by an earlier 
22409 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e 65 2e  OP_Destroy. i.e.
2240a 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 66 6f 6c   if the.  ** fol
2240b 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f 64 65  lowing were code
2240c 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 50 5f  d:.  **.  ** OP_
2240d 44 65 73 74 72 6f 79 20 34 20 30 0a 20 20 2a 2a  Destroy 4 0.  **
2240e 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44 65 73   ....  ** OP_Des
2240f 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a 20 20  troy 5 0.  **.  
22410 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61 67 65  ** and root page
22411 20 35 20 68 61 70 70 65 6e 65 64 20 74 6f 20 62   5 happened to b
22412 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f  e the largest ro
22413 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 69  ot-page number i
22414 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  n the.  ** datab
22415 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74 20 70  ase, then root p
22416 61 67 65 20 35 20 77 6f 75 6c 64 20 62 65 20 6d  age 5 would be m
22417 6f 76 65 64 20 74 6f 20 70 61 67 65 20 34 20 62  oved to page 4 b
22418 79 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f 50 5f  y the .  ** "OP_
22419 44 65 73 74 72 6f 79 20 34 20 30 22 20 6f 70 63  Destroy 4 0" opc
2241a 6f 64 65 2e 20 54 68 65 20 73 75 62 73 65 71 75  ode. The subsequ
2241b 65 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f 79 20  ent "OP_Destroy 
2241c 35 20 30 22 20 77 6f 75 6c 64 20 68 69 74 0a 20  5 0" would hit. 
2241d 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73 74 20   ** a free-list 
2241e 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  page..  */.  int
2241f 20 69 54 61 62 20 3d 20 70 54 61 62 2d 3e 74 6e   iTab = pTab->tn
22420 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73 74 72  um;.  int iDestr
22421 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77 68 69  oyed = 0;..  whi
22422 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 49 6e 64  le( 1 ){.    Ind
22423 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 69 6e  ex *pIdx;.    in
22424 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30 3b 0a  t iLargest = 0;.
22425 0a 20 20 20 20 69 66 28 20 69 44 65 73 74 72 6f  .    if( iDestro
22426 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62 3c 69  yed==0 || iTab<i
22427 44 65 73 74 72 6f 79 65 64 20 29 7b 0a 20 20 20  Destroyed ){.   
22428 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20 69 54     iLargest = iT
22429 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  ab;.    }.    fo
2242a 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
2242b 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2242c 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
2242d 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70      int iIdx = p
2242e 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20  Idx->tnum;.     
2242f 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70   assert( pIdx->p
22430 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
22431 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 69  chema );.      i
22432 66 28 20 28 69 44 65 73 74 72 6f 79 65 64 3d 3d  f( (iDestroyed==
22433 30 20 7c 7c 20 28 69 49 64 78 3c 69 44 65 73 74  0 || (iIdx<iDest
22434 72 6f 79 65 64 29 29 20 26 26 20 69 49 64 78 3e  royed)) && iIdx>
22435 69 4c 61 72 67 65 73 74 20 29 7b 0a 20 20 20 20  iLargest ){.    
22436 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20 69      iLargest = i
22437 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Idx;.      }.   
22438 20 7d 0a 20 20 20 20 69 66 28 20 69 4c 61 72 67   }.    if( iLarg
22439 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  est==0 ){.      
2243a 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65 6c 73  return;.    }els
2243b 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  e{.      int iDb
2243c 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2243d 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
2243e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2243f 61 29 3b 0a 20 20 20 20 20 20 64 65 73 74 72 6f  a);.      destro
22440 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
22441 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44 62 29  , iLargest, iDb)
22442 3b 0a 20 20 20 20 20 20 69 44 65 73 74 72 6f 79  ;.      iDestroy
22443 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b 0a 20  ed = iLargest;. 
22444 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
22445 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
22446 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
22447 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f  to do the work o
22448 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45 20 73  f a DROP TABLE s
22449 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70 4e 61  tatement..** pNa
2244a 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  me is the name o
2244b 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  f the table to b
2244c 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51  e dropped..*/.SQ
2244d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2244e 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62  d sqlite3DropTab
2244f 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  le(Parse *pParse
22450 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65  , SrcList *pName
22451 2c 20 69 6e 74 20 69 73 56 69 65 77 2c 20 69 6e  , int isView, in
22452 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 61 62 6c  t noErr){.  Tabl
22453 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 20  e *pTab;.  Vdbe 
22454 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *v;.  sqlite3 *d
22455 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
22456 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 66    int iDb;..  if
22457 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
22458 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
22459 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  ed ){.    goto e
2245a 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
2245b 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4e    }.  assert( pN
2245c 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ame->nSrc==1 );.
2245d 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
2245e 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72  LocateTable(pPar
2245f 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20 20 20  se, isView, .   
22460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22461 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e           pName->
22462 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d  a[0].zName, pNam
22463 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73  e->a[0].zDatabas
22464 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d  e);..  if( pTab=
22465 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 6f  =0 ){.    if( no
22466 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Err ){.      sql
22467 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70  ite3ErrorClear(p
22468 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Parse);.    }.  
22469 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
2246a 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 44  _table;.  }.  iD
2246b 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2246c 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
2246d 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
2246e 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
2246f 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
22470 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73  .  /* If pTab is
22471 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
22472 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74 43 6f  , call ViewGetCo
22473 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f 20 65  lumnNames() to e
22474 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20 69 73  nsure.  ** it is
22475 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20   initialized..  
22476 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  */.  if( IsVirtu
22477 61 6c 28 70 54 61 62 29 20 26 26 20 73 71 6c 69  al(pTab) && sqli
22478 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
22479 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
2247a 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ab) ){.    goto 
2247b 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
2247c 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
2247d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
2247e 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69  ZATION.  {.    i
2247f 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 6f 6e  nt code;.    con
22480 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20  st char *zTab = 
22481 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
22482 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  );.    const cha
22483 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  r *zDb = db->aDb
22484 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
22485 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
22486 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  g2 = 0;.    if( 
22487 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
22488 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
22489 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c  DELETE, zTab, 0,
2248a 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 67 6f   zDb)){.      go
2248b 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
2248c 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  le;.    }.    if
2248d 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
2248e 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
2248f 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a  DB && iDb==1 ){.
22490 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
22491 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
22492 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73  VIEW;.      }els
22493 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20  e{.        code 
22494 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49  = SQLITE_DROP_VI
22495 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e  EW;.      }.#ifn
22496 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22497 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
22498 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69 72   }else if( IsVir
22499 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
2249a 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
2249b 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0a 20  E_DROP_VTABLE;. 
2249c 20 20 20 20 20 7a 41 72 67 32 20 3d 20 70 54 61       zArg2 = pTa
2249d 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 3b 0a  b->pMod->zName;.
2249e 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65  #endif.    }else
2249f 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49  {.      if( !OMI
224a0 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
224a1 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  =1 ){.        co
224a2 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50  de = SQLITE_DROP
224a3 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20  _TEMP_TABLE;.   
224a4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
224a5 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f    code = SQLITE_
224a6 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20  DROP_TABLE;.    
224a7 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
224a8 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
224a9 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c  ck(pParse, code,
224aa 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 41   pTab->zName, zA
224ab 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  rg2, zDb) ){.   
224ac 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
224ad 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20  p_table;.    }. 
224ae 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
224af 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
224b0 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 70  SQLITE_DELETE, p
224b1 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a  Tab->zName, 0, z
224b2 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
224b3 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
224b4 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  e;.    }.  }.#en
224b5 64 69 66 0a 20 20 69 66 28 20 73 71 6c 69 74 65  dif.  if( sqlite
224b6 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e  3StrNICmp(pTab->
224b7 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22  zName, "sqlite_"
224b8 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  , 7)==0 ){.    s
224b9 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
224ba 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73  Parse, "table %s
224bb 20 6d 61 79 20 6e 6f 74 20 62 65 20 64 72 6f 70   may not be drop
224bc 70 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  ped", pTab->zNam
224bd 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  e);.    goto exi
224be 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
224bf 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
224c0 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 2f 2a  E_OMIT_VIEW.  /*
224c1 20 45 6e 73 75 72 65 20 44 52 4f 50 20 54 41 42   Ensure DROP TAB
224c2 4c 45 20 69 73 20 6e 6f 74 20 75 73 65 64 20 6f  LE is not used o
224c3 6e 20 61 20 76 69 65 77 2c 20 61 6e 64 20 44 52  n a view, and DR
224c4 4f 50 20 56 49 45 57 20 69 73 20 6e 6f 74 20 75  OP VIEW is not u
224c5 73 65 64 0a 20 20 2a 2a 20 6f 6e 20 61 20 74 61  sed.  ** on a ta
224c6 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ble..  */.  if( 
224c7 69 73 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e  isView && pTab->
224c8 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20  pSelect==0 ){.  
224c9 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
224ca 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 44  g(pParse, "use D
224cb 52 4f 50 20 54 41 42 4c 45 20 74 6f 20 64 65 6c  ROP TABLE to del
224cc 65 74 65 20 74 61 62 6c 65 20 25 73 22 2c 20 70  ete table %s", p
224cd 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
224ce 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
224cf 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28  table;.  }.  if(
224d0 20 21 69 73 56 69 65 77 20 26 26 20 70 54 61 62   !isView && pTab
224d1 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
224d2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
224d3 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 44 52  (pParse, "use DR
224d4 4f 50 20 56 49 45 57 20 74 6f 20 64 65 6c 65 74  OP VIEW to delet
224d5 65 20 76 69 65 77 20 25 73 22 2c 20 70 54 61 62  e view %s", pTab
224d6 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
224d7 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
224d8 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  le;.  }.#endif..
224d9 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
224da 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65  de to remove the
224db 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20   table from the 
224dc 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 2a  master table.  *
224dd 2a 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a  * on disk..  */.
224de 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
224df 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
224e0 69 66 28 20 76 20 29 7b 0a 20 20 20 20 54 72 69  if( v ){.    Tri
224e1 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a  gger *pTrigger;.
224e2 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64      Db *pDb = &d
224e3 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20  b->aDb[iDb];.   
224e4 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
224e5 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
224e6 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23 69  se, 1, iDb);..#i
224e7 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
224e8 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
224e9 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c     if( IsVirtual
224ea 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
224eb 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 20  if( v ){.       
224ec 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
224ed 70 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29  p0(v, OP_VBegin)
224ee 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
224ef 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44  #endif..    /* D
224f0 72 6f 70 20 61 6c 6c 20 74 72 69 67 67 65 72 73  rop all triggers
224f1 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
224f2 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
224f3 20 64 72 6f 70 70 65 64 2e 20 43 6f 64 65 0a 20   dropped. Code. 
224f4 20 20 20 2a 2a 20 69 73 20 67 65 6e 65 72 61 74     ** is generat
224f5 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 65 6e 74  ed to remove ent
224f6 72 69 65 73 20 66 72 6f 6d 20 73 71 6c 69 74 65  ries from sqlite
224f7 5f 6d 61 73 74 65 72 20 61 6e 64 2f 6f 72 0a 20  _master and/or. 
224f8 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f 74 65 6d     ** sqlite_tem
224f9 70 5f 6d 61 73 74 65 72 20 69 66 20 72 65 71 75  p_master if requ
224fa 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
224fb 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69   pTrigger = sqli
224fc 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70  te3TriggerList(p
224fd 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
224fe 20 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65    while( pTrigge
224ff 72 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  r ){.      asser
22500 74 28 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63  t( pTrigger->pSc
22501 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
22502 65 6d 61 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  ema || .        
22503 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68    pTrigger->pSch
22504 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e  ema==db->aDb[1].
22505 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20  pSchema );.     
22506 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
22507 67 65 72 50 74 72 28 70 50 61 72 73 65 2c 20 70  gerPtr(pParse, p
22508 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20  Trigger);.      
22509 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 69 67  pTrigger = pTrig
2250a 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ger->pNext;.    
2250b 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2250c 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
2250d 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 52 65 6d 6f  MENT.    /* Remo
2250e 76 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20 6f  ve any entries o
2250f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  f the sqlite_seq
22510 75 65 6e 63 65 20 74 61 62 6c 65 20 61 73 73 6f  uence table asso
22511 63 69 61 74 65 64 20 77 69 74 68 0a 20 20 20 20  ciated with.    
22512 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  ** the table bei
22513 6e 67 20 64 72 6f 70 70 65 64 2e 20 54 68 69 73  ng dropped. This
22514 20 69 73 20 64 6f 6e 65 20 62 65 66 6f 72 65 20   is done before 
22515 74 68 65 20 74 61 62 6c 65 20 69 73 20 64 72 6f  the table is dro
22516 70 70 65 64 0a 20 20 20 20 2a 2a 20 61 74 20 74  pped.    ** at t
22517 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c 2c 20  he btree level, 
22518 69 6e 20 63 61 73 65 20 74 68 65 20 73 71 6c 69  in case the sqli
22519 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
2251a 65 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 20 2a  e needs to.    *
2251b 2a 20 6d 6f 76 65 20 61 73 20 61 20 72 65 73 75  * move as a resu
2251c 6c 74 20 6f 66 20 74 68 65 20 64 72 6f 70 20 28  lt of the drop (
2251d 63 61 6e 20 68 61 70 70 65 6e 20 69 6e 20 61 75  can happen in au
2251e 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 29 2e  to-vacuum mode).
2251f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
22520 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
22521 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e   TF_Autoincremen
22522 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
22523 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
22524 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44  arse,.        "D
22525 45 4c 45 54 45 20 46 52 4f 4d 20 25 73 2e 73 71  ELETE FROM %s.sq
22526 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 57 48  lite_sequence WH
22527 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20  ERE name=%Q",.  
22528 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65        pDb->zName
22529 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20  , pTab->zName.  
2252a 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 23 65 6e      );.    }.#en
2252b 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70  dif..    /* Drop
2252c 20 61 6c 6c 20 53 51 4c 49 54 45 5f 4d 41 53 54   all SQLITE_MAST
2252d 45 52 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64  ER table and ind
2252e 65 78 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  ex entries that 
2252f 72 65 66 65 72 20 74 6f 20 74 68 65 0a 20 20 20  refer to the.   
22530 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65 20 70   ** table. The p
22531 72 6f 67 72 61 6d 20 6e 61 6d 65 20 6c 6f 6f 70  rogram name loop
22532 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 6d 61  s through the ma
22533 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 64  ster table and d
22534 65 6c 65 74 65 73 0a 20 20 20 20 2a 2a 20 65 76  eletes.    ** ev
22535 65 72 79 20 72 6f 77 20 74 68 61 74 20 72 65 66  ery row that ref
22536 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 20 6f  ers to a table o
22537 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20  f the same name 
22538 61 73 20 74 68 65 20 6f 6e 65 20 62 65 69 6e 67  as the one being
22539 0a 20 20 20 20 2a 2a 20 64 72 6f 70 70 65 64 2e  .    ** dropped.
2253a 20 54 72 69 67 67 65 72 73 20 61 72 65 20 68 61   Triggers are ha
2253b 6e 64 6c 65 64 20 73 65 70 65 72 61 74 65 6c 79  ndled seperately
2253c 20 62 65 63 61 75 73 65 20 61 20 74 72 69 67 67   because a trigg
2253d 65 72 20 63 61 6e 20 62 65 0a 20 20 20 20 2a 2a  er can be.    **
2253e 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20   created in the 
2253f 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 74 68  temp database th
22540 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 20 74  at refers to a t
22541 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65 72 0a  able in another.
22542 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e      ** database.
22543 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
22544 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
22545 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
22546 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
22547 25 73 20 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d  %s WHERE tbl_nam
22548 65 3d 25 51 20 61 6e 64 20 74 79 70 65 21 3d 27  e=%Q and type!='
22549 74 72 69 67 67 65 72 27 22 2c 0a 20 20 20 20 20  trigger'",.     
2254a 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 53     pDb->zName, S
2254b 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
2254c 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2254d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79  .    /* Drop any
2254e 20 73 74 61 74 69 73 74 69 63 73 20 66 72 6f 6d   statistics from
2254f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
22550 31 20 74 61 62 6c 65 2c 20 69 66 20 69 74 20 65  1 table, if it e
22551 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 69 66 28  xists */.    if(
22552 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
22553 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74  e(db, "sqlite_st
22554 61 74 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44  at1", db->aDb[iD
22555 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  b].zName) ){.   
22556 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
22557 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
22558 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
22559 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  OM %Q.sqlite_sta
2255a 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22  t1 WHERE tbl=%Q"
2255b 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54  , pDb->zName, pT
2255c 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20  ab->zName.      
2255d 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
2255e 28 20 21 69 73 56 69 65 77 20 26 26 20 21 49 73  ( !isView && !Is
2255f 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
22560 0a 20 20 20 20 20 20 64 65 73 74 72 6f 79 54 61  .      destroyTa
22561 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
22562 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
22563 20 52 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c   Remove the tabl
22564 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 53 51 4c  e entry from SQL
22565 69 74 65 27 73 20 69 6e 74 65 72 6e 61 6c 20 73  ite's internal s
22566 63 68 65 6d 61 20 61 6e 64 20 6d 6f 64 69 66 79  chema and modify
22567 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65  .    ** the sche
22568 6d 61 20 63 6f 6f 6b 69 65 2e 0a 20 20 20 20 2a  ma cookie..    *
22569 2f 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74  /.    if( IsVirt
2256a 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
2256b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2256c 64 4f 70 34 28 76 2c 20 4f 50 5f 56 44 65 73 74  dOp4(v, OP_VDest
2256d 72 6f 79 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  roy, iDb, 0, 0, 
2256e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pTab->zName, 0);
2256f 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
22570 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
22571 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 69 44  OP_DropTable, iD
22572 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a  b, 0, 0, pTab->z
22573 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71  Name, 0);.    sq
22574 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
22575 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
22576 20 20 7d 0a 20 20 73 71 6c 69 74 65 56 69 65 77    }.  sqliteView
22577 52 65 73 65 74 41 6c 6c 28 64 62 2c 20 69 44 62  ResetAll(db, iDb
22578 29 3b 0a 0a 65 78 69 74 5f 64 72 6f 70 5f 74 61  );..exit_drop_ta
22579 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  ble:.  sqlite3Sr
2257a 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
2257b 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pName);.}../*.**
2257c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2257d 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
2257e 65 20 61 20 6e 65 77 20 66 6f 72 65 69 67 6e 20  e a new foreign 
2257f 6b 65 79 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  key on the table
22580 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  .** currently un
22581 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
22582 2e 20 20 70 46 72 6f 6d 43 6f 6c 20 64 65 74 65  .  pFromCol dete
22583 72 6d 69 6e 65 73 20 77 68 69 63 68 20 63 6f 6c  rmines which col
22584 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 63  umns.** in the c
22585 75 72 72 65 6e 74 20 74 61 62 6c 65 20 70 6f 69  urrent table poi
22586 6e 74 20 74 6f 20 74 68 65 20 66 6f 72 65 69 67  nt to the foreig
22587 6e 20 6b 65 79 2e 20 20 49 66 20 70 46 72 6f 6d  n key.  If pFrom
22588 43 6f 6c 3d 3d 30 20 74 68 65 6e 0a 2a 2a 20 63  Col==0 then.** c
22589 6f 6e 6e 65 63 74 20 74 68 65 20 6b 65 79 20 74  onnect the key t
2258a 6f 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  o the last colum
2258b 6e 20 69 6e 73 65 72 74 65 64 2e 20 20 70 54 6f  n inserted.  pTo
2258c 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a   is the name of.
2258d 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 72 65 66  ** the table ref
2258e 65 72 72 65 64 20 74 6f 2e 20 20 70 54 6f 43 6f  erred to.  pToCo
2258f 6c 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 74  l is a list of t
22590 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6f 74 68  ables in the oth
22591 65 72 0a 2a 2a 20 70 54 6f 20 74 61 62 6c 65 20  er.** pTo table 
22592 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e  that the foreign
22593 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f 2e 20   key points to. 
22594 20 66 6c 61 67 73 20 63 6f 6e 74 61 69 6e 73 20   flags contains 
22595 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  all.** informati
22596 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6e  on about the con
22597 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
22598 20 61 6c 67 6f 72 69 74 68 6d 73 20 73 70 65 63   algorithms spec
22599 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20  ified.** in the 
2259a 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e 20 55 50  ON DELETE, ON UP
2259b 44 41 54 45 20 61 6e 64 20 4f 4e 20 49 4e 53 45  DATE and ON INSE
2259c 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a  RT clauses..**.*
2259d 2a 20 41 6e 20 46 4b 65 79 20 73 74 72 75 63 74  * An FKey struct
2259e 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 61  ure is created a
2259f 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  nd added to the 
225a0 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 0a  table currently.
225a1 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ** under constru
225a2 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 50 61  ction in the pPa
225a3 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 66  rse->pNewTable f
225a4 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ield..**.** The 
225a5 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 73  foreign key is s
225a6 65 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45  et for IMMEDIATE
225a7 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 41 20   processing.  A 
225a8 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 0a  subsequent call.
225a9 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 44 65 66  ** to sqlite3Def
225aa 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 29 20 6d  erForeignKey() m
225ab 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 69 73  ight change this
225ac 20 74 6f 20 44 45 46 45 52 52 45 44 2e 0a 2a 2f   to DEFERRED..*/
225ad 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
225ae 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
225af 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 0a 20 20  teForeignKey(.  
225b0 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
225b1 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
225b2 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
225b3 72 4c 69 73 74 20 2a 70 46 72 6f 6d 43 6f 6c 2c  rList *pFromCol,
225b4 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20    /* Columns in 
225b5 74 68 69 73 20 74 61 62 6c 65 20 74 68 61 74 20  this table that 
225b6 70 6f 69 6e 74 20 74 6f 20 6f 74 68 65 72 20 74  point to other t
225b7 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  able */.  Token 
225b8 2a 70 54 6f 2c 20 20 20 20 20 20 20 20 20 20 2f  *pTo,          /
225b9 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 74  * Name of the ot
225ba 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45  her table */.  E
225bb 78 70 72 4c 69 73 74 20 2a 70 54 6f 43 6f 6c 2c  xprList *pToCol,
225bc 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 69      /* Columns i
225bd 6e 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c  n the other tabl
225be 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
225bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
225c0 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69  onflict resoluti
225c1 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 2e 20 2a  on algorithms. *
225c2 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
225c3 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
225c4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
225c5 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
225c6 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 20 3d  .  FKey *pFKey =
225c7 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d   0;.  Table *p =
225c8 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
225c9 6c 65 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b  le;.  int nByte;
225ca 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
225cb 6e 43 6f 6c 3b 0a 20 20 63 68 61 72 20 2a 7a 3b  nCol;.  char *z;
225cc 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 21  ..  assert( pTo!
225cd 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30  =0 );.  if( p==0
225ce 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
225cf 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56   || IN_DECLARE_V
225d0 54 41 42 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e  TAB ) goto fk_en
225d1 64 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 43 6f  d;.  if( pFromCo
225d2 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  l==0 ){.    int 
225d3 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31  iCol = p->nCol-1
225d4 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30  ;.    if( iCol<0
225d5 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a   ) goto fk_end;.
225d6 20 20 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 26      if( pToCol &
225d7 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21  & pToCol->nExpr!
225d8 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =1 ){.      sqli
225d9 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
225da 73 65 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79  se, "foreign key
225db 20 6f 6e 20 25 73 22 0a 20 20 20 20 20 20 20 20   on %s".        
225dc 20 22 20 73 68 6f 75 6c 64 20 72 65 66 65 72 65   " should refere
225dd 6e 63 65 20 6f 6e 6c 79 20 6f 6e 65 20 63 6f 6c  nce only one col
225de 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 25 54 22  umn of table %T"
225df 2c 0a 20 20 20 20 20 20 20 20 20 70 2d 3e 61 43  ,.         p->aC
225e0 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20  ol[iCol].zName, 
225e1 70 54 6f 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  pTo);.      goto
225e2 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20   fk_end;.    }. 
225e3 20 20 20 6e 43 6f 6c 20 3d 20 31 3b 0a 20 20 7d     nCol = 1;.  }
225e4 65 6c 73 65 20 69 66 28 20 70 54 6f 43 6f 6c 20  else if( pToCol 
225e5 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72  && pToCol->nExpr
225e6 21 3d 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70  !=pFromCol->nExp
225e7 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
225e8 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
225e9 0a 20 20 20 20 20 20 20 20 22 6e 75 6d 62 65 72  .        "number
225ea 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 66   of columns in f
225eb 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f 65 73 20  oreign key does 
225ec 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e 75  not match the nu
225ed 6d 62 65 72 20 6f 66 20 22 0a 20 20 20 20 20 20  mber of ".      
225ee 20 20 22 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68    "columns in th
225ef 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62  e referenced tab
225f0 6c 65 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66  le");.    goto f
225f1 6b 5f 65 6e 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a  k_end;.  }else{.
225f2 20 20 20 20 6e 43 6f 6c 20 3d 20 70 46 72 6f 6d      nCol = pFrom
225f3 43 6f 6c 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d 0a  Col->nExpr;.  }.
225f4 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66    nByte = sizeof
225f5 28 2a 70 46 4b 65 79 29 20 2b 20 28 6e 43 6f 6c  (*pFKey) + (nCol
225f6 2d 31 29 2a 73 69 7a 65 6f 66 28 70 46 4b 65 79  -1)*sizeof(pFKey
225f7 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b 20 70 54 6f  ->aCol[0]) + pTo
225f8 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69 66 28 20 70  ->n + 1;.  if( p
225f9 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72  ToCol ){.    for
225fa 28 69 3d 30 3b 20 69 3c 70 54 6f 43 6f 6c 2d 3e  (i=0; i<pToCol->
225fb 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
225fc 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69     nByte += sqli
225fd 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 6f 43  te3Strlen30(pToC
225fe 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20  ol->a[i].zName) 
225ff 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  + 1;.    }.  }. 
22600 20 70 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33   pFKey = sqlite3
22601 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
22602 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20   nByte );.  if( 
22603 70 46 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20  pFKey==0 ){.    
22604 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d  goto fk_end;.  }
22605 0a 20 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 20  .  pFKey->pFrom 
22606 3d 20 70 3b 0a 20 20 70 46 4b 65 79 2d 3e 70 4e  = p;.  pFKey->pN
22607 65 78 74 46 72 6f 6d 20 3d 20 70 2d 3e 70 46 4b  extFrom = p->pFK
22608 65 79 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a  ey;.  z = (char*
22609 29 26 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6e 43  )&pFKey->aCol[nC
2260a 6f 6c 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e 7a 54  ol];.  pFKey->zT
2260b 6f 20 3d 20 7a 3b 0a 20 20 6d 65 6d 63 70 79 28  o = z;.  memcpy(
2260c 7a 2c 20 70 54 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e  z, pTo->z, pTo->
2260d 6e 29 3b 0a 20 20 7a 5b 70 54 6f 2d 3e 6e 5d 20  n);.  z[pTo->n] 
2260e 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  = 0;.  sqlite3De
2260f 71 75 6f 74 65 28 7a 29 3b 0a 20 20 7a 20 2b 3d  quote(z);.  z +=
22610 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 70 46 4b   pTo->n+1;.  pFK
22611 65 79 2d 3e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b  ey->nCol = nCol;
22612 0a 20 20 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d  .  if( pFromCol=
22613 3d 30 20 29 7b 0a 20 20 20 20 70 46 4b 65 79 2d  =0 ){.    pFKey-
22614 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 20 3d  >aCol[0].iFrom =
22615 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 7d 65   p->nCol-1;.  }e
22616 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  lse{.    for(i=0
22617 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
22618 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
22619 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d     for(j=0; j<p-
2261a 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  >nCol; j++){.   
2261b 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2261c 53 74 72 49 43 6d 70 28 70 2d 3e 61 43 6f 6c 5b  StrICmp(p->aCol[
2261d 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 46 72 6f 6d 43  j].zName, pFromC
2261e 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d  ol->a[i].zName)=
2261f 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
22620 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 69  pFKey->aCol[i].i
22621 46 72 6f 6d 20 3d 20 6a 3b 0a 20 20 20 20 20 20  From = j;.      
22622 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
22623 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
22624 20 20 20 69 66 28 20 6a 3e 3d 70 2d 3e 6e 43 6f     if( j>=p->nCo
22625 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  l ){.        sql
22626 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
22627 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
22628 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6c 75 6d 6e 20  "unknown column 
22629 5c 22 25 73 5c 22 20 69 6e 20 66 6f 72 65 69 67  \"%s\" in foreig
2262a 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e  n key definition
2262b 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 70 46  ", .          pF
2262c 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61  romCol->a[i].zNa
2262d 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  me);.        got
2262e 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 20 20  o fk_end;.      
2262f 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  }.    }.  }.  if
22630 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20  ( pToCol ){.    
22631 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
22632 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74   i++){.      int
22633 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
22634 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69  en30(pToCol->a[i
22635 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ].zName);.      
22636 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a  pFKey->aCol[i].z
22637 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20 6d  Col = z;.      m
22638 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d  emcpy(z, pToCol-
22639 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b  >a[i].zName, n);
2263a 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b  .      z[n] = 0;
2263b 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b  .      z += n+1;
2263c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b  .    }.  }.  pFK
2263d 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d  ey->isDeferred =
2263e 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 64 65 6c   0;.  pFKey->del
2263f 65 74 65 43 6f 6e 66 20 3d 20 28 75 38 29 28 66  eteConf = (u8)(f
22640 6c 61 67 73 20 26 20 30 78 66 66 29 3b 0a 20 20  lags & 0xff);.  
22641 70 46 4b 65 79 2d 3e 75 70 64 61 74 65 43 6f 6e  pFKey->updateCon
22642 66 20 3d 20 28 75 38 29 28 28 66 6c 61 67 73 20  f = (u8)((flags 
22643 3e 3e 20 38 20 29 20 26 20 30 78 66 66 29 3b 0a  >> 8 ) & 0xff);.
22644 20 20 70 46 4b 65 79 2d 3e 69 6e 73 65 72 74 43    pFKey->insertC
22645 6f 6e 66 20 3d 20 28 75 38 29 28 28 66 6c 61 67  onf = (u8)((flag
22646 73 20 3e 3e 20 31 36 20 29 20 26 20 30 78 66 66  s >> 16 ) & 0xff
22647 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68  );..  /* Link th
22648 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f  e foreign key to
22649 20 74 68 65 20 74 61 62 6c 65 20 61 73 20 74 68   the table as th
2264a 65 20 6c 61 73 74 20 73 74 65 70 2e 0a 20 20 2a  e last step..  *
2264b 2f 0a 20 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70  /.  p->pFKey = p
2264c 46 4b 65 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20  FKey;.  pFKey = 
2264d 30 3b 0a 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71  0;..fk_end:.  sq
2264e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2264f 70 46 4b 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f  pFKey);.#endif /
22650 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
22651 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
22652 45 59 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  EY) */.  sqlite3
22653 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
22654 62 2c 20 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20  b, pFromCol);.  
22655 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
22656 65 6c 65 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c  elete(db, pToCol
22657 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
22658 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
22659 65 64 20 77 68 65 6e 20 61 6e 20 49 4e 49 54 49  ed when an INITI
2265a 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 6f  ALLY IMMEDIATE o
2265b 72 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  r INITIALLY DEFE
2265c 52 52 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69  RRED.** clause i
2265d 73 20 73 65 65 6e 20 61 73 20 70 61 72 74 20 6f  s seen as part o
2265e 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  f a foreign key 
2265f 64 65 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65  definition.  The
22660 20 69 73 44 65 66 65 72 72 65 64 0a 2a 2a 20 70   isDeferred.** p
22661 61 72 61 6d 65 74 65 72 20 69 73 20 31 20 66 6f  arameter is 1 fo
22662 72 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  r INITIALLY DEFE
22663 52 52 45 44 20 61 6e 64 20 30 20 66 6f 72 20 49  RRED and 0 for I
22664 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41  NITIALLY IMMEDIA
22665 54 45 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61 76  TE..** The behav
22666 69 6f 72 20 6f 66 20 74 68 65 20 6d 6f 73 74 20  ior of the most 
22667 72 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64  recently created
22668 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20   foreign key is 
22669 61 64 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f  adjusted.** acco
2266a 72 64 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  rdingly..*/.SQLI
2266b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2266c 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65  sqlite3DeferFore
2266d 69 67 6e 4b 65 79 28 50 61 72 73 65 20 2a 70 50  ignKey(Parse *pP
2266e 61 72 73 65 2c 20 69 6e 74 20 69 73 44 65 66 65  arse, int isDefe
2266f 72 72 65 64 29 7b 0a 23 69 66 6e 64 65 66 20 53  rred){.#ifndef S
22670 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
22671 47 4e 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a  GN_KEY.  Table *
22672 70 54 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46  pTab;.  FKey *pF
22673 4b 65 79 3b 0a 20 20 69 66 28 20 28 70 54 61 62  Key;.  if( (pTab
22674 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
22675 61 62 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b  able)==0 || (pFK
22676 65 79 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79  ey = pTab->pFKey
22677 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  )==0 ) return;. 
22678 20 61 73 73 65 72 74 28 20 69 73 44 65 66 65 72   assert( isDefer
22679 72 65 64 3d 3d 30 20 7c 7c 20 69 73 44 65 66 65  red==0 || isDefe
2267a 72 72 65 64 3d 3d 31 20 29 3b 0a 20 20 70 46 4b  rred==1 );.  pFK
2267b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d  ey->isDeferred =
2267c 20 28 75 38 29 69 73 44 65 66 65 72 72 65 64 3b   (u8)isDeferred;
2267d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
2267e 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2267f 68 61 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61  hat will erase a
22680 6e 64 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20  nd refill index 
22681 2a 70 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a  *pIdx.  This is.
22682 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69  ** used to initi
22683 61 6c 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72  alize a newly cr
22684 65 61 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74  eated index or t
22685 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a  o recompute the.
22686 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e  ** content of an
22687 20 69 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e   index in respon
22688 73 65 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20  se to a REINDEX 
22689 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69  command..**.** i
2268a 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73  f memRootPage is
2268b 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69   not negative, i
2268c 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
2268d 20 69 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a   index is newly.
2268e 2a 2a 20 63 72 65 61 74 65 64 2e 20 20 54 68 65  ** created.  The
2268f 20 72 65 67 69 73 74 65 72 20 73 70 65 63 69 66   register specif
22690 69 65 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61  ied by memRootPa
22691 67 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a  ge contains the.
22692 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  ** root page num
22693 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ber of the index
22694 2e 20 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67  .  If memRootPag
22695 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74  e is negative, t
22696 68 65 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78  hen.** the index
22697 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
22698 61 6e 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61  and must be clea
22699 72 65 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67  red before being
2269a 20 72 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a   refilled and.**
2269b 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
2269c 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64  umber of the ind
2269d 65 78 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d  ex is taken from
2269e 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a   pIndex->tnum..*
2269f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
226a0 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78  lite3RefillIndex
226a1 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
226a2 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69  Index *pIndex, i
226a3 6e 74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b  nt memRootPage){
226a4 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d  .  Table *pTab =
226a5 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b   pIndex->pTable;
226a6 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
226a7 68 61 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a  hat is indexed *
226a8 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70  /.  int iTab = p
226a9 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20  Parse->nTab++;  
226aa 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73     /* Btree curs
226ab 6f 72 20 75 73 65 64 20 66 6f 72 20 70 54 61 62  or used for pTab
226ac 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d   */.  int iIdx =
226ad 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
226ae 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75       /* Btree cu
226af 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49  rsor used for pI
226b0 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64  ndex */.  int ad
226b1 64 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  dr1;            
226b2 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
226b3 65 73 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f  ess of top of lo
226b4 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d  op */.  int tnum
226b5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
226b6 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70         /* Root p
226b7 61 67 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a  age of index */.
226b8 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
226b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226ba 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
226bb 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74  e into this virt
226bc 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20  ual machine */. 
226bd 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20   KeyInfo *pKey; 
226be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226bf 2f 2a 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69  /* KeyInfo for i
226c0 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ndex */.  int re
226c1 67 49 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20  gIdxKey;        
226c2 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
226c3 73 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67  sters containing
226c4 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a   the index key *
226c5 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72  /.  int regRecor
226c6 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
226c7 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
226c8 6f 6c 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65  olding assemblie
226c9 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a  d index record *
226ca 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
226cb 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
226cc 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
226cd 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
226ce 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c  .  int iDb = sql
226cf 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
226d0 78 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53  x(db, pIndex->pS
226d1 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66  chema);..#ifndef
226d2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
226d3 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28  HORIZATION.  if(
226d4 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
226d5 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
226d6 5f 52 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78  _REINDEX, pIndex
226d7 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20  ->zName, 0,.    
226d8 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a    db->aDb[iDb].z
226d9 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65  Name ) ){.    re
226da 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
226db 0a 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61  ..  /* Require a
226dc 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74   write-lock on t
226dd 68 65 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66  he table to perf
226de 6f 72 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69  orm this operati
226df 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54  on */.  sqlite3T
226e0 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
226e1 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
226e2 2c 20 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 1, pTab->zName
226e3 29 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  );..  v = sqlite
226e4 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
226e5 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72  ;.  if( v==0 ) r
226e6 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d  eturn;.  if( mem
226e7 52 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20  RootPage>=0 ){. 
226e8 20 20 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f     tnum = memRoo
226e9 74 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  tPage;.  }else{.
226ea 20 20 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65      tnum = pInde
226eb 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c  x->tnum;.    sql
226ec 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
226ed 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d  , OP_Clear, tnum
226ee 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b  , iDb);.  }.  pK
226ef 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
226f0 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
226f1 20 70 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69   pIndex);.  sqli
226f2 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
226f3 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69   OP_OpenWrite, i
226f4 49 64 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20  Idx, tnum, iDb, 
226f5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
226f6 20 20 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65       (char *)pKe
226f7 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  y, P4_KEYINFO_HA
226f8 4e 44 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65  NDOFF);.  if( me
226f9 6d 52 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a  mRootPage>=0 ){.
226fa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
226fb 68 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20  hangeP5(v, 1);. 
226fc 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e   }.  sqlite3Open
226fd 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54  Table(pParse, iT
226fe 61 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f  ab, iDb, pTab, O
226ff 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61  P_OpenRead);.  a
22700 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
22701 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
22702 65 77 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b  ewind, iTab, 0);
22703 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73  .  regRecord = s
22704 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
22705 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49  (pParse);.  regI
22706 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47  dxKey = sqlite3G
22707 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28  enerateIndexKey(
22708 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20  pParse, pIndex, 
22709 69 54 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c  iTab, regRecord,
2270a 20 31 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65   1);.  if( pInde
2270b 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
2270c 6f 6e 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  one ){.    const
2270d 20 69 6e 74 20 72 65 67 52 6f 77 69 64 20 3d 20   int regRowid = 
2270e 72 65 67 49 64 78 4b 65 79 20 2b 20 70 49 6e 64  regIdxKey + pInd
2270f 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20  ex->nColumn;.   
22710 20 63 6f 6e 73 74 20 69 6e 74 20 6a 32 20 3d 20   const int j2 = 
22711 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
22712 6e 74 41 64 64 72 28 76 29 20 2b 20 32 3b 0a 20  ntAddr(v) + 2;. 
22713 20 20 20 76 6f 69 64 20 2a 20 63 6f 6e 73 74 20     void * const 
22714 70 52 65 67 4b 65 79 20 3d 20 53 51 4c 49 54 45  pRegKey = SQLITE
22715 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67 49  _INT_TO_PTR(regI
22716 64 78 4b 65 79 29 3b 0a 0a 20 20 20 20 2f 2a 20  dxKey);..    /* 
22717 54 68 65 20 72 65 67 69 73 74 65 72 73 20 61 63  The registers ac
22718 63 65 73 73 65 64 20 62 79 20 74 68 65 20 4f 50  cessed by the OP
22719 5f 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 65  _IsUnique opcode
2271a 20 77 65 72 65 20 61 6c 6c 6f 63 61 74 65 64 0a   were allocated.
2271b 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 73 71 6c      ** using sql
2271c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
2271d 28 29 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65  () inside of the
2271e 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
2271f 49 6e 64 65 78 4b 65 79 28 29 0a 20 20 20 20 2a  IndexKey().    *
22720 2a 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 4a 75  * call above. Ju
22721 73 74 20 62 65 66 6f 72 65 20 74 68 61 74 20 66  st before that f
22722 75 6e 63 74 69 6f 6e 20 77 61 73 20 66 72 65 65  unction was free
22723 64 20 74 68 65 79 20 77 65 72 65 20 72 65 6c 65  d they were rele
22724 61 73 65 64 0a 20 20 20 20 2a 2a 20 28 6d 61 64  ased.    ** (mad
22725 65 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74  e available to t
22726 68 65 20 63 6f 6d 70 69 6c 65 72 20 66 6f 72 20  he compiler for 
22727 72 65 75 73 65 29 20 75 73 69 6e 67 20 0a 20 20  reuse) using .  
22728 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 6c 65    ** sqlite3Rele
22729 61 73 65 54 65 6d 70 52 61 6e 67 65 28 29 2e 20  aseTempRange(). 
2272a 53 6f 20 69 6e 20 73 6f 6d 65 20 77 61 79 73 20  So in some ways 
2272b 68 61 76 69 6e 67 20 74 68 65 20 4f 50 5f 49 73  having the OP_Is
2272c 55 6e 69 71 75 65 0a 20 20 20 20 2a 2a 20 6f 70  Unique.    ** op
2272d 63 6f 64 65 20 75 73 65 20 74 68 65 20 76 61 6c  code use the val
2272e 75 65 73 20 73 74 6f 72 65 64 20 77 69 74 68 69  ues stored withi
2272f 6e 20 73 65 65 6d 73 20 64 61 6e 67 65 72 6f 75  n seems dangerou
22730 73 2e 20 48 6f 77 65 76 65 72 2c 20 73 69 6e 63  s. However, sinc
22731 65 0a 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20  e.    ** we can 
22732 62 65 20 73 75 72 65 20 74 68 61 74 20 6e 6f 20  be sure that no 
22733 6f 74 68 65 72 20 74 65 6d 70 20 72 65 67 69 73  other temp regis
22734 74 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 61  ters have been a
22735 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a 2a 20  llocated.    ** 
22736 73 69 6e 63 65 20 73 71 6c 69 74 65 33 52 65 6c  since sqlite3Rel
22737 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 29 20  easeTempRange() 
22738 77 61 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69  was called, it i
22739 73 20 73 61 66 65 20 74 6f 20 64 6f 20 73 6f 2e  s safe to do so.
2273a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
2273b 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2273c 20 4f 50 5f 49 73 55 6e 69 71 75 65 2c 20 69 49   OP_IsUnique, iI
2273d 64 78 2c 20 6a 32 2c 20 72 65 67 52 6f 77 69 64  dx, j2, regRowid
2273e 2c 20 70 52 65 67 4b 65 79 2c 20 50 34 5f 49 4e  , pRegKey, P4_IN
2273f 54 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  T32);.    sqlite
22740 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
22741 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43  P_Halt, SQLITE_C
22742 4f 4e 53 54 52 41 49 4e 54 2c 20 4f 45 5f 41 62  ONSTRAINT, OE_Ab
22743 6f 72 74 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  ort, 0,.        
22744 20 20 20 20 20 20 20 20 20 20 20 20 22 69 6e 64              "ind
22745 65 78 65 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65  exed columns are
22746 20 6e 6f 74 20 75 6e 69 71 75 65 22 2c 20 50 34   not unique", P4
22747 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20  _STATIC);.  }.  
22748 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22749 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
2274a 74 2c 20 69 49 64 78 2c 20 72 65 67 52 65 63 6f  t, iIdx, regReco
2274b 72 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  rd);.  sqlite3Vd
2274c 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
2274d 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55  FLAG_USESEEKRESU
2274e 4c 54 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  LT);.  sqlite3Re
2274f 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
22750 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b  rse, regRecord);
22751 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
22752 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
22753 20 69 54 61 62 2c 20 61 64 64 72 31 2b 31 29 3b   iTab, addr1+1);
22754 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
22755 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
22756 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
22757 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
22758 65 2c 20 69 54 61 62 29 3b 0a 20 20 73 71 6c 69  e, iTab);.  sqli
22759 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2275a 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 29   OP_Close, iIdx)
2275b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
2275c 65 20 61 20 6e 65 77 20 69 6e 64 65 78 20 66 6f  e a new index fo
2275d 72 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 2e 20  r an SQL table. 
2275e 20 70 4e 61 6d 65 31 2e 70 4e 61 6d 65 32 20 69   pName1.pName2 i
2275f 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
22760 65 20 69 6e 64 65 78 20 0a 2a 2a 20 61 6e 64 20  e index .** and 
22761 70 54 62 6c 4c 69 73 74 20 69 73 20 74 68 65 20  pTblList is the 
22762 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
22763 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  e that is to be 
22764 69 6e 64 65 78 65 64 2e 20 20 42 6f 74 68 20 77  indexed.  Both w
22765 69 6c 6c 20 0a 2a 2a 20 62 65 20 4e 55 4c 4c 20  ill .** be NULL 
22766 66 6f 72 20 61 20 70 72 69 6d 61 72 79 20 6b 65  for a primary ke
22767 79 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 74 68  y or an index th
22768 61 74 20 69 73 20 63 72 65 61 74 65 64 20 74 6f  at is created to
22769 20 73 61 74 69 73 66 79 20 61 0a 2a 2a 20 55 4e   satisfy a.** UN
2276a 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e  IQUE constraint.
2276b 20 20 49 66 20 70 54 61 62 6c 65 20 61 6e 64 20    If pTable and 
2276c 70 49 6e 64 65 78 20 61 72 65 20 4e 55 4c 4c 2c  pIndex are NULL,
2276d 20 75 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65   use pParse->pNe
2276e 77 54 61 62 6c 65 0a 2a 2a 20 61 73 20 74 68 65  wTable.** as the
2276f 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   table to be ind
22770 65 78 65 64 2e 20 20 70 50 61 72 73 65 2d 3e 70  exed.  pParse->p
22771 4e 65 77 54 61 62 6c 65 20 69 73 20 61 20 74 61  NewTable is a ta
22772 62 6c 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 63  ble that is.** c
22773 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63  urrently being c
22774 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 61 20  onstructed by a 
22775 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
22776 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c  tement..**.** pL
22777 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66  ist is a list of
22778 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69   columns to be i
22779 6e 64 65 78 65 64 2e 20 20 70 4c 69 73 74 20 77  ndexed.  pList w
2277a 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 66 20 74  ill be NULL if t
2277b 68 69 73 0a 2a 2a 20 69 73 20 61 20 70 72 69 6d  his.** is a prim
2277c 61 72 79 20 6b 65 79 20 6f 72 20 75 6e 69 71 75  ary key or uniqu
2277d 65 2d 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20  e-constraint on 
2277e 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
2277f 63 6f 6c 75 6d 6e 20 61 64 64 65 64 0a 2a 2a 20  column added.** 
22780 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
22781 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
22782 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a  struction.  .*/.
22783 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
22784 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
22785 65 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  eIndex(.  Parse 
22786 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
22787 41 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  All information 
22788 61 62 6f 75 74 20 74 68 69 73 20 70 61 72 73 65  about this parse
22789 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
2278a 6d 65 31 2c 20 20 20 20 20 2f 2a 20 46 69 72 73  me1,     /* Firs
2278b 74 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20  t part of index 
2278c 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c  name. May be NUL
2278d 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  L */.  Token *pN
2278e 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 53 65 63  ame2,     /* Sec
2278f 6f 6e 64 20 70 61 72 74 20 6f 66 20 69 6e 64 65  ond part of inde
22790 78 20 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e  x name. May be N
22791 55 4c 4c 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ULL */.  SrcList
22792 20 2a 70 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54   *pTblName, /* T
22793 61 62 6c 65 20 74 6f 20 69 6e 64 65 78 2e 20 55  able to index. U
22794 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  se pParse->pNewT
22795 61 62 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 45  able if 0 */.  E
22796 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
22797 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 63    /* A list of c
22798 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64  olumns to be ind
22799 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  exed */.  int on
2279a 45 72 72 6f 72 2c 20 20 20 20 20 20 20 2f 2a 20  Error,       /* 
2279b 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e  OE_Abort, OE_Ign
2279c 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c  ore, OE_Replace,
2279d 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20   or OE_None */. 
2279e 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20   Token *pStart, 
2279f 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54      /* The CREAT
227a0 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67  E token that beg
227a1 69 6e 73 20 74 68 69 73 20 73 74 61 74 65 6d 65  ins this stateme
227a2 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
227a3 45 6e 64 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  End,       /* Th
227a4 65 20 22 29 22 20 74 68 61 74 20 63 6c 6f 73 65  e ")" that close
227a5 73 20 74 68 65 20 43 52 45 41 54 45 20 49 4e 44  s the CREATE IND
227a6 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  EX statement */.
227a7 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 2c    int sortOrder,
227a8 20 20 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72 64       /* Sort ord
227a9 65 72 20 6f 66 20 70 72 69 6d 61 72 79 20 6b 65  er of primary ke
227aa 79 20 77 68 65 6e 20 70 4c 69 73 74 3d 3d 4e 55  y when pList==NU
227ab 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 66 4e 6f  LL */.  int ifNo
227ac 74 45 78 69 73 74 20 20 20 20 20 2f 2a 20 4f 6d  tExist     /* Om
227ad 69 74 20 65 72 72 6f 72 20 69 66 20 69 6e 64 65  it error if inde
227ae 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  x already exists
227af 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
227b0 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 2f 2a  pTab = 0;     /*
227b1 20 54 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   Table to be ind
227b2 65 78 65 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20  exed */.  Index 
227b3 2a 70 49 6e 64 65 78 20 3d 20 30 3b 20 20 20 2f  *pIndex = 0;   /
227b4 2a 20 54 68 65 20 69 6e 64 65 78 20 74 6f 20 62  * The index to b
227b5 65 20 63 72 65 61 74 65 64 20 2a 2f 0a 20 20 63  e created */.  c
227b6 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20  har *zName = 0; 
227b7 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
227b8 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  he index */.  in
227b9 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
227ba 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
227bb 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e  characters in zN
227bc 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  ame */.  int i, 
227bd 6a 3b 0a 20 20 54 6f 6b 65 6e 20 6e 75 6c 6c 49  j;.  Token nullI
227be 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6b  d;        /* Fak
227bf 65 20 74 6f 6b 65 6e 20 66 6f 72 20 61 6e 20 65  e token for an e
227c0 6d 70 74 79 20 49 44 20 6c 69 73 74 20 2a 2f 0a  mpty ID list */.
227c1 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 20    DbFixer sFix; 
227c2 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 73         /* For as
227c3 73 69 67 6e 69 6e 67 20 64 61 74 61 62 61 73 65  signing database
227c4 20 6e 61 6d 65 73 20 74 6f 20 70 54 61 62 6c 65   names to pTable
227c5 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72   */.  int sortOr
227c6 64 65 72 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 20  derMask;   /* 1 
227c7 74 6f 20 68 6f 6e 6f 72 20 44 45 53 43 20 69 6e  to honor DESC in
227c8 20 69 6e 64 65 78 2e 20 20 30 20 74 6f 20 69 67   index.  0 to ig
227c9 6e 6f 72 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  nore. */.  sqlit
227ca 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
227cb 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 20  >db;.  Db *pDb; 
227cc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
227cd 68 65 20 73 70 65 63 69 66 69 63 20 74 61 62 6c  he specific tabl
227ce 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
227cf 20 69 6e 64 65 78 65 64 20 64 61 74 61 62 61 73   indexed databas
227d0 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  e */.  int iDb; 
227d1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
227d2 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
227d3 62 61 73 65 20 74 68 61 74 20 69 73 20 62 65 69  base that is bei
227d4 6e 67 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  ng written */.  
227d5 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30  Token *pName = 0
227d6 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66  ;    /* Unqualif
227d7 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  ied name of the 
227d8 69 6e 64 65 78 20 74 6f 20 63 72 65 61 74 65 20  index to create 
227d9 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  */.  struct Expr
227da 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 69 73 74  List_item *pList
227db 49 74 65 6d 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f  Item; /* For loo
227dc 70 69 6e 67 20 6f 76 65 72 20 70 4c 69 73 74 20  ping over pList 
227dd 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20  */.  int nCol;. 
227de 20 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b   int nExtra = 0;
227df 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b  .  char *zExtra;
227e0 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
227e1 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
227e2 6f 63 46 61 69 6c 65 64 20 7c 7c 20 49 4e 5f 44  ocFailed || IN_D
227e3 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20  ECLARE_VTAB ){. 
227e4 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
227e5 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 0a  ate_index;.  }..
227e6 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74    /*.  ** Find t
227e7 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73  he table that is
227e8 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
227e9 20 52 65 74 75 72 6e 20 65 61 72 6c 79 20 69 66   Return early if
227ea 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f   not found..  */
227eb 0a 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21  .  if( pTblName!
227ec 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 55 73  =0 ){..    /* Us
227ed 65 20 74 68 65 20 74 77 6f 2d 70 61 72 74 20 69  e the two-part i
227ee 6e 64 65 78 20 6e 61 6d 65 20 74 6f 20 64 65 74  ndex name to det
227ef 65 72 6d 69 6e 65 20 74 68 65 20 64 61 74 61 62  ermine the datab
227f0 61 73 65 20 0a 20 20 20 20 2a 2a 20 74 6f 20 73  ase .    ** to s
227f1 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 74 61  earch for the ta
227f2 62 6c 65 2e 20 27 46 69 78 27 20 74 68 65 20 74  ble. 'Fix' the t
227f3 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68 69  able name to thi
227f4 73 20 64 62 0a 20 20 20 20 2a 2a 20 62 65 66 6f  s db.    ** befo
227f5 72 65 20 6c 6f 6f 6b 69 6e 67 20 75 70 20 74 68  re looking up th
227f6 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a  e table..    */.
227f7 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d      assert( pNam
227f8 65 31 20 26 26 20 70 4e 61 6d 65 32 20 29 3b 0a  e1 && pName2 );.
227f9 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
227fa 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61  3TwoPartName(pPa
227fb 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61  rse, pName1, pNa
227fc 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20  me2, &pName);.  
227fd 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 67 6f    if( iDb<0 ) go
227fe 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
227ff 6e 64 65 78 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ndex;..#ifndef S
22800 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44  QLITE_OMIT_TEMPD
22801 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  B.    /* If the 
22802 69 6e 64 65 78 20 6e 61 6d 65 20 77 61 73 20 75  index name was u
22803 6e 71 75 61 6c 69 66 69 65 64 2c 20 63 68 65 63  nqualified, chec
22804 6b 20 69 66 20 74 68 65 20 74 68 65 20 74 61 62  k if the the tab
22805 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 61 20 74  le.    ** is a t
22806 65 6d 70 20 74 61 62 6c 65 2e 20 49 66 20 73 6f  emp table. If so
22807 2c 20 73 65 74 20 74 68 65 20 64 61 74 61 62 61  , set the databa
22808 73 65 20 74 6f 20 31 2e 20 44 6f 20 6e 6f 74 20  se to 1. Do not 
22809 64 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69  do this.    ** i
2280a 66 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 20 61  f initialising a
2280b 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
2280c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2280d 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
2280e 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  ){.      pTab = 
2280f 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
22810 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 62  okup(pParse, pTb
22811 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66  lName);.      if
22812 28 20 70 4e 61 6d 65 32 20 26 26 20 70 4e 61 6d  ( pName2 && pNam
22813 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62  e2->n==0 && pTab
22814 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d   && pTab->pSchem
22815 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  a==db->aDb[1].pS
22816 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20  chema ){.       
22817 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20   iDb = 1;.      
22818 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
22819 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
2281a 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50  ixInit(&sFix, pP
2281b 61 72 73 65 2c 20 69 44 62 2c 20 22 69 6e 64 65  arse, iDb, "inde
2281c 78 22 2c 20 70 4e 61 6d 65 29 20 26 26 0a 20 20  x", pName) &&.  
2281d 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 69 78        sqlite3Fix
2281e 53 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70  SrcList(&sFix, p
2281f 54 62 6c 4e 61 6d 65 29 0a 20 20 20 20 29 7b 0a  TblName).    ){.
22820 20 20 20 20 20 20 2f 2a 20 42 65 63 61 75 73 65        /* Because
22821 20 74 68 65 20 70 61 72 73 65 72 20 63 6f 6e 73   the parser cons
22822 74 72 75 63 74 73 20 70 54 62 6c 4e 61 6d 65 20  tructs pTblName 
22823 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 69 64  from a single id
22824 65 6e 74 69 66 69 65 72 2c 0a 20 20 20 20 20 20  entifier,.      
22825 2a 2a 20 73 71 6c 69 74 65 33 46 69 78 53 72 63  ** sqlite3FixSrc
22826 4c 69 73 74 20 63 61 6e 20 6e 65 76 65 72 20 66  List can never f
22827 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73  ail. */.      as
22828 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20  sert(0);.    }. 
22829 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
2282a 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
2282b 72 73 65 2c 20 30 2c 20 70 54 62 6c 4e 61 6d 65  rse, 0, pTblName
2282c 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 0a 20  ->a[0].zName, . 
2282d 20 20 20 20 20 20 20 70 54 62 6c 4e 61 6d 65 2d         pTblName-
2282e 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29  >a[0].zDatabase)
2282f 3b 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20  ;.    if( !pTab 
22830 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
22831 6c 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  led ) goto exit_
22832 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
22833 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
22834 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3d 3d  b[iDb].pSchema==
22835 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b  pTab->pSchema );
22836 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
22837 73 65 72 74 28 20 70 4e 61 6d 65 3d 3d 30 20 29  sert( pName==0 )
22838 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50 61  ;.    pTab = pPa
22839 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
2283a 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 20      if( !pTab ) 
2283b 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
2283c 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 44 62 20  _index;.    iDb 
2283d 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2283e 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
2283f 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7d 0a 20  >pSchema);.  }. 
22840 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
22841 69 44 62 5d 3b 0a 0a 20 20 69 66 28 20 70 54 61  iDb];..  if( pTa
22842 62 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e  b==0 || pParse->
22843 6e 45 72 72 20 29 20 67 6f 74 6f 20 65 78 69 74  nErr ) goto exit
22844 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
22845 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
22846 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65  ICmp(pTab->zName
22847 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d  , "sqlite_", 7)=
22848 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20 6d 65  =0 .       && me
22849 6d 63 6d 70 28 26 70 54 61 62 2d 3e 7a 4e 61 6d  mcmp(&pTab->zNam
2284a 65 5b 37 5d 2c 22 61 6c 74 65 72 74 61 62 5f 22  e[7],"altertab_"
2284b 2c 39 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ,9)!=0 ){.    sq
2284c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2284d 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
2284e 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78  may not be index
2284f 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ed", pTab->zName
22850 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
22851 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
22852 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
22853 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66  E_OMIT_VIEW.  if
22854 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
22855 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
22856 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
22857 76 69 65 77 73 20 6d 61 79 20 6e 6f 74 20 62 65  views may not be
22858 20 69 6e 64 65 78 65 64 22 29 3b 0a 20 20 20 20   indexed");.    
22859 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
2285a 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64  _index;.  }.#end
2285b 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
2285c 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2285d 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74  BLE.  if( IsVirt
2285e 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
2285f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22860 28 70 50 61 72 73 65 2c 20 22 76 69 72 74 75 61  (pParse, "virtua
22861 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74  l tables may not
22862 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a 20   be indexed");. 
22863 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
22864 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23  ate_index;.  }.#
22865 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  endif..  /*.  **
22866 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f   Find the name o
22867 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 4d 61  f the index.  Ma
22868 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73  ke sure there is
22869 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 6e 6f   not already ano
2286a 74 68 65 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20  ther.  ** index 
2286b 6f 72 20 74 61 62 6c 65 20 77 69 74 68 20 74 68  or table with th
2286c 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 20 0a 20  e same name.  . 
2286d 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 69   **.  ** Excepti
2286e 6f 6e 3a 20 20 49 66 20 77 65 20 61 72 65 20 72  on:  If we are r
2286f 65 61 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 73  eading the names
22870 20 6f 66 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e   of permanent in
22871 64 69 63 65 73 20 66 72 6f 6d 20 74 68 65 0a 20  dices from the. 
22872 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65   ** sqlite_maste
22873 72 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 65  r table (because
22874 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
22875 65 73 73 20 63 68 61 6e 67 65 64 20 74 68 65 20  ess changed the 
22876 73 63 68 65 6d 61 29 20 61 6e 64 0a 20 20 2a 2a  schema) and.  **
22877 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 64 65   one of the inde
22878 78 20 6e 61 6d 65 73 20 63 6f 6c 6c 69 64 65 73  x names collides
22879 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 20 6f   with the name o
2287a 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  f a temporary ta
2287b 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65  ble or.  ** inde
2287c 78 2c 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20  x, then we will 
2287d 63 6f 6e 74 69 6e 75 65 20 74 6f 20 70 72 6f 63  continue to proc
2287e 65 73 73 20 74 68 69 73 20 69 6e 64 65 78 2e 0a  ess this index..
2287f 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 4e 61    **.  ** If pNa
22880 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74  me==0 it means t
22881 68 61 74 20 77 65 20 61 72 65 0a 20 20 2a 2a 20  hat we are.  ** 
22882 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 70  dealing with a p
22883 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 55 4e  rimary key or UN
22884 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e  IQUE constraint.
22885 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76    We have to inv
22886 65 6e 74 20 6f 75 72 0a 20 20 2a 2a 20 6f 77 6e  ent our.  ** own
22887 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   name..  */.  if
22888 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a  ( pName ){.    z
22889 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
2288a 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
2288b 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  pName);.    if( 
2288c 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
2288d 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
2288e 72 73 65 29 20 29 20 67 6f 74 6f 20 65 78 69 74  rse) ) goto exit
2288f 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
22890 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20     if( zName==0 
22891 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61  ) goto exit_crea
22892 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69 66  te_index;.    if
22893 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
22894 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e  ite3CheckObjectN
22895 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d  ame(pParse, zNam
22896 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  e) ){.      goto
22897 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
22898 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ex;.    }.    if
22899 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
2289a 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 53 51   ){.      if( SQ
2289b 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
2289c 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
2289d 65 29 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63  e) ) goto exit_c
2289e 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
2289f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
228a0 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d  ndTable(db, zNam
228a1 65 2c 20 30 29 21 3d 30 20 29 7b 0a 20 20 20 20  e, 0)!=0 ){.    
228a2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
228a3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 68 65  Msg(pParse, "the
228a4 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
228a5 74 61 62 6c 65 20 6e 61 6d 65 64 20 25 73 22 2c  table named %s",
228a6 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
228a7 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
228a8 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d  e_index;.      }
228a9 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
228aa 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
228ab 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 44 62 2d 3e  db, zName, pDb->
228ac 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20  zName)!=0 ){.   
228ad 20 20 20 69 66 28 20 21 69 66 4e 6f 74 45 78 69     if( !ifNotExi
228ae 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  st ){.        sq
228af 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
228b0 61 72 73 65 2c 20 22 69 6e 64 65 78 20 25 73 20  arse, "index %s 
228b1 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c  already exists",
228b2 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d   zName);.      }
228b3 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
228b4 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
228b5 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
228b6 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 49 6e 64    int n;.    Ind
228b7 65 78 20 2a 70 4c 6f 6f 70 3b 0a 20 20 20 20 66  ex *pLoop;.    f
228b8 6f 72 28 70 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70  or(pLoop=pTab->p
228b9 49 6e 64 65 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f  Index, n=1; pLoo
228ba 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e  p; pLoop=pLoop->
228bb 70 4e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20  pNext, n++){}.  
228bc 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
228bd 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71  3MPrintf(db, "sq
228be 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 25  lite_autoindex_%
228bf 73 5f 25 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  s_%d", pTab->zNa
228c0 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 69 66 28 20  me, n);.    if( 
228c1 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  zName==0 ){.    
228c2 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
228c3 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
228c4 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
228c5 66 6f 72 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  for authorizatio
228c6 6e 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 69  n to create an i
228c7 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  ndex..  */.#ifnd
228c8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
228c9 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b  UTHORIZATION.  {
228ca 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
228cb 2a 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d  *zDb = pDb->zNam
228cc 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  e;.    if( sqlit
228cd 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
228ce 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52  se, SQLITE_INSER
228cf 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  T, SCHEMA_TABLE(
228d0 69 44 62 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b  iDb), 0, zDb) ){
228d1 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
228d2 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
228d3 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 53 51 4c     }.    i = SQL
228d4 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58  ITE_CREATE_INDEX
228d5 3b 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f  ;.    if( !OMIT_
228d6 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31  TEMPDB && iDb==1
228d7 20 29 20 69 20 3d 20 53 51 4c 49 54 45 5f 43 52   ) i = SQLITE_CR
228d8 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 3b  EATE_TEMP_INDEX;
228d9 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
228da 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
228db 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62  , i, zName, pTab
228dc 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b  ->zName, zDb) ){
228dd 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
228de 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
228df 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
228e0 0a 20 20 2f 2a 20 49 66 20 70 4c 69 73 74 3d 3d  .  /* If pList==
228e1 30 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73  0, it means this
228e2 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
228e3 6c 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 70 72  led to make a pr
228e4 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20 6f  imary.  ** key o
228e5 75 74 20 6f 66 20 74 68 65 20 6c 61 73 74 20 63  ut of the last c
228e6 6f 6c 75 6d 6e 20 61 64 64 65 64 20 74 6f 20 74  olumn added to t
228e7 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63  he table under c
228e8 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 2a  onstruction..  *
228e9 2a 20 53 6f 20 63 72 65 61 74 65 20 61 20 66 61  * So create a fa
228ea 6b 65 20 6c 69 73 74 20 74 6f 20 73 69 6d 75 6c  ke list to simul
228eb 61 74 65 20 74 68 69 73 2e 0a 20 20 2a 2f 0a 20  ate this..  */. 
228ec 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b   if( pList==0 ){
228ed 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 7a 20 3d 20  .    nullId.z = 
228ee 28 75 38 2a 29 70 54 61 62 2d 3e 61 43 6f 6c 5b  (u8*)pTab->aCol[
228ef 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 5d 2e 7a 4e  pTab->nCol-1].zN
228f0 61 6d 65 3b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e  ame;.    nullId.
228f1 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
228f2 6e 33 30 28 28 63 68 61 72 2a 29 6e 75 6c 6c 49  n30((char*)nullI
228f3 64 2e 7a 29 3b 0a 20 20 20 20 6e 75 6c 6c 49 64  d.z);.    nullId
228f4 2e 71 75 6f 74 65 64 20 3d 20 30 3b 0a 20 20 20  .quoted = 0;.   
228f5 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
228f6 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
228f7 50 61 72 73 65 2c 20 30 2c 20 30 2c 20 26 6e 75  Parse, 0, 0, &nu
228f8 6c 6c 49 64 29 3b 0a 20 20 20 20 69 66 28 20 70  llId);.    if( p
228f9 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f 20 65  List==0 ) goto e
228fa 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
228fb 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 30  ;.    pList->a[0
228fc 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  ].sortOrder = (u
228fd 38 29 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d  8)sortOrder;.  }
228fe 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
228ff 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73  t how many bytes
22900 20 6f 66 20 73 70 61 63 65 20 61 72 65 20 72 65   of space are re
22901 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20  quired to store 
22902 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 2a 2a 20  explicitly.  ** 
22903 73 70 65 63 69 66 69 65 64 20 63 6f 6c 6c 61 74  specified collat
22904 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
22905 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  es..  */.  for(i
22906 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
22907 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78  pr; i++){.    Ex
22908 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20 43  pr *pExpr;.    C
22909 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
2290a 20 20 20 69 66 28 20 28 70 45 78 70 72 20 3d 20     if( (pExpr = 
2290b 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
2290c 72 29 21 3d 30 20 26 26 20 28 70 43 6f 6c 6c 20  r)!=0 && (pColl 
2290d 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 21  = pExpr->pColl)!
2290e 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 78 74  =0 ){.      nExt
2290f 72 61 20 2b 3d 20 28 31 20 2b 20 73 71 6c 69 74  ra += (1 + sqlit
22910 65 33 53 74 72 6c 65 6e 33 30 28 70 43 6f 6c 6c  e3Strlen30(pColl
22911 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d  ->zName));.    }
22912 0a 20 20 7d 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a  .  }..  /* .  **
22913 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 69 6e   Allocate the in
22914 64 65 78 20 73 74 72 75 63 74 75 72 65 2e 20 0a  dex structure. .
22915 20 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73    */.  nName = s
22916 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
22917 4e 61 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20  Name);.  nCol = 
22918 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  pList->nExpr;.  
22919 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
2291a 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2291b 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49   .      sizeof(I
2291c 6e 64 65 78 29 20 2b 20 20 20 20 20 20 20 20 20  ndex) +         
2291d 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74       /* Index st
2291e 72 75 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20  ructure  */.    
2291f 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43    sizeof(int)*nC
22920 6f 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f  ol +           /
22921 2a 20 49 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e  * Index.aiColumn
22922 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65     */.      size
22923 6f 66 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29  of(int)*(nCol+1)
22924 20 2b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65   +       /* Inde
22925 78 2e 61 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a  x.aiRowEst   */.
22926 20 20 20 20 20 20 73 69 7a 65 6f 66 28 63 68 61        sizeof(cha
22927 72 20 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20  r *)*nCol +     
22928 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f     /* Index.azCo
22929 6c 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  ll     */.      
2292a 73 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20  sizeof(u8)*nCol 
2292b 2b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  +            /* 
2292c 49 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72  Index.aSortOrder
2292d 20 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20   */.      nName 
2292e 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20  + 1 +           
2292f 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e         /* Index.
22930 7a 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20  zName      */.  
22931 20 20 20 20 6e 45 78 74 72 61 20 20 20 20 20 20      nExtra      
22932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22933 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65   /* Collation se
22934 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a  quence names */.
22935 20 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d    );.  if( db->m
22936 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
22937 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
22938 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  ate_index;.  }. 
22939 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20   pIndex->azColl 
2293a 3d 20 28 63 68 61 72 2a 2a 29 28 26 70 49 6e 64  = (char**)(&pInd
2293b 65 78 5b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78  ex[1]);.  pIndex
2293c 2d 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e  ->aiColumn = (in
2293d 74 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a  t *)(&pIndex->az
2293e 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70  Coll[nCol]);.  p
2293f 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20  Index->aiRowEst 
22940 3d 20 28 75 6e 73 69 67 6e 65 64 20 2a 29 28 26  = (unsigned *)(&
22941 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e  pIndex->aiColumn
22942 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65  [nCol]);.  pInde
22943 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  x->aSortOrder = 
22944 28 75 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e  (u8 *)(&pIndex->
22945 61 69 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d  aiRowEst[nCol+1]
22946 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61  );.  pIndex->zNa
22947 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70  me = (char *)(&p
22948 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65  Index->aSortOrde
22949 72 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74  r[nCol]);.  zExt
2294a 72 61 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70  ra = (char *)(&p
2294b 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61  Index->zName[nNa
2294c 6d 65 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79  me+1]);.  memcpy
2294d 28 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20  (pIndex->zName, 
2294e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b  zName, nName+1);
2294f 0a 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c  .  pIndex->pTabl
22950 65 20 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64  e = pTab;.  pInd
22951 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c  ex->nColumn = pL
22952 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49  ist->nExpr;.  pI
22953 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20  ndex->onError = 
22954 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 70  (u8)onError;.  p
22955 49 6e 64 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78  Index->autoIndex
22956 20 3d 20 28 75 38 29 28 70 4e 61 6d 65 3d 3d 30   = (u8)(pName==0
22957 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 70 53 63  );.  pIndex->pSc
22958 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69  hema = db->aDb[i
22959 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 0a 20 20  Db].pSchema;..  
2295a 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
2295b 69 66 20 77 65 20 73 68 6f 75 6c 64 20 68 6f 6e  if we should hon
2295c 6f 72 20 44 45 53 43 20 72 65 71 75 65 73 74 73  or DESC requests
2295d 20 6f 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e   on index column
2295e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 62  s.  */.  if( pDb
2295f 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
22960 66 6f 72 6d 61 74 3e 3d 34 20 29 7b 0a 20 20 20  format>=4 ){.   
22961 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d   sortOrderMask =
22962 20 2d 31 3b 20 20 20 2f 2a 20 48 6f 6e 6f 72 20   -1;   /* Honor 
22963 44 45 53 43 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b  DESC */.  }else{
22964 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d 61  .    sortOrderMa
22965 73 6b 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 67  sk = 0;    /* Ig
22966 6e 6f 72 65 20 44 45 53 43 20 2a 2f 0a 20 20 7d  nore DESC */.  }
22967 0a 0a 20 20 2f 2a 20 53 63 61 6e 20 74 68 65 20  ..  /* Scan the 
22968 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f 6c  names of the col
22969 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 6c  umns of the tabl
2296a 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  e to be indexed 
2296b 61 6e 64 0a 20 20 2a 2a 20 6c 6f 61 64 20 74 68  and.  ** load th
2296c 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73  e column indices
2296d 20 69 6e 74 6f 20 74 68 65 20 49 6e 64 65 78 20   into the Index 
2296e 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 70 6f  structure.  Repo
2296f 72 74 20 61 6e 20 65 72 72 6f 72 0a 20 20 2a 2a  rt an error.  **
22970 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 69   if any column i
22971 73 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a  s not found..  *
22972 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 69  /.  for(i=0, pLi
22973 73 74 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b  stItem=pList->a;
22974 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
22975 20 69 2b 2b 2c 20 70 4c 69 73 74 49 74 65 6d 2b   i++, pListItem+
22976 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  +){.    const ch
22977 61 72 20 2a 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70  ar *zColName = p
22978 4c 69 73 74 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b  ListItem->zName;
22979 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 54 61  .    Column *pTa
2297a 62 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20 72 65  bCol;.    int re
2297b 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72  questedSortOrder
2297c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c  ;.    char *zCol
2297d 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
2297e 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f       /* Collatio
2297f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20  n sequence name 
22980 2a 2f 0a 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c  */..    for(j=0,
22981 20 70 54 61 62 43 6f 6c 3d 70 54 61 62 2d 3e 61   pTabCol=pTab->a
22982 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f  Col; j<pTab->nCo
22983 6c 3b 20 6a 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b  l; j++, pTabCol+
22984 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  +){.      if( sq
22985 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 43 6f  lite3StrICmp(zCo
22986 6c 4e 61 6d 65 2c 20 70 54 61 62 43 6f 6c 2d 3e  lName, pTabCol->
22987 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61  zName)==0 ) brea
22988 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
22989 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29   j>=pTab->nCol )
2298a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2298b 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2298c 22 74 61 62 6c 65 20 25 73 20 68 61 73 20 6e 6f  "table %s has no
2298d 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73   column named %s
2298e 22 2c 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d  ",.        pTab-
2298f 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65  >zName, zColName
22990 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  );.      goto ex
22991 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
22992 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 4f  .    }.    /* TO
22993 44 4f 3a 20 20 41 64 64 20 61 20 74 65 73 74 20  DO:  Add a test 
22994 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
22995 74 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d  t the same colum
22996 6e 20 69 73 20 6e 6f 74 20 6e 61 6d 65 64 0a 20  n is not named. 
22997 20 20 20 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20     ** more than 
22998 6f 6e 63 65 20 77 69 74 68 69 6e 20 74 68 65 20  once within the 
22999 73 61 6d 65 20 69 6e 64 65 78 2e 20 20 4f 6e 6c  same index.  Onl
2299a 79 20 74 68 65 20 66 69 72 73 74 20 69 6e 73 74  y the first inst
2299b 61 6e 63 65 20 6f 66 0a 20 20 20 20 2a 2a 20 74  ance of.    ** t
2299c 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 65  he column will e
2299d 76 65 72 20 62 65 20 75 73 65 64 20 62 79 20 74  ver be used by t
2299e 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 4e  he optimizer.  N
2299f 6f 74 65 20 74 68 61 74 20 75 73 69 6e 67 20 74  ote that using t
229a0 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 63  he.    ** same c
229a1 6f 6c 75 6d 6e 20 6d 6f 72 65 20 74 68 61 6e 20  olumn more than 
229a2 6f 6e 63 65 20 63 61 6e 6e 6f 74 20 62 65 20 61  once cannot be a
229a3 6e 20 65 72 72 6f 72 20 62 65 63 61 75 73 65 20  n error because 
229a4 74 68 61 74 20 77 6f 75 6c 64 20 0a 20 20 20 20  that would .    
229a5 2a 2a 20 62 72 65 61 6b 20 62 61 63 6b 77 61 72  ** break backwar
229a6 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ds compatibility
229a7 20 2d 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62   - it needs to b
229a8 65 20 61 20 77 61 72 6e 69 6e 67 2e 0a 20 20 20  e a warning..   
229a9 20 2a 2f 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e   */.    pIndex->
229aa 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b  aiColumn[i] = j;
229ab 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 49 74  .    if( pListIt
229ac 65 6d 2d 3e 70 45 78 70 72 20 26 26 20 70 4c 69  em->pExpr && pLi
229ad 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70  stItem->pExpr->p
229ae 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 61 73  Coll ){.      as
229af 73 65 72 74 28 20 70 4c 69 73 74 49 74 65 6d 2d  sert( pListItem-
229b0 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 29 3b  >pExpr->pColl );
229b1 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 7a  .      zColl = z
229b2 45 78 74 72 61 3b 0a 20 20 20 20 20 20 73 71 6c  Extra;.      sql
229b3 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 45  ite3_snprintf(nE
229b4 78 74 72 61 2c 20 7a 45 78 74 72 61 2c 20 22 25  xtra, zExtra, "%
229b5 73 22 2c 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70  s", pListItem->p
229b6 45 78 70 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a 4e 61  Expr->pColl->zNa
229b7 6d 65 29 3b 0a 20 20 20 20 20 20 7a 45 78 74 72  me);.      zExtr
229b8 61 20 2b 3d 20 28 73 71 6c 69 74 65 33 53 74 72  a += (sqlite3Str
229b9 6c 65 6e 33 30 28 7a 43 6f 6c 6c 29 20 2b 20 31  len30(zColl) + 1
229ba 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
229bb 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62      zColl = pTab
229bc 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b  ->aCol[j].zColl;
229bd 0a 20 20 20 20 20 20 69 66 28 20 21 7a 43 6f 6c  .      if( !zCol
229be 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f  l ){.        zCo
229bf 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
229c0 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ll->zName;.     
229c1 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
229c2 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
229c3 26 26 20 21 73 71 6c 69 74 65 33 4c 6f 63 61 74  && !sqlite3Locat
229c4 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
229c5 20 7a 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20   zColl, -1) ){. 
229c6 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
229c7 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
229c8 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61   }.    pIndex->a
229c9 7a 43 6f 6c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c  zColl[i] = zColl
229ca 3b 0a 20 20 20 20 72 65 71 75 65 73 74 65 64 53  ;.    requestedS
229cb 6f 72 74 4f 72 64 65 72 20 3d 20 70 4c 69 73 74  ortOrder = pList
229cc 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20  Item->sortOrder 
229cd 26 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 3b  & sortOrderMask;
229ce 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f  .    pIndex->aSo
229cf 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 28 75 38  rtOrder[i] = (u8
229d0 29 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72  )requestedSortOr
229d1 64 65 72 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  der;.  }.  sqlit
229d2 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28  e3DefaultRowEst(
229d3 70 49 6e 64 65 78 29 3b 0a 0a 20 20 69 66 28 20  pIndex);..  if( 
229d4 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 70 4e  pTab==pParse->pN
229d5 65 77 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f  ewTable ){.    /
229d6 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68  * This routine h
229d7 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 74  as been called t
229d8 6f 20 63 72 65 61 74 65 20 61 6e 20 61 75 74 6f  o create an auto
229d9 6d 61 74 69 63 20 69 6e 64 65 78 20 61 73 20 61  matic index as a
229da 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 20 6f  .    ** result o
229db 66 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  f a PRIMARY KEY 
229dc 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73 65  or UNIQUE clause
229dd 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 64 65 66   on a column def
229de 69 6e 69 74 69 6f 6e 2c 20 6f 72 0a 20 20 20 20  inition, or.    
229df 2a 2a 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59  ** a PRIMARY KEY
229e0 20 6f 72 20 55 4e 49 51 55 45 20 63 6c 61 75 73   or UNIQUE claus
229e1 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  e following the 
229e2 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  column definitio
229e3 6e 73 2e 0a 20 20 20 20 2a 2a 20 69 2e 65 2e 20  ns..    ** i.e. 
229e4 6f 6e 65 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20  one of:.    **. 
229e5 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42     ** CREATE TAB
229e6 4c 45 20 74 28 78 20 50 52 49 4d 41 52 59 20 4b  LE t(x PRIMARY K
229e7 45 59 2c 20 79 29 3b 0a 20 20 20 20 2a 2a 20 43  EY, y);.    ** C
229e8 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 2c  REATE TABLE t(x,
229e9 20 79 2c 20 55 4e 49 51 55 45 28 78 2c 20 79 29   y, UNIQUE(x, y)
229ea 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  );.    **.    **
229eb 20 45 69 74 68 65 72 20 77 61 79 2c 20 63 68 65   Either way, che
229ec 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
229ed 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68   table already h
229ee 61 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65 78  as such an index
229ef 2e 20 49 66 0a 20 20 20 20 2a 2a 20 73 6f 2c 20  . If.    ** so, 
229f0 64 6f 6e 27 74 20 62 6f 74 68 65 72 20 63 72 65  don't bother cre
229f1 61 74 69 6e 67 20 74 68 69 73 20 6f 6e 65 2e 20  ating this one. 
229f2 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65  This only applie
229f3 73 20 74 6f 0a 20 20 20 20 2a 2a 20 61 75 74 6f  s to.    ** auto
229f4 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65  matically create
229f5 64 20 69 6e 64 69 63 65 73 2e 20 55 73 65 72 73  d indices. Users
229f6 20 63 61 6e 20 64 6f 20 61 73 20 74 68 65 79 20   can do as they 
229f7 77 69 73 68 20 77 69 74 68 0a 20 20 20 20 2a 2a  wish with.    **
229f8 20 65 78 70 6c 69 63 69 74 20 69 6e 64 69 63 65   explicit indice
229f9 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e  s..    */.    In
229fa 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 66  dex *pIdx;.    f
229fb 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
229fc 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
229fd 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
229fe 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20       int k;.    
229ff 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
22a00 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
22a01 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
22a02 28 20 70 49 64 78 2d 3e 61 75 74 6f 49 6e 64 65  ( pIdx->autoInde
22a03 78 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  x );.      asser
22a04 74 28 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72  t( pIndex->onErr
22a05 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 0a  or!=OE_None );..
22a06 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
22a07 6e 43 6f 6c 75 6d 6e 21 3d 70 49 6e 64 65 78 2d  nColumn!=pIndex-
22a08 3e 6e 43 6f 6c 75 6d 6e 20 29 20 63 6f 6e 74 69  >nColumn ) conti
22a09 6e 75 65 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b  nue;.      for(k
22a0a 3d 30 3b 20 6b 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; k<pIdx->nCol
22a0b 75 6d 6e 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20  umn; k++){.     
22a0c 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
22a0d 31 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  1 = pIdx->azColl
22a0e 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e  [k];.        con
22a0f 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 70 49  st char *z2 = pI
22a10 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b  ndex->azColl[k];
22a11 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64  .        if( pId
22a12 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 21 3d  x->aiColumn[k]!=
22a13 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e  pIndex->aiColumn
22a14 5b 6b 5d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  [k] ) break;.   
22a15 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
22a16 53 6f 72 74 4f 72 64 65 72 5b 6b 5d 21 3d 70 49  SortOrder[k]!=pI
22a17 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  ndex->aSortOrder
22a18 5b 6b 5d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  [k] ) break;.   
22a19 20 20 20 20 20 69 66 28 20 7a 31 21 3d 7a 32 20       if( z1!=z2 
22a1a 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
22a1b 70 28 7a 31 2c 20 7a 32 29 20 29 20 62 72 65 61  p(z1, z2) ) brea
22a1c 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
22a1d 20 69 66 28 20 6b 3d 3d 70 49 64 78 2d 3e 6e 43   if( k==pIdx->nC
22a1e 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
22a1f 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72   if( pIdx->onErr
22a20 6f 72 21 3d 70 49 6e 64 65 78 2d 3e 6f 6e 45 72  or!=pIndex->onEr
22a21 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ror ){.         
22a22 20 2f 2a 20 54 68 69 73 20 63 6f 6e 73 74 72 61   /* This constra
22a23 69 6e 74 20 63 72 65 61 74 65 73 20 74 68 65 20  int creates the 
22a24 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20 61 20  same index as a 
22a25 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 20  previous.       
22a26 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74     ** constraint
22a27 20 73 70 65 63 69 66 69 65 64 20 73 6f 6d 65 77   specified somew
22a28 68 65 72 65 20 69 6e 20 74 68 65 20 43 52 45 41  here in the CREA
22a29 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
22a2a 6e 74 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  nt..          **
22a2b 20 48 6f 77 65 76 65 72 20 74 68 65 20 4f 4e 20   However the ON 
22a2c 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73  CONFLICT clauses
22a2d 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 20   are different. 
22a2e 49 66 20 62 6f 74 68 20 74 68 69 73 20 0a 20 20  If both this .  
22a2f 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74          ** const
22a30 72 61 69 6e 74 20 61 6e 64 20 74 68 65 20 70 72  raint and the pr
22a31 65 76 69 6f 75 73 20 65 71 75 69 76 61 6c 65 6e  evious equivalen
22a32 74 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 76  t constraint hav
22a33 65 20 65 78 70 6c 69 63 69 74 0a 20 20 20 20 20  e explicit.     
22a34 20 20 20 20 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c       ** ON CONFL
22a35 49 43 54 20 63 6c 61 75 73 65 73 20 74 68 69 73  ICT clauses this
22a36 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 20 4f 74   is an error. Ot
22a37 68 65 72 77 69 73 65 2c 20 75 73 65 20 74 68 65  herwise, use the
22a38 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78  .          ** ex
22a39 70 6c 69 63 69 74 6c 79 20 73 70 65 63 69 66 69  plicitly specifi
22a3a 65 64 20 62 65 68 61 76 69 6f 75 72 20 66 6f 72  ed behaviour for
22a3b 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20   the index..    
22a3c 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
22a3d 20 20 20 69 66 28 20 21 28 70 49 64 78 2d 3e 6f     if( !(pIdx->o
22a3e 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
22a3f 6c 74 20 7c 7c 20 70 49 6e 64 65 78 2d 3e 6f 6e  lt || pIndex->on
22a40 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c  Error==OE_Defaul
22a41 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  t) ){.          
22a42 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
22a43 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  g(pParse, .     
22a44 20 20 20 20 20 20 20 20 20 20 20 22 63 6f 6e 66             "conf
22a45 6c 69 63 74 69 6e 67 20 4f 4e 20 43 4f 4e 46 4c  licting ON CONFL
22a46 49 43 54 20 63 6c 61 75 73 65 73 20 73 70 65 63  ICT clauses spec
22a47 69 66 69 65 64 22 2c 20 30 29 3b 0a 20 20 20 20  ified", 0);.    
22a48 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
22a49 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72    if( pIdx->onEr
22a4a 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
22a4b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
22a4c 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 70  Idx->onError = p
22a4d 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a  Index->onError;.
22a4e 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
22a4f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f      }.        go
22a50 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
22a51 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ndex;.      }.  
22a52 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69    }.  }..  /* Li
22a53 6e 6b 20 74 68 65 20 6e 65 77 20 49 6e 64 65 78  nk the new Index
22a54 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 69 74   structure to it
22a55 73 20 74 61 62 6c 65 20 61 6e 64 20 74 6f 20 74  s table and to t
22a56 68 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e  he other.  ** in
22a57 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
22a58 20 73 74 72 75 63 74 75 72 65 73 2e 20 0a 20 20   structures. .  
22a59 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  */.  if( db->ini
22a5a 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 49 6e  t.busy ){.    In
22a5b 64 65 78 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20  dex *p;.    p = 
22a5c 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
22a5d 74 28 26 70 49 6e 64 65 78 2d 3e 70 53 63 68 65  t(&pIndex->pSche
22a5e 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 0a 20 20  ma->idxHash, .  
22a5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a60 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e          pIndex->
22a61 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74  zName, sqlite3St
22a62 72 6c 65 6e 33 30 28 70 49 6e 64 65 78 2d 3e 7a  rlen30(pIndex->z
22a63 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20  Name),.         
22a64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a65 20 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 69 66   pIndex);.    if
22a66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ( p ){.      ass
22a67 65 72 74 28 20 70 3d 3d 70 49 6e 64 65 78 20 29  ert( p==pIndex )
22a68 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73  ;  /* Malloc mus
22a69 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a 2f  t have failed */
22a6a 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
22a6b 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
22a6c 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
22a6d 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d  ate_index;.    }
22a6e 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  .    db->flags |
22a6f 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  = SQLITE_InternC
22a70 68 61 6e 67 65 73 3b 0a 20 20 20 20 69 66 28 20  hanges;.    if( 
22a71 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 20  pTblName!=0 ){. 
22a72 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e 75       pIndex->tnu
22a73 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77  m = db->init.new
22a74 54 6e 75 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Tnum;.    }.  }.
22a75 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d  .  /* If the db-
22a76 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30 20  >init.busy is 0 
22a77 74 68 65 6e 20 63 72 65 61 74 65 20 74 68 65 20  then create the 
22a78 69 6e 64 65 78 20 6f 6e 20 64 69 73 6b 2e 20 20  index on disk.  
22a79 54 68 69 73 0a 20 20 2a 2a 20 69 6e 76 6f 6c 76  This.  ** involv
22a7a 65 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 69  es writing the i
22a7b 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 6d 61  ndex into the ma
22a7c 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 66  ster table and f
22a7d 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 0a 20 20  illing in the.  
22a7e 2a 2a 20 69 6e 64 65 78 20 77 69 74 68 20 74 68  ** index with th
22a7f 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20  e current table 
22a80 63 6f 6e 74 65 6e 74 73 2e 0a 20 20 2a 2a 0a 20  contents..  **. 
22a81 20 2a 2a 20 54 68 65 20 64 62 2d 3e 69 6e 69 74   ** The db->init
22a82 2e 62 75 73 79 20 69 73 20 30 20 77 68 65 6e 20  .busy is 0 when 
22a83 74 68 65 20 75 73 65 72 20 66 69 72 73 74 20 65  the user first e
22a84 6e 74 65 72 73 20 61 20 43 52 45 41 54 45 20 49  nters a CREATE I
22a85 4e 44 45 58 20 0a 20 20 2a 2a 20 63 6f 6d 6d 61  NDEX .  ** comma
22a86 6e 64 2e 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75  nd.  db->init.bu
22a87 73 79 20 69 73 20 31 20 77 68 65 6e 20 61 20 64  sy is 1 when a d
22a88 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
22a89 64 20 61 6e 64 20 0a 20 20 2a 2a 20 43 52 45 41  d and .  ** CREA
22a8a 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65  TE INDEX stateme
22a8b 6e 74 73 20 61 72 65 20 72 65 61 64 20 6f 75 74  nts are read out
22a8c 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 74   of the master t
22a8d 61 62 6c 65 2e 20 20 49 6e 0a 20 20 2a 2a 20 74  able.  In.  ** t
22a8e 68 65 20 6c 61 74 74 65 72 20 63 61 73 65 20 74  he latter case t
22a8f 68 65 20 69 6e 64 65 78 20 61 6c 72 65 61 64 79  he index already
22a90 20 65 78 69 73 74 73 20 6f 6e 20 64 69 73 6b 2c   exists on disk,
22a91 20 77 68 69 63 68 20 69 73 20 77 68 79 0a 20 20   which is why.  
22a92 2a 2a 20 77 65 20 64 6f 6e 27 74 20 77 61 6e 74  ** we don't want
22a93 20 74 6f 20 72 65 63 72 65 61 74 65 20 69 74 2e   to recreate it.
22a94 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 54  .  **.  ** If pT
22a95 62 6c 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61  blName==0 it mea
22a96 6e 73 20 74 68 69 73 20 69 6e 64 65 78 20 69 73  ns this index is
22a97 20 67 65 6e 65 72 61 74 65 64 20 61 73 20 61 20   generated as a 
22a98 70 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a  primary key.  **
22a99 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74   or UNIQUE const
22a9a 72 61 69 6e 74 20 6f 66 20 61 20 43 52 45 41 54  raint of a CREAT
22a9b 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
22a9c 74 2e 20 20 53 69 6e 63 65 20 74 68 65 20 74 61  t.  Since the ta
22a9d 62 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6a 75 73  ble.  ** has jus
22a9e 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20  t been created, 
22a9f 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64  it contains no d
22aa0 61 74 61 20 61 6e 64 20 74 68 65 20 69 6e 64 65  ata and the inde
22aa1 78 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  x initialization
22aa2 0a 20 20 2a 2a 20 73 74 65 70 20 63 61 6e 20 62  .  ** step can b
22aa3 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a  e skipped..  */.
22aa4 20 20 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 69    else if( db->i
22aa5 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20  nit.busy==0 ){. 
22aa6 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20     Vdbe *v;.    
22aa7 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20  char *zStmt;.   
22aa8 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50   int iMem = ++pP
22aa9 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 20  arse->nMem;..   
22aaa 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
22aab 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
22aac 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f   if( v==0 ) goto
22aad 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
22aae 65 78 3b 0a 0a 0a 20 20 20 20 2f 2a 20 43 72 65  ex;...    /* Cre
22aaf 61 74 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65  ate the rootpage
22ab0 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a 20   for the index. 
22ab1 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
22ab2 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
22ab3 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20  tion(pParse, 1, 
22ab4 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
22ab5 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22ab6 50 5f 43 72 65 61 74 65 49 6e 64 65 78 2c 20 69  P_CreateIndex, i
22ab7 44 62 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20  Db, iMem);..    
22ab8 2f 2a 20 47 61 74 68 65 72 20 74 68 65 20 63 6f  /* Gather the co
22ab9 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74  mplete text of t
22aba 68 65 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  he CREATE INDEX 
22abb 73 74 61 74 65 6d 65 6e 74 20 69 6e 74 6f 0a 20  statement into. 
22abc 20 20 20 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20     ** the zStmt 
22abd 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2f 0a  variable.    */.
22abe 20 20 20 20 69 66 28 20 70 53 74 61 72 74 20 26      if( pStart &
22abf 26 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20  & pEnd ){.      
22ac0 2f 2a 20 41 20 6e 61 6d 65 64 20 69 6e 64 65 78  /* A named index
22ac1 20 77 69 74 68 20 61 6e 20 65 78 70 6c 69 63 69   with an explici
22ac2 74 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73  t CREATE INDEX s
22ac3 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
22ac4 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65    zStmt = sqlite
22ac5 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 43 52  3MPrintf(db, "CR
22ac6 45 41 54 45 25 73 20 49 4e 44 45 58 20 25 2e 2a  EATE%s INDEX %.*
22ac7 73 22 2c 0a 20 20 20 20 20 20 20 20 6f 6e 45 72  s",.        onEr
22ac8 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f 20 22  ror==OE_None ? "
22ac9 22 20 3a 20 22 20 55 4e 49 51 55 45 22 2c 0a 20  " : " UNIQUE",. 
22aca 20 20 20 20 20 20 20 70 45 6e 64 2d 3e 7a 20 2d         pEnd->z -
22acb 20 70 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c 0a 20   pName->z + 1,. 
22acc 20 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e 7a 29         pName->z)
22acd 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
22ace 20 20 20 2f 2a 20 41 6e 20 61 75 74 6f 6d 61 74     /* An automat
22acf 69 63 20 69 6e 64 65 78 20 63 72 65 61 74 65 64  ic index created
22ad0 20 62 79 20 61 20 50 52 49 4d 41 52 59 20 4b 45   by a PRIMARY KE
22ad1 59 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73  Y or UNIQUE cons
22ad2 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20  traint */.      
22ad3 2f 2a 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74  /* zStmt = sqlit
22ad4 65 33 4d 50 72 69 6e 74 66 28 22 22 29 3b 20 2a  e3MPrintf(""); *
22ad5 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20  /.      zStmt = 
22ad6 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  0;.    }..    /*
22ad7 20 41 64 64 20 61 6e 20 65 6e 74 72 79 20 69 6e   Add an entry in
22ad8 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 66   sqlite_master f
22ad9 6f 72 20 74 68 69 73 20 69 6e 64 65 78 0a 20 20  or this index.  
22ada 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
22adb 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
22adc 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 49 4e  se, .        "IN
22add 53 45 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20  SERT INTO %Q.%s 
22ade 56 41 4c 55 45 53 28 27 69 6e 64 65 78 27 2c 25  VALUES('index',%
22adf 51 2c 25 51 2c 23 25 64 2c 25 51 29 3b 22 2c 0a  Q,%Q,#%d,%Q);",.
22ae0 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b          db->aDb[
22ae1 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45  iDb].zName, SCHE
22ae2 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20  MA_TABLE(iDb),. 
22ae3 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a         pIndex->z
22ae4 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 70 54  Name,.        pT
22ae5 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  ab->zName,.     
22ae6 20 20 20 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20     iMem,.       
22ae7 20 7a 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20   zStmt.    );.  
22ae8 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
22ae9 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 20 20  db, zStmt);..   
22aea 20 2f 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e 64   /* Fill the ind
22aeb 65 78 20 77 69 74 68 20 64 61 74 61 20 61 6e 64  ex with data and
22aec 20 72 65 70 61 72 73 65 20 74 68 65 20 73 63 68   reparse the sch
22aed 65 6d 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f  ema. Code an OP_
22aee 45 78 70 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f  Expire.    ** to
22aef 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20   invalidate all 
22af0 70 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61  pre-compiled sta
22af1 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a  tements..    */.
22af2 20 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65      if( pTblName
22af3 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
22af4 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61  3RefillIndex(pPa
22af5 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65  rse, pIndex, iMe
22af6 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  m);.      sqlite
22af7 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50  3ChangeCookie(pP
22af8 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
22af9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22afa 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53  Op4(v, OP_ParseS
22afb 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30  chema, iDb, 0, 0
22afc 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  ,.         sqlit
22afd 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
22afe 61 6d 65 3d 27 25 71 27 22 2c 20 70 49 6e 64 65  ame='%q'", pInde
22aff 78 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59  x->zName), P4_DY
22b00 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 73 71  NAMIC);.      sq
22b01 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
22b02 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 29  v, OP_Expire, 0)
22b03 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
22b04 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20 61 6e  * When adding an
22b05 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 6c 69   index to the li
22b06 73 74 20 6f 66 20 69 6e 64 69 63 65 73 20 66 6f  st of indices fo
22b07 72 20 61 20 74 61 62 6c 65 2c 20 6d 61 6b 65 0a  r a table, make.
22b08 20 20 2a 2a 20 73 75 72 65 20 61 6c 6c 20 69 6e    ** sure all in
22b09 64 69 63 65 73 20 6c 61 62 65 6c 65 64 20 4f 45  dices labeled OE
22b0a 5f 52 65 70 6c 61 63 65 20 63 6f 6d 65 20 61 66  _Replace come af
22b0b 74 65 72 20 61 6c 6c 20 74 68 6f 73 65 20 6c 61  ter all those la
22b0c 62 65 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f 49 67  beled.  ** OE_Ig
22b0d 6e 6f 72 65 2e 20 20 54 68 69 73 20 69 73 20 6e  nore.  This is n
22b0e 65 63 65 73 73 61 72 79 20 66 6f 72 20 74 68 65  ecessary for the
22b0f 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69   correct operati
22b10 6f 6e 20 6f 66 20 55 50 44 41 54 45 0a 20 20 2a  on of UPDATE.  *
22b11 2a 20 61 6e 64 20 49 4e 53 45 52 54 2e 0a 20 20  * and INSERT..  
22b12 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  */.  if( db->ini
22b13 74 2e 62 75 73 79 20 7c 7c 20 70 54 62 6c 4e 61  t.busy || pTblNa
22b14 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  me==0 ){.    if(
22b15 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70   onError!=OE_Rep
22b16 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49  lace || pTab->pI
22b17 6e 64 65 78 3d 3d 30 0a 20 20 20 20 20 20 20 20  ndex==0.        
22b18 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78   || pTab->pIndex
22b19 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65  ->onError==OE_Re
22b1a 70 6c 61 63 65 29 7b 0a 20 20 20 20 20 20 70 49  place){.      pI
22b1b 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70 54  ndex->pNext = pT
22b1c 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20  ab->pIndex;.    
22b1d 20 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 3d    pTab->pIndex =
22b1e 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 65 6c   pIndex;.    }el
22b1f 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20  se{.      Index 
22b20 2a 70 4f 74 68 65 72 20 3d 20 70 54 61 62 2d 3e  *pOther = pTab->
22b21 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 77 68  pIndex;.      wh
22b22 69 6c 65 28 20 70 4f 74 68 65 72 2d 3e 70 4e 65  ile( pOther->pNe
22b23 78 74 20 26 26 20 70 4f 74 68 65 72 2d 3e 70 4e  xt && pOther->pN
22b24 65 78 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45  ext->onError!=OE
22b25 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20  _Replace ){.    
22b26 20 20 20 20 70 4f 74 68 65 72 20 3d 20 70 4f 74      pOther = pOt
22b27 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  her->pNext;.    
22b28 20 20 7d 0a 20 20 20 20 20 20 70 49 6e 64 65 78    }.      pIndex
22b29 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65 72  ->pNext = pOther
22b2a 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 70  ->pNext;.      p
22b2b 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70  Other->pNext = p
22b2c 49 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20  Index;.    }.   
22b2d 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 7d   pIndex = 0;.  }
22b2e 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20  ..  /* Clean up 
22b2f 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a  before exiting *
22b30 2f 0a 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  /.exit_create_in
22b31 64 65 78 3a 0a 20 20 69 66 28 20 70 49 6e 64 65  dex:.  if( pInde
22b32 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  x ){.    sqlite3
22b33 5f 66 72 65 65 28 70 49 6e 64 65 78 2d 3e 7a 43  _free(pIndex->zC
22b34 6f 6c 41 66 66 29 3b 0a 20 20 20 20 73 71 6c 69  olAff);.    sqli
22b35 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
22b36 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  ndex);.  }.  sql
22b37 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
22b38 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
22b39 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
22b3a 65 6c 65 74 65 28 64 62 2c 20 70 54 62 6c 4e 61  elete(db, pTblNa
22b3b 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  me);.  sqlite3Db
22b3c 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b  Free(db, zName);
22b3d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
22b3e 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
22b3f 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  e to make sure t
22b40 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e  he file format n
22b41 75 6d 62 65 72 20 69 73 20 61 74 20 6c 65 61 73  umber is at leas
22b42 74 20 6d 69 6e 46 6f 72 6d 61 74 2e 0a 2a 2a 20  t minFormat..** 
22b43 54 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f  The generated co
22b44 64 65 20 77 69 6c 6c 20 69 6e 63 72 65 61 73 65  de will increase
22b45 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74   the file format
22b46 20 6e 75 6d 62 65 72 20 69 66 20 6e 65 63 65 73   number if neces
22b47 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sary..*/.SQLITE_
22b48 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22b49 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46  ite3MinimumFileF
22b4a 6f 72 6d 61 74 28 50 61 72 73 65 20 2a 70 50 61  ormat(Parse *pPa
22b4b 72 73 65 2c 20 69 6e 74 20 69 44 62 2c 20 69 6e  rse, int iDb, in
22b4c 74 20 6d 69 6e 46 6f 72 6d 61 74 29 7b 0a 20 20  t minFormat){.  
22b4d 56 64 62 65 20 2a 76 3b 0a 20 20 76 20 3d 20 73  Vdbe *v;.  v = s
22b4e 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
22b4f 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29  arse);.  if( v )
22b50 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73  {.    int r1 = s
22b51 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
22b52 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 6e  (pParse);.    in
22b53 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65  t r2 = sqlite3Ge
22b54 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
22b55 3b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20  ;.    int j1;.  
22b56 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22b57 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f  Op3(v, OP_ReadCo
22b58 6f 6b 69 65 2c 20 69 44 62 2c 20 72 31 2c 20 31  okie, iDb, r1, 1
22b59 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22b5a 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
22b5b 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
22b5c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22b5d 5f 49 6e 74 65 67 65 72 2c 20 6d 69 6e 46 6f 72  _Integer, minFor
22b5e 6d 61 74 2c 20 72 32 29 3b 0a 20 20 20 20 6a 31  mat, r2);.    j1
22b5f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
22b60 64 4f 70 33 28 76 2c 20 4f 50 5f 47 65 2c 20 72  dOp3(v, OP_Ge, r
22b61 32 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20 20 73  2, 0, r1);.    s
22b62 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22b63 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  (v, OP_SetCookie
22b64 2c 20 69 44 62 2c 20 31 2c 20 72 32 29 3b 0a 20  , iDb, 1, r2);. 
22b65 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
22b66 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20  mpHere(v, j1);. 
22b67 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
22b68 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
22b69 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   r1);.    sqlite
22b6a 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
22b6b 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 7d  pParse, r2);.  }
22b6c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74  .}../*.** Fill t
22b6d 68 65 20 49 6e 64 65 78 2e 61 69 52 6f 77 45 73  he Index.aiRowEs
22b6e 74 5b 5d 20 61 72 72 61 79 20 77 69 74 68 20 64  t[] array with d
22b6f 65 66 61 75 6c 74 20 69 6e 66 6f 72 6d 61 74 69  efault informati
22b70 6f 6e 20 2d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  on - information
22b71 0a 2a 2a 20 74 6f 20 62 65 20 75 73 65 64 20 77  .** to be used w
22b72 68 65 6e 20 77 65 20 68 61 76 65 20 6e 6f 74 20  hen we have not 
22b73 72 75 6e 20 74 68 65 20 41 4e 41 4c 59 5a 45 20  run the ANALYZE 
22b74 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61  command..**.** a
22b75 69 52 6f 77 45 73 74 5b 30 5d 20 69 73 20 73 75  iRowEst[0] is su
22b76 70 70 6f 73 65 20 74 6f 20 63 6f 6e 74 61 69 6e  ppose to contain
22b77 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
22b78 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 69  lements in the i
22b79 6e 64 65 78 2e 0a 2a 2a 20 53 69 6e 63 65 20 77  ndex..** Since w
22b7a 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 2c 20 67  e do not know, g
22b7b 75 65 73 73 20 31 20 6d 69 6c 6c 69 6f 6e 2e 20  uess 1 million. 
22b7c 20 61 69 52 6f 77 45 73 74 5b 31 5d 20 69 73 20   aiRowEst[1] is 
22b7d 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74  an estimate of t
22b7e 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
22b7f 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c  rows in the tabl
22b80 65 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79  e that match any
22b81 20 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75   particular valu
22b82 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73  e of the.** firs
22b83 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
22b84 69 6e 64 65 78 2e 20 20 61 69 52 6f 77 45 73 74  index.  aiRowEst
22b85 5b 32 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61  [2] is an estima
22b86 74 65 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  te of the number
22b87 0a 2a 2a 20 6f 66 20 72 6f 77 73 20 74 68 61 74  .** of rows that
22b88 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 74 69   match any parti
22b89 63 75 6c 61 72 20 63 6f 6d 62 69 6e 69 61 74 69  cular combiniati
22b8a 6f 6e 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  on of the first 
22b8b 32 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6f 66 20  2 columns.** of 
22b8c 74 68 65 20 69 6e 64 65 78 2e 20 20 41 6e 64 20  the index.  And 
22b8d 73 6f 20 66 6f 72 74 68 2e 20 20 49 74 20 6d 75  so forth.  It mu
22b8e 73 74 20 61 6c 77 61 79 73 20 62 65 20 74 68 65  st always be the
22b8f 20 63 61 73 65 20 74 68 61 74 0a 2a 0a 2a 2a 20   case that.*.** 
22b90 20 20 20 20 20 20 20 20 20 20 61 69 52 6f 77 45            aiRowE
22b91 73 74 5b 4e 5d 3c 3d 61 69 52 6f 77 45 73 74 5b  st[N]<=aiRowEst[
22b92 4e 2d 31 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  N-1].**         
22b93 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d 3e 3d 31    aiRowEst[N]>=1
22b94 0a 2a 2a 0a 2a 2a 20 41 70 61 72 74 20 66 72 6f  .**.** Apart fro
22b95 6d 20 74 68 61 74 2c 20 77 65 20 68 61 76 65 20  m that, we have 
22b96 6c 69 74 74 6c 65 20 74 6f 20 67 6f 20 6f 6e 20  little to go on 
22b97 62 65 73 69 64 65 73 20 69 6e 74 75 69 74 69 6f  besides intuitio
22b98 6e 20 61 73 20 74 6f 0a 2a 2a 20 68 6f 77 20 61  n as to.** how a
22b99 69 52 6f 77 45 73 74 5b 5d 20 73 68 6f 75 6c 64  iRowEst[] should
22b9a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
22b9b 20 20 54 68 65 20 6e 75 6d 62 65 72 73 20 67 65    The numbers ge
22b9c 6e 65 72 61 74 65 64 20 68 65 72 65 0a 2a 2a 20  nerated here.** 
22b9d 61 72 65 20 62 61 73 65 64 20 6f 6e 20 74 79 70  are based on typ
22b9e 69 63 61 6c 20 76 61 6c 75 65 73 20 66 6f 75 6e  ical values foun
22b9f 64 20 69 6e 20 61 63 74 75 61 6c 20 69 6e 64 69  d in actual indi
22ba0 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ces..*/.SQLITE_P
22ba1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
22ba2 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74  te3DefaultRowEst
22ba3 28 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20  (Index *pIdx){. 
22ba4 20 75 6e 73 69 67 6e 65 64 20 2a 61 20 3d 20 70   unsigned *a = p
22ba5 49 64 78 2d 3e 61 69 52 6f 77 45 73 74 3b 0a 20  Idx->aiRowEst;. 
22ba6 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
22ba7 28 20 61 21 3d 30 20 29 3b 0a 20 20 61 5b 30 5d  ( a!=0 );.  a[0]
22ba8 20 3d 20 31 30 30 30 30 30 30 3b 0a 20 20 66 6f   = 1000000;.  fo
22ba9 72 28 69 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  r(i=pIdx->nColum
22baa 6e 3b 20 69 3e 3d 35 3b 20 69 2d 2d 29 7b 0a 20  n; i>=5; i--){. 
22bab 20 20 20 61 5b 69 5d 20 3d 20 35 3b 0a 20 20 7d     a[i] = 5;.  }
22bac 0a 20 20 77 68 69 6c 65 28 20 69 3e 3d 31 20 29  .  while( i>=1 )
22bad 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 31 31 20  {.    a[i] = 11 
22bae 2d 20 69 3b 0a 20 20 20 20 69 2d 2d 3b 0a 20 20  - i;.    i--;.  
22baf 7d 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e  }.  if( pIdx->on
22bb0 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29  Error!=OE_None )
22bb1 7b 0a 20 20 20 20 61 5b 70 49 64 78 2d 3e 6e 43  {.    a[pIdx->nC
22bb2 6f 6c 75 6d 6e 5d 20 3d 20 31 3b 0a 20 20 7d 0a  olumn] = 1;.  }.
22bb3 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
22bb4 75 74 69 6e 65 20 77 69 6c 6c 20 64 72 6f 70 20  utine will drop 
22bb5 61 6e 20 65 78 69 73 74 69 6e 67 20 6e 61 6d 65  an existing name
22bb6 64 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20 72  d index.  This r
22bb7 6f 75 74 69 6e 65 0a 2a 2a 20 69 6d 70 6c 65 6d  outine.** implem
22bb8 65 6e 74 73 20 74 68 65 20 44 52 4f 50 20 49 4e  ents the DROP IN
22bb9 44 45 58 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  DEX statement..*
22bba 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22bbb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
22bbc 70 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50  pIndex(Parse *pP
22bbd 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
22bbe 4e 61 6d 65 2c 20 69 6e 74 20 69 66 45 78 69 73  Name, int ifExis
22bbf 74 73 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49  ts){.  Index *pI
22bc0 6e 64 65 78 3b 0a 20 20 56 64 62 65 20 2a 76 3b  ndex;.  Vdbe *v;
22bc1 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
22bc2 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
22bc3 6e 74 20 69 44 62 3b 0a 0a 20 20 69 66 28 20 70  nt iDb;..  if( p
22bc4 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
22bc5 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22bc6 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
22bc7 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d  _drop_index;.  }
22bc8 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65  .  assert( pName
22bc9 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69  ->nSrc==1 );.  i
22bca 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
22bcb 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
22bcc 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 67  pParse) ){.    g
22bcd 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e  oto exit_drop_in
22bce 64 65 78 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65  dex;.  }.  pInde
22bcf 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  x = sqlite3FindI
22bd0 6e 64 65 78 28 64 62 2c 20 70 4e 61 6d 65 2d 3e  ndex(db, pName->
22bd1 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d  a[0].zName, pNam
22bd2 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73  e->a[0].zDatabas
22bd3 65 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78  e);.  if( pIndex
22bd4 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ==0 ){.    if( !
22bd5 69 66 45 78 69 73 74 73 20 29 7b 0a 20 20 20 20  ifExists ){.    
22bd6 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
22bd7 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75  g(pParse, "no su
22bd8 63 68 20 69 6e 64 65 78 3a 20 25 53 22 2c 20 70  ch index: %S", p
22bd9 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  Name, 0);.    }.
22bda 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65 63      pParse->chec
22bdb 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20 20  kSchema = 1;.   
22bdc 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
22bdd 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28  index;.  }.  if(
22bde 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f 49 6e 64   pIndex->autoInd
22bdf 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ex ){.    sqlite
22be0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
22be1 2c 20 22 69 6e 64 65 78 20 61 73 73 6f 63 69 61  , "index associa
22be2 74 65 64 20 77 69 74 68 20 55 4e 49 51 55 45 20  ted with UNIQUE 
22be3 22 0a 20 20 20 20 20 20 22 6f 72 20 50 52 49 4d  ".      "or PRIM
22be4 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69  ARY KEY constrai
22be5 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 72 6f  nt cannot be dro
22be6 70 70 65 64 22 2c 20 30 29 3b 0a 20 20 20 20 67  pped", 0);.    g
22be7 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e  oto exit_drop_in
22be8 64 65 78 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d  dex;.  }.  iDb =
22be9 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
22bea 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65 78  Index(db, pIndex
22beb 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 23 69 66 6e  ->pSchema);.#ifn
22bec 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22bed 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
22bee 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d  {.    int code =
22bef 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44   SQLITE_DROP_IND
22bf0 45 58 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  EX;.    Table *p
22bf1 54 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54  Tab = pIndex->pT
22bf2 61 62 6c 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  able;.    const 
22bf3 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e  char *zDb = db->
22bf4 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
22bf5 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
22bf6 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41  zTab = SCHEMA_TA
22bf7 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 69 66  BLE(iDb);.    if
22bf8 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
22bf9 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
22bfa 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20  E_DELETE, zTab, 
22bfb 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  0, zDb) ){.     
22bfc 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
22bfd 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20  index;.    }.   
22bfe 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
22bff 42 20 26 26 20 69 44 62 20 29 20 63 6f 64 65 20  B && iDb ) code 
22c00 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  = SQLITE_DROP_TE
22c01 4d 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66  MP_INDEX;.    if
22c02 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
22c03 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c  ck(pParse, code,
22c04 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20   pIndex->zName, 
22c05 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62  pTab->zName, zDb
22c06 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
22c07 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
22c08 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
22c09 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  f..  /* Generate
22c0a 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20   code to remove 
22c0b 74 68 65 20 69 6e 64 65 78 20 61 6e 64 20 66 72  the index and fr
22c0c 6f 6d 20 74 68 65 20 6d 61 73 74 65 72 20 74 61  om the master ta
22c0d 62 6c 65 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  ble */.  v = sql
22c0e 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
22c0f 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
22c10 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
22c11 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
22c12 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a  Parse, 1, iDb);.
22c13 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
22c14 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
22c15 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
22c16 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20 6e  OM %Q.%s WHERE n
22c17 61 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 20  ame=%Q",.       
22c18 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
22c19 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  me, SCHEMA_TABLE
22c1a 28 69 44 62 29 2c 0a 20 20 20 20 20 20 20 70 49  (iDb),.       pI
22c1b 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20  ndex->zName.    
22c1c 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
22c1d 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
22c1e 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20  "sqlite_stat1", 
22c1f 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
22c20 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  me) ){.      sql
22c21 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
22c22 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20  pParse,.        
22c23 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
22c24 73 71 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45  sqlite_stat1 WHE
22c25 52 45 20 69 64 78 3d 25 51 22 2c 0a 20 20 20 20  RE idx=%Q",.    
22c26 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d      db->aDb[iDb]
22c27 2e 7a 4e 61 6d 65 2c 20 70 49 6e 64 65 78 2d 3e  .zName, pIndex->
22c28 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20  zName.      );. 
22c29 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
22c2a 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
22c2b 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 64  rse, iDb);.    d
22c2c 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70  estroyRootPage(p
22c2d 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2d 3e 74  Parse, pIndex->t
22c2e 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  num, iDb);.    s
22c2f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
22c30 28 76 2c 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78  (v, OP_DropIndex
22c31 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 49 6e  , iDb, 0, 0, pIn
22c32 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  dex->zName, 0);.
22c33 20 20 7d 0a 0a 65 78 69 74 5f 64 72 6f 70 5f 69    }..exit_drop_i
22c34 6e 64 65 78 3a 0a 20 20 73 71 6c 69 74 65 33 53  ndex:.  sqlite3S
22c35 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
22c36 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   pName);.}../*.*
22c37 2a 20 70 41 72 72 61 79 20 69 73 20 61 20 70 6f  * pArray is a po
22c38 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61  inter to an arra
22c39 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 45  y of objects.  E
22c3a 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20 74 68  ach object in th
22c3b 65 0a 2a 2a 20 61 72 72 61 79 20 69 73 20 73 7a  e.** array is sz
22c3c 45 6e 74 72 79 20 62 79 74 65 73 20 69 6e 20 73  Entry bytes in s
22c3d 69 7a 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ize.  This routi
22c3e 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e  ne allocates a n
22c3f 65 77 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e 20  ew.** object on 
22c40 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 61  the end of the a
22c41 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45  rray..**.** *pnE
22c42 6e 74 72 79 20 69 73 20 74 68 65 20 6e 75 6d 62  ntry is the numb
22c43 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c  er of entries al
22c44 72 65 61 64 79 20 69 6e 20 75 73 65 2e 20 20 2a  ready in use.  *
22c45 70 6e 41 6c 6c 6f 63 20 69 73 0a 2a 2a 20 74 68  pnAlloc is.** th
22c46 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  e previously all
22c47 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 74  ocated size of t
22c48 68 65 20 61 72 72 61 79 2e 20 20 69 6e 69 74 53  he array.  initS
22c49 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 73 75  ize is the.** su
22c4a 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20  ggested initial 
22c4b 61 72 72 61 79 20 73 69 7a 65 20 61 6c 6c 6f 63  array size alloc
22c4c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
22c4d 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 65   index of the ne
22c4e 77 20 65 6e 74 72 79 20 69 73 20 72 65 74 75 72  w entry is retur
22c4f 6e 65 64 20 69 6e 20 2a 70 49 64 78 2e 0a 2a 2a  ned in *pIdx..**
22c50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
22c51 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
22c52 65 72 20 74 6f 20 74 68 65 20 61 72 72 61 79 20  er to the array 
22c53 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 69  of objects.  Thi
22c54 73 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 74 68  s.** might be th
22c55 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 70 41  e same as the pA
22c56 72 72 61 79 20 70 61 72 61 6d 65 74 65 72 20 6f  rray parameter o
22c57 72 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 20  r it might be a 
22c58 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 70 6f 69  different.** poi
22c59 6e 74 65 72 20 69 66 20 74 68 65 20 61 72 72 61  nter if the arra
22c5a 79 20 77 61 73 20 72 65 73 69 7a 65 64 2e 0a 2a  y was resized..*
22c5b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22c5c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72   void *sqlite3Ar
22c5d 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 73  rayAllocate(.  s
22c5e 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
22c5f 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74   /* Connection t
22c60 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c  o notify of mall
22c61 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20  oc failures */. 
22c62 20 76 6f 69 64 20 2a 70 41 72 72 61 79 2c 20 20   void *pArray,  
22c63 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6f     /* Array of o
22c64 62 6a 65 63 74 73 2e 20 20 4d 69 67 68 74 20 62  bjects.  Might b
22c65 65 20 72 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  e reallocated */
22c66 0a 20 20 69 6e 74 20 73 7a 45 6e 74 72 79 2c 20  .  int szEntry, 
22c67 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
22c68 65 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20 74  each object in t
22c69 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e  he array */.  in
22c6a 74 20 69 6e 69 74 53 69 7a 65 2c 20 20 20 20 20  t initSize,     
22c6b 2f 2a 20 53 75 67 67 65 73 74 65 64 20 69 6e 69  /* Suggested ini
22c6c 74 69 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  tial allocation,
22c6d 20 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a   in elements */.
22c6e 20 20 69 6e 74 20 2a 70 6e 45 6e 74 72 79 2c 20    int *pnEntry, 
22c6f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
22c70 20 6f 62 6a 65 63 74 73 20 63 75 72 72 65 6e 74   objects current
22c71 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 69  ly in use */.  i
22c72 6e 74 20 2a 70 6e 41 6c 6c 6f 63 2c 20 20 20 20  nt *pnAlloc,    
22c73 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65   /* Current size
22c74 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
22c75 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73 20  on, in elements 
22c76 2a 2f 0a 20 20 69 6e 74 20 2a 70 49 64 78 20 20  */.  int *pIdx  
22c77 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
22c78 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 6e  the index of a n
22c79 65 77 20 73 6c 6f 74 20 68 65 72 65 20 2a 2f 0a  ew slot here */.
22c7a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  ){.  char *z;.  
22c7b 69 66 28 20 2a 70 6e 45 6e 74 72 79 20 3e 3d 20  if( *pnEntry >= 
22c7c 2a 70 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  *pnAlloc ){.    
22c7d 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 20 20  void *pNew;.    
22c7e 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20 20  int newSize;.   
22c7f 20 6e 65 77 53 69 7a 65 20 3d 20 28 2a 70 6e 41   newSize = (*pnA
22c80 6c 6c 6f 63 29 2a 32 20 2b 20 69 6e 69 74 53 69  lloc)*2 + initSi
22c81 7a 65 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73  ze;.    pNew = s
22c82 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
22c83 64 62 2c 20 70 41 72 72 61 79 2c 20 6e 65 77 53  db, pArray, newS
22c84 69 7a 65 2a 73 7a 45 6e 74 72 79 29 3b 0a 20 20  ize*szEntry);.  
22c85 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
22c86 0a 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 2d  .      *pIdx = -
22c87 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  1;.      return 
22c88 70 41 72 72 61 79 3b 0a 20 20 20 20 7d 0a 20 20  pArray;.    }.  
22c89 20 20 2a 70 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c    *pnAlloc = sql
22c8a 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
22c8b 28 64 62 2c 20 70 4e 65 77 29 2f 73 7a 45 6e 74  (db, pNew)/szEnt
22c8c 72 79 3b 0a 20 20 20 20 70 41 72 72 61 79 20 3d  ry;.    pArray =
22c8d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d   pNew;.  }.  z =
22c8e 20 28 63 68 61 72 2a 29 70 41 72 72 61 79 3b 0a   (char*)pArray;.
22c8f 20 20 6d 65 6d 73 65 74 28 26 7a 5b 2a 70 6e 45    memset(&z[*pnE
22c90 6e 74 72 79 20 2a 20 73 7a 45 6e 74 72 79 5d 2c  ntry * szEntry],
22c91 20 30 2c 20 73 7a 45 6e 74 72 79 29 3b 0a 20 20   0, szEntry);.  
22c92 2a 70 49 64 78 20 3d 20 2a 70 6e 45 6e 74 72 79  *pIdx = *pnEntry
22c93 3b 0a 20 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b 0a  ;.  ++*pnEntry;.
22c94 20 20 72 65 74 75 72 6e 20 70 41 72 72 61 79 3b    return pArray;
22c95 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64  .}../*.** Append
22c96 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74   a new element t
22c97 6f 20 74 68 65 20 67 69 76 65 6e 20 49 64 4c 69  o the given IdLi
22c98 73 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65  st.  Create a ne
22c99 77 20 49 64 4c 69 73 74 20 69 66 0a 2a 2a 20 6e  w IdList if.** n
22c9a 65 65 64 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  eed be..**.** A 
22c9b 6e 65 77 20 49 64 4c 69 73 74 20 69 73 20 72 65  new IdList is re
22c9c 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20  turned, or NULL 
22c9d 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c  if malloc() fail
22c9e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
22c9f 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c  VATE IdList *sql
22ca0 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64  ite3IdListAppend
22ca1 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64  (sqlite3 *db, Id
22ca2 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b  List *pList, Tok
22ca3 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 69  en *pToken){.  i
22ca4 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73  nt i;.  if( pLis
22ca5 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73  t==0 ){.    pLis
22ca6 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
22ca7 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
22ca8 6f 66 28 49 64 4c 69 73 74 29 20 29 3b 0a 20 20  of(IdList) );.  
22ca9 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
22caa 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 70   return 0;.    p
22cab 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30  List->nAlloc = 0
22cac 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61  ;.  }.  pList->a
22cad 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41   = sqlite3ArrayA
22cae 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 64  llocate(.      d
22caf 62 2c 0a 20 20 20 20 20 20 70 4c 69 73 74 2d 3e  b,.      pList->
22cb0 61 2c 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  a,.      sizeof(
22cb1 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20 20  pList->a[0]),.  
22cb2 20 20 20 20 35 2c 0a 20 20 20 20 20 20 26 70 4c      5,.      &pL
22cb3 69 73 74 2d 3e 6e 49 64 2c 0a 20 20 20 20 20 20  ist->nId,.      
22cb4 26 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a  &pList->nAlloc,.
22cb5 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20        &i.  );.  
22cb6 69 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 73  if( i<0 ){.    s
22cb7 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
22cb8 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
22cb9 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
22cba 0a 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  .  pList->a[i].z
22cbb 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
22cbc 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
22cbd 70 54 6f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72  pToken);.  retur
22cbe 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  n pList;.}../*.*
22cbf 2a 20 44 65 6c 65 74 65 20 61 6e 20 49 64 4c 69  * Delete an IdLi
22cc0 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  st..*/.SQLITE_PR
22cc1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
22cc2 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73  e3IdListDelete(s
22cc3 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69  qlite3 *db, IdLi
22cc4 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e  st *pList){.  in
22cc5 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  t i;.  if( pList
22cc6 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
22cc7 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
22cc8 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nId; i++){.   
22cc9 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22cca 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  b, pList->a[i].z
22ccb 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  Name);.  }.  sql
22ccc 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
22ccd 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69  List->a);.  sqli
22cce 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
22ccf 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ist);.}../*.** R
22cd0 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
22cd1 69 6e 20 70 4c 69 73 74 20 6f 66 20 74 68 65 20  in pList of the 
22cd2 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 64  identifier named
22cd3 20 7a 49 64 2e 20 20 52 65 74 75 72 6e 20 2d 31   zId.  Return -1
22cd4 0a 2a 2a 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  .** if not found
22cd5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22cd6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
22cd7 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 69 73  dListIndex(IdLis
22cd8 74 20 2a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 20  t *pList, const 
22cd9 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
22cda 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69  int i;.  if( pLi
22cdb 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d  st==0 ) return -
22cdc 31 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  1;.  for(i=0; i<
22cdd 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29  pList->nId; i++)
22cde 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
22cdf 33 53 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e  3StrICmp(pList->
22ce0 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  a[i].zName, zNam
22ce1 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69  e)==0 ) return i
22ce2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d  ;.  }.  return -
22ce3 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 61  1;.}../*.** Expa
22ce4 6e 64 20 74 68 65 20 73 70 61 63 65 20 61 6c 6c  nd the space all
22ce5 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 67  ocated for the g
22ce6 69 76 65 6e 20 53 72 63 4c 69 73 74 20 6f 62 6a  iven SrcList obj
22ce7 65 63 74 20 62 79 0a 2a 2a 20 63 72 65 61 74 69  ect by.** creati
22ce8 6e 67 20 6e 45 78 74 72 61 20 6e 65 77 20 73 6c  ng nExtra new sl
22ce9 6f 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 74  ots beginning at
22cea 20 69 53 74 61 72 74 2e 20 20 69 53 74 61 72 74   iStart.  iStart
22ceb 20 69 73 20 7a 65 72 6f 20 62 61 73 65 64 2e 0a   is zero based..
22cec 2a 2a 20 4e 65 77 20 73 6c 6f 74 73 20 61 72 65  ** New slots are
22ced 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 46   zeroed..**.** F
22cee 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 70  or example, supp
22cef 6f 73 65 20 61 20 53 72 63 4c 69 73 74 20 69 6e  ose a SrcList in
22cf0 69 74 69 61 6c 6c 79 20 63 6f 6e 74 61 69 6e 73  itially contains
22cf1 20 74 77 6f 20 65 6e 74 72 69 65 73 3a 20 41 2c   two entries: A,
22cf2 42 2e 0a 2a 2a 20 54 6f 20 61 70 70 65 6e 64 20  B..** To append 
22cf3 33 20 6e 65 77 20 65 6e 74 72 69 65 73 20 6f 6e  3 new entries on
22cf4 74 6f 20 74 68 65 20 65 6e 64 2c 20 64 6f 20 74  to the end, do t
22cf5 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71  his:.**.**    sq
22cf6 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61  lite3SrcListEnla
22cf7 72 67 65 28 64 62 2c 20 70 53 72 63 6c 69 73 74  rge(db, pSrclist
22cf8 2c 20 33 2c 20 32 29 3b 0a 2a 2a 0a 2a 2a 20 41  , 3, 2);.**.** A
22cf9 66 74 65 72 20 74 68 65 20 63 61 6c 6c 20 61 62  fter the call ab
22cfa 6f 76 65 20 69 74 20 77 6f 75 6c 64 20 63 6f 6e  ove it would con
22cfb 74 61 69 6e 3a 20 20 41 2c 20 42 2c 20 6e 69 6c  tain:  A, B, nil
22cfc 2c 20 6e 69 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20 49  , nil, nil..** I
22cfd 66 20 74 68 65 20 69 53 74 61 72 74 20 61 72 67  f the iStart arg
22cfe 75 6d 65 6e 74 20 68 61 64 20 62 65 65 6e 20 31  ument had been 1
22cff 20 69 6e 73 74 65 61 64 20 6f 66 20 32 2c 20 74   instead of 2, t
22d00 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  hen the result.*
22d01 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65  * would have bee
22d02 6e 3a 20 20 41 2c 20 6e 69 6c 2c 20 6e 69 6c 2c  n:  A, nil, nil,
22d03 20 6e 69 6c 2c 20 42 2e 20 20 54 6f 20 70 72 65   nil, B.  To pre
22d04 70 65 6e 64 20 74 68 65 20 6e 65 77 20 73 6c 6f  pend the new slo
22d05 74 73 2c 0a 2a 2a 20 74 68 65 20 69 53 74 61 72  ts,.** the iStar
22d06 74 20 76 61 6c 75 65 20 77 6f 75 6c 64 20 62 65  t value would be
22d07 20 30 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20   0.  The result 
22d08 74 68 65 6e 20 77 6f 75 6c 64 0a 2a 2a 20 62 65  then would.** be
22d09 3a 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c  : nil, nil, nil,
22d0a 20 41 2c 20 42 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   A, B..**.** If 
22d0b 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
22d0c 69 6f 6e 20 66 61 69 6c 73 20 74 68 65 20 53 72  ion fails the Sr
22d0d 63 4c 69 73 74 20 69 73 20 75 6e 63 68 61 6e 67  cList is unchang
22d0e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 64 62 2d 3e  ed.  The.** db->
22d0f 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
22d10 67 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f  g will be set to
22d11 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   true..*/.SQLITE
22d12 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
22d13 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
22d14 45 6e 6c 61 72 67 65 28 0a 20 20 73 71 6c 69 74  Enlarge(.  sqlit
22d15 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a  e3 *db,       /*
22d16 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
22d17 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f  tion to notify o
22d18 66 20 4f 4f 4d 20 65 72 72 6f 72 73 20 2a 2f 0a  f OOM errors */.
22d19 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
22d1a 20 20 20 20 20 2f 2a 20 54 68 65 20 53 72 63 4c       /* The SrcL
22d1b 69 73 74 20 74 6f 20 62 65 20 65 6e 6c 61 72 67  ist to be enlarg
22d1c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74  ed */.  int nExt
22d1d 72 61 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ra,        /* Nu
22d1e 6d 62 65 72 20 6f 66 20 6e 65 77 20 73 6c 6f 74  mber of new slot
22d1f 73 20 74 6f 20 61 64 64 20 74 6f 20 70 53 72 63  s to add to pSrc
22d20 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  ->a[] */.  int i
22d21 53 74 61 72 74 20 20 20 20 20 20 20 20 20 2f 2a  Start         /*
22d22 20 49 6e 64 65 78 20 69 6e 20 70 53 72 63 2d 3e   Index in pSrc->
22d23 61 5b 5d 20 6f 66 20 66 69 72 73 74 20 6e 65 77  a[] of first new
22d24 20 73 6c 6f 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e   slot */.){.  in
22d25 74 20 69 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74  t i;..  /* Sanit
22d26 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 63 61  y checking on ca
22d27 6c 6c 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73  lling parameters
22d28 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 53   */.  assert( iS
22d29 74 61 72 74 3e 3d 30 20 29 3b 0a 20 20 61 73 73  tart>=0 );.  ass
22d2a 65 72 74 28 20 6e 45 78 74 72 61 3e 3d 31 20 29  ert( nExtra>=1 )
22d2b 3b 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20  ;.  if( pSrc==0 
22d2c 7c 7c 20 69 53 74 61 72 74 3e 70 53 72 63 2d 3e  || iStart>pSrc->
22d2d 6e 53 72 63 20 29 7b 0a 20 20 20 20 61 73 73 65  nSrc ){.    asse
22d2e 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  rt( db->mallocFa
22d2f 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75  iled );.    retu
22d30 72 6e 20 70 53 72 63 3b 0a 20 20 7d 0a 0a 20 20  rn pSrc;.  }..  
22d31 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 64 64 69  /* Allocate addi
22d32 74 69 6f 6e 61 6c 20 73 70 61 63 65 20 69 66 20  tional space if 
22d33 6e 65 65 64 65 64 20 2a 2f 0a 20 20 69 66 28 20  needed */.  if( 
22d34 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 72  pSrc->nSrc+nExtr
22d35 61 3e 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 29  a>pSrc->nAlloc )
22d36 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70  {.    SrcList *p
22d37 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e 41 6c  New;.    int nAl
22d38 6c 6f 63 20 3d 20 70 53 72 63 2d 3e 6e 53 72 63  loc = pSrc->nSrc
22d39 2b 6e 45 78 74 72 61 3b 0a 20 20 20 20 69 6e 74  +nExtra;.    int
22d3a 20 6e 47 6f 74 3b 0a 20 20 20 20 70 4e 65 77 20   nGot;.    pNew 
22d3b 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
22d3c 6f 63 28 64 62 2c 20 70 53 72 63 2c 0a 20 20 20  oc(db, pSrc,.   
22d3d 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65              size
22d3e 6f 66 28 2a 70 53 72 63 29 20 2b 20 28 6e 41 6c  of(*pSrc) + (nAl
22d3f 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 53  loc-1)*sizeof(pS
22d40 72 63 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 20  rc->a[0]) );.   
22d41 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a   if( pNew==0 ){.
22d42 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62        assert( db
22d43 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
22d44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  ;.      return p
22d45 53 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  Src;.    }.    p
22d46 53 72 63 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  Src = pNew;.    
22d47 6e 47 6f 74 20 3d 20 28 73 71 6c 69 74 65 33 44  nGot = (sqlite3D
22d48 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
22d49 70 4e 65 77 29 20 2d 20 73 69 7a 65 6f 66 28 2a  pNew) - sizeof(*
22d4a 70 53 72 63 29 29 2f 73 69 7a 65 6f 66 28 70 53  pSrc))/sizeof(pS
22d4b 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b 0a 20 20 20  rc->a[0])+1;.   
22d4c 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 3d 20   pSrc->nAlloc = 
22d4d 28 75 31 36 29 6e 47 6f 74 3b 0a 20 20 7d 0a 0a  (u16)nGot;.  }..
22d4e 20 20 2f 2a 20 4d 6f 76 65 20 65 78 69 73 74 69    /* Move existi
22d4f 6e 67 20 73 6c 6f 74 73 20 74 68 61 74 20 63 6f  ng slots that co
22d50 6d 65 20 61 66 74 65 72 20 74 68 65 20 6e 65 77  me after the new
22d51 6c 79 20 69 6e 73 65 72 74 65 64 20 73 6c 6f 74  ly inserted slot
22d52 73 0a 20 20 2a 2a 20 6f 75 74 20 6f 66 20 74 68  s.  ** out of th
22d53 65 20 77 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69  e way */.  for(i
22d54 3d 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20 69  =pSrc->nSrc-1; i
22d55 3e 3d 69 53 74 61 72 74 3b 20 69 2d 2d 29 7b 0a  >=iStart; i--){.
22d56 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b 6e 45      pSrc->a[i+nE
22d57 78 74 72 61 5d 20 3d 20 70 53 72 63 2d 3e 61 5b  xtra] = pSrc->a[
22d58 69 5d 3b 0a 20 20 7d 0a 20 20 70 53 72 63 2d 3e  i];.  }.  pSrc->
22d59 6e 53 72 63 20 2b 3d 20 28 69 31 36 29 6e 45 78  nSrc += (i16)nEx
22d5a 74 72 61 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20  tra;..  /* Zero 
22d5b 74 68 65 20 6e 65 77 6c 79 20 61 6c 6c 6f 63 61  the newly alloca
22d5c 74 65 64 20 73 6c 6f 74 73 20 2a 2f 0a 20 20 6d  ted slots */.  m
22d5d 65 6d 73 65 74 28 26 70 53 72 63 2d 3e 61 5b 69  emset(&pSrc->a[i
22d5e 53 74 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65 6f  Start], 0, sizeo
22d5f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2a 6e 45  f(pSrc->a[0])*nE
22d60 78 74 72 61 29 3b 0a 20 20 66 6f 72 28 69 3d 69  xtra);.  for(i=i
22d61 53 74 61 72 74 3b 20 69 3c 69 53 74 61 72 74 2b  Start; i<iStart+
22d62 6e 45 78 74 72 61 3b 20 69 2b 2b 29 7b 0a 20 20  nExtra; i++){.  
22d63 20 20 70 53 72 63 2d 3e 61 5b 69 5d 2e 69 43 75    pSrc->a[i].iCu
22d64 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a  rsor = -1;.  }..
22d65 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f    /* Return a po
22d66 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e 6c  inter to the enl
22d67 61 72 67 65 64 20 53 72 63 4c 69 73 74 20 2a 2f  arged SrcList */
22d68 0a 20 20 72 65 74 75 72 6e 20 70 53 72 63 3b 0a  .  return pSrc;.
22d69 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64  }.../*.** Append
22d6a 20 61 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d   a new table nam
22d6b 65 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20 53  e to the given S
22d6c 72 63 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20  rcList.  Create 
22d6d 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 69 66  a new SrcList if
22d6e 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 20 20 41 20  .** need be.  A 
22d6f 6e 65 77 20 65 6e 74 72 79 20 69 73 20 63 72 65  new entry is cre
22d70 61 74 65 64 20 69 6e 20 74 68 65 20 53 72 63 4c  ated in the SrcL
22d71 69 73 74 20 65 76 65 6e 20 69 66 20 70 54 6f 6b  ist even if pTok
22d72 65 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  en is NULL..**.*
22d73 2a 20 41 20 53 72 63 4c 69 73 74 20 69 73 20 72  * A SrcList is r
22d74 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c  eturned, or NULL
22d75 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
22d76 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 54 68 65 20  OOM error.  The 
22d77 72 65 74 75 72 6e 65 64 0a 2a 2a 20 53 72 63 4c  returned.** SrcL
22d78 69 73 74 20 6d 69 67 68 74 20 62 65 20 74 68 65  ist might be the
22d79 20 73 61 6d 65 20 61 73 20 74 68 65 20 53 72 63   same as the Src
22d7a 4c 69 73 74 20 74 68 61 74 20 77 61 73 20 69 6e  List that was in
22d7b 70 75 74 20 6f 72 20 69 74 20 6d 69 67 68 74 20  put or it might 
22d7c 62 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e 65 2e  be.** a new one.
22d7d 20 20 49 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f    If an OOM erro
22d7e 72 20 64 6f 65 73 20 6f 63 63 75 72 73 2c 20 74  r does occurs, t
22d7f 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 76 61  hen the prior va
22d80 6c 75 65 20 6f 66 20 70 4c 69 73 74 0a 2a 2a 20  lue of pList.** 
22d81 74 68 61 74 20 69 73 20 69 6e 70 75 74 20 74 6f  that is input to
22d82 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
22d83 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66   automatically f
22d84 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  reed..**.** If p
22d85 44 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20  Database is not 
22d86 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74  null, it means t
22d87 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61  hat the table ha
22d88 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a 2a 2a  s an optional.**
22d89 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 70   database name p
22d8a 72 65 66 69 78 2e 20 20 4c 69 6b 65 20 74 68 69  refix.  Like thi
22d8b 73 3a 20 20 22 64 61 74 61 62 61 73 65 2e 74 61  s:  "database.ta
22d8c 62 6c 65 22 2e 20 20 54 68 65 20 70 44 61 74 61  ble".  The pData
22d8d 62 61 73 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  base.** points t
22d8e 6f 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  o the table name
22d8f 20 61 6e 64 20 74 68 65 20 70 54 61 62 6c 65 20   and the pTable 
22d90 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 64 61  points to the da
22d91 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a 2a 20  tabase name..** 
22d92 54 68 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e  The SrcList.a[].
22d93 7a 4e 61 6d 65 20 66 69 65 6c 64 20 69 73 20 66  zName field is f
22d94 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 74  illed with the t
22d95 61 62 6c 65 20 6e 61 6d 65 20 77 68 69 63 68 20  able name which 
22d96 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d 65 20 66 72  might.** come fr
22d97 6f 6d 20 70 54 61 62 6c 65 20 28 69 66 20 70 44  om pTable (if pD
22d98 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 29  atabase is NULL)
22d99 20 6f 72 20 66 72 6f 6d 20 70 44 61 74 61 62 61   or from pDataba
22d9a 73 65 2e 20 20 0a 2a 2a 20 53 72 63 4c 69 73 74  se.  .** SrcList
22d9b 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 73 65 20 69  .a[].zDatabase i
22d9c 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68  s filled with th
22d9d 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
22d9e 66 72 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a 2a 20  from pTable,.** 
22d9f 6f 72 20 77 69 74 68 20 4e 55 4c 4c 20 69 66 20  or with NULL if 
22da0 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73 20 73  no database is s
22da1 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  pecified..**.** 
22da2 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
22da3 69 66 20 63 61 6c 6c 20 6c 69 6b 65 20 74 68 69  if call like thi
22da4 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
22da5 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
22da6 70 70 65 6e 64 28 44 2c 41 2c 42 2c 30 29 3b 0a  ppend(D,A,B,0);.
22da7 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42 20 69 73 20  **.** Then B is 
22da8 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64  a table name and
22da9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
22daa 6d 65 20 69 73 20 75 6e 73 70 65 63 69 66 69 65  me is unspecifie
22dab 64 2e 20 20 49 66 20 63 61 6c 6c 65 64 0a 2a 2a  d.  If called.**
22dac 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
22dad 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  *         sqlite
22dae 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44  3SrcListAppend(D
22daf 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 54  ,A,B,C);.**.** T
22db0 68 65 6e 20 43 20 69 73 20 74 68 65 20 74 61 62  hen C is the tab
22db1 6c 65 20 6e 61 6d 65 20 61 6e 64 20 42 20 69 73  le name and B is
22db2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
22db3 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  me..*/.SQLITE_PR
22db4 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
22db5 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
22db6 65 6e 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  end(.  sqlite3 *
22db7 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  db,        /* Co
22db8 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69  nnection to noti
22db9 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69  fy of malloc fai
22dba 6c 75 72 65 73 20 2a 2f 0a 20 20 53 72 63 4c 69  lures */.  SrcLi
22dbb 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f  st *pList,     /
22dbc 2a 20 41 70 70 65 6e 64 20 74 6f 20 74 68 69 73  * Append to this
22dbd 20 53 72 63 4c 69 73 74 2e 20 4e 55 4c 4c 20 63   SrcList. NULL c
22dbe 72 65 61 74 65 73 20 61 20 6e 65 77 20 53 72 63  reates a new Src
22dbf 4c 69 73 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  List */.  Token 
22dc0 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a  *pTable,      /*
22dc1 20 54 61 62 6c 65 20 74 6f 20 61 70 70 65 6e 64   Table to append
22dc2 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61   */.  Token *pDa
22dc3 74 61 62 61 73 65 20 20 20 20 2f 2a 20 44 61 74  tabase    /* Dat
22dc4 61 62 61 73 65 20 6f 66 20 74 68 65 20 74 61 62  abase of the tab
22dc5 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63  le */.){.  struc
22dc6 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
22dc7 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69  pItem;.  if( pLi
22dc8 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69  st==0 ){.    pLi
22dc9 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  st = sqlite3DbMa
22dca 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
22dcb 65 6f 66 28 53 72 63 4c 69 73 74 29 20 29 3b 0a  eof(SrcList) );.
22dcc 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30      if( pList==0
22dcd 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
22dce 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d   pList->nAlloc =
22dcf 20 31 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 20   1;.  }.  pList 
22dd0 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
22dd1 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 4c 69 73  Enlarge(db, pLis
22dd2 74 2c 20 31 2c 20 70 4c 69 73 74 2d 3e 6e 53 72  t, 1, pList->nSr
22dd3 63 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  c);.  if( db->ma
22dd4 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
22dd5 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
22dd6 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
22dd7 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
22dd8 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26  .  }.  pItem = &
22dd9 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e  pList->a[pList->
22dda 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66 28 20 70  nSrc-1];.  if( p
22ddb 44 61 74 61 62 61 73 65 20 26 26 20 70 44 61 74  Database && pDat
22ddc 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29 7b 0a 20  abase->z==0 ){. 
22ddd 20 20 20 70 44 61 74 61 62 61 73 65 20 3d 20 30     pDatabase = 0
22dde 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61 74  ;.  }.  if( pDat
22ddf 61 62 61 73 65 20 26 26 20 70 54 61 62 6c 65 20  abase && pTable 
22de0 29 7b 0a 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54  ){.    Token *pT
22de1 65 6d 70 20 3d 20 70 44 61 74 61 62 61 73 65 3b  emp = pDatabase;
22de2 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20 3d  .    pDatabase =
22de3 20 70 54 61 62 6c 65 3b 0a 20 20 20 20 70 54 61   pTable;.    pTa
22de4 62 6c 65 20 3d 20 70 54 65 6d 70 3b 0a 20 20 7d  ble = pTemp;.  }
22de5 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20  .  pItem->zName 
22de6 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
22de7 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c  mToken(db, pTabl
22de8 65 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 7a 44 61  e);.  pItem->zDa
22de9 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33  tabase = sqlite3
22dea 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
22deb 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20 20  , pDatabase);.  
22dec 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a  return pList;.}.
22ded 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 56 64  ./*.** Assign Vd
22dee 62 65 43 75 72 73 6f 72 20 69 6e 64 65 78 20 6e  beCursor index n
22def 75 6d 62 65 72 73 20 74 6f 20 61 6c 6c 20 74 61  umbers to all ta
22df0 62 6c 65 73 20 69 6e 20 61 20 53 72 63 4c 69 73  bles in a SrcLis
22df1 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  t.*/.SQLITE_PRIV
22df2 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22df3 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72  SrcListAssignCur
22df4 73 6f 72 73 28 50 61 72 73 65 20 2a 70 50 61 72  sors(Parse *pPar
22df5 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c 69  se, SrcList *pLi
22df6 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  st){.  int i;.  
22df7 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
22df8 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 61 73  tem *pItem;.  as
22df9 73 65 72 74 28 70 4c 69 73 74 20 7c 7c 20 70 50  sert(pList || pP
22dfa 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
22dfb 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
22dfc 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72  pList ){.    for
22dfd 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73  (i=0, pItem=pLis
22dfe 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
22dff 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
22e00 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49  +){.      if( pI
22e01 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3e 3d 30 20  tem->iCursor>=0 
22e02 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70  ) break;.      p
22e03 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20  Item->iCursor = 
22e04 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
22e05 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
22e06 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
22e07 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
22e08 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
22e09 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70  pParse, pItem->p
22e0a 53 65 6c 65 63 74 2d 3e 70 53 72 63 29 3b 0a 20  Select->pSrc);. 
22e0b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
22e0c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
22e0d 20 61 6e 20 65 6e 74 69 72 65 20 53 72 63 4c 69   an entire SrcLi
22e0e 73 74 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c 6c  st including all
22e0f 20 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72   its substructur
22e10 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
22e11 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
22e12 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73  3SrcListDelete(s
22e13 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c  qlite3 *db, SrcL
22e14 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
22e15 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53  nt i;.  struct S
22e16 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
22e17 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d  em;.  if( pList=
22e18 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  =0 ) return;.  f
22e19 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  or(pItem=pList->
22e1a 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  a, i=0; i<pList-
22e1b 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65  >nSrc; i++, pIte
22e1c 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  m++){.    sqlite
22e1d 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65  3DbFree(db, pIte
22e1e 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  m->zDatabase);. 
22e1f 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22e20 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  (db, pItem->zNam
22e21 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  e);.    sqlite3D
22e22 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d  bFree(db, pItem-
22e23 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 73 71  >zAlias);.    sq
22e24 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
22e25 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a  pItem->zIndex);.
22e26 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
22e27 65 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 54  eTable(pItem->pT
22e28 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ab);.    sqlite3
22e29 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
22e2a 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29   pItem->pSelect)
22e2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
22e2c 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65  rDelete(db, pIte
22e2d 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 73 71 6c  m->pOn);.    sql
22e2e 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
22e2f 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 55 73 69  (db, pItem->pUsi
22e30 6e 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ng);.  }.  sqlit
22e31 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69  e3DbFree(db, pLi
22e32 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  st);.}../*.** Th
22e33 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
22e34 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
22e35 65 72 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20  er to add a new 
22e36 74 65 72 6d 20 74 6f 20 74 68 65 0a 2a 2a 20 65  term to the.** e
22e37 6e 64 20 6f 66 20 61 20 67 72 6f 77 69 6e 67 20  nd of a growing 
22e38 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68  FROM clause.  Th
22e39 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20  e "p" parameter 
22e3a 69 73 20 74 68 65 20 70 61 72 74 20 6f 66 0a 2a  is the part of.*
22e3b 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
22e3c 65 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61  e that has alrea
22e3d 64 79 20 62 65 65 6e 20 63 6f 6e 73 74 72 75 63  dy been construc
22e3e 74 65 64 2e 20 20 22 70 22 20 69 73 20 4e 55 4c  ted.  "p" is NUL
22e3f 4c 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20  L.** if this is 
22e40 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 6f  the first term o
22e41 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
22e42 65 2e 20 20 70 54 61 62 6c 65 20 61 6e 64 20 70  e.  pTable and p
22e43 44 61 74 61 62 61 73 65 0a 2a 2a 20 61 72 65 20  Database.** are 
22e44 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
22e45 74 61 62 6c 65 20 61 6e 64 20 64 61 74 61 62 61  table and databa
22e46 73 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20  se named in the 
22e47 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
22e48 2e 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 69  ..** pDatabase i
22e49 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 64 61  s NULL if the da
22e4a 74 61 62 61 73 65 20 6e 61 6d 65 20 71 75 61 6c  tabase name qual
22e4b 69 66 69 65 72 20 69 73 20 6d 69 73 73 69 6e 67  ifier is missing
22e4c 20 2d 20 74 68 65 0a 2a 2a 20 75 73 75 61 6c 20   - the.** usual 
22e4d 63 61 73 65 2e 20 20 49 66 20 74 68 65 20 74 65  case.  If the te
22e4e 72 6d 20 68 61 73 20 61 20 61 6c 69 61 73 2c 20  rm has a alias, 
22e4f 74 68 65 6e 20 70 41 6c 69 61 73 20 70 6f 69 6e  then pAlias poin
22e50 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 69  ts to the.** ali
22e51 61 73 20 74 6f 6b 65 6e 2e 20 20 49 66 20 74 68  as token.  If th
22e52 65 20 74 65 72 6d 20 69 73 20 61 20 73 75 62 71  e term is a subq
22e53 75 65 72 79 2c 20 74 68 65 6e 20 70 53 75 62 71  uery, then pSubq
22e54 75 65 72 79 20 69 73 20 74 68 65 0a 2a 2a 20 53  uery is the.** S
22e55 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
22e56 74 68 61 74 20 74 68 65 20 73 75 62 71 75 65 72  that the subquer
22e57 79 20 65 6e 63 6f 64 65 73 2e 20 20 54 68 65 20  y encodes.  The 
22e58 70 54 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 70 44  pTable and.** pD
22e59 61 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65  atabase paramete
22e5a 72 73 20 61 72 65 20 4e 55 4c 4c 20 66 6f 72 20  rs are NULL for 
22e5b 73 75 62 71 75 65 72 69 65 73 2e 20 20 54 68 65  subqueries.  The
22e5c 20 70 4f 6e 20 61 6e 64 20 70 55 73 69 6e 67 0a   pOn and pUsing.
22e5d 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  ** parameters ar
22e5e 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
22e5f 20 74 68 65 20 4f 4e 20 61 6e 64 20 55 53 49 4e   the ON and USIN
22e60 47 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a  G clauses..**.**
22e61 20 52 65 74 75 72 6e 20 61 20 6e 65 77 20 53 72   Return a new Sr
22e62 63 4c 69 73 74 20 77 68 69 63 68 20 65 6e 63 6f  cList which enco
22e63 64 65 73 20 69 73 20 74 68 65 20 46 52 4f 4d 20  des is the FROM 
22e64 77 69 74 68 20 74 68 65 20 6e 65 77 0a 2a 2a 20  with the new.** 
22e65 74 65 72 6d 20 61 64 64 65 64 2e 0a 2a 2f 0a 53  term added..*/.S
22e66 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72  QLITE_PRIVATE Sr
22e67 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72  cList *sqlite3Sr
22e68 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54  cListAppendFromT
22e69 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50  erm(.  Parse *pP
22e6a 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
22e6b 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
22e6c 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
22e6d 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
22e6e 2a 20 54 68 65 20 6c 65 66 74 20 70 61 72 74 20  * The left part 
22e6f 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
22e70 73 65 20 61 6c 72 65 61 64 79 20 73 65 65 6e 20  se already seen 
22e71 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62  */.  Token *pTab
22e72 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  le,          /* 
22e73 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  Name of the tabl
22e74 65 20 74 6f 20 61 64 64 20 74 6f 20 74 68 65 20  e to add to the 
22e75 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
22e76 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73   Token *pDatabas
22e77 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  e,       /* Name
22e78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
22e79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62   containing pTab
22e7a 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  le */.  Token *p
22e7b 41 6c 69 61 73 2c 20 20 20 20 20 20 20 20 20 20  Alias,          
22e7c 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e  /* The right-han
22e7d 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 41 53  d side of the AS
22e7e 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 2a   subexpression *
22e7f 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62  /.  Select *pSub
22e80 71 75 65 72 79 2c 20 20 20 20 20 20 2f 2a 20 41  query,      /* A
22e81 20 73 75 62 71 75 65 72 79 20 75 73 65 64 20 69   subquery used i
22e82 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62  n place of a tab
22e83 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 45 78 70  le name */.  Exp
22e84 72 20 2a 70 4f 6e 2c 20 20 20 20 20 20 20 20 20  r *pOn,         
22e85 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 4e 20 63       /* The ON c
22e86 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20  lause of a join 
22e87 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 55 73  */.  IdList *pUs
22e88 69 6e 67 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ing          /* 
22e89 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  The USING clause
22e8a 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 29 7b   of a join */.){
22e8b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
22e8c 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
22e8d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
22e8e 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 20 3d  Parse->db;.  p =
22e8f 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
22e90 70 70 65 6e 64 28 64 62 2c 20 70 2c 20 70 54 61  ppend(db, p, pTa
22e91 62 6c 65 2c 20 70 44 61 74 61 62 61 73 65 29 3b  ble, pDatabase);
22e92 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70  .  if( p==0 || p
22e93 2d 3e 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20  ->nSrc==0 ){.   
22e94 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
22e95 74 65 28 64 62 2c 20 70 4f 6e 29 3b 0a 20 20 20  te(db, pOn);.   
22e96 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
22e97 6c 65 74 65 28 64 62 2c 20 70 55 73 69 6e 67 29  lete(db, pUsing)
22e98 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
22e99 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  ectDelete(db, pS
22e9a 75 62 71 75 65 72 79 29 3b 0a 20 20 20 20 72 65  ubquery);.    re
22e9b 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70 49  turn p;.  }.  pI
22e9c 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e  tem = &p->a[p->n
22e9d 53 72 63 2d 31 5d 3b 0a 20 20 69 66 28 20 70 41  Src-1];.  if( pA
22e9e 6c 69 61 73 20 26 26 20 70 41 6c 69 61 73 2d 3e  lias && pAlias->
22e9f 6e 20 29 7b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  n ){.    pItem->
22ea0 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33  zAlias = sqlite3
22ea1 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
22ea2 2c 20 70 41 6c 69 61 73 29 3b 0a 20 20 7d 0a 20  , pAlias);.  }. 
22ea3 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20   pItem->pSelect 
22ea4 3d 20 70 53 75 62 71 75 65 72 79 3b 0a 20 20 70  = pSubquery;.  p
22ea5 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 70 4f 6e 3b  Item->pOn = pOn;
22ea6 0a 20 20 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67  .  pItem->pUsing
22ea7 20 3d 20 70 55 73 69 6e 67 3b 0a 20 20 72 65 74   = pUsing;.  ret
22ea8 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
22ea9 41 64 64 20 61 6e 20 49 4e 44 45 58 45 44 20 42  Add an INDEXED B
22eaa 59 20 6f 72 20 4e 4f 54 20 49 4e 44 45 58 45 44  Y or NOT INDEXED
22eab 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20 6d   clause to the m
22eac 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64  ost recently add
22ead 65 64 20 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 6f  ed .** element o
22eae 66 20 74 68 65 20 73 6f 75 72 63 65 2d 6c 69 73  f the source-lis
22eaf 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  t passed as the 
22eb0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e  second argument.
22eb1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22eb2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
22eb3 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28  rcListIndexedBy(
22eb4 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
22eb5 72 63 4c 69 73 74 20 2a 70 2c 20 54 6f 6b 65 6e  rcList *p, Token
22eb6 20 2a 70 49 6e 64 65 78 65 64 42 79 29 7b 0a 20   *pIndexedBy){. 
22eb7 20 69 66 28 20 70 49 6e 64 65 78 65 64 42 79 20   if( pIndexedBy 
22eb8 26 26 20 70 20 26 26 20 70 2d 3e 6e 53 72 63 3e  && p && p->nSrc>
22eb9 30 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  0 ){.    struct 
22eba 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
22ebb 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e  tem = &p->a[p->n
22ebc 53 72 63 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65  Src-1];.    asse
22ebd 72 74 28 20 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e  rt( pItem->notIn
22ebe 64 65 78 65 64 3d 3d 30 20 26 26 20 70 49 74 65  dexed==0 && pIte
22ebf 6d 2d 3e 7a 49 6e 64 65 78 3d 3d 30 20 29 3b 0a  m->zIndex==0 );.
22ec0 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 65 64      if( pIndexed
22ec1 42 79 2d 3e 6e 3d 3d 31 20 26 26 20 21 70 49 6e  By->n==1 && !pIn
22ec2 64 65 78 65 64 42 79 2d 3e 7a 20 29 7b 0a 20 20  dexedBy->z ){.  
22ec3 20 20 20 20 2f 2a 20 41 20 22 4e 4f 54 20 49 4e      /* A "NOT IN
22ec4 44 45 58 45 44 22 20 63 6c 61 75 73 65 20 77 61  DEXED" clause wa
22ec5 73 20 73 75 70 70 6c 69 65 64 2e 20 53 65 65 20  s supplied. See 
22ec6 70 61 72 73 65 2e 79 20 0a 20 20 20 20 20 20 2a  parse.y .      *
22ec7 2a 20 63 6f 6e 73 74 72 75 63 74 20 22 69 6e 64  * construct "ind
22ec8 65 78 65 64 5f 6f 70 74 22 20 66 6f 72 20 64 65  exed_opt" for de
22ec9 74 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 20 20  tails. */.      
22eca 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65  pItem->notIndexe
22ecb 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  d = 1;.    }else
22ecc 7b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a  {.      pItem->z
22ecd 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 4e  Index = sqlite3N
22ece 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61  ameFromToken(pPa
22ecf 72 73 65 2d 3e 64 62 2c 20 70 49 6e 64 65 78 65  rse->db, pIndexe
22ed0 64 42 79 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  dBy);.    }.  }.
22ed1 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 62 75  }../*.** When bu
22ed2 69 6c 64 69 6e 67 20 75 70 20 61 20 46 52 4f 4d  ilding up a FROM
22ed3 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 70   clause in the p
22ed4 61 72 73 65 72 2c 20 74 68 65 20 6a 6f 69 6e 20  arser, the join 
22ed5 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73 20 69  operator.** is i
22ed6 6e 69 74 69 61 6c 6c 79 20 61 74 74 61 63 68 65  nitially attache
22ed7 64 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 70  d to the left op
22ed8 65 72 61 6e 64 2e 20 20 42 75 74 20 74 68 65 20  erand.  But the 
22ed9 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 0a 2a  code generator.*
22eda 2a 20 65 78 70 65 63 74 73 20 74 68 65 20 6a 6f  * expects the jo
22edb 69 6e 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62  in operator to b
22edc 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 6f  e on the right o
22edd 70 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f  perand.  This ro
22ede 75 74 69 6e 65 0a 2a 2a 20 53 68 69 66 74 73 20  utine.** Shifts 
22edf 61 6c 6c 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f  all join operato
22ee0 72 73 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20  rs from left to 
22ee1 72 69 67 68 74 20 66 6f 72 20 61 6e 20 65 6e 74  right for an ent
22ee2 69 72 65 20 46 52 4f 4d 0a 2a 2a 20 63 6c 61 75  ire FROM.** clau
22ee3 73 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c  se..**.** Exampl
22ee4 65 3a 20 53 75 70 70 6f 73 65 20 74 68 65 20 6a  e: Suppose the j
22ee5 6f 69 6e 20 69 73 20 6c 69 6b 65 20 74 68 69 73  oin is like this
22ee6 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
22ee7 20 20 41 20 6e 61 74 75 72 61 6c 20 63 72 6f 73    A natural cros
22ee8 73 20 6a 6f 69 6e 20 42 0a 2a 2a 0a 2a 2a 20 54  s join B.**.** T
22ee9 68 65 20 6f 70 65 72 61 74 6f 72 20 69 73 20 22  he operator is "
22eea 6e 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f  natural cross jo
22eeb 69 6e 22 2e 20 20 54 68 65 20 41 20 61 6e 64 20  in".  The A and 
22eec 42 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 73  B operands are s
22eed 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 70 2d 3e 61  tored.** in p->a
22eee 5b 30 5d 20 61 6e 64 20 70 2d 3e 61 5b 31 5d 2c  [0] and p->a[1],
22eef 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20   respectively.  
22ef0 54 68 65 20 70 61 72 73 65 72 20 69 6e 69 74 69  The parser initi
22ef1 61 6c 6c 79 20 73 74 6f 72 65 73 20 74 68 65 0a  ally stores the.
22ef2 2a 2a 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68  ** operator with
22ef3 20 41 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e   A.  This routin
22ef4 65 20 73 68 69 66 74 73 20 74 68 61 74 20 6f 70  e shifts that op
22ef5 65 72 61 74 6f 72 20 6f 76 65 72 20 74 6f 20 42  erator over to B
22ef6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22ef7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22ef8 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e  SrcListShiftJoin
22ef9 54 79 70 65 28 53 72 63 4c 69 73 74 20 2a 70 29  Type(SrcList *p)
22efa 7b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e  {.  if( p && p->
22efb 61 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  a ){.    int i;.
22efc 20 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 53 72      for(i=p->nSr
22efd 63 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a  c-1; i>0; i--){.
22efe 20 20 20 20 20 20 70 2d 3e 61 5b 69 5d 2e 6a 6f        p->a[i].jo
22eff 69 6e 74 79 70 65 20 3d 20 70 2d 3e 61 5b 69 2d  intype = p->a[i-
22f00 31 5d 2e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20  1].jointype;.   
22f01 20 7d 0a 20 20 20 20 70 2d 3e 61 5b 30 5d 2e 6a   }.    p->a[0].j
22f02 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20 7d  ointype = 0;.  }
22f03 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20  .}../*.** Begin 
22f04 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f  a transaction.*/
22f05 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22f06 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
22f07 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72  nTransaction(Par
22f08 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
22f09 74 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  type){.  sqlite3
22f0a 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b   *db;.  Vdbe *v;
22f0b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69 66 28  .  int i;..  if(
22f0c 20 70 50 61 72 73 65 3d 3d 30 20 7c 7c 20 28 64   pParse==0 || (d
22f0d 62 3d 70 50 61 72 73 65 2d 3e 64 62 29 3d 3d 30  b=pParse->db)==0
22f0e 20 7c 7c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70   || db->aDb[0].p
22f0f 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  Bt==0 ) return;.
22f10 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
22f11 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
22f12 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b  Failed ) return;
22f13 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
22f14 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
22f15 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49  SQLITE_TRANSACTI
22f16 4f 4e 2c 20 22 42 45 47 49 4e 22 2c 20 30 2c 20  ON, "BEGIN", 0, 
22f17 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  0) ) return;..  
22f18 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
22f19 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
22f1a 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( !v ) return;. 
22f1b 20 69 66 28 20 74 79 70 65 21 3d 54 4b 5f 44 45   if( type!=TK_DE
22f1c 46 45 52 52 45 44 20 29 7b 0a 20 20 20 20 66 6f  FERRED ){.    fo
22f1d 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
22f1e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
22f1f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22f20 76 2c 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  v, OP_Transactio
22f21 6e 2c 20 69 2c 20 28 74 79 70 65 3d 3d 54 4b 5f  n, i, (type==TK_
22f22 45 58 43 4c 55 53 49 56 45 29 2b 31 29 3b 0a 20  EXCLUSIVE)+1);. 
22f23 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22f24 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 29 3b  UsesBtree(v, i);
22f25 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
22f26 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22f27 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c  , OP_AutoCommit,
22f28 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   0, 0);.}../*.**
22f29 20 43 6f 6d 6d 69 74 20 61 20 74 72 61 6e 73 61   Commit a transa
22f2a 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ction.*/.SQLITE_
22f2b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22f2c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61  ite3CommitTransa
22f2d 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61  ction(Parse *pPa
22f2e 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse){.  sqlite3 
22f2f 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  *db;.  Vdbe *v;.
22f30 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30  .  if( pParse==0
22f31 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 65 2d 3e   || (db=pParse->
22f32 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44  db)==0 || db->aD
22f33 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65  b[0].pBt==0 ) re
22f34 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72  turn;.  if( pPar
22f35 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
22f36 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
22f37 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c  eturn;.  if( sql
22f38 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
22f39 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41  arse, SQLITE_TRA
22f3a 4e 53 41 43 54 49 4f 4e 2c 20 22 43 4f 4d 4d 49  NSACTION, "COMMI
22f3b 54 22 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75  T", 0, 0) ) retu
22f3c 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74  rn;..  v = sqlit
22f3d 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
22f3e 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
22f3f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22f40 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f  Op2(v, OP_AutoCo
22f41 6d 6d 69 74 2c 20 31 2c 20 30 29 3b 0a 20 20 7d  mmit, 1, 0);.  }
22f42 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61  .}../*.** Rollba
22f43 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ck a transaction
22f44 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22f45 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
22f46 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69  ollbackTransacti
22f47 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
22f48 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
22f49 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  ;.  Vdbe *v;..  
22f4a 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 7c 7c  if( pParse==0 ||
22f4b 20 28 64 62 3d 70 50 61 72 73 65 2d 3e 64 62 29   (db=pParse->db)
22f4c 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44 62 5b 30  ==0 || db->aDb[0
22f4d 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72  ].pBt==0 ) retur
22f4e 6e 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  n;.  if( pParse-
22f4f 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
22f50 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
22f51 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rn;.  if( sqlite
22f52 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
22f53 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41  e, SQLITE_TRANSA
22f54 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c 42 41 43 4b  CTION, "ROLLBACK
22f55 22 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75 72  ", 0, 0) ) retur
22f56 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  n;..  v = sqlite
22f57 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22f58 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
22f59 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22f5a 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d  p2(v, OP_AutoCom
22f5b 6d 69 74 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 0a  mit, 1, 1);.  }.
22f5c 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
22f5d 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
22f5e 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77   by the parser w
22f5f 68 65 6e 20 69 74 20 70 61 72 73 65 73 20 61 20  hen it parses a 
22f60 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 72 65 61 74  command to creat
22f61 65 2c 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 72  e,.** release or
22f62 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20 53 51 4c   rollback an SQL
22f63 20 73 61 76 65 70 6f 69 6e 74 2e 20 0a 2a 2f 0a   savepoint. .*/.
22f64 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
22f65 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65 70  oid sqlite3Savep
22f66 6f 69 6e 74 28 50 61 72 73 65 20 2a 70 50 61 72  oint(Parse *pPar
22f67 73 65 2c 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65  se, int op, Toke
22f68 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61  n *pName){.  cha
22f69 72 20 2a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  r *zName = sqlit
22f6a 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
22f6b 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d  pParse->db, pNam
22f6c 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20  e);.  if( zName 
22f6d 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  ){.    Vdbe *v =
22f6e 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
22f6f 70 50 61 72 73 65 29 3b 0a 23 69 66 6e 64 65 66  pParse);.#ifndef
22f70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
22f71 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 73  HORIZATION.    s
22f72 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
22f73 20 2a 61 7a 5b 5d 20 3d 20 7b 20 22 42 45 47 49   *az[] = { "BEGI
22f74 4e 22 2c 20 22 52 45 4c 45 41 53 45 22 2c 20 22  N", "RELEASE", "
22f75 52 4f 4c 4c 42 41 43 4b 22 20 7d 3b 0a 20 20 20  ROLLBACK" };.   
22f76 20 61 73 73 65 72 74 28 20 21 53 41 56 45 50 4f   assert( !SAVEPO
22f77 49 4e 54 5f 42 45 47 49 4e 20 26 26 20 53 41 56  INT_BEGIN && SAV
22f78 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 3d 3d  EPOINT_RELEASE==
22f79 31 20 26 26 20 53 41 56 45 50 4f 49 4e 54 5f 52  1 && SAVEPOINT_R
22f7a 4f 4c 4c 42 41 43 4b 3d 3d 32 20 29 3b 0a 23 65  OLLBACK==2 );.#e
22f7b 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 76 20  ndif.    if( !v 
22f7c 7c 7c 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  || sqlite3AuthCh
22f7d 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
22f7e 54 45 5f 53 41 56 45 50 4f 49 4e 54 2c 20 61 7a  TE_SAVEPOINT, az
22f7f 5b 6f 70 5d 2c 20 7a 4e 61 6d 65 2c 20 30 29 20  [op], zName, 0) 
22f80 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
22f81 44 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64  DbFree(pParse->d
22f82 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b, zName);.     
22f83 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
22f84 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22f85 64 4f 70 34 28 76 2c 20 4f 50 5f 53 61 76 65 70  dOp4(v, OP_Savep
22f86 6f 69 6e 74 2c 20 6f 70 2c 20 30 2c 20 30 2c 20  oint, op, 0, 0, 
22f87 7a 4e 61 6d 65 2c 20 50 34 5f 44 59 4e 41 4d 49  zName, P4_DYNAMI
22f88 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  C);.  }.}../*.**
22f89 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 54   Make sure the T
22f8a 45 4d 50 20 64 61 74 61 62 61 73 65 20 69 73 20  EMP database is 
22f8b 6f 70 65 6e 20 61 6e 64 20 61 76 61 69 6c 61 62  open and availab
22f8c 6c 65 20 66 6f 72 20 75 73 65 2e 20 20 52 65 74  le for use.  Ret
22f8d 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  urn.** the numbe
22f8e 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 4c 65  r of errors.  Le
22f8f 61 76 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65  ave any error me
22f90 73 73 61 67 65 73 20 69 6e 20 74 68 65 20 70 50  ssages in the pP
22f91 61 72 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a  arse structure..
22f92 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22f93 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65  E int sqlite3Ope
22f94 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 50 61  nTempDatabase(Pa
22f95 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
22f96 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
22f97 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
22f98 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 3d 3d  db->aDb[1].pBt==
22f99 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e 65 78  0 && !pParse->ex
22f9a 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74  plain ){.    int
22f9b 20 72 63 3b 0a 20 20 20 20 73 74 61 74 69 63 20   rc;.    static 
22f9c 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20  const int flags 
22f9d 3d 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c  = .          SQL
22f9e 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
22f9f 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53  TE |.          S
22fa0 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
22fa1 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51  E |.          SQ
22fa2 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
22fa3 49 56 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20  IVE |.          
22fa4 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
22fa5 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20  TEONCLOSE |.    
22fa6 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
22fa7 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a 20 20 20 20  N_TEMP_DB;..    
22fa8 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
22fa9 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20  eFactory(db, 0, 
22faa 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  0, SQLITE_DEFAUL
22fab 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 66 6c  T_CACHE_SIZE, fl
22fac 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ags,.           
22fad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22fae 20 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 31        &db->aDb[1
22faf 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20  ].pBt);.    if( 
22fb0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
22fb1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
22fb2 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
22fb3 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61  unable to open a
22fb4 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62   temporary datab
22fb5 61 73 65 20 22 0a 20 20 20 20 20 20 20 20 22 66  ase ".        "f
22fb6 69 6c 65 20 66 6f 72 20 73 74 6f 72 69 6e 67 20  ile for storing 
22fb7 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
22fb8 22 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  ");.      pParse
22fb9 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20  ->rc = rc;.     
22fba 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
22fbb 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 64 62  .    assert( (db
22fbc 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
22fbd 5f 49 6e 54 72 61 6e 73 29 3d 3d 30 20 7c 7c 20  _InTrans)==0 || 
22fbe 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29  db->autoCommit )
22fbf 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  ;.    assert( db
22fc0 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61  ->aDb[1].pSchema
22fc1 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   );.    sqlite3P
22fc2 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  agerJournalMode(
22fc3 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
22fc4 72 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74  r(db->aDb[1].pBt
22fc5 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
22fc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
22fc7 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f  b->dfltJournalMo
22fc8 64 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  de);.  }.  retur
22fc9 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  n 0;.}../*.** Ge
22fca 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 65  nerate VDBE code
22fcb 20 74 68 61 74 20 77 69 6c 6c 20 76 65 72 69 66   that will verif
22fcc 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  y the schema coo
22fcd 6b 69 65 20 61 6e 64 20 73 74 61 72 74 0a 2a 2a  kie and start.**
22fce 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74   a read-transact
22fcf 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d 65  ion for all name
22fd0 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  d database files
22fd1 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6d  ..**.** It is im
22fd2 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 6c 6c  portant that all
22fd3 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20   schema cookies 
22fd4 62 65 20 76 65 72 69 66 69 65 64 20 61 6e 64 20  be verified and 
22fd5 61 6c 6c 0a 2a 2a 20 72 65 61 64 20 74 72 61 6e  all.** read tran
22fd6 73 61 63 74 69 6f 6e 73 20 62 65 20 73 74 61 72  sactions be star
22fd7 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 74 68  ted before anyth
22fd8 69 6e 67 20 65 6c 73 65 20 68 61 70 70 65 6e 73  ing else happens
22fd9 20 69 6e 0a 2a 2a 20 74 68 65 20 56 44 42 45 20   in.** the VDBE 
22fda 70 72 6f 67 72 61 6d 2e 20 20 42 75 74 20 74 68  program.  But th
22fdb 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62  is routine can b
22fdc 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 6d  e called after m
22fdd 75 63 68 20 6f 74 68 65 72 0a 2a 2a 20 63 6f 64  uch other.** cod
22fde 65 20 68 61 73 20 62 65 65 6e 20 67 65 6e 65 72  e has been gener
22fdf 61 74 65 64 2e 20 20 53 6f 20 68 65 72 65 20 69  ated.  So here i
22fe0 73 20 77 68 61 74 20 77 65 20 64 6f 3a 0a 2a 2a  s what we do:.**
22fe1 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69  .** The first ti
22fe2 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  me this routine 
22fe3 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 63 6f  is called, we co
22fe4 64 65 20 61 6e 20 4f 50 5f 47 6f 74 6f 20 74 68  de an OP_Goto th
22fe5 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70 20  at.** will jump 
22fe6 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  to a subroutine 
22fe7 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
22fe8 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 65 6e  e program.  Then
22fe9 20 77 65 0a 2a 2a 20 72 65 63 6f 72 64 20 65 76   we.** record ev
22fea 65 72 79 20 64 61 74 61 62 61 73 65 20 74 68 61  ery database tha
22feb 74 20 6e 65 65 64 73 20 69 74 73 20 73 63 68 65  t needs its sche
22fec 6d 61 20 76 65 72 69 66 69 65 64 20 69 6e 20 74  ma verified in t
22fed 68 65 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 63 6f  he.** pParse->co
22fee 6f 6b 69 65 4d 61 73 6b 20 66 69 65 6c 64 2e 20  okieMask field. 
22fef 20 4c 61 74 65 72 2c 20 61 66 74 65 72 20 61 6c   Later, after al
22ff0 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73  l other code has
22ff1 20 62 65 65 6e 0a 2a 2a 20 67 65 6e 65 72 61 74   been.** generat
22ff2 65 64 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69  ed, the subrouti
22ff3 6e 65 20 74 68 61 74 20 64 6f 65 73 20 74 68 65  ne that does the
22ff4 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61   cookie verifica
22ff5 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 73 74 61  tions and.** sta
22ff6 72 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 74  rts the transact
22ff7 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20 63 6f 64  ions will be cod
22ff8 65 64 20 61 6e 64 20 74 68 65 20 4f 50 5f 47 6f  ed and the OP_Go
22ff9 74 6f 20 50 32 20 76 61 6c 75 65 0a 2a 2a 20 77  to P2 value.** w
22ffa 69 6c 6c 20 62 65 20 6d 61 64 65 20 74 6f 20 70  ill be made to p
22ffb 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73 75 62  oint to that sub
22ffc 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 67 65  routine.  The ge
22ffd 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  neration of the.
22ffe 2a 2a 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69  ** cookie verifi
22fff 63 61 74 69 6f 6e 20 73 75 62 72 6f 75 74 69 6e  cation subroutin
23000 65 20 63 6f 64 65 20 68 61 70 70 65 6e 73 20 69  e code happens i
23001 6e 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  n sqlite3FinishC
23002 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  oding()..**.** I
23003 66 20 69 44 62 3c 30 20 74 68 65 6e 20 63 6f 64  f iDb<0 then cod
23004 65 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f 6e  e the OP_Goto on
23005 6c 79 20 2d 20 64 6f 6e 27 74 20 73 65 74 20 66  ly - don't set f
23006 6c 61 67 20 74 6f 20 76 65 72 69 66 79 20 74 68  lag to verify th
23007 65 0a 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20 61  e.** schema on a
23008 6e 79 20 64 61 74 61 62 61 73 65 73 2e 20 20 54  ny databases.  T
23009 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
2300a 74 6f 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20  to position the 
2300b 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c 79  OP_Goto.** early
2300c 20 69 6e 20 74 68 65 20 63 6f 64 65 2c 20 62 65   in the code, be
2300d 66 6f 72 65 20 77 65 20 6b 6e 6f 77 20 69 66 20  fore we know if 
2300e 61 6e 79 20 64 61 74 61 62 61 73 65 20 74 61 62  any database tab
2300f 6c 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64  les will be used
23010 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23011 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23012 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
23013 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
23014 69 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69  int iDb){.  sqli
23015 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20  te3 *db;.  Vdbe 
23016 2a 76 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a  *v;.  int mask;.
23017 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
23018 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
23019 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
2301a 72 6e 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c  rn;  /* This onl
2301b 79 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 65  y happens if the
2301c 72 65 20 77 61 73 20 61 20 70 72 69 6f 72 20 65  re was a prior e
2301d 72 72 6f 72 20 2a 2f 0a 20 20 64 62 20 3d 20 70  rror */.  db = p
2301e 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
2301f 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47   pParse->cookieG
23020 6f 74 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50  oto==0 ){.    pP
23021 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f  arse->cookieGoto
23022 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
23023 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
23024 20 30 2c 20 30 29 2b 31 3b 0a 20 20 7d 0a 20 20   0, 0)+1;.  }.  
23025 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20  if( iDb>=0 ){.  
23026 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
23027 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73  ->nDb );.    ass
23028 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62  ert( db->aDb[iDb
23029 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69 44 62 3d  ].pBt!=0 || iDb=
2302a 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
2302b 28 20 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41 58  ( iDb<SQLITE_MAX
2302c 5f 41 54 54 41 43 48 45 44 2b 32 20 29 3b 0a 20  _ATTACHED+2 );. 
2302d 20 20 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 44 62     mask = 1<<iDb
2302e 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 72 73  ;.    if( (pPars
2302f 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 26 20  e->cookieMask & 
23030 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20  mask)==0 ){.    
23031 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65    pParse->cookie
23032 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20  Mask |= mask;.  
23033 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b      pParse->cook
23034 69 65 56 61 6c 75 65 5b 69 44 62 5d 20 3d 20 64  ieValue[iDb] = d
23035 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
23036 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b  ema->schema_cook
23037 69 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 4f  ie;.      if( !O
23038 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44  MIT_TEMPDB && iD
23039 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  b==1 ){.        
2303a 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44  sqlite3OpenTempD
2303b 61 74 61 62 61 73 65 28 70 50 61 72 73 65 29 3b  atabase(pParse);
2303c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2303d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65   }.}../*.** Gene
2303e 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74  rate VDBE code t
2303f 68 61 74 20 70 72 65 70 61 72 65 73 20 66 6f 72  hat prepares for
23040 20 64 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61 74   doing an operat
23041 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67 68  ion that.** migh
23042 74 20 63 68 61 6e 67 65 20 74 68 65 20 64 61 74  t change the dat
23043 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  abase..**.** Thi
23044 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73  s routine starts
23045 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69   a new transacti
23046 6f 6e 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74  on if we are not
23047 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 0a   already within.
23048 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ** a transaction
23049 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 6c 72  .  If we are alr
2304a 65 61 64 79 20 77 69 74 68 69 6e 20 61 20 74 72  eady within a tr
2304b 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
2304c 61 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20  a checkpoint.** 
2304d 69 73 20 73 65 74 20 69 66 20 74 68 65 20 73 65  is set if the se
2304e 74 53 74 61 74 65 6d 65 6e 74 20 70 61 72 61 6d  tStatement param
2304f 65 74 65 72 20 69 73 20 74 72 75 65 2e 20 20 41  eter is true.  A
23050 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75   checkpoint shou
23051 6c 64 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f 72  ld.** be set for
23052 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74   operations that
23053 20 6d 69 67 68 74 20 66 61 69 6c 20 28 64 75 65   might fail (due
23054 20 74 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74   to a constraint
23055 29 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65  ) part of.** the
23056 20 77 61 79 20 74 68 72 6f 75 67 68 20 61 6e 64   way through and
23057 20 77 68 69 63 68 20 77 69 6c 6c 20 6e 65 65 64   which will need
23058 20 74 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77 72   to undo some wr
23059 69 74 65 73 20 77 69 74 68 6f 75 74 20 68 61 76  ites without hav
2305a 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 61  ing to.** rollba
2305b 63 6b 20 74 68 65 20 77 68 6f 6c 65 20 74 72 61  ck the whole tra
2305c 6e 73 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20 6f  nsaction.  For o
2305d 70 65 72 61 74 69 6f 6e 73 20 77 68 65 72 65 20  perations where 
2305e 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  all constraints.
2305f 2a 2a 20 63 61 6e 20 62 65 20 63 68 65 63 6b 65  ** can be checke
23060 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61  d before any cha
23061 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f  nges are made to
23062 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 69   the database, i
23063 74 20 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e 65  t is never.** ne
23064 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20  cessary to undo 
23065 61 20 77 72 69 74 65 20 61 6e 64 20 74 68 65 20  a write and the 
23066 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c  checkpoint shoul
23067 64 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2f  d not be set..*/
23068 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23069 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
2306a 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
2306b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
2306c 6e 74 20 73 65 74 53 74 61 74 65 6d 65 6e 74 2c  nt setStatement,
2306d 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62   int iDb){.  Vdb
2306e 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
2306f 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
23070 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
23071 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f 64  rn;.  sqlite3Cod
23072 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
23073 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 70 50  arse, iDb);.  pP
23074 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 73 6b 20  arse->writeMask 
23075 7c 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 69 66 28  |= 1<<iDb;.  if(
23076 20 73 65 74 53 74 61 74 65 6d 65 6e 74 20 26 26   setStatement &&
23077 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
23078 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
23079 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2307a 50 5f 53 74 61 74 65 6d 65 6e 74 2c 20 69 44 62  P_Statement, iDb
2307b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2307c 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
2307d 70 49 6e 64 65 78 20 75 73 65 73 20 74 68 65 20  pIndex uses the 
2307e 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2307f 63 65 20 70 43 6f 6c 6c 2e 20 20 52 65 74 75 72  ce pColl.  Retur
23080 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 69 74 20  n.** true if it 
23081 64 6f 65 73 20 61 6e 64 20 66 61 6c 73 65 20 69  does and false i
23082 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a  f it does not..*
23083 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
23084 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74  _OMIT_REINDEX.st
23085 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 61 74 69  atic int collati
23086 6f 6e 4d 61 74 63 68 28 63 6f 6e 73 74 20 63 68  onMatch(const ch
23087 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49 6e 64 65 78  ar *zColl, Index
23088 20 2a 70 49 6e 64 65 78 29 7b 0a 20 20 69 6e 74   *pIndex){.  int
23089 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
2308a 3c 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e  <pIndex->nColumn
2308b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73  ; i++){.    cons
2308c 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 49 6e 64  t char *z = pInd
2308d 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20  ex->azColl[i];. 
2308e 20 20 20 69 66 28 20 7a 3d 3d 7a 43 6f 6c 6c 20     if( z==zColl 
2308f 7c 7c 20 28 7a 20 26 26 20 7a 43 6f 6c 6c 20 26  || (z && zColl &
23090 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49  & 0==sqlite3StrI
23091 43 6d 70 28 7a 2c 20 7a 43 6f 6c 6c 29 29 20 29  Cmp(z, zColl)) )
23092 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
23093 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
23094 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  turn 0;.}.#endif
23095 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74  ../*.** Recomput
23096 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66  e all indices of
23097 20 70 54 61 62 20 74 68 61 74 20 75 73 65 20 74   pTab that use t
23098 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
23099 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 0a 2a 2a 20  uence pColl..** 
2309a 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e  If pColl==0 then
2309b 20 72 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69   recompute all i
2309c 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62 2e 0a  ndices of pTab..
2309d 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2309e 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73  E_OMIT_REINDEX.s
2309f 74 61 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64  tatic void reind
230a0 65 78 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70  exTable(Parse *p
230a1 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
230a2 61 62 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ab, char const *
230a3 7a 43 6f 6c 6c 29 7b 0a 20 20 49 6e 64 65 78 20  zColl){.  Index 
230a4 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  *pIndex;        
230a5 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65        /* An inde
230a6 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
230a7 68 20 70 54 61 62 20 2a 2f 0a 0a 20 20 66 6f 72  h pTab */..  for
230a8 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49  (pIndex=pTab->pI
230a9 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49  ndex; pIndex; pI
230aa 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65  ndex=pIndex->pNe
230ab 78 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 43 6f  xt){.    if( zCo
230ac 6c 6c 3d 3d 30 20 7c 7c 20 63 6f 6c 6c 61 74 69  ll==0 || collati
230ad 6f 6e 4d 61 74 63 68 28 7a 43 6f 6c 6c 2c 20 70  onMatch(zColl, p
230ae 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 20 20  Index) ){.      
230af 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65  int iDb = sqlite
230b0 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
230b1 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
230b2 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20  >pSchema);.     
230b3 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
230b4 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
230b5 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20  se, 0, iDb);.   
230b6 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c     sqlite3Refill
230b7 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49  Index(pParse, pI
230b8 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 7d  ndex, -1);.    }
230b9 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
230ba 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61  *.** Recompute a
230bb 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 61 6c  ll indices of al
230bc 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 6c 6c 20  l tables in all 
230bd 64 61 74 61 62 61 73 65 73 20 77 68 65 72 65 20  databases where 
230be 74 68 65 0a 2a 2a 20 69 6e 64 69 63 65 73 20 75  the.** indices u
230bf 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  se the collating
230c0 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e   sequence pColl.
230c1 20 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68    If pColl==0 th
230c2 65 6e 20 72 65 63 6f 6d 70 75 74 65 0a 2a 2a 20  en recompute.** 
230c3 61 6c 6c 20 69 6e 64 69 63 65 73 20 65 76 65 72  all indices ever
230c4 79 77 68 65 72 65 2e 0a 2a 2f 0a 23 69 66 6e 64  ywhere..*/.#ifnd
230c5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52  ef SQLITE_OMIT_R
230c6 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f  EINDEX.static vo
230c7 69 64 20 72 65 69 6e 64 65 78 44 61 74 61 62 61  id reindexDataba
230c8 73 65 73 28 50 61 72 73 65 20 2a 70 50 61 72 73  ses(Parse *pPars
230c9 65 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  e, char const *z
230ca 43 6f 6c 6c 29 7b 0a 20 20 44 62 20 2a 70 44 62  Coll){.  Db *pDb
230cb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
230cc 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
230cd 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
230ce 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
230cf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
230d0 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  e database index
230d1 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c   number */.  sql
230d2 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
230d3 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68 65 20  e->db;   /* The 
230d4 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
230d5 69 6f 6e 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ion */.  HashEle
230d6 6d 20 2a 6b 3b 20 20 20 20 20 20 20 20 20 20 20  m *k;           
230d7 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
230d8 69 6e 67 20 6f 76 65 72 20 74 61 62 6c 65 73 20  ing over tables 
230d9 69 6e 20 70 44 62 20 2a 2f 0a 20 20 54 61 62 6c  in pDb */.  Tabl
230da 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
230db 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74 61 62          /* A tab
230dc 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  le in the databa
230dd 73 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 44 62  se */..  for(iDb
230de 3d 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44 62 3b  =0, pDb=db->aDb;
230df 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44   iDb<db->nDb; iD
230e0 62 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20 20 20  b++, pDb++){.   
230e1 20 61 73 73 65 72 74 28 20 70 44 62 21 3d 30 20   assert( pDb!=0 
230e2 29 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73 71 6c  );.    for(k=sql
230e3 69 74 65 48 61 73 68 46 69 72 73 74 28 26 70 44  iteHashFirst(&pD
230e4 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  b->pSchema->tblH
230e5 61 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71 6c 69  ash);  k; k=sqli
230e6 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a  teHashNext(k)){.
230e7 20 20 20 20 20 20 70 54 61 62 20 3d 20 28 54 61        pTab = (Ta
230e8 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 68 44  ble*)sqliteHashD
230e9 61 74 61 28 6b 29 3b 0a 20 20 20 20 20 20 72 65  ata(k);.      re
230ea 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61 72 73  indexTable(pPars
230eb 65 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c 29 3b  e, pTab, zColl);
230ec 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e  .    }.  }.}.#en
230ed 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  dif../*.** Gener
230ee 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65  ate code for the
230ef 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61 6e 64   REINDEX command
230f0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 52  ..**.**        R
230f1 45 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20  EINDEX          
230f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
230f3 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20    -- 1.**       
230f4 20 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c 6c 61   REINDEX  <colla
230f5 74 69 6f 6e 3e 20 20 20 20 20 20 20 20 20 20 20  tion>           
230f6 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20      -- 2.**     
230f7 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c 64 61     REINDEX  ?<da
230f8 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e  tabase>.?<tablen
230f9 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20 20 20  ame>  -- 3.**   
230fa 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c       REINDEX  ?<
230fb 64 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e 64 65  database>.?<inde
230fc 78 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a 2a 0a  xname>  -- 4.**.
230fd 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73  ** Form 1 causes
230fe 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20   all indices in 
230ff 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
23100 61 62 61 73 65 73 20 74 6f 20 62 65 20 72 65 62  abases to be reb
23101 75 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20  uilt..** Form 2 
23102 72 65 62 75 69 6c 64 73 20 61 6c 6c 20 69 6e 64  rebuilds all ind
23103 69 63 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61  ices in all data
23104 62 61 73 65 73 20 74 68 61 74 20 75 73 65 20 74  bases that use t
23105 68 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f 6c 6c  he named.** coll
23106 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 20  ating function. 
23107 20 46 6f 72 6d 73 20 33 20 61 6e 64 20 34 20 72   Forms 3 and 4 r
23108 65 62 75 69 6c 64 20 74 68 65 20 6e 61 6d 65 64  ebuild the named
23109 20 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a 2a 2a   index or all.**
2310a 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61   indices associa
2310b 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d  ted with the nam
2310c 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66  ed table..*/.#if
2310d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2310e 5f 52 45 49 4e 44 45 58 0a 53 51 4c 49 54 45 5f  _REINDEX.SQLITE_
2310f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23110 69 74 65 33 52 65 69 6e 64 65 78 28 50 61 72 73  ite3Reindex(Pars
23111 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
23112 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65 6e 20   *pName1, Token 
23113 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 43 6f 6c 6c  *pName2){.  Coll
23114 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
23115 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61          /* Colla
23116 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 6f  ting sequence to
23117 20 62 65 20 72 65 69 6e 64 65 78 65 64 2c 20 6f   be reindexed, o
23118 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72  r NULL */.  char
23119 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
2311a 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2311b 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e  of a table or in
2311c 64 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dex */.  const c
2311d 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
2311e 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2311f 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
23120 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
23121 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23122 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20   A table in the 
23123 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 49 6e  database */.  In
23124 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20  dex *pIndex;    
23125 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20            /* An 
23126 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64  index associated
23127 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 20 20   with pTab */.  
23128 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
23129 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2312a 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65  he database inde
2312b 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71  x number */.  sq
2312c 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2312d 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68 65  se->db;   /* The
2312e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
2312f 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  tion */.  Token 
23130 2a 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 20 20 20  *pObjName;      
23131 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
23132 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
23133 64 65 78 20 74 6f 20 62 65 20 72 65 69 6e 64 65  dex to be reinde
23134 78 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61  xed */..  /* Rea
23135 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
23136 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72  chema. If an err
23137 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65  or occurs, leave
23138 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
23139 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20  e.  ** and code 
2313a 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65  in pParse and re
2313b 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  turn NULL. */.  
2313c 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
2313d 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
2313e 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
2313f 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69  return;.  }..  i
23140 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 7c 7c 20  f( pName1==0 || 
23141 70 4e 61 6d 65 31 2d 3e 7a 3d 3d 30 20 29 7b 0a  pName1->z==0 ){.
23142 20 20 20 20 72 65 69 6e 64 65 78 44 61 74 61 62      reindexDatab
23143 61 73 65 73 28 70 50 61 72 73 65 2c 20 30 29 3b  ases(pParse, 0);
23144 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
23145 65 6c 73 65 20 69 66 28 20 70 4e 61 6d 65 32 3d  else if( pName2=
23146 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e 7a 3d  =0 || pName2->z=
23147 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  =0 ){.    char *
23148 7a 43 6f 6c 6c 3b 0a 20 20 20 20 61 73 73 65 72  zColl;.    asser
23149 74 28 20 70 4e 61 6d 65 31 2d 3e 7a 20 29 3b 0a  t( pName1->z );.
2314a 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69      zColl = sqli
2314b 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
2314c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61  (pParse->db, pNa
2314d 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  me1);.    if( !z
2314e 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20  Coll ) return;. 
2314f 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
23150 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
23151 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c  , ENC(db), zColl
23152 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 69 66  , -1, 0);.    if
23153 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20  ( pColl ){.     
23154 20 69 66 28 20 7a 43 6f 6c 6c 20 29 7b 0a 20 20   if( zColl ){.  
23155 20 20 20 20 20 20 72 65 69 6e 64 65 78 44 61 74        reindexDat
23156 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 7a  abases(pParse, z
23157 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 73  Coll);.        s
23158 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
23159 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 7d   zColl);.      }
2315a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
2315b 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2315c 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c  DbFree(db, zColl
2315d 29 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73  );.  }.  iDb = s
2315e 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
2315f 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
23160 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4f 62 6a 4e  , pName2, &pObjN
23161 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62 3c  ame);.  if( iDb<
23162 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 20  0 ) return;.  z 
23163 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
23164 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4f 62 6a 4e  mToken(db, pObjN
23165 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30  ame);.  if( z==0
23166 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 44 62   ) return;.  zDb
23167 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
23168 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 20 3d 20  zName;.  pTab = 
23169 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
2316a 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20  (db, z, zDb);.  
2316b 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
2316c 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61  reindexTable(pPa
2316d 72 73 65 2c 20 70 54 61 62 2c 20 30 29 3b 0a 20  rse, pTab, 0);. 
2316e 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2316f 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 72 65 74  (db, z);.    ret
23170 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65  urn;.  }.  pInde
23171 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  x = sqlite3FindI
23172 6e 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44 62 29  ndex(db, z, zDb)
23173 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
23174 65 28 64 62 2c 20 7a 29 3b 0a 20 20 69 66 28 20  e(db, z);.  if( 
23175 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71  pIndex ){.    sq
23176 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
23177 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
23178 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71   0, iDb);.    sq
23179 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78  lite3RefillIndex
2317a 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c  (pParse, pIndex,
2317b 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e   -1);.    return
2317c 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
2317d 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2317e 22 75 6e 61 62 6c 65 20 74 6f 20 69 64 65 6e 74  "unable to ident
2317f 69 66 79 20 74 68 65 20 6f 62 6a 65 63 74 20 74  ify the object t
23180 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 22 29  o be reindexed")
23181 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
23182 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d  * Return a dynam
23183 69 63 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 4b  icly allocated K
23184 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
23185 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
23186 64 0a 2a 2a 20 77 69 74 68 20 4f 50 5f 4f 70 65  d.** with OP_Ope
23187 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e  nRead or OP_Open
23188 57 72 69 74 65 20 74 6f 20 61 63 63 65 73 73 20  Write to access 
23189 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 70  database index p
2318a 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  Idx..**.** If su
2318b 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e  ccessful, a poin
2318c 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73  ter to the new s
2318d 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75  tructure is retu
2318e 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  rned. In this ca
2318f 73 65 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72  se.** the caller
23190 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
23191 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  for calling sqli
23192 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20  te3DbFree(db, ) 
23193 6f 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  on the returned 
23194 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 49 66 20  .** pointer. If 
23195 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
23196 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 6f  (out of memory o
23197 72 20 6d 69 73 73 69 6e 67 20 63 6f 6c 6c 61 74  r missing collat
23198 69 6f 6e 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ion .** sequence
23199 29 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  ), NULL is retur
2319a 6e 65 64 20 61 6e 64 20 74 68 65 20 73 74 61 74  ned and the stat
2319b 65 20 6f 66 20 70 50 61 72 73 65 20 75 70 64 61  e of pParse upda
2319c 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74 0a 2a  ted to reflect.*
2319d 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a  * the error..*/.
2319e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b  SQLITE_PRIVATE K
2319f 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49  eyInfo *sqlite3I
231a0 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73  ndexKeyinfo(Pars
231a1 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64 65 78  e *pParse, Index
231a2 20 2a 70 49 64 78 29 7b 0a 20 20 69 6e 74 20 69   *pIdx){.  int i
231a3 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70  ;.  int nCol = p
231a4 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20  Idx->nColumn;.  
231a5 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a  int nBytes = siz
231a6 65 6f 66 28 4b 65 79 49 6e 66 6f 29 20 2b 20 28  eof(KeyInfo) + (
231a7 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28 43  nCol-1)*sizeof(C
231a8 6f 6c 6c 53 65 71 2a 29 20 2b 20 6e 43 6f 6c 3b  ollSeq*) + nCol;
231a9 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
231aa 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 4b   pParse->db;.  K
231ab 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 28  eyInfo *pKey = (
231ac 4b 65 79 49 6e 66 6f 20 2a 29 73 71 6c 69 74 65  KeyInfo *)sqlite
231ad 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
231ae 2c 20 6e 42 79 74 65 73 29 3b 0a 0a 20 20 69 66  , nBytes);..  if
231af 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 70 4b  ( pKey ){.    pK
231b0 65 79 2d 3e 64 62 20 3d 20 70 50 61 72 73 65 2d  ey->db = pParse-
231b1 3e 64 62 3b 0a 20 20 20 20 70 4b 65 79 2d 3e 61  >db;.    pKey->a
231b2 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 20  SortOrder = (u8 
231b3 2a 29 26 28 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b  *)&(pKey->aColl[
231b4 6e 43 6f 6c 5d 29 3b 0a 20 20 20 20 61 73 73 65  nCol]);.    asse
231b5 72 74 28 20 26 70 4b 65 79 2d 3e 61 53 6f 72 74  rt( &pKey->aSort
231b6 4f 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d 26 28 28  Order[nCol]==&((
231b7 28 75 38 20 2a 29 70 4b 65 79 29 5b 6e 42 79 74  (u8 *)pKey)[nByt
231b8 65 73 5d 29 20 29 3b 0a 20 20 20 20 66 6f 72 28  es]) );.    for(
231b9 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
231ba 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
231bb 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43  Coll = pIdx->azC
231bc 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73  oll[i];.      as
231bd 73 65 72 74 28 20 7a 43 6f 6c 6c 20 29 3b 0a 20  sert( zColl );. 
231be 20 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f 6c 6c       pKey->aColl
231bf 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  [i] = sqlite3Loc
231c0 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
231c1 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20  e, zColl, -1);. 
231c2 20 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74       pKey->aSort
231c3 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 64 78 2d  Order[i] = pIdx-
231c4 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a  >aSortOrder[i];.
231c5 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79 2d 3e      }.    pKey->
231c6 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 43  nField = (u16)nC
231c7 6f 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  ol;.  }..  if( p
231c8 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20  Parse->nErr ){. 
231c9 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
231ca 28 64 62 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20  (db, pKey);.    
231cb 70 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pKey = 0;.  }.  
231cc 72 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a  return pKey;.}..
231cd 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
231ce 45 6e 64 20 6f 66 20 62 75 69 6c 64 2e 63 20 2a  End of build.c *
231cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
231d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
231d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
231d2 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
231d3 42 65 67 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62  Begin file callb
231d4 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ack.c **********
231d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
231d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
231d7 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32  /*.** 2005 May 2
231d8 33 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  3 .**.** The aut
231d9 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
231da 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
231db 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
231dc 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
231dd 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
231de 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
231df 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
231e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
231e1 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
231e2 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
231e3 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
231e4 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
231e5 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
231e6 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
231e7 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
231e8 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
231e9 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
231ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
231eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
231ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
231ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
231ee 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
231ef 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74  e contains funct
231f0 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 61 63 63  ions used to acc
231f1 65 73 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ess the internal
231f2 20 68 61 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20   hash tables.** 
231f3 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65 64 20  of user defined 
231f4 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f  functions and co
231f5 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
231f6 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 61  s..**.** $Id: ca
231f7 6c 6c 62 61 63 6b 2e 63 2c 76 20 31 2e 33 39 20  llback.c,v 1.39 
231f8 32 30 30 39 2f 30 35 2f 30 33 20 32 30 3a 32 33  2009/05/03 20:23
231f9 3a 35 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :53 drh Exp $.*/
231fa 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20  .../*.** Invoke 
231fb 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e  the 'collation n
231fc 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63 6b 20  eeded' callback 
231fd 74 6f 20 72 65 71 75 65 73 74 20 61 20 63 6f 6c  to request a col
231fe 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a  lation sequence.
231ff 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ** in the databa
23200 73 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  se text encoding
23201 20 6f 66 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20   of name zName, 
23202 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a  length nName..**
23203 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   If the collatio
23204 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2f 0a 73 74  n sequence.*/.st
23205 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c 43 6f  atic void callCo
23206 6c 6c 4e 65 65 64 65 64 28 73 71 6c 69 74 65 33  llNeeded(sqlite3
23207 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
23208 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61   *zName, int nNa
23209 6d 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  me){.  assert( !
2320a 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20  db->xCollNeeded 
2320b 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  || !db->xCollNee
2320c 64 65 64 31 36 20 29 3b 0a 20 20 69 66 28 20 6e  ded16 );.  if( n
2320d 4e 61 6d 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d  Name<0 ) nName =
2320e 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2320f 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 64  (zName);.  if( d
23210 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 29  b->xCollNeeded )
23211 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 78 74  {.    char *zExt
23212 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 44  ernal = sqlite3D
23213 62 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 4e 61  bStrNDup(db, zNa
23214 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20  me, nName);.    
23215 69 66 28 20 21 7a 45 78 74 65 72 6e 61 6c 20 29  if( !zExternal )
23216 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 64 62 2d   return;.    db-
23217 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2d  >xCollNeeded(db-
23218 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c  >pCollNeededArg,
23219 20 64 62 2c 20 28 69 6e 74 29 45 4e 43 28 64 62   db, (int)ENC(db
2321a 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20  ), zExternal);. 
2321b 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2321c 28 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  (db, zExternal);
2321d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
2321e 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20  ITE_OMIT_UTF16. 
2321f 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65   if( db->xCollNe
23220 65 64 65 64 31 36 20 29 7b 0a 20 20 20 20 63 68  eded16 ){.    ch
23221 61 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72  ar const *zExter
23222 6e 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nal;.    sqlite3
23223 5f 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73  _value *pTmp = s
23224 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64  qlite3ValueNew(d
23225 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b);.    sqlite3V
23226 61 6c 75 65 53 65 74 53 74 72 28 70 54 6d 70 2c  alueSetStr(pTmp,
23227 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 53   nName, zName, S
23228 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
23229 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
2322a 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69  zExternal = sqli
2322b 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 54 6d  te3ValueText(pTm
2322c 70 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  p, SQLITE_UTF16N
2322d 41 54 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20  ATIVE);.    if( 
2322e 7a 45 78 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20  zExternal ){.   
2322f 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64     db->xCollNeed
23230 65 64 31 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65  ed16(db->pCollNe
23231 65 64 65 64 41 72 67 2c 20 64 62 2c 20 28 69 6e  ededArg, db, (in
23232 74 29 45 4e 43 28 64 62 29 2c 20 7a 45 78 74 65  t)ENC(db), zExte
23233 72 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rnal);.    }.   
23234 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
23235 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e  e(pTmp);.  }.#en
23236 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  dif.}../*.** Thi
23237 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
23238 6c 65 64 20 69 66 20 74 68 65 20 63 6f 6c 6c 61  led if the colla
23239 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 66 61 69  tion factory fai
2323a 6c 73 20 74 6f 20 64 65 6c 69 76 65 72 20 61 0a  ls to deliver a.
2323b 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e  ** collation fun
2323c 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 62 65 73  ction in the bes
2323d 74 20 65 6e 63 6f 64 69 6e 67 20 62 75 74 20 74  t encoding but t
2323e 68 65 72 65 20 6d 61 79 20 62 65 20 6f 74 68 65  here may be othe
2323f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66  r versions.** of
23240 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20   this collation 
23241 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74  function (for ot
23242 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  her text encodin
23243 67 73 29 20 61 76 61 69 6c 61 62 6c 65 2e 20 55  gs) available. U
23244 73 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65  se one.** of the
23245 73 65 20 69 6e 73 74 65 61 64 20 69 66 20 74 68  se instead if th
23246 65 79 20 65 78 69 73 74 2e 20 41 76 6f 69 64 20  ey exist. Avoid 
23247 61 20 55 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d  a UTF-8 <-> UTF-
23248 31 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66  16 conversion if
23249 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f  .** possible..*/
2324a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 74  .static int synt
2324b 68 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33  hCollSeq(sqlite3
2324c 20 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70   *db, CollSeq *p
2324d 43 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  Coll){.  CollSeq
2324e 20 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72   *pColl2;.  char
2324f 20 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61   *z = pColl->zNa
23250 6d 65 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 71  me;.  int n = sq
23251 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
23252 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 61  ;.  int i;.  sta
23253 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 45 6e  tic const u8 aEn
23254 63 5b 5d 20 3d 20 7b 20 53 51 4c 49 54 45 5f 55  c[] = { SQLITE_U
23255 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55  TF16BE, SQLITE_U
23256 54 46 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f 55  TF16LE, SQLITE_U
23257 54 46 38 20 7d 3b 0a 20 20 66 6f 72 28 69 3d 30  TF8 };.  for(i=0
23258 3b 20 69 3c 33 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<3; i++){.   
23259 20 70 43 6f 6c 6c 32 20 3d 20 73 71 6c 69 74 65   pColl2 = sqlite
2325a 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
2325b 20 61 45 6e 63 5b 69 5d 2c 20 7a 2c 20 6e 2c 20   aEnc[i], z, n, 
2325c 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c  0);.    if( pCol
2325d 6c 32 2d 3e 78 43 6d 70 21 3d 30 20 29 7b 0a 20  l2->xCmp!=0 ){. 
2325e 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c       memcpy(pCol
2325f 6c 2c 20 70 43 6f 6c 6c 32 2c 20 73 69 7a 65 6f  l, pColl2, sizeo
23260 66 28 43 6f 6c 6c 53 65 71 29 29 3b 0a 20 20 20  f(CollSeq));.   
23261 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d     pColl->xDel =
23262 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44   0;         /* D
23263 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 65 20 64  o not copy the d
23264 65 73 74 72 75 63 74 6f 72 20 2a 2f 0a 20 20 20  estructor */.   
23265 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
23266 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
23267 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
23268 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  RROR;.}../*.** T
23269 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2326a 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
2326b 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 6c  invoking the col
2326c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 63  lation factory c
2326d 61 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 72 20 73 75  allback.** or su
2326e 62 73 74 69 74 75 74 69 6e 67 20 61 20 63 6f 6c  bstituting a col
2326f 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
23270 6f 66 20 61 20 64 69 66 66 65 72 65 6e 74 20 65  of a different e
23271 6e 63 6f 64 69 6e 67 20 77 68 65 6e 20 74 68 65  ncoding when the
23272 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 63 6f  .** requested co
23273 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
23274 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c   is not availabl
23275 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
23276 65 20 6e 61 74 69 76 65 0a 2a 2a 20 65 6e 63 6f  e native.** enco
23277 64 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20  ding..** .** If 
23278 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  it is not NULL, 
23279 74 68 65 6e 20 70 43 6f 6c 6c 20 6d 75 73 74 20  then pColl must 
2327a 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 64 61 74  point to the dat
2327b 61 62 61 73 65 20 6e 61 74 69 76 65 20 65 6e 63  abase native enc
2327c 6f 64 69 6e 67 20 0a 2a 2a 20 63 6f 6c 6c 61 74  oding .** collat
2327d 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74  ion sequence wit
2327e 68 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65  h name zName, le
2327f 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a  ngth nName..**.*
23280 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * The return val
23281 75 65 20 69 73 20 65 69 74 68 65 72 20 74 68 65  ue is either the
23282 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
23283 6e 63 65 20 74 6f 20 62 65 20 75 73 65 64 20 69  nce to be used i
23284 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 64 62  n database.** db
23285 20 66 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 20 74   for collation t
23286 79 70 65 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20  ype name zName, 
23287 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2c 20 6f 72  length nName, or
23288 20 4e 55 4c 4c 2c 20 69 66 20 6e 6f 20 63 6f 6c   NULL, if no col
23289 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e  lation.** sequen
2328a 63 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2e  ce can be found.
2328b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2328c 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
2328d 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 0a 20  te3GetCollSeq(. 
2328e 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20   sqlite3* db, . 
2328f 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 2c   CollSeq *pColl,
23290 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   .  const char *
23291 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 6e 4e  zName, .  int nN
23292 61 6d 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  ame.){.  CollSeq
23293 20 2a 70 3b 0a 0a 20 20 70 20 3d 20 70 43 6f 6c   *p;..  p = pCol
23294 6c 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  l;.  if( !p ){. 
23295 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69     p = sqlite3Fi
23296 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e  ndCollSeq(db, EN
23297 43 28 64 62 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  C(db), zName, nN
23298 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  ame, 0);.  }.  i
23299 66 28 20 21 70 20 7c 7c 20 21 70 2d 3e 78 43 6d  f( !p || !p->xCm
2329a 70 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 63  p ){.    /* No c
2329b 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2329c 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 66  e of this type f
2329d 6f 72 20 74 68 69 73 20 65 6e 63 6f 64 69 6e 67  or this encoding
2329e 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 0a   is registered..
2329f 20 20 20 20 2a 2a 20 43 61 6c 6c 20 74 68 65 20      ** Call the 
232a0 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72  collation factor
232a1 79 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 63  y to see if it c
232a2 61 6e 20 73 75 70 70 6c 79 20 75 73 20 77 69 74  an supply us wit
232a3 68 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  h one..    */.  
232a4 20 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64    callCollNeeded
232a5 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  (db, zName, nNam
232a6 65 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  e);.    p = sqli
232a7 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
232a8 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 4e 61 6d  b, ENC(db), zNam
232a9 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  e, nName, 0);.  
232aa 7d 0a 20 20 69 66 28 20 70 20 26 26 20 21 70 2d  }.  if( p && !p-
232ab 3e 78 43 6d 70 20 26 26 20 73 79 6e 74 68 43 6f  >xCmp && synthCo
232ac 6c 6c 53 65 71 28 64 62 2c 20 70 29 20 29 7b 0a  llSeq(db, p) ){.
232ad 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 0a 20      p = 0;.  }. 
232ae 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c 20 70   assert( !p || p
232af 2d 3e 78 43 6d 70 20 29 3b 0a 20 20 72 65 74 75  ->xCmp );.  retu
232b0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn p;.}../*.** T
232b1 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
232b2 61 6c 6c 65 64 20 6f 6e 20 61 20 63 6f 6c 6c 61  alled on a colla
232b3 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 62 65  tion sequence be
232b4 66 6f 72 65 20 69 74 20 69 73 20 75 73 65 64 20  fore it is used 
232b5 74 6f 0a 2a 2a 20 63 68 65 63 6b 20 74 68 61 74  to.** check that
232b6 20 69 74 20 69 73 20 64 65 66 69 6e 65 64 2e 20   it is defined. 
232b7 41 6e 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c  An undefined col
232b8 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
232b9 65 78 69 73 74 73 20 77 68 65 6e 0a 2a 2a 20 61  exists when.** a
232ba 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 61   database is loa
232bb 64 65 64 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ded that contain
232bc 73 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  s references to 
232bd 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
232be 63 65 73 0a 2a 2a 20 74 68 61 74 20 68 61 76 65  ces.** that have
232bf 20 6e 6f 74 20 62 65 65 6e 20 64 65 66 69 6e 65   not been define
232c0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65  d by sqlite3_cre
232c1 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20  ate_collation() 
232c2 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65  etc..**.** If re
232c3 71 75 69 72 65 64 2c 20 74 68 69 73 20 72 6f 75  quired, this rou
232c4 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20 27  tine calls the '
232c5 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64  collation needed
232c6 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 0a 2a 2a  ' callback to.**
232c7 20 72 65 71 75 65 73 74 20 61 20 64 65 66 69 6e   request a defin
232c8 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c  ition of the col
232c9 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e  lating sequence.
232ca 20 49 66 20 74 68 69 73 20 64 6f 65 73 6e 27 74   If this doesn't
232cb 20 77 6f 72 6b 2c 20 0a 2a 2a 20 61 6e 20 65 71   work, .** an eq
232cc 75 69 76 61 6c 65 6e 74 20 63 6f 6c 6c 61 74 69  uivalent collati
232cd 6e 67 20 73 65 71 75 65 6e 63 65 20 74 68 61 74  ng sequence that
232ce 20 75 73 65 73 20 61 20 74 65 78 74 20 65 6e 63   uses a text enc
232cf 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 74 0a  oding different.
232d0 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ** from the main
232d1 20 64 61 74 61 62 61 73 65 20 69 73 20 73 75 62   database is sub
232d2 73 74 69 74 75 74 65 64 2c 20 69 66 20 6f 6e 65  stituted, if one
232d3 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a   is available..*
232d4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
232d5 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63   int sqlite3Chec
232d6 6b 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  kCollSeq(Parse *
232d7 70 50 61 72 73 65 2c 20 43 6f 6c 6c 53 65 71 20  pParse, CollSeq 
232d8 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 66 28 20 70  *pColl){.  if( p
232d9 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 63 6f 6e 73  Coll ){.    cons
232da 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20  t char *zName = 
232db 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pColl->zName;.  
232dc 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 73    CollSeq *p = s
232dd 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
232de 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 43 6f  (pParse->db, pCo
232df 6c 6c 2c 20 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a  ll, zName, -1);.
232e0 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20      if( !p ){.  
232e1 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
232e2 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nErr==0 ){.     
232e3 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
232e4 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
232e5 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  uch collation se
232e6 71 75 65 6e 63 65 3a 20 25 73 22 2c 20 7a 4e 61  quence: %s", zNa
232e7 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  me);.      }.   
232e8 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
232e9 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  +;.      return 
232ea 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
232eb 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
232ec 70 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 7d 0a  p==pColl );.  }.
232ed 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
232ee 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c  OK;.}..../*.** L
232ef 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e  ocate and return
232f0 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74   an entry from t
232f1 68 65 20 64 62 2e 61 43 6f 6c 6c 53 65 71 20 68  he db.aCollSeq h
232f2 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20 74 68  ash table. If th
232f3 65 20 65 6e 74 72 79 0a 2a 2a 20 73 70 65 63 69  e entry.** speci
232f4 66 69 65 64 20 62 79 20 7a 4e 61 6d 65 20 61 6e  fied by zName an
232f5 64 20 6e 4e 61 6d 65 20 69 73 20 6e 6f 74 20 66  d nName is not f
232f6 6f 75 6e 64 20 61 6e 64 20 70 61 72 61 6d 65 74  ound and paramet
232f7 65 72 20 27 63 72 65 61 74 65 27 20 69 73 0a 2a  er 'create' is.*
232f8 2a 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65  * true, then cre
232f9 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 2e  ate a new entry.
232fa 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
232fb 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61  n NULL..**.** Ea
232fc 63 68 20 70 6f 69 6e 74 65 72 20 73 74 6f 72 65  ch pointer store
232fd 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  d in the sqlite3
232fe 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74  .aCollSeq hash t
232ff 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  able contains an
23300 0a 2a 2a 20 61 72 72 61 79 20 6f 66 20 74 68 72  .** array of thr
23301 65 65 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63  ee CollSeq struc
23302 74 75 72 65 73 2e 20 54 68 65 20 66 69 72 73 74  tures. The first
23303 20 69 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   is the collatio
23304 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 70 72  n sequence.** pr
23305 65 66 66 65 72 72 65 64 20 66 6f 72 20 55 54 46  efferred for UTF
23306 2d 38 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 55  -8, the second U
23307 54 46 2d 31 36 6c 65 2c 20 61 6e 64 20 74 68 65  TF-16le, and the
23308 20 74 68 69 72 64 20 55 54 46 2d 31 36 62 65 2e   third UTF-16be.
23309 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 64 20 69 6d  .**.** Stored im
2330a 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20  mediately after 
2330b 74 68 65 20 74 68 72 65 65 20 63 6f 6c 6c 61 74  the three collat
2330c 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 69 73  ion sequences is
2330d 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68   a copy of.** th
2330e 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
2330f 65 6e 63 65 20 6e 61 6d 65 2e 20 41 20 70 6f 69  ence name. A poi
23310 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
23311 69 6e 67 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ing is stored in
23312 0a 2a 2a 20 65 61 63 68 20 63 6f 6c 6c 61 74 69  .** each collati
23313 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 74 72 75  on sequence stru
23314 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  cture..*/.static
23315 20 43 6f 6c 6c 53 65 71 20 2a 66 69 6e 64 43 6f   CollSeq *findCo
23316 6c 6c 53 65 71 45 6e 74 72 79 28 0a 20 20 73 71  llSeqEntry(.  sq
23317 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e  lite3 *db,.  con
23318 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a  st char *zName,.
23319 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 0a 20 20 69    int nName,.  i
2331a 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a 20 20 43  nt create.){.  C
2331b 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
2331c 20 69 66 28 20 6e 4e 61 6d 65 3c 30 20 29 20 6e   if( nName<0 ) n
2331d 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
2331e 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
2331f 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
23320 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43  HashFind(&db->aC
23321 6f 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e  ollSeq, zName, n
23322 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 30 3d  Name);..  if( 0=
23323 3d 70 43 6f 6c 6c 20 26 26 20 63 72 65 61 74 65  =pColl && create
23324 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20   ){.    pColl = 
23325 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
23326 65 72 6f 28 64 62 2c 20 33 2a 73 69 7a 65 6f 66  ero(db, 3*sizeof
23327 28 2a 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65  (*pColl) + nName
23328 20 2b 20 31 20 29 3b 0a 20 20 20 20 69 66 28 20   + 1 );.    if( 
23329 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 43  pColl ){.      C
2332a 6f 6c 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 30  ollSeq *pDel = 0
2332b 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d  ;.      pColl[0]
2332c 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29  .zName = (char*)
2332d 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20  &pColl[3];.     
2332e 20 70 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20   pColl[0].enc = 
2332f 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20  SQLITE_UTF8;.   
23330 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d     pColl[1].zNam
23331 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c  e = (char*)&pCol
23332 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c  l[3];.      pCol
23333 6c 5b 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54  l[1].enc = SQLIT
23334 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 20  E_UTF16LE;.     
23335 20 70 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20   pColl[2].zName 
23336 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b  = (char*)&pColl[
23337 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b  3];.      pColl[
23338 32 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  2].enc = SQLITE_
23339 55 54 46 31 36 42 45 3b 0a 20 20 20 20 20 20 6d  UTF16BE;.      m
2333a 65 6d 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a  emcpy(pColl[0].z
2333b 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Name, zName, nNa
2333c 6d 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  me);.      pColl
2333d 5b 30 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d  [0].zName[nName]
2333e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 44 65 6c   = 0;.      pDel
2333f 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e   = sqlite3HashIn
23340 73 65 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53  sert(&db->aCollS
23341 65 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61  eq, pColl[0].zNa
23342 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c  me, nName, pColl
23343 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
23344 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  a malloc() failu
23345 72 65 20 6f 63 63 75 72 72 65 64 20 69 6e 20 73  re occurred in s
23346 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
23347 28 29 2c 20 69 74 20 77 69 6c 6c 20 0a 20 20 20  (), it will .   
23348 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65     ** return the
23349 20 70 43 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74   pColl pointer t
2334a 6f 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 65  o be deleted (be
2334b 63 61 75 73 65 20 69 74 20 77 61 73 6e 27 74 20  cause it wasn't 
2334c 61 64 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74  added.      ** t
2334d 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
2334e 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
2334f 20 20 61 73 73 65 72 74 28 20 70 44 65 6c 3d 3d    assert( pDel==
23350 30 20 7c 7c 20 70 44 65 6c 3d 3d 70 43 6f 6c 6c  0 || pDel==pColl
23351 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44   );.      if( pD
23352 65 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  el!=0 ){.       
23353 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
23354 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73  d = 1;.        s
23355 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
23356 20 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20   pDel);.        
23357 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20  pColl = 0;.     
23358 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
23359 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a  eturn pColl;.}..
2335a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20  /*.** Parameter 
2335b 7a 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20  zName points to 
2335c 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  a UTF-8 encoded 
2335d 73 74 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79 74  string nName byt
2335e 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75  es long..** Retu
2335f 72 6e 20 74 68 65 20 43 6f 6c 6c 53 65 71 2a 20  rn the CollSeq* 
23360 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20  pointer for the 
23361 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
23362 63 65 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 0a 2a  ce named zName.*
23363 2a 20 66 6f 72 20 74 68 65 20 65 6e 63 6f 64 69  * for the encodi
23364 6e 67 20 27 65 6e 63 27 20 66 72 6f 6d 20 74 68  ng 'enc' from th
23365 65 20 64 61 74 61 62 61 73 65 20 27 64 62 27 2e  e database 'db'.
23366 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 6e  .**.** If the en
23367 74 72 79 20 73 70 65 63 69 66 69 65 64 20 69 73  try specified is
23368 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 27   not found and '
23369 63 72 65 61 74 65 27 20 69 73 20 74 72 75 65 2c  create' is true,
2336a 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 0a 2a   then create a.*
2336b 2a 20 6e 65 77 20 65 6e 74 72 79 2e 20 20 4f 74  * new entry.  Ot
2336c 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e  herwise return N
2336d 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 70  ULL..**.** A sep
2336e 61 72 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 73  arate function s
2336f 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
23370 53 65 71 28 29 20 69 73 20 61 20 77 72 61 70 70  Seq() is a wrapp
23371 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 69  er around.** thi
23372 73 20 72 6f 75 74 69 6e 65 2e 20 20 73 71 6c 69  s routine.  sqli
23373 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
23374 28 29 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63  () invokes the c
23375 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79  ollation factory
23376 0a 2a 2a 20 69 66 20 6e 65 63 65 73 73 61 72 79  .** if necessary
23377 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61   and generates a
23378 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
23379 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  if the collating
2337a 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 63 61 6e   sequence.** can
2337b 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f  not be found..*/
2337c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2337d 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
2337e 46 69 6e 64 43 6f 6c 6c 53 65 71 28 0a 20 20 73  FindCollSeq(.  s
2337f 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 75 38  qlite3 *db,.  u8
23380 20 65 6e 63 2c 0a 20 20 63 6f 6e 73 74 20 63 68   enc,.  const ch
23381 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74  ar *zName,.  int
23382 20 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72   nName,.  int cr
23383 65 61 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65  eate.){.  CollSe
23384 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20  q *pColl;.  if( 
23385 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 70 43 6f  zName ){.    pCo
23386 6c 6c 20 3d 20 66 69 6e 64 43 6f 6c 6c 53 65 71  ll = findCollSeq
23387 45 6e 74 72 79 28 64 62 2c 20 7a 4e 61 6d 65 2c  Entry(db, zName,
23388 20 6e 4e 61 6d 65 2c 20 63 72 65 61 74 65 29 3b   nName, create);
23389 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43  .  }else{.    pC
2338a 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43  oll = db->pDfltC
2338b 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  oll;.  }.  asser
2338c 74 28 20 53 51 4c 49 54 45 5f 55 54 46 38 3d 3d  t( SQLITE_UTF8==
2338d 31 20 26 26 20 53 51 4c 49 54 45 5f 55 54 46 31  1 && SQLITE_UTF1
2338e 36 4c 45 3d 3d 32 20 26 26 20 53 51 4c 49 54 45  6LE==2 && SQLITE
2338f 5f 55 54 46 31 36 42 45 3d 3d 33 20 29 3b 0a 20  _UTF16BE==3 );. 
23390 20 61 73 73 65 72 74 28 20 65 6e 63 3e 3d 53 51   assert( enc>=SQ
23391 4c 49 54 45 5f 55 54 46 38 20 26 26 20 65 6e 63  LITE_UTF8 && enc
23392 3c 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  <=SQLITE_UTF16BE
23393 20 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20   );.  if( pColl 
23394 29 20 70 43 6f 6c 6c 20 2b 3d 20 65 6e 63 2d 31  ) pColl += enc-1
23395 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c  ;.  return pColl
23396 3b 0a 7d 0a 0a 2f 2a 20 44 75 72 69 6e 67 20 74  ;.}../* During t
23397 68 65 20 73 65 61 72 63 68 20 66 6f 72 20 74 68  he search for th
23398 65 20 62 65 73 74 20 66 75 6e 63 74 69 6f 6e 20  e best function 
23399 64 65 66 69 6e 69 74 69 6f 6e 2c 20 74 68 69 73  definition, this
2339a 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73   procedure.** is
2339b 20 63 61 6c 6c 65 64 20 74 6f 20 74 65 73 74 20   called to test 
2339c 68 6f 77 20 77 65 6c 6c 20 74 68 65 20 66 75 6e  how well the fun
2339d 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20  ction passed as 
2339e 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
2339f 6e 74 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68  nt.** matches th
233a0 65 20 72 65 71 75 65 73 74 20 66 6f 72 20 61 20  e request for a 
233a1 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e 41  function with nA
233a2 72 67 20 61 72 67 75 6d 65 6e 74 73 20 69 6e 20  rg arguments in 
233a3 61 20 73 79 73 74 65 6d 0a 2a 2a 20 74 68 61 74  a system.** that
233a4 20 75 73 65 73 20 65 6e 63 6f 64 69 6e 67 20 65   uses encoding e
233a5 6e 63 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65  nc. The value re
233a6 74 75 72 6e 65 64 20 69 6e 64 69 63 61 74 65 73  turned indicates
233a7 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 0a 2a 2a   how well the.**
233a8 20 72 65 71 75 65 73 74 20 69 73 20 6d 61 74 63   request is matc
233a9 68 65 64 2e 20 41 20 68 69 67 68 65 72 20 76 61  hed. A higher va
233aa 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 61 20  lue indicates a 
233ab 62 65 74 74 65 72 20 6d 61 74 63 68 2e 0a 2a 2a  better match..**
233ac 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
233ad 20 76 61 6c 75 65 20 69 73 20 61 6c 77 61 79 73   value is always
233ae 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 36   between 0 and 6
233af 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  , as follows:.**
233b0 0a 2a 2a 20 30 3a 20 4e 6f 74 20 61 20 6d 61 74  .** 0: Not a mat
233b1 63 68 2c 20 6f 72 20 69 66 20 6e 41 72 67 3c 30  ch, or if nArg<0
233b2 20 61 6e 64 20 74 68 65 20 66 75 6e 63 74 69 6f   and the functio
233b3 6e 20 69 73 20 68 61 73 20 6e 6f 20 69 6d 70 6c  n is has no impl
233b4 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 31  ementation..** 1
233b5 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67  : A variable arg
233b6 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20  uments function 
233b7 74 68 61 74 20 70 72 65 66 65 72 73 20 55 54 46  that prefers UTF
233b8 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36  -8 when a UTF-16
233b9 0a 2a 2a 20 20 20 20 65 6e 63 6f 64 69 6e 67 20  .**    encoding 
233ba 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72  is requested, or
233bb 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20   vice versa..** 
233bc 32 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72  2: A variable ar
233bd 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e  guments function
233be 20 74 68 61 74 20 75 73 65 73 20 55 54 46 2d 31   that uses UTF-1
233bf 36 42 45 20 77 68 65 6e 20 55 54 46 2d 31 36 4c  6BE when UTF-16L
233c0 45 20 69 73 0a 2a 2a 20 20 20 20 72 65 71 75 65  E is.**    reque
233c1 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65  sted, or vice ve
233c2 72 73 61 2e 0a 2a 2a 20 33 3a 20 41 20 76 61 72  rsa..** 3: A var
233c3 69 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20  iable arguments 
233c4 66 75 6e 63 74 69 6f 6e 20 75 73 69 6e 67 20 74  function using t
233c5 68 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63  he same text enc
233c6 6f 64 69 6e 67 2e 0a 2a 2a 20 34 3a 20 41 20 66  oding..** 4: A f
233c7 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  unction with the
233c8 20 65 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66   exact number of
233c9 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65   arguments reque
233ca 73 74 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20  sted that.**    
233cb 70 72 65 66 65 72 73 20 55 54 46 2d 38 20 77 68  prefers UTF-8 wh
233cc 65 6e 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f  en a UTF-16 enco
233cd 64 69 6e 67 20 69 73 20 72 65 71 75 65 73 74 65  ding is requeste
233ce 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61  d, or vice versa
233cf 2e 0a 2a 2a 20 35 3a 20 41 20 66 75 6e 63 74 69  ..** 5: A functi
233d0 6f 6e 20 77 69 74 68 20 74 68 65 20 65 78 61 63  on with the exac
233d1 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  t number of argu
233d2 6d 65 6e 74 73 20 72 65 71 75 65 73 74 65 64 20  ments requested 
233d3 74 68 61 74 0a 2a 2a 20 20 20 20 70 72 65 66 65  that.**    prefe
233d4 72 73 20 55 54 46 2d 31 36 4c 45 20 77 68 65 6e  rs UTF-16LE when
233d5 20 55 54 46 2d 31 36 42 45 20 69 73 20 72 65 71   UTF-16BE is req
233d6 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20  uested, or vice 
233d7 76 65 72 73 61 2e 0a 2a 2a 20 36 3a 20 41 6e 20  versa..** 6: An 
233d8 65 78 61 63 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a  exact match..**.
233d9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61  */.static int ma
233da 74 63 68 51 75 61 6c 69 74 79 28 46 75 6e 63 44  tchQuality(FuncD
233db 65 66 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c  ef *p, int nArg,
233dc 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20   u8 enc){.  int 
233dd 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20 69 66 28  match = 0;.  if(
233de 20 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20   p->nArg==-1 || 
233df 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 0a 20  p->nArg==nArg . 
233e0 20 20 7c 7c 20 28 6e 41 72 67 3d 3d 2d 31 20 26    || (nArg==-1 &
233e1 26 20 28 70 2d 3e 78 46 75 6e 63 21 3d 30 20 7c  & (p->xFunc!=0 |
233e2 7c 20 70 2d 3e 78 53 74 65 70 21 3d 30 29 29 0a  | p->xStep!=0)).
233e3 20 20 29 7b 0a 20 20 20 20 6d 61 74 63 68 20 3d    ){.    match =
233e4 20 31 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e   1;.    if( p->n
233e5 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 72  Arg==nArg || nAr
233e6 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 6d  g==-1 ){.      m
233e7 61 74 63 68 20 3d 20 34 3b 0a 20 20 20 20 7d 0a  atch = 4;.    }.
233e8 20 20 20 20 69 66 28 20 65 6e 63 3d 3d 70 2d 3e      if( enc==p->
233e9 69 50 72 65 66 45 6e 63 20 29 7b 0a 20 20 20 20  iPrefEnc ){.    
233ea 20 20 6d 61 74 63 68 20 2b 3d 20 32 3b 0a 20 20    match += 2;.  
233eb 20 20 7d 0a 20 20 20 20 65 6c 73 65 20 69 66 28    }.    else if(
233ec 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54   (enc==SQLITE_UT
233ed 46 31 36 4c 45 20 26 26 20 70 2d 3e 69 50 72 65  F16LE && p->iPre
233ee 66 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  fEnc==SQLITE_UTF
233ef 31 36 42 45 29 20 7c 7c 0a 20 20 20 20 20 20 20  16BE) ||.       
233f0 20 20 20 20 20 20 28 65 6e 63 3d 3d 53 51 4c 49        (enc==SQLI
233f1 54 45 5f 55 54 46 31 36 42 45 20 26 26 20 70 2d  TE_UTF16BE && p-
233f2 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54  >iPrefEnc==SQLIT
233f3 45 5f 55 54 46 31 36 4c 45 29 20 29 7b 0a 20 20  E_UTF16LE) ){.  
233f4 20 20 20 20 6d 61 74 63 68 20 2b 3d 20 31 3b 0a      match += 1;.
233f5 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
233f6 72 6e 20 6d 61 74 63 68 3b 0a 7d 0a 0a 2f 2a 0a  rn match;.}../*.
233f7 2a 2a 20 53 65 61 72 63 68 20 61 20 46 75 6e 63  ** Search a Func
233f8 44 65 66 48 61 73 68 20 66 6f 72 20 61 20 66 75  DefHash for a fu
233f9 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
233fa 67 69 76 65 6e 20 6e 61 6d 65 2e 20 20 52 65 74  given name.  Ret
233fb 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
233fc 20 74 6f 20 74 68 65 20 6d 61 74 63 68 69 6e 67   to the matching
233fd 20 46 75 6e 63 44 65 66 20 69 66 20 66 6f 75 6e   FuncDef if foun
233fe 64 2c 20 6f 72 20 30 20 69 66 20 74 68 65 72 65  d, or 0 if there
233ff 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f   is no match..*/
23400 0a 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20  .static FuncDef 
23401 2a 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28  *functionSearch(
23402 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a  .  FuncDefHash *
23403 70 48 61 73 68 2c 20 20 2f 2a 20 48 61 73 68 20  pHash,  /* Hash 
23404 74 61 62 6c 65 20 74 6f 20 73 65 61 72 63 68 20  table to search 
23405 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20  */.  int h,     
23406 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
23407 68 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 2a 2f  h of the name */
23408 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
23409 46 75 6e 63 2c 20 20 20 2f 2a 20 4e 61 6d 65 20  Func,   /* Name 
2340a 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  of function */. 
2340b 20 69 6e 74 20 6e 46 75 6e 63 20 20 20 20 20 20   int nFunc      
2340c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2340d 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 46 75 6e  of bytes in zFun
2340e 63 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63 44 65  c */.){.  FuncDe
2340f 66 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 48  f *p;.  for(p=pH
23410 61 73 68 2d 3e 61 5b 68 5d 3b 20 70 3b 20 70 3d  ash->a[h]; p; p=
23411 70 2d 3e 70 48 61 73 68 29 7b 0a 20 20 20 20 69  p->pHash){.    i
23412 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  f( sqlite3StrNIC
23413 6d 70 28 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 46 75  mp(p->zName, zFu
23414 6e 63 2c 20 6e 46 75 6e 63 29 3d 3d 30 20 26 26  nc, nFunc)==0 &&
23415 20 70 2d 3e 7a 4e 61 6d 65 5b 6e 46 75 6e 63 5d   p->zName[nFunc]
23416 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
23417 75 72 6e 20 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn p;.    }.  }
23418 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
23419 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e  /*.** Insert a n
2341a 65 77 20 46 75 6e 63 44 65 66 20 69 6e 74 6f 20  ew FuncDef into 
2341b 61 20 46 75 6e 63 44 65 66 48 61 73 68 20 68 61  a FuncDefHash ha
2341c 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  sh table..*/.SQL
2341d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2341e 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49   sqlite3FuncDefI
2341f 6e 73 65 72 74 28 0a 20 20 46 75 6e 63 44 65 66  nsert(.  FuncDef
23420 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a  Hash *pHash,  /*
23421 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
23422 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 69 6e  into which to in
23423 73 65 72 74 20 2a 2f 0a 20 20 46 75 6e 63 44 65  sert */.  FuncDe
23424 66 20 2a 70 44 65 66 20 20 20 20 20 20 20 20 2f  f *pDef        /
23425 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64  * The function d
23426 65 66 69 6e 69 74 69 6f 6e 20 74 6f 20 69 6e 73  efinition to ins
23427 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63  ert */.){.  Func
23428 44 65 66 20 2a 70 4f 74 68 65 72 3b 0a 20 20 69  Def *pOther;.  i
23429 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
2342a 65 33 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d  e3Strlen30(pDef-
2342b 3e 7a 4e 61 6d 65 29 3b 0a 20 20 75 38 20 63 31  >zName);.  u8 c1
2342c 20 3d 20 28 75 38 29 70 44 65 66 2d 3e 7a 4e 61   = (u8)pDef->zNa
2342d 6d 65 5b 30 5d 3b 0a 20 20 69 6e 74 20 68 20 3d  me[0];.  int h =
2342e 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f   (sqlite3UpperTo
2342f 4c 6f 77 65 72 5b 63 31 5d 20 2b 20 6e 4e 61 6d  Lower[c1] + nNam
23430 65 29 20 25 20 41 72 72 61 79 53 69 7a 65 28 70  e) % ArraySize(p
23431 48 61 73 68 2d 3e 61 29 3b 0a 20 20 70 4f 74 68  Hash->a);.  pOth
23432 65 72 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61  er = functionSea
23433 72 63 68 28 70 48 61 73 68 2c 20 68 2c 20 70 44  rch(pHash, h, pD
23434 65 66 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  ef->zName, nName
23435 29 3b 0a 20 20 69 66 28 20 70 4f 74 68 65 72 20  );.  if( pOther 
23436 29 7b 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65  ){.    pDef->pNe
23437 78 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65  xt = pOther->pNe
23438 78 74 3b 0a 20 20 20 20 70 4f 74 68 65 72 2d 3e  xt;.    pOther->
23439 70 4e 65 78 74 20 3d 20 70 44 65 66 3b 0a 20 20  pNext = pDef;.  
2343a 7d 65 6c 73 65 7b 0a 20 20 20 20 70 44 65 66 2d  }else{.    pDef-
2343b 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20  >pNext = 0;.    
2343c 70 44 65 66 2d 3e 70 48 61 73 68 20 3d 20 70 48  pDef->pHash = pH
2343d 61 73 68 2d 3e 61 5b 68 5d 3b 0a 20 20 20 20 70  ash->a[h];.    p
2343e 48 61 73 68 2d 3e 61 5b 68 5d 20 3d 20 70 44 65  Hash->a[h] = pDe
2343f 66 3b 0a 20 20 7d 0a 7d 0a 20 20 0a 20 20 0a 0a  f;.  }.}.  .  ..
23440 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20 75  /*.** Locate a u
23441 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 67 69 76  ser function giv
23442 65 6e 20 61 20 6e 61 6d 65 2c 20 61 20 6e 75 6d  en a name, a num
23443 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
23444 20 61 6e 64 20 61 20 66 6c 61 67 0a 2a 2a 20 69   and a flag.** i
23445 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74 68 65  ndicating whethe
23446 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70  r the function p
23447 72 65 66 65 72 73 20 55 54 46 2d 31 36 20 6f 76  refers UTF-16 ov
23448 65 72 20 55 54 46 2d 38 2e 20 20 52 65 74 75 72  er UTF-8.  Retur
23449 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74  n a.** pointer t
2344a 6f 20 74 68 65 20 46 75 6e 63 44 65 66 20 73 74  o the FuncDef st
2344b 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 66  ructure that def
2344c 69 6e 65 73 20 74 68 61 74 20 66 75 6e 63 74 69  ines that functi
2344d 6f 6e 2c 20 6f 72 20 72 65 74 75 72 6e 0a 2a 2a  on, or return.**
2344e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 75 6e   NULL if the fun
2344f 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65  ction does not e
23450 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  xist..**.** If t
23451 68 65 20 63 72 65 61 74 65 46 6c 61 67 20 61 72  he createFlag ar
23452 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20  gument is true, 
23453 74 68 65 6e 20 61 20 6e 65 77 20 28 62 6c 61 6e  then a new (blan
23454 6b 29 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74  k) FuncDef.** st
23455 72 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74  ructure is creat
23456 65 64 20 61 6e 64 20 6c 69 6b 65 64 20 69 6e 74  ed and liked int
23457 6f 20 74 68 65 20 22 64 62 22 20 73 74 72 75 63  o the "db" struc
23458 74 75 72 65 20 69 66 20 61 0a 2a 2a 20 6e 6f 20  ture if a.** no 
23459 6d 61 74 63 68 69 6e 67 20 66 75 6e 63 74 69 6f  matching functio
2345a 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69  n previously exi
2345b 73 74 65 64 2e 20 20 57 68 65 6e 20 63 72 65 61  sted.  When crea
2345c 74 65 46 6c 61 67 20 69 73 20 74 72 75 65 0a 2a  teFlag is true.*
2345d 2a 20 61 6e 64 20 74 68 65 20 6e 41 72 67 20 70  * and the nArg p
2345e 61 72 61 6d 65 74 65 72 20 69 73 20 2d 31 2c 20  arameter is -1, 
2345f 74 68 65 6e 20 6f 6e 6c 79 20 61 20 66 75 6e 63  then only a func
23460 74 69 6f 6e 20 74 68 61 74 20 61 63 63 65 70 74  tion that accept
23461 73 0a 2a 2a 20 61 6e 79 20 6e 75 6d 62 65 72 20  s.** any number 
23462 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 77 69 6c  of arguments wil
23463 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a  l be returned..*
23464 2a 0a 2a 2a 20 49 66 20 63 72 65 61 74 65 46 6c  *.** If createFl
23465 61 67 20 69 73 20 66 61 6c 73 65 20 61 6e 64 20  ag is false and 
23466 6e 41 72 67 20 69 73 20 2d 31 2c 20 74 68 65 6e  nArg is -1, then
23467 20 74 68 65 20 66 69 72 73 74 20 76 61 6c 69 64   the first valid
23468 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 66 6f 75  .** function fou
23469 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  nd is returned. 
2346a 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 76   A function is v
2346b 61 6c 69 64 20 69 66 20 65 69 74 68 65 72 20 78  alid if either x
2346c 46 75 6e 63 0a 2a 2a 20 6f 72 20 78 53 74 65 70  Func.** or xStep
2346d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a   is non-zero..**
2346e 0a 2a 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61  .** If createFla
2346f 67 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e  g is false, then
23470 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
23471 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6e 61   the required na
23472 6d 65 20 61 6e 64 0a 2a 2a 20 6e 75 6d 62 65 72  me and.** number
23473 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6d 61   of arguments ma
23474 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 65 76  y be returned ev
23475 65 6e 20 69 66 20 74 68 65 20 65 54 65 78 74 52  en if the eTextR
23476 65 70 20 66 6c 61 67 20 64 6f 65 73 20 6e 6f 74  ep flag does not
23477 0a 2a 2a 20 6d 61 74 63 68 20 74 68 61 74 20 72  .** match that r
23478 65 71 75 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c  equested..*/.SQL
23479 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63  ITE_PRIVATE Func
2347a 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  Def *sqlite3Find
2347b 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69  Function(.  sqli
2347c 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f  te3 *db,       /
2347d 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61  * An open databa
2347e 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  se */.  const ch
2347f 61 72 20 2a 7a 4e 61 6d 65 2c 20 2f 2a 20 4e 61  ar *zName, /* Na
23480 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  me of the functi
23481 6f 6e 2e 20 20 4e 6f 74 20 6e 75 6c 6c 2d 74 65  on.  Not null-te
23482 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 20 20 69 6e  rminated */.  in
23483 74 20 6e 4e 61 6d 65 2c 20 20 20 20 20 20 20 20  t nName,        
23484 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
23485 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20  aracters in the 
23486 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  name */.  int nA
23487 72 67 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rg,          /* 
23488 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  Number of argume
23489 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 61  nts.  -1 means a
2348a 6e 79 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75  ny number */.  u
2348b 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20  8 enc,          
2348c 20 20 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74    /* Preferred t
2348d 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a  ext encoding */.
2348e 20 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67    int createFlag
2348f 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 6e       /* Create n
23490 65 77 20 65 6e 74 72 79 20 69 66 20 74 72 75 65  ew entry if true
23491 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6f 74   and does not ot
23492 68 65 72 77 69 73 65 20 65 78 69 73 74 20 2a 2f  herwise exist */
23493 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  .){.  FuncDef *p
23494 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65  ;         /* Ite
23495 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
23496 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 42 65  /.  FuncDef *pBe
23497 73 74 20 3d 20 30 3b 20 2f 2a 20 42 65 73 74 20  st = 0; /* Best 
23498 6d 61 74 63 68 20 66 6f 75 6e 64 20 73 6f 20 66  match found so f
23499 61 72 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73 74  ar */.  int best
2349a 53 63 6f 72 65 20 3d 20 30 3b 20 20 2f 2a 20 53  Score = 0;  /* S
2349b 63 6f 72 65 20 6f 66 20 62 65 73 74 20 6d 61 74  core of best mat
2349c 63 68 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20  ch */.  int h;  
2349d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
2349e 61 73 68 20 76 61 6c 75 65 20 2a 2f 0a 0a 0a 20  ash value */... 
2349f 20 61 73 73 65 72 74 28 20 65 6e 63 3d 3d 53 51   assert( enc==SQ
234a0 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63  LITE_UTF8 || enc
234a1 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
234a2 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f   || enc==SQLITE_
234a3 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28  UTF16BE );.  if(
234a4 20 6e 41 72 67 3c 2d 31 20 29 20 6e 41 72 67 20   nArg<-1 ) nArg 
234a5 3d 20 2d 31 3b 0a 20 20 68 20 3d 20 28 73 71 6c  = -1;.  h = (sql
234a6 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
234a7 5b 28 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b  [(u8)zName[0]] +
234a8 20 6e 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53   nName) % ArrayS
234a9 69 7a 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29  ize(db->aFunc.a)
234aa 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65  ;..  /* First se
234ab 61 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68  arch for a match
234ac 20 61 6d 6f 6e 67 73 74 20 74 68 65 20 61 70 70   amongst the app
234ad 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
234ae 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f   functions..  */
234af 0a 20 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53  .  p = functionS
234b0 65 61 72 63 68 28 26 64 62 2d 3e 61 46 75 6e 63  earch(&db->aFunc
234b1 2c 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  , h, zName, nNam
234b2 65 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29  e);.  while( p )
234b3 7b 0a 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20  {.    int score 
234b4 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70  = matchQuality(p
234b5 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20  , nArg, enc);.  
234b6 20 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74    if( score>best
234b7 53 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70  Score ){.      p
234b8 42 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20  Best = p;.      
234b9 62 65 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72  bestScore = scor
234ba 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d  e;.    }.    p =
234bb 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a   p->pNext;.  }..
234bc 20 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68    /* If no match
234bd 20 69 73 20 66 6f 75 6e 64 2c 20 73 65 61 72 63   is found, searc
234be 68 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 66  h the built-in f
234bf 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20  unctions..  **. 
234c0 20 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 63   ** Except, if c
234c1 72 65 61 74 65 46 6c 61 67 20 69 73 20 74 72 75  reateFlag is tru
234c2 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  e, that means th
234c3 61 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67  at we are trying
234c4 20 74 6f 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c   to.  ** install
234c5 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e   a new function.
234c6 20 20 57 68 61 74 65 76 65 72 20 46 75 6e 63 44    Whatever FuncD
234c7 65 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ef structure is 
234c8 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 0a 20 20  returned will.  
234c9 2a 2a 20 68 61 76 65 20 66 69 65 6c 64 73 20 6f  ** have fields o
234ca 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20  verwritten with 
234cb 6e 65 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  new information 
234cc 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
234cd 74 68 65 0a 20 20 2a 2a 20 6e 65 77 20 66 75 6e  the.  ** new fun
234ce 63 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20  ction.  But the 
234cf 46 75 6e 63 44 65 66 73 20 66 6f 72 20 62 75 69  FuncDefs for bui
234d0 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20  lt-in functions 
234d1 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 20  are read-only.. 
234d2 20 2a 2a 20 53 6f 20 77 65 20 6d 75 73 74 20 6e   ** So we must n
234d3 6f 74 20 73 65 61 72 63 68 20 66 6f 72 20 62 75  ot search for bu
234d4 69 6c 74 2d 69 6e 73 20 77 68 65 6e 20 63 72 65  ilt-ins when cre
234d5 61 74 69 6e 67 20 61 20 6e 65 77 20 66 75 6e 63  ating a new func
234d6 74 69 6f 6e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66  tion..  */ .  if
234d7 28 20 21 63 72 65 61 74 65 46 6c 61 67 20 26 26  ( !createFlag &&
234d8 20 21 70 42 65 73 74 20 29 7b 0a 20 20 20 20 46   !pBest ){.    F
234d9 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73  uncDefHash *pHas
234da 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63  h = &GLOBAL(Func
234db 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33  DefHash, sqlite3
234dc 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29  GlobalFunctions)
234dd 3b 0a 20 20 20 20 70 20 3d 20 66 75 6e 63 74 69  ;.    p = functi
234de 6f 6e 53 65 61 72 63 68 28 70 48 61 73 68 2c 20  onSearch(pHash, 
234df 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
234e0 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 20 29  ;.    while( p )
234e1 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 63 6f 72  {.      int scor
234e2 65 20 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79  e = matchQuality
234e3 28 70 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a  (p, nArg, enc);.
234e4 20 20 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e        if( score>
234e5 62 65 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20  bestScore ){.   
234e6 20 20 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a       pBest = p;.
234e7 20 20 20 20 20 20 20 20 62 65 73 74 53 63 6f 72          bestScor
234e8 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 20  e = score;.     
234e9 20 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e   }.      p = p->
234ea 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  pNext;.    }.  }
234eb 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72  ..  /* If the cr
234ec 65 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74  eateFlag paramet
234ed 65 72 20 69 73 20 74 72 75 65 20 61 6e 64 20 74  er is true and t
234ee 68 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f  he search did no
234ef 74 20 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a  t reveal an.  **
234f0 20 65 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72   exact match for
234f1 20 74 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65   the name, numbe
234f2 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61  r of arguments a
234f3 6e 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65  nd encoding, the
234f4 6e 20 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77  n add a.  ** new
234f5 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61   entry to the ha
234f6 73 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74  sh table and ret
234f7 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69  urn it..  */.  i
234f8 66 28 20 63 72 65 61 74 65 46 6c 61 67 20 26 26  f( createFlag &&
234f9 20 28 62 65 73 74 53 63 6f 72 65 3c 36 20 7c 7c   (bestScore<6 ||
234fa 20 70 42 65 73 74 2d 3e 6e 41 72 67 21 3d 6e 41   pBest->nArg!=nA
234fb 72 67 29 20 26 26 20 0a 20 20 20 20 20 20 28 70  rg) && .      (p
234fc 42 65 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62  Best = sqlite3Db
234fd 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
234fe 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e  izeof(*pBest)+nN
234ff 61 6d 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20 20  ame+1))!=0 ){.  
23500 20 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20 3d    pBest->zName =
23501 20 28 63 68 61 72 20 2a 29 26 70 42 65 73 74 5b   (char *)&pBest[
23502 31 5d 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 6e  1];.    pBest->n
23503 41 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b  Arg = (u16)nArg;
23504 0a 20 20 20 20 70 42 65 73 74 2d 3e 69 50 72 65  .    pBest->iPre
23505 66 45 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 20 20  fEnc = enc;.    
23506 6d 65 6d 63 70 79 28 70 42 65 73 74 2d 3e 7a 4e  memcpy(pBest->zN
23507 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
23508 65 29 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a  e);.    pBest->z
23509 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b  Name[nName] = 0;
2350a 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63  .    sqlite3Func
2350b 44 65 66 49 6e 73 65 72 74 28 26 64 62 2d 3e 61  DefInsert(&db->a
2350c 46 75 6e 63 2c 20 70 42 65 73 74 29 3b 0a 20 20  Func, pBest);.  
2350d 7d 0a 0a 20 20 69 66 28 20 70 42 65 73 74 20 26  }..  if( pBest &
2350e 26 20 28 70 42 65 73 74 2d 3e 78 53 74 65 70 20  & (pBest->xStep 
2350f 7c 7c 20 70 42 65 73 74 2d 3e 78 46 75 6e 63 20  || pBest->xFunc 
23510 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 29 20 29  || createFlag) )
23511 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 65  {.    return pBe
23512 73 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  st;.  }.  return
23513 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   0;.}../*.** Fre
23514 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20  e all resources 
23515 68 65 6c 64 20 62 79 20 74 68 65 20 73 63 68 65  held by the sche
23516 6d 61 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  ma structure. Th
23517 65 20 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e 74  e void* argument
23518 20 70 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61 20   points.** at a 
23519 53 63 68 65 6d 61 20 73 74 72 75 63 74 2e 20 54  Schema struct. T
2351a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  his function doe
2351b 73 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74  s not call sqlit
2351c 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f  e3DbFree(db, ) o
2351d 6e 20 74 68 65 20 0a 2a 2a 20 70 6f 69 6e 74 65  n the .** pointe
2351e 72 20 69 74 73 65 6c 66 2c 20 69 74 20 6a 75 73  r itself, it jus
2351f 74 20 63 6c 65 61 6e 73 20 75 70 20 73 75 62 73  t cleans up subs
23520 69 64 75 61 72 79 20 72 65 73 6f 75 72 63 65 73  iduary resources
23521 20 28 69 2e 65 2e 20 74 68 65 20 63 6f 6e 74 65   (i.e. the conte
23522 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 63  nts.** of the sc
23523 68 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73  hema hash tables
23524 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 63 68  )..**.** The Sch
23525 65 6d 61 2e 63 61 63 68 65 5f 73 69 7a 65 20 76  ema.cache_size v
23526 61 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 63  ariable is not c
23527 6c 65 61 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  leared..*/.SQLIT
23528 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23529 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65  qlite3SchemaFree
2352a 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48 61 73  (void *p){.  Has
2352b 68 20 74 65 6d 70 31 3b 0a 20 20 48 61 73 68 20  h temp1;.  Hash 
2352c 74 65 6d 70 32 3b 0a 20 20 48 61 73 68 45 6c 65  temp2;.  HashEle
2352d 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 53 63 68 65  m *pElem;.  Sche
2352e 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53  ma *pSchema = (S
2352f 63 68 65 6d 61 20 2a 29 70 3b 0a 0a 20 20 74 65  chema *)p;..  te
23530 6d 70 31 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74  mp1 = pSchema->t
23531 62 6c 48 61 73 68 3b 0a 20 20 74 65 6d 70 32 20  blHash;.  temp2 
23532 3d 20 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  = pSchema->trigH
23533 61 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 48 61  ash;.  sqlite3Ha
23534 73 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61 2d  shInit(&pSchema-
23535 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20 73 71  >trigHash);.  sq
23536 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
23537 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
23538 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73  );.  for(pElem=s
23539 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
2353a 74 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70  temp2); pElem; p
2353b 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
2353c 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
2353d 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
2353e 69 67 67 65 72 28 30 2c 20 28 54 72 69 67 67 65  igger(0, (Trigge
2353f 72 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74  r*)sqliteHashDat
23540 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20  a(pElem));.  }. 
23541 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
23542 72 28 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c  r(&temp2);.  sql
23543 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53  ite3HashInit(&pS
23544 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b  chema->tblHash);
23545 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
23546 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65  iteHashFirst(&te
23547 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c  mp1); pElem; pEl
23548 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  em=sqliteHashNex
23549 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54  t(pElem)){.    T
2354a 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c  able *pTab = sql
2354b 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65  iteHashData(pEle
2354c 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  m);.    assert( 
2354d 70 54 61 62 2d 3e 64 62 4d 65 6d 3d 3d 30 20 29  pTab->dbMem==0 )
2354e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  ;.    sqlite3Del
2354f 65 74 65 54 61 62 6c 65 28 70 54 61 62 29 3b 0a  eteTable(pTab);.
23550 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73    }.  sqlite3Has
23551 68 43 6c 65 61 72 28 26 74 65 6d 70 31 29 3b 0a  hClear(&temp1);.
23552 20 20 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54    pSchema->pSeqT
23553 61 62 20 3d 20 30 3b 0a 20 20 70 53 63 68 65 6d  ab = 0;.  pSchem
23554 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 44 42 5f  a->flags &= ~DB_
23555 53 63 68 65 6d 61 4c 6f 61 64 65 64 3b 0a 7d 0a  SchemaLoaded;.}.
23556 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 6e 64 20  ./*.** Find and 
23557 72 65 74 75 72 6e 20 74 68 65 20 73 63 68 65 6d  return the schem
23558 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
23559 68 20 61 20 42 54 72 65 65 2e 20 20 43 72 65 61  h a BTree.  Crea
2355a 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e 65 20  te.** a new one 
2355b 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f  if necessary..*/
2355c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2355d 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53  Schema *sqlite3S
2355e 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33  chemaGet(sqlite3
2355f 20 2a 64 62 2c 20 42 74 72 65 65 20 2a 70 42 74   *db, Btree *pBt
23560 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a 20 70 3b  ){.  Schema * p;
23561 0a 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20  .  if( pBt ){.  
23562 20 20 70 20 3d 20 28 53 63 68 65 6d 61 20 2a 29    p = (Schema *)
23563 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65  sqlite3BtreeSche
23564 6d 61 28 70 42 74 2c 20 73 69 7a 65 6f 66 28 53  ma(pBt, sizeof(S
23565 63 68 65 6d 61 29 2c 20 73 71 6c 69 74 65 33 53  chema), sqlite3S
23566 63 68 65 6d 61 46 72 65 65 29 3b 0a 20 20 7d 65  chemaFree);.  }e
23567 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 28 53 63  lse{.    p = (Sc
23568 68 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 4d 61  hema *)sqlite3Ma
23569 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28  llocZero(sizeof(
2356a 53 63 68 65 6d 61 29 29 3b 0a 20 20 7d 0a 20 20  Schema));.  }.  
2356b 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 64 62  if( !p ){.    db
2356c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2356d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 20 28   1;.  }else if (
2356e 20 30 3d 3d 70 2d 3e 66 69 6c 65 5f 66 6f 72 6d   0==p->file_form
2356f 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  at ){.    sqlite
23570 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 74 62  3HashInit(&p->tb
23571 6c 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c 69  lHash);.    sqli
23572 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e  te3HashInit(&p->
23573 69 64 78 48 61 73 68 29 3b 0a 20 20 20 20 73 71  idxHash);.    sq
23574 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70  lite3HashInit(&p
23575 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20 20  ->trigHash);.   
23576 20 70 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45   p->enc = SQLITE
23577 5f 55 54 46 38 3b 0a 20 20 7d 0a 20 20 72 65 74  _UTF8;.  }.  ret
23578 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  urn p;.}../*****
23579 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2357a 20 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a   callback.c ****
2357b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2357c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2357d 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2357e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2357f 66 69 6c 65 20 64 65 6c 65 74 65 2e 63 20 2a 2a  file delete.c **
23580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23581 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23582 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
23583 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
23584 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
23585 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
23586 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
23587 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
23588 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
23589 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2358a 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2358b 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2358c 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2358d 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2358e 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2358f 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
23590 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
23591 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
23592 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
23593 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
23594 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
23595 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
23596 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23597 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23598 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23599 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2359a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
2359b 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f  ntains C code ro
2359c 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20  utines that are 
2359d 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
2359e 72 73 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  rser.** in order
2359f 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64   to generate cod
235a0 65 20 66 6f 72 20 44 45 4c 45 54 45 20 46 52 4f  e for DELETE FRO
235a1 4d 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a  M statements..**
235a2 0a 2a 2a 20 24 49 64 3a 20 64 65 6c 65 74 65 2e  .** $Id: delete.
235a3 63 2c 76 20 31 2e 32 30 31 20 32 30 30 39 2f 30  c,v 1.201 2009/0
235a4 35 2f 30 31 20 32 31 3a 31 33 3a 33 37 20 64 72  5/01 21:13:37 dr
235a5 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
235a6 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20  * Look up every 
235a7 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61  table that is na
235a8 6d 65 64 20 69 6e 20 70 53 72 63 2e 20 20 49 66  med in pSrc.  If
235a9 20 61 6e 79 20 74 61 62 6c 65 20 69 73 20 6e 6f   any table is no
235aa 74 20 66 6f 75 6e 64 2c 0a 2a 2a 20 61 64 64 20  t found,.** add 
235ab 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
235ac 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   to pParse->zErr
235ad 4d 73 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  Msg and return N
235ae 55 4c 4c 2e 20 20 49 66 20 61 6c 6c 20 74 61 62  ULL.  If all tab
235af 6c 65 73 0a 2a 2a 20 61 72 65 20 66 6f 75 6e 64  les.** are found
235b0 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  , return a point
235b1 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 74  er to the last t
235b2 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
235b3 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
235b4 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
235b5 6b 75 70 28 50 61 72 73 65 20 2a 70 50 61 72 73  kup(Parse *pPars
235b6 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  e, SrcList *pSrc
235b7 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  ){.  struct SrcL
235b8 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
235b9 3d 20 70 53 72 63 2d 3e 61 3b 0a 20 20 54 61 62  = pSrc->a;.  Tab
235ba 6c 65 20 2a 70 54 61 62 3b 0a 20 20 61 73 73 65  le *pTab;.  asse
235bb 72 74 28 20 70 49 74 65 6d 20 26 26 20 70 53 72  rt( pItem && pSr
235bc 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  c->nSrc==1 );.  
235bd 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pTab = sqlite3Lo
235be 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
235bf 2c 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  , 0, pItem->zNam
235c0 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62  e, pItem->zDatab
235c1 61 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ase);.  sqlite3D
235c2 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d  eleteTable(pItem
235c3 2d 3e 70 54 61 62 29 3b 0a 20 20 70 49 74 65 6d  ->pTab);.  pItem
235c4 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20  ->pTab = pTab;. 
235c5 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
235c6 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20   pTab->nRef++;. 
235c7 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
235c8 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28  IndexedByLookup(
235c9 70 50 61 72 73 65 2c 20 70 49 74 65 6d 29 20 29  pParse, pItem) )
235ca 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a  {.    pTab = 0;.
235cb 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54 61    }.  return pTa
235cc 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  b;.}../*.** Chec
235cd 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  k to make sure t
235ce 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 69  he given table i
235cf 73 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66 20  s writable.  If 
235d0 69 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 77 72 69  it is not.** wri
235d1 74 61 62 6c 65 2c 20 67 65 6e 65 72 61 74 65 20  table, generate 
235d2 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
235d3 20 61 6e 64 20 72 65 74 75 72 6e 20 31 2e 20 20   and return 1.  
235d4 49 66 20 69 74 20 69 73 0a 2a 2a 20 77 72 69 74  If it is.** writ
235d5 61 62 6c 65 20 72 65 74 75 72 6e 20 30 3b 0a 2a  able return 0;.*
235d6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
235d7 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 65   int sqlite3IsRe
235d8 61 64 4f 6e 6c 79 28 50 61 72 73 65 20 2a 70 50  adOnly(Parse *pP
235d9 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
235da 62 2c 20 69 6e 74 20 76 69 65 77 4f 6b 29 7b 0a  b, int viewOk){.
235db 20 20 69 66 28 20 28 28 70 54 61 62 2d 3e 74 61    if( ((pTab->ta
235dc 62 46 6c 61 67 73 20 26 20 54 46 5f 52 65 61 64  bFlags & TF_Read
235dd 6f 6e 6c 79 29 21 3d 30 0a 20 20 20 20 20 20 20  only)!=0.       
235de 20 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62 2d   && (pParse->db-
235df 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
235e0 57 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30 0a  WriteSchema)==0.
235e1 20 20 20 20 20 20 20 20 26 26 20 70 50 61 72 73          && pPars
235e2 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 20 0a 23  e->nested==0) .#
235e3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
235e4 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
235e5 20 20 20 20 20 20 7c 7c 20 28 70 54 61 62 2d 3e        || (pTab->
235e6 70 4d 6f 64 20 26 26 20 70 54 61 62 2d 3e 70 4d  pMod && pTab->pM
235e7 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70  od->pModule->xUp
235e8 64 61 74 65 3d 3d 30 29 0a 23 65 6e 64 69 66 0a  date==0).#endif.
235e9 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33    ){.    sqlite3
235ea 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
235eb 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e   "table %s may n
235ec 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 22 2c  ot be modified",
235ed 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
235ee 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
235ef 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
235f0 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20  OMIT_VIEW.  if( 
235f1 21 76 69 65 77 4f 6b 20 26 26 20 70 54 61 62 2d  !viewOk && pTab-
235f2 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
235f3 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
235f4 70 50 61 72 73 65 2c 22 63 61 6e 6e 6f 74 20 6d  pParse,"cannot m
235f5 6f 64 69 66 79 20 25 73 20 62 65 63 61 75 73 65  odify %s because
235f6 20 69 74 20 69 73 20 61 20 76 69 65 77 22 2c 70   it is a view",p
235f7 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
235f8 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23   return 1;.  }.#
235f9 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30  endif.  return 0
235fa 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e  ;.}...#if !defin
235fb 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
235fc 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64  IEW) && !defined
235fd 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
235fe 47 47 45 52 29 0a 2f 2a 0a 2a 2a 20 45 76 61 6c  GGER)./*.** Eval
235ff 75 61 74 65 20 61 20 76 69 65 77 20 61 6e 64 20  uate a view and 
23600 73 74 6f 72 65 20 69 74 73 20 72 65 73 75 6c 74  store its result
23601 20 69 6e 20 61 6e 20 65 70 68 65 6d 65 72 61 6c   in an ephemeral
23602 20 74 61 62 6c 65 2e 20 20 54 68 65 0a 2a 2a 20   table.  The.** 
23603 70 57 68 65 72 65 20 61 72 67 75 6d 65 6e 74 20  pWhere argument 
23604 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 57  is an optional W
23605 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74  HERE clause that
23606 20 72 65 73 74 72 69 63 74 73 20 74 68 65 0a 2a   restricts the.*
23607 2a 20 73 65 74 20 6f 66 20 72 6f 77 73 20 69 6e  * set of rows in
23608 20 74 68 65 20 76 69 65 77 20 74 68 61 74 20 61   the view that a
23609 72 65 20 74 6f 20 62 65 20 61 64 64 65 64 20 74  re to be added t
2360a 6f 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  o the ephemeral 
2360b 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
2360c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2360d 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69 7a 65  lite3Materialize
2360e 56 69 65 77 28 0a 20 20 50 61 72 73 65 20 2a 70  View(.  Parse *p
2360f 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
23610 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
23611 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 56 69 65  */.  Table *pVie
23612 77 2c 20 20 20 20 20 20 20 20 2f 2a 20 56 69 65  w,        /* Vie
23613 77 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a  w definition */.
23614 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20    Expr *pWhere, 
23615 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e         /* Option
23616 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  al WHERE clause 
23617 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 20  to be added */. 
23618 20 69 6e 74 20 69 43 75 72 20 20 20 20 20 20 20   int iCur       
23619 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
2361a 6e 75 6d 62 65 72 20 66 6f 72 20 65 70 68 65 6d  number for ephem
2361b 65 72 69 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 29  erial table */.)
2361c 7b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64  {.  SelectDest d
2361d 65 73 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70  est;.  Select *p
2361e 44 75 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Dup;.  sqlite3 *
2361f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
23620 0a 0a 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74  ..  pDup = sqlit
23621 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
23622 70 56 69 65 77 2d 3e 70 53 65 6c 65 63 74 2c 20  pView->pSelect, 
23623 30 29 3b 0a 20 20 69 66 28 20 70 57 68 65 72 65  0);.  if( pWhere
23624 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20   ){.    SrcList 
23625 2a 70 46 72 6f 6d 3b 0a 20 20 20 20 54 6f 6b 65  *pFrom;.    Toke
23626 6e 20 76 69 65 77 4e 61 6d 65 3b 0a 20 20 20 20  n viewName;.    
23627 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73 71  .    pWhere = sq
23628 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
23629 20 70 57 68 65 72 65 2c 20 30 29 3b 0a 20 20 20   pWhere, 0);.   
2362a 20 76 69 65 77 4e 61 6d 65 2e 7a 20 3d 20 28 75   viewName.z = (u
2362b 38 2a 29 70 56 69 65 77 2d 3e 7a 4e 61 6d 65 3b  8*)pView->zName;
2362c 0a 20 20 20 20 76 69 65 77 4e 61 6d 65 2e 6e 20  .    viewName.n 
2362d 3d 20 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29  = (unsigned int)
2362e 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2362f 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 76 69 65  (const char*)vie
23630 77 4e 61 6d 65 2e 7a 29 3b 0a 20 20 20 20 76 69  wName.z);.    vi
23631 65 77 4e 61 6d 65 2e 71 75 6f 74 65 64 20 3d 20  ewName.quoted = 
23632 30 3b 0a 20 20 20 20 70 46 72 6f 6d 20 3d 20 73  0;.    pFrom = s
23633 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
23634 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72  endFromTerm(pPar
23635 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 26 76 69  se, 0, 0, 0, &vi
23636 65 77 4e 61 6d 65 2c 20 70 44 75 70 2c 20 30 2c  ewName, pDup, 0,
23637 30 29 3b 0a 20 20 20 20 70 44 75 70 20 3d 20 73  0);.    pDup = s
23638 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
23639 70 50 61 72 73 65 2c 20 30 2c 20 70 46 72 6f 6d  pParse, 0, pFrom
2363a 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30 2c 20  , pWhere, 0, 0, 
2363b 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  0, 0, 0, 0);.  }
2363c 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
2363d 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20  DestInit(&dest, 
2363e 53 52 54 5f 45 70 68 65 6d 54 61 62 2c 20 69 43  SRT_EphemTab, iC
2363f 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  ur);.  sqlite3Se
23640 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 44 75  lect(pParse, pDu
23641 70 2c 20 26 64 65 73 74 29 3b 0a 20 20 73 71 6c  p, &dest);.  sql
23642 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
23643 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 23 65  (db, pDup);.}.#e
23644 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
23645 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
23646 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  W) && !defined(S
23647 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
23648 45 52 29 20 2a 2f 0a 0a 23 69 66 20 64 65 66 69  ER) */..#if defi
23649 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
2364a 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f  E_UPDATE_DELETE_
2364b 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e  LIMIT) && !defin
2364c 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
2364d 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a 20 47  UBQUERY)./*.** G
2364e 65 6e 65 72 61 74 65 20 61 6e 20 65 78 70 72 65  enerate an expre
2364f 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20 69 6d  ssion tree to im
23650 70 6c 65 6d 65 6e 74 20 74 68 65 20 57 48 45 52  plement the WHER
23651 45 2c 20 4f 52 44 45 52 20 42 59 2c 0a 2a 2a 20  E, ORDER BY,.** 
23652 61 6e 64 20 4c 49 4d 49 54 2f 4f 46 46 53 45 54  and LIMIT/OFFSET
23653 20 70 6f 72 74 69 6f 6e 20 6f 66 20 44 45 4c 45   portion of DELE
23654 54 45 20 61 6e 64 20 55 50 44 41 54 45 20 73 74  TE and UPDATE st
23655 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
23656 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20      DELETE FROM 
23657 74 61 62 6c 65 5f 77 78 79 7a 20 57 48 45 52 45  table_wxyz WHERE
23658 20 61 3c 35 20 4f 52 44 45 52 20 42 59 20 61 20   a<5 ORDER BY a 
23659 4c 49 4d 49 54 20 31 3b 0a 2a 2a 20 20 20 20 20  LIMIT 1;.**     
2365a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2365b 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f         \________
2365c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2365d 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  __/.**          
2365e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2365f 20 20 20 20 20 70 4c 69 6d 69 74 57 68 65 72 65       pLimitWhere
23660 20 28 70 49 6e 43 6c 61 75 73 65 29 0a 2a 2f 0a   (pInClause).*/.
23661 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
23662 78 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d 69  xpr *sqlite3Limi
23663 74 57 68 65 72 65 28 0a 20 20 50 61 72 73 65 20  tWhere(.  Parse 
23664 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
23665 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
23666 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
23667 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
23668 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23669 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
2366a 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62 6c 65  e -- which table
2366b 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45  s to scan */.  E
2366c 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
2366d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2366e 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2366f 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f    May be null */
23670 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
23671 64 65 72 42 79 2c 20 20 20 20 20 20 20 20 20 20  derBy,          
23672 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
23673 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
23674 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 20 2a  null */.  Expr *
23675 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20 20 20  pLimit,         
23676 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49         /* The LI
23677 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  MIT clause.  May
23678 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78   be null */.  Ex
23679 70 72 20 2a 70 4f 66 66 73 65 74 2c 20 20 20 20  pr *pOffset,    
2367a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2367b 65 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 2e  e OFFSET clause.
2367c 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f    May be null */
2367d 0a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 54 79  .  char *zStmtTy
2367e 70 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  pe              
2367f 2f 2a 20 45 69 74 68 65 72 20 44 45 4c 45 54 45  /* Either DELETE
23680 20 6f 72 20 55 50 44 41 54 45 2e 20 20 46 6f 72   or UPDATE.  For
23681 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e   error messages.
23682 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
23683 57 68 65 72 65 52 6f 77 69 64 20 3d 20 4e 55 4c  WhereRowid = NUL
23684 4c 3b 20 20 20 20 2f 2a 20 57 48 45 52 45 20 72  L;    /* WHERE r
23685 6f 77 69 64 20 2e 2e 20 2a 2f 0a 20 20 45 78 70  owid .. */.  Exp
23686 72 20 2a 70 49 6e 43 6c 61 75 73 65 20 3d 20 4e  r *pInClause = N
23687 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 57 48 45  ULL;      /* WHE
23688 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20 73 65  RE rowid IN ( se
23689 6c 65 63 74 20 29 20 2a 2f 0a 20 20 45 78 70 72  lect ) */.  Expr
2368a 20 2a 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d   *pSelectRowid =
2368b 20 4e 55 4c 4c 3b 20 20 20 2f 2a 20 53 45 4c 45   NULL;   /* SELE
2368c 43 54 20 72 6f 77 69 64 20 2e 2e 2e 20 2a 2f 0a  CT rowid ... */.
2368d 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2368e 73 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 2f  st = NULL;     /
2368f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  * Expression lis
23690 74 20 63 6f 6e 74 61 6e 69 6e 67 20 6f 6e 6c 79  t contaning only
23691 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 2a 2f   pSelectRowid */
23692 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 65 6c  .  SrcList *pSel
23693 65 63 74 53 72 63 20 3d 20 4e 55 4c 4c 3b 20 20  ectSrc = NULL;  
23694 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20  /* SELECT rowid 
23695 46 52 4f 4d 20 78 20 2e 2e 2e 20 28 64 75 70 20  FROM x ... (dup 
23696 6f 66 20 70 53 72 63 29 20 2a 2f 0a 20 20 53 65  of pSrc) */.  Se
23697 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20  lect *pSelect = 
23698 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 43 6f  NULL;      /* Co
23699 6d 70 6c 65 74 65 20 53 45 4c 45 43 54 20 74 72  mplete SELECT tr
2369a 65 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63  ee */..  /* Chec
2369b 6b 20 74 68 61 74 20 74 68 65 72 65 20 69 73 6e  k that there isn
2369c 27 74 20 61 6e 20 4f 52 44 45 52 20 42 59 20 77  't an ORDER BY w
2369d 69 74 68 6f 75 74 20 61 20 4c 49 4d 49 54 20 63  ithout a LIMIT c
2369e 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  lause..  */.  if
2369f 28 20 70 4f 72 64 65 72 42 79 20 26 26 20 28 70  ( pOrderBy && (p
236a0 4c 69 6d 69 74 20 3d 3d 20 30 29 20 29 20 7b 0a  Limit == 0) ) {.
236a1 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
236a2 4d 73 67 28 70 50 61 72 73 65 2c 20 22 4f 52 44  Msg(pParse, "ORD
236a3 45 52 20 42 59 20 77 69 74 68 6f 75 74 20 4c 49  ER BY without LI
236a4 4d 49 54 20 6f 6e 20 25 73 22 2c 20 7a 53 74 6d  MIT on %s", zStm
236a5 74 54 79 70 65 29 3b 0a 20 20 20 20 70 50 61 72  tType);.    pPar
236a6 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72 20 3d  se->parseError =
236a7 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6c 69 6d   1;.    goto lim
236a8 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
236a9 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65  _2;.  }..  /* We
236aa 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 67 65   only need to ge
236ab 6e 65 72 61 74 65 20 61 20 73 65 6c 65 63 74 20  nerate a select 
236ac 65 78 70 72 65 73 73 69 6f 6e 20 69 66 20 74 68  expression if th
236ad 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 6c 69  ere.  ** is a li
236ae 6d 69 74 2f 6f 66 66 73 65 74 20 74 65 72 6d 20  mit/offset term 
236af 74 6f 20 65 6e 66 6f 72 63 65 2e 0a 20 20 2a 2f  to enforce..  */
236b0 0a 20 20 69 66 28 20 70 4c 69 6d 69 74 20 3d 3d  .  if( pLimit ==
236b1 20 30 20 29 20 7b 0a 20 20 20 20 2f 2a 20 69 66   0 ) {.    /* if
236b2 20 70 4c 69 6d 69 74 20 69 73 20 6e 75 6c 6c 2c   pLimit is null,
236b3 20 70 4f 66 66 73 65 74 20 77 69 6c 6c 20 61 6c   pOffset will al
236b4 77 61 79 73 20 62 65 20 6e 75 6c 6c 20 61 73 20  ways be null as 
236b5 77 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 61 73 73  well. */.    ass
236b6 65 72 74 28 20 70 4f 66 66 73 65 74 20 3d 3d 20  ert( pOffset == 
236b7 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 );.    return 
236b8 70 57 68 65 72 65 3b 0a 20 20 7d 0a 0a 20 20 2f  pWhere;.  }..  /
236b9 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 65 6c  * Generate a sel
236ba 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74  ect expression t
236bb 72 65 65 20 74 6f 20 65 6e 66 6f 72 63 65 20 74  ree to enforce t
236bc 68 65 20 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20  he limit/offset 
236bd 0a 20 20 2a 2a 20 74 65 72 6d 20 66 6f 72 20 74  .  ** term for t
236be 68 65 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44  he DELETE or UPD
236bf 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ATE statement.  
236c0 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20 2a  For example:.  *
236c1 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  *   DELETE FROM 
236c2 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20 63 6f  table_a WHERE co
236c3 6c 31 3d 31 20 4f 52 44 45 52 20 42 59 20 63 6f  l1=1 ORDER BY co
236c4 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46 46 53 45  l2 LIMIT 1 OFFSE
236c5 54 20 31 0a 20 20 2a 2a 20 62 65 63 6f 6d 65 73  T 1.  ** becomes
236c6 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54 45 20  :.  **   DELETE 
236c7 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45  FROM table_a WHE
236c8 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20 0a 20  RE rowid IN ( . 
236c9 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72   **     SELECT r
236ca 6f 77 69 64 20 46 52 4f 4d 20 74 61 62 6c 65 5f  owid FROM table_
236cb 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31 20 4f  a WHERE col1=1 O
236cc 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c 49 4d  RDER BY col2 LIM
236cd 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a 20 20  IT 1 OFFSET 1.  
236ce 2a 2a 20 20 20 29 3b 0a 20 20 2a 2f 0a 0a 20 20  **   );.  */..  
236cf 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d 20 73  pSelectRowid = s
236d0 71 6c 69 74 65 33 45 78 70 72 28 70 50 61 72 73  qlite3Expr(pPars
236d1 65 2d 3e 64 62 2c 20 54 4b 5f 52 4f 57 2c 20 30  e->db, TK_ROW, 0
236d2 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  , 0, 0);.  if( p
236d3 53 65 6c 65 63 74 52 6f 77 69 64 20 3d 3d 20 30  SelectRowid == 0
236d4 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68   ) goto limit_wh
236d5 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20  ere_cleanup_2;. 
236d6 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65   pEList = sqlite
236d7 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
236d8 70 50 61 72 73 65 2c 20 30 2c 20 70 53 65 6c 65  pParse, 0, pSele
236d9 63 74 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 69  ctRowid, 0);.  i
236da 66 28 20 70 45 4c 69 73 74 20 3d 3d 20 30 20 29  f( pEList == 0 )
236db 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72   goto limit_wher
236dc 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 0a 20 20  e_cleanup_2;..  
236dd 2f 2a 20 64 75 70 6c 69 63 61 74 65 20 74 68 65  /* duplicate the
236de 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 73 20   FROM clause as 
236df 69 74 20 69 73 20 6e 65 65 64 65 64 20 62 79 20  it is needed by 
236e0 62 6f 74 68 20 74 68 65 20 44 45 4c 45 54 45 2f  both the DELETE/
236e1 55 50 44 41 54 45 20 74 72 65 65 0a 20 20 2a 2a  UPDATE tree.  **
236e2 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43 54 20   and the SELECT 
236e3 73 75 62 74 72 65 65 2e 20 2a 2f 0a 20 20 70 53  subtree. */.  pS
236e4 65 6c 65 63 74 53 72 63 20 3d 20 73 71 6c 69 74  electSrc = sqlit
236e5 65 33 53 72 63 4c 69 73 74 44 75 70 28 70 50 61  e3SrcListDup(pPa
236e6 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2c 20 30  rse->db, pSrc, 0
236e7 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  );.  if( pSelect
236e8 53 72 63 20 3d 3d 20 30 20 29 20 7b 0a 20 20 20  Src == 0 ) {.   
236e9 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
236ea 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
236eb 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  b, pEList);.    
236ec 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65  goto limit_where
236ed 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a  _cleanup_2;.  }.
236ee 0a 20 20 2f 2a 20 67 65 6e 65 72 61 74 65 20 74  .  /* generate t
236ef 68 65 20 53 45 4c 45 43 54 20 65 78 70 72 65 73  he SELECT expres
236f0 73 69 6f 6e 20 74 72 65 65 2e 20 2a 2f 0a 20 20  sion tree. */.  
236f1 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
236f2 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73  3SelectNew(pPars
236f3 65 2c 70 45 4c 69 73 74 2c 70 53 65 6c 65 63 74  e,pEList,pSelect
236f4 53 72 63 2c 70 57 68 65 72 65 2c 30 2c 30 2c 0a  Src,pWhere,0,0,.
236f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
236f6 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 72               pOr
236f7 64 65 72 42 79 2c 30 2c 70 4c 69 6d 69 74 2c 70  derBy,0,pLimit,p
236f8 4f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20 70  Offset);.  if( p
236f9 53 65 6c 65 63 74 20 3d 3d 20 30 20 29 20 72 65  Select == 0 ) re
236fa 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 6e 6f  turn 0;..  /* no
236fb 77 20 67 65 6e 65 72 61 74 65 20 74 68 65 20 6e  w generate the n
236fc 65 77 20 57 48 45 52 45 20 72 6f 77 69 64 20 49  ew WHERE rowid I
236fd 4e 20 63 6c 61 75 73 65 20 66 6f 72 20 74 68 65  N clause for the
236fe 20 44 45 4c 45 54 45 2f 55 44 50 41 54 45 20 2a   DELETE/UDPATE *
236ff 2f 0a 20 20 70 57 68 65 72 65 52 6f 77 69 64 20  /.  pWhereRowid 
23700 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 50  = sqlite3Expr(pP
23701 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52 4f 57  arse->db, TK_ROW
23702 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66  , 0, 0, 0);.  if
23703 28 20 70 57 68 65 72 65 52 6f 77 69 64 20 3d 3d  ( pWhereRowid ==
23704 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f   0 ) goto limit_
23705 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3b  where_cleanup_1;
23706 0a 20 20 70 49 6e 43 6c 61 75 73 65 20 3d 20 73  .  pInClause = s
23707 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
23708 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 57 68 65 72  se, TK_IN, pWher
23709 65 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b 0a 20  eRowid, 0, 0);. 
2370a 20 69 66 28 20 70 49 6e 43 6c 61 75 73 65 20 3d   if( pInClause =
2370b 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74  = 0 ) goto limit
2370c 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31  _where_cleanup_1
2370d 3b 0a 0a 20 20 70 49 6e 43 6c 61 75 73 65 2d 3e  ;..  pInClause->
2370e 78 2e 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c  x.pSelect = pSel
2370f 65 63 74 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65  ect;.  pInClause
23710 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 78 49  ->flags |= EP_xI
23711 73 53 65 6c 65 63 74 3b 0a 20 20 73 71 6c 69 74  sSelect;.  sqlit
23712 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
23713 70 50 61 72 73 65 2c 20 70 49 6e 43 6c 61 75 73  pParse, pInClaus
23714 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 49 6e  e);.  return pIn
23715 43 6c 61 75 73 65 3b 0a 0a 20 20 2f 2a 20 73 6f  Clause;..  /* so
23716 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 77 72 6f  mething went wro
23717 6e 67 2e 20 63 6c 65 61 6e 20 75 70 20 61 6e 79  ng. clean up any
23718 74 68 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2e  thing allocated.
23719 20 2a 2f 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f   */.limit_where_
2371a 63 6c 65 61 6e 75 70 5f 31 3a 0a 20 20 73 71 6c  cleanup_1:.  sql
2371b 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2371c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65  (pParse->db, pSe
2371d 6c 65 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  lect);.  return 
2371e 30 3b 0a 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f  0;..limit_where_
2371f 63 6c 65 61 6e 75 70 5f 32 3a 0a 20 20 73 71 6c  cleanup_2:.  sql
23720 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
23721 50 61 72 73 65 2d 3e 64 62 2c 20 70 57 68 65 72  Parse->db, pWher
23722 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  e);.  sqlite3Exp
23723 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
23724 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42 79  se->db, pOrderBy
23725 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
23726 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
23727 62 2c 20 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71  b, pLimit);.  sq
23728 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
23729 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 66 66  pParse->db, pOff
2372a 73 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  set);.  return 0
2372b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  ;.}.#endif /* de
2372c 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
2372d 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54  BLE_UPDATE_DELET
2372e 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66  E_LIMIT) && !def
2372f 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
23730 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 2f  _SUBQUERY) */../
23731 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
23732 64 65 20 66 6f 72 20 61 20 44 45 4c 45 54 45 20  de for a DELETE 
23733 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74 2e 0a  FROM statement..
23734 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45  **.**     DELETE
23735 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a   FROM table_wxyz
23736 20 57 48 45 52 45 20 61 3c 35 20 41 4e 44 20 62   WHERE a<5 AND b
23737 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20   NOT NULL;.**   
23738 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f                \_
23739 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c  _______/       \
2373a 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2373b 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  /.**            
2373c 20 20 20 20 20 20 70 54 61 62 4c 69 73 74 20 20        pTabList  
2373d 20 20 20 20 20 20 20 20 20 20 20 20 70 57 68 65              pWhe
2373e 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  re.*/.SQLITE_PRI
2373f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23740 33 44 65 6c 65 74 65 46 72 6f 6d 28 0a 20 20 50  3DeleteFrom(.  P
23741 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
23742 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
23743 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
23744 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
23745 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74  st,     /* The t
23746 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20  able from which 
23747 77 65 20 73 68 6f 75 6c 64 20 64 65 6c 65 74 65  we should delete
23748 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70   things */.  Exp
23749 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20  r *pWhere       
2374a 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2374b 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65   clause.  May be
2374c 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 56 64   null */.){.  Vd
2374d 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
2374e 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74       /* The virt
2374f 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67  ual database eng
23750 69 6e 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ine */.  Table *
23751 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
23752 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f  /* The table fro
23753 6d 20 77 68 69 63 68 20 72 65 63 6f 72 64 73 20  m which records 
23754 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20  will be deleted 
23755 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
23756 2a 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 4e  *zDb;       /* N
23757 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
23758 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a  holding pTab */.
23759 20 20 69 6e 74 20 65 6e 64 2c 20 61 64 64 72 20    int end, addr 
2375a 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f  = 0;     /* A co
2375b 75 70 6c 65 20 61 64 64 72 65 73 73 65 73 20 6f  uple addresses o
2375c 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65  f generated code
2375d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
2375e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2375f 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
23760 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49    WhereInfo *pWI
23761 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f  nfo;     /* Info
23762 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
23763 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
23764 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  /.  Index *pIdx;
23765 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
23766 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69  r looping over i
23767 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20 74 61  ndices of the ta
23768 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75  ble */.  int iCu
23769 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
2376a 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e  /* VDBE Cursor n
2376b 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a  umber for pTab *
2376c 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
2376d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2376e 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75  in database stru
2376f 63 74 75 72 65 20 2a 2f 0a 20 20 41 75 74 68 43  cture */.  AuthC
23770 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b  ontext sContext;
23771 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69    /* Authorizati
23772 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  on context */.  
23773 69 6e 74 20 6f 6c 64 49 64 78 20 3d 20 2d 31 3b  int oldIdx = -1;
23774 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
23775 20 66 6f 72 20 74 68 65 20 4f 4c 44 20 74 61 62   for the OLD tab
23776 6c 65 20 6f 66 20 41 46 54 45 52 20 74 72 69 67  le of AFTER trig
23777 67 65 72 73 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  gers */.  NameCo
23778 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20  ntext sNC;      
23779 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74   /* Name context
2377a 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72   to resolve expr
2377b 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20  essions in */.  
2377c 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
2377d 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2377e 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69  se number */.  i
2377f 6e 74 20 6d 65 6d 43 6e 74 20 3d 20 2d 31 3b 20  nt memCnt = -1; 
23780 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
23781 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 63 68  cell used for ch
23782 61 6e 67 65 20 63 6f 75 6e 74 69 6e 67 20 2a 2f  ange counting */
23783 0a 20 20 69 6e 74 20 72 63 61 75 74 68 3b 20 20  .  int rcauth;  
23784 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
23785 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61  ue returned by a
23786 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c  uthorization cal
23787 6c 62 61 63 6b 20 2a 2f 0a 0a 23 69 66 6e 64 65  lback */..#ifnde
23788 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
23789 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69  IGGER.  int isVi
2378a 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ew;             
2378b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2378c 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 64 65  attempting to de
2378d 6c 65 74 65 20 66 72 6f 6d 20 61 20 76 69 65 77  lete from a view
2378e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
2378f 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20  Trigger;        
23790 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61     /* List of ta
23791 62 6c 65 20 74 72 69 67 67 65 72 73 2c 20 69 66  ble triggers, if
23792 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 23 65 6e   required */.#en
23793 64 69 66 0a 20 20 69 6e 74 20 69 42 65 67 69 6e  dif.  int iBegin
23794 41 66 74 65 72 54 72 69 67 67 65 72 20 3d 20 30  AfterTrigger = 0
23795 3b 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66  ;  /* Address of
23796 20 61 66 74 65 72 20 74 72 69 67 67 65 72 20 70   after trigger p
23797 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20  rogram */.  int 
23798 69 45 6e 64 41 66 74 65 72 54 72 69 67 67 65 72  iEndAfterTrigger
23799 20 3d 20 30 3b 20 20 20 20 2f 2a 20 45 78 69 74   = 0;    /* Exit
2379a 20 6f 66 20 61 66 74 65 72 20 74 72 69 67 67 65   of after trigge
2379b 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69  r program */.  i
2379c 6e 74 20 69 42 65 67 69 6e 42 65 66 6f 72 65 54  nt iBeginBeforeT
2379d 72 69 67 67 65 72 20 3d 20 30 3b 20 2f 2a 20 41  rigger = 0; /* A
2379e 64 64 72 65 73 73 20 6f 66 20 62 65 66 6f 72 65  ddress of before
2379f 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
237a0 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 42 65   */.  int iEndBe
237a1 66 6f 72 65 54 72 69 67 67 65 72 20 3d 20 30 3b  foreTrigger = 0;
237a2 20 20 20 2f 2a 20 45 78 69 74 20 6f 66 20 62 65     /* Exit of be
237a3 66 6f 72 65 20 74 72 69 67 67 65 72 20 70 72 6f  fore trigger pro
237a4 67 72 61 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 6c  gram */.  u32 ol
237a5 64 5f 63 6f 6c 5f 6d 61 73 6b 20 3d 20 30 3b 20  d_col_mask = 0; 
237a6 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
237a7 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73 20  f OLD.* columns 
237a8 69 6e 20 75 73 65 20 2a 2f 0a 0a 20 20 73 43 6f  in use */..  sCo
237a9 6e 74 65 78 74 2e 70 50 61 72 73 65 20 3d 20 30  ntext.pParse = 0
237aa 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
237ab 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73  >db;.  if( pPars
237ac 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
237ad 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
237ae 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66     goto delete_f
237af 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  rom_cleanup;.  }
237b0 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 4c  .  assert( pTabL
237b1 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ist->nSrc==1 );.
237b2 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65  .  /* Locate the
237b3 20 74 61 62 6c 65 20 77 68 69 63 68 20 77 65 20   table which we 
237b4 77 61 6e 74 20 74 6f 20 64 65 6c 65 74 65 2e 20  want to delete. 
237b5 20 54 68 69 73 20 74 61 62 6c 65 20 68 61 73 20   This table has 
237b6 74 6f 20 62 65 0a 20 20 2a 2a 20 70 75 74 20 69  to be.  ** put i
237b7 6e 20 61 6e 20 53 72 63 4c 69 73 74 20 73 74 72  n an SrcList str
237b8 75 63 74 75 72 65 20 62 65 63 61 75 73 65 20 73  ucture because s
237b9 6f 6d 65 20 6f 66 20 74 68 65 20 73 75 62 72 6f  ome of the subro
237ba 75 74 69 6e 65 73 20 77 65 0a 20 20 2a 2a 20 77  utines we.  ** w
237bb 69 6c 6c 20 62 65 20 63 61 6c 6c 69 6e 67 20 61  ill be calling a
237bc 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77  re designed to w
237bd 6f 72 6b 20 77 69 74 68 20 6d 75 6c 74 69 70 6c  ork with multipl
237be 65 20 74 61 62 6c 65 73 20 61 6e 64 20 65 78 70  e tables and exp
237bf 65 63 74 0a 20 20 2a 2a 20 61 6e 20 53 72 63 4c  ect.  ** an SrcL
237c0 69 73 74 2a 20 70 61 72 61 6d 65 74 65 72 20 69  ist* parameter i
237c1 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74 20 61  nstead of just a
237c2 20 54 61 62 6c 65 2a 20 70 61 72 61 6d 65 74 65   Table* paramete
237c3 72 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62 20 3d  r..  */.  pTab =
237c4 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c   sqlite3SrcListL
237c5 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54  ookup(pParse, pT
237c6 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70  abList);.  if( p
237c7 54 61 62 3d 3d 30 20 29 20 20 67 6f 74 6f 20 64  Tab==0 )  goto d
237c8 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
237c9 75 70 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65  up;..  /* Figure
237ca 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20   out if we have 
237cb 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64  any triggers and
237cc 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
237cd 69 6e 67 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64  ing.  ** deleted
237ce 20 66 72 6f 6d 20 69 73 20 61 20 76 69 65 77 0a   from is a view.
237cf 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
237d0 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
237d1 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71  .  pTrigger = sq
237d2 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69  lite3TriggersExi
237d3 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  st(pParse, pTab,
237d4 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 30   TK_DELETE, 0, 0
237d5 29 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54  );.  isView = pT
237d6 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a  ab->pSelect!=0;.
237d7 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70  #else.# define p
237d8 54 72 69 67 67 65 72 20 30 0a 23 20 64 65 66 69  Trigger 0.# defi
237d9 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
237da 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
237db 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64  _OMIT_VIEW.# und
237dc 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69  ef isView.# defi
237dd 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
237de 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  if..  if( sqlite
237df 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72  3IsReadOnly(pPar
237e0 73 65 2c 20 70 54 61 62 2c 20 28 70 54 72 69 67  se, pTab, (pTrig
237e1 67 65 72 3f 31 3a 30 29 29 20 29 7b 0a 20 20 20  ger?1:0)) ){.   
237e2 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
237e3 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  m_cleanup;.  }. 
237e4 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
237e5 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
237e6 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
237e7 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
237e8 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 62 20 3d  ->nDb );.  zDb =
237e9 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
237ea 61 6d 65 3b 0a 20 20 72 63 61 75 74 68 20 3d 20  ame;.  rcauth = 
237eb 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
237ec 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
237ed 44 45 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e  DELETE, pTab->zN
237ee 61 6d 65 2c 20 30 2c 20 7a 44 62 29 3b 0a 20 20  ame, 0, zDb);.  
237ef 61 73 73 65 72 74 28 20 72 63 61 75 74 68 3d 3d  assert( rcauth==
237f0 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 61  SQLITE_OK || rca
237f1 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59  uth==SQLITE_DENY
237f2 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49   || rcauth==SQLI
237f3 54 45 5f 49 47 4e 4f 52 45 20 29 3b 0a 20 20 69  TE_IGNORE );.  i
237f4 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54  f( rcauth==SQLIT
237f5 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 67 6f  E_DENY ){.    go
237f6 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  to delete_from_c
237f7 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73  leanup;.  }.  as
237f8 73 65 72 74 28 21 69 73 56 69 65 77 20 7c 7c 20  sert(!isView || 
237f9 70 54 72 69 67 67 65 72 29 3b 0a 0a 20 20 2f 2a  pTrigger);..  /*
237fa 20 49 66 20 70 54 61 62 20 69 73 20 72 65 61 6c   If pTab is real
237fb 6c 79 20 61 20 76 69 65 77 2c 20 6d 61 6b 65 20  ly a view, make 
237fc 73 75 72 65 20 69 74 20 68 61 73 20 62 65 65 6e  sure it has been
237fd 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20   initialized..  
237fe 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
237ff 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
23800 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  es(pParse, pTab)
23801 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
23802 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
23803 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  ;.  }..  /* Allo
23804 63 61 74 65 20 61 20 63 75 72 73 6f 72 20 75 73  cate a cursor us
23805 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
23806 6f 6c 64 2e 2a 20 64 61 74 61 20 66 6f 72 20 61  old.* data for a
23807 20 74 72 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20   trigger..  */. 
23808 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b   if( pTrigger ){
23809 20 0a 20 20 20 20 6f 6c 64 49 64 78 20 3d 20 70   .    oldIdx = p
2380a 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
2380b 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20   }..  /* Assign 
2380c 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74   cursor number t
2380d 6f 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  o the table and 
2380e 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e  all its indices.
2380f 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
23810 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pTabList->nSrc==
23811 31 20 29 3b 0a 20 20 69 43 75 72 20 3d 20 70 54  1 );.  iCur = pT
23812 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75  abList->a[0].iCu
23813 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  rsor = pParse->n
23814 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64  Tab++;.  for(pId
23815 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
23816 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
23817 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61  >pNext){.    pPa
23818 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d  rse->nTab++;.  }
23819 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65  ..  /* Start the
2381a 20 76 69 65 77 20 63 6f 6e 74 65 78 74 0a 20 20   view context.  
2381b 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 20  */.  if( isView 
2381c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  ){.    sqlite3Au
2381d 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50  thContextPush(pP
2381e 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c  arse, &sContext,
2381f 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
23820 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67   }..  /* Begin g
23821 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a  enerating code..
23822 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
23823 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
23824 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b  );.  if( v==0 ){
23825 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
23826 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
23827 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d   }.  if( pParse-
23828 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c  >nested==0 ) sql
23829 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61  ite3VdbeCountCha
2382a 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74  nges(v);.  sqlit
2382b 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
2382c 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 28 70  ation(pParse, (p
2382d 54 72 69 67 67 65 72 3f 31 3a 30 29 2c 20 69 44  Trigger?1:0), iD
2382e 62 29 3b 0a 0a 20 20 69 66 28 20 70 54 72 69 67  b);..  if( pTrig
2382f 67 65 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f  ger ){.    int o
23830 72 63 6f 6e 66 20 3d 20 28 28 70 50 61 72 73 65  rconf = ((pParse
23831 2d 3e 74 72 69 67 53 74 61 63 6b 29 3f 70 50 61  ->trigStack)?pPa
23832 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 2d 3e  rse->trigStack->
23833 6f 72 63 6f 6e 66 3a 4f 45 5f 44 65 66 61 75 6c  orconf:OE_Defaul
23834 74 29 3b 0a 20 20 20 20 69 6e 74 20 69 47 6f 74  t);.    int iGot
23835 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  o = sqlite3VdbeA
23836 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
23837 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71  );.    addr = sq
23838 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
23839 65 6c 28 76 29 3b 0a 0a 20 20 20 20 69 42 65 67  el(v);..    iBeg
2383a 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 20  inBeforeTrigger 
2383b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2383c 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
2383d 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 43 6f   (void)sqlite3Co
2383e 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  deRowTrigger(pPa
2383f 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54  rse, pTrigger, T
23840 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 0a 20 20  K_DELETE, 0, .  
23841 20 20 20 20 20 20 54 52 49 47 47 45 52 5f 42 45        TRIGGER_BE
23842 46 4f 52 45 2c 20 70 54 61 62 2c 20 2d 31 2c 20  FORE, pTab, -1, 
23843 6f 6c 64 49 64 78 2c 20 6f 72 63 6f 6e 66 2c 20  oldIdx, orconf, 
23844 61 64 64 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f 6d  addr, &old_col_m
23845 61 73 6b 2c 20 30 29 3b 0a 20 20 20 20 69 45 6e  ask, 0);.    iEn
23846 64 42 65 66 6f 72 65 54 72 69 67 67 65 72 20 3d  dBeforeTrigger =
23847 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23848 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a  p0(v, OP_Goto);.
23849 0a 20 20 20 20 69 42 65 67 69 6e 41 66 74 65 72  .    iBeginAfter
2384a 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
2384b 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2384c 28 76 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73  (v);.    (void)s
2384d 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
2384e 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 72  gger(pParse, pTr
2384f 69 67 67 65 72 2c 20 54 4b 5f 44 45 4c 45 54 45  igger, TK_DELETE
23850 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 54 52  , 0, .        TR
23851 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70 54 61  IGGER_AFTER, pTa
23852 62 2c 20 2d 31 2c 20 6f 6c 64 49 64 78 2c 20 6f  b, -1, oldIdx, o
23853 72 63 6f 6e 66 2c 20 61 64 64 72 2c 20 26 6f 6c  rconf, addr, &ol
23854 64 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b 0a  d_col_mask, 0);.
23855 20 20 20 20 69 45 6e 64 41 66 74 65 72 54 72 69      iEndAfterTri
23856 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64  gger = sqlite3Vd
23857 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
23858 6f 74 6f 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  oto);..    sqlit
23859 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2385a 2c 20 69 47 6f 74 6f 29 3b 0a 20 20 7d 0a 0a 20  , iGoto);.  }.. 
2385b 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 74 72   /* If we are tr
2385c 79 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66  ying to delete f
2385d 72 6f 6d 20 61 20 76 69 65 77 2c 20 72 65 61 6c  rom a view, real
2385e 69 7a 65 20 74 68 61 74 20 76 69 65 77 20 69 6e  ize that view in
2385f 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d 65  to.  ** a epheme
23860 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ral table..  */.
23861 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
23862 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26  ITE_OMIT_VIEW) &
23863 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
23864 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
23865 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a    if( isView ){.
23866 20 20 20 20 73 71 6c 69 74 65 33 4d 61 74 65 72      sqlite3Mater
23867 69 61 6c 69 7a 65 56 69 65 77 28 70 50 61 72 73  ializeView(pPars
23868 65 2c 20 70 54 61 62 2c 20 70 57 68 65 72 65 2c  e, pTab, pWhere,
23869 20 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64   iCur);.  }.#end
2386a 69 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65  if..  /* Resolve
2386b 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
2386c 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  s in the WHERE c
2386d 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65  lause..  */.  me
2386e 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69  mset(&sNC, 0, si
2386f 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e  zeof(sNC));.  sN
23870 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  C.pParse = pPars
23871 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73  e;.  sNC.pSrcLis
23872 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20  t = pTabList;.  
23873 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
23874 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
23875 2c 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20  , pWhere) ){.   
23876 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
23877 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  m_cleanup;.  }..
23878 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
23879 74 68 65 20 63 6f 75 6e 74 65 72 20 6f 66 20 74  the counter of t
2387a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
2387b 73 20 64 65 6c 65 74 65 64 2c 20 69 66 0a 20 20  s deleted, if.  
2387c 2a 2a 20 77 65 20 61 72 65 20 63 6f 75 6e 74 69  ** we are counti
2387d 6e 67 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20  ng rows..  */.  
2387e 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
2387f 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
23880 20 29 7b 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d   ){.    memCnt =
23881 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
23882 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23883 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
23884 65 67 65 72 2c 20 30 2c 20 6d 65 6d 43 6e 74 29  eger, 0, memCnt)
23885 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
23886 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43  QLITE_OMIT_TRUNC
23887 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  ATE_OPTIMIZATION
23888 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61  .  /* Special ca
23889 73 65 3a 20 41 20 44 45 4c 45 54 45 20 77 69 74  se: A DELETE wit
2388a 68 6f 75 74 20 61 20 57 48 45 52 45 20 63 6c 61  hout a WHERE cla
2388b 75 73 65 20 64 65 6c 65 74 65 73 20 65 76 65 72  use deletes ever
2388c 79 74 68 69 6e 67 2e 0a 20 20 2a 2a 20 49 74 20  ything..  ** It 
2388d 69 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74  is easier just t
2388e 6f 20 65 72 61 73 65 20 74 68 65 20 77 68 6f 6c  o erase the whol
2388f 65 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 2c 20  e table.  Note, 
23890 68 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 20 20  however, that.  
23891 2a 2a 20 74 68 69 73 20 6d 65 61 6e 73 20 74 68  ** this means th
23892 61 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  at the row chang
23893 65 20 63 6f 75 6e 74 20 77 69 6c 6c 20 62 65 20  e count will be 
23894 69 6e 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f 0a  incorrect..  */.
23895 20 20 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51    if( rcauth==SQ
23896 4c 49 54 45 5f 4f 4b 20 26 26 20 70 57 68 65 72  LITE_OK && pWher
23897 65 3d 3d 30 20 26 26 20 21 70 54 72 69 67 67 65  e==0 && !pTrigge
23898 72 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28  r && !IsVirtual(
23899 70 54 61 62 29 20 29 7b 0a 20 20 20 20 61 73 73  pTab) ){.    ass
2389a 65 72 74 28 20 21 69 73 56 69 65 77 20 29 3b 0a  ert( !isView );.
2389b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2389c 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6c 65 61  ddOp4(v, OP_Clea
2389d 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69  r, pTab->tnum, i
2389e 44 62 2c 20 6d 65 6d 43 6e 74 2c 0a 20 20 20 20  Db, memCnt,.    
2389f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
238a0 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50    pTab->zName, P
238a1 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66  4_STATIC);.    f
238a2 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
238a3 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
238a4 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
238a5 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
238a6 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62  x->pSchema==pTab
238a7 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ->pSchema );.   
238a8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
238a9 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72  dOp2(v, OP_Clear
238aa 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44  , pIdx->tnum, iD
238ab 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  b);.    }.  }els
238ac 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
238ad 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45  TE_OMIT_TRUNCATE
238ae 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
238af 0a 20 20 2f 2a 20 54 68 65 20 75 73 75 61 6c 20  .  /* The usual 
238b0 63 61 73 65 3a 20 54 68 65 72 65 20 69 73 20 61  case: There is a
238b1 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 73 6f   WHERE clause so
238b2 20 77 65 20 68 61 76 65 20 74 6f 20 73 63 61 6e   we have to scan
238b3 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 74 68   through.  ** th
238b4 65 20 74 61 62 6c 65 20 61 6e 64 20 70 69 63 6b  e table and pick
238b5 20 77 68 69 63 68 20 72 65 63 6f 72 64 73 20 74   which records t
238b6 6f 20 64 65 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20  o delete..  */. 
238b7 20 7b 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 69   {.    int iRowi
238b8 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  d = ++pParse->nM
238b9 65 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 66  em;    /* Used f
238ba 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77 69 64  or storing rowid
238bb 20 76 61 6c 75 65 73 2e 20 2a 2f 0a 20 20 20 20   values. */.    
238bc 69 6e 74 20 69 52 6f 77 53 65 74 20 3d 20 2b 2b  int iRowSet = ++
238bd 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20  pParse->nMem;   
238be 2f 2a 20 52 65 67 69 73 74 65 72 20 66 6f 72 20  /* Register for 
238bf 72 6f 77 73 65 74 20 6f 66 20 72 6f 77 73 20 74  rowset of rows t
238c0 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20  o delete */.    
238c1 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20  int regRowid;   
238c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
238c3 2f 2a 20 41 63 74 75 61 6c 20 72 65 67 69 73 74  /* Actual regist
238c4 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f  er containing ro
238c5 77 69 64 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  wids */..    /* 
238c6 43 6f 6c 6c 65 63 74 20 72 6f 77 69 64 73 20 6f  Collect rowids o
238c7 66 20 65 76 65 72 79 20 72 6f 77 20 74 6f 20 62  f every row to b
238c8 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  e deleted..    *
238c9 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
238ca 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
238cb 6c 6c 2c 20 30 2c 20 69 52 6f 77 53 65 74 29 3b  ll, 0, iRowSet);
238cc 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71  .    pWInfo = sq
238cd 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
238ce 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
238cf 2c 20 70 57 68 65 72 65 2c 30 2c 57 48 45 52 45  , pWhere,0,WHERE
238d0 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3b  _DUPLICATES_OK);
238d1 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d  .    if( pWInfo=
238d2 3d 30 20 29 20 67 6f 74 6f 20 64 65 6c 65 74 65  =0 ) goto delete
238d3 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
238d4 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71     regRowid = sq
238d5 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
238d6 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
238d7 54 61 62 2c 20 2d 31 2c 20 69 43 75 72 2c 20 69  Tab, -1, iCur, i
238d8 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 73  Rowid, 0);.    s
238d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
238da 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64  (v, OP_RowSetAdd
238db 2c 20 69 52 6f 77 53 65 74 2c 20 72 65 67 52 6f  , iRowSet, regRo
238dc 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 64 62  wid);.    if( db
238dd 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
238de 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20  _CountRows ){.  
238df 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
238e0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
238e1 6d 6d 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a  mm, memCnt, 1);.
238e2 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
238e3 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
238e4 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20  );..    /* Open 
238e5 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  the pseudo-table
238e6 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 4f   used to store O
238e7 4c 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20  LD if there are 
238e8 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f  triggers..    */
238e9 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67 65  .    if( pTrigge
238ea 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
238eb 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
238ec 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6f  OP_OpenPseudo, o
238ed 6c 64 49 64 78 2c 20 30 2c 20 70 54 61 62 2d 3e  ldIdx, 0, pTab->
238ee 6e 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  nCol);.    }..  
238ef 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 76 65 72    /* Delete ever
238f0 79 20 69 74 65 6d 20 77 68 6f 73 65 20 6b 65 79  y item whose key
238f1 20 77 61 73 20 77 72 69 74 74 65 6e 20 74 6f 20   was written to 
238f2 74 68 65 20 6c 69 73 74 20 64 75 72 69 6e 67 20  the list during 
238f3 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
238f4 61 73 65 20 73 63 61 6e 2e 20 20 57 65 20 68 61  ase scan.  We ha
238f5 76 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 65  ve to delete ite
238f6 6d 73 20 61 66 74 65 72 20 74 68 65 20 73 63 61  ms after the sca
238f7 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 0a 20 20  n is complete.  
238f8 20 20 2a 2a 20 62 65 63 61 75 73 65 20 64 65 6c    ** because del
238f9 65 74 69 6e 67 20 61 6e 20 69 74 65 6d 20 63 61  eting an item ca
238fa 6e 20 63 68 61 6e 67 65 20 74 68 65 20 73 63 61  n change the sca
238fb 6e 20 6f 72 64 65 72 2e 0a 20 20 20 20 2a 2f 0a  n order..    */.
238fc 20 20 20 20 65 6e 64 20 3d 20 73 71 6c 69 74 65      end = sqlite
238fd 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
238fe 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 69 73 56  );..    if( !isV
238ff 69 65 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  iew ){.      /* 
23900 4f 70 65 6e 20 63 75 72 73 6f 72 73 20 66 6f 72  Open cursors for
23901 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72   the table we ar
23902 65 20 64 65 6c 65 74 69 6e 67 20 66 72 6f 6d 20  e deleting from 
23903 61 6e 64 20 0a 20 20 20 20 20 20 2a 2a 20 61 6c  and .      ** al
23904 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e 0a 20  l its indices.. 
23905 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
23906 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e  lite3OpenTableAn
23907 64 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c  dIndices(pParse,
23908 20 70 54 61 62 2c 20 69 43 75 72 2c 20 4f 50 5f   pTab, iCur, OP_
23909 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20  OpenWrite);.    
2390a 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69  }..    /* This i
2390b 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  s the beginning 
2390c 6f 66 20 74 68 65 20 64 65 6c 65 74 65 20 6c 6f  of the delete lo
2390d 6f 70 2e 20 49 66 20 61 20 74 72 69 67 67 65 72  op. If a trigger
2390e 20 65 6e 63 6f 75 6e 74 65 72 73 0a 20 20 20 20   encounters.    
2390f 2a 2a 20 61 6e 20 49 47 4e 4f 52 45 20 63 6f 6e  ** an IGNORE con
23910 73 74 72 61 69 6e 74 2c 20 69 74 20 6a 75 6d 70  straint, it jump
23911 73 20 62 61 63 6b 20 74 6f 20 68 65 72 65 2e 0a  s back to here..
23912 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
23913 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20  Trigger ){.     
23914 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
23915 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
23916 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64  );.    }.    add
23917 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
23918 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 6f 77 53  ddOp3(v, OP_RowS
23919 65 74 52 65 61 64 2c 20 69 52 6f 77 53 65 74 2c  etRead, iRowSet,
2391a 20 65 6e 64 2c 20 69 52 6f 77 69 64 29 3b 0a 0a   end, iRowid);..
2391b 20 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72      if( pTrigger
2391c 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44   ){.      int iD
2391d 61 74 61 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  ata = ++pParse->
2391e 6e 4d 65 6d 3b 20 20 20 2f 2a 20 46 6f 72 20 73  nMem;   /* For s
2391f 74 6f 72 69 6e 67 20 72 6f 77 20 64 61 74 61 20  toring row data 
23920 6f 66 20 4f 4c 44 20 74 61 62 6c 65 20 2a 2f 0a  of OLD table */.
23921 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
23922 20 72 65 63 6f 72 64 20 69 73 20 6e 6f 20 6c 6f   record is no lo
23923 6e 67 65 72 20 70 72 65 73 65 6e 74 20 69 6e 20  nger present in 
23924 74 68 65 20 74 61 62 6c 65 2c 20 6a 75 6d 70 20  the table, jump 
23925 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  to the.      ** 
23926 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
23927 66 20 74 68 65 20 6c 6f 6f 70 20 74 68 72 6f 75  f the loop throu
23928 67 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  gh the contents 
23929 6f 66 20 74 68 65 20 66 69 66 6f 2e 0a 20 20 20  of the fifo..   
2392a 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
2392b 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2392c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69   OP_NotExists, i
2392d 43 75 72 2c 20 61 64 64 72 2c 20 69 52 6f 77 69  Cur, addr, iRowi
2392e 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 50 6f  d);..      /* Po
2392f 70 75 6c 61 74 65 20 74 68 65 20 4f 4c 44 2e 2a  pulate the OLD.*
23930 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f   pseudo-table */
23931 0a 20 20 20 20 20 20 69 66 28 20 6f 6c 64 5f 63  .      if( old_c
23932 6f 6c 5f 6d 61 73 6b 20 29 7b 0a 20 20 20 20 20  ol_mask ){.     
23933 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23934 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44 61  dOp2(v, OP_RowDa
23935 74 61 2c 20 69 43 75 72 2c 20 69 44 61 74 61 29  ta, iCur, iData)
23936 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
23937 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23938 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
23939 75 6c 6c 2c 20 30 2c 20 69 44 61 74 61 29 3b 0a  ull, 0, iData);.
2393a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2393b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2393c 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6f 6c  v, OP_Insert, ol
2393d 64 49 64 78 2c 20 69 44 61 74 61 2c 20 69 52 6f  dIdx, iData, iRo
2393e 77 69 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  wid);..      /* 
2393f 4a 75 6d 70 20 62 61 63 6b 20 61 6e 64 20 72 75  Jump back and ru
23940 6e 20 74 68 65 20 42 45 46 4f 52 45 20 74 72 69  n the BEFORE tri
23941 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 20 20 73  ggers */.      s
23942 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23943 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
23944 69 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69 67  iBeginBeforeTrig
23945 67 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ger);.      sqli
23946 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
23947 76 2c 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69  v, iEndBeforeTri
23948 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  gger);.    }..  
23949 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
2394a 0a 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  .      /* Delete
2394b 20 74 68 65 20 72 6f 77 20 2a 2f 0a 23 69 66 6e   the row */.#ifn
2394c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2394d 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
2394e 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c     if( IsVirtual
2394f 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
23950 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56    const char *pV
23951 74 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  tab = (const cha
23952 72 20 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 3b  r *)pTab->pVtab;
23953 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
23954 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65  VtabMakeWritable
23955 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
23956 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23957 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
23958 56 55 70 64 61 74 65 2c 20 30 2c 20 31 2c 20 69  VUpdate, 0, 1, i
23959 52 6f 77 69 64 2c 20 70 56 74 61 62 2c 20 50 34  Rowid, pVtab, P4
2395a 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20 7d 65  _VTAB);.      }e
2395b 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  lse.#endif.     
2395c 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
2395d 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
2395e 65 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ete(pParse, pTab
2395f 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 2c 20  , iCur, iRowid, 
23960 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
23961 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
23962 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
23963 72 65 20 61 72 65 20 72 6f 77 20 74 72 69 67 67  re are row trigg
23964 65 72 73 2c 20 63 6c 6f 73 65 20 61 6c 6c 20 63  ers, close all c
23965 75 72 73 6f 72 73 20 74 68 65 6e 20 69 6e 76 6f  ursors then invo
23966 6b 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 41 46  ke.    ** the AF
23967 54 45 52 20 74 72 69 67 67 65 72 73 0a 20 20 20  TER triggers.   
23968 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69   */.    if( pTri
23969 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a  gger ){.      /*
2396a 20 4a 75 6d 70 20 62 61 63 6b 20 61 6e 64 20 72   Jump back and r
2396b 75 6e 20 74 68 65 20 41 46 54 45 52 20 74 72 69  un the AFTER tri
2396c 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 20 20 73  ggers */.      s
2396d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2396e 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
2396f 69 42 65 67 69 6e 41 66 74 65 72 54 72 69 67 67  iBeginAfterTrigg
23970 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  er);.      sqlit
23971 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
23972 2c 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 67  , iEndAfterTrigg
23973 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  er);.    }..    
23974 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 64 65  /* End of the de
23975 6c 65 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20  lete loop */.   
23976 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23977 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
23978 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71 6c  , addr);.    sql
23979 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
2397a 61 62 65 6c 28 76 2c 20 65 6e 64 29 3b 0a 0a 20  abel(v, end);.. 
2397b 20 20 20 2f 2a 20 43 6c 6f 73 65 20 74 68 65 20     /* Close the 
2397c 63 75 72 73 6f 72 73 20 61 66 74 65 72 20 74 68  cursors after th
2397d 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 72 65 20  e loop if there 
2397e 61 72 65 20 6e 6f 20 72 6f 77 20 74 72 69 67 67  are no row trigg
2397f 65 72 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 21  ers */.    if( !
23980 69 73 56 69 65 77 20 20 26 26 20 21 49 73 56 69  isView  && !IsVi
23981 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
23982 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 70 49       for(i=1, pI
23983 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
23984 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78   pIdx; i++, pIdx
23985 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
23986 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23987 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
23988 6c 6f 73 65 2c 20 69 43 75 72 20 2b 20 69 2c 20  lose, iCur + i, 
23989 70 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20  pIdx->tnum);.   
2398a 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
2398b 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2398c 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b  OP_Close, iCur);
2398d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2398e 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  .  ** Return the
2398f 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
23990 74 68 61 74 20 77 65 72 65 20 64 65 6c 65 74 65  that were delete
23991 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  d. If this routi
23992 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65  ne is .  ** gene
23993 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61  rating code beca
23994 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f  use of a call to
23995 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
23996 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20  rse(), do not.  
23997 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61  ** invoke the ca
23998 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e  llback function.
23999 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
2399a 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
2399b 6f 75 6e 74 52 6f 77 73 20 26 26 20 70 50 61 72  ountRows && pPar
2399c 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26  se->nested==0 &&
2399d 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74   !pParse->trigSt
2399e 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ack ){.    sqlit
2399f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
239a0 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 6d 65  OP_ResultRow, me
239a1 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73 71  mCnt, 1);.    sq
239a2 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
239a3 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73  ols(v, 1);.    s
239a4 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
239a5 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
239a6 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 64  ME_NAME, "rows d
239a7 65 6c 65 74 65 64 22 2c 20 53 51 4c 49 54 45 5f  eleted", SQLITE_
239a8 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 64 65  STATIC);.  }..de
239a9 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75  lete_from_cleanu
239aa 70 3a 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68  p:.  sqlite3Auth
239ab 43 6f 6e 74 65 78 74 50 6f 70 28 26 73 43 6f 6e  ContextPop(&sCon
239ac 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  text);.  sqlite3
239ad 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
239ae 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73  , pTabList);.  s
239af 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
239b0 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
239b1 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  return;.}../*.**
239b2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
239b3 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64  nerates VDBE cod
239b4 65 20 74 68 61 74 20 63 61 75 73 65 73 20 61 20  e that causes a 
239b5 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 0a  single row of a.
239b6 2a 2a 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20  ** single table 
239b7 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a  to be deleted..*
239b8 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6d 75  *.** The VDBE mu
239b9 73 74 20 62 65 20 69 6e 20 61 20 70 61 72 74 69  st be in a parti
239ba 63 75 6c 61 72 20 73 74 61 74 65 20 77 68 65 6e  cular state when
239bb 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
239bc 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68 65 73   called..** Thes
239bd 65 20 61 72 65 20 74 68 65 20 72 65 71 75 69 72  e are the requir
239be 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ements:.**.**   
239bf 31 2e 20 20 41 20 72 65 61 64 2f 77 72 69 74 65  1.  A read/write
239c0 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   cursor pointing
239c1 20 74 6f 20 70 54 61 62 2c 20 74 68 65 20 74 61   to pTab, the ta
239c2 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ble containing t
239c3 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20  he row.**       
239c4 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2c 20 6d  to be deleted, m
239c5 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 61 73  ust be opened as
239c6 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 22   cursor number "
239c7 62 61 73 65 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 32  base"..**.**   2
239c8 2e 20 20 52 65 61 64 2f 77 72 69 74 65 20 63 75  .  Read/write cu
239c9 72 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20 69 6e  rsors for all in
239ca 64 69 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75  dices of pTab mu
239cb 73 74 20 62 65 20 6f 70 65 6e 20 61 73 0a 2a 2a  st be open as.**
239cc 20 20 20 20 20 20 20 63 75 72 73 6f 72 20 6e 75         cursor nu
239cd 6d 62 65 72 20 62 61 73 65 2b 69 20 66 6f 72 20  mber base+i for 
239ce 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78 2e 0a  the i-th index..
239cf 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68 65 20  **.**   3.  The 
239d0 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66  record number of
239d1 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64   the row to be d
239d2 65 6c 65 74 65 64 20 6d 75 73 74 20 62 65 20 73  eleted must be s
239d3 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 20 20 20 20  tored in.**     
239d4 20 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 52    memory cell iR
239d5 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  owid..**.** This
239d6 20 72 6f 75 74 69 6e 65 20 70 6f 70 73 20 74 68   routine pops th
239d7 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
239d8 63 6b 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65  ck to remove the
239d9 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 0a 2a   record number.*
239da 2a 20 61 6e 64 20 74 68 65 6e 20 67 65 6e 65 72  * and then gener
239db 61 74 65 73 20 63 6f 64 65 20 74 6f 20 72 65 6d  ates code to rem
239dc 6f 76 65 20 62 6f 74 68 20 74 68 65 20 74 61 62  ove both the tab
239dd 6c 65 20 72 65 63 6f 72 64 20 61 6e 64 20 61 6c  le record and al
239de 6c 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 69  l index.** entri
239df 65 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f  es that point to
239e0 20 74 68 61 74 20 72 65 63 6f 72 64 2e 0a 2a 2f   that record..*/
239e1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
239e2 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65  void sqlite3Gene
239e3 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 0a 20  rateRowDelete(. 
239e4 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
239e5 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
239e6 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
239e7 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f  e *pTab,       /
239e8 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  * Table containi
239e9 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65  ng the row to be
239ea 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e   deleted */.  in
239eb 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20  t iCur,         
239ec 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
239ed 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  r for the table 
239ee 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 77 69 64 2c  */.  int iRowid,
239ef 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
239f0 79 20 63 65 6c 6c 20 74 68 61 74 20 63 6f 6e 74  y cell that cont
239f1 61 69 6e 73 20 74 68 65 20 72 6f 77 69 64 20 74  ains the rowid t
239f2 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e  o delete */.  in
239f3 74 20 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20  t count         
239f4 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68   /* Increment th
239f5 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75  e row change cou
239f6 6e 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  nter */.){.  int
239f7 20 61 64 64 72 3b 0a 20 20 56 64 62 65 20 2a 76   addr;.  Vdbe *v
239f8 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d  ;..  v = pParse-
239f9 3e 70 56 64 62 65 3b 0a 20 20 61 64 64 72 20 3d  >pVdbe;.  addr =
239fa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
239fb 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
239fc 74 73 2c 20 69 43 75 72 2c 20 30 2c 20 69 52 6f  ts, iCur, 0, iRo
239fd 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 47  wid);.  sqlite3G
239fe 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44  enerateRowIndexD
239ff 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70 54  elete(pParse, pT
23a00 61 62 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20  ab, iCur, 0);.  
23a01 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23a02 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20  2(v, OP_Delete, 
23a03 69 43 75 72 2c 20 28 63 6f 75 6e 74 3f 4f 50 46  iCur, (count?OPF
23a04 4c 41 47 5f 4e 43 48 41 4e 47 45 3a 30 29 29 3b  LAG_NCHANGE:0));
23a05 0a 20 20 69 66 28 20 63 6f 75 6e 74 20 29 7b 0a  .  if( count ){.
23a06 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
23a07 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70  hangeP4(v, -1, p
23a08 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53  Tab->zName, P4_S
23a09 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71  TATIC);.  }.  sq
23a0a 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
23a0b 65 28 76 2c 20 61 64 64 72 29 3b 0a 7d 0a 0a 2f  e(v, addr);.}../
23a0c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
23a0d 65 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45  e generates VDBE
23a0e 20 63 6f 64 65 20 74 68 61 74 20 63 61 75 73 65   code that cause
23a0f 73 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 20 6f  s the deletion o
23a10 66 20 61 6c 6c 0a 2a 2a 20 69 6e 64 65 78 20 65  f all.** index e
23a11 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65  ntries associate
23a12 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20  d with a single 
23a13 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  row of a single 
23a14 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  table..**.** The
23a15 20 56 44 42 45 20 6d 75 73 74 20 62 65 20 69 6e   VDBE must be in
23a16 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74   a particular st
23a17 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f  ate when this ro
23a18 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
23a19 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 74 68  .** These are th
23a1a 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  e requirements:.
23a1b 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72 65  **.**   1.  A re
23a1c 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20  ad/write cursor 
23a1d 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 61 62  pointing to pTab
23a1e 2c 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74  , the table cont
23a1f 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a  aining the row.*
23a20 2a 20 20 20 20 20 20 20 74 6f 20 62 65 20 64 65  *       to be de
23a21 6c 65 74 65 64 2c 20 6d 75 73 74 20 62 65 20 6f  leted, must be o
23a22 70 65 6e 65 64 20 61 73 20 63 75 72 73 6f 72 20  pened as cursor 
23a23 6e 75 6d 62 65 72 20 22 69 43 75 72 22 2e 0a 2a  number "iCur"..*
23a24 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 64 2f  *.**   2.  Read/
23a25 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 66 6f  write cursors fo
23a26 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66  r all indices of
23a27 20 70 54 61 62 20 6d 75 73 74 20 62 65 20 6f 70   pTab must be op
23a28 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 63  en as.**       c
23a29 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75  ursor number iCu
23a2a 72 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68  r+i for the i-th
23a2b 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20   index..**.**   
23a2c 33 2e 20 20 54 68 65 20 22 69 43 75 72 22 20 63  3.  The "iCur" c
23a2d 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f  ursor must be po
23a2e 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 6f  inting to the ro
23a2f 77 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 0a  w that is to be.
23a30 2a 2a 20 20 20 20 20 20 20 64 65 6c 65 74 65 64  **       deleted
23a31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23a32 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23a33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
23a34 44 65 6c 65 74 65 28 0a 20 20 50 61 72 73 65 20  Delete(.  Parse 
23a35 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20  *pParse,     /* 
23a36 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65  Parsing and code
23a37 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74   generating cont
23a38 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
23a39 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54  pTab,       /* T
23a3a 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
23a3b 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65  the row to be de
23a3c 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  leted */.  int i
23a3d 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Cur,          /*
23a3e 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   Cursor number f
23a3f 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  or the table */.
23a40 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 20    int *aRegIdx  
23a41 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 64 65 6c       /* Only del
23a42 65 74 65 20 69 66 20 61 52 65 67 49 64 78 21 3d  ete if aRegIdx!=
23a43 30 20 26 26 20 61 52 65 67 49 64 78 5b 69 5d 3e  0 && aRegIdx[i]>
23a44 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  0 */.){.  int i;
23a45 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a  .  Index *pIdx;.
23a46 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 66 6f 72    int r1;..  for
23a47 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d  (i=1, pIdx=pTab-
23a48 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 69  >pIndex; pIdx; i
23a49 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  ++, pIdx=pIdx->p
23a4a 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 61  Next){.    if( a
23a4b 52 65 67 49 64 78 21 3d 30 20 26 26 20 61 52 65  RegIdx!=0 && aRe
23a4c 67 49 64 78 5b 69 2d 31 5d 3d 3d 30 20 29 20 63  gIdx[i-1]==0 ) c
23a4d 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 31 20  ontinue;.    r1 
23a4e 3d 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  = sqlite3Generat
23a4f 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65  eIndexKey(pParse
23a50 2c 20 70 49 64 78 2c 20 69 43 75 72 2c 20 30 2c  , pIdx, iCur, 0,
23a51 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
23a52 56 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73  VdbeAddOp3(pPars
23a53 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 49 64 78  e->pVdbe, OP_Idx
23a54 44 65 6c 65 74 65 2c 20 69 43 75 72 2b 69 2c 20  Delete, iCur+i, 
23a55 72 31 2c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  r1,pIdx->nColumn
23a56 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  +1);.  }.}../*.*
23a57 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
23a58 74 68 61 74 20 77 69 6c 6c 20 61 73 73 65 6d 62  that will assemb
23a59 6c 65 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20  le an index key 
23a5a 61 6e 64 20 70 75 74 20 69 74 20 69 6e 20 72 65  and put it in re
23a5b 67 69 73 74 65 72 0a 2a 2a 20 72 65 67 4f 75 74  gister.** regOut
23a5c 2e 20 20 54 68 65 20 6b 65 79 20 77 69 74 68 20  .  The key with 
23a5d 62 65 20 66 6f 72 20 69 6e 64 65 78 20 70 49 64  be for index pId
23a5e 78 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e  x which is an in
23a5f 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a 20  dex on pTab..** 
23a60 69 43 75 72 20 69 73 20 74 68 65 20 69 6e 64 65  iCur is the inde
23a61 78 20 6f 66 20 61 20 63 75 72 73 6f 72 20 6f 70  x of a cursor op
23a62 65 6e 20 6f 6e 20 74 68 65 20 70 54 61 62 20 74  en on the pTab t
23a63 61 62 6c 65 20 61 6e 64 20 70 6f 69 6e 74 69 6e  able and pointin
23a64 67 20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e 74 72  g to.** the entr
23a65 79 20 74 68 61 74 20 6e 65 65 64 73 20 69 6e 64  y that needs ind
23a66 65 78 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  exing..**.** Ret
23a67 75 72 6e 20 61 20 72 65 67 69 73 74 65 72 20 6e  urn a register n
23a68 75 6d 62 65 72 20 77 68 69 63 68 20 69 73 20 74  umber which is t
23a69 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 62 6c  he first in a bl
23a6a 6f 63 6b 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74  ock of.** regist
23a6b 65 72 73 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ers that holds t
23a6c 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  he elements of t
23a6d 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 20 20 54  he index key.  T
23a6e 68 65 0a 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20 72  he.** block of r
23a6f 65 67 69 73 74 65 72 73 20 68 61 73 20 61 6c 72  egisters has alr
23a70 65 61 64 79 20 62 65 65 6e 20 64 65 61 6c 6c 6f  eady been deallo
23a71 63 61 74 65 64 20 62 79 20 74 68 65 20 74 69 6d  cated by the tim
23a72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  e.** this routin
23a73 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51  e returns..*/.SQ
23a74 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
23a75 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
23a76 49 6e 64 65 78 4b 65 79 28 0a 20 20 50 61 72 73  IndexKey(.  Pars
23a77 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
23a78 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
23a79 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  t */.  Index *pI
23a7a 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65  dx,       /* The
23a7b 20 69 6e 64 65 78 20 66 6f 72 20 77 68 69 63 68   index for which
23a7c 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 6b   to generate a k
23a7d 65 79 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  ey */.  int iCur
23a7e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  ,          /* Cu
23a7f 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
23a80 74 68 65 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  the pIdx->pTable
23a81 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
23a82 72 65 67 4f 75 74 2c 20 20 20 20 20 20 20 20 2f  regOut,        /
23a83 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20  * Write the new 
23a84 69 6e 64 65 78 20 6b 65 79 20 74 6f 20 74 68 69  index key to thi
23a85 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  s register */.  
23a86 69 6e 74 20 64 6f 4d 61 6b 65 52 65 63 20 20 20  int doMakeRec   
23a87 20 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 4f 50     /* Run the OP
23a88 5f 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74  _MakeRecord inst
23a89 72 75 63 74 69 6f 6e 20 69 66 20 74 72 75 65 20  ruction if true 
23a8a 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  */.){.  Vdbe *v 
23a8b 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
23a8c 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 54 61 62 6c  .  int j;.  Tabl
23a8d 65 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e  e *pTab = pIdx->
23a8e 70 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 72 65  pTable;.  int re
23a8f 67 42 61 73 65 3b 0a 20 20 69 6e 74 20 6e 43 6f  gBase;.  int nCo
23a90 6c 3b 0a 0a 20 20 6e 43 6f 6c 20 3d 20 70 49 64  l;..  nCol = pId
23a91 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 72 65  x->nColumn;.  re
23a92 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33 47  gBase = sqlite3G
23a93 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
23a94 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 73  se, nCol+1);.  s
23a95 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23a96 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43  (v, OP_Rowid, iC
23a97 75 72 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c  ur, regBase+nCol
23a98 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  );.  for(j=0; j<
23a99 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  nCol; j++){.    
23a9a 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d 3e  int idx = pIdx->
23a9b 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20  aiColumn[j];.   
23a9c 20 69 66 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e   if( idx==pTab->
23a9d 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73  iPKey ){.      s
23a9e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23a9f 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65  (v, OP_SCopy, re
23aa0 67 42 61 73 65 2b 6e 43 6f 6c 2c 20 72 65 67 42  gBase+nCol, regB
23aa1 61 73 65 2b 6a 29 3b 0a 20 20 20 20 7d 65 6c 73  ase+j);.    }els
23aa2 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
23aa3 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
23aa4 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69  _Column, iCur, i
23aa5 64 78 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a  dx, regBase+j);.
23aa6 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c        sqlite3Col
23aa7 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54  umnDefault(v, pT
23aa8 61 62 2c 20 69 64 78 29 3b 0a 20 20 20 20 7d 0a  ab, idx);.    }.
23aa9 20 20 7d 0a 20 20 69 66 28 20 64 6f 4d 61 6b 65    }.  if( doMake
23aaa 52 65 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Rec ){.    sqlit
23aab 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
23aac 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
23aad 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 2c 20  egBase, nCol+1, 
23aae 72 65 67 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c  regOut);.    sql
23aaf 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
23ab0 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b 0a 20  yStr(v, pIdx);. 
23ab1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
23ab2 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
23ab3 65 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  e(pParse, regBas
23ab4 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 7d 0a  e, nCol+1);.  }.
23ab5 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
23ab6 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
23ab7 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b  , regBase, nCol+
23ab8 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 67  1);.  return reg
23ab9 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 20 4d 61 6b 65  Base;.}../* Make
23aba 20 73 75 72 65 20 22 69 73 56 69 65 77 22 20 67   sure "isView" g
23abb 65 74 73 20 75 6e 64 65 66 69 6e 65 64 20 69 6e  ets undefined in
23abc 20 63 61 73 65 20 74 68 69 73 20 66 69 6c 65 20   case this file 
23abd 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 0a  becomes part of.
23abe 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74  ** the amalgamat
23abf 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74 20 73 75  ion - so that su
23ac0 62 73 65 71 75 65 6e 74 20 66 69 6c 65 73 20 64  bsequent files d
23ac1 6f 20 6e 6f 74 20 73 65 65 20 69 73 56 69 65 77  o not see isView
23ac2 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72 6f 2e 20   as a.** macro. 
23ac3 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56 69 65 77  */.#undef isView
23ac4 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
23ac5 2a 20 45 6e 64 20 6f 66 20 64 65 6c 65 74 65 2e  * End of delete.
23ac6 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
23ac7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ac8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ac9 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
23aca 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 75 6e  * Begin file fun
23acb 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c.c ************
23acc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23acd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ace 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 32 20 46 65 62  /./*.** 2002 Feb
23acf 72 75 61 72 79 20 32 33 0a 2a 2a 0a 2a 2a 20 54  ruary 23.**.** T
23ad0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
23ad1 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
23ad2 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
23ad3 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
23ad4 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
23ad5 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
23ad6 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
23ad7 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
23ad8 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
23ad9 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
23ada 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
23adb 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
23adc 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
23add 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
23ade 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
23adf 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
23ae0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
23ae1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ae2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ae3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ae4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ae5 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
23ae6 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
23ae7 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  e C functions th
23ae8 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72  at implement var
23ae9 69 6f 75 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63  ious SQL.** func
23aea 74 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e  tions of SQLite.
23aeb 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69    .**.** There i
23aec 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72  s only one expor
23aed 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68  ted symbol in th
23aee 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75  is file - the fu
23aef 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65  nction.** sqlite
23af0 52 65 67 69 73 74 65 72 42 75 69 6c 64 69 6e 46  RegisterBuildinF
23af1 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64  unctions() found
23af2 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f   at the bottom o
23af3 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41  f the file..** A
23af4 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61  ll other code ha
23af5 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a  s file scope..**
23af6 0a 2a 2a 20 24 49 64 3a 20 66 75 6e 63 2e 63 2c  .** $Id: func.c,
23af7 76 20 31 2e 32 33 34 20 32 30 30 39 2f 30 34 2f  v 1.234 2009/04/
23af8 32 30 20 31 32 3a 30 37 3a 33 37 20 64 72 68 20  20 12:07:37 drh 
23af9 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
23afa 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 6c 61  Return the colla
23afb 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 61 73  ting function as
23afc 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
23afd 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
23afe 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  tic CollSeq *sql
23aff 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
23b00 65 71 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  eq(sqlite3_conte
23b01 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20  xt *context){.  
23b02 72 65 74 75 72 6e 20 63 6f 6e 74 65 78 74 2d 3e  return context->
23b03 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pColl;.}../*.** 
23b04 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
23b05 66 20 74 68 65 20 6e 6f 6e 2d 61 67 67 72 65 67  f the non-aggreg
23b06 61 74 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61  ate min() and ma
23b07 78 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f  x() functions.*/
23b08 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e  .static void min
23b09 6d 61 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  maxFunc(.  sqlit
23b0a 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
23b0b 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
23b0c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
23b0d 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74   **argv.){.  int
23b0e 20 69 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 20   i;.  int mask; 
23b0f 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 69 6e 28     /* 0 for min(
23b10 29 20 6f 72 20 30 78 66 66 66 66 66 66 66 66 20  ) or 0xffffffff 
23b11 66 6f 72 20 6d 61 78 28 29 20 2a 2f 0a 20 20 69  for max() */.  i
23b12 6e 74 20 69 42 65 73 74 3b 0a 20 20 43 6f 6c 6c  nt iBest;.  Coll
23b13 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 61  Seq *pColl;..  a
23b14 73 73 65 72 74 28 20 61 72 67 63 3e 31 20 29 3b  ssert( argc>1 );
23b15 0a 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65  .  mask = sqlite
23b16 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74  3_user_data(cont
23b17 65 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d 31  ext)==0 ? 0 : -1
23b18 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ;.  pColl = sqli
23b19 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65  te3GetFuncCollSe
23b1a 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73  q(context);.  as
23b1b 73 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a 20  sert( pColl );. 
23b1c 20 61 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d 2d   assert( mask==-
23b1d 31 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a  1 || mask==0 );.
23b1e 20 20 69 42 65 73 74 20 3d 20 30 3b 0a 20 20 69    iBest = 0;.  i
23b1f 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
23b20 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d  _type(argv[0])==
23b21 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65  SQLITE_NULL ) re
23b22 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31 3b  turn;.  for(i=1;
23b23 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20   i<argc; i++){. 
23b24 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76     if( sqlite3_v
23b25 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69  alue_type(argv[i
23b26 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
23b27 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66  ) return;.    if
23b28 28 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d  ( (sqlite3MemCom
23b29 70 61 72 65 28 61 72 67 76 5b 69 42 65 73 74 5d  pare(argv[iBest]
23b2a 2c 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c  , argv[i], pColl
23b2b 29 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20 20  )^mask)>=0 ){.  
23b2c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6d 61      testcase( ma
23b2d 73 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  sk==0 );.      i
23b2e 42 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a  Best = i;.    }.
23b2f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65    }.  sqlite3_re
23b30 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
23b31 78 74 2c 20 61 72 67 76 5b 69 42 65 73 74 5d 29  xt, argv[iBest])
23b32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
23b33 6e 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68  n the type of th
23b34 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  e argument..*/.s
23b35 74 61 74 69 63 20 76 6f 69 64 20 74 79 70 65 6f  tatic void typeo
23b36 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  fFunc(.  sqlite3
23b37 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23b38 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
23b39 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
23b3a 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
23b3b 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b  nst char *z = 0;
23b3c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
23b3d 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
23b3e 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f  switch( sqlite3_
23b3f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
23b40 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20  0]) ){.    case 
23b41 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20  SQLITE_INTEGER: 
23b42 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b 20 62  z = "integer"; b
23b43 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
23b44 51 4c 49 54 45 5f 54 45 58 54 3a 20 20 20 20 7a  QLITE_TEXT:    z
23b45 20 3d 20 22 74 65 78 74 22 3b 20 20 20 20 62 72   = "text";    br
23b46 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51  eak;.    case SQ
23b47 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20  LITE_FLOAT:   z 
23b48 3d 20 22 72 65 61 6c 22 3b 20 20 20 20 62 72 65  = "real";    bre
23b49 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
23b4a 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d  ITE_BLOB:    z =
23b4b 20 22 62 6c 6f 62 22 3b 20 20 20 20 62 72 65 61   "blob";    brea
23b4c 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
23b4d 20 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20              z = 
23b4e 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65 61 6b  "null";    break
23b4f 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
23b50 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
23b51 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49  ext, z, -1, SQLI
23b52 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a  TE_STATIC);.}...
23b53 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
23b54 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67  tion of the leng
23b55 74 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f  th() function.*/
23b56 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e  .static void len
23b57 67 74 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  gthFunc(.  sqlit
23b58 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
23b59 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
23b5a 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
23b5b 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74   **argv.){.  int
23b5c 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28   len;..  assert(
23b5d 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e   argc==1 );.  UN
23b5e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
23b5f 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20  rgc);.  switch( 
23b60 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
23b61 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20  pe(argv[0]) ){. 
23b62 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42     case SQLITE_B
23b63 4c 4f 42 3a 0a 20 20 20 20 63 61 73 65 20 53 51  LOB:.    case SQ
23b64 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20  LITE_INTEGER:.  
23b65 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c    case SQLITE_FL
23b66 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  OAT: {.      sql
23b67 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
23b68 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33  context, sqlite3
23b69 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
23b6a 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 62 72  v[0]));.      br
23b6b 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
23b6c 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a  ase SQLITE_TEXT:
23b6d 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75   {.      const u
23b6e 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 20  nsigned char *z 
23b6f 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
23b70 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
23b71 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20       if( z==0 ) 
23b72 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 6c 65  return;.      le
23b73 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69  n = 0;.      whi
23b74 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20  le( *z ){.      
23b75 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20    len++;.       
23b76 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
23b77 38 28 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  8(z);.      }.  
23b78 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
23b79 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20  lt_int(context, 
23b7a 6c 65 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61  len);.      brea
23b7b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
23b7c 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 71  ault: {.      sq
23b7d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
23b7e 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  l(context);.    
23b7f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
23b80 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
23b81 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
23b82 65 20 61 62 73 28 29 20 66 75 6e 63 74 69 6f 6e  e abs() function
23b83 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23b84 61 62 73 46 75 6e 63 28 73 71 6c 69 74 65 33 5f  absFunc(sqlite3_
23b85 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
23b86 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
23b87 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
23b88 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ){.  assert( arg
23b89 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==1 );.  UNUSED
23b8a 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
23b8b 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69  ;.  switch( sqli
23b8c 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
23b8d 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63  rgv[0]) ){.    c
23b8e 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  ase SQLITE_INTEG
23b8f 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 36 34 20  ER: {.      i64 
23b90 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76  iVal = sqlite3_v
23b91 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b  alue_int64(argv[
23b92 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  0]);.      if( i
23b93 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  Val<0 ){.       
23b94 20 69 66 28 20 28 69 56 61 6c 3c 3c 31 29 3d 3d   if( (iVal<<1)==
23b95 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
23b96 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
23b97 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e  ror(context, "in
23b98 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c  teger overflow",
23b99 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20   -1);.          
23b9a 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
23b9b 7d 0a 20 20 20 20 20 20 20 20 69 56 61 6c 20 3d  }.        iVal =
23b9c 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20 20 7d 20   -iVal;.      } 
23b9d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
23b9e 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74  esult_int64(cont
23b9f 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20 20 20 20  ext, iVal);.    
23ba0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
23ba1 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e     case SQLITE_N
23ba2 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  ULL: {.      sql
23ba3 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
23ba4 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20  (context);.     
23ba5 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
23ba6 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
23ba7 20 20 20 64 6f 75 62 6c 65 20 72 56 61 6c 20 3d     double rVal =
23ba8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
23ba9 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a  ouble(argv[0]);.
23baa 20 20 20 20 20 20 69 66 28 20 72 56 61 6c 3c 30        if( rVal<0
23bab 20 29 20 72 56 61 6c 20 3d 20 2d 72 56 61 6c 3b   ) rVal = -rVal;
23bac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
23bad 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e  esult_double(con
23bae 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a 20 20 20  text, rVal);.   
23baf 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
23bb0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
23bb1 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
23bb2 68 65 20 73 75 62 73 74 72 28 29 20 66 75 6e 63  he substr() func
23bb3 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73  tion..**.** subs
23bb4 74 72 28 78 2c 70 31 2c 70 32 29 20 20 72 65 74  tr(x,p1,p2)  ret
23bb5 75 72 6e 73 20 70 32 20 63 68 61 72 61 63 74 65  urns p2 characte
23bb6 72 73 20 6f 66 20 78 5b 5d 20 62 65 67 69 6e 6e  rs of x[] beginn
23bb7 69 6e 67 20 77 69 74 68 20 70 31 2e 0a 2a 2a 20  ing with p1..** 
23bb8 70 31 20 69 73 20 31 2d 69 6e 64 65 78 65 64 2e  p1 is 1-indexed.
23bb9 20 20 53 6f 20 73 75 62 73 74 72 28 78 2c 31 2c    So substr(x,1,
23bba 31 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 66  1) returns the f
23bbb 69 72 73 74 20 63 68 61 72 61 63 74 65 72 0a 2a  irst character.*
23bbc 2a 20 6f 66 20 78 2e 20 20 49 66 20 78 20 69 73  * of x.  If x is
23bbd 20 74 65 78 74 2c 20 74 68 65 6e 20 77 65 20 61   text, then we a
23bbe 63 74 75 61 6c 6c 79 20 63 6f 75 6e 74 20 55 54  ctually count UT
23bbf 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 2e 0a  F-8 characters..
23bc0 2a 2a 20 49 66 20 78 20 69 73 20 61 20 62 6c 6f  ** If x is a blo
23bc1 62 2c 20 74 68 65 6e 20 77 65 20 63 6f 75 6e 74  b, then we count
23bc2 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   bytes..**.** If
23bc3 20 70 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c   p1 is negative,
23bc4 20 74 68 65 6e 20 77 65 20 62 65 67 69 6e 20 61   then we begin a
23bc5 62 73 28 70 31 29 20 66 72 6f 6d 20 74 68 65 20  bs(p1) from the 
23bc6 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2f 0a 73  end of x[]..*/.s
23bc7 74 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74  tatic void subst
23bc8 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  rFunc(.  sqlite3
23bc9 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23bca 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
23bcb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
23bcc 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
23bcd 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
23bce 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  z;.  const unsig
23bcf 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20  ned char *z2;.  
23bd0 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 70  int len;.  int p
23bd1 30 74 79 70 65 3b 0a 20 20 69 36 34 20 70 31 2c  0type;.  i64 p1,
23bd2 20 70 32 3b 0a 20 20 69 6e 74 20 6e 65 67 50 32   p2;.  int negP2
23bd3 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
23bd4 20 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72 67 63   argc==3 || argc
23bd5 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c  ==2 );.  if( sql
23bd6 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
23bd7 61 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45  argv[1])==SQLITE
23bd8 5f 4e 55 4c 4c 0a 20 20 20 7c 7c 20 28 61 72 67  _NULL.   || (arg
23bd9 63 3d 3d 33 20 26 26 20 73 71 6c 69 74 65 33 5f  c==3 && sqlite3_
23bda 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
23bdb 32 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  2])==SQLITE_NULL
23bdc 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ).  ){.    retur
23bdd 6e 3b 0a 20 20 7d 0a 20 20 70 30 74 79 70 65 20  n;.  }.  p0type 
23bde 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
23bdf 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20  type(argv[0]);. 
23be0 20 69 66 28 20 70 30 74 79 70 65 3d 3d 53 51 4c   if( p0type==SQL
23be1 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20  ITE_BLOB ){.    
23be2 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  len = sqlite3_va
23be3 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
23be4 5d 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  ]);.    z = sqli
23be5 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
23be6 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[0]);.    if(
23be7 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
23be8 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3d      assert( len=
23be9 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  =sqlite3_value_b
23bea 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20 29 3b  ytes(argv[0]) );
23beb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20  .  }else{.    z 
23bec 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
23bed 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
23bee 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65     if( z==0 ) re
23bef 74 75 72 6e 3b 0a 20 20 20 20 6c 65 6e 20 3d 20  turn;.    len = 
23bf0 30 3b 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b  0;.    for(z2=z;
23bf1 20 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a 20 20   *z2; len++){.  
23bf2 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
23bf3 55 54 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a  UTF8(z2);.    }.
23bf4 20 20 7d 0a 20 20 70 31 20 3d 20 73 71 6c 69 74    }.  p1 = sqlit
23bf5 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67  e3_value_int(arg
23bf6 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 61 72 67  v[1]);.  if( arg
23bf7 63 3d 3d 33 20 29 7b 0a 20 20 20 20 70 32 20 3d  c==3 ){.    p2 =
23bf8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
23bf9 6e 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  nt(argv[2]);.   
23bfa 20 69 66 28 20 70 32 3c 30 20 29 7b 0a 20 20 20   if( p2<0 ){.   
23bfb 20 20 20 70 32 20 3d 20 2d 70 32 3b 0a 20 20 20     p2 = -p2;.   
23bfc 20 20 20 6e 65 67 50 32 20 3d 20 31 3b 0a 20 20     negP2 = 1;.  
23bfd 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
23bfe 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f   p2 = sqlite3_co
23bff 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
23c00 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d 69 74  context)->aLimit
23c01 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
23c02 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 69 66 28  NGTH];.  }.  if(
23c03 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 70 31 20   p1<0 ){.    p1 
23c04 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20  += len;.    if( 
23c05 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32  p1<0 ){.      p2
23c06 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 69 66   += p1;.      if
23c07 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b  ( p2<0 ) p2 = 0;
23c08 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20  .      p1 = 0;. 
23c09 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
23c0a 20 70 31 3e 30 20 29 7b 0a 20 20 20 20 70 31 2d   p1>0 ){.    p1-
23c0b 2d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  -;.  }else if( p
23c0c 32 3e 30 20 29 7b 0a 20 20 20 20 70 32 2d 2d 3b  2>0 ){.    p2--;
23c0d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 50 32  .  }.  if( negP2
23c0e 20 29 7b 0a 20 20 20 20 70 31 20 2d 3d 20 70 32   ){.    p1 -= p2
23c0f 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30 20 29  ;.    if( p1<0 )
23c10 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 70 31  {.      p2 += p1
23c11 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a  ;.      p1 = 0;.
23c12 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
23c13 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 32 3e  rt( p1>=0 && p2>
23c14 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 31 2b 70  =0 );.  if( p1+p
23c15 32 3e 6c 65 6e 20 29 7b 0a 20 20 20 20 70 32 20  2>len ){.    p2 
23c16 3d 20 6c 65 6e 2d 70 31 3b 0a 20 20 20 20 69 66  = len-p1;.    if
23c17 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b  ( p2<0 ) p2 = 0;
23c18 0a 20 20 7d 0a 20 20 69 66 28 20 70 30 74 79 70  .  }.  if( p0typ
23c19 65 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  e!=SQLITE_BLOB )
23c1a 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20  {.    while( *z 
23c1b 26 26 20 70 31 20 29 7b 0a 20 20 20 20 20 20 53  && p1 ){.      S
23c1c 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
23c1d 7a 29 3b 0a 20 20 20 20 20 20 70 31 2d 2d 3b 0a  z);.      p1--;.
23c1e 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 7a 32      }.    for(z2
23c1f 3d 7a 3b 20 2a 7a 32 20 26 26 20 70 32 3b 20 70  =z; *z2 && p2; p
23c20 32 2d 2d 29 7b 0a 20 20 20 20 20 20 53 51 4c 49  2--){.      SQLI
23c21 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29  TE_SKIP_UTF8(z2)
23c22 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
23c23 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
23c24 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29  context, (char*)
23c25 7a 2c 20 28 69 6e 74 29 28 7a 32 2d 7a 29 2c 20  z, (int)(z2-z), 
23c26 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
23c27 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
23c28 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
23c29 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68  lob(context, (ch
23c2a 61 72 2a 29 26 7a 5b 70 31 5d 2c 20 28 69 6e 74  ar*)&z[p1], (int
23c2b 29 70 32 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  )p2, SQLITE_TRAN
23c2c 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  SIENT);.  }.}../
23c2d 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
23c2e 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f 75 6e 64  ion of the round
23c2f 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 23  () function.*/.#
23c30 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23c31 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
23c32 54 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f  T.static void ro
23c33 75 6e 64 46 75 6e 63 28 73 71 6c 69 74 65 33 5f  undFunc(sqlite3_
23c34 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
23c35 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
23c36 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
23c37 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a  ){.  int n = 0;.
23c38 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68    double r;.  ch
23c39 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 20 20 2f  ar zBuf[500];  /
23c3a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  * larger than th
23c3b 65 20 25 66 20 72 65 70 72 65 73 65 6e 74 61 74  e %f representat
23c3c 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 72 67 65  ion of the large
23c3d 73 74 20 64 6f 75 62 6c 65 20 2a 2f 0a 20 20 61  st double */.  a
23c3e 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c  ssert( argc==1 |
23c3f 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69  | argc==2 );.  i
23c40 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20  f( argc==2 ){.  
23c41 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c    if( SQLITE_NUL
23c42 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  L==sqlite3_value
23c43 5f 74 79 70 65 28 61 72 67 76 5b 31 5d 29 20 29  _type(argv[1]) )
23c44 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6e 20 3d   return;.    n =
23c45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
23c46 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  nt(argv[1]);.   
23c47 20 69 66 28 20 6e 3e 33 30 20 29 20 6e 20 3d 20   if( n>30 ) n = 
23c48 33 30 3b 0a 20 20 20 20 69 66 28 20 6e 3c 30 20  30;.    if( n<0 
23c49 29 20 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  ) n = 0;.  }.  i
23c4a 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
23c4b 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d  _type(argv[0])==
23c4c 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65  SQLITE_NULL ) re
23c4d 74 75 72 6e 3b 0a 20 20 72 20 3d 20 73 71 6c 69  turn;.  r = sqli
23c4e 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
23c4f 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c  (argv[0]);.  sql
23c50 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
23c51 7a 65 6f 66 28 7a 42 75 66 29 2c 7a 42 75 66 2c  zeof(zBuf),zBuf,
23c52 22 25 2e 2a 66 22 2c 6e 2c 72 29 3b 0a 20 20 73  "%.*f",n,r);.  s
23c53 71 6c 69 74 65 33 41 74 6f 46 28 7a 42 75 66 2c  qlite3AtoF(zBuf,
23c54 20 26 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   &r);.  sqlite3_
23c55 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
23c56 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 23 65 6e  ntext, r);.}.#en
23c57 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  dif../*.** Alloc
23c58 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20  ate nByte bytes 
23c59 6f 66 20 73 70 61 63 65 20 75 73 69 6e 67 20 73  of space using s
23c5a 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
23c5b 20 49 66 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63   If the.** alloc
23c5c 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c  ation fails, cal
23c5d 6c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  l sqlite3_result
23c5e 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74  _error_nomem() t
23c5f 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20 74 68 65 20  o notify.** the 
23c60 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
23c61 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61  that malloc() ha
23c62 73 20 66 61 69 6c 65 64 20 61 6e 64 20 72 65 74  s failed and ret
23c63 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20  urn NULL..** If 
23c64 6e 42 79 74 65 20 69 73 20 6c 61 72 67 65 72 20  nByte is larger 
23c65 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d  than the maximum
23c66 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
23c67 6c 65 6e 67 74 68 2c 20 74 68 65 6e 0a 2a 2a 20  length, then.** 
23c68 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f  raise an SQLITE_
23c69 54 4f 4f 42 49 47 20 65 78 63 65 70 74 69 6f 6e  TOOBIG exception
23c6a 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c   and return NULL
23c6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
23c6c 20 2a 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28   *contextMalloc(
23c6d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
23c6e 2a 63 6f 6e 74 65 78 74 2c 20 69 36 34 20 6e 42  *context, i64 nB
23c6f 79 74 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b  yte){.  char *z;
23c70 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23c71 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
23c72 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
23c73 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  xt);.  assert( n
23c74 42 79 74 65 3e 30 20 29 3b 0a 20 20 74 65 73 74  Byte>0 );.  test
23c75 63 61 73 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d  case( nByte==db-
23c76 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
23c77 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a  IMIT_LENGTH] );.
23c78 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 79 74    testcase( nByt
23c79 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  e==db->aLimit[SQ
23c7a 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
23c7b 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 42  H]+1 );.  if( nB
23c7c 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  yte>db->aLimit[S
23c7d 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
23c7e 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  TH] ){.    sqlit
23c7f 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
23c80 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b  toobig(context);
23c81 0a 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 7d 65  .    z = 0;.  }e
23c82 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c  lse{.    z = sql
23c83 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29  ite3Malloc((int)
23c84 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20  nByte);.    if( 
23c85 21 7a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  !z ){.      sqli
23c86 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
23c87 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
23c88 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
23c89 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
23c8a 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
23c8b 66 20 74 68 65 20 75 70 70 65 72 28 29 20 61 6e  f the upper() an
23c8c 64 20 6c 6f 77 65 72 28 29 20 53 51 4c 20 66 75  d lower() SQL fu
23c8d 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74  nctions..*/.stat
23c8e 69 63 20 76 6f 69 64 20 75 70 70 65 72 46 75 6e  ic void upperFun
23c8f 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
23c90 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20  t *context, int 
23c91 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
23c92 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63  lue **argv){.  c
23c93 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74  har *z1;.  const
23c94 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74   char *z2;.  int
23c95 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44 5f   i, n;.  UNUSED_
23c96 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
23c97 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a 29 73  .  z2 = (char*)s
23c98 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
23c99 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20  t(argv[0]);.  n 
23c9a 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
23c9b 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
23c9c 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74    /* Verify that
23c9d 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79   the call to _by
23c9e 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 20 69  tes() does not i
23c9f 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 5f 74  nvalidate the _t
23ca0 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f  ext() pointer */
23ca1 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d 3d 28  .  assert( z2==(
23ca2 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
23ca3 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
23ca4 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b  ) );.  if( z2 ){
23ca5 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78  .    z1 = contex
23ca6 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
23ca7 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20   ((i64)n)+1);.  
23ca8 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 20    if( z1 ){.    
23ca9 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c    memcpy(z1, z2,
23caa 20 6e 2b 31 29 3b 0a 20 20 20 20 20 20 66 6f 72   n+1);.      for
23cab 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b  (i=0; z1[i]; i++
23cac 29 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d  ){.        z1[i]
23cad 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33   = (char)sqlite3
23cae 54 6f 75 70 70 65 72 28 7a 31 5b 69 5d 29 3b 0a  Toupper(z1[i]);.
23caf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
23cb0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
23cb1 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d  t(context, z1, -
23cb2 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  1, sqlite3_free)
23cb3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74  ;.    }.  }.}.st
23cb4 61 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72 46  atic void lowerF
23cb5 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
23cb6 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
23cb7 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
23cb8 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
23cb9 20 75 38 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74   u8 *z1;.  const
23cba 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74   char *z2;.  int
23cbb 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44 5f   i, n;.  UNUSED_
23cbc 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
23cbd 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a 29 73  .  z2 = (char*)s
23cbe 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
23cbf 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20  t(argv[0]);.  n 
23cc0 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
23cc1 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
23cc2 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74    /* Verify that
23cc3 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79   the call to _by
23cc4 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 20 69  tes() does not i
23cc5 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 5f 74  nvalidate the _t
23cc6 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f  ext() pointer */
23cc7 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d 3d 28  .  assert( z2==(
23cc8 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
23cc9 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
23cca 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b  ) );.  if( z2 ){
23ccb 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78  .    z1 = contex
23ccc 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
23ccd 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20   ((i64)n)+1);.  
23cce 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 20    if( z1 ){.    
23ccf 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c    memcpy(z1, z2,
23cd0 20 6e 2b 31 29 3b 0a 20 20 20 20 20 20 66 6f 72   n+1);.      for
23cd1 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b  (i=0; z1[i]; i++
23cd2 29 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d  ){.        z1[i]
23cd3 20 3d 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65   = sqlite3Tolowe
23cd4 72 28 7a 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20  r(z1[i]);.      
23cd5 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
23cd6 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
23cd7 65 78 74 2c 20 28 63 68 61 72 20 2a 29 7a 31 2c  ext, (char *)z1,
23cd8 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65   -1, sqlite3_fre
23cd9 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  e);.    }.  }.}.
23cda 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
23cdb 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 46 4e  ation of the IFN
23cdc 55 4c 4c 28 29 2c 20 4e 56 4c 28 29 2c 20 61 6e  ULL(), NVL(), an
23cdd 64 20 43 4f 41 4c 45 53 43 45 28 29 20 66 75 6e  d COALESCE() fun
23cde 63 74 69 6f 6e 73 2e 20 20 0a 2a 2a 20 41 6c 6c  ctions.  .** All
23cdf 20 74 68 72 65 65 20 64 6f 20 74 68 65 20 73 61   three do the sa
23ce0 6d 65 20 74 68 69 6e 67 2e 20 20 54 68 65 79 20  me thing.  They 
23ce1 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74  return the first
23ce2 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 61 72 67   non-NULL.** arg
23ce3 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ument..*/.static
23ce4 20 76 6f 69 64 20 69 66 6e 75 6c 6c 46 75 6e 63   void ifnullFunc
23ce5 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
23ce6 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
23ce7 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
23ce8 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
23ce9 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  .){.  int i;.  f
23cea 6f 72 28 69 3d 30 3b 20 69 3c 61 72 67 63 3b 20  or(i=0; i<argc; 
23ceb 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 51  i++){.    if( SQ
23cec 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74  LITE_NULL!=sqlit
23ced 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
23cee 67 76 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  gv[i]) ){.      
23cef 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
23cf0 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  alue(context, ar
23cf1 67 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 62 72  gv[i]);.      br
23cf2 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  eak;.    }.  }.}
23cf3 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
23cf4 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d  tation of random
23cf5 28 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61  ().  Return a ra
23cf6 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e 20 20 0a  ndom integer.  .
23cf7 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
23cf8 61 6e 64 6f 6d 46 75 6e 63 28 0a 20 20 73 71 6c  andomFunc(.  sql
23cf9 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
23cfa 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
23cfb 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
23cfc 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
23cfd 0a 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  .){.  sqlite_int
23cfe 36 34 20 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50  64 r;.  UNUSED_P
23cff 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
23d00 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
23d01 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
23d02 73 73 28 73 69 7a 65 6f 66 28 72 29 2c 20 26 72  ss(sizeof(r), &r
23d03 29 3b 0a 20 20 69 66 28 20 72 3c 30 20 29 7b 0a  );.  if( r<0 ){.
23d04 20 20 20 20 2f 2a 20 57 65 20 6e 65 65 64 20 74      /* We need t
23d05 6f 20 70 72 65 76 65 6e 74 20 61 20 72 61 6e 64  o prevent a rand
23d06 6f 6d 20 6e 75 6d 62 65 72 20 6f 66 20 30 78 38  om number of 0x8
23d07 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 20  000000000000000 
23d08 0a 20 20 20 20 2a 2a 20 28 6f 72 20 2d 39 32 32  .    ** (or -922
23d09 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38  3372036854775808
23d0a 29 20 73 69 6e 63 65 20 77 68 65 6e 20 79 6f 75  ) since when you
23d0b 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74 68 61   do abs() of tha
23d0c 74 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20  t.    ** number 
23d0d 6f 66 20 79 6f 75 20 67 65 74 20 74 68 65 20 73  of you get the s
23d0e 61 6d 65 20 76 61 6c 75 65 20 62 61 63 6b 20 61  ame value back a
23d0f 67 61 69 6e 2e 20 20 54 6f 20 64 6f 20 74 68 69  gain.  To do thi
23d10 73 0a 20 20 20 20 2a 2a 20 69 6e 20 61 20 77 61  s.    ** in a wa
23d11 79 20 74 68 61 74 20 69 73 20 74 65 73 74 61 62  y that is testab
23d12 6c 65 2c 20 6d 61 73 6b 20 74 68 65 20 73 69 67  le, mask the sig
23d13 6e 20 62 69 74 20 6f 66 66 20 6f 66 20 6e 65 67  n bit off of neg
23d14 61 74 69 76 65 0a 20 20 20 20 2a 2a 20 76 61 6c  ative.    ** val
23d15 75 65 73 2c 20 72 65 73 75 6c 74 69 6e 67 20 69  ues, resulting i
23d16 6e 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c  n a positive val
23d17 75 65 2e 20 20 54 68 65 6e 20 74 61 6b 65 20 74  ue.  Then take t
23d18 68 65 20 0a 20 20 20 20 2a 2a 20 32 73 20 63 6f  he .    ** 2s co
23d19 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 61 74  mplement of that
23d1a 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 2e   positive value.
23d1b 20 20 54 68 65 20 65 6e 64 20 72 65 73 75 6c 74    The end result
23d1c 20 63 61 6e 0a 20 20 20 20 2a 2a 20 74 68 65 72   can.    ** ther
23d1d 65 66 6f 72 65 20 62 65 20 6e 6f 20 6c 65 73 73  efore be no less
23d1e 20 74 68 61 6e 20 2d 39 32 32 33 33 37 32 30 33   than -922337203
23d1f 36 38 35 34 37 37 35 38 30 37 2e 0a 20 20 20 20  6854775807..    
23d20 2a 2f 0a 20 20 20 20 72 20 3d 20 2d 28 72 20 5e  */.    r = -(r ^
23d21 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   (((sqlite3_int6
23d22 34 29 31 29 3c 3c 36 33 29 29 3b 0a 20 20 7d 0a  4)1)<<63));.  }.
23d23 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23d24 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20  _int64(context, 
23d25 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  r);.}../*.** Imp
23d26 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72  lementation of r
23d27 61 6e 64 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52  andomblob(N).  R
23d28 65 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62  eturn a random b
23d29 6c 6f 62 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e  lob.** that is N
23d2a 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a   bytes long..*/.
23d2b 73 74 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64  static void rand
23d2c 6f 6d 42 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65  omBlob(.  sqlite
23d2d 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
23d2e 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
23d2f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
23d30 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
23d31 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  n;.  unsigned ch
23d32 61 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  ar *p;.  assert(
23d33 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e   argc==1 );.  UN
23d34 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
23d35 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  rgc);.  n = sqli
23d36 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
23d37 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c  gv[0]);.  if( n<
23d38 31 20 29 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a  1 ){.    n = 1;.
23d39 20 20 7d 0a 20 20 70 20 3d 20 63 6f 6e 74 65 78    }.  p = contex
23d3a 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
23d3b 20 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a   n);.  if( p ){.
23d3c 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64      sqlite3_rand
23d3d 6f 6d 6e 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20  omness(n, p);.  
23d3e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23d3f 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28  _blob(context, (
23d40 63 68 61 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69  char*)p, n, sqli
23d41 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d  te3_free);.  }.}
23d42 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
23d43 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61  tation of the la
23d44 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
23d45 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
23d46 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76   The return.** v
23d47 61 6c 75 65 20 69 73 20 74 68 65 20 73 61 6d 65  alue is the same
23d48 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f   as the sqlite3_
23d49 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
23d4a 64 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  d() API function
23d4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
23d4c 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77   last_insert_row
23d4d 69 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  id(.  sqlite3_co
23d4e 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
23d4f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20  .  int NotUsed, 
23d50 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
23d51 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20   **NotUsed2.){. 
23d52 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
23d53 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
23d54 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
23d55 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
23d56 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
23d57 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c  NotUsed2);.  sql
23d58 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
23d59 34 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  4(context, sqlit
23d5a 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
23d5b 6f 77 69 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a  owid(db));.}../*
23d5c 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
23d5d 6f 6e 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65  on of the change
23d5e 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  s() SQL function
23d5f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
23d60 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 73 61  lue is the.** sa
23d61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74 65  me as the sqlite
23d62 33 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49 20  3_changes() API 
23d63 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
23d64 74 69 63 20 76 6f 69 64 20 63 68 61 6e 67 65 73  tic void changes
23d65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
23d66 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
23d67 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
23d68 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
23d69 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c  otUsed2.){.  sql
23d6a 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
23d6b 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
23d6c 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
23d6d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
23d6e 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
23d6f 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
23d70 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
23d71 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 68 61  ext, sqlite3_cha
23d72 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a  nges(db));.}../*
23d73 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
23d74 6f 6e 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f  on of the total_
23d75 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75  changes() SQL fu
23d76 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74  nction.  The ret
23d77 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20  urn value is.** 
23d78 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
23d79 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
23d7a 61 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63  anges() API func
23d7b 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
23d7c 76 6f 69 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67  void total_chang
23d7d 65 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  es(.  sqlite3_co
23d7e 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
23d7f 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
23d80 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
23d81 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73  *NotUsed2.){.  s
23d82 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
23d83 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
23d84 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
23d85 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
23d86 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
23d87 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74  tUsed2);.  sqlit
23d88 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f  e3_result_int(co
23d89 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 74  ntext, sqlite3_t
23d8a 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 64 62 29  otal_changes(db)
23d8b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74  );.}../*.** A st
23d8c 72 75 63 74 75 72 65 20 64 65 66 69 6e 69 6e 67  ructure defining
23d8d 20 68 6f 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d   how to do GLOB-
23d8e 73 74 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e  style comparison
23d8f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 63 6f 6d  s..*/.struct com
23d90 70 61 72 65 49 6e 66 6f 20 7b 0a 20 20 75 38 20  pareInfo {.  u8 
23d91 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d  matchAll;.  u8 m
23d92 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61  atchOne;.  u8 ma
23d93 74 63 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43  tchSet;.  u8 noC
23d94 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  ase;.};../*.** F
23d95 6f 72 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42  or LIKE and GLOB
23d96 20 6d 61 74 63 68 69 6e 67 20 6f 6e 20 45 42 43   matching on EBC
23d97 44 49 43 20 6d 61 63 68 69 6e 65 73 2c 20 61 73  DIC machines, as
23d98 73 75 6d 65 20 74 68 61 74 20 65 76 65 72 79 0a  sume that every.
23d99 2a 2a 20 63 68 61 72 61 63 74 65 72 20 69 73 20  ** character is 
23d9a 65 78 61 63 74 6c 79 20 6f 6e 65 20 62 79 74 65  exactly one byte
23d9b 20 69 6e 20 73 69 7a 65 2e 20 20 41 6c 73 6f 2c   in size.  Also,
23d9c 20 61 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20   all characters 
23d9d 61 72 65 0a 2a 2a 20 61 62 6c 65 20 74 6f 20 70  are.** able to p
23d9e 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 75 70  articipate in up
23d9f 70 65 72 2d 63 61 73 65 2d 74 6f 2d 6c 6f 77 65  per-case-to-lowe
23da0 72 2d 63 61 73 65 20 6d 61 70 70 69 6e 67 73 20  r-case mappings 
23da1 69 6e 20 45 42 43 44 49 43 0a 2a 2a 20 77 68 65  in EBCDIC.** whe
23da2 72 65 61 73 20 6f 6e 6c 79 20 63 68 61 72 61 63  reas only charac
23da3 74 65 72 73 20 6c 65 73 73 20 74 68 61 6e 20 30  ters less than 0
23da4 78 38 30 20 64 6f 20 69 6e 20 41 53 43 49 49 2e  x80 do in ASCII.
23da5 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
23da6 53 51 4c 49 54 45 5f 45 42 43 44 49 43 29 0a 23  SQLITE_EBCDIC).#
23da7 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55   define sqlite3U
23da8 74 66 38 52 65 61 64 28 41 2c 43 29 20 20 20 20  tf8Read(A,C)    
23da9 28 2a 28 41 2b 2b 29 29 0a 23 20 64 65 66 69 6e  (*(A++)).# defin
23daa 65 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77  e GlogUpperToLow
23dab 65 72 28 41 29 20 20 20 20 20 41 20 3d 20 73 71  er(A)     A = sq
23dac 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
23dad 72 5b 41 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  r[A].#else.# def
23dae 69 6e 65 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c  ine GlogUpperToL
23daf 6f 77 65 72 28 41 29 20 20 20 20 20 69 66 28 20  ower(A)     if( 
23db0 41 3c 30 78 38 30 20 29 7b 20 41 20 3d 20 73 71  A<0x80 ){ A = sq
23db1 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
23db2 72 5b 41 5d 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a  r[A]; }.#endif..
23db3 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72  static const str
23db4 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
23db5 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b 20 27 2a 27  globInfo = { '*'
23db6 2c 20 27 3f 27 2c 20 27 5b 27 2c 20 30 20 7d 3b  , '?', '[', 0 };
23db7 0a 2f 2a 20 54 68 65 20 63 6f 72 72 65 63 74 20  ./* The correct 
23db8 53 51 4c 2d 39 32 20 62 65 68 61 76 69 6f 72 20  SQL-92 behavior 
23db9 69 73 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20  is for the LIKE 
23dba 6f 70 65 72 61 74 6f 72 20 74 6f 20 69 67 6e 6f  operator to igno
23dbb 72 65 0a 2a 2a 20 63 61 73 65 2e 20 20 54 68 75  re.** case.  Thu
23dbc 73 20 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20  s  'a' LIKE 'A' 
23dbd 77 6f 75 6c 64 20 62 65 20 74 72 75 65 2e 20 2a  would be true. *
23dbe 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
23dbf 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
23dc0 6f 20 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 20 3d  o likeInfoNorm =
23dc1 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 30   { '%', '_',   0
23dc2 2c 20 31 20 7d 3b 0a 2f 2a 20 49 66 20 53 51 4c  , 1 };./* If SQL
23dc3 49 54 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 49  ITE_CASE_SENSITI
23dc4 56 45 5f 4c 49 4b 45 20 69 73 20 64 65 66 69 6e  VE_LIKE is defin
23dc5 65 64 2c 20 74 68 65 6e 20 74 68 65 20 4c 49 4b  ed, then the LIK
23dc6 45 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73  E operator.** is
23dc7 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 20   case sensitive 
23dc8 63 61 75 73 69 6e 67 20 27 61 27 20 4c 49 4b 45  causing 'a' LIKE
23dc9 20 27 41 27 20 74 6f 20 62 65 20 66 61 6c 73 65   'A' to be false
23dca 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
23dcb 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49   struct compareI
23dcc 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 41 6c 74 20  nfo likeInfoAlt 
23dcd 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 20  = { '%', '_',   
23dce 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  0, 0 };../*.** C
23dcf 6f 6d 70 61 72 65 20 74 77 6f 20 55 54 46 2d 38  ompare two UTF-8
23dd0 20 73 74 72 69 6e 67 73 20 66 6f 72 20 65 71 75   strings for equ
23dd1 61 6c 69 74 79 20 77 68 65 72 65 20 74 68 65 20  ality where the 
23dd2 66 69 72 73 74 20 73 74 72 69 6e 67 20 63 61 6e  first string can
23dd3 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  .** potentially 
23dd4 62 65 20 61 20 22 67 6c 6f 62 22 20 65 78 70 72  be a "glob" expr
23dd5 65 73 73 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20  ession.  Return 
23dd6 74 72 75 65 20 28 31 29 20 69 66 20 74 68 65 79  true (1) if they
23dd7 0a 2a 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65  .** are the same
23dd8 20 61 6e 64 20 66 61 6c 73 65 20 28 30 29 20 69   and false (0) i
23dd9 66 20 74 68 65 79 20 61 72 65 20 64 69 66 66 65  f they are diffe
23dda 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62  rent..**.** Glob
23ddb 62 69 6e 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a  bing rules:.**.*
23ddc 2a 20 20 20 20 20 20 27 2a 27 20 20 20 20 20 20  *      '*'      
23ddd 20 4d 61 74 63 68 65 73 20 61 6e 79 20 73 65 71   Matches any seq
23dde 75 65 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72  uence of zero or
23ddf 20 6d 6f 72 65 20 63 68 61 72 61 63 74 65 72 73   more characters
23de0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 3f 27  ..**.**      '?'
23de1 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 65         Matches e
23de2 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 61 72 61  xactly one chara
23de3 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  cter..**.**     
23de4 5b 2e 2e 2e 5d 20 20 20 20 20 20 4d 61 74 63 68  [...]      Match
23de5 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  es one character
23de6 20 66 72 6f 6d 20 74 68 65 20 65 6e 63 6c 6f 73   from the enclos
23de7 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20  ed list of.**   
23de8 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61               cha
23de9 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20  racters..**.**  
23dea 20 20 20 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d 61     [^...]     Ma
23deb 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61 63  tches one charac
23dec 74 65 72 20 6e 6f 74 20 69 6e 20 74 68 65 20 65  ter not in the e
23ded 6e 63 6c 6f 73 65 64 20 6c 69 73 74 2e 0a 2a 2a  nclosed list..**
23dee 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 5b 2e 2e  .** With the [..
23def 2e 5d 20 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d 61  .] and [^...] ma
23df0 74 63 68 69 6e 67 2c 20 61 20 27 5d 27 20 63 68  tching, a ']' ch
23df1 61 72 61 63 74 65 72 20 63 61 6e 20 62 65 20 69  aracter can be i
23df2 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68  ncluded.** in th
23df3 65 20 6c 69 73 74 20 62 79 20 6d 61 6b 69 6e 67  e list by making
23df4 20 69 74 20 74 68 65 20 66 69 72 73 74 20 63 68   it the first ch
23df5 61 72 61 63 74 65 72 20 61 66 74 65 72 20 27 5b  aracter after '[
23df6 27 20 6f 72 20 27 5e 27 2e 20 20 41 0a 2a 2a 20  ' or '^'.  A.** 
23df7 72 61 6e 67 65 20 6f 66 20 63 68 61 72 61 63 74  range of charact
23df8 65 72 73 20 63 61 6e 20 62 65 20 73 70 65 63 69  ers can be speci
23df9 66 69 65 64 20 75 73 69 6e 67 20 27 2d 27 2e 20  fied using '-'. 
23dfa 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b 61   Example:.** "[a
23dfb 2d 7a 5d 22 20 6d 61 74 63 68 65 73 20 61 6e 79  -z]" matches any
23dfc 20 73 69 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61   single lower-ca
23dfd 73 65 20 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d  se letter.  To m
23dfe 61 74 63 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65  atch a '-', make
23dff 0a 2a 2a 20 69 74 20 74 68 65 20 6c 61 73 74 20  .** it the last 
23e00 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65  character in the
23e01 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   list..**.** Thi
23e02 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75  s routine is usu
23e03 61 6c 6c 79 20 71 75 69 63 6b 2c 20 62 75 74 20  ally quick, but 
23e04 63 61 6e 20 62 65 20 4e 2a 2a 32 20 69 6e 20 74  can be N**2 in t
23e05 68 65 20 77 6f 72 73 74 20 63 61 73 65 2e 0a 2a  he worst case..*
23e06 2a 0a 2a 2a 20 48 69 6e 74 73 3a 20 74 6f 20 6d  *.** Hints: to m
23e07 61 74 63 68 20 27 2a 27 20 6f 72 20 27 3f 27 2c  atch '*' or '?',
23e08 20 70 75 74 20 74 68 65 6d 20 69 6e 20 22 5b 5d   put them in "[]
23e09 22 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a  ".  Like this:.*
23e0a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 62 63  *.**         abc
23e0b 5b 2a 5d 78 79 7a 20 20 20 20 20 20 20 20 4d 61  [*]xyz        Ma
23e0c 74 63 68 65 73 20 22 61 62 63 2a 78 79 7a 22 20  tches "abc*xyz" 
23e0d 6f 6e 6c 79 0a 2a 2f 0a 73 74 61 74 69 63 20 69  only.*/.static i
23e0e 6e 74 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  nt patternCompar
23e0f 65 28 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  e(.  const u8 *z
23e10 50 61 74 74 65 72 6e 2c 20 20 20 20 20 20 20 20  Pattern,        
23e11 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67 6c 6f        /* The glo
23e12 62 20 70 61 74 74 65 72 6e 20 2a 2f 0a 20 20 63  b pattern */.  c
23e13 6f 6e 73 74 20 75 38 20 2a 7a 53 74 72 69 6e 67  onst u8 *zString
23e14 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
23e15 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 74 6f  /* The string to
23e16 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74   compare against
23e17 20 74 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 63   the glob */.  c
23e18 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70  onst struct comp
23e19 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20  areInfo *pInfo, 
23e1a 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
23e1b 62 6f 75 74 20 68 6f 77 20 74 6f 20 64 6f 20 74  bout how to do t
23e1c 68 65 20 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20  he compare */.  
23e1d 63 6f 6e 73 74 20 69 6e 74 20 65 73 63 20 20 20  const int esc   
23e1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23e1f 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63   /* The escape c
23e20 68 61 72 61 63 74 65 72 20 2a 2f 0a 29 7b 0a 20  haracter */.){. 
23e21 20 69 6e 74 20 63 2c 20 63 32 3b 0a 20 20 69 6e   int c, c2;.  in
23e22 74 20 69 6e 76 65 72 74 3b 0a 20 20 69 6e 74 20  t invert;.  int 
23e23 73 65 65 6e 3b 0a 20 20 75 38 20 6d 61 74 63 68  seen;.  u8 match
23e24 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74  One = pInfo->mat
23e25 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63  chOne;.  u8 matc
23e26 68 41 6c 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61  hAll = pInfo->ma
23e27 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74  tchAll;.  u8 mat
23e28 63 68 53 65 74 20 3d 20 70 49 6e 66 6f 2d 3e 6d  chSet = pInfo->m
23e29 61 74 63 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f  atchSet;.  u8 no
23e2a 43 61 73 65 20 3d 20 70 49 6e 66 6f 2d 3e 6e 6f  Case = pInfo->no
23e2b 43 61 73 65 3b 20 0a 20 20 69 6e 74 20 70 72 65  Case; .  int pre
23e2c 76 45 73 63 61 70 65 20 3d 20 30 3b 20 20 20 20  vEscape = 0;    
23e2d 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
23e2e 70 72 65 76 69 6f 75 73 20 63 68 61 72 61 63 74  previous charact
23e2f 65 72 20 77 61 73 20 27 65 73 63 61 70 65 27 20  er was 'escape' 
23e30 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 28 63 20  */..  while( (c 
23e31 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
23e32 64 28 7a 50 61 74 74 65 72 6e 2c 26 7a 50 61 74  d(zPattern,&zPat
23e33 74 65 72 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20  tern))!=0 ){.   
23e34 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65   if( !prevEscape
23e35 20 26 26 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20   && c==matchAll 
23e36 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ){.      while( 
23e37 28 63 3d 73 71 6c 69 74 65 33 55 74 66 38 52 65  (c=sqlite3Utf8Re
23e38 61 64 28 7a 50 61 74 74 65 72 6e 2c 26 7a 50 61  ad(zPattern,&zPa
23e39 74 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 68  ttern)) == match
23e3a 41 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20  All.            
23e3b 20 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 68     || c == match
23e3c 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  One ){.        i
23e3d 66 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26  f( c==matchOne &
23e3e 26 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  & sqlite3Utf8Rea
23e3f 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72  d(zString, &zStr
23e40 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ing)==0 ){.     
23e41 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
23e42 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
23e43 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20  .      if( c==0 
23e44 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
23e45 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 1;.      }else
23e46 20 69 66 28 20 63 3d 3d 65 73 63 20 29 7b 0a 20   if( c==esc ){. 
23e47 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74         c = sqlit
23e48 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
23e49 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b  ern, &zPattern);
23e4a 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
23e4b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
23e4c 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20  eturn 0;.       
23e4d 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69   }.      }else i
23e4e 66 28 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29  f( c==matchSet )
23e4f 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
23e50 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 20  ( esc==0 );     
23e51 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 47      /* This is G
23e52 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f  LOB, not LIKE */
23e53 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
23e54 20 6d 61 74 63 68 53 65 74 3c 30 78 38 30 20 29   matchSet<0x80 )
23e55 3b 20 20 2f 2a 20 27 5b 27 20 69 73 20 61 20 73  ;  /* '[' is a s
23e56 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61  ingle-byte chara
23e57 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  cter */.        
23e58 77 68 69 6c 65 28 20 2a 7a 53 74 72 69 6e 67 20  while( *zString 
23e59 26 26 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  && patternCompar
23e5a 65 28 26 7a 50 61 74 74 65 72 6e 5b 2d 31 5d 2c  e(&zPattern[-1],
23e5b 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73  zString,pInfo,es
23e5c 63 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  c)==0 ){.       
23e5d 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55     SQLITE_SKIP_U
23e5e 54 46 38 28 7a 53 74 72 69 6e 67 29 3b 0a 20 20  TF8(zString);.  
23e5f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
23e60 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 21  return *zString!
23e61 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  =0;.      }.    
23e62 20 20 77 68 69 6c 65 28 20 28 63 32 20 3d 20 73    while( (c2 = s
23e63 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
23e64 53 74 72 69 6e 67 2c 26 7a 53 74 72 69 6e 67 29  String,&zString)
23e65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
23e66 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20  if( noCase ){.  
23e67 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
23e68 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20  rToLower(c2);.  
23e69 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
23e6a 72 54 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20  rToLower(c);.   
23e6b 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32         while( c2
23e6c 20 21 3d 20 30 20 26 26 20 63 32 20 21 3d 20 63   != 0 && c2 != c
23e6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
23e6e 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
23e6f 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a  Read(zString, &z
23e70 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20  String);.       
23e71 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f       GlogUpperTo
23e72 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20  Lower(c2);.     
23e73 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
23e74 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
23e75 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26  while( c2 != 0 &
23e76 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20  & c2 != c ){.   
23e77 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
23e78 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
23e79 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29  tring, &zString)
23e7a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
23e7b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
23e7c 69 66 28 20 63 32 3d 3d 30 20 29 20 72 65 74 75  if( c2==0 ) retu
23e7d 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66  rn 0;.        if
23e7e 28 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65  ( patternCompare
23e7f 28 7a 50 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e  (zPattern,zStrin
23e80 67 2c 70 49 6e 66 6f 2c 65 73 63 29 20 29 20 72  g,pInfo,esc) ) r
23e81 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
23e82 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
23e83 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21  .    }else if( !
23e84 70 72 65 76 45 73 63 61 70 65 20 26 26 20 63 3d  prevEscape && c=
23e85 3d 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20  =matchOne ){.   
23e86 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 74     if( sqlite3Ut
23e87 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20  f8Read(zString, 
23e88 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a  &zString)==0 ){.
23e89 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
23e8a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
23e8b 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68  lse if( c==match
23e8c 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Set ){.      int
23e8d 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20   prior_c = 0;.  
23e8e 20 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d      assert( esc=
23e8f 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68 69 73  =0 );    /* This
23e90 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66 6f 72   only occurs for
23e91 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20   GLOB, not LIKE 
23e92 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20 3d 20  */.      seen = 
23e93 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72 74 20  0;.      invert 
23e94 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d 20 73  = 0;.      c = s
23e95 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
23e96 53 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67  String, &zString
23e97 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  );.      if( c==
23e98 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
23e99 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
23e9a 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
23e9b 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  n, &zPattern);. 
23e9c 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5e 27       if( c2=='^'
23e9d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 65   ){.        inve
23e9e 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  rt = 1;.        
23e9f 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
23ea0 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26  Read(zPattern, &
23ea1 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20  zPattern);.     
23ea2 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d   }.      if( c2=
23ea3 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20  =']' ){.        
23ea4 69 66 28 20 63 3d 3d 27 5d 27 20 29 20 73 65 65  if( c==']' ) see
23ea5 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63  n = 1;.        c
23ea6 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
23ea7 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a  ead(zPattern, &z
23ea8 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20  Pattern);.      
23ea9 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 63  }.      while( c
23eaa 32 20 26 26 20 63 32 21 3d 27 5d 27 20 29 7b 0a  2 && c2!=']' ){.
23eab 20 20 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d          if( c2==
23eac 27 2d 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b  '-' && zPattern[
23ead 30 5d 21 3d 27 5d 27 20 26 26 20 7a 50 61 74 74  0]!=']' && zPatt
23eae 65 72 6e 5b 30 5d 21 3d 30 20 26 26 20 70 72 69  ern[0]!=0 && pri
23eaf 6f 72 5f 63 3e 30 20 29 7b 0a 20 20 20 20 20 20  or_c>0 ){.      
23eb0 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
23eb1 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
23eb2 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  n, &zPattern);. 
23eb3 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3e 3d           if( c>=
23eb4 70 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63 32  prior_c && c<=c2
23eb5 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20   ) seen = 1;.   
23eb6 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d         prior_c =
23eb7 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   0;.        }els
23eb8 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  e{.          if(
23eb9 20 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20 20   c==c2 ){.      
23eba 20 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a        seen = 1;.
23ebb 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
23ebc 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20        prior_c = 
23ebd 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  c2;.        }.  
23ebe 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
23ebf 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
23ec0 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b  ern, &zPattern);
23ec1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
23ec2 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 73 65 65  f( c2==0 || (see
23ec3 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d 30 20 29  n ^ invert)==0 )
23ec4 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
23ec5 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
23ec6 7d 65 6c 73 65 20 69 66 28 20 65 73 63 3d 3d 63  }else if( esc==c
23ec7 20 26 26 20 21 70 72 65 76 45 73 63 61 70 65 20   && !prevEscape 
23ec8 29 7b 0a 20 20 20 20 20 20 70 72 65 76 45 73 63  ){.      prevEsc
23ec9 61 70 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  ape = 1;.    }el
23eca 73 65 7b 0a 20 20 20 20 20 20 63 32 20 3d 20 73  se{.      c2 = s
23ecb 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
23ecc 53 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67  String, &zString
23ecd 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43  );.      if( noC
23ece 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 47  ase ){.        G
23ecf 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
23ed0 63 29 3b 0a 20 20 20 20 20 20 20 20 47 6c 6f 67  c);.        Glog
23ed1 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29  UpperToLower(c2)
23ed2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
23ed3 69 66 28 20 63 21 3d 63 32 20 29 7b 0a 20 20 20  if( c!=c2 ){.   
23ed4 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
23ed5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65       }.      pre
23ed6 76 45 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20  vEscape = 0;.   
23ed7 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
23ed8 2a 7a 53 74 72 69 6e 67 3d 3d 30 3b 0a 7d 0a 0a  *zString==0;.}..
23ed9 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20  /*.** Count the 
23eda 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
23edb 74 68 61 74 20 74 68 65 20 4c 49 4b 45 20 6f 70  that the LIKE op
23edc 65 72 61 74 6f 72 20 28 6f 72 20 47 4c 4f 42 20  erator (or GLOB 
23edd 77 68 69 63 68 20 69 73 0a 2a 2a 20 6a 75 73 74  which is.** just
23ede 20 61 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20   a variation of 
23edf 4c 49 4b 45 29 20 67 65 74 73 20 63 61 6c 6c 65  LIKE) gets calle
23ee0 64 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  d.  This is used
23ee1 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20   for testing.** 
23ee2 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  only..*/.#ifdef 
23ee3 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
23ee4 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
23ee5 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 20 3d 20  e3_like_count = 
23ee6 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  0;.#endif.../*.*
23ee7 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
23ee8 20 6f 66 20 74 68 65 20 6c 69 6b 65 28 29 20 53   of the like() S
23ee9 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
23eea 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c  is function impl
23eeb 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75  ements.** the bu
23eec 69 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72  ild-in LIKE oper
23eed 61 74 6f 72 2e 20 20 54 68 65 20 66 69 72 73 74  ator.  The first
23eee 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
23eef 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65   function is the
23ef0 0a 2a 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 20  .** pattern and 
23ef1 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
23ef2 65 6e 74 20 69 73 20 74 68 65 20 73 74 72 69 6e  ent is the strin
23ef3 67 2e 20 20 53 6f 2c 20 74 68 65 20 53 51 4c 20  g.  So, the SQL 
23ef4 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  statements:.**.*
23ef5 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42  *       A LIKE B
23ef6 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d  .**.** is implem
23ef7 65 6e 74 65 64 20 61 73 20 6c 69 6b 65 28 42 2c  ented as like(B,
23ef8 41 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  A)..**.** This s
23ef9 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 28 77 69  ame function (wi
23efa 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 63  th a different c
23efb 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63  ompareInfo struc
23efc 74 75 72 65 29 20 63 6f 6d 70 75 74 65 73 0a 2a  ture) computes.*
23efd 2a 20 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 61  * the GLOB opera
23efe 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tor..*/.static v
23eff 6f 69 64 20 6c 69 6b 65 46 75 6e 63 28 0a 20 20  oid likeFunc(.  
23f00 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
23f01 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74  *context, .  int
23f02 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65   argc, .  sqlite
23f03 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
23f04 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
23f05 65 64 20 63 68 61 72 20 2a 7a 41 2c 20 2a 7a 42  ed char *zA, *zB
23f06 3b 0a 20 20 69 6e 74 20 65 73 63 61 70 65 20 3d  ;.  int escape =
23f07 20 30 3b 0a 20 20 69 6e 74 20 6e 50 61 74 3b 0a   0;.  int nPat;.
23f08 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23f09 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
23f0a 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
23f0b 74 29 3b 0a 0a 20 20 7a 42 20 3d 20 73 71 6c 69  t);..  zB = sqli
23f0c 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
23f0d 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 41 20 3d 20  rgv[0]);.  zA = 
23f0e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
23f0f 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20  xt(argv[1]);..  
23f10 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e  /* Limit the len
23f11 67 74 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20  gth of the LIKE 
23f12 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20  or GLOB pattern 
23f13 74 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d  to avoid problem
23f14 73 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72  s.  ** of deep r
23f15 65 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e  ecursion and N*N
23f16 20 62 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74   behavior in pat
23f17 74 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20  ternCompare().. 
23f18 20 2a 2f 0a 20 20 6e 50 61 74 20 3d 20 73 71 6c   */.  nPat = sql
23f19 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
23f1a 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73  (argv[0]);.  tes
23f1b 74 63 61 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d  tcase( nPat==db-
23f1c 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
23f1d 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52  IMIT_LIKE_PATTER
23f1e 4e 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74  N_LENGTH] );.  t
23f1f 65 73 74 63 61 73 65 28 20 6e 50 61 74 3d 3d 64  estcase( nPat==d
23f20 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
23f21 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54  _LIMIT_LIKE_PATT
23f22 45 52 4e 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b  ERN_LENGTH]+1 );
23f23 0a 20 20 69 66 28 20 6e 50 61 74 20 3e 20 64 62  .  if( nPat > db
23f24 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
23f25 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45  LIMIT_LIKE_PATTE
23f26 52 4e 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  RN_LENGTH] ){.  
23f27 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23f28 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
23f29 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61  "LIKE or GLOB pa
23f2a 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65  ttern too comple
23f2b 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74  x", -1);.    ret
23f2c 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
23f2d 74 28 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76  t( zB==sqlite3_v
23f2e 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
23f2f 5d 29 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69  ]) );  /* Encodi
23f30 6e 67 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67  ng did not chang
23f31 65 20 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63  e */..  if( argc
23f32 3d 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==3 ){.    /* Th
23f33 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74  e escape charact
23f34 65 72 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63  er string must c
23f35 6f 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67  onsist of a sing
23f36 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  le UTF-8 charact
23f37 65 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72  er..    ** Other
23f38 77 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20  wise, return an 
23f39 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  error..    */.  
23f3a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
23f3b 20 63 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71   char *zEsc = sq
23f3c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
23f3d 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69  (argv[2]);.    i
23f3e 66 28 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74  f( zEsc==0 ) ret
23f3f 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  urn;.    if( sql
23f40 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
23f41 28 63 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29  (char*)zEsc, -1)
23f42 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=1 ){.      sql
23f43 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
23f44 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20  r(context, .    
23f45 20 20 20 20 20 20 22 45 53 43 41 50 45 20 65 78        "ESCAPE ex
23f46 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65  pression must be
23f47 20 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63   a single charac
23f48 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20  ter", -1);.     
23f49 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
23f4a 20 20 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69     escape = sqli
23f4b 74 65 33 55 74 66 38 52 65 61 64 28 7a 45 73 63  te3Utf8Read(zEsc
23f4c 2c 20 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20 20  , &zEsc);.  }.  
23f4d 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a  if( zA && zB ){.
23f4e 20 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61      struct compa
23f4f 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20  reInfo *pInfo = 
23f50 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
23f51 61 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66 64  a(context);.#ifd
23f52 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
23f53 20 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f     sqlite3_like_
23f54 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
23f55 20 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33      .    sqlite3
23f56 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
23f57 65 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d 70  ext, patternComp
23f58 61 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e 66  are(zB, zA, pInf
23f59 6f 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20 7d  o, escape));.  }
23f5a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
23f5b 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
23f5c 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e 63  NULLIF(x,y) func
23f5d 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c  tion.  The resul
23f5e 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a  t is the first.*
23f5f 2a 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 68  * argument if th
23f60 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  e arguments are 
23f61 64 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65 20  different.  The 
23f62 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 69  result is NULL i
23f63 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  f the.** argumen
23f64 74 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f 20  ts are equal to 
23f65 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 73  each other..*/.s
23f66 74 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c 69  tatic void nulli
23f67 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  fFunc(.  sqlite3
23f68 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23f69 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
23f6a 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
23f6b 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43 6f  e **argv.){.  Co
23f6c 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73  llSeq *pColl = s
23f6d 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c  qlite3GetFuncCol
23f6e 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20  lSeq(context);. 
23f6f 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
23f70 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66  R(NotUsed);.  if
23f71 28 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70  ( sqlite3MemComp
23f72 61 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72 67  are(argv[0], arg
23f73 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 20  v[1], pColl)!=0 
23f74 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
23f75 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74  esult_value(cont
23f76 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20  ext, argv[0]);. 
23f77 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
23f78 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
23f79 65 20 56 45 52 53 49 4f 4e 28 2a 29 20 66 75 6e  e VERSION(*) fun
23f7a 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75  ction.  The resu
23f7b 6c 74 20 69 73 20 74 68 65 20 76 65 72 73 69 6f  lt is the versio
23f7c 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 69  n.** of the SQLi
23f7d 74 65 20 6c 69 62 72 61 72 79 20 74 68 61 74 20  te library that 
23f7e 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73  is running..*/.s
23f7f 74 61 74 69 63 20 76 6f 69 64 20 76 65 72 73 69  tatic void versi
23f80 6f 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  onFunc(.  sqlite
23f81 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
23f82 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
23f83 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
23f84 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
23f85 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
23f86 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
23f87 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74  tUsed2);.  sqlit
23f88 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
23f89 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
23f8a 76 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53 51 4c  version, -1, SQL
23f8b 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a  ITE_STATIC);.}..
23f8c 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63 6f 6e  /* Array for con
23f8d 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68 61 6c  verting from hal
23f8e 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c 65 73  f-bytes (nybbles
23f8f 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68 65 78  ) into ASCII hex
23f90 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f 0a 73  .** digits. */.s
23f91 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
23f92 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d 20 7b   hexdigits[] = {
23f93 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27 32 27  .  '0', '1', '2'
23f94 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35 27 2c  , '3', '4', '5',
23f95 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27 38 27   '6', '7',.  '8'
23f96 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42 27 2c  , '9', 'A', 'B',
23f97 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c 20   'C', 'D', 'E', 
23f98 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45  'F' .};../*.** E
23f99 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 54 68  XPERIMENTAL - Th
23f9a 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f 66 66  is is not an off
23f9b 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 2e 20  icial function. 
23f9c 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d   The interface m
23f9d 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20 20 54  ay.** change.  T
23f9e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
23f9f 20 64 69 73 61 70 70 65 61 72 2e 20 20 44 6f 20   disappear.  Do 
23fa0 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65 20 74  not write code t
23fa1 68 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f  hat depends.** o
23fa2 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  n this function.
23fa3 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  .**.** Implement
23fa4 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51 55 4f  ation of the QUO
23fa5 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  TE() function.  
23fa6 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 61  This function ta
23fa7 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  kes a single.** 
23fa8 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68  argument.  If th
23fa9 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 75  e argument is nu
23faa 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74 75 72  meric, the retur
23fab 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73  n value is the s
23fac 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20 61 72  ame as.** the ar
23fad 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20  gument.  If the 
23fae 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c  argument is NULL
23faf 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
23fb0 75 65 20 69 73 20 74 68 65 20 73 74 72 69 6e 67  ue is the string
23fb1 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f 74 68  .** "NULL".  Oth
23fb2 65 72 77 69 73 65 2c 20 74 68 65 20 61 72 67 75  erwise, the argu
23fb3 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73 65 64  ment is enclosed
23fb4 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f 74 65   in single quote
23fb5 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67 6c 65  s with.** single
23fb6 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73 2e 0a  -quote escapes..
23fb7 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71  */.static void q
23fb8 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33  uoteFunc(sqlite3
23fb9 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23fba 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
23fbb 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
23fbc 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72  v){.  assert( ar
23fbd 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
23fbe 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
23fbf 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c  );.  switch( sql
23fc0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
23fc1 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20  argv[0]) ){.    
23fc2 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
23fc3 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 53 51  GER:.    case SQ
23fc4 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20  LITE_FLOAT: {.  
23fc5 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
23fc6 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74  lt_value(context
23fc7 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20  , argv[0]);.    
23fc8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
23fc9 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42     case SQLITE_B
23fca 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63 68 61  LOB: {.      cha
23fcb 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20  r *zText = 0;.  
23fcc 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
23fcd 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f  zBlob = sqlite3_
23fce 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
23fcf 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  0]);.      int n
23fd0 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76  Blob = sqlite3_v
23fd1 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
23fd2 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0]);.      asser
23fd3 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69 74 65  t( zBlob==sqlite
23fd4 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
23fd5 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f 20 65  v[0]) ); /* No e
23fd6 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a  ncoding change *
23fd7 2f 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20  /.      zText = 
23fd8 28 63 68 61 72 20 2a 29 63 6f 6e 74 65 78 74 4d  (char *)contextM
23fd9 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28  alloc(context, (
23fda 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b 34 29  2*(i64)nBlob)+4)
23fdb 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a 54 65  ; .      if( zTe
23fdc 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  xt ){.        in
23fdd 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t i;.        for
23fde 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b 20 69  (i=0; i<nBlob; i
23fdf 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ++){.          z
23fe0 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20 3d 20  Text[(i*2)+2] = 
23fe1 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62  hexdigits[(zBlob
23fe2 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b 0a 20  [i]>>4)&0x0F];. 
23fe3 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28           zText[(
23fe4 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64 69 67  i*2)+3] = hexdig
23fe5 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29 26 30  its[(zBlob[i])&0
23fe6 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  x0F];.        }.
23fe7 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 6e          zText[(n
23fe8 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 5c 27  Blob*2)+2] = '\'
23fe9 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74  ';.        zText
23fea 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 3d 20  [(nBlob*2)+3] = 
23feb 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7a 54  '\0';.        zT
23fec 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a 20 20  ext[0] = 'X';.  
23fed 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d 20 3d        zText[1] =
23fee 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 73   '\'';.        s
23fef 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
23ff0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54 65 78  xt(context, zTex
23ff1 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52  t, -1, SQLITE_TR
23ff2 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20  ANSIENT);.      
23ff3 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
23ff4 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Text);.      }. 
23ff5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23ff6 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
23ff7 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20  E_TEXT: {.      
23ff8 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20 20 75  int i,j;.      u
23ff9 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73  64 n;.      cons
23ffa 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
23ffb 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 33 5f  *zArg = sqlite3_
23ffc 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
23ffd 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20  0]);.      char 
23ffe 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 7a  *z;..      if( z
23fff 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Arg==0 ) return;
24000 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  .      for(i=0, 
24001 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b  n=0; zArg[i]; i+
24002 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d  +){ if( zArg[i]=
24003 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d 0a 20  ='\'' ) n++; }. 
24004 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 78 74       z = context
24005 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
24006 28 28 69 36 34 29 69 29 2b 28 28 69 36 34 29 6e  ((i64)i)+((i64)n
24007 29 2b 33 29 3b 0a 20 20 20 20 20 20 69 66 28 20  )+3);.      if( 
24008 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 30  z ){.        z[0
24009 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20  ] = '\'';.      
2400a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31 3b 20    for(i=0, j=1; 
2400b 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  zArg[i]; i++){. 
2400c 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20           z[j++] 
2400d 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20 20  = zArg[i];.     
2400e 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b 69 5d       if( zArg[i]
2400f 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20  =='\'' ){.      
24010 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27        z[j++] = '
24011 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  \'';.          }
24012 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
24013 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27     z[j++] = '\''
24014 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d 20 3d  ;.        z[j] =
24015 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   0;.        sqli
24016 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
24017 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c 20 73  context, z, j, s
24018 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
24019 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
2401a 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
2401b 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73  ault: {.      as
2401c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 61  sert( sqlite3_va
2401d 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
2401e 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )==SQLITE_NULL )
2401f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
24020 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
24021 65 78 74 2c 20 22 4e 55 4c 4c 22 2c 20 34 2c 20  ext, "NULL", 4, 
24022 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
24023 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
24024 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
24025 54 68 65 20 68 65 78 28 29 20 66 75 6e 63 74 69  The hex() functi
24026 6f 6e 2e 20 20 49 6e 74 65 72 70 72 65 74 20 74  on.  Interpret t
24027 68 65 20 61 72 67 75 6d 65 6e 74 20 61 73 20 61  he argument as a
24028 20 62 6c 6f 62 2e 20 20 52 65 74 75 72 6e 0a 2a   blob.  Return.*
24029 2a 20 61 20 68 65 78 61 64 65 63 69 6d 61 6c 20  * a hexadecimal 
2402a 72 65 6e 64 65 72 69 6e 67 20 61 73 20 74 65 78  rendering as tex
2402b 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
2402c 64 20 68 65 78 46 75 6e 63 28 0a 20 20 73 71 6c  d hexFunc(.  sql
2402d 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
2402e 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
2402f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
24030 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
24031 6e 74 20 69 2c 20 6e 3b 0a 20 20 63 6f 6e 73 74  nt i, n;.  const
24032 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
24033 70 42 6c 6f 62 3b 0a 20 20 63 68 61 72 20 2a 7a  pBlob;.  char *z
24034 48 65 78 2c 20 2a 7a 3b 0a 20 20 61 73 73 65 72  Hex, *z;.  asser
24035 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20  t( argc==1 );.  
24036 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
24037 28 61 72 67 63 29 3b 0a 20 20 70 42 6c 6f 62 20  (argc);.  pBlob 
24038 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
24039 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20  blob(argv[0]);. 
2403a 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
2403b 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
2403c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 6c  );.  assert( pBl
2403d 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  ob==sqlite3_valu
2403e 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20  e_blob(argv[0]) 
2403f 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69  );  /* No encodi
24040 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a  ng change */.  z
24041 20 3d 20 7a 48 65 78 20 3d 20 63 6f 6e 74 65 78   = zHex = contex
24042 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
24043 20 28 28 69 36 34 29 6e 29 2a 32 20 2b 20 31 29   ((i64)n)*2 + 1)
24044 3b 0a 20 20 69 66 28 20 7a 48 65 78 20 29 7b 0a  ;.  if( zHex ){.
24045 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
24046 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b 29 7b  ; i++, pBlob++){
24047 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
24048 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f 62 3b  char c = *pBlob;
24049 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20  .      *(z++) = 
2404a 68 65 78 64 69 67 69 74 73 5b 28 63 3e 3e 34 29  hexdigits[(c>>4)
2404b 26 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a 28 7a  &0xf];.      *(z
2404c 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b  ++) = hexdigits[
2404d 63 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a 20 20  c&0xf];.    }.  
2404e 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20 73 71    *z = 0;.    sq
2404f 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
24050 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65 78 2c  t(context, zHex,
24051 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f 66 72   n*2, sqlite3_fr
24052 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ee);.  }.}../*.*
24053 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62 28 4e  * The zeroblob(N
24054 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  ) function retur
24055 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  ns a zero-filled
24056 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20 4e 20   blob of size N 
24057 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  bytes..*/.static
24058 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62 46 75   void zeroblobFu
24059 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
2405a 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2405b 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
2405c 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2405d 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b 0a 20  gv.){.  i64 n;. 
2405e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
2405f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
24060 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
24061 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  );.  assert( arg
24062 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==1 );.  UNUSED
24063 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29  _PARAMETER(argc)
24064 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  n = sqlite3_
24065 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76  value_int64(argv
24066 5b 30 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65  [0]);.  testcase
24067 28 20 6e 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  ( n==db->aLimit[
24068 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
24069 47 54 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61  GTH] );.  testca
2406a 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c 69 6d 69  se( n==db->aLimi
2406b 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
2406c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66  ENGTH]+1 );.  if
2406d 28 20 6e 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  ( n>db->aLimit[S
2406e 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
2406f 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  TH] ){.    sqlit
24070 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
24071 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b  toobig(context);
24072 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
24073 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
24074 6f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28  oblob(context, (
24075 69 6e 74 29 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  int)n);.  }.}../
24076 2a 0a 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65  *.** The replace
24077 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  () function.  Th
24078 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72  ree arguments ar
24079 65 20 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63  e all strings: c
2407a 61 6c 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42  all.** them A, B
2407b 2c 20 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73  , and C. The res
2407c 75 6c 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74  ult is also a st
2407d 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 64 65  ring which is de
2407e 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20  rived.** from A 
2407f 62 79 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65  by replacing eve
24080 72 79 20 6f 63 63 75 72 61 6e 63 65 20 6f 66 20  ry occurance of 
24081 42 20 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d  B with C.  The m
24082 61 74 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20  atch.** must be 
24083 65 78 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e  exact.  Collatin
24084 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20  g sequences are 
24085 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61  not used..*/.sta
24086 74 69 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65  tic void replace
24087 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
24088 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
24089 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
2408a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2408b 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
2408c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2408d 53 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  Str;        /* T
2408e 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20  he input string 
2408f 41 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73  A */.  const uns
24090 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74  igned char *zPat
24091 74 65 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20  tern;    /* The 
24092 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42  pattern string B
24093 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   */.  const unsi
24094 67 6e 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b  gned char *zRep;
24095 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
24096 65 70 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e  eplacement strin
24097 67 20 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  g C */.  unsigne
24098 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20  d char *zOut;   
24099 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2409a 65 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e  e output */.  in
2409b 74 20 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20  t nStr;         
2409c 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
2409d 66 20 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20  f zStr */.  int 
2409e 6e 50 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20  nPattern;       
2409f 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
240a0 7a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e  zPattern */.  in
240a1 74 20 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20  t nRep;         
240a2 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
240a3 66 20 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20  f zRep */.  i64 
240a4 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20  nOut;           
240a5 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
240a6 73 69 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a  size of zOut */.
240a7 20 20 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b    int loopLimit;
240a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
240a9 73 74 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d  st zStr[] that m
240aa 69 67 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74  ight match zPatt
240ab 65 72 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  ern[] */.  int i
240ac 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  , j;            
240ad 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
240ae 74 65 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72  ters */..  asser
240af 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20  t( argc==3 );.  
240b0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
240b1 28 61 72 67 63 29 3b 0a 20 20 7a 53 74 72 20 3d  (argc);.  zStr =
240b2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
240b3 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
240b4 69 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65  if( zStr==0 ) re
240b5 74 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73  turn;.  nStr = s
240b6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
240b7 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61  es(argv[0]);.  a
240b8 73 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c  ssert( zStr==sql
240b9 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
240ba 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20  argv[0]) );  /* 
240bb 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e  No encoding chan
240bc 67 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e  ge */.  zPattern
240bd 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
240be 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
240bf 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d    if( zPattern==
240c0 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
240c1 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
240c2 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51  ype(argv[1])==SQ
240c3 4c 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 20 20 20  LITE_NULL.      
240c4 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
240c5 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
240c6 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 6d 61 6c  le(context)->mal
240c7 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
240c8 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
240c9 66 28 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d 3d  f( zPattern[0]==
240ca 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
240cb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
240cc 79 70 65 28 61 72 67 76 5b 31 5d 29 21 3d 53 51  ype(argv[1])!=SQ
240cd 4c 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20  LITE_NULL );.   
240ce 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
240cf 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61  value(context, a
240d0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 65 74  rgv[0]);.    ret
240d1 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 50 61 74 74  urn;.  }.  nPatt
240d2 65 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ern = sqlite3_va
240d3 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31  lue_bytes(argv[1
240d4 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 50  ]);.  assert( zP
240d5 61 74 74 65 72 6e 3d 3d 73 71 6c 69 74 65 33 5f  attern==sqlite3_
240d6 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
240d7 31 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e  1]) );  /* No en
240d8 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f  coding change */
240d9 0a 20 20 7a 52 65 70 20 3d 20 73 71 6c 69 74 65  .  zRep = sqlite
240da 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
240db 76 5b 32 5d 29 3b 0a 20 20 69 66 28 20 7a 52 65  v[2]);.  if( zRe
240dc 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
240dd 20 6e 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f   nRep = sqlite3_
240de 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
240df 5b 32 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20  [2]);.  assert( 
240e0 7a 52 65 70 3d 3d 73 71 6c 69 74 65 33 5f 76 61  zRep==sqlite3_va
240e1 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d  lue_text(argv[2]
240e2 29 20 29 3b 0a 20 20 6e 4f 75 74 20 3d 20 6e 53  ) );.  nOut = nS
240e3 74 72 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74  tr + 1;.  assert
240e4 28 20 6e 4f 75 74 3c 53 51 4c 49 54 45 5f 4d 41  ( nOut<SQLITE_MA
240e5 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 7a 4f  X_LENGTH );.  zO
240e6 75 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c  ut = contextMall
240e7 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 69 36 34  oc(context, (i64
240e8 29 6e 4f 75 74 29 3b 0a 20 20 69 66 28 20 7a 4f  )nOut);.  if( zO
240e9 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ut==0 ){.    ret
240ea 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f 70 4c  urn;.  }.  loopL
240eb 69 6d 69 74 20 3d 20 6e 53 74 72 20 2d 20 6e 50  imit = nStr - nP
240ec 61 74 74 65 72 6e 3b 20 20 0a 20 20 66 6f 72 28  attern;  .  for(
240ed 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70 4c 69  i=j=0; i<=loopLi
240ee 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  mit; i++){.    i
240ef 66 28 20 7a 53 74 72 5b 69 5d 21 3d 7a 50 61 74  f( zStr[i]!=zPat
240f0 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d 63 6d  tern[0] || memcm
240f1 70 28 26 7a 53 74 72 5b 69 5d 2c 20 7a 50 61 74  p(&zStr[i], zPat
240f2 74 65 72 6e 2c 20 6e 50 61 74 74 65 72 6e 29 20  tern, nPattern) 
240f3 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 2b  ){.      zOut[j+
240f4 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a 20 20  +] = zStr[i];.  
240f5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75    }else{.      u
240f6 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20 20 20 73  8 *zOld;.      s
240f7 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
240f8 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
240f9 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
240fa 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 6e  .      nOut += n
240fb 52 65 70 20 2d 20 6e 50 61 74 74 65 72 6e 3b 0a  Rep - nPattern;.
240fc 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
240fd 6e 4f 75 74 2d 31 3d 3d 64 62 2d 3e 61 4c 69 6d  nOut-1==db->aLim
240fe 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
240ff 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20 20  LENGTH] );.     
24100 20 74 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d   testcase( nOut-
24101 32 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  2==db->aLimit[SQ
24102 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
24103 48 5d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  H] );.      if( 
24104 6e 4f 75 74 2d 31 3e 64 62 2d 3e 61 4c 69 6d 69  nOut-1>db->aLimi
24105 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
24106 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20  ENGTH] ){.      
24107 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
24108 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f  _error_toobig(co
24109 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20  ntext);.        
2410a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2410b 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 20 20 20  , zOut);.       
2410c 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
2410d 0a 20 20 20 20 20 20 7a 4f 6c 64 20 3d 20 7a 4f  .      zOld = zO
2410e 75 74 3b 0a 20 20 20 20 20 20 7a 4f 75 74 20 3d  ut;.      zOut =
2410f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
24110 28 7a 4f 75 74 2c 20 28 69 6e 74 29 6e 4f 75 74  (zOut, (int)nOut
24111 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4f 75  );.      if( zOu
24112 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
24113 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
24114 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
24115 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  xt);.        sql
24116 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
24117 4f 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 72 65  Old);.        re
24118 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  turn;.      }.  
24119 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74      memcpy(&zOut
2411a 5b 6a 5d 2c 20 7a 52 65 70 2c 20 6e 52 65 70 29  [j], zRep, nRep)
2411b 3b 0a 20 20 20 20 20 20 6a 20 2b 3d 20 6e 52 65  ;.      j += nRe
2411c 70 3b 0a 20 20 20 20 20 20 69 20 2b 3d 20 6e 50  p;.      i += nP
2411d 61 74 74 65 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a  attern-1;.    }.
2411e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 2b    }.  assert( j+
2411f 6e 53 74 72 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29  nStr-i+1==nOut )
24120 3b 0a 20 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74  ;.  memcpy(&zOut
24121 5b 6a 5d 2c 20 26 7a 53 74 72 5b 69 5d 2c 20 6e  [j], &zStr[i], n
24122 53 74 72 2d 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e  Str-i);.  j += n
24123 53 74 72 20 2d 20 69 3b 0a 20 20 61 73 73 65 72  Str - i;.  asser
24124 74 28 20 6a 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20  t( j<=nOut );.  
24125 7a 4f 75 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73  zOut[j] = 0;.  s
24126 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
24127 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  xt(context, (cha
24128 72 2a 29 7a 4f 75 74 2c 20 6a 2c 20 73 71 6c 69  r*)zOut, j, sqli
24129 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a  te3_free);.}../*
2412a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
2412b 6f 6e 20 6f 66 20 74 68 65 20 54 52 49 4d 28 29  on of the TRIM()
2412c 2c 20 4c 54 52 49 4d 28 29 2c 20 61 6e 64 20 52  , LTRIM(), and R
2412d 54 52 49 4d 28 29 20 66 75 6e 63 74 69 6f 6e 73  TRIM() functions
2412e 2e 0a 2a 2a 20 54 68 65 20 75 73 65 72 64 61 74  ..** The userdat
2412f 61 20 69 73 20 30 78 31 20 66 6f 72 20 6c 65 66  a is 0x1 for lef
24130 74 20 74 72 69 6d 2c 20 30 78 32 20 66 6f 72 20  t trim, 0x2 for 
24131 72 69 67 68 74 20 74 72 69 6d 2c 20 30 78 33 20  right trim, 0x3 
24132 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61  for both..*/.sta
24133 74 69 63 20 76 6f 69 64 20 74 72 69 6d 46 75 6e  tic void trimFun
24134 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
24135 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
24136 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
24137 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
24138 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  v.){.  const uns
24139 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b  igned char *zIn;
2413a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
2413b 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 6f  t string */.  co
2413c 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2413d 72 20 2a 7a 43 68 61 72 53 65 74 3b 20 20 20 20  r *zCharSet;    
2413e 2f 2a 20 53 65 74 20 6f 66 20 63 68 61 72 61 63  /* Set of charac
2413f 74 65 72 73 20 74 6f 20 74 72 69 6d 20 2a 2f 0a  ters to trim */.
24140 20 20 69 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20    int nIn;      
24141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24142 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
24143 20 62 79 74 65 73 20 69 6e 20 69 6e 70 75 74 20   bytes in input 
24144 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20  */.  int flags; 
24145 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24146 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 74 72 69         /* 1: tri
24147 6d 6c 65 66 74 20 20 32 3a 20 74 72 69 6d 72 69  mleft  2: trimri
24148 67 68 74 20 20 33 3a 20 74 72 69 6d 20 2a 2f 0a  ght  3: trim */.
24149 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
2414a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2414b 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2414c 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ter */.  unsigne
2414d 64 20 63 68 61 72 20 2a 61 4c 65 6e 20 3d 20 30  d char *aLen = 0
2414e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  ;          /* Le
2414f 6e 67 74 68 20 6f 66 20 65 61 63 68 20 63 68 61  ngth of each cha
24150 72 61 63 74 65 72 20 69 6e 20 7a 43 68 61 72 53  racter in zCharS
24151 65 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  et */.  unsigned
24152 20 63 68 61 72 20 2a 2a 61 7a 43 68 61 72 20 3d   char **azChar =
24153 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64   0;       /* Ind
24154 69 76 69 64 75 61 6c 20 63 68 61 72 61 63 74 65  ividual characte
24155 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  rs in zCharSet *
24156 2f 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 20 20  /.  int nChar;  
24157 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24158 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
24159 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
2415a 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 0a 20 20   zCharSet */..  
2415b 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
2415c 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d  e_type(argv[0])=
2415d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a  =SQLITE_NULL ){.
2415e 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2415f 20 20 7a 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f    zIn = sqlite3_
24160 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
24161 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d  0]);.  if( zIn==
24162 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 49  0 ) return;.  nI
24163 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
24164 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
24165 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 49 6e 3d  ;.  assert( zIn=
24166 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
24167 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a  ext(argv[0]) );.
24168 20 20 69 66 28 20 61 72 67 63 3d 3d 31 20 29 7b    if( argc==1 ){
24169 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
2416a 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
2416b 6c 65 6e 4f 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d  lenOne[] = { 1 }
2416c 3b 0a 20 20 20 20 73 74 61 74 69 63 20 75 6e 73  ;.    static uns
2416d 69 67 6e 65 64 20 63 68 61 72 20 2a 20 63 6f 6e  igned char * con
2416e 73 74 20 61 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28  st azOne[] = { (
2416f 75 38 2a 29 22 20 22 20 7d 3b 0a 20 20 20 20 6e  u8*)" " };.    n
24170 43 68 61 72 20 3d 20 31 3b 0a 20 20 20 20 61 4c  Char = 1;.    aL
24171 65 6e 20 3d 20 28 75 38 2a 29 6c 65 6e 4f 6e 65  en = (u8*)lenOne
24172 3b 0a 20 20 20 20 61 7a 43 68 61 72 20 3d 20 28  ;.    azChar = (
24173 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a  unsigned char **
24174 29 61 7a 4f 6e 65 3b 0a 20 20 20 20 7a 43 68 61  )azOne;.    zCha
24175 72 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  rSet = 0;.  }els
24176 65 20 69 66 28 20 28 7a 43 68 61 72 53 65 74 20  e if( (zCharSet 
24177 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
24178 74 65 78 74 28 61 72 67 76 5b 31 5d 29 29 3d 3d  text(argv[1]))==
24179 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  0 ){.    return;
2417a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f  .  }else{.    co
2417b 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
2417c 72 20 2a 7a 3b 0a 20 20 20 20 66 6f 72 28 7a 3d  r *z;.    for(z=
2417d 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d  zCharSet, nChar=
2417e 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b  0; *z; nChar++){
2417f 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b  .      SQLITE_SK
24180 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20  IP_UTF8(z);.    
24181 7d 0a 20 20 20 20 69 66 28 20 6e 43 68 61 72 3e  }.    if( nChar>
24182 30 20 29 7b 0a 20 20 20 20 20 20 61 7a 43 68 61  0 ){.      azCha
24183 72 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  r = contextMallo
24184 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
24185 29 6e 43 68 61 72 29 2a 28 73 69 7a 65 6f 66 28  )nChar)*(sizeof(
24186 63 68 61 72 2a 29 2b 31 29 29 3b 0a 20 20 20 20  char*)+1));.    
24187 20 20 69 66 28 20 61 7a 43 68 61 72 3d 3d 30 20    if( azChar==0 
24188 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
24189 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
2418a 20 61 4c 65 6e 20 3d 20 28 75 6e 73 69 67 6e 65   aLen = (unsigne
2418b 64 20 63 68 61 72 2a 29 26 61 7a 43 68 61 72 5b  d char*)&azChar[
2418c 6e 43 68 61 72 5d 3b 0a 20 20 20 20 20 20 66 6f  nChar];.      fo
2418d 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43  r(z=zCharSet, nC
2418e 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72  har=0; *z; nChar
2418f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 7a 43  ++){.        azC
24190 68 61 72 5b 6e 43 68 61 72 5d 20 3d 20 28 75 6e  har[nChar] = (un
24191 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 3b  signed char *)z;
24192 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  .        SQLITE_
24193 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20  SKIP_UTF8(z);.  
24194 20 20 20 20 20 20 61 4c 65 6e 5b 6e 43 68 61 72        aLen[nChar
24195 5d 20 3d 20 28 75 38 29 28 7a 20 2d 20 61 7a 43  ] = (u8)(z - azC
24196 68 61 72 5b 6e 43 68 61 72 5d 29 3b 0a 20 20 20  har[nChar]);.   
24197 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
24198 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a   if( nChar>0 ){.
24199 20 20 20 20 66 6c 61 67 73 20 3d 20 53 51 4c 49      flags = SQLI
2419a 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 73 71  TE_PTR_TO_INT(sq
2419b 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
2419c 63 6f 6e 74 65 78 74 29 29 3b 0a 20 20 20 20 69  context));.    i
2419d 66 28 20 66 6c 61 67 73 20 26 20 31 20 29 7b 0a  f( flags & 1 ){.
2419e 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e        while( nIn
2419f 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  >0 ){.        in
241a0 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20  t len = 0;.     
241a1 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
241a2 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  har; i++){.     
241a3 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b       len = aLen[
241a4 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  i];.          if
241a5 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65  ( len<=nIn && me
241a6 6d 63 6d 70 28 7a 49 6e 2c 20 61 7a 43 68 61 72  mcmp(zIn, azChar
241a7 5b 69 5d 2c 20 6c 65 6e 29 3d 3d 30 20 29 20 62  [i], len)==0 ) b
241a8 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
241a9 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 6e          if( i>=n
241aa 43 68 61 72 20 29 20 62 72 65 61 6b 3b 0a 20 20  Char ) break;.  
241ab 20 20 20 20 20 20 7a 49 6e 20 2b 3d 20 6c 65 6e        zIn += len
241ac 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d  ;.        nIn -=
241ad 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20   len;.      }.  
241ae 20 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67    }.    if( flag
241af 73 20 26 20 32 20 29 7b 0a 20 20 20 20 20 20 77  s & 2 ){.      w
241b0 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20  hile( nIn>0 ){. 
241b1 20 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d         int len =
241b2 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   0;.        for(
241b3 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b  i=0; i<nChar; i+
241b4 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  +){.          le
241b5 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20  n = aLen[i];.   
241b6 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3c 3d         if( len<=
241b7 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28 26 7a  nIn && memcmp(&z
241b8 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61 7a 43 68  In[nIn-len],azCh
241b9 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30 20 29 20  ar[i],len)==0 ) 
241ba 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
241bb 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d  .        if( i>=
241bc 6e 43 68 61 72 20 29 20 62 72 65 61 6b 3b 0a 20  nChar ) break;. 
241bd 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c 65         nIn -= le
241be 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
241bf 0a 20 20 20 20 69 66 28 20 7a 43 68 61 72 53 65  .    if( zCharSe
241c0 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
241c1 65 33 5f 66 72 65 65 28 61 7a 43 68 61 72 29 3b  e3_free(azChar);
241c2 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
241c3 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
241c4 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a  (context, (char*
241c5 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51 4c 49 54  )zIn, nIn, SQLIT
241c6 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a  E_TRANSIENT);.}.
241c7 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
241c8 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20 43 6f  SOUNDEX./*.** Co
241c9 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e 64 65  mpute the sounde
241ca 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20  x encoding of a 
241cb 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  word..*/.static 
241cc 76 6f 69 64 20 73 6f 75 6e 64 65 78 46 75 6e 63  void soundexFunc
241cd 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
241ce 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
241cf 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
241d0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
241d1 0a 29 7b 0a 20 20 63 68 61 72 20 7a 52 65 73 75  .){.  char zResu
241d2 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e 73 74 20 75  lt[8];.  const u
241d3 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74 20 69 2c  8 *zIn;.  int i,
241d4 20 6a 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   j;.  static con
241d5 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
241d6 20 69 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20 20   iCode[] = {.   
241d7 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
241d8 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
241d9 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
241da 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c  .    0, 0, 0, 0,
241db 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
241dc 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
241dd 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30  , 0,.    0, 0, 0
241de 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
241df 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
241e0 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20  0, 0, 0,.    0, 
241e1 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
241e2 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
241e3 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20   0, 0, 0, 0,.   
241e4 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20   0, 0, 1, 2, 3, 
241e5 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32  0, 1, 2, 0, 0, 2
241e6 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c  , 2, 4, 5, 5, 0,
241e7 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c  .    1, 2, 6, 2,
241e8 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20   3, 0, 1, 0, 2, 
241e9 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 2, 0, 0, 0, 0
241ea 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31  , 0,.    0, 0, 1
241eb 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c  , 2, 3, 0, 1, 2,
241ec 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20   0, 0, 2, 2, 4, 
241ed 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20  5, 5, 0,.    1, 
241ee 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31  2, 6, 2, 3, 0, 1
241ef 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c  , 0, 2, 0, 2, 0,
241f0 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 7d   0, 0, 0, 0,.  }
241f1 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
241f2 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e 20 3d 20 28  ==1 );.  zIn = (
241f3 75 38 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  u8*)sqlite3_valu
241f4 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
241f5 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20  .  if( zIn==0 ) 
241f6 7a 49 6e 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20  zIn = (u8*)"";. 
241f7 20 66 6f 72 28 69 3d 30 3b 20 7a 49 6e 5b 69 5d   for(i=0; zIn[i]
241f8 20 26 26 20 21 73 71 6c 69 74 65 33 49 73 61 6c   && !sqlite3Isal
241f9 70 68 61 28 7a 49 6e 5b 69 5d 29 3b 20 69 2b 2b  pha(zIn[i]); i++
241fa 29 7b 7d 0a 20 20 69 66 28 20 7a 49 6e 5b 69 5d  ){}.  if( zIn[i]
241fb 20 29 7b 0a 20 20 20 20 75 38 20 70 72 65 76 63   ){.    u8 prevc
241fc 6f 64 65 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b  ode = iCode[zIn[
241fd 69 5d 26 30 78 37 66 5d 3b 0a 20 20 20 20 7a 52  i]&0x7f];.    zR
241fe 65 73 75 6c 74 5b 30 5d 20 3d 20 73 71 6c 69 74  esult[0] = sqlit
241ff 65 33 54 6f 75 70 70 65 72 28 7a 49 6e 5b 69 5d  e3Toupper(zIn[i]
24200 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20  );.    for(j=1; 
24201 6a 3c 34 20 26 26 20 7a 49 6e 5b 69 5d 3b 20 69  j<4 && zIn[i]; i
24202 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 63  ++){.      int c
24203 6f 64 65 20 3d 20 69 43 6f 64 65 5b 7a 49 6e 5b  ode = iCode[zIn[
24204 69 5d 26 30 78 37 66 5d 3b 0a 20 20 20 20 20 20  i]&0x7f];.      
24205 69 66 28 20 63 6f 64 65 3e 30 20 29 7b 0a 20 20  if( code>0 ){.  
24206 20 20 20 20 20 20 69 66 28 20 63 6f 64 65 21 3d        if( code!=
24207 70 72 65 76 63 6f 64 65 20 29 7b 0a 20 20 20 20  prevcode ){.    
24208 20 20 20 20 20 20 70 72 65 76 63 6f 64 65 20 3d        prevcode =
24209 20 63 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 20   code;.         
2420a 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20   zResult[j++] = 
2420b 63 6f 64 65 20 2b 20 27 30 27 3b 0a 20 20 20 20  code + '0';.    
2420c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
2420d 65 7b 0a 20 20 20 20 20 20 20 20 70 72 65 76 63  e{.        prevc
2420e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ode = 0;.      }
2420f 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65  .    }.    while
24210 28 20 6a 3c 34 20 29 7b 0a 20 20 20 20 20 20 7a  ( j<4 ){.      z
24211 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 27 30  Result[j++] = '0
24212 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 52 65  ';.    }.    zRe
24213 73 75 6c 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20  sult[j] = 0;.   
24214 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
24215 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52  text(context, zR
24216 65 73 75 6c 74 2c 20 34 2c 20 53 51 4c 49 54 45  esult, 4, SQLITE
24217 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
24218 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
24219 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
2421a 6e 74 65 78 74 2c 20 22 3f 30 30 30 22 2c 20 34  ntext, "?000", 4
2421b 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2421c 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ;.  }.}.#endif..
2421d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2421e 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
2421f 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74  ON./*.** A funct
24220 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 73 20 61  ion that loads a
24221 20 73 68 61 72 65 64 2d 6c 69 62 72 61 72 79 20   shared-library 
24222 65 78 74 65 6e 73 69 6f 6e 20 74 68 65 6e 20 72  extension then r
24223 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  eturns NULL..*/.
24224 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64  static void load
24225 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  Ext(sqlite3_cont
24226 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
24227 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
24228 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
24229 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
2422a 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  le = (const char
2422b 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
2422c 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
2422d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
2422e 72 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  roc;.  sqlite3 *
2422f 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
24230 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
24231 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68 61 72 20  ontext);.  char 
24232 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20  *zErrMsg = 0;.. 
24233 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a   if( argc==2 ){.
24234 20 20 20 20 7a 50 72 6f 63 20 3d 20 28 63 6f 6e      zProc = (con
24235 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
24236 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
24237 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  v[1]);.  }else{.
24238 20 20 20 20 7a 50 72 6f 63 20 3d 20 30 3b 0a 20      zProc = 0;. 
24239 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65 20 26   }.  if( zFile &
2423a 26 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65  & sqlite3_load_e
2423b 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69  xtension(db, zFi
2423c 6c 65 2c 20 7a 50 72 6f 63 2c 20 26 7a 45 72 72  le, zProc, &zErr
2423d 4d 73 67 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Msg) ){.    sqli
2423e 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2423f 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 4d 73  (context, zErrMs
24240 67 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69  g, -1);.    sqli
24241 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d 73 67  te3_free(zErrMsg
24242 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  );.  }.}.#endif.
24243 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
24244 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
24245 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68  wing structure h
24246 6f 6c 64 73 20 74 68 65 20 63 6f 6e 74 65 78 74  olds the context
24247 20 6f 66 20 61 0a 2a 2a 20 73 75 6d 28 29 20 6f   of a.** sum() o
24248 72 20 61 76 67 28 29 20 61 67 67 72 65 67 61 74  r avg() aggregat
24249 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 2e 0a 2a  e computation..*
2424a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
2424b 20 53 75 6d 43 74 78 20 53 75 6d 43 74 78 3b 0a   SumCtx SumCtx;.
2424c 73 74 72 75 63 74 20 53 75 6d 43 74 78 20 7b 0a  struct SumCtx {.
2424d 20 20 64 6f 75 62 6c 65 20 72 53 75 6d 3b 20 20    double rSum;  
2424e 20 20 20 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20      /* Floating 
2424f 70 6f 69 6e 74 20 73 75 6d 20 2a 2f 0a 20 20 69  point sum */.  i
24250 36 34 20 69 53 75 6d 3b 20 20 20 20 20 20 20 20  64 iSum;        
24251 20 2f 2a 20 49 6e 74 65 67 65 72 20 73 75 6d 20   /* Integer sum 
24252 2a 2f 20 20 20 0a 20 20 69 36 34 20 63 6e 74 3b  */   .  i64 cnt;
24253 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
24254 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
24255 73 75 6d 6d 65 64 20 2a 2f 0a 20 20 75 38 20 6f  summed */.  u8 o
24256 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 2f 2a  verflow;      /*
24257 20 54 72 75 65 20 69 66 20 69 6e 74 65 67 65 72   True if integer
24258 20 6f 76 65 72 66 6c 6f 77 20 73 65 65 6e 20 2a   overflow seen *
24259 2f 0a 20 20 75 38 20 61 70 70 72 6f 78 3b 20 20  /.  u8 approx;  
2425a 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2425b 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 20 76 61 6c   non-integer val
2425c 75 65 20 77 61 73 20 69 6e 70 75 74 20 74 6f 20  ue was input to 
2425d 74 68 65 20 73 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f  the sum */.};../
2425e 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73  *.** Routines us
2425f 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68  ed to compute th
24260 65 20 73 75 6d 2c 20 61 76 65 72 61 67 65 2c 20  e sum, average, 
24261 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a  and total..**.**
24262 20 54 68 65 20 53 55 4d 28 29 20 66 75 6e 63 74   The SUM() funct
24263 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20  ion follows the 
24264 28 62 72 6f 6b 65 6e 29 20 53 51 4c 20 73 74 61  (broken) SQL sta
24265 6e 64 61 72 64 20 77 68 69 63 68 20 6d 65 61 6e  ndard which mean
24266 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 72 65 74  s.** that it ret
24267 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 74 20  urns NULL if it 
24268 73 75 6d 73 20 6f 76 65 72 20 6e 6f 20 69 6e 70  sums over no inp
24269 75 74 73 2e 20 20 54 4f 54 41 4c 20 72 65 74 75  uts.  TOTAL retu
2426a 72 6e 73 0a 2a 2a 20 30 2e 30 20 69 6e 20 74 68  rns.** 0.0 in th
2426b 61 74 20 63 61 73 65 2e 20 20 49 6e 20 61 64 64  at case.  In add
2426c 69 74 69 6f 6e 2c 20 54 4f 54 41 4c 20 61 6c 77  ition, TOTAL alw
2426d 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 66 6c  ays returns a fl
2426e 6f 61 74 20 77 68 65 72 65 0a 2a 2a 20 53 55 4d  oat where.** SUM
2426f 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 61 6e   might return an
24270 20 69 6e 74 65 67 65 72 20 69 66 20 69 74 20 6e   integer if it n
24271 65 76 65 72 20 65 6e 63 6f 75 6e 74 65 72 73 20  ever encounters 
24272 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  a floating point
24273 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 54 4f 54 41  .** value.  TOTA
24274 4c 20 6e 65 76 65 72 20 66 61 69 6c 73 2c 20 62  L never fails, b
24275 75 74 20 53 55 4d 20 6d 69 67 68 74 20 74 68 72  ut SUM might thr
24276 6f 75 67 68 20 61 6e 20 65 78 63 65 70 74 69 6f  ough an exceptio
24277 6e 20 69 66 0a 2a 2a 20 69 74 20 6f 76 65 72 66  n if.** it overf
24278 6c 6f 77 73 20 61 6e 20 69 6e 74 65 67 65 72 2e  lows an integer.
24279 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2427a 73 75 6d 53 74 65 70 28 73 71 6c 69 74 65 33 5f  sumStep(sqlite3_
2427b 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2427c 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
2427d 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
2427e 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a  ){.  SumCtx *p;.
2427f 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 61 73    int type;.  as
24280 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
24281 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
24282 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70 20 3d  TER(argc);.  p =
24283 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
24284 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
24285 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b  xt, sizeof(*p));
24286 0a 20 20 74 79 70 65 20 3d 20 73 71 6c 69 74 65  .  type = sqlite
24287 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
24288 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20  type(argv[0]);. 
24289 20 69 66 28 20 70 20 26 26 20 74 79 70 65 21 3d   if( p && type!=
2428a 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20  SQLITE_NULL ){. 
2428b 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20     p->cnt++;.   
2428c 20 69 66 28 20 74 79 70 65 3d 3d 53 51 4c 49 54   if( type==SQLIT
2428d 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20  E_INTEGER ){.   
2428e 20 20 20 69 36 34 20 76 20 3d 20 73 71 6c 69 74     i64 v = sqlit
2428f 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61  e3_value_int64(a
24290 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70  rgv[0]);.      p
24291 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a 20 20 20  ->rSum += v;.   
24292 20 20 20 69 66 28 20 28 70 2d 3e 61 70 70 72 6f     if( (p->appro
24293 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77 29 3d 3d  x|p->overflow)==
24294 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 36 34  0 ){.        i64
24295 20 69 4e 65 77 53 75 6d 20 3d 20 70 2d 3e 69 53   iNewSum = p->iS
24296 75 6d 20 2b 20 76 3b 0a 20 20 20 20 20 20 20 20  um + v;.        
24297 69 6e 74 20 73 31 20 3d 20 28 69 6e 74 29 28 70  int s1 = (int)(p
24298 2d 3e 69 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f  ->iSum >> (sizeo
24299 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20 20  f(i64)*8-1));.  
2429a 20 20 20 20 20 20 69 6e 74 20 73 32 20 3d 20 28        int s2 = (
2429b 69 6e 74 29 28 76 20 20 20 20 20 20 20 3e 3e 20  int)(v       >> 
2429c 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31  (sizeof(i64)*8-1
2429d 29 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ));.        int 
2429e 73 33 20 3d 20 28 69 6e 74 29 28 69 4e 65 77 53  s3 = (int)(iNewS
2429f 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36  um >> (sizeof(i6
242a0 34 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20  4)*8-1));.      
242a1 20 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20    p->overflow = 
242a2 28 28 73 31 26 73 32 26 7e 73 33 29 20 7c 20 28  ((s1&s2&~s3) | (
242a3 7e 73 31 26 7e 73 32 26 73 33 29 29 3f 31 3a 30  ~s1&~s2&s3))?1:0
242a4 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 53 75  ;.        p->iSu
242a5 6d 20 3d 20 69 4e 65 77 53 75 6d 3b 0a 20 20 20  m = iNewSum;.   
242a6 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
242a7 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d        p->rSum +=
242a8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
242a9 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a  ouble(argv[0]);.
242aa 20 20 20 20 20 20 70 2d 3e 61 70 70 72 6f 78 20        p->approx 
242ab 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 1;.    }.  }.}
242ac 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d  .static void sum
242ad 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  Finalize(sqlite3
242ae 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
242af 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b  t){.  SumCtx *p;
242b0 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61  .  p = sqlite3_a
242b1 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
242b2 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
242b3 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e  if( p && p->cnt>
242b4 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  0 ){.    if( p->
242b5 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  overflow ){.    
242b6 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
242b7 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 22  _error(context,"
242b8 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77  integer overflow
242b9 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ",-1);.    }else
242ba 20 69 66 28 20 70 2d 3e 61 70 70 72 6f 78 20 29   if( p->approx )
242bb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
242bc 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
242bd 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 29 3b  ntext, p->rSum);
242be 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
242bf 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
242c0 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20  _int64(context, 
242c1 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 20 7d 0a  p->iSum);.    }.
242c2 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
242c3 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 28 73 71  d avgFinalize(sq
242c4 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
242c5 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74  ontext){.  SumCt
242c6 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
242c7 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
242c8 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
242c9 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  );.  if( p && p-
242ca 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 73 71  >cnt>0 ){.    sq
242cb 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
242cc 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e  ble(context, p->
242cd 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70 2d 3e  rSum/(double)p->
242ce 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  cnt);.  }.}.stat
242cf 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 46 69 6e  ic void totalFin
242d0 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
242d1 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
242d2 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20  .  SumCtx *p;.  
242d3 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
242d4 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
242d5 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 2f 2a 20  ntext, 0);.  /* 
242d6 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73  (double)0 In cas
242d7 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  e of SQLITE_OMIT
242d8 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e  _FLOATING_POINT.
242d9 2e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .. */.  sqlite3_
242da 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f  result_double(co
242db 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d 3e 72 53  ntext, p ? p->rS
242dc 75 6d 20 3a 20 28 64 6f 75 62 6c 65 29 30 29 3b  um : (double)0);
242dd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
242de 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
242df 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66  e keeps track of
242e0 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69   state informati
242e1 6f 6e 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f  on for the.** co
242e2 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 20  unt() aggregate 
242e3 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  function..*/.typ
242e4 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 75 6e  edef struct Coun
242e5 74 43 74 78 20 43 6f 75 6e 74 43 74 78 3b 0a 73  tCtx CountCtx;.s
242e6 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 7b  truct CountCtx {
242e7 0a 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a 0a 2f 2a  .  i64 n;.};../*
242e8 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20  .** Routines to 
242e9 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f  implement the co
242ea 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 20  unt() aggregate 
242eb 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
242ec 74 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 53 74  tic void countSt
242ed 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ep(sqlite3_conte
242ee 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
242ef 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
242f0 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
242f1 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70  CountCtx *p;.  p
242f2 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
242f3 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
242f4 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29  text, sizeof(*p)
242f5 29 3b 0a 20 20 69 66 28 20 28 61 72 67 63 3d 3d  );.  if( (argc==
242f6 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  0 || SQLITE_NULL
242f7 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  !=sqlite3_value_
242f8 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 20 26  type(argv[0])) &
242f9 26 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 2b  & p ){.    p->n+
242fa 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  +;.  }..#ifndef 
242fb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
242fc 45 43 41 54 45 44 0a 20 20 2f 2a 20 54 68 65 20  ECATED.  /* The 
242fd 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
242fe 65 5f 63 6f 75 6e 74 28 29 20 66 75 6e 63 74 69  e_count() functi
242ff 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64  on is deprecated
24300 2e 20 20 42 75 74 20 6a 75 73 74 20 74 6f 20 6d  .  But just to m
24301 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 69 74  ake.  ** sure it
24302 20 73 74 69 6c 6c 20 6f 70 65 72 61 74 65 73 20   still operates 
24303 63 6f 72 72 65 63 74 6c 79 2c 20 76 65 72 69 66  correctly, verif
24304 79 20 74 68 61 74 20 69 74 73 20 63 6f 75 6e 74  y that its count
24305 20 61 67 72 65 65 73 20 77 69 74 68 20 6f 75 72   agrees with our
24306 20 0a 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c 20   .  ** internal 
24307 63 6f 75 6e 74 20 77 68 65 6e 20 75 73 69 6e 67  count when using
24308 20 63 6f 75 6e 74 28 2a 29 20 61 6e 64 20 77 68   count(*) and wh
24309 65 6e 20 74 68 65 20 74 6f 74 61 6c 20 63 6f 75  en the total cou
2430a 6e 74 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 65  nt can be.  ** e
2430b 78 70 72 65 73 73 65 64 20 61 73 20 61 20 33 32  xpressed as a 32
2430c 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20 2a 2f  -bit integer. */
2430d 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
2430e 3d 31 20 7c 7c 20 70 3d 3d 30 20 7c 7c 20 70 2d  =1 || p==0 || p-
2430f 3e 6e 3e 30 78 37 66 66 66 66 66 66 66 0a 20 20  >n>0x7fffffff.  
24310 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6e 3d          || p->n=
24311 3d 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  =sqlite3_aggrega
24312 74 65 5f 63 6f 75 6e 74 28 63 6f 6e 74 65 78 74  te_count(context
24313 29 20 29 3b 0a 23 65 6e 64 69 66 0a 7d 20 20 20  ) );.#endif.}   
24314 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 75  .static void cou
24315 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  ntFinalize(sqlit
24316 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
24317 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78  ext){.  CountCtx
24318 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74   *p;.  p = sqlit
24319 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
2431a 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29  text(context, 0)
2431b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
2431c 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74  lt_int64(context
2431d 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30 29 3b  , p ? p->n : 0);
2431e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  .}../*.** Routin
2431f 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  es to implement 
24320 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20  min() and max() 
24321 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
24322 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ons..*/.static v
24323 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70 28 0a  oid minmaxStep(.
24324 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
24325 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69  t *context, .  i
24326 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73  nt NotUsed, .  s
24327 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
24328 72 67 76 0a 29 7b 0a 20 20 4d 65 6d 20 2a 70 41  rgv.){.  Mem *pA
24329 72 67 20 20 3d 20 28 4d 65 6d 20 2a 29 61 72 67  rg  = (Mem *)arg
2432a 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a 70 42 65  v[0];.  Mem *pBe
2432b 73 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  st;.  UNUSED_PAR
2432c 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
2432d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ..  if( sqlite3_
2432e 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
2432f 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
24330 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 42 65   ) return;.  pBe
24331 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71 6c 69  st = (Mem *)sqli
24332 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
24333 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
24334 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 29 3b 0a  izeof(*pBest));.
24335 20 20 69 66 28 20 21 70 42 65 73 74 20 29 20 72    if( !pBest ) r
24336 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 70 42  eturn;..  if( pB
24337 65 73 74 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20  est->flags ){.  
24338 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 20 20 69    int max;.    i
24339 6e 74 20 63 6d 70 3b 0a 20 20 20 20 43 6f 6c 6c  nt cmp;.    Coll
2433a 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c  Seq *pColl = sql
2433b 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53  ite3GetFuncCollS
2433c 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  eq(context);.   
2433d 20 2f 2a 20 54 68 69 73 20 73 74 65 70 20 66 75   /* This step fu
2433e 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66  nction is used f
2433f 6f 72 20 62 6f 74 68 20 74 68 65 20 6d 69 6e 28  or both the min(
24340 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72  ) and max() aggr
24341 65 67 61 74 65 73 2c 0a 20 20 20 20 2a 2a 20 74  egates,.    ** t
24342 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e  he only differen
24343 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 74  ce between the t
24344 77 6f 20 62 65 69 6e 67 20 74 68 61 74 20 74 68  wo being that th
24345 65 20 73 65 6e 73 65 20 6f 66 20 74 68 65 0a 20  e sense of the. 
24346 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e     ** comparison
24347 20 69 73 20 69 6e 76 65 72 74 65 64 2e 20 46 6f   is inverted. Fo
24348 72 20 74 68 65 20 6d 61 78 28 29 20 61 67 67 72  r the max() aggr
24349 65 67 61 74 65 2c 20 74 68 65 0a 20 20 20 20 2a  egate, the.    *
2434a 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  * sqlite3_user_d
2434b 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 72  ata() function r
2434c 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 2d  eturns (void *)-
2434d 31 2e 20 46 6f 72 20 6d 69 6e 28 29 20 69 74 0a  1. For min() it.
2434e 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 28      ** returns (
2434f 76 6f 69 64 20 2a 29 64 62 2c 20 77 68 65 72 65  void *)db, where
24350 20 64 62 20 69 73 20 74 68 65 20 73 71 6c 69 74   db is the sqlit
24351 65 33 2a 20 64 61 74 61 62 61 73 65 20 70 6f 69  e3* database poi
24352 6e 74 65 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65  nter..    ** The
24353 72 65 66 6f 72 65 20 74 68 65 20 6e 65 78 74 20  refore the next 
24354 73 74 61 74 65 6d 65 6e 74 20 73 65 74 73 20 76  statement sets v
24355 61 72 69 61 62 6c 65 20 27 6d 61 78 27 20 74 6f  ariable 'max' to
24356 20 31 20 66 6f 72 20 74 68 65 20 6d 61 78 28 29   1 for the max()
24357 0a 20 20 20 20 2a 2a 20 61 67 67 72 65 67 61 74  .    ** aggregat
24358 65 2c 20 6f 72 20 30 20 66 6f 72 20 6d 69 6e 28  e, or 0 for min(
24359 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 61  )..    */.    ma
2435a 78 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72  x = sqlite3_user
2435b 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 21 3d  _data(context)!=
2435c 30 3b 0a 20 20 20 20 63 6d 70 20 3d 20 73 71 6c  0;.    cmp = sql
2435d 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70  ite3MemCompare(p
2435e 42 65 73 74 2c 20 70 41 72 67 2c 20 70 43 6f 6c  Best, pArg, pCol
2435f 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 6d 61 78  l);.    if( (max
24360 20 26 26 20 63 6d 70 3c 30 29 20 7c 7c 20 28 21   && cmp<0) || (!
24361 6d 61 78 20 26 26 20 63 6d 70 3e 30 29 20 29 7b  max && cmp>0) ){
24362 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
24363 62 65 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c  beMemCopy(pBest,
24364 20 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a 20 20   pArg);.    }.  
24365 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
24366 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42  e3VdbeMemCopy(pB
24367 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a  est, pArg);.  }.
24368 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69  }.static void mi
24369 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73 71 6c  nMaxFinalize(sql
2436a 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
2436b 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c 69 74 65  ntext){.  sqlite
2436c 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b 0a 20  3_value *pRes;. 
2436d 20 70 52 65 73 20 3d 20 28 73 71 6c 69 74 65 33   pRes = (sqlite3
2436e 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 74 65 33  _value *)sqlite3
2436f 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
24370 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  xt(context, 0);.
24371 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a 20 20    if( pRes ){.  
24372 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 52 65    if( ALWAYS(pRe
24373 73 2d 3e 66 6c 61 67 73 29 20 29 7b 0a 20 20 20  s->flags) ){.   
24374 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
24375 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
24376 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 0a 20 20   pRes);.    }.  
24377 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
24378 52 65 6c 65 61 73 65 28 70 52 65 73 29 3b 0a 20  Release(pRes);. 
24379 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 67 72 6f 75   }.}../*.** grou
2437a 70 5f 63 6f 6e 63 61 74 28 45 58 50 52 2c 20 3f  p_concat(EXPR, ?
2437b 53 45 50 41 52 41 54 4f 52 3f 29 0a 2a 2f 0a 73  SEPARATOR?).*/.s
2437c 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70  tatic void group
2437d 43 6f 6e 63 61 74 53 74 65 70 28 0a 20 20 73 71  ConcatStep(.  sq
2437e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2437f 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
24380 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
24381 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
24382 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 61 6c  const char *zVal
24383 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41  ;.  StrAccum *pA
24384 63 63 75 6d 3b 0a 20 20 63 6f 6e 73 74 20 63 68  ccum;.  const ch
24385 61 72 20 2a 7a 53 65 70 3b 0a 20 20 69 6e 74 20  ar *zSep;.  int 
24386 6e 56 61 6c 2c 20 6e 53 65 70 3b 0a 20 20 61 73  nVal, nSep;.  as
24387 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c  sert( argc==1 ||
24388 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66   argc==2 );.  if
24389 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
2438a 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53  type(argv[0])==S
2438b 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74  QLITE_NULL ) ret
2438c 75 72 6e 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20  urn;.  pAccum = 
2438d 28 53 74 72 41 63 63 75 6d 2a 29 73 71 6c 69 74  (StrAccum*)sqlit
2438e 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
2438f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69  text(context, si
24390 7a 65 6f 66 28 2a 70 41 63 63 75 6d 29 29 3b 0a  zeof(*pAccum));.
24391 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20 29 7b  .  if( pAccum ){
24392 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
24393 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
24394 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
24395 74 65 78 74 29 3b 0a 20 20 20 20 69 6e 74 20 6e  text);.    int n
24396 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 75 73  ;.    pAccum->us
24397 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 20  eMalloc = 1;.   
24398 20 70 41 63 63 75 6d 2d 3e 6d 78 41 6c 6c 6f 63   pAccum->mxAlloc
24399 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51   = db->aLimit[SQ
2439a 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
2439b 48 5d 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  H];.#ifdef SQLIT
2439c 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
2439d 44 0a 20 20 20 20 6e 20 3d 20 63 6f 6e 74 65 78  D.    n = contex
2439e 74 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a 23 65 6c 73  t->pMem->n;.#els
2439f 65 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65  e.    n = sqlite
243a0 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e  3_aggregate_coun
243a1 74 28 63 6f 6e 74 65 78 74 29 3b 0a 23 65 6e 64  t(context);.#end
243a2 69 66 0a 20 20 20 20 69 66 28 20 6e 3e 31 20 29  if.    if( n>1 )
243a3 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 63  {.      if( argc
243a4 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ==2 ){.        z
243a5 53 65 70 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  Sep = (char*)sql
243a6 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
243a7 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20  argv[1]);.      
243a8 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74 65 33    nSep = sqlite3
243a9 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
243aa 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c  v[1]);.      }el
243ab 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70  se{.        zSep
243ac 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 20 20   = ",";.        
243ad 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 20 20  nSep = 1;.      
243ae 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  }.      sqlite3S
243af 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
243b0 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70  ccum, zSep, nSep
243b1 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 56 61  );.    }.    zVa
243b2 6c 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  l = (char*)sqlit
243b3 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
243b4 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 56 61 6c  gv[0]);.    nVal
243b5 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
243b6 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b  _bytes(argv[0]);
243b7 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41  .    sqlite3StrA
243b8 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
243b9 6d 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 3b 0a  m, zVal, nVal);.
243ba 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
243bb 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e  d groupConcatFin
243bc 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
243bd 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
243be 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63  .  StrAccum *pAc
243bf 63 75 6d 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20  cum;.  pAccum = 
243c0 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
243c1 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
243c2 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 41 63  t, 0);.  if( pAc
243c3 63 75 6d 20 29 7b 0a 20 20 20 20 69 66 28 20 70  cum ){.    if( p
243c4 41 63 63 75 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b  Accum->tooBig ){
243c5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
243c6 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
243c7 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ig(context);.   
243c8 20 7d 65 6c 73 65 20 69 66 28 20 70 41 63 63 75   }else if( pAccu
243c9 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  m->mallocFailed 
243ca 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
243cb 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
243cc 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  mem(context);.  
243cd 20 20 7d 65 6c 73 65 7b 20 20 20 20 0a 20 20 20    }else{    .   
243ce 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
243cf 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
243d0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
243d1 69 6e 69 73 68 28 70 41 63 63 75 6d 29 2c 20 2d  inish(pAccum), -
243d2 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  1, .            
243d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
243d4 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20  lite3_free);.   
243d5 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
243d6 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
243d7 67 69 73 74 65 72 65 64 20 61 6c 6c 20 6f 66 20  gistered all of 
243d8 74 68 65 20 61 62 6f 76 65 20 43 20 66 75 6e 63  the above C func
243d9 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20  tions as SQL.** 
243da 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73  functions.  This
243db 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f   should be the o
243dc 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
243dd 68 69 73 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a  his file with.**
243de 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67   external linkag
243df 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
243e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
243e1 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e  3RegisterBuiltin
243e2 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
243e3 33 20 2a 64 62 29 7b 0a 23 69 66 6e 64 65 66 20  3 *db){.#ifndef 
243e4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45  SQLITE_OMIT_ALTE
243e5 52 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33  RTABLE.  sqlite3
243e6 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 64  AlterFunctions(d
243e7 62 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  b);.#endif.  if(
243e8 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
243e9 65 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  ed ){.    int rc
243ea 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c   = sqlite3_overl
243eb 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  oad_function(db,
243ec 20 22 4d 41 54 43 48 22 2c 20 32 29 3b 0a 20 20   "MATCH", 2);.  
243ed 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
243ee 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63  LITE_NOMEM || rc
243ef 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
243f0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
243f1 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  E_NOMEM ){.     
243f2 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
243f3 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 1;.    }.  }
243f4 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
243f5 53 45 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  SE.  (void)sqlit
243f6 65 33 53 73 65 46 75 6e 63 74 69 6f 6e 73 28 64  e3SseFunctions(d
243f7 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  b);.#endif.}../*
243f8 0a 2a 2a 20 53 65 74 20 74 68 65 20 4c 49 4b 45  .** Set the LIKE
243f9 4f 50 54 20 66 6c 61 67 20 6f 6e 20 74 68 65 20  OPT flag on the 
243fa 32 2d 61 72 67 75 6d 65 6e 74 20 66 75 6e 63 74  2-argument funct
243fb 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76  ion with the giv
243fc 65 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74  en name..*/.stat
243fd 69 63 20 76 6f 69 64 20 73 65 74 4c 69 6b 65 4f  ic void setLikeO
243fe 70 74 46 6c 61 67 28 73 71 6c 69 74 65 33 20 2a  ptFlag(sqlite3 *
243ff 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
24400 7a 4e 61 6d 65 2c 20 75 38 20 66 6c 61 67 56 61  zName, u8 flagVa
24401 6c 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  l){.  FuncDef *p
24402 44 65 66 3b 0a 20 20 70 44 65 66 20 3d 20 73 71  Def;.  pDef = sq
24403 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
24404 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c  n(db, zName, sql
24405 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
24406 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  me),.           
24407 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24408 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38    2, SQLITE_UTF8
24409 2c 20 30 29 3b 0a 20 20 69 66 28 20 41 4c 57 41  , 0);.  if( ALWA
2440a 59 53 28 70 44 65 66 29 20 29 7b 0a 20 20 20 20  YS(pDef) ){.    
2440b 70 44 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66 6c  pDef->flags = fl
2440c 61 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  agVal;.  }.}../*
2440d 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 65  .** Register the
2440e 20 62 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 61   built-in LIKE a
2440f 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e  nd GLOB function
24410 73 2e 20 20 54 68 65 20 63 61 73 65 53 65 6e 73  s.  The caseSens
24411 69 74 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65 74  itive.** paramet
24412 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  er determines wh
24413 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
24414 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 69   LIKE operator i
24415 73 20 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69 74  s case.** sensit
24416 69 76 65 2e 20 20 47 4c 4f 42 20 69 73 20 61 6c  ive.  GLOB is al
24417 77 61 79 73 20 63 61 73 65 20 73 65 6e 73 69 74  ways case sensit
24418 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ive..*/.SQLITE_P
24419 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2441a 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46  te3RegisterLikeF
2441b 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33  unctions(sqlite3
2441c 20 2a 64 62 2c 20 69 6e 74 20 63 61 73 65 53 65   *db, int caseSe
2441d 6e 73 69 74 69 76 65 29 7b 0a 20 20 73 74 72 75  nsitive){.  stru
2441e 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a  ct compareInfo *
2441f 70 49 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61 73  pInfo;.  if( cas
24420 65 53 65 6e 73 69 74 69 76 65 20 29 7b 0a 20 20  eSensitive ){.  
24421 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63    pInfo = (struc
24422 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26  t compareInfo*)&
24423 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d  likeInfoAlt;.  }
24424 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20  else{.    pInfo 
24425 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72  = (struct compar
24426 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f  eInfo*)&likeInfo
24427 4e 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Norm;.  }.  sqli
24428 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
24429 2c 20 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c  , "like", 2, SQL
2442a 49 54 45 5f 41 4e 59 2c 20 70 49 6e 66 6f 2c 20  ITE_ANY, pInfo, 
2442b 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b  likeFunc, 0, 0);
2442c 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  .  sqlite3Create
2442d 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c  Func(db, "like",
2442e 20 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20   3, SQLITE_ANY, 
2442f 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c  pInfo, likeFunc,
24430 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
24431 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
24432 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54  "glob", 2, SQLIT
24433 45 5f 41 4e 59 2c 20 0a 20 20 20 20 20 20 28 73  E_ANY, .      (s
24434 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
24435 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69  o*)&globInfo, li
24436 6b 65 46 75 6e 63 2c 20 30 2c 30 29 3b 0a 20 20  keFunc, 0,0);.  
24437 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64  setLikeOptFlag(d
24438 62 2c 20 22 67 6c 6f 62 22 2c 20 53 51 4c 49 54  b, "glob", SQLIT
24439 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51  E_FUNC_LIKE | SQ
2443a 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3b  LITE_FUNC_CASE);
2443b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61  .  setLikeOptFla
2443c 67 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 0a 20  g(db, "like", . 
2443d 20 20 20 20 20 63 61 73 65 53 65 6e 73 69 74 69       caseSensiti
2443e 76 65 20 3f 20 28 53 51 4c 49 54 45 5f 46 55 4e  ve ? (SQLITE_FUN
2443f 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f  C_LIKE | SQLITE_
24440 46 55 4e 43 5f 43 41 53 45 29 20 3a 20 53 51 4c  FUNC_CASE) : SQL
24441 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3b 0a  ITE_FUNC_LIKE);.
24442 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 70  }../*.** pExpr p
24443 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 78 70 72  oints to an expr
24444 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 6d 70  ession which imp
24445 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69  lements a functi
24446 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73  on.  If.** it is
24447 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20   appropriate to 
24448 61 70 70 6c 79 20 74 68 65 20 4c 49 4b 45 20 6f  apply the LIKE o
24449 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74 6f 20 74  ptimization to t
2444a 68 61 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  hat function.** 
2444b 74 68 65 6e 20 73 65 74 20 61 57 63 5b 30 5d 20  then set aWc[0] 
2444c 74 68 72 6f 75 67 68 20 61 57 63 5b 32 5d 20 74  through aWc[2] t
2444d 6f 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 63  o the wildcard c
2444e 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a 2a 2a  haracters and.**
2444f 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49   return TRUE.  I
24450 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  f the function i
24451 73 20 6e 6f 74 20 61 20 4c 49 4b 45 2d 73 74 79  s not a LIKE-sty
24452 6c 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 65 6e  le function then
24453 0a 2a 2a 20 72 65 74 75 72 6e 20 46 41 4c 53 45  .** return FALSE
24454 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24455 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
24456 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71  sLikeFunction(sq
24457 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
24458 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 49 73  *pExpr, int *pIs
24459 4e 6f 63 61 73 65 2c 20 63 68 61 72 20 2a 61 57  Nocase, char *aW
2445a 63 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  c){.  FuncDef *p
2445b 44 65 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72  Def;.  if( pExpr
2445c 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f  ->op!=TK_FUNCTIO
2445d 4e 20 0a 20 20 20 7c 7c 20 21 70 45 78 70 72 2d  N .   || !pExpr-
2445e 3e 78 2e 70 4c 69 73 74 20 0a 20 20 20 7c 7c 20  >x.pList .   || 
2445f 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
24460 6e 45 78 70 72 21 3d 32 0a 20 20 29 7b 0a 20 20  nExpr!=2.  ){.  
24461 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
24462 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
24463 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
24464 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
24465 29 3b 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69  );.  pDef = sqli
24466 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
24467 64 62 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72  db, (char*)pExpr
24468 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72  ->token.z, pExpr
24469 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 32 2c 0a 20 20  ->token.n, 2,.  
2446a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2446b 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
2446c 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66  E_UTF8, 0);.  if
2446d 28 20 4e 45 56 45 52 28 70 44 65 66 3d 3d 30 29  ( NEVER(pDef==0)
2446e 20 7c 7c 20 28 70 44 65 66 2d 3e 66 6c 61 67 73   || (pDef->flags
2446f 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c   & SQLITE_FUNC_L
24470 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  IKE)==0 ){.    r
24471 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
24472 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 28 29 20  /* The memcpy() 
24473 73 74 61 74 65 6d 65 6e 74 20 61 73 73 75 6d 65  statement assume
24474 73 20 74 68 61 74 20 74 68 65 20 77 69 6c 64 63  s that the wildc
24475 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61  ard characters a
24476 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73  re.  ** the firs
24477 74 20 74 68 72 65 65 20 73 74 61 74 65 6d 65 6e  t three statemen
24478 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 61 72  ts in the compar
24479 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  eInfo structure.
2447a 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 73 65 72    The.  ** asser
2447b 74 73 28 29 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ts() that follow
2447c 20 76 65 72 69 66 79 20 74 68 61 74 20 61 73 73   verify that ass
2447d 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d  umption.  */.  m
2447e 65 6d 63 70 79 28 61 57 63 2c 20 70 44 65 66 2d  emcpy(aWc, pDef-
2447f 3e 70 55 73 65 72 44 61 74 61 2c 20 33 29 3b 0a  >pUserData, 3);.
24480 20 20 61 73 73 65 72 74 28 20 28 63 68 61 72 2a    assert( (char*
24481 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d  )&likeInfoAlt ==
24482 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
24483 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20 29 3b  oAlt.matchAll );
24484 0a 20 20 61 73 73 65 72 74 28 20 26 28 28 63 68  .  assert( &((ch
24485 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  ar*)&likeInfoAlt
24486 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26  )[1] == (char*)&
24487 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63  likeInfoAlt.matc
24488 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 65 72 74  hOne );.  assert
24489 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b 65  ( &((char*)&like
2448a 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20 28  InfoAlt)[2] == (
2448b 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41  char*)&likeInfoA
2448c 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 3b 0a 20  lt.matchSet );. 
2448d 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d 20 28 70   *pIsNocase = (p
2448e 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  Def->flags & SQL
2448f 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3d 3d  ITE_FUNC_CASE)==
24490 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  0;.  return 1;.}
24491 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 20  ../*.** All all 
24492 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 20 73  of the FuncDef s
24493 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65  tructures in the
24494 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20   aBuiltinFunc[] 
24495 61 72 72 61 79 20 61 62 6f 76 65 0a 2a 2a 20 74  array above.** t
24496 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e  o the global fun
24497 63 74 69 6f 6e 20 68 61 73 68 20 74 61 62 6c 65  ction hash table
24498 2e 20 20 54 68 69 73 20 6f 63 63 75 72 73 20 61  .  This occurs a
24499 74 20 73 74 61 72 74 2d 74 69 6d 65 20 28 61 73  t start-time (as
2449a 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 75 65 6e 63  .** a consequenc
2449b 65 20 6f 66 20 63 61 6c 6c 69 6e 67 20 73 71 6c  e of calling sql
2449c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
2449d 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  ))..**.** After 
2449e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e  this routine run
2449f 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  s.*/.SQLITE_PRIV
244a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
244a1 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75  RegisterGlobalFu
244a2 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20  nctions(void){. 
244a3 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c   /*.  ** The fol
244a4 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 68 6f 6c  lowing array hol
244a5 64 73 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  ds FuncDef struc
244a6 74 75 72 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66  tures for all of
244a7 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 20   the functions. 
244a8 20 2a 2a 20 64 65 66 69 6e 65 64 20 69 6e 20 74   ** defined in t
244a9 68 69 73 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20  his file..  **. 
244aa 20 2a 2a 20 54 68 65 20 61 72 72 61 79 20 63 61   ** The array ca
244ab 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 61 6e 74  nnot be constant
244ac 20 73 69 6e 63 65 20 63 68 61 6e 67 65 73 20 61   since changes a
244ad 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 20  re made to the. 
244ae 20 2a 2a 20 46 75 6e 63 44 65 66 2e 70 48 61 73   ** FuncDef.pHas
244af 68 20 65 6c 65 6d 65 6e 74 73 20 61 74 20 73 74  h elements at st
244b0 61 72 74 2d 74 69 6d 65 2e 20 20 54 68 65 20 65  art-time.  The e
244b1 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 69 73 20  lements of this 
244b2 61 72 72 61 79 0a 20 20 2a 2a 20 61 72 65 20 72  array.  ** are r
244b3 65 61 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20 69  ead-only after i
244b4 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 73  nitialization is
244b5 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 20 2a 2f 0a   complete..  */.
244b6 20 20 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f    static SQLITE_
244b7 57 53 44 20 46 75 6e 63 44 65 66 20 61 42 75 69  WSD FuncDef aBui
244b8 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d 20 7b 0a 20  ltinFunc[] = {. 
244b9 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69     FUNCTION(ltri
244ba 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
244bb 31 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e  1, 1, 0, trimFun
244bc 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  c         ),.   
244bd 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d 2c   FUNCTION(ltrim,
244be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
244bf 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20   1, 0, trimFunc 
244c0 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
244c1 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20  UNCTION(rtrim,  
244c2 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 32              1, 2
244c3 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20  , 0, trimFunc   
244c4 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
244c5 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20 20  CTION(rtrim,    
244c6 20 20 20 20 20 20 20 20 20 20 32 2c 20 32 2c 20            2, 2, 
244c7 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
244c8 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
244c9 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20  ION(trim,       
244ca 20 20 20 20 20 20 20 20 31 2c 20 33 2c 20 30 2c          1, 3, 0,
244cb 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
244cc 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
244cd 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20  N(trim,         
244ce 20 20 20 20 20 20 32 2c 20 33 2c 20 30 2c 20 74        2, 3, 0, t
244cf 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20  rimFunc         
244d0 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
244d1 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  min,            
244d2 20 20 20 2d 31 2c 20 30 2c 20 31 2c 20 6d 69 6e     -1, 0, 1, min
244d3 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c  maxFunc       ),
244d4 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69  .    FUNCTION(mi
244d5 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
244d6 20 20 30 2c 20 30 2c 20 31 2c 20 30 20 20 20 20    0, 0, 1, 0    
244d7 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20              ),. 
244d8 20 20 20 41 47 47 52 45 47 41 54 45 28 6d 69 6e     AGGREGATE(min
244d9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
244da 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 53  1, 0, 1, minmaxS
244db 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78  tep,      minMax
244dc 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20  Finalize ),.    
244dd 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20  FUNCTION(max,   
244de 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20              -1, 
244df 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63  1, 1, minmaxFunc
244e0 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
244e1 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20 20  NCTION(max,     
244e2 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 31 2c             0, 1,
244e3 20 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 20   1, 0           
244e4 20 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52       ),.    AGGR
244e5 45 47 41 54 45 28 6d 61 78 2c 20 20 20 20 20 20  EGATE(max,      
244e6 20 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20 31           1, 1, 1
244e7 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20  , minmaxStep,   
244e8 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a     minMaxFinaliz
244e9 65 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f  e ),.    FUNCTIO
244ea 4e 28 74 79 70 65 6f 66 2c 20 20 20 20 20 20 20  N(typeof,       
244eb 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 74        1, 0, 0, t
244ec 79 70 65 6f 66 46 75 6e 63 20 20 20 20 20 20 20  ypeofFunc       
244ed 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
244ee 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 20 20 20  length,         
244ef 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 65 6e      1, 0, 0, len
244f0 67 74 68 46 75 6e 63 20 20 20 20 20 20 20 29 2c  gthFunc       ),
244f1 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75  .    FUNCTION(su
244f2 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  bstr,           
244f3 20 20 32 2c 20 30 2c 20 30 2c 20 73 75 62 73 74    2, 0, 0, subst
244f4 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  rFunc       ),. 
244f5 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73     FUNCTION(subs
244f6 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  tr,             
244f7 33 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46  3, 0, 0, substrF
244f8 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
244f9 20 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c 20 20   FUNCTION(abs,  
244fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
244fb 20 30 2c 20 30 2c 20 61 62 73 46 75 6e 63 20 20   0, 0, absFunc  
244fc 20 20 20 20 20 20 20 20 29 2c 0a 23 69 66 6e 64          ),.#ifnd
244fd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
244fe 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
244ff 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64    FUNCTION(round
24500 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ,              1
24501 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e  , 0, 0, roundFun
24502 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  c        ),.    
24503 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20  FUNCTION(round, 
24504 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20               2, 
24505 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20  0, 0, roundFunc 
24506 20 20 20 20 20 20 20 29 2c 0a 23 65 6e 64 69 66         ),.#endif
24507 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 75 70  .    FUNCTION(up
24508 70 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  per,            
24509 20 20 31 2c 20 30 2c 20 30 2c 20 75 70 70 65 72    1, 0, 0, upper
2450a 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20  Func        ),. 
2450b 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 77 65     FUNCTION(lowe
2450c 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
2450d 31 2c 20 30 2c 20 30 2c 20 6c 6f 77 65 72 46 75  1, 0, 0, lowerFu
2450e 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  nc        ),.   
2450f 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73   FUNCTION(coales
24510 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c  ce,           1,
24511 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20   0, 0, 0        
24512 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
24513 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65  UNCTION(coalesce
24514 2c 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30  ,          -1, 0
24515 2c 20 30 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 20  , 0, ifnullFunc 
24516 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
24517 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20  CTION(coalesce, 
24518 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20            0, 0, 
24519 30 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20  0, 0            
2451a 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
2451b 49 4f 4e 28 68 65 78 2c 20 20 20 20 20 20 20 20  ION(hex,        
2451c 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
2451d 20 68 65 78 46 75 6e 63 20 20 20 20 20 20 20 20   hexFunc        
2451e 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
2451f 4e 28 69 66 6e 75 6c 6c 2c 20 20 20 20 20 20 20  N(ifnull,       
24520 20 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 20 69        2, 0, 1, i
24521 66 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20 20 20  fnullFunc       
24522 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
24523 72 61 6e 64 6f 6d 2c 20 20 20 20 20 20 20 20 20  random,         
24524 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 72 61 6e      0, 0, 0, ran
24525 64 6f 6d 46 75 6e 63 20 20 20 20 20 20 20 29 2c  domFunc       ),
24526 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 61  .    FUNCTION(ra
24527 6e 64 6f 6d 62 6c 6f 62 2c 20 20 20 20 20 20 20  ndomblob,       
24528 20 20 31 2c 20 30 2c 20 30 2c 20 72 61 6e 64 6f    1, 0, 0, rando
24529 6d 42 6c 6f 62 20 20 20 20 20 20 20 29 2c 0a 20  mBlob       ),. 
2452a 20 20 20 46 55 4e 43 54 49 4f 4e 28 6e 75 6c 6c     FUNCTION(null
2452b 69 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  if,             
2452c 32 2c 20 30 2c 20 31 2c 20 6e 75 6c 6c 69 66 46  2, 0, 1, nullifF
2452d 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
2452e 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65   FUNCTION(sqlite
2452f 5f 76 65 72 73 69 6f 6e 2c 20 20 20 20 20 30 2c  _version,     0,
24530 20 30 2c 20 30 2c 20 76 65 72 73 69 6f 6e 46 75   0, 0, versionFu
24531 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  nc      ),.    F
24532 55 4e 43 54 49 4f 4e 28 71 75 6f 74 65 2c 20 20  UNCTION(quote,  
24533 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
24534 2c 20 30 2c 20 71 75 6f 74 65 46 75 6e 63 20 20  , 0, quoteFunc  
24535 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
24536 43 54 49 4f 4e 28 6c 61 73 74 5f 69 6e 73 65 72  CTION(last_inser
24537 74 5f 72 6f 77 69 64 2c 20 20 30 2c 20 30 2c 20  t_rowid,  0, 0, 
24538 30 2c 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  0, last_insert_r
24539 6f 77 69 64 29 2c 0a 20 20 20 20 46 55 4e 43 54  owid),.    FUNCT
2453a 49 4f 4e 28 63 68 61 6e 67 65 73 2c 20 20 20 20  ION(changes,    
2453b 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c          0, 0, 0,
2453c 20 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20   changes        
2453d 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
2453e 4e 28 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 2c  N(total_changes,
2453f 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 74        0, 0, 0, t
24540 6f 74 61 6c 5f 63 68 61 6e 67 65 73 20 20 20 20  otal_changes    
24541 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
24542 72 65 70 6c 61 63 65 2c 20 20 20 20 20 20 20 20  replace,        
24543 20 20 20 20 33 2c 20 30 2c 20 30 2c 20 72 65 70      3, 0, 0, rep
24544 6c 61 63 65 46 75 6e 63 20 20 20 20 20 20 29 2c  laceFunc      ),
24545 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 65  .    FUNCTION(ze
24546 72 6f 62 6c 6f 62 2c 20 20 20 20 20 20 20 20 20  roblob,         
24547 20 20 31 2c 20 30 2c 20 30 2c 20 7a 65 72 6f 62    1, 0, 0, zerob
24548 6c 6f 62 46 75 6e 63 20 20 20 20 20 29 2c 0a 20  lobFunc     ),. 
24549 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53   #ifdef SQLITE_S
2454a 4f 55 4e 44 45 58 0a 20 20 20 20 46 55 4e 43 54  OUNDEX.    FUNCT
2454b 49 4f 4e 28 73 6f 75 6e 64 65 78 2c 20 20 20 20  ION(soundex,    
2454c 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
2454d 20 73 6f 75 6e 64 65 78 46 75 6e 63 20 20 20 20   soundexFunc    
2454e 20 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20    ),.  #endif.  
2454f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
24550 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
24551 4f 4e 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ON.    FUNCTION(
24552 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 2c 20  load_extension, 
24553 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 6f 61      1, 0, 0, loa
24554 64 45 78 74 20 20 20 20 20 20 20 20 20 20 29 2c  dExt          ),
24555 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f  .    FUNCTION(lo
24556 61 64 5f 65 78 74 65 6e 73 69 6f 6e 2c 20 20 20  ad_extension,   
24557 20 20 32 2c 20 30 2c 20 30 2c 20 6c 6f 61 64 45    2, 0, 0, loadE
24558 78 74 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20  xt          ),. 
24559 20 23 65 6e 64 69 66 0a 20 20 20 20 41 47 47 52   #endif.    AGGR
2455a 45 47 41 54 45 28 73 75 6d 2c 20 20 20 20 20 20  EGATE(sum,      
2455b 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
2455c 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20 20  , sumStep,      
2455d 20 20 20 73 75 6d 46 69 6e 61 6c 69 7a 65 20 20     sumFinalize  
2455e 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41    ),.    AGGREGA
2455f 54 45 28 74 6f 74 61 6c 2c 20 20 20 20 20 20 20  TE(total,       
24560 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73        1, 0, 0, s
24561 75 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 20  umStep,         
24562 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 20 20 20  totalFinalize   
24563 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54   ),.    AGGREGAT
24564 45 28 61 76 67 2c 20 20 20 20 20 20 20 20 20 20  E(avg,          
24565 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75       1, 0, 0, su
24566 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 20 61  mStep,         a
24567 76 67 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c  vgFinalize    ),
24568 0a 20 2f 2a 20 41 47 47 52 45 47 41 54 45 28 63  . /* AGGREGATE(c
24569 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20  ount,           
2456a 20 20 30 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74    0, 0, 0, count
2456b 53 74 65 70 2c 20 20 20 20 20 20 20 63 6f 75 6e  Step,       coun
2456c 74 46 69 6e 61 6c 69 7a 65 20 20 29 2c 20 2a 2f  tFinalize  ), */
2456d 0a 20 20 20 20 7b 30 2c 53 51 4c 49 54 45 5f 55  .    {0,SQLITE_U
2456e 54 46 38 2c 53 51 4c 49 54 45 5f 46 55 4e 43 5f  TF8,SQLITE_FUNC_
2456f 43 4f 55 4e 54 2c 30 2c 30 2c 30 2c 63 6f 75 6e  COUNT,0,0,0,coun
24570 74 53 74 65 70 2c 63 6f 75 6e 74 46 69 6e 61 6c  tStep,countFinal
24571 69 7a 65 2c 22 63 6f 75 6e 74 22 2c 30 7d 2c 0a  ize,"count",0},.
24572 20 20 20 20 41 47 47 52 45 47 41 54 45 28 63 6f      AGGREGATE(co
24573 75 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  unt,            
24574 20 31 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53   1, 0, 0, countS
24575 74 65 70 2c 20 20 20 20 20 20 20 63 6f 75 6e 74  tep,       count
24576 46 69 6e 61 6c 69 7a 65 20 20 29 2c 0a 20 20 20  Finalize  ),.   
24577 20 41 47 47 52 45 47 41 54 45 28 67 72 6f 75 70   AGGREGATE(group
24578 5f 63 6f 6e 63 61 74 2c 20 20 20 20 20 20 31 2c  _concat,      1,
24579 20 30 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e 63   0, 0, groupConc
2457a 61 74 53 74 65 70 2c 20 67 72 6f 75 70 43 6f 6e  atStep, groupCon
2457b 63 61 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20 20  catFinalize),.  
2457c 20 20 41 47 47 52 45 47 41 54 45 28 67 72 6f 75    AGGREGATE(grou
2457d 70 5f 63 6f 6e 63 61 74 2c 20 20 20 20 20 20 32  p_concat,      2
2457e 2c 20 30 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e  , 0, 0, groupCon
2457f 63 61 74 53 74 65 70 2c 20 67 72 6f 75 70 43 6f  catStep, groupCo
24580 6e 63 61 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20  ncatFinalize),. 
24581 20 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 67   .    LIKEFUNC(g
24582 6c 6f 62 2c 20 32 2c 20 26 67 6c 6f 62 49 6e 66  lob, 2, &globInf
24583 6f 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  o, SQLITE_FUNC_L
24584 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f  IKE|SQLITE_FUNC_
24585 43 41 53 45 29 2c 0a 20 20 23 69 66 64 65 66 20  CASE),.  #ifdef 
24586 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e 53  SQLITE_CASE_SENS
24587 49 54 49 56 45 5f 4c 49 4b 45 0a 20 20 20 20 4c  ITIVE_LIKE.    L
24588 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c  IKEFUNC(like, 2,
24589 20 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53   &likeInfoAlt, S
2458a 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c  QLITE_FUNC_LIKE|
2458b 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45  SQLITE_FUNC_CASE
2458c 29 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28  ),.    LIKEFUNC(
2458d 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e  like, 3, &likeIn
2458e 66 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55  foAlt, SQLITE_FU
2458f 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46  NC_LIKE|SQLITE_F
24590 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 65 6c  UNC_CASE),.  #el
24591 73 65 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28  se.    LIKEFUNC(
24592 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e  like, 2, &likeIn
24593 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46  foNorm, SQLITE_F
24594 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 20 20 4c  UNC_LIKE),.    L
24595 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c  IKEFUNC(like, 3,
24596 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20   &likeInfoNorm, 
24597 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
24598 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 7d 3b  ),.  #endif.  };
24599 0a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e  ..  int i;.  Fun
2459a 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20  cDefHash *pHash 
2459b 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  = &GLOBAL(FuncDe
2459c 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c  fHash, sqlite3Gl
2459d 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a  obalFunctions);.
2459e 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e 63    FuncDef *aFunc
2459f 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 4c   = (FuncDef*)&GL
245a0 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 42  OBAL(FuncDef, aB
245a1 75 69 6c 74 69 6e 46 75 6e 63 29 3b 0a 0a 20 20  uiltinFunc);..  
245a2 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
245a3 53 69 7a 65 28 61 42 75 69 6c 74 69 6e 46 75 6e  Size(aBuiltinFun
245a4 63 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  c); i++){.    sq
245a5 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65  lite3FuncDefInse
245a6 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e 63  rt(pHash, &aFunc
245a7 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  [i]);.  }.  sqli
245a8 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54  te3RegisterDateT
245a9 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a  imeFunctions();.
245aa 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
245ab 2a 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 2e 63  ** End of func.c
245ac 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
245ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245af 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
245b0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 69 6e  ** Begin file in
245b1 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  sert.c *********
245b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245b4 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
245b5 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
245b6 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
245b7 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
245b8 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
245b9 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
245ba 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
245bb 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
245bc 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
245bd 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
245be 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
245bf 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
245c0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
245c1 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
245c2 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
245c3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
245c4 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
245c5 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
245c6 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
245c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
245cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
245cc 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
245cd 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20  C code routines 
245ce 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20  that are called 
245cf 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  by the parser.**
245d0 20 74 6f 20 68 61 6e 64 6c 65 20 49 4e 53 45 52   to handle INSER
245d1 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20  T statements in 
245d2 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  SQLite..**.** $I
245d3 64 3a 20 69 6e 73 65 72 74 2e 63 2c 76 20 31 2e  d: insert.c,v 1.
245d4 32 36 37 20 32 30 30 39 2f 30 35 2f 30 34 20 31  267 2009/05/04 1
245d5 31 3a 34 32 3a 33 30 20 64 61 6e 69 65 6c 6b 31  1:42:30 danielk1
245d6 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
245d7 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
245d8 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e  e that will open
245d9 20 61 20 74 61 62 6c 65 20 66 6f 72 20 72 65 61   a table for rea
245da 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ding..*/.SQLITE_
245db 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
245dc 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a 20  ite3OpenTable(. 
245dd 20 50 61 72 73 65 20 2a 70 2c 20 20 20 20 20 20   Parse *p,      
245de 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
245df 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45  e into this VDBE
245e0 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20   */.  int iCur, 
245e1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72        /* The cur
245e2 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
245e3 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
245e4 20 69 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20   iDb,        /* 
245e5 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64  The database ind
245e6 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44  ex in sqlite3.aD
245e7 62 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  b[] */.  Table *
245e8 70 54 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 20  pTab,    /* The 
245e9 74 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e  table to be open
245ea 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f  ed */.  int opco
245eb 64 65 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70  de      /* OP_Op
245ec 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65  enRead or OP_Ope
245ed 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 56  nWrite */.){.  V
245ee 64 62 65 20 2a 76 3b 0a 20 20 69 66 28 20 49 73  dbe *v;.  if( Is
245ef 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20  Virtual(pTab) ) 
245f0 72 65 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73 71  return;.  v = sq
245f1 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b  lite3GetVdbe(p);
245f2 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64  .  assert( opcod
245f3 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20  e==OP_OpenWrite 
245f4 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  || opcode==OP_Op
245f5 65 6e 52 65 61 64 20 29 3b 0a 20 20 73 71 6c 69  enRead );.  sqli
245f6 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  te3TableLock(p, 
245f7 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
245f8 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65   (opcode==OP_Ope
245f9 6e 57 72 69 74 65 29 3f 31 3a 30 2c 20 70 54 61  nWrite)?1:0, pTa
245fa 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  b->zName);.  sql
245fb 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
245fc 2c 20 6f 70 63 6f 64 65 2c 20 69 43 75 72 2c 20  , opcode, iCur, 
245fd 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pTab->tnum, iDb)
245fe 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
245ff 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 53  hangeP4(v, -1, S
24600 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
24601 28 70 54 61 62 2d 3e 6e 43 6f 6c 29 2c 20 50 34  (pTab->nCol), P4
24602 5f 49 4e 54 33 32 29 3b 0a 20 20 56 64 62 65 43  _INT32);.  VdbeC
24603 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
24604 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pTab->zName));.
24605 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50 34 20  }../*.** Set P4 
24606 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
24607 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70  ntly inserted op
24608 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e  code to a column
24609 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73 74 72   affinity.** str
2460a 69 6e 67 20 66 6f 72 20 69 6e 64 65 78 20 70 49  ing for index pI
2460b 64 78 2e 20 41 20 63 6f 6c 75 6d 6e 20 61 66 66  dx. A column aff
2460c 69 6e 69 74 79 20 73 74 72 69 6e 67 20 68 61 73  inity string has
2460d 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 0a 2a   one character.*
2460e 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  * for each colum
2460f 6e 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  n in the table, 
24610 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
24611 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
24612 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20   column:.**.**  
24613 43 68 61 72 61 63 74 65 72 20 20 20 20 20 20 43  Character      C
24614 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a  olumn affinity.*
24615 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *  -------------
24616 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24617 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 20 20  -.**  'a'       
24618 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 27 62       TEXT.**  'b
24619 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 4f 4e  '            NON
2461a 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 20 20  E.**  'c'       
2461b 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a 20       NUMERIC.** 
2461c 20 27 64 27 20 20 20 20 20 20 20 20 20 20 20 20   'd'            
2461d 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 27 20  INTEGER.**  'e' 
2461e 20 20 20 20 20 20 20 20 20 20 20 52 45 41 4c 0a             REAL.
2461f 2a 2a 0a 2a 2a 20 41 6e 20 65 78 74 72 61 20 27  **.** An extra '
24620 62 27 20 69 73 20 61 70 70 65 6e 64 65 64 20 74  b' is appended t
24621 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
24622 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 76 65 72   string to cover
24623 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20 74 68   the.** rowid th
24624 61 74 20 61 70 70 65 61 72 73 20 61 73 20 74 68  at appears as th
24625 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 69 6e  e last column in
24626 20 65 76 65 72 79 20 69 6e 64 65 78 2e 0a 2a 2f   every index..*/
24627 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
24628 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 64 65  void sqlite3Inde
24629 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62  xAffinityStr(Vdb
2462a 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70 49 64  e *v, Index *pId
2462b 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64 78 2d  x){.  if( !pIdx-
2462c 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20  >zColAff ){.    
2462d 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  /* The first tim
2462e 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  e a column affin
2462f 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61  ity string for a
24630 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65   particular inde
24631 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 71 75  x is.    ** requ
24632 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f  ired, it is allo
24633 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61  cated and popula
24634 74 65 64 20 68 65 72 65 2e 20 49 74 20 69 73 20  ted here. It is 
24635 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73 0a 20  then stored as. 
24636 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72 20 6f     ** a member o
24637 66 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75  f the Index stru
24638 63 74 75 72 65 20 66 6f 72 20 73 75 62 73 65 71  cture for subseq
24639 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20 2a 2a  uent use..    **
2463a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f 6c 75  .    ** The colu
2463b 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
2463c 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c  ng will eventual
2463d 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 62 79  ly be deleted by
2463e 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 44 65  .    ** sqliteDe
2463f 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68 65 6e  leteIndex() when
24640 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63   the Index struc
24641 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20 63  ture itself is c
24642 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20 75 70  leaned.    ** up
24643 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
24644 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   n;.    Table *p
24645 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  Tab = pIdx->pTab
24646 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  le;.    sqlite3 
24647 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62  *db = sqlite3Vdb
24648 65 44 62 28 76 29 3b 0a 20 20 20 20 70 49 64 78  eDb(v);.    pIdx
24649 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61  ->zColAff = (cha
2464a 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  r *)sqlite3Mallo
2464b 63 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b  c(pIdx->nColumn+
2464c 32 29 3b 0a 20 20 20 20 69 66 28 20 21 70 49 64  2);.    if( !pId
2464d 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20  x->zColAff ){.  
2464e 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
2464f 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
24650 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
24651 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 49 64    for(n=0; n<pId
24652 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29  x->nColumn; n++)
24653 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 7a 43  {.      pIdx->zC
24654 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61 62 2d  olAff[n] = pTab-
24655 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f  >aCol[pIdx->aiCo
24656 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e 69 74  lumn[n]].affinit
24657 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64  y;.    }.    pId
24658 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b 2b 5d 20  x->zColAff[n++] 
24659 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
2465a 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f  E;.    pIdx->zCo
2465b 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d  lAff[n] = 0;.  }
2465c 0a 20 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  . .  sqlite3Vdbe
2465d 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
2465e 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 2c 20 30  pIdx->zColAff, 0
2465f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
24660 50 34 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  P4 of the most r
24661 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64  ecently inserted
24662 20 6f 70 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c   opcode to a col
24663 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20  umn affinity.** 
24664 73 74 72 69 6e 67 20 66 6f 72 20 74 61 62 6c 65  string for table
24665 20 70 54 61 62 2e 20 41 20 63 6f 6c 75 6d 6e 20   pTab. A column 
24666 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
24667 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65  has one characte
24668 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f  r.** for each co
24669 6c 75 6d 6e 20 69 6e 64 65 78 65 64 20 62 79 20  lumn indexed by 
2466a 74 68 65 20 69 6e 64 65 78 2c 20 61 63 63 6f 72  the index, accor
2466b 64 69 6e 67 20 74 6f 20 74 68 65 20 61 66 66 69  ding to the affi
2466c 6e 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63  nity of the.** c
2466d 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20 43 68  olumn:.**.**  Ch
2466e 61 72 61 63 74 65 72 20 20 20 20 20 20 43 6f 6c  aracter      Col
2466f 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20  umn affinity.** 
24670 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
24671 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
24672 2a 2a 20 20 27 61 27 20 20 20 20 20 20 20 20 20  **  'a'         
24673 20 20 20 54 45 58 54 0a 2a 2a 20 20 27 62 27 20     TEXT.**  'b' 
24674 20 20 20 20 20 20 20 20 20 20 20 4e 4f 4e 45 0a             NONE.
24675 2a 2a 20 20 27 63 27 20 20 20 20 20 20 20 20 20  **  'c'         
24676 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a 20 20 27     NUMERIC.**  '
24677 64 27 20 20 20 20 20 20 20 20 20 20 20 20 49 4e  d'            IN
24678 54 45 47 45 52 0a 2a 2a 20 20 27 65 27 20 20 20  TEGER.**  'e'   
24679 20 20 20 20 20 20 20 20 20 52 45 41 4c 0a 2a 2f           REAL.*/
2467a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2467b 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c  void sqlite3Tabl
2467c 65 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62  eAffinityStr(Vdb
2467d 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70 54 61  e *v, Table *pTa
2467e 62 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66 69 72  b){.  /* The fir
2467f 73 74 20 74 69 6d 65 20 61 20 63 6f 6c 75 6d 6e  st time a column
24680 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
24681 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61   for a particula
24682 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69 73 20  r table.  ** is 
24683 72 65 71 75 69 72 65 64 2c 20 69 74 20 69 73 20  required, it is 
24684 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f  allocated and po
24685 70 75 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74  pulated here. It
24686 20 69 73 20 74 68 65 6e 20 0a 20 20 2a 2a 20 73   is then .  ** s
24687 74 6f 72 65 64 20 61 73 20 61 20 6d 65 6d 62 65  tored as a membe
24688 72 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73  r of the Table s
24689 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 75 62  tructure for sub
2468a 73 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 2a  sequent use..  *
2468b 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6c 75 6d  *.  ** The colum
2468c 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  n affinity strin
2468d 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c  g will eventuall
2468e 79 20 62 65 20 64 65 6c 65 74 65 64 20 62 79 0a  y be deleted by.
2468f 20 20 2a 2a 20 73 71 6c 69 74 65 33 44 65 6c 65    ** sqlite3Dele
24690 74 65 54 61 62 6c 65 28 29 20 77 68 65 6e 20 74  teTable() when t
24691 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
24692 72 65 20 69 74 73 65 6c 66 20 69 73 20 63 6c 65  re itself is cle
24693 61 6e 65 64 20 75 70 2e 0a 20 20 2a 2f 0a 20 20  aned up..  */.  
24694 69 66 28 20 21 70 54 61 62 2d 3e 7a 43 6f 6c 41  if( !pTab->zColA
24695 66 66 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  ff ){.    char *
24696 7a 43 6f 6c 41 66 66 3b 0a 20 20 20 20 69 6e 74  zColAff;.    int
24697 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   i;.    sqlite3 
24698 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62  *db = sqlite3Vdb
24699 65 44 62 28 76 29 3b 0a 0a 20 20 20 20 7a 43 6f  eDb(v);..    zCo
2469a 6c 41 66 66 20 3d 20 28 63 68 61 72 20 2a 29 73  lAff = (char *)s
2469b 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 54 61  qlite3Malloc(pTa
2469c 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20 20 20  b->nCol+1);.    
2469d 69 66 28 20 21 7a 43 6f 6c 41 66 66 20 29 7b 0a  if( !zColAff ){.
2469e 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
2469f 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
246a0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
246a1 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
246a2 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
246a3 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 41 66 66 5b  {.      zColAff[
246a4 69 5d 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  i] = pTab->aCol[
246a5 69 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  i].affinity;.   
246a6 20 7d 0a 20 20 20 20 7a 43 6f 6c 41 66 66 5b 70   }.    zColAff[p
246a7 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d 20 27 5c 30  Tab->nCol] = '\0
246a8 27 3b 0a 0a 20 20 20 20 70 54 61 62 2d 3e 7a 43  ';..    pTab->zC
246a9 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c 41 66 66 3b  olAff = zColAff;
246aa 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56  .  }..  sqlite3V
246ab 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
246ac 31 2c 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66  1, pTab->zColAff
246ad 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  , 0);.}../*.** R
246ae 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
246af 66 20 74 68 65 20 74 61 62 6c 65 20 70 54 61 62  f the table pTab
246b0 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
246b1 20 6f 72 20 61 6e 79 20 6f 66 20 69 74 73 20 69   or any of its i
246b2 6e 64 69 63 65 73 0a 2a 2a 20 68 61 76 65 20 62  ndices.** have b
246b3 65 65 6e 20 6f 70 65 6e 65 64 20 61 74 20 61 6e  een opened at an
246b4 79 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 56  y point in the V
246b5 44 42 45 20 70 72 6f 67 72 61 6d 20 62 65 67 69  DBE program begi
246b6 6e 6e 69 6e 67 20 61 74 20 6c 6f 63 61 74 69 6f  nning at locatio
246b7 6e 0a 2a 2a 20 69 53 74 61 72 74 41 64 64 72 20  n.** iStartAddr 
246b8 74 68 72 6f 75 67 68 74 20 74 68 65 20 65 6e 64  throught the end
246b9 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e   of the program.
246ba 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74    This is used t
246bb 6f 20 73 65 65 20 69 66 20 0a 2a 2a 20 61 20 73  o see if .** a s
246bc 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65 20  tatement of the 
246bd 66 6f 72 6d 20 20 22 49 4e 53 45 52 54 20 49 4e  form  "INSERT IN
246be 54 4f 20 3c 69 44 62 2c 20 70 54 61 62 3e 20 53  TO <iDb, pTab> S
246bf 45 4c 45 43 54 20 2e 2e 2e 22 20 63 61 6e 20 0a  ELECT ..." can .
246c0 2a 2a 20 72 75 6e 20 77 69 74 68 6f 75 74 20 75  ** run without u
246c1 73 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74  sing temporary t
246c2 61 62 6c 65 20 66 6f 72 20 74 68 65 20 72 65 73  able for the res
246c3 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45  ults of the SELE
246c4 43 54 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69  CT. .*/.static i
246c5 6e 74 20 72 65 61 64 73 54 61 62 6c 65 28 56 64  nt readsTable(Vd
246c6 62 65 20 2a 76 2c 20 69 6e 74 20 69 53 74 61 72  be *v, int iStar
246c7 74 41 64 64 72 2c 20 69 6e 74 20 69 44 62 2c 20  tAddr, int iDb, 
246c8 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20  Table *pTab){.  
246c9 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 45 6e  int i;.  int iEn
246ca 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  d = sqlite3VdbeC
246cb 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
246cc 20 66 6f 72 28 69 3d 69 53 74 61 72 74 41 64 64   for(i=iStartAdd
246cd 72 3b 20 69 3c 69 45 6e 64 3b 20 69 2b 2b 29 7b  r; i<iEnd; i++){
246ce 0a 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70  .    VdbeOp *pOp
246cf 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65   = sqlite3VdbeGe
246d0 74 4f 70 28 76 2c 20 69 29 3b 0a 20 20 20 20 61  tOp(v, i);.    a
246d1 73 73 65 72 74 28 20 70 4f 70 21 3d 30 20 29 3b  ssert( pOp!=0 );
246d2 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
246d3 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61  code==OP_OpenRea
246d4 64 20 26 26 20 70 4f 70 2d 3e 70 33 3d 3d 69 44  d && pOp->p3==iD
246d5 62 20 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78  b ){.      Index
246d6 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20   *pIndex;.      
246d7 69 6e 74 20 74 6e 75 6d 20 3d 20 70 4f 70 2d 3e  int tnum = pOp->
246d8 70 32 3b 0a 20 20 20 20 20 20 69 66 28 20 74 6e  p2;.      if( tn
246d9 75 6d 3d 3d 70 54 61 62 2d 3e 74 6e 75 6d 20 29  um==pTab->tnum )
246da 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
246db 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
246dc 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70 54 61    for(pIndex=pTa
246dd 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  b->pIndex; pInde
246de 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64 65 78  x; pIndex=pIndex
246df 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
246e0 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70 49 6e 64    if( tnum==pInd
246e1 65 78 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20 20 20  ex->tnum ){.    
246e2 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
246e3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
246e4 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  }.    }.#ifndef 
246e5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
246e6 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28  UALTABLE.    if(
246e7 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
246e8 5f 56 4f 70 65 6e 20 26 26 20 70 4f 70 2d 3e 70  _VOpen && pOp->p
246e9 34 2e 70 56 74 61 62 3d 3d 70 54 61 62 2d 3e 70  4.pVtab==pTab->p
246ea 56 74 61 62 20 29 7b 0a 20 20 20 20 20 20 61 73  Vtab ){.      as
246eb 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 56  sert( pOp->p4.pV
246ec 74 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  tab!=0 );.      
246ed 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
246ee 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a  ype==P4_VTAB );.
246ef 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
246f0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
246f1 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
246f2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
246f3 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
246f4 54 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6f 75  T./*.** Write ou
246f5 74 20 63 6f 64 65 20 74 6f 20 69 6e 69 74 69 61  t code to initia
246f6 6c 69 7a 65 20 74 68 65 20 61 75 74 6f 69 6e 63  lize the autoinc
246f7 72 65 6d 65 6e 74 20 6c 6f 67 69 63 2e 20 20 54  rement logic.  T
246f8 68 69 73 20 63 6f 64 65 0a 2a 2a 20 6c 6f 6f 6b  his code.** look
246f9 73 20 75 70 20 74 68 65 20 63 75 72 72 65 6e 74  s up the current
246fa 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 76   autoincrement v
246fb 61 6c 75 65 20 69 6e 20 74 68 65 20 73 71 6c 69  alue in the sqli
246fc 74 65 5f 73 65 71 75 65 6e 63 65 0a 2a 2a 20 74  te_sequence.** t
246fd 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 73 20  able and stores 
246fe 74 68 61 74 20 76 61 6c 75 65 20 69 6e 20 61 20  that value in a 
246ff 72 65 67 69 73 74 65 72 2e 20 20 43 6f 64 65 20  register.  Code 
24700 67 65 6e 65 72 61 74 65 64 20 62 79 0a 2a 2a 20  generated by.** 
24701 61 75 74 6f 49 6e 63 53 74 65 70 28 29 20 77 69  autoIncStep() wi
24702 6c 6c 20 6b 65 65 70 20 74 68 61 74 20 72 65 67  ll keep that reg
24703 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68  ister holding th
24704 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 72 6f 77  e largest.** row
24705 69 64 20 76 61 6c 75 65 2e 20 20 43 6f 64 65 20  id value.  Code 
24706 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 75 74  generated by aut
24707 6f 49 6e 63 45 6e 64 28 29 20 77 69 6c 6c 20 77  oIncEnd() will w
24708 72 69 74 65 20 74 68 65 20 6e 65 77 0a 2a 2a 20  rite the new.** 
24709 6c 61 72 67 65 73 74 20 76 61 6c 75 65 20 6f 66  largest value of
2470a 20 74 68 65 20 63 6f 75 6e 74 65 72 20 62 61 63   the counter bac
2470b 6b 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74  k into the sqlit
2470c 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
2470d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
2470e 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
2470f 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 65   index of the me
24710 6d 5b 5d 20 63 65 6c 6c 20 74 68 61 74 20 63 6f  m[] cell that co
24711 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 6d 61  ntains.** the ma
24712 78 69 6d 75 6d 20 72 6f 77 69 64 20 63 6f 75 6e  ximum rowid coun
24713 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 65 65  ter..**.** Three
24714 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67   consecutive reg
24715 69 73 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 63  isters are alloc
24716 61 74 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  ated by this rou
24717 74 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 66 69  tine.  The.** fi
24718 72 73 74 20 74 77 6f 20 68 6f 6c 64 20 74 68 65  rst two hold the
24719 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 72   name of the tar
2471a 67 65 74 20 74 61 62 6c 65 20 61 6e 64 20 74 68  get table and th
2471b 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  e maximum rowid 
2471c 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74  .** inserted int
2471d 6f 20 74 68 65 20 74 61 72 67 65 74 20 74 61 62  o the target tab
2471e 6c 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  le, respectively
2471f 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 68  ..** The third h
24720 6f 6c 64 73 20 74 68 65 20 72 6f 77 69 64 20 69  olds the rowid i
24721 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  n sqlite_sequenc
24722 65 20 77 68 65 72 65 20 77 65 20 77 69 6c 6c 0a  e where we will.
24723 2a 2a 20 77 72 69 74 65 20 62 61 63 6b 20 74 68  ** write back th
24724 65 20 72 65 76 69 73 65 64 20 6d 61 78 69 6d 75  e revised maximu
24725 6d 20 72 6f 77 69 64 2e 20 20 54 68 69 73 20 72  m rowid.  This r
24726 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
24727 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74  he.** index of t
24728 68 65 20 73 65 63 6f 6e 64 20 6f 66 20 74 68 65  he second of the
24729 73 65 20 74 68 72 65 65 20 72 65 67 69 73 74 65  se three registe
2472a 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rs..*/.static in
2472b 74 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 0a  t autoIncBegin(.
2472c 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2472d 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2472e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
2472f 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20  t iDb,          
24730 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
24731 65 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69  e database holdi
24732 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 54 61 62  ng pTab */.  Tab
24733 6c 65 20 2a 70 54 61 62 20 20 20 20 20 20 20 20  le *pTab        
24734 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65   /* The table we
24735 20 61 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20   are writing to 
24736 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d 65 6d 49  */.){.  int memI
24737 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 52  d = 0;      /* R
24738 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
24739 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 2a 2f  maximum rowid */
2473a 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62  .  if( pTab->tab
2473b 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69  Flags & TF_Autoi
2473c 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20  ncrement ){.    
2473d 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2473e 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20 44 62 20  ->pVdbe;.    Db 
2473f 2a 70 44 62 20 3d 20 26 70 50 61 72 73 65 2d 3e  *pDb = &pParse->
24740 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20  db->aDb[iDb];.  
24741 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70 50 61    int iCur = pPa
24742 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
24743 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20   int addr;      
24744 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
24745 65 73 73 20 6f 66 20 74 68 65 20 74 6f 70 20 6f  ess of the top o
24746 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  f the loop */.  
24747 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20    assert( v );. 
24748 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b     pParse->nMem+
24749 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  +;         /* Ho
2474a 6c 64 73 20 6e 61 6d 65 20 6f 66 20 74 61 62 6c  lds name of tabl
2474b 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 49 64 20 3d  e */.    memId =
2474c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2474d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
2474e 6d 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  m++;.    sqlite3
2474f 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
24750 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70 44 62  , iCur, iDb, pDb
24751 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54  ->pSchema->pSeqT
24752 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
24753 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ;.    addr = sql
24754 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
24755 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  ddr(v);.    sqli
24756 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
24757 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
24758 6d 65 6d 49 64 2d 31 2c 20 30 2c 20 70 54 61 62  memId-1, 0, pTab
24759 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
2475a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2475b 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
2475c 20 69 43 75 72 2c 20 61 64 64 72 2b 39 29 3b 0a   iCur, addr+9);.
2475d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2475e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
2475f 6d 6e 2c 20 69 43 75 72 2c 20 30 2c 20 6d 65 6d  mn, iCur, 0, mem
24760 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Id);.    sqlite3
24761 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
24762 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c 20 61 64  _Ne, memId-1, ad
24763 64 72 2b 37 2c 20 6d 65 6d 49 64 29 3b 0a 20 20  dr+7, memId);.  
24764 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
24765 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f  ngeP5(v, SQLITE_
24766 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
24767 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24768 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
24769 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a  iCur, memId+1);.
2476a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2476b 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
2476c 6d 6e 2c 20 69 43 75 72 2c 20 31 2c 20 6d 65 6d  mn, iCur, 1, mem
2476d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Id);.    sqlite3
2476e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2476f 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2b 39  _Goto, 0, addr+9
24770 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
24771 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
24772 65 78 74 2c 20 69 43 75 72 2c 20 61 64 64 72 2b  ext, iCur, addr+
24773 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  2);.    sqlite3V
24774 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24775 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65 6d 49  Integer, 0, memI
24776 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
24777 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24778 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30 29 3b  Close, iCur, 0);
24779 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 65  .  }.  return me
2477a 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70  mId;.}../*.** Up
2477b 64 61 74 65 20 74 68 65 20 6d 61 78 69 6d 75 6d  date the maximum
2477c 20 72 6f 77 69 64 20 66 6f 72 20 61 6e 20 61 75   rowid for an au
2477d 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61 6c 63  toincrement calc
2477e 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ulation..**.** T
2477f 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
24780 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65  ld be called whe
24781 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
24782 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61 0a 2a   stack holds a.*
24783 2a 20 6e 65 77 20 72 6f 77 69 64 20 74 68 61 74  * new rowid that
24784 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20   is about to be 
24785 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74 68  inserted.  If th
24786 61 74 20 6e 65 77 20 72 6f 77 69 64 20 69 73 0a  at new rowid is.
24787 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  ** larger than t
24788 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  he maximum rowid
24789 20 69 6e 20 74 68 65 20 6d 65 6d 49 64 20 6d 65   in the memId me
2478a 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65 6e 20  mory cell, then 
2478b 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65  the.** memory ce
2478c 6c 6c 20 69 73 20 75 70 64 61 74 65 64 2e 20 20  ll is updated.  
2478d 54 68 65 20 73 74 61 63 6b 20 69 73 20 75 6e 63  The stack is unc
2478e 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  hanged..*/.stati
2478f 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63 53 74  c void autoIncSt
24790 65 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ep(Parse *pParse
24791 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69 6e 74  , int memId, int
24792 20 72 65 67 52 6f 77 69 64 29 7b 0a 20 20 69 66   regRowid){.  if
24793 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20 20 20  ( memId>0 ){.   
24794 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24795 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  p2(pParse->pVdbe
24796 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d 65 6d  , OP_MemMax, mem
24797 49 64 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  Id, regRowid);. 
24798 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 66 74 65   }.}../*.** Afte
24799 72 20 64 6f 69 6e 67 20 6f 6e 65 20 6f 72 20 6d  r doing one or m
2479a 6f 72 65 20 69 6e 73 65 72 74 73 2c 20 74 68 65  ore inserts, the
2479b 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 69   maximum rowid i
2479c 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 72  s stored.** in r
2479d 65 67 5b 6d 65 6d 49 64 5d 2e 20 20 47 65 6e 65  eg[memId].  Gene
2479e 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72 69  rate code to wri
2479f 74 65 20 74 68 69 73 20 76 61 6c 75 65 20 62 61  te this value ba
247a0 63 6b 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74  ck into the.** t
247a1 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
247a2 63 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ce table..*/.sta
247a3 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63  tic void autoInc
247a4 45 6e 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50  End(.  Parse *pP
247a5 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 54 68 65  arse,     /* The
247a6 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
247a7 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20   */.  int iDb,  
247a8 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
247a9 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
247aa 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
247ab 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
247ac 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
247ad 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67  we are inserting
247ae 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6d   into */.  int m
247af 65 6d 49 64 20 20 20 20 20 20 20 20 20 20 2f 2a  emId          /*
247b0 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 68 6f 6c   Memory cell hol
247b1 64 69 6e 67 20 74 68 65 20 6d 61 78 69 6d 75 6d  ding the maximum
247b2 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69   rowid */.){.  i
247b3 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  f( pTab->tabFlag
247b4 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65  s & TF_Autoincre
247b5 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  ment ){.    int 
247b6 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iCur = pParse->n
247b7 54 61 62 2b 2b 3b 0a 20 20 20 20 56 64 62 65 20  Tab++;.    Vdbe 
247b8 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
247b9 62 65 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20  be;.    Db *pDb 
247ba 3d 20 26 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61  = &pParse->db->a
247bb 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74  Db[iDb];.    int
247bc 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20 69 52 65   j1;.    int iRe
247bd 63 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  c = ++pParse->nM
247be 65 6d 3b 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79  em;    /* Memory
247bf 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 72   cell used for r
247c0 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 20 20 61 73  ecord */..    as
247c1 73 65 72 74 28 20 76 20 29 3b 0a 20 20 20 20 73  sert( v );.    s
247c2 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
247c3 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44  pParse, iCur, iD
247c4 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  b, pDb->pSchema-
247c5 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65  >pSeqTab, OP_Ope
247c6 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 6a 31 20  nWrite);.    j1 
247c7 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
247c8 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  Op1(v, OP_NotNul
247c9 6c 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20  l, memId+1);.   
247ca 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
247cb 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
247cc 64 2c 20 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31  d, iCur, memId+1
247cd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
247ce 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
247cf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
247d0 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
247d1 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64  akeRecord, memId
247d2 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20 20  -1, 2, iRec);.  
247d3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
247d4 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
247d5 2c 20 69 43 75 72 2c 20 69 52 65 63 2c 20 6d 65  , iCur, iRec, me
247d6 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  mId+1);.    sqli
247d7 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
247d8 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  v, OPFLAG_APPEND
247d9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
247da 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
247db 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d  lose, iCur);.  }
247dc 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49  .}.#else./*.** I
247dd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
247de 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64  TOINCREMENT is d
247df 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
247e0 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a   three routines.
247e1 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c  ** above are all
247e2 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66   no-ops.*/.# def
247e3 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  ine autoIncBegin
247e4 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65  (A,B,C) (0).# de
247e5 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70  fine autoIncStep
247e6 28 41 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e 65  (A,B,C).# define
247e7 20 61 75 74 6f 49 6e 63 45 6e 64 28 41 2c 42 2c   autoIncEnd(A,B,
247e8 43 2c 44 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53  C,D).#endif /* S
247e9 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
247ea 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a  NCREMENT */.../*
247eb 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
247ec 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69  tion */.static i
247ed 6e 74 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74  nt xferOptimizat
247ee 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
247ef 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
247f0 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
247f1 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74  /.  Table *pDest
247f2 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
247f3 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e   table we are in
247f4 73 65 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a  serting into */.
247f5 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
247f6 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c  t,      /* A SEL
247f7 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
247f8 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61   use as the data
247f9 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74   source */.  int
247fa 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20   onError,       
247fb 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e     /* How to han
247fc 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  dle constraint e
247fd 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69  rrors */.  int i
247fe 44 62 44 65 73 74 20 20 20 20 20 20 20 20 20 20  DbDest          
247ff 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
24800 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a   of pDest */.);.
24801 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
24802 69 6e 65 20 69 73 20 63 61 6c 6c 20 74 6f 20 68  ine is call to h
24803 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20 74 68 65  andle SQL of the
24804 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73   following forms
24805 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72  :.**.**    inser
24806 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44  t into TABLE (ID
24807 4c 49 53 54 29 20 76 61 6c 75 65 73 28 45 58 50  LIST) values(EXP
24808 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73  RLIST).**    ins
24809 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28  ert into TABLE (
2480a 49 44 4c 49 53 54 29 20 73 65 6c 65 63 74 0a 2a  IDLIST) select.*
2480b 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49 53 54 20  *.** The IDLIST 
2480c 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 61  following the ta
2480d 62 6c 65 20 6e 61 6d 65 20 69 73 20 61 6c 77 61  ble name is alwa
2480e 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66  ys optional.  If
2480f 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65   omitted,.** the
24810 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
24811 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20  columns for the 
24812 74 61 62 6c 65 20 69 73 20 73 75 62 73 74 69 74  table is substit
24813 75 74 65 64 2e 20 20 54 68 65 20 49 44 4c 49 53  uted.  The IDLIS
24814 54 0a 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20  T.** appears in 
24815 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61  the pColumn para
24816 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20  meter.  pColumn 
24817 69 73 20 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53  is NULL if IDLIS
24818 54 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a  T is omitted..**
24819 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74 20 70 61  .** The pList pa
2481a 72 61 6d 65 74 65 72 20 68 6f 6c 64 73 20 45 58  rameter holds EX
2481b 50 52 4c 49 53 54 20 69 6e 20 74 68 65 20 66 69  PRLIST in the fi
2481c 72 73 74 20 66 6f 72 6d 20 6f 66 20 74 68 65 20  rst form of the 
2481d 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d  INSERT.** statem
2481e 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e 64 20 70  ent above, and p
2481f 53 65 6c 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20  Select is NULL. 
24820 20 46 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20   For the second 
24821 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a  form, pList is.*
24822 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65  * NULL and pSele
24823 63 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ct is a pointer 
24824 74 6f 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  to the select st
24825 61 74 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20  atement used to 
24826 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64 61 74 61  generate.** data
24827 20 66 6f 72 20 74 68 65 20 69 6e 73 65 72 74 2e   for the insert.
24828 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
24829 67 65 6e 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77  generated follow
2482a 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72 20 74 65  s one of four te
2482b 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72 20 61 20  mplates.  For a 
2482c 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74  simple.** select
2482d 20 77 69 74 68 20 64 61 74 61 20 63 6f 6d 69 6e   with data comin
2482e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55 45 53 20  g from a VALUES 
2482f 63 6c 61 75 73 65 2c 20 74 68 65 20 63 6f 64 65  clause, the code
24830 20 65 78 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63   executes.** onc
24831 65 20 73 74 72 61 69 67 68 74 20 64 6f 77 6e 20  e straight down 
24832 74 68 72 6f 75 67 68 2e 20 20 50 73 65 75 64 6f  through.  Pseudo
24833 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 28 77  -code follows (w
24834 65 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 74  e call this.** t
24835 68 65 20 22 31 73 74 20 74 65 6d 70 6c 61 74 65  he "1st template
24836 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  "):.**.**       
24837 20 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72    open write cur
24838 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61  sor to <table> a
24839 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a  nd its indices.*
2483a 2a 20 20 20 20 20 20 20 20 20 70 75 74 73 20 56  *         puts V
2483b 41 4c 55 45 53 20 63 6c 61 75 73 65 20 65 78 70  ALUES clause exp
2483c 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f 20 74 68  ressions onto th
2483d 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20 20 20 20  e stack.**      
2483e 20 20 20 77 72 69 74 65 20 74 68 65 20 72 65 73     write the res
2483f 75 6c 74 69 6e 67 20 72 65 63 6f 72 64 20 69 6e  ulting record in
24840 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  to <table>.**   
24841 20 20 20 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2a        cleanup.**
24842 0a 2a 2a 20 54 68 65 20 74 68 72 65 65 20 72 65  .** The three re
24843 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c 61 74 65  maining template
24844 73 20 61 73 73 75 6d 65 20 74 68 65 20 73 74 61  s assume the sta
24845 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65  tement is of the
24846 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 49 4e   form.**.**   IN
24847 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65  SERT INTO <table
24848 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a 2a 2a 0a  > SELECT ....**.
24849 2a 2a 20 49 66 20 74 68 65 20 53 45 4c 45 43 54  ** If the SELECT
2484a 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20 74 68   clause is of th
2484b 65 20 72 65 73 74 72 69 63 74 65 64 20 66 6f 72  e restricted for
2484c 6d 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  m "SELECT * FROM
2484d 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a 2a 2a 20   <table2>" -.** 
2484e 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69  in other words i
2484f 66 20 74 68 65 20 53 45 4c 45 43 54 20 70 75 6c  f the SELECT pul
24850 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66  ls all columns f
24851 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62  rom a single tab
24852 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 72 65 20  le.** and there 
24853 69 73 20 6e 6f 20 57 48 45 52 45 20 6f 72 20 4c  is no WHERE or L
24854 49 4d 49 54 20 6f 72 20 47 52 4f 55 50 20 42 59  IMIT or GROUP BY
24855 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61   or ORDER BY cla
24856 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20 69 66 20  uses, and.** if 
24857 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20 3c 74 61  <table2> and <ta
24858 62 6c 65 31 3e 20 61 72 65 20 64 69 73 74 69 6e  ble1> are distin
24859 63 74 20 74 61 62 6c 65 73 20 62 75 74 20 68 61  ct tables but ha
2485a 76 65 20 69 64 65 6e 74 69 63 61 6c 0a 2a 2a 20  ve identical.** 
2485b 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c 75 64 69  schemas, includi
2485c 6e 67 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 20  ng all the same 
2485d 69 6e 64 69 63 65 73 2c 20 74 68 65 6e 20 61 20  indices, then a 
2485e 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61  special optimiza
2485f 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  tion.** is invok
24860 65 64 20 74 68 61 74 20 63 6f 70 69 65 73 20 72  ed that copies r
24861 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20  aw records from 
24862 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f  <table2> over to
24863 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a 20 53 65   <table1>..** Se
24864 65 20 74 68 65 20 78 66 65 72 4f 70 74 69 6d 69  e the xferOptimi
24865 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f  zation() functio
24866 6e 20 66 6f 72 20 74 68 65 20 69 6d 70 6c 65 6d  n for the implem
24867 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  entation of this
24868 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e 20 20 54  .** template.  T
24869 68 69 73 20 69 73 20 74 68 65 20 32 6e 64 20 74  his is the 2nd t
2486a 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20  emplate..**.**  
2486b 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72         open a wr
2486c 69 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74  ite cursor to <t
2486d 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20  able>.**        
2486e 20 6f 70 65 6e 20 72 65 61 64 20 63 75 72 73 6f   open read curso
2486f 72 20 6f 6e 20 3c 74 61 62 6c 65 32 3e 0a 2a 2a  r on <table2>.**
24870 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65           transfe
24871 72 20 61 6c 6c 20 72 65 63 6f 72 64 73 20 69 6e  r all records in
24872 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74   <table2> over t
24873 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20  o <table>.**    
24874 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f       close curso
24875 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 6f  rs.**         fo
24876 72 65 61 63 68 20 69 6e 64 65 78 20 6f 6e 20 3c  reach index on <
24877 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20  table>.**       
24878 20 20 20 20 6f 70 65 6e 20 61 20 77 72 69 74 65      open a write
24879 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 3c   cursor on the <
2487a 74 61 62 6c 65 3e 20 69 6e 64 65 78 0a 2a 2a 20  table> index.** 
2487b 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61            open a
2487c 20 72 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20   read cursor on 
2487d 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
2487e 67 20 3c 74 61 62 6c 65 32 3e 20 69 6e 64 65 78  g <table2> index
2487f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72  .**           tr
24880 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72  ansfer all recor
24881 64 73 20 66 72 6f 6d 20 74 68 65 20 72 65 61 64  ds from the read
24882 20 74 6f 20 74 68 65 20 77 72 69 74 65 20 63 75   to the write cu
24883 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
24884 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73     close cursors
24885 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20  .**         end 
24886 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a 2a 20 54 68  foreach.**.** Th
24887 65 20 33 72 64 20 74 65 6d 70 6c 61 74 65 20 69  e 3rd template i
24888 73 20 66 6f 72 20 77 68 65 6e 20 74 68 65 20 73  s for when the s
24889 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74 65 20 64  econd template d
2488a 6f 65 73 20 6e 6f 74 20 61 70 70 6c 79 0a 2a 2a  oes not apply.**
2488b 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43 54 20   and the SELECT 
2488c 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20  clause does not 
2488d 72 65 61 64 20 66 72 6f 6d 20 3c 74 61 62 6c 65  read from <table
2488e 3e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a  > at any time..*
2488f 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65 64 20  * The generated 
24890 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 74 68 69  code follows thi
24891 73 20 74 65 6d 70 6c 61 74 65 3a 0a 2a 2a 0a 2a  s template:.**.*
24892 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d  *         EOF <-
24893 20 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20   0.**         X 
24894 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20  <- A.**         
24895 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41  goto B.**      A
24896 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 20  : setup for the 
24897 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
24898 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20    loop over the 
24899 72 6f 77 73 20 69 6e 20 74 68 65 20 53 45 4c 45  rows in the SELE
2489a 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
2489b 6c 6f 61 64 20 76 61 6c 75 65 73 20 69 6e 74 6f  load values into
2489c 20 72 65 67 69 73 74 65 72 73 20 52 2e 2e 52 2b   registers R..R+
2489d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79  n.**           y
2489e 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20  ield X.**       
2489f 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20    end loop.**   
248a0 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66        cleanup af
248a1 74 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a  ter the SELECT.*
248a2 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d  *         EOF <-
248a3 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69   1.**         yi
248a4 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  eld X.**        
248a5 20 67 6f 74 6f 20 41 0a 2a 2a 20 20 20 20 20 20   goto A.**      
248a6 42 3a 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75  B: open write cu
248a7 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20  rsor to <table> 
248a8 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a  and its indices.
248a9 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65 6c 64  **      C: yield
248aa 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66   X.**         if
248ab 20 45 4f 46 20 67 6f 74 6f 20 44 0a 2a 2a 20 20   EOF goto D.**  
248ac 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74 68         insert th
248ad 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20  e select result 
248ae 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f  into <table> fro
248af 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20  m R..R+n.**     
248b0 20 20 20 20 67 6f 74 6f 20 43 0a 2a 2a 20 20 20      goto C.**   
248b1 20 20 20 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2a     D: cleanup.**
248b2 0a 2a 2a 20 54 68 65 20 34 74 68 20 74 65 6d 70  .** The 4th temp
248b3 6c 61 74 65 20 69 73 20 75 73 65 64 20 69 66 20  late is used if 
248b4 74 68 65 20 69 6e 73 65 72 74 20 73 74 61 74 65  the insert state
248b5 6d 65 6e 74 20 74 61 6b 65 73 20 69 74 73 0a 2a  ment takes its.*
248b6 2a 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 61 20  * values from a 
248b7 53 45 4c 45 43 54 20 62 75 74 20 74 68 65 20 64  SELECT but the d
248b8 61 74 61 20 69 73 20 62 65 69 6e 67 20 69 6e 73  ata is being ins
248b9 65 72 74 65 64 20 69 6e 74 6f 20 61 20 74 61 62  erted into a tab
248ba 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 6c  le.** that is al
248bb 73 6f 20 72 65 61 64 20 61 73 20 70 61 72 74 20  so read as part 
248bc 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20 20  of the SELECT.  
248bd 49 6e 20 74 68 65 20 74 68 69 72 64 20 66 6f 72  In the third for
248be 6d 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f  m,.** we have to
248bf 20 75 73 65 20 61 20 69 6e 74 65 72 6d 65 64 69   use a intermedi
248c0 61 74 65 20 74 61 62 6c 65 20 74 6f 20 73 74 6f  ate table to sto
248c1 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  re the results o
248c2 66 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 2e  f.** the select.
248c3 20 20 54 68 65 20 74 65 6d 70 6c 61 74 65 20 69    The template i
248c4 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
248c5 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c  **         EOF <
248c6 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 58  - 0.**         X
248c7 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20 20   <- A.**        
248c8 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20   goto B.**      
248c9 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65  A: setup for the
248ca 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
248cb 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65     loop over the
248cc 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53   tables in the S
248cd 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
248ce 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e     load value in
248cf 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52  to register R..R
248d0 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  +n.**           
248d1 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
248d2 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20     end loop.**  
248d3 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61         cleanup a
248d4 66 74 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a  fter the SELECT.
248d5 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c  **         EOF <
248d6 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 79  - 1.**         y
248d7 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20  ield X.**       
248d8 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a 2a 2a 20    halt-error.** 
248d9 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d       B: open tem
248da 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20  p table.**      
248db 4c 3a 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20  L: yield X.**   
248dc 20 20 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74        if EOF got
248dd 6f 20 4d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69  o M.**         i
248de 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52  nsert row from R
248df 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20  ..R+n into temp 
248e0 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20  table.**        
248e1 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20 20 20 20 20   goto L.**      
248e2 4d 3a 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75  M: open write cu
248e3 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20  rsor to <table> 
248e4 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a  and its indices.
248e5 2a 2a 20 20 20 20 20 20 20 20 20 72 65 77 69 6e  **         rewin
248e6 64 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20  d temp table.** 
248e7 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65       C: loop ove
248e8 72 20 72 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d  r rows of interm
248e9 65 64 69 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20  ediate table.** 
248ea 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66            transf
248eb 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69  er values form i
248ec 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
248ed 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  e into <table>.*
248ee 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f  *         end lo
248ef 6f 70 0a 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c  op.**      D: cl
248f0 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eanup.*/.SQLITE_
248f1 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
248f2 69 74 65 33 49 6e 73 65 72 74 28 0a 20 20 50 61  ite3Insert(.  Pa
248f3 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
248f4 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
248f5 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
248f6 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20  st *pTabList,   
248f7 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c   /* Name of tabl
248f8 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20  e into which we 
248f9 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f  are inserting */
248fa 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
248fb 73 74 2c 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  st,      /* List
248fc 20 6f 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65   of values to be
248fd 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53   inserted */.  S
248fe 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
248ff 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54       /* A SELECT
24900 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73   statement to us
24901 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f  e as the data so
24902 75 72 63 65 20 2a 2f 0a 20 20 49 64 4c 69 73 74  urce */.  IdList
24903 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20   *pColumn,      
24904 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  /* Column names 
24905 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
24906 20 49 44 4c 49 53 54 2e 20 2a 2f 0a 20 20 69 6e   IDLIST. */.  in
24907 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 20 20 20  t onError       
24908 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61      /* How to ha
24909 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20  ndle constraint 
2490a 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 73  errors */.){.  s
2490b 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
2490c 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e       /* The main
2490d 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
2490e 75 72 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ure */.  Table *
2490f 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f  pTab;          /
24910 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 69  * The table to i
24911 6e 73 65 72 74 20 69 6e 74 6f 2e 20 20 61 6b 61  nsert into.  aka
24912 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 68 61 72   TABLE */.  char
24913 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20 20 20   *zTab;         
24914 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
24915 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
24916 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
24917 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ng */.  const ch
24918 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 2f 2a  ar *zDb;      /*
24919 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
2491a 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68  abase holding th
2491b 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  is table */.  in
2491c 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20 20 20 20  t i, j, idx;    
2491d 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2491e 74 65 72 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a  ters */.  Vdbe *
2491f 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
24920 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
24921 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75   into this virtu
24922 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20  al machine */.  
24923 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
24924 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
24925 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65  ping over indice
24926 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  s of the table *
24927 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
24928 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
24929 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
2492a 6e 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20  n the data */.  
2492b 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b  int nHidden = 0;
2492c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2492d 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e  of hidden column
2492e 73 20 69 66 20 54 41 42 4c 45 20 69 73 20 76 69  s if TABLE is vi
2492f 72 74 75 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 62  rtual */.  int b
24930 61 73 65 43 75 72 20 3d 20 30 3b 20 20 20 20 20  aseCur = 0;     
24931 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20   /* VDBE Cursor 
24932 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62 20  number for pTab 
24933 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 43 6f 6c 75  */.  int keyColu
24934 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 43 6f  mn = -1;   /* Co
24935 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 65  lumn that is the
24936 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
24937 20 4b 45 59 20 2a 2f 0a 20 20 69 6e 74 20 65 6e   KEY */.  int en
24938 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20  dOfLoop;        
24939 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65  /* Label for the
2493a 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 73 65   end of the inse
2493b 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20  rtion loop */.  
2493c 69 6e 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65  int useTempTable
2493d 20 3d 20 30 3b 20 2f 2a 20 53 74 6f 72 65 20 53   = 0; /* Store S
2493e 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20 69 6e  ELECT results in
2493f 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61   intermediate ta
24940 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63  ble */.  int src
24941 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  Tab = 0;       /
24942 2a 20 44 61 74 61 20 63 6f 6d 65 73 20 66 72 6f  * Data comes fro
24943 6d 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79  m this temporary
24944 20 63 75 72 73 6f 72 20 69 66 20 3e 3d 30 20 2a   cursor if >=0 *
24945 2f 0a 20 20 69 6e 74 20 61 64 64 72 49 6e 73 54  /.  int addrInsT
24946 6f 70 20 3d 20 30 3b 20 20 20 2f 2a 20 4a 75 6d  op = 0;   /* Jum
24947 70 20 74 6f 20 6c 61 62 65 6c 20 22 44 22 20 2a  p to label "D" *
24948 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74  /.  int addrCont
24949 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 6f 70   = 0;     /* Top
2494a 20 6f 66 20 69 6e 73 65 72 74 20 6c 6f 6f 70 2e   of insert loop.
2494b 20 4c 61 62 65 6c 20 22 43 22 20 69 6e 20 74 65   Label "C" in te
2494c 6d 70 6c 61 74 65 73 20 33 20 61 6e 64 20 34 20  mplates 3 and 4 
2494d 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
2494e 65 63 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41 64  ect = 0;   /* Ad
2494f 64 72 65 73 73 20 6f 66 20 63 6f 72 6f 75 74 69  dress of corouti
24950 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ne that implemen
24951 74 73 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f  ts the SELECT */
24952 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
24953 73 74 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74  st;      /* Dest
24954 69 6e 61 74 69 6f 6e 20 66 6f 72 20 53 45 4c 45  ination for SELE
24955 43 54 20 6f 6e 20 72 68 73 20 6f 66 20 49 4e 53  CT on rhs of INS
24956 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77  ERT */.  int new
24957 49 64 78 20 3d 20 2d 31 3b 20 20 20 20 20 20 2f  Idx = -1;      /
24958 2a 20 43 75 72 73 6f 72 20 66 6f 72 20 74 68 65  * Cursor for the
24959 20 4e 45 57 20 70 73 65 75 64 6f 2d 74 61 62 6c   NEW pseudo-tabl
2495a 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  e */.  int iDb; 
2495b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2495c 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73  Index of databas
2495d 65 20 68 6f 6c 64 69 6e 67 20 54 41 42 4c 45 20  e holding TABLE 
2495e 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20  */.  Db *pDb;   
2495f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
24960 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  e database conta
24961 69 6e 69 6e 67 20 74 61 62 6c 65 20 62 65 69 6e  ining table bein
24962 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
24963 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 46  */.  int appendF
24964 6c 61 67 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72  lag = 0;   /* Tr
24965 75 65 20 69 66 20 74 68 65 20 69 6e 73 65 72 74  ue if the insert
24966 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65   is likely to be
24967 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 20   an append */.. 
24968 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c   /* Register all
24969 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ocations */.  in
2496a 74 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20  t regFromSelect 
2496b 3d 20 30 3b 2f 2a 20 42 61 73 65 20 72 65 67 69  = 0;/* Base regi
2496c 73 74 65 72 20 66 6f 72 20 64 61 74 61 20 63 6f  ster for data co
2496d 6d 69 6e 67 20 66 72 6f 6d 20 53 45 4c 45 43 54  ming from SELECT
2496e 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74   */.  int regAut
2496f 6f 69 6e 63 20 3d 20 30 3b 20 20 20 2f 2a 20 52  oinc = 0;   /* R
24970 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
24971 74 68 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  the AUTOINCREMEN
24972 54 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  T counter */.  i
24973 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d  nt regRowCount =
24974 20 30 3b 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63   0;  /* Memory c
24975 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 74 68 65  ell used for the
24976 20 72 6f 77 20 63 6f 75 6e 74 65 72 20 2a 2f 0a   row counter */.
24977 20 20 69 6e 74 20 72 65 67 49 6e 73 3b 20 20 20    int regIns;   
24978 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b          /* Block
24979 20 6f 66 20 72 65 67 73 20 68 6f 6c 64 69 6e 67   of regs holding
2497a 20 72 6f 77 69 64 2b 64 61 74 61 20 62 65 69 6e   rowid+data bein
2497b 67 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  g inserted */.  
2497c 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20  int regRowid;   
2497d 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65        /* registe
2497e 72 73 20 68 6f 6c 64 69 6e 67 20 69 6e 73 65 72  rs holding inser
2497f 74 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  t rowid */.  int
24980 20 72 65 67 44 61 74 61 3b 20 20 20 20 20 20 20   regData;       
24981 20 20 20 2f 2a 20 72 65 67 69 73 74 65 72 20 68     /* register h
24982 6f 6c 64 69 6e 67 20 66 69 72 73 74 20 63 6f 6c  olding first col
24983 75 6d 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f  umn to insert */
24984 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64  .  int regRecord
24985 3b 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 6c 64  ;        /* Hold
24986 73 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64  s the assemblied
24987 20 72 6f 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20   row record */. 
24988 20 69 6e 74 20 72 65 67 45 6f 66 20 3d 20 30 3b   int regEof = 0;
24989 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
2498a 65 72 20 72 65 63 6f 72 64 69 6e 67 20 65 6e 64  er recording end
2498b 20 6f 66 20 53 45 4c 45 43 54 20 64 61 74 61 20   of SELECT data 
2498c 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64  */.  int *aRegId
2498d 78 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4f 6e  x = 0;     /* On
2498e 65 20 72 65 67 69 73 74 65 72 20 61 6c 6c 6f 63  e register alloc
2498f 61 74 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64  ated to each ind
24990 65 78 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20  ex */...#ifndef 
24991 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
24992 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77  GER.  int isView
24993 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
24994 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74    /* True if att
24995 65 6d 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72  empting to inser
24996 74 20 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f  t into a view */
24997 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
24998 67 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f  gger;          /
24999 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65  * List of trigge
2499a 72 73 20 6f 6e 20 70 54 61 62 2c 20 69 66 20 72  rs on pTab, if r
2499b 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74  equired */.  int
2499c 20 74 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20   tmask;         
2499d 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
2499e 20 6f 66 20 74 72 69 67 67 65 72 20 74 69 6d 65   of trigger time
2499f 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 64  s */.#endif..  d
249a0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
249a1 20 20 6d 65 6d 73 65 74 28 26 64 65 73 74 2c 20    memset(&dest, 
249a2 30 2c 20 73 69 7a 65 6f 66 28 64 65 73 74 29 29  0, sizeof(dest))
249a3 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
249a4 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
249a5 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
249a6 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
249a7 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c  nup;.  }..  /* L
249a8 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  ocate the table 
249a9 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 77 69  into which we wi
249aa 6c 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20  ll be inserting 
249ab 6e 65 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  new information.
249ac 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
249ad 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pTabList->nSrc==
249ae 31 20 29 3b 0a 20 20 7a 54 61 62 20 3d 20 70 54  1 );.  zTab = pT
249af 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 7a 4e 61  abList->a[0].zNa
249b0 6d 65 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  me;.  if( NEVER(
249b1 7a 54 61 62 3d 3d 30 29 20 29 20 67 6f 74 6f 20  zTab==0) ) goto 
249b2 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
249b3 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
249b4 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50  SrcListLookup(pP
249b5 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b  arse, pTabList);
249b6 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
249b7 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  {.    goto inser
249b8 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  t_cleanup;.  }. 
249b9 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
249ba 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
249bb 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
249bc 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
249bd 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 44 62 20 3d  ->nDb );.  pDb =
249be 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
249bf 20 20 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61    zDb = pDb->zNa
249c0 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  me;.  if( sqlite
249c1 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
249c2 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  e, SQLITE_INSERT
249c3 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30  , pTab->zName, 0
249c4 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f  , zDb) ){.    go
249c5 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
249c6 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67  p;.  }..  /* Fig
249c7 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 68 61  ure out if we ha
249c8 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 73 20  ve any triggers 
249c9 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c 65  and if the table
249ca 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 73 65   being.  ** inse
249cb 72 74 65 64 20 69 6e 74 6f 20 69 73 20 61 20 76  rted into is a v
249cc 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  iew.  */.#ifndef
249cd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
249ce 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72 20  GGER.  pTrigger 
249cf 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
249d0 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70  sExist(pParse, p
249d1 54 61 62 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  Tab, TK_INSERT, 
249d2 30 2c 20 26 74 6d 61 73 6b 29 3b 0a 20 20 69 73  0, &tmask);.  is
249d3 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65  View = pTab->pSe
249d4 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23  lect!=0;.#else.#
249d5 20 64 65 66 69 6e 65 20 70 54 72 69 67 67 65 72   define pTrigger
249d6 20 30 0a 23 20 64 65 66 69 6e 65 20 74 6d 61 73   0.# define tmas
249d7 6b 20 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56  k 0.# define isV
249d8 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23 69 66  iew 0.#endif.#if
249d9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
249da 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56  VIEW.# undef isV
249db 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56  iew.# define isV
249dc 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 20 20 61  iew 0.#endif.  a
249dd 73 73 65 72 74 28 20 28 70 54 72 69 67 67 65 72  ssert( (pTrigger
249de 20 26 26 20 74 6d 61 73 6b 29 20 7c 7c 20 28 70   && tmask) || (p
249df 54 72 69 67 67 65 72 3d 3d 30 20 26 26 20 74 6d  Trigger==0 && tm
249e0 61 73 6b 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a  ask==0) );..  /*
249e1 20 45 6e 73 75 72 65 20 74 68 61 74 3a 0a 20 20   Ensure that:.  
249e2 2a 20 20 28 61 29 20 74 68 65 20 74 61 62 6c 65  *  (a) the table
249e3 20 69 73 20 6e 6f 74 20 72 65 61 64 2d 6f 6e 6c   is not read-onl
249e4 79 2c 20 0a 20 20 2a 20 20 28 62 29 20 74 68 61  y, .  *  (b) tha
249e5 74 20 69 66 20 69 74 20 69 73 20 61 20 76 69 65  t if it is a vie
249e6 77 20 74 68 65 6e 20 4f 4e 20 49 4e 53 45 52 54  w then ON INSERT
249e7 20 74 72 69 67 67 65 72 73 20 65 78 69 73 74 0a   triggers exist.
249e8 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
249e9 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61  e3IsReadOnly(pPa
249ea 72 73 65 2c 20 70 54 61 62 2c 20 74 6d 61 73 6b  rse, pTab, tmask
249eb 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  ) ){.    goto in
249ec 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
249ed 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  }.  assert( pTab
249ee 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  !=0 );..  /* If 
249ef 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20 61  pTab is really a
249f0 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72 65   view, make sure
249f1 20 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e 69   it has been ini
249f2 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a 20 56  tialized..  ** V
249f3 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
249f4 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  s() is a no-op i
249f5 66 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20  f pTab is not a 
249f6 76 69 65 77 20 28 6f 72 20 76 69 72 74 75 61 6c  view (or virtual
249f7 20 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 74 61   .  ** module ta
249f8 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ble)..  */.  if(
249f9 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
249fa 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
249fb 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20  e, pTab) ){.    
249fc 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
249fd 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  nup;.  }..  /* A
249fe 6c 6c 6f 63 61 74 65 20 61 20 56 44 42 45 0a 20  llocate a VDBE. 
249ff 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
24a00 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
24a01 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67  ;.  if( v==0 ) g
24a02 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
24a03 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  up;.  if( pParse
24a04 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71  ->nested==0 ) sq
24a05 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68  lite3VdbeCountCh
24a06 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69  anges(v);.  sqli
24a07 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
24a08 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  ration(pParse, p
24a09 53 65 6c 65 63 74 20 7c 7c 20 70 54 72 69 67 67  Select || pTrigg
24a0a 65 72 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20  er, iDb);..  /* 
24a0b 69 66 20 74 68 65 72 65 20 61 72 65 20 72 6f 77  if there are row
24a0c 20 74 72 69 67 67 65 72 73 2c 20 61 6c 6c 6f 63   triggers, alloc
24a0d 61 74 65 20 61 20 74 65 6d 70 20 74 61 62 6c 65  ate a temp table
24a0e 20 66 6f 72 20 6e 65 77 2e 2a 20 72 65 66 65 72   for new.* refer
24a0f 65 6e 63 65 73 2e 20 2a 2f 0a 20 20 69 66 28 20  ences. */.  if( 
24a10 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
24a11 6e 65 77 49 64 78 20 3d 20 70 50 61 72 73 65 2d  newIdx = pParse-
24a12 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 23 69  >nTab++;.  }..#i
24a13 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24a14 54 5f 58 46 45 52 5f 4f 50 54 0a 20 20 2f 2a 20  T_XFER_OPT.  /* 
24a15 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  If the statement
24a16 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a   is of the form.
24a17 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20    **.  **       
24a18 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62  INSERT INTO <tab
24a19 6c 65 31 3e 20 53 45 4c 45 43 54 20 2a 20 46 52  le1> SELECT * FR
24a1a 4f 4d 20 3c 74 61 62 6c 65 32 3e 3b 0a 20 20 2a  OM <table2>;.  *
24a1b 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 73 70 65 63  *.  ** Then spec
24a1c 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ial optimization
24a1d 73 20 63 61 6e 20 62 65 20 61 70 70 6c 69 65 64  s can be applied
24a1e 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 20 74   that make the t
24a1f 72 61 6e 73 66 65 72 0a 20 20 2a 2a 20 76 65 72  ransfer.  ** ver
24a20 79 20 66 61 73 74 20 61 6e 64 20 77 68 69 63 68  y fast and which
24a21 20 72 65 64 75 63 65 20 66 72 61 67 6d 65 6e 74   reduce fragment
24a22 61 74 69 6f 6e 20 6f 66 20 69 6e 64 69 63 65 73  ation of indices
24a23 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
24a24 20 69 73 20 74 68 65 20 32 6e 64 20 74 65 6d 70   is the 2nd temp
24a25 6c 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  late..  */.  if(
24a26 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 78   pColumn==0 && x
24a27 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28  ferOptimization(
24a28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 53  pParse, pTab, pS
24a29 65 6c 65 63 74 2c 20 6f 6e 45 72 72 6f 72 2c 20  elect, onError, 
24a2a 69 44 62 29 20 29 7b 0a 20 20 20 20 61 73 73 65  iDb) ){.    asse
24a2b 72 74 28 20 21 70 54 72 69 67 67 65 72 20 29 3b  rt( !pTrigger );
24a2c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69  .    assert( pLi
24a2d 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 67 6f 74  st==0 );.    got
24a2e 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
24a2f 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
24a30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52  SQLITE_OMIT_XFER
24a31 5f 4f 50 54 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  _OPT */..  /* If
24a32 20 74 68 69 73 20 69 73 20 61 6e 20 41 55 54 4f   this is an AUTO
24a33 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c 65 2c  INCREMENT table,
24a34 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 71   look up the seq
24a35 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e 20  uence number in 
24a36 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f  the.  ** sqlite_
24a37 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 61  sequence table a
24a38 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e 20 6d  nd store it in m
24a39 65 6d 6f 72 79 20 63 65 6c 6c 20 72 65 67 41 75  emory cell regAu
24a3a 74 6f 69 6e 63 2e 0a 20 20 2a 2f 0a 20 20 72 65  toinc..  */.  re
24a3b 67 41 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 49  gAutoinc = autoI
24a3c 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20  ncBegin(pParse, 
24a3d 69 44 62 2c 20 70 54 61 62 29 3b 0a 0a 20 20 2f  iDb, pTab);..  /
24a3e 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77  * Figure out how
24a3f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 66   many columns of
24a40 20 64 61 74 61 20 61 72 65 20 73 75 70 70 6c 69   data are suppli
24a41 65 64 2e 20 20 49 66 20 74 68 65 20 64 61 74 61  ed.  If the data
24a42 0a 20 20 2a 2a 20 69 73 20 63 6f 6d 69 6e 67 20  .  ** is coming 
24a43 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 73 74  from a SELECT st
24a44 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 67 65  atement, then ge
24a45 6e 65 72 61 74 65 20 61 20 63 6f 2d 72 6f 75 74  nerate a co-rout
24a46 69 6e 65 20 74 68 61 74 0a 20 20 2a 2a 20 70 72  ine that.  ** pr
24a47 6f 64 75 63 65 73 20 61 20 73 69 6e 67 6c 65 20  oduces a single 
24a48 72 6f 77 20 6f 66 20 74 68 65 20 53 45 4c 45 43  row of the SELEC
24a49 54 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61  T on each invoca
24a4a 74 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a 2a 20  tion.  The.  ** 
24a4b 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20 74 68  co-routine is th
24a4c 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20  e common header 
24a4d 74 6f 20 74 68 65 20 33 72 64 20 61 6e 64 20 34  to the 3rd and 4
24a4e 74 68 20 74 65 6d 70 6c 61 74 65 73 2e 0a 20 20  th templates..  
24a4f 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  */.  if( pSelect
24a50 20 29 7b 0a 20 20 20 20 2f 2a 20 44 61 74 61 20   ){.    /* Data 
24a51 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61  is coming from a
24a52 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61   SELECT.  Genera
24a53 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65  te code to imple
24a54 6d 65 6e 74 20 74 68 61 74 20 53 45 4c 45 43 54  ment that SELECT
24a55 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 63 6f 2d  .    ** as a co-
24a56 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 63 6f  routine.  The co
24a57 64 65 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20  de is common to 
24a58 62 6f 74 68 20 74 68 65 20 33 72 64 20 61 6e 64  both the 3rd and
24a59 20 34 74 68 0a 20 20 20 20 2a 2a 20 74 65 6d 70   4th.    ** temp
24a5a 6c 61 74 65 73 3a 0a 20 20 20 20 2a 2a 0a 20 20  lates:.    **.  
24a5b 20 20 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46    **         EOF
24a5c 20 3c 2d 20 30 0a 20 20 20 20 2a 2a 20 20 20 20   <- 0.    **    
24a5d 20 20 20 20 20 58 20 3c 2d 20 41 0a 20 20 20 20       X <- A.    
24a5e 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  **         goto 
24a5f 42 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 41 3a  B.    **      A:
24a60 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 20 53   setup for the S
24a61 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20 20 20  ELECT.    **    
24a62 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74       loop over t
24a63 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  he tables in the
24a64 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20   SELECT.    **  
24a65 20 20 20 20 20 20 20 20 20 6c 6f 61 64 20 76 61           load va
24a66 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65  lue into registe
24a67 72 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a 2a 20  r R..R+n.    ** 
24a68 20 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20            yield 
24a69 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  X.    **        
24a6a 20 65 6e 64 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a   end loop.    **
24a6b 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70           cleanup
24a6c 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43   after the SELEC
24a6d 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  T.    **        
24a6e 20 45 4f 46 20 3c 2d 20 31 0a 20 20 20 20 2a 2a   EOF <- 1.    **
24a6f 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20 58           yield X
24a70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
24a71 68 61 6c 74 2d 65 72 72 6f 72 0a 20 20 20 20 2a  halt-error.    *
24a72 2a 0a 20 20 20 20 2a 2a 20 4f 6e 20 65 61 63 68  *.    ** On each
24a73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
24a74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 2c 20 69  he co-routine, i
24a75 74 20 70 75 74 73 20 61 20 73 69 6e 67 6c 65 20  t puts a single 
24a76 72 6f 77 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  row of the.    *
24a77 2a 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 20  * SELECT result 
24a78 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20 64  into registers d
24a79 65 73 74 2e 69 4d 65 6d 2e 2e 2e 64 65 73 74 2e  est.iMem...dest.
24a7a 69 4d 65 6d 2b 64 65 73 74 2e 6e 4d 65 6d 2d 31  iMem+dest.nMem-1
24a7b 2e 0a 20 20 20 20 2a 2a 20 28 54 68 65 73 65 20  ..    ** (These 
24a7c 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 73  output registers
24a7d 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62   are allocated b
24a7e 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  y sqlite3Select(
24a7f 29 2e 29 20 20 57 68 65 6e 0a 20 20 20 20 2a 2a  ).)  When.    **
24a80 20 74 68 65 20 53 45 4c 45 43 54 20 63 6f 6d 70   the SELECT comp
24a81 6c 65 74 65 73 2c 20 69 74 20 73 65 74 73 20 74  letes, it sets t
24a82 68 65 20 45 4f 46 20 66 6c 61 67 20 73 74 6f 72  he EOF flag stor
24a83 65 64 20 69 6e 20 72 65 67 45 6f 66 2e 0a 20 20  ed in regEof..  
24a84 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 2c    */.    int rc,
24a85 20 6a 31 3b 0a 0a 20 20 20 20 72 65 67 45 6f 66   j1;..    regEof
24a86 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
24a87 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  m;.    sqlite3Vd
24a88 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
24a89 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 45 6f  nteger, 0, regEo
24a8a 66 29 3b 20 20 20 20 20 20 2f 2a 20 45 4f 46 20  f);      /* EOF 
24a8b 3c 2d 20 30 20 2a 2f 0a 20 20 20 20 56 64 62 65  <- 0 */.    Vdbe
24a8c 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53 45 4c  Comment((v, "SEL
24a8d 45 43 54 20 65 6f 66 20 66 6c 61 67 22 29 29 3b  ECT eof flag"));
24a8e 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .    sqlite3Sele
24a8f 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
24a90 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c  , SRT_Coroutine,
24a91 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29   ++pParse->nMem)
24a92 3b 0a 20 20 20 20 61 64 64 72 53 65 6c 65 63 74  ;.    addrSelect
24a93 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
24a94 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32 3b 0a  rrentAddr(v)+2;.
24a95 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24a96 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
24a97 67 65 72 2c 20 61 64 64 72 53 65 6c 65 63 74 2d  ger, addrSelect-
24a98 31 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a  1, dest.iParm);.
24a99 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
24a9a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24a9b 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20  _Goto, 0, 0);.  
24a9c 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
24a9d 2c 20 22 4a 75 6d 70 20 6f 76 65 72 20 53 45 4c  , "Jump over SEL
24a9e 45 43 54 20 63 6f 72 6f 75 74 69 6e 65 22 29 29  ECT coroutine"))
24a9f 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76  ;..    /* Resolv
24aa0 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  e the expression
24aa1 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20  s in the SELECT 
24aa2 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 65 78  statement and ex
24aa3 65 63 75 74 65 20 69 74 2e 20 2a 2f 0a 20 20 20  ecute it. */.   
24aa4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
24aa5 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ect(pParse, pSel
24aa6 65 63 74 2c 20 26 64 65 73 74 29 3b 0a 20 20 20  ect, &dest);.   
24aa7 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
24aa8 3e 6e 45 72 72 3d 3d 30 20 7c 7c 20 72 63 20 29  >nErr==0 || rc )
24aa9 3b 0a 20 20 20 20 69 66 28 20 72 63 20 7c 7c 20  ;.    if( rc || 
24aaa 4e 45 56 45 52 28 70 50 61 72 73 65 2d 3e 6e 45  NEVER(pParse->nE
24aab 72 72 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  rr) || db->mallo
24aac 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
24aad 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
24aae 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20  anup;.    }.    
24aaf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24ab0 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
24ab1 20 31 2c 20 72 65 67 45 6f 66 29 3b 20 20 20 20   1, regEof);    
24ab2 20 20 20 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 31       /* EOF <- 1
24ab3 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
24ab4 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
24ab5 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72  Yield, dest.iPar
24ab6 6d 29 3b 20 20 20 2f 2a 20 79 69 65 6c 64 20 58  m);   /* yield X
24ab7 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
24ab8 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24ab9 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 49 4e 54  Halt, SQLITE_INT
24aba 45 52 4e 41 4c 2c 20 4f 45 5f 41 62 6f 72 74 29  ERNAL, OE_Abort)
24abb 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
24abc 74 28 28 76 2c 20 22 45 6e 64 20 6f 66 20 53 45  t((v, "End of SE
24abd 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65 22 29  LECT coroutine")
24abe 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
24abf 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
24ac0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
24ac1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c              /* l
24ac2 61 62 65 6c 20 42 3a 20 2a 2f 0a 0a 20 20 20 20  abel B: */..    
24ac3 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20 3d 20  regFromSelect = 
24ac4 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 20 20 61  dest.iMem;.    a
24ac5 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d 3e  ssert( pSelect->
24ac6 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 6e 43  pEList );.    nC
24ac7 6f 6c 75 6d 6e 20 3d 20 70 53 65 6c 65 63 74 2d  olumn = pSelect-
24ac8 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a  >pEList->nExpr;.
24ac9 20 20 20 20 61 73 73 65 72 74 28 20 64 65 73 74      assert( dest
24aca 2e 6e 4d 65 6d 3d 3d 6e 43 6f 6c 75 6d 6e 20 29  .nMem==nColumn )
24acb 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 75 73  ;..    /* Set us
24acc 65 54 65 6d 70 54 61 62 6c 65 20 74 6f 20 54 52  eTempTable to TR
24acd 55 45 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  UE if the result
24ace 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
24acf 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20  tatement.    ** 
24ad0 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65  should be writte
24ad1 6e 20 69 6e 74 6f 20 61 20 74 65 6d 70 6f 72 61  n into a tempora
24ad2 72 79 20 74 61 62 6c 65 20 28 74 65 6d 70 6c 61  ry table (templa
24ad3 74 65 20 34 29 2e 20 20 53 65 74 20 74 6f 0a 20  te 4).  Set to. 
24ad4 20 20 20 2a 2a 20 46 41 4c 53 45 20 69 66 20 65     ** FALSE if e
24ad5 61 63 68 2a 20 72 6f 77 20 6f 66 20 74 68 65 20  ach* row of the 
24ad6 53 45 4c 45 43 54 20 63 61 6e 20 62 65 20 77 72  SELECT can be wr
24ad7 69 74 74 65 6e 20 64 69 72 65 63 74 6c 79 20 69  itten directly i
24ad8 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64  nto.    ** the d
24ad9 65 73 74 69 6e 61 74 69 6f 6e 20 74 61 62 6c 65  estination table
24ada 20 28 74 65 6d 70 6c 61 74 65 20 33 29 2e 0a 20   (template 3).. 
24adb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 74     **.    ** A t
24adc 65 6d 70 20 74 61 62 6c 65 20 6d 75 73 74 20 62  emp table must b
24add 65 20 75 73 65 64 20 69 66 20 74 68 65 20 74 61  e used if the ta
24ade 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74 65  ble being update
24adf 64 20 69 73 20 61 6c 73 6f 20 6f 6e 65 0a 20 20  d is also one.  
24ae0 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c    ** of the tabl
24ae1 65 73 20 62 65 69 6e 67 20 72 65 61 64 20 62 79  es being read by
24ae2 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
24ae3 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 75 73 65  ement.  Also use
24ae4 20 61 20 0a 20 20 20 20 2a 2a 20 74 65 6d 70 20   a .    ** temp 
24ae5 74 61 62 6c 65 20 69 6e 20 74 68 65 20 63 61 73  table in the cas
24ae6 65 20 6f 66 20 72 6f 77 20 74 72 69 67 67 65 72  e of row trigger
24ae7 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
24ae8 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20 72 65  ( pTrigger || re
24ae9 61 64 73 54 61 62 6c 65 28 76 2c 20 61 64 64 72  adsTable(v, addr
24aea 53 65 6c 65 63 74 2c 20 69 44 62 2c 20 70 54 61  Select, iDb, pTa
24aeb 62 29 20 29 7b 0a 20 20 20 20 20 20 75 73 65 54  b) ){.      useT
24aec 65 6d 70 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20  empTable = 1;.  
24aed 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 75 73 65    }..    if( use
24aee 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
24aef 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65     /* Invoke the
24af0 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 78   coroutine to ex
24af1 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f  tract informatio
24af2 6e 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43  n from the SELEC
24af3 54 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61  T.      ** and a
24af4 64 64 20 69 74 20 74 6f 20 61 20 74 72 61 6e 73  dd it to a trans
24af5 69 65 6e 74 20 74 61 62 6c 65 20 73 72 63 54 61  ient table srcTa
24af6 62 2e 20 20 54 68 65 20 63 6f 64 65 20 67 65 6e  b.  The code gen
24af7 65 72 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20  erated.      ** 
24af8 68 65 72 65 20 69 73 20 66 72 6f 6d 20 74 68 65  here is from the
24af9 20 34 74 68 20 74 65 6d 70 6c 61 74 65 3a 0a 20   4th template:. 
24afa 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
24afb 20 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65        B: open te
24afc 6d 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a  mp table.      *
24afd 2a 20 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20  *      L: yield 
24afe 58 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  X.      **      
24aff 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d     if EOF goto M
24b00 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
24b01 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f    insert row fro
24b02 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65  m R..R+n into te
24b03 6d 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a  mp table.      *
24b04 2a 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 4c  *         goto L
24b05 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 4d  .      **      M
24b06 3a 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  : ....      */. 
24b07 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b       int regRec;
24b08 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
24b09 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 70 61  ister to hold pa
24b0a 63 6b 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20  cked record */. 
24b0b 20 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70       int regTemp
24b0c 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 52 65 67  Rowid;    /* Reg
24b0d 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 65  ister to hold te
24b0e 6d 70 20 74 61 62 6c 65 20 52 4f 57 49 44 20 2a  mp table ROWID *
24b0f 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  /.      int addr
24b10 54 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Top;         /* 
24b11 4c 61 62 65 6c 20 22 4c 22 20 2a 2f 0a 20 20 20  Label "L" */.   
24b12 20 20 20 69 6e 74 20 61 64 64 72 49 66 3b 20 20     int addrIf;  
24b13 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
24b14 73 73 20 6f 66 20 6a 75 6d 70 20 74 6f 20 4d 20  ss of jump to M 
24b15 2a 2f 0a 0a 20 20 20 20 20 20 73 72 63 54 61 62  */..      srcTab
24b16 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
24b17 2b 3b 0a 20 20 20 20 20 20 72 65 67 52 65 63 20  +;.      regRec 
24b18 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
24b19 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
24b1a 20 20 20 72 65 67 54 65 6d 70 52 6f 77 69 64 20     regTempRowid 
24b1b 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
24b1c 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
24b1d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24b1e 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
24b1f 70 68 65 6d 65 72 61 6c 2c 20 73 72 63 54 61 62  phemeral, srcTab
24b20 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
24b21 20 20 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69    addrTop = sqli
24b22 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
24b23 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e   OP_Yield, dest.
24b24 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 61 64  iParm);.      ad
24b25 64 72 49 66 20 3d 20 73 71 6c 69 74 65 33 56 64  drIf = sqlite3Vd
24b26 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
24b27 66 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20 20 20  f, regEof);.    
24b28 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24b29 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
24b2a 63 6f 72 64 2c 20 72 65 67 46 72 6f 6d 53 65 6c  cord, regFromSel
24b2b 65 63 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65  ect, nColumn, re
24b2c 67 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c  gRec);.      sql
24b2d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24b2e 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 73  , OP_NewRowid, s
24b2f 72 63 54 61 62 2c 20 72 65 67 54 65 6d 70 52 6f  rcTab, regTempRo
24b30 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  wid);.      sqli
24b31 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
24b32 20 4f 50 5f 49 6e 73 65 72 74 2c 20 73 72 63 54   OP_Insert, srcT
24b33 61 62 2c 20 72 65 67 52 65 63 2c 20 72 65 67 54  ab, regRec, regT
24b34 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  empRowid);.     
24b35 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24b36 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
24b37 2c 20 61 64 64 72 54 6f 70 29 3b 0a 20 20 20 20  , addrTop);.    
24b38 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
24b39 70 48 65 72 65 28 76 2c 20 61 64 64 72 49 66 29  pHere(v, addrIf)
24b3a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
24b3b 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
24b3c 61 72 73 65 2c 20 72 65 67 52 65 63 29 3b 0a 20  arse, regRec);. 
24b3d 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
24b3e 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
24b3f 65 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29  e, regTempRowid)
24b40 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
24b41 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20  .    /* This is 
24b42 74 68 65 20 63 61 73 65 20 69 66 20 74 68 65 20  the case if the 
24b43 64 61 74 61 20 66 6f 72 20 74 68 65 20 49 4e 53  data for the INS
24b44 45 52 54 20 69 73 20 63 6f 6d 69 6e 67 20 66 72  ERT is coming fr
24b45 6f 6d 20 61 20 56 41 4c 55 45 53 0a 20 20 20 20  om a VALUES.    
24b46 2a 2a 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f  ** clause.    */
24b47 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  .    NameContext
24b48 20 73 4e 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74   sNC;.    memset
24b49 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
24b4a 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e  (sNC));.    sNC.
24b4b 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
24b4c 0a 20 20 20 20 73 72 63 54 61 62 20 3d 20 2d 31  .    srcTab = -1
24b4d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 73  ;.    assert( us
24b4e 65 54 65 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b  eTempTable==0 );
24b4f 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70  .    nColumn = p
24b50 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45  List ? pList->nE
24b51 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 66 6f 72  xpr : 0;.    for
24b52 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b  (i=0; i<nColumn;
24b53 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
24b54 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
24b55 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
24b56 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
24b57 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  ) ){.        got
24b58 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
24b59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
24b5a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
24b5b 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ure the number o
24b5c 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
24b5d 20 73 6f 75 72 63 65 20 64 61 74 61 20 6d 61 74   source data mat
24b5e 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 0a  ches the number.
24b5f 20 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20    ** of columns 
24b60 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69  to be inserted i
24b61 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 20  nto the table.. 
24b62 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74   */.  if( IsVirt
24b63 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
24b64 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
24b65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
24b66 20 20 20 20 6e 48 69 64 64 65 6e 20 2b 3d 20 28      nHidden += (
24b67 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26  IsHiddenColumn(&
24b68 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f  pTab->aCol[i]) ?
24b69 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20   1 : 0);.    }. 
24b6a 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e   }.  if( pColumn
24b6b 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26  ==0 && nColumn &
24b6c 26 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62  & nColumn!=(pTab
24b6d 2d 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20  ->nCol-nHidden) 
24b6e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
24b6f 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
24b70 20 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 53         "table %S
24b71 20 68 61 73 20 25 64 20 63 6f 6c 75 6d 6e 73 20   has %d columns 
24b72 62 75 74 20 25 64 20 76 61 6c 75 65 73 20 77 65  but %d values we
24b73 72 65 20 73 75 70 70 6c 69 65 64 22 2c 0a 20 20  re supplied",.  
24b74 20 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30       pTabList, 0
24b75 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69  , pTab->nCol-nHi
24b76 64 64 65 6e 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a  dden, nColumn);.
24b77 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
24b78 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
24b79 66 28 20 70 43 6f 6c 75 6d 6e 21 3d 30 20 26 26  f( pColumn!=0 &&
24b7a 20 6e 43 6f 6c 75 6d 6e 21 3d 70 43 6f 6c 75 6d   nColumn!=pColum
24b7b 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 73 71  n->nId ){.    sq
24b7c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24b7d 61 72 73 65 2c 20 22 25 64 20 76 61 6c 75 65 73  arse, "%d values
24b7e 20 66 6f 72 20 25 64 20 63 6f 6c 75 6d 6e 73 22   for %d columns"
24b7f 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 75  , nColumn, pColu
24b80 6d 6e 2d 3e 6e 49 64 29 3b 0a 20 20 20 20 67 6f  mn->nId);.    go
24b81 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
24b82 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  p;.  }..  /* If 
24b83 74 68 65 20 49 4e 53 45 52 54 20 73 74 61 74 65  the INSERT state
24b84 6d 65 6e 74 20 69 6e 63 6c 75 64 65 64 20 61 6e  ment included an
24b85 20 49 44 4c 49 53 54 20 74 65 72 6d 2c 20 74 68   IDLIST term, th
24b86 65 6e 20 6d 61 6b 65 20 73 75 72 65 0a 20 20 2a  en make sure.  *
24b87 2a 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f  * all elements o
24b88 66 20 74 68 65 20 49 44 4c 49 53 54 20 72 65 61  f the IDLIST rea
24b89 6c 6c 79 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20  lly are columns 
24b8a 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  of the table and
24b8b 20 0a 20 20 2a 2a 20 72 65 6d 65 6d 62 65 72 20   .  ** remember 
24b8c 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63  the column indic
24b8d 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  es..  **.  ** If
24b8e 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
24b8f 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  n INTEGER PRIMAR
24b90 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 6e 64  Y KEY column and
24b91 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 20 20 2a   that column.  *
24b92 2a 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68  * is named in th
24b93 65 20 49 44 4c 49 53 54 2c 20 74 68 65 6e 20 72  e IDLIST, then r
24b94 65 63 6f 72 64 20 69 6e 20 74 68 65 20 6b 65 79  ecord in the key
24b95 43 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 0a  Column variable.
24b96 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 69    ** the index i
24b97 6e 74 6f 20 49 44 4c 49 53 54 20 6f 66 20 74 68  nto IDLIST of th
24b98 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f  e primary key co
24b99 6c 75 6d 6e 2e 20 20 6b 65 79 43 6f 6c 75 6d 6e  lumn.  keyColumn
24b9a 20 69 73 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64   is.  ** the ind
24b9b 65 78 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72  ex of the primar
24b9c 79 20 6b 65 79 20 61 73 20 69 74 20 61 70 70 65  y key as it appe
24b9d 61 72 73 20 69 6e 20 49 44 4c 49 53 54 2c 20 6e  ars in IDLIST, n
24b9e 6f 74 20 61 73 0a 20 20 2a 2a 20 69 73 20 61 70  ot as.  ** is ap
24b9f 70 65 61 72 73 20 69 6e 20 74 68 65 20 6f 72 69  pears in the ori
24ba0 67 69 6e 61 6c 20 74 61 62 6c 65 2e 20 20 28 54  ginal table.  (T
24ba1 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
24ba2 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79  primary.  ** key
24ba3 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
24ba4 20 74 61 62 6c 65 20 69 73 20 70 54 61 62 2d 3e   table is pTab->
24ba5 69 50 4b 65 79 2e 29 0a 20 20 2a 2f 0a 20 20 69  iPKey.).  */.  i
24ba6 66 28 20 70 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  f( pColumn ){.  
24ba7 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f    for(i=0; i<pCo
24ba8 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b  lumn->nId; i++){
24ba9 0a 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e  .      pColumn->
24baa 61 5b 69 5d 2e 69 64 78 20 3d 20 2d 31 3b 0a 20  a[i].idx = -1;. 
24bab 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30     }.    for(i=0
24bac 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  ; i<pColumn->nId
24bad 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f  ; i++){.      fo
24bae 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e  r(j=0; j<pTab->n
24baf 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
24bb0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
24bb1 72 49 43 6d 70 28 70 43 6f 6c 75 6d 6e 2d 3e 61  rICmp(pColumn->a
24bb2 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  [i].zName, pTab-
24bb3 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d  >aCol[j].zName)=
24bb4 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
24bb5 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64  pColumn->a[i].id
24bb6 78 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20  x = j;.         
24bb7 20 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50   if( j==pTab->iP
24bb8 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Key ){.         
24bb9 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69     keyColumn = i
24bba 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
24bbb 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
24bbc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
24bbd 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 54  .      if( j>=pT
24bbe 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20  ab->nCol ){.    
24bbf 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49      if( sqlite3I
24bc0 73 52 6f 77 69 64 28 70 43 6f 6c 75 6d 6e 2d 3e  sRowid(pColumn->
24bc1 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20  a[i].zName) ){. 
24bc2 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75           keyColu
24bc3 6d 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20  mn = i;.        
24bc4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
24bc5 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24bc6 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
24bc7 25 53 20 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e  %S has no column
24bc8 20 6e 61 6d 65 64 20 25 73 22 2c 0a 20 20 20 20   named %s",.    
24bc9 20 20 20 20 20 20 20 20 20 20 70 54 61 62 4c 69            pTabLi
24bca 73 74 2c 20 30 2c 20 70 43 6f 6c 75 6d 6e 2d 3e  st, 0, pColumn->
24bcb 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  a[i].zName);.   
24bcc 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e         pParse->n
24bcd 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  Err++;.         
24bce 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
24bcf 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a  anup;.        }.
24bd0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
24bd1 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  }..  /* If there
24bd2 20 69 73 20 6e 6f 20 49 44 4c 49 53 54 20 74 65   is no IDLIST te
24bd3 72 6d 20 62 75 74 20 74 68 65 20 74 61 62 6c 65  rm but the table
24bd4 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20   has an integer 
24bd5 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79  primary.  ** key
24bd6 2c 20 74 68 65 20 73 65 74 20 74 68 65 20 6b 65  , the set the ke
24bd7 79 43 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65  yColumn variable
24bd8 20 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20   to the primary 
24bd9 6b 65 79 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  key column index
24bda 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69  .  ** in the ori
24bdb 67 69 6e 61 6c 20 74 61 62 6c 65 20 64 65 66 69  ginal table defi
24bdc 6e 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69  nition..  */.  i
24bdd 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26  f( pColumn==0 &&
24bde 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20   nColumn>0 ){.  
24bdf 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 70 54    keyColumn = pT
24be0 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 7d 0a 0a  ab->iPKey;.  }..
24be1 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 74 65    /* Open the te
24be2 6d 70 20 74 61 62 6c 65 20 66 6f 72 20 46 4f 52  mp table for FOR
24be3 20 45 41 43 48 20 52 4f 57 20 74 72 69 67 67 65   EACH ROW trigge
24be4 72 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  rs.  */.  if( pT
24be5 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 73 71  rigger ){.    sq
24be6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
24be7 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f  v, OP_OpenPseudo
24be8 2c 20 6e 65 77 49 64 78 2c 20 30 2c 20 70 54 61  , newIdx, 0, pTa
24be9 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20  b->nCol);.  }.  
24bea 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69    .  /* Initiali
24beb 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  ze the count of 
24bec 72 6f 77 73 20 74 6f 20 62 65 20 69 6e 73 65 72  rows to be inser
24bed 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ted.  */.  if( d
24bee 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
24bef 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20  E_CountRows ){. 
24bf0 20 20 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d     regRowCount =
24bf1 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
24bf2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24bf3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
24bf4 65 67 65 72 2c 20 30 2c 20 72 65 67 52 6f 77 43  eger, 0, regRowC
24bf5 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ount);.  }..  /*
24bf6 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
24bf7 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 74 68 65  a view, open the
24bf8 20 74 61 62 6c 65 20 61 6e 64 20 61 6e 64 20 61   table and and a
24bf9 6c 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ll indices */.  
24bfa 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20  if( !isView ){. 
24bfb 20 20 20 69 6e 74 20 6e 49 64 78 3b 0a 0a 20 20     int nIdx;..  
24bfc 20 20 62 61 73 65 43 75 72 20 3d 20 70 50 61 72    baseCur = pPar
24bfd 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 6e 49  se->nTab;.    nI
24bfe 64 78 20 3d 20 73 71 6c 69 74 65 33 4f 70 65 6e  dx = sqlite3Open
24bff 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28  TableAndIndices(
24c00 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
24c01 73 65 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72  seCur, OP_OpenWr
24c02 69 74 65 29 3b 0a 20 20 20 20 61 52 65 67 49 64  ite);.    aRegId
24c03 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
24c04 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
24c05 66 28 69 6e 74 29 2a 28 6e 49 64 78 2b 31 29 29  f(int)*(nIdx+1))
24c06 3b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64  ;.    if( aRegId
24c07 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  x==0 ){.      go
24c08 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
24c09 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  p;.    }.    for
24c0a 28 69 3d 30 3b 20 69 3c 6e 49 64 78 3b 20 69 2b  (i=0; i<nIdx; i+
24c0b 2b 29 7b 0a 20 20 20 20 20 20 61 52 65 67 49 64  +){.      aRegId
24c0c 78 5b 69 5d 20 3d 20 2b 2b 70 50 61 72 73 65 2d  x[i] = ++pParse-
24c0d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  >nMem;.    }.  }
24c0e 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74  ..  /* This is t
24c0f 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6d 61  he top of the ma
24c10 69 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f  in insertion loo
24c11 70 20 2a 2f 0a 20 20 69 66 28 20 75 73 65 54 65  p */.  if( useTe
24c12 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f  mpTable ){.    /
24c13 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64  * This block cod
24c14 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f  es the top of lo
24c15 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f  op only.  The co
24c16 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74  mplete loop is t
24c17 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77  he.    ** follow
24c18 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28  ing pseudocode (
24c19 74 65 6d 70 6c 61 74 65 20 34 29 3a 0a 20 20 20  template 4):.   
24c1a 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
24c1b 20 20 20 72 65 77 69 6e 64 20 74 65 6d 70 20 74     rewind temp t
24c1c 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20 20  able.    **     
24c1d 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72 6f   C: loop over ro
24c1e 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 69 61  ws of intermedia
24c1f 74 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  te table.    ** 
24c20 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66            transf
24c21 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69  er values form i
24c22 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
24c23 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 20  e into <table>. 
24c24 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65 6e     **         en
24c25 64 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20  d loop.    **   
24c26 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f     D: ....    */
24c27 0a 20 20 20 20 61 64 64 72 49 6e 73 54 6f 70 20  .    addrInsTop 
24c28 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
24c29 4f 70 31 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op1(v, OP_Rewind
24c2a 2c 20 73 72 63 54 61 62 29 3b 0a 20 20 20 20 61  , srcTab);.    a
24c2b 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65  ddrCont = sqlite
24c2c 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
24c2d 28 76 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  (v);.  }else if(
24c2e 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
24c2f 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f  /* This block co
24c30 64 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c  des the top of l
24c31 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63  oop only.  The c
24c32 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20  omplete loop is 
24c33 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f  the.    ** follo
24c34 77 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20  wing pseudocode 
24c35 28 74 65 6d 70 6c 61 74 65 20 33 29 3a 0a 20 20  (template 3):.  
24c36 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
24c37 20 43 3a 20 79 69 65 6c 64 20 58 0a 20 20 20 20   C: yield X.    
24c38 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
24c39 46 20 67 6f 74 6f 20 44 0a 20 20 20 20 2a 2a 20  F goto D.    ** 
24c3a 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74          insert t
24c3b 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74  he select result
24c3c 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72   into <table> fr
24c3d 6f 6d 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a 2a  om R..R+n.    **
24c3e 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a           goto C.
24c3f 20 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e      **      D: .
24c40 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64  ...    */.    ad
24c41 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33  drCont = sqlite3
24c42 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
24c43 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61  _Yield, dest.iPa
24c44 72 6d 29 3b 0a 20 20 20 20 61 64 64 72 49 6e 73  rm);.    addrIns
24c45 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Top = sqlite3Vdb
24c46 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
24c47 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20 7d 0a 0a  , regEof);.  }..
24c48 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65    /* Allocate re
24c49 67 69 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64  gisters for hold
24c4a 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 6f 66  ing the rowid of
24c4b 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 0a 20 20   the new row,.  
24c4c 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ** the content o
24c4d 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 20 61  f the new row, a
24c4e 6e 64 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65  nd the assemblie
24c4f 64 20 72 6f 77 20 72 65 63 6f 72 64 2e 0a 20 20  d row record..  
24c50 2a 2f 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d  */.  regRecord =
24c51 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
24c52 0a 20 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65  .  regRowid = re
24c53 67 49 6e 73 20 3d 20 70 50 61 72 73 65 2d 3e 6e  gIns = pParse->n
24c54 4d 65 6d 2b 31 3b 0a 20 20 70 50 61 72 73 65 2d  Mem+1;.  pParse-
24c55 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e  >nMem += pTab->n
24c56 43 6f 6c 20 2b 20 31 3b 0a 20 20 69 66 28 20 49  Col + 1;.  if( I
24c57 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
24c58 7b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 2b 2b  {.    regRowid++
24c59 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
24c5a 65 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 67 44  em++;.  }.  regD
24c5b 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64 2b 31  ata = regRowid+1
24c5c 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20  ;..  /* Run the 
24c5d 42 45 46 4f 52 45 20 61 6e 64 20 49 4e 53 54 45  BEFORE and INSTE
24c5e 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 2c 20  AD OF triggers, 
24c5f 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
24c60 0a 20 20 2a 2f 0a 20 20 65 6e 64 4f 66 4c 6f 6f  .  */.  endOfLoo
24c61 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  p = sqlite3VdbeM
24c62 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69  akeLabel(v);.  i
24c63 66 28 20 74 6d 61 73 6b 20 26 20 54 52 49 47 47  f( tmask & TRIGG
24c64 45 52 5f 42 45 46 4f 52 45 20 29 7b 0a 20 20 20  ER_BEFORE ){.   
24c65 20 69 6e 74 20 72 65 67 54 72 69 67 52 6f 77 69   int regTrigRowi
24c66 64 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 43 6f  d;.    int regCo
24c67 6c 73 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 52  ls;.    int regR
24c68 65 63 3b 0a 0a 20 20 20 20 2f 2a 20 62 75 69 6c  ec;..    /* buil
24c69 64 20 74 68 65 20 4e 45 57 2e 2a 20 72 65 66 65  d the NEW.* refe
24c6a 72 65 6e 63 65 20 72 6f 77 2e 20 20 4e 6f 74 65  rence row.  Note
24c6b 20 74 68 61 74 20 69 66 20 74 68 65 72 65 20 69   that if there i
24c6c 73 20 61 6e 20 49 4e 54 45 47 45 52 0a 20 20 20  s an INTEGER.   
24c6d 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20   ** PRIMARY KEY 
24c6e 69 6e 74 6f 20 77 68 69 63 68 20 61 20 4e 55 4c  into which a NUL
24c6f 4c 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72  L is being inser
24c70 74 65 64 2c 20 74 68 61 74 20 4e 55 4c 4c 20 77  ted, that NULL w
24c71 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 74 72  ill be.    ** tr
24c72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20  anslated into a 
24c73 75 6e 69 71 75 65 20 49 44 20 66 6f 72 20 74 68  unique ID for th
24c74 65 20 72 6f 77 2e 20 20 42 75 74 20 6f 6e 20 61  e row.  But on a
24c75 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 2c   BEFORE trigger,
24c76 0a 20 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f  .    ** we do no
24c77 74 20 6b 6e 6f 77 20 77 68 61 74 20 74 68 65 20  t know what the 
24c78 75 6e 69 71 75 65 20 49 44 20 77 69 6c 6c 20 62  unique ID will b
24c79 65 20 28 62 65 63 61 75 73 65 20 74 68 65 20 69  e (because the i
24c7a 6e 73 65 72 74 20 68 61 73 0a 20 20 20 20 2a 2a  nsert has.    **
24c7b 20 6e 6f 74 20 68 61 70 70 65 6e 65 64 20 79 65   not happened ye
24c7c 74 29 20 73 6f 20 77 65 20 73 75 62 73 74 69 74  t) so we substit
24c7d 75 74 65 20 61 20 72 6f 77 69 64 20 6f 66 20 2d  ute a rowid of -
24c7e 31 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67  1.    */.    reg
24c7f 54 72 69 67 52 6f 77 69 64 20 3d 20 73 71 6c 69  TrigRowid = sqli
24c80 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
24c81 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 6b  arse);.    if( k
24c82 65 79 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20  eyColumn<0 ){.  
24c83 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24c84 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
24c85 67 65 72 2c 20 2d 31 2c 20 72 65 67 54 72 69 67  ger, -1, regTrig
24c86 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73  Rowid);.    }els
24c87 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b  e{.      int j1;
24c88 0a 20 20 20 20 20 20 69 66 28 20 75 73 65 54 65  .      if( useTe
24c89 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  mpTable ){.     
24c8a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24c8b 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
24c8c 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f  n, srcTab, keyCo
24c8d 6c 75 6d 6e 2c 20 72 65 67 54 72 69 67 52 6f 77  lumn, regTrigRow
24c8e 69 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  id);.      }else
24c8f 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
24c90 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20  ( pSelect==0 ); 
24c91 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 75 73   /* Otherwise us
24c92 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20 74 72  eTempTable is tr
24c93 75 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  ue */.        sq
24c94 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
24c95 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b  arse, pList->a[k
24c96 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c  eyColumn].pExpr,
24c97 20 72 65 67 54 72 69 67 52 6f 77 69 64 29 3b 0a   regTrigRowid);.
24c98 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a 31        }.      j1
24c99 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
24c9a 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75  dOp1(v, OP_NotNu
24c9b 6c 6c 2c 20 72 65 67 54 72 69 67 52 6f 77 69 64  ll, regTrigRowid
24c9c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
24c9d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24c9e 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65  _Integer, -1, re
24c9f 67 54 72 69 67 52 6f 77 69 64 29 3b 0a 20 20 20  gTrigRowid);.   
24ca0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
24ca1 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20  mpHere(v, j1);. 
24ca2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24ca3 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
24ca4 74 42 65 49 6e 74 2c 20 72 65 67 54 72 69 67 52  tBeInt, regTrigR
24ca5 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  owid);.    }..  
24ca6 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61 76 65    /* Cannot have
24ca7 20 74 72 69 67 67 65 72 73 20 6f 6e 20 61 20 76   triggers on a v
24ca8 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 49 66  irtual table. If
24ca9 20 69 74 20 77 65 72 65 20 70 6f 73 73 69 62 6c   it were possibl
24caa 65 2c 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 62  e,.    ** this b
24cab 6c 6f 63 6b 20 77 6f 75 6c 64 20 68 61 76 65 20  lock would have 
24cac 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 68  to account for h
24cad 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 2e 0a 20 20  idden column..  
24cae 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
24caf 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
24cb0 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74  );..    /* Creat
24cb1 65 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  e the new column
24cb2 20 64 61 74 61 0a 20 20 20 20 2a 2f 0a 20 20 20   data.    */.   
24cb3 20 72 65 67 43 6f 6c 73 20 3d 20 73 71 6c 69 74   regCols = sqlit
24cb4 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
24cb5 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f  Parse, pTab->nCo
24cb6 6c 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  l);.    for(i=0;
24cb7 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
24cb8 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
24cb9 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
24cba 20 20 20 20 20 6a 20 3d 20 69 3b 0a 20 20 20 20       j = i;.    
24cbb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
24cbc 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c   for(j=0; j<pCol
24cbd 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a  umn->nId; j++){.
24cbe 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
24cbf 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d  olumn->a[j].idx=
24cc0 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =i ) break;.    
24cc1 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
24cc2 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 20      if( pColumn 
24cc3 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e  && j>=pColumn->n
24cc4 49 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  Id ){.        sq
24cc5 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
24cc6 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  arse, pTab->aCol
24cc7 5b 69 5d 2e 70 44 66 6c 74 2c 20 72 65 67 43 6f  [i].pDflt, regCo
24cc8 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ls+i);.      }el
24cc9 73 65 20 69 66 28 20 75 73 65 54 65 6d 70 54 61  se if( useTempTa
24cca 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ble ){.        s
24ccb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
24ccc 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73  (v, OP_Column, s
24ccd 72 63 54 61 62 2c 20 6a 2c 20 72 65 67 43 6f 6c  rcTab, j, regCol
24cce 73 2b 69 29 3b 20 0a 20 20 20 20 20 20 7d 65 6c  s+i); .      }el
24ccf 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  se{.        asse
24cd0 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29  rt( pSelect==0 )
24cd1 3b 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 75  ; /* Otherwise u
24cd2 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20 74  seTempTable is t
24cd3 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73  rue */.        s
24cd4 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e  qlite3ExprCodeAn
24cd5 64 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 70  dCache(pParse, p
24cd6 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  List->a[j].pExpr
24cd7 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20  , regCols+i);.  
24cd8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
24cd9 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33  regRec = sqlite3
24cda 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
24cdb 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
24cdc 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
24cdd 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 43  MakeRecord, regC
24cde 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  ols, pTab->nCol,
24cdf 20 72 65 67 52 65 63 29 3b 0a 0a 20 20 20 20 2f   regRec);..    /
24ce0 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
24ce1 49 4e 53 45 52 54 20 6f 6e 20 61 20 76 69 65 77  INSERT on a view
24ce2 20 77 69 74 68 20 61 6e 20 49 4e 53 54 45 41 44   with an INSTEAD
24ce3 20 4f 46 20 49 4e 53 45 52 54 20 74 72 69 67 67   OF INSERT trigg
24ce4 65 72 2c 0a 20 20 20 20 2a 2a 20 64 6f 20 6e 6f  er,.    ** do no
24ce5 74 20 61 74 74 65 6d 70 74 20 61 6e 79 20 63 6f  t attempt any co
24ce6 6e 76 65 72 73 69 6f 6e 73 20 62 65 66 6f 72 65  nversions before
24ce7 20 61 73 73 65 6d 62 6c 69 6e 67 20 74 68 65 20   assembling the 
24ce8 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2a 20 49  record..    ** I
24ce9 66 20 74 68 69 73 20 69 73 20 61 20 72 65 61 6c  f this is a real
24cea 20 74 61 62 6c 65 2c 20 61 74 74 65 6d 70 74 20   table, attempt 
24ceb 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 72  conversions as r
24cec 65 71 75 69 72 65 64 20 62 79 20 74 68 65 0a 20  equired by the. 
24ced 20 20 20 2a 2a 20 74 61 62 6c 65 20 63 6f 6c 75     ** table colu
24cee 6d 6e 20 61 66 66 69 6e 69 74 69 65 73 2e 0a 20  mn affinities.. 
24cef 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69     */.    if( !i
24cf0 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 73  sView ){.      s
24cf1 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e  qlite3TableAffin
24cf2 69 74 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b  ityStr(v, pTab);
24cf3 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
24cf4 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
24cf5 4f 50 5f 49 6e 73 65 72 74 2c 20 6e 65 77 49 64  OP_Insert, newId
24cf6 78 2c 20 72 65 67 52 65 63 2c 20 72 65 67 54 72  x, regRec, regTr
24cf7 69 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  igRowid);.    sq
24cf8 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
24cf9 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
24cfa 65 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ec);.    sqlite3
24cfb 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
24cfc 50 61 72 73 65 2c 20 72 65 67 54 72 69 67 52 6f  Parse, regTrigRo
24cfd 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  wid);.    sqlite
24cfe 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
24cff 65 28 70 50 61 72 73 65 2c 20 72 65 67 43 6f 6c  e(pParse, regCol
24d00 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a  s, pTab->nCol);.
24d01 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 42 45 46  .    /* Fire BEF
24d02 4f 52 45 20 6f 72 20 49 4e 53 54 45 41 44 20 4f  ORE or INSTEAD O
24d03 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20  F triggers */.  
24d04 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f 64    if( sqlite3Cod
24d05 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72  eRowTrigger(pPar
24d06 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b  se, pTrigger, TK
24d07 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52 49 47  _INSERT, 0, TRIG
24d08 47 45 52 5f 42 45 46 4f 52 45 2c 20 0a 20 20 20  GER_BEFORE, .   
24d09 20 20 20 20 20 70 54 61 62 2c 20 6e 65 77 49 64       pTab, newId
24d0a 78 2c 20 2d 31 2c 20 6f 6e 45 72 72 6f 72 2c 20  x, -1, onError, 
24d0b 65 6e 64 4f 66 4c 6f 6f 70 2c 20 30 2c 20 30 29  endOfLoop, 0, 0)
24d0c 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 69   ){.      goto i
24d0d 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
24d0e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50     }.  }..  /* P
24d0f 75 73 68 20 74 68 65 20 72 65 63 6f 72 64 20 6e  ush the record n
24d10 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65  umber for the ne
24d11 77 20 65 6e 74 72 79 20 6f 6e 74 6f 20 74 68 65  w entry onto the
24d12 20 73 74 61 63 6b 2e 20 20 54 68 65 0a 20 20 2a   stack.  The.  *
24d13 2a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  * record number 
24d14 69 73 20 61 20 72 61 6e 64 6f 6d 6c 79 20 67 65  is a randomly ge
24d15 6e 65 72 61 74 65 20 69 6e 74 65 67 65 72 20 63  nerate integer c
24d16 72 65 61 74 65 64 20 62 79 20 4e 65 77 52 6f 77  reated by NewRow
24d17 69 64 0a 20 20 2a 2a 20 65 78 63 65 70 74 20 77  id.  ** except w
24d18 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 68 61  hen the table ha
24d19 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  s an INTEGER PRI
24d1a 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c  MARY KEY column,
24d1b 20 69 6e 20 77 68 69 63 68 0a 20 20 2a 2a 20 63   in which.  ** c
24d1c 61 73 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e  ase the record n
24d1d 75 6d 62 65 72 20 69 73 20 74 68 65 20 73 61 6d  umber is the sam
24d1e 65 20 61 73 20 74 68 61 74 20 63 6f 6c 75 6d 6e  e as that column
24d1f 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69  . .  */.  if( !i
24d20 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69 66 28  sView ){.    if(
24d21 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
24d22 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   ){.      /* The
24d23 20 72 6f 77 20 74 68 61 74 20 74 68 65 20 56 55   row that the VU
24d24 70 64 61 74 65 20 6f 70 63 6f 64 65 20 77 69 6c  pdate opcode wil
24d25 6c 20 64 65 6c 65 74 65 3a 20 6e 6f 6e 65 20 2a  l delete: none *
24d26 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
24d27 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24d28 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 49 6e 73 29  Null, 0, regIns)
24d29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
24d2a 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a  keyColumn>=0 ){.
24d2b 20 20 20 20 20 20 69 66 28 20 75 73 65 54 65 6d        if( useTem
24d2c 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  pTable ){.      
24d2d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24d2e 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
24d2f 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f 6c  , srcTab, keyCol
24d30 75 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  umn, regRowid);.
24d31 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
24d32 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
24d33 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24d34 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
24d35 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b  , regFromSelect+
24d36 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f  keyColumn, regRo
24d37 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  wid);.      }els
24d38 65 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65 4f  e{.        VdbeO
24d39 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 20 20  p *pOp;.        
24d3a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
24d3b 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61  pParse, pList->a
24d3c 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70  [keyColumn].pExp
24d3d 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  r, regRowid);.  
24d3e 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69        pOp = sqli
24d3f 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20  te3VdbeGetOp(v, 
24d40 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
24d41 6e 74 41 64 64 72 28 76 29 20 2d 20 31 29 3b 0a  ntAddr(v) - 1);.
24d42 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 41          if( ALWA
24d43 59 53 28 70 4f 70 29 20 26 26 20 70 4f 70 2d 3e  YS(pOp) && pOp->
24d44 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20  opcode==OP_Null 
24d45 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  && !IsVirtual(pT
24d46 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ab) ){.         
24d47 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b   appendFlag = 1;
24d48 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
24d49 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 65 77 52  opcode = OP_NewR
24d4a 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20  owid;.          
24d4b 70 4f 70 2d 3e 70 31 20 3d 20 62 61 73 65 43 75  pOp->p1 = baseCu
24d4c 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70  r;.          pOp
24d4d 2d 3e 70 32 20 3d 20 72 65 67 52 6f 77 69 64 3b  ->p2 = regRowid;
24d4e 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
24d4f 70 33 20 3d 20 72 65 67 41 75 74 6f 69 6e 63 3b  p3 = regAutoinc;
24d50 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
24d51 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74   }.      /* If t
24d52 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 65  he PRIMARY KEY e
24d53 78 70 72 65 73 73 69 6f 6e 20 69 73 20 4e 55 4c  xpression is NUL
24d54 4c 2c 20 74 68 65 6e 20 75 73 65 20 4f 50 5f 4e  L, then use OP_N
24d55 65 77 52 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a  ewRowid.      **
24d56 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 75   to generate a u
24d57 6e 69 71 75 65 20 70 72 69 6d 61 72 79 20 6b 65  nique primary ke
24d58 79 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a  y value..      *
24d59 2f 0a 20 20 20 20 20 20 69 66 28 20 21 61 70 70  /.      if( !app
24d5a 65 6e 64 46 6c 61 67 20 29 7b 0a 20 20 20 20 20  endFlag ){.     
24d5b 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 20     int j1;.     
24d5c 20 20 20 69 66 28 20 21 49 73 56 69 72 74 75 61     if( !IsVirtua
24d5d 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
24d5e 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
24d5f 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
24d60 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52 6f  P_NotNull, regRo
24d61 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  wid);.          
24d62 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24d63 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  3(v, OP_NewRowid
24d64 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f  , baseCur, regRo
24d65 77 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29  wid, regAutoinc)
24d66 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
24d67 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
24d68 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20  v, j1);.        
24d69 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
24d6a 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
24d6b 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
24d6c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
24d6d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
24d6e 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52 6f  OP_IsNull, regRo
24d6f 77 69 64 2c 20 6a 31 2b 32 29 3b 0a 20 20 20 20  wid, j1+2);.    
24d70 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
24d71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
24d72 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
24d73 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
24d74 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
24d75 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
24d76 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
24d77 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
24d78 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52  OP_Null, 0, regR
24d79 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  owid);.    }else
24d7a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
24d7b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
24d7c 4e 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75  NewRowid, baseCu
24d7d 72 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67  r, regRowid, reg
24d7e 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20  Autoinc);.      
24d7f 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a  appendFlag = 1;.
24d80 20 20 20 20 7d 0a 20 20 20 20 61 75 74 6f 49 6e      }.    autoIn
24d81 63 53 74 65 70 28 70 50 61 72 73 65 2c 20 72 65  cStep(pParse, re
24d82 67 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77  gAutoinc, regRow
24d83 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73  id);..    /* Pus
24d84 68 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  h onto the stack
24d85 2c 20 64 61 74 61 20 66 6f 72 20 61 6c 6c 20 63  , data for all c
24d86 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 6e 65  olumns of the ne
24d87 77 20 65 6e 74 72 79 2c 20 62 65 67 69 6e 6e 69  w entry, beginni
24d88 6e 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74  ng.    ** with t
24d89 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 2e  he first column.
24d8a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 48 69 64  .    */.    nHid
24d8b 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  den = 0;.    for
24d8c 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
24d8d 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
24d8e 69 6e 74 20 69 52 65 67 53 74 6f 72 65 20 3d 20  int iRegStore = 
24d8f 72 65 67 52 6f 77 69 64 2b 31 2b 69 3b 0a 20 20  regRowid+1+i;.  
24d90 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d      if( i==pTab-
24d91 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
24d92 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f    /* The value o
24d93 66 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52  f the INTEGER PR
24d94 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e  IMARY KEY column
24d95 20 69 73 20 61 6c 77 61 79 73 20 61 20 4e 55 4c   is always a NUL
24d96 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 57 68  L..        ** Wh
24d97 65 6e 65 76 65 72 20 74 68 69 73 20 63 6f 6c 75  enever this colu
24d98 6d 6e 20 69 73 20 72 65 61 64 2c 20 74 68 65 20  mn is read, the 
24d99 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69  record number wi
24d9a 6c 6c 20 62 65 20 73 75 62 73 74 69 74 75 74 65  ll be substitute
24d9b 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20  d.        ** in 
24d9c 69 74 73 20 70 6c 61 63 65 2e 20 20 53 6f 20 77  its place.  So w
24d9d 69 6c 6c 20 66 69 6c 6c 20 74 68 69 73 20 63 6f  ill fill this co
24d9e 6c 75 6d 6e 20 77 69 74 68 20 61 20 4e 55 4c 4c  lumn with a NULL
24d9f 20 74 6f 20 61 76 6f 69 64 0a 20 20 20 20 20 20   to avoid.      
24da0 20 20 2a 2a 20 74 61 6b 69 6e 67 20 75 70 20 64    ** taking up d
24da1 61 74 61 20 73 70 61 63 65 20 77 69 74 68 20 69  ata space with i
24da2 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20  nformation that 
24da3 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73  will never be us
24da4 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73  ed. */.        s
24da5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24da6 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
24da7 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20  iRegStore);.    
24da8 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
24da9 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
24daa 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20  pColumn==0 ){.  
24dab 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64        if( IsHidd
24dac 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e  enColumn(&pTab->
24dad 61 43 6f 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20  aCol[i]) ){.    
24dae 20 20 20 20 20 20 61 73 73 65 72 74 28 20 49 73        assert( Is
24daf 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b  Virtual(pTab) );
24db0 0a 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20 2d  .          j = -
24db1 31 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 48 69  1;.          nHi
24db2 64 64 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20  dden++;.        
24db3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
24db4 20 6a 20 3d 20 69 20 2d 20 6e 48 69 64 64 65 6e   j = i - nHidden
24db5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
24db6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
24db7 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c   for(j=0; j<pCol
24db8 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a  umn->nId; j++){.
24db9 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
24dba 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d  olumn->a[j].idx=
24dbb 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =i ) break;.    
24dbc 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
24dbd 20 20 20 20 69 66 28 20 6a 3c 30 20 7c 7c 20 6e      if( j<0 || n
24dbe 43 6f 6c 75 6d 6e 3d 3d 30 20 7c 7c 20 28 70 43  Column==0 || (pC
24dbf 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c  olumn && j>=pCol
24dc0 75 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a 20 20 20  umn->nId) ){.   
24dc1 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
24dc2 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61  Code(pParse, pTa
24dc3 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74  b->aCol[i].pDflt
24dc4 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20  , iRegStore);.  
24dc5 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73      }else if( us
24dc6 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
24dc7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24dc8 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
24dc9 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c  lumn, srcTab, j,
24dca 20 69 52 65 67 53 74 6f 72 65 29 3b 20 0a 20 20   iRegStore); .  
24dcb 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53      }else if( pS
24dcc 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
24dcd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24dce 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
24dcf 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6a 2c  regFromSelect+j,
24dd0 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20   iRegStore);.   
24dd1 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24dd2 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
24dd3 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d  e(pParse, pList-
24dd4 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 52 65  >a[j].pExpr, iRe
24dd5 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d  gStore);.      }
24dd6 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47  .    }..    /* G
24dd7 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
24dd8 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74  check constraint
24dd9 73 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 69  s and generate i
24dda 6e 64 65 78 20 6b 65 79 73 20 61 6e 64 0a 20 20  ndex keys and.  
24ddb 20 20 2a 2a 20 64 6f 20 74 68 65 20 69 6e 73 65    ** do the inse
24ddc 72 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69  rtion..    */.#i
24ddd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24dde 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
24ddf 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c     if( IsVirtual
24de0 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
24de1 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57  sqlite3VtabMakeW
24de2 72 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20  ritable(pParse, 
24de3 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c  pTab);.      sql
24de4 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
24de5 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20 31 2c  , OP_VUpdate, 1,
24de6 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 72   pTab->nCol+2, r
24de7 65 67 49 6e 73 2c 0a 20 20 20 20 20 20 20 20 20  egIns,.         
24de8 20 20 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e              (con
24de9 73 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70  st char*)pTab->p
24dea 56 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a  Vtab, P4_VTAB);.
24deb 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
24dec 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74  .    {.      int
24ded 20 69 73 52 65 70 6c 61 63 65 3b 20 20 20 20 2f   isReplace;    /
24dee 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66  * Set to true if
24def 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6d 61 79   constraints may
24df0 20 63 61 75 73 65 20 61 20 72 65 70 6c 61 63 65   cause a replace
24df1 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
24df2 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61  3GenerateConstra
24df3 69 6e 74 43 68 65 63 6b 73 28 70 50 61 72 73 65  intChecks(pParse
24df4 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c  , pTab, baseCur,
24df5 20 72 65 67 49 6e 73 2c 20 61 52 65 67 49 64 78   regIns, aRegIdx
24df6 2c 0a 20 20 20 20 20 20 20 20 20 20 6b 65 79 43  ,.          keyC
24df7 6f 6c 75 6d 6e 3e 3d 30 2c 20 30 2c 20 6f 6e 45  olumn>=0, 0, onE
24df8 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c  rror, endOfLoop,
24df9 20 26 69 73 52 65 70 6c 61 63 65 0a 20 20 20 20   &isReplace.    
24dfa 20 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74    );.      sqlit
24dfb 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74  e3CompleteInsert
24dfc 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70  ion(.          p
24dfd 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73  Parse, pTab, bas
24dfe 65 43 75 72 2c 20 72 65 67 49 6e 73 2c 20 61 52  eCur, regIns, aR
24dff 65 67 49 64 78 2c 20 30 2c 0a 20 20 20 20 20 20  egIdx, 0,.      
24e00 20 20 20 20 28 74 6d 61 73 6b 26 54 52 49 47 47      (tmask&TRIGG
24e01 45 52 5f 41 46 54 45 52 29 20 3f 20 6e 65 77 49  ER_AFTER) ? newI
24e02 64 78 20 3a 20 2d 31 2c 20 61 70 70 65 6e 64 46  dx : -1, appendF
24e03 6c 61 67 2c 20 69 73 52 65 70 6c 61 63 65 3d 3d  lag, isReplace==
24e04 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  0.      );.    }
24e05 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
24e06 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72  e the count of r
24e07 6f 77 73 20 74 68 61 74 20 61 72 65 20 69 6e 73  ows that are ins
24e08 65 72 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28  erted.  */.  if(
24e09 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
24e0a 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21  LITE_CountRows)!
24e0b 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
24e0c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24e0d 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77  P_AddImm, regRow
24e0e 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a  Count, 1);.  }..
24e0f 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29    if( pTrigger )
24e10 7b 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 41 46  {.    /* Code AF
24e11 54 45 52 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  TER triggers */.
24e12 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43      if( sqlite3C
24e13 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
24e14 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
24e15 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52  TK_INSERT, 0, TR
24e16 49 47 47 45 52 5f 41 46 54 45 52 2c 20 0a 20 20  IGGER_AFTER, .  
24e17 20 20 20 20 20 20 20 20 70 54 61 62 2c 20 6e 65          pTab, ne
24e18 77 49 64 78 2c 20 2d 31 2c 20 6f 6e 45 72 72 6f  wIdx, -1, onErro
24e19 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 30 2c  r, endOfLoop, 0,
24e1a 20 30 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74   0) ){.      got
24e1b 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
24e1c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
24e1d 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20  * The bottom of 
24e1e 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72 74 69  the main inserti
24e1f 6f 6e 20 6c 6f 6f 70 2c 20 69 66 20 74 68 65 20  on loop, if the 
24e20 64 61 74 61 20 73 6f 75 72 63 65 0a 20 20 2a 2a  data source.  **
24e21 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74 61   is a SELECT sta
24e22 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73  tement..  */.  s
24e23 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
24e24 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c  eLabel(v, endOfL
24e25 6f 6f 70 29 3b 0a 20 20 69 66 28 20 75 73 65 54  oop);.  if( useT
24e26 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
24e27 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24e28 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 72  2(v, OP_Next, sr
24e29 63 54 61 62 2c 20 61 64 64 72 43 6f 6e 74 29 3b  cTab, addrCont);
24e2a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24e2b 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
24e2c 49 6e 73 54 6f 70 29 3b 0a 20 20 20 20 73 71 6c  InsTop);.    sql
24e2d 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
24e2e 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 73 72 63 54  , OP_Close, srcT
24e2f 61 62 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ab);.  }else if(
24e30 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
24e31 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24e32 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
24e33 20 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20   addrCont);.    
24e34 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
24e35 65 72 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f  ere(v, addrInsTo
24e36 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  p);.  }..  if( !
24e37 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
24e38 26 26 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  && !isView ){.  
24e39 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74    /* Close all t
24e3a 61 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f 0a  ables opened */.
24e3b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24e3c 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
24e3d 65 2c 20 62 61 73 65 43 75 72 29 3b 0a 20 20 20  e, baseCur);.   
24e3e 20 66 6f 72 28 69 64 78 3d 31 2c 20 70 49 64 78   for(idx=1, pIdx
24e3f 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
24e40 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
24e41 70 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a 20  pNext, idx++){. 
24e42 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24e43 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
24e44 73 65 2c 20 69 64 78 2b 62 61 73 65 43 75 72 29  se, idx+baseCur)
24e45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
24e46 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 71 6c  * Update the sql
24e47 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
24e48 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68  le by storing th
24e49 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
24e4a 0a 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 76 61  .  ** counter va
24e4b 6c 75 65 20 69 6e 20 6d 65 6d 6f 72 79 20 72 65  lue in memory re
24e4c 67 41 75 74 6f 69 6e 63 20 62 61 63 6b 20 69 6e  gAutoinc back in
24e4d 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  to the sqlite_se
24e4e 71 75 65 6e 63 65 0a 20 20 2a 2a 20 74 61 62 6c  quence.  ** tabl
24e4f 65 2e 0a 20 20 2a 2f 0a 20 20 61 75 74 6f 49 6e  e..  */.  autoIn
24e50 63 45 6e 64 28 70 50 61 72 73 65 2c 20 69 44 62  cEnd(pParse, iDb
24e51 2c 20 70 54 61 62 2c 20 72 65 67 41 75 74 6f 69  , pTab, regAutoi
24e52 6e 63 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  nc);..  /*.  ** 
24e53 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
24e54 72 20 6f 66 20 72 6f 77 73 20 69 6e 73 65 72 74  r of rows insert
24e55 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74  ed. If this rout
24e56 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e  ine is .  ** gen
24e57 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63  erating code bec
24e58 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74  ause of a call t
24e59 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  o sqlite3NestedP
24e5a 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20  arse(), do not. 
24e5b 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63   ** invoke the c
24e5c 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
24e5d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
24e5e 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
24e5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 70 50 61  CountRows && pPa
24e60 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26  rse->nested==0 &
24e61 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53  & !pParse->trigS
24e62 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69  tack ){.    sqli
24e63 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
24e64 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72   OP_ResultRow, r
24e65 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a  egRowCount, 1);.
24e66 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
24e67 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
24e68 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24e69 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
24e6a 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
24e6b 72 6f 77 73 20 69 6e 73 65 72 74 65 64 22 2c 20  rows inserted", 
24e6c 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
24e6d 20 20 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61    }..insert_clea
24e6e 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  nup:.  sqlite3Sr
24e6f 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
24e70 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c  pTabList);.  sql
24e71 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
24e72 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
24e73 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
24e74 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
24e75 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
24e76 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  stDelete(db, pCo
24e77 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  lumn);.  sqlite3
24e78 44 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49  DbFree(db, aRegI
24e79 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  dx);.}../*.** Ge
24e7a 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64  nerate code to d
24e7b 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65  o constraint che
24e7c 63 6b 73 20 70 72 69 6f 72 20 74 6f 20 61 6e 20  cks prior to an 
24e7d 49 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50 44  INSERT or an UPD
24e7e 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ATE..**.** The i
24e7f 6e 70 75 74 20 69 73 20 61 20 72 61 6e 67 65 20  nput is a range 
24e80 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72  of consecutive r
24e81 65 67 69 73 74 65 72 73 20 61 73 20 66 6f 6c 6c  egisters as foll
24e82 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e  ows:.**.**    1.
24e83 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74    The rowid of t
24e84 68 65 20 72 6f 77 20 74 6f 20 62 65 20 75 70 64  he row to be upd
24e85 61 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20  ated before the 
24e86 75 70 64 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a  update.  This.**
24e87 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 69 73          value is
24e88 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65 73 73 20   omitted unless 
24e89 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 6e 20  we are doing an 
24e8a 55 50 44 41 54 45 20 74 68 61 74 20 69 6e 76 6f  UPDATE that invo
24e8b 6c 76 65 73 20 61 0a 2a 2a 20 20 20 20 20 20 20  lves a.**       
24e8c 20 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20 72   change to the r
24e8d 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 72 20  ecord number or 
24e8e 77 72 69 74 69 6e 67 20 74 6f 20 61 20 76 69 72  writing to a vir
24e8f 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  tual table..**.*
24e90 2a 20 20 20 20 32 2e 20 20 54 68 65 20 72 6f 77  *    2.  The row
24e91 69 64 20 6f 66 20 74 68 65 20 72 6f 77 20 61 66  id of the row af
24e92 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a  ter the update..
24e93 2a 2a 0a 2a 2a 20 20 20 20 33 2e 20 20 54 68 65  **.**    3.  The
24e94 20 64 61 74 61 20 69 6e 20 74 68 65 20 66 69 72   data in the fir
24e95 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
24e96 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68 65   entry after the
24e97 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20   update..**.**  
24e98 20 20 69 2e 20 20 44 61 74 61 20 66 72 6f 6d 20    i.  Data from 
24e99 6d 69 64 64 6c 65 20 63 6f 6c 75 6d 6e 73 2e 2e  middle columns..
24e9a 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 2e 20 20 54  ..**.**    N.  T
24e9b 68 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 6c  he data in the l
24e9c 61 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ast column of th
24e9d 65 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68  e entry after th
24e9e 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  e update..**.** 
24e9f 54 68 65 20 72 65 67 52 6f 77 69 64 20 70 61 72  The regRowid par
24ea0 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 69 6e  ameter is the in
24ea1 64 65 78 20 6f 66 20 74 68 65 20 72 65 67 69 73  dex of the regis
24ea2 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 28  ter containing (
24ea3 32 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c  2)..**.** The ol
24ea4 64 20 72 6f 77 69 64 20 73 68 6f 77 6e 20 61 73  d rowid shown as
24ea5 20 65 6e 74 72 79 20 28 31 29 20 61 62 6f 76 65   entry (1) above
24ea6 20 69 73 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65   is omitted unle
24ea7 73 73 20 62 6f 74 68 20 69 73 55 70 64 61 74 65  ss both isUpdate
24ea8 0a 2a 2a 20 61 6e 64 20 72 6f 77 69 64 43 68 6e  .** and rowidChn
24ea9 67 20 61 72 65 20 31 2e 20 20 69 73 55 70 64 61  g are 1.  isUpda
24eaa 74 65 20 69 73 20 74 72 75 65 20 66 6f 72 20 55  te is true for U
24eab 50 44 41 54 45 73 20 61 6e 64 20 66 61 6c 73 65  PDATEs and false
24eac 20 66 6f 72 0a 2a 2a 20 49 4e 53 45 52 54 73 2e   for.** INSERTs.
24ead 20 20 52 6f 77 69 64 43 68 6e 67 20 6d 65 61 6e    RowidChng mean
24eae 73 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 72  s that the new r
24eaf 6f 77 69 64 20 69 73 20 65 78 70 6c 69 63 69 74  owid is explicit
24eb0 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 79 0a  ly specified by.
24eb1 2a 2a 20 74 68 65 20 75 70 64 61 74 65 20 6f 72  ** the update or
24eb2 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 6e   insert statemen
24eb3 74 2e 20 20 49 66 20 72 6f 77 69 64 43 68 6e 67  t.  If rowidChng
24eb4 20 69 73 20 66 61 6c 73 65 2c 20 69 74 20 6d 65   is false, it me
24eb5 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ans that.** the 
24eb6 72 6f 77 69 64 20 69 73 20 63 6f 6d 70 75 74 65  rowid is compute
24eb7 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
24eb8 69 6e 20 61 6e 20 69 6e 73 65 72 74 20 6f 72 20  in an insert or 
24eb9 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 76  that the rowid v
24eba 61 6c 75 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 6d  alue.** is not m
24ebb 6f 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75  odified by the u
24ebc 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  pdate..**.** The
24ebd 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
24ebe 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
24ebf 73 74 6f 72 65 20 6e 65 77 20 69 6e 64 65 78 20  store new index 
24ec0 65 6e 74 72 69 65 73 20 69 6e 74 6f 0a 2a 2a 20  entries into.** 
24ec1 72 65 67 69 73 74 65 72 73 20 69 64 65 6e 74 69  registers identi
24ec2 66 69 65 64 20 62 79 20 61 52 65 67 49 64 78 5b  fied by aRegIdx[
24ec3 5d 2e 20 20 4e 6f 20 69 6e 64 65 78 20 65 6e 74  ].  No index ent
24ec4 72 79 20 69 73 20 63 72 65 61 74 65 64 20 66 6f  ry is created fo
24ec5 72 0a 2a 2a 20 69 6e 64 69 63 65 73 20 77 68 65  r.** indices whe
24ec6 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30  re aRegIdx[i]==0
24ec7 2e 20 20 54 68 65 20 6f 72 64 65 72 20 6f 66 20  .  The order of 
24ec8 69 6e 64 69 63 65 73 20 69 6e 20 61 52 65 67 49  indices in aRegI
24ec9 64 78 5b 5d 20 69 73 0a 2a 2a 20 74 68 65 20 73  dx[] is.** the s
24eca 61 6d 65 20 61 73 20 74 68 65 20 6f 72 64 65 72  ame as the order
24ecb 20 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e 20 74   of indices on t
24ecc 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f  he linked list o
24ecd 66 20 69 6e 64 69 63 65 73 0a 2a 2a 20 61 74 74  f indices.** att
24ece 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 61 62  ached to the tab
24ecf 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  le..**.** This r
24ed0 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65  outine also gene
24ed1 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 63 68  rates code to ch
24ed2 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  eck constraints.
24ed3 20 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 2a 2a 20 43    NOT NULL,.** C
24ed4 48 45 43 4b 2c 20 61 6e 64 20 55 4e 49 51 55 45  HECK, and UNIQUE
24ed5 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65   constraints are
24ed6 20 61 6c 6c 20 63 68 65 63 6b 65 64 2e 20 20 49   all checked.  I
24ed7 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  f a constraint f
24ed8 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  ails,.** then th
24ed9 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63  e appropriate ac
24eda 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65  tion is performe
24edb 64 2e 20 20 54 68 65 72 65 20 61 72 65 20 66 69  d.  There are fi
24edc 76 65 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 61  ve possible.** a
24edd 63 74 69 6f 6e 73 3a 20 52 4f 4c 4c 42 41 43 4b  ctions: ROLLBACK
24ede 2c 20 41 42 4f 52 54 2c 20 46 41 49 4c 2c 20 52  , ABORT, FAIL, R
24edf 45 50 4c 41 43 45 2c 20 61 6e 64 20 49 47 4e 4f  EPLACE, and IGNO
24ee0 52 45 2e 0a 2a 2a 0a 2a 2a 20 20 43 6f 6e 73 74  RE..**.**  Const
24ee1 72 61 69 6e 74 20 74 79 70 65 20 20 41 63 74 69  raint type  Acti
24ee2 6f 6e 20 20 20 20 20 20 20 57 68 61 74 20 48 61  on       What Ha
24ee3 70 70 65 6e 73 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d  ppens.**  ------
24ee4 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d  ---------  -----
24ee5 2d 2d 2d 2d 2d 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  -----   --------
24ee6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24ee7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24ee8 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
24ee9 20 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 20        ROLLBACK  
24eea 20 20 20 54 68 65 20 63 75 72 72 65 6e 74 20 74     The current t
24eeb 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
24eec 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64 0a 2a 2a  lled back and.**
24eed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24eee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24eef 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72  sqlite3_exec() r
24ef0 65 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65  eturns immediate
24ef1 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20  ly with a.**    
24ef2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ef3 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
24ef4 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54  rn code of SQLIT
24ef5 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a  E_CONSTRAINT..**
24ef6 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
24ef7 20 20 20 20 20 20 41 42 4f 52 54 20 20 20 20 20        ABORT     
24ef8 20 20 20 42 61 63 6b 20 6f 75 74 20 63 68 61 6e     Back out chan
24ef9 67 65 73 20 66 72 6f 6d 20 74 68 65 20 63 75 72  ges from the cur
24efa 72 65 6e 74 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20  rent command.** 
24efb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24efc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f                 o
24efd 6e 6c 79 20 28 64 6f 20 6e 6f 74 20 64 6f 20 61  nly (do not do a
24efe 20 63 6f 6d 70 6c 65 74 65 20 72 6f 6c 6c 62 61   complete rollba
24eff 63 6b 29 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20  ck) then.**     
24f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f01 20 20 20 20 20 20 20 20 20 20 20 63 61 75 73 65             cause
24f02 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
24f03 74 6f 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69  to return immedi
24f04 61 74 65 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20  ately.**        
24f05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f06 20 20 20 20 20 20 20 20 77 69 74 68 20 53 51 4c          with SQL
24f07 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a  ITE_CONSTRAINT..
24f08 2a 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20  **.**  any      
24f09 20 20 20 20 20 20 20 20 46 41 49 4c 20 20 20 20          FAIL    
24f0a 20 20 20 20 20 53 71 6c 69 74 65 5f 65 78 65 63       Sqlite_exec
24f0b 28 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65 64  () returns immed
24f0c 69 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a  iately with a.**
24f0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f0f 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53  return code of S
24f10 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
24f11 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20  .  The.**       
24f12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f13 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 61 63           transac
24f14 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c  tion is not roll
24f15 65 64 20 62 61 63 6b 20 61 6e 64 20 61 6e 79 0a  ed back and any.
24f16 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
24f17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f18 20 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20    prior changes 
24f19 61 72 65 20 72 65 74 61 69 6e 65 64 2e 0a 2a 2a  are retained..**
24f1a 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20  .**  any        
24f1b 20 20 20 20 20 20 49 47 4e 4f 52 45 20 20 20 20        IGNORE    
24f1c 20 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75     The record nu
24f1d 6d 62 65 72 20 61 6e 64 20 64 61 74 61 20 69 73  mber and data is
24f1e 20 70 6f 70 70 65 64 20 66 72 6f 6d 0a 2a 2a 20   popped from.** 
24f1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
24f21 68 65 20 73 74 61 63 6b 20 61 6e 64 20 74 68 65  he stack and the
24f22 72 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61  re is an immedia
24f23 74 65 20 6a 75 6d 70 0a 2a 2a 20 20 20 20 20 20  te jump.**      
24f24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f25 20 20 20 20 20 20 20 20 20 20 74 6f 20 6c 61 62            to lab
24f26 65 6c 20 69 67 6e 6f 72 65 44 65 73 74 2e 0a 2a  el ignoreDest..*
24f27 2a 0a 2a 2a 20 20 4e 4f 54 20 4e 55 4c 4c 20 20  *.**  NOT NULL  
24f28 20 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20         REPLACE  
24f29 20 20 20 20 54 68 65 20 4e 55 4c 4c 20 76 61 6c      The NULL val
24f2a 75 65 20 69 73 20 72 65 70 6c 61 63 65 20 62 79  ue is replace by
24f2b 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20   the default.** 
24f2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
24f2e 61 6c 75 65 20 66 6f 72 20 74 68 61 74 20 63 6f  alue for that co
24f2f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 65 20 64 65  lumn.  If the de
24f30 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 20  fault value.**  
24f31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
24f33 20 4e 55 4c 4c 2c 20 74 68 65 20 61 63 74 69 6f   NULL, the actio
24f34 6e 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  n is the same as
24f35 20 41 42 4f 52 54 2e 0a 2a 2a 0a 2a 2a 20 20 55   ABORT..**.**  U
24f36 4e 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20  NIQUE           
24f37 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68 65  REPLACE      The
24f38 20 6f 74 68 65 72 20 72 6f 77 20 74 68 61 74 20   other row that 
24f39 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 74  conflicts with t
24f3a 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20  he row.**       
24f3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f3c 20 20 20 20 20 20 20 20 20 62 65 69 6e 67 20 69           being i
24f3d 6e 73 65 72 74 65 64 20 69 73 20 72 65 6d 6f 76  nserted is remov
24f3e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 43 48 45 43 4b  ed..**.**  CHECK
24f3f 20 20 20 20 20 20 20 20 20 20 20 20 52 45 50 4c              REPL
24f40 41 43 45 20 20 20 20 20 20 49 6c 6c 65 67 61 6c  ACE      Illegal
24f41 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 69  .  The results i
24f42 6e 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a  n an exception..
24f43 2a 2a 0a 2a 2a 20 57 68 69 63 68 20 61 63 74 69  **.** Which acti
24f44 6f 6e 20 74 6f 20 74 61 6b 65 20 69 73 20 64 65  on to take is de
24f45 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
24f46 6f 76 65 72 72 69 64 65 45 72 72 6f 72 20 70 61  overrideError pa
24f47 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 72 20 69  rameter..** Or i
24f48 66 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3d  f overrideError=
24f49 3d 4f 45 5f 44 65 66 61 75 6c 74 2c 20 74 68 65  =OE_Default, the
24f4a 6e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 6f 6e  n the pParse->on
24f4b 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 0a  Error parameter.
24f4c 2a 2a 20 69 73 20 75 73 65 64 2e 20 20 4f 72 20  ** is used.  Or 
24f4d 69 66 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72  if pParse->onErr
24f4e 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 74  or==OE_Default t
24f4f 68 65 6e 20 74 68 65 20 6f 6e 45 72 72 6f 72 20  hen the onError 
24f50 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65  value.** for the
24f51 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75   constraint is u
24f52 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  sed..**.** The c
24f53 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d  alling routine m
24f54 75 73 74 20 6f 70 65 6e 20 61 20 72 65 61 64 2f  ust open a read/
24f55 77 72 69 74 65 20 63 75 72 73 6f 72 20 66 6f 72  write cursor for
24f56 20 70 54 61 62 20 77 69 74 68 0a 2a 2a 20 63 75   pTab with.** cu
24f57 72 73 6f 72 20 6e 75 6d 62 65 72 20 22 62 61 73  rsor number "bas
24f58 65 43 75 72 22 2e 20 20 41 6c 6c 20 69 6e 64 69  eCur".  All indi
24f59 63 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74  ces of pTab must
24f5a 20 61 6c 73 6f 20 68 61 76 65 20 6f 70 65 6e 0a   also have open.
24f5b 2a 2a 20 72 65 61 64 2f 77 72 69 74 65 20 63 75  ** read/write cu
24f5c 72 73 6f 72 73 20 77 69 74 68 20 63 75 72 73 6f  rsors with curso
24f5d 72 20 6e 75 6d 62 65 72 20 62 61 73 65 43 75 72  r number baseCur
24f5e 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20  +i for the i-th 
24f5f 63 75 72 73 6f 72 2e 0a 2a 2a 20 45 78 63 65 70  cursor..** Excep
24f60 74 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  t, if there is n
24f61 6f 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66  o possibility of
24f62 20 61 20 52 45 50 4c 41 43 45 20 61 63 74 69 6f   a REPLACE actio
24f63 6e 20 74 68 65 6e 0a 2a 2a 20 63 75 72 73 6f 72  n then.** cursor
24f64 73 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  s do not need to
24f65 20 62 65 20 6f 70 65 6e 20 66 6f 72 20 69 6e 64   be open for ind
24f66 69 63 65 73 20 77 68 65 72 65 20 61 52 65 67 49  ices where aRegI
24f67 64 78 5b 69 5d 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c  dx[i]==0..*/.SQL
24f68 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
24f69 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
24f6a 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
24f6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
24f6c 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  e,      /* The p
24f6d 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
24f6e 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
24f6f 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61         /* the ta
24f70 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
24f71 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
24f72 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72  */.  int baseCur
24f73 2c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  ,        /* Inde
24f74 78 20 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74  x of a read/writ
24f75 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  e cursor pointin
24f76 67 20 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 69  g at pTab */.  i
24f77 6e 74 20 72 65 67 52 6f 77 69 64 2c 20 20 20 20  nt regRowid,    
24f78 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
24f79 68 65 20 72 61 6e 67 65 20 6f 66 20 69 6e 70 75  he range of inpu
24f7a 74 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20  t registers */. 
24f7b 20 69 6e 74 20 2a 61 52 65 67 49 64 78 2c 20 20   int *aRegIdx,  
24f7c 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
24f7d 20 75 73 65 64 20 62 79 20 65 61 63 68 20 69 6e   used by each in
24f7e 64 65 78 2e 20 20 30 20 66 6f 72 20 75 6e 75 73  dex.  0 for unus
24f7f 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ed indices */.  
24f80 69 6e 74 20 72 6f 77 69 64 43 68 6e 67 2c 20 20  int rowidChng,  
24f81 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
24f82 68 65 20 72 6f 77 69 64 20 6d 69 67 68 74 20 63  he rowid might c
24f83 6f 6c 6c 69 64 65 20 77 69 74 68 20 65 78 69 73  ollide with exis
24f84 74 69 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20  ting entry */.  
24f85 69 6e 74 20 69 73 55 70 64 61 74 65 2c 20 20 20  int isUpdate,   
24f86 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
24f87 55 50 44 41 54 45 2c 20 46 61 6c 73 65 20 66 6f  UPDATE, False fo
24f88 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e  r INSERT */.  in
24f89 74 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 2c  t overrideError,
24f8a 20 20 2f 2a 20 4f 76 65 72 72 69 64 65 20 6f 6e    /* Override on
24f8b 45 72 72 6f 72 20 74 6f 20 74 68 69 73 20 69 66  Error to this if
24f8c 20 6e 6f 74 20 4f 45 5f 44 65 66 61 75 6c 74 20   not OE_Default 
24f8d 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 44  */.  int ignoreD
24f8e 65 73 74 2c 20 20 20 20 20 2f 2a 20 4a 75 6d 70  est,     /* Jump
24f8f 20 74 6f 20 74 68 69 73 20 6c 61 62 65 6c 20 6f   to this label o
24f90 6e 20 61 6e 20 4f 45 5f 49 67 6e 6f 72 65 20 72  n an OE_Ignore r
24f91 65 73 6f 6c 75 74 69 6f 6e 20 2a 2f 0a 20 20 69  esolution */.  i
24f92 6e 74 20 2a 70 62 4d 61 79 52 65 70 6c 61 63 65  nt *pbMayReplace
24f93 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74     /* OUT: Set t
24f94 6f 20 74 72 75 65 20 69 66 20 63 6f 6e 73 74 72  o true if constr
24f95 61 69 6e 74 20 6d 61 79 20 63 61 75 73 65 20 61  aint may cause a
24f96 20 72 65 70 6c 61 63 65 20 2a 2f 0a 29 7b 0a 20   replace */.){. 
24f97 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
24f98 20 20 20 20 20 2f 2a 20 6c 6f 6f 70 20 63 6f 75       /* loop cou
24f99 6e 74 65 72 20 2a 2f 0a 20 20 56 64 62 65 20 2a  nter */.  Vdbe *
24f9a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  v;            /*
24f9b 20 56 44 42 45 20 75 6e 64 65 72 20 63 6f 6e 73   VDBE under cons
24f9c 74 72 75 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  trution */.  int
24f9d 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   nCol;          
24f9e 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
24f9f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6f  lumns */.  int o
24fa0 6e 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 2f  nError;        /
24fa1 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c  * Conflict resol
24fa2 75 74 69 6f 6e 20 73 74 72 61 74 65 67 79 20 2a  ution strategy *
24fa3 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20  /.  int j1;     
24fa4 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
24fa5 73 73 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74  sss of jump inst
24fa6 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  ruction */.  int
24fa7 20 6a 32 20 3d 20 30 2c 20 6a 33 3b 20 20 20 20   j2 = 0, j3;    
24fa8 20 2f 2a 20 41 64 64 72 65 73 73 65 73 20 6f 66   /* Addresses of
24fa9 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f   jump instructio
24faa 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44  ns */.  int regD
24fab 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 52  ata;        /* R
24fac 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69  egister containi
24fad 6e 67 20 66 69 72 73 74 20 64 61 74 61 20 63 6f  ng first data co
24fae 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43  lumn */.  int iC
24faf 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ur;           /*
24fb0 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75   Table cursor nu
24fb1 6d 62 65 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20  mber */.  Index 
24fb2 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f  *pIdx;         /
24fb3 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6f 6e 65  * Pointer to one
24fb4 20 6f 66 20 74 68 65 20 69 6e 64 69 63 65 73 20   of the indices 
24fb5 2a 2f 0a 20 20 69 6e 74 20 73 65 65 6e 52 65 70  */.  int seenRep
24fb6 6c 61 63 65 20 3d 20 30 3b 20 2f 2a 20 54 72 75  lace = 0; /* Tru
24fb7 65 20 69 66 20 52 45 50 4c 41 43 45 20 69 73 20  e if REPLACE is 
24fb8 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20  used to resolve 
24fb9 49 4e 54 20 50 4b 20 63 6f 6e 66 6c 69 63 74 20  INT PK conflict 
24fba 2a 2f 0a 20 20 69 6e 74 20 68 61 73 54 77 6f 52  */.  int hasTwoR
24fbb 6f 77 69 64 73 20 3d 20 28 69 73 55 70 64 61 74  owids = (isUpdat
24fbc 65 20 26 26 20 72 6f 77 69 64 43 68 6e 67 29 3b  e && rowidChng);
24fbd 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
24fbe 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
24fbf 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
24fc0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
24fc1 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20  ->pSelect==0 ); 
24fc2 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69   /* This table i
24fc3 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a  s not a VIEW */.
24fc4 20 20 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e    nCol = pTab->n
24fc5 43 6f 6c 3b 0a 20 20 72 65 67 44 61 74 61 20 3d  Col;.  regData =
24fc6 20 72 65 67 52 6f 77 69 64 20 2b 20 31 3b 0a 0a   regRowid + 1;..
24fc7 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20 4e  .  /* Test all N
24fc8 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
24fc9 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  nts..  */.  for(
24fca 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
24fcb 29 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d 70 54  ){.    if( i==pT
24fcc 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
24fcd 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
24fce 20 7d 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d   }.    onError =
24fcf 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e   pTab->aCol[i].n
24fd0 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20 69 66 28 20  otNull;.    if( 
24fd1 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65  onError==OE_None
24fd2 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
24fd3 20 69 66 28 20 6f 76 65 72 72 69 64 65 45 72 72   if( overrideErr
24fd4 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29  or!=OE_Default )
24fd5 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20  {.      onError 
24fd6 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b  = overrideError;
24fd7 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  .    }else if( o
24fd8 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
24fd9 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
24fda 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ror = OE_Abort;.
24fdb 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 6e      }.    if( on
24fdc 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
24fdd 65 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  e && pTab->aCol[
24fde 69 5d 2e 70 44 66 6c 74 3d 3d 30 20 29 7b 0a 20  i].pDflt==0 ){. 
24fdf 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f       onError = O
24fe0 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
24fe1 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72     assert( onErr
24fe2 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20  or==OE_Rollback 
24fe3 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41  || onError==OE_A
24fe4 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d  bort || onError=
24fe5 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20  =OE_Fail.       
24fe6 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
24fe7 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72 72 6f  Ignore || onErro
24fe8 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b  r==OE_Replace );
24fe9 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45  .    switch( onE
24fea 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 63 61  rror ){.      ca
24feb 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a  se OE_Rollback:.
24fec 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62        case OE_Ab
24fed 6f 72 74 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ort:.      case 
24fee 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20  OE_Fail: {.     
24fef 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20     char *zMsg;. 
24ff0 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69         j1 = sqli
24ff1 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
24ff2 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 2c 0a   OP_HaltIfNull,.
24ff3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ff4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ff5 20 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41    SQLITE_CONSTRA
24ff6 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 72 65  INT, onError, re
24ff7 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20  gData+i);.      
24ff8 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
24ff9 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e  MPrintf(pParse->
24ffa 64 62 2c 20 22 25 73 2e 25 73 20 6d 61 79 20 6e  db, "%s.%s may n
24ffb 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a 20 20 20  ot be NULL",.   
24ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ffd 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2d             pTab-
24ffe 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43  >zName, pTab->aC
24fff 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  ol[i].zName);.  
25000 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25001 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
25002 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49   zMsg, P4_DYNAMI
25003 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  C);.        brea
25004 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
25005 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a   case OE_Ignore:
25006 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
25007 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25008 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 44 61  OP_IsNull, regDa
25009 74 61 2b 69 2c 20 69 67 6e 6f 72 65 44 65 73 74  ta+i, ignoreDest
2500a 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
2500b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2500c 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
2500d 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72     assert( onErr
2500e 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or==OE_Replace )
2500f 3b 0a 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73  ;.        j1 = s
25010 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
25011 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20  (v, OP_NotNull, 
25012 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20  regData+i);.    
25013 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
25014 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ode(pParse, pTab
25015 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c  ->aCol[i].pDflt,
25016 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20   regData+i);.   
25017 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25018 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
25019 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2501a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2501b 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c  }..  /* Test all
2501c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
2501d 74 73 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  ts.  */.#ifndef 
2501e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
2501f 4b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 43  K.  if( pTab->pC
25020 68 65 63 6b 20 26 26 20 28 70 50 61 72 73 65 2d  heck && (pParse-
25021 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
25022 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73  ITE_IgnoreChecks
25023 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
25024 61 6c 6c 4f 6b 20 3d 20 73 71 6c 69 74 65 33 56  allOk = sqlite3V
25025 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
25026 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6b 42  .    pParse->ckB
25027 61 73 65 20 3d 20 72 65 67 44 61 74 61 3b 0a 20  ase = regData;. 
25028 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
25029 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 54 61  True(pParse, pTa
2502a 62 2d 3e 70 43 68 65 63 6b 2c 20 61 6c 6c 4f 6b  b->pCheck, allOk
2502b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
2502c 55 4c 4c 29 3b 0a 20 20 20 20 6f 6e 45 72 72 6f  ULL);.    onErro
2502d 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f  r = overrideErro
2502e 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 3f 20  r!=OE_Default ? 
2502f 6f 76 65 72 72 69 64 65 45 72 72 6f 72 20 3a 20  overrideError : 
25030 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66  OE_Abort;.    if
25031 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67  ( onError==OE_Ig
25032 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 73 71  nore ){.      sq
25033 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25034 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
25035 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20  gnoreDest);.    
25036 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
25037 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25038 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
25039 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e  E_CONSTRAINT, on
2503a 45 72 72 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Error);.    }.  
2503b 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2503c 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 6c 6c  olveLabel(v, all
2503d 4f 6b 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  Ok);.  }.#endif 
2503e 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
2503f 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a  TE_OMIT_CHECK) *
25040 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61  /..  /* If we ha
25041 76 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  ve an INTEGER PR
25042 49 4d 41 52 59 20 4b 45 59 2c 20 6d 61 6b 65 20  IMARY KEY, make 
25043 73 75 72 65 20 74 68 65 20 70 72 69 6d 61 72 79  sure the primary
25044 20 6b 65 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65   key.  ** of the
25045 20 6e 65 77 20 72 65 63 6f 72 64 20 64 6f 65 73   new record does
25046 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20   not previously 
25047 65 78 69 73 74 2e 20 20 45 78 63 65 70 74 2c 20  exist.  Except, 
25048 69 66 20 74 68 69 73 0a 20 20 2a 2a 20 69 73 20  if this.  ** is 
25049 61 6e 20 55 50 44 41 54 45 20 61 6e 64 20 74 68  an UPDATE and th
2504a 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73  e primary key is
2504b 20 6e 6f 74 20 63 68 61 6e 67 69 6e 67 2c 20 74   not changing, t
2504c 68 61 74 20 69 73 20 4f 4b 2e 0a 20 20 2a 2f 0a  hat is OK..  */.
2504d 20 20 69 66 28 20 72 6f 77 69 64 43 68 6e 67 20    if( rowidChng 
2504e 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  ){.    onError =
2504f 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 3b 0a   pTab->keyConf;.
25050 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 65      if( override
25051 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c  Error!=OE_Defaul
25052 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72  t ){.      onErr
25053 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72  or = overrideErr
25054 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  or;.    }else if
25055 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ( onError==OE_De
25056 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f  fault ){.      o
25057 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72  nError = OE_Abor
25058 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  t;.    }.    .  
25059 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f    if( onError!=O
2505a 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54 61  E_Replace || pTa
2505b 62 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  b->pIndex ){.   
2505c 20 20 20 69 66 28 20 69 73 55 70 64 61 74 65 20     if( isUpdate 
2505d 29 7b 0a 20 20 20 20 20 20 20 20 6a 32 20 3d 20  ){.        j2 = 
2505e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2505f 33 28 76 2c 20 4f 50 5f 45 71 2c 20 72 65 67 52  3(v, OP_Eq, regR
25060 6f 77 69 64 2c 20 30 2c 20 72 65 67 52 6f 77 69  owid, 0, regRowi
25061 64 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  d-1);.      }.  
25062 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33      j3 = sqlite3
25063 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
25064 5f 4e 6f 74 45 78 69 73 74 73 2c 20 62 61 73 65  _NotExists, base
25065 43 75 72 2c 20 30 2c 20 72 65 67 52 6f 77 69 64  Cur, 0, regRowid
25066 29 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  );.      switch(
25067 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20   onError ){.    
25068 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
25069 20 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72           onError
2506a 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
2506b 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74         /* Fall t
2506c 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65 78  hru into the nex
2506d 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20  t case */.      
2506e 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65    }.        case
2506f 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20   OE_Rollback:.  
25070 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62        case OE_Ab
25071 6f 72 74 3a 0a 20 20 20 20 20 20 20 20 63 61 73  ort:.        cas
25072 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20  e OE_Fail: {.   
25073 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
25074 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48  beAddOp4(v, OP_H
25075 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53  alt, SQLITE_CONS
25076 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c  TRAINT, onError,
25077 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
25078 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
25079 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74  PRIMARY KEY must
2507a 20 62 65 20 75 6e 69 71 75 65 22 2c 20 50 34 5f   be unique", P4_
2507b 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
2507c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2507d 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65    }.        case
2507e 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a 20   OE_Replace: {. 
2507f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25080 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
25081 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70  Delete(pParse, p
25082 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 30 29  Tab, baseCur, 0)
25083 3b 0a 20 20 20 20 20 20 20 20 20 20 73 65 65 6e  ;.          seen
25084 52 65 70 6c 61 63 65 20 3d 20 31 3b 0a 20 20 20  Replace = 1;.   
25085 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
25086 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
25087 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20  case OE_Ignore: 
25088 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
25089 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65 3d  rt( seenReplace=
2508a 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
2508b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2508c 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2508d 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20   ignoreDest);.  
2508e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2508f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
25090 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
25091 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33  beJumpHere(v, j3
25092 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 55  );.      if( isU
25093 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20  pdate ){.       
25094 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
25095 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20  Here(v, j2);.   
25096 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
25097 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20 55 4e    /* Test all UN
25098 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 73  IQUE constraints
25099 20 62 79 20 63 72 65 61 74 69 6e 67 20 65 6e 74   by creating ent
2509a 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 55 4e  ries for each UN
2509b 49 51 55 45 0a 20 20 2a 2a 20 69 6e 64 65 78 20  IQUE.  ** index 
2509c 61 6e 64 20 6d 61 6b 69 6e 67 20 73 75 72 65 20  and making sure 
2509d 74 68 61 74 20 64 75 70 6c 69 63 61 74 65 20 65  that duplicate e
2509e 6e 74 72 69 65 73 20 64 6f 20 6e 6f 74 20 61 6c  ntries do not al
2509f 72 65 61 64 79 20 65 78 69 73 74 2e 0a 20 20 2a  ready exist..  *
250a0 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20 72 65  * Add the new re
250a1 63 6f 72 64 73 20 74 6f 20 74 68 65 20 69 6e 64  cords to the ind
250a2 69 63 65 73 20 61 73 20 77 65 20 67 6f 2e 0a 20  ices as we go.. 
250a3 20 2a 2f 0a 20 20 66 6f 72 28 69 43 75 72 3d 30   */.  for(iCur=0
250a4 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
250a5 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
250a6 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 43 75  pIdx->pNext, iCu
250a7 72 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  r++){.    int re
250a8 67 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 72 65  gIdx;.    int re
250a9 67 52 3b 0a 0a 20 20 20 20 69 66 28 20 61 52 65  gR;..    if( aRe
250aa 67 49 64 78 5b 69 43 75 72 5d 3d 3d 30 20 29 20  gIdx[iCur]==0 ) 
250ab 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 53 6b  continue;  /* Sk
250ac 69 70 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65  ip unused indice
250ad 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 72 65  s */..    /* Cre
250ae 61 74 65 20 61 20 6b 65 79 20 66 6f 72 20 61 63  ate a key for ac
250af 63 65 73 73 69 6e 67 20 74 68 65 20 69 6e 64 65  cessing the inde
250b0 78 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20 20 72  x entry */.    r
250b1 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33 47  egIdx = sqlite3G
250b2 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
250b3 73 65 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  se, pIdx->nColum
250b4 6e 2b 31 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  n+1);.    for(i=
250b5 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; i<pIdx->nColu
250b6 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
250b7 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d 3e  int idx = pIdx->
250b8 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20  aiColumn[i];.   
250b9 20 20 20 69 66 28 20 69 64 78 3d 3d 70 54 61 62     if( idx==pTab
250ba 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
250bb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
250bc 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
250bd 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67 49  , regRowid, regI
250be 64 78 2b 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c  dx+i);.      }el
250bf 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
250c0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
250c1 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 44 61   OP_SCopy, regDa
250c2 74 61 2b 69 64 78 2c 20 72 65 67 49 64 78 2b 69  ta+idx, regIdx+i
250c3 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
250c4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
250c5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
250c6 70 79 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65  py, regRowid, re
250c7 67 49 64 78 2b 69 29 3b 0a 20 20 20 20 73 71 6c  gIdx+i);.    sql
250c8 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
250c9 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
250ca 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e   regIdx, pIdx->n
250cb 43 6f 6c 75 6d 6e 2b 31 2c 20 61 52 65 67 49 64  Column+1, aRegId
250cc 78 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 73 71  x[iCur]);.    sq
250cd 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
250ce 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b 0a  tyStr(v, pIdx);.
250cf 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
250d0 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
250d1 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 49 64  ge(pParse, regId
250d2 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  x, pIdx->nColumn
250d3 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e  +1);..    /* Fin
250d4 64 20 6f 75 74 20 77 68 61 74 20 61 63 74 69 6f  d out what actio
250d5 6e 20 74 6f 20 74 61 6b 65 20 69 6e 20 63 61 73  n to take in cas
250d6 65 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e  e there is an in
250d7 64 65 78 69 6e 67 20 63 6f 6e 66 6c 69 63 74 20  dexing conflict 
250d8 2a 2f 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  */.    onError =
250d9 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a   pIdx->onError;.
250da 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d      if( onError=
250db 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 20 0a 20 20 20  =OE_None ){ .   
250dc 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
250dd 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
250de 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d  e, regIdx, pIdx-
250df 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20  >nColumn+1);.   
250e0 20 20 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a     continue;  /*
250e1 20 70 49 64 78 20 69 73 20 6e 6f 74 20 61 20 55   pIdx is not a U
250e2 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a 20  NIQUE index */. 
250e3 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 76 65     }.    if( ove
250e4 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44  rrideError!=OE_D
250e5 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
250e6 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69  onError = overri
250e7 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c  deError;.    }el
250e8 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d  se if( onError==
250e9 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
250ea 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
250eb 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
250ec 20 20 69 66 28 20 73 65 65 6e 52 65 70 6c 61 63    if( seenReplac
250ed 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6f  e ){.      if( o
250ee 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72  nError==OE_Ignor
250ef 65 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45  e ) onError = OE
250f0 5f 52 65 70 6c 61 63 65 3b 0a 20 20 20 20 20 20  _Replace;.      
250f1 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72  else if( onError
250f2 3d 3d 4f 45 5f 46 61 69 6c 20 29 20 6f 6e 45 72  ==OE_Fail ) onEr
250f3 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ror = OE_Abort;.
250f4 20 20 20 20 7d 0a 20 20 20 20 0a 0a 20 20 20 20      }.    ..    
250f5 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
250f6 69 66 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78  if the new index
250f7 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 75   entry will be u
250f8 6e 69 71 75 65 20 2a 2f 0a 20 20 20 20 72 65 67  nique */.    reg
250f9 52 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  R = sqlite3GetTe
250fa 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
250fb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
250fc 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
250fd 2c 20 72 65 67 52 6f 77 69 64 2d 68 61 73 54 77  , regRowid-hasTw
250fe 6f 52 6f 77 69 64 73 2c 20 72 65 67 52 29 3b 0a  oRowids, regR);.
250ff 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33      j3 = sqlite3
25100 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
25101 5f 49 73 55 6e 69 71 75 65 2c 20 62 61 73 65 43  _IsUnique, baseC
25102 75 72 2b 69 43 75 72 2b 31 2c 20 30 2c 0a 20 20  ur+iCur+1, 0,.  
25103 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25104 20 20 20 20 20 20 20 20 20 72 65 67 52 2c 20 53           regR, S
25105 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
25106 28 72 65 67 49 64 78 29 2c 0a 20 20 20 20 20 20  (regIdx),.      
25107 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25108 20 20 20 20 20 50 34 5f 49 4e 54 33 32 29 3b 0a       P4_INT32);.
25109 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2510a 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
2510b 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78  se, regIdx, pIdx
2510c 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20  ->nColumn+1);.. 
2510d 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63     /* Generate c
2510e 6f 64 65 20 74 68 61 74 20 65 78 65 63 75 74 65  ode that execute
2510f 73 20 69 66 20 74 68 65 20 6e 65 77 20 69 6e 64  s if the new ind
25110 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f 74 20  ex entry is not 
25111 75 6e 69 71 75 65 20 2a 2f 0a 20 20 20 20 61 73  unique */.    as
25112 73 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f  sert( onError==O
25113 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e  E_Rollback || on
25114 45 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20  Error==OE_Abort 
25115 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46  || onError==OE_F
25116 61 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f  ail.        || o
25117 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72  nError==OE_Ignor
25118 65 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  e || onError==OE
25119 5f 52 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20  _Replace );.    
2511a 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20  switch( onError 
2511b 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  ){.      case OE
2511c 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20  _Rollback:.     
2511d 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a   case OE_Abort:.
2511e 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61        case OE_Fa
2511f 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e  il: {.        in
25120 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 53 74 72  t j;.        Str
25121 41 63 63 75 6d 20 65 72 72 4d 73 67 3b 0a 20 20  Accum errMsg;.  
25122 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
25123 20 2a 7a 53 65 70 3b 0a 20 20 20 20 20 20 20 20   *zSep;.        
25124 63 68 61 72 20 2a 7a 45 72 72 3b 0a 0a 20 20 20  char *zErr;..   
25125 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
25126 63 63 75 6d 49 6e 69 74 28 26 65 72 72 4d 73 67  ccumInit(&errMsg
25127 2c 20 30 2c 20 30 2c 20 32 30 30 29 3b 0a 20 20  , 0, 0, 200);.  
25128 20 20 20 20 20 20 65 72 72 4d 73 67 2e 64 62 20        errMsg.db 
25129 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2512a 20 20 20 20 20 20 7a 53 65 70 20 3d 20 70 49 64        zSep = pId
2512b 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22  x->nColumn>1 ? "
2512c 63 6f 6c 75 6d 6e 73 20 22 20 3a 20 22 63 6f 6c  columns " : "col
2512d 75 6d 6e 20 22 3b 0a 20 20 20 20 20 20 20 20 66  umn ";.        f
2512e 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e  or(j=0; j<pIdx->
2512f 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20  nColumn; j++){. 
25130 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
25131 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  Col = pTab->aCol
25132 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  [pIdx->aiColumn[
25133 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  j]].zName;.     
25134 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
25135 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72 72 4d  ccumAppend(&errM
25136 73 67 2c 20 7a 53 65 70 2c 20 2d 31 29 3b 0a 20  sg, zSep, -1);. 
25137 20 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20           zSep = 
25138 22 2c 20 22 3b 0a 20 20 20 20 20 20 20 20 20 20  ", ";.          
25139 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
2513a 70 70 65 6e 64 28 26 65 72 72 4d 73 67 2c 20 7a  ppend(&errMsg, z
2513b 43 6f 6c 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  Col, -1);.      
2513c 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
2513d 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
2513e 64 28 26 65 72 72 4d 73 67 2c 0a 20 20 20 20 20  d(&errMsg,.     
2513f 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f         pIdx->nCo
25140 6c 75 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20 6e  lumn>1 ? " are n
25141 6f 74 20 75 6e 69 71 75 65 22 20 3a 20 22 20 69  ot unique" : " i
25142 73 20 6e 6f 74 20 75 6e 69 71 75 65 22 2c 20 2d  s not unique", -
25143 31 29 3b 0a 20 20 20 20 20 20 20 20 7a 45 72 72  1);.        zErr
25144 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63   = sqlite3StrAcc
25145 75 6d 46 69 6e 69 73 68 28 26 65 72 72 4d 73 67  umFinish(&errMsg
25146 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
25147 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
25148 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f  OP_Halt, SQLITE_
25149 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72  CONSTRAINT, onEr
2514a 72 6f 72 2c 20 30 2c 20 7a 45 72 72 2c 20 30 29  ror, 0, zErr, 0)
2514b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2514c 33 44 62 46 72 65 65 28 65 72 72 4d 73 67 2e 64  3DbFree(errMsg.d
2514d 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
2514e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2514f 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49  .      case OE_I
25150 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20  gnore: {.       
25151 20 61 73 73 65 72 74 28 20 73 65 65 6e 52 65 70   assert( seenRep
25152 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lace==0 );.     
25153 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25154 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
25155 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b   0, ignoreDest);
25156 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
25157 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65        }.      de
25158 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
25159 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72 6f 72   assert( onError
2515a 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b 0a  ==OE_Replace );.
2515b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
2515c 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65  enerateRowDelete
2515d 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62  (pParse, pTab, b
2515e 61 73 65 43 75 72 2c 20 72 65 67 52 2c 20 30 29  aseCur, regR, 0)
2515f 3b 0a 20 20 20 20 20 20 20 20 73 65 65 6e 52 65  ;.        seenRe
25160 70 6c 61 63 65 20 3d 20 31 3b 0a 20 20 20 20 20  place = 1;.     
25161 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
25162 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
25163 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
25164 76 2c 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69  v, j3);.    sqli
25165 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
25166 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 29 3b  g(pParse, regR);
25167 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 62 4d 61  .  }..  if( pbMa
25168 79 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20  yReplace ){.    
25169 2a 70 62 4d 61 79 52 65 70 6c 61 63 65 20 3d 20  *pbMayReplace = 
2516a 73 65 65 6e 52 65 70 6c 61 63 65 3b 0a 20 20 7d  seenReplace;.  }
2516b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2516c 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
2516d 20 63 6f 64 65 20 74 6f 20 66 69 6e 69 73 68 20   code to finish 
2516e 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50  the INSERT or UP
2516f 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 0a 2a  DATE operation.*
25170 2a 20 74 68 61 74 20 77 61 73 20 73 74 61 72 74  * that was start
25171 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63 61  ed by a prior ca
25172 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e  ll to sqlite3Gen
25173 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
25174 68 65 63 6b 73 2e 0a 2a 2a 20 41 20 63 6f 6e 73  hecks..** A cons
25175 65 63 75 74 69 76 65 20 72 61 6e 67 65 20 6f 66  ecutive range of
25176 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74   registers start
25177 69 6e 67 20 61 74 20 72 65 67 52 6f 77 69 64 20  ing at regRowid 
25178 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20  contains the.** 
25179 72 6f 77 69 64 20 61 6e 64 20 74 68 65 20 63 6f  rowid and the co
2517a 6e 74 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65  ntent to be inse
2517b 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rted..**.** The 
2517c 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69  arguments to thi
2517d 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64  s routine should
2517e 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20   be the same as 
2517f 74 68 65 20 66 69 72 73 74 20 73 69 78 0a 2a 2a  the first six.**
25180 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 73 71   arguments to sq
25181 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e  lite3GenerateCon
25182 73 74 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a  straintChecks..*
25183 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
25184 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
25185 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a  pleteInsertion(.
25186 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
25187 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
25188 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
25189 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2518a 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c       /* the tabl
2518b 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20  e into which we 
2518c 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f  are inserting */
2518d 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20  .  int baseCur, 
2518e 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2518f 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  of a read/write 
25190 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
25191 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74  at pTab */.  int
25192 20 72 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20   regRowid,      
25193 20 2f 2a 20 52 61 6e 67 65 20 6f 66 20 63 6f 6e   /* Range of con
25194 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  tent */.  int *a
25195 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a  RegIdx,       /*
25196 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20 62   Register used b
25197 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30  y each index.  0
25198 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69   for unused indi
25199 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55  ces */.  int isU
2519a 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20  pdate,       /* 
2519b 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c  True for UPDATE,
2519c 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52   False for INSER
2519d 54 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64  T */.  int newId
2519e 78 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  x,         /* In
2519f 64 65 78 20 6f 66 20 4e 45 57 20 74 61 62 6c 65  dex of NEW table
251a0 20 66 6f 72 20 74 72 69 67 67 65 72 73 2e 20 20   for triggers.  
251a1 2d 31 20 69 66 20 6e 6f 6e 65 20 2a 2f 0a 20 20  -1 if none */.  
251a2 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c 20  int appendBias, 
251a3 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
251a4 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f  his is likely to
251a5 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f   be an append */
251a6 0a 20 20 69 6e 74 20 75 73 65 53 65 65 6b 52 65  .  int useSeekRe
251a7 73 75 6c 74 20 20 20 2f 2a 20 54 72 75 65 20 74  sult   /* True t
251a8 6f 20 73 65 74 20 74 68 65 20 55 53 45 53 45 45  o set the USESEE
251a9 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 6f 6e 20  KRESULT flag on 
251aa 4f 50 5f 5b 49 64 78 5d 49 6e 73 65 72 74 20 2a  OP_[Idx]Insert *
251ab 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
251ac 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6e  Vdbe *v;.  int n
251ad 49 64 78 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49  Idx;.  Index *pI
251ae 64 78 3b 0a 20 20 75 38 20 70 69 6b 5f 66 6c 61  dx;.  u8 pik_fla
251af 67 73 3b 0a 20 20 69 6e 74 20 72 65 67 44 61 74  gs;.  int regDat
251b0 61 3b 0a 20 20 69 6e 74 20 72 65 67 52 65 63 3b  a;.  int regRec;
251b1 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
251b2 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
251b3 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
251b4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
251b5 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20  ->pSelect==0 ); 
251b6 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69   /* This table i
251b7 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a  s not a VIEW */.
251b8 20 20 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49    for(nIdx=0, pI
251b9 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
251ba 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
251bb 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29  ->pNext, nIdx++)
251bc 7b 7d 0a 20 20 66 6f 72 28 69 3d 6e 49 64 78 2d  {}.  for(i=nIdx-
251bd 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20  1; i>=0; i--){. 
251be 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b 69     if( aRegIdx[i
251bf 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ]==0 ) continue;
251c0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
251c1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
251c2 49 6e 73 65 72 74 2c 20 62 61 73 65 43 75 72 2b  Insert, baseCur+
251c3 69 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 5d 29  i+1, aRegIdx[i])
251c4 3b 0a 20 20 20 20 69 66 28 20 75 73 65 53 65 65  ;.    if( useSee
251c5 6b 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20 20  kResult ){.     
251c6 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
251c7 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 55  geP5(v, OPFLAG_U
251c8 53 45 53 45 45 4b 52 45 53 55 4c 54 29 3b 0a 20  SESEEKRESULT);. 
251c9 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 67 44 61     }.  }.  regDa
251ca 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20  ta = regRowid + 
251cb 31 3b 0a 20 20 72 65 67 52 65 63 20 3d 20 73 71  1;.  regRec = sq
251cc 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
251cd 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
251ce 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
251cf 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
251d0 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43  egData, pTab->nC
251d1 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 73  ol, regRec);.  s
251d2 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e  qlite3TableAffin
251d3 69 74 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b  ityStr(v, pTab);
251d4 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  .  sqlite3ExprCa
251d5 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
251d6 65 28 70 50 61 72 73 65 2c 20 72 65 67 44 61 74  e(pParse, regDat
251d7 61 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a  a, pTab->nCol);.
251d8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
251d9 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69 66  MIT_TRIGGER.  if
251da 28 20 6e 65 77 49 64 78 3e 3d 30 20 29 7b 0a 20  ( newIdx>=0 ){. 
251db 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
251dc 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
251dd 74 2c 20 6e 65 77 49 64 78 2c 20 72 65 67 52 65  t, newIdx, regRe
251de 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  c, regRowid);.  
251df 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  }.#endif.  if( p
251e0 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b  Parse->nested ){
251e1 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 3d  .    pik_flags =
251e2 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
251e3 20 70 69 6b 5f 66 6c 61 67 73 20 3d 20 4f 50 46   pik_flags = OPF
251e4 4c 41 47 5f 4e 43 48 41 4e 47 45 3b 0a 20 20 20  LAG_NCHANGE;.   
251e5 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 28 69   pik_flags |= (i
251e6 73 55 70 64 61 74 65 3f 4f 50 46 4c 41 47 5f 49  sUpdate?OPFLAG_I
251e7 53 55 50 44 41 54 45 3a 4f 50 46 4c 41 47 5f 4c  SUPDATE:OPFLAG_L
251e8 41 53 54 52 4f 57 49 44 29 3b 0a 20 20 7d 0a 20  ASTROWID);.  }. 
251e9 20 69 66 28 20 61 70 70 65 6e 64 42 69 61 73 20   if( appendBias 
251ea 29 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73  ){.    pik_flags
251eb 20 7c 3d 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e   |= OPFLAG_APPEN
251ec 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 73 65  D;.  }.  if( use
251ed 53 65 65 6b 52 65 73 75 6c 74 20 29 7b 0a 20 20  SeekResult ){.  
251ee 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f    pik_flags |= O
251ef 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53  PFLAG_USESEEKRES
251f0 55 4c 54 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ULT;.  }.  sqlit
251f1 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
251f2 4f 50 5f 49 6e 73 65 72 74 2c 20 62 61 73 65 43  OP_Insert, baseC
251f3 75 72 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52  ur, regRec, regR
251f4 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 21 70 50  owid);.  if( !pP
251f5 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a  arse->nested ){.
251f6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
251f7 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70  hangeP4(v, -1, p
251f8 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53  Tab->zName, P4_S
251f9 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71  TATIC);.  }.  sq
251fa 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
251fb 35 28 76 2c 20 70 69 6b 5f 66 6c 61 67 73 29 3b  5(v, pik_flags);
251fc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
251fd 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
251fe 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 66  l open cursors f
251ff 6f 72 20 61 20 74 61 62 6c 65 20 61 6e 64 20 66  or a table and f
25200 6f 72 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65  or all.** indice
25201 73 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e  s of that table.
25202 20 20 54 68 65 20 22 62 61 73 65 43 75 72 22 20    The "baseCur" 
25203 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
25204 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75   cursor number u
25205 73 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74  sed.** for the t
25206 61 62 6c 65 2e 20 20 49 6e 64 69 63 65 73 20 61  able.  Indices a
25207 72 65 20 6f 70 65 6e 65 64 20 6f 6e 20 73 75 62  re opened on sub
25208 73 65 71 75 65 6e 74 20 63 75 72 73 6f 72 73 2e  sequent cursors.
25209 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
2520a 65 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 64 69  e number of indi
2520b 63 65 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ces on the table
2520c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2520d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
2520e 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63  penTableAndIndic
2520f 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  es(.  Parse *pPa
25210 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e  rse,   /* Parsin
25211 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
25212 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
25213 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 6f  /* Table to be o
25214 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 62  pened */.  int b
25215 61 73 65 43 75 72 2c 20 20 20 20 20 2f 2a 20 43  aseCur,     /* C
25216 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 61 73 73  ursor number ass
25217 69 67 6e 65 64 20 74 6f 20 74 68 65 20 74 61 62  igned to the tab
25218 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 20 20  le */.  int op  
25219 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f           /* OP_O
2521a 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70  penRead or OP_Op
2521b 65 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20  enWrite */.){.  
2521c 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 44 62  int i;.  int iDb
2521d 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  ;.  Index *pIdx;
2521e 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69  .  Vdbe *v;..  i
2521f 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
25220 62 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  b) ) return 0;. 
25221 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
25222 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
25223 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
25224 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73 71  chema);.  v = sq
25225 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
25226 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rse);.  assert( 
25227 76 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65  v!=0 );.  sqlite
25228 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
25229 65 2c 20 62 61 73 65 43 75 72 2c 20 69 44 62 2c  e, baseCur, iDb,
2522a 20 70 54 61 62 2c 20 6f 70 29 3b 0a 20 20 66 6f   pTab, op);.  fo
2522b 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62  r(i=1, pIdx=pTab
2522c 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2522d 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2522e 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 4b 65 79 49  , i++){.    KeyI
2522f 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69  nfo *pKey = sqli
25230 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
25231 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20  pParse, pIdx);. 
25232 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
25233 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  >pSchema==pTab->
25234 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73  pSchema );.    s
25235 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
25236 28 76 2c 20 6f 70 2c 20 69 2b 62 61 73 65 43 75  (v, op, i+baseCu
25237 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69  r, pIdx->tnum, i
25238 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Db,.            
25239 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
2523a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46  )pKey, P4_KEYINF
2523b 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
2523c 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2523d 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  "%s", pIdx->zNam
2523e 65 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  e));.  }.  if( p
2523f 50 61 72 73 65 2d 3e 6e 54 61 62 3c 62 61 73 65  Parse->nTab<base
25240 43 75 72 2b 69 20 29 7b 0a 20 20 20 20 70 50 61  Cur+i ){.    pPa
25241 72 73 65 2d 3e 6e 54 61 62 20 3d 20 62 61 73 65  rse->nTab = base
25242 43 75 72 2b 69 3b 0a 20 20 7d 0a 20 20 72 65 74  Cur+i;.  }.  ret
25243 75 72 6e 20 69 2d 31 3b 0a 7d 0a 0a 0a 23 69 66  urn i-1;.}...#if
25244 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
25245 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
25246 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ing global varia
25247 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ble is increment
25248 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a  ed whenever the.
25249 2a 2a 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69  ** transfer opti
2524a 6d 69 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64  mization is used
2524b 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
2524c 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70  for testing.** p
2524d 75 72 70 6f 73 65 73 20 6f 6e 6c 79 20 2d 20 74  urposes only - t
2524e 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
2524f 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a  transfer optimiz
25250 61 74 69 6f 6e 20 72 65 61 6c 6c 79 0a 2a 2a 20  ation really.** 
25251 69 73 20 68 61 70 70 65 6e 69 6e 67 20 77 68 65  is happening whe
25252 6e 20 69 74 20 69 73 20 73 75 70 70 6f 73 65 20  n it is suppose 
25253 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  to..*/.SQLITE_AP
25254 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 78 66  I int sqlite3_xf
25255 65 72 6f 70 74 5f 63 6f 75 6e 74 3b 0a 23 65 6e  eropt_count;.#en
25256 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45  dif /* SQLITE_TE
25257 53 54 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20  ST */...#ifndef 
25258 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52  SQLITE_OMIT_XFER
25259 5f 4f 50 54 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  _OPT./*.** Check
2525a 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61   to collation na
2525b 6d 65 73 20 74 6f 20 73 65 65 20 69 66 20 74 68  mes to see if th
2525c 65 79 20 61 72 65 20 63 6f 6d 70 61 74 69 62 6c  ey are compatibl
2525d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2525e 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43   xferCompatibleC
2525f 6f 6c 6c 61 74 69 6f 6e 28 63 6f 6e 73 74 20 63  ollation(const c
25260 68 61 72 20 2a 7a 31 2c 20 63 6f 6e 73 74 20 63  har *z1, const c
25261 68 61 72 20 2a 7a 32 29 7b 0a 20 20 69 66 28 20  har *z2){.  if( 
25262 7a 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  z1==0 ){.    ret
25263 75 72 6e 20 7a 32 3d 3d 30 3b 0a 20 20 7d 0a 20  urn z2==0;.  }. 
25264 20 69 66 28 20 7a 32 3d 3d 30 20 29 7b 0a 20 20   if( z2==0 ){.  
25265 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
25266 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
25267 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 3d  StrICmp(z1, z2)=
25268 3d 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68  =0;.}.../*.** Ch
25269 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69 6e  eck to see if in
2526a 64 65 78 20 70 53 72 63 20 69 73 20 63 6f 6d 70  dex pSrc is comp
2526b 61 74 69 62 6c 65 20 61 73 20 61 20 73 6f 75 72  atible as a sour
2526c 63 65 20 6f 66 20 64 61 74 61 0a 2a 2a 20 66 6f  ce of data.** fo
2526d 72 20 69 6e 64 65 78 20 70 44 65 73 74 20 69 6e  r index pDest in
2526e 20 61 6e 20 69 6e 73 65 72 74 20 74 72 61 6e 73   an insert trans
2526f 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  fer optimization
25270 2e 20 20 54 68 65 20 72 75 6c 65 73 0a 2a 2a 20  .  The rules.** 
25271 66 6f 72 20 61 20 63 6f 6d 70 61 74 69 62 6c 65  for a compatible
25272 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20   index:.**.**   
25273 20 2a 20 20 20 54 68 65 20 69 6e 64 65 78 20 69   *   The index i
25274 73 20 6f 76 65 72 20 74 68 65 20 73 61 6d 65 20  s over the same 
25275 73 65 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a  set of columns.*
25276 2a 20 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d  *    *   The sam
25277 65 20 44 45 53 43 20 61 6e 64 20 41 53 43 20 6d  e DESC and ASC m
25278 61 72 6b 69 6e 67 73 20 6f 63 63 75 72 73 20 6f  arkings occurs o
25279 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  n all columns.**
2527a 20 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65      *   The same
2527b 20 6f 6e 45 72 72 6f 72 20 70 72 6f 63 65 73 73   onError process
2527c 69 6e 67 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f  ing (OE_Abort, O
2527d 45 5f 49 67 6e 6f 72 65 2c 20 65 74 63 29 0a 2a  E_Ignore, etc).*
2527e 2a 20 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d  *    *   The sam
2527f 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
25280 65 6e 63 65 20 6f 6e 20 65 61 63 68 20 63 6f 6c  ence on each col
25281 75 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  umn.*/.static in
25282 74 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65  t xferCompatible
25283 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 44 65  Index(Index *pDe
25284 73 74 2c 20 49 6e 64 65 78 20 2a 70 53 72 63 29  st, Index *pSrc)
25285 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  {.  int i;.  ass
25286 65 72 74 28 20 70 44 65 73 74 20 26 26 20 70 53  ert( pDest && pS
25287 72 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rc );.  assert( 
25288 70 44 65 73 74 2d 3e 70 54 61 62 6c 65 21 3d 70  pDest->pTable!=p
25289 53 72 63 2d 3e 70 54 61 62 6c 65 20 29 3b 0a 20  Src->pTable );. 
2528a 20 69 66 28 20 70 44 65 73 74 2d 3e 6e 43 6f 6c   if( pDest->nCol
2528b 75 6d 6e 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75  umn!=pSrc->nColu
2528c 6d 6e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mn ){.    return
2528d 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65   0;   /* Differe
2528e 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  nt number of col
2528f 75 6d 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  umns */.  }.  if
25290 28 20 70 44 65 73 74 2d 3e 6f 6e 45 72 72 6f 72  ( pDest->onError
25291 21 3d 70 53 72 63 2d 3e 6f 6e 45 72 72 6f 72 20  !=pSrc->onError 
25292 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
25293 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20     /* Different 
25294 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74  conflict resolut
25295 69 6f 6e 20 73 74 72 61 74 65 67 69 65 73 20 2a  ion strategies *
25296 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  /.  }.  for(i=0;
25297 20 69 3c 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e   i<pSrc->nColumn
25298 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
25299 70 53 72 63 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  pSrc->aiColumn[i
2529a 5d 21 3d 70 44 65 73 74 2d 3e 61 69 43 6f 6c 75  ]!=pDest->aiColu
2529b 6d 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 72  mn[i] ){.      r
2529c 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69  eturn 0;   /* Di
2529d 66 66 65 72 65 6e 74 20 63 6f 6c 75 6d 6e 73 20  fferent columns 
2529e 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 20 20 7d  indexed */.    }
2529f 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61  .    if( pSrc->a
252a0 53 6f 72 74 4f 72 64 65 72 5b 69 5d 21 3d 70 44  SortOrder[i]!=pD
252a1 65 73 74 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  est->aSortOrder[
252a2 69 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  i] ){.      retu
252a3 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65  rn 0;   /* Diffe
252a4 72 65 6e 74 20 73 6f 72 74 20 6f 72 64 65 72 73  rent sort orders
252a5 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
252a6 28 20 21 78 66 65 72 43 6f 6d 70 61 74 69 62 6c  ( !xferCompatibl
252a7 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 53 72 63 2d  eCollation(pSrc-
252a8 3e 61 7a 43 6f 6c 6c 5b 69 5d 2c 70 44 65 73 74  >azColl[i],pDest
252a9 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 29 20 29 7b 0a  ->azColl[i]) ){.
252aa 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
252ab 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63    /* Different c
252ac 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
252ad 65 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a  es */.    }.  }.
252ae 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 74 65 73 74  .  /* If no test
252af 20 61 62 6f 76 65 20 66 61 69 6c 73 20 74 68 65   above fails the
252b0 6e 20 74 68 65 20 69 6e 64 69 63 65 73 20 6d 75  n the indices mu
252b1 73 74 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65  st be compatible
252b2 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a   */.  return 1;.
252b3 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  }../*.** Attempt
252b4 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70   the transfer op
252b5 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e  timization on IN
252b6 53 45 52 54 73 20 6f 66 20 74 68 65 20 66 6f 72  SERTs of the for
252b7 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45  m.**.**     INSE
252b8 52 54 20 49 4e 54 4f 20 74 61 62 31 20 53 45 4c  RT INTO tab1 SEL
252b9 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 32 3b  ECT * FROM tab2;
252ba 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69  .**.** This opti
252bb 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  mization is only
252bc 20 61 74 74 65 6d 70 74 65 64 20 69 66 0a 2a 2a   attempted if.**
252bd 0a 2a 2a 20 20 20 20 28 31 29 20 20 74 61 62 31  .**    (1)  tab1
252be 20 61 6e 64 20 74 61 62 32 20 68 61 76 65 20 69   and tab2 have i
252bf 64 65 6e 74 69 63 61 6c 20 73 63 68 65 6d 61 73  dentical schemas
252c0 20 69 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74   including all t
252c1 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 61  he.**         sa
252c2 6d 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20 63  me indices and c
252c3 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 0a 2a 2a  onstraints.**.**
252c4 20 20 20 20 28 32 29 20 20 74 61 62 31 20 61 6e      (2)  tab1 an
252c5 64 20 74 61 62 32 20 61 72 65 20 64 69 66 66 65  d tab2 are diffe
252c6 72 65 6e 74 20 74 61 62 6c 65 73 0a 2a 2a 0a 2a  rent tables.**.*
252c7 2a 20 20 20 20 28 33 29 20 20 54 68 65 72 65 20  *    (3)  There 
252c8 6d 75 73 74 20 62 65 20 6e 6f 20 74 72 69 67 67  must be no trigg
252c9 65 72 73 20 6f 6e 20 74 61 62 31 0a 2a 2a 0a 2a  ers on tab1.**.*
252ca 2a 20 20 20 20 28 34 29 20 20 54 68 65 20 72 65  *    (4)  The re
252cb 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20  sult set of the 
252cc 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
252cd 20 69 73 20 22 2a 22 0a 2a 2a 0a 2a 2a 20 20 20   is "*".**.**   
252ce 20 28 35 29 20 20 54 68 65 20 53 45 4c 45 43 54   (5)  The SELECT
252cf 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 6e   statement has n
252d0 6f 20 57 48 45 52 45 2c 20 48 41 56 49 4e 47 2c  o WHERE, HAVING,
252d1 20 4f 52 44 45 52 20 42 59 2c 20 47 52 4f 55 50   ORDER BY, GROUP
252d2 20 42 59 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20   BY,.**         
252d3 6f 72 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e  or LIMIT clause.
252d4 0a 2a 2a 0a 2a 2a 20 20 20 20 28 36 29 20 20 54  .**.**    (6)  T
252d5 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
252d6 65 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 65 20  ent is a simple 
252d7 28 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 29  (not a compound)
252d8 20 73 65 6c 65 63 74 20 74 68 61 74 0a 2a 2a 20   select that.** 
252d9 20 20 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73          contains
252da 20 6f 6e 6c 79 20 74 61 62 32 20 69 6e 20 69 74   only tab2 in it
252db 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a  s FROM clause.**
252dc 0a 2a 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20  .** This method 
252dd 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  for implementing
252de 20 74 68 65 20 49 4e 53 45 52 54 20 74 72 61 6e   the INSERT tran
252df 73 66 65 72 73 20 72 61 77 20 72 65 63 6f 72 64  sfers raw record
252e0 73 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 32 20 6f  s from.** tab2 o
252e1 76 65 72 20 74 6f 20 74 61 62 31 2e 20 20 54 68  ver to tab1.  Th
252e2 65 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f  e columns are no
252e3 74 20 64 65 63 6f 64 65 64 2e 20 20 52 61 77 20  t decoded.  Raw 
252e4 72 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20  records from.** 
252e5 74 68 65 20 69 6e 64 69 63 65 73 20 6f 66 20 74  the indices of t
252e6 61 62 32 20 61 72 65 20 74 72 61 6e 73 66 65 72  ab2 are transfer
252e7 65 64 20 74 6f 20 74 61 62 31 20 61 73 20 77 65  ed to tab1 as we
252e8 6c 6c 2e 20 20 49 6e 20 73 6f 20 64 6f 69 6e 67  ll.  In so doing
252e9 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69  ,.** the resulti
252ea 6e 67 20 74 61 62 31 20 68 61 73 20 6d 75 63 68  ng tab1 has much
252eb 20 6c 65 73 73 20 66 72 61 67 6d 65 6e 74 61 74   less fragmentat
252ec 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
252ed 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
252ee 54 52 55 45 20 69 66 20 74 68 65 20 6f 70 74 69  TRUE if the opti
252ef 6d 69 7a 61 74 69 6f 6e 20 69 73 20 61 74 74 65  mization is atte
252f0 6d 70 74 65 64 2e 20 20 49 66 20 61 6e 79 0a 2a  mpted.  If any.*
252f1 2a 20 6f 66 20 74 68 65 20 63 6f 6e 64 69 74 69  * of the conditi
252f2 6f 6e 73 20 61 62 6f 76 65 20 66 61 69 6c 20 73  ons above fail s
252f3 6f 20 74 68 61 74 20 74 68 65 20 6f 70 74 69 6d  o that the optim
252f4 69 7a 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e  ization should n
252f5 6f 74 0a 2a 2a 20 62 65 20 61 74 74 65 6d 70 74  ot.** be attempt
252f6 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  ed, then this ro
252f7 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 46 41  utine returns FA
252f8 4c 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  LSE..*/.static i
252f9 6e 74 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74  nt xferOptimizat
252fa 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
252fb 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
252fc 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
252fd 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74  /.  Table *pDest
252fe 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
252ff 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e   table we are in
25300 73 65 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a  serting into */.
25301 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
25302 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c  t,      /* A SEL
25303 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
25304 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61   use as the data
25305 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74   source */.  int
25306 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20   onError,       
25307 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e     /* How to han
25308 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  dle constraint e
25309 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69  rrors */.  int i
2530a 44 62 44 65 73 74 20 20 20 20 20 20 20 20 20 20  DbDest          
2530b 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2530c 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a 29 7b 0a   of pDest */.){.
2530d 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2530e 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
2530f 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74     /* The result
25310 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45   set of the SELE
25311 43 54 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  CT */.  Table *p
25312 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Src;            
25313 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
25314 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f  table in the FRO
25315 4d 20 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45  M clause of SELE
25316 43 54 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70  CT */.  Index *p
25317 53 72 63 49 64 78 2c 20 2a 70 44 65 73 74 49 64  SrcIdx, *pDestId
25318 78 3b 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72  x;       /* Sour
25319 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69  ce and destinati
2531a 6f 6e 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  on indices */.  
2531b 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2531c 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20  tem *pItem;     
2531d 20 2f 2a 20 41 6e 20 65 6c 65 6d 65 6e 74 20 6f   /* An element o
2531e 66 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 20  f pSelect->pSrc 
2531f 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
25320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25321 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
25322 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69  unter */.  int i
25323 44 62 53 72 63 3b 20 20 20 20 20 20 20 20 20 20  DbSrc;          
25324 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
25325 68 65 20 64 61 74 61 62 61 73 65 20 6f 66 20 70  he database of p
25326 53 72 63 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72  Src */.  int iSr
25327 63 2c 20 69 44 65 73 74 3b 20 20 20 20 20 20 20  c, iDest;       
25328 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
25329 73 6f 72 73 20 66 72 6f 6d 20 73 6f 75 72 63 65  sors from source
2532a 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e   and destination
2532b 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 31 2c   */.  int addr1,
2532c 20 61 64 64 72 32 3b 20 20 20 20 20 20 20 20 20   addr2;         
2532d 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 61         /* Loop a
2532e 64 64 72 65 73 73 65 73 20 2a 2f 0a 20 20 69 6e  ddresses */.  in
2532f 74 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 3b  t emptyDestTest;
25330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25331 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 65 73  * Address of tes
25332 74 20 66 6f 72 20 65 6d 70 74 79 20 70 44 65 73  t for empty pDes
25333 74 20 2a 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79  t */.  int empty
25334 53 72 63 54 65 73 74 3b 20 20 20 20 20 20 20 20  SrcTest;        
25335 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
25336 73 73 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65  ss of test for e
25337 6d 70 74 79 20 70 53 72 63 20 2a 2f 0a 20 20 56  mpty pSrc */.  V
25338 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
25339 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2533a 2f 2a 20 54 68 65 20 56 44 42 45 20 77 65 20 61  /* The VDBE we a
2533b 72 65 20 62 75 69 6c 64 69 6e 67 20 2a 2f 0a 20  re building */. 
2533c 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20   KeyInfo *pKey; 
2533d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2533e 20 20 2f 2a 20 4b 65 79 20 69 6e 66 6f 72 6d 61    /* Key informa
2533f 74 69 6f 6e 20 66 6f 72 20 61 6e 20 69 6e 64 65  tion for an inde
25340 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 75  x */.  int regAu
25341 74 6f 69 6e 63 3b 20 20 20 20 20 20 20 20 20 20  toinc;          
25342 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
25343 79 20 72 65 67 69 73 74 65 72 20 75 73 65 64 20  y register used 
25344 62 79 20 41 55 54 4f 49 4e 43 20 2a 2f 0a 20 20  by AUTOINC */.  
25345 69 6e 74 20 64 65 73 74 48 61 73 55 6e 69 71 75  int destHasUniqu
25346 65 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20  eIdx = 0;       
25347 20 2f 2a 20 54 72 75 65 20 69 66 20 70 44 65 73   /* True if pDes
25348 74 20 68 61 73 20 61 20 55 4e 49 51 55 45 20 69  t has a UNIQUE i
25349 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ndex */.  int re
2534a 67 44 61 74 61 2c 20 72 65 67 52 6f 77 69 64 3b  gData, regRowid;
2534b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2534c 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20  gisters holding 
2534d 64 61 74 61 20 61 6e 64 20 72 6f 77 69 64 20 2a  data and rowid *
2534e 2f 0a 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  /..  if( pSelect
2534f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
25350 6e 20 30 3b 20 20 20 2f 2a 20 4d 75 73 74 20 62  n 0;   /* Must b
25351 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 49  e of the form  I
25352 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53  NSERT INTO ... S
25353 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d  ELECT ... */.  }
25354 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 54 72  .  if( sqlite3Tr
25355 69 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65  iggerList(pParse
25356 2c 20 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20  , pDest) ){.    
25357 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74  return 0;   /* t
25358 61 62 31 20 6d 75 73 74 20 6e 6f 74 20 68 61 76  ab1 must not hav
25359 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20  e triggers */.  
2535a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2535b 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2535c 4c 45 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e  LE.  if( pDest->
2535d 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
2535e 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74  rtual ){.    ret
2535f 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31  urn 0;   /* tab1
25360 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76   must not be a v
25361 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
25362 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
25363 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66   onError==OE_Def
25364 61 75 6c 74 20 29 7b 0a 20 20 20 20 6f 6e 45 72  ault ){.    onEr
25365 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ror = OE_Abort;.
25366 20 20 7d 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f    }.  if( onErro
25367 72 21 3d 4f 45 5f 41 62 6f 72 74 20 26 26 20 6f  r!=OE_Abort && o
25368 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62  nError!=OE_Rollb
25369 61 63 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ack ){.    retur
2536a 6e 20 30 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74  n 0;   /* Cannot
2536b 20 64 6f 20 4f 52 20 52 45 50 4c 41 43 45 20 6f   do OR REPLACE o
2536c 72 20 4f 52 20 49 47 4e 4f 52 45 20 6f 72 20 4f  r OR IGNORE or O
2536d 52 20 46 41 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20  R FAIL */.  }.  
2536e 61 73 73 65 72 74 28 70 53 65 6c 65 63 74 2d 3e  assert(pSelect->
2536f 70 53 72 63 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f  pSrc);   /* allo
25370 63 61 74 65 64 20 65 76 65 6e 20 69 66 20 74 68  cated even if th
25371 65 72 65 20 69 73 20 6e 6f 20 46 52 4f 4d 20 63  ere is no FROM c
25372 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70  lause */.  if( p
25373 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 6e 53  Select->pSrc->nS
25374 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 72 65 74  rc!=1 ){.    ret
25375 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d  urn 0;   /* FROM
25376 20 63 6c 61 75 73 65 20 6d 75 73 74 20 68 61 76   clause must hav
25377 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 74 65  e exactly one te
25378 72 6d 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  rm */.  }.  if( 
25379 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 61  pSelect->pSrc->a
2537a 5b 30 5d 2e 70 53 65 6c 65 63 74 20 29 7b 0a 20  [0].pSelect ){. 
2537b 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
2537c 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 63 61  * FROM clause ca
2537d 6e 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73  nnot contain a s
2537e 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20  ubquery */.  }. 
2537f 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 57   if( pSelect->pW
25380 68 65 72 65 20 29 7b 0a 20 20 20 20 72 65 74 75  here ){.    retu
25381 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
25382 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61  T may not have a
25383 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
25384 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65  .  }.  if( pSele
25385 63 74 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  ct->pOrderBy ){.
25386 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
25387 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f  /* SELECT may no
25388 74 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20  t have an ORDER 
25389 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d  BY clause */.  }
2538a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65 65  .  /* Do not nee
2538b 64 20 74 6f 20 74 65 73 74 20 66 6f 72 20 61 20  d to test for a 
2538c 48 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20 20  HAVING clause.  
2538d 49 66 20 48 41 56 49 4e 47 20 69 73 20 70 72 65  If HAVING is pre
2538e 73 65 6e 74 20 62 75 74 0a 20 20 2a 2a 20 74 68  sent but.  ** th
2538f 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20  ere is no ORDER 
25390 42 59 2c 20 77 65 20 77 69 6c 6c 20 67 65 74 20  BY, we will get 
25391 61 6e 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69  an error. */.  i
25392 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 47 72 6f  f( pSelect->pGro
25393 75 70 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75  upBy ){.    retu
25394 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
25395 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61  T may not have a
25396 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
25397 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53   */.  }.  if( pS
25398 65 6c 65 63 74 2d 3e 70 4c 69 6d 69 74 20 29 7b  elect->pLimit ){
25399 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
2539a 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e   /* SELECT may n
2539b 6f 74 20 68 61 76 65 20 61 20 4c 49 4d 49 54 20  ot have a LIMIT 
2539c 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20  clause */.  }.  
2539d 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d  assert( pSelect-
2539e 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20  >pOffset==0 );  
2539f 2f 2a 20 4d 75 73 74 20 62 65 20 73 6f 20 69 66  /* Must be so if
253a0 20 70 4c 69 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20   pLimit==0 */.  
253a1 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72  if( pSelect->pPr
253a2 69 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ior ){.    retur
253a3 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54  n 0;   /* SELECT
253a4 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 63 6f   may not be a co
253a5 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 2a 2f 0a  mpound query */.
253a6 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63    }.  if( pSelec
253a7 74 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  t->selFlags & SF
253a8 5f 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20  _Distinct ){.   
253a9 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
253aa 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62  SELECT may not b
253ab 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20  e DISTINCT */.  
253ac 7d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65  }.  pEList = pSe
253ad 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20  lect->pEList;.  
253ae 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d  assert( pEList!=
253af 30 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  0 );.  if( pELis
253b0 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20  t->nExpr!=1 ){. 
253b1 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
253b2 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74  * The result set
253b3 20 6d 75 73 74 20 68 61 76 65 20 65 78 61 63 74   must have exact
253b4 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f  ly one column */
253b5 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
253b6 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
253b7 72 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  r );.  if( pELis
253b8 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f  t->a[0].pExpr->o
253b9 70 21 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20  p!=TK_ALL ){.   
253ba 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
253bb 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d  The result set m
253bc 75 73 74 20 62 65 20 74 68 65 20 73 70 65 63 69  ust be the speci
253bd 61 6c 20 6f 70 65 72 61 74 6f 72 20 22 2a 22 20  al operator "*" 
253be 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20  */.  }..  /* At 
253bf 74 68 69 73 20 70 6f 69 6e 74 20 77 65 20 68 61  this point we ha
253c0 76 65 20 65 73 74 61 62 6c 69 73 68 65 64 20 74  ve established t
253c1 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e  hat the statemen
253c2 74 20 69 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a  t is of the.  **
253c3 20 63 6f 72 72 65 63 74 20 73 79 6e 74 61 63 74   correct syntact
253c4 69 63 20 66 6f 72 6d 20 74 6f 20 70 61 72 74 69  ic form to parti
253c5 63 69 70 61 74 65 20 69 6e 20 74 68 69 73 20 6f  cipate in this o
253c6 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f  ptimization.  No
253c7 77 0a 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74  w.  ** we have t
253c8 6f 20 63 68 65 63 6b 20 74 68 65 20 73 65 6d 61  o check the sema
253c9 6e 74 69 63 73 2e 0a 20 20 2a 2f 0a 20 20 70 49  ntics..  */.  pI
253ca 74 65 6d 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  tem = pSelect->p
253cb 53 72 63 2d 3e 61 3b 0a 20 20 70 53 72 63 20 3d  Src->a;.  pSrc =
253cc 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
253cd 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70  ble(pParse, 0, p
253ce 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74  Item->zName, pIt
253cf 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  em->zDatabase);.
253d0 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 7b    if( pSrc==0 ){
253d1 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
253d2 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   /* FROM clause 
253d3 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e  does not contain
253d4 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f   a real table */
253d5 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 72 63 3d  .  }.  if( pSrc=
253d6 3d 70 44 65 73 74 20 29 7b 0a 20 20 20 20 72 65  =pDest ){.    re
253d7 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62  turn 0;   /* tab
253d8 31 20 61 6e 64 20 74 61 62 32 20 6d 61 79 20 6e  1 and tab2 may n
253d9 6f 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 74  ot be the same t
253da 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e  able */.  }.#ifn
253db 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
253dc 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69  VIRTUALTABLE.  i
253dd 66 28 20 70 53 72 63 2d 3e 74 61 62 46 6c 61 67  f( pSrc->tabFlag
253de 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 20 29  s & TF_Virtual )
253df 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
253e0 20 20 2f 2a 20 74 61 62 32 20 6d 75 73 74 20 6e    /* tab2 must n
253e1 6f 74 20 62 65 20 61 20 76 69 72 74 75 61 6c 20  ot be a virtual 
253e2 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e  table */.  }.#en
253e3 64 69 66 0a 20 20 69 66 28 20 70 53 72 63 2d 3e  dif.  if( pSrc->
253e4 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 72  pSelect ){.    r
253e5 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61  eturn 0;   /* ta
253e6 62 32 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 20  b2 may not be a 
253e7 76 69 65 77 20 2a 2f 0a 20 20 7d 0a 20 20 69 66  view */.  }.  if
253e8 28 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70  ( pDest->nCol!=p
253e9 53 72 63 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Src->nCol ){.   
253ea 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
253eb 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
253ec 73 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  s must be the sa
253ed 6d 65 20 69 6e 20 74 61 62 31 20 61 6e 64 20 74  me in tab1 and t
253ee 61 62 32 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ab2 */.  }.  if(
253ef 20 70 44 65 73 74 2d 3e 69 50 4b 65 79 21 3d 70   pDest->iPKey!=p
253f0 53 72 63 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Src->iPKey ){.  
253f1 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
253f2 20 42 6f 74 68 20 74 61 62 6c 65 73 20 6d 75 73   Both tables mus
253f3 74 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20  t have the same 
253f4 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
253f5 4b 45 59 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72  KEY */.  }.  for
253f6 28 69 3d 30 3b 20 69 3c 70 44 65 73 74 2d 3e 6e  (i=0; i<pDest->n
253f7 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  Col; i++){.    i
253f8 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69  f( pDest->aCol[i
253f9 5d 2e 61 66 66 69 6e 69 74 79 21 3d 70 53 72 63  ].affinity!=pSrc
253fa 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69  ->aCol[i].affini
253fb 74 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ty ){.      retu
253fc 72 6e 20 30 3b 20 20 20 20 2f 2a 20 41 66 66 69  rn 0;    /* Affi
253fd 6e 69 74 79 20 6d 75 73 74 20 62 65 20 74 68 65  nity must be the
253fe 20 73 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c   same on all col
253ff 75 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  umns */.    }.  
25400 20 20 69 66 28 20 21 78 66 65 72 43 6f 6d 70 61    if( !xferCompa
25401 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 70  tibleCollation(p
25402 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43  Dest->aCol[i].zC
25403 6f 6c 6c 2c 20 70 53 72 63 2d 3e 61 43 6f 6c 5b  oll, pSrc->aCol[
25404 69 5d 2e 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20  i].zColl) ){.   
25405 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
25406 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
25407 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20 74 68  uence must be th
25408 65 20 73 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f  e same on all co
25409 6c 75 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20  lumns */.    }. 
2540a 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 61 43     if( pDest->aC
2540b 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26  ol[i].notNull &&
2540c 20 21 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e   !pSrc->aCol[i].
2540d 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20  notNull ){.     
2540e 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a   return 0;    /*
2540f 20 74 61 62 32 20 6d 75 73 74 20 62 65 20 4e 4f   tab2 must be NO
25410 54 20 4e 55 4c 4c 20 69 66 20 74 61 62 31 20 69  T NULL if tab1 i
25411 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  s */.    }.  }. 
25412 20 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44   for(pDestIdx=pD
25413 65 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65  est->pIndex; pDe
25414 73 74 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d  stIdx; pDestIdx=
25415 70 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29  pDestIdx->pNext)
25416 7b 0a 20 20 20 20 69 66 28 20 70 44 65 73 74 49  {.    if( pDestI
25417 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
25418 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 64 65  None ){.      de
25419 73 74 48 61 73 55 6e 69 71 75 65 49 64 78 20 3d  stHasUniqueIdx =
2541a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f   1;.    }.    fo
2541b 72 28 70 53 72 63 49 64 78 3d 70 53 72 63 2d 3e  r(pSrcIdx=pSrc->
2541c 70 49 6e 64 65 78 3b 20 70 53 72 63 49 64 78 3b  pIndex; pSrcIdx;
2541d 20 70 53 72 63 49 64 78 3d 70 53 72 63 49 64 78   pSrcIdx=pSrcIdx
2541e 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
2541f 69 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69 62  if( xferCompatib
25420 6c 65 49 6e 64 65 78 28 70 44 65 73 74 49 64 78  leIndex(pDestIdx
25421 2c 20 70 53 72 63 49 64 78 29 20 29 20 62 72 65  , pSrcIdx) ) bre
25422 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
25423 28 20 70 53 72 63 49 64 78 3d 3d 30 20 29 7b 0a  ( pSrcIdx==0 ){.
25424 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
25425 20 20 20 2f 2a 20 70 44 65 73 74 49 64 78 20 68     /* pDestIdx h
25426 61 73 20 6e 6f 20 63 6f 72 72 65 73 70 6f 6e 64  as no correspond
25427 69 6e 67 20 69 6e 64 65 78 20 69 6e 20 70 53 72  ing index in pSr
25428 63 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  c */.    }.  }.#
25429 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2542a 49 54 5f 43 48 45 43 4b 0a 20 20 69 66 28 20 70  IT_CHECK.  if( p
2542b 44 65 73 74 2d 3e 70 43 68 65 63 6b 20 26 26 20  Dest->pCheck && 
2542c 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70  !sqlite3ExprComp
2542d 61 72 65 28 70 53 72 63 2d 3e 70 43 68 65 63 6b  are(pSrc->pCheck
2542e 2c 20 70 44 65 73 74 2d 3e 70 43 68 65 63 6b 29  , pDest->pCheck)
2542f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
25430 3b 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 68 61  ;   /* Tables ha
25431 76 65 20 64 69 66 66 65 72 65 6e 74 20 43 48 45  ve different CHE
25432 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  CK constraints. 
25433 20 54 69 63 6b 65 74 20 23 32 32 35 32 20 2a 2f   Ticket #2252 */
25434 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
25435 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69 73  * If we get this
25436 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 65   far, it means e
25437 69 74 68 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ither:.  **.  **
25438 20 20 20 20 2a 20 20 20 57 65 20 63 61 6e 20 61      *   We can a
25439 6c 77 61 79 73 20 64 6f 20 74 68 65 20 74 72 61  lways do the tra
2543a 6e 73 66 65 72 20 69 66 20 74 68 65 20 74 61 62  nsfer if the tab
2543b 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 20  le contains an. 
2543c 20 2a 2a 20 20 20 20 20 20 20 20 61 6e 20 69 6e   **        an in
2543d 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65  teger primary ke
2543e 79 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a  y.  **.  **    *
2543f 20 20 20 57 65 20 63 61 6e 20 63 6f 6e 64 69 74     We can condit
25440 69 6f 6e 61 6c 6c 79 20 64 6f 20 74 68 65 20 74  ionally do the t
25441 72 61 6e 73 66 65 72 20 69 66 20 74 68 65 20 64  ransfer if the d
25442 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20  estination.  ** 
25443 20 20 20 20 20 20 20 74 61 62 6c 65 20 69 73 20         table is 
25444 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 23 69 66 64  empty..  */.#ifd
25445 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
25446 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74   sqlite3_xferopt
25447 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
25448 0a 20 20 69 44 62 53 72 63 20 3d 20 73 71 6c 69  .  iDbSrc = sqli
25449 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2544a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72  (pParse->db, pSr
2544b 63 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 76  c->pSchema);.  v
2544c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2544d 65 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  e(pParse);.  sql
2544e 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
2544f 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62  hema(pParse, iDb
25450 53 72 63 29 3b 0a 20 20 69 53 72 63 20 3d 20 70  Src);.  iSrc = p
25451 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
25452 20 69 44 65 73 74 20 3d 20 70 50 61 72 73 65 2d   iDest = pParse-
25453 3e 6e 54 61 62 2b 2b 3b 0a 20 20 72 65 67 41 75  >nTab++;.  regAu
25454 74 6f 69 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42  toinc = autoIncB
25455 65 67 69 6e 28 70 50 61 72 73 65 2c 20 69 44 62  egin(pParse, iDb
25456 44 65 73 74 2c 20 70 44 65 73 74 29 3b 0a 20 20  Dest, pDest);.  
25457 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
25458 28 70 50 61 72 73 65 2c 20 69 44 65 73 74 2c 20  (pParse, iDest, 
25459 69 44 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20  iDbDest, pDest, 
2545a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20  OP_OpenWrite);. 
2545b 20 69 66 28 20 28 70 44 65 73 74 2d 3e 69 50 4b   if( (pDest->iPK
2545c 65 79 3c 30 20 26 26 20 70 44 65 73 74 2d 3e 70  ey<0 && pDest->p
2545d 49 6e 64 65 78 21 3d 30 29 20 7c 7c 20 64 65 73  Index!=0) || des
2545e 74 48 61 73 55 6e 69 71 75 65 49 64 78 20 29 7b  tHasUniqueIdx ){
2545f 0a 20 20 20 20 2f 2a 20 49 66 20 74 61 62 6c 65  .    /* If table
25460 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 6e  s do not have an
25461 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
25462 20 4b 45 59 20 61 6e 64 20 74 68 65 72 65 0a 20   KEY and there. 
25463 20 20 20 2a 2a 20 61 72 65 20 69 6e 64 69 63 65     ** are indice
25464 73 20 74 6f 20 62 65 20 63 6f 70 69 65 64 20 61  s to be copied a
25465 6e 64 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  nd the destinati
25466 6f 6e 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c  on is not empty,
25467 0a 20 20 20 20 2a 2a 20 77 65 20 68 61 76 65 20  .    ** we have 
25468 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20  to disallow the 
25469 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a  transfer optimiz
2546a 61 74 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68  ation because th
2546b 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 6f 77  e.    ** the row
2546c 69 64 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65  ids might change
2546d 20 77 68 69 63 68 20 77 69 6c 6c 20 6d 65 73 73   which will mess
2546e 20 75 70 20 69 6e 64 65 78 69 6e 67 2e 0a 20 20   up indexing..  
2546f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 72 20 69    **.    ** Or i
25470 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  f the destinatio
25471 6e 20 68 61 73 20 61 20 55 4e 49 51 55 45 20 69  n has a UNIQUE i
25472 6e 64 65 78 20 61 6e 64 20 69 73 20 6e 6f 74 20  ndex and is not 
25473 65 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65  empty,.    ** we
25474 20 61 6c 73 6f 20 64 69 73 61 6c 6c 6f 77 20 74   also disallow t
25475 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69  he transfer opti
25476 6d 69 7a 61 74 69 6f 6e 20 62 65 63 61 75 73 65  mization because
25477 20 77 65 20 63 61 6e 6e 6f 74 0a 20 20 20 20 2a   we cannot.    *
25478 2a 20 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c  * insure that al
25479 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  l entries in the
2547a 20 75 6e 69 6f 6e 20 6f 66 20 44 45 53 54 20 61   union of DEST a
2547b 6e 64 20 53 52 43 20 77 69 6c 6c 20 62 65 0a 20  nd SRC will be. 
2547c 20 20 20 2a 2a 20 75 6e 69 71 75 65 2e 0a 20 20     ** unique..  
2547d 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 31 20 3d    */.    addr1 =
2547e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2547f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
25480 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20   iDest, 0);.    
25481 65 6d 70 74 79 44 65 73 74 54 65 73 74 20 3d 20  emptyDestTest = 
25482 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25483 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
25484 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
25485 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
25486 61 64 64 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  addr1);.  }else{
25487 0a 20 20 20 20 65 6d 70 74 79 44 65 73 74 54 65  .    emptyDestTe
25488 73 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  st = 0;.  }.  sq
25489 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
2548a 50 61 72 73 65 2c 20 69 53 72 63 2c 20 69 44 62  Parse, iSrc, iDb
2548b 53 72 63 2c 20 70 53 72 63 2c 20 4f 50 5f 4f 70  Src, pSrc, OP_Op
2548c 65 6e 52 65 61 64 29 3b 0a 20 20 65 6d 70 74 79  enRead);.  empty
2548d 53 72 63 54 65 73 74 20 3d 20 73 71 6c 69 74 65  SrcTest = sqlite
2548e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2548f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c 20  P_Rewind, iSrc, 
25490 30 29 3b 0a 20 20 72 65 67 44 61 74 61 20 3d 20  0);.  regData = 
25491 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
25492 67 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67  g(pParse);.  reg
25493 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47  Rowid = sqlite3G
25494 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
25495 29 3b 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e  );.  if( pDest->
25496 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20  iPKey>=0 ){.    
25497 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56  addr1 = sqlite3V
25498 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25499 52 6f 77 69 64 2c 20 69 53 72 63 2c 20 72 65 67  Rowid, iSrc, reg
2549a 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72  Rowid);.    addr
2549b 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
2549c 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
2549d 78 69 73 74 73 2c 20 69 44 65 73 74 2c 20 30 2c  xists, iDest, 0,
2549e 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
2549f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
254a0 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  4(v, OP_Halt, SQ
254a1 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c  LITE_CONSTRAINT,
254a2 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 0a 20 20 20   onError, 0,.   
254a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254a4 20 20 20 22 50 52 49 4d 41 52 59 20 4b 45 59 20     "PRIMARY KEY 
254a5 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 22 2c  must be unique",
254a6 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
254a7 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
254a8 48 65 72 65 28 76 2c 20 61 64 64 72 32 29 3b 0a  Here(v, addr2);.
254a9 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65 70 28      autoIncStep(
254aa 70 50 61 72 73 65 2c 20 72 65 67 41 75 74 6f 69  pParse, regAutoi
254ab 6e 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  nc, regRowid);. 
254ac 20 7d 65 6c 73 65 20 69 66 28 20 70 44 65 73 74   }else if( pDest
254ad 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20  ->pIndex==0 ){. 
254ae 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
254af 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
254b0 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 44 65  OP_NewRowid, iDe
254b1 73 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  st, regRowid);. 
254b2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72   }else{.    addr
254b3 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
254b4 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
254b5 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77 69  d, iSrc, regRowi
254b6 64 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  d);.    assert( 
254b7 28 70 44 65 73 74 2d 3e 74 61 62 46 6c 61 67 73  (pDest->tabFlags
254b8 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d   & TF_Autoincrem
254b9 65 6e 74 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20  ent)==0 );.  }. 
254ba 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
254bb 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61  p2(v, OP_RowData
254bc 2c 20 69 53 72 63 2c 20 72 65 67 44 61 74 61 29  , iSrc, regData)
254bd 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
254be 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
254bf 72 74 2c 20 69 44 65 73 74 2c 20 72 65 67 44 61  rt, iDest, regDa
254c0 74 61 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  ta, regRowid);. 
254c1 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
254c2 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 4e  geP5(v, OPFLAG_N
254c3 43 48 41 4e 47 45 7c 4f 50 46 4c 41 47 5f 4c 41  CHANGE|OPFLAG_LA
254c4 53 54 52 4f 57 49 44 7c 4f 50 46 4c 41 47 5f 41  STROWID|OPFLAG_A
254c5 50 50 45 4e 44 29 3b 0a 20 20 73 71 6c 69 74 65  PPEND);.  sqlite
254c6 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
254c7 20 2d 31 2c 20 70 44 65 73 74 2d 3e 7a 4e 61 6d   -1, pDest->zNam
254c8 65 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  e, 0);.  sqlite3
254c9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
254ca 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64 64  _Next, iSrc, add
254cb 72 31 29 3b 0a 20 20 61 75 74 6f 49 6e 63 45 6e  r1);.  autoIncEn
254cc 64 28 70 50 61 72 73 65 2c 20 69 44 62 44 65 73  d(pParse, iDbDes
254cd 74 2c 20 70 44 65 73 74 2c 20 72 65 67 41 75 74  t, pDest, regAut
254ce 6f 69 6e 63 29 3b 0a 20 20 66 6f 72 28 70 44 65  oinc);.  for(pDe
254cf 73 74 49 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e  stIdx=pDest->pIn
254d0 64 65 78 3b 20 70 44 65 73 74 49 64 78 3b 20 70  dex; pDestIdx; p
254d1 44 65 73 74 49 64 78 3d 70 44 65 73 74 49 64 78  DestIdx=pDestIdx
254d2 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 66 6f  ->pNext){.    fo
254d3 72 28 70 53 72 63 49 64 78 3d 70 53 72 63 2d 3e  r(pSrcIdx=pSrc->
254d4 70 49 6e 64 65 78 3b 20 41 4c 57 41 59 53 28 70  pIndex; ALWAYS(p
254d5 53 72 63 49 64 78 29 3b 20 70 53 72 63 49 64 78  SrcIdx); pSrcIdx
254d6 3d 70 53 72 63 49 64 78 2d 3e 70 4e 65 78 74 29  =pSrcIdx->pNext)
254d7 7b 0a 20 20 20 20 20 20 69 66 28 20 78 66 65 72  {.      if( xfer
254d8 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28  CompatibleIndex(
254d9 70 44 65 73 74 49 64 78 2c 20 70 53 72 63 49 64  pDestIdx, pSrcId
254da 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  x) ) break;.    
254db 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53  }.    assert( pS
254dc 72 63 49 64 78 20 29 3b 0a 20 20 20 20 73 71 6c  rcIdx );.    sql
254dd 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
254de 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63  , OP_Close, iSrc
254df 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
254e0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
254e1 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20  P_Close, iDest, 
254e2 30 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73  0);.    pKey = s
254e3 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
254e4 66 6f 28 70 50 61 72 73 65 2c 20 70 53 72 63 49  fo(pParse, pSrcI
254e5 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dx);.    sqlite3
254e6 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
254e7 5f 4f 70 65 6e 52 65 61 64 2c 20 69 53 72 63 2c  _OpenRead, iSrc,
254e8 20 70 53 72 63 49 64 78 2d 3e 74 6e 75 6d 2c 20   pSrcIdx->tnum, 
254e9 69 44 62 53 72 63 2c 0a 20 20 20 20 20 20 20 20  iDbSrc,.        
254ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
254eb 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45  har*)pKey, P4_KE
254ec 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
254ed 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
254ee 28 76 2c 20 22 25 73 22 2c 20 70 53 72 63 49 64  (v, "%s", pSrcId
254ef 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20  x->zName));.    
254f0 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e  pKey = sqlite3In
254f1 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
254f2 65 2c 20 70 44 65 73 74 49 64 78 29 3b 0a 20 20  e, pDestIdx);.  
254f3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
254f4 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72  Op4(v, OP_OpenWr
254f5 69 74 65 2c 20 69 44 65 73 74 2c 20 70 44 65 73  ite, iDest, pDes
254f6 74 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 44  tIdx->tnum, iDbD
254f7 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  est,.           
254f8 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
254f9 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
254fa 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
254fb 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
254fc 20 22 25 73 22 2c 20 70 44 65 73 74 49 64 78 2d   "%s", pDestIdx-
254fd 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61 64  >zName));.    ad
254fe 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
254ff 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
25500 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a  wind, iSrc, 0);.
25501 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25502 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 4b  ddOp2(v, OP_RowK
25503 65 79 2c 20 69 53 72 63 2c 20 72 65 67 44 61 74  ey, iSrc, regDat
25504 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  a);.    sqlite3V
25505 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
25506 49 64 78 49 6e 73 65 72 74 2c 20 69 44 65 73 74  IdxInsert, iDest
25507 2c 20 72 65 67 44 61 74 61 2c 20 31 29 3b 0a 20  , regData, 1);. 
25508 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25509 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
2550a 20 69 53 72 63 2c 20 61 64 64 72 31 2b 31 29 3b   iSrc, addr1+1);
2550b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2550c 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2550d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  1);.  }.  sqlite
2550e 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2550f 20 65 6d 70 74 79 53 72 63 54 65 73 74 29 3b 0a   emptySrcTest);.
25510 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
25511 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
25512 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c  regRowid);.  sql
25513 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
25514 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 44 61  eg(pParse, regDa
25515 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ta);.  sqlite3Vd
25516 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
25517 6c 6f 73 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a  lose, iSrc, 0);.
25518 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25519 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
2551a 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 69 66   iDest, 0);.  if
2551b 28 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 20  ( emptyDestTest 
2551c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2551d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48  beAddOp2(v, OP_H
2551e 61 6c 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  alt, SQLITE_OK, 
2551f 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
25520 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 65  dbeJumpHere(v, e
25521 6d 70 74 79 44 65 73 74 54 65 73 74 29 3b 0a 20  mptyDestTest);. 
25522 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25523 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
25524 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20  , iDest, 0);.   
25525 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
25526 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  se{.    return 1
25527 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  ;.  }.}.#endif /
25528 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46  * SQLITE_OMIT_XF
25529 45 52 5f 4f 50 54 20 2a 2f 0a 0a 2f 2a 20 4d 61  ER_OPT */../* Ma
2552a 6b 65 20 73 75 72 65 20 22 69 73 56 69 65 77 22  ke sure "isView"
2552b 20 67 65 74 73 20 75 6e 64 65 66 69 6e 65 64 20   gets undefined 
2552c 69 6e 20 63 61 73 65 20 74 68 69 73 20 66 69 6c  in case this fil
2552d 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f  e becomes part o
2552e 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61 6d  f.** the amalgam
2552f 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74 20  ation - so that 
25530 73 75 62 73 65 71 75 65 6e 74 20 66 69 6c 65 73  subsequent files
25531 20 64 6f 20 6e 6f 74 20 73 65 65 20 69 73 56 69   do not see isVi
25532 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72 6f  ew as a.** macro
25533 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56 69  . */.#undef isVi
25534 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ew../***********
25535 2a 2a 2a 20 45 6e 64 20 6f 66 20 69 6e 73 65 72  *** End of inser
25536 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
25537 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25539 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2553a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c  *** Begin file l
2553b 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  egacy.c ********
2553c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2553d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2553e 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
2553f 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
25540 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
25541 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
25542 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
25543 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
25544 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
25545 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
25546 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
25547 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
25548 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
25549 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2554a 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2554b 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2554c 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2554d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2554e 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2554f 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
25550 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
25551 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
25552 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25553 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61  **********.** Ma
25556 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  in file for the 
25557 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
25558 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
25559 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d   this file.** im
2555a 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67  plement the prog
2555b 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65  rammer interface
2555c 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e   to the library.
2555d 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a    Routines in.**
2555e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65   other files are
2555f 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
25560 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20  e by SQLite and 
25561 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
25562 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65   accessed by use
25563 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  rs of the librar
25564 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c 65  y..**.** $Id: le
25565 67 61 63 79 2e 63 2c 76 20 31 2e 33 33 20 32 30  gacy.c,v 1.33 20
25566 30 39 2f 30 35 2f 30 35 20 32 30 3a 30 32 3a 34  09/05/05 20:02:4
25567 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  8 drh Exp $.*/..
25568 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 53  ./*.** Execute S
25569 51 4c 20 63 6f 64 65 2e 20 20 52 65 74 75 72 6e  QL code.  Return
2556a 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49   one of the SQLI
2556b 54 45 5f 20 73 75 63 63 65 73 73 2f 66 61 69 6c  TE_ success/fail
2556c 75 72 65 0a 2a 2a 20 63 6f 64 65 73 2e 20 20 41  ure.** codes.  A
2556d 6c 73 6f 20 77 72 69 74 65 20 61 6e 20 65 72 72  lso write an err
2556e 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20  or message into 
2556f 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
25570 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29  from.** malloc()
25571 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 45 72 72   and make *pzErr
25572 4d 73 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61  Msg point to tha
25573 74 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a  t message..**.**
25574 20 49 66 20 74 68 65 20 53 51 4c 20 69 73 20 61   If the SQL is a
25575 20 71 75 65 72 79 2c 20 74 68 65 6e 20 66 6f 72   query, then for
25576 20 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65   each row in the
25577 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a   query result.**
25578 20 74 68 65 20 78 43 61 6c 6c 62 61 63 6b 28 29   the xCallback()
25579 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
2557a 6c 65 64 2e 20 20 70 41 72 67 20 62 65 63 6f 6d  led.  pArg becom
2557b 65 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  es the first.** 
2557c 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 61 6c  argument to xCal
2557d 6c 62 61 63 6b 28 29 2e 20 20 49 66 20 78 43 61  lback().  If xCa
2557e 6c 6c 62 61 63 6b 3d 4e 55 4c 4c 20 74 68 65 6e  llback=NULL then
2557f 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20   no callback.** 
25580 69 73 20 69 6e 76 6f 6b 65 64 2c 20 65 76 65 6e  is invoked, even
25581 20 66 6f 72 20 71 75 65 72 69 65 73 2e 0a 2a 2f   for queries..*/
25582 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
25583 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20  sqlite3_exec(.  
25584 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
25585 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
25586 68 65 20 64 61 74 61 62 61 73 65 20 6f 6e 20 77  he database on w
25587 68 69 63 68 20 74 68 65 20 53 51 4c 20 65 78 65  hich the SQL exe
25588 63 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  cutes */.  const
25589 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
2558a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 51         /* The SQ
2558b 4c 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64  L to be executed
2558c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 61   */.  sqlite3_ca
2558d 6c 6c 62 61 63 6b 20 78 43 61 6c 6c 62 61 63 6b  llback xCallback
2558e 2c 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 69 73  , /* Invoke this
2558f 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e   callback routin
25590 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  e */.  void *pAr
25591 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g,              
25592 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
25593 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61 63  ment to xCallbac
25594 6b 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  k() */.  char **
25595 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 20  pzErrMsg        
25596 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72       /* Write er
25597 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 72  ror messages her
25598 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
25599 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
2559a 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
2559b 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  code */.  const 
2559c 63 68 61 72 20 2a 7a 4c 65 66 74 6f 76 65 72 3b  char *zLeftover;
2559d 20 20 20 20 20 20 2f 2a 20 54 61 69 6c 20 6f 66        /* Tail of
2559e 20 75 6e 70 72 6f 63 65 73 73 65 64 20 53 51 4c   unprocessed SQL
2559f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
255a0 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 20 20  mt *pStmt = 0;  
255a1 20 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74    /* The current
255a2 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a   SQL statement *
255a3 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c  /.  char **azCol
255a4 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  s = 0;          
255a5 2f 2a 20 4e 61 6d 65 73 20 6f 66 20 72 65 73 75  /* Names of resu
255a6 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  lt columns */.  
255a7 69 6e 74 20 6e 52 65 74 72 79 20 3d 20 30 3b 20  int nRetry = 0; 
255a8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
255a9 75 6d 62 65 72 20 6f 66 20 72 65 74 72 79 20 61  umber of retry a
255aa 74 74 65 6d 70 74 73 20 2a 2f 0a 20 20 69 6e 74  ttempts */.  int
255ab 20 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 3b   callbackIsInit;
255ac 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
255ad 20 69 66 20 63 61 6c 6c 62 61 63 6b 20 64 61 74   if callback dat
255ae 61 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  a is initialized
255af 20 2a 2f 0a 0a 20 20 69 66 28 20 7a 53 71 6c 3d   */..  if( zSql=
255b0 3d 30 20 29 20 7a 53 71 6c 20 3d 20 22 22 3b 0a  =0 ) zSql = "";.
255b1 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
255b2 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
255b3 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  );.  sqlite3Erro
255b4 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  r(db, SQLITE_OK,
255b5 20 30 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 72   0);.  while( (r
255b6 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
255b7 28 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45  (rc==SQLITE_SCHE
255b8 4d 41 20 26 26 20 28 2b 2b 6e 52 65 74 72 79 29  MA && (++nRetry)
255b9 3c 32 29 29 20 26 26 20 7a 53 71 6c 5b 30 5d 20  <2)) && zSql[0] 
255ba 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b  ){.    int nCol;
255bb 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 56 61  .    char **azVa
255bc 6c 73 20 3d 20 30 3b 0a 0a 20 20 20 20 70 53 74  ls = 0;..    pSt
255bd 6d 74 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  mt = 0;.    rc =
255be 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
255bf 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
255c0 70 53 74 6d 74 2c 20 26 7a 4c 65 66 74 6f 76 65  pStmt, &zLeftove
255c1 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  r);.    assert( 
255c2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
255c3 20 70 53 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 20   pStmt==0 );.   
255c4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
255c5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74  OK ){.      cont
255c6 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  inue;.    }.    
255c7 69 66 28 20 21 70 53 74 6d 74 20 29 7b 0a 20 20  if( !pStmt ){.  
255c8 20 20 20 20 2f 2a 20 74 68 69 73 20 68 61 70 70      /* this happ
255c9 65 6e 73 20 66 6f 72 20 61 20 63 6f 6d 6d 65 6e  ens for a commen
255ca 74 20 6f 72 20 77 68 69 74 65 2d 73 70 61 63 65  t or white-space
255cb 20 2a 2f 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d   */.      zSql =
255cc 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20 20   zLeftover;.    
255cd 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
255ce 7d 0a 0a 20 20 20 20 63 61 6c 6c 62 61 63 6b 49  }..    callbackI
255cf 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 6e  sInit = 0;.    n
255d0 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  Col = sqlite3_co
255d1 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74  lumn_count(pStmt
255d2 29 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 31  );..    while( 1
255d3 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
255d4 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
255d5 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b  te3_step(pStmt);
255d6 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  ..      /* Invok
255d7 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
255d8 75 6e 63 74 69 6f 6e 20 69 66 20 72 65 71 75 69  unction if requi
255d9 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  red */.      if(
255da 20 78 43 61 6c 6c 62 61 63 6b 20 26 26 20 28 53   xCallback && (S
255db 51 4c 49 54 45 5f 52 4f 57 3d 3d 72 63 20 7c 7c  QLITE_ROW==rc ||
255dc 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c   .          (SQL
255dd 49 54 45 5f 44 4f 4e 45 3d 3d 72 63 20 26 26 20  ITE_DONE==rc && 
255de 21 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 0a  !callbackIsInit.
255df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
255e0 20 20 20 20 20 20 20 20 20 20 20 26 26 20 64 62             && db
255e1 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 4e  ->flags&SQLITE_N
255e2 75 6c 6c 43 61 6c 6c 62 61 63 6b 29 29 20 29 7b  ullCallback)) ){
255e3 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 63 61  .        if( !ca
255e4 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 29 7b 0a  llbackIsInit ){.
255e5 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c 73            azCols
255e6 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
255e7 6f 63 5a 65 72 6f 28 64 62 2c 20 32 2a 6e 43 6f  ocZero(db, 2*nCo
255e8 6c 2a 73 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63  l*sizeof(const c
255e9 68 61 72 2a 29 20 2b 20 31 29 3b 0a 20 20 20 20  har*) + 1);.    
255ea 20 20 20 20 20 20 69 66 28 20 61 7a 43 6f 6c 73        if( azCols
255eb 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
255ec 20 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74     goto exec_out
255ed 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
255ee 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
255ef 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
255f0 20 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c             azCol
255f1 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a 29 73  s[i] = (char *)s
255f2 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
255f3 6d 65 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20  me(pStmt, i);.  
255f4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c            /* sql
255f5 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
255f6 6d 65 28 29 20 69 6e 73 74 61 6c 6c 73 20 63 6f  me() installs co
255f7 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 73 20 55 54  lumn names as UT
255f8 46 38 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  F8.            *
255f9 2a 20 73 74 72 69 6e 67 73 20 73 6f 20 74 68 65  * strings so the
255fa 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72  re is no way for
255fb 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
255fc 6e 61 6d 65 28 29 20 74 6f 20 66 61 69 6c 2e 20  name() to fail. 
255fd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  */.            a
255fe 73 73 65 72 74 28 20 61 7a 43 6f 6c 73 5b 69 5d  ssert( azCols[i]
255ff 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  !=0 );.         
25600 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 61 6c   }.          cal
25601 6c 62 61 63 6b 49 73 49 6e 69 74 20 3d 20 31 3b  lbackIsInit = 1;
25602 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
25603 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
25604 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20  E_ROW ){.       
25605 20 20 20 61 7a 56 61 6c 73 20 3d 20 26 61 7a 43     azVals = &azC
25606 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a 20 20 20 20 20  ols[nCol];.     
25607 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
25608 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
25609 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 5b 69          azVals[i
2560a 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  ] = (char *)sqli
2560b 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
2560c 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20  pStmt, i);.     
2560d 20 20 20 20 20 20 20 69 66 28 20 21 61 7a 56 61         if( !azVa
2560e 6c 73 5b 69 5d 20 26 26 20 73 71 6c 69 74 65 33  ls[i] && sqlite3
2560f 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74  _column_type(pSt
25610 6d 74 2c 20 69 29 21 3d 53 51 4c 49 54 45 5f 4e  mt, i)!=SQLITE_N
25611 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ULL ){.         
25612 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
25613 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
25614 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78           goto ex
25615 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  ec_out;.        
25616 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
25617 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
25618 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63      if( xCallbac
25619 6b 28 70 41 72 67 2c 20 6e 43 6f 6c 2c 20 61 7a  k(pArg, nCol, az
2561a 56 61 6c 73 2c 20 61 7a 43 6f 6c 73 29 20 29 7b  Vals, azCols) ){
2561b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
2561c 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20  SQLITE_ABORT;.  
2561d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2561e 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
2561f 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 20 20  e *)pStmt);.    
25620 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b        pStmt = 0;
25621 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
25622 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
25623 54 45 5f 41 42 4f 52 54 2c 20 30 29 3b 0a 20 20  TE_ABORT, 0);.  
25624 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 65          goto exe
25625 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d  c_out;.        }
25626 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
25627 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
25628 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OW ){.        rc
25629 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69   = sqlite3VdbeFi
2562a 6e 61 6c 69 7a 65 28 28 56 64 62 65 20 2a 29 70  nalize((Vdbe *)p
2562b 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 70  Stmt);.        p
2562c 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  Stmt = 0;.      
2562d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2562e 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20 20 20  _SCHEMA ){.     
2562f 20 20 20 20 20 6e 52 65 74 72 79 20 3d 20 30 3b       nRetry = 0;
25630 0a 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20  .          zSql 
25631 3d 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20  = zLeftover;.   
25632 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71         while( sq
25633 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 53 71  lite3Isspace(zSq
25634 6c 5b 30 5d 29 20 29 20 7a 53 71 6c 2b 2b 3b 0a  l[0]) ) zSql++;.
25635 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25636 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
25637 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
25638 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 7a  te3DbFree(db, az
25639 43 6f 6c 73 29 3b 0a 20 20 20 20 61 7a 43 6f 6c  Cols);.    azCol
2563a 73 20 3d 20 30 3b 0a 20 20 7d 0a 0a 65 78 65 63  s = 0;.  }..exec
2563b 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 53 74 6d  _out:.  if( pStm
2563c 74 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 46  t ) sqlite3VdbeF
2563d 69 6e 61 6c 69 7a 65 28 28 56 64 62 65 20 2a 29  inalize((Vdbe *)
2563e 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65  pStmt);.  sqlite
2563f 33 44 62 46 72 65 65 28 64 62 2c 20 61 7a 43 6f  3DbFree(db, azCo
25640 6c 73 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c  ls);..  rc = sql
25641 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
25642 72 63 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  rc);.  if( rc!=S
25643 51 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57 41  QLITE_OK && ALWA
25644 59 53 28 72 63 3d 3d 73 71 6c 69 74 65 33 5f 65  YS(rc==sqlite3_e
25645 72 72 63 6f 64 65 28 64 62 29 29 20 26 26 20 70  rrcode(db)) && p
25646 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 69  zErrMsg ){.    i
25647 6e 74 20 6e 45 72 72 4d 73 67 20 3d 20 31 20 2b  nt nErrMsg = 1 +
25648 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
25649 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  (sqlite3_errmsg(
2564a 64 62 29 29 3b 0a 20 20 20 20 2a 70 7a 45 72 72  db));.    *pzErr
2564b 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Msg = sqlite3Mal
2564c 6c 6f 63 28 6e 45 72 72 4d 73 67 29 3b 0a 20 20  loc(nErrMsg);.  
2564d 20 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67 20    if( *pzErrMsg 
2564e 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
2564f 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69 74  *pzErrMsg, sqlit
25650 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 6e  e3_errmsg(db), n
25651 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20  ErrMsg);.    }. 
25652 20 7d 65 6c 73 65 20 69 66 28 20 70 7a 45 72 72   }else if( pzErr
25653 4d 73 67 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72  Msg ){.    *pzEr
25654 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  rMsg = 0;.  }.. 
25655 20 61 73 73 65 72 74 28 20 28 72 63 26 64 62 2d   assert( (rc&db-
25656 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b  >errMask)==rc );
25657 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
25658 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
25659 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2565a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
2565b 2a 2a 20 45 6e 64 20 6f 66 20 6c 65 67 61 63 79  ** End of legacy
2565c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2565d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2565e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2565f 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
25660 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 6f  ** Begin file lo
25661 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  adext.c ********
25662 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25663 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25664 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75  */./*.** 2006 Ju
25665 6e 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ne 7.**.** The a
25666 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
25667 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
25668 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
25669 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2566a 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2566b 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2566c 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2566d 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2566e 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2566f 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
25670 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
25671 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
25672 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
25673 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
25674 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
25675 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
25676 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
25677 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25678 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25679 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2567a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2567b 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
2567c 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75   contains code u
2567d 73 65 64 20 74 6f 20 64 79 6e 61 6d 69 63 61 6c  sed to dynamical
2567e 6c 79 20 6c 6f 61 64 20 65 78 74 65 6e 73 69 6f  ly load extensio
2567f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 53  ns into.** the S
25680 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a  QLite library..*
25681 2a 0a 2a 2a 20 24 49 64 3a 20 6c 6f 61 64 65 78  *.** $Id: loadex
25682 74 2e 63 2c 76 20 31 2e 35 38 20 32 30 30 39 2f  t.c,v 1.58 2009/
25683 30 31 2f 32 30 20 31 36 3a 35 33 3a 34 30 20 64  01/20 16:53:40 d
25684 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
25685 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
25686 49 54 45 5f 43 4f 52 45 0a 20 20 23 64 65 66 69  ITE_CORE.  #defi
25687 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31  ne SQLITE_CORE 1
25688 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 74 68 65    /* Disable the
25689 20 41 50 49 20 72 65 64 65 66 69 6e 69 74 69 6f   API redefinitio
2568a 6e 20 69 6e 20 73 71 6c 69 74 65 33 65 78 74 2e  n in sqlite3ext.
2568b 68 20 2a 2f 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a  h */.#endif./***
2568c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
2568d 75 64 65 20 73 71 6c 69 74 65 33 65 78 74 2e 68  ude sqlite3ext.h
2568e 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
2568f 66 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a  f loadext.c ****
25690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
25691 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
25692 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33 65 78  n file sqlite3ex
25693 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
25694 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25695 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
25696 2a 20 32 30 30 36 20 4a 75 6e 65 20 37 0a 2a 2a  * 2006 June 7.**
25697 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
25698 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
25699 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2569a 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2569b 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2569c 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2569d 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2569e 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2569f 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
256a0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
256a1 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
256a2 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
256a3 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
256a4 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
256a5 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
256a6 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
256a7 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
256a8 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
256a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
256ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
256ad 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
256ae 20 64 65 66 69 6e 65 73 20 74 68 65 20 53 51 4c   defines the SQL
256af 69 74 65 20 69 6e 74 65 72 66 61 63 65 20 66 6f  ite interface fo
256b0 72 20 75 73 65 20 62 79 0a 2a 2a 20 73 68 61 72  r use by.** shar
256b1 65 64 20 6c 69 62 72 61 72 69 65 73 20 74 68 61  ed libraries tha
256b2 74 20 77 61 6e 74 20 74 6f 20 62 65 20 69 6d 70  t want to be imp
256b3 6f 72 74 65 64 20 61 73 20 65 78 74 65 6e 73 69  orted as extensi
256b4 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 53  ons into.** an S
256b5 51 4c 69 74 65 20 69 6e 73 74 61 6e 63 65 2e 20  QLite instance. 
256b6 20 53 68 61 72 65 64 20 6c 69 62 72 61 72 69 65   Shared librarie
256b7 73 20 74 68 61 74 20 69 6e 74 65 6e 64 20 74 6f  s that intend to
256b8 20 62 65 20 6c 6f 61 64 65 64 0a 2a 2a 20 61 73   be loaded.** as
256b9 20 65 78 74 65 6e 73 69 6f 6e 73 20 62 79 20 53   extensions by S
256ba 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 23 69 6e  QLite should #in
256bb 63 6c 75 64 65 20 74 68 69 73 20 66 69 6c 65 20  clude this file 
256bc 69 6e 73 74 65 61 64 20 6f 66 20 0a 2a 2a 20 73  instead of .** s
256bd 71 6c 69 74 65 33 2e 68 2e 0a 2a 2a 0a 2a 2a 20  qlite3.h..**.** 
256be 40 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65  @(#) $Id: sqlite
256bf 33 65 78 74 2e 68 2c 76 20 31 2e 32 35 20 32 30  3ext.h,v 1.25 20
256c0 30 38 2f 31 30 2f 31 32 20 30 30 3a 32 37 3a 35  08/10/12 00:27:5
256c1 34 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f  4 shane Exp $.*/
256c2 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  .#ifndef _SQLITE
256c3 33 45 58 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20  3EXT_H_.#define 
256c4 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a 0a  _SQLITE3EXT_H_..
256c5 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
256c6 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
256c7 6e 65 73 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nes sqlite3_api_
256c8 72 6f 75 74 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a 2a  routines;../*.**
256c9 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
256ca 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 70  tructure holds p
256cb 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 6f  ointers to all o
256cc 66 20 74 68 65 20 53 51 4c 69 74 65 20 41 50 49  f the SQLite API
256cd 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  .** routines..**
256ce 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20 49 6e  .** WARNING:  In
256cf 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61   order to mainta
256d0 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  in backwards com
256d1 70 61 74 69 62 69 6c 69 74 79 2c 20 61 64 64 20  patibility, add 
256d2 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  new.** interface
256d3 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  s to the end of 
256d4 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
256d5 6e 6c 79 2e 20 20 49 66 20 79 6f 75 20 69 6e 73  nly.  If you ins
256d6 65 72 74 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72  ert new.** inter
256d7 66 61 63 65 73 20 69 6e 20 74 68 65 20 6d 69 64  faces in the mid
256d8 64 6c 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  dle of this stru
256d9 63 74 75 72 65 2c 20 74 68 65 6e 20 6f 6c 64 65  cture, then olde
256da 72 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 76  r different.** v
256db 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
256dc 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  e will not be ab
256dd 6c 65 20 74 6f 20 6c 6f 61 64 20 65 61 63 68 20  le to load each 
256de 6f 74 68 65 72 73 27 20 73 68 61 72 65 64 0a 2a  others' shared.*
256df 2a 20 6c 69 62 72 61 72 69 65 73 21 0a 2a 2f 0a  * libraries!.*/.
256e0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 61  struct sqlite3_a
256e1 70 69 5f 72 6f 75 74 69 6e 65 73 20 7b 0a 20 20  pi_routines {.  
256e2 76 6f 69 64 20 2a 20 28 2a 61 67 67 72 65 67 61  void * (*aggrega
256e3 74 65 5f 63 6f 6e 74 65 78 74 29 28 73 71 6c 69  te_context)(sqli
256e4 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
256e5 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 6e 74 20   nBytes);.  int 
256e6 20 28 2a 61 67 67 72 65 67 61 74 65 5f 63 6f 75   (*aggregate_cou
256e7 6e 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nt)(sqlite3_cont
256e8 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ext*);.  int  (*
256e9 62 69 6e 64 5f 62 6c 6f 62 29 28 73 71 6c 69 74  bind_blob)(sqlit
256ea 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e  e3_stmt*,int,con
256eb 73 74 20 76 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76  st void*,int n,v
256ec 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
256ed 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 64 6f    int  (*bind_do
256ee 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74  uble)(sqlite3_st
256ef 6d 74 2a 2c 69 6e 74 2c 64 6f 75 62 6c 65 29 3b  mt*,int,double);
256f0 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69  .  int  (*bind_i
256f1 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
256f2 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e  *,int,int);.  in
256f3 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 36 34 29  t  (*bind_int64)
256f4 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
256f5 6e 74 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29  nt,sqlite_int64)
256f6 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f  ;.  int  (*bind_
256f7 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33 5f 73 74  null)(sqlite3_st
256f8 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  mt*,int);.  int 
256f9 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65   (*bind_paramete
256fa 72 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33  r_count)(sqlite3
256fb 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20  _stmt*);.  int  
256fc 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  (*bind_parameter
256fd 5f 69 6e 64 65 78 29 28 73 71 6c 69 74 65 33 5f  _index)(sqlite3_
256fe 73 74 6d 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72  stmt*,const char
256ff 2a 7a 4e 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74  *zName);.  const
25700 20 63 68 61 72 20 2a 20 28 2a 62 69 6e 64 5f 70   char * (*bind_p
25701 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 29 28 73  arameter_name)(s
25702 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
25703 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
25704 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 73  _text)(sqlite3_s
25705 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  tmt*,int,const c
25706 68 61 72 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28  har*,int n,void(
25707 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  *)(void*));.  in
25708 74 20 20 28 2a 62 69 6e 64 5f 74 65 78 74 31 36  t  (*bind_text16
25709 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
2570a 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
2570b 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  int,void(*)(void
2570c 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  *));.  int  (*bi
2570d 6e 64 5f 76 61 6c 75 65 29 28 73 71 6c 69 74 65  nd_value)(sqlite
2570e 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73  3_stmt*,int,cons
2570f 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t sqlite3_value*
25710 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79  );.  int  (*busy
25711 5f 68 61 6e 64 6c 65 72 29 28 73 71 6c 69 74 65  _handler)(sqlite
25712 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c  3*,int(*)(void*,
25713 69 6e 74 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69  int),void*);.  i
25714 6e 74 20 20 28 2a 62 75 73 79 5f 74 69 6d 65 6f  nt  (*busy_timeo
25715 75 74 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ut)(sqlite3*,int
25716 20 6d 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63   ms);.  int  (*c
25717 68 61 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a  hanges)(sqlite3*
25718 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6c 6f 73  );.  int  (*clos
25719 65 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  e)(sqlite3*);.  
2571a 69 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e  int  (*collation
2571b 5f 6e 65 65 64 65 64 29 28 73 71 6c 69 74 65 33  _needed)(sqlite3
2571c 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28  *,void*,void(*)(
2571d 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69  void*,sqlite3*,i
2571e 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73  nt eTextRep,cons
2571f 74 20 63 68 61 72 2a 29 29 3b 0a 20 20 69 6e 74  t char*));.  int
25720 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65    (*collation_ne
25721 65 64 65 64 31 36 29 28 73 71 6c 69 74 65 33 2a  eded16)(sqlite3*
25722 2c 76 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76  ,void*,void(*)(v
25723 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
25724 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
25725 20 76 6f 69 64 2a 29 29 3b 0a 20 20 63 6f 6e 73   void*));.  cons
25726 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d  t void * (*colum
25727 6e 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f  n_blob)(sqlite3_
25728 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b  stmt*,int iCol);
25729 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e  .  int  (*column
2572a 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f  _bytes)(sqlite3_
2572b 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b  stmt*,int iCol);
2572c 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e  .  int  (*column
2572d 5f 62 79 74 65 73 31 36 29 28 73 71 6c 69 74 65  _bytes16)(sqlite
2572e 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
2572f 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75  );.  int  (*colu
25730 6d 6e 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65  mn_count)(sqlite
25731 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20  3_stmt*pStmt);. 
25732 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
25733 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
25734 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74  name)(sqlite3_st
25735 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73  mt*,int);.  cons
25736 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d  t void * (*colum
25737 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31  n_database_name1
25738 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
25739 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63  ,int);.  const c
2573a 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64  har * (*column_d
2573b 65 63 6c 74 79 70 65 29 28 73 71 6c 69 74 65 33  ecltype)(sqlite3
2573c 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 29 3b 0a 20  _stmt*,int i);. 
2573d 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
2573e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
2573f 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
25740 2c 69 6e 74 29 3b 0a 20 20 64 6f 75 62 6c 65 20  ,int);.  double 
25741 20 28 2a 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65   (*column_double
25742 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
25743 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74  int iCol);.  int
25744 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28    (*column_int)(
25745 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
25746 74 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74  t iCol);.  sqlit
25747 65 5f 69 6e 74 36 34 20 20 28 2a 63 6f 6c 75 6d  e_int64  (*colum
25748 6e 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33  n_int64)(sqlite3
25749 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
2574a 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2574b 20 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28   (*column_name)(
2574c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2574d 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  t);.  const void
2574e 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65   * (*column_name
2574f 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
25750 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
25751 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  char * (*column_
25752 6f 72 69 67 69 6e 5f 6e 61 6d 65 29 28 73 71 6c  origin_name)(sql
25753 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
25754 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
25755 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  (*column_origin_
25756 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f  name16)(sqlite3_
25757 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f  stmt*,int);.  co
25758 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
25759 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 28  umn_table_name)(
2575a 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2575b 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  t);.  const void
2575c 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c   * (*column_tabl
2575d 65 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65  e_name16)(sqlite
2575e 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
2575f 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
25760 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74  har * (*column_t
25761 65 78 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ext)(sqlite3_stm
25762 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
25763 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
25764 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 29 28 73 71  olumn_text16)(sq
25765 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
25766 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a  iCol);.  int  (*
25767 63 6f 6c 75 6d 6e 5f 74 79 70 65 29 28 73 71 6c  column_type)(sql
25768 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
25769 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Col);.  sqlite3_
2576a 76 61 6c 75 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  value* (*column_
2576b 76 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73  value)(sqlite3_s
2576c 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
2576d 20 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6d 6d 69    void * (*commi
2576e 74 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a  t_hook)(sqlite3*
2576f 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76  ,int(*)(void*),v
25770 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
25771 63 6f 6d 70 6c 65 74 65 29 28 63 6f 6e 73 74 20  complete)(const 
25772 63 68 61 72 2a 73 71 6c 29 3b 0a 20 20 69 6e 74  char*sql);.  int
25773 20 20 28 2a 63 6f 6d 70 6c 65 74 65 31 36 29 28    (*complete16)(
25774 63 6f 6e 73 74 20 76 6f 69 64 2a 73 71 6c 29 3b  const void*sql);
25775 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65  .  int  (*create
25776 5f 63 6f 6c 6c 61 74 69 6f 6e 29 28 73 71 6c 69  _collation)(sqli
25777 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
25778 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a  ,int,void*,int(*
25779 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
2577a 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
2577b 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74  t void*));.  int
2577c 20 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61    (*create_colla
2577d 74 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a  tion16)(sqlite3*
2577e 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
2577f 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f  ,void*,int(*)(vo
25780 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
25781 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
25782 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a  id*));.  int  (*
25783 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 29  create_function)
25784 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
25785 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f  char*,int,int,vo
25786 69 64 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63  id*,void (*xFunc
25787 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
25788 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
25789 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78  alue**),void (*x
2578a 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
2578b 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
2578c 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64  e3_value**),void
2578d 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
2578e 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20  e3_context*));. 
2578f 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 66   int  (*create_f
25790 75 6e 63 74 69 6f 6e 31 36 29 28 73 71 6c 69 74  unction16)(sqlit
25791 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  e3*,const void*,
25792 69 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f  int,int,void*,vo
25793 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
25794 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
25795 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
25796 29 2c 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28  ),void (*xStep)(
25797 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
25798 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
25799 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69  ue**),void (*xFi
2579a 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
2579b 74 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74 20 28  text*));.  int (
2579c 2a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 29 28  *create_module)(
2579d 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
2579e 68 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74  har*,const sqlit
2579f 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a  e3_module*,void*
257a0 29 3b 0a 20 20 69 6e 74 20 20 28 2a 64 61 74 61  );.  int  (*data
257a1 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f  _count)(sqlite3_
257a2 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 73  stmt*pStmt);.  s
257a3 71 6c 69 74 65 33 20 2a 20 28 2a 64 62 5f 68 61  qlite3 * (*db_ha
257a4 6e 64 6c 65 29 28 73 71 6c 69 74 65 33 5f 73 74  ndle)(sqlite3_st
257a5 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 64 65  mt*);.  int (*de
257a6 63 6c 61 72 65 5f 76 74 61 62 29 28 73 71 6c 69  clare_vtab)(sqli
257a7 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
257a8 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 6e 61 62  );.  int  (*enab
257a9 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 29  le_shared_cache)
257aa 28 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  (int);.  int  (*
257ab 65 72 72 63 6f 64 65 29 28 73 71 6c 69 74 65 33  errcode)(sqlite3
257ac 2a 64 62 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  *db);.  const ch
257ad 61 72 20 2a 20 28 2a 65 72 72 6d 73 67 29 28 73  ar * (*errmsg)(s
257ae 71 6c 69 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73  qlite3*);.  cons
257af 74 20 76 6f 69 64 20 2a 20 28 2a 65 72 72 6d 73  t void * (*errms
257b0 67 31 36 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  g16)(sqlite3*);.
257b1 20 20 69 6e 74 20 20 28 2a 65 78 65 63 29 28 73    int  (*exec)(s
257b2 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
257b3 61 72 2a 2c 73 71 6c 69 74 65 33 5f 63 61 6c 6c  ar*,sqlite3_call
257b4 62 61 63 6b 2c 76 6f 69 64 2a 2c 63 68 61 72 2a  back,void*,char*
257b5 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78 70  *);.  int  (*exp
257b6 69 72 65 64 29 28 73 71 6c 69 74 65 33 5f 73 74  ired)(sqlite3_st
257b7 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 66  mt*);.  int  (*f
257b8 69 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33  inalize)(sqlite3
257b9 5f 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20  _stmt*pStmt);.  
257ba 76 6f 69 64 20 20 28 2a 66 72 65 65 29 28 76 6f  void  (*free)(vo
257bb 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  id*);.  void  (*
257bc 66 72 65 65 5f 74 61 62 6c 65 29 28 63 68 61 72  free_table)(char
257bd 2a 2a 72 65 73 75 6c 74 29 3b 0a 20 20 69 6e 74  **result);.  int
257be 20 20 28 2a 67 65 74 5f 61 75 74 6f 63 6f 6d 6d    (*get_autocomm
257bf 69 74 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  it)(sqlite3*);. 
257c0 20 76 6f 69 64 20 2a 20 28 2a 67 65 74 5f 61 75   void * (*get_au
257c1 78 64 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63  xdata)(sqlite3_c
257c2 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20  ontext*,int);.  
257c3 69 6e 74 20 20 28 2a 67 65 74 5f 74 61 62 6c 65  int  (*get_table
257c4 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
257c5 20 63 68 61 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69   char*,char***,i
257c6 6e 74 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 2a 29  nt*,int*,char**)
257c7 3b 0a 20 20 69 6e 74 20 20 28 2a 67 6c 6f 62 61  ;.  int  (*globa
257c8 6c 5f 72 65 63 6f 76 65 72 29 28 76 6f 69 64 29  l_recover)(void)
257c9 3b 0a 20 20 76 6f 69 64 20 20 28 2a 69 6e 74 65  ;.  void  (*inte
257ca 72 72 75 70 74 78 29 28 73 71 6c 69 74 65 33 2a  rruptx)(sqlite3*
257cb 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  );.  sqlite_int6
257cc 34 20 20 28 2a 6c 61 73 74 5f 69 6e 73 65 72 74  4  (*last_insert
257cd 5f 72 6f 77 69 64 29 28 73 71 6c 69 74 65 33 2a  _rowid)(sqlite3*
257ce 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
257cf 2a 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e 29 28  * (*libversion)(
257d0 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a  void);.  int  (*
257d1 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
257d2 72 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64  r)(void);.  void
257d3 20 2a 28 2a 6d 61 6c 6c 6f 63 29 28 69 6e 74 29   *(*malloc)(int)
257d4 3b 0a 20 20 63 68 61 72 20 2a 20 28 2a 6d 70 72  ;.  char * (*mpr
257d5 69 6e 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72  intf)(const char
257d6 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28  *,...);.  int  (
257d7 2a 6f 70 65 6e 29 28 63 6f 6e 73 74 20 63 68 61  *open)(const cha
257d8 72 2a 2c 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20  r*,sqlite3**);. 
257d9 20 69 6e 74 20 20 28 2a 6f 70 65 6e 31 36 29 28   int  (*open16)(
257da 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 73 71 6c 69  const void*,sqli
257db 74 65 33 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28  te3**);.  int  (
257dc 2a 70 72 65 70 61 72 65 29 28 73 71 6c 69 74 65  *prepare)(sqlite
257dd 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
257de 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt,sqlite3_stmt*
257df 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b  *,const char**);
257e0 0a 20 20 69 6e 74 20 20 28 2a 70 72 65 70 61 72  .  int  (*prepar
257e1 65 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  e16)(sqlite3*,co
257e2 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71  nst void*,int,sq
257e3 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e  lite3_stmt**,con
257e4 73 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f  st void**);.  vo
257e5 69 64 20 2a 20 28 2a 70 72 6f 66 69 6c 65 29 28  id * (*profile)(
257e6 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29  sqlite3*,void(*)
257e7 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
257e8 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  r*,sqlite_uint64
257e9 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  ),void*);.  void
257ea 20 20 28 2a 70 72 6f 67 72 65 73 73 5f 68 61 6e    (*progress_han
257eb 64 6c 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69  dler)(sqlite3*,i
257ec 6e 74 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29  nt,int(*)(void*)
257ed 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20  ,void*);.  void 
257ee 2a 28 2a 72 65 61 6c 6c 6f 63 29 28 76 6f 69 64  *(*realloc)(void
257ef 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  *,int);.  int  (
257f0 2a 72 65 73 65 74 29 28 73 71 6c 69 74 65 33 5f  *reset)(sqlite3_
257f1 73 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76  stmt*pStmt);.  v
257f2 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 62 6c  oid  (*result_bl
257f3 6f 62 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ob)(sqlite3_cont
257f4 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ext*,const void*
257f5 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69  ,int,void(*)(voi
257f6 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  d*));.  void  (*
257f7 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 29 28 73  result_double)(s
257f8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
257f9 64 6f 75 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20  double);.  void 
257fa 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 29   (*result_error)
257fb 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
257fc 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
257fd 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65  t);.  void  (*re
257fe 73 75 6c 74 5f 65 72 72 6f 72 31 36 29 28 73 71  sult_error16)(sq
257ff 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
25800 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b  onst void*,int);
25801 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c  .  void  (*resul
25802 74 5f 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 63  t_int)(sqlite3_c
25803 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20  ontext*,int);.  
25804 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69  void  (*result_i
25805 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 63 6f  nt64)(sqlite3_co
25806 6e 74 65 78 74 2a 2c 73 71 6c 69 74 65 5f 69 6e  ntext*,sqlite_in
25807 74 36 34 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  t64);.  void  (*
25808 72 65 73 75 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c  result_null)(sql
25809 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
2580a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
2580b 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 63  _text)(sqlite3_c
2580c 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68  ontext*,const ch
2580d 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28  ar*,int,void(*)(
2580e 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20  void*));.  void 
2580f 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36   (*result_text16
25810 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
25811 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  t*,const void*,i
25812 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  nt,void(*)(void*
25813 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65  ));.  void  (*re
25814 73 75 6c 74 5f 74 65 78 74 31 36 62 65 29 28 73  sult_text16be)(s
25815 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
25816 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
25817 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
25818 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c  .  void  (*resul
25819 74 5f 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69  t_text16le)(sqli
2581a 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e  te3_context*,con
2581b 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69  st void*,int,voi
2581c 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
2581d 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 76  void  (*result_v
2581e 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 63 6f  alue)(sqlite3_co
2581f 6e 74 65 78 74 2a 2c 73 71 6c 69 74 65 33 5f 76  ntext*,sqlite3_v
25820 61 6c 75 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  alue*);.  void *
25821 20 28 2a 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b   (*rollback_hook
25822 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28  )(sqlite3*,void(
25823 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29  *)(void*),void*)
25824 3b 0a 20 20 69 6e 74 20 20 28 2a 73 65 74 5f 61  ;.  int  (*set_a
25825 75 74 68 6f 72 69 7a 65 72 29 28 73 71 6c 69 74  uthorizer)(sqlit
25826 65 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a  e3*,int(*)(void*
25827 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
25828 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
25829 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
2582a 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  har*),void*);.  
2582b 76 6f 69 64 20 20 28 2a 73 65 74 5f 61 75 78 64  void  (*set_auxd
2582c 61 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ata)(sqlite3_con
2582d 74 65 78 74 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c  text*,int,void*,
2582e 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29  void (*)(void*))
2582f 3b 0a 20 20 63 68 61 72 20 2a 20 28 2a 73 6e 70  ;.  char * (*snp
25830 72 69 6e 74 66 29 28 69 6e 74 2c 63 68 61 72 2a  rintf)(int,char*
25831 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e  ,const char*,...
25832 29 3b 0a 20 20 69 6e 74 20 20 28 2a 73 74 65 70  );.  int  (*step
25833 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
25834 3b 0a 20 20 69 6e 74 20 20 28 2a 74 61 62 6c 65  ;.  int  (*table
25835 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
25836 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
25837 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
25838 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
25839 68 61 72 20 63 6f 6e 73 74 2a 2a 2c 63 68 61 72  har const**,char
2583a 20 63 6f 6e 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e   const**,int*,in
2583b 74 2a 2c 69 6e 74 2a 29 3b 0a 20 20 76 6f 69 64  t*,int*);.  void
2583c 20 20 28 2a 74 68 72 65 61 64 5f 63 6c 65 61 6e    (*thread_clean
2583d 75 70 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74  up)(void);.  int
2583e 20 20 28 2a 74 6f 74 61 6c 5f 63 68 61 6e 67 65    (*total_change
2583f 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20  s)(sqlite3*);.  
25840 76 6f 69 64 20 2a 20 28 2a 74 72 61 63 65 29 28  void * (*trace)(
25841 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 78  sqlite3*,void(*x
25842 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
25843 73 74 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a 29  st char*),void*)
25844 3b 0a 20 20 69 6e 74 20 20 28 2a 74 72 61 6e 73  ;.  int  (*trans
25845 66 65 72 5f 62 69 6e 64 69 6e 67 73 29 28 73 71  fer_bindings)(sq
25846 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 73 71 6c 69  lite3_stmt*,sqli
25847 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 76 6f  te3_stmt*);.  vo
25848 69 64 20 2a 20 28 2a 75 70 64 61 74 65 5f 68 6f  id * (*update_ho
25849 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69  ok)(sqlite3*,voi
2584a 64 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 20 2c  d(*)(void*,int ,
2584b 63 68 61 72 20 63 6f 6e 73 74 2a 2c 63 68 61 72  char const*,char
2584c 20 63 6f 6e 73 74 2a 2c 73 71 6c 69 74 65 5f 69   const*,sqlite_i
2584d 6e 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  nt64),void*);.  
2584e 76 6f 69 64 20 2a 20 28 2a 75 73 65 72 5f 64 61  void * (*user_da
2584f 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ta)(sqlite3_cont
25850 65 78 74 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76  ext*);.  const v
25851 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 62 6c  oid * (*value_bl
25852 6f 62 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ob)(sqlite3_valu
25853 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61  e*);.  int  (*va
25854 6c 75 65 5f 62 79 74 65 73 29 28 73 71 6c 69 74  lue_bytes)(sqlit
25855 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e  e3_value*);.  in
25856 74 20 20 28 2a 76 61 6c 75 65 5f 62 79 74 65 73  t  (*value_bytes
25857 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  16)(sqlite3_valu
25858 65 2a 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28  e*);.  double  (
25859 2a 76 61 6c 75 65 5f 64 6f 75 62 6c 65 29 28 73  *value_double)(s
2585a 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2585b 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 69    int  (*value_i
2585c 6e 74 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  nt)(sqlite3_valu
2585d 65 2a 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  e*);.  sqlite_in
2585e 74 36 34 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74  t64  (*value_int
2585f 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  64)(sqlite3_valu
25860 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61  e*);.  int  (*va
25861 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
25862 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
25863 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  );.  const unsig
25864 6e 65 64 20 63 68 61 72 20 2a 20 28 2a 76 61 6c  ned char * (*val
25865 75 65 5f 74 65 78 74 29 28 73 71 6c 69 74 65 33  ue_text)(sqlite3
25866 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73  _value*);.  cons
25867 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65  t void * (*value
25868 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74 65 33  _text16)(sqlite3
25869 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73  _value*);.  cons
2586a 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65  t void * (*value
2586b 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c 69 74  _text16be)(sqlit
2586c 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f  e3_value*);.  co
2586d 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c  nst void * (*val
2586e 75 65 5f 74 65 78 74 31 36 6c 65 29 28 73 71 6c  ue_text16le)(sql
2586f 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
25870 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 74 79 70  int  (*value_typ
25871 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e)(sqlite3_value
25872 2a 29 3b 0a 20 20 63 68 61 72 20 2a 28 2a 76 6d  *);.  char *(*vm
25873 70 72 69 6e 74 66 29 28 63 6f 6e 73 74 20 63 68  printf)(const ch
25874 61 72 2a 2c 76 61 5f 6c 69 73 74 29 3b 0a 20 20  ar*,va_list);.  
25875 2f 2a 20 41 64 64 65 64 20 3f 3f 3f 20 2a 2f 0a  /* Added ??? */.
25876 20 20 69 6e 74 20 28 2a 6f 76 65 72 6c 6f 61 64    int (*overload
25877 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74  _function)(sqlit
25878 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  e3*, const char 
25879 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20  *zFuncName, int 
2587a 6e 41 72 67 29 3b 0a 20 20 2f 2a 20 41 64 64 65  nArg);.  /* Adde
2587b 64 20 62 79 20 33 2e 33 2e 31 33 20 2a 2f 0a 20  d by 3.3.13 */. 
2587c 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65 5f 76   int (*prepare_v
2587d 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  2)(sqlite3*,cons
2587e 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c 69  t char*,int,sqli
2587f 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74  te3_stmt**,const
25880 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20   char**);.  int 
25881 28 2a 70 72 65 70 61 72 65 31 36 5f 76 32 29 28  (*prepare16_v2)(
25882 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76  sqlite3*,const v
25883 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  oid*,int,sqlite3
25884 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f  _stmt**,const vo
25885 69 64 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 63  id**);.  int (*c
25886 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 29 28 73  lear_bindings)(s
25887 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20  qlite3_stmt*);. 
25888 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33 2e 34   /* Added by 3.4
25889 2e 31 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 72  .1 */.  int (*cr
2588a 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 29 28  eate_module_v2)(
2588b 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
2588c 68 61 72 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74  har*,const sqlit
2588d 65 33 5f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a  e3_module*,void*
2588e 2c 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79  ,void (*xDestroy
2588f 29 28 76 6f 69 64 20 2a 29 29 3b 0a 20 20 2f 2a  )(void *));.  /*
25890 20 41 64 64 65 64 20 62 79 20 33 2e 35 2e 30 20   Added by 3.5.0 
25891 2a 2f 0a 20 20 69 6e 74 20 28 2a 62 69 6e 64 5f  */.  int (*bind_
25892 7a 65 72 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65  zeroblob)(sqlite
25893 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29  3_stmt*,int,int)
25894 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 62  ;.  int (*blob_b
25895 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f 62 6c  ytes)(sqlite3_bl
25896 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c  ob*);.  int (*bl
25897 6f 62 5f 63 6c 6f 73 65 29 28 73 71 6c 69 74 65  ob_close)(sqlite
25898 33 5f 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74 20  3_blob*);.  int 
25899 28 2a 62 6c 6f 62 5f 6f 70 65 6e 29 28 73 71 6c  (*blob_open)(sql
2589a 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
2589b 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
2589c 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65  nst char*,sqlite
2589d 33 5f 69 6e 74 36 34 2c 69 6e 74 2c 73 71 6c 69  3_int64,int,sqli
2589e 74 65 33 5f 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69  te3_blob**);.  i
2589f 6e 74 20 28 2a 62 6c 6f 62 5f 72 65 61 64 29 28  nt (*blob_read)(
258a0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f  sqlite3_blob*,vo
258a1 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  id*,int,int);.  
258a2 69 6e 74 20 28 2a 62 6c 6f 62 5f 77 72 69 74 65  int (*blob_write
258a3 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c  )(sqlite3_blob*,
258a4 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
258a5 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72  int);.  int (*cr
258a6 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
258a7 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  2)(sqlite3*,cons
258a8 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64  t char*,int,void
258a9 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69  *,int(*)(void*,i
258aa 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
258ab 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c  nt,const void*),
258ac 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
258ad 0a 20 20 69 6e 74 20 28 2a 66 69 6c 65 5f 63 6f  .  int (*file_co
258ae 6e 74 72 6f 6c 29 28 73 71 6c 69 74 65 33 2a 2c  ntrol)(sqlite3*,
258af 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
258b0 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65  void*);.  sqlite
258b1 33 5f 69 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79  3_int64 (*memory
258b2 5f 68 69 67 68 77 61 74 65 72 29 28 69 6e 74 29  _highwater)(int)
258b3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
258b4 34 20 28 2a 6d 65 6d 6f 72 79 5f 75 73 65 64 29  4 (*memory_used)
258b5 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65  (void);.  sqlite
258b6 33 5f 6d 75 74 65 78 20 2a 28 2a 6d 75 74 65 78  3_mutex *(*mutex
258b7 5f 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20  _alloc)(int);.  
258b8 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 65 6e 74  void (*mutex_ent
258b9 65 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  er)(sqlite3_mute
258ba 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75  x*);.  void (*mu
258bb 74 65 78 5f 66 72 65 65 29 28 73 71 6c 69 74 65  tex_free)(sqlite
258bc 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69  3_mutex*);.  voi
258bd 64 20 28 2a 6d 75 74 65 78 5f 6c 65 61 76 65 29  d (*mutex_leave)
258be 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
258bf 3b 0a 20 20 69 6e 74 20 28 2a 6d 75 74 65 78 5f  ;.  int (*mutex_
258c0 74 72 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  try)(sqlite3_mut
258c1 65 78 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 6f 70  ex*);.  int (*op
258c2 65 6e 5f 76 32 29 28 63 6f 6e 73 74 20 63 68 61  en_v2)(const cha
258c3 72 2a 2c 73 71 6c 69 74 65 33 2a 2a 2c 69 6e 74  r*,sqlite3**,int
258c4 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
258c5 20 69 6e 74 20 28 2a 72 65 6c 65 61 73 65 5f 6d   int (*release_m
258c6 65 6d 6f 72 79 29 28 69 6e 74 29 3b 0a 20 20 76  emory)(int);.  v
258c7 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 65 72 72  oid (*result_err
258c8 6f 72 5f 6e 6f 6d 65 6d 29 28 73 71 6c 69 74 65  or_nomem)(sqlite
258c9 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76  3_context*);.  v
258ca 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 65 72 72  oid (*result_err
258cb 6f 72 5f 74 6f 6f 62 69 67 29 28 73 71 6c 69 74  or_toobig)(sqlit
258cc 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20  e3_context*);.  
258cd 69 6e 74 20 28 2a 73 6c 65 65 70 29 28 69 6e 74  int (*sleep)(int
258ce 29 3b 0a 20 20 76 6f 69 64 20 28 2a 73 6f 66 74  );.  void (*soft
258cf 5f 68 65 61 70 5f 6c 69 6d 69 74 29 28 69 6e 74  _heap_limit)(int
258d0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  );.  sqlite3_vfs
258d1 20 2a 28 2a 76 66 73 5f 66 69 6e 64 29 28 63 6f   *(*vfs_find)(co
258d2 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e  nst char*);.  in
258d3 74 20 28 2a 76 66 73 5f 72 65 67 69 73 74 65 72  t (*vfs_register
258d4 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 69  )(sqlite3_vfs*,i
258d5 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 76 66 73  nt);.  int (*vfs
258d6 5f 75 6e 72 65 67 69 73 74 65 72 29 28 73 71 6c  _unregister)(sql
258d7 69 74 65 33 5f 76 66 73 2a 29 3b 0a 20 20 69 6e  ite3_vfs*);.  in
258d8 74 20 28 2a 78 74 68 72 65 61 64 73 61 66 65 29  t (*xthreadsafe)
258d9 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 28  (void);.  void (
258da 2a 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  *result_zeroblob
258db 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
258dc 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  t*,int);.  void 
258dd 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  (*result_error_c
258de 6f 64 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ode)(sqlite3_con
258df 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  text*,int);.  in
258e0 74 20 28 2a 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  t (*test_control
258e1 29 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 20 20 76  )(int, ...);.  v
258e2 6f 69 64 20 28 2a 72 61 6e 64 6f 6d 6e 65 73 73  oid (*randomness
258e3 29 28 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20 20  )(int,void*);.  
258e4 73 71 6c 69 74 65 33 20 2a 28 2a 63 6f 6e 74 65  sqlite3 *(*conte
258e5 78 74 5f 64 62 5f 68 61 6e 64 6c 65 29 28 73 71  xt_db_handle)(sq
258e6 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
258e7 0a 20 20 69 6e 74 20 28 2a 65 78 74 65 6e 64 65  .  int (*extende
258e8 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 29 28  d_result_codes)(
258e9 73 71 6c 69 74 65 33 2a 2c 69 6e 74 29 3b 0a 20  sqlite3*,int);. 
258ea 20 69 6e 74 20 28 2a 6c 69 6d 69 74 29 28 73 71   int (*limit)(sq
258eb 6c 69 74 65 33 2a 2c 69 6e 74 2c 69 6e 74 29 3b  lite3*,int,int);
258ec 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
258ed 2a 28 2a 6e 65 78 74 5f 73 74 6d 74 29 28 73 71  *(*next_stmt)(sq
258ee 6c 69 74 65 33 2a 2c 73 71 6c 69 74 65 33 5f 73  lite3*,sqlite3_s
258ef 74 6d 74 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 63  tmt*);.  const c
258f0 68 61 72 20 2a 28 2a 73 71 6c 29 28 73 71 6c 69  har *(*sql)(sqli
258f1 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e  te3_stmt*);.  in
258f2 74 20 28 2a 73 74 61 74 75 73 29 28 69 6e 74 2c  t (*status)(int,
258f3 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 29 3b 0a  int*,int*,int);.
258f4 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  };../*.** The fo
258f5 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 72  llowing macros r
258f6 65 64 65 66 69 6e 65 20 74 68 65 20 41 50 49 20  edefine the API 
258f7 72 6f 75 74 69 6e 65 73 20 73 6f 20 74 68 61 74  routines so that
258f8 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 72 65 64   they are.** red
258f9 69 72 65 63 74 65 64 20 74 68 72 6f 75 67 68 74  irected throught
258fa 20 74 68 65 20 67 6c 6f 62 61 6c 20 73 71 6c 69   the global sqli
258fb 74 65 33 5f 61 70 69 20 73 74 72 75 63 74 75 72  te3_api structur
258fc 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65  e..**.** This he
258fd 61 64 65 72 20 66 69 6c 65 20 69 73 20 61 6c 73  ader file is als
258fe 6f 20 75 73 65 64 20 62 79 20 74 68 65 20 6c 6f  o used by the lo
258ff 61 64 65 78 74 2e 63 20 73 6f 75 72 63 65 20 66  adext.c source f
25900 69 6c 65 0a 2a 2a 20 28 70 61 72 74 20 6f 66 20  ile.** (part of 
25901 74 68 65 20 6d 61 69 6e 20 53 51 4c 69 74 65 20  the main SQLite 
25902 6c 69 62 72 61 72 79 20 2d 20 6e 6f 74 20 61 6e  library - not an
25903 20 65 78 74 65 6e 73 69 6f 6e 29 20 73 6f 20 74   extension) so t
25904 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e 20 67 65  hat.** it can ge
25905 74 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  t access to the 
25906 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
25907 69 6e 65 73 20 73 74 72 75 63 74 75 72 65 0a 2a  ines structure.*
25908 2a 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 20 42  * definition.  B
25909 75 74 20 74 68 65 20 6d 61 69 6e 20 6c 69 62 72  ut the main libr
2590a 61 72 79 20 64 6f 65 73 20 6e 6f 74 20 77 61 6e  ary does not wan
2590b 74 20 74 6f 20 72 65 64 65 66 69 6e 65 0a 2a 2a  t to redefine.**
2590c 20 74 68 65 20 41 50 49 2e 20 20 53 6f 20 74 68   the API.  So th
2590d 65 20 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 6d  e redefinition m
2590e 61 63 72 6f 73 20 61 72 65 20 6f 6e 6c 79 20 76  acros are only v
2590f 61 6c 69 64 20 69 66 20 74 68 65 0a 2a 2a 20 53  alid if the.** S
25910 51 4c 49 54 45 5f 43 4f 52 45 20 6d 61 63 72 6f  QLITE_CORE macro
25911 73 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  s is undefined..
25912 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
25913 45 5f 43 4f 52 45 0a 23 64 65 66 69 6e 65 20 73  E_CORE.#define s
25914 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
25915 5f 63 6f 6e 74 65 78 74 20 20 20 20 20 20 73 71  _context      sq
25916 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67 72 65  lite3_api->aggre
25917 67 61 74 65 5f 63 6f 6e 74 65 78 74 0a 23 69 66  gate_context.#if
25918 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
25919 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64 65 66  _DEPRECATED.#def
2591a 69 6e 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72  ine sqlite3_aggr
2591b 65 67 61 74 65 5f 63 6f 75 6e 74 20 20 20 20 20  egate_count     
2591c 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2591d 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 0a  aggregate_count.
2591e 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73  #endif.#define s
2591f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
25920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
25921 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
25922 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  blob.#define sql
25923 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65  ite3_bind_double
25924 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25925 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 64 6f  te3_api->bind_do
25926 75 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  uble.#define sql
25927 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 20 20 20  ite3_bind_int   
25928 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25929 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 69 6e  te3_api->bind_in
2592a 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
2592b 33 5f 62 69 6e 64 5f 69 6e 74 36 34 20 20 20 20  3_bind_int64    
2592c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2592d 5f 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 36 34  _api->bind_int64
2592e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2592f 5f 62 69 6e 64 5f 6e 75 6c 6c 20 20 20 20 20 20  _bind_null      
25930 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25931 61 70 69 2d 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23  api->bind_null.#
25932 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
25933 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
25934 75 6e 74 20 20 20 73 71 6c 69 74 65 33 5f 61 70  unt   sqlite3_ap
25935 69 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  i->bind_paramete
25936 72 5f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20  r_count.#define 
25937 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
25938 61 6d 65 74 65 72 5f 69 6e 64 65 78 20 20 20 73  ameter_index   s
25939 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64  qlite3_api->bind
2593a 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
2593b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2593c 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
2593d 6e 61 6d 65 20 20 20 20 73 71 6c 69 74 65 33 5f  name    sqlite3_
2593e 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65  api->bind_parame
2593f 74 65 72 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65  ter_name.#define
25940 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
25941 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  xt              
25942 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e  sqlite3_api->bin
25943 64 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73  d_text.#define s
25944 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
25945 31 36 20 20 20 20 20 20 20 20 20 20 20 20 73 71  16            sq
25946 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
25947 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73  text16.#define s
25948 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
25949 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  e             sq
2594a 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
2594b 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73 71  value.#define sq
2594c 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
2594d 65 72 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  er           sql
2594e 69 74 65 33 5f 61 70 69 2d 3e 62 75 73 79 5f 68  ite3_api->busy_h
2594f 61 6e 64 6c 65 72 0a 23 64 65 66 69 6e 65 20 73  andler.#define s
25950 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
25951 6f 75 74 20 20 20 20 20 20 20 20 20 20 20 73 71  out           sq
25952 6c 69 74 65 33 5f 61 70 69 2d 3e 62 75 73 79 5f  lite3_api->busy_
25953 74 69 6d 65 6f 75 74 0a 23 64 65 66 69 6e 65 20  timeout.#define 
25954 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 20  sqlite3_changes 
25955 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
25956 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 68 61 6e  qlite3_api->chan
25957 67 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ges.#define sqli
25958 74 65 33 5f 63 6c 6f 73 65 20 20 20 20 20 20 20  te3_close       
25959 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2595a 65 33 5f 61 70 69 2d 3e 63 6c 6f 73 65 0a 23 64  e3_api->close.#d
2595b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
2595c 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 20 20  llation_needed  
2595d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2595e 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  ->collation_need
2595f 65 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ed.#define sqlit
25960 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
25961 64 65 64 31 36 20 20 20 20 20 73 71 6c 69 74 65  ded16     sqlite
25962 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e  3_api->collation
25963 5f 6e 65 65 64 65 64 31 36 0a 23 64 65 66 69 6e  _needed16.#defin
25964 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
25965 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
25966 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
25967 6c 75 6d 6e 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  lumn_blob.#defin
25968 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
25969 5f 62 79 74 65 73 20 20 20 20 20 20 20 20 20 20  _bytes          
2596a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
2596b 6c 75 6d 6e 5f 62 79 74 65 73 0a 23 64 65 66 69  lumn_bytes.#defi
2596c 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
2596d 6e 5f 62 79 74 65 73 31 36 20 20 20 20 20 20 20  n_bytes16       
2596e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
2596f 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 0a 23 64  olumn_bytes16.#d
25970 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
25971 6c 75 6d 6e 5f 63 6f 75 6e 74 20 20 20 20 20 20  lumn_count      
25972 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25973 2d 3e 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23  ->column_count.#
25974 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25975 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
25976 61 6d 65 20 20 20 73 71 6c 69 74 65 33 5f 61 70  ame   sqlite3_ap
25977 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  i->column_databa
25978 73 65 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20  se_name.#define 
25979 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
2597a 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 20 73  atabase_name16 s
2597b 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
2597c 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
2597d 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
2597e 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
2597f 70 65 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  pe        sqlite
25980 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65  3_api->column_de
25981 63 6c 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73  cltype.#define s
25982 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
25983 63 6c 74 79 70 65 31 36 20 20 20 20 20 20 73 71  cltype16      sq
25984 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
25985 6e 5f 64 65 63 6c 74 79 70 65 31 36 0a 23 64 65  n_decltype16.#de
25986 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
25987 75 6d 6e 5f 64 6f 75 62 6c 65 20 20 20 20 20 20  umn_double      
25988 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25989 3e 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23  >column_double.#
2598a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
2598b 6f 6c 75 6d 6e 5f 69 6e 74 20 20 20 20 20 20 20  olumn_int       
2598c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2598d 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64  i->column_int.#d
2598e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
2598f 6c 75 6d 6e 5f 69 6e 74 36 34 20 20 20 20 20 20  lumn_int64      
25990 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25991 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23  ->column_int64.#
25992 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25993 6f 6c 75 6d 6e 5f 6e 61 6d 65 20 20 20 20 20 20  olumn_name      
25994 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25995 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23  i->column_name.#
25996 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25997 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20  olumn_name16    
25998 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25999 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  i->column_name16
2599a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2599b 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
2599c 61 6d 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ame     sqlite3_
2599d 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67  api->column_orig
2599e 69 6e 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20  in_name.#define 
2599f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
259a0 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 73  rigin_name16   s
259a1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
259a2 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36  mn_origin_name16
259a3 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
259a4 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
259a5 6d 65 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  me      sqlite3_
259a6 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c  api->column_tabl
259a7 65 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73  e_name.#define s
259a8 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
259a9 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 73 71  ble_name16    sq
259aa 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
259ab 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 0a 23  n_table_name16.#
259ac 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
259ad 6f 6c 75 6d 6e 5f 74 65 78 74 20 20 20 20 20 20  olumn_text      
259ae 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
259af 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23  i->column_text.#
259b0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
259b1 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 20 20 20 20  olumn_text16    
259b2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
259b3 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  i->column_text16
259b4 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
259b5 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 20 20 20 20  _column_type    
259b6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
259b7 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65  api->column_type
259b8 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
259b9 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 20 20 20  _column_value   
259ba 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
259bb 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75  api->column_valu
259bc 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
259bd 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20  3_commit_hook   
259be 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
259bf 5f 61 70 69 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f  _api->commit_hoo
259c0 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  k.#define sqlite
259c1 33 5f 63 6f 6d 70 6c 65 74 65 20 20 20 20 20 20  3_complete      
259c2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
259c3 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23  _api->complete.#
259c4 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
259c5 6f 6d 70 6c 65 74 65 31 36 20 20 20 20 20 20 20  omplete16       
259c6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
259c7 69 2d 3e 63 6f 6d 70 6c 65 74 65 31 36 0a 23 64  i->complete16.#d
259c8 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
259c9 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20  eate_collation  
259ca 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
259cb 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  ->create_collati
259cc 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  on.#define sqlit
259cd 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
259ce 69 6f 6e 31 36 20 20 20 20 20 73 71 6c 69 74 65  ion16     sqlite
259cf 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f  3_api->create_co
259d0 6c 6c 61 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e  llation16.#defin
259d1 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
259d2 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 20  _function       
259d3 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
259d4 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 0a 23 64  eate_function.#d
259d5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
259d6 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20  eate_function16 
259d7 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
259d8 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  ->create_functio
259d9 6e 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  n16.#define sqli
259da 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
259db 65 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  e          sqlit
259dc 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d  e3_api->create_m
259dd 6f 64 75 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  odule.#define sq
259de 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
259df 75 6c 65 5f 76 32 20 20 20 20 20 20 20 73 71 6c  ule_v2       sql
259e0 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65  ite3_api->create
259e1 5f 6d 6f 64 75 6c 65 5f 76 32 0a 23 64 65 66 69  _module_v2.#defi
259e2 6e 65 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f  ne sqlite3_data_
259e3 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20  count           
259e4 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64    sqlite3_api->d
259e5 61 74 61 5f 63 6f 75 6e 74 0a 23 64 65 66 69 6e  ata_count.#defin
259e6 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e  e sqlite3_db_han
259e7 64 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20  dle             
259e8 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 62   sqlite3_api->db
259e9 5f 68 61 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20  _handle.#define 
259ea 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
259eb 76 74 61 62 20 20 20 20 20 20 20 20 20 20 20 73  vtab           s
259ec 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 65 63 6c  qlite3_api->decl
259ed 61 72 65 5f 76 74 61 62 0a 23 64 65 66 69 6e 65  are_vtab.#define
259ee 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
259ef 73 68 61 72 65 64 5f 63 61 63 68 65 20 20 20 20  shared_cache    
259f0 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 6e 61  sqlite3_api->ena
259f1 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
259f2 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
259f3 5f 65 72 72 63 6f 64 65 20 20 20 20 20 20 20 20  _errcode        
259f4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
259f5 61 70 69 2d 3e 65 72 72 63 6f 64 65 0a 23 64 65  api->errcode.#de
259f6 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
259f7 6d 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20  msg             
259f8 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
259f9 3e 65 72 72 6d 73 67 0a 23 64 65 66 69 6e 65 20  >errmsg.#define 
259fa 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
259fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
259fc 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 6d  qlite3_api->errm
259fd 73 67 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  sg16.#define sql
259fe 69 74 65 33 5f 65 78 65 63 20 20 20 20 20 20 20  ite3_exec       
259ff 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25a00 74 65 33 5f 61 70 69 2d 3e 65 78 65 63 0a 23 69  te3_api->exec.#i
25a01 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25a02 54 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64 65  T_DEPRECATED.#de
25a03 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 78 70  fine sqlite3_exp
25a04 69 72 65 64 20 20 20 20 20 20 20 20 20 20 20 20  ired            
25a05 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25a06 3e 65 78 70 69 72 65 64 0a 23 65 6e 64 69 66 0a  >expired.#endif.
25a07 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25a08 66 69 6e 61 6c 69 7a 65 20 20 20 20 20 20 20 20  finalize        
25a09 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25a0a 70 69 2d 3e 66 69 6e 61 6c 69 7a 65 0a 23 64 65  pi->finalize.#de
25a0b 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65  fine sqlite3_fre
25a0c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
25a0d 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25a0e 3e 66 72 65 65 0a 23 64 65 66 69 6e 65 20 73 71  >free.#define sq
25a0f 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
25a10 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
25a11 69 74 65 33 5f 61 70 69 2d 3e 66 72 65 65 5f 74  ite3_api->free_t
25a12 61 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  able.#define sql
25a13 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d  ite3_get_autocom
25a14 6d 69 74 20 20 20 20 20 20 20 20 20 73 71 6c 69  mit         sqli
25a15 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 74  te3_api->get_aut
25a16 6f 63 6f 6d 6d 69 74 0a 23 64 65 66 69 6e 65 20  ocommit.#define 
25a17 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
25a18 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 73  ata            s
25a19 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f  qlite3_api->get_
25a1a 61 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20  auxdata.#define 
25a1b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
25a1c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  e              s
25a1d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f  qlite3_api->get_
25a1e 74 61 62 6c 65 0a 23 69 66 6e 64 65 66 20 53 51  table.#ifndef SQ
25a1f 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
25a20 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c  ATED.#define sql
25a21 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f  ite3_global_reco
25a22 76 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69  ver         sqli
25a23 74 65 33 5f 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f  te3_api->global_
25a24 72 65 63 6f 76 65 72 0a 23 65 6e 64 69 66 0a 23  recover.#endif.#
25a25 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 69  define sqlite3_i
25a26 6e 74 65 72 72 75 70 74 20 20 20 20 20 20 20 20  nterrupt        
25a27 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25a28 69 2d 3e 69 6e 74 65 72 72 75 70 74 78 0a 23 64  i->interruptx.#d
25a29 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 61  efine sqlite3_la
25a2a 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 20  st_insert_rowid 
25a2b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25a2c 2d 3e 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  ->last_insert_ro
25a2d 77 69 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  wid.#define sqli
25a2e 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 20 20  te3_libversion  
25a2f 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25a30 65 33 5f 61 70 69 2d 3e 6c 69 62 76 65 72 73 69  e3_api->libversi
25a31 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  on.#define sqlit
25a32 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
25a33 6d 62 65 72 20 20 20 20 20 20 73 71 6c 69 74 65  mber      sqlite
25a34 33 5f 61 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f  3_api->libversio
25a35 6e 5f 6e 75 6d 62 65 72 0a 23 64 65 66 69 6e 65  n_number.#define
25a36 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20   sqlite3_malloc 
25a37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a38 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 61 6c  sqlite3_api->mal
25a39 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  loc.#define sqli
25a3a 74 65 33 5f 6d 70 72 69 6e 74 66 20 20 20 20 20  te3_mprintf     
25a3b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25a3c 65 33 5f 61 70 69 2d 3e 6d 70 72 69 6e 74 66 0a  e3_api->mprintf.
25a3d 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25a3e 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20  open            
25a3f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25a40 70 69 2d 3e 6f 70 65 6e 0a 23 64 65 66 69 6e 65  pi->open.#define
25a41 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20   sqlite3_open16 
25a42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a43 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65  sqlite3_api->ope
25a44 6e 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  n16.#define sqli
25a45 74 65 33 5f 70 72 65 70 61 72 65 20 20 20 20 20  te3_prepare     
25a46 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25a47 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 0a  e3_api->prepare.
25a48 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25a49 70 72 65 70 61 72 65 31 36 20 20 20 20 20 20 20  prepare16       
25a4a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25a4b 70 69 2d 3e 70 72 65 70 61 72 65 31 36 0a 23 64  pi->prepare16.#d
25a4c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
25a4d 65 70 61 72 65 5f 76 32 20 20 20 20 20 20 20 20  epare_v2        
25a4e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25a4f 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23 64 65  ->prepare_v2.#de
25a50 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
25a51 70 61 72 65 31 36 5f 76 32 20 20 20 20 20 20 20  pare16_v2       
25a52 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25a53 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a 23 64  >prepare16_v2.#d
25a54 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
25a55 6f 66 69 6c 65 20 20 20 20 20 20 20 20 20 20 20  ofile           
25a56 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25a57 2d 3e 70 72 6f 66 69 6c 65 0a 23 64 65 66 69 6e  ->profile.#defin
25a58 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  e sqlite3_progre
25a59 73 73 5f 68 61 6e 64 6c 65 72 20 20 20 20 20 20  ss_handler      
25a5a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
25a5b 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 0a 23  ogress_handler.#
25a5c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
25a5d 65 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20  ealloc          
25a5e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25a5f 69 2d 3e 72 65 61 6c 6c 6f 63 0a 23 64 65 66 69  i->realloc.#defi
25a60 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  ne sqlite3_reset
25a61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a62 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
25a63 65 73 65 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  eset.#define sql
25a64 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
25a65 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
25a66 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
25a67 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  blob.#define sql
25a68 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
25a69 6c 65 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  le          sqli
25a6a 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
25a6b 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65 20 73  double.#define s
25a6c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
25a6d 72 6f 72 20 20 20 20 20 20 20 20 20 20 20 73 71  ror           sq
25a6e 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
25a6f 74 5f 65 72 72 6f 72 0a 23 64 65 66 69 6e 65 20  t_error.#define 
25a70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
25a71 72 72 6f 72 31 36 20 20 20 20 20 20 20 20 20 73  rror16         s
25a72 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
25a73 6c 74 5f 65 72 72 6f 72 31 36 0a 23 64 65 66 69  lt_error16.#defi
25a74 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
25a75 74 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20  t_int           
25a76 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
25a77 65 73 75 6c 74 5f 69 6e 74 0a 23 64 65 66 69 6e  esult_int.#defin
25a78 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
25a79 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _int64          
25a7a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
25a7b 73 75 6c 74 5f 69 6e 74 36 34 0a 23 64 65 66 69  sult_int64.#defi
25a7c 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
25a7d 74 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  t_null          
25a7e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
25a7f 65 73 75 6c 74 5f 6e 75 6c 6c 0a 23 64 65 66 69  esult_null.#defi
25a80 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
25a81 74 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20  t_text          
25a82 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
25a83 65 73 75 6c 74 5f 74 65 78 74 0a 23 64 65 66 69  esult_text.#defi
25a84 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
25a85 74 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  t_text16        
25a86 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
25a87 65 73 75 6c 74 5f 74 65 78 74 31 36 0a 23 64 65  esult_text16.#de
25a88 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
25a89 75 6c 74 5f 74 65 78 74 31 36 62 65 20 20 20 20  ult_text16be    
25a8a 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25a8b 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  >result_text16be
25a8c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25a8d 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
25a8e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25a8f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74  api->result_text
25a90 31 36 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  16le.#define sql
25a91 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
25a92 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  e           sqli
25a93 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
25a94 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73 71  value.#define sq
25a95 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68  lite3_rollback_h
25a96 6f 6f 6b 20 20 20 20 20 20 20 20 20 20 73 71 6c  ook          sql
25a97 69 74 65 33 5f 61 70 69 2d 3e 72 6f 6c 6c 62 61  ite3_api->rollba
25a98 63 6b 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20  ck_hook.#define 
25a99 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
25a9a 6f 72 69 7a 65 72 20 20 20 20 20 20 20 20 20 73  orizer         s
25a9b 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f  qlite3_api->set_
25a9c 61 75 74 68 6f 72 69 7a 65 72 0a 23 64 65 66 69  authorizer.#defi
25a9d 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  ne sqlite3_set_a
25a9e 75 78 64 61 74 61 20 20 20 20 20 20 20 20 20 20  uxdata          
25a9f 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73    sqlite3_api->s
25aa0 65 74 5f 61 75 78 64 61 74 61 0a 23 64 65 66 69  et_auxdata.#defi
25aa1 6e 65 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  ne sqlite3_snpri
25aa2 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20 20  ntf             
25aa3 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73    sqlite3_api->s
25aa4 6e 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20  nprintf.#define 
25aa5 73 71 6c 69 74 65 33 5f 73 74 65 70 20 20 20 20  sqlite3_step    
25aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
25aa7 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 74 65 70  qlite3_api->step
25aa8 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25aa9 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65  _table_column_me
25aaa 74 61 64 61 74 61 20 20 73 71 6c 69 74 65 33 5f  tadata  sqlite3_
25aab 61 70 69 2d 3e 74 61 62 6c 65 5f 63 6f 6c 75 6d  api->table_colum
25aac 6e 5f 6d 65 74 61 64 61 74 61 0a 23 64 65 66 69  n_metadata.#defi
25aad 6e 65 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  ne sqlite3_threa
25aae 64 5f 63 6c 65 61 6e 75 70 20 20 20 20 20 20 20  d_cleanup       
25aaf 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74    sqlite3_api->t
25ab0 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 0a 23 64  hread_cleanup.#d
25ab1 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 6f  efine sqlite3_to
25ab2 74 61 6c 5f 63 68 61 6e 67 65 73 20 20 20 20 20  tal_changes     
25ab3 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25ab4 2d 3e 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 0a  ->total_changes.
25ab5 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25ab6 74 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20  trace           
25ab7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25ab8 70 69 2d 3e 74 72 61 63 65 0a 23 69 66 6e 64 65  pi->trace.#ifnde
25ab9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
25aba 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
25abb 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65   sqlite3_transfe
25abc 72 5f 62 69 6e 64 69 6e 67 73 20 20 20 20 20 20  r_bindings      
25abd 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 72 61  sqlite3_api->tra
25abe 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 0a 23  nsfer_bindings.#
25abf 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71  endif.#define sq
25ac0 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
25ac1 6b 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  k            sql
25ac2 69 74 65 33 5f 61 70 69 2d 3e 75 70 64 61 74 65  ite3_api->update
25ac3 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71  _hook.#define sq
25ac4 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 20  lite3_user_data 
25ac5 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
25ac6 69 74 65 33 5f 61 70 69 2d 3e 75 73 65 72 5f 64  ite3_api->user_d
25ac7 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
25ac8 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 20  te3_value_blob  
25ac9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25aca 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 62 6c  e3_api->value_bl
25acb 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ob.#define sqlit
25acc 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 20 20  e3_value_bytes  
25acd 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25ace 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 62 79 74  3_api->value_byt
25acf 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  es.#define sqlit
25ad0 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36  e3_value_bytes16
25ad1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25ad2 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 62 79 74  3_api->value_byt
25ad3 65 73 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  es16.#define sql
25ad4 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
25ad5 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  e           sqli
25ad6 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 64  te3_api->value_d
25ad7 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  ouble.#define sq
25ad8 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20  lite3_value_int 
25ad9 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
25ada 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
25adb 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  int.#define sqli
25adc 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 20  te3_value_int64 
25add 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25ade 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e  e3_api->value_in
25adf 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t64.#define sqli
25ae0 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69  te3_value_numeri
25ae1 63 5f 74 79 70 65 20 20 20 20 20 73 71 6c 69 74  c_type     sqlit
25ae2 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 6e 75  e3_api->value_nu
25ae3 6d 65 72 69 63 5f 74 79 70 65 0a 23 64 65 66 69  meric_type.#defi
25ae4 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
25ae5 5f 74 65 78 74 20 20 20 20 20 20 20 20 20 20 20  _text           
25ae6 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
25ae7 61 6c 75 65 5f 74 65 78 74 0a 23 64 65 66 69 6e  alue_text.#defin
25ae8 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
25ae9 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20  text16          
25aea 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
25aeb 6c 75 65 5f 74 65 78 74 31 36 0a 23 64 65 66 69  lue_text16.#defi
25aec 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
25aed 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20  _text16be       
25aee 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
25aef 61 6c 75 65 5f 74 65 78 74 31 36 62 65 0a 23 64  alue_text16be.#d
25af0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
25af1 6c 75 65 5f 74 65 78 74 31 36 6c 65 20 20 20 20  lue_text16le    
25af2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25af3 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  ->value_text16le
25af4 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25af5 5f 76 61 6c 75 65 5f 74 79 70 65 20 20 20 20 20  _value_type     
25af6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
25af7 61 70 69 2d 3e 76 61 6c 75 65 5f 74 79 70 65 0a  api->value_type.
25af8 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25af9 76 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20 20  vmprintf        
25afa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
25afb 70 69 2d 3e 76 6d 70 72 69 6e 74 66 0a 23 64 65  pi->vmprintf.#de
25afc 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 76 65  fine sqlite3_ove
25afd 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 20 20  rload_function  
25afe 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25aff 3e 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  >overload_functi
25b00 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  on.#define sqlit
25b01 65 33 5f 70 72 65 70 61 72 65 5f 76 32 20 20 20  e3_prepare_v2   
25b02 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25b03 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 5f 76  3_api->prepare_v
25b04 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  2.#define sqlite
25b05 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20 20  3_prepare16_v2  
25b06 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25b07 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31 36 5f  _api->prepare16_
25b08 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  v2.#define sqlit
25b09 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67  e3_clear_binding
25b0a 73 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  s         sqlite
25b0b 33 5f 61 70 69 2d 3e 63 6c 65 61 72 5f 62 69 6e  3_api->clear_bin
25b0c 64 69 6e 67 73 0a 23 64 65 66 69 6e 65 20 73 71  dings.#define sq
25b0d 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
25b0e 6c 6f 62 20 20 20 20 20 20 20 20 20 20 73 71 6c  lob          sql
25b0f 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 7a  ite3_api->bind_z
25b10 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20  eroblob.#define 
25b11 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
25b12 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20 73  es             s
25b13 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62  qlite3_api->blob
25b14 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73  _bytes.#define s
25b15 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
25b16 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  e             sq
25b17 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f  lite3_api->blob_
25b18 63 6c 6f 73 65 0a 23 64 65 66 69 6e 65 20 73 71  close.#define sq
25b19 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20  lite3_blob_open 
25b1a 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
25b1b 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 6f  ite3_api->blob_o
25b1c 70 65 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  pen.#define sqli
25b1d 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 20 20 20  te3_blob_read   
25b1e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25b1f 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 72 65 61  e3_api->blob_rea
25b20 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  d.#define sqlite
25b21 33 5f 62 6c 6f 62 5f 77 72 69 74 65 20 20 20 20  3_blob_write    
25b22 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
25b23 5f 61 70 69 2d 3e 62 6c 6f 62 5f 77 72 69 74 65  _api->blob_write
25b24 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25b25 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
25b26 6e 5f 76 32 20 20 20 20 73 71 6c 69 74 65 33 5f  n_v2    sqlite3_
25b27 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c  api->create_coll
25b28 61 74 69 6f 6e 5f 76 32 0a 23 64 65 66 69 6e 65  ation_v2.#define
25b29 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f   sqlite3_file_co
25b2a 6e 74 72 6f 6c 20 20 20 20 20 20 20 20 20 20 20  ntrol           
25b2b 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 69 6c  sqlite3_api->fil
25b2c 65 5f 63 6f 6e 74 72 6f 6c 0a 23 64 65 66 69 6e  e_control.#defin
25b2d 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  e sqlite3_memory
25b2e 5f 68 69 67 68 77 61 74 65 72 20 20 20 20 20 20  _highwater      
25b2f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 65   sqlite3_api->me
25b30 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 0a 23  mory_highwater.#
25b31 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
25b32 65 6d 6f 72 79 5f 75 73 65 64 20 20 20 20 20 20  emory_used      
25b33 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25b34 69 2d 3e 6d 65 6d 6f 72 79 5f 75 73 65 64 0a 23  i->memory_used.#
25b35 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
25b36 75 74 65 78 5f 61 6c 6c 6f 63 20 20 20 20 20 20  utex_alloc      
25b37 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25b38 69 2d 3e 6d 75 74 65 78 5f 61 6c 6c 6f 63 0a 23  i->mutex_alloc.#
25b39 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
25b3a 75 74 65 78 5f 65 6e 74 65 72 20 20 20 20 20 20  utex_enter      
25b3b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25b3c 69 2d 3e 6d 75 74 65 78 5f 65 6e 74 65 72 0a 23  i->mutex_enter.#
25b3d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
25b3e 75 74 65 78 5f 66 72 65 65 20 20 20 20 20 20 20  utex_free       
25b3f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
25b40 69 2d 3e 6d 75 74 65 78 5f 66 72 65 65 0a 23 64  i->mutex_free.#d
25b41 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
25b42 74 65 78 5f 6c 65 61 76 65 20 20 20 20 20 20 20  tex_leave       
25b43 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25b44 2d 3e 6d 75 74 65 78 5f 6c 65 61 76 65 0a 23 64  ->mutex_leave.#d
25b45 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75  efine sqlite3_mu
25b46 74 65 78 5f 74 72 79 20 20 20 20 20 20 20 20 20  tex_try         
25b47 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
25b48 2d 3e 6d 75 74 65 78 5f 74 72 79 0a 23 64 65 66  ->mutex_try.#def
25b49 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ine sqlite3_open
25b4a 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 20 20  _v2             
25b4b 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
25b4c 6f 70 65 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20  open_v2.#define 
25b4d 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
25b4e 6d 65 6d 6f 72 79 20 20 20 20 20 20 20 20 20 73  memory         s
25b4f 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 6c 65  qlite3_api->rele
25b50 61 73 65 5f 6d 65 6d 6f 72 79 0a 23 64 65 66 69  ase_memory.#defi
25b51 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
25b52 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 20 20 20  t_error_nomem   
25b53 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
25b54 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
25b55 6d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  m.#define sqlite
25b56 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
25b57 6f 6f 62 69 67 20 20 20 20 73 71 6c 69 74 65 33  oobig    sqlite3
25b58 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72  _api->result_err
25b59 6f 72 5f 74 6f 6f 62 69 67 0a 23 64 65 66 69 6e  or_toobig.#defin
25b5a 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 20  e sqlite3_sleep 
25b5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b5c 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6c   sqlite3_api->sl
25b5d 65 65 70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  eep.#define sqli
25b5e 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69  te3_soft_heap_li
25b5f 6d 69 74 20 20 20 20 20 20 20 20 73 71 6c 69 74  mit        sqlit
25b60 65 33 5f 61 70 69 2d 3e 73 6f 66 74 5f 68 65 61  e3_api->soft_hea
25b61 70 5f 6c 69 6d 69 74 0a 23 64 65 66 69 6e 65 20  p_limit.#define 
25b62 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64  sqlite3_vfs_find
25b63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
25b64 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 66 73 5f  qlite3_api->vfs_
25b65 66 69 6e 64 0a 23 64 65 66 69 6e 65 20 73 71 6c  find.#define sql
25b66 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65  ite3_vfs_registe
25b67 72 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  r           sqli
25b68 74 65 33 5f 61 70 69 2d 3e 76 66 73 5f 72 65 67  te3_api->vfs_reg
25b69 69 73 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71  ister.#define sq
25b6a 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
25b6b 73 74 65 72 20 20 20 20 20 20 20 20 20 73 71 6c  ster         sql
25b6c 69 74 65 33 5f 61 70 69 2d 3e 76 66 73 5f 75 6e  ite3_api->vfs_un
25b6d 72 65 67 69 73 74 65 72 0a 23 64 65 66 69 6e 65  register.#define
25b6e 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73   sqlite3_threads
25b6f 61 66 65 20 20 20 20 20 20 20 20 20 20 20 20 20  afe             
25b70 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 78 74 68  sqlite3_api->xth
25b71 72 65 61 64 73 61 66 65 0a 23 64 65 66 69 6e 65  readsafe.#define
25b72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25b73 7a 65 72 6f 62 6c 6f 62 20 20 20 20 20 20 20 20  zeroblob        
25b74 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73  sqlite3_api->res
25b75 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65  ult_zeroblob.#de
25b76 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
25b77 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 20 20  ult_error_code  
25b78 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25b79 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  >result_error_co
25b7a 64 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  de.#define sqlit
25b7b 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 20  e3_test_control 
25b7c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25b7d 33 5f 61 70 69 2d 3e 74 65 73 74 5f 63 6f 6e 74  3_api->test_cont
25b7e 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  rol.#define sqli
25b7f 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 20 20  te3_randomness  
25b80 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
25b81 65 33 5f 61 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65  e3_api->randomne
25b82 73 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ss.#define sqlit
25b83 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
25b84 6e 64 6c 65 20 20 20 20 20 20 73 71 6c 69 74 65  ndle      sqlite
25b85 33 5f 61 70 69 2d 3e 63 6f 6e 74 65 78 74 5f 64  3_api->context_d
25b86 62 5f 68 61 6e 64 6c 65 0a 23 64 65 66 69 6e 65  b_handle.#define
25b87 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
25b88 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 20 20  d_result_codes  
25b89 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 74  sqlite3_api->ext
25b8a 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64  ended_result_cod
25b8b 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  es.#define sqlit
25b8c 65 33 5f 6c 69 6d 69 74 20 20 20 20 20 20 20 20  e3_limit        
25b8d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25b8e 33 5f 61 70 69 2d 3e 6c 69 6d 69 74 0a 23 64 65  3_api->limit.#de
25b8f 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6e 65 78  fine sqlite3_nex
25b90 74 5f 73 74 6d 74 20 20 20 20 20 20 20 20 20 20  t_stmt          
25b91 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
25b92 3e 6e 65 78 74 5f 73 74 6d 74 0a 23 64 65 66 69  >next_stmt.#defi
25b93 6e 65 20 73 71 6c 69 74 65 33 5f 73 71 6c 20 20  ne sqlite3_sql  
25b94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b95 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73    sqlite3_api->s
25b96 71 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ql.#define sqlit
25b97 65 33 5f 73 74 61 74 75 73 20 20 20 20 20 20 20  e3_status       
25b98 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25b99 33 5f 61 70 69 2d 3e 73 74 61 74 75 73 0a 23 65  3_api->status.#e
25b9a 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 43  ndif /* SQLITE_C
25b9b 4f 52 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20  ORE */..#define 
25b9c 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e  SQLITE_EXTENSION
25b9d 5f 49 4e 49 54 31 20 20 20 20 20 63 6f 6e 73 74  _INIT1     const
25b9e 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
25b9f 74 69 6e 65 73 20 2a 73 71 6c 69 74 65 33 5f 61  tines *sqlite3_a
25ba0 70 69 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  pi = 0;.#define 
25ba1 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e  SQLITE_EXTENSION
25ba2 5f 49 4e 49 54 32 28 76 29 20 20 73 71 6c 69 74  _INIT2(v)  sqlit
25ba3 65 33 5f 61 70 69 20 3d 20 76 3b 0a 0a 23 65 6e  e3_api = v;..#en
25ba4 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 33 45  dif /* _SQLITE3E
25ba5 58 54 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  XT_H_ */../*****
25ba6 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
25ba7 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2a   sqlite3ext.h **
25ba8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25ba9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25baa 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
25bab 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
25bac 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
25bad 66 74 20 6f 66 66 20 69 6e 20 6c 6f 61 64 65 78  ft off in loadex
25bae 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
25baf 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64  ********/..#ifnd
25bb0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
25bb1 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 0a 2f  OAD_EXTENSION../
25bb2 2a 0a 2a 2a 20 53 6f 6d 65 20 41 50 49 20 72 6f  *.** Some API ro
25bb3 75 74 69 6e 65 73 20 61 72 65 20 6f 6d 69 74 74  utines are omitt
25bb4 65 64 20 77 68 65 6e 20 76 61 72 69 6f 75 73 20  ed when various 
25bb5 66 65 61 74 75 72 65 73 20 61 72 65 0a 2a 2a 20  features are.** 
25bb6 65 78 63 6c 75 64 65 64 20 66 72 6f 6d 20 61 20  excluded from a 
25bb7 62 75 69 6c 64 20 6f 66 20 53 51 4c 69 74 65 2e  build of SQLite.
25bb8 20 20 53 75 62 73 74 69 74 75 74 65 20 61 20 4e    Substitute a N
25bb9 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 66  ULL pointer.** f
25bba 6f 72 20 61 6e 79 20 6d 69 73 73 69 6e 67 20 41  or any missing A
25bbb 50 49 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  PIs..*/.#ifndef 
25bbc 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
25bbd 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20  LUMN_METADATA.# 
25bbe 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25bbf 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
25bc0 61 6d 65 20 20 20 30 0a 23 20 64 65 66 69 6e 65  ame   0.# define
25bc1 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25bc2 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 20  database_name16 
25bc3 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
25bc4 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
25bc5 6e 61 6d 65 20 20 20 20 20 20 30 0a 23 20 64 65  name      0.# de
25bc6 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
25bc7 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36  umn_table_name16
25bc8 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
25bc9 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
25bca 69 67 69 6e 5f 6e 61 6d 65 20 20 20 20 20 30 0a  igin_name     0.
25bcb 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25bcc 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
25bcd 61 6d 65 31 36 20 20 20 30 0a 23 20 64 65 66 69  ame16   0.# defi
25bce 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65  ne sqlite3_table
25bcf 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
25bd0 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    0.#endif..#ifd
25bd1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
25bd2 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 23 20 64  UTHORIZATION.# d
25bd3 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 65  efine sqlite3_se
25bd4 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 20 20 20  t_authorizer    
25bd5 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23       0.#endif..#
25bd6 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
25bd7 54 5f 55 54 46 31 36 0a 23 20 64 65 66 69 6e 65  T_UTF16.# define
25bd8 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
25bd9 78 74 31 36 20 20 20 20 20 20 20 20 20 20 20 20  xt16            
25bda 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
25bdb 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
25bdc 64 65 64 31 36 20 20 20 20 20 30 0a 23 20 64 65  ded16     0.# de
25bdd 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
25bde 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20  umn_decltype16  
25bdf 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
25be0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
25be1 6d 65 31 36 20 20 20 20 20 20 20 20 20 20 30 0a  me16          0.
25be2 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25be3 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 20 20  _column_text16  
25be4 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
25be5 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
25be6 65 74 65 31 36 20 20 20 20 20 20 20 20 20 20 20  ete16           
25be7 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
25be8 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
25be9 61 74 69 6f 6e 31 36 20 20 20 20 20 30 0a 23 20  ation16     0.# 
25bea 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
25beb 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
25bec 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
25bed 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31   sqlite3_errmsg1
25bee 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  6               
25bef 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
25bf0 65 33 5f 6f 70 65 6e 31 36 20 20 20 20 20 20 20  e3_open16       
25bf1 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
25bf2 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
25bf3 70 61 72 65 31 36 20 20 20 20 20 20 20 20 20 20  pare16          
25bf4 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
25bf5 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
25bf6 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 30 0a  _v2           0.
25bf7 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25bf8 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 20  _result_error16 
25bf9 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
25bfa 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
25bfb 74 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  t_text16        
25bfc 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
25bfd 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
25bfe 31 36 62 65 20 20 20 20 20 20 20 20 30 0a 23 20  16be        0.# 
25bff 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
25c00 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20  esult_text16le  
25c01 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
25c02 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25c03 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20 20  ext16           
25c04 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
25c05 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62  e3_value_text16b
25c06 65 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65  e         0.# de
25c07 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
25c08 75 65 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20  ue_text16le     
25c09 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
25c0a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61  qlite3_column_da
25c0b 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a  tabase_name16 0.
25c0c 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25c0d 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
25c0e 6d 65 31 36 20 20 20 20 30 0a 23 20 64 65 66 69  me16    0.# defi
25c0f 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
25c10 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20  n_origin_name16 
25c11 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    0.#endif..#ifd
25c12 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
25c13 4f 4d 50 4c 45 54 45 0a 23 20 64 65 66 69 6e 65  OMPLETE.# define
25c14 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
25c15 65 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  e 0.# define sql
25c16 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20  ite3_complete16 
25c17 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  0.#endif..#ifdef
25c18 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f   SQLITE_OMIT_PRO
25c19 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 23  GRESS_CALLBACK.#
25c1a 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
25c1b 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72  progress_handler
25c1c 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65   0.#endif..#ifde
25c1d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
25c1e 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 64 65 66  RTUALTABLE.# def
25c1f 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ine sqlite3_crea
25c20 74 65 5f 6d 6f 64 75 6c 65 20 30 0a 23 20 64 65  te_module 0.# de
25c21 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
25c22 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 20 30 0a  ate_module_v2 0.
25c23 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25c24 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 20 30 0a  _declare_vtab 0.
25c25 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
25c26 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
25c27 44 5f 43 41 43 48 45 0a 23 20 64 65 66 69 6e 65  D_CACHE.# define
25c28 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
25c29 73 68 61 72 65 64 5f 63 61 63 68 65 20 30 0a 23  shared_cache 0.#
25c2a 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
25c2b 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a  LITE_OMIT_TRACE.
25c2c 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
25c2d 5f 70 72 6f 66 69 6c 65 20 20 20 20 20 20 20 30  _profile       0
25c2e 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
25c2f 33 5f 74 72 61 63 65 20 20 20 20 20 20 20 20 20  3_trace         
25c30 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  0.#endif..#ifdef
25c31 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54   SQLITE_OMIT_GET
25c32 5f 54 41 42 4c 45 0a 23 20 64 65 66 69 6e 65 20  _TABLE.# define 
25c33 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
25c34 6c 65 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  le    0.# define
25c35 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
25c36 6c 65 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a  le     0.#endif.
25c37 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
25c38 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 23 64 65  MIT_INCRBLOB.#de
25c39 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
25c3a 64 5f 7a 65 72 6f 62 6c 6f 62 20 20 30 0a 23 64  d_zeroblob  0.#d
25c3b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
25c3c 6f 62 5f 62 79 74 65 73 20 20 20 20 20 30 0a 23  ob_bytes     0.#
25c3d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
25c3e 6c 6f 62 5f 63 6c 6f 73 65 20 20 20 20 20 30 0a  lob_close     0.
25c3f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
25c40 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20 20 30  blob_open      0
25c41 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
25c42 5f 62 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20  _blob_read      
25c43 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  0.#define sqlite
25c44 33 5f 62 6c 6f 62 5f 77 72 69 74 65 20 20 20 20  3_blob_write    
25c45 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
25c46 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
25c47 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
25c48 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c  s pointers to al
25c49 6c 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75  l SQLite API rou
25c4a 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 70 6f 69 6e  tines..** A poin
25c4b 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75  ter to this stru
25c4c 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20  cture is passed 
25c4d 69 6e 74 6f 20 65 78 74 65 6e 73 69 6f 6e 73 20  into extensions 
25c4e 77 68 65 6e 20 74 68 65 79 20 61 72 65 0a 2a 2a  when they are.**
25c4f 20 6c 6f 61 64 65 64 20 73 6f 20 74 68 61 74 20   loaded so that 
25c50 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 63 61  the extension ca
25c51 6e 20 6d 61 6b 65 20 63 61 6c 6c 73 20 62 61 63  n make calls bac
25c52 6b 20 69 6e 74 6f 20 74 68 65 20 53 51 4c 69 74  k into the SQLit
25c53 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2a  e.** library..**
25c54 0a 2a 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20  .** When adding 
25c55 6e 65 77 20 41 50 49 73 2c 20 61 64 64 20 74 68  new APIs, add th
25c56 65 6d 20 74 6f 20 74 68 65 20 62 6f 74 74 6f 6d  em to the bottom
25c57 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
25c58 72 65 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74  re.** in order t
25c59 6f 20 70 72 65 73 65 72 76 65 20 62 61 63 6b 77  o preserve backw
25c5a 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
25c5b 74 79 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73  ty..**.** Extens
25c5c 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 6e 65  ions that use ne
25c5d 77 65 72 20 41 50 49 73 20 73 68 6f 75 6c 64 20  wer APIs should 
25c5e 66 69 72 73 74 20 63 61 6c 6c 20 74 68 65 0a 2a  first call the.*
25c5f 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  * sqlite3_libver
25c60 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f  sion_number() to
25c61 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
25c62 74 68 65 20 41 50 49 20 74 68 65 79 0a 2a 2a 20  the API they.** 
25c63 69 6e 74 65 6e 64 20 74 6f 20 75 73 65 20 69 73  intend to use is
25c64 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 74 68   supported by th
25c65 65 20 6c 69 62 72 61 72 79 2e 20 20 45 78 74 65  e library.  Exte
25c66 6e 73 69 6f 6e 73 20 73 68 6f 75 6c 64 0a 2a 2a  nsions should.**
25c67 20 61 6c 73 6f 20 63 68 65 63 6b 20 74 6f 20 6d   also check to m
25c68 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
25c69 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  e pointer to the
25c6a 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20   function is.** 
25c6b 6e 6f 74 20 4e 55 4c 4c 20 62 65 66 6f 72 65 20  not NULL before 
25c6c 63 61 6c 6c 69 6e 67 20 69 74 2e 0a 2a 2f 0a 73  calling it..*/.s
25c6d 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
25c6e 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
25c6f 20 73 71 6c 69 74 65 33 41 70 69 73 20 3d 20 7b   sqlite3Apis = {
25c70 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65  .  sqlite3_aggre
25c71 67 61 74 65 5f 63 6f 6e 74 65 78 74 2c 0a 23 69  gate_context,.#i
25c72 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25c73 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 73  T_DEPRECATED.  s
25c74 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
25c75 5f 63 6f 75 6e 74 2c 0a 23 65 6c 73 65 0a 20 20  _count,.#else.  
25c76 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  0,.#endif.  sqli
25c77 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 2c 0a 20  te3_bind_blob,. 
25c78 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f   sqlite3_bind_do
25c79 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  uble,.  sqlite3_
25c7a 62 69 6e 64 5f 69 6e 74 2c 0a 20 20 73 71 6c 69  bind_int,.  sqli
25c7b 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 2c 0a  te3_bind_int64,.
25c7c 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e    sqlite3_bind_n
25c7d 75 6c 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ull,.  sqlite3_b
25c7e 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
25c7f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  unt,.  sqlite3_b
25c80 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
25c81 64 65 78 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  dex,.  sqlite3_b
25c82 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
25c83 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  me,.  sqlite3_bi
25c84 6e 64 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74  nd_text,.  sqlit
25c85 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 2c 0a  e3_bind_text16,.
25c86 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76    sqlite3_bind_v
25c87 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  alue,.  sqlite3_
25c88 62 75 73 79 5f 68 61 6e 64 6c 65 72 2c 0a 20 20  busy_handler,.  
25c89 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
25c8a 65 6f 75 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  eout,.  sqlite3_
25c8b 63 68 61 6e 67 65 73 2c 0a 20 20 73 71 6c 69 74  changes,.  sqlit
25c8c 65 33 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69  e3_close,.  sqli
25c8d 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
25c8e 65 64 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  eded,.  sqlite3_
25c8f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
25c90 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
25c91 6c 75 6d 6e 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c  lumn_blob,.  sql
25c92 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
25c93 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  s,.  sqlite3_col
25c94 75 6d 6e 5f 62 79 74 65 73 31 36 2c 0a 20 20 73  umn_bytes16,.  s
25c95 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
25c96 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  unt,.  sqlite3_c
25c97 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
25c98 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ame,.  sqlite3_c
25c99 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
25c9a 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ame16,.  sqlite3
25c9b 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
25c9c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
25c9d 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 2c 0a 20  mn_decltype16,. 
25c9e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
25c9f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65  double,.  sqlite
25ca0 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 2c 0a 20 20  3_column_int,.  
25ca1 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
25ca2 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f  nt64,.  sqlite3_
25ca3 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 2c 0a 20 20 73  column_name,.  s
25ca4 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
25ca5 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  me16,.  sqlite3_
25ca6 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
25ca7 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  me,.  sqlite3_co
25ca8 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
25ca9 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
25caa 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c  lumn_table_name,
25cab 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
25cac 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 2c 0a  n_table_name16,.
25cad 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
25cae 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33  _text,.  sqlite3
25caf 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 2c 0a  _column_text16,.
25cb0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
25cb1 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  _type,.  sqlite3
25cb2 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 2c 0a 20  _column_value,. 
25cb3 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f   sqlite3_commit_
25cb4 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f  hook,.  sqlite3_
25cb5 63 6f 6d 70 6c 65 74 65 2c 0a 20 20 73 71 6c 69  complete,.  sqli
25cb6 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 2c 0a  te3_complete16,.
25cb7 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65    sqlite3_create
25cb8 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 0a 20 20 73 71  _collation,.  sq
25cb9 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
25cba 6c 61 74 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69  lation16,.  sqli
25cbb 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
25cbc 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion,.  sqlite3_c
25cbd 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
25cbe 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ,.  sqlite3_crea
25cbf 74 65 5f 6d 6f 64 75 6c 65 2c 0a 20 20 73 71 6c  te_module,.  sql
25cc0 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 2c  ite3_data_count,
25cc1 0a 20 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  .  sqlite3_db_ha
25cc2 6e 64 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ndle,.  sqlite3_
25cc3 64 65 63 6c 61 72 65 5f 76 74 61 62 2c 0a 20 20  declare_vtab,.  
25cc4 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73  sqlite3_enable_s
25cc5 68 61 72 65 64 5f 63 61 63 68 65 2c 0a 20 20 73  hared_cache,.  s
25cc6 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 2c 0a  qlite3_errcode,.
25cc7 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67    sqlite3_errmsg
25cc8 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  ,.  sqlite3_errm
25cc9 73 67 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  sg16,.  sqlite3_
25cca 65 78 65 63 2c 0a 23 69 66 6e 64 65 66 20 53 51  exec,.#ifndef SQ
25ccb 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
25ccc 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f 65  ATED.  sqlite3_e
25ccd 78 70 69 72 65 64 2c 0a 23 65 6c 73 65 0a 20 20  xpired,.#else.  
25cce 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  0,.#endif.  sqli
25ccf 74 65 33 5f 66 69 6e 61 6c 69 7a 65 2c 0a 20 20  te3_finalize,.  
25cd0 73 71 6c 69 74 65 33 5f 66 72 65 65 2c 0a 20 20  sqlite3_free,.  
25cd1 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
25cd2 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65  le,.  sqlite3_ge
25cd3 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20  t_autocommit,.  
25cd4 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
25cd5 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67  ata,.  sqlite3_g
25cd6 65 74 5f 74 61 62 6c 65 2c 0a 20 20 30 2c 20 20  et_table,.  0,  
25cd7 20 20 20 2f 2a 20 57 61 73 20 73 71 6c 69 74 65     /* Was sqlite
25cd8 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  3_global_recover
25cd9 28 29 2c 20 62 75 74 20 74 68 61 74 20 66 75 6e  (), but that fun
25cda 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61  ction is depreca
25cdb 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ted */.  sqlite3
25cdc 5f 69 6e 74 65 72 72 75 70 74 2c 0a 20 20 73 71  _interrupt,.  sq
25cdd 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
25cde 74 5f 72 6f 77 69 64 2c 0a 20 20 73 71 6c 69 74  t_rowid,.  sqlit
25cdf 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c 0a 20  e3_libversion,. 
25ce0 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
25ce1 69 6f 6e 5f 6e 75 6d 62 65 72 2c 0a 20 20 73 71  ion_number,.  sq
25ce2 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20  lite3_malloc,.  
25ce3 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 2c  sqlite3_mprintf,
25ce4 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 2c  .  sqlite3_open,
25ce5 0a 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  .  sqlite3_open1
25ce6 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65  6,.  sqlite3_pre
25ce7 70 61 72 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  pare,.  sqlite3_
25ce8 70 72 65 70 61 72 65 31 36 2c 0a 20 20 73 71 6c  prepare16,.  sql
25ce9 69 74 65 33 5f 70 72 6f 66 69 6c 65 2c 0a 20 20  ite3_profile,.  
25cea 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
25ceb 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69  _handler,.  sqli
25cec 74 65 33 5f 72 65 61 6c 6c 6f 63 2c 0a 20 20 73  te3_realloc,.  s
25ced 71 6c 69 74 65 33 5f 72 65 73 65 74 2c 0a 20 20  qlite3_reset,.  
25cee 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
25cef 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  lob,.  sqlite3_r
25cf0 65 73 75 6c 74 5f 64 6f 75 62 6c 65 2c 0a 20 20  esult_double,.  
25cf1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
25cf2 72 72 6f 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  rror,.  sqlite3_
25cf3 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 2c 0a  result_error16,.
25cf4 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25cf5 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  _int,.  sqlite3_
25cf6 72 65 73 75 6c 74 5f 69 6e 74 36 34 2c 0a 20 20  result_int64,.  
25cf7 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
25cf8 75 6c 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  ull,.  sqlite3_r
25cf9 65 73 75 6c 74 5f 74 65 78 74 2c 0a 20 20 73 71  esult_text,.  sq
25cfa 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
25cfb 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  t16,.  sqlite3_r
25cfc 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 2c 0a  esult_text16be,.
25cfd 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25cfe 5f 74 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c  _text16le,.  sql
25cff 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
25d00 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 6f 6c  e,.  sqlite3_rol
25d01 6c 62 61 63 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71  lback_hook,.  sq
25d02 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
25d03 69 7a 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  izer,.  sqlite3_
25d04 73 65 74 5f 61 75 78 64 61 74 61 2c 0a 20 20 73  set_auxdata,.  s
25d05 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 2c  qlite3_snprintf,
25d06 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 2c  .  sqlite3_step,
25d07 0a 20 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65  .  sqlite3_table
25d08 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
25d09 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ,.#ifndef SQLITE
25d0a 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
25d0b 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  .  sqlite3_threa
25d0c 64 5f 63 6c 65 61 6e 75 70 2c 0a 23 65 6c 73 65  d_cleanup,.#else
25d0d 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73  .  0,.#endif.  s
25d0e 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
25d0f 6e 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f  nges,.  sqlite3_
25d10 74 72 61 63 65 2c 0a 23 69 66 6e 64 65 66 20 53  trace,.#ifndef S
25d11 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
25d12 43 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f  CATED.  sqlite3_
25d13 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
25d14 73 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65  s,.#else.  0,.#e
25d15 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 75  ndif.  sqlite3_u
25d16 70 64 61 74 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71  pdate_hook,.  sq
25d17 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 2c  lite3_user_data,
25d18 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
25d19 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33  _blob,.  sqlite3
25d1a 5f 76 61 6c 75 65 5f 62 79 74 65 73 2c 0a 20 20  _value_bytes,.  
25d1b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
25d1c 74 65 73 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  tes16,.  sqlite3
25d1d 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 2c 0a 20  _value_double,. 
25d1e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
25d1f 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  nt,.  sqlite3_va
25d20 6c 75 65 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c  lue_int64,.  sql
25d21 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72  ite3_value_numer
25d22 69 63 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69 74  ic_type,.  sqlit
25d23 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 0a 20  e3_value_text,. 
25d24 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25d25 65 78 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ext16,.  sqlite3
25d26 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 2c  _value_text16be,
25d27 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
25d28 5f 74 65 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c  _text16le,.  sql
25d29 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 2c  ite3_value_type,
25d2a 0a 20 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69  .  sqlite3_vmpri
25d2b 6e 74 66 2c 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  ntf,.  /*.  ** T
25d2c 68 65 20 6f 72 69 67 69 6e 61 6c 20 41 50 49 20  he original API 
25d2d 73 65 74 20 65 6e 64 73 20 68 65 72 65 2e 20 20  set ends here.  
25d2e 41 6c 6c 20 65 78 74 65 6e 73 69 6f 6e 73 20 63  All extensions c
25d2f 61 6e 20 63 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a  an call any.  **
25d30 20 6f 66 20 74 68 65 20 41 50 49 73 20 61 62 6f   of the APIs abo
25d31 76 65 20 70 72 6f 76 69 64 65 64 20 74 68 61 74  ve provided that
25d32 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20   the pointer is 
25d33 6e 6f 74 20 4e 55 4c 4c 2e 20 20 42 75 74 0a 20  not NULL.  But. 
25d34 20 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69   ** before calli
25d35 6e 67 20 41 50 49 73 20 74 68 61 74 20 66 6f 6c  ng APIs that fol
25d36 6c 6f 77 2c 20 65 78 74 65 6e 73 69 6f 6e 20 73  low, extension s
25d37 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 0a  hould check the.
25d38 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62    ** sqlite3_lib
25d39 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29  version_number()
25d3a 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
25d3b 65 79 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  ey are dealing w
25d3c 69 74 68 0a 20 20 2a 2a 20 61 20 6c 69 62 72 61  ith.  ** a libra
25d3d 72 79 20 74 68 61 74 20 69 73 20 6e 65 77 20 65  ry that is new e
25d3e 6e 6f 75 67 68 20 74 6f 20 73 75 70 70 6f 72 74  nough to support
25d3f 20 74 68 61 74 20 41 50 49 2e 0a 20 20 2a 2a 2a   that API..  ***
25d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d44 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71  ******.  */.  sq
25d45 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
25d46 75 6e 63 74 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20  unction,..  /*. 
25d47 20 2a 2a 20 41 64 64 65 64 20 61 66 74 65 72 20   ** Added after 
25d48 33 2e 33 2e 31 33 0a 20 20 2a 2f 0a 20 20 73 71  3.3.13.  */.  sq
25d49 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
25d4a 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ,.  sqlite3_prep
25d4b 61 72 65 31 36 5f 76 32 2c 0a 20 20 73 71 6c 69  are16_v2,.  sqli
25d4c 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e  te3_clear_bindin
25d4d 67 73 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41  gs,..  /*.  ** A
25d4e 64 64 65 64 20 66 6f 72 20 33 2e 34 2e 31 0a 20  dded for 3.4.1. 
25d4f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 72   */.  sqlite3_cr
25d50 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a  eate_module_v2,.
25d51 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64  .  /*.  ** Added
25d52 20 66 6f 72 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a   for 3.5.0.  */.
25d53 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a    sqlite3_bind_z
25d54 65 72 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74  eroblob,.  sqlit
25d55 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 2c 0a 20  e3_blob_bytes,. 
25d56 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c   sqlite3_blob_cl
25d57 6f 73 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  ose,.  sqlite3_b
25d58 6c 6f 62 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69  lob_open,.  sqli
25d59 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 2c 0a 20  te3_blob_read,. 
25d5a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72   sqlite3_blob_wr
25d5b 69 74 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ite,.  sqlite3_c
25d5c 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
25d5d 76 32 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  v2,.  sqlite3_fi
25d5e 6c 65 5f 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71  le_control,.  sq
25d5f 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
25d60 68 77 61 74 65 72 2c 0a 20 20 73 71 6c 69 74 65  hwater,.  sqlite
25d61 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 2c 0a 23  3_memory_used,.#
25d62 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  ifdef SQLITE_MUT
25d63 45 58 5f 4f 4d 49 54 0a 20 20 30 2c 20 0a 20 20  EX_OMIT.  0, .  
25d64 30 2c 20 0a 20 20 30 2c 0a 20 20 30 2c 0a 20 20  0, .  0,.  0,.  
25d65 30 2c 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74  0,.#else.  sqlit
25d66 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a  e3_mutex_alloc,.
25d67 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
25d68 65 6e 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33  enter,.  sqlite3
25d69 5f 6d 75 74 65 78 5f 66 72 65 65 2c 0a 20 20 73  _mutex_free,.  s
25d6a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
25d6b 76 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ve,.  sqlite3_mu
25d6c 74 65 78 5f 74 72 79 2c 0a 23 65 6e 64 69 66 0a  tex_try,.#endif.
25d6d 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76    sqlite3_open_v
25d6e 32 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 6c  2,.  sqlite3_rel
25d6f 65 61 73 65 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73  ease_memory,.  s
25d70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
25d71 72 6f 72 5f 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c  ror_nomem,.  sql
25d72 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
25d73 72 5f 74 6f 6f 62 69 67 2c 0a 20 20 73 71 6c 69  r_toobig,.  sqli
25d74 74 65 33 5f 73 6c 65 65 70 2c 0a 20 20 73 71 6c  te3_sleep,.  sql
25d75 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
25d76 69 6d 69 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  imit,.  sqlite3_
25d77 76 66 73 5f 66 69 6e 64 2c 0a 20 20 73 71 6c 69  vfs_find,.  sqli
25d78 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72  te3_vfs_register
25d79 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ,.  sqlite3_vfs_
25d7a 75 6e 72 65 67 69 73 74 65 72 2c 0a 0a 20 20 2f  unregister,..  /
25d7b 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72  *.  ** Added for
25d7c 20 33 2e 35 2e 38 0a 20 20 2a 2f 0a 20 20 73 71   3.5.8.  */.  sq
25d7d 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65  lite3_threadsafe
25d7e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
25d7f 6c 74 5f 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20 73  lt_zeroblob,.  s
25d80 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
25d81 72 6f 72 5f 63 6f 64 65 2c 0a 20 20 73 71 6c 69  ror_code,.  sqli
25d82 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
25d83 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64  ,.  sqlite3_rand
25d84 6f 6d 6e 65 73 73 2c 0a 20 20 73 71 6c 69 74 65  omness,.  sqlite
25d85 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
25d86 64 6c 65 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  dle,..  /*.  ** 
25d87 41 64 64 65 64 20 66 6f 72 20 33 2e 36 2e 30 0a  Added for 3.6.0.
25d88 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 65    */.  sqlite3_e
25d89 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63  xtended_result_c
25d8a 6f 64 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f  odes,.  sqlite3_
25d8b 6c 69 6d 69 74 2c 0a 20 20 73 71 6c 69 74 65 33  limit,.  sqlite3
25d8c 5f 6e 65 78 74 5f 73 74 6d 74 2c 0a 20 20 73 71  _next_stmt,.  sq
25d8d 6c 69 74 65 33 5f 73 71 6c 2c 0a 20 20 73 71 6c  lite3_sql,.  sql
25d8e 69 74 65 33 5f 73 74 61 74 75 73 2c 0a 7d 3b 0a  ite3_status,.};.
25d8f 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
25d90 6f 20 6c 6f 61 64 20 61 6e 20 53 51 4c 69 74 65  o load an SQLite
25d91 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61   extension libra
25d92 72 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ry contained in 
25d93 74 68 65 20 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c  the file.** zFil
25d94 65 2e 20 20 54 68 65 20 65 6e 74 72 79 20 70 6f  e.  The entry po
25d95 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 20 20 7a  int is zProc.  z
25d96 50 72 6f 63 20 6d 61 79 20 62 65 20 30 20 69 6e  Proc may be 0 in
25d97 20 77 68 69 63 68 20 63 61 73 65 20 61 0a 2a 2a   which case a.**
25d98 20 64 65 66 61 75 6c 74 20 65 6e 74 72 79 20 70   default entry p
25d99 6f 69 6e 74 20 6e 61 6d 65 20 28 73 71 6c 69 74  oint name (sqlit
25d9a 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
25d9b 74 29 20 69 73 20 75 73 65 64 2e 20 20 55 73 65  t) is used.  Use
25d9c 0a 2a 2a 20 6f 66 20 74 68 65 20 64 65 66 61 75  .** of the defau
25d9d 6c 74 20 6e 61 6d 65 20 69 73 20 72 65 63 6f 6d  lt name is recom
25d9e 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  mended..**.** Re
25d9f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
25da0 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 53 51  n success and SQ
25da1 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 73 6f  LITE_ERROR if so
25da2 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
25da3 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ng..**.** If an 
25da4 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 64  error occurs and
25da5 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e 6f 74   pzErrMsg is not
25da6 20 30 2c 20 74 68 65 6e 20 66 69 6c 6c 20 2a 70   0, then fill *p
25da7 7a 45 72 72 4d 73 67 20 77 69 74 68 20 0a 2a 2a  zErrMsg with .**
25da8 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
25da9 65 78 74 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  ext.  The callin
25daa 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  g function shoul
25dab 64 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f  d free this memo
25dac 72 79 0a 2a 2a 20 62 79 20 63 61 6c 6c 69 6e 67  ry.** by calling
25dad 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
25dae 62 2c 20 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  b, )..*/.static 
25daf 69 6e 74 20 73 71 6c 69 74 65 33 4c 6f 61 64 45  int sqlite3LoadE
25db0 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69  xtension(.  sqli
25db1 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
25db2 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 78    /* Load the ex
25db3 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69  tension into thi
25db4 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
25db5 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
25db6 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20   char *zFile,   
25db7 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
25db8 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 63  shared library c
25db9 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e 73  ontaining extens
25dba 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
25dbb 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f  har *zProc,    /
25dbc 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20  * Entry point.  
25dbd 55 73 65 20 22 73 71 6c 69 74 65 33 5f 65 78 74  Use "sqlite3_ext
25dbe 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20  ension_init" if 
25dbf 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  0 */.  char **pz
25dc0 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20  ErrMsg       /* 
25dc1 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  Put error messag
25dc2 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20  e here if not 0 
25dc3 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  */.){.  sqlite3_
25dc4 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e  vfs *pVfs = db->
25dc5 70 56 66 73 3b 0a 20 20 76 6f 69 64 20 2a 68 61  pVfs;.  void *ha
25dc6 6e 64 6c 65 3b 0a 20 20 69 6e 74 20 28 2a 78 49  ndle;.  int (*xI
25dc7 6e 69 74 29 28 73 71 6c 69 74 65 33 2a 2c 63 68  nit)(sqlite3*,ch
25dc8 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74  ar**,const sqlit
25dc9 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a  e3_api_routines*
25dca 29 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 6d  );.  char *zErrm
25dcb 73 67 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a  sg = 0;.  void *
25dcc 2a 61 48 61 6e 64 6c 65 3b 0a 0a 20 20 2f 2a 20  *aHandle;..  /* 
25dcd 54 69 63 6b 65 74 20 23 31 38 36 33 2e 20 20 54  Ticket #1863.  T
25dce 6f 20 61 76 6f 69 64 20 61 20 63 72 65 61 74 69  o avoid a creati
25dcf 6e 67 20 73 65 63 75 72 69 74 79 20 70 72 6f 62  ng security prob
25dd0 6c 65 6d 73 20 66 6f 72 20 6f 6c 64 65 72 0a 20  lems for older. 
25dd1 20 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73   ** applications
25dd2 20 74 68 61 74 20 72 65 6c 69 6e 6b 20 61 67 61   that relink aga
25dd3 69 6e 73 74 20 6e 65 77 65 72 20 76 65 72 73 69  inst newer versi
25dd4 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 20 74  ons of SQLite, t
25dd5 68 65 0a 20 20 2a 2a 20 61 62 69 6c 69 74 79 20  he.  ** ability 
25dd6 74 6f 20 72 75 6e 20 6c 6f 61 64 5f 65 78 74 65  to run load_exte
25dd7 6e 73 69 6f 6e 20 69 73 20 74 75 72 6e 65 64 20  nsion is turned 
25dd8 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e 20  off by default. 
25dd9 20 4f 6e 65 0a 20 20 2a 2a 20 6d 75 73 74 20 63   One.  ** must c
25dda 61 6c 6c 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  all sqlite3_enab
25ddb 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le_load_extensio
25ddc 6e 28 29 20 74 6f 20 74 75 72 6e 20 6f 6e 20 65  n() to turn on e
25ddd 78 74 65 6e 73 69 6f 6e 0a 20 20 2a 2a 20 6c 6f  xtension.  ** lo
25dde 61 64 69 6e 67 2e 20 20 4f 74 68 65 72 77 69 73  ading.  Otherwis
25ddf 65 20 79 6f 75 20 67 65 74 20 74 68 65 20 66 6f  e you get the fo
25de0 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 2e 0a 20  llowing error.. 
25de1 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66   */.  if( (db->f
25de2 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4c 6f  lags & SQLITE_Lo
25de3 61 64 45 78 74 65 6e 73 69 6f 6e 29 3d 3d 30 20  adExtension)==0 
25de4 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72 72  ){.    if( pzErr
25de5 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a  Msg ){.      *pz
25de6 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
25de7 5f 6d 70 72 69 6e 74 66 28 22 6e 6f 74 20 61 75  _mprintf("not au
25de8 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20  thorized");.    
25de9 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  }.    return SQL
25dea 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a  ITE_ERROR;.  }..
25deb 20 20 69 66 28 20 7a 50 72 6f 63 3d 3d 30 20 29    if( zProc==0 )
25dec 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 22 73  {.    zProc = "s
25ded 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e  qlite3_extension
25dee 5f 69 6e 69 74 22 3b 0a 20 20 7d 0a 0a 20 20 68  _init";.  }..  h
25def 61 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 4f  andle = sqlite3O
25df0 73 44 6c 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46  sDlOpen(pVfs, zF
25df1 69 6c 65 29 3b 0a 20 20 69 66 28 20 68 61 6e 64  ile);.  if( hand
25df2 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  le==0 ){.    if(
25df3 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
25df4 20 20 20 63 68 61 72 20 7a 45 72 72 5b 32 35 36     char zErr[256
25df5 5d 3b 0a 20 20 20 20 20 20 7a 45 72 72 5b 73 69  ];.      zErr[si
25df6 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d 20  zeof(zErr)-1] = 
25df7 27 5c 30 27 3b 0a 20 20 20 20 20 20 73 71 6c 69  '\0';.      sqli
25df8 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
25df9 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45 72  eof(zErr)-1, zEr
25dfa 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 75  r, .          "u
25dfb 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 73 68  nable to open sh
25dfc 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25 73  ared library [%s
25dfd 5d 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20  ]", zFile);.    
25dfe 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72    sqlite3OsDlErr
25dff 6f 72 28 70 56 66 73 2c 20 73 69 7a 65 6f 66 28  or(pVfs, sizeof(
25e00 7a 45 72 72 29 2d 31 2c 20 7a 45 72 72 29 3b 0a  zErr)-1, zErr);.
25e01 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20        *pzErrMsg 
25e02 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
25e03 70 28 30 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  p(0, zErr);.    
25e04 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  }.    return SQL
25e05 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
25e06 20 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29   xInit = (int(*)
25e07 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a  (sqlite3*,char**
25e08 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61  ,const sqlite3_a
25e09 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20  pi_routines*)). 
25e0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25e0b 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d    sqlite3OsDlSym
25e0c 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 2c 20 7a  (pVfs, handle, z
25e0d 50 72 6f 63 29 3b 0a 20 20 69 66 28 20 78 49 6e  Proc);.  if( xIn
25e0e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  it==0 ){.    if(
25e0f 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
25e10 20 20 20 63 68 61 72 20 7a 45 72 72 5b 32 35 36     char zErr[256
25e11 5d 3b 0a 20 20 20 20 20 20 7a 45 72 72 5b 73 69  ];.      zErr[si
25e12 7a 65 6f 66 28 7a 45 72 72 29 2d 31 5d 20 3d 20  zeof(zErr)-1] = 
25e13 27 5c 30 27 3b 0a 20 20 20 20 20 20 73 71 6c 69  '\0';.      sqli
25e14 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
25e15 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45 72  eof(zErr)-1, zEr
25e16 72 2c 0a 20 20 20 20 20 20 20 20 20 20 22 6e 6f  r,.          "no
25e17 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 5b 25 73   entry point [%s
25e18 5d 20 69 6e 20 73 68 61 72 65 64 20 6c 69 62 72  ] in shared libr
25e19 61 72 79 20 5b 25 73 5d 22 2c 20 7a 50 72 6f 63  ary [%s]", zProc
25e1a 2c 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 73  ,zFile);.      s
25e1b 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28  qlite3OsDlError(
25e1c 70 56 66 73 2c 20 73 69 7a 65 6f 66 28 7a 45 72  pVfs, sizeof(zEr
25e1d 72 29 2d 31 2c 20 7a 45 72 72 29 3b 0a 20 20 20  r)-1, zErr);.   
25e1e 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
25e1f 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30  qlite3DbStrDup(0
25e20 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73  , zErr);.      s
25e21 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28  qlite3OsDlClose(
25e22 70 56 66 73 2c 20 68 61 6e 64 6c 65 29 3b 0a 20  pVfs, handle);. 
25e23 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
25e24 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
25e25 7d 65 6c 73 65 20 69 66 28 20 78 49 6e 69 74 28  }else if( xInit(
25e26 64 62 2c 20 26 7a 45 72 72 6d 73 67 2c 20 26 73  db, &zErrmsg, &s
25e27 71 6c 69 74 65 33 41 70 69 73 29 20 29 7b 0a 20  qlite3Apis) ){. 
25e28 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20     if( pzErrMsg 
25e29 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  ){.      *pzErrM
25e2a 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  sg = sqlite3_mpr
25e2b 69 6e 74 66 28 22 65 72 72 6f 72 20 64 75 72 69  intf("error duri
25e2c 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ng initializatio
25e2d 6e 3a 20 25 73 22 2c 20 7a 45 72 72 6d 73 67 29  n: %s", zErrmsg)
25e2e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
25e2f 74 65 33 5f 66 72 65 65 28 7a 45 72 72 6d 73 67  te3_free(zErrmsg
25e30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  );.    sqlite3Os
25e31 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 68 61  DlClose(pVfs, ha
25e32 6e 64 6c 65 29 3b 0a 20 20 20 20 72 65 74 75 72  ndle);.    retur
25e33 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
25e34 20 20 7d 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64    }..  /* Append
25e35 20 74 68 65 20 6e 65 77 20 73 68 61 72 65 64 20   the new shared 
25e36 6c 69 62 72 61 72 79 20 68 61 6e 64 6c 65 20 74  library handle t
25e37 6f 20 74 68 65 20 64 62 2d 3e 61 45 78 74 65 6e  o the db->aExten
25e38 73 69 6f 6e 20 61 72 72 61 79 2e 20 2a 2f 0a 20  sion array. */. 
25e39 20 61 48 61 6e 64 6c 65 20 3d 20 73 71 6c 69 74   aHandle = sqlit
25e3a 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
25e3b 62 2c 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65  b, sizeof(handle
25e3c 29 2a 28 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f  )*(db->nExtensio
25e3d 6e 2b 31 29 29 3b 0a 20 20 69 66 28 20 61 48 61  n+1));.  if( aHa
25e3e 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ndle==0 ){.    r
25e3f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
25e40 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  EM;.  }.  if( db
25e41 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 3e 30 20 29  ->nExtension>0 )
25e42 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 48 61  {.    memcpy(aHa
25e43 6e 64 6c 65 2c 20 64 62 2d 3e 61 45 78 74 65 6e  ndle, db->aExten
25e44 73 69 6f 6e 2c 20 73 69 7a 65 6f 66 28 68 61 6e  sion, sizeof(han
25e45 64 6c 65 29 2a 64 62 2d 3e 6e 45 78 74 65 6e 73  dle)*db->nExtens
25e46 69 6f 6e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ion);.  }.  sqli
25e47 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62  te3DbFree(db, db
25e48 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 20  ->aExtension);. 
25e49 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 20   db->aExtension 
25e4a 3d 20 61 48 61 6e 64 6c 65 3b 0a 0a 20 20 64 62  = aHandle;..  db
25e4b 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b 64 62 2d  ->aExtension[db-
25e4c 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 2b 5d 20 3d  >nExtension++] =
25e4d 20 68 61 6e 64 6c 65 3b 0a 20 20 72 65 74 75 72   handle;.  retur
25e4e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53  n SQLITE_OK;.}.S
25e4f 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
25e50 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
25e51 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  sion(.  sqlite3 
25e52 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  *db,          /*
25e53 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73   Load the extens
25e54 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61  ion into this da
25e55 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
25e56 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
25e57 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20  r *zFile,    /* 
25e58 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72  Name of the shar
25e59 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61  ed library conta
25e5a 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20  ining extension 
25e5b 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
25e5c 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e  *zProc,    /* En
25e5d 74 72 79 20 70 6f 69 6e 74 2e 20 20 55 73 65 20  try point.  Use 
25e5e 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69  "sqlite3_extensi
25e5f 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30 20 2a 2f  on_init" if 0 */
25e60 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  .  char **pzErrM
25e61 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20  sg       /* Put 
25e62 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65  error message he
25e63 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29  re if not 0 */.)
25e64 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
25e65 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
25e66 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
25e67 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 61 64  rc = sqlite3Load
25e68 45 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 7a 46  Extension(db, zF
25e69 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 70 7a 45 72  ile, zProc, pzEr
25e6a 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33  rMsg);.  sqlite3
25e6b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
25e6c 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
25e6d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
25e6e 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
25e6f 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
25e70 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
25e71 20 63 6c 6f 73 69 6e 67 20 69 6e 20 6f 72 64 65   closing in orde
25e72 72 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20 75 70  r.** to clean up
25e73 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f   loaded extensio
25e74 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ns.*/.SQLITE_PRI
25e75 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25e76 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73  3CloseExtensions
25e77 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
25e78 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
25e79 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
25e7a 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
25e7b 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
25e7c 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 3b 20  db->nExtension; 
25e7d 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
25e7e 33 4f 73 44 6c 43 6c 6f 73 65 28 64 62 2d 3e 70  3OsDlClose(db->p
25e7f 56 66 73 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73  Vfs, db->aExtens
25e80 69 6f 6e 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73  ion[i]);.  }.  s
25e81 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
25e82 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 29   db->aExtension)
25e83 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c  ;.}../*.** Enabl
25e84 65 20 6f 72 20 64 69 73 61 62 6c 65 20 65 78 74  e or disable ext
25e85 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 2e 20  ension loading. 
25e86 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   Extension loadi
25e87 6e 67 20 69 73 20 64 69 73 61 62 6c 65 64 20 62  ng is disabled b
25e88 79 0a 2a 2a 20 64 65 66 61 75 6c 74 20 73 6f 20  y.** default so 
25e89 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73  as not to open s
25e8a 65 63 75 72 69 74 79 20 68 6f 6c 65 73 20 69 6e  ecurity holes in
25e8b 20 6f 6c 64 65 72 20 61 70 70 6c 69 63 61 74 69   older applicati
25e8c 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ons..*/.SQLITE_A
25e8d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
25e8e 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e  nable_load_exten
25e8f 73 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62  sion(sqlite3 *db
25e90 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a 20 20  , int onoff){.  
25e91 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
25e92 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
25e93 20 20 69 66 28 20 6f 6e 6f 66 66 20 29 7b 0a 20    if( onoff ){. 
25e94 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20     db->flags |= 
25e95 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e  SQLITE_LoadExten
25e96 73 69 6f 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  sion;.  }else{. 
25e97 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20     db->flags &= 
25e98 7e 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65  ~SQLITE_LoadExte
25e99 6e 73 69 6f 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c  nsion;.  }.  sql
25e9a 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
25e9b 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
25e9c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
25e9d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
25e9e 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
25e9f 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a  XTENSION */../*.
25ea0 2a 2a 20 54 68 65 20 61 75 74 6f 2d 65 78 74 65  ** The auto-exte
25ea1 6e 73 69 6f 6e 20 63 6f 64 65 20 61 64 64 65 64  nsion code added
25ea2 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
25ea3 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 65 78  hether or not ex
25ea4 74 65 6e 73 69 6f 6e 0a 2a 2a 20 6c 6f 61 64 69  tension.** loadi
25ea5 6e 67 20 69 73 20 73 75 70 70 6f 72 74 65 64 2e  ng is supported.
25ea6 20 20 57 65 20 6e 65 65 64 20 61 20 64 75 6d 6d    We need a dumm
25ea7 79 20 73 71 6c 69 74 65 33 41 70 69 73 20 70 6f  y sqlite3Apis po
25ea8 69 6e 74 65 72 20 66 6f 72 20 74 68 61 74 0a 2a  inter for that.*
25ea9 2a 20 63 6f 64 65 20 69 66 20 72 65 67 75 6c 61  * code if regula
25eaa 72 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  r extension load
25eab 69 6e 67 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  ing is not avail
25eac 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 20 74  able.  This is t
25ead 68 61 74 0a 2a 2a 20 64 75 6d 6d 79 20 70 6f 69  hat.** dummy poi
25eae 6e 74 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  nter..*/.#ifdef 
25eaf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
25eb0 5f 45 58 54 45 4e 53 49 4f 4e 0a 73 74 61 74 69  _EXTENSION.stati
25eb1 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
25eb2 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c  api_routines sql
25eb3 69 74 65 33 41 70 69 73 20 3d 20 7b 20 30 20 7d  ite3Apis = { 0 }
25eb4 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  ;.#endif.../*.**
25eb5 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f   The following o
25eb6 62 6a 65 63 74 20 68 6f 6c 64 73 20 74 68 65 20  bject holds the 
25eb7 6c 69 73 74 20 6f 66 20 61 75 74 6f 6d 61 74 69  list of automati
25eb8 63 61 6c 6c 79 20 6c 6f 61 64 65 64 0a 2a 2a 20  cally loaded.** 
25eb9 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a  extensions..**.*
25eba 2a 20 54 68 69 73 20 6c 69 73 74 20 69 73 20 73  * This list is s
25ebb 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72  hared across thr
25ebc 65 61 64 73 2e 20 20 54 68 65 20 53 51 4c 49 54  eads.  The SQLIT
25ebd 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
25ebe 41 53 54 45 52 0a 2a 2a 20 6d 75 74 65 78 20 6d  ASTER.** mutex m
25ebf 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c  ust be held whil
25ec0 65 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 73  e accessing this
25ec1 20 6c 69 73 74 2e 0a 2a 2f 0a 74 79 70 65 64 65   list..*/.typede
25ec2 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
25ec3 41 75 74 6f 45 78 74 4c 69 73 74 20 73 71 6c 69  AutoExtList sqli
25ec4 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74 3b 0a  te3AutoExtList;.
25ec5 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
25ec6 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  D struct sqlite3
25ec7 41 75 74 6f 45 78 74 4c 69 73 74 20 7b 0a 20 20  AutoExtList {.  
25ec8 69 6e 74 20 6e 45 78 74 3b 20 20 20 20 20 20 20  int nExt;       
25ec9 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
25eca 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
25ecb 45 78 74 5b 5d 20 2a 2f 20 20 20 20 20 20 20 20  Ext[] */        
25ecc 20 20 0a 20 20 76 6f 69 64 20 28 2a 2a 61 45 78    .  void (**aEx
25ecd 74 29 28 76 6f 69 64 29 3b 20 20 20 2f 2a 20 50  t)(void);   /* P
25ece 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 65  ointers to the e
25ecf 78 74 65 6e 73 69 6f 6e 20 69 6e 69 74 20 66 75  xtension init fu
25ed0 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 20 73 71 6c  nctions */.} sql
25ed1 69 74 65 33 41 75 74 6f 65 78 74 20 3d 20 7b 20  ite3Autoext = { 
25ed2 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20  0, 0 };../* The 
25ed3 22 77 73 64 41 75 74 6f 65 78 74 22 20 6d 61 63  "wsdAutoext" mac
25ed4 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20  ro will resolve 
25ed5 74 6f 20 74 68 65 20 61 75 74 6f 65 78 74 65 6e  to the autoexten
25ed6 73 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76 65  sion.** state ve
25ed7 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62  ctor.  If writab
25ed8 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
25ed9 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e  s unsupported on
25eda 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20   the target,.** 
25edb 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74  we have to locat
25edc 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74  e the state vect
25edd 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20  or at run-time. 
25ede 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d   In the more com
25edf 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72  mon.** case wher
25ee0 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  e writable stati
25ee1 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72  c data is suppor
25ee2 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 61 6e  ted, wsdStat can
25ee3 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a   refer directly.
25ee4 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74  ** to the "sqlit
25ee5 65 33 41 75 74 6f 65 78 74 22 20 73 74 61 74 65  e3Autoext" state
25ee6 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64   vector declared
25ee7 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65   above..*/.#ifde
25ee8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53  f SQLITE_OMIT_WS
25ee9 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41 75  D.# define wsdAu
25eea 74 6f 65 78 74 49 6e 69 74 20 5c 0a 20 20 73 71  toextInit \.  sq
25eeb 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74  lite3AutoExtList
25eec 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 73 71   *x = &GLOBAL(sq
25eed 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69 73 74  lite3AutoExtList
25eee 2c 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74 29  ,sqlite3Autoext)
25eef 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41 75 74  .# define wsdAut
25ef0 6f 65 78 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a  oext x[0].#else.
25ef1 23 20 64 65 66 69 6e 65 20 77 73 64 41 75 74 6f  # define wsdAuto
25ef2 65 78 74 49 6e 69 74 0a 23 20 64 65 66 69 6e 65  extInit.# define
25ef3 20 77 73 64 41 75 74 6f 65 78 74 20 73 71 6c 69   wsdAutoext sqli
25ef4 74 65 33 41 75 74 6f 65 78 74 0a 23 65 6e 64 69  te3Autoext.#endi
25ef5 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  f.../*.** Regist
25ef6 65 72 20 61 20 73 74 61 74 69 63 61 6c 6c 79 20  er a statically 
25ef7 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e  linked extension
25ef8 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74   that is automat
25ef9 69 63 61 6c 6c 79 0a 2a 2a 20 6c 6f 61 64 65 64  ically.** loaded
25efa 20 62 79 20 65 76 65 72 79 20 6e 65 77 20 64 61   by every new da
25efb 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
25efc 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
25efd 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74   int sqlite3_aut
25efe 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64  o_extension(void
25eff 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 29 29   (*xInit)(void))
25f00 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
25f01 49 54 45 5f 4f 4b 3b 0a 23 69 66 6e 64 65 66 20  ITE_OK;.#ifndef 
25f02 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
25f03 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69  INIT.  rc = sqli
25f04 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
25f05 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
25f06 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
25f07 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a  else.#endif.  {.
25f08 20 20 20 20 69 6e 74 20 69 3b 0a 23 69 66 20 53      int i;.#if S
25f09 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
25f0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
25f0b 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
25f0c 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
25f0d 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
25f0e 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69  C_MASTER);.#endi
25f0f 66 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78 74  f.    wsdAutoext
25f10 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Init;.    sqlite
25f11 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
25f12 74 65 78 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  tex);.    for(i=
25f13 30 3b 20 69 3c 77 73 64 41 75 74 6f 65 78 74 2e  0; i<wsdAutoext.
25f14 6e 45 78 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nExt; i++){.    
25f15 20 20 69 66 28 20 77 73 64 41 75 74 6f 65 78 74    if( wsdAutoext
25f16 2e 61 45 78 74 5b 69 5d 3d 3d 78 49 6e 69 74 20  .aExt[i]==xInit 
25f17 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
25f18 20 20 20 69 66 28 20 69 3d 3d 77 73 64 41 75 74     if( i==wsdAut
25f19 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20 20 20  oext.nExt ){.   
25f1a 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28     int nByte = (
25f1b 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 2b  wsdAutoext.nExt+
25f1c 31 29 2a 73 69 7a 65 6f 66 28 77 73 64 41 75 74  1)*sizeof(wsdAut
25f1d 6f 65 78 74 2e 61 45 78 74 5b 30 5d 29 3b 0a 20  oext.aExt[0]);. 
25f1e 20 20 20 20 20 76 6f 69 64 20 28 2a 2a 61 4e 65       void (**aNe
25f1f 77 29 28 76 6f 69 64 29 3b 0a 20 20 20 20 20 20  w)(void);.      
25f20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  aNew = sqlite3_r
25f21 65 61 6c 6c 6f 63 28 77 73 64 41 75 74 6f 65 78  ealloc(wsdAutoex
25f22 74 2e 61 45 78 74 2c 20 6e 42 79 74 65 29 3b 0a  t.aExt, nByte);.
25f23 20 20 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d        if( aNew==
25f24 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  0 ){.        rc 
25f25 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
25f26 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
25f27 20 20 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e       wsdAutoext.
25f28 61 45 78 74 20 3d 20 61 4e 65 77 3b 0a 20 20 20  aExt = aNew;.   
25f29 20 20 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e       wsdAutoext.
25f2a 61 45 78 74 5b 77 73 64 41 75 74 6f 65 78 74 2e  aExt[wsdAutoext.
25f2b 6e 45 78 74 5d 20 3d 20 78 49 6e 69 74 3b 0a 20  nExt] = xInit;. 
25f2c 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65 78         wsdAutoex
25f2d 74 2e 6e 45 78 74 2b 2b 3b 0a 20 20 20 20 20 20  t.nExt++;.      
25f2e 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
25f2f 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
25f30 6d 75 74 65 78 29 3b 0a 20 20 20 20 61 73 73 65  mutex);.    asse
25f31 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72  rt( (rc&0xff)==r
25f32 63 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  c );.    return 
25f33 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  rc;.  }.}../*.**
25f34 20 52 65 73 65 74 20 74 68 65 20 61 75 74 6f 6d   Reset the autom
25f35 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 20 6c  atic extension l
25f36 6f 61 64 69 6e 67 20 6d 65 63 68 61 6e 69 73 6d  oading mechanism
25f37 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
25f38 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
25f39 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f  et_auto_extensio
25f3a 6e 28 76 6f 69 64 29 7b 0a 23 69 66 6e 64 65 66  n(void){.#ifndef
25f3b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
25f3c 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69  OINIT.  if( sqli
25f3d 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
25f3e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 0a 23 65  ==SQLITE_OK ).#e
25f3f 6e 64 69 66 0a 20 20 7b 0a 23 69 66 20 53 51 4c  ndif.  {.#if SQL
25f40 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20  ITE_THREADSAFE. 
25f41 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
25f42 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65   *mutex = sqlite
25f43 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
25f44 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
25f45 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a  MASTER);.#endif.
25f46 20 20 20 20 77 73 64 41 75 74 6f 65 78 74 49 6e      wsdAutoextIn
25f47 69 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  it;.    sqlite3_
25f48 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
25f49 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  x);.    sqlite3_
25f4a 66 72 65 65 28 77 73 64 41 75 74 6f 65 78 74 2e  free(wsdAutoext.
25f4b 61 45 78 74 29 3b 0a 20 20 20 20 77 73 64 41 75  aExt);.    wsdAu
25f4c 74 6f 65 78 74 2e 61 45 78 74 20 3d 20 30 3b 0a  toext.aExt = 0;.
25f4d 20 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e 6e      wsdAutoext.n
25f4e 45 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  Ext = 0;.    sql
25f4f 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
25f50 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a  (mutex);.  }.}..
25f51 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 61 6c 6c 20 61  /*.** Load all a
25f52 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69  utomatic extensi
25f53 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
25f54 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
25f55 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73  e3AutoLoadExtens
25f56 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62  ions(sqlite3 *db
25f57 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
25f58 74 20 67 6f 20 3d 20 31 3b 0a 20 20 69 6e 74 20  t go = 1;.  int 
25f59 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
25f5a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 73    int (*xInit)(s
25f5b 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63  qlite3*,char**,c
25f5c 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
25f5d 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 0a 20 20  _routines*);..  
25f5e 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a  wsdAutoextInit;.
25f5f 20 20 69 66 28 20 77 73 64 41 75 74 6f 65 78 74    if( wsdAutoext
25f60 2e 6e 45 78 74 3d 3d 30 20 29 7b 0a 20 20 20 20  .nExt==0 ){.    
25f61 2f 2a 20 43 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20  /* Common case: 
25f62 65 61 72 6c 79 20 6f 75 74 20 77 69 74 68 6f 75  early out withou
25f63 74 20 65 76 65 72 79 20 68 61 76 69 6e 67 20 74  t every having t
25f64 6f 20 61 63 71 75 69 72 65 20 61 20 6d 75 74 65  o acquire a mute
25f65 78 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  x */.    return 
25f66 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
25f67 20 66 6f 72 28 69 3d 30 3b 20 67 6f 3b 20 69 2b   for(i=0; go; i+
25f68 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45  +){.    char *zE
25f69 72 72 6d 73 67 20 3d 20 30 3b 0a 23 69 66 20 53  rrmsg = 0;.#if S
25f6a 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
25f6b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
25f6c 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69  ex *mutex = sqli
25f6d 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
25f6e 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
25f6f 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69  C_MASTER);.#endi
25f70 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  f.    sqlite3_mu
25f71 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
25f72 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d 77 73 64  ;.    if( i>=wsd
25f73 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a  Autoext.nExt ){.
25f74 20 20 20 20 20 20 78 49 6e 69 74 20 3d 20 30 3b        xInit = 0;
25f75 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20  .      go = 0;. 
25f76 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
25f77 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28  xInit = (int(*)(
25f78 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c  sqlite3*,char**,
25f79 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
25f7a 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20  i_routines*)).  
25f7b 20 20 20 20 20 20 20 20 20 20 20 20 77 73 64 41              wsdA
25f7c 75 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3b 0a  utoext.aExt[i];.
25f7d 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
25f7e 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
25f7f 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 78 49  tex);.    if( xI
25f80 6e 69 74 20 26 26 20 78 49 6e 69 74 28 64 62 2c  nit && xInit(db,
25f81 20 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69   &zErrmsg, &sqli
25f82 74 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20  te3Apis) ){.    
25f83 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
25f84 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  b, SQLITE_ERROR,
25f85 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 61 75  .            "au
25f86 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
25f87 6e 20 6c 6f 61 64 69 6e 67 20 66 61 69 6c 65 64  n loading failed
25f88 3a 20 25 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b  : %s", zErrmsg);
25f89 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20  .      go = 0;. 
25f8a 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
25f8b 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 73 71  _ERROR;.      sq
25f8c 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 6d  lite3_free(zErrm
25f8d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  sg);.    }.  }. 
25f8e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
25f8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
25f90 6e 64 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63 20  nd of loadext.c 
25f91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
25f94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
25f95 65 67 69 6e 20 66 69 6c 65 20 70 72 61 67 6d 61  egin file pragma
25f96 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
25f97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
25f99 2a 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20  *.** 2003 April 
25f9a 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
25f9b 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
25f9c 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
25f9d 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
25f9e 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
25f9f 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
25fa0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
25fa1 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
25fa2 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
25fa3 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
25fa4 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
25fa5 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
25fa6 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
25fa7 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
25fa8 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
25fa9 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
25faa 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
25fab 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
25fac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25faf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25fb0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
25fb1 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
25fb2 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
25fb3 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64  e PRAGMA command
25fb4 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 61  ..**.** $Id: pra
25fb5 67 6d 61 2e 63 2c 76 20 31 2e 32 30 39 20 32 30  gma.c,v 1.209 20
25fb6 30 39 2f 30 34 2f 30 37 20 32 32 3a 30 35 3a 34  09/04/07 22:05:4
25fb7 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  3 drh Exp $.*/..
25fb8 2f 2a 20 49 67 6e 6f 72 65 20 74 68 69 73 20 77  /* Ignore this w
25fb9 68 6f 6c 65 20 66 69 6c 65 20 69 66 20 70 72 61  hole file if pra
25fba 67 6d 61 73 20 61 72 65 20 64 69 73 61 62 6c 65  gmas are disable
25fbb 64 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  d.*/.#if !define
25fbc 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52  d(SQLITE_OMIT_PR
25fbd 41 47 4d 41 29 20 26 26 20 21 64 65 66 69 6e 65  AGMA) && !define
25fbe 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  d(SQLITE_OMIT_PA
25fbf 52 53 45 52 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74  RSER)../*.** Int
25fc0 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
25fc1 20 73 74 72 69 6e 67 20 61 73 20 61 20 73 61 66   string as a saf
25fc2 65 74 79 20 6c 65 76 65 6c 2e 20 20 52 65 74 75  ety level.  Retu
25fc3 72 6e 20 30 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a  rn 0 for OFF,.**
25fc4 20 31 20 66 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52   1 for ON or NOR
25fc5 4d 41 4c 20 61 6e 64 20 32 20 66 6f 72 20 46 55  MAL and 2 for FU
25fc6 4c 4c 2e 20 20 52 65 74 75 72 6e 20 31 20 66 6f  LL.  Return 1 fo
25fc7 72 20 61 6e 20 65 6d 70 74 79 20 6f 72 20 0a 2a  r an empty or .*
25fc8 2a 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 73  * unrecognized s
25fc9 74 72 69 6e 67 20 61 72 67 75 6d 65 6e 74 2e 0a  tring argument..
25fca 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
25fcb 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72  the values retur
25fcc 6e 65 64 20 61 72 65 20 6f 6e 65 20 6c 65 73 73  ned are one less
25fcd 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73   that the values
25fce 20 74 68 61 74 0a 2a 2a 20 73 68 6f 75 6c 64 20   that.** should 
25fcf 62 65 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73  be passed into s
25fd0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61  qlite3BtreeSetSa
25fd1 66 65 74 79 4c 65 76 65 6c 28 29 2e 20 20 54 68  fetyLevel().  Th
25fd2 65 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20  e is done.** to 
25fd3 73 75 70 70 6f 72 74 20 6c 65 67 61 63 79 20 53  support legacy S
25fd4 51 4c 20 63 6f 64 65 2e 20 20 54 68 65 20 73 61  QL code.  The sa
25fd5 66 65 74 79 20 6c 65 76 65 6c 20 75 73 65 64 20  fety level used 
25fd6 74 6f 20 62 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a  to be boolean.**
25fd7 20 61 6e 64 20 6f 6c 64 65 72 20 73 63 72 69 70   and older scrip
25fd8 74 73 20 6d 61 79 20 68 61 76 65 20 75 73 65 64  ts may have used
25fd9 20 6e 75 6d 62 65 72 73 20 30 20 66 6f 72 20 4f   numbers 0 for O
25fda 46 46 20 61 6e 64 20 31 20 66 6f 72 20 4f 4e 2e  FF and 1 for ON.
25fdb 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 67 65  .*/.static u8 ge
25fdc 74 53 61 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e  tSafetyLevel(con
25fdd 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 20  st char *z){.   
25fde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25fdf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 32 33            /* 123
25fe0 34 35 36 37 38 39 20 31 32 33 34 35 36 37 38 39  456789 123456789
25fe1 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
25fe2 73 74 20 63 68 61 72 20 7a 54 65 78 74 5b 5d 20  st char zText[] 
25fe3 3d 20 22 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74  = "onoffalseyest
25fe4 72 75 65 66 75 6c 6c 22 3b 0a 20 20 73 74 61 74  ruefull";.  stat
25fe5 69 63 20 63 6f 6e 73 74 20 75 38 20 69 4f 66 66  ic const u8 iOff
25fe6 73 65 74 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20 32  set[] = {0, 1, 2
25fe7 2c 20 34 2c 20 39 2c 20 31 32 2c 20 31 36 7d 3b  , 4, 9, 12, 16};
25fe8 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
25fe9 75 38 20 69 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b  u8 iLength[] = {
25fea 32 2c 20 32 2c 20 33 2c 20 35 2c 20 33 2c 20 34  2, 2, 3, 5, 3, 4
25feb 2c 20 34 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  , 4};.  static c
25fec 6f 6e 73 74 20 75 38 20 69 56 61 6c 75 65 5b 5d  onst u8 iValue[]
25fed 20 3d 20 20 7b 31 2c 20 30 2c 20 30 2c 20 30 2c   =  {1, 0, 0, 0,
25fee 20 31 2c 20 31 2c 20 32 7d 3b 0a 20 20 69 6e 74   1, 1, 2};.  int
25fef 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 73 71 6c   i, n;.  if( sql
25ff0 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20  ite3Isdigit(*z) 
25ff1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 75  ){.    return (u
25ff2 38 29 61 74 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20  8)atoi(z);.  }. 
25ff3 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
25ff4 65 6e 33 30 28 7a 29 3b 0a 20 20 66 6f 72 28 69  en30(z);.  for(i
25ff5 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
25ff6 69 4c 65 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a  iLength); i++){.
25ff7 20 20 20 20 69 66 28 20 69 4c 65 6e 67 74 68 5b      if( iLength[
25ff8 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33  i]==n && sqlite3
25ff9 53 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b  StrNICmp(&zText[
25ffa 69 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29  iOffset[i]],z,n)
25ffb 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
25ffc 75 72 6e 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20  urn iValue[i];. 
25ffd 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
25ffe 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 1;.}../*.** In
25fff 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65  terpret the give
26000 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20 62 6f  n string as a bo
26001 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a  olean value..*/.
26002 73 74 61 74 69 63 20 75 38 20 67 65 74 42 6f 6f  static u8 getBoo
26003 6c 65 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  lean(const char 
26004 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 65  *z){.  return ge
26005 74 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 29 26  tSafetyLevel(z)&
26006 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  1;.}../*.** Inte
26007 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
26008 73 74 72 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b  string as a lock
26009 69 6e 67 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a  ing mode value..
2600a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
2600b 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e  tLockingMode(con
2600c 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
2600d 66 28 20 7a 20 29 7b 0a 20 20 20 20 69 66 28 20  f( z ){.    if( 
2600e 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
2600f 70 28 7a 2c 20 22 65 78 63 6c 75 73 69 76 65 22  p(z, "exclusive"
26010 29 20 29 20 72 65 74 75 72 6e 20 50 41 47 45 52  ) ) return PAGER
26011 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
26012 4c 55 53 49 56 45 3b 0a 20 20 20 20 69 66 28 20  LUSIVE;.    if( 
26013 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
26014 70 28 7a 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29  p(z, "normal") )
26015 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f   return PAGER_LO
26016 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
26017 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 50  ;.  }.  return P
26018 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
26019 5f 51 55 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64  _QUERY;.}..#ifnd
2601a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2601b 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
2601c 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69  Interpret the gi
2601d 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 6e  ven string as an
2601e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64   auto-vacuum mod
2601f 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  e value..**.** T
26020 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
26021 69 6e 67 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66  ings, "none", "f
26022 75 6c 6c 22 20 61 6e 64 20 22 69 6e 63 72 65 6d  ull" and "increm
26023 65 6e 74 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61  ental" are .** a
26024 63 63 65 70 74 61 62 6c 65 2c 20 61 73 20 61 72  cceptable, as ar
26025 65 20 74 68 65 69 72 20 6e 75 6d 65 72 69 63 20  e their numeric 
26026 65 71 75 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20  equivalents: 0, 
26027 31 20 61 6e 64 20 32 20 72 65 73 70 65 63 74 69  1 and 2 respecti
26028 76 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  vely..*/.static 
26029 69 6e 74 20 67 65 74 41 75 74 6f 56 61 63 75 75  int getAutoVacuu
2602a 6d 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  m(const char *z)
2602b 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
2602c 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
2602d 6d 70 28 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20  mp(z, "none") ) 
2602e 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54  return BTREE_AUT
2602f 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20  OVACUUM_NONE;.  
26030 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
26031 72 49 43 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29  rICmp(z, "full")
26032 20 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f   ) return BTREE_
26033 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b  AUTOVACUUM_FULL;
26034 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65  .  if( 0==sqlite
26035 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63  3StrICmp(z, "inc
26036 72 65 6d 65 6e 74 61 6c 22 29 20 29 20 72 65 74  remental") ) ret
26037 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41  urn BTREE_AUTOVA
26038 43 55 55 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d  CUUM_INCR;.  i =
26039 20 61 74 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75   atoi(z);.  retu
2603a 72 6e 20 28 75 38 29 28 28 69 3e 3d 30 26 26 69  rn (u8)((i>=0&&i
2603b 3c 3d 32 29 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e  <=2)?i:0);.}.#en
2603c 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51  dif /* ifndef SQ
2603d 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
2603e 43 55 55 4d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  CUUM */..#ifndef
2603f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
26040 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  ER_PRAGMAS./*.**
26041 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
26042 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
26043 20 74 65 6d 70 20 64 62 20 6c 6f 63 61 74 69 6f   temp db locatio
26044 6e 2e 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20  n. Return 1 for 
26045 66 69 6c 65 0a 2a 2a 20 62 61 63 6b 65 64 20 74  file.** backed t
26046 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
26047 65 73 2c 20 32 20 66 6f 72 20 74 68 65 20 52 65  es, 2 for the Re
26048 64 2d 42 6c 61 63 6b 20 74 72 65 65 20 69 6e 20  d-Black tree in 
26049 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a  memory database.
2604a 2a 2a 20 61 6e 64 20 30 20 74 6f 20 75 73 65 20  ** and 0 to use 
2604b 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  the compile-time
2604c 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61   default..*/.sta
2604d 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 53  tic int getTempS
2604e 74 6f 72 65 28 63 6f 6e 73 74 20 63 68 61 72 20  tore(const char 
2604f 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 5b 30 5d 3e  *z){.  if( z[0]>
26050 3d 27 30 27 20 26 26 20 7a 5b 30 5d 3c 3d 27 32  ='0' && z[0]<='2
26051 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ' ){.    return 
26052 7a 5b 30 5d 20 2d 20 27 30 27 3b 0a 20 20 7d 65  z[0] - '0';.  }e
26053 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53  lse if( sqlite3S
26054 74 72 49 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22  trICmp(z, "file"
26055 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
26056 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
26057 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
26058 28 7a 2c 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30  (z, "memory")==0
26059 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
2605a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
2605b 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23  eturn 0;.  }.}.#
2605c 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2605d 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
2605e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2605f 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
26060 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69  MAS./*.** Invali
26061 64 61 74 65 20 74 65 6d 70 20 73 74 6f 72 61 67  date temp storag
26062 65 2c 20 65 69 74 68 65 72 20 77 68 65 6e 20 74  e, either when t
26063 68 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65 20  he temp storage 
26064 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 66 72  is changed.** fr
26065 6f 6d 20 64 65 66 61 75 6c 74 2c 20 6f 72 20 77  om default, or w
26066 68 65 6e 20 27 66 69 6c 65 27 20 61 6e 64 20 74  hen 'file' and t
26067 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  he temp_store_di
26068 72 65 63 74 6f 72 79 20 68 61 73 20 63 68 61 6e  rectory has chan
26069 67 65 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ged.*/.static in
2606a 74 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70  t invalidateTemp
2606b 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70  Storage(Parse *p
2606c 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
2606d 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2606e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44  db;.  if( db->aD
2606f 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20  b[1].pBt!=0 ){. 
26070 20 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f     if( !db->auto
26071 43 6f 6d 6d 69 74 20 7c 7c 20 73 71 6c 69 74 65  Commit || sqlite
26072 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
26073 61 6e 73 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ans(db->aDb[1].p
26074 42 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Bt) ){.      sql
26075 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
26076 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20  rse, "temporary 
26077 73 74 6f 72 61 67 65 20 63 61 6e 6e 6f 74 20 62  storage cannot b
26078 65 20 63 68 61 6e 67 65 64 20 22 0a 20 20 20 20  e changed ".    
26079 20 20 20 20 22 66 72 6f 6d 20 77 69 74 68 69 6e      "from within
2607a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29   a transaction")
2607b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
2607c 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2607d 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74   }.    sqlite3Bt
2607e 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62  reeClose(db->aDb
2607f 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 64 62  [1].pBt);.    db
26080 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 20 3d 20 30  ->aDb[1].pBt = 0
26081 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  ;.    sqlite3Res
26082 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
26083 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  (db, 0);.  }.  r
26084 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
26085 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
26086 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  ITE_PAGER_PRAGMA
26087 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  S */..#ifndef SQ
26088 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
26089 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66  PRAGMAS./*.** If
2608a 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
2608b 73 65 20 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73  se is open, clos
2608c 65 20 69 74 20 61 6e 64 20 6d 61 72 6b 20 74 68  e it and mark th
2608d 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
2608e 61 0a 2a 2a 20 61 73 20 6e 65 65 64 69 6e 67 20  a.** as needing 
2608f 72 65 6c 6f 61 64 69 6e 67 2e 20 20 54 68 69 73  reloading.  This
26090 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 77 68   must be done wh
26091 65 6e 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c  en using the SQL
26092 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 2a  ITE_TEMP_STORE.*
26093 2a 20 6f 72 20 44 45 46 41 55 4c 54 5f 54 45 4d  * or DEFAULT_TEM
26094 50 5f 53 54 4f 52 45 20 70 72 61 67 6d 61 73 2e  P_STORE pragmas.
26095 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
26096 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65  hangeTempStorage
26097 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
26098 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6f  const char *zSto
26099 72 61 67 65 54 79 70 65 29 7b 0a 20 20 69 6e 74  rageType){.  int
2609a 20 74 73 20 3d 20 67 65 74 54 65 6d 70 53 74 6f   ts = getTempSto
2609b 72 65 28 7a 53 74 6f 72 61 67 65 54 79 70 65 29  re(zStorageType)
2609c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2609d 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2609e 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f  if( db->temp_sto
2609f 72 65 3d 3d 74 73 20 29 20 72 65 74 75 72 6e 20  re==ts ) return 
260a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
260a1 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53   invalidateTempS
260a2 74 6f 72 61 67 65 28 20 70 50 61 72 73 65 20 29  torage( pParse )
260a3 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b   != SQLITE_OK ){
260a4 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
260a5 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
260a6 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 3d  db->temp_store =
260a7 20 28 75 38 29 74 73 3b 0a 20 20 72 65 74 75 72   (u8)ts;.  retur
260a8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
260a9 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
260aa 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
260ab 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
260ac 20 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e 20   code to return 
260ad 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72  a single integer
260ae 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
260af 63 20 76 6f 69 64 20 72 65 74 75 72 6e 53 69 6e  c void returnSin
260b0 67 6c 65 49 6e 74 28 50 61 72 73 65 20 2a 70 50  gleInt(Parse *pP
260b1 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
260b2 20 2a 7a 4c 61 62 65 6c 2c 20 69 36 34 20 76 61   *zLabel, i64 va
260b3 6c 75 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  lue){.  Vdbe *v 
260b4 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
260b5 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20  (pParse);.  int 
260b6 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mem = ++pParse->
260b7 6e 4d 65 6d 3b 0a 20 20 69 36 34 20 2a 70 49 36  nMem;.  i64 *pI6
260b8 34 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  4 = sqlite3DbMal
260b9 6c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64  locRaw(pParse->d
260ba 62 2c 20 73 69 7a 65 6f 66 28 76 61 6c 75 65 29  b, sizeof(value)
260bb 29 3b 0a 20 20 69 66 28 20 70 49 36 34 20 29 7b  );.  if( pI64 ){
260bc 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 49 36 34  .    memcpy(pI64
260bd 2c 20 26 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66  , &value, sizeof
260be 28 76 61 6c 75 65 29 29 3b 0a 20 20 7d 0a 20 20  (value));.  }.  
260bf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
260c0 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30  4(v, OP_Int64, 0
260c1 2c 20 6d 65 6d 2c 20 30 2c 20 28 63 68 61 72 2a  , mem, 0, (char*
260c2 29 70 49 36 34 2c 20 50 34 5f 49 4e 54 36 34 29  )pI64, P4_INT64)
260c3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53  ;.  sqlite3VdbeS
260c4 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
260c5 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  .  sqlite3VdbeSe
260c6 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
260c7 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61  OLNAME_NAME, zLa
260c8 62 65 6c 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  bel, SQLITE_STAT
260c9 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  IC);.  sqlite3Vd
260ca 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
260cb 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31  esultRow, mem, 1
260cc 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  );.}..#ifndef SQ
260cd 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50  LITE_OMIT_FLAG_P
260ce 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65  RAGMAS./*.** Che
260cf 63 6b 20 74 6f 20 73 65 65 20 69 66 20 7a 52 69  ck to see if zRi
260d0 67 68 74 20 61 6e 64 20 7a 4c 65 66 74 20 72 65  ght and zLeft re
260d1 66 65 72 20 74 6f 20 61 20 70 72 61 67 6d 61 20  fer to a pragma 
260d2 74 68 61 74 20 71 75 65 72 69 65 73 0a 2a 2a 20  that queries.** 
260d3 6f 72 20 63 68 61 6e 67 65 73 20 6f 6e 65 20 6f  or changes one o
260d4 66 20 74 68 65 20 66 6c 61 67 73 20 69 6e 20 64  f the flags in d
260d5 62 2d 3e 66 6c 61 67 73 2e 20 20 52 65 74 75 72  b->flags.  Retur
260d6 6e 20 31 20 69 66 20 73 6f 20 61 6e 64 20 30 20  n 1 if so and 0 
260d7 69 66 20 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f 2c  if not..** Also,
260d8 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70   implement the p
260d9 72 61 67 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69 63  ragma..*/.static
260da 20 69 6e 74 20 66 6c 61 67 50 72 61 67 6d 61 28   int flagPragma(
260db 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
260dc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
260dd 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
260de 69 67 68 74 29 7b 0a 20 20 73 74 61 74 69 63 20  ight){.  static 
260df 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50 72  const struct sPr
260e0 61 67 6d 61 54 79 70 65 20 7b 0a 20 20 20 20 63  agmaType {.    c
260e1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
260e2 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  ;  /* Name of th
260e3 65 20 70 72 61 67 6d 61 20 2a 2f 0a 20 20 20 20  e pragma */.    
260e4 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 20 20 20  int mask;       
260e5 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20      /* Mask for 
260e6 74 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 76 61  the db->flags va
260e7 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 50 72 61 67  lue */.  } aPrag
260e8 6d 61 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22  ma[] = {.    { "
260e9 66 75 6c 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  full_column_name
260ea 73 22 2c 20 20 20 20 20 20 20 20 53 51 4c 49 54  s",        SQLIT
260eb 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20  E_FullColNames  
260ec 7d 2c 0a 20 20 20 20 7b 20 22 73 68 6f 72 74 5f  },.    { "short_
260ed 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20  column_names",  
260ee 20 20 20 20 20 53 51 4c 49 54 45 5f 53 68 6f 72       SQLITE_Shor
260ef 74 43 6f 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20 20  tColNames },.   
260f0 20 7b 20 22 63 6f 75 6e 74 5f 63 68 61 6e 67 65   { "count_change
260f1 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53  s",            S
260f2 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
260f3 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 65 6d      },.    { "em
260f4 70 74 79 5f 72 65 73 75 6c 74 5f 63 61 6c 6c 62  pty_result_callb
260f5 61 63 6b 73 22 2c 20 20 20 53 51 4c 49 54 45 5f  acks",   SQLITE_
260f6 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 7d 2c  NullCallback  },
260f7 0a 20 20 20 20 7b 20 22 6c 65 67 61 63 79 5f 66  .    { "legacy_f
260f8 69 6c 65 5f 66 6f 72 6d 61 74 22 2c 20 20 20 20  ile_format",    
260f9 20 20 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79     SQLITE_Legacy
260fa 46 69 6c 65 46 6d 74 20 7d 2c 0a 20 20 20 20 7b  FileFmt },.    {
260fb 20 22 66 75 6c 6c 66 73 79 6e 63 22 2c 20 20 20   "fullfsync",   
260fc 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
260fd 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20  ITE_FullFSync   
260fe 20 20 7d 2c 0a 20 20 20 20 7b 20 22 72 65 76 65    },.    { "reve
260ff 72 73 65 5f 75 6e 6f 72 64 65 72 65 64 5f 73 65  rse_unordered_se
26100 6c 65 63 74 73 22 2c 20 53 51 4c 49 54 45 5f 52  lects", SQLITE_R
26101 65 76 65 72 73 65 4f 72 64 65 72 20 20 7d 2c 0a  everseOrder  },.
26102 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
26103 42 55 47 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74  BUG.    { "sql_t
26104 72 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20  race",          
26105 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 71 6c        SQLITE_Sql
26106 54 72 61 63 65 20 20 20 20 20 20 7d 2c 0a 20 20  Trace      },.  
26107 20 20 7b 20 22 76 64 62 65 5f 6c 69 73 74 69 6e    { "vdbe_listin
26108 67 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  g",             
26109 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69  SQLITE_VdbeListi
2610a 6e 67 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76  ng   },.    { "v
2610b 64 62 65 5f 74 72 61 63 65 22 2c 20 20 20 20 20  dbe_trace",     
2610c 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2610d 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 7d  _VdbeTrace     }
2610e 2c 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  ,.#endif.#ifndef
2610f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
26110 43 4b 0a 20 20 20 20 7b 20 22 69 67 6e 6f 72 65  CK.    { "ignore
26111 5f 63 68 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e  _check_constrain
26112 74 73 22 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f  ts", SQLITE_Igno
26113 72 65 43 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e  reChecks  },.#en
26114 64 69 66 0a 20 20 20 20 2f 2a 20 54 68 65 20 66  dif.    /* The f
26115 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 56 45 52 59  ollowing is VERY
26116 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f   experimental */
26117 0a 20 20 20 20 7b 20 22 77 72 69 74 61 62 6c 65  .    { "writable
26118 5f 73 63 68 65 6d 61 22 2c 20 20 20 20 20 20 20  _schema",       
26119 20 20 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53     SQLITE_WriteS
2611a 63 68 65 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63  chema|SQLITE_Rec
2611b 6f 76 65 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20  overyMode },.   
2611c 20 7b 20 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63   { "omit_readloc
2611d 6b 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53  k",            S
2611e 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b  QLITE_NoReadlock
2611f 20 20 20 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54      },..    /* T
26120 4f 44 4f 3a 20 4d 61 79 62 65 20 69 74 20 73 68  ODO: Maybe it sh
26121 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
26122 62 6c 65 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ble to change th
26123 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  e ReadUncommitte
26124 64 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 66  d.    ** flag if
26125 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61   there are any a
26126 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73  ctive statements
26127 2e 20 2a 2f 0a 20 20 20 20 7b 20 22 72 65 61 64  . */.    { "read
26128 5f 75 6e 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20  _uncommitted",  
26129 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 65         SQLITE_Re
2612a 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c  adUncommitted },
2612b 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  .  };.  int i;. 
2612c 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50   const struct sP
2612d 72 61 67 6d 61 54 79 70 65 20 2a 70 3b 0a 20 20  ragmaType *p;.  
2612e 66 6f 72 28 69 3d 30 2c 20 70 3d 61 50 72 61 67  for(i=0, p=aPrag
2612f 6d 61 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  ma; i<ArraySize(
26130 61 50 72 61 67 6d 61 29 3b 20 69 2b 2b 2c 20 70  aPragma); i++, p
26131 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
26132 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
26133 74 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20  t, p->zName)==0 
26134 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
26135 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
26136 62 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76  b;.      Vdbe *v
26137 3b 0a 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69  ;.      v = sqli
26138 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
26139 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e);.      assert
2613a 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a 20 41 6c  ( v!=0 );  /* Al
2613b 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20  ready allocated 
2613c 62 79 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61  by sqlite3Pragma
2613d 28 29 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  () */.      if( 
2613e 41 4c 57 41 59 53 28 76 29 20 29 7b 0a 20 20 20  ALWAYS(v) ){.   
2613f 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d       if( zRight=
26140 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
26141 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
26142 70 50 61 72 73 65 2c 20 70 2d 3e 7a 4e 61 6d 65  pParse, p->zName
26143 2c 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 70  , (db->flags & p
26144 2d 3e 6d 61 73 6b 29 21 3d 30 20 29 3b 0a 20 20  ->mask)!=0 );.  
26145 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
26146 20 20 20 20 20 20 20 69 66 28 20 67 65 74 42 6f         if( getBo
26147 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 20 29 7b  olean(zRight) ){
26148 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d  .            db-
26149 3e 66 6c 61 67 73 20 7c 3d 20 70 2d 3e 6d 61 73  >flags |= p->mas
2614a 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  k;.          }el
2614b 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
2614c 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 70 2d  db->flags &= ~p-
2614d 3e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20  >mask;.         
2614e 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   }..          /*
2614f 20 4d 61 6e 79 20 6f 66 20 74 68 65 20 66 6c 61   Many of the fla
26150 67 2d 70 72 61 67 6d 61 73 20 6d 6f 64 69 66 79  g-pragmas modify
26151 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
26152 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 0a  ted by the SQL .
26153 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d            ** com
26154 70 69 6c 65 72 20 28 65 67 2e 20 63 6f 75 6e 74  piler (eg. count
26155 5f 63 68 61 6e 67 65 73 29 2e 20 53 6f 20 61 64  _changes). So ad
26156 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 6f 20 65  d an opcode to e
26157 78 70 69 72 65 20 61 6c 6c 0a 20 20 20 20 20 20  xpire all.      
26158 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20      ** compiled 
26159 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61  SQL statements a
2615a 66 74 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61  fter modifying a
2615b 20 70 72 61 67 6d 61 20 76 61 6c 75 65 2e 0a 20   pragma value.. 
2615c 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
2615d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2615e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78  eAddOp2(v, OP_Ex
2615f 70 69 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20  pire, 0, 0);.   
26160 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
26161 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
26162 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
26163 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 0;.}.#endif /
26164 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  * SQLITE_OMIT_FL
26165 41 47 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f  AG_PRAGMAS */../
26166 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 68 75  *.** Return a hu
26167 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 6e 61 6d  man-readable nam
26168 65 20 66 6f 72 20 61 20 63 6f 6e 73 74 72 61 69  e for a constrai
26169 6e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 63  nt resolution ac
2616a 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
2616b 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 63 74 69  const char *acti
2616c 6f 6e 4e 61 6d 65 28 75 38 20 61 63 74 69 6f 6e  onName(u8 action
2616d 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
2616e 2a 7a 4e 61 6d 65 3b 0a 20 20 73 77 69 74 63 68  *zName;.  switch
2616f 28 20 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  ( action ){.    
26170 63 61 73 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 3a  case OE_SetNull:
26171 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20 4e    zName = "SET N
26172 55 4c 4c 22 3b 20 20 20 20 20 20 20 20 20 20 20  ULL";           
26173 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
26174 20 4f 45 5f 53 65 74 44 66 6c 74 3a 20 20 7a 4e   OE_SetDflt:  zN
26175 61 6d 65 20 3d 20 22 53 45 54 20 44 45 46 41 55  ame = "SET DEFAU
26176 4c 54 22 3b 20 20 20 20 20 20 20 20 20 62 72 65  LT";         bre
26177 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f  ak;.    case OE_
26178 43 61 73 63 61 64 65 3a 20 20 7a 4e 61 6d 65 20  Cascade:  zName 
26179 3d 20 22 43 41 53 43 41 44 45 22 3b 20 20 20 20  = "CASCADE";    
2617a 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2617b 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20      default:    
2617c 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 22 52        zName = "R
2617d 45 53 54 52 49 43 54 22 3b 20 20 0a 20 20 20 20  ESTRICT";  .    
2617e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2617f 20 20 61 73 73 65 72 74 28 20 61 63 74 69 6f 6e    assert( action
26180 3d 3d 4f 45 5f 52 65 73 74 72 69 63 74 20 29 3b  ==OE_Restrict );
26181 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65   break;.  }.  re
26182 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f  turn zName;.}../
26183 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 70  *.** Process a p
26184 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e  ragma statement.
26185 20 20 0a 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61 73    .**.** Pragmas
26186 20 61 72 65 20 6f 66 20 74 68 69 73 20 66 6f 72   are of this for
26187 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 52  m:.**.**      PR
26188 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
26189 69 64 20 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a  id [= value].**.
2618a 2a 2a 20 54 68 65 20 69 64 65 6e 74 69 66 69 65  ** The identifie
2618b 72 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20  r might also be 
2618c 61 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 76  a string.  The v
2618d 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67  alue is a string
2618e 2c 20 61 6e 64 0a 2a 2a 20 69 64 65 6e 74 69 66  , and.** identif
2618f 69 65 72 2c 20 6f 72 20 61 20 6e 75 6d 62 65 72  ier, or a number
26190 2e 20 20 49 66 20 6d 69 6e 75 73 46 6c 61 67 20  .  If minusFlag 
26191 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
26192 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20  e value is.** a 
26193 6e 75 6d 62 65 72 20 74 68 61 74 20 77 61 73 20  number that was 
26194 70 72 65 63 65 64 65 64 20 62 79 20 61 20 6d 69  preceded by a mi
26195 6e 75 73 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20  nus sign..**.** 
26196 49 66 20 74 68 65 20 6c 65 66 74 20 73 69 64 65  If the left side
26197 20 69 73 20 22 64 61 74 61 62 61 73 65 2e 69 64   is "database.id
26198 22 20 74 68 65 6e 20 70 49 64 31 20 69 73 20 74  " then pId1 is t
26199 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
2619a 0a 2a 2a 20 61 6e 64 20 70 49 64 32 20 69 73 20  .** and pId2 is 
2619b 74 68 65 20 69 64 2e 20 20 49 66 20 74 68 65 20  the id.  If the 
2619c 6c 65 66 74 20 73 69 64 65 20 69 73 20 6a 75 73  left side is jus
2619d 74 20 22 69 64 22 20 74 68 65 6e 20 70 49 64 31  t "id" then pId1
2619e 20 69 73 20 74 68 65 0a 2a 2a 20 69 64 20 61 6e   is the.** id an
2619f 64 20 70 49 64 32 20 69 73 20 61 6e 79 20 65 6d  d pId2 is any em
261a0 70 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53  pty string..*/.S
261a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
261a2 69 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61  id sqlite3Pragma
261a3 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
261a4 65 2c 20 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64  e, .  Token *pId
261a5 31 2c 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72  1,        /* Fir
261a6 73 74 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61  st part of [data
261a7 62 61 73 65 2e 5d 69 64 20 66 69 65 6c 64 20 2a  base.]id field *
261a8 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 32 2c  /.  Token *pId2,
261a9 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
261aa 64 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62  d part of [datab
261ab 61 73 65 2e 5d 69 64 20 66 69 65 6c 64 2c 20 6f  ase.]id field, o
261ac 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65  r NULL */.  Toke
261ad 6e 20 2a 70 56 61 6c 75 65 2c 20 20 20 20 20 20  n *pValue,      
261ae 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 3c 76 61  /* Token for <va
261af 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  lue>, or NULL */
261b0 0a 20 20 69 6e 74 20 6d 69 6e 75 73 46 6c 61 67  .  int minusFlag
261b1 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
261b2 66 20 61 20 27 2d 27 20 73 69 67 6e 20 70 72 65  f a '-' sign pre
261b3 63 65 64 65 64 20 3c 76 61 6c 75 65 3e 20 2a 2f  ceded <value> */
261b4 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4c 65 66  .){.  char *zLef
261b5 74 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  t = 0;       /* 
261b6 4e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  Nul-terminated U
261b7 54 46 2d 38 20 73 74 72 69 6e 67 20 3c 69 64 3e  TF-8 string <id>
261b8 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 69 67   */.  char *zRig
261b9 68 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  ht = 0;      /* 
261ba 4e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  Nul-terminated U
261bb 54 46 2d 38 20 73 74 72 69 6e 67 20 3c 76 61 6c  TF-8 string <val
261bc 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  ue>, or NULL */.
261bd 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
261be 62 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20  b = 0;   /* The 
261bf 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  database name */
261c0 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 3b 20 20  .  Token *pId;  
261c1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
261c2 6e 74 65 72 20 74 6f 20 3c 69 64 3e 20 74 6f 6b  nter to <id> tok
261c3 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  en */.  int iDb;
261c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
261c5 2a 20 44 61 74 61 62 61 73 65 20 69 6e 64 65 78  * Database index
261c6 20 66 6f 72 20 3c 64 61 74 61 62 61 73 65 3e 20   for <database> 
261c7 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
261c8 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
261c9 20 44 62 20 2a 70 44 62 3b 0a 20 20 56 64 62 65   Db *pDb;.  Vdbe
261ca 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
261cb 64 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dbe = sqlite3Vdb
261cc 65 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20 69  eCreate(db);.  i
261cd 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
261ce 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ;.  pParse->nMem
261cf 20 3d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65   = 2;..  /* Inte
261d0 72 70 72 65 74 20 74 68 65 20 5b 64 61 74 61 62  rpret the [datab
261d1 61 73 65 2e 5d 20 70 61 72 74 20 6f 66 20 74 68  ase.] part of th
261d2 65 20 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65  e pragma stateme
261d3 6e 74 2e 20 69 44 62 20 69 73 20 74 68 65 0a 20  nt. iDb is the. 
261d4 20 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65   ** index of the
261d5 20 64 61 74 61 62 61 73 65 20 74 68 69 73 20 70   database this p
261d6 72 61 67 6d 61 20 69 73 20 62 65 69 6e 67 20 61  ragma is being a
261d7 70 70 6c 69 65 64 20 74 6f 20 69 6e 20 64 62 2e  pplied to in db.
261d8 61 44 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62 20  aDb[]. */.  iDb 
261d9 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
261da 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 49 64  Name(pParse, pId
261db 31 2c 20 70 49 64 32 2c 20 26 70 49 64 29 3b 0a  1, pId2, &pId);.
261dc 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65    if( iDb<0 ) re
261dd 74 75 72 6e 3b 0a 20 20 70 44 62 20 3d 20 26 64  turn;.  pDb = &d
261de 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20  b->aDb[iDb];..  
261df 2f 2a 20 49 66 20 74 68 65 20 74 65 6d 70 20 64  /* If the temp d
261e0 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 6e  atabase has been
261e1 20 65 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d 65   explicitly name
261e2 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  d as part of the
261e3 20 0a 20 20 2a 2a 20 70 72 61 67 6d 61 2c 20 6d   .  ** pragma, m
261e4 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 6f  ake sure it is o
261e5 70 65 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  pen. .  */.  if(
261e6 20 69 44 62 3d 3d 31 20 26 26 20 73 71 6c 69 74   iDb==1 && sqlit
261e7 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61  e3OpenTempDataba
261e8 73 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  se(pParse) ){.  
261e9 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
261ea 20 7a 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33   zLeft = sqlite3
261eb 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
261ec 2c 20 70 49 64 29 3b 0a 20 20 69 66 28 20 21 7a  , pId);.  if( !z
261ed 4c 65 66 74 20 29 20 72 65 74 75 72 6e 3b 0a 20  Left ) return;. 
261ee 20 69 66 28 20 6d 69 6e 75 73 46 6c 61 67 20 29   if( minusFlag )
261ef 7b 0a 20 20 20 20 7a 52 69 67 68 74 20 3d 20 73  {.    zRight = s
261f0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
261f1 2c 20 22 2d 25 54 22 2c 20 70 56 61 6c 75 65 29  , "-%T", pValue)
261f2 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
261f3 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4e  Right = sqlite3N
261f4 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
261f5 20 70 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20   pValue);.  }.. 
261f6 20 61 73 73 65 72 74 28 20 70 49 64 32 20 29 3b   assert( pId2 );
261f7 0a 20 20 7a 44 62 20 3d 20 70 49 64 32 2d 3e 6e  .  zDb = pId2->n
261f8 3e 30 20 3f 20 70 44 62 2d 3e 7a 4e 61 6d 65 20  >0 ? pDb->zName 
261f9 3a 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  : 0;.  if( sqlit
261fa 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
261fb 73 65 2c 20 53 51 4c 49 54 45 5f 50 52 41 47 4d  se, SQLITE_PRAGM
261fc 41 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74  A, zLeft, zRight
261fd 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f  , zDb) ){.    go
261fe 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
261ff 20 7d 0a 20 0a 23 69 66 6e 64 65 66 20 53 51 4c   }. .#ifndef SQL
26200 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
26201 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a  RAGMAS.  /*.  **
26202 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
26203 73 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68  se.]default_cach
26204 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41  e_size.  **  PRA
26205 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 64  GMA [database.]d
26206 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a  efault_cache_siz
26207 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  e=N.  **.  ** Th
26208 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70  e first form rep
26209 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74  orts the current
2620a 20 70 65 72 73 69 73 74 65 6e 74 20 73 65 74 74   persistent sett
2620b 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ing for the.  **
2620c 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
2620d 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  .  The value ret
2620e 75 72 6e 65 64 20 69 73 20 74 68 65 20 6d 61 78  urned is the max
2620f 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 20  imum number of. 
26210 20 2a 2a 20 70 61 67 65 73 20 69 6e 20 74 68 65   ** pages in the
26211 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 54 68   page cache.  Th
26212 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65  e second form se
26213 74 73 20 62 6f 74 68 20 74 68 65 20 63 75 72 72  ts both the curr
26214 65 6e 74 0a 20 20 2a 2a 20 70 61 67 65 20 63 61  ent.  ** page ca
26215 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 20 61  che size value a
26216 6e 64 20 74 68 65 20 70 65 72 73 69 73 74 65 6e  nd the persisten
26217 74 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a  t page cache siz
26218 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 73 74 6f  e value.  ** sto
26219 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  red in the datab
2621a 61 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20  ase file..  **. 
2621b 20 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20   ** The default 
2621c 63 61 63 68 65 20 73 69 7a 65 20 69 73 20 73 74  cache size is st
2621d 6f 72 65 64 20 69 6e 20 6d 65 74 61 2d 76 61 6c  ored in meta-val
2621e 75 65 20 32 20 6f 66 20 70 61 67 65 20 31 20 6f  ue 2 of page 1 o
2621f 66 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  f the.  ** datab
26220 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 63  ase file.  The c
26221 61 63 68 65 20 73 69 7a 65 20 69 73 20 61 63 74  ache size is act
26222 75 61 6c 6c 79 20 74 68 65 20 61 62 73 6f 6c 75  ually the absolu
26223 74 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a  te value of.  **
26224 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63   this memory loc
26225 61 74 69 6f 6e 2e 20 20 54 68 65 20 73 69 67 6e  ation.  The sign
26226 20 6f 66 20 6d 65 74 61 2d 76 61 6c 75 65 20 32   of meta-value 2
26227 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 65 0a   determines the.
26228 20 20 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73    ** synchronous
26229 20 73 65 74 74 69 6e 67 2e 20 20 41 20 6e 65 67   setting.  A neg
2622a 61 74 69 76 65 20 76 61 6c 75 65 20 6d 65 61 6e  ative value mean
2622b 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  s synchronous is
2622c 20 6f 66 66 0a 20 20 2a 2a 20 61 6e 64 20 61 20   off.  ** and a 
2622d 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20 6d  positive value m
2622e 65 61 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73  eans synchronous
2622f 20 69 73 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69   is on..  */.  i
26230 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
26231 70 28 7a 4c 65 66 74 2c 22 64 65 66 61 75 6c 74  p(zLeft,"default
26232 5f 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30  _cache_size")==0
26233 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
26234 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
26235 67 65 74 43 61 63 68 65 53 69 7a 65 5b 5d 20 3d  getCacheSize[] =
26236 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65   {.      { OP_Re
26237 61 64 43 6f 6f 6b 69 65 2c 20 20 30 2c 20 31 2c  adCookie,  0, 1,
26238 20 20 20 20 20 20 20 20 32 7d 2c 20 20 2f 2a 20          2},  /* 
26239 30 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  0 */.      { OP_
2623a 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20  IfPos,       1, 
2623b 36 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  6,        0},.  
2623c 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72      { OP_Integer
2623d 2c 20 20 20 20 20 30 2c 20 32 2c 20 20 20 20 20  ,     0, 2,     
2623e 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     0},.      { O
2623f 50 5f 53 75 62 74 72 61 63 74 2c 20 20 20 20 31  P_Subtract,    1
26240 2c 20 32 2c 20 20 20 20 20 20 20 20 31 7d 2c 0a  , 2,        1},.
26241 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73        { OP_IfPos
26242 2c 20 20 20 20 20 20 20 31 2c 20 36 2c 20 20 20  ,       1, 6,   
26243 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b       0},.      {
26244 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20   OP_Integer,    
26245 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d   0, 1,        0}
26246 2c 20 20 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20  ,  /* 5 */.     
26247 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
26248 20 20 20 31 2c 20 31 2c 20 20 20 20 20 20 20 20     1, 1,        
26249 30 7d 2c 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69  0},.    };.    i
2624a 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 69 66 28  nt addr;.    if(
2624b 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
2624c 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
2624d 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
2624e 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
2624f 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
26250 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
26251 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
26252 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
26253 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
26254 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
26255 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
26256 4e 41 4d 45 2c 20 22 63 61 63 68 65 5f 73 69 7a  NAME, "cache_siz
26257 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
26258 43 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  C);.      pParse
26259 2d 3e 6e 4d 65 6d 20 2b 3d 20 32 3b 0a 20 20 20  ->nMem += 2;.   
2625a 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2625b 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
2625c 2c 20 41 72 72 61 79 53 69 7a 65 28 67 65 74 43  , ArraySize(getC
2625d 61 63 68 65 53 69 7a 65 29 2c 20 67 65 74 43 61  acheSize), getCa
2625e 63 68 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  cheSize);.      
2625f 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
26260 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44 62  eP1(v, addr, iDb
26261 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26262 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
26263 61 64 64 72 2b 35 2c 20 53 51 4c 49 54 45 5f 44  addr+5, SQLITE_D
26264 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
26265 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  E);.    }else{. 
26266 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20       int size = 
26267 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20  atoi(zRight);.  
26268 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29      if( size<0 )
26269 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20   size = -size;. 
2626a 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
2626b 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
2626c 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
2626d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2626e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2626f 6e 74 65 67 65 72 2c 20 73 69 7a 65 2c 20 31 29  nteger, size, 1)
26270 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26271 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26272 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  ReadCookie, iDb,
26273 20 32 2c 20 32 29 3b 0a 20 20 20 20 20 20 61 64   2, 2);.      ad
26274 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
26275 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50  AddOp2(v, OP_IfP
26276 6f 73 2c 20 32 2c 20 30 29 3b 0a 20 20 20 20 20  os, 2, 0);.     
26277 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26278 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
26279 2c 20 2d 73 69 7a 65 2c 20 31 29 3b 0a 20 20 20  , -size, 1);.   
2627a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
2627b 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
2627c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2627d 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53  beAddOp3(v, OP_S
2627e 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 32  etCookie, iDb, 2
2627f 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 44 62 2d  , 1);.      pDb-
26280 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f  >pSchema->cache_
26281 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20  size = size;.   
26282 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53     sqlite3BtreeS
26283 65 74 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d  etCacheSize(pDb-
26284 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65  >pBt, pDb->pSche
26285 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b  ma->cache_size);
26286 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
26287 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
26288 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67  A [database.]pag
26289 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41  e_size.  **  PRA
2628a 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70  GMA [database.]p
2628b 61 67 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a  age_size=N.  **.
2628c 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66    ** The first f
2628d 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20  orm reports the 
2628e 63 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20  current setting 
2628f 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 64 61 74  for the.  ** dat
26290 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20  abase page size 
26291 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 20 73  in bytes.  The s
26292 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20  econd form sets 
26293 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
26294 65 20 70 61 67 65 20 73 69 7a 65 20 76 61 6c 75  e page size valu
26295 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 63 61  e.  The value ca
26296 6e 20 6f 6e 6c 79 20 62 65 20 73 65 74 20 69 66  n only be set if
26297 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61  .  ** the databa
26298 73 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  se has not yet b
26299 65 65 6e 20 63 72 65 61 74 65 64 2e 0a 20 20 2a  een created..  *
2629a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
2629b 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61  trICmp(zLeft,"pa
2629c 67 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a  ge_size")==0 ){.
2629d 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
2629e 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 61   pDb->pBt;.    a
2629f 73 73 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b  ssert( pBt!=0 );
262a0 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74  .    if( !zRight
262a1 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69   ){.      int si
262a2 7a 65 20 3d 20 41 4c 57 41 59 53 28 70 42 74 29  ze = ALWAYS(pBt)
262a3 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   ? sqlite3BtreeG
262a4 65 74 50 61 67 65 53 69 7a 65 28 70 42 74 29 20  etPageSize(pBt) 
262a5 3a 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  : 0;.      retur
262a6 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73  nSingleInt(pPars
262a7 65 2c 20 22 70 61 67 65 5f 73 69 7a 65 22 2c 20  e, "page_size", 
262a8 73 69 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  size);.    }else
262a9 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f  {.      /* Mallo
262aa 63 20 6d 61 79 20 66 61 69 6c 20 77 68 65 6e 20  c may fail when 
262ab 73 65 74 74 69 6e 67 20 74 68 65 20 70 61 67 65  setting the page
262ac 2d 73 69 7a 65 2c 20 61 73 20 74 68 65 72 65 20  -size, as there 
262ad 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0a 20  is an internal. 
262ae 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 74       ** buffer t
262af 68 61 74 20 74 68 65 20 70 61 67 65 72 20 6d 6f  hat the pager mo
262b0 64 75 6c 65 20 72 65 73 69 7a 65 73 20 75 73 69  dule resizes usi
262b1 6e 67 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  ng sqlite3_reall
262b2 6f 63 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  oc()..      */. 
262b3 20 20 20 20 20 64 62 2d 3e 6e 65 78 74 50 61 67       db->nextPag
262b4 65 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69  esize = atoi(zRi
262b5 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ght);.      if( 
262b6 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71  SQLITE_NOMEM==sq
262b7 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
262b8 65 53 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e 6e  eSize(pBt, db->n
262b9 65 78 74 50 61 67 65 73 69 7a 65 2c 20 2d 31 2c  extPagesize, -1,
262ba 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 64   0) ){.        d
262bb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
262bc 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
262bd 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a   }.  }else..  /*
262be 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
262bf 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67  atabase.]max_pag
262c0 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50 52  e_count.  **  PR
262c1 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
262c2 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d 4e  max_page_count=N
262c3 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  **.  ** The f
262c4 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74  irst form report
262c5 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  s the current se
262c6 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20  tting for the.  
262c7 2a 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  ** maximum numbe
262c8 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
262c9 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
262ca 20 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63 6f    The .  ** seco
262cb 6e 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74 73  nd form attempts
262cc 20 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20   to change this 
262cd 73 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a 20  setting.  Both. 
262ce 20 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72 6e   ** forms return
262cf 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
262d0 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ting..  */.  if(
262d1 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
262d2 7a 4c 65 66 74 2c 22 6d 61 78 5f 70 61 67 65 5f  zLeft,"max_page_
262d3 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20  count")==0 ){.  
262d4 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70    Btree *pBt = p
262d5 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e 74  Db->pBt;.    int
262d6 20 6e 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20 20   newMax = 0;.   
262d7 20 61 73 73 65 72 74 28 20 70 42 74 21 3d 30 20   assert( pBt!=0 
262d8 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68  );.    if( zRigh
262d9 74 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61  t ){.      newMa
262da 78 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29  x = atoi(zRight)
262db 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
262dc 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a 20  ALWAYS(pBt) ){. 
262dd 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 73 71       newMax = sq
262de 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67  lite3BtreeMaxPag
262df 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77 4d  eCount(pBt, newM
262e0 61 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ax);.    }.    r
262e1 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
262e2 50 61 72 73 65 2c 20 22 6d 61 78 5f 70 61 67 65  Parse, "max_page
262e3 5f 63 6f 75 6e 74 22 2c 20 6e 65 77 4d 61 78 29  _count", newMax)
262e4 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a  ;.  }else..  /*.
262e5 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
262e6 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 63 6f 75  tabase.]page_cou
262e7 6e 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74  nt.  **.  ** Ret
262e8 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
262e9 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73  f pages in the s
262ea 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61 73  pecified databas
262eb 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  e..  */.  if( sq
262ec 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
262ed 66 74 2c 22 70 61 67 65 5f 63 6f 75 6e 74 22 29  ft,"page_count")
262ee 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ==0 ){.    int i
262ef 52 65 67 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Reg;.    if( sql
262f0 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
262f1 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
262f2 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71  agma_out;.    sq
262f3 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
262f4 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44  chema(pParse, iD
262f5 62 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 2b  b);.    iReg = +
262f6 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
262f7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
262f8 64 4f 70 32 28 76 2c 20 4f 50 5f 50 61 67 65 63  dOp2(v, OP_Pagec
262f9 6f 75 6e 74 2c 20 69 44 62 2c 20 69 52 65 67 29  ount, iDb, iReg)
262fa 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
262fb 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
262fc 73 75 6c 74 52 6f 77 2c 20 69 52 65 67 2c 20 31  sultRow, iReg, 1
262fd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
262fe 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
262ff 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
26300 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
26301 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
26302 2c 20 22 70 61 67 65 5f 63 6f 75 6e 74 22 2c 20  , "page_count", 
26303 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
26304 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
26305 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
26306 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f  base.]locking_mo
26307 64 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  de.  **  PRAGMA 
26308 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69  [database.]locki
26309 6e 67 5f 6d 6f 64 65 20 3d 20 28 6e 6f 72 6d 61  ng_mode = (norma
2630a 6c 7c 65 78 63 6c 75 73 69 76 65 29 0a 20 20 2a  l|exclusive).  *
2630b 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
2630c 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6c 6f  trICmp(zLeft,"lo
2630d 63 6b 69 6e 67 5f 6d 6f 64 65 22 29 3d 3d 30 20  cking_mode")==0 
2630e 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
2630f 72 20 2a 7a 52 65 74 20 3d 20 22 6e 6f 72 6d 61  r *zRet = "norma
26310 6c 22 3b 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64  l";.    int eMod
26311 65 20 3d 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f  e = getLockingMo
26312 64 65 28 7a 52 69 67 68 74 29 3b 0a 0a 20 20 20  de(zRight);..   
26313 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20   if( pId2->n==0 
26314 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  && eMode==PAGER_
26315 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52  LOCKINGMODE_QUER
26316 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69  Y ){.      /* Si
26317 6d 70 6c 65 20 22 50 52 41 47 4d 41 20 6c 6f 63  mple "PRAGMA loc
26318 6b 69 6e 67 5f 6d 6f 64 65 3b 22 20 73 74 61 74  king_mode;" stat
26319 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61  ement. This is a
2631a 20 71 75 65 72 79 20 66 6f 72 0a 20 20 20 20 20   query for.     
2631b 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20   ** the current 
2631c 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20  default locking 
2631d 6d 6f 64 65 20 28 77 68 69 63 68 20 6d 61 79 20  mode (which may 
2631e 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f 0a  be different to.
2631f 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63        ** the loc
26320 6b 69 6e 67 2d 6d 6f 64 65 20 6f 66 20 74 68 65  king-mode of the
26321 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 29 2e   main database).
26322 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
26323 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c 74  eMode = db->dflt
26324 4c 6f 63 6b 4d 6f 64 65 3b 0a 20 20 20 20 7d 65  LockMode;.    }e
26325 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72  lse{.      Pager
26326 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20   *pPager;.      
26327 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 29  if( pId2->n==0 )
26328 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
26329 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  s indicates that
2632a 20 6e 6f 20 64 61 74 61 62 61 73 65 20 6e 61 6d   no database nam
2632b 65 20 77 61 73 20 73 70 65 63 69 66 69 65 64 20  e was specified 
2632c 61 73 20 70 61 72 74 0a 20 20 20 20 20 20 20 20  as part.        
2632d 2a 2a 20 6f 66 20 74 68 65 20 50 52 41 47 4d 41  ** of the PRAGMA
2632e 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68 69   command. In thi
2632f 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63 6b 69  s case the locki
26330 6e 67 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65 0a  ng-mode must be.
26331 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6f          ** set o
26332 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64  n all attached d
26333 61 74 61 62 61 73 65 73 2c 20 61 73 20 77 65 6c  atabases, as wel
26334 6c 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64 62  l as the main db
26335 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a   file..        *
26336 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 6c 73  *.        ** Als
26337 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 33 2e 64  o, the sqlite3.d
26338 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 76 61 72 69  fltLockMode vari
26339 61 62 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74  able is set so t
2633a 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  hat.        ** a
2633b 6e 79 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20  ny subsequently 
2633c 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
2633d 65 73 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20  es also use the 
2633e 73 70 65 63 69 66 69 65 64 0a 20 20 20 20 20 20  specified.      
2633f 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64    ** locking mod
26340 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
26341 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20        int ii;.  
26342 20 20 20 20 20 20 61 73 73 65 72 74 28 70 44 62        assert(pDb
26343 3d 3d 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a  ==&db->aDb[0]);.
26344 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 32          for(ii=2
26345 3b 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69  ; ii<db->nDb; ii
26346 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ++){.          p
26347 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
26348 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44  treePager(db->aD
26349 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20  b[ii].pBt);.    
2634a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
2634b 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50  erLockingMode(pP
2634c 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20  ager, eMode);.  
2634d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2634e 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  db->dfltLockMode
2634f 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20   = (u8)eMode;.  
26350 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
26351 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
26352 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29  ePager(pDb->pBt)
26353 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  ;.      eMode = 
26354 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
26355 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20  ingMode(pPager, 
26356 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a 20  eMode);.    }.. 
26357 20 20 20 61 73 73 65 72 74 28 65 4d 6f 64 65 3d     assert(eMode=
26358 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f  =PAGER_LOCKINGMO
26359 44 45 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64 65  DE_NORMAL||eMode
2635a 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
2635b 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a  ODE_EXCLUSIVE);.
2635c 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 50      if( eMode==P
2635d 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
2635e 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20  _EXCLUSIVE ){.  
2635f 20 20 20 20 7a 52 65 74 20 3d 20 22 65 78 63 6c      zRet = "excl
26360 75 73 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20 20  usive";.    }.  
26361 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
26362 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
26363 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
26364 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
26365 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f  OLNAME_NAME, "lo
26366 63 6b 69 6e 67 5f 6d 6f 64 65 22 2c 20 53 51 4c  cking_mode", SQL
26367 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
26368 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26369 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
2636a 2c 20 30 2c 20 31 2c 20 30 2c 20 7a 52 65 74 2c  , 0, 1, 0, zRet,
2636b 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
2636c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2636d 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31  _ResultRow, 1, 1
2636e 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a  );.  }else..  /*
2636f 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
26370 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c  atabase.]journal
26371 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41 47  _mode.  **  PRAG
26372 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f  MA [database.]jo
26373 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 28 64 65  urnal_mode = (de
26374 6c 65 74 65 7c 70 65 72 73 69 73 74 7c 6f 66 66  lete|persist|off
26375 7c 74 72 75 6e 63 61 74 65 7c 6d 65 6d 6f 72 79  |truncate|memory
26376 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ).  */.  if( sql
26377 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
26378 74 2c 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22  t,"journal_mode"
26379 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
2637a 65 4d 6f 64 65 3b 0a 20 20 20 20 73 74 61 74 69  eMode;.    stati
2637b 63 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61  c char * const a
2637c 7a 4d 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a  zModeName[] = {.
2637d 20 20 20 20 20 20 22 64 65 6c 65 74 65 22 2c 20        "delete", 
2637e 22 70 65 72 73 69 73 74 22 2c 20 22 6f 66 66 22  "persist", "off"
2637f 2c 20 22 74 72 75 6e 63 61 74 65 22 2c 20 22 6d  , "truncate", "m
26380 65 6d 6f 72 79 22 0a 20 20 20 20 7d 3b 0a 0a 20  emory".    };.. 
26381 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30     if( zRight==0
26382 20 29 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20   ){.      eMode 
26383 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  = PAGER_JOURNALM
26384 4f 44 45 5f 51 55 45 52 59 3b 0a 20 20 20 20 7d  ODE_QUERY;.    }
26385 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
26386 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
26387 6e 33 30 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  n30(zRight);.   
26388 20 20 20 65 4d 6f 64 65 20 3d 20 73 69 7a 65 6f     eMode = sizeo
26389 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 29 2f 73 69  f(azModeName)/si
2638a 7a 65 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 5b  zeof(azModeName[
2638b 30 5d 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 77  0]) - 1;.      w
2638c 68 69 6c 65 28 20 65 4d 6f 64 65 3e 3d 30 20 26  hile( eMode>=0 &
2638d 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
2638e 70 28 7a 52 69 67 68 74 2c 20 61 7a 4d 6f 64 65  p(zRight, azMode
2638f 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 6e 29 21  Name[eMode], n)!
26390 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 65 4d  =0 ){.        eM
26391 6f 64 65 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20  ode--;.      }. 
26392 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64     }.    if( pId
26393 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65  2->n==0 && eMode
26394 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
26395 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20 20  ODE_QUERY ){.   
26396 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52     /* Simple "PR
26397 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64  AGMA journal_mod
26398 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  e;" statement. T
26399 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20 66  his is a query f
2639a 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  or.      ** the 
2639b 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74 20  current default 
2639c 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 28 77 68  journal mode (wh
2639d 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66 65  ich may be diffe
2639e 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  rent to.      **
2639f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64   the journal-mod
263a0 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61  e of the main da
263a1 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20 2a  tabase)..      *
263a2 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  /.      eMode = 
263a3 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d  db->dfltJournalM
263a4 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ode;.    }else{.
263a5 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61        Pager *pPa
263a6 67 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ger;.      if( p
263a7 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20  Id2->n==0 ){.   
263a8 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64       /* This ind
263a9 69 63 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64  icates that no d
263aa 61 74 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73  atabase name was
263ab 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61   specified as pa
263ac 72 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66  rt.        ** of
263ad 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d   the PRAGMA comm
263ae 61 6e 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  and. In this cas
263af 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  e the journal-mo
263b0 64 65 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20  de must be.     
263b1 20 20 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c     ** set on all
263b2 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
263b3 73 65 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20  ses, as well as 
263b4 74 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65  the main db file
263b5 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
263b6 20 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68       ** Also, th
263b7 65 20 73 71 6c 69 74 65 33 2e 64 66 6c 74 4a 6f  e sqlite3.dfltJo
263b8 75 72 6e 61 6c 4d 6f 64 65 20 76 61 72 69 61 62  urnalMode variab
263b9 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74 68 61  le is set so tha
263ba 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79  t.        ** any
263bb 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 61 74   subsequently at
263bc 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
263bd 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20 73 70   also use the sp
263be 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 20 20  ecified.        
263bf 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e  ** journal mode.
263c0 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
263c1 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20      int ii;.    
263c2 20 20 20 20 61 73 73 65 72 74 28 70 44 62 3d 3d      assert(pDb==
263c3 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20  &db->aDb[0]);.  
263c4 20 20 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20        for(ii=1; 
263c5 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b  ii<db->nDb; ii++
263c6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
263c7 20 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74   db->aDb[ii].pBt
263c8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
263c9 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
263ca 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61  BtreePager(db->a
263cb 44 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20  Db[ii].pBt);.   
263cc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
263cd 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65  PagerJournalMode
263ce 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b  (pPager, eMode);
263cf 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
263d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
263d1 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f  b->dfltJournalMo
263d2 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a  de = (u8)eMode;.
263d3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50        }.      pP
263d4 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
263d5 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42  reePager(pDb->pB
263d6 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20  t);.      eMode 
263d7 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  = sqlite3PagerJo
263d8 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72  urnalMode(pPager
263d9 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a  , eMode);.    }.
263da 20 20 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64      assert( eMod
263db 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
263dc 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 20 20 20  MODE_DELETE.    
263dd 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
263de 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
263df 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 20  LMODE_TRUNCATE. 
263e0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
263e1 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
263e2 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
263e3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c  .              |
263e4 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
263e5 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 20  OURNALMODE_OFF. 
263e6 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
263e7 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
263e8 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
263e9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
263ea 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
263eb 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
263ec 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
263ed 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
263ee 2c 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22  , "journal_mode"
263ef 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
263f0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
263f1 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
263f2 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20  ring8, 0, 1, 0, 
263f3 0a 20 20 20 20 20 20 20 20 20 20 20 61 7a 4d 6f  .           azMo
263f4 64 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 50  deName[eMode], P
263f5 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  4_STATIC);.    s
263f6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
263f7 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
263f8 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65  , 1, 1);.  }else
263f9 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
263fa 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a  GMA [database.]j
263fb 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69  ournal_size_limi
263fc 74 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  t.  **  PRAGMA [
263fd 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61  database.]journa
263fe 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 3d 4e 0a 20  l_size_limit=N. 
263ff 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20   **.  ** Get or 
26400 73 65 74 20 74 68 65 20 73 69 7a 65 20 6c 69 6d  set the size lim
26401 69 74 20 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a  it on rollback j
26402 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 20 20  ournal files..  
26403 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
26404 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a  StrICmp(zLeft,"j
26405 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69  ournal_size_limi
26406 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61  t")==0 ){.    Pa
26407 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71  ger *pPager = sq
26408 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
26409 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69  pDb->pBt);.    i
2640a 36 34 20 69 4c 69 6d 69 74 20 3d 20 2d 32 3b 0a  64 iLimit = -2;.
2640b 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 29      if( zRight )
2640c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41  {.      sqlite3A
2640d 74 6f 69 36 34 28 7a 52 69 67 68 74 2c 20 26 69  toi64(zRight, &i
2640e 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 69 66  Limit);.      if
2640f 28 20 69 4c 69 6d 69 74 3c 2d 31 20 29 20 69 4c  ( iLimit<-1 ) iL
26410 69 6d 69 74 20 3d 20 2d 31 3b 0a 20 20 20 20 7d  imit = -1;.    }
26411 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 73 71  .    iLimit = sq
26412 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
26413 6c 53 69 7a 65 4c 69 6d 69 74 28 70 50 61 67 65  lSizeLimit(pPage
26414 72 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  r, iLimit);.    
26415 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
26416 70 50 61 72 73 65 2c 20 22 6a 6f 75 72 6e 61 6c  pParse, "journal
26417 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 2c 20 69 4c  _size_limit", iL
26418 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a  imit);.  }else..
26419 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2641a 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
2641b 4d 41 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a  MAS */..  /*.  *
2641c 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
2641d 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d  ase.]auto_vacuum
2641e 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
2641f 61 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61  atabase.]auto_va
26420 63 75 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a  cuum=N.  **.  **
26421 20 47 65 74 20 6f 72 20 73 65 74 20 74 68 65 20   Get or set the 
26422 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74  value of the dat
26423 61 62 61 73 65 20 27 61 75 74 6f 2d 76 61 63 75  abase 'auto-vacu
26424 75 6d 27 20 70 61 72 61 6d 65 74 65 72 2e 0a 20  um' parameter.. 
26425 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73   ** The value is
26426 20 6f 6e 65 20 6f 66 3a 20 20 30 20 4e 4f 4e 45   one of:  0 NONE
26427 20 31 20 46 55 4c 4c 20 32 20 49 4e 43 52 45 4d   1 FULL 2 INCREM
26428 45 4e 54 41 4c 0a 20 20 2a 2f 0a 23 69 66 6e 64  ENTAL.  */.#ifnd
26429 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2642a 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20  UTOVACUUM.  if( 
2642b 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2642c 4c 65 66 74 2c 22 61 75 74 6f 5f 76 61 63 75 75  Left,"auto_vacuu
2642d 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74  m")==0 ){.    Bt
2642e 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e  ree *pBt = pDb->
2642f 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pBt;.    assert(
26430 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 69   pBt!=0 );.    i
26431 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
26432 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a  hema(pParse) ){.
26433 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d        goto pragm
26434 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  a_out;.    }.   
26435 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a   if( !zRight ){.
26436 20 20 20 20 20 20 69 6e 74 20 61 75 74 6f 5f 76        int auto_v
26437 61 63 75 75 6d 3b 0a 20 20 20 20 20 20 69 66 28  acuum;.      if(
26438 20 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a   ALWAYS(pBt) ){.
26439 20 20 20 20 20 20 20 20 20 61 75 74 6f 5f 76 61           auto_va
2643a 63 75 75 6d 20 3d 20 73 71 6c 69 74 65 33 42 74  cuum = sqlite3Bt
2643b 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
2643c 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  (pBt);.      }el
2643d 73 65 7b 0a 20 20 20 20 20 20 20 20 20 61 75 74  se{.         aut
2643e 6f 5f 76 61 63 75 75 6d 20 3d 20 53 51 4c 49 54  o_vacuum = SQLIT
2643f 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41  E_DEFAULT_AUTOVA
26440 43 55 55 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  CUUM;.      }.  
26441 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
26442 49 6e 74 28 70 50 61 72 73 65 2c 20 22 61 75 74  Int(pParse, "aut
26443 6f 5f 76 61 63 75 75 6d 22 2c 20 61 75 74 6f 5f  o_vacuum", auto_
26444 76 61 63 75 75 6d 29 3b 0a 20 20 20 20 7d 65 6c  vacuum);.    }el
26445 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 41  se{.      int eA
26446 75 74 6f 20 3d 20 67 65 74 41 75 74 6f 56 61 63  uto = getAutoVac
26447 75 75 6d 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  uum(zRight);.   
26448 20 20 20 61 73 73 65 72 74 28 20 65 41 75 74 6f     assert( eAuto
26449 3e 3d 30 20 26 26 20 65 41 75 74 6f 3c 3d 32 20  >=0 && eAuto<=2 
2644a 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65 78  );.      db->nex
2644b 74 41 75 74 6f 76 61 63 20 3d 20 28 75 38 29 65  tAutovac = (u8)e
2644c 41 75 74 6f 3b 0a 20 20 20 20 20 20 69 66 28 20  Auto;.      if( 
2644d 41 4c 57 41 59 53 28 65 41 75 74 6f 3e 3d 30 29  ALWAYS(eAuto>=0)
2644e 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
2644f 61 6c 6c 20 53 65 74 41 75 74 6f 56 61 63 75 75  all SetAutoVacuu
26450 6d 28 29 20 74 6f 20 73 65 74 20 69 6e 69 74 69  m() to set initi
26451 61 6c 69 7a 65 20 74 68 65 20 69 6e 74 65 72 6e  alize the intern
26452 61 6c 20 61 75 74 6f 20 61 6e 64 0a 20 20 20 20  al auto and.    
26453 20 20 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63 75      ** incr-vacu
26454 75 6d 20 66 6c 61 67 73 2e 20 54 68 69 73 20 69  um flags. This i
26455 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 63 61  s required in ca
26456 73 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  se this connecti
26457 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 72  on.        ** cr
26458 65 61 74 65 73 20 74 68 65 20 64 61 74 61 62 61  eates the databa
26459 73 65 20 66 69 6c 65 2e 20 49 74 20 69 73 20 69  se file. It is i
2645a 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 69 74  mportant that it
2645b 20 69 73 20 63 72 65 61 74 65 64 0a 20 20 20 20   is created.    
2645c 20 20 20 20 2a 2a 20 61 73 20 61 6e 20 61 75 74      ** as an aut
2645d 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65  o-vacuum capable
2645e 20 64 62 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a   db..        */.
2645f 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d          int rc =
26460 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
26461 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74 2c 20  AutoVacuum(pBt, 
26462 65 41 75 74 6f 29 3b 0a 20 20 20 20 20 20 20 20  eAuto);.        
26463 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
26464 4b 20 26 26 20 28 65 41 75 74 6f 3d 3d 31 20 7c  K && (eAuto==1 |
26465 7c 20 65 41 75 74 6f 3d 3d 32 29 20 29 7b 0a 20  | eAuto==2) ){. 
26466 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e           /* When
26467 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75 74   setting the aut
26468 6f 5f 76 61 63 75 75 6d 20 6d 6f 64 65 20 74 6f  o_vacuum mode to
26469 20 65 69 74 68 65 72 20 22 66 75 6c 6c 22 20 6f   either "full" o
2646a 72 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  r .          ** 
2646b 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 2c 20 77  "incremental", w
2646c 72 69 74 65 20 74 68 65 20 76 61 6c 75 65 20 6f  rite the value o
2646d 66 20 6d 65 74 61 5b 36 5d 20 69 6e 20 74 68 65  f meta[6] in the
2646e 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20   database.      
2646f 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65 66      ** file. Bef
26470 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 6d  ore writing to m
26471 65 74 61 5b 36 5d 2c 20 63 68 65 63 6b 20 74 68  eta[6], check th
26472 61 74 20 6d 65 74 61 5b 33 5d 20 69 6e 64 69 63  at meta[3] indic
26473 61 74 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a  ates.          *
26474 2a 20 74 68 61 74 20 74 68 69 73 20 72 65 61 6c  * that this real
26475 6c 79 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61  ly is an auto-va
26476 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64 61 74  cuum capable dat
26477 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20  abase..         
26478 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74   */.          st
26479 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
2647a 70 4c 69 73 74 20 73 65 74 4d 65 74 61 36 5b 5d  pList setMeta6[]
2647b 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20   = {.           
2647c 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f   { OP_Transactio
2647d 6e 2c 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  n,    0,        
2647e 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 20         1,       
2647f 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a   0},    /* 0 */.
26480 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
26481 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20 20  _ReadCookie,    
26482 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
26483 20 20 31 2c 20 20 20 20 20 20 20 20 33 7d 2c 20    1,        3}, 
26484 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20     /* 1 */.     
26485 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 2c 20         { OP_If, 
26486 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 20              1,  
26487 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
26488 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a         0},    /*
26489 20 32 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   2 */.          
2648a 20 20 7b 20 4f 50 5f 48 61 6c 74 2c 20 20 20 20    { OP_Halt,    
2648b 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b         SQLITE_OK
2648c 2c 20 20 20 20 20 20 20 4f 45 5f 41 62 6f 72 74  ,       OE_Abort
2648d 2c 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f  , 0},    /* 3 */
2648e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
2648f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20  P_Integer,      
26490 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
26491 20 20 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c     1,        0},
26492 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20      /* 4 */.    
26493 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65 74          { OP_Set
26494 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20  Cookie,      0, 
26495 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 2c                6,
26496 20 20 20 20 20 20 20 20 31 7d 2c 20 20 20 20 2f          1},    /
26497 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 5 */.         
26498 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   };.          in
26499 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20 20  t iAddr;.       
2649a 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74     iAddr = sqlit
2649b 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
2649c 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73 65 74  v, ArraySize(set
2649d 4d 65 74 61 36 29 2c 20 73 65 74 4d 65 74 61 36  Meta6), setMeta6
2649e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2649f 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
264a0 28 76 2c 20 69 41 64 64 72 2c 20 69 44 62 29 3b  (v, iAddr, iDb);
264a1 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
264a2 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
264a3 2c 20 69 41 64 64 72 2b 31 2c 20 69 44 62 29 3b  , iAddr+1, iDb);
264a4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
264a5 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
264a6 2c 20 69 41 64 64 72 2b 32 2c 20 69 41 64 64 72  , iAddr+2, iAddr
264a7 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +4);.          s
264a8 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
264a9 50 31 28 76 2c 20 69 41 64 64 72 2b 34 2c 20 65  P1(v, iAddr+4, e
264aa 41 75 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20 20  Auto-1);.       
264ab 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
264ac 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b  angeP1(v, iAddr+
264ad 35 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20  5, iDb);.       
264ae 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
264af 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
264b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
264b1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
264b2 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20  .#endif..  /*.  
264b3 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
264b4 62 61 73 65 2e 5d 69 6e 63 72 65 6d 65 6e 74 61  base.]incrementa
264b5 6c 5f 76 61 63 75 75 6d 28 4e 29 0a 20 20 2a 2a  l_vacuum(N).  **
264b6 0a 20 20 2a 2a 20 44 6f 20 4e 20 73 74 65 70 73  .  ** Do N steps
264b7 20 6f 66 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20   of incremental 
264b8 76 61 63 75 75 6d 69 6e 67 20 6f 6e 20 61 20 64  vacuuming on a d
264b9 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 23 69  atabase..  */.#i
264ba 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
264bb 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69  T_AUTOVACUUM.  i
264bc 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
264bd 70 28 7a 4c 65 66 74 2c 22 69 6e 63 72 65 6d 65  p(zLeft,"increme
264be 6e 74 61 6c 5f 76 61 63 75 75 6d 22 29 3d 3d 30  ntal_vacuum")==0
264bf 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d   ){.    int iLim
264c0 69 74 2c 20 61 64 64 72 3b 0a 20 20 20 20 69 66  it, addr;.    if
264c1 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
264c2 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
264c3 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61       goto pragma
264c4 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
264c5 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 7c 7c  if( zRight==0 ||
264c6 20 21 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33   !sqlite3GetInt3
264c7 32 28 7a 52 69 67 68 74 2c 20 26 69 4c 69 6d 69  2(zRight, &iLimi
264c8 74 29 20 7c 7c 20 69 4c 69 6d 69 74 3c 3d 30 20  t) || iLimit<=0 
264c9 29 7b 0a 20 20 20 20 20 20 69 4c 69 6d 69 74 20  ){.      iLimit 
264ca 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20  = 0x7fffffff;.  
264cb 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42    }.    sqlite3B
264cc 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
264cd 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
264ce 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b);.    sqlite3V
264cf 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
264d0 49 6e 74 65 67 65 72 2c 20 69 4c 69 6d 69 74 2c  Integer, iLimit,
264d1 20 31 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20   1);.    addr = 
264d2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
264d3 31 28 76 2c 20 4f 50 5f 49 6e 63 72 56 61 63 75  1(v, OP_IncrVacu
264d4 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  um, iDb);.    sq
264d5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
264d6 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
264d7 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
264d8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
264d9 5f 41 64 64 49 6d 6d 2c 20 31 2c 20 2d 31 29 3b  _AddImm, 1, -1);
264da 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
264db 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50  AddOp2(v, OP_IfP
264dc 6f 73 2c 20 31 2c 20 61 64 64 72 29 3b 0a 20 20  os, 1, addr);.  
264dd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
264de 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
264df 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a    }else.#endif..
264e0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
264e1 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
264e2 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  S.  /*.  **  PRA
264e3 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 63  GMA [database.]c
264e4 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20  ache_size.  **  
264e5 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
264e6 2e 5d 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20  .]cache_size=N. 
264e7 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72   **.  ** The fir
264e8 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20  st form reports 
264e9 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61  the current loca
264ea 6c 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68  l setting for th
264eb 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68  e.  ** page cach
264ec 65 20 73 69 7a 65 2e 20 20 54 68 65 20 6c 6f 63  e size.  The loc
264ed 61 6c 20 73 65 74 74 69 6e 67 20 63 61 6e 20 62  al setting can b
264ee 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
264ef 0a 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73  .  ** the persis
264f0 74 65 6e 74 20 63 61 63 68 65 20 73 69 7a 65 20  tent cache size 
264f1 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 73 74  value that is st
264f2 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61  ored in the data
264f3 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 20 69  base.  ** file i
264f4 74 73 65 6c 66 2e 20 20 54 68 65 20 76 61 6c 75  tself.  The valu
264f5 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  e returned is th
264f6 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
264f7 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65 73 20 69   of.  ** pages i
264f8 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
264f9 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f  .  The second fo
264fa 72 6d 20 73 65 74 73 20 74 68 65 20 6c 6f 63 61  rm sets the loca
264fb 6c 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68  l.  ** page cach
264fc 65 20 73 69 7a 65 20 76 61 6c 75 65 2e 20 20 49  e size value.  I
264fd 74 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67  t does not chang
264fe 65 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  e the persistent
264ff 0a 20 20 2a 2a 20 63 61 63 68 65 20 73 69 7a 65  .  ** cache size
26500 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 64   stored on the d
26501 69 73 6b 20 73 6f 20 74 68 65 20 63 61 63 68 65  isk so the cache
26502 20 73 69 7a 65 20 77 69 6c 6c 20 72 65 76 65 72   size will rever
26503 74 0a 20 20 2a 2a 20 74 6f 20 69 74 73 20 64 65  t.  ** to its de
26504 66 61 75 6c 74 20 76 61 6c 75 65 20 77 68 65 6e  fault value when
26505 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
26506 20 63 6c 6f 73 65 64 20 61 6e 64 20 72 65 6f 70   closed and reop
26507 65 6e 65 64 2e 0a 20 20 2a 2a 20 4e 20 73 68 6f  ened..  ** N sho
26508 75 6c 64 20 62 65 20 61 20 70 6f 73 69 74 69 76  uld be a positiv
26509 65 20 69 6e 74 65 67 65 72 2e 0a 20 20 2a 2f 0a  e integer..  */.
2650a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2650b 49 43 6d 70 28 7a 4c 65 66 74 2c 22 63 61 63 68  ICmp(zLeft,"cach
2650c 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20  e_size")==0 ){. 
2650d 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
2650e 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
2650f 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
26510 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69  ut;.    if( !zRi
26511 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ght ){.      ret
26512 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
26513 72 73 65 2c 20 22 63 61 63 68 65 5f 73 69 7a 65  rse, "cache_size
26514 22 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  ", pDb->pSchema-
26515 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20  >cache_size);.  
26516 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
26517 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a  nt size = atoi(z
26518 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66  Right);.      if
26519 28 20 73 69 7a 65 3c 30 20 29 20 73 69 7a 65 20  ( size<0 ) size 
2651a 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20 20 20 70  = -size;.      p
2651b 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
2651c 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  he_size = size;.
2651d 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
2651e 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 70  eeSetCacheSize(p
2651f 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53  Db->pBt, pDb->pS
26520 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
26521 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  e);.    }.  }els
26522 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50  e..  /*.  **   P
26523 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65  RAGMA temp_store
26524 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74  .  **   PRAGMA t
26525 65 6d 70 5f 73 74 6f 72 65 20 3d 20 22 64 65 66  emp_store = "def
26526 61 75 6c 74 22 7c 22 6d 65 6d 6f 72 79 22 7c 22  ault"|"memory"|"
26527 66 69 6c 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20  file".  **.  ** 
26528 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68  Return or set th
26529 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66  e local value of
2652a 20 74 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 20   the temp_store 
2652b 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a  flag.  Changing.
2652c 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 76    ** the local v
2652d 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  alue does not ma
2652e 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  ke changes to th
2652f 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e 64 20  e disk file and 
26530 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20 2a 2a  the default.  **
26531 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 72   value will be r
26532 65 73 74 6f 72 65 64 20 74 68 65 20 6e 65 78 74  estored the next
26533 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62 61   time the databa
26534 73 65 20 69 73 20 6f 70 65 6e 65 64 2e 0a 20 20  se is opened..  
26535 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61  **.  ** Note tha
26536 74 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  t it is possible
26537 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79   for the library
26538 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70   compile-time op
26539 74 69 6f 6e 73 20 74 6f 0a 20 20 2a 2a 20 6f 76  tions to.  ** ov
2653a 65 72 72 69 64 65 20 74 68 69 73 20 73 65 74 74  erride this sett
2653b 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ing.  */.  if( s
2653c 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
2653d 65 66 74 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65  eft, "temp_store
2653e 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
2653f 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
26540 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
26541 74 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 5f  t(pParse, "temp_
26542 73 74 6f 72 65 22 2c 20 64 62 2d 3e 74 65 6d 70  store", db->temp
26543 5f 73 74 6f 72 65 29 3b 0a 20 20 20 20 7d 65 6c  _store);.    }el
26544 73 65 7b 0a 20 20 20 20 20 20 63 68 61 6e 67 65  se{.      change
26545 54 65 6d 70 53 74 6f 72 61 67 65 28 70 50 61 72  TempStorage(pPar
26546 73 65 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20 20  se, zRight);.   
26547 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a   }.  }else..  /*
26548 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74  .  **   PRAGMA t
26549 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
2654a 6f 72 79 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ory.  **   PRAGM
2654b 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72  A temp_store_dir
2654c 65 63 74 6f 72 79 20 3d 20 22 22 7c 22 64 69 72  ectory = ""|"dir
2654d 65 63 74 6f 72 79 5f 6e 61 6d 65 22 0a 20 20 2a  ectory_name".  *
2654e 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72  *.  ** Return or
2654f 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76   set the local v
26550 61 6c 75 65 20 6f 66 20 74 68 65 20 74 65 6d 70  alue of the temp
26551 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
26552 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67   flag.  Changing
26553 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  .  ** the value 
26554 73 65 74 73 20 61 20 73 70 65 63 69 66 69 63 20  sets a specific 
26555 64 69 72 65 63 74 6f 72 79 20 74 6f 20 62 65 20  directory to be 
26556 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61  used for tempora
26557 72 79 20 66 69 6c 65 73 2e 0a 20 20 2a 2a 20 53  ry files..  ** S
26558 65 74 74 69 6e 67 20 74 6f 20 61 20 6e 75 6c 6c  etting to a null
26559 20 73 74 72 69 6e 67 20 72 65 76 65 72 74 73 20   string reverts 
2655a 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 74  to the default t
2655b 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f  emporary directo
2655c 72 79 20 73 65 61 72 63 68 2e 0a 20 20 2a 2a 20  ry search..  ** 
2655d 49 66 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72  If temporary dir
2655e 65 63 74 6f 72 79 20 69 73 20 63 68 61 6e 67 65  ectory is change
2655f 64 2c 20 74 68 65 6e 20 69 6e 76 61 6c 69 64 61  d, then invalida
26560 74 65 54 65 6d 70 53 74 6f 72 61 67 65 2e 0a 20  teTempStorage.. 
26561 20 2a 2a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73   **.  */.  if( s
26562 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
26563 65 66 74 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65  eft, "temp_store
26564 5f 64 69 72 65 63 74 6f 72 79 22 29 3d 3d 30 20  _directory")==0 
26565 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  ){.    if( !zRig
26566 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ht ){.      if( 
26567 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
26568 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 20 20  ectory ){.      
26569 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2656a 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
2656b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2656c 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2656d 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
2656e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 74   .            "t
2656f 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
26570 6f 72 79 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ory", SQLITE_STA
26571 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
26572 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
26573 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
26574 2c 20 31 2c 20 30 2c 20 73 71 6c 69 74 65 33 5f  , 1, 0, sqlite3_
26575 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 2c 20  temp_directory, 
26576 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
26577 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26578 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
26579 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 1);.      }.  
2657a 20 20 7d 65 6c 73 65 7b 0a 23 69 66 6e 64 65 66    }else{.#ifndef
2657b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
2657c 0a 20 20 20 20 20 20 69 66 28 20 7a 52 69 67 68  .      if( zRigh
2657d 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  t[0] ){.        
2657e 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 20 20  int rc;.        
2657f 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 20  int res;.       
26580 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
26581 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73 2c 20  ccess(db->pVfs, 
26582 7a 52 69 67 68 74 2c 20 53 51 4c 49 54 45 5f 41  zRight, SQLITE_A
26583 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 2c  CCESS_READWRITE,
26584 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20   &res);.        
26585 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
26586 4b 20 7c 7c 20 72 65 73 3d 3d 30 20 29 7b 0a 20  K || res==0 ){. 
26587 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
26588 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
26589 20 22 6e 6f 74 20 61 20 77 72 69 74 61 62 6c 65   "not a writable
2658a 20 64 69 72 65 63 74 6f 72 79 22 29 3b 0a 20 20   directory");.  
2658b 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61          goto pra
2658c 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  gma_out;.       
2658d 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
2658e 20 69 66 28 20 53 51 4c 49 54 45 5f 54 45 4d 50   if( SQLITE_TEMP
2658f 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20 20 20 20  _STORE==0.      
26590 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54 45 4d 50   || (SQLITE_TEMP
26591 5f 53 54 4f 52 45 3d 3d 31 20 26 26 20 64 62 2d  _STORE==1 && db-
26592 3e 74 65 6d 70 5f 73 74 6f 72 65 3c 3d 31 29 0a  >temp_store<=1).
26593 20 20 20 20 20 20 20 7c 7c 20 28 53 51 4c 49 54         || (SQLIT
26594 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 20  E_TEMP_STORE==2 
26595 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72  && db->temp_stor
26596 65 3d 3d 31 29 0a 20 20 20 20 20 20 29 7b 0a 20  e==1).      ){. 
26597 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74         invalidat
26598 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70 50 61  eTempStorage(pPa
26599 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rse);.      }.  
2659a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2659b 28 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69  (sqlite3_temp_di
2659c 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 20 20  rectory);.      
2659d 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b  if( zRight[0] ){
2659e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2659f 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20  _temp_directory 
265a0 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
265a1 70 28 30 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20  p(0, zRight);.  
265a2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
265a3 20 20 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f     sqlite3_temp_
265a4 64 69 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a 20  directory = 0;. 
265a5 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a       }.#endif /*
265a6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
265a7 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   */.    }.  }els
265a8 65 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  e..#if !defined(
265a9 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
265aa 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20  CKING_STYLE).#  
265ab 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  if defined(__APP
265ac 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e  LE__).#    defin
265ad 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
265ae 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a  LOCKING_STYLE 1.
265af 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66  #  else.#    def
265b0 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ine SQLITE_ENABL
265b1 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
265b2 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69  0.#  endif.#endi
265b3 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  f.#if SQLITE_ENA
265b4 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
265b5 45 0a 20 20 2f 2a 0a 20 20 20 2a 2a 20 20 20 50  E.  /*.   **   P
265b6 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
265b7 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65  ]lock_proxy_file
265b8 0a 20 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  .   **   PRAGMA 
265b9 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f  [database.]lock_
265ba 70 72 6f 78 79 5f 66 69 6c 65 20 3d 20 22 3a 61  proxy_file = ":a
265bb 75 74 6f 3a 22 7c 22 6c 6f 63 6b 5f 66 69 6c 65  uto:"|"lock_file
265bc 5f 70 61 74 68 22 0a 20 20 20 2a 2a 0a 20 20 20  _path".   **.   
265bd 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74  ** Return or set
265be 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
265bf 65 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c  e lock_proxy_fil
265c0 65 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e  e flag.  Changin
265c1 67 0a 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75  g.   ** the valu
265c2 65 20 73 65 74 73 20 61 20 73 70 65 63 69 66 69  e sets a specifi
265c3 63 20 66 69 6c 65 20 74 6f 20 62 65 20 75 73 65  c file to be use
265c4 64 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 61  d for database a
265c5 63 63 65 73 73 20 6c 6f 63 6b 73 2e 0a 20 20 20  ccess locks..   
265c6 2a 2a 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 73  **.   */.  if( s
265c7 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
265c8 65 66 74 2c 20 22 6c 6f 63 6b 5f 70 72 6f 78 79  eft, "lock_proxy
265c9 5f 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20  _file")==0 ){.  
265ca 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
265cb 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50  .      Pager *pP
265cc 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
265cd 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42  reePager(pDb->pB
265ce 74 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  t);.      char *
265cf 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68 20  proxy_file_path 
265d0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 73 71  = NULL;.      sq
265d1 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
265d2 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  e = sqlite3Pager
265d3 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20  File(pPager);.  
265d4 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c      sqlite3OsFil
265d5 65 43 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20  eControl(pFile, 
265d6 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50  SQLITE_GET_LOCKP
265d7 52 4f 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20  ROXYFILE, .     
265d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
265d9 20 20 20 20 20 20 26 70 72 6f 78 79 5f 66 69 6c        &proxy_fil
265da 65 5f 70 61 74 68 29 3b 0a 20 20 20 20 20 20 0a  e_path);.      .
265db 20 20 20 20 20 20 69 66 28 20 70 72 6f 78 79 5f        if( proxy_
265dc 66 69 6c 65 5f 70 61 74 68 20 29 7b 0a 20 20 20  file_path ){.   
265dd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
265de 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
265df 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
265e0 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
265e1 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
265e2 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ME, .           
265e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
265e4 20 20 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66     "lock_proxy_f
265e5 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ile", SQLITE_STA
265e6 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
265e7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
265e8 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
265e9 2c 20 31 2c 20 30 2c 20 70 72 6f 78 79 5f 66 69  , 1, 0, proxy_fi
265ea 6c 65 5f 70 61 74 68 2c 20 30 29 3b 0a 20 20 20  le_path, 0);.   
265eb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
265ec 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
265ed 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20  ultRow, 1, 1);. 
265ee 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
265ef 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70  {.      Pager *p
265f0 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
265f1 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70  treePager(pDb->p
265f2 42 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Bt);.      sqlit
265f3 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d  e3_file *pFile =
265f4 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c   sqlite3PagerFil
265f5 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  e(pPager);.     
265f6 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20   int res;.      
265f7 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b  if( zRight[0] ){
265f8 0a 20 20 20 20 20 20 20 20 72 65 73 3d 73 71 6c  .        res=sql
265f9 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f  ite3OsFileContro
265fa 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f  l(pFile, SQLITE_
265fb 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  SET_LOCKPROXYFIL
265fc 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
265fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
265fe 20 20 20 20 20 20 20 20 20 7a 52 69 67 68 74 29           zRight)
265ff 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b  ;.      } else {
26600 0a 20 20 20 20 20 20 20 20 72 65 73 3d 73 71 6c  .        res=sql
26601 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f  ite3OsFileContro
26602 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f  l(pFile, SQLITE_
26603 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  SET_LOCKPROXYFIL
26604 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
26605 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26606 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 29 3b 0a           NULL);.
26607 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
26608 28 20 72 65 73 21 3d 53 51 4c 49 54 45 5f 4f 4b  ( res!=SQLITE_OK
26609 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2660a 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2660b 73 65 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 73  se, "failed to s
2660c 65 74 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69  et lock proxy fi
2660d 6c 65 22 29 3b 0a 20 20 20 20 20 20 20 20 67 6f  le");.        go
2660e 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
2660f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
26610 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
26611 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
26612 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 20 20 20  KING_STYLE */   
26613 20 20 20 0a 20 20 20 20 0a 20 20 2f 2a 0a 20 20     .    .  /*.  
26614 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74  **   PRAGMA [dat
26615 61 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e 6f  abase.]synchrono
26616 75 73 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  us.  **   PRAGMA
26617 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e 63   [database.]sync
26618 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c 4f 4e 7c 4e  hronous=OFF|ON|N
26619 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20 20 2a 2a 0a  ORMAL|FULL.  **.
2661a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73    ** Return or s
2661b 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c  et the local val
2661c 75 65 20 6f 66 20 74 68 65 20 73 79 6e 63 68 72  ue of the synchr
2661d 6f 6e 6f 75 73 20 66 6c 61 67 2e 20 20 43 68 61  onous flag.  Cha
2661e 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c  nging.  ** the l
2661f 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73 20  ocal value does 
26620 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65 73  not make changes
26621 20 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69 6c   to the disk fil
26622 65 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 64  e and the.  ** d
26623 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77 69 6c  efault value wil
26624 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 68  l be restored th
26625 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20  e next time the 
26626 64 61 74 61 62 61 73 65 20 69 73 0a 20 20 2a 2a  database is.  **
26627 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20   opened..  */.  
26628 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
26629 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e 63 68 72  mp(zLeft,"synchr
2662a 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20  onous")==0 ){.  
2662b 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
2662c 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
2662d 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
2662e 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  t;.    if( !zRig
2662f 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ht ){.      retu
26630 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
26631 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e 6f 75 73  se, "synchronous
26632 22 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c  ", pDb->safety_l
26633 65 76 65 6c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c  evel-1);.    }el
26634 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 64  se{.      if( !d
26635 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
26636 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
26637 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
26638 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 53   .            "S
26639 61 66 65 74 79 20 6c 65 76 65 6c 20 6d 61 79 20  afety level may 
2663a 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 69  not be changed i
2663b 6e 73 69 64 65 20 61 20 74 72 61 6e 73 61 63 74  nside a transact
2663c 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ion");.      }el
2663d 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 62 2d  se{.        pDb-
2663e 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20  >safety_level = 
2663f 67 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 7a  getSafetyLevel(z
26640 52 69 67 68 74 29 2b 31 3b 0a 20 20 20 20 20 20  Right)+1;.      
26641 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
26642 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
26643 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
26644 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  MAS */..#ifndef 
26645 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47  SQLITE_OMIT_FLAG
26646 5f 50 52 41 47 4d 41 53 0a 20 20 69 66 28 20 66  _PRAGMAS.  if( f
26647 6c 61 67 50 72 61 67 6d 61 28 70 50 61 72 73 65  lagPragma(pParse
26648 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 29  , zLeft, zRight)
26649 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 66   ){.    /* The f
2664a 6c 61 67 50 72 61 67 6d 61 28 29 20 73 75 62 72  lagPragma() subr
2664b 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65  outine also gene
2664c 72 61 74 65 73 20 61 6e 79 20 6e 65 63 65 73 73  rates any necess
2664d 61 72 79 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20  ary code.    ** 
2664e 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67  there is nothing
2664f 20 6d 6f 72 65 20 74 6f 20 64 6f 20 68 65 72 65   more to do here
26650 20 2a 2f 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64   */.  }else.#end
26651 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
26652 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 20 2a  T_FLAG_PRAGMAS *
26653 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
26654 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52  E_OMIT_SCHEMA_PR
26655 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20  AGMAS.  /*.  ** 
26656 20 20 50 52 41 47 4d 41 20 74 61 62 6c 65 5f 69    PRAGMA table_i
26657 6e 66 6f 28 3c 74 61 62 6c 65 3e 29 0a 20 20 2a  nfo(<table>).  *
26658 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 20  *.  ** Return a 
26659 73 69 6e 67 6c 65 20 72 6f 77 20 66 6f 72 20 65  single row for e
2665a 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ach column of th
2665b 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 20 54  e named table. T
2665c 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20  he columns of.  
2665d 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  ** the returned 
2665e 64 61 74 61 20 73 65 74 20 61 72 65 3a 0a 20 20  data set are:.  
2665f 2a 2a 0a 20 20 2a 2a 20 63 69 64 3a 20 20 20 20  **.  ** cid:    
26660 20 20 20 20 43 6f 6c 75 6d 6e 20 69 64 20 28 6e      Column id (n
26661 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 6c 65 66  umbered from lef
26662 74 20 74 6f 20 72 69 67 68 74 2c 20 73 74 61 72  t to right, star
26663 74 69 6e 67 20 61 74 20 30 29 0a 20 20 2a 2a 20  ting at 0).  ** 
26664 6e 61 6d 65 3a 20 20 20 20 20 20 20 43 6f 6c 75  name:       Colu
26665 6d 6e 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 79 70  mn name.  ** typ
26666 65 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  e:       Column 
26667 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
26668 2e 0a 20 20 2a 2a 20 6e 6f 74 6e 75 6c 6c 3a 20  ..  ** notnull: 
26669 20 20 20 54 72 75 65 20 69 66 20 27 4e 4f 54 20     True if 'NOT 
2666a 4e 55 4c 4c 27 20 69 73 20 70 61 72 74 20 6f 66  NULL' is part of
2666b 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
2666c 69 6f 6e 0a 20 20 2a 2a 20 64 66 6c 74 5f 76 61  ion.  ** dflt_va
2666d 6c 75 65 3a 20 54 68 65 20 64 65 66 61 75 6c 74  lue: The default
2666e 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63   value for the c
2666f 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e 79 2e 0a 20  olumn, if any.. 
26670 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
26671 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
26672 22 74 61 62 6c 65 5f 69 6e 66 6f 22 29 3d 3d 30  "table_info")==0
26673 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20   && zRight ){.  
26674 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
26675 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
26676 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
26677 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
26678 75 74 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73  ut;.    pTab = s
26679 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
2667a 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29  db, zRight, zDb)
2667b 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 29  ;.    if( pTab )
2667c 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
2667d 20 20 20 20 20 69 6e 74 20 6e 48 69 64 64 65 6e       int nHidden
2667e 20 3d 20 30 3b 0a 20 20 20 20 20 20 43 6f 6c 75   = 0;.      Colu
2667f 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20  mn *pCol;.      
26680 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
26681 6d 43 6f 6c 73 28 76 2c 20 36 29 3b 0a 20 20 20  mCols(v, 6);.   
26682 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
26683 3d 20 36 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 6;.      sqlit
26684 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
26685 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
26686 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49  AME, "cid", SQLI
26687 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
26688 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
26689 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f  ColName(v, 1, CO
2668a 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
2668b 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
2668c 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
2668d 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2668e 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
2668f 4d 45 2c 20 22 74 79 70 65 22 2c 20 53 51 4c 49  ME, "type", SQLI
26690 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
26691 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
26692 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f  ColName(v, 3, CO
26693 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f 74  LNAME_NAME, "not
26694 6e 75 6c 6c 22 2c 20 53 51 4c 49 54 45 5f 53 54  null", SQLITE_ST
26695 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
26696 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
26697 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45  me(v, 4, COLNAME
26698 5f 4e 41 4d 45 2c 20 22 64 66 6c 74 5f 76 61 6c  _NAME, "dflt_val
26699 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ue", SQLITE_STAT
2669a 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
2669b 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2669c 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 5, COLNAME_N
2669d 41 4d 45 2c 20 22 70 6b 22 2c 20 53 51 4c 49 54  AME, "pk", SQLIT
2669e 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
2669f 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
266a0 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
266a1 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20  e, pTab);.      
266a2 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 70 54  for(i=0, pCol=pT
266a3 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c 70 54 61 62  ab->aCol; i<pTab
266a4 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  ->nCol; i++, pCo
266a5 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  l++){.        if
266a6 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e  ( IsHiddenColumn
266a7 28 70 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  (pCol) ){.      
266a8 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20      nHidden++;. 
266a9 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
266aa 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
266ab 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
266ac 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
266ad 65 67 65 72 2c 20 69 2d 6e 48 69 64 64 65 6e 2c  eger, i-nHidden,
266ae 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
266af 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
266b0 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
266b1 20 32 2c 20 30 2c 20 70 43 6f 6c 2d 3e 7a 4e 61   2, 0, pCol->zNa
266b2 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  me, 0);.        
266b3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
266b4 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
266b5 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20 20   0, 3, 0,.      
266b6 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65       pCol->zType
266b7 20 3f 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3a   ? pCol->zType :
266b8 20 22 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20   "", 0);.       
266b9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
266ba 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
266bb 2c 20 28 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c  , (pCol->notNull
266bc 20 3f 20 31 20 3a 20 30 29 2c 20 34 29 3b 0a 20   ? 1 : 0), 4);. 
266bd 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
266be 3e 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 20 20  >pDflt ){.      
266bf 20 20 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20      const Token 
266c0 2a 70 20 3d 20 26 70 43 6f 6c 2d 3e 70 44 66 6c  *p = &pCol->pDfl
266c1 74 2d 3e 73 70 61 6e 3b 0a 20 20 20 20 20 20 20  t->span;.       
266c2 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 20     assert( p->z 
266c3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
266c4 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
266c5 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
266c6 20 35 2c 20 30 2c 20 28 63 68 61 72 2a 29 70 2d   5, 0, (char*)p-
266c7 3e 7a 2c 20 70 2d 3e 6e 29 3b 0a 20 20 20 20 20  >z, p->n);.     
266c8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
266c9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
266ca 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
266cb 2c 20 30 2c 20 35 29 3b 0a 20 20 20 20 20 20 20  , 0, 5);.       
266cc 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
266cd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
266ce 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c  OP_Integer, pCol
266cf 2d 3e 69 73 50 72 69 6d 4b 65 79 2c 20 36 29 3b  ->isPrimKey, 6);
266d0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
266d1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
266d2 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 36  _ResultRow, 1, 6
266d3 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
266d4 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20  .  }else..  if( 
266d5 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
266d6 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e 66  Left, "index_inf
266d7 6f 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  o")==0 && zRight
266d8 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   ){.    Index *p
266d9 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  Idx;.    Table *
266da 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71  pTab;.    if( sq
266db 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
266dc 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
266dd 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70  ragma_out;.    p
266de 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Idx = sqlite3Fin
266df 64 49 6e 64 65 78 28 64 62 2c 20 7a 52 69 67 68  dIndex(db, zRigh
266e0 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28  t, zDb);.    if(
266e1 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69   pIdx ){.      i
266e2 6e 74 20 69 3b 0a 20 20 20 20 20 20 70 54 61 62  nt i;.      pTab
266e3 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b   = pIdx->pTable;
266e4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
266e5 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
266e6 33 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  3);.      pParse
266e7 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
266e8 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
266e9 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
266ea 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71  LNAME_NAME, "seq
266eb 6e 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  no", SQLITE_STAT
266ec 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
266ed 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
266ee 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 1, COLNAME_N
266ef 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49  AME, "cid", SQLI
266f0 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
266f1 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
266f2 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f  ColName(v, 2, CO
266f3 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
266f4 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
266f5 43 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  C);.      for(i=
266f6 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; i<pIdx->nColu
266f7 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
266f8 20 20 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49 64    int cnum = pId
266f9 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a  x->aiColumn[i];.
266fa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
266fb 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
266fc 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a  Integer, i, 1);.
266fd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
266fe 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
266ff 49 6e 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20 32  Integer, cnum, 2
26700 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
26701 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63 6e  t( pTab->nCol>cn
26702 75 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  um );.        sq
26703 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
26704 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
26705 2c 20 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61 43  , 3, 0, pTab->aC
26706 6f 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c 20  ol[cnum].zName, 
26707 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
26708 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26709 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
2670a 2c 20 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 3);.      }.  
2670b 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69    }.  }else..  i
2670c 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2670d 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f  p(zLeft, "index_
2670e 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69  list")==0 && zRi
2670f 67 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78  ght ){.    Index
26710 20 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c   *pIdx;.    Tabl
26711 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28  e *pTab;.    if(
26712 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
26713 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74  ma(pParse) ) got
26714 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
26715 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
26716 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52  FindTable(db, zR
26717 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20  ight, zDb);.    
26718 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
26719 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
2671a 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
2671b 20 20 20 20 70 49 64 78 20 3d 20 70 54 61 62 2d      pIdx = pTab-
2671c 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 69  >pIndex;.      i
2671d 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
2671e 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20     int i = 0; . 
2671f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26720 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
26721 33 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72  3);.        pPar
26722 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20  se->nMem = 3;.  
26723 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26724 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
26725 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
26726 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54  "seq", SQLITE_ST
26727 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
26728 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
26729 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
2672a 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c  ME_NAME, "name",
2672b 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2672c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2672d 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
2672e 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 2, COLNAME_NAM
2672f 45 2c 20 22 75 6e 69 71 75 65 22 2c 20 53 51 4c  E, "unique", SQL
26730 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
26731 20 20 20 20 20 77 68 69 6c 65 28 70 49 64 78 29       while(pIdx)
26732 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
26733 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26734 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
26735 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
26736 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
26737 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
26738 2c 20 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e  , 2, 0, pIdx->zN
26739 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ame, 0);.       
2673a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2673b 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2673c 65 72 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  er, pIdx->onErro
2673d 72 21 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0a  r!=OE_None, 3);.
2673e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2673f 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26740 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
26741 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b  3);.          ++
26742 69 3b 0a 20 20 20 20 20 20 20 20 20 20 70 49 64  i;.          pId
26743 78 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b  x = pIdx->pNext;
26744 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
26745 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
26746 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
26747 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64  trICmp(zLeft, "d
26748 61 74 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d  atabase_list")==
26749 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
2674a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
2674b 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
2674c 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
2674d 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  out;.    sqlite3
2674e 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
2674f 2c 20 33 29 3b 0a 20 20 20 20 70 50 61 72 73 65  , 3);.    pParse
26750 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
26751 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
26752 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
26753 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c  AME_NAME, "seq",
26754 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
26755 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26756 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
26757 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
26758 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  name", SQLITE_ST
26759 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  ATIC);.    sqlit
2675a 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2675b 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 2, COLNAME_N
2675c 41 4d 45 2c 20 22 66 69 6c 65 22 2c 20 53 51 4c  AME, "file", SQL
2675d 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
2675e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
2675f 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
26760 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
26761 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  pBt==0 ) continu
26762 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
26763 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
26764 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  e!=0 );.      sq
26765 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26766 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
26767 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
26768 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
26769 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
2676a 32 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d  2, 0, db->aDb[i]
2676b 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  .zName, 0);.    
2676c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2676d 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
2676e 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20  8, 0, 3, 0,.    
2676f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
26770 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64  reeGetFilename(d
26771 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c 20  b->aDb[i].pBt), 
26772 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
26773 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26774 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
26775 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  3);.    }.  }els
26776 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  e..  if( sqlite3
26777 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
26778 63 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22 29  collation_list")
26779 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ==0 ){.    int i
2677a 20 3d 20 30 3b 0a 20 20 20 20 48 61 73 68 45 6c   = 0;.    HashEl
2677b 65 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74  em *p;.    sqlit
2677c 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
2677d 28 76 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72  (v, 2);.    pPar
2677e 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20  se->nMem = 2;.  
2677f 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
26780 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
26781 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71  LNAME_NAME, "seq
26782 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
26783 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
26784 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
26785 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
26786 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
26787 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72  STATIC);.    for
26788 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (p=sqliteHashFir
26789 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71  st(&db->aCollSeq
2678a 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61  ); p; p=sqliteHa
2678b 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20  shNext(p)){.    
2678c 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2678d 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71   = (CollSeq *)sq
2678e 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b  liteHashData(p);
2678f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26790 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
26791 6e 74 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29 3b  nteger, i++, 1);
26792 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26793 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
26794 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c  tring8, 0, 2, 0,
26795 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 30   pColl->zName, 0
26796 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26797 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26798 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32  _ResultRow, 1, 2
26799 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
2679a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2679b 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52  E_OMIT_SCHEMA_PR
2679c 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65  AGMAS */..#ifnde
2679d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
2679e 52 45 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28 20  REIGN_KEY.  if( 
2679f 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
267a0 4c 65 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f 6b  Left, "foreign_k
267a1 65 79 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20  ey_list")==0 && 
267a2 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 46 4b  zRight ){.    FK
267a3 65 79 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61 62  ey *pFK;.    Tab
267a4 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66  le *pTab;.    if
267a5 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
267a6 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
267a7 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
267a8 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
267a9 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
267aa 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20  Right, zDb);.   
267ab 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
267ac 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
267ad 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
267ae 20 20 20 20 20 70 46 4b 20 3d 20 70 54 61 62 2d       pFK = pTab-
267af 3e 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 69 66  >pFKey;.      if
267b0 28 20 70 46 4b 20 29 7b 0a 20 20 20 20 20 20 20  ( pFK ){.       
267b1 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20   int i = 0; .   
267b2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
267b3 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38 29  SetNumCols(v, 8)
267b4 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  ;.        pParse
267b5 2d 3e 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20 20  ->nMem = 8;.    
267b6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
267b7 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
267b8 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69  COLNAME_NAME, "i
267b9 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  d", SQLITE_STATI
267ba 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  C);.        sqli
267bb 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
267bc 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
267bd 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c  NAME, "seq", SQL
267be 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
267bf 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
267c0 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c  SetColName(v, 2,
267c1 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
267c2 74 61 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53  table", SQLITE_S
267c3 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
267c4 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
267c5 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e  lName(v, 3, COLN
267c6 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22  AME_NAME, "from"
267c7 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
267c8 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
267c9 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
267ca 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 4, COLNAME_NA
267cb 4d 45 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54 45  ME, "to", SQLITE
267cc 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
267cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
267ce 43 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f  ColName(v, 5, CO
267cf 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f  LNAME_NAME, "on_
267d0 75 70 64 61 74 65 22 2c 20 53 51 4c 49 54 45 5f  update", SQLITE_
267d1 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
267d2 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
267d3 6f 6c 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f 4c  olName(v, 6, COL
267d4 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 64  NAME_NAME, "on_d
267d5 65 6c 65 74 65 22 2c 20 53 51 4c 49 54 45 5f 53  elete", SQLITE_S
267d6 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
267d7 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
267d8 6c 4e 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c 4e  lName(v, 7, COLN
267d9 41 4d 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63 68  AME_NAME, "match
267da 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
267db 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  );.        while
267dc 28 70 46 4b 29 7b 0a 20 20 20 20 20 20 20 20 20  (pFK){.         
267dd 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20   int j;.        
267de 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b    for(j=0; j<pFK
267df 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
267e0 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
267e1 7a 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f 6c  zCol = pFK->aCol
267e2 5b 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20 20  [j].zCol;.      
267e3 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e 55        char *zOnU
267e4 70 64 61 74 65 20 3d 20 28 63 68 61 72 20 2a 29  pdate = (char *)
267e5 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e  actionName(pFK->
267e6 75 70 64 61 74 65 43 6f 6e 66 29 3b 0a 20 20 20  updateConf);.   
267e7 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
267e8 4f 6e 44 65 6c 65 74 65 20 3d 20 28 63 68 61 72  OnDelete = (char
267e9 20 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46   *)actionName(pF
267ea 4b 2d 3e 64 65 6c 65 74 65 43 6f 6e 66 29 3b 0a  K->deleteConf);.
267eb 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
267ec 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
267ed 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
267ee 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
267ef 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
267f0 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
267f1 20 6a 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20   j, 2);.        
267f2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
267f3 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
267f4 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 46  ng8, 0, 3, 0, pF
267f5 4b 2d 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20 20  K->zTo, 0);.    
267f6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
267f7 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
267f8 53 74 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20 30  String8, 0, 4, 0
267f9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
267fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
267fb 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d 3e  pTab->aCol[pFK->
267fc 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a  aCol[j].iFrom].z
267fd 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
267fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
267ff 65 41 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c 20  eAddOp4(v, zCol 
26800 3f 20 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20 4f  ? OP_String8 : O
26801 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30 2c  P_Null, 0, 5, 0,
26802 20 7a 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20   zCol, 0);.     
26803 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26804 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
26805 74 72 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30 2c  tring8, 0, 6, 0,
26806 20 7a 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b 0a   zOnUpdate, 0);.
26807 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
26808 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
26809 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
2680a 37 2c 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65 2c  7, 0, zOnDelete,
2680b 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
2680c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2680d 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
2680e 2c 20 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e 45  , 0, 8, 0, "NONE
2680f 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  ", 0);.         
26810 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26811 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
26812 74 52 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20 20  tRow, 1, 8);.   
26813 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
26814 20 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20     ++i;.        
26815 20 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e 65    pFK = pFK->pNe
26816 78 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20  xtFrom;.        
26817 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
26818 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
26819 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
2681a 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
2681b 45 59 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  EY) */..#ifndef 
2681c 4e 44 45 42 55 47 0a 20 20 69 66 28 20 73 71 6c  NDEBUG.  if( sql
2681d 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
2681e 74 2c 20 22 70 61 72 73 65 72 5f 74 72 61 63 65  t, "parser_trace
2681f 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
26820 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20   zRight ){.     
26821 20 69 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28   if( getBoolean(
26822 7a 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  zRight) ){.     
26823 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
26824 54 72 61 63 65 28 73 74 64 65 72 72 2c 20 22 70  Trace(stderr, "p
26825 61 72 73 65 72 3a 20 22 29 3b 0a 20 20 20 20 20  arser: ");.     
26826 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
26827 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72 61  sqlite3ParserTra
26828 63 65 28 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  ce(0, 0);.      
26829 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
2682a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 69  #endif..  /* Rei
2682b 6e 73 74 61 6c 6c 20 74 68 65 20 4c 49 4b 45 20  nstall the LIKE 
2682c 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f  and GLOB functio
2682d 6e 73 2e 20 20 54 68 65 20 76 61 72 69 61 6e 74  ns.  The variant
2682e 20 6f 66 20 4c 49 4b 45 0a 20 20 2a 2a 20 75 73   of LIKE.  ** us
2682f 65 64 20 77 69 6c 6c 20 62 65 20 63 61 73 65 20  ed will be case 
26830 73 65 6e 73 69 74 69 76 65 20 6f 72 20 6e 6f 74  sensitive or not
26831 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
26832 65 20 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 69 66  e RHS..  */.  if
26833 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
26834 28 7a 4c 65 66 74 2c 20 22 63 61 73 65 5f 73 65  (zLeft, "case_se
26835 6e 73 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d 3d  nsitive_like")==
26836 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 52 69  0 ){.    if( zRi
26837 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ght ){.      sql
26838 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65  ite3RegisterLike
26839 46 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 67 65  Functions(db, ge
2683a 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29  tBoolean(zRight)
2683b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
2683c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2683d 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
2683e 5f 45 52 52 4f 52 5f 4d 41 58 0a 23 20 64 65 66  _ERROR_MAX.# def
2683f 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  ine SQLITE_INTEG
26840 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52  RITY_CHECK_ERROR
26841 5f 4d 41 58 20 31 30 30 0a 23 65 6e 64 69 66 0a  _MAX 100.#endif.
26842 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
26843 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
26844 48 45 43 4b 0a 20 20 2f 2a 20 50 72 61 67 6d 61  HECK.  /* Pragma
26845 20 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 20 69   "quick_check" i
26846 73 20 61 6e 20 65 78 70 65 72 69 6d 65 6e 74 61  s an experimenta
26847 6c 20 72 65 64 75 63 65 64 20 76 65 72 73 69 6f  l reduced versio
26848 6e 20 6f 66 20 0a 20 20 2a 2a 20 69 6e 74 65 67  n of .  ** integ
26849 72 69 74 79 5f 63 68 65 63 6b 20 64 65 73 69 67  rity_check desig
2684a 6e 65 64 20 74 6f 20 64 65 74 65 63 74 20 6d 6f  ned to detect mo
2684b 73 74 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  st database corr
2684c 75 70 74 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68  uption.  ** with
2684d 6f 75 74 20 6d 6f 73 74 20 6f 66 20 74 68 65 20  out most of the 
2684e 6f 76 65 72 68 65 61 64 20 6f 66 20 61 20 66 75  overhead of a fu
2684f 6c 6c 20 69 6e 74 65 67 72 69 74 79 2d 63 68 65  ll integrity-che
26850 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ck..  */.  if( s
26851 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
26852 65 66 74 2c 20 22 69 6e 74 65 67 72 69 74 79 5f  eft, "integrity_
26853 63 68 65 63 6b 22 29 3d 3d 30 0a 20 20 20 7c 7c  check")==0.   ||
26854 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
26855 7a 4c 65 66 74 2c 20 22 71 75 69 63 6b 5f 63 68  zLeft, "quick_ch
26856 65 63 6b 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20  eck")==0 .  ){. 
26857 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 61 64 64     int i, j, add
26858 72 2c 20 6d 78 45 72 72 3b 0a 0a 20 20 20 20 2f  r, mxErr;..    /
26859 2a 20 43 6f 64 65 20 74 68 61 74 20 61 70 70 65  * Code that appe
2685a 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ars at the end o
2685b 66 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20  f the integrity 
2685c 63 68 65 63 6b 2e 20 20 49 66 20 6e 6f 20 65 72  check.  If no er
2685d 72 6f 72 0a 20 20 20 20 2a 2a 20 6d 65 73 73 61  ror.    ** messa
2685e 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 67 65  ges have been ge
2685f 6e 65 72 61 74 65 64 2c 20 6f 75 74 70 75 74 20  nerated, output 
26860 4f 4b 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6f  OK.  Otherwise o
26861 75 74 70 75 74 20 74 68 65 0a 20 20 20 20 2a 2a  utput the.    **
26862 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20   error message. 
26863 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63     */.    static
26864 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73   const VdbeOpLis
26865 74 20 65 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b 0a  t endCode[] = {.
26866 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d        { OP_AddIm
26867 6d 2c 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20  m,      1, 0,   
26868 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30       0},    /* 0
26869 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49   */.      { OP_I
2686a 66 4e 65 67 2c 20 20 20 20 20 20 20 31 2c 20 30  fNeg,       1, 0
2686b 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
2686c 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20  /* 1 */.      { 
2686d 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
2686e 30 2c 20 33 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 3,        0},
2686f 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20      /* 2 */.    
26870 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77    { OP_ResultRow
26871 2c 20 20 20 33 2c 20 31 2c 20 20 20 20 20 20 20  ,   3, 1,       
26872 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 0a 20 20 20   0},.    };..   
26873 20 69 6e 74 20 69 73 51 75 69 63 6b 20 3d 20 28   int isQuick = (
26874 7a 4c 65 66 74 5b 30 5d 3d 3d 27 71 27 29 3b 0a  zLeft[0]=='q');.
26875 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
26876 7a 65 20 74 68 65 20 56 44 42 45 20 70 72 6f 67  ze the VDBE prog
26877 72 61 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20 73  ram */.    if( s
26878 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
26879 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
2687a 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
2687b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36  pParse->nMem = 6
2687c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2687d 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
2687e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2687f 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
26880 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
26881 20 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63   "integrity_chec
26882 6b 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  k", SQLITE_STATI
26883 43 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20  C);..    /* Set 
26884 74 68 65 20 6d 61 78 69 6d 75 6d 20 65 72 72 6f  the maximum erro
26885 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 6d  r count */.    m
26886 78 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e  xErr = SQLITE_IN
26887 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52  TEGRITY_CHECK_ER
26888 52 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66 28  ROR_MAX;.    if(
26889 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20   zRight ){.     
2688a 20 6d 78 45 72 72 20 3d 20 61 74 6f 69 28 7a 52   mxErr = atoi(zR
2688b 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28  ight);.      if(
2688c 20 6d 78 45 72 72 3c 3d 30 20 29 7b 0a 20 20 20   mxErr<=0 ){.   
2688d 20 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c       mxErr = SQL
2688e 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  ITE_INTEGRITY_CH
2688f 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20  ECK_ERROR_MAX;. 
26890 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
26891 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26892 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
26893 2c 20 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f 2a  , mxErr, 1);  /*
26894 20 72 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65 72   reg[1] holds er
26895 72 6f 72 73 20 6c 65 66 74 20 2a 2f 0a 0a 20 20  rors left */..  
26896 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67    /* Do an integ
26897 72 69 74 79 20 63 68 65 63 6b 20 6f 6e 20 65 61  rity check on ea
26898 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ch database file
26899 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
2689a 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
2689b 7b 0a 20 20 20 20 20 20 48 61 73 68 45 6c 65 6d  {.      HashElem
2689c 20 2a 78 3b 0a 20 20 20 20 20 20 48 61 73 68 20   *x;.      Hash 
2689d 2a 70 54 62 6c 73 3b 0a 20 20 20 20 20 20 69 6e  *pTbls;.      in
2689e 74 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20  t cnt = 0;..    
2689f 20 20 69 66 28 20 4f 4d 49 54 5f 54 45 4d 50 44    if( OMIT_TEMPD
268a0 42 20 26 26 20 69 3d 3d 31 20 29 20 63 6f 6e 74  B && i==1 ) cont
268a1 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 73 71 6c  inue;..      sql
268a2 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
268a3 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b  hema(pParse, i);
268a4 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
268a5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
268a6 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b  v, OP_IfPos, 1);
268a7 20 2f 2a 20 48 61 6c 74 20 69 66 20 6f 75 74 20   /* Halt if out 
268a8 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  of errors */.   
268a9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
268aa 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  dOp2(v, OP_Halt,
268ab 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71   0, 0);.      sq
268ac 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
268ad 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20  e(v, addr);..   
268ae 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65     /* Do an inte
268af 67 72 69 74 79 20 63 68 65 63 6b 20 6f 66 20 74  grity check of t
268b0 68 65 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20  he B-Tree.      
268b1 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 42 65 67 69  **.      ** Begi
268b2 6e 20 62 79 20 66 69 6c 6c 69 6e 67 20 72 65 67  n by filling reg
268b3 69 73 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e 2e  isters 2, 3, ...
268b4 20 77 69 74 68 20 74 68 65 20 72 6f 6f 74 20 70   with the root p
268b5 61 67 65 73 20 6e 75 6d 62 65 72 73 0a 20 20 20  ages numbers.   
268b6 20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61     ** for all ta
268b7 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73  bles and indices
268b8 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
268b9 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
268ba 20 70 54 62 6c 73 20 3d 20 26 64 62 2d 3e 61 44   pTbls = &db->aD
268bb 62 5b 69 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62  b[i].pSchema->tb
268bc 6c 48 61 73 68 3b 0a 20 20 20 20 20 20 66 6f 72  lHash;.      for
268bd 28 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  (x=sqliteHashFir
268be 73 74 28 70 54 62 6c 73 29 3b 20 78 3b 20 78 3d  st(pTbls); x; x=
268bf 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78  sqliteHashNext(x
268c0 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c  )){.        Tabl
268c1 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65  e *pTab = sqlite
268c2 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20  HashData(x);.   
268c3 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
268c4 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
268c5 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
268c6 50 5f 49 6e 74 65 67 65 72 2c 20 70 54 61 62 2d  P_Integer, pTab-
268c7 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20  >tnum, 2+cnt);. 
268c8 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20         cnt++;.  
268c9 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70        for(pIdx=p
268ca 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
268cb 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
268cc 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ext){.          
268cd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
268ce 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
268cf 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b 63   pIdx->tnum, 2+c
268d0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63  nt);.          c
268d1 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  nt++;.        }.
268d2 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
268d3 2a 20 4d 61 6b 65 20 73 75 72 65 20 73 75 66 66  * Make sure suff
268d4 69 63 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  icient number of
268d5 20 72 65 67 69 73 74 65 72 73 20 68 61 76 65 20   registers have 
268d6 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a  been allocated *
268d7 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72  /.      if( pPar
268d8 73 65 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74 2b 34  se->nMem < cnt+4
268d9 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72   ){.        pPar
268da 73 65 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74 2b 34  se->nMem = cnt+4
268db 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
268dc 20 2f 2a 20 44 6f 20 74 68 65 20 62 2d 74 72 65   /* Do the b-tre
268dd 65 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63  e integrity chec
268de 6b 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ks */.      sqli
268df 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
268e0 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 2c   OP_IntegrityCk,
268e1 20 32 2c 20 63 6e 74 2c 20 31 29 3b 0a 20 20 20   2, cnt, 1);.   
268e2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
268e3 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 69 29  angeP5(v, (u8)i)
268e4 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
268e5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
268e6 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32  (v, OP_IsNull, 2
268e7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
268e8 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
268e9 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20  _String8, 0, 3, 
268ea 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  0,.         sqli
268eb 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
268ec 2a 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20  *** in database 
268ed 25 73 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61  %s ***\n", db->a
268ee 44 62 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20  Db[i].zName),.  
268ef 20 20 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49         P4_DYNAMI
268f0 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
268f1 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
268f2 50 5f 4d 6f 76 65 2c 20 32 2c 20 34 2c 20 31 29  P_Move, 2, 4, 1)
268f3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
268f4 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
268f5 43 6f 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32 29  Concat, 4, 3, 2)
268f6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
268f7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
268f8 52 65 73 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29  ResultRow, 2, 1)
268f9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
268fa 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
268fb 64 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ddr);..      /* 
268fc 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68  Make sure all th
268fd 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 63 6f  e indices are co
268fe 6e 73 74 72 75 63 74 65 64 20 63 6f 72 72 65 63  nstructed correc
268ff 74 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  tly..      */.  
26900 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65      for(x=sqlite
26901 48 61 73 68 46 69 72 73 74 28 70 54 62 6c 73 29  HashFirst(pTbls)
26902 3b 20 78 20 26 26 20 21 69 73 51 75 69 63 6b 3b  ; x && !isQuick;
26903 20 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   x=sqliteHashNex
26904 74 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54  t(x)){.        T
26905 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c  able *pTab = sql
26906 69 74 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a  iteHashData(x);.
26907 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70          Index *p
26908 49 64 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  Idx;.        int
26909 20 6c 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20   loopTop;..     
2690a 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e     if( pTab->pIn
2690b 64 65 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  dex==0 ) continu
2690c 65 3b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20  e;.        addr 
2690d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2690e 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op1(v, OP_IfPos,
2690f 20 31 29 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66   1);  /* Stop if
26910 20 6f 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a   out of errors *
26911 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
26912 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26913 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20  P_Halt, 0, 0);. 
26914 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26915 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
26916 64 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  dr);.        sql
26917 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
26918 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20  Indices(pParse, 
26919 70 54 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e  pTab, 1, OP_Open
2691a 52 65 61 64 29 3b 0a 20 20 20 20 20 20 20 20 73  Read);.        s
2691b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2691c 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2691d 30 2c 20 32 29 3b 20 20 2f 2a 20 72 65 67 28 32  0, 2);  /* reg(2
2691e 29 20 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74  ) will count ent
2691f 72 69 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  ries */.        
26920 6c 6f 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74 65  loopTop = sqlite
26921 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26922 50 5f 52 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b  P_Rewind, 1, 0);
26923 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
26924 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26925 5f 41 64 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20  _AddImm, 2, 1); 
26926 20 20 2f 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65    /* increment e
26927 6e 74 72 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20  ntry count */.  
26928 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70        for(j=0, p
26929 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
2692a 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
2692b 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a  x->pNext, j++){.
2692c 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6d            int jm
2692d 70 32 3b 0a 20 20 20 20 20 20 20 20 20 20 73 74  p2;.          st
2692e 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
2692f 70 4c 69 73 74 20 69 64 78 45 72 72 5b 5d 20 3d  pList idxErr[] =
26930 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b   {.            {
26931 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20   OP_AddImm,     
26932 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20   1, -1,  0},.   
26933 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74           { OP_St
26934 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 33  ring8,     0,  3
26935 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a  ,  0},    /* 1 *
26936 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
26937 4f 50 5f 52 6f 77 69 64 2c 20 20 20 20 20 20 20  OP_Rowid,       
26938 31 2c 20 20 34 2c 20 20 30 7d 2c 0a 20 20 20 20  1,  4,  0},.    
26939 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72          { OP_Str
2693a 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 35 2c  ing8,     0,  5,
2693b 20 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f    0},    /* 3 */
2693c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
2693d 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30  P_String8,     0
2693e 2c 20 20 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a  ,  6,  0},    /*
2693f 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   4 */.          
26940 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20    { OP_Concat,  
26941 20 20 20 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0a      4,  3,  3},.
26942 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
26943 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 35 2c  _Concat,      5,
26944 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20    3,  3},.      
26945 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61        { OP_Conca
26946 74 2c 20 20 20 20 20 20 36 2c 20 20 33 2c 20 20  t,      6,  3,  
26947 33 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  3},.            
26948 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  { OP_ResultRow, 
26949 20 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20    3,  1,  0},.  
2694a 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49            { OP_I
2694b 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 20  fPos,       1,  
2694c 30 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 39 20  0,  0},    /* 9 
2694d 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
2694e 20 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20   OP_Halt,       
2694f 20 30 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20   0,  0,  0},.   
26950 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20         };.      
26951 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
26952 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72  ateIndexKey(pPar
26953 73 65 2c 20 70 49 64 78 2c 20 31 2c 20 33 2c 20  se, pIdx, 1, 3, 
26954 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 6d  1);.          jm
26955 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  p2 = sqlite3Vdbe
26956 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75  AddOp3(v, OP_Fou
26957 6e 64 2c 20 6a 2b 32 2c 20 30 2c 20 33 29 3b 0a  nd, j+2, 0, 3);.
26958 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d            addr =
26959 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2695a 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
2695b 7a 65 28 69 64 78 45 72 72 29 2c 20 69 64 78 45  ze(idxErr), idxE
2695c 72 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  rr);.          s
2695d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2695e 50 34 28 76 2c 20 61 64 64 72 2b 31 2c 20 22 72  P4(v, addr+1, "r
2695f 6f 77 69 64 20 22 2c 20 50 34 5f 53 54 41 54 49  owid ", P4_STATI
26960 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  C);.          sq
26961 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
26962 34 28 76 2c 20 61 64 64 72 2b 33 2c 20 22 20 6d  4(v, addr+3, " m
26963 69 73 73 69 6e 67 20 66 72 6f 6d 20 69 6e 64 65  issing from inde
26964 78 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  x ", P4_STATIC);
26965 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
26966 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
26967 2c 20 61 64 64 72 2b 34 2c 20 70 49 64 78 2d 3e  , addr+4, pIdx->
26968 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43  zName, P4_STATIC
26969 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2696a 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2696b 28 76 2c 20 61 64 64 72 2b 39 29 3b 0a 20 20 20  (v, addr+9);.   
2696c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2696d 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 6d  beJumpHere(v, jm
2696e 70 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  p2);.        }. 
2696f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26970 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
26971 65 78 74 2c 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b  ext, 1, loopTop+
26972 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
26973 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
26974 76 2c 20 6c 6f 6f 70 54 6f 70 29 3b 0a 20 20 20  v, loopTop);.   
26975 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49       for(j=0, pI
26976 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
26977 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
26978 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20  ->pNext, j++){. 
26979 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20           static 
2697a 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
2697b 20 63 6e 74 49 64 78 5b 5d 20 3d 20 7b 0a 20 20   cntIdx[] = {.  
2697c 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2697d 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20 30 2c  Integer,      0,
2697e 20 20 33 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    3,  0},.      
2697f 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 77 69         { OP_Rewi
26980 6e 64 2c 20 20 20 20 20 20 20 30 2c 20 20 30 2c  nd,       0,  0,
26981 20 20 30 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20    0},  /* 1 */. 
26982 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
26983 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 33  _AddImm,       3
26984 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  1,  0},.     
26985 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78          { OP_Nex
26986 74 2c 20 20 20 20 20 20 20 20 20 30 2c 20 20 30  t,         0,  0
26987 2c 20 20 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a  ,  0},  /* 3 */.
26988 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
26989 50 5f 45 71 2c 20 20 20 20 20 20 20 20 20 20 20  P_Eq,           
2698a 32 2c 20 20 30 2c 20 20 33 7d 2c 20 20 2f 2a 20  2,  0,  3},  /* 
2698b 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  4 */.           
2698c 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20    { OP_AddImm,  
2698d 20 20 20 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c       1, -1,  0},
2698e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
2698f 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
26990 20 30 2c 20 20 32 2c 20 20 30 7d 2c 20 20 2f 2a   0,  2,  0},  /*
26991 20 36 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   6 */.          
26992 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
26993 20 20 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d        0,  3,  0}
26994 2c 20 20 2f 2a 20 37 20 2a 2f 0a 20 20 20 20 20  ,  /* 7 */.     
26995 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e          { OP_Con
26996 63 61 74 2c 20 20 20 20 20 20 20 33 2c 20 20 32  cat,       3,  2
26997 2c 20 20 32 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  2},.         
26998 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52      { OP_ResultR
26999 6f 77 2c 20 20 20 20 32 2c 20 20 31 2c 20 20 30  ow,    2,  1,  0
2699a 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a  },.          };.
2699b 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d            addr =
2699c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2699d 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p1(v, OP_IfPos, 
2699e 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
2699f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
269a0 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30  v, OP_Halt, 0, 0
269a1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
269a2 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
269a3 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20  (v, addr);.     
269a4 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
269a5 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
269a6 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 63 6e  (v, ArraySize(cn
269a7 74 49 64 78 29 2c 20 63 6e 74 49 64 78 29 3b 0a  tIdx), cntIdx);.
269a8 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
269a9 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
269aa 20 61 64 64 72 2b 31 2c 20 6a 2b 32 29 3b 0a 20   addr+1, j+2);. 
269ab 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
269ac 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
269ad 61 64 64 72 2b 31 2c 20 61 64 64 72 2b 34 29 3b  addr+1, addr+4);
269ae 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
269af 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
269b0 2c 20 61 64 64 72 2b 33 2c 20 6a 2b 32 29 3b 0a  , addr+3, j+2);.
269b1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
269b2 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
269b3 20 61 64 64 72 2b 33 2c 20 61 64 64 72 2b 32 29   addr+3, addr+2)
269b4 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
269b5 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
269b6 76 2c 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20  v, addr+4);.    
269b7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
269b8 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
269b9 72 2b 36 2c 20 0a 20 20 20 20 20 20 20 20 20 20  r+6, .          
269ba 20 20 20 20 20 20 20 20 20 20 20 22 77 72 6f 6e             "wron
269bb 67 20 23 20 6f 66 20 65 6e 74 72 69 65 73 20 69  g # of entries i
269bc 6e 20 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54  n index ", P4_ST
269bd 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20  ATIC);.         
269be 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
269bf 67 65 50 34 28 76 2c 20 61 64 64 72 2b 37 2c 20  geP4(v, addr+7, 
269c0 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f  pIdx->zName, P4_
269c1 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
269c2 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20   }.      } .    
269c3 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  }.    addr = sql
269c4 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
269c5 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 65  t(v, ArraySize(e
269c6 6e 64 43 6f 64 65 29 2c 20 65 6e 64 43 6f 64 65  ndCode), endCode
269c7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
269c8 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64  beChangeP2(v, ad
269c9 64 72 2c 20 2d 6d 78 45 72 72 29 3b 0a 20 20 20  dr, -mxErr);.   
269ca 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
269cb 48 65 72 65 28 76 2c 20 61 64 64 72 2b 31 29 3b  Here(v, addr+1);
269cc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
269cd 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
269ce 2b 32 2c 20 22 6f 6b 22 2c 20 50 34 5f 53 54 41  +2, "ok", P4_STA
269cf 54 49 43 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  TIC);.  }else.#e
269d0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
269d1 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
269d2 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ECK */..#ifndef 
269d3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
269d4 36 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52  6.  /*.  **   PR
269d5 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 0a 20 20  AGMA encoding.  
269d6 2a 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f  **   PRAGMA enco
269d7 64 69 6e 67 20 3d 20 22 75 74 66 2d 38 22 7c 22  ding = "utf-8"|"
269d8 75 74 66 2d 31 36 22 7c 22 75 74 66 2d 31 36 6c  utf-16"|"utf-16l
269d9 65 22 7c 22 75 74 66 2d 31 36 62 65 22 0a 20 20  e"|"utf-16be".  
269da 2a 2a 0a 20 20 2a 2a 20 49 6e 20 69 74 73 20 66  **.  ** In its f
269db 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 69 73 20  irst form, this 
269dc 70 72 61 67 6d 61 20 72 65 74 75 72 6e 73 20 74  pragma returns t
269dd 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  he encoding of t
269de 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 64 61 74  he main.  ** dat
269df 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 64 61  abase. If the da
269e0 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 69 6e  tabase is not in
269e1 69 74 69 61 6c 69 7a 65 64 2c 20 69 74 20 69 73  itialized, it is
269e2 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6e 6f 77   initialized now
269e3 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
269e4 73 65 63 6f 6e 64 20 66 6f 72 6d 20 6f 66 20 74  second form of t
269e5 68 69 73 20 70 72 61 67 6d 61 20 69 73 20 61 20  his pragma is a 
269e6 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 6d 61 69  no-op if the mai
269e7 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  n database file.
269e8 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 61 6c 72    ** has not alr
269e9 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61  eady been initia
269ea 6c 69 7a 65 64 2e 20 49 6e 20 74 68 69 73 20 63  lized. In this c
269eb 61 73 65 20 69 74 20 73 65 74 73 20 74 68 65 20  ase it sets the 
269ec 64 65 66 61 75 6c 74 0a 20 20 2a 2a 20 65 6e 63  default.  ** enc
269ed 6f 64 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20  oding that will 
269ee 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  be used for the 
269ef 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
269f0 6c 65 20 69 66 20 61 20 6e 65 77 20 66 69 6c 65  le if a new file
269f1 0a 20 20 2a 2a 20 69 73 20 63 72 65 61 74 65 64  .  ** is created
269f2 2e 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67  . If an existing
269f3 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
269f4 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 74  ile is opened, t
269f5 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 64 65 66  hen the.  ** def
269f6 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69  ault text encodi
269f7 6e 67 20 66 6f 72 20 74 68 65 20 65 78 69 73 74  ng for the exist
269f8 69 6e 67 20 64 61 74 61 62 61 73 65 20 69 73 20  ing database is 
269f9 75 73 65 64 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a  used..  ** .  **
269fa 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 20 6e 65   In all cases ne
269fb 77 20 64 61 74 61 62 61 73 65 73 20 63 72 65 61  w databases crea
269fc 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 41 54  ted using the AT
269fd 54 41 43 48 20 63 6f 6d 6d 61 6e 64 20 61 72 65  TACH command are
269fe 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 74 6f  .  ** created to
269ff 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 64 65   use the same de
26a00 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64  fault text encod
26a01 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69 6e 20  ing as the main 
26a02 64 61 74 61 62 61 73 65 2e 20 49 66 0a 20 20 2a  database. If.  *
26a03 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  * the main datab
26a04 61 73 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ase has not been
26a05 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e 64   initialized and
26a06 2f 6f 72 20 63 72 65 61 74 65 64 20 77 68 65 6e  /or created when
26a07 20 41 54 54 41 43 48 0a 20 20 2a 2a 20 69 73 20   ATTACH.  ** is 
26a08 65 78 65 63 75 74 65 64 2c 20 74 68 69 73 20 69  executed, this i
26a09 73 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68  s done before th
26a0a 65 20 41 54 54 41 43 48 20 6f 70 65 72 61 74 69  e ATTACH operati
26a0b 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e  on..  **.  ** In
26a0c 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d   the second form
26a0d 20 74 68 69 73 20 70 72 61 67 6d 61 20 73 65 74   this pragma set
26a0e 73 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64  s the text encod
26a0f 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 69  ing to be used i
26a10 6e 0a 20 20 2a 2a 20 6e 65 77 20 64 61 74 61 62  n.  ** new datab
26a11 61 73 65 20 66 69 6c 65 73 20 63 72 65 61 74 65  ase files create
26a12 64 20 75 73 69 6e 67 20 74 68 69 73 20 64 61 74  d using this dat
26a13 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 49 74  abase handle. It
26a14 20 69 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 75 73   is only.  ** us
26a15 65 66 75 6c 20 69 66 20 69 6e 76 6f 6b 65 64 20  eful if invoked 
26a16 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
26a17 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
26a18 61 73 65 20 69 0a 20 20 2a 2f 0a 20 20 69 66 28  ase i.  */.  if(
26a19 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
26a1a 7a 4c 65 66 74 2c 20 22 65 6e 63 6f 64 69 6e 67  zLeft, "encoding
26a1b 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61  ")==0 ){.    sta
26a1c 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
26a1d 20 45 6e 63 4e 61 6d 65 20 7b 0a 20 20 20 20 20   EncName {.     
26a1e 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
26a1f 20 20 20 20 75 38 20 65 6e 63 3b 0a 20 20 20 20      u8 enc;.    
26a20 7d 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b  } encnames[] = {
26a21 0a 20 20 20 20 20 20 7b 20 22 55 54 46 38 22 2c  .      { "UTF8",
26a22 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38       SQLITE_UTF8
26a23 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20          },.     
26a24 20 7b 20 22 55 54 46 2d 38 22 2c 20 20 20 20 53   { "UTF-8",    S
26a25 51 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20  QLITE_UTF8      
26a26 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65    },  /* Must be
26a27 20 65 6c 65 6d 65 6e 74 20 5b 31 5d 20 2a 2f 0a   element [1] */.
26a28 20 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36 6c        { "UTF-16l
26a29 65 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  e", SQLITE_UTF16
26a2a 4c 45 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d 75  LE     },  /* Mu
26a2b 73 74 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b 32  st be element [2
26a2c 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54  ] */.      { "UT
26a2d 46 2d 31 36 62 65 22 2c 20 53 51 4c 49 54 45 5f  F-16be", SQLITE_
26a2e 55 54 46 31 36 42 45 20 20 20 20 20 7d 2c 20 20  UTF16BE     },  
26a2f 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d 65  /* Must be eleme
26a30 6e 74 20 5b 33 5d 20 2a 2f 0a 20 20 20 20 20 20  nt [3] */.      
26a31 7b 20 22 55 54 46 31 36 6c 65 22 2c 20 20 53 51  { "UTF16le",  SQ
26a32 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20  LITE_UTF16LE    
26a33 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46   },.      { "UTF
26a34 31 36 62 65 22 2c 20 20 53 51 4c 49 54 45 5f 55  16be",  SQLITE_U
26a35 54 46 31 36 42 45 20 20 20 20 20 7d 2c 0a 20 20  TF16BE     },.  
26a36 20 20 20 20 7b 20 22 55 54 46 2d 31 36 22 2c 20      { "UTF-16", 
26a37 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
26a38 20 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49 54       }, /* SQLIT
26a39 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 2a 2f  E_UTF16NATIVE */
26a3a 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31 36 22  .      { "UTF16"
26a3b 2c 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20  ,    0          
26a3c 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53 51          }, /* SQ
26a3d 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
26a3e 20 2a 2f 0a 20 20 20 20 20 20 7b 20 30 2c 20 30   */.      { 0, 0
26a3f 20 7d 0a 20 20 20 20 7d 3b 0a 20 20 20 20 63 6f   }.    };.    co
26a40 6e 73 74 20 73 74 72 75 63 74 20 45 6e 63 4e 61  nst struct EncNa
26a41 6d 65 20 2a 70 45 6e 63 3b 0a 20 20 20 20 69 66  me *pEnc;.    if
26a42 28 20 21 7a 52 69 67 68 74 20 29 7b 20 20 20 20  ( !zRight ){    
26a43 2f 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64  /* "PRAGMA encod
26a44 69 6e 67 22 20 2a 2f 0a 20 20 20 20 20 20 69 66  ing" */.      if
26a45 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
26a46 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
26a47 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
26a48 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26a49 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
26a4a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26a4b 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
26a4c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
26a4d 2c 20 22 65 6e 63 6f 64 69 6e 67 22 2c 20 53 51  , "encoding", SQ
26a4e 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
26a4f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26a50 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp2(v, OP_Stri
26a51 6e 67 38 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20  ng8, 0, 1);.    
26a52 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d    assert( encnam
26a53 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2e  es[SQLITE_UTF8].
26a54 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc==SQLITE_UTF8
26a55 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
26a56 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54  ( encnames[SQLIT
26a57 45 5f 55 54 46 31 36 4c 45 5d 2e 65 6e 63 3d 3d  E_UTF16LE].enc==
26a58 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29  SQLITE_UTF16LE )
26a59 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
26a5a 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f  encnames[SQLITE_
26a5b 55 54 46 31 36 42 45 5d 2e 65 6e 63 3d 3d 53 51  UTF16BE].enc==SQ
26a5c 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a  LITE_UTF16BE );.
26a5d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26a5e 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
26a5f 20 65 6e 63 6e 61 6d 65 73 5b 45 4e 43 28 70 50   encnames[ENC(pP
26a60 61 72 73 65 2d 3e 64 62 29 5d 2e 7a 4e 61 6d 65  arse->db)].zName
26a61 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
26a62 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26a63 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
26a64 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20  ltRow, 1, 1);.  
26a65 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20    }else{        
26a66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a67 2f 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64  /* "PRAGMA encod
26a68 69 6e 67 20 3d 20 58 58 58 22 20 2a 2f 0a 20 20  ing = XXX" */.  
26a69 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 63 68 61 6e      /* Only chan
26a6a 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
26a6b 73 71 6c 69 74 65 2e 65 6e 63 20 69 66 20 74 68  sqlite.enc if th
26a6c 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
26a6d 65 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a  e is not.      *
26a6e 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49  * initialized. I
26a6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
26a70 61 73 65 20 65 78 69 73 74 73 2c 20 74 68 65 20  ase exists, the 
26a71 6e 65 77 20 73 71 6c 69 74 65 2e 65 6e 63 20 76  new sqlite.enc v
26a72 61 6c 75 65 0a 20 20 20 20 20 20 2a 2a 20 77 69  alue.      ** wi
26a73 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65  ll be overwritte
26a74 6e 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d  n when the schem
26a75 61 20 69 73 20 6e 65 78 74 20 6c 6f 61 64 65 64  a is next loaded
26a76 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  . If it does not
26a77 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  .      ** alread
26a78 79 20 65 78 69 73 74 73 2c 20 69 74 20 77 69 6c  y exists, it wil
26a79 6c 20 62 65 20 63 72 65 61 74 65 64 20 74 6f 20  l be created to 
26a7a 75 73 65 20 74 68 65 20 6e 65 77 20 65 6e 63 6f  use the new enco
26a7b 64 69 6e 67 20 76 61 6c 75 65 2e 0a 20 20 20 20  ding value..    
26a7c 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 0a    */.      if( .
26a7d 20 20 20 20 20 20 20 20 21 28 44 62 48 61 73 50          !(DbHasP
26a7e 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44  roperty(db, 0, D
26a7f 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 29  B_SchemaLoaded))
26a80 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 44 62 48   || .        DbH
26a81 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 30  asProperty(db, 0
26a82 2c 20 44 42 5f 45 6d 70 74 79 29 20 0a 20 20 20  , DB_Empty) .   
26a83 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f     ){.        fo
26a84 72 28 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65 73  r(pEnc=&encnames
26a85 5b 30 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65  [0]; pEnc->zName
26a86 3b 20 70 45 6e 63 2b 2b 29 7b 0a 20 20 20 20 20  ; pEnc++){.     
26a87 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69       if( 0==sqli
26a88 74 65 33 53 74 72 49 43 6d 70 28 7a 52 69 67 68  te3StrICmp(zRigh
26a89 74 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20  t, pEnc->zName) 
26a8a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 45  ){.            E
26a8b 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 20 3d  NC(pParse->db) =
26a8c 20 70 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45 6e   pEnc->enc ? pEn
26a8d 63 2d 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45 5f  c->enc : SQLITE_
26a8e 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20  UTF16NATIVE;.   
26a8f 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
26a90 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
26a91 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
26a92 28 20 21 70 45 6e 63 2d 3e 7a 4e 61 6d 65 20 29  ( !pEnc->zName )
26a93 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
26a94 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
26a95 73 65 2c 20 22 75 6e 73 75 70 70 6f 72 74 65 64  se, "unsupported
26a96 20 65 6e 63 6f 64 69 6e 67 3a 20 25 73 22 2c 20   encoding: %s", 
26a97 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20  zRight);.       
26a98 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
26a99 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
26a9a 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
26a9b 54 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  TF16 */..#ifndef
26a9c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48   SQLITE_OMIT_SCH
26a9d 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47  EMA_VERSION_PRAG
26a9e 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20  MAS.  /*.  **   
26a9f 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
26aa0 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e  .]schema_version
26aa1 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
26aa2 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61  database.]schema
26aa3 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65  _version = <inte
26aa4 67 65 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ger>.  **.  **  
26aa5 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
26aa6 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 0a  e.]user_version.
26aa7 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
26aa8 61 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76 65  atabase.]user_ve
26aa9 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72  rsion = <integer
26aaa 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  >.  **.  ** The 
26aab 70 72 61 67 6d 61 27 73 20 73 63 68 65 6d 61 5f  pragma's schema_
26aac 76 65 72 73 69 6f 6e 20 61 6e 64 20 75 73 65 72  version and user
26aad 5f 76 65 72 73 69 6f 6e 20 61 72 65 20 75 73 65  _version are use
26aae 64 20 74 6f 20 73 65 74 20 6f 72 20 67 65 74 0a  d to set or get.
26aaf 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f    ** the value o
26ab0 66 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72  f the schema-ver
26ab1 73 69 6f 6e 20 61 6e 64 20 75 73 65 72 2d 76 65  sion and user-ve
26ab2 72 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69 76  rsion, respectiv
26ab3 65 6c 79 2e 20 42 6f 74 68 0a 20 20 2a 2a 20 74  ely. Both.  ** t
26ab4 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f  he schema-versio
26ab5 6e 20 61 6e 64 20 74 68 65 20 75 73 65 72 2d 76  n and the user-v
26ab6 65 72 73 69 6f 6e 20 61 72 65 20 33 32 2d 62 69  ersion are 32-bi
26ab7 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
26ab8 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e  s.  ** stored in
26ab9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65   the database he
26aba 61 64 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ader..  **.  ** 
26abb 54 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69  The schema-cooki
26abc 65 20 69 73 20 75 73 75 61 6c 6c 79 20 6f 6e 6c  e is usually onl
26abd 79 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 69 6e  y manipulated in
26abe 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69  ternally by SQLi
26abf 74 65 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 69  te. It.  ** is i
26ac0 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 53 51  ncremented by SQ
26ac1 4c 69 74 65 20 77 68 65 6e 65 76 65 72 20 74 68  Lite whenever th
26ac2 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
26ac3 61 20 69 73 20 6d 6f 64 69 66 69 65 64 20 28 62  a is modified (b
26ac4 79 0a 20 20 2a 2a 20 63 72 65 61 74 69 6e 67 20  y.  ** creating 
26ac5 6f 72 20 64 72 6f 70 70 69 6e 67 20 61 20 74 61  or dropping a ta
26ac6 62 6c 65 20 6f 72 20 69 6e 64 65 78 29 2e 20 54  ble or index). T
26ac7 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f  he schema versio
26ac8 6e 20 69 73 20 75 73 65 64 20 62 79 0a 20 20 2a  n is used by.  *
26ac9 2a 20 53 51 4c 69 74 65 20 65 61 63 68 20 74 69  * SQLite each ti
26aca 6d 65 20 61 20 71 75 65 72 79 20 69 73 20 65 78  me a query is ex
26acb 65 63 75 74 65 64 20 74 6f 20 65 6e 73 75 72 65  ecuted to ensure
26acc 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 72 6e   that the intern
26acd 61 6c 20 63 61 63 68 65 0a 20 20 2a 2a 20 6f 66  al cache.  ** of
26ace 20 74 68 65 20 73 63 68 65 6d 61 20 75 73 65 64   the schema used
26acf 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20   when compiling 
26ad0 74 68 65 20 53 51 4c 20 71 75 65 72 79 20 6d 61  the SQL query ma
26ad1 74 63 68 65 73 20 74 68 65 20 73 63 68 65 6d 61  tches the schema
26ad2 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74   of.  ** the dat
26ad3 61 62 61 73 65 20 61 67 61 69 6e 73 74 20 77 68  abase against wh
26ad4 69 63 68 20 74 68 65 20 63 6f 6d 70 69 6c 65 64  ich the compiled
26ad5 20 71 75 65 72 79 20 69 73 20 61 63 74 75 61 6c   query is actual
26ad6 6c 79 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a  ly executed..  *
26ad7 2a 20 53 75 62 76 65 72 74 69 6e 67 20 74 68 69  * Subverting thi
26ad8 73 20 6d 65 63 68 61 6e 69 73 6d 20 62 79 20 75  s mechanism by u
26ad9 73 69 6e 67 20 22 50 52 41 47 4d 41 20 73 63 68  sing "PRAGMA sch
26ada 65 6d 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f 20  ema_version" to 
26adb 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20  modify.  ** the 
26adc 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 69  schema-version i
26add 73 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64 61  s potentially da
26ade 6e 67 65 72 6f 75 73 20 61 6e 64 20 6d 61 79 20  ngerous and may 
26adf 6c 65 61 64 20 74 6f 20 70 72 6f 67 72 61 6d 0a  lead to program.
26ae0 20 20 2a 2a 20 63 72 61 73 68 65 73 20 6f 72 20    ** crashes or 
26ae1 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
26ae2 69 6f 6e 2e 20 55 73 65 20 77 69 74 68 20 63 61  ion. Use with ca
26ae3 75 74 69 6f 6e 21 0a 20 20 2a 2a 0a 20 20 2a 2a  ution!.  **.  **
26ae4 20 54 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f   The user-versio
26ae5 6e 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e  n is not used in
26ae6 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69  ternally by SQLi
26ae7 74 65 2e 20 49 74 20 6d 61 79 20 62 65 20 75 73  te. It may be us
26ae8 65 64 20 62 79 0a 20 20 2a 2a 20 61 70 70 6c 69  ed by.  ** appli
26ae9 63 61 74 69 6f 6e 73 20 66 6f 72 20 61 6e 79 20  cations for any 
26aea 70 75 72 70 6f 73 65 2e 0a 20 20 2a 2f 0a 20 20  purpose..  */.  
26aeb 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
26aec 6d 70 28 7a 4c 65 66 74 2c 20 22 73 63 68 65 6d  mp(zLeft, "schem
26aed 61 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a  a_version")==0 .
26aee 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
26aef 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 75 73 65  ICmp(zLeft, "use
26af0 72 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a  r_version")==0 .
26af1 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
26af2 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66 72 65  ICmp(zLeft, "fre
26af3 65 6c 69 73 74 5f 63 6f 75 6e 74 22 29 3d 3d 30  elist_count")==0
26af4 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69   .  ){.    int i
26af5 43 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f 6f  Cookie;   /* Coo
26af6 6b 69 65 20 69 6e 64 65 78 2e 20 30 20 66 6f 72  kie index. 0 for
26af7 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c 20   schema-cookie, 
26af8 36 20 66 6f 72 20 75 73 65 72 2d 63 6f 6f 6b 69  6 for user-cooki
26af9 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  e. */.    sqlite
26afa 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
26afb 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 77 69 74  , iDb);.    swit
26afc 63 68 28 20 7a 4c 65 66 74 5b 30 5d 20 29 7b 0a  ch( zLeft[0] ){.
26afd 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20        case 's': 
26afe 63 61 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20  case 'S':.      
26aff 20 20 69 43 6f 6f 6b 69 65 20 3d 20 30 3b 0a 20    iCookie = 0;. 
26b00 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
26b01 20 20 20 20 63 61 73 65 20 27 66 27 3a 20 63 61      case 'f': ca
26b02 73 65 20 27 46 27 3a 0a 20 20 20 20 20 20 20 20  se 'F':.        
26b03 69 43 6f 6f 6b 69 65 20 3d 20 31 3b 0a 20 20 20  iCookie = 1;.   
26b04 20 20 20 20 20 69 44 62 20 3d 20 28 2d 31 2a 28       iDb = (-1*(
26b05 69 44 62 2b 31 29 29 3b 0a 20 20 20 20 20 20 20  iDb+1));.       
26b06 20 61 73 73 65 72 74 28 69 44 62 3c 3d 30 29 3b   assert(iDb<=0);
26b07 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
26b08 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20        default:. 
26b09 20 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d         iCookie =
26b0a 20 35 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   5;.        brea
26b0b 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  k;.    }..    if
26b0c 28 20 7a 52 69 67 68 74 20 26 26 20 69 44 62 3e  ( zRight && iDb>
26b0d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57  =0 ){.      /* W
26b0e 72 69 74 65 20 74 68 65 20 73 70 65 63 69 66 69  rite the specifi
26b0f 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20  ed cookie value 
26b10 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  */.      static 
26b11 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
26b12 20 73 65 74 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b   setCookie[] = {
26b13 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54 72  .        { OP_Tr
26b14 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c  ansaction,    0,
26b15 20 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20    1,  0},    /* 
26b16 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f  0 */.        { O
26b17 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20  P_Integer,      
26b18 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20    0,  1,  0},   
26b19 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20   /* 1 */.       
26b1a 20 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c   { OP_SetCookie,
26b1b 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 31 7d        0,  0,  1}
26b1c 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20  ,    /* 2 */.   
26b1d 20 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20     };.      int 
26b1e 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
26b1f 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41  beAddOpList(v, A
26b20 72 72 61 79 53 69 7a 65 28 73 65 74 43 6f 6f 6b  rraySize(setCook
26b21 69 65 29 2c 20 73 65 74 43 6f 6f 6b 69 65 29 3b  ie), setCookie);
26b22 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26b23 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
26b24 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  dr, iDb);.      
26b25 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
26b26 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 61  eP1(v, addr+1, a
26b27 74 6f 69 28 7a 52 69 67 68 74 29 29 3b 0a 20 20  toi(zRight));.  
26b28 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
26b29 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b  hangeP1(v, addr+
26b2a 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  2, iDb);.      s
26b2b 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
26b2c 50 32 28 76 2c 20 61 64 64 72 2b 32 2c 20 69 43  P2(v, addr+2, iC
26b2d 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 7d 65 6c 73  ookie);.    }els
26b2e 65 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 61 64  e{.      /* Read
26b2f 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63   the specified c
26b30 6f 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20  ookie value */. 
26b31 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
26b32 74 20 56 64 62 65 4f 70 4c 69 73 74 20 72 65 61  t VdbeOpList rea
26b33 64 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20  dCookie[] = {.  
26b34 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43        { OP_ReadC
26b35 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20  ookie,      0,  
26b36 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20  1,  0},    /* 0 
26b37 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f  */.        { OP_
26b38 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20 20 20  ResultRow,      
26b39 20 31 2c 20 20 31 2c 20 20 30 7d 0a 20 20 20 20   1,  1,  0}.    
26b3a 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61    };.      int a
26b3b 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
26b3c 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
26b3d 72 61 79 53 69 7a 65 28 72 65 61 64 43 6f 6f 6b  raySize(readCook
26b3e 69 65 29 2c 20 72 65 61 64 43 6f 6f 6b 69 65 29  ie), readCookie)
26b3f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26b40 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
26b41 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  ddr, iDb);.     
26b42 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
26b43 67 65 50 33 28 76 2c 20 61 64 64 72 2c 20 69 43  geP3(v, addr, iC
26b44 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71  ookie);.      sq
26b45 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
26b46 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  ols(v, 1);.     
26b47 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
26b48 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
26b49 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 65 66 74  NAME_NAME, zLeft
26b4a 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
26b4b 4e 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  NT);.    }.  }el
26b4c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
26b4d 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  ITE_OMIT_SCHEMA_
26b4e 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 20  VERSION_PRAGMAS 
26b4f 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
26b50 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c  SQLITE_DEBUG) ||
26b51 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
26b52 54 45 53 54 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20  TEST).  /*.  ** 
26b53 52 65 70 6f 72 74 20 74 68 65 20 63 75 72 72 65  Report the curre
26b54 6e 74 20 73 74 61 74 65 20 6f 66 20 66 69 6c 65  nt state of file
26b55 20 6c 6f 67 73 20 66 6f 72 20 61 6c 6c 20 64 61   logs for all da
26b56 74 61 62 61 73 65 73 0a 20 20 2a 2f 0a 20 20 69  tabases.  */.  i
26b57 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
26b58 70 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 73  p(zLeft, "lock_s
26b59 74 61 74 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20  tatus")==0 ){.  
26b5a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
26b5b 68 61 72 20 2a 63 6f 6e 73 74 20 61 7a 4c 6f 63  har *const azLoc
26b5c 6b 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  kName[] = {.    
26b5d 20 20 22 75 6e 6c 6f 63 6b 65 64 22 2c 20 22 73    "unlocked", "s
26b5e 68 61 72 65 64 22 2c 20 22 72 65 73 65 72 76 65  hared", "reserve
26b5f 64 22 2c 20 22 70 65 6e 64 69 6e 67 22 2c 20 22  d", "pending", "
26b60 65 78 63 6c 75 73 69 76 65 22 0a 20 20 20 20 7d  exclusive".    }
26b61 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
26b62 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
26b63 75 6d 43 6f 6c 73 28 76 2c 20 32 29 3b 0a 20 20  umCols(v, 2);.  
26b64 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
26b65 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   2;.    sqlite3V
26b66 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
26b67 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
26b68 2c 20 22 64 61 74 61 62 61 73 65 22 2c 20 53 51  , "database", SQ
26b69 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
26b6a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
26b6b 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f  ColName(v, 1, CO
26b6c 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 74 61  LNAME_NAME, "sta
26b6d 74 75 73 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  tus", SQLITE_STA
26b6e 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  TIC);.    for(i=
26b6f 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
26b70 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20  +){.      Btree 
26b71 2a 70 42 74 3b 0a 20 20 20 20 20 20 50 61 67 65  *pBt;.      Page
26b72 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20  r *pPager;.     
26b73 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74   const char *zSt
26b74 61 74 65 20 3d 20 22 75 6e 6b 6e 6f 77 6e 22 3b  ate = "unknown";
26b75 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  .      int j;.  
26b76 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
26b77 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 20 63 6f  i].zName==0 ) co
26b78 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 73 71  ntinue;.      sq
26b79 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
26b7a 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
26b7b 2c 20 31 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b  , 1, 0, db->aDb[
26b7c 69 5d 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41  i].zName, P4_STA
26b7d 54 49 43 29 3b 0a 20 20 20 20 20 20 70 42 74 20  TIC);.      pBt 
26b7e 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
26b7f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 3d  ;.      if( pBt=
26b80 3d 30 20 7c 7c 20 28 70 50 61 67 65 72 20 3d 20  =0 || (pPager = 
26b81 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
26b82 72 28 70 42 74 29 29 3d 3d 30 20 29 7b 0a 20 20  r(pBt))==0 ){.  
26b83 20 20 20 20 20 20 7a 53 74 61 74 65 20 3d 20 22        zState = "
26b84 63 6c 6f 73 65 64 22 3b 0a 20 20 20 20 20 20 7d  closed";.      }
26b85 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
26b86 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62  _file_control(db
26b87 2c 20 69 20 3f 20 64 62 2d 3e 61 44 62 5b 69 5d  , i ? db->aDb[i]
26b88 2e 7a 4e 61 6d 65 20 3a 20 30 2c 20 0a 20 20 20  .zName : 0, .   
26b89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26b8b 20 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c    SQLITE_FCNTL_L
26b8c 4f 43 4b 53 54 41 54 45 2c 20 26 6a 29 3d 3d 53  OCKSTATE, &j)==S
26b8d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
26b8e 20 20 20 20 20 7a 53 74 61 74 65 20 3d 20 61 7a       zState = az
26b8f 4c 6f 63 6b 4e 61 6d 65 5b 6a 5d 3b 0a 20 20 20  LockName[j];.   
26b90 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
26b91 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
26b92 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32  OP_String8, 0, 2
26b93 2c 20 30 2c 20 7a 53 74 61 74 65 2c 20 50 34 5f  , 0, zState, P4_
26b94 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
26b95 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26b96 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
26b97 2c 20 31 2c 20 32 29 3b 0a 20 20 20 20 7d 0a 0a  , 1, 2);.    }..
26b98 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a    }else.#endif..
26b99 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53  #ifdef SQLITE_SS
26b9a 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 68 65 63  E.  /*.  ** Chec
26b9b 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
26b9c 73 71 6c 69 74 65 5f 73 74 61 74 65 6d 65 6e 74  sqlite_statement
26b9d 73 20 74 61 62 6c 65 20 65 78 69 73 74 73 2e 20  s table exists. 
26b9e 20 43 72 65 61 74 65 20 69 74 0a 20 20 2a 2a 20   Create it.  ** 
26b9f 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a  if it does not..
26ba0 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
26ba1 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
26ba2 20 22 63 72 65 61 74 65 5f 73 71 6c 69 74 65 5f   "create_sqlite_
26ba3 73 74 61 74 65 6d 65 6e 74 5f 74 61 62 6c 65 22  statement_table"
26ba4 29 3d 3d 30 20 29 7b 0a 20 20 20 20 65 78 74 65  )==0 ){.    exte
26ba5 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72  rn int sqlite3Cr
26ba6 65 61 74 65 53 74 61 74 65 6d 65 6e 74 73 54 61  eateStatementsTa
26ba7 62 6c 65 28 50 61 72 73 65 2a 29 3b 0a 20 20 20  ble(Parse*);.   
26ba8 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 53 74   sqlite3CreateSt
26ba9 61 74 65 6d 65 6e 74 73 54 61 62 6c 65 28 70 50  atementsTable(pP
26baa 61 72 73 65 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  arse);.  }else.#
26bab 65 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54  endif..#if SQLIT
26bac 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66  E_HAS_CODEC.  if
26bad 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
26bae 28 7a 4c 65 66 74 2c 20 22 6b 65 79 22 29 3d 3d  (zLeft, "key")==
26baf 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20  0 && zRight ){. 
26bb0 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 64     sqlite3_key(d
26bb1 62 2c 20 7a 52 69 67 68 74 2c 20 73 71 6c 69 74  b, zRight, sqlit
26bb2 65 33 53 74 72 6c 65 6e 33 30 28 7a 52 69 67 68  e3Strlen30(zRigh
26bb3 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a 20 20 69  t));.  }else.  i
26bb4 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
26bb5 70 28 7a 4c 65 66 74 2c 20 22 72 65 6b 65 79 22  p(zLeft, "rekey"
26bb6 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
26bb7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
26bb8 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  key(db, zRight, 
26bb9 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
26bba 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73  zRight));.  }els
26bbb 65 0a 20 20 69 66 28 20 7a 52 69 67 68 74 20 26  e.  if( zRight &
26bbc 26 20 28 73 71 6c 69 74 65 33 53 74 72 49 43 6d  & (sqlite3StrICm
26bbd 70 28 7a 4c 65 66 74 2c 20 22 68 65 78 6b 65 79  p(zLeft, "hexkey
26bbe 22 29 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20  ")==0 ||.       
26bbf 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
26bc0 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
26bc1 22 68 65 78 72 65 6b 65 79 22 29 3d 3d 30 29 20  "hexrekey")==0) 
26bc2 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 68 31  ){.    int i, h1
26bc3 2c 20 68 32 3b 0a 20 20 20 20 63 68 61 72 20 7a  , h2;.    char z
26bc4 4b 65 79 5b 34 30 5d 3b 0a 20 20 20 20 66 6f 72  Key[40];.    for
26bc5 28 69 3d 30 3b 20 28 68 31 20 3d 20 7a 52 69 67  (i=0; (h1 = zRig
26bc6 68 74 5b 69 5d 29 21 3d 30 20 26 26 20 28 68 32  ht[i])!=0 && (h2
26bc7 20 3d 20 7a 52 69 67 68 74 5b 69 2b 31 5d 29 21   = zRight[i+1])!
26bc8 3d 30 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20  =0; i+=2){.     
26bc9 20 68 31 20 2b 3d 20 39 2a 28 31 26 28 68 31 3e   h1 += 9*(1&(h1>
26bca 3e 36 29 29 3b 0a 20 20 20 20 20 20 68 32 20 2b  >6));.      h2 +
26bcb 3d 20 39 2a 28 31 26 28 68 32 3e 3e 36 29 29 3b  = 9*(1&(h2>>6));
26bcc 0a 20 20 20 20 20 20 7a 4b 65 79 5b 69 2f 32 5d  .      zKey[i/2]
26bcd 20 3d 20 28 68 32 20 26 20 30 78 30 66 29 20 7c   = (h2 & 0x0f) |
26bce 20 28 28 68 31 20 26 20 30 78 66 29 3c 3c 34 29   ((h1 & 0xf)<<4)
26bcf 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
26bd0 28 7a 4c 65 66 74 5b 33 5d 20 26 20 30 78 66 29  (zLeft[3] & 0xf)
26bd1 3d 3d 30 78 62 20 29 7b 0a 20 20 20 20 20 20 73  ==0xb ){.      s
26bd2 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a  qlite3_key(db, z
26bd3 4b 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d  Key, i/2);.    }
26bd4 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
26bd5 74 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 4b  te3_rekey(db, zK
26bd6 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 0a  ey, i/2);.    }.
26bd7 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 23    }else.#endif.#
26bd8 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  if SQLITE_HAS_CO
26bd9 44 45 43 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  DEC || defined(S
26bda 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52  QLITE_ENABLE_CER
26bdb 4f 44 29 0a 20 20 69 66 28 20 73 71 6c 69 74 65  OD).  if( sqlite
26bdc 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
26bdd 22 61 63 74 69 76 61 74 65 5f 65 78 74 65 6e 73  "activate_extens
26bde 69 6f 6e 73 22 29 3d 3d 30 20 29 7b 0a 23 69 66  ions")==0 ){.#if
26bdf 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
26be0 43 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  C.    if( sqlite
26be1 33 53 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74  3StrNICmp(zRight
26be2 2c 20 22 73 65 65 2d 22 2c 20 34 29 3d 3d 30 20  , "see-", 4)==0 
26be3 29 7b 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20  ){.      extern 
26be4 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74  void sqlite3_act
26be5 69 76 61 74 65 5f 73 65 65 28 63 6f 6e 73 74 20  ivate_see(const 
26be6 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71  char*);.      sq
26be7 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 73  lite3_activate_s
26be8 65 65 28 26 7a 52 69 67 68 74 5b 34 5d 29 3b 0a  ee(&zRight[4]);.
26be9 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66      }.#endif.#if
26bea 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
26beb 45 5f 43 45 52 4f 44 0a 20 20 20 20 69 66 28 20  E_CEROD.    if( 
26bec 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
26bed 7a 52 69 67 68 74 2c 20 22 63 65 72 6f 64 2d 22  zRight, "cerod-"
26bee 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 6)==0 ){.     
26bef 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
26bf0 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65  ite3_activate_ce
26bf1 72 6f 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  rod(const char*)
26bf2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
26bf3 61 63 74 69 76 61 74 65 5f 63 65 72 6f 64 28 26  activate_cerod(&
26bf4 7a 52 69 67 68 74 5b 36 5d 29 3b 0a 20 20 20 20  zRight[6]);.    
26bf5 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65  }.#endif.  }else
26bf6 0a 23 65 6e 64 69 66 0a 0a 20 0a 20 20 7b 2f 2a  .#endif.. .  {/*
26bf7 20 45 6d 70 74 79 20 45 4c 53 45 20 63 6c 61 75   Empty ELSE clau
26bf8 73 65 20 2a 2f 7d 0a 0a 20 20 2f 2a 20 43 6f 64  se */}..  /* Cod
26bf9 65 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 20 61  e an OP_Expire a
26bfa 74 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63  t the end of eac
26bfb 68 20 50 52 41 47 4d 41 20 70 72 6f 67 72 61 6d  h PRAGMA program
26bfc 20 74 6f 20 63 61 75 73 65 0a 20 20 2a 2a 20 74   to cause.  ** t
26bfd 68 65 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e  he VDBE implemen
26bfe 74 69 6e 67 20 74 68 65 20 70 72 61 67 6d 61 20  ting the pragma 
26bff 74 6f 20 65 78 70 69 72 65 2e 20 4d 6f 73 74 20  to expire. Most 
26c00 28 61 6c 6c 3f 29 20 70 72 61 67 6d 61 73 0a 20  (all?) pragmas. 
26c01 20 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c   ** are only val
26c02 69 64 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  id for a single 
26c03 65 78 65 63 75 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  execution..  */.
26c04 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26c05 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65  Op2(v, OP_Expire
26c06 2c 20 31 2c 20 30 29 3b 0a 0a 20 20 2f 2a 0a 20  , 1, 0);..  /*. 
26c07 20 2a 2a 20 52 65 73 65 74 20 74 68 65 20 73 61   ** Reset the sa
26c08 66 65 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20 63  fety level, in c
26c09 61 73 65 20 74 68 65 20 66 75 6c 6c 66 73 79 6e  ase the fullfsyn
26c0a 63 20 66 6c 61 67 20 6f 72 20 73 79 6e 63 68 72  c flag or synchr
26c0b 6f 6e 6f 75 73 0a 20 20 2a 2a 20 73 65 74 74 69  onous.  ** setti
26c0c 6e 67 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f  ng changed..  */
26c0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
26c0e 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d  OMIT_PAGER_PRAGM
26c0f 41 53 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74  AS.  if( db->aut
26c10 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73  oCommit ){.    s
26c11 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61  qlite3BtreeSetSa
26c12 66 65 74 79 4c 65 76 65 6c 28 70 44 62 2d 3e 70  fetyLevel(pDb->p
26c13 42 74 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f  Bt, pDb->safety_
26c14 6c 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20  level,.         
26c15 20 20 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73        (db->flags
26c16 26 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e  &SQLITE_FullFSyn
26c17 63 29 21 3d 30 29 3b 0a 20 20 7d 0a 23 65 6e 64  c)!=0);.  }.#end
26c18 69 66 0a 70 72 61 67 6d 61 5f 6f 75 74 3a 0a 20  if.pragma_out:. 
26c19 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
26c1a 62 2c 20 7a 4c 65 66 74 29 3b 0a 20 20 73 71 6c  b, zLeft);.  sql
26c1b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
26c1c 52 69 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69  Right);.}..#endi
26c1d 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
26c1e 5f 50 52 41 47 4d 41 20 7c 7c 20 53 51 4c 49 54  _PRAGMA || SQLIT
26c1f 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 20 2a 2f  E_OMIT_PARSER */
26c20 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
26c21 2a 20 45 6e 64 20 6f 66 20 70 72 61 67 6d 61 2e  * End of pragma.
26c22 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
26c23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c25 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
26c26 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72 65  * Begin file pre
26c27 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  pare.c *********
26c28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79  /./*.** 2005 May
26c2b 20 32 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   25.**.** The au
26c2c 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
26c2d 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
26c2e 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
26c2f 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
26c30 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
26c31 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
26c32 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
26c33 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
26c34 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
26c35 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
26c36 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
26c37 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
26c38 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
26c39 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
26c3a 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
26c3b 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
26c3c 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
26c3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26c41 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
26c42 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70  contains the imp
26c43 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
26c44 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  he sqlite3_prepa
26c45 72 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63  re().** interfac
26c46 65 2c 20 61 6e 64 20 72 6f 75 74 69 6e 65 73 20  e, and routines 
26c47 74 68 61 74 20 63 6f 6e 74 72 69 62 75 74 65 20  that contribute 
26c48 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 64  to loading the d
26c49 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a  atabase schema.*
26c4a 2a 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 0a  * from disk..**.
26c4b 2a 2a 20 24 49 64 3a 20 70 72 65 70 61 72 65 2e  ** $Id: prepare.
26c4c 63 2c 76 20 31 2e 31 31 37 20 32 30 30 39 2f 30  c,v 1.117 2009/0
26c4d 34 2f 32 30 20 31 37 3a 34 33 3a 30 33 20 64 72  4/20 17:43:03 dr
26c4e 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  h Exp $.*/../*.*
26c4f 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 69 74 44  * Fill the InitD
26c50 61 74 61 20 73 74 72 75 63 74 75 72 65 20 77 69  ata structure wi
26c51 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
26c52 61 67 65 20 74 68 61 74 20 69 6e 64 69 63 61 74  age that indicat
26c53 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64  es.** that the d
26c54 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75  atabase is corru
26c55 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  pt..*/.static vo
26c56 69 64 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61  id corruptSchema
26c57 28 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44  (.  InitData *pD
26c58 61 74 61 2c 20 20 20 20 20 2f 2a 20 49 6e 69 74  ata,     /* Init
26c59 69 61 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65  ialization conte
26c5a 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
26c5b 61 72 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f 2a 20  ar *zObj,    /* 
26c5c 4f 62 6a 65 63 74 20 62 65 69 6e 67 20 70 61 72  Object being par
26c5d 73 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74  sed at the point
26c5e 20 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63   of error */.  c
26c5f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78 74 72  onst char *zExtr
26c60 61 20 20 20 2f 2a 20 45 72 72 6f 72 20 69 6e 66  a   /* Error inf
26c61 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  ormation */.){. 
26c62 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
26c63 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 66 28 20  Data->db;.  if( 
26c64 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
26c65 64 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 20  d && (db->flags 
26c66 26 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  & SQLITE_Recover
26c67 79 4d 6f 64 65 29 3d 3d 30 20 29 7b 0a 20 20 20  yMode)==0 ){.   
26c68 20 69 66 28 20 7a 4f 62 6a 3d 3d 30 20 29 20 7a   if( zObj==0 ) z
26c69 4f 62 6a 20 3d 20 22 3f 22 3b 0a 20 20 20 20 73  Obj = "?";.    s
26c6a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
26c6b 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c  pData->pzErrMsg,
26c6c 20 70 44 61 74 61 2d 3e 64 62 2c 0a 20 20 20 20   pData->db,.    
26c6d 20 20 20 22 6d 61 6c 66 6f 72 6d 65 64 20 64 61     "malformed da
26c6e 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 28 25  tabase schema (%
26c6f 73 29 22 2c 20 7a 4f 62 6a 29 3b 0a 20 20 20 20  s)", zObj);.    
26c70 69 66 28 20 7a 45 78 74 72 61 20 26 26 20 7a 45  if( zExtra && zE
26c71 78 74 72 61 5b 30 5d 20 29 7b 0a 20 20 20 20 20  xtra[0] ){.     
26c72 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73   *pData->pzErrMs
26c73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65  g = sqlite3MAppe
26c74 6e 64 66 28 70 44 61 74 61 2d 3e 64 62 2c 20 2a  ndf(pData->db, *
26c75 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c  pData->pzErrMsg,
26c76 20 22 25 73 20 2d 20 25 73 22 2c 0a 20 20 20 20   "%s - %s",.    
26c77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70                *p
26c79 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20  Data->pzErrMsg, 
26c7a 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20  zExtra);.    }. 
26c7b 20 7d 0a 20 20 70 44 61 74 61 2d 3e 72 63 20 3d   }.  pData->rc =
26c7c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b   SQLITE_CORRUPT;
26c7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
26c7e 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72  s the callback r
26c7f 6f 75 74 69 6e 65 20 66 6f 72 20 74 68 65 20 63  outine for the c
26c80 6f 64 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c  ode that initial
26c81 69 7a 65 73 20 74 68 65 0a 2a 2a 20 64 61 74 61  izes the.** data
26c82 62 61 73 65 2e 20 20 53 65 65 20 73 71 6c 69 74  base.  See sqlit
26c83 65 33 49 6e 69 74 28 29 20 62 65 6c 6f 77 20 66  e3Init() below f
26c84 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
26c85 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68  formation..** Th
26c86 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6c  is routine is al
26c87 73 6f 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74  so called from t
26c88 68 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  he OP_ParseSchem
26c89 61 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20  a opcode of the 
26c8a 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68  VDBE..**.** Each
26c8b 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69   callback contai
26c8c 6e 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ns the following
26c8d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a   information:.**
26c8e 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 30 5d 20  .**     argv[0] 
26c8f 3d 20 6e 61 6d 65 20 6f 66 20 74 68 69 6e 67 20  = name of thing 
26c90 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a  being created.**
26c91 20 20 20 20 20 61 72 67 76 5b 31 5d 20 3d 20 72       argv[1] = r
26c92 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
26c93 66 6f 72 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  for table or ind
26c94 65 78 2e 20 30 20 66 6f 72 20 74 72 69 67 67 65  ex. 0 for trigge
26c95 72 20 6f 72 20 76 69 65 77 2e 0a 2a 2a 20 20 20  r or view..**   
26c96 20 20 61 72 67 76 5b 32 5d 20 3d 20 53 51 4c 20    argv[2] = SQL 
26c97 74 65 78 74 20 66 6f 72 20 74 68 65 20 43 52 45  text for the CRE
26c98 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ATE statement..*
26c99 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  *.*/.SQLITE_PRIV
26c9a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
26c9b 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64  nitCallback(void
26c9c 20 2a 70 49 6e 69 74 2c 20 69 6e 74 20 61 72 67   *pInit, int arg
26c9d 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20  c, char **argv, 
26c9e 63 68 61 72 20 2a 2a 4e 6f 74 55 73 65 64 29 7b  char **NotUsed){
26c9f 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61  .  InitData *pDa
26ca0 74 61 20 3d 20 28 49 6e 69 74 44 61 74 61 2a 29  ta = (InitData*)
26ca1 70 49 6e 69 74 3b 0a 20 20 73 71 6c 69 74 65 33  pInit;.  sqlite3
26ca2 20 2a 64 62 20 3d 20 70 44 61 74 61 2d 3e 64 62   *db = pData->db
26ca3 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 44  ;.  int iDb = pD
26ca4 61 74 61 2d 3e 69 44 62 3b 0a 0a 20 20 61 73 73  ata->iDb;..  ass
26ca5 65 72 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a  ert( argc==3 );.
26ca6 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
26ca7 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67  ER2(NotUsed, arg
26ca8 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  c);.  assert( sq
26ca9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
26caa 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
26cab 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79   DbClearProperty
26cac 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d 70  (db, iDb, DB_Emp
26cad 74 79 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  ty);.  if( db->m
26cae 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
26caf 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61     corruptSchema
26cb0 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c  (pData, argv[0],
26cb1 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   0);.    return 
26cb2 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
26cb3 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  }..  assert( iDb
26cb4 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
26cb5 44 62 20 29 3b 0a 20 20 69 66 28 20 61 72 67 76  Db );.  if( argv
26cb6 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ==0 ) return 0; 
26cb7 20 20 2f 2a 20 4d 69 67 68 74 20 68 61 70 70 65    /* Might happe
26cb8 6e 20 69 66 20 45 4d 50 54 59 5f 52 45 53 55 4c  n if EMPTY_RESUL
26cb9 54 5f 43 41 4c 4c 42 41 43 4b 53 20 61 72 65 20  T_CALLBACKS are 
26cba 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 61 72 67 76  on */.  if( argv
26cbb 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f  [1]==0 ){.    co
26cbc 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61 74  rruptSchema(pDat
26cbd 61 2c 20 61 72 67 76 5b 30 5d 2c 20 30 29 3b 0a  a, argv[0], 0);.
26cbe 20 20 7d 65 6c 73 65 20 69 66 28 20 61 72 67 76    }else if( argv
26cbf 5b 32 5d 20 26 26 20 61 72 67 76 5b 32 5d 5b 30  [2] && argv[2][0
26cc0 5d 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c  ] ){.    /* Call
26cc1 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 70   the parser to p
26cc2 72 6f 63 65 73 73 20 61 20 43 52 45 41 54 45 20  rocess a CREATE 
26cc3 54 41 42 4c 45 2c 20 49 4e 44 45 58 20 6f 72 20  TABLE, INDEX or 
26cc4 56 49 45 57 2e 0a 20 20 20 20 2a 2a 20 42 75 74  VIEW..    ** But
26cc5 20 62 65 63 61 75 73 65 20 64 62 2d 3e 69 6e 69   because db->ini
26cc6 74 2e 62 75 73 79 20 69 73 20 73 65 74 20 74 6f  t.busy is set to
26cc7 20 31 2c 20 6e 6f 20 56 44 42 45 20 63 6f 64 65   1, no VDBE code
26cc8 20 69 73 20 67 65 6e 65 72 61 74 65 64 0a 20 20   is generated.  
26cc9 20 20 2a 2a 20 6f 72 20 65 78 65 63 75 74 65 64    ** or executed
26cca 2e 20 20 41 6c 6c 20 74 68 65 20 70 61 72 73 65  .  All the parse
26ccb 72 20 64 6f 65 73 20 69 73 20 62 75 69 6c 64 20  r does is build 
26ccc 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74  the internal dat
26ccd 61 0a 20 20 20 20 2a 2a 20 73 74 72 75 63 74 75  a.    ** structu
26cce 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62  res that describ
26ccf 65 20 74 68 65 20 74 61 62 6c 65 2c 20 69 6e 64  e the table, ind
26cd0 65 78 2c 20 6f 72 20 76 69 65 77 2e 0a 20 20 20  ex, or view..   
26cd1 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 45   */.    char *zE
26cd2 72 72 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a  rr;.    int rc;.
26cd3 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
26cd4 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 20  init.busy );.   
26cd5 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 20 3d 20   db->init.iDb = 
26cd6 69 44 62 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69  iDb;.    db->ini
26cd7 74 2e 6e 65 77 54 6e 75 6d 20 3d 20 61 74 6f 69  t.newTnum = atoi
26cd8 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 72  (argv[1]);.    r
26cd9 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
26cda 28 64 62 2c 20 61 72 67 76 5b 32 5d 2c 20 30 2c  (db, argv[2], 0,
26cdb 20 30 2c 20 26 7a 45 72 72 29 3b 0a 20 20 20 20   0, &zErr);.    
26cdc 64 62 2d 3e 69 6e 69 74 2e 69 44 62 20 3d 20 30  db->init.iDb = 0
26cdd 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
26cde 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 7a  !=SQLITE_OK || z
26cdf 45 72 72 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  Err==0 );.    if
26ce0 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20  ( SQLITE_OK!=rc 
26ce1 29 7b 0a 20 20 20 20 20 20 70 44 61 74 61 2d 3e  ){.      pData->
26ce2 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 69  rc = rc;.      i
26ce3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
26ce4 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 64  MEM ){.        d
26ce5 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
26ce6 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 1;.      }else
26ce7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
26ce8 49 4e 54 45 52 52 55 50 54 20 26 26 20 28 72 63  INTERRUPT && (rc
26ce9 26 30 78 66 66 29 21 3d 53 51 4c 49 54 45 5f 4c  &0xff)!=SQLITE_L
26cea 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 20  OCKED ){.       
26ceb 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70   corruptSchema(p
26cec 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 7a  Data, argv[0], z
26ced 45 72 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Err);.      }.  
26cee 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
26cef 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
26cf0 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61   }.  }else if( a
26cf1 72 67 76 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  rgv[0]==0 ){.   
26cf2 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70   corruptSchema(p
26cf3 44 61 74 61 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  Data, 0, 0);.  }
26cf4 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20  else{.    /* If 
26cf5 74 68 65 20 53 51 4c 20 63 6f 6c 75 6d 6e 20 69  the SQL column i
26cf6 73 20 62 6c 61 6e 6b 20 69 74 20 6d 65 61 6e 73  s blank it means
26cf7 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 64 65   this is an inde
26cf8 78 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 77 61  x that.    ** wa
26cf9 73 20 63 72 65 61 74 65 64 20 74 6f 20 62 65 20  s created to be 
26cfa 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
26cfb 6f 72 20 74 6f 20 66 75 6c 66 69 6c 6c 20 61 20  or to fulfill a 
26cfc 55 4e 49 51 55 45 0a 20 20 20 20 2a 2a 20 63 6f  UNIQUE.    ** co
26cfd 6e 73 74 72 61 69 6e 74 20 66 6f 72 20 61 20 43  nstraint for a C
26cfe 52 45 41 54 45 20 54 41 42 4c 45 2e 20 20 54 68  REATE TABLE.  Th
26cff 65 20 69 6e 64 65 78 20 73 68 6f 75 6c 64 20 68  e index should h
26d00 61 76 65 20 61 6c 72 65 61 64 79 0a 20 20 20 20  ave already.    
26d01 2a 2a 20 62 65 65 6e 20 63 72 65 61 74 65 64 20  ** been created 
26d02 77 68 65 6e 20 77 65 20 70 72 6f 63 65 73 73 65  when we processe
26d03 64 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  d the CREATE TAB
26d04 4c 45 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 65  LE.  All we have
26d05 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20 68 65  .    ** to do he
26d06 72 65 20 69 73 20 72 65 63 6f 72 64 20 74 68 65  re is record the
26d07 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
26d08 72 20 66 6f 72 20 74 68 61 74 20 69 6e 64 65 78  r for that index
26d09 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64  ..    */.    Ind
26d0a 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20 20  ex *pIndex;.    
26d0b 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
26d0c 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 61 72  FindIndex(db, ar
26d0d 67 76 5b 30 5d 2c 20 64 62 2d 3e 61 44 62 5b 69  gv[0], db->aDb[i
26d0e 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  Db].zName);.    
26d0f 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 7c 7c  if( pIndex==0 ||
26d10 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 21 3d 30   pIndex->tnum!=0
26d11 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
26d12 73 20 63 61 6e 20 6f 63 63 75 72 20 69 66 20 74  s can occur if t
26d13 68 65 72 65 20 65 78 69 73 74 73 20 61 6e 20 69  here exists an i
26d14 6e 64 65 78 20 6f 6e 20 61 20 54 45 4d 50 20 74  ndex on a TEMP t
26d15 61 62 6c 65 20 77 68 69 63 68 0a 20 20 20 20 20  able which.     
26d16 20 2a 2a 20 68 61 73 20 74 68 65 20 73 61 6d 65   ** has the same
26d17 20 6e 61 6d 65 20 61 73 20 61 6e 6f 74 68 65 72   name as another
26d18 20 69 6e 64 65 78 20 6f 6e 20 61 20 70 65 72 6d   index on a perm
26d19 61 6e 65 6e 74 20 69 6e 64 65 78 2e 20 20 53 69  anent index.  Si
26d1a 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  nce.      ** the
26d1b 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65   permanent table
26d1c 20 69 73 20 68 69 64 64 65 6e 20 62 79 20 74 68   is hidden by th
26d1d 65 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 77 65  e TEMP table, we
26d1e 20 63 61 6e 20 61 6c 73 6f 0a 20 20 20 20 20 20   can also.      
26d1f 2a 2a 20 73 61 66 65 6c 79 20 69 67 6e 6f 72 65  ** safely ignore
26d20 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 68   the index on th
26d21 65 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c  e permanent tabl
26d22 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
26d23 20 20 2f 2a 20 44 6f 20 4e 6f 74 68 69 6e 67 20    /* Do Nothing 
26d24 2a 2f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  */;.    }else{. 
26d25 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e 75       pIndex->tnu
26d26 6d 20 3d 20 61 74 6f 69 28 61 72 67 76 5b 31 5d  m = atoi(argv[1]
26d27 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
26d28 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
26d29 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61  * Attempt to rea
26d2a 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
26d2b 63 68 65 6d 61 20 61 6e 64 20 69 6e 69 74 69 61  chema and initia
26d2c 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a  lize internal.**
26d2d 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
26d2e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61   for a single da
26d2f 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
26d30 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a  e index of the.*
26d31 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
26d32 69 73 20 67 69 76 65 6e 20 62 79 20 69 44 62 2e  is given by iDb.
26d33 20 20 69 44 62 3d 3d 30 20 69 73 20 75 73 65 64    iDb==0 is used
26d34 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 0a 2a 2a   for the main.**
26d35 20 64 61 74 61 62 61 73 65 2e 20 20 69 44 62 3d   database.  iDb=
26d36 3d 31 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  =1 should never 
26d37 62 65 20 75 73 65 64 2e 20 20 69 44 62 3e 3d 32  be used.  iDb>=2
26d38 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20   is used for.** 
26d39 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
26d3a 73 65 73 2e 20 20 52 65 74 75 72 6e 20 6f 6e 65  ses.  Return one
26d3b 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 20   of the SQLITE_ 
26d3c 65 72 72 6f 72 20 63 6f 64 65 73 20 74 6f 0a 2a  error codes to.*
26d3d 2a 20 69 6e 64 69 63 61 74 65 20 73 75 63 63 65  * indicate succe
26d3e 73 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a  ss or failure..*
26d3f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
26d40 69 74 65 33 49 6e 69 74 4f 6e 65 28 73 71 6c 69  ite3InitOne(sqli
26d41 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
26d42 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73  , char **pzErrMs
26d43 67 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  g){.  int rc;.  
26d44 69 6e 74 20 69 3b 0a 20 20 42 74 43 75 72 73 6f  int i;.  BtCurso
26d45 72 20 2a 63 75 72 4d 61 69 6e 3b 0a 20 20 69 6e  r *curMain;.  in
26d46 74 20 73 69 7a 65 3b 0a 20 20 54 61 62 6c 65 20  t size;.  Table 
26d47 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a 70 44 62  *pTab;.  Db *pDb
26d48 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
26d49 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69 6e 74 20  azArg[4];.  int 
26d4a 6d 65 74 61 5b 31 30 5d 3b 0a 20 20 49 6e 69 74  meta[10];.  Init
26d4b 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20  Data initData;. 
26d4c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61   char const *zMa
26d4d 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 63 68  sterSchema;.  ch
26d4e 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65  ar const *zMaste
26d4f 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54  rName = SCHEMA_T
26d50 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20 20 2f 2a  ABLE(iDb);..  /*
26d51 0a 20 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72  .  ** The master
26d52 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
26d53 68 61 73 20 61 20 73 74 72 75 63 74 75 72 65 20  has a structure 
26d54 6c 69 6b 65 20 74 68 69 73 0a 20 20 2a 2f 0a 20  like this.  */. 
26d55 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
26d56 61 72 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61  ar master_schema
26d57 5b 5d 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41  [] = .     "CREA
26d58 54 45 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f  TE TABLE sqlite_
26d59 6d 61 73 74 65 72 28 5c 6e 22 0a 20 20 20 20 20  master(\n".     
26d5a 22 20 20 74 79 70 65 20 74 65 78 74 2c 5c 6e 22  "  type text,\n"
26d5b 0a 20 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65  .     "  name te
26d5c 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 74  xt,\n".     "  t
26d5d 62 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22  bl_name text,\n"
26d5e 0a 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67  .     "  rootpag
26d5f 65 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20  e integer,\n".  
26d60 20 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c 6e     "  sql text\n
26d61 22 0a 20 20 20 20 20 22 29 22 0a 20 20 3b 0a 23  ".     ")".  ;.#
26d62 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
26d63 49 54 5f 54 45 4d 50 44 42 0a 20 20 73 74 61 74  IT_TEMPDB.  stat
26d64 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 74 65  ic const char te
26d65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d 61  mp_master_schema
26d66 5b 5d 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41  [] = .     "CREA
26d67 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 73 71  TE TEMP TABLE sq
26d68 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72  lite_temp_master
26d69 28 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 79 70  (\n".     "  typ
26d6a 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20  e text,\n".     
26d6b 22 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22  "  name text,\n"
26d6c 0a 20 20 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d  .     "  tbl_nam
26d6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20  e text,\n".     
26d6e 22 20 20 72 6f 6f 74 70 61 67 65 20 69 6e 74 65  "  rootpage inte
26d6f 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ger,\n".     "  
26d70 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20 20  sql text\n".    
26d71 20 22 29 22 0a 20 20 3b 0a 23 65 6c 73 65 0a 20   ")".  ;.#else. 
26d72 20 23 64 65 66 69 6e 65 20 74 65 6d 70 5f 6d 61   #define temp_ma
26d73 73 74 65 72 5f 73 63 68 65 6d 61 20 30 0a 23 65  ster_schema 0.#e
26d74 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20  ndif..  assert( 
26d75 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
26d76 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
26d77 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
26d78 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 61 73 73  pSchema );.  ass
26d79 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
26d7a 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
26d7b 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
26d7c 69 44 62 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65  iDb==1 || sqlite
26d7d 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
26d7e 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42  (db->aDb[iDb].pB
26d7f 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73  t) );..  /* zMas
26d80 74 65 72 53 63 68 65 6d 61 20 61 6e 64 20 7a 49  terSchema and zI
26d81 6e 69 74 53 63 72 69 70 74 20 61 72 65 20 73 65  nitScript are se
26d82 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  t to point at th
26d83 65 20 6d 61 73 74 65 72 20 73 63 68 65 6d 61 0a  e master schema.
26d84 20 20 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c    ** and initial
26d85 69 73 61 74 69 6f 6e 20 73 63 72 69 70 74 20 61  isation script a
26d86 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
26d87 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  he database bein
26d88 67 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73  g.  ** initialis
26d89 65 64 2e 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20  ed. zMasterName 
26d8a 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
26d8b 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  he master table.
26d8c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49  .  */.  if( !OMI
26d8d 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
26d8e 3d 31 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65  =1 ){.    zMaste
26d8f 72 53 63 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d  rSchema = temp_m
26d90 61 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20  aster_schema;.  
26d91 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74  }else{.    zMast
26d92 65 72 53 63 68 65 6d 61 20 3d 20 6d 61 73 74 65  erSchema = maste
26d93 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20  r_schema;.  }.  
26d94 7a 4d 61 73 74 65 72 4e 61 6d 65 20 3d 20 53 43  zMasterName = SC
26d95 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b  HEMA_TABLE(iDb);
26d96 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74  ..  /* Construct
26d97 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c   the schema tabl
26d98 65 73 2e 20 20 2a 2f 0a 20 20 61 7a 41 72 67 5b  es.  */.  azArg[
26d99 30 5d 20 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65  0] = zMasterName
26d9a 3b 0a 20 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22  ;.  azArg[1] = "
26d9b 31 22 3b 0a 20 20 61 7a 41 72 67 5b 32 5d 20 3d  1";.  azArg[2] =
26d9c 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 3b 0a   zMasterSchema;.
26d9d 20 20 61 7a 41 72 67 5b 33 5d 20 3d 20 30 3b 0a    azArg[3] = 0;.
26d9e 20 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20    initData.db = 
26d9f 64 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 69  db;.  initData.i
26da0 44 62 20 3d 20 69 44 62 3b 0a 20 20 69 6e 69 74  Db = iDb;.  init
26da1 44 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45  Data.rc = SQLITE
26da2 5f 4f 4b 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e  _OK;.  initData.
26da3 70 7a 45 72 72 4d 73 67 20 3d 20 70 7a 45 72 72  pzErrMsg = pzErr
26da4 4d 73 67 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c  Msg;.  (void)sql
26da5 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
26da6 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 6e 69 74  );.  sqlite3Init
26da7 43 61 6c 6c 62 61 63 6b 28 26 69 6e 69 74 44 61  Callback(&initDa
26da8 74 61 2c 20 33 2c 20 28 63 68 61 72 20 2a 2a 29  ta, 3, (char **)
26da9 61 7a 41 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f  azArg, 0);.  (vo
26daa 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
26dab 4f 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 69 6e  On(db);.  if( in
26dac 69 74 44 61 74 61 2e 72 63 20 29 7b 0a 20 20 20  itData.rc ){.   
26dad 20 72 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72   rc = initData.r
26dae 63 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  c;.    goto erro
26daf 72 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61  r_out;.  }.  pTa
26db0 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
26db1 61 62 6c 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  able(db, zMaster
26db2 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44  Name, db->aDb[iD
26db3 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b].zName);.  if(
26db4 20 70 54 61 62 20 29 7b 0a 20 20 20 20 70 54 61   pTab ){.    pTa
26db5 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54  b->tabFlags |= T
26db6 46 5f 52 65 61 64 6f 6e 6c 79 3b 0a 20 20 7d 0a  F_Readonly;.  }.
26db7 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 63  .  /* Create a c
26db8 75 72 73 6f 72 20 74 6f 20 68 6f 6c 64 20 74 68  ursor to hold th
26db9 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 6e 0a  e database open.
26dba 20 20 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62    */.  pDb = &db
26dbb 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66  ->aDb[iDb];.  if
26dbc 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b  ( pDb->pBt==0 ){
26dbd 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  .    if( !OMIT_T
26dbe 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20  EMPDB && iDb==1 
26dbf 29 7b 0a 20 20 20 20 20 20 44 62 53 65 74 50 72  ){.      DbSetPr
26dc0 6f 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42  operty(db, 1, DB
26dc1 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a  _SchemaLoaded);.
26dc2 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
26dc3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
26dc4 20 20 63 75 72 4d 61 69 6e 20 3d 20 73 71 6c 69    curMain = sqli
26dc5 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71  te3MallocZero(sq
26dc6 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
26dc7 53 69 7a 65 28 29 29 3b 0a 20 20 69 66 28 20 21  Size());.  if( !
26dc8 63 75 72 4d 61 69 6e 20 29 7b 0a 20 20 20 20 72  curMain ){.    r
26dc9 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
26dca 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ;.    goto error
26dcb 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  _out;.  }.  sqli
26dcc 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 44  te3BtreeEnter(pD
26dcd 62 2d 3e 70 42 74 29 3b 0a 20 20 72 63 20 3d 20  b->pBt);.  rc = 
26dce 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
26dcf 6f 72 28 70 44 62 2d 3e 70 42 74 2c 20 4d 41 53  or(pDb->pBt, MAS
26dd0 54 45 52 5f 52 4f 4f 54 2c 20 30 2c 20 30 2c 20  TER_ROOT, 0, 0, 
26dd1 63 75 72 4d 61 69 6e 29 3b 0a 20 20 69 66 28 20  curMain);.  if( 
26dd2 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59  rc==SQLITE_EMPTY
26dd3 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f   ) rc = SQLITE_O
26dd4 4b 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  K;.  if( rc!=SQL
26dd5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
26dd6 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70  lite3SetString(p
26dd7 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
26dd8 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  ", sqlite3ErrStr
26dd9 28 72 63 29 29 3b 0a 20 20 20 20 67 6f 74 6f 20  (rc));.    goto 
26dda 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75  initone_error_ou
26ddb 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74  t;.  }..  /* Get
26ddc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 65   the database me
26ddd 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  ta information..
26dde 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 74 61 20 76    **.  ** Meta v
26ddf 61 6c 75 65 73 20 61 72 65 20 61 73 20 66 6f 6c  alues are as fol
26de0 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 20 6d 65  lows:.  **    me
26de1 74 61 5b 30 5d 20 20 20 53 63 68 65 6d 61 20 63  ta[0]   Schema c
26de2 6f 6f 6b 69 65 2e 20 20 43 68 61 6e 67 65 73 20  ookie.  Changes 
26de3 77 69 74 68 20 65 61 63 68 20 73 63 68 65 6d 61  with each schema
26de4 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2a 20 20 20   change..  **   
26de5 20 6d 65 74 61 5b 31 5d 20 20 20 46 69 6c 65 20   meta[1]   File 
26de6 66 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d 61  format of schema
26de7 20 6c 61 79 65 72 2e 0a 20 20 2a 2a 20 20 20 20   layer..  **    
26de8 6d 65 74 61 5b 32 5d 20 20 20 53 69 7a 65 20 6f  meta[2]   Size o
26de9 66 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  f the page cache
26dea 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 33  ..  **    meta[3
26deb 5d 20 20 20 55 73 65 20 66 72 65 65 6c 69 73 74  ]   Use freelist
26dec 20 69 66 20 30 2e 20 20 41 75 74 6f 76 61 63 75   if 0.  Autovacu
26ded 75 6d 20 69 66 20 67 72 65 61 74 65 72 20 74 68  um if greater th
26dee 61 6e 20 7a 65 72 6f 2e 0a 20 20 2a 2a 20 20 20  an zero..  **   
26def 20 6d 65 74 61 5b 34 5d 20 20 20 44 62 20 74 65   meta[4]   Db te
26df0 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 31 3a 55  xt encoding. 1:U
26df1 54 46 2d 38 20 32 3a 55 54 46 2d 31 36 4c 45 20  TF-8 2:UTF-16LE 
26df2 33 3a 55 54 46 2d 31 36 42 45 0a 20 20 2a 2a 20  3:UTF-16BE.  ** 
26df3 20 20 20 6d 65 74 61 5b 35 5d 20 20 20 54 68 65     meta[5]   The
26df4 20 75 73 65 72 20 63 6f 6f 6b 69 65 2e 20 55 73   user cookie. Us
26df5 65 64 20 62 79 20 74 68 65 20 61 70 70 6c 69 63  ed by the applic
26df6 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 20 20 20 6d  ation..  **    m
26df7 65 74 61 5b 36 5d 20 20 20 49 6e 63 72 65 6d 65  eta[6]   Increme
26df8 6e 74 61 6c 2d 76 61 63 75 75 6d 20 66 6c 61 67  ntal-vacuum flag
26df9 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 37  ..  **    meta[7
26dfa 5d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 38  ].  **    meta[8
26dfb 5d 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 39  ].  **    meta[9
26dfc 5d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ].  **.  ** Note
26dfd 3a 20 54 68 65 20 23 64 65 66 69 6e 65 64 20 53  : The #defined S
26dfe 51 4c 49 54 45 5f 55 54 46 2a 20 73 79 6d 62 6f  QLITE_UTF* symbo
26dff 6c 73 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ls in sqliteInt.
26e00 68 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 0a  h correspond to.
26e01 20 20 2a 2a 20 74 68 65 20 70 6f 73 73 69 62 6c    ** the possibl
26e02 65 20 76 61 6c 75 65 73 20 6f 66 20 6d 65 74 61  e values of meta
26e03 5b 34 5d 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  [4]..  */.  for(
26e04 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
26e05 28 6d 65 74 61 29 3b 20 69 2b 2b 29 7b 0a 20 20  (meta); i++){.  
26e06 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
26e07 72 65 65 47 65 74 4d 65 74 61 28 70 44 62 2d 3e  reeGetMeta(pDb->
26e08 70 42 74 2c 20 69 2b 31 2c 20 28 75 33 32 20 2a  pBt, i+1, (u32 *
26e09 29 26 6d 65 74 61 5b 69 5d 29 3b 0a 20 20 20 20  )&meta[i]);.    
26e0a 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
26e0b 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
26e0c 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  (pzErrMsg, db, "
26e0d 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53  %s", sqlite3ErrS
26e0e 74 72 28 72 63 29 29 3b 0a 20 20 20 20 20 20 67  tr(rc));.      g
26e0f 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f  oto initone_erro
26e10 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  r_out;.    }.  }
26e11 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  .  pDb->pSchema-
26e12 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d  >schema_cookie =
26e13 20 6d 65 74 61 5b 30 5d 3b 0a 0a 20 20 2f 2a 20   meta[0];..  /* 
26e14 49 66 20 6f 70 65 6e 69 6e 67 20 61 20 6e 6f 6e  If opening a non
26e15 2d 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 2c  -empty database,
26e16 20 63 68 65 63 6b 20 74 68 65 20 74 65 78 74 20   check the text 
26e17 65 6e 63 6f 64 69 6e 67 2e 20 46 6f 72 20 74 68  encoding. For th
26e18 65 0a 20 20 2a 2a 20 6d 61 69 6e 20 64 61 74 61  e.  ** main data
26e19 62 61 73 65 2c 20 73 65 74 20 73 71 6c 69 74 65  base, set sqlite
26e1a 33 2e 65 6e 63 20 74 6f 20 74 68 65 20 65 6e 63  3.enc to the enc
26e1b 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 6d 61 69  oding of the mai
26e1c 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a  n database..  **
26e1d 20 46 6f 72 20 61 6e 20 61 74 74 61 63 68 65 64   For an attached
26e1e 20 64 62 2c 20 69 74 20 69 73 20 61 6e 20 65 72   db, it is an er
26e1f 72 6f 72 20 69 66 20 74 68 65 20 65 6e 63 6f 64  ror if the encod
26e20 69 6e 67 20 69 73 20 6e 6f 74 20 74 68 65 20 73  ing is not the s
26e21 61 6d 65 0a 20 20 2a 2a 20 61 73 20 73 71 6c 69  ame.  ** as sqli
26e22 74 65 33 2e 65 6e 63 2e 0a 20 20 2a 2f 0a 20 20  te3.enc..  */.  
26e23 69 66 28 20 6d 65 74 61 5b 34 5d 20 29 7b 20 20  if( meta[4] ){  
26e24 2f 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  /* text encoding
26e25 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 44 62 3d   */.    if( iDb=
26e26 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  =0 ){.      /* I
26e27 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 6d 61  f opening the ma
26e28 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65 74  in database, set
26e29 20 45 4e 43 28 64 62 29 2e 20 2a 2f 0a 20 20 20   ENC(db). */.   
26e2a 20 20 20 45 4e 43 28 64 62 29 20 3d 20 28 75 38     ENC(db) = (u8
26e2b 29 6d 65 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20  )meta[4];.      
26e2c 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d 20  db->pDfltColl = 
26e2d 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
26e2e 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54  eq(db, SQLITE_UT
26e2f 46 38 2c 20 22 42 49 4e 41 52 59 22 2c 20 36 2c  F8, "BINARY", 6,
26e30 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
26e31 20 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65 6e        /* If open
26e32 69 6e 67 20 61 6e 20 61 74 74 61 63 68 65 64 20  ing an attached 
26e33 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 65 6e  database, the en
26e34 63 6f 64 69 6e 67 20 6d 75 63 68 20 6d 61 74 63  coding much matc
26e35 68 20 45 4e 43 28 64 62 29 20 2a 2f 0a 20 20 20  h ENC(db) */.   
26e36 20 20 20 69 66 28 20 6d 65 74 61 5b 34 5d 21 3d     if( meta[4]!=
26e37 45 4e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20  ENC(db) ){.     
26e38 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
26e39 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62  ing(pzErrMsg, db
26e3a 2c 20 22 61 74 74 61 63 68 65 64 20 64 61 74 61  , "attached data
26e3b 62 61 73 65 73 20 6d 75 73 74 20 75 73 65 20 74  bases must use t
26e3c 68 65 20 73 61 6d 65 22 0a 20 20 20 20 20 20 20  he same".       
26e3d 20 20 20 20 20 22 20 74 65 78 74 20 65 6e 63 6f       " text enco
26e3e 64 69 6e 67 20 61 73 20 6d 61 69 6e 20 64 61 74  ding as main dat
26e3f 61 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20 20  abase");.       
26e40 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
26e41 4f 52 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  OR;.        goto
26e42 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f   initone_error_o
26e43 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
26e44 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 44  }.  }else{.    D
26e45 62 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c  bSetProperty(db,
26e46 20 69 44 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b   iDb, DB_Empty);
26e47 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63 68  .  }.  pDb->pSch
26e48 65 6d 61 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64  ema->enc = ENC(d
26e49 62 29 3b 0a 0a 20 20 69 66 28 20 70 44 62 2d 3e  b);..  if( pDb->
26e4a 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
26e4b 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 69  ize==0 ){.    si
26e4c 7a 65 20 3d 20 6d 65 74 61 5b 32 5d 3b 0a 20 20  ze = meta[2];.  
26e4d 20 20 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b    if( size==0 ){
26e4e 20 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44   size = SQLITE_D
26e4f 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
26e50 45 3b 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a  E; }.    if( siz
26e51 65 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69  e<0 ) size = -si
26e52 7a 65 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63  ze;.    pDb->pSc
26e53 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
26e54 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 73 71 6c   = size;.    sql
26e55 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
26e56 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20  eSize(pDb->pBt, 
26e57 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
26e58 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a  che_size);.  }..
26e59 20 20 2f 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66    /*.  ** file_f
26e5a 6f 72 6d 61 74 3d 3d 31 20 20 20 20 56 65 72 73  ormat==1    Vers
26e5b 69 6f 6e 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20  ion 3.0.0..  ** 
26e5c 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20  file_format==2  
26e5d 20 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e    Version 3.1.3.
26e5e 20 20 2f 2f 20 41 4c 54 45 52 20 54 41 42 4c 45    // ALTER TABLE
26e5f 20 41 44 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a   ADD COLUMN.  **
26e60 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20   file_format==3 
26e61 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34     Version 3.1.4
26e62 2e 20 20 2f 2f 20 64 69 74 74 6f 20 62 75 74 20  .  // ditto but 
26e63 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65  with non-NULL de
26e64 66 61 75 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65  faults.  ** file
26e65 5f 66 6f 72 6d 61 74 3d 3d 34 20 20 20 20 56 65  _format==4    Ve
26e66 72 73 69 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f  rsion 3.3.0.  //
26e67 20 44 45 53 43 20 69 6e 64 69 63 65 73 2e 20 20   DESC indices.  
26e68 42 6f 6f 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74  Boolean constant
26e69 73 0a 20 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53  s.  */.  pDb->pS
26e6a 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
26e6b 61 74 20 3d 20 28 75 38 29 6d 65 74 61 5b 31 5d  at = (u8)meta[1]
26e6c 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63  ;.  if( pDb->pSc
26e6d 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
26e6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 62 2d  t==0 ){.    pDb-
26e6f 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
26e70 6f 72 6d 61 74 20 3d 20 31 3b 0a 20 20 7d 0a 20  ormat = 1;.  }. 
26e71 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d   if( pDb->pSchem
26e72 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 53  a->file_format>S
26e73 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46  QLITE_MAX_FILE_F
26e74 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 73 71 6c  ORMAT ){.    sql
26e75 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a  ite3SetString(pz
26e76 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 75 6e 73  ErrMsg, db, "uns
26e77 75 70 70 6f 72 74 65 64 20 66 69 6c 65 20 66 6f  upported file fo
26e78 72 6d 61 74 22 29 3b 0a 20 20 20 20 72 63 20 3d  rmat");.    rc =
26e79 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
26e7a 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
26e7b 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
26e7c 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 32 38 30    /* Ticket #280
26e7d 34 3a 20 20 57 68 65 6e 20 77 65 20 6f 70 65 6e  4:  When we open
26e7e 20 61 20 64 61 74 61 62 61 73 65 20 69 6e 20 74   a database in t
26e7f 68 65 20 6e 65 77 65 72 20 66 69 6c 65 20 66 6f  he newer file fo
26e80 72 6d 61 74 2c 0a 20 20 2a 2a 20 63 6c 65 61 72  rmat,.  ** clear
26e81 20 74 68 65 20 6c 65 67 61 63 79 5f 66 69 6c 65   the legacy_file
26e82 5f 66 6f 72 6d 61 74 20 70 72 61 67 6d 61 20 66  _format pragma f
26e83 6c 61 67 20 73 6f 20 74 68 61 74 20 61 20 56 41  lag so that a VA
26e84 43 55 55 4d 20 77 69 6c 6c 0a 20 20 2a 2a 20 6e  CUUM will.  ** n
26e85 6f 74 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65  ot downgrade the
26e86 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68   database and th
26e87 75 73 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e  us invalidate an
26e88 79 20 64 65 73 63 65 6e 64 69 6e 67 0a 20 20 2a  y descending.  *
26e89 2a 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 74  * indices that t
26e8a 68 65 20 75 73 65 72 20 6d 69 67 68 74 20 68 61  he user might ha
26e8b 76 65 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f  ve created..  */
26e8c 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20 26 26  .  if( iDb==0 &&
26e8d 20 6d 65 74 61 5b 31 5d 3e 3d 34 20 29 7b 0a 20   meta[1]>=4 ){. 
26e8e 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20     db->flags &= 
26e8f 7e 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69  ~SQLITE_LegacyFi
26e90 6c 65 46 6d 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  leFmt;.  }..  /*
26e91 20 52 65 61 64 20 74 68 65 20 73 63 68 65 6d 61   Read the schema
26e92 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74   information out
26e93 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 74   of the schema t
26e94 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 61 73 73  ables.  */.  ass
26e95 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75  ert( db->init.bu
26e96 73 79 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  sy );.  if( rc==
26e97 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a  SQLITE_EMPTY ){.
26e98 20 20 20 20 2f 2a 20 46 6f 72 20 61 6e 20 65 6d      /* For an em
26e99 70 74 79 20 64 61 74 61 62 61 73 65 2c 20 74 68  pty database, th
26e9a 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74  ere is nothing t
26e9b 6f 20 72 65 61 64 20 2a 2f 0a 20 20 20 20 72 63  o read */.    rc
26e9c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
26e9d 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20  }else{.    char 
26e9e 2a 7a 53 71 6c 3b 0a 20 20 20 20 7a 53 71 6c 20  *zSql;.    zSql 
26e9f 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
26ea0 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 22 53  (db, .        "S
26ea1 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74  ELECT name, root
26ea2 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27  page, sql FROM '
26ea3 25 71 27 2e 25 73 22 2c 0a 20 20 20 20 20 20 20  %q'.%s",.       
26ea4 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
26ea5 61 6d 65 2c 20 7a 4d 61 73 74 65 72 4e 61 6d 65  ame, zMasterName
26ea6 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  );.    (void)sql
26ea7 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
26ea8 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
26ea9 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
26eaa 54 49 4f 4e 0a 20 20 20 20 7b 0a 20 20 20 20 20  TION.    {.     
26eab 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
26eac 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
26ead 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
26eae 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
26eaf 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20  t char*);.      
26eb0 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74  xAuth = db->xAut
26eb1 68 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78 41 75  h;.      db->xAu
26eb2 74 68 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20  th = 0;.#endif. 
26eb3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
26eb4 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c  3_exec(db, zSql,
26eb5 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c   sqlite3InitCall
26eb6 62 61 63 6b 2c 20 26 69 6e 69 74 44 61 74 61 2c  back, &initData,
26eb7 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   0);.#ifndef SQL
26eb8 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
26eb9 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 64 62 2d  ZATION.      db-
26eba 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b 0a  >xAuth = xAuth;.
26ebb 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
26ebc 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
26ebd 4f 4b 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61  OK ) rc = initDa
26ebe 74 61 2e 72 63 3b 0a 20 20 20 20 28 76 6f 69 64  ta.rc;.    (void
26ebf 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
26ec0 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (db);.    sqlite
26ec1 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c  3DbFree(db, zSql
26ec2 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
26ec3 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 20  E_OMIT_ANALYZE. 
26ec4 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
26ec5 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
26ec6 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61  lite3AnalysisLoa
26ec7 64 28 64 62 2c 20 69 44 62 29 3b 0a 20 20 20 20  d(db, iDb);.    
26ec8 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69  }.#endif.  }.  i
26ec9 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
26eca 6c 65 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  led ){.    rc = 
26ecb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
26ecc 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
26ecd 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
26ece 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72   0);.  }.  if( r
26ecf 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
26ed0 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  (db->flags&SQLIT
26ed1 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 29 29  E_RecoveryMode))
26ed2 7b 0a 20 20 20 20 2f 2a 20 42 6c 61 63 6b 20 6d  {.    /* Black m
26ed3 61 67 69 63 3a 20 49 66 20 74 68 65 20 53 51 4c  agic: If the SQL
26ed4 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65  ITE_RecoveryMode
26ed5 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
26ed6 65 6e 20 63 6f 6e 73 69 64 65 72 0a 20 20 20 20  en consider.    
26ed7 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 6c 6f  ** the schema lo
26ed8 61 64 65 64 2c 20 65 76 65 6e 20 69 66 20 65 72  aded, even if er
26ed9 72 6f 72 73 20 6f 63 63 75 72 72 65 64 2e 20 49  rors occurred. I
26eda 6e 20 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e  n this situation
26edb 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 75 72   the .    ** cur
26edc 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65  rent sqlite3_pre
26edd 70 61 72 65 28 29 20 6f 70 65 72 61 74 69 6f 6e  pare() operation
26ede 20 77 69 6c 6c 20 66 61 69 6c 2c 20 62 75 74 20   will fail, but 
26edf 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 6e  the following on
26ee0 65 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 61 74  e.    ** will at
26ee1 74 65 6d 70 74 20 74 6f 20 63 6f 6d 70 69 6c 65  tempt to compile
26ee2 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 74   the supplied st
26ee3 61 74 65 6d 65 6e 74 20 61 67 61 69 6e 73 74 20  atement against 
26ee4 77 68 61 74 65 76 65 72 20 73 75 62 73 65 74 0a  whatever subset.
26ee5 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63      ** of the sc
26ee6 68 65 6d 61 20 77 61 73 20 6c 6f 61 64 65 64 20  hema was loaded 
26ee7 62 65 66 6f 72 65 20 74 68 65 20 65 72 72 6f 72  before the error
26ee8 20 6f 63 63 75 72 72 65 64 2e 20 54 68 65 20 70   occurred. The p
26ee9 72 69 6d 61 72 79 0a 20 20 20 20 2a 2a 20 70 75  rimary.    ** pu
26eea 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 69 73  rpose of this is
26eeb 20 74 6f 20 61 6c 6c 6f 77 20 61 63 63 65 73 73   to allow access
26eec 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d   to the sqlite_m
26eed 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 20 20  aster table.    
26eee 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 69 74 73  ** even when its
26eef 20 63 6f 6e 74 65 6e 74 73 20 68 61 76 65 20 62   contents have b
26ef0 65 65 6e 20 63 6f 72 72 75 70 74 65 64 2e 0a 20  een corrupted.. 
26ef1 20 20 20 2a 2f 0a 20 20 20 20 44 62 53 65 74 50     */.    DbSetP
26ef2 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c  roperty(db, iDb,
26ef3 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
26ef4 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
26ef5 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
26ef6 20 4a 75 6d 70 20 68 65 72 65 20 66 6f 72 20 61   Jump here for a
26ef7 6e 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63  n error that occ
26ef8 75 72 73 20 61 66 74 65 72 20 73 75 63 63 65 73  urs after succes
26ef9 73 66 75 6c 6c 79 20 61 6c 6c 6f 63 61 74 69 6e  sfully allocatin
26efa 67 0a 20 20 2a 2a 20 63 75 72 4d 61 69 6e 20 61  g.  ** curMain a
26efb 6e 64 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  nd calling sqlit
26efc 65 33 42 74 72 65 65 45 6e 74 65 72 28 29 2e 20  e3BtreeEnter(). 
26efd 46 6f 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61  For an error tha
26efe 74 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20 62 65  t occurs.  ** be
26eff 66 6f 72 65 20 74 68 61 74 20 70 6f 69 6e 74 2c  fore that point,
26f00 20 6a 75 6d 70 20 74 6f 20 65 72 72 6f 72 5f 6f   jump to error_o
26f01 75 74 2e 0a 20 20 2a 2f 0a 69 6e 69 74 6f 6e 65  ut..  */.initone
26f02 5f 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 73 71  _error_out:.  sq
26f03 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43  lite3BtreeCloseC
26f04 75 72 73 6f 72 28 63 75 72 4d 61 69 6e 29 3b 0a  ursor(curMain);.
26f05 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
26f06 75 72 4d 61 69 6e 29 3b 0a 20 20 73 71 6c 69 74  urMain);.  sqlit
26f07 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 44 62  e3BtreeLeave(pDb
26f08 2d 3e 70 42 74 29 3b 0a 0a 65 72 72 6f 72 5f 6f  ->pBt);..error_o
26f09 75 74 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  ut:.  if( rc==SQ
26f0a 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63  LITE_NOMEM || rc
26f0b 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  ==SQLITE_IOERR_N
26f0c 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 64 62 2d 3e  OMEM ){.    db->
26f0d 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
26f0e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
26f0f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  c;.}../*.** Init
26f10 69 61 6c 69 7a 65 20 61 6c 6c 20 64 61 74 61 62  ialize all datab
26f11 61 73 65 20 66 69 6c 65 73 20 2d 20 74 68 65 20  ase files - the 
26f12 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
26f13 6c 65 2c 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20  le, the file.** 
26f14 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 65  used to store te
26f15 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20  mporary tables, 
26f16 61 6e 64 20 61 6e 79 20 61 64 64 69 74 69 6f 6e  and any addition
26f17 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  al database file
26f18 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 75 73 69  s.** created usi
26f19 6e 67 20 41 54 54 41 43 48 20 73 74 61 74 65 6d  ng ATTACH statem
26f1a 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 20  ents.  Return a 
26f1b 73 75 63 63 65 73 73 20 63 6f 64 65 2e 20 20 49  success code.  I
26f1c 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63  f an.** error oc
26f1d 63 75 72 73 2c 20 77 72 69 74 65 20 61 6e 20 65  curs, write an e
26f1e 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74  rror message int
26f1f 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a  o *pzErrMsg..**.
26f20 2a 2a 20 41 66 74 65 72 20 61 20 64 61 74 61 62  ** After a datab
26f21 61 73 65 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  ase is initializ
26f22 65 64 2c 20 74 68 65 20 44 42 5f 53 63 68 65 6d  ed, the DB_Schem
26f23 61 4c 6f 61 64 65 64 20 62 69 74 20 69 73 20 73  aLoaded bit is s
26f24 65 74 0a 2a 2a 20 62 69 74 20 69 73 20 73 65 74  et.** bit is set
26f25 20 69 6e 20 74 68 65 20 66 6c 61 67 73 20 66 69   in the flags fi
26f26 65 6c 64 20 6f 66 20 74 68 65 20 44 62 20 73 74  eld of the Db st
26f27 72 75 63 74 75 72 65 2e 20 49 66 20 74 68 65 20  ructure. If the 
26f28 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65  database.** file
26f29 20 77 61 73 20 6f 66 20 7a 65 72 6f 2d 6c 65 6e   was of zero-len
26f2a 67 74 68 2c 20 74 68 65 6e 20 74 68 65 20 44 42  gth, then the DB
26f2b 5f 45 6d 70 74 79 20 66 6c 61 67 20 69 73 20 61  _Empty flag is a
26f2c 6c 73 6f 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  lso set..*/.SQLI
26f2d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
26f2e 71 6c 69 74 65 33 49 6e 69 74 28 73 71 6c 69 74  qlite3Init(sqlit
26f2f 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70  e3 *db, char **p
26f30 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrMsg){.  int 
26f31 69 2c 20 72 63 3b 0a 20 20 69 6e 74 20 63 6f 6d  i, rc;.  int com
26f32 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20 3d 20 21  mit_internal = !
26f33 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  (db->flags&SQLIT
26f34 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29  E_InternChanges)
26f35 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73  ;.  .  assert( s
26f36 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
26f37 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
26f38 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
26f39 75 73 79 20 29 20 72 65 74 75 72 6e 20 53 51 4c  usy ) return SQL
26f3a 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d 20 53  ITE_OK;.  rc = S
26f3b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 64 62 2d 3e  QLITE_OK;.  db->
26f3c 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20  init.busy = 1;. 
26f3d 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51   for(i=0; rc==SQ
26f3e 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d  LITE_OK && i<db-
26f3f 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
26f40 69 66 28 20 44 62 48 61 73 50 72 6f 70 65 72 74  if( DbHasPropert
26f41 79 28 64 62 2c 20 69 2c 20 44 42 5f 53 63 68 65  y(db, i, DB_Sche
26f42 6d 61 4c 6f 61 64 65 64 29 20 7c 7c 20 69 3d 3d  maLoaded) || i==
26f43 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  1 ) continue;.  
26f44 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e    rc = sqlite3In
26f45 69 74 4f 6e 65 28 64 62 2c 20 69 2c 20 70 7a 45  itOne(db, i, pzE
26f46 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 28 20  rrMsg);.    if( 
26f47 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rc ){.      sqli
26f48 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
26f49 53 63 68 65 6d 61 28 64 62 2c 20 69 29 3b 0a 20  Schema(db, i);. 
26f4a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f     }.  }..  /* O
26f4b 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f 74 68 65  nce all the othe
26f4c 72 20 64 61 74 61 62 61 73 65 73 20 68 61 76 65  r databases have
26f4d 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 73 65   been initialise
26f4e 64 2c 20 6c 6f 61 64 20 74 68 65 20 73 63 68 65  d, load the sche
26f4f 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  ma.  ** for the 
26f50 54 45 4d 50 20 64 61 74 61 62 61 73 65 2e 20 54  TEMP database. T
26f51 68 69 73 20 69 73 20 6c 6f 61 64 65 64 20 6c 61  his is loaded la
26f52 73 74 2c 20 61 73 20 74 68 65 20 54 45 4d 50 20  st, as the TEMP 
26f53 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 73 63  database.  ** sc
26f54 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74 61 69 6e  hema may contain
26f55 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 6f   references to o
26f56 62 6a 65 63 74 73 20 69 6e 20 6f 74 68 65 72 20  bjects in other 
26f57 64 61 74 61 62 61 73 65 73 2e 0a 20 20 2a 2f 0a  databases..  */.
26f58 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
26f59 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 69 66 28  MIT_TEMPDB.  if(
26f5a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
26f5b 26 20 64 62 2d 3e 6e 44 62 3e 31 20 26 26 20 21  & db->nDb>1 && !
26f5c 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62  DbHasProperty(db
26f5d 2c 20 31 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f  , 1, DB_SchemaLo
26f5e 61 64 65 64 29 20 29 7b 0a 20 20 20 20 72 63 20  aded) ){.    rc 
26f5f 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65  = sqlite3InitOne
26f60 28 64 62 2c 20 31 2c 20 70 7a 45 72 72 4d 73 67  (db, 1, pzErrMsg
26f61 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
26f62 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
26f63 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
26f64 61 28 64 62 2c 20 31 29 3b 0a 20 20 20 20 7d 0a  a(db, 1);.    }.
26f65 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62    }.#endif..  db
26f66 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b  ->init.busy = 0;
26f67 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
26f68 45 5f 4f 4b 20 26 26 20 63 6f 6d 6d 69 74 5f 69  E_OK && commit_i
26f69 6e 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73  nternal ){.    s
26f6a 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65  qlite3CommitInte
26f6b 72 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b  rnalChanges(db);
26f6c 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
26f6d 63 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  c; .}../*.** Thi
26f6e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
26f6f 6f 2d 6f 70 20 69 66 20 74 68 65 20 64 61 74 61  o-op if the data
26f70 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 61  base schema is a
26f71 6c 72 65 61 64 79 20 69 6e 69 74 69 61 6c 69 73  lready initialis
26f72 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  ed..** Otherwise
26f73 2c 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20  , the schema is 
26f74 6c 6f 61 64 65 64 2e 20 41 6e 20 65 72 72 6f 72  loaded. An error
26f75 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
26f76 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
26f77 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
26f78 52 65 61 64 53 63 68 65 6d 61 28 50 61 72 73 65  ReadSchema(Parse
26f79 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74   *pParse){.  int
26f7a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
26f7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
26f7c 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61   pParse->db;.  a
26f7d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
26f7e 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
26f7f 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 21 64  tex) );.  if( !d
26f80 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
26f81 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
26f82 49 6e 69 74 28 64 62 2c 20 26 70 50 61 72 73 65  Init(db, &pParse
26f83 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a  ->zErrMsg);.  }.
26f84 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
26f85 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73  _OK ){.    pPars
26f86 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20  e->rc = rc;.    
26f87 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a  pParse->nErr++;.
26f88 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
26f89 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  .}.../*.** Check
26f8a 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20   schema cookies 
26f8b 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  in all databases
26f8c 2e 20 20 49 66 20 61 6e 79 20 63 6f 6f 6b 69 65  .  If any cookie
26f8d 20 69 73 20 6f 75 74 0a 2a 2a 20 6f 66 20 64 61   is out.** of da
26f8e 74 65 2c 20 72 65 74 75 72 6e 20 30 2e 20 20 49  te, return 0.  I
26f8f 66 20 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f  f all schema coo
26f90 6b 69 65 73 20 61 72 65 20 63 75 72 72 65 6e 74  kies are current
26f91 2c 20 72 65 74 75 72 6e 20 31 2e 0a 2a 2f 0a 73  , return 1..*/.s
26f92 74 61 74 69 63 20 69 6e 74 20 73 63 68 65 6d 61  tatic int schema
26f93 49 73 56 61 6c 69 64 28 73 71 6c 69 74 65 33 20  IsValid(sqlite3 
26f94 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b  *db){.  int iDb;
26f95 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43  .  int rc;.  BtC
26f96 75 72 73 6f 72 20 2a 63 75 72 54 65 6d 70 3b 0a  ursor *curTemp;.
26f97 20 20 69 6e 74 20 63 6f 6f 6b 69 65 3b 0a 20 20    int cookie;.  
26f98 69 6e 74 20 61 6c 6c 4f 6b 20 3d 20 31 3b 0a 0a  int allOk = 1;..
26f99 20 20 63 75 72 54 65 6d 70 20 3d 20 28 42 74 43    curTemp = (BtC
26f9a 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33 4d  ursor *)sqlite3M
26f9b 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 42 74 72  alloc(sqlite3Btr
26f9c 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 29 3b  eeCursorSize());
26f9d 0a 20 20 69 66 28 20 63 75 72 54 65 6d 70 20 29  .  if( curTemp )
26f9e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  {.    assert( sq
26f9f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
26fa0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
26fa1 20 20 20 66 6f 72 28 69 44 62 3d 30 3b 20 61 6c     for(iDb=0; al
26fa2 6c 4f 6b 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  lOk && iDb<db->n
26fa3 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20  Db; iDb++){.    
26fa4 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20    Btree *pBt;.  
26fa5 20 20 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44      pBt = db->aD
26fa6 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20  b[iDb].pBt;.    
26fa7 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 20 63    if( pBt==0 ) c
26fa8 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 6d  ontinue;.      m
26fa9 65 6d 73 65 74 28 63 75 72 54 65 6d 70 2c 20 30  emset(curTemp, 0
26faa 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  , sqlite3BtreeCu
26fab 72 73 6f 72 53 69 7a 65 28 29 29 3b 0a 20 20 20  rsorSize());.   
26fac 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
26fad 74 72 65 65 43 75 72 73 6f 72 28 70 42 74 2c 20  treeCursor(pBt, 
26fae 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 30 2c 20  MASTER_ROOT, 0, 
26faf 30 2c 20 63 75 72 54 65 6d 70 29 3b 0a 20 20 20  0, curTemp);.   
26fb0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
26fb1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
26fb2 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
26fb3 65 47 65 74 4d 65 74 61 28 70 42 74 2c 20 31 2c  eGetMeta(pBt, 1,
26fb4 20 28 75 33 32 20 2a 29 26 63 6f 6f 6b 69 65 29   (u32 *)&cookie)
26fb5 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
26fb6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63  ==SQLITE_OK && c
26fb7 6f 6f 6b 69 65 21 3d 64 62 2d 3e 61 44 62 5b 69  ookie!=db->aDb[i
26fb8 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68  Db].pSchema->sch
26fb9 65 6d 61 5f 63 6f 6f 6b 69 65 20 29 7b 0a 20 20  ema_cookie ){.  
26fba 20 20 20 20 20 20 20 20 61 6c 6c 4f 6b 20 3d 20          allOk = 
26fbb 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
26fbc 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
26fbd 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 63 75 72  eCloseCursor(cur
26fbe 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Temp);.      }. 
26fbf 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
26fc0 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d  ITE_NOMEM || rc=
26fc1 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
26fc2 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 64  MEM ){.        d
26fc3 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
26fc4 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
26fc5 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
26fc6 72 65 65 28 63 75 72 54 65 6d 70 29 3b 0a 20 20  ree(curTemp);.  
26fc7 7d 65 6c 73 65 7b 0a 20 20 20 20 61 6c 6c 4f 6b  }else{.    allOk
26fc8 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6d 61   = 0;.    db->ma
26fc9 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
26fca 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 6c    }..  return al
26fcb 6c 4f 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  lOk;.}../*.** Co
26fcc 6e 76 65 72 74 20 61 20 73 63 68 65 6d 61 20 70  nvert a schema p
26fcd 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20  ointer into the 
26fce 69 44 62 20 69 6e 64 65 78 20 74 68 61 74 20 69  iDb index that i
26fcf 6e 64 69 63 61 74 65 73 0a 2a 2a 20 77 68 69 63  ndicates.** whic
26fd0 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  h database file 
26fd1 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 74 68 65  in db->aDb[] the
26fd2 20 73 63 68 65 6d 61 20 72 65 66 65 72 73 20 74   schema refers t
26fd3 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  o..**.** If the 
26fd4 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 69 73  same database is
26fd5 20 61 74 74 61 63 68 65 64 20 6d 6f 72 65 20 74   attached more t
26fd6 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 66 69  han once, the fi
26fd7 72 73 74 0a 2a 2a 20 61 74 74 61 63 68 65 64 20  rst.** attached 
26fd8 64 61 74 61 62 61 73 65 20 69 73 20 72 65 74 75  database is retu
26fd9 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
26fda 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
26fdb 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
26fdc 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63  (sqlite3 *db, Sc
26fdd 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 29 7b 0a  hema *pSchema){.
26fde 20 20 69 6e 74 20 69 20 3d 20 2d 31 30 30 30 30    int i = -10000
26fdf 30 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 53 63  00;..  /* If pSc
26fe0 68 65 6d 61 20 69 73 20 4e 55 4c 4c 2c 20 74 68  hema is NULL, th
26fe1 65 6e 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30  en return -10000
26fe2 30 30 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73  00. This happens
26fe3 20 77 68 65 6e 20 63 6f 64 65 20 69 6e 20 0a 20   when code in . 
26fe4 20 2a 2a 20 65 78 70 72 2e 63 20 69 73 20 74 72   ** expr.c is tr
26fe5 79 69 6e 67 20 74 6f 20 72 65 73 6f 6c 76 65 20  ying to resolve 
26fe6 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61  a reference to a
26fe7 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65   transient table
26fe8 20 28 69 2e 65 2e 20 6f 6e 65 0a 20 20 2a 2a 20   (i.e. one.  ** 
26fe9 63 72 65 61 74 65 64 20 62 79 20 61 20 73 75 62  created by a sub
26fea 2d 73 65 6c 65 63 74 29 2e 20 49 6e 20 74 68 69  -select). In thi
26feb 73 20 63 61 73 65 20 74 68 65 20 72 65 74 75 72  s case the retur
26fec 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  n value of this 
26fed 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73  .  ** function s
26fee 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 75  hould never be u
26fef 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  sed..  **.  ** W
26ff0 65 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30  e return -100000
26ff1 30 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  0 instead of the
26ff2 20 6d 6f 72 65 20 75 73 75 61 6c 20 2d 31 20 73   more usual -1 s
26ff3 69 6d 70 6c 79 20 62 65 63 61 75 73 65 20 75 73  imply because us
26ff4 69 6e 67 0a 20 20 2a 2a 20 2d 31 30 30 30 30 30  ing.  ** -100000
26ff5 30 20 61 73 20 74 68 65 20 69 6e 63 6f 72 72 65  0 as the incorre
26ff6 63 74 20 69 6e 64 65 78 20 69 6e 74 6f 20 64 62  ct index into db
26ff7 2d 3e 61 44 62 5b 5d 20 69 73 20 6d 75 63 68 20  ->aDb[] is much 
26ff8 0a 20 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65 6c  .  ** more likel
26ff9 79 20 74 6f 20 63 61 75 73 65 20 61 20 73 65 67  y to cause a seg
26ffa 66 61 75 6c 74 20 74 68 61 6e 20 2d 31 20 28 6f  fault than -1 (o
26ffb 66 20 63 6f 75 72 73 65 20 74 68 65 72 65 20 61  f course there a
26ffc 72 65 20 61 73 73 65 72 74 28 29 0a 20 20 2a 2a  re assert().  **
26ffd 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 6f 2c   statements too,
26ffe 20 62 75 74 20 69 74 20 6e 65 76 65 72 20 68 75   but it never hu
26fff 72 74 73 20 74 6f 20 70 6c 61 79 20 74 68 65 20  rts to play the 
27000 6f 64 64 73 29 2e 0a 20 20 2a 2f 0a 20 20 61 73  odds)..  */.  as
27001 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
27002 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
27003 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 53 63  ex) );.  if( pSc
27004 68 65 6d 61 20 29 7b 0a 20 20 20 20 66 6f 72 28  hema ){.    for(
27005 69 3d 30 3b 20 41 4c 57 41 59 53 28 69 3c 64 62  i=0; ALWAYS(i<db
27006 2d 3e 6e 44 62 29 3b 20 69 2b 2b 29 7b 0a 20 20  ->nDb); i++){.  
27007 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
27008 69 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68  i].pSchema==pSch
27009 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 62  ema ){.        b
2700a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2700b 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
2700c 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d 3e 6e 44  i>=0 && i<db->nD
2700d 62 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  b );.  }.  retur
2700e 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n i;.}../*.** Co
2700f 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 38 20  mpile the UTF-8 
27010 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
27011 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20  ement zSql into 
27012 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  a statement hand
27013 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
27014 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  t sqlite3Prepare
27015 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
27016 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27017 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
27018 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  . */.  const cha
27019 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
2701a 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
2701b 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
2701c 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
2701d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2701e 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
2701f 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
27020 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67   int saveSqlFlag
27021 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ,          /* Tr
27022 75 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74  ue to copy SQL t
27023 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ext into the sql
27024 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73  ite3_stmt */.  s
27025 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
27026 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
27027 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
27028 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
27029 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
2702a 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
2702b 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
2702c 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
2702d 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 73   */.){.  Parse s
2702e 50 61 72 73 65 3b 0a 20 20 63 68 61 72 20 2a 7a  Parse;.  char *z
2702f 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 6e  ErrMsg = 0;.  in
27030 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
27031 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69 66  ;.  int i;..  if
27032 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
27033 6e 28 64 62 29 20 29 20 72 65 74 75 72 6e 20 53  n(db) ) return S
27034 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
27035 61 73 73 65 72 74 28 20 70 70 53 74 6d 74 20 26  assert( ppStmt &
27036 26 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 0a  & *ppStmt==0 );.
27037 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
27038 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
27039 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2703a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
2703b 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20  mutex) );..  /* 
2703c 43 68 65 63 6b 20 74 6f 20 76 65 72 69 66 79 20  Check to verify 
2703d 74 68 61 74 20 69 74 20 69 73 20 70 6f 73 73 69  that it is possi
2703e 62 6c 65 20 74 6f 20 67 65 74 20 61 20 72 65 61  ble to get a rea
2703f 64 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 0a 20 20  d lock on all.  
27040 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ** database sche
27041 6d 61 73 2e 20 20 54 68 65 20 69 6e 61 62 69 6c  mas.  The inabil
27042 69 74 79 20 74 6f 20 67 65 74 20 61 20 72 65 61  ity to get a rea
27043 64 20 6c 6f 63 6b 20 69 6e 64 69 63 61 74 65 73  d lock indicates
27044 20 74 68 61 74 0a 20 20 2a 2a 20 73 6f 6d 65 20   that.  ** some 
27045 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
27046 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c  onnection is hol
27047 64 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63  ding a write-loc
27048 6b 2c 20 77 68 69 63 68 20 69 6e 0a 20 20 2a 2a  k, which in.  **
27049 20 74 75 72 6e 20 6d 65 61 6e 73 20 74 68 61 74   turn means that
2704a 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65   the other conne
2704b 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65 20 75  ction has made u
2704c 6e 63 6f 6d 6d 69 74 74 65 64 20 63 68 61 6e 67  ncommitted chang
2704d 65 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 73  es.  ** to the s
2704e 63 68 65 6d 61 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  chema..  **.  **
2704f 20 57 65 72 65 20 77 65 20 74 6f 20 70 72 6f 63   Were we to proc
27050 65 65 64 20 61 6e 64 20 70 72 65 70 61 72 65 20  eed and prepare 
27051 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61 67  the statement ag
27052 61 69 6e 73 74 20 74 68 65 20 75 6e 63 6f 6d 6d  ainst the uncomm
27053 69 74 74 65 64 0a 20 20 2a 2a 20 73 63 68 65 6d  itted.  ** schem
27054 61 20 63 68 61 6e 67 65 73 20 61 6e 64 20 69 66  a changes and if
27055 20 74 68 6f 73 65 20 73 63 68 65 6d 61 20 63 68   those schema ch
27056 61 6e 67 65 73 20 61 72 65 20 73 75 62 73 65 71  anges are subseq
27057 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 0a 20 20  uently rolled.  
27058 2a 2a 20 62 61 63 6b 20 61 6e 64 20 64 69 66 66  ** back and diff
27059 65 72 65 6e 74 20 63 68 61 6e 67 65 73 20 61 72  erent changes ar
2705a 65 20 6d 61 64 65 20 69 6e 20 74 68 65 69 72 20  e made in their 
2705b 70 6c 61 63 65 2c 20 74 68 65 6e 20 77 68 65 6e  place, then when
2705c 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65 70 61   this.  ** prepa
2705d 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67 6f  red statement go
2705e 65 73 20 74 6f 20 72 75 6e 20 74 68 65 20 73 63  es to run the sc
2705f 68 65 6d 61 20 63 6f 6f 6b 69 65 20 77 6f 75 6c  hema cookie woul
27060 64 20 66 61 69 6c 20 74 6f 20 64 65 74 65 63 74  d fail to detect
27061 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61  .  ** the schema
27062 20 63 68 61 6e 67 65 2e 20 20 44 69 73 61 73 74   change.  Disast
27063 65 72 20 77 6f 75 6c 64 20 66 6f 6c 6c 6f 77 2e  er would follow.
27064 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
27065 74 68 72 65 61 64 20 69 73 20 63 75 72 72 65 6e  thread is curren
27066 74 6c 79 20 68 6f 6c 64 69 6e 67 20 6d 75 74 65  tly holding mute
27067 78 65 73 20 6f 6e 20 61 6c 6c 20 42 74 72 65 65  xes on all Btree
27068 73 20 28 62 65 63 61 75 73 65 0a 20 20 2a 2a 20  s (because.  ** 
27069 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 42 74  of the sqlite3Bt
2706a 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20 69 6e  reeEnterAll() in
2706b 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
2706c 72 65 70 61 72 65 28 29 29 20 73 6f 20 69 74 0a  repare()) so it.
2706d 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73    ** is not poss
2706e 69 62 6c 65 20 66 6f 72 20 61 6e 6f 74 68 65 72  ible for another
2706f 20 74 68 72 65 61 64 20 74 6f 20 73 74 61 72 74   thread to start
27070 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20 63 68   a new schema ch
27071 61 6e 67 65 0a 20 20 2a 2a 20 77 68 69 6c 65 20  ange.  ** while 
27072 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
27073 72 75 6e 6e 69 6e 67 2e 20 20 48 65 6e 63 65 2c  running.  Hence,
27074 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20   we do not need 
27075 74 6f 20 68 6f 6c 64 20 0a 20 20 2a 2a 20 6c 6f  to hold .  ** lo
27076 63 6b 73 20 6f 6e 20 74 68 65 20 73 63 68 65 6d  cks on the schem
27077 61 2c 20 77 65 20 6a 75 73 74 20 6e 65 65 64 20  a, we just need 
27078 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f 62  to make sure nob
27079 6f 64 79 20 65 6c 73 65 20 69 73 20 0a 20 20 2a  ody else is .  *
2707a 2a 20 68 6f 6c 64 69 6e 67 20 74 68 65 6d 2e 0a  * holding them..
2707b 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74    **.  ** Note t
2707c 68 61 74 20 73 65 74 74 69 6e 67 20 52 45 41 44  hat setting READ
2707d 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 6f 76 65  _UNCOMMITTED ove
2707e 72 72 69 64 65 73 20 6d 6f 73 74 20 6c 6f 63 6b  rrides most lock
2707f 20 64 65 74 65 63 74 69 6f 6e 2c 0a 20 20 2a 2a   detection,.  **
27080 20 62 75 74 20 69 74 20 64 6f 65 73 20 2a 6e 6f   but it does *no
27081 74 2a 20 6f 76 65 72 72 69 64 65 20 73 63 68 65  t* override sche
27082 6d 61 20 6c 6f 63 6b 20 64 65 74 65 63 74 69 6f  ma lock detectio
27083 6e 2c 20 73 6f 20 74 68 69 73 20 61 6c 6c 20 73  n, so this all s
27084 74 69 6c 6c 0a 20 20 2a 2a 20 77 6f 72 6b 73 20  till.  ** works 
27085 65 76 65 6e 20 69 66 20 52 45 41 44 5f 55 4e 43  even if READ_UNC
27086 4f 4d 4d 49 54 54 45 44 20 69 73 20 73 65 74 2e  OMMITTED is set.
27087 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
27088 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
27089 20 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42   {.    Btree *pB
2708a 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
2708b 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74 20  Bt;.    if( pBt 
2708c 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
2708d 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
2708e 64 73 4d 75 74 65 78 28 70 42 74 29 20 29 3b 0a  dsMutex(pBt) );.
2708f 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
27090 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63  e3BtreeSchemaLoc
27091 6b 65 64 28 70 42 74 29 3b 0a 20 20 20 20 20 20  ked(pBt);.      
27092 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
27093 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
27094 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a  b = db->aDb[i].z
27095 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 71  Name;.        sq
27096 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
27097 63 2c 20 22 64 61 74 61 62 61 73 65 20 73 63 68  c, "database sch
27098 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25  ema is locked: %
27099 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20  s", zDb);.      
2709a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2709b 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
2709c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2709d 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
2709e 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
2709f 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65  ed );.        re
270a0 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45  turn sqlite3ApiE
270a1 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20  xit(db, rc);.   
270a2 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
270a3 20 0a 20 20 6d 65 6d 73 65 74 28 26 73 50 61 72   .  memset(&sPar
270a4 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 50  se, 0, sizeof(sP
270a5 61 72 73 65 29 29 3b 0a 20 20 73 50 61 72 73 65  arse));.  sParse
270a6 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20  .db = db;.  if( 
270a7 6e 42 79 74 65 73 3e 3d 30 20 26 26 20 28 6e 42  nBytes>=0 && (nB
270a8 79 74 65 73 3d 3d 30 20 7c 7c 20 7a 53 71 6c 5b  ytes==0 || zSql[
270a9 6e 42 79 74 65 73 2d 31 5d 21 3d 30 29 20 29 7b  nBytes-1]!=0) ){
270aa 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 43  .    char *zSqlC
270ab 6f 70 79 3b 0a 20 20 20 20 69 6e 74 20 6d 78 4c  opy;.    int mxL
270ac 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  en = db->aLimit[
270ad 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c  SQLITE_LIMIT_SQL
270ae 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20 69 66  _LENGTH];.    if
270af 28 20 6e 42 79 74 65 73 3e 6d 78 4c 65 6e 20 29  ( nBytes>mxLen )
270b0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
270b1 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
270b2 54 4f 4f 42 49 47 2c 20 22 73 74 61 74 65 6d 65  TOOBIG, "stateme
270b3 6e 74 20 74 6f 6f 20 6c 6f 6e 67 22 29 3b 0a 20  nt too long");. 
270b4 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
270b5 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
270b6 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71  .      return sq
270b7 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
270b8 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 29 3b   SQLITE_TOOBIG);
270b9 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71 6c 43  .    }.    zSqlC
270ba 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 44 62 53  opy = sqlite3DbS
270bb 74 72 4e 44 75 70 28 64 62 2c 20 7a 53 71 6c 2c  trNDup(db, zSql,
270bc 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66   nBytes);.    if
270bd 28 20 7a 53 71 6c 43 6f 70 79 20 29 7b 0a 20 20  ( zSqlCopy ){.  
270be 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61      sqlite3RunPa
270bf 72 73 65 72 28 26 73 50 61 72 73 65 2c 20 7a 53  rser(&sParse, zS
270c0 71 6c 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73 67  qlCopy, &zErrMsg
270c1 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
270c2 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 43  DbFree(db, zSqlC
270c3 6f 70 79 29 3b 0a 20 20 20 20 20 20 73 50 61 72  opy);.      sPar
270c4 73 65 2e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  se.zTail = &zSql
270c5 5b 73 50 61 72 73 65 2e 7a 54 61 69 6c 2d 7a 53  [sParse.zTail-zS
270c6 71 6c 43 6f 70 79 5d 3b 0a 20 20 20 20 7d 65 6c  qlCopy];.    }el
270c7 73 65 7b 0a 20 20 20 20 20 20 73 50 61 72 73 65  se{.      sParse
270c8 2e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 6e  .zTail = &zSql[n
270c9 42 79 74 65 73 5d 3b 0a 20 20 20 20 7d 0a 20 20  Bytes];.    }.  
270ca 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
270cb 65 33 52 75 6e 50 61 72 73 65 72 28 26 73 50 61  e3RunParser(&sPa
270cc 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72  rse, zSql, &zErr
270cd 4d 73 67 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  Msg);.  }..  if(
270ce 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
270cf 64 20 29 7b 0a 20 20 20 20 73 50 61 72 73 65 2e  d ){.    sParse.
270d0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
270d1 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 50 61  M;.  }.  if( sPa
270d2 72 73 65 2e 72 63 3d 3d 53 51 4c 49 54 45 5f 44  rse.rc==SQLITE_D
270d3 4f 4e 45 20 29 20 73 50 61 72 73 65 2e 72 63 20  ONE ) sParse.rc 
270d4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
270d5 66 28 20 73 50 61 72 73 65 2e 63 68 65 63 6b 53  f( sParse.checkS
270d6 63 68 65 6d 61 20 26 26 20 21 73 63 68 65 6d 61  chema && !schema
270d7 49 73 56 61 6c 69 64 28 64 62 29 20 29 7b 0a 20  IsValid(db) ){. 
270d8 20 20 20 73 50 61 72 73 65 2e 72 63 20 3d 20 53     sParse.rc = S
270d9 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20  QLITE_SCHEMA;.  
270da 7d 0a 20 20 69 66 28 20 73 50 61 72 73 65 2e 72  }.  if( sParse.r
270db 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c==SQLITE_SCHEMA
270dc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52   ){.    sqlite3R
270dd 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
270de 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20  ma(db, 0);.  }. 
270df 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
270e0 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 50 61  ailed ){.    sPa
270e1 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f  rse.rc = SQLITE_
270e2 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
270e3 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2a   pzTail ){.    *
270e4 70 7a 54 61 69 6c 20 3d 20 73 50 61 72 73 65 2e  pzTail = sParse.
270e5 7a 54 61 69 6c 3b 0a 20 20 7d 0a 20 20 72 63 20  zTail;.  }.  rc 
270e6 3d 20 73 50 61 72 73 65 2e 72 63 3b 0a 0a 23 69  = sParse.rc;..#i
270e7 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
270e8 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20  T_EXPLAIN.  if( 
270e9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
270ea 20 73 50 61 72 73 65 2e 70 56 64 62 65 20 26 26   sParse.pVdbe &&
270eb 20 73 50 61 72 73 65 2e 65 78 70 6c 61 69 6e 20   sParse.explain 
270ec 29 7b 0a 20 20 20 20 69 66 28 20 73 50 61 72 73  ){.    if( sPars
270ed 65 2e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a  e.explain==2 ){.
270ee 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
270ef 65 53 65 74 4e 75 6d 43 6f 6c 73 28 73 50 61 72  eSetNumCols(sPar
270f0 73 65 2e 70 56 64 62 65 2c 20 33 29 3b 0a 20 20  se.pVdbe, 3);.  
270f1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
270f2 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65  etColName(sParse
270f3 2e 70 56 64 62 65 2c 20 30 2c 20 43 4f 4c 4e 41  .pVdbe, 0, COLNA
270f4 4d 45 5f 4e 41 4d 45 2c 20 22 6f 72 64 65 72 22  ME_NAME, "order"
270f5 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
270f6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
270f7 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50  dbeSetColName(sP
270f8 61 72 73 65 2e 70 56 64 62 65 2c 20 31 2c 20 43  arse.pVdbe, 1, C
270f9 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 72  OLNAME_NAME, "fr
270fa 6f 6d 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  om", SQLITE_STAT
270fb 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
270fc 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
270fd 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 32  (sParse.pVdbe, 2
270fe 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
270ff 22 64 65 74 61 69 6c 22 2c 20 53 51 4c 49 54 45  "detail", SQLITE
27100 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 65  _STATIC);.    }e
27101 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
27102 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
27103 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 38  (sParse.pVdbe, 8
27104 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27105 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73  VdbeSetColName(s
27106 50 61 72 73 65 2e 70 56 64 62 65 2c 20 30 2c 20  Parse.pVdbe, 0, 
27107 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 61  COLNAME_NAME, "a
27108 64 64 72 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ddr", SQLITE_STA
27109 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
2710a 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2710b 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20  e(sParse.pVdbe, 
2710c 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
2710d 20 22 6f 70 63 6f 64 65 22 2c 20 53 51 4c 49 54   "opcode", SQLIT
2710e 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
2710f 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
27110 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56  olName(sParse.pV
27111 64 62 65 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f  dbe, 2, COLNAME_
27112 4e 41 4d 45 2c 20 22 70 31 22 2c 20 53 51 4c 49  NAME, "p1", SQLI
27113 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
27114 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
27115 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70  ColName(sParse.p
27116 56 64 62 65 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45  Vdbe, 3, COLNAME
27117 5f 4e 41 4d 45 2c 20 22 70 32 22 2c 20 53 51 4c  _NAME, "p2", SQL
27118 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
27119 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2711a 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e  tColName(sParse.
2711b 70 56 64 62 65 2c 20 34 2c 20 43 4f 4c 4e 41 4d  pVdbe, 4, COLNAM
2711c 45 5f 4e 41 4d 45 2c 20 22 70 33 22 2c 20 53 51  E_NAME, "p3", SQ
2711d 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
2711e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2711f 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65  etColName(sParse
27120 2e 70 56 64 62 65 2c 20 35 2c 20 43 4f 4c 4e 41  .pVdbe, 5, COLNA
27121 4d 45 5f 4e 41 4d 45 2c 20 22 70 34 22 2c 20 53  ME_NAME, "p4", S
27122 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
27123 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27124 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72 73  SetColName(sPars
27125 65 2e 70 56 64 62 65 2c 20 36 2c 20 43 4f 4c 4e  e.pVdbe, 6, COLN
27126 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 35 22 2c 20  AME_NAME, "p5", 
27127 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
27128 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27129 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
2712a 73 65 2e 70 56 64 62 65 2c 20 37 2c 20 43 4f 4c  se.pVdbe, 7, COL
2712b 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 6f 6d 6d  NAME_NAME, "comm
2712c 65 6e 74 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ent", SQLITE_STA
2712d 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TIC);.    }.  }.
2712e 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71  #endif..  if( sq
2712f 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
27130 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  b) ){.    rc = S
27131 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
27132 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  }..  assert( db-
27133 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 7c 7c  >init.busy==0 ||
27134 20 73 61 76 65 53 71 6c 46 6c 61 67 3d 3d 30 20   saveSqlFlag==0 
27135 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  );.  if( db->ini
27136 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20  t.busy==0 ){.   
27137 20 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 73   Vdbe *pVdbe = s
27138 50 61 72 73 65 2e 70 56 64 62 65 3b 0a 20 20 20  Parse.pVdbe;.   
27139 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53   sqlite3VdbeSetS
2713a 71 6c 28 70 56 64 62 65 2c 20 7a 53 71 6c 2c 20  ql(pVdbe, zSql, 
2713b 28 69 6e 74 29 28 73 50 61 72 73 65 2e 7a 54 61  (int)(sParse.zTa
2713c 69 6c 2d 7a 53 71 6c 29 2c 20 73 61 76 65 53 71  il-zSql), saveSq
2713d 6c 46 6c 61 67 29 3b 0a 20 20 7d 0a 20 20 69 66  lFlag);.  }.  if
2713e 28 20 73 50 61 72 73 65 2e 70 56 64 62 65 20 26  ( sParse.pVdbe &
2713f 26 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  & (rc!=SQLITE_OK
27140 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
27141 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 73 71 6c  iled) ){.    sql
27142 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
27143 28 73 50 61 72 73 65 2e 70 56 64 62 65 29 3b 0a  (sParse.pVdbe);.
27144 20 20 20 20 61 73 73 65 72 74 28 21 28 2a 70 70      assert(!(*pp
27145 53 74 6d 74 29 29 3b 0a 20 20 7d 65 6c 73 65 7b  Stmt));.  }else{
27146 0a 20 20 20 20 2a 70 70 53 74 6d 74 20 3d 20 28  .    *ppStmt = (
27147 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 73 50  sqlite3_stmt*)sP
27148 61 72 73 65 2e 70 56 64 62 65 3b 0a 20 20 7d 0a  arse.pVdbe;.  }.
27149 0a 20 20 69 66 28 20 7a 45 72 72 4d 73 67 20 29  .  if( zErrMsg )
2714a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2714b 6f 72 28 64 62 2c 20 72 63 2c 20 22 25 73 22 2c  or(db, rc, "%s",
2714c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 73   zErrMsg);.    s
2714d 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2714e 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 65 6c   zErrMsg);.  }el
2714f 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  se{.    sqlite3E
27150 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b  rror(db, rc, 0);
27151 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c  .  }..  rc = sql
27152 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
27153 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  rc);.  assert( (
27154 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d  rc&db->errMask)=
27155 3d 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  =rc );.  return 
27156 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  rc;.}.static int
27157 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
27158 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65  repare(.  sqlite
27159 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2715a 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2715b 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e  handle. */.  con
2715c 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
2715d 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20         /* UTF-8 
2715e 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
2715f 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20  ement. */.  int 
27160 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20  nBytes,         
27161 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
27162 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
27163 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53  . */.  int saveS
27164 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20  qlFlag,         
27165 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70 79   /* True to copy
27166 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74   SQL text into t
27167 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  he sqlite3_stmt 
27168 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
27169 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
2716a 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
2716b 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
2716c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2716d 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
2716e 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ail       /* OUT
2716f 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20  : End of parsed 
27170 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  string */.){.  i
27171 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
27172 20 70 70 53 74 6d 74 21 3d 30 20 29 3b 0a 20 20   ppStmt!=0 );.  
27173 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69  *ppStmt = 0;.  i
27174 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
27175 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a  yCheckOk(db) ){.
27176 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
27177 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
27178 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
27179 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
2717a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
2717b 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63  terAll(db);.  rc
2717c 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61 72   = sqlite3Prepar
2717d 65 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74  e(db, zSql, nByt
2717e 65 73 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  es, saveSqlFlag,
2717f 20 70 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29   ppStmt, pzTail)
27180 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
27181 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20  LeaveAll(db);.  
27182 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
27183 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
27184 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
27185 2f 2a 0a 2a 2a 20 52 65 72 75 6e 20 74 68 65 20  /*.** Rerun the 
27186 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 61  compilation of a
27187 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72   statement after
27188 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65   a schema change
27189 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73  ..**.** If the s
2718a 74 61 74 65 6d 65 6e 74 20 69 73 20 73 75 63 63  tatement is succ
2718b 65 73 73 66 75 6c 6c 79 20 72 65 63 6f 6d 70 69  essfully recompi
2718c 6c 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  led, return SQLI
2718d 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65  TE_OK. Otherwise
2718e 2c 0a 2a 2a 20 69 66 20 74 68 65 20 73 74 61 74  ,.** if the stat
2718f 65 6d 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20  ement cannot be 
27190 72 65 63 6f 6d 70 69 6c 65 64 20 62 65 63 61 75  recompiled becau
27191 73 65 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65  se another conne
27192 63 74 69 6f 6e 20 68 61 73 0a 2a 2a 20 6c 6f 63  ction has.** loc
27193 6b 65 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ked the sqlite3_
27194 6d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 72 65  master table, re
27195 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
27196 45 44 2e 20 49 66 20 61 6e 79 20 6f 74 68 65 72  ED. If any other
27197 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73   error.** occurs
27198 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
27199 53 43 48 45 4d 41 2e 0a 2a 2f 0a 53 51 4c 49 54  SCHEMA..*/.SQLIT
2719a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2719b 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56  lite3Reprepare(V
2719c 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  dbe *p){.  int r
2719d 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  c;.  sqlite3_stm
2719e 74 20 2a 70 4e 65 77 3b 0a 20 20 63 6f 6e 73 74  t *pNew;.  const
2719f 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 73   char *zSql;.  s
271a0 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 61  qlite3 *db;..  a
271a1 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
271a2 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65  utex_held(sqlite
271a3 33 56 64 62 65 44 62 28 70 29 2d 3e 6d 75 74 65  3VdbeDb(p)->mute
271a4 78 29 20 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73  x) );.  zSql = s
271a5 71 6c 69 74 65 33 5f 73 71 6c 28 28 73 71 6c 69  qlite3_sql((sqli
271a6 74 65 33 5f 73 74 6d 74 20 2a 29 70 29 3b 0a 20  te3_stmt *)p);. 
271a7 20 61 73 73 65 72 74 28 20 7a 53 71 6c 21 3d 30   assert( zSql!=0
271a8 20 29 3b 20 20 2f 2a 20 52 65 70 72 65 70 61 72   );  /* Reprepar
271a9 65 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 6f  e only called fo
271aa 72 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 73  r prepare_v2() s
271ab 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 64  tatements */.  d
271ac 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 44  b = sqlite3VdbeD
271ad 62 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  b(p);.  assert( 
271ae 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
271af 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
271b0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c  .  rc = sqlite3L
271b1 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62  ockAndPrepare(db
271b2 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 30 2c 20 26  , zSql, -1, 0, &
271b3 70 4e 65 77 2c 20 30 29 3b 0a 20 20 69 66 28 20  pNew, 0);.  if( 
271b4 72 63 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63  rc ){.    if( rc
271b5 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
271b6 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
271b7 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
271b8 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
271b9 70 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 72  pNew==0 );.    r
271ba 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54  eturn (rc==SQLIT
271bb 45 5f 4c 4f 43 4b 45 44 29 20 3f 20 53 51 4c 49  E_LOCKED) ? SQLI
271bc 54 45 5f 4c 4f 43 4b 45 44 20 3a 20 53 51 4c 49  TE_LOCKED : SQLI
271bd 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 65 6c  TE_SCHEMA;.  }el
271be 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
271bf 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20 7d 0a 20  pNew!=0 );.  }. 
271c0 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70   sqlite3VdbeSwap
271c1 28 28 56 64 62 65 2a 29 70 4e 65 77 2c 20 70 29  ((Vdbe*)pNew, p)
271c2 3b 0a 20 20 73 71 6c 69 74 65 33 54 72 61 6e 73  ;.  sqlite3Trans
271c3 66 65 72 42 69 6e 64 69 6e 67 73 28 70 4e 65 77  ferBindings(pNew
271c4 2c 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  , (sqlite3_stmt*
271c5 29 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  )p);.  sqlite3Vd
271c6 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75 6c  beResetStepResul
271c7 74 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b 0a  t((Vdbe*)pNew);.
271c8 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
271c9 61 6c 69 7a 65 28 28 56 64 62 65 2a 29 70 4e 65  alize((Vdbe*)pNe
271ca 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  w);.  return SQL
271cb 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ITE_OK;.}.../*.*
271cc 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f  * Two versions o
271cd 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20 41  f the official A
271ce 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64 20  PI.  Legacy and 
271cf 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68 65  new use.  In the
271d0 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73 69   legacy.** versi
271d1 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  on, the original
271d2 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f 74   SQL text is not
271d3 20 73 61 76 65 64 20 69 6e 20 74 68 65 20 70 72   saved in the pr
271d4 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
271d5 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61 20  .** and so if a 
271d6 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f 63  schema change oc
271d7 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43 48  curs, SQLITE_SCH
271d8 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64 20  EMA is returned 
271d9 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74  by.** sqlite3_st
271da 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20 6e 65  ep().  In the ne
271db 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f  w version, the o
271dc 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74  riginal SQL text
271dd 20 69 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20   is retained.** 
271de 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65 6e  and the statemen
271df 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c  t is automatical
271e0 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 69 66  ly recompiled if
271e1 20 61 6e 20 73 63 68 65 6d 61 20 63 68 61 6e 67   an schema chang
271e2 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a  e.** occurs..*/.
271e3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
271e4 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a  qlite3_prepare(.
271e5 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
271e6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
271e7 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
271e8 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
271e9 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f  *zSql,         /
271ea 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  * UTF-8 encoded 
271eb 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  SQL statement. *
271ec 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  /.  int nBytes, 
271ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
271ee 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   Length of zSql 
271ef 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
271f0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
271f1 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
271f2 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
271f3 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
271f4 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
271f5 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
271f6 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
271f7 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
271f8 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
271f9 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c  .  rc = sqlite3L
271fa 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62  ockAndPrepare(db
271fb 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70  ,zSql,nBytes,0,p
271fc 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20  pStmt,pzTail);. 
271fd 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
271fe 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74  ITE_OK || ppStmt
271ff 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d  ==0 || *ppStmt==
27200 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a  0 );  /* VERIFY:
27201 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74   F13021 */.  ret
27202 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
27203 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
27204 5f 70 72 65 70 61 72 65 5f 76 32 28 0a 20 20 73  _prepare_v2(.  s
27205 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
27206 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
27207 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a  base handle. */.
27208 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
27209 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ql,         /* U
2720a 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
2720b 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
2720c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
2720d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
2720e 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
2720f 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69  bytes. */.  sqli
27210 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
27211 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20  t,    /* OUT: A 
27212 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
27213 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
27214 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
27215 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20  r **pzTail      
27216 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20   /* OUT: End of 
27217 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f  parsed string */
27218 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
27219 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b  rc = sqlite3Lock
2721a 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53  AndPrepare(db,zS
2721b 71 6c 2c 6e 42 79 74 65 73 2c 31 2c 70 70 53 74  ql,nBytes,1,ppSt
2721c 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73  mt,pzTail);.  as
2721d 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
2721e 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30  _OK || ppStmt==0
2721f 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29   || *ppStmt==0 )
27220 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31  ;  /* VERIFY: F1
27221 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3021 */.  return
27222 20 72 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66   rc;.}...#ifndef
27223 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
27224 31 36 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65  16./*.** Compile
27225 20 74 68 65 20 55 54 46 2d 31 36 20 65 6e 63 6f   the UTF-16 enco
27226 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ded SQL statemen
27227 74 20 7a 53 71 6c 20 69 6e 74 6f 20 61 20 73 74  t zSql into a st
27228 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a  atement handle..
27229 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
2722a 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28 0a  lite3Prepare16(.
2722b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2722c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2722d 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
2722e 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  */ .  const void
2722f 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
27230 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  /* UTF-8 encoded
27231 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
27232 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c  */.  int nBytes,
27233 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27234 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  * Length of zSql
27235 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
27236 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  int saveSqlFlag,
27237 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
27238 65 20 74 6f 20 73 61 76 65 20 53 51 4c 20 74 65  e to save SQL te
27239 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  xt into the sqli
2723a 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71  te3_stmt */.  sq
2723b 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
2723c 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
2723d 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
2723e 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2723f 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ent */.  const v
27240 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
27241 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
27242 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
27243 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20  */.){.  /* This 
27244 66 75 6e 63 74 69 6f 6e 20 63 75 72 72 65 6e 74  function current
27245 6c 79 20 77 6f 72 6b 73 20 62 79 20 66 69 72 73  ly works by firs
27246 74 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74  t transforming t
27247 68 65 20 55 54 46 2d 31 36 0a 20 20 2a 2a 20 65  he UTF-16.  ** e
27248 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 20 74 6f  ncoded string to
27249 20 55 54 46 2d 38 2c 20 74 68 65 6e 20 69 6e 76   UTF-8, then inv
2724a 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 70 72  oking sqlite3_pr
2724b 65 70 61 72 65 28 29 2e 20 54 68 65 0a 20 20 2a  epare(). The.  *
2724c 2a 20 74 72 69 63 6b 79 20 62 69 74 20 69 73 20  * tricky bit is 
2724d 66 69 67 75 72 69 6e 67 20 6f 75 74 20 74 68 65  figuring out the
2724e 20 70 6f 69 6e 74 65 72 20 74 6f 20 72 65 74 75   pointer to retu
2724f 72 6e 20 69 6e 20 2a 70 7a 54 61 69 6c 2e 0a 20  rn in *pzTail.. 
27250 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c   */.  char *zSql
27251 38 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  8;.  const char 
27252 2a 7a 54 61 69 6c 38 20 3d 20 30 3b 0a 20 20 69  *zTail8 = 0;.  i
27253 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
27254 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 70  K;..  assert( pp
27255 53 74 6d 74 20 29 3b 0a 20 20 2a 70 70 53 74 6d  Stmt );.  *ppStm
27256 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 21 73 71  t = 0;.  if( !sq
27257 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
27258 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65  Ok(db) ){.    re
27259 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
2725a 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  SE;.  }.  sqlite
2725b 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
2725c 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7a 53 71 6c  ->mutex);.  zSql
2725d 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66 31 36  8 = sqlite3Utf16
2725e 74 6f 38 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42  to8(db, zSql, nB
2725f 79 74 65 73 29 3b 0a 20 20 69 66 28 20 7a 53 71  ytes);.  if( zSq
27260 6c 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  l8 ){.    rc = s
27261 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65  qlite3LockAndPre
27262 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 38 2c 20  pare(db, zSql8, 
27263 2d 31 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  -1, saveSqlFlag,
27264 20 70 70 53 74 6d 74 2c 20 26 7a 54 61 69 6c 38   ppStmt, &zTail8
27265 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 54  );.  }..  if( zT
27266 61 69 6c 38 20 26 26 20 70 7a 54 61 69 6c 20 29  ail8 && pzTail )
27267 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 6c 69  {.    /* If sqli
27268 74 65 33 5f 70 72 65 70 61 72 65 20 72 65 74 75  te3_prepare retu
27269 72 6e 73 20 61 20 74 61 69 6c 20 70 6f 69 6e 74  rns a tail point
2726a 65 72 2c 20 77 65 20 63 61 6c 63 75 6c 61 74 65  er, we calculate
2726b 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 71 75 69   the.    ** equi
2726c 76 61 6c 65 6e 74 20 70 6f 69 6e 74 65 72 20 69  valent pointer i
2726d 6e 74 6f 20 74 68 65 20 55 54 46 2d 31 36 20 73  nto the UTF-16 s
2726e 74 72 69 6e 67 20 62 79 20 63 6f 75 6e 74 69 6e  tring by countin
2726f 67 20 74 68 65 20 75 6e 69 63 6f 64 65 0a 20 20  g the unicode.  
27270 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20    ** characters 
27271 62 65 74 77 65 65 6e 20 7a 53 71 6c 38 20 61 6e  between zSql8 an
27272 64 20 7a 54 61 69 6c 38 2c 20 61 6e 64 20 74 68  d zTail8, and th
27273 65 6e 20 72 65 74 75 72 6e 69 6e 67 20 61 20 70  en returning a p
27274 6f 69 6e 74 65 72 0a 20 20 20 20 2a 2a 20 74 68  ointer.    ** th
27275 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66  e same number of
27276 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f   characters into
27277 20 74 68 65 20 55 54 46 2d 31 36 20 73 74 72 69   the UTF-16 stri
27278 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ng..    */.    i
27279 6e 74 20 63 68 61 72 73 5f 70 61 72 73 65 64 20  nt chars_parsed 
2727a 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61  = sqlite3Utf8Cha
2727b 72 4c 65 6e 28 7a 53 71 6c 38 2c 20 28 69 6e 74  rLen(zSql8, (int
2727c 29 28 7a 54 61 69 6c 38 2d 7a 53 71 6c 38 29 29  )(zTail8-zSql8))
2727d 3b 0a 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20  ;.    *pzTail = 
2727e 28 75 38 20 2a 29 7a 53 71 6c 20 2b 20 73 71 6c  (u8 *)zSql + sql
2727f 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e  ite3Utf16ByteLen
27280 28 7a 53 71 6c 2c 20 63 68 61 72 73 5f 70 61 72  (zSql, chars_par
27281 73 65 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  sed);.  }.  sqli
27282 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
27283 71 6c 38 29 3b 20 0a 20 20 72 63 20 3d 20 73 71  ql8); .  rc = sq
27284 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
27285 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   rc);.  sqlite3_
27286 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
27287 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
27288 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77   rc;.}../*.** Tw
27289 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  o versions of th
2728a 65 20 6f 66 66 69 63 69 61 6c 20 41 50 49 2e 20  e official API. 
2728b 20 4c 65 67 61 63 79 20 61 6e 64 20 6e 65 77 20   Legacy and new 
2728c 75 73 65 2e 20 20 49 6e 20 74 68 65 20 6c 65 67  use.  In the leg
2728d 61 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20  acy.** version, 
2728e 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
2728f 20 74 65 78 74 20 69 73 20 6e 6f 74 20 73 61 76   text is not sav
27290 65 64 20 69 6e 20 74 68 65 20 70 72 65 70 61 72  ed in the prepar
27291 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  ed statement.** 
27292 61 6e 64 20 73 6f 20 69 66 20 61 20 73 63 68 65  and so if a sche
27293 6d 61 20 63 68 61 6e 67 65 20 6f 63 63 75 72 73  ma change occurs
27294 2c 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  , SQLITE_SCHEMA 
27295 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  is returned by.*
27296 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
27297 2e 20 20 49 6e 20 74 68 65 20 6e 65 77 20 76 65  .  In the new ve
27298 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69  rsion, the origi
27299 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20  nal SQL text is 
2729a 72 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20  retained.** and 
2729b 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
2729c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72   automatically r
2729d 65 63 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e 20  ecompiled if an 
2729e 73 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a  schema change.**
2729f 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49   occurs..*/.SQLI
272a0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
272a1 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20  e3_prepare16(.  
272a2 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
272a3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
272a4 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
272a5 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
272a6 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
272a7 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
272a8 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
272a9 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
272aa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
272ab 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
272ac 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
272ad 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
272ae 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
272af 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
272b0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
272b1 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ent */.  const v
272b2 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
272b3 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
272b4 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
272b5 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
272b6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72    rc = sqlite3Pr
272b7 65 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c  epare16(db,zSql,
272b8 6e 42 79 74 65 73 2c 30 2c 70 70 53 74 6d 74 2c  nBytes,0,ppStmt,
272b9 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72  pzTail);.  asser
272ba 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
272bb 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c   || ppStmt==0 ||
272bc 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20   *ppStmt==0 );  
272bd 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32  /* VERIFY: F1302
272be 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63  1 */.  return rc
272bf 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
272c0 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nt sqlite3_prepa
272c1 72 65 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74  re16_v2(.  sqlit
272c2 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
272c3 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
272c4 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63   handle. */ .  c
272c5 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c  onst void *zSql,
272c6 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d           /* UTF-
272c7 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74  8 encoded SQL st
272c8 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e  atement. */.  in
272c9 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20  t nBytes,       
272ca 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
272cb 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
272cc 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
272cd 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
272ce 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69     /* OUT: A poi
272cf 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70  nter to the prep
272d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a  ared statement *
272d1 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
272d2 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a  *pzTail       /*
272d3 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72   OUT: End of par
272d4 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b  sed string */.){
272d5 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20  .  int rc;.  rc 
272d6 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  = sqlite3Prepare
272d7 31 36 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65  16(db,zSql,nByte
272d8 73 2c 31 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69  s,1,ppStmt,pzTai
272d9 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  l);.  assert( rc
272da 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
272db 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53  pStmt==0 || *ppS
272dc 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45  tmt==0 );  /* VE
272dd 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a  RIFY: F13021 */.
272de 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
272df 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
272e0 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
272e1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
272e2 45 6e 64 20 6f 66 20 70 72 65 70 61 72 65 2e 63  End of prepare.c
272e3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
272e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
272e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
272e6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
272e7 42 65 67 69 6e 20 66 69 6c 65 20 73 65 6c 65 63  Begin file selec
272e8 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
272e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
272ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
272eb 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
272ec 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
272ed 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
272ee 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
272ef 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
272f0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
272f1 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
272f2 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
272f3 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
272f4 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
272f5 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
272f6 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
272f7 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
272f8 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
272f9 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
272fa 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
272fb 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
272fc 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
272fd 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
272fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
272ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27301 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27302 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
27303 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
27304 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ode routines tha
27305 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20  t are called by 
27306 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f  the parser.** to
27307 20 68 61 6e 64 6c 65 20 53 45 4c 45 43 54 20 73   handle SELECT s
27308 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c  tatements in SQL
27309 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ite..**.** $Id: 
2730a 73 65 6c 65 63 74 2e 63 2c 76 20 31 2e 35 31 32  select.c,v 1.512
2730b 20 32 30 30 39 2f 30 35 2f 30 33 20 32 30 3a 32   2009/05/03 20:2
2730c 33 3a 35 34 20 64 72 68 20 45 78 70 20 24 0a 2a  3:54 drh Exp $.*
2730d 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  /.../*.** Delete
2730e 20 61 6c 6c 20 74 68 65 20 63 6f 6e 74 65 6e 74   all the content
2730f 20 6f 66 20 61 20 53 65 6c 65 63 74 20 73 74 72   of a Select str
27310 75 63 74 75 72 65 20 62 75 74 20 64 6f 20 6e 6f  ucture but do no
27311 74 20 64 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20  t deallocate.** 
27312 74 68 65 20 73 65 6c 65 63 74 20 73 74 72 75 63  the select struc
27313 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a  ture itself..*/.
27314 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61  static void clea
27315 72 53 65 6c 65 63 74 28 73 71 6c 69 74 65 33 20  rSelect(sqlite3 
27316 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  *db, Select *p){
27317 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
27318 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  stDelete(db, p->
27319 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74  pEList);.  sqlit
2731a 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
2731b 64 62 2c 20 70 2d 3e 70 53 72 63 29 3b 0a 20 20  db, p->pSrc);.  
2731c 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2731d 65 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29  e(db, p->pWhere)
2731e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
2731f 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  istDelete(db, p-
27320 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 73 71  >pGroupBy);.  sq
27321 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
27322 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 3b  db, p->pHaving);
27323 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
27324 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  stDelete(db, p->
27325 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 73 71 6c  pOrderBy);.  sql
27326 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
27327 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 29 3b  (db, p->pPrior);
27328 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
27329 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d  lete(db, p->pLim
2732a 69 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  it);.  sqlite3Ex
2732b 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  prDelete(db, p->
2732c 70 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a  pOffset);.}../*.
2732d 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20  ** Initialize a 
2732e 53 65 6c 65 63 74 44 65 73 74 20 73 74 72 75 63  SelectDest struc
2732f 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
27330 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
27331 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
27332 69 74 28 53 65 6c 65 63 74 44 65 73 74 20 2a 70  it(SelectDest *p
27333 44 65 73 74 2c 20 69 6e 74 20 65 44 65 73 74 2c  Dest, int eDest,
27334 20 69 6e 74 20 69 50 61 72 6d 29 7b 0a 20 20 70   int iParm){.  p
27335 44 65 73 74 2d 3e 65 44 65 73 74 20 3d 20 28 75  Dest->eDest = (u
27336 38 29 65 44 65 73 74 3b 0a 20 20 70 44 65 73 74  8)eDest;.  pDest
27337 2d 3e 69 50 61 72 6d 20 3d 20 69 50 61 72 6d 3b  ->iParm = iParm;
27338 0a 20 20 70 44 65 73 74 2d 3e 61 66 66 69 6e 69  .  pDest->affini
27339 74 79 20 3d 20 30 3b 0a 20 20 70 44 65 73 74 2d  ty = 0;.  pDest-
2733a 3e 69 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 44 65  >iMem = 0;.  pDe
2733b 73 74 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 7d 0a  st->nMem = 0;.}.
2733c 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
2733d 20 61 20 6e 65 77 20 53 65 6c 65 63 74 20 73 74   a new Select st
2733e 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75  ructure and retu
2733f 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
27340 74 68 61 74 0a 2a 2a 20 73 74 72 75 63 74 75 72  that.** structur
27341 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
27342 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c  VATE Select *sql
27343 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 0a 20  ite3SelectNew(. 
27344 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
27345 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
27346 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
27347 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
27348 20 20 20 20 20 2f 2a 20 77 68 69 63 68 20 63 6f       /* which co
27349 6c 75 6d 6e 73 20 74 6f 20 69 6e 63 6c 75 64 65  lumns to include
2734a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a   in the result *
2734b 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
2734c 63 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  c,        /* the
2734d 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20   FROM clause -- 
2734e 77 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20  which tables to 
2734f 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  scan */.  Expr *
27350 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20  pWhere,         
27351 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  /* the WHERE cla
27352 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
27353 74 20 2a 70 47 72 6f 75 70 42 79 2c 20 20 20 2f  t *pGroupBy,   /
27354 2a 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63  * the GROUP BY c
27355 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20  lause */.  Expr 
27356 2a 70 48 61 76 69 6e 67 2c 20 20 20 20 20 20 20  *pHaving,       
27357 20 2f 2a 20 74 68 65 20 48 41 56 49 4e 47 20 63   /* the HAVING c
27358 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lause */.  ExprL
27359 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
2735a 20 2f 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59   /* the ORDER BY
2735b 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
2735c 20 69 73 44 69 73 74 69 6e 63 74 2c 20 20 20 20   isDistinct,    
2735d 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 74 68     /* true if th
2735e 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f  e DISTINCT keywo
2735f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
27360 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c  .  Expr *pLimit,
27361 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49           /* LIMI
27362 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d  T value.  NULL m
27363 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f  eans not used */
27364 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74  .  Expr *pOffset
27365 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53           /* OFFS
27366 45 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20  ET value.  NULL 
27367 6d 65 61 6e 73 20 6e 6f 20 6f 66 66 73 65 74 20  means no offset 
27368 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a  */.){.  Select *
27369 70 4e 65 77 3b 0a 20 20 53 65 6c 65 63 74 20 73  pNew;.  Select s
2736a 74 61 6e 64 69 6e 3b 0a 20 20 73 71 6c 69 74 65  tandin;.  sqlite
2736b 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2736c 64 62 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  db;.  pNew = sql
2736d 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2736e 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  (db, sizeof(*pNe
2736f 77 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  w) );.  assert( 
27370 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
27371 20 7c 7c 20 21 70 4f 66 66 73 65 74 20 7c 7c 20   || !pOffset || 
27372 70 4c 69 6d 69 74 20 29 3b 20 2f 2a 20 4f 46 46  pLimit ); /* OFF
27373 53 45 54 20 69 6d 70 6c 69 65 73 20 4c 49 4d 49  SET implies LIMI
27374 54 20 2a 2f 0a 20 20 69 66 28 20 70 4e 65 77 3d  T */.  if( pNew=
27375 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d  =0 ){.    pNew =
27376 20 26 73 74 61 6e 64 69 6e 3b 0a 20 20 20 20 6d   &standin;.    m
27377 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73  emset(pNew, 0, s
27378 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20  izeof(*pNew));. 
27379 20 7d 0a 20 20 69 66 28 20 70 45 4c 69 73 74 3d   }.  if( pEList=
2737a 3d 30 20 29 7b 0a 20 20 20 20 70 45 4c 69 73 74  =0 ){.    pEList
2737b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2737c 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2737d 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70 72 28   0, sqlite3Expr(
2737e 64 62 2c 54 4b 5f 41 4c 4c 2c 30 2c 30 2c 30 29  db,TK_ALL,0,0,0)
2737f 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 4e 65 77  , 0);.  }.  pNew
27380 2d 3e 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 73  ->pEList = pELis
27381 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20  t;.  pNew->pSrc 
27382 3d 20 70 53 72 63 3b 0a 20 20 70 4e 65 77 2d 3e  = pSrc;.  pNew->
27383 70 57 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b  pWhere = pWhere;
27384 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42  .  pNew->pGroupB
27385 79 20 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20 20  y = pGroupBy;.  
27386 70 4e 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20  pNew->pHaving = 
27387 70 48 61 76 69 6e 67 3b 0a 20 20 70 4e 65 77 2d  pHaving;.  pNew-
27388 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64  >pOrderBy = pOrd
27389 65 72 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 73 65  erBy;.  pNew->se
2738a 6c 46 6c 61 67 73 20 3d 20 69 73 44 69 73 74 69  lFlags = isDisti
2738b 6e 63 74 20 3f 20 53 46 5f 44 69 73 74 69 6e 63  nct ? SF_Distinc
2738c 74 20 3a 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6f  t : 0;.  pNew->o
2738d 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20  p = TK_SELECT;. 
2738e 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20   pNew->pLimit = 
2738f 70 4c 69 6d 69 74 3b 0a 20 20 70 4e 65 77 2d 3e  pLimit;.  pNew->
27390 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65  pOffset = pOffse
27391 74 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  t;.  pNew->addrO
27392 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b  penEphm[0] = -1;
27393 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65  .  pNew->addrOpe
27394 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20  nEphm[1] = -1;. 
27395 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45   pNew->addrOpenE
27396 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 69  phm[2] = -1;.  i
27397 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
27398 6c 65 64 20 29 20 7b 0a 20 20 20 20 63 6c 65 61  led ) {.    clea
27399 72 53 65 6c 65 63 74 28 64 62 2c 20 70 4e 65 77  rSelect(db, pNew
2739a 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 21  );.    if( pNew!
2739b 3d 26 73 74 61 6e 64 69 6e 20 29 20 73 71 6c 69  =&standin ) sqli
2739c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4e  te3DbFree(db, pN
2739d 65 77 29 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20  ew);.    pNew = 
2739e 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
2739f 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  pNew;.}../*.** D
273a0 65 6c 65 74 65 20 74 68 65 20 67 69 76 65 6e 20  elete the given 
273a1 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65  Select structure
273a2 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20   and all of its 
273a3 73 75 62 73 74 72 75 63 74 75 72 65 73 2e 0a 2a  substructures..*
273a4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
273a5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
273a6 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  ectDelete(sqlite
273a7 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70  3 *db, Select *p
273a8 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
273a9 20 20 63 6c 65 61 72 53 65 6c 65 63 74 28 64 62    clearSelect(db
273aa 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , p);.    sqlite
273ab 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
273ac 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76    }.}../*.** Giv
273ad 65 6e 20 31 20 74 6f 20 33 20 69 64 65 6e 74 69  en 1 to 3 identi
273ae 66 69 65 72 73 20 70 72 65 63 65 65 64 69 6e 67  fiers preceeding
273af 20 74 68 65 20 4a 4f 49 4e 20 6b 65 79 77 6f 72   the JOIN keywor
273b0 64 2c 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  d, determine the
273b1 0a 2a 2a 20 74 79 70 65 20 6f 66 20 6a 6f 69 6e  .** type of join
273b2 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74  .  Return an int
273b3 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68  eger constant th
273b4 61 74 20 65 78 70 72 65 73 73 65 73 20 74 68 61  at expresses tha
273b5 74 20 74 79 70 65 0a 2a 2a 20 69 6e 20 74 65 72  t type.** in ter
273b6 6d 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ms of the follow
273b7 69 6e 67 20 62 69 74 20 76 61 6c 75 65 73 3a 0a  ing bit values:.
273b8 2a 2a 0a 2a 2a 20 20 20 20 20 4a 54 5f 49 4e 4e  **.**     JT_INN
273b9 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 43 52 4f  ER.**     JT_CRO
273ba 53 53 0a 2a 2a 20 20 20 20 20 4a 54 5f 4f 55 54  SS.**     JT_OUT
273bb 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 4e 41 54  ER.**     JT_NAT
273bc 55 52 41 4c 0a 2a 2a 20 20 20 20 20 4a 54 5f 4c  URAL.**     JT_L
273bd 45 46 54 0a 2a 2a 20 20 20 20 20 4a 54 5f 52 49  EFT.**     JT_RI
273be 47 48 54 0a 2a 2a 0a 2a 2a 20 41 20 66 75 6c 6c  GHT.**.** A full
273bf 20 6f 75 74 65 72 20 6a 6f 69 6e 20 69 73 20 74   outer join is t
273c0 68 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f  he combination o
273c1 66 20 4a 54 5f 4c 45 46 54 20 61 6e 64 20 4a 54  f JT_LEFT and JT
273c2 5f 52 49 47 48 54 2e 0a 2a 2a 0a 2a 2a 20 49 66  _RIGHT..**.** If
273c3 20 61 6e 20 69 6c 6c 65 67 61 6c 20 6f 72 20 75   an illegal or u
273c4 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20  nsupported join 
273c5 74 79 70 65 20 69 73 20 73 65 65 6e 2c 20 74 68  type is seen, th
273c6 65 6e 20 73 74 69 6c 6c 20 72 65 74 75 72 6e 0a  en still return.
273c7 2a 2a 20 61 20 6a 6f 69 6e 20 74 79 70 65 2c 20  ** a join type, 
273c8 62 75 74 20 70 75 74 20 61 6e 20 65 72 72 6f 72  but put an error
273c9 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 20 73   in the pParse s
273ca 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c  tructure..*/.SQL
273cb 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
273cc 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28  sqlite3JoinType(
273cd 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
273ce 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f 6b 65 6e 20  oken *pA, Token 
273cf 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a 70 43 29 7b  *pB, Token *pC){
273d0 0a 20 20 69 6e 74 20 6a 6f 69 6e 74 79 70 65 20  .  int jointype 
273d1 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e 20 2a 61 70  = 0;.  Token *ap
273d2 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f 6b 65 6e 20  All[3];.  Token 
273d3 2a 70 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  *p;.  static con
273d4 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20  st struct {.    
273d5 63 6f 6e 73 74 20 63 68 61 72 20 7a 4b 65 79 77  const char zKeyw
273d6 6f 72 64 5b 38 5d 3b 0a 20 20 20 20 75 38 20 6e  ord[8];.    u8 n
273d7 43 68 61 72 3b 0a 20 20 20 20 75 38 20 63 6f 64  Char;.    u8 cod
273d8 65 3b 0a 20 20 7d 20 6b 65 79 77 6f 72 64 73 5b  e;.  } keywords[
273d9 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 6e 61 74  ] = {.    { "nat
273da 75 72 61 6c 22 2c 20 37 2c 20 4a 54 5f 4e 41 54  ural", 7, JT_NAT
273db 55 52 41 4c 20 7d 2c 0a 20 20 20 20 7b 20 22 6c  URAL },.    { "l
273dc 65 66 74 22 2c 20 20 20 20 34 2c 20 4a 54 5f 4c  eft",    4, JT_L
273dd 45 46 54 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a  EFT|JT_OUTER },.
273de 20 20 20 20 7b 20 22 72 69 67 68 74 22 2c 20 20      { "right",  
273df 20 35 2c 20 4a 54 5f 52 49 47 48 54 7c 4a 54 5f   5, JT_RIGHT|JT_
273e0 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20 7b 20 22  OUTER },.    { "
273e1 66 75 6c 6c 22 2c 20 20 20 20 34 2c 20 4a 54 5f  full",    4, JT_
273e2 4c 45 46 54 7c 4a 54 5f 52 49 47 48 54 7c 4a 54  LEFT|JT_RIGHT|JT
273e3 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20 7b 20  _OUTER },.    { 
273e4 22 6f 75 74 65 72 22 2c 20 20 20 35 2c 20 4a 54  "outer",   5, JT
273e5 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20 7b 20  _OUTER },.    { 
273e6 22 69 6e 6e 65 72 22 2c 20 20 20 35 2c 20 4a 54  "inner",   5, JT
273e7 5f 49 4e 4e 45 52 20 7d 2c 0a 20 20 20 20 7b 20  _INNER },.    { 
273e8 22 63 72 6f 73 73 22 2c 20 20 20 35 2c 20 4a 54  "cross",   5, JT
273e9 5f 49 4e 4e 45 52 7c 4a 54 5f 43 52 4f 53 53 20  _INNER|JT_CROSS 
273ea 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 2c  },.  };.  int i,
273eb 20 6a 3b 0a 20 20 61 70 41 6c 6c 5b 30 5d 20 3d   j;.  apAll[0] =
273ec 20 70 41 3b 0a 20 20 61 70 41 6c 6c 5b 31 5d 20   pA;.  apAll[1] 
273ed 3d 20 70 42 3b 0a 20 20 61 70 41 6c 6c 5b 32 5d  = pB;.  apAll[2]
273ee 20 3d 20 70 43 3b 0a 20 20 66 6f 72 28 69 3d 30   = pC;.  for(i=0
273ef 3b 20 69 3c 33 20 26 26 20 61 70 41 6c 6c 5b 69  ; i<3 && apAll[i
273f0 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d  ]; i++){.    p =
273f1 20 61 70 41 6c 6c 5b 69 5d 3b 0a 20 20 20 20 66   apAll[i];.    f
273f2 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72 61 79 53  or(j=0; j<ArrayS
273f3 69 7a 65 28 6b 65 79 77 6f 72 64 73 29 3b 20 6a  ize(keywords); j
273f4 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
273f5 2d 3e 6e 3d 3d 6b 65 79 77 6f 72 64 73 5b 6a 5d  ->n==keywords[j]
273f6 2e 6e 43 68 61 72 20 0a 20 20 20 20 20 20 20 20  .nChar .        
273f7 20 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e    && sqlite3StrN
273f8 49 43 6d 70 28 28 63 68 61 72 2a 29 70 2d 3e 7a  ICmp((char*)p->z
273f9 2c 20 6b 65 79 77 6f 72 64 73 5b 6a 5d 2e 7a 4b  , keywords[j].zK
273fa 65 79 77 6f 72 64 2c 20 70 2d 3e 6e 29 3d 3d 30  eyword, p->n)==0
273fb 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 6f 69 6e   ){.        join
273fc 74 79 70 65 20 7c 3d 20 6b 65 79 77 6f 72 64 73  type |= keywords
273fd 5b 6a 5d 2e 63 6f 64 65 3b 0a 20 20 20 20 20 20  [j].code;.      
273fe 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
273ff 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a  .    }.    if( j
27400 3e 3d 41 72 72 61 79 53 69 7a 65 28 6b 65 79 77  >=ArraySize(keyw
27401 6f 72 64 73 29 20 29 7b 0a 20 20 20 20 20 20 6a  ords) ){.      j
27402 6f 69 6e 74 79 70 65 20 7c 3d 20 4a 54 5f 45 52  ointype |= JT_ER
27403 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ROR;.      break
27404 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
27405 28 0a 20 20 20 20 20 28 6a 6f 69 6e 74 79 70 65  (.     (jointype
27406 20 26 20 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f   & (JT_INNER|JT_
27407 4f 55 54 45 52 29 29 3d 3d 28 4a 54 5f 49 4e 4e  OUTER))==(JT_INN
27408 45 52 7c 4a 54 5f 4f 55 54 45 52 29 20 7c 7c 0a  ER|JT_OUTER) ||.
27409 20 20 20 20 20 28 6a 6f 69 6e 74 79 70 65 20 26       (jointype &
2740a 20 4a 54 5f 45 52 52 4f 52 29 21 3d 30 0a 20 20   JT_ERROR)!=0.  
2740b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
2740c 72 20 2a 7a 53 70 20 3d 20 22 20 22 3b 0a 20 20  r *zSp = " ";.  
2740d 20 20 61 73 73 65 72 74 28 20 70 42 21 3d 30 20    assert( pB!=0 
2740e 29 3b 0a 20 20 20 20 69 66 28 20 70 43 3d 3d 30  );.    if( pC==0
2740f 20 29 7b 20 7a 53 70 2b 2b 3b 20 7d 0a 20 20 20   ){ zSp++; }.   
27410 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
27411 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77  (pParse, "unknow
27412 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64  n or unsupported
27413 20 6a 6f 69 6e 20 74 79 70 65 3a 20 22 0a 20 20   join type: ".  
27414 20 20 20 20 20 22 25 54 20 25 54 25 73 25 54 22       "%T %T%s%T"
27415 2c 20 70 41 2c 20 70 42 2c 20 7a 53 70 2c 20 70  , pA, pB, zSp, p
27416 43 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65  C);.    jointype
27417 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d   = JT_INNER;.  }
27418 65 6c 73 65 20 69 66 28 20 6a 6f 69 6e 74 79 70  else if( jointyp
27419 65 20 26 20 4a 54 5f 52 49 47 48 54 20 29 7b 0a  e & JT_RIGHT ){.
2741a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2741b 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
2741c 20 20 20 22 52 49 47 48 54 20 61 6e 64 20 46 55     "RIGHT and FU
2741d 4c 4c 20 4f 55 54 45 52 20 4a 4f 49 4e 73 20 61  LL OUTER JOINs a
2741e 72 65 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  re not currently
2741f 20 73 75 70 70 6f 72 74 65 64 22 29 3b 0a 20 20   supported");.  
27420 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54 5f    jointype = JT_
27421 49 4e 4e 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74  INNER;.  }.  ret
27422 75 72 6e 20 6a 6f 69 6e 74 79 70 65 3b 0a 7d 0a  urn jointype;.}.
27423 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
27424 65 20 69 6e 64 65 78 20 6f 66 20 61 20 63 6f 6c  e index of a col
27425 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e 20  umn in a table. 
27426 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20 74 68   Return -1 if th
27427 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 6e  e column.** is n
27428 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ot contained in 
27429 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  the table..*/.st
2742a 61 74 69 63 20 69 6e 74 20 63 6f 6c 75 6d 6e 49  atic int columnI
2742b 6e 64 65 78 28 54 61 62 6c 65 20 2a 70 54 61 62  ndex(Table *pTab
2742c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43  , const char *zC
2742d 6f 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ol){.  int i;.  
2742e 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
2742f 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
27430 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
27431 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  Cmp(pTab->aCol[i
27432 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d  ].zName, zCol)==
27433 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a 20 20  0 ) return i;.  
27434 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d  }.  return -1;.}
27435 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
27436 76 61 6c 75 65 20 6f 66 20 61 20 74 6f 6b 65 6e  value of a token
27437 20 74 6f 20 61 20 27 5c 30 30 30 27 2d 74 65 72   to a '\000'-ter
27438 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a  minated string..
27439 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
2743a 65 74 54 6f 6b 65 6e 28 54 6f 6b 65 6e 20 2a 70  etToken(Token *p
2743b 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  , const char *z)
2743c 7b 0a 20 20 70 2d 3e 7a 20 3d 20 28 75 38 2a 29  {.  p->z = (u8*)
2743d 7a 3b 0a 20 20 70 2d 3e 6e 20 3d 20 7a 20 3f 20  z;.  p->n = z ? 
2743e 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2743f 7a 29 20 3a 20 30 3b 0a 20 20 70 2d 3e 64 79 6e  z) : 0;.  p->dyn
27440 20 3d 20 30 3b 0a 20 20 70 2d 3e 71 75 6f 74 65   = 0;.  p->quote
27441 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d = 0;.}../*.** 
27442 43 72 65 61 74 65 20 61 6e 20 65 78 70 72 65 73  Create an expres
27443 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 6e  sion node for an
27444 20 69 64 65 6e 74 69 66 69 65 72 20 77 69 74 68   identifier with
27445 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 7a 4e 61   the name of zNa
27446 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  me.*/.SQLITE_PRI
27447 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
27448 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28 50  e3CreateIdExpr(P
27449 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
2744a 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29  nst char *zName)
2744b 7b 0a 20 20 54 6f 6b 65 6e 20 64 75 6d 6d 79 3b  {.  Token dummy;
2744c 0a 20 20 73 65 74 54 6f 6b 65 6e 28 26 64 75 6d  .  setToken(&dum
2744d 6d 79 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65  my, zName);.  re
2744e 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70  turn sqlite3PExp
2744f 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
27450 20 30 2c 20 30 2c 20 26 64 75 6d 6d 79 29 3b 0a   0, 0, &dummy);.
27451 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 74  }../*.** Add a t
27452 65 72 6d 20 74 6f 20 74 68 65 20 57 48 45 52 45  erm to the WHERE
27453 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 2a   expression in *
27454 70 70 45 78 70 72 20 74 68 61 74 20 72 65 71 75  ppExpr that requ
27455 69 72 65 73 20 74 68 65 0a 2a 2a 20 7a 43 6f 6c  ires the.** zCol
27456 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 65 71   column to be eq
27457 75 61 6c 20 69 6e 20 74 68 65 20 74 77 6f 20 74  ual in the two t
27458 61 62 6c 65 73 20 70 54 61 62 31 20 61 6e 64 20  ables pTab1 and 
27459 70 54 61 62 32 2e 0a 2a 2f 0a 73 74 61 74 69 63  pTab2..*/.static
2745a 20 76 6f 69 64 20 61 64 64 57 68 65 72 65 54 65   void addWhereTe
2745b 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  rm(.  Parse *pPa
2745c 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  rse,           /
2745d 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
2745e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
2745f 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20  r *zCol,        
27460 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
27461 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  olumn */.  const
27462 20 54 61 62 6c 65 20 2a 70 54 61 62 31 2c 20 20   Table *pTab1,  
27463 20 20 20 20 2f 2a 20 46 69 72 73 74 20 74 61 62      /* First tab
27464 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
27465 61 72 20 2a 7a 41 6c 69 61 73 31 2c 20 20 20 20  ar *zAlias1,    
27466 20 2f 2a 20 41 6c 69 61 73 20 66 6f 72 20 66 69   /* Alias for fi
27467 72 73 74 20 74 61 62 6c 65 2e 20 20 4d 61 79 20  rst table.  May 
27468 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e  be NULL */.  con
27469 73 74 20 54 61 62 6c 65 20 2a 70 54 61 62 32 2c  st Table *pTab2,
2746a 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20        /* Second 
2746b 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  table */.  const
2746c 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 32 2c 20   char *zAlias2, 
2746d 20 20 20 20 2f 2a 20 41 6c 69 61 73 20 66 6f 72      /* Alias for
2746e 20 73 65 63 6f 6e 64 20 74 61 62 6c 65 2e 20 20   second table.  
2746f 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
27470 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69 6e 54   int iRightJoinT
27471 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 56 44 42  able,     /* VDB
27472 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65  E cursor for the
27473 20 72 69 67 68 74 20 74 61 62 6c 65 20 2a 2f 0a   right table */.
27474 20 20 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c    Expr **ppExpr,
27475 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
27476 64 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 74  d the equality t
27477 65 72 6d 20 74 6f 20 74 68 69 73 20 65 78 70 72  erm to this expr
27478 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ession */.  int 
27479 69 73 4f 75 74 65 72 4a 6f 69 6e 20 20 20 20 20  isOuterJoin     
2747a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2747b 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20  dealing with an 
2747c 4f 55 54 45 52 20 6a 6f 69 6e 20 2a 2f 0a 29 7b  OUTER join */.){
2747d 0a 20 20 45 78 70 72 20 2a 70 45 31 61 2c 20 2a  .  Expr *pE1a, *
2747e 70 45 31 62 2c 20 2a 70 45 31 63 3b 0a 20 20 45  pE1b, *pE1c;.  E
2747f 78 70 72 20 2a 70 45 32 61 2c 20 2a 70 45 32 62  xpr *pE2a, *pE2b
27480 2c 20 2a 70 45 32 63 3b 0a 20 20 45 78 70 72 20  , *pE2c;.  Expr 
27481 2a 70 45 3b 0a 0a 20 20 70 45 31 61 20 3d 20 73  *pE;..  pE1a = s
27482 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78  qlite3CreateIdEx
27483 70 72 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 29  pr(pParse, zCol)
27484 3b 0a 20 20 70 45 32 61 20 3d 20 73 71 6c 69 74  ;.  pE2a = sqlit
27485 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28 70  e3CreateIdExpr(p
27486 50 61 72 73 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20  Parse, zCol);.  
27487 69 66 28 20 7a 41 6c 69 61 73 31 3d 3d 30 20 29  if( zAlias1==0 )
27488 7b 0a 20 20 20 20 7a 41 6c 69 61 73 31 20 3d 20  {.    zAlias1 = 
27489 70 54 61 62 31 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pTab1->zName;.  
2748a 7d 0a 20 20 70 45 31 62 20 3d 20 73 71 6c 69 74  }.  pE1b = sqlit
2748b 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28 70  e3CreateIdExpr(p
2748c 50 61 72 73 65 2c 20 7a 41 6c 69 61 73 31 29 3b  Parse, zAlias1);
2748d 0a 20 20 69 66 28 20 7a 41 6c 69 61 73 32 3d 3d  .  if( zAlias2==
2748e 30 20 29 7b 0a 20 20 20 20 7a 41 6c 69 61 73 32  0 ){.    zAlias2
2748f 20 3d 20 70 54 61 62 32 2d 3e 7a 4e 61 6d 65 3b   = pTab2->zName;
27490 0a 20 20 7d 0a 20 20 70 45 32 62 20 3d 20 73 71  .  }.  pE2b = sq
27491 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70  lite3CreateIdExp
27492 72 28 70 50 61 72 73 65 2c 20 7a 41 6c 69 61 73  r(pParse, zAlias
27493 32 29 3b 0a 20 20 70 45 31 63 20 3d 20 73 71 6c  2);.  pE1c = sql
27494 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
27495 2c 20 54 4b 5f 44 4f 54 2c 20 70 45 31 62 2c 20  , TK_DOT, pE1b, 
27496 70 45 31 61 2c 20 30 29 3b 0a 20 20 70 45 32 63  pE1a, 0);.  pE2c
27497 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
27498 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
27499 70 45 32 62 2c 20 70 45 32 61 2c 20 30 29 3b 0a  pE2b, pE2a, 0);.
2749a 20 20 70 45 20 3d 20 73 71 6c 69 74 65 33 50 45    pE = sqlite3PE
2749b 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45  xpr(pParse, TK_E
2749c 51 2c 20 70 45 31 63 2c 20 70 45 32 63 2c 20 30  Q, pE1c, pE2c, 0
2749d 29 3b 0a 20 20 69 66 28 20 70 45 20 26 26 20 69  );.  if( pE && i
2749e 73 4f 75 74 65 72 4a 6f 69 6e 20 29 7b 0a 20 20  sOuterJoin ){.  
2749f 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
274a0 79 28 70 45 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69  y(pE, EP_FromJoi
274a1 6e 29 3b 0a 20 20 20 20 70 45 2d 3e 69 52 69 67  n);.    pE->iRig
274a2 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20 69 52  htJoinTable = iR
274a3 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a 20  ightJoinTable;. 
274a4 20 7d 0a 20 20 2a 70 70 45 78 70 72 20 3d 20 73   }.  *ppExpr = s
274a5 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 70 50  qlite3ExprAnd(pP
274a6 61 72 73 65 2d 3e 64 62 2c 2a 70 70 45 78 70 72  arse->db,*ppExpr
274a7 2c 20 70 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  , pE);.}../*.** 
274a8 53 65 74 20 74 68 65 20 45 50 5f 46 72 6f 6d 4a  Set the EP_FromJ
274a9 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 6f 6e 20  oin property on 
274aa 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65  all terms of the
274ab 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f   given expressio
274ac 6e 2e 0a 2a 2a 20 41 6e 64 20 73 65 74 20 74 68  n..** And set th
274ad 65 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69  e Expr.iRightJoi
274ae 6e 54 61 62 6c 65 20 74 6f 20 69 54 61 62 6c 65  nTable to iTable
274af 20 66 6f 72 20 65 76 65 72 79 20 74 65 72 6d 20   for every term 
274b0 69 6e 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73  in the.** expres
274b1 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sion..**.** The 
274b2 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70  EP_FromJoin prop
274b3 65 72 74 79 20 69 73 20 75 73 65 64 20 6f 6e 20  erty is used on 
274b4 74 65 72 6d 73 20 6f 66 20 61 6e 20 65 78 70 72  terms of an expr
274b5 65 73 73 69 6f 6e 20 74 6f 20 74 65 6c 6c 0a 2a  ession to tell.*
274b6 2a 20 74 68 65 20 4c 45 46 54 20 4f 55 54 45 52  * the LEFT OUTER
274b7 20 4a 4f 49 4e 20 70 72 6f 63 65 73 73 69 6e 67   JOIN processing
274b8 20 6c 6f 67 69 63 20 74 68 61 74 20 74 68 69 73   logic that this
274b9 20 74 65 72 6d 20 69 73 20 70 61 72 74 20 6f 66   term is part of
274ba 20 74 68 65 0a 2a 2a 20 6a 6f 69 6e 20 72 65 73   the.** join res
274bb 74 72 69 63 74 69 6f 6e 20 73 70 65 63 69 66 69  triction specifi
274bc 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20  ed in the ON or 
274bd 55 53 49 4e 47 20 63 6c 61 75 73 65 20 61 6e 64  USING clause and
274be 20 6e 6f 74 20 61 20 70 61 72 74 0a 2a 2a 20 6f   not a part.** o
274bf 66 20 74 68 65 20 6d 6f 72 65 20 67 65 6e 65 72  f the more gener
274c0 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  al WHERE clause.
274c1 20 20 54 68 65 73 65 20 74 65 72 6d 73 20 61 72    These terms ar
274c2 65 20 6d 6f 76 65 64 20 6f 76 65 72 20 74 6f 20  e moved over to 
274c3 74 68 65 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61  the.** WHERE cla
274c4 75 73 65 20 64 75 72 69 6e 67 20 6a 6f 69 6e 20  use during join 
274c5 70 72 6f 63 65 73 73 69 6e 67 20 62 75 74 20 77  processing but w
274c6 65 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62  e need to rememb
274c7 65 72 20 74 68 61 74 20 74 68 65 79 0a 2a 2a 20  er that they.** 
274c8 6f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68  originated in th
274c9 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  e ON or USING cl
274ca 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ause..**.** The 
274cb 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54  Expr.iRightJoinT
274cc 61 62 6c 65 20 74 65 6c 6c 73 20 74 68 65 20 57  able tells the W
274cd 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63  HERE clause proc
274ce 65 73 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a  essing that the.
274cf 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65  ** expression de
274d0 70 65 6e 64 73 20 6f 6e 20 74 61 62 6c 65 20 69  pends on table i
274d1 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 65  RightJoinTable e
274d2 76 65 6e 20 69 66 20 74 68 61 74 20 74 61 62 6c  ven if that tabl
274d3 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 78 70 6c  e is not.** expl
274d4 69 63 69 74 6c 79 20 6d 65 6e 74 69 6f 6e 65 64  icitly mentioned
274d5 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   in the expressi
274d6 6f 6e 2e 20 20 54 68 61 74 20 69 6e 66 6f 72 6d  on.  That inform
274d7 61 74 69 6f 6e 20 69 73 20 6e 65 65 64 65 64 0a  ation is needed.
274d8 2a 2a 20 66 6f 72 20 63 61 73 65 73 20 6c 69 6b  ** for cases lik
274d9 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
274da 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
274db 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f  1 LEFT JOIN t2 O
274dc 4e 20 74 31 2e 61 3d 74 32 2e 62 20 41 4e 44 20  N t1.a=t2.b AND 
274dd 74 31 2e 78 3d 35 0a 2a 2a 0a 2a 2a 20 54 68 65  t1.x=5.**.** The
274de 20 77 68 65 72 65 20 63 6c 61 75 73 65 20 6e 65   where clause ne
274df 65 64 73 20 74 6f 20 64 65 66 65 72 20 74 68 65  eds to defer the
274e0 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74 68 65   handling of the
274e1 20 74 31 2e 78 3d 35 0a 2a 2a 20 74 65 72 6d 20   t1.x=5.** term 
274e2 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 20  until after the 
274e3 74 32 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 6a  t2 loop of the j
274e4 6f 69 6e 2e 20 20 49 6e 20 74 68 61 74 20 77 61  oin.  In that wa
274e5 79 2c 20 61 0a 2a 2a 20 4e 55 4c 4c 20 74 32 20  y, a.** NULL t2 
274e6 72 6f 77 20 77 69 6c 6c 20 62 65 20 69 6e 73 65  row will be inse
274e7 72 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 31  rted whenever t1
274e8 2e 78 21 3d 35 2e 20 20 49 66 20 77 65 20 64 6f  .x!=5.  If we do
274e9 20 6e 6f 74 0a 2a 2a 20 64 65 66 65 72 20 74 68   not.** defer th
274ea 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74 31  e handling of t1
274eb 2e 78 3d 35 2c 20 69 74 20 77 69 6c 6c 20 62 65  .x=5, it will be
274ec 20 70 72 6f 63 65 73 73 65 64 20 69 6d 6d 65 64   processed immed
274ed 69 61 74 65 6c 79 0a 2a 2a 20 61 66 74 65 72 20  iately.** after 
274ee 74 68 65 20 74 31 20 6c 6f 6f 70 20 61 6e 64 20  the t1 loop and 
274ef 72 6f 77 73 20 77 69 74 68 20 74 31 2e 78 21 3d  rows with t1.x!=
274f0 35 20 77 69 6c 6c 20 6e 65 76 65 72 20 61 70 70  5 will never app
274f1 65 61 72 20 69 6e 0a 2a 2a 20 74 68 65 20 6f 75  ear in.** the ou
274f2 74 70 75 74 2c 20 77 68 69 63 68 20 69 73 20 69  tput, which is i
274f3 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 73 74 61  ncorrect..*/.sta
274f4 74 69 63 20 76 6f 69 64 20 73 65 74 4a 6f 69 6e  tic void setJoin
274f5 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e  Expr(Expr *p, in
274f6 74 20 69 54 61 62 6c 65 29 7b 0a 20 20 77 68 69  t iTable){.  whi
274f7 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 45 78 70  le( p ){.    Exp
274f8 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20  rSetProperty(p, 
274f9 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20  EP_FromJoin);.  
274fa 20 20 70 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54    p->iRightJoinT
274fb 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20  able = iTable;. 
274fc 20 20 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 70     setJoinExpr(p
274fd 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 29  ->pLeft, iTable)
274fe 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69  ;.    p = p->pRi
274ff 67 68 74 3b 0a 20 20 7d 20 0a 7d 0a 0a 2f 2a 0a  ght;.  } .}../*.
27500 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
27501 70 72 6f 63 65 73 73 65 73 20 74 68 65 20 6a 6f  processes the jo
27502 69 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  in information f
27503 6f 72 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  or a SELECT stat
27504 65 6d 65 6e 74 2e 0a 2a 2a 20 4f 4e 20 61 6e 64  ement..** ON and
27505 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61   USING clauses a
27506 72 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  re converted int
27507 6f 20 65 78 74 72 61 20 74 65 72 6d 73 20 6f 66  o extra terms of
27508 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
27509 65 2e 0a 2a 2a 20 4e 41 54 55 52 41 4c 20 6a 6f  e..** NATURAL jo
2750a 69 6e 73 20 61 6c 73 6f 20 63 72 65 61 74 65 20  ins also create 
2750b 65 78 74 72 61 20 57 48 45 52 45 20 63 6c 61 75  extra WHERE clau
2750c 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20  se terms..**.** 
2750d 54 68 65 20 74 65 72 6d 73 20 6f 66 20 61 20 46  The terms of a F
2750e 52 4f 4d 20 63 6c 61 75 73 65 20 61 72 65 20 63  ROM clause are c
2750f 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
27510 53 65 6c 65 63 74 2e 70 53 72 63 20 73 74 72 75  Select.pSrc stru
27511 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 6c 65  cture..** The le
27512 66 74 20 6d 6f 73 74 20 74 61 62 6c 65 20 69 73  ft most table is
27513 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
27514 20 69 6e 20 53 65 6c 65 63 74 2e 70 53 72 63 2e   in Select.pSrc.
27515 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74    The right-most
27516 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 74 68 65  .** table is the
27517 20 6c 61 73 74 20 65 6e 74 72 79 2e 20 20 54 68   last entry.  Th
27518 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20  e join operator 
27519 69 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 65  is held in the e
2751a 6e 74 72 79 20 74 6f 0a 2a 2a 20 74 68 65 20 6c  ntry to.** the l
2751b 65 66 74 2e 20 20 54 68 75 73 20 65 6e 74 72 79  eft.  Thus entry
2751c 20 30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20   0 contains the 
2751d 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 66 6f  join operator fo
2751e 72 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65  r the join betwe
2751f 65 6e 0a 2a 2a 20 65 6e 74 72 69 65 73 20 30 20  en.** entries 0 
27520 61 6e 64 20 31 2e 20 20 41 6e 79 20 4f 4e 20 6f  and 1.  Any ON o
27521 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20  r USING clauses 
27522 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
27523 74 68 65 20 6a 6f 69 6e 20 61 72 65 0a 2a 2a 20  the join are.** 
27524 61 6c 73 6f 20 61 74 74 61 63 68 65 64 20 74 6f  also attached to
27525 20 74 68 65 20 6c 65 66 74 20 65 6e 74 72 79 2e   the left entry.
27526 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
27527 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
27528 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
27529 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f   encountered..*/
2752a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
2752b 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28 50 61  teProcessJoin(Pa
2752c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c  rse *pParse, Sel
2752d 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69  ect *p){.  SrcLi
2752e 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20  st *pSrc;       
2752f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
27530 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  l tables in the 
27531 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
27532 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
27533 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27534 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
27535 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  s */.  struct Sr
27536 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65 66  cList_item *pLef
27537 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 74  t;     /* Left t
27538 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e 65  able being joine
27539 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  d */.  struct Sr
2753a 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 52 69 67  cList_item *pRig
2753b 68 74 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20  ht;    /* Right 
2753c 74 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e  table being join
2753d 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20  ed */..  pSrc = 
2753e 70 2d 3e 70 53 72 63 3b 0a 20 20 70 4c 65 66 74  p->pSrc;.  pLeft
2753f 20 3d 20 26 70 53 72 63 2d 3e 61 5b 30 5d 3b 0a   = &pSrc->a[0];.
27540 20 20 70 52 69 67 68 74 20 3d 20 26 70 4c 65 66    pRight = &pLef
27541 74 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  t[1];.  for(i=0;
27542 20 69 3c 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b   i<pSrc->nSrc-1;
27543 20 69 2b 2b 2c 20 70 52 69 67 68 74 2b 2b 2c 20   i++, pRight++, 
27544 70 4c 65 66 74 2b 2b 29 7b 0a 20 20 20 20 54 61  pLeft++){.    Ta
27545 62 6c 65 20 2a 70 4c 65 66 74 54 61 62 20 3d 20  ble *pLeftTab = 
27546 70 4c 65 66 74 2d 3e 70 54 61 62 3b 0a 20 20 20  pLeft->pTab;.   
27547 20 54 61 62 6c 65 20 2a 70 52 69 67 68 74 54 61   Table *pRightTa
27548 62 20 3d 20 70 52 69 67 68 74 2d 3e 70 54 61 62  b = pRight->pTab
27549 3b 0a 20 20 20 20 69 6e 74 20 69 73 4f 75 74 65  ;.    int isOute
2754a 72 3b 0a 0a 20 20 20 20 69 66 28 20 4e 45 56 45  r;..    if( NEVE
2754b 52 28 70 4c 65 66 74 54 61 62 3d 3d 30 20 7c 7c  R(pLeftTab==0 ||
2754c 20 70 52 69 67 68 74 54 61 62 3d 3d 30 29 20 29   pRightTab==0) )
2754d 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
2754e 73 4f 75 74 65 72 20 3d 20 28 70 52 69 67 68 74  sOuter = (pRight
2754f 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ->jointype & JT_
27550 4f 55 54 45 52 29 21 3d 30 3b 0a 0a 20 20 20 20  OUTER)!=0;..    
27551 2f 2a 20 57 68 65 6e 20 74 68 65 20 4e 41 54 55  /* When the NATU
27552 52 41 4c 20 6b 65 79 77 6f 72 64 20 69 73 20 70  RAL keyword is p
27553 72 65 73 65 6e 74 2c 20 61 64 64 20 57 48 45 52  resent, add WHER
27554 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66  E clause terms f
27555 6f 72 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20  or.    ** every 
27556 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20  column that the 
27557 74 77 6f 20 74 61 62 6c 65 73 20 68 61 76 65 20  two tables have 
27558 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20 20 20 20 2a  in common..    *
27559 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74  /.    if( pRight
2755a 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ->jointype & JT_
2755b 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20  NATURAL ){.     
2755c 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e   if( pRight->pOn
2755d 20 7c 7c 20 70 52 69 67 68 74 2d 3e 70 55 73 69   || pRight->pUsi
2755e 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ng ){.        sq
2755f 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
27560 61 72 73 65 2c 20 22 61 20 4e 41 54 55 52 41 4c  arse, "a NATURAL
27561 20 6a 6f 69 6e 20 6d 61 79 20 6e 6f 74 20 68 61   join may not ha
27562 76 65 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  ve ".           
27563 22 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  "an ON or USING 
27564 63 6c 61 75 73 65 22 2c 20 30 29 3b 0a 20 20 20  clause", 0);.   
27565 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
27566 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
27567 28 6a 3d 30 3b 20 6a 3c 70 4c 65 66 74 54 61 62  (j=0; j<pLeftTab
27568 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
27569 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
2756a 65 20 3d 20 70 4c 65 66 74 54 61 62 2d 3e 61 43  e = pLeftTab->aC
2756b 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  ol[j].zName;.   
2756c 20 20 20 20 20 69 66 28 20 63 6f 6c 75 6d 6e 49       if( columnI
2756d 6e 64 65 78 28 70 52 69 67 68 74 54 61 62 2c 20  ndex(pRightTab, 
2756e 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20 20 20  zName)>=0 ){.   
2756f 20 20 20 20 20 20 20 61 64 64 57 68 65 72 65 54         addWhereT
27570 65 72 6d 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d  erm(pParse, zNam
27571 65 2c 20 70 4c 65 66 74 54 61 62 2c 20 70 4c 65  e, pLeftTab, pLe
27572 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20 20 20  ft->zAlias, .   
27573 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27574 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68             pRigh
27575 74 54 61 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41  tTab, pRight->zA
27576 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lias,.          
27577 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27578 20 20 20 20 70 52 69 67 68 74 2d 3e 69 43 75 72      pRight->iCur
27579 73 6f 72 2c 20 26 70 2d 3e 70 57 68 65 72 65 2c  sor, &p->pWhere,
2757a 20 69 73 4f 75 74 65 72 29 3b 0a 20 20 20 20 20   isOuter);.     
2757b 20 20 20 20 20 0a 20 20 20 20 20 20 20 20 7d 0a       .        }.
2757c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
2757d 20 20 20 2f 2a 20 44 69 73 61 6c 6c 6f 77 20 62     /* Disallow b
2757e 6f 74 68 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  oth ON and USING
2757f 20 63 6c 61 75 73 65 73 20 69 6e 20 74 68 65 20   clauses in the 
27580 73 61 6d 65 20 6a 6f 69 6e 0a 20 20 20 20 2a 2f  same join.    */
27581 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d  .    if( pRight-
27582 3e 70 4f 6e 20 26 26 20 70 52 69 67 68 74 2d 3e  >pOn && pRight->
27583 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20  pUsing ){.      
27584 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
27585 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
27586 68 61 76 65 20 62 6f 74 68 20 4f 4e 20 61 6e 64  have both ON and
27587 20 55 53 49 4e 47 20 22 0a 20 20 20 20 20 20 20   USING ".       
27588 20 22 63 6c 61 75 73 65 73 20 69 6e 20 74 68 65   "clauses in the
27589 20 73 61 6d 65 20 6a 6f 69 6e 22 29 3b 0a 20 20   same join");.  
2758a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2758b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20    }..    /* Add 
2758c 74 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 74 6f  the ON clause to
2758d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
2758e 57 48 45 52 45 20 63 6c 61 75 73 65 2c 20 63 6f  WHERE clause, co
2758f 6e 6e 65 63 74 65 64 20 62 79 0a 20 20 20 20 2a  nnected by.    *
27590 2a 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f  * an AND operato
27591 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  r..    */.    if
27592 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 29 7b  ( pRight->pOn ){
27593 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 75 74  .      if( isOut
27594 65 72 20 29 20 73 65 74 4a 6f 69 6e 45 78 70 72  er ) setJoinExpr
27595 28 70 52 69 67 68 74 2d 3e 70 4f 6e 2c 20 70 52  (pRight->pOn, pR
27596 69 67 68 74 2d 3e 69 43 75 72 73 6f 72 29 3b 0a  ight->iCursor);.
27597 20 20 20 20 20 20 70 2d 3e 70 57 68 65 72 65 20        p->pWhere 
27598 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64  = sqlite3ExprAnd
27599 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 2d 3e  (pParse->db, p->
2759a 70 57 68 65 72 65 2c 20 70 52 69 67 68 74 2d 3e  pWhere, pRight->
2759b 70 4f 6e 29 3b 0a 20 20 20 20 20 20 70 52 69 67  pOn);.      pRig
2759c 68 74 2d 3e 70 4f 6e 20 3d 20 30 3b 0a 20 20 20  ht->pOn = 0;.   
2759d 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74   }..    /* Creat
2759e 65 20 65 78 74 72 61 20 74 65 72 6d 73 20 6f 6e  e extra terms on
2759f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
275a0 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  e for each colum
275a1 6e 20 6e 61 6d 65 64 0a 20 20 20 20 2a 2a 20 69  n named.    ** i
275a2 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75  n the USING clau
275a3 73 65 2e 20 20 45 78 61 6d 70 6c 65 3a 20 49 66  se.  Example: If
275a4 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20   the two tables 
275a5 74 6f 20 62 65 20 6a 6f 69 6e 65 64 20 61 72 65  to be joined are
275a6 20 0a 20 20 20 20 2a 2a 20 41 20 61 6e 64 20 42   .    ** A and B
275a7 20 61 6e 64 20 74 68 65 20 55 53 49 4e 47 20 63   and the USING c
275a8 6c 61 75 73 65 20 6e 61 6d 65 73 20 58 2c 20 59  lause names X, Y
275a9 2c 20 61 6e 64 20 5a 2c 20 74 68 65 6e 20 61 64  , and Z, then ad
275aa 64 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 74 6f  d this.    ** to
275ab 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
275ac 65 3a 20 20 20 20 41 2e 58 3d 42 2e 58 20 41 4e  e:    A.X=B.X AN
275ad 44 20 41 2e 59 3d 42 2e 59 20 41 4e 44 20 41 2e  D A.Y=B.Y AND A.
275ae 5a 3d 42 2e 5a 0a 20 20 20 20 2a 2a 20 52 65 70  Z=B.Z.    ** Rep
275af 6f 72 74 20 61 6e 20 65 72 72 6f 72 20 69 66 20  ort an error if 
275b0 61 6e 79 20 63 6f 6c 75 6d 6e 20 6d 65 6e 74 69  any column menti
275b1 6f 6e 65 64 20 69 6e 20 74 68 65 20 55 53 49 4e  oned in the USIN
275b2 47 20 63 6c 61 75 73 65 20 69 73 0a 20 20 20 20  G clause is.    
275b3 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64  ** not contained
275b4 20 69 6e 20 62 6f 74 68 20 74 61 62 6c 65 73 20   in both tables 
275b5 74 6f 20 62 65 20 6a 6f 69 6e 65 64 2e 0a 20 20  to be joined..  
275b6 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69    */.    if( pRi
275b7 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20  ght->pUsing ){. 
275b8 20 20 20 20 20 49 64 4c 69 73 74 20 2a 70 4c 69       IdList *pLi
275b9 73 74 20 3d 20 70 52 69 67 68 74 2d 3e 70 55 73  st = pRight->pUs
275ba 69 6e 67 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  ing;.      for(j
275bb 3d 30 3b 20 6a 3c 70 4c 69 73 74 2d 3e 6e 49 64  =0; j<pList->nId
275bc 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
275bd 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 4c  char *zName = pL
275be 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b  ist->a[j].zName;
275bf 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f 6c  .        if( col
275c0 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66 74 54 61  umnIndex(pLeftTa
275c1 62 2c 20 7a 4e 61 6d 65 29 3c 30 20 7c 7c 20 63  b, zName)<0 || c
275c2 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68  olumnIndex(pRigh
275c3 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3c 30 20 29  tTab, zName)<0 )
275c4 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
275c5 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
275c6 73 65 2c 20 22 63 61 6e 6e 6f 74 20 6a 6f 69 6e  se, "cannot join
275c7 20 75 73 69 6e 67 20 63 6f 6c 75 6d 6e 20 25 73   using column %s
275c8 20 2d 20 63 6f 6c 75 6d 6e 20 22 0a 20 20 20 20   - column ".    
275c9 20 20 20 20 20 20 20 20 22 6e 6f 74 20 70 72 65          "not pre
275ca 73 65 6e 74 20 69 6e 20 62 6f 74 68 20 74 61 62  sent in both tab
275cb 6c 65 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  les", zName);.  
275cc 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
275cd 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
275ce 20 20 20 20 61 64 64 57 68 65 72 65 54 65 72 6d      addWhereTerm
275cf 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 2c 20  (pParse, zName, 
275d0 70 4c 65 66 74 54 61 62 2c 20 70 4c 65 66 74 2d  pLeftTab, pLeft-
275d1 3e 7a 41 6c 69 61 73 2c 20 0a 20 20 20 20 20 20  >zAlias, .      
275d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275d3 20 20 20 20 20 20 70 52 69 67 68 74 54 61 62 2c        pRightTab,
275d4 20 70 52 69 67 68 74 2d 3e 7a 41 6c 69 61 73 2c   pRight->zAlias,
275d5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
275d6 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69               pRi
275d7 67 68 74 2d 3e 69 43 75 72 73 6f 72 2c 20 26 70  ght->iCursor, &p
275d8 2d 3e 70 57 68 65 72 65 2c 20 69 73 4f 75 74 65  ->pWhere, isOute
275d9 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
275da 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
275db 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72  ;.}../*.** Inser
275dc 74 20 63 6f 64 65 20 69 6e 74 6f 20 22 76 22 20  t code into "v" 
275dd 74 68 61 74 20 77 69 6c 6c 20 70 75 73 68 20 74  that will push t
275de 68 65 20 72 65 63 6f 72 64 20 6f 6e 20 74 68 65  he record on the
275df 20 74 6f 70 20 6f 66 20 74 68 65 0a 2a 2a 20 73   top of the.** s
275e0 74 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 73 6f  tack into the so
275e1 72 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rter..*/.static 
275e2 76 6f 69 64 20 70 75 73 68 4f 6e 74 6f 53 6f 72  void pushOntoSor
275e3 74 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ter(.  Parse *pP
275e4 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  arse,         /*
275e5 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   Parser context 
275e6 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
275e7 4f 72 64 65 72 42 79 2c 20 20 20 20 2f 2a 20 54  OrderBy,    /* T
275e8 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
275e9 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  se */.  Select *
275ea 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 20 2f  pSelect,       /
275eb 2a 20 54 68 65 20 77 68 6f 6c 65 20 53 45 4c 45  * The whole SELE
275ec 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  CT statement */.
275ed 20 20 69 6e 74 20 72 65 67 44 61 74 61 20 20 20    int regData   
275ee 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
275ef 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 64 61 74  ster holding dat
275f0 61 20 74 6f 20 62 65 20 73 6f 72 74 65 64 20 2a  a to be sorted *
275f1 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
275f2 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
275f3 20 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70 4f    int nExpr = pO
275f4 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20  rderBy->nExpr;. 
275f5 20 69 6e 74 20 72 65 67 42 61 73 65 20 3d 20 73   int regBase = s
275f6 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
275f7 67 65 28 70 50 61 72 73 65 2c 20 6e 45 78 70 72  ge(pParse, nExpr
275f8 2b 32 29 3b 0a 20 20 69 6e 74 20 72 65 67 52 65  +2);.  int regRe
275f9 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33 47 65  cord = sqlite3Ge
275fa 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
275fb 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
275fc 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65  acheClear(pParse
275fd 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
275fe 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61  CodeExprList(pPa
275ff 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 72  rse, pOrderBy, r
27600 65 67 42 61 73 65 2c 20 30 29 3b 0a 20 20 73 71  egBase, 0);.  sq
27601 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27602 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63 65 2c 20  v, OP_Sequence, 
27603 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
27604 6f 72 2c 20 72 65 67 42 61 73 65 2b 6e 45 78 70  or, regBase+nExp
27605 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  r);.  sqlite3Exp
27606 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65  rCodeMove(pParse
27607 2c 20 72 65 67 44 61 74 61 2c 20 72 65 67 42 61  , regData, regBa
27608 73 65 2b 6e 45 78 70 72 2b 31 2c 20 31 29 3b 0a  se+nExpr+1, 1);.
27609 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2760a 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
2760b 63 6f 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e  cord, regBase, n
2760c 45 78 70 72 20 2b 20 32 2c 20 72 65 67 52 65 63  Expr + 2, regRec
2760d 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ord);.  sqlite3V
2760e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2760f 49 64 78 49 6e 73 65 72 74 2c 20 70 4f 72 64 65  IdxInsert, pOrde
27610 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 72  rBy->iECursor, r
27611 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
27612 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
27613 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 65  eg(pParse, regRe
27614 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  cord);.  sqlite3
27615 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
27616 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65  (pParse, regBase
27617 2c 20 6e 45 78 70 72 2b 32 29 3b 0a 20 20 69 66  , nExpr+2);.  if
27618 28 20 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69  ( pSelect->iLimi
27619 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 64 64  t ){.    int add
2761a 72 31 2c 20 61 64 64 72 32 3b 0a 20 20 20 20 69  r1, addr2;.    i
2761b 6e 74 20 69 4c 69 6d 69 74 3b 0a 20 20 20 20 69  nt iLimit;.    i
2761c 66 28 20 70 53 65 6c 65 63 74 2d 3e 69 4f 66 66  f( pSelect->iOff
2761d 73 65 74 20 29 7b 0a 20 20 20 20 20 20 69 4c 69  set ){.      iLi
2761e 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 69  mit = pSelect->i
2761f 4f 66 66 73 65 74 2b 31 3b 0a 20 20 20 20 7d 65  Offset+1;.    }e
27620 6c 73 65 7b 0a 20 20 20 20 20 20 69 4c 69 6d 69  lse{.      iLimi
27621 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 69 4c 69  t = pSelect->iLi
27622 6d 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  mit;.    }.    a
27623 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
27624 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
27625 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 29 3b 0a  fZero, iLimit);.
27626 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27627 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
27628 6d 6d 2c 20 69 4c 69 6d 69 74 2c 20 2d 31 29 3b  mm, iLimit, -1);
27629 0a 20 20 20 20 61 64 64 72 32 20 3d 20 73 71 6c  .    addr2 = sql
2762a 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
2762b 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20  , OP_Goto);.    
2762c 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2762d 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20  ere(v, addr1);. 
2762e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2762f 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 61 73 74 2c  dOp1(v, OP_Last,
27630 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72   pOrderBy->iECur
27631 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  sor);.    sqlite
27632 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
27633 50 5f 44 65 6c 65 74 65 2c 20 70 4f 72 64 65 72  P_Delete, pOrder
27634 42 79 2d 3e 69 45 43 75 72 73 6f 72 29 3b 0a 20  By->iECursor);. 
27635 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
27636 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 32 29  mpHere(v, addr2)
27637 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 2d 3e 69  ;.    pSelect->i
27638 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d  Limit = 0;.  }.}
27639 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65  ../*.** Add code
2763a 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
2763b 65 20 4f 46 46 53 45 54 0a 2a 2f 0a 73 74 61 74  e OFFSET.*/.stat
2763c 69 63 20 76 6f 69 64 20 63 6f 64 65 4f 66 66 73  ic void codeOffs
2763d 65 74 28 0a 20 20 56 64 62 65 20 2a 76 2c 20 20  et(.  Vdbe *v,  
2763e 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72          /* Gener
2763f 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68  ate code into th
27640 69 73 20 56 4d 20 2a 2f 0a 20 20 53 65 6c 65 63  is VM */.  Selec
27641 74 20 2a 70 2c 20 20 20 20 20 20 20 20 2f 2a 20  t *p,        /* 
27642 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
27643 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64  ment being coded
27644 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69   */.  int iConti
27645 6e 75 65 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20  nue     /* Jump 
27646 68 65 72 65 20 74 6f 20 73 6b 69 70 20 74 68 65  here to skip the
27647 20 63 75 72 72 65 6e 74 20 72 65 63 6f 72 64 20   current record 
27648 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 2d 3e 69  */.){.  if( p->i
27649 4f 66 66 73 65 74 20 26 26 20 69 43 6f 6e 74 69  Offset && iConti
2764a 6e 75 65 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  nue!=0 ){.    in
2764b 74 20 61 64 64 72 3b 0a 20 20 20 20 73 71 6c 69  t addr;.    sqli
2764c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2764d 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69   OP_AddImm, p->i
2764e 4f 66 66 73 65 74 2c 20 2d 31 29 3b 0a 20 20 20  Offset, -1);.   
2764f 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
27650 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
27651 49 66 4e 65 67 2c 20 70 2d 3e 69 4f 66 66 73 65  IfNeg, p->iOffse
27652 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
27653 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27654 47 6f 74 6f 2c 20 30 2c 20 69 43 6f 6e 74 69 6e  Goto, 0, iContin
27655 75 65 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  ue);.    VdbeCom
27656 6d 65 6e 74 28 28 76 2c 20 22 73 6b 69 70 20 4f  ment((v, "skip O
27657 46 46 53 45 54 20 72 65 63 6f 72 64 73 22 29 29  FFSET records"))
27658 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27659 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2765a 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r);.  }.}../*.**
2765b 20 41 64 64 20 63 6f 64 65 20 74 68 61 74 20 77   Add code that w
2765c 69 6c 6c 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b  ill check to mak
2765d 65 20 73 75 72 65 20 74 68 65 20 4e 20 72 65 67  e sure the N reg
2765e 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20  isters starting 
2765f 61 74 20 69 4d 65 6d 0a 2a 2a 20 66 6f 72 6d 20  at iMem.** form 
27660 61 20 64 69 73 74 69 6e 63 74 20 65 6e 74 72 79  a distinct entry
27661 2e 20 20 69 54 61 62 20 69 73 20 61 20 73 6f 72  .  iTab is a sor
27662 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61 74 20  ting index that 
27663 68 6f 6c 64 73 20 70 72 65 76 69 6f 75 73 6c 79  holds previously
27664 0a 2a 2a 20 73 65 65 6e 20 63 6f 6d 62 69 6e 61  .** seen combina
27665 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 4e 20 76  tions of the N v
27666 61 6c 75 65 73 2e 20 20 41 20 6e 65 77 20 65 6e  alues.  A new en
27667 74 72 79 20 69 73 20 6d 61 64 65 20 69 6e 20 69  try is made in i
27668 54 61 62 0a 2a 2a 20 69 66 20 74 68 65 20 63 75  Tab.** if the cu
27669 72 72 65 6e 74 20 4e 20 76 61 6c 75 65 73 20 61  rrent N values a
2766a 72 65 20 6e 65 77 2e 0a 2a 2a 0a 2a 2a 20 41 20  re new..**.** A 
2766b 6a 75 6d 70 20 74 6f 20 61 64 64 72 52 65 70 65  jump to addrRepe
2766c 61 74 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74  at is made and t
2766d 68 65 20 4e 2b 31 20 76 61 6c 75 65 73 20 61 72  he N+1 values ar
2766e 65 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68  e popped from th
2766f 65 0a 2a 2a 20 73 74 61 63 6b 20 69 66 20 74 68  e.** stack if th
27670 65 20 74 6f 70 20 4e 20 65 6c 65 6d 65 6e 74 73  e top N elements
27671 20 61 72 65 20 6e 6f 74 20 64 69 73 74 69 6e 63   are not distinc
27672 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
27673 64 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28 0a  d codeDistinct(.
27674 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
27675 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
27676 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
27677 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
27678 20 69 6e 74 20 69 54 61 62 2c 20 20 20 20 20 20   int iTab,      
27679 20 20 20 20 2f 2a 20 41 20 73 6f 72 74 69 6e 67      /* A sorting
2767a 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f 20 74   index used to t
2767b 65 73 74 20 66 6f 72 20 64 69 73 74 69 6e 63 74  est for distinct
2767c 6e 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 61 64  ness */.  int ad
2767d 64 72 52 65 70 65 61 74 2c 20 20 20 20 2f 2a 20  drRepeat,    /* 
2767e 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20  Jump to here if 
2767f 6e 6f 74 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a  not distinct */.
27680 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20    int N,        
27681 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
27682 66 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  f elements */.  
27683 69 6e 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20  int iMem        
27684 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d     /* First elem
27685 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  ent */.){.  Vdbe
27686 20 2a 76 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a   *v;.  int r1;..
27687 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56    v = pParse->pV
27688 64 62 65 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69  dbe;.  r1 = sqli
27689 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2768a 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
2768b 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2768c 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 69 4d 65  _MakeRecord, iMe
2768d 6d 2c 20 4e 2c 20 72 31 29 3b 0a 20 20 73 71 6c  m, N, r1);.  sql
2768e 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2768f 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 69 54 61 62  , OP_Found, iTab
27690 2c 20 61 64 64 72 52 65 70 65 61 74 2c 20 72 31  , addrRepeat, r1
27691 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
27692 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
27693 49 6e 73 65 72 74 2c 20 69 54 61 62 2c 20 72 31  Insert, iTab, r1
27694 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
27695 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
27696 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  e, r1);.}../*.**
27697 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72   Generate an err
27698 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65 6e 20  or message when 
27699 61 20 53 45 4c 45 43 54 20 69 73 20 75 73 65 64  a SELECT is used
2769a 20 77 69 74 68 69 6e 20 61 20 73 75 62 65 78 70   within a subexp
2769b 72 65 73 73 69 6f 6e 0a 2a 2a 20 28 65 78 61 6d  ression.** (exam
2769c 70 6c 65 3a 20 20 22 61 20 49 4e 20 28 53 45 4c  ple:  "a IN (SEL
2769d 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 6c 65  ECT * FROM table
2769e 29 22 29 20 62 75 74 20 69 74 20 68 61 73 20 6d  )") but it has m
2769f 6f 72 65 20 74 68 61 6e 20 31 20 72 65 73 75 6c  ore than 1 resul
276a0 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20 57 65  t.** column.  We
276a1 20 64 6f 20 74 68 69 73 20 69 6e 20 61 20 73 75   do this in a su
276a2 62 72 6f 75 74 69 6e 65 20 62 65 63 61 75 73 65  broutine because
276a3 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72   the error occur
276a4 73 20 69 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  s in multiple.**
276a5 20 70 6c 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74   places..*/.stat
276a6 69 63 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 4d  ic int checkForM
276a7 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74  ultiColumnSelect
276a8 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65 20 2a  Error(.  Parse *
276a9 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
276aa 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 2e 20   Parse context. 
276ab 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
276ac 2a 70 44 65 73 74 2c 20 20 20 2f 2a 20 44 65 73  *pDest,   /* Des
276ad 74 69 6e 61 74 69 6f 6e 20 6f 66 20 53 45 4c 45  tination of SELE
276ae 43 54 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20  CT results */.  
276af 69 6e 74 20 6e 45 78 70 72 20 20 20 20 20 20 20  int nExpr       
276b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
276b1 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
276b2 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 45 4c   returned by SEL
276b3 45 43 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ECT */.){.  int 
276b4 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65  eDest = pDest->e
276b5 44 65 73 74 3b 0a 20 20 69 66 28 20 6e 45 78 70  Dest;.  if( nExp
276b6 72 3e 31 20 26 26 20 28 65 44 65 73 74 3d 3d 53  r>1 && (eDest==S
276b7 52 54 5f 4d 65 6d 20 7c 7c 20 65 44 65 73 74 3d  RT_Mem || eDest=
276b8 3d 53 52 54 5f 53 65 74 29 20 29 7b 0a 20 20 20  =SRT_Set) ){.   
276b9 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
276ba 28 70 50 61 72 73 65 2c 20 22 6f 6e 6c 79 20 61  (pParse, "only a
276bb 20 73 69 6e 67 6c 65 20 72 65 73 75 6c 74 20 61   single result a
276bc 6c 6c 6f 77 65 64 20 66 6f 72 20 22 0a 20 20 20  llowed for ".   
276bd 20 20 20 20 22 61 20 53 45 4c 45 43 54 20 74 68      "a SELECT th
276be 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e  at is part of an
276bf 20 65 78 70 72 65 73 73 69 6f 6e 22 29 3b 0a 20   expression");. 
276c0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
276c1 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
276c2 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
276c3 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
276c4 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65  nerates the code
276c5 20 66 6f 72 20 74 68 65 20 69 6e 73 69 64 65 20   for the inside 
276c6 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f  of the inner loo
276c7 70 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54  p.** of a SELECT
276c8 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 54 61  ..**.** If srcTa
276c9 62 20 61 6e 64 20 6e 43 6f 6c 75 6d 6e 20 61 72  b and nColumn ar
276ca 65 20 62 6f 74 68 20 7a 65 72 6f 2c 20 74 68 65  e both zero, the
276cb 6e 20 74 68 65 20 70 45 4c 69 73 74 20 65 78 70  n the pEList exp
276cc 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  ressions.** are 
276cd 65 76 61 6c 75 61 74 65 64 20 69 6e 20 6f 72 64  evaluated in ord
276ce 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 64 61  er to get the da
276cf 74 61 20 66 6f 72 20 74 68 69 73 20 72 6f 77 2e  ta for this row.
276d0 20 20 49 66 20 6e 43 6f 6c 75 6d 6e 3e 30 0a 2a    If nColumn>0.*
276d1 2a 20 74 68 65 6e 20 64 61 74 61 20 69 73 20 70  * then data is p
276d2 75 6c 6c 65 64 20 66 72 6f 6d 20 73 72 63 54 61  ulled from srcTa
276d3 62 20 61 6e 64 20 70 45 4c 69 73 74 20 69 73 20  b and pEList is 
276d4 75 73 65 64 20 6f 6e 6c 79 20 74 6f 20 67 65 74  used only to get
276d5 20 74 68 65 0a 2a 2a 20 64 61 74 61 74 79 70 65   the.** datatype
276d6 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  s for each colum
276d7 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
276d8 64 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f  d selectInnerLoo
276d9 70 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  p(.  Parse *pPar
276da 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
276db 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
276dc 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
276dd 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
276de 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  /* The complete 
276df 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
276e0 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a   being coded */.
276e1 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
276e2 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  st,       /* Lis
276e3 74 20 6f 66 20 76 61 6c 75 65 73 20 62 65 69 6e  t of values bein
276e4 67 20 65 78 74 72 61 63 74 65 64 20 2a 2f 0a 20  g extracted */. 
276e5 20 69 6e 74 20 73 72 63 54 61 62 2c 20 20 20 20   int srcTab,    
276e6 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 6c 6c           /* Pull
276e7 20 64 61 74 61 20 66 72 6f 6d 20 74 68 69 73 20   data from this 
276e8 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
276e9 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20  Column,         
276ea 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
276eb 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73  columns in the s
276ec 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20  ource table */. 
276ed 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
276ee 72 42 79 2c 20 20 20 20 20 2f 2a 20 49 66 20 6e  rBy,     /* If n
276ef 6f 74 20 4e 55 4c 4c 2c 20 73 6f 72 74 20 72 65  ot NULL, sort re
276f0 73 75 6c 74 73 20 75 73 69 6e 67 20 74 68 69 73  sults using this
276f1 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 64 69   key */.  int di
276f2 73 74 69 6e 63 74 2c 20 20 20 20 20 20 20 20 20  stinct,         
276f3 20 20 2f 2a 20 49 66 20 3e 3d 30 2c 20 6d 61 6b    /* If >=0, mak
276f4 65 20 73 75 72 65 20 72 65 73 75 6c 74 73 20 61  e sure results a
276f5 72 65 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20  re distinct */. 
276f6 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65   SelectDest *pDe
276f7 73 74 2c 20 20 20 20 20 20 2f 2a 20 48 6f 77 20  st,      /* How 
276f8 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68  to dispose of th
276f9 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69  e results */.  i
276fa 6e 74 20 69 43 6f 6e 74 69 6e 75 65 2c 20 20 20  nt iContinue,   
276fb 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
276fc 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  ere to continue 
276fd 77 69 74 68 20 6e 65 78 74 20 72 6f 77 20 2a 2f  with next row */
276fe 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20  .  int iBreak   
276ff 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75             /* Ju
27700 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b  mp here to break
27701 20 6f 75 74 20 6f 66 20 74 68 65 20 69 6e 6e 65   out of the inne
27702 72 20 6c 6f 6f 70 20 2a 2f 0a 29 7b 0a 20 20 56  r loop */.){.  V
27703 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
27704 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b  >pVdbe;.  int i;
27705 0a 20 20 69 6e 74 20 68 61 73 44 69 73 74 69 6e  .  int hasDistin
27706 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ct;        /* Tr
27707 75 65 20 69 66 20 74 68 65 20 44 49 53 54 49 4e  ue if the DISTIN
27708 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72  CT keyword is pr
27709 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72  esent */.  int r
2770a 65 67 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20  egResult;       
2770b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20         /* Start 
2770c 6f 66 20 6d 65 6d 6f 72 79 20 68 6f 6c 64 69 6e  of memory holdin
2770d 67 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  g result set */.
2770e 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44    int eDest = pD
2770f 65 73 74 2d 3e 65 44 65 73 74 3b 20 20 20 2f 2a  est->eDest;   /*
27710 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20   How to dispose 
27711 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20  of results */.  
27712 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73  int iParm = pDes
27713 74 2d 3e 69 50 61 72 6d 3b 20 20 20 2f 2a 20 46  t->iParm;   /* F
27714 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
27715 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64   disposal method
27716 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c   */.  int nResul
27717 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  tCol;           
27718 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
27719 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f  esult columns */
2771a 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b  ..  assert( v );
2771b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d  .  if( NEVER(v==
2771c 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  0) ) return;.  a
2771d 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30  ssert( pEList!=0
2771e 20 29 3b 0a 20 20 68 61 73 44 69 73 74 69 6e 63   );.  hasDistinc
2771f 74 20 3d 20 64 69 73 74 69 6e 63 74 3e 3d 30 3b  t = distinct>=0;
27720 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d  .  if( pOrderBy=
27721 3d 30 20 26 26 20 21 68 61 73 44 69 73 74 69 6e  =0 && !hasDistin
27722 63 74 20 29 7b 0a 20 20 20 20 63 6f 64 65 4f 66  ct ){.    codeOf
27723 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74  fset(v, p, iCont
27724 69 6e 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  inue);.  }..  /*
27725 20 50 75 6c 6c 20 74 68 65 20 72 65 71 75 65 73   Pull the reques
27726 74 65 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a  ted columns..  *
27727 2f 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e  /.  if( nColumn>
27728 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74  0 ){.    nResult
27729 43 6f 6c 20 3d 20 6e 43 6f 6c 75 6d 6e 3b 0a 20  Col = nColumn;. 
2772a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 52 65 73   }else{.    nRes
2772b 75 6c 74 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d  ultCol = pEList-
2772c 3e 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 69 66  >nExpr;.  }.  if
2772d 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30  ( pDest->iMem==0
2772e 20 29 7b 0a 20 20 20 20 70 44 65 73 74 2d 3e 69   ){.    pDest->i
2772f 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Mem = pParse->nM
27730 65 6d 2b 31 3b 0a 20 20 20 20 70 44 65 73 74 2d  em+1;.    pDest-
27731 3e 6e 4d 65 6d 20 3d 20 6e 52 65 73 75 6c 74 43  >nMem = nResultC
27732 6f 6c 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  ol;.    pParse->
27733 6e 4d 65 6d 20 2b 3d 20 6e 52 65 73 75 6c 74 43  nMem += nResultC
27734 6f 6c 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20  ol;.  }else{ .  
27735 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74 2d    assert( pDest-
27736 3e 6e 4d 65 6d 3d 3d 6e 52 65 73 75 6c 74 43 6f  >nMem==nResultCo
27737 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 67 52 65  l );.  }.  regRe
27738 73 75 6c 74 20 3d 20 70 44 65 73 74 2d 3e 69 4d  sult = pDest->iM
27739 65 6d 3b 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d  em;.  if( nColum
2773a 6e 3e 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  n>0 ){.    for(i
2773b 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69  =0; i<nColumn; i
2773c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
2773d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2773e 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61  OP_Column, srcTa
2773f 62 2c 20 69 2c 20 72 65 67 52 65 73 75 6c 74 2b  b, i, regResult+
27740 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  i);.    }.  }els
27741 65 20 69 66 28 20 65 44 65 73 74 21 3d 53 52 54  e if( eDest!=SRT
27742 5f 45 78 69 73 74 73 20 29 7b 0a 20 20 20 20 2f  _Exists ){.    /
27743 2a 20 49 66 20 74 68 65 20 64 65 73 74 69 6e 61  * If the destina
27744 74 69 6f 6e 20 69 73 20 61 6e 20 45 58 49 53 54  tion is an EXIST
27745 53 28 2e 2e 2e 29 20 65 78 70 72 65 73 73 69 6f  S(...) expressio
27746 6e 2c 20 74 68 65 20 61 63 74 75 61 6c 0a 20 20  n, the actual.  
27747 20 20 2a 2a 20 76 61 6c 75 65 73 20 72 65 74 75    ** values retu
27748 72 6e 65 64 20 62 79 20 74 68 65 20 53 45 4c 45  rned by the SELE
27749 43 54 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69  CT are not requi
2774a 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  red..    */.    
2774b 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2774c 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20  Clear(pParse);. 
2774d 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
2774e 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  deExprList(pPars
2774f 65 2c 20 70 45 4c 69 73 74 2c 20 72 65 67 52 65  e, pEList, regRe
27750 73 75 6c 74 2c 20 65 44 65 73 74 3d 3d 53 52 54  sult, eDest==SRT
27751 5f 4f 75 74 70 75 74 29 3b 0a 20 20 7d 0a 20 20  _Output);.  }.  
27752 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 75 6c  nColumn = nResul
27753 74 43 6f 6c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  tCol;..  /* If t
27754 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  he DISTINCT keyw
27755 6f 72 64 20 77 61 73 20 70 72 65 73 65 6e 74 20  ord was present 
27756 6f 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  on the SELECT st
27757 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 61 6e 64  atement.  ** and
27758 20 74 68 69 73 20 72 6f 77 20 68 61 73 20 62 65   this row has be
27759 65 6e 20 73 65 65 6e 20 62 65 66 6f 72 65 2c 20  en seen before, 
2775a 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6d 61 6b 65  then do not make
2775b 20 74 68 69 73 20 72 6f 77 0a 20 20 2a 2a 20 70   this row.  ** p
2775c 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c  art of the resul
2775d 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 68 61  t..  */.  if( ha
2775e 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20  sDistinct ){.   
2775f 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
27760 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
27761 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  ( pEList->nExpr=
27762 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20  =nColumn );.    
27763 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50 61  codeDistinct(pPa
27764 72 73 65 2c 20 64 69 73 74 69 6e 63 74 2c 20 69  rse, distinct, i
27765 43 6f 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d  Continue, nColum
27766 6e 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20  n, regResult);. 
27767 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d     if( pOrderBy=
27768 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 64 65  =0 ){.      code
27769 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f  Offset(v, p, iCo
2776a 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 7d 0a 20  ntinue);.    }. 
2776b 20 7d 0a 0a 20 20 69 66 28 20 63 68 65 63 6b 46   }..  if( checkF
2776c 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c  orMultiColumnSel
2776d 65 63 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c  ectError(pParse,
2776e 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e   pDest, pEList->
2776f 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 72 65  nExpr) ){.    re
27770 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  turn;.  }..  swi
27771 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a 20 20  tch( eDest ){.  
27772 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64    /* In this mod
27773 65 2c 20 77 72 69 74 65 20 65 61 63 68 20 71 75  e, write each qu
27774 65 72 79 20 72 65 73 75 6c 74 20 74 6f 20 74 68  ery result to th
27775 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 65 6d  e key of the tem
27776 70 6f 72 61 72 79 0a 20 20 20 20 2a 2a 20 74 61  porary.    ** ta
27777 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a  ble iParm..    *
27778 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
27779 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
2777a 45 4c 45 43 54 0a 20 20 20 20 63 61 73 65 20 53  ELECT.    case S
2777b 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20 20 20  RT_Union: {.    
2777c 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20    int r1;.      
2777d 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
2777e 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
2777f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27780 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
27781 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73  keRecord, regRes
27782 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31  ult, nColumn, r1
27783 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27784 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27785 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72  _IdxInsert, iPar
27786 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  m, r1);.      sq
27787 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
27788 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
27789 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2778a 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 73    }..    /* Cons
2778b 74 72 75 63 74 20 61 20 72 65 63 6f 72 64 20 66  truct a record f
2778c 72 6f 6d 20 74 68 65 20 71 75 65 72 79 20 72 65  rom the query re
2778d 73 75 6c 74 2c 20 62 75 74 20 69 6e 73 74 65 61  sult, but instea
2778e 64 20 6f 66 0a 20 20 20 20 2a 2a 20 73 61 76 69  d of.    ** savi
2778f 6e 67 20 74 68 61 74 20 72 65 63 6f 72 64 2c 20  ng that record, 
27790 75 73 65 20 69 74 20 61 73 20 61 20 6b 65 79 20  use it as a key 
27791 74 6f 20 64 65 6c 65 74 65 20 65 6c 65 6d 65 6e  to delete elemen
27792 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74  ts from.    ** t
27793 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
27794 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f  le iParm..    */
27795 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78  .    case SRT_Ex
27796 63 65 70 74 3a 20 7b 0a 20 20 20 20 20 20 73 71  cept: {.      sq
27797 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
27798 76 2c 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c  v, OP_IdxDelete,
27799 20 69 50 61 72 6d 2c 20 72 65 67 52 65 73 75 6c   iParm, regResul
2779a 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  t, nColumn);.   
2779b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2779c 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53  #endif..    /* S
2779d 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
2779e 61 73 20 64 61 74 61 20 75 73 69 6e 67 20 61 20  as data using a 
2779f 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20  unique key..    
277a0 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
277a1 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
277a2 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
277a3 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
277a4 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
277a5 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
277a6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
277a7 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
277a8 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e  rd, regResult, n
277a9 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20  Column, r1);.   
277aa 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
277ab 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f  ){.        pushO
277ac 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65  ntoSorter(pParse
277ad 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72  , pOrderBy, p, r
277ae 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  1);.      }else{
277af 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 20  .        int r2 
277b0 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
277b1 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
277b2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
277b3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
277b4 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 72 32  Rowid, iParm, r2
277b5 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
277b6 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
277b7 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  OP_Insert, iParm
277b8 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20  , r1, r2);.     
277b9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
277ba 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
277bb 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20  _APPEND);.      
277bc 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
277bd 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
277be 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r2);.      }.   
277bf 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
277c0 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
277c1 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r1);.      brea
277c2 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65  k;.    }..#ifnde
277c3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
277c4 42 51 55 45 52 59 0a 20 20 20 20 2f 2a 20 49 66  BQUERY.    /* If
277c5 20 77 65 20 61 72 65 20 63 72 65 61 74 69 6e 67   we are creating
277c6 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20 22 65   a set for an "e
277c7 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54 20 2e  xpr IN (SELECT .
277c8 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a  ..)" construct,.
277c9 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72      ** then ther
277ca 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 69  e should be a si
277cb 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74 68 65  ngle item on the
277cc 20 73 74 61 63 6b 2e 20 20 57 72 69 74 65 20 74   stack.  Write t
277cd 68 69 73 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20  his.    ** item 
277ce 69 6e 74 6f 20 74 68 65 20 73 65 74 20 74 61 62  into the set tab
277cf 6c 65 20 77 69 74 68 20 62 6f 67 75 73 20 64 61  le with bogus da
277d0 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ta..    */.    c
277d1 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20  ase SRT_Set: {. 
277d2 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f       assert( nCo
277d3 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20  lumn==1 );.     
277d4 20 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73   p->affinity = s
277d5 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
277d6 69 6e 69 74 79 28 70 45 4c 69 73 74 2d 3e 61 5b  inity(pEList->a[
277d7 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d  0].pExpr, pDest-
277d8 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20  >affinity);.    
277d9 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
277da 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 74 20  {.        /* At 
277db 66 69 72 73 74 20 67 6c 61 6e 63 65 20 79 6f 75  first glance you
277dc 20 77 6f 75 6c 64 20 74 68 69 6e 6b 20 77 65 20   would think we 
277dd 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20 6f  could optimize o
277de 75 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ut the.        *
277df 2a 20 4f 52 44 45 52 20 42 59 20 69 6e 20 74 68  * ORDER BY in th
277e0 69 73 20 63 61 73 65 20 73 69 6e 63 65 20 74 68  is case since th
277e1 65 20 6f 72 64 65 72 20 6f 66 20 65 6e 74 72 69  e order of entri
277e2 65 73 20 69 6e 20 74 68 65 20 73 65 74 0a 20 20  es in the set.  
277e3 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f        ** does no
277e4 74 20 6d 61 74 74 65 72 2e 20 20 42 75 74 20 74  t matter.  But t
277e5 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 61 20  here might be a 
277e6 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20 69 6e  LIMIT clause, in
277e7 20 77 68 69 63 68 0a 20 20 20 20 20 20 20 20 2a   which.        *
277e8 2a 20 63 61 73 65 20 74 68 65 20 6f 72 64 65 72  * case the order
277e9 20 64 6f 65 73 20 6d 61 74 74 65 72 20 2a 2f 0a   does matter */.
277ea 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f          pushOnto
277eb 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70  Sorter(pParse, p
277ec 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67 52  OrderBy, p, regR
277ed 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65  esult);.      }e
277ee 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
277ef 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
277f0 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
277f1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
277f2 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
277f3 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
277f4 52 65 73 75 6c 74 2c 20 31 2c 20 72 31 2c 20 26  Result, 1, r1, &
277f5 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b  p->affinity, 1);
277f6 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
277f7 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
277f8 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
277f9 72 65 67 52 65 73 75 6c 74 2c 20 31 29 3b 0a 20  regResult, 1);. 
277fa 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
277fb 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
277fc 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c  dxInsert, iParm,
277fd 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r1);.        sq
277fe 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
277ff 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
27800 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
27801 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
27802 20 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77 20 65   /* If any row e
27803 78 69 73 74 20 69 6e 20 74 68 65 20 72 65 73 75  xist in the resu
27804 6c 74 20 73 65 74 2c 20 72 65 63 6f 72 64 20 74  lt set, record t
27805 68 61 74 20 66 61 63 74 20 61 6e 64 20 61 62 6f  hat fact and abo
27806 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  rt..    */.    c
27807 61 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a 20  ase SRT_Exists: 
27808 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
27809 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2780a 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 50 61 72  Integer, 1, iPar
2780b 6d 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  m);.      /* The
2780c 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69   LIMIT clause wi
2780d 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  ll terminate the
2780e 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a   loop for us */.
2780f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27810 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
27811 69 73 20 69 73 20 61 20 73 63 61 6c 61 72 20 73  is is a scalar s
27812 65 6c 65 63 74 20 74 68 61 74 20 69 73 20 70 61  elect that is pa
27813 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  rt of an express
27814 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  ion, then.    **
27815 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
27816 74 73 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70  ts in the approp
27817 72 69 61 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c  riate memory cel
27818 6c 20 61 6e 64 20 62 72 65 61 6b 20 6f 75 74 0a  l and break out.
27819 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63      ** of the sc
2781a 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a  an loop..    */.
2781b 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d      case SRT_Mem
2781c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
2781d 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
2781e 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72        if( pOrder
2781f 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75  By ){.        pu
27820 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61  shOntoSorter(pPa
27821 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  rse, pOrderBy, p
27822 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20  , regResult);.  
27823 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27824 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
27825 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 72  deMove(pParse, r
27826 65 67 52 65 73 75 6c 74 2c 20 69 50 61 72 6d 2c  egResult, iParm,
27827 20 31 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20   1);.        /* 
27828 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  The LIMIT clause
27829 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f   will jump out o
2782a 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  f the loop for u
2782b 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  s */.      }.   
2782c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2782d 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65  #endif /* #ifnde
2782e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
2782f 42 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20 2f  BQUERY */..    /
27830 2a 20 53 65 6e 64 20 74 68 65 20 64 61 74 61 20  * Send the data 
27831 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
27832 66 75 6e 63 74 69 6f 6e 20 6f 72 20 74 6f 20 61  function or to a
27833 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 49 6e   subroutine.  In
27834 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 73 65   the.    ** case
27835 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65   of a subroutine
27836 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65  , the subroutine
27837 20 69 74 73 65 6c 66 20 69 73 20 72 65 73 70 6f   itself is respo
27838 6e 73 69 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a  nsible for.    *
27839 2a 20 70 6f 70 70 69 6e 67 20 74 68 65 20 64 61  * popping the da
2783a 74 61 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ta from the stac
2783b 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  k..    */.    ca
2783c 73 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  se SRT_Coroutine
2783d 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4f  :.    case SRT_O
2783e 75 74 70 75 74 3a 20 7b 0a 20 20 20 20 20 20 69  utput: {.      i
2783f 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
27840 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20         int r1 = 
27841 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
27842 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
27843 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27844 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
27845 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c 74  ecord, regResult
27846 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a  , nColumn, r1);.
27847 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f          pushOnto
27848 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70  Sorter(pParse, p
27849 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31 29 3b  OrderBy, p, r1);
2784a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2784b 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2784c 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20  Parse, r1);.    
2784d 20 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73    }else if( eDes
2784e 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  t==SRT_Coroutine
2784f 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
27850 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
27851 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74   OP_Yield, pDest
27852 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20  ->iParm);.      
27853 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
27854 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27855 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
27856 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f  , regResult, nCo
27857 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 73  lumn);.        s
27858 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
27859 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
2785a 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c  arse, regResult,
2785b 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
2785c 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
2785d 20 20 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69      }..#if !defi
2785e 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2785f 54 52 49 47 47 45 52 29 0a 20 20 20 20 2f 2a 20  TRIGGER).    /* 
27860 44 69 73 63 61 72 64 20 74 68 65 20 72 65 73 75  Discard the resu
27861 6c 74 73 2e 20 20 54 68 69 73 20 69 73 20 75 73  lts.  This is us
27862 65 64 20 66 6f 72 20 53 45 4c 45 43 54 20 73 74  ed for SELECT st
27863 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 0a  atements inside.
27864 20 20 20 20 2a 2a 20 74 68 65 20 62 6f 64 79 20      ** the body 
27865 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20 20 54  of a TRIGGER.  T
27866 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 73 75  he purpose of su
27867 63 68 20 73 65 6c 65 63 74 73 20 69 73 20 74 6f  ch selects is to
27868 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75 73 65   call.    ** use
27869 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  r-defined functi
2786a 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20 73 69  ons that have si
2786b 64 65 20 65 66 66 65 63 74 73 2e 20 20 57 65 20  de effects.  We 
2786c 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20 20 20  do not care.    
2786d 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ** about the act
2786e 75 61 6c 20 72 65 73 75 6c 74 73 20 6f 66 20 74  ual results of t
2786f 68 65 20 73 65 6c 65 63 74 2e 0a 20 20 20 20 2a  he select..    *
27870 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  /.    default: {
27871 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
27872 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 63 61 72  Dest==SRT_Discar
27873 64 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  d );.      break
27874 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
27875 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f   }..  /* Jump to
27876 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
27877 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c 49 4d 49  loop if the LIMI
27878 54 20 69 73 20 72 65 61 63 68 65 64 2e 0a 20 20  T is reached..  
27879 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d  */.  if( p->iLim
2787a 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  it ){.    assert
2787b 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b  ( pOrderBy==0 );
2787c 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
2787d 20 61 6e 20 4f 52 44 45 52 20 42 59 2c 20 74 68   an ORDER BY, th
2787e 65 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20  e call to.      
2787f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27880 20 20 20 20 20 20 2a 2a 20 70 75 73 68 4f 6e 74        ** pushOnt
27881 6f 53 6f 72 74 65 72 28 29 20 77 6f 75 6c 64 20  oSorter() would 
27882 68 61 76 65 20 63 6c 65 61 72 65 64 20 70 2d 3e  have cleared p->
27883 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 20 20 73 71  iLimit */.    sq
27884 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27885 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70 2d  v, OP_AddImm, p-
27886 3e 69 4c 69 6d 69 74 2c 20 2d 31 29 3b 0a 20 20  >iLimit, -1);.  
27887 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27888 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f  Op2(v, OP_IfZero
27889 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42 72  , p->iLimit, iBr
2788a 65 61 6b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  eak);.  }.}../*.
2788b 2a 2a 20 47 69 76 65 6e 20 61 6e 20 65 78 70 72  ** Given an expr
2788c 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 67 65 6e  ession list, gen
2788d 65 72 61 74 65 20 61 20 4b 65 79 49 6e 66 6f 20  erate a KeyInfo 
2788e 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 72  structure that r
2788f 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20 63 6f  ecords.** the co
27890 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
27891 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65 73   for each expres
27892 73 69 6f 6e 20 69 6e 20 74 68 61 74 20 65 78 70  sion in that exp
27893 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a  ression list..**
27894 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 70 72 4c  .** If the ExprL
27895 69 73 74 20 69 73 20 61 6e 20 4f 52 44 45 52 20  ist is an ORDER 
27896 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
27897 6c 61 75 73 65 20 74 68 65 6e 20 74 68 65 20 72  lause then the r
27898 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 4b 65 79 49  esulting.** KeyI
27899 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73  nfo structure is
2789a 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
2789b 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20   initializing a 
2789c 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20 74 6f  virtual index to
2789d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  .** implement th
2789e 61 74 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74  at clause.  If t
2789f 68 65 20 45 78 70 72 4c 69 73 74 20 69 73 20 74  he ExprList is t
278a0 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
278a1 20 61 20 53 45 4c 45 43 54 0a 2a 2a 20 74 68 65   a SELECT.** the
278a2 6e 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  n the KeyInfo st
278a3 72 75 63 74 75 72 65 20 69 73 20 61 70 70 72 6f  ructure is appro
278a4 70 72 69 61 74 65 20 66 6f 72 20 69 6e 69 74 69  priate for initi
278a5 61 6c 69 7a 69 6e 67 20 61 20 76 69 72 74 75 61  alizing a virtua
278a6 6c 0a 2a 2a 20 69 6e 64 65 78 20 74 6f 20 69 6d  l.** index to im
278a7 70 6c 65 6d 65 6e 74 20 61 20 44 49 53 54 49 4e  plement a DISTIN
278a8 43 54 20 74 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 53  CT test..**.** S
278a9 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
278aa 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
278ab 72 65 20 69 73 20 6f 62 74 61 69 6e 20 66 72 6f  re is obtain fro
278ac 6d 20 6d 61 6c 6c 6f 63 2e 20 20 54 68 65 20 63  m malloc.  The c
278ad 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
278ae 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  on is responsibl
278af 65 20 66 6f 72 20 73 65 65 69 6e 67 20 74 68 61  e for seeing tha
278b0 74 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  t this structure
278b1 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a   is eventually.*
278b2 2a 20 66 72 65 65 64 2e 20 20 41 64 64 20 74 68  * freed.  Add th
278b3 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
278b4 75 72 65 20 74 6f 20 74 68 65 20 50 34 20 66 69  ure to the P4 fi
278b5 65 6c 64 20 6f 66 20 61 6e 20 6f 70 63 6f 64 65  eld of an opcode
278b6 20 75 73 69 6e 67 0a 2a 2a 20 50 34 5f 4b 45 59   using.** P4_KEY
278b7 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 73 20  INFO_HANDOFF is 
278b8 74 68 65 20 75 73 75 61 6c 20 77 61 79 20 6f 66  the usual way of
278b9 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 74 68   dealing with th
278ba 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4b 65  is..*/.static Ke
278bb 79 49 6e 66 6f 20 2a 6b 65 79 49 6e 66 6f 46 72  yInfo *keyInfoFr
278bc 6f 6d 45 78 70 72 4c 69 73 74 28 50 61 72 73 65  omExprList(Parse
278bd 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c 69   *pParse, ExprLi
278be 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 73 71  st *pList){.  sq
278bf 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
278c0 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 6e 45  se->db;.  int nE
278c1 78 70 72 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  xpr;.  KeyInfo *
278c2 70 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20  pInfo;.  struct 
278c3 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
278c4 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a  Item;.  int i;..
278c5 20 20 6e 45 78 70 72 20 3d 20 70 4c 69 73 74 2d    nExpr = pList-
278c6 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 66 6f 20  >nExpr;.  pInfo 
278c7 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
278c8 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
278c9 28 2a 70 49 6e 66 6f 29 20 2b 20 6e 45 78 70 72  (*pInfo) + nExpr
278ca 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  *(sizeof(CollSeq
278cb 2a 29 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 70  *)+1) );.  if( p
278cc 49 6e 66 6f 20 29 7b 0a 20 20 20 20 70 49 6e 66  Info ){.    pInf
278cd 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  o->aSortOrder = 
278ce 28 75 38 2a 29 26 70 49 6e 66 6f 2d 3e 61 43 6f  (u8*)&pInfo->aCo
278cf 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20 20 20 70  ll[nExpr];.    p
278d0 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20 28  Info->nField = (
278d1 75 31 36 29 6e 45 78 70 72 3b 0a 20 20 20 20 70  u16)nExpr;.    p
278d2 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28  Info->enc = ENC(
278d3 64 62 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e  db);.    pInfo->
278d4 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 66 6f 72  db = db;.    for
278d5 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73  (i=0, pItem=pLis
278d6 74 2d 3e 61 3b 20 69 3c 6e 45 78 70 72 3b 20 69  t->a; i<nExpr; i
278d7 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
278d8 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
278d9 6c 6c 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20  ll;.      pColl 
278da 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
278db 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 49 74  lSeq(pParse, pIt
278dc 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20  em->pExpr);.    
278dd 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a    if( !pColl ){.
278de 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
278df 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20  db->pDfltColl;. 
278e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 6e       }.      pIn
278e1 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70  fo->aColl[i] = p
278e2 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 49 6e 66  Coll;.      pInf
278e3 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  o->aSortOrder[i]
278e4 20 3d 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f 72   = pItem->sortOr
278e5 64 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  der;.    }.  }. 
278e6 20 72 65 74 75 72 6e 20 70 49 6e 66 6f 3b 0a 7d   return pInfo;.}
278e7 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  .../*.** If the 
278e8 69 6e 6e 65 72 20 6c 6f 6f 70 20 77 61 73 20 67  inner loop was g
278e9 65 6e 65 72 61 74 65 64 20 75 73 69 6e 67 20 61  enerated using a
278ea 20 6e 6f 6e 2d 6e 75 6c 6c 20 70 4f 72 64 65 72   non-null pOrder
278eb 42 79 20 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20  By argument,.** 
278ec 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  then the results
278ed 20 77 65 72 65 20 70 6c 61 63 65 64 20 69 6e 20   were placed in 
278ee 61 20 73 6f 72 74 65 72 2e 20 20 41 66 74 65 72  a sorter.  After
278ef 20 74 68 65 20 6c 6f 6f 70 20 69 73 20 74 65 72   the loop is ter
278f0 6d 69 6e 61 74 65 64 0a 2a 2a 20 77 65 20 6e 65  minated.** we ne
278f1 65 64 20 74 6f 20 72 75 6e 20 74 68 65 20 73 6f  ed to run the so
278f2 72 74 65 72 20 61 6e 64 20 6f 75 74 70 75 74 20  rter and output 
278f3 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 54 68  the results.  Th
278f4 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 72  e following.** r
278f5 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
278f6 20 74 68 65 20 63 6f 64 65 20 6e 65 65 64 65 64   the code needed
278f7 20 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a   to do that..*/.
278f8 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65  static void gene
278f9 72 61 74 65 53 6f 72 74 54 61 69 6c 28 0a 20 20  rateSortTail(.  
278fa 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
278fb 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
278fc 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
278fd 20 2a 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54   *p,        /* T
278fe 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
278ff 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  ent */.  Vdbe *v
27900 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65  ,          /* Ge
27901 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f  nerate code into
27902 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20   this VDBE */.  
27903 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  int nColumn,    
27904 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
27905 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 2a  olumns of data *
27906 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
27907 70 44 65 73 74 20 2f 2a 20 57 72 69 74 65 20 74  pDest /* Write t
27908 68 65 20 73 6f 72 74 65 64 20 72 65 73 75 6c 74  he sorted result
27909 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
2790a 6e 74 20 61 64 64 72 42 72 65 61 6b 20 3d 20 73  nt addrBreak = s
2790b 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
2790c 62 65 6c 28 76 29 3b 20 20 20 20 20 2f 2a 20 4a  bel(v);     /* J
2790d 75 6d 70 20 68 65 72 65 20 74 6f 20 65 78 69 74  ump here to exit
2790e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61   loop */.  int a
2790f 64 64 72 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71  ddrContinue = sq
27910 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
27911 65 6c 28 76 29 3b 20 20 2f 2a 20 4a 75 6d 70 20  el(v);  /* Jump 
27912 68 65 72 65 20 66 6f 72 20 6e 65 78 74 20 63 79  here for next cy
27913 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  cle */.  int add
27914 72 3b 0a 20 20 69 6e 74 20 69 54 61 62 3b 0a 20  r;.  int iTab;. 
27915 20 69 6e 74 20 70 73 65 75 64 6f 54 61 62 20 3d   int pseudoTab =
27916 20 30 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   0;.  ExprList *
27917 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f  pOrderBy = p->pO
27918 72 64 65 72 42 79 3b 0a 0a 20 20 69 6e 74 20 65  rderBy;..  int e
27919 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65 44  Dest = pDest->eD
2791a 65 73 74 3b 0a 20 20 69 6e 74 20 69 50 61 72 6d  est;.  int iParm
2791b 20 3d 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 3b   = pDest->iParm;
2791c 0a 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 3b 0a  ..  int regRow;.
2791d 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 0a    int regRowid;.
2791e 0a 20 20 69 54 61 62 20 3d 20 70 4f 72 64 65 72  .  iTab = pOrder
2791f 42 79 2d 3e 69 45 43 75 72 73 6f 72 3b 0a 20 20  By->iECursor;.  
27920 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  if( eDest==SRT_O
27921 75 74 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d  utput || eDest==
27922 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b  SRT_Coroutine ){
27923 0a 20 20 20 20 70 73 65 75 64 6f 54 61 62 20 3d  .    pseudoTab =
27924 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
27925 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27926 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65  AddOp3(v, OP_Ope
27927 6e 50 73 65 75 64 6f 2c 20 70 73 65 75 64 6f 54  nPseudo, pseudoT
27928 61 62 2c 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  ab, eDest==SRT_O
27929 75 74 70 75 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  utput, nColumn);
2792a 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 31 20  .  }.  addr = 1 
2792b 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  + sqlite3VdbeAdd
2792c 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 2c 20  Op2(v, OP_Sort, 
2792d 69 54 61 62 2c 20 61 64 64 72 42 72 65 61 6b 29  iTab, addrBreak)
2792e 3b 0a 20 20 63 6f 64 65 4f 66 66 73 65 74 28 76  ;.  codeOffset(v
2792f 2c 20 70 2c 20 61 64 64 72 43 6f 6e 74 69 6e 75  , p, addrContinu
27930 65 29 3b 0a 20 20 72 65 67 52 6f 77 20 3d 20 73  e);.  regRow = s
27931 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
27932 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 52  (pParse);.  regR
27933 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65  owid = sqlite3Ge
27934 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
27935 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
27936 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
27937 6d 6e 2c 20 69 54 61 62 2c 20 70 4f 72 64 65 72  mn, iTab, pOrder
27938 42 79 2d 3e 6e 45 78 70 72 20 2b 20 31 2c 20 72  By->nExpr + 1, r
27939 65 67 52 6f 77 29 3b 0a 20 20 73 77 69 74 63 68  egRow);.  switch
2793a 28 20 65 44 65 73 74 20 29 7b 0a 20 20 20 20 63  ( eDest ){.    c
2793b 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20  ase SRT_Table:. 
2793c 20 20 20 63 61 73 65 20 53 52 54 5f 45 70 68 65     case SRT_Ephe
2793d 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 74 65  mTab: {.      te
2793e 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53  stcase( eDest==S
2793f 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  RT_Table );.    
27940 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73    testcase( eDes
27941 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20  t==SRT_EphemTab 
27942 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27943 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27944 5f 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72 6d  _NewRowid, iParm
27945 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
27946 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27947 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
27948 74 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77  t, iParm, regRow
27949 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
2794a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2794b 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
2794c 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20  _APPEND);.      
2794d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66  break;.    }.#if
2794e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2794f 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61  _SUBQUERY.    ca
27950 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20  se SRT_Set: {.  
27951 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c      assert( nCol
27952 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
27953 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27954 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  4(v, OP_MakeReco
27955 72 64 2c 20 72 65 67 52 6f 77 2c 20 31 2c 20 72  rd, regRow, 1, r
27956 65 67 52 6f 77 69 64 2c 20 26 70 2d 3e 61 66 66  egRowid, &p->aff
27957 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20  inity, 1);.     
27958 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
27959 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
2795a 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c 20  pParse, regRow, 
2795b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2795c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2795d 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61  P_IdxInsert, iPa
2795e 72 6d 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  rm, regRowid);. 
2795f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27960 7d 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d  }.    case SRT_M
27961 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  em: {.      asse
27962 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29  rt( nColumn==1 )
27963 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
27964 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72  xprCodeMove(pPar
27965 73 65 2c 20 72 65 67 52 6f 77 2c 20 69 50 61 72  se, regRow, iPar
27966 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20  m, 1);.      /* 
27967 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  The LIMIT clause
27968 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20   will terminate 
27969 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20  the loop for us 
2796a 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  */.      break;.
2796b 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
2796c 20 63 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74   case SRT_Output
2796d 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 43  :.    case SRT_C
2796e 6f 72 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20  oroutine: {.    
2796f 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 74    int i;.      t
27970 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d  estcase( eDest==
27971 53 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20  SRT_Output );.  
27972 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
27973 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
27974 6e 65 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ne );.      sqli
27975 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27976 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
27977 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
27978 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27979 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2797a 20 70 73 65 75 64 6f 54 61 62 2c 20 72 65 67 52   pseudoTab, regR
2797b 6f 77 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  ow, regRowid);. 
2797c 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
2797d 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
2797e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
2797f 65 67 52 6f 77 21 3d 70 44 65 73 74 2d 3e 69 4d  egRow!=pDest->iM
27980 65 6d 2b 69 20 29 3b 0a 20 20 20 20 20 20 20 20  em+i );.        
27981 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27982 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
27983 70 73 65 75 64 6f 54 61 62 2c 20 69 2c 20 70 44  pseudoTab, i, pD
27984 65 73 74 2d 3e 69 4d 65 6d 2b 69 29 3b 0a 20 20  est->iMem+i);.  
27985 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
27986 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
27987 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
27988 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27989 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
2798a 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43 6f  pDest->iMem, nCo
2798b 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 73  lumn);.        s
2798c 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
2798d 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
2798e 61 72 73 65 2c 20 70 44 65 73 74 2d 3e 69 4d 65  arse, pDest->iMe
2798f 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  m, nColumn);.   
27990 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27991 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27992 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
27993 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a   pDest->iParm);.
27994 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
27995 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
27996 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
27997 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f  /* Do nothing */
27998 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
27999 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
2799a 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2799b 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 29 3b  pParse, regRow);
2799c 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2799d 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2799e 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20 2f   regRowid);..  /
2799f 2a 20 4c 49 4d 49 54 20 68 61 73 20 62 65 65 6e  * LIMIT has been
279a0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
279a1 74 68 65 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74  the pushOntoSort
279a2 65 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 20 20  er() routine..  
279a3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  */.  assert( p->
279a4 69 4c 69 6d 69 74 3d 3d 30 20 29 3b 0a 0a 20 20  iLimit==0 );..  
279a5 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66  /* The bottom of
279a6 20 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20   the loop.  */. 
279a7 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
279a8 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
279a9 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73 71 6c  Continue);.  sql
279aa 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
279ab 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c  , OP_Next, iTab,
279ac 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74 65   addr);.  sqlite
279ad 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
279ae 6c 28 76 2c 20 61 64 64 72 42 72 65 61 6b 29 3b  l(v, addrBreak);
279af 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52  .  if( eDest==SR
279b0 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44 65 73  T_Output || eDes
279b1 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  t==SRT_Coroutine
279b2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
279b3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
279b4 43 6c 6f 73 65 2c 20 70 73 65 75 64 6f 54 61 62  Close, pseudoTab
279b5 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  , 0);.  }.}../*.
279b6 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
279b7 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20  ter to a string 
279b8 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 27  containing the '
279b9 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
279ba 27 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78 70 72  ' of the.** expr
279bb 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 54 68  ession pExpr. Th
279bc 65 20 73 74 72 69 6e 67 20 6d 61 79 20 62 65 20  e string may be 
279bd 74 72 65 61 74 65 64 20 61 73 20 73 74 61 74 69  treated as stati
279be 63 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e  c by the caller.
279bf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 63 6c 61  .**.** The decla
279c0 72 61 74 69 6f 6e 20 74 79 70 65 20 69 73 20 74  ration type is t
279c1 68 65 20 65 78 61 63 74 20 64 61 74 61 74 79 70  he exact datatyp
279c2 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 65 78 74  e definition ext
279c3 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 0a  racted from the.
279c4 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 43 52 45 41  ** original CREA
279c5 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
279c6 6e 74 20 69 66 20 74 68 65 20 65 78 70 72 65 73  nt if the expres
279c7 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e  sion is a column
279c8 2e 20 54 68 65 0a 2a 2a 20 64 65 63 6c 61 72 61  . The.** declara
279c9 74 69 6f 6e 20 74 79 70 65 20 66 6f 72 20 61 20  tion type for a 
279ca 52 4f 57 49 44 20 66 69 65 6c 64 20 69 73 20 49  ROWID field is I
279cb 4e 54 45 47 45 52 2e 20 45 78 61 63 74 6c 79 20  NTEGER. Exactly 
279cc 77 68 65 6e 20 61 6e 20 65 78 70 72 65 73 73 69  when an expressi
279cd 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  on.** is conside
279ce 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20 63 61 6e  red a column can
279cf 20 62 65 20 63 6f 6d 70 6c 65 78 20 69 6e 20 74   be complex in t
279d0 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 73  he presence of s
279d1 75 62 71 75 65 72 69 65 73 2e 20 54 68 65 0a 2a  ubqueries. The.*
279d2 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70  * result-set exp
279d3 72 65 73 73 69 6f 6e 20 69 6e 20 61 6c 6c 20 6f  ression in all o
279d4 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
279d5 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
279d6 73 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65  s is .** conside
279d7 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20 62 79 20  red a column by 
279d8 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  this function..*
279d9 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 63 6f  *.**   SELECT co
279da 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a 2a 2a 20 20  l FROM tbl;.**  
279db 20 53 45 4c 45 43 54 20 28 53 45 4c 45 43 54 20   SELECT (SELECT 
279dc 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a 2a 2a  col FROM tbl;.**
279dd 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45 43     SELECT (SELEC
279de 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 29 3b  T col FROM tbl);
279df 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 61 62 63  .**   SELECT abc
279e0 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 63 6f   FROM (SELECT co
279e1 6c 20 41 53 20 61 62 63 20 46 52 4f 4d 20 74 62  l AS abc FROM tb
279e2 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 64  l);.** .** The d
279e3 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
279e4 66 6f 72 20 61 6e 79 20 65 78 70 72 65 73 73 69  for any expressi
279e5 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20  on other than a 
279e6 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 2e 0a  column is NULL..
279e7 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
279e8 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 54 79 70 65  char *columnType
279e9 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  (.  NameContext 
279ea 2a 70 4e 43 2c 20 0a 20 20 45 78 70 72 20 2a 70  *pNC, .  Expr *p
279eb 45 78 70 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Expr,.  const ch
279ec 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 44 62 2c  ar **pzOriginDb,
279ed 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
279ee 70 7a 4f 72 69 67 69 6e 54 61 62 2c 0a 20 20 63  pzOriginTab,.  c
279ef 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72  onst char **pzOr
279f0 69 67 69 6e 43 6f 6c 0a 29 7b 0a 20 20 63 68 61  iginCol.){.  cha
279f1 72 20 63 6f 6e 73 74 20 2a 7a 54 79 70 65 20 3d  r const *zType =
279f2 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   0;.  char const
279f3 20 2a 7a 4f 72 69 67 69 6e 44 62 20 3d 20 30 3b   *zOriginDb = 0;
279f4 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
279f5 4f 72 69 67 69 6e 54 61 62 20 3d 20 30 3b 0a 20  OriginTab = 0;. 
279f6 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 72   char const *zOr
279f7 69 67 69 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 69  iginCol = 0;.  i
279f8 6e 74 20 6a 3b 0a 20 20 69 66 28 20 70 45 78 70  nt j;.  if( pExp
279f9 72 3d 3d 30 20 7c 7c 20 70 4e 43 2d 3e 70 53 72  r==0 || pNC->pSr
279fa 63 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  cList==0 ) retur
279fb 6e 20 30 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  n 0;..  switch( 
279fc 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20  pExpr->op ){.   
279fd 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c   case TK_AGG_COL
279fe 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  UMN:.    case TK
279ff 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20  _COLUMN: {.     
27a00 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
27a01 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20  on is a column. 
27a02 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65  Locate the table
27a03 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 62   the column is b
27a04 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 65 78  eing.      ** ex
27a05 74 72 61 63 74 65 64 20 66 72 6f 6d 20 69 6e 20  tracted from in 
27a06 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72 63  NameContext.pSrc
27a07 4c 69 73 74 2e 20 54 68 69 73 20 74 61 62 6c 65  List. This table
27a08 20 6d 61 79 20 62 65 20 72 65 61 6c 0a 20 20 20   may be real.   
27a09 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 74     ** database t
27a0a 61 62 6c 65 20 6f 72 20 61 20 73 75 62 71 75 65  able or a subque
27a0b 72 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ry..      */.   
27a0c 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
27a0d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
27a0e 2a 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  * Table structur
27a0f 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72  e column is extr
27a10 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20  acted from */.  
27a11 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d      Select *pS =
27a12 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
27a13 2f 2a 20 53 65 6c 65 63 74 20 74 68 65 20 63 6f  /* Select the co
27a14 6c 75 6d 6e 20 69 73 20 65 78 74 72 61 63 74 65  lumn is extracte
27a15 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20  d from */.      
27a16 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 70 72  int iCol = pExpr
27a17 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 2f 2a 20 49  ->iColumn;  /* I
27a18 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69  ndex of column i
27a19 6e 20 70 54 61 62 20 2a 2f 0a 20 20 20 20 20 20  n pTab */.      
27a1a 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20 21 70  while( pNC && !p
27a1b 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 53  Tab ){.        S
27a1c 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
27a1d 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
27a1e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
27a1f 30 3b 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  0;j<pTabList->nS
27a20 72 63 20 26 26 20 70 54 61 62 4c 69 73 74 2d 3e  rc && pTabList->
27a21 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 21 3d 70 45  a[j].iCursor!=pE
27a22 78 70 72 2d 3e 69 54 61 62 6c 65 3b 6a 2b 2b 29  xpr->iTable;j++)
27a23 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3c  ;.        if( j<
27a24 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29  pTabList->nSrc )
27a25 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 61 62  {.          pTab
27a26 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a   = pTabList->a[j
27a27 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20  ].pTab;.        
27a28 20 20 70 53 20 3d 20 70 54 61 62 4c 69 73 74 2d    pS = pTabList-
27a29 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63 74 3b 0a 20  >a[j].pSelect;. 
27a2a 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
27a2b 20 20 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e          pNC = pN
27a2c 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  C->pNext;.      
27a2d 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
27a2e 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29     if( pTab==0 )
27a2f 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 49 58  {.        /* FIX
27a30 20 4d 45 3a 0a 20 20 20 20 20 20 20 20 2a 2a 20   ME:.        ** 
27a31 54 68 69 73 20 63 61 6e 20 6f 63 63 75 72 73 20  This can occurs 
27a32 69 66 20 79 6f 75 20 68 61 76 65 20 73 6f 6d 65  if you have some
27a33 74 68 69 6e 67 20 6c 69 6b 65 20 22 53 45 4c 45  thing like "SELE
27a34 43 54 20 6e 65 77 2e 78 3b 22 20 69 6e 73 69 64  CT new.x;" insid
27a35 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 74  e.        ** a t
27a36 72 69 67 67 65 72 2e 20 20 49 6e 20 6f 74 68 65  rigger.  In othe
27a37 72 20 77 6f 72 64 73 2c 20 69 66 20 79 6f 75 20  r words, if you 
27a38 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 73 70  reference the sp
27a39 65 63 69 61 6c 20 22 6e 65 77 22 0a 20 20 20 20  ecial "new".    
27a3a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 69 6e 20      ** table in 
27a3b 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
27a3c 66 20 61 20 73 65 6c 65 63 74 2e 20 20 57 65 20  f a select.  We 
27a3d 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 20 67 6f  do not have a go
27a3e 6f 64 20 77 61 79 0a 20 20 20 20 20 20 20 20 2a  od way.        *
27a3f 2a 20 74 6f 20 66 69 6e 64 20 74 68 65 20 61 63  * to find the ac
27a40 74 75 61 6c 20 74 61 62 6c 65 20 74 79 70 65 2c  tual table type,
27a41 20 73 6f 20 63 61 6c 6c 20 69 74 20 22 54 45 58   so call it "TEX
27a42 54 22 2e 20 20 54 68 69 73 20 69 73 20 72 65 61  T".  This is rea
27a43 6c 6c 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  lly.        ** s
27a44 6f 6d 65 74 68 69 6e 67 20 6f 66 20 61 20 62 75  omething of a bu
27a45 67 2c 20 62 75 74 20 49 20 64 6f 20 6e 6f 74 20  g, but I do not 
27a46 6b 6e 6f 77 20 68 6f 77 20 74 6f 20 66 69 78 20  know how to fix 
27a47 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  it..        **. 
27a48 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 63         ** This c
27a49 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f  ode does not pro
27a4a 64 75 63 65 20 74 68 65 20 63 6f 72 72 65 63 74  duce the correct
27a4b 20 61 6e 73 77 65 72 20 2d 20 69 74 20 6a 75 73   answer - it jus
27a4c 74 20 70 72 65 76 65 6e 74 73 0a 20 20 20 20 20  t prevents.     
27a4d 20 20 20 2a 2a 20 61 20 73 65 67 66 61 75 6c 74     ** a segfault
27a4e 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 31  .  See ticket #1
27a4f 32 32 39 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  229..        */.
27a50 20 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20          zType = 
27a51 22 54 45 58 54 22 3b 0a 20 20 20 20 20 20 20 20  "TEXT";.        
27a52 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a  break;.      }..
27a53 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
27a54 61 62 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ab );.      if( 
27a55 70 53 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  pS ){.        /*
27a56 20 54 68 65 20 22 74 61 62 6c 65 22 20 69 73 20   The "table" is 
27a57 61 63 74 75 61 6c 6c 79 20 61 20 73 75 62 2d 73  actually a sub-s
27a58 65 6c 65 63 74 20 6f 72 20 61 20 76 69 65 77 20  elect or a view 
27a59 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
27a5a 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66  se.        ** of
27a5b 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
27a5c 65 6d 65 6e 74 2e 20 52 65 74 75 72 6e 20 74 68  ement. Return th
27a5d 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79  e declaration ty
27a5e 70 65 20 61 6e 64 20 6f 72 69 67 69 6e 0a 20 20  pe and origin.  
27a5f 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20 66 6f        ** data fo
27a60 72 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74  r the result-set
27a61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 73   column of the s
27a62 75 62 2d 73 65 6c 65 63 74 2e 0a 20 20 20 20 20  ub-select..     
27a63 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66     */.        if
27a64 28 20 41 4c 57 41 59 53 28 69 43 6f 6c 3e 3d 30  ( ALWAYS(iCol>=0
27a65 20 26 26 20 69 43 6f 6c 3c 70 53 2d 3e 70 45 4c   && iCol<pS->pEL
27a66 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20  ist->nExpr) ){. 
27a67 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69           /* If i
27a68 43 6f 6c 20 69 73 20 6c 65 73 73 20 74 68 61 6e  Col is less than
27a69 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
27a6a 65 78 70 72 65 73 73 69 6f 6e 20 72 65 71 75 65  expression reque
27a6b 73 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20  sts the.        
27a6c 20 20 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68    ** rowid of th
27a6d 65 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20  e sub-select or 
27a6e 76 69 65 77 2e 20 54 68 69 73 20 65 78 70 72 65  view. This expre
27a6f 73 73 69 6f 6e 20 69 73 20 6c 65 67 61 6c 20 28  ssion is legal (
27a70 73 65 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a  see .          *
27a71 2a 20 74 65 73 74 20 63 61 73 65 20 6d 69 73 63  * test case misc
27a72 32 2e 32 2e 32 29 20 2d 20 69 74 20 61 6c 77 61  2.2.2) - it alwa
27a73 79 73 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20  ys evaluates to 
27a74 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 20 20  NULL..          
27a75 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 4e 61 6d  */.          Nam
27a76 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20  eContext sNC;.  
27a77 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 20          Expr *p 
27a78 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  = pS->pEList->a[
27a79 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 20  iCol].pExpr;.   
27a7a 20 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c         sNC.pSrcL
27a7b 69 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a  ist = pS->pSrc;.
27a7c 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e 70 4e            sNC.pN
27a7d 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ext = 0;.       
27a7e 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20     sNC.pParse = 
27a7f 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20  pNC->pParse;.   
27a80 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63         zType = c
27a81 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20  olumnType(&sNC, 
27a82 70 2c 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26  p, &zOriginDb, &
27a83 7a 4f 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72  zOriginTab, &zOr
27a84 69 67 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20  iginCol); .     
27a85 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
27a86 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62   if( ALWAYS(pTab
27a87 2d 3e 70 53 63 68 65 6d 61 29 20 29 7b 0a 20 20  ->pSchema) ){.  
27a88 20 20 20 20 20 20 2f 2a 20 41 20 72 65 61 6c 20        /* A real 
27a89 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  table */.       
27a8a 20 61 73 73 65 72 74 28 20 21 70 53 20 29 3b 0a   assert( !pS );.
27a8b 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
27a8c 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62  <0 ) iCol = pTab
27a8d 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20  ->iPKey;.       
27a8e 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d   assert( iCol==-
27a8f 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26  1 || (iCol>=0 &&
27a90 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c   iCol<pTab->nCol
27a91 29 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ) );.        if(
27a92 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20   iCol<0 ){.     
27a93 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 49 4e       zType = "IN
27a94 54 45 47 45 52 22 3b 0a 20 20 20 20 20 20 20 20  TEGER";.        
27a95 20 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 22    zOriginCol = "
27a96 72 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 20 20  rowid";.        
27a97 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
27a98 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61   zType = pTab->a
27a99 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b  Col[iCol].zType;
27a9a 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 67  .          zOrig
27a9b 69 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43  inCol = pTab->aC
27a9c 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a  ol[iCol].zName;.
27a9d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27a9e 20 20 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 70    zOriginTab = p
27a9f 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
27aa0 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 70 50 61      if( pNC->pPa
27aa1 72 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  rse ){.         
27aa2 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74   int iDb = sqlit
27aa3 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
27aa4 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c  pNC->pParse->db,
27aa5 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
27aa6 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 67  .          zOrig
27aa7 69 6e 44 62 20 3d 20 70 4e 43 2d 3e 70 50 61 72  inDb = pNC->pPar
27aa8 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d  se->db->aDb[iDb]
27aa9 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
27aaa 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
27aab 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66  break;.    }.#if
27aac 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27aad 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61  _SUBQUERY.    ca
27aae 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a  se TK_SELECT: {.
27aaf 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
27ab0 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62  ression is a sub
27ab1 2d 73 65 6c 65 63 74 2e 20 52 65 74 75 72 6e 20  -select. Return 
27ab2 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  the declaration 
27ab3 74 79 70 65 20 61 6e 64 0a 20 20 20 20 20 20 2a  type and.      *
27ab4 2a 20 6f 72 69 67 69 6e 20 69 6e 66 6f 20 66 6f  * origin info fo
27ab5 72 20 74 68 65 20 73 69 6e 67 6c 65 20 63 6f 6c  r the single col
27ab6 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c  umn in the resul
27ab7 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c  t set of the SEL
27ab8 45 43 54 0a 20 20 20 20 20 20 2a 2a 20 73 74 61  ECT.      ** sta
27ab9 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2f  tement..      */
27aba 0a 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65  .      NameConte
27abb 78 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 53 65  xt sNC;.      Se
27abc 6c 65 63 74 20 2a 70 53 20 3d 20 70 45 78 70 72  lect *pS = pExpr
27abd 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20  ->x.pSelect;.   
27abe 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70 53 2d     Expr *p = pS-
27abf 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
27ac0 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xpr;.      asser
27ac1 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  t( ExprHasProper
27ac2 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
27ac3 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
27ac4 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20   sNC.pSrcList = 
27ac5 70 53 2d 3e 70 53 72 63 3b 0a 20 20 20 20 20 20  pS->pSrc;.      
27ac6 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b  sNC.pNext = pNC;
27ac7 0a 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73  .      sNC.pPars
27ac8 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
27ac9 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63  .      zType = c
27aca 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20  olumnType(&sNC, 
27acb 70 2c 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26  p, &zOriginDb, &
27acc 7a 4f 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72  zOriginTab, &zOr
27acd 69 67 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20  iginCol); .     
27ace 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
27acf 6e 64 69 66 0a 20 20 7d 0a 20 20 0a 20 20 69 66  ndif.  }.  .  if
27ad0 28 20 70 7a 4f 72 69 67 69 6e 44 62 20 29 7b 0a  ( pzOriginDb ){.
27ad1 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 4f 72      assert( pzOr
27ad2 69 67 69 6e 54 61 62 20 26 26 20 70 7a 4f 72 69  iginTab && pzOri
27ad3 67 69 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 2a 70  ginCol );.    *p
27ad4 7a 4f 72 69 67 69 6e 44 62 20 3d 20 7a 4f 72 69  zOriginDb = zOri
27ad5 67 69 6e 44 62 3b 0a 20 20 20 20 2a 70 7a 4f 72  ginDb;.    *pzOr
27ad6 69 67 69 6e 54 61 62 20 3d 20 7a 4f 72 69 67 69  iginTab = zOrigi
27ad7 6e 54 61 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69  nTab;.    *pzOri
27ad8 67 69 6e 43 6f 6c 20 3d 20 7a 4f 72 69 67 69 6e  ginCol = zOrigin
27ad9 43 6f 6c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Col;.  }.  retur
27ada 6e 20 7a 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n zType;.}../*.*
27adb 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
27adc 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74  that will tell t
27add 68 65 20 56 44 42 45 20 74 68 65 20 64 65 63 6c  he VDBE the decl
27ade 61 72 61 74 69 6f 6e 20 74 79 70 65 73 20 6f 66  aration types of
27adf 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74   columns.** in t
27ae0 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a  he result set..*
27ae1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
27ae2 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65  nerateColumnType
27ae3 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  s(.  Parse *pPar
27ae4 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
27ae5 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
27ae6 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
27ae7 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  t,  /* List of t
27ae8 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c  ables */.  ExprL
27ae9 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f  ist *pEList    /
27aea 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65  * Expressions de
27aeb 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c  fining the resul
27aec 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 23 69 66 6e  t set */.){.#ifn
27aed 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27aee 44 45 43 4c 54 59 50 45 0a 20 20 56 64 62 65 20  DECLTYPE.  Vdbe 
27aef 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
27af0 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e  be;.  int i;.  N
27af1 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a  ameContext sNC;.
27af2 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
27af3 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 4e 43   pTabList;.  sNC
27af4 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
27af5 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
27af6 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  EList->nExpr; i+
27af7 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20  +){.    Expr *p 
27af8 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  = pEList->a[i].p
27af9 45 78 70 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Expr;.    const 
27afa 63 68 61 72 20 2a 7a 54 79 70 65 3b 0a 23 69 66  char *zType;.#if
27afb 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
27afc 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
27afd 41 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  A.    const char
27afe 20 2a 7a 4f 72 69 67 44 62 20 3d 20 30 3b 0a 20   *zOrigDb = 0;. 
27aff 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
27b00 4f 72 69 67 54 61 62 20 3d 20 30 3b 0a 20 20 20  OrigTab = 0;.   
27b01 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72   const char *zOr
27b02 69 67 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 7a  igCol = 0;.    z
27b03 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70  Type = columnTyp
27b04 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69  e(&sNC, p, &zOri
27b05 67 44 62 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20  gDb, &zOrigTab, 
27b06 26 7a 4f 72 69 67 43 6f 6c 29 3b 0a 0a 20 20 20  &zOrigCol);..   
27b07 20 2f 2a 20 54 68 65 20 76 64 62 65 20 6d 75 73   /* The vdbe mus
27b08 74 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 63  t make its own c
27b09 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  opy of the colum
27b0a 6e 2d 74 79 70 65 20 61 6e 64 20 6f 74 68 65 72  n-type and other
27b0b 20 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20   .    ** column 
27b0c 73 70 65 63 69 66 69 63 20 73 74 72 69 6e 67 73  specific strings
27b0d 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 63  , in case the sc
27b0e 68 65 6d 61 20 69 73 20 72 65 73 65 74 20 62 65  hema is reset be
27b0f 66 6f 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a  fore this.    **
27b10 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
27b11 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 20 20 20   is deleted..   
27b12 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
27b13 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
27b14 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41   i, COLNAME_DATA
27b15 42 41 53 45 2c 20 7a 4f 72 69 67 44 62 2c 20 53  BASE, zOrigDb, S
27b16 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
27b17 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27b18 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
27b19 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 2c  , COLNAME_TABLE,
27b1a 20 7a 4f 72 69 67 54 61 62 2c 20 53 51 4c 49 54   zOrigTab, SQLIT
27b1b 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
27b1c 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
27b1d 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
27b1e 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 2c 20 7a 4f  LNAME_COLUMN, zO
27b1f 72 69 67 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54  rigCol, SQLITE_T
27b20 52 41 4e 53 49 45 4e 54 29 3b 0a 23 65 6c 73 65  RANSIENT);.#else
27b21 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c  .    zType = col
27b22 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c  umnType(&sNC, p,
27b23 20 30 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69   0, 0, 0);.#endi
27b24 66 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  f.    sqlite3Vdb
27b25 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
27b26 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59  , COLNAME_DECLTY
27b27 50 45 2c 20 7a 54 79 70 65 2c 20 53 51 4c 49 54  PE, zType, SQLIT
27b28 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
27b29 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
27b2a 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45  TE_OMIT_DECLTYPE
27b2b 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   */.}../*.** Gen
27b2c 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
27b2d 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44  will tell the VD
27b2e 42 45 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  BE the names of 
27b2f 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68  columns.** in th
27b30 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54  e result set.  T
27b31 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
27b32 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 76 69  is used to provi
27b33 64 65 20 74 68 65 0a 2a 2a 20 61 7a 43 6f 6c 5b  de the.** azCol[
27b34 5d 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20  ] values in the 
27b35 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61  callback..*/.sta
27b36 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74  tic void generat
27b37 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 0a 20 20  eColumnNames(.  
27b38 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
27b39 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
27b3a 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
27b3b 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f  st *pTabList,  /
27b3c 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  * List of tables
27b3d 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
27b3e 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70  pEList    /* Exp
27b3f 72 65 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e  ressions definin
27b40 67 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  g the result set
27b41 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
27b42 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
27b43 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
27b44 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
27b45 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
27b46 66 75 6c 6c 4e 61 6d 65 73 2c 20 73 68 6f 72 74  fullNames, short
27b47 4e 61 6d 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20  Names;..#ifndef 
27b48 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
27b49 41 49 4e 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  AIN.  /* If this
27b4a 20 69 73 20 61 6e 20 45 58 50 4c 41 49 4e 2c 20   is an EXPLAIN, 
27b4b 73 6b 69 70 20 74 68 69 73 20 73 74 65 70 20 2a  skip this step *
27b4c 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  /.  if( pParse->
27b4d 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72  explain ){.    r
27b4e 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  eturn;.  }.#endi
27b4f 66 0a 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  f..  assert( v!=
27b50 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  0 );.  if( pPars
27b51 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 7c  e->colNamesSet |
27b52 7c 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 7c 7c  | NEVER(v==0) ||
27b53 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
27b54 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 50  d ) return;.  pP
27b55 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65  arse->colNamesSe
27b56 74 20 3d 20 31 3b 0a 20 20 66 75 6c 6c 4e 61 6d  t = 1;.  fullNam
27b57 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20  es = (db->flags 
27b58 26 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c  & SQLITE_FullCol
27b59 4e 61 6d 65 73 29 21 3d 30 3b 0a 20 20 73 68 6f  Names)!=0;.  sho
27b5a 72 74 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66  rtNames = (db->f
27b5b 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68  lags & SQLITE_Sh
27b5c 6f 72 74 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b  ortColNames)!=0;
27b5d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  .  sqlite3VdbeSe
27b5e 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 70 45 4c 69  tNumCols(v, pELi
27b5f 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 66 6f  st->nExpr);.  fo
27b60 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d  r(i=0; i<pEList-
27b61 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
27b62 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 20 20 70    Expr *p;.    p
27b63 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
27b64 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28 20 70  pExpr;.    if( p
27b65 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
27b66 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e      if( pEList->
27b67 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29 7b 0a 20 20  a[i].zName ){.  
27b68 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
27b69 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  = pEList->a[i].z
27b6a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  Name;.      sqli
27b6b 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
27b6c 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, i, COLNAME_
27b6d 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  NAME, zName, SQL
27b6e 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
27b6f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
27b70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
27b71 7c 7c 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47  || p->op==TK_AGG
27b72 5f 43 4f 4c 55 4d 4e 29 20 26 26 20 70 54 61 62  _COLUMN) && pTab
27b73 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 54 61  List ){.      Ta
27b74 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20  ble *pTab;.     
27b75 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 0a 20 20 20   char *zCol;.   
27b76 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d     int iCol = p-
27b77 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
27b78 66 6f 72 28 6a 3d 30 3b 20 41 4c 57 41 59 53 28  for(j=0; ALWAYS(
27b79 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  j<pTabList->nSrc
27b7a 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ); j++){.       
27b7b 20 69 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 61   if( pTabList->a
27b7c 5b 6a 5d 2e 69 43 75 72 73 6f 72 3d 3d 70 2d 3e  [j].iCursor==p->
27b7d 69 54 61 62 6c 65 20 29 20 62 72 65 61 6b 3b 0a  iTable ) break;.
27b7e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
27b7f 73 65 72 74 28 20 6a 3c 70 54 61 62 4c 69 73 74  sert( j<pTabList
27b80 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20  ->nSrc );.      
27b81 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d  pTab = pTabList-
27b82 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20  >a[j].pTab;.    
27b83 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 69    if( iCol<0 ) i
27b84 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65  Col = pTab->iPKe
27b85 79 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  y;.      assert(
27b86 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43   iCol==-1 || (iC
27b87 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54  ol>=0 && iCol<pT
27b88 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20  ab->nCol) );.   
27b89 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
27b8a 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20  .        zCol = 
27b8b 22 72 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 7d  "rowid";.      }
27b8c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 43  else{.        zC
27b8d 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ol = pTab->aCol[
27b8e 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  iCol].zName;.   
27b8f 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
27b90 73 68 6f 72 74 4e 61 6d 65 73 20 26 26 20 21 66  shortNames && !f
27b91 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20  ullNames ){.    
27b92 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
27b93 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
27b94 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20  COLNAME_NAME, . 
27b95 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27b96 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
27b97 28 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a  (char*)p->span.z
27b98 2c 20 70 2d 3e 73 70 61 6e 2e 6e 29 2c 20 53 51  , p->span.n), SQ
27b99 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
27b9a 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66       }else if( f
27b9b 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20  ullNames ){.    
27b9c 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
27b9d 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  = 0;.        zNa
27b9e 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
27b9f 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73 22 2c  ntf(db, "%s.%s",
27ba0 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
27ba1 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ol);.        sql
27ba2 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
27ba3 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45  me(v, i, COLNAME
27ba4 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53 51  _NAME, zName, SQ
27ba5 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
27ba6 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27ba7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
27ba8 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
27ba9 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 43  COLNAME_NAME, zC
27baa 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ol, SQLITE_TRANS
27bab 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20  IENT);.      }. 
27bac 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27bad 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
27bae 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
27baf 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20  AME_NAME, .     
27bb0 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 53 74       sqlite3DbSt
27bb1 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a  rNDup(db, (char*
27bb2 29 70 2d 3e 73 70 61 6e 2e 7a 2c 20 70 2d 3e 73  )p->span.z, p->s
27bb3 70 61 6e 2e 6e 29 2c 20 53 51 4c 49 54 45 5f 44  pan.n), SQLITE_D
27bb4 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a 20  YNAMIC);.    }. 
27bb5 20 7d 0a 20 20 67 65 6e 65 72 61 74 65 43 6f 6c   }.  generateCol
27bb6 75 6d 6e 54 79 70 65 73 28 70 50 61 72 73 65 2c  umnTypes(pParse,
27bb7 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 73   pTabList, pELis
27bb8 74 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  t);.}..#ifndef S
27bb9 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
27bba 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a  UND_SELECT./*.**
27bbb 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e   Name of the con
27bbc 6e 65 63 74 69 6f 6e 20 6f 70 65 72 61 74 6f 72  nection operator
27bbd 2c 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72  , used for error
27bbe 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2f 0a 73 74   messages..*/.st
27bbf 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
27bc0 2a 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 69 6e  *selectOpName(in
27bc1 74 20 69 64 29 7b 0a 20 20 63 68 61 72 20 2a 7a  t id){.  char *z
27bc2 3b 0a 20 20 73 77 69 74 63 68 28 20 69 64 20 29  ;.  switch( id )
27bc3 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4c  {.    case TK_AL
27bc4 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 55 4e  L:       z = "UN
27bc5 49 4f 4e 20 41 4c 4c 22 3b 20 20 20 62 72 65 61  ION ALL";   brea
27bc6 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  k;.    case TK_I
27bc7 4e 54 45 52 53 45 43 54 3a 20 7a 20 3d 20 22 49  NTERSECT: z = "I
27bc8 4e 54 45 52 53 45 43 54 22 3b 20 20 20 62 72 65  NTERSECT";   bre
27bc9 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ak;.    case TK_
27bca 45 58 43 45 50 54 3a 20 20 20 20 7a 20 3d 20 22  EXCEPT:    z = "
27bcb 45 58 43 45 50 54 22 3b 20 20 20 20 20 20 62 72  EXCEPT";      br
27bcc 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
27bcd 3a 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20  :           z = 
27bce 22 55 4e 49 4f 4e 22 3b 20 20 20 20 20 20 20 62  "UNION";       b
27bcf 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  reak;.  }.  retu
27bd0 72 6e 20 7a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn z;.}.#endif /
27bd1 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  * SQLITE_OMIT_CO
27bd2 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f  MPOUND_SELECT */
27bd3 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20  ../*.** Given a 
27bd4 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  an expression li
27bd5 73 74 20 28 77 68 69 63 68 20 69 73 20 72 65 61  st (which is rea
27bd6 6c 6c 79 20 74 68 65 20 6c 69 73 74 20 6f 66 20  lly the list of 
27bd7 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74  expressions.** t
27bd8 68 61 74 20 66 6f 72 6d 20 74 68 65 20 72 65 73  hat form the res
27bd9 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c  ult set of a SEL
27bda 45 43 54 20 73 74 61 74 65 6d 65 6e 74 29 20 63  ECT statement) c
27bdb 6f 6d 70 75 74 65 20 61 70 70 72 6f 70 72 69 61  ompute appropria
27bdc 74 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d  te.** column nam
27bdd 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 20 74  es for a table t
27bde 68 61 74 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74  hat would hold t
27bdf 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
27be0 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 63 6f  st..**.** All co
27be1 6c 75 6d 6e 20 6e 61 6d 65 73 20 77 69 6c 6c 20  lumn names will 
27be2 62 65 20 75 6e 69 71 75 65 2e 0a 2a 2a 0a 2a 2a  be unique..**.**
27be3 20 4f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e   Only the column
27be4 20 6e 61 6d 65 73 20 61 72 65 20 63 6f 6d 70 75   names are compu
27be5 74 65 64 2e 20 20 43 6f 6c 75 6d 6e 2e 7a 54 79  ted.  Column.zTy
27be6 70 65 2c 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c  pe, Column.zColl
27be7 2c 0a 2a 2a 20 61 6e 64 20 6f 74 68 65 72 20 66  ,.** and other f
27be8 69 65 6c 64 73 20 6f 66 20 43 6f 6c 75 6d 6e 20  ields of Column 
27be9 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a  are zeroed..**.*
27bea 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
27beb 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  OK on success.  
27bec 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
27bed 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
27bee 75 72 73 2c 0a 2a 2a 20 73 74 6f 72 65 20 4e 55  urs,.** store NU
27bef 4c 4c 20 69 6e 20 2a 70 61 43 6f 6c 20 61 6e 64  LL in *paCol and
27bf0 20 30 20 69 6e 20 2a 70 6e 43 6f 6c 20 61 6e 64   0 in *pnCol and
27bf1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
27bf2 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  OMEM..*/.static 
27bf3 69 6e 74 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e  int selectColumn
27bf4 73 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 0a 20  sFromExprList(. 
27bf5 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
27bf6 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
27bf7 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
27bf8 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
27bf9 74 2c 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72  t,       /* Expr
27bfa 20 6c 69 73 74 20 66 72 6f 6d 20 77 68 69 63 68   list from which
27bfb 20 74 6f 20 64 65 72 69 76 65 20 63 6f 6c 75 6d   to derive colum
27bfc 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74  n names */.  int
27bfd 20 2a 70 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20   *pnCol,        
27bfe 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
27bff 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
27c00 6d 6e 73 20 68 65 72 65 20 2a 2f 0a 20 20 43 6f  mns here */.  Co
27c01 6c 75 6d 6e 20 2a 2a 70 61 43 6f 6c 20 20 20 20  lumn **paCol    
27c02 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
27c03 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6c 69  he new column li
27c04 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  st here */.){.  
27c05 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
27c06 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 44  arse->db;   /* D
27c07 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
27c08 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a  on */.  int i, j
27c09 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
27c0a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
27c0b 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e  ters */.  int cn
27c0c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
27c0d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 61        /* Index a
27c0e 64 64 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65  dded to make the
27c0f 20 6e 61 6d 65 20 75 6e 69 71 75 65 20 2a 2f 0a   name unique */.
27c10 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20    Column *aCol, 
27c11 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a  *pCol;        /*
27c12 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
27c13 72 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  r result columns
27c14 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20   */.  int nCol; 
27c15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27c16 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
27c17 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65  olumns in the re
27c18 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 78  sult set */.  Ex
27c19 70 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  pr *p;          
27c1a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
27c1b 72 65 73 73 69 6f 6e 20 66 6f 72 20 61 20 73 69  ression for a si
27c1c 6e 67 6c 65 20 72 65 73 75 6c 74 20 63 6f 6c 75  ngle result colu
27c1d 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  mn */.  char *zN
27c1e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
27c1f 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61      /* Column na
27c20 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d  me */.  int nNam
27c21 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
27c22 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6e      /* Size of n
27c23 61 6d 65 20 69 6e 20 7a 4e 61 6d 65 5b 5d 20 2a  ame in zName[] *
27c24 2f 0a 0a 20 20 2a 70 6e 43 6f 6c 20 3d 20 6e 43  /..  *pnCol = nC
27c25 6f 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78  ol = pEList->nEx
27c26 70 72 3b 0a 20 20 61 43 6f 6c 20 3d 20 2a 70 61  pr;.  aCol = *pa
27c27 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Col = sqlite3DbM
27c28 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
27c29 7a 65 6f 66 28 61 43 6f 6c 5b 30 5d 29 2a 6e 43  zeof(aCol[0])*nC
27c2a 6f 6c 29 3b 0a 20 20 69 66 28 20 61 43 6f 6c 3d  ol);.  if( aCol=
27c2b 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
27c2c 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 66 6f 72 28  TE_NOMEM;.  for(
27c2d 69 3d 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20  i=0, pCol=aCol; 
27c2e 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  i<nCol; i++, pCo
27c2f 6c 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 47 65 74  l++){.    /* Get
27c30 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
27c31 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c  name for the col
27c32 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  umn.    */.    p
27c33 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
27c34 70 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72  pExpr;.    asser
27c35 74 28 20 70 2d 3e 70 52 69 67 68 74 3d 3d 30 20  t( p->pRight==0 
27c36 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 74 6f  || p->pRight->to
27c37 6b 65 6e 2e 7a 3d 3d 30 20 7c 7c 20 70 2d 3e 70  ken.z==0 || p->p
27c38 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30  Right->token.z[0
27c39 5d 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  ]!=0 );.    if( 
27c3a 28 7a 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d  (zName = pEList-
27c3b 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 21 3d 30 20  >a[i].zName)!=0 
27c3c 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ){.      /* If t
27c3d 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69  he column contai
27c3e 6e 73 20 61 6e 20 22 41 53 20 3c 6e 61 6d 65 3e  ns an "AS <name>
27c3f 22 20 70 68 72 61 73 65 2c 20 75 73 65 20 3c 6e  " phrase, use <n
27c40 61 6d 65 3e 20 61 73 20 74 68 65 20 6e 61 6d 65  ame> as the name
27c41 20 2a 2f 0a 20 20 20 20 20 20 7a 4e 61 6d 65 20   */.      zName 
27c42 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
27c43 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  p(db, zName);.  
27c44 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 45    }else{.      E
27c45 78 70 72 20 2a 70 43 6f 6c 45 78 70 72 20 3d 20  xpr *pColExpr = 
27c46 70 3b 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65  p;  /* The expre
27c47 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 74 68  ssion that is th
27c48 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20  e result column 
27c49 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 54 61  name */.      Ta
27c4a 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
27c4b 20 20 20 2f 2a 20 54 61 62 6c 65 20 61 73 73 6f     /* Table asso
27c4c 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
27c4d 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
27c4e 20 20 20 20 20 77 68 69 6c 65 28 20 70 43 6f 6c       while( pCol
27c4f 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54  Expr->op==TK_DOT
27c50 20 29 20 70 43 6f 6c 45 78 70 72 20 3d 20 70 43   ) pColExpr = pC
27c51 6f 6c 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a  olExpr->pRight;.
27c52 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 45 78        if( pColEx
27c53 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  pr->op==TK_COLUM
27c54 4e 20 26 26 20 28 70 54 61 62 20 3d 20 70 43 6f  N && (pTab = pCo
27c55 6c 45 78 70 72 2d 3e 70 54 61 62 29 21 3d 30 20  lExpr->pTab)!=0 
27c56 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  ){.        /* Fo
27c57 72 20 63 6f 6c 75 6d 6e 73 20 75 73 65 20 74 68  r columns use th
27c58 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 6e 61  e column name na
27c59 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  me */.        in
27c5a 74 20 69 43 6f 6c 20 3d 20 70 43 6f 6c 45 78 70  t iCol = pColExp
27c5b 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  r->iColumn;.    
27c5c 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29      if( iCol<0 )
27c5d 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50   iCol = pTab->iP
27c5e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61  Key;.        zNa
27c5f 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
27c60 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 0a 20 20  ntf(db, "%s",.  
27c61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
27c62 43 6f 6c 3e 3d 30 20 3f 20 70 54 61 62 2d 3e 61  Col>=0 ? pTab->a
27c63 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 20  Col[iCol].zName 
27c64 3a 20 22 72 6f 77 69 64 22 29 3b 0a 20 20 20 20  : "rowid");.    
27c65 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
27c66 20 2f 2a 20 55 73 65 20 74 68 65 20 6f 72 69 67   /* Use the orig
27c67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65  inal text of the
27c68 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65 73 73 69   column expressi
27c69 6f 6e 20 61 73 20 69 74 73 20 6e 61 6d 65 20 2a  on as its name *
27c6a 2f 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20  /.        Token 
27c6b 2a 70 54 6f 6b 65 6e 20 3d 20 28 70 43 6f 6c 45  *pToken = (pColE
27c6c 78 70 72 2d 3e 73 70 61 6e 2e 7a 3f 26 70 43 6f  xpr->span.z?&pCo
27c6d 6c 45 78 70 72 2d 3e 73 70 61 6e 3a 26 70 43 6f  lExpr->span:&pCo
27c6e 6c 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20  lExpr->token);. 
27c6f 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73         zName = s
27c70 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
27c71 2c 20 22 25 54 22 2c 20 70 54 6f 6b 65 6e 29 3b  , "%T", pToken);
27c72 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
27c73 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
27c74 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
27c75 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
27c76 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b, zName);.     
27c77 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
27c78 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
27c79 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  the column name 
27c7a 69 73 20 75 6e 69 71 75 65 2e 20 20 49 66 20 74  is unique.  If t
27c7b 68 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 75  he name is not u
27c7c 6e 69 71 75 65 2c 0a 20 20 20 20 2a 2a 20 61 70  nique,.    ** ap
27c7d 70 65 6e 64 20 61 20 69 6e 74 65 67 65 72 20 74  pend a integer t
27c7e 6f 20 74 68 65 20 6e 61 6d 65 20 73 6f 20 74 68  o the name so th
27c7f 61 74 20 69 74 20 62 65 63 6f 6d 65 73 20 75 6e  at it becomes un
27c80 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ique..    */.   
27c81 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
27c82 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
27c83 0a 20 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30  .    for(j=cnt=0
27c84 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ; j<i; j++){.   
27c85 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
27c86 72 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e  rICmp(aCol[j].zN
27c87 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29  ame, zName)==0 )
27c88 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
27c89 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 20 20 20  zNewName;.      
27c8a 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d    zName[nName] =
27c8b 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65 77   0;.        zNew
27c8c 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
27c8d 72 69 6e 74 66 28 64 62 2c 20 22 25 73 3a 25 64  rintf(db, "%s:%d
27c8e 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29  ", zName, ++cnt)
27c8f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
27c90 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
27c91 65 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d  e);.        zNam
27c92 65 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20  e = zNewName;.  
27c93 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20        j = -1;.  
27c94 20 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d        if( zName=
27c95 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
27c96 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43    }.    }.    pC
27c97 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d  ol->zName = zNam
27c98 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  e;.  }.  if( db-
27c99 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
27c9a 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
27c9b 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73  i; j++){.      s
27c9c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
27c9d 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3b   aCol[j].zName);
27c9e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
27c9f 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 43 6f  e3DbFree(db, aCo
27ca0 6c 29 3b 0a 20 20 20 20 2a 70 61 43 6f 6c 20 3d  l);.    *paCol =
27ca1 20 30 3b 0a 20 20 20 20 2a 70 6e 43 6f 6c 20 3d   0;.    *pnCol =
27ca2 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0;.    return S
27ca3 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
27ca4 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
27ca5 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  _OK;.}../*.** Ad
27ca6 64 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61  d type and colla
27ca7 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tion information
27ca8 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 6c 69 73   to a column lis
27ca9 74 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 61 20  t based on.** a 
27caa 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
27cab 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 6c  ..** .** The col
27cac 75 6d 6e 20 6c 69 73 74 20 70 72 65 73 75 6d 61  umn list presuma
27cad 62 6c 79 20 63 61 6d 65 20 66 72 6f 6d 20 73 65  bly came from se
27cae 6c 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 46  lectColumnNamesF
27caf 72 6f 6d 45 78 70 72 4c 69 73 74 28 29 2e 0a 2a  romExprList()..*
27cb0 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73  * The column lis
27cb1 74 20 68 61 73 20 6f 6e 6c 79 20 6e 61 6d 65 73  t has only names
27cb2 2c 20 6e 6f 74 20 74 79 70 65 73 20 6f 72 20 63  , not types or c
27cb3 6f 6c 6c 61 74 69 6f 6e 73 2e 20 20 54 68 69 73  ollations.  This
27cb4 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 6f 65 73  .** routine goes
27cb5 20 74 68 72 6f 75 67 68 20 61 6e 64 20 61 64 64   through and add
27cb6 73 20 74 68 65 20 74 79 70 65 73 20 61 6e 64 20  s the types and 
27cb7 63 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  collations..**.*
27cb8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
27cb9 65 71 75 69 72 65 73 20 74 68 61 74 20 61 6c 6c  equires that all
27cba 20 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20   identifiers in 
27cbb 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74  the SELECT.** st
27cbc 61 74 65 6d 65 6e 74 20 62 65 20 72 65 73 6f 6c  atement be resol
27cbd 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ved..*/.static v
27cbe 6f 69 64 20 73 65 6c 65 63 74 41 64 64 43 6f 6c  oid selectAddCol
27cbf 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74  umnTypeAndCollat
27cc0 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
27cc1 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
27cc2 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 73  Parsing contexts
27cc3 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20   */.  int nCol, 
27cc4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
27cc5 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
27cc6 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43   */.  Column *aC
27cc7 6f 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ol,         /* L
27cc8 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a  ist of columns *
27cc9 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
27cca 65 63 74 20 20 20 20 20 20 20 2f 2a 20 53 45 4c  ect       /* SEL
27ccb 45 43 54 20 75 73 65 64 20 74 6f 20 64 65 74 65  ECT used to dete
27ccc 72 6d 69 6e 65 20 74 79 70 65 73 20 61 6e 64 20  rmine types and 
27ccd 63 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f 0a 29 7b  collations */.){
27cce 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
27ccf 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 4e   pParse->db;.  N
27cd0 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a  ameContext sNC;.
27cd1 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
27cd2 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
27cd3 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78 70  ;.  int i;.  Exp
27cd4 72 20 2a 70 3b 0a 20 20 73 74 72 75 63 74 20 45  r *p;.  struct E
27cd5 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 3b  xprList_item *a;
27cd6 0a 0a 20 20 61 73 73 65 72 74 28 20 70 53 65 6c  ..  assert( pSel
27cd7 65 63 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ect!=0 );.  asse
27cd8 72 74 28 20 28 70 53 65 6c 65 63 74 2d 3e 73 65  rt( (pSelect->se
27cd9 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f  lFlags & SF_Reso
27cda 6c 76 65 64 29 21 3d 30 20 29 3b 0a 20 20 61 73  lved)!=0 );.  as
27cdb 73 65 72 74 28 20 6e 43 6f 6c 3d 3d 70 53 65 6c  sert( nCol==pSel
27cdc 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  ect->pEList->nEx
27cdd 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  pr || db->malloc
27cde 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
27cdf 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
27ce0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d   ) return;.  mem
27ce1 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
27ce2 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
27ce3 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c  .pSrcList = pSel
27ce4 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 61 20 3d  ect->pSrc;.  a =
27ce5 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
27ce6 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  ->a;.  for(i=0, 
27ce7 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f  pCol=aCol; i<nCo
27ce8 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; i++, pCol++){
27ce9 0a 20 20 20 20 70 20 3d 20 61 5b 69 5d 2e 70 45  .    p = a[i].pE
27cea 78 70 72 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a  xpr;.    pCol->z
27ceb 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Type = sqlite3Db
27cec 53 74 72 44 75 70 28 64 62 2c 20 63 6f 6c 75 6d  StrDup(db, colum
27ced 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 30  nType(&sNC, p, 0
27cee 2c 20 30 2c 20 30 29 29 3b 0a 20 20 20 20 70 43  , 0, 0));.    pC
27cef 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73  ol->affinity = s
27cf0 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
27cf1 74 79 28 70 29 3b 0a 20 20 20 20 70 43 6f 6c 6c  ty(p);.    pColl
27cf2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
27cf3 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 29  llSeq(pParse, p)
27cf4 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ;.    if( pColl 
27cf5 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a  ){.      pCol->z
27cf6 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  Coll = sqlite3Db
27cf7 53 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 6c  StrDup(db, pColl
27cf8 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ->zName);.    }.
27cf9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76    }.}../*.** Giv
27cfa 65 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  en a SELECT stat
27cfb 65 6d 65 6e 74 2c 20 67 65 6e 65 72 61 74 65 20  ement, generate 
27cfc 61 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  a Table structur
27cfd 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
27cfe 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73  .** the result s
27cff 65 74 20 6f 66 20 74 68 61 74 20 53 45 4c 45 43  et of that SELEC
27d00 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  T..*/.SQLITE_PRI
27d01 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
27d02 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65  te3ResultSetOfSe
27d03 6c 65 63 74 28 50 61 72 73 65 20 2a 70 50 61 72  lect(Parse *pPar
27d04 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  se, Select *pSel
27d05 65 63 74 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ect){.  Table *p
27d06 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Tab;.  sqlite3 *
27d07 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
27d08 0a 20 20 69 6e 74 20 73 61 76 65 64 46 6c 61 67  .  int savedFlag
27d09 73 3b 0a 0a 20 20 73 61 76 65 64 46 6c 61 67 73  s;..  savedFlags
27d0a 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20   = db->flags;.  
27d0b 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51  db->flags &= ~SQ
27d0c 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65  LITE_FullColName
27d0d 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  s;.  db->flags |
27d0e 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f  = SQLITE_ShortCo
27d0f 6c 4e 61 6d 65 73 3b 0a 20 20 73 71 6c 69 74 65  lNames;.  sqlite
27d10 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 72  3SelectPrep(pPar
27d11 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 30 29 3b  se, pSelect, 0);
27d12 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
27d13 45 72 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  Err ) return 0;.
27d14 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74    while( pSelect
27d15 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c 65  ->pPrior ) pSele
27d16 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50  ct = pSelect->pP
27d17 72 69 6f 72 3b 0a 20 20 64 62 2d 3e 66 6c 61 67  rior;.  db->flag
27d18 73 20 3d 20 73 61 76 65 64 46 6c 61 67 73 3b 0a  s = savedFlags;.
27d19 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
27d1a 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
27d1b 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 20 29   sizeof(Table) )
27d1c 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
27d1d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
27d1e 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e 64 62 4d  .  }.  pTab->dbM
27d1f 65 6d 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  em = db->lookasi
27d20 64 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 64 62  de.bEnabled ? db
27d21 20 3a 20 30 3b 0a 20 20 70 54 61 62 2d 3e 6e 52   : 0;.  pTab->nR
27d22 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62 2d 3e  ef = 1;.  pTab->
27d23 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 73 65 6c  zName = 0;.  sel
27d24 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78  ectColumnsFromEx
27d25 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
27d26 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20  Select->pEList, 
27d27 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70 54  &pTab->nCol, &pT
27d28 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 73 65 6c  ab->aCol);.  sel
27d29 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  ectAddColumnType
27d2a 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61  AndCollation(pPa
27d2b 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c  rse, pTab->nCol,
27d2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65   pTab->aCol, pSe
27d2d 6c 65 63 74 29 3b 0a 20 20 70 54 61 62 2d 3e 69  lect);.  pTab->i
27d2e 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 69 66 28  PKey = -1;.  if(
27d2f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
27d30 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
27d31 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62  DeleteTable(pTab
27d32 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
27d33 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54  .  }.  return pT
27d34 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  ab;.}../*.** Get
27d35 20 61 20 56 44 42 45 20 66 6f 72 20 74 68 65 20   a VDBE for the 
27d36 67 69 76 65 6e 20 70 61 72 73 65 72 20 63 6f 6e  given parser con
27d37 74 65 78 74 2e 20 20 43 72 65 61 74 65 20 61 20  text.  Create a 
27d38 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73  new one if neces
27d39 73 61 72 79 2e 0a 2a 2a 20 49 66 20 61 6e 20 65  sary..** If an e
27d3a 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74  rror occurs, ret
27d3b 75 72 6e 20 4e 55 4c 4c 20 61 6e 64 20 6c 65 61  urn NULL and lea
27d3c 76 65 20 61 20 6d 65 73 73 61 67 65 20 69 6e 20  ve a message in 
27d3d 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  pParse..*/.SQLIT
27d3e 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a  E_PRIVATE Vdbe *
27d3f 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 50  sqlite3GetVdbe(P
27d40 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
27d41 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
27d42 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 66 28 20  e->pVdbe;.  if( 
27d43 76 3d 3d 30 20 29 7b 0a 20 20 20 20 76 20 3d 20  v==0 ){.    v = 
27d44 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20  pParse->pVdbe = 
27d45 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74  sqlite3VdbeCreat
27d46 65 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 23  e(pParse->db);.#
27d47 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27d48 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28  IT_TRACE.    if(
27d49 20 76 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69   v ){.      sqli
27d4a 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
27d4b 20 4f 50 5f 54 72 61 63 65 29 3b 0a 20 20 20 20   OP_Trace);.    
27d4c 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  }.#endif.  }.  r
27d4d 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn v;.}.../*.
27d4e 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 69  ** Compute the i
27d4f 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
27d50 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  t fields of the 
27d51 53 45 4c 45 43 54 20 62 61 73 65 64 20 6f 6e 20  SELECT based on 
27d52 74 68 65 0a 2a 2a 20 70 4c 69 6d 69 74 20 61 6e  the.** pLimit an
27d53 64 20 70 4f 66 66 73 65 74 20 65 78 70 72 65 73  d pOffset expres
27d54 73 69 6f 6e 73 2e 20 20 70 4c 69 6d 69 74 20 61  sions.  pLimit a
27d55 6e 64 20 70 4f 66 66 73 65 74 20 68 6f 6c 64 20  nd pOffset hold 
27d56 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a  the expressions.
27d57 2a 2a 20 74 68 61 74 20 61 70 70 65 61 72 20 69  ** that appear i
27d58 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  n the original S
27d59 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74  QL statement aft
27d5a 65 72 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64  er the LIMIT and
27d5b 20 4f 46 46 53 45 54 0a 2a 2a 20 6b 65 79 77 6f   OFFSET.** keywo
27d5c 72 64 73 2e 20 20 4f 72 20 4e 55 4c 4c 20 69 66  rds.  Or NULL if
27d5d 20 74 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20   those keywords 
27d5e 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 69 4c 69  are omitted. iLi
27d5f 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20  mit and iOffset 
27d60 0a 2a 2a 20 61 72 65 20 74 68 65 20 69 6e 74 65  .** are the inte
27d61 67 65 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73  ger memory regis
27d62 74 65 72 20 6e 75 6d 62 65 72 73 20 66 6f 72 20  ter numbers for 
27d63 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20 74 6f  counters used to
27d64 20 63 6f 6d 70 75 74 65 20 0a 2a 2a 20 74 68 65   compute .** the
27d65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65   limit and offse
27d66 74 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  t.  If there is 
27d67 6e 6f 20 6c 69 6d 69 74 20 61 6e 64 2f 6f 72 20  no limit and/or 
27d68 6f 66 66 73 65 74 2c 20 74 68 65 6e 20 0a 2a 2a  offset, then .**
27d69 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66   iLimit and iOff
27d6a 73 65 74 20 61 72 65 20 6e 65 67 61 74 69 76 65  set are negative
27d6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
27d6c 74 69 6e 65 20 63 68 61 6e 67 65 73 20 74 68 65  tine changes the
27d6d 20 76 61 6c 75 65 73 20 6f 66 20 69 4c 69 6d 69   values of iLimi
27d6e 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 6f 6e  t and iOffset on
27d6f 6c 79 20 69 66 0a 2a 2a 20 61 20 6c 69 6d 69 74  ly if.** a limit
27d70 20 6f 72 20 6f 66 66 73 65 74 20 69 73 20 64 65   or offset is de
27d71 66 69 6e 65 64 20 62 79 20 70 4c 69 6d 69 74 20  fined by pLimit 
27d72 61 6e 64 20 70 4f 66 66 73 65 74 2e 20 20 69 4c  and pOffset.  iL
27d73 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 69 4f 66 66  imit and.** iOff
27d74 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20  set should have 
27d75 62 65 65 6e 20 70 72 65 73 65 74 20 74 6f 20 61  been preset to a
27d76 70 70 72 6f 70 72 69 61 74 65 20 64 65 66 61 75  ppropriate defau
27d77 6c 74 20 76 61 6c 75 65 73 0a 2a 2a 20 28 75 73  lt values.** (us
27d78 75 61 6c 6c 79 20 62 75 74 20 6e 6f 74 20 61 6c  ually but not al
27d79 77 61 79 73 20 2d 31 29 20 70 72 69 6f 72 20 74  ways -1) prior t
27d7a 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  o calling this r
27d7b 6f 75 74 69 6e 65 2e 0a 2a 2a 20 4f 6e 6c 79 20  outine..** Only 
27d7c 69 66 20 70 4c 69 6d 69 74 21 3d 30 20 6f 72 20  if pLimit!=0 or 
27d7d 70 4f 66 66 73 65 74 21 3d 30 20 64 6f 20 74 68  pOffset!=0 do th
27d7e 65 20 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72  e limit register
27d7f 73 20 67 65 74 0a 2a 2a 20 72 65 64 65 66 69 6e  s get.** redefin
27d80 65 64 2e 20 20 54 68 65 20 55 4e 49 4f 4e 20 41  ed.  The UNION A
27d81 4c 4c 20 6f 70 65 72 61 74 6f 72 20 75 73 65 73  LL operator uses
27d82 20 74 68 69 73 20 70 72 6f 70 65 72 74 79 20 74   this property t
27d83 6f 20 66 6f 72 63 65 0a 2a 2a 20 74 68 65 20 72  o force.** the r
27d84 65 75 73 65 20 6f 66 20 74 68 65 20 73 61 6d 65  euse of the same
27d85 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65   limit and offse
27d86 74 20 72 65 67 69 73 74 65 72 73 20 61 63 72 6f  t registers acro
27d87 73 73 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 53  ss multiple.** S
27d88 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
27d89 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
27d8a 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
27d8b 69 73 74 65 72 73 28 50 61 72 73 65 20 2a 70 50  isters(Parse *pP
27d8c 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c  arse, Select *p,
27d8d 20 69 6e 74 20 69 42 72 65 61 6b 29 7b 0a 20 20   int iBreak){.  
27d8e 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69  Vdbe *v = 0;.  i
27d8f 6e 74 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  nt iLimit = 0;. 
27d90 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a 20 20   int iOffset;.  
27d91 69 6e 74 20 61 64 64 72 31 3b 0a 20 20 69 66 28  int addr1;.  if(
27d92 20 70 2d 3e 69 4c 69 6d 69 74 20 29 20 72 65 74   p->iLimit ) ret
27d93 75 72 6e 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a  urn;..  /* .  **
27d94 20 22 4c 49 4d 49 54 20 2d 31 22 20 61 6c 77 61   "LIMIT -1" alwa
27d95 79 73 20 73 68 6f 77 73 20 61 6c 6c 20 72 6f 77  ys shows all row
27d96 73 2e 20 20 54 68 65 72 65 20 69 73 20 73 6f 6d  s.  There is som
27d97 65 0a 20 20 2a 2a 20 63 6f 6e 74 72 61 76 65 72  e.  ** contraver
27d98 73 79 20 61 62 6f 75 74 20 77 68 61 74 20 74 68  sy about what th
27d99 65 20 63 6f 72 72 65 63 74 20 62 65 68 61 76 69  e correct behavi
27d9a 6f 72 20 73 68 6f 75 6c 64 20 62 65 2e 0a 20 20  or should be..  
27d9b 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 69  ** The current i
27d9c 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e  mplementation in
27d9d 74 65 72 70 72 65 74 73 20 22 4c 49 4d 49 54 20  terprets "LIMIT 
27d9e 30 22 20 74 6f 20 6d 65 61 6e 0a 20 20 2a 2a 20  0" to mean.  ** 
27d9f 6e 6f 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20  no rows..  */.  
27da0 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
27da1 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20  Clear(pParse);. 
27da2 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29   if( p->pLimit )
27da3 7b 0a 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20  {.    p->iLimit 
27da4 3d 20 69 4c 69 6d 69 74 20 3d 20 2b 2b 70 50 61  = iLimit = ++pPa
27da5 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 76  rse->nMem;.    v
27da6 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
27da7 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  e(pParse);.    i
27da8 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
27da9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
27daa 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d  rCode(pParse, p-
27dab 3e 70 4c 69 6d 69 74 2c 20 69 4c 69 6d 69 74 29  >pLimit, iLimit)
27dac 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27dad 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75  eAddOp1(v, OP_Mu
27dae 73 74 42 65 49 6e 74 2c 20 69 4c 69 6d 69 74 29  stBeInt, iLimit)
27daf 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
27db0 74 28 28 76 2c 20 22 4c 49 4d 49 54 20 63 6f 75  t((v, "LIMIT cou
27db1 6e 74 65 72 22 29 29 3b 0a 20 20 20 20 73 71 6c  nter"));.    sql
27db2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27db3 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69  , OP_IfZero, iLi
27db4 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20  mit, iBreak);.  
27db5 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 66 66 73  }.  if( p->pOffs
27db6 65 74 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4f 66  et ){.    p->iOf
27db7 66 73 65 74 20 3d 20 69 4f 66 66 73 65 74 20 3d  fset = iOffset =
27db8 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
27db9 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d  .    if( p->pLim
27dba 69 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  it ){.      pPar
27dbb 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20 20 2f 2a  se->nMem++;   /*
27dbc 20 41 6c 6c 6f 63 61 74 65 20 61 6e 20 65 78 74   Allocate an ext
27dbd 72 61 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ra register for 
27dbe 6c 69 6d 69 74 2b 6f 66 66 73 65 74 20 2a 2f 0a  limit+offset */.
27dbf 20 20 20 20 7d 0a 20 20 20 20 76 20 3d 20 73 71      }.    v = sq
27dc0 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
27dc1 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d  rse);.    if( v=
27dc2 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
27dc3 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
27dc4 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4f 66 66  (pParse, p->pOff
27dc5 73 65 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20  set, iOffset);. 
27dc6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27dc7 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42  dOp1(v, OP_MustB
27dc8 65 49 6e 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a  eInt, iOffset);.
27dc9 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
27dca 28 76 2c 20 22 4f 46 46 53 45 54 20 63 6f 75 6e  (v, "OFFSET coun
27dcb 74 65 72 22 29 29 3b 0a 20 20 20 20 61 64 64 72  ter"));.    addr
27dcc 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
27dcd 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f  ddOp1(v, OP_IfPo
27dce 73 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20  s, iOffset);.   
27dcf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27dd0 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
27dd1 2c 20 30 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20  , 0, iOffset);. 
27dd2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
27dd3 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
27dd4 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 69  ;.    if( p->pLi
27dd5 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  mit ){.      sql
27dd6 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
27dd7 2c 20 4f 50 5f 41 64 64 2c 20 69 4c 69 6d 69 74  , OP_Add, iLimit
27dd8 2c 20 69 4f 66 66 73 65 74 2c 20 69 4f 66 66 73  , iOffset, iOffs
27dd9 65 74 2b 31 29 3b 0a 20 20 20 20 20 20 56 64 62  et+1);.      Vdb
27dda 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49  eComment((v, "LI
27ddb 4d 49 54 2b 4f 46 46 53 45 54 22 29 29 3b 0a 20  MIT+OFFSET"));. 
27ddc 20 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c       addr1 = sql
27ddd 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
27dde 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4c 69 6d  , OP_IfPos, iLim
27ddf 69 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  it);.      sqlit
27de0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27de1 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20  OP_Integer, -1, 
27de2 69 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 20 20  iOffset+1);.    
27de3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
27de4 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
27de5 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69  .    }.  }.}..#i
27de6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
27de7 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
27de8 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  T./*.** Return t
27de9 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 63  he appropriate c
27dea 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
27deb 65 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74  e for the iCol-t
27dec 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74  h column of.** t
27ded 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f  he result set fo
27dee 72 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 2d 73  r the compound-s
27def 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
27df0 22 70 22 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  "p".  Return NUL
27df1 4c 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75  L if.** the colu
27df2 6d 6e 20 68 61 73 20 6e 6f 20 64 65 66 61 75 6c  mn has no defaul
27df3 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  t collating sequ
27df4 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ence..**.** The 
27df5 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
27df6 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f  ce for the compo
27df7 75 6e 64 20 73 65 6c 65 63 74 20 69 73 20 74 61  und select is ta
27df8 6b 65 6e 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ken from the.** 
27df9 6c 65 66 74 2d 6d 6f 73 74 20 74 65 72 6d 20 6f  left-most term o
27dfa 66 20 74 68 65 20 73 65 6c 65 63 74 20 74 68 61  f the select tha
27dfb 74 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e  t has a collatin
27dfc 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2f 0a 73  g sequence..*/.s
27dfd 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 6d  tatic CollSeq *m
27dfe 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65  ultiSelectCollSe
27dff 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
27e00 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20   Select *p, int 
27e01 69 43 6f 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  iCol){.  CollSeq
27e02 20 2a 70 52 65 74 3b 0a 20 20 69 66 28 20 70 2d   *pRet;.  if( p-
27e03 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 70  >pPrior ){.    p
27e04 52 65 74 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63  Ret = multiSelec
27e05 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  tCollSeq(pParse,
27e06 20 70 2d 3e 70 50 72 69 6f 72 2c 20 69 43 6f 6c   p->pPrior, iCol
27e07 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
27e08 70 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  pRet = 0;.  }.  
27e09 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20  assert( iCol>=0 
27e0a 29 3b 0a 20 20 69 66 28 20 70 52 65 74 3d 3d 30  );.  if( pRet==0
27e0b 20 26 26 20 69 43 6f 6c 3c 70 2d 3e 70 45 4c 69   && iCol<p->pELi
27e0c 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
27e0d 20 70 52 65 74 20 3d 20 73 71 6c 69 74 65 33 45   pRet = sqlite3E
27e0e 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
27e0f 65 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  e, p->pEList->a[
27e10 69 43 6f 6c 5d 2e 70 45 78 70 72 29 3b 0a 20 20  iCol].pExpr);.  
27e11 7d 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  }.  return pRet;
27e12 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
27e13 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
27e14 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 20  D_SELECT */../* 
27e15 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
27e16 65 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  e */.static int 
27e17 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72  multiSelectOrder
27e18 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  By(.  Parse *pPa
27e19 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
27e1a 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
27e1b 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
27e1c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
27e1d 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53   right-most of S
27e1e 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64  ELECTs to be cod
27e1f 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  ed */.  SelectDe
27e20 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a  st *pDest     /*
27e21 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68   What to do with
27e22 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a   query results *
27e23 2f 0a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 53  /.);...#ifndef S
27e24 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
27e25 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a  UND_SELECT./*.**
27e26 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
27e27 20 63 61 6c 6c 65 64 20 74 6f 20 70 72 6f 63 65   called to proce
27e28 73 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75  ss a compound qu
27e29 65 72 79 20 66 6f 72 6d 20 66 72 6f 6d 0a 2a 2a  ery form from.**
27e2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 65 70   two or more sep
27e2b 61 72 61 74 65 20 71 75 65 72 69 65 73 20 75 73  arate queries us
27e2c 69 6e 67 20 55 4e 49 4f 4e 2c 20 55 4e 49 4f 4e  ing UNION, UNION
27e2d 20 41 4c 4c 2c 20 45 58 43 45 50 54 2c 20 6f 72   ALL, EXCEPT, or
27e2e 0a 2a 2a 20 49 4e 54 45 52 53 45 43 54 0a 2a 2a  .** INTERSECT.**
27e2f 0a 2a 2a 20 22 70 22 20 70 6f 69 6e 74 73 20 74  .** "p" points t
27e30 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  o the right-most
27e31 20 6f 66 20 74 68 65 20 74 77 6f 20 71 75 65 72   of the two quer
27e32 69 65 73 2e 20 20 74 68 65 20 71 75 65 72 79 20  ies.  the query 
27e33 6f 6e 20 74 68 65 0a 2a 2a 20 6c 65 66 74 20 69  on the.** left i
27e34 73 20 70 2d 3e 70 50 72 69 6f 72 2e 20 20 54 68  s p->pPrior.  Th
27e35 65 20 6c 65 66 74 20 71 75 65 72 79 20 63 6f 75  e left query cou
27e36 6c 64 20 61 6c 73 6f 20 62 65 20 61 20 63 6f 6d  ld also be a com
27e37 70 6f 75 6e 64 20 71 75 65 72 79 0a 2a 2a 20 69  pound query.** i
27e38 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 69  n which case thi
27e39 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62  s routine will b
27e3a 65 20 63 61 6c 6c 65 64 20 72 65 63 75 72 73 69  e called recursi
27e3b 76 65 6c 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  vely. .**.** The
27e3c 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
27e3d 74 6f 74 61 6c 20 71 75 65 72 79 20 61 72 65 20  total query are 
27e3e 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  to be written in
27e3f 74 6f 20 61 20 64 65 73 74 69 6e 61 74 69 6f 6e  to a destination
27e40 0a 2a 2a 20 6f 66 20 74 79 70 65 20 65 44 65 73  .** of type eDes
27e41 74 20 77 69 74 68 20 70 61 72 61 6d 65 74 65 72  t with parameter
27e42 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 45 78   iParm..**.** Ex
27e43 61 6d 70 6c 65 20 31 3a 20 20 43 6f 6e 73 69 64  ample 1:  Consid
27e44 65 72 20 61 20 74 68 72 65 65 2d 77 61 79 20 63  er a three-way c
27e45 6f 6d 70 6f 75 6e 64 20 53 51 4c 20 73 74 61 74  ompound SQL stat
27e46 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ement..**.**    
27e47 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74   SELECT a FROM t
27e48 31 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 62  1 UNION SELECT b
27e49 20 46 52 4f 4d 20 74 32 20 55 4e 49 4f 4e 20 53   FROM t2 UNION S
27e4a 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33 0a  ELECT c FROM t3.
27e4b 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 61 74 65  **.** This state
27e4c 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 20 75  ment is parsed u
27e4d 70 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  p as follows:.**
27e4e 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 63  .**     SELECT c
27e4f 20 46 52 4f 4d 20 74 33 0a 2a 2a 20 20 20 20 20   FROM t3.**     
27e50 20 7c 0a 2a 2a 20 20 20 20 20 20 60 2d 2d 2d 2d   |.**      `----
27e51 2d 3e 20 20 53 45 4c 45 43 54 20 62 20 46 52 4f  ->  SELECT b FRO
27e52 4d 20 74 32 0a 2a 2a 20 20 20 20 20 20 20 20 20  M t2.**         
27e53 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20         |.**     
27e54 20 20 20 20 20 20 20 20 20 20 20 60 2d 2d 2d 2d             `----
27e55 2d 2d 3e 20 20 53 45 4c 45 43 54 20 61 20 46 52  -->  SELECT a FR
27e56 4f 4d 20 74 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  OM t1.**.** The 
27e57 61 72 72 6f 77 73 20 69 6e 20 74 68 65 20 64 69  arrows in the di
27e58 61 67 72 61 6d 20 61 62 6f 76 65 20 72 65 70 72  agram above repr
27e59 65 73 65 6e 74 20 74 68 65 20 53 65 6c 65 63 74  esent the Select
27e5a 2e 70 50 72 69 6f 72 20 70 6f 69 6e 74 65 72 2e  .pPrior pointer.
27e5b 0a 2a 2a 20 53 6f 20 69 66 20 74 68 69 73 20 72  .** So if this r
27e5c 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
27e5d 20 77 69 74 68 20 70 20 65 71 75 61 6c 20 74 6f   with p equal to
27e5e 20 74 68 65 20 74 33 20 71 75 65 72 79 2c 20 74   the t3 query, t
27e5f 68 65 6e 0a 2a 2a 20 70 50 72 69 6f 72 20 77 69  hen.** pPrior wi
27e60 6c 6c 20 62 65 20 74 68 65 20 74 32 20 71 75 65  ll be the t2 que
27e61 72 79 2e 20 20 70 2d 3e 6f 70 20 77 69 6c 6c 20  ry.  p->op will 
27e62 62 65 20 54 4b 5f 55 4e 49 4f 4e 20 69 6e 20 74  be TK_UNION in t
27e63 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
27e64 4e 6f 74 69 63 65 20 74 68 61 74 20 62 65 63 61  Notice that beca
27e65 75 73 65 20 6f 66 20 74 68 65 20 77 61 79 20 53  use of the way S
27e66 51 4c 69 74 65 20 70 61 72 73 65 73 20 63 6f 6d  QLite parses com
27e67 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 2c 20 74  pound SELECTs, t
27e68 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  he.** individual
27e69 20 73 65 6c 65 63 74 73 20 61 6c 77 61 79 73 20   selects always 
27e6a 67 72 6f 75 70 20 66 72 6f 6d 20 6c 65 66 74 20  group from left 
27e6b 74 6f 20 72 69 67 68 74 2e 0a 2a 2f 0a 73 74 61  to right..*/.sta
27e6c 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c  tic int multiSel
27e6d 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ect(.  Parse *pP
27e6e 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
27e6f 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
27e70 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
27e71 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
27e72 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20  e right-most of 
27e73 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f  SELECTs to be co
27e74 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  ded */.  SelectD
27e75 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f  est *pDest     /
27e76 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74  * What to do wit
27e77 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20  h query results 
27e78 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
27e79 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a   SQLITE_OK;   /*
27e7a 20 53 75 63 63 65 73 73 20 63 6f 64 65 20 66 72   Success code fr
27e7b 6f 6d 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  om a subroutine 
27e7c 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72  */.  Select *pPr
27e7d 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  ior;       /* An
27e7e 6f 74 68 65 72 20 53 45 4c 45 43 54 20 69 6d 6d  other SELECT imm
27e7f 65 64 69 61 74 65 6c 79 20 74 6f 20 6f 75 72 20  ediately to our 
27e80 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a  left */.  Vdbe *
27e81 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
27e82 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
27e83 20 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f   to this VDBE */
27e84 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
27e85 73 74 3b 20 20 20 20 20 20 2f 2a 20 41 6c 74 65  st;      /* Alte
27e86 72 6e 61 74 69 76 65 20 64 61 74 61 20 64 65 73  rnative data des
27e87 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 65  tination */.  Se
27e88 6c 65 63 74 20 2a 70 44 65 6c 65 74 65 20 3d 20  lect *pDelete = 
27e89 30 3b 20 20 2f 2a 20 43 68 61 69 6e 20 6f 66 20  0;  /* Chain of 
27e8a 73 69 6d 70 6c 65 20 73 65 6c 65 63 74 73 20 74  simple selects t
27e8b 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 73 71  o delete */.  sq
27e8c 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
27e8d 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
27e8e 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
27e8f 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
27e90 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20  ere is no ORDER 
27e91 42 59 20 6f 72 20 4c 49 4d 49 54 20 63 6c 61 75  BY or LIMIT clau
27e92 73 65 20 6f 6e 20 70 72 69 6f 72 20 53 45 4c 45  se on prior SELE
27e93 43 54 73 2e 20 20 4f 6e 6c 79 0a 20 20 2a 2a 20  CTs.  Only.  ** 
27e94 74 68 65 20 6c 61 73 74 20 28 72 69 67 68 74 2d  the last (right-
27e95 6d 6f 73 74 29 20 53 45 4c 45 43 54 20 69 6e 20  most) SELECT in 
27e96 74 68 65 20 73 65 72 69 65 73 20 6d 61 79 20 68  the series may h
27e97 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ave an ORDER BY 
27e98 6f 72 20 4c 49 4d 49 54 2e 0a 20 20 2a 2f 0a 20  or LIMIT..  */. 
27e99 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d   assert( p && p-
27e9a 3e 70 50 72 69 6f 72 20 29 3b 20 20 2f 2a 20 43  >pPrior );  /* C
27e9b 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
27e9c 67 75 61 72 61 6e 74 65 65 73 20 74 68 69 73 20  guarantees this 
27e9d 6d 75 63 68 20 2a 2f 0a 20 20 64 62 20 3d 20 70  much */.  db = p
27e9e 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 72  Parse->db;.  pPr
27e9f 69 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b  ior = p->pPrior;
27ea0 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
27ea1 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d 70  r->pRightmost!=p
27ea2 50 72 69 6f 72 20 29 3b 0a 20 20 61 73 73 65 72  Prior );.  asser
27ea3 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68  t( pPrior->pRigh
27ea4 74 6d 6f 73 74 3d 3d 70 2d 3e 70 52 69 67 68 74  tmost==p->pRight
27ea5 6d 6f 73 74 20 29 3b 0a 20 20 64 65 73 74 20 3d  most );.  dest =
27ea6 20 2a 70 44 65 73 74 3b 0a 20 20 69 66 28 20 70   *pDest;.  if( p
27ea7 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
27ea8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
27ea9 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 4f  rorMsg(pParse,"O
27eaa 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73  RDER BY clause s
27eab 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 65 72  hould come after
27eac 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 22 2c   %s not before",
27ead 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f 70 4e  .      selectOpN
27eae 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20  ame(p->op));.   
27eaf 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74   rc = 1;.    got
27eb0 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
27eb1 6e 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  nd;.  }.  if( pP
27eb2 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  rior->pLimit ){.
27eb3 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
27eb4 4d 73 67 28 70 50 61 72 73 65 2c 22 4c 49 4d 49  Msg(pParse,"LIMI
27eb5 54 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20  T clause should 
27eb6 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f  come after %s no
27eb7 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20  t before",.     
27eb8 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d   selectOpName(p-
27eb9 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  >op));.    rc = 
27eba 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  1;.    goto mult
27ebb 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
27ebc 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  }..  v = sqlite3
27ebd 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
27ebe 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
27ebf 29 3b 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20  );  /* The VDBE 
27ec0 61 6c 72 65 61 64 79 20 63 72 65 61 74 65 64 20  already created 
27ec1 62 79 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  by calling funct
27ec2 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 43 72 65  ion */..  /* Cre
27ec3 61 74 65 20 74 68 65 20 64 65 73 74 69 6e 61 74  ate the destinat
27ec4 69 6f 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ion temporary ta
27ec5 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72 79  ble if necessary
27ec6 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 74  .  */.  if( dest
27ec7 2e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65  .eDest==SRT_Ephe
27ec8 6d 54 61 62 20 29 7b 0a 20 20 20 20 61 73 73 65  mTab ){.    asse
27ec9 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b  rt( p->pEList );
27eca 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27ecb 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
27ecc 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64 65 73 74  nEphemeral, dest
27ecd 2e 69 50 61 72 6d 2c 20 70 2d 3e 70 45 4c 69 73  .iParm, p->pELis
27ece 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 64  t->nExpr);.    d
27ecf 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f  est.eDest = SRT_
27ed0 54 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Table;.  }..  /*
27ed1 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 53   Make sure all S
27ed2 45 4c 45 43 54 73 20 69 6e 20 74 68 65 20 73 74  ELECTs in the st
27ed3 61 74 65 6d 65 6e 74 20 68 61 76 65 20 74 68 65  atement have the
27ed4 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
27ed5 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 69 6e  elements.  ** in
27ed6 20 74 68 65 69 72 20 72 65 73 75 6c 74 20 73 65   their result se
27ed7 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ts..  */.  asser
27ed8 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20  t( p->pEList && 
27ed9 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 20 29  pPrior->pEList )
27eda 3b 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c 69 73  ;.  if( p->pELis
27edb 74 2d 3e 6e 45 78 70 72 21 3d 70 50 72 69 6f 72  t->nExpr!=pPrior
27edc 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  ->pEList->nExpr 
27edd 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
27ede 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
27edf 53 45 4c 45 43 54 73 20 74 6f 20 74 68 65 20 6c  SELECTs to the l
27ee0 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 6f 66  eft and right of
27ee1 20 25 73 22 0a 20 20 20 20 20 20 22 20 64 6f 20   %s".      " do 
27ee2 6e 6f 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  not have the sam
27ee3 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75  e number of resu
27ee4 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c 20 73 65 6c  lt columns", sel
27ee5 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29  ectOpName(p->op)
27ee6 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  );.    rc = 1;. 
27ee7 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65     goto multi_se
27ee8 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20  lect_end;.  }.. 
27ee9 20 2f 2a 20 43 6f 6d 70 6f 75 6e 64 20 53 45 4c   /* Compound SEL
27eea 45 43 54 73 20 74 68 61 74 20 68 61 76 65 20 61  ECTs that have a
27eeb 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
27eec 65 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65  e are handled se
27eed 70 61 72 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20  parately..  */. 
27eee 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79   if( p->pOrderBy
27eef 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
27ef0 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42  ultiSelectOrderB
27ef1 79 28 70 50 61 72 73 65 2c 20 70 2c 20 70 44 65  y(pParse, p, pDe
27ef2 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47  st);.  }..  /* G
27ef3 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
27ef4 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69   the left and ri
27ef5 67 68 74 20 53 45 4c 45 43 54 20 73 74 61 74 65  ght SELECT state
27ef6 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 73 77  ments..  */.  sw
27ef7 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20  itch( p->op ){. 
27ef8 20 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20     case TK_ALL: 
27ef9 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  {.      int addr
27efa 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65   = 0;.      asse
27efb 72 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4c 69  rt( !pPrior->pLi
27efc 6d 69 74 20 29 3b 0a 20 20 20 20 20 20 70 50 72  mit );.      pPr
27efd 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 2d  ior->pLimit = p-
27efe 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70  >pLimit;.      p
27eff 50 72 69 6f 72 2d 3e 70 4f 66 66 73 65 74 20 3d  Prior->pOffset =
27f00 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20   p->pOffset;.   
27f01 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
27f02 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 50  elect(pParse, pP
27f03 72 69 6f 72 2c 20 26 64 65 73 74 29 3b 0a 20 20  rior, &dest);.  
27f04 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
27f05 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  0;.      p->pOff
27f06 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  set = 0;.      i
27f07 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
27f08 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
27f09 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  ct_end;.      }.
27f0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
27f0b 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  = 0;.      p->iL
27f0c 69 6d 69 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69  imit = pPrior->i
27f0d 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
27f0e 69 4f 66 66 73 65 74 20 3d 20 70 50 72 69 6f 72  iOffset = pPrior
27f0f 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
27f10 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29   if( p->iLimit )
27f11 7b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d  {.        addr =
27f12 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27f13 70 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c  p1(v, OP_IfZero,
27f14 20 70 2d 3e 69 4c 69 6d 69 74 29 3b 0a 20 20 20   p->iLimit);.   
27f15 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
27f16 28 28 76 2c 20 22 4a 75 6d 70 20 61 68 65 61 64  ((v, "Jump ahead
27f17 20 69 66 20 4c 49 4d 49 54 20 72 65 61 63 68 65   if LIMIT reache
27f18 64 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  d"));.      }.  
27f19 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
27f1a 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
27f1b 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20  , &dest);.      
27f1c 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50 72  pDelete = p->pPr
27f1d 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50  ior;.      p->pP
27f1e 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20  rior = pPrior;. 
27f1f 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
27f20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74         goto mult
27f21 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
27f22 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
27f23 61 64 64 72 20 29 7b 0a 20 20 20 20 20 20 20 20  addr ){.        
27f24 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
27f25 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20  ere(v, addr);.  
27f26 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
27f27 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
27f28 65 20 54 4b 5f 45 58 43 45 50 54 3a 0a 20 20 20  e TK_EXCEPT:.   
27f29 20 63 61 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a 20   case TK_UNION: 
27f2a 7b 0a 20 20 20 20 20 20 69 6e 74 20 75 6e 69 6f  {.      int unio
27f2b 6e 54 61 62 3b 20 20 20 20 2f 2a 20 43 75 72 73  nTab;    /* Curs
27f2c 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  or number of the
27f2d 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
27f2e 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 20   holding result 
27f2f 2a 2f 0a 20 20 20 20 20 20 75 38 20 6f 70 20 3d  */.      u8 op =
27f30 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65   0;       /* One
27f31 20 6f 66 20 74 68 65 20 53 52 54 5f 20 6f 70 65   of the SRT_ ope
27f32 72 61 74 69 6f 6e 73 20 74 6f 20 61 70 70 6c 79  rations to apply
27f33 20 74 6f 20 73 65 6c 66 20 2a 2f 0a 20 20 20 20   to self */.    
27f34 20 20 69 6e 74 20 70 72 69 6f 72 4f 70 3b 20 20    int priorOp;  
27f35 20 20 20 2f 2a 20 54 68 65 20 53 52 54 5f 20 6f     /* The SRT_ o
27f36 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 70 70 6c  peration to appl
27f37 79 20 74 6f 20 70 72 69 6f 72 20 73 65 6c 65 63  y to prior selec
27f38 74 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  ts */.      Expr
27f39 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66 73   *pLimit, *pOffs
27f3a 65 74 3b 20 2f 2a 20 53 61 76 65 64 20 76 61 6c  et; /* Saved val
27f3b 75 65 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d 69 74  ues of p->nLimit
27f3c 20 61 6e 64 20 70 2d 3e 6e 4f 66 66 73 65 74 20   and p->nOffset 
27f3d 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
27f3e 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44  r;.      SelectD
27f3f 65 73 74 20 75 6e 69 6f 6e 64 65 73 74 3b 0a 0a  est uniondest;..
27f40 20 20 20 20 20 20 70 72 69 6f 72 4f 70 20 3d 20        priorOp = 
27f41 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20 20  SRT_Union;.     
27f42 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d   if( dest.eDest=
27f43 3d 70 72 69 6f 72 4f 70 20 26 26 20 41 4c 57 41  =priorOp && ALWA
27f44 59 53 28 21 70 2d 3e 70 4c 69 6d 69 74 20 26 26  YS(!p->pLimit &&
27f45 21 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a  !p->pOffset) ){.
27f46 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 63 61          /* We ca
27f47 6e 20 72 65 75 73 65 20 61 20 74 65 6d 70 6f 72  n reuse a tempor
27f48 61 72 79 20 74 61 62 6c 65 20 67 65 6e 65 72 61  ary table genera
27f49 74 65 64 20 62 79 20 61 20 53 45 4c 45 43 54 20  ted by a SELECT 
27f4a 74 6f 20 6f 75 72 0a 20 20 20 20 20 20 20 20 2a  to our.        *
27f4b 2a 20 72 69 67 68 74 2e 0a 20 20 20 20 20 20 20  * right..       
27f4c 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
27f4d 72 74 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73  rt( p->pRightmos
27f4e 74 21 3d 70 20 29 3b 20 20 2f 2a 20 43 61 6e 20  t!=p );  /* Can 
27f4f 6f 6e 6c 79 20 68 61 70 70 65 6e 20 66 6f 72 20  only happen for 
27f50 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e 74  leftward element
27f51 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s.              
27f52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f53 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 33         ** of a 3
27f54 2d 77 61 79 20 6f 72 20 6d 6f 72 65 20 63 6f 6d  -way or more com
27f55 70 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20  pound */.       
27f56 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4c 69 6d   assert( p->pLim
27f57 69 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a  it==0 );      /*
27f58 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20   Not allowed on 
27f59 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e 74  leftward element
27f5a 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73  s */.        ass
27f5b 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d  ert( p->pOffset=
27f5c 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20 4e 6f 74  =0 );     /* Not
27f5d 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 6c 65 66 74   allowed on left
27f5e 77 61 72 64 20 65 6c 65 6d 65 6e 74 73 20 2a 2f  ward elements */
27f5f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54 61  .        unionTa
27f60 62 20 3d 20 64 65 73 74 2e 69 50 61 72 6d 3b 0a  b = dest.iParm;.
27f61 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27f62 20 20 20 20 20 2f 2a 20 57 65 20 77 69 6c 6c 20       /* We will 
27f63 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20 6f  need to create o
27f64 75 72 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72 79  ur own temporary
27f65 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74   table to hold t
27f66 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  he.        ** in
27f67 74 65 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c  termediate resul
27f68 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ts..        */. 
27f69 20 20 20 20 20 20 20 75 6e 69 6f 6e 54 61 62 20         unionTab 
27f6a 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
27f6b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
27f6c 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30  ( p->pOrderBy==0
27f6d 20 29 3b 0a 20 20 20 20 20 20 20 20 61 64 64 72   );.        addr
27f6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
27f6f 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
27f70 70 68 65 6d 65 72 61 6c 2c 20 75 6e 69 6f 6e 54  phemeral, unionT
27f71 61 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ab, 0);.        
27f72 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 72 4f  assert( p->addrO
27f73 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d 20 2d 31  penEphm[0] == -1
27f74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61   );.        p->a
27f75 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d  ddrOpenEphm[0] =
27f76 20 61 64 64 72 3b 0a 20 20 20 20 20 20 20 20 70   addr;.        p
27f77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73 65  ->pRightmost->se
27f78 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73 65  lFlags |= SF_Use
27f79 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 20 20  sEphemeral;.    
27f7a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
27f7b 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 7d  EList );.      }
27f7c 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20  ..      /* Code 
27f7d 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  the SELECT state
27f7e 6d 65 6e 74 73 20 74 6f 20 6f 75 72 20 6c 65 66  ments to our lef
27f7f 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  t.      */.     
27f80 20 61 73 73 65 72 74 28 20 21 70 50 72 69 6f 72   assert( !pPrior
27f81 2d 3e 70 4f 72 64 65 72 42 79 20 29 3b 0a 20 20  ->pOrderBy );.  
27f82 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
27f83 74 44 65 73 74 49 6e 69 74 28 26 75 6e 69 6f 6e  tDestInit(&union
27f84 64 65 73 74 2c 20 70 72 69 6f 72 4f 70 2c 20 75  dest, priorOp, u
27f85 6e 69 6f 6e 54 61 62 29 3b 0a 20 20 20 20 20 20  nionTab);.      
27f86 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  rc = sqlite3Sele
27f87 63 74 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f  ct(pParse, pPrio
27f88 72 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b 0a  r, &uniondest);.
27f89 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
27f8a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
27f8b 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
27f8c 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
27f8d 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e   Code the curren
27f8e 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  t SELECT stateme
27f8f 6e 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  nt.      */.    
27f90 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f    if( p->op==TK_
27f91 45 58 43 45 50 54 20 29 7b 0a 20 20 20 20 20 20  EXCEPT ){.      
27f92 20 20 6f 70 20 3d 20 53 52 54 5f 45 78 63 65 70    op = SRT_Excep
27f93 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  t;.      }else{.
27f94 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
27f95 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20  p->op==TK_UNION 
27f96 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 20 3d 20  );.        op = 
27f97 53 52 54 5f 55 6e 69 6f 6e 3b 0a 20 20 20 20 20  SRT_Union;.     
27f98 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69   }.      p->pPri
27f99 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c  or = 0;.      pL
27f9a 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74  imit = p->pLimit
27f9b 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
27f9c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 66  t = 0;.      pOf
27f9d 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65  fset = p->pOffse
27f9e 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  t;.      p->pOff
27f9f 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 75  set = 0;.      u
27fa0 6e 69 6f 6e 64 65 73 74 2e 65 44 65 73 74 20 3d  niondest.eDest =
27fa1 20 6f 70 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   op;.      rc = 
27fa2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
27fa3 61 72 73 65 2c 20 70 2c 20 26 75 6e 69 6f 6e 64  arse, p, &uniond
27fa4 65 73 74 29 3b 0a 20 20 20 20 20 20 2f 2a 20 51  est);.      /* Q
27fa5 75 65 72 79 20 66 6c 61 74 74 65 6e 69 6e 67 20  uery flattening 
27fa6 69 6e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  in sqlite3Select
27fa7 28 29 20 6d 69 67 68 74 20 72 65 66 69 6c 6c 20  () might refill 
27fa8 70 2d 3e 70 4f 72 64 65 72 42 79 2e 0a 20 20 20  p->pOrderBy..   
27fa9 20 20 20 2a 2a 20 42 65 20 73 75 72 65 20 74 6f     ** Be sure to
27faa 20 64 65 6c 65 74 65 20 70 2d 3e 70 4f 72 64 65   delete p->pOrde
27fab 72 42 79 2c 20 74 68 65 72 65 66 6f 72 65 2c 20  rBy, therefore, 
27fac 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f 72  to avoid a memor
27fad 79 20 6c 65 61 6b 2e 20 2a 2f 0a 20 20 20 20 20  y leak. */.     
27fae 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
27faf 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f  Delete(db, p->pO
27fb0 72 64 65 72 42 79 29 3b 0a 20 20 20 20 20 20 70  rderBy);.      p
27fb1 44 65 6c 65 74 65 20 3d 20 70 2d 3e 70 50 72 69  Delete = p->pPri
27fb2 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50 72  or;.      p->pPr
27fb3 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20  ior = pPrior;.  
27fb4 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20      p->pOrderBy 
27fb5 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
27fb6 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
27fb7 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20   p->pLimit);.   
27fb8 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70     p->pLimit = p
27fb9 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
27fba 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65  pOffset = pOffse
27fbb 74 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 69 6d  t;.      p->iLim
27fbc 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  it = 0;.      p-
27fbd 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >iOffset = 0;.  
27fbe 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
27fbf 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69        goto multi
27fc0 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20  _select_end;.   
27fc1 20 20 20 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a 20     }...      /* 
27fc2 43 6f 6e 76 65 72 74 20 74 68 65 20 64 61 74 61  Convert the data
27fc3 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72   in the temporar
27fc4 79 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 61  y table into wha
27fc5 74 65 76 65 72 20 66 6f 72 6d 0a 20 20 20 20 20  tever form.     
27fc6 20 2a 2a 20 69 74 20 69 73 20 74 68 61 74 20 77   ** it is that w
27fc7 65 20 63 75 72 72 65 6e 74 6c 79 20 6e 65 65 64  e currently need
27fc8 2e 0a 20 20 20 20 20 20 2a 2f 20 20 20 20 20 20  ..      */      
27fc9 0a 20 20 20 20 20 20 69 66 28 20 64 65 73 74 2e  .      if( dest.
27fca 65 44 65 73 74 21 3d 70 72 69 6f 72 4f 70 20 7c  eDest!=priorOp |
27fcb 7c 20 75 6e 69 6f 6e 54 61 62 21 3d 64 65 73 74  | unionTab!=dest
27fcc 2e 69 50 61 72 6d 20 29 7b 0a 20 20 20 20 20 20  .iParm ){.      
27fcd 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72    int iCont, iBr
27fce 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20 20  eak, iStart;.   
27fcf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
27fd0 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20  pEList );.      
27fd1 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
27fd2 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a  ==SRT_Output ){.
27fd3 20 20 20 20 20 20 20 20 20 20 53 65 6c 65 63 74            Select
27fd4 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a 20 20   *pFirst = p;.  
27fd5 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
27fd6 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20  First->pPrior ) 
27fd7 70 46 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d  pFirst = pFirst-
27fd8 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20  >pPrior;.       
27fd9 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d     generateColum
27fda 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30  nNames(pParse, 0
27fdb 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74  , pFirst->pEList
27fdc 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
27fdd 20 20 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71       iBreak = sq
27fde 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
27fdf 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 69  el(v);.        i
27fe0 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Cont = sqlite3Vd
27fe1 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
27fe2 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c          computeL
27fe3 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50  imitRegisters(pP
27fe4 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29  arse, p, iBreak)
27fe5 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
27fe6 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27fe7 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69 6f 6e 54  P_Rewind, unionT
27fe8 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20  ab, iBreak);.   
27fe9 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 73 71       iStart = sq
27fea 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
27feb 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 20  Addr(v);.       
27fec 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70   selectInnerLoop
27fed 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70  (pParse, p, p->p
27fee 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54 61 62 2c  EList, unionTab,
27fef 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
27ff0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
27ff1 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 2d 31             0, -1
27ff2 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74 2c 20  , &dest, iCont, 
27ff3 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20  iBreak);.       
27ff4 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
27ff5 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e  lveLabel(v, iCon
27ff6 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
27ff7 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27ff8 20 4f 50 5f 4e 65 78 74 2c 20 75 6e 69 6f 6e 54   OP_Next, unionT
27ff9 61 62 2c 20 69 53 74 61 72 74 29 3b 0a 20 20 20  ab, iStart);.   
27ffa 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27ffb 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
27ffc 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20  iBreak);.       
27ffd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27ffe 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
27fff 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20  unionTab, 0);.  
28000 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
28001 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
28002 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20  e TK_INTERSECT: 
28003 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 61 62 31  {.      int tab1
28004 2c 20 74 61 62 32 3b 0a 20 20 20 20 20 20 69 6e  , tab2;.      in
28005 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 2c  t iCont, iBreak,
28006 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20 45   iStart;.      E
28007 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f  xpr *pLimit, *pO
28008 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69 6e 74  ffset;.      int
28009 20 61 64 64 72 3b 0a 20 20 20 20 20 20 53 65 6c   addr;.      Sel
2800a 65 63 74 44 65 73 74 20 69 6e 74 65 72 73 65 63  ectDest intersec
2800b 74 64 65 73 74 3b 0a 20 20 20 20 20 20 69 6e 74  tdest;.      int
2800c 20 72 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49   r1;..      /* I
2800d 4e 54 45 52 53 45 43 54 20 69 73 20 64 69 66 66  NTERSECT is diff
2800e 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 6f  erent from the o
2800f 74 68 65 72 73 20 73 69 6e 63 65 20 69 74 20 72  thers since it r
28010 65 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a 2a  equires.      **
28011 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 20 74   two temporary t
28012 61 62 6c 65 73 2e 20 20 48 65 6e 63 65 20 69 74  ables.  Hence it
28013 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63 61 73   has its own cas
28014 65 2e 20 20 42 65 67 69 6e 0a 20 20 20 20 20 20  e.  Begin.      
28015 2a 2a 20 62 79 20 61 6c 6c 6f 63 61 74 69 6e 67  ** by allocating
28016 20 74 68 65 20 74 61 62 6c 65 73 20 77 65 20 77   the tables we w
28017 69 6c 6c 20 6e 65 65 64 2e 0a 20 20 20 20 20 20  ill need..      
28018 2a 2f 0a 20 20 20 20 20 20 74 61 62 31 20 3d 20  */.      tab1 = 
28019 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
2801a 20 20 20 20 20 20 74 61 62 32 20 3d 20 70 50 61        tab2 = pPa
2801b 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2801c 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f     assert( p->pO
2801d 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 0a 20 20  rderBy==0 );..  
2801e 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2801f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28020 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
28021 2c 20 74 61 62 31 2c 20 30 29 3b 0a 20 20 20 20  , tab1, 0);.    
28022 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64    assert( p->add
28023 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d 20  rOpenEphm[0] == 
28024 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  -1 );.      p->a
28025 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d  ddrOpenEphm[0] =
28026 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e   addr;.      p->
28027 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73 65 6c 46  pRightmost->selF
28028 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73 65 73 45  lags |= SF_UsesE
28029 70 68 65 6d 65 72 61 6c 3b 0a 20 20 20 20 20 20  phemeral;.      
2802a 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73  assert( p->pELis
2802b 74 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43  t );..      /* C
2802c 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54 73 20  ode the SELECTs 
2802d 74 6f 20 6f 75 72 20 6c 65 66 74 20 69 6e 74 6f  to our left into
2802e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2802f 20 22 74 61 62 31 22 2e 0a 20 20 20 20 20 20 2a   "tab1"..      *
28030 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  /.      sqlite3S
28031 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 69  electDestInit(&i
28032 6e 74 65 72 73 65 63 74 64 65 73 74 2c 20 53 52  ntersectdest, SR
28033 54 5f 55 6e 69 6f 6e 2c 20 74 61 62 31 29 3b 0a  T_Union, tab1);.
28034 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
28035 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
28036 20 70 50 72 69 6f 72 2c 20 26 69 6e 74 65 72 73   pPrior, &inters
28037 65 63 74 64 65 73 74 29 3b 0a 20 20 20 20 20 20  ectdest);.      
28038 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
28039 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c    goto multi_sel
2803a 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d  ect_end;.      }
2803b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20  ..      /* Code 
2803c 74 68 65 20 63 75 72 72 65 6e 74 20 53 45 4c 45  the current SELE
2803d 43 54 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72  CT into temporar
2803e 79 20 74 61 62 6c 65 20 22 74 61 62 32 22 0a 20  y table "tab2". 
2803f 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64       */.      ad
28040 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
28041 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
28042 6e 45 70 68 65 6d 65 72 61 6c 2c 20 74 61 62 32  nEphemeral, tab2
28043 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65  , 0);.      asse
28044 72 74 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45  rt( p->addrOpenE
28045 70 68 6d 5b 31 5d 20 3d 3d 20 2d 31 20 29 3b 0a  phm[1] == -1 );.
28046 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65        p->addrOpe
28047 6e 45 70 68 6d 5b 31 5d 20 3d 20 61 64 64 72 3b  nEphm[1] = addr;
28048 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  .      p->pPrior
28049 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 69 6d   = 0;.      pLim
2804a 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a  it = p->pLimit;.
2804b 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20        p->pLimit 
2804c 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 66 66 73  = 0;.      pOffs
2804d 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b  et = p->pOffset;
2804e 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65  .      p->pOffse
2804f 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74  t = 0;.      int
28050 65 72 73 65 63 74 64 65 73 74 2e 69 50 61 72 6d  ersectdest.iParm
28051 20 3d 20 74 61 62 32 3b 0a 20 20 20 20 20 20 72   = tab2;.      r
28052 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  c = sqlite3Selec
28053 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 69 6e  t(pParse, p, &in
28054 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a 20 20  tersectdest);.  
28055 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70 2d      pDelete = p-
28056 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70  >pPrior;.      p
28057 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f  ->pPrior = pPrio
28058 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
28059 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2805a 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20  ->pLimit);.     
2805b 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69   p->pLimit = pLi
2805c 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f  mit;.      p->pO
2805d 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b  ffset = pOffset;
2805e 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
2805f 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
28060 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
28061 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
28062 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
28063 74 6f 20 74 61 6b 65 20 74 68 65 20 69 6e 74 65  to take the inte
28064 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20  rsection of the 
28065 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a 20 20  two temporary.  
28066 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e 0a 20      ** tables.. 
28067 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73       */.      as
28068 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
28069 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 73  );.      if( des
2806a 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  t.eDest==SRT_Out
2806b 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 53  put ){.        S
2806c 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20  elect *pFirst = 
2806d 70 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  p;.        while
2806e 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  ( pFirst->pPrior
2806f 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72   ) pFirst = pFir
28070 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20  st->pPrior;.    
28071 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75      generateColu
28072 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
28073 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73  0, pFirst->pELis
28074 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
28075 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74    iBreak = sqlit
28076 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
28077 76 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e 74 20  v);.      iCont 
28078 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
28079 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
2807a 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
2807b 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70  isters(pParse, p
2807c 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
2807d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2807e 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
2807f 20 74 61 62 31 2c 20 69 42 72 65 61 6b 29 3b 0a   tab1, iBreak);.
28080 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
28081 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
28082 72 73 65 29 3b 0a 20 20 20 20 20 20 69 53 74 61  rse);.      iSta
28083 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rt = sqlite3Vdbe
28084 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
28085 4b 65 79 2c 20 74 61 62 31 2c 20 72 31 29 3b 0a  Key, tab1, r1);.
28086 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28087 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
28088 74 46 6f 75 6e 64 2c 20 74 61 62 32 2c 20 69 43  tFound, tab2, iC
28089 6f 6e 74 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  ont, r1);.      
2808a 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2808b 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2808c 29 3b 0a 20 20 20 20 20 20 73 65 6c 65 63 74 49  );.      selectI
2808d 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c  nnerLoop(pParse,
2808e 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 74   p, p->pEList, t
2808f 61 62 31 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e  ab1, p->pEList->
28090 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20  nExpr,.         
28091 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
28092 2d 31 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74  -1, &dest, iCont
28093 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
28094 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
28095 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e  lveLabel(v, iCon
28096 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
28097 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28098 50 5f 4e 65 78 74 2c 20 74 61 62 31 2c 20 69 53  P_Next, tab1, iS
28099 74 61 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  tart);.      sql
2809a 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
2809b 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b  abel(v, iBreak);
2809c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2809d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2809e 6c 6f 73 65 2c 20 74 61 62 32 2c 20 30 29 3b 0a  lose, tab2, 0);.
2809f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
280a0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
280a1 6f 73 65 2c 20 74 61 62 31 2c 20 30 29 3b 0a 20  ose, tab1, 0);. 
280a2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
280a3 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70  }.  }..  /* Comp
280a4 75 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ute collating se
280a5 71 75 65 6e 63 65 73 20 75 73 65 64 20 62 79 20  quences used by 
280a6 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20  .  ** temporary 
280a7 74 61 62 6c 65 73 20 6e 65 65 64 65 64 20 74 6f  tables needed to
280a8 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63   implement the c
280a9 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a  ompound select..
280aa 20 20 2a 2a 20 41 74 74 61 63 68 20 74 68 65 20    ** Attach the 
280ab 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
280ac 65 20 74 6f 20 61 6c 6c 20 74 65 6d 70 6f 72 61  e to all tempora
280ad 72 79 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a  ry tables..  **.
280ae 20 20 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f    ** This sectio
280af 6e 20 69 73 20 72 75 6e 20 62 79 20 74 68 65 20  n is run by the 
280b0 72 69 67 68 74 2d 6d 6f 73 74 20 53 45 4c 45 43  right-most SELEC
280b1 54 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 6c 79  T statement only
280b2 2e 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74  ..  ** SELECT st
280b3 61 74 65 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  atements to the 
280b4 6c 65 66 74 20 61 6c 77 61 79 73 20 73 6b 69 70  left always skip
280b5 20 74 68 69 73 20 70 61 72 74 2e 20 20 54 68 65   this part.  The
280b6 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a   right-most.  **
280b7 20 53 45 4c 45 43 54 20 6d 69 67 68 74 20 61 6c   SELECT might al
280b8 73 6f 20 73 6b 69 70 20 74 68 69 73 20 70 61 72  so skip this par
280b9 74 20 69 66 20 69 74 20 68 61 73 20 6e 6f 20 4f  t if it has no O
280ba 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61  RDER BY clause a
280bb 6e 64 0a 20 20 2a 2a 20 6e 6f 20 74 65 6d 70 20  nd.  ** no temp 
280bc 74 61 62 6c 65 73 20 61 72 65 20 72 65 71 75 69  tables are requi
280bd 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  red..  */.  if( 
280be 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
280bf 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 20 29  _UsesEphemeral )
280c0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20  {.    int i;    
280c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
280c2 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
280c3 74 65 72 20 2a 2f 0a 20 20 20 20 4b 65 79 49 6e  ter */.    KeyIn
280c4 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20  fo *pKeyInfo;   
280c5 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c           /* Coll
280c6 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
280c7 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  or the result se
280c8 74 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20  t */.    Select 
280c9 2a 70 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20  *pLoop;         
280ca 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
280cb 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 53 45  oping through SE
280cc 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
280cd 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  */.    CollSeq *
280ce 2a 61 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20  *apColl;        
280cf 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
280d0 69 6e 67 20 74 68 72 6f 75 67 68 20 70 4b 65 79  ing through pKey
280d1 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d 20 2a 2f  Info->aColl[] */
280d2 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20  .    int nCol;  
280d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
280d4 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
280d5 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73 75 6c  columns in resul
280d6 74 20 73 65 74 20 2a 2f 0a 0a 20 20 20 20 61 73  t set */..    as
280d7 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74 6d  sert( p->pRightm
280d8 6f 73 74 3d 3d 70 20 29 3b 0a 20 20 20 20 6e 43  ost==p );.    nC
280d9 6f 6c 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e  ol = p->pEList->
280da 6e 45 78 70 72 3b 0a 20 20 20 20 70 4b 65 79 49  nExpr;.    pKeyI
280db 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  nfo = sqlite3DbM
280dc 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 0a 20 20  allocZero(db,.  
280dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
280de 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4b 65       sizeof(*pKe
280df 79 49 6e 66 6f 29 2b 6e 43 6f 6c 2a 28 73 69 7a  yInfo)+nCol*(siz
280e0 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20  eof(CollSeq*) + 
280e1 31 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4b  1));.    if( !pK
280e2 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20  eyInfo ){.      
280e3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
280e4 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  M;.      goto mu
280e5 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
280e6 20 20 20 20 7d 0a 0a 20 20 20 20 70 4b 65 79 49      }..    pKeyI
280e7 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64  nfo->enc = ENC(d
280e8 62 29 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f  b);.    pKeyInfo
280e9 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
280ea 6e 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69  nCol;..    for(i
280eb 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65 79 49  =0, apColl=pKeyI
280ec 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c 6e 43  nfo->aColl; i<nC
280ed 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c 6c 2b  ol; i++, apColl+
280ee 2b 29 7b 0a 20 20 20 20 20 20 2a 61 70 43 6f 6c  +){.      *apCol
280ef 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43  l = multiSelectC
280f0 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
280f1 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , i);.      if( 
280f2 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a 20 20  0==*apColl ){.  
280f3 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d 20        *apColl = 
280f4 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20  db->pDfltColl;. 
280f5 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
280f6 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70    for(pLoop=p; p
280f7 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f  Loop; pLoop=pLoo
280f8 70 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20  p->pPrior){.    
280f9 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 3b 20    for(i=0; i<2; 
280fa 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  i++){.        in
280fb 74 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70 2d 3e  t addr = pLoop->
280fc 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 3b  addrOpenEphm[i];
280fd 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 64 64  .        if( add
280fe 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  r<0 ){.         
280ff 20 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20 75 6e   /* If [0] is un
28100 75 73 65 64 20 74 68 65 6e 20 5b 31 5d 20 69 73  used then [1] is
28101 20 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20 20 53   also unused.  S
28102 6f 20 77 65 20 63 61 6e 0a 20 20 20 20 20 20 20  o we can.       
28103 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 61 66     ** always saf
28104 65 6c 79 20 61 62 6f 72 74 20 61 73 20 73 6f 6f  ely abort as soo
28105 6e 20 61 73 20 74 68 65 20 66 69 72 73 74 20 75  n as the first u
28106 6e 75 73 65 64 20 73 6c 6f 74 20 69 73 20 66 6f  nused slot is fo
28107 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  und */.         
28108 20 61 73 73 65 72 74 28 20 70 4c 6f 6f 70 2d 3e   assert( pLoop->
28109 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 3c  addrOpenEphm[1]<
2810a 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  0 );.          b
2810b 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
2810c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2810d 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
2810e 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20  ddr, nCol);.    
2810f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
28110 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c  hangeP4(v, addr,
28111 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
28112 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20  , P4_KEYINFO);. 
28113 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 61 64         pLoop->ad
28114 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20 3d 20  drOpenEphm[i] = 
28115 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
28116 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
28117 72 65 65 28 64 62 2c 20 70 4b 65 79 49 6e 66 6f  ree(db, pKeyInfo
28118 29 3b 0a 20 20 7d 0a 0a 6d 75 6c 74 69 5f 73 65  );.  }..multi_se
28119 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 70 44 65 73  lect_end:.  pDes
2811a 74 2d 3e 69 4d 65 6d 20 3d 20 64 65 73 74 2e 69  t->iMem = dest.i
2811b 4d 65 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 6e 4d  Mem;.  pDest->nM
2811c 65 6d 20 3d 20 64 65 73 74 2e 6e 4d 65 6d 3b 0a  em = dest.nMem;.
2811d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2811e 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 65 74  elete(db, pDelet
2811f 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
28120 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
28121 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
28122 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a  D_SELECT */../*.
28123 2a 2a 20 43 6f 64 65 20 61 6e 20 6f 75 74 70 75  ** Code an outpu
28124 74 20 73 75 62 72 6f 75 74 69 6e 65 20 66 6f 72  t subroutine for
28125 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 69 6d 70   a coroutine imp
28126 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
28127 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d  .** SELECT statm
28128 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  ent..**.** The d
28129 61 74 61 20 74 6f 20 62 65 20 6f 75 74 70 75 74  ata to be output
2812a 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e   is contained in
2812b 20 70 49 6e 2d 3e 69 4d 65 6d 2e 20 20 54 68 65   pIn->iMem.  The
2812c 72 65 20 61 72 65 0a 2a 2a 20 70 49 6e 2d 3e 6e  re are.** pIn->n
2812d 4d 65 6d 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62  Mem columns to b
2812e 65 20 6f 75 74 70 75 74 2e 20 20 70 44 65 73 74  e output.  pDest
2812f 20 69 73 20 77 68 65 72 65 20 74 68 65 20 6f 75   is where the ou
28130 74 70 75 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62  tput should.** b
28131 65 20 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 72 65  e sent..**.** re
28132 67 52 65 74 75 72 6e 20 69 73 20 74 68 65 20 6e  gReturn is the n
28133 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 65 67  umber of the reg
28134 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68  ister holding th
28135 65 20 73 75 62 72 6f 75 74 69 6e 65 0a 2a 2a 20  e subroutine.** 
28136 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 2e 0a  return address..
28137 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 50 72 65 76  **.** If regPrev
28138 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 61 20  >0 then it is a 
28139 74 68 65 20 66 69 72 73 74 20 72 65 67 69 73 74  the first regist
2813a 65 72 20 69 6e 20 61 20 76 65 63 74 6f 72 20 74  er in a vector t
2813b 68 61 74 0a 2a 2a 20 72 65 63 6f 72 64 73 20 74  hat.** records t
2813c 68 65 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70  he previous outp
2813d 75 74 2e 20 20 6d 65 6d 5b 72 65 67 50 72 65 76  ut.  mem[regPrev
2813e 5d 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74  ] is a flag that
2813f 20 69 73 20 66 61 6c 73 65 0a 2a 2a 20 69 66 20   is false.** if 
28140 74 68 65 72 65 20 68 61 73 20 62 65 65 6e 20 6e  there has been n
28141 6f 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70 75  o previous outpu
28142 74 2e 20 20 49 66 20 72 65 67 50 72 65 76 3e 30  t.  If regPrev>0
28143 20 74 68 65 6e 20 63 6f 64 65 20 69 73 0a 2a 2a   then code is.**
28144 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 73 75   generated to su
28145 70 70 72 65 73 73 20 64 75 70 6c 69 63 61 74 65  ppress duplicate
28146 73 2e 20 20 70 4b 65 79 49 6e 66 6f 20 69 73 20  s.  pKeyInfo is 
28147 75 73 65 64 20 66 6f 72 20 63 6f 6d 70 61 72 69  used for compari
28148 6e 67 0a 2a 2a 20 6b 65 79 73 2e 0a 2a 2a 0a 2a  ng.** keys..**.*
28149 2a 20 49 66 20 74 68 65 20 4c 49 4d 49 54 20 66  * If the LIMIT f
2814a 6f 75 6e 64 20 69 6e 20 70 2d 3e 69 4c 69 6d 69  ound in p->iLimi
2814b 74 20 69 73 20 72 65 61 63 68 65 64 2c 20 6a 75  t is reached, ju
2814c 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
2814d 6f 0a 2a 2a 20 69 42 72 65 61 6b 2e 0a 2a 2f 0a  o.** iBreak..*/.
2814e 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e 65 72  static int gener
2814f 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74  ateOutputSubrout
28150 69 6e 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ine(.  Parse *pP
28151 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
28152 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
28153 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
28154 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
28155 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
28156 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65  tement */.  Sele
28157 63 74 44 65 73 74 20 2a 70 49 6e 2c 20 20 20 20  ctDest *pIn,    
28158 20 20 20 20 2f 2a 20 43 6f 72 6f 75 74 69 6e 65      /* Coroutine
28159 20 73 75 70 70 6c 79 69 6e 67 20 64 61 74 61 20   supplying data 
2815a 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
2815b 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a 20  *pDest,      /* 
2815c 57 68 65 72 65 20 74 6f 20 73 65 6e 64 20 74 68  Where to send th
2815d 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  e data */.  int 
2815e 72 65 67 52 65 74 75 72 6e 2c 20 20 20 20 20 20  regReturn,      
2815f 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72      /* The retur
28160 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73 74  n address regist
28161 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 50  er */.  int regP
28162 72 65 76 2c 20 20 20 20 20 20 20 20 20 20 20 20  rev,            
28163 2f 2a 20 50 72 65 76 69 6f 75 73 20 72 65 73 75  /* Previous resu
28164 6c 74 20 72 65 67 69 73 74 65 72 2e 20 20 4e 6f  lt register.  No
28165 20 75 6e 69 71 75 65 6e 65 73 73 20 69 66 20 30   uniqueness if 0
28166 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
28167 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 2f 2a  KeyInfo,      /*
28168 20 46 6f 72 20 63 6f 6d 70 61 72 69 6e 67 20 77   For comparing w
28169 69 74 68 20 70 72 65 76 69 6f 75 73 20 65 6e 74  ith previous ent
2816a 72 79 20 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79  ry */.  int p4ty
2816b 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pe,             
2816c 2f 2a 20 54 68 65 20 70 34 20 74 79 70 65 20 66  /* The p4 type f
2816d 6f 72 20 70 4b 65 79 49 6e 66 6f 20 2a 2f 0a 20  or pKeyInfo */. 
2816e 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20 20 20   int iBreak     
2816f 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
28170 20 68 65 72 65 20 69 66 20 77 65 20 68 69 74 20   here if we hit 
28171 74 68 65 20 4c 49 4d 49 54 20 2a 2f 0a 29 7b 0a  the LIMIT */.){.
28172 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
28173 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
28174 20 69 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 69 6e   iContinue;.  in
28175 74 20 61 64 64 72 3b 0a 0a 20 20 61 64 64 72 20  t addr;..  addr 
28176 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
28177 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69  rentAddr(v);.  i
28178 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c 69 74  Continue = sqlit
28179 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2817a 76 29 3b 0a 0a 20 20 2f 2a 20 53 75 70 70 72 65  v);..  /* Suppre
2817b 73 73 20 64 75 70 6c 69 63 61 74 65 73 20 66 6f  ss duplicates fo
2817c 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c  r UNION, EXCEPT,
2817d 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54 20 0a   and INTERSECT .
2817e 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67 50 72    */.  if( regPr
2817f 65 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31  ev ){.    int j1
28180 2c 20 6a 32 3b 0a 20 20 20 20 6a 31 20 3d 20 73  , j2;.    j1 = s
28181 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
28182 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 65  (v, OP_IfNot, re
28183 67 50 72 65 76 29 3b 0a 20 20 20 20 6a 32 20 3d  gPrev);.    j2 =
28184 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28185 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72 65  p4(v, OP_Compare
28186 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72 65 67  , pIn->iMem, reg
28187 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e 4d 65  Prev+1, pIn->nMe
28188 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m,.             
28189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2818a 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
2818b 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 20 20 73  , p4type);.    s
2818c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2818d 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 32 2b  (v, OP_Jump, j2+
2818e 32 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6a 32  2, iContinue, j2
2818f 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +2);.    sqlite3
28190 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
28191 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j1);.    sqlite3
28192 45 78 70 72 43 6f 64 65 43 6f 70 79 28 70 50 61  ExprCodeCopy(pPa
28193 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20  rse, pIn->iMem, 
28194 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e  regPrev+1, pIn->
28195 6e 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74  nMem);.    sqlit
28196 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28197 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72  OP_Integer, 1, r
28198 65 67 50 72 65 76 29 3b 0a 20 20 7d 0a 20 20 69  egPrev);.  }.  i
28199 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  f( pParse->db->m
2819a 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
2819b 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 53 75  turn 0;..  /* Su
2819c 70 70 72 65 73 73 20 74 68 65 20 74 68 65 20 66  ppress the the f
2819d 69 72 73 74 20 4f 46 46 53 45 54 20 65 6e 74 72  irst OFFSET entr
2819e 69 65 73 20 69 66 20 74 68 65 72 65 20 69 73 20  ies if there is 
2819f 61 6e 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  an OFFSET clause
281a0 0a 20 20 2a 2f 0a 20 20 63 6f 64 65 4f 66 66 73  .  */.  codeOffs
281a1 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e  et(v, p, iContin
281a2 75 65 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  ue);..  switch( 
281a3 70 44 65 73 74 2d 3e 65 44 65 73 74 20 29 7b 0a  pDest->eDest ){.
281a4 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65      /* Store the
281a5 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 61 20   result as data 
281a6 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65 20 6b  using a unique k
281a7 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ey..    */.    c
281a8 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20  ase SRT_Table:. 
281a9 20 20 20 63 61 73 65 20 53 52 54 5f 45 70 68 65     case SRT_Ephe
281aa 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 69 6e  mTab: {.      in
281ab 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65  t r1 = sqlite3Ge
281ac 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
281ad 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 32 20 3d  ;.      int r2 =
281ae 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
281af 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
281b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
281b1 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
281b2 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  cord, pIn->iMem,
281b3 20 70 49 6e 2d 3e 6e 4d 65 6d 2c 20 72 31 29 3b   pIn->nMem, r1);
281b4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
281b5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
281b6 65 77 52 6f 77 69 64 2c 20 70 44 65 73 74 2d 3e  ewRowid, pDest->
281b7 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20 20  iParm, r2);.    
281b8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
281b9 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
281ba 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20  , pDest->iParm, 
281bb 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 73  r1, r2);.      s
281bc 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
281bd 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
281be 45 4e 44 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  END);.      sqli
281bf 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
281c0 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20  g(pParse, r2);. 
281c1 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
281c2 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
281c3 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72  e, r1);.      br
281c4 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e  eak;.    }..#ifn
281c5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
281c6 53 55 42 51 55 45 52 59 0a 20 20 20 20 2f 2a 20  SUBQUERY.    /* 
281c7 49 66 20 77 65 20 61 72 65 20 63 72 65 61 74 69  If we are creati
281c8 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20  ng a set for an 
281c9 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54  "expr IN (SELECT
281ca 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74   ...)" construct
281cb 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68  ,.    ** then th
281cc 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20  ere should be a 
281cd 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74  single item on t
281ce 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74 65  he stack.  Write
281cf 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 74 65   this.    ** ite
281d0 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74 20 74  m into the set t
281d1 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75 73 20  able with bogus 
281d2 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  data..    */.   
281d3 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b   case SRT_Set: {
281d4 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 20  .      int r1;. 
281d5 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e       assert( pIn
281d6 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20 20  ->nMem==1 );.   
281d7 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d     p->affinity =
281d8 20 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74   .         sqlit
281d9 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
281da 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30  y(p->pEList->a[0
281db 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d 3e  ].pExpr, pDest->
281dc 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20 20  affinity);.     
281dd 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
281de 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
281df 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
281e0 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
281e1 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d 3e  akeRecord, pIn->
281e2 69 4d 65 6d 2c 20 31 2c 20 72 31 2c 20 26 70 2d  iMem, 1, r1, &p-
281e3 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20  >affinity, 1);. 
281e4 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
281e5 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
281e6 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d  nge(pParse, pIn-
281e7 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 20  >iMem, 1);.     
281e8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
281e9 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p2(v, OP_IdxInse
281ea 72 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  rt, pDest->iParm
281eb 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
281ec 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
281ed 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
281ee 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
281ef 20 7d 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e 65   }..#if 0  /* Ne
281f0 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 6e  ver occurs on an
281f1 20 4f 52 44 45 52 20 42 59 20 71 75 65 72 79 20   ORDER BY query 
281f2 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79  */.    /* If any
281f3 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68   row exist in th
281f4 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65  e result set, re
281f5 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 20 61  cord that fact a
281f6 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f  nd abort..    */
281f7 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78  .    case SRT_Ex
281f8 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71  ists: {.      sq
281f9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
281fa 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
281fb 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b  , pDest->iParm);
281fc 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49  .      /* The LI
281fd 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20  MIT clause will 
281fe 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f  terminate the lo
281ff 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20  op for us */.   
28200 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
28201 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49  #endif..    /* I
28202 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61 6c  f this is a scal
28203 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20 69  ar select that i
28204 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70  s part of an exp
28205 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20 20  ression, then.  
28206 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72    ** store the r
28207 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61 70  esults in the ap
28208 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72 79  propriate memory
28209 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b 20   cell and break 
2820a 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  out.    ** of th
2820b 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20  e scan loop..   
2820c 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
2820d 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73  _Mem: {.      as
2820e 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d  sert( pIn->nMem=
2820f 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =1 );.      sqli
28210 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
28211 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65  pParse, pIn->iMe
28212 6d 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c  m, pDest->iParm,
28213 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68   1);.      /* Th
28214 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77  e LIMIT clause w
28215 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66 20  ill jump out of 
28216 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20  the loop for us 
28217 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  */.      break;.
28218 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
28219 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2821a 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a  MIT_SUBQUERY */.
2821b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75  .    /* The resu
2821c 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 69  lts are stored i
2821d 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  n a sequence of 
2821e 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a  registers.    **
2821f 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 44 65   starting at pDe
28220 73 74 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 6e 20  st->iMem.  Then 
28221 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 79  the co-routine y
28222 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ields..    */.  
28223 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f 75    case SRT_Corou
28224 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20 69 66  tine: {.      if
28225 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30  ( pDest->iMem==0
28226 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 73   ){.        pDes
28227 74 2d 3e 69 4d 65 6d 20 3d 20 73 71 6c 69 74 65  t->iMem = sqlite
28228 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
28229 61 72 73 65 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29  arse, pIn->nMem)
2822a 3b 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 2d  ;.        pDest-
2822b 3e 6e 4d 65 6d 20 3d 20 70 49 6e 2d 3e 6e 4d 65  >nMem = pIn->nMe
2822c 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  m;.      }.     
2822d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2822e 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49 6e  Move(pParse, pIn
2822f 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 69  ->iMem, pDest->i
28230 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d 65 6d  Mem, pDest->nMem
28231 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
28232 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
28233 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69  _Yield, pDest->i
28234 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 62 72 65  Parm);.      bre
28235 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ak;.    }..    /
28236 2a 20 52 65 73 75 6c 74 73 20 61 72 65 20 73 74  * Results are st
28237 6f 72 65 64 20 69 6e 20 61 20 73 65 71 75 65 6e  ored in a sequen
28238 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 2e  ce of registers.
28239 20 20 54 68 65 6e 20 74 68 65 0a 20 20 20 20 2a    Then the.    *
2823a 2a 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 6f  * OP_ResultRow o
2823b 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f  pcode is used to
2823c 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 73   cause sqlite3_s
2823d 74 65 70 28 29 20 74 6f 20 72 65 74 75 72 6e 0a  tep() to return.
2823e 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78 74 20      ** the next 
2823f 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a 20  row of result.. 
28240 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
28241 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20 20 20  RT_Output: {.   
28242 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28243 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
28244 74 52 6f 77 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  tRow, pIn->iMem,
28245 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20   pIn->nMem);.   
28246 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
28247 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
28248 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69  e(pParse, pIn->i
28249 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b  Mem, pIn->nMem);
2824a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2824b 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65    }..#if !define
2824c 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
2824d 49 47 47 45 52 29 0a 20 20 20 20 2f 2a 20 44 69  IGGER).    /* Di
2824e 73 63 61 72 64 20 74 68 65 20 72 65 73 75 6c 74  scard the result
2824f 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  s.  This is used
28250 20 66 6f 72 20 53 45 4c 45 43 54 20 73 74 61 74   for SELECT stat
28251 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 0a 20 20  ements inside.  
28252 20 20 2a 2a 20 74 68 65 20 62 6f 64 79 20 6f 66    ** the body of
28253 20 61 20 54 52 49 47 47 45 52 2e 20 20 54 68 65   a TRIGGER.  The
28254 20 70 75 72 70 6f 73 65 20 6f 66 20 73 75 63 68   purpose of such
28255 20 73 65 6c 65 63 74 73 20 69 73 20 74 6f 20 63   selects is to c
28256 61 6c 6c 0a 20 20 20 20 2a 2a 20 75 73 65 72 2d  all.    ** user-
28257 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
28258 73 20 74 68 61 74 20 68 61 76 65 20 73 69 64 65  s that have side
28259 20 65 66 66 65 63 74 73 2e 20 20 57 65 20 64 6f   effects.  We do
2825a 20 6e 6f 74 20 63 61 72 65 0a 20 20 20 20 2a 2a   not care.    **
2825b 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 75 61   about the actua
2825c 6c 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65  l results of the
2825d 20 73 65 6c 65 63 74 2e 0a 20 20 20 20 2a 2f 0a   select..    */.
2825e 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
2825f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
28260 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  }.#endif.  }..  
28261 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 65  /* Jump to the e
28262 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69  nd of the loop i
28263 66 20 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72  f the LIMIT is r
28264 65 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  eached..  */.  i
28265 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a  f( p->iLimit ){.
28266 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28267 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
28268 6d 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d  mm, p->iLimit, -
28269 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2826a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2826b 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69  IfZero, p->iLimi
2826c 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d 0a  t, iBreak);.  }.
2826d 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74  .  /* Generate t
2826e 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 72 65  he subroutine re
2826f 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  turn.  */.  sqli
28270 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
28271 62 65 6c 28 76 2c 20 69 43 6f 6e 74 69 6e 75 65  bel(v, iContinue
28272 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
28273 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74  AddOp1(v, OP_Ret
28274 75 72 6e 2c 20 72 65 67 52 65 74 75 72 6e 29 3b  urn, regReturn);
28275 0a 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b  ..  return addr;
28276 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 74 65 72 6e  .}../*.** Altern
28277 61 74 69 76 65 20 63 6f 6d 70 6f 75 6e 64 20 73  ative compound s
28278 65 6c 65 63 74 20 63 6f 64 65 20 67 65 6e 65 72  elect code gener
28279 61 74 6f 72 20 66 6f 72 20 63 61 73 65 73 20 77  ator for cases w
2827a 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 69 73 20  hen there.** is 
2827b 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
2827c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 73 73  se..**.** We ass
2827d 75 6d 65 20 61 20 71 75 65 72 79 20 6f 66 20 74  ume a query of t
2827e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72  he following for
2827f 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 3c 73  m:.**.**      <s
28280 65 6c 65 63 74 41 3e 20 20 3c 6f 70 65 72 61 74  electA>  <operat
28281 6f 72 3e 20 20 3c 73 65 6c 65 63 74 42 3e 20 20  or>  <selectB>  
28282 4f 52 44 45 52 20 42 59 20 3c 6f 72 64 65 72 62  ORDER BY <orderb
28283 79 6c 69 73 74 3e 0a 2a 2a 0a 2a 2a 20 3c 6f 70  ylist>.**.** <op
28284 65 72 61 74 6f 72 3e 20 69 73 20 6f 6e 65 20 6f  erator> is one o
28285 66 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 55 4e 49  f UNION ALL, UNI
28286 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72 20 49  ON, EXCEPT, or I
28287 4e 54 45 52 53 45 43 54 2e 20 20 54 68 65 20 69  NTERSECT.  The i
28288 64 65 61 0a 2a 2a 20 69 73 20 74 6f 20 63 6f 64  dea.** is to cod
28289 65 20 62 6f 74 68 20 3c 73 65 6c 65 63 74 41 3e  e both <selectA>
2828a 20 61 6e 64 20 3c 73 65 6c 65 63 74 42 3e 20 77   and <selectB> w
2828b 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59  ith the ORDER BY
2828c 20 63 6c 61 75 73 65 20 61 73 0a 2a 2a 20 63 6f   clause as.** co
2828d 2d 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 6e  -routines.  Then
2828e 20 72 75 6e 20 74 68 65 20 63 6f 2d 72 6f 75 74   run the co-rout
2828f 69 6e 65 73 20 69 6e 20 70 61 72 61 6c 6c 65 6c  ines in parallel
28290 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20 72   and merge the r
28291 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20 74  esults.** into t
28292 68 65 20 6f 75 74 70 75 74 2e 20 20 49 6e 20 61  he output.  In a
28293 64 64 69 74 69 6f 6e 20 74 6f 20 74 68 65 20 74  ddition to the t
28294 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20 28 63  wo coroutines (c
28295 61 6c 6c 65 64 20 73 65 6c 65 63 74 41 20 61 6e  alled selectA an
28296 64 0a 2a 2a 20 73 65 6c 65 63 74 42 29 20 74 68  d.** selectB) th
28297 65 72 65 20 61 72 65 20 37 20 73 75 62 72 6f 75  ere are 7 subrou
28298 74 69 6e 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tines:.**.**    
28299 6f 75 74 41 3a 20 20 20 20 4d 6f 76 65 20 74 68  outA:    Move th
2829a 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20  e output of the 
2829b 73 65 6c 65 63 74 41 20 63 6f 72 6f 75 74 69 6e  selectA coroutin
2829c 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75  e into the outpu
2829d 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2829e 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   of the compound
2829f 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20   query..**.**   
282a0 20 6f 75 74 42 3a 20 20 20 20 4d 6f 76 65 20 74   outB:    Move t
282a1 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65  he output of the
282a2 20 73 65 6c 65 63 74 42 20 63 6f 72 6f 75 74 69   selectB corouti
282a3 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70  ne into the outp
282a4 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
282a5 20 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e    of the compoun
282a6 64 20 71 75 65 72 79 2e 20 20 28 4f 6e 6c 79 20  d query.  (Only 
282a7 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 55 4e  generated for UN
282a8 49 4f 4e 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ION and.**      
282a9 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c         UNION ALL
282aa 2e 20 20 45 58 43 45 50 54 20 61 6e 64 20 49 4e  .  EXCEPT and IN
282ab 53 45 52 54 53 45 43 54 20 6e 65 76 65 72 20 6f  SERTSECT never o
282ac 75 74 70 75 74 20 61 20 72 6f 77 20 74 68 61 74  utput a row that
282ad 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
282ae 61 70 70 65 61 72 73 20 6f 6e 6c 79 20 69 6e 20  appears only in 
282af 42 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 41 6c 74  B.).**.**    Alt
282b0 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65  B:    Called whe
282b1 6e 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20  n there is data 
282b2 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74  from both corout
282b3 69 6e 65 73 20 61 6e 64 20 41 3c 42 2e 0a 2a 2a  ines and A<B..**
282b4 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20 20 20 20  .**    AeqB:    
282b5 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72  Called when ther
282b6 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62  e is data from b
282b7 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61  oth coroutines a
282b8 6e 64 20 41 3d 3d 42 2e 0a 2a 2a 0a 2a 2a 20 20  nd A==B..**.**  
282b9 20 20 41 67 74 42 3a 20 20 20 20 43 61 6c 6c 65    AgtB:    Calle
282ba 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  d when there is 
282bb 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63  data from both c
282bc 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3e  oroutines and A>
282bd 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 41  B..**.**    EofA
282be 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e  :    Called when
282bf 20 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74   data is exhaust
282c0 65 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 2e  ed from selectA.
282c1 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20  .**.**    EofB: 
282c2 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 64     Called when d
282c3 61 74 61 20 69 73 20 65 78 68 61 75 73 74 65 64  ata is exhausted
282c4 20 66 72 6f 6d 20 73 65 6c 65 63 74 42 2e 0a 2a   from selectB..*
282c5 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65  *.** The impleme
282c6 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  ntation of the l
282c7 61 74 74 65 72 20 66 69 76 65 20 73 75 62 72 6f  atter five subro
282c8 75 74 69 6e 65 73 20 64 65 70 65 6e 64 20 6f 6e  utines depend on
282c9 20 77 68 69 63 68 20 0a 2a 2a 20 3c 6f 70 65 72   which .** <oper
282ca 61 74 6f 72 3e 20 69 73 20 75 73 65 64 3a 0a 2a  ator> is used:.*
282cb 2a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  *.**.**         
282cc 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 20 20 20      UNION ALL   
282cd 20 20 20 20 20 20 55 4e 49 4f 4e 20 20 20 20 20        UNION     
282ce 20 20 20 20 20 20 20 45 58 43 45 50 54 20 20 20         EXCEPT   
282cf 20 20 20 20 20 20 20 49 4e 54 45 52 53 45 43 54         INTERSECT
282d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2d 2d 2d  .**          ---
282d1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d  ----------  ----
282d2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d  -------------  -
282d3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d  -------------  -
282d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
282d5 0a 2a 2a 20 20 20 41 6c 74 42 3a 20 20 20 6f 75  .**   AltB:   ou
282d6 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 6f  tA, nextA      o
282d7 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
282d8 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20   outA, nextA    
282d9 20 20 20 20 20 6e 65 78 74 41 0a 2a 2a 0a 2a 2a       nextA.**.**
282da 20 20 20 41 65 71 42 3a 20 20 20 6f 75 74 41 2c     AeqB:   outA,
282db 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 6e   nextA         n
282dc 65 78 74 41 20 20 20 20 20 20 20 20 20 20 20 20  extA            
282dd 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 6f   nextA         o
282de 75 74 41 2c 20 6e 65 78 74 41 0a 2a 2a 0a 2a 2a  utA, nextA.**.**
282df 20 20 20 41 67 74 42 3a 20 20 20 6f 75 74 42 2c     AgtB:   outB,
282e0 20 6e 65 78 74 42 20 20 20 20 20 20 6f 75 74 42   nextB      outB
282e1 2c 20 6e 65 78 74 42 20 20 20 20 20 20 20 20 20  , nextB         
282e2 20 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20   nextB          
282e3 20 20 6e 65 78 74 42 0a 2a 2a 0a 2a 2a 20 20 20    nextB.**.**   
282e4 45 6f 66 41 3a 20 20 20 6f 75 74 42 2c 20 6e 65  EofA:   outB, ne
282e5 78 74 42 20 20 20 20 20 20 6f 75 74 42 2c 20 6e  xtB      outB, n
282e6 65 78 74 42 20 20 20 20 20 20 20 20 20 20 68 61  extB          ha
282e7 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 68  lt             h
282e8 61 6c 74 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66 42  alt.**.**   EofB
282e9 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20  :   outA, nextA 
282ea 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41       outA, nextA
282eb 20 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78         outA, nex
282ec 74 41 20 20 20 20 20 20 20 20 20 68 61 6c 74 0a  tA         halt.
282ed 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 41 6c 74  **.** In the Alt
282ee 42 2c 20 41 65 71 42 2c 20 61 6e 64 20 41 67 74  B, AeqB, and Agt
282ef 42 20 73 75 62 72 6f 75 74 69 6e 65 73 2c 20 61  B subroutines, a
282f0 6e 20 45 4f 46 20 6f 6e 20 41 20 66 6f 6c 6c 6f  n EOF on A follo
282f1 77 69 6e 67 20 6e 65 78 74 41 0a 2a 2a 20 63 61  wing nextA.** ca
282f2 75 73 65 73 20 61 6e 20 69 6d 6d 65 64 69 61 74  uses an immediat
282f3 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66 41 20 61  e jump to EofA a
282f4 6e 64 20 61 6e 20 45 4f 46 20 6f 6e 20 42 20 66  nd an EOF on B f
282f5 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 42 20 63  ollowing nextB c
282f6 61 75 73 65 73 0a 2a 2a 20 61 6e 20 69 6d 6d 65  auses.** an imme
282f7 64 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f  diate jump to Eo
282f8 66 42 2e 20 20 57 69 74 68 69 6e 20 45 6f 66 41  fB.  Within EofA
282f9 20 61 6e 64 20 45 6f 66 42 2c 20 61 6e 64 20 45   and EofB, and E
282fa 4f 46 20 6f 6e 20 65 6e 74 72 79 20 6f 72 0a 2a  OF on entry or.*
282fb 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74  * following next
282fc 58 20 63 61 75 73 65 73 20 61 20 6a 75 6d 70 20  X causes a jump 
282fd 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
282fe 65 20 73 65 6c 65 63 74 20 70 72 6f 63 65 73 73  e select process
282ff 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 44 75 70 6c 69  ing..**.** Dupli
28300 63 61 74 65 20 72 65 6d 6f 76 61 6c 20 69 6e 20  cate removal in 
28301 74 68 65 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50  the UNION, EXCEP
28302 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54  T, and INTERSECT
28303 20 63 61 73 65 73 20 69 73 20 68 61 6e 64 6c 65   cases is handle
28304 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20  d.** within the 
28305 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e  output subroutin
28306 65 2e 20 20 54 68 65 20 72 65 67 50 72 65 76 20  e.  The regPrev 
28307 72 65 67 69 73 74 65 72 20 73 65 74 20 68 6f 6c  register set hol
28308 64 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c  ds the previousl
28309 79 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 6c 75  y.** output valu
2830a 65 2e 20 20 41 20 63 6f 6d 70 61 72 69 73 6f 6e  e.  A comparison
2830b 20 69 73 20 6d 61 64 65 20 61 67 61 69 6e 73 74   is made against
2830c 20 74 68 69 73 20 76 61 6c 75 65 20 61 6e 64 20   this value and 
2830d 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 69 73  the output.** is
2830e 20 73 6b 69 70 70 65 64 20 69 66 20 74 68 65 20   skipped if the 
2830f 6e 65 78 74 20 72 65 73 75 6c 74 73 20 77 6f 75  next results wou
28310 6c 64 20 62 65 20 74 68 65 20 73 61 6d 65 20 61  ld be the same a
28311 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e 0a  s the previous..
28312 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d  **.** The implem
28313 65 6e 74 61 74 69 6f 6e 20 70 6c 61 6e 20 69 73  entation plan is
28314 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
28315 65 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73  e two coroutines
28316 20 61 6e 64 20 73 65 76 65 6e 0a 2a 2a 20 73 75   and seven.** su
28317 62 72 6f 75 74 69 6e 65 73 20 66 69 72 73 74 2c  broutines first,
28318 20 74 68 65 6e 20 70 75 74 20 74 68 65 20 63 6f   then put the co
28319 6e 74 72 6f 6c 20 6c 6f 67 69 63 20 61 74 20 74  ntrol logic at t
2831a 68 65 20 62 6f 74 74 6f 6d 2e 20 20 4c 69 6b 65  he bottom.  Like
2831b 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
2831c 20 20 20 20 20 20 67 6f 74 6f 20 49 6e 69 74 0a        goto Init.
2831d 2a 2a 20 20 20 20 20 63 6f 41 3a 20 63 6f 72 6f  **     coA: coro
2831e 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 71  utine for left q
2831f 75 65 72 79 20 28 41 29 0a 2a 2a 20 20 20 20 20  uery (A).**     
28320 63 6f 42 3a 20 63 6f 72 6f 75 74 69 6e 65 20 66  coB: coroutine f
28321 6f 72 20 72 69 67 68 74 20 71 75 65 72 79 20 28  or right query (
28322 42 29 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20 6f  B).**    outA: o
28323 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66  utput one row of
28324 20 41 0a 2a 2a 20 20 20 20 6f 75 74 42 3a 20 6f   A.**    outB: o
28325 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66  utput one row of
28326 20 42 20 28 55 4e 49 4f 4e 20 61 6e 64 20 55 4e   B (UNION and UN
28327 49 4f 4e 20 41 4c 4c 20 6f 6e 6c 79 29 0a 2a 2a  ION ALL only).**
28328 20 20 20 20 45 6f 66 41 3a 20 2e 2e 2e 0a 2a 2a      EofA: ....**
28329 20 20 20 20 45 6f 66 42 3a 20 2e 2e 2e 0a 2a 2a      EofB: ....**
2832a 20 20 20 20 41 6c 74 42 3a 20 2e 2e 2e 0a 2a 2a      AltB: ....**
2832b 20 20 20 20 41 65 71 42 3a 20 2e 2e 2e 0a 2a 2a      AeqB: ....**
2832c 20 20 20 20 41 67 74 42 3a 20 2e 2e 2e 0a 2a 2a      AgtB: ....**
2832d 20 20 20 20 49 6e 69 74 3a 20 69 6e 69 74 69 61      Init: initia
2832e 6c 69 7a 65 20 63 6f 72 6f 75 74 69 6e 65 20 72  lize coroutine r
2832f 65 67 69 73 74 65 72 73 0a 2a 2a 20 20 20 20 20  egisters.**     
28330 20 20 20 20 20 79 69 65 6c 64 20 63 6f 41 0a 2a       yield coA.*
28331 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 65 6f  *          if eo
28332 66 28 41 29 20 67 6f 74 6f 20 45 6f 66 41 0a 2a  f(A) goto EofA.*
28333 2a 20 20 20 20 20 20 20 20 20 20 79 69 65 6c 64  *          yield
28334 20 63 6f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20   coB.**         
28335 20 69 66 20 65 6f 66 28 42 29 20 67 6f 74 6f 20   if eof(B) goto 
28336 45 6f 66 42 0a 2a 2a 20 20 20 20 43 6d 70 72 3a  EofB.**    Cmpr:
28337 20 43 6f 6d 70 61 72 65 20 41 2c 20 42 0a 2a 2a   Compare A, B.**
28338 20 20 20 20 20 20 20 20 20 20 4a 75 6d 70 20 41            Jump A
28339 6c 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42 0a  ltB, AeqB, AgtB.
2833a 2a 2a 20 20 20 20 20 45 6e 64 3a 20 2e 2e 2e 0a  **     End: ....
2833b 2a 2a 0a 2a 2a 20 57 65 20 63 61 6c 6c 20 41 6c  **.** We call Al
2833c 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42 2c 20  tB, AeqB, AgtB, 
2833d 45 6f 66 41 2c 20 61 6e 64 20 45 6f 66 42 20 22  EofA, and EofB "
2833e 73 75 62 72 6f 75 74 69 6e 65 73 22 20 62 75 74  subroutines" but
2833f 20 74 68 65 79 20 61 72 65 20 6e 6f 74 0a 2a 2a   they are not.**
28340 20 61 63 74 75 61 6c 6c 79 20 63 61 6c 6c 65 64   actually called
28341 20 75 73 69 6e 67 20 47 6f 73 75 62 20 61 6e 64   using Gosub and
28342 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 52 65 74   they do not Ret
28343 75 72 6e 2e 20 20 45 6f 66 41 20 61 6e 64 20 45  urn.  EofA and E
28344 6f 66 42 20 6c 6f 6f 70 0a 2a 2a 20 75 6e 74 69  ofB loop.** unti
28345 6c 20 61 6c 6c 20 64 61 74 61 20 69 73 20 65 78  l all data is ex
28346 68 61 75 73 74 65 64 20 74 68 65 6e 20 6a 75 6d  hausted then jum
28347 70 20 74 6f 20 74 68 65 20 22 65 6e 64 22 20 6c  p to the "end" l
28348 61 62 65 2e 20 20 41 6c 74 42 2c 20 41 65 71 42  abe.  AltB, AeqB
28349 2c 0a 2a 2a 20 61 6e 64 20 41 67 74 42 20 6a 75  ,.** and AgtB ju
2834a 6d 70 20 74 6f 20 65 69 74 68 65 72 20 4c 32 20  mp to either L2 
2834b 6f 72 20 74 6f 20 6f 6e 65 20 6f 66 20 45 6f 66  or to one of Eof
2834c 41 20 6f 72 20 45 6f 66 42 2e 0a 2a 2f 0a 23 69  A or EofB..*/.#i
2834d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2834e 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
2834f 54 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c  T.static int mul
28350 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79 28  tiSelectOrderBy(
28351 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
28352 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
28353 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
28354 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
28355 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69         /* The ri
28356 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45  ght-most of SELE
28357 43 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20  CTs to be coded 
28358 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
28359 2a 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68  *pDest     /* Wh
2835a 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75  at to do with qu
2835b 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29  ery results */.)
2835c 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  {.  int i, j;   
2835d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2835e 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  p counters */.  
2835f 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20  Select *pPrior; 
28360 20 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72        /* Another
28361 20 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74   SELECT immediat
28362 65 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20  ely to our left 
28363 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  */.  Vdbe *v;   
28364 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65             /* Ge
28365 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74  nerate code to t
28366 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65  his VDBE */.  Se
28367 6c 65 63 74 44 65 73 74 20 64 65 73 74 41 3b 20  lectDest destA; 
28368 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69      /* Destinati
28369 6f 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65  on for coroutine
2836a 20 41 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65   A */.  SelectDe
2836b 73 74 20 64 65 73 74 42 3b 20 20 20 20 20 2f 2a  st destB;     /*
2836c 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72   Destination for
2836d 20 63 6f 72 6f 75 74 69 6e 65 20 42 20 2a 2f 0a   coroutine B */.
2836e 20 20 69 6e 74 20 72 65 67 41 64 64 72 41 3b 20    int regAddrA; 
2836f 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
28370 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
28371 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f 75 74 69  select-A corouti
28372 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45  ne */.  int regE
28373 6f 66 41 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ofA;          /*
28374 20 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74   Flag to indicat
28375 65 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 41 20  e when select-A 
28376 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a 20  is complete */. 
28377 20 69 6e 74 20 72 65 67 41 64 64 72 42 3b 20 20   int regAddrB;  
28378 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
28379 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73  s register for s
2837a 65 6c 65 63 74 2d 42 20 63 6f 72 6f 75 74 69 6e  elect-B coroutin
2837b 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f  e */.  int regEo
2837c 66 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fB;          /* 
2837d 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65  Flag to indicate
2837e 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 42 20 69   when select-B i
2837f 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a 20 20  s complete */.  
28380 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 41 3b  int addrSelectA;
28381 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
28382 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d 41   of the select-A
28383 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20   coroutine */.  
28384 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 42 3b  int addrSelectB;
28385 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
28386 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d 42   of the select-B
28387 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20   coroutine */.  
28388 69 6e 74 20 72 65 67 4f 75 74 41 3b 20 20 20 20  int regOutA;    
28389 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2838a 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 68   register for th
2838b 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72 6f  e output-A subro
2838c 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72  utine */.  int r
2838d 65 67 4f 75 74 42 3b 20 20 20 20 20 20 20 20 20  egOutB;         
2838e 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 69   /* Address regi
2838f 73 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75 74  ster for the out
28390 70 75 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  put-B subroutine
28391 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 75   */.  int addrOu
28392 74 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  tA;         /* A
28393 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6f 75  ddress of the ou
28394 74 70 75 74 2d 41 20 73 75 62 72 6f 75 74 69 6e  tput-A subroutin
28395 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f  e */.  int addrO
28396 75 74 42 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  utB = 0;     /* 
28397 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6f  Address of the o
28398 75 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74 69  utput-B subrouti
28399 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
2839a 45 6f 66 41 3b 20 20 20 20 20 20 20 20 20 2f 2a  EofA;         /*
2839b 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2839c 73 65 6c 65 63 74 2d 41 2d 65 78 68 61 75 73 74  select-A-exhaust
2839d 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f  ed subroutine */
2839e 0a 20 20 69 6e 74 20 61 64 64 72 45 6f 66 42 3b  .  int addrEofB;
2839f 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
283a0 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63  ess of the selec
283a1 74 2d 42 2d 65 78 68 61 75 73 74 65 64 20 73 75  t-B-exhausted su
283a2 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
283a3 74 20 61 64 64 72 41 6c 74 42 3b 20 20 20 20 20  t addrAltB;     
283a4 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
283a5 66 20 74 68 65 20 41 3c 42 20 73 75 62 72 6f 75  f the A<B subrou
283a6 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64  tine */.  int ad
283a7 64 72 41 65 71 42 3b 20 20 20 20 20 20 20 20 20  drAeqB;         
283a8 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
283a9 65 20 41 3d 3d 42 20 73 75 62 72 6f 75 74 69 6e  e A==B subroutin
283aa 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 41  e */.  int addrA
283ab 67 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  gtB;         /* 
283ac 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 41  Address of the A
283ad 3e 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f  >B subroutine */
283ae 0a 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74 41  .  int regLimitA
283af 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69  ;        /* Limi
283b0 74 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73  t register for s
283b1 65 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74  elect-A */.  int
283b2 20 72 65 67 4c 69 6d 69 74 42 3b 20 20 20 20 20   regLimitB;     
283b3 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67 69     /* Limit regi
283b4 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74 2d  ster for select-
283b5 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 50 72  A */.  int regPr
283b6 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ev;          /* 
283b7 41 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69 73  A range of regis
283b8 74 65 72 73 20 74 6f 20 68 6f 6c 64 20 70 72 65  ters to hold pre
283b9 76 69 6f 75 73 20 6f 75 74 70 75 74 20 2a 2f 0a  vious output */.
283ba 20 20 69 6e 74 20 73 61 76 65 64 4c 69 6d 69 74    int savedLimit
283bb 3b 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64  ;       /* Saved
283bc 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4c 69   value of p->iLi
283bd 6d 69 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  mit */.  int sav
283be 65 64 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f  edOffset;      /
283bf 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66  * Saved value of
283c0 20 70 2d 3e 69 4f 66 66 73 65 74 20 2a 2f 0a 20   p->iOffset */. 
283c1 20 69 6e 74 20 6c 61 62 65 6c 43 6d 70 72 3b 20   int labelCmpr; 
283c2 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20         /* Label 
283c3 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f 66  for the start of
283c4 20 74 68 65 20 6d 65 72 67 65 20 61 6c 67 6f 72   the merge algor
283c5 69 74 68 6d 20 2a 2f 0a 20 20 69 6e 74 20 6c 61  ithm */.  int la
283c6 62 65 6c 45 6e 64 3b 20 20 20 20 20 20 20 20 20  belEnd;         
283c7 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65  /* Label for the
283c8 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 76 65 72   end of the over
283c9 61 6c 6c 20 53 45 4c 45 43 54 20 73 74 6d 74 20  all SELECT stmt 
283ca 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20 20  */.  int j1;    
283cb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75             /* Ju
283cc 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  mp instructions 
283cd 74 68 61 74 20 67 65 74 20 72 65 74 61 72 67 65  that get retarge
283ce 74 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  tted */.  int op
283cf 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
283d0 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 41 4c 4c  /* One of TK_ALL
283d1 2c 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54 4b 5f 45  , TK_UNION, TK_E
283d2 58 43 45 50 54 2c 20 54 4b 5f 49 4e 54 45 52 53  XCEPT, TK_INTERS
283d3 45 43 54 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f  ECT */.  KeyInfo
283d4 20 2a 70 4b 65 79 44 75 70 20 3d 20 30 3b 20 2f   *pKeyDup = 0; /
283d5 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66  * Comparison inf
283d6 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 64 75 70  ormation for dup
283d7 6c 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20 2a  licate removal *
283d8 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  /.  KeyInfo *pKe
283d9 79 4d 65 72 67 65 3b 20 20 20 2f 2a 20 43 6f 6d  yMerge;   /* Com
283da 70 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d 61 74  parison informat
283db 69 6f 6e 20 66 6f 72 20 6d 65 72 67 69 6e 67 20  ion for merging 
283dc 72 6f 77 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rows */.  sqlite
283dd 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
283de 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
283df 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72  ection */.  Expr
283e0 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20  List *pOrderBy; 
283e1 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
283e2 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  Y clause */.  in
283e3 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20 20  t nOrderBy;     
283e4 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
283e5 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52   terms in the OR
283e6 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
283e7 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74 65  .  int *aPermute
283e8 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70  ;        /* Mapp
283e9 69 6e 67 20 66 72 6f 6d 20 4f 52 44 45 52 20 42  ing from ORDER B
283ea 59 20 74 65 72 6d 73 20 74 6f 20 72 65 73 75 6c  Y terms to resul
283eb 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f  t set columns */
283ec 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  ..  assert( p->p
283ed 4f 72 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20  OrderBy!=0 );.  
283ee 61 73 73 65 72 74 28 20 70 4b 65 79 44 75 70 3d  assert( pKeyDup=
283ef 3d 30 20 29 3b 20 2f 2a 20 22 4d 61 6e 61 67 65  =0 ); /* "Manage
283f0 64 22 20 63 6f 64 65 20 6e 65 65 64 73 20 74 68  d" code needs th
283f1 69 73 2e 20 20 54 69 63 6b 65 74 20 23 33 33 38  is.  Ticket #338
283f2 32 2e 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61  2. */.  db = pPa
283f3 72 73 65 2d 3e 64 62 3b 0a 20 20 76 20 3d 20 70  rse->db;.  v = p
283f4 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
283f5 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72  if( v==0 ) retur
283f6 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
283f7 20 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73 71 6c    labelEnd = sql
283f8 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
283f9 6c 28 76 29 3b 0a 20 20 6c 61 62 65 6c 43 6d 70  l(v);.  labelCmp
283fa 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  r = sqlite3VdbeM
283fb 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 0a 20  akeLabel(v);... 
283fc 20 2f 2a 20 50 61 74 63 68 20 75 70 20 74 68 65   /* Patch up the
283fd 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
283fe 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 2d 3e  .  */.  op = p->
283ff 6f 70 3b 20 20 0a 20 20 70 50 72 69 6f 72 20 3d  op;  .  pPrior =
28400 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73   p->pPrior;.  as
28401 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 4f  sert( pPrior->pO
28402 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 70  rderBy==0 );.  p
28403 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72  OrderBy = p->pOr
28404 64 65 72 42 79 3b 0a 20 20 61 73 73 65 72 74 28  derBy;.  assert(
28405 20 70 4f 72 64 65 72 42 79 20 29 3b 0a 20 20 6e   pOrderBy );.  n
28406 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
28407 42 79 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20 2f 2a  By->nExpr;..  /*
28408 20 46 6f 72 20 6f 70 65 72 61 74 6f 72 73 20 6f   For operators o
28409 74 68 65 72 20 74 68 61 6e 20 55 4e 49 4f 4e 20  ther than UNION 
2840a 41 4c 4c 20 77 65 20 68 61 76 65 20 74 6f 20 6d  ALL we have to m
2840b 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20 20  ake sure that.  
2840c 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ** the ORDER BY 
2840d 63 6c 61 75 73 65 20 63 6f 76 65 72 73 20 65 76  clause covers ev
2840e 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ery term of the 
2840f 72 65 73 75 6c 74 20 73 65 74 2e 20 20 41 64 64  result set.  Add
28410 0a 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f 20 74  .  ** terms to t
28411 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
28412 73 65 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  se as necessary.
28413 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 21 3d  .  */.  if( op!=
28414 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 66 6f  TK_ALL ){.    fo
28415 72 28 69 3d 31 3b 20 64 62 2d 3e 6d 61 6c 6c 6f  r(i=1; db->mallo
28416 63 46 61 69 6c 65 64 3d 3d 30 20 26 26 20 69 3c  cFailed==0 && i<
28417 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  =p->pEList->nExp
28418 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  r; i++){.      s
28419 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2841a 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20  tem *pItem;.    
2841b 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d    for(j=0, pItem
2841c 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c  =pOrderBy->a; j<
2841d 6e 4f 72 64 65 72 42 79 3b 20 6a 2b 2b 2c 20 70  nOrderBy; j++, p
2841e 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
2841f 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e   assert( pItem->
28420 69 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20  iCol>0 );.      
28421 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f    if( pItem->iCo
28422 6c 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20  l==i ) break;.  
28423 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
28424 6a 3d 3d 6e 4f 72 64 65 72 42 79 20 29 7b 0a 20  j==nOrderBy ){. 
28425 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65         Expr *pNe
28426 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  w = sqlite3PExpr
28427 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45  (pParse, TK_INTE
28428 47 45 52 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  GER, 0, 0, 0);. 
28429 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d         if( pNew=
2842a 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
2842b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
2842c 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d    pNew->flags |=
2842d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20   EP_IntValue;.  
2842e 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 54 61 62        pNew->iTab
2842f 6c 65 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20  le = i;.        
28430 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74  pOrderBy = sqlit
28431 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
28432 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
28433 79 2c 20 70 4e 65 77 2c 20 30 29 3b 0a 20 20 20  y, pNew, 0);.   
28434 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 61       pOrderBy->a
28435 5b 6e 4f 72 64 65 72 42 79 2b 2b 5d 2e 69 43 6f  [nOrderBy++].iCo
28436 6c 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 20 20  l = (u16)i;.    
28437 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
28438 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
28439 63 6f 6d 70 61 72 69 73 6f 6e 20 70 65 72 6d 75  comparison permu
2843a 74 61 74 69 6f 6e 20 61 6e 64 20 6b 65 79 69 6e  tation and keyin
2843b 66 6f 20 74 68 61 74 20 69 73 20 75 73 65 64 20  fo that is used 
2843c 77 69 74 68 0a 20 20 2a 2a 20 74 68 65 20 70 65  with.  ** the pe
2843d 72 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20 74  rmutation used t
2843e 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
2843f 68 65 20 6e 65 78 74 0a 20 20 2a 2a 20 72 6f 77  he next.  ** row
28440 20 6f 66 20 72 65 73 75 6c 74 73 20 63 6f 6d 65   of results come
28441 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 20 6f  s from selectA o
28442 72 20 73 65 6c 65 63 74 42 2e 20 20 41 6c 73 6f  r selectB.  Also
28443 20 61 64 64 20 65 78 70 6c 69 63 69 74 0a 20 20   add explicit.  
28444 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 74 6f  ** collations to
28445 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
28446 61 75 73 65 20 74 65 72 6d 73 20 73 6f 20 74 68  ause terms so th
28447 61 74 20 77 68 65 6e 20 74 68 65 20 73 75 62 71  at when the subq
28448 75 65 72 69 65 73 0a 20 20 2a 2a 20 74 6f 20 74  ueries.  ** to t
28449 68 65 20 72 69 67 68 74 20 61 6e 64 20 74 68 65  he right and the
2844a 20 6c 65 66 74 20 61 72 65 20 65 76 61 6c 75 61   left are evalua
2844b 74 65 64 2c 20 74 68 65 79 20 75 73 65 20 74 68  ted, they use th
2844c 65 20 63 6f 72 72 65 63 74 0a 20 20 2a 2a 20 63  e correct.  ** c
2844d 6f 6c 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  ollation..  */. 
2844e 20 61 50 65 72 6d 75 74 65 20 3d 20 73 71 6c 69   aPermute = sqli
2844f 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
28450 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e  b, sizeof(int)*n
28451 4f 72 64 65 72 42 79 29 3b 0a 20 20 69 66 28 20  OrderBy);.  if( 
28452 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 20  aPermute ){.    
28453 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
28454 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
28455 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
28456 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 6e  pOrderBy->a; i<n
28457 4f 72 64 65 72 42 79 3b 20 69 2b 2b 2c 20 70 49  OrderBy; i++, pI
28458 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73  tem++){.      as
28459 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 43 6f  sert( pItem->iCo
2845a 6c 3e 30 20 20 26 26 20 70 49 74 65 6d 2d 3e 69  l>0  && pItem->i
2845b 43 6f 6c 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e  Col<=p->pEList->
2845c 6e 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 61  nExpr );.      a
2845d 50 65 72 6d 75 74 65 5b 69 5d 20 3d 20 70 49 74  Permute[i] = pIt
2845e 65 6d 2d 3e 69 43 6f 6c 20 2d 20 31 3b 0a 20 20  em->iCol - 1;.  
2845f 20 20 7d 0a 20 20 20 20 70 4b 65 79 4d 65 72 67    }.    pKeyMerg
28460 65 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e =.      sqlite
28461 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
28462 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 4d 65 72   sizeof(*pKeyMer
28463 67 65 29 2b 6e 4f 72 64 65 72 42 79 2a 28 73 69  ge)+nOrderBy*(si
28464 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31  zeof(CollSeq*)+1
28465 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79  ));.    if( pKey
28466 4d 65 72 67 65 20 29 7b 0a 20 20 20 20 20 20 70  Merge ){.      p
28467 4b 65 79 4d 65 72 67 65 2d 3e 61 53 6f 72 74 4f  KeyMerge->aSortO
28468 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70 4b 65  rder = (u8*)&pKe
28469 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 6e 4f  yMerge->aColl[nO
2846a 72 64 65 72 42 79 5d 3b 0a 20 20 20 20 20 20 70  rderBy];.      p
2846b 4b 65 79 4d 65 72 67 65 2d 3e 6e 46 69 65 6c 64  KeyMerge->nField
2846c 20 3d 20 28 75 31 36 29 6e 4f 72 64 65 72 42 79   = (u16)nOrderBy
2846d 3b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67  ;.      pKeyMerg
2846e 65 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  e->enc = ENC(db)
2846f 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
28470 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b   i<nOrderBy; i++
28471 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53  ){.        CollS
28472 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  eq *pColl;.     
28473 20 20 20 45 78 70 72 20 2a 70 54 65 72 6d 20 3d     Expr *pTerm =
28474 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e   pOrderBy->a[i].
28475 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 69  pExpr;.        i
28476 66 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20  f( pTerm->flags 
28477 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20  & EP_ExpCollate 
28478 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f  ){.          pCo
28479 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e 70 43 6f 6c  ll = pTerm->pCol
2847a 6c 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  l;.        }else
2847b 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c  {.          pCol
2847c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43  l = multiSelectC
2847d 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
2847e 2c 20 61 50 65 72 6d 75 74 65 5b 69 5d 29 3b 0a  , aPermute[i]);.
2847f 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d            pTerm-
28480 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70  >flags |= EP_Exp
28481 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20  Collate;.       
28482 20 20 20 70 54 65 72 6d 2d 3e 70 43 6f 6c 6c 20     pTerm->pColl 
28483 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20  = pColl;.       
28484 20 7d 0a 20 20 20 20 20 20 20 20 70 4b 65 79 4d   }.        pKeyM
28485 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d  erge->aColl[i] =
28486 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20   pColl;.        
28487 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53 6f 72 74  pKeyMerge->aSort
28488 4f 72 64 65 72 5b 69 5d 20 3d 20 70 4f 72 64 65  Order[i] = pOrde
28489 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72  rBy->a[i].sortOr
2848a 64 65 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  der;.      }.   
2848b 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
2848c 70 4b 65 79 4d 65 72 67 65 20 3d 20 30 3b 0a 20  pKeyMerge = 0;. 
2848d 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 74 74 61 63   }..  /* Reattac
2848e 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  h the ORDER BY c
2848f 6c 61 75 73 65 20 74 6f 20 74 68 65 20 71 75 65  lause to the que
28490 72 79 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 4f  ry..  */.  p->pO
28491 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42  rderBy = pOrderB
28492 79 3b 0a 20 20 70 50 72 69 6f 72 2d 3e 70 4f 72  y;.  pPrior->pOr
28493 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45  derBy = sqlite3E
28494 78 70 72 4c 69 73 74 44 75 70 28 70 50 61 72 73  xprListDup(pPars
28495 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42 79 2c  e->db, pOrderBy,
28496 20 30 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63   0);..  /* Alloc
28497 61 74 65 20 61 20 72 61 6e 67 65 20 6f 66 20 74  ate a range of t
28498 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
28499 72 73 20 61 6e 64 20 74 68 65 20 4b 65 79 49 6e  rs and the KeyIn
2849a 66 6f 20 6e 65 65 64 65 64 0a 20 20 2a 2a 20 66  fo needed.  ** f
2849b 6f 72 20 74 68 65 20 6c 6f 67 69 63 20 74 68 61  or the logic tha
2849c 74 20 72 65 6d 6f 76 65 73 20 64 75 70 6c 69 63  t removes duplic
2849d 61 74 65 20 72 65 73 75 6c 74 20 72 6f 77 73 20  ate result rows 
2849e 77 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 70  when the.  ** op
2849f 65 72 61 74 6f 72 20 69 73 20 55 4e 49 4f 4e 2c  erator is UNION,
284a0 20 45 58 43 45 50 54 2c 20 6f 72 20 49 4e 54 45   EXCEPT, or INTE
284a1 52 53 45 43 54 20 28 62 75 74 20 6e 6f 74 20 55  RSECT (but not U
284a2 4e 49 4f 4e 20 41 4c 4c 29 2e 0a 20 20 2a 2f 0a  NION ALL)..  */.
284a3 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c    if( op==TK_ALL
284a4 20 29 7b 0a 20 20 20 20 72 65 67 50 72 65 76 20   ){.    regPrev 
284a5 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
284a6 20 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70 2d    int nExpr = p-
284a7 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a  >pEList->nExpr;.
284a8 20 20 20 20 61 73 73 65 72 74 28 20 6e 4f 72 64      assert( nOrd
284a9 65 72 42 79 3e 3d 6e 45 78 70 72 20 7c 7c 20 64  erBy>=nExpr || d
284aa 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
284ab 29 3b 0a 20 20 20 20 72 65 67 50 72 65 76 20 3d  );.    regPrev =
284ac 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
284ad 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 45 78  ange(pParse, nEx
284ae 70 72 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  pr+1);.    sqlit
284af 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
284b0 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72  OP_Integer, 0, r
284b1 65 67 50 72 65 76 29 3b 0a 20 20 20 20 70 4b 65  egPrev);.    pKe
284b2 79 44 75 70 20 3d 20 73 71 6c 69 74 65 33 44 62  yDup = sqlite3Db
284b3 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 0a 20  MallocZero(db,. 
284b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284b5 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 44 75 70   sizeof(*pKeyDup
284b6 29 20 2b 20 6e 45 78 70 72 2a 28 73 69 7a 65 6f  ) + nExpr*(sizeo
284b7 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31 29 20 29  f(CollSeq*)+1) )
284b8 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79 44 75  ;.    if( pKeyDu
284b9 70 20 29 7b 0a 20 20 20 20 20 20 70 4b 65 79 44  p ){.      pKeyD
284ba 75 70 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d  up->aSortOrder =
284bb 20 28 75 38 2a 29 26 70 4b 65 79 44 75 70 2d 3e   (u8*)&pKeyDup->
284bc 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20  aColl[nExpr];.  
284bd 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 6e 46 69      pKeyDup->nFi
284be 65 6c 64 20 3d 20 28 75 31 36 29 6e 45 78 70 72  eld = (u16)nExpr
284bf 3b 0a 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d  ;.      pKeyDup-
284c0 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a  >enc = ENC(db);.
284c1 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
284c2 3c 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  <nExpr; i++){.  
284c3 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61        pKeyDup->a
284c4 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75 6c 74 69 53  Coll[i] = multiS
284c5 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61  electCollSeq(pPa
284c6 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20 20  rse, p, i);.    
284c7 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f      pKeyDup->aSo
284c8 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 30 3b 0a  rtOrder[i] = 0;.
284c9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
284ca 7d 0a 20 0a 20 20 2f 2a 20 53 65 70 61 72 61 74  }. .  /* Separat
284cb 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 74  e the left and t
284cc 68 65 20 72 69 67 68 74 20 71 75 65 72 79 20 66  he right query f
284cd 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 0a  rom one another.
284ce 20 20 2a 2f 0a 20 20 70 2d 3e 70 50 72 69 6f 72    */.  p->pPrior
284cf 20 3d 20 30 3b 0a 20 20 70 50 72 69 6f 72 2d 3e   = 0;.  pPrior->
284d0 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a  pRightmost = 0;.
284d1 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65    sqlite3Resolve
284d2 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61  OrderGroupBy(pPa
284d3 72 73 65 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65  rse, p, p->pOrde
284d4 72 42 79 2c 20 22 4f 52 44 45 52 22 29 3b 0a 20  rBy, "ORDER");. 
284d5 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 50 72   if( pPrior->pPr
284d6 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ior==0 ){.    sq
284d7 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65  lite3ResolveOrde
284d8 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65 2c  rGroupBy(pParse,
284d9 20 70 50 72 69 6f 72 2c 20 70 50 72 69 6f 72 2d   pPrior, pPrior-
284da 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44 45  >pOrderBy, "ORDE
284db 52 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  R");.  }..  /* C
284dc 6f 6d 70 75 74 65 20 74 68 65 20 6c 69 6d 69 74  ompute the limit
284dd 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20 20   registers */.  
284de 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69  computeLimitRegi
284df 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c  sters(pParse, p,
284e0 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 69 66   labelEnd);.  if
284e1 28 20 70 2d 3e 69 4c 69 6d 69 74 20 26 26 20 6f  ( p->iLimit && o
284e2 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20  p==TK_ALL ){.   
284e3 20 72 65 67 4c 69 6d 69 74 41 20 3d 20 2b 2b 70   regLimitA = ++p
284e4 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
284e5 20 72 65 67 4c 69 6d 69 74 42 20 3d 20 2b 2b 70   regLimitB = ++p
284e6 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
284e7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
284e8 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 70  p2(v, OP_Copy, p
284e9 2d 3e 69 4f 66 66 73 65 74 20 3f 20 70 2d 3e 69  ->iOffset ? p->i
284ea 4f 66 66 73 65 74 2b 31 20 3a 20 70 2d 3e 69 4c  Offset+1 : p->iL
284eb 69 6d 69 74 2c 0a 20 20 20 20 20 20 20 20 20 20  imit,.          
284ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284ed 20 20 20 20 20 20 20 20 72 65 67 4c 69 6d 69 74          regLimit
284ee 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  A);.    sqlite3V
284ef 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
284f0 43 6f 70 79 2c 20 72 65 67 4c 69 6d 69 74 41 2c  Copy, regLimitA,
284f1 20 72 65 67 4c 69 6d 69 74 42 29 3b 0a 20 20 7d   regLimitB);.  }
284f2 65 6c 73 65 7b 0a 20 20 20 20 72 65 67 4c 69 6d  else{.    regLim
284f3 69 74 41 20 3d 20 72 65 67 4c 69 6d 69 74 42 20  itA = regLimitB 
284f4 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
284f5 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
284f6 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 70   p->pLimit);.  p
284f7 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->pLimit = 0;.  
284f8 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
284f9 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74  e(db, p->pOffset
284fa 29 3b 0a 20 20 70 2d 3e 70 4f 66 66 73 65 74 20  );.  p->pOffset 
284fb 3d 20 30 3b 0a 0a 20 20 72 65 67 41 64 64 72 41  = 0;..  regAddrA
284fc 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
284fd 6d 3b 0a 20 20 72 65 67 45 6f 66 41 20 3d 20 2b  m;.  regEofA = +
284fe 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
284ff 20 72 65 67 41 64 64 72 42 20 3d 20 2b 2b 70 50   regAddrB = ++pP
28500 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65  arse->nMem;.  re
28501 67 45 6f 66 42 20 3d 20 2b 2b 70 50 61 72 73 65  gEofB = ++pParse
28502 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74  ->nMem;.  regOut
28503 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  A = ++pParse->nM
28504 65 6d 3b 0a 20 20 72 65 67 4f 75 74 42 20 3d 20  em;.  regOutB = 
28505 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
28506 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
28507 65 73 74 49 6e 69 74 28 26 64 65 73 74 41 2c 20  estInit(&destA, 
28508 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72  SRT_Coroutine, r
28509 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71 6c 69  egAddrA);.  sqli
2850a 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
2850b 74 28 26 64 65 73 74 42 2c 20 53 52 54 5f 43 6f  t(&destB, SRT_Co
2850c 72 6f 75 74 69 6e 65 2c 20 72 65 67 41 64 64 72  routine, regAddr
2850d 42 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 70  B);..  /* Jump p
2850e 61 73 74 20 74 68 65 20 76 61 72 69 6f 75 73 20  ast the various 
2850f 73 75 62 72 6f 75 74 69 6e 65 73 20 61 6e 64 20  subroutines and 
28510 63 6f 72 6f 75 74 69 6e 65 73 20 74 6f 20 74 68  coroutines to th
28511 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 6d 65 72 67  e main.  ** merg
28512 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 6a 31  e loop.  */.  j1
28513 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
28514 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29  dOp0(v, OP_Goto)
28515 3b 0a 20 20 61 64 64 72 53 65 6c 65 63 74 41 20  ;.  addrSelectA 
28516 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
28517 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a 0a 20  rentAddr(v);... 
28518 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 63   /* Generate a c
28519 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 76 61 6c  oroutine to eval
2851a 75 61 74 65 20 74 68 65 20 53 45 4c 45 43 54 20  uate the SELECT 
2851b 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74 68 65  statement to the
2851c 0a 20 20 2a 2a 20 6c 65 66 74 20 6f 66 20 74 68  .  ** left of th
2851d 65 20 63 6f 6d 70 6f 75 6e 64 20 6f 70 65 72 61  e compound opera
2851e 74 6f 72 20 2d 20 74 68 65 20 22 41 22 20 73 65  tor - the "A" se
2851f 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64 62  lect..  */.  Vdb
28520 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
28521 20 22 42 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e   "Begin coroutin
28522 65 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45 43  e for left SELEC
28523 54 22 29 29 3b 0a 20 20 70 50 72 69 6f 72 2d 3e  T"));.  pPrior->
28524 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69 6d 69  iLimit = regLimi
28525 74 41 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  tA;.  sqlite3Sel
28526 65 63 74 28 70 50 61 72 73 65 2c 20 70 50 72 69  ect(pParse, pPri
28527 6f 72 2c 20 26 64 65 73 74 41 29 3b 0a 20 20 73  or, &destA);.  s
28528 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28529 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2852a 31 2c 20 72 65 67 45 6f 66 41 29 3b 0a 20 20 73  1, regEofA);.  s
2852b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2852c 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
2852d 67 41 64 64 72 41 29 3b 0a 20 20 56 64 62 65 4e  gAddrA);.  VdbeN
2852e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
2852f 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f  End coroutine fo
28530 72 20 6c 65 66 74 20 53 45 4c 45 43 54 22 29 29  r left SELECT"))
28531 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ;..  /* Generate
28532 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20   a coroutine to 
28533 65 76 61 6c 75 61 74 65 20 74 68 65 20 53 45 4c  evaluate the SEL
28534 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e  ECT statement on
28535 20 0a 20 20 2a 2a 20 74 68 65 20 72 69 67 68 74   .  ** the right
28536 20 2d 20 74 68 65 20 22 42 22 20 73 65 6c 65 63   - the "B" selec
28537 74 0a 20 20 2a 2f 0a 20 20 61 64 64 72 53 65 6c  t.  */.  addrSel
28538 65 63 74 42 20 3d 20 73 71 6c 69 74 65 33 56 64  ectB = sqlite3Vd
28539 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2853a 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ;.  VdbeNoopComm
2853b 65 6e 74 28 28 76 2c 20 22 42 65 67 69 6e 20 63  ent((v, "Begin c
2853c 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 69 67  oroutine for rig
2853d 68 74 20 53 45 4c 45 43 54 22 29 29 3b 0a 20 20  ht SELECT"));.  
2853e 73 61 76 65 64 4c 69 6d 69 74 20 3d 20 70 2d 3e  savedLimit = p->
2853f 69 4c 69 6d 69 74 3b 0a 20 20 73 61 76 65 64 4f  iLimit;.  savedO
28540 66 66 73 65 74 20 3d 20 70 2d 3e 69 4f 66 66 73  ffset = p->iOffs
28541 65 74 3b 0a 20 20 70 2d 3e 69 4c 69 6d 69 74 20  et;.  p->iLimit 
28542 3d 20 72 65 67 4c 69 6d 69 74 42 3b 0a 20 20 70  = regLimitB;.  p
28543 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 20 20  ->iOffset = 0;  
28544 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
28545 28 70 50 61 72 73 65 2c 20 70 2c 20 26 64 65 73  (pParse, p, &des
28546 74 42 29 3b 0a 20 20 70 2d 3e 69 4c 69 6d 69 74  tB);.  p->iLimit
28547 20 3d 20 73 61 76 65 64 4c 69 6d 69 74 3b 0a 20   = savedLimit;. 
28548 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 73 61   p->iOffset = sa
28549 76 65 64 4f 66 66 73 65 74 3b 0a 20 20 73 71 6c  vedOffset;.  sql
2854a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2854b 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
2854c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20 73 71 6c   regEofB);.  sql
2854d 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2854e 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41  , OP_Yield, regA
2854f 64 64 72 42 29 3b 0a 20 20 56 64 62 65 4e 6f 6f  ddrB);.  VdbeNoo
28550 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e  pComment((v, "En
28551 64 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20  d coroutine for 
28552 72 69 67 68 74 20 53 45 4c 45 43 54 22 29 29 3b  right SELECT"));
28553 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
28554 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61  a subroutine tha
28555 74 20 6f 75 74 70 75 74 73 20 74 68 65 20 63 75  t outputs the cu
28556 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65  rrent row of the
28557 20 41 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20 61   A.  ** select a
28558 73 20 74 68 65 20 6e 65 78 74 20 6f 75 74 70 75  s the next outpu
28559 74 20 72 6f 77 20 6f 66 20 74 68 65 20 63 6f 6d  t row of the com
2855a 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a 20 20  pound select..  
2855b 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  */.  VdbeNoopCom
2855c 6d 65 6e 74 28 28 76 2c 20 22 4f 75 74 70 75 74  ment((v, "Output
2855d 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 41 22 29   routine for A")
2855e 29 3b 0a 20 20 61 64 64 72 4f 75 74 41 20 3d 20  );.  addrOutA = 
2855f 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75  generateOutputSu
28560 62 72 6f 75 74 69 6e 65 28 70 50 61 72 73 65 2c  broutine(pParse,
28561 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28562 20 20 70 2c 20 26 64 65 73 74 41 2c 20 70 44 65    p, &destA, pDe
28563 73 74 2c 20 72 65 67 4f 75 74 41 2c 0a 20 20 20  st, regOutA,.   
28564 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
28565 67 50 72 65 76 2c 20 70 4b 65 79 44 75 70 2c 20  gPrev, pKeyDup, 
28566 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
28567 46 46 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20  FF, labelEnd);. 
28568 20 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20   .  /* Generate 
28569 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61  a subroutine tha
2856a 74 20 6f 75 74 70 75 74 73 20 74 68 65 20 63 75  t outputs the cu
2856b 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65  rrent row of the
2856c 20 42 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20 61   B.  ** select a
2856d 73 20 74 68 65 20 6e 65 78 74 20 6f 75 74 70 75  s the next outpu
2856e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 63 6f 6d  t row of the com
2856f 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a 20 20  pound select..  
28570 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  */.  if( op==TK_
28571 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e  ALL || op==TK_UN
28572 49 4f 4e 20 29 7b 0a 20 20 20 20 56 64 62 65 4e  ION ){.    VdbeN
28573 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
28574 4f 75 74 70 75 74 20 72 6f 75 74 69 6e 65 20 66  Output routine f
28575 6f 72 20 42 22 29 29 3b 0a 20 20 20 20 61 64 64  or B"));.    add
28576 72 4f 75 74 42 20 3d 20 67 65 6e 65 72 61 74 65  rOutB = generate
28577 4f 75 74 70 75 74 53 75 62 72 6f 75 74 69 6e 65  OutputSubroutine
28578 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
28579 20 20 20 20 20 20 20 20 20 20 70 2c 20 26 64 65            p, &de
2857a 73 74 42 2c 20 70 44 65 73 74 2c 20 72 65 67 4f  stB, pDest, regO
2857b 75 74 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20  utB,.           
2857c 20 20 20 20 20 20 72 65 67 50 72 65 76 2c 20 70        regPrev, p
2857d 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45 59 49 4e  KeyDup, P4_KEYIN
2857e 46 4f 5f 53 54 41 54 49 43 2c 20 6c 61 62 65 6c  FO_STATIC, label
2857f 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  End);.  }..  /* 
28580 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
28581 75 74 69 6e 65 20 74 6f 20 72 75 6e 20 77 68 65  utine to run whe
28582 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 66 72  n the results fr
28583 6f 6d 20 73 65 6c 65 63 74 20 41 0a 20 20 2a 2a  om select A.  **
28584 20 61 72 65 20 65 78 68 61 75 73 74 65 64 20 61   are exhausted a
28585 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20 69 6e 20  nd only data in 
28586 73 65 6c 65 63 74 20 42 20 72 65 6d 61 69 6e 73  select B remains
28587 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f  ..  */.  VdbeNoo
28588 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6f  pComment((v, "eo
28589 66 2d 41 20 73 75 62 72 6f 75 74 69 6e 65 22 29  f-A subroutine")
2858a 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  );.  if( op==TK_
2858b 45 58 43 45 50 54 20 7c 7c 20 6f 70 3d 3d 54 4b  EXCEPT || op==TK
2858c 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20  _INTERSECT ){.  
2858d 20 20 61 64 64 72 45 6f 66 41 20 3d 20 73 71 6c    addrEofA = sql
2858e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2858f 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61  , OP_Goto, 0, la
28590 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 65 6c 73 65  belEnd);.  }else
28591 7b 20 20 0a 20 20 20 20 61 64 64 72 45 6f 66 41  {  .    addrEofA
28592 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
28593 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp2(v, OP_If, r
28594 65 67 45 6f 66 42 2c 20 6c 61 62 65 6c 45 6e 64  egEofB, labelEnd
28595 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
28596 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
28597 6f 73 75 62 2c 20 72 65 67 4f 75 74 42 2c 20 61  osub, regOutB, a
28598 64 64 72 4f 75 74 42 29 3b 0a 20 20 20 20 73 71  ddrOutB);.    sq
28599 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2859a 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67  v, OP_Yield, reg
2859b 41 64 64 72 42 29 3b 0a 20 20 20 20 73 71 6c 69  AddrB);.    sqli
2859c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2859d 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64   OP_Goto, 0, add
2859e 72 45 6f 66 41 29 3b 0a 20 20 7d 0a 0a 20 20 2f  rEofA);.  }..  /
2859f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62  * Generate a sub
285a0 72 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20 77  routine to run w
285a1 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20  hen the results 
285a2 66 72 6f 6d 20 73 65 6c 65 63 74 20 42 0a 20 20  from select B.  
285a3 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74 65 64  ** are exhausted
285a4 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20 69   and only data i
285a5 6e 20 73 65 6c 65 63 74 20 41 20 72 65 6d 61 69  n select A remai
285a6 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f  ns..  */.  if( o
285a7 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20  p==TK_INTERSECT 
285a8 29 7b 0a 20 20 20 20 61 64 64 72 45 6f 66 42 20  ){.    addrEofB 
285a9 3d 20 61 64 64 72 45 6f 66 41 3b 0a 20 20 7d 65  = addrEofA;.  }e
285aa 6c 73 65 7b 20 20 0a 20 20 20 20 56 64 62 65 4e  lse{  .    VdbeN
285ab 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
285ac 65 6f 66 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  eof-B subroutine
285ad 22 29 29 3b 0a 20 20 20 20 61 64 64 72 45 6f 66  "));.    addrEof
285ae 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  B = sqlite3VdbeA
285af 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
285b0 72 65 67 45 6f 66 41 2c 20 6c 61 62 65 6c 45 6e  regEofA, labelEn
285b1 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
285b2 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
285b3 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 41 2c 20  Gosub, regOutA, 
285b4 61 64 64 72 4f 75 74 41 29 3b 0a 20 20 20 20 73  addrOutA);.    s
285b5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
285b6 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
285b7 67 41 64 64 72 41 29 3b 0a 20 20 20 20 73 71 6c  gAddrA);.    sql
285b8 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
285b9 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64  , OP_Goto, 0, ad
285ba 64 72 45 6f 66 42 29 3b 0a 20 20 7d 0a 0a 20 20  drEofB);.  }..  
285bb 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
285bc 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63   to handle the c
285bd 61 73 65 20 6f 66 20 41 3c 42 0a 20 20 2a 2f 0a  ase of A<B.  */.
285be 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
285bf 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42 20 73 75  t((v, "A-lt-B su
285c0 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 61  broutine"));.  a
285c1 64 64 72 41 6c 74 42 20 3d 20 73 71 6c 69 74 65  ddrAltB = sqlite
285c2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
285c3 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 41  P_Gosub, regOutA
285c4 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 20 73  , addrOutA);.  s
285c5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
285c6 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
285c7 67 41 64 64 72 41 29 3b 0a 20 20 73 71 6c 69 74  gAddrA);.  sqlit
285c8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
285c9 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20  OP_If, regEofA, 
285ca 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 73 71 6c  addrEofA);.  sql
285cb 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
285cc 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61  , OP_Goto, 0, la
285cd 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20  belCmpr);..  /* 
285ce 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
285cf 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61 73 65   handle the case
285d0 20 6f 66 20 41 3d 3d 42 0a 20 20 2a 2f 0a 20 20   of A==B.  */.  
285d1 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29  if( op==TK_ALL )
285d2 7b 0a 20 20 20 20 61 64 64 72 41 65 71 42 20 3d  {.    addrAeqB =
285d3 20 61 64 64 72 41 6c 74 42 3b 0a 20 20 7d 65 6c   addrAltB;.  }el
285d4 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e  se if( op==TK_IN
285d5 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20 20 61  TERSECT ){.    a
285d6 64 64 72 41 65 71 42 20 3d 20 61 64 64 72 41 6c  ddrAeqB = addrAl
285d7 74 42 3b 0a 20 20 20 20 61 64 64 72 41 6c 74 42  tB;.    addrAltB
285d8 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ++;.  }else{.   
285d9 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
285da 28 28 76 2c 20 22 41 2d 65 71 2d 42 20 73 75 62  ((v, "A-eq-B sub
285db 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20  routine"));.    
285dc 61 64 64 72 41 65 71 42 20 3d 0a 20 20 20 20 73  addrAeqB =.    s
285dd 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
285de 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
285df 67 41 64 64 72 41 29 3b 0a 20 20 20 20 73 71 6c  gAddrA);.    sql
285e0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
285e1 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41  , OP_If, regEofA
285e2 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 20  , addrEofA);.   
285e3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
285e4 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
285e5 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20  , labelCmpr);.  
285e6 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
285e7 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20   code to handle 
285e8 74 68 65 20 63 61 73 65 20 6f 66 20 41 3e 42 0a  the case of A>B.
285e9 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43    */.  VdbeNoopC
285ea 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 67 74  omment((v, "A-gt
285eb 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29  -B subroutine"))
285ec 3b 0a 20 20 61 64 64 72 41 67 74 42 20 3d 20 73  ;.  addrAgtB = s
285ed 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
285ee 74 41 64 64 72 28 76 29 3b 0a 20 20 69 66 28 20  tAddr(v);.  if( 
285ef 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70  op==TK_ALL || op
285f0 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20  ==TK_UNION ){.  
285f1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
285f2 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
285f3 20 72 65 67 4f 75 74 42 2c 20 61 64 64 72 4f 75   regOutB, addrOu
285f4 74 42 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  tB);.  }.  sqlit
285f5 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
285f6 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64 64  OP_Yield, regAdd
285f7 72 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  rB);.  sqlite3Vd
285f8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
285f9 66 2c 20 72 65 67 45 6f 66 42 2c 20 61 64 64 72  f, regEofB, addr
285fa 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69 74 65 33  EofB);.  sqlite3
285fb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
285fc 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43  _Goto, 0, labelC
285fd 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  mpr);..  /* This
285fe 20 63 6f 64 65 20 72 75 6e 73 20 6f 6e 63 65 20   code runs once 
285ff 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 65 76  to initialize ev
28600 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20  erything..  */. 
28601 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
28602 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 73  Here(v, j1);.  s
28603 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28604 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
28605 30 2c 20 72 65 67 45 6f 66 41 29 3b 0a 20 20 73  0, regEofA);.  s
28606 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28607 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
28608 30 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20 73  0, regEofB);.  s
28609 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2860a 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
2860b 67 41 64 64 72 41 2c 20 61 64 64 72 53 65 6c 65  gAddrA, addrSele
2860c 63 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ctA);.  sqlite3V
2860d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2860e 47 6f 73 75 62 2c 20 72 65 67 41 64 64 72 42 2c  Gosub, regAddrB,
2860f 20 61 64 64 72 53 65 6c 65 63 74 42 29 3b 0a 20   addrSelectB);. 
28610 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28611 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67  p2(v, OP_If, reg
28612 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b  EofA, addrEofA);
28613 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
28614 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp2(v, OP_If, r
28615 65 67 45 6f 66 42 2c 20 61 64 64 72 45 6f 66 42  egEofB, addrEofB
28616 29 3b 0a 0a 20 20 2f 2a 20 49 6d 70 6c 65 6d 65  );..  /* Impleme
28617 6e 74 20 74 68 65 20 6d 61 69 6e 20 6d 65 72 67  nt the main merg
28618 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 73 71  e loop.  */.  sq
28619 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2861a 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65 6c 43 6d  Label(v, labelCm
2861b 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  pr);.  sqlite3Vd
2861c 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
2861d 65 72 6d 75 74 61 74 69 6f 6e 2c 20 30 2c 20 30  ermutation, 0, 0
2861e 2c 20 30 2c 20 28 63 68 61 72 2a 29 61 50 65 72  , 0, (char*)aPer
2861f 6d 75 74 65 2c 20 50 34 5f 49 4e 54 41 52 52 41  mute, P4_INTARRA
28620 59 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  Y);.  sqlite3Vdb
28621 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f  eAddOp4(v, OP_Co
28622 6d 70 61 72 65 2c 20 64 65 73 74 41 2e 69 4d 65  mpare, destA.iMe
28623 6d 2c 20 64 65 73 74 42 2e 69 4d 65 6d 2c 20 6e  m, destB.iMem, n
28624 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20 20 20  OrderBy,.       
28625 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28626 20 20 28 63 68 61 72 2a 29 70 4b 65 79 4d 65 72    (char*)pKeyMer
28627 67 65 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  ge, P4_KEYINFO_H
28628 41 4e 44 4f 46 46 29 3b 0a 20 20 73 71 6c 69 74  ANDOFF);.  sqlit
28629 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2862a 4f 50 5f 4a 75 6d 70 2c 20 61 64 64 72 41 6c 74  OP_Jump, addrAlt
2862b 42 2c 20 61 64 64 72 41 65 71 42 2c 20 61 64 64  B, addrAeqB, add
2862c 72 41 67 74 42 29 3b 0a 0a 20 20 2f 2a 20 52 65  rAgtB);..  /* Re
2862d 6c 65 61 73 65 20 74 65 6d 70 6f 72 61 72 79 20  lease temporary 
2862e 72 65 67 69 73 74 65 72 73 0a 20 20 2a 2f 0a 20  registers.  */. 
2862f 20 69 66 28 20 72 65 67 50 72 65 76 20 29 7b 0a   if( regPrev ){.
28630 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
28631 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
28632 73 65 2c 20 72 65 67 50 72 65 76 2c 20 6e 4f 72  se, regPrev, nOr
28633 64 65 72 42 79 2b 31 29 3b 0a 20 20 7d 0a 0a 20  derBy+1);.  }.. 
28634 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20   /* Jump to the 
28635 74 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 6f 72  this point in or
28636 64 65 72 20 74 6f 20 74 65 72 6d 69 6e 61 74 65  der to terminate
28637 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a 2f   the query..  */
28638 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
28639 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c 61  solveLabel(v, la
2863a 62 65 6c 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 53  belEnd);..  /* S
2863b 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  et the number of
2863c 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d 6e 73 0a   output columns.
2863d 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65 73 74    */.  if( pDest
2863e 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  ->eDest==SRT_Out
2863f 70 75 74 20 29 7b 0a 20 20 20 20 53 65 6c 65 63  put ){.    Selec
28640 74 20 2a 70 46 69 72 73 74 20 3d 20 70 50 72 69  t *pFirst = pPri
28641 6f 72 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  or;.    while( p
28642 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20  First->pPrior ) 
28643 70 46 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d  pFirst = pFirst-
28644 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 67 65 6e  >pPrior;.    gen
28645 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73  erateColumnNames
28646 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 69 72  (pParse, 0, pFir
28647 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 7d  st->pEList);.  }
28648 0a 0a 20 20 2f 2a 20 52 65 61 73 73 65 6d 62 6c  ..  /* Reassembl
28649 79 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71  y the compound q
2864a 75 65 72 79 20 73 6f 20 74 68 61 74 20 69 74 20  uery so that it 
2864b 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 63 6f  will be freed co
2864c 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20 62 79 20  rrectly.  ** by 
2864d 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
2864e 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 70 2d  tion */.  if( p-
2864f 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 73  >pPrior ){.    s
28650 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
28651 74 65 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72  te(db, p->pPrior
28652 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 50 72 69  );.  }.  p->pPri
28653 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 0a 20 20  or = pPrior;..  
28654 2f 2a 2a 2a 20 54 42 44 3a 20 20 49 6e 73 65 72  /*** TBD:  Inser
28655 74 20 73 75 62 72 6f 75 74 69 6e 65 20 63 61 6c  t subroutine cal
28656 6c 73 20 74 6f 20 63 6c 6f 73 65 20 63 75 72 73  ls to close curs
28657 6f 72 73 20 6f 6e 20 69 6e 63 6f 6d 70 6c 65 74  ors on incomplet
28658 65 0a 20 20 2a 2a 2a 2a 20 73 75 62 71 75 65 72  e.  **** subquer
28659 69 65 73 20 2a 2a 2a 2a 2f 0a 20 20 72 65 74 75  ies ****/.  retu
2865a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2865b 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66  #endif..#if !def
2865c 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2865d 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64  _SUBQUERY) || !d
2865e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2865f 49 54 5f 56 49 45 57 29 0a 2f 2a 20 46 6f 72 77  IT_VIEW)./* Forw
28660 61 72 64 20 44 65 63 6c 61 72 61 74 69 6f 6e 73  ard Declarations
28661 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
28662 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 73 71  substExprList(sq
28663 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74  lite3*, ExprList
28664 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74  *, int, ExprList
28665 2a 29 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20  *);.static void 
28666 73 75 62 73 74 53 65 6c 65 63 74 28 73 71 6c 69  substSelect(sqli
28667 74 65 33 2a 2c 20 53 65 6c 65 63 74 20 2a 2c 20  te3*, Select *, 
28668 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 29  int, ExprList *)
28669 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68  ;../*.** Scan th
2866a 72 6f 75 67 68 20 74 68 65 20 65 78 70 72 65 73  rough the expres
2866b 73 69 6f 6e 20 70 45 78 70 72 2e 20 20 52 65 70  sion pExpr.  Rep
2866c 6c 61 63 65 20 65 76 65 72 79 20 72 65 66 65 72  lace every refer
2866d 65 6e 63 65 20 74 6f 0a 2a 2a 20 61 20 63 6f 6c  ence to.** a col
2866e 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 6e 75 6d  umn in table num
2866f 62 65 72 20 69 54 61 62 6c 65 20 77 69 74 68 20  ber iTable with 
28670 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43  a copy of the iC
28671 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65 6e 74 72  olumn-th.** entr
28672 79 20 69 6e 20 70 45 4c 69 73 74 2e 20 20 28 42  y in pEList.  (B
28673 75 74 20 6c 65 61 76 65 20 72 65 66 65 72 65 6e  ut leave referen
28674 63 65 73 20 74 6f 20 74 68 65 20 52 4f 57 49 44  ces to the ROWID
28675 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75 6e 63 68   column .** unch
28676 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a 20 54 68  anged.).**.** Th
28677 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 70 61  is routine is pa
28678 72 74 20 6f 66 20 74 68 65 20 66 6c 61 74 74 65  rt of the flatte
28679 6e 69 6e 67 20 70 72 6f 63 65 64 75 72 65 2e 20  ning procedure. 
2867a 20 41 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 77   A subquery.** w
2867b 68 6f 73 65 20 72 65 73 75 6c 74 20 73 65 74 20  hose result set 
2867c 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 70 45  is defined by pE
2867d 4c 69 73 74 20 61 70 70 65 61 72 73 20 61 73 20  List appears as 
2867e 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 2a 2a 20  entry in the.** 
2867f 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
28680 20 53 45 4c 45 43 54 20 73 75 63 68 20 74 68 61   SELECT such tha
28681 74 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f  t the VDBE curso
28682 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  r assigned to th
28683 61 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c 61 75 73  at.** FORM claus
28684 65 20 65 6e 74 72 79 20 69 73 20 69 54 61 62 6c  e entry is iTabl
28685 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
28686 20 6d 61 6b 65 20 74 68 65 20 6e 65 63 65 73 73   make the necess
28687 61 72 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20  ary .** changes 
28688 74 6f 20 70 45 78 70 72 20 73 6f 20 74 68 61 74  to pExpr so that
28689 20 69 74 20 72 65 66 65 72 73 20 64 69 72 65 63   it refers direc
2868a 74 6c 79 20 74 6f 20 74 68 65 20 73 6f 75 72 63  tly to the sourc
2868b 65 20 74 61 62 6c 65 0a 2a 2a 20 6f 66 20 74 68  e table.** of th
2868c 65 20 73 75 62 71 75 65 72 79 20 72 61 74 68 65  e subquery rathe
2868d 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  r the result set
2868e 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
2868f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
28690 20 73 75 62 73 74 45 78 70 72 28 0a 20 20 73 71   substExpr(.  sq
28691 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
28692 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c    /* Report mall
28693 6f 63 20 65 72 72 6f 72 73 20 74 6f 20 74 68 69  oc errors to thi
28694 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  s connection */.
28695 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20    Expr *pExpr,  
28696 20 20 20 20 20 20 2f 2a 20 45 78 70 72 20 69 6e        /* Expr in
28697 20 77 68 69 63 68 20 73 75 62 73 74 69 74 75 74   which substitut
28698 69 6f 6e 20 6f 63 63 75 72 73 20 2a 2f 0a 20 20  ion occurs */.  
28699 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20  int iTable,     
2869a 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20      /* Table to 
2869b 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20 2a  be substituted *
2869c 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2869d 4c 69 73 74 20 20 20 20 2f 2a 20 53 75 62 73 74  List    /* Subst
2869e 69 74 75 74 65 20 65 78 70 72 65 73 73 69 6f 6e  itute expression
2869f 73 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45  s */.){.  if( pE
286a0 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  xpr==0 ) return;
286a1 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
286a2 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70  ==TK_COLUMN && p
286a3 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54  Expr->iTable==iT
286a4 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20  able ){.    if( 
286a5 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30  pExpr->iColumn<0
286a6 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d   ){.      pExpr-
286a7 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20  >op = TK_NULL;. 
286a8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
286a9 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 20 20  Expr *pNew;.    
286aa 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
286ab 21 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 69 43  !=0 && pExpr->iC
286ac 6f 6c 75 6d 6e 3c 70 45 4c 69 73 74 2d 3e 6e 45  olumn<pEList->nE
286ad 78 70 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73  xpr );.      ass
286ae 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66  ert( pExpr->pLef
286af 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 70  t==0 && pExpr->p
286b0 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20  Right==0 );.    
286b1 20 20 70 4e 65 77 20 3d 20 70 45 4c 69 73 74 2d    pNew = pEList-
286b2 3e 61 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  >a[pExpr->iColum
286b3 6e 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  n].pExpr;.      
286b4 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20  assert( pNew!=0 
286b5 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  );.      pExpr->
286b6 6f 70 20 3d 20 70 4e 65 77 2d 3e 6f 70 3b 0a 20  op = pNew->op;. 
286b7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
286b8 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 29 3b 0a  pr->pLeft==0 );.
286b9 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 4c 65        pExpr->pLe
286ba 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
286bb 44 75 70 28 64 62 2c 20 70 4e 65 77 2d 3e 70 4c  Dup(db, pNew->pL
286bc 65 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 61  eft, 0);.      a
286bd 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 52  ssert( pExpr->pR
286be 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ight==0 );.     
286bf 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d   pExpr->pRight =
286c0 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
286c1 64 62 2c 20 70 4e 65 77 2d 3e 70 52 69 67 68 74  db, pNew->pRight
286c2 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 45 78 70  , 0);.      pExp
286c3 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 4e 65 77  r->iTable = pNew
286c4 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
286c5 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 4e  pExpr->pTab = pN
286c6 65 77 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20  ew->pTab;.      
286c7 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
286c8 20 70 4e 65 77 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a   pNew->iColumn;.
286c9 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67        pExpr->iAg
286ca 67 20 3d 20 70 4e 65 77 2d 3e 69 41 67 67 3b 0a  g = pNew->iAgg;.
286cb 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 6f 6b        sqlite3Tok
286cc 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 45 78 70  enCopy(db, &pExp
286cd 72 2d 3e 74 6f 6b 65 6e 2c 20 26 70 4e 65 77 2d  r->token, &pNew-
286ce 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 73  >token);.      s
286cf 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28  qlite3TokenCopy(
286d0 64 62 2c 20 26 70 45 78 70 72 2d 3e 73 70 61 6e  db, &pExpr->span
286d1 2c 20 26 70 4e 65 77 2d 3e 73 70 61 6e 29 3b 0a  , &pNew->span);.
286d2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
286d3 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30 20  xpr->x.pList==0 
286d4 26 26 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c  && pExpr->x.pSel
286d5 65 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ect==0 );.      
286d6 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
286d7 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 78 49 73  rty(pNew, EP_xIs
286d8 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20  Select) ){.     
286d9 20 20 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c     pExpr->x.pSel
286da 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
286db 65 63 74 44 75 70 28 64 62 2c 20 70 4e 65 77 2d  ectDup(db, pNew-
286dc 3e 78 2e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  >x.pSelect, 0);.
286dd 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
286de 20 20 20 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c       pExpr->x.pL
286df 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
286e0 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 4e 65  rListDup(db, pNe
286e1 77 2d 3e 78 2e 70 4c 69 73 74 2c 20 30 29 3b 0a  w->x.pList, 0);.
286e2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 45        }.      pE
286e3 78 70 72 2d 3e 66 6c 61 67 73 20 3d 20 70 4e 65  xpr->flags = pNe
286e4 77 2d 3e 66 6c 61 67 73 3b 0a 20 20 20 20 20 20  w->flags;.      
286e5 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20  pExpr->pAggInfo 
286e6 3d 20 70 4e 65 77 2d 3e 70 41 67 67 49 6e 66 6f  = pNew->pAggInfo
286e7 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 41  ;.      pNew->pA
286e8 67 67 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 20 20  ggInfo = 0;.    
286e9 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  }.  }else{.    s
286ea 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 45 78  ubstExpr(db, pEx
286eb 70 72 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c  pr->pLeft, iTabl
286ec 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  e, pEList);.    
286ed 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 45  substExpr(db, pE
286ee 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69 54 61  xpr->pRight, iTa
286ef 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
286f0 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
286f1 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
286f2 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
286f3 20 20 20 20 73 75 62 73 74 53 65 6c 65 63 74 28      substSelect(
286f4 64 62 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  db, pExpr->x.pSe
286f5 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45  lect, iTable, pE
286f6 4c 69 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  List);.    }else
286f7 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  {.      substExp
286f8 72 4c 69 73 74 28 64 62 2c 20 70 45 78 70 72 2d  rList(db, pExpr-
286f9 3e 78 2e 70 4c 69 73 74 2c 20 69 54 61 62 6c 65  >x.pList, iTable
286fa 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d  , pEList);.    }
286fb 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
286fc 69 64 20 73 75 62 73 74 45 78 70 72 4c 69 73 74  id substExprList
286fd 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
286fe 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f           /* Repo
286ff 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  rt malloc errors
28700 20 68 65 72 65 20 2a 2f 0a 20 20 45 78 70 72 4c   here */.  ExprL
28701 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
28702 2f 2a 20 4c 69 73 74 20 74 6f 20 73 63 61 6e 20  /* List to scan 
28703 61 6e 64 20 69 6e 20 77 68 69 63 68 20 74 6f 20  and in which to 
28704 6d 61 6b 65 20 73 75 62 73 74 69 74 75 74 65 73  make substitutes
28705 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
28706 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  ,          /* Ta
28707 62 6c 65 20 74 6f 20 62 65 20 73 75 62 73 74 69  ble to be substi
28708 74 75 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  tuted */.  ExprL
28709 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 20  ist *pEList     
2870a 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 76 61  /* Substitute va
2870b 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  lues */.){.  int
2870c 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   i;.  if( pList=
2870d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  =0 ) return;.  f
2870e 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
2870f 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
28710 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20    substExpr(db, 
28711 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
28712 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  r, iTable, pELis
28713 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  t);.  }.}.static
28714 20 76 6f 69 64 20 73 75 62 73 74 53 65 6c 65 63   void substSelec
28715 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
28716 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70  ,         /* Rep
28717 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72  ort malloc error
28718 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65  s here */.  Sele
28719 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2871a 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65   /* SELECT state
2871b 6d 65 6e 74 20 69 6e 20 77 68 69 63 68 20 74 6f  ment in which to
2871c 20 6d 61 6b 65 20 73 75 62 73 74 69 74 75 74 69   make substituti
2871d 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ons */.  int iTa
2871e 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ble,          /*
2871f 20 54 61 62 6c 65 20 74 6f 20 62 65 20 72 65 70   Table to be rep
28720 6c 61 63 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  laced */.  ExprL
28721 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 20  ist *pEList     
28722 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 76 61  /* Substitute va
28723 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20 53 72 63  lues */.){.  Src
28724 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 73 74  List *pSrc;.  st
28725 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
28726 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20  m *pItem;.  int 
28727 69 3b 0a 20 20 69 66 28 20 21 70 20 29 20 72 65  i;.  if( !p ) re
28728 74 75 72 6e 3b 0a 20 20 73 75 62 73 74 45 78 70  turn;.  substExp
28729 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 45 4c  rList(db, p->pEL
2872a 69 73 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ist, iTable, pEL
2872b 69 73 74 29 3b 0a 20 20 73 75 62 73 74 45 78 70  ist);.  substExp
2872c 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 47 72  rList(db, p->pGr
2872d 6f 75 70 42 79 2c 20 69 54 61 62 6c 65 2c 20 70  oupBy, iTable, p
2872e 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74 45  EList);.  substE
2872f 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70  xprList(db, p->p
28730 4f 72 64 65 72 42 79 2c 20 69 54 61 62 6c 65 2c  OrderBy, iTable,
28731 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
28732 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 48 61  tExpr(db, p->pHa
28733 76 69 6e 67 2c 20 69 54 61 62 6c 65 2c 20 70 45  ving, iTable, pE
28734 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74 45 78  List);.  substEx
28735 70 72 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65  pr(db, p->pWhere
28736 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74  , iTable, pEList
28737 29 3b 0a 20 20 73 75 62 73 74 53 65 6c 65 63 74  );.  substSelect
28738 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20  (db, p->pPrior, 
28739 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2873a 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72  .  pSrc = p->pSr
2873b 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72  c;.  assert( pSr
2873c 63 20 29 3b 20 20 2f 2a 20 45 76 65 6e 20 66 6f  c );  /* Even fo
2873d 72 20 28 53 45 4c 45 43 54 20 31 29 20 77 65 20  r (SELECT 1) we 
2873e 68 61 76 65 3a 20 70 53 72 63 21 3d 30 20 62 75  have: pSrc!=0 bu
2873f 74 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20  t pSrc->nSrc==0 
28740 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  */.  if( ALWAYS(
28741 70 53 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72  pSrc) ){.    for
28742 28 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70  (i=pSrc->nSrc, p
28743 49 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e  Item=pSrc->a; i>
28744 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; i--, pItem++)
28745 7b 0a 20 20 20 20 20 20 73 75 62 73 74 53 65 6c  {.      substSel
28746 65 63 74 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70  ect(db, pItem->p
28747 53 65 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c 20  Select, iTable, 
28748 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20  pEList);.    }. 
28749 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21   }.}.#endif /* !
2874a 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2874b 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
2874c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2874d 5f 4f 4d 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a  _OMIT_VIEW) */..
2874e 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2874f 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
28750 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  Y) || !defined(S
28751 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
28752 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
28753 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
28754 66 6c 61 74 74 65 6e 20 73 75 62 71 75 65 72 69  flatten subqueri
28755 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  es in order to s
28756 70 65 65 64 0a 2a 2a 20 65 78 65 63 75 74 69 6f  peed.** executio
28757 6e 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 31  n.  It returns 1
28758 20 69 66 20 69 74 20 6d 61 6b 65 73 20 63 68 61   if it makes cha
28759 6e 67 65 73 20 61 6e 64 20 30 20 69 66 20 6e 6f  nges and 0 if no
2875a 20 66 6c 61 74 74 65 6e 69 6e 67 0a 2a 2a 20 6f   flattening.** o
2875b 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  ccurs..**.** To 
2875c 75 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 63  understand the c
2875d 6f 6e 63 65 70 74 20 6f 66 20 66 6c 61 74 74 65  oncept of flatte
2875e 6e 69 6e 67 2c 20 63 6f 6e 73 69 64 65 72 20 74  ning, consider t
2875f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
28760 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  query:.**.**    
28761 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 28   SELECT a FROM (
28762 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20 61 20  SELECT x+y AS a 
28763 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 7a 3c  FROM t1 WHERE z<
28764 31 30 30 29 20 57 48 45 52 45 20 61 3e 35 0a 2a  100) WHERE a>5.*
28765 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  *.** The default
28766 20 77 61 79 20 6f 66 20 69 6d 70 6c 65 6d 65 6e   way of implemen
28767 74 69 6e 67 20 74 68 69 73 20 71 75 65 72 79 20  ting this query 
28768 69 73 20 74 6f 20 65 78 65 63 75 74 65 20 74 68  is to execute th
28769 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 66 69  e.** subquery fi
2876a 72 73 74 20 61 6e 64 20 73 74 6f 72 65 20 74 68  rst and store th
2876b 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 74  e results in a t
2876c 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20  emporary table, 
2876d 74 68 65 6e 0a 2a 2a 20 72 75 6e 20 74 68 65 20  then.** run the 
2876e 6f 75 74 65 72 20 71 75 65 72 79 20 6f 6e 20 74  outer query on t
2876f 68 61 74 20 74 65 6d 70 6f 72 61 72 79 20 74 61  hat temporary ta
28770 62 6c 65 2e 20 20 54 68 69 73 20 72 65 71 75 69  ble.  This requi
28771 72 65 73 20 74 77 6f 0a 2a 2a 20 70 61 73 73 65  res two.** passe
28772 73 20 6f 76 65 72 20 74 68 65 20 64 61 74 61 2e  s over the data.
28773 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 62    Furthermore, b
28774 65 63 61 75 73 65 20 74 68 65 20 74 65 6d 70 6f  ecause the tempo
28775 72 61 72 79 20 74 61 62 6c 65 0a 2a 2a 20 68 61  rary table.** ha
28776 73 20 6e 6f 20 69 6e 64 69 63 65 73 2c 20 74 68  s no indices, th
28777 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  e WHERE clause o
28778 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  n the outer quer
28779 79 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6f  y cannot be.** o
2877a 70 74 69 6d 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20  ptimized..**.** 
2877b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74  This routine att
2877c 65 6d 70 74 73 20 74 6f 20 72 65 77 72 69 74 65  empts to rewrite
2877d 20 71 75 65 72 69 65 73 20 73 75 63 68 20 61 73   queries such as
2877e 20 74 68 65 20 61 62 6f 76 65 20 69 6e 74 6f 0a   the above into.
2877f 2a 2a 20 61 20 73 69 6e 67 6c 65 20 66 6c 61 74  ** a single flat
28780 20 73 65 6c 65 63 74 2c 20 6c 69 6b 65 20 74 68   select, like th
28781 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  is:.**.**     SE
28782 4c 45 43 54 20 78 2b 79 20 41 53 20 61 20 46 52  LECT x+y AS a FR
28783 4f 4d 20 74 31 20 57 48 45 52 45 20 7a 3c 31 30  OM t1 WHERE z<10
28784 30 20 41 4e 44 20 61 3e 35 0a 2a 2a 0a 2a 2a 20  0 AND a>5.**.** 
28785 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  The code generat
28786 65 64 20 66 6f 72 20 74 68 69 73 20 73 69 6d 70  ed for this simp
28787 69 66 69 63 61 74 69 6f 6e 20 67 69 76 65 73 20  ification gives 
28788 74 68 65 20 73 61 6d 65 20 72 65 73 75 6c 74 0a  the same result.
28789 2a 2a 20 62 75 74 20 6f 6e 6c 79 20 68 61 73 20  ** but only has 
2878a 74 6f 20 73 63 61 6e 20 74 68 65 20 64 61 74 61  to scan the data
2878b 20 6f 6e 63 65 2e 20 20 41 6e 64 20 62 65 63 61   once.  And beca
2878c 75 73 65 20 69 6e 64 69 63 65 73 20 6d 69 67 68  use indices migh
2878d 74 20 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74  t .** exist on t
2878e 68 65 20 74 61 62 6c 65 20 74 31 2c 20 61 20 63  he table t1, a c
2878f 6f 6d 70 6c 65 74 65 20 73 63 61 6e 20 6f 66 20  omplete scan of 
28790 74 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 62  the data might b
28791 65 0a 2a 2a 20 61 76 6f 69 64 65 64 2e 0a 2a 2a  e.** avoided..**
28792 0a 2a 2a 20 46 6c 61 74 74 65 6e 69 6e 67 20 69  .** Flattening i
28793 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64  s only attempted
28794 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66   if all of the f
28795 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75  ollowing are tru
28796 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20  e:.**.**   (1)  
28797 54 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64  The subquery and
28798 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
28799 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 75 73 65   do not both use
2879a 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a   aggregates..**.
2879b 2a 2a 20 20 20 28 32 29 20 20 54 68 65 20 73 75  **   (2)  The su
2879c 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e  bquery is not an
2879d 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 74 68   aggregate or th
2879e 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73  e outer query is
2879f 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a   not a join..**.
287a0 2a 2a 20 20 20 28 33 29 20 20 54 68 65 20 73 75  **   (3)  The su
287a1 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 74 68  bquery is not th
287a2 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
287a3 6f 66 20 61 20 6c 65 66 74 20 6f 75 74 65 72 20  of a left outer 
287a4 6a 6f 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 28  join.**        (
287a5 4f 72 69 67 69 6e 61 6c 6c 79 20 74 69 63 6b 65  Originally ticke
287a6 74 20 23 33 30 36 2e 20 20 53 74 72 65 6e 67 68  t #306.  Strengh
287a7 74 65 6e 65 64 20 62 79 20 74 69 63 6b 65 74 20  tened by ticket 
287a8 23 33 33 30 30 29 0a 2a 2a 0a 2a 2a 20 20 20 28  #3300).**.**   (
287a9 34 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  4)  The subquery
287aa 20 69 73 20 6e 6f 74 20 44 49 53 54 49 4e 43 54   is not DISTINCT
287ab 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
287ac 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69  ery is not a joi
287ad 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 20  n..**.**   (5)  
287ae 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20  The subquery is 
287af 6e 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72 20  not DISTINCT or 
287b0 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
287b1 64 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20  does not use.** 
287b2 20 20 20 20 20 20 20 61 67 67 72 65 67 61 74 65         aggregate
287b3 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 20  s..**.**   (6)  
287b4 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65  The subquery doe
287b5 73 20 6e 6f 74 20 75 73 65 20 61 67 67 72 65 67  s not use aggreg
287b6 61 74 65 73 20 6f 72 20 74 68 65 20 6f 75 74 65  ates or the oute
287b7 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 0a 2a  r query is not.*
287b8 2a 20 20 20 20 20 20 20 20 44 49 53 54 49 4e 43  *        DISTINC
287b9 54 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 37 29 20 20  T..**.**   (7)  
287ba 54 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73  The subquery has
287bb 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a   a FROM clause..
287bc 2a 2a 0a 2a 2a 20 20 20 28 38 29 20 20 54 68 65  **.**   (8)  The
287bd 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
287be 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20  ot use LIMIT or 
287bf 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
287c0 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a  is not a join..*
287c1 2a 0a 2a 2a 20 20 20 28 39 29 20 20 54 68 65 20  *.**   (9)  The 
287c2 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
287c3 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74  t use LIMIT or t
287c4 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64  he outer query d
287c5 6f 65 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20  oes not use.**  
287c6 20 20 20 20 20 20 61 67 67 72 65 67 61 74 65 73        aggregates
287c7 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 30 29 20 20 54  ..**.**  (10)  T
287c8 68 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73  he subquery does
287c9 20 6e 6f 74 20 75 73 65 20 61 67 67 72 65 67 61   not use aggrega
287ca 74 65 73 20 6f 72 20 74 68 65 20 6f 75 74 65 72  tes or the outer
287cb 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a   query does not.
287cc 2a 2a 20 20 20 20 20 20 20 20 75 73 65 20 4c 49  **        use LI
287cd 4d 49 54 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 31 29  MIT..**.**  (11)
287ce 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 61    The subquery a
287cf 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  nd the outer que
287d0 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 68  ry do not both h
287d1 61 76 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  ave ORDER BY cla
287d2 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 32  uses..**.**  (12
287d3 29 20 20 4e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74  )  Not implement
287d4 65 64 2e 20 20 53 75 62 73 75 6d 65 64 20 69 6e  ed.  Subsumed in
287d5 74 6f 20 72 65 73 74 72 69 63 74 69 6f 6e 20 28  to restriction (
287d6 33 29 2e 20 20 57 61 73 20 70 72 65 76 69 6f 75  3).  Was previou
287d7 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 61 20  sly.**        a 
287d8 73 65 70 61 72 61 74 65 20 72 65 73 74 72 69 63  separate restric
287d9 74 69 6f 6e 20 64 65 72 69 76 69 6e 67 20 66 72  tion deriving fr
287da 6f 6d 20 74 69 63 6b 65 74 20 23 33 35 30 2e 0a  om ticket #350..
287db 2a 2a 0a 2a 2a 20 20 28 31 33 29 20 20 54 68 65  **.**  (13)  The
287dc 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 6f 75   subquery and ou
287dd 74 65 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74  ter query do not
287de 20 62 6f 74 68 20 75 73 65 20 4c 49 4d 49 54 0a   both use LIMIT.
287df 2a 2a 0a 2a 2a 20 20 28 31 34 29 20 20 54 68 65  **.**  (14)  The
287e0 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
287e1 6f 74 20 75 73 65 20 4f 46 46 53 45 54 0a 2a 2a  ot use OFFSET.**
287e2 0a 2a 2a 20 20 28 31 35 29 20 20 54 68 65 20 6f  .**  (15)  The o
287e3 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
287e4 74 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70  t part of a comp
287e5 6f 75 6e 64 20 73 65 6c 65 63 74 20 6f 72 20 74  ound select or t
287e6 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 73 75 62  he.**        sub
287e7 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 68  query does not h
287e8 61 76 65 20 62 6f 74 68 20 61 6e 20 4f 52 44 45  ave both an ORDE
287e9 52 20 42 59 20 61 6e 64 20 61 20 4c 49 4d 49 54  R BY and a LIMIT
287ea 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 20 20 20 20   clause..**     
287eb 20 20 20 28 53 65 65 20 74 69 63 6b 65 74 20 23     (See ticket #
287ec 32 33 33 39 29 0a 2a 2a 0a 2a 2a 20 20 28 31 36  2339).**.**  (16
287ed 29 20 20 54 68 65 20 6f 75 74 65 72 20 71 75 65  )  The outer que
287ee 72 79 20 69 73 20 6e 6f 74 20 61 6e 20 61 67 67  ry is not an agg
287ef 72 65 67 61 74 65 20 6f 72 20 74 68 65 20 73 75  regate or the su
287f0 62 71 75 65 72 79 20 64 6f 65 73 0a 2a 2a 20 20  bquery does.**  
287f1 20 20 20 20 20 20 6e 6f 74 20 63 6f 6e 74 61 69        not contai
287f2 6e 20 4f 52 44 45 52 20 42 59 2e 20 20 28 54 69  n ORDER BY.  (Ti
287f3 63 6b 65 74 20 23 32 39 34 32 29 20 20 54 68 69  cket #2942)  Thi
287f4 73 20 75 73 65 64 20 74 6f 20 6e 6f 74 20 6d 61  s used to not ma
287f5 74 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 75  tter.**        u
287f6 6e 74 69 6c 20 77 65 20 69 6e 74 72 6f 64 75 63  ntil we introduc
287f7 65 64 20 74 68 65 20 67 72 6f 75 70 5f 63 6f 6e  ed the group_con
287f8 63 61 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  cat() function. 
287f9 20 0a 2a 2a 0a 2a 2a 20 20 28 31 37 29 20 20 54   .**.**  (17)  T
287fa 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20  he sub-query is 
287fb 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73  not a compound s
287fc 65 6c 65 63 74 2c 20 6f 72 20 69 74 20 69 73 20  elect, or it is 
287fd 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 0a 2a 2a 20  a UNION ALL .** 
287fe 20 20 20 20 20 20 20 63 6f 6d 70 6f 75 6e 64 20         compound 
287ff 63 6c 61 75 73 65 20 6d 61 64 65 20 75 70 20 65  clause made up e
28800 6e 74 69 72 65 6c 79 20 6f 66 20 6e 6f 6e 2d 61  ntirely of non-a
28801 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
28802 2c 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20  , and .**       
28803 20 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72   the parent quer
28804 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  y:.**.**        
28805 20 20 2a 20 69 73 20 6e 6f 74 20 69 74 73 65 6c    * is not itsel
28806 66 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70  f part of a comp
28807 6f 75 6e 64 20 73 65 6c 65 63 74 2c 0a 2a 2a 20  ound select,.** 
28808 20 20 20 20 20 20 20 20 20 2a 20 69 73 20 6e 6f           * is no
28809 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f  t an aggregate o
2880a 72 20 44 49 53 54 49 4e 43 54 20 71 75 65 72 79  r DISTINCT query
2880b 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  , and.**        
2880c 20 20 2a 20 68 61 73 20 6e 6f 20 6f 74 68 65 72    * has no other
2880d 20 74 61 62 6c 65 73 20 6f 72 20 73 75 62 2d 73   tables or sub-s
2880e 65 6c 65 63 74 73 20 69 6e 20 74 68 65 20 46 52  elects in the FR
2880f 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  OM clause..**.**
28810 20 20 20 20 20 20 20 20 54 68 65 20 70 61 72 65          The pare
28811 6e 74 20 61 6e 64 20 73 75 62 2d 71 75 65 72 79  nt and sub-query
28812 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 57 48 45   may contain WHE
28813 52 45 20 63 6c 61 75 73 65 73 2e 20 53 75 62 6a  RE clauses. Subj
28814 65 63 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  ect to.**       
28815 20 72 75 6c 65 73 20 28 31 31 29 2c 20 28 31 33   rules (11), (13
28816 29 20 61 6e 64 20 28 31 34 29 2c 20 74 68 65 79  ) and (14), they
28817 20 6d 61 79 20 61 6c 73 6f 20 63 6f 6e 74 61 69   may also contai
28818 6e 20 4f 52 44 45 52 20 42 59 2c 0a 2a 2a 20 20  n ORDER BY,.**  
28819 20 20 20 20 20 20 4c 49 4d 49 54 20 61 6e 64 20        LIMIT and 
2881a 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e 0a  OFFSET clauses..
2881b 2a 2a 0a 2a 2a 20 20 28 31 38 29 20 20 49 66 20  **.**  (18)  If 
2881c 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73  the sub-query is
2881d 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
2881e 63 74 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 72  ct, then all ter
2881f 6d 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20  ms of the.**    
28820 20 20 20 20 4f 52 44 45 52 20 62 79 20 63 6c 61      ORDER by cla
28821 75 73 65 20 6f 66 20 74 68 65 20 70 61 72 65 6e  use of the paren
28822 74 20 6d 75 73 74 20 62 65 20 73 69 6d 70 6c 65  t must be simple
28823 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 0a   references to .
28824 2a 2a 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e  **        column
28825 73 20 6f 66 20 74 68 65 20 73 75 62 2d 71 75 65  s of the sub-que
28826 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 39 29 20  ry..**.**  (19) 
28827 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   The subquery do
28828 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54  es not use LIMIT
28829 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
2882a 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  ery does not.** 
2882b 20 20 20 20 20 20 20 68 61 76 65 20 61 20 57 48         have a WH
2882c 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  ERE clause..**.*
2882d 2a 20 20 28 32 30 29 20 20 49 66 20 74 68 65 20  *  (20)  If the 
2882e 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63  sub-query is a c
2882f 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20  ompound select, 
28830 74 68 65 6e 20 69 74 20 6d 75 73 74 20 6e 6f 74  then it must not
28831 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 61   use.**        a
28832 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
28833 65 2e 20 20 54 69 63 6b 65 74 20 23 33 37 37 33  e.  Ticket #3773
28834 2e 20 20 57 65 20 63 6f 75 6c 64 20 72 65 6c 61  .  We could rela
28835 78 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  x this constrain
28836 74 0a 2a 2a 20 20 20 20 20 20 20 20 73 6f 6d 65  t.**        some
28837 77 68 61 74 20 62 79 20 73 61 79 69 6e 67 20 74  what by saying t
28838 68 61 74 20 74 68 65 20 74 65 72 6d 73 20 6f 66  hat the terms of
28839 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2883a 61 75 73 65 20 6d 75 73 74 0a 2a 2a 20 20 20 20  ause must.**    
2883b 20 20 20 20 61 70 70 65 61 72 20 61 73 20 75 6e      appear as un
2883c 6d 6f 64 69 66 69 65 64 20 72 65 73 75 6c 74 20  modified result 
2883d 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f  columns in the o
2883e 75 74 65 72 20 71 75 65 72 79 2e 20 20 42 75 74  uter query.  But
2883f 0a 2a 2a 20 20 20 20 20 20 20 20 68 61 76 65 20  .**        have 
28840 6f 74 68 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  other optimizati
28841 6f 6e 73 20 69 6e 20 6d 69 6e 64 20 74 6f 20 64  ons in mind to d
28842 65 61 6c 20 77 69 74 68 20 74 68 61 74 20 63 61  eal with that ca
28843 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69  se..**.** In thi
28844 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 22  s routine, the "
28845 70 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  p" parameter is 
28846 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
28847 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a 2a 2a   outer query..**
28848 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
28849 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 46 72 6f   p->pSrc->a[iFro
2884a 6d 5d 2e 20 20 69 73 41 67 67 20 69 73 20 74 72  m].  isAgg is tr
2884b 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 20  ue if the outer 
2884c 71 75 65 72 79 0a 2a 2a 20 75 73 65 73 20 61 67  query.** uses ag
2884d 67 72 65 67 61 74 65 73 20 61 6e 64 20 73 75 62  gregates and sub
2884e 71 75 65 72 79 49 73 41 67 67 20 69 73 20 74 72  queryIsAgg is tr
2884f 75 65 20 69 66 20 74 68 65 20 73 75 62 71 75 65  ue if the subque
28850 72 79 20 75 73 65 73 20 61 67 67 72 65 67 61 74  ry uses aggregat
28851 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 66 6c 61  es..**.** If fla
28852 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f 74 20 61  ttening is not a
28853 74 74 65 6d 70 74 65 64 2c 20 74 68 69 73 20 72  ttempted, this r
28854 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
28855 70 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 2e  p and returns 0.
28856 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e 69 6e  .** If flattenin
28857 67 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 74  g is attempted t
28858 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
28859 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c  rns 1..**.** All
2885a 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
2885b 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d 75 73 74  on analysis must
2885c 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74 68 20 74   occur on both t
2885d 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61  he outer query a
2885e 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62 71 75 65  nd.** the subque
2885f 72 79 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ry before this r
28860 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a 2a 2f 0a  outine runs..*/.
28861 73 74 61 74 69 63 20 69 6e 74 20 66 6c 61 74 74  static int flatt
28862 65 6e 53 75 62 71 75 65 72 79 28 0a 20 20 50 61  enSubquery(.  Pa
28863 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
28864 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
28865 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63  ntext */.  Selec
28866 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  t *p,           
28867 2f 2a 20 54 68 65 20 70 61 72 65 6e 74 20 6f 72  /* The parent or
28868 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20 73 74   outer SELECT st
28869 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
2886a 20 69 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20   iFrom,         
2886b 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 2d    /* Index in p-
2886c 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 74 68  >pSrc->a[] of th
2886d 65 20 69 6e 6e 65 72 20 73 75 62 71 75 65 72 79  e inner subquery
2886e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67 67 2c   */.  int isAgg,
2886f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
28870 75 65 20 69 66 20 6f 75 74 65 72 20 53 45 4c 45  ue if outer SELE
28871 43 54 20 75 73 65 73 20 61 67 67 72 65 67 61 74  CT uses aggregat
28872 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20  e functions */. 
28873 20 69 6e 74 20 73 75 62 71 75 65 72 79 49 73 41   int subqueryIsA
28874 67 67 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  gg    /* True if
28875 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75 73   the subquery us
28876 65 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  es aggregate fun
28877 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 63  ctions */.){.  c
28878 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65  onst char *zSave
28879 64 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70  dAuthContext = p
2887a 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
2887b 65 78 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70  ext;.  Select *p
2887c 50 61 72 65 6e 74 3b 0a 20 20 53 65 6c 65 63 74  Parent;.  Select
2887d 20 2a 70 53 75 62 3b 20 20 20 20 20 20 20 2f 2a   *pSub;       /*
2887e 20 54 68 65 20 69 6e 6e 65 72 20 71 75 65 72 79   The inner query
2887f 20 6f 72 20 22 73 75 62 71 75 65 72 79 22 20 2a   or "subquery" *
28880 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62  /.  Select *pSub
28881 31 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  1;      /* Point
28882 65 72 20 74 6f 20 74 68 65 20 72 69 67 68 74 6d  er to the rightm
28883 6f 73 74 20 73 65 6c 65 63 74 20 69 6e 20 73 75  ost select in su
28884 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20 53 72 63  b-query */.  Src
28885 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20  List *pSrc;     
28886 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61   /* The FROM cla
28887 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72  use of the outer
28888 20 71 75 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c   query */.  SrcL
28889 69 73 74 20 2a 70 53 75 62 53 72 63 3b 20 20 20  ist *pSubSrc;   
2888a 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
2888b 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  se of the subque
2888c 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ry */.  ExprList
2888d 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54   *pList;    /* T
2888e 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
2888f 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
28890 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e   */.  int iParen
28891 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42  t;        /* VDB
28892 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
28893 6f 66 20 74 68 65 20 70 53 75 62 20 72 65 73 75  of the pSub resu
28894 6c 74 20 73 65 74 20 74 65 6d 70 20 74 61 62 6c  lt set temp tabl
28895 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  e */.  int i;   
28896 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
28897 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
28898 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20  Expr *pWhere;   
28899 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2889a 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
2889b 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  ause */.  struct
2889c 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
2889d 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a 20 54 68  Subitem;   /* Th
2889e 65 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20  e subquery */.  
2889f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
288a0 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20  arse->db;..  /* 
288a1 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
288a2 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65  flattening is pe
288a3 72 6d 69 74 74 65 64 2e 20 20 52 65 74 75 72 6e  rmitted.  Return
288a4 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a   0 if not..  */.
288a5 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
288a6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  ;.  assert( p->p
288a7 50 72 69 6f 72 3d 3d 30 20 29 3b 20 20 2f 2a 20  Prior==0 );  /* 
288a8 55 6e 61 62 6c 65 20 74 6f 20 66 6c 61 74 74 65  Unable to flatte
288a9 6e 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 69  n compound queri
288aa 65 73 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70  es */.  pSrc = p
288ab 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74  ->pSrc;.  assert
288ac 28 20 70 53 72 63 20 26 26 20 69 46 72 6f 6d 3e  ( pSrc && iFrom>
288ad 3d 30 20 26 26 20 69 46 72 6f 6d 3c 70 53 72 63  =0 && iFrom<pSrc
288ae 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 70 53 75 62  ->nSrc );.  pSub
288af 69 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b  item = &pSrc->a[
288b0 69 46 72 6f 6d 5d 3b 0a 20 20 69 50 61 72 65 6e  iFrom];.  iParen
288b1 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 69 43  t = pSubitem->iC
288b2 75 72 73 6f 72 3b 0a 20 20 70 53 75 62 20 3d 20  ursor;.  pSub = 
288b3 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63  pSubitem->pSelec
288b4 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 75  t;.  assert( pSu
288b5 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73  b!=0 );.  if( is
288b6 41 67 67 20 26 26 20 73 75 62 71 75 65 72 79 49  Agg && subqueryI
288b7 73 41 67 67 20 29 20 72 65 74 75 72 6e 20 30 3b  sAgg ) return 0;
288b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288b9 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
288ba 28 31 29 20 20 2a 2f 0a 20 20 69 66 28 20 73 75  (1)  */.  if( su
288bb 62 71 75 65 72 79 49 73 41 67 67 20 26 26 20 70  bqueryIsAgg && p
288bc 53 72 63 2d 3e 6e 53 72 63 3e 31 20 29 20 72 65  Src->nSrc>1 ) re
288bd 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
288be 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
288bf 28 32 29 20 20 2a 2f 0a 20 20 70 53 75 62 53 72  (2)  */.  pSubSr
288c0 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b 0a  c = pSub->pSrc;.
288c1 20 20 61 73 73 65 72 74 28 20 70 53 75 62 53 72    assert( pSubSr
288c2 63 20 29 3b 0a 20 20 2f 2a 20 50 72 69 6f 72 20  c );.  /* Prior 
288c3 74 6f 20 76 65 72 73 69 6f 6e 20 33 2e 31 2e 32  to version 3.1.2
288c4 2c 20 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64  , when LIMIT and
288c5 20 4f 46 46 53 45 54 20 68 61 64 20 74 6f 20 62   OFFSET had to b
288c6 65 20 73 69 6d 70 6c 65 20 63 6f 6e 73 74 61 6e  e simple constan
288c7 74 73 2c 0a 20 20 2a 2a 20 6e 6f 74 20 61 72 62  ts,.  ** not arb
288c8 69 74 72 61 72 79 20 65 78 70 72 65 73 73 73 69  itrary expresssi
288c9 6f 6e 73 2c 20 77 65 20 61 6c 6c 6f 77 65 64 20  ons, we allowed 
288ca 73 6f 6d 65 20 63 6f 6d 62 69 6e 69 6e 67 20 6f  some combining o
288cb 66 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53  f LIMIT and OFFS
288cc 45 54 0a 20 20 2a 2a 20 62 65 63 61 75 73 65 20  ET.  ** because 
288cd 74 68 65 79 20 63 6f 75 6c 64 20 62 65 20 63 6f  they could be co
288ce 6d 70 75 74 65 64 20 61 74 20 63 6f 6d 70 69 6c  mputed at compil
288cf 65 2d 74 69 6d 65 2e 20 20 42 75 74 20 77 68 65  e-time.  But whe
288d0 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53  n LIMIT and OFFS
288d1 45 54 0a 20 20 2a 2a 20 62 65 63 61 6d 65 20 61  ET.  ** became a
288d2 72 62 69 74 72 61 72 79 20 65 78 70 72 65 73 73  rbitrary express
288d3 69 6f 6e 73 2c 20 77 65 20 77 65 72 65 20 66 6f  ions, we were fo
288d4 72 63 65 64 20 74 6f 20 61 64 64 20 72 65 73 74  rced to add rest
288d5 72 69 63 74 69 6f 6e 73 20 28 31 33 29 0a 20 20  rictions (13).  
288d6 2a 2a 20 61 6e 64 20 28 31 34 29 2e 20 2a 2f 0a  ** and (14). */.
288d7 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d    if( pSub->pLim
288d8 69 74 20 26 26 20 70 2d 3e 70 4c 69 6d 69 74 20  it && p->pLimit 
288d9 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
288da 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
288db 72 69 63 74 69 6f 6e 20 28 31 33 29 20 2a 2f 0a  riction (13) */.
288dc 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f 66 66    if( pSub->pOff
288dd 73 65 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20  set ) return 0; 
288de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288df 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
288e0 72 69 63 74 69 6f 6e 20 28 31 34 29 20 2a 2f 0a  riction (14) */.
288e1 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 6d    if( p->pRightm
288e2 6f 73 74 20 26 26 20 70 53 75 62 2d 3e 70 4c 69  ost && pSub->pLi
288e3 6d 69 74 20 26 26 20 70 53 75 62 2d 3e 70 4f 72  mit && pSub->pOr
288e4 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74  derBy ){.    ret
288e5 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
288e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288e8 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
288e9 20 28 31 35 29 20 2a 2f 0a 20 20 7d 0a 20 20 69   (15) */.  }.  i
288ea 66 28 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63  f( pSubSrc->nSrc
288eb 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ==0 ) return 0; 
288ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288ed 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
288ee 74 69 6f 6e 20 28 37 29 20 20 2a 2f 0a 20 20 69  tion (7)  */.  i
288ef 66 28 20 28 28 70 53 75 62 2d 3e 73 65 6c 46 6c  f( ((pSub->selFl
288f0 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63  ags & SF_Distinc
288f1 74 29 21 3d 30 20 7c 7c 20 70 53 75 62 2d 3e 70  t)!=0 || pSub->p
288f2 4c 69 6d 69 74 29 20 0a 20 20 20 20 20 20 20 20  Limit) .        
288f3 20 26 26 20 28 70 53 72 63 2d 3e 6e 53 72 63 3e   && (pSrc->nSrc>
288f4 31 20 7c 7c 20 69 73 41 67 67 29 20 29 7b 20 20  1 || isAgg) ){  
288f5 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
288f6 69 63 74 69 6f 6e 73 20 28 34 29 28 35 29 28 38  ictions (4)(5)(8
288f7 29 28 39 29 20 2a 2f 0a 20 20 20 20 20 72 65 74  )(9) */.     ret
288f8 75 72 6e 20 30 3b 20 20 20 20 20 20 20 0a 20 20  urn 0;       .  
288f9 7d 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46  }.  if( (p->selF
288fa 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e  lags & SF_Distin
288fb 63 74 29 21 3d 30 20 26 26 20 73 75 62 71 75 65  ct)!=0 && subque
288fc 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20 20 20  ryIsAgg ){.     
288fd 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
288fe 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
288ff 20 28 36 29 20 20 2a 2f 0a 20 20 7d 0a 20 20 69   (6)  */.  }.  i
28900 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 26  f( p->pOrderBy &
28901 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79  & pSub->pOrderBy
28902 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 20   ){.     return 
28903 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
28904 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28905 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28906 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 31 29  Restriction (11)
28907 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 69 73   */.  }.  if( is
28908 41 67 67 20 26 26 20 70 53 75 62 2d 3e 70 4f 72  Agg && pSub->pOr
28909 64 65 72 42 79 20 29 20 72 65 74 75 72 6e 20 30  derBy ) return 0
2890a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2890b 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2890c 28 31 36 29 20 2a 2f 0a 20 20 69 66 28 20 70 53  (16) */.  if( pS
2890d 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d  ub->pLimit && p-
2890e 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e  >pWhere ) return
2890f 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
28910 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
28911 28 31 39 29 20 2a 2f 0a 0a 20 20 2f 2a 20 4f 42  (19) */..  /* OB
28912 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 20 31  SOLETE COMMENT 1
28913 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69 63 74 69  :.  ** Restricti
28914 6f 6e 20 33 3a 20 20 49 66 20 74 68 65 20 73 75  on 3:  If the su
28915 62 71 75 65 72 79 20 69 73 20 61 20 6a 6f 69 6e  bquery is a join
28916 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  , make sure the 
28917 73 75 62 71 75 65 72 79 20 69 73 20 0a 20 20 2a  subquery is .  *
28918 2a 20 6e 6f 74 20 75 73 65 64 20 61 73 20 74 68  * not used as th
28919 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20  e right operand 
2891a 6f 66 20 61 6e 20 6f 75 74 65 72 20 6a 6f 69 6e  of an outer join
2891b 2e 20 20 45 78 61 6d 70 6c 65 73 20 6f 66 20 77  .  Examples of w
2891c 68 79 20 74 68 69 73 0a 20 20 2a 2a 20 69 73 20  hy this.  ** is 
2891d 6e 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a  not allowed:.  *
2891e 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74  *.  **         t
2891f 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  1 LEFT OUTER JOI
28920 4e 20 28 74 32 20 4a 4f 49 4e 20 74 33 29 0a 20  N (t2 JOIN t3). 
28921 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65 20 66   **.  ** If we f
28922 6c 61 74 74 65 6e 20 74 68 65 20 61 62 6f 76 65  latten the above
28923 2c 20 77 65 20 77 6f 75 6c 64 20 67 65 74 0a 20  , we would get. 
28924 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
28925 20 28 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20   (t1 LEFT OUTER 
28926 4a 4f 49 4e 20 74 32 29 20 4a 4f 49 4e 20 74 33  JOIN t2) JOIN t3
28927 0a 20 20 2a 2a 0a 20 20 2a 2a 20 77 68 69 63 68  .  **.  ** which
28928 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 74   is not at all t
28929 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e 0a 20  he same thing.. 
2892a 20 2a 2a 0a 20 20 2a 2a 20 4f 42 53 4f 4c 45 54   **.  ** OBSOLET
2892b 45 20 43 4f 4d 4d 45 4e 54 20 32 3a 0a 20 20 2a  E COMMENT 2:.  *
2892c 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31 32  * Restriction 12
2892d 3a 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65  :  If the subque
2892e 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74 20  ry is the right 
2892f 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 6c 65 66  operand of a lef
28930 74 20 6f 75 74 65 72 0a 20 20 2a 2a 20 6a 6f 69  t outer.  ** joi
28931 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  n, make sure the
28932 20 73 75 62 71 75 65 72 79 20 68 61 73 20 6e 6f   subquery has no
28933 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20   WHERE clause.. 
28934 20 2a 2a 20 41 6e 20 65 78 61 6d 70 6c 65 73 20   ** An examples 
28935 6f 66 20 77 68 79 20 74 68 69 73 20 69 73 20 6e  of why this is n
28936 6f 74 20 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a  ot allowed:.  **
28937 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74 31  .  **         t1
28938 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
28939 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20   (SELECT * FROM 
2893a 74 32 20 57 48 45 52 45 20 74 32 2e 78 3e 30 29  t2 WHERE t2.x>0)
2893b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65  .  **.  ** If we
2893c 20 66 6c 61 74 74 65 6e 20 74 68 65 20 61 62 6f   flatten the abo
2893d 76 65 2c 20 77 65 20 77 6f 75 6c 64 20 67 65 74  ve, we would get
2893e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
2893f 20 20 20 28 74 31 20 4c 45 46 54 20 4f 55 54 45     (t1 LEFT OUTE
28940 52 20 4a 4f 49 4e 20 74 32 29 20 57 48 45 52 45  R JOIN t2) WHERE
28941 20 74 32 2e 78 3e 30 0a 20 20 2a 2a 0a 20 20 2a   t2.x>0.  **.  *
28942 2a 20 42 75 74 20 74 68 65 20 74 32 2e 78 3e 30  * But the t2.x>0
28943 20 74 65 73 74 20 77 69 6c 6c 20 61 6c 77 61 79   test will alway
28944 73 20 66 61 69 6c 20 6f 6e 20 61 20 4e 55 4c 4c  s fail on a NULL
28945 20 72 6f 77 20 6f 66 20 74 32 2c 20 77 68 69 63   row of t2, whic
28946 68 0a 20 20 2a 2a 20 65 66 66 65 63 74 69 76 65  h.  ** effective
28947 6c 79 20 63 6f 6e 76 65 72 74 73 20 74 68 65 20  ly converts the 
28948 4f 55 54 45 52 20 4a 4f 49 4e 20 69 6e 74 6f 20  OUTER JOIN into 
28949 61 6e 20 49 4e 4e 45 52 20 4a 4f 49 4e 2e 0a 20  an INNER JOIN.. 
2894a 20 2a 2a 0a 20 20 2a 2a 20 54 48 49 53 20 4f 56   **.  ** THIS OV
2894b 45 52 52 49 44 45 53 20 4f 42 53 4f 4c 45 54 45  ERRIDES OBSOLETE
2894c 20 43 4f 4d 4d 45 4e 54 53 20 31 20 41 4e 44 20   COMMENTS 1 AND 
2894d 32 20 41 42 4f 56 45 3a 0a 20 20 2a 2a 20 54 69  2 ABOVE:.  ** Ti
2894e 63 6b 65 74 20 23 33 33 30 30 20 73 68 6f 77 73  cket #3300 shows
2894f 20 74 68 61 74 20 66 6c 61 74 74 65 6e 69 6e 67   that flattening
28950 20 74 68 65 20 72 69 67 68 74 20 74 65 72 6d 20   the right term 
28951 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 0a 20  of a LEFT JOIN. 
28952 20 2a 2a 20 69 73 20 66 72 61 75 67 68 74 20 77   ** is fraught w
28953 69 74 68 20 64 61 6e 67 65 72 2e 20 20 42 65 73  ith danger.  Bes
28954 74 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20 77  t to avoid the w
28955 68 6f 6c 65 20 74 68 69 6e 67 2e 20 20 49 66 20  hole thing.  If 
28956 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72  the.  ** subquer
28957 79 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74  y is the right t
28958 65 72 6d 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f  erm of a LEFT JO
28959 49 4e 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  IN, then do not 
2895a 66 6c 61 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20  flatten..  */.  
2895b 69 66 28 20 28 70 53 75 62 69 74 65 6d 2d 3e 6a  if( (pSubitem->j
2895c 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f 55 54  ointype & JT_OUT
2895d 45 52 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65  ER)!=0 ){.    re
2895e 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 0;.  }..  /
2895f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 31 37  * Restriction 17
28960 3a 20 49 66 20 74 68 65 20 73 75 62 2d 71 75 65  : If the sub-que
28961 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64  ry is a compound
28962 20 53 45 4c 45 43 54 2c 20 74 68 65 6e 20 69 74   SELECT, then it
28963 20 6d 75 73 74 0a 20 20 2a 2a 20 75 73 65 20 6f   must.  ** use o
28964 6e 6c 79 20 74 68 65 20 55 4e 49 4f 4e 20 41 4c  nly the UNION AL
28965 4c 20 6f 70 65 72 61 74 6f 72 2e 20 41 6e 64 20  L operator. And 
28966 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 69 6d 70  none of the simp
28967 6c 65 20 73 65 6c 65 63 74 20 71 75 65 72 69 65  le select querie
28968 73 0a 20 20 2a 2a 20 74 68 61 74 20 6d 61 6b 65  s.  ** that make
28969 20 75 70 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   up the compound
2896a 20 53 45 4c 45 43 54 20 61 72 65 20 61 6c 6c 6f   SELECT are allo
2896b 77 65 64 20 74 6f 20 62 65 20 61 67 67 72 65 67  wed to be aggreg
2896c 61 74 65 20 6f 72 20 64 69 73 74 69 6e 63 74 0a  ate or distinct.
2896d 20 20 2a 2a 20 71 75 65 72 69 65 73 2e 0a 20 20    ** queries..  
2896e 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70  */.  if( pSub->p
2896f 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 69 66 28  Prior ){.    if(
28970 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20   pSub->pOrderBy 
28971 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
28972 30 3b 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69  0;  /* Restricti
28973 6f 6e 20 32 30 20 2a 2f 0a 20 20 20 20 7d 0a 20  on 20 */.    }. 
28974 20 20 20 69 66 28 20 69 73 41 67 67 20 7c 7c 20     if( isAgg || 
28975 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
28976 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 20 7c  F_Distinct)!=0 |
28977 7c 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20  | pSrc->nSrc!=1 
28978 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
28979 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  0;.    }.    for
2897a 28 70 53 75 62 31 3d 70 53 75 62 3b 20 70 53 75  (pSub1=pSub; pSu
2897b 62 31 3b 20 70 53 75 62 31 3d 70 53 75 62 31 2d  b1; pSub1=pSub1-
2897c 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20 20 20  >pPrior){.      
2897d 69 66 28 20 28 70 53 75 62 31 2d 3e 73 65 6c 46  if( (pSub1->selF
2897e 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69  lags & (SF_Disti
2897f 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65  nct|SF_Aggregate
28980 29 29 21 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20  ))!=0.       || 
28981 28 70 53 75 62 31 2d 3e 70 50 72 69 6f 72 20 26  (pSub1->pPrior &
28982 26 20 70 53 75 62 31 2d 3e 6f 70 21 3d 54 4b 5f  & pSub1->op!=TK_
28983 41 4c 4c 29 20 0a 20 20 20 20 20 20 20 7c 7c 20  ALL) .       || 
28984 21 70 53 75 62 31 2d 3e 70 53 72 63 20 7c 7c 20  !pSub1->pSrc || 
28985 70 53 75 62 31 2d 3e 70 53 72 63 2d 3e 6e 53 72  pSub1->pSrc->nSr
28986 63 21 3d 31 0a 20 20 20 20 20 20 29 7b 0a 20 20  c!=1.      ){.  
28987 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
28988 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
28989 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2898a 6e 20 31 38 2e 20 2a 2f 0a 20 20 20 20 69 66 28  n 18. */.    if(
2898b 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a   p->pOrderBy ){.
2898c 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20        int ii;.  
2898d 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
2898e 3c 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 6e 45  <p->pOrderBy->nE
2898f 78 70 72 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  xpr; ii++){.    
28990 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65      if( p->pOrde
28991 72 42 79 2d 3e 61 5b 69 69 5d 2e 69 43 6f 6c 3d  rBy->a[ii].iCol=
28992 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
28993 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
28994 0a 0a 20 20 2f 2a 2a 2a 2a 2a 20 49 66 20 77 65  ..  /***** If we
28995 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e   reach this poin
28996 74 2c 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73  t, flattening is
28997 20 70 65 72 6d 69 74 74 65 64 2e 20 2a 2a 2a 2a   permitted. ****
28998 2a 2f 0a 0a 20 20 2f 2a 20 41 75 74 68 6f 72 69  */..  /* Authori
28999 7a 65 20 74 68 65 20 73 75 62 71 75 65 72 79 20  ze the subquery 
2899a 2a 2f 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41 75  */.  pParse->zAu
2899b 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 53 75 62  thContext = pSub
2899c 69 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 73  item->zName;.  s
2899d 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2899e 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 53  pParse, SQLITE_S
2899f 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20 30 29 3b  ELECT, 0, 0, 0);
289a0 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68  .  pParse->zAuth
289a1 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61 76 65 64  Context = zSaved
289a2 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 0a 20 20  AuthContext;..  
289a3 2f 2a 20 49 66 20 74 68 65 20 73 75 62 2d 71 75  /* If the sub-qu
289a4 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e  ery is a compoun
289a5 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  d SELECT stateme
289a6 6e 74 2c 20 74 68 65 6e 20 28 62 79 20 72 65 73  nt, then (by res
289a7 74 72 69 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 31  trictions.  ** 1
289a8 37 20 61 6e 64 20 31 38 20 61 62 6f 76 65 29 20  7 and 18 above) 
289a9 69 74 20 6d 75 73 74 20 62 65 20 61 20 55 4e 49  it must be a UNI
289aa 4f 4e 20 41 4c 4c 20 61 6e 64 20 74 68 65 20 70  ON ALL and the p
289ab 61 72 65 6e 74 20 71 75 65 72 79 20 6d 75 73 74  arent query must
289ac 20 0a 20 20 2a 2a 20 62 65 20 6f 66 20 74 68 65   .  ** be of the
289ad 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a   form:.  **.  **
289ae 20 20 20 20 20 53 45 4c 45 43 54 20 3c 65 78 70       SELECT <exp
289af 72 2d 6c 69 73 74 3e 20 46 52 4f 4d 20 28 3c 73  r-list> FROM (<s
289b0 75 62 2d 71 75 65 72 79 3e 29 20 3c 77 68 65 72  ub-query>) <wher
289b1 65 2d 63 6c 61 75 73 65 3e 20 0a 20 20 2a 2a 0a  e-clause> .  **.
289b2 20 20 2a 2a 20 66 6f 6c 6c 6f 77 65 64 20 62 79    ** followed by
289b3 20 61 6e 79 20 4f 52 44 45 52 20 42 59 2c 20 4c   any ORDER BY, L
289b4 49 4d 49 54 20 61 6e 64 2f 6f 72 20 4f 46 46 53  IMIT and/or OFFS
289b5 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 69 73  ET clauses. This
289b6 20 62 6c 6f 63 6b 0a 20 20 2a 2a 20 63 72 65 61   block.  ** crea
289b7 74 65 73 20 4e 2d 31 20 63 6f 70 69 65 73 20 6f  tes N-1 copies o
289b8 66 20 74 68 65 20 70 61 72 65 6e 74 20 71 75 65  f the parent que
289b9 72 79 20 77 69 74 68 6f 75 74 20 61 6e 79 20 4f  ry without any O
289ba 52 44 45 52 20 42 59 2c 20 4c 49 4d 49 54 20 6f  RDER BY, LIMIT o
289bb 72 20 0a 20 20 2a 2a 20 4f 46 46 53 45 54 20 63  r .  ** OFFSET c
289bc 6c 61 75 73 65 73 20 61 6e 64 20 6a 6f 69 6e 73  lauses and joins
289bd 20 74 68 65 6d 20 74 6f 20 74 68 65 20 6c 65 66   them to the lef
289be 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66 20 74  t-hand-side of t
289bf 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a  he original.  **
289c0 20 75 73 69 6e 67 20 55 4e 49 4f 4e 20 41 4c 4c   using UNION ALL
289c1 20 6f 70 65 72 61 74 6f 72 73 2e 20 49 6e 20 74   operators. In t
289c2 68 69 73 20 63 61 73 65 20 4e 20 69 73 20 74 68  his case N is th
289c3 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 6d 70  e number of simp
289c4 6c 65 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20 73  le.  ** select s
289c5 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65  tatements in the
289c6 20 63 6f 6d 70 6f 75 6e 64 20 73 75 62 2d 71 75   compound sub-qu
289c7 65 72 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  ery..  **.  ** E
289c8 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a  xample:.  **.  *
289c9 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 31  *     SELECT a+1
289ca 20 46 52 4f 4d 20 28 0a 20 20 2a 2a 20 20 20 20   FROM (.  **    
289cb 20 20 20 20 53 45 4c 45 43 54 20 78 20 46 52 4f      SELECT x FRO
289cc 4d 20 74 61 62 0a 20 20 2a 2a 20 20 20 20 20 20  M tab.  **      
289cd 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a    UNION ALL.  **
289ce 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 79          SELECT y
289cf 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a 20 20   FROM tab.  **  
289d0 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a        UNION ALL.
289d1 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c 45    **        SELE
289d2 43 54 20 61 62 73 28 7a 2a 32 29 20 46 52 4f 4d  CT abs(z*2) FROM
289d3 20 74 61 62 32 0a 20 20 2a 2a 20 20 20 20 20 29   tab2.  **     )
289d4 20 57 48 45 52 45 20 61 21 3d 35 20 4f 52 44 45   WHERE a!=5 ORDE
289d5 52 20 42 59 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a  R BY 1.  **.  **
289d6 20 54 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74   Transformed int
289d7 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  o:.  **.  **    
289d8 20 53 45 4c 45 43 54 20 78 2b 31 20 46 52 4f 4d   SELECT x+1 FROM
289d9 20 74 61 62 20 57 48 45 52 45 20 78 2b 31 21 3d   tab WHERE x+1!=
289da 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e 49 4f 4e  5.  **     UNION
289db 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 53 45   ALL.  **     SE
289dc 4c 45 43 54 20 79 2b 31 20 46 52 4f 4d 20 74 61  LECT y+1 FROM ta
289dd 62 20 57 48 45 52 45 20 79 2b 31 21 3d 35 0a 20  b WHERE y+1!=5. 
289de 20 2a 2a 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c   **     UNION AL
289df 4c 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43  L.  **     SELEC
289e0 54 20 61 62 73 28 7a 2a 32 29 2b 31 20 46 52 4f  T abs(z*2)+1 FRO
289e1 4d 20 74 61 62 32 20 57 48 45 52 45 20 61 62 73  M tab2 WHERE abs
289e2 28 7a 2a 32 29 2b 31 21 3d 35 0a 20 20 2a 2a 20  (z*2)+1!=5.  ** 
289e3 20 20 20 20 4f 52 44 45 52 20 42 59 20 31 0a 20      ORDER BY 1. 
289e4 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 63 61 6c 6c   **.  ** We call
289e5 20 74 68 69 73 20 74 68 65 20 22 63 6f 6d 70 6f   this the "compo
289e6 75 6e 64 2d 73 75 62 71 75 65 72 79 20 66 6c 61  und-subquery fla
289e7 74 74 65 6e 69 6e 67 22 2e 0a 20 20 2a 2f 0a 20  ttening"..  */. 
289e8 20 66 6f 72 28 70 53 75 62 3d 70 53 75 62 2d 3e   for(pSub=pSub->
289e9 70 50 72 69 6f 72 3b 20 70 53 75 62 3b 20 70 53  pPrior; pSub; pS
289ea 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72 29  ub=pSub->pPrior)
289eb 7b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 4e  {.    Select *pN
289ec 65 77 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74  ew;.    ExprList
289ed 20 2a 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e   *pOrderBy = p->
289ee 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 45 78  pOrderBy;.    Ex
289ef 70 72 20 2a 70 4c 69 6d 69 74 20 3d 20 70 2d 3e  pr *pLimit = p->
289f0 70 4c 69 6d 69 74 3b 0a 20 20 20 20 53 65 6c 65  pLimit;.    Sele
289f1 63 74 20 2a 70 50 72 69 6f 72 20 3d 20 70 2d 3e  ct *pPrior = p->
289f2 70 50 72 69 6f 72 3b 0a 20 20 20 20 70 2d 3e 70  pPrior;.    p->p
289f3 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20  OrderBy = 0;.   
289f4 20 70 2d 3e 70 53 72 63 20 3d 20 30 3b 0a 20 20   p->pSrc = 0;.  
289f5 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b    p->pPrior = 0;
289f6 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d  .    p->pLimit =
289f7 20 30 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73   0;.    pNew = s
289f8 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
289f9 64 62 2c 20 70 2c 20 30 29 3b 0a 20 20 20 20 70  db, p, 0);.    p
289fa 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69  ->pLimit = pLimi
289fb 74 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72  t;.    p->pOrder
289fc 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20  By = pOrderBy;. 
289fd 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 70 53 72     p->pSrc = pSr
289fe 63 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54  c;.    p->op = T
289ff 4b 5f 41 4c 4c 3b 0a 20 20 20 20 70 2d 3e 70 52  K_ALL;.    p->pR
28a00 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20  ightmost = 0;.  
28a01 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
28a02 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 50  .      pNew = pP
28a03 72 69 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rior;.    }else{
28a04 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 50 72  .      pNew->pPr
28a05 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20  ior = pPrior;.  
28a06 20 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74      pNew->pRight
28a07 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  most = 0;.    }.
28a08 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20      p->pPrior = 
28a09 70 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 64 62  pNew;.    if( db
28a0a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
28a0b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a   return 1;.  }..
28a0c 20 20 2f 2a 20 42 65 67 69 6e 20 66 6c 61 74 74    /* Begin flatt
28a0d 65 6e 69 6e 67 20 74 68 65 20 69 46 72 6f 6d 2d  ening the iFrom-
28a0e 74 68 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20  th entry of the 
28a0f 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a 20 20 2a  FROM clause .  *
28a10 2a 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71  * in the outer q
28a11 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 70 53 75  uery..  */.  pSu
28a12 62 20 3d 20 70 53 75 62 31 20 3d 20 70 53 75 62  b = pSub1 = pSub
28a13 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 0a  item->pSelect;..
28a14 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
28a15 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20  transient table 
28a16 73 74 72 75 63 74 75 72 65 20 61 73 73 6f 63 69  structure associ
28a17 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 20 20  ated with the.  
28a18 2a 2a 20 73 75 62 71 75 65 72 79 0a 20 20 2a 2f  ** subquery.  */
28a19 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
28a1a 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a  (db, pSubitem->z
28a1b 44 61 74 61 62 61 73 65 29 3b 0a 20 20 73 71 6c  Database);.  sql
28a1c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
28a1d 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  Subitem->zName);
28a1e 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
28a1f 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a  (db, pSubitem->z
28a20 41 6c 69 61 73 29 3b 0a 20 20 70 53 75 62 69 74  Alias);.  pSubit
28a21 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20  em->zDatabase = 
28a22 30 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a  0;.  pSubitem->z
28a23 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 70 53 75 62  Name = 0;.  pSub
28a24 69 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 30  item->zAlias = 0
28a25 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 70 53  ;.  pSubitem->pS
28a26 65 6c 65 63 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a  elect = 0;..  /*
28a27 20 44 65 66 65 72 20 64 65 6c 65 74 69 6e 67 20   Defer deleting 
28a28 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74  the Table object
28a29 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
28a2a 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65   the.  ** subque
28a2b 72 79 20 75 6e 74 69 6c 20 63 6f 64 65 20 67 65  ry until code ge
28a2c 6e 65 72 61 74 69 6f 6e 20 69 73 0a 20 20 2a 2a  neration is.  **
28a2d 20 63 6f 6d 70 6c 65 74 65 2c 20 73 69 6e 63 65   complete, since
28a2e 20 74 68 65 72 65 20 6d 61 79 20 73 74 69 6c 6c   there may still
28a2f 20 65 78 69 73 74 20 45 78 70 72 2e 70 54 61 62   exist Expr.pTab
28a30 20 65 6e 74 72 69 65 73 20 74 68 61 74 0a 20 20   entries that.  
28a31 2a 2a 20 72 65 66 65 72 20 74 6f 20 74 68 65 20  ** refer to the 
28a32 73 75 62 71 75 65 72 79 20 65 76 65 6e 20 61 66  subquery even af
28a33 74 65 72 20 66 6c 61 74 74 65 6e 69 6e 67 2e 20  ter flattening. 
28a34 20 54 69 63 6b 65 74 20 23 33 33 34 36 2e 0a 20   Ticket #3346.. 
28a35 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 69 74   */.  if( pSubit
28a36 65 6d 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20  em->pTab!=0 ){. 
28a37 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 54 6f     Table *pTabTo
28a38 44 65 6c 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e  Del = pSubitem->
28a39 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54  pTab;.    if( pT
28a3a 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 3d 3d 31  abToDel->nRef==1
28a3b 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 54 6f   ){.      pTabTo
28a3c 44 65 6c 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69 65  Del->pNextZombie
28a3d 20 3d 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62   = pParse->pZomb
28a3e 69 65 54 61 62 3b 0a 20 20 20 20 20 20 70 50 61  ieTab;.      pPa
28a3f 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20  rse->pZombieTab 
28a40 3d 20 70 54 61 62 54 6f 44 65 6c 3b 0a 20 20 20  = pTabToDel;.   
28a41 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54   }else{.      pT
28a42 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 2d 2d 3b  abToDel->nRef--;
28a43 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 75 62 69  .    }.    pSubi
28a44 74 65 6d 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20  tem->pTab = 0;. 
28a45 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c   }..  /* The fol
28a46 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73  lowing loop runs
28a47 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74   once for each t
28a48 65 72 6d 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  erm in a compoun
28a49 64 2d 73 75 62 71 75 65 72 79 0a 20 20 2a 2a 20  d-subquery.  ** 
28a4a 66 6c 61 74 74 65 6e 69 6e 67 20 28 61 73 20 64  flattening (as d
28a4b 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 29 2e  escribed above).
28a4c 20 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e    If we are doin
28a4d 67 20 61 20 64 69 66 66 65 72 65 6e 74 20 6b 69  g a different ki
28a4e 6e 64 0a 20 20 2a 2a 20 6f 66 20 66 6c 61 74 74  nd.  ** of flatt
28a4f 65 6e 69 6e 67 20 2d 20 61 20 66 6c 61 74 74 65  ening - a flatte
28a50 6e 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20  ning other than 
28a51 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75  a compound-subqu
28a52 65 72 79 20 66 6c 61 74 74 65 6e 69 6e 67 20 2d  ery flattening -
28a53 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 69 73 20  .  ** then this 
28a54 6c 6f 6f 70 20 6f 6e 6c 79 20 72 75 6e 73 20 6f  loop only runs o
28a55 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  nce..  **.  ** T
28a56 68 69 73 20 6c 6f 6f 70 20 6d 6f 76 65 73 20 61  his loop moves a
28a57 6c 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 65  ll of the FROM e
28a58 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 73  lements of the s
28a59 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65  ubquery into the
28a5a 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63  .  ** the FROM c
28a5b 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74  lause of the out
28a5c 65 72 20 71 75 65 72 79 2e 20 20 42 65 66 6f 72  er query.  Befor
28a5d 65 20 64 6f 69 6e 67 20 74 68 69 73 2c 20 72 65  e doing this, re
28a5e 6d 65 6d 62 65 72 0a 20 20 2a 2a 20 74 68 65 20  member.  ** the 
28a5f 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  cursor number fo
28a60 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f  r the original o
28a61 75 74 65 72 20 71 75 65 72 79 20 46 52 4f 4d 20  uter query FROM 
28a62 65 6c 65 6d 65 6e 74 20 69 6e 0a 20 20 2a 2a 20  element in.  ** 
28a63 69 50 61 72 65 6e 74 2e 20 20 54 68 65 20 69 50  iParent.  The iP
28a64 61 72 65 6e 74 20 63 75 72 73 6f 72 20 77 69 6c  arent cursor wil
28a65 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e  l never be used.
28a66 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 6f 64    Subsequent cod
28a67 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e  e.  ** will scan
28a68 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 6f 6f   expressions loo
28a69 6b 69 6e 67 20 66 6f 72 20 69 50 61 72 65 6e 74  king for iParent
28a6a 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e 64 20   references and 
28a6b 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 74 68 6f  replace.  ** tho
28a6c 73 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 69  se references wi
28a6d 74 68 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74  th expressions t
28a6e 68 61 74 20 72 65 73 6f 6c 76 65 20 74 6f 20 74  hat resolve to t
28a6f 68 65 20 73 75 62 71 75 65 72 79 20 46 52 4f 4d  he subquery FROM
28a70 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 77  .  ** elements w
28a71 65 20 61 72 65 20 6e 6f 77 20 63 6f 70 79 69 6e  e are now copyin
28a72 67 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  g in..  */.  for
28a73 28 70 50 61 72 65 6e 74 3d 70 3b 20 70 50 61 72  (pParent=p; pPar
28a74 65 6e 74 3b 20 70 50 61 72 65 6e 74 3d 70 50 61  ent; pParent=pPa
28a75 72 65 6e 74 2d 3e 70 50 72 69 6f 72 2c 20 70 53  rent->pPrior, pS
28a76 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72 29  ub=pSub->pPrior)
28a77 7b 0a 20 20 20 20 69 6e 74 20 6e 53 75 62 53 72  {.    int nSubSr
28a78 63 3b 0a 20 20 20 20 75 38 20 6a 6f 69 6e 74 79  c;.    u8 jointy
28a79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 53 75 62  pe = 0;.    pSub
28a7a 53 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63  Src = pSub->pSrc
28a7b 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c  ;     /* FROM cl
28a7c 61 75 73 65 20 6f 66 20 73 75 62 71 75 65 72 79  ause of subquery
28a7d 20 2a 2f 0a 20 20 20 20 6e 53 75 62 53 72 63 20   */.    nSubSrc 
28a7e 3d 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63 3b  = pSubSrc->nSrc;
28a7f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
28a80 65 72 6d 73 20 69 6e 20 73 75 62 71 75 65 72 79  erms in subquery
28a81 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
28a82 20 20 20 20 70 53 72 63 20 3d 20 70 50 61 72 65      pSrc = pPare
28a83 6e 74 2d 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a  nt->pSrc;     /*
28a84 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
28a85 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
28a86 2a 2f 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63  */..    if( pSrc
28a87 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
28a88 28 20 70 50 61 72 65 6e 74 3d 3d 70 20 29 3b 20  ( pParent==p ); 
28a89 20 2f 2a 20 46 69 72 73 74 20 74 69 6d 65 20 74   /* First time t
28a8a 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 20  hrough the loop 
28a8b 2a 2f 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70  */.      jointyp
28a8c 65 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 6a 6f  e = pSubitem->jo
28a8d 69 6e 74 79 70 65 3b 0a 20 20 20 20 7d 65 6c 73  intype;.    }els
28a8e 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
28a8f 20 70 50 61 72 65 6e 74 21 3d 70 20 29 3b 20 20   pParent!=p );  
28a90 2f 2a 20 32 6e 64 20 61 6e 64 20 73 75 62 73 65  /* 2nd and subse
28a91 71 75 65 6e 74 20 74 69 6d 65 73 20 74 68 72 6f  quent times thro
28a92 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a  ugh the loop */.
28a93 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 50 61        pSrc = pPa
28a94 72 65 6e 74 2d 3e 70 53 72 63 20 3d 20 73 71 6c  rent->pSrc = sql
28a95 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
28a96 64 28 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  d(db, 0, 0, 0);.
28a97 20 20 20 20 20 20 69 66 28 20 70 53 72 63 3d 3d        if( pSrc==
28a98 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  0 ){.        ass
28a99 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
28a9a 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20  ailed );.       
28a9b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
28a9c 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68      }..    /* Th
28a9d 65 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20  e subquery uses 
28a9e 61 20 73 69 6e 67 6c 65 20 73 6c 6f 74 20 6f 66  a single slot of
28a9f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
28aa0 20 6f 66 20 74 68 65 20 6f 75 74 65 72 0a 20 20   of the outer.  
28aa1 20 20 2a 2a 20 71 75 65 72 79 2e 20 20 49 66 20    ** query.  If 
28aa2 74 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73  the subquery has
28aa3 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 65   more than one e
28aa4 6c 65 6d 65 6e 74 20 69 6e 20 69 74 73 20 46 52  lement in its FR
28aa5 4f 4d 20 63 6c 61 75 73 65 2c 0a 20 20 20 20 2a  OM clause,.    *
28aa6 2a 20 74 68 65 6e 20 65 78 70 61 6e 64 20 74 68  * then expand th
28aa7 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 74 6f  e outer query to
28aa8 20 6d 61 6b 65 20 73 70 61 63 65 20 66 6f 72 20   make space for 
28aa9 69 74 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 65  it to hold all e
28aaa 6c 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f  lements.    ** o
28aab 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a  f the subquery..
28aac 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 78      **.    ** Ex
28aad 61 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20  ample:.    **.  
28aae 20 20 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a    **    SELECT *
28aaf 20 46 52 4f 4d 20 74 61 62 41 2c 20 28 53 45 4c   FROM tabA, (SEL
28ab0 45 43 54 20 2a 20 46 52 4f 4d 20 73 75 62 31 2c  ECT * FROM sub1,
28ab1 20 73 75 62 32 29 2c 20 74 61 62 42 3b 0a 20 20   sub2), tabB;.  
28ab2 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
28ab3 6f 75 74 65 72 20 71 75 65 72 79 20 68 61 73 20  outer query has 
28ab4 33 20 73 6c 6f 74 73 20 69 6e 20 69 74 73 20 46  3 slots in its F
28ab5 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f 6e 65  ROM clause.  One
28ab6 20 73 6c 6f 74 20 6f 66 20 74 68 65 0a 20 20 20   slot of the.   
28ab7 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79 20   ** outer query 
28ab8 28 74 68 65 20 6d 69 64 64 6c 65 20 73 6c 6f 74  (the middle slot
28ab9 29 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  ) is used by the
28aba 20 73 75 62 71 75 65 72 79 2e 20 20 54 68 65 20   subquery.  The 
28abb 6e 65 78 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63  next.    ** bloc
28abc 6b 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 65  k of code will e
28abd 78 70 61 6e 64 20 74 68 65 20 6f 75 74 20 71 75  xpand the out qu
28abe 65 72 79 20 74 6f 20 34 20 73 6c 6f 74 73 2e 20  ery to 4 slots. 
28abf 20 54 68 65 20 6d 69 64 64 6c 65 0a 20 20 20 20   The middle.    
28ac0 2a 2a 20 73 6c 6f 74 20 69 73 20 65 78 70 61 6e  ** slot is expan
28ac1 64 65 64 20 74 6f 20 74 77 6f 20 73 6c 6f 74 73  ded to two slots
28ac2 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b   in order to mak
28ac3 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 0a  e space for the.
28ac4 20 20 20 20 2a 2a 20 74 77 6f 20 65 6c 65 6d 65      ** two eleme
28ac5 6e 74 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  nts in the FROM 
28ac6 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73 75  clause of the su
28ac7 62 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20  bquery..    */. 
28ac8 20 20 20 69 66 28 20 6e 53 75 62 53 72 63 3e 31     if( nSubSrc>1
28ac9 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 65 6e   ){.      pParen
28aca 74 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 20 3d  t->pSrc = pSrc =
28acb 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45   sqlite3SrcListE
28acc 6e 6c 61 72 67 65 28 64 62 2c 20 70 53 72 63 2c  nlarge(db, pSrc,
28acd 20 6e 53 75 62 53 72 63 2d 31 2c 69 46 72 6f 6d   nSubSrc-1,iFrom
28ace 2b 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64  +1);.      if( d
28acf 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
28ad0 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ){.        break
28ad1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
28ad2 0a 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 65 72  .    /* Transfer
28ad3 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
28ad4 20 74 65 72 6d 73 20 66 72 6f 6d 20 74 68 65 20   terms from the 
28ad5 73 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74 68  subquery into th
28ad6 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71  e.    ** outer q
28ad7 75 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  uery..    */.   
28ad8 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 75 62   for(i=0; i<nSub
28ad9 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Src; i++){.     
28ada 20 70 53 72 63 2d 3e 61 5b 69 2b 69 46 72 6f 6d   pSrc->a[i+iFrom
28adb 5d 20 3d 20 70 53 75 62 53 72 63 2d 3e 61 5b 69  ] = pSubSrc->a[i
28adc 5d 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  ];.      memset(
28add 26 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 2c 20  &pSubSrc->a[i], 
28ade 30 2c 20 73 69 7a 65 6f 66 28 70 53 75 62 53 72  0, sizeof(pSubSr
28adf 63 2d 3e 61 5b 69 5d 29 29 3b 0a 20 20 20 20 7d  c->a[i]));.    }
28ae0 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 46 72  .    pSrc->a[iFr
28ae1 6f 6d 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 6a  om].jointype = j
28ae2 6f 69 6e 74 79 70 65 3b 0a 20 20 0a 20 20 20 20  ointype;.  .    
28ae3 2f 2a 20 4e 6f 77 20 62 65 67 69 6e 20 73 75 62  /* Now begin sub
28ae4 73 74 69 74 75 74 69 6e 67 20 73 75 62 71 75 65  stituting subque
28ae5 72 79 20 72 65 73 75 6c 74 20 73 65 74 20 65 78  ry result set ex
28ae6 70 72 65 73 73 69 6f 6e 73 20 66 6f 72 20 0a 20  pressions for . 
28ae7 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73     ** references
28ae8 20 74 6f 20 74 68 65 20 69 50 61 72 65 6e 74 20   to the iParent 
28ae9 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  in the outer que
28aea 72 79 2e 0a 20 20 20 20 2a 2a 20 0a 20 20 20 20  ry..    ** .    
28aeb 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 20 20  ** Example:.    
28aec 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 53 45 4c 45  **.    **   SELE
28aed 43 54 20 61 2b 35 2c 20 62 2a 31 30 20 46 52 4f  CT a+5, b*10 FRO
28aee 4d 20 28 53 45 4c 45 43 54 20 78 2a 33 20 41 53  M (SELECT x*3 AS
28aef 20 61 2c 20 79 2b 31 30 20 41 53 20 62 20 46 52   a, y+10 AS b FR
28af0 4f 4d 20 74 31 29 20 57 48 45 52 45 20 61 3e 62  OM t1) WHERE a>b
28af1 3b 0a 20 20 20 20 2a 2a 20 20 20 5c 20 20 20 20  ;.    **   \    
28af2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28af3 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20   \_____________ 
28af4 73 75 62 71 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f  subquery _______
28af5 5f 5f 5f 2f 20 20 20 20 20 20 20 20 20 20 2f 0a  ___/          /.
28af6 20 20 20 20 2a 2a 20 20 20 20 5c 5f 5f 5f 5f 5f      **    \_____
28af7 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
28af8 20 6f 75 74 65 72 20 71 75 65 72 79 20 5f 5f 5f   outer query ___
28af9 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
28afa 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 20 20 20  ___________/.   
28afb 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 65 20 6c 6f   **.    ** We lo
28afc 6f 6b 20 61 74 20 65 76 65 72 79 20 65 78 70 72  ok at every expr
28afd 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 6f 75  ession in the ou
28afe 74 65 72 20 71 75 65 72 79 20 61 6e 64 20 65 76  ter query and ev
28aff 65 72 79 20 70 6c 61 63 65 20 77 65 20 73 65 65  ery place we see
28b00 0a 20 20 20 20 2a 2a 20 22 61 22 20 77 65 20 73  .    ** "a" we s
28b01 75 62 73 74 69 74 75 74 65 20 22 78 2a 33 22 20  ubstitute "x*3" 
28b02 61 6e 64 20 65 76 65 72 79 20 70 6c 61 63 65 20  and every place 
28b03 77 65 20 73 65 65 20 22 62 22 20 77 65 20 73 75  we see "b" we su
28b04 62 73 74 69 74 75 74 65 20 22 79 2b 31 30 22 2e  bstitute "y+10".
28b05 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 69 73  .    */.    pLis
28b06 74 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c  t = pParent->pEL
28b07 69 73 74 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  ist;.    for(i=0
28b08 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ; i<pList->nExpr
28b09 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78  ; i++){.      Ex
28b0a 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20 20  pr *pExpr;.     
28b0b 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b 69 5d   if( pList->a[i]
28b0c 2e 7a 4e 61 6d 65 3d 3d 30 20 26 26 20 28 70 45  .zName==0 && (pE
28b0d 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69  xpr = pList->a[i
28b0e 5d 2e 70 45 78 70 72 29 2d 3e 73 70 61 6e 2e 7a  ].pExpr)->span.z
28b0f 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  !=0 ){.        p
28b10 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
28b11 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20   = .            
28b12 20 20 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e     sqlite3DbStrN
28b13 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70  Dup(db, (char*)p
28b14 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 2c 20 70 45  Expr->span.z, pE
28b15 78 70 72 2d 3e 73 70 61 6e 2e 6e 29 3b 0a 20 20  xpr->span.n);.  
28b16 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
28b17 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62  substExprList(db
28b18 2c 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69 73  , pParent->pELis
28b19 74 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62  t, iParent, pSub
28b1a 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 69  ->pEList);.    i
28b1b 66 28 20 69 73 41 67 67 20 29 7b 0a 20 20 20 20  f( isAgg ){.    
28b1c 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
28b1d 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 47 72  db, pParent->pGr
28b1e 6f 75 70 42 79 2c 20 69 50 61 72 65 6e 74 2c 20  oupBy, iParent, 
28b1f 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  pSub->pEList);. 
28b20 20 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64       substExpr(d
28b21 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76  b, pParent->pHav
28b22 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  ing, iParent, pS
28b23 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  ub->pEList);.   
28b24 20 7d 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d   }.    if( pSub-
28b25 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
28b26 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65     assert( pPare
28b27 6e 74 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20  nt->pOrderBy==0 
28b28 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  );.      pParent
28b29 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 53 75  ->pOrderBy = pSu
28b2a 62 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20  b->pOrderBy;.   
28b2b 20 20 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42     pSub->pOrderB
28b2c 79 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  y = 0;.    }else
28b2d 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f   if( pParent->pO
28b2e 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20  rderBy ){.      
28b2f 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62  substExprList(db
28b30 2c 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65  , pParent->pOrde
28b31 72 42 79 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  rBy, iParent, pS
28b32 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  ub->pEList);.   
28b33 20 7d 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d   }.    if( pSub-
28b34 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 20  >pWhere ){.     
28b35 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65   pWhere = sqlite
28b36 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53 75  3ExprDup(db, pSu
28b37 62 2d 3e 70 57 68 65 72 65 2c 20 30 29 3b 0a 20  b->pWhere, 0);. 
28b38 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
28b39 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20 20  pWhere = 0;.    
28b3a 7d 0a 20 20 20 20 69 66 28 20 73 75 62 71 75 65  }.    if( subque
28b3b 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20 20 20  ryIsAgg ){.     
28b3c 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
28b3d 2d 3e 70 48 61 76 69 6e 67 3d 3d 30 20 29 3b 0a  ->pHaving==0 );.
28b3e 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
28b3f 48 61 76 69 6e 67 20 3d 20 70 50 61 72 65 6e 74  Having = pParent
28b40 2d 3e 70 57 68 65 72 65 3b 0a 20 20 20 20 20 20  ->pWhere;.      
28b41 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20  pParent->pWhere 
28b42 3d 20 70 57 68 65 72 65 3b 0a 20 20 20 20 20 20  = pWhere;.      
28b43 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 50  substExpr(db, pP
28b44 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20  arent->pHaving, 
28b45 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70  iParent, pSub->p
28b46 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70 50  EList);.      pP
28b47 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d  arent->pHaving =
28b48 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
28b49 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61  db, pParent->pHa
28b4a 76 69 6e 67 2c 20 0a 20 20 20 20 20 20 20 20 20  ving, .         
28b4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b4c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28b4d 45 78 70 72 44 75 70 28 64 62 2c 20 70 53 75 62  ExprDup(db, pSub
28b4e 2d 3e 70 48 61 76 69 6e 67 2c 20 30 29 29 3b 0a  ->pHaving, 0));.
28b4f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
28b50 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 3d  arent->pGroupBy=
28b51 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72  =0 );.      pPar
28b52 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20  ent->pGroupBy = 
28b53 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
28b54 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 47 72  up(db, pSub->pGr
28b55 6f 75 70 42 79 2c 20 30 29 3b 0a 20 20 20 20 7d  oupBy, 0);.    }
28b56 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 75 62 73  else{.      subs
28b57 74 45 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e  tExpr(db, pParen
28b58 74 2d 3e 70 57 68 65 72 65 2c 20 69 50 61 72 65  t->pWhere, iPare
28b59 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74  nt, pSub->pEList
28b5a 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  );.      pParent
28b5b 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74  ->pWhere = sqlit
28b5c 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 50  e3ExprAnd(db, pP
28b5d 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c 20 70  arent->pWhere, p
28b5e 57 68 65 72 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Where);.    }.  
28b5f 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 74  .    /* The flat
28b60 74 65 6e 65 64 20 71 75 65 72 79 20 69 73 20 64  tened query is d
28b61 69 73 74 69 6e 63 74 20 69 66 20 65 69 74 68 65  istinct if eithe
28b62 72 20 74 68 65 20 69 6e 6e 65 72 20 6f 72 20 74  r the inner or t
28b63 68 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20  he.    ** outer 
28b64 71 75 65 72 79 20 69 73 20 64 69 73 74 69 6e 63  query is distinc
28b65 74 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  t. .    */.    p
28b66 50 61 72 65 6e 74 2d 3e 73 65 6c 46 6c 61 67 73  Parent->selFlags
28b67 20 7c 3d 20 70 53 75 62 2d 3e 73 65 6c 46 6c 61   |= pSub->selFla
28b68 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74  gs & SF_Distinct
28b69 3b 0a 20 20 0a 20 20 20 20 2f 2a 0a 20 20 20 20  ;.  .    /*.    
28b6a 2a 2a 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52  ** SELECT ... FR
28b6b 4f 4d 20 28 53 45 4c 45 43 54 20 2e 2e 2e 20 4c  OM (SELECT ... L
28b6c 49 4d 49 54 20 61 20 4f 46 46 53 45 54 20 62 29  IMIT a OFFSET b)
28b6d 20 4c 49 4d 49 54 20 78 20 4f 46 46 53 45 54 20   LIMIT x OFFSET 
28b6e 79 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  y;.    **.    **
28b6f 20 4f 6e 65 20 69 73 20 74 65 6d 70 74 65 64 20   One is tempted 
28b70 74 6f 20 74 72 79 20 74 6f 20 61 64 64 20 61 20  to try to add a 
28b71 61 6e 64 20 62 20 74 6f 20 63 6f 6d 62 69 6e 65  and b to combine
28b72 20 74 68 65 20 6c 69 6d 69 74 73 2e 20 20 42 75   the limits.  Bu
28b73 74 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 64 6f  t this.    ** do
28b74 65 73 20 6e 6f 74 20 77 6f 72 6b 20 69 66 20 65  es not work if e
28b75 69 74 68 65 72 20 6c 69 6d 69 74 20 69 73 20 6e  ither limit is n
28b76 65 67 61 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a  egative..    */.
28b77 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c      if( pSub->pL
28b78 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 70 50  imit ){.      pP
28b79 61 72 65 6e 74 2d 3e 70 4c 69 6d 69 74 20 3d 20  arent->pLimit = 
28b7a 70 53 75 62 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20  pSub->pLimit;.  
28b7b 20 20 20 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74      pSub->pLimit
28b7c 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
28b7d 0a 20 20 2f 2a 20 46 69 6e 69 61 6c 6c 79 2c 20  .  /* Finially, 
28b7e 64 65 6c 65 74 65 20 77 68 61 74 20 69 73 20 6c  delete what is l
28b7f 65 66 74 20 6f 66 20 74 68 65 20 73 75 62 71 75  eft of the subqu
28b80 65 72 79 20 61 6e 64 20 72 65 74 75 72 6e 0a 20  ery and return. 
28b81 20 2a 2a 20 73 75 63 63 65 73 73 2e 0a 20 20 2a   ** success..  *
28b82 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  /.  sqlite3Selec
28b83 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 75 62  tDelete(db, pSub
28b84 31 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b  1);..  return 1;
28b85 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  .}.#endif /* !de
28b86 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
28b87 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21  T_SUBQUERY) || !
28b88 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
28b89 4d 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 2f 2a  MIT_VIEW) */../*
28b8a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20  .** Analyze the 
28b8b 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
28b8c 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72   passed as an ar
28b8d 67 75 6d 65 6e 74 20 74 6f 20 73 65 65 20 69 66  gument to see if
28b8e 20 69 74 0a 2a 2a 20 69 73 20 61 20 6d 69 6e 28   it.** is a min(
28b8f 29 20 6f 72 20 6d 61 78 28 29 20 71 75 65 72 79  ) or max() query
28b90 2e 20 52 65 74 75 72 6e 20 57 48 45 52 45 5f 4f  . Return WHERE_O
28b91 52 44 45 52 42 59 5f 4d 49 4e 20 6f 72 20 57 48  RDERBY_MIN or WH
28b92 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20  ERE_ORDERBY_MAX 
28b93 69 66 20 0a 2a 2a 20 69 74 20 69 73 2c 20 6f 72  if .** it is, or
28b94 20 30 20 6f 74 68 65 72 77 69 73 65 2e 20 41 74   0 otherwise. At
28b95 20 70 72 65 73 65 6e 74 2c 20 61 20 71 75 65 72   present, a quer
28b96 79 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  y is considered 
28b97 74 6f 20 62 65 0a 2a 2a 20 61 20 6d 69 6e 28 29  to be.** a min()
28b98 2f 6d 61 78 28 29 20 71 75 65 72 79 20 69 66 3a  /max() query if:
28b99 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 54 68 65 72  .**.**   1. Ther
28b9a 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 6f 62  e is a single ob
28b9b 6a 65 63 74 20 69 6e 20 74 68 65 20 46 52 4f 4d  ject in the FROM
28b9c 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
28b9d 20 32 2e 20 54 68 65 72 65 20 69 73 20 61 20 73   2. There is a s
28b9e 69 6e 67 6c 65 20 65 78 70 72 65 73 73 69 6f 6e  ingle expression
28b9f 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
28ba0 65 74 2c 20 61 6e 64 20 69 74 20 69 73 0a 2a 2a  et, and it is.**
28ba1 20 20 20 20 20 20 65 69 74 68 65 72 20 6d 69 6e        either min
28ba2 28 78 29 20 6f 72 20 6d 61 78 28 78 29 2c 20 77  (x) or max(x), w
28ba3 68 65 72 65 20 78 20 69 73 20 61 20 63 6f 6c 75  here x is a colu
28ba4 6d 6e 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2f  mn reference..*/
28ba5 0a 73 74 61 74 69 63 20 75 38 20 6d 69 6e 4d 61  .static u8 minMa
28ba6 78 51 75 65 72 79 28 53 65 6c 65 63 74 20 2a 70  xQuery(Select *p
28ba7 29 7b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  ){.  Expr *pExpr
28ba8 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ;.  ExprList *pE
28ba9 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74  List = p->pEList
28baa 3b 0a 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  ;..  if( pEList-
28bab 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75  >nExpr!=1 ) retu
28bac 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  rn WHERE_ORDERBY
28bad 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 70 45 78 70 72  _NORMAL;.  pExpr
28bae 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e   = pEList->a[0].
28baf 70 45 78 70 72 3b 0a 20 20 69 66 28 20 45 78 70  pExpr;.  if( Exp
28bb0 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
28bb1 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
28bb2 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
28bb3 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  pEList = pExpr->
28bb4 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  x.pList;.  if( p
28bb5 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47  Expr->op!=TK_AGG
28bb6 5f 46 55 4e 43 54 49 4f 4e 20 7c 7c 20 70 45 4c  _FUNCTION || pEL
28bb7 69 73 74 3d 3d 30 20 7c 7c 20 70 45 4c 69 73 74  ist==0 || pEList
28bb8 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74  ->nExpr!=1 ) ret
28bb9 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 4c  urn 0;.  if( pEL
28bba 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d  ist->a[0].pExpr-
28bbb 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55  >op!=TK_AGG_COLU
28bbc 4d 4e 20 29 20 72 65 74 75 72 6e 20 57 48 45 52  MN ) return WHER
28bbd 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c  E_ORDERBY_NORMAL
28bbe 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74  ;.  if( pExpr->t
28bbf 6f 6b 65 6e 2e 6e 21 3d 33 20 29 20 72 65 74 75  oken.n!=3 ) retu
28bc0 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  rn WHERE_ORDERBY
28bc1 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 69 66 28 20 73  _NORMAL;.  if( s
28bc2 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28  qlite3StrNICmp((
28bc3 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
28bc4 65 6e 2e 7a 2c 22 6d 69 6e 22 2c 33 29 3d 3d 30  en.z,"min",3)==0
28bc5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
28bc6 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
28bc7 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
28bc8 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63  lite3StrNICmp((c
28bc9 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
28bca 6e 2e 7a 2c 22 6d 61 78 22 2c 33 29 3d 3d 30 20  n.z,"max",3)==0 
28bcb 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 48  ){.    return WH
28bcc 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 3b  ERE_ORDERBY_MAX;
28bcd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 48  .  }.  return WH
28bce 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d  ERE_ORDERBY_NORM
28bcf 41 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  AL;.}../*.** The
28bd0 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   select statemen
28bd1 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  t passed as the 
28bd2 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
28bd3 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 71  s an aggregate q
28bd4 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63  uery..** The sec
28bd5 6f 6e 64 20 61 72 67 6d 65 6e 74 20 69 73 20 74  ond argment is t
28bd6 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 61 67  he associated ag
28bd7 67 72 65 67 61 74 65 2d 69 6e 66 6f 20 6f 62 6a  gregate-info obj
28bd8 65 63 74 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75  ect. This .** fu
28bd9 6e 63 74 69 6f 6e 20 74 65 73 74 73 20 69 66 20  nction tests if 
28bda 74 68 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66  the SELECT is of
28bdb 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a   the form:.**.**
28bdc 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28     SELECT count(
28bdd 2a 29 20 46 52 4f 4d 20 3c 74 62 6c 3e 0a 2a 2a  *) FROM <tbl>.**
28bde 0a 2a 2a 20 77 68 65 72 65 20 74 61 62 6c 65 20  .** where table 
28bdf 69 73 20 61 20 64 61 74 61 62 61 73 65 20 74 61  is a database ta
28be0 62 6c 65 2c 20 6e 6f 74 20 61 20 73 75 62 2d 73  ble, not a sub-s
28be1 65 6c 65 63 74 20 6f 72 20 76 69 65 77 2e 20 49  elect or view. I
28be2 66 20 74 68 65 20 71 75 65 72 79 0a 2a 2a 20 64  f the query.** d
28be3 6f 65 73 20 6d 61 74 63 68 20 74 68 69 73 20 70  oes match this p
28be4 61 74 74 65 72 6e 2c 20 74 68 65 6e 20 61 20 70  attern, then a p
28be5 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 54 61  ointer to the Ta
28be6 62 6c 65 20 6f 62 6a 65 63 74 20 72 65 70 72 65  ble object repre
28be7 73 65 6e 74 69 6e 67 0a 2a 2a 20 3c 74 62 6c 3e  senting.** <tbl>
28be8 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
28be9 68 65 72 77 69 73 65 2c 20 30 20 69 73 20 72 65  herwise, 0 is re
28bea 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
28beb 63 20 54 61 62 6c 65 20 2a 69 73 53 69 6d 70 6c  c Table *isSimpl
28bec 65 43 6f 75 6e 74 28 53 65 6c 65 63 74 20 2a 70  eCount(Select *p
28bed 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49  , AggInfo *pAggI
28bee 6e 66 6f 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  nfo){.  Table *p
28bef 54 61 62 3b 0a 20 20 45 78 70 72 20 2a 70 45 78  Tab;.  Expr *pEx
28bf0 70 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21  pr;..  assert( !
28bf1 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 3b 0a 0a  p->pGroupBy );..
28bf2 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20    if( p->pWhere 
28bf3 7c 7c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  || p->pEList->nE
28bf4 78 70 72 21 3d 31 20 0a 20 20 20 7c 7c 20 70 2d  xpr!=1 .   || p-
28bf5 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 7c  >pSrc->nSrc!=1 |
28bf6 7c 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e  | p->pSrc->a[0].
28bf7 70 53 65 6c 65 63 74 0a 20 20 29 7b 0a 20 20 20  pSelect.  ){.   
28bf8 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
28bf9 20 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d   pTab = p->pSrc-
28bfa 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 70 45  >a[0].pTab;.  pE
28bfb 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d  xpr = p->pEList-
28bfc 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 61  >a[0].pExpr;.  a
28bfd 73 73 65 72 74 28 20 70 54 61 62 20 26 26 20 21  ssert( pTab && !
28bfe 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 26 26  pTab->pSelect &&
28bff 20 70 45 78 70 72 20 29 3b 0a 0a 20 20 69 66 28   pExpr );..  if(
28c00 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
28c01 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
28c02 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  f( pExpr->op!=TK
28c03 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 20  _AGG_FUNCTION ) 
28c04 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
28c05 28 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63  (pAggInfo->aFunc
28c06 5b 30 5d 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73  [0].pFunc->flags
28c07 26 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 55  &SQLITE_FUNC_COU
28c08 4e 54 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  NT)==0 ) return 
28c09 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  0;.  if( pExpr->
28c0a 66 6c 61 67 73 26 45 50 5f 44 69 73 74 69 6e 63  flags&EP_Distinc
28c0b 74 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  t ) return 0;.. 
28c0c 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a   return pTab;.}.
28c0d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 6f  ./*.** If the so
28c0e 75 72 63 65 2d 6c 69 73 74 20 69 74 65 6d 20 70  urce-list item p
28c0f 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75  assed as an argu
28c10 6d 65 6e 74 20 77 61 73 20 61 75 67 6d 65 6e 74  ment was augment
28c11 65 64 20 77 69 74 68 20 61 6e 0a 2a 2a 20 49 4e  ed with an.** IN
28c12 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 2c  DEXED BY clause,
28c13 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6c 6f 63   then try to loc
28c14 61 74 65 20 74 68 65 20 73 70 65 63 69 66 69 65  ate the specifie
28c15 64 20 69 6e 64 65 78 2e 20 49 66 20 74 68 65 72  d index. If ther
28c16 65 0a 2a 2a 20 77 61 73 20 73 75 63 68 20 61 20  e.** was such a 
28c17 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20 6e  clause and the n
28c18 61 6d 65 64 20 69 6e 64 65 78 20 63 61 6e 6e 6f  amed index canno
28c19 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65 74 75  t be found, retu
28c1a 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52  rn .** SQLITE_ER
28c1b 52 4f 52 20 61 6e 64 20 6c 65 61 76 65 20 61 6e  ROR and leave an
28c1c 20 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73 65   error in pParse
28c1d 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 70 6f 70  . Otherwise, pop
28c1e 75 6c 61 74 65 20 0a 2a 2a 20 70 46 72 6f 6d 2d  ulate .** pFrom-
28c1f 3e 70 49 6e 64 65 78 20 61 6e 64 20 72 65 74 75  >pIndex and retu
28c20 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  rn SQLITE_OK..*/
28c21 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28c22 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78  int sqlite3Index
28c23 65 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65  edByLookup(Parse
28c24 20 2a 70 50 61 72 73 65 2c 20 73 74 72 75 63 74   *pParse, struct
28c25 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
28c26 46 72 6f 6d 29 7b 0a 20 20 69 66 28 20 70 46 72  From){.  if( pFr
28c27 6f 6d 2d 3e 70 54 61 62 20 26 26 20 70 46 72 6f  om->pTab && pFro
28c28 6d 2d 3e 7a 49 6e 64 65 78 20 29 7b 0a 20 20 20  m->zIndex ){.   
28c29 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
28c2a 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  From->pTab;.    
28c2b 63 68 61 72 20 2a 7a 49 6e 64 65 78 20 3d 20 70  char *zIndex = p
28c2c 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78 3b 0a 20 20  From->zIndex;.  
28c2d 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
28c2e 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62     for(pIdx=pTab
28c2f 2d 3e 70 49 6e 64 65 78 3b 20 0a 20 20 20 20 20  ->pIndex; .     
28c30 20 20 20 70 49 64 78 20 26 26 20 73 71 6c 69 74     pIdx && sqlit
28c31 65 33 53 74 72 49 43 6d 70 28 70 49 64 78 2d 3e  e3StrICmp(pIdx->
28c32 7a 4e 61 6d 65 2c 20 7a 49 6e 64 65 78 29 3b 20  zName, zIndex); 
28c33 0a 20 20 20 20 20 20 20 20 70 49 64 78 3d 70 49  .        pIdx=pI
28c34 64 78 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 3b  dx->pNext.    );
28c35 0a 20 20 20 20 69 66 28 20 21 70 49 64 78 20 29  .    if( !pIdx )
28c36 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
28c37 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
28c38 22 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20  "no such index: 
28c39 25 73 22 2c 20 7a 49 6e 64 65 78 2c 20 30 29 3b  %s", zIndex, 0);
28c3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
28c3b 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
28c3c 7d 0a 20 20 20 20 70 46 72 6f 6d 2d 3e 70 49 6e  }.    pFrom->pIn
28c3d 64 65 78 20 3d 20 70 49 64 78 3b 0a 20 20 7d 0a  dex = pIdx;.  }.
28c3e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
28c3f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OK;.}../*.** Thi
28c40 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 57  s routine is a W
28c41 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b 20 66  alker callback f
28c42 6f 72 20 22 65 78 70 61 6e 64 69 6e 67 22 20 61  or "expanding" a
28c43 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
28c44 74 2e 0a 2a 2a 20 22 45 78 70 61 6e 64 69 6e 67  t..** "Expanding
28c45 22 20 6d 65 61 6e 73 20 74 6f 20 64 6f 20 74 68  " means to do th
28c46 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
28c47 2a 2a 20 20 20 20 28 31 29 20 20 4d 61 6b 65 20  **    (1)  Make 
28c48 73 75 72 65 20 56 44 42 45 20 63 75 72 73 6f 72  sure VDBE cursor
28c49 20 6e 75 6d 62 65 72 73 20 68 61 76 65 20 62 65   numbers have be
28c4a 65 6e 20 61 73 73 69 67 6e 65 64 20 74 6f 20 65  en assigned to e
28c4b 76 65 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  very.**         
28c4c 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 46  element of the F
28c4d 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  ROM clause..**.*
28c4e 2a 20 20 20 20 28 32 29 20 20 46 69 6c 6c 20 69  *    (2)  Fill i
28c4f 6e 20 74 68 65 20 70 54 61 62 4c 69 73 74 2d 3e  n the pTabList->
28c50 61 5b 5d 2e 70 54 61 62 20 66 69 65 6c 64 73 20  a[].pTab fields 
28c51 69 6e 20 74 68 65 20 53 72 63 4c 69 73 74 20 74  in the SrcList t
28c52 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  hat .**         
28c53 64 65 66 69 6e 65 73 20 46 52 4f 4d 20 63 6c 61  defines FROM cla
28c54 75 73 65 2e 20 20 57 68 65 6e 20 76 69 65 77 73  use.  When views
28c55 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 46   appear in the F
28c56 52 4f 4d 20 63 6c 61 75 73 65 2c 0a 2a 2a 20 20  ROM clause,.**  
28c57 20 20 20 20 20 20 20 66 69 6c 6c 20 70 54 61 62         fill pTab
28c58 4c 69 73 74 2d 3e 61 5b 5d 2e 70 53 65 6c 65 63  List->a[].pSelec
28c59 74 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66  t with a copy of
28c5a 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
28c5b 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20  ement.**        
28c5c 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
28c5d 20 74 68 65 20 76 69 65 77 2e 20 20 41 20 63 6f   the view.  A co
28c5e 70 79 20 69 73 20 6d 61 64 65 20 6f 66 20 74 68  py is made of th
28c5f 65 20 76 69 65 77 27 73 20 53 45 4c 45 43 54 0a  e view's SELECT.
28c60 2a 2a 20 20 20 20 20 20 20 20 20 73 74 61 74 65  **         state
28c61 6d 65 6e 74 20 73 6f 20 74 68 61 74 20 77 65 20  ment so that we 
28c62 63 61 6e 20 66 72 65 65 6c 79 20 6d 6f 64 69 66  can freely modif
28c63 79 20 6f 72 20 64 65 6c 65 74 65 20 74 68 61 74  y or delete that
28c64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20   statement.**   
28c65 20 20 20 20 20 20 77 69 74 68 6f 75 74 20 77 6f        without wo
28c66 72 72 79 69 6e 67 20 61 62 6f 75 74 20 6d 65 73  rrying about mes
28c67 73 69 6e 67 20 75 70 20 74 68 65 20 70 72 65 73  sing up the pres
28c68 69 73 74 65 6e 74 20 72 65 70 72 65 73 65 6e 74  istent represent
28c69 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ation.**        
28c6a 20 6f 66 20 74 68 65 20 76 69 65 77 2e 0a 2a 2a   of the view..**
28c6b 0a 2a 2a 20 20 20 20 28 33 29 20 20 41 64 64 20  .**    (3)  Add 
28c6c 74 65 72 6d 73 20 74 6f 20 74 68 65 20 57 48 45  terms to the WHE
28c6d 52 45 20 63 6c 61 75 73 65 20 74 6f 20 61 63 63  RE clause to acc
28c6e 6f 6d 6f 64 61 74 65 20 74 68 65 20 4e 41 54 55  omodate the NATU
28c6f 52 41 4c 20 6b 65 79 77 6f 72 64 0a 2a 2a 20 20  RAL keyword.**  
28c70 20 20 20 20 20 20 20 6f 6e 20 6a 6f 69 6e 73 20         on joins 
28c71 61 6e 64 20 74 68 65 20 4f 4e 20 61 6e 64 20 55  and the ON and U
28c72 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 6a  SING clause of j
28c73 6f 69 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28  oins..**.**    (
28c74 34 29 20 20 53 63 61 6e 20 74 68 65 20 6c 69 73  4)  Scan the lis
28c75 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  t of columns in 
28c76 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 28  the result set (
28c77 70 45 4c 69 73 74 29 20 6c 6f 6f 6b 69 6e 67 0a  pEList) looking.
28c78 2a 2a 20 20 20 20 20 20 20 20 20 66 6f 72 20 69  **         for i
28c79 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20  nstances of the 
28c7a 22 2a 22 20 6f 70 65 72 61 74 6f 72 20 6f 72 20  "*" operator or 
28c7b 74 68 65 20 54 41 42 4c 45 2e 2a 20 6f 70 65 72  the TABLE.* oper
28c7c 61 74 6f 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20  ator..**        
28c7d 20 49 66 20 66 6f 75 6e 64 2c 20 65 78 70 61 6e   If found, expan
28c7e 64 20 65 61 63 68 20 22 2a 22 20 74 6f 20 62 65  d each "*" to be
28c7f 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e   every column in
28c80 20 65 76 65 72 79 20 74 61 62 6c 65 0a 2a 2a 20   every table.** 
28c81 20 20 20 20 20 20 20 20 61 6e 64 20 54 41 42 4c          and TABL
28c82 45 2e 2a 20 74 6f 20 62 65 20 65 76 65 72 79 20  E.* to be every 
28c83 63 6f 6c 75 6d 6e 20 69 6e 20 54 41 42 4c 45 2e  column in TABLE.
28c84 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  .**.*/.static in
28c85 74 20 73 65 6c 65 63 74 45 78 70 61 6e 64 65 72  t selectExpander
28c86 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
28c87 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
28c88 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
28c89 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b  pWalker->pParse;
28c8a 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 0a  .  int i, j, k;.
28c8b 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
28c8c 69 73 74 3b 0a 20 20 45 78 70 72 4c 69 73 74 20  ist;.  ExprList 
28c8d 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 75 63  *pEList;.  struc
28c8e 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
28c8f 70 46 72 6f 6d 3b 0a 20 20 73 71 6c 69 74 65 33  pFrom;.  sqlite3
28c90 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
28c91 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  b;..  if( db->ma
28c92 6c 6c 6f 63 46 61 69 6c 65 64 20 20 29 7b 0a 20  llocFailed  ){. 
28c93 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
28c94 6f 72 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ort;.  }.  if( p
28c95 2d 3e 70 53 72 63 3d 3d 30 20 7c 7c 20 28 70 2d  ->pSrc==0 || (p-
28c96 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45  >selFlags & SF_E
28c97 78 70 61 6e 64 65 64 29 21 3d 30 20 29 7b 0a 20  xpanded)!=0 ){. 
28c98 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
28c99 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 73 65  une;.  }.  p->se
28c9a 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 45 78 70  lFlags |= SF_Exp
28c9b 61 6e 64 65 64 3b 0a 20 20 70 54 61 62 4c 69 73  anded;.  pTabLis
28c9c 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70  t = p->pSrc;.  p
28c9d 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
28c9e 74 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  t;..  /* Make su
28c9f 72 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  re cursor number
28ca0 73 20 68 61 76 65 20 62 65 65 6e 20 61 73 73 69  s have been assi
28ca1 67 6e 65 64 20 74 6f 20 61 6c 6c 20 65 6e 74 72  gned to all entr
28ca2 69 65 73 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  ies in.  ** the 
28ca3 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
28ca4 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
28ca5 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ent..  */.  sqli
28ca6 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e  te3SrcListAssign
28ca7 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20  Cursors(pParse, 
28ca8 70 54 61 62 4c 69 73 74 29 3b 0a 0a 20 20 2f 2a  pTabList);..  /*
28ca9 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20 74   Look up every t
28caa 61 62 6c 65 20 6e 61 6d 65 64 20 69 6e 20 74 68  able named in th
28cab 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
28cac 20 74 68 65 20 73 65 6c 65 63 74 2e 20 20 49 66   the select.  If
28cad 0a 20 20 2a 2a 20 61 6e 20 65 6e 74 72 79 20 6f  .  ** an entry o
28cae 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
28caf 65 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20  e is a subquery 
28cb0 69 6e 73 74 65 61 64 20 6f 66 20 61 20 74 61 62  instead of a tab
28cb1 6c 65 20 6f 72 20 76 69 65 77 2c 0a 20 20 2a 2a  le or view,.  **
28cb2 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 74   then create a t
28cb3 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 73  ransient table s
28cb4 74 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63  tructure to desc
28cb5 72 69 62 65 20 74 68 65 20 73 75 62 71 75 65 72  ribe the subquer
28cb6 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  y..  */.  for(i=
28cb7 30 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73  0, pFrom=pTabLis
28cb8 74 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74  t->a; i<pTabList
28cb9 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72  ->nSrc; i++, pFr
28cba 6f 6d 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65  om++){.    Table
28cbb 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20   *pTab;.    if( 
28cbc 70 46 72 6f 6d 2d 3e 70 54 61 62 21 3d 30 20 29  pFrom->pTab!=0 )
28cbd 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  {.      /* This 
28cbe 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 61 6c  statement has al
28cbf 72 65 61 64 79 20 62 65 65 6e 20 70 72 65 70 61  ready been prepa
28cc0 72 65 64 2e 20 20 54 68 65 72 65 20 69 73 20 6e  red.  There is n
28cc1 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 2a 2a 20  o need.      ** 
28cc2 74 6f 20 67 6f 20 66 75 72 74 68 65 72 2e 20 2a  to go further. *
28cc3 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
28cc4 69 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 65  i==0 );.      re
28cc5 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
28cc6 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46      }.    if( pF
28cc7 72 6f 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b  rom->zName==0 ){
28cc8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28cc9 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
28cca 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c      Select *pSel
28ccb 20 3d 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63   = pFrom->pSelec
28ccc 74 3b 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 75  t;.      /* A su
28ccd 62 2d 71 75 65 72 79 20 69 6e 20 74 68 65 20 46  b-query in the F
28cce 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20  ROM clause of a 
28ccf 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20  SELECT */.      
28cd0 61 73 73 65 72 74 28 20 70 53 65 6c 21 3d 30 20  assert( pSel!=0 
28cd1 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
28cd2 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20   pFrom->pTab==0 
28cd3 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
28cd4 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b  WalkSelect(pWalk
28cd5 65 72 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 20  er, pSel);.     
28cd6 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20 70   pFrom->pTab = p
28cd7 54 61 62 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Tab = sqlite3DbM
28cd8 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
28cd9 7a 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20  zeof(Table));.  
28cda 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20      if( pTab==0 
28cdb 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
28cdc 72 74 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  rt;.      pTab->
28cdd 64 62 4d 65 6d 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  dbMem = db->look
28cde 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3f  aside.bEnabled ?
28cdf 20 64 62 20 3a 20 30 3b 0a 20 20 20 20 20 20 70   db : 0;.      p
28ce0 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  Tab->nRef = 1;. 
28ce1 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65       pTab->zName
28ce2 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
28ce3 66 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 75  f(db, "sqlite_su
28ce4 62 71 75 65 72 79 5f 25 70 5f 22 2c 20 28 76 6f  bquery_%p_", (vo
28ce5 69 64 2a 29 70 54 61 62 29 3b 0a 20 20 20 20 20  id*)pTab);.     
28ce6 20 77 68 69 6c 65 28 20 70 53 65 6c 2d 3e 70 50   while( pSel->pP
28ce7 72 69 6f 72 20 29 7b 20 70 53 65 6c 20 3d 20 70  rior ){ pSel = p
28ce8 53 65 6c 2d 3e 70 50 72 69 6f 72 3b 20 7d 0a 20  Sel->pPrior; }. 
28ce9 20 20 20 20 20 73 65 6c 65 63 74 43 6f 6c 75 6d       selectColum
28cea 6e 73 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70  nsFromExprList(p
28ceb 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70 45 4c  Parse, pSel->pEL
28cec 69 73 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c  ist, &pTab->nCol
28ced 2c 20 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a  , &pTab->aCol);.
28cee 20 20 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65        pTab->iPKe
28cef 79 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 54  y = -1;.      pT
28cf0 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20  ab->tabFlags |= 
28cf1 54 46 5f 45 70 68 65 6d 65 72 61 6c 3b 0a 23 65  TF_Ephemeral;.#e
28cf2 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ndif.    }else{.
28cf3 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 72 64 69        /* An ordi
28cf4 6e 61 72 79 20 74 61 62 6c 65 20 6f 72 20 76 69  nary table or vi
28cf5 65 77 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 46  ew name in the F
28cf6 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
28cf7 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f      assert( pFro
28cf8 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20 20  m->pTab==0 );.  
28cf9 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20      pFrom->pTab 
28cfa 3d 20 70 54 61 62 20 3d 20 0a 20 20 20 20 20 20  = pTab = .      
28cfb 20 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54    sqlite3LocateT
28cfc 61 62 6c 65 28 70 50 61 72 73 65 2c 30 2c 70 46  able(pParse,0,pF
28cfd 72 6f 6d 2d 3e 7a 4e 61 6d 65 2c 70 46 72 6f 6d  rom->zName,pFrom
28cfe 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
28cff 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20      if( pTab==0 
28d00 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
28d01 72 74 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  rt;.      pTab->
28d02 6e 52 65 66 2b 2b 3b 0a 23 69 66 20 21 64 65 66  nRef++;.#if !def
28d03 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
28d04 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
28d05 65 64 20 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ed (SQLITE_OMIT_
28d06 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20  VIRTUALTABLE).  
28d07 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53      if( pTab->pS
28d08 65 6c 65 63 74 20 7c 7c 20 49 73 56 69 72 74 75  elect || IsVirtu
28d09 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
28d0a 20 20 20 20 2f 2a 20 57 65 20 72 65 61 63 68 20      /* We reach 
28d0b 68 65 72 65 20 69 66 20 74 68 65 20 6e 61 6d 65  here if the name
28d0c 64 20 74 61 62 6c 65 20 69 73 20 61 20 72 65 61  d table is a rea
28d0d 6c 6c 79 20 61 20 76 69 65 77 20 2a 2f 0a 20 20  lly a view */.  
28d0e 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
28d0f 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
28d10 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  mes(pParse, pTab
28d11 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
28d12 62 6f 72 74 3b 0a 0a 20 20 20 20 20 20 20 20 2f  bort;..        /
28d13 2a 20 49 66 20 70 46 72 6f 6d 2d 3e 70 53 65 6c  * If pFrom->pSel
28d14 65 63 74 21 3d 30 20 69 74 20 6d 65 61 6e 73 20  ect!=0 it means 
28d15 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  we are dealing w
28d16 69 74 68 20 61 0a 20 20 20 20 20 20 20 20 2a 2a  ith a.        **
28d17 20 76 69 65 77 20 77 69 74 68 69 6e 20 61 20 76   view within a v
28d18 69 65 77 2e 20 20 54 68 65 20 53 45 4c 45 43 54  iew.  The SELECT
28d19 20 73 74 72 75 63 74 75 72 65 20 68 61 73 20 61   structure has a
28d1a 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20 20 20  lready been.    
28d1b 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 62 79      ** copied by
28d1c 20 74 68 65 20 6f 75 74 65 72 20 76 69 65 77 20   the outer view 
28d1d 73 6f 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74  so we can skip t
28d1e 68 65 20 63 6f 70 79 20 73 74 65 70 20 68 65 72  he copy step her
28d1f 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20  e.        ** in 
28d20 74 68 65 20 69 6e 6e 65 72 20 76 69 65 77 2e 0a  the inner view..
28d21 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
28d22 20 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70 53     if( pFrom->pS
28d23 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20  elect==0 ){.    
28d24 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 53 65        pFrom->pSe
28d25 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
28d26 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54 61 62  lectDup(db, pTab
28d27 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20  ->pSelect, 0);. 
28d28 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28d29 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b  WalkSelect(pWalk
28d2a 65 72 2c 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65  er, pFrom->pSele
28d2b 63 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ct);.        }. 
28d2c 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
28d2d 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 61    }..    /* Loca
28d2e 74 65 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d  te the index nam
28d2f 65 64 20 62 79 20 74 68 65 20 49 4e 44 45 58 45  ed by the INDEXE
28d30 44 20 42 59 20 63 6c 61 75 73 65 2c 20 69 66 20  D BY clause, if 
28d31 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  any. */.    if( 
28d32 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79  sqlite3IndexedBy
28d33 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70  Lookup(pParse, p
28d34 46 72 6f 6d 29 20 29 7b 0a 20 20 20 20 20 20 72  From) ){.      r
28d35 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
28d36 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
28d37 20 50 72 6f 63 65 73 73 20 4e 41 54 55 52 41 4c   Process NATURAL
28d38 20 6b 65 79 77 6f 72 64 73 2c 20 61 6e 64 20 4f   keywords, and O
28d39 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
28d3a 73 65 73 20 6f 66 20 6a 6f 69 6e 73 2e 0a 20 20  ses of joins..  
28d3b 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  */.  if( db->mal
28d3c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73 71 6c  locFailed || sql
28d3d 69 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28 70  iteProcessJoin(p
28d3e 50 61 72 73 65 2c 20 70 29 20 29 7b 0a 20 20 20  Parse, p) ){.   
28d3f 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
28d40 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72  t;.  }..  /* For
28d41 20 65 76 65 72 79 20 22 2a 22 20 74 68 61 74 20   every "*" that 
28d42 6f 63 63 75 72 73 20 69 6e 20 74 68 65 20 63 6f  occurs in the co
28d43 6c 75 6d 6e 20 6c 69 73 74 2c 20 69 6e 73 65 72  lumn list, inser
28d44 74 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a 20  t the names of. 
28d45 20 2a 2a 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20   ** all columns 
28d46 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e 20 20  in all tables.  
28d47 41 6e 64 20 66 6f 72 20 65 76 65 72 79 20 54 41  And for every TA
28d48 42 4c 45 2e 2a 20 69 6e 73 65 72 74 20 74 68 65  BLE.* insert the
28d49 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61   names.  ** of a
28d4a 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 54 41  ll columns in TA
28d4b 42 4c 45 2e 20 20 54 68 65 20 70 61 72 73 65 72  BLE.  The parser
28d4c 20 69 6e 73 65 72 74 65 64 20 61 20 73 70 65 63   inserted a spec
28d4d 69 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  ial expression. 
28d4e 20 2a 2a 20 77 69 74 68 20 74 68 65 20 54 4b 5f   ** with the TK_
28d4f 41 4c 4c 20 6f 70 65 72 61 74 6f 72 20 66 6f 72  ALL operator for
28d50 20 65 61 63 68 20 22 2a 22 20 74 68 61 74 20 69   each "*" that i
28d51 74 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 63  t found in the c
28d52 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0a 20 20 2a 2a  olumn list..  **
28d53 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
28d54 6f 64 65 20 6a 75 73 74 20 68 61 73 20 74 6f 20  ode just has to 
28d55 6c 6f 63 61 74 65 20 74 68 65 20 54 4b 5f 41 4c  locate the TK_AL
28d56 4c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e  L expressions an
28d57 64 20 65 78 70 61 6e 64 0a 20 20 2a 2a 20 65 61  d expand.  ** ea
28d58 63 68 20 6f 6e 65 20 74 6f 20 74 68 65 20 6c 69  ch one to the li
28d59 73 74 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e  st of all column
28d5a 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e  s in all tables.
28d5b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  **.  ** The f
28d5c 69 72 73 74 20 6c 6f 6f 70 20 6a 75 73 74 20 63  irst loop just c
28d5d 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20  hecks to see if 
28d5e 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 22 2a  there are any "*
28d5f 22 20 6f 70 65 72 61 74 6f 72 73 0a 20 20 2a 2a  " operators.  **
28d60 20 74 68 61 74 20 6e 65 65 64 20 65 78 70 61 6e   that need expan
28d61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ding..  */.  for
28d62 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74 2d 3e  (k=0; k<pEList->
28d63 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20 20  nExpr; k++){.   
28d64 20 45 78 70 72 20 2a 70 45 20 3d 20 70 45 4c 69   Expr *pE = pELi
28d65 73 74 2d 3e 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a  st->a[k].pExpr;.
28d66 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d      if( pE->op==
28d67 54 4b 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a  TK_ALL ) break;.
28d68 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d      if( pE->op==
28d69 54 4b 5f 44 4f 54 20 26 26 20 70 45 2d 3e 70 52  TK_DOT && pE->pR
28d6a 69 67 68 74 20 26 26 20 70 45 2d 3e 70 52 69 67  ight && pE->pRig
28d6b 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 0a 20  ht->op==TK_ALL. 
28d6c 20 20 20 20 20 20 20 20 26 26 20 70 45 2d 3e 70          && pE->p
28d6d 4c 65 66 74 20 26 26 20 70 45 2d 3e 70 4c 65 66  Left && pE->pLef
28d6e 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 20 62  t->op==TK_ID ) b
28d6f 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  reak;.  }.  if( 
28d70 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20  k<pEList->nExpr 
28d71 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  ){.    /*.    **
28d72 20 49 66 20 77 65 20 67 65 74 20 68 65 72 65 20   If we get here 
28d73 69 74 20 6d 65 61 6e 73 20 74 68 65 20 72 65 73  it means the res
28d74 75 6c 74 20 73 65 74 20 63 6f 6e 74 61 69 6e 73  ult set contains
28d75 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 22 2a 22   one or more "*"
28d76 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72  .    ** operator
28d77 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62  s that need to b
28d78 65 20 65 78 70 61 6e 64 65 64 2e 20 20 4c 6f 6f  e expanded.  Loo
28d79 70 20 74 68 72 6f 75 67 68 20 65 61 63 68 20 65  p through each e
28d7a 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a 2a  xpression.    **
28d7b 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
28d7c 65 74 20 61 6e 64 20 65 78 70 61 6e 64 20 74 68  et and expand th
28d7d 65 6d 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20  em one by one.. 
28d7e 20 20 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74     */.    struct
28d7f 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
28d80 61 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20  a = pEList->a;. 
28d81 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65     ExprList *pNe
28d82 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 66  w = 0;.    int f
28d83 6c 61 67 73 20 3d 20 70 50 61 72 73 65 2d 3e 64  lags = pParse->d
28d84 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 20 20 69 6e  b->flags;.    in
28d85 74 20 6c 6f 6e 67 4e 61 6d 65 73 20 3d 20 28 66  t longNames = (f
28d86 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75  lags & SQLITE_Fu
28d87 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 0a 20  llColNames)!=0. 
28d88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d89 20 20 20 20 20 26 26 20 28 66 6c 61 67 73 20 26       && (flags &
28d8a 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c   SQLITE_ShortCol
28d8b 4e 61 6d 65 73 29 3d 3d 30 3b 0a 0a 20 20 20 20  Names)==0;..    
28d8c 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73  for(k=0; k<pELis
28d8d 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a  t->nExpr; k++){.
28d8e 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d        Expr *pE =
28d8f 20 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20   a[k].pExpr;.   
28d90 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 21 3d 54     if( pE->op!=T
28d91 4b 5f 41 4c 4c 20 26 26 0a 20 20 20 20 20 20 20  K_ALL &&.       
28d92 20 20 20 20 28 70 45 2d 3e 6f 70 21 3d 54 4b 5f      (pE->op!=TK_
28d93 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68  DOT || pE->pRigh
28d94 74 3d 3d 30 20 7c 7c 20 70 45 2d 3e 70 52 69 67  t==0 || pE->pRig
28d95 68 74 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20  ht->op!=TK_ALL) 
28d96 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
28d97 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 65 78  is particular ex
28d98 70 72 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f  pression does no
28d99 74 20 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70  t need to be exp
28d9a 61 6e 64 65 64 2e 0a 20 20 20 20 20 20 20 20 2a  anded..        *
28d9b 2f 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  /.        pNew =
28d9c 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
28d9d 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
28d9e 4e 65 77 2c 20 61 5b 6b 5d 2e 70 45 78 70 72 2c  New, a[k].pExpr,
28d9f 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
28da0 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20   pNew ){.       
28da1 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d     pNew->a[pNew-
28da2 3e 6e 45 78 70 72 2d 31 5d 2e 7a 4e 61 6d 65 20  >nExpr-1].zName 
28da3 3d 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  = a[k].zName;.  
28da4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
28da5 61 5b 6b 5d 2e 70 45 78 70 72 20 3d 20 30 3b 0a  a[k].pExpr = 0;.
28da6 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 4e 61          a[k].zNa
28da7 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  me = 0;.      }e
28da8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
28da9 54 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20  This expression 
28daa 69 73 20 61 20 22 2a 22 20 6f 72 20 61 20 22 54  is a "*" or a "T
28dab 41 42 4c 45 2e 2a 22 20 61 6e 64 20 6e 65 65 64  ABLE.*" and need
28dac 73 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20  s to be.        
28dad 2a 2a 20 65 78 70 61 6e 64 65 64 2e 20 2a 2f 0a  ** expanded. */.
28dae 20 20 20 20 20 20 20 20 69 6e 74 20 74 61 62 6c          int tabl
28daf 65 53 65 65 6e 20 3d 20 30 3b 20 20 20 20 20 20  eSeen = 0;      
28db0 2f 2a 20 53 65 74 20 74 6f 20 31 20 77 68 65 6e  /* Set to 1 when
28db1 20 54 41 42 4c 45 20 6d 61 74 63 68 65 73 20 2a   TABLE matches *
28db2 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  /.        char *
28db3 7a 54 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  zTName;         
28db4 20 20 20 2f 2a 20 74 65 78 74 20 6f 66 20 6e 61     /* text of na
28db5 6d 65 20 6f 66 20 54 41 42 4c 45 20 2a 2f 0a 20  me of TABLE */. 
28db6 20 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f         if( pE->o
28db7 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20 70 45 2d  p==TK_DOT && pE-
28db8 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  >pLeft ){.      
28db9 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 73 71 6c      zTName = sql
28dba 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
28dbb 6e 28 64 62 2c 20 26 70 45 2d 3e 70 4c 65 66 74  n(db, &pE->pLeft
28dbc 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20  ->token);.      
28dbd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
28dbe 20 20 20 7a 54 4e 61 6d 65 20 3d 20 30 3b 0a 20     zTName = 0;. 
28dbf 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
28dc0 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d   for(i=0, pFrom=
28dc1 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  pTabList->a; i<p
28dc2 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
28dc3 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20  ++, pFrom++){.  
28dc4 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70          Table *p
28dc5 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61  Tab = pFrom->pTa
28dc6 62 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  b;.          cha
28dc7 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 46  r *zTabName = pF
28dc8 72 6f 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20  rom->zAlias;.   
28dc9 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62 4e         if( zTabN
28dca 61 6d 65 3d 3d 30 20 7c 7c 20 7a 54 61 62 4e 61  ame==0 || zTabNa
28dcb 6d 65 5b 30 5d 3d 3d 30 20 29 7b 20 0a 20 20 20  me[0]==0 ){ .   
28dcc 20 20 20 20 20 20 20 20 20 7a 54 61 62 4e 61 6d           zTabNam
28dcd 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  e = pTab->zName;
28dce 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
28dcf 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d         if( db->m
28dd0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 62 72  allocFailed ) br
28dd1 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 69  eak;.          i
28dd2 66 28 20 7a 54 4e 61 6d 65 20 26 26 20 73 71 6c  f( zTName && sql
28dd3 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 4e 61  ite3StrICmp(zTNa
28dd4 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 29 21 3d 30  me, zTabName)!=0
28dd5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
28dd6 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
28dd7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
28dd8 74 61 62 6c 65 53 65 65 6e 20 3d 20 31 3b 0a 20  tableSeen = 1;. 
28dd9 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
28dda 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; j<pTab->nCol; 
28ddb 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
28ddc 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 2a    Expr *pExpr, *
28ddd 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
28dde 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
28ddf 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  = pTab->aCol[j].
28de0 7a 4e 61 6d 65 3b 0a 0a 20 20 20 20 20 20 20 20  zName;..        
28de1 20 20 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75      /* If a colu
28de2 6d 6e 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  mn is marked as 
28de3 27 68 69 64 64 65 6e 27 20 28 63 75 72 72 65 6e  'hidden' (curren
28de4 74 6c 79 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c  tly only possibl
28de5 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  e.            **
28de6 20 66 6f 72 20 76 69 72 74 75 61 6c 20 74 61 62   for virtual tab
28de7 6c 65 73 29 2c 20 64 6f 20 6e 6f 74 20 69 6e 63  les), do not inc
28de8 6c 75 64 65 20 69 74 20 69 6e 20 74 68 65 20 65  lude it in the e
28de9 78 70 61 6e 64 65 64 0a 20 20 20 20 20 20 20 20  xpanded.        
28dea 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73 65      ** result-se
28deb 74 20 6c 69 73 74 2e 0a 20 20 20 20 20 20 20 20  t list..        
28dec 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
28ded 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e 43     if( IsHiddenC
28dee 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f  olumn(&pTab->aCo
28def 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20 20 20 20 20  l[j]) ){.       
28df0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 49 73         assert(Is
28df1 56 69 72 74 75 61 6c 28 70 54 61 62 29 29 3b 0a  Virtual(pTab));.
28df2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
28df3 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
28df4 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
28df5 20 20 20 69 66 28 20 69 3e 30 20 26 26 20 7a 54     if( i>0 && zT
28df6 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Name==0 ){.     
28df7 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20           struct 
28df8 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c  SrcList_item *pL
28df9 65 66 74 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  eft = &pTabList-
28dfa 3e 61 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 20  >a[i-1];.       
28dfb 20 20 20 20 20 20 20 69 66 28 20 28 70 4c 65 66         if( (pLef
28dfc 74 5b 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20  t[1].jointype & 
28dfd 4a 54 5f 4e 41 54 55 52 41 4c 29 21 3d 30 20 26  JT_NATURAL)!=0 &
28dfe 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  &.              
28dff 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e            column
28e00 49 6e 64 65 78 28 70 4c 65 66 74 2d 3e 70 54 61  Index(pLeft->pTa
28e01 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a  b, zName)>=0 ){.
28e02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28e03 2f 2a 20 49 6e 20 61 20 4e 41 54 55 52 41 4c 20  /* In a NATURAL 
28e04 6a 6f 69 6e 2c 20 6f 6d 69 74 20 74 68 65 20 6a  join, omit the j
28e05 6f 69 6e 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d  oin columns from
28e06 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20   the .          
28e07 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 6f        ** table o
28e08 6e 20 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 20  n the right */. 
28e09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
28e0a 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
28e0b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
28e0c 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
28e0d 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 70 4c  e3IdListIndex(pL
28e0e 65 66 74 5b 31 5d 2e 70 55 73 69 6e 67 2c 20 7a  eft[1].pUsing, z
28e0f 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20 20 20 20  Name)>=0 ){.    
28e10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
28e11 6e 20 61 20 6a 6f 69 6e 20 77 69 74 68 20 61 20  n a join with a 
28e12 55 53 49 4e 47 20 63 6c 61 75 73 65 2c 20 6f 6d  USING clause, om
28e13 69 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  it columns in th
28e14 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e.              
28e15 20 20 2a 2a 20 75 73 69 6e 67 20 63 6c 61 75 73    ** using claus
28e16 65 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65  e from the table
28e17 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2e 20 2a   on the right. *
28e18 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
28e19 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
28e1a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
28e1b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
28e1c 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 73        pRight = s
28e1d 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
28e1e 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
28e1f 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
28e20 20 69 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29   if( pRight==0 )
28e21 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
28e22 20 20 20 20 73 65 74 54 6f 6b 65 6e 28 26 70 52      setToken(&pR
28e23 69 67 68 74 2d 3e 74 6f 6b 65 6e 2c 20 7a 4e 61  ight->token, zNa
28e24 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  me);.           
28e25 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 7c   if( longNames |
28e26 7c 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  | pTabList->nSrc
28e27 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >1 ){.          
28e28 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20      Expr *pLeft 
28e29 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
28e2a 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
28e2b 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
28e2c 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 73 71        pExpr = sq
28e2d 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
28e2e 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74  e, TK_DOT, pLeft
28e2f 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20  , pRight, 0);.  
28e30 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
28e31 70 45 78 70 72 3d 3d 30 20 29 20 62 72 65 61 6b  pExpr==0 ) break
28e32 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
28e33 73 65 74 54 6f 6b 65 6e 28 26 70 4c 65 66 74 2d  setToken(&pLeft-
28e34 3e 74 6f 6b 65 6e 2c 20 7a 54 61 62 4e 61 6d 65  >token, zTabName
28e35 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
28e36 20 73 65 74 54 6f 6b 65 6e 28 26 70 45 78 70 72   setToken(&pExpr
28e37 2d 3e 73 70 61 6e 2c 20 0a 20 20 20 20 20 20 20  ->span, .       
28e38 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28e39 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
28e3a 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d 65 2c  s.%s", zTabName,
28e3b 20 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20   zName));.      
28e3c 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73          pExpr->s
28e3d 70 61 6e 2e 64 79 6e 20 3d 20 31 3b 0a 20 20 20  pan.dyn = 1;.   
28e3e 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
28e3f 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 30 3b 0a 20  ->token.z = 0;. 
28e40 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
28e41 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20 30 3b  pr->token.n = 0;
28e42 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
28e43 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20  Expr->token.dyn 
28e44 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
28e45 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
28e46 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 70 52        pExpr = pR
28e47 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20  ight;.          
28e48 20 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e 20      pExpr->span 
28e49 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 3b 0a  = pExpr->token;.
28e4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
28e4b 78 70 72 2d 3e 73 70 61 6e 2e 64 79 6e 20 3d 20  xpr->span.dyn = 
28e4c 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  0;.            }
28e4d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
28e4e 20 6c 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a 20 20   longNames ){.  
28e4f 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77              pNew
28e50 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
28e51 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
28e52 20 70 4e 65 77 2c 20 70 45 78 70 72 2c 20 26 70   pNew, pExpr, &p
28e53 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  Expr->span);.   
28e54 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
28e55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
28e56 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
28e57 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
28e58 65 2c 20 70 4e 65 77 2c 20 70 45 78 70 72 2c 20  e, pNew, pExpr, 
28e59 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29 3b  &pRight->token);
28e5a 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
28e5b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
28e5c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
28e5d 20 21 74 61 62 6c 65 53 65 65 6e 20 29 7b 0a 20   !tableSeen ){. 
28e5e 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 4e           if( zTN
28e5f 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ame ){.         
28e60 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
28e61 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
28e62 75 63 68 20 74 61 62 6c 65 3a 20 25 73 22 2c 20  uch table: %s", 
28e63 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  zTName);.       
28e64 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
28e65 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
28e66 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
28e67 6f 20 74 61 62 6c 65 73 20 73 70 65 63 69 66 69  o tables specifi
28e68 65 64 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ed");.          
28e69 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
28e6a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
28e6b 65 28 64 62 2c 20 7a 54 4e 61 6d 65 29 3b 0a 20  e(db, zTName);. 
28e6c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
28e6d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
28e6e 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c 69 73  Delete(db, pELis
28e6f 74 29 3b 0a 20 20 20 20 70 2d 3e 70 45 4c 69 73  t);.    p->pELis
28e70 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 23 69  t = pNew;.  }.#i
28e71 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c  f SQLITE_MAX_COL
28e72 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c  UMN.  if( p->pEL
28e73 69 73 74 20 26 26 20 70 2d 3e 70 45 4c 69 73 74  ist && p->pEList
28e74 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d  ->nExpr>db->aLim
28e75 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
28e76 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73  COLUMN] ){.    s
28e77 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
28e78 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
28e79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73 75   columns in resu
28e7a 6c 74 20 73 65 74 22 29 3b 0a 20 20 7d 0a 23 65  lt set");.  }.#e
28e7b 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 57 52  ndif.  return WR
28e7c 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f  C_Continue;.}../
28e7d 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 72 6f 75 74 69  *.** No-op routi
28e7e 6e 65 20 66 6f 72 20 74 68 65 20 70 61 72 73 65  ne for the parse
28e7f 2d 74 72 65 65 20 77 61 6c 6b 65 72 2e 0a 2a 2a  -tree walker..**
28e80 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f  .** When this ro
28e81 75 74 69 6e 65 20 69 73 20 74 68 65 20 57 61 6c  utine is the Wal
28e82 6b 65 72 2e 78 45 78 70 72 43 61 6c 6c 62 61 63  ker.xExprCallbac
28e83 6b 20 74 68 65 6e 20 65 78 70 72 65 73 73 69 6f  k then expressio
28e84 6e 20 74 72 65 65 73 0a 2a 2a 20 61 72 65 20 77  n trees.** are w
28e85 61 6c 6b 65 64 20 77 69 74 68 6f 75 74 20 61 6e  alked without an
28e86 79 20 61 63 74 69 6f 6e 73 20 62 65 69 6e 67 20  y actions being 
28e87 74 61 6b 65 6e 20 61 74 20 65 61 63 68 20 6e 6f  taken at each no
28e88 64 65 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 2c  de.  Presumably,
28e89 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f  .** when this ro
28e8a 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
28e8b 72 20 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61  r Walker.xExprCa
28e8c 6c 6c 62 61 63 6b 20 74 68 65 6e 20 0a 2a 2a 20  llback then .** 
28e8d 57 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74 43 61  Walker.xSelectCa
28e8e 6c 6c 62 61 63 6b 20 69 73 20 73 65 74 20 74 6f  llback is set to
28e8f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 75 73   do something us
28e90 65 66 75 6c 20 66 6f 72 20 65 76 65 72 79 20 0a  eful for every .
28e91 2a 2a 20 73 75 62 71 75 65 72 79 20 69 6e 20 74  ** subquery in t
28e92 68 65 20 70 61 72 73 65 72 20 74 72 65 65 2e 0a  he parser tree..
28e93 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78  */.static int ex
28e94 70 72 57 61 6c 6b 4e 6f 6f 70 28 57 61 6c 6b 65  prWalkNoop(Walke
28e95 72 20 2a 4e 6f 74 55 73 65 64 2c 20 45 78 70 72  r *NotUsed, Expr
28e96 20 2a 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55   *NotUsed2){.  U
28e97 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
28e98 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
28e99 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 57 52  d2);.  return WR
28e9a 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f  C_Continue;.}../
28e9b 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
28e9c 65 20 22 65 78 70 61 6e 64 73 22 20 61 20 53 45  e "expands" a SE
28e9d 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61  LECT statement a
28e9e 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 73 75  nd all of its su
28e9f 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 46 6f 72  bqueries..** For
28ea0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
28ea1 72 6d 61 74 69 6f 6e 20 6f 6e 20 77 68 61 74 20  rmation on what 
28ea2 69 74 20 6d 65 61 6e 73 20 74 6f 20 22 65 78 70  it means to "exp
28ea3 61 6e 64 22 20 61 20 53 45 4c 45 43 54 0a 2a 2a  and" a SELECT.**
28ea4 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65 65 20   statement, see 
28ea5 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74  the comment on t
28ea6 68 65 20 73 65 6c 65 63 74 45 78 70 61 6e 64 20  he selectExpand 
28ea7 77 6f 72 6b 65 72 20 63 61 6c 6c 62 61 63 6b 20  worker callback 
28ea8 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70  above..**.** Exp
28ea9 61 6e 64 69 6e 67 20 61 20 53 45 4c 45 43 54 20  anding a SELECT 
28eaa 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 65  statement is the
28eab 20 66 69 72 73 74 20 73 74 65 70 20 69 6e 20 70   first step in p
28eac 72 6f 63 65 73 73 69 6e 67 20 61 0a 2a 2a 20 53  rocessing a.** S
28ead 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
28eae 20 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61    The SELECT sta
28eaf 74 65 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 65  tement must be e
28eb0 78 70 61 6e 64 65 64 20 62 65 66 6f 72 65 0a 2a  xpanded before.*
28eb1 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f  * name resolutio
28eb2 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a  n is performed..
28eb3 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e  **.** If anythin
28eb4 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 61 6e  g goes wrong, an
28eb5 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
28eb6 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 70  s written into p
28eb7 50 61 72 73 65 2e 0a 2a 2a 20 54 68 65 20 63 61  Parse..** The ca
28eb8 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63  lling function c
28eb9 61 6e 20 64 65 74 65 63 74 20 74 68 65 20 70 72  an detect the pr
28eba 6f 62 6c 65 6d 20 62 79 20 6c 6f 6f 6b 69 6e 67  oblem by looking
28ebb 20 61 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   at pParse->nErr
28ebc 0a 2a 2a 20 61 6e 64 2f 6f 72 20 70 50 61 72 73  .** and/or pPars
28ebd 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
28ebe 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  led..*/.static v
28ebf 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
28ec0 74 45 78 70 61 6e 64 28 50 61 72 73 65 20 2a 70  tExpand(Parse *p
28ec1 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70  Parse, Select *p
28ec2 53 65 6c 65 63 74 29 7b 0a 20 20 57 61 6c 6b 65  Select){.  Walke
28ec3 72 20 77 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  r w;.  w.xSelect
28ec4 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63  Callback = selec
28ec5 74 45 78 70 61 6e 64 65 72 3b 0a 20 20 77 2e 78  tExpander;.  w.x
28ec6 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65  ExprCallback = e
28ec7 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20 20 77  xprWalkNoop;.  w
28ec8 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
28ec9 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  ;.  sqlite3WalkS
28eca 65 6c 65 63 74 28 26 77 2c 20 70 53 65 6c 65 63  elect(&w, pSelec
28ecb 74 29 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20  t);.}...#ifndef 
28ecc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
28ecd 55 45 52 59 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  UERY./*.** This 
28ece 69 73 20 61 20 57 61 6c 6b 65 72 2e 78 53 65 6c  is a Walker.xSel
28ecf 65 63 74 43 61 6c 6c 62 61 63 6b 20 63 61 6c 6c  ectCallback call
28ed0 62 61 63 6b 20 66 6f 72 20 74 68 65 20 73 71 6c  back for the sql
28ed1 69 74 65 33 53 65 6c 65 63 74 54 79 70 65 49 6e  ite3SelectTypeIn
28ed2 66 6f 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63  fo().** interfac
28ed3 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 61 63  e..**.** For eac
28ed4 68 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75  h FROM-clause su
28ed5 62 71 75 65 72 79 2c 20 61 64 64 20 43 6f 6c 75  bquery, add Colu
28ed6 6d 6e 2e 7a 54 79 70 65 20 61 6e 64 20 43 6f 6c  mn.zType and Col
28ed7 75 6d 6e 2e 7a 43 6f 6c 6c 0a 2a 2a 20 69 6e 66  umn.zColl.** inf
28ed8 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 65 20  ormation to the 
28ed9 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
28eda 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20  that represents 
28edb 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 0a 2a  the result set.*
28edc 2a 20 6f 66 20 74 68 61 74 20 73 75 62 71 75 65  * of that subque
28edd 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61  ry..**.** The Ta
28ede 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 68  ble structure th
28edf 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  at represents th
28ee0 65 20 72 65 73 75 6c 74 20 73 65 74 20 77 61 73  e result set was
28ee1 20 63 6f 6e 73 74 72 75 63 74 65 64 0a 2a 2a 20   constructed.** 
28ee2 62 79 20 73 65 6c 65 63 74 45 78 70 61 6e 64 65  by selectExpande
28ee3 72 28 29 20 62 75 74 20 74 68 65 20 74 79 70 65  r() but the type
28ee4 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 69   and collation i
28ee5 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 6f  nformation was o
28ee6 6d 69 74 74 65 64 0a 2a 2a 20 61 74 20 74 68 61  mitted.** at tha
28ee7 74 20 70 6f 69 6e 74 20 62 65 63 61 75 73 65 20  t point because 
28ee8 69 64 65 6e 74 69 66 69 65 72 73 20 68 61 64 20  identifiers had 
28ee9 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65 73  not yet been res
28eea 6f 6c 76 65 64 2e 20 20 54 68 69 73 0a 2a 2a 20  olved.  This.** 
28eeb 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
28eec 64 20 61 66 74 65 72 20 69 64 65 6e 74 69 66 69  d after identifi
28eed 65 72 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a  er resolution..*
28eee 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c  /.static int sel
28eef 65 63 74 41 64 64 53 75 62 71 75 65 72 79 54 79  ectAddSubqueryTy
28ef0 70 65 49 6e 66 6f 28 57 61 6c 6b 65 72 20 2a 70  peInfo(Walker *p
28ef1 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
28ef2 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  p){.  Parse *pPa
28ef3 72 73 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  rse;.  int i;.  
28ef4 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
28ef5 74 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  t;.  struct SrcL
28ef6 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 3b  ist_item *pFrom;
28ef7 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73  ..  assert( p->s
28ef8 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73  elFlags & SF_Res
28ef9 6f 6c 76 65 64 20 29 3b 0a 20 20 69 66 28 20 28  olved );.  if( (
28efa 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
28efb 5f 48 61 73 54 79 70 65 49 6e 66 6f 29 3d 3d 30  _HasTypeInfo)==0
28efc 20 29 7b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c   ){.    p->selFl
28efd 61 67 73 20 7c 3d 20 53 46 5f 48 61 73 54 79 70  ags |= SF_HasTyp
28efe 65 49 6e 66 6f 3b 0a 20 20 20 20 70 50 61 72 73  eInfo;.    pPars
28eff 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61  e = pWalker->pPa
28f00 72 73 65 3b 0a 20 20 20 20 70 54 61 62 4c 69 73  rse;.    pTabLis
28f01 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20  t = p->pSrc;.   
28f02 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d   for(i=0, pFrom=
28f03 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  pTabList->a; i<p
28f04 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
28f05 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20  ++, pFrom++){.  
28f06 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
28f07 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20  = pFrom->pTab;. 
28f08 20 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26       if( pTab &&
28f09 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
28f0a 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29   & TF_Ephemeral)
28f0b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  !=0 ){.        /
28f0c 2a 20 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e  * A sub-query in
28f0d 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
28f0e 20 6f 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a   of a SELECT */.
28f0f 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a          Select *
28f10 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53  pSel = pFrom->pS
28f11 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 20 20 61  elect;.        a
28f12 73 73 65 72 74 28 20 70 53 65 6c 20 29 3b 0a 20  ssert( pSel );. 
28f13 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53         while( pS
28f14 65 6c 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65  el->pPrior ) pSe
28f15 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72  l = pSel->pPrior
28f16 3b 0a 20 20 20 20 20 20 20 20 73 65 6c 65 63 74  ;.        select
28f17 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64  AddColumnTypeAnd
28f18 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 72 73 65  Collation(pParse
28f19 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54  , pTab->nCol, pT
28f1a 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c 29 3b  ab->aCol, pSel);
28f1b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
28f1c 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f   }.  return WRC_
28f1d 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 23 65 6e 64  Continue;.}.#end
28f1e 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  if.../*.** This 
28f1f 72 6f 75 74 69 6e 65 20 61 64 64 73 20 64 61 74  routine adds dat
28f20 61 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74  atype and collat
28f21 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 6e 66  ing sequence inf
28f22 6f 72 6d 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 74  ormation to.** t
28f23 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
28f24 72 65 73 20 6f 66 20 61 6c 6c 20 46 52 4f 4d 2d  res of all FROM-
28f25 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 69 65  clause subquerie
28f26 73 20 69 6e 20 61 0a 2a 2a 20 53 45 4c 45 43 54  s in a.** SELECT
28f27 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
28f28 2a 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69  * Use this routi
28f29 6e 65 20 61 66 74 65 72 20 6e 61 6d 65 20 72 65  ne after name re
28f2a 73 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  solution..*/.sta
28f2b 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
28f2c 53 65 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66  SelectAddTypeInf
28f2d 6f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  o(Parse *pParse,
28f2e 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
28f2f 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
28f30 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
28f31 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e    Walker w;.  w.
28f32 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
28f33 3d 20 73 65 6c 65 63 74 41 64 64 53 75 62 71 75  = selectAddSubqu
28f34 65 72 79 54 79 70 65 49 6e 66 6f 3b 0a 20 20 77  eryTypeInfo;.  w
28f35 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
28f36 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20   exprWalkNoop;. 
28f37 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72   w.pParse = pPar
28f38 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c  se;.  sqlite3Wal
28f39 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 53 65 6c  kSelect(&w, pSel
28f3a 65 63 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ect);.#endif.}..
28f3b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
28f3c 69 6e 65 20 73 65 74 73 20 6f 66 20 61 20 53 45  ine sets of a SE
28f3d 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 66  LECT statement f
28f3e 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20  or processing.  
28f3f 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  The.** following
28f40 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 65 64   is accomplished
28f41 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20 56  :.**.**     *  V
28f42 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65  DBE Cursor numbe
28f43 72 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 20  rs are assigned 
28f44 74 6f 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75  to all FROM-clau
28f45 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 20 20 20  se terms..**    
28f46 20 2a 20 20 45 70 68 65 6d 65 72 61 6c 20 54 61   *  Ephemeral Ta
28f47 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65 20  ble objects are 
28f48 63 72 65 61 74 65 64 20 66 6f 72 20 61 6c 6c 20  created for all 
28f49 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
28f4a 75 65 72 69 65 73 2e 0a 2a 2a 20 20 20 20 20 2a  ueries..**     *
28f4b 20 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63    ON and USING c
28f4c 6c 61 75 73 65 73 20 61 72 65 20 73 68 69 66 74  lauses are shift
28f4d 65 64 20 69 6e 74 6f 20 57 48 45 52 45 20 73 74  ed into WHERE st
28f4e 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20  atements.**     
28f4f 2a 20 20 57 69 6c 64 63 61 72 64 73 20 22 2a 22  *  Wildcards "*"
28f50 20 61 6e 64 20 22 54 41 42 4c 45 2e 2a 22 20 69   and "TABLE.*" i
28f51 6e 20 72 65 73 75 6c 74 20 73 65 74 73 20 61 72  n result sets ar
28f52 65 20 65 78 70 61 6e 64 65 64 2e 0a 2a 2a 20 20  e expanded..**  
28f53 20 20 20 2a 20 20 49 64 65 6e 74 69 66 69 65 72     *  Identifier
28f54 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s in expression 
28f55 61 72 65 20 6d 61 74 63 68 65 64 20 74 6f 20 74  are matched to t
28f56 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ables..**.** Thi
28f57 73 20 72 6f 75 74 69 6e 65 20 61 63 74 73 20 72  s routine acts r
28f58 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20 61 6c  ecursively on al
28f59 6c 20 73 75 62 71 75 65 72 69 65 73 20 77 69 74  l subqueries wit
28f5a 68 69 6e 20 74 68 65 20 53 45 4c 45 43 54 2e 0a  hin the SELECT..
28f5b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
28f5c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
28f5d 6c 65 63 74 50 72 65 70 28 0a 20 20 50 61 72 73  lectPrep(.  Pars
28f5e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
28f5f 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
28f60 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
28f61 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
28f62 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
28f63 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  CT statement bei
28f64 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e  ng coded. */.  N
28f65 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74  ameContext *pOut
28f66 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 6f  erNC  /* Name co
28f67 6e 74 65 78 74 20 66 6f 72 20 63 6f 6e 74 61 69  ntext for contai
28f68 6e 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ner */.){.  sqli
28f69 74 65 33 20 2a 64 62 3b 0a 20 20 69 66 28 20 70  te3 *db;.  if( p
28f6a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
28f6b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
28f6c 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61  .  if( p->selFla
28f6d 67 73 20 26 20 53 46 5f 48 61 73 54 79 70 65 49  gs & SF_HasTypeI
28f6e 6e 66 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  nfo ) return;.  
28f6f 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
28f70 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
28f71 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
28f72 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
28f73 70 61 6e 64 28 70 50 61 72 73 65 2c 20 70 29 3b  pand(pParse, p);
28f74 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
28f75 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
28f76 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
28f77 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  ;.  sqlite3Resol
28f78 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 50  veSelectNames(pP
28f79 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e  arse, p, pOuterN
28f7a 43 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  C);.  if( pParse
28f7b 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
28f7c 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
28f7d 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  urn;.  sqlite3Se
28f7e 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66 6f 28  lectAddTypeInfo(
28f7f 70 50 61 72 73 65 2c 20 70 29 3b 0a 7d 0a 0a 2f  pParse, p);.}../
28f80 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61  *.** Reset the a
28f81 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c  ggregate accumul
28f82 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ator..**.** The 
28f83 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75  aggregate accumu
28f84 6c 61 74 6f 72 20 69 73 20 61 20 73 65 74 20 6f  lator is a set o
28f85 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74  f memory cells t
28f86 68 61 74 20 68 6f 6c 64 0a 2a 2a 20 69 6e 74 65  hat hold.** inte
28f87 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c 74 73  rmediate results
28f88 20 77 68 69 6c 65 20 63 61 6c 63 75 6c 61 74 69   while calculati
28f89 6e 67 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e  ng an aggregate.
28f8a 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
28f8b 65 20 73 69 6d 70 6c 79 20 73 74 6f 72 65 73 20  e simply stores 
28f8c 4e 55 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f 66 20  NULLs in all of 
28f8d 74 68 6f 73 65 20 6d 65 6d 6f 72 79 20 63 65 6c  those memory cel
28f8e 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ls..*/.static vo
28f8f 69 64 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61  id resetAccumula
28f90 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  tor(Parse *pPars
28f91 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  e, AggInfo *pAgg
28f92 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76  Info){.  Vdbe *v
28f93 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
28f94 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  ;.  int i;.  str
28f95 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63  uct AggInfo_func
28f96 20 2a 70 46 75 6e 63 3b 0a 20 20 69 66 28 20 70   *pFunc;.  if( p
28f97 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2b 70  AggInfo->nFunc+p
28f98 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e  AggInfo->nColumn
28f99 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
28f9a 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  n;.  }.  for(i=0
28f9b 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 43  ; i<pAggInfo->nC
28f9c 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
28f9d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28f9e 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
28f9f 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  , pAggInfo->aCol
28fa0 5b 69 5d 2e 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20  [i].iMem);.  }. 
28fa1 20 66 6f 72 28 70 46 75 6e 63 3d 70 41 67 67 49   for(pFunc=pAggI
28fa2 6e 66 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d 30 3b  nfo->aFunc, i=0;
28fa3 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75   i<pAggInfo->nFu
28fa4 6e 63 3b 20 69 2b 2b 2c 20 70 46 75 6e 63 2b 2b  nc; i++, pFunc++
28fa5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
28fa6 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
28fa7 75 6c 6c 2c 20 30 2c 20 70 46 75 6e 63 2d 3e 69  ull, 0, pFunc->i
28fa8 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 46  Mem);.    if( pF
28fa9 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 3e 3d  unc->iDistinct>=
28faa 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  0 ){.      Expr 
28fab 2a 70 45 20 3d 20 70 46 75 6e 63 2d 3e 70 45 78  *pE = pFunc->pEx
28fac 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  pr;.      assert
28fad 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
28fae 74 79 28 70 45 2c 20 45 50 5f 78 49 73 53 65 6c  ty(pE, EP_xIsSel
28faf 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66  ect) );.      if
28fb0 28 20 70 45 2d 3e 78 2e 70 4c 69 73 74 3d 3d 30  ( pE->x.pList==0
28fb1 20 7c 7c 20 70 45 2d 3e 78 2e 70 4c 69 73 74 2d   || pE->x.pList-
28fb2 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20  >nExpr!=1 ){.   
28fb3 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
28fb4 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 44 49  rMsg(pParse, "DI
28fb5 53 54 49 4e 43 54 20 61 67 67 72 65 67 61 74 65  STINCT aggregate
28fb6 73 20 6d 75 73 74 20 68 61 76 65 20 65 78 61 63  s must have exac
28fb7 74 6c 79 20 6f 6e 65 20 22 0a 20 20 20 20 20 20  tly one ".      
28fb8 20 20 20 20 20 22 61 72 67 75 6d 65 6e 74 22 29       "argument")
28fb9 3b 0a 20 20 20 20 20 20 20 20 70 46 75 6e 63 2d  ;.        pFunc-
28fba 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b  >iDistinct = -1;
28fbb 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
28fbc 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
28fbd 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66  KeyInfo = keyInf
28fbe 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50  oFromExprList(pP
28fbf 61 72 73 65 2c 20 70 45 2d 3e 78 2e 70 4c 69 73  arse, pE->x.pLis
28fc0 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
28fc1 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
28fc2 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
28fc3 6c 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69  l, pFunc->iDisti
28fc4 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  nct, 0, 0,.     
28fc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28fc6 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
28fc7 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  Info, P4_KEYINFO
28fc8 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20  _HANDOFF);.     
28fc9 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
28fca 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
28fcb 20 4f 50 5f 41 67 67 46 69 6e 61 6c 69 7a 65 20   OP_AggFinalize 
28fcc 6f 70 63 6f 64 65 20 66 6f 72 20 65 76 65 72 79  opcode for every
28fcd 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
28fce 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 41 67  ion.** in the Ag
28fcf 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  gInfo structure.
28fd0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
28fd1 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74  finalizeAggFunct
28fd2 69 6f 6e 73 28 50 61 72 73 65 20 2a 70 50 61 72  ions(Parse *pPar
28fd3 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  se, AggInfo *pAg
28fd4 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a  gInfo){.  Vdbe *
28fd5 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
28fd6 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
28fd7 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
28fd8 63 20 2a 70 46 3b 0a 20 20 66 6f 72 28 69 3d 30  c *pF;.  for(i=0
28fd9 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61  , pF=pAggInfo->a
28fda 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f  Func; i<pAggInfo
28fdb 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46  ->nFunc; i++, pF
28fdc 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73  ++){.    ExprLis
28fdd 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70  t *pList = pF->p
28fde 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20  Expr->x.pList;. 
28fdf 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
28fe0 48 61 73 50 72 6f 70 65 72 74 79 28 70 46 2d 3e  HasProperty(pF->
28fe1 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
28fe2 65 63 74 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  ect) );.    sqli
28fe3 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
28fe4 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20 70 46   OP_AggFinal, pF
28fe5 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20 3f 20  ->iMem, pList ? 
28fe6 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30  pList->nExpr : 0
28fe7 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
28fe8 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64             (void
28fe9 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34 5f  *)pF->pFunc, P4_
28fea 46 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a 7d 0a  FUNCDEF);.  }.}.
28feb 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74 68  ./*.** Update th
28fec 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d 65  e accumulator me
28fed 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 61  mory cells for a
28fee 6e 20 61 67 67 72 65 67 61 74 65 20 62 61 73 65  n aggregate base
28fef 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72  d on.** the curr
28ff0 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74  ent cursor posit
28ff1 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
28ff2 6f 69 64 20 75 70 64 61 74 65 41 63 63 75 6d 75  oid updateAccumu
28ff3 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61  lator(Parse *pPa
28ff4 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41  rse, AggInfo *pA
28ff5 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20  ggInfo){.  Vdbe 
28ff6 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
28ff7 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  be;.  int i;.  s
28ff8 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
28ff9 6e 63 20 2a 70 46 3b 0a 20 20 73 74 72 75 63 74  nc *pF;.  struct
28ffa 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43   AggInfo_col *pC
28ffb 3b 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64  ;..  pAggInfo->d
28ffc 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20  irectMode = 1;. 
28ffd 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
28ffe 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a  eClear(pParse);.
28fff 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41    for(i=0, pF=pA
29000 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69  ggInfo->aFunc; i
29001 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63  <pAggInfo->nFunc
29002 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20  ; i++, pF++){.  
29003 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20    int nArg;.    
29004 69 6e 74 20 61 64 64 72 4e 65 78 74 20 3d 20 30  int addrNext = 0
29005 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67 67  ;.    int regAgg
29006 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a  ;.    ExprList *
29007 70 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70  pList = pF->pExp
29008 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20  r->x.pList;.    
29009 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2900a 50 72 6f 70 65 72 74 79 28 70 46 2d 3e 70 45 78  Property(pF->pEx
2900b 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
2900c 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69  ) );.    if( pLi
2900d 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 41 72 67  st ){.      nArg
2900e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
2900f 0a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d 20  .      regAgg = 
29010 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
29011 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 41 72 67  nge(pParse, nArg
29012 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29013 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74  ExprCodeExprList
29014 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20  (pParse, pList, 
29015 72 65 67 41 67 67 2c 20 30 29 3b 0a 20 20 20 20  regAgg, 0);.    
29016 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 41 72  }else{.      nAr
29017 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 67  g = 0;.      reg
29018 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Agg = 0;.    }. 
29019 20 20 20 69 66 28 20 70 46 2d 3e 69 44 69 73 74     if( pF->iDist
2901a 69 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20  inct>=0 ){.     
2901b 20 61 64 64 72 4e 65 78 74 20 3d 20 73 71 6c 69   addrNext = sqli
2901c 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2901d 28 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  (v);.      asser
2901e 74 28 20 6e 41 72 67 3d 3d 31 20 29 3b 0a 20 20  t( nArg==1 );.  
2901f 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63 74      codeDistinct
29020 28 70 50 61 72 73 65 2c 20 70 46 2d 3e 69 44 69  (pParse, pF->iDi
29021 73 74 69 6e 63 74 2c 20 61 64 64 72 4e 65 78 74  stinct, addrNext
29022 2c 20 31 2c 20 72 65 67 41 67 67 29 3b 0a 20 20  , 1, regAgg);.  
29023 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e    }.    if( pF->
29024 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53  pFunc->flags & S
29025 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
29026 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c  OLL ){.      Col
29027 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b  lSeq *pColl = 0;
29028 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78  .      struct Ex
29029 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
2902a 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  em;.      int j;
2902b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2902c 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 70  List!=0 );  /* p
2902d 4c 69 73 74 21 3d 30 20 69 66 20 70 46 2d 3e 70  List!=0 if pF->p
2902e 46 75 6e 63 20 68 61 73 20 4e 45 45 44 43 4f 4c  Func has NEEDCOL
2902f 4c 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6a  L */.      for(j
29030 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d  =0, pItem=pList-
29031 3e 61 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a 3c  >a; !pColl && j<
29032 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65 6d  nArg; j++, pItem
29033 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f  ++){.        pCo
29034 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
29035 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
29036 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20  pItem->pExpr);. 
29037 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
29038 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
29039 20 20 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72 73     pColl = pPars
2903a 65 2d 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c  e->db->pDfltColl
2903b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2903c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2903d 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c  4(v, OP_CollSeq,
2903e 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20   0, 0, 0, (char 
2903f 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c  *)pColl, P4_COLL
29040 53 45 51 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  SEQ);.    }.    
29041 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29042 34 28 76 2c 20 4f 50 5f 41 67 67 53 74 65 70 2c  4(v, OP_AggStep,
29043 20 30 2c 20 72 65 67 41 67 67 2c 20 70 46 2d 3e   0, regAgg, pF->
29044 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20  iMem,.          
29045 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69              (voi
29046 64 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34  d*)pF->pFunc, P4
29047 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 73  _FUNCDEF);.    s
29048 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
29049 50 35 28 76 2c 20 28 75 38 29 6e 41 72 67 29 3b  P5(v, (u8)nArg);
2904a 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
2904b 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61  aseTempRange(pPa
2904c 72 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41 72  rse, regAgg, nAr
2904d 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  g);.    sqlite3E
2904e 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
2904f 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
29050 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20  egAgg, nArg);.  
29051 20 20 69 66 28 20 61 64 64 72 4e 65 78 74 20 29    if( addrNext )
29052 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
29053 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
29054 76 2c 20 61 64 64 72 4e 65 78 74 29 3b 0a 20 20  v, addrNext);.  
29055 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
29056 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65  acheClear(pParse
29057 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  );.    }.  }.  f
29058 6f 72 28 69 3d 30 2c 20 70 43 3d 70 41 67 67 49  or(i=0, pC=pAggI
29059 6e 66 6f 2d 3e 61 43 6f 6c 3b 20 69 3c 70 41 67  nfo->aCol; i<pAg
2905a 67 49 6e 66 6f 2d 3e 6e 41 63 63 75 6d 75 6c 61  gInfo->nAccumula
2905b 74 6f 72 3b 20 69 2b 2b 2c 20 70 43 2b 2b 29 7b  tor; i++, pC++){
2905c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2905d 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43 2d  Code(pParse, pC-
2905e 3e 70 45 78 70 72 2c 20 70 43 2d 3e 69 4d 65 6d  >pExpr, pC->iMem
2905f 29 3b 0a 20 20 7d 0a 20 20 70 41 67 67 49 6e 66  );.  }.  pAggInf
29060 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20  o->directMode = 
29061 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  0;.  sqlite3Expr
29062 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
29063 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  e);.}../*.** Gen
29064 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
29065 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
29066 65 6e 74 20 67 69 76 65 6e 20 69 6e 20 74 68 65  ent given in the
29067 20 70 20 61 72 67 75 6d 65 6e 74 2e 20 20 0a 2a   p argument.  .*
29068 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73  *.** The results
29069 20 61 72 65 20 64 69 73 74 72 69 62 75 74 65 64   are distributed
2906a 20 69 6e 20 76 61 72 69 6f 75 73 20 77 61 79 73   in various ways
2906b 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
2906c 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66  e.** contents of
2906d 20 74 68 65 20 53 65 6c 65 63 74 44 65 73 74 20   the SelectDest 
2906e 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65  structure pointe
2906f 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e 74  d to by argument
29070 20 70 44 65 73 74 0a 2a 2a 20 61 73 20 66 6f 6c   pDest.** as fol
29071 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lows:.**.**     
29072 70 44 65 73 74 2d 3e 65 44 65 73 74 20 20 20 20  pDest->eDest    
29073 52 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 2d 2d  Result.**     --
29074 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d  ----------    --
29075 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
29076 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
29077 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20  ---------.**    
29078 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20   SRT_Output     
29079 20 47 65 6e 65 72 61 74 65 20 61 20 72 6f 77 20   Generate a row 
2907a 6f 66 20 6f 75 74 70 75 74 20 28 75 73 69 6e 67  of output (using
2907b 20 74 68 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f   the OP_ResultRo
2907c 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  w.**            
2907d 20 20 20 20 20 20 20 20 20 6f 70 63 6f 64 65 29           opcode)
2907e 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e   for each row in
2907f 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
29080 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 4d  .**.**     SRT_M
29081 65 6d 20 20 20 20 20 20 20 20 20 4f 6e 6c 79 20  em         Only 
29082 76 61 6c 69 64 20 69 66 20 74 68 65 20 72 65 73  valid if the res
29083 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c 65 20  ult is a single 
29084 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 20 20 20 20 20  column..**      
29085 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
29086 74 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 63  tore the first c
29087 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 69 72  olumn of the fir
29088 73 74 20 72 65 73 75 6c 74 20 72 6f 77 0a 2a 2a  st result row.**
29089 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2908a 20 20 20 20 20 69 6e 20 72 65 67 69 73 74 65 72       in register
2908b 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 74 68   pDest->iParm th
2908c 65 6e 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 72  en abandon the r
2908d 65 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  est.**          
2908e 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68             of th
2908f 65 20 71 75 65 72 79 2e 20 20 54 68 69 73 20 64  e query.  This d
29090 65 73 74 69 6e 61 74 69 6f 6e 20 69 6d 70 6c 69  estination impli
29091 65 73 20 22 4c 49 4d 49 54 20 31 22 2e 0a 2a 2a  es "LIMIT 1"..**
29092 0a 2a 2a 20 20 20 20 20 53 52 54 5f 53 65 74 20  .**     SRT_Set 
29093 20 20 20 20 20 20 20 20 54 68 65 20 72 65 73 75          The resu
29094 6c 74 20 6d 75 73 74 20 62 65 20 61 20 73 69 6e  lt must be a sin
29095 67 6c 65 20 63 6f 6c 75 6d 6e 2e 20 20 53 74 6f  gle column.  Sto
29096 72 65 20 65 61 63 68 0a 2a 2a 20 20 20 20 20 20  re each.**      
29097 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
29098 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 61 73 20  ow of result as 
29099 74 68 65 20 6b 65 79 20 69 6e 20 74 61 62 6c 65  the key in table
2909a 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 20 0a   pDest->iParm. .
2909b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2909c 20 20 20 20 20 20 20 41 70 70 6c 79 20 74 68 65         Apply the
2909d 20 61 66 66 69 6e 69 74 79 20 70 44 65 73 74 2d   affinity pDest-
2909e 3e 61 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65  >affinity before
2909f 20 73 74 6f 72 69 6e 67 0a 2a 2a 20 20 20 20 20   storing.**     
290a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290a1 72 65 73 75 6c 74 73 2e 20 20 55 73 65 64 20 74  results.  Used t
290a2 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 49 4e 20  o implement "IN 
290a3 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 2e 0a 2a  (SELECT ...)"..*
290a4 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 55 6e 69  *.**     SRT_Uni
290a5 6f 6e 20 20 20 20 20 20 20 53 74 6f 72 65 20 72  on       Store r
290a6 65 73 75 6c 74 73 20 61 73 20 61 20 6b 65 79 20  esults as a key 
290a7 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74  in a temporary t
290a8 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72  able pDest->iPar
290a9 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  m..**.**     SRT
290aa 5f 45 78 63 65 70 74 20 20 20 20 20 20 52 65 6d  _Except      Rem
290ab 6f 76 65 20 72 65 73 75 6c 74 73 20 66 72 6f 6d  ove results from
290ac 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
290ad 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72  able pDest->iPar
290ae 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  m..**.**     SRT
290af 5f 54 61 62 6c 65 20 20 20 20 20 20 20 53 74 6f  _Table       Sto
290b0 72 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74 65  re results in te
290b1 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44  mporary table pD
290b2 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 20 20  est->iParm..**  
290b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290b4 20 20 20 54 68 69 73 20 69 73 20 6c 69 6b 65 20     This is like 
290b5 53 52 54 5f 45 70 68 65 6d 54 61 62 20 65 78 63  SRT_EphemTab exc
290b6 65 70 74 20 74 68 61 74 20 74 68 65 20 74 61 62  ept that the tab
290b7 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  le.**           
290b8 20 20 20 20 20 20 20 20 20 20 69 73 20 61 73 73            is ass
290b9 75 6d 65 64 20 74 6f 20 61 6c 72 65 61 64 79 20  umed to already 
290ba 62 65 20 6f 70 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20  be open..**.**  
290bb 20 20 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20     SRT_EphemTab 
290bc 20 20 20 43 72 65 61 74 65 20 61 6e 20 74 65 6d     Create an tem
290bd 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44 65  porary table pDe
290be 73 74 2d 3e 69 50 61 72 6d 20 61 6e 64 20 73 74  st->iParm and st
290bf 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ore.**          
290c0 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72             the r
290c1 65 73 75 6c 74 20 74 68 65 72 65 2e 20 54 68 65  esult there. The
290c2 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20   cursor is left 
290c3 6f 70 65 6e 20 61 66 74 65 72 0a 2a 2a 20 20 20  open after.**   
290c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290c5 20 20 72 65 74 75 72 6e 69 6e 67 2e 20 20 54 68    returning.  Th
290c6 69 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f 54  is is like SRT_T
290c7 61 62 6c 65 20 65 78 63 65 70 74 20 74 68 61 74  able except that
290c8 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
290c9 20 20 20 20 20 20 20 20 74 68 69 73 20 64 65 73          this des
290ca 74 69 6e 61 74 69 6f 6e 20 75 73 65 73 20 4f 50  tination uses OP
290cb 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 74  _OpenEphemeral t
290cc 6f 20 63 72 65 61 74 65 0a 2a 2a 20 20 20 20 20  o create.**     
290cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290ce 74 68 65 20 74 61 62 6c 65 20 66 69 72 73 74 2e  the table first.
290cf 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 43  .**.**     SRT_C
290d0 6f 72 6f 75 74 69 6e 65 20 20 20 47 65 6e 65 72  oroutine   Gener
290d1 61 74 65 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65  ate a co-routine
290d2 20 74 68 61 74 20 72 65 74 75 72 6e 73 20 61 20   that returns a 
290d3 6e 65 77 20 72 6f 77 20 6f 66 0a 2a 2a 20 20 20  new row of.**   
290d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290d5 20 20 72 65 73 75 6c 74 73 20 65 61 63 68 20 74    results each t
290d6 69 6d 65 20 69 74 20 69 73 20 69 6e 76 6f 6b 65  ime it is invoke
290d7 64 2e 20 20 54 68 65 20 65 6e 74 72 79 20 70 6f  d.  The entry po
290d8 69 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  int.**          
290d9 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74 68             of th
290da 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20  e co-routine is 
290db 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
290dc 65 72 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e  er pDest->iParm.
290dd 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45  .**.**     SRT_E
290de 78 69 73 74 73 20 20 20 20 20 20 53 74 6f 72 65  xists      Store
290df 20 61 20 31 20 69 6e 20 6d 65 6d 6f 72 79 20 63   a 1 in memory c
290e0 65 6c 6c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  ell pDest->iParm
290e1 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 0a 2a   if the result.*
290e2 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
290e3 20 20 20 20 20 20 73 65 74 20 69 73 20 6e 6f 74        set is not
290e4 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 20 20   empty..**.**   
290e5 20 20 53 52 54 5f 44 69 73 63 61 72 64 20 20 20    SRT_Discard   
290e6 20 20 54 68 72 6f 77 20 74 68 65 20 72 65 73 75    Throw the resu
290e7 6c 74 73 20 61 77 61 79 2e 20 20 54 68 69 73 20  lts away.  This 
290e8 69 73 20 75 73 65 64 20 62 79 20 53 45 4c 45 43  is used by SELEC
290e9 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  T.**            
290ea 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d 65           stateme
290eb 6e 74 73 20 77 69 74 68 69 6e 20 74 72 69 67 67  nts within trigg
290ec 65 72 73 20 77 68 6f 73 65 20 6f 6e 6c 79 20 70  ers whose only p
290ed 75 72 70 6f 73 65 20 69 73 0a 2a 2a 20 20 20 20  urpose is.**    
290ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290ef 20 74 68 65 20 73 69 64 65 2d 65 66 66 65 63 74   the side-effect
290f0 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  s of functions..
290f1 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
290f2 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ne returns the n
290f3 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e  umber of errors.
290f4 20 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73 20    If any errors 
290f5 61 72 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72  are.** encounter
290f6 65 64 2c 20 74 68 65 6e 20 61 6e 20 61 70 70 72  ed, then an appr
290f7 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65  opriate error me
290f8 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 69 6e  ssage is left in
290f9 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72  .** pParse->zErr
290fa 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  Msg..**.** This 
290fb 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f 54  routine does NOT
290fc 20 66 72 65 65 20 74 68 65 20 53 65 6c 65 63 74   free the Select
290fd 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65   structure passe
290fe 64 20 69 6e 2e 20 20 54 68 65 0a 2a 2a 20 63 61  d in.  The.** ca
290ff 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6e  lling function n
29100 65 65 64 73 20 74 6f 20 64 6f 20 74 68 61 74 2e  eeds to do that.
29101 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
29102 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65  TE int sqlite3Se
29103 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70  lect(.  Parse *p
29104 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
29105 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
29106 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
29107 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
29108 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
29109 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63  tatement being c
2910a 6f 64 65 64 2e 20 2a 2f 0a 20 20 53 65 6c 65 63  oded. */.  Selec
2910b 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 20 20  tDest *pDest    
2910c 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
2910d 77 69 74 68 20 74 68 65 20 71 75 65 72 79 20 72  with the query r
2910e 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69  esults */.){.  i
2910f 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
29110 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
29111 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 65 72  unters */.  Wher
29112 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20  eInfo *pWInfo;  
29113 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f     /* Return fro
29114 6d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  m sqlite3WhereBe
29115 67 69 6e 28 29 20 2a 2f 0a 20 20 56 64 62 65 20  gin() */.  Vdbe 
29116 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
29117 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
29118 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72 20 63   machine under c
29119 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  onstruction */. 
2911a 20 69 6e 74 20 69 73 41 67 67 3b 20 20 20 20 20   int isAgg;     
2911b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2911c 66 6f 72 20 73 65 6c 65 63 74 20 6c 69 73 74 73  for select lists
2911d 20 6c 69 6b 65 20 22 63 6f 75 6e 74 28 2a 29 22   like "count(*)"
2911e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2911f 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a 20  pEList;      /* 
29120 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  List of columns 
29121 74 6f 20 65 78 74 72 61 63 74 2e 20 2a 2f 0a 20  to extract. */. 
29122 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
29123 73 74 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  st;     /* List 
29124 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 73 65 6c  of tables to sel
29125 65 63 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 45 78  ect from */.  Ex
29126 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20  pr *pWhere;     
29127 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
29128 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  E clause.  May b
29129 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72  e NULL */.  Expr
2912a 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20  List *pOrderBy; 
2912b 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
2912c 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  BY clause.  May 
2912d 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70  be NULL */.  Exp
2912e 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b  rList *pGroupBy;
2912f 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50      /* The GROUP
29130 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79   BY clause.  May
29131 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78   be NULL */.  Ex
29132 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 20  pr *pHaving;    
29133 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 49       /* The HAVI
29134 4e 47 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  NG clause.  May 
29135 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74  be NULL */.  int
29136 20 69 73 44 69 73 74 69 6e 63 74 3b 20 20 20 20   isDistinct;    
29137 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
29138 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  he DISTINCT keyw
29139 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
2913a 2f 0a 20 20 69 6e 74 20 64 69 73 74 69 6e 63 74  /.  int distinct
2913b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  ;          /* Ta
2913c 62 6c 65 20 74 6f 20 75 73 65 20 66 6f 72 20 74  ble to use for t
2913d 68 65 20 64 69 73 74 69 6e 63 74 20 73 65 74 20  he distinct set 
2913e 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b  */.  int rc = 1;
2913f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
29140 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 66  alue to return f
29141 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f  rom this functio
29142 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53  n */.  int addrS
29143 6f 72 74 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a  ortIndex;     /*
29144 20 41 64 64 72 65 73 73 20 6f 66 20 61 6e 20 4f   Address of an O
29145 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
29146 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  instruction */. 
29147 20 41 67 67 49 6e 66 6f 20 73 41 67 67 49 6e 66   AggInfo sAggInf
29148 6f 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72  o;      /* Infor
29149 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 61  mation used by a
2914a 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
2914b 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 3b 20   */.  int iEnd; 
2914c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2914d 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 65  Address of the e
2914e 6e 64 20 6f 66 20 74 68 65 20 71 75 65 72 79 20  nd of the query 
2914f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
29150 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
29151 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
29152 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 64 62 20  ection */..  db 
29153 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
29154 69 66 28 20 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e  if( p==0 || db->
29155 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
29156 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a  pParse->nErr ){.
29157 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
29158 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  }.  if( sqlite3A
29159 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2915a 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 2c 20   SQLITE_SELECT, 
2915b 30 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75 72  0, 0, 0) ) retur
2915c 6e 20 31 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73  n 1;.  memset(&s
2915d 41 67 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  AggInfo, 0, size
2915e 6f 66 28 73 41 67 67 49 6e 66 6f 29 29 3b 0a 0a  of(sAggInfo));..
2915f 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e    pOrderBy = p->
29160 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20  pOrderBy;.  if( 
29161 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79  IgnorableOrderby
29162 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 70  (pDest) ){.    p
29163 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  ->pOrderBy = 0;.
29164 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 73 65  .    /* In these
29165 20 63 61 73 65 73 20 74 68 65 20 44 49 53 54 49   cases the DISTI
29166 4e 43 54 20 6f 70 65 72 61 74 6f 72 20 6d 61 6b  NCT operator mak
29167 65 73 20 6e 6f 20 64 69 66 66 65 72 65 6e 63 65  es no difference
29168 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 72   to the.    ** r
29169 65 73 75 6c 74 73 2c 20 73 6f 20 72 65 6d 6f 76  esults, so remov
2916a 65 20 69 74 20 69 66 20 69 74 20 77 65 72 65 20  e it if it were 
2916b 73 70 65 63 69 66 69 65 64 2e 0a 20 20 20 20 2a  specified..    *
2916c 2f 0a 20 20 20 20 61 73 73 65 72 74 28 70 44 65  /.    assert(pDe
2916d 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45  st->eDest==SRT_E
2916e 78 69 73 74 73 20 7c 7c 20 70 44 65 73 74 2d 3e  xists || pDest->
2916f 65 44 65 73 74 3d 3d 53 52 54 5f 55 6e 69 6f 6e  eDest==SRT_Union
29170 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20   || .           
29171 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
29172 54 5f 45 78 63 65 70 74 20 7c 7c 20 70 44 65 73  T_Except || pDes
29173 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 44 69  t->eDest==SRT_Di
29174 73 63 61 72 64 29 3b 0a 20 20 20 20 70 2d 3e 73  scard);.    p->s
29175 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44  elFlags &= ~SF_D
29176 69 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 20 20 73  istinct;.  }.  s
29177 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70  qlite3SelectPrep
29178 28 70 50 61 72 73 65 2c 20 70 2c 20 30 29 3b 0a  (pParse, p, 0);.
29179 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e    pTabList = p->
2917a 70 53 72 63 3b 0a 20 20 70 45 4c 69 73 74 20 3d  pSrc;.  pEList =
2917b 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66   p->pEList;.  if
2917c 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
2917d 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
2917e 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73  ed ){.    goto s
2917f 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20  elect_end;.  }. 
29180 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70   p->pOrderBy = p
29181 4f 72 64 65 72 42 79 3b 0a 20 20 69 73 41 67 67  OrderBy;.  isAgg
29182 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20   = (p->selFlags 
29183 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 21  & SF_Aggregate)!
29184 3d 30 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74  =0;.  if( pEList
29185 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63  ==0 ) goto selec
29186 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 0a 20 20  t_end;..  /* .  
29187 2a 2a 20 44 6f 20 6e 6f 74 20 65 76 65 6e 20 61  ** Do not even a
29188 74 74 65 6d 70 74 20 74 6f 20 67 65 6e 65 72 61  ttempt to genera
29189 74 65 20 61 6e 79 20 63 6f 64 65 20 69 66 20 77  te any code if w
2918a 65 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 73  e have already s
2918b 65 65 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 73 20  een.  ** errors 
2918c 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74  before this rout
2918d 69 6e 65 20 73 74 61 72 74 73 2e 0a 20 20 2a 2f  ine starts..  */
2918e 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
2918f 45 72 72 3e 30 20 29 20 67 6f 74 6f 20 73 65 6c  Err>0 ) goto sel
29190 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 4f  ect_end;..  /* O
29191 52 44 45 52 20 42 59 20 69 73 20 69 67 6e 6f 72  RDER BY is ignor
29192 65 64 20 66 6f 72 20 73 6f 6d 65 20 64 65 73 74  ed for some dest
29193 69 6e 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20  inations..  */. 
29194 20 69 66 28 20 49 67 6e 6f 72 61 62 6c 65 4f 72   if( IgnorableOr
29195 64 65 72 62 79 28 70 44 65 73 74 29 20 29 7b 0a  derby(pDest) ){.
29196 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 30      pOrderBy = 0
29197 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69  ;.  }..  /* Begi
29198 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64  n generating cod
29199 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71  e..  */.  v = sq
2919a 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2919b 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  rse);.  if( v==0
2919c 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65   ) goto select_e
2919d 6e 64 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  nd;..  /* Genera
2919e 74 65 20 63 6f 64 65 20 66 6f 72 20 61 6c 6c 20  te code for all 
2919f 73 75 62 2d 71 75 65 72 69 65 73 20 69 6e 20 74  sub-queries in t
291a0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20  he FROM clause. 
291a1 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64   */.#if !defined
291a2 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
291a3 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e  QUERY) || !defin
291a4 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
291a5 49 45 57 29 0a 20 20 66 6f 72 28 69 3d 30 3b 20  IEW).  for(i=0; 
291a6 21 70 2d 3e 70 50 72 69 6f 72 20 26 26 20 69 3c  !p->pPrior && i<
291a7 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
291a8 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74  i++){.    struct
291a9 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
291aa 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74  Item = &pTabList
291ab 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 53 65 6c 65  ->a[i];.    Sele
291ac 63 74 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20  ctDest dest;.   
291ad 20 53 65 6c 65 63 74 20 2a 70 53 75 62 20 3d 20   Select *pSub = 
291ae 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a  pItem->pSelect;.
291af 20 20 20 20 69 6e 74 20 69 73 41 67 67 53 75 62      int isAggSub
291b0 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 75 62 3d  ;..    if( pSub=
291b1 3d 30 20 7c 7c 20 70 49 74 65 6d 2d 3e 69 73 50  =0 || pItem->isP
291b2 6f 70 75 6c 61 74 65 64 20 29 20 63 6f 6e 74 69  opulated ) conti
291b3 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 63  nue;..    /* Inc
291b4 72 65 6d 65 6e 74 20 50 61 72 73 65 2e 6e 48 65  rement Parse.nHe
291b5 69 67 68 74 20 62 79 20 74 68 65 20 68 65 69 67  ight by the heig
291b6 68 74 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73  ht of the larges
291b7 74 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20  t expression.   
291b8 20 2a 2a 20 74 72 65 65 20 72 65 66 65 72 65 64   ** tree refered
291b9 20 74 6f 20 62 79 20 74 68 69 73 2c 20 74 68 65   to by this, the
291ba 20 70 61 72 65 6e 74 20 73 65 6c 65 63 74 2e 20   parent select. 
291bb 54 68 65 20 63 68 69 6c 64 20 73 65 6c 65 63 74  The child select
291bc 0a 20 20 20 20 2a 2a 20 6d 61 79 20 63 6f 6e 74  .    ** may cont
291bd 61 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74  ain expression t
291be 72 65 65 73 20 6f 66 20 61 74 20 6d 6f 73 74 0a  rees of at most.
291bf 20 20 20 20 2a 2a 20 28 53 51 4c 49 54 45 5f 4d      ** (SQLITE_M
291c0 41 58 5f 45 58 50 52 5f 44 45 50 54 48 2d 50 61  AX_EXPR_DEPTH-Pa
291c1 72 73 65 2e 6e 48 65 69 67 68 74 29 20 68 65 69  rse.nHeight) hei
291c2 67 68 74 2e 20 54 68 69 73 20 69 73 20 61 20 62  ght. This is a b
291c3 69 74 0a 20 20 20 20 2a 2a 20 6d 6f 72 65 20 63  it.    ** more c
291c4 6f 6e 73 65 72 76 61 74 69 76 65 20 74 68 61 6e  onservative than
291c5 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75 74 20   necessary, but 
291c6 6d 75 63 68 20 65 61 73 69 65 72 20 74 68 61 6e  much easier than
291c7 20 65 6e 66 6f 72 63 69 6e 67 0a 20 20 20 20 2a   enforcing.    *
291c8 2a 20 61 6e 20 65 78 61 63 74 20 6c 69 6d 69 74  * an exact limit
291c9 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61  ..    */.    pPa
291ca 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20  rse->nHeight += 
291cb 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
291cc 72 48 65 69 67 68 74 28 70 29 3b 0a 0a 20 20 20  rHeight(p);..   
291cd 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   /* Check to see
291ce 20 69 66 20 74 68 65 20 73 75 62 71 75 65 72 79   if the subquery
291cf 20 63 61 6e 20 62 65 20 61 62 73 6f 72 62 65 64   can be absorbed
291d0 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74   into the parent
291d1 2e 20 2a 2f 0a 20 20 20 20 69 73 41 67 67 53 75  . */.    isAggSu
291d2 62 20 3d 20 28 70 53 75 62 2d 3e 73 65 6c 46 6c  b = (pSub->selFl
291d3 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61  ags & SF_Aggrega
291d4 74 65 29 21 3d 30 3b 0a 20 20 20 20 69 66 28 20  te)!=0;.    if( 
291d5 66 6c 61 74 74 65 6e 53 75 62 71 75 65 72 79 28  flattenSubquery(
291d6 70 50 61 72 73 65 2c 20 70 2c 20 69 2c 20 69 73  pParse, p, i, is
291d7 41 67 67 2c 20 69 73 41 67 67 53 75 62 29 20 29  Agg, isAggSub) )
291d8 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 41 67  {.      if( isAg
291d9 67 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20  gSub ){.        
291da 69 73 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20  isAgg = 1;.     
291db 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c     p->selFlags |
291dc 3d 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b 0a  = SF_Aggregate;.
291dd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 20        }.      i 
291de 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  = -1;.    }else{
291df 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
291e0 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
291e1 73 74 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62  st, SRT_EphemTab
291e2 2c 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  , pItem->iCursor
291e3 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
291e4 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61   pItem->isPopula
291e5 74 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ted==0 );.      
291e6 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
291e7 61 72 73 65 2c 20 70 53 75 62 2c 20 26 64 65 73  arse, pSub, &des
291e8 74 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d  t);.      pItem-
291e9 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 31  >isPopulated = 1
291ea 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
291eb 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
291ec 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
291ed 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
291ee 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 7d  elect_end;.    }
291ef 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65  .    pParse->nHe
291f0 69 67 68 74 20 2d 3d 20 73 71 6c 69 74 65 33 53  ight -= sqlite3S
291f1 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28  electExprHeight(
291f2 70 29 3b 0a 20 20 20 20 70 54 61 62 4c 69 73 74  p);.    pTabList
291f3 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20   = p->pSrc;.    
291f4 69 66 28 20 21 49 67 6e 6f 72 61 62 6c 65 4f 72  if( !IgnorableOr
291f5 64 65 72 62 79 28 70 44 65 73 74 29 20 29 7b 0a  derby(pDest) ){.
291f6 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d        pOrderBy =
291f7 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20   p->pOrderBy;.  
291f8 20 20 7d 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74    }.  }.  pEList
291f9 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 23 65   = p->pEList;.#e
291fa 6e 64 69 66 0a 20 20 70 57 68 65 72 65 20 3d 20  ndif.  pWhere = 
291fb 70 2d 3e 70 57 68 65 72 65 3b 0a 20 20 70 47 72  p->pWhere;.  pGr
291fc 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
291fd 70 42 79 3b 0a 20 20 70 48 61 76 69 6e 67 20 3d  pBy;.  pHaving =
291fe 20 70 2d 3e 70 48 61 76 69 6e 67 3b 0a 20 20 69   p->pHaving;.  i
291ff 73 44 69 73 74 69 6e 63 74 20 3d 20 28 70 2d 3e  sDistinct = (p->
29200 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
29201 73 74 69 6e 63 74 29 21 3d 30 3b 0a 0a 23 69 66  stinct)!=0;..#if
29202 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
29203 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
29204 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
29205 73 20 61 72 65 20 61 20 73 65 71 75 65 6e 63 65  s are a sequence
29206 20 6f 66 20 71 75 65 72 69 65 73 2c 20 64 6f 20   of queries, do 
29207 74 68 65 20 65 61 72 6c 69 65 72 20 6f 6e 65 73  the earlier ones
29208 20 66 69 72 73 74 2e 0a 20 20 2a 2f 0a 20 20 69   first..  */.  i
29209 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a  f( p->pPrior ){.
2920a 20 20 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68      if( p->pRigh
2920b 74 6d 6f 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  tmost==0 ){.    
2920c 20 20 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 2c    Select *pLoop,
2920d 20 2a 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20   *pRight = 0;.  
2920e 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b      int cnt = 0;
2920f 0a 20 20 20 20 20 20 69 6e 74 20 6d 78 53 65 6c  .      int mxSel
29210 65 63 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  ect;.      for(p
29211 4c 6f 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b 20 70  Loop=p; pLoop; p
29212 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50 72 69  Loop=pLoop->pPri
29213 6f 72 2c 20 63 6e 74 2b 2b 29 7b 0a 20 20 20 20  or, cnt++){.    
29214 20 20 20 20 70 4c 6f 6f 70 2d 3e 70 52 69 67 68      pLoop->pRigh
29215 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20 20 20 20  tmost = p;.     
29216 20 20 20 70 4c 6f 6f 70 2d 3e 70 4e 65 78 74 20     pLoop->pNext 
29217 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  = pRight;.      
29218 20 20 70 52 69 67 68 74 20 3d 20 70 4c 6f 6f 70    pRight = pLoop
29219 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2921a 6d 78 53 65 6c 65 63 74 20 3d 20 64 62 2d 3e 61  mxSelect = db->a
2921b 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
2921c 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
2921d 43 54 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 6d  CT];.      if( m
2921e 78 53 65 6c 65 63 74 20 26 26 20 63 6e 74 3e 6d  xSelect && cnt>m
2921f 78 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  xSelect ){.     
29220 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
29221 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
29222 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20 63 6f  many terms in co
29223 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 22 29 3b  mpound SELECT");
29224 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
29225 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
29226 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 75 6c 74  .    return mult
29227 69 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  iSelect(pParse, 
29228 70 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 23  p, pDest);.  }.#
29229 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77  endif..  /* If w
2922a 72 69 74 69 6e 67 20 74 6f 20 6d 65 6d 6f 72 79  riting to memory
2922b 20 6f 72 20 67 65 6e 65 72 61 74 69 6e 67 20 61   or generating a
2922c 20 73 65 74 0a 20 20 2a 2a 20 6f 6e 6c 79 20 61   set.  ** only a
2922d 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6d   single column m
2922e 61 79 20 62 65 20 6f 75 74 70 75 74 2e 0a 20 20  ay be output..  
2922f 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
29230 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
29231 20 20 69 66 28 20 63 68 65 63 6b 46 6f 72 4d 75    if( checkForMu
29232 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45  ltiColumnSelectE
29233 72 72 6f 72 28 70 50 61 72 73 65 2c 20 70 44 65  rror(pParse, pDe
29234 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  st, pEList->nExp
29235 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73  r) ){.    goto s
29236 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23  elect_end;.  }.#
29237 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70  endif..  /* If p
29238 6f 73 73 69 62 6c 65 2c 20 72 65 77 72 69 74 65  ossible, rewrite
29239 20 74 68 65 20 71 75 65 72 79 20 74 6f 20 75 73   the query to us
2923a 65 20 47 52 4f 55 50 20 42 59 20 69 6e 73 74 65  e GROUP BY inste
2923b 61 64 20 6f 66 20 44 49 53 54 49 4e 43 54 2e 0a  ad of DISTINCT..
2923c 20 20 2a 2a 20 47 52 4f 55 50 20 42 59 20 6d 69    ** GROUP BY mi
2923d 67 68 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78  ght use an index
2923e 2c 20 44 49 53 54 49 4e 43 54 20 6e 65 76 65 72  , DISTINCT never
2923f 20 64 6f 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66   does..  */.  if
29240 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  ( (p->selFlags &
29241 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46   (SF_Distinct|SF
29242 5f 41 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46  _Aggregate))==SF
29243 5f 44 69 73 74 69 6e 63 74 20 26 26 20 21 70 2d  _Distinct && !p-
29244 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20  >pGroupBy ){.   
29245 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73   p->pGroupBy = s
29246 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
29247 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c  p(db, p->pEList,
29248 20 30 29 3b 0a 20 20 20 20 70 47 72 6f 75 70 42   0);.    pGroupB
29249 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b  y = p->pGroupBy;
2924a 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73  .    p->selFlags
2924b 20 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e 63 74   &= ~SF_Distinct
2924c 3b 0a 20 20 20 20 69 73 44 69 73 74 69 6e 63 74  ;.    isDistinct
2924d 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
2924e 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  If there is an O
2924f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20  RDER BY clause, 
29250 74 68 65 6e 20 74 68 69 73 20 73 6f 72 74 69 6e  then this sortin
29251 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6d 69 67  g.  ** index mig
29252 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20  ht end up being 
29253 75 6e 75 73 65 64 20 69 66 20 74 68 65 20 64 61  unused if the da
29254 74 61 20 63 61 6e 20 62 65 20 0a 20 20 2a 2a 20  ta can be .  ** 
29255 65 78 74 72 61 63 74 65 64 20 69 6e 20 70 72 65  extracted in pre
29256 2d 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20  -sorted order.  
29257 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63  If that is the c
29258 61 73 65 2c 20 74 68 65 6e 20 74 68 65 0a 20 20  ase, then the.  
29259 2a 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  ** OP_OpenEpheme
2925a 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ral instruction 
2925b 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 20  will be changed 
2925c 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 20 6f 6e  to an OP_Noop on
2925d 63 65 0a 20 20 2a 2a 20 77 65 20 66 69 67 75 72  ce.  ** we figur
2925e 65 20 6f 75 74 20 74 68 61 74 20 74 68 65 20 73  e out that the s
2925f 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 69 73 20  orting index is 
29260 6e 6f 74 20 6e 65 65 64 65 64 2e 20 20 54 68 65  not needed.  The
29261 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 0a 20   addrSortIndex. 
29262 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20   ** variable is 
29263 75 73 65 64 20 74 6f 20 66 61 63 69 6c 69 74 61  used to facilita
29264 74 65 20 74 68 61 74 20 63 68 61 6e 67 65 2e 0a  te that change..
29265 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65    */.  if( pOrde
29266 72 42 79 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e  rBy ){.    KeyIn
29267 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
29268 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79    pKeyInfo = key
29269 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74  InfoFromExprList
2926a 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
2926b 79 29 3b 0a 20 20 20 20 70 4f 72 64 65 72 42 79  y);.    pOrderBy
2926c 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 70 50 61  ->iECursor = pPa
2926d 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2926e 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
2926f 5b 32 5d 20 3d 20 61 64 64 72 53 6f 72 74 49 6e  [2] = addrSortIn
29270 64 65 78 20 3d 0a 20 20 20 20 20 20 73 71 6c 69  dex =.      sqli
29271 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
29272 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
29273 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
29274 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f                pO
29275 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72  rderBy->iECursor
29276 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  , pOrderBy->nExp
29277 72 2b 32 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  r+2, 0,.        
29278 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29279 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
2927a 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  fo, P4_KEYINFO_H
2927b 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65 6c 73 65  ANDOFF);.  }else
2927c 7b 0a 20 20 20 20 61 64 64 72 53 6f 72 74 49 6e  {.    addrSortIn
2927d 64 65 78 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20  dex = -1;.  }.. 
2927e 20 2f 2a 20 49 66 20 74 68 65 20 6f 75 74 70 75   /* If the outpu
2927f 74 20 69 73 20 64 65 73 74 69 6e 65 64 20 66 6f  t is destined fo
29280 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  r a temporary ta
29281 62 6c 65 2c 20 6f 70 65 6e 20 74 68 61 74 20 74  ble, open that t
29282 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
29283 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
29284 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a 20  RT_EphemTab ){. 
29285 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29286 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
29287 70 68 65 6d 65 72 61 6c 2c 20 70 44 65 73 74 2d  phemeral, pDest-
29288 3e 69 50 61 72 6d 2c 20 70 45 4c 69 73 74 2d 3e  >iParm, pEList->
29289 6e 45 78 70 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f  nExpr);.  }..  /
2928a 2a 20 53 65 74 20 74 68 65 20 6c 69 6d 69 74 65  * Set the limite
2928b 72 2e 0a 20 20 2a 2f 0a 20 20 69 45 6e 64 20 3d  r..  */.  iEnd =
2928c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2928d 4c 61 62 65 6c 28 76 29 3b 0a 20 20 63 6f 6d 70  Label(v);.  comp
2928e 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72  uteLimitRegister
2928f 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69 45 6e  s(pParse, p, iEn
29290 64 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61  d);..  /* Open a
29291 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20 74   virtual index t
29292 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 64 69  o use for the di
29293 73 74 69 6e 63 74 20 73 65 74 2e 0a 20 20 2a 2f  stinct set..  */
29294 0a 20 20 69 66 28 20 69 73 44 69 73 74 69 6e 63  .  if( isDistinc
29295 74 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f  t ){.    KeyInfo
29296 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20   *pKeyInfo;.    
29297 61 73 73 65 72 74 28 20 69 73 41 67 67 20 7c 7c  assert( isAgg ||
29298 20 70 47 72 6f 75 70 42 79 20 29 3b 0a 20 20 20   pGroupBy );.   
29299 20 64 69 73 74 69 6e 63 74 20 3d 20 70 50 61 72   distinct = pPar
2929a 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
2929b 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e  pKeyInfo = keyIn
2929c 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70  foFromExprList(p
2929d 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74  Parse, p->pEList
2929e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2929f 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
292a0 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64 69  penEphemeral, di
292a1 73 74 69 6e 63 74 2c 20 30 2c 20 30 2c 0a 20 20  stinct, 0, 0,.  
292a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292a3 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
292a4 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
292a5 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65  O_HANDOFF);.  }e
292a6 6c 73 65 7b 0a 20 20 20 20 64 69 73 74 69 6e 63  lse{.    distinc
292a7 74 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f  t = -1;.  }..  /
292a8 2a 20 41 67 67 72 65 67 61 74 65 20 61 6e 64 20  * Aggregate and 
292a9 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75  non-aggregate qu
292aa 65 72 69 65 73 20 61 72 65 20 68 61 6e 64 6c 65  eries are handle
292ab 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 2a 2f  d differently */
292ac 0a 20 20 69 66 28 20 21 69 73 41 67 67 20 26 26  .  if( !isAgg &&
292ad 20 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a   pGroupBy==0 ){.
292ae 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65      /* This case
292af 20 69 73 20 66 6f 72 20 6e 6f 6e 2d 61 67 67 72   is for non-aggr
292b0 65 67 61 74 65 20 71 75 65 72 69 65 73 0a 20 20  egate queries.  
292b1 20 20 2a 2a 20 42 65 67 69 6e 20 74 68 65 20 64    ** Begin the d
292b2 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20 20  atabase scan.   
292b3 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d   */.    pWInfo =
292b4 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
292b5 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
292b6 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70 4f  ist, pWhere, &pO
292b7 72 64 65 72 42 79 2c 20 30 29 3b 0a 20 20 20 20  rderBy, 0);.    
292b8 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20  if( pWInfo==0 ) 
292b9 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
292ba 0a 0a 20 20 20 20 2f 2a 20 49 66 20 73 6f 72 74  ..    /* If sort
292bb 69 6e 67 20 69 6e 64 65 78 20 74 68 61 74 20 77  ing index that w
292bc 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61 20  as created by a 
292bd 70 72 69 6f 72 20 4f 50 5f 4f 70 65 6e 45 70 68  prior OP_OpenEph
292be 65 6d 65 72 61 6c 20 0a 20 20 20 20 2a 2a 20 69  emeral .    ** i
292bf 6e 73 74 72 75 63 74 69 6f 6e 20 65 6e 64 65 64  nstruction ended
292c0 20 75 70 20 6e 6f 74 20 62 65 69 6e 67 20 6e 65   up not being ne
292c1 65 64 65 64 2c 20 74 68 65 6e 20 63 68 61 6e 67  eded, then chang
292c2 65 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68  e the OP_OpenEph
292c3 65 6d 65 72 61 6c 0a 20 20 20 20 2a 2a 20 69 6e  emeral.    ** in
292c4 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a 20  to an OP_Noop.. 
292c5 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 64     */.    if( ad
292c6 64 72 53 6f 72 74 49 6e 64 65 78 3e 3d 30 20 26  drSortIndex>=0 &
292c7 26 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b  & pOrderBy==0 ){
292c8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
292c9 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76  beChangeToNoop(v
292ca 2c 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 2c  , addrSortIndex,
292cb 20 31 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64   1);.      p->ad
292cc 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20  drOpenEphm[2] = 
292cd 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  -1;.    }..    /
292ce 2a 20 55 73 65 20 74 68 65 20 73 74 61 6e 64 61  * Use the standa
292cf 72 64 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 20 20  rd inner loop.  
292d0 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
292d1 21 69 73 44 69 73 74 69 6e 63 74 29 3b 0a 20 20  !isDistinct);.  
292d2 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f    selectInnerLoo
292d3 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 45 4c  p(pParse, p, pEL
292d4 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65  ist, 0, 0, pOrde
292d5 72 42 79 2c 20 2d 31 2c 20 70 44 65 73 74 2c 0a  rBy, -1, pDest,.
292d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292d7 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e      pWInfo->iCon
292d8 74 69 6e 75 65 2c 20 70 57 49 6e 66 6f 2d 3e 69  tinue, pWInfo->i
292d9 42 72 65 61 6b 29 3b 0a 0a 20 20 20 20 2f 2a 20  Break);..    /* 
292da 45 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  End the database
292db 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20   scan loop..    
292dc 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 57 68  */.    sqlite3Wh
292dd 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
292de 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
292df 54 68 69 73 20 69 73 20 74 68 65 20 70 72 6f 63  This is the proc
292e0 65 73 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65  essing for aggre
292e1 67 61 74 65 20 71 75 65 72 69 65 73 20 2a 2f 0a  gate queries */.
292e2 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
292e3 73 4e 43 3b 20 20 20 20 2f 2a 20 4e 61 6d 65 20  sNC;    /* Name 
292e4 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 72 6f 63  context for proc
292e5 65 73 73 69 6e 67 20 61 67 67 72 65 67 61 74 65  essing aggregate
292e6 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a   information */.
292e7 20 20 20 20 69 6e 74 20 69 41 4d 65 6d 3b 20 20      int iAMem;  
292e8 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
292e9 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72   Mem address for
292ea 20 73 74 6f 72 69 6e 67 20 63 75 72 72 65 6e 74   storing current
292eb 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20   GROUP BY */.   
292ec 20 69 6e 74 20 69 42 4d 65 6d 3b 20 20 20 20 20   int iBMem;     
292ed 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d 65       /* First Me
292ee 6d 20 61 64 64 72 65 73 73 20 66 6f 72 20 70 72  m address for pr
292ef 65 76 69 6f 75 73 20 47 52 4f 55 50 20 42 59 20  evious GROUP BY 
292f0 2a 2f 0a 20 20 20 20 69 6e 74 20 69 55 73 65 46  */.    int iUseF
292f1 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65  lag;       /* Me
292f2 6d 20 61 64 64 72 65 73 73 20 68 6f 6c 64 69 6e  m address holdin
292f3 67 20 66 6c 61 67 20 69 6e 64 69 63 61 74 69 6e  g flag indicatin
292f4 67 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 0a  g that at least.
292f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292f6 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 65 20 72          ** one r
292f7 6f 77 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  ow of the input 
292f8 74 6f 20 74 68 65 20 61 67 67 72 65 67 61 74 6f  to the aggregato
292f9 72 20 68 61 73 20 62 65 65 6e 0a 20 20 20 20 20  r has been.     
292fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292fb 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 65 64 20     ** processed 
292fc 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 62 6f 72  */.    int iAbor
292fd 74 46 6c 61 67 3b 20 20 20 20 20 2f 2a 20 4d 65  tFlag;     /* Me
292fe 6d 20 61 64 64 72 65 73 73 20 77 68 69 63 68 20  m address which 
292ff 63 61 75 73 65 73 20 71 75 65 72 79 20 61 62 6f  causes query abo
29300 72 74 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a  rt if positive *
29301 2f 0a 20 20 20 20 69 6e 74 20 67 72 6f 75 70 42  /.    int groupB
29302 79 53 6f 72 74 3b 20 20 20 20 2f 2a 20 52 6f 77  ySort;    /* Row
29303 73 20 63 6f 6d 65 20 66 72 6f 6d 20 73 6f 75 72  s come from sour
29304 63 65 20 69 6e 20 47 52 4f 55 50 20 42 59 20 6f  ce in GROUP BY o
29305 72 64 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  rder */.    int 
29306 61 64 64 72 45 6e 64 3b 20 20 20 20 20 20 20 20  addrEnd;        
29307 2f 2a 20 45 6e 64 20 6f 66 20 70 72 6f 63 65 73  /* End of proces
29308 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53 45  sing for this SE
29309 4c 45 43 54 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  LECT */..    /* 
2930a 52 65 6d 6f 76 65 20 61 6e 79 20 61 6e 64 20 61  Remove any and a
2930b 6c 6c 20 61 6c 69 61 73 65 73 20 62 65 74 77 65  ll aliases betwe
2930c 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  en the result se
2930d 74 20 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a  t and the.    **
2930e 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
2930f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
29310 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20   pGroupBy ){.   
29311 20 20 20 69 6e 74 20 6b 3b 20 20 20 20 20 20 20     int k;       
29312 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29313 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
29314 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74   */.      struct
29315 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
29316 70 49 74 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c  pItem;  /* For l
29317 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 70 72  ooping over expr
29318 65 73 73 69 6f 6e 20 69 6e 20 61 20 6c 69 73 74  ession in a list
29319 20 2a 2f 0a 0a 20 20 20 20 20 20 66 6f 72 28 6b   */..      for(k
2931a 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  =p->pEList->nExp
2931b 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 70 45 4c 69  r, pItem=p->pELi
2931c 73 74 2d 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c  st->a; k>0; k--,
2931d 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
2931e 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73     pItem->iAlias
2931f 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
29320 20 20 20 20 66 6f 72 28 6b 3d 70 47 72 6f 75 70      for(k=pGroup
29321 42 79 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d  By->nExpr, pItem
29322 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b 20 6b 3e  =pGroupBy->a; k>
29323 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; k--, pItem++)
29324 7b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d  {.        pItem-
29325 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20  >iAlias = 0;.   
29326 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 0a 20 20     }.    }.. .  
29327 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6c 61    /* Create a la
29328 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 77  bel to jump to w
29329 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20 61  hen we want to a
2932a 62 6f 72 74 20 74 68 65 20 71 75 65 72 79 20 2a  bort the query *
2932b 2f 0a 20 20 20 20 61 64 64 72 45 6e 64 20 3d 20  /.    addrEnd = 
2932c 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2932d 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a  abel(v);..    /*
2932e 20 43 6f 6e 76 65 72 74 20 54 4b 5f 43 4f 4c 55   Convert TK_COLU
2932f 4d 4e 20 6e 6f 64 65 73 20 69 6e 74 6f 20 54 4b  MN nodes into TK
29330 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20  _AGG_COLUMN and 
29331 6d 61 6b 65 20 65 6e 74 72 69 65 73 20 69 6e 0a  make entries in.
29332 20 20 20 20 2a 2a 20 73 41 67 67 49 6e 66 6f 20      ** sAggInfo 
29333 66 6f 72 20 61 6c 6c 20 54 4b 5f 41 47 47 5f 46  for all TK_AGG_F
29334 55 4e 43 54 49 4f 4e 20 6e 6f 64 65 73 20 69 6e  UNCTION nodes in
29335 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   expressions of 
29336 74 68 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43  the.    ** SELEC
29337 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  T statement..   
29338 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26   */.    memset(&
29339 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
2933a 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50  NC));.    sNC.pP
2933b 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2933c 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20     sNC.pSrcList 
2933d 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 20 20  = pTabList;.    
2933e 73 4e 43 2e 70 41 67 67 49 6e 66 6f 20 3d 20 26  sNC.pAggInfo = &
2933f 73 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 73 41  sAggInfo;.    sA
29340 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43  ggInfo.nSortingC
29341 6f 6c 75 6d 6e 20 3d 20 70 47 72 6f 75 70 42 79  olumn = pGroupBy
29342 20 3f 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78   ? pGroupBy->nEx
29343 70 72 2b 31 20 3a 20 30 3b 0a 20 20 20 20 73 41  pr+1 : 0;.    sA
29344 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70 42 79 20  ggInfo.pGroupBy 
29345 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20  = pGroupBy;.    
29346 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
29347 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20  zeAggList(&sNC, 
29348 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c  pEList);.    sql
29349 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
2934a 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 4f 72  ggList(&sNC, pOr
2934b 64 65 72 42 79 29 3b 0a 20 20 20 20 69 66 28 20  derBy);.    if( 
2934c 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 20  pHaving ){.     
2934d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
2934e 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 26 73  yzeAggregates(&s
2934f 4e 43 2c 20 70 48 61 76 69 6e 67 29 3b 0a 20 20  NC, pHaving);.  
29350 20 20 7d 0a 20 20 20 20 73 41 67 67 49 6e 66 6f    }.    sAggInfo
29351 2e 6e 41 63 63 75 6d 75 6c 61 74 6f 72 20 3d 20  .nAccumulator = 
29352 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e  sAggInfo.nColumn
29353 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
29354 3c 73 41 67 67 49 6e 66 6f 2e 6e 46 75 6e 63 3b  <sAggInfo.nFunc;
29355 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73   i++){.      ass
29356 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
29357 70 65 72 74 79 28 73 41 67 67 49 6e 66 6f 2e 61  perty(sAggInfo.a
29358 46 75 6e 63 5b 69 5d 2e 70 45 78 70 72 2c 20 45  Func[i].pExpr, E
29359 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2935a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2935b 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28  rAnalyzeAggList(
2935c 26 73 4e 43 2c 20 73 41 67 67 49 6e 66 6f 2e 61  &sNC, sAggInfo.a
2935d 46 75 6e 63 5b 69 5d 2e 70 45 78 70 72 2d 3e 78  Func[i].pExpr->x
2935e 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20  .pList);.    }. 
2935f 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
29360 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 73  cFailed ) goto s
29361 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20  elect_end;..    
29362 2f 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 66 6f  /* Processing fo
29363 72 20 61 67 67 72 65 67 61 74 65 73 20 77 69 74  r aggregates wit
29364 68 20 47 52 4f 55 50 20 42 59 20 69 73 20 76 65  h GROUP BY is ve
29365 72 79 20 64 69 66 66 65 72 65 6e 74 20 61 6e 64  ry different and
29366 0a 20 20 20 20 2a 2a 20 6d 75 63 68 20 6d 6f 72  .    ** much mor
29367 65 20 63 6f 6d 70 6c 65 78 20 74 68 61 6e 20 61  e complex than a
29368 67 67 72 65 67 61 74 65 73 20 77 69 74 68 6f 75  ggregates withou
29369 74 20 61 20 47 52 4f 55 50 20 42 59 2e 0a 20 20  t a GROUP BY..  
2936a 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47 72    */.    if( pGr
2936b 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 4b  oupBy ){.      K
2936c 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
2936d 3b 20 20 2f 2a 20 4b 65 79 69 6e 67 20 69 6e 66  ;  /* Keying inf
2936e 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  ormation for the
2936f 20 67 72 6f 75 70 20 62 79 20 63 6c 61 75 73 65   group by clause
29370 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 31   */.      int j1
29371 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
29372 20 41 2d 76 73 2d 42 20 63 6f 6d 70 61 72 69 73   A-vs-B comparis
29373 69 6f 6e 20 6a 75 6d 70 20 2a 2f 0a 20 20 20 20  ion jump */.    
29374 20 20 69 6e 74 20 61 64 64 72 4f 75 74 70 75 74    int addrOutput
29375 52 6f 77 3b 20 20 2f 2a 20 53 74 61 72 74 20 6f  Row;  /* Start o
29376 66 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61  f subroutine tha
29377 74 20 6f 75 74 70 75 74 73 20 61 20 72 65 73 75  t outputs a resu
29378 6c 74 20 72 6f 77 20 2a 2f 0a 20 20 20 20 20 20  lt row */.      
29379 69 6e 74 20 72 65 67 4f 75 74 70 75 74 52 6f 77  int regOutputRow
2937a 3b 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64  ;   /* Return ad
2937b 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66  dress register f
2937c 6f 72 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75  or output subrou
2937d 74 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e  tine */.      in
2937e 74 20 61 64 64 72 53 65 74 41 62 6f 72 74 3b 20  t addrSetAbort; 
2937f 20 20 2f 2a 20 53 65 74 20 74 68 65 20 61 62 6f    /* Set the abo
29380 72 74 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75  rt flag and retu
29381 72 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  rn */.      int 
29382 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 3b 20 20  addrTopOfLoop;  
29383 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 69 6e  /* Top of the in
29384 70 75 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20  put loop */.    
29385 20 20 69 6e 74 20 61 64 64 72 53 6f 72 74 69 6e    int addrSortin
29386 67 49 64 78 3b 20 2f 2a 20 54 68 65 20 4f 50 5f  gIdx; /* The OP_
29387 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 66 6f  OpenEphemeral fo
29388 72 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e  r the sorting in
29389 64 65 78 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  dex */.      int
2938a 20 61 64 64 72 52 65 73 65 74 3b 20 20 20 20 20   addrReset;     
2938b 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 66   /* Subroutine f
2938c 6f 72 20 72 65 73 65 74 74 69 6e 67 20 74 68 65  or resetting the
2938d 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a   accumulator */.
2938e 20 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 73        int regRes
2938f 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74  et;       /* Ret
29390 75 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69  urn address regi
29391 73 74 65 72 20 66 6f 72 20 72 65 73 65 74 20 73  ster for reset s
29392 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 0a 20 20  ubroutine */..  
29393 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
29394 69 73 20 61 20 47 52 4f 55 50 20 42 59 20 63 6c  is a GROUP BY cl
29395 61 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e 65  ause we might ne
29396 65 64 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64  ed a sorting ind
29397 65 78 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 69  ex to.      ** i
29398 6d 70 6c 65 6d 65 6e 74 20 69 74 2e 20 20 41 6c  mplement it.  Al
29399 6c 6f 63 61 74 65 20 74 68 61 74 20 73 6f 72 74  locate that sort
2939a 69 6e 67 20 69 6e 64 65 78 20 6e 6f 77 2e 20 20  ing index now.  
2939b 49 66 20 69 74 20 74 75 72 6e 73 20 6f 75 74 0a  If it turns out.
2939c 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65        ** that we
2939d 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 69 74 20   do not need it 
2939e 61 66 74 65 72 20 61 6c 6c 2c 20 74 68 65 20 4f  after all, the O
2939f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73  penEphemeral ins
293a0 74 72 75 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a  truction.      *
293a1 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72  * will be conver
293a2 74 65 64 20 69 6e 74 6f 20 61 20 4e 6f 6f 70 2e  ted into a Noop.
293a3 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20    .      */.    
293a4 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69    sAggInfo.sorti
293a5 6e 67 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e  ngIdx = pParse->
293a6 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 70 4b  nTab++;.      pK
293a7 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f  eyInfo = keyInfo
293a8 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61  FromExprList(pPa
293a9 72 73 65 2c 20 70 47 72 6f 75 70 42 79 29 3b 0a  rse, pGroupBy);.
293aa 20 20 20 20 20 20 61 64 64 72 53 6f 72 74 69 6e        addrSortin
293ab 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33 56 64  gIdx = sqlite3Vd
293ac 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
293ad 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 0a 20  penEphemeral, . 
293ae 20 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66           sAggInf
293af 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 73 41  o.sortingIdx, sA
293b0 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67 43  ggInfo.nSortingC
293b1 6f 6c 75 6d 6e 2c 20 0a 20 20 20 20 20 20 20 20  olumn, .        
293b2 20 20 30 2c 20 28 63 68 61 72 2a 29 70 4b 65 79    0, (char*)pKey
293b3 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  Info, P4_KEYINFO
293b4 5f 48 41 4e 44 4f 46 46 29 3b 0a 0a 20 20 20 20  _HANDOFF);..    
293b5 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
293b6 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
293b7 20 75 73 65 64 20 62 79 20 47 52 4f 55 50 20 42   used by GROUP B
293b8 59 20 61 67 67 72 65 67 61 74 65 20 70 72 6f 63  Y aggregate proc
293b9 65 73 73 69 6e 67 0a 20 20 20 20 20 20 2a 2f 0a  essing.      */.
293ba 20 20 20 20 20 20 69 55 73 65 46 6c 61 67 20 3d        iUseFlag =
293bb 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
293bc 0a 20 20 20 20 20 20 69 41 62 6f 72 74 46 6c 61  .      iAbortFla
293bd 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  g = ++pParse->nM
293be 65 6d 3b 0a 20 20 20 20 20 20 72 65 67 4f 75 74  em;.      regOut
293bf 70 75 74 52 6f 77 20 3d 20 2b 2b 70 50 61 72 73  putRow = ++pPars
293c0 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61  e->nMem;.      a
293c1 64 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73  ddrOutputRow = s
293c2 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
293c3 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 72 65  bel(v);.      re
293c4 67 52 65 73 65 74 20 3d 20 2b 2b 70 50 61 72 73  gReset = ++pPars
293c5 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 61  e->nMem;.      a
293c6 64 64 72 52 65 73 65 74 20 3d 20 73 71 6c 69 74  ddrReset = sqlit
293c7 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
293c8 76 29 3b 0a 20 20 20 20 20 20 69 41 4d 65 6d 20  v);.      iAMem 
293c9 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b  = pParse->nMem +
293ca 20 31 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65   1;.      pParse
293cb 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70  ->nMem += pGroup
293cc 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20  By->nExpr;.     
293cd 20 69 42 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d   iBMem = pParse-
293ce 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20  >nMem + 1;.     
293cf 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
293d0 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
293d1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
293d2 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
293d3 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 41 62 6f  Integer, 0, iAbo
293d4 72 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56  rtFlag);.      V
293d5 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
293d6 63 6c 65 61 72 20 61 62 6f 72 74 20 66 6c 61 67  clear abort flag
293d7 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  "));.      sqlit
293d8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
293d9 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69  OP_Integer, 0, i
293da 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20 20 20  UseFlag);.      
293db 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
293dc 22 69 6e 64 69 63 61 74 65 20 61 63 63 75 6d 75  "indicate accumu
293dd 6c 61 74 6f 72 20 65 6d 70 74 79 22 29 29 3b 0a  lator empty"));.
293de 0a 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20  .      /* Begin 
293df 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c  a loop that will
293e0 20 65 78 74 72 61 63 74 20 61 6c 6c 20 73 6f 75   extract all sou
293e1 72 63 65 20 72 6f 77 73 20 69 6e 20 47 52 4f 55  rce rows in GROU
293e2 50 20 42 59 20 6f 72 64 65 72 2e 0a 20 20 20 20  P BY order..    
293e3 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20    ** This might 
293e4 69 6e 76 6f 6c 76 65 20 74 77 6f 20 73 65 70 61  involve two sepa
293e5 72 61 74 65 20 6c 6f 6f 70 73 20 77 69 74 68 20  rate loops with 
293e6 61 6e 20 4f 50 5f 53 6f 72 74 20 69 6e 20 62 65  an OP_Sort in be
293e7 74 77 65 65 6e 2c 20 6f 72 0a 20 20 20 20 20 20  tween, or.      
293e8 2a 2a 20 69 74 20 6d 69 67 68 74 20 62 65 20 61  ** it might be a
293e9 20 73 69 6e 67 6c 65 20 6c 6f 6f 70 20 74 68 61   single loop tha
293ea 74 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78 20  t uses an index 
293eb 74 6f 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72  to extract infor
293ec 6d 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20  mation.      ** 
293ed 69 6e 20 74 68 65 20 72 69 67 68 74 20 6f 72 64  in the right ord
293ee 65 72 20 74 6f 20 62 65 67 69 6e 20 77 69 74 68  er to begin with
293ef 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
293f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
293f1 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
293f2 72 65 67 52 65 73 65 74 2c 20 61 64 64 72 52 65  regReset, addrRe
293f3 73 65 74 29 3b 0a 20 20 20 20 20 20 70 57 49 6e  set);.      pWIn
293f4 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72  fo = sqlite3Wher
293f5 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70  eBegin(pParse, p
293f6 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c  TabList, pWhere,
293f7 20 26 70 47 72 6f 75 70 42 79 2c 20 30 29 3b 0a   &pGroupBy, 0);.
293f8 20 20 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f        if( pWInfo
293f9 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63  ==0 ) goto selec
293fa 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 69 66 28  t_end;.      if(
293fb 20 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b 0a   pGroupBy==0 ){.
293fc 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
293fd 70 74 69 6d 69 7a 65 72 20 69 73 20 61 62 6c 65  ptimizer is able
293fe 20 74 6f 20 64 65 6c 69 76 65 72 20 72 6f 77 73   to deliver rows
293ff 20 69 6e 20 67 72 6f 75 70 20 62 79 20 6f 72 64   in group by ord
29400 65 72 20 73 6f 0a 20 20 20 20 20 20 20 20 2a 2a  er so.        **
29401 20 77 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20   we do not have 
29402 74 6f 20 73 6f 72 74 2e 20 20 54 68 65 20 4f 50  to sort.  The OP
29403 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 74  _OpenEphemeral t
29404 61 62 6c 65 20 77 69 6c 6c 20 62 65 0a 20 20 20  able will be.   
29405 20 20 20 20 20 2a 2a 20 63 61 6e 63 65 6c 6c 65       ** cancelle
29406 64 20 6c 61 74 65 72 20 62 65 63 61 75 73 65 20  d later because 
29407 77 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f  we still need to
29408 20 75 73 65 20 74 68 65 20 70 4b 65 79 49 6e 66   use the pKeyInf
29409 6f 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  o.        */.   
2940a 20 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20       pGroupBy = 
2940b 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20  p->pGroupBy;.   
2940c 20 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72 74       groupBySort
2940d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
2940e 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  e{.        /* Ro
2940f 77 73 20 61 72 65 20 63 6f 6d 69 6e 67 20 6f 75  ws are coming ou
29410 74 20 69 6e 20 75 6e 64 65 74 65 72 6d 69 6e 65  t in undetermine
29411 64 20 6f 72 64 65 72 2e 20 20 57 65 20 68 61 76  d order.  We hav
29412 65 20 74 6f 20 70 75 73 68 0a 20 20 20 20 20 20  e to push.      
29413 20 20 2a 2a 20 65 61 63 68 20 72 6f 77 20 69 6e    ** each row in
29414 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64  to a sorting ind
29415 65 78 2c 20 74 65 72 6d 69 6e 61 74 65 20 74 68  ex, terminate th
29416 65 20 66 69 72 73 74 20 6c 6f 6f 70 2c 0a 20 20  e first loop,.  
29417 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 6c 6f        ** then lo
29418 6f 70 20 6f 76 65 72 20 74 68 65 20 73 6f 72 74  op over the sort
29419 69 6e 67 20 69 6e 64 65 78 20 69 6e 20 6f 72 64  ing index in ord
2941a 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 6f 75  er to get the ou
2941b 74 70 75 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  tput.        ** 
2941c 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 0a  in sorted order.
2941d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2941e 20 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b 0a     int regBase;.
2941f 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 67 52          int regR
29420 65 63 6f 72 64 3b 0a 20 20 20 20 20 20 20 20 69  ecord;.        i
29421 6e 74 20 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20  nt nCol;.       
29422 20 69 6e 74 20 6e 47 72 6f 75 70 42 79 3b 0a 0a   int nGroupBy;..
29423 20 20 20 20 20 20 20 20 67 72 6f 75 70 42 79 53          groupByS
29424 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ort = 1;.       
29425 20 6e 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f   nGroupBy = pGro
29426 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  upBy->nExpr;.   
29427 20 20 20 20 20 6e 43 6f 6c 20 3d 20 6e 47 72 6f       nCol = nGro
29428 75 70 42 79 20 2b 20 31 3b 0a 20 20 20 20 20 20  upBy + 1;.      
29429 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31    j = nGroupBy+1
2942a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
2942b 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43  0; i<sAggInfo.nC
2942c 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
2942d 20 20 20 20 20 20 20 69 66 28 20 73 41 67 67 49         if( sAggI
2942e 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 2e 69 53 6f 72  nfo.aCol[i].iSor
2942f 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a  terColumn>=j ){.
29430 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f 6c              nCol
29431 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
29432 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  j++;.          }
29433 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29434 20 20 20 72 65 67 42 61 73 65 20 3d 20 73 71 6c     regBase = sql
29435 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
29436 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a  (pParse, nCol);.
29437 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
29438 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50  xprCacheClear(pP
29439 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73  arse);.        s
2943a 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78  qlite3ExprCodeEx
2943b 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
2943c 47 72 6f 75 70 42 79 2c 20 72 65 67 42 61 73 65  GroupBy, regBase
2943d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
2943e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2943f 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63 65 2c 20  v, OP_Sequence, 
29440 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
29441 49 64 78 2c 72 65 67 42 61 73 65 2b 6e 47 72 6f  Idx,regBase+nGro
29442 75 70 42 79 29 3b 0a 20 20 20 20 20 20 20 20 6a  upBy);.        j
29443 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a 20   = nGroupBy+1;. 
29444 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
29445 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75  i<sAggInfo.nColu
29446 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
29447 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e      struct AggIn
29448 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26  fo_col *pCol = &
29449 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d  sAggInfo.aCol[i]
2944a 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
2944b 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c  pCol->iSorterCol
2944c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 20  umn>=j ){.      
2944d 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 6a        int r1 = j
2944e 20 2b 20 72 65 67 42 61 73 65 3b 0a 20 20 20 20   + regBase;.    
2944f 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 3b 0a          int r2;.
29450 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 32 20  .            r2 
29451 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
29452 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73  eGetColumn(pPars
29453 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
29454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29455 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 2c 20 70     pCol->pTab, p
29456 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 43  Col->iColumn, pC
29457 6f 6c 2d 3e 69 54 61 62 6c 65 2c 20 72 31 2c 20  ol->iTable, r1, 
29458 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
29459 69 66 28 20 72 31 21 3d 72 32 20 29 7b 0a 20 20  if( r1!=r2 ){.  
2945a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2945b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2945c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 32 2c 20 72   OP_SCopy, r2, r
2945d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
2945e 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 2b  }.            j+
2945f 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  +;.          }. 
29460 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
29461 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c   regRecord = sql
29462 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
29463 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
29464 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29465 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
29466 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  rd, regBase, nCo
29467 6c 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  l, regRecord);. 
29468 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29469 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2946a 64 78 49 6e 73 65 72 74 2c 20 73 41 67 67 49 6e  dxInsert, sAggIn
2946b 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 72  fo.sortingIdx, r
2946c 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 20 20 20  egRecord);.     
2946d 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2946e 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2946f 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 20   regRecord);.   
29470 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
29471 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61  aseTempRange(pPa
29472 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43  rse, regBase, nC
29473 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ol);.        sql
29474 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49  ite3WhereEnd(pWI
29475 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 71  nfo);.        sq
29476 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29477 76 2c 20 4f 50 5f 53 6f 72 74 2c 20 73 41 67 67  v, OP_Sort, sAgg
29478 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c  Info.sortingIdx,
29479 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 20   addrEnd);.     
2947a 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2947b 76 2c 20 22 47 52 4f 55 50 20 42 59 20 73 6f 72  v, "GROUP BY sor
2947c 74 22 29 29 3b 0a 20 20 20 20 20 20 20 20 73 41  t"));.        sA
2947d 67 67 49 6e 66 6f 2e 75 73 65 53 6f 72 74 69 6e  ggInfo.useSortin
2947e 67 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 20 20  gIdx = 1;.      
2947f 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
29480 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
29481 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
29482 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20  /* Evaluate the 
29483 63 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59  current GROUP BY
29484 20 74 65 72 6d 73 20 61 6e 64 20 73 74 6f 72 65   terms and store
29485 20 69 6e 20 62 30 2c 20 62 31 2c 20 62 32 2e 2e   in b0, b1, b2..
29486 2e 0a 20 20 20 20 20 20 2a 2a 20 28 62 30 20 69  ..      ** (b0 i
29487 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  s memory locatio
29488 6e 20 69 42 4d 65 6d 2b 30 2c 20 62 31 20 69 73  n iBMem+0, b1 is
29489 20 69 42 4d 65 6d 2b 31 2c 20 61 6e 64 20 73 6f   iBMem+1, and so
2948a 20 66 6f 72 74 68 29 0a 20 20 20 20 20 20 2a 2a   forth).      **
2948b 20 54 68 65 6e 20 63 6f 6d 70 61 72 65 20 74 68   Then compare th
2948c 65 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50 20  e current GROUP 
2948d 42 59 20 74 65 72 6d 73 20 61 67 61 69 6e 73 74  BY terms against
2948e 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 74 65   the GROUP BY te
2948f 72 6d 73 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f  rms.      ** fro
29490 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 72  m the previous r
29491 6f 77 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f  ow currently sto
29492 72 65 64 20 69 6e 20 61 30 2c 20 61 31 2c 20 61  red in a0, a1, a
29493 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  2....      */.  
29494 20 20 20 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f      addrTopOfLoo
29495 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  p = sqlite3VdbeC
29496 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
29497 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
29498 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73  CacheClear(pPars
29499 65 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  e);.      for(j=
2949a 30 3b 20 6a 3c 70 47 72 6f 75 70 42 79 2d 3e 6e  0; j<pGroupBy->n
2949b 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  Expr; j++){.    
2949c 20 20 20 20 69 66 28 20 67 72 6f 75 70 42 79 53      if( groupByS
2949d 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ort ){.         
2949e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2949f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
294a0 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
294a1 67 49 64 78 2c 20 6a 2c 20 69 42 4d 65 6d 2b 6a  gIdx, j, iBMem+j
294a2 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
294a3 7b 0a 20 20 20 20 20 20 20 20 20 20 73 41 67 67  {.          sAgg
294a4 49 6e 66 6f 2e 64 69 72 65 63 74 4d 6f 64 65 20  Info.directMode 
294a5 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 73  = 1;.          s
294a6 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
294a7 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79 2d  Parse, pGroupBy-
294a8 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 42 4d  >a[j].pExpr, iBM
294a9 65 6d 2b 6a 29 3b 0a 20 20 20 20 20 20 20 20 7d  em+j);.        }
294aa 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
294ab 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
294ac 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20  (v, OP_Compare, 
294ad 69 41 4d 65 6d 2c 20 69 42 4d 65 6d 2c 20 70 47  iAMem, iBMem, pG
294ae 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2c 0a 20  roupBy->nExpr,. 
294af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
294b0 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
294b1 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59  pKeyInfo, P4_KEY
294b2 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 6a 31 20  INFO);.      j1 
294b3 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
294b4 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
294b5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
294b6 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c  dOp3(v, OP_Jump,
294b7 20 6a 31 2b 31 2c 20 30 2c 20 6a 31 2b 31 29 3b   j1+1, 0, j1+1);
294b8 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72  ..      /* Gener
294b9 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 72 75  ate code that ru
294ba 6e 73 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  ns whenever the 
294bb 47 52 4f 55 50 20 42 59 20 63 68 61 6e 67 65 73  GROUP BY changes
294bc 2e 0a 20 20 20 20 20 20 2a 2a 20 43 68 61 6e 67  ..      ** Chang
294bd 65 73 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20  es in the GROUP 
294be 42 59 20 61 72 65 20 64 65 74 65 63 74 65 64 20  BY are detected 
294bf 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  by the previous 
294c0 63 6f 64 65 0a 20 20 20 20 20 20 2a 2a 20 62 6c  code.      ** bl
294c1 6f 63 6b 2e 20 20 49 66 20 74 68 65 72 65 20 77  ock.  If there w
294c2 65 72 65 20 6e 6f 20 63 68 61 6e 67 65 73 2c 20  ere no changes, 
294c3 74 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 73 6b  this block is sk
294c4 69 70 70 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a  ipped..      **.
294c5 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 63 6f        ** This co
294c6 64 65 20 63 6f 70 69 65 73 20 63 75 72 72 65 6e  de copies curren
294c7 74 20 67 72 6f 75 70 20 62 79 20 74 65 72 6d 73  t group by terms
294c8 20 69 6e 20 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e   in b0,b1,b2,...
294c9 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 20 74  .      ** over t
294ca 6f 20 61 30 2c 61 31 2c 61 32 2e 20 20 49 74 20  o a0,a1,a2.  It 
294cb 74 68 65 6e 20 63 61 6c 6c 73 20 74 68 65 20 6f  then calls the o
294cc 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65  utput subroutine
294cd 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65  .      ** and re
294ce 73 65 74 73 20 74 68 65 20 61 67 67 72 65 67 61  sets the aggrega
294cf 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 72  te accumulator r
294d0 65 67 69 73 74 65 72 73 20 69 6e 20 70 72 65 70  egisters in prep
294d1 61 72 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  aration.      **
294d2 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 47 52   for the next GR
294d3 4f 55 50 20 42 59 20 62 61 74 63 68 2e 0a 20 20  OUP BY batch..  
294d4 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c      */.      sql
294d5 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
294d6 28 70 50 61 72 73 65 2c 20 69 42 4d 65 6d 2c 20  (pParse, iBMem, 
294d7 69 41 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d  iAMem, pGroupBy-
294d8 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 73  >nExpr);.      s
294d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
294da 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
294db 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72  gOutputRow, addr
294dc 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
294dd 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
294de 2c 20 22 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f  , "output one ro
294df 77 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  w"));.      sqli
294e0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
294e1 20 4f 50 5f 49 66 50 6f 73 2c 20 69 41 62 6f 72   OP_IfPos, iAbor
294e2 74 46 6c 61 67 2c 20 61 64 64 72 45 6e 64 29 3b  tFlag, addrEnd);
294e3 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
294e4 6e 74 28 28 76 2c 20 22 63 68 65 63 6b 20 61 62  nt((v, "check ab
294e5 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  ort flag"));.   
294e6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
294e7 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
294e8 2c 20 72 65 67 52 65 73 65 74 2c 20 61 64 64 72  , regReset, addr
294e9 52 65 73 65 74 29 3b 0a 20 20 20 20 20 20 56 64  Reset);.      Vd
294ea 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72  beComment((v, "r
294eb 65 73 65 74 20 61 63 63 75 6d 75 6c 61 74 6f 72  eset accumulator
294ec 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 55  "));..      /* U
294ed 70 64 61 74 65 20 74 68 65 20 61 67 67 72 65 67  pdate the aggreg
294ee 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 73  ate accumulators
294ef 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 6f   based on the co
294f0 6e 74 65 6e 74 20 6f 66 0a 20 20 20 20 20 20 2a  ntent of.      *
294f1 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  * the current ro
294f2 77 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  w.      */.     
294f3 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
294f4 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20  Here(v, j1);.   
294f5 20 20 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c     updateAccumul
294f6 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41  ator(pParse, &sA
294f7 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73  ggInfo);.      s
294f8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
294f9 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
294fa 31 2c 20 69 55 73 65 46 6c 61 67 29 3b 0a 20 20  1, iUseFlag);.  
294fb 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
294fc 28 76 2c 20 22 69 6e 64 69 63 61 74 65 20 64 61  (v, "indicate da
294fd 74 61 20 69 6e 20 61 63 63 75 6d 75 6c 61 74 6f  ta in accumulato
294fe 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  r"));..      /* 
294ff 45 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a  End of the loop.
29500 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
29501 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74 20 29  f( groupBySort )
29502 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
29503 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29504 50 5f 4e 65 78 74 2c 20 73 41 67 67 49 6e 66 6f  P_Next, sAggInfo
29505 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64  .sortingIdx, add
29506 72 54 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20  rTopOfLoop);.   
29507 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29508 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e    sqlite3WhereEn
29509 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20  d(pWInfo);.     
2950a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2950b 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64  angeToNoop(v, ad
2950c 64 72 53 6f 72 74 69 6e 67 49 64 78 2c 20 31 29  drSortingIdx, 1)
2950d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
2950e 20 2f 2a 20 4f 75 74 70 75 74 20 74 68 65 20 66   /* Output the f
2950f 69 6e 61 6c 20 72 6f 77 20 6f 66 20 72 65 73 75  inal row of resu
29510 6c 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  lt.      */.    
29511 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29512 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
29513 20 72 65 67 4f 75 74 70 75 74 52 6f 77 2c 20 61   regOutputRow, a
29514 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20  ddrOutputRow);. 
29515 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
29516 28 28 76 2c 20 22 6f 75 74 70 75 74 20 66 69 6e  ((v, "output fin
29517 61 6c 20 72 6f 77 22 29 29 3b 0a 0a 20 20 20 20  al row"));..    
29518 20 20 2f 2a 20 4a 75 6d 70 20 6f 76 65 72 20 74    /* Jump over t
29519 68 65 20 73 75 62 72 6f 75 74 69 6e 65 73 0a 20  he subroutines. 
2951a 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2951b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2951c 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
2951d 64 64 72 45 6e 64 29 3b 0a 0a 20 20 20 20 20 20  ddrEnd);..      
2951e 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 75  /* Generate a su
2951f 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6f 75  broutine that ou
29520 74 70 75 74 73 20 61 20 73 69 6e 67 6c 65 20 72  tputs a single r
29521 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
29522 0a 20 20 20 20 20 20 2a 2a 20 73 65 74 2e 20 20  .      ** set.  
29523 54 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 20  This subroutine 
29524 66 69 72 73 74 20 6c 6f 6f 6b 73 20 61 74 20 74  first looks at t
29525 68 65 20 69 55 73 65 46 6c 61 67 2e 20 20 49 66  he iUseFlag.  If
29526 20 69 55 73 65 46 6c 61 67 0a 20 20 20 20 20 20   iUseFlag.      
29527 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  ** is less than 
29528 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f  or equal to zero
29529 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65  , the subroutine
2952a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66   is a no-op.  If
2952b 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72  .      ** the pr
2952c 6f 63 65 73 73 69 6e 67 20 63 61 6c 6c 73 20 66  ocessing calls f
2952d 6f 72 20 74 68 65 20 71 75 65 72 79 20 74 6f 20  or the query to 
2952e 61 62 6f 72 74 2c 20 74 68 69 73 20 73 75 62 72  abort, this subr
2952f 6f 75 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20  outine.      ** 
29530 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 69  increments the i
29531 41 62 6f 72 74 46 6c 61 67 20 6d 65 6d 6f 72 79  AbortFlag memory
29532 20 6c 6f 63 61 74 69 6f 6e 20 62 65 66 6f 72 65   location before
29533 20 72 65 74 75 72 6e 69 6e 67 20 69 6e 0a 20 20   returning in.  
29534 20 20 20 20 2a 2a 20 6f 72 64 65 72 20 74 6f 20      ** order to 
29535 73 69 67 6e 61 6c 20 74 68 65 20 63 61 6c 6c 65  signal the calle
29536 72 20 74 6f 20 61 62 6f 72 74 2e 0a 20 20 20 20  r to abort..    
29537 20 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 53    */.      addrS
29538 65 74 41 62 6f 72 74 20 3d 20 73 71 6c 69 74 65  etAbort = sqlite
29539 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2953a 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  (v);.      sqlit
2953b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2953c 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69  OP_Integer, 1, i
2953d 41 62 6f 72 74 46 6c 61 67 29 3b 0a 20 20 20 20  AbortFlag);.    
2953e 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2953f 2c 20 22 73 65 74 20 61 62 6f 72 74 20 66 6c 61  , "set abort fla
29540 67 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  g"));.      sqli
29541 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
29542 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f   OP_Return, regO
29543 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20  utputRow);.     
29544 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
29545 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
29546 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
29547 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 20    addrOutputRow 
29548 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
29549 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
2954a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2954b 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73  dOp2(v, OP_IfPos
2954c 2c 20 69 55 73 65 46 6c 61 67 2c 20 61 64 64 72  , iUseFlag, addr
2954d 4f 75 74 70 75 74 52 6f 77 2b 32 29 3b 0a 20 20  OutputRow+2);.  
2954e 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2954f 28 76 2c 20 22 47 72 6f 75 70 62 79 20 72 65 73  (v, "Groupby res
29550 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 20 65 6e  ult generator en
29551 74 72 79 20 70 6f 69 6e 74 22 29 29 3b 0a 20 20  try point"));.  
29552 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29553 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
29554 72 6e 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77  rn, regOutputRow
29555 29 3b 0a 20 20 20 20 20 20 66 69 6e 61 6c 69 7a  );.      finaliz
29556 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28 70 50  eAggFunctions(pP
29557 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29  arse, &sAggInfo)
29558 3b 0a 20 20 20 20 20 20 69 66 28 20 70 48 61 76  ;.      if( pHav
29559 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ing ){.        s
2955a 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
2955b 65 28 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e  e(pParse, pHavin
2955c 67 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  g, addrOutputRow
2955d 2b 31 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  +1, SQLITE_JUMPI
2955e 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a  FNULL);.      }.
2955f 20 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65        selectInne
29560 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c  rLoop(pParse, p,
29561 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30   p->pEList, 0, 0
29562 2c 20 70 4f 72 64 65 72 42 79 2c 0a 20 20 20 20  , pOrderBy,.    
29563 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29564 20 20 64 69 73 74 69 6e 63 74 2c 20 70 44 65 73    distinct, pDes
29565 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
29566 20 20 20 20 20 20 20 20 20 61 64 64 72 4f 75 74           addrOut
29567 70 75 74 52 6f 77 2b 31 2c 20 61 64 64 72 53 65  putRow+1, addrSe
29568 74 41 62 6f 72 74 29 3b 0a 20 20 20 20 20 20 73  tAbort);.      s
29569 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2956a 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72  (v, OP_Return, r
2956b 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20  egOutputRow);.  
2956c 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2956d 28 76 2c 20 22 65 6e 64 20 67 72 6f 75 70 62 79  (v, "end groupby
2956e 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74 6f   result generato
2956f 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  r"));..      /* 
29570 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f  Generate a subro
29571 75 74 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20  utine that will 
29572 72 65 73 65 74 20 74 68 65 20 67 72 6f 75 70 2d  reset the group-
29573 62 79 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a 20  by accumulator. 
29574 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
29575 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
29576 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 52 65 73  Label(v, addrRes
29577 65 74 29 3b 0a 20 20 20 20 20 20 72 65 73 65 74  et);.      reset
29578 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72  Accumulator(pPar
29579 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
2957a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2957b 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
2957c 74 75 72 6e 2c 20 72 65 67 52 65 73 65 74 29 3b  turn, regReset);
2957d 0a 20 20 20 20 20 0a 20 20 20 20 7d 20 2f 2a 20  .     .    } /* 
2957e 65 6e 64 69 66 20 70 47 72 6f 75 70 42 79 20 2a  endif pGroupBy *
2957f 2f 0a 20 20 20 20 65 6c 73 65 20 7b 0a 20 20 20  /.    else {.   
29580 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 44 65     ExprList *pDe
29581 6c 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53  l = 0;.#ifndef S
29582 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45  QLITE_OMIT_BTREE
29583 43 4f 55 4e 54 0a 20 20 20 20 20 20 54 61 62 6c  COUNT.      Tabl
29584 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 69  e *pTab;.      i
29585 66 28 20 28 70 54 61 62 20 3d 20 69 73 53 69 6d  f( (pTab = isSim
29586 70 6c 65 43 6f 75 6e 74 28 70 2c 20 26 73 41 67  pleCount(p, &sAg
29587 67 49 6e 66 6f 29 29 21 3d 30 20 29 7b 0a 20 20  gInfo))!=0 ){.  
29588 20 20 20 20 20 20 2f 2a 20 49 66 20 69 73 53 69        /* If isSi
29589 6d 70 6c 65 43 6f 75 6e 74 28 29 20 72 65 74 75  mpleCount() retu
2958a 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
2958b 20 61 20 54 61 62 6c 65 20 73 74 72 75 63 74 75   a Table structu
2958c 72 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 20  re, then.       
2958d 20 2a 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74   ** the SQL stat
2958e 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20  ement is of the 
2958f 66 6f 72 6d 3a 0a 20 20 20 20 20 20 20 20 2a 2a  form:.        **
29590 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45  .        **   SE
29591 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52  LECT count(*) FR
29592 4f 4d 20 3c 74 62 6c 3e 0a 20 20 20 20 20 20 20  OM <tbl>.       
29593 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 77   **.        ** w
29594 68 65 72 65 20 74 68 65 20 54 61 62 6c 65 20 73  here the Table s
29595 74 72 75 63 74 75 72 65 20 72 65 74 75 72 6e 65  tructure returne
29596 64 20 72 65 70 72 65 73 65 6e 74 73 20 74 61 62  d represents tab
29597 6c 65 20 3c 74 62 6c 3e 2e 0a 20 20 20 20 20 20  le <tbl>..      
29598 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
29599 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69  This statement i
2959a 73 20 73 6f 20 63 6f 6d 6d 6f 6e 20 74 68 61 74  s so common that
2959b 20 69 74 20 69 73 20 6f 70 74 69 6d 69 7a 65 64   it is optimized
2959c 20 73 70 65 63 69 61 6c 6c 79 2e 20 54 68 65 0a   specially. The.
2959d 20 20 20 20 20 20 20 20 2a 2a 20 4f 50 5f 43 6f          ** OP_Co
2959e 75 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  unt instruction 
2959f 69 73 20 65 78 65 63 75 74 65 64 20 65 69 74 68  is executed eith
295a0 65 72 20 6f 6e 20 74 68 65 20 69 6e 74 6b 65 79  er on the intkey
295a1 20 74 61 62 6c 65 20 74 68 61 74 0a 20 20 20 20   table that.    
295a2 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20      ** contains 
295a3 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 61 62  the data for tab
295a4 6c 65 20 3c 74 62 6c 3e 20 6f 72 20 6f 6e 20 6f  le <tbl> or on o
295a5 6e 65 20 6f 66 20 69 74 73 20 69 6e 64 65 78 65  ne of its indexe
295a6 73 2e 20 49 74 0a 20 20 20 20 20 20 20 20 2a 2a  s. It.        **
295a7 20 69 73 20 62 65 74 74 65 72 20 74 6f 20 65 78   is better to ex
295a8 65 63 75 74 65 20 74 68 65 20 6f 70 20 6f 6e 20  ecute the op on 
295a9 61 6e 20 69 6e 64 65 78 2c 20 61 73 20 69 6e 64  an index, as ind
295aa 65 78 65 73 20 61 72 65 20 61 6c 6d 6f 73 74 0a  exes are almost.
295ab 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79          ** alway
295ac 73 20 73 70 72 65 61 64 20 61 63 72 6f 73 73 20  s spread across 
295ad 6c 65 73 73 20 70 61 67 65 73 20 74 68 61 6e 20  less pages than 
295ae 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64  their correspond
295af 69 6e 67 20 74 61 62 6c 65 73 2e 0a 20 20 20 20  ing tables..    
295b0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63      */.        c
295b1 6f 6e 73 74 20 69 6e 74 20 69 44 62 20 3d 20 73  onst int iDb = s
295b2 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
295b3 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
295b4 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
295b5 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e          const in
295b6 74 20 69 43 73 72 20 3d 20 70 50 61 72 73 65 2d  t iCsr = pParse-
295b7 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20  >nTab++;     /* 
295b8 43 75 72 73 6f 72 20 74 6f 20 73 63 61 6e 20 62  Cursor to scan b
295b9 2d 74 72 65 65 20 2a 2f 0a 20 20 20 20 20 20 20  -tree */.       
295ba 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
295bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
295bc 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
295bd 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
295be 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
295bf 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 20 20 20 20  KeyInfo = 0;    
295c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65             /* Ke
295c1 79 69 6e 66 6f 20 66 6f 72 20 73 63 61 6e 6e 65  yinfo for scanne
295c2 64 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20  d index */.     
295c3 20 20 20 49 6e 64 65 78 20 2a 70 42 65 73 74 20     Index *pBest 
295c4 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
295c5 20 20 20 20 20 20 20 20 2f 2a 20 42 65 73 74 20          /* Best 
295c6 69 6e 64 65 78 20 66 6f 75 6e 64 20 73 6f 20 66  index found so f
295c7 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  ar */.        in
295c8 74 20 69 52 6f 6f 74 20 3d 20 70 54 61 62 2d 3e  t iRoot = pTab->
295c9 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  tnum;           
295ca 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20     /* Root page 
295cb 6f 66 20 73 63 61 6e 6e 65 64 20 62 2d 74 72 65  of scanned b-tre
295cc 65 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 73 71  e */..        sq
295cd 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
295ce 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44  chema(pParse, iD
295cf 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  b);.        sqli
295d0 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61  te3TableLock(pPa
295d1 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e  rse, iDb, pTab->
295d2 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a  tnum, 0, pTab->z
295d3 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 20 20  Name);..        
295d4 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 74 68  /* Search for th
295d5 65 20 69 6e 64 65 78 20 74 68 61 74 20 68 61 73  e index that has
295d6 20 74 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e   the least amoun
295d7 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 49 66  t of columns. If
295d8 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 72  .        ** ther
295d9 65 20 69 73 20 73 75 63 68 20 61 6e 20 69 6e 64  e is such an ind
295da 65 78 2c 20 61 6e 64 20 69 74 20 68 61 73 20 6c  ex, and it has l
295db 65 73 73 20 63 6f 6c 75 6d 6e 73 20 74 68 61 6e  ess columns than
295dc 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 20   the table.     
295dd 20 20 20 2a 2a 20 64 6f 65 73 2c 20 74 68 65 6e     ** does, then
295de 20 77 65 20 63 61 6e 20 61 73 73 75 6d 65 20 74   we can assume t
295df 68 61 74 20 69 74 20 63 6f 6e 73 75 6d 65 73 20  hat it consumes 
295e0 6c 65 73 73 20 73 70 61 63 65 20 6f 6e 20 64 69  less space on di
295e1 73 6b 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  sk and.        *
295e2 2a 20 77 69 6c 6c 20 74 68 65 72 65 66 6f 72 65  * will therefore
295e3 20 62 65 20 63 68 65 61 70 65 72 20 74 6f 20 73   be cheaper to s
295e4 63 61 6e 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  can to determine
295e5 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75 6c   the query resul
295e6 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e  t..        ** In
295e7 20 74 68 69 73 20 63 61 73 65 20 73 65 74 20 69   this case set i
295e8 52 6f 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f 74  Root to the root
295e9 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
295ea 74 68 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65  the index b-tree
295eb 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  .        ** and 
295ec 70 4b 65 79 49 6e 66 6f 20 74 6f 20 74 68 65 20  pKeyInfo to the 
295ed 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
295ee 65 20 72 65 71 75 69 72 65 64 20 74 6f 20 6e 61  e required to na
295ef 76 69 67 61 74 65 20 74 68 65 0a 20 20 20 20 20  vigate the.     
295f0 20 20 20 2a 2a 20 69 6e 64 65 78 2e 0a 20 20 20     ** index..   
295f1 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
295f2 2a 2a 20 49 6e 20 70 72 61 63 74 69 63 65 20 74  ** In practice t
295f3 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63  he KeyInfo struc
295f4 74 75 72 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65  ture will not be
295f5 20 75 73 65 64 2e 20 49 74 20 69 73 20 6f 6e 6c   used. It is onl
295f6 79 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61  y .        ** pa
295f7 73 73 65 64 20 74 6f 20 6b 65 65 70 20 4f 50 5f  ssed to keep OP_
295f8 4f 70 65 6e 52 65 61 64 20 68 61 70 70 79 2e 0a  OpenRead happy..
295f9 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
295fa 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62     for(pIdx=pTab
295fb 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
295fc 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
295fd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
295fe 20 21 70 42 65 73 74 20 7c 7c 20 70 49 64 78 2d   !pBest || pIdx-
295ff 3e 6e 43 6f 6c 75 6d 6e 3c 70 42 65 73 74 2d 3e  >nColumn<pBest->
29600 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  nColumn ){.     
29601 20 20 20 20 20 20 20 70 42 65 73 74 20 3d 20 70         pBest = p
29602 49 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  Idx;.          }
29603 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29604 20 20 20 69 66 28 20 70 42 65 73 74 20 26 26 20     if( pBest && 
29605 70 42 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70  pBest->nColumn<p
29606 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
29607 20 20 20 20 20 20 20 69 52 6f 6f 74 20 3d 20 70         iRoot = p
29608 42 65 73 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20  Best->tnum;.    
29609 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d        pKeyInfo =
2960a 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
2960b 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 42 65  info(pParse, pBe
2960c 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  st);.        }..
2960d 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20          /* Open 
2960e 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73  a read-only curs
2960f 6f 72 2c 20 65 78 65 63 75 74 65 20 74 68 65 20  or, execute the 
29610 4f 50 5f 43 6f 75 6e 74 2c 20 63 6c 6f 73 65 20  OP_Count, close 
29611 74 68 65 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20  the cursor. */. 
29612 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29613 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f  beAddOp3(v, OP_O
29614 70 65 6e 52 65 61 64 2c 20 69 43 73 72 2c 20 69  penRead, iCsr, i
29615 52 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20  Root, iDb);.    
29616 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f      if( pKeyInfo
29617 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
29618 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29619 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a  4(v, -1, (char *
2961a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45  )pKeyInfo, P4_KE
2961b 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
2961c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2961d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2961e 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 75 6e 74 2c  Op2(v, OP_Count,
2961f 20 69 43 73 72 2c 20 73 41 67 67 49 6e 66 6f 2e   iCsr, sAggInfo.
29620 61 46 75 6e 63 5b 30 5d 2e 69 4d 65 6d 29 3b 0a  aFunc[0].iMem);.
29621 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29622 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
29623 43 6c 6f 73 65 2c 20 69 43 73 72 29 3b 0a 20 20  Close, iCsr);.  
29624 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
29625 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
29626 42 54 52 45 45 43 4f 55 4e 54 20 2a 2f 0a 20 20  BTREECOUNT */.  
29627 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a      {.        /*
29628 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 71 75   Check if the qu
29629 65 72 79 20 69 73 20 6f 66 20 6f 6e 65 20 6f 66  ery is of one of
2962a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
2962b 6f 72 6d 73 3a 0a 20 20 20 20 20 20 20 20 2a 2a  orms:.        **
2962c 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45  .        **   SE
2962d 4c 45 43 54 20 6d 69 6e 28 78 29 20 46 52 4f 4d  LECT min(x) FROM
2962e 20 2e 2e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20   ....        ** 
2962f 20 20 53 45 4c 45 43 54 20 6d 61 78 28 78 29 20    SELECT max(x) 
29630 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 20  FROM ....       
29631 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49   **.        ** I
29632 66 20 69 74 20 69 73 2c 20 74 68 65 6e 20 61 73  f it is, then as
29633 6b 20 74 68 65 20 63 6f 64 65 20 69 6e 20 77 68  k the code in wh
29634 65 72 65 2e 63 20 74 6f 20 61 74 74 65 6d 70 74  ere.c to attempt
29635 20 74 6f 20 73 6f 72 74 20 72 65 73 75 6c 74 73   to sort results
29636 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73 20 69  .        ** as i
29637 66 20 74 68 65 72 65 20 77 61 73 20 61 6e 20 22  f there was an "
29638 4f 52 44 45 52 20 4f 4e 20 78 22 20 6f 72 20 22  ORDER ON x" or "
29639 4f 52 44 45 52 20 4f 4e 20 78 20 44 45 53 43 22  ORDER ON x DESC"
2963a 20 63 6c 61 75 73 65 2e 20 0a 20 20 20 20 20 20   clause. .      
2963b 20 20 2a 2a 20 49 66 20 77 68 65 72 65 2e 63 20    ** If where.c 
2963c 69 73 20 61 62 6c 65 20 74 6f 20 70 72 6f 64 75  is able to produ
2963d 63 65 20 72 65 73 75 6c 74 73 20 73 6f 72 74 65  ce results sorte
2963e 64 20 69 6e 20 74 68 69 73 20 6f 72 64 65 72 2c  d in this order,
2963f 20 74 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a   then.        **
29640 20 61 64 64 20 76 64 62 65 20 63 6f 64 65 20 74   add vdbe code t
29641 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74  o break out of t
29642 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f  he processing lo
29643 6f 70 20 61 66 74 65 72 20 74 68 65 20 0a 20 20  op after the .  
29644 20 20 20 20 20 20 2a 2a 20 66 69 72 73 74 20 69        ** first i
29645 74 65 72 61 74 69 6f 6e 20 28 73 69 6e 63 65 20  teration (since 
29646 74 68 65 20 66 69 72 73 74 20 69 74 65 72 61 74  the first iterat
29647 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  ion of the loop 
29648 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 67  is .        ** g
29649 75 61 72 61 6e 74 65 65 64 20 74 6f 20 6f 70 65  uaranteed to ope
2964a 72 61 74 65 20 6f 6e 20 74 68 65 20 72 6f 77 20  rate on the row 
2964b 77 69 74 68 20 74 68 65 20 6d 69 6e 69 6d 75 6d  with the minimum
2964c 20 6f 72 20 6d 61 78 69 6d 75 6d 20 0a 20 20 20   or maximum .   
2964d 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66       ** value of
2964e 20 78 2c 20 74 68 65 20 6f 6e 6c 79 20 72 6f 77   x, the only row
2964f 20 72 65 71 75 69 72 65 64 29 2e 0a 20 20 20 20   required)..    
29650 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
29651 2a 20 41 20 73 70 65 63 69 61 6c 20 66 6c 61 67  * A special flag
29652 20 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 20   must be passed 
29653 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  to sqlite3WhereB
29654 65 67 69 6e 28 29 20 74 6f 20 73 6c 69 67 68 74  egin() to slight
29655 6c 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f  ly.        ** mo
29656 64 69 66 79 20 62 65 68 61 76 69 6f 75 72 20 61  dify behaviour a
29657 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 20  s follows:.     
29658 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
29659 20 20 20 2b 20 49 66 20 74 68 65 20 71 75 65 72     + If the quer
2965a 79 20 69 73 20 61 20 22 53 45 4c 45 43 54 20 6d  y is a "SELECT m
2965b 69 6e 28 78 29 22 2c 20 74 68 65 6e 20 74 68 65  in(x)", then the
2965c 20 6c 6f 6f 70 20 63 6f 64 65 64 20 62 79 0a 20   loop coded by. 
2965d 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 77 68         **     wh
2965e 65 72 65 2e 63 20 73 68 6f 75 6c 64 20 6e 6f 74  ere.c should not
2965f 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6e   iterate over an
29660 79 20 76 61 6c 75 65 73 20 77 69 74 68 20 61 20  y values with a 
29661 4e 55 4c 4c 20 76 61 6c 75 65 0a 20 20 20 20 20  NULL value.     
29662 20 20 20 2a 2a 20 20 20 20 20 66 6f 72 20 78 2e     **     for x.
29663 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
29664 20 20 20 20 2a 2a 20 20 20 2b 20 54 68 65 20 6f      **   + The o
29665 70 74 69 6d 69 7a 65 72 20 63 6f 64 65 20 69 6e  ptimizer code in
29666 20 77 68 65 72 65 2e 63 20 28 74 68 65 20 74 68   where.c (the th
29667 69 6e 67 20 74 68 61 74 20 64 65 63 69 64 65 73  ing that decides
29668 20 77 68 69 63 68 0a 20 20 20 20 20 20 20 20 2a   which.        *
29669 2a 20 20 20 20 20 69 6e 64 65 78 20 6f 72 20 69  *     index or i
2966a 6e 64 69 63 65 73 20 74 6f 20 75 73 65 29 20 73  ndices to use) s
2966b 68 6f 75 6c 64 20 70 6c 61 63 65 20 61 20 64 69  hould place a di
2966c 66 66 65 72 65 6e 74 20 70 72 69 6f 72 69 74 79  fferent priority
2966d 20 6f 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20   on .        ** 
2966e 20 20 20 20 73 61 74 69 73 66 79 69 6e 67 20 74      satisfying t
2966f 68 65 20 27 4f 52 44 45 52 20 42 59 27 20 63 6c  he 'ORDER BY' cl
29670 61 75 73 65 20 74 68 61 6e 20 69 74 20 64 6f 65  ause than it doe
29671 73 20 69 6e 20 6f 74 68 65 72 20 63 61 73 65 73  s in other cases
29672 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20  ..        **    
29673 20 52 65 66 65 72 20 74 6f 20 63 6f 64 65 20 61   Refer to code a
29674 6e 64 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 77  nd comments in w
29675 68 65 72 65 2e 63 20 66 6f 72 20 64 65 74 61 69  here.c for detai
29676 6c 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ls..        */. 
29677 20 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20         ExprList 
29678 2a 70 4d 69 6e 4d 61 78 20 3d 20 30 3b 0a 20 20  *pMinMax = 0;.  
29679 20 20 20 20 20 20 75 38 20 66 6c 61 67 20 3d 20        u8 flag = 
2967a 6d 69 6e 4d 61 78 51 75 65 72 79 28 70 29 3b 0a  minMaxQuery(p);.
2967b 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
2967c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73   ){.          as
2967d 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
2967e 6f 70 65 72 74 79 28 70 2d 3e 70 45 4c 69 73 74  operty(p->pEList
2967f 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 45 50  ->a[0].pExpr, EP
29680 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20  _xIsSelect) );. 
29681 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78           pMinMax
29682 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
29683 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c  stDup(db, p->pEL
29684 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d  ist->a[0].pExpr-
29685 3e 78 2e 70 4c 69 73 74 2c 30 29 3b 0a 20 20 20  >x.pList,0);.   
29686 20 20 20 20 20 20 20 70 44 65 6c 20 3d 20 70 4d         pDel = pM
29687 69 6e 4d 61 78 3b 0a 20 20 20 20 20 20 20 20 20  inMax;.         
29688 20 69 66 28 20 70 4d 69 6e 4d 61 78 20 26 26 20   if( pMinMax && 
29689 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
2968a 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  d ){.           
2968b 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 73   pMinMax->a[0].s
2968c 6f 72 74 4f 72 64 65 72 20 3d 20 66 6c 61 67 21  ortOrder = flag!
2968d 3d 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d  =WHERE_ORDERBY_M
2968e 49 4e 20 3f 31 3a 30 3b 0a 20 20 20 20 20 20 20  IN ?1:0;.       
2968f 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b       pMinMax->a[
29690 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54  0].pExpr->op = T
29691 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20  K_COLUMN;.      
29692 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
29693 20 20 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68    .        /* Th
29694 69 73 20 63 61 73 65 20 72 75 6e 73 20 69 66 20  is case runs if 
29695 74 68 65 20 61 67 67 72 65 67 61 74 65 20 68 61  the aggregate ha
29696 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c  s no GROUP BY cl
29697 61 75 73 65 2e 20 20 54 68 65 0a 20 20 20 20 20  ause.  The.     
29698 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67     ** processing
29699 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c 65 72   is much simpler
2969a 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 20   since there is 
2969b 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f  only a single ro
2969c 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20  w.        ** of 
2969d 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20 20 20  output..        
2969e 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 74  */.        reset
2969f 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72  Accumulator(pPar
296a0 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
296a1 20 20 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d          pWInfo =
296a2 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
296a3 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
296a4 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70 4d  ist, pWhere, &pM
296a5 69 6e 4d 61 78 2c 20 66 6c 61 67 29 3b 0a 20 20  inMax, flag);.  
296a6 20 20 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f        if( pWInfo
296a7 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
296a8 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
296a9 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 29  Delete(db, pDel)
296aa 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
296ab 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20   select_end;.   
296ac 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 75       }.        u
296ad 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72  pdateAccumulator
296ae 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
296af 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  fo);.        if(
296b0 20 21 70 4d 69 6e 4d 61 78 20 26 26 20 66 6c 61   !pMinMax && fla
296b1 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  g ){.          s
296b2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
296b3 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
296b4 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29 3b  pWInfo->iBreak);
296b5 0a 20 20 20 20 20 20 20 20 20 20 56 64 62 65 43  .          VdbeC
296b6 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 28 29  omment((v, "%s()
296b7 20 62 79 20 69 6e 64 65 78 22 2c 0a 20 20 20 20   by index",.    
296b8 20 20 20 20 20 20 20 20 20 20 20 20 28 66 6c 61              (fla
296b9 67 3d 3d 57 48 45 52 45 5f 4f 52 44 45 52 42 59  g==WHERE_ORDERBY
296ba 5f 4d 49 4e 3f 22 6d 69 6e 22 3a 22 6d 61 78 22  _MIN?"min":"max"
296bb 29 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  )));.        }. 
296bc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
296bd 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
296be 20 20 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65          finalize
296bf 41 67 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61  AggFunctions(pPa
296c0 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b  rse, &sAggInfo);
296c1 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
296c2 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  pOrderBy = 0;.  
296c3 20 20 20 20 69 66 28 20 70 48 61 76 69 6e 67 20      if( pHaving 
296c4 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
296c5 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
296c6 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c 20 61  arse, pHaving, a
296c7 64 64 72 45 6e 64 2c 20 53 51 4c 49 54 45 5f 4a  ddrEnd, SQLITE_J
296c8 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
296c9 20 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63 74    }.      select
296ca 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65  InnerLoop(pParse
296cb 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20  , p, p->pEList, 
296cc 30 2c 20 30 2c 20 30 2c 20 2d 31 2c 20 0a 20 20  0, 0, 0, -1, .  
296cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
296ce 20 20 20 20 70 44 65 73 74 2c 20 61 64 64 72 45      pDest, addrE
296cf 6e 64 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20  nd, addrEnd);.  
296d0 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c      sqlite3ExprL
296d1 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44  istDelete(db, pD
296d2 65 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  el);.    }.    s
296d3 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
296d4 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 45 6e  eLabel(v, addrEn
296d5 64 29 3b 0a 20 20 20 20 0a 20 20 7d 20 2f 2a 20  d);.    .  } /* 
296d6 65 6e 64 69 66 20 61 67 67 72 65 67 61 74 65 20  endif aggregate 
296d7 71 75 65 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20 49  query */..  /* I
296d8 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52  f there is an OR
296d9 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74  DER BY clause, t
296da 68 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 73  hen we need to s
296db 6f 72 74 20 74 68 65 20 72 65 73 75 6c 74 73 0a  ort the results.
296dc 20 20 2a 2a 20 61 6e 64 20 73 65 6e 64 20 74 68    ** and send th
296dd 65 6d 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  em to the callba
296de 63 6b 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20  ck one by one.. 
296df 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72   */.  if( pOrder
296e0 42 79 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61  By ){.    genera
296e1 74 65 53 6f 72 74 54 61 69 6c 28 70 50 61 72 73  teSortTail(pPars
296e2 65 2c 20 70 2c 20 76 2c 20 70 45 4c 69 73 74 2d  e, p, v, pEList-
296e3 3e 6e 45 78 70 72 2c 20 70 44 65 73 74 29 3b 0a  >nExpr, pDest);.
296e4 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68    }..  /* Jump h
296e5 65 72 65 20 74 6f 20 73 6b 69 70 20 74 68 69 73  ere to skip this
296e6 20 71 75 65 72 79 0a 20 20 2a 2f 0a 20 20 73 71   query.  */.  sq
296e7 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
296e8 4c 61 62 65 6c 28 76 2c 20 69 45 6e 64 29 3b 0a  Label(v, iEnd);.
296e9 0a 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54  .  /* The SELECT
296ea 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c   was successfull
296eb 79 20 63 6f 64 65 64 2e 20 20 20 53 65 74 20 74  y coded.   Set t
296ec 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74  he return code t
296ed 6f 20 30 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69  o 0.  ** to indi
296ee 63 61 74 65 20 6e 6f 20 65 72 72 6f 72 73 2e 0a  cate no errors..
296ef 20 20 2a 2f 0a 20 20 72 63 20 3d 20 30 3b 0a 0a    */.  rc = 0;..
296f0 20 20 2f 2a 20 43 6f 6e 74 72 6f 6c 20 6a 75 6d    /* Control jum
296f1 70 73 20 74 6f 20 68 65 72 65 20 69 66 20 61 6e  ps to here if an
296f2 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
296f3 74 65 72 65 64 20 61 62 6f 76 65 2c 20 6f 72 20  tered above, or 
296f4 75 70 6f 6e 0a 20 20 2a 2a 20 73 75 63 63 65 73  upon.  ** succes
296f5 73 66 75 6c 20 63 6f 64 69 6e 67 20 6f 66 20 74  sful coding of t
296f6 68 65 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2f 0a  he SELECT..  */.
296f7 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 0a 20 20 2f  select_end:..  /
296f8 2a 20 49 64 65 6e 74 69 66 79 20 63 6f 6c 75 6d  * Identify colum
296f9 6e 20 6e 61 6d 65 73 20 69 66 20 72 65 73 75 6c  n names if resul
296fa 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ts of the SELECT
296fb 20 61 72 65 20 74 6f 20 62 65 20 6f 75 74 70 75   are to be outpu
296fc 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  t..  */.  if( rc
296fd 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
296fe 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54  Dest->eDest==SRT
296ff 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 67  _Output ){.    g
29700 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d  enerateColumnNam
29701 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  es(pParse, pTabL
29702 69 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ist, pEList);.  
29703 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  }..  sqlite3DbFr
29704 65 65 28 64 62 2c 20 73 41 67 67 49 6e 66 6f 2e  ee(db, sAggInfo.
29705 61 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33  aCol);.  sqlite3
29706 44 62 46 72 65 65 28 64 62 2c 20 73 41 67 67 49  DbFree(db, sAggI
29707 6e 66 6f 2e 61 46 75 6e 63 29 3b 0a 20 20 72 65  nfo.aFunc);.  re
29708 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20  turn rc;.}..#if 
29709 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
2970a 45 42 55 47 29 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a  EBUG)./*.*******
2970b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2970c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2970d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2970e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2970f 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
29710 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 69  following code i
29711 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
29712 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
29713 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 64 65   only.  The code
29714 0a 2a 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  .** that follows
29715 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 65 61 72   does not appear
29716 20 69 6e 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64   in normal build
29717 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  s..**.** These r
29718 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
29719 20 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 74 68   to print out th
2971a 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6c 6c  e content of all
2971b 20 6f 72 20 70 61 72 74 20 6f 66 20 61 20 0a 2a   or part of a .*
2971c 2a 20 70 61 72 73 65 20 73 74 72 75 63 74 75 72  * parse structur
2971d 65 73 20 73 75 63 68 20 61 73 20 53 65 6c 65 63  es such as Selec
2971e 74 20 6f 72 20 45 78 70 72 2e 20 20 53 75 63 68  t or Expr.  Such
2971f 20 70 72 69 6e 74 6f 75 74 73 20 61 72 65 20 75   printouts are u
29720 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 68 65 6c  seful.** for hel
29721 70 69 6e 67 20 74 6f 20 75 6e 64 65 72 73 74 61  ping to understa
29722 6e 64 20 77 68 61 74 20 69 73 20 68 61 70 70 65  nd what is happe
29723 6e 69 6e 67 20 69 6e 73 69 64 65 20 74 68 65 20  ning inside the 
29724 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 0a 2a  code generator.*
29725 2a 20 64 75 72 69 6e 67 20 74 68 65 20 65 78 65  * during the exe
29726 63 75 74 69 6f 6e 20 6f 66 20 63 6f 6d 70 6c 65  cution of comple
29727 78 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  x SELECT stateme
29728 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  nts..**.** These
29729 20 72 6f 75 74 69 6e 65 20 61 72 65 20 6e 6f 74   routine are not
2972a 20 63 61 6c 6c 65 64 20 61 6e 79 77 68 65 72 65   called anywhere
2972b 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
2972c 20 6e 6f 72 6d 61 6c 0a 2a 2a 20 63 6f 64 65 20   normal.** code 
2972d 62 61 73 65 2e 20 20 54 68 65 6e 20 61 72 65 20  base.  Then are 
2972e 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 63  intended to be c
2972f 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
29730 6e 20 74 68 65 20 64 65 62 75 67 67 65 72 0a 2a  n the debugger.*
29731 2a 20 6f 72 20 66 72 6f 6d 20 74 65 6d 70 6f 72  * or from tempor
29732 61 72 79 20 22 70 72 69 6e 74 66 22 20 73 74 61  ary "printf" sta
29733 74 65 6d 65 6e 74 73 20 69 6e 73 65 72 74 65 64  tements inserted
29734 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a   for debugging..
29735 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
29736 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
29737 69 6e 74 45 78 70 72 28 45 78 70 72 20 2a 70 29  intExpr(Expr *p)
29738 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6b 65 6e  {.  if( p->token
29739 2e 7a 20 26 26 20 70 2d 3e 74 6f 6b 65 6e 2e 6e  .z && p->token.n
2973a 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  >0 ){.    sqlite
2973b 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 25  3DebugPrintf("(%
2973c 2e 2a 73 22 2c 20 70 2d 3e 74 6f 6b 65 6e 2e 6e  .*s", p->token.n
2973d 2c 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 29 3b 0a 20  , p->token.z);. 
2973e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2973f 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
29740 28 25 64 22 2c 20 70 2d 3e 6f 70 29 3b 0a 20 20  (%d", p->op);.  
29741 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74  }.  if( p->pLeft
29742 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
29743 65 62 75 67 50 72 69 6e 74 66 28 22 20 22 29 3b  ebugPrintf(" ");
29744 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
29745 74 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 29 3b  tExpr(p->pLeft);
29746 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 52  .  }.  if( p->pR
29747 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  ight ){.    sqli
29748 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
29749 20 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   ");.    sqlite3
2974a 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 52 69  PrintExpr(p->pRi
2974b 67 68 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ght);.  }.  sqli
2974c 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2974d 29 22 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  )");.}.SQLITE_PR
2974e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2974f 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74 28  e3PrintExprList(
29750 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
29751 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  {.  int i;.  for
29752 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
29753 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
29754 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
29755 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78  (pList->a[i].pEx
29756 70 72 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 70  pr);.    if( i<p
29757 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 29 7b  List->nExpr-1 ){
29758 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
29759 62 75 67 50 72 69 6e 74 66 28 22 2c 20 22 29 3b  bugPrintf(", ");
2975a 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c  .    }.  }.}.SQL
2975b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2975c 20 73 71 6c 69 74 65 33 50 72 69 6e 74 53 65 6c   sqlite3PrintSel
2975d 65 63 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 69  ect(Select *p, i
2975e 6e 74 20 69 6e 64 65 6e 74 29 7b 0a 20 20 73 71  nt indent){.  sq
2975f 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
29760 28 22 25 2a 73 53 45 4c 45 43 54 28 25 70 29 20  ("%*sSELECT(%p) 
29761 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 2c 20 70  ", indent, "", p
29762 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 72 69 6e  );.  sqlite3Prin
29763 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c  tExprList(p->pEL
29764 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ist);.  sqlite3D
29765 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29  ebugPrintf("\n")
29766 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53 72 63 20  ;.  if( p->pSrc 
29767 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 50 72  ){.    char *zPr
29768 65 66 69 78 3b 0a 20 20 20 20 69 6e 74 20 69 3b  efix;.    int i;
29769 0a 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20 22  .    zPrefix = "
2976a 46 52 4f 4d 22 3b 0a 20 20 20 20 66 6f 72 28 69  FROM";.    for(i
2976b 3d 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e  =0; i<p->pSrc->n
2976c 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Src; i++){.     
2976d 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2976e 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
2976f 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20  ->pSrc->a[i];.  
29770 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
29771 50 72 69 6e 74 66 28 22 25 2a 73 20 22 2c 20 69  Printf("%*s ", i
29772 6e 64 65 6e 74 2b 36 2c 20 7a 50 72 65 66 69 78  ndent+6, zPrefix
29773 29 3b 0a 20 20 20 20 20 20 7a 50 72 65 66 69 78  );.      zPrefix
29774 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 69 66 28   = "";.      if(
29775 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20   pItem->pSelect 
29776 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
29777 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 28  e3DebugPrintf("(
29778 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71  \n");.        sq
29779 6c 69 74 65 33 50 72 69 6e 74 53 65 6c 65 63 74  lite3PrintSelect
2977a 28 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c  (pItem->pSelect,
2977b 20 69 6e 64 65 6e 74 2b 31 30 29 3b 0a 20 20 20   indent+10);.   
2977c 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
2977d 67 50 72 69 6e 74 66 28 22 25 2a 73 29 22 2c 20  gPrintf("%*s)", 
2977e 69 6e 64 65 6e 74 2b 38 2c 20 22 22 29 3b 0a 20  indent+8, "");. 
2977f 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
29780 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20  Item->zName ){. 
29781 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
29782 62 75 67 50 72 69 6e 74 66 28 22 25 73 22 2c 20  bugPrintf("%s", 
29783 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pItem->zName);. 
29784 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
29785 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 29 7b 0a   pItem->pTab ){.
29786 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
29787 65 62 75 67 50 72 69 6e 74 66 28 22 28 74 61 62  ebugPrintf("(tab
29788 6c 65 3a 20 25 73 29 22 2c 20 70 49 74 65 6d 2d  le: %s)", pItem-
29789 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  >pTab->zName);. 
2978a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2978b 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29   pItem->zAlias )
2978c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2978d 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 41  3DebugPrintf(" A
2978e 53 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 41  S %s", pItem->zA
2978f 6c 69 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lias);.      }. 
29790 20 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 70 53       if( i<p->pS
29791 72 63 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20  rc->nSrc-1 ){.  
29792 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62        sqlite3Deb
29793 75 67 50 72 69 6e 74 66 28 22 2c 22 29 3b 0a 20  ugPrintf(",");. 
29794 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
29795 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
29796 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  "\n");.    }.  }
29797 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65  .  if( p->pWhere
29798 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
29799 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20  ebugPrintf("%*s 
2979a 57 48 45 52 45 20 22 2c 20 69 6e 64 65 6e 74 2c  WHERE ", indent,
2979b 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   "");.    sqlite
2979c 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e 70 57  3PrintExpr(p->pW
2979d 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  here);.    sqlit
2979e 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c  e3DebugPrintf("\
2979f 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n");.  }.  if( p
297a0 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20  ->pGroupBy ){.  
297a1 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
297a2 69 6e 74 66 28 22 25 2a 73 20 47 52 4f 55 50 20  intf("%*s GROUP 
297a3 42 59 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  BY ", indent, ""
297a4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
297a5 69 6e 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70  intExprList(p->p
297a6 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 73 71  GroupBy);.    sq
297a7 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
297a8 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66  ("\n");.  }.  if
297a9 28 20 70 2d 3e 70 48 61 76 69 6e 67 20 29 7b 0a  ( p->pHaving ){.
297aa 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
297ab 50 72 69 6e 74 66 28 22 25 2a 73 20 48 41 56 49  Printf("%*s HAVI
297ac 4e 47 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  NG ", indent, ""
297ad 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
297ae 69 6e 74 45 78 70 72 28 70 2d 3e 70 48 61 76 69  intExpr(p->pHavi
297af 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ng);.    sqlite3
297b0 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22  DebugPrintf("\n"
297b1 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
297b2 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
297b3 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
297b4 74 66 28 22 25 2a 73 20 4f 52 44 45 52 20 42 59  tf("%*s ORDER BY
297b5 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b   ", indent, "");
297b6 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
297b7 74 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72  tExprList(p->pOr
297b8 64 65 72 42 79 29 3b 0a 20 20 20 20 73 71 6c 69  derBy);.    sqli
297b9 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
297ba 5c 6e 22 29 3b 0a 20 20 7d 0a 7d 0a 2f 2a 20 45  \n");.  }.}./* E
297bb 6e 64 20 6f 66 20 74 68 65 20 73 74 72 75 63 74  nd of the struct
297bc 75 72 65 20 64 65 62 75 67 20 70 72 69 6e 74 69  ure debug printi
297bd 6e 67 20 63 6f 64 65 0a 2a 2a 2a 2a 2a 2a 2a 2a  ng code.********
297be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297c2 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  *****/.#endif /*
297c3 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
297c4 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64  TEST) || defined
297c5 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a  (SQLITE_DEBUG) *
297c6 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
297c7 2a 2a 20 45 6e 64 20 6f 66 20 73 65 6c 65 63 74  ** End of select
297c8 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
297c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297cb 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
297cc 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 61  ** Begin file ta
297cd 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ble.c **********
297ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297d0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
297d1 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
297d2 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
297d3 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
297d4 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
297d5 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
297d6 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
297d7 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
297d8 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
297d9 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
297da 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
297db 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
297dc 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
297dd 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
297de 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
297df 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
297e0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
297e1 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
297e2 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
297e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
297e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
297e8 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
297e9 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  the sqlite3_get_
297ea 74 61 62 6c 65 28 29 20 61 6e 64 20 73 71 6c 69  table() and sqli
297eb 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
297ec 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 6f  .** interface ro
297ed 75 74 69 6e 65 73 2e 20 20 54 68 65 73 65 20 61  utines.  These a
297ee 72 65 20 6a 75 73 74 20 77 72 61 70 70 65 72 73  re just wrappers
297ef 20 61 72 6f 75 6e 64 20 74 68 65 20 6d 61 69 6e   around the main
297f0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 72 6f  .** interface ro
297f1 75 74 69 6e 65 20 6f 66 20 73 71 6c 69 74 65 33  utine of sqlite3
297f2 5f 65 78 65 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  _exec()..**.** T
297f3 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
297f4 65 20 69 6e 20 61 20 73 65 70 61 72 61 74 65 20  e in a separate 
297f5 66 69 6c 65 73 20 73 6f 20 74 68 61 74 20 74 68  files so that th
297f6 65 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6c  ey will not be l
297f7 69 6e 6b 65 64 0a 2a 2a 20 69 66 20 74 68 65 79  inked.** if they
297f8 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e 0a 2a   are not used..*
297f9 2a 0a 2a 2a 20 24 49 64 3a 20 74 61 62 6c 65 2e  *.** $Id: table.
297fa 63 2c 76 20 31 2e 34 30 20 32 30 30 39 2f 30 34  c,v 1.40 2009/04
297fb 2f 31 30 20 31 34 3a 32 38 3a 30 30 20 64 72 68  /10 14:28:00 drh
297fc 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64   Exp $.*/..#ifnd
297fd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47  ef SQLITE_OMIT_G
297fe 45 54 5f 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20  ET_TABLE../*.** 
297ff 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  This structure i
29800 73 20 75 73 65 64 20 74 6f 20 70 61 73 73 20 64  s used to pass d
29801 61 74 61 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ata from sqlite3
29802 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 74 68 72  _get_table() thr
29803 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 63  ough.** to the c
29804 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
29805 20 69 73 20 75 73 65 73 20 74 6f 20 62 75 69 6c   is uses to buil
29806 64 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f  d the result..*/
29807 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
29808 54 61 62 52 65 73 75 6c 74 20 7b 0a 20 20 63 68  TabResult {.  ch
29809 61 72 20 2a 2a 61 7a 52 65 73 75 6c 74 3b 20 20  ar **azResult;  
2980a 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 64 20   /* Accumulated 
2980b 6f 75 74 70 75 74 20 2a 2f 0a 20 20 63 68 61 72  output */.  char
2980c 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 2f   *zErrMsg;     /
2980d 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20  * Error message 
2980e 74 65 78 74 2c 20 69 66 20 61 6e 20 65 72 72 6f  text, if an erro
2980f 72 20 6f 63 63 75 72 73 20 2a 2f 0a 20 20 69 6e  r occurs */.  in
29810 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20  t nAlloc;       
29811 20 2f 2a 20 53 6c 6f 74 73 20 61 6c 6c 6f 63 61   /* Slots alloca
29812 74 65 64 20 66 6f 72 20 61 7a 52 65 73 75 6c 74  ted for azResult
29813 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 77  [] */.  int nRow
29814 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ;          /* Nu
29815 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
29816 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20  the result */.  
29817 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
29818 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
29819 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72  columns in the r
2981a 65 73 75 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  esult */.  int n
2981b 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 2f 2a  Data;         /*
2981c 20 53 6c 6f 74 73 20 75 73 65 64 20 69 6e 20 61   Slots used in a
2981d 7a 52 65 73 75 6c 74 5b 5d 2e 20 20 28 6e 52 6f  zResult[].  (nRo
2981e 77 2b 31 29 2a 6e 43 6f 6c 75 6d 6e 20 2a 2f 0a  w+1)*nColumn */.
2981f 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
29820 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
29821 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ode from sqlite3
29822 5f 65 78 65 63 28 29 20 2a 2f 0a 7d 20 54 61 62  _exec() */.} Tab
29823 52 65 73 75 6c 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54  Result;../*.** T
29824 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
29825 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65  alled once for e
29826 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20 72  ach row in the r
29827 65 73 75 6c 74 20 74 61 62 6c 65 2e 20 20 49 74  esult table.  It
29828 73 20 6a 6f 62 0a 2a 2a 20 69 73 20 74 6f 20 66  s job.** is to f
29829 69 6c 6c 20 69 6e 20 74 68 65 20 54 61 62 52 65  ill in the TabRe
2982a 73 75 6c 74 20 73 74 72 75 63 74 75 72 65 20 61  sult structure a
2982b 70 70 72 6f 70 72 69 61 74 65 6c 79 2c 20 61 6c  ppropriately, al
2982c 6c 6f 63 61 74 69 6e 67 20 6e 65 77 0a 2a 2a 20  locating new.** 
2982d 6d 65 6d 6f 72 79 20 61 73 20 6e 65 63 65 73 73  memory as necess
2982e 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ary..*/.static i
2982f 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  nt sqlite3_get_t
29830 61 62 6c 65 5f 63 62 28 76 6f 69 64 20 2a 70 41  able_cb(void *pA
29831 72 67 2c 20 69 6e 74 20 6e 43 6f 6c 2c 20 63 68  rg, int nCol, ch
29832 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20  ar **argv, char 
29833 2a 2a 63 6f 6c 76 29 7b 0a 20 20 54 61 62 52 65  **colv){.  TabRe
29834 73 75 6c 74 20 2a 70 20 3d 20 28 54 61 62 52 65  sult *p = (TabRe
29835 73 75 6c 74 2a 29 70 41 72 67 3b 20 20 2f 2a 20  sult*)pArg;  /* 
29836 52 65 73 75 6c 74 20 61 63 63 75 6d 75 6c 61 74  Result accumulat
29837 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 65 64  or */.  int need
29838 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
29839 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6c 6f            /* Slo
2983a 74 73 20 6e 65 65 64 65 64 20 69 6e 20 70 2d 3e  ts needed in p->
2983b 61 7a 52 65 73 75 6c 74 5b 5d 20 2a 2f 0a 20 20  azResult[] */.  
2983c 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
2983d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2983e 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2983f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20  r */.  char *z; 
29840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29841 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69           /* A si
29842 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 72  ngle column of r
29843 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 2f 2a 20 4d  esult */..  /* M
29844 61 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69  ake sure there i
29845 73 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 69  s enough space i
29846 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74 20 74 6f  n p->azResult to
29847 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e 67   hold everything
29848 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64 20 74 6f  .  ** we need to
29849 20 72 65 6d 65 6d 62 65 72 20 66 72 6f 6d 20 74   remember from t
2984a 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  his invocation o
2984b 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a  f the callback..
2984c 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 52    */.  if( p->nR
2984d 6f 77 3d 3d 30 20 26 26 20 61 72 67 76 21 3d 30  ow==0 && argv!=0
2984e 20 29 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e   ){.    need = n
2984f 43 6f 6c 2a 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Col*2;.  }else{.
29850 20 20 20 20 6e 65 65 64 20 3d 20 6e 43 6f 6c 3b      need = nCol;
29851 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 44  .  }.  if( p->nD
29852 61 74 61 20 2b 20 6e 65 65 64 20 3e 20 70 2d 3e  ata + need > p->
29853 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68  nAlloc ){.    ch
29854 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20  ar **azNew;.    
29855 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e  p->nAlloc = p->n
29856 41 6c 6c 6f 63 2a 32 20 2b 20 6e 65 65 64 3b 0a  Alloc*2 + need;.
29857 20 20 20 20 61 7a 4e 65 77 20 3d 20 73 71 6c 69      azNew = sqli
29858 74 65 33 5f 72 65 61 6c 6c 6f 63 28 20 70 2d 3e  te3_realloc( p->
29859 61 7a 52 65 73 75 6c 74 2c 20 73 69 7a 65 6f 66  azResult, sizeof
2985a 28 63 68 61 72 2a 29 2a 70 2d 3e 6e 41 6c 6c 6f  (char*)*p->nAllo
2985b 63 20 29 3b 0a 20 20 20 20 69 66 28 20 61 7a 4e  c );.    if( azN
2985c 65 77 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c  ew==0 ) goto mal
2985d 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20  loc_failed;.    
2985e 70 2d 3e 61 7a 52 65 73 75 6c 74 20 3d 20 61 7a  p->azResult = az
2985f 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  New;.  }..  /* I
29860 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  f this is the fi
29861 72 73 74 20 72 6f 77 2c 20 74 68 65 6e 20 67 65  rst row, then ge
29862 6e 65 72 61 74 65 20 61 6e 20 65 78 74 72 61 20  nerate an extra 
29863 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20  row containing. 
29864 20 2a 2a 20 74 68 65 20 6e 61 6d 65 73 20 6f 66   ** the names of
29865 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20   all columns..  
29866 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 52 6f 77  */.  if( p->nRow
29867 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43  ==0 ){.    p->nC
29868 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 3b 0a 20 20  olumn = nCol;.  
29869 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
2986a 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a  l; i++){.      z
2986b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
2986c 74 66 28 22 25 73 22 2c 20 63 6f 6c 76 5b 69 5d  tf("%s", colv[i]
2986d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d  );.      if( z==
2986e 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f  0 ) goto malloc_
2986f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 70 2d  failed;.      p-
29870 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61  >azResult[p->nDa
29871 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d  ta++] = z;.    }
29872 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
29873 6e 43 6f 6c 75 6d 6e 21 3d 6e 43 6f 6c 20 29 7b  nColumn!=nCol ){
29874 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
29875 65 28 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  e(p->zErrMsg);. 
29876 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
29877 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
29878 0a 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 33  .       "sqlite3
29879 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 63 61 6c  _get_table() cal
2987a 6c 65 64 20 77 69 74 68 20 74 77 6f 20 6f 72 20  led with two or 
2987b 6d 6f 72 65 20 69 6e 63 6f 6d 70 61 74 69 62 6c  more incompatibl
2987c 65 20 71 75 65 72 69 65 73 22 0a 20 20 20 20 29  e queries".    )
2987d 3b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51  ;.    p->rc = SQ
2987e 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
2987f 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20  return 1;.  }.. 
29880 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20 74 68   /* Copy over th
29881 65 20 72 6f 77 20 64 61 74 61 0a 20 20 2a 2f 0a  e row data.  */.
29882 20 20 69 66 28 20 61 72 67 76 21 3d 30 20 29 7b    if( argv!=0 ){
29883 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
29884 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
29885 20 20 69 66 28 20 61 72 67 76 5b 69 5d 3d 3d 30    if( argv[i]==0
29886 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 20 3d 20   ){.        z = 
29887 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
29888 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20          int n = 
29889 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2988a 61 72 67 76 5b 69 5d 29 2b 31 3b 0a 20 20 20 20  argv[i])+1;.    
2988b 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
2988c 6d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20  malloc( n );.   
2988d 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20       if( z==0 ) 
2988e 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c  goto malloc_fail
2988f 65 64 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  ed;.        memc
29890 70 79 28 7a 2c 20 61 72 67 76 5b 69 5d 2c 20 6e  py(z, argv[i], n
29891 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
29892 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e   p->azResult[p->
29893 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20  nData++] = z;.  
29894 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 6f 77 2b    }.    p->nRow+
29895 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  +;.  }.  return 
29896 30 3b 0a 0a 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65  0;..malloc_faile
29897 64 3a 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  d:.  p->rc = SQL
29898 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 65 74  ITE_NOMEM;.  ret
29899 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
2989a 51 75 65 72 79 20 74 68 65 20 64 61 74 61 62 61  Query the databa
2989b 73 65 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64  se.  But instead
2989c 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 61 20 63   of invoking a c
2989d 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61 63 68  allback for each
2989e 20 72 6f 77 2c 0a 2a 2a 20 6d 61 6c 6c 6f 63 28   row,.** malloc(
2989f 29 20 66 6f 72 20 73 70 61 63 65 20 74 6f 20 68  ) for space to h
298a0 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 20 61  old the result a
298a1 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 6e  nd return the en
298a2 74 69 72 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20  tire results.** 
298a3 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f  at the conclusio
298a4 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 2e 0a 2a  n of the call..*
298a5 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20  *.** The result 
298a6 74 68 61 74 20 69 73 20 77 72 69 74 74 65 6e 20  that is written 
298a7 74 6f 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 20  to ***pazResult 
298a8 69 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72  is held in memor
298a9 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  y obtained.** fr
298aa 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 42 75  om malloc().  Bu
298ab 74 20 74 68 65 20 63 61 6c 6c 65 72 20 63 61 6e  t the caller can
298ac 6e 6f 74 20 66 72 65 65 20 74 68 69 73 20 6d 65  not free this me
298ad 6d 6f 72 79 20 64 69 72 65 63 74 6c 79 2e 20 20  mory directly.  
298ae 0a 2a 2a 20 49 6e 73 74 65 61 64 2c 20 74 68 65  .** Instead, the
298af 20 65 6e 74 69 72 65 20 74 61 62 6c 65 20 73 68   entire table sh
298b0 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 74  ould be passed t
298b1 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  o sqlite3_free_t
298b2 61 62 6c 65 28 29 20 77 68 65 6e 0a 2a 2a 20 74  able() when.** t
298b3 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65  he calling proce
298b4 64 75 72 65 20 69 73 20 66 69 6e 69 73 68 65 64  dure is finished
298b5 20 75 73 69 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51   using it..*/.SQ
298b6 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
298b7 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a  ite3_get_table(.
298b8 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
298b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
298ba 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 6e   The database on
298bb 20 77 68 69 63 68 20 74 68 65 20 53 51 4c 20 65   which the SQL e
298bc 78 65 63 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e  xecutes */.  con
298bd 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
298be 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
298bf 53 51 4c 20 74 6f 20 62 65 20 65 78 65 63 75 74  SQL to be execut
298c0 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a  ed */.  char ***
298c1 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 20 20 20  pazResult,      
298c2 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
298c3 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68 65   result table he
298c4 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52  re */.  int *pnR
298c5 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ow,             
298c6 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
298c7 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
298c8 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 68 65  in the result he
298c9 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  re */.  int *pnC
298ca 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
298cb 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
298cc 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
298cd 6e 73 20 6f 66 20 72 65 73 75 6c 74 20 68 65 72  ns of result her
298ce 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
298cf 45 72 72 4d 73 67 20 20 20 20 20 20 20 20 20 20  ErrMsg          
298d0 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f     /* Write erro
298d1 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20  r messages here 
298d2 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
298d3 20 20 54 61 62 52 65 73 75 6c 74 20 72 65 73 3b    TabResult res;
298d4 0a 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d  ..  *pazResult =
298d5 20 30 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75   0;.  if( pnColu
298d6 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d  mn ) *pnColumn =
298d7 20 30 3b 0a 20 20 69 66 28 20 70 6e 52 6f 77 20   0;.  if( pnRow 
298d8 29 20 2a 70 6e 52 6f 77 20 3d 20 30 3b 0a 20 20  ) *pnRow = 0;.  
298d9 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 20 2a  if( pzErrMsg ) *
298da 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  pzErrMsg = 0;.  
298db 72 65 73 2e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  res.zErrMsg = 0;
298dc 0a 20 20 72 65 73 2e 6e 52 6f 77 20 3d 20 30 3b  .  res.nRow = 0;
298dd 0a 20 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e 20 3d  .  res.nColumn =
298de 20 30 3b 0a 20 20 72 65 73 2e 6e 44 61 74 61 20   0;.  res.nData 
298df 3d 20 31 3b 0a 20 20 72 65 73 2e 6e 41 6c 6c 6f  = 1;.  res.nAllo
298e0 63 20 3d 20 32 30 3b 0a 20 20 72 65 73 2e 72 63  c = 20;.  res.rc
298e1 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
298e2 72 65 73 2e 61 7a 52 65 73 75 6c 74 20 3d 20 73  res.azResult = s
298e3 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
298e4 7a 65 6f 66 28 63 68 61 72 2a 29 2a 72 65 73 2e  zeof(char*)*res.
298e5 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 69 66 28 20  nAlloc );.  if( 
298e6 72 65 73 2e 61 7a 52 65 73 75 6c 74 3d 3d 30 20  res.azResult==0 
298e7 29 7b 0a 20 20 20 20 20 64 62 2d 3e 65 72 72 43  ){.     db->errC
298e8 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  ode = SQLITE_NOM
298e9 45 4d 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20  EM;.     return 
298ea 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
298eb 7d 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74  }.  res.azResult
298ec 5b 30 5d 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20  [0] = 0;.  rc = 
298ed 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
298ee 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 5f 67   zSql, sqlite3_g
298ef 65 74 5f 74 61 62 6c 65 5f 63 62 2c 20 26 72 65  et_table_cb, &re
298f0 73 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20  s, pzErrMsg);.  
298f1 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 72  assert( sizeof(r
298f2 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3e  es.azResult[0])>
298f3 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e 6e 44 61  = sizeof(res.nDa
298f4 74 61 29 20 29 3b 0a 20 20 72 65 73 2e 61 7a 52  ta) );.  res.azR
298f5 65 73 75 6c 74 5b 30 5d 20 3d 20 53 51 4c 49 54  esult[0] = SQLIT
298f6 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 73  E_INT_TO_PTR(res
298f7 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28 20 28  .nData);.  if( (
298f8 72 63 26 30 78 66 66 29 3d 3d 53 51 4c 49 54 45  rc&0xff)==SQLITE
298f9 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 73 71  _ABORT ){.    sq
298fa 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
298fb 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31  (&res.azResult[1
298fc 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 2e  ]);.    if( res.
298fd 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
298fe 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b   if( pzErrMsg ){
298ff 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29900 5f 66 72 65 65 28 2a 70 7a 45 72 72 4d 73 67 29  _free(*pzErrMsg)
29901 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72  ;.        *pzErr
29902 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Msg = sqlite3_mp
29903 72 69 6e 74 66 28 22 25 73 22 2c 72 65 73 2e 7a  rintf("%s",res.z
29904 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d  ErrMsg);.      }
29905 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
29906 72 65 65 28 72 65 73 2e 7a 45 72 72 4d 73 67 29  ree(res.zErrMsg)
29907 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e  ;.    }.    db->
29908 65 72 72 43 6f 64 65 20 3d 20 72 65 73 2e 72 63  errCode = res.rc
29909 3b 20 20 2f 2a 20 41 73 73 75 6d 65 20 33 32 2d  ;  /* Assume 32-
2990a 62 69 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 69  bit assignment i
2990b 73 20 61 74 6f 6d 69 63 20 2a 2f 0a 20 20 20 20  s atomic */.    
2990c 72 65 74 75 72 6e 20 72 65 73 2e 72 63 3b 0a 20  return res.rc;. 
2990d 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
2990e 65 28 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a  e(res.zErrMsg);.
2990f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
29910 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
29911 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72  e3_free_table(&r
29912 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b  es.azResult[1]);
29913 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
29914 20 20 7d 0a 20 20 69 66 28 20 72 65 73 2e 6e 41    }.  if( res.nA
29915 6c 6c 6f 63 3e 72 65 73 2e 6e 44 61 74 61 20 29  lloc>res.nData )
29916 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e  {.    char **azN
29917 65 77 3b 0a 20 20 20 20 61 7a 4e 65 77 20 3d 20  ew;.    azNew = 
29918 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
29919 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 2c 20 73   res.azResult, s
2991a 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 72 65 73  izeof(char*)*res
2991b 2e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 69 66  .nData );.    if
2991c 28 20 61 7a 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  ( azNew==0 ){.  
2991d 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2991e 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65  _table(&res.azRe
2991f 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 20 20  sult[1]);.      
29920 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51  db->errCode = SQ
29921 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
29922 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
29923 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
29924 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 20 3d 20   res.azResult = 
29925 61 7a 4e 65 77 3b 0a 20 20 7d 0a 20 20 2a 70 61  azNew;.  }.  *pa
29926 7a 52 65 73 75 6c 74 20 3d 20 26 72 65 73 2e 61  zResult = &res.a
29927 7a 52 65 73 75 6c 74 5b 31 5d 3b 0a 20 20 69 66  zResult[1];.  if
29928 28 20 70 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e  ( pnColumn ) *pn
29929 43 6f 6c 75 6d 6e 20 3d 20 72 65 73 2e 6e 43 6f  Column = res.nCo
2992a 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 70 6e 52 6f  lumn;.  if( pnRo
2992b 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20 72 65 73  w ) *pnRow = res
2992c 2e 6e 52 6f 77 3b 0a 20 20 72 65 74 75 72 6e 20  .nRow;.  return 
2992d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
2992e 73 20 72 6f 75 74 69 6e 65 20 66 72 65 65 73 20  s routine frees 
2992f 74 68 65 20 73 70 61 63 65 20 74 68 65 20 73 71  the space the sq
29930 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
29931 29 20 6d 61 6c 6c 6f 63 65 64 2e 0a 2a 2f 0a 53  ) malloced..*/.S
29932 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
29933 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
29934 65 28 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65  e(.  char **azRe
29935 73 75 6c 74 20 20 20 20 20 20 20 20 20 20 20 20  sult            
29936 2f 2a 20 52 65 73 75 6c 74 20 72 65 74 75 72 6e  /* Result return
29937 65 64 20 66 72 6f 6d 20 66 72 6f 6d 20 73 71 6c  ed from from sql
29938 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29  ite3_get_table()
29939 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 7a 52   */.){.  if( azR
2993a 65 73 75 6c 74 20 29 7b 0a 20 20 20 20 69 6e 74  esult ){.    int
2993b 20 69 2c 20 6e 3b 0a 20 20 20 20 61 7a 52 65 73   i, n;.    azRes
2993c 75 6c 74 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72  ult--;.    asser
2993d 74 28 20 61 7a 52 65 73 75 6c 74 21 3d 30 20 29  t( azResult!=0 )
2993e 3b 0a 20 20 20 20 6e 20 3d 20 53 51 4c 49 54 45  ;.    n = SQLITE
2993f 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 61 7a 52 65  _PTR_TO_INT(azRe
29940 73 75 6c 74 5b 30 5d 29 3b 0a 20 20 20 20 66 6f  sult[0]);.    fo
29941 72 28 69 3d 31 3b 20 69 3c 6e 3b 20 69 2b 2b 29  r(i=1; i<n; i++)
29942 7b 20 69 66 28 20 61 7a 52 65 73 75 6c 74 5b 69  { if( azResult[i
29943 5d 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ] ) sqlite3_free
29944 28 61 7a 52 65 73 75 6c 74 5b 69 5d 29 3b 20 7d  (azResult[i]); }
29945 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
29946 65 28 61 7a 52 65 73 75 6c 74 29 3b 0a 20 20 7d  e(azResult);.  }
29947 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
29948 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41  LITE_OMIT_GET_TA
29949 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  BLE */../*******
2994a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
2994b 61 62 6c 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  able.c *********
2994c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2994d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2994e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2994f 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
29950 6c 65 20 74 72 69 67 67 65 72 2e 63 20 2a 2a 2a  le trigger.c ***
29951 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29952 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29953 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 0a 2a 2a  ******/./*.**.**
29954 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
29955 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
29956 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
29957 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
29958 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
29959 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2995a 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2995b 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2995c 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2995d 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2995e 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2995f 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
29960 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
29961 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
29962 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
29963 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
29964 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
29965 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29966 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29967 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29968 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29969 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 0a  *********.**.**.
2996a 2a 2a 20 24 49 64 3a 20 74 72 69 67 67 65 72 2e  ** $Id: trigger.
2996b 63 2c 76 20 31 2e 31 33 38 20 32 30 30 39 2f 30  c,v 1.138 2009/0
2996c 35 2f 30 36 20 31 38 3a 34 32 3a 32 31 20 64 72  5/06 18:42:21 dr
2996d 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e  h Exp $.*/..#ifn
2996e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2996f 54 52 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65  TRIGGER./*.** De
29970 6c 65 74 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69  lete a linked li
29971 73 74 20 6f 66 20 54 72 69 67 67 65 72 53 74 65  st of TriggerSte
29972 70 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  p structures..*/
29973 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
29974 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
29975 74 65 54 72 69 67 67 65 72 53 74 65 70 28 73 71  teTriggerStep(sq
29976 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67  lite3 *db, Trigg
29977 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72  erStep *pTrigger
29978 53 74 65 70 29 7b 0a 20 20 77 68 69 6c 65 28 20  Step){.  while( 
29979 70 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a  pTriggerStep ){.
2997a 20 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20      TriggerStep 
2997b 2a 20 70 54 6d 70 20 3d 20 70 54 72 69 67 67 65  * pTmp = pTrigge
2997c 72 53 74 65 70 3b 0a 20 20 20 20 70 54 72 69 67  rStep;.    pTrig
2997d 67 65 72 53 74 65 70 20 3d 20 70 54 72 69 67 67  gerStep = pTrigg
2997e 65 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 0a  erStep->pNext;..
2997f 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 74 61      if( pTmp->ta
29980 72 67 65 74 2e 64 79 6e 20 29 20 73 71 6c 69 74  rget.dyn ) sqlit
29981 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68  e3DbFree(db, (ch
29982 61 72 2a 29 70 54 6d 70 2d 3e 74 61 72 67 65 74  ar*)pTmp->target
29983 2e 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  .z);.    sqlite3
29984 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
29985 54 6d 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  Tmp->pWhere);.  
29986 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
29987 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70  tDelete(db, pTmp
29988 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20  ->pExprList);.  
29989 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2998a 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e  elete(db, pTmp->
2998b 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71  pSelect);.    sq
2998c 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
2998d 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 49 64 4c  e(db, pTmp->pIdL
2998e 69 73 74 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  ist);..    sqlit
2998f 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d  e3DbFree(db, pTm
29990 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
29991 20 47 69 76 65 6e 20 74 61 62 6c 65 20 70 54 61   Given table pTa
29992 62 2c 20 72 65 74 75 72 6e 20 61 20 6c 69 73 74  b, return a list
29993 20 6f 66 20 61 6c 6c 20 74 68 65 20 74 72 69 67   of all the trig
29994 67 65 72 73 20 61 74 74 61 63 68 65 64 20 74 6f  gers attached to
29995 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20   .** the table. 
29996 54 68 65 20 6c 69 73 74 20 69 73 20 63 6f 6e 6e  The list is conn
29997 65 63 74 65 64 20 62 79 20 54 72 69 67 67 65 72  ected by Trigger
29998 2e 70 4e 65 78 74 20 70 6f 69 6e 74 65 72 73 2e  .pNext pointers.
29999 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2999a 54 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69  TE Trigger *sqli
2999b 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 50  te3TriggerList(P
2999c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
2999d 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 53 63  ble *pTab){.  Sc
2999e 68 65 6d 61 20 2a 20 63 6f 6e 73 74 20 70 54 6d  hema * const pTm
2999f 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72 73 65  pSchema = pParse
299a0 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ->db->aDb[1].pSc
299a1 68 65 6d 61 3b 0a 20 20 54 72 69 67 67 65 72 20  hema;.  Trigger 
299a2 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20  *pList = 0;     
299a3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
299a4 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73  List of triggers
299a5 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20   to return */.. 
299a6 20 69 66 28 20 70 54 6d 70 53 63 68 65 6d 61 21   if( pTmpSchema!
299a7 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
299a8 7b 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a  {.    HashElem *
299a9 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c  p;.    for(p=sql
299aa 69 74 65 48 61 73 68 46 69 72 73 74 28 26 70 54  iteHashFirst(&pT
299ab 6d 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61  mpSchema->trigHa
299ac 73 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65  sh); p; p=sqlite
299ad 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20  HashNext(p)){.  
299ae 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72      Trigger *pTr
299af 69 67 20 3d 20 28 54 72 69 67 67 65 72 20 2a 29  ig = (Trigger *)
299b0 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70  sqliteHashData(p
299b1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 72  );.      if( pTr
299b2 69 67 2d 3e 70 54 61 62 53 63 68 65 6d 61 3d 3d  ig->pTabSchema==
299b3 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 0a 20 20  pTab->pSchema.  
299b4 20 20 20 20 20 26 26 20 30 3d 3d 73 71 6c 69 74       && 0==sqlit
299b5 65 33 53 74 72 49 43 6d 70 28 70 54 72 69 67 2d  e3StrICmp(pTrig-
299b6 3e 74 61 62 6c 65 2c 20 70 54 61 62 2d 3e 7a 4e  >table, pTab->zN
299b7 61 6d 65 29 20 0a 20 20 20 20 20 20 29 7b 0a 20  ame) .      ){. 
299b8 20 20 20 20 20 20 20 70 54 72 69 67 2d 3e 70 4e         pTrig->pN
299b9 65 78 74 20 3d 20 28 70 4c 69 73 74 20 3f 20 70  ext = (pList ? p
299ba 4c 69 73 74 20 3a 20 70 54 61 62 2d 3e 70 54 72  List : pTab->pTr
299bb 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20  igger);.        
299bc 70 4c 69 73 74 20 3d 20 70 54 72 69 67 3b 0a 20  pList = pTrig;. 
299bd 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
299be 0a 0a 20 20 72 65 74 75 72 6e 20 28 70 4c 69 73  ..  return (pLis
299bf 74 20 3f 20 70 4c 69 73 74 20 3a 20 70 54 61 62  t ? pList : pTab
299c0 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a 7d 0a 0a  ->pTrigger);.}..
299c1 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61  /*.** This is ca
299c2 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
299c3 65 72 20 77 68 65 6e 20 69 74 20 73 65 65 73 20  er when it sees 
299c4 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  a CREATE TRIGGER
299c5 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 75 70   statement.** up
299c6 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 20 6f 66   to the point of
299c7 20 74 68 65 20 42 45 47 49 4e 20 62 65 66 6f 72   the BEGIN befor
299c8 65 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63  e the trigger ac
299c9 74 69 6f 6e 73 2e 20 20 41 20 54 72 69 67 67 65  tions.  A Trigge
299ca 72 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69  r.** structure i
299cb 73 20 67 65 6e 65 72 61 74 65 64 20 62 61 73 65  s generated base
299cc 64 20 6f 6e 20 74 68 65 20 69 6e 66 6f 72 6d 61  d on the informa
299cd 74 69 6f 6e 20 61 76 61 69 6c 61 62 6c 65 20 61  tion available a
299ce 6e 64 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  nd stored.** in 
299cf 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
299d0 67 65 72 2e 20 20 41 66 74 65 72 20 74 68 65 20  ger.  After the 
299d1 74 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73 20  trigger actions 
299d2 68 61 76 65 20 62 65 65 6e 20 70 61 72 73 65 64  have been parsed
299d3 2c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  , the.** sqlite3
299d4 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 29 20  FinishTrigger() 
299d5 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
299d6 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74  ed to complete t
299d7 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 63 6f  he trigger.** co
299d8 6e 73 74 72 75 63 74 69 6f 6e 20 70 72 6f 63 65  nstruction proce
299d9 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ss..*/.SQLITE_PR
299da 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
299db 65 33 42 65 67 69 6e 54 72 69 67 67 65 72 28 0a  e3BeginTrigger(.
299dc 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
299dd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
299de 73 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68  se context of th
299df 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  e CREATE TRIGGER
299e0 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
299e1 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20  Token *pName1,  
299e2 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20      /* The name 
299e3 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 2a  of the trigger *
299e4 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
299e5 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e  2,      /* The n
299e6 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67 67  ame of the trigg
299e7 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74 72 5f 74  er */.  int tr_t
299e8 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  m,          /* O
299e9 6e 65 20 6f 66 20 54 4b 5f 42 45 46 4f 52 45 2c  ne of TK_BEFORE,
299ea 20 54 4b 5f 41 46 54 45 52 2c 20 54 4b 5f 49 4e   TK_AFTER, TK_IN
299eb 53 54 45 41 44 20 2a 2f 0a 20 20 69 6e 74 20 6f  STEAD */.  int o
299ec 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  p,             /
299ed 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 49 4e 53 45  * One of TK_INSE
299ee 52 54 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54  RT, TK_UPDATE, T
299ef 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 49 64  K_DELETE */.  Id
299f0 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 73 2c 20  List *pColumns, 
299f1 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6c 69 73 74    /* column list
299f2 20 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 55   if this is an U
299f3 50 44 41 54 45 20 4f 46 20 74 72 69 67 67 65 72  PDATE OF trigger
299f4 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
299f5 54 61 62 6c 65 4e 61 6d 65 2c 2f 2a 20 54 68 65  TableName,/* The
299f6 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
299f7 6c 65 2f 76 69 65 77 20 74 68 65 20 74 72 69 67  le/view the trig
299f8 67 65 72 20 61 70 70 6c 69 65 73 20 74 6f 20 2a  ger applies to *
299f9 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 2c  /.  Expr *pWhen,
299fa 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 4e 20          /* WHEN 
299fb 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
299fc 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20 20 20  isTemp,         
299fd 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 54  /* True if the T
299fe 45 4d 50 4f 52 41 52 59 20 6b 65 79 77 6f 72 64  EMPORARY keyword
299ff 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
29a00 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20   int noErr      
29a01 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73 73       /* Suppress
29a02 20 65 72 72 6f 72 73 20 69 66 20 74 68 65 20 74   errors if the t
29a03 72 69 67 67 65 72 20 61 6c 72 65 61 64 79 20 65  rigger already e
29a04 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 72  xists */.){.  Tr
29a05 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 20  igger *pTrigger 
29a06 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  = 0;.  Table *pT
29a07 61 62 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  ab;.  char *zNam
29a08 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  e = 0;        /*
29a09 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69   Name of the tri
29a0a 67 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  gger */.  sqlite
29a0b 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
29a0c 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  db;.  int iDb;  
29a0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29a0e 20 54 68 65 20 64 61 74 61 62 61 73 65 20 74 6f   The database to
29a0f 20 73 74 6f 72 65 20 74 68 65 20 74 72 69 67 67   store the trigg
29a10 65 72 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  er in */.  Token
29a11 20 2a 70 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *pName;        
29a12 20 20 20 2f 2a 20 54 68 65 20 75 6e 71 75 61 6c     /* The unqual
29a13 69 66 69 65 64 20 64 62 20 6e 61 6d 65 20 2a 2f  ified db name */
29a14 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b  .  DbFixer sFix;
29a15 0a 20 20 69 6e 74 20 69 54 61 62 44 62 3b 0a 0a  .  int iTabDb;..
29a16 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31    assert( pName1
29a17 21 3d 30 20 29 3b 20 20 20 2f 2a 20 70 4e 61 6d  !=0 );   /* pNam
29a18 65 31 2d 3e 7a 20 6d 69 67 68 74 20 62 65 20 4e  e1->z might be N
29a19 55 4c 4c 2c 20 62 75 74 20 6e 6f 74 20 70 4e 61  ULL, but not pNa
29a1a 6d 65 31 20 69 74 73 65 6c 66 20 2a 2f 0a 20 20  me1 itself */.  
29a1b 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32 21 3d  assert( pName2!=
29a1c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  0 );.  assert( o
29a1d 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 7c 7c 20  p==TK_INSERT || 
29a1e 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c 7c  op==TK_UPDATE ||
29a1f 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 29   op==TK_DELETE )
29a20 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 30  ;.  assert( op>0
29a21 20 26 26 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20   && op<0xff );. 
29a22 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a 20   if( isTemp ){. 
29a23 20 20 20 2f 2a 20 49 66 20 54 45 4d 50 20 77 61     /* If TEMP wa
29a24 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65  s specified, the
29a25 6e 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61  n the trigger na
29a26 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71 75  me may not be qu
29a27 61 6c 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20 20  alified. */.    
29a28 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20  if( pName2->n>0 
29a29 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
29a2a 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
29a2b 20 22 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67   "temporary trig
29a2c 67 65 72 20 6d 61 79 20 6e 6f 74 20 68 61 76 65  ger may not have
29a2d 20 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 22   qualified name"
29a2e 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72  );.      goto tr
29a2f 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
29a30 20 20 20 7d 0a 20 20 20 20 69 44 62 20 3d 20 31     }.    iDb = 1
29a31 3b 0a 20 20 20 20 70 4e 61 6d 65 20 3d 20 70 4e  ;.    pName = pN
29a32 61 6d 65 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ame1;.  }else{. 
29a33 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
29a34 20 74 68 65 20 64 62 20 74 68 61 74 20 74 68 65   the db that the
29a35 20 74 68 65 20 74 72 69 67 67 65 72 20 77 69 6c   the trigger wil
29a36 6c 20 62 65 20 63 72 65 61 74 65 64 20 69 6e 20  l be created in 
29a37 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c  */.    iDb = sql
29a38 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
29a39 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
29a3a 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b  pName2, &pName);
29a3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29  .    if( iDb<0 )
29a3c 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69  {.      goto tri
29a3d 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
29a3e 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
29a3f 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d   the trigger nam
29a40 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69 65  e was unqualifie
29a41 64 2c 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65  d, and the table
29a42 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c 65   is a temp table
29a43 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20  ,.  ** then set 
29a44 69 44 62 20 74 6f 20 31 20 74 6f 20 63 72 65 61  iDb to 1 to crea
29a45 74 65 20 74 68 65 20 74 72 69 67 67 65 72 20 69  te the trigger i
29a46 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  n the temporary 
29a47 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49  database..  ** I
29a48 66 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  f sqlite3SrcList
29a49 4c 6f 6f 6b 75 70 28 29 20 72 65 74 75 72 6e 73  Lookup() returns
29a4a 20 30 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74   0, indicating t
29a4b 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f  he table does no
29a4c 74 0a 20 20 2a 2a 20 65 78 69 73 74 2c 20 74 68  t.  ** exist, th
29a4d 65 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 68  e error is caugh
29a4e 74 20 62 79 20 74 68 65 20 62 6c 6f 63 6b 20 62  t by the block b
29a4f 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  elow..  */.  if(
29a50 20 21 70 54 61 62 6c 65 4e 61 6d 65 20 7c 7c 20   !pTableName || 
29a51 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29a52 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69   ){.    goto tri
29a53 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
29a54 7d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  }.  pTab = sqlit
29a55 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
29a56 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 4e 61  pParse, pTableNa
29a57 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65  me);.  if( pName
29a58 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 20  2->n==0 && pTab 
29a59 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  && pTab->pSchema
29a5a 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ==db->aDb[1].pSc
29a5b 68 65 6d 61 20 29 7b 0a 20 20 20 20 69 44 62 20  hema ){.    iDb 
29a5c 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45  = 1;.  }..  /* E
29a5d 6e 73 75 72 65 20 74 68 65 20 74 61 62 6c 65 20  nsure the table 
29a5e 6e 61 6d 65 20 6d 61 74 63 68 65 73 20 64 61 74  name matches dat
29a5f 61 62 61 73 65 20 6e 61 6d 65 20 61 6e 64 20 74  abase name and t
29a60 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 65 78  hat the table ex
29a61 69 73 74 73 20 2a 2f 0a 20 20 69 66 28 20 64 62  ists */.  if( db
29a62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
29a63 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
29a64 65 61 6e 75 70 3b 0a 20 20 61 73 73 65 72 74 28  eanup;.  assert(
29a65 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 6e 53 72   pTableName->nSr
29a66 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 73 71  c==1 );.  if( sq
29a67 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46  lite3FixInit(&sF
29a68 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c  ix, pParse, iDb,
29a69 20 22 74 72 69 67 67 65 72 22 2c 20 70 4e 61 6d   "trigger", pNam
29a6a 65 29 20 26 26 20 0a 20 20 20 20 20 20 73 71 6c  e) && .      sql
29a6b 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 26  ite3FixSrcList(&
29a6c 73 46 69 78 2c 20 70 54 61 62 6c 65 4e 61 6d 65  sFix, pTableName
29a6d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72  ) ){.    goto tr
29a6e 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
29a6f 20 7d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69   }.  pTab = sqli
29a70 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70  te3SrcListLookup
29a71 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 4e  (pParse, pTableN
29a72 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61  ame);.  if( !pTa
29a73 62 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  b ){.    /* The 
29a74 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65  table does not e
29a75 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 67 6f 74  xist. */.    got
29a76 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
29a77 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56  p;.  }.  if( IsV
29a78 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
29a79 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
29a7a 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e  Msg(pParse, "can
29a7b 6e 6f 74 20 63 72 65 61 74 65 20 74 72 69 67 67  not create trigg
29a7c 65 72 73 20 6f 6e 20 76 69 72 74 75 61 6c 20 74  ers on virtual t
29a7d 61 62 6c 65 73 22 29 3b 0a 20 20 20 20 67 6f 74  ables");.    got
29a7e 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
29a7f 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  p;.  }..  /* Che
29a80 63 6b 20 74 68 61 74 20 74 68 65 20 74 72 69 67  ck that the trig
29a81 67 65 72 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20  ger name is not 
29a82 72 65 73 65 72 76 65 64 20 61 6e 64 20 74 68 61  reserved and tha
29a83 74 20 6e 6f 20 74 72 69 67 67 65 72 20 6f 66 20  t no trigger of 
29a84 74 68 65 0a 20 20 2a 2a 20 73 70 65 63 69 66 69  the.  ** specifi
29a85 65 64 20 6e 61 6d 65 20 65 78 69 73 74 73 20 2a  ed name exists *
29a86 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  /.  zName = sqli
29a87 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
29a88 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69  (db, pName);.  i
29a89 66 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 53 51 4c  f( !zName || SQL
29a8a 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43  ITE_OK!=sqlite3C
29a8b 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70  heckObjectName(p
29a8c 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b  Parse, zName) ){
29a8d 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
29a8e 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
29a8f 20 69 66 28 20 73 71 6c 69 74 65 33 48 61 73 68   if( sqlite3Hash
29a90 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 69  Find(&(db->aDb[i
29a91 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69  Db].pSchema->tri
29a92 67 48 61 73 68 29 2c 0a 20 20 20 20 20 20 20 20  gHash),.        
29a93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e                zN
29a94 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ame, sqlite3Strl
29a95 65 6e 33 30 28 7a 4e 61 6d 65 29 29 20 29 7b 0a  en30(zName)) ){.
29a96 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29      if( !noErr )
29a97 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
29a98 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
29a99 22 74 72 69 67 67 65 72 20 25 54 20 61 6c 72 65  "trigger %T alre
29a9a 61 64 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61  ady exists", pNa
29a9b 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67  me);.    }.    g
29a9c 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
29a9d 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44  nup;.  }..  /* D
29a9e 6f 20 6e 6f 74 20 63 72 65 61 74 65 20 61 20 74  o not create a t
29a9f 72 69 67 67 65 72 20 6f 6e 20 61 20 73 79 73 74  rigger on a syst
29aa0 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66  em table */.  if
29aa1 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ( sqlite3StrNICm
29aa2 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22  p(pTab->zName, "
29aa3 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20  sqlite_", 7)==0 
29aa4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
29aa5 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
29aa6 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 74 72  cannot create tr
29aa7 69 67 67 65 72 20 6f 6e 20 73 79 73 74 65 6d 20  igger on system 
29aa8 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 70 50 61  table");.    pPa
29aa9 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  rse->nErr++;.   
29aaa 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
29aab 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
29aac 20 49 4e 53 54 45 41 44 20 6f 66 20 74 72 69 67   INSTEAD of trig
29aad 67 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 66 6f  gers are only fo
29aae 72 20 76 69 65 77 73 20 61 6e 64 20 76 69 65 77  r views and view
29aaf 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 49  s only support I
29ab0 4e 53 54 45 41 44 0a 20 20 2a 2a 20 6f 66 20 74  NSTEAD.  ** of t
29ab1 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20  riggers..  */.  
29ab2 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
29ab3 74 20 26 26 20 74 72 5f 74 6d 21 3d 54 4b 5f 49  t && tr_tm!=TK_I
29ab4 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20 73 71  NSTEAD ){.    sq
29ab5 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
29ab6 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72  arse, "cannot cr
29ab7 65 61 74 65 20 25 73 20 74 72 69 67 67 65 72 20  eate %s trigger 
29ab8 6f 6e 20 76 69 65 77 3a 20 25 53 22 2c 20 0a 20  on view: %S", . 
29ab9 20 20 20 20 20 20 20 28 74 72 5f 74 6d 20 3d 3d         (tr_tm ==
29aba 20 54 4b 5f 42 45 46 4f 52 45 29 3f 22 42 45 46   TK_BEFORE)?"BEF
29abb 4f 52 45 22 3a 22 41 46 54 45 52 22 2c 20 70 54  ORE":"AFTER", pT
29abc 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  ableName, 0);.  
29abd 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
29abe 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
29abf 28 20 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  ( !pTab->pSelect
29ac0 20 26 26 20 74 72 5f 74 6d 3d 3d 54 4b 5f 49 4e   && tr_tm==TK_IN
29ac1 53 54 45 41 44 20 29 7b 0a 20 20 20 20 73 71 6c  STEAD ){.    sql
29ac2 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
29ac3 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65  rse, "cannot cre
29ac4 61 74 65 20 49 4e 53 54 45 41 44 20 4f 46 22 0a  ate INSTEAD OF".
29ac5 20 20 20 20 20 20 20 20 22 20 74 72 69 67 67 65          " trigge
29ac6 72 20 6f 6e 20 74 61 62 6c 65 3a 20 25 53 22 2c  r on table: %S",
29ac7 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b   pTableName, 0);
29ac8 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
29ac9 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
29aca 20 69 54 61 62 44 62 20 3d 20 73 71 6c 69 74 65   iTabDb = sqlite
29acb 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
29acc 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
29acd 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
29ace 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
29acf 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e  ATION.  {.    in
29ad0 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  t code = SQLITE_
29ad1 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 3b 0a  CREATE_TRIGGER;.
29ad2 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
29ad3 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 54  zDb = db->aDb[iT
29ad4 61 62 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  abDb].zName;.   
29ad5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
29ad6 54 72 69 67 20 3d 20 69 73 54 65 6d 70 20 3f 20  Trig = isTemp ? 
29ad7 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65  db->aDb[1].zName
29ad8 20 3a 20 7a 44 62 3b 0a 20 20 20 20 69 66 28 20   : zDb;.    if( 
29ad9 69 54 61 62 44 62 3d 3d 31 20 7c 7c 20 69 73 54  iTabDb==1 || isT
29ada 65 6d 70 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  emp ) code = SQL
29adb 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
29adc 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28  TRIGGER;.    if(
29add 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
29ade 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20  k(pParse, code, 
29adf 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  zName, pTab->zNa
29ae0 6d 65 2c 20 7a 44 62 54 72 69 67 29 20 29 7b 0a  me, zDbTrig) ){.
29ae1 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67        goto trigg
29ae2 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  er_cleanup;.    
29ae3 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
29ae4 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
29ae5 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  e, SQLITE_INSERT
29ae6 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
29ae7 54 61 62 44 62 29 2c 30 2c 7a 44 62 29 29 7b 0a  TabDb),0,zDb)){.
29ae8 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67        goto trigg
29ae9 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  er_cleanup;.    
29aea 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
29aeb 2f 2a 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  /* INSTEAD OF tr
29aec 69 67 67 65 72 73 20 63 61 6e 20 6f 6e 6c 79 20  iggers can only 
29aed 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77 73 20  appear on views 
29aee 61 6e 64 20 42 45 46 4f 52 45 20 74 72 69 67 67  and BEFORE trigg
29aef 65 72 73 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74 20  ers.  ** cannot 
29af0 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77 73 2e  appear on views.
29af1 20 20 53 6f 20 77 65 20 6d 69 67 68 74 20 61 73    So we might as
29af2 20 77 65 6c 6c 20 74 72 61 6e 73 6c 61 74 65 20   well translate 
29af3 65 76 65 72 79 0a 20 20 2a 2a 20 49 4e 53 54 45  every.  ** INSTE
29af4 41 44 20 4f 46 20 74 72 69 67 67 65 72 20 69 6e  AD OF trigger in
29af5 74 6f 20 61 20 42 45 46 4f 52 45 20 74 72 69 67  to a BEFORE trig
29af6 67 65 72 2e 20 20 49 74 20 73 69 6d 70 6c 69 66  ger.  It simplif
29af7 69 65 73 20 63 6f 64 65 0a 20 20 2a 2a 20 65 6c  ies code.  ** el
29af8 73 65 77 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20  sewhere..  */.  
29af9 69 66 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f  if (tr_tm == TK_
29afa 49 4e 53 54 45 41 44 29 7b 0a 20 20 20 20 74 72  INSTEAD){.    tr
29afb 5f 74 6d 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b  _tm = TK_BEFORE;
29afc 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 75 69 6c 64  .  }..  /* Build
29afd 20 74 68 65 20 54 72 69 67 67 65 72 20 6f 62 6a   the Trigger obj
29afe 65 63 74 20 2a 2f 0a 20 20 70 54 72 69 67 67 65  ect */.  pTrigge
29aff 72 20 3d 20 28 54 72 69 67 67 65 72 2a 29 73 71  r = (Trigger*)sq
29b00 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
29b01 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69  o(db, sizeof(Tri
29b02 67 67 65 72 29 29 3b 0a 20 20 69 66 28 20 70 54  gger));.  if( pT
29b03 72 69 67 67 65 72 3d 3d 30 20 29 20 67 6f 74 6f  rigger==0 ) goto
29b04 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
29b05 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 6e 61  ;.  pTrigger->na
29b06 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7a 4e  me = zName;.  zN
29b07 61 6d 65 20 3d 20 30 3b 0a 20 20 70 54 72 69 67  ame = 0;.  pTrig
29b08 67 65 72 2d 3e 74 61 62 6c 65 20 3d 20 73 71 6c  ger->table = sql
29b09 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
29b0a 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 61 5b 30   pTableName->a[0
29b0b 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 70 54 72 69  ].zName);.  pTri
29b0c 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 3d 20  gger->pSchema = 
29b0d 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
29b0e 68 65 6d 61 3b 0a 20 20 70 54 72 69 67 67 65 72  hema;.  pTrigger
29b0f 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 3d 20 70  ->pTabSchema = p
29b10 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20  Tab->pSchema;.  
29b11 70 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d 20 28  pTrigger->op = (
29b12 75 38 29 6f 70 3b 0a 20 20 70 54 72 69 67 67 65  u8)op;.  pTrigge
29b13 72 2d 3e 74 72 5f 74 6d 20 3d 20 74 72 5f 74 6d  r->tr_tm = tr_tm
29b14 3d 3d 54 4b 5f 42 45 46 4f 52 45 20 3f 20 54 52  ==TK_BEFORE ? TR
29b15 49 47 47 45 52 5f 42 45 46 4f 52 45 20 3a 20 54  IGGER_BEFORE : T
29b16 52 49 47 47 45 52 5f 41 46 54 45 52 3b 0a 20 20  RIGGER_AFTER;.  
29b17 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20  pTrigger->pWhen 
29b18 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
29b19 28 64 62 2c 20 70 57 68 65 6e 2c 20 45 58 50 52  (db, pWhen, EXPR
29b1a 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 70  DUP_REDUCE);.  p
29b1b 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e  Trigger->pColumn
29b1c 73 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  s = sqlite3IdLis
29b1d 74 44 75 70 28 64 62 2c 20 70 43 6f 6c 75 6d 6e  tDup(db, pColumn
29b1e 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 6f 6b  s);.  sqlite3Tok
29b1f 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 54 72 69  enCopy(db, &pTri
29b20 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e 2c  gger->nameToken,
29b21 70 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74  pName);.  assert
29b22 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72  ( pParse->pNewTr
29b23 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 70 50  igger==0 );.  pP
29b24 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
29b25 72 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 0a 74  r = pTrigger;..t
29b26 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a 0a  rigger_cleanup:.
29b27 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
29b28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71  db, zName);.  sq
29b29 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
29b2a 74 65 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d  te(db, pTableNam
29b2b 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c  e);.  sqlite3IdL
29b2c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43  istDelete(db, pC
29b2d 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74  olumns);.  sqlit
29b2e 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
29b2f 20 70 57 68 65 6e 29 3b 0a 20 20 69 66 28 20 21   pWhen);.  if( !
29b30 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
29b31 67 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ger ){.    sqlit
29b32 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  e3DeleteTrigger(
29b33 64 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20  db, pTrigger);. 
29b34 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
29b35 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  rt( pParse->pNew
29b36 54 72 69 67 67 65 72 3d 3d 70 54 72 69 67 67 65  Trigger==pTrigge
29b37 72 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  r );.  }.}../*.*
29b38 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
29b39 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
29b3a 6c 6c 20 6f 66 20 74 68 65 20 74 72 69 67 67 65  ll of the trigge
29b3b 72 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62  r actions have b
29b3c 65 65 6e 20 70 61 72 73 65 64 0a 2a 2a 20 69 6e  een parsed.** in
29b3d 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65   order to comple
29b3e 74 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f  te the process o
29b3f 66 20 62 75 69 6c 64 69 6e 67 20 74 68 65 20 74  f building the t
29b40 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  rigger..*/.SQLIT
29b41 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
29b42 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
29b43 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ger(.  Parse *pP
29b44 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
29b45 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
29b46 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
29b47 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 2f 2a  p *pStepList, /*
29b48 20 54 68 65 20 74 72 69 67 67 65 72 65 64 20 70   The triggered p
29b49 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65  rogram */.  Toke
29b4a 6e 20 2a 70 41 6c 6c 20 20 20 20 20 20 20 20 20  n *pAll         
29b4b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 74 68 61      /* Token tha
29b4c 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  t describes the 
29b4d 63 6f 6d 70 6c 65 74 65 20 43 52 45 41 54 45 20  complete CREATE 
29b4e 54 52 49 47 47 45 52 20 2a 2f 0a 29 7b 0a 20 20  TRIGGER */.){.  
29b4f 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d  Trigger *pTrig =
29b50 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
29b51 67 67 65 72 3b 20 20 20 20 2f 2a 20 54 72 69 67  gger;    /* Trig
29b52 67 65 72 20 62 65 69 6e 67 20 66 69 6e 69 73 68  ger being finish
29b53 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  ed */.  char *zN
29b54 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
29b55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b56 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 72 69 67   /* Name of trig
29b57 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ger */.  sqlite3
29b58 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
29b59 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
29b5a 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
29b5b 65 20 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73  e */.  DbFixer s
29b5c 46 69 78 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20  Fix;.  int iDb; 
29b5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b5f 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74  /* Database cont
29b60 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67  aining the trigg
29b61 65 72 20 2a 2f 0a 0a 20 20 70 54 72 69 67 20 3d  er */..  pTrig =
29b62 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
29b63 67 67 65 72 3b 0a 20 20 70 50 61 72 73 65 2d 3e  gger;.  pParse->
29b64 70 4e 65 77 54 72 69 67 67 65 72 20 3d 20 30 3b  pNewTrigger = 0;
29b65 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
29b66 45 72 72 20 7c 7c 20 21 70 54 72 69 67 20 29 20  Err || !pTrig ) 
29b67 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69  goto triggerfini
29b68 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7a 4e  sh_cleanup;.  zN
29b69 61 6d 65 20 3d 20 70 54 72 69 67 2d 3e 6e 61 6d  ame = pTrig->nam
29b6a 65 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  e;.  iDb = sqlit
29b6b 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
29b6c 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69  pParse->db, pTri
29b6d 67 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 70  g->pSchema);.  p
29b6e 54 72 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 20  Trig->step_list 
29b6f 3d 20 70 53 74 65 70 4c 69 73 74 3b 0a 20 20 77  = pStepList;.  w
29b70 68 69 6c 65 28 20 70 53 74 65 70 4c 69 73 74 20  hile( pStepList 
29b71 29 7b 0a 20 20 20 20 70 53 74 65 70 4c 69 73 74  ){.    pStepList
29b72 2d 3e 70 54 72 69 67 20 3d 20 70 54 72 69 67 3b  ->pTrig = pTrig;
29b73 0a 20 20 20 20 70 53 74 65 70 4c 69 73 74 20 3d  .    pStepList =
29b74 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 4e 65 78   pStepList->pNex
29b75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  t;.  }.  if( sql
29b76 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69  ite3FixInit(&sFi
29b77 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20  x, pParse, iDb, 
29b78 22 74 72 69 67 67 65 72 22 2c 20 26 70 54 72 69  "trigger", &pTri
29b79 67 2d 3e 6e 61 6d 65 54 6f 6b 65 6e 29 20 0a 20  g->nameToken) . 
29b7a 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69           && sqli
29b7b 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
29b7c 70 28 26 73 46 69 78 2c 20 70 54 72 69 67 2d 3e  p(&sFix, pTrig->
29b7d 73 74 65 70 5f 6c 69 73 74 29 20 29 7b 0a 20 20  step_list) ){.  
29b7e 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69    goto triggerfi
29b7f 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  nish_cleanup;.  
29b80 7d 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 72  }..  /* if we ar
29b81 65 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69  e not initializi
29b82 6e 67 2c 20 61 6e 64 20 74 68 69 73 20 74 72 69  ng, and this tri
29b83 67 67 65 72 20 69 73 20 6e 6f 74 20 6f 6e 20 61  gger is not on a
29b84 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 0a 20 20   TEMP table, .  
29b85 2a 2a 20 62 75 69 6c 64 20 74 68 65 20 73 71 6c  ** build the sql
29b86 69 74 65 5f 6d 61 73 74 65 72 20 65 6e 74 72 79  ite_master entry
29b87 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d  .  */.  if( !db-
29b88 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
29b89 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 63    Vdbe *v;.    c
29b8a 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20  har *z;..    /* 
29b8b 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  Make an entry in
29b8c 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
29b8d 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  er table */.    
29b8e 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
29b8f 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
29b90 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
29b91 74 72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c  triggerfinish_cl
29b92 65 61 6e 75 70 3b 0a 20 20 20 20 73 71 6c 69 74  eanup;.    sqlit
29b93 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
29b94 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
29b95 20 69 44 62 29 3b 0a 20 20 20 20 7a 20 3d 20 73   iDb);.    z = s
29b96 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
29b97 64 62 2c 20 28 63 68 61 72 2a 29 70 41 6c 6c 2d  db, (char*)pAll-
29b98 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20  >z, pAll->n);.  
29b99 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
29b9a 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
29b9b 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f      "INSERT INTO
29b9c 20 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27 74   %Q.%s VALUES('t
29b9d 72 69 67 67 65 72 27 2c 25 51 2c 25 51 2c 30 2c  rigger',%Q,%Q,0,
29b9e 27 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20  'CREATE TRIGGER 
29b9f 25 71 27 29 22 2c 0a 20 20 20 20 20 20 20 64 62  %q')",.       db
29ba0 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
29ba1 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
29ba2 44 62 29 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20  Db), zName,.    
29ba3 20 20 20 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c     pTrig->table,
29ba4 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   z);.    sqlite3
29ba5 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
29ba6 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
29ba7 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
29ba8 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
29ba9 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
29baa 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44  _ParseSchema, iD
29bab 62 2c 20 30 2c 20 30 2c 20 73 71 6c 69 74 65 33  b, 0, 0, sqlite3
29bac 4d 50 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20  MPrintf(.       
29bad 20 64 62 2c 20 22 74 79 70 65 3d 27 74 72 69 67   db, "type='trig
29bae 67 65 72 27 20 41 4e 44 20 6e 61 6d 65 3d 27 25  ger' AND name='%
29baf 71 27 22 2c 20 7a 4e 61 6d 65 29 2c 20 50 34 5f  q'", zName), P4_
29bb0 44 59 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a 20  DYNAMIC.    );. 
29bb1 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   }..  if( db->in
29bb2 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 54  it.busy ){.    T
29bb3 72 69 67 67 65 72 20 2a 70 4c 69 6e 6b 20 3d 20  rigger *pLink = 
29bb4 70 54 72 69 67 3b 0a 20 20 20 20 48 61 73 68 20  pTrig;.    Hash 
29bb5 2a 70 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44  *pHash = &db->aD
29bb6 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
29bb7 74 72 69 67 48 61 73 68 3b 0a 20 20 20 20 70 54  trigHash;.    pT
29bb8 72 69 67 20 3d 20 73 71 6c 69 74 65 33 48 61 73  rig = sqlite3Has
29bb9 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a  hInsert(pHash, z
29bba 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72  Name, sqlite3Str
29bbb 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 70 54  len30(zName), pT
29bbc 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 70 54  rig);.    if( pT
29bbd 72 69 67 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  rig ){.      db-
29bbe 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
29bbf 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  1;.    }else if(
29bc0 20 70 4c 69 6e 6b 2d 3e 70 53 63 68 65 6d 61 3d   pLink->pSchema=
29bc1 3d 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68 65  =pLink->pTabSche
29bc2 6d 61 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c  ma ){.      Tabl
29bc3 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 69  e *pTab;.      i
29bc4 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
29bc5 72 6c 65 6e 33 30 28 70 4c 69 6e 6b 2d 3e 74 61  rlen30(pLink->ta
29bc6 62 6c 65 29 3b 0a 20 20 20 20 20 20 70 54 61 62  ble);.      pTab
29bc7 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69   = sqlite3HashFi
29bc8 6e 64 28 26 70 4c 69 6e 6b 2d 3e 70 54 61 62 53  nd(&pLink->pTabS
29bc9 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
29bca 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65 2c 20 6e 29  pLink->table, n)
29bcb 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
29bcc 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 20  pTab!=0 );.     
29bcd 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20   pLink->pNext = 
29bce 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a  pTab->pTrigger;.
29bcf 20 20 20 20 20 20 70 54 61 62 2d 3e 70 54 72 69        pTab->pTri
29bd0 67 67 65 72 20 3d 20 70 4c 69 6e 6b 3b 0a 20 20  gger = pLink;.  
29bd1 20 20 7d 0a 20 20 7d 0a 0a 74 72 69 67 67 65 72    }.  }..trigger
29bd2 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a 0a  finish_cleanup:.
29bd3 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
29bd4 72 69 67 67 65 72 28 64 62 2c 20 70 54 72 69 67  rigger(db, pTrig
29bd5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50  );.  assert( !pP
29bd6 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
29bd7 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  r );.  sqlite3De
29bd8 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
29bd9 64 62 2c 20 70 53 74 65 70 4c 69 73 74 29 3b 0a  db, pStepList);.
29bda 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20  }../*.** Make a 
29bdb 63 6f 70 79 20 6f 66 20 61 6c 6c 20 63 6f 6d 70  copy of all comp
29bdc 6f 6e 65 6e 74 73 20 6f 66 20 74 68 65 20 67 69  onents of the gi
29bdd 76 65 6e 20 74 72 69 67 67 65 72 20 73 74 65 70  ven trigger step
29bde 2e 20 20 54 68 69 73 20 68 61 73 0a 2a 2a 20 74  .  This has.** t
29bdf 68 65 20 65 66 66 65 63 74 20 6f 66 20 63 6f 70  he effect of cop
29be0 79 69 6e 67 20 61 6c 6c 20 45 78 70 72 2e 74 6f  ying all Expr.to
29be1 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 20 69 6e 74  ken.z values int
29be2 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
29be3 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65  d.** from sqlite
29be4 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 41 73 20  3_malloc().  As 
29be5 69 6e 69 74 69 61 6c 6c 79 20 63 72 65 61 74 65  initially create
29be6 64 2c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65  d, the Expr.toke
29be7 6e 2e 7a 20 76 61 6c 75 65 73 0a 2a 2a 20 61 6c  n.z values.** al
29be8 6c 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69  l point to the i
29be9 6e 70 75 74 20 73 74 72 69 6e 67 20 74 68 61 74  nput string that
29bea 20 77 61 73 20 66 65 64 20 74 6f 20 74 68 65 20   was fed to the 
29beb 70 61 72 73 65 72 2e 20 20 42 75 74 20 74 68 61  parser.  But tha
29bec 74 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 65  t.** string is e
29bed 70 68 65 6d 65 72 61 6c 20 2d 20 69 74 20 77 69  phemeral - it wi
29bee 6c 6c 20 67 6f 20 61 77 61 79 20 61 73 20 73 6f  ll go away as so
29bef 6f 6e 20 61 73 20 74 68 65 20 73 71 6c 69 74 65  on as the sqlite
29bf0 33 5f 65 78 65 63 28 29 0a 2a 2a 20 63 61 6c 6c  3_exec().** call
29bf1 20 74 68 61 74 20 73 74 61 72 74 65 64 20 74 68   that started th
29bf2 65 20 70 61 72 73 65 72 20 65 78 69 74 73 2e 20  e parser exits. 
29bf3 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61   This routine ma
29bf4 6b 65 73 20 61 20 70 65 72 73 69 73 74 65 6e 74  kes a persistent
29bf5 0a 2a 2a 20 63 6f 70 79 20 6f 66 20 61 6c 6c 20  .** copy of all 
29bf6 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a  the Expr.token.z
29bf7 20 73 74 72 69 6e 67 73 20 73 6f 20 74 68 61 74   strings so that
29bf8 20 74 68 65 20 54 72 69 67 67 65 72 53 74 65 70   the TriggerStep
29bf9 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 69   structure.** wi
29bfa 6c 6c 20 62 65 20 76 61 6c 69 64 20 65 76 65 6e  ll be valid even
29bfb 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74   after the sqlit
29bfc 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 72  e3_exec() call r
29bfd 65 74 75 72 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69  eturns..*/.stati
29bfe 63 20 76 6f 69 64 20 73 71 6c 69 74 65 50 65 72  c void sqlitePer
29bff 73 69 73 74 54 72 69 67 67 65 72 53 74 65 70 28  sistTriggerStep(
29c00 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69  sqlite3 *db, Tri
29c01 67 67 65 72 53 74 65 70 20 2a 70 29 7b 0a 20 20  ggerStep *p){.  
29c02 69 66 28 20 70 2d 3e 74 61 72 67 65 74 2e 7a 20  if( p->target.z 
29c03 29 7b 0a 20 20 20 20 70 2d 3e 74 61 72 67 65 74  ){.    p->target
29c04 2e 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65  .z = (u8*)sqlite
29c05 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
29c06 63 68 61 72 2a 29 70 2d 3e 74 61 72 67 65 74 2e  char*)p->target.
29c07 7a 2c 20 70 2d 3e 74 61 72 67 65 74 2e 6e 29 3b  z, p->target.n);
29c08 0a 20 20 20 20 70 2d 3e 74 61 72 67 65 74 2e 64  .    p->target.d
29c09 79 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66  yn = 1;.  }.  if
29c0a 28 20 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  ( p->pSelect ){.
29c0b 20 20 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77      Select *pNew
29c0c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
29c0d 44 75 70 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65  Dup(db, p->pSele
29c0e 63 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  ct, 1);.    sqli
29c0f 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
29c10 64 62 2c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b  db, p->pSelect);
29c11 0a 20 20 20 20 70 2d 3e 70 53 65 6c 65 63 74 20  .    p->pSelect 
29c12 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 66  = pNew;.  }.  if
29c13 28 20 70 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20  ( p->pWhere ){. 
29c14 20 20 20 45 78 70 72 20 2a 70 4e 65 77 20 3d 20     Expr *pNew = 
29c15 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
29c16 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 45 58  b, p->pWhere, EX
29c17 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20  PRDUP_REDUCE);. 
29c18 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
29c19 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 57 68 65  lete(db, p->pWhe
29c1a 72 65 29 3b 0a 20 20 20 20 70 2d 3e 70 57 68 65  re);.    p->pWhe
29c1b 72 65 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20  re = pNew;.  }. 
29c1c 20 69 66 28 20 70 2d 3e 70 45 78 70 72 4c 69 73   if( p->pExprLis
29c1d 74 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73  t ){.    ExprLis
29c1e 74 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  t *pNew = sqlite
29c1f 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
29c20 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20 31   p->pExprList, 1
29c21 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
29c22 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
29c23 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 3b 0a   p->pExprList);.
29c24 20 20 20 20 70 2d 3e 70 45 78 70 72 4c 69 73 74      p->pExprList
29c25 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69   = pNew;.  }.  i
29c26 66 28 20 70 2d 3e 70 49 64 4c 69 73 74 20 29 7b  f( p->pIdList ){
29c27 0a 20 20 20 20 49 64 4c 69 73 74 20 2a 70 4e 65  .    IdList *pNe
29c28 77 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  w = sqlite3IdLis
29c29 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 49 64 4c  tDup(db, p->pIdL
29c2a 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
29c2b 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
29c2c 2c 20 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a 20  , p->pIdList);. 
29c2d 20 20 20 70 2d 3e 70 49 64 4c 69 73 74 20 3d 20     p->pIdList = 
29c2e 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  pNew;.  }.}../*.
29c2f 2a 2a 20 54 75 72 6e 20 61 20 53 45 4c 45 43 54  ** Turn a SELECT
29c30 20 73 74 61 74 65 6d 65 6e 74 20 28 74 68 61 74   statement (that
29c31 20 74 68 65 20 70 53 65 6c 65 63 74 20 70 61 72   the pSelect par
29c32 61 6d 65 74 65 72 20 70 6f 69 6e 74 73 20 74 6f  ameter points to
29c33 29 20 69 6e 74 6f 0a 2a 2a 20 61 20 74 72 69 67  ) into.** a trig
29c34 67 65 72 20 73 74 65 70 2e 20 20 52 65 74 75 72  ger step.  Retur
29c35 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  n a pointer to a
29c36 20 54 72 69 67 67 65 72 53 74 65 70 20 73 74 72   TriggerStep str
29c37 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
29c38 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
29c39 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
29c3a 20 69 74 20 66 69 6e 64 73 20 61 20 53 45 4c 45   it finds a SELE
29c3b 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 0a  CT statement in.
29c3c 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20 54 52 49  ** body of a TRI
29c3d 47 47 45 52 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54  GGER.  .*/.SQLIT
29c3e 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65  E_PRIVATE Trigge
29c3f 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
29c40 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28  iggerSelectStep(
29c41 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c  sqlite3 *db, Sel
29c42 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20  ect *pSelect){. 
29c43 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54   TriggerStep *pT
29c44 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c  riggerStep = sql
29c45 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
29c46 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67  (db, sizeof(Trig
29c47 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28  gerStep));.  if(
29c48 20 70 54 72 69 67 67 65 72 53 74 65 70 3d 3d 30   pTriggerStep==0
29c49 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33   ) {.    sqlite3
29c4a 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
29c4b 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72   pSelect);.    r
29c4c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
29c4d 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70  pTriggerStep->op
29c4e 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20   = TK_SELECT;.  
29c4f 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53  pTriggerStep->pS
29c50 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 3b  elect = pSelect;
29c51 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
29c52 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66  >orconf = OE_Def
29c53 61 75 6c 74 3b 0a 20 20 73 71 6c 69 74 65 50 65  ault;.  sqlitePe
29c54 72 73 69 73 74 54 72 69 67 67 65 72 53 74 65 70  rsistTriggerStep
29c55 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74 65  (db, pTriggerSte
29c56 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54  p);..  return pT
29c57 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f  riggerStep;.}../
29c58 2a 0a 2a 2a 20 42 75 69 6c 64 20 61 20 74 72 69  *.** Build a tri
29c59 67 67 65 72 20 73 74 65 70 20 6f 75 74 20 6f 66  gger step out of
29c5a 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65   an INSERT state
29c5b 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20  ment.  Return a 
29c5c 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68  pointer.** to th
29c5d 65 20 6e 65 77 20 74 72 69 67 67 65 72 20 73 74  e new trigger st
29c5e 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  ep..**.** The pa
29c5f 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
29c60 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20  routine when it 
29c61 73 65 65 73 20 61 6e 20 49 4e 53 45 52 54 20 69  sees an INSERT i
29c62 6e 73 69 64 65 20 74 68 65 0a 2a 2a 20 62 6f 64  nside the.** bod
29c63 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a  y of a trigger..
29c64 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
29c65 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73  E TriggerStep *s
29c66 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73  qlite3TriggerIns
29c67 65 72 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74  ertStep(.  sqlit
29c68 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f  e3 *db,        /
29c69 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
29c6a 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54  onnection */.  T
29c6b 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65  oken *pTableName
29c6c 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  ,  /* Name of th
29c6d 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  e table into whi
29c6e 63 68 20 77 65 20 69 6e 73 65 72 74 20 2a 2f 0a  ch we insert */.
29c6f 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d    IdList *pColum
29c70 6e 2c 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  n,    /* List of
29c71 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 61 62   columns in pTab
29c72 6c 65 4e 61 6d 65 20 74 6f 20 69 6e 73 65 72 74  leName to insert
29c73 20 69 6e 74 6f 20 2a 2f 0a 20 20 45 78 70 72 4c   into */.  ExprL
29c74 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 2f  ist *pEList,   /
29c75 2a 20 54 68 65 20 56 41 4c 55 45 20 63 6c 61 75  * The VALUE clau
29c76 73 65 3a 20 61 20 6c 69 73 74 20 6f 66 20 76 61  se: a list of va
29c77 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72  lues to be inser
29c78 74 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ted */.  Select 
29c79 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 2f 2a 20  *pSelect,    /* 
29c7a 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
29c7b 6e 74 20 74 68 61 74 20 73 75 70 70 6c 69 65 73  nt that supplies
29c7c 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74   values */.  int
29c7d 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20   orconf         
29c7e 20 2f 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63 74   /* The conflict
29c7f 20 61 6c 67 6f 72 69 74 68 6d 20 28 4f 45 5f 41   algorithm (OE_A
29c80 62 6f 72 74 2c 20 4f 45 5f 52 65 70 6c 61 63 65  bort, OE_Replace
29c81 2c 20 65 74 63 2e 29 20 2a 2f 0a 29 7b 0a 20 20  , etc.) */.){.  
29c82 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72  TriggerStep *pTr
29c83 69 67 67 65 72 53 74 65 70 3b 0a 0a 20 20 61 73  iggerStep;..  as
29c84 73 65 72 74 28 70 45 4c 69 73 74 20 3d 3d 20 30  sert(pEList == 0
29c85 20 7c 7c 20 70 53 65 6c 65 63 74 20 3d 3d 20 30   || pSelect == 0
29c86 29 3b 0a 20 20 61 73 73 65 72 74 28 70 45 4c 69  );.  assert(pELi
29c87 73 74 20 21 3d 20 30 20 7c 7c 20 70 53 65 6c 65  st != 0 || pSele
29c88 63 74 20 21 3d 20 30 20 7c 7c 20 64 62 2d 3e 6d  ct != 0 || db->m
29c89 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 0a 20  allocFailed);.. 
29c8a 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20   pTriggerStep = 
29c8b 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
29c8c 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
29c8d 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20  riggerStep));.  
29c8e 69 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70  if( pTriggerStep
29c8f 20 29 7b 0a 20 20 20 20 70 54 72 69 67 67 65 72   ){.    pTrigger
29c90 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e  Step->op = TK_IN
29c91 53 45 52 54 3b 0a 20 20 20 20 70 54 72 69 67 67  SERT;.    pTrigg
29c92 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20  erStep->pSelect 
29c93 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70  = pSelect;.    p
29c94 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
29c95 67 65 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61  get  = *pTableNa
29c96 6d 65 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  me;.    pTrigger
29c97 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 20 3d 20  Step->pIdList = 
29c98 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 54 72  pColumn;.    pTr
29c99 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72  iggerStep->pExpr
29c9a 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20  List = pEList;. 
29c9b 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
29c9c 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66  >orconf = orconf
29c9d 3b 0a 20 20 20 20 73 71 6c 69 74 65 50 65 72 73  ;.    sqlitePers
29c9e 69 73 74 54 72 69 67 67 65 72 53 74 65 70 28 64  istTriggerStep(d
29c9f 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29  b, pTriggerStep)
29ca0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
29ca1 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
29ca2 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b  te(db, pColumn);
29ca3 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
29ca4 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
29ca5 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  EList);.    sqli
29ca6 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
29ca7 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20  db, pSelect);.  
29ca8 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69  }..  return pTri
29ca9 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a  ggerStep;.}../*.
29caa 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 20 74  ** Construct a t
29cab 72 69 67 67 65 72 20 73 74 65 70 20 74 68 61 74  rigger step that
29cac 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 55   implements an U
29cad 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
29cae 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  and return.** a 
29caf 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20  pointer to that 
29cb0 74 72 69 67 67 65 72 20 73 74 65 70 2e 20 20 54  trigger step.  T
29cb1 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20  he parser calls 
29cb2 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
29cb3 6e 20 69 74 0a 2a 2a 20 73 65 65 73 20 61 6e 20  n it.** sees an 
29cb4 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
29cb5 20 69 6e 73 69 64 65 20 74 68 65 20 62 6f 64 79   inside the body
29cb6 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49   of a CREATE TRI
29cb7 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  GGER..*/.SQLITE_
29cb8 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72 53  PRIVATE TriggerS
29cb9 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67  tep *sqlite3Trig
29cba 67 65 72 55 70 64 61 74 65 53 74 65 70 28 0a 20  gerUpdateStep(. 
29cbb 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
29cbc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
29cbd 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
29cbe 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61   */.  Token *pTa
29cbf 62 6c 65 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61  bleName,   /* Na
29cc0 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
29cc1 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f  to be updated */
29cc2 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
29cc3 69 73 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 53  ist,    /* The S
29cc4 45 54 20 63 6c 61 75 73 65 3a 20 6c 69 73 74 20  ET clause: list 
29cc5 6f 66 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 6e 65  of column and ne
29cc6 77 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 45 78  w values */.  Ex
29cc7 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20  pr *pWhere,     
29cc8 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
29cc9 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
29cca 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20  orconf          
29ccb 20 2f 2a 20 54 68 65 20 63 6f 6e 66 6c 69 63 74   /* The conflict
29ccc 20 61 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f   algorithm. (OE_
29ccd 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65  Abort, OE_Ignore
29cce 2c 20 65 74 63 29 20 2a 2f 0a 29 7b 0a 20 20 54  , etc) */.){.  T
29ccf 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72 69  riggerStep *pTri
29cd0 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74  ggerStep = sqlit
29cd1 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
29cd2 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65  b, sizeof(Trigge
29cd3 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28 20 70  rStep));.  if( p
29cd4 54 72 69 67 67 65 72 53 74 65 70 3d 3d 30 20 29  TriggerStep==0 )
29cd5 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  {.     sqlite3Ex
29cd6 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
29cd7 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 73   pEList);.     s
29cd8 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
29cd9 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
29cda 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
29cdb 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ..  pTriggerStep
29cdc 2d 3e 6f 70 20 3d 20 54 4b 5f 55 50 44 41 54 45  ->op = TK_UPDATE
29cdd 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
29cde 2d 3e 74 61 72 67 65 74 20 20 3d 20 2a 70 54 61  ->target  = *pTa
29cdf 62 6c 65 4e 61 6d 65 3b 0a 20 20 70 54 72 69 67  bleName;.  pTrig
29ce0 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  gerStep->pExprLi
29ce1 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70  st = pEList;.  p
29ce2 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 57 68  TriggerStep->pWh
29ce3 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20  ere = pWhere;.  
29ce4 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
29ce5 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20  conf = orconf;. 
29ce6 20 73 71 6c 69 74 65 50 65 72 73 69 73 74 54 72   sqlitePersistTr
29ce7 69 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54  iggerStep(db, pT
29ce8 72 69 67 67 65 72 53 74 65 70 29 3b 0a 0a 20 20  riggerStep);..  
29ce9 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
29cea 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  tep;.}../*.** Co
29ceb 6e 73 74 72 75 63 74 20 61 20 74 72 69 67 67 65  nstruct a trigge
29cec 72 20 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c  r step that impl
29ced 65 6d 65 6e 74 73 20 61 20 44 45 4c 45 54 45 20  ements a DELETE 
29cee 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
29cef 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  turn.** a pointe
29cf0 72 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65  r to that trigge
29cf1 72 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72  r step.  The par
29cf2 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
29cf3 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a  outine when it.*
29cf4 2a 20 73 65 65 73 20 61 20 44 45 4c 45 54 45 20  * sees a DELETE 
29cf5 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65  statement inside
29cf6 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43   the body of a C
29cf7 52 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a  REATE TRIGGER..*
29cf8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
29cf9 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
29cfa 6c 69 74 65 33 54 72 69 67 67 65 72 44 65 6c 65  lite3TriggerDele
29cfb 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65  teStep(.  sqlite
29cfc 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
29cfd 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
29cfe 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f  nnection */.  To
29cff 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c  ken *pTableName,
29d00 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
29d01 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 72 6f  le from which ro
29d02 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 2a  ws are deleted *
29d03 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
29d04 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
29d05 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
29d06 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53  */.){.  TriggerS
29d07 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
29d08 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
29d09 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
29d0a 6f 66 28 54 72 69 67 67 65 72 53 74 65 70 29 29  of(TriggerStep))
29d0b 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
29d0c 53 74 65 70 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Step==0 ){.    s
29d0d 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
29d0e 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
29d0f 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
29d10 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
29d11 3e 6f 70 20 3d 20 54 4b 5f 44 45 4c 45 54 45 3b  >op = TK_DELETE;
29d12 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
29d13 3e 74 61 72 67 65 74 20 20 3d 20 2a 70 54 61 62  >target  = *pTab
29d14 6c 65 4e 61 6d 65 3b 0a 20 20 70 54 72 69 67 67  leName;.  pTrigg
29d15 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d  erStep->pWhere =
29d16 20 70 57 68 65 72 65 3b 0a 20 20 70 54 72 69 67   pWhere;.  pTrig
29d17 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20  gerStep->orconf 
29d18 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20  = OE_Default;.  
29d19 73 71 6c 69 74 65 50 65 72 73 69 73 74 54 72 69  sqlitePersistTri
29d1a 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54 72  ggerStep(db, pTr
29d1b 69 67 67 65 72 53 74 65 70 29 3b 0a 0a 20 20 72  iggerStep);..  r
29d1c 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74  eturn pTriggerSt
29d1d 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65  ep;.}../* .** Re
29d1e 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65  cursively delete
29d1f 20 61 20 54 72 69 67 67 65 72 20 73 74 72 75 63   a Trigger struc
29d20 74 75 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ture.*/.SQLITE_P
29d21 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
29d22 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
29d23 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72  (sqlite3 *db, Tr
29d24 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29  igger *pTrigger)
29d25 7b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  {.  if( pTrigger
29d26 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
29d27 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
29d28 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54 72  ggerStep(db, pTr
29d29 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74  igger->step_list
29d2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
29d2b 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  ee(db, pTrigger-
29d2c 3e 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  >name);.  sqlite
29d2d 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69  3DbFree(db, pTri
29d2e 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 20  gger->table);.  
29d2f 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
29d30 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e  e(db, pTrigger->
29d31 70 57 68 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65  pWhen);.  sqlite
29d32 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
29d33 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c  , pTrigger->pCol
29d34 75 6d 6e 73 29 3b 0a 20 20 69 66 28 20 70 54 72  umns);.  if( pTr
29d35 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e  igger->nameToken
29d36 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33 44 62  .dyn ) sqlite3Db
29d37 46 72 65 65 28 64 62 2c 20 28 63 68 61 72 2a 29  Free(db, (char*)
29d38 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 54 6f  pTrigger->nameTo
29d39 6b 65 6e 2e 7a 29 3b 0a 20 20 73 71 6c 69 74 65  ken.z);.  sqlite
29d3a 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69  3DbFree(db, pTri
29d3b 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  gger);.}../*.** 
29d3c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
29d3d 20 63 61 6c 6c 65 64 20 74 6f 20 64 72 6f 70 20   called to drop 
29d3e 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74  a trigger from t
29d3f 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
29d40 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ma. .**.** This 
29d41 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 64 69  may be called di
29d42 72 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20  rectly from the 
29d43 70 61 72 73 65 72 20 61 6e 64 20 74 68 65 72 65  parser and there
29d44 66 6f 72 65 20 69 64 65 6e 74 69 66 69 65 73 0a  fore identifies.
29d45 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 62  ** the trigger b
29d46 79 20 6e 61 6d 65 2e 20 20 54 68 65 20 73 71 6c  y name.  The sql
29d47 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 50  ite3DropTriggerP
29d48 74 72 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  tr() routine doe
29d49 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6a 6f  s the.** same jo
29d4a 62 20 61 73 20 74 68 69 73 20 72 6f 75 74 69 6e  b as this routin
29d4b 65 20 65 78 63 65 70 74 20 69 74 20 74 61 6b 65  e except it take
29d4c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
29d4d 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 6e  he trigger.** in
29d4e 73 74 65 61 64 20 6f 66 20 74 68 65 20 74 72 69  stead of the tri
29d4f 67 67 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53  gger name..**/.S
29d50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
29d51 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  id sqlite3DropTr
29d52 69 67 67 65 72 28 50 61 72 73 65 20 2a 70 50 61  igger(Parse *pPa
29d53 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e  rse, SrcList *pN
29d54 61 6d 65 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b  ame, int noErr){
29d55 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
29d56 67 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20  gger = 0;.  int 
29d57 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  i;.  const char 
29d58 2a 7a 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68  *zDb;.  const ch
29d59 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74  ar *zName;.  int
29d5a 20 6e 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65   nName;.  sqlite
29d5b 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
29d5c 64 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d  db;..  if( db->m
29d5d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
29d5e 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72 5f  to drop_trigger_
29d5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 53  cleanup;.  if( S
29d60 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
29d61 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
29d62 73 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  se) ){.    goto 
29d63 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65  drop_trigger_cle
29d64 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  anup;.  }..  ass
29d65 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63  ert( pName->nSrc
29d66 3d 3d 31 20 29 3b 0a 20 20 7a 44 62 20 3d 20 70  ==1 );.  zDb = p
29d67 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61  Name->a[0].zData
29d68 62 61 73 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  base;.  zName = 
29d69 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  pName->a[0].zNam
29d6a 65 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c  e;.  nName = sql
29d6b 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
29d6c 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49  me);.  for(i=OMI
29d6d 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e  T_TEMPDB; i<db->
29d6e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nDb; i++){.    i
29d6f 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69  nt j = (i<2) ? i
29d70 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72  ^1 : i;  /* Sear
29d71 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d  ch TEMP before M
29d72 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a  AIN */.    if( z
29d73 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  Db && sqlite3Str
29d74 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e  ICmp(db->aDb[j].
29d75 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 20 63 6f  zName, zDb) ) co
29d76 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 54 72 69  ntinue;.    pTri
29d77 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61  gger = sqlite3Ha
29d78 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62  shFind(&(db->aDb
29d79 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69  [j].pSchema->tri
29d7a 67 48 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e  gHash), zName, n
29d7b 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Name);.    if( p
29d7c 54 72 69 67 67 65 72 20 29 20 62 72 65 61 6b 3b  Trigger ) break;
29d7d 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 54 72 69  .  }.  if( !pTri
29d7e 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20  gger ){.    if( 
29d7f 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20  !noErr ){.      
29d80 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
29d81 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
29d82 20 74 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70   trigger: %S", p
29d83 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  Name, 0);.    }.
29d84 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72      goto drop_tr
29d85 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
29d86 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70   }.  sqlite3Drop
29d87 54 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73  TriggerPtr(pPars
29d88 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 64  e, pTrigger);..d
29d89 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61  rop_trigger_clea
29d8a 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  nup:.  sqlite3Sr
29d8b 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
29d8c 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pName);.}../*.**
29d8d 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
29d8e 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73  r to the Table s
29d8f 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65  tructure for the
29d90 20 74 61 62 6c 65 20 74 68 61 74 20 61 20 74 72   table that a tr
29d91 69 67 67 65 72 0a 2a 2a 20 69 73 20 73 65 74 20  igger.** is set 
29d92 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61  on..*/.static Ta
29d93 62 6c 65 20 2a 74 61 62 6c 65 4f 66 54 72 69 67  ble *tableOfTrig
29d94 67 65 72 28 54 72 69 67 67 65 72 20 2a 70 54 72  ger(Trigger *pTr
29d95 69 67 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e 20  igger){.  int n 
29d96 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
29d97 30 28 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c  0(pTrigger->tabl
29d98 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  e);.  return sql
29d99 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 70 54  ite3HashFind(&pT
29d9a 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65  rigger->pTabSche
29d9b 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54 72  ma->tblHash, pTr
29d9c 69 67 67 65 72 2d 3e 74 61 62 6c 65 2c 20 6e 29  igger->table, n)
29d9d 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70  ;.}.../*.** Drop
29d9e 20 61 20 74 72 69 67 67 65 72 20 67 69 76 65 6e   a trigger given
29d9f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
29da0 61 74 20 74 72 69 67 67 65 72 2e 20 0a 2a 2f 0a  at trigger. .*/.
29da1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
29da2 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
29da3 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 20  riggerPtr(Parse 
29da4 2a 70 50 61 72 73 65 2c 20 54 72 69 67 67 65 72  *pParse, Trigger
29da5 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 54   *pTrigger){.  T
29da6 61 62 6c 65 20 20 20 2a 70 54 61 62 6c 65 3b 0a  able   *pTable;.
29da7 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c    Vdbe *v;.  sql
29da8 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
29da9 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62  e->db;.  int iDb
29daa 3b 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  ;..  iDb = sqlit
29dab 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
29dac 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69  pParse->db, pTri
29dad 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  gger->pSchema);.
29dae 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
29daf 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
29db0 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 74 61  );.  pTable = ta
29db1 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70 54 72  bleOfTrigger(pTr
29db2 69 67 67 65 72 29 3b 0a 20 20 61 73 73 65 72 74  igger);.  assert
29db3 28 20 70 54 61 62 6c 65 20 29 3b 0a 20 20 61 73  ( pTable );.  as
29db4 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70 53  sert( pTable->pS
29db5 63 68 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d  chema==pTrigger-
29db6 3e 70 53 63 68 65 6d 61 20 7c 7c 20 69 44 62 3d  >pSchema || iDb=
29db7 3d 31 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  =1 );.#ifndef SQ
29db8 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
29db9 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20  IZATION.  {.    
29dba 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  int code = SQLIT
29dbb 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 3b 0a  E_DROP_TRIGGER;.
29dbc 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
29dbd 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
29dbe 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f  b].zName;.    co
29dbf 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d  nst char *zTab =
29dc0 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
29dc1 62 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3d  b);.    if( iDb=
29dc2 3d 31 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49  =1 ) code = SQLI
29dc3 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49  TE_DROP_TEMP_TRI
29dc4 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73 71  GGER;.    if( sq
29dc5 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
29dc6 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 54 72  Parse, code, pTr
29dc7 69 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 70 54 61  igger->name, pTa
29dc8 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29  ble->zName, zDb)
29dc9 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ||.      sqlite
29dca 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
29dcb 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  e, SQLITE_DELETE
29dcc 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20  , zTab, 0, zDb) 
29dcd 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  ){.      return;
29dce 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
29dcf 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  f..  /* Generate
29dd0 20 63 6f 64 65 20 74 6f 20 64 65 73 74 72 6f 79   code to destroy
29dd1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65   the database re
29dd2 63 6f 72 64 20 6f 66 20 74 68 65 20 74 72 69 67  cord of the trig
29dd3 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ger..  */.  asse
29dd4 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20 29 3b  rt( pTable!=0 );
29dd5 0a 20 20 69 66 28 20 28 76 20 3d 20 73 71 6c 69  .  if( (v = sqli
29dd6 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
29dd7 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  e))!=0 ){.    in
29dd8 74 20 62 61 73 65 3b 0a 20 20 20 20 73 74 61 74  t base;.    stat
29dd9 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
29dda 69 73 74 20 64 72 6f 70 54 72 69 67 67 65 72 5b  ist dropTrigger[
29ddb 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50  ] = {.      { OP
29ddc 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 30 2c 20  _Rewind,     0, 
29ddd 41 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a 20 20  ADDR(9),  0},.  
29dde 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38      { OP_String8
29ddf 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20  ,    0, 1,      
29de0 20 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a 20 20    0}, /* 1 */.  
29de1 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c      { OP_Column,
29de2 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20       0, 1,      
29de3 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    2},.      { OP
29de4 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c 20  _Ne,         2, 
29de5 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20  ADDR(8),  1},.  
29de6 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38      { OP_String8
29de7 2c 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20  ,    0, 1,      
29de8 20 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74 72 69    0}, /* 4: "tri
29de9 67 67 65 72 22 20 2a 2f 0a 20 20 20 20 20 20 7b  gger" */.      {
29dea 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20   OP_Column,     
29deb 30 2c 20 30 2c 20 20 20 20 20 20 20 20 32 7d 2c  0, 0,        2},
29dec 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20  .      { OP_Ne, 
29ded 20 20 20 20 20 20 20 20 32 2c 20 41 44 44 52 28          2, ADDR(
29dee 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b  8),  1},.      {
29def 20 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20 20 20   OP_Delete,     
29df0 30 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 0,        0},
29df1 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74  .      { OP_Next
29df2 2c 20 20 20 20 20 20 20 30 2c 20 41 44 44 52 28  ,       0, ADDR(
29df3 31 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20 2a 2f  1),  0}, /* 8 */
29df4 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73 71 6c  .    };..    sql
29df5 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
29df6 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
29df7 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  0, iDb);.    sql
29df8 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61  ite3OpenMasterTa
29df9 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  ble(pParse, iDb)
29dfa 3b 0a 20 20 20 20 62 61 73 65 20 3d 20 73 71 6c  ;.    base = sql
29dfb 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
29dfc 74 28 76 2c 20 20 41 72 72 61 79 53 69 7a 65 28  t(v,  ArraySize(
29dfd 64 72 6f 70 54 72 69 67 67 65 72 29 2c 20 64 72  dropTrigger), dr
29dfe 6f 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  opTrigger);.    
29dff 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29e00 65 50 34 28 76 2c 20 62 61 73 65 2b 31 2c 20 70  eP4(v, base+1, p
29e01 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 30  Trigger->name, 0
29e02 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
29e03 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61  beChangeP4(v, ba
29e04 73 65 2b 34 2c 20 22 74 72 69 67 67 65 72 22 2c  se+4, "trigger",
29e05 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
29e06 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
29e07 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62  okie(pParse, iDb
29e08 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
29e09 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
29e0a 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20  lose, 0, 0);.   
29e0b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29e0c 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69  p4(v, OP_DropTri
29e0d 67 67 65 72 2c 20 69 44 62 2c 20 30 2c 20 30 2c  gger, iDb, 0, 0,
29e0e 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 2c   pTrigger->name,
29e0f 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61   0);.    if( pPa
29e10 72 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29 7b 0a 20  rse->nMem<3 ){. 
29e11 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
29e12 6d 20 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 7d  m = 3;.    }.  }
29e13 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
29e14 20 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20   a trigger from 
29e15 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 73 20  the hash tables 
29e16 6f 66 20 74 68 65 20 73 71 6c 69 74 65 2a 20 70  of the sqlite* p
29e17 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  ointer..*/.SQLIT
29e18 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
29e19 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
29e1a 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c  eleteTrigger(sql
29e1b 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44  ite3 *db, int iD
29e1c 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
29e1d 4e 61 6d 65 29 7b 0a 20 20 48 61 73 68 20 2a 70  Name){.  Hash *p
29e1e 48 61 73 68 20 3d 20 26 28 64 62 2d 3e 61 44 62  Hash = &(db->aDb
29e1f 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  [iDb].pSchema->t
29e20 72 69 67 48 61 73 68 29 3b 0a 20 20 54 72 69 67  rigHash);.  Trig
29e21 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20  ger *pTrigger;. 
29e22 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69   pTrigger = sqli
29e23 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 70 48  te3HashInsert(pH
29e24 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69  ash, zName, sqli
29e25 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
29e26 65 29 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 54  e), 0);.  if( pT
29e27 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66  rigger ){.    if
29e28 28 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68  ( pTrigger->pSch
29e29 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e 70  ema==pTrigger->p
29e2a 54 61 62 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  TabSchema ){.   
29e2b 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
29e2c 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28   tableOfTrigger(
29e2d 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20  pTrigger);.     
29e2e 20 54 72 69 67 67 65 72 20 2a 2a 70 70 3b 0a 20   Trigger **pp;. 
29e2f 20 20 20 20 20 66 6f 72 28 70 70 3d 26 70 54 61       for(pp=&pTa
29e30 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 2a 70 70  b->pTrigger; *pp
29e31 21 3d 70 54 72 69 67 67 65 72 3b 20 70 70 3d 26  !=pTrigger; pp=&
29e32 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b  ((*pp)->pNext));
29e33 0a 20 20 20 20 20 20 2a 70 70 20 3d 20 28 2a 70  .      *pp = (*p
29e34 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  p)->pNext;.    }
29e35 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
29e36 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 54  teTrigger(db, pT
29e37 72 69 67 67 65 72 29 3b 0a 20 20 20 20 64 62 2d  rigger);.    db-
29e38 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
29e39 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a  _InternChanges;.
29e3a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c    }.}../*.** pEL
29e3b 69 73 74 20 69 73 20 74 68 65 20 53 45 54 20 63  ist is the SET c
29e3c 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41  lause of an UPDA
29e3d 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 45  TE statement.  E
29e3e 61 63 68 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20  ach entry.** in 
29e3f 70 45 4c 69 73 74 20 69 73 20 6f 66 20 74 68 65  pEList is of the
29e40 20 66 6f 72 6d 61 74 20 3c 69 64 3e 3d 3c 65 78   format <id>=<ex
29e41 70 72 3e 2e 20 20 49 66 20 61 6e 79 20 6f 66 20  pr>.  If any of 
29e42 74 68 65 20 65 6e 74 72 69 65 73 0a 2a 2a 20 69  the entries.** i
29e43 6e 20 70 45 4c 69 73 74 20 68 61 76 65 20 61 6e  n pEList have an
29e44 20 3c 69 64 3e 20 77 68 69 63 68 20 6d 61 74 63   <id> which matc
29e45 68 65 73 20 61 6e 20 69 64 65 6e 74 69 66 69 65  hes an identifie
29e46 72 20 69 6e 20 70 49 64 4c 69 73 74 2c 0a 2a 2a  r in pIdList,.**
29e47 20 74 68 65 6e 20 72 65 74 75 72 6e 20 54 52 55   then return TRU
29e48 45 2e 20 20 49 66 20 70 49 64 4c 69 73 74 3d 3d  E.  If pIdList==
29e49 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73  NULL, then it is
29e4a 20 63 6f 6e 73 69 64 65 72 65 64 20 61 0a 2a 2a   considered a.**
29e4b 20 77 69 6c 64 63 61 72 64 20 74 68 61 74 20 6d   wildcard that m
29e4c 61 74 63 68 65 73 20 61 6e 79 74 68 69 6e 67 2e  atches anything.
29e4d 20 20 4c 69 6b 65 77 69 73 65 20 69 66 20 70 45    Likewise if pE
29e4e 4c 69 73 74 3d 3d 4e 55 4c 4c 20 74 68 65 6e 0a  List==NULL then.
29e4f 2a 2a 20 69 74 20 6d 61 74 63 68 65 73 20 61 6e  ** it matches an
29e50 79 74 68 69 6e 67 20 73 6f 20 61 6c 77 61 79 73  ything so always
29e51 20 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20 52   return true.  R
29e52 65 74 75 72 6e 20 66 61 6c 73 65 20 6f 6e 6c 79  eturn false only
29e53 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20  .** if there is 
29e54 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 61  no match..*/.sta
29e55 74 69 63 20 69 6e 74 20 63 68 65 63 6b 43 6f 6c  tic int checkCol
29e56 75 6d 6e 4f 76 65 72 4c 61 70 28 49 64 4c 69 73  umnOverLap(IdLis
29e57 74 20 2a 70 49 64 4c 69 73 74 2c 20 45 78 70 72  t *pIdList, Expr
29e58 4c 69 73 74 20 2a 70 45 4c 69 73 74 29 7b 0a 20  List *pEList){. 
29e59 20 69 6e 74 20 65 3b 0a 20 20 69 66 28 20 21 70   int e;.  if( !p
29e5a 49 64 4c 69 73 74 20 7c 7c 20 21 70 45 4c 69 73  IdList || !pELis
29e5b 74 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  t ) return 1;.  
29e5c 66 6f 72 28 65 3d 30 3b 20 65 3c 70 45 4c 69 73  for(e=0; e<pELis
29e5d 74 2d 3e 6e 45 78 70 72 3b 20 65 2b 2b 29 7b 0a  t->nExpr; e++){.
29e5e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49      if( sqlite3I
29e5f 64 4c 69 73 74 49 6e 64 65 78 28 70 49 64 4c 69  dListIndex(pIdLi
29e60 73 74 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 65 5d  st, pEList->a[e]
29e61 2e 7a 4e 61 6d 65 29 3e 3d 30 20 29 20 72 65 74  .zName)>=0 ) ret
29e62 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 1;.  }.  ret
29e63 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn 0; .}../*.**
29e64 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74 20 6f   Return a list o
29e65 66 20 61 6c 6c 20 74 72 69 67 67 65 72 73 20 6f  f all triggers o
29e66 6e 20 74 61 62 6c 65 20 70 54 61 62 20 69 66 20  n table pTab if 
29e67 74 68 65 72 65 20 65 78 69 73 74 73 20 61 74 20  there exists at 
29e68 6c 65 61 73 74 0a 2a 2a 20 6f 6e 65 20 74 72 69  least.** one tri
29e69 67 67 65 72 20 74 68 61 74 20 6d 75 73 74 20 62  gger that must b
29e6a 65 20 66 69 72 65 64 20 77 68 65 6e 20 61 6e 20  e fired when an 
29e6b 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 79 70  operation of typ
29e6c 65 20 27 6f 70 27 20 69 73 20 0a 2a 2a 20 70 65  e 'op' is .** pe
29e6d 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 74  rformed on the t
29e6e 61 62 6c 65 2c 20 61 6e 64 2c 20 69 66 20 74 68  able, and, if th
29e6f 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  at operation is 
29e70 61 6e 20 55 50 44 41 54 45 2c 20 69 66 20 61 74  an UPDATE, if at
29e71 0a 2a 2a 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66  .** least one of
29e72 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   the columns in 
29e73 70 43 68 61 6e 67 65 73 20 69 73 20 62 65 69 6e  pChanges is bein
29e74 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2f 0a 53  g modified..*/.S
29e75 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72  QLITE_PRIVATE Tr
29e76 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72  igger *sqlite3Tr
29e77 69 67 67 65 72 73 45 78 69 73 74 28 0a 20 20 50  iggersExist(.  P
29e78 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
29e79 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
29e7a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
29e7b 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
29e7c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
29e7d 65 20 74 68 65 20 63 6f 6e 74 61 69 6e 73 20 74  e the contains t
29e7e 68 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  he triggers */. 
29e7f 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
29e80 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 65 20           /* one 
29e81 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b  of TK_DELETE, TK
29e82 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50 44 41  _INSERT, TK_UPDA
29e83 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  TE */.  ExprList
29e84 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20 20 20   *pChanges,     
29e85 2f 2a 20 43 6f 6c 75 6d 6e 73 20 74 68 61 74 20  /* Columns that 
29e86 63 68 61 6e 67 65 20 69 6e 20 61 6e 20 55 50 44  change in an UPD
29e87 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ATE statement */
29e88 0a 20 20 69 6e 74 20 2a 70 4d 61 73 6b 20 20 20  .  int *pMask   
29e89 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
29e8a 54 3a 20 4d 61 73 6b 20 6f 66 20 54 52 49 47 47  T: Mask of TRIGG
29e8b 45 52 5f 42 45 46 4f 52 45 7c 54 52 49 47 47 45  ER_BEFORE|TRIGGE
29e8c 52 5f 41 46 54 45 52 20 2a 2f 0a 29 7b 0a 20 20  R_AFTER */.){.  
29e8d 69 6e 74 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20  int mask = 0;.  
29e8e 54 72 69 67 67 65 72 20 2a 70 4c 69 73 74 20 3d  Trigger *pList =
29e8f 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c   sqlite3TriggerL
29e90 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
29e91 29 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b  );.  Trigger *p;
29e92 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74  .  assert( pList
29e93 3d 3d 30 20 7c 7c 20 49 73 56 69 72 74 75 61 6c  ==0 || IsVirtual
29e94 28 70 54 61 62 29 3d 3d 30 20 29 3b 0a 20 20 66  (pTab)==0 );.  f
29e95 6f 72 28 70 3d 70 4c 69 73 74 3b 20 70 3b 20 70  or(p=pList; p; p
29e96 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
29e97 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20 26 26  if( p->op==op &&
29e98 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72   checkColumnOver
29e99 4c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c  Lap(p->pColumns,
29e9a 20 70 43 68 61 6e 67 65 73 29 20 29 7b 0a 20 20   pChanges) ){.  
29e9b 20 20 20 20 6d 61 73 6b 20 7c 3d 20 70 2d 3e 74      mask |= p->t
29e9c 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  r_tm;.    }.  }.
29e9d 20 20 69 66 28 20 70 4d 61 73 6b 20 29 7b 0a 20    if( pMask ){. 
29e9e 20 20 20 2a 70 4d 61 73 6b 20 3d 20 6d 61 73 6b     *pMask = mask
29e9f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
29ea0 6d 61 73 6b 20 3f 20 70 4c 69 73 74 20 3a 20 30  mask ? pList : 0
29ea1 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  );.}../*.** Conv
29ea2 65 72 74 20 74 68 65 20 70 53 74 65 70 2d 3e 74  ert the pStep->t
29ea3 61 72 67 65 74 20 74 6f 6b 65 6e 20 69 6e 74 6f  arget token into
29ea4 20 61 20 53 72 63 4c 69 73 74 20 61 6e 64 20 72   a SrcList and r
29ea5 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
29ea6 2a 2a 20 74 6f 20 74 68 61 74 20 53 72 63 4c 69  ** to that SrcLi
29ea7 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  st..**.** This r
29ea8 6f 75 74 69 6e 65 20 61 64 64 73 20 61 20 73 70  outine adds a sp
29ea9 65 63 69 66 69 63 20 64 61 74 61 62 61 73 65 20  ecific database 
29eaa 6e 61 6d 65 2c 20 69 66 20 6e 65 65 64 65 64 2c  name, if needed,
29eab 20 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 77   to the target w
29eac 68 65 6e 0a 2a 2a 20 66 6f 72 6d 69 6e 67 20 74  hen.** forming t
29ead 68 65 20 53 72 63 4c 69 73 74 2e 20 20 54 68 69  he SrcList.  Thi
29eae 73 20 70 72 65 76 65 6e 74 73 20 61 20 74 72 69  s prevents a tri
29eaf 67 67 65 72 20 69 6e 20 6f 6e 65 20 64 61 74 61  gger in one data
29eb0 62 61 73 65 20 66 72 6f 6d 0a 2a 2a 20 72 65 66  base from.** ref
29eb1 65 72 72 69 6e 67 20 74 6f 20 61 20 74 61 72 67  erring to a targ
29eb2 65 74 20 69 6e 20 61 6e 6f 74 68 65 72 20 64 61  et in another da
29eb3 74 61 62 61 73 65 2e 20 20 41 6e 20 65 78 63 65  tabase.  An exce
29eb4 70 74 69 6f 6e 20 69 73 20 77 68 65 6e 20 74 68  ption is when th
29eb5 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 69 73 20  e.** trigger is 
29eb6 69 6e 20 54 45 4d 50 20 69 6e 20 77 68 69 63 68  in TEMP in which
29eb7 20 63 61 73 65 20 69 74 20 63 61 6e 20 72 65 66   case it can ref
29eb8 65 72 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 20  er to any other 
29eb9 64 61 74 61 62 61 73 65 20 69 74 0a 2a 2a 20 77  database it.** w
29eba 61 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ants..*/.static 
29ebb 53 72 63 4c 69 73 74 20 2a 74 61 72 67 65 74 53  SrcList *targetS
29ebc 72 63 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20  rcList(.  Parse 
29ebd 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
29ebe 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
29ebf 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67  ntext */.  Trigg
29ec0 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 20 20  erStep *pStep   
29ec1 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 63  /* The trigger c
29ec2 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
29ec3 72 67 65 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b  rget token */.){
29ec4 0a 20 20 54 6f 6b 65 6e 20 73 44 62 3b 20 20 20  .  Token sDb;   
29ec5 20 20 20 20 20 20 20 20 2f 2a 20 44 75 6d 6d 79          /* Dummy
29ec6 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 74   database name t
29ec7 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44  oken */.  int iD
29ec8 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
29ec9 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * Index of the d
29eca 61 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 2a  atabase to use *
29ecb 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
29ecc 63 3b 20 20 20 20 20 20 20 2f 2a 20 53 72 63 4c  c;       /* SrcL
29ecd 69 73 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e  ist to be return
29ece 65 64 20 2a 2f 0a 0a 20 20 69 44 62 20 3d 20 73  ed */..  iDb = s
29ecf 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
29ed0 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
29ed1 70 53 74 65 70 2d 3e 70 54 72 69 67 2d 3e 70 53  pStep->pTrig->pS
29ed2 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 69 44  chema);.  if( iD
29ed3 62 3d 3d 30 20 7c 7c 20 69 44 62 3e 3d 32 20 29  b==0 || iDb>=2 )
29ed4 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  {.    assert( iD
29ed5 62 3c 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44  b<pParse->db->nD
29ed6 62 20 29 3b 0a 20 20 20 20 73 44 62 2e 7a 20 3d  b );.    sDb.z =
29ed7 20 28 75 38 2a 29 70 50 61 72 73 65 2d 3e 64 62   (u8*)pParse->db
29ed8 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
29ed9 3b 0a 20 20 20 20 73 44 62 2e 6e 20 3d 20 73 71  ;.    sDb.n = sq
29eda 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 28 63  lite3Strlen30((c
29edb 68 61 72 2a 29 73 44 62 2e 7a 29 3b 0a 20 20 20  har*)sDb.z);.   
29edc 20 73 44 62 2e 71 75 6f 74 65 64 20 3d 20 30 3b   sDb.quoted = 0;
29edd 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c 69  .    pSrc = sqli
29ede 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
29edf 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 20  (pParse->db, 0, 
29ee0 26 73 44 62 2c 20 26 70 53 74 65 70 2d 3e 74 61  &sDb, &pStep->ta
29ee1 72 67 65 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20  rget);.  } else 
29ee2 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 73 71 6c  {.    pSrc = sql
29ee3 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
29ee4 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c  d(pParse->db, 0,
29ee5 20 26 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2c   &pStep->target,
29ee6 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
29ee7 6e 20 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pSrc;.}../*.**
29ee8 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
29ee9 6f 64 65 20 66 6f 72 20 7a 65 72 6f 20 6f 72 20  ode for zero or 
29eea 6d 6f 72 65 20 73 74 61 74 65 6d 65 6e 74 73 20  more statements 
29eeb 69 6e 73 69 64 65 20 74 68 65 20 62 6f 64 79 20  inside the body 
29eec 6f 66 20 61 0a 2a 2a 20 74 72 69 67 67 65 72 2e  of a.** trigger.
29eed 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74    .*/.static int
29eee 20 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67   codeTriggerProg
29eef 72 61 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ram(.  Parse *pP
29ef0 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
29ef1 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
29ef2 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67  ontext */.  Trig
29ef3 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 4c 69  gerStep *pStepLi
29ef4 73 74 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  st,   /* List of
29ef5 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69   statements insi
29ef6 64 65 20 74 68 65 20 74 72 69 67 67 65 72 20 62  de the trigger b
29ef7 6f 64 79 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63  ody */.  int orc
29ef8 6f 6e 66 69 6e 20 20 20 20 20 20 20 20 20 20 20  onfin           
29ef9 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 61     /* Conflict a
29efa 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62  lgorithm. (OE_Ab
29efb 6f 72 74 2c 20 65 74 63 29 20 2a 2f 20 20 0a 29  ort, etc) */  .)
29efc 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
29efd 2a 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d  * pTriggerStep =
29efe 20 70 53 74 65 70 4c 69 73 74 3b 0a 20 20 69 6e   pStepList;.  in
29eff 74 20 6f 72 63 6f 6e 66 3b 0a 20 20 56 64 62 65  t orconf;.  Vdbe
29f00 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
29f01 64 62 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  dbe;.  sqlite3 *
29f02 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
29f03 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 72 69  ..  assert( pTri
29f04 67 67 65 72 53 74 65 70 21 3d 30 20 29 3b 0a 20  ggerStep!=0 );. 
29f05 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
29f06 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
29f07 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 6e 74 65  dOp2(v, OP_Conte
29f08 78 74 50 75 73 68 2c 20 30 2c 20 30 29 3b 0a 20  xtPush, 0, 0);. 
29f09 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
29f0a 20 22 62 65 67 69 6e 20 74 72 69 67 67 65 72 20   "begin trigger 
29f0b 25 73 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e  %s", pStepList->
29f0c 70 54 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 20  pTrig->name));. 
29f0d 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65 72   while( pTrigger
29f0e 53 74 65 70 20 29 7b 0a 20 20 20 20 73 71 6c 69  Step ){.    sqli
29f0f 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
29f10 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 6f  r(pParse);.    o
29f11 72 63 6f 6e 66 20 3d 20 28 6f 72 63 6f 6e 66 69  rconf = (orconfi
29f12 6e 20 3d 3d 20 4f 45 5f 44 65 66 61 75 6c 74 29  n == OE_Default)
29f13 3f 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f  ?pTriggerStep->o
29f14 72 63 6f 6e 66 3a 6f 72 63 6f 6e 66 69 6e 3b 0a  rconf:orconfin;.
29f15 20 20 20 20 70 50 61 72 73 65 2d 3e 74 72 69 67      pParse->trig
29f16 53 74 61 63 6b 2d 3e 6f 72 63 6f 6e 66 20 3d 20  Stack->orconf = 
29f17 6f 72 63 6f 6e 66 3b 0a 20 20 20 20 73 77 69 74  orconf;.    swit
29f18 63 68 28 20 70 54 72 69 67 67 65 72 53 74 65 70  ch( pTriggerStep
29f19 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61  ->op ){.      ca
29f1a 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a  se TK_SELECT: {.
29f1b 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a          Select *
29f1c 73 73 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  ss = sqlite3Sele
29f1d 63 74 44 75 70 28 64 62 2c 20 70 54 72 69 67 67  ctDup(db, pTrigg
29f1e 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 2c  erStep->pSelect,
29f1f 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
29f20 20 73 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20   ss ){.         
29f21 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
29f22 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ;..          sql
29f23 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
29f24 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 44 69  it(&dest, SRT_Di
29f25 73 63 61 72 64 2c 20 30 29 3b 0a 20 20 20 20 20  scard, 0);.     
29f26 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
29f27 63 74 28 70 50 61 72 73 65 2c 20 73 73 2c 20 26  ct(pParse, ss, &
29f28 64 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 20  dest);.         
29f29 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
29f2a 6c 65 74 65 28 64 62 2c 20 73 73 29 3b 0a 20 20  lete(db, ss);.  
29f2b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
29f2c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
29f2d 20 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 44       case TK_UPD
29f2e 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 53  ATE: {.        S
29f2f 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20  rcList *pSrc;.  
29f30 20 20 20 20 20 20 70 53 72 63 20 3d 20 74 61 72        pSrc = tar
29f31 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72 73  getSrcList(pPars
29f32 65 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29  e, pTriggerStep)
29f33 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29f34 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29f35 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 30 2c  P_ResetCount, 0,
29f36 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
29f37 69 74 65 33 55 70 64 61 74 65 28 70 50 61 72 73  ite3Update(pPars
29f38 65 2c 20 70 53 72 63 2c 0a 20 20 20 20 20 20 20  e, pSrc,.       
29f39 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29f3a 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
29f3b 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45  pTriggerStep->pE
29f3c 78 70 72 4c 69 73 74 2c 20 30 29 2c 20 0a 20 20  xprList, 0), .  
29f3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
29f3e 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
29f3f 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
29f40 57 68 65 72 65 2c 20 30 29 2c 20 6f 72 63 6f 6e  Where, 0), orcon
29f41 66 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  f);.        sqli
29f42 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29f43 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20   OP_ResetCount, 
29f44 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62  1, 0);.        b
29f45 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
29f46 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 53 45      case TK_INSE
29f47 52 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72  RT: {.        Sr
29f48 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 20  cList *pSrc;.   
29f49 20 20 20 20 20 70 53 72 63 20 3d 20 74 61 72 67       pSrc = targ
29f4a 65 74 53 72 63 4c 69 73 74 28 70 50 61 72 73 65  etSrcList(pParse
29f4b 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29 3b  , pTriggerStep);
29f4c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29f4d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29f4e 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 30 2c 20  _ResetCount, 0, 
29f4f 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
29f50 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73 65  te3Insert(pParse
29f51 2c 20 70 53 72 63 2c 0a 20 20 20 20 20 20 20 20  , pSrc,.        
29f52 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
29f53 74 44 75 70 28 64 62 2c 20 70 54 72 69 67 67 65  tDup(db, pTrigge
29f54 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74  rStep->pExprList
29f55 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20  , 0), .         
29f56 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
29f57 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74  p(db, pTriggerSt
29f58 65 70 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 2c  ep->pSelect, 0),
29f59 20 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69   .          sqli
29f5a 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c  te3IdListDup(db,
29f5b 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
29f5c 49 64 4c 69 73 74 29 2c 20 6f 72 63 6f 6e 66 29  IdList), orconf)
29f5d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29f5e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29f5f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 31 2c  P_ResetCount, 1,
29f60 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65   0);.        bre
29f61 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
29f62 20 20 63 61 73 65 20 54 4b 5f 44 45 4c 45 54 45    case TK_DELETE
29f63 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c  : {.        SrcL
29f64 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 20 20 20  ist *pSrc;.     
29f65 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29f66 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65 74  dOp2(v, OP_Reset
29f67 43 6f 75 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20  Count, 0, 0);.  
29f68 20 20 20 20 20 20 70 53 72 63 20 3d 20 74 61 72        pSrc = tar
29f69 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72 73  getSrcList(pPars
29f6a 65 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 29  e, pTriggerStep)
29f6b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29f6c 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72  3DeleteFrom(pPar
29f6d 73 65 2c 20 70 53 72 63 2c 20 0a 20 20 20 20 20  se, pSrc, .     
29f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f6f 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
29f70 44 75 70 28 64 62 2c 20 70 54 72 69 67 67 65 72  Dup(db, pTrigger
29f71 53 74 65 70 2d 3e 70 57 68 65 72 65 2c 20 30 29  Step->pWhere, 0)
29f72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29f73 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29f74 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 31  OP_ResetCount, 1
29f75 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62 72  , 0);.        br
29f76 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
29f77 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
29f78 20 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20      assert(0);. 
29f79 20 20 20 7d 20 0a 20 20 20 20 70 54 72 69 67 67     } .    pTrigg
29f7a 65 72 53 74 65 70 20 3d 20 70 54 72 69 67 67 65  erStep = pTrigge
29f7b 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  rStep->pNext;.  
29f7c 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
29f7d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 6e 74  ddOp2(v, OP_Cont
29f7e 65 78 74 50 6f 70 2c 20 30 2c 20 30 29 3b 0a 20  extPop, 0, 0);. 
29f7f 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
29f80 20 22 65 6e 64 20 74 72 69 67 67 65 72 20 25 73   "end trigger %s
29f81 22 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 54  ", pStepList->pT
29f82 72 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 0a 20 20  rig->name));..  
29f83 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
29f84 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  ** This is calle
29f85 64 20 74 6f 20 63 6f 64 65 20 46 4f 52 20 45 41  d to code FOR EA
29f86 43 48 20 52 4f 57 20 74 72 69 67 67 65 72 73 2e  CH ROW triggers.
29f87 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
29f88 63 6f 64 65 20 74 68 61 74 20 74 68 69 73 20 66  code that this f
29f89 75 6e 63 74 69 6f 6e 20 67 65 6e 65 72 61 74 65  unction generate
29f8a 73 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 74  s is executed, t
29f8b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a  he following .**
29f8c 20 6d 75 73 74 20 62 65 20 74 72 75 65 3a 0a 2a   must be true:.*
29f8d 2a 0a 2a 2a 20 31 2e 20 4e 6f 20 63 75 72 73 6f  *.** 1. No curso
29f8e 72 73 20 6d 61 79 20 62 65 20 6f 70 65 6e 20 69  rs may be open i
29f8f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
29f90 61 73 65 2e 20 20 28 42 75 74 20 6e 65 77 49 64  ase.  (But newId
29f91 78 20 61 6e 64 20 6f 6c 64 49 64 78 0a 2a 2a 20  x and oldIdx.** 
29f92 20 20 20 63 61 6e 20 62 65 20 69 6e 64 69 63 65     can be indice
29f93 73 20 6f 66 20 63 75 72 73 6f 72 73 20 69 6e 20  s of cursors in 
29f94 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
29f95 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 29 0a 2a  .  See below.).*
29f96 2a 0a 2a 2a 20 32 2e 20 49 66 20 74 68 65 20 74  *.** 2. If the t
29f97 72 69 67 67 65 72 73 20 62 65 69 6e 67 20 63 6f  riggers being co
29f98 64 65 64 20 61 72 65 20 4f 4e 20 49 4e 53 45 52  ded are ON INSER
29f99 54 20 6f 72 20 4f 4e 20 55 50 44 41 54 45 20 74  T or ON UPDATE t
29f9a 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a 2a 2a  riggers, then.**
29f9b 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72 79 20      a temporary 
29f9c 76 64 62 65 20 63 75 72 73 6f 72 20 28 69 6e 64  vdbe cursor (ind
29f9d 65 78 20 6e 65 77 49 64 78 29 20 6d 75 73 74 20  ex newIdx) must 
29f9e 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f 69 6e  be open and poin
29f9f 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20 61 20  ting at.**    a 
29fa0 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76  row containing v
29fa1 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75 62 73  alues to be subs
29fa2 74 69 74 75 74 65 64 20 66 6f 72 20 6e 65 77 2e  tituted for new.
29fa3 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  * expressions in
29fa4 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69 67 67   the.**    trigg
29fa5 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e 0a 2a  er program(s)..*
29fa6 2a 0a 2a 2a 20 33 2e 20 49 66 20 74 68 65 20 74  *.** 3. If the t
29fa7 72 69 67 67 65 72 73 20 62 65 69 6e 67 20 63 6f  riggers being co
29fa8 64 65 64 20 61 72 65 20 4f 4e 20 44 45 4c 45 54  ded are ON DELET
29fa9 45 20 6f 72 20 4f 4e 20 55 50 44 41 54 45 20 74  E or ON UPDATE t
29faa 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a 2a 2a  riggers, then.**
29fab 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72 79 20      a temporary 
29fac 76 64 62 65 20 63 75 72 73 6f 72 20 28 69 6e 64  vdbe cursor (ind
29fad 65 78 20 6f 6c 64 49 64 78 29 20 6d 75 73 74 20  ex oldIdx) must 
29fae 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f 69 6e  be open and poin
29faf 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20 61 20  ting at.**    a 
29fb0 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76  row containing v
29fb1 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75 62 73  alues to be subs
29fb2 74 69 74 75 74 65 64 20 66 6f 72 20 6f 6c 64 2e  tituted for old.
29fb3 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  * expressions in
29fb4 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69 67 67   the.**    trigg
29fb5 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e 0a 2a  er program(s)..*
29fb6 2a 0a 2a 2a 20 49 66 20 74 68 65 79 20 61 72 65  *.** If they are
29fb7 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 70   not NULL, the p
29fb8 69 4f 6c 64 43 6f 6c 4d 61 73 6b 20 61 6e 64 20  iOldColMask and 
29fb9 70 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 6f 75 74  piNewColMask out
29fba 70 75 74 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a  put variables.**
29fbb 20 61 72 65 20 73 65 74 20 74 6f 20 76 61 6c 75   are set to valu
29fbc 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65  es that describe
29fbd 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 75 73 65   the columns use
29fbe 64 20 62 79 20 74 68 65 20 74 72 69 67 67 65 72  d by the trigger
29fbf 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 6e 20 74   program.** in t
29fc0 68 65 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e 45 57  he OLD.* and NEW
29fc1 2e 2a 20 74 61 62 6c 65 73 20 72 65 73 70 65 63  .* tables respec
29fc2 74 69 76 65 6c 79 2e 20 49 66 20 63 6f 6c 75 6d  tively. If colum
29fc3 6e 20 4e 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70  n N of the .** p
29fc4 73 65 75 64 6f 2d 74 61 62 6c 65 20 69 73 20 72  seudo-table is r
29fc5 65 61 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63  ead at least onc
29fc6 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  e, the correspon
29fc7 64 69 6e 67 20 62 69 74 20 6f 66 20 74 68 65 20  ding bit of the 
29fc8 6f 75 74 70 75 74 0a 2a 2a 20 6d 61 73 6b 20 69  output.** mask i
29fc9 73 20 73 65 74 2e 20 49 66 20 61 20 63 6f 6c 75  s set. If a colu
29fca 6d 6e 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78  mn with an index
29fcb 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 33 32   greater than 32
29fcc 20 69 73 20 72 65 61 64 2c 20 74 68 65 0a 2a 2a   is read, the.**
29fcd 20 6f 75 74 70 75 74 20 6d 61 73 6b 20 69 73 20   output mask is 
29fce 73 65 74 20 74 6f 20 74 68 65 20 73 70 65 63 69  set to the speci
29fcf 61 6c 20 76 61 6c 75 65 20 30 78 66 66 66 66 66  al value 0xfffff
29fd0 66 66 66 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54  fff..**.*/.SQLIT
29fd1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
29fd2 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
29fd3 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ger(.  Parse *pP
29fd4 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50  arse,       /* P
29fd5 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  arse context */.
29fd6 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
29fd7 67 65 72 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f  ger,   /* List o
29fd8 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61  f triggers on ta
29fd9 62 6c 65 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e  ble pTab */.  in
29fda 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
29fdb 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f     /* One of TK_
29fdc 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52  UPDATE, TK_INSER
29fdd 54 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a  T, TK_DELETE */.
29fde 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61    ExprList *pCha
29fdf 6e 67 65 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65  nges,  /* Change
29fe0 73 20 6c 69 73 74 20 66 6f 72 20 61 6e 79 20 55  s list for any U
29fe1 50 44 41 54 45 20 4f 46 20 74 72 69 67 67 65 72  PDATE OF trigger
29fe2 73 20 2a 2f 0a 20 20 69 6e 74 20 74 72 5f 74 6d  s */.  int tr_tm
29fe3 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ,           /* O
29fe4 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45  ne of TRIGGER_BE
29fe5 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46  FORE, TRIGGER_AF
29fe6 54 45 52 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  TER */.  Table *
29fe7 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a  pTab,         /*
29fe8 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 63 6f   The table to co
29fe9 64 65 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d  de triggers from
29fea 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64 78   */.  int newIdx
29feb 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
29fec 65 20 69 6e 64 69 63 65 20 6f 66 20 74 68 65 20  e indice of the 
29fed 22 6e 65 77 22 20 72 6f 77 20 74 6f 20 61 63 63  "new" row to acc
29fee 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6c 64  ess */.  int old
29fef 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Idx,          /*
29ff0 20 54 68 65 20 69 6e 64 69 63 65 20 6f 66 20 74   The indice of t
29ff1 68 65 20 22 6f 6c 64 22 20 72 6f 77 20 74 6f 20  he "old" row to 
29ff2 61 63 63 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20  access */.  int 
29ff3 6f 72 63 6f 6e 66 2c 20 20 20 20 20 20 20 20 20  orconf,         
29ff4 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20   /* ON CONFLICT 
29ff5 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20  policy */.  int 
29ff6 69 67 6e 6f 72 65 4a 75 6d 70 2c 20 20 20 20 20  ignoreJump,     
29ff7 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20   /* Instruction 
29ff8 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 52  to jump to for R
29ff9 41 49 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f 0a  AISE(IGNORE) */.
29ffa 20 20 75 33 32 20 2a 70 69 4f 6c 64 43 6f 6c 4d    u32 *piOldColM
29ffb 61 73 6b 2c 20 20 20 2f 2a 20 4f 55 54 3a 20 4d  ask,   /* OUT: M
29ffc 61 73 6b 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 75  ask of columns u
29ffd 73 65 64 20 66 72 6f 6d 20 74 68 65 20 4f 4c 44  sed from the OLD
29ffe 2e 2a 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 33  .* table */.  u3
29fff 32 20 2a 70 69 4e 65 77 43 6f 6c 4d 61 73 6b 20  2 *piNewColMask 
2a000 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20     /* OUT: Mask 
2a001 6f 66 20 63 6f 6c 75 6d 6e 73 20 75 73 65 64 20  of columns used 
2a002 66 72 6f 6d 20 74 68 65 20 4e 45 57 2e 2a 20 74  from the NEW.* t
2a003 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  able */.){.  Tri
2a004 67 67 65 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74  gger *p;.  sqlit
2a005 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2a006 3e 64 62 3b 0a 20 20 54 72 69 67 67 65 72 53 74  >db;.  TriggerSt
2a007 61 63 6b 20 74 72 69 67 53 74 61 63 6b 45 6e 74  ack trigStackEnt
2a008 72 79 3b 0a 0a 20 20 74 72 69 67 53 74 61 63 6b  ry;..  trigStack
2a009 45 6e 74 72 79 2e 6f 6c 64 43 6f 6c 4d 61 73 6b  Entry.oldColMask
2a00a 20 3d 20 30 3b 0a 20 20 74 72 69 67 53 74 61 63   = 0;.  trigStac
2a00b 6b 45 6e 74 72 79 2e 6e 65 77 43 6f 6c 4d 61 73  kEntry.newColMas
2a00c 6b 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  k = 0;..  assert
2a00d 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45  (op == TK_UPDATE
2a00e 20 7c 7c 20 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53   || op == TK_INS
2a00f 45 52 54 20 7c 7c 20 6f 70 20 3d 3d 20 54 4b 5f  ERT || op == TK_
2a010 44 45 4c 45 54 45 29 3b 0a 20 20 61 73 73 65 72  DELETE);.  asser
2a011 74 28 74 72 5f 74 6d 20 3d 3d 20 54 52 49 47 47  t(tr_tm == TRIGG
2a012 45 52 5f 42 45 46 4f 52 45 20 7c 7c 20 74 72 5f  ER_BEFORE || tr_
2a013 74 6d 20 3d 3d 20 54 52 49 47 47 45 52 5f 41 46  tm == TRIGGER_AF
2a014 54 45 52 20 29 3b 0a 0a 20 20 61 73 73 65 72 74  TER );..  assert
2a015 28 6e 65 77 49 64 78 20 21 3d 20 2d 31 20 7c 7c  (newIdx != -1 ||
2a016 20 6f 6c 64 49 64 78 20 21 3d 20 2d 31 29 3b 0a   oldIdx != -1);.
2a017 0a 20 20 66 6f 72 28 70 3d 70 54 72 69 67 67 65  .  for(p=pTrigge
2a018 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  r; p; p=p->pNext
2a019 29 7b 0a 20 20 20 20 69 6e 74 20 66 69 72 65 5f  ){.    int fire_
2a01a 74 68 69 73 20 3d 20 30 3b 0a 0a 20 20 20 20 2f  this = 0;..    /
2a01b 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74  * Determine whet
2a01c 68 65 72 20 77 65 20 73 68 6f 75 6c 64 20 63 6f  her we should co
2a01d 64 65 20 74 68 69 73 20 74 72 69 67 67 65 72 20  de this trigger 
2a01e 2a 2f 0a 20 20 20 20 69 66 28 20 0a 20 20 20 20  */.    if( .    
2a01f 20 20 70 2d 3e 6f 70 3d 3d 6f 70 20 26 26 20 0a    p->op==op && .
2a020 20 20 20 20 20 20 70 2d 3e 74 72 5f 74 6d 3d 3d        p->tr_tm==
2a021 74 72 5f 74 6d 20 26 26 20 0a 20 20 20 20 20 20  tr_tm && .      
2a022 28 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 2d 3e  (p->pSchema==p->
2a023 70 54 61 62 53 63 68 65 6d 61 20 7c 7c 20 70 2d  pTabSchema || p-
2a024 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44  >pSchema==db->aD
2a025 62 5b 31 5d 2e 70 53 63 68 65 6d 61 29 20 26 26  b[1].pSchema) &&
2a026 0a 20 20 20 20 20 20 28 6f 70 21 3d 54 4b 5f 55  .      (op!=TK_U
2a027 50 44 41 54 45 7c 7c 21 70 2d 3e 70 43 6f 6c 75  PDATE||!p->pColu
2a028 6d 6e 73 7c 7c 63 68 65 63 6b 43 6f 6c 75 6d 6e  mns||checkColumn
2a029 4f 76 65 72 4c 61 70 28 70 2d 3e 70 43 6f 6c 75  OverLap(p->pColu
2a02a 6d 6e 73 2c 70 43 68 61 6e 67 65 73 29 29 0a 20  mns,pChanges)). 
2a02b 20 20 20 29 7b 0a 20 20 20 20 20 20 54 72 69 67     ){.      Trig
2a02c 67 65 72 53 74 61 63 6b 20 2a 70 53 3b 20 20 20  gerStack *pS;   
2a02d 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
2a02e 20 74 72 69 67 67 65 72 2d 73 74 61 63 6b 20 65   trigger-stack e
2a02f 6e 74 72 79 20 2a 2f 0a 20 20 20 20 20 20 66 6f  ntry */.      fo
2a030 72 28 70 53 3d 70 50 61 72 73 65 2d 3e 74 72 69  r(pS=pParse->tri
2a031 67 53 74 61 63 6b 3b 20 70 53 20 26 26 20 70 21  gStack; pS && p!
2a032 3d 70 53 2d 3e 70 54 72 69 67 67 65 72 3b 20 70  =pS->pTrigger; p
2a033 53 3d 70 53 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20  S=pS->pNext){}. 
2a034 20 20 20 20 20 69 66 28 20 21 70 53 20 29 7b 0a       if( !pS ){.
2a035 20 20 20 20 20 20 20 20 66 69 72 65 5f 74 68 69          fire_thi
2a036 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 23  s = 1;.      }.#
2a037 69 66 20 30 20 20 20 20 2f 2a 20 47 69 76 65 20  if 0    /* Give 
2a038 6e 6f 20 77 61 72 6e 69 6e 67 20 66 6f 72 20 72  no warning for r
2a039 65 63 75 72 73 69 76 65 20 74 72 69 67 67 65 72  ecursive trigger
2a03a 73 2e 20 20 4a 75 73 74 20 64 6f 20 6e 6f 74 20  s.  Just do not 
2a03b 64 6f 20 74 68 65 6d 20 2a 2f 0a 20 20 20 20 20  do them */.     
2a03c 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73   else{.        s
2a03d 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2a03e 50 61 72 73 65 2c 20 22 72 65 63 75 72 73 69 76  Parse, "recursiv
2a03f 65 20 74 72 69 67 67 65 72 73 20 6e 6f 74 20 73  e triggers not s
2a040 75 70 70 6f 72 74 65 64 20 28 25 73 29 22 2c 0a  upported (%s)",.
2a041 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6e              p->n
2a042 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65  ame);.        re
2a043 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
2a044 52 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  R;.      }.#endi
2a045 66 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 69 66  f.    }. .    if
2a046 28 20 66 69 72 65 5f 74 68 69 73 20 29 7b 0a 20  ( fire_this ){. 
2a047 20 20 20 20 20 69 6e 74 20 65 6e 64 54 72 69 67       int endTrig
2a048 67 65 72 3b 0a 20 20 20 20 20 20 45 78 70 72 20  ger;.      Expr 
2a049 2a 20 77 68 65 6e 45 78 70 72 3b 0a 20 20 20 20  * whenExpr;.    
2a04a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43    AuthContext sC
2a04b 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 4e 61  ontext;.      Na
2a04c 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 0a  meContext sNC;..
2a04d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2a04e 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 20 20  MIT_TRACE.      
2a04f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a050 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  4(pParse->pVdbe,
2a051 20 4f 50 5f 54 72 61 63 65 2c 20 30 2c 20 30 2c   OP_Trace, 0, 0,
2a052 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
2a053 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2a054 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2a055 2d 2d 20 54 52 49 47 47 45 52 20 25 73 22 2c 20  -- TRIGGER %s", 
2a056 70 2d 3e 6e 61 6d 65 29 2c 0a 20 20 20 20 20 20  p->name),.      
2a057 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a058 20 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 23    P4_DYNAMIC);.#
2a059 65 6e 64 69 66 0a 20 20 20 20 20 20 6d 65 6d 73  endif.      mems
2a05a 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65  et(&sNC, 0, size
2a05b 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 20 20  of(sNC));.      
2a05c 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61  sNC.pParse = pPa
2a05d 72 73 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 50  rse;..      /* P
2a05e 75 73 68 20 61 6e 20 65 6e 74 72 79 20 6f 6e 20  ush an entry on 
2a05f 74 6f 20 74 68 65 20 74 72 69 67 67 65 72 20 73  to the trigger s
2a060 74 61 63 6b 20 2a 2f 0a 20 20 20 20 20 20 74 72  tack */.      tr
2a061 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 54 72  igStackEntry.pTr
2a062 69 67 67 65 72 20 3d 20 70 3b 0a 20 20 20 20 20  igger = p;.     
2a063 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e   trigStackEntry.
2a064 6e 65 77 49 64 78 20 3d 20 6e 65 77 49 64 78 3b  newIdx = newIdx;
2a065 0a 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b  .      trigStack
2a066 45 6e 74 72 79 2e 6f 6c 64 49 64 78 20 3d 20 6f  Entry.oldIdx = o
2a067 6c 64 49 64 78 3b 0a 20 20 20 20 20 20 74 72 69  ldIdx;.      tri
2a068 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 54 61 62  gStackEntry.pTab
2a069 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20 20 74   = pTab;.      t
2a06a 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 4e  rigStackEntry.pN
2a06b 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 74 72  ext = pParse->tr
2a06c 69 67 53 74 61 63 6b 3b 0a 20 20 20 20 20 20 74  igStack;.      t
2a06d 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 69 67  rigStackEntry.ig
2a06e 6e 6f 72 65 4a 75 6d 70 20 3d 20 69 67 6e 6f 72  noreJump = ignor
2a06f 65 4a 75 6d 70 3b 0a 20 20 20 20 20 20 70 50 61  eJump;.      pPa
2a070 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 3d  rse->trigStack =
2a071 20 26 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79   &trigStackEntry
2a072 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41  ;.      sqlite3A
2a073 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70  uthContextPush(p
2a074 50 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74  Parse, &sContext
2a075 2c 20 70 2d 3e 6e 61 6d 65 29 3b 0a 0a 20 20 20  , p->name);..   
2a076 20 20 20 2f 2a 20 63 6f 64 65 20 74 68 65 20 57     /* code the W
2a077 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  HEN clause */.  
2a078 20 20 20 20 65 6e 64 54 72 69 67 67 65 72 20 3d      endTrigger =
2a079 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2a07a 4c 61 62 65 6c 28 70 50 61 72 73 65 2d 3e 70 56  Label(pParse->pV
2a07b 64 62 65 29 3b 0a 20 20 20 20 20 20 77 68 65 6e  dbe);.      when
2a07c 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78  Expr = sqlite3Ex
2a07d 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 68  prDup(db, p->pWh
2a07e 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  en, 0);.      if
2a07f 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2a080 65 64 20 7c 7c 20 73 71 6c 69 74 65 33 52 65 73  ed || sqlite3Res
2a081 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
2a082 4e 43 2c 20 77 68 65 6e 45 78 70 72 29 20 29 7b  NC, whenExpr) ){
2a083 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
2a084 3e 74 72 69 67 53 74 61 63 6b 20 3d 20 74 72 69  >trigStack = tri
2a085 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 4e 65 78  gStackEntry.pNex
2a086 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  t;.        sqlit
2a087 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2a088 20 77 68 65 6e 45 78 70 72 29 3b 0a 20 20 20 20   whenExpr);.    
2a089 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2a08a 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2a08b 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
2a08c 50 61 72 73 65 2c 20 77 68 65 6e 45 78 70 72 2c  Parse, whenExpr,
2a08d 20 65 6e 64 54 72 69 67 67 65 72 2c 20 53 51 4c   endTrigger, SQL
2a08e 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
2a08f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2a090 70 72 44 65 6c 65 74 65 28 64 62 2c 20 77 68 65  prDelete(db, whe
2a091 6e 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 63  nExpr);..      c
2a092 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72 61  odeTriggerProgra
2a093 6d 28 70 50 61 72 73 65 2c 20 70 2d 3e 73 74 65  m(pParse, p->ste
2a094 70 5f 6c 69 73 74 2c 20 6f 72 63 6f 6e 66 29 3b  p_list, orconf);
2a095 20 0a 0a 20 20 20 20 20 20 2f 2a 20 50 6f 70 20   ..      /* Pop 
2a096 74 68 65 20 65 6e 74 72 79 20 6f 66 66 20 74 68  the entry off th
2a097 65 20 74 72 69 67 67 65 72 20 73 74 61 63 6b 20  e trigger stack 
2a098 2a 2f 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  */.      pParse-
2a099 3e 74 72 69 67 53 74 61 63 6b 20 3d 20 74 72 69  >trigStack = tri
2a09a 67 53 74 61 63 6b 45 6e 74 72 79 2e 70 4e 65 78  gStackEntry.pNex
2a09b 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
2a09c 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26  AuthContextPop(&
2a09d 73 43 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 20 20  sContext);..    
2a09e 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2a09f 6f 6c 76 65 4c 61 62 65 6c 28 70 50 61 72 73 65  olveLabel(pParse
2a0a0 2d 3e 70 56 64 62 65 2c 20 65 6e 64 54 72 69 67  ->pVdbe, endTrig
2a0a1 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ger);.    }.  }.
2a0a2 20 20 69 66 28 20 70 69 4f 6c 64 43 6f 6c 4d 61    if( piOldColMa
2a0a3 73 6b 20 29 20 2a 70 69 4f 6c 64 43 6f 6c 4d 61  sk ) *piOldColMa
2a0a4 73 6b 20 7c 3d 20 74 72 69 67 53 74 61 63 6b 45  sk |= trigStackE
2a0a5 6e 74 72 79 2e 6f 6c 64 43 6f 6c 4d 61 73 6b 3b  ntry.oldColMask;
2a0a6 0a 20 20 69 66 28 20 70 69 4e 65 77 43 6f 6c 4d  .  if( piNewColM
2a0a7 61 73 6b 20 29 20 2a 70 69 4e 65 77 43 6f 6c 4d  ask ) *piNewColM
2a0a8 61 73 6b 20 7c 3d 20 74 72 69 67 53 74 61 63 6b  ask |= trigStack
2a0a9 45 6e 74 72 79 2e 6e 65 77 43 6f 6c 4d 61 73 6b  Entry.newColMask
2a0aa 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
2a0ab 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2a0ac 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
2a0ad 52 49 47 47 45 52 29 20 2a 2f 0a 0a 2f 2a 2a 2a  RIGGER) */../***
2a0ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2a0af 6f 66 20 74 72 69 67 67 65 72 2e 63 20 2a 2a 2a  of trigger.c ***
2a0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2a0b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2a0b4 6e 20 66 69 6c 65 20 75 70 64 61 74 65 2e 63 20  n file update.c 
2a0b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2a0b8 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
2a0b9 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
2a0ba 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2a0bb 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2a0bc 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2a0bd 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
2a0be 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
2a0bf 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
2a0c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
2a0c1 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2a0c2 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
2a0c3 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
2a0c4 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
2a0c5 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
2a0c6 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
2a0c7 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
2a0c8 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
2a0c9 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
2a0ca 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
2a0cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a0cf 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
2a0d0 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
2a0d1 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72  routines that ar
2a0d2 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  e called by the 
2a0d3 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e  parser.** to han
2a0d4 64 6c 65 20 55 50 44 41 54 45 20 73 74 61 74 65  dle UPDATE state
2a0d5 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  ments..**.** $Id
2a0d6 3a 20 75 70 64 61 74 65 2e 63 2c 76 20 31 2e 32  : update.c,v 1.2
2a0d7 30 30 20 32 30 30 39 2f 30 35 2f 30 35 20 31 35  00 2009/05/05 15
2a0d8 3a 34 36 3a 31 30 20 64 72 68 20 45 78 70 20 24  :46:10 drh Exp $
2a0d9 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
2a0da 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2a0db 54 41 42 4c 45 0a 2f 2a 20 46 6f 72 77 61 72 64  TABLE./* Forward
2a0dc 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a   declaration */.
2a0dd 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61  static void upda
2a0de 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0a  teVirtualTable(.
2a0df 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2a0e0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2a0e1 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2a0e2 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
2a0e3 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76  ,       /* The v
2a0e4 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20  irtual table to 
2a0e5 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20  be modified */. 
2a0e6 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2a0e7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
2a0e8 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
2a0e9 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
2a0ea 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75  es,  /* The colu
2a0eb 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e  mns to change in
2a0ec 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74   the UPDATE stat
2a0ed 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  ement */.  Expr 
2a0ee 2a 70 52 6f 77 69 64 45 78 70 72 2c 20 20 20 20  *pRowidExpr,    
2a0ef 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73  /* Expression us
2a0f0 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20  ed to recompute 
2a0f1 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  the rowid */.  i
2a0f2 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20  nt *aXRef,      
2a0f3 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66      /* Mapping f
2a0f4 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70  rom columns of p
2a0f5 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69  Tab to entries i
2a0f6 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20  n pChanges */.  
2a0f7 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20  Expr *pWhere    
2a0f8 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c       /* WHERE cl
2a0f9 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 41  ause of the UPDA
2a0fa 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  TE statement */.
2a0fb 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  );.#endif /* SQL
2a0fc 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2a0fd 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  TABLE */../*.** 
2a0fe 54 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  The most recentl
2a0ff 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74  y coded instruct
2a100 69 6f 6e 20 77 61 73 20 61 6e 20 4f 50 5f 43 6f  ion was an OP_Co
2a101 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65  lumn to retrieve
2a102 20 74 68 65 0a 2a 2a 20 69 2d 74 68 20 63 6f 6c   the.** i-th col
2a103 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 70 54 61  umn of table pTa
2a104 62 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  b. This routine 
2a105 73 65 74 73 20 74 68 65 20 50 34 20 70 61 72 61  sets the P4 para
2a106 6d 65 74 65 72 20 6f 66 20 74 68 65 20 0a 2a 2a  meter of the .**
2a107 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74 68   OP_Column to th
2a108 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2c  e default value,
2a109 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54   if any..**.** T
2a10a 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2a10b 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20   of a column is 
2a10c 73 70 65 63 69 66 69 65 64 20 62 79 20 61 20 44  specified by a D
2a10d 45 46 41 55 4c 54 20 63 6c 61 75 73 65 20 69 6e  EFAULT clause in
2a10e 20 74 68 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20   the .** column 
2a10f 64 65 66 69 6e 69 74 69 6f 6e 2e 20 54 68 69 73  definition. This
2a110 20 77 61 73 20 65 69 74 68 65 72 20 73 75 70 70   was either supp
2a111 6c 69 65 64 20 62 79 20 74 68 65 20 75 73 65 72  lied by the user
2a112 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 0a   when the table.
2a113 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2c 20  ** was created, 
2a114 6f 72 20 61 64 64 65 64 20 6c 61 74 65 72 20 74  or added later t
2a115 6f 20 74 68 65 20 74 61 62 6c 65 20 64 65 66 69  o the table defi
2a116 6e 69 74 69 6f 6e 20 62 79 20 61 6e 20 41 4c 54  nition by an ALT
2a117 45 52 20 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d 6d  ER TABLE.** comm
2a118 61 6e 64 2e 20 49 66 20 74 68 65 20 6c 61 74 74  and. If the latt
2a119 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  er, then the row
2a11a 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20  -records in the 
2a11b 74 61 62 6c 65 20 62 74 72 65 65 20 6f 6e 20 64  table btree on d
2a11c 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 63  isk.** may not c
2a11d 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20 66  ontain a value f
2a11e 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e  or the column an
2a11f 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  d the default va
2a120 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66 72  lue, taken.** fr
2a121 6f 6d 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  om the P4 parame
2a122 74 65 72 20 6f 66 20 74 68 65 20 4f 50 5f 43 6f  ter of the OP_Co
2a123 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e  lumn instruction
2a124 2c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  , is returned in
2a125 73 74 65 61 64 2e 0a 2a 2a 20 49 66 20 74 68 65  stead..** If the
2a126 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e 20 61 6c   former, then al
2a127 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 61 72  l row-records ar
2a128 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  e guaranteed to 
2a129 69 6e 63 6c 75 64 65 20 61 20 76 61 6c 75 65 0a  include a value.
2a12a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ** for the colum
2a12b 6e 20 61 6e 64 20 74 68 65 20 50 34 20 76 61 6c  n and the P4 val
2a12c 75 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  ue is not requir
2a12d 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e  ed..**.** Column
2a12e 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 63 72 65   definitions cre
2a12f 61 74 65 64 20 62 79 20 61 6e 20 41 4c 54 45 52  ated by an ALTER
2a130 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 6d   TABLE command m
2a131 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 0a 2a 2a  ay only have .**
2a132 20 6c 69 74 65 72 61 6c 20 64 65 66 61 75 6c 74   literal default
2a133 20 76 61 6c 75 65 73 20 73 70 65 63 69 66 69 65   values specifie
2a134 64 3a 20 61 20 6e 75 6d 62 65 72 2c 20 6e 75 6c  d: a number, nul
2a135 6c 20 6f 72 20 61 20 73 74 72 69 6e 67 2e 20 28  l or a string. (
2a136 49 66 20 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f 6d  If a more.** com
2a137 70 6c 69 63 61 74 65 64 20 64 65 66 61 75 6c 74  plicated default
2a138 20 65 78 70 72 65 73 73 69 6f 6e 20 76 61 6c 75   expression valu
2a139 65 20 77 61 73 20 70 72 6f 76 69 64 65 64 2c 20  e was provided, 
2a13a 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64 20  it is evaluated 
2a13b 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 4c 54  .** when the ALT
2a13c 45 52 20 54 41 42 4c 45 20 69 73 20 65 78 65 63  ER TABLE is exec
2a13d 75 74 65 64 20 61 6e 64 20 6f 6e 65 20 6f 66 20  uted and one of 
2a13e 74 68 65 20 6c 69 74 65 72 61 6c 20 76 61 6c 75  the literal valu
2a13f 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e  es written.** in
2a140 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  to the sqlite_ma
2a141 73 74 65 72 20 74 61 62 6c 65 2e 29 0a 2a 2a 0a  ster table.).**.
2a142 2a 2a 20 54 68 65 72 65 66 6f 72 65 2c 20 74 68  ** Therefore, th
2a143 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69  e P4 parameter i
2a144 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20  s only required 
2a145 69 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  if the default v
2a146 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20  alue for.** the 
2a147 63 6f 6c 75 6d 6e 20 69 73 20 61 20 6c 69 74 65  column is a lite
2a148 72 61 6c 20 6e 75 6d 62 65 72 2c 20 73 74 72 69  ral number, stri
2a149 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20 54 68 65 20  ng or null. The 
2a14a 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d  sqlite3ValueFrom
2a14b 45 78 70 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69  Expr().** functi
2a14c 6f 6e 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66  on is capable of
2a14d 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68   transforming th
2a14e 65 73 65 20 74 79 70 65 73 20 6f 66 20 65 78 70  ese types of exp
2a14f 72 65 73 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a  ressions into.**
2a150 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
2a151 62 6a 65 63 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  bjects..*/.SQLIT
2a152 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2a153 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
2a154 75 6c 74 28 56 64 62 65 20 2a 76 2c 20 54 61 62  ult(Vdbe *v, Tab
2a155 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 69 29  le *pTab, int i)
2a156 7b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  {.  assert( pTab
2a157 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 54  !=0 );.  if( !pT
2a158 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  ab->pSelect ){. 
2a159 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
2a15a 20 2a 70 56 61 6c 75 65 3b 0a 20 20 20 20 75 38   *pValue;.    u8
2a15b 20 65 6e 63 20 3d 20 45 4e 43 28 73 71 6c 69 74   enc = ENC(sqlit
2a15c 65 33 56 64 62 65 44 62 28 76 29 29 3b 0a 20 20  e3VdbeDb(v));.  
2a15d 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
2a15e 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 3b   &pTab->aCol[i];
2a15f 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
2a160 28 28 76 2c 20 22 25 73 2e 25 73 22 2c 20 70 54  ((v, "%s.%s", pT
2a161 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d  ab->zName, pCol-
2a162 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61 73  >zName));.    as
2a163 73 65 72 74 28 20 69 3c 70 54 61 62 2d 3e 6e 43  sert( i<pTab->nC
2a164 6f 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ol );.    sqlite
2a165 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73  3ValueFromExpr(s
2a166 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c  qlite3VdbeDb(v),
2a167 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e   pCol->pDflt, en
2a168 63 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  c, .            
2a169 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
2a16a 6c 2d 3e 61 66 66 69 6e 69 74 79 2c 20 26 70 56  l->affinity, &pV
2a16b 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20 70  alue);.    if( p
2a16c 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 20 20 73  Value ){.      s
2a16d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2a16e 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74  P4(v, -1, (const
2a16f 20 63 68 61 72 20 2a 29 70 56 61 6c 75 65 2c 20   char *)pValue, 
2a170 50 34 5f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 20  P4_MEM);.    }. 
2a171 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63   }.}../*.** Proc
2a172 65 73 73 20 61 6e 20 55 50 44 41 54 45 20 73 74  ess an UPDATE st
2a173 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
2a174 20 55 50 44 41 54 45 20 4f 52 20 49 47 4e 4f 52   UPDATE OR IGNOR
2a175 45 20 74 61 62 6c 65 5f 77 78 79 7a 20 53 45 54  E table_wxyz SET
2a176 20 61 3d 62 2c 20 63 3d 64 20 57 48 45 52 45 20   a=b, c=d WHERE 
2a177 65 3c 35 20 41 4e 44 20 66 20 4e 4f 54 20 4e 55  e<5 AND f NOT NU
2a178 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  LL;.**          
2a179 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f 5f  \_______/ \_____
2a17a 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  ___/     \______
2a17b 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f  /       \_______
2a17c 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20 20  _________/.*    
2a17d 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20          onError 
2a17e 20 20 70 54 61 62 4c 69 73 74 20 20 20 20 20 20    pTabList      
2a17f 70 43 68 61 6e 67 65 73 20 20 20 20 20 20 20 20  pChanges        
2a180 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a 53       pWhere.*/.S
2a181 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2a182 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74 65  id sqlite3Update
2a183 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2a184 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
2a185 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2a186 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2a187 54 61 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20  TabList,     /* 
2a188 54 68 65 20 74 61 62 6c 65 20 69 6e 20 77 68 69  The table in whi
2a189 63 68 20 77 65 20 73 68 6f 75 6c 64 20 63 68 61  ch we should cha
2a18a 6e 67 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20  nge things */.  
2a18b 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
2a18c 65 73 2c 20 20 20 20 2f 2a 20 54 68 69 6e 67 73  es,    /* Things
2a18d 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 20 2a   to be changed *
2a18e 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
2a18f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
2a190 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
2a191 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a   May be null */.
2a192 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20    int onError   
2a193 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
2a194 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72  to handle constr
2a195 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29  aint errors */.)
2a196 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  {.  int i, j;   
2a197 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2a198 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
2a199 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
2a19a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
2a19b 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61 74  able to be updat
2a19c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ed */.  int addr
2a19d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f   = 0;          /
2a19e 2a 20 56 44 42 45 20 69 6e 73 74 72 75 63 74 69  * VDBE instructi
2a19f 6f 6e 20 61 64 64 72 65 73 73 20 6f 66 20 74 68  on address of th
2a1a0 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6c  e start of the l
2a1a1 6f 6f 70 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e  oop */.  WhereIn
2a1a2 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20  fo *pWInfo;     
2a1a3 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
2a1a4 62 6f 75 74 20 74 68 65 20 57 48 45 52 45 20 63  bout the WHERE c
2a1a5 6c 61 75 73 65 20 2a 2f 0a 20 20 56 64 62 65 20  lause */.  Vdbe 
2a1a6 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2a1a7 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
2a1a8 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
2a1a9 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
2a1aa 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
2a1ab 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
2a1ac 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e   indices */.  in
2a1ad 74 20 6e 49 64 78 3b 20 20 20 20 20 20 20 20 20  t nIdx;         
2a1ae 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2a1af 66 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 6e  f indices that n
2a1b0 65 65 64 20 75 70 64 61 74 69 6e 67 20 2a 2f 0a  eed updating */.
2a1b1 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
2a1b2 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
2a1b3 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
2a1b4 66 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c 69  f pTab */.  sqli
2a1b5 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
2a1b6 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2a1b7 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  se structure */.
2a1b8 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 3d    int *aRegIdx =
2a1b9 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20   0;      /* One 
2a1ba 72 65 67 69 73 74 65 72 20 61 73 73 69 67 6e 65  register assigne
2a1bb 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78 20  d to each index 
2a1bc 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f  to be updated */
2a1bd 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 20 3d 20  .  int *aXRef = 
2a1be 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 58 52  0;        /* aXR
2a1bf 65 66 5b 69 5d 20 69 73 20 74 68 65 20 69 6e 64  ef[i] is the ind
2a1c0 65 78 20 69 6e 20 70 43 68 61 6e 67 65 73 2d 3e  ex in pChanges->
2a1c1 61 5b 5d 20 6f 66 20 74 68 65 0a 20 20 20 20 20  a[] of the.     
2a1c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a1c3 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73      ** an expres
2a1c4 73 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d 74  sion for the i-t
2a1c5 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
2a1c6 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 20 20 20  table..         
2a1c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a1c8 2a 2a 20 61 58 52 65 66 5b 69 5d 3d 3d 2d 31 20  ** aXRef[i]==-1 
2a1c9 69 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75  if the i-th colu
2a1ca 6d 6e 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  mn is not change
2a1cb 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 63 68 6e 67  d. */.  int chng
2a1cc 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 2f  Rowid;         /
2a1cd 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 72 65  * True if the re
2a1ce 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 62  cord number is b
2a1cf 65 69 6e 67 20 63 68 61 6e 67 65 64 20 2a 2f 0a  eing changed */.
2a1d0 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 45 78    Expr *pRowidEx
2a1d1 70 72 20 3d 20 30 3b 20 20 2f 2a 20 45 78 70 72  pr = 0;  /* Expr
2a1d2 65 73 73 69 6f 6e 20 64 65 66 69 6e 69 6e 67 20  ession defining 
2a1d3 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  the new record n
2a1d4 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f  umber */.  int o
2a1d5 70 65 6e 41 6c 6c 20 3d 20 30 3b 20 20 20 20 20  penAll = 0;     
2a1d6 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c 6c    /* True if all
2a1d7 20 69 6e 64 69 63 65 73 20 6e 65 65 64 20 74 6f   indices need to
2a1d8 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
2a1d9 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e  AuthContext sCon
2a1da 74 65 78 74 3b 20 20 2f 2a 20 54 68 65 20 61 75  text;  /* The au
2a1db 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  thorization cont
2a1dc 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e  ext */.  NameCon
2a1dd 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20  text sNC;       
2a1de 2f 2a 20 54 68 65 20 6e 61 6d 65 2d 63 6f 6e 74  /* The name-cont
2a1df 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20 65  ext to resolve e
2a1e0 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a 2f  xpressions in */
2a1e1 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
2a1e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2a1e3 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
2a1e4 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
2a1e5 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e   updated */.  in
2a1e6 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20  t j1;           
2a1e7 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 65       /* Addresse
2a1e8 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75  s of jump instru
2a1e9 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ctions */.  int 
2a1ea 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20  okOnePass;      
2a1eb 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6f     /* True for o
2a1ec 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68  ne-pass algorith
2a1ed 6d 20 77 69 74 68 6f 75 74 20 74 68 65 20 46 49  m without the FI
2a1ee 46 4f 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  FO */..#ifndef S
2a1ef 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2a1f0 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b  ER.  int isView;
2a1f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a1f2 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 75    /* Trying to u
2a1f3 70 64 61 74 65 20 61 20 76 69 65 77 20 2a 2f 0a  pdate a view */.
2a1f4 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
2a1f5 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ger;           /
2a1f6 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65  * List of trigge
2a1f7 72 73 20 6f 6e 20 70 54 61 62 2c 20 69 66 20 72  rs on pTab, if r
2a1f8 65 71 75 69 72 65 64 20 2a 2f 0a 23 65 6e 64 69  equired */.#endi
2a1f9 66 0a 20 20 69 6e 74 20 69 42 65 67 69 6e 41 66  f.  int iBeginAf
2a1fa 74 65 72 54 72 69 67 67 65 72 20 3d 20 30 3b 20  terTrigger = 0; 
2a1fb 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61   /* Address of a
2a1fc 66 74 65 72 20 74 72 69 67 67 65 72 20 70 72 6f  fter trigger pro
2a1fd 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45  gram */.  int iE
2a1fe 6e 64 41 66 74 65 72 54 72 69 67 67 65 72 20 3d  ndAfterTrigger =
2a1ff 20 30 3b 20 20 20 20 2f 2a 20 45 78 69 74 20 6f   0;    /* Exit o
2a200 66 20 61 66 74 65 72 20 74 72 69 67 67 65 72 20  f after trigger 
2a201 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74  program */.  int
2a202 20 69 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69   iBeginBeforeTri
2a203 67 67 65 72 20 3d 20 30 3b 20 2f 2a 20 41 64 64  gger = 0; /* Add
2a204 72 65 73 73 20 6f 66 20 62 65 66 6f 72 65 20 74  ress of before t
2a205 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a  rigger program *
2a206 2f 0a 20 20 69 6e 74 20 69 45 6e 64 42 65 66 6f  /.  int iEndBefo
2a207 72 65 54 72 69 67 67 65 72 20 3d 20 30 3b 20 20  reTrigger = 0;  
2a208 20 2f 2a 20 45 78 69 74 20 6f 66 20 62 65 66 6f   /* Exit of befo
2a209 72 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  re trigger progr
2a20a 61 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 5f  am */.  u32 old_
2a20b 63 6f 6c 5f 6d 61 73 6b 20 3d 20 30 3b 20 20 20  col_mask = 0;   
2a20c 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
2a20d 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e  OLD.* columns in
2a20e 20 75 73 65 20 2a 2f 0a 20 20 75 33 32 20 6e 65   use */.  u32 ne
2a20f 77 5f 63 6f 6c 5f 6d 61 73 6b 20 3d 20 30 3b 20  w_col_mask = 0; 
2a210 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
2a211 66 20 4e 45 57 2e 2a 20 63 6f 6c 75 6d 6e 73 20  f NEW.* columns 
2a212 69 6e 20 75 73 65 20 2a 2f 0a 0a 20 20 69 6e 74  in use */..  int
2a213 20 6e 65 77 49 64 78 20 20 20 20 20 20 3d 20 2d   newIdx      = -
2a214 31 3b 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20  1;  /* index of 
2a215 74 72 69 67 67 65 72 20 22 6e 65 77 22 20 74 65  trigger "new" te
2a216 6d 70 20 74 61 62 6c 65 20 20 20 20 20 20 20 2a  mp table       *
2a217 2f 0a 20 20 69 6e 74 20 6f 6c 64 49 64 78 20 20  /.  int oldIdx  
2a218 20 20 20 20 3d 20 2d 31 3b 20 20 2f 2a 20 69 6e      = -1;  /* in
2a219 64 65 78 20 6f 66 20 74 72 69 67 67 65 72 20 22  dex of trigger "
2a21a 6f 6c 64 22 20 74 65 6d 70 20 74 61 62 6c 65 20  old" temp table 
2a21b 20 20 20 20 20 20 2a 2f 0a 0a 20 20 2f 2a 20 52        */..  /* R
2a21c 65 67 69 73 74 65 72 20 41 6c 6c 6f 63 61 74 69  egister Allocati
2a21d 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ons */.  int reg
2a21e 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b 20 20 20  RowCount = 0;   
2a21f 2f 2a 20 41 20 63 6f 75 6e 74 20 6f 66 20 72 6f  /* A count of ro
2a220 77 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20  ws changed */.  
2a221 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69 64 3b  int regOldRowid;
2a222 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6c         /* The ol
2a223 64 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  d rowid */.  int
2a224 20 72 65 67 4e 65 77 52 6f 77 69 64 3b 20 20 20   regNewRowid;   
2a225 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72      /* The new r
2a226 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65  owid */.  int re
2a227 67 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  gData;          
2a228 20 2f 2a 20 4e 65 77 20 64 61 74 61 20 66 6f 72   /* New data for
2a229 20 74 68 65 20 72 6f 77 20 2a 2f 0a 20 20 69 6e   the row */.  in
2a22a 74 20 72 65 67 52 6f 77 53 65 74 20 3d 20 30 3b  t regRowSet = 0;
2a22b 20 20 20 20 20 2f 2a 20 52 6f 77 73 65 74 20 6f       /* Rowset o
2a22c 66 20 72 6f 77 73 20 74 6f 20 62 65 20 75 70 64  f rows to be upd
2a22d 61 74 65 64 20 2a 2f 0a 0a 20 20 73 43 6f 6e 74  ated */..  sCont
2a22e 65 78 74 2e 70 50 61 72 73 65 20 3d 20 30 3b 0a  ext.pParse = 0;.
2a22f 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2a230 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  b;.  if( pParse-
2a231 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
2a232 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2a233 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
2a234 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65  anup;.  }.  asse
2a235 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  rt( pTabList->nS
2a236 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c  rc==1 );..  /* L
2a237 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  ocate the table 
2a238 77 68 69 63 68 20 77 65 20 77 61 6e 74 20 74 6f  which we want to
2a239 20 75 70 64 61 74 65 2e 20 0a 20 20 2a 2f 0a 20   update. .  */. 
2a23a 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53   pTab = sqlite3S
2a23b 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61  rcListLookup(pPa
2a23c 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a  rse, pTabList);.
2a23d 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20    if( pTab==0 ) 
2a23e 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
2a23f 6e 75 70 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  nup;.  iDb = sql
2a240 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2a241 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
2a242 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20  ab->pSchema);.. 
2a243 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69   /* Figure out i
2a244 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 74 72  f we have any tr
2a245 69 67 67 65 72 73 20 61 6e 64 20 69 66 20 74 68  iggers and if th
2a246 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20  e table being.  
2a247 2a 2a 20 75 70 64 61 74 65 64 20 69 73 20 61 20  ** updated is a 
2a248 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  view.  */.#ifnde
2a249 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2a24a 49 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72  IGGER.  pTrigger
2a24b 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
2a24c 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
2a24d 70 54 61 62 2c 20 54 4b 5f 55 50 44 41 54 45 2c  pTab, TK_UPDATE,
2a24e 20 70 43 68 61 6e 67 65 73 2c 20 30 29 3b 0a 20   pChanges, 0);. 
2a24f 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e   isView = pTab->
2a250 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73  pSelect!=0;.#els
2a251 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69 67  e.# define pTrig
2a252 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20 69  ger 0.# define i
2a253 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23  sView 0.#endif.#
2a254 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2a255 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69  T_VIEW.# undef i
2a256 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69  sView.# define i
2a257 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a  sView 0.#endif..
2a258 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52    if( sqlite3IsR
2a259 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20  eadOnly(pParse, 
2a25a 70 54 61 62 2c 20 28 70 54 72 69 67 67 65 72 3f  pTab, (pTrigger?
2a25b 31 3a 30 29 29 20 29 7b 0a 20 20 20 20 67 6f 74  1:0)) ){.    got
2a25c 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
2a25d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
2a25e 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
2a25f 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
2a260 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ab) ){.    goto 
2a261 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2a262 20 20 7d 0a 20 20 61 58 52 65 66 20 3d 20 73 71    }.  aXRef = sq
2a263 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
2a264 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29  (db, sizeof(int)
2a265 20 2a 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b   * pTab->nCol );
2a266 0a 20 20 69 66 28 20 61 58 52 65 66 3d 3d 30 20  .  if( aXRef==0 
2a267 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c  ) goto update_cl
2a268 65 61 6e 75 70 3b 0a 20 20 66 6f 72 28 69 3d 30  eanup;.  for(i=0
2a269 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
2a26a 69 2b 2b 29 20 61 58 52 65 66 5b 69 5d 20 3d 20  i++) aXRef[i] = 
2a26b 2d 31 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  -1;..  /* If the
2a26c 72 65 20 61 72 65 20 46 4f 52 20 45 41 43 48 20  re are FOR EACH 
2a26d 52 4f 57 20 74 72 69 67 67 65 72 73 2c 20 61 6c  ROW triggers, al
2a26e 6c 6f 63 61 74 65 20 63 75 72 73 6f 72 73 20 66  locate cursors f
2a26f 6f 72 20 74 68 65 0a 20 20 2a 2a 20 73 70 65 63  or the.  ** spec
2a270 69 61 6c 20 4f 4c 44 20 61 6e 64 20 4e 45 57 20  ial OLD and NEW 
2a271 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 69 66  tables.  */.  if
2a272 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20  ( pTrigger ){.  
2a273 20 20 6e 65 77 49 64 78 20 3d 20 70 50 61 72 73    newIdx = pPars
2a274 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 6f  e->nTab++;.    o
2a275 6c 64 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e  ldIdx = pParse->
2a276 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f  nTab++;.  }..  /
2a277 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 63 75 72  * Allocate a cur
2a278 73 6f 72 73 20 66 6f 72 20 74 68 65 20 6d 61 69  sors for the mai
2a279 6e 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  n database table
2a27a 20 61 6e 64 20 66 6f 72 20 61 6c 6c 20 69 6e 64   and for all ind
2a27b 69 63 65 73 2e 0a 20 20 2a 2a 20 54 68 65 20 69  ices..  ** The i
2a27c 6e 64 65 78 20 63 75 72 73 6f 72 73 20 6d 69 67  ndex cursors mig
2a27d 68 74 20 6e 6f 74 20 62 65 20 75 73 65 64 2c 20  ht not be used, 
2a27e 62 75 74 20 69 66 20 74 68 65 79 20 61 72 65 20  but if they are 
2a27f 75 73 65 64 20 74 68 65 79 0a 20 20 2a 2a 20 6e  used they.  ** n
2a280 65 65 64 20 74 6f 20 6f 63 63 75 72 20 72 69 67  eed to occur rig
2a281 68 74 20 61 66 74 65 72 20 74 68 65 20 64 61 74  ht after the dat
2a282 61 62 61 73 65 20 63 75 72 73 6f 72 2e 20 20 53  abase cursor.  S
2a283 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 0a 20  o go ahead and. 
2a284 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f   ** allocate eno
2a285 75 67 68 20 73 70 61 63 65 2c 20 6a 75 73 74 20  ugh space, just 
2a286 69 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  in case..  */.  
2a287 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69  pTabList->a[0].i
2a288 43 75 72 73 6f 72 20 3d 20 69 43 75 72 20 3d 20  Cursor = iCur = 
2a289 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
2a28a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
2a28b 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
2a28c 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
2a28d 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  {.    pParse->nT
2a28e 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ab++;.  }..  /* 
2a28f 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e  Initialize the n
2a290 61 6d 65 2d 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ame-context */. 
2a291 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
2a292 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
2a293 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
2a294 61 72 73 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63  arse;.  sNC.pSrc
2a295 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
2a296 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
2a297 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
2a298 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65  in all the expre
2a299 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20  ssions of the.  
2a29a 2a 2a 20 6f 66 20 74 68 65 20 55 50 44 41 54 45  ** of the UPDATE
2a29b 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73   statement.  Als
2a29c 6f 20 66 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d  o find the colum
2a29d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 66 6f 72  n index.  ** for
2a29e 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 74 6f 20   each column to 
2a29f 62 65 20 75 70 64 61 74 65 64 20 69 6e 20 74 68  be updated in th
2a2a0 65 20 70 43 68 61 6e 67 65 73 20 61 72 72 61 79  e pChanges array
2a2a1 2e 20 20 46 6f 72 20 65 61 63 68 0a 20 20 2a 2a  .  For each.  **
2a2a2 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70   column to be up
2a2a3 64 61 74 65 64 2c 20 6d 61 6b 65 20 73 75 72 65  dated, make sure
2a2a4 20 77 65 20 68 61 76 65 20 61 75 74 68 6f 72 69   we have authori
2a2a5 7a 61 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65  zation to change
2a2a6 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d  .  ** that colum
2a2a7 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 6e 67 52 6f  n..  */.  chngRo
2a2a8 77 69 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  wid = 0;.  for(i
2a2a9 3d 30 3b 20 69 3c 70 43 68 61 6e 67 65 73 2d 3e  =0; i<pChanges->
2a2aa 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
2a2ab 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
2a2ac 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e  lveExprNames(&sN
2a2ad 43 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69  C, pChanges->a[i
2a2ae 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ].pExpr) ){.    
2a2af 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2a2b0 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20  eanup;.    }.   
2a2b1 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62   for(j=0; j<pTab
2a2b2 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
2a2b3 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
2a2b4 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f  trICmp(pTab->aCo
2a2b5 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 43 68 61  l[j].zName, pCha
2a2b6 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  nges->a[i].zName
2a2b7 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
2a2b8 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( j==pTab->iPK
2a2b9 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
2a2ba 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a 20  chngRowid = 1;. 
2a2bb 20 20 20 20 20 20 20 20 20 70 52 6f 77 69 64 45           pRowidE
2a2bc 78 70 72 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e  xpr = pChanges->
2a2bd 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
2a2be 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 58      }.        aX
2a2bf 52 65 66 5b 6a 5d 20 3d 20 69 3b 0a 20 20 20 20  Ref[j] = i;.    
2a2c0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a2c1 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
2a2c2 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29   j>=pTab->nCol )
2a2c3 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
2a2c4 74 65 33 49 73 52 6f 77 69 64 28 70 43 68 61 6e  te3IsRowid(pChan
2a2c5 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  ges->a[i].zName)
2a2c6 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67   ){.        chng
2a2c7 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
2a2c8 20 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20     pRowidExpr = 
2a2c9 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70  pChanges->a[i].p
2a2ca 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Expr;.      }els
2a2cb 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
2a2cc 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2a2cd 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75  e, "no such colu
2a2ce 6d 6e 3a 20 25 73 22 2c 20 70 43 68 61 6e 67 65  mn: %s", pChange
2a2cf 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  s->a[i].zName);.
2a2d0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64          goto upd
2a2d1 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ate_cleanup;.   
2a2d2 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
2a2d3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2a2d4 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20  UTHORIZATION.   
2a2d5 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b   {.      int rc;
2a2d6 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
2a2d7 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2a2d8 72 73 65 2c 20 53 51 4c 49 54 45 5f 55 50 44 41  rse, SQLITE_UPDA
2a2d9 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  TE, pTab->zName,
2a2da 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a2db 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62              pTab
2a2dc 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c  ->aCol[j].zName,
2a2dd 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2a2de 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ame);.      if( 
2a2df 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  rc==SQLITE_DENY 
2a2e0 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
2a2e1 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2a2e2 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2a2e3 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52  rc==SQLITE_IGNOR
2a2e4 45 20 29 7b 0a 20 20 20 20 20 20 20 20 61 58 52  E ){.        aXR
2a2e5 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a 20 20 20 20  ef[j] = -1;.    
2a2e6 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66    }.    }.#endif
2a2e7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
2a2e8 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74  ate memory for t
2a2e9 68 65 20 61 72 72 61 79 20 61 52 65 67 49 64 78  he array aRegIdx
2a2ea 5b 5d 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  [].  There is on
2a2eb 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 20  e entry in the. 
2a2ec 20 2a 2a 20 61 72 72 61 79 20 66 6f 72 20 65 61   ** array for ea
2a2ed 63 68 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61  ch index associa
2a2ee 74 65 64 20 77 69 74 68 20 74 61 62 6c 65 20 62  ted with table b
2a2ef 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 20 46  eing updated.  F
2a2f0 69 6c 6c 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  ill in.  ** the 
2a2f1 76 61 6c 75 65 20 77 69 74 68 20 61 20 72 65 67  value with a reg
2a2f2 69 73 74 65 72 20 6e 75 6d 62 65 72 20 66 6f 72  ister number for
2a2f3 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 61 72   indices that ar
2a2f4 65 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20 2a  e to be used.  *
2a2f5 2a 20 61 6e 64 20 77 69 74 68 20 7a 65 72 6f 20  * and with zero 
2a2f6 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63  for unused indic
2a2f7 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 6e  es..  */.  for(n
2a2f8 49 64 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  Idx=0, pIdx=pTab
2a2f9 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2a2fa 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2a2fb 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20 69 66  , nIdx++){}.  if
2a2fc 28 20 6e 49 64 78 3e 30 20 29 7b 0a 20 20 20 20  ( nIdx>0 ){.    
2a2fd 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65  aRegIdx = sqlite
2a2fe 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2a2ff 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 2a 29 20   sizeof(Index*) 
2a300 2a 20 6e 49 64 78 20 29 3b 0a 20 20 20 20 69 66  * nIdx );.    if
2a301 28 20 61 52 65 67 49 64 78 3d 3d 30 20 29 20 67  ( aRegIdx==0 ) g
2a302 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2a303 75 70 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 6a 3d  up;.  }.  for(j=
2a304 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
2a305 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
2a306 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b  =pIdx->pNext, j+
2a307 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67 3b  +){.    int reg;
2a308 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77  .    if( chngRow
2a309 69 64 20 29 7b 0a 20 20 20 20 20 20 72 65 67 20  id ){.      reg 
2a30a 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2a30b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2a30c 20 20 20 72 65 67 20 3d 20 30 3b 0a 20 20 20 20     reg = 0;.    
2a30d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
2a30e 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
2a30f 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 58  {.        if( aX
2a310 52 65 66 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75  Ref[pIdx->aiColu
2a311 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 7b 0a 20 20 20  mn[i]]>=0 ){.   
2a312 20 20 20 20 20 20 20 72 65 67 20 3d 20 2b 2b 70         reg = ++p
2a313 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2a314 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a315 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2a316 20 20 20 20 7d 0a 20 20 20 20 61 52 65 67 49 64      }.    aRegId
2a317 78 5b 6a 5d 20 3d 20 72 65 67 3b 0a 20 20 7d 0a  x[j] = reg;.  }.
2a318 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
2a319 20 62 6c 6f 63 6b 20 6f 66 20 72 65 67 69 73 74   block of regist
2a31a 65 72 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  er used to store
2a31b 20 74 68 65 20 63 68 61 6e 67 65 20 72 65 63 6f   the change reco
2a31c 72 64 0a 20 20 2a 2a 20 73 65 6e 74 20 74 6f 20  rd.  ** sent to 
2a31d 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43  sqlite3GenerateC
2a31e 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28  onstraintChecks(
2a31f 29 2e 20 20 54 68 65 72 65 20 61 72 65 20 65 69  ).  There are ei
2a320 74 68 65 72 0a 20 20 2a 2a 20 6f 6e 65 20 6f 72  ther.  ** one or
2a321 20 74 77 6f 20 72 65 67 69 73 74 65 72 73 20 66   two registers f
2a322 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72  or holding the r
2a323 6f 77 69 64 2e 20 20 4f 6e 65 20 72 6f 77 69 64  owid.  One rowid
2a324 20 72 65 67 69 73 74 65 72 0a 20 20 2a 2a 20 69   register.  ** i
2a325 73 20 75 73 65 64 20 69 66 20 63 68 6e 67 52 6f  s used if chngRo
2a326 77 69 64 20 69 73 20 66 61 6c 73 65 20 61 6e 64  wid is false and
2a327 20 74 77 6f 20 61 72 65 20 75 73 65 64 20 69 66   two are used if
2a328 20 63 68 6e 67 52 6f 77 69 64 20 69 73 0a 20 20   chngRowid is.  
2a329 2a 2a 20 74 72 75 65 2e 20 20 46 6f 6c 6c 6f 77  ** true.  Follow
2a32a 69 6e 67 20 74 68 65 73 65 20 61 72 65 20 70 54  ing these are pT
2a32b 61 62 2d 3e 6e 43 6f 6c 20 72 65 67 69 73 74 65  ab->nCol registe
2a32c 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e  r holding column
2a32d 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f  .  ** data..  */
2a32e 0a 20 20 72 65 67 4f 6c 64 52 6f 77 69 64 20 3d  .  regOldRowid =
2a32f 20 72 65 67 4e 65 77 52 6f 77 69 64 20 3d 20 70   regNewRowid = p
2a330 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b  Parse->nMem + 1;
2a331 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  .  pParse->nMem 
2a332 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20  += pTab->nCol + 
2a333 31 3b 0a 20 20 69 66 28 20 63 68 6e 67 52 6f 77  1;.  if( chngRow
2a334 69 64 20 29 7b 0a 20 20 20 20 72 65 67 4e 65 77  id ){.    regNew
2a335 52 6f 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50 61  Rowid++;.    pPa
2a336 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 7d  rse->nMem++;.  }
2a337 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65 67  .  regData = reg
2a338 4e 65 77 52 6f 77 69 64 2b 31 3b 0a 20 0a 0a 20  NewRowid+1;. .. 
2a339 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61   /* Begin genera
2a33a 74 69 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  ting code..  */.
2a33b 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
2a33c 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
2a33d 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
2a33e 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2a33f 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
2a340 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65  sted==0 ) sqlite
2a341 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
2a342 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  s(v);.  sqlite3B
2a343 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
2a344 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44  on(pParse, 1, iD
2a345 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  b);..#ifndef SQL
2a346 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2a347 54 41 42 4c 45 0a 20 20 2f 2a 20 56 69 72 74 75  TABLE.  /* Virtu
2a348 61 6c 20 74 61 62 6c 65 73 20 6d 75 73 74 20 62  al tables must b
2a349 65 20 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61  e handled separa
2a34a 74 65 6c 79 20 2a 2f 0a 20 20 69 66 28 20 49 73  tely */.  if( Is
2a34b 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
2a34c 0a 20 20 20 20 75 70 64 61 74 65 56 69 72 74 75  .    updateVirtu
2a34d 61 6c 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  alTable(pParse, 
2a34e 70 54 61 62 4c 69 73 74 2c 20 70 54 61 62 2c 20  pTabList, pTab, 
2a34f 70 43 68 61 6e 67 65 73 2c 20 70 52 6f 77 69 64  pChanges, pRowid
2a350 45 78 70 72 2c 20 61 58 52 65 66 2c 0a 20 20 20  Expr, aXRef,.   
2a351 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a352 20 20 20 20 70 57 68 65 72 65 29 3b 0a 20 20 20      pWhere);.   
2a353 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20   pWhere = 0;.   
2a354 20 70 54 61 62 4c 69 73 74 20 3d 20 30 3b 0a 20   pTabList = 0;. 
2a355 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
2a356 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64  leanup;.  }.#end
2a357 69 66 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 74  if..  /* Start t
2a358 68 65 20 76 69 65 77 20 63 6f 6e 74 65 78 74 0a  he view context.
2a359 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65    */.  if( isVie
2a35a 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  w ){.    sqlite3
2a35b 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28  AuthContextPush(
2a35c 70 50 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78  pParse, &sContex
2a35d 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  t, pTab->zName);
2a35e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2a35f 61 74 65 20 74 68 65 20 63 6f 64 65 20 66 6f 72  ate the code for
2a360 20 74 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a   triggers..  */.
2a361 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29    if( pTrigger )
2a362 7b 0a 20 20 20 20 69 6e 74 20 69 47 6f 74 6f 3b  {.    int iGoto;
2a363 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20  ..    /* Create 
2a364 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 66 6f  pseudo-tables fo
2a365 72 20 4e 45 57 20 61 6e 64 20 4f 4c 44 0a 20 20  r NEW and OLD.  
2a366 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
2a367 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2a368 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6f 6c 64  _OpenPseudo, old
2a369 49 64 78 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43  Idx, 0, pTab->nC
2a36a 6f 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ol);.    sqlite3
2a36b 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2a36c 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 6e 65 77  _OpenPseudo, new
2a36d 49 64 78 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43  Idx, 0, pTab->nC
2a36e 6f 6c 29 3b 0a 0a 20 20 20 20 69 47 6f 74 6f 20  ol);..    iGoto 
2a36f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2a370 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2a371 30 2c 20 30 29 3b 0a 20 20 20 20 61 64 64 72 20  0, 0);.    addr 
2a372 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2a373 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 69  eLabel(v);.    i
2a374 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69 67 67  BeginBeforeTrigg
2a375 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  er = sqlite3Vdbe
2a376 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
2a377 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43      if( sqlite3C
2a378 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
2a379 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
2a37a 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61 6e  TK_UPDATE, pChan
2a37b 67 65 73 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ges, .          
2a37c 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20  TRIGGER_BEFORE, 
2a37d 70 54 61 62 2c 20 6e 65 77 49 64 78 2c 20 6f 6c  pTab, newIdx, ol
2a37e 64 49 64 78 2c 20 6f 6e 45 72 72 6f 72 2c 20 61  dIdx, onError, a
2a37f 64 64 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ddr, .          
2a380 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 26  &old_col_mask, &
2a381 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 29 20 29 7b  new_col_mask) ){
2a382 0a 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61  .      goto upda
2a383 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  te_cleanup;.    
2a384 7d 0a 20 20 20 20 69 45 6e 64 42 65 66 6f 72 65  }.    iEndBefore
2a385 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
2a386 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a387 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20  P_Goto, 0, 0);. 
2a388 20 20 20 69 42 65 67 69 6e 41 66 74 65 72 54 72     iBeginAfterTr
2a389 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56  igger = sqlite3V
2a38a 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2a38b 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
2a38c 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
2a38d 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
2a38e 72 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43  r, TK_UPDATE, pC
2a38f 68 61 6e 67 65 73 2c 20 0a 20 20 20 20 20 20 20  hanges, .       
2a390 20 20 20 54 52 49 47 47 45 52 5f 41 46 54 45 52     TRIGGER_AFTER
2a391 2c 20 70 54 61 62 2c 20 6e 65 77 49 64 78 2c 20  , pTab, newIdx, 
2a392 6f 6c 64 49 64 78 2c 20 6f 6e 45 72 72 6f 72 2c  oldIdx, onError,
2a393 20 61 64 64 72 2c 20 0a 20 20 20 20 20 20 20 20   addr, .        
2a394 20 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c    &old_col_mask,
2a395 20 26 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 29 20   &new_col_mask) 
2a396 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75 70  ){.      goto up
2a397 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
2a398 20 20 7d 0a 20 20 20 20 69 45 6e 64 41 66 74 65    }.    iEndAfte
2a399 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  rTrigger = sqlit
2a39a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a39b 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a  OP_Goto, 0, 0);.
2a39c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2a39d 75 6d 70 48 65 72 65 28 76 2c 20 69 47 6f 74 6f  umpHere(v, iGoto
2a39e 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
2a39f 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f  we are trying to
2a3a0 20 75 70 64 61 74 65 20 61 20 76 69 65 77 2c 20   update a view, 
2a3a1 72 65 61 6c 69 7a 65 20 74 68 61 74 20 76 69 65  realize that vie
2a3a2 77 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70  w into.  ** a ep
2a3a3 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20  hemeral table.. 
2a3a4 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64   */.#if !defined
2a3a5 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
2a3a6 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  W) && !defined(S
2a3a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2a3a8 45 52 29 0a 20 20 69 66 28 20 69 73 56 69 65 77  ER).  if( isView
2a3a9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d   ){.    sqlite3M
2a3aa 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 70  aterializeView(p
2a3ab 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 57 68  Parse, pTab, pWh
2a3ac 65 72 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a  ere, iCur);.  }.
2a3ad 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 73  #endif..  /* Res
2a3ae 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  olve the column 
2a3af 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 74 68 65  names in all the
2a3b0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
2a3b1 74 68 65 0a 20 20 2a 2a 20 57 48 45 52 45 20 63  the.  ** WHERE c
2a3b2 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  lause..  */.  if
2a3b3 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
2a3b4 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
2a3b5 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 67  pWhere) ){.    g
2a3b6 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2a3b7 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65  up;.  }..  /* Be
2a3b8 67 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  gin the database
2a3b9 20 73 63 61 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c   scan.  */.  sql
2a3ba 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a3bb 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65  , OP_Null, 0, re
2a3bc 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 70 57  gOldRowid);.  pW
2a3bd 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68  Info = sqlite3Wh
2a3be 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c  ereBegin(pParse,
2a3bf 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72   pTabList, pWher
2a3c0 65 2c 30 2c 20 57 48 45 52 45 5f 4f 4e 45 50 41  e,0, WHERE_ONEPA
2a3c1 53 53 5f 44 45 53 49 52 45 44 29 3b 0a 20 20 69  SS_DESIRED);.  i
2a3c2 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67  f( pWInfo==0 ) g
2a3c3 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2a3c4 75 70 3b 0a 20 20 6f 6b 4f 6e 65 50 61 73 73 20  up;.  okOnePass 
2a3c5 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50  = pWInfo->okOneP
2a3c6 61 73 73 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d  ass;..  /* Remem
2a3c7 62 65 72 20 74 68 65 20 72 6f 77 69 64 20 6f 66  ber the rowid of
2a3c8 20 65 76 65 72 79 20 69 74 65 6d 20 74 6f 20 62   every item to b
2a3c9 65 20 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a  e updated..  */.
2a3ca 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a3cb 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c  Op2(v, OP_Rowid,
2a3cc 20 69 43 75 72 2c 20 72 65 67 4f 6c 64 52 6f 77   iCur, regOldRow
2a3cd 69 64 29 3b 0a 20 20 69 66 28 20 21 6f 6b 4f 6e  id);.  if( !okOn
2a3ce 65 50 61 73 73 20 29 7b 0a 20 20 20 20 72 65 67  ePass ){.    reg
2a3cf 52 6f 77 53 65 74 20 3d 20 2b 2b 70 50 61 72 73  RowSet = ++pPars
2a3d0 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
2a3d1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a3d2 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 2c 20  , OP_RowSetAdd, 
2a3d3 72 65 67 52 6f 77 53 65 74 2c 20 72 65 67 4f 6c  regRowSet, regOl
2a3d4 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20  dRowid);.  }..  
2a3d5 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74 61 62  /* End the datab
2a3d6 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20  ase scan loop.. 
2a3d7 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 57 68 65   */.  sqlite3Whe
2a3d8 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 0a  reEnd(pWInfo);..
2a3d9 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
2a3da 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 75 70 64  the count of upd
2a3db 61 74 65 64 20 72 6f 77 73 0a 20 20 2a 2f 0a 20  ated rows.  */. 
2a3dc 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26   if( db->flags &
2a3dd 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77   SQLITE_CountRow
2a3de 73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72  s && !pParse->tr
2a3df 69 67 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 72  igStack ){.    r
2a3e0 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70  egRowCount = ++p
2a3e1 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2a3e2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a3e3 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2a3e4 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74  , 0, regRowCount
2a3e5 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69  );.  }..  if( !i
2a3e6 73 56 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20  sView ){.    /* 
2a3e7 0a 20 20 20 20 2a 2a 20 4f 70 65 6e 20 65 76 65  .    ** Open eve
2a3e8 72 79 20 69 6e 64 65 78 20 74 68 61 74 20 6e 65  ry index that ne
2a3e9 65 64 73 20 75 70 64 61 74 69 6e 67 2e 20 20 4e  eds updating.  N
2a3ea 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 79 0a  ote that if any.
2a3eb 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 63 6f 75      ** index cou
2a3ec 6c 64 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 69  ld potentially i
2a3ed 6e 76 6f 6b 65 20 61 20 52 45 50 4c 41 43 45 20  nvoke a REPLACE 
2a3ee 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74  conflict resolut
2a3ef 69 6f 6e 20 0a 20 20 20 20 2a 2a 20 61 63 74 69  ion .    ** acti
2a3f0 6f 6e 2c 20 74 68 65 6e 20 77 65 20 6e 65 65 64  on, then we need
2a3f1 20 74 6f 20 6f 70 65 6e 20 61 6c 6c 20 69 6e 64   to open all ind
2a3f2 69 63 65 73 20 62 65 63 61 75 73 65 20 77 65 20  ices because we 
2a3f3 6d 69 67 68 74 20 6e 65 65 64 0a 20 20 20 20 2a  might need.    *
2a3f4 2a 20 74 6f 20 62 65 20 64 65 6c 65 74 69 6e 67  * to be deleting
2a3f5 20 73 6f 6d 65 20 72 65 63 6f 72 64 73 2e 0a 20   some records.. 
2a3f6 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 6f     */.    if( !o
2a3f7 6b 4f 6e 65 50 61 73 73 20 29 20 73 71 6c 69 74  kOnePass ) sqlit
2a3f8 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
2a3f9 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70  se, iCur, iDb, p
2a3fa 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  Tab, OP_OpenWrit
2a3fb 65 29 3b 20 0a 20 20 20 20 69 66 28 20 6f 6e 45  e); .    if( onE
2a3fc 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
2a3fd 20 29 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c   ){.      openAl
2a3fe 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  l = 1;.    }else
2a3ff 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20  {.      openAll 
2a400 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  = 0;.      for(p
2a401 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
2a402 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
2a403 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
2a404 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45     if( pIdx->onE
2a405 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
2a406 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 70   ){.          op
2a407 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20  enAll = 1;.     
2a408 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a409 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2a40a 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c    }.    for(i=0,
2a40b 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
2a40c 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
2a40d 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29  Idx->pNext, i++)
2a40e 7b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 65 6e  {.      if( open
2a40f 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b 69  All || aRegIdx[i
2a410 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 4b  ]>0 ){.        K
2a411 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73  eyInfo *pKey = s
2a412 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
2a413 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29  fo(pParse, pIdx)
2a414 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2a415 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2a416 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 43 75  P_OpenWrite, iCu
2a417 72 2b 69 2b 31 2c 20 70 49 64 78 2d 3e 74 6e 75  r+i+1, pIdx->tnu
2a418 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20  m, iDb,.        
2a419 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2a41a 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
2a41b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
2a41c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2a41d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3e 69 43   pParse->nTab>iC
2a41e 75 72 2b 69 2b 31 20 29 3b 0a 20 20 20 20 20 20  ur+i+1 );.      
2a41f 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20  }.    }.  }.  . 
2a420 20 2f 2a 20 4a 75 6d 70 20 62 61 63 6b 20 74 6f   /* Jump back to
2a421 20 74 68 69 73 20 70 6f 69 6e 74 20 69 66 20 61   this point if a
2a422 20 74 72 69 67 67 65 72 20 65 6e 63 6f 75 6e 74   trigger encount
2a423 65 72 73 20 61 6e 20 49 47 4e 4f 52 45 20 63 6f  ers an IGNORE co
2a424 6e 73 74 72 61 69 6e 74 2e 20 2a 2f 0a 20 20 69  nstraint. */.  i
2a425 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  f( pTrigger ){. 
2a426 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2a427 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64  solveLabel(v, ad
2a428 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  dr);.  }..  /* T
2a429 6f 70 20 6f 66 20 74 68 65 20 75 70 64 61 74 65  op of the update
2a42a 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20 6f   loop */.  if( o
2a42b 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20 20 20  kOnePass ){.    
2a42c 69 6e 74 20 61 31 20 3d 20 73 71 6c 69 74 65 33  int a1 = sqlite3
2a42d 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2a42e 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 4f 6c 64  _NotNull, regOld
2a42f 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72  Rowid);.    addr
2a430 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2a431 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29  dOp0(v, OP_Goto)
2a432 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a433 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 31 29  eJumpHere(v, a1)
2a434 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
2a435 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
2a436 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 6f  eAddOp3(v, OP_Ro
2a437 77 53 65 74 52 65 61 64 2c 20 72 65 67 52 6f 77  wSetRead, regRow
2a438 53 65 74 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f  Set, 0, regOldRo
2a439 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  wid);.  }..  if(
2a43a 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
2a43b 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 0a 20   int regRowid;. 
2a43c 20 20 20 69 6e 74 20 72 65 67 52 6f 77 3b 0a 20     int regRow;. 
2a43d 20 20 20 69 6e 74 20 72 65 67 43 6f 6c 73 3b 0a     int regCols;.
2a43e 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 63 75 72  .    /* Make cur
2a43f 73 6f 72 20 69 43 75 72 20 70 6f 69 6e 74 20 74  sor iCur point t
2a440 6f 20 74 68 65 20 72 65 63 6f 72 64 20 74 68 61  o the record tha
2a441 74 20 69 73 20 62 65 69 6e 67 20 75 70 64 61 74  t is being updat
2a442 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ed..    */.    s
2a443 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a444 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
2a445 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72 65  , iCur, addr, re
2a446 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20 20 20  gOldRowid);..   
2a447 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65   /* Generate the
2a448 20 4f 4c 44 20 74 61 62 6c 65 0a 20 20 20 20 2a   OLD table.    *
2a449 2f 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d  /.    regRowid =
2a44a 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2a44b 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2a44c 72 65 67 52 6f 77 20 3d 20 73 71 6c 69 74 65 33  regRow = sqlite3
2a44d 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2a44e 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
2a44f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a450 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67  Rowid, iCur, reg
2a451 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20  Rowid);.    if( 
2a452 21 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 29 7b  !old_col_mask ){
2a453 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a454 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2a455 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 29 3b  ull, 0, regRow);
2a456 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2a457 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a458 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74  Op2(v, OP_RowDat
2a459 61 2c 20 69 43 75 72 2c 20 72 65 67 52 6f 77 29  a, iCur, regRow)
2a45a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2a45b 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2a45c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6f 6c 64 49   OP_Insert, oldI
2a45d 64 78 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52  dx, regRow, regR
2a45e 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 47  owid);..    /* G
2a45f 65 6e 65 72 61 74 65 20 74 68 65 20 4e 45 57 20  enerate the NEW 
2a460 74 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20  table.    */.   
2a461 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29   if( chngRowid )
2a462 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2a463 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28  xprCodeAndCache(
2a464 70 50 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78  pParse, pRowidEx
2a465 70 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  pr, regRowid);. 
2a466 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2a467 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
2a468 74 42 65 49 6e 74 2c 20 72 65 67 52 6f 77 69 64  tBeInt, regRowid
2a469 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2a46a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a46b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
2a46c 64 2c 20 69 43 75 72 2c 20 72 65 67 52 6f 77 69  d, iCur, regRowi
2a46d 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  d);.    }.    re
2a46e 67 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 47  gCols = sqlite3G
2a46f 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
2a470 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b  se, pTab->nCol);
2a471 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2a472 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
2a473 7b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 70  {.      if( i==p
2a474 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
2a475 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a476 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2a477 6c 6c 2c 20 30 2c 20 72 65 67 43 6f 6c 73 2b 69  ll, 0, regCols+i
2a478 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69  );.        conti
2a479 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nue;.      }.   
2a47a 20 20 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b     j = aXRef[i];
2a47b 0a 20 20 20 20 20 20 69 66 28 20 28 69 3c 33 32  .      if( (i<32
2a47c 20 26 26 20 28 6e 65 77 5f 63 6f 6c 5f 6d 61 73   && (new_col_mas
2a47d 6b 26 28 28 75 33 32 29 31 3c 3c 69 29 29 21 3d  k&((u32)1<<i))!=
2a47e 30 29 20 7c 7c 20 6e 65 77 5f 63 6f 6c 5f 6d 61  0) || new_col_ma
2a47f 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29  sk==0xffffffff )
2a480 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3c  {.        if( j<
2a481 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
2a482 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a483 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69  (v, OP_Column, i
2a484 43 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c 73 2b  Cur, i, regCols+
2a485 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  i);.          sq
2a486 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
2a487 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 29 3b 0a  lt(v, pTab, i);.
2a488 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2a489 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2a48a 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65  ExprCodeAndCache
2a48b 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67 65  (pParse, pChange
2a48c 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72  s->a[j].pExpr, r
2a48d 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20  egCols+i);.     
2a48e 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
2a48f 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2a490 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a491 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 43 6f  P_Null, 0, regCo
2a492 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ls+i);.      }. 
2a493 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2a494 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2a495 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
2a496 43 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  Cols, pTab->nCol
2a497 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 20 20 69  , regRow);.    i
2a498 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
2a499 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65      sqlite3Table
2a49a 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
2a49b 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Tab);.      sqli
2a49c 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
2a49d 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
2a49e 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61 62  e, regCols, pTab
2a49f 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20  ->nCol);.    }. 
2a4a0 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2a4a1 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
2a4a2 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54 61 62  e, regCols, pTab
2a4a3 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 2f 2a 20  ->nCol);.    /* 
2a4a4 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
2a4a5 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63   ) goto update_c
2a4a6 6c 65 61 6e 75 70 3b 20 2a 2f 0a 20 20 20 20 73  leanup; */.    s
2a4a7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a4a8 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 6e  (v, OP_Insert, n
2a4a9 65 77 49 64 78 2c 20 72 65 67 52 6f 77 2c 20 72  ewIdx, regRow, r
2a4aa 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  egRowid);.    sq
2a4ab 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2a4ac 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
2a4ad 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
2a4ae 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2a4af 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 29  (pParse, regRow)
2a4b0 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ;..    sqlite3Vd
2a4b1 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2a4b2 6f 74 6f 2c 20 30 2c 20 69 42 65 67 69 6e 42 65  oto, 0, iBeginBe
2a4b3 66 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20  foreTrigger);.  
2a4b4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2a4b5 70 48 65 72 65 28 76 2c 20 69 45 6e 64 42 65 66  pHere(v, iEndBef
2a4b6 6f 72 65 54 72 69 67 67 65 72 29 3b 0a 20 20 7d  oreTrigger);.  }
2a4b7 0a 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20  ..  if( !isView 
2a4b8 29 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 6f  ){.    /* Loop o
2a4b9 76 65 72 20 65 76 65 72 79 20 72 65 63 6f 72 64  ver every record
2a4ba 20 74 68 61 74 20 6e 65 65 64 73 20 75 70 64 61   that needs upda
2a4bb 74 69 6e 67 2e 20 20 57 65 20 68 61 76 65 20 74  ting.  We have t
2a4bc 6f 20 6c 6f 61 64 0a 20 20 20 20 2a 2a 20 74 68  o load.    ** th
2a4bd 65 20 6f 6c 64 20 64 61 74 61 20 66 6f 72 20 65  e old data for e
2a4be 61 63 68 20 72 65 63 6f 72 64 20 74 6f 20 62 65  ach record to be
2a4bf 20 75 70 64 61 74 65 64 20 62 65 63 61 75 73 65   updated because
2a4c0 20 73 6f 6d 65 20 63 6f 6c 75 6d 6e 73 0a 20 20   some columns.  
2a4c1 20 20 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 63    ** might not c
2a4c2 68 61 6e 67 65 20 61 6e 64 20 77 65 20 77 69 6c  hange and we wil
2a4c3 6c 20 6e 65 65 64 20 74 6f 20 63 6f 70 79 20 74  l need to copy t
2a4c4 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 20 20  he old value..  
2a4c5 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20 6f    ** Also, the o
2a4c6 6c 64 20 64 61 74 61 20 69 73 20 6e 65 65 64 65  ld data is neede
2a4c7 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  d to delete the 
2a4c8 6f 6c 64 20 69 6e 64 65 78 20 65 6e 74 72 69 65  old index entrie
2a4c9 73 2e 0a 20 20 20 20 2a 2a 20 53 6f 20 6d 61 6b  s..    ** So mak
2a4ca 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69  e the cursor poi
2a4cb 6e 74 20 61 74 20 74 68 65 20 6f 6c 64 20 72 65  nt at the old re
2a4cc 63 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  cord..    */.   
2a4cd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a4ce 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
2a4cf 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20  ts, iCur, addr, 
2a4d0 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20  regOldRowid);.. 
2a4d1 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 63     /* If the rec
2a4d2 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20  ord number will 
2a4d3 63 68 61 6e 67 65 2c 20 70 75 73 68 20 74 68 65  change, push the
2a4d4 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61   record number a
2a4d5 73 20 69 74 0a 20 20 20 20 2a 2a 20 77 69 6c 6c  s it.    ** will
2a4d6 20 62 65 20 61 66 74 65 72 20 74 68 65 20 75 70   be after the up
2a4d7 64 61 74 65 2e 20 28 54 68 65 20 6f 6c 64 20 72  date. (The old r
2a4d8 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
2a4d9 63 75 72 72 65 6e 74 6c 79 0a 20 20 20 20 2a 2a  currently.    **
2a4da 20 6f 6e 20 74 6f 70 20 6f 66 20 74 68 65 20 73   on top of the s
2a4db 74 61 63 6b 2e 29 0a 20 20 20 20 2a 2f 0a 20 20  tack.).    */.  
2a4dc 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20    if( chngRowid 
2a4dd 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2a4de 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2a4df 20 70 52 6f 77 69 64 45 78 70 72 2c 20 72 65 67   pRowidExpr, reg
2a4e0 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  NewRowid);.     
2a4e1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a4e2 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
2a4e3 6e 74 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29  nt, regNewRowid)
2a4e4 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2a4e5 43 6f 6d 70 75 74 65 20 6e 65 77 20 64 61 74 61  Compute new data
2a4e6 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72 64   for this record
2a4e7 2e 20 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  .  .    */.    f
2a4e8 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e  or(i=0; i<pTab->
2a4e9 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
2a4ea 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69    if( i==pTab->i
2a4eb 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  PKey ){.        
2a4ec 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a4ed 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2a4ee 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20   regData+i);.   
2a4ef 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2a4f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a 20 3d       }.      j =
2a4f1 20 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20   aXRef[i];.     
2a4f2 20 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20   if( j<0 ){.    
2a4f3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a4f4 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
2a4f5 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67  mn, iCur, i, reg
2a4f6 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 20  Data+i);.       
2a4f7 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
2a4f8 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69  fault(v, pTab, i
2a4f9 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2a4fa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2a4fb 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
2a4fc 70 43 68 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70  pChanges->a[j].p
2a4fd 45 78 70 72 2c 20 72 65 67 44 61 74 61 2b 69 29  Expr, regData+i)
2a4fe 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2a4ff 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74  .    /* Do const
2a500 72 61 69 6e 74 20 63 68 65 63 6b 73 0a 20 20 20  raint checks.   
2a501 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47   */.    sqlite3G
2a502 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
2a503 74 43 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20  tChecks(pParse, 
2a504 70 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e  pTab, iCur, regN
2a505 65 77 52 6f 77 69 64 2c 0a 20 20 20 20 20 20 20  ewRowid,.       
2a506 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a507 20 20 20 20 20 20 20 20 20 20 20 20 20 61 52 65               aRe
2a508 67 49 64 78 2c 20 63 68 6e 67 52 6f 77 69 64 2c  gIdx, chngRowid,
2a509 20 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   1,.            
2a50a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a50b 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 2c          onError,
2a50c 20 61 64 64 72 2c 20 30 29 3b 0a 0a 20 20 20 20   addr, 0);..    
2a50d 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6f 6c  /* Delete the ol
2a50e 64 20 69 6e 64 69 63 65 73 20 66 6f 72 20 74 68  d indices for th
2a50f 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f 72 64  e current record
2a510 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6a 31 20  ..    */.    j1 
2a511 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2a512 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
2a513 73 74 73 2c 20 69 43 75 72 2c 20 30 2c 20 72 65  sts, iCur, 0, re
2a514 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  gOldRowid);.    
2a515 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
2a516 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 70 50  owIndexDelete(pP
2a517 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72  arse, pTab, iCur
2a518 2c 20 61 52 65 67 49 64 78 29 3b 0a 0a 20 20 20  , aRegIdx);..   
2a519 20 2f 2a 20 49 66 20 63 68 61 6e 67 69 6e 67 20   /* If changing 
2a51a 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
2a51b 72 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6f 6c  r, delete the ol
2a51c 64 20 72 65 63 6f 72 64 2e 0a 20 20 20 20 2a 2f  d record..    */
2a51d 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77  .    if( chngRow
2a51e 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  id ){.      sqli
2a51f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a520 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75 72   OP_Delete, iCur
2a521 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
2a522 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2a523 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20  ere(v, j1);..   
2a524 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 6e   /* Create the n
2a525 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73  ew index entries
2a526 20 61 6e 64 20 74 68 65 20 6e 65 77 20 72 65 63   and the new rec
2a527 6f 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ord..    */.    
2a528 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49  sqlite3CompleteI
2a529 6e 73 65 72 74 69 6f 6e 28 70 50 61 72 73 65 2c  nsertion(pParse,
2a52a 20 70 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67   pTab, iCur, reg
2a52b 4e 65 77 52 6f 77 69 64 2c 20 0a 20 20 20 20 20  NewRowid, .     
2a52c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a52d 20 20 20 20 20 20 20 20 61 52 65 67 49 64 78 2c          aRegIdx,
2a52e 20 31 2c 20 2d 31 2c 20 30 2c 20 30 29 3b 0a 20   1, -1, 0, 0);. 
2a52f 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65   }..  /* Increme
2a530 6e 74 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74  nt the row count
2a531 65 72 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  er .  */.  if( d
2a532 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2a533 45 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 21  E_CountRows && !
2a534 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63  pParse->trigStac
2a535 6b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  k){.    sqlite3V
2a536 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a537 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f  AddImm, regRowCo
2a538 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20  unt, 1);.  }..  
2a539 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
2a53a 74 72 69 67 67 65 72 73 2c 20 63 6c 6f 73 65 20  triggers, close 
2a53b 61 6c 6c 20 74 68 65 20 63 75 72 73 6f 72 73 20  all the cursors 
2a53c 61 66 74 65 72 20 65 61 63 68 20 69 74 65 72 61  after each itera
2a53d 74 69 6f 6e 0a 20 20 2a 2a 20 74 68 72 6f 75 67  tion.  ** throug
2a53e 68 20 74 68 65 20 6c 6f 6f 70 2e 20 20 54 68 65  h the loop.  The
2a53f 20 66 69 72 65 20 74 68 65 20 61 66 74 65 72 20   fire the after 
2a540 74 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20  triggers..  */. 
2a541 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b   if( pTrigger ){
2a542 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a543 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2a544 6f 2c 20 30 2c 20 69 42 65 67 69 6e 41 66 74 65  o, 0, iBeginAfte
2a545 72 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 73  rTrigger);.    s
2a546 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2a547 72 65 28 76 2c 20 69 45 6e 64 41 66 74 65 72 54  re(v, iEndAfterT
2a548 72 69 67 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20  rigger);.  }..  
2a549 2f 2a 20 52 65 70 65 61 74 20 74 68 65 20 61 62  /* Repeat the ab
2a54a 6f 76 65 20 77 69 74 68 20 74 68 65 20 6e 65 78  ove with the nex
2a54b 74 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20 75  t record to be u
2a54c 70 64 61 74 65 64 2c 20 75 6e 74 69 6c 0a 20 20  pdated, until.  
2a54d 2a 2a 20 61 6c 6c 20 72 65 63 6f 72 64 20 73 65  ** all record se
2a54e 6c 65 63 74 65 64 20 62 79 20 74 68 65 20 57 48  lected by the WH
2a54f 45 52 45 20 63 6c 61 75 73 65 20 68 61 76 65 20  ERE clause have 
2a550 62 65 65 6e 20 75 70 64 61 74 65 64 2e 0a 20 20  been updated..  
2a551 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
2a552 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2a553 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 73  o, 0, addr);.  s
2a554 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2a555 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20  re(v, addr);..  
2a556 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62  /* Close all tab
2a557 6c 65 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  les */.  for(i=0
2a558 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
2a559 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2a55a 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b  pIdx->pNext, i++
2a55b 29 7b 0a 20 20 20 20 69 66 28 20 6f 70 65 6e 41  ){.    if( openA
2a55c 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d  ll || aRegIdx[i]
2a55d 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  >0 ){.      sqli
2a55e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a55f 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2b   OP_Close, iCur+
2a560 69 2b 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  i+1, 0);.    }. 
2a561 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
2a562 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
2a563 73 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20 20  se, iCur, 0);.  
2a564 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a  if( pTrigger ){.
2a565 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a566 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
2a567 65 2c 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a 20  e, newIdx, 0);. 
2a568 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2a569 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
2a56a 2c 20 6f 6c 64 49 64 78 2c 20 30 29 3b 0a 20 20  , oldIdx, 0);.  
2a56b 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74  }..  /*.  ** Ret
2a56c 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
2a56d 66 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65  f rows that were
2a56e 20 63 68 61 6e 67 65 64 2e 20 49 66 20 74 68 69   changed. If thi
2a56f 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20 20  s routine is .  
2a570 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  ** generating co
2a571 64 65 20 62 65 63 61 75 73 65 20 6f 66 20 61 20  de because of a 
2a572 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e  call to sqlite3N
2a573 65 73 74 65 64 50 61 72 73 65 28 29 2c 20 64 6f  estedParse(), do
2a574 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65   not.  ** invoke
2a575 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
2a576 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69  nction..  */.  i
2a577 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  f( db->flags & S
2a578 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
2a579 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67  && !pParse->trig
2a57a 53 74 61 63 6b 20 26 26 20 70 50 61 72 73 65 2d  Stack && pParse-
2a57b 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20  >nested==0 ){.  
2a57c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a57d 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
2a57e 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74  Row, regRowCount
2a57f 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
2a580 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
2a581 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  v, 1);.    sqlit
2a582 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2a583 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
2a584 41 4d 45 2c 20 22 72 6f 77 73 20 75 70 64 61 74  AME, "rows updat
2a585 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ed", SQLITE_STAT
2a586 49 43 29 3b 0a 20 20 7d 0a 0a 75 70 64 61 74 65  IC);.  }..update
2a587 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69  _cleanup:.  sqli
2a588 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f  te3AuthContextPo
2a589 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20  p(&sContext);.  
2a58a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2a58b 2c 20 61 52 65 67 49 64 78 29 3b 0a 20 20 73 71  , aRegIdx);.  sq
2a58c 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2a58d 61 58 52 65 66 29 3b 0a 20 20 73 71 6c 69 74 65  aXRef);.  sqlite
2a58e 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64  3SrcListDelete(d
2a58f 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  b, pTabList);.  
2a590 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2a591 65 6c 65 74 65 28 64 62 2c 20 70 43 68 61 6e 67  elete(db, pChang
2a592 65 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  es);.  sqlite3Ex
2a593 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68  prDelete(db, pWh
2a594 65 72 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  ere);.  return;.
2a595 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2a596 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2a597 42 4c 45 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  BLE./*.** Genera
2a598 74 65 20 63 6f 64 65 20 66 6f 72 20 61 6e 20 55  te code for an U
2a599 50 44 41 54 45 20 6f 66 20 61 20 76 69 72 74 75  PDATE of a virtu
2a59a 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
2a59b 54 68 65 20 73 74 72 61 74 65 67 79 20 69 73 20  The strategy is 
2a59c 74 68 61 74 20 77 65 20 63 72 65 61 74 65 20 61  that we create a
2a59d 6e 20 65 70 68 65 6d 65 72 69 61 6c 20 74 61 62  n ephemerial tab
2a59e 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  le that contains
2a59f 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 72 6f 77  .** for each row
2a5a0 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 3a 0a   to be changed:.
2a5a1 2a 2a 0a 2a 2a 20 20 20 28 41 29 20 20 54 68 65  **.**   (A)  The
2a5a2 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 69 64 20   original rowid 
2a5a3 6f 66 20 74 68 61 74 20 72 6f 77 2e 0a 2a 2a 20  of that row..** 
2a5a4 20 20 28 42 29 20 20 54 68 65 20 72 65 76 69 73    (B)  The revis
2a5a5 65 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65  ed rowid for the
2a5a6 20 72 6f 77 2e 20 28 6e 6f 74 65 31 29 0a 2a 2a   row. (note1).**
2a5a7 20 20 20 28 43 29 20 20 54 68 65 20 63 6f 6e 74     (C)  The cont
2a5a8 65 6e 74 20 6f 66 20 65 76 65 72 79 20 63 6f 6c  ent of every col
2a5a9 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 2e 0a  umn in the row..
2a5aa 2a 2a 0a 2a 2a 20 54 68 65 6e 20 77 65 20 6c 6f  **.** Then we lo
2a5ab 6f 70 20 6f 76 65 72 20 74 68 69 73 20 65 70 68  op over this eph
2a5ac 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 61 6e 64  emeral table and
2a5ad 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e   for each row in
2a5ae 0a 2a 2a 20 74 68 65 20 65 70 68 65 72 6d 65 72  .** the ephermer
2a5af 61 6c 20 74 61 62 6c 65 20 63 61 6c 6c 20 56 55  al table call VU
2a5b0 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  pdate..**.** Whe
2a5b1 6e 20 66 69 6e 69 73 68 65 64 2c 20 64 72 6f 70  n finished, drop
2a5b2 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74   the ephemeral t
2a5b3 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 6e 6f 74  able..**.** (not
2a5b4 65 31 29 20 41 63 74 75 61 6c 6c 79 2c 20 69 66  e1) Actually, if
2a5b5 20 77 65 20 6b 6e 6f 77 20 69 6e 20 61 64 76 61   we know in adva
2a5b6 6e 63 65 20 74 68 61 74 20 28 41 29 20 69 73 20  nce that (A) is 
2a5b7 61 6c 77 61 79 73 20 74 68 65 20 73 61 6d 65 0a  always the same.
2a5b8 2a 2a 20 61 73 20 28 42 29 20 77 65 20 6f 6e 6c  ** as (B) we onl
2a5b9 79 20 73 74 6f 72 65 20 28 41 29 2c 20 74 68 65  y store (A), the
2a5ba 6e 20 64 75 70 6c 69 63 61 74 65 20 28 41 29 20  n duplicate (A) 
2a5bb 77 68 65 6e 20 70 75 6c 6c 69 6e 67 0a 2a 2a 20  when pulling.** 
2a5bc 69 74 20 6f 75 74 20 6f 66 20 74 68 65 20 65 70  it out of the ep
2a5bd 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 62 65  hemeral table be
2a5be 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 56 55 70  fore calling VUp
2a5bf 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  date..*/.static 
2a5c0 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74 75  void updateVirtu
2a5c1 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  alTable(.  Parse
2a5c2 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2a5c3 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
2a5c4 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
2a5c5 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
2a5c6 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
2a5c7 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64 69  table to be modi
2a5c8 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20  fied */.  Table 
2a5c9 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f  *pTab,         /
2a5ca 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74 61  * The virtual ta
2a5cb 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ble */.  ExprLis
2a5cc 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a  t *pChanges,  /*
2a5cd 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20   The columns to 
2a5ce 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55 50  change in the UP
2a5cf 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  DATE statement *
2a5d0 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64  /.  Expr *pRowid
2a5d1 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72  ,        /* Expr
2a5d2 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20 72  ession used to r
2a5d3 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f 77  ecompute the row
2a5d4 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52  id */.  int *aXR
2a5d5 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ef,          /* 
2a5d6 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f 6c  Mapping from col
2a5d7 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f 20  umns of pTab to 
2a5d8 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61 6e  entries in pChan
2a5d9 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ges */.  Expr *p
2a5da 57 68 65 72 65 20 20 20 20 20 20 20 20 20 2f 2a  Where         /*
2a5db 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66   WHERE clause of
2a5dc 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74   the UPDATE stat
2a5dd 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  ement */.){.  Vd
2a5de 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2a5df 70 56 64 62 65 3b 20 20 2f 2a 20 56 69 72 74 75  pVdbe;  /* Virtu
2a5e0 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72  al machine under
2a5e1 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f   construction */
2a5e2 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
2a5e3 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ist = 0;     /* 
2a5e4 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  The result set o
2a5e5 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
2a5e6 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65  tement */.  Sele
2a5e7 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b  ct *pSelect = 0;
2a5e8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
2a5e9 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ECT statement */
2a5ea 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20  .  Expr *pExpr; 
2a5eb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a5ec 54 65 6d 70 6f 72 61 72 79 20 65 78 70 72 65 73  Temporary expres
2a5ed 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 65 70  sion */.  int ep
2a5ee 68 65 6d 54 61 62 3b 20 20 20 20 20 20 20 20 20  hemTab;         
2a5ef 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 6f 6c      /* Table hol
2a5f0 64 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ding the result 
2a5f1 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f  of the SELECT */
2a5f2 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2a5f3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a5f4 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
2a5f5 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20    int addr;     
2a5f6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2a5f7 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f 66  ddress of top of
2a5f8 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69   loop */.  int i
2a5f9 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Reg;            
2a5fa 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65       /* First re
2a5fb 67 69 73 74 65 72 20 69 6e 20 73 65 74 20 70 61  gister in set pa
2a5fc 73 73 65 64 20 74 6f 20 4f 50 5f 56 55 70 64 61  ssed to OP_VUpda
2a5fd 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  te */.  sqlite3 
2a5fe 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2a5ff 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f  ; /* Database co
2a600 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f  nnection */.  co
2a601 6e 73 74 20 63 68 61 72 20 2a 70 56 74 61 62 20  nst char *pVtab 
2a602 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  = (const char*)p
2a603 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 53 65  Tab->pVtab;.  Se
2a604 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a 0a  lectDest dest;..
2a605 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74    /* Construct t
2a606 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2a607 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20 66 69  ent that will fi
2a608 6e 64 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65  nd the new value
2a609 73 20 66 6f 72 0a 20 20 2a 2a 20 61 6c 6c 20 75  s for.  ** all u
2a60a 70 64 61 74 65 64 20 72 6f 77 73 2e 20 0a 20 20  pdated rows. .  
2a60b 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71  */.  pEList = sq
2a60c 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2a60d 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 0a  end(pParse, 0, .
2a60e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a60f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a610 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64   sqlite3CreateId
2a611 45 78 70 72 28 70 50 61 72 73 65 2c 20 22 5f 72  Expr(pParse, "_r
2a612 6f 77 69 64 5f 22 29 2c 20 30 29 3b 0a 20 20 69  owid_"), 0);.  i
2a613 66 28 20 70 52 6f 77 69 64 20 29 7b 0a 20 20 20  f( pRowid ){.   
2a614 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65   pEList = sqlite
2a615 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2a616 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 0a  pParse, pEList,.
2a617 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a619 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
2a61a 70 28 64 62 2c 20 70 52 6f 77 69 64 2c 20 30 29  p(db, pRowid, 0)
2a61b 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  , 0);.  }.  asse
2a61c 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c  rt( pTab->iPKey<
2a61d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  0 );.  for(i=0; 
2a61e 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b  i<pTab->nCol; i+
2a61f 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 58 52 65  +){.    if( aXRe
2a620 66 5b 69 5d 3e 3d 30 20 29 7b 0a 20 20 20 20 20  f[i]>=0 ){.     
2a621 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33   pExpr = sqlite3
2a622 45 78 70 72 44 75 70 28 64 62 2c 20 70 43 68 61  ExprDup(db, pCha
2a623 6e 67 65 73 2d 3e 61 5b 61 58 52 65 66 5b 69 5d  nges->a[aXRef[i]
2a624 5d 2e 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20  ].pExpr, 0);.   
2a625 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45   }else{.      pE
2a626 78 70 72 20 3d 20 73 71 6c 69 74 65 33 43 72 65  xpr = sqlite3Cre
2a627 61 74 65 49 64 45 78 70 72 28 70 50 61 72 73 65  ateIdExpr(pParse
2a628 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e  , pTab->aCol[i].
2a629 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zName);.    }.  
2a62a 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74    pEList = sqlit
2a62b 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2a62c 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c  (pParse, pEList,
2a62d 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d 0a   pExpr, 0);.  }.
2a62e 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69    pSelect = sqli
2a62f 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
2a630 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 53 72  rse, pEList, pSr
2a631 63 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30 2c  c, pWhere, 0, 0,
2a632 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20   0, 0, 0, 0);.  
2a633 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65  .  /* Create the
2a634 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
2a635 20 69 6e 74 6f 20 77 68 69 63 68 20 74 68 65 20   into which the 
2a636 75 70 64 61 74 65 20 72 65 73 75 6c 74 73 20 77  update results w
2a637 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 73 74 6f 72  ill.  ** be stor
2a638 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ed..  */.  asser
2a639 74 28 20 76 20 29 3b 0a 20 20 65 70 68 65 6d 54  t( v );.  ephemT
2a63a 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ab = pParse->nTa
2a63b 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  b++;.  sqlite3Vd
2a63c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f  beAddOp2(v, OP_O
2a63d 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 65 70  penEphemeral, ep
2a63e 68 65 6d 54 61 62 2c 20 70 54 61 62 2d 3e 6e 43  hemTab, pTab->nC
2a63f 6f 6c 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29  ol+1+(pRowid!=0)
2a640 29 3b 0a 0a 20 20 2f 2a 20 66 69 6c 6c 20 74 68  );..  /* fill th
2a641 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
2a642 65 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  e .  */.  sqlite
2a643 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
2a644 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65  &dest, SRT_Table
2a645 2c 20 65 70 68 65 6d 54 61 62 29 3b 0a 20 20 73  , ephemTab);.  s
2a646 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2a647 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64  rse, pSelect, &d
2a648 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65  est);..  /* Gene
2a649 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 63 61  rate code to sca
2a64a 6e 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  n the ephemeral 
2a64b 74 61 62 6c 65 20 61 6e 64 20 63 61 6c 6c 20 56  table and call V
2a64c 55 70 64 61 74 65 2e 20 2a 2f 0a 20 20 69 52 65  Update. */.  iRe
2a64d 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  g = ++pParse->nM
2a64e 65 6d 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d  em;.  pParse->nM
2a64f 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c  em += pTab->nCol
2a650 2b 31 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  +1;.  sqlite3Vdb
2a651 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
2a652 77 69 6e 64 2c 20 65 70 68 65 6d 54 61 62 2c 20  wind, ephemTab, 
2a653 30 29 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 6c  0);.  addr = sql
2a654 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2a655 64 64 72 28 76 29 3b 0a 20 20 73 71 6c 69 74 65  ddr(v);.  sqlite
2a656 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2a657 50 5f 43 6f 6c 75 6d 6e 2c 20 20 65 70 68 65 6d  P_Column,  ephem
2a658 54 61 62 2c 20 30 2c 20 69 52 65 67 29 3b 0a 20  Tab, 0, iReg);. 
2a659 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a65a 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
2a65b 20 65 70 68 65 6d 54 61 62 2c 20 28 70 52 6f 77   ephemTab, (pRow
2a65c 69 64 3f 31 3a 30 29 2c 20 69 52 65 67 2b 31 29  id?1:0), iReg+1)
2a65d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2a65e 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
2a65f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a660 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
2a661 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20 69  umn, ephemTab, i
2a662 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29 2c 20  +1+(pRowid!=0), 
2a663 69 52 65 67 2b 32 2b 69 29 3b 0a 20 20 7d 0a 20  iReg+2+i);.  }. 
2a664 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65   sqlite3VtabMake
2a665 57 72 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c  Writable(pParse,
2a666 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65   pTab);.  sqlite
2a667 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2a668 50 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 70 54  P_VUpdate, 0, pT
2a669 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 69 52 65 67  ab->nCol+2, iReg
2a66a 2c 20 70 56 74 61 62 2c 20 50 34 5f 56 54 41 42  , pVtab, P4_VTAB
2a66b 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2a66c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
2a66d 74 2c 20 65 70 68 65 6d 54 61 62 2c 20 61 64 64  t, ephemTab, add
2a66e 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  r);.  sqlite3Vdb
2a66f 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2a670 72 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  r-1);.  sqlite3V
2a671 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a672 43 6c 6f 73 65 2c 20 65 70 68 65 6d 54 61 62 2c  Close, ephemTab,
2a673 20 30 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e   0);..  /* Clean
2a674 75 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53  up */.  sqlite3S
2a675 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
2a676 70 53 65 6c 65 63 74 29 3b 20 20 0a 7d 0a 23 65  pSelect);  .}.#e
2a677 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2a678 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2a679 20 2a 2f 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72   */../* Make sur
2a67a 65 20 22 69 73 56 69 65 77 22 20 67 65 74 73 20  e "isView" gets 
2a67b 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 63 61 73  undefined in cas
2a67c 65 20 74 68 69 73 20 66 69 6c 65 20 62 65 63 6f  e this file beco
2a67d 6d 65 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74  mes part of.** t
2a67e 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  he amalgamation 
2a67f 2d 20 73 6f 20 74 68 61 74 20 73 75 62 73 65 71  - so that subseq
2a680 75 65 6e 74 20 66 69 6c 65 73 20 64 6f 20 6e 6f  uent files do no
2a681 74 20 73 65 65 20 69 73 56 69 65 77 20 61 73 20  t see isView as 
2a682 61 0a 2a 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a 23  a.** macro. */.#
2a683 75 6e 64 65 66 20 69 73 56 69 65 77 0a 0a 2f 2a  undef isView../*
2a684 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2a685 64 20 6f 66 20 75 70 64 61 74 65 2e 63 20 2a 2a  d of update.c **
2a686 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2a689 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2a68a 67 69 6e 20 66 69 6c 65 20 76 61 63 75 75 6d 2e  gin file vacuum.
2a68b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2a68c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a68d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2a68e 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36  .** 2003 April 6
2a68f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
2a690 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
2a691 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
2a692 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
2a693 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
2a694 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
2a695 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
2a696 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
2a697 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
2a698 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
2a699 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
2a69a 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2a69b 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
2a69c 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
2a69d 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
2a69e 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
2a69f 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
2a6a0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
2a6a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a6a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 0a  ***************.
2a6a5 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
2a6a6 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
2a6a7 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
2a6a8 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 2e   VACUUM command.
2a6a9 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74  .**.** Most of t
2a6aa 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
2a6ab 66 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69 74  file may be omit
2a6ac 74 65 64 20 62 79 20 64 65 66 69 6e 69 6e 67 20  ted by defining 
2a6ad 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d  the.** SQLITE_OM
2a6ae 49 54 5f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e  IT_VACUUM macro.
2a6af 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 61 63 75  .**.** $Id: vacu
2a6b0 75 6d 2e 63 2c 76 20 31 2e 38 38 20 32 30 30 39  um.c,v 1.88 2009
2a6b1 2f 30 35 2f 30 35 20 31 37 3a 33 37 3a 32 33 20  /05/05 17:37:23 
2a6b2 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69  drh Exp $.*/..#i
2a6b3 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2a6b4 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 26  E_OMIT_VACUUM) &
2a6b5 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
2a6b6 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a 2f  E_OMIT_ATTACH)./
2a6b7 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 7a 53 71  *.** Execute zSq
2a6b8 6c 20 6f 6e 20 64 61 74 61 62 61 73 65 20 64 62  l on database db
2a6b9 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  . Return an erro
2a6ba 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  r code..*/.stati
2a6bb 63 20 69 6e 74 20 65 78 65 63 53 71 6c 28 73 71  c int execSql(sq
2a6bc 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
2a6bd 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20   char *zSql){.  
2a6be 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
2a6bf 74 6d 74 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28  tmt;.  VVA_ONLY(
2a6c0 20 69 6e 74 20 72 63 3b 20 29 0a 20 20 69 66 28   int rc; ).  if(
2a6c1 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 65   !zSql ){.    re
2a6c2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2a6c3 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c  M;.  }.  if( SQL
2a6c4 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 5f  ITE_OK!=sqlite3_
2a6c5 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c  prepare(db, zSql
2a6c6 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29  , -1, &pStmt, 0)
2a6c7 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
2a6c8 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64  qlite3_errcode(d
2a6c9 62 29 3b 0a 20 20 7d 0a 20 20 56 56 41 5f 4f 4e  b);.  }.  VVA_ON
2a6ca 4c 59 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74  LY( rc = ) sqlit
2a6cb 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a  e3_step(pStmt);.
2a6cc 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
2a6cd 4c 49 54 45 5f 52 4f 57 20 29 3b 0a 20 20 72 65  LITE_ROW );.  re
2a6ce 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e  turn sqlite3_fin
2a6cf 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a  alize(pStmt);.}.
2a6d0 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 7a  ./*.** Execute z
2a6d1 53 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65 20  Sql on database 
2a6d2 64 62 2e 20 54 68 65 20 73 74 61 74 65 6d 65 6e  db. The statemen
2a6d3 74 20 72 65 74 75 72 6e 73 20 65 78 61 63 74 6c  t returns exactl
2a6d4 79 0a 2a 2a 20 6f 6e 65 20 63 6f 6c 75 6d 6e 2e  y.** one column.
2a6d5 20 45 78 65 63 75 74 65 20 74 68 69 73 20 61 73   Execute this as
2a6d6 20 53 51 4c 20 6f 6e 20 74 68 65 20 73 61 6d 65   SQL on the same
2a6d7 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74   database..*/.st
2a6d8 61 74 69 63 20 69 6e 74 20 65 78 65 63 45 78 65  atic int execExe
2a6d9 63 53 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  cSql(sqlite3 *db
2a6da 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
2a6db 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  ql){.  sqlite3_s
2a6dc 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e  tmt *pStmt;.  in
2a6dd 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  t rc;..  rc = sq
2a6de 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 64 62  lite3_prepare(db
2a6df 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
2a6e0 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
2a6e1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2a6e2 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68 69 6c  turn rc;..  whil
2a6e3 65 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73  e( SQLITE_ROW==s
2a6e4 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d  qlite3_step(pStm
2a6e5 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 65  t) ){.    rc = e
2a6e6 78 65 63 53 71 6c 28 64 62 2c 20 28 63 68 61 72  xecSql(db, (char
2a6e7 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  *)sqlite3_column
2a6e8 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 30 29 29  _text(pStmt, 0))
2a6e9 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2a6ea 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2a6eb 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
2a6ec 65 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20  e(pStmt);.      
2a6ed 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
2a6ee 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73  .  }..  return s
2a6ef 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
2a6f0 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pStmt);.}../*.**
2a6f1 20 54 68 65 20 6e 6f 6e 2d 73 74 61 6e 64 61 72   The non-standar
2a6f2 64 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64  d VACUUM command
2a6f3 20 69 73 20 75 73 65 64 20 74 6f 20 63 6c 65 61   is used to clea
2a6f4 6e 20 75 70 20 74 68 65 20 64 61 74 61 62 61 73  n up the databas
2a6f5 65 2c 0a 2a 2a 20 63 6f 6c 6c 61 70 73 65 20 66  e,.** collapse f
2a6f6 72 65 65 20 73 70 61 63 65 2c 20 65 74 63 2e 20  ree space, etc. 
2a6f7 20 49 74 20 69 73 20 6d 6f 64 65 6c 6c 65 64 20   It is modelled 
2a6f8 61 66 74 65 72 20 74 68 65 20 56 41 43 55 55 4d  after the VACUUM
2a6f9 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 69 6e 20 50   command.** in P
2a6fa 6f 73 74 67 72 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a  ostgreSQL..**.**
2a6fb 20 49 6e 20 76 65 72 73 69 6f 6e 20 31 2e 30 2e   In version 1.0.
2a6fc 78 20 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65  x of SQLite, the
2a6fd 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20   VACUUM command 
2a6fe 77 6f 75 6c 64 20 63 61 6c 6c 0a 2a 2a 20 67 64  would call.** gd
2a6ff 62 6d 5f 72 65 6f 72 67 61 6e 69 7a 65 28 29 20  bm_reorganize() 
2a700 6f 6e 20 61 6c 6c 20 74 68 65 20 64 61 74 61 62  on all the datab
2a701 61 73 65 20 74 61 62 6c 65 73 2e 20 20 42 75 74  ase tables.  But
2a702 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69   beginning.** wi
2a703 74 68 20 32 2e 30 2e 30 2c 20 53 51 4c 69 74 65  th 2.0.0, SQLite
2a704 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20   no longer uses 
2a705 47 44 42 4d 20 73 6f 20 74 68 69 73 20 63 6f 6d  GDBM so this com
2a706 6d 61 6e 64 20 68 61 73 0a 2a 2a 20 62 65 63 6f  mand has.** beco
2a707 6d 65 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  me a no-op..*/.S
2a708 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2a709 69 64 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d  id sqlite3Vacuum
2a70a 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
2a70b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
2a70c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2a70d 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
2a70e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a70f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 56 61 63 75  ddOp2(v, OP_Vacu
2a710 75 6d 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20  um, 0, 0);.  }. 
2a711 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
2a712 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2a713 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 4f 50  mplements the OP
2a714 5f 56 61 63 75 75 6d 20 6f 70 63 6f 64 65 20 6f  _Vacuum opcode o
2a715 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 53  f the VDBE..*/.S
2a716 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2a717 74 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75  t sqlite3RunVacu
2a718 75 6d 28 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  um(char **pzErrM
2a719 73 67 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 29  sg, sqlite3 *db)
2a71a 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
2a71b 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 52  ITE_OK;     /* R
2a71c 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
2a71d 73 65 72 76 69 63 65 20 72 6f 75 74 69 6e 65 73  service routines
2a71e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4d 61   */.  Btree *pMa
2a71f 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  in;           /*
2a720 20 54 68 65 20 64 61 74 61 62 61 73 65 20 62 65   The database be
2a721 69 6e 67 20 76 61 63 75 75 6d 65 64 20 2a 2f 0a  ing vacuumed */.
2a722 20 20 50 61 67 65 72 20 2a 70 4d 61 69 6e 50 61    Pager *pMainPa
2a723 67 65 72 3b 20 20 20 20 20 20 2f 2a 20 50 61 67  ger;      /* Pag
2a724 65 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  er for database 
2a725 62 65 69 6e 67 20 76 61 63 75 75 6d 65 64 20 2a  being vacuumed *
2a726 2f 0a 20 20 42 74 72 65 65 20 2a 70 54 65 6d 70  /.  Btree *pTemp
2a727 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
2a728 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  he temporary dat
2a729 61 62 61 73 65 20 77 65 20 76 61 63 75 75 6d 20  abase we vacuum 
2a72a 69 6e 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 2a  into */.  char *
2a72b 7a 53 71 6c 20 3d 20 30 3b 20 20 20 20 20 20 20  zSql = 0;       
2a72c 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    /* SQL stateme
2a72d 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  nts */.  int sav
2a72e 65 64 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20  ed_flags;       
2a72f 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20   /* Saved value 
2a730 6f 66 20 74 68 65 20 64 62 2d 3e 66 6c 61 67 73  of the db->flags
2a731 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f   */.  int saved_
2a732 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a  nChange;      /*
2a733 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20   Saved value of 
2a734 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 20  db->nChange */. 
2a735 20 69 6e 74 20 73 61 76 65 64 5f 6e 54 6f 74 61   int saved_nTota
2a736 6c 43 68 61 6e 67 65 3b 20 2f 2a 20 53 61 76 65  lChange; /* Save
2a737 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6e  d value of db->n
2a738 54 6f 74 61 6c 43 68 61 6e 67 65 20 2a 2f 0a 20  TotalChange */. 
2a739 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20 20   Db *pDb = 0;   
2a73a 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2a73b 62 61 73 65 20 74 6f 20 64 65 74 61 63 68 20 61  base to detach a
2a73c 74 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d 20  t end of vacuum 
2a73d 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 65 6d 44 62  */.  int isMemDb
2a73e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2a73f 54 72 75 65 20 69 73 20 76 61 63 75 75 6d 69 6e  True is vacuumin
2a740 67 20 61 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74  g a :memory: dat
2a741 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  abase */.  int n
2a742 52 65 73 3b 0a 0a 20 20 69 66 28 20 21 64 62 2d  Res;..  if( !db-
2a743 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
2a744 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
2a745 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62  ing(pzErrMsg, db
2a746 2c 20 22 63 61 6e 6e 6f 74 20 56 41 43 55 55 4d  , "cannot VACUUM
2a747 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74   from within a t
2a748 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
2a749 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2a74a 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ERROR;.  }..  /*
2a74b 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e   Save the curren
2a74c 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 77  t value of the w
2a74d 72 69 74 65 2d 73 63 68 65 6d 61 20 66 6c 61 67  rite-schema flag
2a74e 20 62 65 66 6f 72 65 20 73 65 74 74 69 6e 67 20   before setting 
2a74f 69 74 2e 20 2a 2f 0a 20 20 73 61 76 65 64 5f 66  it. */.  saved_f
2a750 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73  lags = db->flags
2a751 3b 0a 20 20 73 61 76 65 64 5f 6e 43 68 61 6e 67  ;.  saved_nChang
2a752 65 20 3d 20 64 62 2d 3e 6e 43 68 61 6e 67 65 3b  e = db->nChange;
2a753 0a 20 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43  .  saved_nTotalC
2a754 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 54 6f 74  hange = db->nTot
2a755 61 6c 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e  alChange;.  db->
2a756 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
2a757 57 72 69 74 65 53 63 68 65 6d 61 20 7c 20 53 51  WriteSchema | SQ
2a758 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b  LITE_IgnoreCheck
2a759 73 3b 0a 0a 20 20 70 4d 61 69 6e 20 3d 20 64 62  s;..  pMain = db
2a75a 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3b 0a 20 20  ->aDb[0].pBt;.  
2a75b 70 4d 61 69 6e 50 61 67 65 72 20 3d 20 73 71 6c  pMainPager = sql
2a75c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
2a75d 4d 61 69 6e 29 3b 0a 20 20 69 73 4d 65 6d 44 62  Main);.  isMemDb
2a75e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46   = sqlite3PagerF
2a75f 69 6c 65 28 70 4d 61 69 6e 50 61 67 65 72 29 2d  ile(pMainPager)-
2a760 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 3b 0a 0a 20  >pMethods==0;.. 
2a761 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20 74   /* Attach the t
2a762 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
2a763 65 20 61 73 20 27 76 61 63 75 75 6d 5f 64 62 27  e as 'vacuum_db'
2a764 2e 20 54 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75  . The synchronou
2a765 73 20 70 72 61 67 6d 61 0a 20 20 2a 2a 20 63 61  s pragma.  ** ca
2a766 6e 20 62 65 20 73 65 74 20 74 6f 20 27 6f 66 66  n be set to 'off
2a767 27 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 2c  ' for this file,
2a768 20 61 73 20 69 74 20 69 73 20 6e 6f 74 20 72 65   as it is not re
2a769 63 6f 76 65 72 65 64 20 69 66 20 61 20 63 72 61  covered if a cra
2a76a 73 68 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20 61  sh.  ** occurs a
2a76b 6e 79 77 61 79 2e 20 54 68 65 20 69 6e 74 65 67  nyway. The integ
2a76c 72 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61  rity of the data
2a76d 62 61 73 65 20 69 73 20 6d 61 69 6e 74 61 69 6e  base is maintain
2a76e 65 64 20 62 79 20 61 0a 20 20 2a 2a 20 28 70 6f  ed by a.  ** (po
2a76f 73 73 69 62 6c 79 20 73 79 6e 63 68 72 6f 6e 6f  ssibly synchrono
2a770 75 73 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  us) transaction 
2a771 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 6d 61  opened on the ma
2a772 69 6e 20 64 61 74 61 62 61 73 65 20 62 65 66 6f  in database befo
2a773 72 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42  re.  ** sqlite3B
2a774 74 72 65 65 43 6f 70 79 46 69 6c 65 28 29 20 69  treeCopyFile() i
2a775 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2a 0a 20  s called..  **. 
2a776 20 2a 2a 20 41 6e 20 6f 70 74 69 6d 69 73 61 74   ** An optimisat
2a777 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 74 6f 20  ion would be to 
2a778 75 73 65 20 61 20 6e 6f 6e 2d 6a 6f 75 72 6e 61  use a non-journa
2a779 6c 65 64 20 70 61 67 65 72 2e 0a 20 20 2a 2a 20  led pager..  ** 
2a77a 28 4c 61 74 65 72 3a 29 20 49 20 74 72 69 65 64  (Later:) I tried
2a77b 20 73 65 74 74 69 6e 67 20 22 50 52 41 47 4d 41   setting "PRAGMA
2a77c 20 76 61 63 75 75 6d 5f 64 62 2e 6a 6f 75 72 6e   vacuum_db.journ
2a77d 61 6c 5f 6d 6f 64 65 3d 4f 46 46 22 20 62 75 74  al_mode=OFF" but
2a77e 0a 20 20 2a 2a 20 74 68 61 74 20 61 63 74 75 61  .  ** that actua
2a77f 6c 6c 79 20 6d 61 64 65 20 74 68 65 20 56 41 43  lly made the VAC
2a780 55 55 4d 20 72 75 6e 20 73 6c 6f 77 65 72 2e 20  UUM run slower. 
2a781 20 56 65 72 79 20 6c 69 74 74 6c 65 20 6a 6f 75   Very little jou
2a782 72 6e 61 6c 6c 69 6e 67 0a 20 20 2a 2a 20 61 63  rnalling.  ** ac
2a783 74 75 61 6c 6c 79 20 6f 63 63 75 72 73 20 77 68  tually occurs wh
2a784 65 6e 20 64 6f 69 6e 67 20 61 20 76 61 63 75 75  en doing a vacuu
2a785 6d 20 73 69 6e 63 65 20 74 68 65 20 76 61 63 75  m since the vacu
2a786 75 6d 5f 64 62 20 69 73 20 69 6e 69 74 69 61 6c  um_db is initial
2a787 6c 79 0a 20 20 2a 2a 20 65 6d 70 74 79 2e 20 20  ly.  ** empty.  
2a788 4f 6e 6c 79 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Only the journal
2a789 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74   header is writt
2a78a 65 6e 2e 20 20 41 70 70 61 72 65 6e 74 6c 79 20  en.  Apparently 
2a78b 69 74 20 74 61 6b 65 73 20 6d 6f 72 65 0a 20 20  it takes more.  
2a78c 2a 2a 20 74 69 6d 65 20 74 6f 20 70 61 72 73 65  ** time to parse
2a78d 20 61 6e 64 20 72 75 6e 20 74 68 65 20 50 52 41   and run the PRA
2a78e 47 4d 41 20 74 6f 20 74 75 72 6e 20 6a 6f 75 72  GMA to turn jour
2a78f 6e 61 6c 6c 69 6e 67 20 6f 66 66 20 74 68 61 6e  nalling off than
2a790 20 69 74 20 64 6f 65 73 0a 20 20 2a 2a 20 74 6f   it does.  ** to
2a791 20 77 72 69 74 65 20 74 68 65 20 6a 6f 75 72 6e   write the journ
2a792 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a  al header file..
2a793 20 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 22 41    */.  zSql = "A
2a794 54 54 41 43 48 20 27 27 20 41 53 20 76 61 63 75  TTACH '' AS vacu
2a795 75 6d 5f 64 62 3b 22 3b 0a 20 20 72 63 20 3d 20  um_db;";.  rc = 
2a796 65 78 65 63 53 71 6c 28 64 62 2c 20 7a 53 71 6c  execSql(db, zSql
2a797 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2a798 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
2a799 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 70  d_of_vacuum;.  p
2a79a 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64 62  Db = &db->aDb[db
2a79b 2d 3e 6e 44 62 2d 31 5d 3b 0a 20 20 61 73 73 65  ->nDb-1];.  asse
2a79c 72 74 28 20 73 74 72 63 6d 70 28 64 62 2d 3e 61  rt( strcmp(db->a
2a79d 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 2e 7a 4e  Db[db->nDb-1].zN
2a79e 61 6d 65 2c 22 76 61 63 75 75 6d 5f 64 62 22 29  ame,"vacuum_db")
2a79f 3d 3d 30 20 29 3b 0a 20 20 70 54 65 6d 70 20 3d  ==0 );.  pTemp =
2a7a0 20 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62   db->aDb[db->nDb
2a7a1 2d 31 5d 2e 70 42 74 3b 0a 0a 20 20 6e 52 65 73  -1].pBt;..  nRes
2a7a2 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
2a7a3 65 74 52 65 73 65 72 76 65 28 70 4d 61 69 6e 29  etReserve(pMain)
2a7a4 3b 0a 0a 20 20 2f 2a 20 41 20 56 41 43 55 55 4d  ;..  /* A VACUUM
2a7a5 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74   cannot change t
2a7a6 68 65 20 70 61 67 65 73 69 7a 65 20 6f 66 20 61  he pagesize of a
2a7a7 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61 74 61  n encrypted data
2a7a8 62 61 73 65 2e 20 2a 2f 0a 23 69 66 64 65 66 20  base. */.#ifdef 
2a7a9 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
2a7aa 0a 20 20 69 66 28 20 64 62 2d 3e 6e 65 78 74 50  .  if( db->nextP
2a7ab 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 65  agesize ){.    e
2a7ac 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74  xtern void sqlit
2a7ad 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 73 71  e3CodecGetKey(sq
2a7ae 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69  lite3*, int, voi
2a7af 64 2a 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20  d**, int*);.    
2a7b0 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68  int nKey;.    ch
2a7b1 61 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 73 71  ar *zKey;.    sq
2a7b2 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79  lite3CodecGetKey
2a7b3 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29  (db, 0, (void**)
2a7b4 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20  &zKey, &nKey);. 
2a7b5 20 20 20 69 66 28 20 6e 4b 65 79 20 29 20 64 62     if( nKey ) db
2a7b6 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d  ->nextPagesize =
2a7b7 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a   0;.  }.#endif..
2a7b8 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72    if( sqlite3Btr
2a7b9 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 54  eeSetPageSize(pT
2a7ba 65 6d 70 2c 20 73 71 6c 69 74 65 33 42 74 72 65  emp, sqlite3Btre
2a7bb 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 4d 61  eGetPageSize(pMa
2a7bc 69 6e 29 2c 20 6e 52 65 73 2c 20 30 29 0a 20 20  in), nRes, 0).  
2a7bd 20 7c 7c 20 28 21 69 73 4d 65 6d 44 62 20 26 26   || (!isMemDb &&
2a7be 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
2a7bf 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 2c 20  PageSize(pTemp, 
2a7c0 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65  db->nextPagesize
2a7c1 2c 20 6e 52 65 73 2c 20 30 29 29 0a 20 20 20 7c  , nRes, 0)).   |
2a7c2 7c 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61 6c 6c  | NEVER(db->mall
2a7c3 6f 63 46 61 69 6c 65 64 29 0a 20 20 29 7b 0a 20  ocFailed).  ){. 
2a7c4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
2a7c5 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65  OMEM;.    goto e
2a7c6 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2a7c7 7d 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c  }.  rc = execSql
2a7c8 28 64 62 2c 20 22 50 52 41 47 4d 41 20 76 61 63  (db, "PRAGMA vac
2a7c9 75 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e 6f  uum_db.synchrono
2a7ca 75 73 3d 4f 46 46 22 29 3b 0a 20 20 69 66 28 20  us=OFF");.  if( 
2a7cb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2a7cc 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  .    goto end_of
2a7cd 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23 69  _vacuum;.  }..#i
2a7ce 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2a7cf 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 73  T_AUTOVACUUM.  s
2a7d0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
2a7d1 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 2c 20  toVacuum(pTemp, 
2a7d2 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 3e  db->nextAutovac>
2a7d3 3d 30 20 3f 20 64 62 2d 3e 6e 65 78 74 41 75 74  =0 ? db->nextAut
2a7d4 6f 76 61 63 20 3a 0a 20 20 20 20 20 20 20 20 20  ovac :.         
2a7d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a7d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a7d7 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
2a7d8 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69  tAutoVacuum(pMai
2a7d9 6e 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  n));.#endif..  /
2a7da 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61  * Begin a transa
2a7db 63 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d 20  ction */.  rc = 
2a7dc 65 78 65 63 53 71 6c 28 64 62 2c 20 22 42 45 47  execSql(db, "BEG
2a7dd 49 4e 20 45 58 43 4c 55 53 49 56 45 3b 22 29 3b  IN EXCLUSIVE;");
2a7de 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2a7df 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
2a7e0 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a  of_vacuum;..  /*
2a7e1 20 51 75 65 72 79 20 74 68 65 20 73 63 68 65 6d   Query the schem
2a7e2 61 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61  a of the main da
2a7e3 74 61 62 61 73 65 2e 20 43 72 65 61 74 65 20 61  tabase. Create a
2a7e4 20 6d 69 72 72 6f 72 20 73 63 68 65 6d 61 0a 20   mirror schema. 
2a7e5 20 2a 2a 20 69 6e 20 74 68 65 20 74 65 6d 70 6f   ** in the tempo
2a7e6 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20  rary database.. 
2a7e7 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45   */.  rc = execE
2a7e8 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20  xecSql(db, .    
2a7e9 20 20 22 53 45 4c 45 43 54 20 27 43 52 45 41 54    "SELECT 'CREAT
2a7ea 45 20 54 41 42 4c 45 20 76 61 63 75 75 6d 5f 64  E TABLE vacuum_d
2a7eb 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71  b.' || substr(sq
2a7ec 6c 2c 31 34 29 20 22 0a 20 20 20 20 20 20 22 20  l,14) ".      " 
2a7ed 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73   FROM sqlite_mas
2a7ee 74 65 72 20 57 48 45 52 45 20 74 79 70 65 3d 27  ter WHERE type='
2a7ef 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65 21  table' AND name!
2a7f0 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  ='sqlite_sequenc
2a7f1 65 27 22 0a 20 20 20 20 20 20 22 20 20 20 41 4e  e'".      "   AN
2a7f2 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 20 20  D rootpage>0".  
2a7f3 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2a7f4 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
2a7f5 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72  d_of_vacuum;.  r
2a7f6 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28  c = execExecSql(
2a7f7 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45  db, .      "SELE
2a7f8 43 54 20 27 43 52 45 41 54 45 20 49 4e 44 45 58  CT 'CREATE INDEX
2a7f9 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
2a7fa 73 75 62 73 74 72 28 73 71 6c 2c 31 34 29 22 0a  substr(sql,14)".
2a7fb 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71        "  FROM sq
2a7fc 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52  lite_master WHER
2a7fd 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45 41  E sql LIKE 'CREA
2a7fe 54 45 20 49 4e 44 45 58 20 25 27 20 22 29 3b 0a  TE INDEX %' ");.
2a7ff 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2a800 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2a801 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d  f_vacuum;.  rc =
2a802 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c   execExecSql(db,
2a803 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20   .      "SELECT 
2a804 27 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49  'CREATE UNIQUE I
2a805 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e 27  NDEX vacuum_db.'
2a806 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 32   || substr(sql,2
2a807 31 29 20 22 0a 20 20 20 20 20 20 22 20 20 46 52  1) ".      "  FR
2a808 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
2a809 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45 20   WHERE sql LIKE 
2a80a 27 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49  'CREATE UNIQUE I
2a80b 4e 44 45 58 20 25 27 22 29 3b 0a 20 20 69 66 28  NDEX %'");.  if(
2a80c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2a80d 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63   goto end_of_vac
2a80e 75 75 6d 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20  uum;..  /* Loop 
2a80f 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62 6c  through the tabl
2a810 65 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  es in the main d
2a811 61 74 61 62 61 73 65 2e 20 46 6f 72 20 65 61 63  atabase. For eac
2a812 68 2c 20 64 6f 0a 20 20 2a 2a 20 61 6e 20 22 49  h, do.  ** an "I
2a813 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75  NSERT INTO vacuu
2a814 6d 5f 64 62 2e 78 78 78 20 53 45 4c 45 43 54 20  m_db.xxx SELECT 
2a815 2a 20 46 52 4f 4d 20 78 78 78 3b 22 20 74 6f 20  * FROM xxx;" to 
2a816 63 6f 70 79 0a 20 20 2a 2a 20 74 68 65 20 63 6f  copy.  ** the co
2a817 6e 74 65 6e 74 73 20 74 6f 20 74 68 65 20 74 65  ntents to the te
2a818 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
2a819 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78  ..  */.  rc = ex
2a81a 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20  ecExecSql(db, . 
2a81b 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 49 4e       "SELECT 'IN
2a81c 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d  SERT INTO vacuum
2a81d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e  _db.' || quote(n
2a81e 61 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c 7c  ame) ".      "||
2a81f 20 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   ' SELECT * FROM
2a820 20 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65   ' || quote(name
2a821 29 20 7c 7c 20 27 3b 27 22 0a 20 20 20 20 20 20  ) || ';'".      
2a822 22 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73  "FROM sqlite_mas
2a823 74 65 72 20 22 0a 20 20 20 20 20 20 22 57 48 45  ter ".      "WHE
2a824 52 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65  RE type = 'table
2a825 27 20 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c  ' AND name!='sql
2a826 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22 0a  ite_sequence' ".
2a827 20 20 20 20 20 20 22 20 20 41 4e 44 20 72 6f 6f        "  AND roo
2a828 74 70 61 67 65 3e 30 22 0a 0a 20 20 29 3b 0a 20  tpage>0"..  );. 
2a829 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2a82a 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
2a82b 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 43  _vacuum;..  /* C
2a82c 6f 70 79 20 6f 76 65 72 20 74 68 65 20 73 65 71  opy over the seq
2a82d 75 65 6e 63 65 20 74 61 62 6c 65 0a 20 20 2a 2f  uence table.  */
2a82e 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65 63  .  rc = execExec
2a82f 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22  Sql(db, .      "
2a830 53 45 4c 45 43 54 20 27 44 45 4c 45 54 45 20 46  SELECT 'DELETE F
2a831 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 27 20  ROM vacuum_db.' 
2a832 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c  || quote(name) |
2a833 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20 22 46  | ';' ".      "F
2a834 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73 71  ROM vacuum_db.sq
2a835 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52  lite_master WHER
2a836 45 20 6e 61 6d 65 3d 27 73 71 6c 69 74 65 5f 73  E name='sqlite_s
2a837 65 71 75 65 6e 63 65 27 20 22 0a 20 20 29 3b 0a  equence' ".  );.
2a838 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2a839 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2a83a 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d  f_vacuum;.  rc =
2a83b 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c   execExecSql(db,
2a83c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20   .      "SELECT 
2a83d 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63  'INSERT INTO vac
2a83e 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74  uum_db.' || quot
2a83f 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20 20 20  e(name) ".      
2a840 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a 20 46  "|| ' SELECT * F
2a841 52 4f 4d 20 27 20 7c 7c 20 71 75 6f 74 65 28 6e  ROM ' || quote(n
2a842 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a 20 20  ame) || ';' ".  
2a843 20 20 20 20 22 46 52 4f 4d 20 76 61 63 75 75 6d      "FROM vacuum
2a844 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65  _db.sqlite_maste
2a845 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 3d 27 73  r WHERE name=='s
2a846 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 3b  qlite_sequence';
2a847 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21  ".  );.  if( rc!
2a848 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
2a849 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b  o end_of_vacuum;
2a84a 0a 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65  ...  /* Copy the
2a84b 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77 73   triggers, views
2a84c 2c 20 61 6e 64 20 76 69 72 74 75 61 6c 20 74 61  , and virtual ta
2a84d 62 6c 65 73 20 66 72 6f 6d 20 74 68 65 20 6d 61  bles from the ma
2a84e 69 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  in database.  **
2a84f 20 6f 76 65 72 20 74 6f 20 74 68 65 20 74 65 6d   over to the tem
2a850 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2e  porary database.
2a851 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 73 65 20    None of these 
2a852 6f 62 6a 65 63 74 73 20 68 61 73 20 61 6e 79 0a  objects has any.
2a853 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20    ** associated 
2a854 73 74 6f 72 61 67 65 2c 20 73 6f 20 61 6c 6c 20  storage, so all 
2a855 77 65 20 68 61 76 65 20 74 6f 20 64 6f 20 69 73  we have to do is
2a856 20 63 6f 70 79 20 74 68 65 69 72 20 65 6e 74 72   copy their entr
2a857 69 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68  ies.  ** from th
2a858 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  e SQLITE_MASTER 
2a859 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  table..  */.  rc
2a85a 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 0a 20   = execSql(db,. 
2a85b 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54       "INSERT INT
2a85c 4f 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69  O vacuum_db.sqli
2a85d 74 65 5f 6d 61 73 74 65 72 20 22 0a 20 20 20 20  te_master ".    
2a85e 20 20 22 20 20 53 45 4c 45 43 54 20 74 79 70 65    "  SELECT type
2a85f 2c 20 6e 61 6d 65 2c 20 74 62 6c 5f 6e 61 6d 65  , name, tbl_name
2a860 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 22  , rootpage, sql"
2a861 0a 20 20 20 20 20 20 22 20 20 20 20 46 52 4f 4d  .      "    FROM
2a862 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a   sqlite_master".
2a863 20 20 20 20 20 20 22 20 20 20 57 48 45 52 45 20        "   WHERE 
2a864 74 79 70 65 3d 27 76 69 65 77 27 20 4f 52 20 74  type='view' OR t
2a865 79 70 65 3d 27 74 72 69 67 67 65 72 27 22 0a 20  ype='trigger'". 
2a866 20 20 20 20 20 22 20 20 20 20 20 20 4f 52 20 28       "      OR (
2a867 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e 44  type='table' AND
2a868 20 72 6f 6f 74 70 61 67 65 3d 30 29 22 0a 20 20   rootpage=0)".  
2a869 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f  );.  if( rc ) go
2a86a 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2a86b 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  ;..  /* At this 
2a86c 70 6f 69 6e 74 2c 20 75 6e 6c 65 73 73 20 74 68  point, unless th
2a86d 65 20 6d 61 69 6e 20 64 62 20 77 61 73 20 63 6f  e main db was co
2a86e 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2c 20  mpletely empty, 
2a86f 74 68 65 72 65 20 69 73 20 6e 6f 77 20 61 0a 20  there is now a. 
2a870 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
2a871 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75  open on the vacu
2a872 75 6d 20 64 61 74 61 62 61 73 65 2c 20 62 75 74  um database, but
2a873 20 6e 6f 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e   not on the main
2a874 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20   database..  ** 
2a875 4f 70 65 6e 20 61 20 62 74 72 65 65 20 6c 65 76  Open a btree lev
2a876 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  el transaction o
2a877 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
2a878 61 73 65 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73  ase. This allows
2a879 20 61 0a 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20   a.  ** call to 
2a87a 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79  sqlite3BtreeCopy
2a87b 46 69 6c 65 28 29 2e 20 54 68 65 20 6d 61 69 6e  File(). The main
2a87c 20 64 61 74 61 62 61 73 65 20 62 74 72 65 65 20   database btree 
2a87d 6c 65 76 65 6c 0a 20 20 2a 2a 20 74 72 61 6e 73  level.  ** trans
2a87e 61 63 74 69 6f 6e 20 69 73 20 74 68 65 6e 20 63  action is then c
2a87f 6f 6d 6d 69 74 74 65 64 2c 20 73 6f 20 74 68 65  ommitted, so the
2a880 20 53 51 4c 20 6c 65 76 65 6c 20 6e 65 76 65 72   SQL level never
2a881 20 6b 6e 6f 77 73 20 69 74 20 77 61 73 0a 20 20   knows it was.  
2a882 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  ** opened for wr
2a883 69 74 69 6e 67 2e 20 54 68 69 73 20 77 61 79 2c  iting. This way,
2a884 20 74 68 65 20 53 51 4c 20 74 72 61 6e 73 61 63   the SQL transac
2a885 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65  tion used to cre
2a886 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 74 65 6d  ate the.  ** tem
2a887 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20  porary database 
2a888 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62  never needs to b
2a889 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 2a  e committed..  *
2a88a 2f 0a 20 20 7b 0a 20 20 20 20 75 33 32 20 6d 65  /.  {.    u32 me
2a88b 74 61 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a  ta;.    int i;..
2a88c 20 20 20 20 2f 2a 20 54 68 69 73 20 61 72 72 61      /* This arra
2a88d 79 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69  y determines whi
2a88e 63 68 20 6d 65 74 61 20 6d 65 74 61 20 76 61 6c  ch meta meta val
2a88f 75 65 73 20 61 72 65 20 70 72 65 73 65 72 76 65  ues are preserve
2a890 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  d in the.    ** 
2a891 76 61 63 75 75 6d 2e 20 20 45 76 65 6e 20 65 6e  vacuum.  Even en
2a892 74 72 69 65 73 20 61 72 65 20 74 68 65 20 6d 65  tries are the me
2a893 74 61 20 76 61 6c 75 65 20 6e 75 6d 62 65 72 20  ta value number 
2a894 61 6e 64 20 6f 64 64 20 65 6e 74 72 69 65 73 0a  and odd entries.
2a895 20 20 20 20 2a 2a 20 61 72 65 20 61 6e 20 69 6e      ** are an in
2a896 63 72 65 6d 65 6e 74 20 74 6f 20 61 70 70 6c 79  crement to apply
2a897 20 74 6f 20 74 68 65 20 6d 65 74 61 20 76 61 6c   to the meta val
2a898 75 65 20 61 66 74 65 72 20 74 68 65 20 76 61 63  ue after the vac
2a899 75 75 6d 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  uum..    ** The 
2a89a 69 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 73 65  increment is use
2a89b 64 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68  d to increase th
2a89c 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
2a89d 73 6f 20 74 68 61 74 20 6f 74 68 65 72 0a 20 20  so that other.  
2a89e 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73    ** connections
2a89f 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74   to the same dat
2a8a0 61 62 61 73 65 20 77 69 6c 6c 20 6b 6e 6f 77 20  abase will know 
2a8a1 74 6f 20 72 65 72 65 61 64 20 74 68 65 20 73 63  to reread the sc
2a8a2 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  hema..    */.   
2a8a3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
2a8a4 73 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f 70  signed char aCop
2a8a5 79 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 31  y[] = {.       1
2a8a6 2c 20 31 2c 20 20 20 20 2f 2a 20 41 64 64 20 6f  , 1,    /* Add o
2a8a7 6e 65 20 74 6f 20 74 68 65 20 6f 6c 64 20 73 63  ne to the old sc
2a8a8 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20  hema cookie */. 
2a8a9 20 20 20 20 20 20 33 2c 20 30 2c 20 20 20 20 2f        3, 0,    /
2a8aa 2a 20 50 72 65 73 65 72 76 65 20 74 68 65 20 64  * Preserve the d
2a8ab 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68  efault page cach
2a8ac 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20  e size */.      
2a8ad 20 35 2c 20 30 2c 20 20 20 20 2f 2a 20 50 72 65   5, 0,    /* Pre
2a8ae 73 65 72 76 65 20 74 68 65 20 64 65 66 61 75 6c  serve the defaul
2a8af 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  t text encoding 
2a8b0 2a 2f 0a 20 20 20 20 20 20 20 36 2c 20 30 2c 20  */.       6, 0, 
2a8b1 20 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74     /* Preserve t
2a8b2 68 65 20 75 73 65 72 20 76 65 72 73 69 6f 6e 20  he user version 
2a8b3 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 61  */.    };..    a
2a8b4 73 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65  ssert( 1==sqlite
2a8b5 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
2a8b6 70 54 65 6d 70 29 20 29 3b 0a 20 20 20 20 61 73  pTemp) );.    as
2a8b7 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33  sert( 1==sqlite3
2a8b8 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70  BtreeIsInTrans(p
2a8b9 4d 61 69 6e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a  Main) );..    /*
2a8ba 20 43 6f 70 79 20 42 74 72 65 65 20 6d 65 74 61   Copy Btree meta
2a8bb 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 66   values */.    f
2a8bc 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53  or(i=0; i<ArrayS
2a8bd 69 7a 65 28 61 43 6f 70 79 29 3b 20 69 2b 3d 32  ize(aCopy); i+=2
2a8be 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 4d  ){.      /* GetM
2a8bf 65 74 61 28 29 20 61 6e 64 20 55 70 64 61 74 65  eta() and Update
2a8c0 4d 65 74 61 28 29 20 63 61 6e 6e 6f 74 20 66 61  Meta() cannot fa
2a8c1 69 6c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 65  il in this conte
2a8c2 78 74 20 62 65 63 61 75 73 65 0a 20 20 20 20 20  xt because.     
2a8c3 20 2a 2a 20 77 65 20 61 6c 72 65 61 64 79 20 68   ** we already h
2a8c4 61 76 65 20 70 61 67 65 20 31 20 6c 6f 61 64 65  ave page 1 loade
2a8c5 64 20 69 6e 74 6f 20 63 61 63 68 65 20 61 6e 64  d into cache and
2a8c6 20 6d 61 72 6b 65 64 20 64 69 72 74 79 2e 20 2a   marked dirty. *
2a8c7 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  /.      rc = sql
2a8c8 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
2a8c9 28 70 4d 61 69 6e 2c 20 61 43 6f 70 79 5b 69 5d  (pMain, aCopy[i]
2a8ca 2c 20 26 6d 65 74 61 29 3b 0a 20 20 20 20 20 20  , &meta);.      
2a8cb 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51  if( NEVER(rc!=SQ
2a8cc 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20  LITE_OK) ) goto 
2a8cd 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2a8ce 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2a8cf 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
2a8d0 28 70 54 65 6d 70 2c 20 61 43 6f 70 79 5b 69 5d  (pTemp, aCopy[i]
2a8d1 2c 20 6d 65 74 61 2b 61 43 6f 70 79 5b 69 2b 31  , meta+aCopy[i+1
2a8d2 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 4e 45  ]);.      if( NE
2a8d3 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f  VER(rc!=SQLITE_O
2a8d4 4b 29 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  K) ) goto end_of
2a8d5 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a  _vacuum;.    }..
2a8d6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2a8d7 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 70 4d  BtreeCopyFile(pM
2a8d8 61 69 6e 2c 20 70 54 65 6d 70 29 3b 0a 20 20 20  ain, pTemp);.   
2a8d9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2a8da 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
2a8db 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20 72 63 20  _vacuum;.    rc 
2a8dc 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
2a8dd 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a 20 20 20  mmit(pTemp);.   
2a8de 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2a8df 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
2a8e0 5f 76 61 63 75 75 6d 3b 0a 23 69 66 6e 64 65 66  _vacuum;.#ifndef
2a8e1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2a8e2 4f 56 41 43 55 55 4d 0a 20 20 20 20 73 71 6c 69  OVACUUM.    sqli
2a8e3 74 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56  te3BtreeSetAutoV
2a8e4 61 63 75 75 6d 28 70 4d 61 69 6e 2c 20 73 71 6c  acuum(pMain, sql
2a8e5 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f  ite3BtreeGetAuto
2a8e6 56 61 63 75 75 6d 28 70 54 65 6d 70 29 29 3b 0a  Vacuum(pTemp));.
2a8e7 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61 73  #endif.  }..  as
2a8e8 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
2a8e9 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  _OK );.  rc = sq
2a8ea 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
2a8eb 65 53 69 7a 65 28 70 4d 61 69 6e 2c 20 73 71 6c  eSize(pMain, sql
2a8ec 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
2a8ed 53 69 7a 65 28 70 54 65 6d 70 29 2c 20 6e 52 65  Size(pTemp), nRe
2a8ee 73 2c 31 29 3b 0a 0a 65 6e 64 5f 6f 66 5f 76 61  s,1);..end_of_va
2a8ef 63 75 75 6d 3a 0a 20 20 2f 2a 20 52 65 73 74 6f  cuum:.  /* Resto
2a8f0 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  re the original 
2a8f1 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 66 6c 61  value of db->fla
2a8f2 67 73 20 2a 2f 0a 20 20 64 62 2d 3e 66 6c 61 67  gs */.  db->flag
2a8f3 73 20 3d 20 73 61 76 65 64 5f 66 6c 61 67 73 3b  s = saved_flags;
2a8f4 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d  .  db->nChange =
2a8f5 20 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 3b 0a   saved_nChange;.
2a8f6 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e    db->nTotalChan
2a8f7 67 65 20 3d 20 73 61 76 65 64 5f 6e 54 6f 74 61  ge = saved_nTota
2a8f8 6c 43 68 61 6e 67 65 3b 0a 0a 20 20 2f 2a 20 43  lChange;..  /* C
2a8f9 75 72 72 65 6e 74 6c 79 20 74 68 65 72 65 20 69  urrently there i
2a8fa 73 20 61 6e 20 53 51 4c 20 6c 65 76 65 6c 20 74  s an SQL level t
2a8fb 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20  ransaction open 
2a8fc 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 0a 20 20  on the vacuum.  
2a8fd 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 4e 6f 20  ** database. No 
2a8fe 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f  locks are held o
2a8ff 6e 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65  n any other file
2a900 73 20 28 73 69 6e 63 65 20 74 68 65 20 6d 61 69  s (since the mai
2a901 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77 61 73 20  n file.  ** was 
2a902 63 6f 6d 6d 69 74 74 65 64 20 61 74 20 74 68 65  committed at the
2a903 20 62 74 72 65 65 20 6c 65 76 65 6c 29 2e 20 53   btree level). S
2a904 6f 20 69 74 20 73 61 66 65 20 74 6f 20 65 6e 64  o it safe to end
2a905 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
2a906 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75 61 6c 6c  .  ** by manuall
2a907 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75  y setting the au
2a908 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f  toCommit flag to
2a909 20 74 72 75 65 20 61 6e 64 20 64 65 74 61 63 68   true and detach
2a90a 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 76 61 63  ing the.  ** vac
2a90b 75 75 6d 20 64 61 74 61 62 61 73 65 2e 20 54 68  uum database. Th
2a90c 65 20 76 61 63 75 75 6d 5f 64 62 20 6a 6f 75 72  e vacuum_db jour
2a90d 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65 6c 65  nal file is dele
2a90e 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67  ted when the pag
2a90f 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c 6f 73 65  er.  ** is close
2a910 64 20 62 79 20 74 68 65 20 44 45 54 41 43 48 2e  d by the DETACH.
2a911 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61 75 74 6f  .  */.  db->auto
2a912 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 69  Commit = 1;..  i
2a913 66 28 20 70 44 62 20 29 7b 0a 20 20 20 20 73 71  f( pDb ){.    sq
2a914 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
2a915 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 70  pDb->pBt);.    p
2a916 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20  Db->pBt = 0;.   
2a917 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20   pDb->pSchema = 
2a918 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  0;.  }..  sqlite
2a919 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
2a91a 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20  hema(db, 0);..  
2a91b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
2a91c 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  dif  /* SQLITE_O
2a91d 4d 49 54 5f 56 41 43 55 55 4d 20 26 26 20 53 51  MIT_VACUUM && SQ
2a91e 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
2a91f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2a920 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 61 63 75  **** End of vacu
2a921 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  um.c ***********
2a922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a924 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2a925 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2a926 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vtab.c *********
2a927 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a928 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a929 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
2a92a 4a 75 6e 65 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68  June 10.**.** Th
2a92b 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2a92c 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2a92d 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2a92e 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2a92f 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2a930 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2a931 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2a932 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2a933 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2a934 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2a935 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2a936 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2a937 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2a938 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2a939 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2a93a 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2a93b 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2a93c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a93d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a93e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a93f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a940 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
2a941 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
2a942 65 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69  e used to help i
2a943 6d 70 6c 65 6d 65 6e 74 20 76 69 72 74 75 61 6c  mplement virtual
2a944 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 24   tables..**.** $
2a945 49 64 3a 20 76 74 61 62 2e 63 2c 76 20 31 2e 38  Id: vtab.c,v 1.8
2a946 36 20 32 30 30 39 2f 30 34 2f 32 38 20 31 33 3a  6 2009/04/28 13:
2a947 30 31 3a 30 39 20 64 72 68 20 45 78 70 20 24 0a  01:09 drh Exp $.
2a948 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2a949 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2a94a 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  BLE../*.** The a
2a94b 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e 20 74  ctual function t
2a94c 68 61 74 20 64 6f 65 73 20 74 68 65 20 77 6f 72  hat does the wor
2a94d 6b 20 6f 66 20 63 72 65 61 74 69 6e 67 20 61 20  k of creating a 
2a94e 6e 65 77 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 54  new module..** T
2a94f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  his function imp
2a950 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69  lements the sqli
2a951 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
2a952 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74  e() and.** sqlit
2a953 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
2a954 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 73  _v2() interfaces
2a955 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2a956 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 0a 20 20  createModule(.  
2a957 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2a958 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a959 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e 20 77  /* Database in w
2a95a 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72  hich module is r
2a95b 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20 20 63  egistered */.  c
2a95c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2a95d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2a95e 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20  * Name assigned 
2a95f 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a  to this module *
2a960 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  /.  const sqlite
2a961 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
2a962 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e  e,  /* The defin
2a963 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64  ition of the mod
2a964 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ule */.  void *p
2a965 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Aux,            
2a966 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
2a967 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f 72 20  ext pointer for 
2a968 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74  xCreate/xConnect
2a969 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65   */.  void (*xDe
2a96a 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 20 20  stroy)(void *)  
2a96b 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20        /* Module 
2a96c 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
2a96d 69 6f 6e 20 2a 2f 0a 29 20 7b 0a 20 20 69 6e 74  ion */.) {.  int
2a96e 20 72 63 2c 20 6e 4e 61 6d 65 3b 0a 20 20 4d 6f   rc, nName;.  Mo
2a96f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 0a 20 20 73  dule *pMod;..  s
2a970 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2a971 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
2a972 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2a973 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
2a974 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c  .  pMod = (Modul
2a975 65 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  e *)sqlite3DbMal
2a976 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
2a977 66 28 4d 6f 64 75 6c 65 29 20 2b 20 6e 4e 61 6d  f(Module) + nNam
2a978 65 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 70 4d  e + 1);.  if( pM
2a979 6f 64 20 29 7b 0a 20 20 20 20 4d 6f 64 75 6c 65  od ){.    Module
2a97a 20 2a 70 44 65 6c 3b 0a 20 20 20 20 63 68 61 72   *pDel;.    char
2a97b 20 2a 7a 43 6f 70 79 20 3d 20 28 63 68 61 72 20   *zCopy = (char 
2a97c 2a 29 28 26 70 4d 6f 64 5b 31 5d 29 3b 0a 20 20  *)(&pMod[1]);.  
2a97d 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79 2c 20    memcpy(zCopy, 
2a97e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b  zName, nName+1);
2a97f 0a 20 20 20 20 70 4d 6f 64 2d 3e 7a 4e 61 6d 65  .    pMod->zName
2a980 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20 70 4d   = zCopy;.    pM
2a981 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d  od->pModule = pM
2a982 6f 64 75 6c 65 3b 0a 20 20 20 20 70 4d 6f 64 2d  odule;.    pMod-
2a983 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 20  >pAux = pAux;.  
2a984 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79    pMod->xDestroy
2a985 20 3d 20 78 44 65 73 74 72 6f 79 3b 0a 20 20 20   = xDestroy;.   
2a986 20 70 44 65 6c 20 3d 20 28 4d 6f 64 75 6c 65 20   pDel = (Module 
2a987 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  *)sqlite3HashIns
2a988 65 72 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  ert(&db->aModule
2a989 2c 20 7a 43 6f 70 79 2c 20 6e 4e 61 6d 65 2c 20  , zCopy, nName, 
2a98a 28 76 6f 69 64 2a 29 70 4d 6f 64 29 3b 0a 20 20  (void*)pMod);.  
2a98b 20 20 69 66 28 20 70 44 65 6c 20 26 26 20 70 44    if( pDel && pD
2a98c 65 6c 2d 3e 78 44 65 73 74 72 6f 79 20 29 7b 0a  el->xDestroy ){.
2a98d 20 20 20 20 20 20 70 44 65 6c 2d 3e 78 44 65 73        pDel->xDes
2a98e 74 72 6f 79 28 70 44 65 6c 2d 3e 70 41 75 78 29  troy(pDel->pAux)
2a98f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2a990 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44  te3DbFree(db, pD
2a991 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 70 44 65  el);.    if( pDe
2a992 6c 3d 3d 70 4d 6f 64 20 29 7b 0a 20 20 20 20 20  l==pMod ){.     
2a993 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2a994 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  d = 1;.    }.   
2a995 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
2a996 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
2a997 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  0);.  }else if( 
2a998 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20  xDestroy ){.    
2a999 78 44 65 73 74 72 6f 79 28 70 41 75 78 29 3b 0a  xDestroy(pAux);.
2a99a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
2a99b 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 53 51  e3ApiExit(db, SQ
2a99c 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69  LITE_OK);.  sqli
2a99d 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
2a99e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
2a99f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
2a9a0 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41 50 49 20  ** External API 
2a9a1 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f  function used to
2a9a2 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   create a new vi
2a9a3 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 6f 64 75  rtual-table modu
2a9a4 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
2a9a5 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
2a9a6 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73  eate_module(.  s
2a9a7 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2a9a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a9a9 2a 20 44 61 74 61 62 61 73 65 20 69 6e 20 77 68  * Database in wh
2a9aa 69 63 68 20 6d 6f 64 75 6c 65 20 69 73 20 72 65  ich module is re
2a9ab 67 69 73 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f  gistered */.  co
2a9ac 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
2a9ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a9ae 20 4e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74   Name assigned t
2a9af 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f  o this module */
2a9b0 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
2a9b1 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
2a9b2 2c 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69  ,  /* The defini
2a9b3 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75  tion of the modu
2a9b4 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  le */.  void *pA
2a9b5 75 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ux              
2a9b6 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65          /* Conte
2a9b7 78 74 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 78  xt pointer for x
2a9b8 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20  Create/xConnect 
2a9b9 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  */.){.  return c
2a9ba 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20  reateModule(db, 
2a9bb 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20  zName, pModule, 
2a9bc 70 41 75 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  pAux, 0);.}../*.
2a9bd 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41 50 49 20  ** External API 
2a9be 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f  function used to
2a9bf 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   create a new vi
2a9c0 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 6f 64 75  rtual-table modu
2a9c1 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
2a9c2 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
2a9c3 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a  eate_module_v2(.
2a9c4 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2a9c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a9c6 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e    /* Database in
2a9c7 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73   which module is
2a9c8 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20   registered */. 
2a9c9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2a9ca 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  me,             
2a9cb 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65   /* Name assigne
2a9cc 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65  d to this module
2a9cd 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   */.  const sqli
2a9ce 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
2a9cf 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66  ule,  /* The def
2a9d0 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  inition of the m
2a9d1 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20  odule */.  void 
2a9d2 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20  *pAux,          
2a9d3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
2a9d4 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f  ntext pointer fo
2a9d5 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65  r xCreate/xConne
2a9d6 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  ct */.  void (*x
2a9d7 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29  Destroy)(void *)
2a9d8 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c          /* Modul
2a9d9 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
2a9da 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 72 65  ction */.){.  re
2a9db 74 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c  turn createModul
2a9dc 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f  e(db, zName, pMo
2a9dd 64 75 6c 65 2c 20 70 41 75 78 2c 20 78 44 65 73  dule, pAux, xDes
2a9de 74 72 6f 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  troy);.}../*.** 
2a9df 4c 6f 63 6b 20 74 68 65 20 76 69 72 74 75 61 6c  Lock the virtual
2a9e0 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20 69   table so that i
2a9e1 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 69 73 63  t cannot be disc
2a9e2 6f 6e 6e 65 63 74 65 64 2e 0a 2a 2a 20 4c 6f 63  onnected..** Loc
2a9e3 6b 73 20 6e 65 73 74 2e 20 20 45 76 65 72 79 20  ks nest.  Every 
2a9e4 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 68 61 76 65  lock should have
2a9e5 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67   a corresponding
2a9e6 20 75 6e 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20 61   unlock..** If a
2a9e7 6e 20 75 6e 6c 6f 63 6b 20 69 73 20 6f 6d 69 74  n unlock is omit
2a9e8 74 65 64 2c 20 72 65 73 6f 75 72 63 65 73 20 6c  ted, resources l
2a9e9 65 61 6b 73 20 77 69 6c 6c 20 6f 63 63 75 72 2e  eaks will occur.
2a9ea 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 64 69    .**.** If a di
2a9eb 73 63 6f 6e 6e 65 63 74 20 69 73 20 61 74 74 65  sconnect is atte
2a9ec 6d 70 74 65 64 20 77 68 69 6c 65 20 61 20 76 69  mpted while a vi
2a9ed 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 6c  rtual table is l
2a9ee 6f 63 6b 65 64 2c 0a 2a 2a 20 74 68 65 20 64 69  ocked,.** the di
2a9ef 73 63 6f 6e 6e 65 63 74 20 69 73 20 64 65 66 65  sconnect is defe
2a9f0 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6c  rred until all l
2a9f1 6f 63 6b 73 20 68 61 76 65 20 62 65 65 6e 20 72  ocks have been r
2a9f2 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  emoved..*/.SQLIT
2a9f3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2a9f4 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 73  qlite3VtabLock(s
2a9f5 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
2a9f6 61 62 29 7b 0a 20 20 70 56 74 61 62 2d 3e 6e 52  ab){.  pVtab->nR
2a9f7 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  ef++;.}../*.** U
2a9f8 6e 6c 6f 63 6b 20 61 20 76 69 72 74 75 61 6c 20  nlock a virtual 
2a9f9 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 74 68 65  table.  When the
2a9fa 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 72 65   last lock is re
2a9fb 6d 6f 76 65 64 2c 0a 2a 2a 20 64 69 73 63 6f 6e  moved,.** discon
2a9fc 6e 65 63 74 20 74 68 65 20 76 69 72 74 75 61 6c  nect the virtual
2a9fd 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
2a9fe 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2a9ff 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
2aa00 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73 71  (sqlite3 *db, sq
2aa01 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
2aa02 62 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 56  b){.  assert( pV
2aa03 74 61 62 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  tab->nRef>0 );. 
2aa04 20 70 56 74 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a   pVtab->nRef--;.
2aa05 20 20 61 73 73 65 72 74 28 64 62 29 3b 0a 20 20    assert(db);.  
2aa06 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
2aa07 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29  afetyCheckOk(db)
2aa08 20 29 3b 0a 20 20 69 66 28 20 70 56 74 61 62 2d   );.  if( pVtab-
2aa09 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
2aa0a 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  if( db->magic==S
2aa0b 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
2aa0c 20 29 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29   ){.      (void)
2aa0d 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
2aa0e 28 64 62 29 3b 0a 20 20 20 20 20 20 70 56 74 61  (db);.      pVta
2aa0f 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73  b->pModule->xDis
2aa10 63 6f 6e 6e 65 63 74 28 70 56 74 61 62 29 3b 0a  connect(pVtab);.
2aa11 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69        (void)sqli
2aa12 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
2aa13 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
2aa14 20 20 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75      pVtab->pModu
2aa15 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28  le->xDisconnect(
2aa16 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  pVtab);.    }.  
2aa17 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  }.}../*.** Clear
2aa18 20 61 6e 79 20 61 6e 64 20 61 6c 6c 20 76 69 72   any and all vir
2aa19 74 75 61 6c 2d 74 61 62 6c 65 20 69 6e 66 6f 72  tual-table infor
2aa1a 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  mation from the 
2aa1b 54 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2a  Table record..**
2aa1c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2aa1d 20 63 61 6c 6c 65 64 2c 20 66 6f 72 20 65 78 61   called, for exa
2aa1e 6d 70 6c 65 2c 20 6a 75 73 74 20 62 65 66 6f 72  mple, just befor
2aa1f 65 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54  e deleting the T
2aa20 61 62 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a  able.** record..
2aa21 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2aa22 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2aa23 61 62 43 6c 65 61 72 28 54 61 62 6c 65 20 2a 70  abClear(Table *p
2aa24 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  ){.  sqlite3_vta
2aa25 62 20 2a 70 56 74 61 62 20 3d 20 70 2d 3e 70 56  b *pVtab = p->pV
2aa26 74 61 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70  tab;.  Schema *p
2aa27 53 63 68 65 6d 61 20 3d 20 70 2d 3e 70 53 63 68  Schema = p->pSch
2aa28 65 6d 61 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ema;.  sqlite3 *
2aa29 64 62 20 3d 20 70 53 63 68 65 6d 61 20 3f 20 70  db = pSchema ? p
2aa2a 53 63 68 65 6d 61 2d 3e 64 62 20 3a 20 30 3b 0a  Schema->db : 0;.
2aa2b 20 20 69 66 28 20 70 56 74 61 62 20 29 7b 0a 20    if( pVtab ){. 
2aa2c 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4d     assert( p->pM
2aa2d 6f 64 20 26 26 20 70 2d 3e 70 4d 6f 64 2d 3e 70  od && p->pMod->p
2aa2e 4d 6f 64 75 6c 65 20 29 3b 0a 20 20 20 20 73 71  Module );.    sq
2aa2f 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
2aa30 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20  db, pVtab);.    
2aa31 70 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20 20  p->pVtab = 0;.  
2aa32 7d 0a 20 20 69 66 28 20 70 2d 3e 61 7a 4d 6f 64  }.  if( p->azMod
2aa33 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 69 6e  uleArg ){.    in
2aa34 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
2aa35 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75 6c 65 41 72  ; i<p->nModuleAr
2aa36 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  g; i++){.      s
2aa37 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2aa38 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b   p->azModuleArg[
2aa39 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  i]);.    }.    s
2aa3a 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2aa3b 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29   p->azModuleArg)
2aa3c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
2aa3d 64 64 20 61 20 6e 65 77 20 6d 6f 64 75 6c 65 20  dd a new module 
2aa3e 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 54 61 62  argument to pTab
2aa3f 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  le->azModuleArg[
2aa40 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67  ]..** The string
2aa41 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 2d   is not copied -
2aa42 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20   the pointer is 
2aa43 73 74 6f 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20  stored.  The.** 
2aa44 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 66  string will be f
2aa45 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  reed automatical
2aa46 6c 79 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c  ly when the tabl
2aa47 65 20 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 2e  e is.** deleted.
2aa48 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2aa49 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e  addModuleArgumen
2aa4a 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54  t(sqlite3 *db, T
2aa4b 61 62 6c 65 20 2a 70 54 61 62 6c 65 2c 20 63 68  able *pTable, ch
2aa4c 61 72 20 2a 7a 41 72 67 29 7b 0a 20 20 69 6e 74  ar *zArg){.  int
2aa4d 20 69 20 3d 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f   i = pTable->nMo
2aa4e 64 75 6c 65 41 72 67 2b 2b 3b 0a 20 20 69 6e 74  duleArg++;.  int
2aa4f 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66   nBytes = sizeof
2aa50 28 63 68 61 72 20 2a 29 2a 28 31 2b 70 54 61 62  (char *)*(1+pTab
2aa51 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 29 3b  le->nModuleArg);
2aa52 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75  .  char **azModu
2aa53 6c 65 41 72 67 3b 0a 20 20 61 7a 4d 6f 64 75 6c  leArg;.  azModul
2aa54 65 41 72 67 20 3d 20 73 71 6c 69 74 65 33 44 62  eArg = sqlite3Db
2aa55 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 54 61 62  Realloc(db, pTab
2aa56 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 2c  le->azModuleArg,
2aa57 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20   nBytes);.  if( 
2aa58 61 7a 4d 6f 64 75 6c 65 41 72 67 3d 3d 30 20 29  azModuleArg==0 )
2aa59 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20  {.    int j;.   
2aa5a 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a   for(j=0; j<i; j
2aa5b 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
2aa5c 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61  e3DbFree(db, pTa
2aa5d 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  ble->azModuleArg
2aa5e 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  [j]);.    }.    
2aa5f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2aa60 2c 20 7a 41 72 67 29 3b 0a 20 20 20 20 73 71 6c  , zArg);.    sql
2aa61 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2aa62 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41  Table->azModuleA
2aa63 72 67 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d  rg);.    pTable-
2aa64 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20 30 3b  >nModuleArg = 0;
2aa65 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 7a  .  }else{.    az
2aa66 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 20 3d 20 7a  ModuleArg[i] = z
2aa67 41 72 67 3b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c  Arg;.    azModul
2aa68 65 41 72 67 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20  eArg[i+1] = 0;. 
2aa69 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d   }.  pTable->azM
2aa6a 6f 64 75 6c 65 41 72 67 20 3d 20 61 7a 4d 6f 64  oduleArg = azMod
2aa6b 75 6c 65 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  uleArg;.}../*.**
2aa6c 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
2aa6d 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  s this routine w
2aa6e 68 65 6e 20 69 74 20 66 69 72 73 74 20 73 65 65  hen it first see
2aa6f 73 20 61 20 43 52 45 41 54 45 20 56 49 52 54 55  s a CREATE VIRTU
2aa70 41 4c 20 54 41 42 4c 45 0a 2a 2a 20 73 74 61 74  AL TABLE.** stat
2aa71 65 6d 65 6e 74 2e 20 20 54 68 65 20 6d 6f 64 75  ement.  The modu
2aa72 6c 65 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e  le name has been
2aa73 20 70 61 72 73 65 64 2c 20 62 75 74 20 74 68 65   parsed, but the
2aa74 20 6f 70 74 69 6f 6e 61 6c 20 6c 69 73 74 0a 2a   optional list.*
2aa75 2a 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20  * of parameters 
2aa76 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20  that follow the 
2aa77 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 61 72 65 20  module name are 
2aa78 73 74 69 6c 6c 20 70 65 6e 64 69 6e 67 2e 0a 2a  still pending..*
2aa79 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2aa7a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
2aa7b 62 42 65 67 69 6e 50 61 72 73 65 28 0a 20 20 50  bBeginParse(.  P
2aa7c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2aa7d 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2aa7e 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b  context */.  Tok
2aa7f 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20  en *pName1,     
2aa80 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65     /* Name of ne
2aa81 77 20 74 61 62 6c 65 2c 20 6f 72 20 64 61 74 61  w table, or data
2aa82 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54  base name */.  T
2aa83 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
2aa84 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2aa85 6e 65 77 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c  new table or NUL
2aa86 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4d  L */.  Token *pM
2aa87 6f 64 75 6c 65 4e 61 6d 65 20 20 20 20 2f 2a 20  oduleName    /* 
2aa88 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75  Name of the modu
2aa89 6c 65 20 66 6f 72 20 74 68 65 20 76 69 72 74 75  le for the virtu
2aa8a 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  al table */.){. 
2aa8b 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
2aa8c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2aa8d 74 61 62 61 73 65 20 74 68 65 20 74 61 62 6c 65  tabase the table
2aa8e 20 69 73 20 62 65 69 6e 67 20 63 72 65 61 74 65   is being create
2aa8f 64 20 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20  d in */.  Table 
2aa90 2a 70 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20  *pTable;        
2aa91 2f 2a 20 54 68 65 20 6e 65 77 20 76 69 72 74 75  /* The new virtu
2aa92 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71  al table */.  sq
2aa93 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
2aa94 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2aa95 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
2aa96 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
2aa97 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2aa98 53 68 61 72 65 64 43 61 63 68 65 20 29 7b 0a 20  SharedCache ){. 
2aa99 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2aa9a 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e  sg(pParse, "Cann
2aa9b 6f 74 20 75 73 65 20 76 69 72 74 75 61 6c 20 74  ot use virtual t
2aa9c 61 62 6c 65 73 20 69 6e 20 73 68 61 72 65 64 2d  ables in shared-
2aa9d 63 61 63 68 65 20 6d 6f 64 65 22 29 3b 0a 20 20  cache mode");.  
2aa9e 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
2aa9f 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62   sqlite3StartTab
2aaa0 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  le(pParse, pName
2aaa1 31 2c 20 70 4e 61 6d 65 32 2c 20 30 2c 20 30 2c  1, pName2, 0, 0,
2aaa2 20 31 2c 20 30 29 3b 0a 20 20 70 54 61 62 6c 65   1, 0);.  pTable
2aaa3 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
2aaa4 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61 62  able;.  if( pTab
2aaa5 6c 65 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d  le==0 || pParse-
2aaa6 3e 6e 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a  >nErr ) return;.
2aaa7 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 54 61    assert( 0==pTa
2aaa8 62 6c 65 2d 3e 70 49 6e 64 65 78 20 29 3b 0a 0a  ble->pIndex );..
2aaa9 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2aaaa 62 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  b;.  iDb = sqlit
2aaab 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2aaac 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 63 68  db, pTable->pSch
2aaad 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ema);.  assert( 
2aaae 69 44 62 3e 3d 30 20 29 3b 0a 0a 20 20 70 54 61  iDb>=0 );..  pTa
2aaaf 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d  ble->tabFlags |=
2aab0 20 54 46 5f 56 69 72 74 75 61 6c 3b 0a 20 20 70   TF_Virtual;.  p
2aab1 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72  Table->nModuleAr
2aab2 67 20 3d 20 30 3b 0a 20 20 61 64 64 4d 6f 64 75  g = 0;.  addModu
2aab3 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70  leArgument(db, p
2aab4 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 4e 61  Table, sqlite3Na
2aab5 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
2aab6 70 4d 6f 64 75 6c 65 4e 61 6d 65 29 29 3b 0a 20  pModuleName));. 
2aab7 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65   addModuleArgume
2aab8 6e 74 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73  nt(db, pTable, s
2aab9 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
2aaba 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  b, db->aDb[iDb].
2aabb 7a 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f  zName));.  addMo
2aabc 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c  duleArgument(db,
2aabd 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33   pTable, sqlite3
2aabe 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 54 61  DbStrDup(db, pTa
2aabf 62 6c 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20  ble->zName));.  
2aac0 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
2aac1 65 6e 2e 6e 20 3d 20 28 69 6e 74 29 28 26 70 4d  en.n = (int)(&pM
2aac2 6f 64 75 6c 65 4e 61 6d 65 2d 3e 7a 5b 70 4d 6f  oduleName->z[pMo
2aac3 64 75 6c 65 4e 61 6d 65 2d 3e 6e 5d 20 2d 20 70  duleName->n] - p
2aac4 4e 61 6d 65 31 2d 3e 7a 29 3b 0a 0a 23 69 66 6e  Name1->z);..#ifn
2aac5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2aac6 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
2aac7 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 76 69  /* Creating a vi
2aac8 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 76 6f  rtual table invo
2aac9 6b 65 73 20 74 68 65 20 61 75 74 68 6f 72 69 7a  kes the authoriz
2aaca 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 74  ation callback t
2aacb 77 69 63 65 2e 0a 20 20 2a 2a 20 54 68 65 20 66  wice..  ** The f
2aacc 69 72 73 74 20 69 6e 76 6f 63 61 74 69 6f 6e 2c  irst invocation,
2aacd 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d 69   to obtain permi
2aace 73 73 69 6f 6e 20 74 6f 20 49 4e 53 45 52 54 20  ssion to INSERT 
2aacf 61 20 72 6f 77 20 69 6e 74 6f 20 74 68 65 0a 20  a row into the. 
2aad0 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65   ** sqlite_maste
2aad1 72 20 74 61 62 6c 65 2c 20 68 61 73 20 61 6c 72  r table, has alr
2aad2 65 61 64 79 20 62 65 65 6e 20 6d 61 64 65 20 62  eady been made b
2aad3 79 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61  y sqlite3StartTa
2aad4 62 6c 65 28 29 2e 0a 20 20 2a 2a 20 54 68 65 20  ble()..  ** The 
2aad5 73 65 63 6f 6e 64 20 63 61 6c 6c 2c 20 74 6f 20  second call, to 
2aad6 6f 62 74 61 69 6e 20 70 65 72 6d 69 73 73 69 6f  obtain permissio
2aad7 6e 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  n to create the 
2aad8 74 61 62 6c 65 2c 20 69 73 20 6d 61 64 65 20 6e  table, is made n
2aad9 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ow..  */.  if( p
2aada 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41  Table->azModuleA
2aadb 72 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rg ){.    sqlite
2aadc 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2aadd 65 2c 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  e, SQLITE_CREATE
2aade 5f 56 54 41 42 4c 45 2c 20 70 54 61 62 6c 65 2d  _VTABLE, pTable-
2aadf 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20  >zName, .       
2aae0 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d       pTable->azM
2aae1 6f 64 75 6c 65 41 72 67 5b 30 5d 2c 20 70 50 61  oduleArg[0], pPa
2aae2 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62  rse->db->aDb[iDb
2aae3 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65  ].zName);.  }.#e
2aae4 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ndif.}../*.** Th
2aae5 69 73 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73  is routine takes
2aae6 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 72 67 75   the module argu
2aae7 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 62 65  ment that has be
2aae8 65 6e 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 0a  en accumulating.
2aae9 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 41  ** in pParse->zA
2aaea 72 67 5b 5d 20 61 6e 64 20 61 70 70 65 6e 64 73  rg[] and appends
2aaeb 20 69 74 20 74 6f 20 74 68 65 20 6c 69 73 74 20   it to the list 
2aaec 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20  of arguments on 
2aaed 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  the.** virtual t
2aaee 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
2aaef 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
2aaf0 6e 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 54 61  n in pParse->pTa
2aaf1 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
2aaf2 6f 69 64 20 61 64 64 41 72 67 75 6d 65 6e 74 54  oid addArgumentT
2aaf3 6f 56 74 61 62 28 50 61 72 73 65 20 2a 70 50 61  oVtab(Parse *pPa
2aaf4 72 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72  rse){.  if( pPar
2aaf5 73 65 2d 3e 73 41 72 67 2e 7a 20 26 26 20 70 50  se->sArg.z && pP
2aaf6 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
2aaf7 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
2aaf8 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68  r *z = (const ch
2aaf9 61 72 2a 29 70 50 61 72 73 65 2d 3e 73 41 72 67  ar*)pParse->sArg
2aafa 2e 7a 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  .z;.    int n = 
2aafb 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e 3b 0a  pParse->sArg.n;.
2aafc 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
2aafd 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2aafe 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d    addModuleArgum
2aaff 65 6e 74 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ent(db, pParse->
2ab00 70 4e 65 77 54 61 62 6c 65 2c 20 73 71 6c 69 74  pNewTable, sqlit
2ab01 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
2ab02 7a 2c 20 6e 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  z, n));.  }.}../
2ab03 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20  *.** The parser 
2ab04 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
2ab05 6e 65 20 61 66 74 65 72 20 74 68 65 20 43 52 45  ne after the CRE
2ab06 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
2ab07 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68  E statement.** h
2ab08 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65  as been complete
2ab09 6c 79 20 70 61 72 73 65 64 2e 0a 2a 2f 0a 53 51  ly parsed..*/.SQ
2ab0a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2ab0b 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e  d sqlite3VtabFin
2ab0c 69 73 68 50 61 72 73 65 28 50 61 72 73 65 20 2a  ishParse(Parse *
2ab0d 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70  pParse, Token *p
2ab0e 45 6e 64 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  End){.  Table *p
2ab0f 54 61 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  Tab;        /* T
2ab10 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63  he table being c
2ab11 6f 6e 73 74 72 75 63 74 65 64 20 2a 2f 0a 20 20  onstructed */.  
2ab12 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
2ab13 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2ab14 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2ab15 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c  /.  char *zModul
2ab16 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d  e;      /* The m
2ab17 6f 64 75 6c 65 20 6e 61 6d 65 20 6f 66 20 74 68  odule name of th
2ab18 65 20 74 61 62 6c 65 3a 20 55 53 49 4e 47 20 6d  e table: USING m
2ab19 6f 64 75 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 4d  odulename */.  M
2ab1a 6f 64 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 30 3b  odule *pMod = 0;
2ab1b 0a 0a 20 20 61 64 64 41 72 67 75 6d 65 6e 74 54  ..  addArgumentT
2ab1c 6f 56 74 61 62 28 70 50 61 72 73 65 29 3b 0a 20  oVtab(pParse);. 
2ab1d 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
2ab1e 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 75  = 0;..  /* Looku
2ab1f 70 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d  p the module nam
2ab20 65 2e 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 70  e. */.  pTab = p
2ab21 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2ab22 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
2ab23 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d  ) return;.  db =
2ab24 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
2ab25 66 28 20 70 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65  f( pTab->nModule
2ab26 41 72 67 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a  Arg<1 ) return;.
2ab27 20 20 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62    zModule = pTab
2ab28 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
2ab29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75  ;.  pMod = (Modu
2ab2a 6c 65 2a 29 73 71 6c 69 74 65 33 48 61 73 68 46  le*)sqlite3HashF
2ab2b 69 6e 64 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  ind(&db->aModule
2ab2c 2c 20 7a 4d 6f 64 75 6c 65 2c 0a 20 20 20 20 20  , zModule,.     
2ab2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ab2e 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2ab2f 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4d 6f  ite3Strlen30(zMo
2ab30 64 75 6c 65 29 29 3b 0a 20 20 70 54 61 62 2d 3e  dule));.  pTab->
2ab31 70 4d 6f 64 20 3d 20 70 4d 6f 64 3b 0a 20 20 0a  pMod = pMod;.  .
2ab32 20 20 2f 2a 20 49 66 20 74 68 65 20 43 52 45 41    /* If the CREA
2ab33 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
2ab34 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62 65   statement is be
2ab35 69 6e 67 20 65 6e 74 65 72 65 64 20 66 6f 72 20  ing entered for 
2ab36 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74  the.  ** first t
2ab37 69 6d 65 20 28 69 6e 20 6f 74 68 65 72 20 77 6f  ime (in other wo
2ab38 72 64 73 20 69 66 20 74 68 65 20 76 69 72 74 75  rds if the virtu
2ab39 61 6c 20 74 61 62 6c 65 20 69 73 20 61 63 74 75  al table is actu
2ab3a 61 6c 6c 79 20 62 65 69 6e 67 0a 20 20 2a 2a 20  ally being.  ** 
2ab3b 63 72 65 61 74 65 64 20 6e 6f 77 20 69 6e 73 74  created now inst
2ab3c 65 61 64 20 6f 66 20 6a 75 73 74 20 62 65 69 6e  ead of just bein
2ab3d 67 20 72 65 61 64 20 6f 75 74 20 6f 66 20 73 71  g read out of sq
2ab3e 6c 69 74 65 5f 6d 61 73 74 65 72 29 20 74 68 65  lite_master) the
2ab3f 6e 0a 20 20 2a 2a 20 64 6f 20 61 64 64 69 74 69  n.  ** do additi
2ab40 6f 6e 61 6c 20 69 6e 69 74 69 61 6c 69 7a 61 74  onal initializat
2ab41 69 6f 6e 20 77 6f 72 6b 20 61 6e 64 20 73 74 6f  ion work and sto
2ab42 72 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  re the statement
2ab43 20 74 65 78 74 0a 20 20 2a 2a 20 69 6e 20 74 68   text.  ** in th
2ab44 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
2ab45 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  table..  */.  if
2ab46 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  ( !db->init.busy
2ab47 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53   ){.    char *zS
2ab48 74 6d 74 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  tmt;.    char *z
2ab49 57 68 65 72 65 3b 0a 20 20 20 20 69 6e 74 20 69  Where;.    int i
2ab4a 44 62 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b  Db;.    Vdbe *v;
2ab4b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65  ..    /* Compute
2ab4c 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65   the complete te
2ab4d 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
2ab4e 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73   VIRTUAL TABLE s
2ab4f 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
2ab50 69 66 28 20 70 45 6e 64 20 29 7b 0a 20 20 20 20  if( pEnd ){.    
2ab51 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54    pParse->sNameT
2ab52 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e 74 29 28 70  oken.n = (int)(p
2ab53 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 73 65 2d  End->z - pParse-
2ab54 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 20 2b  >sNameToken.z) +
2ab55 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20 20 20 7d 0a   pEnd->n;.    }.
2ab56 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69      zStmt = sqli
2ab57 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2ab58 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
2ab59 41 42 4c 45 20 25 54 22 2c 20 26 70 50 61 72 73  ABLE %T", &pPars
2ab5a 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 29 3b 0a  e->sNameToken);.
2ab5b 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f 74 20 66  .    /* A slot f
2ab5c 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 68 61  or the record ha
2ab5d 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61  s already been a
2ab5e 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20  llocated in the 
2ab5f 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4d  .    ** SQLITE_M
2ab60 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 57 65  ASTER table.  We
2ab61 20 6a 75 73 74 20 6e 65 65 64 20 74 6f 20 75 70   just need to up
2ab62 64 61 74 65 20 74 68 61 74 20 73 6c 6f 74 20 77  date that slot w
2ab63 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a 2a 20 74  ith all.    ** t
2ab64 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77  he information w
2ab65 65 27 76 65 20 63 6f 6c 6c 65 63 74 65 64 2e 20  e've collected. 
2ab66 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20   .    **.    ** 
2ab67 54 68 65 20 56 4d 20 72 65 67 69 73 74 65 72 20  The VM register 
2ab68 6e 75 6d 62 65 72 20 70 50 61 72 73 65 2d 3e 72  number pParse->r
2ab69 65 67 52 6f 77 69 64 20 68 6f 6c 64 73 20 74 68  egRowid holds th
2ab6a 65 20 72 6f 77 69 64 20 6f 66 20 61 6e 0a 20 20  e rowid of an.  
2ab6b 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68    ** entry in th
2ab6c 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
2ab6d 74 61 62 6c 65 20 74 68 74 20 77 61 73 20 63 72  table tht was cr
2ab6e 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 76  eated for this v
2ab6f 74 61 62 0a 20 20 20 20 2a 2a 20 62 79 20 73 71  tab.    ** by sq
2ab70 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
2ab71 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 44  )..    */.    iD
2ab72 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2ab73 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
2ab74 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
2ab75 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
2ab76 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
2ab77 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20    "UPDATE %Q.%s 
2ab78 22 0a 20 20 20 20 20 20 20 20 20 22 53 45 54 20  ".         "SET 
2ab79 74 79 70 65 3d 27 74 61 62 6c 65 27 2c 20 6e 61  type='table', na
2ab7a 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d  me=%Q, tbl_name=
2ab7b 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 30 2c 20  %Q, rootpage=0, 
2ab7c 73 71 6c 3d 25 51 20 22 0a 20 20 20 20 20 20 20  sql=%Q ".       
2ab7d 22 57 48 45 52 45 20 72 6f 77 69 64 3d 23 25 64  "WHERE rowid=#%d
2ab7e 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62  ",.      db->aDb
2ab7f 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48  [iDb].zName, SCH
2ab80 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a  EMA_TABLE(iDb),.
2ab81 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
2ab82 65 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  e,.      pTab->z
2ab83 4e 61 6d 65 2c 0a 20 20 20 20 20 20 7a 53 74 6d  Name,.      zStm
2ab84 74 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  t,.      pParse-
2ab85 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 20 29 3b  >regRowid.    );
2ab86 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2ab87 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 20  ee(db, zStmt);. 
2ab88 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
2ab89 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
2ab8a 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
2ab8b 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
2ab8c 44 62 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  Db);..    sqlite
2ab8d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2ab8e 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30 29 3b  P_Expire, 0, 0);
2ab8f 0a 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73 71  .    zWhere = sq
2ab90 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2ab91 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70 54   "name='%q'", pT
2ab92 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
2ab93 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ab94 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68  4(v, OP_ParseSch
2ab95 65 6d 61 2c 20 69 44 62 2c 20 31 2c 20 30 2c 20  ema, iDb, 1, 0, 
2ab96 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d  zWhere, P4_DYNAM
2ab97 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
2ab98 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2ab99 5f 56 43 72 65 61 74 65 2c 20 69 44 62 2c 20 30  _VCreate, iDb, 0
2ab9a 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20  , 0, .          
2ab9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2ab9c 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69  Tab->zName, sqli
2ab9d 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61 62  te3Strlen30(pTab
2ab9e 2d 3e 7a 4e 61 6d 65 29 20 2b 20 31 29 3b 0a 20  ->zName) + 1);. 
2ab9f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61   }..  /* If we a
2aba0 72 65 20 72 65 72 65 61 64 69 6e 67 20 74 68 65  re rereading the
2aba1 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
2aba2 61 62 6c 65 20 63 72 65 61 74 65 20 74 68 65 20  able create the 
2aba3 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 72  in-memory.  ** r
2aba4 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74 61 62  ecord of the tab
2aba5 6c 65 2e 20 49 66 20 74 68 65 20 6d 6f 64 75 6c  le. If the modul
2aba6 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  e has already be
2aba7 65 6e 20 72 65 67 69 73 74 65 72 65 64 2c 0a 20  en registered,. 
2aba8 20 2a 2a 20 61 6c 73 6f 20 63 61 6c 6c 20 74 68   ** also call th
2aba9 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f  e xConnect metho
2abaa 64 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 65  d here..  */.  e
2abab 6c 73 65 20 7b 0a 20 20 20 20 54 61 62 6c 65 20  lse {.    Table 
2abac 2a 70 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d  *pOld;.    Schem
2abad 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 54 61  a *pSchema = pTa
2abae 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20 20  b->pSchema;.    
2abaf 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2abb0 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  e = pTab->zName;
2abb1 0a 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d  .    int nName =
2abb2 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2abb3 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4f 6c  (zName);.    pOl
2abb4 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  d = sqlite3HashI
2abb5 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e  nsert(&pSchema->
2abb6 74 62 6c 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20  tblHash, zName, 
2abb7 6e 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0a 20 20  nName, pTab);.  
2abb8 20 20 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20    if( pOld ){.  
2abb9 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
2abba 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
2abbb 61 73 73 65 72 74 28 20 70 54 61 62 3d 3d 70 4f  assert( pTab==pO
2abbc 6c 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63  ld );  /* Malloc
2abbd 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65   must have faile
2abbe 64 20 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73  d inside HashIns
2abbf 65 72 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 72  ert() */.      r
2abc0 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
2abc1 20 70 53 63 68 65 6d 61 2d 3e 64 62 20 3d 20 70   pSchema->db = p
2abc2 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70  Parse->db;.    p
2abc3 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2abc4 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 0;.  }.}../*.
2abc5 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
2abc6 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
2abc7 20 77 68 65 6e 20 69 74 20 73 65 65 73 20 74 68   when it sees th
2abc8 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a  e first token.**
2abc9 20 6f 66 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   of an argument 
2abca 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61  to the module na
2abcb 6d 65 20 69 6e 20 61 20 43 52 45 41 54 45 20 56  me in a CREATE V
2abcc 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
2abcd 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  tement..*/.SQLIT
2abce 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2abcf 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69  qlite3VtabArgIni
2abd0 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  t(Parse *pParse)
2abd1 7b 0a 20 20 61 64 64 41 72 67 75 6d 65 6e 74 54  {.  addArgumentT
2abd2 6f 56 74 61 62 28 70 50 61 72 73 65 29 3b 0a 20  oVtab(pParse);. 
2abd3 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20   pParse->sArg.z 
2abd4 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73  = 0;.  pParse->s
2abd5 41 72 67 2e 6e 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  Arg.n = 0;.}../*
2abd6 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63  .** The parser c
2abd7 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2abd8 65 20 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e  e for each token
2abd9 20 61 66 74 65 72 20 74 68 65 20 66 69 72 73 74   after the first
2abda 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 61 6e 20   token.** in an 
2abdb 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
2abdc 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61  module name in a
2abdd 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2abde 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
2abdf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2abe0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
2abe1 74 61 62 41 72 67 45 78 74 65 6e 64 28 50 61 72  tabArgExtend(Par
2abe2 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
2abe3 6e 20 2a 70 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a  n *p){.  Token *
2abe4 70 41 72 67 20 3d 20 26 70 50 61 72 73 65 2d 3e  pArg = &pParse->
2abe5 73 41 72 67 3b 0a 20 20 69 66 28 20 70 41 72 67  sArg;.  if( pArg
2abe6 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 70 41  ->z==0 ){.    pA
2abe7 72 67 2d 3e 7a 20 3d 20 70 2d 3e 7a 3b 0a 20 20  rg->z = p->z;.  
2abe8 20 20 70 41 72 67 2d 3e 6e 20 3d 20 70 2d 3e 6e    pArg->n = p->n
2abe9 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
2abea 73 73 65 72 74 28 70 41 72 67 2d 3e 7a 20 3c 20  ssert(pArg->z < 
2abeb 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 41 72 67 2d  p->z);.    pArg-
2abec 3e 6e 20 3d 20 28 69 6e 74 29 28 26 70 2d 3e 7a  >n = (int)(&p->z
2abed 5b 70 2d 3e 6e 5d 20 2d 20 70 41 72 67 2d 3e 7a  [p->n] - pArg->z
2abee 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2abef 49 6e 76 6f 6b 65 20 61 20 76 69 72 74 75 61 6c  Invoke a virtual
2abf0 20 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74   table construct
2abf1 6f 72 20 28 65 69 74 68 65 72 20 78 43 72 65 61  or (either xCrea
2abf2 74 65 20 6f 72 20 78 43 6f 6e 6e 65 63 74 29 2e  te or xConnect).
2abf3 20 54 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20   The.** pointer 
2abf4 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
2abf5 74 6f 20 69 6e 76 6f 6b 65 20 69 73 20 70 61 73  to invoke is pas
2abf6 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74  sed as the fourt
2abf7 68 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74  h parameter.** t
2abf8 6f 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  o this procedure
2abf9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2abfa 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63  vtabCallConstruc
2abfb 74 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  tor(.  sqlite3 *
2abfc 64 62 2c 20 0a 20 20 54 61 62 6c 65 20 2a 70 54  db, .  Table *pT
2abfd 61 62 2c 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d  ab,.  Module *pM
2abfe 6f 64 2c 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e  od,.  int (*xCon
2abff 73 74 72 75 63 74 29 28 73 71 6c 69 74 65 33 2a  struct)(sqlite3*
2ac00 2c 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  ,void*,int,const
2ac01 20 63 68 61 72 2a 63 6f 6e 73 74 2a 2c 73 71 6c   char*const*,sql
2ac02 69 74 65 33 5f 76 74 61 62 2a 2a 2c 63 68 61 72  ite3_vtab**,char
2ac03 2a 2a 29 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a  **),.  char **pz
2ac04 45 72 72 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Err.){.  int rc;
2ac05 0a 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 73 71  .  int rc2;.  sq
2ac06 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
2ac07 62 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63  b = 0;.  const c
2ac08 68 61 72 20 2a 63 6f 6e 73 74 2a 61 7a 41 72 67  har *const*azArg
2ac09 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
2ac0a 63 6f 6e 73 74 2a 29 70 54 61 62 2d 3e 61 7a 4d  const*)pTab->azM
2ac0b 6f 64 75 6c 65 41 72 67 3b 0a 20 20 69 6e 74 20  oduleArg;.  int 
2ac0c 6e 41 72 67 20 3d 20 70 54 61 62 2d 3e 6e 4d 6f  nArg = pTab->nMo
2ac0d 64 75 6c 65 41 72 67 3b 0a 20 20 63 68 61 72 20  duleArg;.  char 
2ac0e 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 63 68 61  *zErr = 0;.  cha
2ac0f 72 20 2a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 3d  r *zModuleName =
2ac10 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2ac11 64 62 2c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e  db, "%s", pTab->
2ac12 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 21  zName);..  if( !
2ac13 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 29 7b 0a 20  zModuleName ){. 
2ac14 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2ac15 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61  _NOMEM;.  }..  a
2ac16 73 73 65 72 74 28 20 21 64 62 2d 3e 70 56 54 61  ssert( !db->pVTa
2ac17 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78  b );.  assert( x
2ac18 43 6f 6e 73 74 72 75 63 74 20 29 3b 0a 0a 20 20  Construct );..  
2ac19 64 62 2d 3e 70 56 54 61 62 20 3d 20 70 54 61 62  db->pVTab = pTab
2ac1a 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
2ac1b 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
2ac1c 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
2ac1d 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d  ITE_OK );.  rc =
2ac1e 20 78 43 6f 6e 73 74 72 75 63 74 28 64 62 2c 20   xConstruct(db, 
2ac1f 70 4d 6f 64 2d 3e 70 41 75 78 2c 20 6e 41 72 67  pMod->pAux, nArg
2ac20 2c 20 61 7a 41 72 67 2c 20 26 70 56 74 61 62 2c  , azArg, &pVtab,
2ac21 20 26 7a 45 72 72 29 3b 0a 20 20 72 63 32 20 3d   &zErr);.  rc2 =
2ac22 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
2ac23 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  (db);.  if( rc==
2ac24 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 56 74  SQLITE_OK && pVt
2ac25 61 62 20 29 7b 0a 20 20 20 20 70 56 74 61 62 2d  ab ){.    pVtab-
2ac26 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d  >pModule = pMod-
2ac27 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56  >pModule;.    pV
2ac28 74 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  tab->nRef = 1;. 
2ac29 20 20 20 70 54 61 62 2d 3e 70 56 74 61 62 20 3d     pTab->pVtab =
2ac2a 20 70 56 74 61 62 3b 0a 20 20 7d 0a 0a 20 20 69   pVtab;.  }..  i
2ac2b 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63  f( SQLITE_OK!=rc
2ac2c 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 45 72 72   ){.    if( zErr
2ac2d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a  ==0 ){.      *pz
2ac2e 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
2ac2f 69 6e 74 66 28 64 62 2c 20 22 76 74 61 62 6c 65  intf(db, "vtable
2ac30 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 66 61 69   constructor fai
2ac31 6c 65 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c  led: %s", zModul
2ac32 65 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73  eName);.    }els
2ac33 65 20 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72  e {.      *pzErr
2ac34 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2ac35 66 28 64 62 2c 20 22 25 73 22 2c 20 7a 45 72 72  f(db, "%s", zErr
2ac36 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2ac37 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
2ac38 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
2ac39 69 66 28 20 64 62 2d 3e 70 56 54 61 62 20 29 7b  if( db->pVTab ){
2ac3a 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2ac3b 2a 7a 46 6f 72 6d 61 74 20 3d 20 22 76 74 61 62  *zFormat = "vtab
2ac3c 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 64  le constructor d
2ac3d 69 64 20 6e 6f 74 20 64 65 63 6c 61 72 65 20 73  id not declare s
2ac3e 63 68 65 6d 61 3a 20 25 73 22 3b 0a 20 20 20 20  chema: %s";.    
2ac3f 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33  *pzErr = sqlite3
2ac40 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72  MPrintf(db, zFor
2ac41 6d 61 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  mat, pTab->zName
2ac42 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
2ac43 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 20 0a 20  TE_ERROR;.  } . 
2ac44 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2ac45 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72  OK ){.    rc = r
2ac46 63 32 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 56  c2;.  }.  db->pV
2ac47 54 61 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  Tab = 0;.  sqlit
2ac48 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 6f  e3DbFree(db, zMo
2ac49 64 75 6c 65 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a  duleName);..  /*
2ac4a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   If everything w
2ac4b 65 6e 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ent according to
2ac4c 20 70 6c 61 6e 2c 20 6c 6f 6f 70 20 74 68 72 6f   plan, loop thro
2ac4d 75 67 68 20 74 68 65 20 63 6f 6c 75 6d 6e 73 0a  ugh the columns.
2ac4e 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c    ** of the tabl
2ac4f 65 20 74 6f 20 73 65 65 20 69 66 20 61 6e 79 20  e to see if any 
2ac50 6f 66 20 74 68 65 6d 20 63 6f 6e 74 61 69 6e 20  of them contain 
2ac51 74 68 65 20 74 6f 6b 65 6e 20 22 68 69 64 64 65  the token "hidde
2ac52 6e 22 2e 0a 20 20 2a 2a 20 49 66 20 73 6f 2c 20  n"..  ** If so, 
2ac53 73 65 74 20 74 68 65 20 43 6f 6c 75 6d 6e 2e 69  set the Column.i
2ac54 73 48 69 64 64 65 6e 20 66 6c 61 67 20 61 6e 64  sHidden flag and
2ac55 20 72 65 6d 6f 76 65 20 74 68 65 20 74 6f 6b 65   remove the toke
2ac56 6e 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20  n from.  ** the 
2ac57 74 79 70 65 20 73 74 72 69 6e 67 2e 0a 20 20 2a  type string..  *
2ac58 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
2ac59 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74  TE_OK ){.    int
2ac5a 20 69 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69   iCol;.    for(i
2ac5b 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62  Col=0; iCol<pTab
2ac5c 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b  ->nCol; iCol++){
2ac5d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 79  .      char *zTy
2ac5e 70 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  pe = pTab->aCol[
2ac5f 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20  iCol].zType;.   
2ac60 20 20 20 69 6e 74 20 6e 54 79 70 65 3b 0a 20 20     int nType;.  
2ac61 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20      int i = 0;. 
2ac62 20 20 20 20 20 69 66 28 20 21 7a 54 79 70 65 20       if( !zType 
2ac63 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2ac64 20 20 6e 54 79 70 65 20 3d 20 73 71 6c 69 74 65    nType = sqlite
2ac65 33 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29  3Strlen30(zType)
2ac66 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
2ac67 74 65 33 53 74 72 4e 49 43 6d 70 28 22 68 69 64  te3StrNICmp("hid
2ac68 64 65 6e 22 2c 20 7a 54 79 70 65 2c 20 36 29 20  den", zType, 6) 
2ac69 7c 7c 20 28 7a 54 79 70 65 5b 36 5d 20 26 26 20  || (zType[6] && 
2ac6a 7a 54 79 70 65 5b 36 5d 21 3d 27 20 27 29 20 29  zType[6]!=' ') )
2ac6b 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  {.        for(i=
2ac6c 30 3b 20 69 3c 6e 54 79 70 65 3b 20 69 2b 2b 29  0; i<nType; i++)
2ac6d 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
2ac6e 28 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49  (0==sqlite3StrNI
2ac6f 43 6d 70 28 22 20 68 69 64 64 65 6e 22 2c 20 26  Cmp(" hidden", &
2ac70 7a 54 79 70 65 5b 69 5d 2c 20 37 29 29 0a 20 20  zType[i], 7)).  
2ac71 20 20 20 20 20 20 20 20 20 26 26 20 28 7a 54 79           && (zTy
2ac72 70 65 5b 69 2b 37 5d 3d 3d 27 5c 30 27 20 7c 7c  pe[i+7]=='\0' ||
2ac73 20 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 20 27   zType[i+7]==' '
2ac74 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20  ).          ){. 
2ac75 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
2ac76 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
2ac77 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
2ac78 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2ac79 0a 20 20 20 20 20 20 69 66 28 20 69 3c 6e 54 79  .      if( i<nTy
2ac7a 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  pe ){.        in
2ac7b 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  t j;.        int
2ac7c 20 6e 44 65 6c 20 3d 20 36 20 2b 20 28 7a 54 79   nDel = 6 + (zTy
2ac7d 70 65 5b 69 2b 36 5d 20 3f 20 31 20 3a 20 30 29  pe[i+6] ? 1 : 0)
2ac7e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
2ac7f 69 3b 20 28 6a 2b 6e 44 65 6c 29 3c 3d 6e 54 79  i; (j+nDel)<=nTy
2ac80 70 65 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  pe; j++){.      
2ac81 20 20 20 20 7a 54 79 70 65 5b 6a 5d 20 3d 20 7a      zType[j] = z
2ac82 54 79 70 65 5b 6a 2b 6e 44 65 6c 5d 3b 0a 20 20  Type[j+nDel];.  
2ac83 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ac84 69 66 28 20 7a 54 79 70 65 5b 69 5d 3d 3d 27 5c  if( zType[i]=='\
2ac85 30 27 20 26 26 20 69 3e 30 20 29 7b 0a 20 20 20  0' && i>0 ){.   
2ac86 20 20 20 20 20 20 20 61 73 73 65 72 74 28 7a 54         assert(zT
2ac87 79 70 65 5b 69 2d 31 5d 3d 3d 27 20 27 29 3b 0a  ype[i-1]==' ');.
2ac88 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 5b            zType[
2ac89 69 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20  i-1] = '\0';.   
2ac8a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
2ac8b 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
2ac8c 69 73 48 69 64 64 65 6e 20 3d 20 31 3b 0a 20 20  isHidden = 1;.  
2ac8d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2ac8e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2ac8f 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2ac90 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62  ion is invoked b
2ac91 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20  y the parser to 
2ac92 63 61 6c 6c 20 74 68 65 20 78 43 6f 6e 6e 65 63  call the xConnec
2ac93 74 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66  t() method.** of
2ac94 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2ac95 6c 65 20 70 54 61 62 2e 20 49 66 20 61 6e 20 65  le pTab. If an e
2ac96 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20  rror occurs, an 
2ac97 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
2ac98 74 75 72 6e 65 64 20 0a 2a 2a 20 61 6e 64 20 61  turned .** and a
2ac99 6e 20 65 72 72 6f 72 20 6c 65 66 74 20 69 6e 20  n error left in 
2ac9a 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  pParse..**.** Th
2ac9b 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
2ac9c 6f 70 20 69 66 20 74 61 62 6c 65 20 70 54 61 62  op if table pTab
2ac9d 20 69 73 20 6e 6f 74 20 61 20 76 69 72 74 75 61   is not a virtua
2ac9e 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  l table..*/.SQLI
2ac9f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2aca0 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f  qlite3VtabCallCo
2aca1 6e 6e 65 63 74 28 50 61 72 73 65 20 2a 70 50 61  nnect(Parse *pPa
2aca2 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
2aca3 29 7b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f  ){.  Module *pMo
2aca4 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  d;.  int rc = SQ
2aca5 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20  LITE_OK;..  if( 
2aca6 21 70 54 61 62 20 7c 7c 20 28 70 54 61 62 2d 3e  !pTab || (pTab->
2aca7 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
2aca8 72 74 75 61 6c 29 3d 3d 30 20 7c 7c 20 70 54 61  rtual)==0 || pTa
2aca9 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20  b->pVtab ){.    
2acaa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2acab 3b 0a 20 20 7d 0a 0a 20 20 70 4d 6f 64 20 3d 20  ;.  }..  pMod = 
2acac 70 54 61 62 2d 3e 70 4d 6f 64 3b 0a 20 20 69 66  pTab->pMod;.  if
2acad 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20 20 63  ( !pMod ){.    c
2acae 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 75  onst char *zModu
2acaf 6c 65 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64  le = pTab->azMod
2acb0 75 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 20 20 73  uleArg[0];.    s
2acb1 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2acb2 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
2acb3 6d 6f 64 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f  module: %s", zMo
2acb4 64 75 6c 65 29 3b 0a 20 20 20 20 72 63 20 3d 20  dule);.    rc = 
2acb5 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2acb6 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 63 68 61  } else {.    cha
2acb7 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 20  r *zErr = 0;.   
2acb8 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2acb9 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 72  Parse->db;.    r
2acba 63 20 3d 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73  c = vtabCallCons
2acbb 74 72 75 63 74 6f 72 28 64 62 2c 20 70 54 61 62  tructor(db, pTab
2acbc 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d  , pMod, pMod->pM
2acbd 6f 64 75 6c 65 2d 3e 78 43 6f 6e 6e 65 63 74 2c  odule->xConnect,
2acbe 20 26 7a 45 72 72 29 3b 0a 20 20 20 20 69 66 28   &zErr);.    if(
2acbf 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2acc0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2acc1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2acc2 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 20  "%s", zErr);.   
2acc3 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
2acc4 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
2acc5 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
2acc6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74  ;.}../*.** Add t
2acc7 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2acc8 20 70 56 74 61 62 20 74 6f 20 74 68 65 20 61 72   pVtab to the ar
2acc9 72 61 79 20 73 71 6c 69 74 65 33 2e 61 56 54 72  ray sqlite3.aVTr
2acca 61 6e 73 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  ans[]..*/.static
2accb 20 69 6e 74 20 61 64 64 54 6f 56 54 72 61 6e 73   int addToVTrans
2accc 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73 71  (sqlite3 *db, sq
2accd 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
2acce 62 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  b){.  const int 
2accf 41 52 52 41 59 5f 49 4e 43 52 20 3d 20 35 3b 0a  ARRAY_INCR = 5;.
2acd0 0a 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20 73  .  /* Grow the s
2acd1 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
2acd2 72 72 61 79 20 69 66 20 72 65 71 75 69 72 65 64  rray if required
2acd3 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 6e   */.  if( (db->n
2acd4 56 54 72 61 6e 73 25 41 52 52 41 59 5f 49 4e 43  VTrans%ARRAY_INC
2acd5 52 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  R)==0 ){.    sql
2acd6 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 54 72  ite3_vtab **aVTr
2acd7 61 6e 73 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79  ans;.    int nBy
2acd8 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 73 71 6c  tes = sizeof(sql
2acd9 69 74 65 33 5f 76 74 61 62 20 2a 29 20 2a 20 28  ite3_vtab *) * (
2acda 64 62 2d 3e 6e 56 54 72 61 6e 73 20 2b 20 41 52  db->nVTrans + AR
2acdb 52 41 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20 61  RAY_INCR);.    a
2acdc 56 54 72 61 6e 73 20 3d 20 73 71 6c 69 74 65 33  VTrans = sqlite3
2acdd 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 28 76  DbRealloc(db, (v
2acde 6f 69 64 20 2a 29 64 62 2d 3e 61 56 54 72 61 6e  oid *)db->aVTran
2acdf 73 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  s, nBytes);.    
2ace0 69 66 28 20 21 61 56 54 72 61 6e 73 20 29 7b 0a  if( !aVTrans ){.
2ace1 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
2ace2 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
2ace3 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61 56 54  .    memset(&aVT
2ace4 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72 61 6e 73  rans[db->nVTrans
2ace5 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c  ], 0, sizeof(sql
2ace6 69 74 65 33 5f 76 74 61 62 20 2a 29 2a 41 52 52  ite3_vtab *)*ARR
2ace7 41 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20 64 62  AY_INCR);.    db
2ace8 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61 56 54 72  ->aVTrans = aVTr
2ace9 61 6e 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  ans;.  }..  /* A
2acea 64 64 20 70 56 74 61 62 20 74 6f 20 74 68 65 20  dd pVtab to the 
2aceb 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 61  end of sqlite3.a
2acec 56 54 72 61 6e 73 20 2a 2f 0a 20 20 64 62 2d 3e  VTrans */.  db->
2aced 61 56 54 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72  aVTrans[db->nVTr
2acee 61 6e 73 2b 2b 5d 20 3d 20 70 56 74 61 62 3b 0a  ans++] = pVtab;.
2acef 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63    sqlite3VtabLoc
2acf0 6b 28 70 56 74 61 62 29 3b 0a 20 20 72 65 74 75  k(pVtab);.  retu
2acf1 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2acf2 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
2acf3 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20  tion is invoked 
2acf4 62 79 20 74 68 65 20 76 64 62 65 20 74 6f 20 63  by the vdbe to c
2acf5 61 6c 6c 20 74 68 65 20 78 43 72 65 61 74 65 20  all the xCreate 
2acf6 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65  method.** of the
2acf7 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e   virtual table n
2acf8 61 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74  amed zTab in dat
2acf9 61 62 61 73 65 20 69 44 62 2e 20 0a 2a 2a 0a 2a  abase iDb. .**.*
2acfa 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
2acfb 63 75 72 73 2c 20 2a 70 7a 45 72 72 20 69 73 20  curs, *pzErr is 
2acfc 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 6e 20  set to point an 
2acfd 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75  an English langu
2acfe 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 69  age.** descripti
2acff 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  on of the error 
2ad00 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 58 58  and an SQLITE_XX
2ad01 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  X error code is 
2ad02 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 6e 20  returned..** In 
2ad03 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61  this case the ca
2ad04 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73  ller must call s
2ad05 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2ad06 20 29 20 6f 6e 20 2a 70 7a 45 72 72 2e 0a 2a 2f   ) on *pzErr..*/
2ad07 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ad08 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43  int sqlite3VtabC
2ad09 61 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65  allCreate(sqlite
2ad0a 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20  3 *db, int iDb, 
2ad0b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
2ad0c 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b  , char **pzErr){
2ad0d 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2ad0e 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a  TE_OK;.  Table *
2ad0f 70 54 61 62 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a  pTab;.  Module *
2ad10 70 4d 6f 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68  pMod;.  const ch
2ad11 61 72 20 2a 7a 4d 6f 64 75 6c 65 3b 0a 0a 20 20  ar *zModule;..  
2ad12 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
2ad13 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
2ad14 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
2ad15 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28  Name);.  assert(
2ad16 70 54 61 62 20 26 26 20 28 70 54 61 62 2d 3e 74  pTab && (pTab->t
2ad17 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72  abFlags & TF_Vir
2ad18 74 75 61 6c 29 21 3d 30 20 26 26 20 21 70 54 61  tual)!=0 && !pTa
2ad19 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20 70 4d 6f  b->pVtab);.  pMo
2ad1a 64 20 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 3b 0a  d = pTab->pMod;.
2ad1b 20 20 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62    zModule = pTab
2ad1c 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
2ad1d 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6d  ;..  /* If the m
2ad1e 6f 64 75 6c 65 20 68 61 73 20 62 65 65 6e 20 72  odule has been r
2ad1f 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 69 6e  egistered and in
2ad20 63 6c 75 64 65 73 20 61 20 43 72 65 61 74 65 20  cludes a Create 
2ad21 6d 65 74 68 6f 64 2c 20 0a 20 20 2a 2a 20 69 6e  method, .  ** in
2ad22 76 6f 6b 65 20 69 74 20 6e 6f 77 2e 20 49 66 20  voke it now. If 
2ad23 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 6e  the module has n
2ad24 6f 74 20 62 65 65 6e 20 72 65 67 69 73 74 65 72  ot been register
2ad25 65 64 2c 20 72 65 74 75 72 6e 20 61 6e 20 0a 20  ed, return an . 
2ad26 20 2a 2a 20 65 72 72 6f 72 2e 20 4f 74 68 65 72   ** error. Other
2ad27 77 69 73 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67  wise, do nothing
2ad28 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4d  ..  */.  if( !pM
2ad29 6f 64 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  od ){.    *pzErr
2ad2a 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2ad2b 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 6d  f(db, "no such m
2ad2c 6f 64 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64  odule: %s", zMod
2ad2d 75 6c 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ule);.    rc = S
2ad2e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2ad2f 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 76  else{.    rc = v
2ad30 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74  tabCallConstruct
2ad31 6f 72 28 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f  or(db, pTab, pMo
2ad32 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65  d, pMod->pModule
2ad33 2d 3e 78 43 72 65 61 74 65 2c 20 70 7a 45 72 72  ->xCreate, pzErr
2ad34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  );.  }..  if( rc
2ad35 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
2ad36 54 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20  Tab->pVtab ){.  
2ad37 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 56 54      rc = addToVT
2ad38 72 61 6e 73 28 64 62 2c 20 70 54 61 62 2d 3e 70  rans(db, pTab->p
2ad39 56 74 61 62 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  Vtab);.  }..  re
2ad3a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2ad3b 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2ad3c 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74  is used to set t
2ad3d 68 65 20 73 63 68 65 6d 61 20 6f 66 20 61 20 76  he schema of a v
2ad3e 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 49  irtual table.  I
2ad3f 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 76 61 6c  t is only.** val
2ad40 69 64 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  id to call this 
2ad41 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 20 77 69  function from wi
2ad42 74 68 69 6e 20 74 68 65 20 78 43 72 65 61 74 65  thin the xCreate
2ad43 28 29 20 6f 72 20 78 43 6f 6e 6e 65 63 74 28 29  () or xConnect()
2ad44 20 6f 66 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c   of a.** virtual
2ad45 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a   table module..*
2ad46 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ad47 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
2ad48 5f 76 74 61 62 28 73 71 6c 69 74 65 33 20 2a 64  _vtab(sqlite3 *d
2ad49 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2ad4a 43 72 65 61 74 65 54 61 62 6c 65 29 7b 0a 20 20  CreateTable){.  
2ad4b 50 61 72 73 65 20 73 50 61 72 73 65 3b 0a 0a 20  Parse sParse;.. 
2ad4c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2ad4d 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  _OK;.  Table *pT
2ad4e 61 62 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  ab;.  char *zErr
2ad4f 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33   = 0;..  sqlite3
2ad50 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
2ad51 3e 6d 75 74 65 78 29 3b 0a 20 20 70 54 61 62 20  >mutex);.  pTab 
2ad52 3d 20 64 62 2d 3e 70 56 54 61 62 3b 0a 20 20 69  = db->pVTab;.  i
2ad53 66 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20  f( !pTab ){.    
2ad54 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2ad55 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20   SQLITE_MISUSE, 
2ad56 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
2ad57 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2ad58 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75  mutex);.    retu
2ad59 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
2ad5a 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 28  ;.  }.  assert((
2ad5b 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
2ad5c 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 20   TF_Virtual)!=0 
2ad5d 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d 3d 30  && pTab->nCol==0
2ad5e 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 3d 3d   && pTab->aCol==
2ad5f 30 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73  0);..  memset(&s
2ad60 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66  Parse, 0, sizeof
2ad61 28 50 61 72 73 65 29 29 3b 0a 20 20 73 50 61 72  (Parse));.  sPar
2ad62 73 65 2e 64 65 63 6c 61 72 65 56 74 61 62 20 3d  se.declareVtab =
2ad63 20 31 3b 0a 20 20 73 50 61 72 73 65 2e 64 62 20   1;.  sParse.db 
2ad64 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20 0a 20 20  = db;..  if( .  
2ad65 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 20 3d 3d      SQLITE_OK ==
2ad66 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65   sqlite3RunParse
2ad67 72 28 26 73 50 61 72 73 65 2c 20 7a 43 72 65 61  r(&sParse, zCrea
2ad68 74 65 54 61 62 6c 65 2c 20 26 7a 45 72 72 29 20  teTable, &zErr) 
2ad69 26 26 20 0a 20 20 20 20 20 20 73 50 61 72 73 65  && .      sParse
2ad6a 2e 70 4e 65 77 54 61 62 6c 65 20 26 26 20 0a 20  .pNewTable && . 
2ad6b 20 20 20 20 20 21 73 50 61 72 73 65 2e 70 4e 65       !sParse.pNe
2ad6c 77 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20  wTable->pSelect 
2ad6d 26 26 20 0a 20 20 20 20 20 20 28 73 50 61 72 73  && .      (sPars
2ad6e 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 74 61 62  e.pNewTable->tab
2ad6f 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75  Flags & TF_Virtu
2ad70 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a 20 20 20 20  al)==0.  ){.    
2ad71 70 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 73 50 61  pTab->aCol = sPa
2ad72 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 61  rse.pNewTable->a
2ad73 43 6f 6c 3b 0a 20 20 20 20 70 54 61 62 2d 3e 6e  Col;.    pTab->n
2ad74 43 6f 6c 20 3d 20 73 50 61 72 73 65 2e 70 4e 65  Col = sParse.pNe
2ad75 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20 20  wTable->nCol;.  
2ad76 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62    sParse.pNewTab
2ad77 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->nCol = 0;.  
2ad78 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62    sParse.pNewTab
2ad79 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->aCol = 0;.  
2ad7a 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b    db->pVTab = 0;
2ad7b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
2ad7c 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2ad7d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 7a   SQLITE_ERROR, z
2ad7e 45 72 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Err);.    sqlite
2ad7f 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
2ad80 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
2ad81 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
2ad82 73 50 61 72 73 65 2e 64 65 63 6c 61 72 65 56 74  sParse.declareVt
2ad83 61 62 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 73  ab = 0;..  if( s
2ad84 50 61 72 73 65 2e 70 56 64 62 65 20 29 7b 0a 20  Parse.pVdbe ){. 
2ad85 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69     sqlite3VdbeFi
2ad86 6e 61 6c 69 7a 65 28 73 50 61 72 73 65 2e 70 56  nalize(sParse.pV
2ad87 64 62 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  dbe);.  }.  sqli
2ad88 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 73  te3DeleteTable(s
2ad89 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 29  Parse.pNewTable)
2ad8a 3b 0a 20 20 73 50 61 72 73 65 2e 70 4e 65 77 54  ;.  sParse.pNewT
2ad8b 61 62 6c 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73  able = 0;..  ass
2ad8c 65 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d  ert( (rc&0xff)==
2ad8d 72 63 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  rc );.  rc = sql
2ad8e 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
2ad8f 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
2ad90 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
2ad91 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
2ad92 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
2ad93 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  s function is in
2ad94 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64 62  voked by the vdb
2ad95 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 44  e to call the xD
2ad96 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a  estroy method.**
2ad97 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
2ad98 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62  table named zTab
2ad99 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
2ad9a 2e 20 54 68 69 73 20 6f 63 63 75 72 73 0a 2a 2a  . This occurs.**
2ad9b 20 77 68 65 6e 20 61 20 44 52 4f 50 20 54 41 42   when a DROP TAB
2ad9c 4c 45 20 69 73 20 6d 65 6e 74 69 6f 6e 65 64 2e  LE is mentioned.
2ad9d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c  .**.** This call
2ad9e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 7a   is a no-op if z
2ad9f 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76 69 72  Tab is not a vir
2ada0 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  tual table..*/.S
2ada1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2ada2 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  t sqlite3VtabCal
2ada3 6c 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  lDestroy(sqlite3
2ada4 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63   *db, int iDb, c
2ada5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 29  onst char *zTab)
2ada6 0a 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  .{.  int rc = SQ
2ada7 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65  LITE_OK;.  Table
2ada8 20 2a 70 54 61 62 3b 0a 0a 20 20 70 54 61 62 20   *pTab;..  pTab 
2ada9 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
2adaa 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d  le(db, zTab, db-
2adab 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
2adac 3b 0a 20 20 61 73 73 65 72 74 28 70 54 61 62 29  ;.  assert(pTab)
2adad 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 56  ;.  if( pTab->pV
2adae 74 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 28  tab ){.    int (
2adaf 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74  *xDestroy)(sqlit
2adb0 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 20  e3_vtab *pVTab) 
2adb1 3d 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d  = pTab->pMod->pM
2adb2 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79 3b  odule->xDestroy;
2adb3 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
2adb4 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
2adb5 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
2adb6 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
2adb7 20 69 66 28 20 78 44 65 73 74 72 6f 79 20 29 7b   if( xDestroy ){
2adb8 0a 20 20 20 20 20 20 72 63 20 3d 20 78 44 65 73  .      rc = xDes
2adb9 74 72 6f 79 28 70 54 61 62 2d 3e 70 56 74 61 62  troy(pTab->pVtab
2adba 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 28 76 6f  );.    }.    (vo
2adbb 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2adbc 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20  On(db);.    if( 
2adbd 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2adbe 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
2adbf 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
2adc0 62 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29  b->nVTrans; i++)
2adc1 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62  {.        if( db
2adc2 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3d 3d 70 54  ->aVTrans[i]==pT
2adc3 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20 20  ab->pVtab ){.   
2adc4 20 20 20 20 20 20 20 64 62 2d 3e 61 56 54 72 61         db->aVTra
2adc5 6e 73 5b 69 5d 20 3d 20 64 62 2d 3e 61 56 54 72  ns[i] = db->aVTr
2adc6 61 6e 73 5b 2d 2d 64 62 2d 3e 6e 56 54 72 61 6e  ans[--db->nVTran
2adc7 73 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  s];.          br
2adc8 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
2adc9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 54 61       }.      pTa
2adca 62 2d 3e 70 56 74 61 62 20 3d 20 30 3b 0a 20 20  b->pVtab = 0;.  
2adcb 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
2adcc 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
2adcd 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76  his function inv
2adce 6f 6b 65 73 20 65 69 74 68 65 72 20 74 68 65 20  okes either the 
2adcf 78 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 78 43 6f  xRollback or xCo
2add0 6d 6d 69 74 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f  mmit method.** o
2add1 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 76 69  f each of the vi
2add2 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20  rtual tables in 
2add3 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72  the sqlite3.aVTr
2add4 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 20 6d  ans array. The m
2add5 65 74 68 6f 64 0a 2a 2a 20 63 61 6c 6c 65 64 20  ethod.** called 
2add6 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  is identified by
2add7 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
2add8 6d 65 6e 74 2c 20 22 6f 66 66 73 65 74 22 2c 20  ment, "offset", 
2add9 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20  which is.** the 
2adda 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6d 65  offset of the me
2addb 74 68 6f 64 20 74 6f 20 63 61 6c 6c 20 69 6e 20  thod to call in 
2addc 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  the sqlite3_modu
2addd 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  le structure..**
2adde 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20 69 73  .** The array is
2addf 20 63 6c 65 61 72 65 64 20 61 66 74 65 72 20 69   cleared after i
2ade0 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c  nvoking the call
2ade1 62 61 63 6b 73 2e 20 0a 2a 2f 0a 73 74 61 74 69  backs. .*/.stati
2ade2 63 20 76 6f 69 64 20 63 61 6c 6c 46 69 6e 61 6c  c void callFinal
2ade3 69 73 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62  iser(sqlite3 *db
2ade4 2c 20 69 6e 74 20 6f 66 66 73 65 74 29 7b 0a 20  , int offset){. 
2ade5 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 64 62   int i;.  if( db
2ade6 2d 3e 61 56 54 72 61 6e 73 20 29 7b 0a 20 20 20  ->aVTrans ){.   
2ade7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
2ade8 6e 56 54 72 61 6e 73 20 26 26 20 64 62 2d 3e 61  nVTrans && db->a
2ade9 56 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b  VTrans[i]; i++){
2adea 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76  .      sqlite3_v
2adeb 74 61 62 20 2a 70 56 74 61 62 20 3d 20 64 62 2d  tab *pVtab = db-
2adec 3e 61 56 54 72 61 6e 73 5b 69 5d 3b 0a 20 20 20  >aVTrans[i];.   
2aded 20 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c 69     int (*x)(sqli
2adee 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20  te3_vtab *);.   
2adef 20 20 20 78 20 3d 20 2a 28 69 6e 74 20 28 2a 2a     x = *(int (**
2adf0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
2adf1 29 29 28 28 63 68 61 72 20 2a 29 70 56 74 61 62  ))((char *)pVtab
2adf2 2d 3e 70 4d 6f 64 75 6c 65 20 2b 20 6f 66 66 73  ->pModule + offs
2adf3 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 78  et);.      if( x
2adf4 20 29 20 78 28 70 56 74 61 62 29 3b 0a 20 20 20   ) x(pVtab);.   
2adf5 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e     sqlite3VtabUn
2adf6 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b  lock(db, pVtab);
2adf7 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2adf8 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d  e3DbFree(db, db-
2adf9 3e 61 56 54 72 61 6e 73 29 3b 0a 20 20 20 20 64  >aVTrans);.    d
2adfa 62 2d 3e 6e 56 54 72 61 6e 73 20 3d 20 30 3b 0a  b->nVTrans = 0;.
2adfb 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20      db->aVTrans 
2adfc 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
2adfd 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 53 79  * Invoke the xSy
2adfe 6e 63 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c  nc method of all
2adff 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
2ae00 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61  in the sqlite3.a
2ae01 56 54 72 61 6e 73 0a 2a 2a 20 61 72 72 61 79 2e  VTrans.** array.
2ae02 20 52 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   Return the erro
2ae03 72 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 66  r code for the f
2ae04 69 72 73 74 20 65 72 72 6f 72 20 74 68 61 74 20  irst error that 
2ae05 6f 63 63 75 72 73 2c 20 6f 72 0a 2a 2a 20 53 51  occurs, or.** SQ
2ae06 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 20 78  LITE_OK if all x
2ae07 53 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e 73 20  Sync operations 
2ae08 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 2e 0a  are successful..
2ae09 2a 2a 0a 2a 2a 20 53 65 74 20 2a 70 7a 45 72 72  **.** Set *pzErr
2ae0a 6d 73 67 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  msg to point to 
2ae0b 61 20 62 75 66 66 65 72 20 74 68 61 74 20 73 68  a buffer that sh
2ae0c 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 73 65 64  ould be released
2ae0d 20 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74   using .** sqlit
2ae0e 65 33 44 62 46 72 65 65 28 29 20 63 6f 6e 74 61  e3DbFree() conta
2ae0f 69 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d  ining an error m
2ae10 65 73 73 61 67 65 2c 20 69 66 20 6f 6e 65 20 69  essage, if one i
2ae11 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a  s available..*/.
2ae12 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2ae13 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79  nt sqlite3VtabSy
2ae14 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  nc(sqlite3 *db, 
2ae15 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d 73 67 29  char **pzErrmsg)
2ae16 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
2ae17 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
2ae18 0a 20 20 69 6e 74 20 72 63 73 61 66 65 74 79 3b  .  int rcsafety;
2ae19 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
2ae1a 2a 2a 61 56 54 72 61 6e 73 20 3d 20 64 62 2d 3e  **aVTrans = db->
2ae1b 61 56 54 72 61 6e 73 3b 0a 0a 20 20 72 63 20 3d  aVTrans;..  rc =
2ae1c 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
2ae1d 66 28 64 62 29 3b 0a 20 20 64 62 2d 3e 61 56 54  f(db);.  db->aVT
2ae1e 72 61 6e 73 20 3d 20 30 3b 0a 20 20 66 6f 72 28  rans = 0;.  for(
2ae1f 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
2ae20 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 56 54 72  OK && i<db->nVTr
2ae21 61 6e 73 20 26 26 20 61 56 54 72 61 6e 73 5b 69  ans && aVTrans[i
2ae22 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  ]; i++){.    sql
2ae23 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
2ae24 20 3d 20 61 56 54 72 61 6e 73 5b 69 5d 3b 0a 20   = aVTrans[i];. 
2ae25 20 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c 69     int (*x)(sqli
2ae26 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20  te3_vtab *);.   
2ae27 20 78 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64   x = pVtab->pMod
2ae28 75 6c 65 2d 3e 78 53 79 6e 63 3b 0a 20 20 20 20  ule->xSync;.    
2ae29 69 66 28 20 78 20 29 7b 0a 20 20 20 20 20 20 72  if( x ){.      r
2ae2a 63 20 3d 20 78 28 70 56 74 61 62 29 3b 0a 20 20  c = x(pVtab);.  
2ae2b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2ae2c 65 28 64 62 2c 20 2a 70 7a 45 72 72 6d 73 67 29  e(db, *pzErrmsg)
2ae2d 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 6d 73  ;.      *pzErrms
2ae2e 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  g = pVtab->zErrM
2ae2f 73 67 3b 0a 20 20 20 20 20 20 70 56 74 61 62 2d  sg;.      pVtab-
2ae30 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
2ae31 20 20 7d 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 56    }.  }.  db->aV
2ae32 54 72 61 6e 73 20 3d 20 61 56 54 72 61 6e 73 3b  Trans = aVTrans;
2ae33 0a 20 20 72 63 73 61 66 65 74 79 20 3d 20 73 71  .  rcsafety = sq
2ae34 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
2ae35 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  );..  if( rc==SQ
2ae36 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
2ae37 63 20 3d 20 72 63 73 61 66 65 74 79 3b 0a 20 20  c = rcsafety;.  
2ae38 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
2ae39 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74  ../*.** Invoke t
2ae3a 68 65 20 78 52 6f 6c 6c 62 61 63 6b 20 6d 65 74  he xRollback met
2ae3b 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75  hod of all virtu
2ae3c 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  al tables in the
2ae3d 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 61 56 54   .** sqlite3.aVT
2ae3e 72 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 6e  rans array. Then
2ae3f 20 63 6c 65 61 72 20 74 68 65 20 61 72 72 61 79   clear the array
2ae40 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49   itself..*/.SQLI
2ae41 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2ae42 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
2ae43 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ck(sqlite3 *db){
2ae44 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72  .  callFinaliser
2ae45 28 64 62 2c 20 6f 66 66 73 65 74 6f 66 28 73 71  (db, offsetof(sq
2ae46 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78 52 6f  lite3_module,xRo
2ae47 6c 6c 62 61 63 6b 29 29 3b 0a 20 20 72 65 74 75  llback));.  retu
2ae48 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2ae49 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
2ae4a 65 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68 6f 64  e xCommit method
2ae4b 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20   of all virtual 
2ae4c 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a  tables in the .*
2ae4d 2a 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  * sqlite3.aVTran
2ae4e 73 20 61 72 72 61 79 2e 20 54 68 65 6e 20 63 6c  s array. Then cl
2ae4f 65 61 72 20 74 68 65 20 61 72 72 61 79 20 69 74  ear the array it
2ae50 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  self..*/.SQLITE_
2ae51 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2ae52 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 73 71  te3VtabCommit(sq
2ae53 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 61  lite3 *db){.  ca
2ae54 6c 6c 46 69 6e 61 6c 69 73 65 72 28 64 62 2c 20  llFinaliser(db, 
2ae55 6f 66 66 73 65 74 6f 66 28 73 71 6c 69 74 65 33  offsetof(sqlite3
2ae56 5f 6d 6f 64 75 6c 65 2c 78 43 6f 6d 6d 69 74 29  _module,xCommit)
2ae57 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
2ae58 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
2ae59 49 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  If the virtual t
2ae5a 61 62 6c 65 20 70 56 74 61 62 20 73 75 70 70 6f  able pVtab suppo
2ae5b 72 74 73 20 74 68 65 20 74 72 61 6e 73 61 63 74  rts the transact
2ae5c 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  ion interface.**
2ae5d 20 28 78 42 65 67 69 6e 2f 78 52 6f 6c 6c 62 61   (xBegin/xRollba
2ae5e 63 6b 2f 78 43 6f 6d 6d 69 74 20 61 6e 64 20 6f  ck/xCommit and o
2ae5f 70 74 69 6f 6e 61 6c 6c 79 20 78 53 79 6e 63 29  ptionally xSync)
2ae60 20 61 6e 64 20 61 20 74 72 61 6e 73 61 63 74 69   and a transacti
2ae61 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 75 72  on is.** not cur
2ae62 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 69 6e 76  rently open, inv
2ae63 6f 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d  oke the xBegin m
2ae64 65 74 68 6f 64 20 6e 6f 77 2e 0a 2a 2a 0a 2a 2a  ethod now..**.**
2ae65 20 49 66 20 74 68 65 20 78 42 65 67 69 6e 20 63   If the xBegin c
2ae66 61 6c 6c 20 69 73 20 73 75 63 63 65 73 73 66 75  all is successfu
2ae67 6c 2c 20 70 6c 61 63 65 20 74 68 65 20 73 71 6c  l, place the sql
2ae68 69 74 65 33 5f 76 74 61 62 20 70 6f 69 6e 74 65  ite3_vtab pointe
2ae69 72 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69  r.** in the sqli
2ae6a 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
2ae6b 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
2ae6c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2ae6d 56 74 61 62 42 65 67 69 6e 28 73 71 6c 69 74 65  VtabBegin(sqlite
2ae6e 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f 76  3 *db, sqlite3_v
2ae6f 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 69  tab *pVtab){.  i
2ae70 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
2ae71 4b 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  K;.  const sqlit
2ae72 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
2ae73 6c 65 3b 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61  le;..  /* Specia
2ae74 6c 20 63 61 73 65 3a 20 49 66 20 64 62 2d 3e 61  l case: If db->a
2ae75 56 54 72 61 6e 73 20 69 73 20 4e 55 4c 4c 20 61  VTrans is NULL a
2ae76 6e 64 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20 69  nd db->nVTrans i
2ae77 73 20 67 72 65 61 74 65 72 0a 20 20 2a 2a 20 74  s greater.  ** t
2ae78 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
2ae79 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2ae7a 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f  being called fro
2ae7b 6d 20 77 69 74 68 69 6e 20 61 0a 20 20 2a 2a 20  m within a.  ** 
2ae7c 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 78  virtual module x
2ae7d 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 2e  Sync() callback.
2ae7e 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
2ae7f 6f 20 77 72 69 74 65 20 74 6f 20 0a 20 20 2a 2a  o write to .  **
2ae80 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20   virtual module 
2ae81 74 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 63  tables in this c
2ae82 61 73 65 2c 20 73 6f 20 72 65 74 75 72 6e 20 53  ase, so return S
2ae83 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 20 20  QLITE_MISUSE..  
2ae84 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
2ae85 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 29  VtabInSync(db) )
2ae86 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2ae87 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a  ITE_LOCKED;.  }.
2ae88 20 20 69 66 28 20 21 70 56 74 61 62 20 29 7b 0a    if( !pVtab ){.
2ae89 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2ae8a 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20 70 4d 6f  E_OK;.  } .  pMo
2ae8b 64 75 6c 65 20 3d 20 70 56 74 61 62 2d 3e 70 4d  dule = pVtab->pM
2ae8c 6f 64 75 6c 65 3b 0a 0a 20 20 69 66 28 20 70 4d  odule;..  if( pM
2ae8d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 20 29 7b  odule->xBegin ){
2ae8e 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 0a 20 20  .    int i;...  
2ae8f 20 20 2f 2a 20 49 66 20 70 56 74 61 62 20 69 73    /* If pVtab is
2ae90 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
2ae91 61 56 54 72 61 6e 73 20 61 72 72 61 79 2c 20 72  aVTrans array, r
2ae92 65 74 75 72 6e 20 65 61 72 6c 79 20 2a 2f 0a 20  eturn early */. 
2ae93 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 69 3c 64     for(i=0; (i<d
2ae94 62 2d 3e 6e 56 54 72 61 6e 73 29 20 26 26 20 30  b->nVTrans) && 0
2ae95 21 3d 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  !=db->aVTrans[i]
2ae96 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
2ae97 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  ( db->aVTrans[i]
2ae98 3d 3d 70 56 74 61 62 20 29 7b 0a 20 20 20 20 20  ==pVtab ){.     
2ae99 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2ae9a 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _OK;.      }.   
2ae9b 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b   }..    /* Invok
2ae9c 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65 74  e the xBegin met
2ae9d 68 6f 64 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  hod */.    rc = 
2ae9e 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 28  pModule->xBegin(
2ae9f 70 56 74 61 62 29 3b 0a 20 20 20 20 69 66 28 20  pVtab);.    if( 
2aea0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2aea1 0a 20 20 20 20 20 20 72 63 20 3d 20 61 64 64 54  .      rc = addT
2aea2 6f 56 54 72 61 6e 73 28 64 62 2c 20 70 56 74 61  oVTrans(db, pVta
2aea3 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  b);.    }.  }.  
2aea4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2aea5 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
2aea6 72 61 6d 65 74 65 72 20 28 70 44 65 66 29 20 69  rameter (pDef) i
2aea7 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  s a function imp
2aea8 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
2aea9 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61  e.** second para
2aeaa 6d 65 74 65 72 20 28 70 45 78 70 72 29 20 69 73  meter (pExpr) is
2aeab 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
2aeac 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
2aead 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20 70 45 78 70  tion..** If pExp
2aeae 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  r is a column in
2aeaf 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2aeb0 2c 20 74 68 65 6e 20 6c 65 74 20 74 68 65 20 76  , then let the v
2aeb1 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20  irtual.** table 
2aeb2 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68  implementation h
2aeb3 61 76 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69  ave an opportuni
2aeb4 74 79 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74  ty to overload t
2aeb5 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  he function..**.
2aeb6 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2aeb7 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 77  is used to allow
2aeb8 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
2aeb9 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 74  mplementations t
2aeba 6f 0a 2a 2a 20 6f 76 65 72 6c 6f 61 64 20 4d 41  o.** overload MA
2aebb 54 43 48 2c 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c  TCH, LIKE, GLOB,
2aebc 20 61 6e 64 20 52 45 47 45 58 50 20 6f 70 65 72   and REGEXP oper
2aebd 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  ators..**.** Ret
2aebe 75 72 6e 20 65 69 74 68 65 72 20 74 68 65 20 70  urn either the p
2aebf 44 65 66 20 61 72 67 75 6d 65 6e 74 20 28 69 6e  Def argument (in
2aec0 64 69 63 61 74 69 6e 67 20 6e 6f 20 63 68 61 6e  dicating no chan
2aec1 67 65 29 20 6f 72 20 61 20 0a 2a 2a 20 6e 65 77  ge) or a .** new
2aec2 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
2aec3 72 65 20 74 68 61 74 20 69 73 20 6d 61 72 6b 65  re that is marke
2aec4 64 20 61 73 20 65 70 68 65 6d 65 72 61 6c 20 75  d as ephemeral u
2aec5 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49  sing the.** SQLI
2aec6 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 66 6c  TE_FUNC_EPHEM fl
2aec7 61 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ag..*/.SQLITE_PR
2aec8 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73  IVATE FuncDef *s
2aec9 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
2aeca 61 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71  adFunction(.  sq
2aecb 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 2f 2a  lite3 *db,    /*
2aecc 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2aecd 74 69 6f 6e 20 66 6f 72 20 72 65 70 6f 72 74 69  tion for reporti
2aece 6e 67 20 6d 61 6c 6c 6f 63 20 70 72 6f 62 6c 65  ng malloc proble
2aecf 6d 73 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20  ms */.  FuncDef 
2aed0 2a 70 44 65 66 2c 20 20 2f 2a 20 46 75 6e 63 74  *pDef,  /* Funct
2aed1 69 6f 6e 20 74 6f 20 70 6f 73 73 69 62 6c 79 20  ion to possibly 
2aed2 6f 76 65 72 6c 6f 61 64 20 2a 2f 0a 20 20 69 6e  overload */.  in
2aed3 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 2f 2a  t nArg,       /*
2aed4 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
2aed5 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63  ents to the func
2aed6 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  tion */.  Expr *
2aed7 70 45 78 70 72 20 20 20 20 20 2f 2a 20 46 69 72  pExpr     /* Fir
2aed8 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
2aed9 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  he function */.)
2aeda 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  {.  Table *pTab;
2aedb 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
2aedc 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65  *pVtab;.  sqlite
2aedd 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a  3_module *pMod;.
2aede 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
2aedf 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2aee0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
2aee1 75 65 2a 2a 29 20 3d 20 30 3b 0a 20 20 76 6f 69  ue**) = 0;.  voi
2aee2 64 20 2a 70 41 72 67 20 3d 20 30 3b 0a 20 20 46  d *pArg = 0;.  F
2aee3 75 6e 63 44 65 66 20 2a 70 4e 65 77 3b 0a 20 20  uncDef *pNew;.  
2aee4 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 68  int rc = 0;.  ch
2aee5 61 72 20 2a 7a 4c 6f 77 65 72 4e 61 6d 65 3b 0a  ar *zLowerName;.
2aee6 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
2aee7 2a 7a 3b 0a 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  *z;...  /* Check
2aee8 20 74 6f 20 73 65 65 20 74 68 65 20 6c 65 66 74   to see the left
2aee9 20 6f 70 65 72 61 6e 64 20 69 73 20 61 20 63 6f   operand is a co
2aeea 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61  lumn in a virtua
2aeeb 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28  l table */.  if(
2aeec 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75   pExpr==0 ) retu
2aeed 72 6e 20 70 44 65 66 3b 0a 20 20 69 66 28 20 70  rn pDef;.  if( p
2aeee 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  Expr->op!=TK_COL
2aeef 55 4d 4e 20 29 20 72 65 74 75 72 6e 20 70 44 65  UMN ) return pDe
2aef0 66 3b 0a 20 20 70 54 61 62 20 3d 20 70 45 78 70  f;.  pTab = pExp
2aef1 72 2d 3e 70 54 61 62 3b 0a 20 20 69 66 28 20 70  r->pTab;.  if( p
2aef2 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Tab==0 ) return 
2aef3 70 44 65 66 3b 0a 20 20 69 66 28 20 28 70 54 61  pDef;.  if( (pTa
2aef4 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
2aef5 5f 56 69 72 74 75 61 6c 29 3d 3d 30 20 29 20 72  _Virtual)==0 ) r
2aef6 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70 56  eturn pDef;.  pV
2aef7 74 61 62 20 3d 20 70 54 61 62 2d 3e 70 56 74 61  tab = pTab->pVta
2aef8 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 74  b;.  assert( pVt
2aef9 61 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ab!=0 );.  asser
2aefa 74 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  t( pVtab->pModul
2aefb 65 21 3d 30 20 29 3b 0a 20 20 70 4d 6f 64 20 3d  e!=0 );.  pMod =
2aefc 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65   (sqlite3_module
2aefd 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   *)pVtab->pModul
2aefe 65 3b 0a 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78  e;.  if( pMod->x
2aeff 46 69 6e 64 46 75 6e 63 74 69 6f 6e 3d 3d 30 20  FindFunction==0 
2af00 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20  ) return pDef;. 
2af01 0a 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 78  .  /* Call the x
2af02 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74  FindFunction met
2af03 68 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75  hod on the virtu
2af04 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  al table impleme
2af05 6e 74 61 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20  ntation.  ** to 
2af06 73 65 65 20 69 66 20 74 68 65 20 69 6d 70 6c 65  see if the imple
2af07 6d 65 6e 74 61 74 69 6f 6e 20 77 61 6e 74 73 20  mentation wants 
2af08 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 69 73  to overload this
2af09 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2f 0a   function .  */.
2af0a 20 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 3d 20 73    zLowerName = s
2af0b 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
2af0c 62 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b  b, pDef->zName);
2af0d 0a 20 20 69 66 28 20 7a 4c 6f 77 65 72 4e 61 6d  .  if( zLowerNam
2af0e 65 20 29 7b 0a 20 20 20 20 66 6f 72 28 7a 3d 28  e ){.    for(z=(
2af0f 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a  unsigned char*)z
2af10 4c 6f 77 65 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a  LowerName; *z; z
2af11 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 7a 20 3d 20  ++){.      *z = 
2af12 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
2af13 77 65 72 5b 2a 7a 5d 3b 0a 20 20 20 20 7d 0a 20  wer[*z];.    }. 
2af14 20 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 46     rc = pMod->xF
2af15 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 56 74 61  indFunction(pVta
2af16 62 2c 20 6e 41 72 67 2c 20 7a 4c 6f 77 65 72 4e  b, nArg, zLowerN
2af17 61 6d 65 2c 20 26 78 46 75 6e 63 2c 20 26 70 41  ame, &xFunc, &pA
2af18 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rg);.    sqlite3
2af19 44 62 46 72 65 65 28 64 62 2c 20 7a 4c 6f 77 65  DbFree(db, zLowe
2af1a 72 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  rName);.    if( 
2af1b 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 29  pVtab->zErrMsg )
2af1c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2af1d 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 25 73  rror(db, rc, "%s
2af1e 22 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  ", pVtab->zErrMs
2af1f 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
2af20 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 74 61  3DbFree(db, pVta
2af21 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  b->zErrMsg);.   
2af22 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73     pVtab->zErrMs
2af23 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  g = 0;.    }.  }
2af24 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a  .  if( rc==0 ){.
2af25 20 20 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b      return pDef;
2af26 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74  .  }..  /* Creat
2af27 65 20 61 20 6e 65 77 20 65 70 68 65 6d 65 72 61  e a new ephemera
2af28 6c 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  l function defin
2af29 69 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6f 76  ition for the ov
2af2a 65 72 6c 6f 61 64 65 64 0a 20 20 2a 2a 20 66 75  erloaded.  ** fu
2af2b 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e 65 77  nction */.  pNew
2af2c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2af2d 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
2af2e 66 28 2a 70 4e 65 77 29 0a 20 20 20 20 20 20 20  f(*pNew).       
2af2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af30 20 20 20 20 20 20 2b 20 73 71 6c 69 74 65 33 53        + sqlite3S
2af31 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a 4e  trlen30(pDef->zN
2af32 61 6d 65 29 20 29 3b 0a 20 20 69 66 28 20 70 4e  ame) );.  if( pN
2af33 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ew==0 ){.    ret
2af34 75 72 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 20 20  urn pDef;.  }.  
2af35 2a 70 4e 65 77 20 3d 20 2a 70 44 65 66 3b 0a 20  *pNew = *pDef;. 
2af36 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28   pNew->zName = (
2af37 63 68 61 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b  char *)&pNew[1];
2af38 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e  .  memcpy(pNew->
2af39 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e 7a 4e 61  zName, pDef->zNa
2af3a 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  me, sqlite3Strle
2af3b 6e 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29  n30(pDef->zName)
2af3c 2b 31 29 3b 0a 20 20 70 4e 65 77 2d 3e 78 46 75  +1);.  pNew->xFu
2af3d 6e 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 4e  nc = xFunc;.  pN
2af3e 65 77 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20  ew->pUserData = 
2af3f 70 41 72 67 3b 0a 20 20 70 4e 65 77 2d 3e 66 6c  pArg;.  pNew->fl
2af40 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 46 55  ags |= SQLITE_FU
2af41 4e 43 5f 45 50 48 45 4d 3b 0a 20 20 72 65 74 75  NC_EPHEM;.  retu
2af42 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
2af43 2a 20 4d 61 6b 65 20 73 75 72 65 20 76 69 72 74  * Make sure virt
2af44 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62 20 69  ual table pTab i
2af45 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  s contained in t
2af46 68 65 20 70 50 61 72 73 65 2d 3e 61 70 56 69 72  he pParse->apVir
2af47 74 75 61 6c 4c 6f 63 6b 5b 5d 0a 2a 2a 20 61 72  tualLock[].** ar
2af48 72 61 79 20 73 6f 20 74 68 61 74 20 61 6e 20 4f  ray so that an O
2af49 50 5f 56 42 65 67 69 6e 20 77 69 6c 6c 20 67 65  P_VBegin will ge
2af4a 74 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  t generated for 
2af4b 69 74 2e 20 20 41 64 64 20 70 54 61 62 20 74 6f  it.  Add pTab to
2af4c 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 69 66   the.** array if
2af4d 20 69 74 20 69 73 20 6d 69 73 73 69 6e 67 2e 20   it is missing. 
2af4e 20 49 66 20 70 54 61 62 20 69 73 20 61 6c 72 65   If pTab is alre
2af4f 61 64 79 20 69 6e 20 74 68 65 20 61 72 72 61 79  ady in the array
2af50 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  , this routine.*
2af51 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  * is a no-op..*/
2af52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2af53 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
2af54 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72  MakeWritable(Par
2af55 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
2af56 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20  e *pTab){.  int 
2af57 69 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  i, n;.  assert( 
2af58 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
2af59 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2af5a 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63  pParse->nVtabLoc
2af5b 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  k; i++){.    if(
2af5c 20 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 61   pTab==pParse->a
2af5d 70 56 74 61 62 4c 6f 63 6b 5b 69 5d 20 29 20 72  pVtabLock[i] ) r
2af5e 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 20 3d  eturn;.  }.  n =
2af5f 20 28 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c   (pParse->nVtabL
2af60 6f 63 6b 2b 31 29 2a 73 69 7a 65 6f 66 28 70 50  ock+1)*sizeof(pP
2af61 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  arse->apVtabLock
2af62 5b 30 5d 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  [0]);.  pParse->
2af63 61 70 56 74 61 62 4c 6f 63 6b 20 3d 20 73 71 6c  apVtabLock = sql
2af64 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 50 61  ite3_realloc(pPa
2af65 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 2c  rse->apVtabLock,
2af66 20 6e 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73   n);.  if( pPars
2af67 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 20 29 7b  e->apVtabLock ){
2af68 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56  .    pParse->apV
2af69 74 61 62 4c 6f 63 6b 5b 70 50 61 72 73 65 2d 3e  tabLock[pParse->
2af6a 6e 56 74 61 62 4c 6f 63 6b 2b 2b 5d 20 3d 20 70  nVtabLock++] = p
2af6b 54 61 62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Tab;.  }else{.  
2af6c 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61    pParse->db->ma
2af6d 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
2af6e 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
2af6f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2af70 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
2af71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2af72 64 20 6f 66 20 76 74 61 62 2e 63 20 2a 2a 2a 2a  d of vtab.c ****
2af73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2af74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2af75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2af76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2af77 67 69 6e 20 66 69 6c 65 20 77 68 65 72 65 2e 63  gin file where.c
2af78 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2af79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2af7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2af7b 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
2af7c 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
2af7d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2af7e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2af7f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2af80 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2af81 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2af82 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2af83 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2af84 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2af85 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2af86 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2af87 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2af88 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2af89 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2af8a 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2af8b 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2af8c 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2af8d 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2af8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2af8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2af90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2af91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2af92 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64  ****.** This mod
2af93 75 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ule contains C c
2af94 6f 64 65 20 74 68 61 74 20 67 65 6e 65 72 61 74  ode that generat
2af95 65 73 20 56 44 42 45 20 63 6f 64 65 20 75 73 65  es VDBE code use
2af96 64 20 74 6f 20 70 72 6f 63 65 73 73 0a 2a 2a 20  d to process.** 
2af97 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2af98 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   of SQL statemen
2af99 74 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65  ts.  This module
2af9a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
2af9b 66 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e  for.** generatin
2af9c 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20  g the code that 
2af9d 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 20  loops through a 
2af9e 74 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f  table looking fo
2af9f 72 20 61 70 70 6c 69 63 61 62 6c 65 0a 2a 2a 20  r applicable.** 
2afa0 72 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 20 61  rows.  Indices a
2afa1 72 65 20 73 65 6c 65 63 74 65 64 20 61 6e 64 20  re selected and 
2afa2 75 73 65 64 20 74 6f 20 73 70 65 65 64 20 74 68  used to speed th
2afa3 65 20 73 65 61 72 63 68 20 77 68 65 6e 20 64 6f  e search when do
2afa4 69 6e 67 0a 2a 2a 20 73 6f 20 69 73 20 61 70 70  ing.** so is app
2afa5 6c 69 63 61 62 6c 65 2e 20 20 42 65 63 61 75 73  licable.  Becaus
2afa6 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  e this module is
2afa7 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
2afa8 20 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 69 6e   selecting.** in
2afa9 64 69 63 65 73 2c 20 79 6f 75 20 6d 69 67 68 74  dices, you might
2afaa 20 61 6c 73 6f 20 74 68 69 6e 6b 20 6f 66 20 74   also think of t
2afab 68 69 73 20 6d 6f 64 75 6c 65 20 61 73 20 74 68  his module as th
2afac 65 20 22 71 75 65 72 79 20 6f 70 74 69 6d 69 7a  e "query optimiz
2afad 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  er"..**.** $Id: 
2afae 77 68 65 72 65 2e 63 2c 76 20 31 2e 33 39 36 20  where.c,v 1.396 
2afaf 32 30 30 39 2f 30 35 2f 30 36 20 31 39 3a 30 33  2009/05/06 19:03
2afb0 3a 31 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :14 drh Exp $.*/
2afb1 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f 75  ../*.** Trace ou
2afb2 74 70 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a 23  tput macros.*/.#
2afb3 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
2afb4 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e  E_TEST) || defin
2afb5 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
2afb6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2afb7 69 6e 74 20 73 71 6c 69 74 65 33 57 68 65 72 65  int sqlite3Where
2afb8 54 72 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69  Trace = 0;.#endi
2afb9 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  f.#if defined(SQ
2afba 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65  LITE_TEST) && de
2afbb 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
2afbc 55 47 29 0a 23 20 64 65 66 69 6e 65 20 57 48 45  UG).# define WHE
2afbd 52 45 54 52 41 43 45 28 58 29 20 20 69 66 28 73  RETRACE(X)  if(s
2afbe 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63 65  qlite3WhereTrace
2afbf 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
2afc0 69 6e 74 66 20 58 0a 23 65 6c 73 65 0a 23 20 64  intf X.#else.# d
2afc1 65 66 69 6e 65 20 57 48 45 52 45 54 52 41 43 45  efine WHERETRACE
2afc2 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46  (X).#endif../* F
2afc3 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65  orward reference
2afc4 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2afc5 63 74 20 57 68 65 72 65 43 6c 61 75 73 65 20 57  ct WhereClause W
2afc6 68 65 72 65 43 6c 61 75 73 65 3b 0a 74 79 70 65  hereClause;.type
2afc7 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65  def struct Where
2afc8 4d 61 73 6b 53 65 74 20 57 68 65 72 65 4d 61 73  MaskSet WhereMas
2afc9 6b 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74  kSet;.typedef st
2afca 72 75 63 74 20 57 68 65 72 65 4f 72 49 6e 66 6f  ruct WhereOrInfo
2afcb 20 57 68 65 72 65 4f 72 49 6e 66 6f 3b 0a 74 79   WhereOrInfo;.ty
2afcc 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65  pedef struct Whe
2afcd 72 65 41 6e 64 49 6e 66 6f 20 57 68 65 72 65 41  reAndInfo WhereA
2afce 6e 64 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20  ndInfo;.typedef 
2afcf 73 74 72 75 63 74 20 57 68 65 72 65 43 6f 73 74  struct WhereCost
2afd0 20 57 68 65 72 65 43 6f 73 74 3b 0a 0a 2f 2a 0a   WhereCost;../*.
2afd1 2a 2a 20 54 68 65 20 71 75 65 72 79 20 67 65 6e  ** The query gen
2afd2 65 72 61 74 6f 72 20 75 73 65 73 20 61 6e 20 61  erator uses an a
2afd3 72 72 61 79 20 6f 66 20 69 6e 73 74 61 6e 63 65  rray of instance
2afd4 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
2afd5 75 72 65 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 69  ure to.** help i
2afd6 74 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 73 75  t analyze the su
2afd7 62 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20  bexpressions of 
2afd8 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2afd9 2e 20 20 45 61 63 68 20 57 48 45 52 45 0a 2a 2a  .  Each WHERE.**
2afda 20 63 6c 61 75 73 65 20 73 75 62 65 78 70 72 65   clause subexpre
2afdb 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74  ssion is separat
2afdc 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65  ed from the othe
2afdd 72 73 20 62 79 20 41 4e 44 20 6f 70 65 72 61 74  rs by AND operat
2afde 6f 72 73 2c 0a 2a 2a 20 75 73 75 61 6c 6c 79 2c  ors,.** usually,
2afdf 20 6f 72 20 73 6f 6d 65 74 69 6d 65 73 20 73 75   or sometimes su
2afe0 62 65 78 70 72 65 73 73 69 6f 6e 73 20 73 65 70  bexpressions sep
2afe1 61 72 61 74 65 64 20 62 79 20 4f 52 2e 0a 2a 2a  arated by OR..**
2afe2 0a 2a 2a 20 41 6c 6c 20 57 68 65 72 65 54 65 72  .** All WhereTer
2afe3 6d 73 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64  ms are collected
2afe4 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 57   into a single W
2afe5 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63  hereClause struc
2afe6 74 75 72 65 2e 20 20 0a 2a 2a 20 54 68 65 20 66  ture.  .** The f
2afe7 6f 6c 6c 6f 77 69 6e 67 20 69 64 65 6e 74 69 74  ollowing identit
2afe8 79 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20  y holds:.**.**  
2afe9 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e        WhereTerm.
2afea 70 57 43 2d 3e 61 5b 57 68 65 72 65 54 65 72 6d  pWC->a[WhereTerm
2afeb 2e 69 64 78 5d 20 3d 3d 20 57 68 65 72 65 54 65  .idx] == WhereTe
2afec 72 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  rm.**.** When a 
2afed 74 65 72 6d 20 69 73 20 6f 66 20 74 68 65 20 66  term is of the f
2afee 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  orm:.**.**      
2afef 20 20 20 20 20 20 20 20 58 20 3c 6f 70 3e 20 3c          X <op> <
2aff0 65 78 70 72 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72  expr>.**.** wher
2aff1 65 20 58 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20  e X is a column 
2aff2 6e 61 6d 65 20 61 6e 64 20 3c 6f 70 3e 20 69 73  name and <op> is
2aff3 20 6f 6e 65 20 6f 66 20 63 65 72 74 61 69 6e 20   one of certain 
2aff4 6f 70 65 72 61 74 6f 72 73 2c 0a 2a 2a 20 74 68  operators,.** th
2aff5 65 6e 20 57 68 65 72 65 54 65 72 6d 2e 6c 65 66  en WhereTerm.lef
2aff6 74 43 75 72 73 6f 72 20 61 6e 64 20 57 68 65 72  tCursor and Wher
2aff7 65 54 65 72 6d 2e 75 2e 6c 65 66 74 43 6f 6c 75  eTerm.u.leftColu
2aff8 6d 6e 20 72 65 63 6f 72 64 20 74 68 65 0a 2a 2a  mn record the.**
2aff9 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 61   cursor number a
2affa 6e 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  nd column number
2affb 20 66 6f 72 20 58 2e 20 20 57 68 65 72 65 54 65   for X.  WhereTe
2affc 72 6d 2e 65 4f 70 65 72 61 74 6f 72 20 72 65 63  rm.eOperator rec
2affd 6f 72 64 73 0a 2a 2a 20 74 68 65 20 3c 6f 70 3e  ords.** the <op>
2affe 20 75 73 69 6e 67 20 61 20 62 69 74 6d 61 73 6b   using a bitmask
2afff 20 65 6e 63 6f 64 69 6e 67 20 64 65 66 69 6e 65   encoding define
2b000 64 20 62 79 20 57 4f 5f 78 78 78 20 62 65 6c 6f  d by WO_xxx belo
2b001 77 2e 20 20 54 68 65 0a 2a 2a 20 75 73 65 20 6f  w.  The.** use o
2b002 66 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f  f a bitmask enco
2b003 64 69 6e 67 20 66 6f 72 20 74 68 65 20 6f 70 65  ding for the ope
2b004 72 61 74 6f 72 20 61 6c 6c 6f 77 73 20 75 73 20  rator allows us 
2b005 74 6f 20 73 65 61 72 63 68 0a 2a 2a 20 71 75 69  to search.** qui
2b006 63 6b 6c 79 20 66 6f 72 20 74 65 72 6d 73 20 74  ckly for terms t
2b007 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20 6f 66  hat match any of
2b008 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 65   several differe
2b009 6e 74 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a  nt operators..**
2b00a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d 20  .** A WhereTerm 
2b00b 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 74 77  might also be tw
2b00c 6f 20 6f 72 20 6d 6f 72 65 20 73 75 62 74 65 72  o or more subter
2b00d 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ms connected by 
2b00e 4f 52 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  OR:.**.**       
2b00f 20 20 28 74 31 2e 58 20 3c 6f 70 3e 20 3c 65 78    (t1.X <op> <ex
2b010 70 72 3e 29 20 4f 52 20 28 74 31 2e 59 20 3c 6f  pr>) OR (t1.Y <o
2b011 70 3e 20 3c 65 78 70 72 3e 29 20 4f 52 20 2e 2e  p> <expr>) OR ..
2b012 2e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73  ...**.** In this
2b013 20 73 65 63 6f 6e 64 20 63 61 73 65 2c 20 77 74   second case, wt
2b014 46 6c 61 67 20 61 73 20 74 68 65 20 54 45 52 4d  Flag as the TERM
2b015 5f 4f 52 49 4e 46 4f 20 73 65 74 20 61 6e 64 20  _ORINFO set and 
2b016 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52  eOperator==WO_OR
2b017 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72  .** and the Wher
2b018 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20  eTerm.u.pOrInfo 
2b019 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20  field points to 
2b01a 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d  auxiliary inform
2b01b 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 73  ation that.** is
2b01c 20 63 6f 6c 6c 65 63 74 65 64 20 61 62 6f 75 74   collected about
2b01d 20 74 68 65 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20   the.**.** If a 
2b01e 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52  term in the WHER
2b01f 45 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f  E clause does no
2b020 74 20 6d 61 74 63 68 20 65 69 74 68 65 72 20 6f  t match either o
2b021 66 20 74 68 65 20 74 77 6f 20 70 72 65 76 69 6f  f the two previo
2b022 75 73 0a 2a 2a 20 63 61 74 65 67 6f 72 69 65 73  us.** categories
2b023 2c 20 74 68 65 6e 20 65 4f 70 65 72 61 74 6f 72  , then eOperator
2b024 3d 3d 30 2e 20 20 54 68 65 20 57 68 65 72 65 54  ==0.  The WhereT
2b025 65 72 6d 2e 70 45 78 70 72 20 66 69 65 6c 64 20  erm.pExpr field 
2b026 69 73 20 73 74 69 6c 6c 20 73 65 74 0a 2a 2a 20  is still set.** 
2b027 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  to the original 
2b028 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 63 6f  subexpression co
2b029 6e 74 65 6e 74 20 61 6e 64 20 77 74 46 6c 61 67  ntent and wtFlag
2b02a 73 20 69 73 20 73 65 74 20 75 70 20 61 70 70 72  s is set up appr
2b02b 6f 70 72 69 61 74 65 6c 79 0a 2a 2a 20 62 75 74  opriately.** but
2b02c 20 6e 6f 20 6f 74 68 65 72 20 66 69 65 6c 64 73   no other fields
2b02d 20 69 6e 20 74 68 65 20 57 68 65 72 65 54 65 72   in the WhereTer
2b02e 6d 20 6f 62 6a 65 63 74 20 61 72 65 20 6d 65 61  m object are mea
2b02f 6e 69 6e 67 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 57  ningful..**.** W
2b030 68 65 6e 20 65 4f 70 65 72 61 74 6f 72 21 3d 30  hen eOperator!=0
2b031 2c 20 70 72 65 72 65 71 52 69 67 68 74 20 61 6e  , prereqRight an
2b032 64 20 70 72 65 72 65 71 41 6c 6c 20 72 65 63 6f  d prereqAll reco
2b033 72 64 20 73 65 74 73 20 6f 66 20 63 75 72 73 6f  rd sets of curso
2b034 72 20 6e 75 6d 62 65 72 73 2c 0a 2a 2a 20 62 75  r numbers,.** bu
2b035 74 20 74 68 65 79 20 64 6f 20 73 6f 20 69 6e 64  t they do so ind
2b036 69 72 65 63 74 6c 79 2e 20 20 41 20 73 69 6e 67  irectly.  A sing
2b037 6c 65 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20  le WhereMaskSet 
2b038 73 74 72 75 63 74 75 72 65 20 74 72 61 6e 73 6c  structure transl
2b039 61 74 65 73 0a 2a 2a 20 63 75 72 73 6f 72 20 6e  ates.** cursor n
2b03a 75 6d 62 65 72 20 69 6e 74 6f 20 62 69 74 73 20  umber into bits 
2b03b 61 6e 64 20 74 68 65 20 74 72 61 6e 73 6c 61 74  and the translat
2b03c 65 64 20 62 69 74 20 69 73 20 73 74 6f 72 65 64  ed bit is stored
2b03d 20 69 6e 20 74 68 65 20 70 72 65 72 65 71 0a 2a   in the prereq.*
2b03e 2a 20 66 69 65 6c 64 73 2e 20 20 54 68 65 20 74  * fields.  The t
2b03f 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 75 73  ranslation is us
2b040 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d  ed in order to m
2b041 61 78 69 6d 69 7a 65 20 74 68 65 20 6e 75 6d 62  aximize the numb
2b042 65 72 20 6f 66 0a 2a 2a 20 62 69 74 73 20 74 68  er of.** bits th
2b043 61 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61  at will fit in a
2b044 20 42 69 74 6d 61 73 6b 2e 20 20 54 68 65 20 56   Bitmask.  The V
2b045 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2b046 72 73 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 73  rs might be.** s
2b047 70 72 65 61 64 20 6f 75 74 20 6f 76 65 72 20 74  pread out over t
2b048 68 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  he non-negative 
2b049 69 6e 74 65 67 65 72 73 2e 20 20 46 6f 72 20 65  integers.  For e
2b04a 78 61 6d 70 6c 65 2c 20 74 68 65 20 63 75 72 73  xample, the curs
2b04b 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 6d 69  or.** numbers mi
2b04c 67 68 74 20 62 65 20 33 2c 20 38 2c 20 39 2c 20  ght be 3, 8, 9, 
2b04d 31 30 2c 20 32 30 2c 20 32 33 2c 20 34 31 2c 20  10, 20, 23, 41, 
2b04e 61 6e 64 20 34 35 2e 20 20 54 68 65 20 57 68 65  and 45.  The Whe
2b04f 72 65 4d 61 73 6b 53 65 74 0a 2a 2a 20 74 72 61  reMaskSet.** tra
2b050 6e 73 6c 61 74 65 73 20 74 68 65 73 65 20 73 70  nslates these sp
2b051 61 72 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  arse cursor numb
2b052 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75  ers into consecu
2b053 74 69 76 65 20 69 6e 74 65 67 65 72 73 0a 2a 2a  tive integers.**
2b054 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
2b055 30 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61  0 in order to ma
2b056 6b 65 20 74 68 65 20 62 65 73 74 20 70 6f 73 73  ke the best poss
2b057 69 62 6c 65 20 75 73 65 20 6f 66 20 74 68 65 20  ible use of the 
2b058 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 62 69 74  available.** bit
2b059 73 20 69 6e 20 74 68 65 20 42 69 74 6d 61 73 6b  s in the Bitmask
2b05a 2e 20 20 53 6f 2c 20 69 6e 20 74 68 65 20 65 78  .  So, in the ex
2b05b 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74 68 65  ample above, the
2b05c 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 0a   cursor numbers.
2b05d 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 61 70 70  ** would be mapp
2b05e 65 64 20 69 6e 74 6f 20 69 6e 74 65 67 65 72 73  ed into integers
2b05f 20 30 20 74 68 72 6f 75 67 68 20 37 2e 0a 2a 2a   0 through 7..**
2b060 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  .** The number o
2b061 66 20 74 65 72 6d 73 20 69 6e 20 61 20 6a 6f 69  f terms in a joi
2b062 6e 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20  n is limited by 
2b063 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  the number of bi
2b064 74 73 0a 2a 2a 20 69 6e 20 70 72 65 72 65 71 52  ts.** in prereqR
2b065 69 67 68 74 20 61 6e 64 20 70 72 65 72 65 71 41  ight and prereqA
2b066 6c 6c 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  ll.  The default
2b067 20 69 73 20 36 34 20 62 69 74 73 2c 20 68 65 6e   is 64 bits, hen
2b068 63 65 20 53 51 4c 69 74 65 0a 2a 2a 20 69 73 20  ce SQLite.** is 
2b069 6f 6e 6c 79 20 61 62 6c 65 20 74 6f 20 70 72 6f  only able to pro
2b06a 63 65 73 73 20 6a 6f 69 6e 73 20 77 69 74 68 20  cess joins with 
2b06b 36 34 20 6f 72 20 66 65 77 65 72 20 74 61 62 6c  64 or fewer tabl
2b06c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  es..*/.typedef s
2b06d 74 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20  truct WhereTerm 
2b06e 57 68 65 72 65 54 65 72 6d 3b 0a 73 74 72 75 63  WhereTerm;.struc
2b06f 74 20 57 68 65 72 65 54 65 72 6d 20 7b 0a 20 20  t WhereTerm {.  
2b070 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20  Expr *pExpr;    
2b071 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
2b072 65 72 20 74 6f 20 74 68 65 20 73 75 62 65 78 70  er to the subexp
2b073 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20  ression that is 
2b074 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69  this term */.  i
2b075 6e 74 20 69 50 61 72 65 6e 74 3b 20 20 20 20 20  nt iParent;     
2b076 20 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c         /* Disabl
2b077 65 20 70 57 43 2d 3e 61 5b 69 50 61 72 65 6e 74  e pWC->a[iParent
2b078 5d 20 77 68 65 6e 20 74 68 69 73 20 74 65 72 6d  ] when this term
2b079 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20 69   disabled */.  i
2b07a 6e 74 20 6c 65 66 74 43 75 72 73 6f 72 3b 20 20  nt leftCursor;  
2b07b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
2b07c 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 69 6e 20   number of X in 
2b07d 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20  "X <op> <expr>" 
2b07e 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20  */.  union {.   
2b07f 20 69 6e 74 20 6c 65 66 74 43 6f 6c 75 6d 6e 3b   int leftColumn;
2b080 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
2b081 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 69  mn number of X i
2b082 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e  n "X <op> <expr>
2b083 22 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 4f 72  " */.    WhereOr
2b084 49 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20  Info *pOrInfo;  
2b085 20 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f 72 6d   /* Extra inform
2b086 61 74 69 6f 6e 20 69 66 20 65 4f 70 65 72 61 74  ation if eOperat
2b087 6f 72 3d 3d 57 4f 5f 4f 52 20 2a 2f 0a 20 20 20  or==WO_OR */.   
2b088 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70   WhereAndInfo *p
2b089 41 6e 64 49 6e 66 6f 3b 20 2f 2a 20 45 78 74 72  AndInfo; /* Extr
2b08a 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66  a information if
2b08b 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41   eOperator==WO_A
2b08c 4e 44 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75  ND */.  } u;.  u
2b08d 31 36 20 65 4f 70 65 72 61 74 6f 72 3b 20 20 20  16 eOperator;   
2b08e 20 20 20 20 20 20 20 2f 2a 20 41 20 57 4f 5f 78         /* A WO_x
2b08f 78 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 69  x value describi
2b090 6e 67 20 3c 6f 70 3e 20 2a 2f 0a 20 20 75 38 20  ng <op> */.  u8 
2b091 77 74 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  wtFlags;        
2b092 20 20 20 20 20 2f 2a 20 54 45 52 4d 5f 78 78 78       /* TERM_xxx
2b093 20 62 69 74 20 66 6c 61 67 73 2e 20 20 53 65 65   bit flags.  See
2b094 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 38 20 6e   below */.  u8 n
2b095 43 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20 20  Child;          
2b096 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2b097 20 63 68 69 6c 64 72 65 6e 20 74 68 61 74 20 6d   children that m
2b098 75 73 74 20 64 69 73 61 62 6c 65 20 75 73 20 2a  ust disable us *
2b099 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2b09a 2a 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 54  *pWC;       /* T
2b09b 68 65 20 63 6c 61 75 73 65 20 74 68 69 73 20 74  he clause this t
2b09c 65 72 6d 20 69 73 20 70 61 72 74 20 6f 66 20 2a  erm is part of *
2b09d 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72  /.  Bitmask prer
2b09e 65 71 52 69 67 68 74 3b 20 20 20 20 2f 2a 20 42  eqRight;    /* B
2b09f 69 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c 65 73  itmask of tables
2b0a0 20 75 73 65 64 20 62 79 20 70 45 78 70 72 2d 3e   used by pExpr->
2b0a1 70 52 69 67 68 74 20 2a 2f 0a 20 20 42 69 74 6d  pRight */.  Bitm
2b0a2 61 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 20  ask prereqAll;  
2b0a3 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f      /* Bitmask o
2b0a4 66 20 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e  f tables referen
2b0a5 63 65 64 20 62 79 20 70 45 78 70 72 20 2a 2f 0a  ced by pExpr */.
2b0a6 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65  };../*.** Allowe
2b0a7 64 20 76 61 6c 75 65 73 20 6f 66 20 57 68 65 72  d values of Wher
2b0a8 65 54 65 72 6d 2e 77 74 46 6c 61 67 73 0a 2a 2f  eTerm.wtFlags.*/
2b0a9 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 44 59  .#define TERM_DY
2b0aa 4e 41 4d 49 43 20 20 20 20 30 78 30 31 20 20 20  NAMIC    0x01   
2b0ab 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20  /* Need to call 
2b0ac 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2b0ad 65 28 64 62 2c 20 70 45 78 70 72 29 20 2a 2f 0a  e(db, pExpr) */.
2b0ae 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 56 49 52  #define TERM_VIR
2b0af 54 55 41 4c 20 20 20 20 30 78 30 32 20 20 20 2f  TUAL    0x02   /
2b0b0 2a 20 41 64 64 65 64 20 62 79 20 74 68 65 20 6f  * Added by the o
2b0b1 70 74 69 6d 69 7a 65 72 2e 20 20 44 6f 20 6e 6f  ptimizer.  Do no
2b0b2 74 20 63 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e  t code */.#defin
2b0b3 65 20 54 45 52 4d 5f 43 4f 44 45 44 20 20 20 20  e TERM_CODED    
2b0b4 20 20 30 78 30 34 20 20 20 2f 2a 20 54 68 69 73    0x04   /* This
2b0b5 20 74 65 72 6d 20 69 73 20 61 6c 72 65 61 64 79   term is already
2b0b6 20 63 6f 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e   coded */.#defin
2b0b7 65 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 20 20  e TERM_COPIED   
2b0b8 20 20 30 78 30 38 20 20 20 2f 2a 20 48 61 73 20    0x08   /* Has 
2b0b9 61 20 63 68 69 6c 64 20 2a 2f 0a 23 64 65 66 69  a child */.#defi
2b0ba 6e 65 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 20  ne TERM_ORINFO  
2b0bb 20 20 20 30 78 31 30 20 20 20 2f 2a 20 4e 65 65     0x10   /* Nee
2b0bc 64 20 74 6f 20 66 72 65 65 20 74 68 65 20 57 68  d to free the Wh
2b0bd 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66  ereTerm.u.pOrInf
2b0be 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66  o object */.#def
2b0bf 69 6e 65 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f  ine TERM_ANDINFO
2b0c0 20 20 20 20 30 78 32 30 20 20 20 2f 2a 20 4e 65      0x20   /* Ne
2b0c1 65 64 20 74 6f 20 66 72 65 65 20 74 68 65 20 57  ed to free the W
2b0c2 68 65 72 65 54 65 72 6d 2e 75 2e 70 41 6e 64 49  hereTerm.u.pAndI
2b0c3 6e 66 6f 20 6f 62 6a 20 2a 2f 0a 23 64 65 66 69  nfo obj */.#defi
2b0c4 6e 65 20 54 45 52 4d 5f 4f 52 5f 4f 4b 20 20 20  ne TERM_OR_OK   
2b0c5 20 20 20 30 78 34 30 20 20 20 2f 2a 20 55 73 65     0x40   /* Use
2b0c6 64 20 64 75 72 69 6e 67 20 4f 52 2d 63 6c 61 75  d during OR-clau
2b0c7 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 2a 2f  se processing */
2b0c8 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
2b0c9 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
2b0ca 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68  wing structure h
2b0cb 6f 6c 64 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61  olds all informa
2b0cc 74 69 6f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20  tion about a.** 
2b0cd 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d  WHERE clause.  M
2b0ce 6f 73 74 6c 79 20 74 68 69 73 20 69 73 20 61 20  ostly this is a 
2b0cf 63 6f 6e 74 61 69 6e 65 72 20 66 6f 72 20 6f 6e  container for on
2b0d0 65 20 6f 72 20 6d 6f 72 65 20 57 68 65 72 65 54  e or more WhereT
2b0d1 65 72 6d 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  erms..*/.struct 
2b0d2 57 68 65 72 65 43 6c 61 75 73 65 20 7b 0a 20 20  WhereClause {.  
2b0d3 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
2b0d4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2b0d5 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
2b0d6 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74  /.  WhereMaskSet
2b0d7 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20 2f 2a 20   *pMaskSet;  /* 
2b0d8 4d 61 70 70 69 6e 67 20 6f 66 20 74 61 62 6c 65  Mapping of table
2b0d9 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2b0da 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 20  to bitmasks */. 
2b0db 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20   u8 op;         
2b0dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 6c            /* Spl
2b0dd 69 74 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 4b  it operator.  TK
2b0de 5f 41 4e 44 20 6f 72 20 54 4b 5f 4f 52 20 2a 2f  _AND or TK_OR */
2b0df 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20  .  int nTerm;   
2b0e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2b0e1 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 2a  umber of terms *
2b0e2 2f 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20  /.  int nSlot;  
2b0e3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b0e4 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
2b0e5 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20 57 68  s in a[] */.  Wh
2b0e6 65 72 65 54 65 72 6d 20 2a 61 3b 20 20 20 20 20  ereTerm *a;     
2b0e7 20 20 20 20 20 20 20 2f 2a 20 45 61 63 68 20 61         /* Each a
2b0e8 5b 5d 20 64 65 73 63 72 69 62 65 73 20 61 20 74  [] describes a t
2b0e9 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45  erm of the WHERE
2b0ea 20 63 6c 75 61 73 65 20 2a 2f 0a 20 20 57 68 65   cluase */.  Whe
2b0eb 72 65 54 65 72 6d 20 61 53 74 61 74 69 63 5b 34  reTerm aStatic[4
2b0ec 5d 3b 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c  ];    /* Initial
2b0ed 20 73 74 61 74 69 63 20 73 70 61 63 65 20 66 6f   static space fo
2b0ee 72 20 61 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  r a[] */.};../*.
2b0ef 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d 20 77  ** A WhereTerm w
2b0f0 69 74 68 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57  ith eOperator==W
2b0f1 4f 5f 4f 52 20 68 61 73 20 69 74 73 20 75 2e 70  O_OR has its u.p
2b0f2 4f 72 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73  OrInfo pointer s
2b0f3 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d  et to.** a dynam
2b0f4 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
2b0f5 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
2b0f6 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
2b0f7 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
2b0f8 57 68 65 72 65 4f 72 49 6e 66 6f 20 7b 0a 20 20  WhereOrInfo {.  
2b0f9 57 68 65 72 65 43 6c 61 75 73 65 20 77 63 3b 20  WhereClause wc; 
2b0fa 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f           /* Deco
2b0fb 6d 70 6f 73 69 74 69 6f 6e 20 69 6e 74 6f 20 73  mposition into s
2b0fc 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 42 69 74  ubterms */.  Bit
2b0fd 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20  mask indexable; 
2b0fe 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
2b0ff 20 6f 66 20 61 6c 6c 20 69 6e 64 65 78 61 62 6c   of all indexabl
2b100 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  e tables in the 
2b101 63 6c 61 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  clause */.};../*
2b102 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d 20  .** A WhereTerm 
2b103 77 69 74 68 20 65 4f 70 65 72 61 74 6f 72 3d 3d  with eOperator==
2b104 57 4f 5f 41 4e 44 20 68 61 73 20 69 74 73 20 75  WO_AND has its u
2b105 2e 70 41 6e 64 49 6e 66 6f 20 70 6f 69 6e 74 65  .pAndInfo pointe
2b106 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79  r set to.** a dy
2b107 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
2b108 74 65 64 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ted instance of 
2b109 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2b10a 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
2b10b 63 74 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20  ct WhereAndInfo 
2b10c 7b 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  {.  WhereClause 
2b10d 77 63 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  wc;          /* 
2b10e 54 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  The subexpressio
2b10f 6e 20 62 72 6f 6b 65 6e 20 6f 75 74 20 2a 2f 0a  n broken out */.
2b110 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
2b111 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
2b112 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
2b113 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20   keeps track of 
2b114 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20 62 65 74  a mapping.** bet
2b115 77 65 65 6e 20 56 44 42 45 20 63 75 72 73 6f 72  ween VDBE cursor
2b116 20 6e 75 6d 62 65 72 73 20 61 6e 64 20 62 69 74   numbers and bit
2b117 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b  s of the bitmask
2b118 73 20 69 6e 20 57 68 65 72 65 54 65 72 6d 2e 0a  s in WhereTerm..
2b119 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 63  **.** The VDBE c
2b11a 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72  ursor numbers ar
2b11b 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73  e small integers
2b11c 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 0a 2a   contained in .*
2b11d 2a 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 2e 69  * SrcList_item.i
2b11e 43 75 72 73 6f 72 20 61 6e 64 20 45 78 70 72 2e  Cursor and Expr.
2b11f 69 54 61 62 6c 65 20 66 69 65 6c 64 73 2e 20 20  iTable fields.  
2b120 46 6f 72 20 61 6e 79 20 67 69 76 65 6e 20 57 48  For any given WH
2b121 45 52 45 20 0a 2a 2a 20 63 6c 61 75 73 65 2c 20  ERE .** clause, 
2b122 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  the cursor numbe
2b123 72 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 67  rs might not beg
2b124 69 6e 20 77 69 74 68 20 30 20 61 6e 64 20 74 68  in with 0 and th
2b125 65 79 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6e 74  ey might.** cont
2b126 61 69 6e 20 67 61 70 73 20 69 6e 20 74 68 65 20  ain gaps in the 
2b127 6e 75 6d 62 65 72 69 6e 67 20 73 65 71 75 65 6e  numbering sequen
2b128 63 65 2e 20 20 42 75 74 20 77 65 20 77 61 6e 74  ce.  But we want
2b129 20 74 6f 20 6d 61 6b 65 20 6d 61 78 69 6d 75 6d   to make maximum
2b12a 0a 2a 2a 20 75 73 65 20 6f 66 20 74 68 65 20 62  .** use of the b
2b12b 69 74 73 20 69 6e 20 6f 75 72 20 62 69 74 6d 61  its in our bitma
2b12c 73 6b 73 2e 20 20 54 68 69 73 20 73 74 72 75 63  sks.  This struc
2b12d 74 75 72 65 20 70 72 6f 76 69 64 65 73 20 61 20  ture provides a 
2b12e 6d 61 70 70 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20  mapping.** from 
2b12f 74 68 65 20 73 70 61 72 73 65 20 63 75 72 73 6f  the sparse curso
2b130 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 63  r numbers into c
2b131 6f 6e 73 65 63 75 74 69 76 65 20 69 6e 74 65 67  onsecutive integ
2b132 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  ers beginning.**
2b133 20 77 69 74 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49   with 0..**.** I
2b134 66 20 57 68 65 72 65 4d 61 73 6b 53 65 74 2e 69  f WhereMaskSet.i
2b135 78 5b 41 5d 3d 3d 42 20 69 74 20 6d 65 61 6e 73  x[A]==B it means
2b136 20 74 68 61 74 20 54 68 65 20 41 2d 74 68 20 62   that The A-th b
2b137 69 74 20 6f 66 20 61 20 42 69 74 6d 61 73 6b 0a  it of a Bitmask.
2b138 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 56  ** corresponds V
2b139 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2b13a 72 20 42 2e 20 20 54 68 65 20 41 2d 74 68 20 62  r B.  The A-th b
2b13b 69 74 20 6f 66 20 61 20 62 69 74 6d 61 73 6b 20  it of a bitmask 
2b13c 69 73 20 31 3c 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46  is 1<<A..**.** F
2b13d 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74  or example, if t
2b13e 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2b13f 65 78 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20  expression used 
2b140 74 68 65 73 65 20 56 44 42 45 0a 2a 2a 20 63 75  these VDBE.** cu
2b141 72 73 6f 72 73 3a 20 20 34 2c 20 35 2c 20 38 2c  rsors:  4, 5, 8,
2b142 20 32 39 2c 20 35 37 2c 20 37 33 2e 20 20 54 68   29, 57, 73.  Th
2b143 65 6e 20 74 68 65 20 20 57 68 65 72 65 4d 61 73  en the  WhereMas
2b144 6b 53 65 74 20 73 74 72 75 63 74 75 72 65 0a 2a  kSet structure.*
2b145 2a 20 77 6f 75 6c 64 20 6d 61 70 20 74 68 6f 73  * would map thos
2b146 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
2b147 20 69 6e 74 6f 20 62 69 74 73 20 30 20 74 68 72   into bits 0 thr
2b148 6f 75 67 68 20 35 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  ough 5..**.** No
2b149 74 65 20 74 68 61 74 20 74 68 65 20 6d 61 70 70  te that the mapp
2b14a 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  ing is not neces
2b14b 73 61 72 69 6c 79 20 6f 72 64 65 72 65 64 2e 20  sarily ordered. 
2b14c 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 0a   In the example.
2b14d 2a 2a 20 61 62 6f 76 65 2c 20 74 68 65 20 6d 61  ** above, the ma
2b14e 70 70 69 6e 67 20 6d 69 67 68 74 20 67 6f 20 6c  pping might go l
2b14f 69 6b 65 20 74 68 69 73 3a 20 20 34 2d 3e 33 2c  ike this:  4->3,
2b150 20 35 2d 3e 31 2c 20 38 2d 3e 32 2c 20 32 39 2d   5->1, 8->2, 29-
2b151 3e 30 2c 0a 2a 2a 20 35 37 2d 3e 35 2c 20 37 33  >0,.** 57->5, 73
2b152 2d 3e 34 2e 20 20 4f 72 20 6f 6e 65 20 6f 66 20  ->4.  Or one of 
2b153 37 31 39 20 6f 74 68 65 72 20 63 6f 6d 62 69 6e  719 other combin
2b154 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20  ations might be 
2b155 75 73 65 64 2e 20 49 74 0a 2a 2a 20 64 6f 65 73  used. It.** does
2b156 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6d 61 74 74   not really matt
2b157 65 72 2e 20 20 57 68 61 74 20 69 73 20 69 6d 70  er.  What is imp
2b158 6f 72 74 61 6e 74 20 69 73 20 74 68 61 74 20 73  ortant is that s
2b159 70 61 72 73 65 20 63 75 72 73 6f 72 0a 2a 2a 20  parse cursor.** 
2b15a 6e 75 6d 62 65 72 73 20 61 6c 6c 20 67 65 74 20  numbers all get 
2b15b 6d 61 70 70 65 64 20 69 6e 74 6f 20 62 69 74 20  mapped into bit 
2b15c 6e 75 6d 62 65 72 73 20 74 68 61 74 20 62 65 67  numbers that beg
2b15d 69 6e 20 77 69 74 68 20 30 20 61 6e 64 20 63 6f  in with 0 and co
2b15e 6e 74 61 69 6e 0a 2a 2a 20 6e 6f 20 67 61 70 73  ntain.** no gaps
2b15f 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
2b160 65 4d 61 73 6b 53 65 74 20 7b 0a 20 20 69 6e 74  eMaskSet {.  int
2b161 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   n;             
2b162 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2b163 6d 62 65 72 20 6f 66 20 61 73 73 69 67 6e 65 64  mber of assigned
2b164 20 63 75 72 73 6f 72 20 76 61 6c 75 65 73 20 2a   cursor values *
2b165 2f 0a 20 20 69 6e 74 20 69 78 5b 42 4d 53 5d 3b  /.  int ix[BMS];
2b166 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b167 20 20 2f 2a 20 43 75 72 73 6f 72 20 61 73 73 69    /* Cursor assi
2b168 67 6e 65 64 20 74 6f 20 65 61 63 68 20 62 69 74  gned to each bit
2b169 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
2b16a 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65 63 74  WhereCost object
2b16b 20 72 65 63 6f 72 64 73 20 61 20 6c 6f 6f 6b 75   records a looku
2b16c 70 20 73 74 72 61 74 65 67 79 20 61 6e 64 20 74  p strategy and t
2b16d 68 65 20 65 73 74 69 6d 61 74 65 64 0a 2a 2a 20  he estimated.** 
2b16e 63 6f 73 74 20 6f 66 20 70 75 72 73 75 69 6e 67  cost of pursuing
2b16f 20 74 68 61 74 20 73 74 72 61 74 65 67 79 2e 0a   that strategy..
2b170 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 43  */.struct WhereC
2b171 6f 73 74 20 7b 0a 20 20 57 68 65 72 65 50 6c 61  ost {.  WherePla
2b172 6e 20 70 6c 61 6e 3b 20 20 20 20 2f 2a 20 54 68  n plan;    /* Th
2b173 65 20 6c 6f 6f 6b 75 70 20 73 74 72 61 74 65 67  e lookup strateg
2b174 79 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 43  y */.  double rC
2b175 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 4f 76 65  ost;      /* Ove
2b176 72 61 6c 6c 20 63 6f 73 74 20 6f 66 20 70 75 72  rall cost of pur
2b177 73 75 69 6e 67 20 74 68 69 73 20 73 65 61 72 63  suing this searc
2b178 68 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20  h strategy */.  
2b179 64 6f 75 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20  double nRow;    
2b17a 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20     /* Estimated 
2b17b 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74  number of output
2b17c 20 72 6f 77 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   rows */.};../*.
2b17d 2a 2a 20 42 69 74 6d 61 73 6b 73 20 66 6f 72 20  ** Bitmasks for 
2b17e 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74 68  the operators th
2b17f 61 74 20 69 6e 64 69 63 65 73 20 61 72 65 20 61  at indices are a
2b180 62 6c 65 20 74 6f 20 65 78 70 6c 6f 69 74 2e 20  ble to exploit. 
2b181 20 41 6e 0a 2a 2a 20 4f 52 2d 65 64 20 63 6f 6d   An.** OR-ed com
2b182 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 73  bination of thes
2b183 65 20 76 61 6c 75 65 73 20 63 61 6e 20 62 65 20  e values can be 
2b184 75 73 65 64 20 77 68 65 6e 20 73 65 61 72 63 68  used when search
2b185 69 6e 67 20 66 6f 72 0a 2a 2a 20 74 65 72 6d 73  ing for.** terms
2b186 20 69 6e 20 74 68 65 20 77 68 65 72 65 20 63 6c   in the where cl
2b187 61 75 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ause..*/.#define
2b188 20 57 4f 5f 49 4e 20 20 20 20 20 30 78 30 30 31   WO_IN     0x001
2b189 0a 23 64 65 66 69 6e 65 20 57 4f 5f 45 51 20 20  .#define WO_EQ  
2b18a 20 20 20 30 78 30 30 32 0a 23 64 65 66 69 6e 65     0x002.#define
2b18b 20 57 4f 5f 4c 54 20 20 20 20 20 28 57 4f 5f 45   WO_LT     (WO_E
2b18c 51 3c 3c 28 54 4b 5f 4c 54 2d 54 4b 5f 45 51 29  Q<<(TK_LT-TK_EQ)
2b18d 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c 45 20  ).#define WO_LE 
2b18e 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f      (WO_EQ<<(TK_
2b18f 4c 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69  LE-TK_EQ)).#defi
2b190 6e 65 20 57 4f 5f 47 54 20 20 20 20 20 28 57 4f  ne WO_GT     (WO
2b191 5f 45 51 3c 3c 28 54 4b 5f 47 54 2d 54 4b 5f 45  _EQ<<(TK_GT-TK_E
2b192 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47  Q)).#define WO_G
2b193 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54  E     (WO_EQ<<(T
2b194 4b 5f 47 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65  K_GE-TK_EQ)).#de
2b195 66 69 6e 65 20 57 4f 5f 4d 41 54 43 48 20 20 30  fine WO_MATCH  0
2b196 78 30 34 30 0a 23 64 65 66 69 6e 65 20 57 4f 5f  x040.#define WO_
2b197 49 53 4e 55 4c 4c 20 30 78 30 38 30 0a 23 64 65  ISNULL 0x080.#de
2b198 66 69 6e 65 20 57 4f 5f 4f 52 20 20 20 20 20 30  fine WO_OR     0
2b199 78 31 30 30 20 20 20 20 20 20 20 2f 2a 20 54 77  x100       /* Tw
2b19a 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63 6f 6e  o or more OR-con
2b19b 6e 65 63 74 65 64 20 74 65 72 6d 73 20 2a 2f 0a  nected terms */.
2b19c 23 64 65 66 69 6e 65 20 57 4f 5f 41 4e 44 20 20  #define WO_AND  
2b19d 20 20 30 78 32 30 30 20 20 20 20 20 20 20 2f 2a    0x200       /*
2b19e 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 41 4e 44   Two or more AND
2b19f 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d 73  -connected terms
2b1a0 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 57 4f 5f   */..#define WO_
2b1a1 41 4c 4c 20 20 20 20 30 78 66 66 66 20 20 20 20  ALL    0xfff    
2b1a2 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c     /* Mask of al
2b1a3 6c 20 70 6f 73 73 69 62 6c 65 20 57 4f 5f 2a 20  l possible WO_* 
2b1a4 76 61 6c 75 65 73 20 2a 2f 0a 23 64 65 66 69 6e  values */.#defin
2b1a5 65 20 57 4f 5f 53 49 4e 47 4c 45 20 30 78 30 66  e WO_SINGLE 0x0f
2b1a6 66 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20  f       /* Mask 
2b1a7 6f 66 20 61 6c 6c 20 6e 6f 6e 2d 63 6f 6d 70 6f  of all non-compo
2b1a8 75 6e 64 20 57 4f 5f 2a 20 76 61 6c 75 65 73 20  und WO_* values 
2b1a9 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75 65 20  */../*.** Value 
2b1aa 66 6f 72 20 77 73 46 6c 61 67 73 20 72 65 74 75  for wsFlags retu
2b1ab 72 6e 65 64 20 62 79 20 62 65 73 74 49 6e 64 65  rned by bestInde
2b1ac 78 28 29 20 61 6e 64 20 73 74 6f 72 65 64 20 69  x() and stored i
2b1ad 6e 0a 2a 2a 20 57 68 65 72 65 4c 65 76 65 6c 2e  n.** WhereLevel.
2b1ae 77 73 46 6c 61 67 73 2e 20 20 54 68 65 73 65 20  wsFlags.  These 
2b1af 66 6c 61 67 73 20 64 65 74 65 72 6d 69 6e 65 20  flags determine 
2b1b0 77 68 69 63 68 20 73 65 61 72 63 68 0a 2a 2a 20  which search.** 
2b1b1 73 74 72 61 74 65 67 69 65 73 20 61 72 65 20 61  strategies are a
2b1b2 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2a 0a 2a  ppropriate..**.*
2b1b3 2a 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e  * The least sign
2b1b4 69 66 69 63 61 6e 74 20 31 32 20 62 69 74 73 20  ificant 12 bits 
2b1b5 69 73 20 72 65 73 65 72 76 65 64 20 61 73 20 61  is reserved as a
2b1b6 20 6d 61 73 6b 20 66 6f 72 20 57 4f 5f 20 76 61   mask for WO_ va
2b1b7 6c 75 65 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 54  lues above..** T
2b1b8 68 65 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73  he WhereLevel.ws
2b1b9 46 6c 61 67 73 20 66 69 65 6c 64 20 69 73 20 75  Flags field is u
2b1ba 73 75 61 6c 6c 79 20 73 65 74 20 74 6f 20 57 4f  sually set to WO
2b1bb 5f 49 4e 7c 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e  _IN|WO_EQ|WO_ISN
2b1bc 55 4c 4c 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74  ULL..** But if t
2b1bd 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  he table is the 
2b1be 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61  right table of a
2b1bf 20 6c 65 66 74 20 6a 6f 69 6e 2c 20 57 68 65 72   left join, Wher
2b1c0 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 0a 2a  eLevel.wsFlags.*
2b1c1 2a 20 69 73 20 73 65 74 20 74 6f 20 57 4f 5f 49  * is set to WO_I
2b1c2 4e 7c 57 4f 5f 45 51 2e 20 20 54 68 65 20 57 68  N|WO_EQ.  The Wh
2b1c3 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73  ereLevel.wsFlags
2b1c4 20 66 69 65 6c 64 20 63 61 6e 20 74 68 65 6e 20   field can then 
2b1c5 62 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 74 68  be used as.** th
2b1c6 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74 65 72  e "op" parameter
2b1c7 20 74 6f 20 66 69 6e 64 54 65 72 6d 20 77 68 65   to findTerm whe
2b1c8 6e 20 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69  n we are resolvi
2b1c9 6e 67 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73  ng equality cons
2b1ca 74 72 61 69 6e 74 73 2e 0a 2a 2a 20 49 53 4e 55  traints..** ISNU
2b1cb 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77  LL constraints w
2b1cc 69 6c 6c 20 74 68 65 6e 20 6e 6f 74 20 62 65 20  ill then not be 
2b1cd 75 73 65 64 20 6f 6e 20 74 68 65 20 72 69 67 68  used on the righ
2b1ce 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c 65 66  t table of a lef
2b1cf 74 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69 63 6b  t.** join.  Tick
2b1d0 65 74 73 20 23 32 31 37 37 20 61 6e 64 20 23 32  ets #2177 and #2
2b1d1 31 38 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  189..*/.#define 
2b1d2 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 20  WHERE_ROWID_EQ  
2b1d3 20 20 20 30 78 30 30 30 30 31 30 30 30 20 20 2f     0x00001000  /
2b1d4 2a 20 72 6f 77 69 64 3d 45 58 50 52 20 6f 72 20  * rowid=EXPR or 
2b1d5 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29 20 2a  rowid IN (...) *
2b1d6 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2b1d7 52 4f 57 49 44 5f 52 41 4e 47 45 20 20 30 78 30  ROWID_RANGE  0x0
2b1d8 30 30 30 32 30 30 30 20 20 2f 2a 20 72 6f 77 69  0002000  /* rowi
2b1d9 64 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20 72 6f  d<EXPR and/or ro
2b1da 77 69 64 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66  wid>EXPR */.#def
2b1db 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  ine WHERE_COLUMN
2b1dc 5f 45 51 20 20 20 20 30 78 30 30 30 31 30 30 30  _EQ    0x0001000
2b1dd 30 20 20 2f 2a 20 78 3d 45 58 50 52 20 6f 72 20  0  /* x=EXPR or 
2b1de 78 20 49 4e 20 28 2e 2e 2e 29 20 6f 72 20 78 20  x IN (...) or x 
2b1df 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69  IS NULL */.#defi
2b1e0 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  ne WHERE_COLUMN_
2b1e1 52 41 4e 47 45 20 30 78 30 30 30 32 30 30 30 30  RANGE 0x00020000
2b1e2 20 20 2f 2a 20 78 3c 45 58 50 52 20 61 6e 64 2f    /* x<EXPR and/
2b1e3 6f 72 20 78 3e 45 58 50 52 20 2a 2f 0a 23 64 65  or x>EXPR */.#de
2b1e4 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d  fine WHERE_COLUM
2b1e5 4e 5f 49 4e 20 20 20 20 30 78 30 30 30 34 30 30  N_IN    0x000400
2b1e6 30 30 20 20 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e  00  /* x IN (...
2b1e7 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ) */.#define WHE
2b1e8 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 20  RE_COLUMN_NULL  
2b1e9 30 78 30 30 30 38 30 30 30 30 20 20 2f 2a 20 78  0x00080000  /* x
2b1ea 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66   IS NULL */.#def
2b1eb 69 6e 65 20 57 48 45 52 45 5f 49 4e 44 45 58 45  ine WHERE_INDEXE
2b1ec 44 20 20 20 20 20 20 30 78 30 30 30 66 30 30 30  D      0x000f000
2b1ed 30 20 20 2f 2a 20 41 6e 79 74 68 69 6e 67 20 74  0  /* Anything t
2b1ee 68 61 74 20 75 73 65 73 20 61 6e 20 69 6e 64 65  hat uses an inde
2b1ef 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  x */.#define WHE
2b1f0 52 45 5f 49 4e 5f 41 42 4c 45 20 20 20 20 20 20  RE_IN_ABLE      
2b1f1 30 78 30 30 30 66 31 30 30 30 20 20 2f 2a 20 41  0x000f1000  /* A
2b1f2 62 6c 65 20 74 6f 20 73 75 70 70 6f 72 74 20 61  ble to support a
2b1f3 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  n IN operator */
2b1f4 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 54  .#define WHERE_T
2b1f5 4f 50 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 30  OP_LIMIT    0x00
2b1f6 31 30 30 30 30 30 20 20 2f 2a 20 78 3c 45 58 50  100000  /* x<EXP
2b1f7 52 20 6f 72 20 78 3c 3d 45 58 50 52 20 63 6f 6e  R or x<=EXPR con
2b1f8 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69  straint */.#defi
2b1f9 6e 65 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d  ne WHERE_BTM_LIM
2b1fa 49 54 20 20 20 20 30 78 30 30 32 30 30 30 30 30  IT    0x00200000
2b1fb 20 20 2f 2a 20 78 3e 45 58 50 52 20 6f 72 20 78    /* x>EXPR or x
2b1fc 3e 3d 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e  >=EXPR constrain
2b1fd 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  t */.#define WHE
2b1fe 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 20 20 20 20  RE_IDX_ONLY     
2b1ff 30 78 30 30 38 30 30 30 30 30 20 20 2f 2a 20 55  0x00800000  /* U
2b200 73 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2d 20  se index only - 
2b201 6f 6d 69 74 20 74 61 62 6c 65 20 2a 2f 0a 23 64  omit table */.#d
2b202 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
2b203 52 42 59 20 20 20 20 20 20 30 78 30 31 30 30 30  RBY      0x01000
2b204 30 30 30 20 20 2f 2a 20 4f 75 74 70 75 74 20 77  000  /* Output w
2b205 69 6c 6c 20 61 70 70 65 61 72 20 69 6e 20 63 6f  ill appear in co
2b206 72 72 65 63 74 20 6f 72 64 65 72 20 2a 2f 0a 23  rrect order */.#
2b207 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 45 56  define WHERE_REV
2b208 45 52 53 45 20 20 20 20 20 20 30 78 30 32 30 30  ERSE      0x0200
2b209 30 30 30 30 20 20 2f 2a 20 53 63 61 6e 20 69 6e  0000  /* Scan in
2b20a 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a   reverse order *
2b20b 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2b20c 55 4e 49 51 55 45 20 20 20 20 20 20 20 30 78 30  UNIQUE       0x0
2b20d 34 30 30 30 30 30 30 20 20 2f 2a 20 53 65 6c 65  4000000  /* Sele
2b20e 63 74 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e  cts no more than
2b20f 20 6f 6e 65 20 72 6f 77 20 2a 2f 0a 23 64 65 66   one row */.#def
2b210 69 6e 65 20 57 48 45 52 45 5f 56 49 52 54 55 41  ine WHERE_VIRTUA
2b211 4c 54 41 42 4c 45 20 30 78 30 38 30 30 30 30 30  LTABLE 0x0800000
2b212 30 20 20 2f 2a 20 55 73 65 20 76 69 72 74 75 61  0  /* Use virtua
2b213 6c 2d 74 61 62 6c 65 20 70 72 6f 63 65 73 73 69  l-table processi
2b214 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  ng */.#define WH
2b215 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 20 20 20  ERE_MULTI_OR    
2b216 20 30 78 31 30 30 30 30 30 30 30 20 20 2f 2a 20   0x10000000  /* 
2b217 4f 52 20 75 73 69 6e 67 20 6d 75 6c 74 69 70 6c  OR using multipl
2b218 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a 2f 2a  e indices */../*
2b219 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
2b21a 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 57 68   preallocated Wh
2b21b 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74  ereClause struct
2b21c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ure..*/.static v
2b21d 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 49  oid whereClauseI
2b21e 6e 69 74 28 0a 20 20 57 68 65 72 65 43 6c 61 75  nit(.  WhereClau
2b21f 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20  se *pWC,        
2b220 2f 2a 20 54 68 65 20 57 68 65 72 65 43 6c 61 75  /* The WhereClau
2b221 73 65 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c  se to be initial
2b222 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20  ized */.  Parse 
2b223 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2b224 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
2b225 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57  g context */.  W
2b226 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2b227 73 6b 53 65 74 20 20 20 2f 2a 20 4d 61 70 70 69  skSet   /* Mappi
2b228 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 20 63 75  ng from table cu
2b229 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20  rsor numbers to 
2b22a 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 29 7b 0a 20  bitmasks */.){. 
2b22b 20 70 57 43 2d 3e 70 50 61 72 73 65 20 3d 20 70   pWC->pParse = p
2b22c 50 61 72 73 65 3b 0a 20 20 70 57 43 2d 3e 70 4d  Parse;.  pWC->pM
2b22d 61 73 6b 53 65 74 20 3d 20 70 4d 61 73 6b 53 65  askSet = pMaskSe
2b22e 74 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d 20  t;.  pWC->nTerm 
2b22f 3d 20 30 3b 0a 20 20 70 57 43 2d 3e 6e 53 6c 6f  = 0;.  pWC->nSlo
2b230 74 20 3d 20 41 72 72 61 79 53 69 7a 65 28 70 57  t = ArraySize(pW
2b231 43 2d 3e 61 53 74 61 74 69 63 29 3b 0a 20 20 70  C->aStatic);.  p
2b232 57 43 2d 3e 61 20 3d 20 70 57 43 2d 3e 61 53 74  WC->a = pWC->aSt
2b233 61 74 69 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77  atic;.}../* Forw
2b234 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f  ard reference */
2b235 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65  .static void whe
2b236 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 57 68  reClauseClear(Wh
2b237 65 72 65 43 6c 61 75 73 65 2a 29 3b 0a 0a 2f 2a  ereClause*);../*
2b238 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61  .** Deallocate a
2b239 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69  ll memory associ
2b23a 61 74 65 64 20 77 69 74 68 20 61 20 57 68 65 72  ated with a Wher
2b23b 65 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a  eOrInfo object..
2b23c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
2b23d 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74 65  hereOrInfoDelete
2b23e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68  (sqlite3 *db, Wh
2b23f 65 72 65 4f 72 49 6e 66 6f 20 2a 70 29 7b 0a 20  ereOrInfo *p){. 
2b240 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61   whereClauseClea
2b241 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c  r(&p->wc);.  sql
2b242 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2b243 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c  );.}../*.** Deal
2b244 6c 6f 63 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72  locate all memor
2b245 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
2b246 68 20 61 20 57 68 65 72 65 41 6e 64 49 6e 66 6f  h a WhereAndInfo
2b247 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74   object..*/.stat
2b248 69 63 20 76 6f 69 64 20 77 68 65 72 65 41 6e 64  ic void whereAnd
2b249 49 6e 66 6f 44 65 6c 65 74 65 28 73 71 6c 69 74  InfoDelete(sqlit
2b24a 65 33 20 2a 64 62 2c 20 57 68 65 72 65 41 6e 64  e3 *db, WhereAnd
2b24b 49 6e 66 6f 20 2a 70 29 7b 0a 20 20 77 68 65 72  Info *p){.  wher
2b24c 65 43 6c 61 75 73 65 43 6c 65 61 72 28 26 70 2d  eClauseClear(&p-
2b24d 3e 77 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  >wc);.  sqlite3D
2b24e 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a  bFree(db, p);.}.
2b24f 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74  ./*.** Deallocat
2b250 65 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20  e a WhereClause 
2b251 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
2b252 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
2b253 63 74 75 72 65 0a 2a 2a 20 69 74 73 65 6c 66 20  cture.** itself 
2b254 69 73 20 6e 6f 74 20 66 72 65 65 64 2e 20 20 54  is not freed.  T
2b255 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
2b256 68 65 20 69 6e 76 65 72 73 65 20 6f 66 20 77 68  he inverse of wh
2b257 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 29 2e  ereClauseInit().
2b258 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2b259 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72  whereClauseClear
2b25a 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57  (WhereClause *pW
2b25b 43 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 57  C){.  int i;.  W
2b25c 68 65 72 65 54 65 72 6d 20 2a 61 3b 0a 20 20 73  hereTerm *a;.  s
2b25d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 57 43  qlite3 *db = pWC
2b25e 2d 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  ->pParse->db;.  
2b25f 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d  for(i=pWC->nTerm
2b260 2d 31 2c 20 61 3d 70 57 43 2d 3e 61 3b 20 69 3e  -1, a=pWC->a; i>
2b261 3d 30 3b 20 69 2d 2d 2c 20 61 2b 2b 29 7b 0a 20  =0; i--, a++){. 
2b262 20 20 20 69 66 28 20 61 2d 3e 77 74 46 6c 61 67     if( a->wtFlag
2b263 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43  s & TERM_DYNAMIC
2b264 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2b265 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2b266 61 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  a->pExpr);.    }
2b267 0a 20 20 20 20 69 66 28 20 61 2d 3e 77 74 46 6c  .    if( a->wtFl
2b268 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e 46  ags & TERM_ORINF
2b269 4f 20 29 7b 0a 20 20 20 20 20 20 77 68 65 72 65  O ){.      where
2b26a 4f 72 49 6e 66 6f 44 65 6c 65 74 65 28 64 62 2c  OrInfoDelete(db,
2b26b 20 61 2d 3e 75 2e 70 4f 72 49 6e 66 6f 29 3b 0a   a->u.pOrInfo);.
2b26c 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 2d      }else if( a-
2b26d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2b26e 41 4e 44 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20  ANDINFO ){.     
2b26f 20 77 68 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c   whereAndInfoDel
2b270 65 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70 41 6e  ete(db, a->u.pAn
2b271 64 49 6e 66 6f 29 3b 0a 20 20 20 20 7d 0a 20 20  dInfo);.    }.  
2b272 7d 0a 20 20 69 66 28 20 70 57 43 2d 3e 61 21 3d  }.  if( pWC->a!=
2b273 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29 7b 0a  pWC->aStatic ){.
2b274 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2b275 65 28 64 62 2c 20 70 57 43 2d 3e 61 29 3b 0a 20  e(db, pWC->a);. 
2b276 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
2b277 61 20 73 69 6e 67 6c 65 20 6e 65 77 20 57 68 65  a single new Whe
2b278 72 65 54 65 72 6d 20 65 6e 74 72 79 20 74 6f 20  reTerm entry to 
2b279 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  the WhereClause 
2b27a 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20 54  object pWC..** T
2b27b 68 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d  he new WhereTerm
2b27c 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e 73 74   object is const
2b27d 72 75 63 74 65 64 20 66 72 6f 6d 20 45 78 70 72  ructed from Expr
2b27e 20 70 20 61 6e 64 20 77 69 74 68 20 77 74 46 6c   p and with wtFl
2b27f 61 67 73 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65  ags..** The inde
2b280 78 20 69 6e 20 70 57 43 2d 3e 61 5b 5d 20 6f 66  x in pWC->a[] of
2b281 20 74 68 65 20 6e 65 77 20 57 68 65 72 65 54 65   the new WhereTe
2b282 72 6d 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  rm is returned o
2b283 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 30 20  n success..** 0 
2b284 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
2b285 68 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d  he new WhereTerm
2b286 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 64   could not be ad
2b287 64 65 64 20 64 75 65 20 74 6f 20 61 20 6d 65 6d  ded due to a mem
2b288 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ory.** allocatio
2b289 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 6d 65  n error.  The me
2b28a 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
2b28b 66 61 69 6c 75 72 65 20 77 69 6c 6c 20 62 65 20  failure will be 
2b28c 72 65 63 6f 72 64 65 64 20 69 6e 0a 2a 2a 20 74  recorded in.** t
2b28d 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  he db->mallocFai
2b28e 6c 65 64 20 66 6c 61 67 20 73 6f 20 74 68 61 74  led flag so that
2b28f 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 66 75   higher-level fu
2b290 6e 63 74 69 6f 6e 73 20 63 61 6e 20 64 65 74 65  nctions can dete
2b291 63 74 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ct it..**.** Thi
2b292 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 69  s routine will i
2b293 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65  ncrease the size
2b294 20 6f 66 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d   of the pWC->a[]
2b295 20 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73   array as necess
2b296 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ary..**.** If th
2b297 65 20 77 74 46 6c 61 67 73 20 61 72 67 75 6d 65  e wtFlags argume
2b298 6e 74 20 69 6e 63 6c 75 64 65 73 20 54 45 52 4d  nt includes TERM
2b299 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 72  _DYNAMIC, then r
2b29a 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a 2a 2a  esponsibility.**
2b29b 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65   for freeing the
2b29c 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73   expression p is
2b29d 20 61 73 73 75 6d 65 64 20 62 79 20 74 68 65 20   assumed by the 
2b29e 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65  WhereClause obje
2b29f 63 74 20 70 57 43 2e 0a 2a 2a 20 54 68 69 73 20  ct pWC..** This 
2b2a0 69 73 20 74 72 75 65 20 65 76 65 6e 20 69 66 20  is true even if 
2b2a1 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69  this routine fai
2b2a2 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  ls to allocate a
2b2a3 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 2e 0a   new WhereTerm..
2b2a4 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20  **.** WARNING:  
2b2a5 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67  This routine mig
2b2a6 68 74 20 72 65 61 6c 6c 6f 63 61 74 65 20 74 68  ht reallocate th
2b2a7 65 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20  e space used to 
2b2a8 73 74 6f 72 65 0a 2a 2a 20 57 68 65 72 65 54 65  store.** WhereTe
2b2a9 72 6d 73 2e 20 20 41 6c 6c 20 70 6f 69 6e 74 65  rms.  All pointe
2b2aa 72 73 20 74 6f 20 57 68 65 72 65 54 65 72 6d 73  rs to WhereTerms
2b2ab 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 61 6c   should be inval
2b2ac 69 64 61 74 65 64 20 61 66 74 65 72 0a 2a 2a 20  idated after.** 
2b2ad 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
2b2ae 74 69 6e 65 2e 20 20 53 75 63 68 20 70 6f 69 6e  tine.  Such poin
2b2af 74 65 72 73 20 6d 61 79 20 62 65 20 72 65 69 6e  ters may be rein
2b2b0 69 74 69 61 6c 69 7a 65 64 20 62 79 20 72 65 66  itialized by ref
2b2b1 65 72 65 6e 63 69 6e 67 0a 2a 2a 20 74 68 65 20  erencing.** the 
2b2b2 70 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79 2e 0a  pWC->a[] array..
2b2b3 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68  */.static int wh
2b2b4 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
2b2b5 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2b2b6 2c 20 45 78 70 72 20 2a 70 2c 20 75 38 20 77 74  , Expr *p, u8 wt
2b2b7 46 6c 61 67 73 29 7b 0a 20 20 57 68 65 72 65 54  Flags){.  WhereT
2b2b8 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e  erm *pTerm;.  in
2b2b9 74 20 69 64 78 3b 0a 20 20 69 66 28 20 70 57 43  t idx;.  if( pWC
2b2ba 2d 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d 3e 6e 53  ->nTerm>=pWC->nS
2b2bb 6c 6f 74 20 29 7b 0a 20 20 20 20 57 68 65 72 65  lot ){.    Where
2b2bc 54 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 70 57 43  Term *pOld = pWC
2b2bd 2d 3e 61 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ->a;.    sqlite3
2b2be 20 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50 61 72   *db = pWC->pPar
2b2bf 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 57 43 2d  se->db;.    pWC-
2b2c0 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  >a = sqlite3DbMa
2b2c1 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
2b2c2 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57  of(pWC->a[0])*pW
2b2c3 43 2d 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0a 20 20  C->nSlot*2 );.  
2b2c4 20 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d 30 20    if( pWC->a==0 
2b2c5 29 7b 0a 20 20 20 20 20 20 69 66 28 20 77 74 46  ){.      if( wtF
2b2c6 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41  lags & TERM_DYNA
2b2c7 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 73  MIC ){.        s
2b2c8 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2b2c9 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d  (db, p);.      }
2b2ca 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 20 3d 20  .      pWC->a = 
2b2cb 70 4f 6c 64 3b 0a 20 20 20 20 20 20 72 65 74 75  pOld;.      retu
2b2cc 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
2b2cd 6d 65 6d 63 70 79 28 70 57 43 2d 3e 61 2c 20 70  memcpy(pWC->a, p
2b2ce 4f 6c 64 2c 20 73 69 7a 65 6f 66 28 70 57 43 2d  Old, sizeof(pWC-
2b2cf 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 54 65 72  >a[0])*pWC->nTer
2b2d0 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64  m);.    if( pOld
2b2d1 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29  !=pWC->aStatic )
2b2d2 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
2b2d3 62 46 72 65 65 28 64 62 2c 20 70 4f 6c 64 29 3b  bFree(db, pOld);
2b2d4 0a 20 20 20 20 7d 0a 20 20 20 20 70 57 43 2d 3e  .    }.    pWC->
2b2d5 6e 53 6c 6f 74 20 3d 20 73 71 6c 69 74 65 33 44  nSlot = sqlite3D
2b2d6 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
2b2d7 70 57 43 2d 3e 61 29 2f 73 69 7a 65 6f 66 28 70  pWC->a)/sizeof(p
2b2d8 57 43 2d 3e 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20  WC->a[0]);.  }. 
2b2d9 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
2b2da 5b 69 64 78 20 3d 20 70 57 43 2d 3e 6e 54 65 72  [idx = pWC->nTer
2b2db 6d 2b 2b 5d 3b 0a 20 20 70 54 65 72 6d 2d 3e 70  m++];.  pTerm->p
2b2dc 45 78 70 72 20 3d 20 70 3b 0a 20 20 70 54 65 72  Expr = p;.  pTer
2b2dd 6d 2d 3e 77 74 46 6c 61 67 73 20 3d 20 77 74 46  m->wtFlags = wtF
2b2de 6c 61 67 73 3b 0a 20 20 70 54 65 72 6d 2d 3e 70  lags;.  pTerm->p
2b2df 57 43 20 3d 20 70 57 43 3b 0a 20 20 70 54 65 72  WC = pWC;.  pTer
2b2e0 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b  m->iParent = -1;
2b2e1 0a 20 20 72 65 74 75 72 6e 20 69 64 78 3b 0a 7d  .  return idx;.}
2b2e2 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2b2e3 74 69 6e 65 20 69 64 65 6e 74 69 66 69 65 73 20  tine identifies 
2b2e4 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 69  subexpressions i
2b2e5 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
2b2e6 73 65 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68  se where.** each
2b2e7 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69   subexpression i
2b2e8 73 20 73 65 70 61 72 61 74 65 64 20 62 79 20 74  s separated by t
2b2e9 68 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 20  he AND operator 
2b2ea 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a  or some other.**
2b2eb 20 6f 70 65 72 61 74 6f 72 20 73 70 65 63 69 66   operator specif
2b2ec 69 65 64 20 69 6e 20 74 68 65 20 6f 70 20 70 61  ied in the op pa
2b2ed 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 57 68  rameter.  The Wh
2b2ee 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74  ereClause struct
2b2ef 75 72 65 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64  ure.** is filled
2b2f0 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
2b2f1 6f 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  o subexpressions
2b2f2 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  .  For example:.
2b2f3 2a 2a 0a 2a 2a 20 20 20 20 57 48 45 52 45 20 20  **.**    WHERE  
2b2f4 61 3d 3d 27 68 65 6c 6c 6f 27 20 41 4e 44 20 63  a=='hello' AND c
2b2f5 6f 61 6c 65 73 63 65 28 62 2c 31 31 29 3c 31 30  oalesce(b,11)<10
2b2f6 20 41 4e 44 20 28 63 2b 31 32 21 3d 64 20 4f 52   AND (c+12!=d OR
2b2f7 20 63 3d 3d 32 32 29 0a 2a 2a 20 20 20 20 20 20   c==22).**      
2b2f8 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20       \________/ 
2b2f9 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
2b2fa 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f  ____/     \_____
2b2fb 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20  ___________/.** 
2b2fc 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b             slot[
2b2fd 30 5d 20 20 20 20 20 20 20 20 20 20 20 20 73 6c  0]            sl
2b2fe 6f 74 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20  ot[1]           
2b2ff 20 20 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a 2a      slot[2].**.*
2b300 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 57  * The original W
2b301 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 70  HERE clause in p
2b302 45 78 70 72 20 69 73 20 75 6e 61 6c 74 65 72 65  Expr is unaltere
2b303 64 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75  d.  All this rou
2b304 74 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 69 73 20  tine.** does is 
2b305 6d 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74 72  make slot[] entr
2b306 69 65 73 20 70 6f 69 6e 74 20 74 6f 20 73 75 62  ies point to sub
2b307 73 74 72 75 63 74 75 72 65 20 77 69 74 68 69 6e  structure within
2b308 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e   pExpr..**.** In
2b309 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
2b30a 6e 74 65 6e 63 65 20 61 6e 64 20 69 6e 20 74 68  ntence and in th
2b30b 65 20 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f 74  e diagram, "slot
2b30c 5b 5d 22 20 72 65 66 65 72 73 20 74 6f 0a 2a 2a  []" refers to.**
2b30d 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
2b30e 2e 61 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 65  .a[] array.  The
2b30f 20 73 6c 6f 74 5b 5d 20 61 72 72 61 79 20 67 72   slot[] array gr
2b310 6f 77 73 20 61 73 20 6e 65 65 64 65 64 20 74 6f  ows as needed to
2b311 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c 6c 20   contain.** all 
2b312 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45  terms of the WHE
2b313 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74  RE clause..*/.st
2b314 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 53  atic void whereS
2b315 70 6c 69 74 28 57 68 65 72 65 43 6c 61 75 73 65  plit(WhereClause
2b316 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 45 78   *pWC, Expr *pEx
2b317 70 72 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 70  pr, int op){.  p
2b318 57 43 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b  WC->op = (u8)op;
2b319 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20  .  if( pExpr==0 
2b31a 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
2b31b 70 45 78 70 72 2d 3e 6f 70 21 3d 6f 70 20 29 7b  pExpr->op!=op ){
2b31c 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65  .    whereClause
2b31d 49 6e 73 65 72 74 28 70 57 43 2c 20 70 45 78 70  Insert(pWC, pExp
2b31e 72 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  r, 0);.  }else{.
2b31f 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70      whereSplit(p
2b320 57 43 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  WC, pExpr->pLeft
2b321 2c 20 6f 70 29 3b 0a 20 20 20 20 77 68 65 72 65  , op);.    where
2b322 53 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72  Split(pWC, pExpr
2b323 2d 3e 70 52 69 67 68 74 2c 20 6f 70 29 3b 0a 20  ->pRight, op);. 
2b324 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74   }.}../*.** Init
2b325 69 61 6c 69 7a 65 20 61 6e 20 65 78 70 72 65 73  ialize an expres
2b326 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20 28 61  sion mask set (a
2b327 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 6f 62   WhereMaskSet ob
2b328 6a 65 63 74 29 0a 2a 2f 0a 23 64 65 66 69 6e 65  ject).*/.#define
2b329 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 50 29 20   initMaskSet(P) 
2b32a 20 6d 65 6d 73 65 74 28 50 2c 20 30 2c 20 73 69   memset(P, 0, si
2b32b 7a 65 6f 66 28 2a 50 29 29 0a 0a 2f 2a 0a 2a 2a  zeof(*P))../*.**
2b32c 20 52 65 74 75 72 6e 20 74 68 65 20 62 69 74 6d   Return the bitm
2b32d 61 73 6b 20 66 6f 72 20 74 68 65 20 67 69 76 65  ask for the give
2b32e 6e 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e  n cursor number.
2b32f 20 20 52 65 74 75 72 6e 20 30 20 69 66 0a 2a 2a    Return 0 if.**
2b330 20 69 43 75 72 73 6f 72 20 69 73 20 6e 6f 74 20   iCursor is not 
2b331 69 6e 20 74 68 65 20 73 65 74 2e 0a 2a 2f 0a 73  in the set..*/.s
2b332 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 67 65  tatic Bitmask ge
2b333 74 4d 61 73 6b 28 57 68 65 72 65 4d 61 73 6b 53  tMask(WhereMaskS
2b334 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e  et *pMaskSet, in
2b335 74 20 69 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e  t iCursor){.  in
2b336 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t i;.  assert( p
2b337 4d 61 73 6b 53 65 74 2d 3e 6e 3c 3d 73 69 7a 65  MaskSet->n<=size
2b338 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 20 29 3b  of(Bitmask)*8 );
2b339 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4d  .  for(i=0; i<pM
2b33a 61 73 6b 53 65 74 2d 3e 6e 3b 20 69 2b 2b 29 7b  askSet->n; i++){
2b33b 0a 20 20 20 20 69 66 28 20 70 4d 61 73 6b 53 65  .    if( pMaskSe
2b33c 74 2d 3e 69 78 5b 69 5d 3d 3d 69 43 75 72 73 6f  t->ix[i]==iCurso
2b33d 72 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  r ){.      retur
2b33e 6e 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c  n ((Bitmask)1)<<
2b33f 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  i;.    }.  }.  r
2b340 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
2b341 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6d  * Create a new m
2b342 61 73 6b 20 66 6f 72 20 63 75 72 73 6f 72 20 69  ask for cursor i
2b343 43 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  Cursor..**.** Th
2b344 65 72 65 20 69 73 20 6f 6e 65 20 63 75 72 73 6f  ere is one curso
2b345 72 20 70 65 72 20 74 61 62 6c 65 20 69 6e 20 74  r per table in t
2b346 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20  he FROM clause. 
2b347 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   The number of.*
2b348 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  * tables in the 
2b349 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c  FROM clause is l
2b34a 69 6d 69 74 65 64 20 62 79 20 61 20 74 65 73 74  imited by a test
2b34b 20 65 61 72 6c 79 20 69 6e 20 74 68 65 0a 2a 2a   early in the.**
2b34c 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
2b34d 69 6e 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 53  in() routine.  S
2b34e 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  o we know that t
2b34f 68 65 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b  he pMaskSet->ix[
2b350 5d 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c 6c 20  ].** array will 
2b351 6e 65 76 65 72 20 6f 76 65 72 66 6c 6f 77 2e 0a  never overflow..
2b352 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
2b353 72 65 61 74 65 4d 61 73 6b 28 57 68 65 72 65 4d  reateMask(WhereM
2b354 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
2b355 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a  , int iCursor){.
2b356 20 20 61 73 73 65 72 74 28 20 70 4d 61 73 6b 53    assert( pMaskS
2b357 65 74 2d 3e 6e 20 3c 20 41 72 72 61 79 53 69 7a  et->n < ArraySiz
2b358 65 28 70 4d 61 73 6b 53 65 74 2d 3e 69 78 29 20  e(pMaskSet->ix) 
2b359 29 3b 0a 20 20 70 4d 61 73 6b 53 65 74 2d 3e 69  );.  pMaskSet->i
2b35a 78 5b 70 4d 61 73 6b 53 65 74 2d 3e 6e 2b 2b 5d  x[pMaskSet->n++]
2b35b 20 3d 20 69 43 75 72 73 6f 72 3b 0a 7d 0a 0a 2f   = iCursor;.}../
2b35c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2b35d 65 20 77 61 6c 6b 73 20 28 72 65 63 75 72 73 69  e walks (recursi
2b35e 76 65 6c 79 29 20 61 6e 20 65 78 70 72 65 73 73  vely) an express
2b35f 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 67 65 6e  ion tree and gen
2b360 65 72 61 74 65 73 0a 2a 2a 20 61 20 62 69 74 6d  erates.** a bitm
2b361 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77  ask indicating w
2b362 68 69 63 68 20 74 61 62 6c 65 73 20 61 72 65 20  hich tables are 
2b363 75 73 65 64 20 69 6e 20 74 68 61 74 20 65 78 70  used in that exp
2b364 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e  ression.** tree.
2b365 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20  .**.** In order 
2b366 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  for this routine
2b367 20 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20 63 61   to work, the ca
2b368 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d  lling function m
2b369 75 73 74 20 68 61 76 65 0a 2a 2a 20 70 72 65 76  ust have.** prev
2b36a 69 6f 75 73 6c 79 20 69 6e 76 6f 6b 65 64 20 73  iously invoked s
2b36b 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2b36c 72 4e 61 6d 65 73 28 29 20 6f 6e 20 74 68 65 20  rNames() on the 
2b36d 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 53 65 65  expression.  See
2b36e 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 63  .** the header c
2b36f 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 61 74 20 72  omment on that r
2b370 6f 75 74 69 6e 65 20 66 6f 72 20 61 64 64 69 74  outine for addit
2b371 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2b372 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  n..** The sqlite
2b373 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2b374 73 28 29 20 72 6f 75 74 69 6e 65 73 20 6c 6f 6f  s() routines loo
2b375 6b 73 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61  ks for column na
2b376 6d 65 73 20 61 6e 64 0a 2a 2a 20 73 65 74 73 20  mes and.** sets 
2b377 74 68 65 69 72 20 6f 70 63 6f 64 65 73 20 74 6f  their opcodes to
2b378 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 74   TK_COLUMN and t
2b379 68 65 69 72 20 45 78 70 72 2e 69 54 61 62 6c 65  heir Expr.iTable
2b37a 20 66 69 65 6c 64 73 20 74 6f 0a 2a 2a 20 74 68   fields to.** th
2b37b 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
2b37c 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c  mber of the tabl
2b37d 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
2b37e 20 6a 75 73 74 20 68 61 73 20 74 6f 0a 2a 2a 20   just has to.** 
2b37f 74 72 61 6e 73 6c 61 74 65 20 74 68 65 20 63 75  translate the cu
2b380 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74  rsor numbers int
2b381 6f 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73  o bitmask values
2b382 20 61 6e 64 20 4f 52 20 61 6c 6c 0a 2a 2a 20 74   and OR all.** t
2b383 68 65 20 62 69 74 6d 61 73 6b 73 20 74 6f 67 65  he bitmasks toge
2b384 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ther..*/.static 
2b385 42 69 74 6d 61 73 6b 20 65 78 70 72 4c 69 73 74  Bitmask exprList
2b386 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65  TableUsage(Where
2b387 4d 61 73 6b 53 65 74 2a 2c 20 45 78 70 72 4c 69  MaskSet*, ExprLi
2b388 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 42 69 74  st*);.static Bit
2b389 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63 74 54  mask exprSelectT
2b38a 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d  ableUsage(WhereM
2b38b 61 73 6b 53 65 74 2a 2c 20 53 65 6c 65 63 74 2a  askSet*, Select*
2b38c 29 3b 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73  );.static Bitmas
2b38d 6b 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  k exprTableUsage
2b38e 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70  (WhereMaskSet *p
2b38f 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 20 2a 70  MaskSet, Expr *p
2b390 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61 73  ){.  Bitmask mas
2b391 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 3d 3d  k = 0;.  if( p==
2b392 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
2b393 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  if( p->op==TK_CO
2b394 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6d 61 73 6b  LUMN ){.    mask
2b395 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b   = getMask(pMask
2b396 53 65 74 2c 20 70 2d 3e 69 54 61 62 6c 65 29 3b  Set, p->iTable);
2b397 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 61 73 6b  .    return mask
2b398 3b 0a 20 20 7d 0a 20 20 6d 61 73 6b 20 3d 20 65  ;.  }.  mask = e
2b399 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
2b39a 61 73 6b 53 65 74 2c 20 70 2d 3e 70 52 69 67 68  askSet, p->pRigh
2b39b 74 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d 20 65 78  t);.  mask |= ex
2b39c 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
2b39d 73 6b 53 65 74 2c 20 70 2d 3e 70 4c 65 66 74 29  skSet, p->pLeft)
2b39e 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  ;.  if( ExprHasP
2b39f 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49  roperty(p, EP_xI
2b3a0 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
2b3a1 6d 61 73 6b 20 7c 3d 20 65 78 70 72 53 65 6c 65  mask |= exprSele
2b3a2 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  ctTableUsage(pMa
2b3a3 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 53 65 6c  skSet, p->x.pSel
2b3a4 65 63 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ect);.  }else{. 
2b3a5 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c     mask |= exprL
2b3a6 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  istTableUsage(pM
2b3a7 61 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 4c 69  askSet, p->x.pLi
2b3a8 73 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  st);.  }.  retur
2b3a9 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63  n mask;.}.static
2b3aa 20 42 69 74 6d 61 73 6b 20 65 78 70 72 4c 69 73   Bitmask exprLis
2b3ab 74 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72  tTableUsage(Wher
2b3ac 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
2b3ad 65 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  et, ExprList *pL
2b3ae 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ist){.  int i;. 
2b3af 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20   Bitmask mask = 
2b3b0 30 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29  0;.  if( pList )
2b3b1 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
2b3b2 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  <pList->nExpr; i
2b3b3 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20  ++){.      mask 
2b3b4 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67  |= exprTableUsag
2b3b5 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c 69 73  e(pMaskSet, pLis
2b3b6 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a  t->a[i].pExpr);.
2b3b7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2b3b8 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74 69  rn mask;.}.stati
2b3b9 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65  c Bitmask exprSe
2b3ba 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 57  lectTableUsage(W
2b3bb 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2b3bc 73 6b 53 65 74 2c 20 53 65 6c 65 63 74 20 2a 70  skSet, Select *p
2b3bd 53 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61  S){.  Bitmask ma
2b3be 73 6b 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  sk = 0;.  while(
2b3bf 20 70 53 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20   pS ){.    mask 
2b3c0 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65  |= exprListTable
2b3c1 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2b3c2 70 53 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  pS->pEList);.   
2b3c3 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73   mask |= exprLis
2b3c4 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
2b3c5 6b 53 65 74 2c 20 70 53 2d 3e 70 47 72 6f 75 70  kSet, pS->pGroup
2b3c6 42 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d  By);.    mask |=
2b3c7 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73   exprListTableUs
2b3c8 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53  age(pMaskSet, pS
2b3c9 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20  ->pOrderBy);.   
2b3ca 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62   mask |= exprTab
2b3cb 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2b3cc 2c 20 70 53 2d 3e 70 57 68 65 72 65 29 3b 0a 20  , pS->pWhere);. 
2b3cd 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54     mask |= exprT
2b3ce 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2b3cf 65 74 2c 20 70 53 2d 3e 70 48 61 76 69 6e 67 29  et, pS->pHaving)
2b3d0 3b 0a 20 20 20 20 70 53 20 3d 20 70 53 2d 3e 70  ;.    pS = pS->p
2b3d1 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74  Prior;.  }.  ret
2b3d2 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a  urn mask;.}../*.
2b3d3 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
2b3d4 66 20 74 68 65 20 67 69 76 65 6e 20 6f 70 65 72  f the given oper
2b3d5 61 74 6f 72 20 69 73 20 6f 6e 65 20 6f 66 20 74  ator is one of t
2b3d6 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61  he operators tha
2b3d7 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20  t is.** allowed 
2b3d8 66 6f 72 20 61 6e 20 69 6e 64 65 78 61 62 6c 65  for an indexable
2b3d9 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
2b3da 72 6d 2e 20 20 54 68 65 20 61 6c 6c 6f 77 65 64  rm.  The allowed
2b3db 20 6f 70 65 72 61 74 6f 72 73 20 61 72 65 0a 2a   operators are.*
2b3dc 2a 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3e 22 2c  * "=", "<", ">",
2b3dd 20 22 3c 3d 22 2c 20 22 3e 3d 22 2c 20 61 6e 64   "<=", ">=", and
2b3de 20 22 49 4e 22 2e 0a 2a 2f 0a 73 74 61 74 69 63   "IN"..*/.static
2b3df 20 69 6e 74 20 61 6c 6c 6f 77 65 64 4f 70 28 69   int allowedOp(i
2b3e0 6e 74 20 6f 70 29 7b 0a 20 20 61 73 73 65 72 74  nt op){.  assert
2b3e1 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 26 26  ( TK_GT>TK_EQ &&
2b3e2 20 54 4b 5f 47 54 3c 54 4b 5f 47 45 20 29 3b 0a   TK_GT<TK_GE );.
2b3e3 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3e    assert( TK_LT>
2b3e4 54 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 54 3c 54  TK_EQ && TK_LT<T
2b3e5 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74  K_GE );.  assert
2b3e6 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45 51 20 26 26  ( TK_LE>TK_EQ &&
2b3e7 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45 20 29 3b 0a   TK_LE<TK_GE );.
2b3e8 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d    assert( TK_GE=
2b3e9 3d 54 4b 5f 45 51 2b 34 20 29 3b 0a 20 20 72 65  =TK_EQ+4 );.  re
2b3ea 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f 49 4e 20 7c  turn op==TK_IN |
2b3eb 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51 20 26 26 20  | (op>=TK_EQ && 
2b3ec 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c 7c 20 6f 70  op<=TK_GE) || op
2b3ed 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b 0a 7d 0a 0a  ==TK_ISNULL;.}..
2b3ee 2f 2a 0a 2a 2a 20 53 77 61 70 20 74 77 6f 20 6f  /*.** Swap two o
2b3ef 62 6a 65 63 74 73 20 6f 66 20 74 79 70 65 20 54  bjects of type T
2b3f0 59 50 45 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  YPE..*/.#define 
2b3f1 53 57 41 50 28 54 59 50 45 2c 41 2c 42 29 20 7b  SWAP(TYPE,A,B) {
2b3f2 54 59 50 45 20 74 3d 41 3b 20 41 3d 42 3b 20 42  TYPE t=A; A=B; B
2b3f3 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d  =t;}../*.** Comm
2b3f4 75 74 65 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e  ute a comparison
2b3f5 20 6f 70 65 72 61 74 6f 72 2e 20 20 45 78 70 72   operator.  Expr
2b3f6 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66  essions of the f
2b3f7 6f 72 6d 20 22 58 20 6f 70 20 59 22 0a 2a 2a 20  orm "X op Y".** 
2b3f8 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e  are converted in
2b3f9 74 6f 20 22 59 20 6f 70 20 58 22 2e 0a 2a 2a 0a  to "Y op X"..**.
2b3fa 2a 2a 20 49 66 20 61 20 63 6f 6c 6c 61 74 69 6f  ** If a collatio
2b3fb 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 73  n sequence is as
2b3fc 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 69  sociated with ei
2b3fd 74 68 65 72 20 74 68 65 20 6c 65 66 74 20 6f 72  ther the left or
2b3fe 20 72 69 67 68 74 0a 2a 2a 20 73 69 64 65 20 6f   right.** side o
2b3ff 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  f the comparison
2b400 2c 20 69 74 20 72 65 6d 61 69 6e 73 20 61 73 73  , it remains ass
2b401 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
2b402 20 73 61 6d 65 20 73 69 64 65 20 61 66 74 65 72   same side after
2b403 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 75 74 61 74  .** the commutat
2b404 69 6f 6e 2e 20 53 6f 20 22 59 20 63 6f 6c 6c 61  ion. So "Y colla
2b405 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 58 22 20  te NOCASE op X" 
2b406 62 65 63 6f 6d 65 73 20 0a 2a 2a 20 22 58 20 63  becomes .** "X c
2b407 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70  ollate NOCASE op
2b408 20 59 22 2e 20 54 68 69 73 20 69 73 20 62 65 63   Y". This is bec
2b409 61 75 73 65 20 61 6e 79 20 63 6f 6c 6c 61 74 69  ause any collati
2b40a 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 6e 0a 2a  on sequence on.*
2b40b 2a 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20  * the left hand 
2b40c 73 69 64 65 20 6f 66 20 61 20 63 6f 6d 70 61 72  side of a compar
2b40d 69 73 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61  ison overrides a
2b40e 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ny collation seq
2b40f 75 65 6e 63 65 20 0a 2a 2a 20 61 74 74 61 63 68  uence .** attach
2b410 65 64 20 74 6f 20 74 68 65 20 72 69 67 68 74 2e  ed to the right.
2b411 20 46 6f 72 20 74 68 65 20 73 61 6d 65 20 72 65   For the same re
2b412 61 73 6f 6e 20 74 68 65 20 45 50 5f 45 78 70 43  ason the EP_ExpC
2b413 6f 6c 6c 61 74 65 20 66 6c 61 67 0a 2a 2a 20 69  ollate flag.** i
2b414 73 20 6e 6f 74 20 63 6f 6d 6d 75 74 65 64 2e 0a  s not commuted..
2b415 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65  */.static void e
2b416 78 70 72 43 6f 6d 6d 75 74 65 28 50 61 72 73 65  xprCommute(Parse
2b417 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
2b418 70 45 78 70 72 29 7b 0a 20 20 75 31 36 20 65 78  pExpr){.  u16 ex
2b419 70 52 69 67 68 74 20 3d 20 28 70 45 78 70 72 2d  pRight = (pExpr-
2b41a 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26  >pRight->flags &
2b41b 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 3b   EP_ExpCollate);
2b41c 0a 20 20 75 31 36 20 65 78 70 4c 65 66 74 20 3d  .  u16 expLeft =
2b41d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e   (pExpr->pLeft->
2b41e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
2b41f 6c 6c 61 74 65 29 3b 0a 20 20 61 73 73 65 72 74  llate);.  assert
2b420 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 45 78 70  ( allowedOp(pExp
2b421 72 2d 3e 6f 70 29 20 26 26 20 70 45 78 70 72 2d  r->op) && pExpr-
2b422 3e 6f 70 21 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20  >op!=TK_IN );.  
2b423 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 70  pExpr->pRight->p
2b424 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
2b425 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
2b426 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
2b427 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  ;.  pExpr->pLeft
2b428 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65  ->pColl = sqlite
2b429 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
2b42a 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
2b42b 74 29 3b 0a 20 20 53 57 41 50 28 43 6f 6c 6c 53  t);.  SWAP(CollS
2b42c 65 71 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68  eq*,pExpr->pRigh
2b42d 74 2d 3e 70 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e  t->pColl,pExpr->
2b42e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 29 3b 0a 20  pLeft->pColl);. 
2b42f 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e   pExpr->pRight->
2b430 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e  flags = (pExpr->
2b431 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20  pRight->flags & 
2b432 7e 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20  ~EP_ExpCollate) 
2b433 7c 20 65 78 70 4c 65 66 74 3b 0a 20 20 70 45 78  | expLeft;.  pEx
2b434 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73  pr->pLeft->flags
2b435 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74   = (pExpr->pLeft
2b436 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f 45 78  ->flags & ~EP_Ex
2b437 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78 70 52  pCollate) | expR
2b438 69 67 68 74 3b 0a 20 20 53 57 41 50 28 45 78 70  ight;.  SWAP(Exp
2b439 72 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74  r*,pExpr->pRight
2b43a 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a  ,pExpr->pLeft);.
2b43b 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3e    if( pExpr->op>
2b43c 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20 61 73  =TK_GT ){.    as
2b43d 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f  sert( TK_LT==TK_
2b43e 47 54 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65  GT+2 );.    asse
2b43f 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 4c 45  rt( TK_GE==TK_LE
2b440 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +2 );.    assert
2b441 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 29 3b  ( TK_GT>TK_EQ );
2b442 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f  .    assert( TK_
2b443 47 54 3c 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20  GT<TK_LE );.    
2b444 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
2b445 70 3e 3d 54 4b 5f 47 54 20 26 26 20 70 45 78 70  p>=TK_GT && pExp
2b446 72 2d 3e 6f 70 3c 3d 54 4b 5f 47 45 20 29 3b 0a  r->op<=TK_GE );.
2b447 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
2b448 28 28 70 45 78 70 72 2d 3e 6f 70 2d 54 4b 5f 47  ((pExpr->op-TK_G
2b449 54 29 5e 32 29 2b 54 4b 5f 47 54 3b 0a 20 20 7d  T)^2)+TK_GT;.  }
2b44a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c  .}../*.** Transl
2b44b 61 74 65 20 66 72 6f 6d 20 54 4b 5f 78 78 20 6f  ate from TK_xx o
2b44c 70 65 72 61 74 6f 72 20 74 6f 20 57 4f 5f 78 78  perator to WO_xx
2b44d 20 62 69 74 6d 61 73 6b 2e 0a 2a 2f 0a 73 74 61   bitmask..*/.sta
2b44e 74 69 63 20 75 31 36 20 6f 70 65 72 61 74 6f 72  tic u16 operator
2b44f 4d 61 73 6b 28 69 6e 74 20 6f 70 29 7b 0a 20 20  Mask(int op){.  
2b450 75 31 36 20 63 3b 0a 20 20 61 73 73 65 72 74 28  u16 c;.  assert(
2b451 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 29   allowedOp(op) )
2b452 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49  ;.  if( op==TK_I
2b453 4e 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f  N ){.    c = WO_
2b454 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  IN;.  }else if( 
2b455 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b  op==TK_ISNULL ){
2b456 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 53 4e 55  .    c = WO_ISNU
2b457 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  LL;.  }else{.   
2b458 20 61 73 73 65 72 74 28 20 28 57 4f 5f 45 51 3c   assert( (WO_EQ<
2b459 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 20 3c 20 30  <(op-TK_EQ)) < 0
2b45a 78 37 66 66 66 20 29 3b 0a 20 20 20 20 63 20 3d  x7fff );.    c =
2b45b 20 28 75 31 36 29 28 57 4f 5f 45 51 3c 3c 28 6f   (u16)(WO_EQ<<(o
2b45c 70 2d 54 4b 5f 45 51 29 29 3b 0a 20 20 7d 0a 20  p-TK_EQ));.  }. 
2b45d 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2b45e 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d 3d 57 4f 5f  ISNULL || c==WO_
2b45f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65  ISNULL );.  asse
2b460 72 74 28 20 6f 70 21 3d 54 4b 5f 49 4e 20 7c 7c  rt( op!=TK_IN ||
2b461 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 61   c==WO_IN );.  a
2b462 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 45 51  ssert( op!=TK_EQ
2b463 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51 20 29 3b 0a   || c==WO_EQ );.
2b464 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b    assert( op!=TK
2b465 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 54 20  _LT || c==WO_LT 
2b466 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21  );.  assert( op!
2b467 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d 3d 57 4f 5f  =TK_LE || c==WO_
2b468 4c 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LE );.  assert( 
2b469 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 63 3d 3d  op!=TK_GT || c==
2b46a 57 4f 5f 47 54 20 29 3b 0a 20 20 61 73 73 65 72  WO_GT );.  asser
2b46b 74 28 20 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20  t( op!=TK_GE || 
2b46c 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20 20 72 65  c==WO_GE );.  re
2b46d 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn c;.}../*.**
2b46e 20 53 65 61 72 63 68 20 66 6f 72 20 61 20 74 65   Search for a te
2b46f 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  rm in the WHERE 
2b470 63 6c 61 75 73 65 20 74 68 61 74 20 69 73 20 6f  clause that is o
2b471 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f  f the form "X <o
2b472 70 3e 20 3c 65 78 70 72 3e 22 0a 2a 2a 20 77 68  p> <expr>".** wh
2b473 65 72 65 20 58 20 69 73 20 61 20 72 65 66 65 72  ere X is a refer
2b474 65 6e 63 65 20 74 6f 20 74 68 65 20 69 43 6f 6c  ence to the iCol
2b475 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 69 43 75  umn of table iCu
2b476 72 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e  r and <op> is on
2b477 65 20 6f 66 0a 2a 2a 20 74 68 65 20 57 4f 5f 78  e of.** the WO_x
2b478 78 20 6f 70 65 72 61 74 6f 72 20 63 6f 64 65 73  x operator codes
2b479 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
2b47a 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 0a  e op parameter..
2b47b 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
2b47c 74 65 72 20 74 6f 20 74 68 65 20 74 65 72 6d 2e  ter to the term.
2b47d 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f    Return 0 if no
2b47e 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74  t found..*/.stat
2b47f 69 63 20 57 68 65 72 65 54 65 72 6d 20 2a 66 69  ic WhereTerm *fi
2b480 6e 64 54 65 72 6d 28 0a 20 20 57 68 65 72 65 43  ndTerm(.  WhereC
2b481 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20  lause *pWC,     
2b482 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2b483 75 73 65 20 74 6f 20 62 65 20 73 65 61 72 63 68  use to be search
2b484 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  ed */.  int iCur
2b485 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2b486 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
2b487 66 20 4c 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69  f LHS */.  int i
2b488 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20  Column,         
2b489 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65   /* Column numbe
2b48a 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 42 69  r of LHS */.  Bi
2b48b 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20  tmask notReady, 
2b48c 20 20 20 20 2f 2a 20 52 48 53 20 6d 75 73 74 20      /* RHS must 
2b48d 6e 6f 74 20 6f 76 65 72 6c 61 70 20 77 69 74 68  not overlap with
2b48e 20 74 68 69 73 20 6d 61 73 6b 20 2a 2f 0a 20 20   this mask */.  
2b48f 75 33 32 20 6f 70 2c 20 20 20 20 20 20 20 20 20  u32 op,         
2b490 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2b491 20 57 4f 5f 78 78 20 76 61 6c 75 65 73 20 64 65   WO_xx values de
2b492 73 63 72 69 62 69 6e 67 20 6f 70 65 72 61 74 6f  scribing operato
2b493 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  r */.  Index *pI
2b494 64 78 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx           /* 
2b495 4d 75 73 74 20 62 65 20 63 6f 6d 70 61 74 69 62  Must be compatib
2b496 6c 65 20 77 69 74 68 20 74 68 69 73 20 69 6e 64  le with this ind
2b497 65 78 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20  ex, if not NULL 
2b498 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72  */.){.  WhereTer
2b499 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20  m *pTerm;.  int 
2b49a 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 43 75  k;.  assert( iCu
2b49b 72 3e 3d 30 20 29 3b 0a 20 20 6f 70 20 26 3d 20  r>=0 );.  op &= 
2b49c 57 4f 5f 41 4c 4c 3b 0a 20 20 66 6f 72 28 70 54  WO_ALL;.  for(pT
2b49d 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6b 3d 70 57  erm=pWC->a, k=pW
2b49e 43 2d 3e 6e 54 65 72 6d 3b 20 6b 3b 20 6b 2d 2d  C->nTerm; k; k--
2b49f 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  , pTerm++){.    
2b4a0 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43  if( pTerm->leftC
2b4a1 75 72 73 6f 72 3d 3d 69 43 75 72 0a 20 20 20 20  ursor==iCur.    
2b4a2 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 70 72     && (pTerm->pr
2b4a3 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52  ereqRight & notR
2b4a4 65 61 64 79 29 3d 3d 30 0a 20 20 20 20 20 20 20  eady)==0.       
2b4a5 26 26 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74  && pTerm->u.left
2b4a6 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a  Column==iColumn.
2b4a7 20 20 20 20 20 20 20 26 26 20 28 70 54 65 72 6d         && (pTerm
2b4a8 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 6f 70  ->eOperator & op
2b4a9 29 21 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20  )!=0.    ){.    
2b4aa 20 20 69 66 28 20 70 49 64 78 20 26 26 20 70 54    if( pIdx && pT
2b4ab 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d  erm->eOperator!=
2b4ac 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20  WO_ISNULL ){.   
2b4ad 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20       Expr *pX = 
2b4ae 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
2b4af 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
2b4b0 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 63 68  Coll;.        ch
2b4b1 61 72 20 69 64 78 61 66 66 3b 0a 20 20 20 20 20  ar idxaff;.     
2b4b2 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2b4b3 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20    Parse *pParse 
2b4b4 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 0a 0a  = pWC->pParse;..
2b4b5 20 20 20 20 20 20 20 20 69 64 78 61 66 66 20 3d          idxaff =
2b4b6 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 61   pIdx->pTable->a
2b4b7 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66  Col[iColumn].aff
2b4b8 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 69  inity;.        i
2b4b9 66 28 20 21 73 71 6c 69 74 65 33 49 6e 64 65 78  f( !sqlite3Index
2b4ba 41 66 66 69 6e 69 74 79 4f 6b 28 70 58 2c 20 69  AffinityOk(pX, i
2b4bb 64 78 61 66 66 29 20 29 20 63 6f 6e 74 69 6e 75  dxaff) ) continu
2b4bc 65 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 46  e;..        /* F
2b4bd 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f  igure out the co
2b4be 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2b4bf 20 72 65 71 75 69 72 65 64 20 66 72 6f 6d 20 61   required from a
2b4c0 6e 20 69 6e 64 65 78 20 66 6f 72 0a 20 20 20 20  n index for.    
2b4c1 20 20 20 20 2a 2a 20 69 74 20 74 6f 20 62 65 20      ** it to be 
2b4c2 75 73 65 66 75 6c 20 66 6f 72 20 6f 70 74 69 6d  useful for optim
2b4c3 69 73 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e  ising expression
2b4c4 20 70 58 2e 20 53 74 6f 72 65 20 74 68 69 73 0a   pX. Store this.
2b4c5 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65          ** value
2b4c6 20 69 6e 20 76 61 72 69 61 62 6c 65 20 70 43 6f   in variable pCo
2b4c7 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ll..        */. 
2b4c8 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 58         assert(pX
2b4c9 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  ->pLeft);.      
2b4ca 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2b4cb 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f  3BinaryCompareCo
2b4cc 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58  llSeq(pParse, pX
2b4cd 2d 3e 70 4c 65 66 74 2c 20 70 58 2d 3e 70 52 69  ->pLeft, pX->pRi
2b4ce 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73  ght);.        as
2b4cf 73 65 72 74 28 70 43 6f 6c 6c 20 7c 7c 20 70 50  sert(pColl || pP
2b4d0 61 72 73 65 2d 3e 6e 45 72 72 29 3b 0a 0a 20 20  arse->nErr);..  
2b4d1 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 70        for(j=0; p
2b4d2 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
2b4d3 21 3d 69 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b  !=iColumn; j++){
2b4d4 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 4e  .          if( N
2b4d5 45 56 45 52 28 6a 3e 3d 70 49 64 78 2d 3e 6e 43  EVER(j>=pIdx->nC
2b4d6 6f 6c 75 6d 6e 29 20 29 20 72 65 74 75 72 6e 20  olumn) ) return 
2b4d7 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
2b4d8 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 26       if( pColl &
2b4d9 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
2b4da 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 70  (pColl->zName, p
2b4db 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d 29 20  Idx->azColl[j]) 
2b4dc 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2b4dd 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
2b4de 20 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20   pTerm;.    }.  
2b4df 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2b4e0 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
2b4e1 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20  rence */.static 
2b4e2 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65  void exprAnalyze
2b4e3 28 53 72 63 4c 69 73 74 2a 2c 20 57 68 65 72 65  (SrcList*, Where
2b4e4 43 6c 61 75 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a  Clause*, int);..
2b4e5 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 65 78 70 72 41  /*.** Call exprA
2b4e6 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c 6c 20 74 65  nalyze on all te
2b4e7 72 6d 73 20 69 6e 20 61 20 57 48 45 52 45 20 63  rms in a WHERE c
2b4e8 6c 61 75 73 65 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a  lause.  .**.**.*
2b4e9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  /.static void ex
2b4ea 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 0a 20 20  prAnalyzeAll(.  
2b4eb 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
2b4ec 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20  t,       /* the 
2b4ed 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
2b4ee 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2b4ef 43 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  C         /* the
2b4f0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f   WHERE clause to
2b4f1 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
2b4f2 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
2b4f3 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d 31  r(i=pWC->nTerm-1
2b4f4 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20  ; i>=0; i--){.  
2b4f5 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 54    exprAnalyze(pT
2b4f6 61 62 4c 69 73 74 2c 20 70 57 43 2c 20 69 29 3b  abList, pWC, i);
2b4f7 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
2b4f8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45  SQLITE_OMIT_LIKE
2b4f9 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 2f 2a  _OPTIMIZATION./*
2b4fa 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
2b4fb 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 65 78   if the given ex
2b4fc 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 4c 49  pression is a LI
2b4fd 4b 45 20 6f 72 20 47 4c 4f 42 20 6f 70 65 72 61  KE or GLOB opera
2b4fe 74 6f 72 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20  tor that.** can 
2b4ff 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 75 73 69  be optimized usi
2b500 6e 67 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  ng inequality co
2b501 6e 73 74 72 61 69 6e 74 73 2e 20 20 52 65 74 75  nstraints.  Retu
2b502 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73  rn TRUE if it is
2b503 0a 2a 2a 20 73 6f 20 61 6e 64 20 66 61 6c 73 65  .** so and false
2b504 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49   if not..**.** I
2b505 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 65 20  n order for the 
2b506 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f  operator to be o
2b507 70 74 69 6d 69 7a 69 62 6c 65 2c 20 74 68 65 20  ptimizible, the 
2b508 52 48 53 20 6d 75 73 74 20 62 65 20 61 20 73 74  RHS must be a st
2b509 72 69 6e 67 0a 2a 2a 20 6c 69 74 65 72 61 6c 20  ring.** literal 
2b50a 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 62 65  that does not be
2b50b 67 69 6e 20 77 69 74 68 20 61 20 77 69 6c 64 63  gin with a wildc
2b50c 61 72 64 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63  ard.  .*/.static
2b50d 20 69 6e 74 20 69 73 4c 69 6b 65 4f 72 47 6c 6f   int isLikeOrGlo
2b50e 62 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  b(.  Parse *pPar
2b50f 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
2b510 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
2b511 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
2b512 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
2b513 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68 69       /* Test thi
2b514 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  s expression */.
2b515 20 20 69 6e 74 20 2a 70 6e 50 61 74 74 65 72 6e    int *pnPattern
2b516 2c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ,   /* Number of
2b517 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20 70 72   non-wildcard pr
2b518 65 66 69 78 20 63 68 61 72 61 63 74 65 72 73 20  efix characters 
2b519 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 73 43 6f 6d  */.  int *pisCom
2b51a 70 6c 65 74 65 2c 20 2f 2a 20 54 72 75 65 20 69  plete, /* True i
2b51b 66 20 74 68 65 20 6f 6e 6c 79 20 77 69 6c 64 63  f the only wildc
2b51c 61 72 64 20 69 73 20 25 20 69 6e 20 74 68 65 20  ard is % in the 
2b51d 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20 2a  last character *
2b51e 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f 43 61 73 65  /.  int *pnoCase
2b51f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2b520 20 75 70 70 65 72 63 61 73 65 20 69 73 20 65 71   uppercase is eq
2b521 75 69 76 61 6c 65 6e 74 20 74 6f 20 6c 6f 77 65  uivalent to lowe
2b522 72 63 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f  rcase */.){.  co
2b523 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  nst char *z;    
2b524 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
2b525 6e 67 20 6f 6e 20 52 48 53 20 6f 66 20 4c 49 4b  ng on RHS of LIK
2b526 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  E operator */.  
2b527 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70  Expr *pRight, *p
2b528 4c 65 66 74 3b 20 20 20 20 20 20 2f 2a 20 52 69  Left;      /* Ri
2b529 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73 69 7a  ght and left siz
2b52a 65 20 6f 66 20 4c 49 4b 45 20 6f 70 65 72 61 74  e of LIKE operat
2b52b 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  or */.  ExprList
2b52c 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20 20 20   *pList;        
2b52d 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 70     /* List of op
2b52e 65 72 61 6e 64 73 20 74 6f 20 74 68 65 20 4c 49  erands to the LI
2b52f 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  KE operator */. 
2b530 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20 20 20   int c;         
2b531 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
2b532 6e 65 20 63 68 61 72 61 63 74 65 72 20 69 6e 20  ne character in 
2b533 7a 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20  z[] */.  int n; 
2b534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b535 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
2b536 20 73 74 72 69 6e 67 20 7a 5b 5d 20 2a 2f 0a 20   string z[] */. 
2b537 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20   int cnt;       
2b538 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2b539 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 77 69 6c  umber of non-wil
2b53a 64 63 61 72 64 20 70 72 65 66 69 78 20 63 68 61  dcard prefix cha
2b53b 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 63 68 61  racters */.  cha
2b53c 72 20 77 63 5b 33 5d 3b 20 20 20 20 20 20 20 20  r wc[3];        
2b53d 20 20 20 20 20 20 20 20 2f 2a 20 57 69 6c 64 63          /* Wildc
2b53e 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20 2a  ard characters *
2b53f 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  /.  CollSeq *pCo
2b540 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ll;            /
2b541 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
2b542 65 6e 63 65 20 66 6f 72 20 4c 48 53 20 2a 2f 0a  ence for LHS */.
2b543 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2b544 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20  pParse->db;  /* 
2b545 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2b546 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 21 73  ion */..  if( !s
2b547 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63  qlite3IsLikeFunc
2b548 74 69 6f 6e 28 64 62 2c 20 70 45 78 70 72 2c 20  tion(db, pExpr, 
2b549 70 6e 6f 43 61 73 65 2c 20 77 63 29 20 29 7b 0a  pnoCase, wc) ){.
2b54a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2b54b 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
2b54c 45 42 43 44 49 43 0a 20 20 69 66 28 20 2a 70 6e  EBCDIC.  if( *pn
2b54d 6f 43 61 73 65 20 29 20 72 65 74 75 72 6e 20 30  oCase ) return 0
2b54e 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4c 69 73 74  ;.#endif.  pList
2b54f 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
2b550 74 3b 0a 20 20 70 52 69 67 68 74 20 3d 20 70 4c  t;.  pRight = pL
2b551 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b  ist->a[0].pExpr;
2b552 0a 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6f  .  if( pRight->o
2b553 70 21 3d 54 4b 5f 53 54 52 49 4e 47 20 29 7b 0a  p!=TK_STRING ){.
2b554 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2b555 7d 0a 20 20 70 4c 65 66 74 20 3d 20 70 4c 69 73  }.  pLeft = pLis
2b556 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20  t->a[1].pExpr;. 
2b557 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 21 3d   if( pLeft->op!=
2b558 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  TK_COLUMN ){.   
2b559 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2b55a 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
2b55b 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
2b55c 73 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 61 73  se, pLeft);.  as
2b55d 73 65 72 74 28 20 70 43 6f 6c 6c 21 3d 30 20 7c  sert( pColl!=0 |
2b55e 7c 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e  | pLeft->iColumn
2b55f 3d 3d 2d 31 20 29 3b 0a 20 20 69 66 28 20 70 43  ==-1 );.  if( pC
2b560 6f 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  oll==0 ){.    /*
2b561 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73   No collation is
2b562 20 64 65 66 69 6e 65 64 20 66 6f 72 20 74 68 65   defined for the
2b563 20 52 4f 57 49 44 2e 20 20 55 73 65 20 74 68 65   ROWID.  Use the
2b564 20 64 65 66 61 75 6c 74 2e 20 2a 2f 0a 20 20 20   default. */.   
2b565 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   pColl = db->pDf
2b566 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66  ltColl;.  }.  if
2b567 28 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d  ( (pColl->type!=
2b568 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41  SQLITE_COLL_BINA
2b569 52 59 20 7c 7c 20 2a 70 6e 6f 43 61 73 65 29 20  RY || *pnoCase) 
2b56a 26 26 0a 20 20 20 20 20 20 28 70 43 6f 6c 6c 2d  &&.      (pColl-
2b56b 3e 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 43 4f  >type!=SQLITE_CO
2b56c 4c 4c 5f 4e 4f 43 41 53 45 20 7c 7c 20 21 2a 70  LL_NOCASE || !*p
2b56d 6e 6f 43 61 73 65 29 20 29 7b 0a 20 20 20 20 72  noCase) ){.    r
2b56e 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a  eturn 0;.  }.  z
2b56f 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
2b570 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 3b  pRight->token.z;
2b571 0a 20 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 66  .  cnt = 0;.  if
2b572 28 20 7a 20 29 7b 0a 20 20 20 20 6e 20 3d 20 70  ( z ){.    n = p
2b573 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a  Right->token.n;.
2b574 20 20 20 20 77 68 69 6c 65 28 20 63 6e 74 3c 6e      while( cnt<n
2b575 20 26 26 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d   && (c=z[cnt])!=
2b576 30 20 26 26 20 63 21 3d 77 63 5b 30 5d 20 26 26  0 && c!=wc[0] &&
2b577 20 63 21 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d   c!=wc[1] && c!=
2b578 77 63 5b 32 5d 20 29 7b 0a 20 20 20 20 20 20 63  wc[2] ){.      c
2b579 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  nt++;.    }.  }.
2b57a 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 7c 7c 20    if( cnt==0 || 
2b57b 32 35 35 3d 3d 28 75 38 29 7a 5b 63 6e 74 2d 31  255==(u8)z[cnt-1
2b57c 5d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ] ){.    return 
2b57d 30 3b 0a 20 20 7d 0a 20 20 2a 70 69 73 43 6f 6d  0;.  }.  *pisCom
2b57e 70 6c 65 74 65 20 3d 20 7a 5b 63 6e 74 5d 3d 3d  plete = z[cnt]==
2b57f 77 63 5b 30 5d 20 26 26 20 7a 5b 63 6e 74 2b 31  wc[0] && z[cnt+1
2b580 5d 3d 3d 30 3b 0a 20 20 2a 70 6e 50 61 74 74 65  ]==0;.  *pnPatte
2b581 72 6e 20 3d 20 63 6e 74 3b 0a 20 20 72 65 74 75  rn = cnt;.  retu
2b582 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 1;.}.#endif /
2b583 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49  * SQLITE_OMIT_LI
2b584 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  KE_OPTIMIZATION 
2b585 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
2b586 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2b587 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43 68 65 63  TABLE./*.** Chec
2b588 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
2b589 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e  given expression
2b58a 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a   is of the form.
2b58b 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f  **.**         co
2b58c 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 0a  lumn MATCH expr.
2b58d 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 74  **.** If it is t
2b58e 68 65 6e 20 72 65 74 75 72 6e 20 54 52 55 45 2e  hen return TRUE.
2b58f 20 20 49 66 20 6e 6f 74 2c 20 72 65 74 75 72 6e    If not, return
2b590 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61 74 69   FALSE..*/.stati
2b591 63 20 69 6e 74 20 69 73 4d 61 74 63 68 4f 66 43  c int isMatchOfC
2b592 6f 6c 75 6d 6e 28 0a 20 20 45 78 70 72 20 2a 70  olumn(.  Expr *p
2b593 45 78 70 72 20 20 20 20 20 20 2f 2a 20 54 65 73  Expr      /* Tes
2b594 74 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  t this expressio
2b595 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69  n */.){.  ExprLi
2b596 73 74 20 2a 70 4c 69 73 74 3b 0a 0a 20 20 69 66  st *pList;..  if
2b597 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
2b598 46 55 4e 43 54 49 4f 4e 20 29 7b 0a 20 20 20 20  FUNCTION ){.    
2b599 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2b59a 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  if( pExpr->token
2b59b 2e 6e 21 3d 35 20 7c 7c 0a 20 20 20 20 20 20 20  .n!=5 ||.       
2b59c 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2b59d 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 45 78  (const char*)pEx
2b59e 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 22 6d 61 74  pr->token.z,"mat
2b59f 63 68 22 2c 35 29 21 3d 30 20 29 7b 0a 20 20 20  ch",5)!=0 ){.   
2b5a0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2b5a1 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e   pList = pExpr->
2b5a2 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  x.pList;.  if( p
2b5a3 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 20 29  List->nExpr!=2 )
2b5a4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2b5a5 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d    }.  if( pList-
2b5a6 3e 61 5b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 20  >a[1].pExpr->op 
2b5a7 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  != TK_COLUMN ){.
2b5a8 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2b5a9 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
2b5aa 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2b5ab 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2b5ac 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  LE */../*.** If 
2b5ad 74 68 65 20 70 42 61 73 65 20 65 78 70 72 65 73  the pBase expres
2b5ae 73 69 6f 6e 20 6f 72 69 67 69 6e 61 74 65 64 20  sion originated 
2b5af 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  in the ON or USI
2b5b0 4e 47 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20  NG clause of.** 
2b5b1 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 72 61  a join, then tra
2b5b2 6e 73 66 65 72 20 74 68 65 20 61 70 70 72 6f 70  nsfer the approp
2b5b3 72 69 61 74 65 20 6d 61 72 6b 69 6e 67 73 20 6f  riate markings o
2b5b4 76 65 72 20 74 6f 20 64 65 72 69 76 65 64 2e 0a  ver to derived..
2b5b5 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
2b5b6 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69  ransferJoinMarki
2b5b7 6e 67 73 28 45 78 70 72 20 2a 70 44 65 72 69 76  ngs(Expr *pDeriv
2b5b8 65 64 2c 20 45 78 70 72 20 2a 70 42 61 73 65 29  ed, Expr *pBase)
2b5b9 7b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 66 6c  {.  pDerived->fl
2b5ba 61 67 73 20 7c 3d 20 70 42 61 73 65 2d 3e 66 6c  ags |= pBase->fl
2b5bb 61 67 73 20 26 20 45 50 5f 46 72 6f 6d 4a 6f 69  ags & EP_FromJoi
2b5bc 6e 3b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e 69  n;.  pDerived->i
2b5bd 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
2b5be 20 70 42 61 73 65 2d 3e 69 52 69 67 68 74 4a 6f   pBase->iRightJo
2b5bf 69 6e 54 61 62 6c 65 3b 0a 7d 0a 0a 23 69 66 20  inTable;.}..#if 
2b5c0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2b5c1 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2b5c2 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e 65  TION) && !define
2b5c3 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
2b5c4 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a 20 41 6e  BQUERY)./*.** An
2b5c5 61 6c 79 7a 65 20 61 20 74 65 72 6d 20 74 68 61  alyze a term tha
2b5c6 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77  t consists of tw
2b5c7 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63 6f 6e  o or more OR-con
2b5c8 6e 65 63 74 65 64 0a 2a 2a 20 73 75 62 74 65 72  nected.** subter
2b5c9 6d 73 2e 20 20 53 6f 20 69 6e 3a 0a 2a 2a 0a 2a  ms.  So in:.**.*
2b5ca 2a 20 20 20 20 20 2e 2e 2e 20 57 48 45 52 45 20  *     ... WHERE 
2b5cb 20 28 61 3d 35 29 20 41 4e 44 20 28 62 3d 37 20   (a=5) AND (b=7 
2b5cc 4f 52 20 63 3d 39 20 4f 52 20 64 3d 31 33 29 20  OR c=9 OR d=13) 
2b5cd 41 4e 44 20 28 64 3d 31 33 29 0a 2a 2a 20 20 20  AND (d=13).**   
2b5ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5cf 20 20 20 20 20 20 20 5e 5e 5e 5e 5e 5e 5e 5e 5e         ^^^^^^^^^
2b5d0 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 0a 2a 2a 0a 2a  ^^^^^^^^^^^.**.*
2b5d1 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
2b5d2 6e 61 6c 79 7a 65 73 20 74 65 72 6d 73 20 73 75  nalyzes terms su
2b5d3 63 68 20 61 73 20 74 68 65 20 6d 69 64 64 6c 65  ch as the middle
2b5d4 20 74 65 72 6d 20 69 6e 20 74 68 65 20 61 62 6f   term in the abo
2b5d5 76 65 20 65 78 61 6d 70 6c 65 2e 0a 2a 2a 20 41  ve example..** A
2b5d6 20 57 68 65 72 65 4f 72 54 65 72 6d 20 6f 62 6a   WhereOrTerm obj
2b5d7 65 63 74 20 69 73 20 63 6f 6d 70 75 74 65 64 20  ect is computed 
2b5d8 61 6e 64 20 61 74 74 61 63 68 65 64 20 74 6f 20  and attached to 
2b5d9 74 68 65 20 74 65 72 6d 20 75 6e 64 65 72 0a 2a  the term under.*
2b5da 2a 20 61 6e 61 6c 79 73 69 73 2c 20 72 65 67 61  * analysis, rega
2b5db 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6f 75  rdless of the ou
2b5dc 74 63 6f 6d 65 20 6f 66 20 74 68 65 20 61 6e 61  tcome of the ana
2b5dd 6c 79 73 69 73 2e 20 20 48 65 6e 63 65 3a 0a 2a  lysis.  Hence:.*
2b5de 2a 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65  *.**     WhereTe
2b5df 72 6d 2e 77 74 46 6c 61 67 73 20 20 20 7c 3d 20  rm.wtFlags   |= 
2b5e0 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 0a 2a 2a 20   TERM_ORINFO.** 
2b5e1 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 75 2e      WhereTerm.u.
2b5e2 70 4f 72 49 6e 66 6f 20 20 3d 20 20 61 20 64 79  pOrInfo  =  a dy
2b5e3 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
2b5e4 74 65 64 20 57 68 65 72 65 4f 72 54 65 72 6d 20  ted WhereOrTerm 
2b5e5 6f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65  object.**.** The
2b5e6 20 74 65 72 6d 20 62 65 69 6e 67 20 61 6e 61 6c   term being anal
2b5e7 79 7a 65 64 20 6d 75 73 74 20 68 61 76 65 20 74  yzed must have t
2b5e8 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 66 20 4f 52  wo or more of OR
2b5e9 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62 74 65  -connected subte
2b5ea 72 6d 73 2e 0a 2a 2a 20 41 20 73 69 6e 67 6c 65  rms..** A single
2b5eb 20 73 75 62 74 65 72 6d 20 6d 69 67 68 74 20 62   subterm might b
2b5ec 65 20 61 20 73 65 74 20 6f 66 20 41 4e 44 2d 63  e a set of AND-c
2b5ed 6f 6e 6e 65 63 74 65 64 20 73 75 62 2d 73 75 62  onnected sub-sub
2b5ee 74 65 72 6d 73 2e 0a 2a 2a 20 45 78 61 6d 70 6c  terms..** Exampl
2b5ef 65 73 20 6f 66 20 74 65 72 6d 73 20 75 6e 64 65  es of terms unde
2b5f0 72 20 61 6e 61 6c 79 73 69 73 3a 0a 2a 2a 0a 2a  r analysis:.**.*
2b5f1 2a 20 20 20 20 20 28 41 29 20 20 20 20 20 74 31  *     (A)     t1
2b5f2 2e 78 3d 74 32 2e 79 20 4f 52 20 74 31 2e 78 3d  .x=t2.y OR t1.x=
2b5f3 74 32 2e 7a 20 4f 52 20 74 31 2e 79 3d 31 35 20  t2.z OR t1.y=15 
2b5f4 4f 52 20 74 31 2e 7a 3d 74 33 2e 61 2b 35 0a 2a  OR t1.z=t3.a+5.*
2b5f5 2a 20 20 20 20 20 28 42 29 20 20 20 20 20 78 3d  *     (B)     x=
2b5f6 65 78 70 72 31 20 4f 52 20 65 78 70 72 32 3d 78  expr1 OR expr2=x
2b5f7 20 4f 52 20 78 3d 65 78 70 72 33 0a 2a 2a 20 20   OR x=expr3.**  
2b5f8 20 20 20 28 43 29 20 20 20 20 20 74 31 2e 78 3d     (C)     t1.x=
2b5f9 74 32 2e 79 20 4f 52 20 28 74 31 2e 78 3d 74 32  t2.y OR (t1.x=t2
2b5fa 2e 7a 20 41 4e 44 20 74 31 2e 79 3d 31 35 29 0a  .z AND t1.y=15).
2b5fb 2a 2a 20 20 20 20 20 28 44 29 20 20 20 20 20 78  **     (D)     x
2b5fc 3d 65 78 70 72 31 20 4f 52 20 28 79 3e 31 31 20  =expr1 OR (y>11 
2b5fd 41 4e 44 20 79 3c 32 32 20 41 4e 44 20 7a 20 4c  AND y<22 AND z L
2b5fe 49 4b 45 20 27 2a 68 65 6c 6c 6f 2a 27 29 0a 2a  IKE '*hello*').*
2b5ff 2a 20 20 20 20 20 28 45 29 20 20 20 20 20 28 70  *     (E)     (p
2b600 2e 61 3d 31 20 41 4e 44 20 71 2e 62 3d 32 20 41  .a=1 AND q.b=2 A
2b601 4e 44 20 72 2e 63 3d 33 29 20 4f 52 20 28 70 2e  ND r.c=3) OR (p.
2b602 78 3d 34 20 41 4e 44 20 71 2e 79 3d 35 20 41 4e  x=4 AND q.y=5 AN
2b603 44 20 72 2e 7a 3d 36 29 0a 2a 2a 0a 2a 2a 20 43  D r.z=6).**.** C
2b604 41 53 45 20 31 3a 0a 2a 2a 0a 2a 2a 20 49 66 20  ASE 1:.**.** If 
2b605 61 6c 6c 20 73 75 62 74 65 72 6d 73 20 61 72 65  all subterms are
2b606 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 54 2e 43   of the form T.C
2b607 3d 65 78 70 72 20 66 6f 72 20 73 6f 6d 65 20 73  =expr for some s
2b608 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  ingle column of 
2b609 43 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61  C.** a single ta
2b60a 62 6c 65 20 54 20 28 61 73 20 73 68 6f 77 6e 20  ble T (as shown 
2b60b 69 6e 20 65 78 61 6d 70 6c 65 20 42 20 61 62 6f  in example B abo
2b60c 76 65 29 20 74 68 65 6e 20 63 72 65 61 74 65 20  ve) then create 
2b60d 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a 2a  a new virtual.**
2b60e 20 74 65 72 6d 20 74 68 61 74 20 69 73 20 61 6e   term that is an
2b60f 20 65 71 75 69 76 61 6c 65 6e 74 20 49 4e 20 65   equivalent IN e
2b610 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 6e 20 6f  xpression.  In o
2b611 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74  ther words, if t
2b612 68 65 20 74 65 72 6d 0a 2a 2a 20 62 65 69 6e 67  he term.** being
2b613 20 61 6e 61 6c 79 7a 65 64 20 69 73 3a 0a 2a 2a   analyzed is:.**
2b614 0a 2a 2a 20 20 20 20 20 20 78 20 3d 20 65 78 70  .**      x = exp
2b615 72 31 20 20 4f 52 20 20 65 78 70 72 32 20 3d 20  r1  OR  expr2 = 
2b616 78 20 20 4f 52 20 20 78 20 3d 20 65 78 70 72 33  x  OR  x = expr3
2b617 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 63 72 65 61  .**.** then crea
2b618 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c  te a new virtual
2b619 20 74 65 72 6d 20 6c 69 6b 65 20 74 68 69 73 3a   term like this:
2b61a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 49 4e  .**.**      x IN
2b61b 20 28 65 78 70 72 31 2c 65 78 70 72 32 2c 65 78   (expr1,expr2,ex
2b61c 70 72 33 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45 20  pr3).**.** CASE 
2b61d 32 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c 20  2:.**.** If all 
2b61e 73 75 62 74 65 72 6d 73 20 61 72 65 20 69 6e 64  subterms are ind
2b61f 65 78 61 62 6c 65 20 62 79 20 61 20 73 69 6e 67  exable by a sing
2b620 6c 65 20 74 61 62 6c 65 20 54 2c 20 74 68 65 6e  le table T, then
2b621 20 73 65 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57   set.**.**     W
2b622 68 65 72 65 54 65 72 6d 2e 65 4f 70 65 72 61 74  hereTerm.eOperat
2b623 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  or              
2b624 3d 20 20 57 4f 5f 4f 52 0a 2a 2a 20 20 20 20 20  =  WO_OR.**     
2b625 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49  WhereTerm.u.pOrI
2b626 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 20  nfo->indexable  
2b627 7c 3d 20 20 74 68 65 20 63 75 72 73 6f 72 20 6e  |=  the cursor n
2b628 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
2b629 54 0a 2a 2a 0a 2a 2a 20 41 20 73 75 62 74 65 72  T.**.** A subter
2b62a 6d 20 69 73 20 22 69 6e 64 65 78 61 62 6c 65 22  m is "indexable"
2b62b 20 69 66 20 69 74 20 69 73 20 6f 66 20 74 68 65   if it is of the
2b62c 20 66 6f 72 6d 0a 2a 2a 20 22 54 2e 43 20 3c 6f   form.** "T.C <o
2b62d 70 3e 20 3c 65 78 70 72 3e 22 20 77 68 65 72 65  p> <expr>" where
2b62e 20 43 20 69 73 20 61 6e 79 20 63 6f 6c 75 6d 6e   C is any column
2b62f 20 6f 66 20 74 61 62 6c 65 20 54 20 61 6e 64 20   of table T and 
2b630 0a 2a 2a 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20  .** <op> is one 
2b631 6f 66 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3c 3d  of "=", "<", "<=
2b632 22 2c 20 22 3e 22 2c 20 22 3e 3d 22 2c 20 22 49  ", ">", ">=", "I
2b633 53 20 4e 55 4c 4c 22 2c 20 6f 72 20 22 49 4e 22  S NULL", or "IN"
2b634 2e 0a 2a 2a 20 41 20 73 75 62 74 65 72 6d 20 69  ..** A subterm i
2b635 73 20 61 6c 73 6f 20 69 6e 64 65 78 61 62 6c 65  s also indexable
2b636 20 69 66 20 69 74 20 69 73 20 61 6e 20 41 4e 44   if it is an AND
2b637 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a   of two or more.
2b638 2a 2a 20 73 75 62 73 75 62 74 65 72 6d 73 20 61  ** subsubterms a
2b639 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20 77  t least one of w
2b63a 68 69 63 68 20 69 73 20 69 6e 64 65 78 61 62 6c  hich is indexabl
2b63b 65 2e 20 20 49 6e 64 65 78 61 62 6c 65 20 41 4e  e.  Indexable AN
2b63c 44 20 0a 2a 2a 20 73 75 62 74 65 72 6d 73 20 68  D .** subterms h
2b63d 61 76 65 20 74 68 65 69 72 20 65 4f 70 65 72 61  ave their eOpera
2b63e 74 6f 72 20 73 65 74 20 74 6f 20 57 4f 5f 41 4e  tor set to WO_AN
2b63f 44 20 61 6e 64 20 74 68 65 79 20 68 61 76 65 0a  D and they have.
2b640 2a 2a 20 75 2e 70 41 6e 64 49 6e 66 6f 20 73 65  ** u.pAndInfo se
2b641 74 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c  t to a dynamical
2b642 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 57 68 65  ly allocated Whe
2b643 72 65 41 6e 64 54 65 72 6d 20 6f 62 6a 65 63 74  reAndTerm object
2b644 2e 0a 2a 2a 0a 2a 2a 20 46 72 6f 6d 20 61 6e 6f  ..**.** From ano
2b645 74 68 65 72 20 70 6f 69 6e 74 20 6f 66 20 76 69  ther point of vi
2b646 65 77 2c 20 22 69 6e 64 65 78 61 62 6c 65 22 20  ew, "indexable" 
2b647 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 73  means that the s
2b648 75 62 74 65 72 6d 20 63 6f 75 6c 64 0a 2a 2a 20  ubterm could.** 
2b649 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 75  potentially be u
2b64a 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64 65  sed with an inde
2b64b 78 20 69 66 20 61 6e 20 61 70 70 72 6f 70 72 69  x if an appropri
2b64c 61 74 65 20 69 6e 64 65 78 20 65 78 69 73 74 73  ate index exists
2b64d 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 61 6c 79 73  ..** This analys
2b64e 69 73 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 73  is does not cons
2b64f 69 64 65 72 20 77 68 65 74 68 65 72 20 6f 72 20  ider whether or 
2b650 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 20 65 78  not the index ex
2b651 69 73 74 73 3b 20 74 68 61 74 0a 2a 2a 20 69 73  ists; that.** is
2b652 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 65 20 62   something the b
2b653 65 73 74 49 6e 64 65 78 28 29 20 72 6f 75 74 69  estIndex() routi
2b654 6e 65 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e  ne will determin
2b655 65 2e 20 20 54 68 69 73 20 61 6e 61 6c 79 73 69  e.  This analysi
2b656 73 0a 2a 2a 20 6f 6e 6c 79 20 6c 6f 6f 6b 73 20  s.** only looks 
2b657 61 74 20 77 68 65 74 68 65 72 20 73 75 62 74 65  at whether subte
2b658 72 6d 73 20 61 70 70 72 6f 70 72 69 61 74 65 20  rms appropriate 
2b659 66 6f 72 20 69 6e 64 65 78 69 6e 67 20 65 78 69  for indexing exi
2b65a 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 78  st..**.** All ex
2b65b 61 6d 70 6c 65 73 20 41 20 74 68 72 6f 75 67 68  amples A through
2b65c 20 45 20 61 62 6f 76 65 20 61 6c 6c 20 73 61 74   E above all sat
2b65d 69 73 66 79 20 63 61 73 65 20 32 2e 20 20 42 75  isfy case 2.  Bu
2b65e 74 20 69 66 20 61 20 74 65 72 6d 0a 2a 2a 20 61  t if a term.** a
2b65f 6c 73 6f 20 73 74 61 74 69 73 66 69 65 73 20 63  lso statisfies c
2b660 61 73 65 20 31 20 28 73 75 63 68 20 61 73 20 42  ase 1 (such as B
2b661 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  ) we know that t
2b662 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 77 69 6c  he optimizer wil
2b663 6c 0a 2a 2a 20 61 6c 77 61 79 73 20 70 72 65 66  l.** always pref
2b664 65 72 20 63 61 73 65 20 31 2c 20 73 6f 20 69 6e  er case 1, so in
2b665 20 74 68 61 74 20 63 61 73 65 20 77 65 20 70 72   that case we pr
2b666 65 74 65 6e 64 20 74 68 61 74 20 63 61 73 65 20  etend that case 
2b667 32 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 61 74 69  2 is not.** sati
2b668 73 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  sfied..**.** It 
2b669 6d 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73  might be the cas
2b66a 65 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20  e that multiple 
2b66b 74 61 62 6c 65 73 20 61 72 65 20 69 6e 64 65 78  tables are index
2b66c 61 62 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d 70  able.  For examp
2b66d 6c 65 2c 0a 2a 2a 20 28 45 29 20 61 62 6f 76 65  le,.** (E) above
2b66e 20 69 73 20 69 6e 64 65 78 61 62 6c 65 20 6f 6e   is indexable on
2b66f 20 74 61 62 6c 65 73 20 50 2c 20 51 2c 20 61 6e   tables P, Q, an
2b670 64 20 52 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73  d R..**.** Terms
2b671 20 74 68 61 74 20 73 61 74 69 73 66 79 20 63 61   that satisfy ca
2b672 73 65 20 32 20 61 72 65 20 63 61 6e 64 69 64 61  se 2 are candida
2b673 74 65 73 20 66 6f 72 20 6c 6f 6f 6b 75 70 20 62  tes for lookup b
2b674 79 20 75 73 69 6e 67 0a 2a 2a 20 73 65 70 61 72  y using.** separ
2b675 61 74 65 20 69 6e 64 69 63 65 73 20 74 6f 20 66  ate indices to f
2b676 69 6e 64 20 72 6f 77 69 64 73 20 66 6f 72 20 65  ind rowids for e
2b677 61 63 68 20 73 75 62 74 65 72 6d 20 61 6e 64 20  ach subterm and 
2b678 63 6f 6d 70 6f 73 69 6e 67 0a 2a 2a 20 74 68 65  composing.** the
2b679 20 75 6e 69 6f 6e 20 6f 66 20 61 6c 6c 20 72 6f   union of all ro
2b67a 77 69 64 73 20 75 73 69 6e 67 20 61 20 52 6f 77  wids using a Row
2b67b 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 54 68 69  Set object.  Thi
2b67c 73 20 69 73 20 73 69 6d 69 6c 61 72 0a 2a 2a 20  s is similar.** 
2b67d 74 6f 20 22 62 69 74 6d 61 70 20 69 6e 64 69 63  to "bitmap indic
2b67e 65 73 22 20 69 6e 20 6f 74 68 65 72 20 64 61 74  es" in other dat
2b67f 61 62 61 73 65 20 65 6e 67 69 6e 65 73 2e 0a 2a  abase engines..*
2b680 2a 0a 2a 2a 20 4f 54 48 45 52 57 49 53 45 3a 0a  *.** OTHERWISE:.
2b681 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69 74 68 65 72  **.** If neither
2b682 20 63 61 73 65 20 31 20 6e 6f 72 20 63 61 73 65   case 1 nor case
2b683 20 32 20 61 70 70 6c 79 2c 20 74 68 65 6e 20 6c   2 apply, then l
2b684 65 61 76 65 20 74 68 65 20 65 4f 70 65 72 61 74  eave the eOperat
2b685 6f 72 20 73 65 74 20 74 6f 0a 2a 2a 20 7a 65 72  or set to.** zer
2b686 6f 2e 20 20 54 68 69 73 20 74 65 72 6d 20 69 73  o.  This term is
2b687 20 6e 6f 74 20 75 73 65 66 75 6c 20 66 6f 72 20   not useful for 
2b688 73 65 61 72 63 68 2e 0a 2a 2f 0a 73 74 61 74 69  search..*/.stati
2b689 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
2b68a 7a 65 4f 72 54 65 72 6d 28 0a 20 20 53 72 63 4c  zeOrTerm(.  SrcL
2b68b 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
2b68c 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f        /* the FRO
2b68d 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68  M clause */.  Wh
2b68e 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2b68f 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 63          /* the c
2b690 6f 6d 70 6c 65 74 65 20 57 48 45 52 45 20 63 6c  omplete WHERE cl
2b691 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64  ause */.  int id
2b692 78 54 65 72 6d 20 20 20 20 20 20 20 20 20 20 20  xTerm           
2b693 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2b694 74 68 65 20 4f 52 2d 74 65 72 6d 20 74 6f 20 62  the OR-term to b
2b695 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b  e analyzed */.){
2b696 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2b697 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 20   = pWC->pParse; 
2b698 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2b699 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
2b69a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2b69b 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
2b69c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2b69d 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2b69e 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
2b69f 2a 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61  *pTerm = &pWC->a
2b6a0 5b 69 64 78 54 65 72 6d 5d 3b 20 20 20 20 2f 2a  [idxTerm];    /*
2b6a1 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   The term to be 
2b6a2 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 45 78  analyzed */.  Ex
2b6a3 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65 72  pr *pExpr = pTer
2b6a4 6d 2d 3e 70 45 78 70 72 3b 20 20 20 20 20 20 20  m->pExpr;       
2b6a5 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
2b6a6 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 74  ression of the t
2b6a7 65 72 6d 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61  erm */.  WhereMa
2b6a8 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 20  skSet *pMaskSet 
2b6a9 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b  = pWC->pMaskSet;
2b6aa 20 2f 2a 20 54 61 62 6c 65 20 75 73 65 20 6d 61   /* Table use ma
2b6ab 73 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  sks */.  int i; 
2b6ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b6ae 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2b6af 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  s */.  WhereClau
2b6b0 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20 20 20  se *pOrWc;      
2b6b1 20 2f 2a 20 42 72 65 61 6b 75 70 20 6f 66 20 70   /* Breakup of p
2b6b2 54 65 72 6d 20 69 6e 74 6f 20 73 75 62 74 65 72  Term into subter
2b6b3 6d 73 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  ms */.  WhereTer
2b6b4 6d 20 2a 70 4f 72 54 65 72 6d 3b 20 20 20 20 20  m *pOrTerm;     
2b6b5 20 20 2f 2a 20 41 20 53 75 62 2d 74 65 72 6d 20    /* A Sub-term 
2b6b6 77 69 74 68 69 6e 20 74 68 65 20 70 4f 72 57 63  within the pOrWc
2b6b7 20 2a 2f 0a 20 20 57 68 65 72 65 4f 72 49 6e 66   */.  WhereOrInf
2b6b8 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20 20 20  o *pOrInfo;     
2b6b9 2f 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e  /* Additional in
2b6ba 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f 63 69  formation associ
2b6bb 61 74 65 64 20 77 69 74 68 20 70 54 65 72 6d 20  ated with pTerm 
2b6bc 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 63 68 6e  */.  Bitmask chn
2b6bd 67 54 6f 49 4e 3b 20 20 20 20 20 20 20 20 20 2f  gToIN;         /
2b6be 2a 20 54 61 62 6c 65 73 20 74 68 61 74 20 6d 69  * Tables that mi
2b6bf 67 68 74 20 73 61 74 69 73 66 79 20 63 61 73 65  ght satisfy case
2b6c0 20 31 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20   1 */.  Bitmask 
2b6c1 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20 20 20  indexable;      
2b6c2 20 20 2f 2a 20 54 61 62 6c 65 73 20 74 68 61 74    /* Tables that
2b6c3 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65 2c 20   are indexable, 
2b6c4 73 61 74 69 73 66 79 69 6e 67 20 63 61 73 65 20  satisfying case 
2b6c5 32 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  2 */..  /*.  ** 
2b6c6 42 72 65 61 6b 20 74 68 65 20 4f 52 20 63 6c 61  Break the OR cla
2b6c7 75 73 65 20 69 6e 74 6f 20 69 74 73 20 73 65 70  use into its sep
2b6c8 61 72 61 74 65 20 73 75 62 74 65 72 6d 73 2e 20  arate subterms. 
2b6c9 20 54 68 65 20 73 75 62 74 65 72 6d 73 20 61 72   The subterms ar
2b6ca 65 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e  e.  ** stored in
2b6cb 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20 73   a WhereClause s
2b6cc 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
2b6cd 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 57  ing within the W
2b6ce 68 65 72 65 4f 72 49 6e 66 6f 0a 20 20 2a 2a 20  hereOrInfo.  ** 
2b6cf 6f 62 6a 65 63 74 20 74 68 61 74 20 69 73 20 61  object that is a
2b6d0 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6f  ttached to the o
2b6d1 72 69 67 69 6e 61 6c 20 4f 52 20 63 6c 61 75 73  riginal OR claus
2b6d2 65 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 61  e term..  */.  a
2b6d3 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77  ssert( (pTerm->w
2b6d4 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 44  tFlags & (TERM_D
2b6d5 59 4e 41 4d 49 43 7c 54 45 52 4d 5f 4f 52 49 4e  YNAMIC|TERM_ORIN
2b6d6 46 4f 7c 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 29  FO|TERM_ANDINFO)
2b6d7 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )==0 );.  assert
2b6d8 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2b6d9 4f 52 20 29 3b 0a 20 20 70 54 65 72 6d 2d 3e 75  OR );.  pTerm->u
2b6da 2e 70 4f 72 49 6e 66 6f 20 3d 20 70 4f 72 49 6e  .pOrInfo = pOrIn
2b6db 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
2b6dc 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2b6dd 65 6f 66 28 2a 70 4f 72 49 6e 66 6f 29 29 3b 0a  eof(*pOrInfo));.
2b6de 20 20 69 66 28 20 70 4f 72 49 6e 66 6f 3d 3d 30    if( pOrInfo==0
2b6df 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 54 65   ) return;.  pTe
2b6e0 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
2b6e1 45 52 4d 5f 4f 52 49 4e 46 4f 3b 0a 20 20 70 4f  ERM_ORINFO;.  pO
2b6e2 72 57 63 20 3d 20 26 70 4f 72 49 6e 66 6f 2d 3e  rWc = &pOrInfo->
2b6e3 77 63 3b 0a 20 20 77 68 65 72 65 43 6c 61 75 73  wc;.  whereClaus
2b6e4 65 49 6e 69 74 28 70 4f 72 57 63 2c 20 70 57 43  eInit(pOrWc, pWC
2b6e5 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53  ->pParse, pMaskS
2b6e6 65 74 29 3b 0a 20 20 77 68 65 72 65 53 70 6c 69  et);.  whereSpli
2b6e7 74 28 70 4f 72 57 63 2c 20 70 45 78 70 72 2c 20  t(pOrWc, pExpr, 
2b6e8 54 4b 5f 4f 52 29 3b 0a 20 20 65 78 70 72 41 6e  TK_OR);.  exprAn
2b6e9 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70  alyzeAll(pSrc, p
2b6ea 4f 72 57 63 29 3b 0a 20 20 69 66 28 20 64 62 2d  OrWc);.  if( db-
2b6eb 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2b6ec 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
2b6ed 28 20 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 3e 3d  ( pOrWc->nTerm>=
2b6ee 32 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  2 );..  /*.  ** 
2b6ef 43 6f 6d 70 75 74 65 20 74 68 65 20 73 65 74 20  Compute the set 
2b6f0 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20 6d  of tables that m
2b6f1 69 67 68 74 20 73 61 74 69 73 66 79 20 63 61 73  ight satisfy cas
2b6f2 65 73 20 31 20 6f 72 20 32 2e 0a 20 20 2a 2f 0a  es 1 or 2..  */.
2b6f3 20 20 69 6e 64 65 78 61 62 6c 65 20 3d 20 63 68    indexable = ch
2b6f4 6e 67 54 6f 49 4e 20 3d 20 7e 28 42 69 74 6d 61  ngToIN = ~(Bitma
2b6f5 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69 3d 70 4f  sk)0;.  for(i=pO
2b6f6 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70 4f  rWc->nTerm-1, pO
2b6f7 72 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b 20  rTerm=pOrWc->a; 
2b6f8 69 3e 3d 30 20 26 26 20 69 6e 64 65 78 61 62 6c  i>=0 && indexabl
2b6f9 65 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b  e; i--, pOrTerm+
2b6fa 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 70 4f 72  +){.    if( (pOr
2b6fb 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2b6fc 26 20 57 4f 5f 53 49 4e 47 4c 45 29 3d 3d 30 20  & WO_SINGLE)==0 
2b6fd 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 41 6e  ){.      WhereAn
2b6fe 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66 6f 3b  dInfo *pAndInfo;
2b6ff 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2b700 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2b701 72 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73  r==0 );.      as
2b702 73 65 72 74 28 20 28 70 4f 72 54 65 72 6d 2d 3e  sert( (pOrTerm->
2b703 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f  wtFlags & (TERM_
2b704 41 4e 44 49 4e 46 4f 7c 54 45 52 4d 5f 4f 52 49  ANDINFO|TERM_ORI
2b705 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  NFO))==0 );.    
2b706 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a    chngToIN = 0;.
2b707 20 20 20 20 20 20 70 41 6e 64 49 6e 66 6f 20 3d        pAndInfo =
2b708 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2b709 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  Raw(db, sizeof(*
2b70a 70 41 6e 64 49 6e 66 6f 29 29 3b 0a 20 20 20 20  pAndInfo));.    
2b70b 20 20 69 66 28 20 70 41 6e 64 49 6e 66 6f 20 29    if( pAndInfo )
2b70c 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 43  {.        WhereC
2b70d 6c 61 75 73 65 20 2a 70 41 6e 64 57 43 3b 0a 20  lause *pAndWC;. 
2b70e 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d         WhereTerm
2b70f 20 2a 70 41 6e 64 54 65 72 6d 3b 0a 20 20 20 20   *pAndTerm;.    
2b710 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
2b711 20 20 20 42 69 74 6d 61 73 6b 20 62 20 3d 20 30     Bitmask b = 0
2b712 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65 72  ;.        pOrTer
2b713 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 20 3d 20  m->u.pAndInfo = 
2b714 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pAndInfo;.      
2b715 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61    pOrTerm->wtFla
2b716 67 73 20 7c 3d 20 54 45 52 4d 5f 41 4e 44 49 4e  gs |= TERM_ANDIN
2b717 46 4f 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54  FO;.        pOrT
2b718 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d  erm->eOperator =
2b719 20 57 4f 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20   WO_AND;.       
2b71a 20 70 41 6e 64 57 43 20 3d 20 26 70 41 6e 64 49   pAndWC = &pAndI
2b71b 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 20  nfo->wc;.       
2b71c 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74   whereClauseInit
2b71d 28 70 41 6e 64 57 43 2c 20 70 57 43 2d 3e 70 50  (pAndWC, pWC->pP
2b71e 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 29 3b  arse, pMaskSet);
2b71f 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 53 70  .        whereSp
2b720 6c 69 74 28 70 41 6e 64 57 43 2c 20 70 4f 72 54  lit(pAndWC, pOrT
2b721 65 72 6d 2d 3e 70 45 78 70 72 2c 20 54 4b 5f 41  erm->pExpr, TK_A
2b722 4e 44 29 3b 0a 20 20 20 20 20 20 20 20 65 78 70  ND);.        exp
2b723 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63  rAnalyzeAll(pSrc
2b724 2c 20 70 41 6e 64 57 43 29 3b 0a 20 20 20 20 20  , pAndWC);.     
2b725 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62 2d     testcase( db-
2b726 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
2b727 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 64 62  .        if( !db
2b728 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2b729 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  {.          for(
2b72a 6a 3d 30 2c 20 70 41 6e 64 54 65 72 6d 3d 70 41  j=0, pAndTerm=pA
2b72b 6e 64 57 43 2d 3e 61 3b 20 6a 3c 70 41 6e 64 57  ndWC->a; j<pAndW
2b72c 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70  C->nTerm; j++, p
2b72d 41 6e 64 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  AndTerm++){.    
2b72e 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2b72f 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72 20  pAndTerm->pExpr 
2b730 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
2b731 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 41 6e  f( allowedOp(pAn
2b732 64 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 6f 70  dTerm->pExpr->op
2b733 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
2b734 20 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b 28     b |= getMask(
2b735 70 4d 61 73 6b 53 65 74 2c 20 70 41 6e 64 54 65  pMaskSet, pAndTe
2b736 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b  rm->leftCursor);
2b737 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
2b738 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2b739 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 6e 64     }.        ind
2b73a 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20 20  exable &= b;.   
2b73b 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
2b73c 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c  f( pOrTerm->wtFl
2b73d 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49 45  ags & TERM_COPIE
2b73e 44 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 6b  D ){.      /* Sk
2b73f 69 70 20 74 68 69 73 20 74 65 72 6d 20 66 6f 72  ip this term for
2b740 20 6e 6f 77 2e 20 20 57 65 20 72 65 76 69 73 69   now.  We revisi
2b741 74 20 69 74 20 77 68 65 6e 20 77 65 20 70 72 6f  t it when we pro
2b742 63 65 73 73 20 74 68 65 0a 20 20 20 20 20 20 2a  cess the.      *
2b743 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  * corresponding 
2b744 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 74 65 72  TERM_VIRTUAL ter
2b745 6d 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  m */.    }else{.
2b746 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62 3b        Bitmask b;
2b747 0a 20 20 20 20 20 20 62 20 3d 20 67 65 74 4d 61  .      b = getMa
2b748 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f 72  sk(pMaskSet, pOr
2b749 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
2b74a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72  );.      if( pOr
2b74b 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2b74c 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 7b 0a  TERM_VIRTUAL ){.
2b74d 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72          WhereTer
2b74e 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70 4f 72  m *pOther = &pOr
2b74f 57 63 2d 3e 61 5b 70 4f 72 54 65 72 6d 2d 3e 69  Wc->a[pOrTerm->i
2b750 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20 20  Parent];.       
2b751 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b 28 70 4d   b |= getMask(pM
2b752 61 73 6b 53 65 74 2c 20 70 4f 74 68 65 72 2d 3e  askSet, pOther->
2b753 6c 65 66 74 43 75 72 73 6f 72 29 3b 0a 20 20 20  leftCursor);.   
2b754 20 20 20 7d 0a 20 20 20 20 20 20 69 6e 64 65 78     }.      index
2b755 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20 20 20 20  able &= b;.     
2b756 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f   if( pOrTerm->eO
2b757 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45 51 20 29  perator!=WO_EQ )
2b758 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67 54 6f  {.        chngTo
2b759 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  IN = 0;.      }e
2b75a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 68 6e  lse{.        chn
2b75b 67 54 6f 49 4e 20 26 3d 20 62 3b 0a 20 20 20 20  gToIN &= b;.    
2b75c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
2b75d 20 2f 2a 0a 20 20 2a 2a 20 52 65 63 6f 72 64 20   /*.  ** Record 
2b75e 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c 65  the set of table
2b75f 73 20 74 68 61 74 20 73 61 74 69 73 66 79 20 63  s that satisfy c
2b760 61 73 65 20 32 2e 20 20 54 68 65 20 73 65 74 20  ase 2.  The set 
2b761 6d 69 67 68 74 20 62 65 0a 20 20 2a 2a 20 65 6d  might be.  ** em
2b762 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 70 4f 72 49  pty..  */.  pOrI
2b763 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 3d  nfo->indexable =
2b764 20 69 6e 64 65 78 61 62 6c 65 3b 0a 20 20 70 54   indexable;.  pT
2b765 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d  erm->eOperator =
2b766 20 69 6e 64 65 78 61 62 6c 65 3d 3d 30 20 3f 20   indexable==0 ? 
2b767 30 20 3a 20 57 4f 5f 4f 52 3b 0a 0a 20 20 2f 2a  0 : WO_OR;..  /*
2b768 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e 20 68  .  ** chngToIN h
2b769 6f 6c 64 73 20 61 20 73 65 74 20 6f 66 20 74 61  olds a set of ta
2b76a 62 6c 65 73 20 74 68 61 74 20 2a 6d 69 67 68 74  bles that *might
2b76b 2a 20 73 61 74 69 73 66 79 20 63 61 73 65 20 31  * satisfy case 1
2b76c 2e 20 20 42 75 74 0a 20 20 2a 2a 20 77 65 20 68  .  But.  ** we h
2b76d 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20 61  ave to do some a
2b76e 64 64 69 74 69 6f 6e 61 6c 20 63 68 65 63 6b 69  dditional checki
2b76f 6e 67 20 74 6f 20 73 65 65 20 69 66 20 63 61 73  ng to see if cas
2b770 65 20 31 20 72 65 61 6c 6c 79 0a 20 20 2a 2a 20  e 1 really.  ** 
2b771 69 73 20 73 61 74 69 73 66 69 65 64 2e 0a 20 20  is satisfied..  
2b772 2a 2f 0a 20 20 69 66 28 20 63 68 6e 67 54 6f 49  */.  if( chngToI
2b773 4e 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 6b 54  N ){.    int okT
2b774 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 20 20  oChngToIN = 0;  
2b775 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2b776 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20  e conversion to 
2b777 49 4e 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20  IN is valid */. 
2b778 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 20 3d     int iColumn =
2b779 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   -1;         /* 
2b77a 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 6f 6e 20  Column index on 
2b77b 6c 68 73 20 6f 66 20 49 4e 20 6f 70 65 72 61 74  lhs of IN operat
2b77c 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  or */.    int iC
2b77d 75 72 73 6f 72 20 3d 20 2d 31 3b 20 20 20 20 20  ursor = -1;     
2b77e 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72      /* Table cur
2b77f 73 6f 72 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c  sor common to al
2b780 6c 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 69  l terms */.    i
2b781 6e 74 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20  nt j = 0;       
2b782 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2b783 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 20   counter */..   
2b784 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61   /* Search for a
2b785 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
2b786 6e 20 74 68 61 74 20 61 70 70 65 61 72 73 20 6f  n that appears o
2b787 6e 20 6f 6e 65 20 73 69 64 65 20 6f 72 20 74 68  n one side or th
2b788 65 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 6f  e.    ** other o
2b789 66 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74 6f  f the == operato
2b78a 72 20 69 6e 20 65 76 65 72 79 20 73 75 62 74 65  r in every subte
2b78b 72 6d 2e 20 20 54 68 61 74 20 74 61 62 6c 65 20  rm.  That table 
2b78c 61 6e 64 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a  and column.    *
2b78d 2a 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 72 64  * will be record
2b78e 65 64 20 69 6e 20 69 43 75 72 73 6f 72 20 61 6e  ed in iCursor an
2b78f 64 20 69 43 6f 6c 75 6d 6e 2e 20 20 54 68 65 72  d iColumn.  Ther
2b790 65 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61  e might not be a
2b791 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63 68 20 74  ny.    ** such t
2b792 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e  able and column.
2b793 20 20 53 65 74 20 6f 6b 54 6f 43 68 6e 67 54 6f    Set okToChngTo
2b794 49 4e 20 69 66 20 61 6e 20 61 70 70 72 6f 70 72  IN if an appropr
2b795 69 61 74 65 20 74 61 62 6c 65 0a 20 20 20 20 2a  iate table.    *
2b796 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 69 73 20  * and column is 
2b797 66 6f 75 6e 64 20 62 75 74 20 6c 65 61 76 65 20  found but leave 
2b798 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 66 61 6c  okToChngToIN fal
2b799 73 65 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e  se if not found.
2b79a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
2b79b 6a 3d 30 3b 20 6a 3c 32 20 26 26 20 21 6f 6b 54  j=0; j<2 && !okT
2b79c 6f 43 68 6e 67 54 6f 49 4e 3b 20 6a 2b 2b 29 7b  oChngToIN; j++){
2b79d 0a 20 20 20 20 20 20 70 4f 72 54 65 72 6d 20 3d  .      pOrTerm =
2b79e 20 70 4f 72 57 63 2d 3e 61 3b 0a 20 20 20 20 20   pOrWc->a;.     
2b79f 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54   for(i=pOrWc->nT
2b7a0 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  erm-1; i>=0; i--
2b7a1 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20  , pOrTerm++){.  
2b7a2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
2b7a3 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  rTerm->eOperator
2b7a4 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20  ==WO_EQ );.     
2b7a5 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c     pOrTerm->wtFl
2b7a6 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f  ags &= ~TERM_OR_
2b7a7 4f 4b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  OK;.        if( 
2b7a8 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2b7a9 73 6f 72 3d 3d 69 43 6f 6c 75 6d 6e 20 29 20 63  sor==iColumn ) c
2b7aa 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2b7ab 20 69 66 28 20 28 63 68 6e 67 54 6f 49 4e 20 26   if( (chngToIN &
2b7ac 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65   getMask(pMaskSe
2b7ad 74 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74  t, pOrTerm->left
2b7ae 43 75 72 73 6f 72 29 29 3d 3d 30 20 29 20 63 6f  Cursor))==0 ) co
2b7af 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
2b7b0 69 43 6f 6c 75 6d 6e 20 3d 20 70 4f 72 54 65 72  iColumn = pOrTer
2b7b1 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b  m->u.leftColumn;
2b7b2 0a 20 20 20 20 20 20 20 20 69 43 75 72 73 6f 72  .        iCursor
2b7b3 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74   = pOrTerm->left
2b7b4 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 20 20  Cursor;.        
2b7b5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2b7b6 20 20 20 20 20 69 66 28 20 69 3c 30 20 29 7b 0a       if( i<0 ){.
2b7b7 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2b7b8 6a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20  j==1 );.        
2b7b9 61 73 73 65 72 74 28 20 28 63 68 6e 67 54 6f 49  assert( (chngToI
2b7ba 4e 26 28 63 68 6e 67 54 6f 49 4e 2d 31 29 29 3d  N&(chngToIN-1))=
2b7bb 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =0 );.        as
2b7bc 73 65 72 74 28 20 63 68 6e 67 54 6f 49 4e 3d 3d  sert( chngToIN==
2b7bd 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
2b7be 2c 20 69 43 6f 6c 75 6d 6e 29 20 29 3b 0a 20 20  , iColumn) );.  
2b7bf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b7c0 20 20 20 7d 0a 20 20 20 20 20 20 6f 6b 54 6f 43     }.      okToC
2b7c1 68 6e 67 54 6f 49 4e 20 3d 20 31 3b 0a 20 20 20  hngToIN = 1;.   
2b7c2 20 20 20 66 6f 72 28 3b 20 69 3e 3d 30 20 26 26     for(; i>=0 &&
2b7c3 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b 20 69   okToChngToIN; i
2b7c4 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a  --, pOrTerm++){.
2b7c5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2b7c6 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
2b7c7 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20  or==WO_EQ );.   
2b7c8 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d       if( pOrTerm
2b7c9 2d 3e 6c 65 66 74 43 75 72 73 6f 72 21 3d 69 43  ->leftCursor!=iC
2b7ca 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20  ursor ){.       
2b7cb 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c     pOrTerm->wtFl
2b7cc 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f  ags &= ~TERM_OR_
2b7cd 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  OK;.        }els
2b7ce 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 75  e if( pOrTerm->u
2b7cf 2e 6c 65 66 74 43 6f 6c 75 6d 6e 21 3d 69 43 6f  .leftColumn!=iCo
2b7d0 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  lumn ){.        
2b7d1 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d    okToChngToIN =
2b7d2 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   0;.        }els
2b7d3 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  e{.          int
2b7d4 20 61 66 66 4c 65 66 74 2c 20 61 66 66 52 69 67   affLeft, affRig
2b7d5 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  ht;.          /*
2b7d6 20 49 66 20 74 68 65 20 72 69 67 68 74 2d 68 61   If the right-ha
2b7d7 6e 64 20 73 69 64 65 20 69 73 20 61 6c 73 6f 20  nd side is also 
2b7d8 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74  a column, then t
2b7d9 68 65 20 61 66 66 69 6e 69 74 69 65 73 0a 20 20  he affinities.  
2b7da 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 62 6f          ** of bo
2b7db 74 68 20 72 69 67 68 74 20 61 6e 64 20 6c 65 66  th right and lef
2b7dc 74 20 73 69 64 65 73 20 6d 75 73 74 20 62 65 20  t sides must be 
2b7dd 73 75 63 68 20 74 68 61 74 20 6e 6f 20 74 79 70  such that no typ
2b7de 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  e.          ** c
2b7df 6f 6e 76 65 72 73 69 6f 6e 73 20 61 72 65 20 72  onversions are r
2b7e0 65 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 72  equired on the r
2b7e1 69 67 68 74 2e 20 20 28 54 69 63 6b 65 74 20 23  ight.  (Ticket #
2b7e2 32 32 34 39 29 0a 20 20 20 20 20 20 20 20 20 20  2249).          
2b7e3 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 66 66  */.          aff
2b7e4 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45  Right = sqlite3E
2b7e5 78 70 72 41 66 66 69 6e 69 74 79 28 70 4f 72 54  xprAffinity(pOrT
2b7e6 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67  erm->pExpr->pRig
2b7e7 68 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61  ht);.          a
2b7e8 66 66 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33  ffLeft = sqlite3
2b7e9 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4f 72  ExprAffinity(pOr
2b7ea 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65  Term->pExpr->pLe
2b7eb 66 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ft);.          i
2b7ec 66 28 20 61 66 66 52 69 67 68 74 21 3d 30 20 26  f( affRight!=0 &
2b7ed 26 20 61 66 66 52 69 67 68 74 21 3d 61 66 66 4c  & affRight!=affL
2b7ee 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  eft ){.         
2b7ef 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20     okToChngToIN 
2b7f0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 0;.          }
2b7f1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2b7f2 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61    pOrTerm->wtFla
2b7f3 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52 5f 4f 4b  gs |= TERM_OR_OK
2b7f4 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2b7f5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2b7f6 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74      }..    /* At
2b7f7 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6f 6b 54   this point, okT
2b7f8 6f 43 68 6e 67 54 6f 49 4e 20 69 73 20 74 72 75  oChngToIN is tru
2b7f9 65 20 69 66 20 6f 72 69 67 69 6e 61 6c 20 70 54  e if original pT
2b7fa 65 72 6d 20 73 61 74 69 73 66 69 65 73 0a 20 20  erm satisfies.  
2b7fb 20 20 2a 2a 20 63 61 73 65 20 31 2e 20 20 49 6e    ** case 1.  In
2b7fc 20 74 68 61 74 20 63 61 73 65 2c 20 63 6f 6e 73   that case, cons
2b7fd 74 72 75 63 74 20 61 20 6e 65 77 20 76 69 72 74  truct a new virt
2b7fe 75 61 6c 20 74 65 72 6d 20 74 68 61 74 20 69 73  ual term that is
2b7ff 20 0a 20 20 20 20 2a 2a 20 70 54 65 72 6d 20 63   .    ** pTerm c
2b800 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e  onverted into an
2b801 20 49 4e 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20   IN operator..  
2b802 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 6b 54    */.    if( okT
2b803 6f 43 68 6e 67 54 6f 49 4e 20 29 7b 0a 20 20 20  oChngToIN ){.   
2b804 20 20 20 45 78 70 72 20 2a 70 44 75 70 3b 20 20     Expr *pDup;  
2b805 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74            /* A t
2b806 72 61 6e 73 69 65 6e 74 20 64 75 70 6c 69 63 61  ransient duplica
2b807 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  te expression */
2b808 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
2b809 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 2f 2a  *pList = 0;   /*
2b80a 20 54 68 65 20 52 48 53 20 6f 66 20 74 68 65 20   The RHS of the 
2b80b 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  IN operator */. 
2b80c 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74       Expr *pLeft
2b80d 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 54   = 0;       /* T
2b80e 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20 49 4e  he LHS of the IN
2b80f 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20   operator */.   
2b810 20 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 20 20     Expr *pNew;  
2b811 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2b812 20 63 6f 6d 70 6c 65 74 65 20 49 4e 20 6f 70 65   complete IN ope
2b813 72 61 74 6f 72 20 2a 2f 0a 0a 20 20 20 20 20 20  rator */..      
2b814 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65  for(i=pOrWc->nTe
2b815 72 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f  rm-1, pOrTerm=pO
2b816 72 57 63 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d  rWc->a; i>=0; i-
2b817 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20  -, pOrTerm++){. 
2b818 20 20 20 20 20 20 20 69 66 28 20 28 70 4f 72 54         if( (pOrT
2b819 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2b81a 45 52 4d 5f 4f 52 5f 4f 4b 29 3d 3d 30 20 29 20  ERM_OR_OK)==0 ) 
2b81b 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2b81c 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72    assert( pOrTer
2b81d 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
2b81e 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20 61  _EQ );.        a
2b81f 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e  ssert( pOrTerm->
2b820 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
2b821 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 20 20 61  sor );.        a
2b822 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e  ssert( pOrTerm->
2b823 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43  u.leftColumn==iC
2b824 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20 20  olumn );.       
2b825 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
2b826 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 54 65  xprDup(db, pOrTe
2b827 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  rm->pExpr->pRigh
2b828 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70  t, 0);.        p
2b829 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
2b82a 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 57 43  prListAppend(pWC
2b82b 2d 3e 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  ->pParse, pList,
2b82c 20 70 44 75 70 2c 20 30 29 3b 0a 20 20 20 20 20   pDup, 0);.     
2b82d 20 20 20 70 4c 65 66 74 20 3d 20 70 4f 72 54 65     pLeft = pOrTe
2b82e 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74  rm->pExpr->pLeft
2b82f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2b830 61 73 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30  assert( pLeft!=0
2b831 20 29 3b 0a 20 20 20 20 20 20 70 44 75 70 20 3d   );.      pDup =
2b832 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2b833 64 62 2c 20 70 4c 65 66 74 2c 20 30 29 3b 0a 20  db, pLeft, 0);. 
2b834 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
2b835 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49  te3Expr(db, TK_I
2b836 4e 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a  N, pDup, 0, 0);.
2b837 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29        if( pNew )
2b838 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 64  {.        int id
2b839 78 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 74 72  xNew;.        tr
2b83a 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e  ansferJoinMarkin
2b83b 67 73 28 70 4e 65 77 2c 20 70 45 78 70 72 29 3b  gs(pNew, pExpr);
2b83c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2b83d 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
2b83e 79 28 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65  y(pNew, EP_xIsSe
2b83f 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 20  lect) );.       
2b840 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d   pNew->x.pList =
2b841 20 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20   pList;.        
2b842 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c  idxNew = whereCl
2b843 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
2b844 70 4e 65 77 2c 20 54 45 52 4d 5f 56 49 52 54 55  pNew, TERM_VIRTU
2b845 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
2b846 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
2b847 73 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b  se( idxNew==0 );
2b848 0a 20 20 20 20 20 20 20 20 65 78 70 72 41 6e 61  .        exprAna
2b849 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20  lyze(pSrc, pWC, 
2b84a 69 64 78 4e 65 77 29 3b 0a 20 20 20 20 20 20 20  idxNew);.       
2b84b 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
2b84c 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20  [idxTerm];.     
2b84d 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77     pWC->a[idxNew
2b84e 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ].iParent = idxT
2b84f 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  erm;.        pTe
2b850 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a  rm->nChild = 1;.
2b851 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2b852 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2b853 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2b854 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  List);.      }. 
2b855 20 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65       pTerm->eOpe
2b856 72 61 74 6f 72 20 3d 20 30 3b 20 20 2f 2a 20 63  rator = 0;  /* c
2b857 61 73 65 20 31 20 74 72 75 6d 70 73 20 63 61 73  ase 1 trumps cas
2b858 65 20 32 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d  e 2 */.    }.  }
2b859 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51  .}.#endif /* !SQ
2b85a 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54  LITE_OMIT_OR_OPT
2b85b 49 4d 49 5a 41 54 49 4f 4e 20 26 26 20 21 53 51  IMIZATION && !SQ
2b85c 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2b85d 52 59 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  RY */.../*.** Th
2b85e 65 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73 20  e input to this 
2b85f 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 57 68  routine is an Wh
2b860 65 72 65 54 65 72 6d 20 73 74 72 75 63 74 75 72  ereTerm structur
2b861 65 20 77 69 74 68 20 6f 6e 6c 79 20 74 68 65 0a  e with only the.
2b862 2a 2a 20 22 70 45 78 70 72 22 20 66 69 65 6c 64  ** "pExpr" field
2b863 20 66 69 6c 6c 65 64 20 69 6e 2e 20 20 54 68 65   filled in.  The
2b864 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75   job of this rou
2b865 74 69 6e 65 20 69 73 20 74 6f 20 61 6e 61 6c 79  tine is to analy
2b866 7a 65 20 74 68 65 0a 2a 2a 20 73 75 62 65 78 70  ze the.** subexp
2b867 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 6f 70 75  ression and popu
2b868 6c 61 74 65 20 61 6c 6c 20 74 68 65 20 6f 74 68  late all the oth
2b869 65 72 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  er fields of the
2b86a 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a 20 73 74   WhereTerm.** st
2b86b 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49  ructure..**.** I
2b86c 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
2b86d 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   is of the form 
2b86e 22 3c 65 78 70 72 3e 20 3c 6f 70 3e 20 58 22 20  "<expr> <op> X" 
2b86f 69 74 20 67 65 74 73 20 63 6f 6d 6d 75 74 65 64  it gets commuted
2b870 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 6e 64  .** to the stand
2b871 61 72 64 20 66 6f 72 6d 20 6f 66 20 22 58 20 3c  ard form of "X <
2b872 6f 70 3e 20 3c 65 78 70 72 3e 22 2e 0a 2a 2a 0a  op> <expr>"..**.
2b873 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
2b874 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66  sion is of the f
2b875 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 59 22 20 77  orm "X <op> Y" w
2b876 68 65 72 65 20 62 6f 74 68 20 58 20 61 6e 64 20  here both X and 
2b877 59 20 61 72 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 73  Y are.** columns
2b878 2c 20 74 68 65 6e 20 74 68 65 20 6f 72 69 67 69  , then the origi
2b879 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 69  nal expression i
2b87a 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20  s unchanged and 
2b87b 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a 2a  a new virtual.**
2b87c 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 72   term of the for
2b87d 6d 20 22 59 20 3c 6f 70 3e 20 58 22 20 69 73 20  m "Y <op> X" is 
2b87e 61 64 64 65 64 20 74 6f 20 74 68 65 20 57 48 45  added to the WHE
2b87f 52 45 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a  RE clause and.**
2b880 20 61 6e 61 6c 79 7a 65 64 20 73 65 70 61 72 61   analyzed separa
2b881 74 65 6c 79 2e 20 20 54 68 65 20 6f 72 69 67 69  tely.  The origi
2b882 6e 61 6c 20 74 65 72 6d 20 69 73 20 6d 61 72 6b  nal term is mark
2b883 65 64 20 77 69 74 68 20 54 45 52 4d 5f 43 4f 50  ed with TERM_COP
2b884 49 45 44 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  IED.** and the n
2b885 65 77 20 74 65 72 6d 20 69 73 20 6d 61 72 6b 65  ew term is marke
2b886 64 20 77 69 74 68 20 54 45 52 4d 5f 44 59 4e 41  d with TERM_DYNA
2b887 4d 49 43 20 28 62 65 63 61 75 73 65 20 69 74 27  MIC (because it'
2b888 73 20 70 45 78 70 72 0a 2a 2a 20 6e 65 65 64 73  s pExpr.** needs
2b889 20 74 6f 20 62 65 20 66 72 65 65 64 20 77 69 74   to be freed wit
2b88a 68 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73  h the WhereClaus
2b88b 65 29 20 61 6e 64 20 54 45 52 4d 5f 56 49 52 54  e) and TERM_VIRT
2b88c 55 41 4c 20 28 62 65 63 61 75 73 65 20 69 74 0a  UAL (because it.
2b88d 2a 2a 20 69 73 20 61 20 63 6f 6d 6d 75 74 65 64  ** is a commuted
2b88e 20 63 6f 70 79 20 6f 66 20 61 20 70 72 69 6f 72   copy of a prior
2b88f 20 74 65 72 6d 2e 29 20 20 54 68 65 20 6f 72 69   term.)  The ori
2b890 67 69 6e 61 6c 20 74 65 72 6d 20 68 61 73 20 6e  ginal term has n
2b891 43 68 69 6c 64 3d 31 0a 2a 2a 20 61 6e 64 20 74  Child=1.** and t
2b892 68 65 20 63 6f 70 79 20 68 61 73 20 69 64 78 50  he copy has idxP
2b893 61 72 65 6e 74 20 73 65 74 20 74 6f 20 74 68 65  arent set to the
2b894 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6f 72   index of the or
2b895 69 67 69 6e 61 6c 20 74 65 72 6d 2e 0a 2a 2f 0a  iginal term..*/.
2b896 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72  static void expr
2b897 41 6e 61 6c 79 7a 65 28 0a 20 20 53 72 63 4c 69  Analyze(.  SrcLi
2b898 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20  st *pSrc,       
2b899 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d       /* the FROM
2b89a 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
2b89b 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
2b89c 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48         /* the WH
2b89d 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2b89e 69 6e 74 20 69 64 78 54 65 72 6d 20 20 20 20 20  int idxTerm     
2b89f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
2b8a0 65 78 20 6f 66 20 74 68 65 20 74 65 72 6d 20 74  ex of the term t
2b8a1 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  o be analyzed */
2b8a2 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20  .){.  WhereTerm 
2b8a3 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20  *pTerm;         
2b8a4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65         /* The te
2b8a5 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  rm to be analyze
2b8a6 64 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b  d */.  WhereMask
2b8a7 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20  Set *pMaskSet;  
2b8a8 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6f          /* Set o
2b8a9 66 20 74 61 62 6c 65 20 69 6e 64 65 78 20 6d 61  f table index ma
2b8aa 73 6b 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  sks */.  Expr *p
2b8ab 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
2b8ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2b8ad 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62   expression to b
2b8ae 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20  e analyzed */.  
2b8af 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 4c 65  Bitmask prereqLe
2b8b0 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ft;             
2b8b1 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69 74 65   /* Prerequesite
2b8b2 73 20 6f 66 20 74 68 65 20 70 45 78 70 72 2d 3e  s of the pExpr->
2b8b3 70 4c 65 66 74 20 2a 2f 0a 20 20 42 69 74 6d 61  pLeft */.  Bitma
2b8b4 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 20 20  sk prereqAll;   
2b8b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2b8b6 72 65 72 65 71 75 65 73 69 74 65 73 20 6f 66 20  rerequesites of 
2b8b7 70 45 78 70 72 20 2a 2f 0a 20 20 42 69 74 6d 61  pExpr */.  Bitma
2b8b8 73 6b 20 65 78 74 72 61 52 69 67 68 74 20 3d 20  sk extraRight = 
2b8b9 30 3b 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72  0;.  int nPatter
2b8ba 6e 3b 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70 6c  n;.  int isCompl
2b8bb 65 74 65 3b 0a 20 20 69 6e 74 20 6e 6f 43 61 73  ete;.  int noCas
2b8bc 65 3b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20  e;.  int op;    
2b8bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8be 20 20 20 20 20 20 2f 2a 20 54 6f 70 2d 6c 65 76        /* Top-lev
2b8bf 65 6c 20 6f 70 65 72 61 74 6f 72 2e 20 20 70 45  el operator.  pE
2b8c0 78 70 72 2d 3e 6f 70 20 2a 2f 0a 20 20 50 61 72  xpr->op */.  Par
2b8c1 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43  se *pParse = pWC
2b8c2 2d 3e 70 50 61 72 73 65 3b 20 20 20 20 20 2f 2a  ->pParse;     /*
2b8c3 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2b8c4 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2b8c5 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2b8c6 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2b8c7 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2b8c8 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ..  if( db->mall
2b8c9 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2b8ca 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 54  return;.  }.  pT
2b8cb 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
2b8cc 78 54 65 72 6d 5d 3b 0a 20 20 70 4d 61 73 6b 53  xTerm];.  pMaskS
2b8cd 65 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53  et = pWC->pMaskS
2b8ce 65 74 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 54  et;.  pExpr = pT
2b8cf 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 70 72  erm->pExpr;.  pr
2b8d0 65 72 65 71 4c 65 66 74 20 3d 20 65 78 70 72 54  ereqLeft = exprT
2b8d1 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2b8d2 65 74 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  et, pExpr->pLeft
2b8d3 29 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d  );.  op = pExpr-
2b8d4 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54  >op;.  if( op==T
2b8d5 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 61 73 73 65  K_IN ){.    asse
2b8d6 72 74 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68  rt( pExpr->pRigh
2b8d7 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  t==0 );.    if( 
2b8d8 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2b8d9 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2b8da 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 70 54  ect) ){.      pT
2b8db 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2b8dc 20 3d 20 65 78 70 72 53 65 6c 65 63 74 54 61 62   = exprSelectTab
2b8dd 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2b8de 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  , pExpr->x.pSele
2b8df 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ct);.    }else{.
2b8e0 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65        pTerm->pre
2b8e1 72 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 4c  reqRight = exprL
2b8e2 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  istTableUsage(pM
2b8e3 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 78  askSet, pExpr->x
2b8e4 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20  .pList);.    }. 
2b8e5 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54   }else if( op==T
2b8e6 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  K_ISNULL ){.    
2b8e7 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
2b8e8 68 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ht = 0;.  }else{
2b8e9 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72  .    pTerm->prer
2b8ea 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 54 61  eqRight = exprTa
2b8eb 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2b8ec 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t, pExpr->pRight
2b8ed 29 3b 0a 20 20 7d 0a 20 20 70 72 65 72 65 71 41  );.  }.  prereqA
2b8ee 6c 6c 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73  ll = exprTableUs
2b8ef 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  age(pMaskSet, pE
2b8f0 78 70 72 29 3b 0a 20 20 69 66 28 20 45 78 70 72  xpr);.  if( Expr
2b8f1 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2b8f2 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20  r, EP_FromJoin) 
2b8f3 29 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 78  ){.    Bitmask x
2b8f4 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b   = getMask(pMask
2b8f5 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 52 69 67  Set, pExpr->iRig
2b8f6 68 74 4a 6f 69 6e 54 61 62 6c 65 29 3b 0a 20 20  htJoinTable);.  
2b8f7 20 20 70 72 65 72 65 71 41 6c 6c 20 7c 3d 20 78    prereqAll |= x
2b8f8 3b 0a 20 20 20 20 65 78 74 72 61 52 69 67 68 74  ;.    extraRight
2b8f9 20 3d 20 78 2d 31 3b 20 20 2f 2a 20 4f 4e 20 63   = x-1;  /* ON c
2b8fa 6c 61 75 73 65 20 74 65 72 6d 73 20 6d 61 79 20  lause terms may 
2b8fb 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 68  not be used with
2b8fc 20 61 6e 20 69 6e 64 65 78 0a 20 20 20 20 20 20   an index.      
2b8fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8fe 20 2a 2a 20 6f 6e 20 6c 65 66 74 20 74 61 62 6c   ** on left tabl
2b8ff 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e  e of a LEFT JOIN
2b900 2e 20 20 54 69 63 6b 65 74 20 23 33 30 31 35 20  .  Ticket #3015 
2b901 2a 2f 0a 20 20 7d 0a 20 20 70 54 65 72 6d 2d 3e  */.  }.  pTerm->
2b902 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72  prereqAll = prer
2b903 65 71 41 6c 6c 3b 0a 20 20 70 54 65 72 6d 2d 3e  eqAll;.  pTerm->
2b904 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 2d 31 3b  leftCursor = -1;
2b905 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e  .  pTerm->iParen
2b906 74 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d  t = -1;.  pTerm-
2b907 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 0a  >eOperator = 0;.
2b908 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28    if( allowedOp(
2b909 6f 70 29 20 26 26 20 28 70 54 65 72 6d 2d 3e 70  op) && (pTerm->p
2b90a 72 65 72 65 71 52 69 67 68 74 20 26 20 70 72 65  rereqRight & pre
2b90b 72 65 71 4c 65 66 74 29 3d 3d 30 20 29 7b 0a 20  reqLeft)==0 ){. 
2b90c 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
2b90d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
2b90e 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20     Expr *pRight 
2b90f 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  = pExpr->pRight;
2b910 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  .    if( pLeft->
2b911 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b  op==TK_COLUMN ){
2b912 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6c 65  .      pTerm->le
2b913 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74  ftCursor = pLeft
2b914 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
2b915 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  pTerm->u.leftCol
2b916 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f  umn = pLeft->iCo
2b917 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 54 65 72  lumn;.      pTer
2b918 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f  m->eOperator = o
2b919 70 65 72 61 74 6f 72 4d 61 73 6b 28 6f 70 29 3b  peratorMask(op);
2b91a 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
2b91b 52 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d  Right && pRight-
2b91c 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29  >op==TK_COLUMN )
2b91d 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  {.      WhereTer
2b91e 6d 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 45  m *pNew;.      E
2b91f 78 70 72 20 2a 70 44 75 70 3b 0a 20 20 20 20 20  xpr *pDup;.     
2b920 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
2b921 43 75 72 73 6f 72 3e 3d 30 20 29 7b 0a 20 20 20  Cursor>=0 ){.   
2b922 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b       int idxNew;
2b923 0a 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20  .        pDup = 
2b924 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2b925 62 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20  b, pExpr, 0);.  
2b926 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61        if( db->ma
2b927 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2b928 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2b929 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44  xprDelete(db, pD
2b92a 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  up);.          r
2b92b 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d  eturn;.        }
2b92c 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65 77 20  .        idxNew 
2b92d 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
2b92e 65 72 74 28 70 57 43 2c 20 70 44 75 70 2c 20 54  ert(pWC, pDup, T
2b92f 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
2b930 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
2b931 20 20 20 69 66 28 20 69 64 78 4e 65 77 3d 3d 30     if( idxNew==0
2b932 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20   ) return;.     
2b933 20 20 20 70 4e 65 77 20 3d 20 26 70 57 43 2d 3e     pNew = &pWC->
2b934 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20  a[idxNew];.     
2b935 20 20 20 70 4e 65 77 2d 3e 69 50 61 72 65 6e 74     pNew->iParent
2b936 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
2b937 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
2b938 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20  ->a[idxTerm];.  
2b939 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68        pTerm->nCh
2b93a 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ild = 1;.       
2b93b 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2b93c 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a  |= TERM_COPIED;.
2b93d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2b93e 20 20 20 20 20 70 44 75 70 20 3d 20 70 45 78 70       pDup = pExp
2b93f 72 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20  r;.        pNew 
2b940 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d  = pTerm;.      }
2b941 0a 20 20 20 20 20 20 65 78 70 72 43 6f 6d 6d 75  .      exprCommu
2b942 74 65 28 70 50 61 72 73 65 2c 20 70 44 75 70 29  te(pParse, pDup)
2b943 3b 0a 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20  ;.      pLeft = 
2b944 70 44 75 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20  pDup->pLeft;.   
2b945 20 20 20 70 4e 65 77 2d 3e 6c 65 66 74 43 75 72     pNew->leftCur
2b946 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61  sor = pLeft->iTa
2b947 62 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  ble;.      pNew-
2b948 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20  >u.leftColumn = 
2b949 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pLeft->iColumn;.
2b94a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 72        pNew->prer
2b94b 65 71 52 69 67 68 74 20 3d 20 70 72 65 72 65 71  eqRight = prereq
2b94c 4c 65 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77  Left;.      pNew
2b94d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72  ->prereqAll = pr
2b94e 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 20 20 70  ereqAll;.      p
2b94f 4e 65 77 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d  New->eOperator =
2b950 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b 28 70 44   operatorMask(pD
2b951 75 70 2d 3e 6f 70 29 3b 0a 20 20 20 20 7d 0a 20  up->op);.    }. 
2b952 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
2b953 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f  TE_OMIT_BETWEEN_
2b954 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f  OPTIMIZATION.  /
2b955 2a 20 49 66 20 61 20 74 65 72 6d 20 69 73 20 74  * If a term is t
2b956 68 65 20 42 45 54 57 45 45 4e 20 6f 70 65 72 61  he BETWEEN opera
2b957 74 6f 72 2c 20 63 72 65 61 74 65 20 74 77 6f 20  tor, create two 
2b958 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d  new virtual term
2b959 73 0a 20 20 2a 2a 20 74 68 61 74 20 64 65 66 69  s.  ** that defi
2b95a 6e 65 20 74 68 65 20 72 61 6e 67 65 20 74 68 61  ne the range tha
2b95b 74 20 74 68 65 20 42 45 54 57 45 45 4e 20 69 6d  t the BETWEEN im
2b95c 70 6c 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65  plements.  For e
2b95d 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a  xample:.  **.  *
2b95e 2a 20 20 20 20 20 20 61 20 42 45 54 57 45 45 4e  *      a BETWEEN
2b95f 20 62 20 41 4e 44 20 63 0a 20 20 2a 2a 0a 20 20   b AND c.  **.  
2b960 2a 2a 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  ** is converted 
2b961 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  into:.  **.  ** 
2b962 20 20 20 20 20 28 61 20 42 45 54 57 45 45 4e 20       (a BETWEEN 
2b963 62 20 41 4e 44 20 63 29 20 41 4e 44 20 28 61 3e  b AND c) AND (a>
2b964 3d 62 29 20 41 4e 44 20 28 61 3c 3d 63 29 0a 20  =b) AND (a<=c). 
2b965 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 74 77 6f   **.  ** The two
2b966 20 6e 65 77 20 74 65 72 6d 73 20 61 72 65 20 61   new terms are a
2b967 64 64 65 64 20 6f 6e 74 6f 20 74 68 65 20 65 6e  dded onto the en
2b968 64 20 6f 66 20 74 68 65 20 57 68 65 72 65 43 6c  d of the WhereCl
2b969 61 75 73 65 20 6f 62 6a 65 63 74 2e 0a 20 20 2a  ause object..  *
2b96a 2a 20 54 68 65 20 6e 65 77 20 74 65 72 6d 73 20  * The new terms 
2b96b 61 72 65 20 22 64 79 6e 61 6d 69 63 22 20 61 6e  are "dynamic" an
2b96c 64 20 61 72 65 20 63 68 69 6c 64 72 65 6e 20 6f  d are children o
2b96d 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 42  f the original B
2b96e 45 54 57 45 45 4e 0a 20 20 2a 2a 20 74 65 72 6d  ETWEEN.  ** term
2b96f 2e 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68  .  That means th
2b970 61 74 20 69 66 20 74 68 65 20 42 45 54 57 45 45  at if the BETWEE
2b971 4e 20 74 65 72 6d 20 69 73 20 63 6f 64 65 64 2c  N term is coded,
2b972 20 74 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72   the children ar
2b973 65 0a 20 20 2a 2a 20 73 6b 69 70 70 65 64 2e 20  e.  ** skipped. 
2b974 20 4f 72 2c 20 69 66 20 74 68 65 20 63 68 69 6c   Or, if the chil
2b975 64 72 65 6e 20 61 72 65 20 73 61 74 69 73 66 69  dren are satisfi
2b976 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78 2c 20  ed by an index, 
2b977 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a  the original.  *
2b978 2a 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69  * BETWEEN term i
2b979 73 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a  s skipped..  */.
2b97a 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72    else if( pExpr
2b97b 2d 3e 6f 70 3d 3d 54 4b 5f 42 45 54 57 45 45 4e  ->op==TK_BETWEEN
2b97c 20 26 26 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f   && pWC->op==TK_
2b97d 41 4e 44 20 29 7b 0a 20 20 20 20 45 78 70 72 4c  AND ){.    ExprL
2b97e 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78  ist *pList = pEx
2b97f 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20  pr->x.pList;.   
2b980 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 74 61 74   int i;.    stat
2b981 69 63 20 63 6f 6e 73 74 20 75 38 20 6f 70 73 5b  ic const u8 ops[
2b982 5d 20 3d 20 7b 54 4b 5f 47 45 2c 20 54 4b 5f 4c  ] = {TK_GE, TK_L
2b983 45 7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  E};.    assert( 
2b984 70 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20  pList!=0 );.    
2b985 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e  assert( pList->n
2b986 45 78 70 72 3d 3d 32 20 29 3b 0a 20 20 20 20 66  Expr==2 );.    f
2b987 6f 72 28 69 3d 30 3b 20 69 3c 32 3b 20 69 2b 2b  or(i=0; i<2; i++
2b988 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
2b989 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20 69  NewExpr;.      i
2b98a 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20  nt idxNew;.     
2b98b 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69   pNewExpr = sqli
2b98c 74 65 33 45 78 70 72 28 64 62 2c 20 6f 70 73 5b  te3Expr(db, ops[
2b98d 69 5d 2c 20 73 71 6c 69 74 65 33 45 78 70 72 44  i], sqlite3ExprD
2b98e 75 70 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c  up(db, pExpr->pL
2b98f 65 66 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20  eft, 0),.       
2b990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b991 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2b992 72 44 75 70 28 64 62 2c 20 70 4c 69 73 74 2d 3e  rDup(db, pList->
2b993 61 5b 69 5d 2e 70 45 78 70 72 2c 20 30 29 2c 20  a[i].pExpr, 0), 
2b994 30 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65 77  0);.      idxNew
2b995 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e   = whereClauseIn
2b996 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78  sert(pWC, pNewEx
2b997 70 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  pr, TERM_VIRTUAL
2b998 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
2b999 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b99a 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20  idxNew==0 );.   
2b99b 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70     exprAnalyze(p
2b99c 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77  Src, pWC, idxNew
2b99d 29 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d  );.      pTerm =
2b99e 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
2b99f 5d 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b  ];.      pWC->a[
2b9a0 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20  idxNew].iParent 
2b9a1 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 7d  = idxTerm;.    }
2b9a2 0a 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69  .    pTerm->nChi
2b9a3 6c 64 20 3d 20 32 3b 0a 20 20 7d 0a 23 65 6e 64  ld = 2;.  }.#end
2b9a4 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2b9a5 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49  T_BETWEEN_OPTIMI
2b9a6 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 20 21  ZATION */..#if !
2b9a7 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2b9a8 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54  MIT_OR_OPTIMIZAT
2b9a9 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e 65 64  ION) && !defined
2b9aa 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
2b9ab 51 55 45 52 59 29 0a 20 20 2f 2a 20 41 6e 61 6c  QUERY).  /* Anal
2b9ac 79 7a 65 20 61 20 74 65 72 6d 20 74 68 61 74 20  yze a term that 
2b9ad 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 74  is composed of t
2b9ae 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 62 74 65  wo or more subte
2b9af 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  rms connected by
2b9b0 0a 20 20 2a 2a 20 61 6e 20 4f 52 20 6f 70 65 72  .  ** an OR oper
2b9b1 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73  ator..  */.  els
2b9b2 65 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d  e if( pExpr->op=
2b9b3 3d 54 4b 5f 4f 52 20 29 7b 0a 20 20 20 20 61 73  =TK_OR ){.    as
2b9b4 73 65 72 74 28 20 70 57 43 2d 3e 6f 70 3d 3d 54  sert( pWC->op==T
2b9b5 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20 65 78 70  K_AND );.    exp
2b9b6 72 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28 70  rAnalyzeOrTerm(p
2b9b7 53 72 63 2c 20 70 57 43 2c 20 69 64 78 54 65 72  Src, pWC, idxTer
2b9b8 6d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  m);.  }.#endif /
2b9b9 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52  * SQLITE_OMIT_OR
2b9ba 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
2b9bb 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2b9bc 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d  _OMIT_LIKE_OPTIM
2b9bd 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 41 64 64  IZATION.  /* Add
2b9be 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20   constraints to 
2b9bf 72 65 64 75 63 65 20 74 68 65 20 73 65 61 72 63  reduce the searc
2b9c0 68 20 73 70 61 63 65 20 6f 6e 20 61 20 4c 49 4b  h space on a LIK
2b9c1 45 20 6f 72 20 47 4c 4f 42 0a 20 20 2a 2a 20 6f  E or GLOB.  ** o
2b9c2 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 0a 20 20  perator..  **.  
2b9c3 2a 2a 20 41 20 6c 69 6b 65 20 70 61 74 74 65 72  ** A like patter
2b9c4 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78  n of the form "x
2b9c5 20 4c 49 4b 45 20 27 61 62 63 25 27 22 20 69 73   LIKE 'abc%'" is
2b9c6 20 63 68 61 6e 67 65 64 20 69 6e 74 6f 20 63 6f   changed into co
2b9c7 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 0a 20  nstraints.  **. 
2b9c8 20 2a 2a 20 20 20 20 20 20 20 20 20 20 78 3e 3d   **          x>=
2b9c9 27 61 62 63 27 20 41 4e 44 20 78 3c 27 61 62 64  'abc' AND x<'abd
2b9ca 27 20 41 4e 44 20 78 20 4c 49 4b 45 20 27 61 62  ' AND x LIKE 'ab
2b9cb 63 25 27 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  c%'.  **.  ** Th
2b9cc 65 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72  e last character
2b9cd 20 6f 66 20 74 68 65 20 70 72 65 66 69 78 20 22   of the prefix "
2b9ce 61 62 63 22 20 69 73 20 69 6e 63 72 65 6d 65 6e  abc" is incremen
2b9cf 74 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 0a  ted to form the.
2b9d0 20 20 2a 2a 20 74 65 72 6d 69 6e 61 74 69 6f 6e    ** termination
2b9d1 20 63 6f 6e 64 69 74 69 6f 6e 20 22 61 62 64 22   condition "abd"
2b9d2 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4c  ..  */.  if( isL
2b9d3 69 6b 65 4f 72 47 6c 6f 62 28 70 50 61 72 73 65  ikeOrGlob(pParse
2b9d4 2c 20 70 45 78 70 72 2c 20 26 6e 50 61 74 74 65  , pExpr, &nPatte
2b9d5 72 6e 2c 20 26 69 73 43 6f 6d 70 6c 65 74 65 2c  rn, &isComplete,
2b9d6 20 26 6e 6f 43 61 73 65 29 0a 20 20 20 20 20 20   &noCase).      
2b9d7 20 20 20 26 26 20 70 57 43 2d 3e 6f 70 3d 3d 54     && pWC->op==T
2b9d8 4b 5f 41 4e 44 20 29 7b 0a 20 20 20 20 45 78 70  K_AND ){.    Exp
2b9d9 72 20 2a 70 4c 65 66 74 2c 20 2a 70 52 69 67 68  r *pLeft, *pRigh
2b9da 74 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 53 74  t;.    Expr *pSt
2b9db 72 31 2c 20 2a 70 53 74 72 32 3b 0a 20 20 20 20  r1, *pStr2;.    
2b9dc 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 31 2c  Expr *pNewExpr1,
2b9dd 20 2a 70 4e 65 77 45 78 70 72 32 3b 0a 20 20 20   *pNewExpr2;.   
2b9de 20 69 6e 74 20 69 64 78 4e 65 77 31 2c 20 69 64   int idxNew1, id
2b9df 78 4e 65 77 32 3b 0a 0a 20 20 20 20 70 4c 65 66  xNew2;..    pLef
2b9e0 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2b9e1 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a  st->a[1].pExpr;.
2b9e2 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78      pRight = pEx
2b9e3 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30  pr->x.pList->a[0
2b9e4 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 53 74  ].pExpr;.    pSt
2b9e5 72 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  r1 = sqlite3PExp
2b9e6 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 53 54 52  r(pParse, TK_STR
2b9e7 49 4e 47 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ING, 0, 0, 0);. 
2b9e8 20 20 20 69 66 28 20 70 53 74 72 31 20 29 7b 0a     if( pStr1 ){.
2b9e9 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 6f 6b        sqlite3Tok
2b9ea 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 53 74 72  enCopy(db, &pStr
2b9eb 31 2d 3e 74 6f 6b 65 6e 2c 20 26 70 52 69 67 68  1->token, &pRigh
2b9ec 74 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  t->token);.     
2b9ed 20 70 53 74 72 31 2d 3e 74 6f 6b 65 6e 2e 6e 20   pStr1->token.n 
2b9ee 3d 20 6e 50 61 74 74 65 72 6e 3b 0a 20 20 20 20  = nPattern;.    
2b9ef 7d 0a 20 20 20 20 70 53 74 72 32 20 3d 20 73 71  }.    pStr2 = sq
2b9f0 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2b9f1 20 70 53 74 72 31 2c 20 30 29 3b 0a 20 20 20 20   pStr1, 0);.    
2b9f2 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
2b9f3 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 75  ailed ){.      u
2b9f4 38 20 63 2c 20 2a 70 43 3b 0a 20 20 20 20 20 20  8 c, *pC;.      
2b9f5 2f 2a 20 61 73 73 65 72 74 28 20 70 53 74 72 32  /* assert( pStr2
2b9f6 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20 29 3b 20 2a  ->token.dyn ); *
2b9f7 2f 0a 20 20 20 20 20 20 70 43 20 3d 20 28 75 38  /.      pC = (u8
2b9f8 2a 29 26 70 53 74 72 32 2d 3e 74 6f 6b 65 6e 2e  *)&pStr2->token.
2b9f9 7a 5b 6e 50 61 74 74 65 72 6e 2d 31 5d 3b 0a 20  z[nPattern-1];. 
2b9fa 20 20 20 20 20 63 20 3d 20 2a 70 43 3b 0a 20 20       c = *pC;.  
2b9fb 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29      if( noCase )
2b9fc 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d  {.        if( c=
2b9fd 3d 27 40 27 20 29 20 69 73 43 6f 6d 70 6c 65 74  ='@' ) isComplet
2b9fe 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63  e = 0;.        c
2b9ff 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54   = sqlite3UpperT
2ba00 6f 4c 6f 77 65 72 5b 63 5d 3b 0a 20 20 20 20 20  oLower[c];.     
2ba01 20 7d 0a 20 20 20 20 20 20 2a 70 43 20 3d 20 63   }.      *pC = c
2ba02 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   + 1;.    }.    
2ba03 70 4e 65 77 45 78 70 72 31 20 3d 20 73 71 6c 69  pNewExpr1 = sqli
2ba04 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2ba05 20 54 4b 5f 47 45 2c 20 73 71 6c 69 74 65 33 45   TK_GE, sqlite3E
2ba06 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74 2c  xprDup(db,pLeft,
2ba07 30 29 2c 70 53 74 72 31 2c 30 29 3b 0a 20 20 20  0),pStr1,0);.   
2ba08 20 69 64 78 4e 65 77 31 20 3d 20 77 68 65 72 65   idxNew1 = where
2ba09 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
2ba0a 2c 20 70 4e 65 77 45 78 70 72 31 2c 20 54 45 52  , pNewExpr1, TER
2ba0b 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2ba0c 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 73  YNAMIC);.    tes
2ba0d 74 63 61 73 65 28 20 69 64 78 4e 65 77 31 3d 3d  tcase( idxNew1==
2ba0e 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61  0 );.    exprAna
2ba0f 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20  lyze(pSrc, pWC, 
2ba10 69 64 78 4e 65 77 31 29 3b 0a 20 20 20 20 70 4e  idxNew1);.    pN
2ba11 65 77 45 78 70 72 32 20 3d 20 73 71 6c 69 74 65  ewExpr2 = sqlite
2ba12 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2ba13 4b 5f 4c 54 2c 20 73 71 6c 69 74 65 33 45 78 70  K_LT, sqlite3Exp
2ba14 72 44 75 70 28 64 62 2c 70 4c 65 66 74 2c 30 29  rDup(db,pLeft,0)
2ba15 2c 70 53 74 72 32 2c 30 29 3b 0a 20 20 20 20 69  ,pStr2,0);.    i
2ba16 64 78 4e 65 77 32 20 3d 20 77 68 65 72 65 43 6c  dxNew2 = whereCl
2ba17 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
2ba18 70 4e 65 77 45 78 70 72 32 2c 20 54 45 52 4d 5f  pNewExpr2, TERM_
2ba19 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e  VIRTUAL|TERM_DYN
2ba1a 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 73 74 63  AMIC);.    testc
2ba1b 61 73 65 28 20 69 64 78 4e 65 77 32 3d 3d 30 20  ase( idxNew2==0 
2ba1c 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79  );.    exprAnaly
2ba1d 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64  ze(pSrc, pWC, id
2ba1e 78 4e 65 77 32 29 3b 0a 20 20 20 20 70 54 65 72  xNew2);.    pTer
2ba1f 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
2ba20 65 72 6d 5d 3b 0a 20 20 20 20 69 66 28 20 69 73  erm];.    if( is
2ba21 43 6f 6d 70 6c 65 74 65 20 29 7b 0a 20 20 20 20  Complete ){.    
2ba22 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 31    pWC->a[idxNew1
2ba23 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ].iParent = idxT
2ba24 65 72 6d 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e  erm;.      pWC->
2ba25 61 5b 69 64 78 4e 65 77 32 5d 2e 69 50 61 72 65  a[idxNew2].iPare
2ba26 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20  nt = idxTerm;.  
2ba27 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
2ba28 64 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 2;.    }.  }
2ba29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2ba2a 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49  E_OMIT_LIKE_OPTI
2ba2b 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66  MIZATION */..#if
2ba2c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ba2d 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2ba2e 2f 2a 20 41 64 64 20 61 20 57 4f 5f 4d 41 54 43  /* Add a WO_MATC
2ba2f 48 20 61 75 78 69 6c 69 61 72 79 20 74 65 72 6d  H auxiliary term
2ba30 20 74 6f 20 74 68 65 20 63 6f 6e 73 74 72 61 69   to the constrai
2ba31 6e 74 20 73 65 74 20 69 66 20 74 68 65 0a 20 20  nt set if the.  
2ba32 2a 2a 20 63 75 72 72 65 6e 74 20 65 78 70 72 65  ** current expre
2ba33 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20  ssion is of the 
2ba34 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e 20 4d 41  form:  column MA
2ba35 54 43 48 20 65 78 70 72 2e 0a 20 20 2a 2a 20 54  TCH expr..  ** T
2ba36 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
2ba37 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 78  is used by the x
2ba38 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64  BestIndex method
2ba39 73 20 6f 66 0a 20 20 2a 2a 20 76 69 72 74 75 61  s of.  ** virtua
2ba3a 6c 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20 6e  l tables.  The n
2ba3b 61 74 69 76 65 20 71 75 65 72 79 20 6f 70 74 69  ative query opti
2ba3c 6d 69 7a 65 72 20 64 6f 65 73 20 6e 6f 74 20 61  mizer does not a
2ba3d 74 74 65 6d 70 74 0a 20 20 2a 2a 20 74 6f 20 64  ttempt.  ** to d
2ba3e 6f 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68 20  o anything with 
2ba3f 4d 41 54 43 48 20 66 75 6e 63 74 69 6f 6e 73 2e  MATCH functions.
2ba40 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4d 61  .  */.  if( isMa
2ba41 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 70 45 78 70  tchOfColumn(pExp
2ba42 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64  r) ){.    int id
2ba43 78 4e 65 77 3b 0a 20 20 20 20 45 78 70 72 20 2a  xNew;.    Expr *
2ba44 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74 3b 0a  pRight, *pLeft;.
2ba45 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2ba46 4e 65 77 54 65 72 6d 3b 0a 20 20 20 20 42 69 74  NewTerm;.    Bit
2ba47 6d 61 73 6b 20 70 72 65 72 65 71 43 6f 6c 75 6d  mask prereqColum
2ba48 6e 2c 20 70 72 65 72 65 71 45 78 70 72 3b 0a 0a  n, prereqExpr;..
2ba49 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78      pRight = pEx
2ba4a 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30  pr->x.pList->a[0
2ba4b 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 4c 65  ].pExpr;.    pLe
2ba4c 66 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ft = pExpr->x.pL
2ba4d 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b  ist->a[1].pExpr;
2ba4e 0a 20 20 20 20 70 72 65 72 65 71 45 78 70 72 20  .    prereqExpr 
2ba4f 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2ba50 28 70 4d 61 73 6b 53 65 74 2c 20 70 52 69 67 68  (pMaskSet, pRigh
2ba51 74 29 3b 0a 20 20 20 20 70 72 65 72 65 71 43 6f  t);.    prereqCo
2ba52 6c 75 6d 6e 20 3d 20 65 78 70 72 54 61 62 6c 65  lumn = exprTable
2ba53 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2ba54 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20  pLeft);.    if( 
2ba55 28 70 72 65 72 65 71 45 78 70 72 20 26 20 70 72  (prereqExpr & pr
2ba56 65 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29  ereqColumn)==0 )
2ba57 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  {.      Expr *pN
2ba58 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4e  ewExpr;.      pN
2ba59 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  ewExpr = sqlite3
2ba5a 45 78 70 72 28 64 62 2c 20 54 4b 5f 4d 41 54 43  Expr(db, TK_MATC
2ba5b 48 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70  H, 0, sqlite3Exp
2ba5c 72 44 75 70 28 64 62 2c 20 70 52 69 67 68 74 2c  rDup(db, pRight,
2ba5d 20 30 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 69   0), 0);.      i
2ba5e 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61  dxNew = whereCla
2ba5f 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
2ba60 4e 65 77 45 78 70 72 2c 20 54 45 52 4d 5f 56 49  NewExpr, TERM_VI
2ba61 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d  RTUAL|TERM_DYNAM
2ba62 49 43 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  IC);.      testc
2ba63 61 73 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29  ase( idxNew==0 )
2ba64 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d  ;.      pNewTerm
2ba65 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65   = &pWC->a[idxNe
2ba66 77 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65  w];.      pNewTe
2ba67 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
2ba68 3d 20 70 72 65 72 65 71 45 78 70 72 3b 0a 20 20  = prereqExpr;.  
2ba69 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 6c 65      pNewTerm->le
2ba6a 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74  ftCursor = pLeft
2ba6b 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20  ->iTable;.      
2ba6c 70 4e 65 77 54 65 72 6d 2d 3e 75 2e 6c 65 66 74  pNewTerm->u.left
2ba6d 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e  Column = pLeft->
2ba6e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70  iColumn;.      p
2ba6f 4e 65 77 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  NewTerm->eOperat
2ba70 6f 72 20 3d 20 57 4f 5f 4d 41 54 43 48 3b 0a 20  or = WO_MATCH;. 
2ba71 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 69       pNewTerm->i
2ba72 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d  Parent = idxTerm
2ba73 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20  ;.      pTerm = 
2ba74 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d  &pWC->a[idxTerm]
2ba75 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e  ;.      pTerm->n
2ba76 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20  Child = 1;.     
2ba77 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2ba78 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a  |= TERM_COPIED;.
2ba79 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e        pNewTerm->
2ba7a 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 54 65 72  prereqAll = pTer
2ba7b 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20  m->prereqAll;.  
2ba7c 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f    }.  }.#endif /
2ba7d 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
2ba7e 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 20  RTUALTABLE */.. 
2ba7f 20 2f 2a 20 50 72 65 76 65 6e 74 20 4f 4e 20 63   /* Prevent ON c
2ba80 6c 61 75 73 65 20 74 65 72 6d 73 20 6f 66 20 61  lause terms of a
2ba81 20 4c 45 46 54 20 4a 4f 49 4e 20 66 72 6f 6d 20   LEFT JOIN from 
2ba82 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 64 72  being used to dr
2ba83 69 76 65 0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65  ive.  ** an inde
2ba84 78 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20  x for tables to 
2ba85 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20  the left of the 
2ba86 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 70 54 65  join..  */.  pTe
2ba87 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
2ba88 7c 3d 20 65 78 74 72 61 52 69 67 68 74 3b 0a 7d  |= extraRight;.}
2ba89 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
2ba8a 52 55 45 20 69 66 20 61 6e 79 20 6f 66 20 74 68  RUE if any of th
2ba8b 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  e expressions in
2ba8c 20 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73 74   pList->a[iFirst
2ba8d 2e 2e 2e 5d 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20  ...] contain.** 
2ba8e 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61  a reference to a
2ba8f 6e 79 20 74 61 62 6c 65 20 6f 74 68 65 72 20 74  ny table other t
2ba90 68 61 6e 20 74 68 65 20 69 42 61 73 65 20 74 61  han the iBase ta
2ba91 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
2ba92 6e 74 20 72 65 66 65 72 65 6e 63 65 73 4f 74 68  nt referencesOth
2ba93 65 72 54 61 62 6c 65 73 28 0a 20 20 45 78 70 72  erTables(.  Expr
2ba94 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20  List *pList,    
2ba95 20 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20        /* Search 
2ba96 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74  expressions in t
2ba97 68 73 20 6c 69 73 74 20 2a 2f 0a 20 20 57 68 65  hs list */.  Whe
2ba98 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
2ba99 53 65 74 2c 20 20 20 2f 2a 20 4d 61 70 70 69 6e  Set,   /* Mappin
2ba9a 67 20 66 72 6f 6d 20 74 61 62 6c 65 73 20 74 6f  g from tables to
2ba9b 20 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 69 6e   bitmaps */.  in
2ba9c 74 20 69 46 69 72 73 74 2c 20 20 20 20 20 20 20  t iFirst,       
2ba9d 20 20 20 20 20 20 20 20 2f 2a 20 42 65 20 73 65          /* Be se
2ba9e 61 72 63 68 69 6e 67 20 77 69 74 68 20 74 68 65  arching with the
2ba9f 20 69 46 69 72 73 74 2d 74 68 20 65 78 70 72 65   iFirst-th expre
2baa0 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ssion */.  int i
2baa1 42 61 73 65 20 20 20 20 20 20 20 20 20 20 20 20  Base            
2baa2 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 72       /* Ignore r
2baa3 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69  eferences to thi
2baa4 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  s table */.){.  
2baa5 42 69 74 6d 61 73 6b 20 61 6c 6c 6f 77 65 64 20  Bitmask allowed 
2baa6 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b  = ~getMask(pMask
2baa7 53 65 74 2c 20 69 42 61 73 65 29 3b 0a 20 20 77  Set, iBase);.  w
2baa8 68 69 6c 65 28 20 69 46 69 72 73 74 3c 70 4c 69  hile( iFirst<pLi
2baa9 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
2baaa 20 69 66 28 20 28 65 78 70 72 54 61 62 6c 65 55   if( (exprTableU
2baab 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2baac 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73 74 2b 2b  List->a[iFirst++
2baad 5d 2e 70 45 78 70 72 29 26 61 6c 6c 6f 77 65 64  ].pExpr)&allowed
2baae 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )!=0 ){.      re
2baaf 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2bab0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2bab1 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2bab2 74 69 6e 65 20 64 65 63 69 64 65 73 20 69 66 20  tine decides if 
2bab3 70 49 64 78 20 63 61 6e 20 62 65 20 75 73 65 64  pIdx can be used
2bab4 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20   to satisfy the 
2bab5 4f 52 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61 75  ORDER BY.** clau
2bab6 73 65 2e 20 20 49 66 20 69 74 20 63 61 6e 2c 20  se.  If it can, 
2bab7 69 74 20 72 65 74 75 72 6e 73 20 31 2e 20 20 49  it returns 1.  I
2bab8 66 20 70 49 64 78 20 63 61 6e 6e 6f 74 20 73 61  f pIdx cannot sa
2bab9 74 69 73 66 79 20 74 68 65 0a 2a 2a 20 4f 52 44  tisfy the.** ORD
2baba 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68  ER BY clause, th
2babb 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
2babc 6e 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 4f 72 64  ns 0..**.** pOrd
2babd 65 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45 52  erBy is an ORDER
2babe 20 42 59 20 63 6c 61 75 73 65 20 66 72 6f 6d 20   BY clause from 
2babf 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
2bac0 6e 74 2e 20 20 70 54 61 62 20 69 73 20 74 68 65  nt.  pTab is the
2bac1 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 61  .** left-most ta
2bac2 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
2bac3 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20 73  clause of that s
2bac4 61 6d 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  ame SELECT state
2bac5 6d 65 6e 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20  ment and.** the 
2bac6 74 61 62 6c 65 20 68 61 73 20 61 20 63 75 72 73  table has a curs
2bac7 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 22 62 61  or number of "ba
2bac8 73 65 22 2e 20 20 70 49 64 78 20 69 73 20 61 6e  se".  pIdx is an
2bac9 20 69 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a   index on pTab..
2baca 2a 2a 0a 2a 2a 20 6e 45 71 43 6f 6c 20 69 73 20  **.** nEqCol is 
2bacb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
2bacc 6c 75 6d 6e 73 20 6f 66 20 70 49 64 78 20 74 68  lumns of pIdx th
2bacd 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 65  at are used as e
2bace 71 75 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73 74  quality.** const
2bacf 72 61 69 6e 74 73 2e 20 20 41 6e 79 20 6f 66 20  raints.  Any of 
2bad0 74 68 65 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 61  these columns ma
2bad1 79 20 62 65 20 6d 69 73 73 69 6e 67 20 66 72 6f  y be missing fro
2bad2 6d 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 2a  m the ORDER BY.*
2bad3 2a 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65  * clause and the
2bad4 20 6d 61 74 63 68 20 63 61 6e 20 73 74 69 6c 6c   match can still
2bad5 20 62 65 20 61 20 73 75 63 63 65 73 73 2e 0a 2a   be a success..*
2bad6 2a 0a 2a 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f  *.** All terms o
2bad7 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74  f the ORDER BY t
2bad8 68 61 74 20 6d 61 74 63 68 20 61 67 61 69 6e 73  hat match agains
2bad9 74 20 74 68 65 20 69 6e 64 65 78 20 6d 75 73 74  t the index must
2bada 20 62 65 20 65 69 74 68 65 72 0a 2a 2a 20 41 53   be either.** AS
2badb 43 20 6f 72 20 44 45 53 43 2e 20 20 28 54 65 72  C or DESC.  (Ter
2badc 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  ms of the ORDER 
2badd 42 59 20 63 6c 61 75 73 65 20 70 61 73 74 20 74  BY clause past t
2bade 68 65 20 65 6e 64 20 6f 66 20 61 20 55 4e 49 51  he end of a UNIQ
2badf 55 45 0a 2a 2a 20 69 6e 64 65 78 20 64 6f 20 6e  UE.** index do n
2bae0 6f 74 20 6e 65 65 64 20 74 6f 20 73 61 74 69 73  ot need to satis
2bae1 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  fy this constrai
2bae2 6e 74 2e 29 20 20 54 68 65 20 2a 70 62 52 65 76  nt.)  The *pbRev
2bae3 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74   value is.** set
2bae4 20 74 6f 20 31 20 69 66 20 74 68 65 20 4f 52 44   to 1 if the ORD
2bae5 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ER BY clause is 
2bae6 61 6c 6c 20 44 45 53 43 20 61 6e 64 20 69 74 20  all DESC and it 
2bae7 69 73 20 73 65 74 20 74 6f 20 30 20 69 66 0a 2a  is set to 0 if.*
2bae8 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  * the ORDER BY c
2bae9 6c 61 75 73 65 20 69 73 20 61 6c 6c 20 41 53 43  lause is all ASC
2baea 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2baeb 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 0a  isSortingIndex(.
2baec 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2baed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2baee 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2baef 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a    WhereMaskSet *
2baf0 70 4d 61 73 6b 53 65 74 2c 20 2f 2a 20 4d 61 70  pMaskSet, /* Map
2baf1 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 20  ping from table 
2baf2 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 74  cursor numbers t
2baf3 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 49  o bitmaps */.  I
2baf4 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20  ndex *pIdx,     
2baf5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
2baf6 64 65 78 20 77 65 20 61 72 65 20 74 65 73 74 69  dex we are testi
2baf7 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65  ng */.  int base
2baf8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2baf9 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
2bafa 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 74   for the table t
2bafb 6f 20 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a 20  o be sorted */. 
2bafc 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2bafd 72 42 79 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  rBy,     /* The 
2bafe 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2baff 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 43 6f 6c 2c  */.  int nEqCol,
2bb00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bb01 4e 75 6d 62 65 72 20 6f 66 20 69 6e 64 65 78 20  Number of index 
2bb02 63 6f 6c 75 6d 6e 73 20 77 69 74 68 20 3d 3d 20  columns with == 
2bb03 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20  constraints */. 
2bb04 20 69 6e 74 20 2a 70 62 52 65 76 20 20 20 20 20   int *pbRev     
2bb05 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20           /* Set 
2bb06 74 6f 20 31 20 69 66 20 4f 52 44 45 52 20 42 59  to 1 if ORDER BY
2bb07 20 69 73 20 44 45 53 43 20 2a 2f 0a 29 7b 0a 20   is DESC */.){. 
2bb08 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
2bb09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb0a 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2bb0b 73 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f  s */.  int sortO
2bb0c 72 64 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20  rder = 0;       
2bb0d 20 20 20 20 20 20 20 2f 2a 20 58 4f 52 20 6f 66         /* XOR of
2bb0e 20 69 6e 64 65 78 20 61 6e 64 20 4f 52 44 45 52   index and ORDER
2bb0f 20 42 59 20 73 6f 72 74 20 64 69 72 65 63 74 69   BY sort directi
2bb10 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  on */.  int nTer
2bb11 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
2bb12 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2bb13 72 20 6f 66 20 4f 52 44 45 52 20 42 59 20 74 65  r of ORDER BY te
2bb14 72 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  rms */.  struct 
2bb15 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
2bb16 54 65 72 6d 3b 20 20 20 20 2f 2a 20 41 20 74 65  Term;    /* A te
2bb17 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20  rm of the ORDER 
2bb18 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73  BY clause */.  s
2bb19 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2bb1a 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65  rse->db;..  asse
2bb1b 72 74 28 20 70 4f 72 64 65 72 42 79 21 3d 30 20  rt( pOrderBy!=0 
2bb1c 29 3b 0a 20 20 6e 54 65 72 6d 20 3d 20 70 4f 72  );.  nTerm = pOr
2bb1d 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20  derBy->nExpr;.  
2bb1e 61 73 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20  assert( nTerm>0 
2bb1f 29 3b 0a 0a 20 20 2f 2a 20 4d 61 74 63 68 20 74  );..  /* Match t
2bb20 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45  erms of the ORDE
2bb21 52 20 42 59 20 63 6c 61 75 73 65 20 61 67 61 69  R BY clause agai
2bb22 6e 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20  nst columns of. 
2bb23 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 2e 0a 20   ** the index.. 
2bb24 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68   **.  ** Note th
2bb25 61 74 20 69 6e 64 69 63 65 73 20 68 61 76 65 20  at indices have 
2bb26 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72 65  pIdx->nColumn re
2bb27 67 75 6c 61 72 20 63 6f 6c 75 6d 6e 73 20 70 6c  gular columns pl
2bb28 75 73 0a 20 20 2a 2a 20 6f 6e 65 20 61 64 64 69  us.  ** one addi
2bb29 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20 63 6f  tional column co
2bb2a 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77  ntaining the row
2bb2b 69 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 63  id.  The rowid c
2bb2c 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 66 20 74 68  olumn.  ** of th
2bb2d 65 20 69 6e 64 65 78 20 69 73 20 61 6c 73 6f 20  e index is also 
2bb2e 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63 68  allowed to match
2bb2f 20 61 67 61 69 6e 73 74 20 74 68 65 20 4f 52 44   against the ORD
2bb30 45 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75 73  ER BY.  ** claus
2bb31 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  e..  */.  for(i=
2bb32 6a 3d 30 2c 20 70 54 65 72 6d 3d 70 4f 72 64 65  j=0, pTerm=pOrde
2bb33 72 42 79 2d 3e 61 3b 20 6a 3c 6e 54 65 72 6d 20  rBy->a; j<nTerm 
2bb34 26 26 20 69 3c 3d 70 49 64 78 2d 3e 6e 43 6f 6c  && i<=pIdx->nCol
2bb35 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45  umn; i++){.    E
2bb36 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20  xpr *pExpr;     
2bb37 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73    /* The express
2bb38 69 6f 6e 20 6f 66 20 74 68 65 20 4f 52 44 45 52  ion of the ORDER
2bb39 20 42 59 20 70 54 65 72 6d 20 2a 2f 0a 20 20 20   BY pTerm */.   
2bb3a 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
2bb3b 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61      /* The colla
2bb3c 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66  ting sequence of
2bb3d 20 70 45 78 70 72 20 2a 2f 0a 20 20 20 20 69 6e   pExpr */.    in
2bb3e 74 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b  t termSortOrder;
2bb3f 20 2f 2a 20 53 6f 72 74 20 6f 72 64 65 72 20 66   /* Sort order f
2bb40 6f 72 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a  or this term */.
2bb41 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b      int iColumn;
2bb42 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 2d         /* The i-
2bb43 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
2bb44 20 69 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72 20   index.  -1 for 
2bb45 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74  rowid */.    int
2bb46 20 69 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20   iSortOrder;    
2bb47 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 2c 20 30  /* 1 for DESC, 0
2bb48 20 66 6f 72 20 41 53 43 20 6f 6e 20 74 68 65 20   for ASC on the 
2bb49 69 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20  i-th index term 
2bb4a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  */.    const cha
2bb4b 72 20 2a 7a 43 6f 6c 6c 3b 20 2f 2a 20 4e 61 6d  r *zColl; /* Nam
2bb4c 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  e of the collati
2bb4d 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
2bb4e 69 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d 20  i-th index term 
2bb4f 2a 2f 0a 0a 20 20 20 20 70 45 78 70 72 20 3d 20  */..    pExpr = 
2bb50 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
2bb51 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21    if( pExpr->op!
2bb52 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45  =TK_COLUMN || pE
2bb53 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d 62 61 73  xpr->iTable!=bas
2bb54 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61  e ){.      /* Ca
2bb55 6e 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64  n not use an ind
2bb56 65 78 20 73 6f 72 74 20 6f 6e 20 61 6e 79 74 68  ex sort on anyth
2bb57 69 6e 67 20 74 68 61 74 20 69 73 20 6e 6f 74 20  ing that is not 
2bb58 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 0a  a column in the.
2bb59 20 20 20 20 20 20 2a 2a 20 6c 65 66 74 2d 6d 6f        ** left-mo
2bb5a 73 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20  st table of the 
2bb5b 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
2bb5c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2bb5d 7d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71  }.    pColl = sq
2bb5e 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
2bb5f 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b  (pParse, pExpr);
2bb60 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20  .    if( !pColl 
2bb61 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d  ){.      pColl =
2bb62 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a   db->pDfltColl;.
2bb63 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c      }.    if( i<
2bb64 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b  pIdx->nColumn ){
2bb65 0a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d  .      iColumn =
2bb66 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
2bb67 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43  i];.      if( iC
2bb68 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54 61  olumn==pIdx->pTa
2bb69 62 6c 65 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  ble->iPKey ){.  
2bb6a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20        iColumn = 
2bb6b 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
2bb6c 20 20 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 70    iSortOrder = p
2bb6d 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Idx->aSortOrder[
2bb6e 69 5d 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20  i];.      zColl 
2bb6f 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  = pIdx->azColl[i
2bb70 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ];.    }else{.  
2bb71 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31      iColumn = -1
2bb72 3b 0a 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64  ;.      iSortOrd
2bb73 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 43  er = 0;.      zC
2bb74 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  oll = pColl->zNa
2bb75 6d 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  me;.    }.    if
2bb76 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
2bb77 21 3d 69 43 6f 6c 75 6d 6e 20 7c 7c 20 73 71 6c  !=iColumn || sql
2bb78 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c  ite3StrICmp(pCol
2bb79 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29  l->zName, zColl)
2bb7a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 65 72   ){.      /* Ter
2bb7b 6d 20 6a 20 6f 66 20 74 68 65 20 4f 52 44 45 52  m j of the ORDER
2bb7c 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20   BY clause does 
2bb7d 6e 6f 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e  not match column
2bb7e 20 69 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20   i of the index 
2bb7f 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 3c 6e  */.      if( i<n
2bb80 45 71 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20  EqCol ){.       
2bb81 20 2f 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20   /* If an index 
2bb82 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 63  column that is c
2bb83 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d  onstrained by ==
2bb84 20 66 61 69 6c 73 20 74 6f 20 6d 61 74 63 68 20   fails to match 
2bb85 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 52  an.        ** OR
2bb86 44 45 52 20 42 59 20 74 65 72 6d 2c 20 74 68 61  DER BY term, tha
2bb87 74 20 69 73 20 4f 4b 2e 20 20 4a 75 73 74 20 69  t is OK.  Just i
2bb88 67 6e 6f 72 65 20 74 68 61 74 20 63 6f 6c 75 6d  gnore that colum
2bb89 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 20  n of the index. 
2bb8a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2bb8b 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2bb8c 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3d 3d 70    }else if( i==p
2bb8d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  Idx->nColumn ){.
2bb8e 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2bb8f 20 63 6f 6c 75 6d 6e 20 69 20 69 73 20 74 68 65   column i is the
2bb90 20 72 6f 77 69 64 2e 20 20 41 6c 6c 20 6f 74 68   rowid.  All oth
2bb91 65 72 20 74 65 72 6d 73 20 6d 61 74 63 68 2e 20  er terms match. 
2bb92 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  */.        break
2bb93 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2bb94 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20         /* If an 
2bb95 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 66 61 69  index column fai
2bb96 6c 73 20 74 6f 20 6d 61 74 63 68 20 61 6e 64 20  ls to match and 
2bb97 69 73 20 6e 6f 74 20 63 6f 6e 73 74 72 61 69 6e  is not constrain
2bb98 65 64 20 62 79 20 3d 3d 0a 20 20 20 20 20 20 20  ed by ==.       
2bb99 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 69 6e 64   ** then the ind
2bb9a 65 78 20 63 61 6e 6e 6f 74 20 73 61 74 69 73 66  ex cannot satisf
2bb9b 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  y the ORDER BY c
2bb9c 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20 20  onstraint..     
2bb9d 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65     */.        re
2bb9e 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
2bb9f 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
2bba0 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64  ( pIdx->aSortOrd
2bba1 65 72 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  er!=0 );.    ass
2bba2 65 72 74 28 20 70 54 65 72 6d 2d 3e 73 6f 72 74  ert( pTerm->sort
2bba3 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 70 54 65 72  Order==0 || pTer
2bba4 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 31 20  m->sortOrder==1 
2bba5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
2bba6 53 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20  SortOrder==0 || 
2bba7 69 53 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b  iSortOrder==1 );
2bba8 0a 20 20 20 20 74 65 72 6d 53 6f 72 74 4f 72 64  .    termSortOrd
2bba9 65 72 20 3d 20 69 53 6f 72 74 4f 72 64 65 72 20  er = iSortOrder 
2bbaa 5e 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64  ^ pTerm->sortOrd
2bbab 65 72 3b 0a 20 20 20 20 69 66 28 20 69 3e 6e 45  er;.    if( i>nE
2bbac 71 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  qCol ){.      if
2bbad 28 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 21  ( termSortOrder!
2bbae 3d 73 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20  =sortOrder ){.  
2bbaf 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 65 73        /* Indices
2bbb0 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65   can only be use
2bbb1 64 20 69 66 20 61 6c 6c 20 4f 52 44 45 52 20 42  d if all ORDER B
2bbb2 59 20 74 65 72 6d 73 20 70 61 73 74 20 74 68 65  Y terms past the
2bbb3 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 71 75 61  .        ** equa
2bbb4 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2bbb5 20 61 72 65 20 61 6c 6c 20 65 69 74 68 65 72 20   are all either 
2bbb6 44 45 53 43 20 6f 72 20 41 53 43 2e 20 2a 2f 0a  DESC or ASC. */.
2bbb7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
2bbb8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2bbb9 6c 73 65 7b 0a 20 20 20 20 20 20 73 6f 72 74 4f  lse{.      sortO
2bbba 72 64 65 72 20 3d 20 74 65 72 6d 53 6f 72 74 4f  rder = termSortO
2bbbb 72 64 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rder;.    }.    
2bbbc 6a 2b 2b 3b 0a 20 20 20 20 70 54 65 72 6d 2b 2b  j++;.    pTerm++
2bbbd 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d  ;.    if( iColum
2bbbe 6e 3c 30 20 26 26 20 21 72 65 66 65 72 65 6e 63  n<0 && !referenc
2bbbf 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f  esOtherTables(pO
2bbc0 72 64 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74  rderBy, pMaskSet
2bbc1 2c 20 6a 2c 20 62 61 73 65 29 20 29 7b 0a 20 20  , j, base) ){.  
2bbc2 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 6e      /* If the in
2bbc3 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20  dexed column is 
2bbc4 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
2bbc5 61 6e 64 20 65 76 65 72 79 74 68 69 6e 67 20 6d  and everything m
2bbc6 61 74 63 68 65 73 0a 20 20 20 20 20 20 2a 2a 20  atches.      ** 
2bbc7 73 6f 20 66 61 72 20 61 6e 64 20 6e 6f 6e 65 20  so far and none 
2bbc8 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
2bbc9 74 65 72 6d 73 20 74 6f 20 74 68 65 20 72 69 67  terms to the rig
2bbca 68 74 20 72 65 66 65 72 65 6e 63 65 20 6f 74 68  ht reference oth
2bbcb 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  er.      ** tabl
2bbcc 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e 2c 20  es in the join, 
2bbcd 74 68 65 6e 20 77 65 20 61 72 65 20 61 73 73 75  then we are assu
2bbce 72 65 64 20 74 68 61 74 20 74 68 65 20 69 6e 64  red that the ind
2bbcf 65 78 20 63 61 6e 20 62 65 20 75 73 65 64 20 0a  ex can be used .
2bbd0 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 6f 72 74        ** to sort
2bbd1 20 62 65 63 61 75 73 65 20 74 68 65 20 70 72 69   because the pri
2bbd2 6d 61 72 79 20 6b 65 79 20 69 73 20 75 6e 69 71  mary key is uniq
2bbd3 75 65 20 61 6e 64 20 73 6f 20 6e 6f 6e 65 20 6f  ue and so none o
2bbd4 66 20 74 68 65 20 6f 74 68 65 72 0a 20 20 20 20  f the other.    
2bbd5 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 77 69 6c    ** columns wil
2bbd6 6c 20 6d 61 6b 65 20 61 6e 79 20 64 69 66 66 65  l make any diffe
2bbd7 72 65 6e 63 65 0a 20 20 20 20 20 20 2a 2f 0a 20  rence.      */. 
2bbd8 20 20 20 20 20 6a 20 3d 20 6e 54 65 72 6d 3b 0a       j = nTerm;.
2bbd9 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 62      }.  }..  *pb
2bbda 52 65 76 20 3d 20 73 6f 72 74 4f 72 64 65 72 21  Rev = sortOrder!
2bbdb 3d 30 3b 0a 20 20 69 66 28 20 6a 3e 3d 6e 54 65  =0;.  if( j>=nTe
2bbdc 72 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c  rm ){.    /* All
2bbdd 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52   terms of the OR
2bbde 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 72  DER BY clause ar
2bbdf 65 20 63 6f 76 65 72 65 64 20 62 79 20 74 68 69  e covered by thi
2bbe0 73 20 69 6e 64 65 78 20 73 6f 0a 20 20 20 20 2a  s index so.    *
2bbe1 2a 20 74 68 69 73 20 69 6e 64 65 78 20 63 61 6e  * this index can
2bbe2 20 62 65 20 75 73 65 64 20 66 6f 72 20 73 6f 72   be used for sor
2bbe3 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 65 74  ting. */.    ret
2bbe4 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 1;.  }.  if(
2bbe5 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d   pIdx->onError!=
2bbe6 4f 45 5f 4e 6f 6e 65 20 26 26 20 69 3d 3d 70 49  OE_None && i==pI
2bbe7 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 0a 20 20 20 20  dx->nColumn.    
2bbe8 20 20 26 26 20 21 72 65 66 65 72 65 6e 63 65 73    && !references
2bbe9 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72 64  OtherTables(pOrd
2bbea 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20  erBy, pMaskSet, 
2bbeb 6a 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20 20  j, base) ){.    
2bbec 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20  /* All terms of 
2bbed 74 68 69 73 20 69 6e 64 65 78 20 6d 61 74 63 68  this index match
2bbee 20 73 6f 6d 65 20 70 72 65 66 69 78 20 6f 66 20   some prefix of 
2bbef 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2bbf0 75 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74  use.    ** and t
2bbf1 68 65 20 69 6e 64 65 78 20 69 73 20 55 4e 49 51  he index is UNIQ
2bbf2 55 45 20 61 6e 64 20 6e 6f 20 74 65 72 6d 73 20  UE and no terms 
2bbf3 6f 6e 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74  on the tail of t
2bbf4 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20  he ORDER BY.    
2bbf5 2a 2a 20 63 6c 61 75 73 65 20 72 65 66 65 72 65  ** clause refere
2bbf6 6e 63 65 20 6f 74 68 65 72 20 74 61 62 6c 65 73  nce other tables
2bbf7 20 69 6e 20 61 20 6a 6f 69 6e 2e 20 20 49 66 20   in a join.  If 
2bbf8 74 68 69 73 20 69 73 20 61 6c 6c 20 74 72 75 65  this is all true
2bbf9 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65   then.    ** the
2bbfa 20 6f 72 64 65 72 20 62 79 20 63 6c 61 75 73 65   order by clause
2bbfb 20 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e   is superfluous.
2bbfc 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
2bbfd 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
2bbfe 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  ;.}../*.** Check
2bbff 20 74 61 62 6c 65 20 74 6f 20 73 65 65 20 69 66   table to see if
2bc00 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2bc01 61 75 73 65 20 69 6e 20 70 4f 72 64 65 72 42 79  ause in pOrderBy
2bc02 20 63 61 6e 20 62 65 20 73 61 74 69 73 66 69 65   can be satisfie
2bc03 64 0a 2a 2a 20 62 79 20 73 6f 72 74 69 6e 67 20  d.** by sorting 
2bc04 69 6e 20 6f 72 64 65 72 20 6f 66 20 52 4f 57 49  in order of ROWI
2bc05 44 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20  D.  Return true 
2bc06 69 66 20 73 6f 20 61 6e 64 20 73 65 74 20 2a 70  if so and set *p
2bc07 62 52 65 76 20 74 6f 20 62 65 0a 2a 2a 20 74 72  bRev to be.** tr
2bc08 75 65 20 66 6f 72 20 72 65 76 65 72 73 65 20 52  ue for reverse R
2bc09 4f 57 49 44 20 61 6e 64 20 66 61 6c 73 65 20 66  OWID and false f
2bc0a 6f 72 20 66 6f 72 77 61 72 64 20 52 4f 57 49 44  or forward ROWID
2bc0b 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69   order..*/.stati
2bc0c 63 20 69 6e 74 20 73 6f 72 74 61 62 6c 65 42 79  c int sortableBy
2bc0d 52 6f 77 69 64 28 0a 20 20 69 6e 74 20 62 61 73  Rowid(.  int bas
2bc0e 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
2bc0f 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
2bc10 72 20 66 6f 72 20 74 61 62 6c 65 20 74 6f 20 62  r for table to b
2bc11 65 20 73 6f 72 74 65 64 20 2a 2f 0a 20 20 45 78  e sorted */.  Ex
2bc12 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
2bc13 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44  ,     /* The ORD
2bc14 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2bc15 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a    WhereMaskSet *
2bc16 70 4d 61 73 6b 53 65 74 2c 20 2f 2a 20 4d 61 70  pMaskSet, /* Map
2bc17 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 20  ping from table 
2bc18 63 75 72 73 6f 72 73 20 74 6f 20 62 69 74 6d 61  cursors to bitma
2bc19 70 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52  ps */.  int *pbR
2bc1a 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ev              
2bc1b 2f 2a 20 53 65 74 20 74 6f 20 31 20 69 66 20 4f  /* Set to 1 if O
2bc1c 52 44 45 52 20 42 59 20 69 73 20 44 45 53 43 20  RDER BY is DESC 
2bc1d 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 3b  */.){.  Expr *p;
2bc1e 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 64  ..  assert( pOrd
2bc1f 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 61 73 73  erBy!=0 );.  ass
2bc20 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e  ert( pOrderBy->n
2bc21 45 78 70 72 3e 30 20 29 3b 0a 20 20 70 20 3d 20  Expr>0 );.  p = 
2bc22 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 70  pOrderBy->a[0].p
2bc23 45 78 70 72 3b 0a 20 20 69 66 28 20 70 2d 3e 6f  Expr;.  if( p->o
2bc24 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20  p==TK_COLUMN && 
2bc25 70 2d 3e 69 54 61 62 6c 65 3d 3d 62 61 73 65 20  p->iTable==base 
2bc26 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d  && p->iColumn==-
2bc27 31 0a 20 20 20 20 26 26 20 21 72 65 66 65 72 65  1.    && !refere
2bc28 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28  ncesOtherTables(
2bc29 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b 53  pOrderBy, pMaskS
2bc2a 65 74 2c 20 31 2c 20 62 61 73 65 29 20 29 7b 0a  et, 1, base) ){.
2bc2b 20 20 20 20 2a 70 62 52 65 76 20 3d 20 70 4f 72      *pbRev = pOr
2bc2c 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 73 6f 72 74  derBy->a[0].sort
2bc2d 4f 72 64 65 72 3b 0a 20 20 20 20 72 65 74 75 72  Order;.    retur
2bc2e 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
2bc2f 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  n 0;.}../*.** Pr
2bc30 65 70 61 72 65 20 61 20 63 72 75 64 65 20 65 73  epare a crude es
2bc31 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6c 6f  timate of the lo
2bc32 67 61 72 69 74 68 6d 20 6f 66 20 74 68 65 20 69  garithm of the i
2bc33 6e 70 75 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 54  nput value..** T
2bc34 68 65 20 72 65 73 75 6c 74 73 20 6e 65 65 64 20  he results need 
2bc35 6e 6f 74 20 62 65 20 65 78 61 63 74 2e 20 20 54  not be exact.  T
2bc36 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  his is only used
2bc37 20 66 6f 72 20 65 73 74 69 6d 61 74 69 6e 67 0a   for estimating.
2bc38 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 63 6f 73  ** the total cos
2bc39 74 20 6f 66 20 70 65 72 66 6f 72 6d 69 6e 67 20  t of performing 
2bc3a 6f 70 65 72 61 74 69 6f 6e 73 20 77 69 74 68 20  operations with 
2bc3b 4f 28 6c 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c 6f  O(logN) or O(Nlo
2bc3c 67 4e 29 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69 74  gN).** complexit
2bc3d 79 2e 20 20 42 65 63 61 75 73 65 20 4e 20 69 73  y.  Because N is
2bc3e 20 6a 75 73 74 20 61 20 67 75 65 73 73 2c 20 69   just a guess, i
2bc3f 74 20 69 73 20 6e 6f 20 67 72 65 61 74 20 74 72  t is no great tr
2bc40 61 67 65 64 79 20 69 66 0a 2a 2a 20 6c 6f 67 4e  agedy if.** logN
2bc41 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6f 66 66   is a little off
2bc42 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f 75 62  ..*/.static doub
2bc43 6c 65 20 65 73 74 4c 6f 67 28 64 6f 75 62 6c 65  le estLog(double
2bc44 20 4e 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6c 6f   N){.  double lo
2bc45 67 4e 20 3d 20 31 3b 0a 20 20 64 6f 75 62 6c 65  gN = 1;.  double
2bc46 20 78 20 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65   x = 10;.  while
2bc47 28 20 4e 3e 78 20 29 7b 0a 20 20 20 20 6c 6f 67  ( N>x ){.    log
2bc48 4e 20 2b 3d 20 31 3b 0a 20 20 20 20 78 20 2a 3d  N += 1;.    x *=
2bc49 20 31 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   10;.  }.  retur
2bc4a 6e 20 6c 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n logN;.}../*.**
2bc4b 20 54 77 6f 20 72 6f 75 74 69 6e 65 73 20 66 6f   Two routines fo
2bc4c 72 20 70 72 69 6e 74 69 6e 67 20 74 68 65 20 63  r printing the c
2bc4d 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 73 71 6c  ontent of an sql
2bc4e 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a  ite3_index_info.
2bc4f 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 55  ** structure.  U
2bc50 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20  sed for testing 
2bc51 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e  and debugging on
2bc52 6c 79 2e 20 20 49 66 20 6e 65 69 74 68 65 72 0a  ly.  If neither.
2bc53 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6f  ** SQLITE_TEST o
2bc54 72 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61  r SQLITE_DEBUG a
2bc55 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  re defined, then
2bc56 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a   these routines.
2bc57 2a 2a 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a  ** are no-ops..*
2bc58 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
2bc59 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2bc5a 41 4c 54 41 42 4c 45 29 20 26 26 20 64 65 66 69  ALTABLE) && defi
2bc5b 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
2bc5c 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 54 52  ).static void TR
2bc5d 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28 73  ACE_IDX_INPUTS(s
2bc5e 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2bc5f 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  o *p){.  int i;.
2bc60 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 57 68    if( !sqlite3Wh
2bc61 65 72 65 54 72 61 63 65 20 29 20 72 65 74 75 72  ereTrace ) retur
2bc62 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
2bc63 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20  p->nConstraint; 
2bc64 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
2bc65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20  3DebugPrintf("  
2bc66 63 6f 6e 73 74 72 61 69 6e 74 5b 25 64 5d 3a 20  constraint[%d]: 
2bc67 63 6f 6c 3d 25 64 20 74 65 72 6d 69 64 3d 25 64  col=%d termid=%d
2bc68 20 6f 70 3d 25 64 20 75 73 61 62 6c 65 64 3d 25   op=%d usabled=%
2bc69 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a  d\n",.       i,.
2bc6a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74         p->aConst
2bc6b 72 61 69 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e  raint[i].iColumn
2bc6c 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
2bc6d 73 74 72 61 69 6e 74 5b 69 5d 2e 69 54 65 72 6d  straint[i].iTerm
2bc6e 4f 66 66 73 65 74 2c 0a 20 20 20 20 20 20 20 70  Offset,.       p
2bc6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d  ->aConstraint[i]
2bc70 2e 6f 70 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61  .op,.       p->a
2bc71 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73  Constraint[i].us
2bc72 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72  able);.  }.  for
2bc73 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 72 64 65  (i=0; i<p->nOrde
2bc74 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  rBy; i++){.    s
2bc75 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2bc76 66 28 22 20 20 6f 72 64 65 72 62 79 5b 25 64 5d  f("  orderby[%d]
2bc77 3a 20 63 6f 6c 3d 25 64 20 64 65 73 63 3d 25 64  : col=%d desc=%d
2bc78 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20  \n",.       i,. 
2bc79 20 20 20 20 20 20 70 2d 3e 61 4f 72 64 65 72 42        p->aOrderB
2bc7a 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20  y[i].iColumn,.  
2bc7b 20 20 20 20 20 70 2d 3e 61 4f 72 64 65 72 42 79       p->aOrderBy
2bc7c 5b 69 5d 2e 64 65 73 63 29 3b 0a 20 20 7d 0a 7d  [i].desc);.  }.}
2bc7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 54 52 41  .static void TRA
2bc7e 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 73  CE_IDX_OUTPUTS(s
2bc7f 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2bc80 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  o *p){.  int i;.
2bc81 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 57 68    if( !sqlite3Wh
2bc82 65 72 65 54 72 61 63 65 20 29 20 72 65 74 75 72  ereTrace ) retur
2bc83 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
2bc84 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20  p->nConstraint; 
2bc85 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
2bc86 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20  3DebugPrintf("  
2bc87 75 73 61 67 65 5b 25 64 5d 3a 20 61 72 67 76 49  usage[%d]: argvI
2bc88 64 78 3d 25 64 20 6f 6d 69 74 3d 25 64 5c 6e 22  dx=%d omit=%d\n"
2bc89 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20  ,.       i,.    
2bc8a 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e     p->aConstrain
2bc8b 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e  tUsage[i].argvIn
2bc8c 64 65 78 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61  dex,.       p->a
2bc8d 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
2bc8e 69 5d 2e 6f 6d 69 74 29 3b 0a 20 20 7d 0a 20 20  i].omit);.  }.  
2bc8f 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2bc90 74 66 28 22 20 20 69 64 78 4e 75 6d 3d 25 64 5c  tf("  idxNum=%d\
2bc91 6e 22 2c 20 70 2d 3e 69 64 78 4e 75 6d 29 3b 0a  n", p->idxNum);.
2bc92 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2bc93 69 6e 74 66 28 22 20 20 69 64 78 53 74 72 3d 25  intf("  idxStr=%
2bc94 73 5c 6e 22 2c 20 70 2d 3e 69 64 78 53 74 72 29  s\n", p->idxStr)
2bc95 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67  ;.  sqlite3Debug
2bc96 50 72 69 6e 74 66 28 22 20 20 6f 72 64 65 72 42  Printf("  orderB
2bc97 79 43 6f 6e 73 75 6d 65 64 3d 25 64 5c 6e 22 2c  yConsumed=%d\n",
2bc98 20 70 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75   p->orderByConsu
2bc99 6d 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  med);.  sqlite3D
2bc9a 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 65 73  ebugPrintf("  es
2bc9b 74 69 6d 61 74 65 64 43 6f 73 74 3d 25 67 5c 6e  timatedCost=%g\n
2bc9c 22 2c 20 70 2d 3e 65 73 74 69 6d 61 74 65 64 43  ", p->estimatedC
2bc9d 6f 73 74 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64  ost);.}.#else.#d
2bc9e 65 66 69 6e 65 20 54 52 41 43 45 5f 49 44 58 5f  efine TRACE_IDX_
2bc9f 49 4e 50 55 54 53 28 41 29 0a 23 64 65 66 69 6e  INPUTS(A).#defin
2bca0 65 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54 50  e TRACE_IDX_OUTP
2bca1 55 54 53 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f  UTS(A).#endif../
2bca2 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 64 20 62  * .** Required b
2bca3 65 63 61 75 73 65 20 62 65 73 74 49 6e 64 65 78  ecause bestIndex
2bca4 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  () is called by 
2bca5 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65  bestOrClauseInde
2bca6 78 28 29 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76  x() .*/.static v
2bca7 6f 69 64 20 62 65 73 74 49 6e 64 65 78 28 0a 20  oid bestIndex(. 
2bca8 20 20 20 50 61 72 73 65 2a 2c 20 57 68 65 72 65     Parse*, Where
2bca9 43 6c 61 75 73 65 2a 2c 20 73 74 72 75 63 74 20  Clause*, struct 
2bcaa 53 72 63 4c 69 73 74 5f 69 74 65 6d 2a 2c 20 42  SrcList_item*, B
2bcab 69 74 6d 61 73 6b 2c 20 45 78 70 72 4c 69 73 74  itmask, ExprList
2bcac 2a 2c 20 57 68 65 72 65 43 6f 73 74 2a 29 3b 0a  *, WhereCost*);.
2bcad 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2bcae 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
2bcaf 66 69 6e 64 20 61 6e 20 73 63 61 6e 6e 69 6e 67  find an scanning
2bcb0 20 73 74 72 61 74 65 67 79 20 74 68 61 74 20 63   strategy that c
2bcb1 61 6e 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 74  an be used .** t
2bcb2 6f 20 6f 70 74 69 6d 69 7a 65 20 61 6e 20 27 4f  o optimize an 'O
2bcb3 52 27 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  R' expression th
2bcb4 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 20  at is part of a 
2bcb5 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 0a 2a  WHERE clause. .*
2bcb6 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61  *.** The table a
2bcb7 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 46  ssociated with F
2bcb8 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20  ROM clause term 
2bcb9 70 53 72 63 20 6d 61 79 20 62 65 20 65 69 74 68  pSrc may be eith
2bcba 65 72 20 61 0a 2a 2a 20 72 65 67 75 6c 61 72 20  er a.** regular 
2bcbb 42 2d 54 72 65 65 20 74 61 62 6c 65 20 6f 72 20  B-Tree table or 
2bcbc 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
2bcbd 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2bcbe 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65  bestOrClauseInde
2bcbf 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  x(.  Parse *pPar
2bcc0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
2bcc1 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2bcc2 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65  context */.  Whe
2bcc3 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
2bcc4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2bcc5 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2bcc6 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2bcc7 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f 2a  _item *pSrc,  /*
2bcc8 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
2bcc9 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20   term to search 
2bcca 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
2bccb 52 65 61 64 79 2c 20 20 20 20 20 20 20 20 20 20  Ready,          
2bccc 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73   /* Mask of curs
2bccd 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74  ors that are not
2bcce 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20   available */.  
2bccf 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2bcd0 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  By,         /* T
2bcd1 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2bcd2 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73  se */.  WhereCos
2bcd3 74 20 2a 70 43 6f 73 74 20 20 20 20 20 20 20 20  t *pCost        
2bcd4 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f      /* Lowest co
2bcd5 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f  st query plan */
2bcd6 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .){.#ifndef SQLI
2bcd7 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
2bcd8 49 5a 41 54 49 4f 4e 0a 20 20 63 6f 6e 73 74 20  IZATION.  const 
2bcd9 69 6e 74 20 69 43 75 72 20 3d 20 70 53 72 63 2d  int iCur = pSrc-
2bcda 3e 69 43 75 72 73 6f 72 3b 20 20 20 2f 2a 20 54  >iCursor;   /* T
2bcdb 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68 65  he cursor of the
2bcdc 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63   table to be acc
2bcdd 65 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  essed */.  const
2bcde 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 53 72 63   Bitmask maskSrc
2bcdf 20 3d 20 67 65 74 4d 61 73 6b 28 70 57 43 2d 3e   = getMask(pWC->
2bce0 70 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29 3b  pMaskSet, iCur);
2bce1 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 66 6f 72    /* Bitmask for
2bce2 20 70 53 72 63 20 2a 2f 0a 20 20 57 68 65 72 65   pSrc */.  Where
2bce3 54 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70 57 43  Term * const pWC
2bce4 45 6e 64 20 3d 20 26 70 57 43 2d 3e 61 5b 70 57  End = &pWC->a[pW
2bce5 43 2d 3e 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20  C->nTerm];      
2bce6 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 57 43 2d    /* End of pWC-
2bce7 3e 61 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 65 54  >a[] */.  WhereT
2bce8 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20  erm *pTerm;     
2bce9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2bcea 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66 20   single term of 
2bceb 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2bcec 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68   */..  /* Search
2bced 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2bcee 65 20 74 65 72 6d 73 20 66 6f 72 20 61 20 75 73  e terms for a us
2bcef 61 62 6c 65 20 57 4f 5f 4f 52 20 74 65 72 6d 2e  able WO_OR term.
2bcf0 20 2a 2f 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d   */.  for(pTerm=
2bcf1 70 57 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70 57  pWC->a; pTerm<pW
2bcf2 43 45 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b 0a  CEnd; pTerm++){.
2bcf3 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65      if( pTerm->e
2bcf4 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20  Operator==WO_OR 
2bcf5 0a 20 20 20 20 20 26 26 20 28 28 70 54 65 72 6d  .     && ((pTerm
2bcf6 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 7e 6d  ->prereqAll & ~m
2bcf7 61 73 6b 53 72 63 29 20 26 20 6e 6f 74 52 65 61  askSrc) & notRea
2bcf8 64 79 29 3d 3d 30 0a 20 20 20 20 20 26 26 20 28  dy)==0.     && (
2bcf9 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f  pTerm->u.pOrInfo
2bcfa 2d 3e 69 6e 64 65 78 61 62 6c 65 20 26 20 6d 61  ->indexable & ma
2bcfb 73 6b 53 72 63 29 21 3d 30 20 0a 20 20 20 20 29  skSrc)!=0 .    )
2bcfc 7b 0a 20 20 20 20 20 20 57 68 65 72 65 43 6c 61  {.      WhereCla
2bcfd 75 73 65 20 2a 20 63 6f 6e 73 74 20 70 4f 72 57  use * const pOrW
2bcfe 43 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e 70 4f  C = &pTerm->u.pO
2bcff 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20  rInfo->wc;.     
2bd00 20 57 68 65 72 65 54 65 72 6d 20 2a 20 63 6f 6e   WhereTerm * con
2bd01 73 74 20 70 4f 72 57 43 45 6e 64 20 3d 20 26 70  st pOrWCEnd = &p
2bd02 4f 72 57 43 2d 3e 61 5b 70 4f 72 57 43 2d 3e 6e  OrWC->a[pOrWC->n
2bd03 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 57 68 65  Term];.      Whe
2bd04 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b  reTerm *pOrTerm;
2bd05 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73  .      int flags
2bd06 20 3d 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f   = WHERE_MULTI_O
2bd07 52 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  R;.      double 
2bd08 72 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20 20  rTotal = 0;.    
2bd09 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 20 3d 20    double nRow = 
2bd0a 30 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 70 4f  0;..      for(pO
2bd0b 72 54 65 72 6d 3d 70 4f 72 57 43 2d 3e 61 3b 20  rTerm=pOrWC->a; 
2bd0c 70 4f 72 54 65 72 6d 3c 70 4f 72 57 43 45 6e 64  pOrTerm<pOrWCEnd
2bd0d 3b 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20  ; pOrTerm++){.  
2bd0e 20 20 20 20 20 20 57 68 65 72 65 43 6f 73 74 20        WhereCost 
2bd0f 73 54 65 72 6d 43 6f 73 74 3b 0a 20 20 20 20 20  sTermCost;.     
2bd10 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
2bd11 2e 2e 2e 20 4d 75 6c 74 69 2d 69 6e 64 65 78 20  ... Multi-index 
2bd12 4f 52 20 74 65 73 74 69 6e 67 20 66 6f 72 20 74  OR testing for t
2bd13 65 72 6d 20 25 64 20 6f 66 20 25 64 2e 2e 2e 2e  erm %d of %d....
2bd14 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20  \n", .          
2bd15 28 70 4f 72 54 65 72 6d 20 2d 20 70 4f 72 57 43  (pOrTerm - pOrWC
2bd16 2d 3e 61 29 2c 20 28 70 54 65 72 6d 20 2d 20 70  ->a), (pTerm - p
2bd17 57 43 2d 3e 61 29 0a 20 20 20 20 20 20 20 20 29  WC->a).        )
2bd18 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
2bd19 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2bd1a 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20 20 20  r==WO_AND ){.   
2bd1b 20 20 20 20 20 20 20 57 68 65 72 65 43 6c 61 75         WhereClau
2bd1c 73 65 20 2a 70 41 6e 64 57 43 20 3d 20 26 70 4f  se *pAndWC = &pO
2bd1d 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66  rTerm->u.pAndInf
2bd1e 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 20 20 20  o->wc;.         
2bd1f 20 62 65 73 74 49 6e 64 65 78 28 70 50 61 72 73   bestIndex(pPars
2bd20 65 2c 20 70 41 6e 64 57 43 2c 20 70 53 72 63 2c  e, pAndWC, pSrc,
2bd21 20 6e 6f 74 52 65 61 64 79 2c 20 30 2c 20 26 73   notReady, 0, &s
2bd22 54 65 72 6d 43 6f 73 74 29 3b 0a 20 20 20 20 20  TermCost);.     
2bd23 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72     }else if( pOr
2bd24 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
2bd25 3d 3d 69 43 75 72 20 29 7b 0a 20 20 20 20 20 20  ==iCur ){.      
2bd26 20 20 20 20 57 68 65 72 65 43 6c 61 75 73 65 20      WhereClause 
2bd27 74 65 6d 70 57 43 3b 0a 20 20 20 20 20 20 20 20  tempWC;.        
2bd28 20 20 74 65 6d 70 57 43 2e 70 50 61 72 73 65 20    tempWC.pParse 
2bd29 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 0a 20  = pWC->pParse;. 
2bd2a 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e           tempWC.
2bd2b 70 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e  pMaskSet = pWC->
2bd2c 70 4d 61 73 6b 53 65 74 3b 0a 20 20 20 20 20 20  pMaskSet;.      
2bd2d 20 20 20 20 74 65 6d 70 57 43 2e 6f 70 20 3d 20      tempWC.op = 
2bd2e 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20 20  TK_AND;.        
2bd2f 20 20 74 65 6d 70 57 43 2e 61 20 3d 20 70 4f 72    tempWC.a = pOr
2bd30 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  Term;.          
2bd31 74 65 6d 70 57 43 2e 6e 54 65 72 6d 20 3d 20 31  tempWC.nTerm = 1
2bd32 3b 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74  ;.          best
2bd33 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 26 74  Index(pParse, &t
2bd34 65 6d 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74  empWC, pSrc, not
2bd35 52 65 61 64 79 2c 20 30 2c 20 26 73 54 65 72 6d  Ready, 0, &sTerm
2bd36 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  Cost);.        }
2bd37 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2bd38 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2bd39 20 20 7d 0a 20 20 20 20 20 20 20 20 72 54 6f 74    }.        rTot
2bd3a 61 6c 20 2b 3d 20 73 54 65 72 6d 43 6f 73 74 2e  al += sTermCost.
2bd3b 72 43 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 6e  rCost;.        n
2bd3c 52 6f 77 20 2b 3d 20 73 54 65 72 6d 43 6f 73 74  Row += sTermCost
2bd3d 2e 6e 52 6f 77 3b 0a 20 20 20 20 20 20 20 20 69  .nRow;.        i
2bd3e 66 28 20 72 54 6f 74 61 6c 3e 3d 70 43 6f 73 74  f( rTotal>=pCost
2bd3f 2d 3e 72 43 6f 73 74 20 29 20 62 72 65 61 6b 3b  ->rCost ) break;
2bd40 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
2bd41 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
2bd42 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
2bd43 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  e, increase the 
2bd44 73 63 61 6e 20 63 6f 73 74 20 74 6f 20 61 63 63  scan cost to acc
2bd45 6f 75 6e 74 20 0a 20 20 20 20 20 20 2a 2a 20 66  ount .      ** f
2bd46 6f 72 20 74 68 65 20 63 6f 73 74 20 6f 66 20 74  or the cost of t
2bd47 68 65 20 73 6f 72 74 2e 20 2a 2f 0a 20 20 20 20  he sort. */.    
2bd48 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 21 3d    if( pOrderBy!=
2bd49 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 54 6f  0 ){.        rTo
2bd4a 74 61 6c 20 2b 3d 20 6e 52 6f 77 2a 65 73 74 4c  tal += nRow*estL
2bd4b 6f 67 28 6e 52 6f 77 29 3b 0a 20 20 20 20 20 20  og(nRow);.      
2bd4c 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2e    WHERETRACE((".
2bd4d 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63 72 65  .. sorting incre
2bd4e 61 73 65 73 20 4f 52 20 63 6f 73 74 20 74 6f 20  ases OR cost to 
2bd4f 25 2e 39 67 5c 6e 22 2c 20 72 54 6f 74 61 6c 29  %.9g\n", rTotal)
2bd50 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
2bd51 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 73 74    /* If the cost
2bd52 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 75 73 69   of scanning usi
2bd53 6e 67 20 74 68 69 73 20 4f 52 20 74 65 72 6d 20  ng this OR term 
2bd54 66 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  for optimization
2bd55 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 6c 65 73   is.      ** les
2bd56 73 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65  s than the curre
2bd57 6e 74 20 63 6f 73 74 20 73 74 6f 72 65 64 20 69  nt cost stored i
2bd58 6e 20 70 43 6f 73 74 2c 20 72 65 70 6c 61 63 65  n pCost, replace
2bd59 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20   the contents.  
2bd5a 20 20 20 20 2a 2a 20 6f 66 20 70 43 6f 73 74 2e      ** of pCost.
2bd5b 20 2a 2f 0a 20 20 20 20 20 20 57 48 45 52 45 54   */.      WHERET
2bd5c 52 41 43 45 28 28 22 2e 2e 2e 20 6d 75 6c 74 69  RACE(("... multi
2bd5d 2d 69 6e 64 65 78 20 4f 52 20 63 6f 73 74 3d 25  -index OR cost=%
2bd5e 2e 39 67 20 6e 72 6f 77 3d 25 2e 39 67 5c 6e 22  .9g nrow=%.9g\n"
2bd5f 2c 20 72 54 6f 74 61 6c 2c 20 6e 52 6f 77 29 29  , rTotal, nRow))
2bd60 3b 0a 20 20 20 20 20 20 69 66 28 20 72 54 6f 74  ;.      if( rTot
2bd61 61 6c 3c 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20  al<pCost->rCost 
2bd62 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74  ){.        pCost
2bd63 2d 3e 72 43 6f 73 74 20 3d 20 72 54 6f 74 61 6c  ->rCost = rTotal
2bd64 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d  ;.        pCost-
2bd65 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20  >nRow = nRow;.  
2bd66 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61        pCost->pla
2bd67 6e 2e 77 73 46 6c 61 67 73 20 3d 20 66 6c 61 67  n.wsFlags = flag
2bd68 73 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74  s;.        pCost
2bd69 2d 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d 20 3d  ->plan.u.pTerm =
2bd6a 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a   pTerm;.      }.
2bd6b 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
2bd6c 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2bd6d 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  OR_OPTIMIZATION 
2bd6e 2a 2f 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  */.}..#ifndef SQ
2bd6f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2bd70 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 41 6c 6c  LTABLE./*.** All
2bd71 6f 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c 61  ocate and popula
2bd72 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 69 6e  te an sqlite3_in
2bd73 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2bd74 72 65 2e 20 49 74 20 69 73 20 74 68 65 20 0a 2a  re. It is the .*
2bd75 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  * responsibility
2bd76 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74   of the caller t
2bd77 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 72 65 6c  o eventually rel
2bd78 65 61 73 65 20 74 68 65 20 73 74 72 75 63 74 75  ease the structu
2bd79 72 65 0a 2a 2a 20 62 79 20 70 61 73 73 69 6e 67  re.** by passing
2bd7a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74   the pointer ret
2bd7b 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 66 75  urned by this fu
2bd7c 6e 63 74 69 6f 6e 20 74 6f 20 73 71 6c 69 74 65  nction to sqlite
2bd7d 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61  3_free()..*/.sta
2bd7e 74 69 63 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  tic sqlite3_inde
2bd7f 78 5f 69 6e 66 6f 20 2a 61 6c 6c 6f 63 61 74 65  x_info *allocate
2bd80 49 6e 64 65 78 49 6e 66 6f 28 0a 20 20 50 61 72  IndexInfo(.  Par
2bd81 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 57  se *pParse, .  W
2bd82 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
2bd83 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2bd84 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 0a 20 20  t_item *pSrc,.  
2bd85 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2bd86 42 79 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  By.){.  int i, j
2bd87 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20  ;.  int nTerm;. 
2bd88 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2bd89 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2bd8a 20 2a 70 49 64 78 43 6f 6e 73 3b 0a 20 20 73 74   *pIdxCons;.  st
2bd8b 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2bd8c 65 78 5f 6f 72 64 65 72 62 79 20 2a 70 49 64 78  ex_orderby *pIdx
2bd8d 4f 72 64 65 72 42 79 3b 0a 20 20 73 74 72 75 63  OrderBy;.  struc
2bd8e 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2bd8f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
2bd90 20 2a 70 55 73 61 67 65 3b 0a 20 20 57 68 65 72   *pUsage;.  Wher
2bd91 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20  eTerm *pTerm;.  
2bd92 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20  int nOrderBy;.  
2bd93 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2bd94 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 0a 0a 20  fo *pIdxInfo;.. 
2bd95 20 57 48 45 52 45 54 52 41 43 45 28 28 22 52 65   WHERETRACE(("Re
2bd96 63 6f 6d 70 75 74 69 6e 67 20 69 6e 64 65 78 20  computing index 
2bd97 69 6e 66 6f 20 66 6f 72 20 25 73 2e 2e 2e 5c 6e  info for %s...\n
2bd98 22 2c 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e 7a  ", pSrc->pTab->z
2bd99 4e 61 6d 65 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f  Name));..  /* Co
2bd9a 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  unt the number o
2bd9b 66 20 70 6f 73 73 69 62 6c 65 20 57 48 45 52 45  f possible WHERE
2bd9c 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69   clause constrai
2bd9d 6e 74 73 20 72 65 66 65 72 72 69 6e 67 0a 20 20  nts referring.  
2bd9e 2a 2a 20 74 6f 20 74 68 69 73 20 76 69 72 74 75  ** to this virtu
2bd9f 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66 6f  al table */.  fo
2bda0 72 28 69 3d 6e 54 65 72 6d 3d 30 2c 20 70 54 65  r(i=nTerm=0, pTe
2bda1 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43  rm=pWC->a; i<pWC
2bda2 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54  ->nTerm; i++, pT
2bda3 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  erm++){.    if( 
2bda4 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
2bda5 72 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73  r != pSrc->iCurs
2bda6 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  or ) continue;. 
2bda7 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72     assert( (pTer
2bda8 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28 70 54  m->eOperator&(pT
2bda9 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31  erm->eOperator-1
2bdaa 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73  ))==0 );.    tes
2bdab 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
2bdac 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29  perator==WO_IN )
2bdad 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2bdae 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2bdaf 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  ==WO_ISNULL );. 
2bdb0 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f     if( pTerm->eO
2bdb1 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e  perator & (WO_IN
2bdb2 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f  |WO_ISNULL) ) co
2bdb3 6e 74 69 6e 75 65 3b 0a 20 20 20 20 6e 54 65 72  ntinue;.    nTer
2bdb4 6d 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  m++;.  }..  /* I
2bdb5 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
2bdb6 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 6f  lause contains o
2bdb7 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74  nly columns in t
2bdb8 68 65 20 63 75 72 72 65 6e 74 20 0a 20 20 2a 2a  he current .  **
2bdb9 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74   virtual table t
2bdba 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 73 70 61  hen allocate spa
2bdbb 63 65 20 66 6f 72 20 74 68 65 20 61 4f 72 64 65  ce for the aOrde
2bdbc 72 42 79 20 70 61 72 74 20 6f 66 0a 20 20 2a 2a  rBy part of.  **
2bdbd 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2bdbe 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2bdbf 65 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 72 64 65 72  e..  */.  nOrder
2bdc0 42 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4f  By = 0;.  if( pO
2bdc1 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 66 6f  rderBy ){.    fo
2bdc2 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42  r(i=0; i<pOrderB
2bdc3 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  y->nExpr; i++){.
2bdc4 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70        Expr *pExp
2bdc5 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  r = pOrderBy->a[
2bdc6 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
2bdc7 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
2bdc8 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70  K_COLUMN || pExp
2bdc9 72 2d 3e 69 54 61 62 6c 65 21 3d 70 53 72 63 2d  r->iTable!=pSrc-
2bdca 3e 69 43 75 72 73 6f 72 20 29 20 62 72 65 61 6b  >iCursor ) break
2bdcb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2bdcc 69 3d 3d 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78  i==pOrderBy->nEx
2bdcd 70 72 20 29 7b 0a 20 20 20 20 20 20 6e 4f 72 64  pr ){.      nOrd
2bdce 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d  erBy = pOrderBy-
2bdcf 3e 6e 45 78 70 72 3b 0a 20 20 20 20 7d 0a 20 20  >nExpr;.    }.  
2bdd0 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
2bdd1 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2bdd2 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2bdd3 65 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66  e.  */.  pIdxInf
2bdd4 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
2bdd5 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e  locZero(pParse->
2bdd6 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 64 78  db, sizeof(*pIdx
2bdd7 49 6e 66 6f 29 0a 20 20 20 20 20 20 20 20 20 20  Info).          
2bdd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdd9 20 2b 20 28 73 69 7a 65 6f 66 28 2a 70 49 64 78   + (sizeof(*pIdx
2bdda 43 6f 6e 73 29 20 2b 20 73 69 7a 65 6f 66 28 2a  Cons) + sizeof(*
2bddb 70 55 73 61 67 65 29 29 2a 6e 54 65 72 6d 0a 20  pUsage))*nTerm. 
2bddc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bddd 20 20 20 20 20 20 20 20 20 20 2b 20 73 69 7a 65            + size
2bdde 6f 66 28 2a 70 49 64 78 4f 72 64 65 72 42 79 29  of(*pIdxOrderBy)
2bddf 2a 6e 4f 72 64 65 72 42 79 20 29 3b 0a 20 20 69  *nOrderBy );.  i
2bde0 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29  f( pIdxInfo==0 )
2bde1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2bde2 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f  orMsg(pParse, "o
2bde3 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a  ut of memory");.
2bde4 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30      /* (double)0
2bde5 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
2bde6 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
2bde7 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20  _POINT... */.   
2bde8 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
2bde9 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
2bdea 74 68 65 20 73 74 72 75 63 74 75 72 65 2e 20 20  the structure.  
2bdeb 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  The sqlite3_inde
2bdec 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2bded 20 63 6f 6e 74 61 69 6e 73 0a 20 20 2a 2a 20 6d   contains.  ** m
2bdee 61 6e 79 20 66 69 65 6c 64 73 20 74 68 61 74 20  any fields that 
2bdef 61 72 65 20 64 65 63 6c 61 72 65 64 20 22 63 6f  are declared "co
2bdf0 6e 73 74 22 20 74 6f 20 70 72 65 76 65 6e 74 20  nst" to prevent 
2bdf1 78 42 65 73 74 49 6e 64 65 78 20 66 72 6f 6d 0a  xBestIndex from.
2bdf2 20 20 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 68    ** changing th
2bdf3 65 6d 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  em.  We have to 
2bdf4 64 6f 20 73 6f 6d 65 20 66 75 6e 6b 79 20 63 61  do some funky ca
2bdf5 73 74 69 6e 67 20 69 6e 20 6f 72 64 65 72 20 74  sting in order t
2bdf6 6f 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  o.  ** initializ
2bdf7 65 20 74 68 6f 73 65 20 66 69 65 6c 64 73 2e 0a  e those fields..
2bdf8 20 20 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20    */.  pIdxCons 
2bdf9 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  = (struct sqlite
2bdfa 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2bdfb 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 5b 31 5d  nt*)&pIdxInfo[1]
2bdfc 3b 0a 20 20 70 49 64 78 4f 72 64 65 72 42 79 20  ;.  pIdxOrderBy 
2bdfd 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  = (struct sqlite
2bdfe 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a  3_index_orderby*
2bdff 29 26 70 49 64 78 43 6f 6e 73 5b 6e 54 65 72 6d  )&pIdxCons[nTerm
2be00 5d 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 28 73  ];.  pUsage = (s
2be01 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2be02 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75  dex_constraint_u
2be03 73 61 67 65 2a 29 26 70 49 64 78 4f 72 64 65 72  sage*)&pIdxOrder
2be04 42 79 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20  By[nOrderBy];.  
2be05 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f  *(int*)&pIdxInfo
2be06 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  ->nConstraint = 
2be07 6e 54 65 72 6d 3b 0a 20 20 2a 28 69 6e 74 2a 29  nTerm;.  *(int*)
2be08 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65  &pIdxInfo->nOrde
2be09 72 42 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a  rBy = nOrderBy;.
2be0a 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74    *(struct sqlit
2be0b 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2be0c 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d  int**)&pIdxInfo-
2be0d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70  >aConstraint = p
2be0e 49 64 78 43 6f 6e 73 3b 0a 20 20 2a 28 73 74 72  IdxCons;.  *(str
2be0f 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2be10 78 5f 6f 72 64 65 72 62 79 2a 2a 29 26 70 49 64  x_orderby**)&pId
2be11 78 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79 20  xInfo->aOrderBy 
2be12 3d 20 70 49 64 78 4f 72 64 65 72 42 79 3b 0a 20  = pIdxOrderBy;. 
2be13 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65   *(struct sqlite
2be14 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2be15 6e 74 5f 75 73 61 67 65 2a 2a 29 26 70 49 64 78  nt_usage**)&pIdx
2be16 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
2be17 74 55 73 61 67 65 20 3d 0a 20 20 20 20 20 20 20  tUsage =.       
2be18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be1b 20 20 20 20 20 20 20 20 20 20 20 20 70 55 73 61              pUsa
2be1c 67 65 3b 0a 0a 20 20 66 6f 72 28 69 3d 6a 3d 30  ge;..  for(i=j=0
2be1d 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20  , pTerm=pWC->a; 
2be1e 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b  i<pWC->nTerm; i+
2be1f 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  +, pTerm++){.   
2be20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
2be21 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e  Cursor != pSrc->
2be22 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e  iCursor ) contin
2be23 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ue;.    assert( 
2be24 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  (pTerm->eOperato
2be25 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  r&(pTerm->eOpera
2be26 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  tor-1))==0 );.  
2be27 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
2be28 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
2be29 5f 49 4e 20 29 3b 0a 20 20 20 20 74 65 73 74 63  _IN );.    testc
2be2a 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
2be2b 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c  rator==WO_ISNULL
2be2c 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72   );.    if( pTer
2be2d 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
2be2e 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29  WO_IN|WO_ISNULL)
2be2f 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2be30 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 43 6f   pIdxCons[j].iCo
2be31 6c 75 6d 6e 20 3d 20 70 54 65 72 6d 2d 3e 75 2e  lumn = pTerm->u.
2be32 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  leftColumn;.    
2be33 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 54 65 72  pIdxCons[j].iTer
2be34 6d 4f 66 66 73 65 74 20 3d 20 69 3b 0a 20 20 20  mOffset = i;.   
2be35 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70 20   pIdxCons[j].op 
2be36 3d 20 28 75 38 29 70 54 65 72 6d 2d 3e 65 4f 70  = (u8)pTerm->eOp
2be37 65 72 61 74 6f 72 3b 0a 20 20 20 20 2f 2a 20 54  erator;.    /* T
2be38 68 65 20 64 69 72 65 63 74 20 61 73 73 69 67 6e  he direct assign
2be39 6d 65 6e 74 20 69 6e 20 74 68 65 20 70 72 65 76  ment in the prev
2be3a 69 6f 75 73 20 6c 69 6e 65 20 69 73 20 70 6f 73  ious line is pos
2be3b 73 69 62 6c 65 20 6f 6e 6c 79 20 62 65 63 61 75  sible only becau
2be3c 73 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 57 4f  se.    ** the WO
2be3d 5f 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4e 44  _ and SQLITE_IND
2be3e 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 20 63  EX_CONSTRAINT_ c
2be3f 6f 64 65 73 20 61 72 65 20 69 64 65 6e 74 69 63  odes are identic
2be40 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 2a 2a 20  al.  The.    ** 
2be41 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
2be42 73 20 76 65 72 69 66 79 20 74 68 69 73 20 66 61  s verify this fa
2be43 63 74 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ct. */.    asser
2be44 74 28 20 57 4f 5f 45 51 3d 3d 53 51 4c 49 54 45  t( WO_EQ==SQLITE
2be45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
2be46 54 5f 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65  T_EQ );.    asse
2be47 72 74 28 20 57 4f 5f 4c 54 3d 3d 53 51 4c 49 54  rt( WO_LT==SQLIT
2be48 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2be49 4e 54 5f 4c 54 20 29 3b 0a 20 20 20 20 61 73 73  NT_LT );.    ass
2be4a 65 72 74 28 20 57 4f 5f 4c 45 3d 3d 53 51 4c 49  ert( WO_LE==SQLI
2be4b 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
2be4c 49 4e 54 5f 4c 45 20 29 3b 0a 20 20 20 20 61 73  INT_LE );.    as
2be4d 73 65 72 74 28 20 57 4f 5f 47 54 3d 3d 53 51 4c  sert( WO_GT==SQL
2be4e 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
2be4f 41 49 4e 54 5f 47 54 20 29 3b 0a 20 20 20 20 61  AINT_GT );.    a
2be50 73 73 65 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51  ssert( WO_GE==SQ
2be51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
2be52 52 41 49 4e 54 5f 47 45 20 29 3b 0a 20 20 20 20  RAINT_GE );.    
2be53 61 73 73 65 72 74 28 20 57 4f 5f 4d 41 54 43 48  assert( WO_MATCH
2be54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
2be55 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20  ONSTRAINT_MATCH 
2be56 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2be57 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2be58 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c 57  & (WO_EQ|WO_LT|W
2be59 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45  O_LE|WO_GT|WO_GE
2be5a 7c 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a 20 20  |WO_MATCH) );.  
2be5b 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 66 6f 72    j++;.  }.  for
2be5c 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79  (i=0; i<nOrderBy
2be5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  ; i++){.    Expr
2be5e 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65 72   *pExpr = pOrder
2be5f 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  By->a[i].pExpr;.
2be60 20 20 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b      pIdxOrderBy[
2be61 69 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78  i].iColumn = pEx
2be62 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
2be63 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e   pIdxOrderBy[i].
2be64 64 65 73 63 20 3d 20 70 4f 72 64 65 72 42 79 2d  desc = pOrderBy-
2be65 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b  >a[i].sortOrder;
2be66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
2be67 49 64 78 49 6e 66 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a  IdxInfo;.}../*.*
2be68 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 62 6a 65  * The table obje
2be69 63 74 20 72 65 66 65 72 65 6e 63 65 20 70 61 73  ct reference pas
2be6a 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
2be6b 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
2be6c 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d  is function.** m
2be6d 75 73 74 20 72 65 70 72 65 73 65 6e 74 20 61 20  ust represent a 
2be6e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 54  virtual table. T
2be6f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76  his function inv
2be70 6f 6b 65 73 20 74 68 65 20 78 42 65 73 74 49 6e  okes the xBestIn
2be71 64 65 78 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  dex().** method 
2be72 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
2be73 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 71  able with the sq
2be74 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2be75 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 0a   pointer passed.
2be76 2a 2a 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  ** as the argume
2be77 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  nt..**.** If an 
2be78 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 70 50  error occurs, pP
2be79 61 72 73 65 20 69 73 20 70 6f 70 75 6c 61 74 65  arse is populate
2be7a 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  d with an error 
2be7b 6d 65 73 73 61 67 65 20 61 6e 64 20 61 0a 2a 2a  message and a.**
2be7c 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20   non-zero value 
2be7d 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  is returned. Oth
2be7e 65 72 77 69 73 65 2c 20 30 20 69 73 20 72 65 74  erwise, 0 is ret
2be7f 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 6f 75  urned and the ou
2be80 74 70 75 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20  tput.** part of 
2be81 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
2be82 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2be83 20 69 73 20 6c 65 66 74 20 70 6f 70 75 6c 61 74   is left populat
2be84 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65  ed..**.** Whethe
2be85 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f  r or not an erro
2be86 72 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69  r is returned, i
2be87 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73  t is the respons
2be88 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 0a 2a  ibility of the.*
2be89 2a 20 63 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e  * caller to even
2be8a 74 75 61 6c 6c 79 20 66 72 65 65 20 70 2d 3e 69  tually free p->i
2be8b 64 78 53 74 72 20 69 66 20 70 2d 3e 6e 65 65 64  dxStr if p->need
2be8c 54 6f 46 72 65 65 49 64 78 53 74 72 20 69 6e 64  ToFreeIdxStr ind
2be8d 69 63 61 74 65 73 0a 2a 2a 20 74 68 61 74 20 74  icates.** that t
2be8e 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64 2e  his is required.
2be8f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
2be90 74 61 62 42 65 73 74 49 6e 64 65 78 28 50 61 72  tabBestIndex(Par
2be91 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
2be92 65 20 2a 70 54 61 62 2c 20 73 71 6c 69 74 65 33  e *pTab, sqlite3
2be93 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b  _index_info *p){
2be94 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
2be95 2a 70 56 74 61 62 20 3d 20 70 54 61 62 2d 3e 70  *pVtab = pTab->p
2be96 56 74 61 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Vtab;.  int i;. 
2be97 20 69 6e 74 20 72 63 3b 0a 0a 20 20 28 76 6f 69   int rc;..  (voi
2be98 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2be99 66 66 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a  ff(pParse->db);.
2be9a 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 78    WHERETRACE(("x
2be9b 42 65 73 74 49 6e 64 65 78 20 66 6f 72 20 25 73  BestIndex for %s
2be9c 5c 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  \n", pTab->zName
2be9d 29 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f  ));.  TRACE_IDX_
2be9e 49 4e 50 55 54 53 28 70 29 3b 0a 20 20 72 63 20  INPUTS(p);.  rc 
2be9f 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  = pVtab->pModule
2bea0 2d 3e 78 42 65 73 74 49 6e 64 65 78 28 70 56 74  ->xBestIndex(pVt
2bea1 61 62 2c 20 70 29 3b 0a 20 20 54 52 41 43 45 5f  ab, p);.  TRACE_
2bea2 49 44 58 5f 4f 55 54 50 55 54 53 28 70 29 3b 0a  IDX_OUTPUTS(p);.
2bea3 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2bea4 61 66 65 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e  afetyOn(pParse->
2bea5 64 62 29 3b 0a 0a 20 20 69 66 28 20 72 63 21 3d  db);..  if( rc!=
2bea6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2bea7 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2bea8 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 70  NOMEM ){.      p
2bea9 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
2beaa 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
2beab 20 7d 65 6c 73 65 20 69 66 28 20 21 70 56 74 61   }else if( !pVta
2beac 62 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  b->zErrMsg ){.  
2bead 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2beae 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22  Msg(pParse, "%s"
2beaf 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  , sqlite3ErrStr(
2beb0 72 63 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rc));.    }else{
2beb1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2beb2 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2beb3 25 73 22 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72  %s", pVtab->zErr
2beb4 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Msg);.    }.  }.
2beb5 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2beb6 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 56 74 61  pParse->db, pVta
2beb7 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  b->zErrMsg);.  p
2beb8 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
2beb9 30 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  0;..  for(i=0; i
2beba 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  <p->nConstraint;
2bebb 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21   i++){.    if( !
2bebc 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
2bebd 5d 2e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 61  ].usable && p->a
2bebe 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
2bebf 69 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29  i].argvIndex>0 )
2bec0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2bec1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2bec2 0a 20 20 20 20 20 20 20 20 20 20 22 74 61 62 6c  .          "tabl
2bec3 65 20 25 73 3a 20 78 42 65 73 74 49 6e 64 65 78  e %s: xBestIndex
2bec4 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6e 76   returned an inv
2bec5 61 6c 69 64 20 70 6c 61 6e 22 2c 20 70 54 61 62  alid plan", pTab
2bec6 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ->zName);.    }.
2bec7 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 50    }..  return pP
2bec8 61 72 73 65 2d 3e 6e 45 72 72 3b 0a 7d 0a 0a 0a  arse->nErr;.}...
2bec9 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
2beca 65 20 62 65 73 74 20 69 6e 64 65 78 20 66 6f 72  e best index for
2becb 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2becc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 65 73 74  ..**.** The best
2becd 20 69 6e 64 65 78 20 69 73 20 63 6f 6d 70 75 74   index is comput
2bece 65 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49  ed by the xBestI
2becf 6e 64 65 78 20 6d 65 74 68 6f 64 20 6f 66 20 74  ndex method of t
2bed0 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61  he virtual.** ta
2bed1 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 69  ble module.  Thi
2bed2 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 61  s routine is rea
2bed3 6c 6c 79 20 6a 75 73 74 20 61 20 77 72 61 70 70  lly just a wrapp
2bed4 65 72 20 74 68 61 74 20 73 65 74 73 20 75 70 0a  er that sets up.
2bed5 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ** the sqlite3_i
2bed6 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2bed7 75 72 65 20 74 68 61 74 20 69 73 20 75 73 65 64  ure that is used
2bed8 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20   to communicate 
2bed9 77 69 74 68 0a 2a 2a 20 78 42 65 73 74 49 6e 64  with.** xBestInd
2beda 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a  ex..**.** In a j
2bedb 6f 69 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  oin, this routin
2bedc 65 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65  e might be calle
2bedd 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
2bede 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65   for the.** same
2bedf 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
2bee0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   The sqlite3_ind
2bee1 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2bee2 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20  e is created.** 
2bee3 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  and initialized 
2bee4 6f 6e 20 74 68 65 20 66 69 72 73 74 20 69 6e 76  on the first inv
2bee5 6f 63 61 74 69 6f 6e 20 61 6e 64 20 72 65 75 73  ocation and reus
2bee6 65 64 20 6f 6e 20 61 6c 6c 20 73 75 62 73 65 71  ed on all subseq
2bee7 75 65 6e 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  uent.** invocati
2bee8 6f 6e 73 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ons.  The sqlite
2bee9 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2beea 75 63 74 75 72 65 20 69 73 20 61 6c 73 6f 20 75  ucture is also u
2beeb 73 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 64 65  sed when.** code
2beec 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f   is generated to
2beed 20 61 63 63 65 73 73 20 74 68 65 20 76 69 72 74   access the virt
2beee 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20  ual table.  The 
2beef 77 68 65 72 65 49 6e 66 6f 44 65 6c 65 74 65 28  whereInfoDelete(
2bef0 29 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61  ) .** routine ta
2bef1 6b 65 73 20 63 61 72 65 20 6f 66 20 66 72 65 65  kes care of free
2bef2 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
2bef3 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
2bef4 74 75 72 65 20 61 66 74 65 72 0a 2a 2a 20 65 76  ture after.** ev
2bef5 65 72 79 62 6f 64 79 20 68 61 73 20 66 69 6e 69  erybody has fini
2bef6 73 68 65 64 20 77 69 74 68 20 69 74 2e 0a 2a 2f  shed with it..*/
2bef7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73  .static void bes
2bef8 74 56 69 72 74 75 61 6c 49 6e 64 65 78 28 0a 20  tVirtualIndex(. 
2bef9 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2befa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2befb 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2befc 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65  context */.  Whe
2befd 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20  reClause *pWC,  
2befe 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2beff 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2bf00 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2bf01 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c  List_item *pSrc,
2bf02 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f        /* The FRO
2bf03 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f  M clause term to
2bf04 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74   search */.  Bit
2bf05 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20  mask notReady,  
2bf06 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bf07 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
2bf08 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61  that are not ava
2bf09 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72  ilable */.  Expr
2bf0a 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2bf0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2bf0c 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c 61 75  he order by clau
2bf0d 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73  se */.  WhereCos
2bf0e 74 20 2a 70 43 6f 73 74 2c 20 20 20 20 20 20 20  t *pCost,       
2bf0f 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73          /* Lowes
2bf10 74 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61  t cost query pla
2bf11 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  n */.  sqlite3_i
2bf12 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64  ndex_info **ppId
2bf13 78 49 6e 66 6f 20 20 2f 2a 20 49 6e 64 65 78 20  xInfo  /* Index 
2bf14 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73  information pass
2bf15 65 64 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78  ed to xBestIndex
2bf16 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
2bf17 70 54 61 62 20 3d 20 70 53 72 63 2d 3e 70 54 61  pTab = pSrc->pTa
2bf18 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64  b;.  sqlite3_ind
2bf19 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66  ex_info *pIdxInf
2bf1a 6f 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69  o;.  struct sqli
2bf1b 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
2bf1c 61 69 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b 0a  aint *pIdxCons;.
2bf1d 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
2bf1e 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2bf1f 74 5f 75 73 61 67 65 20 2a 70 55 73 61 67 65 3b  t_usage *pUsage;
2bf20 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
2bf21 65 72 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  erm;.  int i, j;
2bf22 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b  .  int nOrderBy;
2bf23 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
2bf24 20 77 73 46 6c 61 67 73 20 69 73 20 69 6e 69 74   wsFlags is init
2bf25 69 61 6c 69 7a 65 64 20 74 6f 20 73 6f 6d 65 20  ialized to some 
2bf26 73 61 6e 65 20 76 61 6c 75 65 2e 20 4f 74 68 65  sane value. Othe
2bf27 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 0a 20  rwise, if the . 
2bf28 20 2a 2a 20 6d 61 6c 6c 6f 63 20 69 6e 20 61 6c   ** malloc in al
2bf29 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28  locateIndexInfo(
2bf2a 29 20 66 61 69 6c 73 20 61 6e 64 20 74 68 69 73  ) fails and this
2bf2b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
2bf2c 73 20 6c 65 61 76 69 6e 67 0a 20 20 2a 2a 20 77  s leaving.  ** w
2bf2d 73 46 6c 61 67 73 20 69 6e 20 61 6e 20 75 6e 69  sFlags in an uni
2bf2e 6e 69 74 69 61 6c 69 7a 65 64 20 73 74 61 74 65  nitialized state
2bf2f 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79  , the caller may
2bf30 20 62 65 68 61 76 65 20 75 6e 70 72 65 64 69 63   behave unpredic
2bf31 74 61 62 6c 79 2e 0a 20 20 2a 2f 0a 20 20 6d 65  tably..  */.  me
2bf32 6d 73 65 74 28 70 43 6f 73 74 2c 20 30 2c 20 73  mset(pCost, 0, s
2bf33 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b 0a  izeof(*pCost));.
2bf34 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
2bf35 46 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 56 49  Flags = WHERE_VI
2bf36 52 54 55 41 4c 54 41 42 4c 45 3b 0a 0a 20 20 2f  RTUALTABLE;..  /
2bf37 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33  * If the sqlite3
2bf38 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2bf39 63 74 75 72 65 20 68 61 73 20 6e 6f 74 20 62 65  cture has not be
2bf3a 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 20  en previously.  
2bf3b 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  ** allocated and
2bf3c 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68   initialized, th
2bf3d 65 6e 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20  en allocate and 
2bf3e 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 20 6e 6f  initialize it no
2bf3f 77 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e  w..  */.  pIdxIn
2bf40 66 6f 20 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b  fo = *ppIdxInfo;
2bf41 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d  .  if( pIdxInfo=
2bf42 3d 30 20 29 7b 0a 20 20 20 20 2a 70 70 49 64 78  =0 ){.    *ppIdx
2bf43 49 6e 66 6f 20 3d 20 70 49 64 78 49 6e 66 6f 20  Info = pIdxInfo 
2bf44 3d 20 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78 49  = allocateIndexI
2bf45 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 57 43 2c  nfo(pParse, pWC,
2bf46 20 70 53 72 63 2c 20 70 4f 72 64 65 72 42 79 29   pSrc, pOrderBy)
2bf47 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78  ;.  }.  if( pIdx
2bf48 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Info==0 ){.    r
2bf49 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eturn;.  }..  /*
2bf4a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   At this point, 
2bf4b 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
2bf4c 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2bf4d 20 74 68 61 74 20 70 49 64 78 49 6e 66 6f 20 70   that pIdxInfo p
2bf4e 6f 69 6e 74 73 0a 20 20 2a 2a 20 74 6f 20 77 69  oints.  ** to wi
2bf4f 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69  ll have been ini
2bf50 74 69 61 6c 69 7a 65 64 2c 20 65 69 74 68 65 72  tialized, either
2bf51 20 64 75 72 69 6e 67 20 74 68 65 20 63 75 72 72   during the curr
2bf52 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ent invocation o
2bf53 72 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 73 6f  r.  ** during so
2bf54 6d 65 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 74  me prior invocat
2bf55 69 6f 6e 2e 20 20 4e 6f 77 20 77 65 20 6a 75 73  ion.  Now we jus
2bf56 74 20 68 61 76 65 20 74 6f 20 63 75 73 74 6f 6d  t have to custom
2bf57 69 7a 65 20 74 68 65 0a 20 20 2a 2a 20 64 65 74  ize the.  ** det
2bf58 61 69 6c 73 20 6f 66 20 70 49 64 78 49 6e 66 6f  ails of pIdxInfo
2bf59 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
2bf5a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20   invocation and 
2bf5b 70 61 73 73 20 69 74 20 74 6f 0a 20 20 2a 2a 20  pass it to.  ** 
2bf5c 78 42 65 73 74 49 6e 64 65 78 2e 0a 20 20 2a 2f  xBestIndex..  */
2bf5d 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  ..  /* The modul
2bf5e 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 64  e name must be d
2bf5f 65 66 69 6e 65 64 2e 20 41 6c 73 6f 2c 20 62 79  efined. Also, by
2bf60 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72   this point ther
2bf61 65 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 61  e must.  ** be a
2bf62 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
2bf63 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
2bf64 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65  cture. Otherwise
2bf65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 69 65  .  ** sqlite3Vie
2bf66 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
2bf67 29 20 77 6f 75 6c 64 20 68 61 76 65 20 70 69 63  ) would have pic
2bf68 6b 65 64 20 75 70 20 74 68 65 20 65 72 72 6f 72  ked up the error
2bf69 2e 20 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  . .  */.  assert
2bf6a 28 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  ( pTab->azModule
2bf6b 41 72 67 20 26 26 20 70 54 61 62 2d 3e 61 7a 4d  Arg && pTab->azM
2bf6c 6f 64 75 6c 65 41 72 67 5b 30 5d 20 29 3b 0a 20  oduleArg[0] );. 
2bf6d 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 70   assert( pTab->p
2bf6e 56 74 61 62 20 29 3b 0a 0a 20 20 2f 2a 20 53 65  Vtab );..  /* Se
2bf6f 74 20 74 68 65 20 61 43 6f 6e 73 74 72 61 69 6e  t the aConstrain
2bf70 74 5b 5d 2e 75 73 61 62 6c 65 20 66 69 65 6c 64  t[].usable field
2bf71 73 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  s and initialize
2bf72 20 61 6c 6c 20 0a 20 20 2a 2a 20 6f 75 74 70 75   all .  ** outpu
2bf73 74 20 76 61 72 69 61 62 6c 65 73 20 74 6f 20 7a  t variables to z
2bf74 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61  ero..  **.  ** a
2bf75 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61  Constraint[].usa
2bf76 62 6c 65 20 69 73 20 74 72 75 65 20 66 6f 72 20  ble is true for 
2bf77 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 65 72  constraints wher
2bf78 65 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  e the right-hand
2bf79 0a 20 20 2a 2a 20 73 69 64 65 20 63 6f 6e 74 61  .  ** side conta
2bf7a 69 6e 73 20 6f 6e 6c 79 20 72 65 66 65 72 65 6e  ins only referen
2bf7b 63 65 73 20 74 6f 20 74 61 62 6c 65 73 20 74 6f  ces to tables to
2bf7c 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
2bf7d 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 74 61   current.  ** ta
2bf7e 62 6c 65 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ble.  In other w
2bf7f 6f 72 64 73 2c 20 69 66 20 74 68 65 20 63 6f 6e  ords, if the con
2bf80 73 74 72 61 69 6e 74 20 69 73 20 6f 66 20 74 68  straint is of th
2bf81 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a  e form:.  **.  *
2bf82 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75  *           colu
2bf83 6d 6e 20 3d 20 65 78 70 72 0a 20 20 2a 2a 0a 20  mn = expr.  **. 
2bf84 20 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 20 65   ** and we are e
2bf85 76 61 6c 75 61 74 69 6e 67 20 61 20 6a 6f 69 6e  valuating a join
2bf86 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74  , then the const
2bf87 72 61 69 6e 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20  raint on column 
2bf88 69 73 20 0a 20 20 2a 2a 20 6f 6e 6c 79 20 76 61  is .  ** only va
2bf89 6c 69 64 20 69 66 20 61 6c 6c 20 74 61 62 6c 65  lid if all table
2bf8a 73 20 72 65 66 65 72 65 6e 63 65 64 20 69 6e 20  s referenced in 
2bf8b 65 78 70 72 20 6f 63 63 75 72 20 74 6f 20 74 68  expr occur to th
2bf8c 65 20 6c 65 66 74 0a 20 20 2a 2a 20 6f 66 20 74  e left.  ** of t
2bf8d 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
2bf8e 69 6e 67 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a  ing column..  **
2bf8f 0a 20 20 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74  .  ** The aConst
2bf90 72 61 69 6e 74 73 5b 5d 20 61 72 72 61 79 20 63  raints[] array c
2bf91 6f 6e 74 61 69 6e 73 20 65 6e 74 72 69 65 73 20  ontains entries 
2bf92 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69  for all constrai
2bf93 6e 74 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  nts.  ** on the 
2bf94 63 75 72 72 65 6e 74 20 74 61 62 6c 65 2e 20 20  current table.  
2bf95 54 68 61 74 20 77 61 79 20 77 65 20 6f 6e 6c 79  That way we only
2bf96 20 68 61 76 65 20 74 6f 20 63 6f 6d 70 75 74 65   have to compute
2bf97 20 69 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 65 76   it once.  ** ev
2bf98 65 6e 20 74 68 6f 75 67 68 20 77 65 20 6d 69 67  en though we mig
2bf99 68 74 20 74 72 79 20 74 6f 20 70 69 63 6b 20 74  ht try to pick t
2bf9a 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 6d 75  he best index mu
2bf9b 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a 20 20  ltiple times..  
2bf9c 2a 2a 20 46 6f 72 20 65 61 63 68 20 61 74 74 65  ** For each atte
2bf9d 6d 70 74 20 61 74 20 70 69 63 6b 69 6e 67 20 61  mpt at picking a
2bf9e 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 64  n index, the ord
2bf9f 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
2bfa0 74 68 65 0a 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69  the.  ** join mi
2bfa1 67 68 74 20 62 65 20 64 69 66 66 65 72 65 6e 74  ght be different
2bfa2 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 72   so we have to r
2bfa3 65 63 6f 6d 70 75 74 65 20 74 68 65 20 75 73 61  ecompute the usa
2bfa4 62 6c 65 20 66 6c 61 67 0a 20 20 2a 2a 20 65 61  ble flag.  ** ea
2bfa5 63 68 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20  ch time..  */.  
2bfa6 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72  pIdxCons = *(str
2bfa7 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2bfa8 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26  x_constraint**)&
2bfa9 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
2bfaa 72 61 69 6e 74 3b 0a 20 20 70 55 73 61 67 65 20  raint;.  pUsage 
2bfab 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  = pIdxInfo->aCon
2bfac 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20  straintUsage;.  
2bfad 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 49  for(i=0; i<pIdxI
2bfae 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  nfo->nConstraint
2bfaf 3b 20 69 2b 2b 2c 20 70 49 64 78 43 6f 6e 73 2b  ; i++, pIdxCons+
2bfb0 2b 29 7b 0a 20 20 20 20 6a 20 3d 20 70 49 64 78  +){.    j = pIdx
2bfb1 43 6f 6e 73 2d 3e 69 54 65 72 6d 4f 66 66 73 65  Cons->iTermOffse
2bfb2 74 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26  t;.    pTerm = &
2bfb3 70 57 43 2d 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70  pWC->a[j];.    p
2bfb4 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 20  IdxCons->usable 
2bfb5 3d 20 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65  =  (pTerm->prere
2bfb6 71 52 69 67 68 74 20 26 20 6e 6f 74 52 65 61 64  qRight & notRead
2bfb7 79 29 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 7d 0a  y)==0 ?1:0;.  }.
2bfb8 20 20 6d 65 6d 73 65 74 28 70 55 73 61 67 65 2c    memset(pUsage,
2bfb9 20 30 2c 20 73 69 7a 65 6f 66 28 70 55 73 61 67   0, sizeof(pUsag
2bfba 65 5b 30 5d 29 2a 70 49 64 78 49 6e 66 6f 2d 3e  e[0])*pIdxInfo->
2bfbb 6e 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20  nConstraint);.  
2bfbc 69 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65  if( pIdxInfo->ne
2bfbd 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 29  edToFreeIdxStr )
2bfbe 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
2bfbf 65 65 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  ee(pIdxInfo->idx
2bfc0 53 74 72 29 3b 0a 20 20 7d 0a 20 20 70 49 64 78  Str);.  }.  pIdx
2bfc1 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d 20 30  Info->idxStr = 0
2bfc2 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64  ;.  pIdxInfo->id
2bfc3 78 4e 75 6d 20 3d 20 30 3b 0a 20 20 70 49 64 78  xNum = 0;.  pIdx
2bfc4 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65  Info->needToFree
2bfc5 49 64 78 53 74 72 20 3d 20 30 3b 0a 20 20 70 49  IdxStr = 0;.  pI
2bfc6 64 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43  dxInfo->orderByC
2bfc7 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a 20 20 2f  onsumed = 0;.  /
2bfc8 2a 20 28 28 64 6f 75 62 6c 65 29 32 29 20 49 6e  * ((double)2) In
2bfc9 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f   case of SQLITE_
2bfca 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
2bfcb 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 70 49 64 78  INT... */.  pIdx
2bfcc 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43  Info->estimatedC
2bfcd 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47  ost = SQLITE_BIG
2bfce 5f 44 42 4c 20 2f 20 28 28 64 6f 75 62 6c 65 29  _DBL / ((double)
2bfcf 32 29 3b 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d  2);.  nOrderBy =
2bfd0 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65   pIdxInfo->nOrde
2bfd1 72 42 79 3b 0a 20 20 69 66 28 20 21 70 4f 72 64  rBy;.  if( !pOrd
2bfd2 65 72 42 79 20 29 7b 0a 20 20 20 20 70 49 64 78  erBy ){.    pIdx
2bfd3 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d  Info->nOrderBy =
2bfd4 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 76   0;.  }..  if( v
2bfd5 74 61 62 42 65 73 74 49 6e 64 65 78 28 70 50 61  tabBestIndex(pPa
2bfd6 72 73 65 2c 20 70 54 61 62 2c 20 70 49 64 78 49  rse, pTab, pIdxI
2bfd7 6e 66 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 75  nfo) ){.    retu
2bfd8 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  rn;.  }..  /* Th
2bfd9 65 20 63 6f 73 74 20 69 73 20 6e 6f 74 20 61 6c  e cost is not al
2bfda 6c 6f 77 65 64 20 74 6f 20 62 65 20 6c 61 72 67  lowed to be larg
2bfdb 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 42  er than SQLITE_B
2bfdc 49 47 5f 44 42 4c 20 28 74 68 65 0a 20 20 2a 2a  IG_DBL (the.  **
2bfdd 20 69 6e 69 74 61 6c 20 76 61 6c 75 65 20 6f 66   inital value of
2bfde 20 6c 6f 77 65 73 74 43 6f 73 74 20 69 6e 20 74   lowestCost in t
2bfdf 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20 69 74 20  his loop. If it 
2bfe0 69 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a  is, then the.  *
2bfe1 2a 20 28 63 6f 73 74 3c 6c 6f 77 65 73 74 43 6f  * (cost<lowestCo
2bfe2 73 74 29 20 74 65 73 74 20 62 65 6c 6f 77 20 77  st) test below w
2bfe3 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 74 72 75  ill never be tru
2bfe4 65 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 55 73  e..  ** .  ** Us
2bfe5 65 20 22 28 64 6f 75 62 6c 65 29 32 22 20 69 6e  e "(double)2" in
2bfe6 73 74 65 61 64 20 6f 66 20 22 32 2e 30 22 20 69  stead of "2.0" i
2bfe7 6e 20 63 61 73 65 20 4f 4d 49 54 5f 46 4c 4f 41  n case OMIT_FLOA
2bfe8 54 49 4e 47 5f 50 4f 49 4e 54 20 0a 20 20 2a 2a  TING_POINT .  **
2bfe9 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 2a   is defined..  *
2bfea 2f 0a 20 20 69 66 28 20 28 53 51 4c 49 54 45 5f  /.  if( (SQLITE_
2bfeb 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c 65  BIG_DBL/((double
2bfec 29 32 29 29 3c 70 49 64 78 49 6e 66 6f 2d 3e 65  )2))<pIdxInfo->e
2bfed 73 74 69 6d 61 74 65 64 43 6f 73 74 20 29 7b 0a  stimatedCost ){.
2bfee 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74      pCost->rCost
2bfef 20 3d 20 28 53 51 4c 49 54 45 5f 42 49 47 5f 44   = (SQLITE_BIG_D
2bff0 42 4c 2f 28 28 64 6f 75 62 6c 65 29 32 29 29 3b  BL/((double)2));
2bff1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43  .  }else{.    pC
2bff2 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 70 49 64  ost->rCost = pId
2bff3 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64  xInfo->estimated
2bff4 43 6f 73 74 3b 0a 20 20 7d 0a 20 20 70 43 6f 73  Cost;.  }.  pCos
2bff5 74 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49  t->plan.u.pVtabI
2bff6 64 78 20 3d 20 70 49 64 78 49 6e 66 6f 3b 0a 20  dx = pIdxInfo;. 
2bff7 20 69 66 28 20 70 49 64 78 49 6e 66 6f 20 26 26   if( pIdxInfo &&
2bff8 20 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64 65 72   pIdxInfo->order
2bff9 42 79 43 6f 6e 73 75 6d 65 64 20 29 7b 0a 20 20  ByConsumed ){.  
2bffa 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73    pCost->plan.ws
2bffb 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f  Flags |= WHERE_O
2bffc 52 44 45 52 42 59 3b 0a 20 20 7d 0a 20 20 70 43  RDERBY;.  }.  pC
2bffd 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20  ost->plan.nEq = 
2bffe 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e  0;.  pIdxInfo->n
2bfff 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72 64 65 72  OrderBy = nOrder
2c000 42 79 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f  By;..  /* Try to
2c001 20 66 69 6e 64 20 61 20 6d 6f 72 65 20 65 66 66   find a more eff
2c002 69 63 69 65 6e 74 20 61 63 63 65 73 73 20 70 61  icient access pa
2c003 74 74 65 72 6e 20 62 79 20 75 73 69 6e 67 20 6d  ttern by using m
2c004 75 6c 74 69 70 6c 65 20 69 6e 64 65 78 65 73 0a  ultiple indexes.
2c005 20 20 2a 2a 20 74 6f 20 6f 70 74 69 6d 69 7a 65    ** to optimize
2c006 20 61 6e 20 4f 52 20 65 78 70 72 65 73 73 69 6f   an OR expressio
2c007 6e 20 77 69 74 68 69 6e 20 74 68 65 20 57 48 45  n within the WHE
2c008 52 45 20 63 6c 61 75 73 65 2e 20 0a 20 20 2a 2f  RE clause. .  */
2c009 0a 20 20 62 65 73 74 4f 72 43 6c 61 75 73 65 49  .  bestOrClauseI
2c00a 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43  ndex(pParse, pWC
2c00b 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79  , pSrc, notReady
2c00c 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73  , pOrderBy, pCos
2c00d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  t);.}.#endif /* 
2c00e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2c00f 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a  UALTABLE */../*.
2c010 2a 2a 20 46 69 6e 64 20 74 68 65 20 71 75 65 72  ** Find the quer
2c011 79 20 70 6c 61 6e 20 66 6f 72 20 61 63 63 65 73  y plan for acces
2c012 73 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61  sing a particula
2c013 72 20 74 61 62 6c 65 2e 20 20 57 72 69 74 65 20  r table.  Write 
2c014 74 68 65 0a 2a 2a 20 62 65 73 74 20 71 75 65 72  the.** best quer
2c015 79 20 70 6c 61 6e 20 61 6e 64 20 69 74 73 20 63  y plan and its c
2c016 6f 73 74 20 69 6e 74 6f 20 74 68 65 20 57 68 65  ost into the Whe
2c017 72 65 43 6f 73 74 20 6f 62 6a 65 63 74 20 73 75  reCost object su
2c018 70 70 6c 69 65 64 20 61 73 20 74 68 65 0a 2a 2a  pplied as the.**
2c019 20 6c 61 73 74 20 70 61 72 61 6d 65 74 65 72 2e   last parameter.
2c01a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 6f 77 65 73  .**.** The lowes
2c01b 74 20 63 6f 73 74 20 70 6c 61 6e 20 77 69 6e 73  t cost plan wins
2c01c 2e 20 20 54 68 65 20 63 6f 73 74 20 69 73 20 61  .  The cost is a
2c01d 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68  n estimate of th
2c01e 65 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20 43  e amount of.** C
2c01f 50 55 20 61 6e 64 20 64 69 73 6b 20 49 2f 4f 20  PU and disk I/O 
2c020 6e 65 65 64 20 74 6f 20 70 72 6f 63 65 73 73 20  need to process 
2c021 74 68 65 20 72 65 71 75 65 73 74 20 75 73 69 6e  the request usin
2c022 67 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 70  g the selected p
2c023 6c 61 6e 2e 0a 2a 2a 20 46 61 63 74 6f 72 73 20  lan..** Factors 
2c024 74 68 61 74 20 69 6e 66 6c 75 65 6e 63 65 20 63  that influence c
2c025 6f 73 74 20 69 6e 63 6c 75 64 65 3a 0a 2a 2a 0a  ost include:.**.
2c026 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 65 73 74  **    *  The est
2c027 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66  imated number of
2c028 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c 6c 20   rows that will 
2c029 62 65 20 72 65 74 72 69 65 76 65 64 2e 20 20 28  be retrieved.  (
2c02a 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 66 65 77  The.**       few
2c02b 65 72 20 74 68 65 20 62 65 74 74 65 72 2e 29 0a  er the better.).
2c02c 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 57 68 65 74  **.**    *  Whet
2c02d 68 65 72 20 6f 72 20 6e 6f 74 20 73 6f 72 74 69  her or not sorti
2c02e 6e 67 20 6d 75 73 74 20 6f 63 63 75 72 2e 0a 2a  ng must occur..*
2c02f 2a 0a 2a 2a 20 20 20 20 2a 20 20 57 68 65 74 68  *.**    *  Wheth
2c030 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 72 65 20  er or not there 
2c031 6d 75 73 74 20 62 65 20 73 65 70 61 72 61 74 65  must be separate
2c032 20 6c 6f 6f 6b 75 70 73 20 69 6e 20 74 68 65 0a   lookups in the.
2c033 2a 2a 20 20 20 20 20 20 20 69 6e 64 65 78 20 61  **       index a
2c034 6e 64 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 74  nd in the main t
2c035 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  able..**.** If t
2c036 68 65 72 65 20 77 61 73 20 61 6e 20 49 4e 44 45  here was an INDE
2c037 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 28 70  XED BY clause (p
2c038 53 72 63 2d 3e 70 49 6e 64 65 78 29 20 61 74 74  Src->pIndex) att
2c039 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 61 62  ached to the tab
2c03a 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20 53 51 4c  le in.** the SQL
2c03b 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e   statement, then
2c03c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f   this function o
2c03d 6e 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 70 6c  nly considers pl
2c03e 61 6e 73 20 75 73 69 6e 67 20 74 68 65 20 0a 2a  ans using the .*
2c03f 2a 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e 20 49  * named index. I
2c040 66 20 6e 6f 20 73 75 63 68 20 70 6c 61 6e 20 69  f no such plan i
2c041 73 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68  s found, then th
2c042 65 20 72 65 74 75 72 6e 65 64 20 63 6f 73 74 20  e returned cost 
2c043 69 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 49 47  is.** SQLITE_BIG
2c044 5f 44 42 4c 2e 20 49 66 20 61 20 70 6c 61 6e 20  _DBL. If a plan 
2c045 69 73 20 66 6f 75 6e 64 20 74 68 61 74 20 75 73  is found that us
2c046 65 73 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64  es the named ind
2c047 65 78 2c 20 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ex, .** then the
2c048 20 63 6f 73 74 20 69 73 20 63 61 6c 63 75 6c 61   cost is calcula
2c049 74 65 64 20 69 6e 20 74 68 65 20 75 73 75 61 6c  ted in the usual
2c04a 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61   way..**.** If a
2c04b 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61   NOT INDEXED cla
2c04c 75 73 65 20 28 70 53 72 63 2d 3e 6e 6f 74 49 6e  use (pSrc->notIn
2c04d 64 65 78 65 64 21 3d 30 29 20 77 61 73 20 61 74  dexed!=0) was at
2c04e 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 61  tached to the ta
2c04f 62 6c 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 53  ble .** in the S
2c050 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c  ELECT statement,
2c051 20 74 68 65 6e 20 6e 6f 20 69 6e 64 65 78 65 73   then no indexes
2c052 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 2e   are considered.
2c053 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 0a 2a   However, the .*
2c054 2a 20 73 65 6c 65 63 74 65 64 20 70 6c 61 6e 20  * selected plan 
2c055 6d 61 79 20 73 74 69 6c 6c 20 74 61 6b 65 20 61  may still take a
2c056 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20  dvantage of the 
2c057 74 61 62 6c 65 73 20 62 75 69 6c 74 2d 69 6e 20  tables built-in 
2c058 72 6f 77 69 64 0a 2a 2a 20 69 6e 64 65 78 2e 0a  rowid.** index..
2c059 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62  */.static void b
2c05a 65 73 74 42 74 72 65 65 49 6e 64 65 78 28 0a 20  estBtreeIndex(. 
2c05b 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2c05c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c05d 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
2c05e 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ext */.  WhereCl
2c05f 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20  ause *pWC,      
2c060 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2c061 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74  E clause */.  st
2c062 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2c063 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65  m *pSrc,  /* The
2c064 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
2c065 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20  m to search */. 
2c066 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
2c067 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
2c068 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
2c069 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61  that are not ava
2c06a 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72  ilable */.  Expr
2c06b 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2c06c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
2c06d 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
2c06e 2f 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70  /.  WhereCost *p
2c06f 43 6f 73 74 20 20 20 20 20 20 20 20 20 20 20 20  Cost            
2c070 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71  /* Lowest cost q
2c071 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a  uery plan */.){.
2c072 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
2c073 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  rm;           /*
2c074 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f   A single term o
2c075 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2c076 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  se */.  int iCur
2c077 20 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72   = pSrc->iCursor
2c078 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f  ;   /* The curso
2c079 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  r of the table t
2c07a 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f  o be accessed */
2c07b 0a 20 20 49 6e 64 65 78 20 2a 70 50 72 6f 62 65  .  Index *pProbe
2c07c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2c07d 2a 20 41 6e 20 69 6e 64 65 78 20 77 65 20 61 72  * An index we ar
2c07e 65 20 65 76 61 6c 75 61 74 69 6e 67 20 2a 2f 0a  e evaluating */.
2c07f 20 20 69 6e 74 20 72 65 76 3b 20 20 20 20 20 20    int rev;      
2c080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c081 20 54 72 75 65 20 74 6f 20 73 63 61 6e 20 69 6e   True to scan in
2c082 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a   reverse order *
2c083 2f 0a 20 20 69 6e 74 20 77 73 46 6c 61 67 73 3b  /.  int wsFlags;
2c084 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c085 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61  /* Flags associa
2c086 74 65 64 20 77 69 74 68 20 70 50 72 6f 62 65 20  ted with pProbe 
2c087 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 3b 20 20 20  */.  int nEq;   
2c088 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c089 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d   /* Number of ==
2c08a 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e   or IN constrain
2c08b 74 73 20 2a 2f 0a 20 20 69 6e 74 20 65 71 54 65  ts */.  int eqTe
2c08c 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  rmMask;         
2c08d 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 76      /* Mask of v
2c08e 61 6c 69 64 20 65 71 75 61 6c 69 74 79 20 6f 70  alid equality op
2c08f 65 72 61 74 6f 72 73 20 2a 2f 0a 20 20 64 6f 75  erators */.  dou
2c090 62 6c 65 20 63 6f 73 74 3b 20 20 20 20 20 20 20  ble cost;       
2c091 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 73 74           /* Cost
2c092 20 6f 66 20 75 73 69 6e 67 20 70 50 72 6f 62 65   of using pProbe
2c093 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 52 6f   */.  double nRo
2c094 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
2c095 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e    /* Estimated n
2c096 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e  umber of rows in
2c097 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
2c098 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
2c099 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c09a 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
2c09b 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22  .  WHERETRACE(("
2c09c 62 65 73 74 49 6e 64 65 78 3a 20 74 62 6c 3d 25  bestIndex: tbl=%
2c09d 73 20 6e 6f 74 52 65 61 64 79 3d 25 6c 6c 78 5c  s notReady=%llx\
2c09e 6e 22 2c 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e  n", pSrc->pTab->
2c09f 7a 4e 61 6d 65 2c 6e 6f 74 52 65 61 64 79 29 29  zName,notReady))
2c0a0 3b 0a 20 20 70 50 72 6f 62 65 20 3d 20 70 53 72  ;.  pProbe = pSr
2c0a1 63 2d 3e 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  c->pTab->pIndex;
2c0a2 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 6f 74  .  if( pSrc->not
2c0a3 49 6e 64 65 78 65 64 20 29 7b 0a 20 20 20 20 70  Indexed ){.    p
2c0a4 50 72 6f 62 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Probe = 0;.  }..
2c0a5 20 20 2f 2a 20 49 66 20 74 68 65 20 74 61 62 6c    /* If the tabl
2c0a6 65 20 68 61 73 20 6e 6f 20 69 6e 64 69 63 65 73  e has no indices
2c0a7 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 6e   and there are n
2c0a8 6f 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 77  o terms in the w
2c0a9 68 65 72 65 0a 20 20 2a 2a 20 63 6c 61 75 73 65  here.  ** clause
2c0aa 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
2c0ab 68 65 20 52 4f 57 49 44 2c 20 74 68 65 6e 20 77  he ROWID, then w
2c0ac 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  e will never be 
2c0ad 61 62 6c 65 20 74 6f 20 64 6f 0a 20 20 2a 2a 20  able to do.  ** 
2c0ae 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74  anything other t
2c0af 68 61 6e 20 61 20 66 75 6c 6c 20 74 61 62 6c 65  han a full table
2c0b0 20 73 63 61 6e 20 6f 6e 20 74 68 69 73 20 74 61   scan on this ta
2c0b1 62 6c 65 2e 20 20 57 65 20 6d 69 67 68 74 20 61  ble.  We might a
2c0b2 73 0a 20 20 2a 2a 20 77 65 6c 6c 20 70 75 74 20  s.  ** well put 
2c0b3 69 74 20 66 69 72 73 74 20 69 6e 20 74 68 65 20  it first in the 
2c0b4 6a 6f 69 6e 20 6f 72 64 65 72 2e 20 20 54 68 61  join order.  Tha
2c0b5 74 20 77 61 79 2c 20 70 65 72 68 61 70 73 20 69  t way, perhaps i
2c0b6 74 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 72 65  t can be.  ** re
2c0b7 66 65 72 65 6e 63 65 64 20 62 79 20 6f 74 68 65  ferenced by othe
2c0b8 72 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  r tables in the 
2c0b9 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d  join..  */.  mem
2c0ba 73 65 74 28 70 43 6f 73 74 2c 20 30 2c 20 73 69  set(pCost, 0, si
2c0bb 7a 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b 0a 20  zeof(*pCost));. 
2c0bc 20 69 66 28 20 70 50 72 6f 62 65 3d 3d 30 20 26   if( pProbe==0 &
2c0bd 26 0a 20 20 20 20 20 66 69 6e 64 54 65 72 6d 28  &.     findTerm(
2c0be 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 30  pWC, iCur, -1, 0
2c0bf 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f  , WO_EQ|WO_IN|WO
2c0c0 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c  _LT|WO_LE|WO_GT|
2c0c1 57 4f 5f 47 45 2c 30 29 3d 3d 30 20 26 26 0a 20  WO_GE,0)==0 &&. 
2c0c2 20 20 20 20 28 70 4f 72 64 65 72 42 79 3d 3d 30      (pOrderBy==0
2c0c3 20 7c 7c 20 21 73 6f 72 74 61 62 6c 65 42 79 52   || !sortableByR
2c0c4 6f 77 69 64 28 69 43 75 72 2c 20 70 4f 72 64 65  owid(iCur, pOrde
2c0c5 72 42 79 2c 20 70 57 43 2d 3e 70 4d 61 73 6b 53  rBy, pWC->pMaskS
2c0c6 65 74 2c 20 26 72 65 76 29 29 20 29 7b 0a 20 20  et, &rev)) ){.  
2c0c7 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64     if( pParse->d
2c0c8 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2c0c9 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20 29  E_ReverseOrder )
2c0ca 7b 0a 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61  {.      /* For a
2c0cb 70 70 6c 69 63 61 74 69 6f 6e 20 74 65 73 74 69  pplication testi
2c0cc 6e 67 2c 20 72 61 6e 64 6f 6d 6c 79 20 72 65 76  ng, randomly rev
2c0cd 65 72 73 65 20 74 68 65 20 6f 75 74 70 75 74 20  erse the output 
2c0ce 6f 72 64 65 72 20 66 6f 72 0a 20 20 20 20 20 20  order for.      
2c0cf 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
2c0d0 65 6e 74 73 20 74 68 61 74 20 6f 6d 69 74 20 74  ents that omit t
2c0d1 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2c0d2 73 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 68  se.  This will h
2c0d3 65 6c 70 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  elp.      ** to 
2c0d4 66 69 6e 64 20 63 61 73 65 73 20 77 68 65 72 65  find cases where
2c0d5 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2c0d6 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c  pCost->plan.wsFl
2c0d7 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56  ags |= WHERE_REV
2c0d8 45 52 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ERSE;.    }.    
2c0d9 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 43  return;.  }.  pC
2c0da 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 53 51 4c  ost->rCost = SQL
2c0db 49 54 45 5f 42 49 47 5f 44 42 4c 3b 0a 0a 20 20  ITE_BIG_DBL;..  
2c0dc 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20 72  /* Check for a r
2c0dd 6f 77 69 64 3d 45 58 50 52 20 6f 72 20 72 6f 77  owid=EXPR or row
2c0de 69 64 20 49 4e 20 28 2e 2e 2e 29 20 63 6f 6e 73  id IN (...) cons
2c0df 74 72 61 69 6e 74 73 2e 20 49 66 20 74 68 65 72  traints. If ther
2c0e0 65 20 77 61 73 0a 20 20 2a 2a 20 61 6e 20 49 4e  e was.  ** an IN
2c0e1 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20  DEXED BY clause 
2c0e2 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 69 73  attached to this
2c0e3 20 74 61 62 6c 65 2c 20 73 6b 69 70 20 74 68 69   table, skip thi
2c0e4 73 20 73 74 65 70 2e 0a 20 20 2a 2f 0a 20 20 69  s step..  */.  i
2c0e5 66 28 20 21 70 53 72 63 2d 3e 70 49 6e 64 65 78  f( !pSrc->pIndex
2c0e6 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20   ){.    pTerm = 
2c0e7 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
2c0e8 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
2c0e9 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c 20 30  , WO_EQ|WO_IN, 0
2c0ea 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  );.    if( pTerm
2c0eb 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
2c0ec 70 45 78 70 72 3b 0a 20 20 20 20 20 20 70 43 6f  pExpr;.      pCo
2c0ed 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  st->plan.wsFlags
2c0ee 20 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45   = WHERE_ROWID_E
2c0ef 51 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65  Q;.      if( pTe
2c0f0 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2c0f1 57 4f 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 20  WO_EQ ){.       
2c0f2 20 2f 2a 20 52 6f 77 69 64 3d 3d 20 69 73 20 61   /* Rowid== is a
2c0f3 6c 77 61 79 73 20 74 68 65 20 62 65 73 74 20 70  lways the best p
2c0f4 69 63 6b 2e 20 20 4c 6f 6f 6b 20 6e 6f 20 66 75  ick.  Look no fu
2c0f5 72 74 68 65 72 2e 20 20 42 65 63 61 75 73 65 20  rther.  Because 
2c0f6 6f 6e 6c 79 0a 20 20 20 20 20 20 20 20 2a 2a 20  only.        ** 
2c0f7 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 69 73 20  a single row is 
2c0f8 67 65 6e 65 72 61 74 65 64 2c 20 6f 75 74 70 75  generated, outpu
2c0f9 74 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 73  t is always in s
2c0fa 6f 72 74 65 64 20 6f 72 64 65 72 20 2a 2f 0a 20  orted order */. 
2c0fb 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c         pCost->pl
2c0fc 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45  an.wsFlags = WHE
2c0fd 52 45 5f 52 4f 57 49 44 5f 45 51 20 7c 20 57 48  RE_ROWID_EQ | WH
2c0fe 45 52 45 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20  ERE_UNIQUE;.    
2c0ff 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e      pCost->plan.
2c100 6e 45 71 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  nEq = 1;.       
2c101 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e   WHERETRACE(("..
2c102 2e 20 62 65 73 74 20 69 73 20 72 6f 77 69 64 5c  . best is rowid\
2c103 6e 22 29 29 3b 0a 20 20 20 20 20 20 20 20 70 43  n"));.        pC
2c104 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 30 3b 0a  ost->rCost = 0;.
2c105 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 6e          pCost->n
2c106 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Row = 1;.       
2c107 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
2c108 65 6c 73 65 20 69 66 28 20 21 45 78 70 72 48 61  else if( !ExprHa
2c109 73 50 72 6f 70 65 72 74 79 28 28 70 45 78 70 72  sProperty((pExpr
2c10a 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 29   = pTerm->pExpr)
2c10b 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2c10c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26  .             &&
2c10d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20   pExpr->x.pList 
2c10e 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
2c10f 20 20 2f 2a 20 52 6f 77 69 64 20 49 4e 20 28 4c    /* Rowid IN (L
2c110 49 53 54 29 3a 20 63 6f 73 74 20 69 73 20 4e 6c  IST): cost is Nl
2c111 6f 67 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74  ogN where N is t
2c112 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 69 73  he number of lis
2c113 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 6c 65  t.        ** ele
2c114 6d 65 6e 74 73 2e 20 20 2a 2f 0a 20 20 20 20 20  ments.  */.     
2c115 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20     pCost->rCost 
2c116 3d 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20  = pCost->nRow = 
2c117 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
2c118 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 70  nExpr;.        p
2c119 43 6f 73 74 2d 3e 72 43 6f 73 74 20 2a 3d 20 65  Cost->rCost *= e
2c11a 73 74 4c 6f 67 28 70 43 6f 73 74 2d 3e 72 43 6f  stLog(pCost->rCo
2c11b 73 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  st);.      }else
2c11c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77  {.        /* Row
2c11d 69 64 20 49 4e 20 28 53 45 4c 45 43 54 29 3a 20  id IN (SELECT): 
2c11e 63 6f 73 74 20 69 73 20 4e 6c 6f 67 4e 20 77 68  cost is NlogN wh
2c11f 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ere N is the num
2c120 62 65 72 20 6f 66 20 72 6f 77 73 0a 20 20 20 20  ber of rows.    
2c121 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65      ** in the re
2c122 73 75 6c 74 20 6f 66 20 74 68 65 20 69 6e 6e 65  sult of the inne
2c123 72 20 73 65 6c 65 63 74 2e 20 20 57 65 20 68 61  r select.  We ha
2c124 76 65 20 6e 6f 20 77 61 79 20 74 6f 20 65 73 74  ve no way to est
2c125 69 6d 61 74 65 0a 20 20 20 20 20 20 20 20 2a 2a  imate.        **
2c126 20 74 68 61 74 20 76 61 6c 75 65 20 73 6f 20 6d   that value so m
2c127 61 6b 65 20 61 20 77 69 6c 64 20 67 75 65 73 73  ake a wild guess
2c128 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 43 6f  . */.        pCo
2c129 73 74 2d 3e 6e 52 6f 77 20 3d 20 31 30 30 3b 0a  st->nRow = 100;.
2c12a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72          pCost->r
2c12b 43 6f 73 74 20 3d 20 32 30 30 3b 0a 20 20 20 20  Cost = 200;.    
2c12c 20 20 7d 0a 20 20 20 20 20 20 57 48 45 52 45 54    }.      WHERET
2c12d 52 41 43 45 28 28 22 2e 2e 2e 20 72 6f 77 69 64  RACE(("... rowid
2c12e 20 49 4e 20 63 6f 73 74 3a 20 25 2e 39 67 5c 6e   IN cost: %.9g\n
2c12f 22 2c 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 29  ", pCost->rCost)
2c130 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  );.    }.  .    
2c131 2f 2a 20 45 73 74 69 6d 61 74 65 20 74 68 65 20  /* Estimate the 
2c132 63 6f 73 74 20 6f 66 20 61 20 74 61 62 6c 65 20  cost of a table 
2c133 73 63 61 6e 2e 20 20 49 66 20 77 65 20 64 6f 20  scan.  If we do 
2c134 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 6d 61 6e  not know how man
2c135 79 0a 20 20 20 20 2a 2a 20 65 6e 74 72 69 65 73  y.    ** entries
2c136 20 61 72 65 20 69 6e 20 74 68 65 20 74 61 62 6c   are in the tabl
2c137 65 2c 20 75 73 65 20 31 20 6d 69 6c 6c 69 6f 6e  e, use 1 million
2c138 20 61 73 20 61 20 67 75 65 73 73 2e 0a 20 20 20   as a guess..   
2c139 20 2a 2f 0a 20 20 20 20 63 6f 73 74 20 3d 20 70   */.    cost = p
2c13a 50 72 6f 62 65 20 3f 20 70 50 72 6f 62 65 2d 3e  Probe ? pProbe->
2c13b 61 69 52 6f 77 45 73 74 5b 30 5d 20 3a 20 31 30  aiRowEst[0] : 10
2c13c 30 30 30 30 30 3b 0a 20 20 20 20 57 48 45 52 45  00000;.    WHERE
2c13d 54 52 41 43 45 28 28 22 2e 2e 2e 20 74 61 62 6c  TRACE(("... tabl
2c13e 65 20 73 63 61 6e 20 62 61 73 65 20 63 6f 73 74  e scan base cost
2c13f 3a 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29  : %.9g\n", cost)
2c140 29 3b 0a 20 20 20 20 77 73 46 6c 61 67 73 20 3d  );.    wsFlags =
2c141 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e   WHERE_ROWID_RAN
2c142 47 45 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 68  GE;.  .    /* Ch
2c143 65 63 6b 20 66 6f 72 20 63 6f 6e 73 74 72 61 69  eck for constrai
2c144 6e 74 73 20 6f 6e 20 61 20 72 61 6e 67 65 20 6f  nts on a range o
2c145 66 20 72 6f 77 69 64 73 20 69 6e 20 61 20 74 61  f rowids in a ta
2c146 62 6c 65 20 73 63 61 6e 2e 0a 20 20 20 20 2a 2f  ble scan..    */
2c147 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e  .    pTerm = fin
2c148 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
2c149 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57   -1, notReady, W
2c14a 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54  O_LT|WO_LE|WO_GT
2c14b 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a 20 20 20 20  |WO_GE, 0);.    
2c14c 69 66 28 20 70 54 65 72 6d 20 29 7b 0a 20 20 20  if( pTerm ){.   
2c14d 20 20 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28     if( findTerm(
2c14e 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e  pWC, iCur, -1, n
2c14f 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57  otReady, WO_LT|W
2c150 4f 5f 4c 45 2c 20 30 29 20 29 7b 0a 20 20 20 20  O_LE, 0) ){.    
2c151 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2c152 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0a  HERE_TOP_LIMIT;.
2c153 20 20 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20          cost /= 
2c154 33 3b 20 20 2f 2a 20 47 75 65 73 73 20 74 68 61  3;  /* Guess tha
2c155 74 20 72 6f 77 69 64 3c 45 58 50 52 20 65 6c 69  t rowid<EXPR eli
2c156 6d 69 6e 61 74 65 73 20 74 77 6f 2d 74 68 69 72  minates two-thir
2c157 64 73 20 6f 66 20 72 6f 77 73 20 2a 2f 0a 20 20  ds of rows */.  
2c158 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2c159 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
2c15a 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
2c15b 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30  , WO_GT|WO_GE, 0
2c15c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 77 73 46  ) ){.        wsF
2c15d 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 42 54  lags |= WHERE_BT
2c15e 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 20  M_LIMIT;.       
2c15f 20 63 6f 73 74 20 2f 3d 20 33 3b 20 20 2f 2a 20   cost /= 3;  /* 
2c160 47 75 65 73 73 20 74 68 61 74 20 72 6f 77 69 64  Guess that rowid
2c161 3e 45 58 50 52 20 65 6c 69 6d 69 6e 61 74 65 73  >EXPR eliminates
2c162 20 74 77 6f 2d 74 68 69 72 64 73 20 6f 66 20 72   two-thirds of r
2c163 6f 77 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ows */.      }. 
2c164 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
2c165 28 22 2e 2e 2e 20 72 6f 77 69 64 20 72 61 6e 67  ("... rowid rang
2c166 65 20 72 65 64 75 63 65 73 20 63 6f 73 74 20 74  e reduces cost t
2c167 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29  o %.9g\n", cost)
2c168 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2c169 20 20 20 20 77 73 46 6c 61 67 73 20 3d 20 30 3b      wsFlags = 0;
2c16a 0a 20 20 20 20 7d 0a 20 20 20 20 6e 52 6f 77 20  .    }.    nRow 
2c16b 3d 20 63 6f 73 74 3b 0a 20 20 0a 20 20 20 20 2f  = cost;.  .    /
2c16c 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 73  * If the table s
2c16d 63 61 6e 20 64 6f 65 73 20 6e 6f 74 20 73 61 74  can does not sat
2c16e 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 42  isfy the ORDER B
2c16f 59 20 63 6c 61 75 73 65 2c 20 69 6e 63 72 65 61  Y clause, increa
2c170 73 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f  se.    ** the co
2c171 73 74 20 62 79 20 4e 6c 6f 67 4e 20 74 6f 20 63  st by NlogN to c
2c172 6f 76 65 72 20 74 68 65 20 65 78 70 65 6e 73 65  over the expense
2c173 20 6f 66 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a   of sorting. */.
2c174 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
2c175 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 6f   ){.      if( so
2c176 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28 69 43  rtableByRowid(iC
2c177 75 72 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 57  ur, pOrderBy, pW
2c178 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 26 72 65  C->pMaskSet, &re
2c179 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 77 73  v) ){.        ws
2c17a 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f  Flags |= WHERE_O
2c17b 52 44 45 52 42 59 7c 57 48 45 52 45 5f 52 4f 57  RDERBY|WHERE_ROW
2c17c 49 44 5f 52 41 4e 47 45 3b 0a 20 20 20 20 20 20  ID_RANGE;.      
2c17d 20 20 69 66 28 20 72 65 76 20 29 7b 0a 20 20 20    if( rev ){.   
2c17e 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
2c17f 3d 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 3b  = WHERE_REVERSE;
2c180 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2c181 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2c182 63 6f 73 74 20 2b 3d 20 63 6f 73 74 2a 65 73 74  cost += cost*est
2c183 4c 6f 67 28 63 6f 73 74 29 3b 0a 20 20 20 20 20  Log(cost);.     
2c184 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
2c185 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63 72  ... sorting incr
2c186 65 61 73 65 73 20 63 6f 73 74 20 74 6f 20 25 2e  eases cost to %.
2c187 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20  9g\n", cost));. 
2c188 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2c189 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
2c18a 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2c18b 52 65 76 65 72 73 65 4f 72 64 65 72 20 29 7b 0a  ReverseOrder ){.
2c18c 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 70 70        /* For app
2c18d 6c 69 63 61 74 69 6f 6e 20 74 65 73 74 69 6e 67  lication testing
2c18e 2c 20 72 61 6e 64 6f 6d 6c 79 20 72 65 76 65 72  , randomly rever
2c18f 73 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f 72  se the output or
2c190 64 65 72 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a  der for.      **
2c191 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2c192 74 73 20 74 68 61 74 20 6f 6d 69 74 20 74 68 65  ts that omit the
2c193 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2c194 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 68 65 6c  .  This will hel
2c195 70 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 66 69  p.      ** to fi
2c196 6e 64 20 63 61 73 65 73 20 77 68 65 72 65 0a 20  nd cases where. 
2c197 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 77 73       */.      ws
2c198 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52  Flags |= WHERE_R
2c199 45 56 45 52 53 45 3b 0a 20 20 20 20 7d 0a 0a 20  EVERSE;.    }.. 
2c19a 20 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74     /* Remember t
2c19b 68 69 73 20 63 61 73 65 20 69 66 20 69 74 20 69  his case if it i
2c19c 73 20 74 68 65 20 62 65 73 74 20 73 6f 20 66 61  s the best so fa
2c19d 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6f 73  r */.    if( cos
2c19e 74 3c 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29  t<pCost->rCost )
2c19f 7b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 72  {.      pCost->r
2c1a0 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20 20 20  Cost = cost;.   
2c1a1 20 20 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d     pCost->nRow =
2c1a2 20 6e 52 6f 77 3b 0a 20 20 20 20 20 20 70 43 6f   nRow;.      pCo
2c1a3 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  st->plan.wsFlags
2c1a4 20 3d 20 77 73 46 6c 61 67 73 3b 0a 20 20 20 20   = wsFlags;.    
2c1a5 7d 0a 20 20 7d 0a 0a 20 20 62 65 73 74 4f 72 43  }.  }..  bestOrC
2c1a6 6c 61 75 73 65 49 6e 64 65 78 28 70 50 61 72 73  lauseIndex(pPars
2c1a7 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f  e, pWC, pSrc, no
2c1a8 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79  tReady, pOrderBy
2c1a9 2c 20 70 43 6f 73 74 29 3b 0a 0a 20 20 2f 2a 20  , pCost);..  /* 
2c1aa 49 66 20 74 68 65 20 70 53 72 63 20 74 61 62 6c  If the pSrc tabl
2c1ab 65 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74  e is the right t
2c1ac 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4a  able of a LEFT J
2c1ad 4f 49 4e 20 74 68 65 6e 20 77 65 20 6d 61 79 20  OIN then we may 
2c1ae 6e 6f 74 0a 20 20 2a 2a 20 75 73 65 20 61 6e 20  not.  ** use an 
2c1af 69 6e 64 65 78 20 74 6f 20 73 61 74 69 73 66 79  index to satisfy
2c1b0 20 49 53 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61   IS NULL constra
2c1b1 69 6e 74 73 20 6f 6e 20 74 68 61 74 20 74 61 62  ints on that tab
2c1b2 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20 20 2a  le.  This is.  *
2c1b3 2a 20 62 65 63 61 75 73 65 20 63 6f 6c 75 6d 6e  * because column
2c1b4 73 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20 62  s might end up b
2c1b5 65 69 6e 67 20 4e 55 4c 4c 20 69 66 20 74 68 65  eing NULL if the
2c1b6 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20   table does not 
2c1b7 6d 61 74 63 68 20 2d 0a 20 20 2a 2a 20 61 20 63  match -.  ** a c
2c1b8 69 72 63 75 6d 73 74 61 6e 63 65 20 77 68 69 63  ircumstance whic
2c1b9 68 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e 6e  h the index cann
2c1ba 6f 74 20 68 65 6c 70 20 75 73 20 64 69 73 63 6f  ot help us disco
2c1bb 76 65 72 2e 20 20 54 69 63 6b 65 74 20 23 32 31  ver.  Ticket #21
2c1bc 37 37 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  77..  */.  if( (
2c1bd 70 53 72 63 2d 3e 6a 6f 69 6e 74 79 70 65 20 26  pSrc->jointype &
2c1be 20 4a 54 5f 4c 45 46 54 29 21 3d 30 20 29 7b 0a   JT_LEFT)!=0 ){.
2c1bf 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d      eqTermMask =
2c1c0 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20   WO_EQ|WO_IN;.  
2c1c1 7d 65 6c 73 65 7b 0a 20 20 20 20 65 71 54 65 72  }else{.    eqTer
2c1c2 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f  mMask = WO_EQ|WO
2c1c3 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 3b 0a 20  _IN|WO_ISNULL;. 
2c1c4 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 61 74   }..  /* Look at
2c1c5 20 65 61 63 68 20 69 6e 64 65 78 2e 0a 20 20 2a   each index..  *
2c1c6 2f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70 49  /.  if( pSrc->pI
2c1c7 6e 64 65 78 20 29 7b 0a 20 20 20 20 70 50 72 6f  ndex ){.    pPro
2c1c8 62 65 20 3d 20 70 53 72 63 2d 3e 70 49 6e 64 65  be = pSrc->pInde
2c1c9 78 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 3b 20 70  x;.  }.  for(; p
2c1ca 50 72 6f 62 65 3b 20 70 50 72 6f 62 65 3d 28 70  Probe; pProbe=(p
2c1cb 53 72 63 2d 3e 70 49 6e 64 65 78 20 3f 20 30 20  Src->pIndex ? 0 
2c1cc 3a 20 70 50 72 6f 62 65 2d 3e 70 4e 65 78 74 29  : pProbe->pNext)
2c1cd 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 69 6e  ){.    double in
2c1ce 4d 75 6c 74 69 70 6c 69 65 72 20 3d 20 31 3b 20  Multiplier = 1; 
2c1cf 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 71   /* Number of eq
2c1d0 75 61 6c 69 74 79 20 6c 6f 6f 6b 2d 75 70 73 20  uality look-ups 
2c1d1 6e 65 65 64 65 64 20 2a 2f 0a 20 20 20 20 69 6e  needed */.    in
2c1d2 74 20 69 6e 4d 75 6c 74 49 73 45 73 74 20 3d 20  t inMultIsEst = 
2c1d3 30 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  0;      /* True 
2c1d4 69 66 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20  if inMultiplier 
2c1d5 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 2a  is an estimate *
2c1d6 2f 0a 0a 20 20 20 20 57 48 45 52 45 54 52 41 43  /..    WHERETRAC
2c1d7 45 28 28 22 2e 2e 2e 20 69 6e 64 65 78 20 25 73  E(("... index %s
2c1d8 3a 5c 6e 22 2c 20 70 50 72 6f 62 65 2d 3e 7a 4e  :\n", pProbe->zN
2c1d9 61 6d 65 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  ame));..    /* C
2c1da 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
2c1db 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
2c1dc 65 20 69 6e 64 65 78 20 74 68 61 74 20 61 72 65  e index that are
2c1dd 20 73 61 74 69 73 66 69 65 64 0a 20 20 20 20 2a   satisfied.    *
2c1de 2a 20 62 79 20 78 3d 45 58 50 52 20 6f 72 20 78  * by x=EXPR or x
2c1df 20 49 53 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61   IS NULL constra
2c1e0 69 6e 74 73 20 6f 72 20 78 20 49 4e 20 28 2e 2e  ints or x IN (..
2c1e1 2e 29 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a  .) constraints..
2c1e2 20 20 20 20 2a 2a 20 46 6f 72 20 61 20 74 65 72      ** For a ter
2c1e3 6d 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 78 3d  m of the form x=
2c1e4 45 58 50 52 20 6f 72 20 78 20 49 53 20 4e 55 4c  EXPR or x IS NUL
2c1e5 4c 20 77 65 20 6f 6e 6c 79 20 68 61 76 65 20 74  L we only have t
2c1e6 6f 20 64 6f 20 0a 20 20 20 20 2a 2a 20 61 20 73  o do .    ** a s
2c1e7 69 6e 67 6c 65 20 62 69 6e 61 72 79 20 73 65 61  ingle binary sea
2c1e8 72 63 68 2e 20 20 42 75 74 20 66 6f 72 20 78 20  rch.  But for x 
2c1e9 49 4e 20 28 2e 2e 2e 29 20 77 65 20 68 61 76 65  IN (...) we have
2c1ea 20 74 6f 20 64 6f 20 61 0a 20 20 20 20 2a 2a 20   to do a.    ** 
2c1eb 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e 61 72 79  number of binary
2c1ec 20 73 65 61 72 63 68 65 64 0a 20 20 20 20 2a 2a   searched.    **
2c1ed 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75   equal to the nu
2c1ee 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
2c1ef 6f 6e 20 74 68 65 20 52 48 53 20 6f 66 20 74 68  on the RHS of th
2c1f0 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 2e 0a 20  e IN operator.. 
2c1f1 20 20 20 2a 2a 20 54 68 65 20 69 6e 4d 75 6c 74     ** The inMult
2c1f2 69 70 6c 65 72 20 76 61 72 69 61 62 6c 65 20 77  ipler variable w
2c1f3 69 74 68 20 74 72 79 20 74 6f 20 65 73 74 69 6d  ith try to estim
2c1f4 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ate the number o
2c1f5 66 0a 20 20 20 20 2a 2a 20 62 69 6e 61 72 79 20  f.    ** binary 
2c1f6 73 65 61 72 63 68 65 73 20 6e 65 65 64 65 64 2e  searches needed.
2c1f7 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 73 46 6c  .    */.    wsFl
2c1f8 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  ags = 0;.    for
2c1f9 28 69 3d 30 3b 20 69 3c 70 50 72 6f 62 65 2d 3e  (i=0; i<pProbe->
2c1fa 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
2c1fb 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72       int j = pPr
2c1fc 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  obe->aiColumn[i]
2c1fd 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20  ;.      pTerm = 
2c1fe 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
2c1ff 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c  ur, j, notReady,
2c200 20 65 71 54 65 72 6d 4d 61 73 6b 2c 20 70 50 72   eqTermMask, pPr
2c201 6f 62 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  obe);.      if( 
2c202 70 54 65 72 6d 3d 3d 30 20 29 20 62 72 65 61 6b  pTerm==0 ) break
2c203 3b 0a 20 20 20 20 20 20 77 73 46 6c 61 67 73 20  ;.      wsFlags 
2c204 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  |= WHERE_COLUMN_
2c205 45 51 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  EQ;.      if( pT
2c206 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
2c207 20 57 4f 5f 49 4e 20 29 7b 0a 20 20 20 20 20 20   WO_IN ){.      
2c208 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20    Expr *pExpr = 
2c209 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
2c20a 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2c20b 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e   WHERE_COLUMN_IN
2c20c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78  ;.        if( Ex
2c20d 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2c20e 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2c20f 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  t) ){.          
2c210 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 2a 3d 20  inMultiplier *= 
2c211 32 35 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  25;.          in
2c212 4d 75 6c 74 49 73 45 73 74 20 3d 20 31 3b 0a 20  MultIsEst = 1;. 
2c213 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2c214 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20   pExpr->x.pList 
2c215 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 4d  ){.          inM
2c216 75 6c 74 69 70 6c 69 65 72 20 2a 3d 20 70 45 78  ultiplier *= pEx
2c217 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78  pr->x.pList->nEx
2c218 70 72 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20  pr + 1;.        
2c219 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
2c21a 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2c21b 6f 72 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29  or & WO_ISNULL )
2c21c 7b 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67  {.        wsFlag
2c21d 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d  s |= WHERE_COLUM
2c21e 4e 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a  N_NULL;.      }.
2c21f 20 20 20 20 7d 0a 20 20 20 20 6e 52 6f 77 20 3d      }.    nRow =
2c220 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73   pProbe->aiRowEs
2c221 74 5b 69 5d 20 2a 20 69 6e 4d 75 6c 74 69 70 6c  t[i] * inMultipl
2c222 69 65 72 3b 0a 20 20 20 20 2f 2a 20 49 66 20 69  ier;.    /* If i
2c223 6e 4d 75 6c 74 69 70 6c 69 65 72 20 69 73 20 61  nMultiplier is a
2c224 6e 20 65 73 74 69 6d 61 74 65 20 61 6e 64 20 74  n estimate and t
2c225 68 61 74 20 65 73 74 69 6d 61 74 65 20 72 65 73  hat estimate res
2c226 75 6c 74 73 20 69 6e 20 61 6e 0a 20 20 20 20 2a  ults in an.    *
2c227 2a 20 6e 52 6f 77 20 69 74 20 74 68 61 74 20 69  * nRow it that i
2c228 73 20 6d 6f 72 65 20 74 68 61 6e 20 68 61 6c 66  s more than half
2c229 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
2c22a 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 0a 20 20  in the table,.  
2c22b 20 20 2a 2a 20 74 68 65 6e 20 72 65 64 75 63 65    ** then reduce
2c22c 20 69 6e 4d 75 6c 74 69 70 6c 65 72 20 2a 2f 0a   inMultipler */.
2c22d 20 20 20 20 69 66 28 20 69 6e 4d 75 6c 74 49 73      if( inMultIs
2c22e 45 73 74 20 26 26 20 6e 52 6f 77 2a 32 20 3e 20  Est && nRow*2 > 
2c22f 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73 74  pProbe->aiRowEst
2c230 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 6e 52 6f  [0] ){.      nRo
2c231 77 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f  w = pProbe->aiRo
2c232 77 45 73 74 5b 30 5d 2f 32 3b 0a 20 20 20 20 20  wEst[0]/2;.     
2c233 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 3d 20   inMultiplier = 
2c234 6e 52 6f 77 2f 70 50 72 6f 62 65 2d 3e 61 69 52  nRow/pProbe->aiR
2c235 6f 77 45 73 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a  owEst[i];.    }.
2c236 20 20 20 20 63 6f 73 74 20 3d 20 6e 52 6f 77 20      cost = nRow 
2c237 2b 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 2a 65  + inMultiplier*e
2c238 73 74 4c 6f 67 28 70 50 72 6f 62 65 2d 3e 61 69  stLog(pProbe->ai
2c239 52 6f 77 45 73 74 5b 30 5d 29 3b 0a 20 20 20 20  RowEst[0]);.    
2c23a 6e 45 71 20 3d 20 69 3b 0a 20 20 20 20 69 66 28  nEq = i;.    if(
2c23b 20 70 50 72 6f 62 65 2d 3e 6f 6e 45 72 72 6f 72   pProbe->onError
2c23c 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 6e 45 71  !=OE_None && nEq
2c23d 3d 3d 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d  ==pProbe->nColum
2c23e 6e 20 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63  n ){.      testc
2c23f 61 73 65 28 20 77 73 46 6c 61 67 73 20 26 20 57  ase( wsFlags & W
2c240 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 29  HERE_COLUMN_IN )
2c241 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2c242 28 20 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  ( wsFlags & WHER
2c243 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 29 3b  E_COLUMN_NULL );
2c244 0a 20 20 20 20 20 20 69 66 28 20 28 77 73 46 6c  .      if( (wsFl
2c245 61 67 73 20 26 20 28 57 48 45 52 45 5f 43 4f 4c  ags & (WHERE_COL
2c246 55 4d 4e 5f 49 4e 7c 57 48 45 52 45 5f 43 4f 4c  UMN_IN|WHERE_COL
2c247 55 4d 4e 5f 4e 55 4c 4c 29 29 3d 3d 30 20 29 7b  UMN_NULL))==0 ){
2c248 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73  .        wsFlags
2c249 20 7c 3d 20 57 48 45 52 45 5f 55 4e 49 51 55 45   |= WHERE_UNIQUE
2c24a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2c24b 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
2c24c 22 2e 2e 2e 2e 2e 2e 20 6e 45 71 3d 25 64 20 69  "...... nEq=%d i
2c24d 6e 4d 75 6c 74 3d 25 2e 39 67 20 6e 52 6f 77 3d  nMult=%.9g nRow=
2c24e 25 2e 39 67 20 63 6f 73 74 3d 25 2e 39 67 5c 6e  %.9g cost=%.9g\n
2c24f 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
2c250 20 20 20 6e 45 71 2c 20 69 6e 4d 75 6c 74 69 70     nEq, inMultip
2c251 6c 69 65 72 2c 20 6e 52 6f 77 2c 20 63 6f 73 74  lier, nRow, cost
2c252 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b  ));..    /* Look
2c253 20 66 6f 72 20 72 61 6e 67 65 20 63 6f 6e 73 74   for range const
2c254 72 61 69 6e 74 73 2e 20 20 41 73 73 75 6d 65 20  raints.  Assume 
2c255 74 68 61 74 20 65 61 63 68 20 72 61 6e 67 65 20  that each range 
2c256 63 6f 6e 73 74 72 61 69 6e 74 0a 20 20 20 20 2a  constraint.    *
2c257 2a 20 6d 61 6b 65 73 20 74 68 65 20 73 65 61 72  * makes the sear
2c258 63 68 20 73 70 61 63 65 20 31 2f 33 72 64 20 73  ch space 1/3rd s
2c259 6d 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20  maller..    */. 
2c25a 20 20 20 69 66 28 20 6e 45 71 3c 70 50 72 6f 62     if( nEq<pProb
2c25b 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  e->nColumn ){.  
2c25c 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f      int j = pPro
2c25d 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71  be->aiColumn[nEq
2c25e 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d  ];.      pTerm =
2c25f 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2c260 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79  Cur, j, notReady
2c261 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f  , WO_LT|WO_LE|WO
2c262 5f 47 54 7c 57 4f 5f 47 45 2c 20 70 50 72 6f 62  _GT|WO_GE, pProb
2c263 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  e);.      if( pT
2c264 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 77  erm ){.        w
2c265 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2c266 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 3b 0a 20 20  COLUMN_RANGE;.  
2c267 20 20 20 20 20 20 69 66 28 20 66 69 6e 64 54 65        if( findTe
2c268 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c  rm(pWC, iCur, j,
2c269 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54   notReady, WO_LT
2c26a 7c 57 4f 5f 4c 45 2c 20 70 50 72 6f 62 65 29 20  |WO_LE, pProbe) 
2c26b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 73 46  ){.          wsF
2c26c 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 54 4f  lags |= WHERE_TO
2c26d 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 20  P_LIMIT;.       
2c26e 20 20 20 63 6f 73 74 20 2f 3d 20 33 3b 0a 20 20     cost /= 3;.  
2c26f 20 20 20 20 20 20 20 20 6e 52 6f 77 20 2f 3d 20          nRow /= 
2c270 33 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  3;.        }.   
2c271 20 20 20 20 20 69 66 28 20 66 69 6e 64 54 65 72       if( findTer
2c272 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20  m(pWC, iCur, j, 
2c273 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c  notReady, WO_GT|
2c274 57 4f 5f 47 45 2c 20 70 50 72 6f 62 65 29 20 29  WO_GE, pProbe) )
2c275 7b 0a 20 20 20 20 20 20 20 20 20 20 77 73 46 6c  {.          wsFl
2c276 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 42 54 4d  ags |= WHERE_BTM
2c277 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 20 20  _LIMIT;.        
2c278 20 20 63 6f 73 74 20 2f 3d 20 33 3b 0a 20 20 20    cost /= 3;.   
2c279 20 20 20 20 20 20 20 6e 52 6f 77 20 2f 3d 20 33         nRow /= 3
2c27a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2c27b 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
2c27c 22 2e 2e 2e 2e 2e 2e 20 72 61 6e 67 65 20 72 65  "...... range re
2c27d 64 75 63 65 73 20 6e 52 6f 77 20 74 6f 20 25 2e  duces nRow to %.
2c27e 39 67 20 61 6e 64 20 63 6f 73 74 20 74 6f 20 25  9g and cost to %
2c27f 2e 39 67 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  .9g\n",.        
2c280 20 20 20 20 20 20 20 20 20 20 20 20 6e 52 6f 77              nRow
2c281 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20 20 20 20  , cost));.      
2c282 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
2c283 41 64 64 20 74 68 65 20 61 64 64 69 74 69 6f 6e  Add the addition
2c284 61 6c 20 63 6f 73 74 20 6f 66 20 73 6f 72 74 69  al cost of sorti
2c285 6e 67 20 69 66 20 74 68 61 74 20 69 73 20 61 20  ng if that is a 
2c286 66 61 63 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  factor..    */. 
2c287 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
2c288 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 77 73  ){.      if( (ws
2c289 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 43  Flags & (WHERE_C
2c28a 4f 4c 55 4d 4e 5f 49 4e 7c 57 48 45 52 45 5f 43  OLUMN_IN|WHERE_C
2c28b 4f 4c 55 4d 4e 5f 4e 55 4c 4c 29 29 3d 3d 30 0a  OLUMN_NULL))==0.
2c28c 20 20 20 20 20 20 20 26 26 20 69 73 53 6f 72 74         && isSort
2c28d 69 6e 67 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ingIndex(pParse,
2c28e 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 70 50  pWC->pMaskSet,pP
2c28f 72 6f 62 65 2c 69 43 75 72 2c 70 4f 72 64 65 72  robe,iCur,pOrder
2c290 42 79 2c 6e 45 71 2c 26 72 65 76 29 0a 20 20 20  By,nEq,&rev).   
2c291 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66     ){.        if
2c292 28 20 77 73 46 6c 61 67 73 3d 3d 30 20 29 7b 0a  ( wsFlags==0 ){.
2c293 20 20 20 20 20 20 20 20 20 20 77 73 46 6c 61 67            wsFlag
2c294 73 20 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  s = WHERE_COLUMN
2c295 5f 52 41 4e 47 45 3b 0a 20 20 20 20 20 20 20 20  _RANGE;.        
2c296 7d 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67  }.        wsFlag
2c297 73 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44 45 52  s |= WHERE_ORDER
2c298 42 59 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  BY;.        if( 
2c299 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20 20  rev ){.         
2c29a 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2c29b 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 20 20 20  E_REVERSE;.     
2c29c 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
2c29d 7b 0a 20 20 20 20 20 20 20 20 63 6f 73 74 20 2b  {.        cost +
2c29e 3d 20 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f  = cost*estLog(co
2c29f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 57 48 45  st);.        WHE
2c2a0 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e  RETRACE(("......
2c2a1 20 6f 72 64 65 72 62 79 20 69 6e 63 72 65 61 73   orderby increas
2c2a2 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c  es cost to %.9g\
2c2a3 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20 20  n", cost));.    
2c2a4 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
2c2a5 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c  ( pParse->db->fl
2c2a6 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 76  ags & SQLITE_Rev
2c2a7 65 72 73 65 4f 72 64 65 72 20 29 7b 0a 20 20 20  erseOrder ){.   
2c2a8 20 20 20 2f 2a 20 46 6f 72 20 61 70 70 6c 69 63     /* For applic
2c2a9 61 74 69 6f 6e 20 74 65 73 74 69 6e 67 2c 20 72  ation testing, r
2c2aa 61 6e 64 6f 6d 6c 79 20 72 65 76 65 72 73 65 20  andomly reverse 
2c2ab 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72  the output order
2c2ac 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 53 45   for.      ** SE
2c2ad 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
2c2ae 74 68 61 74 20 6f 6d 69 74 20 74 68 65 20 4f 52  that omit the OR
2c2af 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20  DER BY clause.  
2c2b0 54 68 69 73 20 77 69 6c 6c 20 68 65 6c 70 0a 20  This will help. 
2c2b1 20 20 20 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20       ** to find 
2c2b2 63 61 73 65 73 20 77 68 65 72 65 0a 20 20 20 20  cases where.    
2c2b3 20 20 2a 2f 0a 20 20 20 20 20 20 77 73 46 6c 61    */.      wsFla
2c2b4 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56 45  gs |= WHERE_REVE
2c2b5 52 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  RSE;.    }..    
2c2b6 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
2c2b7 69 66 20 77 65 20 63 61 6e 20 67 65 74 20 61 77  if we can get aw
2c2b8 61 79 20 77 69 74 68 20 75 73 69 6e 67 20 6a 75  ay with using ju
2c2b9 73 74 20 74 68 65 20 69 6e 64 65 78 20 77 69 74  st the index wit
2c2ba 68 6f 75 74 0a 20 20 20 20 2a 2a 20 65 76 65 72  hout.    ** ever
2c2bb 20 72 65 61 64 69 6e 67 20 74 68 65 20 74 61 62   reading the tab
2c2bc 6c 65 2e 20 20 49 66 20 74 68 61 74 20 69 73 20  le.  If that is 
2c2bd 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 68  the case, then h
2c2be 61 6c 76 65 20 74 68 65 0a 20 20 20 20 2a 2a 20  alve the.    ** 
2c2bf 63 6f 73 74 20 6f 66 20 74 68 69 73 20 69 6e 64  cost of this ind
2c2c0 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ex..    */.    i
2c2c1 66 28 20 77 73 46 6c 61 67 73 20 26 26 20 70 53  f( wsFlags && pS
2c2c2 72 63 2d 3e 63 6f 6c 55 73 65 64 20 3c 20 28 28  rc->colUsed < ((
2c2c3 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 28 42 4d  (Bitmask)1)<<(BM
2c2c4 53 2d 31 29 29 20 29 7b 0a 20 20 20 20 20 20 42  S-1)) ){.      B
2c2c5 69 74 6d 61 73 6b 20 6d 20 3d 20 70 53 72 63 2d  itmask m = pSrc-
2c2c6 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20 20  >colUsed;.      
2c2c7 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72  int j;.      for
2c2c8 28 6a 3d 30 3b 20 6a 3c 70 50 72 6f 62 65 2d 3e  (j=0; j<pProbe->
2c2c9 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20  nColumn; j++){. 
2c2ca 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 70         int x = p
2c2cb 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  Probe->aiColumn[
2c2cc 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  j];.        if( 
2c2cd 78 3c 42 4d 53 2d 31 20 29 7b 0a 20 20 20 20 20  x<BMS-1 ){.     
2c2ce 20 20 20 20 20 6d 20 26 3d 20 7e 28 28 28 42 69       m &= ~(((Bi
2c2cf 74 6d 61 73 6b 29 31 29 3c 3c 78 29 3b 0a 20 20  tmask)1)<<x);.  
2c2d0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2c2d1 20 20 20 20 20 20 69 66 28 20 6d 3d 3d 30 20 29        if( m==0 )
2c2d2 7b 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67  {.        wsFlag
2c2d3 73 20 7c 3d 20 57 48 45 52 45 5f 49 44 58 5f 4f  s |= WHERE_IDX_O
2c2d4 4e 4c 59 3b 0a 20 20 20 20 20 20 20 20 63 6f 73  NLY;.        cos
2c2d5 74 20 2f 3d 20 32 3b 0a 20 20 20 20 20 20 20 20  t /= 2;.        
2c2d6 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e  WHERETRACE(("...
2c2d7 2e 2e 2e 20 69 64 78 2d 6f 6e 6c 79 20 72 65 64  ... idx-only red
2c2d8 75 63 65 73 20 63 6f 73 74 20 74 6f 20 25 2e 39  uces cost to %.9
2c2d9 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20  g\n", cost));.  
2c2da 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2c2db 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 64 65   /* If this inde
2c2dc 78 20 68 61 73 20 61 63 68 69 65 76 65 64 20 74  x has achieved t
2c2dd 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 73  he lowest cost s
2c2de 6f 20 66 61 72 2c 20 74 68 65 6e 20 75 73 65 20  o far, then use 
2c2df 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  it..    */.    i
2c2e0 66 28 20 77 73 46 6c 61 67 73 21 3d 30 20 26 26  f( wsFlags!=0 &&
2c2e1 20 63 6f 73 74 20 3c 20 70 43 6f 73 74 2d 3e 72   cost < pCost->r
2c2e2 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 70 43  Cost ){.      pC
2c2e3 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 63 6f 73  ost->rCost = cos
2c2e4 74 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  t;.      pCost->
2c2e5 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20  nRow = nRow;.   
2c2e6 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77     pCost->plan.w
2c2e7 73 46 6c 61 67 73 20 3d 20 77 73 46 6c 61 67 73  sFlags = wsFlags
2c2e8 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70  ;.      pCost->p
2c2e9 6c 61 6e 2e 6e 45 71 20 3d 20 6e 45 71 3b 0a 20  lan.nEq = nEq;. 
2c2ea 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f       assert( pCo
2c2eb 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  st->plan.wsFlags
2c2ec 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44   & WHERE_INDEXED
2c2ed 20 29 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d   );.      pCost-
2c2ee 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 20 3d 20 70  >plan.u.pIdx = p
2c2ef 50 72 6f 62 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  Probe;.    }.  }
2c2f0 0a 0a 20 20 2f 2a 20 52 65 70 6f 72 74 20 74 68  ..  /* Report th
2c2f1 65 20 62 65 73 74 20 72 65 73 75 6c 74 0a 20 20  e best result.  
2c2f2 2a 2f 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e  */.  pCost->plan
2c2f3 2e 77 73 46 6c 61 67 73 20 7c 3d 20 65 71 54 65  .wsFlags |= eqTe
2c2f4 72 6d 4d 61 73 6b 3b 0a 20 20 57 48 45 52 45 54  rmMask;.  WHERET
2c2f5 52 41 43 45 28 28 22 62 65 73 74 20 69 6e 64 65  RACE(("best inde
2c2f6 78 20 69 73 20 25 73 2c 20 63 6f 73 74 3d 25 2e  x is %s, cost=%.
2c2f7 39 67 2c 20 6e 72 6f 77 3d 25 2e 39 67 2c 20 77  9g, nrow=%.9g, w
2c2f8 73 46 6c 61 67 73 3d 25 78 2c 20 6e 45 71 3d 25  sFlags=%x, nEq=%
2c2f9 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 28 70  d\n",.        (p
2c2fa 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  Cost->plan.wsFla
2c2fb 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58  gs & WHERE_INDEX
2c2fc 45 44 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20  ED)!=0 ?.       
2c2fd 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61        pCost->pla
2c2fe 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 20  n.u.pIdx->zName 
2c2ff 3a 20 22 28 6e 6f 6e 65 29 22 2c 20 70 43 6f 73  : "(none)", pCos
2c300 74 2d 3e 6e 52 6f 77 2c 0a 20 20 20 20 20 20 20  t->nRow,.       
2c301 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 2c 20 70   pCost->rCost, p
2c302 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  Cost->plan.wsFla
2c303 67 73 2c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e  gs, pCost->plan.
2c304 6e 45 71 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  nEq));.}../*.** 
2c305 46 69 6e 64 20 74 68 65 20 71 75 65 72 79 20 70  Find the query p
2c306 6c 61 6e 20 66 6f 72 20 61 63 63 65 73 73 69 6e  lan for accessin
2c307 67 20 74 61 62 6c 65 20 70 53 72 63 2d 3e 70 54  g table pSrc->pT
2c308 61 62 2e 20 57 72 69 74 65 20 74 68 65 0a 2a 2a  ab. Write the.**
2c309 20 62 65 73 74 20 71 75 65 72 79 20 70 6c 61 6e   best query plan
2c30a 20 61 6e 64 20 69 74 73 20 63 6f 73 74 20 69 6e   and its cost in
2c30b 74 6f 20 74 68 65 20 57 68 65 72 65 43 6f 73 74  to the WhereCost
2c30c 20 6f 62 6a 65 63 74 20 73 75 70 70 6c 69 65 64   object supplied
2c30d 20 0a 2a 2a 20 61 73 20 74 68 65 20 6c 61 73 74   .** as the last
2c30e 20 70 61 72 61 6d 65 74 65 72 2e 20 54 68 69 73   parameter. This
2c30f 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 63 61   function may ca
2c310 6c 63 75 6c 61 74 65 20 74 68 65 20 63 6f 73 74  lculate the cost
2c311 20 6f 66 0a 2a 2a 20 62 6f 74 68 20 72 65 61 6c   of.** both real
2c312 20 61 6e 64 20 76 69 72 74 75 61 6c 20 74 61 62   and virtual tab
2c313 6c 65 20 73 63 61 6e 73 2e 0a 2a 2f 0a 73 74 61  le scans..*/.sta
2c314 74 69 63 20 76 6f 69 64 20 62 65 73 74 49 6e 64  tic void bestInd
2c315 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ex(.  Parse *pPa
2c316 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
2c317 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
2c318 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
2c319 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2c31a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2c31b 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2c31c 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2c31d 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f  t_item *pSrc,  /
2c31e 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * The FROM claus
2c31f 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68  e term to search
2c320 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f   */.  Bitmask no
2c321 74 52 65 61 64 79 2c 20 20 20 20 20 20 20 20 20  tReady,         
2c322 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72    /* Mask of cur
2c323 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f  sors that are no
2c324 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20  t available */. 
2c325 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2c326 72 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rBy,         /* 
2c327 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
2c328 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f  use */.  WhereCo
2c329 73 74 20 2a 70 43 6f 73 74 20 20 20 20 20 20 20  st *pCost       
2c32a 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63       /* Lowest c
2c32b 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a  ost query plan *
2c32c 2f 0a 29 7b 0a 20 20 69 66 28 20 49 73 56 69 72  /.){.  if( IsVir
2c32d 74 75 61 6c 28 70 53 72 63 2d 3e 70 54 61 62 29  tual(pSrc->pTab)
2c32e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2c32f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 20 3d 20  index_info *p = 
2c330 30 3b 0a 20 20 20 20 62 65 73 74 56 69 72 74 75  0;.    bestVirtu
2c331 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  alIndex(pParse, 
2c332 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65  pWC, pSrc, notRe
2c333 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  ady, pOrderBy, p
2c334 43 6f 73 74 2c 20 26 70 29 3b 0a 20 20 20 20 69  Cost, &p);.    i
2c335 66 28 20 70 2d 3e 6e 65 65 64 54 6f 46 72 65 65  f( p->needToFree
2c336 49 64 78 53 74 72 20 29 7b 0a 20 20 20 20 20 20  IdxStr ){.      
2c337 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
2c338 69 64 78 53 74 72 29 3b 0a 20 20 20 20 7d 0a 20  idxStr);.    }. 
2c339 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2c33a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 29 3b  (pParse->db, p);
2c33b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 65  .  }else{.    be
2c33c 73 74 42 74 72 65 65 49 6e 64 65 78 28 70 50 61  stBtreeIndex(pPa
2c33d 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20  rse, pWC, pSrc, 
2c33e 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72  notReady, pOrder
2c33f 42 79 2c 20 70 43 6f 73 74 29 3b 0a 20 20 7d 0a  By, pCost);.  }.
2c340 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c 65  }../*.** Disable
2c341 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57   a term in the W
2c342 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45 78  HERE clause.  Ex
2c343 63 65 70 74 2c 20 64 6f 20 6e 6f 74 20 64 69 73  cept, do not dis
2c344 61 62 6c 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a  able the term.**
2c345 20 69 66 20 69 74 20 63 6f 6e 74 72 6f 6c 73 20   if it controls 
2c346 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  a LEFT OUTER JOI
2c347 4e 20 61 6e 64 20 69 74 20 64 69 64 20 6e 6f 74  N and it did not
2c348 20 6f 72 69 67 69 6e 61 74 65 20 69 6e 20 74 68   originate in th
2c349 65 20 4f 4e 0a 2a 2a 20 6f 72 20 55 53 49 4e 47  e ON.** or USING
2c34a 20 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20   clause of that 
2c34b 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73  join..**.** Cons
2c34c 69 64 65 72 20 74 68 65 20 74 65 72 6d 20 74 32  ider the term t2
2c34d 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74 68 65 20 66  .z='ok' in the f
2c34e 6f 6c 6c 6f 77 69 6e 67 20 71 75 65 72 69 65 73  ollowing queries
2c34f 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 53  :.**.**   (1)  S
2c350 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20  ELECT * FROM t1 
2c351 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20  LEFT JOIN t2 ON 
2c352 74 31 2e 61 3d 74 32 2e 78 20 57 48 45 52 45 20  t1.a=t2.x WHERE 
2c353 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20 28  t2.z='ok'.**   (
2c354 32 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  2)  SELECT * FRO
2c355 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74  M t1 LEFT JOIN t
2c356 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 41  2 ON t1.a=t2.x A
2c357 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20  ND t2.z='ok'.** 
2c358 20 20 28 33 29 20 20 53 45 4c 45 43 54 20 2a 20    (3)  SELECT * 
2c359 46 52 4f 4d 20 74 31 2c 20 74 32 20 57 48 45 52  FROM t1, t2 WHER
2c35a 45 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e 44 20  E t1.a=t2.x AND 
2c35b 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a 2a 2a 20  t2.z='ok'.**.** 
2c35c 54 68 65 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 73  The t2.z='ok' is
2c35d 20 64 69 73 61 62 6c 65 64 20 69 6e 20 74 68 65   disabled in the
2c35e 20 69 6e 20 28 32 29 20 62 65 63 61 75 73 65 20   in (2) because 
2c35f 69 74 20 6f 72 69 67 69 6e 61 74 65 73 0a 2a 2a  it originates.**
2c360 20 69 6e 20 74 68 65 20 4f 4e 20 63 6c 61 75 73   in the ON claus
2c361 65 2e 20 20 54 68 65 20 74 65 72 6d 20 69 73 20  e.  The term is 
2c362 64 69 73 61 62 6c 65 64 20 69 6e 20 28 33 29 20  disabled in (3) 
2c363 62 65 63 61 75 73 65 20 69 74 20 69 73 20 6e 6f  because it is no
2c364 74 20 70 61 72 74 0a 2a 2a 20 6f 66 20 61 20 4c  t part.** of a L
2c365 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2e 20  EFT OUTER JOIN. 
2c366 20 49 6e 20 28 31 29 2c 20 74 68 65 20 74 65 72   In (1), the ter
2c367 6d 20 69 73 20 6e 6f 74 20 64 69 73 61 62 6c 65  m is not disable
2c368 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62 6c 69  d..**.** Disabli
2c369 6e 67 20 61 20 74 65 72 6d 20 63 61 75 73 65 73  ng a term causes
2c36a 20 74 68 61 74 20 74 65 72 6d 20 74 6f 20 6e 6f   that term to no
2c36b 74 20 62 65 20 74 65 73 74 65 64 20 69 6e 20 74  t be tested in t
2c36c 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a  he inner loop.**
2c36d 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 44   of the join.  D
2c36e 69 73 61 62 6c 69 6e 67 20 69 73 20 61 6e 20 6f  isabling is an o
2c36f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 57 68  ptimization.  Wh
2c370 65 6e 20 74 65 72 6d 73 20 61 72 65 20 73 61 74  en terms are sat
2c371 69 73 66 69 65 64 0a 2a 2a 20 62 79 20 69 6e 64  isfied.** by ind
2c372 69 63 65 73 2c 20 77 65 20 64 69 73 61 62 6c 65  ices, we disable
2c373 20 74 68 65 6d 20 74 6f 20 70 72 65 76 65 6e 74   them to prevent
2c374 20 72 65 64 75 6e 64 61 6e 74 20 74 65 73 74 73   redundant tests
2c375 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 0a 2a 2a   in the inner.**
2c376 20 6c 6f 6f 70 2e 20 20 57 65 20 77 6f 75 6c 64   loop.  We would
2c377 20 67 65 74 20 74 68 65 20 63 6f 72 72 65 63 74   get the correct
2c378 20 72 65 73 75 6c 74 73 20 69 66 20 6e 6f 74 68   results if noth
2c379 69 6e 67 20 77 65 72 65 20 65 76 65 72 20 64 69  ing were ever di
2c37a 73 61 62 6c 65 64 2c 0a 2a 2a 20 62 75 74 20 6a  sabled,.** but j
2c37b 6f 69 6e 73 20 6d 69 67 68 74 20 72 75 6e 20 61  oins might run a
2c37c 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20   little slower. 
2c37d 20 54 68 65 20 74 72 69 63 6b 20 69 73 20 74 6f   The trick is to
2c37e 20 64 69 73 61 62 6c 65 20 61 73 20 6d 75 63 68   disable as much
2c37f 0a 2a 2a 20 61 73 20 77 65 20 63 61 6e 20 77 69  .** as we can wi
2c380 74 68 6f 75 74 20 64 69 73 61 62 6c 69 6e 67 20  thout disabling 
2c381 74 6f 6f 20 6d 75 63 68 2e 20 20 49 66 20 77 65  too much.  If we
2c382 20 64 69 73 61 62 6c 65 64 20 69 6e 20 28 31 29   disabled in (1)
2c383 2c 20 77 65 27 64 20 67 65 74 0a 2a 2a 20 74 68  , we'd get.** th
2c384 65 20 77 72 6f 6e 67 20 61 6e 73 77 65 72 2e 20  e wrong answer. 
2c385 20 53 65 65 20 74 69 63 6b 65 74 20 23 38 31 33   See ticket #813
2c386 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2c387 20 64 69 73 61 62 6c 65 54 65 72 6d 28 57 68 65   disableTerm(Whe
2c388 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c  reLevel *pLevel,
2c389 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2c38a 6d 29 7b 0a 20 20 69 66 28 20 70 54 65 72 6d 0a  m){.  if( pTerm.
2c38b 20 20 20 20 20 20 26 26 20 41 4c 57 41 59 53 28        && ALWAYS(
2c38c 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20  (pTerm->wtFlags 
2c38d 26 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30  & TERM_CODED)==0
2c38e 29 0a 20 20 20 20 20 20 26 26 20 28 70 4c 65 76  ).      && (pLev
2c38f 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 3d 3d 30  el->iLeftJoin==0
2c390 20 7c 7c 20 45 78 70 72 48 61 73 50 72 6f 70 65   || ExprHasPrope
2c391 72 74 79 28 70 54 65 72 6d 2d 3e 70 45 78 70 72  rty(pTerm->pExpr
2c392 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 29 0a  , EP_FromJoin)).
2c393 20 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e    ){.    pTerm->
2c394 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2c395 43 4f 44 45 44 3b 0a 20 20 20 20 69 66 28 20 70  CODED;.    if( p
2c396 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 3e 3d 30  Term->iParent>=0
2c397 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54   ){.      WhereT
2c398 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70  erm *pOther = &p
2c399 54 65 72 6d 2d 3e 70 57 43 2d 3e 61 5b 70 54 65  Term->pWC->a[pTe
2c39a 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20  rm->iParent];.  
2c39b 20 20 20 20 69 66 28 20 28 2d 2d 70 4f 74 68 65      if( (--pOthe
2c39c 72 2d 3e 6e 43 68 69 6c 64 29 3d 3d 30 20 29 7b  r->nChild)==0 ){
2c39d 0a 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65  .        disable
2c39e 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 4f 74  Term(pLevel, pOt
2c39f 68 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  her);.      }.  
2c3a0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2c3a1 20 41 70 70 6c 79 20 74 68 65 20 61 66 66 69 6e   Apply the affin
2c3a2 69 74 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  ities associated
2c3a3 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20   with the first 
2c3a4 6e 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 69 6e 64  n columns of ind
2c3a5 65 78 0a 2a 2a 20 70 49 64 78 20 74 6f 20 74 68  ex.** pIdx to th
2c3a6 65 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20  e values in the 
2c3a7 6e 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  n registers star
2c3a8 74 69 6e 67 20 61 74 20 62 61 73 65 2e 0a 2a 2f  ting at base..*/
2c3a9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
2c3aa 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 50  eApplyAffinity(P
2c3ab 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
2c3ac 74 20 62 61 73 65 2c 20 69 6e 74 20 6e 2c 20 49  t base, int n, I
2c3ad 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69  ndex *pIdx){.  i
2c3ae 66 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 56 64  f( n>0 ){.    Vd
2c3af 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2c3b0 70 56 64 62 65 3b 0a 20 20 20 20 61 73 73 65 72  pVdbe;.    asser
2c3b1 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 20 20 73  t( v!=0 );.    s
2c3b2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c3b3 28 76 2c 20 4f 50 5f 41 66 66 69 6e 69 74 79 2c  (v, OP_Affinity,
2c3b4 20 62 61 73 65 2c 20 6e 29 3b 0a 20 20 20 20 73   base, n);.    s
2c3b5 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e  qlite3IndexAffin
2c3b6 69 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b  ityStr(v, pIdx);
2c3b7 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2c3b8 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2c3b9 6e 67 65 28 70 50 61 72 73 65 2c 20 62 61 73 65  nge(pParse, base
2c3ba 2c 20 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  , n);.  }.}.../*
2c3bb 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2c3bc 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65  e for a single e
2c3bd 71 75 61 6c 69 74 79 20 74 65 72 6d 20 6f 66 20  quality term of 
2c3be 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2c3bf 2e 20 20 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a  .  An equality.*
2c3c0 2a 20 74 65 72 6d 20 63 61 6e 20 62 65 20 65 69  * term can be ei
2c3c1 74 68 65 72 20 58 3d 65 78 70 72 20 6f 72 20 58  ther X=expr or X
2c3c2 20 49 4e 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65   IN (...).   pTe
2c3c3 72 6d 20 69 73 20 74 68 65 20 74 65 72 6d 20 74  rm is the term t
2c3c4 6f 20 62 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a  o be .** coded..
2c3c5 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e  **.** The curren
2c3c6 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
2c3c7 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6c 65  constraint is le
2c3c8 66 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 69  ft in register i
2c3c9 52 65 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61  Reg..**.** For a
2c3ca 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 74   constraint of t
2c3cb 68 65 20 66 6f 72 6d 20 58 3d 65 78 70 72 2c 20  he form X=expr, 
2c3cc 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
2c3cd 73 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  s evaluated and 
2c3ce 69 74 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69 73  its.** result is
2c3cf 20 6c 65 66 74 20 6f 6e 20 74 68 65 20 73 74 61   left on the sta
2c3d0 63 6b 2e 20 20 46 6f 72 20 63 6f 6e 73 74 72 61  ck.  For constra
2c3d1 69 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ints of the form
2c3d2 20 58 20 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74   X IN (...).** t
2c3d3 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
2c3d4 20 75 70 20 61 20 6c 6f 6f 70 20 74 68 61 74 20   up a loop that 
2c3d5 77 69 6c 6c 20 69 74 65 72 61 74 65 20 6f 76 65  will iterate ove
2c3d6 72 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20  r all values of 
2c3d7 58 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  X..*/.static int
2c3d8 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72   codeEqualityTer
2c3d9 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  m(.  Parse *pPar
2c3da 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  se,      /* The 
2c3db 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
2c3dc 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
2c3dd 70 54 65 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20  pTerm,   /* The 
2c3de 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52  term of the WHER
2c3df 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 63  E clause to be c
2c3e0 6f 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c  oded */.  WhereL
2c3e1 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a  evel *pLevel, /*
2c3e2 20 57 68 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74   When level of t
2c3e3 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 77  he FROM clause w
2c3e4 65 20 61 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e  e are working on
2c3e5 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 72 67 65   */.  int iTarge
2c3e6 74 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74  t         /* Att
2c3e7 65 6d 70 74 20 74 6f 20 6c 65 61 76 65 20 72 65  empt to leave re
2c3e8 73 75 6c 74 73 20 69 6e 20 74 68 69 73 20 72 65  sults in this re
2c3e9 67 69 73 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 45  gister */.){.  E
2c3ea 78 70 72 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d  xpr *pX = pTerm-
2c3eb 3e 70 45 78 70 72 3b 0a 20 20 56 64 62 65 20 2a  >pExpr;.  Vdbe *
2c3ec 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2c3ed 65 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20  e;.  int iReg;  
2c3ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3ef 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
2c3f0 69 6e 67 20 72 65 73 75 6c 74 73 20 2a 2f 0a 0a  ing results */..
2c3f1 20 20 61 73 73 65 72 74 28 20 69 54 61 72 67 65    assert( iTarge
2c3f2 74 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 58 2d  t>0 );.  if( pX-
2c3f3 3e 6f 70 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20  >op==TK_EQ ){.  
2c3f4 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33    iReg = sqlite3
2c3f5 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
2c3f6 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68  Parse, pX->pRigh
2c3f7 74 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 7d  t, iTarget);.  }
2c3f8 65 6c 73 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d  else if( pX->op=
2c3f9 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20  =TK_ISNULL ){.  
2c3fa 20 20 69 52 65 67 20 3d 20 69 54 61 72 67 65 74    iReg = iTarget
2c3fb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2c3fc 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2c3fd 6c 6c 2c 20 30 2c 20 69 52 65 67 29 3b 0a 23 69  ll, 0, iReg);.#i
2c3fe 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2c3ff 54 5f 53 55 42 51 55 45 52 59 0a 20 20 7d 65 6c  T_SUBQUERY.  }el
2c400 73 65 7b 0a 20 20 20 20 69 6e 74 20 65 54 79 70  se{.    int eTyp
2c401 65 3b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 3b  e;.    int iTab;
2c402 0a 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f  .    struct InLo
2c403 6f 70 20 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73  op *pIn;..    as
2c404 73 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b  sert( pX->op==TK
2c405 5f 49 4e 20 29 3b 0a 20 20 20 20 69 52 65 67 20  _IN );.    iReg 
2c406 3d 20 69 54 61 72 67 65 74 3b 0a 20 20 20 20 65  = iTarget;.    e
2c407 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69  Type = sqlite3Fi
2c408 6e 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65  ndInIndex(pParse
2c409 2c 20 70 58 2c 20 30 29 3b 0a 20 20 20 20 69 54  , pX, 0);.    iT
2c40a 61 62 20 3d 20 70 58 2d 3e 69 54 61 62 6c 65 3b  ab = pX->iTable;
2c40b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2c40c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
2c40d 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20  ind, iTab, 0);. 
2c40e 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2c40f 76 2c 20 22 25 2e 2a 73 22 2c 20 70 58 2d 3e 73  v, "%.*s", pX->s
2c410 70 61 6e 2e 6e 2c 20 70 58 2d 3e 73 70 61 6e 2e  pan.n, pX->span.
2c411 7a 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  z));.    assert(
2c412 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2c413 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
2c414 5f 41 42 4c 45 20 29 3b 0a 20 20 20 20 69 66 28  _ABLE );.    if(
2c415 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
2c416 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c  n==0 ){.      pL
2c417 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20  evel->addrNxt = 
2c418 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2c419 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 7d 0a 20  abel(v);.    }. 
2c41a 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e     pLevel->u.in.
2c41b 6e 49 6e 2b 2b 3b 0a 20 20 20 20 70 4c 65 76 65  nIn++;.    pLeve
2c41c 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 20  l->u.in.aInLoop 
2c41d 3d 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  =.       sqlite3
2c41e 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
2c41f 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 65 76  pParse->db, pLev
2c420 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70  el->u.in.aInLoop
2c421 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2c422 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c423 73 69 7a 65 6f 66 28 70 4c 65 76 65 6c 2d 3e 75  sizeof(pLevel->u
2c424 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 5b 30 5d 29 2a  .in.aInLoop[0])*
2c425 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e  pLevel->u.in.nIn
2c426 29 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 4c 65  );.    pIn = pLe
2c427 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f  vel->u.in.aInLoo
2c428 70 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 20 29  p;.    if( pIn )
2c429 7b 0a 20 20 20 20 20 20 70 49 6e 20 2b 3d 20 70  {.      pIn += p
2c42a 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 20  Level->u.in.nIn 
2c42b 2d 20 31 3b 0a 20 20 20 20 20 20 70 49 6e 2d 3e  - 1;.      pIn->
2c42c 69 43 75 72 20 3d 20 69 54 61 62 3b 0a 20 20 20  iCur = iTab;.   
2c42d 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e     if( eType==IN
2c42e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a  _INDEX_ROWID ){.
2c42f 20 20 20 20 20 20 20 20 70 49 6e 2d 3e 61 64 64          pIn->add
2c430 72 49 6e 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  rInTop = sqlite3
2c431 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c432 5f 52 6f 77 69 64 2c 20 69 54 61 62 2c 20 69 52  _Rowid, iTab, iR
2c433 65 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  eg);.      }else
2c434 7b 0a 20 20 20 20 20 20 20 20 70 49 6e 2d 3e 61  {.        pIn->a
2c435 64 64 72 49 6e 54 6f 70 20 3d 20 73 71 6c 69 74  ddrInTop = sqlit
2c436 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2c437 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c  OP_Column, iTab,
2c438 20 30 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20   0, iReg);.     
2c439 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2c43a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2c43b 5f 49 73 4e 75 6c 6c 2c 20 69 52 65 67 29 3b 0a  _IsNull, iReg);.
2c43c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2c43d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
2c43e 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 65 6e  n = 0;.    }.#en
2c43f 64 69 66 0a 20 20 7d 0a 20 20 64 69 73 61 62 6c  dif.  }.  disabl
2c440 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54  eTerm(pLevel, pT
2c441 65 72 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  erm);.  return i
2c442 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Reg;.}../*.** Ge
2c443 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2c444 20 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 61   will evaluate a
2c445 6c 6c 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e  ll == and IN con
2c446 73 74 72 61 69 6e 74 73 20 66 6f 72 20 61 6e 0a  straints for an.
2c447 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65 20 76  ** index.  The v
2c448 61 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f  alues for all co
2c449 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 6c 65  nstraints are le
2c44a 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e  ft on the stack.
2c44b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
2c44c 6c 65 2c 20 63 6f 6e 73 69 64 65 72 20 74 61 62  le, consider tab
2c44d 6c 65 20 74 31 28 61 2c 62 2c 63 2c 64 2c 65 2c  le t1(a,b,c,d,e,
2c44e 66 29 20 77 69 74 68 20 69 6e 64 65 78 20 69 31  f) with index i1
2c44f 28 61 2c 62 2c 63 29 2e 0a 2a 2a 20 53 75 70 70  (a,b,c)..** Supp
2c450 6f 73 65 20 74 68 65 20 57 48 45 52 45 20 63 6c  ose the WHERE cl
2c451 61 75 73 65 20 69 73 20 74 68 69 73 3a 20 20 61  ause is this:  a
2c452 3d 3d 35 20 41 4e 44 20 62 20 49 4e 20 28 31 2c  ==5 AND b IN (1,
2c453 32 2c 33 29 20 41 4e 44 20 63 3e 35 20 41 4e 44  2,3) AND c>5 AND
2c454 20 63 3c 31 30 0a 2a 2a 20 54 68 65 20 69 6e 64   c<10.** The ind
2c455 65 78 20 68 61 73 20 61 73 20 6d 61 6e 79 20 61  ex has as many a
2c456 73 20 74 68 72 65 65 20 65 71 75 61 6c 69 74 79  s three equality
2c457 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 62 75   constraints, bu
2c458 74 20 69 6e 20 74 68 69 73 0a 2a 2a 20 65 78 61  t in this.** exa
2c459 6d 70 6c 65 2c 20 74 68 65 20 74 68 69 72 64 20  mple, the third 
2c45a 22 63 22 20 76 61 6c 75 65 20 69 73 20 61 6e 20  "c" value is an 
2c45b 69 6e 65 71 75 61 6c 69 74 79 2e 20 20 53 6f 20  inequality.  So 
2c45c 6f 6e 6c 79 20 74 77 6f 20 0a 2a 2a 20 63 6f 6e  only two .** con
2c45d 73 74 72 61 69 6e 74 73 20 61 72 65 20 63 6f 64  straints are cod
2c45e 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ed.  This routin
2c45f 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 20  e will generate 
2c460 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65  code to evaluate
2c461 0a 2a 2a 20 61 3d 3d 35 20 61 6e 64 20 62 20 49  .** a==5 and b I
2c462 4e 20 28 31 2c 32 2c 33 29 2e 20 20 54 68 65 20  N (1,2,3).  The 
2c463 63 75 72 72 65 6e 74 20 76 61 6c 75 65 73 20 66  current values f
2c464 6f 72 20 61 20 61 6e 64 20 62 20 77 69 6c 6c 20  or a and b will 
2c465 62 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  be stored.** in 
2c466 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69  consecutive regi
2c467 73 74 65 72 73 20 61 6e 64 20 74 68 65 20 69 6e  sters and the in
2c468 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73 74  dex of the first
2c469 20 72 65 67 69 73 74 65 72 20 69 73 20 72 65 74   register is ret
2c46a 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  urned..**.** In 
2c46b 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76  the example abov
2c46c 65 20 6e 45 71 3d 3d 32 2e 20 20 42 75 74 20 74  e nEq==2.  But t
2c46d 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 20 77  his subroutine w
2c46e 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 76 61 6c  orks for any val
2c46f 75 65 0a 2a 2a 20 6f 66 20 6e 45 71 20 69 6e 63  ue.** of nEq inc
2c470 6c 75 64 69 6e 67 20 30 2e 20 20 49 66 20 6e 45  luding 0.  If nE
2c471 71 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69  q==0, this routi
2c472 6e 65 20 69 73 20 6e 65 61 72 6c 79 20 61 20 6e  ne is nearly a n
2c473 6f 2d 6f 70 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c  o-op..** The onl
2c474 79 20 74 68 69 6e 67 20 69 74 20 64 6f 65 73 20  y thing it does 
2c475 69 73 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  is allocate the 
2c476 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 6d 65 6d  pLevel->iMem mem
2c477 6f 72 79 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ory cell..**.** 
2c478 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77  This routine alw
2c479 61 79 73 20 61 6c 6c 6f 63 61 74 65 73 20 61 74  ays allocates at
2c47a 20 6c 65 61 73 74 20 6f 6e 65 20 6d 65 6d 6f 72   least one memor
2c47b 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 74 75 72  y cell and retur
2c47c 6e 73 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  ns.** the index 
2c47d 6f 66 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63  of that memory c
2c47e 65 6c 6c 2e 20 54 68 65 20 63 6f 64 65 20 74 68  ell. The code th
2c47f 61 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 68 69 73  at.** calls this
2c480 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 75 73   routine will us
2c481 65 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65  e that memory ce
2c482 6c 6c 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ll to store the 
2c483 74 65 72 6d 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6b  termination.** k
2c484 65 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ey value of the 
2c485 6c 6f 6f 70 2e 20 20 49 66 20 6f 6e 65 20 6f 72  loop.  If one or
2c486 20 6d 6f 72 65 20 49 4e 20 6f 70 65 72 61 74 6f   more IN operato
2c487 72 73 20 61 70 70 65 61 72 2c 20 74 68 65 6e 0a  rs appear, then.
2c488 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
2c489 61 6c 6c 6f 63 61 74 65 73 20 61 6e 20 61 64 64  allocates an add
2c48a 69 74 69 6f 6e 61 6c 20 6e 45 71 20 6d 65 6d 6f  itional nEq memo
2c48b 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 69 6e 74  ry cells for int
2c48c 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 2e 0a 2a 2f  ernal.** use..*/
2c48d 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65  .static int code
2c48e 41 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73  AllEqualityTerms
2c48f 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2c490 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
2c491 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2c492 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c    WhereLevel *pL
2c493 65 76 65 6c 2c 20 20 20 2f 2a 20 57 68 69 63 68  evel,   /* Which
2c494 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20   nested loop of 
2c495 74 68 65 20 46 52 4f 4d 20 77 65 20 61 72 65 20  the FROM we are 
2c496 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 57 68 65 72  coding */.  Wher
2c497 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2c498 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
2c499 6c 61 75 73 65 20 2a 2f 0a 20 20 42 69 74 6d 61  lause */.  Bitma
2c49a 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20  sk notReady,    
2c49b 20 2f 2a 20 57 68 69 63 68 20 70 61 72 74 73 20   /* Which parts 
2c49c 6f 66 20 46 52 4f 4d 20 68 61 76 65 20 6e 6f 74  of FROM have not
2c49d 20 79 65 74 20 62 65 65 6e 20 63 6f 64 65 64 20   yet been coded 
2c49e 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 52  */.  int nExtraR
2c49f 65 67 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  eg         /* Nu
2c4a0 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 72 65  mber of extra re
2c4a1 67 69 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63  gisters to alloc
2c4a2 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ate */.){.  int 
2c4a3 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c  nEq = pLevel->pl
2c4a4 61 6e 2e 6e 45 71 3b 20 20 20 2f 2a 20 54 68 65  an.nEq;   /* The
2c4a5 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72   number of == or
2c4a6 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   IN constraints 
2c4a7 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 56 64 62  to code */.  Vdb
2c4a8 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
2c4a9 56 64 62 65 3b 20 20 20 20 20 20 2f 2a 20 54 68  Vdbe;      /* Th
2c4aa 65 20 76 6d 20 75 6e 64 65 72 20 63 6f 6e 73 74  e vm under const
2c4ab 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e 64  ruction */.  Ind
2c4ac 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
2c4ad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2c4ae 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 75 73  e index being us
2c4af 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 6f 70  ed for this loop
2c4b0 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20 3d   */.  int iCur =
2c4b1 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72   pLevel->iTabCur
2c4b2 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f  ;   /* The curso
2c4b3 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  r of the table *
2c4b4 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  /.  WhereTerm *p
2c4b5 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
2c4b6 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 6f    /* A single co
2c4b7 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 20 2a 2f  nstraint term */
2c4b8 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20  .  int j;       
2c4b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4ba 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2c4bb 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 42 61 73   */.  int regBas
2c4bc 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
2c4bd 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69      /* Base regi
2c4be 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  ster */.  int nR
2c4bf 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  eg;             
2c4c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2c4c1 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 74  r of registers t
2c4c2 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 0a 20  o allocate */.. 
2c4c3 20 2f 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20   /* This module 
2c4c4 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 6f  is only called o
2c4c5 6e 20 71 75 65 72 79 20 70 6c 61 6e 73 20 74 68  n query plans th
2c4c6 61 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2e  at use an index.
2c4c7 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4c   */.  assert( pL
2c4c8 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2c4c9 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58  gs & WHERE_INDEX
2c4ca 45 44 20 29 3b 0a 20 20 70 49 64 78 20 3d 20 70  ED );.  pIdx = p
2c4cb 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49  Level->plan.u.pI
2c4cc 64 78 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65  dx;..  /* Figure
2c4cd 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6d 65   out how many me
2c4ce 6d 6f 72 79 20 63 65 6c 6c 73 20 77 65 20 77 69  mory cells we wi
2c4cf 6c 6c 20 6e 65 65 64 20 74 68 65 6e 20 61 6c 6c  ll need then all
2c4d0 6f 63 61 74 65 20 74 68 65 6d 2e 0a 20 20 2a 2f  ocate them..  */
2c4d1 0a 20 20 72 65 67 42 61 73 65 20 3d 20 70 50 61  .  regBase = pPa
2c4d2 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20  rse->nMem + 1;. 
2c4d3 20 6e 52 65 67 20 3d 20 70 4c 65 76 65 6c 2d 3e   nReg = pLevel->
2c4d4 70 6c 61 6e 2e 6e 45 71 20 2b 20 6e 45 78 74 72  plan.nEq + nExtr
2c4d5 61 52 65 67 3b 0a 20 20 70 50 61 72 73 65 2d 3e  aReg;.  pParse->
2c4d6 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a 0a 20  nMem += nReg;.. 
2c4d7 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65   /* Evaluate the
2c4d8 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
2c4d9 61 69 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73  aints.  */.  ass
2c4da 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  ert( pIdx->nColu
2c4db 6d 6e 3e 3d 6e 45 71 20 29 3b 0a 20 20 66 6f 72  mn>=nEq );.  for
2c4dc 28 6a 3d 30 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b  (j=0; j<nEq; j++
2c4dd 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 3b 0a 20  ){.    int r1;. 
2c4de 20 20 20 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d     int k = pIdx-
2c4df 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20  >aiColumn[j];.  
2c4e0 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65    pTerm = findTe
2c4e1 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c  rm(pWC, iCur, k,
2c4e2 20 6e 6f 74 52 65 61 64 79 2c 20 70 4c 65 76 65   notReady, pLeve
2c4e3 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2c  l->plan.wsFlags,
2c4e4 20 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20   pIdx);.    if( 
2c4e5 4e 45 56 45 52 28 70 54 65 72 6d 3d 3d 30 29 20  NEVER(pTerm==0) 
2c4e6 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73 73  ) break;.    ass
2c4e7 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46  ert( (pTerm->wtF
2c4e8 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45  lags & TERM_CODE
2c4e9 44 29 3d 3d 30 20 29 3b 0a 20 20 20 20 72 31 20  D)==0 );.    r1 
2c4ea 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65  = codeEqualityTe
2c4eb 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d  rm(pParse, pTerm
2c4ec 2c 20 70 4c 65 76 65 6c 2c 20 72 65 67 42 61 73  , pLevel, regBas
2c4ed 65 2b 6a 29 3b 0a 20 20 20 20 69 66 28 20 72 31  e+j);.    if( r1
2c4ee 21 3d 72 65 67 42 61 73 65 2b 6a 20 29 7b 0a 20  !=regBase+j ){. 
2c4ef 20 20 20 20 20 69 66 28 20 6e 52 65 67 3d 3d 31       if( nReg==1
2c4f0 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2c4f1 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2c4f2 67 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  g(pParse, regBas
2c4f3 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 67 42  e);.        regB
2c4f4 61 73 65 20 3d 20 72 31 3b 0a 20 20 20 20 20 20  ase = r1;.      
2c4f5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2c4f6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c4f7 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 31  (v, OP_SCopy, r1
2c4f8 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20  , regBase+j);.  
2c4f9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2c4fa 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
2c4fb 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
2c4fc 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 74 65  ISNULL );.    te
2c4fd 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65  stcase( pTerm->e
2c4fe 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e  Operator & WO_IN
2c4ff 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 65   );.    if( (pTe
2c500 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2c501 28 57 4f 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e  (WO_ISNULL|WO_IN
2c502 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ))==0 ){.      s
2c503 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c504 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72  (v, OP_IsNull, r
2c505 65 67 42 61 73 65 2b 6a 2c 20 70 4c 65 76 65 6c  egBase+j, pLevel
2c506 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20  ->addrBrk);.    
2c507 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
2c508 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  egBase;.}../*.**
2c509 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
2c50a 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  or the start of 
2c50b 74 68 65 20 69 4c 65 76 65 6c 2d 74 68 20 6c 6f  the iLevel-th lo
2c50c 6f 70 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  op in the WHERE 
2c50d 63 6c 61 75 73 65 0a 2a 2a 20 69 6d 70 6c 65 6d  clause.** implem
2c50e 65 6e 74 61 74 69 6f 6e 20 64 65 73 63 72 69 62  entation describ
2c50f 65 64 20 62 79 20 70 57 49 6e 66 6f 2e 0a 2a 2f  ed by pWInfo..*/
2c510 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20  .static Bitmask 
2c511 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72 74  codeOneLoopStart
2c512 28 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  (.  WhereInfo *p
2c513 57 49 6e 66 6f 2c 20 20 20 2f 2a 20 43 6f 6d 70  WInfo,   /* Comp
2c514 6c 65 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  lete information
2c515 20 61 62 6f 75 74 20 74 68 65 20 57 48 45 52 45   about the WHERE
2c516 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
2c517 20 69 4c 65 76 65 6c 2c 20 20 20 20 20 20 20 20   iLevel,        
2c518 20 20 2f 2a 20 57 68 69 63 68 20 6c 65 76 65 6c    /* Which level
2c519 20 6f 66 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 20   of pWInfo->a[] 
2c51a 73 68 6f 75 6c 64 20 62 65 20 63 6f 64 65 64 20  should be coded 
2c51b 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c 46 6c  */.  u16 wctrlFl
2c51c 61 67 73 2c 20 20 20 20 20 20 2f 2a 20 4f 6e 65  ags,      /* One
2c51d 20 6f 66 20 74 68 65 20 57 48 45 52 45 5f 2a 20   of the WHERE_* 
2c51e 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20 69 6e  flags defined in
2c51f 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f 0a   sqliteInt.h */.
2c520 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
2c521 64 79 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20  dy     /* Which 
2c522 74 61 62 6c 65 73 20 61 72 65 20 63 75 72 72 65  tables are curre
2c523 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 2a  ntly available *
2c524 2f 0a 29 7b 0a 20 20 69 6e 74 20 6a 2c 20 6b 3b  /.){.  int j, k;
2c525 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2c526 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
2c527 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
2c528 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
2c529 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  BE cursor for th
2c52a 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
2c52b 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20   addrNxt;       
2c52c 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 6a 75    /* Where to ju
2c52d 6d 70 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77  mp to continue w
2c52e 69 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20  ith the next IN 
2c52f 63 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d  case */.  int om
2c530 69 74 54 61 62 6c 65 3b 20 20 20 20 20 20 20 2f  itTable;       /
2c531 2a 20 54 72 75 65 20 69 66 20 77 65 20 75 73 65  * True if we use
2c532 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20   the index only 
2c533 2a 2f 0a 20 20 69 6e 74 20 62 52 65 76 3b 20 20  */.  int bRev;  
2c534 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
2c535 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20  e if we need to 
2c536 73 63 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20  scan in reverse 
2c537 6f 72 64 65 72 20 2a 2f 0a 20 20 57 68 65 72 65  order */.  Where
2c538 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20  Level *pLevel;  
2c539 2f 2a 20 54 68 65 20 77 68 65 72 65 20 6c 65 76  /* The where lev
2c53a 65 6c 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a  el to be coded *
2c53b 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2c53c 2a 70 57 43 3b 20 20 20 20 2f 2a 20 44 65 63 6f  *pWC;    /* Deco
2c53d 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65  mposition of the
2c53e 20 65 6e 74 69 72 65 20 57 48 45 52 45 20 63 6c   entire WHERE cl
2c53f 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 54  ause */.  WhereT
2c540 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20  erm *pTerm;     
2c541 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 57            /* A W
2c542 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
2c543 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
2c544 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rse;            
2c545 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2c546 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 56 64   context */.  Vd
2c547 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
2c548 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c549 20 54 68 65 20 70 72 65 70 61 72 65 64 20 73 74   The prepared st
2c54a 6d 74 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  mt under constru
2c54b 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75  ctions */.  stru
2c54c 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2c54d 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20 46  *pTabItem;  /* F
2c54e 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20  ROM clause term 
2c54f 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20  being coded */. 
2c550 20 69 6e 74 20 61 64 64 72 42 72 6b 3b 20 20 20   int addrBrk;   
2c551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c552 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
2c553 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68   break out of th
2c554 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  e loop */.  int 
2c555 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 20 20  addrCont;       
2c556 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a              /* J
2c557 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74  ump here to cont
2c558 69 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 63  inue with next c
2c559 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52  ycle */.  int iR
2c55a 6f 77 69 64 52 65 67 20 3d 20 30 3b 20 20 20 20  owidReg = 0;    
2c55b 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 69 73 20      /* Rowid is 
2c55c 73 74 6f 72 65 64 20 69 6e 20 74 68 69 73 20 72  stored in this r
2c55d 65 67 69 73 74 65 72 2c 20 69 66 20 6e 6f 74 20  egister, if not 
2c55e 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 52  zero */.  int iR
2c55f 65 6c 65 61 73 65 52 65 67 20 3d 20 30 3b 20 20  eleaseReg = 0;  
2c560 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67 69      /* Temp regi
2c561 73 74 65 72 20 74 6f 20 66 72 65 65 20 62 65 66  ster to free bef
2c562 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 2a 2f  ore returning */
2c563 0a 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 49  ..  pParse = pWI
2c564 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 76  nfo->pParse;.  v
2c565 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2c566 3b 0a 20 20 70 57 43 20 3d 20 70 57 49 6e 66 6f  ;.  pWC = pWInfo
2c567 2d 3e 70 57 43 3b 0a 20 20 70 4c 65 76 65 6c 20  ->pWC;.  pLevel 
2c568 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 4c 65  = &pWInfo->a[iLe
2c569 76 65 6c 5d 3b 0a 20 20 70 54 61 62 49 74 65 6d  vel];.  pTabItem
2c56a 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 70 54 61 62   = &pWInfo->pTab
2c56b 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e  List->a[pLevel->
2c56c 69 46 72 6f 6d 5d 3b 0a 20 20 69 43 75 72 20 3d  iFrom];.  iCur =
2c56d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
2c56e 6f 72 3b 0a 20 20 62 52 65 76 20 3d 20 28 70 4c  or;.  bRev = (pL
2c56f 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2c570 67 73 20 26 20 57 48 45 52 45 5f 52 45 56 45 52  gs & WHERE_REVER
2c571 53 45 29 21 3d 30 3b 0a 20 20 6f 6d 69 74 54 61  SE)!=0;.  omitTa
2c572 62 6c 65 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 70  ble = (pLevel->p
2c573 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2c574 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 21 3d 30  ERE_IDX_ONLY)!=0
2c575 20 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20   .           && 
2c576 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48  (wctrlFlags & WH
2c577 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 29  ERE_FORCE_TABLE)
2c578 3d 3d 30 3b 0a 0a 20 20 2f 2a 20 43 72 65 61 74  ==0;..  /* Creat
2c579 65 20 6c 61 62 65 6c 73 20 66 6f 72 20 74 68 65  e labels for the
2c57a 20 22 62 72 65 61 6b 22 20 61 6e 64 20 22 63 6f   "break" and "co
2c57b 6e 74 69 6e 75 65 22 20 69 6e 73 74 72 75 63 74  ntinue" instruct
2c57c 69 6f 6e 73 0a 20 20 2a 2a 20 66 6f 72 20 74 68  ions.  ** for th
2c57d 65 20 63 75 72 72 65 6e 74 20 6c 6f 6f 70 2e 20  e current loop. 
2c57e 20 4a 75 6d 70 20 74 6f 20 61 64 64 72 42 72 6b   Jump to addrBrk
2c57f 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
2c580 20 61 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 4a 75   a loop..  ** Ju
2c581 6d 70 20 74 6f 20 63 6f 6e 74 20 74 6f 20 67 6f  mp to cont to go
2c582 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
2c583 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69  the next iterati
2c584 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c  on of the.  ** l
2c585 6f 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  oop..  **.  ** W
2c586 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 6e 20  hen there is an 
2c587 49 4e 20 6f 70 65 72 61 74 6f 72 2c 20 77 65 20  IN operator, we 
2c588 61 6c 73 6f 20 68 61 76 65 20 61 20 22 61 64 64  also have a "add
2c589 72 4e 78 74 22 20 6c 61 62 65 6c 20 74 68 61 74  rNxt" label that
2c58a 0a 20 20 2a 2a 20 6d 65 61 6e 73 20 74 6f 20 63  .  ** means to c
2c58b 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65  ontinue with the
2c58c 20 6e 65 78 74 20 49 4e 20 76 61 6c 75 65 20 63   next IN value c
2c58d 6f 6d 62 69 6e 61 74 69 6f 6e 2e 20 20 57 68 65  ombination.  Whe
2c58e 6e 0a 20 20 2a 2a 20 74 68 65 72 65 20 61 72 65  n.  ** there are
2c58f 20 6e 6f 20 49 4e 20 6f 70 65 72 61 74 6f 72 73   no IN operators
2c590 20 69 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69   in the constrai
2c591 6e 74 73 2c 20 74 68 65 20 22 61 64 64 72 4e 78  nts, the "addrNx
2c592 74 22 20 6c 61 62 65 6c 0a 20 20 2a 2a 20 69 73  t" label.  ** is
2c593 20 74 68 65 20 73 61 6d 65 20 61 73 20 22 61 64   the same as "ad
2c594 64 72 42 72 6b 22 2e 0a 20 20 2a 2f 0a 20 20 61  drBrk"..  */.  a
2c595 64 64 72 42 72 6b 20 3d 20 70 4c 65 76 65 6c 2d  ddrBrk = pLevel-
2c596 3e 61 64 64 72 42 72 6b 20 3d 20 70 4c 65 76 65  >addrBrk = pLeve
2c597 6c 2d 3e 61 64 64 72 4e 78 74 20 3d 20 73 71 6c  l->addrNxt = sql
2c598 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2c599 6c 28 76 29 3b 0a 20 20 61 64 64 72 43 6f 6e 74  l(v);.  addrCont
2c59a 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 43   = pLevel->addrC
2c59b 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ont = sqlite3Vdb
2c59c 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a  eMakeLabel(v);..
2c59d 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
2c59e 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
2c59f 6f 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20  of a LEFT OUTER 
2c5a0 4a 4f 49 4e 2c 20 61 6c 6c 6f 63 61 74 65 20 61  JOIN, allocate a
2c5a1 6e 64 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69  nd.  ** initiali
2c5a2 7a 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  ze a memory cell
2c5a3 20 74 68 61 74 20 72 65 63 6f 72 64 73 20 69 66   that records if
2c5a4 20 74 68 69 73 20 74 61 62 6c 65 20 6d 61 74 63   this table matc
2c5a5 68 65 73 20 61 6e 79 0a 20 20 2a 2a 20 72 6f 77  hes any.  ** row
2c5a6 20 6f 66 20 74 68 65 20 6c 65 66 74 20 74 61 62   of the left tab
2c5a7 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a  le of the join..
2c5a8 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 65 76 65    */.  if( pLeve
2c5a9 6c 2d 3e 69 46 72 6f 6d 3e 30 20 26 26 20 28 70  l->iFrom>0 && (p
2c5aa 54 61 62 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74  TabItem[0].joint
2c5ab 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d  ype & JT_LEFT)!=
2c5ac 30 20 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  0 ){.    pLevel-
2c5ad 3e 69 4c 65 66 74 4a 6f 69 6e 20 3d 20 2b 2b 70  >iLeftJoin = ++p
2c5ae 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2c5af 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c5b0 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2c5b1 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65  , 0, pLevel->iLe
2c5b2 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 56 64 62  ftJoin);.    Vdb
2c5b3 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e  eComment((v, "in
2c5b4 69 74 20 4c 45 46 54 20 4a 4f 49 4e 20 6e 6f 2d  it LEFT JOIN no-
2c5b5 6d 61 74 63 68 20 66 6c 61 67 22 29 29 3b 0a 20  match flag"));. 
2c5b6 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
2c5b7 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2c5b8 41 42 4c 45 0a 20 20 69 66 28 20 20 28 70 4c 65  ABLE.  if(  (pLe
2c5b9 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2c5ba 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 41  s & WHERE_VIRTUA
2c5bb 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20  LTABLE)!=0 ){.  
2c5bc 20 20 2f 2a 20 43 61 73 65 20 30 3a 20 20 54 68    /* Case 0:  Th
2c5bd 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72  e table is a vir
2c5be 74 75 61 6c 2d 74 61 62 6c 65 2e 20 20 55 73 65  tual-table.  Use
2c5bf 20 74 68 65 20 56 46 69 6c 74 65 72 20 61 6e 64   the VFilter and
2c5c0 20 56 4e 65 78 74 0a 20 20 20 20 2a 2a 20 20 20   VNext.    **   
2c5c1 20 20 20 20 20 20 20 74 6f 20 61 63 63 65 73 73         to access
2c5c2 20 74 68 65 20 64 61 74 61 2e 0a 20 20 20 20 2a   the data..    *
2c5c3 2f 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20  /.    int iReg; 
2c5c4 20 20 2f 2a 20 50 33 20 56 61 6c 75 65 20 66 6f    /* P3 Value fo
2c5c5 72 20 4f 50 5f 56 46 69 6c 74 65 72 20 2a 2f 0a  r OP_VFilter */.
2c5c6 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65      sqlite3_inde
2c5c7 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78  x_info *pVtabIdx
2c5c8 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
2c5c9 75 2e 70 56 74 61 62 49 64 78 3b 0a 20 20 20 20  u.pVtabIdx;.    
2c5ca 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20  int nConstraint 
2c5cb 3d 20 70 56 74 61 62 49 64 78 2d 3e 6e 43 6f 6e  = pVtabIdx->nCon
2c5cc 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 73 74 72  straint;.    str
2c5cd 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2c5ce 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
2c5cf 67 65 20 2a 61 55 73 61 67 65 20 3d 0a 20 20 20  ge *aUsage =.   
2c5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c5d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c5d2 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56 74               pVt
2c5d3 61 62 49 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69  abIdx->aConstrai
2c5d4 6e 74 55 73 61 67 65 3b 0a 20 20 20 20 63 6f 6e  ntUsage;.    con
2c5d5 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  st struct sqlite
2c5d6 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2c5d7 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 20  nt *aConstraint 
2c5d8 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
2c5d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c5da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c5db 20 20 70 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e    pVtabIdx->aCon
2c5dc 73 74 72 61 69 6e 74 3b 0a 0a 20 20 20 20 69 52  straint;..    iR
2c5dd 65 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  eg = sqlite3GetT
2c5de 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2c5df 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b   nConstraint+2);
2c5e0 0a 20 20 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c  .    for(j=1; j<
2c5e1 3d 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b  =nConstraint; j+
2c5e2 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d  +){.      for(k=
2c5e3 30 3b 20 6b 3c 6e 43 6f 6e 73 74 72 61 69 6e 74  0; k<nConstraint
2c5e4 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; k++){.        
2c5e5 69 66 28 20 61 55 73 61 67 65 5b 6b 5d 2e 61 72  if( aUsage[k].ar
2c5e6 67 76 49 6e 64 65 78 3d 3d 6a 20 29 7b 0a 20 20  gvIndex==j ){.  
2c5e7 20 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72          int iTer
2c5e8 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  m = aConstraint[
2c5e9 6b 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a  k].iTermOffset;.
2c5ea 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2c5eb 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
2c5ec 2c 20 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 2e  , pWC->a[iTerm].
2c5ed 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69  pExpr->pRight, i
2c5ee 52 65 67 2b 6a 2b 31 29 3b 0a 20 20 20 20 20 20  Reg+j+1);.      
2c5ef 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2c5f0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2c5f1 20 20 20 69 66 28 20 6b 3d 3d 6e 43 6f 6e 73 74     if( k==nConst
2c5f2 72 61 69 6e 74 20 29 20 62 72 65 61 6b 3b 0a 20  raint ) break;. 
2c5f3 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2c5f4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c5f5 5f 49 6e 74 65 67 65 72 2c 20 70 56 74 61 62 49  _Integer, pVtabI
2c5f6 64 78 2d 3e 69 64 78 4e 75 6d 2c 20 69 52 65 67  dx->idxNum, iReg
2c5f7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2c5f8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2c5f9 6e 74 65 67 65 72 2c 20 6a 2d 31 2c 20 69 52 65  nteger, j-1, iRe
2c5fa 67 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  g+1);.    sqlite
2c5fb 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2c5fc 50 5f 56 46 69 6c 74 65 72 2c 20 69 43 75 72 2c  P_VFilter, iCur,
2c5fd 20 61 64 64 72 42 72 6b 2c 20 69 52 65 67 2c 20   addrBrk, iReg, 
2c5fe 70 56 74 61 62 49 64 78 2d 3e 69 64 78 53 74 72  pVtabIdx->idxStr
2c5ff 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2c600 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78          pVtabIdx
2c601 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
2c602 74 72 20 3f 20 50 34 5f 4d 50 52 49 4e 54 46 20  tr ? P4_MPRINTF 
2c603 3a 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  : P4_STATIC);.  
2c604 20 20 70 56 74 61 62 49 64 78 2d 3e 6e 65 65 64    pVtabIdx->need
2c605 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d 20 30  ToFreeIdxStr = 0
2c606 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
2c607 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6a 2b  <nConstraint; j+
2c608 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 55  +){.      if( aU
2c609 73 61 67 65 5b 6a 5d 2e 6f 6d 69 74 20 29 7b 0a  sage[j].omit ){.
2c60a 20 20 20 20 20 20 20 20 69 6e 74 20 69 54 65 72          int iTer
2c60b 6d 20 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  m = aConstraint[
2c60c 6a 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a  j].iTermOffset;.
2c60d 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54          disableT
2c60e 65 72 6d 28 70 4c 65 76 65 6c 2c 20 26 70 57 43  erm(pLevel, &pWC
2c60f 2d 3e 61 5b 69 54 65 72 6d 5d 29 3b 0a 20 20 20  ->a[iTerm]);.   
2c610 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
2c611 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 56  Level->op = OP_V
2c612 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c  Next;.    pLevel
2c613 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20  ->p1 = iCur;.   
2c614 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 71   pLevel->p2 = sq
2c615 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2c616 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c  Addr(v);.    sql
2c617 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2c618 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 52 65  ange(pParse, iRe
2c619 67 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 32  g, nConstraint+2
2c61a 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
2c61b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2c61c 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
2c61d 0a 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  ..  if( pLevel->
2c61e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2c61f 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 7b  HERE_ROWID_EQ ){
2c620 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20  .    /* Case 1: 
2c621 20 57 65 20 63 61 6e 20 64 69 72 65 63 74 6c 79   We can directly
2c622 20 72 65 66 65 72 65 6e 63 65 20 61 20 73 69 6e   reference a sin
2c623 67 6c 65 20 72 6f 77 20 75 73 69 6e 67 20 61 6e  gle row using an
2c624 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2c625 20 65 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72   equality compar
2c626 69 73 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65  ison against the
2c627 20 52 4f 57 49 44 20 66 69 65 6c 64 2e 20 20 4f   ROWID field.  O
2c628 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  r.    **        
2c629 20 20 77 65 20 72 65 66 65 72 65 6e 63 65 20 6d    we reference m
2c62a 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 75 73 69  ultiple rows usi
2c62b 6e 67 20 61 20 22 72 6f 77 69 64 20 49 4e 20 28  ng a "rowid IN (
2c62c 2e 2e 2e 29 22 0a 20 20 20 20 2a 2a 20 20 20 20  ...)".    **    
2c62d 20 20 20 20 20 20 63 6f 6e 73 74 72 75 63 74 2e        construct.
2c62e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 52 65 6c  .    */.    iRel
2c62f 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74 65  easeReg = sqlite
2c630 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2c631 73 65 29 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d  se);.    pTerm =
2c632 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2c633 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64  Cur, -1, notRead
2c634 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c 20  y, WO_EQ|WO_IN, 
2c635 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
2c636 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20  pTerm!=0 );.    
2c637 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70  assert( pTerm->p
2c638 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 61  Expr!=0 );.    a
2c639 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 6c 65  ssert( pTerm->le
2c63a 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29  ftCursor==iCur )
2c63b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d  ;.    assert( om
2c63c 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  itTable==0 );.  
2c63d 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20 63 6f    iRowidReg = co
2c63e 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 70  deEqualityTerm(p
2c63f 50 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70 4c  Parse, pTerm, pL
2c640 65 76 65 6c 2c 20 69 52 65 6c 65 61 73 65 52 65  evel, iReleaseRe
2c641 67 29 3b 0a 20 20 20 20 61 64 64 72 4e 78 74 20  g);.    addrNxt 
2c642 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78  = pLevel->addrNx
2c643 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
2c644 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d  beAddOp2(v, OP_M
2c645 75 73 74 42 65 49 6e 74 2c 20 69 52 6f 77 69 64  ustBeInt, iRowid
2c646 52 65 67 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20  Reg, addrNxt);. 
2c647 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c648 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
2c649 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72  ists, iCur, addr
2c64a 4e 78 74 2c 20 69 52 6f 77 69 64 52 65 67 29 3b  Nxt, iRowidReg);
2c64b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2c64c 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 72 73  CacheStore(pPars
2c64d 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f  e, iCur, -1, iRo
2c64e 77 69 64 52 65 67 29 3b 0a 20 20 20 20 56 64 62  widReg);.    Vdb
2c64f 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 70 6b  eComment((v, "pk
2c650 22 29 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  "));.    pLevel-
2c651 3e 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20  >op = OP_Noop;. 
2c652 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65   }else if( pLeve
2c653 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2c654 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  & WHERE_ROWID_RA
2c655 4e 47 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61  NGE ){.    /* Ca
2c656 73 65 20 32 3a 20 20 57 65 20 68 61 76 65 20 61  se 2:  We have a
2c657 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6d  n inequality com
2c658 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73 74 20  parison against 
2c659 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c 64 2e  the ROWID field.
2c65a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
2c65b 74 65 73 74 4f 70 20 3d 20 4f 50 5f 4e 6f 6f 70  testOp = OP_Noop
2c65c 3b 0a 20 20 20 20 69 6e 74 20 73 74 61 72 74 3b  ;.    int start;
2c65d 0a 20 20 20 20 69 6e 74 20 6d 65 6d 45 6e 64 56  .    int memEndV
2c65e 61 6c 75 65 20 3d 20 30 3b 0a 20 20 20 20 57 68  alue = 0;.    Wh
2c65f 65 72 65 54 65 72 6d 20 2a 70 53 74 61 72 74 2c  ereTerm *pStart,
2c660 20 2a 70 45 6e 64 3b 0a 0a 20 20 20 20 61 73 73   *pEnd;..    ass
2c661 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d  ert( omitTable==
2c662 30 20 29 3b 0a 20 20 20 20 70 53 74 61 72 74 20  0 );.    pStart 
2c663 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
2c664 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61  iCur, -1, notRea
2c665 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c  dy, WO_GT|WO_GE,
2c666 20 30 29 3b 0a 20 20 20 20 70 45 6e 64 20 3d 20   0);.    pEnd = 
2c667 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
2c668 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
2c669 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30  , WO_LT|WO_LE, 0
2c66a 29 3b 0a 20 20 20 20 69 66 28 20 62 52 65 76 20  );.    if( bRev 
2c66b 29 7b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d  ){.      pTerm =
2c66c 20 70 53 74 61 72 74 3b 0a 20 20 20 20 20 20 70   pStart;.      p
2c66d 53 74 61 72 74 20 3d 20 70 45 6e 64 3b 0a 20 20  Start = pEnd;.  
2c66e 20 20 20 20 70 45 6e 64 20 3d 20 70 54 65 72 6d      pEnd = pTerm
2c66f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2c670 70 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20  pStart ){.      
2c671 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20  Expr *pX;       
2c672 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
2c673 72 65 73 73 69 6f 6e 20 74 68 61 74 20 64 65 66  ression that def
2c674 69 6e 65 73 20 74 68 65 20 73 74 61 72 74 20 62  ines the start b
2c675 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ound */.      in
2c676 74 20 72 31 2c 20 72 54 65 6d 70 3b 20 20 20 20  t r1, rTemp;    
2c677 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 73      /* Registers
2c678 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65   for holding the
2c679 20 73 74 61 72 74 20 62 6f 75 6e 64 61 72 79 20   start boundary 
2c67a 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  */..      /* The
2c67b 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74   following const
2c67c 61 6e 74 20 6d 61 70 73 20 54 4b 5f 78 78 20 63  ant maps TK_xx c
2c67d 6f 64 65 73 20 69 6e 74 6f 20 63 6f 72 72 65 73  odes into corres
2c67e 70 6f 6e 64 69 6e 67 20 0a 20 20 20 20 20 20 2a  ponding .      *
2c67f 2a 20 73 65 65 6b 20 6f 70 63 6f 64 65 73 2e 20  * seek opcodes. 
2c680 20 49 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 61   It depends on a
2c681 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
2c682 72 69 6e 67 20 6f 66 20 54 4b 5f 78 78 0a 20 20  ring of TK_xx.  
2c683 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e      */.      con
2c684 73 74 20 75 38 20 61 4d 6f 76 65 4f 70 5b 5d 20  st u8 aMoveOp[] 
2c685 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 2f  = {.           /
2c686 2a 20 54 4b 5f 47 54 20 2a 2f 20 20 4f 50 5f 53  * TK_GT */  OP_S
2c687 65 65 6b 47 74 2c 0a 20 20 20 20 20 20 20 20 20  eekGt,.         
2c688 20 20 2f 2a 20 54 4b 5f 4c 45 20 2a 2f 20 20 4f    /* TK_LE */  O
2c689 50 5f 53 65 65 6b 4c 65 2c 0a 20 20 20 20 20 20  P_SeekLe,.      
2c68a 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 54 20 2a 2f       /* TK_LT */
2c68b 20 20 4f 50 5f 53 65 65 6b 4c 74 2c 0a 20 20 20    OP_SeekLt,.   
2c68c 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 47 45          /* TK_GE
2c68d 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 47 65 0a 20   */  OP_SeekGe. 
2c68e 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 61 73       };.      as
2c68f 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 54 4b 5f  sert( TK_LE==TK_
2c690 47 54 2b 31 20 29 3b 20 20 20 20 20 20 2f 2a 20  GT+1 );      /* 
2c691 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72  Make sure the or
2c692 64 65 72 69 6e 67 2e 2e 20 2a 2f 0a 20 20 20 20  dering.. */.    
2c693 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d    assert( TK_LT=
2c694 3d 54 4b 5f 47 54 2b 32 20 29 3b 20 20 20 20 20  =TK_GT+2 );     
2c695 20 2f 2a 20 20 2e 2e 2e 20 6f 66 20 74 68 65 20   /*  ... of the 
2c696 54 4b 5f 78 78 20 76 61 6c 75 65 73 2e 2e 2e 20  TK_xx values... 
2c697 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
2c698 20 54 4b 5f 47 45 3d 3d 54 4b 5f 47 54 2b 33 20   TK_GE==TK_GT+3 
2c699 29 3b 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20  );      /*  ... 
2c69a 69 73 20 63 6f 72 72 65 63 63 74 2e 20 2a 2f 0a  is correcct. */.
2c69b 0a 20 20 20 20 20 20 70 58 20 3d 20 70 53 74 61  .      pX = pSta
2c69c 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  rt->pExpr;.     
2c69d 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 20 29   assert( pX!=0 )
2c69e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2c69f 70 53 74 61 72 74 2d 3e 6c 65 66 74 43 75 72 73  pStart->leftCurs
2c6a0 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20 20 20 20  or==iCur );.    
2c6a1 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
2c6a2 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
2c6a3 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 26  e, pX->pRight, &
2c6a4 72 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71  rTemp);.      sq
2c6a5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2c6a6 76 2c 20 61 4d 6f 76 65 4f 70 5b 70 58 2d 3e 6f  v, aMoveOp[pX->o
2c6a7 70 2d 54 4b 5f 47 54 5d 2c 20 69 43 75 72 2c 20  p-TK_GT], iCur, 
2c6a8 61 64 64 72 42 72 6b 2c 20 72 31 29 3b 0a 20 20  addrBrk, r1);.  
2c6a9 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2c6aa 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 20  (v, "pk"));.    
2c6ab 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2c6ac 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
2c6ad 28 70 50 61 72 73 65 2c 20 72 31 2c 20 31 29 3b  (pParse, r1, 1);
2c6ae 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
2c6af 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2c6b0 72 73 65 2c 20 72 54 65 6d 70 29 3b 0a 20 20 20  rse, rTemp);.   
2c6b1 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70     disableTerm(p
2c6b2 4c 65 76 65 6c 2c 20 70 53 74 61 72 74 29 3b 0a  Level, pStart);.
2c6b3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2c6b4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c6b5 70 32 28 76 2c 20 62 52 65 76 20 3f 20 4f 50 5f  p2(v, bRev ? OP_
2c6b6 4c 61 73 74 20 3a 20 4f 50 5f 52 65 77 69 6e 64  Last : OP_Rewind
2c6b7 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b 29  , iCur, addrBrk)
2c6b8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2c6b9 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 45 78  pEnd ){.      Ex
2c6ba 70 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 70 58  pr *pX;.      pX
2c6bb 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70 72 3b 0a   = pEnd->pExpr;.
2c6bc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58        assert( pX
2c6bd 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
2c6be 65 72 74 28 20 70 45 6e 64 2d 3e 6c 65 66 74 43  ert( pEnd->leftC
2c6bf 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20  ursor==iCur );. 
2c6c0 20 20 20 20 20 6d 65 6d 45 6e 64 56 61 6c 75 65       memEndValue
2c6c1 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2c6c2 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  m;.      sqlite3
2c6c3 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2c6c4 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 6d 65 6d   pX->pRight, mem
2c6c5 45 6e 64 56 61 6c 75 65 29 3b 0a 20 20 20 20 20  EndValue);.     
2c6c6 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f   if( pX->op==TK_
2c6c7 4c 54 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b  LT || pX->op==TK
2c6c8 5f 47 54 20 29 7b 0a 20 20 20 20 20 20 20 20 74  _GT ){.        t
2c6c9 65 73 74 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f  estOp = bRev ? O
2c6ca 50 5f 4c 65 20 3a 20 4f 50 5f 47 65 3b 0a 20 20  P_Le : OP_Ge;.  
2c6cb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2c6cc 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65 76     testOp = bRev
2c6cd 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47 74   ? OP_Lt : OP_Gt
2c6ce 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c6cf 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
2c6d0 65 6c 2c 20 70 45 6e 64 29 3b 0a 20 20 20 20 7d  el, pEnd);.    }
2c6d1 0a 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c  .    start = sql
2c6d2 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2c6d3 64 64 72 28 76 29 3b 0a 20 20 20 20 70 4c 65 76  ddr(v);.    pLev
2c6d4 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f 20  el->op = bRev ? 
2c6d5 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65 78  OP_Prev : OP_Nex
2c6d6 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  t;.    pLevel->p
2c6d7 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70 4c  1 = iCur;.    pL
2c6d8 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72 74  evel->p2 = start
2c6d9 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 35  ;.    pLevel->p5
2c6da 20 3d 20 28 70 53 74 61 72 74 3d 3d 30 20 26 26   = (pStart==0 &&
2c6db 20 70 45 6e 64 3d 3d 30 29 20 3f 31 3a 30 3b 0a   pEnd==0) ?1:0;.
2c6dc 20 20 20 20 69 66 28 20 74 65 73 74 4f 70 21 3d      if( testOp!=
2c6dd 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20  OP_Noop ){.     
2c6de 20 69 52 6f 77 69 64 52 65 67 20 3d 20 69 52 65   iRowidReg = iRe
2c6df 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74  leaseReg = sqlit
2c6e0 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2c6e1 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rse);.      sqli
2c6e2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c6e3 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c   OP_Rowid, iCur,
2c6e4 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20   iRowidReg);.   
2c6e5 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2c6e6 63 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c  cheStore(pParse,
2c6e7 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69   iCur, -1, iRowi
2c6e8 64 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c  dReg);.      sql
2c6e9 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2c6ea 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d 45 6e 64  , testOp, memEnd
2c6eb 56 61 6c 75 65 2c 20 61 64 64 72 42 72 6b 2c 20  Value, addrBrk, 
2c6ec 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
2c6ed 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2c6ee 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f  ngeP5(v, SQLITE_
2c6ef 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 20 53 51  AFF_NUMERIC | SQ
2c6f0 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
2c6f1 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
2c6f2 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
2c6f3 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  .wsFlags & (WHER
2c6f4 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57  E_COLUMN_RANGE|W
2c6f5 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 29 20  HERE_COLUMN_EQ) 
2c6f6 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 33  ){.    /* Case 3
2c6f7 3a 20 41 20 73 63 61 6e 20 75 73 69 6e 67 20 61  : A scan using a
2c6f8 6e 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 0a  n index..    **.
2c6f9 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54      **         T
2c6fa 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2c6fb 6d 61 79 20 63 6f 6e 74 61 69 6e 20 7a 65 72 6f  may contain zero
2c6fc 20 6f 72 20 6d 6f 72 65 20 65 71 75 61 6c 69 74   or more equalit
2c6fd 79 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  y .    **       
2c6fe 20 20 74 65 72 6d 73 20 28 22 3d 3d 22 20 6f 72    terms ("==" or
2c6ff 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 29   "IN" operators)
2c700 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
2c701 68 65 20 4e 0a 20 20 20 20 2a 2a 20 20 20 20 20  he N.    **     
2c702 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f      left-most co
2c703 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 69 6e 64  lumns of the ind
2c704 65 78 2e 20 49 74 20 6d 61 79 20 61 6c 73 6f 20  ex. It may also 
2c705 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 20  contain.    **  
2c706 20 20 20 20 20 20 20 69 6e 65 71 75 61 6c 69 74         inequalit
2c707 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 3e  y constraints (>
2c708 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d 29 20 6f  , <, >= or <=) o
2c709 6e 20 74 68 65 20 69 6e 64 65 78 65 64 0a 20 20  n the indexed.  
2c70a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6c    **         col
2c70b 75 6d 6e 20 74 68 61 74 20 69 6d 6d 65 64 69 61  umn that immedia
2c70c 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65  tely follows the
2c70d 20 4e 20 65 71 75 61 6c 69 74 69 65 73 2e 20 4f   N equalities. O
2c70e 6e 6c 79 20 0a 20 20 20 20 2a 2a 20 20 20 20 20  nly .    **     
2c70f 20 20 20 20 74 68 65 20 72 69 67 68 74 2d 6d 6f      the right-mo
2c710 73 74 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65  st column can be
2c711 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 2d   an inequality -
2c712 20 74 68 65 20 72 65 73 74 20 6d 75 73 74 0a 20   the rest must. 
2c713 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 75 73     **         us
2c714 65 20 74 68 65 20 22 3d 3d 22 20 61 6e 64 20 22  e the "==" and "
2c715 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 2e 20 46  IN" operators. F
2c716 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74  or example, if t
2c717 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  he .    **      
2c718 20 20 20 69 6e 64 65 78 20 69 73 20 6f 6e 20 28     index is on (
2c719 78 2c 79 2c 7a 29 2c 20 74 68 65 6e 20 74 68 65  x,y,z), then the
2c71a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6c 61 75 73   following claus
2c71b 65 73 20 61 72 65 20 61 6c 6c 20 0a 20 20 20 20  es are all .    
2c71c 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 74 69 6d  **         optim
2c71d 69 7a 65 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  ized:.    **.   
2c71e 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78   **            x
2c71f 3d 35 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  =5.    **       
2c720 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3d 31       x=5 AND y=1
2c721 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  0.    **        
2c722 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3c 31 30      x=5 AND y<10
2c723 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2c724 20 20 20 78 3d 35 20 41 4e 44 20 79 3e 35 20 41     x=5 AND y>5 A
2c725 4e 44 20 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20  ND y<10.    **  
2c726 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e            x=5 AN
2c727 44 20 79 3d 35 20 41 4e 44 20 7a 3c 3d 31 30 0a  D y=5 AND z<=10.
2c728 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2c729 20 20 20 20 20 20 54 68 65 20 7a 3c 31 30 20 74        The z<10 t
2c72a 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  erm of the follo
2c72b 77 69 6e 67 20 63 61 6e 6e 6f 74 20 62 65 20 75  wing cannot be u
2c72c 73 65 64 2c 20 6f 6e 6c 79 0a 20 20 20 20 2a 2a  sed, only.    **
2c72d 20 20 20 20 20 20 20 20 20 74 68 65 20 78 3d 35           the x=5
2c72e 20 74 65 72 6d 3a 0a 20 20 20 20 2a 2a 0a 20 20   term:.    **.  
2c72f 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
2c730 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a 20 20 20  x=5 AND z<10.   
2c731 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
2c732 20 20 20 4e 20 6d 61 79 20 62 65 20 7a 65 72 6f     N may be zero
2c733 20 69 66 20 74 68 65 72 65 20 61 72 65 20 69 6e   if there are in
2c734 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2c735 69 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 20 20 20  ints..    **    
2c736 20 20 20 20 20 49 66 20 74 68 65 72 65 20 61 72       If there ar
2c737 65 20 6e 6f 20 69 6e 65 71 75 61 6c 69 74 79 20  e no inequality 
2c738 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65  constraints, the
2c739 6e 20 4e 20 69 73 20 61 74 0a 20 20 20 20 2a 2a  n N is at.    **
2c73a 20 20 20 20 20 20 20 20 20 6c 65 61 73 74 20 6f           least o
2c73b 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ne..    **.    *
2c73c 2a 20 20 20 20 20 20 20 20 20 54 68 69 73 20 63  *         This c
2c73d 61 73 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ase is also used
2c73e 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20   when there are 
2c73f 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a  no WHERE clause.
2c740 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63      **         c
2c741 6f 6e 73 74 72 61 69 6e 74 73 20 62 75 74 20 61  onstraints but a
2c742 6e 20 69 6e 64 65 78 20 69 73 20 73 65 6c 65 63  n index is selec
2c743 74 65 64 20 61 6e 79 77 61 79 2c 20 69 6e 20 6f  ted anyway, in o
2c744 72 64 65 72 0a 20 20 20 20 2a 2a 20 20 20 20 20  rder.    **     
2c745 20 20 20 20 74 6f 20 66 6f 72 63 65 20 74 68 65      to force the
2c746 20 6f 75 74 70 75 74 20 6f 72 64 65 72 20 74 6f   output order to
2c747 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61 6e 20 4f   conform to an O
2c748 52 44 45 52 20 42 59 2e 0a 20 20 20 20 2a 2f 20  RDER BY..    */ 
2c749 20 0a 20 20 20 20 69 6e 74 20 61 53 74 61 72 74   .    int aStart
2c74a 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 30  Op[] = {.      0
2c74b 2c 0a 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20  ,.      0,.     
2c74c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20   OP_Rewind,     
2c74d 20 20 20 20 20 20 2f 2a 20 32 3a 20 28 21 73 74        /* 2: (!st
2c74e 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20  art_constraints 
2c74f 26 26 20 73 74 61 72 74 45 71 20 26 26 20 20 21  && startEq &&  !
2c750 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f  bRev) */.      O
2c751 50 5f 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20  P_Last,         
2c752 20 20 20 20 2f 2a 20 33 3a 20 28 21 73 74 61 72      /* 3: (!star
2c753 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26  t_constraints &&
2c754 20 73 74 61 72 74 45 71 20 26 26 20 20 20 62 52   startEq &&   bR
2c755 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f  ev) */.      OP_
2c756 53 65 65 6b 47 74 2c 20 20 20 20 20 20 20 20 20  SeekGt,         
2c757 20 20 2f 2a 20 34 3a 20 28 73 74 61 72 74 5f 63    /* 4: (start_c
2c758 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 21  onstraints  && !
2c759 73 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76  startEq && !bRev
2c75a 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65  ) */.      OP_Se
2c75b 65 6b 4c 74 2c 20 20 20 20 20 20 20 20 20 20 20  ekLt,           
2c75c 2f 2a 20 35 3a 20 28 73 74 61 72 74 5f 63 6f 6e  /* 5: (start_con
2c75d 73 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 74  straints  && !st
2c75e 61 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20  artEq &&  bRev) 
2c75f 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b  */.      OP_Seek
2c760 47 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Ge,           /*
2c761 20 36 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74   6: (start_const
2c762 72 61 69 6e 74 73 20 20 26 26 20 20 73 74 61 72  raints  &&  star
2c763 74 45 71 20 26 26 20 21 62 52 65 76 29 20 2a 2f  tEq && !bRev) */
2c764 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 4c 65  .      OP_SeekLe
2c765 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 37              /* 7
2c766 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61  : (start_constra
2c767 69 6e 74 73 20 20 26 26 20 20 73 74 61 72 74 45  ints  &&  startE
2c768 71 20 26 26 20 20 62 52 65 76 29 20 2a 2f 0a 20  q &&  bRev) */. 
2c769 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 61 45     };.    int aE
2c76a 6e 64 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  ndOp[] = {.     
2c76b 20 4f 50 5f 4e 6f 6f 70 2c 20 20 20 20 20 20 20   OP_Noop,       
2c76c 20 20 20 20 20 20 2f 2a 20 30 3a 20 28 21 65 6e        /* 0: (!en
2c76d 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 29 20 2a  d_constraints) *
2c76e 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64 78 47 45  /.      OP_IdxGE
2c76f 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2c770 31 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69  1: (end_constrai
2c771 6e 74 73 20 26 26 20 21 62 52 65 76 29 20 2a 2f  nts && !bRev) */
2c772 0a 20 20 20 20 20 20 4f 50 5f 49 64 78 4c 54 20  .      OP_IdxLT 
2c773 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32              /* 2
2c774 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e  : (end_constrain
2c775 74 73 20 26 26 20 62 52 65 76 29 20 2a 2f 0a 20  ts && bRev) */. 
2c776 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6e 45     };.    int nE
2c777 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  q = pLevel->plan
2c778 2e 6e 45 71 3b 0a 20 20 20 20 69 6e 74 20 69 73  .nEq;.    int is
2c779 4d 69 6e 51 75 65 72 79 20 3d 20 30 3b 20 20 20  MinQuery = 0;   
2c77a 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69         /* If thi
2c77b 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 65  s is an optimize
2c77c 64 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 2e  d SELECT min(x).
2c77d 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67  . */.    int reg
2c77e 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  Base;           
2c77f 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65        /* Base re
2c780 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63  gister holding c
2c781 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c 75 65 73  onstraint values
2c782 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 31 3b 20   */.    int r1; 
2c783 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c784 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67       /* Temp reg
2c785 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 57 68 65  ister */.    Whe
2c786 72 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 53 74  reTerm *pRangeSt
2c787 61 72 74 20 3d 20 30 3b 20 20 2f 2a 20 49 6e 65  art = 0;  /* Ine
2c788 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2c789 6e 74 20 61 74 20 72 61 6e 67 65 20 73 74 61 72  nt at range star
2c78a 74 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 54 65  t */.    WhereTe
2c78b 72 6d 20 2a 70 52 61 6e 67 65 45 6e 64 20 3d 20  rm *pRangeEnd = 
2c78c 30 3b 20 20 20 20 2f 2a 20 49 6e 65 71 75 61 6c  0;    /* Inequal
2c78d 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61  ity constraint a
2c78e 74 20 72 61 6e 67 65 20 65 6e 64 20 2a 2f 0a 20  t range end */. 
2c78f 20 20 20 69 6e 74 20 73 74 61 72 74 45 71 3b 20     int startEq; 
2c790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c791 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e 67 65  /* True if range
2c792 20 73 74 61 72 74 20 75 73 65 73 20 3d 3d 2c 20   start uses ==, 
2c793 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20  >= or <= */.    
2c794 69 6e 74 20 65 6e 64 45 71 3b 20 20 20 20 20 20  int endEq;      
2c795 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c796 54 72 75 65 20 69 66 20 72 61 6e 67 65 20 65 6e  True if range en
2c797 64 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72  d uses ==, >= or
2c798 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73   <= */.    int s
2c799 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
2c79a 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74  ;       /* Start
2c79b 20 6f 66 20 72 61 6e 67 65 20 69 73 20 63 6f 6e   of range is con
2c79c 73 74 72 61 69 6e 65 64 20 2a 2f 0a 20 20 20 20  strained */.    
2c79d 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  int nConstraint;
2c79e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c79f 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6e 73 74 72  Number of constr
2c7a0 61 69 6e 74 20 74 65 72 6d 73 20 2a 2f 0a 20 20  aint terms */.  
2c7a1 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
2c7a2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
2c7a3 64 65 78 20 77 65 20 77 69 6c 6c 20 62 65 20 75  dex we will be u
2c7a4 73 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20  sing */.    int 
2c7a5 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 20  iIdxCur;        
2c7a6 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
2c7a7 73 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65  sor for the inde
2c7a8 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 78  x */.    int nEx
2c7a9 74 72 61 52 65 67 20 3d 20 30 3b 20 20 20 2f 2a  traReg = 0;   /*
2c7aa 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61   Number of extra
2c7ab 20 72 65 67 69 73 74 65 72 73 20 6e 65 65 64 65   registers neede
2c7ac 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 3b  d */.    int op;
2c7ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c7ae 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 6f 70 63   Instruction opc
2c7af 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 70 49 64 78  ode */..    pIdx
2c7b0 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
2c7b1 75 2e 70 49 64 78 3b 0a 20 20 20 20 69 49 64 78  u.pIdx;.    iIdx
2c7b2 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49  Cur = pLevel->iI
2c7b3 64 78 43 75 72 3b 0a 20 20 20 20 6b 20 3d 20 70  dxCur;.    k = p
2c7b4 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45  Idx->aiColumn[nE
2c7b5 71 5d 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  q];     /* Colum
2c7b6 6e 20 66 6f 72 20 69 6e 65 71 75 61 6c 69 74 79  n for inequality
2c7b7 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
2c7b8 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
2c7b9 6c 6f 6f 70 20 73 61 74 69 73 66 69 65 73 20 61  loop satisfies a
2c7ba 20 73 6f 72 74 20 6f 72 64 65 72 20 28 70 4f 72   sort order (pOr
2c7bb 64 65 72 42 79 29 20 72 65 71 75 65 73 74 20 74  derBy) request t
2c7bc 68 61 74 20 0a 20 20 20 20 2a 2a 20 77 61 73 20  hat .    ** was 
2c7bd 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
2c7be 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6d 70 6c 65  unction to imple
2c7bf 6d 65 6e 74 20 61 20 22 53 45 4c 45 43 54 20 6d  ment a "SELECT m
2c7c0 69 6e 28 78 29 20 2e 2e 2e 22 20 0a 20 20 20 20  in(x) ..." .    
2c7c1 2a 2a 20 71 75 65 72 79 2c 20 74 68 65 6e 20 74  ** query, then t
2c7c2 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f  he caller will o
2c7c3 6e 6c 79 20 61 6c 6c 6f 77 20 74 68 65 20 6c 6f  nly allow the lo
2c7c4 6f 70 20 74 6f 20 72 75 6e 20 66 6f 72 0a 20 20  op to run for.  
2c7c5 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69 74    ** a single it
2c7c6 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 6d 65  eration. This me
2c7c7 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 69 72  ans that the fir
2c7c8 73 74 20 72 6f 77 20 72 65 74 75 72 6e 65 64 0a  st row returned.
2c7c9 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f      ** should no
2c7ca 74 20 68 61 76 65 20 61 20 4e 55 4c 4c 20 76 61  t have a NULL va
2c7cb 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 27 78  lue stored in 'x
2c7cc 27 2e 20 49 66 20 63 6f 6c 75 6d 6e 20 27 78 27  '. If column 'x'
2c7cd 20 69 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 66   is.    ** the f
2c7ce 69 72 73 74 20 6f 6e 65 20 61 66 74 65 72 20 74  irst one after t
2c7cf 68 65 20 6e 45 71 20 65 71 75 61 6c 69 74 79 20  he nEq equality 
2c7d0 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20 74  constraints in t
2c7d1 68 65 20 69 6e 64 65 78 2c 0a 20 20 20 20 2a 2a  he index,.    **
2c7d2 20 74 68 69 73 20 72 65 71 75 69 72 65 73 20 73   this requires s
2c7d3 6f 6d 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64  ome special hand
2c7d4 6c 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ling..    */.   
2c7d5 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73   if( (wctrlFlags
2c7d6 26 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d  &WHERE_ORDERBY_M
2c7d7 49 4e 29 21 3d 30 0a 20 20 20 20 20 26 26 20 28  IN)!=0.     && (
2c7d8 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2c7d9 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45 52  lags&WHERE_ORDER
2c7da 42 59 29 0a 20 20 20 20 20 26 26 20 28 70 49 64  BY).     && (pId
2c7db 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 6e 45 71 29 0a  x->nColumn>nEq).
2c7dc 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20      ){.      /* 
2c7dd 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79  assert( pOrderBy
2c7de 2d 3e 6e 45 78 70 72 3d 3d 31 20 29 3b 20 2a 2f  ->nExpr==1 ); */
2c7df 0a 20 20 20 20 20 20 2f 2a 20 61 73 73 65 72 74  .      /* assert
2c7e0 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d  ( pOrderBy->a[0]
2c7e1 2e 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d  .pExpr->iColumn=
2c7e2 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  =pIdx->aiColumn[
2c7e3 6e 45 71 5d 20 29 3b 20 2a 2f 0a 20 20 20 20 20  nEq] ); */.     
2c7e4 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20 31 3b   isMinQuery = 1;
2c7e5 0a 20 20 20 20 20 20 6e 45 78 74 72 61 52 65 67  .      nExtraReg
2c7e6 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 1;.    }..   
2c7e7 20 2f 2a 20 46 69 6e 64 20 61 6e 79 20 69 6e 65   /* Find any ine
2c7e8 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2c7e9 6e 74 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65  nt terms for the
2c7ea 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64 20 0a   start and end .
2c7eb 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 61      ** of the ra
2c7ec 6e 67 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  nge. .    */.   
2c7ed 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61   if( pLevel->pla
2c7ee 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2c7ef 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 7b 0a 20  E_TOP_LIMIT ){. 
2c7f0 20 20 20 20 20 70 52 61 6e 67 65 45 6e 64 20 3d       pRangeEnd =
2c7f1 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
2c7f2 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79  Cur, k, notReady
2c7f3 2c 20 28 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 29 2c  , (WO_LT|WO_LE),
2c7f4 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 6e 45   pIdx);.      nE
2c7f5 78 74 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20  xtraReg = 1;.   
2c7f6 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65   }.    if( pLeve
2c7f7 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2c7f8 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  & WHERE_BTM_LIMI
2c7f9 54 20 29 7b 0a 20 20 20 20 20 20 70 52 61 6e 67  T ){.      pRang
2c7fa 65 53 74 61 72 74 20 3d 20 66 69 6e 64 54 65 72  eStart = findTer
2c7fb 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20  m(pWC, iCur, k, 
2c7fc 6e 6f 74 52 65 61 64 79 2c 20 28 57 4f 5f 47 54  notReady, (WO_GT
2c7fd 7c 57 4f 5f 47 45 29 2c 20 70 49 64 78 29 3b 0a  |WO_GE), pIdx);.
2c7fe 20 20 20 20 20 20 6e 45 78 74 72 61 52 65 67 20        nExtraReg 
2c7ff 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 1;.    }..    
2c800 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2c801 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6c 6c   to evaluate all
2c802 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d   constraint term
2c803 73 20 75 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e  s using == or IN
2c804 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72  .    ** and stor
2c805 65 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  e the values of 
2c806 74 68 6f 73 65 20 74 65 72 6d 73 20 69 6e 20 61  those terms in a
2c807 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73  n array of regis
2c808 74 65 72 73 0a 20 20 20 20 2a 2a 20 73 74 61 72  ters.    ** star
2c809 74 69 6e 67 20 61 74 20 72 65 67 42 61 73 65 2e  ting at regBase.
2c80a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 42  .    */.    regB
2c80b 61 73 65 20 3d 20 63 6f 64 65 41 6c 6c 45 71 75  ase = codeAllEqu
2c80c 61 6c 69 74 79 54 65 72 6d 73 28 70 50 61 72 73  alityTerms(pPars
2c80d 65 2c 20 70 4c 65 76 65 6c 2c 20 70 57 43 2c 20  e, pLevel, pWC, 
2c80e 6e 6f 74 52 65 61 64 79 2c 20 6e 45 78 74 72 61  notReady, nExtra
2c80f 52 65 67 29 3b 0a 20 20 20 20 61 64 64 72 4e 78  Reg);.    addrNx
2c810 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72  t = pLevel->addr
2c811 4e 78 74 3b 0a 0a 0a 20 20 20 20 2f 2a 20 49 66  Nxt;...    /* If
2c812 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 20   we are doing a 
2c813 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 73 63  reverse order sc
2c814 61 6e 20 6f 6e 20 61 6e 20 61 73 63 65 6e 64 69  an on an ascendi
2c815 6e 67 20 69 6e 64 65 78 2c 20 6f 72 0a 20 20 20  ng index, or.   
2c816 20 2a 2a 20 61 20 66 6f 72 77 61 72 64 20 6f 72   ** a forward or
2c817 64 65 72 20 73 63 61 6e 20 6f 6e 20 61 20 64 65  der scan on a de
2c818 73 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20  scending index, 
2c819 69 6e 74 65 72 63 68 61 6e 67 65 20 74 68 65 20  interchange the 
2c81a 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 20 61 6e  .    ** start an
2c81b 64 20 65 6e 64 20 74 65 72 6d 73 20 28 70 52 61  d end terms (pRa
2c81c 6e 67 65 53 74 61 72 74 20 61 6e 64 20 70 52 61  ngeStart and pRa
2c81d 6e 67 65 45 6e 64 29 2e 0a 20 20 20 20 2a 2f 0a  ngeEnd)..    */.
2c81e 20 20 20 20 69 66 28 20 62 52 65 76 3d 3d 28 70      if( bRev==(p
2c81f 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Idx->aSortOrder[
2c820 6e 45 71 5d 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f  nEq]==SQLITE_SO_
2c821 41 53 43 29 20 29 7b 0a 20 20 20 20 20 20 53 57  ASC) ){.      SW
2c822 41 50 28 57 68 65 72 65 54 65 72 6d 20 2a 2c 20  AP(WhereTerm *, 
2c823 70 52 61 6e 67 65 45 6e 64 2c 20 70 52 61 6e 67  pRangeEnd, pRang
2c824 65 53 74 61 72 74 29 3b 0a 20 20 20 20 7d 0a 0a  eStart);.    }..
2c825 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52      testcase( pR
2c826 61 6e 67 65 53 74 61 72 74 20 26 26 20 70 52 61  angeStart && pRa
2c827 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 61  ngeStart->eOpera
2c828 74 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20  tor & WO_LE );. 
2c829 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52 61     testcase( pRa
2c82a 6e 67 65 53 74 61 72 74 20 26 26 20 70 52 61 6e  ngeStart && pRan
2c82b 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 61 74  geStart->eOperat
2c82c 6f 72 20 26 20 57 4f 5f 47 45 20 29 3b 0a 20 20  or & WO_GE );.  
2c82d 20 20 74 65 73 74 63 61 73 65 28 20 70 52 61 6e    testcase( pRan
2c82e 67 65 45 6e 64 20 26 26 20 70 52 61 6e 67 65 45  geEnd && pRangeE
2c82f 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  nd->eOperator & 
2c830 57 4f 5f 4c 45 20 29 3b 0a 20 20 20 20 74 65 73  WO_LE );.    tes
2c831 74 63 61 73 65 28 20 70 52 61 6e 67 65 45 6e 64  tcase( pRangeEnd
2c832 20 26 26 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65   && pRangeEnd->e
2c833 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45  Operator & WO_GE
2c834 20 29 3b 0a 20 20 20 20 73 74 61 72 74 45 71 20   );.    startEq 
2c835 3d 20 21 70 52 61 6e 67 65 53 74 61 72 74 20 7c  = !pRangeStart |
2c836 7c 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65  | pRangeStart->e
2c837 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c  Operator & (WO_L
2c838 45 7c 57 4f 5f 47 45 29 3b 0a 20 20 20 20 65 6e  E|WO_GE);.    en
2c839 64 45 71 20 3d 20 20 20 21 70 52 61 6e 67 65 45  dEq =   !pRangeE
2c83a 6e 64 20 7c 7c 20 70 52 61 6e 67 65 45 6e 64 2d  nd || pRangeEnd-
2c83b 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f  >eOperator & (WO
2c83c 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20 20 20 20  _LE|WO_GE);.    
2c83d 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74  start_constraint
2c83e 73 20 3d 20 70 52 61 6e 67 65 53 74 61 72 74 20  s = pRangeStart 
2c83f 7c 7c 20 6e 45 71 3e 30 3b 0a 0a 20 20 20 20 2f  || nEq>0;..    /
2c840 2a 20 53 65 65 6b 20 74 68 65 20 69 6e 64 65 78  * Seek the index
2c841 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 73   cursor to the s
2c842 74 61 72 74 20 6f 66 20 74 68 65 20 72 61 6e 67  tart of the rang
2c843 65 2e 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e 73 74  e. */.    nConst
2c844 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20 20  raint = nEq;.   
2c845 20 69 66 28 20 70 52 61 6e 67 65 53 74 61 72 74   if( pRangeStart
2c846 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2c847 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
2c848 2c 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 70  , pRangeStart->p
2c849 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 72 65  Expr->pRight, re
2c84a 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20  gBase+nEq);.    
2c84b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c84c 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c  Op2(v, OP_IsNull
2c84d 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 2c 20 61  , regBase+nEq, a
2c84e 64 64 72 4e 78 74 29 3b 0a 20 20 20 20 20 20 6e  ddrNxt);.      n
2c84f 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20  Constraint++;.  
2c850 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4d 69    }else if( isMi
2c851 6e 51 75 65 72 79 20 29 7b 0a 20 20 20 20 20 20  nQuery ){.      
2c852 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c853 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2c854 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20   regBase+nEq);. 
2c855 20 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74       nConstraint
2c856 2b 2b 3b 0a 20 20 20 20 20 20 73 74 61 72 74 45  ++;.      startE
2c857 71 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 61  q = 0;.      sta
2c858 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d  rt_constraints =
2c859 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f   1;.    }.    co
2c85a 64 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28  deApplyAffinity(
2c85b 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c  pParse, regBase,
2c85c 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2c 20 70 49   nConstraint, pI
2c85d 64 78 29 3b 0a 20 20 20 20 6f 70 20 3d 20 61 53  dx);.    op = aS
2c85e 74 61 72 74 4f 70 5b 28 73 74 61 72 74 5f 63 6f  tartOp[(start_co
2c85f 6e 73 74 72 61 69 6e 74 73 3c 3c 32 29 20 2b 20  nstraints<<2) + 
2c860 28 73 74 61 72 74 45 71 3c 3c 31 29 20 2b 20 62  (startEq<<1) + b
2c861 52 65 76 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  Rev];.    assert
2c862 28 20 6f 70 21 3d 30 20 29 3b 0a 20 20 20 20 74  ( op!=0 );.    t
2c863 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
2c864 52 65 77 69 6e 64 20 29 3b 0a 20 20 20 20 74 65  Rewind );.    te
2c865 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4c  stcase( op==OP_L
2c866 61 73 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ast );.    testc
2c867 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b  ase( op==OP_Seek
2c868 47 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  Gt );.    testca
2c869 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47  se( op==OP_SeekG
2c86a 65 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  e );.    testcas
2c86b 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 4c 65  e( op==OP_SeekLe
2c86c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2c86d 28 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20  ( op==OP_SeekLt 
2c86e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2c86f 62 65 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20  beAddOp4(v, op, 
2c870 69 49 64 78 43 75 72 2c 20 61 64 64 72 4e 78 74  iIdxCur, addrNxt
2c871 2c 20 72 65 67 42 61 73 65 2c 20 0a 20 20 20 20  , regBase, .    
2c872 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c873 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f    SQLITE_INT_TO_
2c874 50 54 52 28 6e 43 6f 6e 73 74 72 61 69 6e 74 29  PTR(nConstraint)
2c875 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 0a 20 20  , P4_INT32);..  
2c876 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 76 61    /* Load the va
2c877 6c 75 65 20 66 6f 72 20 74 68 65 20 69 6e 65 71  lue for the ineq
2c878 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2c879 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  t at the end of 
2c87a 74 68 65 0a 20 20 20 20 2a 2a 20 72 61 6e 67 65  the.    ** range
2c87b 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 2a   (if any)..    *
2c87c 2f 0a 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e  /.    nConstrain
2c87d 74 20 3d 20 6e 45 71 3b 0a 20 20 20 20 69 66 28  t = nEq;.    if(
2c87e 20 70 52 61 6e 67 65 45 6e 64 20 29 7b 0a 20 20   pRangeEnd ){.  
2c87f 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c880 61 63 68 65 52 65 6d 6f 76 65 28 70 50 61 72 73  acheRemove(pPars
2c881 65 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b  e, regBase+nEq);
2c882 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2c883 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
2c884 52 61 6e 67 65 45 6e 64 2d 3e 70 45 78 70 72 2d  RangeEnd->pExpr-
2c885 3e 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65  >pRight, regBase
2c886 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 73 71 6c  +nEq);.      sql
2c887 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c888 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67  , OP_IsNull, reg
2c889 42 61 73 65 2b 6e 45 71 2c 20 61 64 64 72 4e 78  Base+nEq, addrNx
2c88a 74 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 41 70  t);.      codeAp
2c88b 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61 72  plyAffinity(pPar
2c88c 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 71  se, regBase, nEq
2c88d 2b 31 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  +1, pIdx);.     
2c88e 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a   nConstraint++;.
2c88f 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 6f      }..    /* To
2c890 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 62 6f  p of the loop bo
2c891 64 79 20 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c  dy */.    pLevel
2c892 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64  ->p2 = sqlite3Vd
2c893 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2c894 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  ;..    /* Check 
2c895 69 66 20 74 68 65 20 69 6e 64 65 78 20 63 75 72  if the index cur
2c896 73 6f 72 20 69 73 20 70 61 73 74 20 74 68 65 20  sor is past the 
2c897 65 6e 64 20 6f 66 20 74 68 65 20 72 61 6e 67 65  end of the range
2c898 2e 20 2a 2f 0a 20 20 20 20 6f 70 20 3d 20 61 45  . */.    op = aE
2c899 6e 64 4f 70 5b 28 70 52 61 6e 67 65 45 6e 64 20  ndOp[(pRangeEnd 
2c89a 7c 7c 20 6e 45 71 29 20 2a 20 28 31 20 2b 20 62  || nEq) * (1 + b
2c89b 52 65 76 29 5d 3b 0a 20 20 20 20 74 65 73 74 63  Rev)];.    testc
2c89c 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e 6f 6f 70  ase( op==OP_Noop
2c89d 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2c89e 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 47 45 20 29  ( op==OP_IdxGE )
2c89f 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2c8a0 6f 70 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 3b 0a  op==OP_IdxLT );.
2c8a1 20 20 20 20 69 66 28 20 6f 70 21 3d 4f 50 5f 4e      if( op!=OP_N
2c8a2 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  oop ){.      sql
2c8a3 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2c8a4 2c 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 61  , op, iIdxCur, a
2c8a5 64 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65 2c  ddrNxt, regBase,
2c8a6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c8a7 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
2c8a8 49 4e 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73  INT_TO_PTR(nCons
2c8a9 74 72 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33  traint), P4_INT3
2c8aa 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
2c8ab 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2c8ac 20 65 6e 64 45 71 21 3d 62 52 65 76 20 3f 31 3a   endEq!=bRev ?1:
2c8ad 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  0);.    }..    /
2c8ae 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 69  * If there are i
2c8af 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
2c8b0 61 69 6e 74 73 2c 20 63 68 65 63 6b 20 74 68 61  aints, check tha
2c8b1 74 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20  t the value.    
2c8b2 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ** of the table 
2c8b3 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20  column that the 
2c8b4 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 74 72  inequality contr
2c8b5 61 69 6e 73 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ains is not NULL
2c8b6 2e 0a 20 20 20 20 2a 2a 20 49 66 20 69 74 20 69  ..    ** If it i
2c8b7 73 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e  s, jump to the n
2c8b8 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ext iteration of
2c8b9 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a   the loop..    *
2c8ba 2f 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74  /.    r1 = sqlit
2c8bb 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2c8bc 72 73 65 29 3b 0a 20 20 20 20 74 65 73 74 63 61  rse);.    testca
2c8bd 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  se( pLevel->plan
2c8be 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2c8bf 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20 20  _BTM_LIMIT );.  
2c8c0 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76    testcase( pLev
2c8c1 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2c8c2 20 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d   & WHERE_TOP_LIM
2c8c3 49 54 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  IT );.    if( pL
2c8c4 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2c8c5 67 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d 5f  gs & (WHERE_BTM_
2c8c6 4c 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f  LIMIT|WHERE_TOP_
2c8c7 4c 49 4d 49 54 29 20 29 7b 0a 20 20 20 20 20 20  LIMIT) ){.      
2c8c8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c8c9 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2c8ca 69 49 64 78 43 75 72 2c 20 6e 45 71 2c 20 72 31  iIdxCur, nEq, r1
2c8cb 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c8cc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c8cd 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20 61 64 64  _IsNull, r1, add
2c8ce 72 43 6f 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20  rCont);.    }.  
2c8cf 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2c8d0 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2c8d1 72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65  r1);..    /* See
2c8d2 6b 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73  k the table curs
2c8d3 6f 72 2c 20 69 66 20 72 65 71 75 69 72 65 64 20  or, if required 
2c8d4 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65  */.    disableTe
2c8d5 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67  rm(pLevel, pRang
2c8d6 65 53 74 61 72 74 29 3b 0a 20 20 20 20 64 69 73  eStart);.    dis
2c8d7 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
2c8d8 20 70 52 61 6e 67 65 45 6e 64 29 3b 0a 20 20 20   pRangeEnd);.   
2c8d9 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65 20   if( !omitTable 
2c8da 29 7b 0a 20 20 20 20 20 20 69 52 6f 77 69 64 52  ){.      iRowidR
2c8db 65 67 20 3d 20 69 52 65 6c 65 61 73 65 52 65 67  eg = iReleaseReg
2c8dc 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2c8dd 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2c8de 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c8df 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 52  ddOp2(v, OP_IdxR
2c8e0 6f 77 69 64 2c 20 69 49 64 78 43 75 72 2c 20 69  owid, iIdxCur, i
2c8e1 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20  RowidReg);.     
2c8e2 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2c8e3 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69  eStore(pParse, i
2c8e4 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52  Cur, -1, iRowidR
2c8e5 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  eg);.      sqlit
2c8e6 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2c8e7 4f 50 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20 69  OP_Seek, iCur, i
2c8e8 52 6f 77 69 64 52 65 67 29 3b 20 20 2f 2a 20 44  RowidReg);  /* D
2c8e9 65 66 65 72 72 65 64 20 73 65 65 6b 20 2a 2f 0a  eferred seek */.
2c8ea 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
2c8eb 63 6f 72 64 20 74 68 65 20 69 6e 73 74 72 75 63  cord the instruc
2c8ec 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65 72  tion used to ter
2c8ed 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e  minate the loop.
2c8ee 20 44 69 73 61 62 6c 65 20 0a 20 20 20 20 2a 2a   Disable .    **
2c8ef 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
2c8f0 72 6d 73 20 6d 61 64 65 20 72 65 64 75 6e 64 61  rms made redunda
2c8f1 6e 74 20 62 79 20 74 68 65 20 69 6e 64 65 78 20  nt by the index 
2c8f2 72 61 6e 67 65 20 73 63 61 6e 2e 0a 20 20 20 20  range scan..    
2c8f3 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f  */.    pLevel->o
2c8f4 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72  p = bRev ? OP_Pr
2c8f5 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20  ev : OP_Next;.  
2c8f6 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69    pLevel->p1 = i
2c8f7 49 64 78 43 75 72 3b 0a 20 20 7d 65 6c 73 65 0a  IdxCur;.  }else.
2c8f8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c8f9 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2c8fa 54 49 4f 4e 0a 20 20 69 66 28 20 70 4c 65 76 65  TION.  if( pLeve
2c8fb 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2c8fc 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52  & WHERE_MULTI_OR
2c8fd 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20   ){.    /* Case 
2c8fe 34 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20  4:  Two or more 
2c8ff 73 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65 78  separately index
2c900 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74  ed terms connect
2c901 65 64 20 62 79 20 4f 52 0a 20 20 20 20 2a 2a 0a  ed by OR.    **.
2c902 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a      ** Example:.
2c903 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2c904 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
2c905 61 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 2a 2a  a,b,c,d);.    **
2c906 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20     CREATE INDEX 
2c907 69 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 20 20  i1 ON t1(a);.   
2c908 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44   **   CREATE IND
2c909 45 58 20 69 32 20 4f 4e 20 74 31 28 62 29 3b 0a  EX i2 ON t1(b);.
2c90a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20      **   CREATE 
2c90b 49 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 28 63  INDEX i3 ON t1(c
2c90c 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  );.    **.    **
2c90d 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
2c90e 20 74 31 20 57 48 45 52 45 20 61 3d 35 20 4f 52   t1 WHERE a=5 OR
2c90f 20 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 41 4e   b=7 OR (c=11 AN
2c910 44 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a 0a 20  D d=13).    **. 
2c911 20 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 78 61     ** In the exa
2c912 6d 70 6c 65 2c 20 74 68 65 72 65 20 61 72 65 20  mple, there are 
2c913 74 68 72 65 65 20 69 6e 64 65 78 65 64 20 74 65  three indexed te
2c914 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  rms connected by
2c915 20 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20   OR..    ** The 
2c916 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  top of the loop 
2c917 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
2c918 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2c919 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20 20          Null    
2c91a 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20     1            
2c91b 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20 72      # Zero the r
2c91c 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 0a 20  owset in reg 1. 
2c91d 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
2c91e 6e 2c 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  n, for each inde
2c91f 78 65 64 20 74 65 72 6d 2c 20 74 68 65 20 66 6f  xed term, the fo
2c920 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 72 67  llowing. The arg
2c921 75 6d 65 6e 74 73 20 74 6f 0a 20 20 20 20 2a 2a  uments to.    **
2c922 20 52 6f 77 53 65 74 54 65 73 74 20 61 72 65 20   RowSetTest are 
2c923 73 75 63 68 20 74 68 61 74 20 74 68 65 20 72 6f  such that the ro
2c924 77 69 64 20 6f 66 20 74 68 65 20 63 75 72 72 65  wid of the curre
2c925 6e 74 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74  nt row is insert
2c926 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74  ed.    ** into t
2c927 68 65 20 52 6f 77 53 65 74 2e 20 49 66 20 69 74  he RowSet. If it
2c928 20 69 73 20 61 6c 72 65 61 64 79 20 70 72 65 73   is already pres
2c929 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73 6b 69  ent, control ski
2c92a 70 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 47 6f  ps the.    ** Go
2c92b 73 75 62 20 6f 70 63 6f 64 65 20 61 6e 64 20 6a  sub opcode and j
2c92c 75 6d 70 73 20 73 74 72 61 69 67 68 74 20 74 6f  umps straight to
2c92d 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
2c92e 74 65 64 20 62 79 20 57 68 65 72 65 45 6e 64 28  ted by WhereEnd(
2c92f 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  )..    **.    **
2c930 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57          sqlite3W
2c931 68 65 72 65 42 65 67 69 6e 28 3c 74 65 72 6d 3e  hereBegin(<term>
2c932 29 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ).    **        
2c933 20 20 52 6f 77 53 65 74 54 65 73 74 20 20 20 20    RowSetTest    
2c934 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20                # 
2c935 49 6e 73 65 72 74 20 72 6f 77 69 64 20 69 6e 74  Insert rowid int
2c936 6f 20 72 6f 77 73 65 74 0a 20 20 20 20 2a 2a 20  o rowset.    ** 
2c937 20 20 20 20 20 20 20 20 20 47 6f 73 75 62 20 20           Gosub  
2c938 20 20 20 20 32 20 41 0a 20 20 20 20 2a 2a 20 20      2 A.    **  
2c939 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68 65        sqlite3Whe
2c93a 72 65 45 6e 64 28 29 0a 20 20 20 20 2a 2a 0a 20  reEnd().    **. 
2c93b 20 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20     ** Following 
2c93c 74 68 65 20 61 62 6f 76 65 2c 20 63 6f 64 65 20  the above, code 
2c93d 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  to terminate the
2c93e 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 41 2c 20   loop. Label A, 
2c93f 74 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 2a  the target.    *
2c940 2a 20 6f 66 20 74 68 65 20 47 6f 73 75 62 20 61  * of the Gosub a
2c941 62 6f 76 65 2c 20 6a 75 6d 70 73 20 74 6f 20 74  bove, jumps to t
2c942 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 72  he instruction r
2c943 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20 47  ight after the G
2c944 6f 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  oto..    **.    
2c945 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c 6c  **          Null
2c946 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20         1        
2c947 20 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20 74          # Zero t
2c948 68 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65 67  he rowset in reg
2c949 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   1.    **       
2c94a 20 20 20 47 6f 74 6f 20 20 20 20 20 20 20 42 20     Goto       B 
2c94b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23                 #
2c94c 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 66 69 6e   The loop is fin
2c94d 69 73 68 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  ished..    **.  
2c94e 20 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 3c 6c    **       A: <l
2c94f 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 20 20  oop body>       
2c950 20 20 20 20 20 20 20 20 20 20 23 20 52 65 74 75            # Retu
2c951 72 6e 20 64 61 74 61 2c 20 77 68 61 74 65 76 65  rn data, whateve
2c952 72 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  r..    **.    **
2c953 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e            Return
2c954 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20       2          
2c955 20 20 20 20 20 20 23 20 4a 75 6d 70 20 62 61 63        # Jump bac
2c956 6b 20 74 6f 20 74 68 65 20 47 6f 73 75 62 0a 20  k to the Gosub. 
2c957 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
2c958 20 20 20 42 3a 20 3c 61 66 74 65 72 20 74 68 65     B: <after the
2c959 20 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a 0a 20 20   loop>.    **.  
2c95a 20 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 43 6c    */.    WhereCl
2c95b 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20  ause *pOrWc;    
2c95c 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75 73 65  /* The OR-clause
2c95d 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e 74 6f   broken out into
2c95e 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 20   subterms */.   
2c95f 20 57 68 65 72 65 54 65 72 6d 20 2a 70 46 69 6e   WhereTerm *pFin
2c960 61 6c 3b 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c  al;     /* Final
2c961 20 73 75 62 74 65 72 6d 20 77 69 74 68 69 6e 20   subterm within 
2c962 74 68 65 20 4f 52 2d 63 6c 61 75 73 65 2e 20 2a  the OR-clause. *
2c963 2f 0a 20 20 20 20 53 72 63 4c 69 73 74 20 6f 6e  /.    SrcList on
2c964 65 54 61 62 3b 20 20 20 20 20 20 20 20 2f 2a 20  eTab;        /* 
2c965 53 68 6f 72 74 65 6e 65 64 20 74 61 62 6c 65 20  Shortened table 
2c966 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 69 6e 74  list */..    int
2c967 20 72 65 67 52 65 74 75 72 6e 20 3d 20 2b 2b 70   regReturn = ++p
2c968 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20  Parse->nMem;    
2c969 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
2c96a 65 72 20 75 73 65 64 20 77 69 74 68 20 4f 50 5f  er used with OP_
2c96b 47 6f 73 75 62 20 2a 2f 0a 20 20 20 20 69 6e 74  Gosub */.    int
2c96c 20 72 65 67 52 6f 77 73 65 74 3b 20 20 20 20 20   regRowset;     
2c96d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c96e 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
2c96f 65 72 20 66 6f 72 20 52 6f 77 53 65 74 20 6f 62  er for RowSet ob
2c970 6a 65 63 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ject */.    int 
2c971 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20  regRowid;       
2c972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c973 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
2c974 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 20  r holding rowid 
2c975 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c 6f 6f 70  */.    int iLoop
2c976 42 6f 64 79 20 3d 20 73 71 6c 69 74 65 33 56 64  Body = sqlite3Vd
2c977 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20  beMakeLabel(v); 
2c978 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6c 6f 6f   /* Start of loo
2c979 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 69 6e  p body */.    in
2c97a 74 20 69 52 65 74 49 6e 69 74 3b 20 20 20 20 20  t iRetInit;     
2c97b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c97c 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2c97d 73 73 20 6f 66 20 72 65 67 52 65 74 75 72 6e 20  ss of regReturn 
2c97e 69 6e 69 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  init */.    int 
2c97f 69 69 3b 0a 20 20 20 0a 20 20 20 20 70 54 65 72  ii;.   .    pTer
2c980 6d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  m = pLevel->plan
2c981 2e 75 2e 70 54 65 72 6d 3b 0a 20 20 20 20 61 73  .u.pTerm;.    as
2c982 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29  sert( pTerm!=0 )
2c983 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
2c984 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
2c985 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61 73 73  WO_OR );.    ass
2c986 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46  ert( (pTerm->wtF
2c987 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e  lags & TERM_ORIN
2c988 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f  FO)!=0 );.    pO
2c989 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e  rWc = &pTerm->u.
2c98a 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20  pOrInfo->wc;.   
2c98b 20 70 46 69 6e 61 6c 20 3d 20 26 70 4f 72 57 63   pFinal = &pOrWc
2c98c 2d 3e 61 5b 70 4f 72 57 63 2d 3e 6e 54 65 72 6d  ->a[pOrWc->nTerm
2c98d 2d 31 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74  -1];..    /* Set
2c98e 20 75 70 20 61 20 53 72 63 4c 69 73 74 20 63 6f   up a SrcList co
2c98f 6e 74 61 69 6e 69 6e 67 20 6a 75 73 74 20 74 68  ntaining just th
2c990 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 73 63  e table being sc
2c991 61 6e 6e 65 64 20 62 79 20 74 68 69 73 20 6c 6f  anned by this lo
2c992 6f 70 2e 20 2a 2f 0a 20 20 20 20 6f 6e 65 54 61  op. */.    oneTa
2c993 62 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20  b.nSrc = 1;.    
2c994 6f 6e 65 54 61 62 2e 6e 41 6c 6c 6f 63 20 3d 20  oneTab.nAlloc = 
2c995 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62 2e 61 5b  1;.    oneTab.a[
2c996 30 5d 20 3d 20 2a 70 54 61 62 49 74 65 6d 3b 0a  0] = *pTabItem;.
2c997 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
2c998 7a 65 20 74 68 65 20 72 6f 77 73 65 74 20 72 65  ze the rowset re
2c999 67 69 73 74 65 72 20 74 6f 20 63 6f 6e 74 61 69  gister to contai
2c99a 6e 20 4e 55 4c 4c 2e 20 41 6e 20 53 51 4c 20 4e  n NULL. An SQL N
2c99b 55 4c 4c 20 69 73 20 0a 20 20 20 20 2a 2a 20 65  ULL is .    ** e
2c99c 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 6e 20  quivalent to an 
2c99d 65 6d 70 74 79 20 72 6f 77 73 65 74 2e 0a 20 20  empty rowset..  
2c99e 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f    **.    ** Also
2c99f 20 69 6e 69 74 69 61 6c 69 7a 65 20 72 65 67 52   initialize regR
2c9a0 65 74 75 72 6e 20 74 6f 20 63 6f 6e 74 61 69 6e  eturn to contain
2c9a1 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
2c9a2 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
2c9a3 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74  .    ** immediat
2c9a4 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ely following th
2c9a5 65 20 4f 50 5f 52 65 74 75 72 6e 20 61 74 20 74  e OP_Return at t
2c9a6 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65  he bottom of the
2c9a7 20 6c 6f 6f 70 2e 20 54 68 69 73 0a 20 20 20 20   loop. This.    
2c9a8 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 69  ** is required i
2c9a9 6e 20 61 20 66 65 77 20 6f 62 73 63 75 72 65 20  n a few obscure 
2c9aa 4c 45 46 54 20 4a 4f 49 4e 20 63 61 73 65 73 20  LEFT JOIN cases 
2c9ab 77 68 65 72 65 20 63 6f 6e 74 72 6f 6c 20 6a 75  where control ju
2c9ac 6d 70 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 20  mps.    ** over 
2c9ad 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c  the top of the l
2c9ae 6f 6f 70 20 69 6e 74 6f 20 74 68 65 20 62 6f 64  oop into the bod
2c9af 79 20 6f 66 20 69 74 2e 20 49 6e 20 74 68 69 73  y of it. In this
2c9b0 20 63 61 73 65 20 74 68 65 20 0a 20 20 20 20 2a   case the .    *
2c9b1 2a 20 63 6f 72 72 65 63 74 20 72 65 73 70 6f 6e  * correct respon
2c9b2 73 65 20 66 6f 72 20 74 68 65 20 65 6e 64 2d 6f  se for the end-o
2c9b3 66 2d 6c 6f 6f 70 20 63 6f 64 65 20 28 74 68 65  f-loop code (the
2c9b4 20 4f 50 5f 52 65 74 75 72 6e 29 20 69 73 20 74   OP_Return) is t
2c9b5 6f 20 0a 20 20 20 20 2a 2a 20 66 61 6c 6c 20 74  o .    ** fall t
2c9b6 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
2c9b7 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20  xt instruction, 
2c9b8 6a 75 73 74 20 61 73 20 61 6e 20 4f 50 5f 4e 65  just as an OP_Ne
2c9b9 78 74 20 64 6f 65 73 20 69 66 0a 20 20 20 20 2a  xt does if.    *
2c9ba 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 75  * called on an u
2c9bb 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 75 72  ninitialized cur
2c9bc 73 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  sor..    */.    
2c9bd 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20  if( (wctrlFlags 
2c9be 26 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  & WHERE_DUPLICAT
2c9bf 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20 20  ES_OK)==0 ){.   
2c9c0 20 20 20 72 65 67 52 6f 77 73 65 74 20 3d 20 2b     regRowset = +
2c9c1 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
2c9c2 20 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20       regRowid = 
2c9c3 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2c9c4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c9c5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2c9c6 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 73 65 74  ll, 0, regRowset
2c9c7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 52 65  );.    }.    iRe
2c9c8 74 49 6e 69 74 20 3d 20 73 71 6c 69 74 65 33 56  tInit = sqlite3V
2c9c9 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c9ca 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52  Integer, 0, regR
2c9cb 65 74 75 72 6e 29 3b 0a 0a 20 20 20 20 66 6f 72  eturn);..    for
2c9cc 28 69 69 3d 30 3b 20 69 69 3c 70 4f 72 57 63 2d  (ii=0; ii<pOrWc-
2c9cd 3e 6e 54 65 72 6d 3b 20 69 69 2b 2b 29 7b 0a 20  >nTerm; ii++){. 
2c9ce 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
2c9cf 70 4f 72 54 65 72 6d 20 3d 20 26 70 4f 72 57 63  pOrTerm = &pOrWc
2c9d0 2d 3e 61 5b 69 69 5d 3b 0a 20 20 20 20 20 20 69  ->a[ii];.      i
2c9d1 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74  f( pOrTerm->left
2c9d2 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 7c 7c 20  Cursor==iCur || 
2c9d3 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
2c9d4 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20 20  or==WO_AND ){.  
2c9d5 20 20 20 20 20 20 57 68 65 72 65 49 6e 66 6f 20        WhereInfo 
2c9d6 2a 70 53 75 62 57 49 6e 66 6f 3b 20 20 20 20 20  *pSubWInfo;     
2c9d7 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 66 6f 72       /* Info for
2c9d8 20 73 69 6e 67 6c 65 20 4f 52 2d 74 65 72 6d 20   single OR-term 
2c9d9 73 63 61 6e 20 2a 2f 0a 0a 20 20 20 20 20 20 20  scan */..       
2c9da 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
2c9db 20 74 61 62 6c 65 20 65 6e 74 72 69 65 73 20 74   table entries t
2c9dc 68 61 74 20 6d 61 74 63 68 20 74 65 72 6d 20 70  hat match term p
2c9dd 4f 72 54 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20  OrTerm. */.     
2c9de 20 20 20 70 53 75 62 57 49 6e 66 6f 20 3d 20 73     pSubWInfo = s
2c9df 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2c9e0 28 70 50 61 72 73 65 2c 20 26 6f 6e 65 54 61 62  (pParse, &oneTab
2c9e1 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  , pOrTerm->pExpr
2c9e2 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2c9e3 20 20 20 20 20 20 20 20 20 20 20 20 20 57 48 45               WHE
2c9e4 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 7c 20 57  RE_OMIT_OPEN | W
2c9e5 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20  HERE_OMIT_CLOSE 
2c9e6 7c 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41  | WHERE_FORCE_TA
2c9e7 42 4c 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66  BLE);.        if
2c9e8 28 20 70 53 75 62 57 49 6e 66 6f 20 29 7b 0a 20  ( pSubWInfo ){. 
2c9e9 20 20 20 20 20 20 20 20 20 69 66 28 20 28 77 63           if( (wc
2c9ea 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
2c9eb 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3d  _DUPLICATES_OK)=
2c9ec 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2c9ed 20 20 69 6e 74 20 69 53 65 74 20 3d 20 28 28 69    int iSet = ((i
2c9ee 69 3d 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d  i==pOrWc->nTerm-
2c9ef 31 29 3f 2d 31 3a 69 69 29 3b 0a 20 20 20 20 20  1)?-1:ii);.     
2c9f0 20 20 20 20 20 20 20 69 6e 74 20 72 3b 0a 20 20         int r;.  
2c9f1 20 20 20 20 20 20 20 20 20 20 72 20 3d 20 73 71            r = sq
2c9f2 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
2c9f3 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
2c9f4 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2c 20 2d  TabItem->pTab, -
2c9f5 31 2c 20 69 43 75 72 2c 20 0a 20 20 20 20 20 20  1, iCur, .      
2c9f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9f8 20 20 20 72 65 67 52 6f 77 69 64 2c 20 30 29 3b     regRowid, 0);
2c9f9 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2c9fa 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2c9fb 2c 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 2c  , OP_RowSetTest,
2c9fc 20 72 65 67 52 6f 77 73 65 74 2c 20 0a 20 20 20   regRowset, .   
2c9fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9fe 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2c9ff 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2ca00 72 28 76 29 2b 32 2c 0a 20 20 20 20 20 20 20 20  r(v)+2,.        
2ca01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca02 20 20 20 20 20 20 72 2c 20 53 51 4c 49 54 45 5f        r, SQLITE_
2ca03 49 4e 54 5f 54 4f 5f 50 54 52 28 69 53 65 74 29  INT_TO_PTR(iSet)
2ca04 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20  , P4_INT32);.   
2ca05 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2ca06 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2ca07 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
2ca08 2c 20 72 65 67 52 65 74 75 72 6e 2c 20 69 4c 6f  , regReturn, iLo
2ca09 6f 70 42 6f 64 79 29 3b 0a 0a 20 20 20 20 20 20  opBody);..      
2ca0a 20 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 74 68      /* Finish th
2ca0b 65 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  e loop through t
2ca0c 61 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61  able entries tha
2ca0d 74 20 6d 61 74 63 68 20 74 65 72 6d 20 70 4f 72  t match term pOr
2ca0e 54 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20  Term. */.       
2ca0f 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45     sqlite3WhereE
2ca10 6e 64 28 70 53 75 62 57 49 6e 66 6f 29 3b 0a 20  nd(pSubWInfo);. 
2ca11 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2ca12 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2ca13 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
2ca14 2c 20 69 52 65 74 49 6e 69 74 2c 20 73 71 6c 69  , iRetInit, sqli
2ca15 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2ca16 64 72 28 76 29 29 3b 0a 20 20 20 20 2f 2a 20 73  dr(v));.    /* s
2ca17 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2ca18 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2ca19 72 65 67 52 6f 77 73 65 74 29 3b 20 2a 2f 0a 20  regRowset); */. 
2ca1a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2ca1b 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2ca1c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72   0, pLevel->addr
2ca1d 42 72 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Brk);.    sqlite
2ca1e 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2ca1f 6c 28 76 2c 20 69 4c 6f 6f 70 42 6f 64 79 29 3b  l(v, iLoopBody);
2ca20 0a 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70  ..    pLevel->op
2ca21 20 3d 20 4f 50 5f 52 65 74 75 72 6e 3b 0a 20 20   = OP_Return;.  
2ca22 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 72    pLevel->p1 = r
2ca23 65 67 52 65 74 75 72 6e 3b 0a 20 20 20 20 64 69  egReturn;.    di
2ca24 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
2ca25 2c 20 70 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73  , pTerm);.  }els
2ca26 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
2ca27 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
2ca28 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 7b 0a  IZATION */..  {.
2ca29 20 20 20 20 2f 2a 20 43 61 73 65 20 35 3a 20 20      /* Case 5:  
2ca2a 54 68 65 72 65 20 69 73 20 6e 6f 20 75 73 61 62  There is no usab
2ca2b 6c 65 20 69 6e 64 65 78 2e 20 20 57 65 20 6d 75  le index.  We mu
2ca2c 73 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65  st do a complete
2ca2d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2ca2e 20 73 63 61 6e 20 6f 66 20 74 68 65 20 65 6e 74   scan of the ent
2ca2f 69 72 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a  ire table..    *
2ca30 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  /.    static con
2ca31 73 74 20 75 38 20 61 53 74 65 70 5b 5d 20 3d 20  st u8 aStep[] = 
2ca32 7b 20 4f 50 5f 4e 65 78 74 2c 20 4f 50 5f 50 72  { OP_Next, OP_Pr
2ca33 65 76 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 63  ev };.    static
2ca34 20 63 6f 6e 73 74 20 75 38 20 61 53 74 61 72 74   const u8 aStart
2ca35 5b 5d 20 3d 20 7b 20 4f 50 5f 52 65 77 69 6e 64  [] = { OP_Rewind
2ca36 2c 20 4f 50 5f 4c 61 73 74 20 7d 3b 0a 20 20 20  , OP_Last };.   
2ca37 20 61 73 73 65 72 74 28 20 62 52 65 76 3d 3d 30   assert( bRev==0
2ca38 20 7c 7c 20 62 52 65 76 3d 3d 31 20 29 3b 0a 20   || bRev==1 );. 
2ca39 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54     assert( omitT
2ca3a 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70  able==0 );.    p
2ca3b 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 61 53 74 65  Level->op = aSte
2ca3c 70 5b 62 52 65 76 5d 3b 0a 20 20 20 20 70 4c 65  p[bRev];.    pLe
2ca3d 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a  vel->p1 = iCur;.
2ca3e 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d      pLevel->p2 =
2ca3f 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65   1 + sqlite3Vdbe
2ca40 41 64 64 4f 70 32 28 76 2c 20 61 53 74 61 72 74  AddOp2(v, aStart
2ca41 5b 62 52 65 76 5d 2c 20 69 43 75 72 2c 20 61 64  [bRev], iCur, ad
2ca42 64 72 42 72 6b 29 3b 0a 20 20 20 20 70 4c 65 76  drBrk);.    pLev
2ca43 65 6c 2d 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f  el->p5 = SQLITE_
2ca44 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53  STMTSTATUS_FULLS
2ca45 43 41 4e 5f 53 54 45 50 3b 0a 20 20 7d 0a 20 20  CAN_STEP;.  }.  
2ca46 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65 74  notReady &= ~get
2ca47 4d 61 73 6b 28 70 57 43 2d 3e 70 4d 61 73 6b 53  Mask(pWC->pMaskS
2ca48 65 74 2c 20 69 43 75 72 29 3b 0a 0a 20 20 2f 2a  et, iCur);..  /*
2ca49 20 49 6e 73 65 72 74 20 63 6f 64 65 20 74 6f 20   Insert code to 
2ca4a 74 65 73 74 20 65 76 65 72 79 20 73 75 62 65 78  test every subex
2ca4b 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 61  pression that ca
2ca4c 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a  n be completely.
2ca4d 20 20 2a 2a 20 63 6f 6d 70 75 74 65 64 20 75 73    ** computed us
2ca4e 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20  ing the current 
2ca4f 73 65 74 20 6f 66 20 74 61 62 6c 65 73 2e 0a 20  set of tables.. 
2ca50 20 2a 2f 0a 20 20 6b 20 3d 20 30 3b 0a 20 20 66   */.  k = 0;.  f
2ca51 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c  or(pTerm=pWC->a,
2ca52 20 6a 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a   j=pWC->nTerm; j
2ca53 3e 30 3b 20 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b  >0; j--, pTerm++
2ca54 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 3b  ){.    Expr *pE;
2ca55 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
2ca56 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2ca57 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a  TERM_VIRTUAL );.
2ca58 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
2ca59 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2ca5a 45 52 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20  ERM_CODED );.   
2ca5b 20 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c   if( pTerm->wtFl
2ca5c 61 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54  ags & (TERM_VIRT
2ca5d 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20  UAL|TERM_CODED) 
2ca5e 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2ca5f 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72  if( (pTerm->prer
2ca60 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79  eqAll & notReady
2ca61 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )!=0 ) continue;
2ca62 0a 20 20 20 20 70 45 20 3d 20 70 54 65 72 6d 2d  .    pE = pTerm-
2ca63 3e 70 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65  >pExpr;.    asse
2ca64 72 74 28 20 70 45 21 3d 30 20 29 3b 0a 20 20 20  rt( pE!=0 );.   
2ca65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65   if( pLevel->iLe
2ca66 66 74 4a 6f 69 6e 20 26 26 20 21 45 78 70 72 48  ftJoin && !ExprH
2ca67 61 73 50 72 6f 70 65 72 74 79 28 70 45 2c 20 45  asProperty(pE, E
2ca68 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20  P_FromJoin) ){. 
2ca69 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2ca6a 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2ca6b 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
2ca6c 73 65 2c 20 70 45 2c 20 61 64 64 72 43 6f 6e 74  se, pE, addrCont
2ca6d 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
2ca6e 55 4c 4c 29 3b 0a 20 20 20 20 6b 20 3d 20 31 3b  ULL);.    k = 1;
2ca6f 0a 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c  .    pTerm->wtFl
2ca70 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45  ags |= TERM_CODE
2ca71 44 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72  D;.  }..  /* For
2ca72 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f   a LEFT OUTER JO
2ca73 49 4e 2c 20 67 65 6e 65 72 61 74 65 20 63 6f 64  IN, generate cod
2ca74 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65 63 6f  e that will reco
2ca75 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74  rd the fact that
2ca76 0a 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f  .  ** at least o
2ca77 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 69  ne row of the ri
2ca78 67 68 74 20 74 61 62 6c 65 20 68 61 73 20 6d 61  ght table has ma
2ca79 74 63 68 65 64 20 74 68 65 20 6c 65 66 74 20 74  tched the left t
2ca7a 61 62 6c 65 2e 20 20 0a 20 20 2a 2f 0a 20 20 69  able.  .  */.  i
2ca7b 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  f( pLevel->iLeft
2ca7c 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 70 4c 65 76  Join ){.    pLev
2ca7d 65 6c 2d 3e 61 64 64 72 46 69 72 73 74 20 3d 20  el->addrFirst = 
2ca7e 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2ca7f 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73  ntAddr(v);.    s
2ca80 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2ca81 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2ca82 31 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  1, pLevel->iLeft
2ca83 4a 6f 69 6e 29 3b 0a 20 20 20 20 56 64 62 65 43  Join);.    VdbeC
2ca84 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 63 6f  omment((v, "reco
2ca85 72 64 20 4c 45 46 54 20 4a 4f 49 4e 20 68 69 74  rd LEFT JOIN hit
2ca86 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "));.    sqlite3
2ca87 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
2ca88 50 61 72 73 65 29 3b 0a 20 20 20 20 66 6f 72 28  Parse);.    for(
2ca89 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d  pTerm=pWC->a, j=
2ca8a 30 3b 20 6a 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b  0; j<pWC->nTerm;
2ca8b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   j++, pTerm++){.
2ca8c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ca8d 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
2ca8e 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b   TERM_VIRTUAL );
2ca8f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ca90 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2ca91 26 20 54 45 52 4d 5f 43 4f 44 45 44 20 29 3b 0a  & TERM_CODED );.
2ca92 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d        if( pTerm-
2ca93 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d  >wtFlags & (TERM
2ca94 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f  _VIRTUAL|TERM_CO
2ca95 44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b  DED) ) continue;
2ca96 0a 20 20 20 20 20 20 69 66 28 20 28 70 54 65 72  .      if( (pTer
2ca97 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e  m->prereqAll & n
2ca98 6f 74 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f  otReady)!=0 ) co
2ca99 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 61 73  ntinue;.      as
2ca9a 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70 45 78  sert( pTerm->pEx
2ca9b 70 72 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  pr );.      sqli
2ca9c 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
2ca9d 50 61 72 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45  Parse, pTerm->pE
2ca9e 78 70 72 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53  xpr, addrCont, S
2ca9f 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
2caa0 29 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  );.      pTerm->
2caa1 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2caa2 43 4f 44 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d  CODED;.    }.  }
2caa3 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2caa4 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2caa5 20 69 52 65 6c 65 61 73 65 52 65 67 29 3b 0a 0a   iReleaseReg);..
2caa6 20 20 72 65 74 75 72 6e 20 6e 6f 74 52 65 61 64    return notRead
2caa7 79 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65  y;.}..#if define
2caa8 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f  d(SQLITE_TEST)./
2caa9 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
2caaa 6e 67 20 76 61 72 69 61 62 6c 65 20 68 6f 6c 64  ng variable hold
2caab 73 20 61 20 74 65 78 74 20 64 65 73 63 72 69 70  s a text descrip
2caac 74 69 6f 6e 20 6f 66 20 71 75 65 72 79 20 70 6c  tion of query pl
2caad 61 6e 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20  an generated.** 
2caae 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  by the most rece
2caaf 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  nt call to sqlit
2cab0 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 2e 20  e3WhereBegin(). 
2cab1 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 57 68   Each call to Wh
2cab2 65 72 65 42 65 67 69 6e 0a 2a 2a 20 6f 76 65 72  ereBegin.** over
2cab3 77 72 69 74 65 73 20 74 68 65 20 70 72 65 76 69  writes the previ
2cab4 6f 75 73 2e 20 20 54 68 69 73 20 69 6e 66 6f 72  ous.  This infor
2cab5 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66  mation is used f
2cab6 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 0a 2a  or testing and.*
2cab7 2a 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e  * analysis only.
2cab8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
2cab9 68 61 72 20 73 71 6c 69 74 65 33 5f 71 75 65 72  har sqlite3_quer
2caba 79 5f 70 6c 61 6e 5b 42 4d 53 2a 32 2a 34 30 5d  y_plan[BMS*2*40]
2cabb 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68  ;  /* Text of th
2cabc 65 20 6a 6f 69 6e 20 2a 2f 0a 73 74 61 74 69 63  e join */.static
2cabd 20 69 6e 74 20 6e 51 50 6c 61 6e 20 3d 20 30 3b   int nQPlan = 0;
2cabe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2cabf 20 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 77 20   Next free slow 
2cac0 69 6e 20 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 5d  in _query_plan[]
2cac1 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53   */..#endif /* S
2cac2 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a  QLITE_TEST */...
2cac3 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 57 68 65  /*.** Free a Whe
2cac4 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  reInfo structure
2cac5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2cac6 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28 73 71  whereInfoFree(sq
2cac7 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72 65  lite3 *db, Where
2cac8 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20  Info *pWInfo){. 
2cac9 20 69 66 28 20 70 57 49 6e 66 6f 20 29 7b 0a 20   if( pWInfo ){. 
2caca 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
2cacb 72 28 69 3d 30 3b 20 69 3c 70 57 49 6e 66 6f 2d  r(i=0; i<pWInfo-
2cacc 3e 6e 4c 65 76 65 6c 3b 20 69 2b 2b 29 7b 0a 20  >nLevel; i++){. 
2cacd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64       sqlite3_ind
2cace 65 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d  ex_info *pInfo =
2cacf 20 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 49   pWInfo->a[i].pI
2cad0 64 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66  dxInfo;.      if
2cad1 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20  ( pInfo ){.     
2cad2 20 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70 49     /* assert( pI
2cad3 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
2cad4 64 78 53 74 72 3d 3d 30 20 7c 7c 20 64 62 2d 3e  dxStr==0 || db->
2cad5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 20  mallocFailed ); 
2cad6 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  */.        if( p
2cad7 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65  Info->needToFree
2cad8 49 64 78 53 74 72 20 29 7b 0a 20 20 20 20 20 20  IdxStr ){.      
2cad9 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2cada 28 70 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b  (pInfo->idxStr);
2cadb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2cadc 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2cadd 28 64 62 2c 20 70 49 6e 66 6f 29 3b 0a 20 20 20  (db, pInfo);.   
2cade 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77     }.    }.    w
2cadf 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28  hereClauseClear(
2cae0 70 57 49 6e 66 6f 2d 3e 70 57 43 29 3b 0a 20 20  pWInfo->pWC);.  
2cae1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2cae2 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20 20 7d  db, pWInfo);.  }
2cae3 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  .}.../*.** Gener
2cae4 61 74 65 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  ate the beginnin
2cae5 67 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 75 73  g of the loop us
2cae6 65 64 20 66 6f 72 20 57 48 45 52 45 20 63 6c 61  ed for WHERE cla
2cae7 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a  use processing..
2cae8 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
2cae9 6c 75 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72  lue is a pointer
2caea 20 74 6f 20 61 6e 20 6f 70 61 71 75 65 20 73 74   to an opaque st
2caeb 72 75 63 74 75 72 65 20 74 68 61 74 20 63 6f 6e  ructure that con
2caec 74 61 69 6e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61  tains.** informa
2caed 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 74  tion needed to t
2caee 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f  erminate the loo
2caef 70 2e 20 20 4c 61 74 65 72 2c 20 74 68 65 20 63  p.  Later, the c
2caf0 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 0a 2a  alling routine.*
2caf1 2a 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20  * should invoke 
2caf2 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
2caf3 29 20 77 69 74 68 20 74 68 65 20 72 65 74 75 72  ) with the retur
2caf4 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  n value of this 
2caf5 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 6f  function.** in o
2caf6 72 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65  rder to complete
2caf7 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2caf8 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a  e processing..**
2caf9 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
2cafa 6f 63 63 75 72 73 2c 20 74 68 69 73 20 72 6f 75  occurs, this rou
2cafb 74 69 6e 65 20 72 65 74 75 72 6e 73 20 4e 55 4c  tine returns NUL
2cafc 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73  L..**.** The bas
2cafd 69 63 20 69 64 65 61 20 69 73 20 74 6f 20 64 6f  ic idea is to do
2cafe 20 61 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2c 20   a nested loop, 
2caff 6f 6e 65 20 6c 6f 6f 70 20 66 6f 72 20 65 61 63  one loop for eac
2cb00 68 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68  h table in.** th
2cb01 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
2cb02 20 61 20 73 65 6c 65 63 74 2e 20 20 28 49 4e 53   a select.  (INS
2cb03 45 52 54 20 61 6e 64 20 55 50 44 41 54 45 20 73  ERT and UPDATE s
2cb04 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74 68  tatements are th
2cb05 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20 61 20 53  e.** same as a S
2cb06 45 4c 45 43 54 20 77 69 74 68 20 6f 6e 6c 79 20  ELECT with only 
2cb07 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69  a single table i
2cb08 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
2cb09 65 2e 29 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  e.)  For.** exam
2cb0a 70 6c 65 2c 20 69 66 20 74 68 65 20 53 51 4c 20  ple, if the SQL 
2cb0b 69 73 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  is this:.**.**  
2cb0c 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52       SELECT * FR
2cb0d 4f 4d 20 74 31 2c 20 74 32 2c 20 74 33 20 57 48  OM t1, t2, t3 WH
2cb0e 45 52 45 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54  ERE ...;.**.** T
2cb0f 68 65 6e 20 74 68 65 20 63 6f 64 65 20 67 65 6e  hen the code gen
2cb10 65 72 61 74 65 64 20 69 73 20 63 6f 6e 63 65 70  erated is concep
2cb11 74 75 61 6c 6c 79 20 6c 69 6b 65 20 74 68 65 20  tually like the 
2cb12 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
2cb13 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f        foreach ro
2cb14 77 31 20 69 6e 20 74 31 20 64 6f 20 20 20 20 20  w1 in t1 do     
2cb15 20 20 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65    \    Code gene
2cb16 72 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  rated.**        
2cb17 66 6f 72 65 61 63 68 20 72 6f 77 32 20 69 6e 20  foreach row2 in 
2cb18 74 32 20 64 6f 20 20 20 20 20 20 7c 2d 2d 20 62  t2 do      |-- b
2cb19 79 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  y sqlite3WhereBe
2cb1a 67 69 6e 28 29 0a 2a 2a 20 20 20 20 20 20 20 20  gin().**        
2cb1b 20 20 66 6f 72 65 61 63 68 20 72 6f 77 33 20 69    foreach row3 i
2cb1c 6e 20 74 33 20 64 6f 20 20 20 2f 0a 2a 2a 20 20  n t3 do   /.**  
2cb1d 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a            ....**
2cb1e 20 20 20 20 20 20 20 20 20 20 65 6e 64 20 20 20            end   
2cb1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb20 20 20 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65    \    Code gene
2cb21 72 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  rated.**        
2cb22 65 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  end             
2cb23 20 20 20 20 20 20 20 20 20 20 20 7c 2d 2d 20 62             |-- b
2cb24 79 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e  y sqlite3WhereEn
2cb25 64 28 29 0a 2a 2a 20 20 20 20 20 20 65 6e 64 20  d().**      end 
2cb26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb27 20 20 20 20 20 20 20 20 2f 0a 2a 2a 0a 2a 2a 20          /.**.** 
2cb28 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6c 6f  Note that the lo
2cb29 6f 70 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65  ops might not be
2cb2a 20 6e 65 73 74 65 64 20 69 6e 20 74 68 65 20 6f   nested in the o
2cb2b 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 74 68  rder in which th
2cb2c 65 79 0a 2a 2a 20 61 70 70 65 61 72 20 69 6e 20  ey.** appear in 
2cb2d 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2cb2e 69 66 20 61 20 64 69 66 66 65 72 65 6e 74 20 6f  if a different o
2cb2f 72 64 65 72 20 69 73 20 62 65 74 74 65 72 20 61  rder is better a
2cb30 62 6c 65 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 75  ble to make.** u
2cb31 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20  se of indices.  
2cb32 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 77  Note also that w
2cb33 68 65 6e 20 74 68 65 20 49 4e 20 6f 70 65 72 61  hen the IN opera
2cb34 74 6f 72 20 61 70 70 65 61 72 73 20 69 6e 0a 2a  tor appears in.*
2cb35 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  * the WHERE clau
2cb36 73 65 2c 20 69 74 20 6d 69 67 68 74 20 72 65 73  se, it might res
2cb37 75 6c 74 20 69 6e 20 61 64 64 69 74 69 6f 6e 61  ult in additiona
2cb38 6c 20 6e 65 73 74 65 64 20 6c 6f 6f 70 73 20 66  l nested loops f
2cb39 6f 72 0a 2a 2a 20 73 63 61 6e 6e 69 6e 67 20 74  or.** scanning t
2cb3a 68 72 6f 75 67 68 20 61 6c 6c 20 76 61 6c 75 65  hrough all value
2cb3b 73 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68  s on the right-h
2cb3c 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20  and side of the 
2cb3d 49 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  IN..**.** There 
2cb3e 61 72 65 20 42 74 72 65 65 20 63 75 72 73 6f 72  are Btree cursor
2cb3f 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2cb40 68 20 65 61 63 68 20 74 61 62 6c 65 2e 20 20 74  h each table.  t
2cb41 31 20 75 73 65 73 20 63 75 72 73 6f 72 0a 2a 2a  1 uses cursor.**
2cb42 20 6e 75 6d 62 65 72 20 70 54 61 62 4c 69 73 74   number pTabList
2cb43 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 2e 20  ->a[0].iCursor. 
2cb44 20 74 32 20 75 73 65 73 20 74 68 65 20 63 75 72   t2 uses the cur
2cb45 73 6f 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  sor pTabList->a[
2cb46 31 5d 2e 69 43 75 72 73 6f 72 2e 0a 2a 2a 20 41  1].iCursor..** A
2cb47 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 54 68  nd so forth.  Th
2cb48 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72  is routine gener
2cb49 61 74 65 73 20 63 6f 64 65 20 74 6f 20 6f 70 65  ates code to ope
2cb4a 6e 20 74 68 6f 73 65 20 56 44 42 45 20 63 75 72  n those VDBE cur
2cb4b 73 6f 72 73 0a 2a 2a 20 61 6e 64 20 73 71 6c 69  sors.** and sqli
2cb4c 74 65 33 57 68 65 72 65 45 6e 64 28 29 20 67 65  te3WhereEnd() ge
2cb4d 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65  nerates the code
2cb4e 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 6d 2e 0a   to close them..
2cb4f 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74  **.** The code t
2cb50 68 61 74 20 73 71 6c 69 74 65 33 57 68 65 72 65  hat sqlite3Where
2cb51 42 65 67 69 6e 28 29 20 67 65 6e 65 72 61 74 65  Begin() generate
2cb52 73 20 6c 65 61 76 65 73 20 74 68 65 20 63 75 72  s leaves the cur
2cb53 73 6f 72 73 20 6e 61 6d 65 64 0a 2a 2a 20 69 6e  sors named.** in
2cb54 20 70 54 61 62 4c 69 73 74 20 70 6f 69 6e 74 69   pTabList pointi
2cb55 6e 67 20 61 74 20 74 68 65 69 72 20 61 70 70 72  ng at their appr
2cb56 6f 70 72 69 61 74 65 20 65 6e 74 72 69 65 73 2e  opriate entries.
2cb57 20 20 54 68 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65    The [...] code
2cb58 0a 2a 2a 20 63 61 6e 20 75 73 65 20 4f 50 5f 43  .** can use OP_C
2cb59 6f 6c 75 6d 6e 20 61 6e 64 20 4f 50 5f 52 6f 77  olumn and OP_Row
2cb5a 69 64 20 6f 70 63 6f 64 65 73 20 6f 6e 20 74 68  id opcodes on th
2cb5b 65 73 65 20 63 75 72 73 6f 72 73 20 74 6f 20 65  ese cursors to e
2cb5c 78 74 72 61 63 74 0a 2a 2a 20 64 61 74 61 20 66  xtract.** data f
2cb5d 72 6f 6d 20 74 68 65 20 76 61 72 69 6f 75 73 20  rom the various 
2cb5e 74 61 62 6c 65 73 20 6f 66 20 74 68 65 20 6c 6f  tables of the lo
2cb5f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  op..**.** If the
2cb60 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 73   WHERE clause is
2cb61 20 65 6d 70 74 79 2c 20 74 68 65 20 66 6f 72 65   empty, the fore
2cb62 61 63 68 20 6c 6f 6f 70 73 20 6d 75 73 74 20 65  ach loops must e
2cb63 61 63 68 20 73 63 61 6e 20 74 68 65 69 72 0a 2a  ach scan their.*
2cb64 2a 20 65 6e 74 69 72 65 20 74 61 62 6c 65 73 2e  * entire tables.
2cb65 20 20 54 68 75 73 20 61 20 74 68 72 65 65 2d 77    Thus a three-w
2cb66 61 79 20 6a 6f 69 6e 20 69 73 20 61 6e 20 4f 28  ay join is an O(
2cb67 4e 5e 33 29 20 6f 70 65 72 61 74 69 6f 6e 2e 20  N^3) operation. 
2cb68 20 42 75 74 20 69 66 0a 2a 2a 20 74 68 65 20 74   But if.** the t
2cb69 61 62 6c 65 73 20 68 61 76 65 20 69 6e 64 69 63  ables have indic
2cb6a 65 73 20 61 6e 64 20 74 68 65 72 65 20 61 72 65  es and there are
2cb6b 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 57 48   terms in the WH
2cb6c 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 0a  ERE clause that.
2cb6d 2a 2a 20 72 65 66 65 72 20 74 6f 20 74 68 6f 73  ** refer to thos
2cb6e 65 20 69 6e 64 69 63 65 73 2c 20 61 20 63 6f 6d  e indices, a com
2cb6f 70 6c 65 74 65 20 74 61 62 6c 65 20 73 63 61 6e  plete table scan
2cb70 20 63 61 6e 20 62 65 20 61 76 6f 69 64 65 64 20   can be avoided 
2cb71 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20  and the.** code 
2cb72 77 69 6c 6c 20 72 75 6e 20 6d 75 63 68 20 66 61  will run much fa
2cb73 73 74 65 72 2e 20 20 4d 6f 73 74 20 6f 66 20 74  ster.  Most of t
2cb74 68 65 20 77 6f 72 6b 20 6f 66 20 74 68 69 73 20  he work of this 
2cb75 72 6f 75 74 69 6e 65 20 69 73 20 63 68 65 63 6b  routine is check
2cb76 69 6e 67 0a 2a 2a 20 74 6f 20 73 65 65 20 69 66  ing.** to see if
2cb77 20 74 68 65 72 65 20 61 72 65 20 69 6e 64 69 63   there are indic
2cb78 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  es that can be u
2cb79 73 65 64 20 74 6f 20 73 70 65 65 64 20 75 70 20  sed to speed up 
2cb7a 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20  the loop..**.** 
2cb7b 54 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45  Terms of the WHE
2cb7c 52 45 20 63 6c 61 75 73 65 20 61 72 65 20 61 6c  RE clause are al
2cb7d 73 6f 20 75 73 65 64 20 74 6f 20 6c 69 6d 69 74  so used to limit
2cb7e 20 77 68 69 63 68 20 72 6f 77 73 20 61 63 74 75   which rows actu
2cb7f 61 6c 6c 79 0a 2a 2a 20 6d 61 6b 65 20 69 74 20  ally.** make it 
2cb80 74 6f 20 74 68 65 20 22 2e 2e 2e 22 20 69 6e 20  to the "..." in 
2cb81 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68  the middle of th
2cb82 65 20 6c 6f 6f 70 2e 20 20 41 66 74 65 72 20 65  e loop.  After e
2cb83 61 63 68 20 22 66 6f 72 65 61 63 68 22 2c 0a 2a  ach "foreach",.*
2cb84 2a 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57  * terms of the W
2cb85 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74  HERE clause that
2cb86 20 75 73 65 20 6f 6e 6c 79 20 74 65 72 6d 73 20   use only terms 
2cb87 69 6e 20 74 68 61 74 20 6c 6f 6f 70 20 61 6e 64  in that loop and
2cb88 20 6f 75 74 65 72 0a 2a 2a 20 6c 6f 6f 70 73 20   outer.** loops 
2cb89 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e  are evaluated an
2cb8a 64 20 69 66 20 66 61 6c 73 65 20 61 20 6a 75 6d  d if false a jum
2cb8b 70 20 69 73 20 6d 61 64 65 20 61 72 6f 75 6e 64  p is made around
2cb8c 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a   all subsequent.
2cb8d 2a 2a 20 69 6e 6e 65 72 20 6c 6f 6f 70 73 20 28  ** inner loops (
2cb8e 6f 72 20 61 72 6f 75 6e 64 20 74 68 65 20 22 2e  or around the ".
2cb8f 2e 2e 22 20 69 66 20 74 68 65 20 74 65 73 74 20  .." if the test 
2cb90 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68  occurs within th
2cb91 65 20 69 6e 6e 65 72 2d 0a 2a 2a 20 6d 6f 73 74  e inner-.** most
2cb92 20 6c 6f 6f 70 29 0a 2a 2a 0a 2a 2a 20 4f 55 54   loop).**.** OUT
2cb93 45 52 20 4a 4f 49 4e 53 0a 2a 2a 0a 2a 2a 20 41  ER JOINS.**.** A
2cb94 6e 20 6f 75 74 65 72 20 6a 6f 69 6e 20 6f 66 20  n outer join of 
2cb95 74 61 62 6c 65 73 20 74 31 20 61 6e 64 20 74 32  tables t1 and t2
2cb96 20 69 73 20 63 6f 6e 63 65 70 74 61 6c 6c 79 20   is conceptally 
2cb97 63 6f 64 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  coded as follows
2cb98 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 66 6f 72 65 61  :.**.**    forea
2cb99 63 68 20 72 6f 77 31 20 69 6e 20 74 31 20 64 6f  ch row1 in t1 do
2cb9a 0a 2a 2a 20 20 20 20 20 20 66 6c 61 67 20 3d 20  .**      flag = 
2cb9b 30 0a 2a 2a 20 20 20 20 20 20 66 6f 72 65 61 63  0.**      foreac
2cb9c 68 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 0a  h row2 in t2 do.
2cb9d 2a 2a 20 20 20 20 20 20 20 20 73 74 61 72 74 3a  **        start:
2cb9e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2e 2e 2e  .**          ...
2cb9f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6c 61  .**          fla
2cba0 67 20 3d 20 31 0a 2a 2a 20 20 20 20 20 20 65 6e  g = 1.**      en
2cba1 64 0a 2a 2a 20 20 20 20 20 20 69 66 20 66 6c 61  d.**      if fla
2cba2 67 3d 3d 30 20 74 68 65 6e 0a 2a 2a 20 20 20 20  g==0 then.**    
2cba3 20 20 20 20 6d 6f 76 65 20 74 68 65 20 72 6f 77      move the row
2cba4 32 20 63 75 72 73 6f 72 20 74 6f 20 61 20 6e 75  2 cursor to a nu
2cba5 6c 6c 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20  ll row.**       
2cba6 20 67 6f 74 6f 20 73 74 61 72 74 0a 2a 2a 20 20   goto start.**  
2cba7 20 20 20 20 66 69 0a 2a 2a 20 20 20 20 65 6e 64      fi.**    end
2cba8 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20  .**.** ORDER BY 
2cba9 43 4c 41 55 53 45 20 50 52 4f 43 45 53 53 49 4e  CLAUSE PROCESSIN
2cbaa 47 0a 2a 2a 0a 2a 2a 20 2a 70 70 4f 72 64 65 72  G.**.** *ppOrder
2cbab 42 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  By is a pointer 
2cbac 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  to the ORDER BY 
2cbad 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
2cbae 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a  CT statement,.**
2cbaf 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65   if there is one
2cbb0 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  .  If there is n
2cbb1 6f 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  o ORDER BY claus
2cbb2 65 20 6f 72 20 69 66 20 74 68 69 73 20 72 6f 75  e or if this rou
2cbb3 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65  tine.** is calle
2cbb4 64 20 66 72 6f 6d 20 61 6e 20 55 50 44 41 54 45  d from an UPDATE
2cbb5 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65   or DELETE state
2cbb6 6d 65 6e 74 2c 20 74 68 65 6e 20 70 70 4f 72 64  ment, then ppOrd
2cbb7 65 72 42 79 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a  erBy is NULL..**
2cbb8 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20  .** If an index 
2cbb9 63 61 6e 20 62 65 20 75 73 65 64 20 73 6f 20 74  can be used so t
2cbba 68 61 74 20 74 68 65 20 6e 61 74 75 72 61 6c 20  hat the natural 
2cbbb 6f 75 74 70 75 74 20 6f 72 64 65 72 20 6f 66 20  output order of 
2cbbc 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 73 63 61  the table.** sca
2cbbd 6e 20 69 73 20 63 6f 72 72 65 63 74 20 66 6f 72  n is correct for
2cbbe 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2cbbf 61 75 73 65 2c 20 74 68 65 6e 20 74 68 61 74 20  ause, then that 
2cbc0 69 6e 64 65 78 20 69 73 20 75 73 65 64 20 61 6e  index is used an
2cbc1 64 0a 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79 20  d.** *ppOrderBy 
2cbc2 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20  is set to NULL. 
2cbc3 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69   This is an opti
2cbc4 6d 69 7a 61 74 69 6f 6e 20 74 68 61 74 20 70 72  mization that pr
2cbc5 65 76 65 6e 74 73 20 61 6e 0a 2a 2a 20 75 6e 6e  events an.** unn
2cbc6 65 63 65 73 73 61 72 79 20 73 6f 72 74 20 6f 66  ecessary sort of
2cbc7 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2cbc8 69 66 20 61 6e 20 69 6e 64 65 78 20 61 70 70 72  if an index appr
2cbc9 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 0a  opriate for the.
2cbca 2a 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  ** ORDER BY clau
2cbcb 73 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  se already exist
2cbcc 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  s..**.** If the 
2cbcd 77 68 65 72 65 20 63 6c 61 75 73 65 20 6c 6f 6f  where clause loo
2cbce 70 73 20 63 61 6e 6e 6f 74 20 62 65 20 61 72 72  ps cannot be arr
2cbcf 61 6e 67 65 64 20 74 6f 20 70 72 6f 76 69 64 65  anged to provide
2cbd0 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20   the correct.** 
2cbd1 6f 75 74 70 75 74 20 6f 72 64 65 72 2c 20 74 68  output order, th
2cbd2 65 6e 20 74 68 65 20 2a 70 70 4f 72 64 65 72 42  en the *ppOrderB
2cbd3 79 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  y is unchanged..
2cbd4 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2cbd5 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c  E WhereInfo *sql
2cbd6 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 0a  ite3WhereBegin(.
2cbd7 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2cbd8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2cbd9 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2cbda 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
2cbdb 4c 69 73 74 2c 20 20 20 20 2f 2a 20 41 20 6c 69  List,    /* A li
2cbdc 73 74 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73  st of all tables
2cbdd 20 74 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 2a   to be scanned *
2cbde 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
2cbdf 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2cbe0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2cbe1 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 2a 70 70  .  ExprList **pp
2cbe2 4f 72 64 65 72 42 79 2c 20 2f 2a 20 41 6e 20 4f  OrderBy, /* An O
2cbe3 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20  RDER BY clause, 
2cbe4 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 75 31 36  or NULL */.  u16
2cbe5 20 77 63 74 72 6c 46 6c 61 67 73 20 20 20 20 20   wctrlFlags     
2cbe6 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65     /* One of the
2cbe7 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 64   WHERE_* flags d
2cbe8 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65  efined in sqlite
2cbe9 49 6e 74 2e 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e  Int.h */.){.  in
2cbea 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
2cbeb 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2cbec 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
2cbed 74 20 6e 42 79 74 65 57 49 6e 66 6f 3b 20 20 20  t nByteWInfo;   
2cbee 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 2e           /* Num.
2cbef 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64   bytes allocated
2cbf0 20 66 6f 72 20 57 68 65 72 65 49 6e 66 6f 20 73   for WhereInfo s
2cbf1 74 72 75 63 74 20 2a 2f 0a 20 20 57 68 65 72 65  truct */.  Where
2cbf2 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20  Info *pWInfo;   
2cbf3 20 20 20 20 20 20 2f 2a 20 57 69 6c 6c 20 62 65        /* Will be
2cbf4 63 6f 6d 65 20 74 68 65 20 72 65 74 75 72 6e 20  come the return 
2cbf5 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75  value of this fu
2cbf6 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 56 64 62 65  nction */.  Vdbe
2cbf7 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2cbf8 64 62 65 3b 20 20 20 2f 2a 20 54 68 65 20 76 69  dbe;   /* The vi
2cbf9 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
2cbfa 6e 67 69 6e 65 20 2a 2f 0a 20 20 42 69 74 6d 61  ngine */.  Bitma
2cbfb 73 6b 20 6e 6f 74 52 65 61 64 79 3b 20 20 20 20  sk notReady;    
2cbfc 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 73        /* Cursors
2cbfd 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 79 65   that are not ye
2cbfe 74 20 70 6f 73 69 74 69 6f 6e 65 64 20 2a 2f 0a  t positioned */.
2cbff 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a    WhereMaskSet *
2cc00 70 4d 61 73 6b 53 65 74 3b 20 20 20 20 2f 2a 20  pMaskSet;    /* 
2cc01 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6d  The expression m
2cc02 61 73 6b 20 73 65 74 20 2a 2f 0a 20 20 57 68 65  ask set */.  Whe
2cc03 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20  reClause *pWC;  
2cc04 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2cc05 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66  Decomposition of
2cc06 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2cc07 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  e */.  struct Sr
2cc08 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62  cList_item *pTab
2cc09 49 74 65 6d 3b 20 20 2f 2a 20 41 20 73 69 6e 67  Item;  /* A sing
2cc0a 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 70 54  le entry from pT
2cc0b 61 62 4c 69 73 74 20 2a 2f 0a 20 20 57 68 65 72  abList */.  Wher
2cc0c 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20  eLevel *pLevel; 
2cc0d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2cc0e 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 69 6e   single level in
2cc0f 20 74 68 65 20 70 57 49 6e 66 6f 20 6c 69 73 74   the pWInfo list
2cc10 20 2a 2f 0a 20 20 69 6e 74 20 69 46 72 6f 6d 3b   */.  int iFrom;
2cc11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cc12 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75        /* First u
2cc13 6e 75 73 65 64 20 46 52 4f 4d 20 63 6c 61 75 73  nused FROM claus
2cc14 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 69  e element */.  i
2cc15 6e 74 20 61 6e 64 46 6c 61 67 73 3b 20 20 20 20  nt andFlags;    
2cc16 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 4e 44            /* AND
2cc17 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  -ed combination 
2cc18 6f 66 20 61 6c 6c 20 70 57 43 2d 3e 61 5b 5d 2e  of all pWC->a[].
2cc19 77 74 46 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c  wtFlags */.  sql
2cc1a 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
2cc1b 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2cc1c 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2cc1d 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62  /..  /* The numb
2cc1e 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
2cc1f 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2cc20 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20 74 68  is limited by th
2cc21 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a  e number of.  **
2cc22 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d 61   bits in a Bitma
2cc23 73 6b 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  sk .  */.  if( p
2cc24 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 42 4d  TabList->nSrc>BM
2cc25 53 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  S ){.    sqlite3
2cc26 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2cc27 20 22 61 74 20 6d 6f 73 74 20 25 64 20 74 61 62   "at most %d tab
2cc28 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 22 2c 20  les in a join", 
2cc29 42 4d 53 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  BMS);.    return
2cc2a 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c   0;.  }..  /* Al
2cc2b 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69  locate and initi
2cc2c 61 6c 69 7a 65 20 74 68 65 20 57 68 65 72 65 49  alize the WhereI
2cc2d 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
2cc2e 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74  at will become t
2cc2f 68 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 76  he.  ** return v
2cc30 61 6c 75 65 2e 20 41 20 73 69 6e 67 6c 65 20 61  alue. A single a
2cc31 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 75 73 65  llocation is use
2cc32 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 57  d to store the W
2cc33 68 65 72 65 49 6e 66 6f 0a 20 20 2a 2a 20 73 74  hereInfo.  ** st
2cc34 72 75 63 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e  ruct, the conten
2cc35 74 73 20 6f 66 20 57 68 65 72 65 49 6e 66 6f 2e  ts of WhereInfo.
2cc36 61 5b 5d 2c 20 74 68 65 20 57 68 65 72 65 43 6c  a[], the WhereCl
2cc37 61 75 73 65 20 73 74 72 75 63 74 75 72 65 0a 20  ause structure. 
2cc38 20 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72   ** and the Wher
2cc39 65 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74 75  eMaskSet structu
2cc3a 72 65 2e 20 53 69 6e 63 65 20 57 68 65 72 65 43  re. Since WhereC
2cc3b 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 61  lause contains a
2cc3c 6e 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20 66 69  n 8-byte.  ** fi
2cc3d 65 6c 64 20 28 74 79 70 65 20 42 69 74 6d 61 73  eld (type Bitmas
2cc3e 6b 29 20 69 74 20 6d 75 73 74 20 62 65 20 61 6c  k) it must be al
2cc3f 69 67 6e 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79  igned on an 8-by
2cc40 74 65 20 62 6f 75 6e 64 61 72 79 20 6f 6e 0a 20  te boundary on. 
2cc41 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 65   ** some archite
2cc42 63 74 75 72 65 73 2e 20 48 65 6e 63 65 20 74 68  ctures. Hence th
2cc43 65 20 52 4f 55 4e 44 38 28 29 20 62 65 6c 6f 77  e ROUND8() below
2cc44 2e 0a 20 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50  ..  */.  db = pP
2cc45 61 72 73 65 2d 3e 64 62 3b 0a 20 20 6e 42 79 74  arse->db;.  nByt
2cc46 65 57 49 6e 66 6f 20 3d 20 52 4f 55 4e 44 38 28  eWInfo = ROUND8(
2cc47 73 69 7a 65 6f 66 28 57 68 65 72 65 49 6e 66 6f  sizeof(WhereInfo
2cc48 29 2b 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  )+(pTabList->nSr
2cc49 63 2d 31 29 2a 73 69 7a 65 6f 66 28 57 68 65 72  c-1)*sizeof(Wher
2cc4a 65 4c 65 76 65 6c 29 29 3b 0a 20 20 70 57 49 6e  eLevel));.  pWIn
2cc4b 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
2cc4c 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20 20  llocZero(db, .  
2cc4d 20 20 20 20 6e 42 79 74 65 57 49 6e 66 6f 20 2b      nByteWInfo +
2cc4e 20 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 57   .      sizeof(W
2cc4f 68 65 72 65 43 6c 61 75 73 65 29 20 2b 0a 20 20  hereClause) +.  
2cc50 20 20 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65      sizeof(Where
2cc51 4d 61 73 6b 53 65 74 29 0a 20 20 29 3b 0a 20 20  MaskSet).  );.  
2cc52 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2cc53 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
2cc54 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72   whereBeginError
2cc55 3b 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e  ;.  }.  pWInfo->
2cc56 6e 4c 65 76 65 6c 20 3d 20 70 54 61 62 4c 69 73  nLevel = pTabLis
2cc57 74 2d 3e 6e 53 72 63 3b 0a 20 20 70 57 49 6e 66  t->nSrc;.  pWInf
2cc58 6f 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  o->pParse = pPar
2cc59 73 65 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 54  se;.  pWInfo->pT
2cc5a 61 62 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73  abList = pTabLis
2cc5b 74 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 42 72  t;.  pWInfo->iBr
2cc5c 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62  eak = sqlite3Vdb
2cc5d 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
2cc5e 20 70 57 49 6e 66 6f 2d 3e 70 57 43 20 3d 20 70   pWInfo->pWC = p
2cc5f 57 43 20 3d 20 28 57 68 65 72 65 43 6c 61 75 73  WC = (WhereClaus
2cc60 65 20 2a 29 26 28 28 75 38 20 2a 29 70 57 49 6e  e *)&((u8 *)pWIn
2cc61 66 6f 29 5b 6e 42 79 74 65 57 49 6e 66 6f 5d 3b  fo)[nByteWInfo];
2cc62 0a 20 20 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c  .  pWInfo->wctrl
2cc63 46 6c 61 67 73 20 3d 20 77 63 74 72 6c 46 6c 61  Flags = wctrlFla
2cc64 67 73 3b 0a 20 20 70 4d 61 73 6b 53 65 74 20 3d  gs;.  pMaskSet =
2cc65 20 28 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 29   (WhereMaskSet*)
2cc66 26 70 57 43 5b 31 5d 3b 0a 0a 20 20 2f 2a 20 53  &pWC[1];..  /* S
2cc67 70 6c 69 74 20 74 68 65 20 57 48 45 52 45 20 63  plit the WHERE c
2cc68 6c 61 75 73 65 20 69 6e 74 6f 20 73 65 70 61 72  lause into separ
2cc69 61 74 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  ate subexpressio
2cc6a 6e 73 20 77 68 65 72 65 20 65 61 63 68 0a 20 20  ns where each.  
2cc6b 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  ** subexpression
2cc6c 20 69 73 20 73 65 70 61 72 61 74 65 64 20 62 79   is separated by
2cc6d 20 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72   an AND operator
2cc6e 2e 0a 20 20 2a 2f 0a 20 20 69 6e 69 74 4d 61 73  ..  */.  initMas
2cc6f 6b 53 65 74 28 70 4d 61 73 6b 53 65 74 29 3b 0a  kSet(pMaskSet);.
2cc70 20 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69    whereClauseIni
2cc71 74 28 70 57 43 2c 20 70 50 61 72 73 65 2c 20 70  t(pWC, pParse, p
2cc72 4d 61 73 6b 53 65 74 29 3b 0a 20 20 73 71 6c 69  MaskSet);.  sqli
2cc73 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74  te3ExprCodeConst
2cc74 61 6e 74 73 28 70 50 61 72 73 65 2c 20 70 57 68  ants(pParse, pWh
2cc75 65 72 65 29 3b 0a 20 20 77 68 65 72 65 53 70 6c  ere);.  whereSpl
2cc76 69 74 28 70 57 43 2c 20 70 57 68 65 72 65 2c 20  it(pWC, pWhere, 
2cc77 54 4b 5f 41 4e 44 29 3b 0a 20 20 20 20 0a 20 20  TK_AND);.    .  
2cc78 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a  /* Special case:
2cc79 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20   a WHERE clause 
2cc7a 74 68 61 74 20 69 73 20 63 6f 6e 73 74 61 6e 74  that is constant
2cc7b 2e 20 20 45 76 61 6c 75 61 74 65 20 74 68 65 0a  .  Evaluate the.
2cc7c 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
2cc7d 61 6e 64 20 65 69 74 68 65 72 20 6a 75 6d 70 20  and either jump 
2cc7e 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20  over all of the 
2cc7f 63 6f 64 65 20 6f 72 20 66 61 6c 6c 20 74 68 72  code or fall thr
2cc80 75 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57  u..  */.  if( pW
2cc81 68 65 72 65 20 26 26 20 28 70 54 61 62 4c 69 73  here && (pTabLis
2cc82 74 2d 3e 6e 53 72 63 3d 3d 30 20 7c 7c 20 73 71  t->nSrc==0 || sq
2cc83 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
2cc84 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 57 68 65 72  antNotJoin(pWher
2cc85 65 29 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  e)) ){.    sqlit
2cc86 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
2cc87 61 72 73 65 2c 20 70 57 68 65 72 65 2c 20 70 57  arse, pWhere, pW
2cc88 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 2c 20 53 51  Info->iBreak, SQ
2cc89 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
2cc8a 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 30  ;.    pWhere = 0
2cc8b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 69  ;.  }..  /* Assi
2cc8c 67 6e 20 61 20 62 69 74 20 66 72 6f 6d 20 74 68  gn a bit from th
2cc8d 65 20 62 69 74 6d 61 73 6b 20 74 6f 20 65 76 65  e bitmask to eve
2cc8e 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 46  ry term in the F
2cc8f 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a  ROM clause..  **
2cc90 0a 20 20 2a 2a 20 57 68 65 6e 20 61 73 73 69 67  .  ** When assig
2cc91 6e 69 6e 67 20 62 69 74 6d 61 73 6b 20 76 61 6c  ning bitmask val
2cc92 75 65 73 20 74 6f 20 46 52 4f 4d 20 63 6c 61 75  ues to FROM clau
2cc93 73 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 6d  se cursors, it m
2cc94 75 73 74 20 62 65 0a 20 20 2a 2a 20 74 68 65 20  ust be.  ** the 
2cc95 63 61 73 65 20 74 68 61 74 20 69 66 20 58 20 69  case that if X i
2cc96 73 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f  s the bitmask fo
2cc97 72 20 74 68 65 20 4e 2d 74 68 20 46 52 4f 4d 20  r the N-th FROM 
2cc98 63 6c 61 75 73 65 20 74 65 72 6d 20 74 68 65 6e  clause term then
2cc99 0a 20 20 2a 2a 20 74 68 65 20 62 69 74 6d 61 73  .  ** the bitmas
2cc9a 6b 20 66 6f 72 20 61 6c 6c 20 46 52 4f 4d 20 63  k for all FROM c
2cc9b 6c 61 75 73 65 20 74 65 72 6d 73 20 74 6f 20 74  lause terms to t
2cc9c 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 4e  he left of the N
2cc9d 2d 74 68 20 74 65 72 6d 0a 20 20 2a 2a 20 69 73  -th term.  ** is
2cc9e 20 28 58 2d 31 29 2e 20 20 20 41 6e 20 65 78 70   (X-1).   An exp
2cc9f 72 65 73 73 69 6f 6e 20 66 72 6f 6d 20 74 68 65  ression from the
2cca0 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20   ON clause of a 
2cca1 4c 45 46 54 20 4a 4f 49 4e 20 63 61 6e 20 75 73  LEFT JOIN can us
2cca2 65 0a 20 20 2a 2a 20 69 74 73 20 45 78 70 72 2e  e.  ** its Expr.
2cca3 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
2cca4 76 61 6c 75 65 20 74 6f 20 66 69 6e 64 20 74 68  value to find th
2cca5 65 20 62 69 74 6d 61 73 6b 20 6f 66 20 74 68 65  e bitmask of the
2cca6 20 72 69 67 68 74 20 74 61 62 6c 65 0a 20 20 2a   right table.  *
2cca7 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20  * of the join.  
2cca8 53 75 62 74 72 61 63 74 69 6e 67 20 6f 6e 65 20  Subtracting one 
2cca9 66 72 6f 6d 20 74 68 65 20 72 69 67 68 74 20 74  from the right t
2ccaa 61 62 6c 65 20 62 69 74 6d 61 73 6b 20 67 69 76  able bitmask giv
2ccab 65 73 20 61 0a 20 20 2a 2a 20 62 69 74 6d 61 73  es a.  ** bitmas
2ccac 6b 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73  k for all tables
2ccad 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
2ccae 74 68 65 20 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 69  the join.  Knowi
2ccaf 6e 67 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20  ng the bitmask. 
2ccb0 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c   ** for all tabl
2ccb1 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  es to the left o
2ccb2 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e 20 69 73  f a left join is
2ccb3 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 69 63   important.  Tic
2ccb4 6b 65 74 20 23 33 30 31 35 2e 0a 20 20 2a 2f 0a  ket #3015..  */.
2ccb5 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
2ccb6 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
2ccb7 29 7b 0a 20 20 20 20 63 72 65 61 74 65 4d 61 73  ){.    createMas
2ccb8 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62  k(pMaskSet, pTab
2ccb9 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73  List->a[i].iCurs
2ccba 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66  or);.  }.#ifndef
2ccbb 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20 20 20   NDEBUG.  {.    
2ccbc 42 69 74 6d 61 73 6b 20 74 6f 54 68 65 4c 65 66  Bitmask toTheLef
2ccbd 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69  t = 0;.    for(i
2ccbe 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  =0; i<pTabList->
2ccbf 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSrc; i++){.    
2ccc0 20 20 42 69 74 6d 61 73 6b 20 6d 20 3d 20 67 65    Bitmask m = ge
2ccc1 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
2ccc2 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69  pTabList->a[i].i
2ccc3 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 61  Cursor);.      a
2ccc4 73 73 65 72 74 28 20 28 6d 2d 31 29 3d 3d 74 6f  ssert( (m-1)==to
2ccc5 54 68 65 4c 65 66 74 20 29 3b 0a 20 20 20 20 20  TheLeft );.     
2ccc6 20 74 6f 54 68 65 4c 65 66 74 20 7c 3d 20 6d 3b   toTheLeft |= m;
2ccc7 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
2ccc8 66 0a 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20  f..  /* Analyze 
2ccc9 61 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 65 78  all of the subex
2ccca 70 72 65 73 73 69 6f 6e 73 2e 20 20 4e 6f 74 65  pressions.  Note
2cccb 20 74 68 61 74 20 65 78 70 72 41 6e 61 6c 79 7a   that exprAnalyz
2cccc 65 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 61  e() might.  ** a
2cccd 64 64 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74  dd new virtual t
2ccce 65 72 6d 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e  erms onto the en
2cccf 64 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  d of the WHERE c
2ccd0 6c 61 75 73 65 2e 20 20 57 65 20 64 6f 20 6e 6f  lause.  We do no
2ccd1 74 0a 20 20 2a 2a 20 77 61 6e 74 20 74 6f 20 61  t.  ** want to a
2ccd2 6e 61 6c 79 7a 65 20 74 68 65 73 65 20 76 69 72  nalyze these vir
2ccd3 74 75 61 6c 20 74 65 72 6d 73 2c 20 73 6f 20 73  tual terms, so s
2ccd4 74 61 72 74 20 61 6e 61 6c 79 7a 69 6e 67 20 61  tart analyzing a
2ccd5 74 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 61  t the end.  ** a
2ccd6 6e 64 20 77 6f 72 6b 20 66 6f 72 77 61 72 64 20  nd work forward 
2ccd7 73 6f 20 74 68 61 74 20 74 68 65 20 61 64 64 65  so that the adde
2ccd8 64 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73 20  d virtual terms 
2ccd9 61 72 65 20 6e 65 76 65 72 20 70 72 6f 63 65 73  are never proces
2ccda 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 78 70 72  sed..  */.  expr
2ccdb 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 54 61 62 4c  AnalyzeAll(pTabL
2ccdc 69 73 74 2c 20 70 57 43 29 3b 0a 20 20 69 66 28  ist, pWC);.  if(
2ccdd 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2ccde 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68  d ){.    goto wh
2ccdf 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20  ereBeginError;. 
2cce0 20 7d 0a 0a 20 20 2f 2a 20 43 68 6f 73 65 20 74   }..  /* Chose t
2cce1 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 74 6f  he best index to
2cce2 20 75 73 65 20 66 6f 72 20 65 61 63 68 20 74 61   use for each ta
2cce3 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
2cce4 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a  clause..  **.  *
2cce5 2a 20 54 68 69 73 20 6c 6f 6f 70 20 66 69 6c 6c  * This loop fill
2cce6 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  s in the followi
2cce7 6e 67 20 66 69 65 6c 64 73 3a 0a 20 20 2a 2a 0a  ng fields:.  **.
2cce8 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
2cce9 5b 5d 2e 70 49 64 78 20 20 20 20 20 20 54 68 65  [].pIdx      The
2ccea 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f   index to use fo
2cceb 72 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20  r this level of 
2ccec 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 20  the loop..  **  
2cced 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 77 73 46   pWInfo->a[].wsF
2ccee 6c 61 67 73 20 20 20 57 48 45 52 45 5f 78 78 78  lags   WHERE_xxx
2ccef 20 66 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65   flags associate
2ccf0 64 20 77 69 74 68 20 70 49 64 78 0a 20 20 2a 2a  d with pIdx.  **
2ccf1 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 6e     pWInfo->a[].n
2ccf2 45 71 20 20 20 20 20 20 20 54 68 65 20 6e 75 6d  Eq       The num
2ccf3 62 65 72 20 6f 66 20 3d 3d 20 61 6e 64 20 49 4e  ber of == and IN
2ccf4 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a   constraints.  *
2ccf5 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e  *   pWInfo->a[].
2ccf6 69 46 72 6f 6d 20 20 20 20 20 57 68 69 63 68 20  iFrom     Which 
2ccf7 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d  term of the FROM
2ccf8 20 63 6c 61 75 73 65 20 69 73 20 62 65 69 6e 67   clause is being
2ccf9 20 63 6f 64 65 64 0a 20 20 2a 2a 20 20 20 70 57   coded.  **   pW
2ccfa 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 54 61 62 43 75  Info->a[].iTabCu
2ccfb 72 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72  r   The VDBE cur
2ccfc 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61  sor for the data
2ccfd 62 61 73 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20  base table.  ** 
2ccfe 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 49    pWInfo->a[].iI
2ccff 64 78 43 75 72 20 20 20 54 68 65 20 56 44 42 45  dxCur   The VDBE
2cd00 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20   cursor for the 
2cd01 69 6e 64 65 78 0a 20 20 2a 2a 20 20 20 70 57 49  index.  **   pWI
2cd02 6e 66 6f 2d 3e 61 5b 5d 2e 70 54 65 72 6d 20 20  nfo->a[].pTerm  
2cd03 20 20 20 57 68 65 6e 20 77 73 46 6c 61 67 73 3d     When wsFlags=
2cd04 3d 57 4f 5f 4f 52 2c 20 74 68 65 20 4f 52 2d 63  =WO_OR, the OR-c
2cd05 6c 61 75 73 65 20 74 65 72 6d 0a 20 20 2a 2a 0a  lause term.  **.
2cd06 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 61    ** This loop a
2cd07 6c 73 6f 20 66 69 67 75 72 65 73 20 6f 75 74 20  lso figures out 
2cd08 74 68 65 20 6e 65 73 74 69 6e 67 20 6f 72 64 65  the nesting orde
2cd09 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74  r of tables in t
2cd0a 68 65 20 46 52 4f 4d 0a 20 20 2a 2a 20 63 6c 61  he FROM.  ** cla
2cd0b 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 74 52  use..  */.  notR
2cd0c 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61 73 6b  eady = ~(Bitmask
2cd0d 29 30 3b 0a 20 20 70 54 61 62 49 74 65 6d 20 3d  )0;.  pTabItem =
2cd0e 20 70 54 61 62 4c 69 73 74 2d 3e 61 3b 0a 20 20   pTabList->a;.  
2cd0f 70 4c 65 76 65 6c 20 3d 20 70 57 49 6e 66 6f 2d  pLevel = pWInfo-
2cd10 3e 61 3b 0a 20 20 61 6e 64 46 6c 61 67 73 20 3d  >a;.  andFlags =
2cd11 20 7e 30 3b 0a 20 20 57 48 45 52 45 54 52 41 43   ~0;.  WHERETRAC
2cd12 45 28 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65  E(("*** Optimize
2cd13 72 20 53 74 61 72 74 20 2a 2a 2a 5c 6e 22 29 29  r Start ***\n"))
2cd14 3b 0a 20 20 66 6f 72 28 69 3d 69 46 72 6f 6d 3d  ;.  for(i=iFrom=
2cd15 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f  0, pLevel=pWInfo
2cd16 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  ->a; i<pTabList-
2cd17 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65 76  >nSrc; i++, pLev
2cd18 65 6c 2b 2b 29 7b 0a 20 20 20 20 57 68 65 72 65  el++){.    Where
2cd19 43 6f 73 74 20 62 65 73 74 50 6c 61 6e 3b 20 20  Cost bestPlan;  
2cd1a 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 65         /* Most e
2cd1b 66 66 69 63 69 65 6e 74 20 70 6c 61 6e 20 73 65  fficient plan se
2cd1c 65 6e 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 20  en so far */.   
2cd1d 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
2cd1e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2cd1f 49 6e 64 65 78 20 66 6f 72 20 46 52 4f 4d 20 74  Index for FROM t
2cd20 61 62 6c 65 20 61 74 20 70 54 61 62 49 74 65 6d  able at pTabItem
2cd21 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a 3b 20 20   */.    int j;  
2cd22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cd23 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
2cd24 6e 67 20 6f 76 65 72 20 46 52 4f 4d 20 74 61 62  ng over FROM tab
2cd25 6c 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62  les */.    int b
2cd26 65 73 74 4a 20 3d 20 30 3b 20 20 20 20 20 20 20  estJ = 0;       
2cd27 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61         /* The va
2cd28 6c 75 65 20 6f 66 20 6a 20 2a 2f 0a 20 20 20 20  lue of j */.    
2cd29 42 69 74 6d 61 73 6b 20 6d 3b 20 20 20 20 20 20  Bitmask m;      
2cd2a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
2cd2b 69 74 6d 61 73 6b 20 76 61 6c 75 65 20 66 6f 72  itmask value for
2cd2c 20 6a 20 6f 72 20 62 65 73 74 4a 20 2a 2f 0a 20   j or bestJ */. 
2cd2d 20 20 20 69 6e 74 20 6f 6e 63 65 20 3d 20 30 3b     int once = 0;
2cd2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cd2f 2a 20 54 72 75 65 20 77 68 65 6e 20 66 69 72 73  * True when firs
2cd30 74 20 74 61 62 6c 65 20 69 73 20 73 65 65 6e 20  t table is seen 
2cd31 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  */..    memset(&
2cd32 62 65 73 74 50 6c 61 6e 2c 20 30 2c 20 73 69 7a  bestPlan, 0, siz
2cd33 65 6f 66 28 62 65 73 74 50 6c 61 6e 29 29 3b 0a  eof(bestPlan));.
2cd34 20 20 20 20 62 65 73 74 50 6c 61 6e 2e 72 43 6f      bestPlan.rCo
2cd35 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f  st = SQLITE_BIG_
2cd36 44 42 4c 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69  DBL;.    for(j=i
2cd37 46 72 6f 6d 2c 20 70 54 61 62 49 74 65 6d 3d 26  From, pTabItem=&
2cd38 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 3b 20  pTabList->a[j]; 
2cd39 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  j<pTabList->nSrc
2cd3a 3b 20 6a 2b 2b 2c 20 70 54 61 62 49 74 65 6d 2b  ; j++, pTabItem+
2cd3b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 6f  +){.      int do
2cd3c 4e 6f 74 52 65 6f 72 64 65 72 3b 20 20 20 20 2f  NotReorder;    /
2cd3d 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 74  * True if this t
2cd3e 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  able should not 
2cd3f 62 65 20 72 65 6f 72 64 65 72 65 64 20 2a 2f 0a  be reordered */.
2cd40 20 20 20 20 20 20 57 68 65 72 65 43 6f 73 74 20        WhereCost 
2cd41 73 43 6f 73 74 3b 20 20 20 20 20 2f 2a 20 43 6f  sCost;     /* Co
2cd42 73 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  st information f
2cd43 72 6f 6d 20 62 65 73 74 5b 56 69 72 74 75 61 6c  rom best[Virtual
2cd44 5d 49 6e 64 65 78 28 29 20 2a 2f 0a 20 20 20 20  ]Index() */.    
2cd45 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
2cd46 65 72 42 79 3b 20 20 2f 2a 20 4f 52 44 45 52 20  erBy;  /* ORDER 
2cd47 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 69 6e  BY clause for in
2cd48 64 65 78 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20  dex to optimize 
2cd49 2a 2f 0a 0a 20 20 20 20 20 20 64 6f 4e 6f 74 52  */..      doNotR
2cd4a 65 6f 72 64 65 72 20 3d 20 20 28 70 54 61 62 49  eorder =  (pTabI
2cd4b 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20  tem->jointype & 
2cd4c 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 43 52 4f 53  (JT_LEFT|JT_CROS
2cd4d 53 29 29 21 3d 30 3b 0a 20 20 20 20 20 20 69 66  S))!=0;.      if
2cd4e 28 20 6f 6e 63 65 20 26 26 20 64 6f 4e 6f 74 52  ( once && doNotR
2cd4f 65 6f 72 64 65 72 20 29 20 62 72 65 61 6b 3b 0a  eorder ) break;.
2cd50 20 20 20 20 20 20 6d 20 3d 20 67 65 74 4d 61 73        m = getMas
2cd51 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62  k(pMaskSet, pTab
2cd52 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a  Item->iCursor);.
2cd53 20 20 20 20 20 20 69 66 28 20 28 6d 20 26 20 6e        if( (m & n
2cd54 6f 74 52 65 61 64 79 29 3d 3d 30 20 29 7b 0a 20  otReady)==0 ){. 
2cd55 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 69 46         if( j==iF
2cd56 72 6f 6d 20 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20  rom ) iFrom++;. 
2cd57 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
2cd58 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
2cd59 4f 72 64 65 72 42 79 20 3d 20 28 28 69 3d 3d 30  OrderBy = ((i==0
2cd5a 20 26 26 20 70 70 4f 72 64 65 72 42 79 20 29 3f   && ppOrderBy )?
2cd5b 2a 70 70 4f 72 64 65 72 42 79 3a 30 29 3b 0a 0a  *ppOrderBy:0);..
2cd5c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
2cd5d 61 62 49 74 65 6d 2d 3e 70 54 61 62 20 29 3b 0a  abItem->pTab );.
2cd5e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2cd5f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2cd60 0a 20 20 20 20 20 20 69 66 28 20 49 73 56 69 72  .      if( IsVir
2cd61 74 75 61 6c 28 70 54 61 62 49 74 65 6d 2d 3e 70  tual(pTabItem->p
2cd62 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Tab) ){.        
2cd63 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2cd64 66 6f 20 2a 2a 70 70 20 3d 20 26 70 57 49 6e 66  fo **pp = &pWInf
2cd65 6f 2d 3e 61 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f  o->a[j].pIdxInfo
2cd66 3b 0a 20 20 20 20 20 20 20 20 62 65 73 74 56 69  ;.        bestVi
2cd67 72 74 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73  rtualIndex(pPars
2cd68 65 2c 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d  e, pWC, pTabItem
2cd69 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64  , notReady, pOrd
2cd6a 65 72 42 79 2c 20 26 73 43 6f 73 74 2c 20 70 70  erBy, &sCost, pp
2cd6b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 0a  );.      }else .
2cd6c 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20  #endif.      {. 
2cd6d 20 20 20 20 20 20 20 62 65 73 74 42 74 72 65 65         bestBtree
2cd6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 57  Index(pParse, pW
2cd6f 43 2c 20 70 54 61 62 49 74 65 6d 2c 20 6e 6f 74  C, pTabItem, not
2cd70 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79 2c  Ready, pOrderBy,
2cd71 20 26 73 43 6f 73 74 29 3b 0a 20 20 20 20 20 20   &sCost);.      
2cd72 7d 0a 20 20 20 20 20 20 69 66 28 20 6f 6e 63 65  }.      if( once
2cd73 3d 3d 30 20 7c 7c 20 73 43 6f 73 74 2e 72 43 6f  ==0 || sCost.rCo
2cd74 73 74 3c 62 65 73 74 50 6c 61 6e 2e 72 43 6f 73  st<bestPlan.rCos
2cd75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 6e 63  t ){.        onc
2cd76 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62  e = 1;.        b
2cd77 65 73 74 50 6c 61 6e 20 3d 20 73 43 6f 73 74 3b  estPlan = sCost;
2cd78 0a 20 20 20 20 20 20 20 20 62 65 73 74 4a 20 3d  .        bestJ =
2cd79 20 6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   j;.      }.    
2cd7a 20 20 69 66 28 20 64 6f 4e 6f 74 52 65 6f 72 64    if( doNotReord
2cd7b 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  er ) break;.    
2cd7c 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e  }.    assert( on
2cd7d 63 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ce );.    assert
2cd7e 28 20 6e 6f 74 52 65 61 64 79 20 26 20 67 65 74  ( notReady & get
2cd7f 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
2cd80 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a  TabList->a[bestJ
2cd81 5d 2e 69 43 75 72 73 6f 72 29 20 29 3b 0a 20 20  ].iCursor) );.  
2cd82 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a    WHERETRACE(("*
2cd83 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 73 65 6c  ** Optimizer sel
2cd84 65 63 74 73 20 74 61 62 6c 65 20 25 64 20 66 6f  ects table %d fo
2cd85 72 20 6c 6f 6f 70 20 25 64 5c 6e 22 2c 20 62 65  r loop %d\n", be
2cd86 73 74 4a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  stJ,.           
2cd87 70 4c 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61  pLevel-pWInfo->a
2cd88 29 29 3b 0a 20 20 20 20 69 66 28 20 28 62 65 73  ));.    if( (bes
2cd89 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61  tPlan.plan.wsFla
2cd8a 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52  gs & WHERE_ORDER
2cd8b 42 59 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  BY)!=0 ){.      
2cd8c 2a 70 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  *ppOrderBy = 0;.
2cd8d 20 20 20 20 7d 0a 20 20 20 20 61 6e 64 46 6c 61      }.    andFla
2cd8e 67 73 20 26 3d 20 62 65 73 74 50 6c 61 6e 2e 70  gs &= bestPlan.p
2cd8f 6c 61 6e 2e 77 73 46 6c 61 67 73 3b 0a 20 20 20  lan.wsFlags;.   
2cd90 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 20 3d 20   pLevel->plan = 
2cd91 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 3b 0a 20  bestPlan.plan;. 
2cd92 20 20 20 69 66 28 20 62 65 73 74 50 6c 61 6e 2e     if( bestPlan.
2cd93 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2cd94 48 45 52 45 5f 49 4e 44 45 58 45 44 20 29 7b 0a  HERE_INDEXED ){.
2cd95 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49        pLevel->iI
2cd96 64 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e  dxCur = pParse->
2cd97 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73  nTab++;.    }els
2cd98 65 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  e{.      pLevel-
2cd99 3e 69 49 64 78 43 75 72 20 3d 20 2d 31 3b 0a 20  >iIdxCur = -1;. 
2cd9a 20 20 20 7d 0a 20 20 20 20 6e 6f 74 52 65 61 64     }.    notRead
2cd9b 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d  y &= ~getMask(pM
2cd9c 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74  askSet, pTabList
2cd9d 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72 73  ->a[bestJ].iCurs
2cd9e 6f 72 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  or);.    pLevel-
2cd9f 3e 69 46 72 6f 6d 20 3d 20 28 75 38 29 62 65 73  >iFrom = (u8)bes
2cda0 74 4a 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63  tJ;..    /* Chec
2cda1 6b 20 74 68 61 74 20 69 66 20 74 68 65 20 74 61  k that if the ta
2cda2 62 6c 65 20 73 63 61 6e 6e 65 64 20 62 79 20 74  ble scanned by t
2cda3 68 69 73 20 6c 6f 6f 70 20 69 74 65 72 61 74 69  his loop iterati
2cda4 6f 6e 20 68 61 64 20 61 6e 0a 20 20 20 20 2a 2a  on had an.    **
2cda5 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
2cda6 73 65 20 61 74 74 61 63 68 65 64 20 74 6f 20 69  se attached to i
2cda7 74 2c 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65  t, that the name
2cda8 64 20 69 6e 64 65 78 20 69 73 20 62 65 69 6e 67  d index is being
2cda9 0a 20 20 20 20 2a 2a 20 75 73 65 64 20 66 6f 72  .    ** used for
2cdaa 20 74 68 65 20 73 63 61 6e 2e 20 49 66 20 6e 6f   the scan. If no
2cdab 74 2c 20 74 68 65 6e 20 71 75 65 72 79 20 63 6f  t, then query co
2cdac 6d 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 66 61  mpilation has fa
2cdad 69 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 52 65 74  iled..    ** Ret
2cdae 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  urn an error..  
2cdaf 20 20 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d 20    */.    pIdx = 
2cdb0 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
2cdb1 4a 5d 2e 70 49 6e 64 65 78 3b 0a 20 20 20 20 69  J].pIndex;.    i
2cdb2 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
2cdb3 20 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e 70   if( (bestPlan.p
2cdb4 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2cdb5 45 52 45 5f 49 4e 44 45 58 45 44 29 3d 3d 30 20  ERE_INDEXED)==0 
2cdb6 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2cdb7 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2cdb8 65 2c 20 22 63 61 6e 6e 6f 74 20 75 73 65 20 69  e, "cannot use i
2cdb9 6e 64 65 78 3a 20 25 73 22 2c 20 70 49 64 78 2d  ndex: %s", pIdx-
2cdba 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  >zName);.       
2cdbb 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e   goto whereBegin
2cdbc 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 65 6c  Error;.      }el
2cdbd 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  se{.        /* I
2cdbe 66 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20  f an INDEXED BY 
2cdbf 63 6c 61 75 73 65 20 69 73 20 75 73 65 64 2c 20  clause is used, 
2cdc0 74 68 65 20 62 65 73 74 49 6e 64 65 78 28 29 20  the bestIndex() 
2cdc1 66 75 6e 63 74 69 6f 6e 20 69 73 0a 20 20 20 20  function is.    
2cdc2 20 20 20 20 2a 2a 20 67 75 61 72 61 6e 74 65 65      ** guarantee
2cdc3 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 69 6e  d to find the in
2cdc4 64 65 78 20 73 70 65 63 69 66 69 65 64 20 69 6e  dex specified in
2cdc5 20 74 68 65 20 49 4e 44 45 58 45 44 20 42 59 20   the INDEXED BY 
2cdc6 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a  clause.        *
2cdc7 2a 20 69 66 20 69 74 20 66 69 6e 64 20 61 6e 20  * if it find an 
2cdc8 69 6e 64 65 78 20 61 74 20 61 6c 6c 2e 20 2a 2f  index at all. */
2cdc9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2cdca 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 75   bestPlan.plan.u
2cdcb 2e 70 49 64 78 3d 3d 70 49 64 78 20 29 3b 0a 20  .pIdx==pIdx );. 
2cdcc 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2cdcd 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22  .  WHERETRACE(("
2cdce 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 46 69  *** Optimizer Fi
2cdcf 6e 69 73 68 65 64 20 2a 2a 2a 5c 6e 22 29 29 3b  nished ***\n"));
2cdd0 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
2cdd1 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
2cdd2 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
2cdd3 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72  oto whereBeginEr
2cdd4 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ror;.  }..  /* I
2cdd5 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72  f the total quer
2cdd6 79 20 6f 6e 6c 79 20 73 65 6c 65 63 74 73 20 61  y only selects a
2cdd7 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 74 68 65   single row, the
2cdd8 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20  n the ORDER BY. 
2cdd9 20 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 69 72   ** clause is ir
2cdda 72 65 6c 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20  relevant..  */. 
2cddb 20 69 66 28 20 28 61 6e 64 46 6c 61 67 73 20 26   if( (andFlags &
2cddc 20 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d   WHERE_UNIQUE)!=
2cddd 30 20 26 26 20 70 70 4f 72 64 65 72 42 79 20 29  0 && ppOrderBy )
2cdde 7b 0a 20 20 20 20 2a 70 70 4f 72 64 65 72 42 79  {.    *ppOrderBy
2cddf 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
2cde0 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73  If the caller is
2cde1 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   an UPDATE or DE
2cde2 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 74  LETE statement t
2cde3 68 61 74 20 69 73 20 72 65 71 75 65 73 74 69 6e  hat is requestin
2cde4 67 0a 20 20 2a 2a 20 74 6f 20 75 73 65 20 61 20  g.  ** to use a 
2cde5 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74  one-pass algorit
2cde6 68 6d 2c 20 64 65 74 65 72 6d 69 6e 65 20 69 66  hm, determine if
2cde7 20 74 68 69 73 20 69 73 20 61 70 70 72 6f 70 72   this is appropr
2cde8 69 61 74 65 2e 0a 20 20 2a 2a 20 54 68 65 20 6f  iate..  ** The o
2cde9 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68  ne-pass algorith
2cdea 6d 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20  m only works if 
2cdeb 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2cdec 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a   constraints.  *
2cded 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
2cdee 74 6f 20 75 70 64 61 74 65 20 61 20 73 69 6e 67  to update a sing
2cdef 6c 65 20 72 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61  le row..  */.  a
2cdf0 73 73 65 72 74 28 20 28 77 63 74 72 6c 46 6c 61  ssert( (wctrlFla
2cdf1 67 73 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41  gs & WHERE_ONEPA
2cdf2 53 53 5f 44 45 53 49 52 45 44 29 3d 3d 30 20 7c  SS_DESIRED)==0 |
2cdf3 7c 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c  | pWInfo->nLevel
2cdf4 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 28 77 63  ==1 );.  if( (wc
2cdf5 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
2cdf6 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44  _ONEPASS_DESIRED
2cdf7 29 21 3d 30 20 26 26 20 28 61 6e 64 46 6c 61 67  )!=0 && (andFlag
2cdf8 73 20 26 20 57 48 45 52 45 5f 55 4e 49 51 55 45  s & WHERE_UNIQUE
2cdf9 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 57 49 6e  )!=0 ){.    pWIn
2cdfa 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3d 20  fo->okOnePass = 
2cdfb 31 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 61  1;.    pWInfo->a
2cdfc 5b 30 5d 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  [0].plan.wsFlags
2cdfd 20 26 3d 20 7e 57 48 45 52 45 5f 49 44 58 5f 4f   &= ~WHERE_IDX_O
2cdfe 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  NLY;.  }..  /* O
2cdff 70 65 6e 20 61 6c 6c 20 74 61 62 6c 65 73 20 69  pen all tables i
2ce00 6e 20 74 68 65 20 70 54 61 62 4c 69 73 74 20 61  n the pTabList a
2ce01 6e 64 20 61 6e 79 20 69 6e 64 69 63 65 73 20 73  nd any indices s
2ce02 65 6c 65 63 74 65 64 20 66 6f 72 0a 20 20 2a 2a  elected for.  **
2ce03 20 73 65 61 72 63 68 69 6e 67 20 74 68 6f 73 65   searching those
2ce04 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
2ce05 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
2ce06 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
2ce07 2d 31 29 3b 20 2f 2a 20 49 6e 73 65 72 74 20 74  -1); /* Insert t
2ce08 68 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69  he cookie verifi
2ce09 65 72 20 47 6f 74 6f 20 2a 2f 0a 20 20 66 6f 72  er Goto */.  for
2ce0a 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49  (i=0, pLevel=pWI
2ce0b 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69  nfo->a; i<pTabLi
2ce0c 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
2ce0d 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 54 61  Level++){.    Ta
2ce0e 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 2f  ble *pTab;     /
2ce0f 2a 20 54 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  * Table to open 
2ce10 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20  */.    int iDb; 
2ce11 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2ce12 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
2ce13 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 2f 69 6e  taining table/in
2ce14 64 65 78 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  dex */..#ifndef 
2ce15 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
2ce16 41 49 4e 0a 20 20 20 20 69 66 28 20 70 50 61 72  AIN.    if( pPar
2ce17 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29  se->explain==2 )
2ce18 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d  {.      char *zM
2ce19 73 67 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74  sg;.      struct
2ce1a 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
2ce1b 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74  Item = &pTabList
2ce1c 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f  ->a[pLevel->iFro
2ce1d 6d 5d 3b 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d  m];.      zMsg =
2ce1e 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2ce1f 64 62 2c 20 22 54 41 42 4c 45 20 25 73 22 2c 20  db, "TABLE %s", 
2ce20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pItem->zName);. 
2ce21 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
2ce22 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20  zAlias ){.      
2ce23 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
2ce24 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73  MAppendf(db, zMs
2ce25 67 2c 20 22 25 73 20 41 53 20 25 73 22 2c 20 7a  g, "%s AS %s", z
2ce26 4d 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  Msg, pItem->zAli
2ce27 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  as);.      }.   
2ce28 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e     if( (pLevel->
2ce29 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2ce2a 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30  HERE_INDEXED)!=0
2ce2b 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67   ){.        zMsg
2ce2c 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
2ce2d 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
2ce2e 20 57 49 54 48 20 49 4e 44 45 58 20 25 73 22 2c   WITH INDEX %s",
2ce2f 0a 20 20 20 20 20 20 20 20 20 20 20 7a 4d 73 67  .           zMsg
2ce30 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  , pLevel->plan.u
2ce31 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  .pIdx->zName);. 
2ce32 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2ce33 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2ce34 61 67 73 20 26 20 57 48 45 52 45 5f 4d 55 4c 54  ags & WHERE_MULT
2ce35 49 5f 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20  I_OR ){.        
2ce36 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41  zMsg = sqlite3MA
2ce37 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c  ppendf(db, zMsg,
2ce38 20 22 25 73 20 56 49 41 20 4d 55 4c 54 49 2d 49   "%s VIA MULTI-I
2ce39 4e 44 45 58 20 55 4e 49 4f 4e 22 2c 20 7a 4d 73  NDEX UNION", zMs
2ce3a 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  g);.      }else 
2ce3b 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
2ce3c 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  .wsFlags & (WHER
2ce3d 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45  E_ROWID_EQ|WHERE
2ce3e 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b  _ROWID_RANGE) ){
2ce3f 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  .        zMsg = 
2ce40 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
2ce41 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 55 53  db, zMsg, "%s US
2ce42 49 4e 47 20 50 52 49 4d 41 52 59 20 4b 45 59 22  ING PRIMARY KEY"
2ce43 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d  , zMsg);.      }
2ce44 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ce45 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2ce46 45 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28  E.      else if(
2ce47 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
2ce48 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56  sFlags & WHERE_V
2ce49 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20  IRTUALTABLE)!=0 
2ce4a 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2ce4b 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
2ce4c 56 74 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c  VtabIdx = pLevel
2ce4d 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64  ->plan.u.pVtabId
2ce4e 78 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20  x;.        zMsg 
2ce4f 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64  = sqlite3MAppend
2ce50 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20  f(db, zMsg, "%s 
2ce51 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 49 4e  VIRTUAL TABLE IN
2ce52 44 45 58 20 25 64 3a 25 73 22 2c 20 7a 4d 73 67  DEX %d:%s", zMsg
2ce53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2ce54 20 20 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e        pVtabIdx->
2ce55 69 64 78 4e 75 6d 2c 20 70 56 74 61 62 49 64 78  idxNum, pVtabIdx
2ce56 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20 20  ->idxStr);.     
2ce57 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
2ce58 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
2ce59 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2ce5a 5f 4f 52 44 45 52 42 59 20 29 7b 0a 20 20 20 20  _ORDERBY ){.    
2ce5b 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
2ce5c 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a  e3MAppendf(db, z
2ce5d 4d 73 67 2c 20 22 25 73 20 4f 52 44 45 52 20 42  Msg, "%s ORDER B
2ce5e 59 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20  Y", zMsg);.     
2ce5f 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2ce60 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2ce61 5f 45 78 70 6c 61 69 6e 2c 20 69 2c 20 70 4c 65  _Explain, i, pLe
2ce62 76 65 6c 2d 3e 69 46 72 6f 6d 2c 20 30 2c 20 7a  vel->iFrom, 0, z
2ce63 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  Msg, P4_DYNAMIC)
2ce64 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
2ce65 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  * SQLITE_OMIT_EX
2ce66 50 4c 41 49 4e 20 2a 2f 0a 20 20 20 20 70 54 61  PLAIN */.    pTa
2ce67 62 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73  bItem = &pTabLis
2ce68 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72  t->a[pLevel->iFr
2ce69 6f 6d 5d 3b 0a 20 20 20 20 70 54 61 62 20 3d 20  om];.    pTab = 
2ce6a 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a  pTabItem->pTab;.
2ce6b 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
2ce6c 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
2ce6d 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
2ce6e 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 69  >pSchema);.    i
2ce6f 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61  f( (pTab->tabFla
2ce70 67 73 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61  gs & TF_Ephemera
2ce71 6c 29 21 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70  l)!=0 || pTab->p
2ce72 53 65 6c 65 63 74 20 29 20 63 6f 6e 74 69 6e 75  Select ) continu
2ce73 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e;.#ifndef SQLIT
2ce74 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2ce75 42 4c 45 0a 20 20 20 20 69 66 28 20 28 70 4c 65  BLE.    if( (pLe
2ce76 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2ce77 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 41  s & WHERE_VIRTUA
2ce78 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20  LTABLE)!=0 ){.  
2ce79 20 20 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70      int iCur = p
2ce7a 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
2ce7b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2ce7c 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2ce7d 56 4f 70 65 6e 2c 20 69 43 75 72 2c 20 30 2c 20  VOpen, iCur, 0, 
2ce7e 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
2ce7f 20 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e 73             (cons
2ce80 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56  t char*)pTab->pV
2ce81 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  tab, P4_VTAB);. 
2ce82 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
2ce83 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d      if( (pLevel-
2ce84 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2ce85 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d  WHERE_IDX_ONLY)=
2ce86 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  =0.         && (
2ce87 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
2ce88 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 29 3d 3d 30  RE_OMIT_OPEN)==0
2ce89 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70   ){.      int op
2ce8a 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65   = pWInfo->okOne
2ce8b 50 61 73 73 20 3f 20 4f 50 5f 4f 70 65 6e 57 72  Pass ? OP_OpenWr
2ce8c 69 74 65 20 3a 20 4f 50 5f 4f 70 65 6e 52 65 61  ite : OP_OpenRea
2ce8d 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  d;.      sqlite3
2ce8e 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
2ce8f 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72  , pTabItem->iCur
2ce90 73 6f 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  sor, iDb, pTab, 
2ce91 6f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  op);.      if( !
2ce92 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73  pWInfo->okOnePas
2ce93 73 20 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3c  s && pTab->nCol<
2ce94 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 20 20 42  BMS ){.        B
2ce95 69 74 6d 61 73 6b 20 62 20 3d 20 70 54 61 62 49  itmask b = pTabI
2ce96 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20  tem->colUsed;.  
2ce97 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b        int n = 0;
2ce98 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 62  .        for(; b
2ce99 3b 20 62 3d 62 3e 3e 31 2c 20 6e 2b 2b 29 7b 7d  ; b=b>>1, n++){}
2ce9a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ce9b 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2ce9c 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2ce9d 6e 74 41 64 64 72 28 76 29 2d 31 2c 20 53 51 4c  ntAddr(v)-1, SQL
2ce9e 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6e  ITE_INT_TO_PTR(n
2ce9f 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  ), P4_INT32);.  
2cea0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c        assert( n<
2cea1 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  =pTab->nCol );. 
2cea2 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2cea3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54  {.      sqlite3T
2cea4 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
2cea5 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
2cea6 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
2cea7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65  );.    }.    pLe
2cea8 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 3d 20 70  vel->iTabCur = p
2cea9 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
2ceaa 3b 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76 65  ;.    if( (pLeve
2ceab 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2ceac 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29  & WHERE_INDEXED)
2cead 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 49 6e 64  !=0 ){.      Ind
2ceae 65 78 20 2a 70 49 78 20 3d 20 70 4c 65 76 65 6c  ex *pIx = pLevel
2ceaf 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20  ->plan.u.pIdx;. 
2ceb0 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b       KeyInfo *pK
2ceb1 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
2ceb2 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
2ceb3 20 70 49 78 29 3b 0a 20 20 20 20 20 20 69 6e 74   pIx);.      int
2ceb4 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65   iIdxCur = pLeve
2ceb5 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20  l->iIdxCur;.    
2ceb6 20 20 61 73 73 65 72 74 28 20 70 49 78 2d 3e 70    assert( pIx->p
2ceb7 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
2ceb8 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 61  chema );.      a
2ceb9 73 73 65 72 74 28 20 69 49 64 78 43 75 72 3e 3d  ssert( iIdxCur>=
2ceba 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
2cebb 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2cebc 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64  OP_OpenRead, iId
2cebd 78 43 75 72 2c 20 70 49 78 2d 3e 74 6e 75 6d 2c  xCur, pIx->tnum,
2cebe 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20   iDb,.          
2cebf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
2cec0 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45  har*)pKey, P4_KE
2cec1 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
2cec2 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2cec3 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 78 2d  t((v, "%s", pIx-
2cec4 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a  >zName));.    }.
2cec5 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56      sqlite3CodeV
2cec6 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72  erifySchema(pPar
2cec7 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20  se, iDb);.  }.  
2cec8 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 20 3d 20 73  pWInfo->iTop = s
2cec9 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2ceca 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 2f 2a 20  tAddr(v);..  /* 
2cecb 47 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64  Generate the cod
2cecc 65 20 74 6f 20 64 6f 20 74 68 65 20 73 65 61 72  e to do the sear
2cecd 63 68 2e 20 20 45 61 63 68 20 69 74 65 72 61 74  ch.  Each iterat
2cece 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 0a 20  ion of the for. 
2cecf 20 2a 2a 20 6c 6f 6f 70 20 62 65 6c 6f 77 20 67   ** loop below g
2ced0 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 66 6f  enerates code fo
2ced1 72 20 61 20 73 69 6e 67 6c 65 20 6e 65 73 74 65  r a single neste
2ced2 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 56 4d  d loop of the VM
2ced3 0a 20 20 2a 2a 20 70 72 6f 67 72 61 6d 2e 0a 20  .  ** program.. 
2ced4 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d   */.  notReady =
2ced5 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20   ~(Bitmask)0;.  
2ced6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c  for(i=0; i<pTabL
2ced7 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  ist->nSrc; i++){
2ced8 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20 3d 20  .    notReady = 
2ced9 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72 74  codeOneLoopStart
2ceda 28 70 57 49 6e 66 6f 2c 20 69 2c 20 77 63 74 72  (pWInfo, i, wctr
2cedb 6c 46 6c 61 67 73 2c 20 6e 6f 74 52 65 61 64 79  lFlags, notReady
2cedc 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69  );.    pWInfo->i
2cedd 43 6f 6e 74 69 6e 75 65 20 3d 20 70 57 49 6e 66  Continue = pWInf
2cede 6f 2d 3e 61 5b 69 5d 2e 61 64 64 72 43 6f 6e 74  o->a[i].addrCont
2cedf 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51  ;.  }..#ifdef SQ
2cee0 4c 49 54 45 5f 54 45 53 54 20 20 2f 2a 20 46 6f  LITE_TEST  /* Fo
2cee1 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  r testing and de
2cee2 62 75 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79  bugging use only
2cee3 20 2a 2f 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20   */.  /* Record 
2cee4 69 6e 20 74 68 65 20 71 75 65 72 79 20 70 6c 61  in the query pla
2cee5 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  n information ab
2cee6 6f 75 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  out the current 
2cee7 74 61 62 6c 65 0a 20 20 2a 2a 20 61 6e 64 20 74  table.  ** and t
2cee8 68 65 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f  he index used to
2cee9 20 61 63 63 65 73 73 20 69 74 20 28 69 66 20 61   access it (if a
2ceea 6e 79 29 2e 20 20 49 66 20 74 68 65 20 74 61 62  ny).  If the tab
2ceeb 6c 65 20 69 74 73 65 6c 66 0a 20 20 2a 2a 20 69  le itself.  ** i
2ceec 73 20 6e 6f 74 20 75 73 65 64 2c 20 69 74 73 20  s not used, its 
2ceed 6e 61 6d 65 20 69 73 20 6a 75 73 74 20 27 7b 7d  name is just '{}
2ceee 27 2e 20 20 49 66 20 6e 6f 20 69 6e 64 65 78 20  '.  If no index 
2ceef 69 73 20 75 73 65 64 0a 20 20 2a 2a 20 74 68 65  is used.  ** the
2cef0 20 69 6e 64 65 78 20 69 73 20 6c 69 73 74 65 64   index is listed
2cef1 20 61 73 20 22 7b 7d 22 2e 20 20 49 66 20 74 68   as "{}".  If th
2cef2 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73  e primary key is
2cef3 20 75 73 65 64 20 74 68 65 0a 20 20 2a 2a 20 69   used the.  ** i
2cef4 6e 64 65 78 20 6e 61 6d 65 20 69 73 20 27 2a 27  ndex name is '*'
2cef5 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
2cef6 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
2cef7 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68  rc; i++){.    ch
2cef8 61 72 20 2a 7a 3b 0a 20 20 20 20 69 6e 74 20 6e  ar *z;.    int n
2cef9 3b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26  ;.    pLevel = &
2cefa 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20  pWInfo->a[i];.  
2cefb 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70 54    pTabItem = &pT
2cefc 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c  abList->a[pLevel
2cefd 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 7a 20  ->iFrom];.    z 
2cefe 3d 20 70 54 61 62 49 74 65 6d 2d 3e 7a 41 6c 69  = pTabItem->zAli
2ceff 61 73 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30  as;.    if( z==0
2cf00 20 29 20 7a 20 3d 20 70 54 61 62 49 74 65 6d 2d   ) z = pTabItem-
2cf01 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  >pTab->zName;.  
2cf02 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72    n = sqlite3Str
2cf03 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 69 66  len30(z);.    if
2cf04 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69 7a  ( n+nQPlan < siz
2cf05 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75 65 72  eof(sqlite3_quer
2cf06 79 5f 70 6c 61 6e 29 2d 31 30 20 29 7b 0a 20 20  y_plan)-10 ){.  
2cf07 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2cf08 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2cf09 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 29 7b  HERE_IDX_ONLY ){
2cf0a 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
2cf0b 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70  &sqlite3_query_p
2cf0c 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d  lan[nQPlan], "{}
2cf0d 22 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 6e  ", 2);.        n
2cf0e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20 20 20  QPlan += 2;.    
2cf0f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2cf10 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
2cf11 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
2cf12 61 6e 5d 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20  an], z, n);.    
2cf13 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b      nQPlan += n;
2cf14 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
2cf15 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
2cf16 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20  n[nQPlan++] = ' 
2cf17 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73  ';.    }.    tes
2cf18 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70  tcase( pLevel->p
2cf19 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2cf1a 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 3b 0a  ERE_ROWID_EQ );.
2cf1b 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c      testcase( pL
2cf1c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2cf1d 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
2cf1e 5f 52 41 4e 47 45 20 29 3b 0a 20 20 20 20 69 66  _RANGE );.    if
2cf1f 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
2cf20 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f  sFlags & (WHERE_
2cf21 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52  ROWID_EQ|WHERE_R
2cf22 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20  OWID_RANGE) ){. 
2cf23 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c       memcpy(&sql
2cf24 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
2cf25 6e 51 50 6c 61 6e 5d 2c 20 22 2a 20 22 2c 20 32  nQPlan], "* ", 2
2cf26 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  );.      nQPlan 
2cf27 2b 3d 20 32 3b 0a 20 20 20 20 7d 65 6c 73 65 20  += 2;.    }else 
2cf28 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
2cf29 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2cf2a 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b  E_INDEXED)!=0 ){
2cf2b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  .      n = sqlit
2cf2c 65 33 53 74 72 6c 65 6e 33 30 28 70 4c 65 76 65  e3Strlen30(pLeve
2cf2d 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e  l->plan.u.pIdx->
2cf2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66  zName);.      if
2cf2f 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69 7a  ( n+nQPlan < siz
2cf30 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75 65 72  eof(sqlite3_quer
2cf31 79 5f 70 6c 61 6e 29 2d 32 20 29 7b 0a 20 20 20  y_plan)-2 ){.   
2cf32 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c       memcpy(&sql
2cf33 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
2cf34 6e 51 50 6c 61 6e 5d 2c 20 70 4c 65 76 65 6c 2d  nQPlan], pLevel-
2cf35 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e  >plan.u.pIdx->zN
2cf36 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20  ame, n);.       
2cf37 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20   nQPlan += n;.  
2cf38 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75        sqlite3_qu
2cf39 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2b  ery_plan[nQPlan+
2cf3a 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20  +] = ' ';.      
2cf3b 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
2cf3c 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
2cf3d 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
2cf3e 50 6c 61 6e 5d 2c 20 22 7b 7d 20 22 2c 20 33 29  Plan], "{} ", 3)
2cf3f 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b  ;.      nQPlan +
2cf40 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 3;.    }.  }. 
2cf41 20 77 68 69 6c 65 28 20 6e 51 50 6c 61 6e 3e 30   while( nQPlan>0
2cf42 20 26 26 20 73 71 6c 69 74 65 33 5f 71 75 65 72   && sqlite3_quer
2cf43 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2d 31 5d  y_plan[nQPlan-1]
2cf44 3d 3d 27 20 27 20 29 7b 0a 20 20 20 20 73 71 6c  ==' ' ){.    sql
2cf45 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
2cf46 2d 2d 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20  --nQPlan] = 0;. 
2cf47 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 71 75 65   }.  sqlite3_que
2cf48 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 20  ry_plan[nQPlan] 
2cf49 3d 20 30 3b 0a 20 20 6e 51 50 6c 61 6e 20 3d 20  = 0;.  nQPlan = 
2cf4a 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  0;.#endif /* SQL
2cf4b 49 54 45 5f 54 45 53 54 20 2f 2f 20 54 65 73 74  ITE_TEST // Test
2cf4c 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
2cf4d 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 0a 20  g use only */.. 
2cf4e 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20 63   /* Record the c
2cf4f 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 61 64 64 72  ontinuation addr
2cf50 65 73 73 20 69 6e 20 74 68 65 20 57 68 65 72 65  ess in the Where
2cf51 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20  Info structure. 
2cf52 20 54 68 65 6e 0a 20 20 2a 2a 20 63 6c 65 61 6e   Then.  ** clean
2cf53 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a   up and return..
2cf54 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 57    */.  return pW
2cf55 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70  Info;..  /* Jump
2cf56 20 68 65 72 65 20 69 66 20 6d 61 6c 6c 6f 63 20   here if malloc 
2cf57 66 61 69 6c 73 20 2a 2f 0a 77 68 65 72 65 42 65  fails */.whereBe
2cf58 67 69 6e 45 72 72 6f 72 3a 0a 20 20 77 68 65 72  ginError:.  wher
2cf59 65 49 6e 66 6f 46 72 65 65 28 64 62 2c 20 70 57  eInfoFree(db, pW
2cf5a 49 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20  Info);.  return 
2cf5b 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  0;.}../*.** Gene
2cf5c 72 61 74 65 20 74 68 65 20 65 6e 64 20 6f 66 20  rate the end of 
2cf5d 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e 20  the WHERE loop. 
2cf5e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e   See comments on
2cf5f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65 72   .** sqlite3Wher
2cf60 65 42 65 67 69 6e 28 29 20 66 6f 72 20 61 64 64  eBegin() for add
2cf61 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2cf62 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
2cf63 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2cf64 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72  te3WhereEnd(Wher
2cf65 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a  eInfo *pWInfo){.
2cf66 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20    Parse *pParse 
2cf67 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65  = pWInfo->pParse
2cf68 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
2cf69 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2cf6a 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 4c 65 76  nt i;.  WhereLev
2cf6b 65 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20 20 53 72  el *pLevel;.  Sr
2cf6c 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20  cList *pTabList 
2cf6d 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69  = pWInfo->pTabLi
2cf6e 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  st;.  sqlite3 *d
2cf6f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2cf70 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6c  .  /* Generate l
2cf71 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20  oop termination 
2cf72 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  code..  */.  sql
2cf73 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
2cf74 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 66 6f  ar(pParse);.  fo
2cf75 72 28 69 3d 70 54 61 62 4c 69 73 74 2d 3e 6e 53  r(i=pTabList->nS
2cf76 72 63 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  rc-1; i>=0; i--)
2cf77 7b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26  {.    pLevel = &
2cf78 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20  pWInfo->a[i];.  
2cf79 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2cf7a 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65  olveLabel(v, pLe
2cf7b 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 29 3b 0a  vel->addrCont);.
2cf7c 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2cf7d 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20  op!=OP_Noop ){. 
2cf7e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2cf7f 41 64 64 4f 70 32 28 76 2c 20 70 4c 65 76 65 6c  AddOp2(v, pLevel
2cf80 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70 31  ->op, pLevel->p1
2cf81 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0a 20  , pLevel->p2);. 
2cf82 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2cf83 43 68 61 6e 67 65 50 35 28 76 2c 20 70 4c 65 76  ChangeP5(v, pLev
2cf84 65 6c 2d 3e 70 35 29 3b 0a 20 20 20 20 7d 0a 20  el->p5);.    }. 
2cf85 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70     if( pLevel->p
2cf86 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2cf87 45 52 45 5f 49 4e 5f 41 42 4c 45 20 26 26 20 70  ERE_IN_ABLE && p
2cf88 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 3e  Level->u.in.nIn>
2cf89 30 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63  0 ){.      struc
2cf8a 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 20  t InLoop *pIn;. 
2cf8b 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2cf8c 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2cf8d 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65  olveLabel(v, pLe
2cf8e 76 65 6c 2d 3e 61 64 64 72 4e 78 74 29 3b 0a 20  vel->addrNxt);. 
2cf8f 20 20 20 20 20 66 6f 72 28 6a 3d 70 4c 65 76 65       for(j=pLeve
2cf90 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2c 20 70 49 6e  l->u.in.nIn, pIn
2cf91 3d 26 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61  =&pLevel->u.in.a
2cf92 49 6e 4c 6f 6f 70 5b 6a 2d 31 5d 3b 20 6a 3e 30  InLoop[j-1]; j>0
2cf93 3b 20 6a 2d 2d 2c 20 70 49 6e 2d 2d 29 7b 0a 20  ; j--, pIn--){. 
2cf94 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2cf95 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 49  beJumpHere(v, pI
2cf96 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 2b 31 29 3b  n->addrInTop+1);
2cf97 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2cf98 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2cf99 5f 4e 65 78 74 2c 20 70 49 6e 2d 3e 69 43 75 72  _Next, pIn->iCur
2cf9a 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70  , pIn->addrInTop
2cf9b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2cf9c 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2cf9d 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70  , pIn->addrInTop
2cf9e 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  -1);.      }.   
2cf9f 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2cfa0 28 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69  (db, pLevel->u.i
2cfa1 6e 2e 61 49 6e 4c 6f 6f 70 29 3b 0a 20 20 20 20  n.aInLoop);.    
2cfa2 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
2cfa3 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2cfa4 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b   pLevel->addrBrk
2cfa5 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  );.    if( pLeve
2cfa6 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a  l->iLeftJoin ){.
2cfa7 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a        int addr;.
2cfa8 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
2cfa9 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2cfaa 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 70 4c 65 76  , OP_IfPos, pLev
2cfab 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a  el->iLeftJoin);.
2cfac 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2cfad 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 75  eAddOp1(v, OP_Nu
2cfae 6c 6c 52 6f 77 2c 20 70 54 61 62 4c 69 73 74 2d  llRow, pTabList-
2cfaf 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a  >a[i].iCursor);.
2cfb0 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c        if( pLevel
2cfb1 2d 3e 69 49 64 78 43 75 72 3e 3d 30 20 29 7b 0a  ->iIdxCur>=0 ){.
2cfb2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2cfb3 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2cfb4 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65 6c 2d  NullRow, pLevel-
2cfb5 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20  >iIdxCur);.     
2cfb6 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65   }.      if( pLe
2cfb7 76 65 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65 74 75  vel->op==OP_Retu
2cfb8 72 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  rn ){.        sq
2cfb9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2cfba 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 70 4c 65  v, OP_Gosub, pLe
2cfbb 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c 2d  vel->p1, pLevel-
2cfbc 3e 61 64 64 72 46 69 72 73 74 29 3b 0a 20 20 20  >addrFirst);.   
2cfbd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2cfbe 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2cfbf 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2cfc0 30 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46  0, pLevel->addrF
2cfc1 69 72 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  irst);.      }. 
2cfc2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2cfc3 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2cfc4 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2cfc5 2f 2a 20 54 68 65 20 22 62 72 65 61 6b 22 20 70  /* The "break" p
2cfc6 6f 69 6e 74 20 69 73 20 68 65 72 65 2c 20 6a 75  oint is here, ju
2cfc7 73 74 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  st past the end 
2cfc8 6f 66 20 74 68 65 20 6f 75 74 65 72 20 6c 6f 6f  of the outer loo
2cfc9 70 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74 2e 0a  p..  ** Set it..
2cfca 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2cfcb 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2cfcc 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b  , pWInfo->iBreak
2cfcd 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61  );..  /* Close a
2cfce 6c 6c 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72  ll of the cursor
2cfcf 73 20 74 68 61 74 20 77 65 72 65 20 6f 70 65 6e  s that were open
2cfd0 65 64 20 62 79 20 73 71 6c 69 74 65 33 57 68 65  ed by sqlite3Whe
2cfd1 72 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a 20 20  reBegin..  */.  
2cfd2 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d  for(i=0, pLevel=
2cfd3 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61  pWInfo->a; i<pTa
2cfd4 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
2cfd5 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20  , pLevel++){.   
2cfd6 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2cfd7 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 20 3d  item *pTabItem =
2cfd8 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c   &pTabList->a[pL
2cfd9 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20  evel->iFrom];.  
2cfda 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2cfdb 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a  pTabItem->pTab;.
2cfdc 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
2cfdd 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28  !=0 );.    if( (
2cfde 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
2cfdf 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d   TF_Ephemeral)!=
2cfe0 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65  0 || pTab->pSele
2cfe1 63 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ct ) continue;. 
2cfe2 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e     if( (pWInfo->
2cfe3 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
2cfe4 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29 3d 3d  RE_OMIT_CLOSE)==
2cfe5 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  0 ){.      if( !
2cfe6 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73  pWInfo->okOnePas
2cfe7 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c  s && (pLevel->pl
2cfe8 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2cfe9 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 20  RE_IDX_ONLY)==0 
2cfea 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2cfeb 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2cfec 4f 50 5f 43 6c 6f 73 65 2c 20 70 54 61 62 49 74  OP_Close, pTabIt
2cfed 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  em->iCursor);.  
2cfee 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2cfef 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
2cff0 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
2cff1 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20  DEXED)!=0 ){.   
2cff2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2cff3 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
2cff4 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  se, pLevel->iIdx
2cff5 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Cur);.      }.  
2cff6 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
2cff7 68 69 73 20 73 63 61 6e 20 75 73 65 73 20 61 6e  his scan uses an
2cff8 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f 64   index, make cod
2cff9 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20  e substitutions 
2cffa 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20 20  to read data.   
2cffb 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 64   ** from the ind
2cffc 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63 65  ex in preference
2cffd 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20 53   to the table. S
2cffe 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20 6d  ometimes, this m
2cfff 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20  eans.    ** the 
2d000 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65 72  table need never
2d001 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20 54   be read from. T
2d002 68 69 73 20 69 73 20 61 20 70 65 72 66 6f 72 6d  his is a perform
2d003 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20 20  ance boost,.    
2d004 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65 20 6c  ** as the vdbe l
2d005 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69 6c  evel waits until
2d006 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 72 65   the table is re
2d007 61 64 20 62 65 66 6f 72 65 20 61 63 74 75 61 6c  ad before actual
2d008 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69 6e  ly.    ** seekin
2d009 67 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73  g the table curs
2d00a 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64  or to the record
2d00b 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
2d00c 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20  o the current.  
2d00d 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e    ** position in
2d00e 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20   the index..    
2d00f 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 73  ** .    ** Calls
2d010 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e   to the code gen
2d011 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65 65  erator in betwee
2d012 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  n sqlite3WhereBe
2d013 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20 73  gin and.    ** s
2d014 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20 77  qlite3WhereEnd w
2d015 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65 64  ill have created
2d016 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65 72   code that refer
2d017 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65 0a  ences the table.
2d018 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 2e      ** directly.
2d019 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61 6e    This loop scan
2d01a 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65 20  s all that code 
2d01b 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63 6f  looking for opco
2d01c 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  des.    ** that 
2d01d 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 74 61  reference the ta
2d01e 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74 73  ble and converts
2d01f 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f 64   them into opcod
2d020 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 72  es that.    ** r
2d021 65 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e 64  eference the ind
2d022 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ex..    */.    i
2d023 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
2d024 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2d025 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a  _INDEXED)!=0 ){.
2d026 20 20 20 20 20 20 69 6e 74 20 6b 2c 20 6a 2c 20        int k, j, 
2d027 6c 61 73 74 3b 0a 20 20 20 20 20 20 56 64 62 65  last;.      Vdbe
2d028 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 49  Op *pOp;.      I
2d029 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 70 4c 65  ndex *pIdx = pLe
2d02a 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78  vel->plan.u.pIdx
2d02b 3b 0a 20 20 20 20 20 20 69 6e 74 20 75 73 65 49  ;.      int useI
2d02c 6e 64 65 78 4f 6e 6c 79 20 3d 20 70 4c 65 76 65  ndexOnly = pLeve
2d02d 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2d02e 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
2d02f 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
2d030 20 70 49 64 78 21 3d 30 20 29 3b 0a 20 20 20 20   pIdx!=0 );.    
2d031 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56    pOp = sqlite3V
2d032 64 62 65 47 65 74 4f 70 28 76 2c 20 70 57 49 6e  dbeGetOp(v, pWIn
2d033 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20 20 20 20 20  fo->iTop);.     
2d034 20 6c 61 73 74 20 3d 20 73 71 6c 69 74 65 33 56   last = sqlite3V
2d035 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2d036 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70  );.      for(k=p
2d037 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20 6b 3c 6c  WInfo->iTop; k<l
2d038 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70 2b 2b 29  ast; k++, pOp++)
2d039 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  {.        if( pO
2d03a 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c 2d 3e 69  p->p1!=pLevel->i
2d03b 54 61 62 43 75 72 20 29 20 63 6f 6e 74 69 6e 75  TabCur ) continu
2d03c 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  e;.        if( p
2d03d 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43  Op->opcode==OP_C
2d03e 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
2d03f 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49     for(j=0; j<pI
2d040 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b  dx->nColumn; j++
2d041 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
2d042 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 70 49 64 78  f( pOp->p2==pIdx
2d043 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 20 29 7b  ->aiColumn[j] ){
2d044 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2d045 4f 70 2d 3e 70 32 20 3d 20 6a 3b 0a 20 20 20 20  Op->p2 = j;.    
2d046 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70            pOp->p
2d047 31 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  1 = pLevel->iIdx
2d048 43 75 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Cur;.           
2d049 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d04a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2d04b 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 61 73    }.          as
2d04c 73 65 72 74 28 21 75 73 65 49 6e 64 65 78 4f 6e  sert(!useIndexOn
2d04d 6c 79 20 7c 7c 20 6a 3c 70 49 64 78 2d 3e 6e 43  ly || j<pIdx->nC
2d04e 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20  olumn);.        
2d04f 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 6f  }else if( pOp->o
2d050 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 69 64 20  pcode==OP_Rowid 
2d051 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70  ){.          pOp
2d052 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e 69  ->p1 = pLevel->i
2d053 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20 20  IdxCur;.        
2d054 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
2d055 4f 50 5f 49 64 78 52 6f 77 69 64 3b 0a 20 20 20  OP_IdxRowid;.   
2d056 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2d057 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e  Op->opcode==OP_N
2d058 75 6c 6c 52 6f 77 20 26 26 20 75 73 65 49 6e 64  ullRow && useInd
2d059 65 78 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20  exOnly ){.      
2d05a 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20      pOp->opcode 
2d05b 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20  = OP_Noop;.     
2d05c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2d05d 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e   }.  }..  /* Fin
2d05e 61 6c 20 63 6c 65 61 6e 75 70 0a 20 20 2a 2f 0a  al cleanup.  */.
2d05f 20 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28    whereInfoFree(
2d060 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20 20 72  db, pWInfo);.  r
2d061 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  eturn;.}../*****
2d062 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2d063 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   where.c *******
2d064 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d065 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d066 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2d067 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2d068 66 69 6c 65 20 70 61 72 73 65 2e 63 20 2a 2a 2a  file parse.c ***
2d069 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d06a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d06b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 72 69  ********/./* Dri
2d06c 76 65 72 20 74 65 6d 70 6c 61 74 65 20 66 6f 72  ver template for
2d06d 20 74 68 65 20 4c 45 4d 4f 4e 20 70 61 72 73 65   the LEMON parse
2d06e 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20  r generator..** 
2d06f 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
2d070 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
2d071 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
2d072 64 65 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74 20  de..*/./* First 
2d073 6f 66 66 2c 20 63 6f 64 65 20 69 73 20 69 6e 63  off, code is inc
2d074 6c 75 64 65 64 20 74 68 61 74 20 66 6f 6c 6c 6f  luded that follo
2d075 77 73 20 74 68 65 20 22 69 6e 63 6c 75 64 65 22  ws the "include"
2d076 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20   declaration.** 
2d077 69 6e 20 74 68 65 20 69 6e 70 75 74 20 67 72 61  in the input gra
2d078 6d 6d 61 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a  mmar file. */...
2d079 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
2d07a 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
2d07b 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d  ure holds inform
2d07c 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a  ation about the.
2d07d 2a 2a 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  ** LIMIT clause 
2d07e 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
2d07f 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74  ement..*/.struct
2d080 20 4c 69 6d 69 74 56 61 6c 20 7b 0a 20 20 45 78   LimitVal {.  Ex
2d081 70 72 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20 2f  pr *pLimit;    /
2d082 2a 20 54 68 65 20 4c 49 4d 49 54 20 65 78 70 72  * The LIMIT expr
2d083 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66  ession.  NULL if
2d084 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 6d   there is no lim
2d085 69 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f  it */.  Expr *pO
2d086 66 66 73 65 74 3b 20 20 20 2f 2a 20 54 68 65 20  ffset;   /* The 
2d087 4f 46 46 53 45 54 20 65 78 70 72 65 73 73 69 6f  OFFSET expressio
2d088 6e 2e 20 20 4e 55 4c 4c 20 69 66 20 74 68 65 72  n.  NULL if ther
2d089 65 20 69 73 20 6e 6f 6e 65 20 2a 2f 0a 7d 3b 0a  e is none */.};.
2d08a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
2d08b 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
2d08c 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20  ture is used to 
2d08d 73 74 6f 72 65 20 74 68 65 20 4c 49 4b 45 2c 0a  store the LIKE,.
2d08e 2a 2a 20 47 4c 4f 42 2c 20 4e 4f 54 20 4c 49 4b  ** GLOB, NOT LIK
2d08f 45 2c 20 61 6e 64 20 4e 4f 54 20 47 4c 4f 42 20  E, and NOT GLOB 
2d090 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a 73 74  operators..*/.st
2d091 72 75 63 74 20 4c 69 6b 65 4f 70 20 7b 0a 20 20  ruct LikeOp {.  
2d092 54 6f 6b 65 6e 20 65 4f 70 65 72 61 74 6f 72 3b  Token eOperator;
2d093 20 20 2f 2a 20 22 6c 69 6b 65 22 20 6f 72 20 22    /* "like" or "
2d094 67 6c 6f 62 22 20 6f 72 20 22 72 65 67 65 78 70  glob" or "regexp
2d095 22 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 74 3b 20  " */.  int not; 
2d096 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2d097 69 66 20 74 68 65 20 4e 4f 54 20 6b 65 79 77 6f  if the NOT keywo
2d098 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  rd is present */
2d099 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
2d09a 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
2d09b 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
2d09c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
2d09d 65 76 65 6e 74 20 6f 66 20 61 0a 2a 2a 20 54 52  event of a.** TR
2d09e 49 47 47 45 52 2e 20 20 22 61 22 20 69 73 20 74  IGGER.  "a" is t
2d09f 68 65 20 65 76 65 6e 74 20 74 79 70 65 2c 20 6f  he event type, o
2d0a0 6e 65 20 6f 66 20 54 4b 5f 55 50 44 41 54 45 2c  ne of TK_UPDATE,
2d0a1 20 54 4b 5f 49 4e 53 45 52 54 2c 0a 2a 2a 20 54   TK_INSERT,.** T
2d0a2 4b 5f 44 45 4c 45 54 45 2c 20 6f 72 20 54 4b 5f  K_DELETE, or TK_
2d0a3 49 4e 53 54 45 41 44 2e 20 20 49 66 20 74 68 65  INSTEAD.  If the
2d0a4 20 65 76 65 6e 74 20 69 73 20 6f 66 20 74 68 65   event is of the
2d0a5 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20   form.**.**     
2d0a6 20 55 50 44 41 54 45 20 4f 4e 20 28 61 2c 62 2c   UPDATE ON (a,b,
2d0a7 63 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68  c).**.** Then th
2d0a8 65 20 22 62 22 20 49 64 4c 69 73 74 20 72 65 63  e "b" IdList rec
2d0a9 6f 72 64 73 20 74 68 65 20 6c 69 73 74 20 22 61  ords the list "a
2d0aa 2c 62 2c 63 22 2e 0a 2a 2f 0a 73 74 72 75 63 74  ,b,c"..*/.struct
2d0ab 20 54 72 69 67 45 76 65 6e 74 20 7b 20 69 6e 74   TrigEvent { int
2d0ac 20 61 3b 20 49 64 4c 69 73 74 20 2a 20 62 3b 20   a; IdList * b; 
2d0ad 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
2d0ae 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
2d0af 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 74 68  ructure holds th
2d0b0 65 20 41 54 54 41 43 48 20 6b 65 79 20 61 6e 64  e ATTACH key and
2d0b1 20 74 68 65 20 6b 65 79 20 74 79 70 65 2e 0a 2a   the key type..*
2d0b2 2f 0a 73 74 72 75 63 74 20 41 74 74 61 63 68 4b  /.struct AttachK
2d0b3 65 79 20 7b 20 69 6e 74 20 74 79 70 65 3b 20 20  ey { int type;  
2d0b4 54 6f 6b 65 6e 20 6b 65 79 3b 20 7d 3b 0a 0a 2f  Token key; };../
2d0b5 2a 20 4e 65 78 74 20 69 73 20 61 6c 6c 20 74 6f  * Next is all to
2d0b6 6b 65 6e 20 76 61 6c 75 65 73 2c 20 69 6e 20 61  ken values, in a
2d0b7 20 66 6f 72 6d 20 73 75 69 74 61 62 6c 65 20 66   form suitable f
2d0b8 6f 72 20 75 73 65 20 62 79 20 6d 61 6b 65 68 65  or use by makehe
2d0b9 61 64 65 72 73 2e 0a 2a 2a 20 54 68 69 73 20 73  aders..** This s
2d0ba 65 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 6e  ection will be n
2d0bb 75 6c 6c 20 75 6e 6c 65 73 73 20 6c 65 6d 6f 6e  ull unless lemon
2d0bc 20 69 73 20 72 75 6e 20 77 69 74 68 20 74 68 65   is run with the
2d0bd 20 2d 6d 20 73 77 69 74 63 68 2e 0a 2a 2f 0a 2f   -m switch..*/./
2d0be 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73  * .** These cons
2d0bf 74 61 6e 74 73 20 28 61 6c 6c 20 67 65 6e 65 72  tants (all gener
2d0c0 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  ated automatical
2d0c1 6c 79 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ly by the parser
2d0c2 20 67 65 6e 65 72 61 74 6f 72 29 0a 2a 2a 20 73   generator).** s
2d0c3 70 65 63 69 66 79 20 74 68 65 20 76 61 72 69 6f  pecify the vario
2d0c4 75 73 20 6b 69 6e 64 73 20 6f 66 20 74 6f 6b 65  us kinds of toke
2d0c5 6e 73 20 28 74 65 72 6d 69 6e 61 6c 73 29 20 74  ns (terminals) t
2d0c6 68 61 74 20 74 68 65 20 70 61 72 73 65 72 0a 2a  hat the parser.*
2d0c7 2a 20 75 6e 64 65 72 73 74 61 6e 64 73 2e 20 0a  * understands. .
2d0c8 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 79 6d 62 6f  **.** Each symbo
2d0c9 6c 20 68 65 72 65 20 69 73 20 61 20 74 65 72 6d  l here is a term
2d0ca 69 6e 61 6c 20 73 79 6d 62 6f 6c 20 69 6e 20 74  inal symbol in t
2d0cb 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2f 0a 2f  he grammar..*/./
2d0cc 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
2d0cd 49 4e 54 45 52 46 41 43 45 20 6d 61 63 72 6f 20  INTERFACE macro 
2d0ce 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  is defined..*/.#
2d0cf 69 66 6e 64 65 66 20 49 4e 54 45 52 46 41 43 45  ifndef INTERFACE
2d0d0 0a 23 20 64 65 66 69 6e 65 20 49 4e 54 45 52 46  .# define INTERF
2d0d1 41 43 45 20 31 0a 23 65 6e 64 69 66 0a 2f 2a 20  ACE 1.#endif./* 
2d0d2 54 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20 69  The next thing i
2d0d3 6e 63 6c 75 64 65 64 20 69 73 20 73 65 72 69 65  ncluded is serie
2d0d4 73 20 6f 66 20 64 65 66 69 6e 65 73 20 77 68 69  s of defines whi
2d0d5 63 68 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 76 61  ch control.** va
2d0d6 72 69 6f 75 73 20 61 73 70 65 63 74 73 20 6f 66  rious aspects of
2d0d7 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 70   the generated p
2d0d8 61 72 73 65 72 2e 0a 2a 2a 20 20 20 20 59 59 43  arser..**    YYC
2d0d9 4f 44 45 54 59 50 45 20 20 20 20 20 20 20 20 20  ODETYPE         
2d0da 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65  is the data type
2d0db 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69 6e   used for storin
2d0dc 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20  g terminal.**   
2d0dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d0de 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69      and nontermi
2d0df 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22 75  nal numbers.  "u
2d0e0 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20 69 73  nsigned char" is
2d0e1 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2d0e2 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20 69            used i
2d0e3 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65  f there are fewe
2d0e4 72 20 74 68 61 6e 20 32 35 30 20 74 65 72 6d 69  r than 250 termi
2d0e5 6e 61 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  nals.**         
2d0e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
2d0e7 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 2e 20  d nonterminals. 
2d0e8 20 22 69 6e 74 22 20 69 73 20 75 73 65 64 20 6f   "int" is used o
2d0e9 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20 20  therwise..**    
2d0ea 59 59 4e 4f 43 4f 44 45 20 20 20 20 20 20 20 20  YYNOCODE        
2d0eb 20 20 20 69 73 20 61 20 6e 75 6d 62 65 72 20 6f     is a number o
2d0ec 66 20 74 79 70 65 20 59 59 43 4f 44 45 54 59 50  f type YYCODETYP
2d0ed 45 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f  E which correspo
2d0ee 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nds.**          
2d0ef 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 20               to 
2d0f0 6e 6f 20 6c 65 67 61 6c 20 74 65 72 6d 69 6e 61  no legal termina
2d0f1 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c  l or nonterminal
2d0f2 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 0a 2a   number.  This.*
2d0f3 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2d0f4 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20 69          number i
2d0f5 73 20 75 73 65 64 20 74 6f 20 66 69 6c 6c 20 69  s used to fill i
2d0f6 6e 20 65 6d 70 74 79 20 73 6c 6f 74 73 20 6f 66  n empty slots of
2d0f7 20 74 68 65 20 68 61 73 68 20 0a 2a 2a 20 20 20   the hash .**   
2d0f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d0f9 20 20 20 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20      table..**   
2d0fa 20 59 59 46 41 4c 4c 42 41 43 4b 20 20 20 20 20   YYFALLBACK     
2d0fb 20 20 20 20 49 66 20 64 65 66 69 6e 65 64 2c 20      If defined, 
2d0fc 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 74  this indicates t
2d0fd 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  hat one or more 
2d0fe 74 6f 6b 65 6e 73 0a 2a 2a 20 20 20 20 20 20 20  tokens.**       
2d0ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d100 68 61 76 65 20 66 61 6c 6c 2d 62 61 63 6b 20 76  have fall-back v
2d101 61 6c 75 65 73 20 77 68 69 63 68 20 73 68 6f 75  alues which shou
2d102 6c 64 20 62 65 20 75 73 65 64 20 69 66 20 74 68  ld be used if th
2d103 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
2d104 20 20 20 20 20 20 20 20 20 20 20 6f 72 69 67 69             origi
2d105 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65  nal value of the
2d106 20 74 6f 6b 65 6e 20 77 69 6c 6c 20 6e 6f 74 20   token will not 
2d107 70 61 72 73 65 2e 0a 2a 2a 20 20 20 20 59 59 41  parse..**    YYA
2d108 43 54 49 4f 4e 54 59 50 45 20 20 20 20 20 20 20  CTIONTYPE       
2d109 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70 65  is the data type
2d10a 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69 6e   used for storin
2d10b 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20 20  g terminal.**   
2d10c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d10d 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69      and nontermi
2d10e 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22 75  nal numbers.  "u
2d10f 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20 69 73  nsigned char" is
2d110 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2d111 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20 69            used i
2d112 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65  f there are fewe
2d113 72 20 74 68 61 6e 20 32 35 30 20 72 75 6c 65 73  r than 250 rules
2d114 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
2d115 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
2d116 61 74 65 73 20 63 6f 6d 62 69 6e 65 64 2e 20 20  ates combined.  
2d117 22 69 6e 74 22 20 69 73 20 75 73 65 64 20 6f 74  "int" is used ot
2d118 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20 20 73  herwise..**    s
2d119 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
2d11a 4e 54 59 50 45 20 20 20 20 20 69 73 20 74 68 65  NTYPE     is the
2d11b 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64 20   data type used 
2d11c 66 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73  for minor tokens
2d11d 20 67 69 76 65 6e 20 0a 2a 2a 20 20 20 20 20 20   given .**      
2d11e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d11f 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68 65   directly to the
2d120 20 70 61 72 73 65 72 20 66 72 6f 6d 20 74 68 65   parser from the
2d121 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2a 20 20   tokenizer..**  
2d122 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 20 20    YYMINORTYPE   
2d123 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74 61       is the data
2d124 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20 61   type used for a
2d125 6c 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73 2e  ll minor tokens.
2d126 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2d127 20 20 20 20 20 20 20 20 20 20 54 68 69 73 20 69            This i
2d128 73 20 74 79 70 69 63 61 6c 6c 79 20 61 20 75 6e  s typically a un
2d129 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 74 79 70 65  ion of many type
2d12a 73 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20 20  s, one of.**    
2d12b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d12c 20 20 20 77 68 69 63 68 20 69 73 20 73 71 6c 69     which is sqli
2d12d 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59  te3ParserTOKENTY
2d12e 50 45 2e 20 20 54 68 65 20 65 6e 74 72 79 20 69  PE.  The entry i
2d12f 6e 20 74 68 65 20 75 6e 69 6f 6e 0a 2a 2a 20 20  n the union.**  
2d130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d131 20 20 20 20 20 66 6f 72 20 62 61 73 65 20 74 6f       for base to
2d132 6b 65 6e 73 20 69 73 20 63 61 6c 6c 65 64 20 22  kens is called "
2d133 79 79 30 22 2e 0a 2a 2a 20 20 20 20 59 59 53 54  yy0"..**    YYST
2d134 41 43 4b 44 45 50 54 48 20 20 20 20 20 20 20 69  ACKDEPTH       i
2d135 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 65  s the maximum de
2d136 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65  pth of the parse
2d137 72 27 73 20 73 74 61 63 6b 2e 20 20 49 66 0a 2a  r's stack.  If.*
2d138 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2d139 20 20 20 20 20 20 20 20 7a 65 72 6f 20 74 68 65          zero the
2d13a 20 73 74 61 63 6b 20 69 73 20 64 79 6e 61 6d 69   stack is dynami
2d13b 63 61 6c 6c 79 20 73 69 7a 65 64 20 75 73 69 6e  cally sized usin
2d13c 67 20 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20  g realloc().**  
2d13d 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
2d13e 52 47 5f 53 44 45 43 4c 20 20 20 20 20 41 20 73  RG_SDECL     A s
2d13f 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 20 64  tatic variable d
2d140 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20 74  eclaration for t
2d141 68 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  he %extra_argume
2d142 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  nt.**    sqlite3
2d143 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20  ParserARG_PDECL 
2d144 20 20 20 20 41 20 70 61 72 61 6d 65 74 65 72 20      A parameter 
2d145 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20  declaration for 
2d146 74 68 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d  the %extra_argum
2d147 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  ent.**    sqlite
2d148 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45  3ParserARG_STORE
2d149 20 20 20 20 20 43 6f 64 65 20 74 6f 20 73 74 6f       Code to sto
2d14a 72 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  re %extra_argume
2d14b 6e 74 20 69 6e 74 6f 20 79 79 70 50 61 72 73 65  nt into yypParse
2d14c 72 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50  r.**    sqlite3P
2d14d 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 20 20  arserARG_FETCH  
2d14e 20 20 20 43 6f 64 65 20 74 6f 20 65 78 74 72 61     Code to extra
2d14f 63 74 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65  ct %extra_argume
2d150 6e 74 20 66 72 6f 6d 20 79 79 70 50 61 72 73 65  nt from yypParse
2d151 72 0a 2a 2a 20 20 20 20 59 59 4e 53 54 41 54 45  r.**    YYNSTATE
2d152 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63             the c
2d153 6f 6d 62 69 6e 65 64 20 6e 75 6d 62 65 72 20 6f  ombined number o
2d154 66 20 73 74 61 74 65 73 2e 0a 2a 2a 20 20 20 20  f states..**    
2d155 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20 20  YYNRULE         
2d156 20 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66     the number of
2d157 20 72 75 6c 65 73 20 69 6e 20 74 68 65 20 67 72   rules in the gr
2d158 61 6d 6d 61 72 0a 2a 2a 20 20 20 20 59 59 45 52  ammar.**    YYER
2d159 52 4f 52 53 59 4d 42 4f 4c 20 20 20 20 20 20 69  RORSYMBOL      i
2d15a 73 20 74 68 65 20 63 6f 64 65 20 6e 75 6d 62 65  s the code numbe
2d15b 72 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 73  r of the error s
2d15c 79 6d 62 6f 6c 2e 20 20 49 66 20 6e 6f 74 0a 2a  ymbol.  If not.*
2d15d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2d15e 20 20 20 20 20 20 20 20 64 65 66 69 6e 65 64 2c          defined,
2d15f 20 74 68 65 6e 20 64 6f 20 6e 6f 20 65 72 72 6f   then do no erro
2d160 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2f  r processing..*/
2d161 0a 23 64 65 66 69 6e 65 20 59 59 43 4f 44 45 54  .#define YYCODET
2d162 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68 6f  YPE unsigned sho
2d163 72 74 20 69 6e 74 0a 23 64 65 66 69 6e 65 20 59  rt int.#define Y
2d164 59 4e 4f 43 4f 44 45 20 32 35 32 0a 23 64 65 66  YNOCODE 252.#def
2d165 69 6e 65 20 59 59 41 43 54 49 4f 4e 54 59 50 45  ine YYACTIONTYPE
2d166 20 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20   unsigned short 
2d167 69 6e 74 0a 23 64 65 66 69 6e 65 20 59 59 57 49  int.#define YYWI
2d168 4c 44 43 41 52 44 20 36 35 0a 23 64 65 66 69 6e  LDCARD 65.#defin
2d169 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54  e sqlite3ParserT
2d16a 4f 4b 45 4e 54 59 50 45 20 54 6f 6b 65 6e 0a 74  OKENTYPE Token.t
2d16b 79 70 65 64 65 66 20 75 6e 69 6f 6e 20 7b 0a 20  ypedef union {. 
2d16c 20 69 6e 74 20 79 79 69 6e 69 74 3b 0a 20 20 73   int yyinit;.  s
2d16d 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
2d16e 4e 54 59 50 45 20 79 79 30 3b 0a 20 20 45 78 70  NTYPE yy0;.  Exp
2d16f 72 2a 20 79 79 37 32 3b 0a 20 20 54 72 69 67 67  r* yy72;.  Trigg
2d170 65 72 53 74 65 70 2a 20 79 79 31 34 35 3b 0a 20  erStep* yy145;. 
2d171 20 45 78 70 72 4c 69 73 74 2a 20 79 79 31 34 38   ExprList* yy148
2d172 3b 0a 20 20 53 72 63 4c 69 73 74 2a 20 79 79 31  ;.  SrcList* yy1
2d173 38 35 3b 0a 20 20 69 6e 74 20 79 79 31 39 34 3b  85;.  int yy194;
2d174 0a 20 20 53 65 6c 65 63 74 2a 20 79 79 32 34 33  .  Select* yy243
2d175 3b 0a 20 20 49 64 4c 69 73 74 2a 20 79 79 32 35  ;.  IdList* yy25
2d176 34 3b 0a 20 20 73 74 72 75 63 74 20 54 72 69 67  4;.  struct Trig
2d177 45 76 65 6e 74 20 79 79 33 33 32 3b 0a 20 20 73  Event yy332;.  s
2d178 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 79  truct LimitVal y
2d179 79 33 35 34 3b 0a 20 20 73 74 72 75 63 74 20 4c  y354;.  struct L
2d17a 69 6b 65 4f 70 20 79 79 33 39 32 3b 0a 20 20 73  ikeOp yy392;.  s
2d17b 74 72 75 63 74 20 7b 69 6e 74 20 76 61 6c 75 65  truct {int value
2d17c 3b 20 69 6e 74 20 6d 61 73 6b 3b 7d 20 79 79 34  ; int mask;} yy4
2d17d 39 37 3b 0a 7d 20 59 59 4d 49 4e 4f 52 54 59 50  97;.} YYMINORTYP
2d17e 45 3b 0a 23 69 66 6e 64 65 66 20 59 59 53 54 41  E;.#ifndef YYSTA
2d17f 43 4b 44 45 50 54 48 0a 23 64 65 66 69 6e 65 20  CKDEPTH.#define 
2d180 59 59 53 54 41 43 4b 44 45 50 54 48 20 31 30 30  YYSTACKDEPTH 100
2d181 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20  .#endif.#define 
2d182 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2d183 5f 53 44 45 43 4c 20 50 61 72 73 65 20 2a 70 50  _SDECL Parse *pP
2d184 61 72 73 65 3b 0a 23 64 65 66 69 6e 65 20 73 71  arse;.#define sq
2d185 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 50  lite3ParserARG_P
2d186 44 45 43 4c 20 2c 50 61 72 73 65 20 2a 70 50 61  DECL ,Parse *pPa
2d187 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  rse.#define sqli
2d188 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
2d189 43 48 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  CH Parse *pParse
2d18a 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50   = yypParser->pP
2d18b 61 72 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  arse.#define sql
2d18c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54  ite3ParserARG_ST
2d18d 4f 52 45 20 79 79 70 50 61 72 73 65 72 2d 3e 70  ORE yypParser->p
2d18e 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 0a 23  Parse = pParse.#
2d18f 64 65 66 69 6e 65 20 59 59 4e 53 54 41 54 45 20  define YYNSTATE 
2d190 36 31 36 0a 23 64 65 66 69 6e 65 20 59 59 4e 52  616.#define YYNR
2d191 55 4c 45 20 33 32 33 0a 23 64 65 66 69 6e 65 20  ULE 323.#define 
2d192 59 59 46 41 4c 4c 42 41 43 4b 20 31 0a 23 64 65  YYFALLBACK 1.#de
2d193 66 69 6e 65 20 59 59 5f 4e 4f 5f 41 43 54 49 4f  fine YY_NO_ACTIO
2d194 4e 20 20 20 20 20 20 28 59 59 4e 53 54 41 54 45  N      (YYNSTATE
2d195 2b 59 59 4e 52 55 4c 45 2b 32 29 0a 23 64 65 66  +YYNRULE+2).#def
2d196 69 6e 65 20 59 59 5f 41 43 43 45 50 54 5f 41 43  ine YY_ACCEPT_AC
2d197 54 49 4f 4e 20 20 28 59 59 4e 53 54 41 54 45 2b  TION  (YYNSTATE+
2d198 59 59 4e 52 55 4c 45 2b 31 29 0a 23 64 65 66 69  YYNRULE+1).#defi
2d199 6e 65 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49  ne YY_ERROR_ACTI
2d19a 4f 4e 20 20 20 28 59 59 4e 53 54 41 54 45 2b 59  ON   (YYNSTATE+Y
2d19b 59 4e 52 55 4c 45 29 0a 0a 2f 2a 20 54 68 65 20  YNRULE)../* The 
2d19c 79 79 7a 65 72 6f 6d 69 6e 6f 72 20 63 6f 6e 73  yyzerominor cons
2d19d 74 61 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20  tant is used to 
2d19e 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 73 74 61  initialize insta
2d19f 6e 63 65 73 20 6f 66 0a 2a 2a 20 59 59 4d 49 4e  nces of.** YYMIN
2d1a0 4f 52 54 59 50 45 20 6f 62 6a 65 63 74 73 20 74  ORTYPE objects t
2d1a1 6f 20 7a 65 72 6f 2e 20 2a 2f 0a 73 74 61 74 69  o zero. */.stati
2d1a2 63 20 63 6f 6e 73 74 20 59 59 4d 49 4e 4f 52 54  c const YYMINORT
2d1a3 59 50 45 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20  YPE yyzerominor 
2d1a4 3d 20 7b 20 30 20 7d 3b 0a 0a 0a 2f 2a 20 4e 65  = { 0 };.../* Ne
2d1a5 78 74 20 61 72 65 20 74 68 65 20 74 61 62 6c 65  xt are the table
2d1a6 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
2d1a7 69 6e 65 20 77 68 61 74 20 61 63 74 69 6f 6e 20  ine what action 
2d1a8 74 6f 20 74 61 6b 65 20 62 61 73 65 64 20 6f 6e  to take based on
2d1a9 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
2d1aa 73 74 61 74 65 20 61 6e 64 20 6c 6f 6f 6b 61 68  state and lookah
2d1ab 65 61 64 20 74 6f 6b 65 6e 2e 20 20 54 68 65 73  ead token.  Thes
2d1ac 65 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65  e tables are use
2d1ad 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a  d to implement.*
2d1ae 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  * functions that
2d1af 20 74 61 6b 65 20 61 20 73 74 61 74 65 20 6e 75   take a state nu
2d1b0 6d 62 65 72 20 61 6e 64 20 6c 6f 6f 6b 61 68 65  mber and lookahe
2d1b1 61 64 20 76 61 6c 75 65 20 61 6e 64 20 72 65 74  ad value and ret
2d1b2 75 72 6e 20 61 6e 0a 2a 2a 20 61 63 74 69 6f 6e  urn an.** action
2d1b3 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2a 0a 2a   integer.  .**.*
2d1b4 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20 61 63  * Suppose the ac
2d1b5 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 69 73 20  tion integer is 
2d1b6 4e 2e 20 20 54 68 65 6e 20 74 68 65 20 61 63 74  N.  Then the act
2d1b7 69 6f 6e 20 69 73 20 64 65 74 65 72 6d 69 6e 65  ion is determine
2d1b8 64 20 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 0a  d as.** follows.
2d1b9 2a 2a 0a 2a 2a 20 20 20 30 20 3c 3d 20 4e 20 3c  **.**   0 <= N <
2d1ba 20 59 59 4e 53 54 41 54 45 20 20 20 20 20 20 20   YYNSTATE       
2d1bb 20 20 20 20 20 20 20 20 20 20 20 53 68 69 66 74             Shift
2d1bc 20 4e 2e 20 20 54 68 61 74 20 69 73 2c 20 70 75   N.  That is, pu
2d1bd 73 68 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64  sh the lookahead
2d1be 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2d1bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d1c0 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 6f           token o
2d1c1 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61 6e  nto the stack an
2d1c2 64 20 67 6f 74 6f 20 73 74 61 74 65 20 4e 2e 0a  d goto state N..
2d1c3 2a 2a 0a 2a 2a 20 20 20 59 59 4e 53 54 41 54 45  **.**   YYNSTATE
2d1c4 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45   <= N < YYNSTATE
2d1c5 2b 59 59 4e 52 55 4c 45 20 20 20 52 65 64 75 63  +YYNRULE   Reduc
2d1c6 65 20 62 79 20 72 75 6c 65 20 4e 2d 59 59 4e 53  e by rule N-YYNS
2d1c7 54 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20  TATE..**.**   N 
2d1c8 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52  == YYNSTATE+YYNR
2d1c9 55 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ULE             
2d1ca 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20   A syntax error 
2d1cb 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a  has occurred..**
2d1cc 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54  .**   N == YYNST
2d1cd 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 20 20 20  ATE+YYNRULE+1   
2d1ce 20 20 20 20 20 20 20 20 20 54 68 65 20 70 61 72           The par
2d1cf 73 65 72 20 61 63 63 65 70 74 73 20 69 74 73 20  ser accepts its 
2d1d0 69 6e 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e  input..**.**   N
2d1d1 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e   == YYNSTATE+YYN
2d1d2 52 55 4c 45 2b 32 20 20 20 20 20 20 20 20 20 20  RULE+2          
2d1d3 20 20 4e 6f 20 73 75 63 68 20 61 63 74 69 6f 6e    No such action
2d1d4 2e 20 20 44 65 6e 6f 74 65 73 20 75 6e 75 73 65  .  Denotes unuse
2d1d5 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
2d1d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d1d7 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 73 20            slots 
2d1d8 69 6e 20 74 68 65 20 79 79 5f 61 63 74 69 6f 6e  in the yy_action
2d1d9 5b 5d 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  [] table..**.** 
2d1da 54 68 65 20 61 63 74 69 6f 6e 20 74 61 62 6c 65  The action table
2d1db 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20   is constructed 
2d1dc 61 73 20 61 20 73 69 6e 67 6c 65 20 6c 61 72 67  as a single larg
2d1dd 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 79 79  e table named yy
2d1de 5f 61 63 74 69 6f 6e 5b 5d 2e 0a 2a 2a 20 47 69  _action[]..** Gi
2d1df 76 65 6e 20 73 74 61 74 65 20 53 20 61 6e 64 20  ven state S and 
2d1e0 6c 6f 6f 6b 61 68 65 61 64 20 58 2c 20 74 68 65  lookahead X, the
2d1e1 20 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75   action is compu
2d1e2 74 65 64 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20 20  ted as.**.**    
2d1e3 20 20 79 79 5f 61 63 74 69 6f 6e 5b 20 79 79 5f    yy_action[ yy_
2d1e4 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 20 2b 20  shift_ofst[S] + 
2d1e5 58 20 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  X ].**.** If the
2d1e6 20 69 6e 64 65 78 20 76 61 6c 75 65 20 79 79 5f   index value yy_
2d1e7 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58 20  shift_ofst[S]+X 
2d1e8 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  is out of range 
2d1e9 6f 72 20 69 66 20 74 68 65 20 76 61 6c 75 65 0a  or if the value.
2d1ea 2a 2a 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  ** yy_lookahead[
2d1eb 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d  yy_shift_ofst[S]
2d1ec 2b 58 5d 20 69 73 20 6e 6f 74 20 65 71 75 61 6c  +X] is not equal
2d1ed 20 74 6f 20 58 20 6f 72 20 69 66 20 79 79 5f 73   to X or if yy_s
2d1ee 68 69 66 74 5f 6f 66 73 74 5b 53 5d 0a 2a 2a 20  hift_ofst[S].** 
2d1ef 69 73 20 65 71 75 61 6c 20 74 6f 20 59 59 5f 53  is equal to YY_S
2d1f0 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2c 20 69  HIFT_USE_DFLT, i
2d1f1 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
2d1f2 20 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 69   action is not i
2d1f3 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 61  n the table.** a
2d1f4 6e 64 20 74 68 61 74 20 79 79 5f 64 65 66 61 75  nd that yy_defau
2d1f5 6c 74 5b 53 5d 20 73 68 6f 75 6c 64 20 62 65 20  lt[S] should be 
2d1f6 75 73 65 64 20 69 6e 73 74 65 61 64 2e 20 20 0a  used instead.  .
2d1f7 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c  **.** The formul
2d1f8 61 20 61 62 6f 76 65 20 69 73 20 66 6f 72 20 63  a above is for c
2d1f9 6f 6d 70 75 74 69 6e 67 20 74 68 65 20 61 63 74  omputing the act
2d1fa 69 6f 6e 20 77 68 65 6e 20 74 68 65 20 6c 6f 6f  ion when the loo
2d1fb 6b 61 68 65 61 64 20 69 73 0a 2a 2a 20 61 20 74  kahead is.** a t
2d1fc 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 2e 20  erminal symbol. 
2d1fd 20 49 66 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61   If the lookahea
2d1fe 64 20 69 73 20 61 20 6e 6f 6e 2d 74 65 72 6d 69  d is a non-termi
2d1ff 6e 61 6c 20 28 61 73 20 6f 63 63 75 72 73 20 61  nal (as occurs a
2d200 66 74 65 72 0a 2a 2a 20 61 20 72 65 64 75 63 65  fter.** a reduce
2d201 20 61 63 74 69 6f 6e 29 20 74 68 65 6e 20 74 68   action) then th
2d202 65 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74  e yy_reduce_ofst
2d203 5b 5d 20 61 72 72 61 79 20 69 73 20 75 73 65 64  [] array is used
2d204 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   in place of.** 
2d205 74 68 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  the yy_shift_ofs
2d206 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 59 59  t[] array and YY
2d207 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54  _REDUCE_USE_DFLT
2d208 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63   is used in plac
2d209 65 20 6f 66 0a 2a 2a 20 59 59 5f 53 48 49 46 54  e of.** YY_SHIFT
2d20a 5f 55 53 45 5f 44 46 4c 54 2e 0a 2a 2a 0a 2a 2a  _USE_DFLT..**.**
2d20b 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
2d20c 72 65 20 74 68 65 20 74 61 62 6c 65 73 20 67 65  re the tables ge
2d20d 6e 65 72 61 74 65 64 20 69 6e 20 74 68 69 73 20  nerated in this 
2d20e 73 65 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20  section:.**.**  
2d20f 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 20 20 20 20  yy_action[]     
2d210 20 20 20 41 20 73 69 6e 67 6c 65 20 74 61 62 6c     A single tabl
2d211 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c  e containing all
2d212 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79   actions..**  yy
2d213 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 20 20 20  _lookahead[]    
2d214 20 41 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e   A table contain
2d215 69 6e 67 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61  ing the lookahea
2d216 64 20 66 6f 72 20 65 61 63 68 20 65 6e 74 72 79  d for each entry
2d217 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   in.**          
2d218 20 20 20 20 20 20 20 20 20 20 20 79 79 5f 61 63             yy_ac
2d219 74 69 6f 6e 2e 20 20 55 73 65 64 20 74 6f 20 64  tion.  Used to d
2d21a 65 74 65 63 74 20 68 61 73 68 20 63 6f 6c 6c 69  etect hash colli
2d21b 73 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 73 68  sions..**  yy_sh
2d21c 69 66 74 5f 6f 66 73 74 5b 5d 20 20 20 20 46 6f  ift_ofst[]    Fo
2d21d 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74 68  r each state, th
2d21e 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79  e offset into yy
2d21f 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20  _action for.**  
2d220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d221 20 20 20 73 68 69 66 74 69 6e 67 20 74 65 72 6d     shifting term
2d222 69 6e 61 6c 73 2e 0a 2a 2a 20 20 79 79 5f 72 65  inals..**  yy_re
2d223 64 75 63 65 5f 6f 66 73 74 5b 5d 20 20 20 46 6f  duce_ofst[]   Fo
2d224 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74 68  r each state, th
2d225 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79  e offset into yy
2d226 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20  _action for.**  
2d227 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d228 20 20 20 73 68 69 66 74 69 6e 67 20 6e 6f 6e 2d     shifting non-
2d229 74 65 72 6d 69 6e 61 6c 73 20 61 66 74 65 72 20  terminals after 
2d22a 61 20 72 65 64 75 63 65 2e 0a 2a 2a 20 20 79 79  a reduce..**  yy
2d22b 5f 64 65 66 61 75 6c 74 5b 5d 20 20 20 20 20 20  _default[]      
2d22c 20 44 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20   Default action 
2d22d 66 6f 72 20 65 61 63 68 20 73 74 61 74 65 2e 0a  for each state..
2d22e 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
2d22f 59 59 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f  YYACTIONTYPE yy_
2d230 61 63 74 69 6f 6e 5b 5d 20 3d 20 7b 0a 20 2f 2a  action[] = {. /*
2d231 20 20 20 20 20 30 20 2a 2f 20 20 20 33 30 34 2c       0 */   304,
2d232 20 20 39 34 30 2c 20 20 31 37 36 2c 20 20 36 31    940,  176,  61
2d233 35 2c 20 20 20 20 32 2c 20 20 31 35 30 2c 20 20  5,    2,  150,  
2d234 32 31 34 2c 20 20 34 33 39 2c 20 20 20 32 34 2c  214,  439,   24,
2d235 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 20 31 30     24,. /*    10
2d236 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c   */    24,   24,
2d237 20 20 34 38 38 2c 20 20 20 32 36 2c 20 20 20 32    488,   26,   2
2d238 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2d239 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
2d23a 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20  . /*    20 */   
2d23b 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c   28,   28,   29,
2d23c 20 20 32 31 36 2c 20 20 34 31 33 2c 20 20 34 31    216,  413,  41
2d23d 34 2c 20 20 32 31 32 2c 20 20 34 31 33 2c 20 20  4,  212,  413,  
2d23e 34 31 34 2c 20 20 34 34 36 2c 0a 20 2f 2a 20 20  414,  446,. /*  
2d23f 20 20 33 30 20 2a 2f 20 20 20 34 35 32 2c 20 20    30 */   452,  
2d240 20 33 31 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   31,   26,   26,
2d241 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2d242 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20  7,   27,   28,  
2d243 20 32 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a   28,. /*    40 *
2d244 2f 20 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  /    28,   29,  
2d245 32 31 36 2c 20 20 20 33 30 2c 20 20 34 38 33 2c  216,   30,  483,
2d246 20 20 20 33 32 2c 20 20 31 33 34 2c 20 20 20 32     32,  134,   2
2d247 33 2c 20 20 20 32 32 2c 20 20 33 30 38 2c 0a 20  3,   22,  308,. 
2d248 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 34 35  /*    50 */   45
2d249 36 2c 20 20 34 35 37 2c 20 20 34 35 33 2c 20 20  6,  457,  453,  
2d24a 34 35 33 2c 20 20 20 32 35 2c 20 20 20 32 35 2c  453,   25,   25,
2d24b 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
2d24c 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 20  4,   24,. /*    
2d24d 36 30 20 2a 2f 20 20 20 34 33 36 2c 20 20 20 32  60 */   436,   2
2d24e 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2d24f 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c   26,   27,   27,
2d250 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2d251 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20  8,. /*    70 */ 
2d252 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20 33 30     29,  216,  30
2d253 34 2c 20 20 32 31 36 2c 20 20 33 31 31 2c 20 20  4,  216,  311,  
2d254 34 33 39 2c 20 20 35 31 32 2c 20 20 34 39 30 2c  439,  512,  490,
2d255 20 20 20 34 35 2c 20 20 20 32 36 2c 0a 20 2f 2a     45,   26,. /*
2d256 20 20 20 20 38 30 20 2a 2f 20 20 20 20 32 36 2c      80 */    26,
2d257 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2d258 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20  7,   27,   28,  
2d259 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c   28,   28,   29,
2d25a 20 20 32 31 36 2c 0a 20 2f 2a 20 20 20 20 39 30    216,. /*    90
2d25b 20 2a 2f 20 20 20 34 31 33 2c 20 20 34 31 34 2c   */   413,  414,
2d25c 20 20 34 31 36 2c 20 20 34 31 37 2c 20 20 31 35    416,  417,  15
2d25d 36 2c 20 20 34 31 36 2c 20 20 34 31 37 2c 20 20  6,  416,  417,  
2d25e 33 36 30 2c 20 20 33 36 33 2c 20 20 33 36 34 2c  360,  363,  364,
2d25f 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20  . /*   100 */   
2d260 33 31 31 2c 20 20 34 34 36 2c 20 20 34 35 32 2c  311,  446,  452,
2d261 20 20 33 38 35 2c 20 20 35 31 34 2c 20 20 20 32    385,  514,   2
2d262 31 2c 20 20 31 38 36 2c 20 20 34 39 35 2c 20 20  1,  186,  495,  
2d263 33 36 35 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20  365,   27,. /*  
2d264 20 31 31 30 20 2a 2f 20 20 20 20 32 37 2c 20 20   110 */    27,  
2d265 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   28,   28,   28,
2d266 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20 34 31     29,  216,  41
2d267 33 2c 20 20 34 31 34 2c 20 20 34 31 35 2c 20 20  3,  414,  415,  
2d268 20 32 33 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a   23,. /*   120 *
2d269 2f 20 20 20 20 32 32 2c 20 20 33 30 38 2c 20 20  /    22,  308,  
2d26a 34 35 36 2c 20 20 34 35 37 2c 20 20 34 35 33 2c  456,  457,  453,
2d26b 20 20 34 35 33 2c 20 20 20 32 35 2c 20 20 20 32    453,   25,   2
2d26c 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20  5,   24,   24,. 
2d26d 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 20 32  /*   130 */    2
2d26e 34 2c 20 20 20 32 34 2c 20 20 35 35 35 2c 20 20  4,   24,  555,  
2d26f 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2d270 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
2d271 37 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 31  7,   28,. /*   1
2d272 34 30 20 2a 2f 20 20 20 20 32 38 2c 20 20 20 32  40 */    28,   2
2d273 38 2c 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20  8,   29,  216,  
2d274 33 30 34 2c 20 20 32 32 38 2c 20 20 35 30 34 2c  304,  228,  504,
2d275 20 20 31 33 35 2c 20 20 34 36 38 2c 20 20 32 31    135,  468,  21
2d276 38 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20  8,. /*   150 */ 
2d277 20 20 35 34 38 2c 20 20 31 34 35 2c 20 20 31 33    548,  145,  13
2d278 32 2c 20 20 32 35 36 2c 20 20 33 35 38 2c 20 20  2,  256,  358,  
2d279 32 36 31 2c 20 20 33 35 39 2c 20 20 31 35 33 2c  261,  359,  153,
2d27a 20 20 34 31 36 2c 20 20 34 31 37 2c 0a 20 2f 2a    416,  417,. /*
2d27b 20 20 20 31 36 30 20 2a 2f 20 20 20 32 34 31 2c     160 */   241,
2d27c 20 20 35 39 38 2c 20 20 33 33 31 2c 20 20 20 33    598,  331,   3
2d27d 30 2c 20 20 32 36 35 2c 20 20 20 33 32 2c 20 20  0,  265,   32,  
2d27e 31 33 34 2c 20 20 34 33 39 2c 20 20 35 39 36 2c  134,  439,  596,
2d27f 20 20 35 39 37 2c 0a 20 2f 2a 20 20 20 31 37 30    597,. /*   170
2d280 20 2a 2f 20 20 20 32 33 30 2c 20 20 32 32 38 2c   */   230,  228,
2d281 20 20 34 39 30 2c 20 20 34 34 36 2c 20 20 34 35    490,  446,  45
2d282 32 2c 20 20 20 35 37 2c 20 20 35 30 36 2c 20 20  2,   57,  506,  
2d283 33 32 38 2c 20 20 31 33 32 2c 20 20 32 35 36 2c  328,  132,  256,
2d284 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20  . /*   180 */   
2d285 33 35 38 2c 20 20 32 36 31 2c 20 20 33 35 39 2c  358,  261,  359,
2d286 20 20 31 35 33 2c 20 20 34 31 36 2c 20 20 34 31    153,  416,  41
2d287 37 2c 20 20 34 33 35 2c 20 20 20 37 38 2c 20 20  7,  435,   78,  
2d288 34 30 38 2c 20 20 34 30 35 2c 0a 20 2f 2a 20 20  408,  405,. /*  
2d289 20 31 39 30 20 2a 2f 20 20 20 32 36 35 2c 20 20   190 */   265,  
2d28a 20 32 33 2c 20 20 20 32 32 2c 20 20 33 30 38 2c   23,   22,  308,
2d28b 20 20 34 35 36 2c 20 20 34 35 37 2c 20 20 34 35    456,  457,  45
2d28c 33 2c 20 20 34 35 33 2c 20 20 20 32 35 2c 20 20  3,  453,   25,  
2d28d 20 32 35 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a   25,. /*   200 *
2d28e 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  /    24,   24,  
2d28f 20 32 34 2c 20 20 20 32 34 2c 20 20 33 34 32 2c   24,   24,  342,
2d290 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2d291 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 0a 20  6,   26,   27,. 
2d292 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 20 32  /*   210 */    2
2d293 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  7,   28,   28,  
2d294 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36 2c   28,   29,  216,
2d295 20 20 33 30 34 2c 20 20 32 31 34 2c 20 20 35 33    304,  214,  53
2d296 34 2c 20 20 35 34 37 2c 0a 20 2f 2a 20 20 20 32  4,  547,. /*   2
2d297 32 30 20 2a 2f 20 20 20 33 30 37 2c 20 20 31 32  20 */   307,  12
2d298 37 2c 20 20 34 38 39 2c 20 20 35 39 35 2c 20 20  7,  489,  595,  
2d299 20 33 30 2c 20 20 33 33 31 2c 20 20 20 33 32 2c   30,  331,   32,
2d29a 20 20 31 33 34 2c 20 20 33 34 35 2c 20 20 33 38    134,  345,  38
2d29b 37 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20  7,. /*   230 */ 
2d29c 20 20 34 32 39 2c 20 20 20 36 33 2c 20 20 33 33    429,   63,  33
2d29d 31 2c 20 20 33 35 35 2c 20 20 34 31 35 2c 20 20  1,  355,  415,  
2d29e 34 33 39 2c 20 20 35 30 37 2c 20 20 33 33 31 2c  439,  507,  331,
2d29f 20 20 34 31 35 2c 20 20 35 33 35 2c 0a 20 2f 2a    415,  535,. /*
2d2a0 20 20 20 32 34 30 20 2a 2f 20 20 20 33 32 38 2c     240 */   328,
2d2a1 20 20 32 31 35 2c 20 20 31 39 33 2c 20 20 35 39    215,  193,  59
2d2a2 34 2c 20 20 35 39 33 2c 20 20 34 34 36 2c 20 20  4,  593,  446,  
2d2a3 34 35 32 2c 20 20 33 32 38 2c 20 20 20 31 38 2c  452,  328,   18,
2d2a4 20 20 34 33 35 2c 0a 20 2f 2a 20 20 20 32 35 30    435,. /*   250
2d2a5 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 31 36 2c   */    85,   16,
2d2a6 20 20 33 32 38 2c 20 20 31 38 33 2c 20 20 31 39    328,  183,  19
2d2a7 30 2c 20 20 35 35 36 2c 20 20 34 33 35 2c 20 20  0,  556,  435,  
2d2a8 20 37 38 2c 20 20 33 30 39 2c 20 20 34 36 33 2c   78,  309,  463,
2d2a9 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20  . /*   260 */   
2d2aa 34 36 34 2c 20 20 34 33 35 2c 20 20 20 38 35 2c  464,  435,   85,
2d2ab 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 33 30     23,   22,  30
2d2ac 38 2c 20 20 34 35 36 2c 20 20 34 35 37 2c 20 20  8,  456,  457,  
2d2ad 34 35 33 2c 20 20 34 35 33 2c 0a 20 2f 2a 20 20  453,  453,. /*  
2d2ae 20 32 37 30 20 2a 2f 20 20 20 20 32 35 2c 20 20   270 */    25,  
2d2af 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   25,   24,   24,
2d2b0 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 34 33     24,   24,  43
2d2b1 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2d2b2 20 32 36 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a   26,. /*   280 *
2d2b3 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  /    26,   27,  
2d2b4 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
2d2b5 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31     28,   29,  21
2d2b6 36 2c 20 20 33 30 34 2c 20 20 33 34 37 2c 0a 20  6,  304,  347,. 
2d2b7 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 32 32  /*   290 */   22
2d2b8 31 2c 20 20 33 31 33 2c 20 20 35 39 35 2c 20 20  1,  313,  595,  
2d2b9 31 39 31 2c 20 20 33 37 38 2c 20 20 33 33 31 2c  191,  378,  331,
2d2ba 20 20 34 37 32 2c 20 20 32 33 34 2c 20 20 33 34    472,  234,  34
2d2bb 35 2c 20 20 33 38 31 2c 0a 20 2f 2a 20 20 20 33  5,  381,. /*   3
2d2bc 30 30 20 2a 2f 20 20 20 33 32 34 2c 20 20 34 31  00 */   324,  41
2d2bd 30 2c 20 20 32 32 30 2c 20 20 33 34 34 2c 20 20  0,  220,  344,  
2d2be 35 39 32 2c 20 20 32 31 37 2c 20 20 32 31 33 2c  592,  217,  213,
2d2bf 20 20 34 31 35 2c 20 20 31 31 32 2c 20 20 33 33    415,  112,  33
2d2c0 31 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20  1,. /*   310 */ 
2d2c1 20 20 33 32 38 2c 20 20 20 20 34 2c 20 20 35 39    328,    4,  59
2d2c2 34 2c 20 20 33 39 39 2c 20 20 32 31 31 2c 20 20  4,  399,  211,  
2d2c3 35 35 34 2c 20 20 35 32 39 2c 20 20 34 34 36 2c  554,  529,  446,
2d2c4 20 20 34 35 32 2c 20 20 34 33 35 2c 0a 20 2f 2a    452,  435,. /*
2d2c5 20 20 20 33 32 30 20 2a 2f 20 20 20 20 37 39 2c     320 */    79,
2d2c6 20 20 32 31 37 2c 20 20 35 35 33 2c 20 20 35 31    217,  553,  51
2d2c7 35 2c 20 20 33 32 38 2c 20 20 33 33 34 2c 20 20  5,  328,  334,  
2d2c8 35 31 33 2c 20 20 34 35 39 2c 20 20 34 35 39 2c  513,  459,  459,
2d2c9 20 20 34 36 39 2c 0a 20 2f 2a 20 20 20 33 33 30    469,. /*   330
2d2ca 20 2a 2f 20 20 20 34 34 31 2c 20 20 35 37 32 2c   */   441,  572,
2d2cb 20 20 34 33 32 2c 20 20 34 33 35 2c 20 20 20 37    432,  435,   7
2d2cc 38 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20  8,   23,   22,  
2d2cd 33 30 38 2c 20 20 34 35 36 2c 20 20 34 35 37 2c  308,  456,  457,
2d2ce 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20  . /*   340 */   
2d2cf 34 35 33 2c 20 20 34 35 33 2c 20 20 20 32 35 2c  453,  453,   25,
2d2d0 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32     25,   24,   2
2d2d1 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
2d2d2 34 33 36 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20  436,   26,. /*  
2d2d3 20 33 35 30 20 2a 2f 20 20 20 20 32 36 2c 20 20   350 */    26,  
2d2d4 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
2d2d5 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
2d2d6 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  8,   28,   29,  
2d2d7 32 31 36 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a  216,. /*   360 *
2d2d8 2f 20 20 20 33 30 34 2c 20 20 34 34 33 2c 20 20  /   304,  443,  
2d2d9 34 34 33 2c 20 20 34 34 33 2c 20 20 31 35 36 2c  443,  443,  156,
2d2da 20 20 34 36 38 2c 20 20 32 31 38 2c 20 20 33 36    468,  218,  36
2d2db 30 2c 20 20 33 36 33 2c 20 20 33 36 34 2c 0a 20  0,  363,  364,. 
2d2dc 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 33 33  /*   370 */   33
2d2dd 31 2c 20 20 32 34 37 2c 20 20 33 39 35 2c 20 20  1,  247,  395,  
2d2de 33 39 38 2c 20 20 32 31 37 2c 20 20 33 34 39 2c  398,  217,  349,
2d2df 20 20 33 33 31 2c 20 20 20 33 30 2c 20 20 33 36    331,   30,  36
2d2e0 35 2c 20 20 20 33 32 2c 0a 20 2f 2a 20 20 20 33  5,   32,. /*   3
2d2e1 38 30 20 2a 2f 20 20 20 31 33 34 2c 20 20 33 38  80 */   134,  38
2d2e2 38 2c 20 20 32 38 32 2c 20 20 32 38 31 2c 20 20  8,  282,  281,  
2d2e3 20 33 39 2c 20 20 33 32 38 2c 20 20 20 34 31 2c   39,  328,   41,
2d2e4 20 20 34 33 30 2c 20 20 35 34 35 2c 20 20 34 34    430,  545,  44
2d2e5 36 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20  6,. /*   390 */ 
2d2e6 20 20 34 35 32 2c 20 20 33 32 38 2c 20 20 32 31    452,  328,  21
2d2e7 34 2c 20 20 35 33 31 2c 20 20 34 33 35 2c 20 20  4,  531,  435,  
2d2e8 20 39 33 2c 20 20 35 34 32 2c 20 20 36 30 31 2c   93,  542,  601,
2d2e9 20 20 20 20 31 2c 20 20 34 30 34 2c 0a 20 2f 2a      1,  404,. /*
2d2ea 20 20 20 34 30 30 20 2a 2f 20 20 20 34 33 35 2c     400 */   435,
2d2eb 20 20 20 39 33 2c 20 20 34 31 33 2c 20 20 34 31     93,  413,  41
2d2ec 34 2c 20 20 34 39 35 2c 20 20 20 34 30 2c 20 20  4,  495,   40,  
2d2ed 35 33 36 2c 20 20 20 32 33 2c 20 20 20 32 32 2c  536,   23,   22,
2d2ee 20 20 33 30 38 2c 0a 20 2f 2a 20 20 20 34 31 30    308,. /*   410
2d2ef 20 2a 2f 20 20 20 34 35 36 2c 20 20 34 35 37 2c   */   456,  457,
2d2f0 20 20 34 35 33 2c 20 20 34 35 33 2c 20 20 20 32    453,  453,   2
2d2f1 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20  5,   25,   24,  
2d2f2 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
2d2f3 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20  . /*   420 */   
2d2f4 35 37 33 2c 20 20 20 32 36 2c 20 20 20 32 36 2c  573,   26,   26,
2d2f5 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2d2f6 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20  7,   27,   28,  
2d2f7 20 32 38 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20   28,   28,. /*  
2d2f8 20 34 33 30 20 2a 2f 20 20 20 20 32 39 2c 20 20   430 */    29,  
2d2f9 32 31 36 2c 20 20 33 30 34 2c 20 20 32 37 36 2c  216,  304,  276,
2d2fa 20 20 33 33 31 2c 20 20 31 37 39 2c 20 20 35 30    331,  179,  50
2d2fb 38 2c 20 20 34 39 30 2c 20 20 32 31 30 2c 20 20  8,  490,  210,  
2d2fc 35 34 37 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a  547,. /*   440 *
2d2fd 2f 20 20 20 33 31 39 2c 20 20 34 31 33 2c 20 20  /   319,  413,  
2d2fe 34 31 34 2c 20 20 32 32 32 2c 20 20 31 39 32 2c  414,  222,  192,
2d2ff 20 20 33 38 35 2c 20 20 33 32 30 2c 20 20 32 34    385,  320,  24
2d300 30 2c 20 20 34 31 35 2c 20 20 33 32 38 2c 0a 20  0,  415,  328,. 
2d301 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 35 35  /*   450 */   55
2d302 37 2c 20 20 20 36 33 2c 20 20 34 31 33 2c 20 20  7,   63,  413,  
2d303 34 31 34 2c 20 20 34 31 35 2c 20 20 36 31 36 2c  414,  415,  616,
2d304 20 20 34 30 38 2c 20 20 34 30 35 2c 20 20 34 33    408,  405,  43
2d305 35 2c 20 20 20 37 31 2c 0a 20 2f 2a 20 20 20 34  5,   71,. /*   4
2d306 36 30 20 2a 2f 20 20 20 34 31 35 2c 20 20 34 34  60 */   415,  44
2d307 36 2c 20 20 34 35 32 2c 20 20 36 31 31 2c 20 20  6,  452,  611,  
2d308 35 37 32 2c 20 20 20 32 38 2c 20 20 20 32 38 2c  572,   28,   28,
2d309 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31     28,   29,  21
2d30a 36 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20  6,. /*   470 */ 
2d30b 20 20 34 31 36 2c 20 20 34 31 37 2c 20 20 34 33    416,  417,  43
2d30c 36 2c 20 20 33 33 36 2c 20 20 34 36 33 2c 20 20  6,  336,  463,  
2d30d 34 36 34 2c 20 20 34 30 31 2c 20 20 20 34 33 2c  464,  401,   43,
2d30e 20 20 34 33 36 2c 20 20 20 32 33 2c 0a 20 2f 2a    436,   23,. /*
2d30f 20 20 20 34 38 30 20 2a 2f 20 20 20 20 32 32 2c     480 */    22,
2d310 20 20 33 30 38 2c 20 20 34 35 36 2c 20 20 34 35    308,  456,  45
2d311 37 2c 20 20 34 35 33 2c 20 20 34 35 33 2c 20 20  7,  453,  453,  
2d312 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c   25,   25,   24,
2d313 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 34 39 30     24,. /*   490
2d314 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c   */    24,   24,
2d315 20 20 34 39 35 2c 20 20 20 32 36 2c 20 20 20 32    495,   26,   2
2d316 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2d317 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
2d318 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20  . /*   500 */   
2d319 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c   28,   28,   29,
2d31a 20 20 32 31 36 2c 20 20 33 30 34 2c 20 20 36 31    216,  304,  61
2d31b 32 2c 20 20 32 30 39 2c 20 20 31 33 35 2c 20 20  2,  209,  135,  
2d31c 35 31 31 2c 20 20 34 31 36 2c 0a 20 2f 2a 20 20  511,  416,. /*  
2d31d 20 35 31 30 20 2a 2f 20 20 20 34 31 37 2c 20 20   510 */   417,  
2d31e 34 33 31 2c 20 20 32 33 33 2c 20 20 20 36 34 2c  431,  233,   64,
2d31f 20 20 33 38 38 2c 20 20 32 38 32 2c 20 20 32 38    388,  282,  28
2d320 31 2c 20 20 34 33 39 2c 20 20 20 36 36 2c 20 20  1,  439,   66,  
2d321 35 34 32 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a  542,. /*   520 *
2d322 2f 20 20 20 34 31 36 2c 20 20 34 31 37 2c 20 20  /   416,  417,  
2d323 34 31 33 2c 20 20 34 31 34 2c 20 20 31 35 36 2c  413,  414,  156,
2d324 20 20 32 31 34 2c 20 20 34 30 33 2c 20 20 33 36    214,  403,  36
2d325 30 2c 20 20 33 36 33 2c 20 20 33 36 34 2c 0a 20  0,  363,  364,. 
2d326 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 35 34  /*   530 */   54
2d327 37 2c 20 20 32 35 32 2c 20 20 34 39 30 2c 20 20  7,  252,  490,  
2d328 34 34 36 2c 20 20 34 35 32 2c 20 20 34 39 31 2c  446,  452,  491,
2d329 20 20 32 31 37 2c 20 20 20 20 38 2c 20 20 33 36    217,    8,  36
2d32a 35 2c 20 20 34 39 35 2c 0a 20 2f 2a 20 20 20 35  5,  495,. /*   5
2d32b 34 30 20 2a 2f 20 20 20 34 33 36 2c 20 20 36 30  40 */   436,  60
2d32c 36 2c 20 20 20 36 33 2c 20 20 35 33 37 2c 20 20  6,   63,  537,  
2d32d 32 39 39 2c 20 20 34 31 35 2c 20 20 34 39 32 2c  299,  415,  492,
2d32e 20 20 34 37 30 2c 20 20 35 34 36 2c 20 20 32 30    470,  546,  20
2d32f 30 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20  0,. /*   550 */ 
2d330 20 20 31 39 36 2c 20 20 20 32 33 2c 20 20 20 32    196,   23,   2
2d331 32 2c 20 20 33 30 38 2c 20 20 34 35 36 2c 20 20  2,  308,  456,  
2d332 34 35 37 2c 20 20 34 35 33 2c 20 20 34 35 33 2c  457,  453,  453,
2d333 20 20 20 32 35 2c 20 20 20 32 35 2c 0a 20 2f 2a     25,   25,. /*
2d334 20 20 20 35 36 30 20 2a 2f 20 20 20 20 32 34 2c     560 */    24,
2d335 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
2d336 34 2c 20 20 33 38 36 2c 20 20 20 32 36 2c 20 20  4,  386,   26,  
2d337 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2d338 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 35 37 30     27,. /*   570
2d339 20 2a 2f 20 20 20 20 32 37 2c 20 20 20 32 38 2c   */    27,   28,
2d33a 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2d33b 39 2c 20 20 32 31 36 2c 20 20 33 30 34 2c 20 20  9,  216,  304,  
2d33c 34 37 37 2c 20 20 32 35 34 2c 20 20 33 35 34 2c  477,  254,  354,
2d33d 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20  . /*   580 */   
2d33e 35 32 38 2c 20 20 20 36 30 2c 20 20 35 31 37 2c  528,   60,  517,
2d33f 20 20 35 31 38 2c 20 20 34 33 36 2c 20 20 34 33    518,  436,  43
2d340 39 2c 20 20 33 38 39 2c 20 20 33 33 31 2c 20 20  9,  389,  331,  
2d341 33 35 36 2c 20 20 20 20 37 2c 0a 20 2f 2a 20 20  356,    7,. /*  
2d342 20 35 39 30 20 2a 2f 20 20 20 34 31 36 2c 20 20   590 */   416,  
2d343 34 31 37 2c 20 20 33 33 31 2c 20 20 34 37 38 2c  417,  331,  478,
2d344 20 20 33 32 38 2c 20 20 32 30 38 2c 20 20 31 39    328,  208,  19
2d345 37 2c 20 20 31 33 37 2c 20 20 34 36 30 2c 20 20  7,  137,  460,  
2d346 34 39 39 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a  499,. /*   600 *
2d347 2f 20 20 20 34 34 37 2c 20 20 34 34 38 2c 20 20  /   447,  448,  
2d348 33 32 38 2c 20 20 34 33 35 2c 20 20 20 20 39 2c  328,  435,    9,
2d349 20 20 34 34 36 2c 20 20 34 35 32 2c 20 20 33 32    446,  452,  32
2d34a 38 2c 20 20 34 37 39 2c 20 20 34 38 35 2c 0a 20  8,  479,  485,. 
2d34b 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 35 31  /*   610 */   51
2d34c 39 2c 20 20 34 33 35 2c 20 20 20 37 32 2c 20 20  9,  435,   72,  
2d34d 35 36 37 2c 20 20 34 31 35 2c 20 20 34 33 34 2c  567,  415,  434,
2d34e 20 20 34 33 35 2c 20 20 20 36 37 2c 20 20 34 38    435,   67,  48
2d34f 36 2c 20 20 34 33 33 2c 0a 20 2f 2a 20 20 20 36  6,  433,. /*   6
2d350 32 30 20 2a 2f 20 20 20 35 32 30 2c 20 20 34 35  20 */   520,  45
2d351 30 2c 20 20 34 35 31 2c 20 20 20 32 33 2c 20 20  0,  451,   23,  
2d352 20 32 32 2c 20 20 33 30 38 2c 20 20 34 35 36 2c   22,  308,  456,
2d353 20 20 34 35 37 2c 20 20 34 35 33 2c 20 20 34 35    457,  453,  45
2d354 33 2c 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f 20  3,. /*   630 */ 
2d355 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32     25,   25,   2
2d356 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
2d357 20 32 34 2c 20 20 33 33 31 2c 20 20 20 32 36 2c   24,  331,   26,
2d358 20 20 20 32 36 2c 20 20 20 32 36 2c 0a 20 2f 2a     26,   26,. /*
2d359 20 20 20 36 34 30 20 2a 2f 20 20 20 20 32 36 2c     640 */    26,
2d35a 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32     27,   27,   2
2d35b 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  8,   28,   28,  
2d35c 20 32 39 2c 20 20 32 31 36 2c 20 20 33 30 34 2c   29,  216,  304,
2d35d 20 20 33 33 31 2c 0a 20 2f 2a 20 20 20 36 35 30    331,. /*   650
2d35e 20 2a 2f 20 20 20 34 34 39 2c 20 20 33 32 38 2c   */   449,  328,
2d35f 20 20 32 36 38 2c 20 20 33 39 30 2c 20 20 34 36    268,  390,  46
2d360 31 2c 20 20 33 33 31 2c 20 20 20 36 35 2c 20 20  1,  331,   65,  
2d361 33 33 31 2c 20 20 33 36 38 2c 20 20 34 33 34 2c  331,  368,  434,
2d362 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20 20  . /*   660 */   
2d363 34 33 35 2c 20 20 20 37 36 2c 20 20 33 31 30 2c  435,   76,  310,
2d364 20 20 34 33 33 2c 20 20 33 32 38 2c 20 20 31 35    433,  328,  15
2d365 30 2c 20 20 34 32 37 2c 20 20 34 33 39 2c 20 20  0,  427,  439,  
2d366 34 37 33 2c 20 20 33 33 31 2c 0a 20 2f 2a 20 20  473,  331,. /*  
2d367 20 36 37 30 20 2a 2f 20 20 20 33 32 38 2c 20 20   670 */   328,  
2d368 34 39 39 2c 20 20 33 32 38 2c 20 20 34 33 35 2c  499,  328,  435,
2d369 20 20 20 39 37 2c 20 20 20 32 39 2c 20 20 32 31     97,   29,  21
2d36a 36 2c 20 20 34 34 36 2c 20 20 34 35 32 2c 20 20  6,  446,  452,  
2d36b 34 33 35 2c 0a 20 2f 2a 20 20 20 36 38 30 20 2a  435,. /*   680 *
2d36c 2f 20 20 20 20 39 36 2c 20 20 34 33 35 2c 20 20  /    96,  435,  
2d36d 31 30 31 2c 20 20 33 35 33 2c 20 20 33 32 38 2c  101,  353,  328,
2d36e 20 20 33 37 32 2c 20 20 34 31 35 2c 20 20 33 33    372,  415,  33
2d36f 34 2c 20 20 31 35 34 2c 20 20 34 35 39 2c 0a 20  4,  154,  459,. 
2d370 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20 20 34 35  /*   690 */   45
2d371 39 2c 20 20 33 35 32 2c 20 20 35 36 39 2c 20 20  9,  352,  569,  
2d372 34 33 35 2c 20 20 20 39 39 2c 20 20 20 32 33 2c  435,   99,   23,
2d373 20 20 20 32 32 2c 20 20 33 30 38 2c 20 20 34 35     22,  308,  45
2d374 36 2c 20 20 34 35 37 2c 0a 20 2f 2a 20 20 20 37  6,  457,. /*   7
2d375 30 30 20 2a 2f 20 20 20 34 35 33 2c 20 20 34 35  00 */   453,  45
2d376 33 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20  3,   25,   25,  
2d377 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
2d378 20 20 20 32 34 2c 20 20 33 33 31 2c 20 20 20 32     24,  331,   2
2d379 36 2c 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20  6,. /*   710 */ 
2d37a 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2d37b 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20  6,   27,   27,  
2d37c 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   28,   28,   28,
2d37d 20 20 20 32 39 2c 20 20 32 31 36 2c 0a 20 2f 2a     29,  216,. /*
2d37e 20 20 20 37 32 30 20 2a 2f 20 20 20 33 30 34 2c     720 */   304,
2d37f 20 20 33 33 31 2c 20 20 32 34 38 2c 20 20 33 32    331,  248,  32
2d380 38 2c 20 20 32 36 34 2c 20 20 20 35 36 2c 20 20  8,  264,   56,  
2d381 33 33 34 2c 20 20 33 33 31 2c 20 20 34 35 39 2c  334,  331,  459,
2d382 20 20 34 35 39 2c 0a 20 2f 2a 20 20 20 37 33 30    459,. /*   730
2d383 20 2a 2f 20 20 20 38 36 31 2c 20 20 33 33 33 2c   */   861,  333,
2d384 20 20 34 33 35 2c 20 20 31 30 34 2c 20 20 33 37    435,  104,  37
2d385 36 2c 20 20 34 33 39 2c 20 20 33 32 38 2c 20 20  6,  439,  328,  
2d386 34 31 35 2c 20 20 33 33 31 2c 20 20 34 31 35 2c  415,  331,  415,
2d387 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20 20  . /*   740 */   
2d388 35 36 35 2c 20 20 33 33 31 2c 20 20 33 32 38 2c  565,  331,  328,
2d389 20 20 33 30 36 2c 20 20 35 36 34 2c 20 20 34 33    306,  564,  43
2d38a 35 2c 20 20 31 30 35 2c 20 20 31 38 35 2c 20 20  5,  105,  185,  
2d38b 32 36 35 2c 20 20 34 34 36 2c 0a 20 2f 2a 20 20  265,  446,. /*  
2d38c 20 37 35 30 20 2a 2f 20 20 20 34 35 32 2c 20 20   750 */   452,  
2d38d 34 33 35 2c 20 20 31 32 36 2c 20 20 33 32 38 2c  435,  126,  328,
2d38e 20 20 35 37 30 2c 20 20 35 31 38 2c 20 20 33 32    570,  518,  32
2d38f 38 2c 20 20 33 33 34 2c 20 20 33 37 37 2c 20 20  8,  334,  377,  
2d390 34 35 39 2c 0a 20 2f 2a 20 20 20 37 36 30 20 2a  459,. /*   760 *
2d391 2f 20 20 20 34 35 39 2c 20 20 33 31 34 2c 20 20  /   459,  314,  
2d392 34 33 35 2c 20 20 31 32 38 2c 20 20 31 39 34 2c  435,  128,  194,
2d393 20 20 34 33 35 2c 20 20 20 35 39 2c 20 20 20 32    435,   59,   2
2d394 33 2c 20 20 20 32 32 2c 20 20 33 30 38 2c 0a 20  3,   22,  308,. 
2d395 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20 20 34 35  /*   770 */   45
2d396 36 2c 20 20 34 35 37 2c 20 20 34 35 33 2c 20 20  6,  457,  453,  
2d397 34 35 33 2c 20 20 20 32 35 2c 20 20 20 32 35 2c  453,   25,   25,
2d398 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
2d399 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 37  4,   24,. /*   7
2d39a 38 30 20 2a 2f 20 20 20 33 33 31 2c 20 20 20 32  80 */   331,   2
2d39b 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2d39c 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c   26,   27,   27,
2d39d 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2d39e 38 2c 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f 20  8,. /*   790 */ 
2d39f 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20 33 30     29,  216,  30
2d3a0 34 2c 20 20 33 33 31 2c 20 20 31 33 36 2c 20 20  4,  331,  136,  
2d3a1 33 32 38 2c 20 20 32 34 32 2c 20 20 34 37 37 2c  328,  242,  477,
2d3a2 20 20 34 33 36 2c 20 20 33 33 31 2c 0a 20 2f 2a    436,  331,. /*
2d3a3 20 20 20 38 30 30 20 2a 2f 20 20 20 33 35 30 2c     800 */   350,
2d3a4 20 20 33 33 31 2c 20 20 36 30 39 2c 20 20 33 30    331,  609,  30
2d3a5 33 2c 20 20 34 33 35 2c 20 20 31 30 32 2c 20 20  3,  435,  102,  
2d3a6 32 30 31 2c 20 20 31 33 37 2c 20 20 33 32 38 2c  201,  137,  328,
2d3a7 20 20 34 31 35 2c 0a 20 2f 2a 20 20 20 38 31 30    415,. /*   810
2d3a8 20 2a 2f 20 20 20 34 35 34 2c 20 20 31 37 38 2c   */   454,  178,
2d3a9 20 20 33 33 31 2c 20 20 34 37 38 2c 20 20 33 32    331,  478,  32
2d3aa 38 2c 20 20 34 31 35 2c 20 20 33 32 38 2c 20 20  8,  415,  328,  
2d3ab 34 33 35 2c 20 20 20 37 37 2c 20 20 34 34 30 2c  435,   77,  440,
2d3ac 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20 20  . /*   820 */   
2d3ad 32 34 39 2c 20 20 34 34 36 2c 20 20 34 35 32 2c  249,  446,  452,
2d3ae 20 20 34 33 35 2c 20 20 31 30 30 2c 20 20 34 33    435,  100,  43
2d3af 35 2c 20 20 20 36 38 2c 20 20 33 32 38 2c 20 20  5,   68,  328,  
2d3b0 34 37 39 2c 20 20 34 36 35 2c 0a 20 2f 2a 20 20  479,  465,. /*  
2d3b1 20 38 33 30 20 2a 2f 20 20 20 33 34 31 2c 20 20   830 */   341,  
2d3b2 36 31 33 2c 20 20 39 33 31 2c 20 20 34 38 34 2c  613,  931,  484,
2d3b3 20 20 39 33 31 2c 20 20 34 31 35 2c 20 20 34 33    931,  415,  43
2d3b4 35 2c 20 20 20 39 38 2c 20 20 34 36 37 2c 20 20  5,   98,  467,  
2d3b5 20 32 33 2c 0a 20 2f 2a 20 20 20 38 34 30 20 2a   23,. /*   840 *
2d3b6 2f 20 20 20 20 32 32 2c 20 20 33 30 38 2c 20 20  /    22,  308,  
2d3b7 34 35 36 2c 20 20 34 35 37 2c 20 20 34 35 33 2c  456,  457,  453,
2d3b8 20 20 34 35 33 2c 20 20 20 32 35 2c 20 20 20 32    453,   25,   2
2d3b9 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20  5,   24,   24,. 
2d3ba 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20 20 20 32  /*   850 */    2
2d3bb 34 2c 20 20 20 32 34 2c 20 20 33 33 31 2c 20 20  4,   24,  331,  
2d3bc 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2d3bd 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
2d3be 37 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 38  7,   28,. /*   8
2d3bf 36 30 20 2a 2f 20 20 20 20 32 38 2c 20 20 20 32  60 */    28,   2
2d3c0 38 2c 20 20 20 32 39 2c 20 20 32 31 36 2c 20 20  8,   29,  216,  
2d3c1 33 30 34 2c 20 20 33 33 31 2c 20 20 33 39 37 2c  304,  331,  397,
2d3c2 20 20 33 32 38 2c 20 20 31 36 34 2c 20 20 32 36    328,  164,  26
2d3c3 34 2c 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20  4,. /*   870 */ 
2d3c4 20 20 32 30 35 2c 20 20 33 33 31 2c 20 20 32 36    205,  331,  26
2d3c5 34 2c 20 20 33 33 32 2c 20 20 36 31 30 2c 20 20  4,  332,  610,  
2d3c6 33 33 39 2c 20 20 34 33 35 2c 20 20 31 32 39 2c  339,  435,  129,
2d3c7 20 20 34 30 37 2c 20 20 20 20 32 2c 0a 20 2f 2a    407,    2,. /*
2d3c8 20 20 20 38 38 30 20 2a 2f 20 20 20 33 32 38 2c     880 */   328,
2d3c9 20 20 33 32 32 2c 20 20 31 37 35 2c 20 20 33 33    322,  175,  33
2d3ca 31 2c 20 20 34 31 35 2c 20 20 32 31 34 2c 20 20  1,  415,  214,  
2d3cb 33 32 38 2c 20 20 34 31 35 2c 20 20 34 31 35 2c  328,  415,  415,
2d3cc 20 20 34 33 35 2c 0a 20 2f 2a 20 20 20 38 39 30    435,. /*   890
2d3cd 20 2a 2f 20 20 20 31 33 30 2c 20 20 34 36 36 2c   */   130,  466,
2d3ce 20 20 34 36 36 2c 20 20 34 34 36 2c 20 20 34 35    466,  446,  45
2d3cf 32 2c 20 20 34 33 35 2c 20 20 31 33 31 2c 20 20  2,  435,  131,  
2d3d0 33 39 36 2c 20 20 33 32 38 2c 20 20 32 35 37 2c  396,  328,  257,
2d3d1 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20 20  . /*   900 */   
2d3d2 33 33 34 2c 20 20 34 38 37 2c 20 20 34 35 39 2c  334,  487,  459,
2d3d3 20 20 34 35 39 2c 20 20 34 33 36 2c 20 20 31 35    459,  436,  15
2d3d4 34 2c 20 20 32 32 39 2c 20 20 34 33 35 2c 20 20  4,  229,  435,  
2d3d5 20 36 39 2c 20 20 33 31 35 2c 0a 20 2f 2a 20 20   69,  315,. /*  
2d3d6 20 39 31 30 20 2a 2f 20 20 20 32 35 38 2c 20 20   910 */   258,  
2d3d7 20 32 33 2c 20 20 20 33 33 2c 20 20 33 30 38 2c   23,   33,  308,
2d3d8 20 20 34 35 36 2c 20 20 34 35 37 2c 20 20 34 35    456,  457,  45
2d3d9 33 2c 20 20 34 35 33 2c 20 20 20 32 35 2c 20 20  3,  453,   25,  
2d3da 20 32 35 2c 0a 20 2f 2a 20 20 20 39 32 30 20 2a   25,. /*   920 *
2d3db 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  /    24,   24,  
2d3dc 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33 31 2c   24,   24,  331,
2d3dd 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2d3de 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 0a 20  6,   26,   27,. 
2d3df 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20 20 20 32  /*   930 */    2
2d3e0 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  7,   28,   28,  
2d3e1 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 36 2c   28,   29,  216,
2d3e2 20 20 33 30 34 2c 20 20 33 33 31 2c 20 20 34 39    304,  331,  49
2d3e3 37 2c 20 20 33 32 38 2c 0a 20 2f 2a 20 20 20 39  7,  328,. /*   9
2d3e4 34 30 20 2a 2f 20 20 20 31 35 31 2c 20 20 32 36  40 */   151,  26
2d3e5 34 2c 20 20 34 31 32 2c 20 20 33 33 31 2c 20 20  4,  412,  331,  
2d3e6 32 36 34 2c 20 20 34 37 30 2c 20 20 33 33 37 2c  264,  470,  337,
2d3e7 20 20 32 30 30 2c 20 20 34 33 35 2c 20 20 20 38    200,  435,   8
2d3e8 30 2c 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f 20  0,. /*   950 */ 
2d3e9 20 20 32 35 30 2c 20 20 31 35 35 2c 20 20 33 32    250,  155,  32
2d3ea 38 2c 20 20 35 32 33 2c 20 20 35 32 34 2c 20 20  8,  523,  524,  
2d3eb 33 33 31 2c 20 20 34 31 35 2c 20 20 34 31 35 2c  331,  415,  415,
2d3ec 20 20 33 32 38 2c 20 20 34 31 35 2c 0a 20 2f 2a    328,  415,. /*
2d3ed 20 20 20 39 36 30 20 2a 2f 20 20 20 33 30 36 2c     960 */   306,
2d3ee 20 20 34 33 35 2c 20 20 20 38 31 2c 20 20 35 33    435,   81,  53
2d3ef 33 2c 20 20 35 33 32 2c 20 20 34 34 36 2c 20 20  3,  532,  446,  
2d3f0 34 35 32 2c 20 20 34 33 35 2c 20 20 20 37 30 2c  452,  435,   70,
2d3f1 20 20 20 34 37 2c 0a 20 2f 2a 20 20 20 39 37 30     47,. /*   970
2d3f2 20 2a 2f 20 20 20 33 32 38 2c 20 20 36 31 33 2c   */   328,  613,
2d3f3 20 20 39 33 30 2c 20 20 32 35 39 2c 20 20 39 33    930,  259,  93
2d3f4 30 2c 20 20 34 31 38 2c 20 20 34 31 39 2c 20 20  0,  418,  419,  
2d3f5 34 32 30 2c 20 20 33 31 36 2c 20 20 34 33 35 2c  420,  316,  435,
2d3f6 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20 20  . /*   980 */   
2d3f7 20 38 32 2c 20 20 33 31 37 2c 20 20 32 30 36 2c   82,  317,  206,
2d3f8 20 20 35 33 39 2c 20 20 20 32 32 2c 20 20 33 30    539,   22,  30
2d3f9 38 2c 20 20 34 35 36 2c 20 20 34 35 37 2c 20 20  8,  456,  457,  
2d3fa 34 35 33 2c 20 20 34 35 33 2c 0a 20 2f 2a 20 20  453,  453,. /*  
2d3fb 20 39 39 30 20 2a 2f 20 20 20 20 32 35 2c 20 20   990 */    25,  
2d3fc 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   25,   24,   24,
2d3fd 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33     24,   24,  33
2d3fe 31 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  1,   26,   26,  
2d3ff 20 32 36 2c 0a 20 2f 2a 20 20 31 30 30 30 20 2a   26,. /*  1000 *
2d400 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  /    26,   27,  
2d401 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
2d402 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31     28,   29,  21
2d403 36 2c 20 20 33 30 34 2c 20 20 33 33 31 2c 0a 20  6,  304,  331,. 
2d404 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20 20 32 30  /*  1010 */   20
2d405 39 2c 20 20 33 32 38 2c 20 20 35 32 39 2c 20 20  9,  328,  529,  
2d406 35 34 30 2c 20 20 36 31 30 2c 20 20 33 33 31 2c  540,  610,  331,
2d407 20 20 34 33 36 2c 20 20 35 36 33 2c 20 20 33 37    436,  563,  37
2d408 35 2c 20 20 35 36 33 2c 0a 20 2f 2a 20 20 31 30  5,  563,. /*  10
2d409 32 30 20 2a 2f 20 20 20 34 33 35 2c 20 20 20 38  20 */   435,   8
2d40a 33 2c 20 20 33 36 32 2c 20 20 35 33 38 2c 20 20  3,  362,  538,  
2d40b 33 32 38 2c 20 20 31 35 35 2c 20 20 35 34 31 2c  328,  155,  541,
2d40c 20 20 33 33 31 2c 20 20 34 39 39 2c 20 20 35 32    331,  499,  52
2d40d 36 2c 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20  6,. /*  1030 */ 
2d40e 20 20 33 32 38 2c 20 20 33 33 31 2c 20 20 35 37    328,  331,  57
2d40f 35 2c 20 20 34 33 35 2c 20 20 20 38 34 2c 20 20  5,  435,   84,  
2d410 34 32 34 2c 20 20 35 34 33 2c 20 20 34 34 36 2c  424,  543,  446,
2d411 20 20 34 35 32 2c 20 20 34 33 35 2c 0a 20 2f 2a    452,  435,. /*
2d412 20 20 31 30 34 30 20 2a 2f 20 20 20 20 38 36 2c    1040 */    86,
2d413 20 20 32 39 30 2c 20 20 33 32 38 2c 20 20 34 31    290,  328,  41
2d414 35 2c 20 20 34 33 36 2c 20 20 32 36 37 2c 20 20  5,  436,  267,  
2d415 33 32 38 2c 20 20 31 35 35 2c 20 20 33 39 34 2c  328,  155,  394,
2d416 20 20 31 34 31 2c 0a 20 2f 2a 20 20 31 30 35 30    141,. /*  1050
2d417 20 2a 2f 20 20 20 34 31 35 2c 20 20 34 33 35 2c   */   415,  435,
2d418 20 20 20 38 37 2c 20 20 35 38 38 2c 20 20 34 31     87,  588,  41
2d419 31 2c 20 20 34 33 35 2c 20 20 20 38 38 2c 20 20  1,  435,   88,  
2d41a 33 30 38 2c 20 20 34 35 36 2c 20 20 34 35 37 2c  308,  456,  457,
2d41b 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20 20  . /*  1060 */   
2d41c 34 35 33 2c 20 20 34 35 33 2c 20 20 20 32 35 2c  453,  453,   25,
2d41d 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32     25,   24,   2
2d41e 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
2d41f 33 38 36 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20  386,   26,. /*  
2d420 31 30 37 30 20 2a 2f 20 20 20 20 32 36 2c 20 20  1070 */    26,  
2d421 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
2d422 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
2d423 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  8,   28,   29,  
2d424 32 31 36 2c 0a 20 2f 2a 20 20 31 30 38 30 20 2a  216,. /*  1080 *
2d425 2f 20 20 20 20 33 35 2c 20 20 33 33 38 2c 20 20  /    35,  338,  
2d426 32 38 36 2c 20 20 20 20 33 2c 20 20 33 33 31 2c  286,    3,  331,
2d427 20 20 32 37 30 2c 20 20 33 33 31 2c 20 20 33 32    270,  331,  32
2d428 37 2c 20 20 34 31 34 2c 20 20 34 32 31 2c 0a 20  7,  414,  421,. 
2d429 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20 20 33 38  /*  1090 */   38
2d42a 32 2c 20 20 33 31 38 2c 20 20 32 37 36 2c 20 20  2,  318,  276,  
2d42b 34 32 32 2c 20 20 33 32 35 2c 20 20 20 33 35 2c  422,  325,   35,
2d42c 20 20 33 33 38 2c 20 20 33 33 35 2c 20 20 20 20    338,  335,    
2d42d 33 2c 20 20 33 32 38 2c 0a 20 2f 2a 20 20 31 31  3,  328,. /*  11
2d42e 30 30 20 2a 2f 20 20 20 34 32 33 2c 20 20 33 32  00 */   423,  32
2d42f 38 2c 20 20 33 32 37 2c 20 20 34 31 34 2c 20 20  8,  327,  414,  
2d430 31 34 32 2c 20 20 31 34 34 2c 20 20 32 37 36 2c  142,  144,  276,
2d431 20 20 34 31 35 2c 20 20 34 33 35 2c 20 20 20 37    415,  435,   7
2d432 33 2c 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f 20  3,. /*  1110 */ 
2d433 20 20 34 33 35 2c 20 20 20 37 34 2c 20 20 33 33    435,   74,  33
2d434 35 2c 20 20 33 33 31 2c 20 20 20 20 36 2c 20 20  5,  331,    6,  
2d435 33 34 30 2c 20 20 34 32 35 2c 20 20 33 33 31 2c  340,  425,  331,
2d436 20 20 33 32 36 2c 20 20 33 33 31 2c 0a 20 2f 2a    326,  331,. /*
2d437 20 20 31 31 32 30 20 2a 2f 20 20 20 33 36 37 2c    1120 */   367,
2d438 20 20 34 31 35 2c 20 20 31 35 35 2c 20 20 34 33    415,  155,  43
2d439 37 2c 20 20 32 38 39 2c 20 20 34 37 32 2c 20 20  7,  289,  472,  
2d43a 32 38 37 2c 20 20 32 37 34 2c 20 20 33 32 38 2c  287,  274,  328,
2d43b 20 20 32 37 32 2c 0a 20 2f 2a 20 20 31 31 33 30    272,. /*  1130
2d43c 20 2a 2f 20 20 20 33 34 30 2c 20 20 34 31 35 2c   */   340,  415,
2d43d 20 20 33 32 38 2c 20 20 20 34 37 2c 20 20 33 32    328,   47,  32
2d43e 38 2c 20 20 32 37 37 2c 20 20 32 37 36 2c 20 20  8,  277,  276,  
2d43f 34 33 35 2c 20 20 20 38 39 2c 20 20 33 34 38 2c  435,   89,  348,
2d440 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20 20  . /*  1140 */   
2d441 34 37 32 2c 20 20 34 33 35 2c 20 20 20 39 30 2c  472,  435,   90,
2d442 20 20 34 33 35 2c 20 20 20 39 31 2c 20 20 20 33    435,   91,   3
2d443 38 2c 20 20 20 33 37 2c 20 20 32 34 33 2c 20 20  8,   37,  243,  
2d444 33 33 31 2c 20 20 35 38 32 2c 0a 20 2f 2a 20 20  331,  582,. /*  
2d445 31 31 35 30 20 2a 2f 20 20 20 32 34 34 2c 20 20  1150 */   244,  
2d446 34 31 35 2c 20 20 34 32 36 2c 20 20 32 37 36 2c  415,  426,  276,
2d447 20 20 20 33 36 2c 20 20 33 32 39 2c 20 20 33 33     36,  329,  33
2d448 30 2c 20 20 20 34 36 2c 20 20 32 34 35 2c 20 20  0,   46,  245,  
2d449 34 34 31 2c 0a 20 2f 2a 20 20 31 31 36 30 20 2a  441,. /*  1160 *
2d44a 2f 20 20 20 20 33 38 2c 20 20 20 33 37 2c 20 20  /    38,   37,  
2d44b 35 30 35 2c 20 20 33 32 38 2c 20 20 32 30 32 2c  505,  328,  202,
2d44c 20 20 32 30 33 2c 20 20 32 30 34 2c 20 20 34 31    203,  204,  41
2d44d 35 2c 20 20 34 31 35 2c 20 20 20 33 36 2c 0a 20  5,  415,   36,. 
2d44e 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20 20 33 32  /*  1170 */   32
2d44f 39 2c 20 20 33 33 30 2c 20 20 34 33 35 2c 20 20  9,  330,  435,  
2d450 20 39 32 2c 20 20 34 34 31 2c 20 20 31 39 38 2c   92,  441,  198,
2d451 20 20 35 36 38 2c 20 20 32 31 34 2c 20 20 31 35    568,  214,  15
2d452 35 2c 20 20 35 38 34 2c 0a 20 2f 2a 20 20 31 31  5,  584,. /*  11
2d453 38 30 20 2a 2f 20 20 20 32 33 35 2c 20 20 32 33  80 */   235,  23
2d454 36 2c 20 20 32 33 37 2c 20 20 31 34 33 2c 20 20  6,  237,  143,  
2d455 32 33 39 2c 20 20 33 34 36 2c 20 20 31 33 33 2c  239,  346,  133,
2d456 20 20 35 38 31 2c 20 20 34 33 38 2c 20 20 32 34    581,  438,  24
2d457 36 2c 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20  6,. /*  1190 */ 
2d458 20 20 34 34 33 2c 20 20 34 34 33 2c 20 20 34 34    443,  443,  44
2d459 33 2c 20 20 34 34 34 2c 20 20 34 34 35 2c 20 20  3,  444,  445,  
2d45a 20 31 30 2c 20 20 35 38 35 2c 20 20 32 37 36 2c   10,  585,  276,
2d45b 20 20 20 32 30 2c 20 20 20 34 32 2c 0a 20 2f 2a     20,   42,. /*
2d45c 20 20 31 32 30 30 20 2a 2f 20 20 20 31 37 32 2c    1200 */   172,
2d45d 20 20 34 31 35 2c 20 20 32 39 34 2c 20 20 33 33    415,  294,  33
2d45e 31 2c 20 20 32 38 38 2c 20 20 34 34 33 2c 20 20  1,  288,  443,  
2d45f 34 34 33 2c 20 20 34 34 33 2c 20 20 34 34 34 2c  443,  443,  444,
2d460 20 20 34 34 35 2c 0a 20 2f 2a 20 20 31 32 31 30    445,. /*  1210
2d461 20 2a 2f 20 20 20 20 31 30 2c 20 20 32 39 35 2c   */    10,  295,
2d462 20 20 34 31 35 2c 20 20 20 33 35 2c 20 20 33 33    415,   35,  33
2d463 38 2c 20 20 32 31 39 2c 20 20 20 20 33 2c 20 20  8,  219,    3,  
2d464 31 34 39 2c 20 20 33 32 38 2c 20 20 34 38 32 2c  149,  328,  482,
2d465 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20 20  . /*  1220 */   
2d466 33 32 37 2c 20 20 34 31 34 2c 20 20 33 33 31 2c  327,  414,  331,
2d467 20 20 31 37 30 2c 20 20 32 37 36 2c 20 20 35 37    170,  276,  57
2d468 32 2c 20 20 20 34 38 2c 20 20 34 33 35 2c 20 20  2,   48,  435,  
2d469 20 37 35 2c 20 20 31 36 39 2c 0a 20 2f 2a 20 20   75,  169,. /*  
2d46a 31 32 33 30 20 2a 2f 20 20 20 33 33 35 2c 20 20  1230 */   335,  
2d46b 20 31 39 2c 20 20 31 37 31 2c 20 20 32 35 31 2c   19,  171,  251,
2d46c 20 20 34 34 32 2c 20 20 34 31 33 2c 20 20 34 31    442,  413,  41
2d46d 34 2c 20 20 33 32 38 2c 20 20 33 33 31 2c 20 20  4,  328,  331,  
2d46e 34 31 35 2c 0a 20 2f 2a 20 20 31 32 34 30 20 2a  415,. /*  1240 *
2d46f 2f 20 20 20 35 38 36 2c 20 20 33 34 33 2c 20 20  /   586,  343,  
2d470 32 37 36 2c 20 20 31 37 37 2c 20 20 33 35 31 2c  276,  177,  351,
2d471 20 20 34 39 36 2c 20 20 34 33 35 2c 20 20 20 31    496,  435,   1
2d472 37 2c 20 20 33 34 30 2c 20 20 34 31 35 2c 0a 20  7,  340,  415,. 
2d473 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20 20 34 38  /*  1250 */   48
2d474 31 2c 20 20 32 35 33 2c 20 20 32 35 35 2c 20 20  1,  253,  255,  
2d475 33 32 38 2c 20 20 32 37 36 2c 20 20 35 30 32 2c  328,  276,  502,
2d476 20 20 34 31 35 2c 20 20 34 31 35 2c 20 20 34 37    415,  415,  47
2d477 32 2c 20 20 33 33 31 2c 0a 20 2f 2a 20 20 31 32  2,  331,. /*  12
2d478 36 30 20 2a 2f 20 20 20 35 30 33 2c 20 20 33 35  60 */   503,  35
2d479 37 2c 20 20 34 33 35 2c 20 20 20 39 34 2c 20 20  7,  435,   94,  
2d47a 35 37 36 2c 20 20 34 31 35 2c 20 20 31 35 31 2c  576,  415,  151,
2d47b 20 20 32 33 31 2c 20 20 33 31 32 2c 20 20 34 31    231,  312,  41
2d47c 35 2c 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f 20  5,. /*  1270 */ 
2d47d 20 20 35 37 37 2c 20 20 35 31 36 2c 20 20 20 35    577,  516,   5
2d47e 34 2c 20 20 34 37 32 2c 20 20 33 32 38 2c 20 20  4,  472,  328,  
2d47f 33 39 33 2c 20 20 32 39 31 2c 20 20 32 38 31 2c  393,  291,  281,
2d480 20 20 20 33 38 2c 20 20 20 33 37 2c 0a 20 2f 2a     38,   37,. /*
2d481 20 20 31 32 38 30 20 2a 2f 20 20 20 34 39 34 2c    1280 */   494,
2d482 20 20 33 30 35 2c 20 20 35 32 31 2c 20 20 34 33    305,  521,  43
2d483 35 2c 20 20 20 39 35 2c 20 20 32 33 32 2c 20 20  5,   95,  232,  
2d484 32 31 34 2c 20 20 20 33 36 2c 20 20 33 32 39 2c  214,   36,  329,
2d485 20 20 33 33 30 2c 0a 20 2f 2a 20 20 31 32 39 30    330,. /*  1290
2d486 20 2a 2f 20 20 20 35 32 36 2c 20 20 34 39 38 2c   */   526,  498,
2d487 20 20 34 34 31 2c 20 20 31 38 38 2c 20 20 31 38    441,  188,  18
2d488 39 2c 20 20 34 31 35 2c 20 20 35 30 30 2c 20 20  9,  415,  500,  
2d489 32 39 32 2c 20 20 35 32 32 2c 20 20 32 36 32 2c  292,  522,  262,
2d48a 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20 20  . /*  1300 */   
2d48b 35 33 30 2c 20 20 32 36 30 2c 20 20 32 36 33 2c  530,  260,  263,
2d48c 20 20 35 31 33 2c 20 20 35 34 39 2c 20 20 32 36    513,  549,  26
2d48d 39 2c 20 20 34 31 35 2c 20 20 34 34 31 2c 20 20  9,  415,  441,  
2d48e 35 38 39 2c 20 20 34 30 30 2c 0a 20 2f 2a 20 20  589,  400,. /*  
2d48f 31 33 31 30 20 2a 2f 20 20 20 20 35 34 2c 20 20  1310 */    54,  
2d490 34 31 35 2c 20 20 35 32 35 2c 20 20 35 32 37 2c  415,  525,  527,
2d491 20 20 34 31 35 2c 20 20 34 31 35 2c 20 20 32 37    415,  415,  27
2d492 31 2c 20 20 34 31 35 2c 20 20 32 37 33 2c 20 20  1,  415,  273,  
2d493 34 31 35 2c 0a 20 2f 2a 20 20 31 33 32 30 20 2a  415,. /*  1320 *
2d494 2f 20 20 20 34 31 35 2c 20 20 32 37 35 2c 20 20  /   415,  275,  
2d495 32 38 30 2c 20 20 34 34 33 2c 20 20 34 34 33 2c  280,  443,  443,
2d496 20 20 34 34 33 2c 20 20 34 34 34 2c 20 20 34 34    443,  444,  44
2d497 35 2c 20 20 20 31 30 2c 20 20 31 30 37 2c 0a 20  5,   10,  107,. 
2d498 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20 20 33 38  /*  1330 */   38
2d499 30 2c 20 20 34 31 35 2c 20 20 33 38 33 2c 20 20  0,  415,  383,  
2d49a 34 31 35 2c 20 20 33 38 34 2c 20 20 32 38 33 2c  415,  384,  283,
2d49b 20 20 34 31 35 2c 20 20 34 31 35 2c 20 20 34 34    415,  415,  44
2d49c 33 2c 20 20 34 34 33 2c 0a 20 2f 2a 20 20 31 33  3,  443,. /*  13
2d49d 34 30 20 2a 2f 20 20 20 34 34 33 2c 20 20 32 38  40 */   443,  28
2d49e 34 2c 20 20 32 38 35 2c 20 20 35 38 30 2c 20 20  4,  285,  580,  
2d49f 33 30 30 2c 20 20 34 31 35 2c 20 20 35 39 31 2c  300,  415,  591,
2d4a0 20 20 34 31 35 2c 20 20 32 39 33 2c 20 20 34 31    415,  293,  41
2d4a1 35 2c 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20  5,. /*  1350 */ 
2d4a2 20 20 34 31 35 2c 20 20 32 39 36 2c 20 20 32 39    415,  296,  29
2d4a3 37 2c 20 20 36 30 35 2c 20 20 32 32 36 2c 20 20  7,  605,  226,  
2d4a4 35 35 30 2c 20 20 34 31 35 2c 20 20 34 31 35 2c  550,  415,  415,
2d4a5 20 20 34 31 35 2c 20 20 32 32 35 2c 0a 20 2f 2a    415,  225,. /*
2d4a6 20 20 31 33 36 30 20 2a 2f 20 20 20 36 30 38 2c    1360 */   608,
2d4a7 20 20 34 31 35 2c 20 20 33 30 32 2c 20 20 34 31    415,  302,  41
2d4a8 35 2c 20 20 35 35 31 2c 20 20 32 32 37 2c 20 20  5,  551,  227,  
2d4a9 34 31 35 2c 20 20 34 31 35 2c 20 20 34 31 35 2c  415,  415,  415,
2d4aa 20 20 33 30 31 2c 0a 20 2f 2a 20 20 31 33 37 30    301,. /*  1370
2d4ab 20 2a 2f 20 20 20 35 34 34 2c 20 20 35 35 32 2c   */   544,  552,
2d4ac 20 20 33 36 39 2c 20 20 31 35 38 2c 20 20 33 37    369,  158,  37
2d4ad 33 2c 20 20 35 35 38 2c 20 20 31 35 39 2c 20 20  3,  558,  159,  
2d4ae 32 37 38 2c 20 20 33 37 31 2c 20 20 31 36 30 2c  278,  371,  160,
2d4af 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f 20 20 20  . /*  1380 */   
2d4b0 20 35 31 2c 20 20 32 30 37 2c 20 20 35 36 30 2c   51,  207,  560,
2d4b1 20 20 35 36 31 2c 20 20 31 36 31 2c 20 20 31 34    561,  161,  14
2d4b2 30 2c 20 20 33 37 39 2c 20 20 31 31 37 2c 20 20  0,  379,  117,  
2d4b3 35 37 31 2c 20 20 31 36 33 2c 0a 20 2f 2a 20 20  571,  163,. /*  
2d4b4 31 33 39 30 20 2a 2f 20 20 20 33 39 31 2c 20 20  1390 */   391,  
2d4b5 33 39 32 2c 20 20 31 38 31 2c 20 20 31 38 30 2c  392,  181,  180,
2d4b6 20 20 33 32 31 2c 20 20 36 30 32 2c 20 20 35 37    321,  602,  57
2d4b7 38 2c 20 20 31 31 38 2c 20 20 31 31 39 2c 20 20  8,  118,  119,  
2d4b8 31 32 30 2c 0a 20 2f 2a 20 20 31 34 30 30 20 2a  120,. /*  1400 *
2d4b9 2f 20 20 20 31 32 31 2c 20 20 31 32 33 2c 20 20  /   121,  123,  
2d4ba 20 35 35 2c 20 20 35 38 37 2c 20 20 20 35 38 2c   55,  587,   58,
2d4bb 20 20 36 30 33 2c 20 20 36 30 34 2c 20 20 36 30    603,  604,  60
2d4bc 37 2c 20 20 20 36 32 2c 20 20 31 37 34 2c 0a 20  7,   62,  174,. 
2d4bd 2f 2a 20 20 31 34 31 30 20 2a 2f 20 20 20 31 30  /*  1410 */   10
2d4be 33 2c 20 20 32 32 34 2c 20 20 31 31 31 2c 20 20  3,  224,  111,  
2d4bf 34 30 39 2c 20 20 32 33 38 2c 20 20 34 32 38 2c  409,  238,  428,
2d4c0 20 20 31 39 39 2c 20 20 33 32 33 2c 20 20 36 35    199,  323,  65
2d4c1 37 2c 20 20 36 35 38 2c 0a 20 2f 2a 20 20 31 34  7,  658,. /*  14
2d4c2 32 30 20 2a 2f 20 20 20 36 35 39 2c 20 20 31 34  20 */   659,  14
2d4c3 36 2c 20 20 31 34 37 2c 20 20 34 35 35 2c 20 20  6,  147,  455,  
2d4c4 34 35 38 2c 20 20 20 33 34 2c 20 20 34 37 34 2c  458,   34,  474,
2d4c5 20 20 34 36 32 2c 20 20 34 37 31 2c 20 20 31 38    462,  471,  18
2d4c6 32 2c 0a 20 2f 2a 20 20 31 34 33 30 20 2a 2f 20  2,. /*  1430 */ 
2d4c7 20 20 31 39 35 2c 20 20 31 34 38 2c 20 20 34 37    195,  148,  47
2d4c8 35 2c 20 20 34 37 36 2c 20 20 34 38 30 2c 20 20  5,  476,  480,  
2d4c9 20 20 35 2c 20 20 20 31 32 2c 20 20 34 39 33 2c    5,   12,  493,
2d4ca 20 20 20 34 34 2c 20 20 20 31 31 2c 0a 20 2f 2a     44,   11,. /*
2d4cb 20 20 31 34 34 30 20 2a 2f 20 20 20 31 30 36 2c    1440 */   106,
2d4cc 20 20 31 33 38 2c 20 20 35 30 39 2c 20 20 35 31    138,  509,  51
2d4cd 30 2c 20 20 35 30 31 2c 20 20 32 32 33 2c 20 20  0,  501,  223,  
2d4ce 20 34 39 2c 20 20 33 36 31 2c 20 20 31 30 38 2c   49,  361,  108,
2d4cf 20 20 31 30 39 2c 0a 20 2f 2a 20 20 31 34 35 30    109,. /*  1450
2d4d0 20 2a 2f 20 20 20 31 35 32 2c 20 20 32 36 36 2c   */   152,  266,
2d4d1 20 20 20 35 30 2c 20 20 31 31 30 2c 20 20 31 35     50,  110,  15
2d4d2 37 2c 20 20 32 35 38 2c 20 20 33 37 30 2c 20 20  7,  258,  370,  
2d4d3 31 38 34 2c 20 20 35 35 39 2c 20 20 31 33 39 2c  184,  559,  139,
2d4d4 0a 20 2f 2a 20 20 31 34 36 30 20 2a 2f 20 20 20  . /*  1460 */   
2d4d5 31 35 31 2c 20 20 31 31 33 2c 20 20 32 37 39 2c  151,  113,  279,
2d4d6 20 20 31 36 32 2c 20 20 31 31 35 2c 20 20 33 37    162,  115,  37
2d4d7 34 2c 20 20 20 31 35 2c 20 20 35 37 34 2c 20 20  4,   15,  574,  
2d4d8 31 31 36 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20  116,  165,. /*  
2d4d9 31 34 37 30 20 2a 2f 20 20 20 20 35 32 2c 20 20  1470 */    52,  
2d4da 20 31 33 2c 20 20 33 36 36 2c 20 20 35 37 39 2c   13,  366,  579,
2d4db 20 20 20 35 33 2c 20 20 31 36 37 2c 20 20 31 36     53,  167,  16
2d4dc 38 2c 20 20 31 36 36 2c 20 20 35 38 33 2c 20 20  8,  166,  583,  
2d4dd 31 32 34 2c 0a 20 2f 2a 20 20 31 34 38 30 20 2a  124,. /*  1480 *
2d4de 2f 20 20 20 31 31 34 2c 20 20 31 32 32 2c 20 20  /   114,  122,  
2d4df 35 36 32 2c 20 20 35 36 36 2c 20 20 20 31 34 2c  562,  566,   14,
2d4e0 20 20 20 36 31 2c 20 20 35 39 39 2c 20 20 36 30     61,  599,  60
2d4e1 30 2c 20 20 31 32 35 2c 20 20 31 37 33 2c 0a 20  0,  125,  173,. 
2d4e2 2f 2a 20 20 31 34 39 30 20 2a 2f 20 20 20 32 39  /*  1490 */   29
2d4e3 38 2c 20 20 35 39 30 2c 20 20 31 38 37 2c 20 20  8,  590,  187,  
2d4e4 34 30 36 2c 20 20 39 34 31 2c 20 20 36 31 34 2c  406,  941,  614,
2d4e5 20 20 39 34 31 2c 20 20 34 30 32 2c 0a 7d 3b 0a    941,  402,.};.
2d4e6 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59 43  static const YYC
2d4e7 4f 44 45 54 59 50 45 20 79 79 5f 6c 6f 6f 6b 61  ODETYPE yy_looka
2d4e8 68 65 61 64 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20  head[] = {. /*  
2d4e9 20 20 20 30 20 2a 2f 20 20 20 20 31 39 2c 20 20     0 */    19,  
2d4ea 31 34 32 2c 20 20 31 34 33 2c 20 20 31 34 34 2c  142,  143,  144,
2d4eb 20 20 31 34 35 2c 20 20 20 32 34 2c 20 20 31 31    145,   24,  11
2d4ec 36 2c 20 20 20 32 36 2c 20 20 20 37 35 2c 20 20  6,   26,   75,  
2d4ed 20 37 36 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a   76,. /*    10 *
2d4ee 2f 20 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  /    77,   78,  
2d4ef 20 32 35 2c 20 20 20 38 30 2c 20 20 20 38 31 2c   25,   80,   81,
2d4f0 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
2d4f1 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 0a 20  4,   85,   86,. 
2d4f2 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 20 38  /*    20 */    8
2d4f3 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
2d4f4 20 39 30 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   90,   26,   27,
2d4f5 20 20 31 36 30 2c 20 20 20 32 36 2c 20 20 20 32    160,   26,   2
2d4f6 37 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20 20  7,   48,. /*    
2d4f7 33 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 20 37  30 */    49,   7
2d4f8 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  9,   80,   81,  
2d4f9 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
2d4fa 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
2d4fb 37 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20  7,. /*    40 */ 
2d4fc 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
2d4fd 30 2c 20 20 32 32 32 2c 20 20 32 32 33 2c 20 20  0,  222,  223,  
2d4fe 32 32 34 2c 20 20 32 32 35 2c 20 20 20 36 36 2c  224,  225,   66,
2d4ff 20 20 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f 2a     67,   68,. /*
2d500 20 20 20 20 35 30 20 2a 2f 20 20 20 20 36 39 2c      50 */    69,
2d501 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2d502 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
2d503 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
2d504 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20 20 36 30     78,. /*    60
2d505 20 2a 2f 20 20 20 31 39 34 2c 20 20 20 38 30 2c   */   194,   80,
2d506 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2d507 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
2d508 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
2d509 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20  . /*    70 */   
2d50a 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c   89,   90,   19,
2d50b 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 20 39     90,   19,   9
2d50c 34 2c 20 20 31 37 34 2c 20 20 20 32 35 2c 20 20  4,  174,   25,  
2d50d 20 32 35 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20   25,   80,. /*  
2d50e 20 20 38 30 20 2a 2f 20 20 20 20 38 31 2c 20 20    80 */    81,  
2d50f 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
2d510 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
2d511 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
2d512 20 39 30 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a   90,. /*    90 *
2d513 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  /    26,   27,  
2d514 20 39 34 2c 20 20 20 39 35 2c 20 20 20 39 36 2c   94,   95,   96,
2d515 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20 39     94,   95,   9
2d516 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 0a 20  9,  100,  101,. 
2d517 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 20 31  /*   100 */    1
2d518 39 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20  9,   48,   49,  
2d519 31 35 30 2c 20 20 31 37 34 2c 20 20 20 35 32 2c  150,  174,   52,
2d51a 20 20 31 31 39 2c 20 20 31 36 36 2c 20 20 31 31    119,  166,  11
2d51b 30 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20 31  0,   84,. /*   1
2d51c 31 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 38  10 */    85,   8
2d51d 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
2d51e 20 38 39 2c 20 20 20 39 30 2c 20 20 20 32 36 2c   89,   90,   26,
2d51f 20 20 20 32 37 2c 20 20 31 36 35 2c 20 20 20 36     27,  165,   6
2d520 36 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20  6,. /*   120 */ 
2d521 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36     67,   68,   6
2d522 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
2d523 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
2d524 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a     75,   76,. /*
2d525 20 20 20 31 33 30 20 2a 2f 20 20 20 20 37 37 2c     130 */    77,
2d526 20 20 20 37 38 2c 20 20 31 38 36 2c 20 20 20 38     78,  186,   8
2d527 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20  0,   81,   82,  
2d528 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
2d529 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 31 34 30     86,. /*   140
2d52a 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c   */    87,   88,
2d52b 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31     89,   90,   1
2d52c 39 2c 20 20 20 39 30 2c 20 20 32 30 35 2c 20 20  9,   90,  205,  
2d52d 20 39 35 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   95,   84,   85,
2d52e 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20  . /*   150 */   
2d52f 31 38 36 2c 20 20 20 39 36 2c 20 20 20 39 37 2c  186,   96,   97,
2d530 20 20 20 39 38 2c 20 20 20 39 39 2c 20 20 31 30     98,   99,  10
2d531 30 2c 20 20 31 30 31 2c 20 20 31 30 32 2c 20 20  0,  101,  102,  
2d532 20 39 34 2c 20 20 20 39 35 2c 0a 20 2f 2a 20 20   94,   95,. /*  
2d533 20 31 36 30 20 2a 2f 20 20 20 31 39 35 2c 20 20   160 */   195,  
2d534 20 39 37 2c 20 20 31 35 30 2c 20 20 32 32 32 2c   97,  150,  222,
2d535 20 20 31 30 39 2c 20 20 32 32 34 2c 20 20 32 32    109,  224,  22
2d536 35 2c 20 20 20 32 36 2c 20 20 31 30 34 2c 20 20  5,   26,  104,  
2d537 31 30 35 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a  105,. /*   170 *
2d538 2f 20 20 20 32 31 37 2c 20 20 20 39 30 2c 20 20  /   217,   90,  
2d539 31 32 30 2c 20 20 20 34 38 2c 20 20 20 34 39 2c  120,   48,   49,
2d53a 20 20 20 35 30 2c 20 20 20 38 36 2c 20 20 31 36     50,   86,  16
2d53b 35 2c 20 20 20 39 37 2c 20 20 20 39 38 2c 0a 20  5,   97,   98,. 
2d53c 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 20 39  /*   180 */    9
2d53d 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20  9,  100,  101,  
2d53e 31 30 32 2c 20 20 20 39 34 2c 20 20 20 39 35 2c  102,   94,   95,
2d53f 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 20    174,  175,    
2d540 31 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20 31  1,    2,. /*   1
2d541 39 30 20 2a 2f 20 20 20 31 30 39 2c 20 20 20 36  90 */   109,   6
2d542 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  6,   67,   68,  
2d543 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
2d544 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
2d545 34 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20  4,. /*   200 */ 
2d546 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
2d547 37 2c 20 20 20 37 38 2c 20 20 31 39 31 2c 20 20  7,   78,  191,  
2d548 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
2d549 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a     83,   84,. /*
2d54a 20 20 20 32 31 30 20 2a 2f 20 20 20 20 38 35 2c     210 */    85,
2d54b 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
2d54c 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
2d54d 20 31 39 2c 20 20 31 31 36 2c 20 20 20 33 35 2c   19,  116,   35,
2d54e 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 32 32 30    150,. /*   220
2d54f 20 2a 2f 20 20 20 31 35 35 2c 20 20 20 32 34 2c   */   155,   24,
2d550 20 20 32 30 38 2c 20 20 31 35 30 2c 20 20 32 32    208,  150,  22
2d551 32 2c 20 20 31 35 30 2c 20 20 32 32 34 2c 20 20  2,  150,  224,  
2d552 32 32 35 2c 20 20 32 31 36 2c 20 20 31 32 38 2c  225,  216,  128,
2d553 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20  . /*   230 */   
2d554 31 36 31 2c 20 20 31 36 32 2c 20 20 31 35 30 2c  161,  162,  150,
2d555 20 20 32 32 31 2c 20 20 31 36 35 2c 20 20 20 39    221,  165,   9
2d556 34 2c 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20  4,   23,  150,  
2d557 31 36 35 2c 20 20 20 35 36 2c 0a 20 2f 2a 20 20  165,   56,. /*  
2d558 20 32 34 30 20 2a 2f 20 20 20 31 36 35 2c 20 20   240 */   165,  
2d559 31 39 37 2c 20 20 31 36 30 2c 20 20 31 37 30 2c  197,  160,  170,
2d55a 20 20 31 37 31 2c 20 20 20 34 38 2c 20 20 20 34    171,   48,   4
2d55b 39 2c 20 20 31 36 35 2c 20 20 32 30 34 2c 20 20  9,  165,  204,  
2d55c 31 37 34 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a  174,. /*   250 *
2d55d 2f 20 20 20 31 37 35 2c 20 20 20 32 32 2c 20 20  /   175,   22,  
2d55e 31 36 35 2c 20 20 20 32 34 2c 20 20 31 38 35 2c  165,   24,  185,
2d55f 20 20 31 38 36 2c 20 20 31 37 34 2c 20 20 31 37    186,  174,  17
2d560 35 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 0a 20  5,  169,  170,. 
2d561 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 31 37  /*   260 */   17
2d562 31 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  1,  174,  175,  
2d563 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c   66,   67,   68,
2d564 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
2d565 31 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 20 32  1,   72,. /*   2
2d566 37 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 20 37  70 */    73,   7
2d567 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
2d568 20 37 37 2c 20 20 20 37 38 2c 20 20 31 39 34 2c   77,   78,  194,
2d569 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38     80,   81,   8
2d56a 32 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20  2,. /*   280 */ 
2d56b 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
2d56c 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
2d56d 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
2d56e 20 20 20 31 39 2c 20 20 32 31 34 2c 0a 20 2f 2a     19,  214,. /*
2d56f 20 20 20 32 39 30 20 2a 2f 20 20 20 32 31 35 2c     290 */   215,
2d570 20 20 31 30 38 2c 20 20 31 35 30 2c 20 20 20 32    108,  150,   2
2d571 35 2c 20 20 32 32 39 2c 20 20 31 35 30 2c 20 20  5,  229,  150,  
2d572 20 36 34 2c 20 20 31 34 38 2c 20 20 32 31 36 2c   64,  148,  216,
2d573 20 20 32 33 34 2c 0a 20 2f 2a 20 20 20 33 30 30    234,. /*   300
2d574 20 2a 2f 20 20 20 31 34 36 2c 20 20 31 34 37 2c   */   146,  147,
2d575 20 20 32 31 35 2c 20 20 32 32 31 2c 20 20 32 33    215,  221,  23
2d576 31 2c 20 20 32 33 32 2c 20 20 31 35 32 2c 20 20  1,  232,  152,  
2d577 31 36 35 2c 20 20 31 35 34 2c 20 20 31 35 30 2c  165,  154,  150,
2d578 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20  . /*   310 */   
2d579 31 36 35 2c 20 20 31 39 36 2c 20 20 31 37 30 2c  165,  196,  170,
2d57a 20 20 31 37 31 2c 20 20 31 36 30 2c 20 20 31 38    171,  160,  18
2d57b 31 2c 20 20 31 38 32 2c 20 20 20 34 38 2c 20 20  1,  182,   48,  
2d57c 20 34 39 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20   49,  174,. /*  
2d57d 20 33 32 30 20 2a 2f 20 20 20 31 37 35 2c 20 20   320 */   175,  
2d57e 32 33 32 2c 20 20 31 38 38 2c 20 20 31 36 35 2c  232,  188,  165,
2d57f 20 20 31 36 35 2c 20 20 31 31 32 2c 20 20 20 39    165,  112,   9
2d580 34 2c 20 20 31 31 34 2c 20 20 31 31 35 2c 20 20  4,  114,  115,  
2d581 31 36 36 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a  166,. /*   330 *
2d582 2f 20 20 20 20 39 38 2c 20 20 20 35 35 2c 20 20  /    98,   55,  
2d583 31 37 34 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  174,  174,  175,
2d584 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36     66,   67,   6
2d585 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20  8,   69,   70,. 
2d586 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 20 37  /*   340 */    7
2d587 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
2d588 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
2d589 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 31 39     77,   78,  19
2d58a 34 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 33  4,   80,. /*   3
2d58b 35 30 20 2a 2f 20 20 20 20 38 31 2c 20 20 20 38  50 */    81,   8
2d58c 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
2d58d 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
2d58e 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
2d58f 30 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20  0,. /*   360 */ 
2d590 20 20 20 31 39 2c 20 20 31 32 39 2c 20 20 31 33     19,  129,  13
2d591 30 2c 20 20 31 33 31 2c 20 20 20 39 36 2c 20 20  0,  131,   96,  
2d592 20 38 34 2c 20 20 20 38 35 2c 20 20 20 39 39 2c   84,   85,   99,
2d593 20 20 31 30 30 2c 20 20 31 30 31 2c 0a 20 2f 2a    100,  101,. /*
2d594 20 20 20 33 37 30 20 2a 2f 20 20 20 31 35 30 2c     370 */   150,
2d595 20 20 32 32 36 2c 20 20 32 31 38 2c 20 20 32 33    226,  218,  23
2d596 31 2c 20 20 32 33 32 2c 20 20 32 31 36 2c 20 20  1,  232,  216,  
2d597 31 35 30 2c 20 20 32 32 32 2c 20 20 31 31 30 2c  150,  222,  110,
2d598 20 20 32 32 34 2c 0a 20 2f 2a 20 20 20 33 38 30    224,. /*   380
2d599 20 2a 2f 20 20 20 32 32 35 2c 20 20 31 30 35 2c   */   225,  105,
2d59a 20 20 31 30 36 2c 20 20 31 30 37 2c 20 20 31 33    106,  107,  13
2d59b 35 2c 20 20 31 36 35 2c 20 20 31 33 37 2c 20 20  5,  165,  137,  
2d59c 31 37 32 2c 20 20 31 37 33 2c 20 20 20 34 38 2c  172,  173,   48,
2d59d 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20  . /*   390 */   
2d59e 20 34 39 2c 20 20 31 36 35 2c 20 20 31 31 36 2c   49,  165,  116,
2d59f 20 20 31 38 33 2c 20 20 31 37 34 2c 20 20 31 37    183,  174,  17
2d5a0 35 2c 20 20 31 38 31 2c 20 20 32 34 32 2c 20 20  5,  181,  242,  
2d5a1 20 32 32 2c 20 20 32 34 35 2c 0a 20 2f 2a 20 20   22,  245,. /*  
2d5a2 20 34 30 30 20 2a 2f 20 20 20 31 37 34 2c 20 20   400 */   174,  
2d5a3 31 37 35 2c 20 20 20 32 36 2c 20 20 20 32 37 2c  175,   26,   27,
2d5a4 20 20 31 36 36 2c 20 20 31 33 36 2c 20 20 31 38    166,  136,  18
2d5a5 33 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  3,   66,   67,  
2d5a6 20 36 38 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a   68,. /*   410 *
2d5a7 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  /    69,   70,  
2d5a8 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
2d5a9 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
2d5aa 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20  6,   77,   78,. 
2d5ab 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 20 31  /*   420 */    1
2d5ac 31 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  1,   80,   81,  
2d5ad 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
2d5ae 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
2d5af 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 34  7,   88,. /*   4
2d5b0 33 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39  30 */    89,   9
2d5b1 30 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20  0,   19,  150,  
2d5b2 31 35 30 2c 20 20 20 32 33 2c 20 20 20 32 33 2c  150,   23,   23,
2d5b3 20 20 20 32 35 2c 20 20 31 36 30 2c 20 20 31 35     25,  160,  15
2d5b4 30 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20  0,. /*   440 */ 
2d5b5 20 20 32 32 30 2c 20 20 20 32 36 2c 20 20 20 32    220,   26,   2
2d5b6 37 2c 20 20 32 30 35 2c 20 20 31 36 30 2c 20 20  7,  205,  160,  
2d5b7 31 35 30 2c 20 20 32 32 30 2c 20 20 31 35 38 2c  150,  220,  158,
2d5b8 20 20 31 36 35 2c 20 20 31 36 35 2c 0a 20 2f 2a    165,  165,. /*
2d5b9 20 20 20 34 35 30 20 2a 2f 20 20 20 31 36 31 2c     450 */   161,
2d5ba 20 20 31 36 32 2c 20 20 20 32 36 2c 20 20 20 32    162,   26,   2
2d5bb 37 2c 20 20 31 36 35 2c 20 20 20 20 30 2c 20 20  7,  165,    0,  
2d5bc 20 20 31 2c 20 20 20 20 32 2c 20 20 31 37 34 2c    1,    2,  174,
2d5bd 20 20 31 37 35 2c 0a 20 2f 2a 20 20 20 34 36 30    175,. /*   460
2d5be 20 2a 2f 20 20 20 31 36 35 2c 20 20 20 34 38 2c   */   165,   48,
2d5bf 20 20 20 34 39 2c 20 20 20 32 33 2c 20 20 20 35     49,   23,   5
2d5c0 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
2d5c1 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
2d5c2 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20  . /*   470 */   
2d5c3 20 39 34 2c 20 20 20 39 35 2c 20 20 31 39 34 2c   94,   95,  194,
2d5c4 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20 31 37    169,  170,  17
2d5c5 31 2c 20 20 31 39 33 2c 20 20 31 33 36 2c 20 20  1,  193,  136,  
2d5c6 31 39 34 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20  194,   66,. /*  
2d5c7 20 34 38 30 20 2a 2f 20 20 20 20 36 37 2c 20 20   480 */    67,  
2d5c8 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
2d5c9 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
2d5ca 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
2d5cb 20 37 36 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a   76,. /*   490 *
2d5cc 2f 20 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  /    77,   78,  
2d5cd 31 36 36 2c 20 20 20 38 30 2c 20 20 20 38 31 2c  166,   80,   81,
2d5ce 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
2d5cf 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 0a 20  4,   85,   86,. 
2d5d0 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 20 38  /*   500 */    8
2d5d1 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
2d5d2 20 39 30 2c 20 20 20 31 39 2c 20 20 20 36 35 2c   90,   19,   65,
2d5d3 20 20 31 36 30 2c 20 20 20 39 35 2c 20 20 20 32    160,   95,   2
2d5d4 33 2c 20 20 20 39 34 2c 0a 20 2f 2a 20 20 20 35  3,   94,. /*   5
2d5d5 31 30 20 2a 2f 20 20 20 20 39 35 2c 20 20 31 37  10 */    95,  17
2d5d6 33 2c 20 20 32 31 37 2c 20 20 20 32 32 2c 20 20  3,  217,   22,  
2d5d7 31 30 35 2c 20 20 31 30 36 2c 20 20 31 30 37 2c  105,  106,  107,
2d5d8 20 20 20 32 36 2c 20 20 20 32 32 2c 20 20 31 38     26,   22,  18
2d5d9 31 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20  1,. /*   520 */ 
2d5da 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20 32     94,   95,   2
2d5db 36 2c 20 20 20 32 37 2c 20 20 20 39 36 2c 20 20  6,   27,   96,  
2d5dc 31 31 36 2c 20 20 32 34 33 2c 20 20 20 39 39 2c  116,  243,   99,
2d5dd 20 20 31 30 30 2c 20 20 31 30 31 2c 0a 20 2f 2a    100,  101,. /*
2d5de 20 20 20 35 33 30 20 2a 2f 20 20 20 31 35 30 2c     530 */   150,
2d5df 20 20 32 30 35 2c 20 20 31 32 30 2c 20 20 20 34    205,  120,   4
2d5e0 38 2c 20 20 20 34 39 2c 20 20 31 32 30 2c 20 20  8,   49,  120,  
2d5e1 32 33 32 2c 20 20 20 32 32 2c 20 20 31 31 30 2c  232,   22,  110,
2d5e2 20 20 31 36 36 2c 0a 20 2f 2a 20 20 20 35 34 30    166,. /*   540
2d5e3 20 2a 2f 20 20 20 31 39 34 2c 20 20 31 36 31 2c   */   194,  161,
2d5e4 20 20 31 36 32 2c 20 20 31 38 33 2c 20 20 31 36    162,  183,  16
2d5e5 33 2c 20 20 31 36 35 2c 20 20 31 32 30 2c 20 20  3,  165,  120,  
2d5e6 31 36 36 2c 20 20 31 36 37 2c 20 20 31 36 38 2c  166,  167,  168,
2d5e7 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20  . /*   550 */   
2d5e8 31 36 30 2c 20 20 20 36 36 2c 20 20 20 36 37 2c  160,   66,   67,
2d5e9 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
2d5ea 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
2d5eb 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20   73,   74,. /*  
2d5ec 20 35 36 30 20 2a 2f 20 20 20 20 37 35 2c 20 20   560 */    75,  
2d5ed 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
2d5ee 20 20 32 31 38 2c 20 20 20 38 30 2c 20 20 20 38    218,   80,   8
2d5ef 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
2d5f0 20 38 34 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a   84,. /*   570 *
2d5f1 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  /    85,   86,  
2d5f2 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
2d5f3 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 20 31     90,   19,   1
2d5f4 32 2c 20 20 32 30 35 2c 20 20 31 35 30 2c 0a 20  2,  205,  150,. 
2d5f5 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 20 32  /*   580 */    2
2d5f6 33 2c 20 20 32 33 35 2c 20 20 31 39 30 2c 20 20  3,  235,  190,  
2d5f7 31 39 31 2c 20 20 31 39 34 2c 20 20 20 39 34 2c  191,  194,   94,
2d5f8 20 20 32 34 30 2c 20 20 31 35 30 2c 20 20 20 38    240,  150,   8
2d5f9 36 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 35  6,   74,. /*   5
2d5fa 39 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 20 39  90 */    94,   9
2d5fb 35 2c 20 20 31 35 30 2c 20 20 20 32 38 2c 20 20  5,  150,   28,  
2d5fc 31 36 35 2c 20 20 32 33 36 2c 20 20 32 30 36 2c  165,  236,  206,
2d5fd 20 20 32 30 37 2c 20 20 20 32 33 2c 20 20 31 35    207,   23,  15
2d5fe 30 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20  0,. /*   600 */ 
2d5ff 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 31 36     48,   49,  16
2d600 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
2d601 20 34 38 2c 20 20 20 34 39 2c 20 20 31 36 35 2c   48,   49,  165,
2d602 20 20 20 34 33 2c 20 20 20 33 31 2c 0a 20 2f 2a     43,   31,. /*
2d603 20 20 20 36 31 30 20 2a 2f 20 20 20 20 34 35 2c     610 */    45,
2d604 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32    174,  175,   2
2d605 31 2c 20 20 31 36 35 2c 20 20 31 31 33 2c 20 20  1,  165,  113,  
2d606 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34 30 2c  174,  175,   40,
2d607 20 20 31 31 37 2c 0a 20 2f 2a 20 20 20 36 32 30    117,. /*   620
2d608 20 2a 2f 20 20 20 20 35 35 2c 20 20 20 36 39 2c   */    55,   69,
2d609 20 20 20 37 30 2c 20 20 20 36 36 2c 20 20 20 36     70,   66,   6
2d60a 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
2d60b 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2d60c 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20 20  . /*   630 */   
2d60d 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
2d60e 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
2d60f 38 2c 20 20 31 35 30 2c 20 20 20 38 30 2c 20 20  8,  150,   80,  
2d610 20 38 31 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20   81,   82,. /*  
2d611 20 36 34 30 20 2a 2f 20 20 20 20 38 33 2c 20 20   640 */    83,  
2d612 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
2d613 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
2d614 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20  9,   90,   19,  
2d615 31 35 30 2c 0a 20 2f 2a 20 20 20 36 35 30 20 2a  150,. /*   650 *
2d616 2f 20 20 20 20 39 38 2c 20 20 31 36 35 2c 20 20  /    98,  165,  
2d617 20 32 33 2c 20 20 20 36 31 2c 20 20 20 32 33 2c   23,   61,   23,
2d618 20 20 31 35 30 2c 20 20 20 32 35 2c 20 20 31 35    150,   25,  15
2d619 30 2c 20 20 20 31 39 2c 20 20 31 31 33 2c 0a 20  0,   19,  113,. 
2d61a 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20 20 31 37  /*   660 */   17
2d61b 34 2c 20 20 31 37 35 2c 20 20 32 31 33 2c 20 20  4,  175,  213,  
2d61c 31 31 37 2c 20 20 31 36 35 2c 20 20 20 32 34 2c  117,  165,   24,
2d61d 20 20 31 35 33 2c 20 20 20 32 36 2c 20 20 20 32    153,   26,   2
2d61e 33 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 36  3,  150,. /*   6
2d61f 37 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 35  70 */   165,  15
2d620 30 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20  0,  165,  174,  
2d621 31 37 35 2c 20 20 20 38 39 2c 20 20 20 39 30 2c  175,   89,   90,
2d622 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 31 37     48,   49,  17
2d623 34 2c 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f 20  4,. /*   680 */ 
2d624 20 20 31 37 35 2c 20 20 31 37 34 2c 20 20 31 37    175,  174,  17
2d625 35 2c 20 20 20 31 39 2c 20 20 31 36 35 2c 20 20  5,   19,  165,  
2d626 32 33 37 2c 20 20 31 36 35 2c 20 20 31 31 32 2c  237,  165,  112,
2d627 20 20 20 34 39 2c 20 20 31 31 34 2c 0a 20 2f 2a     49,  114,. /*
2d628 20 20 20 36 39 30 20 2a 2f 20 20 20 31 31 35 2c     690 */   115,
2d629 20 20 20 32 37 2c 20 20 31 30 30 2c 20 20 31 37     27,  100,  17
2d62a 34 2c 20 20 31 37 35 2c 20 20 20 36 36 2c 20 20  4,  175,   66,  
2d62b 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2d62c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 37 30 30     70,. /*   700
2d62d 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32 2c   */    71,   72,
2d62e 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2d62f 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
2d630 20 37 38 2c 20 20 31 35 30 2c 20 20 20 38 30 2c   78,  150,   80,
2d631 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20 20  . /*   710 */   
2d632 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2d633 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2d634 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
2d635 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20   89,   90,. /*  
2d636 20 37 32 30 20 2a 2f 20 20 20 20 31 39 2c 20 20   720 */    19,  
2d637 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  150,  150,  165,
2d638 20 20 31 35 30 2c 20 20 20 32 34 2c 20 20 31 31    150,   24,  11
2d639 32 2c 20 20 31 35 30 2c 20 20 31 31 34 2c 20 20  2,  150,  114,  
2d63a 31 31 35 2c 0a 20 2f 2a 20 20 20 37 33 30 20 2a  115,. /*   730 *
2d63b 2f 20 20 20 31 33 38 2c 20 20 20 31 39 2c 20 20  /   138,   19,  
2d63c 31 37 34 2c 20 20 31 37 35 2c 20 20 32 31 33 2c  174,  175,  213,
2d63d 20 20 20 39 34 2c 20 20 31 36 35 2c 20 20 31 36     94,  165,  16
2d63e 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 0a 20  5,  150,  165,. 
2d63f 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20 20 20 32  /*   740 */    2
2d640 39 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  9,  150,  165,  
2d641 31 30 34 2c 20 20 20 33 33 2c 20 20 31 37 34 2c  104,   33,  174,
2d642 20 20 31 37 35 2c 20 20 31 39 36 2c 20 20 31 30    175,  196,  10
2d643 39 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20 37  9,   48,. /*   7
2d644 35 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 31 37  50 */    49,  17
2d645 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20  4,  175,  165,  
2d646 31 39 30 2c 20 20 31 39 31 2c 20 20 31 36 35 2c  190,  191,  165,
2d647 20 20 31 31 32 2c 20 20 20 34 37 2c 20 20 31 31    112,   47,  11
2d648 34 2c 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f 20  4,. /*   760 */ 
2d649 20 20 31 31 35 2c 20 20 31 38 37 2c 20 20 31 37    115,  187,  17
2d64a 34 2c 20 20 31 37 35 2c 20 20 31 36 30 2c 20 20  4,  175,  160,  
2d64b 31 37 34 2c 20 20 31 37 35 2c 20 20 20 36 36 2c  174,  175,   66,
2d64c 20 20 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f 2a     67,   68,. /*
2d64d 20 20 20 37 37 30 20 2a 2f 20 20 20 20 36 39 2c     770 */    69,
2d64e 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2d64f 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
2d650 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
2d651 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20 37 38 30     78,. /*   780
2d652 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 38 30 2c   */   150,   80,
2d653 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2d654 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
2d655 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
2d656 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20 20  . /*   790 */   
2d657 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c   89,   90,   19,
2d658 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36    150,  150,  16
2d659 35 2c 20 20 31 39 38 2c 20 20 20 31 32 2c 20 20  5,  198,   12,  
2d65a 31 39 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  194,  150,. /*  
2d65b 20 38 30 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   800 */   150,  
2d65c 31 35 30 2c 20 20 32 34 38 2c 20 20 32 34 39 2c  150,  248,  249,
2d65d 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 30    174,  175,  20
2d65e 36 2c 20 20 32 30 37 2c 20 20 31 36 35 2c 20 20  6,  207,  165,  
2d65f 31 36 35 2c 0a 20 2f 2a 20 20 20 38 31 30 20 2a  165,. /*   810 *
2d660 2f 20 20 20 20 39 38 2c 20 20 20 32 33 2c 20 20  /    98,   23,  
2d661 31 35 30 2c 20 20 20 32 38 2c 20 20 31 36 35 2c  150,   28,  165,
2d662 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 37    165,  165,  17
2d663 34 2c 20 20 31 37 35 2c 20 20 31 36 36 2c 0a 20  4,  175,  166,. 
2d664 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20 20 31 35  /*   820 */   15
2d665 30 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20  0,   48,   49,  
2d666 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c  174,  175,  174,
2d667 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 20 34    175,  165,   4
2d668 33 2c 20 20 32 33 33 2c 0a 20 2f 2a 20 20 20 38  3,  233,. /*   8
2d669 33 30 20 2a 2f 20 20 20 20 34 35 2c 20 20 20 32  30 */    45,   2
2d66a 32 2c 20 20 20 32 33 2c 20 20 31 37 37 2c 20 20  2,   23,  177,  
2d66b 20 32 35 2c 20 20 31 36 35 2c 20 20 31 37 34 2c   25,  165,  174,
2d66c 20 20 31 37 35 2c 20 20 32 33 33 2c 20 20 20 36    175,  233,   6
2d66d 36 2c 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f 20  6,. /*   840 */ 
2d66e 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36     67,   68,   6
2d66f 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
2d670 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
2d671 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a     75,   76,. /*
2d672 20 20 20 38 35 30 20 2a 2f 20 20 20 20 37 37 2c     850 */    77,
2d673 20 20 20 37 38 2c 20 20 31 35 30 2c 20 20 20 38     78,  150,   8
2d674 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20  0,   81,   82,  
2d675 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
2d676 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 38 36 30     86,. /*   860
2d677 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c   */    87,   88,
2d678 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31     89,   90,   1
2d679 39 2c 20 20 31 35 30 2c 20 20 20 39 37 2c 20 20  9,  150,   97,  
2d67a 31 36 35 2c 20 20 20 32 35 2c 20 20 31 35 30 2c  165,   25,  150,
2d67b 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20 20  . /*   870 */   
2d67c 31 36 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  160,  150,  150,
2d67d 20 20 31 35 30 2c 20 20 20 36 35 2c 20 20 32 32    150,   65,  22
2d67e 38 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  8,  174,  175,  
2d67f 31 34 34 2c 20 20 31 34 35 2c 0a 20 2f 2a 20 20  144,  145,. /*  
2d680 20 38 38 30 20 2a 2f 20 20 20 31 36 35 2c 20 20   880 */   165,  
2d681 32 34 36 2c 20 20 32 34 37 2c 20 20 31 35 30 2c  246,  247,  150,
2d682 20 20 31 36 35 2c 20 20 31 31 36 2c 20 20 31 36    165,  116,  16
2d683 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
2d684 31 37 34 2c 0a 20 2f 2a 20 20 20 38 39 30 20 2a  174,. /*   890 *
2d685 2f 20 20 20 31 37 35 2c 20 20 31 32 39 2c 20 20  /   175,  129,  
2d686 31 33 30 2c 20 20 20 34 38 2c 20 20 20 34 39 2c  130,   48,   49,
2d687 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 32    174,  175,  12
2d688 38 2c 20 20 31 36 35 2c 20 20 20 39 38 2c 0a 20  8,  165,   98,. 
2d689 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20 20 31 31  /*   900 */   11
2d68a 32 2c 20 20 31 37 37 2c 20 20 31 31 34 2c 20 20  2,  177,  114,  
2d68b 31 31 35 2c 20 20 31 39 34 2c 20 20 20 34 39 2c  115,  194,   49,
2d68c 20 20 31 38 37 2c 20 20 31 37 34 2c 20 20 31 37    187,  174,  17
2d68d 35 2c 20 20 31 38 37 2c 0a 20 2f 2a 20 20 20 39  5,  187,. /*   9
2d68e 31 30 20 2a 2f 20 20 20 31 30 39 2c 20 20 20 36  10 */   109,   6
2d68f 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  6,   67,   68,  
2d690 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
2d691 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
2d692 34 2c 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f 20  4,. /*   920 */ 
2d693 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
2d694 37 2c 20 20 20 37 38 2c 20 20 31 35 30 2c 20 20  7,   78,  150,  
2d695 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
2d696 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a     83,   84,. /*
2d697 20 20 20 39 33 30 20 2a 2f 20 20 20 20 38 35 2c     930 */    85,
2d698 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
2d699 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
2d69a 20 31 39 2c 20 20 31 35 30 2c 20 20 20 32 33 2c   19,  150,   23,
2d69b 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 39 34 30    165,. /*   940
2d69c 20 2a 2f 20 20 20 20 32 35 2c 20 20 31 35 30 2c   */    25,  150,
2d69d 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35    150,  150,  15
2d69e 30 2c 20 20 31 36 36 2c 20 20 31 36 37 2c 20 20  0,  166,  167,  
2d69f 31 36 38 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  168,  174,  175,
2d6a0 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20 20  . /*   950 */   
2d6a1 32 30 39 2c 20 20 20 32 35 2c 20 20 31 36 35 2c  209,   25,  165,
2d6a2 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 31 35      7,    8,  15
2d6a3 30 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  0,  165,  165,  
2d6a4 31 36 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20  165,  165,. /*  
2d6a5 20 39 36 30 20 2a 2f 20 20 20 31 30 34 2c 20 20   960 */   104,  
2d6a6 31 37 34 2c 20 20 31 37 35 2c 20 20 20 39 37 2c  174,  175,   97,
2d6a7 20 20 20 39 38 2c 20 20 20 34 38 2c 20 20 20 34     98,   48,   4
2d6a8 39 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  9,  174,  175,  
2d6a9 31 32 36 2c 0a 20 2f 2a 20 20 20 39 37 30 20 2a  126,. /*   970 *
2d6aa 2f 20 20 20 31 36 35 2c 20 20 20 32 32 2c 20 20  /   165,   22,  
2d6ab 20 32 33 2c 20 20 31 37 37 2c 20 20 20 32 35 2c   23,  177,   25,
2d6ac 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20 20      7,    8,    
2d6ad 39 2c 20 20 31 38 37 2c 20 20 31 37 34 2c 0a 20  9,  187,  174,. 
2d6ae 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20 20 31 37  /*   980 */   17
2d6af 35 2c 20 20 31 38 37 2c 20 20 31 36 30 2c 20 20  5,  187,  160,  
2d6b0 31 37 37 2c 20 20 20 36 37 2c 20 20 20 36 38 2c  177,   67,   68,
2d6b1 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
2d6b2 31 2c 20 20 20 37 32 2c 0a 20 2f 2a 20 20 20 39  1,   72,. /*   9
2d6b3 39 30 20 2a 2f 20 20 20 20 37 33 2c 20 20 20 37  90 */    73,   7
2d6b4 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
2d6b5 20 37 37 2c 20 20 20 37 38 2c 20 20 31 35 30 2c   77,   78,  150,
2d6b6 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38     80,   81,   8
2d6b7 32 2c 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f 20  2,. /*  1000 */ 
2d6b8 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
2d6b9 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
2d6ba 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
2d6bb 20 20 20 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a     19,  150,. /*
2d6bc 20 20 31 30 31 30 20 2a 2f 20 20 20 31 36 30 2c    1010 */   160,
2d6bd 20 20 31 36 35 2c 20 20 31 38 32 2c 20 20 31 36    165,  182,  16
2d6be 36 2c 20 20 20 36 35 2c 20 20 31 35 30 2c 20 20  6,   65,  150,  
2d6bf 31 39 34 2c 20 20 31 30 35 2c 20 20 31 30 36 2c  194,  105,  106,
2d6c0 20 20 31 30 37 2c 0a 20 2f 2a 20 20 31 30 32 30    107,. /*  1020
2d6c1 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35 2c   */   174,  175,
2d6c2 20 20 31 37 38 2c 20 20 20 32 33 2c 20 20 31 36    178,   23,  16
2d6c3 35 2c 20 20 20 32 35 2c 20 20 31 37 37 2c 20 20  5,   25,  177,  
2d6c4 31 35 30 2c 20 20 31 35 30 2c 20 20 31 30 33 2c  150,  150,  103,
2d6c5 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20 20  . /*  1030 */   
2d6c6 31 36 35 2c 20 20 31 35 30 2c 20 20 31 39 39 2c  165,  150,  199,
2d6c7 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 35    174,  175,  15
2d6c8 30 2c 20 20 31 36 36 2c 20 20 20 34 38 2c 20 20  0,  166,   48,  
2d6c9 20 34 39 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20   49,  174,. /*  
2d6ca 31 30 34 30 20 2a 2f 20 20 20 31 37 35 2c 20 20  1040 */   175,  
2d6cb 32 30 39 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  209,  165,  165,
2d6cc 20 20 31 39 34 2c 20 20 20 32 33 2c 20 20 31 36    194,   23,  16
2d6cd 35 2c 20 20 20 32 35 2c 20 20 32 30 39 2c 20 20  5,   25,  209,  
2d6ce 20 20 36 2c 0a 20 2f 2a 20 20 31 30 35 30 20 2a    6,. /*  1050 *
2d6cf 2f 20 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20  /   165,  174,  
2d6d0 31 37 35 2c 20 20 31 39 39 2c 20 20 31 34 39 2c  175,  199,  149,
2d6d1 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 36    174,  175,   6
2d6d2 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20  8,   69,   70,. 
2d6d3 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20 20 20 37  /*  1060 */    7
2d6d4 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
2d6d5 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
2d6d6 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 32 31     77,   78,  21
2d6d7 38 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 31 30  8,   80,. /*  10
2d6d8 37 30 20 2a 2f 20 20 20 20 38 31 2c 20 20 20 38  70 */    81,   8
2d6d9 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
2d6da 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
2d6db 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
2d6dc 30 2c 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f 20  0,. /*  1080 */ 
2d6dd 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 20 31     19,   20,   1
2d6de 36 2c 20 20 20 32 32 2c 20 20 31 35 30 2c 20 20  6,   22,  150,  
2d6df 20 31 36 2c 20 20 31 35 30 2c 20 20 20 32 36 2c   16,  150,   26,
2d6e0 20 20 20 32 37 2c 20 20 31 34 39 2c 0a 20 2f 2a     27,  149,. /*
2d6e1 20 20 31 30 39 30 20 2a 2f 20 20 20 32 34 30 2c    1090 */   240,
2d6e2 20 20 32 31 33 2c 20 20 31 35 30 2c 20 20 31 34    213,  150,  14
2d6e3 39 2c 20 20 31 34 39 2c 20 20 20 31 39 2c 20 20  9,  149,   19,  
2d6e4 20 32 30 2c 20 20 20 33 36 2c 20 20 20 32 32 2c   20,   36,   22,
2d6e5 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 31 30 30    165,. /*  1100
2d6e6 20 2a 2f 20 20 20 20 31 33 2c 20 20 31 36 35 2c   */    13,  165,
2d6e7 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 35     26,   27,  15
2d6e8 31 2c 20 20 31 35 31 2c 20 20 31 35 30 2c 20 20  1,  151,  150,  
2d6e9 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  165,  174,  175,
2d6ea 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20 20  . /*  1110 */   
2d6eb 31 37 34 2c 20 20 31 37 35 2c 20 20 20 33 36 2c  174,  175,   36,
2d6ec 20 20 31 35 30 2c 20 20 20 32 35 2c 20 20 20 35    150,   25,   5
2d6ed 34 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  4,  150,  150,  
2d6ee 31 35 39 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  159,  150,. /*  
2d6ef 31 31 32 30 20 2a 2f 20 20 20 20 32 33 2c 20 20  1120 */    23,  
2d6f0 31 36 35 2c 20 20 20 32 35 2c 20 20 31 39 34 2c  165,   25,  194,
2d6f1 20 20 20 35 38 2c 20 20 20 36 34 2c 20 20 20 36     58,   64,   6
2d6f2 30 2c 20 20 20 35 38 2c 20 20 31 36 35 2c 20 20  0,   58,  165,  
2d6f3 20 36 30 2c 0a 20 2f 2a 20 20 31 31 33 30 20 2a   60,. /*  1130 *
2d6f4 2f 20 20 20 20 35 34 2c 20 20 31 36 35 2c 20 20  /    54,  165,  
2d6f5 31 36 35 2c 20 20 31 32 36 2c 20 20 31 36 35 2c  165,  126,  165,
2d6f6 20 20 31 39 33 2c 20 20 31 35 30 2c 20 20 31 37    193,  150,  17
2d6f7 34 2c 20 20 31 37 35 2c 20 20 31 32 33 2c 0a 20  4,  175,  123,. 
2d6f8 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20 20 20 36  /*  1140 */    6
2d6f9 34 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  4,  174,  175,  
2d6fa 31 37 34 2c 20 20 31 37 35 2c 20 20 20 38 34 2c  174,  175,   84,
2d6fb 20 20 20 38 35 2c 20 20 31 39 39 2c 20 20 31 35     85,  199,  15
2d6fc 30 2c 20 20 31 39 33 2c 0a 20 2f 2a 20 20 31 31  0,  193,. /*  11
2d6fd 35 30 20 2a 2f 20 20 20 32 30 30 2c 20 20 31 36  50 */   200,  16
2d6fe 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  5,  150,  150,  
2d6ff 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c   93,   94,   95,
2d700 20 20 31 32 34 2c 20 20 32 30 31 2c 20 20 20 39    124,  201,   9
2d701 38 2c 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f 20  8,. /*  1160 */ 
2d702 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2d703 36 2c 20 20 31 36 35 2c 20 20 31 30 35 2c 20 20  6,  165,  105,  
2d704 31 30 36 2c 20 20 31 30 37 2c 20 20 31 36 35 2c  106,  107,  165,
2d705 20 20 31 36 35 2c 20 20 20 39 33 2c 0a 20 2f 2a    165,   93,. /*
2d706 20 20 31 31 37 30 20 2a 2f 20 20 20 20 39 34 2c    1170 */    94,
2d707 20 20 20 39 35 2c 20 20 31 37 34 2c 20 20 31 37     95,  174,  17
2d708 35 2c 20 20 20 39 38 2c 20 20 20 20 35 2c 20 20  5,   98,    5,  
2d709 20 32 33 2c 20 20 31 31 36 2c 20 20 20 32 35 2c   23,  116,   25,
2d70a 20 20 31 39 33 2c 0a 20 2f 2a 20 20 31 31 38 30    193,. /*  1180
2d70b 20 2a 2f 20 20 20 20 31 30 2c 20 20 20 31 31 2c   */    10,   11,
2d70c 20 20 20 31 32 2c 20 20 20 31 33 2c 20 20 20 31     12,   13,   1
2d70d 34 2c 20 20 31 32 32 2c 20 20 31 35 30 2c 20 20  4,  122,  150,  
2d70e 20 31 37 2c 20 20 32 30 33 2c 20 20 32 30 32 2c   17,  203,  202,
2d70f 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20 20  . /*  1190 */   
2d710 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c  129,  130,  131,
2d711 20 20 31 33 32 2c 20 20 31 33 33 2c 20 20 31 33    132,  133,  13
2d712 34 2c 20 20 31 39 33 2c 20 20 31 35 30 2c 20 20  4,  193,  150,  
2d713 31 32 35 2c 20 20 31 33 35 2c 0a 20 2f 2a 20 20  125,  135,. /*  
2d714 31 32 30 30 20 2a 2f 20 20 20 20 33 30 2c 20 20  1200 */    30,  
2d715 31 36 35 2c 20 20 20 33 32 2c 20 20 31 35 30 2c  165,   32,  150,
2d716 20 20 31 33 38 2c 20 20 31 32 39 2c 20 20 31 33    138,  129,  13
2d717 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c 20 20  0,  131,  132,  
2d718 31 33 33 2c 0a 20 2f 2a 20 20 31 32 31 30 20 2a  133,. /*  1210 *
2d719 2f 20 20 20 31 33 34 2c 20 20 20 34 31 2c 20 20  /   134,   41,  
2d71a 31 36 35 2c 20 20 20 31 39 2c 20 20 20 32 30 2c  165,   19,   20,
2d71b 20 20 32 32 37 2c 20 20 20 32 32 2c 20 20 31 31    227,   22,  11
2d71c 38 2c 20 20 31 36 35 2c 20 20 31 35 37 2c 0a 20  8,  165,  157,. 
2d71d 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20 20 20 32  /*  1220 */    2
2d71e 36 2c 20 20 20 32 37 2c 20 20 31 35 30 2c 20 20  6,   27,  150,  
2d71f 20 35 33 2c 20 20 31 35 30 2c 20 20 20 35 35 2c   53,  150,   55,
2d720 20 20 31 30 34 2c 20 20 31 37 34 2c 20 20 31 37    104,  174,  17
2d721 35 2c 20 20 20 35 39 2c 0a 20 2f 2a 20 20 31 32  5,   59,. /*  12
2d722 33 30 20 2a 2f 20 20 20 20 33 36 2c 20 20 20 32  30 */    36,   2
2d723 32 2c 20 20 20 36 32 2c 20 20 32 31 30 2c 20 20  2,   62,  210,  
2d724 31 35 30 2c 20 20 20 32 36 2c 20 20 20 32 37 2c  150,   26,   27,
2d725 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36    165,  150,  16
2d726 35 2c 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f 20  5,. /*  1240 */ 
2d727 20 20 31 39 33 2c 20 20 31 35 30 2c 20 20 31 35    193,  150,  15
2d728 30 2c 20 20 31 35 37 2c 20 20 31 32 31 2c 20 20  0,  157,  121,  
2d729 32 31 31 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  211,  174,  175,
2d72a 20 20 20 35 34 2c 20 20 31 36 35 2c 0a 20 2f 2a     54,  165,. /*
2d72b 20 20 31 32 35 30 20 2a 2f 20 20 20 31 35 30 2c    1250 */   150,
2d72c 20 20 32 31 30 2c 20 20 32 31 30 2c 20 20 31 36    210,  210,  16
2d72d 35 2c 20 20 31 35 30 2c 20 20 32 31 31 2c 20 20  5,  150,  211,  
2d72e 31 36 35 2c 20 20 31 36 35 2c 20 20 20 36 34 2c  165,  165,   64,
2d72f 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 32 36 30    150,. /*  1260
2d730 20 2a 2f 20 20 20 32 31 31 2c 20 20 31 30 34 2c   */   211,  104,
2d731 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32    174,  175,   2
2d732 33 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20 20  3,  165,   25,  
2d733 31 39 33 2c 20 20 20 34 36 2c 20 20 31 36 35 2c  193,   46,  165,
2d734 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20 20  . /*  1270 */   
2d735 20 32 33 2c 20 20 31 37 36 2c 20 20 20 32 35 2c   23,  176,   25,
2d736 20 20 20 36 34 2c 20 20 31 36 35 2c 20 20 31 30     64,  165,  10
2d737 35 2c 20 20 31 30 36 2c 20 20 31 30 37 2c 20 20  5,  106,  107,  
2d738 20 38 34 2c 20 20 20 38 35 2c 0a 20 2f 2a 20 20   84,   85,. /*  
2d739 31 32 38 30 20 2a 2f 20 20 20 31 35 30 2c 20 20  1280 */   150,  
2d73a 31 31 31 2c 20 20 31 37 36 2c 20 20 31 37 34 2c  111,  176,  174,
2d73b 20 20 31 37 35 2c 20 20 31 39 33 2c 20 20 31 31    175,  193,  11
2d73c 36 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20  6,   93,   94,  
2d73d 20 39 35 2c 0a 20 2f 2a 20 20 31 32 39 30 20 2a   95,. /*  1290 *
2d73e 2f 20 20 20 31 30 33 2c 20 20 31 35 30 2c 20 20  /   103,  150,  
2d73f 20 39 38 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   98,   84,   85,
2d740 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 39    165,  150,  19
2d741 33 2c 20 20 31 38 34 2c 20 20 31 35 30 2c 0a 20  3,  184,  150,. 
2d742 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20 20 31 35  /*  1300 */   15
2d743 30 2c 20 20 31 37 36 2c 20 20 31 35 30 2c 20 20  0,  176,  150,  
2d744 20 39 34 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   94,  150,  150,
2d745 20 20 31 36 35 2c 20 20 20 39 38 2c 20 20 20 32    165,   98,   2
2d746 33 2c 20 20 31 33 39 2c 0a 20 2f 2a 20 20 31 33  3,  139,. /*  13
2d747 31 30 20 2a 2f 20 20 20 20 32 35 2c 20 20 31 36  10 */    25,  16
2d748 35 2c 20 20 31 37 38 2c 20 20 31 37 36 2c 20 20  5,  178,  176,  
2d749 31 36 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c  165,  165,  150,
2d74a 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36    165,  150,  16
2d74b 35 2c 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f 20  5,. /*  1320 */ 
2d74c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 35    165,  150,  15
2d74d 30 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  0,  129,  130,  
2d74e 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c  131,  132,  133,
2d74f 20 20 31 33 34 2c 20 20 20 32 32 2c 0a 20 2f 2a    134,   22,. /*
2d750 20 20 31 33 33 30 20 2a 2f 20 20 20 31 35 30 2c    1330 */   150,
2d751 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36    165,  150,  16
2d752 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  5,  150,  150,  
2d753 31 36 35 2c 20 20 31 36 35 2c 20 20 31 32 39 2c  165,  165,  129,
2d754 20 20 31 33 30 2c 0a 20 2f 2a 20 20 31 33 34 30    130,. /*  1340
2d755 20 2a 2f 20 20 20 31 33 31 2c 20 20 31 35 30 2c   */   131,  150,
2d756 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 37    150,  150,  17
2d757 39 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  9,  165,  150,  
2d758 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  165,  150,  165,
2d759 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20 20 20  . /*  1350 */   
2d75a 31 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  165,  150,  150,
2d75b 20 20 31 35 30 2c 20 20 20 39 30 2c 20 20 31 37    150,   90,  17
2d75c 36 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  6,  165,  165,  
2d75d 31 36 35 2c 20 20 32 33 30 2c 0a 20 2f 2a 20 20  165,  230,. /*  
2d75e 31 33 36 30 20 2a 2f 20 20 20 20 32 33 2c 20 20  1360 */    23,  
2d75f 31 36 35 2c 20 20 20 32 35 2c 20 20 31 36 35 2c  165,   25,  165,
2d760 20 20 31 37 36 2c 20 20 32 33 30 2c 20 20 31 36    176,  230,  16
2d761 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
2d762 31 37 39 2c 0a 20 2f 2a 20 20 31 33 37 30 20 2a  179,. /*  1370 *
2d763 2f 20 20 20 31 38 34 2c 20 20 31 37 36 2c 20 20  /   184,  176,  
2d764 20 31 38 2c 20 20 31 35 36 2c 20 20 20 34 34 2c   18,  156,   44,
2d765 20 20 31 35 37 2c 20 20 31 35 36 2c 20 20 32 33    157,  156,  23
2d766 38 2c 20 20 31 35 37 2c 20 20 31 35 36 2c 0a 20  8,  157,  156,. 
2d767 2f 2a 20 20 31 33 38 30 20 2a 2f 20 20 20 31 33  /*  1380 */   13
2d768 35 2c 20 20 31 35 37 2c 20 20 31 35 37 2c 20 20  5,  157,  157,  
2d769 32 33 39 2c 20 20 31 35 36 2c 20 20 20 36 36 2c  239,  156,   66,
2d76a 20 20 31 35 37 2c 20 20 20 32 32 2c 20 20 31 38    157,   22,  18
2d76b 39 2c 20 20 31 38 39 2c 0a 20 2f 2a 20 20 31 33  9,  189,. /*  13
2d76c 39 30 20 2a 2f 20 20 20 31 35 37 2c 20 20 20 31  90 */   157,   1
2d76d 38 2c 20 20 32 31 39 2c 20 20 32 31 39 2c 20 20  8,  219,  219,  
2d76e 31 35 37 2c 20 20 20 33 39 2c 20 20 31 39 39 2c  157,   39,  199,
2d76f 20 20 31 39 32 2c 20 20 31 39 32 2c 20 20 31 39    192,  192,  19
2d770 32 2c 0a 20 2f 2a 20 20 31 34 30 30 20 2a 2f 20  2,. /*  1400 */ 
2d771 20 20 31 39 32 2c 20 20 31 38 39 2c 20 20 32 34    192,  189,  24
2d772 31 2c 20 20 31 39 39 2c 20 20 32 34 31 2c 20 20  1,  199,  241,  
2d773 31 35 37 2c 20 20 31 35 37 2c 20 20 20 33 37 2c  157,  157,   37,
2d774 20 20 32 34 34 2c 20 20 32 34 37 2c 0a 20 2f 2a    244,  247,. /*
2d775 20 20 31 34 31 30 20 2a 2f 20 20 20 31 36 34 2c    1410 */   164,
2d776 20 20 31 38 30 2c 20 20 31 38 30 2c 20 20 20 20    180,  180,    
2d777 31 2c 20 20 20 31 35 2c 20 20 20 32 33 2c 20 20  1,   15,   23,  
2d778 20 32 32 2c 20 20 32 35 30 2c 20 20 31 31 38 2c   22,  250,  118,
2d779 20 20 31 31 38 2c 0a 20 2f 2a 20 20 31 34 32 30    118,. /*  1420
2d77a 20 2a 2f 20 20 20 31 31 38 2c 20 20 31 31 38 2c   */   118,  118,
2d77b 20 20 31 31 38 2c 20 20 20 39 38 2c 20 20 31 31    118,   98,  11
2d77c 33 2c 20 20 20 32 32 2c 20 20 20 31 31 2c 20 20  3,   22,   11,  
2d77d 20 32 33 2c 20 20 20 32 33 2c 20 20 20 32 32 2c   23,   23,   22,
2d77e 0a 20 2f 2a 20 20 31 34 33 30 20 2a 2f 20 20 20  . /*  1430 */   
2d77f 20 32 32 2c 20 20 20 32 35 2c 20 20 20 32 33 2c   22,   25,   23,
2d780 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20 20 33     23,   23,   3
2d781 34 2c 20 20 20 33 34 2c 20 20 31 32 30 2c 20 20  4,   34,  120,  
2d782 20 32 35 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20   25,   25,. /*  
2d783 31 34 34 30 20 2a 2f 20 20 20 20 32 32 2c 20 20  1440 */    22,  
2d784 31 31 38 2c 20 20 20 32 33 2c 20 20 20 32 33 2c  118,   23,   23,
2d785 20 20 20 32 37 2c 20 20 20 35 30 2c 20 20 20 32     27,   50,   2
2d786 32 2c 20 20 20 35 30 2c 20 20 20 32 32 2c 20 20  2,   50,   22,  
2d787 20 32 32 2c 0a 20 2f 2a 20 20 31 34 35 30 20 2a   22,. /*  1450 *
2d788 2f 20 20 20 20 33 34 2c 20 20 20 32 33 2c 20 20  /    34,   23,  
2d789 20 32 32 2c 20 20 20 32 32 2c 20 20 31 30 32 2c   22,   22,  102,
2d78a 20 20 31 30 39 2c 20 20 20 31 39 2c 20 20 20 32    109,   19,   2
2d78b 34 2c 20 20 20 32 30 2c 20 20 20 33 38 2c 0a 20  4,   20,   38,. 
2d78c 2f 2a 20 20 31 34 36 30 20 2a 2f 20 20 20 20 32  /*  1460 */    2
2d78d 35 2c 20 20 31 30 34 2c 20 20 31 33 38 2c 20 20  5,  104,  138,  
2d78e 31 30 34 2c 20 20 20 32 32 2c 20 20 20 34 32 2c  104,   22,   42,
2d78f 20 20 20 20 35 2c 20 20 20 20 31 2c 20 20 31 30      5,    1,  10
2d790 38 2c 20 20 31 32 37 2c 0a 20 2f 2a 20 20 31 34  8,  127,. /*  14
2d791 37 30 20 2a 2f 20 20 20 20 37 34 2c 20 20 20 32  70 */    74,   2
2d792 32 2c 20 20 20 35 30 2c 20 20 20 20 31 2c 20 20  2,   50,    1,  
2d793 20 37 34 2c 20 20 20 31 36 2c 20 20 31 32 31 2c   74,   16,  121,
2d794 20 20 31 31 39 2c 20 20 20 32 30 2c 20 20 31 30    119,   20,  10
2d795 38 2c 0a 20 2f 2a 20 20 31 34 38 30 20 2a 2f 20  8,. /*  1480 */ 
2d796 20 20 20 35 31 2c 20 20 31 31 39 2c 20 20 20 35     51,  119,   5
2d797 37 2c 20 20 20 35 31 2c 20 20 20 32 32 2c 20 20  7,   51,   22,  
2d798 20 31 36 2c 20 20 20 32 33 2c 20 20 20 32 33 2c   16,   23,   23,
2d799 20 20 31 32 37 2c 20 20 20 31 35 2c 0a 20 2f 2a    127,   15,. /*
2d79a 20 20 31 34 39 30 20 2a 2f 20 20 20 31 34 30 2c    1490 */   140,
2d79b 20 20 31 32 38 2c 20 20 20 32 32 2c 20 20 20 20    128,   22,    
2d79c 33 2c 20 20 32 35 31 2c 20 20 20 20 34 2c 20 20  3,  251,    4,  
2d79d 32 35 31 2c 20 20 20 36 33 2c 0a 7d 3b 0a 23 64  251,   63,.};.#d
2d79e 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 55  efine YY_SHIFT_U
2d79f 53 45 5f 44 46 4c 54 20 28 2d 31 31 31 29 0a 23  SE_DFLT (-111).#
2d7a0 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f  define YY_SHIFT_
2d7a1 4d 41 58 20 34 30 36 0a 73 74 61 74 69 63 20 63  MAX 406.static c
2d7a2 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 73 68  onst short yy_sh
2d7a3 69 66 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20  ift_ofst[] = {. 
2d7a4 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 31 38  /*     0 */   18
2d7a5 37 2c 20 31 30 36 31 2c 20 31 31 37 30 2c 20 31  7, 1061, 1170, 1
2d7a6 30 36 31 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  061, 1194, 1194,
2d7a7 20 20 20 2d 32 2c 20 20 20 36 34 2c 20 20 20 36     -2,   64,   6
2d7a8 34 2c 20 20 2d 31 39 2c 0a 20 2f 2a 20 20 20 20  4,  -19,. /*    
2d7a9 31 30 20 2a 2f 20 20 31 31 39 34 2c 20 31 31 39  10 */  1194, 119
2d7aa 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2d7ab 31 39 34 2c 20 20 32 37 36 2c 20 20 20 20 31 2c  194,  276,    1,
2d7ac 20 20 31 32 35 2c 20 31 30 37 36 2c 20 31 31 39    125, 1076, 119
2d7ad 34 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20  4,. /*    20 */ 
2d7ae 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2d7af 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2d7b0 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2d7b1 20 31 31 39 34 2c 20 31 31 39 34 2c 0a 20 2f 2a   1194, 1194,. /*
2d7b2 20 20 20 20 33 30 20 2a 2f 20 20 31 31 39 34 2c      30 */  1194,
2d7b3 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2d7b4 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2d7b5 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2d7b6 20 31 31 39 34 2c 0a 20 2f 2a 20 20 20 20 34 30   1194,. /*    40
2d7b7 20 2a 2f 20 20 31 31 39 34 2c 20 31 31 39 34 2c   */  1194, 1194,
2d7b8 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2d7b9 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2d7ba 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2d7bb 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 31  . /*    50 */  1
2d7bc 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2d7bd 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2d7be 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2d7bf 31 39 34 2c 20 20 2d 34 38 2c 0a 20 2f 2a 20 20  194,  -48,. /*  
2d7c0 20 20 36 30 20 2a 2f 20 20 20 34 30 39 2c 20 20    60 */   409,  
2d7c1 20 20 31 2c 20 20 20 20 31 2c 20 20 31 34 31 2c    1,    1,  141,
2d7c2 20 20 32 38 31 2c 20 20 32 38 31 2c 20 2d 31 31    281,  281, -11
2d7c3 30 2c 20 20 20 35 33 2c 20 20 31 39 37 2c 20 20  0,   53,  197,  
2d7c4 32 36 39 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a  269,. /*    70 *
2d7c5 2f 20 20 20 33 34 31 2c 20 20 34 31 33 2c 20 20  /   341,  413,  
2d7c6 34 38 35 2c 20 20 35 35 37 2c 20 20 36 32 39 2c  485,  557,  629,
2d7c7 20 20 37 30 31 2c 20 20 37 37 33 2c 20 20 38 34    701,  773,  84
2d7c8 35 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 0a 20  5,  773,  773,. 
2d7c9 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 37 37  /*    80 */   77
2d7ca 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20  3,  773,  773,  
2d7cb 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c  773,  773,  773,
2d7cc 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37    773,  773,  77
2d7cd 33 2c 20 20 37 37 33 2c 0a 20 2f 2a 20 20 20 20  3,  773,. /*    
2d7ce 39 30 20 2a 2f 20 20 20 37 37 33 2c 20 20 37 37  90 */   773,  77
2d7cf 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20  3,  773,  773,  
2d7d0 37 37 33 2c 20 20 37 37 33 2c 20 20 39 31 37 2c  773,  773,  917,
2d7d1 20 20 39 38 39 2c 20 20 39 38 39 2c 20 20 2d 36    989,  989,  -6
2d7d2 37 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20  7,. /*   100 */ 
2d7d3 20 20 2d 36 37 2c 20 20 20 2d 31 2c 20 20 20 2d    -67,   -1,   -
2d7d4 31 2c 20 20 20 35 35 2c 20 20 20 32 35 2c 20 20  1,   55,   25,  
2d7d5 33 37 39 2c 20 20 20 20 31 2c 20 20 20 20 31 2c  379,    1,    1,
2d7d6 20 20 20 20 31 2c 20 20 20 20 31 2c 0a 20 2f 2a      1,    1,. /*
2d7d7 20 20 20 31 31 30 20 2a 2f 20 20 20 20 20 31 2c     110 */     1,
2d7d8 20 20 36 33 39 2c 20 20 35 39 32 2c 20 20 20 20    639,  592,    
2d7d9 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2d7da 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2d7db 20 20 20 20 31 2c 0a 20 2f 2a 20 20 20 31 32 30      1,. /*   120
2d7dc 20 2a 2f 20 20 20 20 20 31 2c 20 20 20 20 31 2c   */     1,    1,
2d7dd 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2d7de 31 2c 20 20 20 20 31 2c 20 20 35 38 36 2c 20 20  1,    1,  586,  
2d7df 31 34 31 2c 20 20 2d 31 37 2c 20 2d 31 31 31 2c  141,  -17, -111,
2d7e0 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 2d  . /*   130 */  -
2d7e1 31 31 31 2c 20 2d 31 31 31 2c 20 31 32 30 39 2c  111, -111, 1209,
2d7e2 20 20 20 38 31 2c 20 20 33 37 36 2c 20 20 34 31     81,  376,  41
2d7e3 35 2c 20 20 34 32 36 2c 20 20 34 39 36 2c 20 20  5,  426,  496,  
2d7e4 20 39 30 2c 20 20 35 36 35 2c 0a 20 2f 2a 20 20   90,  565,. /*  
2d7e5 20 31 34 30 20 2a 2f 20 20 20 35 36 35 2c 20 20   140 */   565,  
2d7e6 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2d7e7 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2d7e8 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2d7e9 20 20 31 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a    1,. /*   150 *
2d7ea 2f 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  /     1,    1,  
2d7eb 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2d7ec 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2d7ed 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 0a 20  1,    1,    1,. 
2d7ee 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 20 20  /*   160 */     
2d7ef 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2d7f0 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2d7f1 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2d7f2 31 2c 20 20 20 20 31 2c 0a 20 2f 2a 20 20 20 31  1,    1,. /*   1
2d7f3 37 30 20 2a 2f 20 20 20 20 20 31 2c 20 20 20 20  70 */     1,    
2d7f4 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2d7f5 38 30 39 2c 20 20 39 34 39 2c 20 20 34 35 35 2c  809,  949,  455,
2d7f6 20 20 36 34 31 2c 20 20 36 34 31 2c 20 20 36 34    641,  641,  64
2d7f7 31 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20  1,. /*   180 */ 
2d7f8 20 20 37 36 39 2c 20 20 31 30 31 2c 20 2d 31 31    769,  101, -11
2d7f9 30 2c 20 2d 31 31 30 2c 20 2d 31 31 30 2c 20 2d  0, -110, -110, -
2d7fa 31 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c  111, -111, -111,
2d7fb 20 20 32 33 32 2c 20 20 32 33 32 2c 0a 20 2f 2a    232,  232,. /*
2d7fc 20 20 20 31 39 30 20 2a 2f 20 20 20 32 36 38 2c     190 */   268,
2d7fd 20 20 34 32 38 2c 20 20 32 31 33 2c 20 20 35 37    428,  213,  57
2d7fe 35 2c 20 20 36 34 35 2c 20 20 37 38 35 2c 20 20  5,  645,  785,  
2d7ff 37 38 38 2c 20 20 34 31 32 2c 20 20 39 36 38 2c  788,  412,  968,
2d800 20 20 35 30 32 2c 0a 20 2f 2a 20 20 20 32 30 30    502,. /*   200
2d801 20 2a 2f 20 20 20 34 39 31 2c 20 20 20 35 32 2c   */   491,   52,
2d802 20 20 31 38 33 2c 20 20 31 38 33 2c 20 20 31 38    183,  183,  18
2d803 33 2c 20 20 36 31 34 2c 20 20 36 31 34 2c 20 20  3,  614,  614,  
2d804 37 31 31 2c 20 20 39 31 32 2c 20 20 36 31 34 2c  711,  912,  614,
2d805 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20  . /*   210 */   
2d806 36 31 34 2c 20 20 36 31 34 2c 20 20 36 31 34 2c  614,  614,  614,
2d807 20 20 32 32 39 2c 20 20 35 34 36 2c 20 20 2d 31    229,  546,  -1
2d808 33 2c 20 20 31 34 31 2c 20 20 37 36 32 2c 20 20  3,  141,  762,  
2d809 37 36 32 2c 20 20 32 34 39 2c 0a 20 2f 2a 20 20  762,  249,. /*  
2d80a 20 32 32 30 20 2a 2f 20 20 20 35 37 38 2c 20 20   220 */   578,  
2d80b 35 37 38 2c 20 20 36 36 34 2c 20 20 35 37 38 2c  578,  664,  578,
2d80c 20 20 38 35 36 2c 20 20 35 37 38 2c 20 20 31 34    856,  578,  14
2d80d 31 2c 20 20 35 37 38 2c 20 20 31 34 31 2c 20 20  1,  578,  141,  
2d80e 39 32 36 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a  926,. /*   230 *
2d80f 2f 20 20 20 38 34 33 2c 20 20 36 36 34 2c 20 20  /   843,  664,  
2d810 36 36 34 2c 20 20 38 34 33 2c 20 31 30 34 33 2c  664,  843, 1043,
2d811 20 31 30 34 33 2c 20 31 30 34 33 2c 20 31 30 34   1043, 1043, 104
2d812 33 2c 20 31 30 38 37 2c 20 31 30 38 37 2c 0a 20  3, 1087, 1087,. 
2d813 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31 30 38  /*   240 */  108
2d814 39 2c 20 2d 31 31 30 2c 20 31 30 30 37 2c 20 31  9, -110, 1007, 1
2d815 30 31 36 2c 20 31 30 33 33 2c 20 31 30 36 33 2c  016, 1033, 1063,
2d816 20 31 30 37 33 2c 20 31 30 36 34 2c 20 31 30 39   1073, 1064, 109
2d817 39 2c 20 31 30 39 39 2c 0a 20 2f 2a 20 20 20 32  9, 1099,. /*   2
2d818 35 30 20 2a 2f 20 20 31 31 32 32 2c 20 31 31 32  50 */  1122, 112
2d819 33 2c 20 31 31 32 32 2c 20 31 31 32 33 2c 20 31  3, 1122, 1123, 1
2d81a 31 32 32 2c 20 31 31 32 33 2c 20 31 31 35 37 2c  122, 1123, 1157,
2d81b 20 31 31 35 37 2c 20 31 32 32 32 2c 20 31 31 35   1157, 1222, 115
2d81c 37 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20  7,. /*   260 */ 
2d81d 20 31 31 38 37 2c 20 31 31 35 37 2c 20 31 33 30   1187, 1157, 130
2d81e 37 2c 20 31 32 36 34 2c 20 31 32 36 34 2c 20 31  7, 1264, 1264, 1
2d81f 32 32 32 2c 20 31 31 35 37 2c 20 31 31 35 37 2c  222, 1157, 1157,
2d820 20 31 31 35 37 2c 20 31 33 30 37 2c 0a 20 2f 2a   1157, 1307,. /*
2d821 20 20 20 32 37 30 20 2a 2f 20 20 31 33 35 34 2c     270 */  1354,
2d822 20 31 30 39 39 2c 20 31 33 35 34 2c 20 31 30 39   1099, 1354, 109
2d823 39 2c 20 31 33 35 34 2c 20 31 30 39 39 2c 20 31  9, 1354, 1099, 1
2d824 30 39 39 2c 20 31 33 33 30 2c 20 31 32 34 35 2c  099, 1330, 1245,
2d825 20 31 33 35 34 2c 0a 20 2f 2a 20 20 20 32 38 30   1354,. /*   280
2d826 20 2a 2f 20 20 31 30 39 39 2c 20 31 33 31 39 2c   */  1099, 1319,
2d827 20 31 33 31 39 2c 20 31 33 36 35 2c 20 31 30 30   1319, 1365, 100
2d828 37 2c 20 31 30 39 39 2c 20 31 33 37 33 2c 20 31  7, 1099, 1373, 1
2d829 33 37 33 2c 20 31 33 37 33 2c 20 31 33 37 33 2c  373, 1373, 1373,
2d82a 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31  . /*   290 */  1
2d82b 30 30 37 2c 20 31 33 31 39 2c 20 31 33 36 35 2c  007, 1319, 1365,
2d82c 20 31 30 39 39 2c 20 31 33 35 36 2c 20 31 33 35   1099, 1356, 135
2d82d 36 2c 20 31 30 39 39 2c 20 31 30 39 39 2c 20 31  6, 1099, 1099, 1
2d82e 33 37 30 2c 20 2d 31 31 31 2c 0a 20 2f 2a 20 20  370, -111,. /*  
2d82f 20 33 30 30 20 2a 2f 20 20 2d 31 31 31 2c 20 2d   300 */  -111, -
2d830 31 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c  111, -111, -111,
2d831 20 20 35 35 32 2c 20 31 30 36 36 2c 20 31 30 35    552, 1066, 105
2d832 39 2c 20 31 30 36 39 2c 20 20 37 31 32 2c 20 20  9, 1069,  712,  
2d833 36 33 31 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a  631,. /*   310 *
2d834 2f 20 20 20 39 31 35 2c 20 20 38 30 31 2c 20 20  /   915,  801,  
2d835 39 34 36 2c 20 20 38 36 36 2c 20 31 30 30 30 2c  946,  866, 1000,
2d836 20 31 30 32 32 2c 20 31 30 39 37 2c 20 31 31 35   1022, 1097, 115
2d837 33 2c 20 31 32 34 31 2c 20 31 32 34 37 2c 0a 20  3, 1241, 1247,. 
2d838 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 31 32 38  /*   320 */  128
2d839 35 2c 20 20 35 31 35 2c 20 31 33 33 37 2c 20 20  5,  515, 1337,  
2d83a 34 34 30 2c 20 31 34 31 32 2c 20 31 33 39 39 2c  440, 1412, 1399,
2d83b 20 31 33 39 32 2c 20 31 33 39 34 2c 20 31 33 30   1392, 1394, 130
2d83c 30 2c 20 31 33 30 31 2c 0a 20 2f 2a 20 20 20 33  0, 1301,. /*   3
2d83d 33 30 20 2a 2f 20 20 31 33 30 32 2c 20 31 33 30  30 */  1302, 130
2d83e 33 2c 20 31 33 30 34 2c 20 31 33 32 35 2c 20 31  3, 1304, 1325, 1
2d83f 33 31 31 2c 20 31 34 30 33 2c 20 31 34 30 34 2c  311, 1403, 1404,
2d840 20 31 34 30 35 2c 20 31 34 30 37 2c 20 31 34 31   1405, 1407, 141
2d841 35 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20  5,. /*   340 */ 
2d842 20 31 34 30 38 2c 20 31 34 30 39 2c 20 31 34 30   1408, 1409, 140
2d843 36 2c 20 31 34 31 30 2c 20 31 34 31 31 2c 20 31  6, 1410, 1411, 1
2d844 34 31 33 2c 20 31 34 30 31 2c 20 31 34 31 34 2c  413, 1401, 1414,
2d845 20 31 34 30 32 2c 20 31 34 31 33 2c 0a 20 2f 2a   1402, 1413,. /*
2d846 20 20 20 33 35 30 20 2a 2f 20 20 31 33 31 37 2c     350 */  1317,
2d847 20 31 34 31 38 2c 20 31 34 31 36 2c 20 31 34 31   1418, 1416, 141
2d848 37 2c 20 31 33 32 33 2c 20 31 34 31 39 2c 20 31  7, 1323, 1419, 1
2d849 34 32 30 2c 20 31 34 32 31 2c 20 31 33 39 35 2c  420, 1421, 1395,
2d84a 20 31 34 32 34 2c 0a 20 2f 2a 20 20 20 33 36 30   1424,. /*   360
2d84b 20 2a 2f 20 20 31 33 39 37 2c 20 31 34 32 36 2c   */  1397, 1426,
2d84c 20 31 34 32 38 2c 20 31 34 32 37 2c 20 31 34 33   1428, 1427, 143
2d84d 30 2c 20 31 34 32 32 2c 20 31 34 33 31 2c 20 31  0, 1422, 1431, 1
2d84e 33 35 32 2c 20 31 33 34 36 2c 20 31 34 33 37 2c  352, 1346, 1437,
2d84f 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 31  . /*   370 */  1
2d850 34 33 38 2c 20 31 34 33 33 2c 20 31 33 35 37 2c  438, 1433, 1357,
2d851 20 31 34 32 33 2c 20 31 34 32 35 2c 20 31 34 32   1423, 1425, 142
2d852 39 2c 20 31 34 33 35 2c 20 31 34 33 32 2c 20 31  9, 1435, 1432, 1
2d853 33 32 34 2c 20 31 33 35 39 2c 0a 20 2f 2a 20 20  324, 1359,. /*  
2d854 20 33 38 30 20 2a 2f 20 20 31 34 34 32 2c 20 31   380 */  1442, 1
2d855 34 36 31 2c 20 31 34 36 36 2c 20 31 33 36 30 2c  461, 1466, 1360,
2d856 20 31 33 39 36 2c 20 31 34 30 30 2c 20 31 33 34   1396, 1400, 134
2d857 32 2c 20 31 34 34 39 2c 20 31 33 35 38 2c 20 31  2, 1449, 1358, 1
2d858 34 37 32 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a  472,. /*   390 *
2d859 2f 20 20 31 34 35 39 2c 20 31 33 35 35 2c 20 31  /  1459, 1355, 1
2d85a 34 35 38 2c 20 31 33 36 32 2c 20 31 33 37 31 2c  458, 1362, 1371,
2d85b 20 31 33 36 31 2c 20 31 34 36 32 2c 20 31 33 36   1361, 1462, 136
2d85c 33 2c 20 31 34 36 33 2c 20 31 34 36 34 2c 0a 20  3, 1463, 1464,. 
2d85d 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 31 34 36  /*   400 */  146
2d85e 39 2c 20 31 34 33 34 2c 20 31 34 37 34 2c 20 31  9, 1434, 1474, 1
2d85f 33 35 30 2c 20 31 34 37 30 2c 20 31 34 39 30 2c  350, 1470, 1490,
2d860 20 31 34 39 31 2c 0a 7d 3b 0a 23 64 65 66 69 6e   1491,.};.#defin
2d861 65 20 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f  e YY_REDUCE_USE_
2d862 44 46 4c 54 20 28 2d 31 38 30 29 0a 23 64 65 66  DFLT (-180).#def
2d863 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f 4d 41  ine YY_REDUCE_MA
2d864 58 20 33 30 33 0a 73 74 61 74 69 63 20 63 6f 6e  X 303.static con
2d865 73 74 20 73 68 6f 72 74 20 79 79 5f 72 65 64 75  st short yy_redu
2d866 63 65 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f  ce_ofst[] = {. /
2d867 2a 20 20 20 20 20 30 20 2a 2f 20 20 2d 31 34 31  *     0 */  -141
2d868 2c 20 20 20 38 32 2c 20 20 31 35 34 2c 20 20 32  ,   82,  154,  2
2d869 38 34 2c 20 20 20 31 32 2c 20 20 20 37 35 2c 20  84,   12,   75, 
2d86a 20 20 36 39 2c 20 20 20 37 33 2c 20 20 31 34 32    69,   73,  142
2d86b 2c 20 20 2d 35 39 2c 0a 20 2f 2a 20 20 20 20 31  ,  -59,. /*    1
2d86c 30 20 2a 2f 20 20 20 31 34 35 2c 20 20 20 38 37  0 */   145,   87
2d86d 2c 20 20 31 35 39 2c 20 20 32 32 30 2c 20 20 32  ,  159,  220,  2
2d86e 32 36 2c 20 20 33 34 36 2c 20 20 32 38 39 2c 20  26,  346,  289, 
2d86f 20 31 35 35 2c 20 20 34 32 39 2c 20 20 34 33 37   155,  429,  437
2d870 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
2d871 20 34 34 32 2c 20 20 34 38 36 2c 20 20 34 39 39   442,  486,  499
2d872 2c 20 20 35 30 35 2c 20 20 35 30 37 2c 20 20 35  ,  505,  507,  5
2d873 31 39 2c 20 20 35 35 38 2c 20 20 35 37 31 2c 20  19,  558,  571, 
2d874 20 35 37 37 2c 20 20 35 38 38 2c 0a 20 2f 2a 20   577,  588,. /* 
2d875 20 20 20 33 30 20 2a 2f 20 20 20 35 39 31 2c 20     30 */   591, 
2d876 20 36 33 30 2c 20 20 36 34 33 2c 20 20 36 34 39   630,  643,  649
2d877 2c 20 20 36 35 31 2c 20 20 36 36 32 2c 20 20 37  ,  651,  662,  7
2d878 30 32 2c 20 20 37 31 35 2c 20 20 37 32 31 2c 20  02,  715,  721, 
2d879 20 37 33 33 2c 0a 20 2f 2a 20 20 20 20 34 30 20   733,. /*    40 
2d87a 2a 2f 20 20 20 37 37 34 2c 20 20 37 38 37 2c 20  */   774,  787, 
2d87b 20 37 39 33 2c 20 20 38 30 35 2c 20 20 38 34 36   793,  805,  846
2d87c 2c 20 20 38 35 39 2c 20 20 38 36 35 2c 20 20 38  ,  859,  865,  8
2d87d 37 37 2c 20 20 38 38 31 2c 20 20 39 33 34 2c 0a  77,  881,  934,.
2d87e 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 39   /*    50 */   9
2d87f 33 36 2c 20 20 39 36 33 2c 20 20 39 36 37 2c 20  36,  963,  967, 
2d880 20 39 36 39 2c 20 20 39 39 38 2c 20 31 30 35 33   969,  998, 1053
2d881 2c 20 31 30 37 32 2c 20 31 30 38 38 2c 20 31 31  , 1072, 1088, 11
2d882 30 39 2c 20 2d 31 37 39 2c 0a 20 2f 2a 20 20 20  09, -179,. /*   
2d883 20 36 30 20 2a 2f 20 20 20 38 35 30 2c 20 20 32   60 */   850,  2
2d884 38 33 2c 20 20 33 38 30 2c 20 20 33 38 31 2c 20  83,  380,  381, 
2d885 20 20 38 39 2c 20 20 33 30 34 2c 20 20 33 39 30    89,  304,  390
2d886 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20  ,    2,    2,   
2d887 20 32 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f   2,. /*    70 */
2d888 20 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20       2,    2,   
2d889 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2d88a 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2d88b 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 0a 20 2f  ,    2,    2,. /
2d88c 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20 20 32  *    80 */     2
2d88d 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20  ,    2,    2,   
2d88e 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2d88f 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2d890 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20 20 39  ,    2,. /*    9
2d891 30 20 2a 2f 20 20 20 20 20 32 2c 20 20 20 20 32  0 */     2,    2
2d892 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20  ,    2,    2,   
2d893 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2d894 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2d895 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
2d896 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2d897 2c 20 20 32 31 35 2c 20 20 20 20 32 2c 20 20 20  ,  215,    2,   
2d898 20 32 2c 20 20 34 34 39 2c 20 20 35 37 34 2c 20   2,  449,  574, 
2d899 20 37 31 39 2c 20 20 37 32 32 2c 0a 20 2f 2a 20   719,  722,. /* 
2d89a 20 20 31 31 30 20 2a 2f 20 20 20 37 39 31 2c 20    110 */   791, 
2d89b 20 31 33 34 2c 20 20 20 36 35 2c 20 20 39 34 32   134,   65,  942
2d89c 2c 20 20 35 32 31 2c 20 20 37 39 34 2c 20 20 2d  ,  521,  794,  -
2d89d 34 37 2c 20 20 38 37 38 2c 20 20 39 35 36 2c 20  47,  878,  956, 
2d89e 20 39 38 36 2c 0a 20 2f 2a 20 20 20 31 32 30 20   986,. /*   120 
2d89f 2a 2f 20 20 31 30 30 33 2c 20 31 30 34 37 2c 20  */  1003, 1047, 
2d8a0 31 30 37 34 2c 20 31 30 39 32 2c 20 20 32 39 35  1074, 1092,  295
2d8a1 2c 20 31 31 30 34 2c 20 20 20 20 32 2c 20 20 37  , 1104,    2,  7
2d8a2 37 39 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 0a  79,    2,    2,.
2d8a3 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 20   /*   130 */    
2d8a4 20 32 2c 20 20 20 20 32 2c 20 20 31 35 38 2c 20   2,    2,  158, 
2d8a5 20 33 33 38 2c 20 20 35 37 32 2c 20 20 36 34 34   338,  572,  644
2d8a6 2c 20 20 36 35 30 2c 20 20 36 37 30 2c 20 20 37  ,  650,  670,  7
2d8a7 32 33 2c 20 20 33 39 32 2c 0a 20 2f 2a 20 20 20  23,  392,. /*   
2d8a8 31 34 30 20 2a 2f 20 20 20 35 36 34 2c 20 20 37  140 */   564,  7
2d8a9 39 32 2c 20 20 38 38 35 2c 20 20 39 36 36 2c 20  92,  885,  966, 
2d8aa 31 30 30 32 2c 20 31 30 33 36 2c 20 20 37 32 33  1002, 1036,  723
2d8ab 2c 20 31 30 38 34 2c 20 31 30 39 31 2c 20 31 31  , 1084, 1091, 11
2d8ac 30 30 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  00,. /*   150 */
2d8ad 20 20 31 31 33 30 2c 20 31 31 34 31 2c 20 31 31    1130, 1141, 11
2d8ae 34 36 2c 20 31 31 34 39 2c 20 31 31 35 30 2c 20  46, 1149, 1150, 
2d8af 31 31 35 32 2c 20 31 31 35 34 2c 20 31 31 35 35  1152, 1154, 1155
2d8b0 2c 20 31 31 36 36 2c 20 31 31 36 38 2c 0a 20 2f  , 1166, 1168,. /
2d8b1 2a 20 20 20 31 36 30 20 2a 2f 20 20 31 31 37 31  *   160 */  1171
2d8b2 2c 20 31 31 37 32 2c 20 31 31 38 30 2c 20 31 31  , 1172, 1180, 11
2d8b3 38 32 2c 20 31 31 38 34 2c 20 31 31 38 35 2c 20  82, 1184, 1185, 
2d8b4 31 31 39 31 2c 20 31 31 39 32 2c 20 31 31 39 33  1191, 1192, 1193
2d8b5 2c 20 31 31 39 36 2c 0a 20 2f 2a 20 20 20 31 37  , 1196,. /*   17
2d8b6 30 20 2a 2f 20 20 31 31 39 38 2c 20 31 32 30 31  0 */  1198, 1201
2d8b7 2c 20 31 32 30 32 2c 20 31 32 30 33 2c 20 20 35  , 1202, 1203,  5
2d8b8 35 34 2c 20 20 35 35 34 2c 20 20 37 33 34 2c 20  54,  554,  734, 
2d8b9 20 32 33 38 2c 20 20 33 32 36 2c 20 20 33 37 33   238,  326,  373
2d8ba 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
2d8bb 2d 31 33 34 2c 20 20 32 37 38 2c 20 20 36 30 34  -134,  278,  604
2d8bc 2c 20 20 37 31 30 2c 20 20 38 32 32 2c 20 20 20  ,  710,  822,   
2d8bd 34 34 2c 20 20 36 30 30 2c 20 20 36 33 35 2c 20  44,  600,  635, 
2d8be 20 2d 39 38 2c 20 20 2d 37 30 2c 0a 20 2f 2a 20   -98,  -70,. /* 
2d8bf 20 20 31 39 30 20 2a 2f 20 20 20 2d 35 34 2c 20    190 */   -54, 
2d8c0 20 2d 33 36 2c 20 20 2d 33 35 2c 20 20 2d 33 35   -36,  -35,  -35
2d8c1 2c 20 20 2d 33 35 2c 20 20 20 31 33 2c 20 20 2d  ,  -35,   13,  -
2d8c2 33 35 2c 20 20 20 31 34 2c 20 20 31 34 39 2c 20  35,   14,  149, 
2d8c3 20 31 31 35 2c 0a 20 2f 2a 20 20 20 32 30 30 20   115,. /*   200 
2d8c4 2a 2f 20 20 20 31 36 33 2c 20 20 20 31 34 2c 20  */   163,   14, 
2d8c5 20 32 31 30 2c 20 20 32 32 33 2c 20 20 33 36 30   210,  223,  360
2d8c6 2c 20 20 2d 33 35 2c 20 20 2d 33 35 2c 20 20 33  ,  -35,  -35,  3
2d8c7 35 39 2c 20 20 34 34 38 2c 20 20 2d 33 35 2c 0a  59,  448,  -35,.
2d8c8 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 2d   /*   210 */   -
2d8c9 33 35 2c 20 20 2d 33 35 2c 20 20 2d 33 35 2c 20  35,  -35,  -35, 
2d8ca 20 35 31 33 2c 20 20 35 35 31 2c 20 20 35 39 38   513,  551,  598
2d8cb 2c 20 20 36 35 33 2c 20 20 35 39 36 2c 20 20 36  ,  653,  596,  6
2d8cc 30 35 2c 20 20 36 34 37 2c 0a 20 2f 2a 20 20 20  05,  647,. /*   
2d8cd 32 32 30 20 2a 2f 20 20 20 36 35 36 2c 20 20 37  220 */   656,  7
2d8ce 32 34 2c 20 20 37 34 31 2c 20 20 37 39 36 2c 20  24,  741,  796, 
2d8cf 20 38 33 30 2c 20 20 38 30 36 2c 20 20 38 34 37   830,  806,  847
2d8d0 2c 20 20 38 34 39 2c 20 20 38 37 30 2c 20 20 38  ,  849,  870,  8
2d8d1 34 34 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  44,. /*   230 */
2d8d2 20 20 20 38 33 33 2c 20 20 38 33 32 2c 20 20 38     833,  832,  8
2d8d3 33 39 2c 20 20 38 35 34 2c 20 20 39 30 35 2c 20  39,  854,  905, 
2d8d4 20 39 34 30 2c 20 20 39 34 34 2c 20 20 39 34 35   940,  944,  945
2d8d5 2c 20 20 39 35 33 2c 20 20 39 35 34 2c 0a 20 2f  ,  953,  954,. /
2d8d6 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 39 35 39  *   240 */   959
2d8d7 2c 20 20 39 32 39 2c 20 20 39 34 38 2c 20 20 39  ,  929,  948,  9
2d8d8 35 30 2c 20 20 39 35 37 2c 20 20 39 38 37 2c 20  50,  957,  987, 
2d8d9 20 39 38 35 2c 20 20 39 38 38 2c 20 31 30 36 32   985,  988, 1062
2d8da 2c 20 31 30 38 36 2c 0a 20 2f 2a 20 20 20 32 35  , 1086,. /*   25
2d8db 30 20 2a 2f 20 20 31 30 32 33 2c 20 31 30 33 34  0 */  1023, 1034
2d8dc 2c 20 31 30 34 31 2c 20 31 30 34 34 2c 20 31 30  , 1041, 1044, 10
2d8dd 34 32 2c 20 31 30 34 39 2c 20 31 30 39 35 2c 20  42, 1049, 1095, 
2d8de 31 31 30 36 2c 20 31 31 31 34 2c 20 31 31 32 35  1106, 1114, 1125
2d8df 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
2d8e0 31 31 33 34 2c 20 31 31 33 37 2c 20 31 31 36 35  1134, 1137, 1165
2d8e1 2c 20 31 31 32 39 2c 20 31 31 33 35 2c 20 31 31  , 1129, 1135, 11
2d8e2 38 36 2c 20 31 31 37 39 2c 20 31 31 38 38 2c 20  86, 1179, 1188, 
2d8e3 31 31 39 35 2c 20 31 31 39 30 2c 0a 20 2f 2a 20  1195, 1190,. /* 
2d8e4 20 20 32 37 30 20 2a 2f 20 20 31 32 31 37 2c 20    270 */  1217, 
2d8e5 31 32 31 38 2c 20 31 32 32 30 2c 20 31 32 32 31  1218, 1220, 1221
2d8e6 2c 20 31 32 32 33 2c 20 31 32 32 34 2c 20 31 32  , 1223, 1224, 12
2d8e7 32 35 2c 20 31 31 33 39 2c 20 31 31 34 34 2c 20  25, 1139, 1144, 
2d8e8 31 32 32 38 2c 0a 20 2f 2a 20 20 20 32 38 30 20  1228,. /*   280 
2d8e9 2a 2f 20 20 31 32 32 39 2c 20 31 31 39 39 2c 20  */  1229, 1199, 
2d8ea 31 32 30 30 2c 20 31 31 37 33 2c 20 31 31 39 37  1200, 1173, 1197
2d8eb 2c 20 31 32 33 33 2c 20 31 32 30 35 2c 20 31 32  , 1233, 1205, 12
2d8ec 30 36 2c 20 31 32 30 37 2c 20 31 32 30 38 2c 0a  06, 1207, 1208,.
2d8ed 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 32   /*   290 */  12
2d8ee 30 34 2c 20 31 32 31 32 2c 20 31 31 37 34 2c 20  04, 1212, 1174, 
2d8ef 31 32 33 37 2c 20 31 31 36 31 2c 20 31 31 36 33  1237, 1161, 1163
2d8f0 2c 20 31 32 34 38 2c 20 31 32 34 39 2c 20 31 31  , 1248, 1249, 11
2d8f1 36 34 2c 20 31 32 34 36 2c 0a 20 2f 2a 20 20 20  64, 1246,. /*   
2d8f2 33 30 30 20 2a 2f 20 20 31 32 33 31 2c 20 31 32  300 */  1231, 12
2d8f3 33 32 2c 20 31 31 36 32 2c 20 31 31 36 37 2c 0a  32, 1162, 1167,.
2d8f4 7d 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  };.static const 
2d8f5 59 59 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f  YYACTIONTYPE yy_
2d8f6 64 65 66 61 75 6c 74 5b 5d 20 3d 20 7b 0a 20 2f  default[] = {. /
2d8f7 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 36 32 31  *     0 */   621
2d8f8 2c 20 20 38 35 36 2c 20 20 39 33 39 2c 20 20 39  ,  856,  939,  9
2d8f9 33 39 2c 20 20 38 35 36 2c 20 20 39 33 39 2c 20  39,  856,  939, 
2d8fa 20 39 33 39 2c 20 20 38 38 35 2c 20 20 38 38 35   939,  885,  885
2d8fb 2c 20 20 37 34 34 2c 0a 20 2f 2a 20 20 20 20 31  ,  744,. /*    1
2d8fc 30 20 2a 2f 20 20 20 38 35 34 2c 20 20 39 33 39  0 */   854,  939
2d8fd 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d8fe 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d8ff 20 39 31 34 2c 20 20 39 33 39 2c 20 20 39 33 39   914,  939,  939
2d900 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
2d901 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d902 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d903 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d904 20 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20   939,  939,. /* 
2d905 20 20 20 33 30 20 2a 2f 20 20 20 39 33 39 2c 20     30 */   939, 
2d906 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d907 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d908 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d909 20 39 33 39 2c 0a 20 2f 2a 20 20 20 20 34 30 20   939,. /*    40 
2d90a 2a 2f 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20  */   939,  939, 
2d90b 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d90c 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d90d 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 0a  39,  939,  939,.
2d90e 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 39   /*    50 */   9
2d90f 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d910 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d911 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d912 33 39 2c 20 20 38 32 38 2c 0a 20 2f 2a 20 20 20  39,  828,. /*   
2d913 20 36 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39   60 */   939,  9
2d914 33 39 2c 20 20 39 33 39 2c 20 20 36 36 30 2c 20  39,  939,  660, 
2d915 20 38 38 35 2c 20 20 38 38 35 2c 20 20 37 34 38   885,  885,  748
2d916 2c 20 20 37 37 39 2c 20 20 39 33 39 2c 20 20 39  ,  779,  939,  9
2d917 33 39 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  39,. /*    70 */
2d918 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39     939,  939,  9
2d919 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d91a 20 39 33 39 2c 20 20 37 38 30 2c 20 20 39 33 39   939,  780,  939
2d91b 2c 20 20 38 35 38 2c 20 20 38 35 33 2c 0a 20 2f  ,  858,  853,. /
2d91c 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 38 34 39  *    80 */   849
2d91d 2c 20 20 38 35 31 2c 20 20 38 35 30 2c 20 20 38  ,  851,  850,  8
2d91e 35 37 2c 20 20 37 38 31 2c 20 20 37 37 30 2c 20  57,  781,  770, 
2d91f 20 37 37 37 2c 20 20 37 38 34 2c 20 20 37 35 39   777,  784,  759
2d920 2c 20 20 38 39 38 2c 0a 20 2f 2a 20 20 20 20 39  ,  898,. /*    9
2d921 30 20 2a 2f 20 20 20 37 38 36 2c 20 20 37 38 37  0 */   786,  787
2d922 2c 20 20 37 39 33 2c 20 20 37 39 34 2c 20 20 39  ,  793,  794,  9
2d923 31 35 2c 20 20 39 31 33 2c 20 20 38 31 36 2c 20  15,  913,  816, 
2d924 20 38 31 35 2c 20 20 38 33 34 2c 20 20 38 31 38   815,  834,  818
2d925 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
2d926 20 38 34 30 2c 20 20 38 31 37 2c 20 20 38 32 37   840,  817,  827
2d927 2c 20 20 36 35 32 2c 20 20 38 31 39 2c 20 20 38  ,  652,  819,  8
2d928 32 30 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  20,  939,  939, 
2d929 20 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20   939,  939,. /* 
2d92a 20 20 31 31 30 20 2a 2f 20 20 20 39 33 39 2c 20    110 */   939, 
2d92b 20 37 31 33 2c 20 20 36 34 37 2c 20 20 39 33 39   713,  647,  939
2d92c 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d92d 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d92e 20 39 33 39 2c 0a 20 2f 2a 20 20 20 31 32 30 20   939,. /*   120 
2d92f 2a 2f 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20  */   939,  939, 
2d930 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d931 2c 20 20 39 33 39 2c 20 20 38 32 31 2c 20 20 39  ,  939,  821,  9
2d932 33 39 2c 20 20 38 32 32 2c 20 20 38 33 35 2c 0a  39,  822,  835,.
2d933 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 38   /*   130 */   8
2d934 33 36 2c 20 20 38 33 37 2c 20 20 39 33 39 2c 20  36,  837,  939, 
2d935 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d936 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d937 33 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20 20  39,  939,. /*   
2d938 31 34 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 36  140 */   939,  6
2d939 32 37 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  27,  939,  939, 
2d93a 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d93b 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d93c 33 39 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  39,. /*   150 */
2d93d 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39     939,  939,  9
2d93e 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d93f 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d940 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f  ,  939,  939,. /
2d941 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 39 33 39  *   160 */   939
2d942 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d943 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d944 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d945 2c 20 20 38 36 39 2c 0a 20 2f 2a 20 20 20 31 37  ,  869,. /*   17
2d946 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 31 38  0 */   939,  918
2d947 2c 20 20 39 32 30 2c 20 20 39 33 39 2c 20 20 39  ,  920,  939,  9
2d948 33 39 2c 20 20 39 33 39 2c 20 20 36 32 31 2c 20  39,  939,  621, 
2d949 20 37 34 34 2c 20 20 37 34 34 2c 20 20 37 34 34   744,  744,  744
2d94a 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
2d94b 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d94c 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 37  ,  939,  939,  7
2d94d 33 38 2c 20 20 37 34 38 2c 20 20 39 33 32 2c 20  38,  748,  932, 
2d94e 20 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20   939,  939,. /* 
2d94f 20 20 31 39 30 20 2a 2f 20 20 20 37 30 34 2c 20    190 */   704, 
2d950 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d951 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d952 33 39 2c 20 20 39 33 39 2c 20 20 36 32 39 2c 20  39,  939,  629, 
2d953 20 37 33 36 2c 0a 20 2f 2a 20 20 20 32 30 30 20   736,. /*   200 
2d954 2a 2f 20 20 20 36 36 32 2c 20 20 37 34 36 2c 20  */   662,  746, 
2d955 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d956 2c 20 20 36 34 39 2c 20 20 37 32 35 2c 20 20 38  ,  649,  725,  8
2d957 39 31 2c 20 20 39 33 39 2c 20 20 39 30 35 2c 0a  91,  939,  905,.
2d958 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 39   /*   210 */   9
2d959 30 33 2c 20 20 37 32 37 2c 20 20 37 38 39 2c 20  03,  727,  789, 
2d95a 20 39 33 39 2c 20 20 37 33 36 2c 20 20 37 34 35   939,  736,  745
2d95b 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d95c 33 39 2c 20 20 38 35 32 2c 0a 20 2f 2a 20 20 20  39,  852,. /*   
2d95d 32 32 30 20 2a 2f 20 20 20 37 37 33 2c 20 20 37  220 */   773,  7
2d95e 37 33 2c 20 20 37 36 31 2c 20 20 37 37 33 2c 20  73,  761,  773, 
2d95f 20 36 38 33 2c 20 20 37 37 33 2c 20 20 39 33 39   683,  773,  939
2d960 2c 20 20 37 37 33 2c 20 20 39 33 39 2c 20 20 36  ,  773,  939,  6
2d961 38 36 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  86,. /*   230 */
2d962 20 20 20 37 38 33 2c 20 20 37 36 31 2c 20 20 37     783,  761,  7
2d963 36 31 2c 20 20 37 38 33 2c 20 20 36 32 36 2c 20  61,  783,  626, 
2d964 20 36 32 36 2c 20 20 36 32 36 2c 20 20 36 32 36   626,  626,  626
2d965 2c 20 20 36 33 37 2c 20 20 36 33 37 2c 0a 20 2f  ,  637,  637,. /
2d966 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 37 30 33  *   240 */   703
2d967 2c 20 20 39 33 39 2c 20 20 37 38 33 2c 20 20 37  ,  939,  783,  7
2d968 37 34 2c 20 20 37 37 36 2c 20 20 37 36 36 2c 20  74,  776,  766, 
2d969 20 37 37 38 2c 20 20 39 33 39 2c 20 20 37 35 32   778,  939,  752
2d96a 2c 20 20 37 35 32 2c 0a 20 2f 2a 20 20 20 32 35  ,  752,. /*   25
2d96b 30 20 2a 2f 20 20 20 37 36 30 2c 20 20 37 36 35  0 */   760,  765
2d96c 2c 20 20 37 36 30 2c 20 20 37 36 35 2c 20 20 37  ,  760,  765,  7
2d96d 36 30 2c 20 20 37 36 35 2c 20 20 37 31 35 2c 20  60,  765,  715, 
2d96e 20 37 31 35 2c 20 20 37 30 30 2c 20 20 37 31 35   715,  700,  715
2d96f 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
2d970 20 36 38 36 2c 20 20 37 31 35 2c 20 20 38 36 32   686,  715,  862
2d971 2c 20 20 38 36 36 2c 20 20 38 36 36 2c 20 20 37  ,  866,  866,  7
2d972 30 30 2c 20 20 37 31 35 2c 20 20 37 31 35 2c 20  00,  715,  715, 
2d973 20 37 31 35 2c 20 20 38 36 32 2c 0a 20 2f 2a 20   715,  862,. /* 
2d974 20 20 32 37 30 20 2a 2f 20 20 20 36 34 34 2c 20    270 */   644, 
2d975 20 37 35 32 2c 20 20 36 34 34 2c 20 20 37 35 32   752,  644,  752
2d976 2c 20 20 36 34 34 2c 20 20 37 35 32 2c 20 20 37  ,  644,  752,  7
2d977 35 32 2c 20 20 38 39 35 2c 20 20 38 39 37 2c 20  52,  895,  897, 
2d978 20 36 34 34 2c 0a 20 2f 2a 20 20 20 32 38 30 20   644,. /*   280 
2d979 2a 2f 20 20 20 37 35 32 2c 20 20 37 31 37 2c 20  */   752,  717, 
2d97a 20 37 31 37 2c 20 20 37 39 35 2c 20 20 37 38 33   717,  795,  783
2d97b 2c 20 20 37 35 32 2c 20 20 37 32 34 2c 20 20 37  ,  752,  724,  7
2d97c 32 34 2c 20 20 37 32 34 2c 20 20 37 32 34 2c 0a  24,  724,  724,.
2d97d 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 37   /*   290 */   7
2d97e 38 33 2c 20 20 37 31 37 2c 20 20 37 39 35 2c 20  83,  717,  795, 
2d97f 20 37 35 32 2c 20 20 39 31 37 2c 20 20 39 31 37   752,  917,  917
2d980 2c 20 20 37 35 32 2c 20 20 37 35 32 2c 20 20 39  ,  752,  752,  9
2d981 32 35 2c 20 20 36 37 30 2c 0a 20 2f 2a 20 20 20  25,  670,. /*   
2d982 33 30 30 20 2a 2f 20 20 20 36 38 38 2c 20 20 36  300 */   688,  6
2d983 38 38 2c 20 20 39 33 32 2c 20 20 39 33 37 2c 20  88,  932,  937, 
2d984 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d985 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d986 33 39 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f  39,. /*   310 */
2d987 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39     939,  939,  9
2d988 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d989 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d98a 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f  ,  939,  939,. /
2d98b 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 39 33 39  *   320 */   939
2d98c 2c 20 20 38 37 31 2c 20 20 39 33 39 2c 20 20 39  ,  871,  939,  9
2d98d 33 39 2c 20 20 39 33 39 2c 20 20 36 33 35 2c 20  39,  939,  635, 
2d98e 20 39 33 39 2c 20 20 36 35 34 2c 20 20 38 30 32   939,  654,  802
2d98f 2c 20 20 38 30 37 2c 0a 20 2f 2a 20 20 20 33 33  ,  807,. /*   33
2d990 30 20 2a 2f 20 20 20 38 30 33 2c 20 20 39 33 39  0 */   803,  939
2d991 2c 20 20 38 30 34 2c 20 20 39 33 39 2c 20 20 37  ,  804,  939,  7
2d992 33 30 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  30,  939,  939, 
2d993 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d994 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20  ,. /*   340 */  
2d995 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d996 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 38  ,  939,  939,  8
2d997 35 35 2c 20 20 39 33 39 2c 20 20 37 36 37 2c 20  55,  939,  767, 
2d998 20 39 33 39 2c 20 20 37 37 35 2c 0a 20 2f 2a 20   939,  775,. /* 
2d999 20 20 33 35 30 20 2a 2f 20 20 20 39 33 39 2c 20    350 */   939, 
2d99a 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d99b 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d99c 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d99d 20 39 33 39 2c 0a 20 2f 2a 20 20 20 33 36 30 20   939,. /*   360 
2d99e 2a 2f 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20  */   939,  939, 
2d99f 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d9a0 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d9a1 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 0a  39,  939,  939,.
2d9a2 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 39   /*   370 */   9
2d9a3 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d9a4 20 39 33 39 2c 20 20 39 33 39 2c 20 20 38 39 33   939,  939,  893
2d9a5 2c 20 20 38 39 34 2c 20 20 39 33 39 2c 20 20 39  ,  894,  939,  9
2d9a6 33 39 2c 20 20 39 33 39 2c 0a 20 2f 2a 20 20 20  39,  939,. /*   
2d9a7 33 38 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39  380 */   939,  9
2d9a8 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d9a9 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d9aa 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39  ,  939,  939,  9
2d9ab 33 39 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f  39,. /*   390 */
2d9ac 20 20 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39     939,  939,  9
2d9ad 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 20  39,  939,  939, 
2d9ae 20 39 33 39 2c 20 20 39 33 39 2c 20 20 39 33 39   939,  939,  939
2d9af 2c 20 20 39 33 39 2c 20 20 39 33 39 2c 0a 20 2f  ,  939,  939,. /
2d9b0 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 39 33 39  *   400 */   939
2d9b1 2c 20 20 39 32 34 2c 20 20 39 33 39 2c 20 20 39  ,  924,  939,  9
2d9b2 33 39 2c 20 20 39 32 37 2c 20 20 36 32 32 2c 20  39,  927,  622, 
2d9b3 20 39 33 39 2c 20 20 36 31 37 2c 20 20 36 31 39   939,  617,  619
2d9b4 2c 20 20 36 32 30 2c 0a 20 2f 2a 20 20 20 34 31  ,  620,. /*   41
2d9b5 30 20 2a 2f 20 20 20 36 32 34 2c 20 20 36 32 35  0 */   624,  625
2d9b6 2c 20 20 36 32 38 2c 20 20 36 35 34 2c 20 20 36  ,  628,  654,  6
2d9b7 35 35 2c 20 20 36 35 37 2c 20 20 36 35 38 2c 20  55,  657,  658, 
2d9b8 20 36 35 39 2c 20 20 36 33 30 2c 20 20 36 33 31   659,  630,  631
2d9b9 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20  ,. /*   420 */  
2d9ba 20 36 33 32 2c 20 20 36 33 33 2c 20 20 36 33 34   632,  633,  634
2d9bb 2c 20 20 36 33 36 2c 20 20 36 34 30 2c 20 20 36  ,  636,  640,  6
2d9bc 33 38 2c 20 20 36 33 39 2c 20 20 36 34 31 2c 20  38,  639,  641, 
2d9bd 20 36 34 38 2c 20 20 36 35 30 2c 0a 20 2f 2a 20   648,  650,. /* 
2d9be 20 20 34 33 30 20 2a 2f 20 20 20 36 36 39 2c 20    430 */   669, 
2d9bf 20 36 37 31 2c 20 20 36 37 33 2c 20 20 37 33 34   671,  673,  734
2d9c0 2c 20 20 37 33 35 2c 20 20 37 39 39 2c 20 20 37  ,  735,  799,  7
2d9c1 32 38 2c 20 20 37 32 39 2c 20 20 37 33 33 2c 20  28,  729,  733, 
2d9c2 20 36 35 36 2c 0a 20 2f 2a 20 20 20 34 34 30 20   656,. /*   440 
2d9c3 2a 2f 20 20 20 38 31 30 2c 20 20 38 30 31 2c 20  */   810,  801, 
2d9c4 20 38 30 35 2c 20 20 38 30 36 2c 20 20 38 30 38   805,  806,  808
2d9c5 2c 20 20 38 30 39 2c 20 20 38 32 33 2c 20 20 38  ,  809,  823,  8
2d9c6 32 34 2c 20 20 38 32 36 2c 20 20 38 33 32 2c 0a  24,  826,  832,.
2d9c7 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 38   /*   450 */   8
2d9c8 33 39 2c 20 20 38 34 32 2c 20 20 38 32 35 2c 20  39,  842,  825, 
2d9c9 20 38 33 30 2c 20 20 38 33 31 2c 20 20 38 33 33   830,  831,  833
2d9ca 2c 20 20 38 33 38 2c 20 20 38 34 31 2c 20 20 37  ,  838,  841,  7
2d9cb 33 31 2c 20 20 37 33 32 2c 0a 20 2f 2a 20 20 20  31,  732,. /*   
2d9cc 34 36 30 20 2a 2f 20 20 20 38 34 35 2c 20 20 36  460 */   845,  6
2d9cd 36 33 2c 20 20 36 36 34 2c 20 20 36 36 37 2c 20  63,  664,  667, 
2d9ce 20 36 36 38 2c 20 20 38 38 31 2c 20 20 38 38 33   668,  881,  883
2d9cf 2c 20 20 38 38 32 2c 20 20 38 38 34 2c 20 20 36  ,  882,  884,  6
2d9d0 36 36 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f  66,. /*   470 */
2d9d1 20 20 20 36 36 35 2c 20 20 38 31 31 2c 20 20 38     665,  811,  8
2d9d2 31 34 2c 20 20 38 34 37 2c 20 20 38 34 38 2c 20  14,  847,  848, 
2d9d3 20 39 30 36 2c 20 20 39 30 37 2c 20 20 39 30 38   906,  907,  908
2d9d4 2c 20 20 39 30 39 2c 20 20 39 31 30 2c 0a 20 2f  ,  909,  910,. /
2d9d5 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 38 34 33  *   480 */   843
2d9d6 2c 20 20 37 35 33 2c 20 20 38 34 36 2c 20 20 38  ,  753,  846,  8
2d9d7 32 39 2c 20 20 37 36 38 2c 20 20 37 37 31 2c 20  29,  768,  771, 
2d9d8 20 37 37 32 2c 20 20 37 36 39 2c 20 20 37 33 37   772,  769,  737
2d9d9 2c 20 20 37 34 37 2c 0a 20 2f 2a 20 20 20 34 39  ,  747,. /*   49
2d9da 30 20 2a 2f 20 20 20 37 35 35 2c 20 20 37 35 36  0 */   755,  756
2d9db 2c 20 20 37 35 37 2c 20 20 37 35 38 2c 20 20 37  ,  757,  758,  7
2d9dc 34 32 2c 20 20 37 34 33 2c 20 20 37 34 39 2c 20  42,  743,  749, 
2d9dd 20 37 36 34 2c 20 20 37 39 37 2c 20 20 37 39 38   764,  797,  798
2d9de 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20  ,. /*   500 */  
2d9df 20 37 36 32 2c 20 20 37 36 33 2c 20 20 37 35 30   762,  763,  750
2d9e0 2c 20 20 37 35 31 2c 20 20 37 33 39 2c 20 20 37  ,  751,  739,  7
2d9e1 34 30 2c 20 20 37 34 31 2c 20 20 38 34 34 2c 20  40,  741,  844, 
2d9e2 20 38 30 30 2c 20 20 38 31 32 2c 0a 20 2f 2a 20   800,  812,. /* 
2d9e3 20 20 35 31 30 20 2a 2f 20 20 20 38 31 33 2c 20    510 */   813, 
2d9e4 20 36 37 34 2c 20 20 36 37 35 2c 20 20 38 30 37   674,  675,  807
2d9e5 2c 20 20 36 37 36 2c 20 20 36 37 37 2c 20 20 36  ,  676,  677,  6
2d9e6 37 38 2c 20 20 37 31 36 2c 20 20 37 31 39 2c 20  78,  716,  719, 
2d9e7 20 37 32 30 2c 0a 20 2f 2a 20 20 20 35 32 30 20   720,. /*   520 
2d9e8 2a 2f 20 20 20 37 32 31 2c 20 20 36 37 39 2c 20  */   721,  679, 
2d9e9 20 36 39 38 2c 20 20 37 30 31 2c 20 20 37 30 32   698,  701,  702
2d9ea 2c 20 20 36 38 30 2c 20 20 36 38 37 2c 20 20 36  ,  680,  687,  6
2d9eb 38 31 2c 20 20 36 38 32 2c 20 20 36 38 39 2c 0a  81,  682,  689,.
2d9ec 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 36   /*   530 */   6
2d9ed 39 30 2c 20 20 36 39 31 2c 20 20 36 39 34 2c 20  90,  691,  694, 
2d9ee 20 36 39 35 2c 20 20 36 39 36 2c 20 20 36 39 37   695,  696,  697
2d9ef 2c 20 20 36 39 32 2c 20 20 36 39 33 2c 20 20 38  ,  692,  693,  8
2d9f0 36 33 2c 20 20 38 36 34 2c 0a 20 2f 2a 20 20 20  63,  864,. /*   
2d9f1 35 34 30 20 2a 2f 20 20 20 38 36 37 2c 20 20 38  540 */   867,  8
2d9f2 36 35 2c 20 20 36 38 34 2c 20 20 36 38 35 2c 20  65,  684,  685, 
2d9f3 20 36 39 39 2c 20 20 36 37 32 2c 20 20 36 36 31   699,  672,  661
2d9f4 2c 20 20 36 35 33 2c 20 20 37 30 35 2c 20 20 37  ,  653,  705,  7
2d9f5 30 38 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f  08,. /*   550 */
2d9f6 20 20 20 37 30 39 2c 20 20 37 31 30 2c 20 20 37     709,  710,  7
2d9f7 31 31 2c 20 20 37 31 32 2c 20 20 37 31 34 2c 20  11,  712,  714, 
2d9f8 20 37 30 36 2c 20 20 37 30 37 2c 20 20 36 35 31   706,  707,  651
2d9f9 2c 20 20 36 34 32 2c 20 20 36 34 35 2c 0a 20 2f  ,  642,  645,. /
2d9fa 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 37 35 34  *   560 */   754
2d9fb 2c 20 20 38 38 37 2c 20 20 38 39 36 2c 20 20 38  ,  887,  896,  8
2d9fc 39 32 2c 20 20 38 38 38 2c 20 20 38 38 39 2c 20  92,  888,  889, 
2d9fd 20 38 39 30 2c 20 20 36 34 36 2c 20 20 38 35 39   890,  646,  859
2d9fe 2c 20 20 38 36 30 2c 0a 20 2f 2a 20 20 20 35 37  ,  860,. /*   57
2d9ff 30 20 2a 2f 20 20 20 37 31 38 2c 20 20 37 39 31  0 */   718,  791
2da00 2c 20 20 37 39 32 2c 20 20 38 38 36 2c 20 20 38  ,  792,  886,  8
2da01 39 39 2c 20 20 39 30 31 2c 20 20 37 39 36 2c 20  99,  901,  796, 
2da02 20 39 30 32 2c 20 20 39 30 34 2c 20 20 39 30 30   902,  904,  900
2da03 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20  ,. /*   580 */  
2da04 20 39 32 39 2c 20 20 36 34 33 2c 20 20 37 32 32   929,  643,  722
2da05 2c 20 20 37 32 33 2c 20 20 37 32 36 2c 20 20 38  ,  723,  726,  8
2da06 36 38 2c 20 20 39 31 31 2c 20 20 37 38 32 2c 20  68,  911,  782, 
2da07 20 37 38 35 2c 20 20 37 38 38 2c 0a 20 2f 2a 20   785,  788,. /* 
2da08 20 20 35 39 30 20 2a 2f 20 20 20 37 39 30 2c 20    590 */   790, 
2da09 20 38 37 30 2c 20 20 38 37 32 2c 20 20 38 37 34   870,  872,  874
2da0a 2c 20 20 38 37 36 2c 20 20 38 37 37 2c 20 20 38  ,  876,  877,  8
2da0b 37 38 2c 20 20 38 37 39 2c 20 20 38 38 30 2c 20  78,  879,  880, 
2da0c 20 38 37 33 2c 0a 20 2f 2a 20 20 20 36 30 30 20   873,. /*   600 
2da0d 2a 2f 20 20 20 38 37 35 2c 20 20 39 31 32 2c 20  */   875,  912, 
2da0e 20 39 31 36 2c 20 20 39 31 39 2c 20 20 39 32 31   916,  919,  921
2da0f 2c 20 20 39 32 32 2c 20 20 39 32 33 2c 20 20 39  ,  922,  923,  9
2da10 32 36 2c 20 20 39 32 38 2c 20 20 39 33 33 2c 0a  26,  928,  933,.
2da11 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 39   /*   610 */   9
2da12 33 34 2c 20 20 39 33 35 2c 20 20 39 33 38 2c 20  34,  935,  938, 
2da13 20 39 33 36 2c 20 20 36 32 33 2c 20 20 36 31 38   936,  623,  618
2da14 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f  ,.};.#define YY_
2da15 53 5a 5f 41 43 54 54 41 42 20 28 69 6e 74 29 28  SZ_ACTTAB (int)(
2da16 73 69 7a 65 6f 66 28 79 79 5f 61 63 74 69 6f 6e  sizeof(yy_action
2da17 29 2f 73 69 7a 65 6f 66 28 79 79 5f 61 63 74 69  )/sizeof(yy_acti
2da18 6f 6e 5b 30 5d 29 29 0a 0a 2f 2a 20 54 68 65 20  on[0]))../* The 
2da19 6e 65 78 74 20 74 61 62 6c 65 20 6d 61 70 73 20  next table maps 
2da1a 74 6f 6b 65 6e 73 20 69 6e 74 6f 20 66 61 6c 6c  tokens into fall
2da1b 62 61 63 6b 20 74 6f 6b 65 6e 73 2e 20 20 49 66  back tokens.  If
2da1c 20 61 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20   a construct.** 
2da1d 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  like the followi
2da1e 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 20  ng:.** .**      
2da1f 25 66 61 6c 6c 62 61 63 6b 20 49 44 20 58 20 59  %fallback ID X Y
2da20 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61 70 70 65 61 72   Z..**.** appear
2da21 73 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72  s in the grammar
2da22 2c 20 74 68 65 6e 20 49 44 20 62 65 63 6f 6d 65  , then ID become
2da23 73 20 61 20 66 61 6c 6c 62 61 63 6b 20 74 6f 6b  s a fallback tok
2da24 65 6e 20 66 6f 72 20 58 2c 20 59 2c 0a 2a 2a 20  en for X, Y,.** 
2da25 61 6e 64 20 5a 2e 20 20 57 68 65 6e 65 76 65 72  and Z.  Whenever
2da26 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 6f 6b 65   one of the toke
2da27 6e 73 20 58 2c 20 59 2c 20 6f 72 20 5a 20 69 73  ns X, Y, or Z is
2da28 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 70 61   input to the pa
2da29 72 73 65 72 0a 2a 2a 20 62 75 74 20 69 74 20 64  rser.** but it d
2da2a 6f 65 73 20 6e 6f 74 20 70 61 72 73 65 2c 20 74  oes not parse, t
2da2b 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20 74  he type of the t
2da2c 6f 6b 65 6e 20 69 73 20 63 68 61 6e 67 65 64 20  oken is changed 
2da2d 74 6f 20 49 44 20 61 6e 64 0a 2a 2a 20 74 68 65  to ID and.** the
2da2e 20 70 61 72 73 65 20 69 73 20 72 65 74 72 69 65   parse is retrie
2da2f 64 20 62 65 66 6f 72 65 20 61 6e 20 65 72 72 6f  d before an erro
2da30 72 20 69 73 20 74 68 72 6f 77 6e 2e 0a 2a 2f 0a  r is thrown..*/.
2da31 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41 43  #ifdef YYFALLBAC
2da32 4b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59  K.static const Y
2da33 59 43 4f 44 45 54 59 50 45 20 79 79 46 61 6c 6c  YCODETYPE yyFall
2da34 62 61 63 6b 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  back[] = {.    0
2da35 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 20 24  ,  /*          $
2da36 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2da37 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2da38 53 45 4d 49 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  SEMI => nothing 
2da39 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2da3a 20 45 58 50 4c 41 49 4e 20 3d 3e 20 49 44 20 2a   EXPLAIN => ID *
2da3b 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2da3c 20 20 51 55 45 52 59 20 3d 3e 20 49 44 20 2a 2f    QUERY => ID */
2da3d 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2da3e 20 20 50 4c 41 4e 20 3d 3e 20 49 44 20 2a 2f 0a    PLAN => ID */.
2da3f 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
2da40 42 45 47 49 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20  BEGIN => ID */. 
2da41 20 20 20 30 2c 20 20 2f 2a 20 54 52 41 4e 53 41     0,  /* TRANSA
2da42 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67  CTION => nothing
2da43 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2da44 20 44 45 46 45 52 52 45 44 20 3d 3e 20 49 44 20   DEFERRED => ID 
2da45 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 49  */.   26,  /*  I
2da46 4d 4d 45 44 49 41 54 45 20 3d 3e 20 49 44 20 2a  MMEDIATE => ID *
2da47 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 45 58  /.   26,  /*  EX
2da48 43 4c 55 53 49 56 45 20 3d 3e 20 49 44 20 2a 2f  CLUSIVE => ID */
2da49 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
2da4a 43 4f 4d 4d 49 54 20 3d 3e 20 6e 6f 74 68 69 6e  COMMIT => nothin
2da4b 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  g */.   26,  /* 
2da4c 20 20 20 20 20 20 20 45 4e 44 20 3d 3e 20 49 44         END => ID
2da4d 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2da4e 20 52 4f 4c 4c 42 41 43 4b 20 3d 3e 20 49 44 20   ROLLBACK => ID 
2da4f 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 53  */.   26,  /*  S
2da50 41 56 45 50 4f 49 4e 54 20 3d 3e 20 49 44 20 2a  AVEPOINT => ID *
2da51 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2da52 52 45 4c 45 41 53 45 20 3d 3e 20 49 44 20 2a 2f  RELEASE => ID */
2da53 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
2da54 20 20 20 20 54 4f 20 3d 3e 20 6e 6f 74 68 69 6e      TO => nothin
2da55 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  g */.    0,  /* 
2da56 20 20 20 20 20 54 41 42 4c 45 20 3d 3e 20 6e 6f       TABLE => no
2da57 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
2da58 20 2f 2a 20 20 20 20 20 43 52 45 41 54 45 20 3d   /*     CREATE =
2da59 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
2da5a 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20  26,  /*         
2da5b 49 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20  IF => ID */.    
2da5c 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 4e 4f  0,  /*        NO
2da5d 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  T => nothing */.
2da5e 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45      0,  /*     E
2da5f 58 49 53 54 53 20 3d 3e 20 6e 6f 74 68 69 6e 67  XISTS => nothing
2da60 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2da61 20 20 20 20 20 54 45 4d 50 20 3d 3e 20 49 44 20       TEMP => ID 
2da62 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2da63 20 20 20 20 20 20 4c 50 20 3d 3e 20 6e 6f 74 68        LP => noth
2da64 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2da65 2a 20 20 20 20 20 20 20 20 20 52 50 20 3d 3e 20  *         RP => 
2da66 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2da67 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 41 53  ,  /*         AS
2da68 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2da69 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 43     0,  /*      C
2da6a 4f 4d 4d 41 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  OMMA => nothing 
2da6b 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2da6c 20 20 20 20 20 20 49 44 20 3d 3e 20 6e 6f 74 68        ID => noth
2da6d 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2da6e 2a 20 20 20 20 49 4e 44 45 58 45 44 20 3d 3e 20  *    INDEXED => 
2da6f 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36  nothing */.   26
2da70 2c 20 20 2f 2a 20 20 20 20 20 20 41 42 4f 52 54  ,  /*      ABORT
2da71 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2da72 20 20 2f 2a 20 20 20 20 20 20 41 46 54 45 52 20    /*      AFTER 
2da73 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2da74 20 2f 2a 20 20 20 20 41 4e 41 4c 59 5a 45 20 3d   /*    ANALYZE =
2da75 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2da76 2f 2a 20 20 20 20 20 20 20 20 41 53 43 20 3d 3e  /*        ASC =>
2da77 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2da78 2a 20 20 20 20 20 41 54 54 41 43 48 20 3d 3e 20  *     ATTACH => 
2da79 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2da7a 20 20 20 20 20 42 45 46 4f 52 45 20 3d 3e 20 49       BEFORE => I
2da7b 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2da7c 20 20 20 20 20 20 20 20 42 59 20 3d 3e 20 49 44          BY => ID
2da7d 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2da7e 20 20 43 41 53 43 41 44 45 20 3d 3e 20 49 44 20    CASCADE => ID 
2da7f 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2da80 20 20 20 20 43 41 53 54 20 3d 3e 20 49 44 20 2a      CAST => ID *
2da81 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43  /.   26,  /*   C
2da82 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 49 44 20 2a 2f  OLUMNKW => ID */
2da83 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 43 4f  .   26,  /*   CO
2da84 4e 46 4c 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a  NFLICT => ID */.
2da85 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 44 41 54     26,  /*   DAT
2da86 41 42 41 53 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  ABASE => ID */. 
2da87 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
2da88 44 45 53 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  DESC => ID */.  
2da89 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 44 45 54   26,  /*     DET
2da8a 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ACH => ID */.   
2da8b 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 45 41  26,  /*       EA
2da8c 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  CH => ID */.   2
2da8d 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 46 41 49  6,  /*       FAI
2da8e 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  L => ID */.   26
2da8f 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 46 4f 52  ,  /*        FOR
2da90 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2da91 20 20 2f 2a 20 20 20 20 20 49 47 4e 4f 52 45 20    /*     IGNORE 
2da92 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2da93 20 2f 2a 20 20 49 4e 49 54 49 41 4c 4c 59 20 3d   /*  INITIALLY =
2da94 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2da95 2f 2a 20 20 20 20 49 4e 53 54 45 41 44 20 3d 3e  /*    INSTEAD =>
2da96 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2da97 2a 20 20 20 20 4c 49 4b 45 5f 4b 57 20 3d 3e 20  *    LIKE_KW => 
2da98 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2da99 20 20 20 20 20 20 4d 41 54 43 48 20 3d 3e 20 49        MATCH => I
2da9a 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2da9b 20 20 20 20 20 20 20 4b 45 59 20 3d 3e 20 49 44         KEY => ID
2da9c 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2da9d 20 20 20 20 20 20 20 4f 46 20 3d 3e 20 49 44 20         OF => ID 
2da9e 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2da9f 20 20 4f 46 46 53 45 54 20 3d 3e 20 49 44 20 2a    OFFSET => ID *
2daa0 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2daa1 20 50 52 41 47 4d 41 20 3d 3e 20 49 44 20 2a 2f   PRAGMA => ID */
2daa2 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2daa3 20 52 41 49 53 45 20 3d 3e 20 49 44 20 2a 2f 0a   RAISE => ID */.
2daa4 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 52 45     26,  /*    RE
2daa5 50 4c 41 43 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  PLACE => ID */. 
2daa6 20 20 32 36 2c 20 20 2f 2a 20 20 20 52 45 53 54    26,  /*   REST
2daa7 52 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  RICT => ID */.  
2daa8 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
2daa9 52 4f 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ROW => ID */.   
2daaa 32 36 2c 20 20 2f 2a 20 20 20 20 54 52 49 47 47  26,  /*    TRIGG
2daab 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ER => ID */.   2
2daac 36 2c 20 20 2f 2a 20 20 20 20 20 56 41 43 55 55  6,  /*     VACUU
2daad 4d 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  M => ID */.   26
2daae 2c 20 20 2f 2a 20 20 20 20 20 20 20 56 49 45 57  ,  /*       VIEW
2daaf 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2dab0 20 20 2f 2a 20 20 20 20 56 49 52 54 55 41 4c 20    /*    VIRTUAL 
2dab1 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2dab2 20 2f 2a 20 20 20 20 52 45 49 4e 44 45 58 20 3d   /*    REINDEX =
2dab3 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2dab4 2f 2a 20 20 20 20 20 52 45 4e 41 4d 45 20 3d 3e  /*     RENAME =>
2dab5 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2dab6 2a 20 20 20 43 54 49 4d 45 5f 4b 57 20 3d 3e 20  *   CTIME_KW => 
2dab7 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
2dab8 20 20 20 20 20 20 20 20 41 4e 59 20 3d 3e 20 6e          ANY => n
2dab9 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2daba 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4f 52 20    /*         OR 
2dabb 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2dabc 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
2dabd 41 4e 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  AND => nothing *
2dabe 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2dabf 20 20 20 20 20 49 53 20 3d 3e 20 6e 6f 74 68 69       IS => nothi
2dac0 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2dac1 20 20 20 20 42 45 54 57 45 45 4e 20 3d 3e 20 6e      BETWEEN => n
2dac2 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2dac3 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49 4e 20    /*         IN 
2dac4 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2dac5 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 49 53 4e    0,  /*     ISN
2dac6 55 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ULL => nothing *
2dac7 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2dac8 4e 4f 54 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68 69  NOTNULL => nothi
2dac9 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2daca 20 20 20 20 20 20 20 20 20 4e 45 20 3d 3e 20 6e           NE => n
2dacb 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2dacc 20 20 2f 2a 20 20 20 20 20 20 20 20 20 45 51 20    /*         EQ 
2dacd 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2dace 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
2dacf 20 47 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a   GT => nothing *
2dad0 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2dad1 20 20 20 20 20 4c 45 20 3d 3e 20 6e 6f 74 68 69       LE => nothi
2dad2 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2dad3 20 20 20 20 20 20 20 20 20 4c 54 20 3d 3e 20 6e           LT => n
2dad4 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2dad5 20 20 2f 2a 20 20 20 20 20 20 20 20 20 47 45 20    /*         GE 
2dad6 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2dad7 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45 53 43    0,  /*     ESC
2dad8 41 50 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  APE => nothing *
2dad9 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2dada 20 42 49 54 41 4e 44 20 3d 3e 20 6e 6f 74 68 69   BITAND => nothi
2dadb 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2dadc 20 20 20 20 20 20 42 49 54 4f 52 20 3d 3e 20 6e        BITOR => n
2dadd 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2dade 20 20 2f 2a 20 20 20 20 20 4c 53 48 49 46 54 20    /*     LSHIFT 
2dadf 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2dae0 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 52 53 48    0,  /*     RSH
2dae1 49 46 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  IFT => nothing *
2dae2 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2dae3 20 20 20 50 4c 55 53 20 3d 3e 20 6e 6f 74 68 69     PLUS => nothi
2dae4 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2dae5 20 20 20 20 20 20 4d 49 4e 55 53 20 3d 3e 20 6e        MINUS => n
2dae6 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2dae7 20 20 2f 2a 20 20 20 20 20 20 20 53 54 41 52 20    /*       STAR 
2dae8 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2dae9 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 53 4c    0,  /*      SL
2daea 41 53 48 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ASH => nothing *
2daeb 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2daec 20 20 20 20 52 45 4d 20 3d 3e 20 6e 6f 74 68 69      REM => nothi
2daed 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2daee 20 20 20 20 20 43 4f 4e 43 41 54 20 3d 3e 20 6e       CONCAT => n
2daef 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2daf0 20 20 2f 2a 20 20 20 20 43 4f 4c 4c 41 54 45 20    /*    COLLATE 
2daf1 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2daf2 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 55 4d 49    0,  /*     UMI
2daf3 4e 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  NUS => nothing *
2daf4 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2daf5 20 20 55 50 4c 55 53 20 3d 3e 20 6e 6f 74 68 69    UPLUS => nothi
2daf6 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2daf7 20 20 20 20 20 42 49 54 4e 4f 54 20 3d 3e 20 6e       BITNOT => n
2daf8 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2daf9 20 20 2f 2a 20 20 20 20 20 53 54 52 49 4e 47 20    /*     STRING 
2dafa 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2dafb 20 20 30 2c 20 20 2f 2a 20 20 20 20 4a 4f 49 4e    0,  /*    JOIN
2dafc 5f 4b 57 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  _KW => nothing *
2dafd 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 43 4f 4e  /.    0,  /* CON
2dafe 53 54 52 41 49 4e 54 20 3d 3e 20 6e 6f 74 68 69  STRAINT => nothi
2daff 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db00 20 20 20 20 44 45 46 41 55 4c 54 20 3d 3e 20 6e      DEFAULT => n
2db01 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db02 20 20 2f 2a 20 20 20 20 20 20 20 4e 55 4c 4c 20    /*       NULL 
2db03 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db04 20 20 30 2c 20 20 2f 2a 20 20 20 20 50 52 49 4d    0,  /*    PRIM
2db05 41 52 59 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ARY => nothing *
2db06 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db07 20 55 4e 49 51 55 45 20 3d 3e 20 6e 6f 74 68 69   UNIQUE => nothi
2db08 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db09 20 20 20 20 20 20 43 48 45 43 4b 20 3d 3e 20 6e        CHECK => n
2db0a 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db0b 20 20 2f 2a 20 52 45 46 45 52 45 4e 43 45 53 20    /* REFERENCES 
2db0c 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db0d 20 20 30 2c 20 20 2f 2a 20 20 20 41 55 54 4f 49    0,  /*   AUTOI
2db0e 4e 43 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  NCR => nothing *
2db0f 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db10 20 20 20 20 20 4f 4e 20 3d 3e 20 6e 6f 74 68 69       ON => nothi
2db11 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db12 20 20 20 20 20 44 45 4c 45 54 45 20 3d 3e 20 6e       DELETE => n
2db13 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db14 20 20 2f 2a 20 20 20 20 20 55 50 44 41 54 45 20    /*     UPDATE 
2db15 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db16 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 49 4e 53    0,  /*     INS
2db17 45 52 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ERT => nothing *
2db18 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db19 20 20 20 20 53 45 54 20 3d 3e 20 6e 6f 74 68 69      SET => nothi
2db1a 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db1b 20 44 45 46 45 52 52 41 42 4c 45 20 3d 3e 20 6e   DEFERRABLE => n
2db1c 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db1d 20 20 2f 2a 20 20 20 20 46 4f 52 45 49 47 4e 20    /*    FOREIGN 
2db1e 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db1f 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 44    0,  /*       D
2db20 52 4f 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ROP => nothing *
2db21 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db22 20 20 55 4e 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69    UNION => nothi
2db23 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db24 20 20 20 20 20 20 20 20 41 4c 4c 20 3d 3e 20 6e          ALL => n
2db25 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db26 20 20 2f 2a 20 20 20 20 20 45 58 43 45 50 54 20    /*     EXCEPT 
2db27 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db28 20 20 30 2c 20 20 2f 2a 20 20 49 4e 54 45 52 53    0,  /*  INTERS
2db29 45 43 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ECT => nothing *
2db2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db2b 20 53 45 4c 45 43 54 20 3d 3e 20 6e 6f 74 68 69   SELECT => nothi
2db2c 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db2d 20 20 20 44 49 53 54 49 4e 43 54 20 3d 3e 20 6e     DISTINCT => n
2db2e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db2f 20 20 2f 2a 20 20 20 20 20 20 20 20 44 4f 54 20    /*        DOT 
2db30 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db31 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 46    0,  /*       F
2db32 52 4f 4d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ROM => nothing *
2db33 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db34 20 20 20 4a 4f 49 4e 20 3d 3e 20 6e 6f 74 68 69     JOIN => nothi
2db35 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db36 20 20 20 20 20 20 55 53 49 4e 47 20 3d 3e 20 6e        USING => n
2db37 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db38 20 20 2f 2a 20 20 20 20 20 20 4f 52 44 45 52 20    /*      ORDER 
2db39 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db3a 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 47 52    0,  /*      GR
2db3b 4f 55 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  OUP => nothing *
2db3c 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db3d 20 48 41 56 49 4e 47 20 3d 3e 20 6e 6f 74 68 69   HAVING => nothi
2db3e 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db3f 20 20 20 20 20 20 4c 49 4d 49 54 20 3d 3e 20 6e        LIMIT => n
2db40 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db41 20 20 2f 2a 20 20 20 20 20 20 57 48 45 52 45 20    /*      WHERE 
2db42 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db43 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 49    0,  /*       I
2db44 4e 54 4f 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  NTO => nothing *
2db45 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db46 20 56 41 4c 55 45 53 20 3d 3e 20 6e 6f 74 68 69   VALUES => nothi
2db47 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db48 20 20 20 20 49 4e 54 45 47 45 52 20 3d 3e 20 6e      INTEGER => n
2db49 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db4a 20 20 2f 2a 20 20 20 20 20 20 46 4c 4f 41 54 20    /*      FLOAT 
2db4b 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db4c 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 42    0,  /*       B
2db4d 4c 4f 42 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  LOB => nothing *
2db4e 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 52  /.    0,  /*   R
2db4f 45 47 49 53 54 45 52 20 3d 3e 20 6e 6f 74 68 69  EGISTER => nothi
2db50 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db51 20 20 20 56 41 52 49 41 42 4c 45 20 3d 3e 20 6e     VARIABLE => n
2db52 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db53 20 20 2f 2a 20 20 20 20 20 20 20 43 41 53 45 20    /*       CASE 
2db54 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db55 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 57    0,  /*       W
2db56 48 45 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  HEN => nothing *
2db57 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db58 20 20 20 54 48 45 4e 20 3d 3e 20 6e 6f 74 68 69     THEN => nothi
2db59 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2db5a 20 20 20 20 20 20 20 45 4c 53 45 20 3d 3e 20 6e         ELSE => n
2db5b 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2db5c 20 20 2f 2a 20 20 20 20 20 20 49 4e 44 45 58 20    /*      INDEX 
2db5d 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2db5e 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 41 4c    0,  /*      AL
2db5f 54 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  TER => nothing *
2db60 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2db61 20 20 20 20 41 44 44 20 3d 3e 20 6e 6f 74 68 69      ADD => nothi
2db62 6e 67 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 20  ng */.};.#endif 
2db63 2f 2a 20 59 59 46 41 4c 4c 42 41 43 4b 20 2a 2f  /* YYFALLBACK */
2db64 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
2db65 6e 67 20 73 74 72 75 63 74 75 72 65 20 72 65 70  ng structure rep
2db66 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65  resents a single
2db67 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a   element of the.
2db68 2a 2a 20 70 61 72 73 65 72 27 73 20 73 74 61 63  ** parser's stac
2db69 6b 2e 20 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20  k.  Information 
2db6a 73 74 6f 72 65 64 20 69 6e 63 6c 75 64 65 73 3a  stored includes:
2db6b 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20  .**.**   +  The 
2db6c 73 74 61 74 65 20 6e 75 6d 62 65 72 20 66 6f 72  state number for
2db6d 20 74 68 65 20 70 61 72 73 65 72 20 61 74 20 74   the parser at t
2db6e 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  his level of the
2db6f 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20   stack..**.**   
2db70 2b 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  +  The value of 
2db71 74 68 65 20 74 6f 6b 65 6e 20 73 74 6f 72 65 64  the token stored
2db72 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f   at this level o
2db73 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20  f the stack..** 
2db74 20 20 20 20 20 28 49 6e 20 6f 74 68 65 72 20 77       (In other w
2db75 6f 72 64 73 2c 20 74 68 65 20 22 6d 61 6a 6f 72  ords, the "major
2db76 22 20 74 6f 6b 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20  " token.).**.** 
2db77 20 20 2b 20 20 54 68 65 20 73 65 6d 61 6e 74 69    +  The semanti
2db78 63 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 61  c value stored a
2db79 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20  t this level of 
2db7a 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 69 73  the stack.  This
2db7b 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 68 65 20   is.**      the 
2db7c 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64  information used
2db7d 20 62 79 20 74 68 65 20 61 63 74 69 6f 6e 20 72   by the action r
2db7e 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 67  outines in the g
2db7f 72 61 6d 6d 61 72 2e 0a 2a 2a 20 20 20 20 20 20  rammar..**      
2db80 49 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 20  It is sometimes 
2db81 63 61 6c 6c 65 64 20 74 68 65 20 22 6d 69 6e 6f  called the "mino
2db82 72 22 20 74 6f 6b 65 6e 2e 0a 2a 2f 0a 73 74 72  r" token..*/.str
2db83 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72 79  uct yyStackEntry
2db84 20 7b 0a 20 20 59 59 41 43 54 49 4f 4e 54 59 50   {.  YYACTIONTYP
2db85 45 20 73 74 61 74 65 6e 6f 3b 20 20 2f 2a 20 54  E stateno;  /* T
2db86 68 65 20 73 74 61 74 65 2d 6e 75 6d 62 65 72 20  he state-number 
2db87 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  */.  YYCODETYPE 
2db88 6d 61 6a 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54  major;      /* T
2db89 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 76  he major token v
2db8a 61 6c 75 65 2e 20 20 54 68 69 73 20 69 73 20 74  alue.  This is t
2db8b 68 65 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20  he code.        
2db8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db8d 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74   ** number for t
2db8e 68 65 20 74 6f 6b 65 6e 20 61 74 20 74 68 69 73  he token at this
2db8f 20 73 74 61 63 6b 20 6c 65 76 65 6c 20 2a 2f 0a   stack level */.
2db90 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6d 69    YYMINORTYPE mi
2db91 6e 6f 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  nor;     /* The 
2db92 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20 6d 69  user-supplied mi
2db93 6e 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e  nor token value.
2db94 20 20 54 68 69 73 0a 20 20 20 20 20 20 20 20 20    This.         
2db95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db96 2a 2a 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  ** is the value 
2db97 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 20 2a 2f  of the token  */
2db98 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  .};.typedef stru
2db99 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  ct yyStackEntry 
2db9a 79 79 53 74 61 63 6b 45 6e 74 72 79 3b 0a 0a 2f  yyStackEntry;../
2db9b 2a 20 54 68 65 20 73 74 61 74 65 20 6f 66 20 74  * The state of t
2db9c 68 65 20 70 61 72 73 65 72 20 69 73 20 63 6f 6d  he parser is com
2db9d 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61 69 6e 65  pletely containe
2db9e 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65  d in an instance
2db9f 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f   of.** the follo
2dba0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 2a  wing structure *
2dba1 2f 0a 73 74 72 75 63 74 20 79 79 50 61 72 73 65  /.struct yyParse
2dba2 72 20 7b 0a 20 20 69 6e 74 20 79 79 69 64 78 3b  r {.  int yyidx;
2dba3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dba4 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2dba5 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e 20 73  top element in s
2dba6 74 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66 20 59  tack */.#ifdef Y
2dba7 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45  YTRACKMAXSTACKDE
2dba8 50 54 48 0a 20 20 69 6e 74 20 79 79 69 64 78 4d  PTH.  int yyidxM
2dba9 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ax;             
2dbaa 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76      /* Maximum v
2dbab 61 6c 75 65 20 6f 66 20 79 79 69 64 78 20 2a 2f  alue of yyidx */
2dbac 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 79 79  .#endif.  int yy
2dbad 65 72 72 63 6e 74 3b 20 20 20 20 20 20 20 20 20  errcnt;         
2dbae 20 20 20 20 20 20 20 20 2f 2a 20 53 68 69 66 74          /* Shift
2dbaf 73 20 6c 65 66 74 20 62 65 66 6f 72 65 20 6f 75  s left before ou
2dbb0 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 2a  t of the error *
2dbb1 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
2dbb2 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20 20  rARG_SDECL      
2dbb3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70            /* A p
2dbb4 6c 61 63 65 20 74 6f 20 68 6f 6c 64 20 25 65 78  lace to hold %ex
2dbb5 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  tra_argument */.
2dbb6 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48  #if YYSTACKDEPTH
2dbb7 3c 3d 30 0a 20 20 69 6e 74 20 79 79 73 74 6b 73  <=0.  int yystks
2dbb8 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
2dbb9 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73      /* Current s
2dbba 69 64 65 20 6f 66 20 74 68 65 20 73 74 61 63 6b  ide of the stack
2dbbb 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e 74   */.  yyStackEnt
2dbbc 72 79 20 2a 79 79 73 74 61 63 6b 3b 20 20 20 20  ry *yystack;    
2dbbd 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
2dbbe 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6c  r's stack */.#el
2dbbf 73 65 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72  se.  yyStackEntr
2dbc0 79 20 79 79 73 74 61 63 6b 5b 59 59 53 54 41 43  y yystack[YYSTAC
2dbc1 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20 54 68 65  KDEPTH];  /* The
2dbc2 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 20   parser's stack 
2dbc3 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70  */.#endif.};.typ
2dbc4 65 64 65 66 20 73 74 72 75 63 74 20 79 79 50 61  edef struct yyPa
2dbc5 72 73 65 72 20 79 79 50 61 72 73 65 72 3b 0a 0a  rser yyParser;..
2dbc6 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73  #ifndef NDEBUG.s
2dbc7 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79 54 72  tatic FILE *yyTr
2dbc8 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 73 74 61  aceFILE = 0;.sta
2dbc9 74 69 63 20 63 68 61 72 20 2a 79 79 54 72 61 63  tic char *yyTrac
2dbca 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 23 65 6e  ePrompt = 0;.#en
2dbcb 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
2dbcc 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
2dbcd 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20 70 61 72  ./* .** Turn par
2dbce 73 65 72 20 74 72 61 63 69 6e 67 20 6f 6e 20 62  ser tracing on b
2dbcf 79 20 67 69 76 69 6e 67 20 61 20 73 74 72 65 61  y giving a strea
2dbd0 6d 20 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  m to which to wr
2dbd1 69 74 65 20 74 68 65 20 74 72 61 63 65 0a 2a 2a  ite the trace.**
2dbd2 20 61 6e 64 20 61 20 70 72 6f 6d 70 74 20 74 6f   and a prompt to
2dbd3 20 70 72 65 66 61 63 65 20 65 61 63 68 20 74 72   preface each tr
2dbd4 61 63 65 20 6d 65 73 73 61 67 65 2e 20 20 54 72  ace message.  Tr
2dbd5 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20  acing is turned 
2dbd6 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67  off.** by making
2dbd7 20 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e 74   either argument
2dbd8 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49 6e 70   NULL .**.** Inp
2dbd9 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  uts:.** <ul>.** 
2dbda 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20 74 6f 20  <li> A FILE* to 
2dbdb 77 68 69 63 68 20 74 72 61 63 65 20 6f 75 74 70  which trace outp
2dbdc 75 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69  ut should be wri
2dbdd 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 49 66  tten..**      If
2dbde 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63   NULL, then trac
2dbdf 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 66  ing is turned of
2dbe0 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 72 65  f..** <li> A pre
2dbe1 66 69 78 20 73 74 72 69 6e 67 20 77 72 69 74 74  fix string writt
2dbe2 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  en at the beginn
2dbe3 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20  ing of every.** 
2dbe4 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74 72 61       line of tra
2dbe5 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66 20 4e  ce output.  If N
2dbe6 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69 6e  ULL, then tracin
2dbe7 67 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 75 72  g is.**      tur
2dbe8 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 2f 75 6c  ned off..** </ul
2dbe9 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  >.**.** Outputs:
2dbea 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c  .** None..*/.SQL
2dbeb 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2dbec 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
2dbed 61 63 65 28 46 49 4c 45 20 2a 54 72 61 63 65 46  ace(FILE *TraceF
2dbee 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54 72 61 63  ILE, char *zTrac
2dbef 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79 79 54 72  ePrompt){.  yyTr
2dbf0 61 63 65 46 49 4c 45 20 3d 20 54 72 61 63 65 46  aceFILE = TraceF
2dbf1 49 4c 45 3b 0a 20 20 79 79 54 72 61 63 65 50 72  ILE;.  yyTracePr
2dbf2 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65 50 72 6f  ompt = zTracePro
2dbf3 6d 70 74 3b 0a 20 20 69 66 28 20 79 79 54 72 61  mpt;.  if( yyTra
2dbf4 63 65 46 49 4c 45 3d 3d 30 20 29 20 79 79 54 72  ceFILE==0 ) yyTr
2dbf5 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 20  acePrompt = 0;. 
2dbf6 20 65 6c 73 65 20 69 66 28 20 79 79 54 72 61 63   else if( yyTrac
2dbf7 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20 79 79 54  ePrompt==0 ) yyT
2dbf8 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 7d 0a  raceFILE = 0;.}.
2dbf9 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
2dbfa 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45   */..#ifndef NDE
2dbfb 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69  BUG./* For traci
2dbfc 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65 20 6e  ng shifts, the n
2dbfd 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65 72 6d  ames of all term
2dbfe 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74 65 72  inals and nonter
2dbff 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65 20 72 65  minals.** are re
2dc00 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 6f 6c  quired.  The fol
2dc01 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73 75 70  lowing table sup
2dc02 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61 6d 65  plies these name
2dc03 73 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  s */.static cons
2dc04 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79 79  t char *const yy
2dc05 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20 7b 20  TokenName[] = { 
2dc06 0a 20 20 22 24 22 2c 20 20 20 20 20 20 20 20 20  .  "$",         
2dc07 20 20 20 20 22 53 45 4d 49 22 2c 20 20 20 20 20      "SEMI",     
2dc08 20 20 20 20 20 22 45 58 50 4c 41 49 4e 22 2c 20       "EXPLAIN", 
2dc09 20 20 20 20 20 20 22 51 55 45 52 59 22 2c 20 20        "QUERY",  
2dc0a 20 20 20 20 20 0a 20 20 22 50 4c 41 4e 22 2c 20       .  "PLAN", 
2dc0b 20 20 20 20 20 20 20 20 20 22 42 45 47 49 4e 22           "BEGIN"
2dc0c 2c 20 20 20 20 20 20 20 20 20 22 54 52 41 4e 53  ,         "TRANS
2dc0d 41 43 54 49 4f 4e 22 2c 20 20 20 22 44 45 46 45  ACTION",   "DEFE
2dc0e 52 52 45 44 22 2c 20 20 20 20 0a 20 20 22 49 4d  RRED",    .  "IM
2dc0f 4d 45 44 49 41 54 45 22 2c 20 20 20 20 20 22 45  MEDIATE",     "E
2dc10 58 43 4c 55 53 49 56 45 22 2c 20 20 20 20 20 22  XCLUSIVE",     "
2dc11 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20 20 20 20  COMMIT",        
2dc12 22 45 4e 44 22 2c 20 20 20 20 20 20 20 20 20 0a  "END",         .
2dc13 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 20 20    "ROLLBACK",   
2dc14 20 20 20 22 53 41 56 45 50 4f 49 4e 54 22 2c 20     "SAVEPOINT", 
2dc15 20 20 20 20 22 52 45 4c 45 41 53 45 22 2c 20 20      "RELEASE",  
2dc16 20 20 20 20 20 22 54 4f 22 2c 20 20 20 20 20 20       "TO",      
2dc17 20 20 20 20 0a 20 20 22 54 41 42 4c 45 22 2c 20      .  "TABLE", 
2dc18 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45 22          "CREATE"
2dc19 2c 20 20 20 20 20 20 20 20 22 49 46 22 2c 20 20  ,        "IF",  
2dc1a 20 20 20 20 20 20 20 20 20 20 22 4e 4f 54 22 2c            "NOT",
2dc1b 20 20 20 20 20 20 20 20 20 0a 20 20 22 45 58 49           .  "EXI
2dc1c 53 54 53 22 2c 20 20 20 20 20 20 20 20 22 54 45  STS",        "TE
2dc1d 4d 50 22 2c 20 20 20 20 20 20 20 20 20 20 22 4c  MP",          "L
2dc1e 50 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  P",            "
2dc1f 52 50 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20  RP",          . 
2dc20 20 22 41 53 22 2c 20 20 20 20 20 20 20 20 20 20   "AS",          
2dc21 20 20 22 43 4f 4d 4d 41 22 2c 20 20 20 20 20 20    "COMMA",      
2dc22 20 20 20 22 49 44 22 2c 20 20 20 20 20 20 20 20     "ID",        
2dc23 20 20 20 20 22 49 4e 44 45 58 45 44 22 2c 20 20      "INDEXED",  
2dc24 20 20 20 0a 20 20 22 41 42 4f 52 54 22 2c 20 20     .  "ABORT",  
2dc25 20 20 20 20 20 20 20 22 41 46 54 45 52 22 2c 20         "AFTER", 
2dc26 20 20 20 20 20 20 20 20 22 41 4e 41 4c 59 5a 45          "ANALYZE
2dc27 22 2c 20 20 20 20 20 20 20 22 41 53 43 22 2c 20  ",       "ASC", 
2dc28 20 20 20 20 20 20 20 20 0a 20 20 22 41 54 54 41          .  "ATTA
2dc29 43 48 22 2c 20 20 20 20 20 20 20 20 22 42 45 46  CH",        "BEF
2dc2a 4f 52 45 22 2c 20 20 20 20 20 20 20 20 22 42 59  ORE",        "BY
2dc2b 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 43  ",            "C
2dc2c 41 53 43 41 44 45 22 2c 20 20 20 20 20 0a 20 20  ASCADE",     .  
2dc2d 22 43 41 53 54 22 2c 20 20 20 20 20 20 20 20 20  "CAST",         
2dc2e 20 22 43 4f 4c 55 4d 4e 4b 57 22 2c 20 20 20 20   "COLUMNKW",    
2dc2f 20 20 22 43 4f 4e 46 4c 49 43 54 22 2c 20 20 20    "CONFLICT",   
2dc30 20 20 20 22 44 41 54 41 42 41 53 45 22 2c 20 20     "DATABASE",  
2dc31 20 20 0a 20 20 22 44 45 53 43 22 2c 20 20 20 20    .  "DESC",    
2dc32 20 20 20 20 20 20 22 44 45 54 41 43 48 22 2c 20        "DETACH", 
2dc33 20 20 20 20 20 20 20 22 45 41 43 48 22 2c 20 20         "EACH",  
2dc34 20 20 20 20 20 20 20 20 22 46 41 49 4c 22 2c 20          "FAIL", 
2dc35 20 20 20 20 20 20 20 0a 20 20 22 46 4f 52 22 2c         .  "FOR",
2dc36 20 20 20 20 20 20 20 20 20 20 20 22 49 47 4e 4f             "IGNO
2dc37 52 45 22 2c 20 20 20 20 20 20 20 20 22 49 4e 49  RE",        "INI
2dc38 54 49 41 4c 4c 59 22 2c 20 20 20 20 20 22 49 4e  TIALLY",     "IN
2dc39 53 54 45 41 44 22 2c 20 20 20 20 20 0a 20 20 22  STEAD",     .  "
2dc3a 4c 49 4b 45 5f 4b 57 22 2c 20 20 20 20 20 20 20  LIKE_KW",       
2dc3b 22 4d 41 54 43 48 22 2c 20 20 20 20 20 20 20 20  "MATCH",        
2dc3c 20 22 4b 45 59 22 2c 20 20 20 20 20 20 20 20 20   "KEY",         
2dc3d 20 20 22 4f 46 22 2c 20 20 20 20 20 20 20 20 20    "OF",         
2dc3e 20 0a 20 20 22 4f 46 46 53 45 54 22 2c 20 20 20   .  "OFFSET",   
2dc3f 20 20 20 20 20 22 50 52 41 47 4d 41 22 2c 20 20       "PRAGMA",  
2dc40 20 20 20 20 20 20 22 52 41 49 53 45 22 2c 20 20        "RAISE",  
2dc41 20 20 20 20 20 20 20 22 52 45 50 4c 41 43 45 22         "REPLACE"
2dc42 2c 20 20 20 20 20 0a 20 20 22 52 45 53 54 52 49  ,     .  "RESTRI
2dc43 43 54 22 2c 20 20 20 20 20 20 22 52 4f 57 22 2c  CT",      "ROW",
2dc44 20 20 20 20 20 20 20 20 20 20 20 22 54 52 49 47             "TRIG
2dc45 47 45 52 22 2c 20 20 20 20 20 20 20 22 56 41 43  GER",       "VAC
2dc46 55 55 4d 22 2c 20 20 20 20 20 20 0a 20 20 22 56  UUM",      .  "V
2dc47 49 45 57 22 2c 20 20 20 20 20 20 20 20 20 20 22  IEW",          "
2dc48 56 49 52 54 55 41 4c 22 2c 20 20 20 20 20 20 20  VIRTUAL",       
2dc49 22 52 45 49 4e 44 45 58 22 2c 20 20 20 20 20 20  "REINDEX",      
2dc4a 20 22 52 45 4e 41 4d 45 22 2c 20 20 20 20 20 20   "RENAME",      
2dc4b 0a 20 20 22 43 54 49 4d 45 5f 4b 57 22 2c 20 20  .  "CTIME_KW",  
2dc4c 20 20 20 20 22 41 4e 59 22 2c 20 20 20 20 20 20      "ANY",      
2dc4d 20 20 20 20 20 22 4f 52 22 2c 20 20 20 20 20 20       "OR",      
2dc4e 20 20 20 20 20 20 22 41 4e 44 22 2c 20 20 20 20        "AND",    
2dc4f 20 20 20 20 20 0a 20 20 22 49 53 22 2c 20 20 20       .  "IS",   
2dc50 20 20 20 20 20 20 20 20 20 22 42 45 54 57 45 45           "BETWEE
2dc51 4e 22 2c 20 20 20 20 20 20 20 22 49 4e 22 2c 20  N",       "IN", 
2dc52 20 20 20 20 20 20 20 20 20 20 20 22 49 53 4e 55             "ISNU
2dc53 4c 4c 22 2c 20 20 20 20 20 20 0a 20 20 22 4e 4f  LL",      .  "NO
2dc54 54 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20 22 4e  TNULL",       "N
2dc55 45 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  E",            "
2dc56 45 51 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  EQ",            
2dc57 22 47 54 22 2c 20 20 20 20 20 20 20 20 20 20 0a  "GT",          .
2dc58 20 20 22 4c 45 22 2c 20 20 20 20 20 20 20 20 20    "LE",         
2dc59 20 20 20 22 4c 54 22 2c 20 20 20 20 20 20 20 20     "LT",        
2dc5a 20 20 20 20 22 47 45 22 2c 20 20 20 20 20 20 20      "GE",       
2dc5b 20 20 20 20 20 22 45 53 43 41 50 45 22 2c 20 20       "ESCAPE",  
2dc5c 20 20 20 20 0a 20 20 22 42 49 54 41 4e 44 22 2c      .  "BITAND",
2dc5d 20 20 20 20 20 20 20 20 22 42 49 54 4f 52 22 2c          "BITOR",
2dc5e 20 20 20 20 20 20 20 20 20 22 4c 53 48 49 46 54           "LSHIFT
2dc5f 22 2c 20 20 20 20 20 20 20 20 22 52 53 48 49 46  ",        "RSHIF
2dc60 54 22 2c 20 20 20 20 20 20 0a 20 20 22 50 4c 55  T",      .  "PLU
2dc61 53 22 2c 20 20 20 20 20 20 20 20 20 20 22 4d 49  S",          "MI
2dc62 4e 55 53 22 2c 20 20 20 20 20 20 20 20 20 22 53  NUS",         "S
2dc63 54 41 52 22 2c 20 20 20 20 20 20 20 20 20 20 22  TAR",          "
2dc64 53 4c 41 53 48 22 2c 20 20 20 20 20 20 20 0a 20  SLASH",       . 
2dc65 20 22 52 45 4d 22 2c 20 20 20 20 20 20 20 20 20   "REM",         
2dc66 20 20 22 43 4f 4e 43 41 54 22 2c 20 20 20 20 20    "CONCAT",     
2dc67 20 20 20 22 43 4f 4c 4c 41 54 45 22 2c 20 20 20     "COLLATE",   
2dc68 20 20 20 20 22 55 4d 49 4e 55 53 22 2c 20 20 20      "UMINUS",   
2dc69 20 20 20 0a 20 20 22 55 50 4c 55 53 22 2c 20 20     .  "UPLUS",  
2dc6a 20 20 20 20 20 20 20 22 42 49 54 4e 4f 54 22 2c         "BITNOT",
2dc6b 20 20 20 20 20 20 20 20 22 53 54 52 49 4e 47 22          "STRING"
2dc6c 2c 20 20 20 20 20 20 20 20 22 4a 4f 49 4e 5f 4b  ,        "JOIN_K
2dc6d 57 22 2c 20 20 20 20 20 0a 20 20 22 43 4f 4e 53  W",     .  "CONS
2dc6e 54 52 41 49 4e 54 22 2c 20 20 20 20 22 44 45 46  TRAINT",    "DEF
2dc6f 41 55 4c 54 22 2c 20 20 20 20 20 20 20 22 4e 55  AULT",       "NU
2dc70 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22 50  LL",          "P
2dc71 52 49 4d 41 52 59 22 2c 20 20 20 20 20 0a 20 20  RIMARY",     .  
2dc72 22 55 4e 49 51 55 45 22 2c 20 20 20 20 20 20 20  "UNIQUE",       
2dc73 20 22 43 48 45 43 4b 22 2c 20 20 20 20 20 20 20   "CHECK",       
2dc74 20 20 22 52 45 46 45 52 45 4e 43 45 53 22 2c 20    "REFERENCES", 
2dc75 20 20 20 22 41 55 54 4f 49 4e 43 52 22 2c 20 20     "AUTOINCR",  
2dc76 20 20 0a 20 20 22 4f 4e 22 2c 20 20 20 20 20 20    .  "ON",      
2dc77 20 20 20 20 20 20 22 44 45 4c 45 54 45 22 2c 20        "DELETE", 
2dc78 20 20 20 20 20 20 20 22 55 50 44 41 54 45 22 2c         "UPDATE",
2dc79 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 22          "INSERT"
2dc7a 2c 20 20 20 20 20 20 0a 20 20 22 53 45 54 22 2c  ,      .  "SET",
2dc7b 20 20 20 20 20 20 20 20 20 20 20 22 44 45 46 45             "DEFE
2dc7c 52 52 41 42 4c 45 22 2c 20 20 20 20 22 46 4f 52  RRABLE",    "FOR
2dc7d 45 49 47 4e 22 2c 20 20 20 20 20 20 20 22 44 52  EIGN",       "DR
2dc7e 4f 50 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  OP",        .  "
2dc7f 55 4e 49 4f 4e 22 2c 20 20 20 20 20 20 20 20 20  UNION",         
2dc80 22 41 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20  "ALL",          
2dc81 20 22 45 58 43 45 50 54 22 2c 20 20 20 20 20 20   "EXCEPT",      
2dc82 20 20 22 49 4e 54 45 52 53 45 43 54 22 2c 20 20    "INTERSECT",  
2dc83 20 0a 20 20 22 53 45 4c 45 43 54 22 2c 20 20 20   .  "SELECT",   
2dc84 20 20 20 20 20 22 44 49 53 54 49 4e 43 54 22 2c       "DISTINCT",
2dc85 20 20 20 20 20 20 22 44 4f 54 22 2c 20 20 20 20        "DOT",    
2dc86 20 20 20 20 20 20 20 22 46 52 4f 4d 22 2c 20 20         "FROM",  
2dc87 20 20 20 20 20 20 0a 20 20 22 4a 4f 49 4e 22 2c        .  "JOIN",
2dc88 20 20 20 20 20 20 20 20 20 20 22 55 53 49 4e 47            "USING
2dc89 22 2c 20 20 20 20 20 20 20 20 20 22 4f 52 44 45  ",         "ORDE
2dc8a 52 22 2c 20 20 20 20 20 20 20 20 20 22 47 52 4f  R",         "GRO
2dc8b 55 50 22 2c 20 20 20 20 20 20 20 0a 20 20 22 48  UP",       .  "H
2dc8c 41 56 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22  AVING",        "
2dc8d 4c 49 4d 49 54 22 2c 20 20 20 20 20 20 20 20 20  LIMIT",         
2dc8e 22 57 48 45 52 45 22 2c 20 20 20 20 20 20 20 20  "WHERE",        
2dc8f 20 22 49 4e 54 4f 22 2c 20 20 20 20 20 20 20 20   "INTO",        
2dc90 0a 20 20 22 56 41 4c 55 45 53 22 2c 20 20 20 20  .  "VALUES",    
2dc91 20 20 20 20 22 49 4e 54 45 47 45 52 22 2c 20 20      "INTEGER",  
2dc92 20 20 20 20 20 22 46 4c 4f 41 54 22 2c 20 20 20       "FLOAT",   
2dc93 20 20 20 20 20 20 22 42 4c 4f 42 22 2c 20 20 20        "BLOB",   
2dc94 20 20 20 20 20 0a 20 20 22 52 45 47 49 53 54 45       .  "REGISTE
2dc95 52 22 2c 20 20 20 20 20 20 22 56 41 52 49 41 42  R",      "VARIAB
2dc96 4c 45 22 2c 20 20 20 20 20 20 22 43 41 53 45 22  LE",      "CASE"
2dc97 2c 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e  ,          "WHEN
2dc98 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 54 48  ",        .  "TH
2dc99 45 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 45  EN",          "E
2dc9a 4c 53 45 22 2c 20 20 20 20 20 20 20 20 20 20 22  LSE",          "
2dc9b 49 4e 44 45 58 22 2c 20 20 20 20 20 20 20 20 20  INDEX",         
2dc9c 22 41 4c 54 45 52 22 2c 20 20 20 20 20 20 20 0a  "ALTER",       .
2dc9d 20 20 22 41 44 44 22 2c 20 20 20 20 20 20 20 20    "ADD",        
2dc9e 20 20 20 22 65 72 72 6f 72 22 2c 20 20 20 20 20     "error",     
2dc9f 20 20 20 20 22 69 6e 70 75 74 22 2c 20 20 20 20      "input",    
2dca0 20 20 20 20 20 22 63 6d 64 6c 69 73 74 22 2c 20       "cmdlist", 
2dca1 20 20 20 20 0a 20 20 22 65 63 6d 64 22 2c 20 20      .  "ecmd",  
2dca2 20 20 20 20 20 20 20 20 22 65 78 70 6c 61 69 6e          "explain
2dca3 22 2c 20 20 20 20 20 20 20 22 63 6d 64 78 22 2c  ",       "cmdx",
2dca4 20 20 20 20 20 20 20 20 20 20 22 63 6d 64 22 2c            "cmd",
2dca5 20 20 20 20 20 20 20 20 20 0a 20 20 22 74 72 61           .  "tra
2dca6 6e 73 74 79 70 65 22 2c 20 20 20 20 20 22 74 72  nstype",     "tr
2dca7 61 6e 73 5f 6f 70 74 22 2c 20 20 20 20 20 22 6e  ans_opt",     "n
2dca8 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  m",            "
2dca9 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 22 2c 0a  savepoint_opt",.
2dcaa 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 22    "create_table"
2dcab 2c 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65  ,  "create_table
2dcac 5f 61 72 67 73 22 2c 20 20 22 63 72 65 61 74 65  _args",  "create
2dcad 6b 77 22 2c 20 20 20 20 20 20 22 74 65 6d 70 22  kw",      "temp"
2dcae 2c 20 20 20 20 20 20 20 20 0a 20 20 22 69 66 6e  ,        .  "ifn
2dcaf 6f 74 65 78 69 73 74 73 22 2c 20 20 20 22 64 62  otexists",   "db
2dcb0 6e 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 63  nm",          "c
2dcb1 6f 6c 75 6d 6e 6c 69 73 74 22 2c 20 20 20 20 22  olumnlist",    "
2dcb2 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 22 2c 0a 20  conslist_opt",. 
2dcb3 20 22 73 65 6c 65 63 74 22 2c 20 20 20 20 20 20   "select",      
2dcb4 20 20 22 63 6f 6c 75 6d 6e 22 2c 20 20 20 20 20    "column",     
2dcb5 20 20 20 22 63 6f 6c 75 6d 6e 69 64 22 2c 20 20     "columnid",  
2dcb6 20 20 20 20 22 74 79 70 65 22 2c 20 20 20 20 20      "type",     
2dcb7 20 20 20 0a 20 20 22 63 61 72 67 6c 69 73 74 22     .  "carglist"
2dcb8 2c 20 20 20 20 20 20 22 69 64 22 2c 20 20 20 20  ,      "id",    
2dcb9 20 20 20 20 20 20 20 20 22 69 64 73 22 2c 20 20          "ids",  
2dcba 20 20 20 20 20 20 20 20 20 22 74 79 70 65 74 6f           "typeto
2dcbb 6b 65 6e 22 2c 20 20 20 0a 20 20 22 74 79 70 65  ken",   .  "type
2dcbc 6e 61 6d 65 22 2c 20 20 20 20 20 20 22 73 69 67  name",      "sig
2dcbd 6e 65 64 22 2c 20 20 20 20 20 20 20 20 22 70 6c  ned",        "pl
2dcbe 75 73 5f 6e 75 6d 22 2c 20 20 20 20 20 20 22 6d  us_num",      "m
2dcbf 69 6e 75 73 5f 6e 75 6d 22 2c 20 20 20 0a 20 20  inus_num",   .  
2dcc0 22 63 61 72 67 22 2c 20 20 20 20 20 20 20 20 20  "carg",         
2dcc1 20 22 63 63 6f 6e 73 22 2c 20 20 20 20 20 20 20   "ccons",       
2dcc2 20 20 22 74 65 72 6d 22 2c 20 20 20 20 20 20 20    "term",       
2dcc3 20 20 20 22 65 78 70 72 22 2c 20 20 20 20 20 20     "expr",      
2dcc4 20 20 0a 20 20 22 6f 6e 63 6f 6e 66 22 2c 20 20    .  "onconf",  
2dcc5 20 20 20 20 20 20 22 73 6f 72 74 6f 72 64 65 72        "sortorder
2dcc6 22 2c 20 20 20 20 20 22 61 75 74 6f 69 6e 63 22  ",     "autoinc"
2dcc7 2c 20 20 20 20 20 20 20 22 69 64 78 6c 69 73 74  ,       "idxlist
2dcc8 5f 6f 70 74 22 2c 20 0a 20 20 22 72 65 66 61 72  _opt", .  "refar
2dcc9 67 73 22 2c 20 20 20 20 20 20 20 22 64 65 66 65  gs",       "defe
2dcca 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 20 20 22  r_subclause",  "
2dccb 72 65 66 61 72 67 22 2c 20 20 20 20 20 20 20 20  refarg",        
2dccc 22 72 65 66 61 63 74 22 2c 20 20 20 20 20 20 0a  "refact",      .
2dccd 20 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64    "init_deferred
2dcce 5f 70 72 65 64 5f 6f 70 74 22 2c 20 20 22 63 6f  _pred_opt",  "co
2dccf 6e 73 6c 69 73 74 22 2c 20 20 20 20 20 20 22 74  nslist",      "t
2dcd0 63 6f 6e 73 22 2c 20 20 20 20 20 20 20 20 20 22  cons",         "
2dcd1 69 64 78 6c 69 73 74 22 2c 20 20 20 20 20 0a 20  idxlist",     . 
2dcd2 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
2dcd3 65 5f 6f 70 74 22 2c 20 20 22 6f 72 63 6f 6e 66  e_opt",  "orconf
2dcd4 22 2c 20 20 20 20 20 20 20 20 22 72 65 73 6f 6c  ",        "resol
2dcd5 76 65 74 79 70 65 22 2c 20 20 20 22 72 61 69 73  vetype",   "rais
2dcd6 65 74 79 70 65 22 2c 20 20 20 0a 20 20 22 69 66  etype",   .  "if
2dcd7 65 78 69 73 74 73 22 2c 20 20 20 20 20 20 22 66  exists",      "f
2dcd8 75 6c 6c 6e 61 6d 65 22 2c 20 20 20 20 20 20 22  ullname",      "
2dcd9 6f 6e 65 73 65 6c 65 63 74 22 2c 20 20 20 20 20  oneselect",     
2dcda 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 22  "multiselect_op"
2dcdb 2c 0a 20 20 22 64 69 73 74 69 6e 63 74 22 2c 20  ,.  "distinct", 
2dcdc 20 20 20 20 20 22 73 65 6c 63 6f 6c 6c 69 73 74       "selcollist
2dcdd 22 2c 20 20 20 20 22 66 72 6f 6d 22 2c 20 20 20  ",    "from",   
2dcde 20 20 20 20 20 20 20 22 77 68 65 72 65 5f 6f 70         "where_op
2dcdf 74 22 2c 20 20 20 0a 20 20 22 67 72 6f 75 70 62  t",   .  "groupb
2dce0 79 5f 6f 70 74 22 2c 20 20 20 22 68 61 76 69 6e  y_opt",   "havin
2dce1 67 5f 6f 70 74 22 2c 20 20 20 20 22 6f 72 64 65  g_opt",    "orde
2dce2 72 62 79 5f 6f 70 74 22 2c 20 20 20 22 6c 69 6d  rby_opt",   "lim
2dce3 69 74 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 73  it_opt",   .  "s
2dce4 63 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20 22  clp",          "
2dce5 61 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  as",            
2dce6 22 73 65 6c 74 61 62 6c 69 73 74 22 2c 20 20 20  "seltablist",   
2dce7 20 22 73 74 6c 5f 70 72 65 66 69 78 22 2c 20 20   "stl_prefix",  
2dce8 0a 20 20 22 6a 6f 69 6e 6f 70 22 2c 20 20 20 20  .  "joinop",    
2dce9 20 20 20 20 22 69 6e 64 65 78 65 64 5f 6f 70 74      "indexed_opt
2dcea 22 2c 20 20 20 22 6f 6e 5f 6f 70 74 22 2c 20 20  ",   "on_opt",  
2dceb 20 20 20 20 20 20 22 75 73 69 6e 67 5f 6f 70 74        "using_opt
2dcec 22 2c 20 20 20 0a 20 20 22 6a 6f 69 6e 6f 70 32  ",   .  "joinop2
2dced 22 2c 20 20 20 20 20 20 20 22 69 6e 73 63 6f 6c  ",       "inscol
2dcee 6c 69 73 74 22 2c 20 20 20 20 22 73 6f 72 74 6c  list",    "sortl
2dcef 69 73 74 22 2c 20 20 20 20 20 20 22 73 6f 72 74  ist",      "sort
2dcf0 69 74 65 6d 22 2c 20 20 20 20 0a 20 20 22 6e 65  item",    .  "ne
2dcf1 78 70 72 6c 69 73 74 22 2c 20 20 20 20 20 22 73  xprlist",     "s
2dcf2 65 74 6c 69 73 74 22 2c 20 20 20 20 20 20 20 22  etlist",       "
2dcf3 69 6e 73 65 72 74 5f 63 6d 64 22 2c 20 20 20 20  insert_cmd",    
2dcf4 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 22  "inscollist_opt"
2dcf5 2c 0a 20 20 22 69 74 65 6d 6c 69 73 74 22 2c 20  ,.  "itemlist", 
2dcf6 20 20 20 20 20 22 65 78 70 72 6c 69 73 74 22 2c       "exprlist",
2dcf7 20 20 20 20 20 20 22 6c 69 6b 65 6f 70 22 2c 20        "likeop", 
2dcf8 20 20 20 20 20 20 20 22 65 73 63 61 70 65 22 2c         "escape",
2dcf9 20 20 20 20 20 20 0a 20 20 22 62 65 74 77 65 65        .  "betwee
2dcfa 6e 5f 6f 70 22 2c 20 20 20 20 22 69 6e 5f 6f 70  n_op",    "in_op
2dcfb 22 2c 20 20 20 20 20 20 20 20 20 22 63 61 73 65  ",         "case
2dcfc 5f 6f 70 65 72 61 6e 64 22 2c 20 20 22 63 61 73  _operand",  "cas
2dcfd 65 5f 65 78 70 72 6c 69 73 74 22 2c 0a 20 20 22  e_exprlist",.  "
2dcfe 63 61 73 65 5f 65 6c 73 65 22 2c 20 20 20 20 20  case_else",     
2dcff 22 75 6e 69 71 75 65 66 6c 61 67 22 2c 20 20 20  "uniqueflag",   
2dd00 20 22 63 6f 6c 6c 61 74 65 22 2c 20 20 20 20 20   "collate",     
2dd01 20 20 22 6e 6d 6e 75 6d 22 2c 20 20 20 20 20 20    "nmnum",      
2dd02 20 0a 20 20 22 70 6c 75 73 5f 6f 70 74 22 2c 20   .  "plus_opt", 
2dd03 20 20 20 20 20 22 6e 75 6d 62 65 72 22 2c 20 20       "number",  
2dd04 20 20 20 20 20 20 22 74 72 69 67 67 65 72 5f 64        "trigger_d
2dd05 65 63 6c 22 2c 20 20 22 74 72 69 67 67 65 72 5f  ecl",  "trigger_
2dd06 63 6d 64 5f 6c 69 73 74 22 2c 0a 20 20 22 74 72  cmd_list",.  "tr
2dd07 69 67 67 65 72 5f 74 69 6d 65 22 2c 20 20 22 74  igger_time",  "t
2dd08 72 69 67 67 65 72 5f 65 76 65 6e 74 22 2c 20 20  rigger_event",  
2dd09 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 22  "foreach_clause"
2dd0a 2c 20 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 22  ,  "when_clause"
2dd0b 2c 20 0a 20 20 22 74 72 69 67 67 65 72 5f 63 6d  , .  "trigger_cm
2dd0c 64 22 2c 20 20 20 22 64 61 74 61 62 61 73 65 5f  d",   "database_
2dd0d 6b 77 5f 6f 70 74 22 2c 20 20 22 6b 65 79 5f 6f  kw_opt",  "key_o
2dd0e 70 74 22 2c 20 20 20 20 20 20 20 22 61 64 64 5f  pt",       "add_
2dd0f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 22  column_fullname"
2dd10 2c 0a 20 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70  ,.  "kwcolumn_op
2dd11 74 22 2c 20 20 22 63 72 65 61 74 65 5f 76 74 61  t",  "create_vta
2dd12 62 22 2c 20 20 20 22 76 74 61 62 61 72 67 6c 69  b",   "vtabargli
2dd13 73 74 22 2c 20 20 20 22 76 74 61 62 61 72 67 22  st",   "vtabarg"
2dd14 2c 20 20 20 20 20 0a 20 20 22 76 74 61 62 61 72  ,     .  "vtabar
2dd15 67 74 6f 6b 65 6e 22 2c 20 20 22 6c 70 22 2c 20  gtoken",  "lp", 
2dd16 20 20 20 20 20 20 20 20 20 20 20 22 61 6e 79 6c             "anyl
2dd17 69 73 74 22 2c 20 20 20 20 20 0a 7d 3b 0a 23 65  ist",     .};.#e
2dd18 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
2dd19 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
2dd1a 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69 6e 67  G./* For tracing
2dd1b 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 73 2c   reduce actions,
2dd1c 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c   the names of al
2dd1d 6c 20 72 75 6c 65 73 20 61 72 65 20 72 65 71 75  l rules are requ
2dd1e 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ired..*/.static 
2dd1f 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
2dd20 74 20 79 79 52 75 6c 65 4e 61 6d 65 5b 5d 20 3d  t yyRuleName[] =
2dd21 20 7b 0a 20 2f 2a 20 20 20 30 20 2a 2f 20 22 69   {. /*   0 */ "i
2dd22 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74  nput ::= cmdlist
2dd23 22 2c 0a 20 2f 2a 20 20 20 31 20 2a 2f 20 22 63  ",. /*   1 */ "c
2dd24 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69  mdlist ::= cmdli
2dd25 73 74 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20  st ecmd",. /*   
2dd26 32 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a  2 */ "cmdlist ::
2dd27 3d 20 65 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 33  = ecmd",. /*   3
2dd28 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20 53 45   */ "ecmd ::= SE
2dd29 4d 49 22 2c 0a 20 2f 2a 20 20 20 34 20 2a 2f 20  MI",. /*   4 */ 
2dd2a 22 65 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69  "ecmd ::= explai
2dd2b 6e 20 63 6d 64 78 20 53 45 4d 49 22 2c 0a 20 2f  n cmdx SEMI",. /
2dd2c 2a 20 20 20 35 20 2a 2f 20 22 65 78 70 6c 61 69  *   5 */ "explai
2dd2d 6e 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 20 36 20  n ::=",. /*   6 
2dd2e 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 20  */ "explain ::= 
2dd2f 45 58 50 4c 41 49 4e 22 2c 0a 20 2f 2a 20 20 20  EXPLAIN",. /*   
2dd30 37 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a  7 */ "explain ::
2dd31 3d 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20  = EXPLAIN QUERY 
2dd32 50 4c 41 4e 22 2c 0a 20 2f 2a 20 20 20 38 20 2a  PLAN",. /*   8 *
2dd33 2f 20 22 63 6d 64 78 20 3a 3a 3d 20 63 6d 64 22  / "cmdx ::= cmd"
2dd34 2c 0a 20 2f 2a 20 20 20 39 20 2a 2f 20 22 63 6d  ,. /*   9 */ "cm
2dd35 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e  d ::= BEGIN tran
2dd36 73 74 79 70 65 20 74 72 61 6e 73 5f 6f 70 74 22  stype trans_opt"
2dd37 2c 0a 20 2f 2a 20 20 31 30 20 2a 2f 20 22 74 72  ,. /*  10 */ "tr
2dd38 61 6e 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ans_opt ::=",. /
2dd39 2a 20 20 31 31 20 2a 2f 20 22 74 72 61 6e 73 5f  *  11 */ "trans_
2dd3a 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54  opt ::= TRANSACT
2dd3b 49 4f 4e 22 2c 0a 20 2f 2a 20 20 31 32 20 2a 2f  ION",. /*  12 */
2dd3c 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20   "trans_opt ::= 
2dd3d 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 22 2c  TRANSACTION nm",
2dd3e 0a 20 2f 2a 20 20 31 33 20 2a 2f 20 22 74 72 61  . /*  13 */ "tra
2dd3f 6e 73 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  nstype ::=",. /*
2dd40 20 20 31 34 20 2a 2f 20 22 74 72 61 6e 73 74 79    14 */ "transty
2dd41 70 65 20 3a 3a 3d 20 44 45 46 45 52 52 45 44 22  pe ::= DEFERRED"
2dd42 2c 0a 20 2f 2a 20 20 31 35 20 2a 2f 20 22 74 72  ,. /*  15 */ "tr
2dd43 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d 45  anstype ::= IMME
2dd44 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 31 36 20  DIATE",. /*  16 
2dd45 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
2dd46 3d 20 45 58 43 4c 55 53 49 56 45 22 2c 0a 20 2f  = EXCLUSIVE",. /
2dd47 2a 20 20 31 37 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  17 */ "cmd ::
2dd48 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f  = COMMIT trans_o
2dd49 70 74 22 2c 0a 20 2f 2a 20 20 31 38 20 2a 2f 20  pt",. /*  18 */ 
2dd4a 22 63 6d 64 20 3a 3a 3d 20 45 4e 44 20 74 72 61  "cmd ::= END tra
2dd4b 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 39  ns_opt",. /*  19
2dd4c 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c   */ "cmd ::= ROL
2dd4d 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 22  LBACK trans_opt"
2dd4e 2c 0a 20 2f 2a 20 20 32 30 20 2a 2f 20 22 73 61  ,. /*  20 */ "sa
2dd4f 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20  vepoint_opt ::= 
2dd50 53 41 56 45 50 4f 49 4e 54 22 2c 0a 20 2f 2a 20  SAVEPOINT",. /* 
2dd51 20 32 31 20 2a 2f 20 22 73 61 76 65 70 6f 69 6e   21 */ "savepoin
2dd52 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  t_opt ::=",. /* 
2dd53 20 32 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20   22 */ "cmd ::= 
2dd54 53 41 56 45 50 4f 49 4e 54 20 6e 6d 22 2c 0a 20  SAVEPOINT nm",. 
2dd55 2f 2a 20 20 32 33 20 2a 2f 20 22 63 6d 64 20 3a  /*  23 */ "cmd :
2dd56 3a 3d 20 52 45 4c 45 41 53 45 20 73 61 76 65 70  := RELEASE savep
2dd57 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c 0a 20 2f  oint_opt nm",. /
2dd58 2a 20 20 32 34 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  24 */ "cmd ::
2dd59 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73  = ROLLBACK trans
2dd5a 5f 6f 70 74 20 54 4f 20 73 61 76 65 70 6f 69 6e  _opt TO savepoin
2dd5b 74 5f 6f 70 74 20 6e 6d 22 2c 0a 20 2f 2a 20 20  t_opt nm",. /*  
2dd5c 32 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63  25 */ "cmd ::= c
2dd5d 72 65 61 74 65 5f 74 61 62 6c 65 20 63 72 65 61  reate_table crea
2dd5e 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c 0a  te_table_args",.
2dd5f 20 2f 2a 20 20 32 36 20 2a 2f 20 22 63 72 65 61   /*  26 */ "crea
2dd60 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 63 72 65  te_table ::= cre
2dd61 61 74 65 6b 77 20 74 65 6d 70 20 54 41 42 4c 45  atekw temp TABLE
2dd62 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2dd63 64 62 6e 6d 22 2c 0a 20 2f 2a 20 20 32 37 20 2a  dbnm",. /*  27 *
2dd64 2f 20 22 63 72 65 61 74 65 6b 77 20 3a 3a 3d 20  / "createkw ::= 
2dd65 43 52 45 41 54 45 22 2c 0a 20 2f 2a 20 20 32 38  CREATE",. /*  28
2dd66 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73 74 73   */ "ifnotexists
2dd67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32 39 20 2a   ::=",. /*  29 *
2dd68 2f 20 22 69 66 6e 6f 74 65 78 69 73 74 73 20 3a  / "ifnotexists :
2dd69 3a 3d 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53  := IF NOT EXISTS
2dd6a 22 2c 0a 20 2f 2a 20 20 33 30 20 2a 2f 20 22 74  ",. /*  30 */ "t
2dd6b 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 22 2c 0a 20  emp ::= TEMP",. 
2dd6c 2f 2a 20 20 33 31 20 2a 2f 20 22 74 65 6d 70 20  /*  31 */ "temp 
2dd6d 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 33 32 20 2a 2f  ::=",. /*  32 */
2dd6e 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61   "create_table_a
2dd6f 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d  rgs ::= LP colum
2dd70 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f  nlist conslist_o
2dd71 70 74 20 52 50 22 2c 0a 20 2f 2a 20 20 33 33 20  pt RP",. /*  33 
2dd72 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65  */ "create_table
2dd73 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c  _args ::= AS sel
2dd74 65 63 74 22 2c 0a 20 2f 2a 20 20 33 34 20 2a 2f  ect",. /*  34 */
2dd75 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d   "columnlist ::=
2dd76 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d   columnlist COMM
2dd77 41 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20  A column",. /*  
2dd78 33 35 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73  35 */ "columnlis
2dd79 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 22 2c 0a 20  t ::= column",. 
2dd7a 2f 2a 20 20 33 36 20 2a 2f 20 22 63 6f 6c 75 6d  /*  36 */ "colum
2dd7b 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74  n ::= columnid t
2dd7c 79 70 65 20 63 61 72 67 6c 69 73 74 22 2c 0a 20  ype carglist",. 
2dd7d 2f 2a 20 20 33 37 20 2a 2f 20 22 63 6f 6c 75 6d  /*  37 */ "colum
2dd7e 6e 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a  nid ::= nm",. /*
2dd7f 20 20 33 38 20 2a 2f 20 22 69 64 20 3a 3a 3d 20    38 */ "id ::= 
2dd80 49 44 22 2c 0a 20 2f 2a 20 20 33 39 20 2a 2f 20  ID",. /*  39 */ 
2dd81 22 69 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44 22  "id ::= INDEXED"
2dd82 2c 0a 20 2f 2a 20 20 34 30 20 2a 2f 20 22 69 64  ,. /*  40 */ "id
2dd83 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47 22  s ::= ID|STRING"
2dd84 2c 0a 20 2f 2a 20 20 34 31 20 2a 2f 20 22 6e 6d  ,. /*  41 */ "nm
2dd85 20 3a 3a 3d 20 69 64 22 2c 0a 20 2f 2a 20 20 34   ::= id",. /*  4
2dd86 32 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 53 54 52  2 */ "nm ::= STR
2dd87 49 4e 47 22 2c 0a 20 2f 2a 20 20 34 33 20 2a 2f  ING",. /*  43 */
2dd88 20 22 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57   "nm ::= JOIN_KW
2dd89 22 2c 0a 20 2f 2a 20 20 34 34 20 2a 2f 20 22 74  ",. /*  44 */ "t
2dd8a 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 34  ype ::=",. /*  4
2dd8b 35 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 20 74  5 */ "type ::= t
2dd8c 79 70 65 74 6f 6b 65 6e 22 2c 0a 20 2f 2a 20 20  ypetoken",. /*  
2dd8d 34 36 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e  46 */ "typetoken
2dd8e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 22 2c 0a   ::= typename",.
2dd8f 20 2f 2a 20 20 34 37 20 2a 2f 20 22 74 79 70 65   /*  47 */ "type
2dd90 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61  token ::= typena
2dd91 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 52 50 22  me LP signed RP"
2dd92 2c 0a 20 2f 2a 20 20 34 38 20 2a 2f 20 22 74 79  ,. /*  48 */ "ty
2dd93 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
2dd94 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 43  name LP signed C
2dd95 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50 22 2c  OMMA signed RP",
2dd96 0a 20 2f 2a 20 20 34 39 20 2a 2f 20 22 74 79 70  . /*  49 */ "typ
2dd97 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73 22 2c 0a  ename ::= ids",.
2dd98 20 2f 2a 20 20 35 30 20 2a 2f 20 22 74 79 70 65   /*  50 */ "type
2dd99 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e 61 6d  name ::= typenam
2dd9a 65 20 69 64 73 22 2c 0a 20 2f 2a 20 20 35 31 20  e ids",. /*  51 
2dd9b 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a 3d 20 70  */ "signed ::= p
2dd9c 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 20 35  lus_num",. /*  5
2dd9d 32 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a 3d  2 */ "signed ::=
2dd9e 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a   minus_num",. /*
2dd9f 20 20 35 33 20 2a 2f 20 22 63 61 72 67 6c 69 73    53 */ "carglis
2dda0 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74 20 63  t ::= carglist c
2dda1 61 72 67 22 2c 0a 20 2f 2a 20 20 35 34 20 2a 2f  arg",. /*  54 */
2dda2 20 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d 22 2c   "carglist ::=",
2dda3 0a 20 2f 2a 20 20 35 35 20 2a 2f 20 22 63 61 72  . /*  55 */ "car
2dda4 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54  g ::= CONSTRAINT
2dda5 20 6e 6d 20 63 63 6f 6e 73 22 2c 0a 20 2f 2a 20   nm ccons",. /* 
2dda6 20 35 36 20 2a 2f 20 22 63 61 72 67 20 3a 3a 3d   56 */ "carg ::=
2dda7 20 63 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 35 37   ccons",. /*  57
2dda8 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
2dda9 45 46 41 55 4c 54 20 74 65 72 6d 22 2c 0a 20 2f  EFAULT term",. /
2ddaa 2a 20 20 35 38 20 2a 2f 20 22 63 63 6f 6e 73 20  *  58 */ "ccons 
2ddab 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4c 50 20 65  ::= DEFAULT LP e
2ddac 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 20 35 39  xpr RP",. /*  59
2ddad 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
2ddae 45 46 41 55 4c 54 20 50 4c 55 53 20 74 65 72 6d  EFAULT PLUS term
2ddaf 22 2c 0a 20 2f 2a 20 20 36 30 20 2a 2f 20 22 63  ",. /*  60 */ "c
2ddb0 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54  cons ::= DEFAULT
2ddb1 20 4d 49 4e 55 53 20 74 65 72 6d 22 2c 0a 20 2f   MINUS term",. /
2ddb2 2a 20 20 36 31 20 2a 2f 20 22 63 63 6f 6e 73 20  *  61 */ "ccons 
2ddb3 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69 64 22 2c  ::= DEFAULT id",
2ddb4 0a 20 2f 2a 20 20 36 32 20 2a 2f 20 22 63 63 6f  . /*  62 */ "cco
2ddb5 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20 6f 6e 63 6f  ns ::= NULL onco
2ddb6 6e 66 22 2c 0a 20 2f 2a 20 20 36 33 20 2a 2f 20  nf",. /*  63 */ 
2ddb7 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f 54 20 4e  "ccons ::= NOT N
2ddb8 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a  ULL onconf",. /*
2ddb9 20 20 36 34 20 2a 2f 20 22 63 63 6f 6e 73 20 3a    64 */ "ccons :
2ddba 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20 73  := PRIMARY KEY s
2ddbb 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66 20  ortorder onconf 
2ddbc 61 75 74 6f 69 6e 63 22 2c 0a 20 2f 2a 20 20 36  autoinc",. /*  6
2ddbd 35 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  5 */ "ccons ::= 
2ddbe 55 4e 49 51 55 45 20 6f 6e 63 6f 6e 66 22 2c 0a  UNIQUE onconf",.
2ddbf 20 2f 2a 20 20 36 36 20 2a 2f 20 22 63 63 6f 6e   /*  66 */ "ccon
2ddc0 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65  s ::= CHECK LP e
2ddc1 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 20 36 37  xpr RP",. /*  67
2ddc2 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 52   */ "ccons ::= R
2ddc3 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78  EFERENCES nm idx
2ddc4 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73  list_opt refargs
2ddc5 22 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f 20 22 63  ",. /*  68 */ "c
2ddc6 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72 5f 73  cons ::= defer_s
2ddc7 75 62 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 20  ubclause",. /*  
2ddc8 36 39 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  69 */ "ccons ::=
2ddc9 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20   COLLATE ids",. 
2ddca 2f 2a 20 20 37 30 20 2a 2f 20 22 61 75 74 6f 69  /*  70 */ "autoi
2ddcb 6e 63 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 37 31  nc ::=",. /*  71
2ddcc 20 2a 2f 20 22 61 75 74 6f 69 6e 63 20 3a 3a 3d   */ "autoinc ::=
2ddcd 20 41 55 54 4f 49 4e 43 52 22 2c 0a 20 2f 2a 20   AUTOINCR",. /* 
2ddce 20 37 32 20 2a 2f 20 22 72 65 66 61 72 67 73 20   72 */ "refargs 
2ddcf 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 37 33 20 2a 2f  ::=",. /*  73 */
2ddd0 20 22 72 65 66 61 72 67 73 20 3a 3a 3d 20 72 65   "refargs ::= re
2ddd1 66 61 72 67 73 20 72 65 66 61 72 67 22 2c 0a 20  fargs refarg",. 
2ddd2 2f 2a 20 20 37 34 20 2a 2f 20 22 72 65 66 61 72  /*  74 */ "refar
2ddd3 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d 22 2c  g ::= MATCH nm",
2ddd4 0a 20 2f 2a 20 20 37 35 20 2a 2f 20 22 72 65 66  . /*  75 */ "ref
2ddd5 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45 54  arg ::= ON DELET
2ddd6 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20  E refact",. /*  
2ddd7 37 36 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a  76 */ "refarg ::
2ddd8 3d 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66 61  = ON UPDATE refa
2ddd9 63 74 22 2c 0a 20 2f 2a 20 20 37 37 20 2a 2f 20  ct",. /*  77 */ 
2ddda 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 49  "refarg ::= ON I
2dddb 4e 53 45 52 54 20 72 65 66 61 63 74 22 2c 0a 20  NSERT refact",. 
2dddc 2f 2a 20 20 37 38 20 2a 2f 20 22 72 65 66 61 63  /*  78 */ "refac
2dddd 74 20 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c 22 2c  t ::= SET NULL",
2ddde 0a 20 2f 2a 20 20 37 39 20 2a 2f 20 22 72 65 66  . /*  79 */ "ref
2dddf 61 63 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41  act ::= SET DEFA
2dde0 55 4c 54 22 2c 0a 20 2f 2a 20 20 38 30 20 2a 2f  ULT",. /*  80 */
2dde1 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53   "refact ::= CAS
2dde2 43 41 44 45 22 2c 0a 20 2f 2a 20 20 38 31 20 2a  CADE",. /*  81 *
2dde3 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 52 45  / "refact ::= RE
2dde4 53 54 52 49 43 54 22 2c 0a 20 2f 2a 20 20 38 32  STRICT",. /*  82
2dde5 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c   */ "defer_subcl
2dde6 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45 46  ause ::= NOT DEF
2dde7 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66  ERRABLE init_def
2dde8 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c  erred_pred_opt",
2dde9 0a 20 2f 2a 20 20 38 33 20 2a 2f 20 22 64 65 66  . /*  83 */ "def
2ddea 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d  er_subclause ::=
2ddeb 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74   DEFERRABLE init
2ddec 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
2dded 70 74 22 2c 0a 20 2f 2a 20 20 38 34 20 2a 2f 20  pt",. /*  84 */ 
2ddee 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70  "init_deferred_p
2ddef 72 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  red_opt ::=",. /
2ddf0 2a 20 20 38 35 20 2a 2f 20 22 69 6e 69 74 5f 64  *  85 */ "init_d
2ddf1 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
2ddf2 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 44   ::= INITIALLY D
2ddf3 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20 38  EFERRED",. /*  8
2ddf4 36 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65 72  6 */ "init_defer
2ddf5 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d  red_pred_opt ::=
2ddf6 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44   INITIALLY IMMED
2ddf7 49 41 54 45 22 2c 0a 20 2f 2a 20 20 38 37 20 2a  IATE",. /*  87 *
2ddf8 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20  / "conslist_opt 
2ddf9 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38 38 20 2a 2f  ::=",. /*  88 */
2ddfa 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a   "conslist_opt :
2ddfb 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73  := COMMA conslis
2ddfc 74 22 2c 0a 20 2f 2a 20 20 38 39 20 2a 2f 20 22  t",. /*  89 */ "
2ddfd 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e  conslist ::= con
2ddfe 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63 6f 6e  slist COMMA tcon
2ddff 73 22 2c 0a 20 2f 2a 20 20 39 30 20 2a 2f 20 22  s",. /*  90 */ "
2de00 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e  conslist ::= con
2de01 73 6c 69 73 74 20 74 63 6f 6e 73 22 2c 0a 20 2f  slist tcons",. /
2de02 2a 20 20 39 31 20 2a 2f 20 22 63 6f 6e 73 6c 69  *  91 */ "consli
2de03 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 22 2c 0a 20  st ::= tcons",. 
2de04 2f 2a 20 20 39 32 20 2a 2f 20 22 74 63 6f 6e 73  /*  92 */ "tcons
2de05 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20   ::= CONSTRAINT 
2de06 6e 6d 22 2c 0a 20 2f 2a 20 20 39 33 20 2a 2f 20  nm",. /*  93 */ 
2de07 22 74 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41  "tcons ::= PRIMA
2de08 52 59 20 4b 45 59 20 4c 50 20 69 64 78 6c 69 73  RY KEY LP idxlis
2de09 74 20 61 75 74 6f 69 6e 63 20 52 50 20 6f 6e 63  t autoinc RP onc
2de0a 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 34 20 2a 2f  onf",. /*  94 */
2de0b 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51   "tcons ::= UNIQ
2de0c 55 45 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50  UE LP idxlist RP
2de0d 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39   onconf",. /*  9
2de0e 35 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20  5 */ "tcons ::= 
2de0f 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50  CHECK LP expr RP
2de10 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39   onconf",. /*  9
2de11 36 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20  6 */ "tcons ::= 
2de12 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50 20 69  FOREIGN KEY LP i
2de13 64 78 6c 69 73 74 20 52 50 20 52 45 46 45 52 45  dxlist RP REFERE
2de14 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f  NCES nm idxlist_
2de15 6f 70 74 20 72 65 66 61 72 67 73 20 64 65 66 65  opt refargs defe
2de16 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 22  r_subclause_opt"
2de17 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f 20 22 64 65  ,. /*  97 */ "de
2de18 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70  fer_subclause_op
2de19 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 38 20  t ::=",. /*  98 
2de1a 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61  */ "defer_subcla
2de1b 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65  use_opt ::= defe
2de1c 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a 20 2f  r_subclause",. /
2de1d 2a 20 20 39 39 20 2a 2f 20 22 6f 6e 63 6f 6e 66  *  99 */ "onconf
2de1e 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 30 20 2a   ::=",. /* 100 *
2de1f 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e  / "onconf ::= ON
2de20 20 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c 76   CONFLICT resolv
2de21 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30 31 20  etype",. /* 101 
2de22 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 22 2c  */ "orconf ::=",
2de23 0a 20 2f 2a 20 31 30 32 20 2a 2f 20 22 6f 72 63  . /* 102 */ "orc
2de24 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c  onf ::= OR resol
2de25 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30 33  vetype",. /* 103
2de26 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65   */ "resolvetype
2de27 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65 22 2c   ::= raisetype",
2de28 0a 20 2f 2a 20 31 30 34 20 2a 2f 20 22 72 65 73  . /* 104 */ "res
2de29 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 49 47 4e  olvetype ::= IGN
2de2a 4f 52 45 22 2c 0a 20 2f 2a 20 31 30 35 20 2a 2f  ORE",. /* 105 */
2de2b 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a   "resolvetype ::
2de2c 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a 20  = REPLACE",. /* 
2de2d 31 30 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  106 */ "cmd ::= 
2de2e 44 52 4f 50 20 54 41 42 4c 45 20 69 66 65 78 69  DROP TABLE ifexi
2de2f 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20  sts fullname",. 
2de30 2f 2a 20 31 30 37 20 2a 2f 20 22 69 66 65 78 69  /* 107 */ "ifexi
2de31 73 74 73 20 3a 3a 3d 20 49 46 20 45 58 49 53 54  sts ::= IF EXIST
2de32 53 22 2c 0a 20 2f 2a 20 31 30 38 20 2a 2f 20 22  S",. /* 108 */ "
2de33 69 66 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a 20  ifexists ::=",. 
2de34 2f 2a 20 31 30 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 109 */ "cmd :
2de35 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70  := createkw temp
2de36 20 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73 74   VIEW ifnotexist
2de37 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c  s nm dbnm AS sel
2de38 65 63 74 22 2c 0a 20 2f 2a 20 31 31 30 20 2a 2f  ect",. /* 110 */
2de39 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56   "cmd ::= DROP V
2de3a 49 45 57 20 69 66 65 78 69 73 74 73 20 66 75 6c  IEW ifexists ful
2de3b 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31 31 31 20  lname",. /* 111 
2de3c 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 73 65 6c 65  */ "cmd ::= sele
2de3d 63 74 22 2c 0a 20 2f 2a 20 31 31 32 20 2a 2f 20  ct",. /* 112 */ 
2de3e 22 73 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65 73  "select ::= ones
2de3f 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 33 20  elect",. /* 113 
2de40 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 73  */ "select ::= s
2de41 65 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c 65 63  elect multiselec
2de42 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74 22 2c  t_op oneselect",
2de43 0a 20 2f 2a 20 31 31 34 20 2a 2f 20 22 6d 75 6c  . /* 114 */ "mul
2de44 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20  tiselect_op ::= 
2de45 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31 31 35 20  UNION",. /* 115 
2de46 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f  */ "multiselect_
2de47 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c  op ::= UNION ALL
2de48 22 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f 20 22 6d  ",. /* 116 */ "m
2de49 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a  ultiselect_op ::
2de4a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53 45  = EXCEPT|INTERSE
2de4b 43 54 22 2c 0a 20 2f 2a 20 31 31 37 20 2a 2f 20  CT",. /* 117 */ 
2de4c 22 6f 6e 65 73 65 6c 65 63 74 20 3a 3a 3d 20 53  "oneselect ::= S
2de4d 45 4c 45 43 54 20 64 69 73 74 69 6e 63 74 20 73  ELECT distinct s
2de4e 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f 6d 20 77  elcollist from w
2de4f 68 65 72 65 5f 6f 70 74 20 67 72 6f 75 70 62 79  here_opt groupby
2de50 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f 70 74 20  _opt having_opt 
2de51 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c 69 6d 69  orderby_opt limi
2de52 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 31 38 20  t_opt",. /* 118 
2de53 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d  */ "distinct ::=
2de54 20 44 49 53 54 49 4e 43 54 22 2c 0a 20 2f 2a 20   DISTINCT",. /* 
2de55 31 31 39 20 2a 2f 20 22 64 69 73 74 69 6e 63 74  119 */ "distinct
2de56 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31   ::= ALL",. /* 1
2de57 32 30 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20  20 */ "distinct 
2de58 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 31 20 2a 2f  ::=",. /* 121 */
2de59 20 22 73 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f   "sclp ::= selco
2de5a 6c 6c 69 73 74 20 43 4f 4d 4d 41 22 2c 0a 20 2f  llist COMMA",. /
2de5b 2a 20 31 32 32 20 2a 2f 20 22 73 63 6c 70 20 3a  * 122 */ "sclp :
2de5c 3a 3d 22 2c 0a 20 2f 2a 20 31 32 33 20 2a 2f 20  :=",. /* 123 */ 
2de5d 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20  "selcollist ::= 
2de5e 73 63 6c 70 20 65 78 70 72 20 61 73 22 2c 0a 20  sclp expr as",. 
2de5f 2f 2a 20 31 32 34 20 2a 2f 20 22 73 65 6c 63 6f  /* 124 */ "selco
2de60 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 53  llist ::= sclp S
2de61 54 41 52 22 2c 0a 20 2f 2a 20 31 32 35 20 2a 2f  TAR",. /* 125 */
2de62 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d   "selcollist ::=
2de63 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41   sclp nm DOT STA
2de64 52 22 2c 0a 20 2f 2a 20 31 32 36 20 2a 2f 20 22  R",. /* 126 */ "
2de65 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 22 2c 0a 20  as ::= AS nm",. 
2de66 2f 2a 20 31 32 37 20 2a 2f 20 22 61 73 20 3a 3a  /* 127 */ "as ::
2de67 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 31 32 38 20  = ids",. /* 128 
2de68 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a 20 2f 2a  */ "as ::=",. /*
2de69 20 31 32 39 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a   129 */ "from ::
2de6a 3d 22 2c 0a 20 2f 2a 20 31 33 30 20 2a 2f 20 22  =",. /* 130 */ "
2de6b 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65  from ::= FROM se
2de6c 6c 74 61 62 6c 69 73 74 22 2c 0a 20 2f 2a 20 31  ltablist",. /* 1
2de6d 33 31 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69  31 */ "stl_prefi
2de6e 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74  x ::= seltablist
2de6f 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a 20 31 33   joinop",. /* 13
2de70 32 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69 78  2 */ "stl_prefix
2de71 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 33 20 2a   ::=",. /* 133 *
2de72 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  / "seltablist ::
2de73 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20  = stl_prefix nm 
2de74 64 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f  dbnm as indexed_
2de75 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67  opt on_opt using
2de76 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 34 20 2a  _opt",. /* 134 *
2de77 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  / "seltablist ::
2de78 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20  = stl_prefix LP 
2de79 73 65 6c 65 63 74 20 52 50 20 61 73 20 6f 6e 5f  select RP as on_
2de7a 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a  opt using_opt",.
2de7b 20 2f 2a 20 31 33 35 20 2a 2f 20 22 73 65 6c 74   /* 135 */ "selt
2de7c 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70  ablist ::= stl_p
2de7d 72 65 66 69 78 20 4c 50 20 73 65 6c 74 61 62 6c  refix LP seltabl
2de7e 69 73 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74  ist RP as on_opt
2de7f 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a   using_opt",. /*
2de80 20 31 33 36 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a   136 */ "dbnm ::
2de81 3d 22 2c 0a 20 2f 2a 20 31 33 37 20 2a 2f 20 22  =",. /* 137 */ "
2de82 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 22  dbnm ::= DOT nm"
2de83 2c 0a 20 2f 2a 20 31 33 38 20 2a 2f 20 22 66 75  ,. /* 138 */ "fu
2de84 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62  llname ::= nm db
2de85 6e 6d 22 2c 0a 20 2f 2a 20 31 33 39 20 2a 2f 20  nm",. /* 139 */ 
2de86 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d  "joinop ::= COMM
2de87 41 7c 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 30  A|JOIN",. /* 140
2de88 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20   */ "joinop ::= 
2de89 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 22 2c 0a 20  JOIN_KW JOIN",. 
2de8a 2f 2a 20 31 34 31 20 2a 2f 20 22 6a 6f 69 6e 6f  /* 141 */ "joino
2de8b 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d  p ::= JOIN_KW nm
2de8c 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 32 20   JOIN",. /* 142 
2de8d 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a  */ "joinop ::= J
2de8e 4f 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49  OIN_KW nm nm JOI
2de8f 4e 22 2c 0a 20 2f 2a 20 31 34 33 20 2a 2f 20 22  N",. /* 143 */ "
2de90 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65 78  on_opt ::= ON ex
2de91 70 72 22 2c 0a 20 2f 2a 20 31 34 34 20 2a 2f 20  pr",. /* 144 */ 
2de92 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  "on_opt ::=",. /
2de93 2a 20 31 34 35 20 2a 2f 20 22 69 6e 64 65 78 65  * 145 */ "indexe
2de94 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  d_opt ::=",. /* 
2de95 31 34 36 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f  146 */ "indexed_
2de96 6f 70 74 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20  opt ::= INDEXED 
2de97 42 59 20 6e 6d 22 2c 0a 20 2f 2a 20 31 34 37 20  BY nm",. /* 147 
2de98 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 20  */ "indexed_opt 
2de99 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 22  ::= NOT INDEXED"
2de9a 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f 20 22 75 73  ,. /* 148 */ "us
2de9b 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55 53 49 4e  ing_opt ::= USIN
2de9c 47 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20  G LP inscollist 
2de9d 52 50 22 2c 0a 20 2f 2a 20 31 34 39 20 2a 2f 20  RP",. /* 149 */ 
2de9e 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 22 2c  "using_opt ::=",
2de9f 0a 20 2f 2a 20 31 35 30 20 2a 2f 20 22 6f 72 64  . /* 150 */ "ord
2dea0 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  erby_opt ::=",. 
2dea1 2f 2a 20 31 35 31 20 2a 2f 20 22 6f 72 64 65 72  /* 151 */ "order
2dea2 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52  by_opt ::= ORDER
2dea3 20 42 59 20 73 6f 72 74 6c 69 73 74 22 2c 0a 20   BY sortlist",. 
2dea4 2f 2a 20 31 35 32 20 2a 2f 20 22 73 6f 72 74 6c  /* 152 */ "sortl
2dea5 69 73 74 20 3a 3a 3d 20 73 6f 72 74 6c 69 73 74  ist ::= sortlist
2dea6 20 43 4f 4d 4d 41 20 73 6f 72 74 69 74 65 6d 20   COMMA sortitem 
2dea7 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20  sortorder",. /* 
2dea8 31 35 33 20 2a 2f 20 22 73 6f 72 74 6c 69 73 74  153 */ "sortlist
2dea9 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f   ::= sortitem so
2deaa 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 31 35  rtorder",. /* 15
2deab 34 20 2a 2f 20 22 73 6f 72 74 69 74 65 6d 20 3a  4 */ "sortitem :
2deac 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35  := expr",. /* 15
2dead 35 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72 20  5 */ "sortorder 
2deae 3a 3a 3d 20 41 53 43 22 2c 0a 20 2f 2a 20 31 35  ::= ASC",. /* 15
2deaf 36 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72 20  6 */ "sortorder 
2deb0 3a 3a 3d 20 44 45 53 43 22 2c 0a 20 2f 2a 20 31  ::= DESC",. /* 1
2deb1 35 37 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72  57 */ "sortorder
2deb2 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 38 20 2a   ::=",. /* 158 *
2deb3 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a  / "groupby_opt :
2deb4 3a 3d 22 2c 0a 20 2f 2a 20 31 35 39 20 2a 2f 20  :=",. /* 159 */ 
2deb5 22 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d  "groupby_opt ::=
2deb6 20 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72 6c   GROUP BY nexprl
2deb7 69 73 74 22 2c 0a 20 2f 2a 20 31 36 30 20 2a 2f  ist",. /* 160 */
2deb8 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d   "having_opt ::=
2deb9 22 2c 0a 20 2f 2a 20 31 36 31 20 2a 2f 20 22 68  ",. /* 161 */ "h
2deba 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41  aving_opt ::= HA
2debb 56 49 4e 47 20 65 78 70 72 22 2c 0a 20 2f 2a 20  VING expr",. /* 
2debc 31 36 32 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70  162 */ "limit_op
2debd 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 33 20  t ::=",. /* 163 
2debe 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a  */ "limit_opt ::
2debf 3d 20 4c 49 4d 49 54 20 65 78 70 72 22 2c 0a 20  = LIMIT expr",. 
2dec0 2f 2a 20 31 36 34 20 2a 2f 20 22 6c 69 6d 69 74  /* 164 */ "limit
2dec1 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65  _opt ::= LIMIT e
2dec2 78 70 72 20 4f 46 46 53 45 54 20 65 78 70 72 22  xpr OFFSET expr"
2dec3 2c 0a 20 2f 2a 20 31 36 35 20 2a 2f 20 22 6c 69  ,. /* 165 */ "li
2dec4 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49  mit_opt ::= LIMI
2dec5 54 20 65 78 70 72 20 43 4f 4d 4d 41 20 65 78 70  T expr COMMA exp
2dec6 72 22 2c 0a 20 2f 2a 20 31 36 36 20 2a 2f 20 22  r",. /* 166 */ "
2dec7 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46  cmd ::= DELETE F
2dec8 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64  ROM fullname ind
2dec9 65 78 65 64 5f 6f 70 74 20 77 68 65 72 65 5f 6f  exed_opt where_o
2deca 70 74 22 2c 0a 20 2f 2a 20 31 36 37 20 2a 2f 20  pt",. /* 167 */ 
2decb 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 22 2c  "where_opt ::=",
2decc 0a 20 2f 2a 20 31 36 38 20 2a 2f 20 22 77 68 65  . /* 168 */ "whe
2decd 72 65 5f 6f 70 74 20 3a 3a 3d 20 57 48 45 52 45  re_opt ::= WHERE
2dece 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 39 20   expr",. /* 169 
2decf 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 55 50 44 41  */ "cmd ::= UPDA
2ded0 54 45 20 6f 72 63 6f 6e 66 20 66 75 6c 6c 6e 61  TE orconf fullna
2ded1 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 53  me indexed_opt S
2ded2 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
2ded3 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 37 30 20 2a  _opt",. /* 170 *
2ded4 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d 20 73  / "setlist ::= s
2ded5 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20  etlist COMMA nm 
2ded6 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37  EQ expr",. /* 17
2ded7 31 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a  1 */ "setlist ::
2ded8 3d 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20  = nm EQ expr",. 
2ded9 2f 2a 20 31 37 32 20 2a 2f 20 22 63 6d 64 20 3a  /* 172 */ "cmd :
2deda 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
2dedb 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
2dedc 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45  ollist_opt VALUE
2dedd 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50  S LP itemlist RP
2dede 22 2c 0a 20 2f 2a 20 31 37 33 20 2a 2f 20 22 63  ",. /* 173 */ "c
2dedf 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
2dee0 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20  d INTO fullname 
2dee1 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
2dee2 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 37 34 20  elect",. /* 174 
2dee3 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  */ "cmd ::= inse
2dee4 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
2dee5 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
2dee6 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41 4c 55  opt DEFAULT VALU
2dee7 45 53 22 2c 0a 20 2f 2a 20 31 37 35 20 2a 2f 20  ES",. /* 175 */ 
2dee8 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20  "insert_cmd ::= 
2dee9 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 22 2c 0a  INSERT orconf",.
2deea 20 2f 2a 20 31 37 36 20 2a 2f 20 22 69 6e 73 65   /* 176 */ "inse
2deeb 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41  rt_cmd ::= REPLA
2deec 43 45 22 2c 0a 20 2f 2a 20 31 37 37 20 2a 2f 20  CE",. /* 177 */ 
2deed 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74  "itemlist ::= it
2deee 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70  emlist COMMA exp
2deef 72 22 2c 0a 20 2f 2a 20 31 37 38 20 2a 2f 20 22  r",. /* 178 */ "
2def0 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70  itemlist ::= exp
2def1 72 22 2c 0a 20 2f 2a 20 31 37 39 20 2a 2f 20 22  r",. /* 179 */ "
2def2 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a  inscollist_opt :
2def3 3a 3d 22 2c 0a 20 2f 2a 20 31 38 30 20 2a 2f 20  :=",. /* 180 */ 
2def4 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20  "inscollist_opt 
2def5 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73  ::= LP inscollis
2def6 74 20 52 50 22 2c 0a 20 2f 2a 20 31 38 31 20 2a  t RP",. /* 181 *
2def7 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a  / "inscollist ::
2def8 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d  = inscollist COM
2def9 4d 41 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 32 20  MA nm",. /* 182 
2defa 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a  */ "inscollist :
2defb 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 33 20  := nm",. /* 183 
2defc 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 74 65 72  */ "expr ::= ter
2defd 6d 22 2c 0a 20 2f 2a 20 31 38 34 20 2a 2f 20 22  m",. /* 184 */ "
2defe 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72  expr ::= LP expr
2deff 20 52 50 22 2c 0a 20 2f 2a 20 31 38 35 20 2a 2f   RP",. /* 185 */
2df00 20 22 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 22   "term ::= NULL"
2df01 2c 0a 20 2f 2a 20 31 38 36 20 2a 2f 20 22 65 78  ,. /* 186 */ "ex
2df02 70 72 20 3a 3a 3d 20 69 64 22 2c 0a 20 2f 2a 20  pr ::= id",. /* 
2df03 31 38 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  187 */ "expr ::=
2df04 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 31   JOIN_KW",. /* 1
2df05 38 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  88 */ "expr ::= 
2df06 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20  nm DOT nm",. /* 
2df07 31 38 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  189 */ "expr ::=
2df08 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54 20 6e   nm DOT nm DOT n
2df09 6d 22 2c 0a 20 2f 2a 20 31 39 30 20 2a 2f 20 22  m",. /* 190 */ "
2df0a 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45 47 45 52  term ::= INTEGER
2df0b 7c 46 4c 4f 41 54 7c 42 4c 4f 42 22 2c 0a 20 2f  |FLOAT|BLOB",. /
2df0c 2a 20 31 39 31 20 2a 2f 20 22 74 65 72 6d 20 3a  * 191 */ "term :
2df0d 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20  := STRING",. /* 
2df0e 31 39 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  192 */ "expr ::=
2df0f 20 52 45 47 49 53 54 45 52 22 2c 0a 20 2f 2a 20   REGISTER",. /* 
2df10 31 39 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  193 */ "expr ::=
2df11 20 56 41 52 49 41 42 4c 45 22 2c 0a 20 2f 2a 20   VARIABLE",. /* 
2df12 31 39 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  194 */ "expr ::=
2df13 20 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64   expr COLLATE id
2df14 73 22 2c 0a 20 2f 2a 20 31 39 35 20 2a 2f 20 22  s",. /* 195 */ "
2df15 65 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c 50  expr ::= CAST LP
2df16 20 65 78 70 72 20 41 53 20 74 79 70 65 74 6f 6b   expr AS typetok
2df17 65 6e 20 52 50 22 2c 0a 20 2f 2a 20 31 39 36 20  en RP",. /* 196 
2df18 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 20  */ "expr ::= ID 
2df19 4c 50 20 64 69 73 74 69 6e 63 74 20 65 78 70 72  LP distinct expr
2df1a 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 39  list RP",. /* 19
2df1b 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49  7 */ "expr ::= I
2df1c 44 20 4c 50 20 53 54 41 52 20 52 50 22 2c 0a 20  D LP STAR RP",. 
2df1d 2f 2a 20 31 39 38 20 2a 2f 20 22 74 65 72 6d 20  /* 198 */ "term 
2df1e 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57 22 2c 0a 20  ::= CTIME_KW",. 
2df1f 2f 2a 20 31 39 39 20 2a 2f 20 22 65 78 70 72 20  /* 199 */ "expr 
2df20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20 65 78 70  ::= expr AND exp
2df21 72 22 2c 0a 20 2f 2a 20 32 30 30 20 2a 2f 20 22  r",. /* 200 */ "
2df22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52  expr ::= expr OR
2df23 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 31 20   expr",. /* 201 
2df24 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
2df25 72 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78  r LT|GT|GE|LE ex
2df26 70 72 22 2c 0a 20 2f 2a 20 32 30 32 20 2a 2f 20  pr",. /* 202 */ 
2df27 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 45  "expr ::= expr E
2df28 51 7c 4e 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20  Q|NE expr",. /* 
2df29 32 30 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  203 */ "expr ::=
2df2a 20 65 78 70 72 20 42 49 54 41 4e 44 7c 42 49 54   expr BITAND|BIT
2df2b 4f 52 7c 4c 53 48 49 46 54 7c 52 53 48 49 46 54  OR|LSHIFT|RSHIFT
2df2c 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 34 20   expr",. /* 204 
2df2d 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
2df2e 72 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65 78 70  r PLUS|MINUS exp
2df2f 72 22 2c 0a 20 2f 2a 20 32 30 35 20 2a 2f 20 22  r",. /* 205 */ "
2df30 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 53 54  expr ::= expr ST
2df31 41 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65 78 70  AR|SLASH|REM exp
2df32 72 22 2c 0a 20 2f 2a 20 32 30 36 20 2a 2f 20 22  r",. /* 206 */ "
2df33 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f  expr ::= expr CO
2df34 4e 43 41 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20  NCAT expr",. /* 
2df35 32 30 37 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a  207 */ "likeop :
2df36 3a 3d 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a  := LIKE_KW",. /*
2df37 20 32 30 38 20 2a 2f 20 22 6c 69 6b 65 6f 70 20   208 */ "likeop 
2df38 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 22  ::= NOT LIKE_KW"
2df39 2c 0a 20 2f 2a 20 32 30 39 20 2a 2f 20 22 6c 69  ,. /* 209 */ "li
2df3a 6b 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 22 2c  keop ::= MATCH",
2df3b 0a 20 2f 2a 20 32 31 30 20 2a 2f 20 22 6c 69 6b  . /* 210 */ "lik
2df3c 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43  eop ::= NOT MATC
2df3d 48 22 2c 0a 20 2f 2a 20 32 31 31 20 2a 2f 20 22  H",. /* 211 */ "
2df3e 65 73 63 61 70 65 20 3a 3a 3d 20 45 53 43 41 50  escape ::= ESCAP
2df3f 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 32  E expr",. /* 212
2df40 20 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 22   */ "escape ::="
2df41 2c 0a 20 2f 2a 20 32 31 33 20 2a 2f 20 22 65 78  ,. /* 213 */ "ex
2df42 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65  pr ::= expr like
2df43 6f 70 20 65 78 70 72 20 65 73 63 61 70 65 22 2c  op expr escape",
2df44 0a 20 2f 2a 20 32 31 34 20 2a 2f 20 22 65 78 70  . /* 214 */ "exp
2df45 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55 4c  r ::= expr ISNUL
2df46 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c 0a 20 2f 2a 20  L|NOTNULL",. /* 
2df47 32 31 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  215 */ "expr ::=
2df48 20 65 78 70 72 20 49 53 20 4e 55 4c 4c 22 2c 0a   expr IS NULL",.
2df49 20 2f 2a 20 32 31 36 20 2a 2f 20 22 65 78 70 72   /* 216 */ "expr
2df4a 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55   ::= expr NOT NU
2df4b 4c 4c 22 2c 0a 20 2f 2a 20 32 31 37 20 2a 2f 20  LL",. /* 217 */ 
2df4c 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49  "expr ::= expr I
2df4d 53 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a  S NOT NULL",. /*
2df4e 20 32 31 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a   218 */ "expr ::
2df4f 3d 20 4e 4f 54 20 65 78 70 72 22 2c 0a 20 2f 2a  = NOT expr",. /*
2df50 20 32 31 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a   219 */ "expr ::
2df51 3d 20 42 49 54 4e 4f 54 20 65 78 70 72 22 2c 0a  = BITNOT expr",.
2df52 20 2f 2a 20 32 32 30 20 2a 2f 20 22 65 78 70 72   /* 220 */ "expr
2df53 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78 70 72 22   ::= MINUS expr"
2df54 2c 0a 20 2f 2a 20 32 32 31 20 2a 2f 20 22 65 78  ,. /* 221 */ "ex
2df55 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78 70 72  pr ::= PLUS expr
2df56 22 2c 0a 20 2f 2a 20 32 32 32 20 2a 2f 20 22 62  ",. /* 222 */ "b
2df57 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45  etween_op ::= BE
2df58 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32 32 33 20  TWEEN",. /* 223 
2df59 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a  */ "between_op :
2df5a 3a 3d 20 4e 4f 54 20 42 45 54 57 45 45 4e 22 2c  := NOT BETWEEN",
2df5b 0a 20 2f 2a 20 32 32 34 20 2a 2f 20 22 65 78 70  . /* 224 */ "exp
2df5c 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74 77 65  r ::= expr betwe
2df5d 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20 65  en_op expr AND e
2df5e 78 70 72 22 2c 0a 20 2f 2a 20 32 32 35 20 2a 2f  xpr",. /* 225 */
2df5f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 49 4e 22 2c   "in_op ::= IN",
2df60 0a 20 2f 2a 20 32 32 36 20 2a 2f 20 22 69 6e 5f  . /* 226 */ "in_
2df61 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 22 2c 0a  op ::= NOT IN",.
2df62 20 2f 2a 20 32 32 37 20 2a 2f 20 22 65 78 70 72   /* 227 */ "expr
2df63 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
2df64 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50 22 2c  LP exprlist RP",
2df65 0a 20 2f 2a 20 32 32 38 20 2a 2f 20 22 65 78 70  . /* 228 */ "exp
2df66 72 20 3a 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20  r ::= LP select 
2df67 52 50 22 2c 0a 20 2f 2a 20 32 32 39 20 2a 2f 20  RP",. /* 229 */ 
2df68 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69  "expr ::= expr i
2df69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65 63 74 20 52  n_op LP select R
2df6a 50 22 2c 0a 20 2f 2a 20 32 33 30 20 2a 2f 20 22  P",. /* 230 */ "
2df6b 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e  expr ::= expr in
2df6c 5f 6f 70 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f  _op nm dbnm",. /
2df6d 2a 20 32 33 31 20 2a 2f 20 22 65 78 70 72 20 3a  * 231 */ "expr :
2df6e 3a 3d 20 45 58 49 53 54 53 20 4c 50 20 73 65 6c  := EXISTS LP sel
2df6f 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 33 32  ect RP",. /* 232
2df70 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 43 41   */ "expr ::= CA
2df71 53 45 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20  SE case_operand 
2df72 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 63 61  case_exprlist ca
2df73 73 65 5f 65 6c 73 65 20 45 4e 44 22 2c 0a 20 2f  se_else END",. /
2df74 2a 20 32 33 33 20 2a 2f 20 22 63 61 73 65 5f 65  * 233 */ "case_e
2df75 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65  xprlist ::= case
2df76 5f 65 78 70 72 6c 69 73 74 20 57 48 45 4e 20 65  _exprlist WHEN e
2df77 78 70 72 20 54 48 45 4e 20 65 78 70 72 22 2c 0a  xpr THEN expr",.
2df78 20 2f 2a 20 32 33 34 20 2a 2f 20 22 63 61 73 65   /* 234 */ "case
2df79 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57 48  _exprlist ::= WH
2df7a 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70  EN expr THEN exp
2df7b 72 22 2c 0a 20 2f 2a 20 32 33 35 20 2a 2f 20 22  r",. /* 235 */ "
2df7c 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 45 4c  case_else ::= EL
2df7d 53 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33  SE expr",. /* 23
2df7e 36 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65 20  6 */ "case_else 
2df7f 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33 37 20 2a 2f  ::=",. /* 237 */
2df80 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a   "case_operand :
2df81 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33  := expr",. /* 23
2df82 38 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72 61  8 */ "case_opera
2df83 6e 64 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33 39  nd ::=",. /* 239
2df84 20 2a 2f 20 22 65 78 70 72 6c 69 73 74 20 3a 3a   */ "exprlist ::
2df85 3d 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f  = nexprlist",. /
2df86 2a 20 32 34 30 20 2a 2f 20 22 65 78 70 72 6c 69  * 240 */ "exprli
2df87 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 31  st ::=",. /* 241
2df88 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73 74 20 3a   */ "nexprlist :
2df89 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d  := nexprlist COM
2df8a 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 34  MA expr",. /* 24
2df8b 32 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73 74 20  2 */ "nexprlist 
2df8c 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  ::= expr",. /* 2
2df8d 34 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63  43 */ "cmd ::= c
2df8e 72 65 61 74 65 6b 77 20 75 6e 69 71 75 65 66 6c  reatekw uniquefl
2df8f 61 67 20 49 4e 44 45 58 20 69 66 6e 6f 74 65 78  ag INDEX ifnotex
2df90 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e 20  ists nm dbnm ON 
2df91 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50  nm LP idxlist RP
2df92 22 2c 0a 20 2f 2a 20 32 34 34 20 2a 2f 20 22 75  ",. /* 244 */ "u
2df93 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55 4e  niqueflag ::= UN
2df94 49 51 55 45 22 2c 0a 20 2f 2a 20 32 34 35 20 2a  IQUE",. /* 245 *
2df95 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a  / "uniqueflag ::
2df96 3d 22 2c 0a 20 2f 2a 20 32 34 36 20 2a 2f 20 22  =",. /* 246 */ "
2df97 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 22  idxlist_opt ::="
2df98 2c 0a 20 2f 2a 20 32 34 37 20 2a 2f 20 22 69 64  ,. /* 247 */ "id
2df99 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50  xlist_opt ::= LP
2df9a 20 69 64 78 6c 69 73 74 20 52 50 22 2c 0a 20 2f   idxlist RP",. /
2df9b 2a 20 32 34 38 20 2a 2f 20 22 69 64 78 6c 69 73  * 248 */ "idxlis
2df9c 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43 4f  t ::= idxlist CO
2df9d 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73  MMA nm collate s
2df9e 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 32  ortorder",. /* 2
2df9f 34 39 20 2a 2f 20 22 69 64 78 6c 69 73 74 20 3a  49 */ "idxlist :
2dfa0 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f  := nm collate so
2dfa1 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 32 35  rtorder",. /* 25
2dfa2 30 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a  0 */ "collate ::
2dfa3 3d 22 2c 0a 20 2f 2a 20 32 35 31 20 2a 2f 20 22  =",. /* 251 */ "
2dfa4 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c  collate ::= COLL
2dfa5 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 32 35  ATE ids",. /* 25
2dfa6 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52  2 */ "cmd ::= DR
2dfa7 4f 50 20 49 4e 44 45 58 20 69 66 65 78 69 73 74  OP INDEX ifexist
2dfa8 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a  s fullname",. /*
2dfa9 20 32 35 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   253 */ "cmd ::=
2dfaa 20 56 41 43 55 55 4d 22 2c 0a 20 2f 2a 20 32 35   VACUUM",. /* 25
2dfab 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 56 41  4 */ "cmd ::= VA
2dfac 43 55 55 4d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 35  CUUM nm",. /* 25
2dfad 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52  5 */ "cmd ::= PR
2dfae 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20  AGMA nm dbnm",. 
2dfaf 2f 2a 20 32 35 36 20 2a 2f 20 22 63 6d 64 20 3a  /* 256 */ "cmd :
2dfb0 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
2dfb1 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20 2f 2a  m EQ nmnum",. /*
2dfb2 20 32 35 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   257 */ "cmd ::=
2dfb3 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
2dfb4 4c 50 20 6e 6d 6e 75 6d 20 52 50 22 2c 0a 20 2f  LP nmnum RP",. /
2dfb5 2a 20 32 35 38 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 258 */ "cmd ::
2dfb6 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
2dfb7 20 45 51 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a   EQ minus_num",.
2dfb8 20 2f 2a 20 32 35 39 20 2a 2f 20 22 63 6d 64 20   /* 259 */ "cmd 
2dfb9 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
2dfba 6e 6d 20 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d 20  nm LP minus_num 
2dfbb 52 50 22 2c 0a 20 2f 2a 20 32 36 30 20 2a 2f 20  RP",. /* 260 */ 
2dfbc 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f  "nmnum ::= plus_
2dfbd 6e 75 6d 22 2c 0a 20 2f 2a 20 32 36 31 20 2a 2f  num",. /* 261 */
2dfbe 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 22 2c   "nmnum ::= nm",
2dfbf 0a 20 2f 2a 20 32 36 32 20 2a 2f 20 22 6e 6d 6e  . /* 262 */ "nmn
2dfc0 75 6d 20 3a 3a 3d 20 4f 4e 22 2c 0a 20 2f 2a 20  um ::= ON",. /* 
2dfc1 32 36 33 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a  263 */ "nmnum ::
2dfc2 3d 20 44 45 4c 45 54 45 22 2c 0a 20 2f 2a 20 32  = DELETE",. /* 2
2dfc3 36 34 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d  64 */ "nmnum ::=
2dfc4 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a 20 32   DEFAULT",. /* 2
2dfc5 36 35 20 2a 2f 20 22 70 6c 75 73 5f 6e 75 6d 20  65 */ "plus_num 
2dfc6 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d  ::= plus_opt num
2dfc7 62 65 72 22 2c 0a 20 2f 2a 20 32 36 36 20 2a 2f  ber",. /* 266 */
2dfc8 20 22 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20   "minus_num ::= 
2dfc9 4d 49 4e 55 53 20 6e 75 6d 62 65 72 22 2c 0a 20  MINUS number",. 
2dfca 2f 2a 20 32 36 37 20 2a 2f 20 22 6e 75 6d 62 65  /* 267 */ "numbe
2dfcb 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c  r ::= INTEGER|FL
2dfcc 4f 41 54 22 2c 0a 20 2f 2a 20 32 36 38 20 2a 2f  OAT",. /* 268 */
2dfcd 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50   "plus_opt ::= P
2dfce 4c 55 53 22 2c 0a 20 2f 2a 20 32 36 39 20 2a 2f  LUS",. /* 269 */
2dfcf 20 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 22 2c   "plus_opt ::=",
2dfd0 0a 20 2f 2a 20 32 37 30 20 2a 2f 20 22 63 6d 64  . /* 270 */ "cmd
2dfd1 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 72   ::= createkw tr
2dfd2 69 67 67 65 72 5f 64 65 63 6c 20 42 45 47 49 4e  igger_decl BEGIN
2dfd3 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73   trigger_cmd_lis
2dfd4 74 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 37 31 20  t END",. /* 271 
2dfd5 2a 2f 20 22 74 72 69 67 67 65 72 5f 64 65 63 6c  */ "trigger_decl
2dfd6 20 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47 45   ::= temp TRIGGE
2dfd7 52 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  R ifnotexists nm
2dfd8 20 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74 69   dbnm trigger_ti
2dfd9 6d 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74  me trigger_event
2dfda 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72   ON fullname for
2dfdb 65 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65 6e  each_clause when
2dfdc 5f 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 32 37  _clause",. /* 27
2dfdd 32 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69  2 */ "trigger_ti
2dfde 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 22 2c 0a  me ::= BEFORE",.
2dfdf 20 2f 2a 20 32 37 33 20 2a 2f 20 22 74 72 69 67   /* 273 */ "trig
2dfe0 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54  ger_time ::= AFT
2dfe1 45 52 22 2c 0a 20 2f 2a 20 32 37 34 20 2a 2f 20  ER",. /* 274 */ 
2dfe2 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a  "trigger_time ::
2dfe3 3d 20 49 4e 53 54 45 41 44 20 4f 46 22 2c 0a 20  = INSTEAD OF",. 
2dfe4 2f 2a 20 32 37 35 20 2a 2f 20 22 74 72 69 67 67  /* 275 */ "trigg
2dfe5 65 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c 0a 20 2f  er_time ::=",. /
2dfe6 2a 20 32 37 36 20 2a 2f 20 22 74 72 69 67 67 65  * 276 */ "trigge
2dfe7 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45  r_event ::= DELE
2dfe8 54 45 7c 49 4e 53 45 52 54 22 2c 0a 20 2f 2a 20  TE|INSERT",. /* 
2dfe9 32 37 37 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  277 */ "trigger_
2dfea 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45  event ::= UPDATE
2dfeb 22 2c 0a 20 2f 2a 20 32 37 38 20 2a 2f 20 22 74  ",. /* 278 */ "t
2dfec 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
2dfed 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63 6f   UPDATE OF insco
2dfee 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 37 39 20  llist",. /* 279 
2dfef 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61 75  */ "foreach_clau
2dff0 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 30  se ::=",. /* 280
2dff1 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61   */ "foreach_cla
2dff2 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48  use ::= FOR EACH
2dff3 20 52 4f 57 22 2c 0a 20 2f 2a 20 32 38 31 20 2a   ROW",. /* 281 *
2dff4 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a  / "when_clause :
2dff5 3a 3d 22 2c 0a 20 2f 2a 20 32 38 32 20 2a 2f 20  :=",. /* 282 */ 
2dff6 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d  "when_clause ::=
2dff7 20 57 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a   WHEN expr",. /*
2dff8 20 32 38 33 20 2a 2f 20 22 74 72 69 67 67 65 72   283 */ "trigger
2dff9 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72  _cmd_list ::= tr
2dffa 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74  igger_cmd_list t
2dffb 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 22  rigger_cmd SEMI"
2dffc 2c 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22 74 72  ,. /* 284 */ "tr
2dffd 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a  igger_cmd_list :
2dffe 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53  := trigger_cmd S
2dfff 45 4d 49 22 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f  EMI",. /* 285 */
2e000 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
2e001 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
2e002 6e 6d 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  nm SET setlist w
2e003 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32  here_opt",. /* 2
2e004 38 36 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  86 */ "trigger_c
2e005 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
2e006 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c  d INTO nm inscol
2e007 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20  list_opt VALUES 
2e008 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c  LP itemlist RP",
2e009 0a 20 2f 2a 20 32 38 37 20 2a 2f 20 22 74 72 69  . /* 287 */ "tri
2e00a 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73  gger_cmd ::= ins
2e00b 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20  ert_cmd INTO nm 
2e00c 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
2e00d 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32 38 38 20  elect",. /* 288 
2e00e 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20  */ "trigger_cmd 
2e00f 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
2e010 6e 6d 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20  nm where_opt",. 
2e011 2f 2a 20 32 38 39 20 2a 2f 20 22 74 72 69 67 67  /* 289 */ "trigg
2e012 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  er_cmd ::= selec
2e013 74 22 2c 0a 20 2f 2a 20 32 39 30 20 2a 2f 20 22  t",. /* 290 */ "
2e014 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c  expr ::= RAISE L
2e015 50 20 49 47 4e 4f 52 45 20 52 50 22 2c 0a 20 2f  P IGNORE RP",. /
2e016 2a 20 32 39 31 20 2a 2f 20 22 65 78 70 72 20 3a  * 291 */ "expr :
2e017 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69 73  := RAISE LP rais
2e018 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52  etype COMMA nm R
2e019 50 22 2c 0a 20 2f 2a 20 32 39 32 20 2a 2f 20 22  P",. /* 292 */ "
2e01a 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f  raisetype ::= RO
2e01b 4c 4c 42 41 43 4b 22 2c 0a 20 2f 2a 20 32 39 33  LLBACK",. /* 293
2e01c 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a   */ "raisetype :
2e01d 3a 3d 20 41 42 4f 52 54 22 2c 0a 20 2f 2a 20 32  := ABORT",. /* 2
2e01e 39 34 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65  94 */ "raisetype
2e01f 20 3a 3a 3d 20 46 41 49 4c 22 2c 0a 20 2f 2a 20   ::= FAIL",. /* 
2e020 32 39 35 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  295 */ "cmd ::= 
2e021 44 52 4f 50 20 54 52 49 47 47 45 52 20 69 66 65  DROP TRIGGER ife
2e022 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c  xists fullname",
2e023 0a 20 2f 2a 20 32 39 36 20 2a 2f 20 22 63 6d 64  . /* 296 */ "cmd
2e024 20 3a 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61   ::= ATTACH data
2e025 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72  base_kw_opt expr
2e026 20 41 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74   AS expr key_opt
2e027 22 2c 0a 20 2f 2a 20 32 39 37 20 2a 2f 20 22 63  ",. /* 297 */ "c
2e028 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61  md ::= DETACH da
2e029 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78  tabase_kw_opt ex
2e02a 70 72 22 2c 0a 20 2f 2a 20 32 39 38 20 2a 2f 20  pr",. /* 298 */ 
2e02b 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  "key_opt ::=",. 
2e02c 2f 2a 20 32 39 39 20 2a 2f 20 22 6b 65 79 5f 6f  /* 299 */ "key_o
2e02d 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 22  pt ::= KEY expr"
2e02e 2c 0a 20 2f 2a 20 33 30 30 20 2a 2f 20 22 64 61  ,. /* 300 */ "da
2e02f 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a  tabase_kw_opt ::
2e030 3d 20 44 41 54 41 42 41 53 45 22 2c 0a 20 2f 2a  = DATABASE",. /*
2e031 20 33 30 31 20 2a 2f 20 22 64 61 74 61 62 61 73   301 */ "databas
2e032 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  e_kw_opt ::=",. 
2e033 2f 2a 20 33 30 32 20 2a 2f 20 22 63 6d 64 20 3a  /* 302 */ "cmd :
2e034 3a 3d 20 52 45 49 4e 44 45 58 22 2c 0a 20 2f 2a  := REINDEX",. /*
2e035 20 33 30 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   303 */ "cmd ::=
2e036 20 52 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d   REINDEX nm dbnm
2e037 22 2c 0a 20 2f 2a 20 33 30 34 20 2a 2f 20 22 63  ",. /* 304 */ "c
2e038 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c  md ::= ANALYZE",
2e039 0a 20 2f 2a 20 33 30 35 20 2a 2f 20 22 63 6d 64  . /* 305 */ "cmd
2e03a 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20   ::= ANALYZE nm 
2e03b 64 62 6e 6d 22 2c 0a 20 2f 2a 20 33 30 36 20 2a  dbnm",. /* 306 *
2e03c 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52  / "cmd ::= ALTER
2e03d 20 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20   TABLE fullname 
2e03e 52 45 4e 41 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20  RENAME TO nm",. 
2e03f 2f 2a 20 33 30 37 20 2a 2f 20 22 63 6d 64 20 3a  /* 307 */ "cmd :
2e040 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61  := ALTER TABLE a
2e041 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
2e042 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f  me ADD kwcolumn_
2e043 6f 70 74 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a  opt column",. /*
2e044 20 33 30 38 20 2a 2f 20 22 61 64 64 5f 63 6f 6c   308 */ "add_col
2e045 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d  umn_fullname ::=
2e046 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20   fullname",. /* 
2e047 33 30 39 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e  309 */ "kwcolumn
2e048 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33  _opt ::=",. /* 3
2e049 31 30 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f  10 */ "kwcolumn_
2e04a 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57  opt ::= COLUMNKW
2e04b 22 2c 0a 20 2f 2a 20 33 31 31 20 2a 2f 20 22 63  ",. /* 311 */ "c
2e04c 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74  md ::= create_vt
2e04d 61 62 22 2c 0a 20 2f 2a 20 33 31 32 20 2a 2f 20  ab",. /* 312 */ 
2e04e 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f  "cmd ::= create_
2e04f 76 74 61 62 20 4c 50 20 76 74 61 62 61 72 67 6c  vtab LP vtabargl
2e050 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 31 33  ist RP",. /* 313
2e051 20 2a 2f 20 22 63 72 65 61 74 65 5f 76 74 61 62   */ "create_vtab
2e052 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 56 49   ::= createkw VI
2e053 52 54 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64  RTUAL TABLE nm d
2e054 62 6e 6d 20 55 53 49 4e 47 20 6e 6d 22 2c 0a 20  bnm USING nm",. 
2e055 2f 2a 20 33 31 34 20 2a 2f 20 22 76 74 61 62 61  /* 314 */ "vtaba
2e056 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61  rglist ::= vtaba
2e057 72 67 22 2c 0a 20 2f 2a 20 33 31 35 20 2a 2f 20  rg",. /* 315 */ 
2e058 22 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d  "vtabarglist ::=
2e059 20 76 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d   vtabarglist COM
2e05a 4d 41 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a  MA vtabarg",. /*
2e05b 20 33 31 36 20 2a 2f 20 22 76 74 61 62 61 72 67   316 */ "vtabarg
2e05c 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 31 37 20 2a   ::=",. /* 317 *
2e05d 2f 20 22 76 74 61 62 61 72 67 20 3a 3a 3d 20 76  / "vtabarg ::= v
2e05e 74 61 62 61 72 67 20 76 74 61 62 61 72 67 74 6f  tabarg vtabargto
2e05f 6b 65 6e 22 2c 0a 20 2f 2a 20 33 31 38 20 2a 2f  ken",. /* 318 */
2e060 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a   "vtabargtoken :
2e061 3a 3d 20 41 4e 59 22 2c 0a 20 2f 2a 20 33 31 39  := ANY",. /* 319
2e062 20 2a 2f 20 22 76 74 61 62 61 72 67 74 6f 6b 65   */ "vtabargtoke
2e063 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74  n ::= lp anylist
2e064 20 52 50 22 2c 0a 20 2f 2a 20 33 32 30 20 2a 2f   RP",. /* 320 */
2e065 20 22 6c 70 20 3a 3a 3d 20 4c 50 22 2c 0a 20 2f   "lp ::= LP",. /
2e066 2a 20 33 32 31 20 2a 2f 20 22 61 6e 79 6c 69 73  * 321 */ "anylis
2e067 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 32 32 20  t ::=",. /* 322 
2e068 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20  */ "anylist ::= 
2e069 61 6e 79 6c 69 73 74 20 41 4e 59 22 2c 0a 7d 3b  anylist ANY",.};
2e06a 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55  .#endif /* NDEBU
2e06b 47 20 2a 2f 0a 0a 0a 23 69 66 20 59 59 53 54 41  G */...#if YYSTA
2e06c 43 4b 44 45 50 54 48 3c 3d 30 0a 2f 2a 0a 2a 2a  CKDEPTH<=0./*.**
2e06d 20 54 72 79 20 74 6f 20 69 6e 63 72 65 61 73 65   Try to increase
2e06e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
2e06f 20 70 61 72 73 65 72 20 73 74 61 63 6b 2e 0a 2a   parser stack..*
2e070 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  /.static void yy
2e071 47 72 6f 77 53 74 61 63 6b 28 79 79 50 61 72 73  GrowStack(yyPars
2e072 65 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 65  er *p){.  int ne
2e073 77 53 69 7a 65 3b 0a 20 20 79 79 53 74 61 63 6b  wSize;.  yyStack
2e074 45 6e 74 72 79 20 2a 70 4e 65 77 3b 0a 0a 20 20  Entry *pNew;..  
2e075 6e 65 77 53 69 7a 65 20 3d 20 70 2d 3e 79 79 73  newSize = p->yys
2e076 74 6b 73 7a 2a 32 20 2b 20 31 30 30 3b 0a 20 20  tksz*2 + 100;.  
2e077 70 4e 65 77 20 3d 20 72 65 61 6c 6c 6f 63 28 70  pNew = realloc(p
2e078 2d 3e 79 79 73 74 61 63 6b 2c 20 6e 65 77 53 69  ->yystack, newSi
2e079 7a 65 2a 73 69 7a 65 6f 66 28 70 4e 65 77 5b 30  ze*sizeof(pNew[0
2e07a 5d 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20  ]));.  if( pNew 
2e07b 29 7b 0a 20 20 20 20 70 2d 3e 79 79 73 74 61 63  ){.    p->yystac
2e07c 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d  k = pNew;.    p-
2e07d 3e 79 79 73 74 6b 73 7a 20 3d 20 6e 65 77 53 69  >yystksz = newSi
2e07e 7a 65 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  ze;.#ifndef NDEB
2e07f 55 47 0a 20 20 20 20 69 66 28 20 79 79 54 72 61  UG.    if( yyTra
2e080 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20  ceFILE ){.      
2e081 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
2e082 49 4c 45 2c 22 25 73 53 74 61 63 6b 20 67 72 6f  ILE,"%sStack gro
2e083 77 73 20 74 6f 20 25 64 20 65 6e 74 72 69 65 73  ws to %d entries
2e084 21 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  !\n",.          
2e085 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70      yyTracePromp
2e086 74 2c 20 70 2d 3e 79 79 73 74 6b 73 7a 29 3b 0a  t, p->yystksz);.
2e087 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
2e088 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a  .}.#endif../* .*
2e089 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2e08a 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20  allocates a new 
2e08b 70 61 72 73 65 72 2e 0a 2a 2a 20 54 68 65 20 6f  parser..** The o
2e08c 6e 6c 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20  nly argument is 
2e08d 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
2e08e 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 77 6f  unction which wo
2e08f 72 6b 73 20 6c 69 6b 65 0a 2a 2a 20 6d 61 6c 6c  rks like.** mall
2e090 6f 63 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73  oc..**.** Inputs
2e091 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74  :.** A pointer t
2e092 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75  o the function u
2e093 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  sed to allocate 
2e094 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 75  memory..**.** Ou
2e095 74 70 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e  tputs:.** A poin
2e096 74 65 72 20 74 6f 20 61 20 70 61 72 73 65 72 2e  ter to a parser.
2e097 20 20 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69    This pointer i
2e098 73 20 75 73 65 64 20 69 6e 20 73 75 62 73 65 71  s used in subseq
2e099 75 65 6e 74 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f  uent calls.** to
2e09a 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 20 61   sqlite3Parser a
2e09b 6e 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  nd sqlite3Parser
2e09c 46 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Free..*/.SQLITE_
2e09d 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
2e09e 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63  lite3ParserAlloc
2e09f 28 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 50  (void *(*mallocP
2e0a0 72 6f 63 29 28 73 69 7a 65 5f 74 29 29 7b 0a 20  roc)(size_t)){. 
2e0a1 20 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73   yyParser *pPars
2e0a2 65 72 3b 0a 20 20 70 50 61 72 73 65 72 20 3d 20  er;.  pParser = 
2e0a3 28 79 79 50 61 72 73 65 72 2a 29 28 2a 6d 61 6c  (yyParser*)(*mal
2e0a4 6c 6f 63 50 72 6f 63 29 28 20 28 73 69 7a 65 5f  locProc)( (size_
2e0a5 74 29 73 69 7a 65 6f 66 28 79 79 50 61 72 73 65  t)sizeof(yyParse
2e0a6 72 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72  r) );.  if( pPar
2e0a7 73 65 72 20 29 7b 0a 20 20 20 20 70 50 61 72 73  ser ){.    pPars
2e0a8 65 72 2d 3e 79 79 69 64 78 20 3d 20 2d 31 3b 0a  er->yyidx = -1;.
2e0a9 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41  #ifdef YYTRACKMA
2e0aa 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 20 20  XSTACKDEPTH.    
2e0ab 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61  pParser->yyidxMa
2e0ac 78 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69  x = 0;.#endif.#i
2e0ad 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d  f YYSTACKDEPTH<=
2e0ae 30 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79  0.    pParser->y
2e0af 79 73 74 61 63 6b 20 3d 20 4e 55 4c 4c 3b 0a 20  ystack = NULL;. 
2e0b0 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79 73 74     pParser->yyst
2e0b1 6b 73 7a 20 3d 20 30 3b 0a 20 20 20 20 79 79 47  ksz = 0;.    yyG
2e0b2 72 6f 77 53 74 61 63 6b 28 70 50 61 72 73 65 72  rowStack(pParser
2e0b3 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
2e0b4 72 65 74 75 72 6e 20 70 50 61 72 73 65 72 3b 0a  return pParser;.
2e0b5 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  }../* The follow
2e0b6 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c  ing function del
2e0b7 65 74 65 73 20 74 68 65 20 76 61 6c 75 65 20 61  etes the value a
2e0b8 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
2e0b9 0a 2a 2a 20 73 79 6d 62 6f 6c 2e 20 20 54 68 65  .** symbol.  The
2e0ba 20 73 79 6d 62 6f 6c 20 63 61 6e 20 62 65 20 65   symbol can be e
2e0bb 69 74 68 65 72 20 61 20 74 65 72 6d 69 6e 61 6c  ither a terminal
2e0bc 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e   or nonterminal.
2e0bd 0a 2a 2a 20 22 79 79 6d 61 6a 6f 72 22 20 69 73  .** "yymajor" is
2e0be 20 74 68 65 20 73 79 6d 62 6f 6c 20 63 6f 64 65   the symbol code
2e0bf 2c 20 61 6e 64 20 22 79 79 70 6d 69 6e 6f 72 22  , and "yypminor"
2e0c0 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2e0c1 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 2e 0a 2a  .** the value..*
2e0c2 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  /.static void yy
2e0c3 5f 64 65 73 74 72 75 63 74 6f 72 28 0a 20 20 79  _destructor(.  y
2e0c4 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2e0c5 65 72 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61  er,    /* The pa
2e0c6 72 73 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45  rser */.  YYCODE
2e0c7 54 59 50 45 20 79 79 6d 61 6a 6f 72 2c 20 20 20  TYPE yymajor,   
2e0c8 20 20 2f 2a 20 54 79 70 65 20 63 6f 64 65 20 66    /* Type code f
2e0c9 6f 72 20 6f 62 6a 65 63 74 20 74 6f 20 64 65 73  or object to des
2e0ca 74 72 6f 79 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f  troy */.  YYMINO
2e0cb 52 54 59 50 45 20 2a 79 79 70 6d 69 6e 6f 72 20  RTYPE *yypminor 
2e0cc 20 20 2f 2a 20 54 68 65 20 6f 62 6a 65 63 74 20    /* The object 
2e0cd 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 20  to be destroyed 
2e0ce 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50  */.){.  sqlite3P
2e0cf 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a  arserARG_FETCH;.
2e0d0 20 20 73 77 69 74 63 68 28 20 79 79 6d 61 6a 6f    switch( yymajo
2e0d1 72 20 29 7b 0a 20 20 20 20 2f 2a 20 48 65 72 65  r ){.    /* Here
2e0d2 20 69 73 20 69 6e 73 65 72 74 65 64 20 74 68 65   is inserted the
2e0d3 20 61 63 74 69 6f 6e 73 20 77 68 69 63 68 20 74   actions which t
2e0d4 61 6b 65 20 70 6c 61 63 65 20 77 68 65 6e 20 61  ake place when a
2e0d5 0a 20 20 20 20 2a 2a 20 74 65 72 6d 69 6e 61 6c  .    ** terminal
2e0d6 20 6f 72 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c   or non-terminal
2e0d7 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20   is destroyed.  
2e0d8 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a  This can happen.
2e0d9 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20      ** when the 
2e0da 73 79 6d 62 6f 6c 20 69 73 20 70 6f 70 70 65 64  symbol is popped
2e0db 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 20   from the stack 
2e0dc 64 75 72 69 6e 67 20 61 0a 20 20 20 20 2a 2a 20  during a.    ** 
2e0dd 72 65 64 75 63 65 20 6f 72 20 64 75 72 69 6e 67  reduce or during
2e0de 20 65 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e   error processin
2e0df 67 20 6f 72 20 77 68 65 6e 20 61 20 70 61 72 73  g or when a pars
2e0e0 65 72 20 69 73 20 0a 20 20 20 20 2a 2a 20 62 65  er is .    ** be
2e0e1 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 62 65  ing destroyed be
2e0e2 66 6f 72 65 20 69 74 20 69 73 20 66 69 6e 69 73  fore it is finis
2e0e3 68 65 64 20 70 61 72 73 69 6e 67 2e 0a 20 20 20  hed parsing..   
2e0e4 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a   **.    ** Note:
2e0e5 20 64 75 72 69 6e 67 20 61 20 72 65 64 75 63 65   during a reduce
2e0e6 2c 20 74 68 65 20 6f 6e 6c 79 20 73 79 6d 62 6f  , the only symbo
2e0e7 6c 73 20 64 65 73 74 72 6f 79 65 64 20 61 72 65  ls destroyed are
2e0e8 20 74 68 6f 73 65 0a 20 20 20 20 2a 2a 20 77 68   those.    ** wh
2e0e9 69 63 68 20 61 70 70 65 61 72 20 6f 6e 20 74 68  ich appear on th
2e0ea 65 20 52 48 53 20 6f 66 20 74 68 65 20 72 75 6c  e RHS of the rul
2e0eb 65 2c 20 62 75 74 20 77 68 69 63 68 20 61 72 65  e, but which are
2e0ec 20 6e 6f 74 20 75 73 65 64 0a 20 20 20 20 2a 2a   not used.    **
2e0ed 20 69 6e 73 69 64 65 20 74 68 65 20 43 20 63 6f   inside the C co
2e0ee 64 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  de..    */.    c
2e0ef 61 73 65 20 31 36 30 3a 20 2f 2a 20 73 65 6c 65  ase 160: /* sele
2e0f0 63 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31  ct */.    case 1
2e0f1 39 34 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74  94: /* oneselect
2e0f2 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53 65 6c   */.{.sqlite3Sel
2e0f3 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  ectDelete(pParse
2e0f4 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
2e0f5 3e 79 79 32 34 33 29 29 3b 0a 7d 0a 20 20 20 20  >yy243));.}.    
2e0f6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
2e0f7 65 20 31 37 34 3a 20 2f 2a 20 74 65 72 6d 20 2a  e 174: /* term *
2e0f8 2f 0a 20 20 20 20 63 61 73 65 20 31 37 35 3a 20  /.    case 175: 
2e0f9 2f 2a 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 63  /* expr */.    c
2e0fa 61 73 65 20 31 39 39 3a 20 2f 2a 20 77 68 65 72  ase 199: /* wher
2e0fb 65 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73  e_opt */.    cas
2e0fc 65 20 32 30 31 3a 20 2f 2a 20 68 61 76 69 6e 67  e 201: /* having
2e0fd 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
2e0fe 20 32 31 30 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20   210: /* on_opt 
2e0ff 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 35 3a  */.    case 215:
2e100 20 2f 2a 20 73 6f 72 74 69 74 65 6d 20 2a 2f 0a   /* sortitem */.
2e101 20 20 20 20 63 61 73 65 20 32 32 33 3a 20 2f 2a      case 223: /*
2e102 20 65 73 63 61 70 65 20 2a 2f 0a 20 20 20 20 63   escape */.    c
2e103 61 73 65 20 32 32 36 3a 20 2f 2a 20 63 61 73 65  ase 226: /* case
2e104 5f 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20  _operand */.    
2e105 63 61 73 65 20 32 32 38 3a 20 2f 2a 20 63 61 73  case 228: /* cas
2e106 65 5f 65 6c 73 65 20 2a 2f 0a 20 20 20 20 63 61  e_else */.    ca
2e107 73 65 20 32 33 39 3a 20 2f 2a 20 77 68 65 6e 5f  se 239: /* when_
2e108 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 63 61  clause */.    ca
2e109 73 65 20 32 34 32 3a 20 2f 2a 20 6b 65 79 5f 6f  se 242: /* key_o
2e10a 70 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45  pt */.{.sqlite3E
2e10b 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65  xprDelete(pParse
2e10c 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
2e10d 3e 79 79 37 32 29 29 3b 0a 7d 0a 20 20 20 20 20  >yy72));.}.     
2e10e 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
2e10f 20 31 37 39 3a 20 2f 2a 20 69 64 78 6c 69 73 74   179: /* idxlist
2e110 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
2e111 20 31 38 37 3a 20 2f 2a 20 69 64 78 6c 69 73 74   187: /* idxlist
2e112 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 37   */.    case 197
2e113 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20  : /* selcollist 
2e114 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 30 3a  */.    case 200:
2e115 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20   /* groupby_opt 
2e116 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 32 3a  */.    case 202:
2e117 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20   /* orderby_opt 
2e118 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 34 3a  */.    case 204:
2e119 20 2f 2a 20 73 63 6c 70 20 2a 2f 0a 20 20 20 20   /* sclp */.    
2e11a 63 61 73 65 20 32 31 34 3a 20 2f 2a 20 73 6f 72  case 214: /* sor
2e11b 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  tlist */.    cas
2e11c 65 20 32 31 36 3a 20 2f 2a 20 6e 65 78 70 72 6c  e 216: /* nexprl
2e11d 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
2e11e 32 31 37 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20  217: /* setlist 
2e11f 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 30 3a  */.    case 220:
2e120 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20 2a 2f 0a   /* itemlist */.
2e121 20 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f 2a      case 221: /*
2e122 20 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20   exprlist */.   
2e123 20 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 63 61   case 227: /* ca
2e124 73 65 5f 65 78 70 72 6c 69 73 74 20 2a 2f 0a 7b  se_exprlist */.{
2e125 0a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74  .sqlite3ExprList
2e126 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
2e127 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
2e128 31 34 38 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62  148));.}.      b
2e129 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31  reak;.    case 1
2e12a 39 33 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20  93: /* fullname 
2e12b 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 38 3a  */.    case 198:
2e12c 20 2f 2a 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20   /* from */.    
2e12d 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 73 65 6c  case 206: /* sel
2e12e 74 61 62 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63  tablist */.    c
2e12f 61 73 65 20 32 30 37 3a 20 2f 2a 20 73 74 6c 5f  ase 207: /* stl_
2e130 70 72 65 66 69 78 20 2a 2f 0a 7b 0a 73 71 6c 69  prefix */.{.sqli
2e131 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
2e132 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79  (pParse->db, (yy
2e133 70 6d 69 6e 6f 72 2d 3e 79 79 31 38 35 29 29 3b  pminor->yy185));
2e134 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .}.      break;.
2e135 20 20 20 20 63 61 73 65 20 32 31 31 3a 20 2f 2a      case 211: /*
2e136 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20   using_opt */.  
2e137 20 20 63 61 73 65 20 32 31 33 3a 20 2f 2a 20 69    case 213: /* i
2e138 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a 20 20 20  nscollist */.   
2e139 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20 69 6e   case 219: /* in
2e13a 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a  scollist_opt */.
2e13b 7b 0a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44  {.sqlite3IdListD
2e13c 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2e13d 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 32  , (yypminor->yy2
2e13e 35 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  54));.}.      br
2e13f 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 33  eak;.    case 23
2e140 35 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  5: /* trigger_cm
2e141 64 5f 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  d_list */.    ca
2e142 73 65 20 32 34 30 3a 20 2f 2a 20 74 72 69 67 67  se 240: /* trigg
2e143 65 72 5f 63 6d 64 20 2a 2f 0a 7b 0a 73 71 6c 69  er_cmd */.{.sqli
2e144 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
2e145 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
2e146 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 34   (yypminor->yy14
2e147 35 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  5));.}.      bre
2e148 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 33 37  ak;.    case 237
2e149 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65  : /* trigger_eve
2e14a 6e 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49  nt */.{.sqlite3I
2e14b 64 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  dListDelete(pPar
2e14c 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f  se->db, (yypmino
2e14d 72 2d 3e 79 79 33 33 32 29 2e 62 29 3b 0a 7d 0a  r->yy332).b);.}.
2e14e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e14f 20 64 65 66 61 75 6c 74 3a 20 20 62 72 65 61 6b   default:  break
2e150 3b 20 20 20 2f 2a 20 49 66 20 6e 6f 20 64 65 73  ;   /* If no des
2e151 74 72 75 63 74 6f 72 20 61 63 74 69 6f 6e 20 73  tructor action s
2e152 70 65 63 69 66 69 65 64 3a 20 64 6f 20 6e 6f 74  pecified: do not
2e153 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f  hing */.  }.}../
2e154 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20 70 61 72  *.** Pop the par
2e155 73 65 72 27 73 20 73 74 61 63 6b 20 6f 6e 63 65  ser's stack once
2e156 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
2e157 20 69 73 20 61 20 64 65 73 74 72 75 63 74 6f 72   is a destructor
2e158 20 72 6f 75 74 69 6e 65 20 61 73 73 6f 63 69 61   routine associa
2e159 74 65 64 20 77 69 74 68 20 74 68 65 20 74 6f 6b  ted with the tok
2e15a 65 6e 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 70  en which.** is p
2e15b 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73  opped from the s
2e15c 74 61 63 6b 2c 20 74 68 65 6e 20 63 61 6c 6c 20  tack, then call 
2e15d 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  it..**.** Return
2e15e 20 74 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e   the major token
2e15f 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
2e160 73 79 6d 62 6f 6c 20 70 6f 70 70 65 64 2e 0a 2a  symbol popped..*
2e161 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f  /.static int yy_
2e162 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
2e163 28 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73  (yyParser *pPars
2e164 65 72 29 7b 0a 20 20 59 59 43 4f 44 45 54 59 50  er){.  YYCODETYP
2e165 45 20 79 79 6d 61 6a 6f 72 3b 0a 20 20 79 79 53  E yymajor;.  yyS
2e166 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73  tackEntry *yytos
2e167 20 3d 20 26 70 50 61 72 73 65 72 2d 3e 79 79 73   = &pParser->yys
2e168 74 61 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79  tack[pParser->yy
2e169 69 64 78 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61  idx];..  if( pPa
2e16a 72 73 65 72 2d 3e 79 79 69 64 78 3c 30 20 29 20  rser->yyidx<0 ) 
2e16b 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 6e 64 65  return 0;.#ifnde
2e16c 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79  f NDEBUG.  if( y
2e16d 79 54 72 61 63 65 46 49 4c 45 20 26 26 20 70 50  yTraceFILE && pP
2e16e 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20  arser->yyidx>=0 
2e16f 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79  ){.    fprintf(y
2e170 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 50 6f  yTraceFILE,"%sPo
2e171 70 70 69 6e 67 20 25 73 5c 6e 22 2c 0a 20 20 20  pping %s\n",.   
2e172 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74     yyTracePrompt
2e173 2c 0a 20 20 20 20 20 20 79 79 54 6f 6b 65 6e 4e  ,.      yyTokenN
2e174 61 6d 65 5b 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72  ame[yytos->major
2e175 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ]);.  }.#endif. 
2e176 20 79 79 6d 61 6a 6f 72 20 3d 20 79 79 74 6f 73   yymajor = yytos
2e177 2d 3e 6d 61 6a 6f 72 3b 0a 20 20 79 79 5f 64 65  ->major;.  yy_de
2e178 73 74 72 75 63 74 6f 72 28 70 50 61 72 73 65 72  structor(pParser
2e179 2c 20 79 79 6d 61 6a 6f 72 2c 20 26 79 79 74 6f  , yymajor, &yyto
2e17a 73 2d 3e 6d 69 6e 6f 72 29 3b 0a 20 20 70 50 61  s->minor);.  pPa
2e17b 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20  rser->yyidx--;. 
2e17c 20 72 65 74 75 72 6e 20 79 79 6d 61 6a 6f 72 3b   return yymajor;
2e17d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c  .}../* .** Deall
2e17e 6f 63 61 74 65 20 61 6e 64 20 64 65 73 74 72 6f  ocate and destro
2e17f 79 20 61 20 70 61 72 73 65 72 2e 20 20 44 65 73  y a parser.  Des
2e180 74 72 75 63 74 6f 72 73 20 61 72 65 20 61 6c 6c  tructors are all
2e181 20 63 61 6c 6c 65 64 20 66 6f 72 0a 2a 2a 20 61   called for.** a
2e182 6c 6c 20 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74  ll stack element
2e183 73 20 62 65 66 6f 72 65 20 73 68 75 74 74 69 6e  s before shuttin
2e184 67 20 74 68 65 20 70 61 72 73 65 72 20 64 6f 77  g the parser dow
2e185 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a  n..**.** Inputs:
2e186 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
2e187 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74    A pointer to t
2e188 68 65 20 70 61 72 73 65 72 2e 20 20 54 68 69 73  he parser.  This
2e189 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 69   should be a poi
2e18a 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6f 62  nter.**       ob
2e18b 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
2e18c 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0a  te3ParserAlloc..
2e18d 2a 2a 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74  ** <li>  A point
2e18e 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
2e18f 20 75 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d   used to reclaim
2e190 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
2e191 0a 2a 2a 20 20 20 20 20 20 20 66 72 6f 6d 20 6d  .**       from m
2e192 61 6c 6c 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a  alloc..** </ul>.
2e193 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e194 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
2e195 72 73 65 72 46 72 65 65 28 0a 20 20 76 6f 69 64  rserFree(.  void
2e196 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
2e197 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2e198 61 72 73 65 72 20 74 6f 20 62 65 20 64 65 6c 65  arser to be dele
2e199 74 65 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ted */.  void (*
2e19a 66 72 65 65 50 72 6f 63 29 28 76 6f 69 64 2a 29  freeProc)(void*)
2e19b 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e       /* Function
2e19c 20 75 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d   used to reclaim
2e19d 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20   memory */.){.  
2e19e 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65  yyParser *pParse
2e19f 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70  r = (yyParser*)p
2e1a0 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 72 3d  ;.  if( pParser=
2e1a1 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77  =0 ) return;.  w
2e1a2 68 69 6c 65 28 20 70 50 61 72 73 65 72 2d 3e 79  hile( pParser->y
2e1a3 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70  yidx>=0 ) yy_pop
2e1a4 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 70 50  _parser_stack(pP
2e1a5 61 72 73 65 72 29 3b 0a 23 69 66 20 59 59 53 54  arser);.#if YYST
2e1a6 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 66 72  ACKDEPTH<=0.  fr
2e1a7 65 65 28 70 50 61 72 73 65 72 2d 3e 79 79 73 74  ee(pParser->yyst
2e1a8 61 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 28  ack);.#endif.  (
2e1a9 2a 66 72 65 65 50 72 6f 63 29 28 28 76 6f 69 64  *freeProc)((void
2e1aa 2a 29 70 50 61 72 73 65 72 29 3b 0a 7d 0a 0a 2f  *)pParser);.}../
2e1ab 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
2e1ac 70 65 61 6b 20 64 65 70 74 68 20 6f 66 20 74 68  peak depth of th
2e1ad 65 20 73 74 61 63 6b 20 66 6f 72 20 61 20 70 61  e stack for a pa
2e1ae 72 73 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rser..*/.#ifdef 
2e1af 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
2e1b0 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56  EPTH.SQLITE_PRIV
2e1b1 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
2e1b2 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76  arserStackPeak(v
2e1b3 6f 69 64 20 2a 70 29 7b 0a 20 20 79 79 50 61 72  oid *p){.  yyPar
2e1b4 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d 20 28  ser *pParser = (
2e1b5 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20 20 72  yyParser*)p;.  r
2e1b6 65 74 75 72 6e 20 70 50 61 72 73 65 72 2d 3e 79  eturn pParser->y
2e1b7 79 69 64 78 4d 61 78 3b 0a 7d 0a 23 65 6e 64 69  yidxMax;.}.#endi
2e1b8 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  f../*.** Find th
2e1b9 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63  e appropriate ac
2e1ba 74 69 6f 6e 20 66 6f 72 20 61 20 70 61 72 73 65  tion for a parse
2e1bb 72 20 67 69 76 65 6e 20 74 68 65 20 74 65 72 6d  r given the term
2e1bc 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65  inal.** look-ahe
2e1bd 61 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68  ad token iLookAh
2e1be 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ead..**.** If th
2e1bf 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b  e look-ahead tok
2e1c0 65 6e 20 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20  en is YYNOCODE, 
2e1c1 74 68 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65  then check to se
2e1c2 65 20 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20  e if the action 
2e1c3 69 73 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e  is.** independen
2e1c4 74 20 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68  t of the look-ah
2e1c5 65 61 64 2e 20 20 49 66 20 69 74 20 69 73 2c 20  ead.  If it is, 
2e1c6 72 65 74 75 72 6e 20 74 68 65 20 61 63 74 69 6f  return the actio
2e1c7 6e 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20  n, otherwise.** 
2e1c8 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54  return YY_NO_ACT
2e1c9 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ION..*/.static i
2e1ca 6e 74 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74  nt yy_find_shift
2e1cb 5f 61 63 74 69 6f 6e 28 0a 20 20 79 79 50 61 72  _action(.  yyPar
2e1cc 73 65 72 20 2a 70 50 61 72 73 65 72 2c 20 20 20  ser *pParser,   
2e1cd 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2e1ce 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59  er */.  YYCODETY
2e1cf 50 45 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20  PE iLookAhead   
2e1d0 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68    /* The look-ah
2e1d1 65 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a  ead token */.){.
2e1d2 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 73    int i;.  int s
2e1d3 74 61 74 65 6e 6f 20 3d 20 70 50 61 72 73 65 72  tateno = pParser
2e1d4 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65  ->yystack[pParse
2e1d5 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e  r->yyidx].staten
2e1d6 6f 3b 0a 20 0a 20 20 69 66 28 20 73 74 61 74 65  o;. .  if( state
2e1d7 6e 6f 3e 59 59 5f 53 48 49 46 54 5f 4d 41 58 20  no>YY_SHIFT_MAX 
2e1d8 7c 7c 20 28 69 20 3d 20 79 79 5f 73 68 69 66 74  || (i = yy_shift
2e1d9 5f 6f 66 73 74 5b 73 74 61 74 65 6e 6f 5d 29 3d  _ofst[stateno])=
2e1da 3d 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46  =YY_SHIFT_USE_DF
2e1db 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  LT ){.    return
2e1dc 20 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74   yy_default[stat
2e1dd 65 6e 6f 5d 3b 0a 20 20 7d 0a 20 20 61 73 73 65  eno];.  }.  asse
2e1de 72 74 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21 3d  rt( iLookAhead!=
2e1df 59 59 4e 4f 43 4f 44 45 20 29 3b 0a 20 20 69 20  YYNOCODE );.  i 
2e1e0 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 20  += iLookAhead;. 
2e1e1 20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59   if( i<0 || i>=Y
2e1e2 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79  Y_SZ_ACTTAB || y
2e1e3 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d  y_lookahead[i]!=
2e1e4 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20  iLookAhead ){.  
2e1e5 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64    if( iLookAhead
2e1e6 3e 30 20 29 7b 0a 23 69 66 64 65 66 20 59 59 46  >0 ){.#ifdef YYF
2e1e7 41 4c 4c 42 41 43 4b 0a 20 20 20 20 20 20 59 59  ALLBACK.      YY
2e1e8 43 4f 44 45 54 59 50 45 20 69 46 61 6c 6c 62 61  CODETYPE iFallba
2e1e9 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ck;            /
2e1ea 2a 20 46 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e  * Fallback token
2e1eb 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 4c   */.      if( iL
2e1ec 6f 6f 6b 41 68 65 61 64 3c 73 69 7a 65 6f 66 28  ookAhead<sizeof(
2e1ed 79 79 46 61 6c 6c 62 61 63 6b 29 2f 73 69 7a 65  yyFallback)/size
2e1ee 6f 66 28 79 79 46 61 6c 6c 62 61 63 6b 5b 30 5d  of(yyFallback[0]
2e1ef 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26  ).             &
2e1f0 26 20 28 69 46 61 6c 6c 62 61 63 6b 20 3d 20 79  & (iFallback = y
2e1f1 79 46 61 6c 6c 62 61 63 6b 5b 69 4c 6f 6f 6b 41  yFallback[iLookA
2e1f2 68 65 61 64 5d 29 21 3d 30 20 29 7b 0a 23 69 66  head])!=0 ){.#if
2e1f3 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
2e1f4 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46      if( yyTraceF
2e1f5 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ILE ){.         
2e1f6 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2e1f7 46 49 4c 45 2c 20 22 25 73 46 41 4c 4c 42 41 43  FILE, "%sFALLBAC
2e1f8 4b 20 25 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a 20  K %s => %s\n",. 
2e1f9 20 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72              yyTr
2e1fa 61 63 65 50 72 6f 6d 70 74 2c 20 79 79 54 6f 6b  acePrompt, yyTok
2e1fb 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61  enName[iLookAhea
2e1fc 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  d], yyTokenName[
2e1fd 69 46 61 6c 6c 62 61 63 6b 5d 29 3b 0a 20 20 20  iFallback]);.   
2e1fe 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
2e1ff 20 20 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f        return yy_
2e200 66 69 6e 64 5f 73 68 69 66 74 5f 61 63 74 69 6f  find_shift_actio
2e201 6e 28 70 50 61 72 73 65 72 2c 20 69 46 61 6c 6c  n(pParser, iFall
2e202 62 61 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 23  back);.      }.#
2e203 65 6e 64 69 66 0a 23 69 66 64 65 66 20 59 59 57  endif.#ifdef YYW
2e204 49 4c 44 43 41 52 44 0a 20 20 20 20 20 20 7b 0a  ILDCARD.      {.
2e205 20 20 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20          int j = 
2e206 69 20 2d 20 69 4c 6f 6f 6b 41 68 65 61 64 20 2b  i - iLookAhead +
2e207 20 59 59 57 49 4c 44 43 41 52 44 3b 0a 20 20 20   YYWILDCARD;.   
2e208 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 26 26       if( j>=0 &&
2e209 20 6a 3c 59 59 5f 53 5a 5f 41 43 54 54 41 42 20   j<YY_SZ_ACTTAB 
2e20a 26 26 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  && yy_lookahead[
2e20b 6a 5d 3d 3d 59 59 57 49 4c 44 43 41 52 44 20 29  j]==YYWILDCARD )
2e20c 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  {.#ifndef NDEBUG
2e20d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 79  .          if( y
2e20e 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
2e20f 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
2e210 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22  f(yyTraceFILE, "
2e211 25 73 57 49 4c 44 43 41 52 44 20 25 73 20 3d 3e  %sWILDCARD %s =>
2e212 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
2e213 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72         yyTracePr
2e214 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d  ompt, yyTokenNam
2e215 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79  e[iLookAhead], y
2e216 79 54 6f 6b 65 6e 4e 61 6d 65 5b 59 59 57 49 4c  yTokenName[YYWIL
2e217 44 43 41 52 44 5d 29 3b 0a 20 20 20 20 20 20 20  DCARD]);.       
2e218 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e     }.#endif /* N
2e219 44 45 42 55 47 20 2a 2f 0a 20 20 20 20 20 20 20  DEBUG */.       
2e21a 20 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74     return yy_act
2e21b 69 6f 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20  ion[j];.        
2e21c 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
2e21d 20 2f 2a 20 59 59 57 49 4c 44 43 41 52 44 20 2a   /* YYWILDCARD *
2e21e 2f 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  /.    }.    retu
2e21f 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b 73 74  rn yy_default[st
2e220 61 74 65 6e 6f 5d 3b 0a 20 20 7d 65 6c 73 65 7b  ateno];.  }else{
2e221 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 61  .    return yy_a
2e222 63 74 69 6f 6e 5b 69 5d 3b 0a 20 20 7d 0a 7d 0a  ction[i];.  }.}.
2e223 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
2e224 61 70 70 72 6f 70 72 69 61 74 65 20 61 63 74 69  appropriate acti
2e225 6f 6e 20 66 6f 72 20 61 20 70 61 72 73 65 72 20  on for a parser 
2e226 67 69 76 65 6e 20 74 68 65 20 6e 6f 6e 2d 74 65  given the non-te
2e227 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61  rminal.** look-a
2e228 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b  head token iLook
2e229 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  Ahead..**.** If 
2e22a 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74  the look-ahead t
2e22b 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43 4f 44 45  oken is YYNOCODE
2e22c 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 74 6f 20  , then check to 
2e22d 73 65 65 20 69 66 20 74 68 65 20 61 63 74 69 6f  see if the actio
2e22e 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70 65 6e 64  n is.** independ
2e22f 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d  ent of the look-
2e230 61 68 65 61 64 2e 20 20 49 66 20 69 74 20 69 73  ahead.  If it is
2e231 2c 20 72 65 74 75 72 6e 20 74 68 65 20 61 63 74  , return the act
2e232 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a  ion, otherwise.*
2e233 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f 41  * return YY_NO_A
2e234 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63  CTION..*/.static
2e235 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f 72 65 64   int yy_find_red
2e236 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20 20 69 6e  uce_action(.  in
2e237 74 20 73 74 61 74 65 6e 6f 2c 20 20 20 20 20 20  t stateno,      
2e238 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
2e239 6e 74 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20  nt state number 
2e23a 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  */.  YYCODETYPE 
2e23b 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20 20 20 2f  iLookAhead     /
2e23c 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  * The look-ahead
2e23d 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69   token */.){.  i
2e23e 6e 74 20 69 3b 0a 23 69 66 64 65 66 20 59 59 45  nt i;.#ifdef YYE
2e23f 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 66 28  RRORSYMBOL.  if(
2e240 20 73 74 61 74 65 6e 6f 3e 59 59 5f 52 45 44 55   stateno>YY_REDU
2e241 43 45 5f 4d 41 58 20 29 7b 0a 20 20 20 20 72 65  CE_MAX ){.    re
2e242 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b  turn yy_default[
2e243 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 23 65  stateno];.  }.#e
2e244 6c 73 65 0a 20 20 61 73 73 65 72 74 28 20 73 74  lse.  assert( st
2e245 61 74 65 6e 6f 3c 3d 59 59 5f 52 45 44 55 43 45  ateno<=YY_REDUCE
2e246 5f 4d 41 58 20 29 3b 0a 23 65 6e 64 69 66 0a 20  _MAX );.#endif. 
2e247 20 69 20 3d 20 79 79 5f 72 65 64 75 63 65 5f 6f   i = yy_reduce_o
2e248 66 73 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  fst[stateno];.  
2e249 61 73 73 65 72 74 28 20 69 21 3d 59 59 5f 52 45  assert( i!=YY_RE
2e24a 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 29 3b  DUCE_USE_DFLT );
2e24b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b  .  assert( iLook
2e24c 41 68 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20  Ahead!=YYNOCODE 
2e24d 29 3b 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41  );.  i += iLookA
2e24e 68 65 61 64 3b 0a 23 69 66 64 65 66 20 59 59 45  head;.#ifdef YYE
2e24f 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 66 28  RRORSYMBOL.  if(
2e250 20 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f 53 5a   i<0 || i>=YY_SZ
2e251 5f 41 43 54 54 41 42 20 7c 7c 20 79 79 5f 6c 6f  _ACTTAB || yy_lo
2e252 6f 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f  okahead[i]!=iLoo
2e253 6b 41 68 65 61 64 20 29 7b 0a 20 20 20 20 72 65  kAhead ){.    re
2e254 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b  turn yy_default[
2e255 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 23 65  stateno];.  }.#e
2e256 6c 73 65 0a 20 20 61 73 73 65 72 74 28 20 69 3e  lse.  assert( i>
2e257 3d 30 20 26 26 20 69 3c 59 59 5f 53 5a 5f 41 43  =0 && i<YY_SZ_AC
2e258 54 54 41 42 20 29 3b 0a 20 20 61 73 73 65 72 74  TTAB );.  assert
2e259 28 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69  ( yy_lookahead[i
2e25a 5d 3d 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29 3b  ]==iLookAhead );
2e25b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
2e25c 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b 0a 7d   yy_action[i];.}
2e25d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
2e25e 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73  owing routine is
2e25f 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20 73   called if the s
2e260 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 73 2e 0a  tack overflows..
2e261 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  */.static void y
2e262 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79  yStackOverflow(y
2e263 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2e264 65 72 2c 20 59 59 4d 49 4e 4f 52 54 59 50 45 20  er, YYMINORTYPE 
2e265 2a 79 79 70 4d 69 6e 6f 72 29 7b 0a 20 20 20 73  *yypMinor){.   s
2e266 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
2e267 46 45 54 43 48 3b 0a 20 20 20 79 79 70 50 61 72  FETCH;.   yypPar
2e268 73 65 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 23 69  ser->yyidx--;.#i
2e269 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
2e26a 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
2e26b 29 7b 0a 20 20 20 20 20 66 70 72 69 6e 74 66 28  ){.     fprintf(
2e26c 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53  yyTraceFILE,"%sS
2e26d 74 61 63 6b 20 4f 76 65 72 66 6c 6f 77 21 5c 6e  tack Overflow!\n
2e26e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29  ",yyTracePrompt)
2e26f 3b 0a 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  ;.   }.#endif.  
2e270 20 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65   while( yypParse
2e271 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79  r->yyidx>=0 ) yy
2e272 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63  _pop_parser_stac
2e273 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  k(yypParser);.  
2e274 20 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73   /* Here code is
2e275 20 69 6e 73 65 72 74 65 64 20 77 68 69 63 68 20   inserted which 
2e276 77 69 6c 6c 20 65 78 65 63 75 74 65 20 69 66 20  will execute if 
2e277 74 68 65 20 70 61 72 73 65 72 0a 20 20 20 2a 2a  the parser.   **
2e278 20 73 74 61 63 6b 20 65 76 65 72 79 20 6f 76 65   stack every ove
2e279 72 66 6c 6f 77 73 20 2a 2f 0a 0a 20 20 55 4e 55  rflows */..  UNU
2e27a 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 79  SED_PARAMETER(yy
2e27b 70 4d 69 6e 6f 72 29 3b 20 2f 2a 20 53 69 6c 65  pMinor); /* Sile
2e27c 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65  nce some compile
2e27d 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0a 20 20  r warnings */.  
2e27e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2e27f 70 50 61 72 73 65 2c 20 22 70 61 72 73 65 72 20  pParse, "parser 
2e280 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 22 29  stack overflow")
2e281 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61 72 73  ;.  pParse->pars
2e282 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 20 73  eError = 1;.   s
2e283 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
2e284 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65  STORE; /* Suppre
2e285 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74  ss warning about
2e286 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61   unused %extra_a
2e287 72 67 75 6d 65 6e 74 20 76 61 72 20 2a 2f 0a 7d  rgument var */.}
2e288 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20  ../*.** Perform 
2e289 61 20 73 68 69 66 74 20 61 63 74 69 6f 6e 2e 0a  a shift action..
2e28a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  */.static void y
2e28b 79 5f 73 68 69 66 74 28 0a 20 20 79 79 50 61 72  y_shift(.  yyPar
2e28c 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20  ser *yypParser, 
2e28d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2e28e 70 61 72 73 65 72 20 74 6f 20 62 65 20 73 68 69  parser to be shi
2e28f 66 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 79 79  fted */.  int yy
2e290 4e 65 77 53 74 61 74 65 2c 20 20 20 20 20 20 20  NewState,       
2e291 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
2e292 65 77 20 73 74 61 74 65 20 74 6f 20 73 68 69 66  ew state to shif
2e293 74 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 79 79  t in */.  int yy
2e294 4d 61 6a 6f 72 2c 20 20 20 20 20 20 20 20 20 20  Major,          
2e295 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
2e296 61 6a 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68  ajor token to sh
2e297 69 66 74 20 69 6e 20 2a 2f 0a 20 20 59 59 4d 49  ift in */.  YYMI
2e298 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f  NORTYPE *yypMino
2e299 72 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  r         /* Poi
2e29a 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 69 6e 6f  nter to the mino
2e29b 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68 69 66 74  r token to shift
2e29c 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20 79 79 53 74   in */.){.  yySt
2e29d 61 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73 3b  ackEntry *yytos;
2e29e 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  .  yypParser->yy
2e29f 69 64 78 2b 2b 3b 0a 23 69 66 64 65 66 20 59 59  idx++;.#ifdef YY
2e2a0 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50  TRACKMAXSTACKDEP
2e2a1 54 48 0a 20 20 69 66 28 20 79 79 70 50 61 72 73  TH.  if( yypPars
2e2a2 65 72 2d 3e 79 79 69 64 78 3e 79 79 70 50 61 72  er->yyidx>yypPar
2e2a3 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 29 7b  ser->yyidxMax ){
2e2a4 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  .    yypParser->
2e2a5 79 79 69 64 78 4d 61 78 20 3d 20 79 79 70 50 61  yyidxMax = yypPa
2e2a6 72 73 65 72 2d 3e 79 79 69 64 78 3b 0a 20 20 7d  rser->yyidx;.  }
2e2a7 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59 53 54  .#endif.#if YYST
2e2a8 41 43 4b 44 45 50 54 48 3e 30 20 0a 20 20 69 66  ACKDEPTH>0 .  if
2e2a9 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
2e2aa 64 78 3e 3d 59 59 53 54 41 43 4b 44 45 50 54 48  dx>=YYSTACKDEPTH
2e2ab 20 29 7b 0a 20 20 20 20 79 79 53 74 61 63 6b 4f   ){.    yyStackO
2e2ac 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73 65  verflow(yypParse
2e2ad 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20  r, yypMinor);.  
2e2ae 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65    return;.  }.#e
2e2af 6c 73 65 0a 20 20 69 66 28 20 79 79 70 50 61 72  lse.  if( yypPar
2e2b0 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50  ser->yyidx>=yypP
2e2b1 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29  arser->yystksz )
2e2b2 7b 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61 63  {.    yyGrowStac
2e2b3 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  k(yypParser);.  
2e2b4 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
2e2b5 3e 79 79 69 64 78 3e 3d 79 79 70 50 61 72 73 65  >yyidx>=yypParse
2e2b6 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0a 20 20  r->yystksz ){.  
2e2b7 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66      yyStackOverf
2e2b8 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20 79  low(yypParser, y
2e2b9 79 70 4d 69 6e 6f 72 29 3b 0a 20 20 20 20 20 20  ypMinor);.      
2e2ba 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
2e2bb 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 74 6f 73  }.#endif.  yytos
2e2bc 20 3d 20 26 79 79 70 50 61 72 73 65 72 2d 3e 79   = &yypParser->y
2e2bd 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72  ystack[yypParser
2e2be 2d 3e 79 79 69 64 78 5d 3b 0a 20 20 79 79 74 6f  ->yyidx];.  yyto
2e2bf 73 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 28 59 59  s->stateno = (YY
2e2c0 41 43 54 49 4f 4e 54 59 50 45 29 79 79 4e 65 77  ACTIONTYPE)yyNew
2e2c1 53 74 61 74 65 3b 0a 20 20 79 79 74 6f 73 2d 3e  State;.  yytos->
2e2c2 6d 61 6a 6f 72 20 3d 20 28 59 59 43 4f 44 45 54  major = (YYCODET
2e2c3 59 50 45 29 79 79 4d 61 6a 6f 72 3b 0a 20 20 79  YPE)yyMajor;.  y
2e2c4 79 74 6f 73 2d 3e 6d 69 6e 6f 72 20 3d 20 2a 79  ytos->minor = *y
2e2c5 79 70 4d 69 6e 6f 72 3b 0a 23 69 66 6e 64 65 66  ypMinor;.#ifndef
2e2c6 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
2e2c7 54 72 61 63 65 46 49 4c 45 20 26 26 20 79 79 70  TraceFILE && yyp
2e2c8 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 30 20  Parser->yyidx>0 
2e2c9 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
2e2ca 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
2e2cb 65 46 49 4c 45 2c 22 25 73 53 68 69 66 74 20 25  eFILE,"%sShift %
2e2cc 64 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  d\n",yyTraceProm
2e2cd 70 74 2c 79 79 4e 65 77 53 74 61 74 65 29 3b 0a  pt,yyNewState);.
2e2ce 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
2e2cf 61 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63 6b  aceFILE,"%sStack
2e2d0 3a 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  :",yyTracePrompt
2e2d1 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  );.    for(i=1; 
2e2d2 69 3c 3d 79 79 70 50 61 72 73 65 72 2d 3e 79 79  i<=yypParser->yy
2e2d3 69 64 78 3b 20 69 2b 2b 29 0a 20 20 20 20 20 20  idx; i++).      
2e2d4 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
2e2d5 49 4c 45 2c 22 20 25 73 22 2c 79 79 54 6f 6b 65  ILE," %s",yyToke
2e2d6 6e 4e 61 6d 65 5b 79 79 70 50 61 72 73 65 72 2d  nName[yypParser-
2e2d7 3e 79 79 73 74 61 63 6b 5b 69 5d 2e 6d 61 6a 6f  >yystack[i].majo
2e2d8 72 5d 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  r]);.    fprintf
2e2d9 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 5c 6e  (yyTraceFILE,"\n
2e2da 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  ");.  }.#endif.}
2e2db 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
2e2dc 6e 67 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  ng table contain
2e2dd 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  s information ab
2e2de 6f 75 74 20 65 76 65 72 79 20 72 75 6c 65 20 74  out every rule t
2e2df 68 61 74 0a 2a 2a 20 69 73 20 75 73 65 64 20 64  hat.** is used d
2e2e0 75 72 69 6e 67 20 74 68 65 20 72 65 64 75 63 65  uring the reduce
2e2e1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
2e2e2 74 20 73 74 72 75 63 74 20 7b 0a 20 20 59 59 43  t struct {.  YYC
2e2e3 4f 44 45 54 59 50 45 20 6c 68 73 3b 20 20 20 20  ODETYPE lhs;    
2e2e4 20 20 20 20 20 2f 2a 20 53 79 6d 62 6f 6c 20 6f       /* Symbol o
2e2e5 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 20  n the left-hand 
2e2e6 73 69 64 65 20 6f 66 20 74 68 65 20 72 75 6c 65  side of the rule
2e2e7 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
2e2e8 68 61 72 20 6e 72 68 73 3b 20 20 20 20 20 2f 2a  har nrhs;     /*
2e2e9 20 4e 75 6d 62 65 72 20 6f 66 20 72 69 67 68 74   Number of right
2e2ea 2d 68 61 6e 64 20 73 69 64 65 20 73 79 6d 62 6f  -hand side symbo
2e2eb 6c 73 20 69 6e 20 74 68 65 20 72 75 6c 65 20 2a  ls in the rule *
2e2ec 2f 0a 7d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 5d  /.} yyRuleInfo[]
2e2ed 20 3d 20 7b 0a 20 20 7b 20 31 34 32 2c 20 31 20   = {.  { 142, 1 
2e2ee 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32 20 7d 2c  },.  { 143, 2 },
2e2ef 0a 20 20 7b 20 31 34 33 2c 20 31 20 7d 2c 0a 20  .  { 143, 1 },. 
2e2f0 20 7b 20 31 34 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 144, 1 },.  {
2e2f1 20 31 34 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   144, 3 },.  { 1
2e2f2 34 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 35  45, 0 },.  { 145
2e2f3 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20  , 1 },.  { 145, 
2e2f4 33 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20 31 20  3 },.  { 146, 1 
2e2f5 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c  },.  { 147, 3 },
2e2f6 0a 20 20 7b 20 31 34 39 2c 20 30 20 7d 2c 0a 20  .  { 149, 0 },. 
2e2f7 20 7b 20 31 34 39 2c 20 31 20 7d 2c 0a 20 20 7b   { 149, 1 },.  {
2e2f8 20 31 34 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   149, 2 },.  { 1
2e2f9 34 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 38  48, 0 },.  { 148
2e2fa 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20  , 1 },.  { 148, 
2e2fb 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 31 20  1 },.  { 148, 1 
2e2fc 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c  },.  { 147, 2 },
2e2fd 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20  .  { 147, 2 },. 
2e2fe 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b   { 147, 2 },.  {
2e2ff 20 31 35 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   151, 1 },.  { 1
2e300 35 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37  51, 0 },.  { 147
2e301 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 2 },.  { 147, 
2e302 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20  3 },.  { 147, 5 
2e303 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c  },.  { 147, 2 },
2e304 0a 20 20 7b 20 31 35 32 2c 20 36 20 7d 2c 0a 20  .  { 152, 6 },. 
2e305 20 7b 20 31 35 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 154, 1 },.  {
2e306 20 31 35 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   156, 0 },.  { 1
2e307 35 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 35 35  56, 3 },.  { 155
2e308 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 35 2c 20  , 1 },.  { 155, 
2e309 30 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 34 20  0 },.  { 153, 4 
2e30a 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 32 20 7d 2c  },.  { 153, 2 },
2e30b 0a 20 20 7b 20 31 35 38 2c 20 33 20 7d 2c 0a 20  .  { 158, 3 },. 
2e30c 20 7b 20 31 35 38 2c 20 31 20 7d 2c 0a 20 20 7b   { 158, 1 },.  {
2e30d 20 31 36 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   161, 3 },.  { 1
2e30e 36 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 35  62, 1 },.  { 165
2e30f 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20  , 1 },.  { 165, 
2e310 31 20 7d 2c 0a 20 20 7b 20 31 36 36 2c 20 31 20  1 },.  { 166, 1 
2e311 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c  },.  { 150, 1 },
2e312 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20  .  { 150, 1 },. 
2e313 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 150, 1 },.  {
2e314 20 31 36 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   163, 0 },.  { 1
2e315 36 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 37  63, 1 },.  { 167
2e316 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 37 2c 20  , 1 },.  { 167, 
2e317 34 20 7d 2c 0a 20 20 7b 20 31 36 37 2c 20 36 20  4 },.  { 167, 6 
2e318 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 31 20 7d 2c  },.  { 168, 1 },
2e319 0a 20 20 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20  .  { 168, 2 },. 
2e31a 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20 7b   { 169, 1 },.  {
2e31b 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   169, 1 },.  { 1
2e31c 36 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 34  64, 2 },.  { 164
2e31d 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20  , 0 },.  { 172, 
2e31e 33 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 31 20  3 },.  { 172, 1 
2e31f 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c  },.  { 173, 2 },
2e320 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d 2c 0a 20  .  { 173, 4 },. 
2e321 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20 20 7b   { 173, 3 },.  {
2e322 20 31 37 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   173, 3 },.  { 1
2e323 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33  73, 2 },.  { 173
2e324 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 2 },.  { 173, 
2e325 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 35 20  3 },.  { 173, 5 
2e326 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c  },.  { 173, 2 },
2e327 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d 2c 0a 20  .  { 173, 4 },. 
2e328 20 7b 20 31 37 33 2c 20 34 20 7d 2c 0a 20 20 7b   { 173, 4 },.  {
2e329 20 31 37 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   173, 1 },.  { 1
2e32a 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 38  73, 2 },.  { 178
2e32b 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 38 2c 20  , 0 },.  { 178, 
2e32c 31 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 30 20  1 },.  { 180, 0 
2e32d 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 32 20 7d 2c  },.  { 180, 2 },
2e32e 0a 20 20 7b 20 31 38 32 2c 20 32 20 7d 2c 0a 20  .  { 182, 2 },. 
2e32f 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20 20 7b   { 182, 3 },.  {
2e330 20 31 38 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   182, 3 },.  { 1
2e331 38 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 33  82, 3 },.  { 183
2e332 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20  , 2 },.  { 183, 
2e333 32 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 31 20  2 },.  { 183, 1 
2e334 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 31 20 7d 2c  },.  { 183, 1 },
2e335 0a 20 20 7b 20 31 38 31 2c 20 33 20 7d 2c 0a 20  .  { 181, 3 },. 
2e336 20 7b 20 31 38 31 2c 20 32 20 7d 2c 0a 20 20 7b   { 181, 2 },.  {
2e337 20 31 38 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   184, 0 },.  { 1
2e338 38 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 34  84, 2 },.  { 184
2e339 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 39 2c 20  , 2 },.  { 159, 
2e33a 30 20 7d 2c 0a 20 20 7b 20 31 35 39 2c 20 32 20  0 },.  { 159, 2 
2e33b 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 33 20 7d 2c  },.  { 185, 3 },
2e33c 0a 20 20 7b 20 31 38 35 2c 20 32 20 7d 2c 0a 20  .  { 185, 2 },. 
2e33d 20 7b 20 31 38 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 185, 1 },.  {
2e33e 20 31 38 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   186, 2 },.  { 1
2e33f 38 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 31 38 36  86, 7 },.  { 186
2e340 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20  , 5 },.  { 186, 
2e341 35 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 31 30  5 },.  { 186, 10
2e342 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 30 20 7d   },.  { 188, 0 }
2e343 2c 0a 20 20 7b 20 31 38 38 2c 20 31 20 7d 2c 0a  ,.  { 188, 1 },.
2e344 20 20 7b 20 31 37 36 2c 20 30 20 7d 2c 0a 20 20    { 176, 0 },.  
2e345 7b 20 31 37 36 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 176, 3 },.  { 
2e346 31 38 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38  189, 0 },.  { 18
2e347 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 30 2c  9, 2 },.  { 190,
2e348 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31   1 },.  { 190, 1
2e349 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d   },.  { 190, 1 }
2e34a 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a  ,.  { 147, 4 },.
2e34b 20 20 7b 20 31 39 32 2c 20 32 20 7d 2c 0a 20 20    { 192, 2 },.  
2e34c 7b 20 31 39 32 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 192, 0 },.  { 
2e34d 31 34 37 2c 20 38 20 7d 2c 0a 20 20 7b 20 31 34  147, 8 },.  { 14
2e34e 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 4 },.  { 147,
2e34f 20 31 20 7d 2c 0a 20 20 7b 20 31 36 30 2c 20 31   1 },.  { 160, 1
2e350 20 7d 2c 0a 20 20 7b 20 31 36 30 2c 20 33 20 7d   },.  { 160, 3 }
2e351 2c 0a 20 20 7b 20 31 39 35 2c 20 31 20 7d 2c 0a  ,.  { 195, 1 },.
2e352 20 20 7b 20 31 39 35 2c 20 32 20 7d 2c 0a 20 20    { 195, 2 },.  
2e353 7b 20 31 39 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 195, 1 },.  { 
2e354 31 39 34 2c 20 39 20 7d 2c 0a 20 20 7b 20 31 39  194, 9 },.  { 19
2e355 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 36 2c  6, 1 },.  { 196,
2e356 20 31 20 7d 2c 0a 20 20 7b 20 31 39 36 2c 20 30   1 },.  { 196, 0
2e357 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 32 20 7d   },.  { 204, 2 }
2e358 2c 0a 20 20 7b 20 32 30 34 2c 20 30 20 7d 2c 0a  ,.  { 204, 0 },.
2e359 20 20 7b 20 31 39 37 2c 20 33 20 7d 2c 0a 20 20    { 197, 3 },.  
2e35a 7b 20 31 39 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 197, 2 },.  { 
2e35b 31 39 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 30  197, 4 },.  { 20
2e35c 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 35 2c  5, 2 },.  { 205,
2e35d 20 31 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 30   1 },.  { 205, 0
2e35e 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 30 20 7d   },.  { 198, 0 }
2e35f 2c 0a 20 20 7b 20 31 39 38 2c 20 32 20 7d 2c 0a  ,.  { 198, 2 },.
2e360 20 20 7b 20 32 30 37 2c 20 32 20 7d 2c 0a 20 20    { 207, 2 },.  
2e361 7b 20 32 30 37 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 207, 0 },.  { 
2e362 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 30  206, 7 },.  { 20
2e363 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 30 36 2c  6, 7 },.  { 206,
2e364 20 37 20 7d 2c 0a 20 20 7b 20 31 35 37 2c 20 30   7 },.  { 157, 0
2e365 20 7d 2c 0a 20 20 7b 20 31 35 37 2c 20 32 20 7d   },.  { 157, 2 }
2e366 2c 0a 20 20 7b 20 31 39 33 2c 20 32 20 7d 2c 0a  ,.  { 193, 2 },.
2e367 20 20 7b 20 32 30 38 2c 20 31 20 7d 2c 0a 20 20    { 208, 1 },.  
2e368 7b 20 32 30 38 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 208, 2 },.  { 
2e369 32 30 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30  208, 3 },.  { 20
2e36a 38 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31 30 2c  8, 4 },.  { 210,
2e36b 20 32 20 7d 2c 0a 20 20 7b 20 32 31 30 2c 20 30   2 },.  { 210, 0
2e36c 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 30 20 7d   },.  { 209, 0 }
2e36d 2c 0a 20 20 7b 20 32 30 39 2c 20 33 20 7d 2c 0a  ,.  { 209, 3 },.
2e36e 20 20 7b 20 32 30 39 2c 20 32 20 7d 2c 0a 20 20    { 209, 2 },.  
2e36f 7b 20 32 31 31 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 211, 4 },.  { 
2e370 32 31 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30  211, 0 },.  { 20
2e371 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 32 2c  2, 0 },.  { 202,
2e372 20 33 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 34   3 },.  { 214, 4
2e373 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 32 20 7d   },.  { 214, 2 }
2e374 2c 0a 20 20 7b 20 32 31 35 2c 20 31 20 7d 2c 0a  ,.  { 215, 1 },.
2e375 20 20 7b 20 31 37 37 2c 20 31 20 7d 2c 0a 20 20    { 177, 1 },.  
2e376 7b 20 31 37 37 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 177, 1 },.  { 
2e377 31 37 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30  177, 0 },.  { 20
2e378 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 30 2c  0, 0 },.  { 200,
2e379 20 33 20 7d 2c 0a 20 20 7b 20 32 30 31 2c 20 30   3 },.  { 201, 0
2e37a 20 7d 2c 0a 20 20 7b 20 32 30 31 2c 20 32 20 7d   },.  { 201, 2 }
2e37b 2c 0a 20 20 7b 20 32 30 33 2c 20 30 20 7d 2c 0a  ,.  { 203, 0 },.
2e37c 20 20 7b 20 32 30 33 2c 20 32 20 7d 2c 0a 20 20    { 203, 2 },.  
2e37d 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 203, 4 },.  { 
2e37e 32 30 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34  203, 4 },.  { 14
2e37f 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 39 39 2c  7, 5 },.  { 199,
2e380 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20 32   0 },.  { 199, 2
2e381 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 37 20 7d   },.  { 147, 7 }
2e382 2c 0a 20 20 7b 20 32 31 37 2c 20 35 20 7d 2c 0a  ,.  { 217, 5 },.
2e383 20 20 7b 20 32 31 37 2c 20 33 20 7d 2c 0a 20 20    { 217, 3 },.  
2e384 7b 20 31 34 37 2c 20 38 20 7d 2c 0a 20 20 7b 20  { 147, 8 },.  { 
2e385 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34  147, 5 },.  { 14
2e386 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 31 38 2c  7, 6 },.  { 218,
2e387 20 32 20 7d 2c 0a 20 20 7b 20 32 31 38 2c 20 31   2 },.  { 218, 1
2e388 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 33 20 7d   },.  { 220, 3 }
2e389 2c 0a 20 20 7b 20 32 32 30 2c 20 31 20 7d 2c 0a  ,.  { 220, 1 },.
2e38a 20 20 7b 20 32 31 39 2c 20 30 20 7d 2c 0a 20 20    { 219, 0 },.  
2e38b 7b 20 32 31 39 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 219, 3 },.  { 
2e38c 32 31 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31  213, 3 },.  { 21
2e38d 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  3, 1 },.  { 175,
2e38e 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   1 },.  { 175, 3
2e38f 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d   },.  { 174, 1 }
2e390 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a  ,.  { 175, 1 },.
2e391 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20    { 175, 1 },.  
2e392 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
2e393 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37  175, 5 },.  { 17
2e394 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c  4, 1 },.  { 174,
2e395 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31   1 },.  { 175, 1
2e396 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d   },.  { 175, 1 }
2e397 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
2e398 20 20 7b 20 31 37 35 2c 20 36 20 7d 2c 0a 20 20    { 175, 6 },.  
2e399 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 175, 5 },.  { 
2e39a 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37  175, 4 },.  { 17
2e39b 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  4, 1 },.  { 175,
2e39c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   3 },.  { 175, 3
2e39d 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d   },.  { 175, 3 }
2e39e 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
2e39f 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
2e3a0 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
2e3a1 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  175, 3 },.  { 17
2e3a2 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 32 2c  5, 3 },.  { 222,
2e3a3 20 31 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 32   1 },.  { 222, 2
2e3a4 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 31 20 7d   },.  { 222, 1 }
2e3a5 2c 0a 20 20 7b 20 32 32 32 2c 20 32 20 7d 2c 0a  ,.  { 222, 2 },.
2e3a6 20 20 7b 20 32 32 33 2c 20 32 20 7d 2c 0a 20 20    { 223, 2 },.  
2e3a7 7b 20 32 32 33 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 223, 0 },.  { 
2e3a8 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37  175, 4 },.  { 17
2e3a9 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 2 },.  { 175,
2e3aa 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   3 },.  { 175, 3
2e3ab 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d   },.  { 175, 4 }
2e3ac 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a  ,.  { 175, 2 },.
2e3ad 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20    { 175, 2 },.  
2e3ae 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 175, 2 },.  { 
2e3af 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32  175, 2 },.  { 22
2e3b0 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 34 2c  4, 1 },.  { 224,
2e3b1 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35   2 },.  { 175, 5
2e3b2 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 31 20 7d   },.  { 225, 1 }
2e3b3 2c 0a 20 20 7b 20 32 32 35 2c 20 32 20 7d 2c 0a  ,.  { 225, 2 },.
2e3b4 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20    { 175, 5 },.  
2e3b5 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
2e3b6 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37  175, 5 },.  { 17
2e3b7 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 4 },.  { 175,
2e3b8 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35   4 },.  { 175, 5
2e3b9 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 35 20 7d   },.  { 227, 5 }
2e3ba 2c 0a 20 20 7b 20 32 32 37 2c 20 34 20 7d 2c 0a  ,.  { 227, 4 },.
2e3bb 20 20 7b 20 32 32 38 2c 20 32 20 7d 2c 0a 20 20    { 228, 2 },.  
2e3bc 7b 20 32 32 38 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 228, 0 },.  { 
2e3bd 32 32 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32  226, 1 },.  { 22
2e3be 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32 31 2c  6, 0 },.  { 221,
2e3bf 20 31 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 30   1 },.  { 221, 0
2e3c0 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 33 20 7d   },.  { 216, 3 }
2e3c1 2c 0a 20 20 7b 20 32 31 36 2c 20 31 20 7d 2c 0a  ,.  { 216, 1 },.
2e3c2 20 20 7b 20 31 34 37 2c 20 31 31 20 7d 2c 0a 20    { 147, 11 },. 
2e3c3 20 7b 20 32 32 39 2c 20 31 20 7d 2c 0a 20 20 7b   { 229, 1 },.  {
2e3c4 20 32 32 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   229, 0 },.  { 1
2e3c5 37 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 39  79, 0 },.  { 179
2e3c6 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20  , 3 },.  { 187, 
2e3c7 35 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 33 20  5 },.  { 187, 3 
2e3c8 7d 2c 0a 20 20 7b 20 32 33 30 2c 20 30 20 7d 2c  },.  { 230, 0 },
2e3c9 0a 20 20 7b 20 32 33 30 2c 20 32 20 7d 2c 0a 20  .  { 230, 2 },. 
2e3ca 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 147, 4 },.  {
2e3cb 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   147, 1 },.  { 1
2e3cc 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 2 },.  { 147
2e3cd 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 3 },.  { 147, 
2e3ce 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20  5 },.  { 147, 6 
2e3cf 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c  },.  { 147, 5 },
2e3d0 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20  .  { 147, 6 },. 
2e3d1 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 231, 1 },.  {
2e3d2 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   231, 1 },.  { 2
2e3d3 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 31  31, 1 },.  { 231
2e3d4 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20  , 1 },.  { 231, 
2e3d5 31 20 7d 2c 0a 20 20 7b 20 31 37 30 2c 20 32 20  1 },.  { 170, 2 
2e3d6 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 32 20 7d 2c  },.  { 171, 2 },
2e3d7 0a 20 20 7b 20 32 33 33 2c 20 31 20 7d 2c 0a 20  .  { 233, 1 },. 
2e3d8 20 7b 20 32 33 32 2c 20 31 20 7d 2c 0a 20 20 7b   { 232, 1 },.  {
2e3d9 20 32 33 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   232, 0 },.  { 1
2e3da 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 33 34  47, 5 },.  { 234
2e3db 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32 33 36 2c  , 11 },.  { 236,
2e3dc 20 31 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 31   1 },.  { 236, 1
2e3dd 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 32 20 7d   },.  { 236, 2 }
2e3de 2c 0a 20 20 7b 20 32 33 36 2c 20 30 20 7d 2c 0a  ,.  { 236, 0 },.
2e3df 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20    { 237, 1 },.  
2e3e0 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 237, 1 },.  { 
2e3e1 32 33 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33  237, 3 },.  { 23
2e3e2 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 38 2c  8, 0 },.  { 238,
2e3e3 20 33 20 7d 2c 0a 20 20 7b 20 32 33 39 2c 20 30   3 },.  { 239, 0
2e3e4 20 7d 2c 0a 20 20 7b 20 32 33 39 2c 20 32 20 7d   },.  { 239, 2 }
2e3e5 2c 0a 20 20 7b 20 32 33 35 2c 20 33 20 7d 2c 0a  ,.  { 235, 3 },.
2e3e6 20 20 7b 20 32 33 35 2c 20 32 20 7d 2c 0a 20 20    { 235, 2 },.  
2e3e7 7b 20 32 34 30 2c 20 36 20 7d 2c 0a 20 20 7b 20  { 240, 6 },.  { 
2e3e8 32 34 30 2c 20 38 20 7d 2c 0a 20 20 7b 20 32 34  240, 8 },.  { 24
2e3e9 30 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 34 30 2c  0, 5 },.  { 240,
2e3ea 20 34 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 31   4 },.  { 240, 1
2e3eb 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d   },.  { 175, 4 }
2e3ec 2c 0a 20 20 7b 20 31 37 35 2c 20 36 20 7d 2c 0a  ,.  { 175, 6 },.
2e3ed 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20    { 191, 1 },.  
2e3ee 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 191, 1 },.  { 
2e3ef 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  191, 1 },.  { 14
2e3f0 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 4 },.  { 147,
2e3f1 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33   6 },.  { 147, 3
2e3f2 20 7d 2c 0a 20 20 7b 20 32 34 32 2c 20 30 20 7d   },.  { 242, 0 }
2e3f3 2c 0a 20 20 7b 20 32 34 32 2c 20 32 20 7d 2c 0a  ,.  { 242, 2 },.
2e3f4 20 20 7b 20 32 34 31 2c 20 31 20 7d 2c 0a 20 20    { 241, 1 },.  
2e3f5 7b 20 32 34 31 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 241, 0 },.  { 
2e3f6 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  147, 1 },.  { 14
2e3f7 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 3 },.  { 147,
2e3f8 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33   1 },.  { 147, 3
2e3f9 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d   },.  { 147, 6 }
2e3fa 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a  ,.  { 147, 6 },.
2e3fb 20 20 7b 20 32 34 33 2c 20 31 20 7d 2c 0a 20 20    { 243, 1 },.  
2e3fc 7b 20 32 34 34 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 244, 0 },.  { 
2e3fd 32 34 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  244, 1 },.  { 14
2e3fe 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 1 },.  { 147,
2e3ff 20 34 20 7d 2c 0a 20 20 7b 20 32 34 35 2c 20 37   4 },.  { 245, 7
2e400 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 31 20 7d   },.  { 246, 1 }
2e401 2c 0a 20 20 7b 20 32 34 36 2c 20 33 20 7d 2c 0a  ,.  { 246, 3 },.
2e402 20 20 7b 20 32 34 37 2c 20 30 20 7d 2c 0a 20 20    { 247, 0 },.  
2e403 7b 20 32 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 247, 2 },.  { 
2e404 32 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34  248, 1 },.  { 24
2e405 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 39 2c  8, 3 },.  { 249,
2e406 20 31 20 7d 2c 0a 20 20 7b 20 32 35 30 2c 20 30   1 },.  { 250, 0
2e407 20 7d 2c 0a 20 20 7b 20 32 35 30 2c 20 32 20 7d   },.  { 250, 2 }
2e408 2c 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  ,.};..static voi
2e409 64 20 79 79 5f 61 63 63 65 70 74 28 79 79 50 61  d yy_accept(yyPa
2e40a 72 73 65 72 2a 29 3b 20 20 2f 2a 20 46 6f 72 77  rser*);  /* Forw
2e40b 61 72 64 20 44 65 63 6c 61 72 61 74 69 6f 6e 20  ard Declaration 
2e40c 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  */../*.** Perfor
2e40d 6d 20 61 20 72 65 64 75 63 65 20 61 63 74 69 6f  m a reduce actio
2e40e 6e 20 61 6e 64 20 74 68 65 20 73 68 69 66 74 20  n and the shift 
2e40f 74 68 61 74 20 6d 75 73 74 20 69 6d 6d 65 64 69  that must immedi
2e410 61 74 65 6c 79 0a 2a 2a 20 66 6f 6c 6c 6f 77 20  ately.** follow 
2e411 74 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73  the reduce..*/.s
2e412 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 72 65  tatic void yy_re
2e413 64 75 63 65 28 0a 20 20 79 79 50 61 72 73 65 72  duce(.  yyParser
2e414 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20 20   *yypParser,    
2e415 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2e416 65 72 20 2a 2f 0a 20 20 69 6e 74 20 79 79 72 75  er */.  int yyru
2e417 6c 65 6e 6f 20 20 20 20 20 20 20 20 20 20 20 20  leno            
2e418 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2e419 66 20 74 68 65 20 72 75 6c 65 20 62 79 20 77 68  f the rule by wh
2e41a 69 63 68 20 74 6f 20 72 65 64 75 63 65 20 2a 2f  ich to reduce */
2e41b 0a 29 7b 0a 20 20 69 6e 74 20 79 79 67 6f 74 6f  .){.  int yygoto
2e41c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2e41d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78        /* The nex
2e41e 74 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74  t state */.  int
2e41f 20 79 79 61 63 74 3b 20 20 20 20 20 20 20 20 20   yyact;         
2e420 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e421 54 68 65 20 6e 65 78 74 20 61 63 74 69 6f 6e 20  The next action 
2e422 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  */.  YYMINORTYPE
2e423 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 20 20 20   yygotominor;   
2e424 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20       /* The LHS 
2e425 6f 66 20 74 68 65 20 72 75 6c 65 20 72 65 64 75  of the rule redu
2e426 63 65 64 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b  ced */.  yyStack
2e427 45 6e 74 72 79 20 2a 79 79 6d 73 70 3b 20 20 20  Entry *yymsp;   
2e428 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2e429 74 6f 70 20 6f 66 20 74 68 65 20 70 61 72 73 65  top of the parse
2e42a 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69  r's stack */.  i
2e42b 6e 74 20 79 79 73 69 7a 65 3b 20 20 20 20 20 20  nt yysize;      
2e42c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e42d 2a 20 41 6d 6f 75 6e 74 20 74 6f 20 70 6f 70 20  * Amount to pop 
2e42e 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 73  the stack */.  s
2e42f 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
2e430 46 45 54 43 48 3b 0a 20 20 79 79 6d 73 70 20 3d  FETCH;.  yymsp =
2e431 20 26 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73   &yypParser->yys
2e432 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e  tack[yypParser->
2e433 79 79 69 64 78 5d 3b 0a 23 69 66 6e 64 65 66 20  yyidx];.#ifndef 
2e434 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54  NDEBUG.  if( yyT
2e435 72 61 63 65 46 49 4c 45 20 26 26 20 79 79 72 75  raceFILE && yyru
2e436 6c 65 6e 6f 3e 3d 30 20 0a 20 20 20 20 20 20 20  leno>=0 .       
2e437 20 26 26 20 79 79 72 75 6c 65 6e 6f 3c 28 69 6e   && yyruleno<(in
2e438 74 29 28 73 69 7a 65 6f 66 28 79 79 52 75 6c 65  t)(sizeof(yyRule
2e439 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 79 79 52  Name)/sizeof(yyR
2e43a 75 6c 65 4e 61 6d 65 5b 30 5d 29 29 20 29 7b 0a  uleName[0])) ){.
2e43b 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
2e43c 61 63 65 46 49 4c 45 2c 20 22 25 73 52 65 64 75  aceFILE, "%sRedu
2e43d 63 65 20 5b 25 73 5d 2e 5c 6e 22 2c 20 79 79 54  ce [%s].\n", yyT
2e43e 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20  racePrompt,.    
2e43f 20 20 79 79 52 75 6c 65 4e 61 6d 65 5b 79 79 72    yyRuleName[yyr
2e440 75 6c 65 6e 6f 5d 29 3b 0a 20 20 7d 0a 23 65 6e  uleno]);.  }.#en
2e441 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
2e442 0a 0a 20 20 2f 2a 20 53 69 6c 65 6e 63 65 20 63  ..  /* Silence c
2e443 6f 6d 70 6c 61 69 6e 74 73 20 66 72 6f 6d 20 70  omplaints from p
2e444 75 72 69 66 79 20 61 62 6f 75 74 20 79 79 67 6f  urify about yygo
2e445 74 6f 6d 69 6e 6f 72 20 62 65 69 6e 67 20 75 6e  tominor being un
2e446 69 6e 69 74 69 61 6c 69 7a 65 64 0a 20 20 2a 2a  initialized.  **
2e447 20 69 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 77   in some cases w
2e448 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64  hen it is copied
2e449 20 69 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20   into the stack 
2e44a 61 66 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77  after the follow
2e44b 69 6e 67 0a 20 20 2a 2a 20 73 77 69 74 63 68 2e  ing.  ** switch.
2e44c 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 69 73    yygotominor is
2e44d 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 77   uninitialized w
2e44e 68 65 6e 20 61 20 72 75 6c 65 20 72 65 64 75 63  hen a rule reduc
2e44f 65 73 20 74 68 61 74 20 64 6f 65 73 0a 20 20 2a  es that does.  *
2e450 2a 20 6e 6f 74 20 73 65 74 20 74 68 65 20 76 61  * not set the va
2e451 6c 75 65 20 6f 66 20 69 74 73 20 6c 65 66 74 2d  lue of its left-
2e452 68 61 6e 64 20 73 69 64 65 20 6e 6f 6e 74 65 72  hand side nonter
2e453 6d 69 6e 61 6c 2e 20 20 4c 65 61 76 69 6e 67 20  minal.  Leaving 
2e454 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75 65 20 6f  the.  ** value o
2e455 66 20 74 68 65 20 6e 6f 6e 74 65 72 6d 69 6e 61  f the nontermina
2e456 6c 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20  l uninitialized 
2e457 69 73 20 75 74 74 65 72 6c 79 20 68 61 72 6d 6c  is utterly harml
2e458 65 73 73 20 61 73 20 6c 6f 6e 67 0a 20 20 2a 2a  ess as long.  **
2e459 20 61 73 20 74 68 65 20 76 61 6c 75 65 20 69 73   as the value is
2e45a 20 6e 65 76 65 72 20 75 73 65 64 2e 20 20 53 6f   never used.  So
2e45b 20 72 65 61 6c 6c 79 20 74 68 65 20 6f 6e 6c 79   really the only
2e45c 20 74 68 69 6e 67 20 74 68 69 73 20 63 6f 64 65   thing this code
2e45d 0a 20 20 2a 2a 20 61 63 63 6f 6d 70 6c 69 73 68  .  ** accomplish
2e45e 65 73 20 69 73 20 74 6f 20 71 75 69 65 74 65 6e  es is to quieten
2e45f 20 70 75 72 69 66 79 2e 20 20 0a 20 20 2a 2a 0a   purify.  .  **.
2e460 20 20 2a 2a 20 32 30 30 37 2d 30 31 2d 31 36 3a    ** 2007-01-16:
2e461 20 20 54 68 65 20 77 69 72 65 73 68 61 72 6b 20    The wireshark 
2e462 70 72 6f 6a 65 63 74 20 28 77 77 77 2e 77 69 72  project (www.wir
2e463 65 73 68 61 72 6b 2e 6f 72 67 29 20 72 65 70 6f  eshark.org) repo
2e464 72 74 73 20 74 68 61 74 0a 20 20 2a 2a 20 77 69  rts that.  ** wi
2e465 74 68 6f 75 74 20 74 68 69 73 20 63 6f 64 65 2c  thout this code,
2e466 20 74 68 65 69 72 20 70 61 72 73 65 72 20 73 65   their parser se
2e467 67 66 61 75 6c 74 73 2e 20 20 49 27 6d 20 6e 6f  gfaults.  I'm no
2e468 74 20 73 75 72 65 20 77 68 61 74 20 74 68 65 72  t sure what ther
2e469 65 0a 20 20 2a 2a 20 70 61 72 73 65 72 20 69 73  e.  ** parser is
2e46a 20 64 6f 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74   doing to make t
2e46b 68 69 73 20 68 61 70 70 65 6e 2e 20 20 54 68 69  his happen.  Thi
2e46c 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20  s is the second 
2e46d 62 75 67 20 72 65 70 6f 72 74 0a 20 20 2a 2a 20  bug report.  ** 
2e46e 66 72 6f 6d 20 77 69 72 65 73 68 61 72 6b 20 74  from wireshark t
2e46f 68 69 73 20 77 65 65 6b 2e 20 20 43 6c 65 61 72  his week.  Clear
2e470 6c 79 20 74 68 65 79 20 61 72 65 20 73 74 72 65  ly they are stre
2e471 73 73 69 6e 67 20 4c 65 6d 6f 6e 20 69 6e 20 77  ssing Lemon in w
2e472 61 79 73 0a 20 20 2a 2a 20 74 68 61 74 20 69 74  ays.  ** that it
2e473 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72   has not been pr
2e474 65 76 69 6f 75 73 6c 79 20 73 74 72 65 73 73 65  eviously stresse
2e475 64 2e 2e 2e 20 20 28 53 51 4c 69 74 65 20 74 69  d...  (SQLite ti
2e476 63 6b 65 74 20 23 32 31 37 32 29 0a 20 20 2a 2f  cket #2172).  */
2e477 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79 67  .  /*memset(&yyg
2e478 6f 74 6f 6d 69 6e 6f 72 2c 20 30 2c 20 73 69 7a  otominor, 0, siz
2e479 65 6f 66 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 29  eof(yygotominor)
2e47a 29 3b 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  );*/.  yygotomin
2e47b 6f 72 20 3d 20 79 79 7a 65 72 6f 6d 69 6e 6f 72  or = yyzerominor
2e47c 3b 0a 0a 0a 20 20 73 77 69 74 63 68 28 20 79 79  ;...  switch( yy
2e47d 72 75 6c 65 6e 6f 20 29 7b 0a 20 20 2f 2a 20 42  ruleno ){.  /* B
2e47e 65 67 69 6e 6e 69 6e 67 20 68 65 72 65 20 61 72  eginning here ar
2e47f 65 20 74 68 65 20 72 65 64 75 63 74 69 6f 6e 20  e the reduction 
2e480 63 61 73 65 73 2e 20 20 41 20 74 79 70 69 63 61  cases.  A typica
2e481 6c 20 65 78 61 6d 70 6c 65 0a 20 20 2a 2a 20 66  l example.  ** f
2e482 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 63  ollows:.  **   c
2e483 61 73 65 20 30 3a 0a 20 20 2a 2a 20 20 23 6c 69  ase 0:.  **  #li
2e484 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 67 72 61  ne <lineno> <gra
2e485 6d 6d 61 72 66 69 6c 65 3e 0a 20 20 2a 2a 20 20  mmarfile>.  **  
2e486 20 20 20 7b 20 2e 2e 2e 20 7d 20 20 20 20 20 20     { ... }      
2e487 20 20 20 20 20 2f 2f 20 55 73 65 72 20 73 75 70       // User sup
2e488 70 6c 69 65 64 20 63 6f 64 65 0a 20 20 2a 2a 20  plied code.  ** 
2e489 20 23 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20   #line <lineno> 
2e48a 3c 74 68 69 73 66 69 6c 65 3e 0a 20 20 2a 2a 20  <thisfile>.  ** 
2e48b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 2a 2f 0a      break;.  */.
2e48c 20 20 20 20 20 20 63 61 73 65 20 30 3a 20 2f 2a        case 0: /*
2e48d 20 69 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69   input ::= cmdli
2e48e 73 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  st */.      case
2e48f 20 31 3a 20 2f 2a 20 63 6d 64 6c 69 73 74 20 3a   1: /* cmdlist :
2e490 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d 64 20  := cmdlist ecmd 
2e491 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 3a  */.      case 2:
2e492 20 2f 2a 20 63 6d 64 6c 69 73 74 20 3a 3a 3d 20   /* cmdlist ::= 
2e493 65 63 6d 64 20 2a 2f 0a 20 20 20 20 20 20 63 61  ecmd */.      ca
2e494 73 65 20 33 3a 20 2f 2a 20 65 63 6d 64 20 3a 3a  se 3: /* ecmd ::
2e495 3d 20 53 45 4d 49 20 2a 2f 0a 20 20 20 20 20 20  = SEMI */.      
2e496 63 61 73 65 20 34 3a 20 2f 2a 20 65 63 6d 64 20  case 4: /* ecmd 
2e497 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64 78  ::= explain cmdx
2e498 20 53 45 4d 49 20 2a 2f 0a 20 20 20 20 20 20 63   SEMI */.      c
2e499 61 73 65 20 31 30 3a 20 2f 2a 20 74 72 61 6e 73  ase 10: /* trans
2e49a 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2e49b 20 20 63 61 73 65 20 31 31 3a 20 2f 2a 20 74 72    case 11: /* tr
2e49c 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e  ans_opt ::= TRAN
2e49d 53 41 43 54 49 4f 4e 20 2a 2f 0a 20 20 20 20 20  SACTION */.     
2e49e 20 63 61 73 65 20 31 32 3a 20 2f 2a 20 74 72 61   case 12: /* tra
2e49f 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53  ns_opt ::= TRANS
2e4a0 41 43 54 49 4f 4e 20 6e 6d 20 2a 2f 0a 20 20 20  ACTION nm */.   
2e4a1 20 20 20 63 61 73 65 20 32 30 3a 20 2f 2a 20 73     case 20: /* s
2e4a2 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d  avepoint_opt ::=
2e4a3 20 53 41 56 45 50 4f 49 4e 54 20 2a 2f 0a 20 20   SAVEPOINT */.  
2e4a4 20 20 20 20 63 61 73 65 20 32 31 3a 20 2f 2a 20      case 21: /* 
2e4a5 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a  savepoint_opt ::
2e4a6 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2e4a7 32 35 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63  25: /* cmd ::= c
2e4a8 72 65 61 74 65 5f 74 61 62 6c 65 20 63 72 65 61  reate_table crea
2e4a9 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 2a 2f  te_table_args */
2e4aa 0a 20 20 20 20 20 20 63 61 73 65 20 33 34 3a 20  .      case 34: 
2e4ab 2f 2a 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a  /* columnlist ::
2e4ac 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d  = columnlist COM
2e4ad 4d 41 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20  MA column */.   
2e4ae 20 20 20 63 61 73 65 20 33 35 3a 20 2f 2a 20 63     case 35: /* c
2e4af 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f  olumnlist ::= co
2e4b0 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 63 61  lumn */.      ca
2e4b1 73 65 20 34 34 3a 20 2f 2a 20 74 79 70 65 20 3a  se 44: /* type :
2e4b2 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2e4b3 20 35 31 3a 20 2f 2a 20 73 69 67 6e 65 64 20 3a   51: /* signed :
2e4b4 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 0a 20  := plus_num */. 
2e4b5 20 20 20 20 20 63 61 73 65 20 35 32 3a 20 2f 2a       case 52: /*
2e4b6 20 73 69 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75   signed ::= minu
2e4b7 73 5f 6e 75 6d 20 2a 2f 0a 20 20 20 20 20 20 63  s_num */.      c
2e4b8 61 73 65 20 35 33 3a 20 2f 2a 20 63 61 72 67 6c  ase 53: /* cargl
2e4b9 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74  ist ::= carglist
2e4ba 20 63 61 72 67 20 2a 2f 0a 20 20 20 20 20 20 63   carg */.      c
2e4bb 61 73 65 20 35 34 3a 20 2f 2a 20 63 61 72 67 6c  ase 54: /* cargl
2e4bc 69 73 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  ist ::= */.     
2e4bd 20 63 61 73 65 20 35 35 3a 20 2f 2a 20 63 61 72   case 55: /* car
2e4be 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54  g ::= CONSTRAINT
2e4bf 20 6e 6d 20 63 63 6f 6e 73 20 2a 2f 0a 20 20 20   nm ccons */.   
2e4c0 20 20 20 63 61 73 65 20 35 36 3a 20 2f 2a 20 63     case 56: /* c
2e4c1 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 20 2a 2f  arg ::= ccons */
2e4c2 0a 20 20 20 20 20 20 63 61 73 65 20 36 32 3a 20  .      case 62: 
2e4c3 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c  /* ccons ::= NUL
2e4c4 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 20 20 20 20  L onconf */.    
2e4c5 20 20 63 61 73 65 20 38 39 3a 20 2f 2a 20 63 6f    case 89: /* co
2e4c6 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c  nslist ::= consl
2e4c7 69 73 74 20 43 4f 4d 4d 41 20 74 63 6f 6e 73 20  ist COMMA tcons 
2e4c8 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39 30  */.      case 90
2e4c9 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 20 3a 3a  : /* conslist ::
2e4ca 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73  = conslist tcons
2e4cb 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39   */.      case 9
2e4cc 31 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 20 3a  1: /* conslist :
2e4cd 3a 3d 20 74 63 6f 6e 73 20 2a 2f 0a 20 20 20 20  := tcons */.    
2e4ce 20 20 63 61 73 65 20 39 32 3a 20 2f 2a 20 74 63    case 92: /* tc
2e4cf 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49  ons ::= CONSTRAI
2e4d0 4e 54 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63  NT nm */.      c
2e4d1 61 73 65 20 32 36 38 3a 20 2f 2a 20 70 6c 75 73  ase 268: /* plus
2e4d2 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55 53 20 2a 2f  _opt ::= PLUS */
2e4d3 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 39 3a  .      case 269:
2e4d4 20 2f 2a 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d   /* plus_opt ::=
2e4d5 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2e4d6 37 39 3a 20 2f 2a 20 66 6f 72 65 61 63 68 5f 63  79: /* foreach_c
2e4d7 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20  lause ::= */.   
2e4d8 20 20 20 63 61 73 65 20 32 38 30 3a 20 2f 2a 20     case 280: /* 
2e4d9 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a  foreach_clause :
2e4da 3a 3d 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20  := FOR EACH ROW 
2e4db 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  */.      case 30
2e4dc 30 3a 20 2f 2a 20 64 61 74 61 62 61 73 65 5f 6b  0: /* database_k
2e4dd 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42 41  w_opt ::= DATABA
2e4de 53 45 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  SE */.      case
2e4df 20 33 30 31 3a 20 2f 2a 20 64 61 74 61 62 61 73   301: /* databas
2e4e0 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  e_kw_opt ::= */.
2e4e1 20 20 20 20 20 20 63 61 73 65 20 33 30 39 3a 20        case 309: 
2e4e2 2f 2a 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  /* kwcolumn_opt 
2e4e3 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2e4e4 65 20 33 31 30 3a 20 2f 2a 20 6b 77 63 6f 6c 75  e 310: /* kwcolu
2e4e5 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d  mn_opt ::= COLUM
2e4e6 4e 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  NKW */.      cas
2e4e7 65 20 33 31 34 3a 20 2f 2a 20 76 74 61 62 61 72  e 314: /* vtabar
2e4e8 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72  glist ::= vtabar
2e4e9 67 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  g */.      case 
2e4ea 33 31 35 3a 20 2f 2a 20 76 74 61 62 61 72 67 6c  315: /* vtabargl
2e4eb 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c  ist ::= vtabargl
2e4ec 69 73 74 20 43 4f 4d 4d 41 20 76 74 61 62 61 72  ist COMMA vtabar
2e4ed 67 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  g */.      case 
2e4ee 33 31 37 3a 20 2f 2a 20 76 74 61 62 61 72 67 20  317: /* vtabarg 
2e4ef 3a 3a 3d 20 76 74 61 62 61 72 67 20 76 74 61 62  ::= vtabarg vtab
2e4f0 61 72 67 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  argtoken */.    
2e4f1 20 20 63 61 73 65 20 33 32 31 3a 20 2f 2a 20 61    case 321: /* a
2e4f2 6e 79 6c 69 73 74 20 3a 3a 3d 20 2a 2f 0a 7b 0a  nylist ::= */.{.
2e4f3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e4f4 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 20 2f  .      case 5: /
2e4f5 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 2a 2f  * explain ::= */
2e4f6 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50  .{ sqlite3BeginP
2e4f7 61 72 73 65 28 70 50 61 72 73 65 2c 20 30 29 3b  arse(pParse, 0);
2e4f8 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2e4f9 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 3a 20  ;.      case 6: 
2e4fa 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45  /* explain ::= E
2e4fb 58 50 4c 41 49 4e 20 2a 2f 0a 7b 20 73 71 6c 69  XPLAIN */.{ sqli
2e4fc 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 70 50  te3BeginParse(pP
2e4fd 61 72 73 65 2c 20 31 29 3b 20 7d 0a 20 20 20 20  arse, 1); }.    
2e4fe 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e4ff 20 63 61 73 65 20 37 3a 20 2f 2a 20 65 78 70 6c   case 7: /* expl
2e500 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20  ain ::= EXPLAIN 
2e501 51 55 45 52 59 20 50 4c 41 4e 20 2a 2f 0a 7b 20  QUERY PLAN */.{ 
2e502 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73  sqlite3BeginPars
2e503 65 28 70 50 61 72 73 65 2c 20 32 29 3b 20 7d 0a  e(pParse, 2); }.
2e504 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e505 20 20 20 20 20 63 61 73 65 20 38 3a 20 2f 2a 20       case 8: /* 
2e506 63 6d 64 78 20 3a 3a 3d 20 63 6d 64 20 2a 2f 0a  cmdx ::= cmd */.
2e507 7b 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  { sqlite3FinishC
2e508 6f 64 69 6e 67 28 70 50 61 72 73 65 29 3b 20 7d  oding(pParse); }
2e509 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e50a 20 20 20 20 20 20 63 61 73 65 20 39 3a 20 2f 2a        case 9: /*
2e50b 20 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74   cmd ::= BEGIN t
2e50c 72 61 6e 73 74 79 70 65 20 74 72 61 6e 73 5f 6f  ranstype trans_o
2e50d 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 42 65  pt */.{sqlite3Be
2e50e 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  ginTransaction(p
2e50f 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d  Parse, yymsp[-1]
2e510 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a  .minor.yy194);}.
2e511 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e512 20 20 20 20 20 63 61 73 65 20 31 33 3a 20 2f 2a       case 13: /*
2e513 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 2a   transtype ::= *
2e514 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2e515 79 31 39 34 20 3d 20 54 4b 5f 44 45 46 45 52 52  y194 = TK_DEFERR
2e516 45 44 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ED;}.        bre
2e517 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e518 34 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20  4: /* transtype 
2e519 3a 3a 3d 20 44 45 46 45 52 52 45 44 20 2a 2f 0a  ::= DEFERRED */.
2e51a 20 20 20 20 20 20 63 61 73 65 20 31 35 3a 20 2f        case 15: /
2e51b 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20  * transtype ::= 
2e51c 49 4d 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20 20  IMMEDIATE */.   
2e51d 20 20 20 63 61 73 65 20 31 36 3a 20 2f 2a 20 74     case 16: /* t
2e51e 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43  ranstype ::= EXC
2e51f 4c 55 53 49 56 45 20 2a 2f 0a 20 20 20 20 20 20  LUSIVE */.      
2e520 63 61 73 65 20 31 31 34 3a 20 2f 2a 20 6d 75 6c  case 114: /* mul
2e521 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20  tiselect_op ::= 
2e522 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 63  UNION */.      c
2e523 61 73 65 20 31 31 36 3a 20 2f 2a 20 6d 75 6c 74  ase 116: /* mult
2e524 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 45  iselect_op ::= E
2e525 58 43 45 50 54 7c 49 4e 54 45 52 53 45 43 54 20  XCEPT|INTERSECT 
2e526 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e527 79 79 31 39 34 20 3d 20 79 79 6d 73 70 5b 30 5d  yy194 = yymsp[0]
2e528 2e 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20 20 20  .major;}.       
2e529 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e52a 73 65 20 31 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 17: /* cmd ::
2e52b 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f  = COMMIT trans_o
2e52c 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pt */.      case
2e52d 20 31 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20   18: /* cmd ::= 
2e52e 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f  END trans_opt */
2e52f 0a 7b 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54  .{sqlite3CommitT
2e530 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73  ransaction(pPars
2e531 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  e);}.        bre
2e532 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e533 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f  9: /* cmd ::= RO
2e534 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74  LLBACK trans_opt
2e535 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 6f 6c 6c   */.{sqlite3Roll
2e536 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28  backTransaction(
2e537 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20  pParse);}.      
2e538 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e539 61 73 65 20 32 32 3a 20 2f 2a 20 63 6d 64 20 3a  ase 22: /* cmd :
2e53a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 6e 6d 20  := SAVEPOINT nm 
2e53b 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61  */.{.  sqlite3Sa
2e53c 76 65 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20  vepoint(pParse, 
2e53d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 2c  SAVEPOINT_BEGIN,
2e53e 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
2e53f 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
2e540 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e541 73 65 20 32 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 23: /* cmd ::
2e542 3d 20 52 45 4c 45 41 53 45 20 73 61 76 65 70 6f  = RELEASE savepo
2e543 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a  int_opt nm */.{.
2e544 20 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69    sqlite3Savepoi
2e545 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56 45 50  nt(pParse, SAVEP
2e546 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 26 79  OINT_RELEASE, &y
2e547 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e548 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2e549 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e54a 32 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52  24: /* cmd ::= R
2e54b 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70  OLLBACK trans_op
2e54c 74 20 54 4f 20 73 61 76 65 70 6f 69 6e 74 5f 6f  t TO savepoint_o
2e54d 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c  pt nm */.{.  sql
2e54e 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 70 50  ite3Savepoint(pP
2e54f 61 72 73 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f  arse, SAVEPOINT_
2e550 52 4f 4c 4c 42 41 43 4b 2c 20 26 79 79 6d 73 70  ROLLBACK, &yymsp
2e551 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2e552 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e553 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 3a 20  .      case 26: 
2e554 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20  /* create_table 
2e555 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d  ::= createkw tem
2e556 70 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69  p TABLE ifnotexi
2e557 73 74 73 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b  sts nm dbnm */.{
2e558 0a 20 20 20 73 71 6c 69 74 65 33 53 74 61 72 74  .   sqlite3Start
2e559 54 61 62 6c 65 28 70 50 61 72 73 65 2c 26 79 79  Table(pParse,&yy
2e55a 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2e55b 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
2e55c 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e  r.yy0,yymsp[-4].
2e55d 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 30 2c 30 2c  minor.yy194,0,0,
2e55e 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e55f 79 79 31 39 34 29 3b 0a 7d 0a 20 20 20 20 20 20  yy194);.}.      
2e560 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e561 61 73 65 20 32 37 3a 20 2f 2a 20 63 72 65 61 74  ase 27: /* creat
2e562 65 6b 77 20 3a 3a 3d 20 43 52 45 41 54 45 20 2a  ekw ::= CREATE *
2e563 2f 0a 7b 0a 20 20 70 50 61 72 73 65 2d 3e 64 62  /.{.  pParse->db
2e564 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
2e565 62 6c 65 64 20 3d 20 30 3b 0a 20 20 79 79 67 6f  bled = 0;.  yygo
2e566 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79  tominor.yy0 = yy
2e567 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2e568 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2e569 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  k;.      case 28
2e56a 3a 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74 73  : /* ifnotexists
2e56b 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2e56c 73 65 20 33 31 3a 20 2f 2a 20 74 65 6d 70 20 3a  se 31: /* temp :
2e56d 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2e56e 20 37 30 3a 20 2f 2a 20 61 75 74 6f 69 6e 63 20   70: /* autoinc 
2e56f 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2e570 65 20 38 34 3a 20 2f 2a 20 69 6e 69 74 5f 64 65  e 84: /* init_de
2e571 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20  ferred_pred_opt 
2e572 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2e573 65 20 38 36 3a 20 2f 2a 20 69 6e 69 74 5f 64 65  e 86: /* init_de
2e574 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20  ferred_pred_opt 
2e575 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d  ::= INITIALLY IM
2e576 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20 20 20 20  MEDIATE */.     
2e577 20 63 61 73 65 20 39 37 3a 20 2f 2a 20 64 65 66   case 97: /* def
2e578 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
2e579 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2e57a 73 65 20 31 30 38 3a 20 2f 2a 20 69 66 65 78 69  se 108: /* ifexi
2e57b 73 74 73 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20  sts ::= */.     
2e57c 20 63 61 73 65 20 31 31 39 3a 20 2f 2a 20 64 69   case 119: /* di
2e57d 73 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 20 2a  stinct ::= ALL *
2e57e 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 30  /.      case 120
2e57f 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a  : /* distinct ::
2e580 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2e581 32 32 32 3a 20 2f 2a 20 62 65 74 77 65 65 6e 5f  222: /* between_
2e582 6f 70 20 3a 3a 3d 20 42 45 54 57 45 45 4e 20 2a  op ::= BETWEEN *
2e583 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 35  /.      case 225
2e584 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 49  : /* in_op ::= I
2e585 4e 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  N */.{yygotomino
2e586 72 2e 79 79 31 39 34 20 3d 20 30 3b 7d 0a 20 20  r.yy194 = 0;}.  
2e587 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e588 20 20 20 63 61 73 65 20 32 39 3a 20 2f 2a 20 69     case 29: /* i
2e589 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49  fnotexists ::= I
2e58a 46 20 4e 4f 54 20 45 58 49 53 54 53 20 2a 2f 0a  F NOT EXISTS */.
2e58b 20 20 20 20 20 20 63 61 73 65 20 33 30 3a 20 2f        case 30: /
2e58c 2a 20 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 20  * temp ::= TEMP 
2e58d 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 37 31  */.      case 71
2e58e 3a 20 2f 2a 20 61 75 74 6f 69 6e 63 20 3a 3a 3d  : /* autoinc ::=
2e58f 20 41 55 54 4f 49 4e 43 52 20 2a 2f 0a 20 20 20   AUTOINCR */.   
2e590 20 20 20 63 61 73 65 20 38 35 3a 20 2f 2a 20 69     case 85: /* i
2e591 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
2e592 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41  d_opt ::= INITIA
2e593 4c 4c 59 20 44 45 46 45 52 52 45 44 20 2a 2f 0a  LLY DEFERRED */.
2e594 20 20 20 20 20 20 63 61 73 65 20 31 30 37 3a 20        case 107: 
2e595 2f 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20  /* ifexists ::= 
2e596 49 46 20 45 58 49 53 54 53 20 2a 2f 0a 20 20 20  IF EXISTS */.   
2e597 20 20 20 63 61 73 65 20 31 31 38 3a 20 2f 2a 20     case 118: /* 
2e598 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 44 49 53  distinct ::= DIS
2e599 54 49 4e 43 54 20 2a 2f 0a 20 20 20 20 20 20 63  TINCT */.      c
2e59a 61 73 65 20 32 32 33 3a 20 2f 2a 20 62 65 74 77  ase 223: /* betw
2e59b 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42  een_op ::= NOT B
2e59c 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20  ETWEEN */.      
2e59d 63 61 73 65 20 32 32 36 3a 20 2f 2a 20 69 6e 5f  case 226: /* in_
2e59e 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 20 2a 2f  op ::= NOT IN */
2e59f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2e5a0 31 39 34 20 3d 20 31 3b 7d 0a 20 20 20 20 20 20  194 = 1;}.      
2e5a1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e5a2 61 73 65 20 33 32 3a 20 2f 2a 20 63 72 65 61 74  ase 32: /* creat
2e5a3 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d  e_table_args ::=
2e5a4 20 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63   LP columnlist c
2e5a5 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 52 50 20 2a  onslist_opt RP *
2e5a6 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64  /.{.  sqlite3End
2e5a7 54 61 62 6c 65 28 70 50 61 72 73 65 2c 26 79 79  Table(pParse,&yy
2e5a8 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2e5a9 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
2e5aa 72 2e 79 79 30 2c 30 29 3b 0a 7d 0a 20 20 20 20  r.yy0,0);.}.    
2e5ab 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e5ac 20 63 61 73 65 20 33 33 3a 20 2f 2a 20 63 72 65   case 33: /* cre
2e5ad 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a  ate_table_args :
2e5ae 3a 3d 20 41 53 20 73 65 6c 65 63 74 20 2a 2f 0a  := AS select */.
2e5af 7b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61  {.  sqlite3EndTa
2e5b0 62 6c 65 28 70 50 61 72 73 65 2c 30 2c 30 2c 79  ble(pParse,0,0,y
2e5b1 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e5b2 32 34 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  243);.  sqlite3S
2e5b3 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72  electDelete(pPar
2e5b4 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 30 5d  se->db, yymsp[0]
2e5b5 2e 6d 69 6e 6f 72 2e 79 79 32 34 33 29 3b 0a 7d  .minor.yy243);.}
2e5b6 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e5b7 20 20 20 20 20 20 63 61 73 65 20 33 36 3a 20 2f        case 36: /
2e5b8 2a 20 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c  * column ::= col
2e5b9 75 6d 6e 69 64 20 74 79 70 65 20 63 61 72 67 6c  umnid type cargl
2e5ba 69 73 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  ist */.{.  yygot
2e5bb 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79  ominor.yy0.z = y
2e5bc 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e5bd 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y0.z;.  yygotomi
2e5be 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74  nor.yy0.n = (int
2e5bf 29 28 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54  )(pParse->sLastT
2e5c0 6f 6b 65 6e 2e 7a 2d 79 79 6d 73 70 5b 2d 32 5d  oken.z-yymsp[-2]
2e5c1 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 20 2b 20  .minor.yy0.z) + 
2e5c2 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b  pParse->sLastTok
2e5c3 65 6e 2e 6e 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  en.n;.  yygotomi
2e5c4 6e 6f 72 2e 79 79 30 2e 71 75 6f 74 65 64 20 3d  nor.yy0.quoted =
2e5c5 20 30 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f   0;.  yygotomino
2e5c6 72 2e 79 79 30 2e 64 79 6e 20 3d 20 30 3b 0a 7d  r.yy0.dyn = 0;.}
2e5c7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e5c8 20 20 20 20 20 20 63 61 73 65 20 33 37 3a 20 2f        case 37: /
2e5c9 2a 20 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e  * columnid ::= n
2e5ca 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  m */.{.  sqlite3
2e5cb 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65  AddColumn(pParse
2e5cc 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2e5cd 2e 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  .yy0);.  yygotom
2e5ce 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70  inor.yy0 = yymsp
2e5cf 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d  [0].minor.yy0;.}
2e5d0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e5d1 20 20 20 20 20 20 63 61 73 65 20 33 38 3a 20 2f        case 38: /
2e5d2 2a 20 69 64 20 3a 3a 3d 20 49 44 20 2a 2f 0a 20  * id ::= ID */. 
2e5d3 20 20 20 20 20 63 61 73 65 20 33 39 3a 20 2f 2a       case 39: /*
2e5d4 20 69 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20   id ::= INDEXED 
2e5d5 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 34 30  */.      case 40
2e5d6 3a 20 2f 2a 20 69 64 73 20 3a 3a 3d 20 49 44 7c  : /* ids ::= ID|
2e5d7 53 54 52 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20  STRING */.      
2e5d8 63 61 73 65 20 34 31 3a 20 2f 2a 20 6e 6d 20 3a  case 41: /* nm :
2e5d9 3a 3d 20 69 64 20 2a 2f 0a 20 20 20 20 20 20 63  := id */.      c
2e5da 61 73 65 20 34 32 3a 20 2f 2a 20 6e 6d 20 3a 3a  ase 42: /* nm ::
2e5db 3d 20 53 54 52 49 4e 47 20 2a 2f 0a 20 20 20 20  = STRING */.    
2e5dc 20 20 63 61 73 65 20 34 33 3a 20 2f 2a 20 6e 6d    case 43: /* nm
2e5dd 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a   ::= JOIN_KW */.
2e5de 20 20 20 20 20 20 63 61 73 65 20 34 36 3a 20 2f        case 46: /
2e5df 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20  * typetoken ::= 
2e5e0 74 79 70 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  typename */.    
2e5e1 20 20 63 61 73 65 20 34 39 3a 20 2f 2a 20 74 79    case 49: /* ty
2e5e2 70 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73 20 2a  pename ::= ids *
2e5e3 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 36  /.      case 126
2e5e4 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 41 53 20 6e  : /* as ::= AS n
2e5e5 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  m */.      case 
2e5e6 31 32 37 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 69  127: /* as ::= i
2e5e7 64 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  ds */.      case
2e5e8 20 31 33 37 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a   137: /* dbnm ::
2e5e9 3d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 20 20 20 20  = DOT nm */.    
2e5ea 20 20 63 61 73 65 20 31 34 36 3a 20 2f 2a 20 69    case 146: /* i
2e5eb 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49  ndexed_opt ::= I
2e5ec 4e 44 45 58 45 44 20 42 59 20 6e 6d 20 2a 2f 0a  NDEXED BY nm */.
2e5ed 20 20 20 20 20 20 63 61 73 65 20 32 35 31 3a 20        case 251: 
2e5ee 2f 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43  /* collate ::= C
2e5ef 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 20 20  OLLATE ids */.  
2e5f0 20 20 20 20 63 61 73 65 20 32 36 30 3a 20 2f 2a      case 260: /*
2e5f1 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f   nmnum ::= plus_
2e5f2 6e 75 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  num */.      cas
2e5f3 65 20 32 36 31 3a 20 2f 2a 20 6e 6d 6e 75 6d 20  e 261: /* nmnum 
2e5f4 3a 3a 3d 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20  ::= nm */.      
2e5f5 63 61 73 65 20 32 36 32 3a 20 2f 2a 20 6e 6d 6e  case 262: /* nmn
2e5f6 75 6d 20 3a 3a 3d 20 4f 4e 20 2a 2f 0a 20 20 20  um ::= ON */.   
2e5f7 20 20 20 63 61 73 65 20 32 36 33 3a 20 2f 2a 20     case 263: /* 
2e5f8 6e 6d 6e 75 6d 20 3a 3a 3d 20 44 45 4c 45 54 45  nmnum ::= DELETE
2e5f9 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2e5fa 36 34 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d  64: /* nmnum ::=
2e5fb 20 44 45 46 41 55 4c 54 20 2a 2f 0a 20 20 20 20   DEFAULT */.    
2e5fc 20 20 63 61 73 65 20 32 36 35 3a 20 2f 2a 20 70    case 265: /* p
2e5fd 6c 75 73 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73  lus_num ::= plus
2e5fe 5f 6f 70 74 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  _opt number */. 
2e5ff 20 20 20 20 20 63 61 73 65 20 32 36 36 3a 20 2f       case 266: /
2e600 2a 20 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20  * minus_num ::= 
2e601 4d 49 4e 55 53 20 6e 75 6d 62 65 72 20 2a 2f 0a  MINUS number */.
2e602 20 20 20 20 20 20 63 61 73 65 20 32 36 37 3a 20        case 267: 
2e603 2f 2a 20 6e 75 6d 62 65 72 20 3a 3a 3d 20 49 4e  /* number ::= IN
2e604 54 45 47 45 52 7c 46 4c 4f 41 54 20 2a 2f 0a 7b  TEGER|FLOAT */.{
2e605 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20  yygotominor.yy0 
2e606 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
2e607 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20 20 20 62  .yy0;}.        b
2e608 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e609 20 34 35 3a 20 2f 2a 20 74 79 70 65 20 3a 3a 3d   45: /* type ::=
2e60a 20 74 79 70 65 74 6f 6b 65 6e 20 2a 2f 0a 7b 73   typetoken */.{s
2e60b 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54  qlite3AddColumnT
2e60c 79 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  ype(pParse,&yyms
2e60d 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2e60e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e60f 0a 20 20 20 20 20 20 63 61 73 65 20 34 37 3a 20  .      case 47: 
2e610 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d  /* typetoken ::=
2e611 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67   typename LP sig
2e612 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79  ned RP */.{.  yy
2e613 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  gotominor.yy0.z 
2e614 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  = yymsp[-3].mino
2e615 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74  r.yy0.z;.  yygot
2e616 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28  ominor.yy0.n = (
2e617 69 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d  int)(&yymsp[0].m
2e618 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
2e619 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
2e61a 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e   - yymsp[-3].min
2e61b 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20  or.yy0.z);.}.   
2e61c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e61d 20 20 63 61 73 65 20 34 38 3a 20 2f 2a 20 74 79    case 48: /* ty
2e61e 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
2e61f 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 43  name LP signed C
2e620 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50 20 2a  OMMA signed RP *
2e621 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2e622 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b  r.yy0.z = yymsp[
2e623 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -5].minor.yy0.z;
2e624 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2e625 79 30 2e 6e 20 3d 20 28 69 6e 74 29 28 26 79 79  y0.n = (int)(&yy
2e626 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2e627 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
2e628 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70  r.yy0.n] - yymsp
2e629 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-5].minor.yy0.z
2e62a 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2e62b 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35  ak;.      case 5
2e62c 30 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a  0: /* typename :
2e62d 3a 3d 20 74 79 70 65 6e 61 6d 65 20 69 64 73 20  := typename ids 
2e62e 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e62f 79 79 30 2e 7a 3d 79 79 6d 73 70 5b 2d 31 5d 2e  yy0.z=yymsp[-1].
2e630 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 20 79 79 67  minor.yy0.z; yyg
2e631 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 79  otominor.yy0.n=y
2e632 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e633 30 2e 6e 2b 28 69 6e 74 29 28 79 79 6d 73 70 5b  0.n+(int)(yymsp[
2e634 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 2d 79  0].minor.yy0.z-y
2e635 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e636 79 30 2e 7a 29 3b 7d 0a 20 20 20 20 20 20 20 20  y0.z);}.        
2e637 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e638 65 20 35 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 57: /* ccons :
2e639 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72 6d 20  := DEFAULT term 
2e63a 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 35 39  */.      case 59
2e63b 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44  : /* ccons ::= D
2e63c 45 46 41 55 4c 54 20 50 4c 55 53 20 74 65 72 6d  EFAULT PLUS term
2e63d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 44   */.{sqlite3AddD
2e63e 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72  efaultValue(pPar
2e63f 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  se,yymsp[0].mino
2e640 72 2e 79 79 37 32 29 3b 7d 0a 20 20 20 20 20 20  r.yy72);}.      
2e641 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e642 61 73 65 20 35 38 3a 20 2f 2a 20 63 63 6f 6e 73  ase 58: /* ccons
2e643 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4c 50 20   ::= DEFAULT LP 
2e644 65 78 70 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69  expr RP */.{sqli
2e645 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c  te3AddDefaultVal
2e646 75 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  ue(pParse,yymsp[
2e647 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 29 3b  -1].minor.yy72);
2e648 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e649 0a 20 20 20 20 20 20 63 61 73 65 20 36 30 3a 20  .      case 60: 
2e64a 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  /* ccons ::= DEF
2e64b 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d 20  AULT MINUS term 
2e64c 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d  */.{.  Expr *p =
2e64d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2e64e 61 72 73 65 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c  arse, TK_UMINUS,
2e64f 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e650 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73  yy72, 0, 0);.  s
2e651 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 70  qlite3ExprSpan(p
2e652 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
2e653 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
2e654 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e  minor.yy72->span
2e655 29 3b 0a 20 20 73 71 6c 69 74 65 33 41 64 64 44  );.  sqlite3AddD
2e656 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72  efaultValue(pPar
2e657 73 65 2c 70 29 3b 0a 7d 0a 20 20 20 20 20 20 20  se,p);.}.       
2e658 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e659 73 65 20 36 31 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 61: /* ccons 
2e65a 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69 64 20 2a  ::= DEFAULT id *
2e65b 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  /.{.  Expr *p = 
2e65c 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2e65d 72 73 65 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20  rse, TK_STRING, 
2e65e 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  0, 0, &yymsp[0].
2e65f 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71  minor.yy0);.  sq
2e660 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56  lite3AddDefaultV
2e661 61 6c 75 65 28 70 50 61 72 73 65 2c 70 29 3b 0a  alue(pParse,p);.
2e662 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e663 0a 20 20 20 20 20 20 63 61 73 65 20 36 33 3a 20  .      case 63: 
2e664 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f 54  /* ccons ::= NOT
2e665 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 0a   NULL onconf */.
2e666 7b 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75  {sqlite3AddNotNu
2e667 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ll(pParse, yymsp
2e668 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29  [0].minor.yy194)
2e669 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2e66a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 34 3a  ;.      case 64:
2e66b 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 50 52   /* ccons ::= PR
2e66c 49 4d 41 52 59 20 4b 45 59 20 73 6f 72 74 6f 72  IMARY KEY sortor
2e66d 64 65 72 20 6f 6e 63 6f 6e 66 20 61 75 74 6f 69  der onconf autoi
2e66e 6e 63 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  nc */.{sqlite3Ad
2e66f 64 50 72 69 6d 61 72 79 4b 65 79 28 70 50 61 72  dPrimaryKey(pPar
2e670 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  se,0,yymsp[-1].m
2e671 69 6e 6f 72 2e 79 79 31 39 34 2c 79 79 6d 73 70  inor.yy194,yymsp
2e672 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c  [0].minor.yy194,
2e673 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e674 79 79 31 39 34 29 3b 7d 0a 20 20 20 20 20 20 20  yy194);}.       
2e675 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e676 73 65 20 36 35 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 65: /* ccons 
2e677 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63 6f 6e  ::= UNIQUE oncon
2e678 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72 65  f */.{sqlite3Cre
2e679 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ateIndex(pParse,
2e67a 30 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d  0,0,0,0,yymsp[0]
2e67b 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 30 2c 30  .minor.yy194,0,0
2e67c 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  ,0,0);}.        
2e67d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e67e 65 20 36 36 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 66: /* ccons :
2e67f 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72  := CHECK LP expr
2e680 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41   RP */.{sqlite3A
2e681 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e  ddCheckConstrain
2e682 74 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  t(pParse,yymsp[-
2e683 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 7d  1].minor.yy72);}
2e684 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e685 20 20 20 20 20 20 63 61 73 65 20 36 37 3a 20 2f        case 67: /
2e686 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 52 45 46 45  * ccons ::= REFE
2e687 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73  RENCES nm idxlis
2e688 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20 2a 2f  t_opt refargs */
2e689 0a 7b 73 71 6c 69 74 65 33 43 72 65 61 74 65 46  .{sqlite3CreateF
2e68a 6f 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65  oreignKey(pParse
2e68b 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  ,0,&yymsp[-2].mi
2e68c 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 31  nor.yy0,yymsp[-1
2e68d 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 79 79  ].minor.yy148,yy
2e68e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2e68f 39 34 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  94);}.        br
2e690 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e691 36 38 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  68: /* ccons ::=
2e692 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65   defer_subclause
2e693 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 65 66 65   */.{sqlite3Defe
2e694 72 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61 72  rForeignKey(pPar
2e695 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  se,yymsp[0].mino
2e696 72 2e 79 79 31 39 34 29 3b 7d 0a 20 20 20 20 20  r.yy194);}.     
2e697 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e698 63 61 73 65 20 36 39 3a 20 2f 2a 20 63 63 6f 6e  case 69: /* ccon
2e699 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64  s ::= COLLATE id
2e69a 73 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64  s */.{sqlite3Add
2e69b 43 6f 6c 6c 61 74 65 54 79 70 65 28 70 50 61 72  CollateType(pPar
2e69c 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  se, &yymsp[0].mi
2e69d 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
2e69e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e69f 63 61 73 65 20 37 32 3a 20 2f 2a 20 72 65 66 61  case 72: /* refa
2e6a0 72 67 73 20 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67  rgs ::= */.{ yyg
2e6a1 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d  otominor.yy194 =
2e6a2 20 4f 45 5f 52 65 73 74 72 69 63 74 20 2a 20 30   OE_Restrict * 0
2e6a3 78 30 31 30 31 30 31 3b 20 7d 0a 20 20 20 20 20  x010101; }.     
2e6a4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e6a5 63 61 73 65 20 37 33 3a 20 2f 2a 20 72 65 66 61  case 73: /* refa
2e6a6 72 67 73 20 3a 3a 3d 20 72 65 66 61 72 67 73 20  rgs ::= refargs 
2e6a7 72 65 66 61 72 67 20 2a 2f 0a 7b 20 79 79 67 6f  refarg */.{ yygo
2e6a8 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20  tominor.yy194 = 
2e6a9 28 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  (yymsp[-1].minor
2e6aa 2e 79 79 31 39 34 20 26 20 7e 79 79 6d 73 70 5b  .yy194 & ~yymsp[
2e6ab 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 6d  0].minor.yy497.m
2e6ac 61 73 6b 29 20 7c 20 79 79 6d 73 70 5b 30 5d 2e  ask) | yymsp[0].
2e6ad 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 76 61 6c 75  minor.yy497.valu
2e6ae 65 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  e; }.        bre
2e6af 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37  ak;.      case 7
2e6b0 34 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d  4: /* refarg ::=
2e6b1 20 4d 41 54 43 48 20 6e 6d 20 2a 2f 0a 7b 20 79   MATCH nm */.{ y
2e6b2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 39 37  ygotominor.yy497
2e6b3 2e 76 61 6c 75 65 20 3d 20 30 3b 20 20 20 20 20  .value = 0;     
2e6b4 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 39  yygotominor.yy49
2e6b5 37 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30 30 30  7.mask = 0x00000
2e6b6 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0; }.        bre
2e6b7 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37  ak;.      case 7
2e6b8 35 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d  5: /* refarg ::=
2e6b9 20 4f 4e 20 44 45 4c 45 54 45 20 72 65 66 61 63   ON DELETE refac
2e6ba 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  t */.{ yygotomin
2e6bb 6f 72 2e 79 79 34 39 37 2e 76 61 6c 75 65 20 3d  or.yy497.value =
2e6bc 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e6bd 79 79 31 39 34 3b 20 20 20 20 20 79 79 67 6f 74  yy194;     yygot
2e6be 6f 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 6d 61 73  ominor.yy497.mas
2e6bf 6b 20 3d 20 30 78 30 30 30 30 66 66 3b 20 7d 0a  k = 0x0000ff; }.
2e6c0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e6c1 20 20 20 20 20 63 61 73 65 20 37 36 3a 20 2f 2a       case 76: /*
2e6c2 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55   refarg ::= ON U
2e6c3 50 44 41 54 45 20 72 65 66 61 63 74 20 2a 2f 0a  PDATE refact */.
2e6c4 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
2e6c5 34 39 37 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73  497.value = yyms
2e6c6 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34  p[0].minor.yy194
2e6c7 3c 3c 38 3b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  <<8;  yygotomino
2e6c8 72 2e 79 79 34 39 37 2e 6d 61 73 6b 20 3d 20 30  r.yy497.mask = 0
2e6c9 78 30 30 66 66 30 30 3b 20 7d 0a 20 20 20 20 20  x00ff00; }.     
2e6ca 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e6cb 63 61 73 65 20 37 37 3a 20 2f 2a 20 72 65 66 61  case 77: /* refa
2e6cc 72 67 20 3a 3a 3d 20 4f 4e 20 49 4e 53 45 52 54  rg ::= ON INSERT
2e6cd 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67   refact */.{ yyg
2e6ce 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 39 37 2e 76  otominor.yy497.v
2e6cf 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  alue = yymsp[0].
2e6d0 6d 69 6e 6f 72 2e 79 79 31 39 34 3c 3c 31 36 3b  minor.yy194<<16;
2e6d1 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2e6d2 39 37 2e 6d 61 73 6b 20 3d 20 30 78 66 66 30 30  97.mask = 0xff00
2e6d3 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  00; }.        br
2e6d4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e6d5 37 38 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a  78: /* refact ::
2e6d6 3d 20 53 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 20  = SET NULL */.{ 
2e6d7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39  yygotominor.yy19
2e6d8 34 20 3d 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b 20  4 = OE_SetNull; 
2e6d9 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e6da 0a 20 20 20 20 20 20 63 61 73 65 20 37 39 3a 20  .      case 79: 
2e6db 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 53 45  /* refact ::= SE
2e6dc 54 20 44 45 46 41 55 4c 54 20 2a 2f 0a 7b 20 79  T DEFAULT */.{ y
2e6dd 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34  ygotominor.yy194
2e6de 20 3d 20 4f 45 5f 53 65 74 44 66 6c 74 3b 20 7d   = OE_SetDflt; }
2e6df 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e6e0 20 20 20 20 20 20 63 61 73 65 20 38 30 3a 20 2f        case 80: /
2e6e1 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53  * refact ::= CAS
2e6e2 43 41 44 45 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  CADE */.{ yygoto
2e6e3 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45  minor.yy194 = OE
2e6e4 5f 43 61 73 63 61 64 65 3b 20 7d 0a 20 20 20 20  _Cascade; }.    
2e6e5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e6e6 20 63 61 73 65 20 38 31 3a 20 2f 2a 20 72 65 66   case 81: /* ref
2e6e7 61 63 74 20 3a 3a 3d 20 52 45 53 54 52 49 43 54  act ::= RESTRICT
2e6e8 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2e6e9 72 2e 79 79 31 39 34 20 3d 20 4f 45 5f 52 65 73  r.yy194 = OE_Res
2e6ea 74 72 69 63 74 3b 20 7d 0a 20 20 20 20 20 20 20  trict; }.       
2e6eb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e6ec 73 65 20 38 32 3a 20 2f 2a 20 64 65 66 65 72 5f  se 82: /* defer_
2e6ed 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f  subclause ::= NO
2e6ee 54 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69  T DEFERRABLE ini
2e6ef 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
2e6f0 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  opt */.      cas
2e6f1 65 20 38 33 3a 20 2f 2a 20 64 65 66 65 72 5f 73  e 83: /* defer_s
2e6f2 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 44 45 46  ubclause ::= DEF
2e6f3 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66  ERRABLE init_def
2e6f4 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 2a  erred_pred_opt *
2e6f5 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39 38 3a  /.      case 98:
2e6f6 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61   /* defer_subcla
2e6f7 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65  use_opt ::= defe
2e6f8 72 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f 0a 20  r_subclause */. 
2e6f9 20 20 20 20 20 63 61 73 65 20 31 30 30 3a 20 2f       case 100: /
2e6fa 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20  * onconf ::= ON 
2e6fb 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c 76 65  CONFLICT resolve
2e6fc 74 79 70 65 20 2a 2f 0a 20 20 20 20 20 20 63 61  type */.      ca
2e6fd 73 65 20 31 30 32 3a 20 2f 2a 20 6f 72 63 6f 6e  se 102: /* orcon
2e6fe 66 20 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65  f ::= OR resolve
2e6ff 74 79 70 65 20 2a 2f 0a 20 20 20 20 20 20 63 61  type */.      ca
2e700 73 65 20 31 30 33 3a 20 2f 2a 20 72 65 73 6f 6c  se 103: /* resol
2e701 76 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65  vetype ::= raise
2e702 74 79 70 65 20 2a 2f 0a 20 20 20 20 20 20 63 61  type */.      ca
2e703 73 65 20 31 37 35 3a 20 2f 2a 20 69 6e 73 65 72  se 175: /* inser
2e704 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53 45 52 54  t_cmd ::= INSERT
2e705 20 6f 72 63 6f 6e 66 20 2a 2f 0a 7b 79 79 67 6f   orconf */.{yygo
2e706 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20  tominor.yy194 = 
2e707 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e708 79 31 39 34 3b 7d 0a 20 20 20 20 20 20 20 20 62  y194;}.        b
2e709 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e70a 20 38 37 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74   87: /* conslist
2e70b 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  _opt ::= */.{yyg
2e70c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
2e70d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   0; yygotominor.
2e70e 79 79 30 2e 7a 20 3d 20 30 3b 7d 0a 20 20 20 20  yy0.z = 0;}.    
2e70f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e710 20 63 61 73 65 20 38 38 3a 20 2f 2a 20 63 6f 6e   case 88: /* con
2e711 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f  slist_opt ::= CO
2e712 4d 4d 41 20 63 6f 6e 73 6c 69 73 74 20 2a 2f 0a  MMA conslist */.
2e713 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
2e714 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
2e715 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20 20  or.yy0;}.       
2e716 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e717 73 65 20 39 33 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 93: /* tcons 
2e718 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ::= PRIMARY KEY 
2e719 4c 50 20 69 64 78 6c 69 73 74 20 61 75 74 6f 69  LP idxlist autoi
2e71a 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a  nc RP onconf */.
2e71b 7b 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61  {sqlite3AddPrima
2e71c 72 79 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d  ryKey(pParse,yym
2e71d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
2e71e 34 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  48,yymsp[0].mino
2e71f 72 2e 79 79 31 39 34 2c 79 79 6d 73 70 5b 2d 32  r.yy194,yymsp[-2
2e720 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 30 29  ].minor.yy194,0)
2e721 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2e722 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 34 3a  ;.      case 94:
2e723 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e   /* tcons ::= UN
2e724 49 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74 20  IQUE LP idxlist 
2e725 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71  RP onconf */.{sq
2e726 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78  lite3CreateIndex
2e727 28 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 79 79  (pParse,0,0,0,yy
2e728 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2e729 31 34 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  148,yymsp[0].min
2e72a 6f 72 2e 79 79 31 39 34 2c 30 2c 30 2c 30 2c 30  or.yy194,0,0,0,0
2e72b 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2e72c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 35  k;.      case 95
2e72d 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 43  : /* tcons ::= C
2e72e 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20  HECK LP expr RP 
2e72f 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74  onconf */.{sqlit
2e730 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72  e3AddCheckConstr
2e731 61 69 6e 74 28 70 50 61 72 73 65 2c 79 79 6d 73  aint(pParse,yyms
2e732 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  p[-2].minor.yy72
2e733 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2e734 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 36  k;.      case 96
2e735 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 46  : /* tcons ::= F
2e736 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50 20 69 64  OREIGN KEY LP id
2e737 78 6c 69 73 74 20 52 50 20 52 45 46 45 52 45 4e  xlist RP REFEREN
2e738 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f  CES nm idxlist_o
2e739 70 74 20 72 65 66 61 72 67 73 20 64 65 66 65 72  pt refargs defer
2e73a 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 2a  _subclause_opt *
2e73b 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  /.{.    sqlite3C
2e73c 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28  reateForeignKey(
2e73d 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 36  pParse, yymsp[-6
2e73e 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20 26  ].minor.yy148, &
2e73f 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2e740 79 79 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  yy0, yymsp[-2].m
2e741 69 6e 6f 72 2e 79 79 31 34 38 2c 20 79 79 6d 73  inor.yy148, yyms
2e742 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  p[-1].minor.yy19
2e743 34 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  4);.    sqlite3D
2e744 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 70  eferForeignKey(p
2e745 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
2e746 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 0a 7d 0a  minor.yy194);.}.
2e747 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e748 20 20 20 20 20 63 61 73 65 20 39 39 3a 20 2f 2a       case 99: /*
2e749 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a 20   onconf ::= */. 
2e74a 20 20 20 20 20 63 61 73 65 20 31 30 31 3a 20 2f       case 101: /
2e74b 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a  * orconf ::= */.
2e74c 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2e74d 39 34 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b  94 = OE_Default;
2e74e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e74f 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 34 3a  .      case 104:
2e750 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20   /* resolvetype 
2e751 3a 3a 3d 20 49 47 4e 4f 52 45 20 2a 2f 0a 7b 79  ::= IGNORE */.{y
2e752 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34  ygotominor.yy194
2e753 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 7d 0a 20   = OE_Ignore;}. 
2e754 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e755 20 20 20 20 63 61 73 65 20 31 30 35 3a 20 2f 2a      case 105: /*
2e756 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d   resolvetype ::=
2e757 20 52 45 50 4c 41 43 45 20 2a 2f 0a 20 20 20 20   REPLACE */.    
2e758 20 20 63 61 73 65 20 31 37 36 3a 20 2f 2a 20 69    case 176: /* i
2e759 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 52 45  nsert_cmd ::= RE
2e75a 50 4c 41 43 45 20 2a 2f 0a 7b 79 79 67 6f 74 6f  PLACE */.{yygoto
2e75b 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45  minor.yy194 = OE
2e75c 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20 20 20 20  _Replace;}.     
2e75d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e75e 63 61 73 65 20 31 30 36 3a 20 2f 2a 20 63 6d 64  case 106: /* cmd
2e75f 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45 20   ::= DROP TABLE 
2e760 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
2e761 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  e */.{.  sqlite3
2e762 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65  DropTable(pParse
2e763 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2e764 2e 79 79 31 38 35 2c 20 30 2c 20 79 79 6d 73 70  .yy185, 0, yymsp
2e765 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34  [-1].minor.yy194
2e766 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2e767 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e768 30 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63  09: /* cmd ::= c
2e769 72 65 61 74 65 6b 77 20 74 65 6d 70 20 56 49 45  reatekw temp VIE
2e76a 57 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  W ifnotexists nm
2e76b 20 64 62 6e 6d 20 41 53 20 73 65 6c 65 63 74 20   dbnm AS select 
2e76c 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 43 72  */.{.  sqlite3Cr
2e76d 65 61 74 65 56 69 65 77 28 70 50 61 72 73 65 2c  eateView(pParse,
2e76e 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f   &yymsp[-7].mino
2e76f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 33  r.yy0, &yymsp[-3
2e770 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
2e771 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2e772 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0, yymsp[0].mino
2e773 72 2e 79 79 32 34 33 2c 20 79 79 6d 73 70 5b 2d  r.yy243, yymsp[-
2e774 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 20  6].minor.yy194, 
2e775 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2e776 79 79 31 39 34 29 3b 0a 7d 0a 20 20 20 20 20 20  yy194);.}.      
2e777 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e778 61 73 65 20 31 31 30 3a 20 2f 2a 20 63 6d 64 20  ase 110: /* cmd 
2e779 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57 20 69 66  ::= DROP VIEW if
2e77a 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20  exists fullname 
2e77b 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72  */.{.  sqlite3Dr
2e77c 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  opTable(pParse, 
2e77d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2e77e 79 31 38 35 2c 20 31 2c 20 79 79 6d 73 70 5b 2d  y185, 1, yymsp[-
2e77f 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b  1].minor.yy194);
2e780 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2e781 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 31  ;.      case 111
2e782 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73 65 6c  : /* cmd ::= sel
2e783 65 63 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c 65 63  ect */.{.  Selec
2e784 74 44 65 73 74 20 64 65 73 74 20 3d 20 7b 53 52  tDest dest = {SR
2e785 54 5f 4f 75 74 70 75 74 2c 20 30 2c 20 30 2c 20  T_Output, 0, 0, 
2e786 30 2c 20 30 7d 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0};.  sqlite3
2e787 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 79  Select(pParse, y
2e788 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e789 32 34 33 2c 20 26 64 65 73 74 29 3b 0a 20 20 73  243, &dest);.  s
2e78a 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2e78b 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
2e78c 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e78d 32 34 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  243);.}.        
2e78e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e78f 65 20 31 31 32 3a 20 2f 2a 20 73 65 6c 65 63 74  e 112: /* select
2e790 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 20 2a   ::= oneselect *
2e791 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2e792 79 32 34 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y243 = yymsp[0].
2e793 6d 69 6e 6f 72 2e 79 79 32 34 33 3b 7d 0a 20 20  minor.yy243;}.  
2e794 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e795 20 20 20 63 61 73 65 20 31 31 33 3a 20 2f 2a 20     case 113: /* 
2e796 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65 6c 65 63  select ::= selec
2e797 74 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70  t multiselect_op
2e798 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a   oneselect */.{.
2e799 20 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d    if( yymsp[0].m
2e79a 69 6e 6f 72 2e 79 79 32 34 33 20 29 7b 0a 20 20  inor.yy243 ){.  
2e79b 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72    yymsp[0].minor
2e79c 2e 79 79 32 34 33 2d 3e 6f 70 20 3d 20 28 75 38  .yy243->op = (u8
2e79d 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  )yymsp[-1].minor
2e79e 2e 79 79 31 39 34 3b 0a 20 20 20 20 79 79 6d 73  .yy194;.    yyms
2e79f 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  p[0].minor.yy243
2e7a0 2d 3e 70 50 72 69 6f 72 20 3d 20 79 79 6d 73 70  ->pPrior = yymsp
2e7a1 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  [-2].minor.yy243
2e7a2 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
2e7a3 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2e7a4 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
2e7a5 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e7a6 79 32 34 33 29 3b 0a 20 20 7d 0a 20 20 79 79 67  y243);.  }.  yyg
2e7a7 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 33 20 3d  otominor.yy243 =
2e7a8 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2e7a9 79 79 32 34 33 3b 0a 7d 0a 20 20 20 20 20 20 20  yy243;.}.       
2e7aa 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e7ab 73 65 20 31 31 35 3a 20 2f 2a 20 6d 75 6c 74 69  se 115: /* multi
2e7ac 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e  select_op ::= UN
2e7ad 49 4f 4e 20 41 4c 4c 20 2a 2f 0a 7b 79 79 67 6f  ION ALL */.{yygo
2e7ae 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20  tominor.yy194 = 
2e7af 54 4b 5f 41 4c 4c 3b 7d 0a 20 20 20 20 20 20 20  TK_ALL;}.       
2e7b0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e7b1 73 65 20 31 31 37 3a 20 2f 2a 20 6f 6e 65 73 65  se 117: /* onese
2e7b2 6c 65 63 74 20 3a 3a 3d 20 53 45 4c 45 43 54 20  lect ::= SELECT 
2e7b3 64 69 73 74 69 6e 63 74 20 73 65 6c 63 6f 6c 6c  distinct selcoll
2e7b4 69 73 74 20 66 72 6f 6d 20 77 68 65 72 65 5f 6f  ist from where_o
2e7b5 70 74 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 68  pt groupby_opt h
2e7b6 61 76 69 6e 67 5f 6f 70 74 20 6f 72 64 65 72 62  aving_opt orderb
2e7b7 79 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f 70 74 20  y_opt limit_opt 
2e7b8 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2e7b9 6f 72 2e 79 79 32 34 33 20 3d 20 73 71 6c 69 74  or.yy243 = sqlit
2e7ba 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72  e3SelectNew(pPar
2e7bb 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e  se,yymsp[-6].min
2e7bc 6f 72 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b 2d  or.yy148,yymsp[-
2e7bd 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 79  5].minor.yy185,y
2e7be 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2e7bf 79 37 32 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  y72,yymsp[-3].mi
2e7c0 6e 6f 72 2e 79 79 31 34 38 2c 79 79 6d 73 70 5b  nor.yy148,yymsp[
2e7c1 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 79  -2].minor.yy72,y
2e7c2 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e7c3 79 31 34 38 2c 79 79 6d 73 70 5b 2d 37 5d 2e 6d  y148,yymsp[-7].m
2e7c4 69 6e 6f 72 2e 79 79 31 39 34 2c 79 79 6d 73 70  inor.yy194,yymsp
2e7c5 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 34 2e  [0].minor.yy354.
2e7c6 70 4c 69 6d 69 74 2c 79 79 6d 73 70 5b 30 5d 2e  pLimit,yymsp[0].
2e7c7 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4f 66 66  minor.yy354.pOff
2e7c8 73 65 74 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  set);.}.        
2e7c9 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e7ca 65 20 31 32 31 3a 20 2f 2a 20 73 63 6c 70 20 3a  e 121: /* sclp :
2e7cb 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f  := selcollist CO
2e7cc 4d 4d 41 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  MMA */.      cas
2e7cd 65 20 32 34 37 3a 20 2f 2a 20 69 64 78 6c 69 73  e 247: /* idxlis
2e7ce 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 64 78  t_opt ::= LP idx
2e7cf 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f  list RP */.{yygo
2e7d0 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20  tominor.yy148 = 
2e7d1 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2e7d2 79 79 31 34 38 3b 7d 0a 20 20 20 20 20 20 20 20  yy148;}.        
2e7d3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e7d4 65 20 31 32 32 3a 20 2f 2a 20 73 63 6c 70 20 3a  e 122: /* sclp :
2e7d5 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2e7d6 20 31 35 30 3a 20 2f 2a 20 6f 72 64 65 72 62 79   150: /* orderby
2e7d7 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2e7d8 20 20 63 61 73 65 20 31 35 38 3a 20 2f 2a 20 67    case 158: /* g
2e7d9 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a  roupby_opt ::= *
2e7da 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 30  /.      case 240
2e7db 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a  : /* exprlist ::
2e7dc 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2e7dd 32 34 36 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f  246: /* idxlist_
2e7de 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f  opt ::= */.{yygo
2e7df 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20  tominor.yy148 = 
2e7e0 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2e7e1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32  k;.      case 12
2e7e2 33 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74  3: /* selcollist
2e7e3 20 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61   ::= sclp expr a
2e7e4 73 20 2a 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f  s */.{.   yygoto
2e7e5 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73 71  minor.yy148 = sq
2e7e6 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2e7e7 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
2e7e8 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38  [-2].minor.yy148
2e7e9 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
2e7ea 2e 79 79 37 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d  .yy72,yymsp[0].m
2e7eb 69 6e 6f 72 2e 79 79 30 2e 6e 3f 26 79 79 6d 73  inor.yy0.n?&yyms
2e7ec 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a 30  p[0].minor.yy0:0
2e7ed 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2e7ee 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e7ef 32 34 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73  24: /* selcollis
2e7f0 74 20 3a 3a 3d 20 73 63 6c 70 20 53 54 41 52 20  t ::= sclp STAR 
2e7f1 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d  */.{.  Expr *p =
2e7f2 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2e7f3 61 72 73 65 2c 20 54 4b 5f 41 4c 4c 2c 20 30 2c  arse, TK_ALL, 0,
2e7f4 20 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f   0, 0);.  yygoto
2e7f5 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73 71  minor.yy148 = sq
2e7f6 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2e7f7 65 6e 64 28 70 50 61 72 73 65 2c 20 79 79 6d 73  end(pParse, yyms
2e7f8 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2e7f9 38 2c 20 70 2c 20 30 29 3b 0a 7d 0a 20 20 20 20  8, p, 0);.}.    
2e7fa 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2e7fb 20 63 61 73 65 20 31 32 35 3a 20 2f 2a 20 73 65   case 125: /* se
2e7fc 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
2e7fd 70 20 6e 6d 20 44 4f 54 20 53 54 41 52 20 2a 2f  p nm DOT STAR */
2e7fe 0a 7b 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68  .{.  Expr *pRigh
2e7ff 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  t = sqlite3PExpr
2e800 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 4c 4c 2c  (pParse, TK_ALL,
2e801 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d   0, 0, &yymsp[0]
2e802 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
2e803 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 73 71 6c  xpr *pLeft = sql
2e804 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2e805 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
2e806 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e807 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 70 44  yy0);.  Expr *pD
2e808 6f 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ot = sqlite3PExp
2e809 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
2e80a 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  , pLeft, pRight,
2e80b 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e   0);.  yygotomin
2e80c 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69 74  or.yy148 = sqlit
2e80d 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2e80e 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33  (pParse,yymsp[-3
2e80f 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20 70  ].minor.yy148, p
2e810 44 6f 74 2c 20 30 29 3b 0a 7d 0a 20 20 20 20 20  Dot, 0);.}.     
2e811 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e812 63 61 73 65 20 31 32 38 3a 20 2f 2a 20 61 73 20  case 128: /* as 
2e813 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2e814 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a  nor.yy0.n = 0;}.
2e815 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e816 20 20 20 20 20 63 61 73 65 20 31 32 39 3a 20 2f       case 129: /
2e817 2a 20 66 72 6f 6d 20 3a 3a 3d 20 2a 2f 0a 7b 79  * from ::= */.{y
2e818 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35  ygotominor.yy185
2e819 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2e81a 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64  ocZero(pParse->d
2e81b 62 2c 20 73 69 7a 65 6f 66 28 2a 79 79 67 6f 74  b, sizeof(*yygot
2e81c 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 29 29 3b 7d  ominor.yy185));}
2e81d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e81e 20 20 20 20 20 20 63 61 73 65 20 31 33 30 3a 20        case 130: 
2e81f 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d  /* from ::= FROM
2e820 20 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 7b   seltablist */.{
2e821 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2e822 79 31 38 35 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y185 = yymsp[0].
2e823 6d 69 6e 6f 72 2e 79 79 31 38 35 3b 0a 20 20 73  minor.yy185;.  s
2e824 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69  qlite3SrcListShi
2e825 66 74 4a 6f 69 6e 54 79 70 65 28 79 79 67 6f 74  ftJoinType(yygot
2e826 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 29 3b 0a 7d  ominor.yy185);.}
2e827 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e828 20 20 20 20 20 20 63 61 73 65 20 31 33 31 3a 20        case 131: 
2e829 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a  /* stl_prefix ::
2e82a 3d 20 73 65 6c 74 61 62 6c 69 73 74 20 6a 6f 69  = seltablist joi
2e82b 6e 6f 70 20 2a 2f 0a 7b 0a 20 20 20 79 79 67 6f  nop */.{.   yygo
2e82c 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20  tominor.yy185 = 
2e82d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2e82e 79 79 31 38 35 3b 0a 20 20 20 69 66 28 20 79 79  yy185;.   if( yy
2e82f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20  gotominor.yy185 
2e830 26 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  && yygotominor.y
2e831 79 31 38 35 2d 3e 6e 53 72 63 3e 30 20 29 20 79  y185->nSrc>0 ) y
2e832 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35  ygotominor.yy185
2e833 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ->a[yygotominor.
2e834 79 79 31 38 35 2d 3e 6e 53 72 63 2d 31 5d 2e 6a  yy185->nSrc-1].j
2e835 6f 69 6e 74 79 70 65 20 3d 20 28 75 38 29 79 79  ointype = (u8)yy
2e836 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2e837 39 34 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  94;.}.        br
2e838 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2e839 31 33 32 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66  132: /* stl_pref
2e83a 69 78 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  ix ::= */.{yygot
2e83b 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20 30  ominor.yy185 = 0
2e83c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2e83d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 33  ;.      case 133
2e83e 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20  : /* seltablist 
2e83f 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 6e  ::= stl_prefix n
2e840 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64 65 78 65  m dbnm as indexe
2e841 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69  d_opt on_opt usi
2e842 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79  ng_opt */.{.  yy
2e843 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20  gotominor.yy185 
2e844 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
2e845 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70  AppendFromTerm(p
2e846 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e  Parse,yymsp[-6].
2e847 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 26 79 79 6d  minor.yy185,&yym
2e848 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-5].minor.yy0
2e849 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  ,&yymsp[-4].mino
2e84a 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d  r.yy0,&yymsp[-3]
2e84b 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 79 79 6d  .minor.yy0,0,yym
2e84c 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-1].minor.yy7
2e84d 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  2,yymsp[0].minor
2e84e 2e 79 79 32 35 34 29 3b 0a 20 20 73 71 6c 69 74  .yy254);.  sqlit
2e84f 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64  e3SrcListIndexed
2e850 42 79 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  By(pParse, yygot
2e851 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 20 26 79  ominor.yy185, &y
2e852 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2e853 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
2e854 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e855 20 31 33 34 3a 20 2f 2a 20 73 65 6c 74 61 62 6c   134: /* seltabl
2e856 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66  ist ::= stl_pref
2e857 69 78 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20  ix LP select RP 
2e858 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f  as on_opt using_
2e859 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67  opt */.{.    yyg
2e85a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d  otominor.yy185 =
2e85b 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
2e85c 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50  ppendFromTerm(pP
2e85d 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d  arse,yymsp[-6].m
2e85e 69 6e 6f 72 2e 79 79 31 38 35 2c 30 2c 30 2c 26  inor.yy185,0,0,&
2e85f 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e860 79 79 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  yy0,yymsp[-4].mi
2e861 6e 6f 72 2e 79 79 32 34 33 2c 79 79 6d 73 70 5b  nor.yy243,yymsp[
2e862 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 79  -1].minor.yy72,y
2e863 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e864 32 35 34 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20  254);.  }.      
2e865 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e866 61 73 65 20 31 33 35 3a 20 2f 2a 20 73 65 6c 74  ase 135: /* selt
2e867 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70  ablist ::= stl_p
2e868 72 65 66 69 78 20 4c 50 20 73 65 6c 74 61 62 6c  refix LP seltabl
2e869 69 73 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74  ist RP as on_opt
2e86a 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a   using_opt */.{.
2e86b 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 36      if( yymsp[-6
2e86c 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 3d 3d 30  ].minor.yy185==0
2e86d 20 26 26 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69   && yymsp[-2].mi
2e86e 6e 6f 72 2e 79 79 30 2e 6e 3d 3d 30 20 26 26 20  nor.yy0.n==0 && 
2e86f 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2e870 79 79 37 32 3d 3d 30 20 26 26 20 79 79 6d 73 70  yy72==0 && yymsp
2e871 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 3d  [0].minor.yy254=
2e872 3d 30 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f  =0 ){.      yygo
2e873 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d 20  tominor.yy185 = 
2e874 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2e875 79 79 31 38 35 3b 0a 20 20 20 20 7d 65 6c 73 65  yy185;.    }else
2e876 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  {.      Select *
2e877 70 53 75 62 71 75 65 72 79 3b 0a 20 20 20 20 20  pSubquery;.     
2e878 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53   sqlite3SrcListS
2e879 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 79 79 6d  hiftJoinType(yym
2e87a 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
2e87b 38 35 29 3b 0a 20 20 20 20 20 20 70 53 75 62 71  85);.      pSubq
2e87c 75 65 72 79 20 3d 20 73 71 6c 69 74 65 33 53 65  uery = sqlite3Se
2e87d 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 30  lectNew(pParse,0
2e87e 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
2e87f 2e 79 79 31 38 35 2c 30 2c 30 2c 30 2c 30 2c 30  .yy185,0,0,0,0,0
2e880 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 79 79 67  ,0,0);.      yyg
2e881 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35 20 3d  otominor.yy185 =
2e882 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
2e883 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50  ppendFromTerm(pP
2e884 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d  arse,yymsp[-6].m
2e885 69 6e 6f 72 2e 79 79 31 38 35 2c 30 2c 30 2c 26  inor.yy185,0,0,&
2e886 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2e887 79 79 30 2c 70 53 75 62 71 75 65 72 79 2c 79 79  yy0,pSubquery,yy
2e888 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2e889 37 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  72,yymsp[0].mino
2e88a 72 2e 79 79 32 35 34 29 3b 0a 20 20 20 20 7d 0a  r.yy254);.    }.
2e88b 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
2e88c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
2e88d 36 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 2a  6: /* dbnm ::= *
2e88e 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 35  /.      case 145
2e88f 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74  : /* indexed_opt
2e890 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
2e891 69 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79  inor.yy0.z=0; yy
2e892 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d  gotominor.yy0.n=
2e893 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2e894 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
2e895 38 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 3a  8: /* fullname :
2e896 3a 3d 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 79  := nm dbnm */.{y
2e897 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 35  ygotominor.yy185
2e898 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2e899 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
2e89a 64 62 2c 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  db,0,&yymsp[-1].
2e89b 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2e89c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
2e89d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e89e 20 20 20 20 20 20 63 61 73 65 20 31 33 39 3a 20        case 139: 
2e89f 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f  /* joinop ::= CO
2e8a0 4d 4d 41 7c 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79  MMA|JOIN */.{ yy
2e8a1 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20  gotominor.yy194 
2e8a2 3d 20 4a 54 5f 49 4e 4e 45 52 3b 20 7d 0a 20 20  = JT_INNER; }.  
2e8a3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e8a4 20 20 20 63 61 73 65 20 31 34 30 3a 20 2f 2a 20     case 140: /* 
2e8a5 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
2e8a6 4b 57 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67  KW JOIN */.{ yyg
2e8a7 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d  otominor.yy194 =
2e8a8 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65   sqlite3JoinType
2e8a9 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
2e8aa 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30  1].minor.yy0,0,0
2e8ab 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
2e8ac 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e8ad 34 31 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a  41: /* joinop ::
2e8ae 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49  = JOIN_KW nm JOI
2e8af 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  N */.{ yygotomin
2e8b0 6f 72 2e 79 79 31 39 34 20 3d 20 73 71 6c 69 74  or.yy194 = sqlit
2e8b1 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73  e3JoinType(pPars
2e8b2 65 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  e,&yymsp[-2].min
2e8b3 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31  or.yy0,&yymsp[-1
2e8b4 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 20  ].minor.yy0,0); 
2e8b5 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e8b6 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 32 3a  .      case 142:
2e8b7 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a   /* joinop ::= J
2e8b8 4f 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49  OIN_KW nm nm JOI
2e8b9 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  N */.{ yygotomin
2e8ba 6f 72 2e 79 79 31 39 34 20 3d 20 73 71 6c 69 74  or.yy194 = sqlit
2e8bb 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73  e3JoinType(pPars
2e8bc 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  e,&yymsp[-3].min
2e8bd 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32  or.yy0,&yymsp[-2
2e8be 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2e8bf 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2e8c0 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
2e8c1 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e8c2 34 33 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a  43: /* on_opt ::
2e8c3 3d 20 4f 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20  = ON expr */.   
2e8c4 20 20 20 63 61 73 65 20 31 35 34 3a 20 2f 2a 20     case 154: /* 
2e8c5 73 6f 72 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70  sortitem ::= exp
2e8c6 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2e8c7 31 36 31 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f  161: /* having_o
2e8c8 70 74 20 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78  pt ::= HAVING ex
2e8c9 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
2e8ca 20 31 36 38 3a 20 2f 2a 20 77 68 65 72 65 5f 6f   168: /* where_o
2e8cb 70 74 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70  pt ::= WHERE exp
2e8cc 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
2e8cd 31 38 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  183: /* expr ::=
2e8ce 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63   term */.      c
2e8cf 61 73 65 20 32 31 31 3a 20 2f 2a 20 65 73 63 61  ase 211: /* esca
2e8d0 70 65 20 3a 3a 3d 20 45 53 43 41 50 45 20 65 78  pe ::= ESCAPE ex
2e8d1 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
2e8d2 20 32 33 35 3a 20 2f 2a 20 63 61 73 65 5f 65 6c   235: /* case_el
2e8d3 73 65 20 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72  se ::= ELSE expr
2e8d4 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2e8d5 33 37 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72  37: /* case_oper
2e8d6 61 6e 64 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a  and ::= expr */.
2e8d7 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  {yygotominor.yy7
2e8d8 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  2 = yymsp[0].min
2e8d9 6f 72 2e 79 79 37 32 3b 7d 0a 20 20 20 20 20 20  or.yy72;}.      
2e8da 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e8db 61 73 65 20 31 34 34 3a 20 2f 2a 20 6f 6e 5f 6f  ase 144: /* on_o
2e8dc 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20  pt ::= */.      
2e8dd 63 61 73 65 20 31 36 30 3a 20 2f 2a 20 68 61 76  case 160: /* hav
2e8de 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  ing_opt ::= */. 
2e8df 20 20 20 20 20 63 61 73 65 20 31 36 37 3a 20 2f       case 167: /
2e8e0 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20  * where_opt ::= 
2e8e1 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  */.      case 21
2e8e2 32 3a 20 2f 2a 20 65 73 63 61 70 65 20 3a 3a 3d  2: /* escape ::=
2e8e3 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2e8e4 33 36 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65  36: /* case_else
2e8e5 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2e8e6 73 65 20 32 33 38 3a 20 2f 2a 20 63 61 73 65 5f  se 238: /* case_
2e8e7 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 2a 2f 0a 7b  operand ::= */.{
2e8e8 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2e8e9 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
2e8ea 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2e8eb 20 31 34 37 3a 20 2f 2a 20 69 6e 64 65 78 65 64   147: /* indexed
2e8ec 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44  _opt ::= NOT IND
2e8ed 45 58 45 44 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  EXED */.{yygotom
2e8ee 69 6e 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79  inor.yy0.z=0; yy
2e8ef 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d  gotominor.yy0.n=
2e8f0 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  1;}.        brea
2e8f1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
2e8f2 38 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20  8: /* using_opt 
2e8f3 3a 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69 6e 73  ::= USING LP ins
2e8f4 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f 0a 20 20  collist RP */.  
2e8f5 20 20 20 20 63 61 73 65 20 31 38 30 3a 20 2f 2a      case 180: /*
2e8f6 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
2e8f7 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73  ::= LP inscollis
2e8f8 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  t RP */.{yygotom
2e8f9 69 6e 6f 72 2e 79 79 32 35 34 20 3d 20 79 79 6d  inor.yy254 = yym
2e8fa 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-1].minor.yy2
2e8fb 35 34 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  54;}.        bre
2e8fc 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e8fd 34 39 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74  49: /* using_opt
2e8fe 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2e8ff 73 65 20 31 37 39 3a 20 2f 2a 20 69 6e 73 63 6f  se 179: /* insco
2e900 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  llist_opt ::= */
2e901 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2e902 32 35 34 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  254 = 0;}.      
2e903 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2e904 61 73 65 20 31 35 31 3a 20 2f 2a 20 6f 72 64 65  ase 151: /* orde
2e905 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45  rby_opt ::= ORDE
2e906 52 20 42 59 20 73 6f 72 74 6c 69 73 74 20 2a 2f  R BY sortlist */
2e907 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 39 3a  .      case 159:
2e908 20 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20   /* groupby_opt 
2e909 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e 65 78  ::= GROUP BY nex
2e90a 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20  prlist */.      
2e90b 63 61 73 65 20 32 33 39 3a 20 2f 2a 20 65 78 70  case 239: /* exp
2e90c 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c  rlist ::= nexprl
2e90d 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ist */.{yygotomi
2e90e 6e 6f 72 2e 79 79 31 34 38 20 3d 20 79 79 6d 73  nor.yy148 = yyms
2e90f 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38  p[0].minor.yy148
2e910 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2e911 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 32  ;.      case 152
2e912 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a  : /* sortlist ::
2e913 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41  = sortlist COMMA
2e914 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72   sortitem sortor
2e915 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  der */.{.  yygot
2e916 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73  ominor.yy148 = s
2e917 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2e918 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
2e919 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-3].minor.yy14
2e91a 38 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  8,yymsp[-1].mino
2e91b 72 2e 79 79 37 32 2c 30 29 3b 0a 20 20 69 66 28  r.yy72,0);.  if(
2e91c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2e91d 34 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  48 ) yygotominor
2e91e 2e 79 79 31 34 38 2d 3e 61 5b 79 79 67 6f 74 6f  .yy148->a[yygoto
2e91f 6d 69 6e 6f 72 2e 79 79 31 34 38 2d 3e 6e 45 78  minor.yy148->nEx
2e920 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20  pr-1].sortOrder 
2e921 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d  = (u8)yymsp[0].m
2e922 69 6e 6f 72 2e 79 79 31 39 34 3b 0a 7d 0a 20 20  inor.yy194;.}.  
2e923 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2e924 20 20 20 63 61 73 65 20 31 35 33 3a 20 2f 2a 20     case 153: /* 
2e925 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72  sortlist ::= sor
2e926 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 20  titem sortorder 
2e927 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2e928 6f 72 2e 79 79 31 34 38 20 3d 20 73 71 6c 69 74  or.yy148 = sqlit
2e929 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2e92a 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b  (pParse,0,yymsp[
2e92b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 30  -1].minor.yy72,0
2e92c 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  );.  if( yygotom
2e92d 69 6e 6f 72 2e 79 79 31 34 38 20 26 26 20 79 79  inor.yy148 && yy
2e92e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 2d  gotominor.yy148-
2e92f 3e 61 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  >a ) yygotominor
2e930 2e 79 79 31 34 38 2d 3e 61 5b 30 5d 2e 73 6f 72  .yy148->a[0].sor
2e931 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79 6d  tOrder = (u8)yym
2e932 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  sp[0].minor.yy19
2e933 34 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  4;.}.        bre
2e934 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e935 35 35 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72  55: /* sortorder
2e936 20 3a 3a 3d 20 41 53 43 20 2a 2f 0a 20 20 20 20   ::= ASC */.    
2e937 20 20 63 61 73 65 20 31 35 37 3a 20 2f 2a 20 73    case 157: /* s
2e938 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 2a 2f 0a  ortorder ::= */.
2e939 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2e93a 39 34 20 3d 20 53 51 4c 49 54 45 5f 53 4f 5f 41  94 = SQLITE_SO_A
2e93b 53 43 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  SC;}.        bre
2e93c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e93d 35 36 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72  56: /* sortorder
2e93e 20 3a 3a 3d 20 44 45 53 43 20 2a 2f 0a 7b 79 79   ::= DESC */.{yy
2e93f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20  gotominor.yy194 
2e940 3d 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43  = SQLITE_SO_DESC
2e941 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2e942 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 32  ;.      case 162
2e943 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a  : /* limit_opt :
2e944 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  := */.{yygotomin
2e945 6f 72 2e 79 79 33 35 34 2e 70 4c 69 6d 69 74 20  or.yy354.pLimit 
2e946 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  = 0; yygotominor
2e947 2e 79 79 33 35 34 2e 70 4f 66 66 73 65 74 20 3d  .yy354.pOffset =
2e948 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
2e949 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e94a 36 33 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74  63: /* limit_opt
2e94b 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20   ::= LIMIT expr 
2e94c 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2e94d 79 79 33 35 34 2e 70 4c 69 6d 69 74 20 3d 20 79  yy354.pLimit = y
2e94e 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e94f 37 32 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  72; yygotominor.
2e950 79 79 33 35 34 2e 70 4f 66 66 73 65 74 20 3d 20  yy354.pOffset = 
2e951 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2e952 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  k;.      case 16
2e953 34 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20  4: /* limit_opt 
2e954 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 4f  ::= LIMIT expr O
2e955 46 46 53 45 54 20 65 78 70 72 20 2a 2f 0a 7b 79  FFSET expr */.{y
2e956 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35 34  ygotominor.yy354
2e957 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b  .pLimit = yymsp[
2e958 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b 20  -2].minor.yy72; 
2e959 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35  yygotominor.yy35
2e95a 34 2e 70 4f 66 66 73 65 74 20 3d 20 79 79 6d 73  4.pOffset = yyms
2e95b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b  p[0].minor.yy72;
2e95c 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e95d 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 35 3a  .      case 165:
2e95e 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a   /* limit_opt ::
2e95f 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d  = LIMIT expr COM
2e960 4d 41 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  MA expr */.{yygo
2e961 74 6f 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 70 4f  tominor.yy354.pO
2e962 66 66 73 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32  ffset = yymsp[-2
2e963 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b 20 79 79  ].minor.yy72; yy
2e964 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35 34 2e  gotominor.yy354.
2e965 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30  pLimit = yymsp[0
2e966 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b 7d 0a 20  ].minor.yy72;}. 
2e967 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2e968 20 20 20 20 63 61 73 65 20 31 36 36 3a 20 2f 2a      case 166: /*
2e969 20 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20   cmd ::= DELETE 
2e96a 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e  FROM fullname in
2e96b 64 65 78 65 64 5f 6f 70 74 20 77 68 65 72 65 5f  dexed_opt where_
2e96c 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  opt */.{.  sqlit
2e96d 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64  e3SrcListIndexed
2e96e 42 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  By(pParse, yymsp
2e96f 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35  [-2].minor.yy185
2e970 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
2e971 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74  or.yy0);.  sqlit
2e972 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50 61  e3DeleteFrom(pPa
2e973 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  rse,yymsp[-2].mi
2e974 6e 6f 72 2e 79 79 31 38 35 2c 79 79 6d 73 70 5b  nor.yy185,yymsp[
2e975 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 29 3b 0a  0].minor.yy72);.
2e976 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2e977 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 39 3a  .      case 169:
2e978 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 55 50 44 41   /* cmd ::= UPDA
2e979 54 45 20 6f 72 63 6f 6e 66 20 66 75 6c 6c 6e 61  TE orconf fullna
2e97a 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 53  me indexed_opt S
2e97b 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
2e97c 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  _opt */.{.  sqli
2e97d 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65  te3SrcListIndexe
2e97e 64 42 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73  dBy(pParse, yyms
2e97f 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38  p[-4].minor.yy18
2e980 35 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  5, &yymsp[-3].mi
2e981 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
2e982 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
2e983 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 79 79  Length(pParse,yy
2e984 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2e985 31 34 38 2c 22 73 65 74 20 6c 69 73 74 22 29 3b  148,"set list");
2e986 20 0a 20 20 73 71 6c 69 74 65 33 55 70 64 61 74   .  sqlite3Updat
2e987 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  e(pParse,yymsp[-
2e988 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 79  4].minor.yy185,y
2e989 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2e98a 79 31 34 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y148,yymsp[0].mi
2e98b 6e 6f 72 2e 79 79 37 32 2c 79 79 6d 73 70 5b 2d  nor.yy72,yymsp[-
2e98c 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b  5].minor.yy194);
2e98d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2e98e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 30  ;.      case 170
2e98f 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20 3a 3a 3d  : /* setlist ::=
2e990 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e   setlist COMMA n
2e991 6d 20 45 51 20 65 78 70 72 20 2a 2f 0a 7b 79 79  m EQ expr */.{yy
2e992 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20  gotominor.yy148 
2e993 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2e994 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79  tAppend(pParse,y
2e995 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2e996 79 31 34 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y148,yymsp[0].mi
2e997 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b  nor.yy72,&yymsp[
2e998 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  -2].minor.yy0);}
2e999 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e99a 20 20 20 20 20 20 63 61 73 65 20 31 37 31 3a 20        case 171: 
2e99b 2f 2a 20 73 65 74 6c 69 73 74 20 3a 3a 3d 20 6e  /* setlist ::= n
2e99c 6d 20 45 51 20 65 78 70 72 20 2a 2f 0a 7b 79 79  m EQ expr */.{yy
2e99d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20  gotominor.yy148 
2e99e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2e99f 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
2e9a0 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
2e9a1 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  yy72,&yymsp[-2].
2e9a2 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
2e9a3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2e9a4 20 20 63 61 73 65 20 31 37 32 3a 20 2f 2a 20 63    case 172: /* c
2e9a5 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
2e9a6 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20  d INTO fullname 
2e9a7 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56  inscollist_opt V
2e9a8 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73  ALUES LP itemlis
2e9a9 74 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  t RP */.{sqlite3
2e9aa 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79  Insert(pParse, y
2e9ab 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
2e9ac 79 31 38 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  y185, yymsp[-1].
2e9ad 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20 30 2c 20  minor.yy148, 0, 
2e9ae 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2e9af 79 79 32 35 34 2c 20 79 79 6d 73 70 5b 2d 37 5d  yy254, yymsp[-7]
2e9b0 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d 0a  .minor.yy194);}.
2e9b1 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e9b2 20 20 20 20 20 63 61 73 65 20 31 37 33 3a 20 2f       case 173: /
2e9b3 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74  * cmd ::= insert
2e9b4 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61  _cmd INTO fullna
2e9b5 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  me inscollist_op
2e9b6 74 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 73 71 6c  t select */.{sql
2e9b7 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72 73  ite3Insert(pPars
2e9b8 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  e, yymsp[-2].min
2e9b9 6f 72 2e 79 79 31 38 35 2c 20 30 2c 20 79 79 6d  or.yy185, 0, yym
2e9ba 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  sp[0].minor.yy24
2e9bb 33 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  3, yymsp[-1].min
2e9bc 6f 72 2e 79 79 32 35 34 2c 20 79 79 6d 73 70 5b  or.yy254, yymsp[
2e9bd 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29  -4].minor.yy194)
2e9be 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2e9bf 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 34  ;.      case 174
2e9c0 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73  : /* cmd ::= ins
2e9c1 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c  ert_cmd INTO ful
2e9c2 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74  lname inscollist
2e9c3 5f 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41 4c  _opt DEFAULT VAL
2e9c4 55 45 53 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49  UES */.{sqlite3I
2e9c5 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79 79  nsert(pParse, yy
2e9c6 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2e9c7 31 38 35 2c 20 30 2c 20 30 2c 20 79 79 6d 73 70  185, 0, 0, yymsp
2e9c8 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 34  [-2].minor.yy254
2e9c9 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  , yymsp[-5].mino
2e9ca 72 2e 79 79 31 39 34 29 3b 7d 0a 20 20 20 20 20  r.yy194);}.     
2e9cb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e9cc 63 61 73 65 20 31 37 37 3a 20 2f 2a 20 69 74 65  case 177: /* ite
2e9cd 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d 6c 69  mlist ::= itemli
2e9ce 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f  st COMMA expr */
2e9cf 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 31 3a  .      case 241:
2e9d0 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a 3a   /* nexprlist ::
2e9d1 3d 20 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d  = nexprlist COMM
2e9d2 41 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74  A expr */.{yygot
2e9d3 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20 73  ominor.yy148 = s
2e9d4 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2e9d5 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
2e9d6 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-2].minor.yy14
2e9d7 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8,yymsp[0].minor
2e9d8 2e 79 79 37 32 2c 30 29 3b 7d 0a 20 20 20 20 20  .yy72,0);}.     
2e9d9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e9da 63 61 73 65 20 31 37 38 3a 20 2f 2a 20 69 74 65  case 178: /* ite
2e9db 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a  mlist ::= expr *
2e9dc 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 32  /.      case 242
2e9dd 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a  : /* nexprlist :
2e9de 3a 3d 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  := expr */.{yygo
2e9df 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 3d 20  tominor.yy148 = 
2e9e0 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2e9e1 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 79  ppend(pParse,0,y
2e9e2 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e9e3 37 32 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  72,0);}.        
2e9e4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2e9e5 65 20 31 38 31 3a 20 2f 2a 20 69 6e 73 63 6f 6c  e 181: /* inscol
2e9e6 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c  list ::= inscoll
2e9e7 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f 0a  ist COMMA nm */.
2e9e8 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32  {yygotominor.yy2
2e9e9 35 34 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69  54 = sqlite3IdLi
2e9ea 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d  stAppend(pParse-
2e9eb 3e 64 62 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  >db,yymsp[-2].mi
2e9ec 6e 6f 72 2e 79 79 32 35 34 2c 26 79 79 6d 73 70  nor.yy254,&yymsp
2e9ed 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
2e9ee 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e9ef 20 20 20 20 20 20 63 61 73 65 20 31 38 32 3a 20        case 182: 
2e9f0 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a  /* inscollist ::
2e9f1 3d 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  = nm */.{yygotom
2e9f2 69 6e 6f 72 2e 79 79 32 35 34 20 3d 20 73 71 6c  inor.yy254 = sql
2e9f3 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64  ite3IdListAppend
2e9f4 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79  (pParse->db,0,&y
2e9f5 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2e9f6 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2e9f7 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2e9f8 38 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  84: /* expr ::= 
2e9f9 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a 7b 79  LP expr RP */.{y
2e9fa 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2e9fb 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
2e9fc 72 2e 79 79 37 32 3b 20 73 71 6c 69 74 65 33 45  r.yy72; sqlite3E
2e9fd 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2e9fe 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b  nor.yy72,&yymsp[
2e9ff 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -2].minor.yy0,&y
2ea00 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2ea01 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  0); }.        br
2ea02 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ea03 31 38 35 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d  185: /* term ::=
2ea04 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 63   NULL */.      c
2ea05 61 73 65 20 31 39 30 3a 20 2f 2a 20 74 65 72 6d  ase 190: /* term
2ea06 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f   ::= INTEGER|FLO
2ea07 41 54 7c 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20  AT|BLOB */.     
2ea08 20 63 61 73 65 20 31 39 31 3a 20 2f 2a 20 74 65   case 191: /* te
2ea09 72 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 20 2a 2f  rm ::= STRING */
2ea0a 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2ea0b 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
2ea0c 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  r(pParse, yymsp[
2ea0d 30 5d 2e 6d 61 6a 6f 72 2c 20 30 2c 20 30 2c 20  0].major, 0, 0, 
2ea0e 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2ea0f 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
2ea10 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ea11 20 31 38 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   186: /* expr ::
2ea12 3d 20 69 64 20 2a 2f 0a 20 20 20 20 20 20 63 61  = id */.      ca
2ea13 73 65 20 31 38 37 3a 20 2f 2a 20 65 78 70 72 20  se 187: /* expr 
2ea14 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 7b  ::= JOIN_KW */.{
2ea15 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2ea16 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2ea17 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
2ea18 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  , 0, &yymsp[0].m
2ea19 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
2ea1a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ea1b 20 63 61 73 65 20 31 38 38 3a 20 2f 2a 20 65 78   case 188: /* ex
2ea1c 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d  pr ::= nm DOT nm
2ea1d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 74 65   */.{.  Expr *te
2ea1e 6d 70 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78  mp1 = sqlite3PEx
2ea1f 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44  pr(pParse, TK_ID
2ea20 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d  , 0, 0, &yymsp[-
2ea21 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  2].minor.yy0);. 
2ea22 20 45 78 70 72 20 2a 74 65 6d 70 32 20 3d 20 73   Expr *temp2 = s
2ea23 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2ea24 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
2ea25 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
2ea26 2e 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  .yy0);.  yygotom
2ea27 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69  inor.yy72 = sqli
2ea28 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2ea29 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20   TK_DOT, temp1, 
2ea2a 74 65 6d 70 32 2c 20 30 29 3b 0a 7d 0a 20 20 20  temp2, 0);.}.   
2ea2b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ea2c 20 20 63 61 73 65 20 31 38 39 3a 20 2f 2a 20 65    case 189: /* e
2ea2d 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e  xpr ::= nm DOT n
2ea2e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20  m DOT nm */.{.  
2ea2f 45 78 70 72 20 2a 74 65 6d 70 31 20 3d 20 73 71  Expr *temp1 = sq
2ea30 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ea31 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
2ea32 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
2ea33 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74  .yy0);.  Expr *t
2ea34 65 6d 70 32 20 3d 20 73 71 6c 69 74 65 33 50 45  emp2 = sqlite3PE
2ea35 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
2ea36 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  D, 0, 0, &yymsp[
2ea37 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -2].minor.yy0);.
2ea38 20 20 45 78 70 72 20 2a 74 65 6d 70 33 20 3d 20    Expr *temp3 = 
2ea39 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2ea3a 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
2ea3b 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2ea3c 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a  r.yy0);.  Expr *
2ea3d 74 65 6d 70 34 20 3d 20 73 71 6c 69 74 65 33 50  temp4 = sqlite3P
2ea3e 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2ea3f 44 4f 54 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70  DOT, temp2, temp
2ea40 33 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  3, 0);.  yygotom
2ea41 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69  inor.yy72 = sqli
2ea42 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2ea43 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20   TK_DOT, temp1, 
2ea44 74 65 6d 70 34 2c 20 30 29 3b 0a 7d 0a 20 20 20  temp4, 0);.}.   
2ea45 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ea46 20 20 63 61 73 65 20 31 39 32 3a 20 2f 2a 20 65    case 192: /* e
2ea47 78 70 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52  xpr ::= REGISTER
2ea48 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2ea49 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 52  .yy72 = sqlite3R
2ea4a 65 67 69 73 74 65 72 45 78 70 72 28 70 50 61 72  egisterExpr(pPar
2ea4b 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  se, &yymsp[0].mi
2ea4c 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
2ea4d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ea4e 63 61 73 65 20 31 39 33 3a 20 2f 2a 20 65 78 70  case 193: /* exp
2ea4f 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 20 2a  r ::= VARIABLE *
2ea50 2f 0a 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 6f  /.{.  Token *pTo
2ea51 6b 65 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e  ken = &yymsp[0].
2ea52 6d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 45 78 70  minor.yy0;.  Exp
2ea53 72 20 2a 70 45 78 70 72 20 3d 20 79 79 67 6f 74  r *pExpr = yygot
2ea54 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71  ominor.yy72 = sq
2ea55 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ea56 65 2c 20 54 4b 5f 56 41 52 49 41 42 4c 45 2c 20  e, TK_VARIABLE, 
2ea57 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  0, 0, pToken);. 
2ea58 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69   sqlite3ExprAssi
2ea59 67 6e 56 61 72 4e 75 6d 62 65 72 28 70 50 61 72  gnVarNumber(pPar
2ea5a 73 65 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 20 20  se, pExpr);.}.  
2ea5b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ea5c 20 20 20 63 61 73 65 20 31 39 34 3a 20 2f 2a 20     case 194: /* 
2ea5d 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f  expr ::= expr CO
2ea5e 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b 0a 20  LLATE ids */.{. 
2ea5f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2ea60 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 53  2 = sqlite3ExprS
2ea61 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20 79  etColl(pParse, y
2ea62 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2ea63 79 37 32 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  y72, &yymsp[0].m
2ea64 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
2ea65 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ea66 20 20 63 61 73 65 20 31 39 35 3a 20 2f 2a 20 65    case 195: /* e
2ea67 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20  xpr ::= CAST LP 
2ea68 65 78 70 72 20 41 53 20 74 79 70 65 74 6f 6b 65  expr AS typetoke
2ea69 6e 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  n RP */.{.  yygo
2ea6a 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73  tominor.yy72 = s
2ea6b 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2ea6c 73 65 2c 20 54 4b 5f 43 41 53 54 2c 20 79 79 6d  se, TK_CAST, yym
2ea6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-3].minor.yy7
2ea6e 32 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d  2, 0, &yymsp[-1]
2ea6f 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73  .minor.yy0);.  s
2ea70 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
2ea71 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c  ygotominor.yy72,
2ea72 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72  &yymsp[-5].minor
2ea73 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
2ea74 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
2ea75 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ea76 20 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20 65    case 196: /* e
2ea77 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64 69  xpr ::= ID LP di
2ea78 73 74 69 6e 63 74 20 65 78 70 72 6c 69 73 74 20  stinct exprlist 
2ea79 52 50 20 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79  RP */.{.  if( yy
2ea7a 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2ea7b 31 34 38 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d  148 && yymsp[-1]
2ea7c 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2d 3e 6e 45  .minor.yy148->nE
2ea7d 78 70 72 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46  xpr>SQLITE_MAX_F
2ea7e 55 4e 43 54 49 4f 4e 5f 41 52 47 20 29 7b 0a 20  UNCTION_ARG ){. 
2ea7f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2ea80 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
2ea81 6d 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20 6f  many arguments o
2ea82 6e 20 66 75 6e 63 74 69 6f 6e 20 25 54 22 2c 20  n function %T", 
2ea83 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
2ea84 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67  .yy0);.  }.  yyg
2ea85 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20  otominor.yy72 = 
2ea86 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
2ea87 69 6f 6e 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ion(pParse, yyms
2ea88 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2ea89 38 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  8, &yymsp[-4].mi
2ea8a 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
2ea8b 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f  te3ExprSpan(yygo
2ea8c 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79  tominor.yy72,&yy
2ea8d 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2ea8e 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
2ea8f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79 79  r.yy0);.  if( yy
2ea90 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2ea91 31 39 34 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e  194 && yygotomin
2ea92 6f 72 2e 79 79 37 32 20 29 7b 0a 20 20 20 20 79  or.yy72 ){.    y
2ea93 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d  ygotominor.yy72-
2ea94 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44 69 73  >flags |= EP_Dis
2ea95 74 69 6e 63 74 3b 0a 20 20 7d 0a 7d 0a 20 20 20  tinct;.  }.}.   
2ea96 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ea97 20 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 65    case 197: /* e
2ea98 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54  xpr ::= ID LP ST
2ea99 41 52 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67  AR RP */.{.  yyg
2ea9a 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20  otominor.yy72 = 
2ea9b 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
2ea9c 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 26  ion(pParse, 0, &
2ea9d 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2ea9e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  yy0);.  sqlite3E
2ea9f 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2eaa0 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b  nor.yy72,&yymsp[
2eaa1 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
2eaa2 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2eaa3 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2eaa4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2eaa5 31 39 38 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d  198: /* term ::=
2eaa6 20 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 7b 0a 20   CTIME_KW */.{. 
2eaa7 20 2f 2a 20 54 68 65 20 43 55 52 52 45 4e 54 5f   /* The CURRENT_
2eaa8 54 49 4d 45 2c 20 43 55 52 52 45 4e 54 5f 44 41  TIME, CURRENT_DA
2eaa9 54 45 2c 20 61 6e 64 20 43 55 52 52 45 4e 54 5f  TE, and CURRENT_
2eaaa 54 49 4d 45 53 54 41 4d 50 20 76 61 6c 75 65 73  TIMESTAMP values
2eaab 20 61 72 65 0a 20 20 2a 2a 20 74 72 65 61 74 65   are.  ** treate
2eaac 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 73 20 74  d as functions t
2eaad 68 61 74 20 72 65 74 75 72 6e 20 63 6f 6e 73 74  hat return const
2eaae 61 6e 74 73 20 2a 2f 0a 20 20 79 79 67 6f 74 6f  ants */.  yygoto
2eaaf 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c  minor.yy72 = sql
2eab0 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
2eab1 28 70 50 61 72 73 65 2c 20 30 2c 26 79 79 6d 73  (pParse, 0,&yyms
2eab2 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2eab3 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e  .  if( yygotomin
2eab4 6f 72 2e 79 79 37 32 20 29 7b 0a 20 20 20 20 79  or.yy72 ){.    y
2eab5 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d  ygotominor.yy72-
2eab6 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4e 53 54 5f 46  >op = TK_CONST_F
2eab7 55 4e 43 3b 20 20 0a 20 20 20 20 79 79 67 6f 74  UNC;  .    yygot
2eab8 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61  ominor.yy72->spa
2eab9 6e 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  n = yymsp[0].min
2eaba 6f 72 2e 79 79 30 3b 0a 20 20 7d 0a 7d 0a 20 20  or.yy0;.  }.}.  
2eabb 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2eabc 20 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20     case 199: /* 
2eabd 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e  expr ::= expr AN
2eabe 44 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  D expr */.      
2eabf 63 61 73 65 20 32 30 30 3a 20 2f 2a 20 65 78 70  case 200: /* exp
2eac0 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78  r ::= expr OR ex
2eac1 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
2eac2 20 32 30 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   201: /* expr ::
2eac3 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c  = expr LT|GT|GE|
2eac4 4c 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  LE expr */.     
2eac5 20 63 61 73 65 20 32 30 32 3a 20 2f 2a 20 65 78   case 202: /* ex
2eac6 70 72 20 3a 3a 3d 20 65 78 70 72 20 45 51 7c 4e  pr ::= expr EQ|N
2eac7 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  E expr */.      
2eac8 63 61 73 65 20 32 30 33 3a 20 2f 2a 20 65 78 70  case 203: /* exp
2eac9 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e  r ::= expr BITAN
2eaca 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52  D|BITOR|LSHIFT|R
2eacb 53 48 49 46 54 20 65 78 70 72 20 2a 2f 0a 20 20  SHIFT expr */.  
2eacc 20 20 20 20 63 61 73 65 20 32 30 34 3a 20 2f 2a      case 204: /*
2eacd 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 50   expr ::= expr P
2eace 4c 55 53 7c 4d 49 4e 55 53 20 65 78 70 72 20 2a  LUS|MINUS expr *
2eacf 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 35  /.      case 205
2ead0 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
2ead1 70 72 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45  pr STAR|SLASH|RE
2ead2 4d 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  M expr */.      
2ead3 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 65 78 70  case 206: /* exp
2ead4 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41  r ::= expr CONCA
2ead5 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74  T expr */.{yygot
2ead6 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71  ominor.yy72 = sq
2ead7 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2ead8 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f  e,yymsp[-1].majo
2ead9 72 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  r,yymsp[-2].mino
2eada 72 2e 79 79 37 32 2c 79 79 6d 73 70 5b 30 5d 2e  r.yy72,yymsp[0].
2eadb 6d 69 6e 6f 72 2e 79 79 37 32 2c 30 29 3b 7d 0a  minor.yy72,0);}.
2eadc 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2eadd 20 20 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f       case 207: /
2eade 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b  * likeop ::= LIK
2eadf 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61  E_KW */.      ca
2eae0 73 65 20 32 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f  se 209: /* likeo
2eae1 70 20 3a 3a 3d 20 4d 41 54 43 48 20 2a 2f 0a 7b  p ::= MATCH */.{
2eae2 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 39  yygotominor.yy39
2eae3 32 2e 65 4f 70 65 72 61 74 6f 72 20 3d 20 79 79  2.eOperator = yy
2eae4 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2eae5 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
2eae6 33 39 32 2e 6e 6f 74 20 3d 20 30 3b 7d 0a 20 20  392.not = 0;}.  
2eae7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2eae8 20 20 20 63 61 73 65 20 32 30 38 3a 20 2f 2a 20     case 208: /* 
2eae9 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c  likeop ::= NOT L
2eaea 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  IKE_KW */.      
2eaeb 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 6c 69 6b  case 210: /* lik
2eaec 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43  eop ::= NOT MATC
2eaed 48 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  H */.{yygotomino
2eaee 72 2e 79 79 33 39 32 2e 65 4f 70 65 72 61 74 6f  r.yy392.eOperato
2eaef 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  r = yymsp[0].min
2eaf0 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69  or.yy0; yygotomi
2eaf1 6e 6f 72 2e 79 79 33 39 32 2e 6e 6f 74 20 3d 20  nor.yy392.not = 
2eaf2 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  1;}.        brea
2eaf3 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  k;.      case 21
2eaf4 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  3: /* expr ::= e
2eaf5 78 70 72 20 6c 69 6b 65 6f 70 20 65 78 70 72 20  xpr likeop expr 
2eaf6 65 73 63 61 70 65 20 2a 2f 0a 7b 0a 20 20 45 78  escape */.{.  Ex
2eaf7 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 20  prList *pList;. 
2eaf8 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
2eaf9 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2eafa 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d  Parse,0, yymsp[-
2eafb 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30  1].minor.yy72, 0
2eafc 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  );.  pList = sql
2eafd 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2eafe 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c  nd(pParse,pList,
2eaff 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
2eb00 2e 79 79 37 32 2c 20 30 29 3b 0a 20 20 69 66 28  .yy72, 0);.  if(
2eb01 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2eb02 79 79 37 32 20 29 7b 0a 20 20 20 20 70 4c 69 73  yy72 ){.    pLis
2eb03 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
2eb04 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2eb05 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30 5d  ,pList, yymsp[0]
2eb06 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b  .minor.yy72, 0);
2eb07 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .  }.  yygotomin
2eb08 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65  or.yy72 = sqlite
2eb09 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50  3ExprFunction(pP
2eb0a 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 26 79 79  arse, pList, &yy
2eb0b 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2eb0c 33 39 32 2e 65 4f 70 65 72 61 74 6f 72 29 3b 0a  392.eOperator);.
2eb0d 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e    if( yymsp[-2].
2eb0e 6d 69 6e 6f 72 2e 79 79 33 39 32 2e 6e 6f 74 20  minor.yy392.not 
2eb0f 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
2eb10 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
2eb11 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
2eb12 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
2eb13 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c  72, 0, 0);.  sql
2eb14 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67  ite3ExprSpan(yyg
2eb15 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 26  otominor.yy72, &
2eb16 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2eb17 79 79 37 32 2d 3e 73 70 61 6e 2c 20 26 79 79 6d  yy72->span, &yym
2eb18 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-1].minor.yy7
2eb19 32 2d 3e 73 70 61 6e 29 3b 0a 20 20 69 66 28 20  2->span);.  if( 
2eb1a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2eb1b 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
2eb1c 79 37 32 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  y72->flags |= EP
2eb1d 5f 49 6e 66 69 78 46 75 6e 63 3b 0a 7d 0a 20 20  _InfixFunc;.}.  
2eb1e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2eb1f 20 20 20 63 61 73 65 20 32 31 34 3a 20 2f 2a 20     case 214: /* 
2eb20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53  expr ::= expr IS
2eb21 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a  NULL|NOTNULL */.
2eb22 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2eb23 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45  yy72 = sqlite3PE
2eb24 78 70 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73  xpr(pParse, yyms
2eb25 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20 79 79 6d 73  p[0].major, yyms
2eb26 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  p[-1].minor.yy72
2eb27 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
2eb28 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2eb29 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d  ominor.yy72,&yym
2eb2a 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-1].minor.yy7
2eb2b 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30  2->span,&yymsp[0
2eb2c 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
2eb2d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2eb2e 20 20 20 20 20 63 61 73 65 20 32 31 35 3a 20 2f       case 215: /
2eb2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
2eb30 49 53 20 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79  IS NULL */.{.  y
2eb31 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2eb32 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2eb33 50 61 72 73 65 2c 20 54 4b 5f 49 53 4e 55 4c 4c  Parse, TK_ISNULL
2eb34 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
2eb35 72 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20  r.yy72, 0, 0);. 
2eb36 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
2eb37 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  (yygotominor.yy7
2eb38 32 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  2,&yymsp[-2].min
2eb39 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 2c 26 79  or.yy72->span,&y
2eb3a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2eb3b 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2eb3c 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2eb3d 32 31 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  216: /* expr ::=
2eb3e 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c 4c 20 2a   expr NOT NULL *
2eb3f 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2eb40 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2eb41 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2eb42 5f 4e 4f 54 4e 55 4c 4c 2c 20 79 79 6d 73 70 5b  _NOTNULL, yymsp[
2eb43 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20  -2].minor.yy72, 
2eb44 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0);.  sqlite3
2eb45 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
2eb46 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70  inor.yy72,&yymsp
2eb47 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d  [-2].minor.yy72-
2eb48 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d 2e  >span,&yymsp[0].
2eb49 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
2eb4a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2eb4b 20 20 20 63 61 73 65 20 32 31 37 3a 20 2f 2a 20     case 217: /* 
2eb4c 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53  expr ::= expr IS
2eb4d 20 4e 4f 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20   NOT NULL */.{. 
2eb4e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2eb4f 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2eb50 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 4e  (pParse, TK_NOTN
2eb51 55 4c 4c 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  ULL, yymsp[-3].m
2eb52 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20 30 29  inor.yy72, 0, 0)
2eb53 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
2eb54 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
2eb55 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  yy72,&yymsp[-3].
2eb56 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e  minor.yy72->span
2eb57 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2eb58 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
2eb59 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2eb5a 73 65 20 32 31 38 3a 20 2f 2a 20 65 78 70 72 20  se 218: /* expr 
2eb5b 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 20 2a 2f 0a  ::= NOT expr */.
2eb5c 20 20 20 20 20 20 63 61 73 65 20 32 31 39 3a 20        case 219: 
2eb5d 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e  /* expr ::= BITN
2eb5e 4f 54 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79  OT expr */.{.  y
2eb5f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2eb60 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2eb61 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d  Parse, yymsp[-1]
2eb62 2e 6d 61 6a 6f 72 2c 20 79 79 6d 73 70 5b 30 5d  .major, yymsp[0]
2eb63 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20  .minor.yy72, 0, 
2eb64 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
2eb65 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2eb66 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 31  r.yy72,&yymsp[-1
2eb67 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2eb68 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  sp[0].minor.yy72
2eb69 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20  ->span);.}.     
2eb6a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2eb6b 63 61 73 65 20 32 32 30 3a 20 2f 2a 20 65 78 70  case 220: /* exp
2eb6c 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78 70 72  r ::= MINUS expr
2eb6d 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
2eb6e 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74  nor.yy72 = sqlit
2eb6f 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2eb70 54 4b 5f 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70  TK_UMINUS, yymsp
2eb71 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20  [0].minor.yy72, 
2eb72 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0);.  sqlite3
2eb73 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d  ExprSpan(yygotom
2eb74 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73 70  inor.yy72,&yymsp
2eb75 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-1].minor.yy0,&
2eb76 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2eb77 79 37 32 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20  y72->span);.}.  
2eb78 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2eb79 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f 2a 20     case 221: /* 
2eb7a 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78  expr ::= PLUS ex
2eb7b 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
2eb7c 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c  minor.yy72 = sql
2eb7d 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2eb7e 2c 20 54 4b 5f 55 50 4c 55 53 2c 20 79 79 6d 73  , TK_UPLUS, yyms
2eb7f 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c  p[0].minor.yy72,
2eb80 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
2eb81 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
2eb82 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73  minor.yy72,&yyms
2eb83 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
2eb84 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2eb85 79 79 37 32 2d 3e 73 70 61 6e 29 3b 0a 7d 0a 20  yy72->span);.}. 
2eb86 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2eb87 20 20 20 20 63 61 73 65 20 32 32 34 3a 20 2f 2a      case 224: /*
2eb88 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62   expr ::= expr b
2eb89 65 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41  etween_op expr A
2eb8a 4e 44 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 45  ND expr */.{.  E
2eb8b 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
2eb8c 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2eb8d 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c  Append(pParse,0,
2eb8e 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2eb8f 2e 79 79 37 32 2c 20 30 29 3b 0a 20 20 70 4c 69  .yy72, 0);.  pLi
2eb90 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2eb91 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2eb92 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30  e,pList, yymsp[0
2eb93 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29  ].minor.yy72, 0)
2eb94 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
2eb95 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45  yy72 = sqlite3PE
2eb96 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 42  xpr(pParse, TK_B
2eb97 45 54 57 45 45 4e 2c 20 79 79 6d 73 70 5b 2d 34  ETWEEN, yymsp[-4
2eb98 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 2c  ].minor.yy72, 0,
2eb99 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74   0);.  if( yygot
2eb9a 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 7b 0a 20  ominor.yy72 ){. 
2eb9b 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
2eb9c 79 37 32 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70  y72->x.pList = p
2eb9d 4c 69 73 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  List;.  }else{. 
2eb9e 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2eb9f 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
2eba0 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 7d  >db, pList);.  }
2eba1 20 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33   .  if( yymsp[-3
2eba2 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 20 29 20  ].minor.yy194 ) 
2eba3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2eba4 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2eba5 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20  pParse, TK_NOT, 
2eba6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2eba7 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
2eba8 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2eba9 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d  ominor.yy72,&yym
2ebaa 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-4].minor.yy7
2ebab 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30  2->span,&yymsp[0
2ebac 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70  ].minor.yy72->sp
2ebad 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  an);.}.        b
2ebae 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ebaf 20 32 32 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   227: /* expr ::
2ebb0 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20  = expr in_op LP 
2ebb1 65 78 70 72 6c 69 73 74 20 52 50 20 2a 2f 0a 7b  exprlist RP */.{
2ebb2 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
2ebb3 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33 50  .yy72 = sqlite3P
2ebb4 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2ebb5 49 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  IN, yymsp[-4].mi
2ebb6 6e 6f 72 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b  nor.yy72, 0, 0);
2ebb7 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  .    if( yygotom
2ebb8 69 6e 6f 72 2e 79 79 37 32 20 29 7b 0a 20 20 20  inor.yy72 ){.   
2ebb9 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
2ebba 79 37 32 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79  y72->x.pList = y
2ebbb 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ebbc 79 31 34 38 3b 0a 20 20 20 20 20 20 73 71 6c 69  y148;.      sqli
2ebbd 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
2ebbe 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
2ebbf 69 6e 6f 72 2e 79 79 37 32 29 3b 0a 20 20 20 20  inor.yy72);.    
2ebc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
2ebc1 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2ebc2 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
2ebc3 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ebc4 79 31 34 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20  y148);.    }.   
2ebc5 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d   if( yymsp[-3].m
2ebc6 69 6e 6f 72 2e 79 79 31 39 34 20 29 20 79 79 67  inor.yy194 ) yyg
2ebc7 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20  otominor.yy72 = 
2ebc8 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2ebc9 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
2ebca 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30  otominor.yy72, 0
2ebcb 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
2ebcc 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
2ebcd 6d 69 6e 6f 72 2e 79 79 37 32 2c 26 79 79 6d 73  minor.yy72,&yyms
2ebce 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32  p[-4].minor.yy72
2ebcf 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d  ->span,&yymsp[0]
2ebd0 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d  .minor.yy0);.  }
2ebd1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ebd2 20 20 20 20 20 20 63 61 73 65 20 32 32 38 3a 20        case 228: 
2ebd3 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73  /* expr ::= LP s
2ebd4 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  elect RP */.{.  
2ebd5 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2ebd6 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
2ebd7 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 53 45 4c  r(pParse, TK_SEL
2ebd8 45 43 54 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ECT, 0, 0, 0);. 
2ebd9 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e     if( yygotomin
2ebda 6f 72 2e 79 79 37 32 20 29 7b 0a 20 20 20 20 20  or.yy72 ){.     
2ebdb 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2ebdc 32 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 79  2->x.pSelect = y
2ebdd 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ebde 79 32 34 33 3b 0a 20 20 20 20 20 20 45 78 70 72  y243;.      Expr
2ebdf 53 65 74 50 72 6f 70 65 72 74 79 28 79 79 67 6f  SetProperty(yygo
2ebe0 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 45 50  tominor.yy72, EP
2ebe1 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20 20  _xIsSelect);.   
2ebe2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65     sqlite3ExprSe
2ebe3 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
2ebe4 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2ebe5 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2ebe6 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2ebe7 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
2ebe8 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  db, yymsp[-1].mi
2ebe9 6e 6f 72 2e 79 79 32 34 33 29 3b 0a 20 20 20 20  nor.yy243);.    
2ebea 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  }.    sqlite3Exp
2ebeb 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2ebec 72 2e 79 79 37 32 2c 26 79 79 6d 73 70 5b 2d 32  r.yy72,&yymsp[-2
2ebed 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2ebee 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2ebef 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  ;.  }.        br
2ebf0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ebf1 32 32 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  229: /* expr ::=
2ebf2 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73   expr in_op LP s
2ebf3 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  elect RP */.{.  
2ebf4 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2ebf5 37 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  72 = sqlite3PExp
2ebf6 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c  r(pParse, TK_IN,
2ebf7 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
2ebf8 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20 20  .yy72, 0, 0);.  
2ebf9 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2ebfa 72 2e 79 79 37 32 20 29 7b 0a 20 20 20 20 20 20  r.yy72 ){.      
2ebfb 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2ebfc 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 79 79  ->x.pSelect = yy
2ebfd 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2ebfe 32 34 33 3b 0a 20 20 20 20 20 20 45 78 70 72 53  243;.      ExprS
2ebff 65 74 50 72 6f 70 65 72 74 79 28 79 79 67 6f 74  etProperty(yygot
2ec00 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 45 50 5f  ominor.yy72, EP_
2ec01 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  xIsSelect);.    
2ec02 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
2ec03 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79  Height(pParse, y
2ec04 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 29  ygotominor.yy72)
2ec05 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2ec06 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2ec07 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
2ec08 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  b, yymsp[-1].min
2ec09 6f 72 2e 79 79 32 34 33 29 3b 0a 20 20 20 20 7d  or.yy243);.    }
2ec0a 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  .    if( yymsp[-
2ec0b 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 20 29  3].minor.yy194 )
2ec0c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2ec0d 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
2ec0e 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c  (pParse, TK_NOT,
2ec0f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37   yygotominor.yy7
2ec10 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  2, 0, 0);.    sq
2ec11 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79  lite3ExprSpan(yy
2ec12 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2c 26  gotominor.yy72,&
2ec13 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2ec14 79 79 37 32 2d 3e 73 70 61 6e 2c 26 79 79 6d 73  yy72->span,&yyms
2ec15 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2ec16 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .  }.        bre
2ec17 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2ec18 33 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  30: /* expr ::= 
2ec19 65 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62  expr in_op nm db
2ec1a 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 53 72 63 4c  nm */.{.    SrcL
2ec1b 69 73 74 20 2a 70 53 72 63 20 3d 20 73 71 6c 69  ist *pSrc = sqli
2ec1c 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
2ec1d 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 26  (pParse->db, 0,&
2ec1e 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2ec1f 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
2ec20 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 79 79  nor.yy0);.    yy
2ec21 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 3d  gotominor.yy72 =
2ec22 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2ec23 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d  arse, TK_IN, yym
2ec24 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-3].minor.yy7
2ec25 32 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  2, 0, 0);.    if
2ec26 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2ec27 37 32 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f  72 ){.      yygo
2ec28 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e 78 2e  tominor.yy72->x.
2ec29 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
2ec2a 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73  3SelectNew(pPars
2ec2b 65 2c 20 30 2c 70 53 72 63 2c 30 2c 30 2c 30 2c  e, 0,pSrc,0,0,0,
2ec2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  0,0,0,0);.      
2ec2d 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28  ExprSetProperty(
2ec2e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32  yygotominor.yy72
2ec2f 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b  , EP_xIsSelect);
2ec30 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2ec31 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
2ec32 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
2ec33 79 79 37 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65  yy72);.    }else
2ec34 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
2ec35 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  rcListDelete(pPa
2ec36 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 29 3b 0a  rse->db, pSrc);.
2ec37 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79 79      }.    if( yy
2ec38 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2ec39 31 39 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  194 ) yygotomino
2ec3a 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2ec3b 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2ec3c 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  _NOT, yygotomino
2ec3d 72 2e 79 79 37 32 2c 20 30 2c 20 30 29 3b 0a 20  r.yy72, 0, 0);. 
2ec3e 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70     sqlite3ExprSp
2ec3f 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2ec40 79 37 32 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  y72,&yymsp[-3].m
2ec41 69 6e 6f 72 2e 79 79 37 32 2d 3e 73 70 61 6e 2c  inor.yy72->span,
2ec42 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ec43 79 30 2e 7a 3f 26 79 79 6d 73 70 5b 30 5d 2e 6d  y0.z?&yymsp[0].m
2ec44 69 6e 6f 72 2e 79 79 30 3a 26 79 79 6d 73 70 5b  inor.yy0:&yymsp[
2ec45 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -1].minor.yy0);.
2ec46 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
2ec47 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  k;.      case 23
2ec48 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 45  1: /* expr ::= E
2ec49 58 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74 20  XISTS LP select 
2ec4a 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 45 78 70 72  RP */.{.    Expr
2ec4b 20 2a 70 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f   *p = yygotomino
2ec4c 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2ec4d 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2ec4e 5f 45 58 49 53 54 53 2c 20 30 2c 20 30 2c 20 30  _EXISTS, 0, 0, 0
2ec4f 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  );.    if( p ){.
2ec50 20 20 20 20 20 20 70 2d 3e 78 2e 70 53 65 6c 65        p->x.pSele
2ec51 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  ct = yymsp[-1].m
2ec52 69 6e 6f 72 2e 79 79 32 34 33 3b 0a 20 20 20 20  inor.yy243;.    
2ec53 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
2ec54 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  y(yygotominor.yy
2ec55 37 32 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  72, EP_xIsSelect
2ec56 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2ec57 45 78 70 72 53 70 61 6e 28 70 2c 26 79 79 6d 73  ExprSpan(p,&yyms
2ec58 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
2ec59 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2ec5a 79 79 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  yy0);.      sqli
2ec5b 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
2ec5c 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
2ec5d 69 6e 6f 72 2e 79 79 37 32 29 3b 0a 20 20 20 20  inor.yy72);.    
2ec5e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
2ec5f 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2ec60 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
2ec61 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-1].minor.yy2
2ec62 34 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  43);.    }.  }. 
2ec63 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ec64 20 20 20 20 63 61 73 65 20 32 33 32 3a 20 2f 2a      case 232: /*
2ec65 20 65 78 70 72 20 3a 3a 3d 20 43 41 53 45 20 63   expr ::= CASE c
2ec66 61 73 65 5f 6f 70 65 72 61 6e 64 20 63 61 73 65  ase_operand case
2ec67 5f 65 78 70 72 6c 69 73 74 20 63 61 73 65 5f 65  _exprlist case_e
2ec68 6c 73 65 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20 79  lse END */.{.  y
2ec69 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20  ygotominor.yy72 
2ec6a 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2ec6b 50 61 72 73 65 2c 20 54 4b 5f 43 41 53 45 2c 20  Parse, TK_CASE, 
2ec6c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2ec6d 79 79 37 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  yy72, yymsp[-1].
2ec6e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a  minor.yy72, 0);.
2ec6f 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2ec70 72 2e 79 79 37 32 20 29 7b 0a 20 20 20 20 79 79  r.yy72 ){.    yy
2ec71 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 2d 3e  gotominor.yy72->
2ec72 78 2e 70 4c 69 73 74 20 3d 20 79 79 6d 73 70 5b  x.pList = yymsp[
2ec73 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 3b  -2].minor.yy148;
2ec74 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2ec75 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65  SetHeight(pParse
2ec76 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
2ec77 37 32 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  72);.  }else{.  
2ec78 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2ec79 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
2ec7a 64 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  db, yymsp[-2].mi
2ec7b 6e 6f 72 2e 79 79 31 34 38 29 3b 0a 20 20 7d 0a  nor.yy148);.  }.
2ec7c 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2ec7d 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2ec7e 37 32 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  72, &yymsp[-4].m
2ec7f 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
2ec80 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2ec81 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ec82 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 33 3a  .      case 233:
2ec83 20 2f 2a 20 63 61 73 65 5f 65 78 70 72 6c 69 73   /* case_exprlis
2ec84 74 20 3a 3a 3d 20 63 61 73 65 5f 65 78 70 72 6c  t ::= case_exprl
2ec85 69 73 74 20 57 48 45 4e 20 65 78 70 72 20 54 48  ist WHEN expr TH
2ec86 45 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79  EN expr */.{.  y
2ec87 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38  ygotominor.yy148
2ec88 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2ec89 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2ec8a 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2ec8b 79 79 31 34 38 2c 20 79 79 6d 73 70 5b 2d 32 5d  yy148, yymsp[-2]
2ec8c 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b  .minor.yy72, 0);
2ec8d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2ec8e 79 31 34 38 20 3d 20 73 71 6c 69 74 65 33 45 78  y148 = sqlite3Ex
2ec8f 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2ec90 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  rse,yygotominor.
2ec91 79 79 31 34 38 2c 20 79 79 6d 73 70 5b 30 5d 2e  yy148, yymsp[0].
2ec92 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a  minor.yy72, 0);.
2ec93 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ec94 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 34 3a  .      case 234:
2ec95 20 2f 2a 20 63 61 73 65 5f 65 78 70 72 6c 69 73   /* case_exprlis
2ec96 74 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 20  t ::= WHEN expr 
2ec97 54 48 45 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20  THEN expr */.{. 
2ec98 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2ec99 34 38 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  48 = sqlite3Expr
2ec9a 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2ec9b 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  e,0, yymsp[-2].m
2ec9c 69 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a 20  inor.yy72, 0);. 
2ec9d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2ec9e 34 38 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  48 = sqlite3Expr
2ec9f 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2eca0 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  e,yygotominor.yy
2eca1 31 34 38 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  148, yymsp[0].mi
2eca2 6e 6f 72 2e 79 79 37 32 2c 20 30 29 3b 0a 7d 0a  nor.yy72, 0);.}.
2eca3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2eca4 20 20 20 20 20 63 61 73 65 20 32 34 33 3a 20 2f       case 243: /
2eca5 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65  * cmd ::= create
2eca6 6b 77 20 75 6e 69 71 75 65 66 6c 61 67 20 49 4e  kw uniqueflag IN
2eca7 44 45 58 20 69 66 6e 6f 74 65 78 69 73 74 73 20  DEX ifnotexists 
2eca8 6e 6d 20 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50  nm dbnm ON nm LP
2eca9 20 69 64 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b   idxlist RP */.{
2ecaa 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  .  sqlite3Create
2ecab 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 26 79  Index(pParse, &y
2ecac 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
2ecad 79 30 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d  y0, &yymsp[-5].m
2ecae 69 6e 6f 72 2e 79 79 30 2c 20 0a 20 20 20 20 20  inor.yy0, .     
2ecaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ecb0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
2ecb1 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  pend(pParse->db,
2ecb2 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
2ecb3 6f 72 2e 79 79 30 2c 30 29 2c 20 79 79 6d 73 70  or.yy0,0), yymsp
2ecb4 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 38  [-1].minor.yy148
2ecb5 2c 20 79 79 6d 73 70 5b 2d 39 5d 2e 6d 69 6e 6f  , yymsp[-9].mino
2ecb6 72 2e 79 79 31 39 34 2c 0a 20 20 20 20 20 20 20  r.yy194,.       
2ecb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
2ecb8 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72  yymsp[-10].minor
2ecb9 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  .yy0, &yymsp[0].
2ecba 6d 69 6e 6f 72 2e 79 79 30 2c 20 53 51 4c 49 54  minor.yy0, SQLIT
2ecbb 45 5f 53 4f 5f 41 53 43 2c 20 79 79 6d 73 70 5b  E_SO_ASC, yymsp[
2ecbc 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29  -7].minor.yy194)
2ecbd 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2ecbe 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  k;.      case 24
2ecbf 34 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c 61 67  4: /* uniqueflag
2ecc0 20 3a 3a 3d 20 55 4e 49 51 55 45 20 2a 2f 0a 20   ::= UNIQUE */. 
2ecc1 20 20 20 20 20 63 61 73 65 20 32 39 33 3a 20 2f       case 293: /
2ecc2 2a 20 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20  * raisetype ::= 
2ecc3 41 42 4f 52 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f  ABORT */.{yygoto
2ecc4 6d 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 4f 45  minor.yy194 = OE
2ecc5 5f 41 62 6f 72 74 3b 7d 0a 20 20 20 20 20 20 20  _Abort;}.       
2ecc6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2ecc7 73 65 20 32 34 35 3a 20 2f 2a 20 75 6e 69 71 75  se 245: /* uniqu
2ecc8 65 66 6c 61 67 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  eflag ::= */.{yy
2ecc9 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34 20  gotominor.yy194 
2ecca 3d 20 4f 45 5f 4e 6f 6e 65 3b 7d 0a 20 20 20 20  = OE_None;}.    
2eccb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2eccc 20 63 61 73 65 20 32 34 38 3a 20 2f 2a 20 69 64   case 248: /* id
2eccd 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73  xlist ::= idxlis
2ecce 74 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61  t COMMA nm colla
2eccf 74 65 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a  te sortorder */.
2ecd0 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 30 3b  {.  Expr *p = 0;
2ecd1 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d  .  if( yymsp[-1]
2ecd2 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29  .minor.yy0.n>0 )
2ecd3 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  {.    p = sqlite
2ecd4 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2ecd5 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20  K_COLUMN, 0, 0, 
2ecd6 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  0);.    sqlite3E
2ecd7 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72 73  xprSetColl(pPars
2ecd8 65 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 31 5d  e, p, &yymsp[-1]
2ecd9 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d  .minor.yy0);.  }
2ecda 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2ecdb 79 31 34 38 20 3d 20 73 71 6c 69 74 65 33 45 78  y148 = sqlite3Ex
2ecdc 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2ecdd 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  rse,yymsp[-4].mi
2ecde 6e 6f 72 2e 79 79 31 34 38 2c 20 70 2c 20 26 79  nor.yy148, p, &y
2ecdf 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2ece0 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  y0);.  sqlite3Ex
2ece1 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74  prListCheckLengt
2ece2 68 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  h(pParse, yygoto
2ece3 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20 22 69 6e  minor.yy148, "in
2ece4 64 65 78 22 29 3b 0a 20 20 69 66 28 20 79 79 67  dex");.  if( yyg
2ece5 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 20 29  otominor.yy148 )
2ece6 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
2ece7 34 38 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f  48->a[yygotomino
2ece8 72 2e 79 79 31 34 38 2d 3e 6e 45 78 70 72 2d 31  r.yy148->nExpr-1
2ece9 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  ].sortOrder = (u
2ecea 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8)yymsp[0].minor
2eceb 2e 79 79 31 39 34 3b 0a 7d 0a 20 20 20 20 20 20  .yy194;.}.      
2ecec 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2eced 61 73 65 20 32 34 39 3a 20 2f 2a 20 69 64 78 6c  ase 249: /* idxl
2ecee 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61  ist ::= nm colla
2ecef 74 65 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a  te sortorder */.
2ecf0 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 30 3b  {.  Expr *p = 0;
2ecf1 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d  .  if( yymsp[-1]
2ecf2 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29  .minor.yy0.n>0 )
2ecf3 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  {.    p = sqlite
2ecf4 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2ecf5 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20  K_COLUMN, 0, 0, 
2ecf6 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  0);.    sqlite3E
2ecf7 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72 73  xprSetColl(pPars
2ecf8 65 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d 31 5d  e, p, &yymsp[-1]
2ecf9 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d  .minor.yy0);.  }
2ecfa 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2ecfb 79 31 34 38 20 3d 20 73 71 6c 69 74 65 33 45 78  y148 = sqlite3Ex
2ecfc 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2ecfd 72 73 65 2c 30 2c 20 70 2c 20 26 79 79 6d 73 70  rse,0, p, &yymsp
2ecfe 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-2].minor.yy0);
2ecff 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
2ed00 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50  stCheckLength(pP
2ed01 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
2ed02 72 2e 79 79 31 34 38 2c 20 22 69 6e 64 65 78 22  r.yy148, "index"
2ed03 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  );.  if( yygotom
2ed04 69 6e 6f 72 2e 79 79 31 34 38 20 29 20 79 79 67  inor.yy148 ) yyg
2ed05 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 38 2d 3e  otominor.yy148->
2ed06 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  a[yygotominor.yy
2ed07 31 34 38 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f  148->nExpr-1].so
2ed08 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79  rtOrder = (u8)yy
2ed09 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2ed0a 39 34 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  94;.}.        br
2ed0b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ed0c 32 35 30 3a 20 2f 2a 20 63 6f 6c 6c 61 74 65 20  250: /* collate 
2ed0d 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2ed0e 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 30 3b 20 79  nor.yy0.z = 0; y
2ed0f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e  ygotominor.yy0.n
2ed10 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
2ed11 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ed12 20 32 35 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   252: /* cmd ::=
2ed13 20 44 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78   DROP INDEX ifex
2ed14 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f  ists fullname */
2ed15 0a 7b 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64  .{sqlite3DropInd
2ed16 65 78 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ex(pParse, yymsp
2ed17 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c  [0].minor.yy185,
2ed18 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2ed19 2e 79 79 31 39 34 29 3b 7d 0a 20 20 20 20 20 20  .yy194);}.      
2ed1a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2ed1b 61 73 65 20 32 35 33 3a 20 2f 2a 20 63 6d 64 20  ase 253: /* cmd 
2ed1c 3a 3a 3d 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20  ::= VACUUM */.  
2ed1d 20 20 20 20 63 61 73 65 20 32 35 34 3a 20 2f 2a      case 254: /*
2ed1e 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20   cmd ::= VACUUM 
2ed1f 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 61  nm */.{sqlite3Va
2ed20 63 75 75 6d 28 70 50 61 72 73 65 29 3b 7d 0a 20  cuum(pParse);}. 
2ed21 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ed22 20 20 20 20 63 61 73 65 20 32 35 35 3a 20 2f 2a      case 255: /*
2ed23 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20   cmd ::= PRAGMA 
2ed24 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69  nm dbnm */.{sqli
2ed25 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65  te3Pragma(pParse
2ed26 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
2ed27 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
2ed28 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 7d  minor.yy0,0,0);}
2ed29 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ed2a 20 20 20 20 20 20 63 61 73 65 20 32 35 36 3a 20        case 256: 
2ed2b 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  /* cmd ::= PRAGM
2ed2c 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e  A nm dbnm EQ nmn
2ed2d 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72  um */.{sqlite3Pr
2ed2e 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d  agma(pParse,&yym
2ed2f 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2ed30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
2ed31 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
2ed32 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20  minor.yy0,0);}. 
2ed33 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ed34 20 20 20 20 63 61 73 65 20 32 35 37 3a 20 2f 2a      case 257: /*
2ed35 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20   cmd ::= PRAGMA 
2ed36 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d  nm dbnm LP nmnum
2ed37 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50   RP */.{sqlite3P
2ed38 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79  ragma(pParse,&yy
2ed39 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2ed3a 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
2ed3b 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31  or.yy0,&yymsp[-1
2ed3c 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d  ].minor.yy0,0);}
2ed3d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ed3e 20 20 20 20 20 20 63 61 73 65 20 32 35 38 3a 20        case 258: 
2ed3f 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  /* cmd ::= PRAGM
2ed40 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e  A nm dbnm EQ min
2ed41 75 73 5f 6e 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74  us_num */.{sqlit
2ed42 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c  e3Pragma(pParse,
2ed43 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
2ed44 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  .yy0,&yymsp[-2].
2ed45 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2ed46 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29  [0].minor.yy0,1)
2ed47 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2ed48 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 39  ;.      case 259
2ed49 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41  : /* cmd ::= PRA
2ed4a 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6d  GMA nm dbnm LP m
2ed4b 69 6e 75 73 5f 6e 75 6d 20 52 50 20 2a 2f 0a 7b  inus_num RP */.{
2ed4c 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50  sqlite3Pragma(pP
2ed4d 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e  arse,&yymsp[-4].
2ed4e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2ed4f 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-3].minor.yy0,&
2ed50 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2ed51 79 79 30 2c 31 29 3b 7d 0a 20 20 20 20 20 20 20  yy0,1);}.       
2ed52 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2ed53 73 65 20 32 37 30 3a 20 2f 2a 20 63 6d 64 20 3a  se 270: /* cmd :
2ed54 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 72 69 67  := createkw trig
2ed55 67 65 72 5f 64 65 63 6c 20 42 45 47 49 4e 20 74  ger_decl BEGIN t
2ed56 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
2ed57 45 4e 44 20 2a 2f 0a 7b 0a 20 20 54 6f 6b 65 6e  END */.{.  Token
2ed58 20 61 6c 6c 3b 0a 20 20 61 6c 6c 2e 7a 20 3d 20   all;.  all.z = 
2ed59 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2ed5a 79 79 30 2e 7a 3b 0a 20 20 61 6c 6c 2e 6e 20 3d  yy0.z;.  all.n =
2ed5b 20 28 69 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e   (int)(yymsp[0].
2ed5c 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 2d 20 79 79  minor.yy0.z - yy
2ed5d 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2ed5e 30 2e 7a 29 20 2b 20 79 79 6d 73 70 5b 30 5d 2e  0.z) + yymsp[0].
2ed5f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3b 0a 20 20 73  minor.yy0.n;.  s
2ed60 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
2ed61 67 65 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ger(pParse, yyms
2ed62 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-1].minor.yy14
2ed63 35 2c 20 26 61 6c 6c 29 3b 0a 7d 0a 20 20 20 20  5, &all);.}.    
2ed64 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ed65 20 63 61 73 65 20 32 37 31 3a 20 2f 2a 20 74 72   case 271: /* tr
2ed66 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74  igger_decl ::= t
2ed67 65 6d 70 20 54 52 49 47 47 45 52 20 69 66 6e 6f  emp TRIGGER ifno
2ed68 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
2ed69 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74 72 69  trigger_time tri
2ed6a 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75  gger_event ON fu
2ed6b 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68 5f 63  llname foreach_c
2ed6c 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61 75 73  lause when_claus
2ed6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  e */.{.  sqlite3
2ed6e 42 65 67 69 6e 54 72 69 67 67 65 72 28 70 50 61  BeginTrigger(pPa
2ed6f 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e  rse, &yymsp[-7].
2ed70 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
2ed71 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-6].minor.yy0,
2ed72 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72   yymsp[-5].minor
2ed73 2e 79 79 31 39 34 2c 20 79 79 6d 73 70 5b 2d 34  .yy194, yymsp[-4
2ed74 5d 2e 6d 69 6e 6f 72 2e 79 79 33 33 32 2e 61 2c  ].minor.yy332.a,
2ed75 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
2ed76 2e 79 79 33 33 32 2e 62 2c 20 79 79 6d 73 70 5b  .yy332.b, yymsp[
2ed77 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c  -2].minor.yy185,
2ed78 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2ed79 79 79 37 32 2c 20 79 79 6d 73 70 5b 2d 31 30 5d  yy72, yymsp[-10]
2ed7a 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 2c 20 79 79  .minor.yy194, yy
2ed7b 6d 73 70 5b 2d 38 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-8].minor.yy
2ed7c 31 39 34 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  194);.  yygotomi
2ed7d 6e 6f 72 2e 79 79 30 20 3d 20 28 79 79 6d 73 70  nor.yy0 = (yymsp
2ed7e 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  [-6].minor.yy0.n
2ed7f 3d 3d 30 3f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69  ==0?yymsp[-7].mi
2ed80 6e 6f 72 2e 79 79 30 3a 79 79 6d 73 70 5b 2d 36  nor.yy0:yymsp[-6
2ed81 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
2ed82 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2ed83 20 20 20 20 20 63 61 73 65 20 32 37 32 3a 20 2f       case 272: /
2ed84 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a  * trigger_time :
2ed85 3a 3d 20 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20  := BEFORE */.   
2ed86 20 20 20 63 61 73 65 20 32 37 35 3a 20 2f 2a 20     case 275: /* 
2ed87 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
2ed88 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2ed89 72 2e 79 79 31 39 34 20 3d 20 54 4b 5f 42 45 46  r.yy194 = TK_BEF
2ed8a 4f 52 45 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  ORE; }.        b
2ed8b 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ed8c 20 32 37 33 3a 20 2f 2a 20 74 72 69 67 67 65 72   273: /* trigger
2ed8d 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 20  _time ::= AFTER 
2ed8e 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
2ed8f 2e 79 79 31 39 34 20 3d 20 54 4b 5f 41 46 54 45  .yy194 = TK_AFTE
2ed90 52 3b 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  R;  }.        br
2ed91 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ed92 32 37 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  274: /* trigger_
2ed93 74 69 6d 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44  time ::= INSTEAD
2ed94 20 4f 46 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d   OF */.{ yygotom
2ed95 69 6e 6f 72 2e 79 79 31 39 34 20 3d 20 54 4b 5f  inor.yy194 = TK_
2ed96 49 4e 53 54 45 41 44 3b 7d 0a 20 20 20 20 20 20  INSTEAD;}.      
2ed97 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2ed98 61 73 65 20 32 37 36 3a 20 2f 2a 20 74 72 69 67  ase 276: /* trig
2ed99 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45  ger_event ::= DE
2ed9a 4c 45 54 45 7c 49 4e 53 45 52 54 20 2a 2f 0a 20  LETE|INSERT */. 
2ed9b 20 20 20 20 20 63 61 73 65 20 32 37 37 3a 20 2f       case 277: /
2ed9c 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  * trigger_event 
2ed9d 3a 3a 3d 20 55 50 44 41 54 45 20 2a 2f 0a 7b 79  ::= UPDATE */.{y
2ed9e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 33 32  ygotominor.yy332
2ed9f 2e 61 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 61  .a = yymsp[0].ma
2eda0 6a 6f 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  jor; yygotominor
2eda1 2e 79 79 33 33 32 2e 62 20 3d 20 30 3b 7d 0a 20  .yy332.b = 0;}. 
2eda2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2eda3 20 20 20 20 63 61 73 65 20 32 37 38 3a 20 2f 2a      case 278: /*
2eda4 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a   trigger_event :
2eda5 3a 3d 20 55 50 44 41 54 45 20 4f 46 20 69 6e 73  := UPDATE OF ins
2eda6 63 6f 6c 6c 69 73 74 20 2a 2f 0a 7b 79 79 67 6f  collist */.{yygo
2eda7 74 6f 6d 69 6e 6f 72 2e 79 79 33 33 32 2e 61 20  tominor.yy332.a 
2eda8 3d 20 54 4b 5f 55 50 44 41 54 45 3b 20 79 79 67  = TK_UPDATE; yyg
2eda9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 33 32 2e 62  otominor.yy332.b
2edaa 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2edab 72 2e 79 79 32 35 34 3b 7d 0a 20 20 20 20 20 20  r.yy254;}.      
2edac 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2edad 61 73 65 20 32 38 31 3a 20 2f 2a 20 77 68 65 6e  ase 281: /* when
2edae 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a 20  _clause ::= */. 
2edaf 20 20 20 20 20 63 61 73 65 20 32 39 38 3a 20 2f       case 298: /
2edb0 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  * key_opt ::= */
2edb1 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
2edb2 79 37 32 20 3d 20 30 3b 20 7d 0a 20 20 20 20 20  y72 = 0; }.     
2edb3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2edb4 63 61 73 65 20 32 38 32 3a 20 2f 2a 20 77 68 65  case 282: /* whe
2edb5 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45  n_clause ::= WHE
2edb6 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  N expr */.      
2edb7 63 61 73 65 20 32 39 39 3a 20 2f 2a 20 6b 65 79  case 299: /* key
2edb8 5f 6f 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70  _opt ::= KEY exp
2edb9 72 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  r */.{ yygotomin
2edba 6f 72 2e 79 79 37 32 20 3d 20 79 79 6d 73 70 5b  or.yy72 = yymsp[
2edbb 30 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 3b 20 7d  0].minor.yy72; }
2edbc 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2edbd 20 20 20 20 20 20 63 61 73 65 20 32 38 33 3a 20        case 283: 
2edbe 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  /* trigger_cmd_l
2edbf 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f  ist ::= trigger_
2edc0 63 6d 64 5f 6c 69 73 74 20 74 72 69 67 67 65 72  cmd_list trigger
2edc1 5f 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b 0a 2f  _cmd SEMI */.{./
2edc2 2a 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32  *.  if( yymsp[-2
2edc3 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 20 29 7b  ].minor.yy145 ){
2edc4 0a 20 20 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  .    yymsp[-2].m
2edc5 69 6e 6f 72 2e 79 79 31 34 35 2d 3e 70 4c 61 73  inor.yy145->pLas
2edc6 74 2d 3e 70 4e 65 78 74 20 3d 20 79 79 6d 73 70  t->pNext = yymsp
2edc7 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35  [-1].minor.yy145
2edc8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 79  ;.  }else{.    y
2edc9 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2edca 79 31 34 35 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  y145 = yymsp[-1]
2edcb 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 3b 0a 20 20  .minor.yy145;.  
2edcc 7d 0a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 79  }.*/.  assert( y
2edcd 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2edce 79 31 34 35 21 3d 30 20 29 3b 0a 20 20 79 79 6d  y145!=0 );.  yym
2edcf 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
2edd0 34 35 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74  45->pLast->pNext
2edd1 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
2edd2 6f 72 2e 79 79 31 34 35 3b 0a 20 20 79 79 6d 73  or.yy145;.  yyms
2edd3 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-2].minor.yy14
2edd4 35 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73 70  5->pLast = yymsp
2edd5 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35  [-1].minor.yy145
2edd6 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
2edd7 79 79 31 34 35 20 3d 20 79 79 6d 73 70 5b 2d 32  yy145 = yymsp[-2
2edd8 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 3b 0a 7d  ].minor.yy145;.}
2edd9 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2edda 20 20 20 20 20 20 63 61 73 65 20 32 38 34 3a 20        case 284: 
2eddb 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  /* trigger_cmd_l
2eddc 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f  ist ::= trigger_
2eddd 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b 20 0a 20  cmd SEMI */.{ . 
2edde 20 2f 2a 20 69 66 28 20 79 79 6d 73 70 5b 2d 31   /* if( yymsp[-1
2eddf 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 20 29 20  ].minor.yy145 ) 
2ede0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 79 79 6d  */.  assert( yym
2ede1 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2ede2 34 35 21 3d 30 20 29 3b 0a 20 20 79 79 6d 73 70  45!=0 );.  yymsp
2ede3 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35  [-1].minor.yy145
2ede4 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b  ->pLast = yymsp[
2ede5 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 3b  -1].minor.yy145;
2ede6 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2ede7 79 31 34 35 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  y145 = yymsp[-1]
2ede8 2e 6d 69 6e 6f 72 2e 79 79 31 34 35 3b 0a 7d 0a  .minor.yy145;.}.
2ede9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2edea 20 20 20 20 20 63 61 73 65 20 32 38 35 3a 20 2f       case 285: /
2edeb 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a  * trigger_cmd ::
2edec 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
2eded 6e 6d 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  nm SET setlist w
2edee 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 20 79 79  here_opt */.{ yy
2edef 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 35 20  gotominor.yy145 
2edf0 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
2edf1 55 70 64 61 74 65 53 74 65 70 28 70 50 61 72 73  UpdateStep(pPars
2edf2 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 33  e->db, &yymsp[-3
2edf3 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d  ].minor.yy0, yym
2edf4 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
2edf5 34 38 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  48, yymsp[0].min
2edf6 6f 72 2e 79 79 37 32 2c 20 79 79 6d 73 70 5b 2d  or.yy72, yymsp[-
2edf7 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b  4].minor.yy194);
2edf8 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2edf9 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 36  ;.      case 286
2edfa 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
2edfb 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
2edfc 49 4e 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69  INTO nm inscolli
2edfd 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50  st_opt VALUES LP
2edfe 20 69 74 65 6d 6c 69 73 74 20 52 50 20 2a 2f 0a   itemlist RP */.
2edff 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
2ee00 34 35 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  45 = sqlite3Trig
2ee01 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 70 50  gerInsertStep(pP
2ee02 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70  arse->db, &yymsp
2ee03 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-5].minor.yy0, 
2ee04 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2ee05 79 79 32 35 34 2c 20 79 79 6d 73 70 5b 2d 31 5d  yy254, yymsp[-1]
2ee06 2e 6d 69 6e 6f 72 2e 79 79 31 34 38 2c 20 30 2c  .minor.yy148, 0,
2ee07 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72   yymsp[-7].minor
2ee08 2e 79 79 31 39 34 29 3b 7d 0a 20 20 20 20 20 20  .yy194);}.      
2ee09 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2ee0a 61 73 65 20 32 38 37 3a 20 2f 2a 20 74 72 69 67  ase 287: /* trig
2ee0b 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  ger_cmd ::= inse
2ee0c 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69  rt_cmd INTO nm i
2ee0d 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65  nscollist_opt se
2ee0e 6c 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  lect */.{yygotom
2ee0f 69 6e 6f 72 2e 79 79 31 34 35 20 3d 20 73 71 6c  inor.yy145 = sql
2ee10 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72  ite3TriggerInser
2ee11 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62  tStep(pParse->db
2ee12 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
2ee13 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 31  or.yy0, yymsp[-1
2ee14 5d 2e 6d 69 6e 6f 72 2e 79 79 32 35 34 2c 20 30  ].minor.yy254, 0
2ee15 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2ee16 2e 79 79 32 34 33 2c 20 79 79 6d 73 70 5b 2d 34  .yy243, yymsp[-4
2ee17 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 29 3b 7d  ].minor.yy194);}
2ee18 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ee19 20 20 20 20 20 20 63 61 73 65 20 32 38 38 3a 20        case 288: 
2ee1a 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  /* trigger_cmd :
2ee1b 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 6e  := DELETE FROM n
2ee1c 6d 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b  m where_opt */.{
2ee1d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
2ee1e 35 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67  5 = sqlite3Trigg
2ee1f 65 72 44 65 6c 65 74 65 53 74 65 70 28 70 50 61  erDeleteStep(pPa
2ee20 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b  rse->db, &yymsp[
2ee21 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79  -1].minor.yy0, y
2ee22 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2ee23 37 32 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  72);}.        br
2ee24 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ee25 32 38 39 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  289: /* trigger_
2ee26 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 20 2a  cmd ::= select *
2ee27 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2ee28 79 31 34 35 20 3d 20 73 71 6c 69 74 65 33 54 72  y145 = sqlite3Tr
2ee29 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28  iggerSelectStep(
2ee2a 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
2ee2b 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 33  p[0].minor.yy243
2ee2c 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
2ee2d 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2ee2e 39 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  90: /* expr ::= 
2ee2f 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52 45 20  RAISE LP IGNORE 
2ee30 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  RP */.{.  yygoto
2ee31 6d 69 6e 6f 72 2e 79 79 37 32 20 3d 20 73 71 6c  minor.yy72 = sql
2ee32 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2ee33 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20 30  , TK_RAISE, 0, 0
2ee34 2c 20 30 29 3b 20 0a 20 20 69 66 28 20 79 79 67  , 0); .  if( yyg
2ee35 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 7b  otominor.yy72 ){
2ee36 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
2ee37 2e 79 79 37 32 2d 3e 61 66 66 69 6e 69 74 79 20  .yy72->affinity 
2ee38 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 0a 20 20 20  = OE_Ignore;.   
2ee39 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e   sqlite3ExprSpan
2ee3a 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37  (yygotominor.yy7
2ee3b 32 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  2, &yymsp[-3].mi
2ee3c 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
2ee3d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
2ee3e 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65   }.}.        bre
2ee3f 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2ee40 39 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  91: /* expr ::= 
2ee41 52 41 49 53 45 20 4c 50 20 72 61 69 73 65 74 79  RAISE LP raisety
2ee42 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 20 2a  pe COMMA nm RP *
2ee43 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2ee44 72 2e 79 79 37 32 20 3d 20 73 71 6c 69 74 65 33  r.yy72 = sqlite3
2ee45 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2ee46 5f 52 41 49 53 45 2c 20 30 2c 20 30 2c 20 26 79  _RAISE, 0, 0, &y
2ee47 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ee48 79 30 29 3b 20 0a 20 20 69 66 28 20 79 79 67 6f  y0); .  if( yygo
2ee49 74 6f 6d 69 6e 6f 72 2e 79 79 37 32 20 29 20 7b  tominor.yy72 ) {
2ee4a 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
2ee4b 2e 79 79 37 32 2d 3e 61 66 66 69 6e 69 74 79 20  .yy72->affinity 
2ee4c 3d 20 28 63 68 61 72 29 79 79 6d 73 70 5b 2d 33  = (char)yymsp[-3
2ee4d 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39 34 3b 0a 20  ].minor.yy194;. 
2ee4e 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70     sqlite3ExprSp
2ee4f 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2ee50 79 37 32 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e  y72, &yymsp[-5].
2ee51 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
2ee52 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2ee53 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62  .  }.}.        b
2ee54 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ee55 20 32 39 32 3a 20 2f 2a 20 72 61 69 73 65 74 79   292: /* raisety
2ee56 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20  pe ::= ROLLBACK 
2ee57 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2ee58 79 79 31 39 34 20 3d 20 4f 45 5f 52 6f 6c 6c 62  yy194 = OE_Rollb
2ee59 61 63 6b 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ack;}.        br
2ee5a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ee5b 32 39 34 3a 20 2f 2a 20 72 61 69 73 65 74 79 70  294: /* raisetyp
2ee5c 65 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a 7b 79  e ::= FAIL */.{y
2ee5d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 39 34  ygotominor.yy194
2ee5e 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20 20 20   = OE_Fail;}.   
2ee5f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ee60 20 20 63 61 73 65 20 32 39 35 3a 20 2f 2a 20 63    case 295: /* c
2ee61 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47  md ::= DROP TRIG
2ee62 47 45 52 20 69 66 65 78 69 73 74 73 20 66 75 6c  GER ifexists ful
2ee63 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c  lname */.{.  sql
2ee64 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 28  ite3DropTrigger(
2ee65 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e  pParse,yymsp[0].
2ee66 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 79 79 6d 73  minor.yy185,yyms
2ee67 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 39  p[-1].minor.yy19
2ee68 34 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  4);.}.        br
2ee69 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ee6a 32 39 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  296: /* cmd ::= 
2ee6b 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65 5f  ATTACH database_
2ee6c 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65  kw_opt expr AS e
2ee6d 78 70 72 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b  xpr key_opt */.{
2ee6e 0a 20 20 73 71 6c 69 74 65 33 41 74 74 61 63 68  .  sqlite3Attach
2ee6f 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
2ee70 33 5d 2e 6d 69 6e 6f 72 2e 79 79 37 32 2c 20 79  3].minor.yy72, y
2ee71 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2ee72 79 37 32 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  y72, yymsp[0].mi
2ee73 6e 6f 72 2e 79 79 37 32 29 3b 0a 7d 0a 20 20 20  nor.yy72);.}.   
2ee74 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ee75 20 20 63 61 73 65 20 32 39 37 3a 20 2f 2a 20 63    case 297: /* c
2ee76 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61  md ::= DETACH da
2ee77 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78  tabase_kw_opt ex
2ee78 70 72 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  pr */.{.  sqlite
2ee79 33 44 65 74 61 63 68 28 70 50 61 72 73 65 2c 20  3Detach(pParse, 
2ee7a 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ee7b 79 37 32 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y72);.}.        
2ee7c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2ee7d 65 20 33 30 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 302: /* cmd ::
2ee7e 3d 20 52 45 49 4e 44 45 58 20 2a 2f 0a 7b 73 71  = REINDEX */.{sq
2ee7f 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70 50 61  lite3Reindex(pPa
2ee80 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a 20 20 20  rse, 0, 0);}.   
2ee81 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ee82 20 20 63 61 73 65 20 33 30 33 3a 20 2f 2a 20 63    case 303: /* c
2ee83 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 6e  md ::= REINDEX n
2ee84 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74  m dbnm */.{sqlit
2ee85 65 33 52 65 69 6e 64 65 78 28 70 50 61 72 73 65  e3Reindex(pParse
2ee86 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  , &yymsp[-1].min
2ee87 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30  or.yy0, &yymsp[0
2ee88 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
2ee89 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2ee8a 20 20 20 20 63 61 73 65 20 33 30 34 3a 20 2f 2a      case 304: /*
2ee8b 20 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45   cmd ::= ANALYZE
2ee8c 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 6e 61 6c   */.{sqlite3Anal
2ee8d 79 7a 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30  yze(pParse, 0, 0
2ee8e 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2ee8f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
2ee90 35 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e  5: /* cmd ::= AN
2ee91 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d 20 2a 2f  ALYZE nm dbnm */
2ee92 0a 7b 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65  .{sqlite3Analyze
2ee93 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
2ee94 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  -1].minor.yy0, &
2ee95 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2ee96 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
2ee97 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2ee98 33 30 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  306: /* cmd ::= 
2ee99 41 4c 54 45 52 20 54 41 42 4c 45 20 66 75 6c 6c  ALTER TABLE full
2ee9a 6e 61 6d 65 20 52 45 4e 41 4d 45 20 54 4f 20 6e  name RENAME TO n
2ee9b 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  m */.{.  sqlite3
2ee9c 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65  AlterRenameTable
2ee9d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33  (pParse,yymsp[-3
2ee9e 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 35 2c 26 79  ].minor.yy185,&y
2ee9f 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2eea0 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2eea1 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2eea2 33 30 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  307: /* cmd ::= 
2eea3 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64 64 5f  ALTER TABLE add_
2eea4 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20  column_fullname 
2eea5 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  ADD kwcolumn_opt
2eea6 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 7b 0a 20 20 73   column */.{.  s
2eea7 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73  qlite3AlterFinis
2eea8 68 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73  hAddColumn(pPars
2eea9 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e, &yymsp[0].min
2eeaa 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
2eeab 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2eeac 63 61 73 65 20 33 30 38 3a 20 2f 2a 20 61 64 64  case 308: /* add
2eead 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65  _column_fullname
2eeae 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f   ::= fullname */
2eeaf 0a 7b 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 2d  .{.  pParse->db-
2eeb0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
2eeb1 6c 65 64 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  led = 0;.  sqlit
2eeb2 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43  e3AlterBeginAddC
2eeb3 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 79 79  olumn(pParse, yy
2eeb4 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
2eeb5 38 35 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  85);.}.        b
2eeb6 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2eeb7 20 33 31 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   311: /* cmd ::=
2eeb8 20 63 72 65 61 74 65 5f 76 74 61 62 20 2a 2f 0a   create_vtab */.
2eeb9 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69  {sqlite3VtabFini
2eeba 73 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 30  shParse(pParse,0
2eebb 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2eebc 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  k;.      case 31
2eebd 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72  2: /* cmd ::= cr
2eebe 65 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74 61  eate_vtab LP vta
2eebf 62 61 72 67 6c 69 73 74 20 52 50 20 2a 2f 0a 7b  barglist RP */.{
2eec0 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73  sqlite3VtabFinis
2eec1 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 26 79  hParse(pParse,&y
2eec2 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2eec3 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2eec4 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
2eec5 31 33 3a 20 2f 2a 20 63 72 65 61 74 65 5f 76 74  13: /* create_vt
2eec6 61 62 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20  ab ::= createkw 
2eec7 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 6e 6d  VIRTUAL TABLE nm
2eec8 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e 6d 20 2a   dbnm USING nm *
2eec9 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  /.{.    sqlite3V
2eeca 74 61 62 42 65 67 69 6e 50 61 72 73 65 28 70 50  tabBeginParse(pP
2eecb 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  arse, &yymsp[-3]
2eecc 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
2eecd 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
2eece 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2eecf 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
2eed0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2eed1 61 73 65 20 33 31 36 3a 20 2f 2a 20 76 74 61 62  ase 316: /* vtab
2eed2 61 72 67 20 3a 3a 3d 20 2a 2f 0a 7b 73 71 6c 69  arg ::= */.{sqli
2eed3 74 65 33 56 74 61 62 41 72 67 49 6e 69 74 28 70  te3VtabArgInit(p
2eed4 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20  Parse);}.       
2eed5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2eed6 73 65 20 33 31 38 3a 20 2f 2a 20 76 74 61 62 61  se 318: /* vtaba
2eed7 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 20  rgtoken ::= ANY 
2eed8 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  */.      case 31
2eed9 39 3a 20 2f 2a 20 76 74 61 62 61 72 67 74 6f 6b  9: /* vtabargtok
2eeda 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73  en ::= lp anylis
2eedb 74 20 52 50 20 2a 2f 0a 20 20 20 20 20 20 63 61  t RP */.      ca
2eedc 73 65 20 33 32 30 3a 20 2f 2a 20 6c 70 20 3a 3a  se 320: /* lp ::
2eedd 3d 20 4c 50 20 2a 2f 0a 20 20 20 20 20 20 63 61  = LP */.      ca
2eede 73 65 20 33 32 32 3a 20 2f 2a 20 61 6e 79 6c 69  se 322: /* anyli
2eedf 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41  st ::= anylist A
2eee0 4e 59 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74  NY */.{sqlite3Vt
2eee1 61 62 41 72 67 45 78 74 65 6e 64 28 70 50 61 72  abArgExtend(pPar
2eee2 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se,&yymsp[0].min
2eee3 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2eee4 20 20 62 72 65 61 6b 3b 0a 20 20 7d 3b 0a 20 20    break;.  };.  
2eee5 79 79 67 6f 74 6f 20 3d 20 79 79 52 75 6c 65 49  yygoto = yyRuleI
2eee6 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6c 68  nfo[yyruleno].lh
2eee7 73 3b 0a 20 20 79 79 73 69 7a 65 20 3d 20 79 79  s;.  yysize = yy
2eee8 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e  RuleInfo[yyrulen
2eee9 6f 5d 2e 6e 72 68 73 3b 0a 20 20 79 79 70 50 61  o].nrhs;.  yypPa
2eeea 72 73 65 72 2d 3e 79 79 69 64 78 20 2d 3d 20 79  rser->yyidx -= y
2eeeb 79 73 69 7a 65 3b 0a 20 20 79 79 61 63 74 20 3d  ysize;.  yyact =
2eeec 20 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f   yy_find_reduce_
2eeed 61 63 74 69 6f 6e 28 79 79 6d 73 70 5b 2d 79 79  action(yymsp[-yy
2eeee 73 69 7a 65 5d 2e 73 74 61 74 65 6e 6f 2c 28 59  size].stateno,(Y
2eeef 59 43 4f 44 45 54 59 50 45 29 79 79 67 6f 74 6f  YCODETYPE)yygoto
2eef0 29 3b 0a 20 20 69 66 28 20 79 79 61 63 74 20 3c  );.  if( yyact <
2eef1 20 59 59 4e 53 54 41 54 45 20 29 7b 0a 23 69 66   YYNSTATE ){.#if
2eef2 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f  def NDEBUG.    /
2eef3 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20  * If we are not 
2eef4 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 68  debugging and th
2eef5 65 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20  e reduce action 
2eef6 70 6f 70 70 65 64 20 61 74 20 6c 65 61 73 74 0a  popped at least.
2eef7 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6c 65 6d 65      ** one eleme
2eef8 6e 74 20 6f 66 66 20 74 68 65 20 73 74 61 63 6b  nt off the stack
2eef9 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 70 75  , then we can pu
2eefa 73 68 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65  sh the new eleme
2eefb 6e 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20 6f  nt back.    ** o
2eefc 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 68 65  nto the stack he
2eefd 72 65 2c 20 61 6e 64 20 73 6b 69 70 20 74 68 65  re, and skip the
2eefe 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20   stack overflow 
2eeff 74 65 73 74 20 69 6e 20 79 79 5f 73 68 69 66 74  test in yy_shift
2ef00 28 29 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74 20  ()..    ** That 
2ef01 67 69 76 65 73 20 61 20 73 69 67 6e 69 66 69 63  gives a signific
2ef02 61 6e 74 20 73 70 65 65 64 20 69 6d 70 72 6f 76  ant speed improv
2ef03 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 66  ement. */.    if
2ef04 28 20 79 79 73 69 7a 65 20 29 7b 0a 20 20 20 20  ( yysize ){.    
2ef05 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69    yypParser->yyi
2ef06 64 78 2b 2b 3b 0a 20 20 20 20 20 20 79 79 6d 73  dx++;.      yyms
2ef07 70 20 2d 3d 20 79 79 73 69 7a 65 2d 31 3b 0a 20  p -= yysize-1;. 
2ef08 20 20 20 20 20 79 79 6d 73 70 2d 3e 73 74 61 74       yymsp->stat
2ef09 65 6e 6f 20 3d 20 28 59 59 41 43 54 49 4f 4e 54  eno = (YYACTIONT
2ef0a 59 50 45 29 79 79 61 63 74 3b 0a 20 20 20 20 20  YPE)yyact;.     
2ef0b 20 79 79 6d 73 70 2d 3e 6d 61 6a 6f 72 20 3d 20   yymsp->major = 
2ef0c 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 67 6f  (YYCODETYPE)yygo
2ef0d 74 6f 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d  to;.      yymsp-
2ef0e 3e 6d 69 6e 6f 72 20 3d 20 79 79 67 6f 74 6f 6d  >minor = yygotom
2ef0f 69 6e 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 0a  inor;.    }else.
2ef10 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20  #endif.    {.   
2ef11 20 20 20 79 79 5f 73 68 69 66 74 28 79 79 70 50     yy_shift(yypP
2ef12 61 72 73 65 72 2c 79 79 61 63 74 2c 79 79 67 6f  arser,yyact,yygo
2ef13 74 6f 2c 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 29  to,&yygotominor)
2ef14 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
2ef15 0a 20 20 20 20 61 73 73 65 72 74 28 20 79 79 61  .    assert( yya
2ef16 63 74 20 3d 3d 20 59 59 4e 53 54 41 54 45 20 2b  ct == YYNSTATE +
2ef17 20 59 59 4e 52 55 4c 45 20 2b 20 31 20 29 3b 0a   YYNRULE + 1 );.
2ef18 20 20 20 20 79 79 5f 61 63 63 65 70 74 28 79 79      yy_accept(yy
2ef19 70 50 61 72 73 65 72 29 3b 0a 20 20 7d 0a 7d 0a  pParser);.  }.}.
2ef1a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
2ef1b 77 69 6e 67 20 63 6f 64 65 20 65 78 65 63 75 74  wing code execut
2ef1c 65 73 20 77 68 65 6e 20 74 68 65 20 70 61 72 73  es when the pars
2ef1d 65 20 66 61 69 6c 73 0a 2a 2f 0a 73 74 61 74 69  e fails.*/.stati
2ef1e 63 20 76 6f 69 64 20 79 79 5f 70 61 72 73 65 5f  c void yy_parse_
2ef1f 66 61 69 6c 65 64 28 0a 20 20 79 79 50 61 72 73  failed(.  yyPars
2ef20 65 72 20 2a 79 79 70 50 61 72 73 65 72 20 20 20  er *yypParser   
2ef21 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2ef22 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71  arser */.){.  sq
2ef23 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46  lite3ParserARG_F
2ef24 45 54 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44  ETCH;.#ifndef ND
2ef25 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
2ef26 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70  ceFILE ){.    fp
2ef27 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2ef28 45 2c 22 25 73 46 61 69 6c 21 5c 6e 22 2c 79 79  E,"%sFail!\n",yy
2ef29 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20  TracePrompt);.  
2ef2a 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68 69 6c 65  }.#endif.  while
2ef2b 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
2ef2c 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70  dx>=0 ) yy_pop_p
2ef2d 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50  arser_stack(yypP
2ef2e 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65 72  arser);.  /* Her
2ef2f 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74  e code is insert
2ef30 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65  ed which will be
2ef31 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 65 76   executed whenev
2ef32 65 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 72 73  er the.  ** pars
2ef33 65 72 20 66 61 69 6c 73 20 2a 2f 0a 20 20 73 71  er fails */.  sq
2ef34 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
2ef35 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73  TORE; /* Suppres
2ef36 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20  s warning about 
2ef37 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61 72  unused %extra_ar
2ef38 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20  gument variable 
2ef39 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  */.}../*.** The 
2ef3a 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 65  following code e
2ef3b 78 65 63 75 74 65 73 20 77 68 65 6e 20 61 20 73  xecutes when a s
2ef3c 79 6e 74 61 78 20 65 72 72 6f 72 20 66 69 72 73  yntax error firs
2ef3d 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61  t occurs..*/.sta
2ef3e 74 69 63 20 76 6f 69 64 20 79 79 5f 73 79 6e 74  tic void yy_synt
2ef3f 61 78 5f 65 72 72 6f 72 28 0a 20 20 79 79 50 61  ax_error(.  yyPa
2ef40 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c  rser *yypParser,
2ef41 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ef42 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e  e parser */.  in
2ef43 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20  t yymajor,      
2ef44 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ef45 54 68 65 20 6d 61 6a 6f 72 20 74 79 70 65 20 6f  The major type o
2ef46 66 20 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65  f the error toke
2ef47 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59  n */.  YYMINORTY
2ef48 50 45 20 79 79 6d 69 6e 6f 72 20 20 20 20 20 20  PE yyminor      
2ef49 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 69 6e        /* The min
2ef4a 6f 72 20 74 79 70 65 20 6f 66 20 74 68 65 20 65  or type of the e
2ef4b 72 72 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b  rror token */.){
2ef4c 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
2ef4d 41 52 47 5f 46 45 54 43 48 3b 0a 23 64 65 66 69  ARG_FETCH;.#defi
2ef4e 6e 65 20 54 4f 4b 45 4e 20 28 79 79 6d 69 6e 6f  ne TOKEN (yymino
2ef4f 72 2e 79 79 30 29 0a 0a 20 20 55 4e 55 53 45 44  r.yy0)..  UNUSED
2ef50 5f 50 41 52 41 4d 45 54 45 52 28 79 79 6d 61 6a  _PARAMETER(yymaj
2ef51 6f 72 29 3b 20 20 2f 2a 20 53 69 6c 65 6e 63 65  or);  /* Silence
2ef52 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 20 77   some compiler w
2ef53 61 72 6e 69 6e 67 73 20 2a 2f 0a 20 20 61 73 73  arnings */.  ass
2ef54 65 72 74 28 20 54 4f 4b 45 4e 2e 7a 5b 30 5d 20  ert( TOKEN.z[0] 
2ef55 29 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e  );  /* The token
2ef56 69 7a 65 72 20 61 6c 77 61 79 73 20 67 69 76 65  izer always give
2ef57 73 20 75 73 20 61 20 74 6f 6b 65 6e 20 2a 2f 0a  s us a token */.
2ef58 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2ef59 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61 72 20  g(pParse, "near 
2ef5a 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78 20 65  \"%T\": syntax e
2ef5b 72 72 6f 72 22 2c 20 26 54 4f 4b 45 4e 29 3b 0a  rror", &TOKEN);.
2ef5c 20 20 70 50 61 72 73 65 2d 3e 70 61 72 73 65 45    pParse->parseE
2ef5d 72 72 6f 72 20 3d 20 31 3b 0a 20 20 73 71 6c 69  rror = 1;.  sqli
2ef5e 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
2ef5f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20  RE; /* Suppress 
2ef60 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e  warning about un
2ef61 75 73 65 64 20 25 65 78 74 72 61 5f 61 72 67 75  used %extra_argu
2ef62 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20 2a 2f  ment variable */
2ef63 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
2ef64 6c 6c 6f 77 69 6e 67 20 69 73 20 65 78 65 63 75  llowing is execu
2ef65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 72  ted when the par
2ef66 73 65 72 20 61 63 63 65 70 74 73 0a 2a 2f 0a 73  ser accepts.*/.s
2ef67 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63  tatic void yy_ac
2ef68 63 65 70 74 28 0a 20 20 79 79 50 61 72 73 65 72  cept(.  yyParser
2ef69 20 2a 79 79 70 50 61 72 73 65 72 20 20 20 20 20   *yypParser     
2ef6a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2ef6b 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ser */.){.  sqli
2ef6c 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
2ef6d 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  CH;.#ifndef NDEB
2ef6e 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  UG.  if( yyTrace
2ef6f 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69  FILE ){.    fpri
2ef70 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
2ef71 22 25 73 41 63 63 65 70 74 21 5c 6e 22 2c 79 79  "%sAccept!\n",yy
2ef72 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20  TracePrompt);.  
2ef73 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68 69 6c 65  }.#endif.  while
2ef74 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
2ef75 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70  dx>=0 ) yy_pop_p
2ef76 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50  arser_stack(yypP
2ef77 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65 72  arser);.  /* Her
2ef78 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74  e code is insert
2ef79 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65  ed which will be
2ef7a 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 65 76   executed whenev
2ef7b 65 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 72 73  er the.  ** pars
2ef7c 65 72 20 61 63 63 65 70 74 73 20 2a 2f 0a 20 20  er accepts */.  
2ef7d 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2ef7e 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72  _STORE; /* Suppr
2ef7f 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75  ess warning abou
2ef80 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f  t unused %extra_
2ef81 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c  argument variabl
2ef82 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54 68 65 20 6d  e */.}../* The m
2ef83 61 69 6e 20 70 61 72 73 65 72 20 70 72 6f 67 72  ain parser progr
2ef84 61 6d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  am..** The first
2ef85 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
2ef86 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75  ointer to a stru
2ef87 63 74 75 72 65 20 6f 62 74 61 69 6e 65 64 20 66  cture obtained f
2ef88 72 6f 6d 0a 2a 2a 20 22 73 71 6c 69 74 65 33 50  rom.** "sqlite3P
2ef89 61 72 73 65 72 41 6c 6c 6f 63 22 20 77 68 69 63  arserAlloc" whic
2ef8a 68 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  h describes the 
2ef8b 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
2ef8c 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20   the parser..** 
2ef8d 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
2ef8e 65 6e 74 20 69 73 20 74 68 65 20 6d 61 6a 6f 72  ent is the major
2ef8f 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 20 20   token number.  
2ef90 54 68 65 20 74 68 69 72 64 20 69 73 0a 2a 2a 20  The third is.** 
2ef91 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 2e  the minor token.
2ef92 20 20 54 68 65 20 66 6f 75 72 74 68 20 6f 70 74    The fourth opt
2ef93 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20 69  ional argument i
2ef94 73 20 77 68 61 74 65 76 65 72 20 74 68 65 0a 2a  s whatever the.*
2ef95 2a 20 75 73 65 72 20 77 61 6e 74 73 20 28 61 6e  * user wants (an
2ef96 64 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  d specified in t
2ef97 68 65 20 67 72 61 6d 6d 61 72 29 20 61 6e 64 20  he grammar) and 
2ef98 69 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  is available for
2ef99 0a 2a 2a 20 75 73 65 20 62 79 20 74 68 65 20 61  .** use by the a
2ef9a 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a  ction routines..
2ef9b 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a  **.** Inputs:.**
2ef9c 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20   <ul>.** <li> A 
2ef9d 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
2ef9e 61 72 73 65 72 20 28 61 6e 20 6f 70 61 71 75 65  arser (an opaque
2ef9f 20 73 74 72 75 63 74 75 72 65 2e 29 0a 2a 2a 20   structure.).** 
2efa0 3c 6c 69 3e 20 54 68 65 20 6d 61 6a 6f 72 20 74  <li> The major t
2efa1 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  oken number..** 
2efa2 3c 6c 69 3e 20 54 68 65 20 6d 69 6e 6f 72 20 74  <li> The minor t
2efa3 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  oken number..** 
2efa4 3c 6c 69 3e 20 41 6e 20 6f 70 74 69 6f 6e 20 61  <li> An option a
2efa5 72 67 75 6d 65 6e 74 20 6f 66 20 61 20 67 72 61  rgument of a gra
2efa6 6d 6d 61 72 2d 73 70 65 63 69 66 69 65 64 20 74  mmar-specified t
2efa7 79 70 65 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  ype..** </ul>.**
2efa8 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a 20  .** Outputs:.** 
2efa9 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  None..*/.SQLITE_
2efaa 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2efab 69 74 65 33 50 61 72 73 65 72 28 0a 20 20 76 6f  ite3Parser(.  vo
2efac 69 64 20 2a 79 79 70 2c 20 20 20 20 20 20 20 20  id *yyp,        
2efad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2efae 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e  e parser */.  in
2efaf 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20 20  t yymajor,      
2efb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2efb1 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 63 6f  e major token co
2efb2 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73  de number */.  s
2efb3 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
2efb4 4e 54 59 50 45 20 79 79 6d 69 6e 6f 72 20 20 20  NTYPE yyminor   
2efb5 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65      /* The value
2efb6 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 2a   for the token *
2efb7 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
2efb8 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20 20  rARG_PDECL      
2efb9 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69           /* Opti
2efba 6f 6e 61 6c 20 25 65 78 74 72 61 5f 61 72 67 75  onal %extra_argu
2efbb 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 2a  ment parameter *
2efbc 2f 0a 29 7b 0a 20 20 59 59 4d 49 4e 4f 52 54 59  /.){.  YYMINORTY
2efbd 50 45 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 3b  PE yyminorunion;
2efbe 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20 20  .  int yyact;   
2efbf 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2efc0 70 61 72 73 65 72 20 61 63 74 69 6f 6e 2e 20 2a  parser action. *
2efc1 2f 0a 20 20 69 6e 74 20 79 79 65 6e 64 6f 66 69  /.  int yyendofi
2efc2 6e 70 75 74 3b 20 20 20 20 20 2f 2a 20 54 72 75  nput;     /* Tru
2efc3 65 20 69 66 20 77 65 20 61 72 65 20 61 74 20 74  e if we are at t
2efc4 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20  he end of input 
2efc5 2a 2f 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f  */.#ifdef YYERRO
2efc6 52 53 59 4d 42 4f 4c 0a 20 20 69 6e 74 20 79 79  RSYMBOL.  int yy
2efc7 65 72 72 6f 72 68 69 74 20 3d 20 30 3b 20 20 20  errorhit = 0;   
2efc8 2f 2a 20 54 72 75 65 20 69 66 20 79 79 6d 61 6a  /* True if yymaj
2efc9 6f 72 20 68 61 73 20 69 6e 76 6f 6b 65 64 20 61  or has invoked a
2efca 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 65 6e 64 69  n error */.#endi
2efcb 66 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79  f.  yyParser *yy
2efcc 70 50 61 72 73 65 72 3b 20 20 2f 2a 20 54 68 65  pParser;  /* The
2efcd 20 70 61 72 73 65 72 20 2a 2f 0a 0a 20 20 2f 2a   parser */..  /*
2efce 20 28 72 65 29 69 6e 69 74 69 61 6c 69 7a 65 20   (re)initialize 
2efcf 74 68 65 20 70 61 72 73 65 72 2c 20 69 66 20 6e  the parser, if n
2efd0 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20 79 79  ecessary */.  yy
2efd1 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72  pParser = (yyPar
2efd2 73 65 72 2a 29 79 79 70 3b 0a 20 20 69 66 28 20  ser*)yyp;.  if( 
2efd3 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
2efd4 3c 30 20 29 7b 0a 23 69 66 20 59 59 53 54 41 43  <0 ){.#if YYSTAC
2efd5 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20 69 66  KDEPTH<=0.    if
2efd6 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73  ( yypParser->yys
2efd7 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a 20 20 20 20  tksz <=0 ){.    
2efd8 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79 6d 69    /*memset(&yymi
2efd9 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c 20 73 69 7a  norunion, 0, siz
2efda 65 6f 66 28 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  eof(yyminorunion
2efdb 29 29 3b 2a 2f 0a 20 20 20 20 20 20 79 79 6d 69  ));*/.      yymi
2efdc 6e 6f 72 75 6e 69 6f 6e 20 3d 20 79 79 7a 65 72  norunion = yyzer
2efdd 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 20 20 79 79  ominor;.      yy
2efde 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79  StackOverflow(yy
2efdf 70 50 61 72 73 65 72 2c 20 26 79 79 6d 69 6e 6f  pParser, &yymino
2efe0 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 72  runion);.      r
2efe1 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 23 65 6e  eturn;.    }.#en
2efe2 64 69 66 0a 20 20 20 20 79 79 70 50 61 72 73 65  dif.    yypParse
2efe3 72 2d 3e 79 79 69 64 78 20 3d 20 30 3b 0a 20 20  r->yyidx = 0;.  
2efe4 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65    yypParser->yye
2efe5 72 72 63 6e 74 20 3d 20 2d 31 3b 0a 20 20 20 20  rrcnt = -1;.    
2efe6 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
2efe7 63 6b 5b 30 5d 2e 73 74 61 74 65 6e 6f 20 3d 20  ck[0].stateno = 
2efe8 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72  0;.    yypParser
2efe9 2d 3e 79 79 73 74 61 63 6b 5b 30 5d 2e 6d 61 6a  ->yystack[0].maj
2efea 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 79 79  or = 0;.  }.  yy
2efeb 6d 69 6e 6f 72 75 6e 69 6f 6e 2e 79 79 30 20 3d  minorunion.yy0 =
2efec 20 79 79 6d 69 6e 6f 72 3b 0a 20 20 79 79 65 6e   yyminor;.  yyen
2efed 64 6f 66 69 6e 70 75 74 20 3d 20 28 79 79 6d 61  dofinput = (yyma
2efee 6a 6f 72 3d 3d 30 29 3b 0a 20 20 73 71 6c 69 74  jor==0);.  sqlit
2efef 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52  e3ParserARG_STOR
2eff0 45 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  E;..#ifndef NDEB
2eff1 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  UG.  if( yyTrace
2eff2 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72 69  FILE ){.    fpri
2eff3 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
2eff4 22 25 73 49 6e 70 75 74 20 25 73 5c 6e 22 2c 79  "%sInput %s\n",y
2eff5 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54  yTracePrompt,yyT
2eff6 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72  okenName[yymajor
2eff7 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ]);.  }.#endif..
2eff8 20 20 64 6f 7b 0a 20 20 20 20 79 79 61 63 74 20    do{.    yyact 
2eff9 3d 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f  = yy_find_shift_
2effa 61 63 74 69 6f 6e 28 79 79 70 50 61 72 73 65 72  action(yypParser
2effb 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d  ,(YYCODETYPE)yym
2effc 61 6a 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 79  ajor);.    if( y
2effd 79 61 63 74 3c 59 59 4e 53 54 41 54 45 20 29 7b  yact<YYNSTATE ){
2effe 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
2efff 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29 3b 20  yyendofinput ); 
2f000 20 2f 2a 20 49 6d 70 6f 73 73 69 62 6c 65 20 74   /* Impossible t
2f001 6f 20 73 68 69 66 74 20 74 68 65 20 24 20 74 6f  o shift the $ to
2f002 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 79 79 5f  ken */.      yy_
2f003 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72 2c  shift(yypParser,
2f004 79 79 61 63 74 2c 79 79 6d 61 6a 6f 72 2c 26 79  yyact,yymajor,&y
2f005 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
2f006 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
2f007 79 65 72 72 63 6e 74 2d 2d 3b 0a 20 20 20 20 20  yerrcnt--;.     
2f008 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
2f009 4f 44 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ODE;.    }else i
2f00a 66 28 20 79 79 61 63 74 20 3c 20 59 59 4e 53 54  f( yyact < YYNST
2f00b 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 29 7b  ATE + YYNRULE ){
2f00c 0a 20 20 20 20 20 20 79 79 5f 72 65 64 75 63 65  .      yy_reduce
2f00d 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63 74  (yypParser,yyact
2f00e 2d 59 59 4e 53 54 41 54 45 29 3b 0a 20 20 20 20  -YYNSTATE);.    
2f00f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
2f010 65 72 74 28 20 79 79 61 63 74 20 3d 3d 20 59 59  ert( yyact == YY
2f011 5f 45 52 52 4f 52 5f 41 43 54 49 4f 4e 20 29 3b  _ERROR_ACTION );
2f012 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53  .#ifdef YYERRORS
2f013 59 4d 42 4f 4c 0a 20 20 20 20 20 20 69 6e 74 20  YMBOL.      int 
2f014 79 79 6d 78 3b 0a 23 65 6e 64 69 66 0a 23 69 66  yymx;.#endif.#if
2f015 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
2f016 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
2f017 45 20 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72  E ){.        fpr
2f018 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
2f019 2c 22 25 73 53 79 6e 74 61 78 20 45 72 72 6f 72  ,"%sSyntax Error
2f01a 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  !\n",yyTraceProm
2f01b 70 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  pt);.      }.#en
2f01c 64 69 66 0a 23 69 66 64 65 66 20 59 59 45 52 52  dif.#ifdef YYERR
2f01d 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20 2f  ORSYMBOL.      /
2f01e 2a 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f 72  * A syntax error
2f01f 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 20   has occurred.. 
2f020 20 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 70       ** The resp
2f021 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f 72  onse to an error
2f022 20 64 65 70 65 6e 64 73 20 75 70 6f 6e 20 77 68   depends upon wh
2f023 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
2f024 0a 20 20 20 20 20 20 2a 2a 20 67 72 61 6d 6d 61  .      ** gramma
2f025 72 20 64 65 66 69 6e 65 73 20 61 6e 20 65 72 72  r defines an err
2f026 6f 72 20 74 6f 6b 65 6e 20 22 45 52 52 4f 52 22  or token "ERROR"
2f027 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  .  .      **.   
2f028 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 77 68     ** This is wh
2f029 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65 20  at we do if the 
2f02a 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 64 65 66  grammar does def
2f02b 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20 20  ine ERROR:.     
2f02c 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20   **.      **  * 
2f02d 43 61 6c 6c 20 74 68 65 20 25 73 79 6e 74 61 78  Call the %syntax
2f02e 5f 65 72 72 6f 72 20 66 75 6e 63 74 69 6f 6e 2e  _error function.
2f02f 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
2f030 2a 2a 20 20 2a 20 42 65 67 69 6e 20 70 6f 70 70  **  * Begin popp
2f031 69 6e 67 20 74 68 65 20 73 74 61 63 6b 20 75 6e  ing the stack un
2f032 74 69 6c 20 77 65 20 65 6e 74 65 72 20 61 20 73  til we enter a s
2f033 74 61 74 65 20 77 68 65 72 65 0a 20 20 20 20 20  tate where.     
2f034 20 2a 2a 20 20 20 20 69 74 20 69 73 20 6c 65 67   **    it is leg
2f035 61 6c 20 74 6f 20 73 68 69 66 74 20 74 68 65 20  al to shift the 
2f036 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2c 20 74 68  error symbol, th
2f037 65 6e 20 73 68 69 66 74 0a 20 20 20 20 20 20 2a  en shift.      *
2f038 2a 20 20 20 20 74 68 65 20 65 72 72 6f 72 20 73  *    the error s
2f039 79 6d 62 6f 6c 2e 0a 20 20 20 20 20 20 2a 2a 0a  ymbol..      **.
2f03a 20 20 20 20 20 20 2a 2a 20 20 2a 20 53 65 74 20        **  * Set 
2f03b 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20  the error count 
2f03c 74 6f 20 74 68 72 65 65 2e 0a 20 20 20 20 20 20  to three..      
2f03d 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42  **.      **  * B
2f03e 65 67 69 6e 20 61 63 63 65 70 74 69 6e 67 20 61  egin accepting a
2f03f 6e 64 20 73 68 69 66 74 69 6e 67 20 6e 65 77 20  nd shifting new 
2f040 74 6f 6b 65 6e 73 2e 20 20 4e 6f 20 6e 65 77 20  tokens.  No new 
2f041 65 72 72 6f 72 0a 20 20 20 20 20 20 2a 2a 20 20  error.      **  
2f042 20 20 70 72 6f 63 65 73 73 69 6e 67 20 77 69 6c    processing wil
2f043 6c 20 6f 63 63 75 72 20 75 6e 74 69 6c 20 74 68  l occur until th
2f044 72 65 65 20 74 6f 6b 65 6e 73 20 68 61 76 65 20  ree tokens have 
2f045 62 65 65 6e 0a 20 20 20 20 20 20 2a 2a 20 20 20  been.      **   
2f046 20 73 68 69 66 74 65 64 20 73 75 63 63 65 73 73   shifted success
2f047 66 75 6c 6c 79 2e 0a 20 20 20 20 20 20 2a 2a 0a  fully..      **.
2f048 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
2f049 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
2f04a 65 72 72 63 6e 74 3c 30 20 29 7b 0a 20 20 20 20  errcnt<0 ){.    
2f04b 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f 65 72      yy_syntax_er
2f04c 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79 79  ror(yypParser,yy
2f04d 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e 69  major,yyminoruni
2f04e 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  on);.      }.   
2f04f 20 20 20 79 79 6d 78 20 3d 20 79 79 70 50 61 72     yymx = yypPar
2f050 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70  ser->yystack[yyp
2f051 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 6d  Parser->yyidx].m
2f052 61 6a 6f 72 3b 0a 20 20 20 20 20 20 69 66 28 20  ajor;.      if( 
2f053 79 79 6d 78 3d 3d 59 59 45 52 52 4f 52 53 59 4d  yymx==YYERRORSYM
2f054 42 4f 4c 20 7c 7c 20 79 79 65 72 72 6f 72 68 69  BOL || yyerrorhi
2f055 74 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45  t ){.#ifndef NDE
2f056 42 55 47 0a 20 20 20 20 20 20 20 20 69 66 28 20  BUG.        if( 
2f057 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
2f058 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66           fprintf
2f059 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
2f05a 44 69 73 63 61 72 64 20 69 6e 70 75 74 20 74 6f  Discard input to
2f05b 6b 65 6e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20  ken %s\n",.     
2f05c 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65 50          yyTraceP
2f05d 72 6f 6d 70 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d  rompt,yyTokenNam
2f05e 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20  e[yymajor]);.   
2f05f 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
2f060 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75 63        yy_destruc
2f061 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 20 28  tor(yypParser, (
2f062 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
2f063 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
2f064 29 3b 0a 20 20 20 20 20 20 20 20 79 79 6d 61 6a  );.        yymaj
2f065 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20  or = YYNOCODE;. 
2f066 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2f067 20 20 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20       while(.    
2f068 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d        yypParser-
2f069 3e 79 79 69 64 78 20 3e 3d 20 30 20 26 26 0a 20  >yyidx >= 0 &&. 
2f06a 20 20 20 20 20 20 20 20 20 79 79 6d 78 20 21 3d           yymx !=
2f06b 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 26   YYERRORSYMBOL &
2f06c 26 0a 20 20 20 20 20 20 20 20 20 20 28 79 79 61  &.          (yya
2f06d 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65 64  ct = yy_find_red
2f06e 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20 20 20 20  uce_action(.    
2f06f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f070 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
2f071 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72  ystack[yypParser
2f072 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e 6f  ->yyidx].stateno
2f073 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2f074 20 20 20 20 20 20 20 20 20 20 59 59 45 52 52 4f            YYERRO
2f075 52 53 59 4d 42 4f 4c 29 29 20 3e 3d 20 59 59 4e  RSYMBOL)) >= YYN
2f076 53 54 41 54 45 0a 20 20 20 20 20 20 20 20 29 7b  STATE.        ){
2f077 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 70 6f  .          yy_po
2f078 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79  p_parser_stack(y
2f079 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20 20 20  ypParser);.     
2f07a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
2f07b 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
2f07c 78 20 3c 20 30 20 7c 7c 20 79 79 6d 61 6a 6f 72  x < 0 || yymajor
2f07d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2f07e 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79   yy_destructor(y
2f07f 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45  ypParser,(YYCODE
2f080 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79  TYPE)yymajor,&yy
2f081 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
2f082 20 20 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f         yy_parse_
2f083 66 61 69 6c 65 64 28 79 79 70 50 61 72 73 65 72  failed(yypParser
2f084 29 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d  );.          yym
2f085 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b  ajor = YYNOCODE;
2f086 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
2f087 66 28 20 79 79 6d 78 21 3d 59 59 45 52 52 4f 52  f( yymx!=YYERROR
2f088 53 59 4d 42 4f 4c 20 29 7b 0a 20 20 20 20 20 20  SYMBOL ){.      
2f089 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20      YYMINORTYPE 
2f08a 75 32 3b 0a 20 20 20 20 20 20 20 20 20 20 75 32  u2;.          u2
2f08b 2e 59 59 45 52 52 53 59 4d 44 54 20 3d 20 30 3b  .YYERRSYMDT = 0;
2f08c 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 73 68  .          yy_sh
2f08d 69 66 74 28 79 79 70 50 61 72 73 65 72 2c 79 79  ift(yypParser,yy
2f08e 61 63 74 2c 59 59 45 52 52 4f 52 53 59 4d 42 4f  act,YYERRORSYMBO
2f08f 4c 2c 26 75 32 29 3b 0a 20 20 20 20 20 20 20 20  L,&u2);.        
2f090 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
2f091 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72  yypParser->yyerr
2f092 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 20 20 79  cnt = 3;.      y
2f093 79 65 72 72 6f 72 68 69 74 20 3d 20 31 3b 0a 23  yerrorhit = 1;.#
2f094 65 6c 73 65 20 20 2f 2a 20 59 59 45 52 52 4f 52  else  /* YYERROR
2f095 53 59 4d 42 4f 4c 20 69 73 20 6e 6f 74 20 64 65  SYMBOL is not de
2f096 66 69 6e 65 64 20 2a 2f 0a 20 20 20 20 20 20 2f  fined */.      /
2f097 2a 20 54 68 69 73 20 69 73 20 77 68 61 74 20 77  * This is what w
2f098 65 20 64 6f 20 69 66 20 74 68 65 20 67 72 61 6d  e do if the gram
2f099 6d 61 72 20 64 6f 65 73 20 6e 6f 74 20 64 65 66  mar does not def
2f09a 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20 20  ine ERROR:.     
2f09b 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20   **.      **  * 
2f09c 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 20  Report an error 
2f09d 6d 65 73 73 61 67 65 2c 20 61 6e 64 20 74 68 72  message, and thr
2f09e 6f 77 20 61 77 61 79 20 74 68 65 20 69 6e 70 75  ow away the inpu
2f09f 74 20 74 6f 6b 65 6e 2e 0a 20 20 20 20 20 20 2a  t token..      *
2f0a0 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 49 66  *.      **  * If
2f0a1 20 74 68 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e   the input token
2f0a2 20 69 73 20 24 2c 20 74 68 65 6e 20 66 61 69 6c   is $, then fail
2f0a3 20 74 68 65 20 70 61 72 73 65 2e 0a 20 20 20 20   the parse..    
2f0a4 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73    **.      ** As
2f0a5 20 62 65 66 6f 72 65 2c 20 73 75 62 73 65 71 75   before, subsequ
2f0a6 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
2f0a7 65 73 20 61 72 65 20 73 75 70 70 72 65 73 73 65  es are suppresse
2f0a8 64 20 75 6e 74 69 6c 0a 20 20 20 20 20 20 2a 2a  d until.      **
2f0a9 20 74 68 72 65 65 20 69 6e 70 75 74 20 74 6f 6b   three input tok
2f0aa 65 6e 73 20 68 61 76 65 20 62 65 65 6e 20 73 75  ens have been su
2f0ab 63 63 65 73 73 66 75 6c 6c 79 20 73 68 69 66 74  ccessfully shift
2f0ac 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
2f0ad 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
2f0ae 2d 3e 79 79 65 72 72 63 6e 74 3c 3d 30 20 29 7b  ->yyerrcnt<=0 ){
2f0af 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74  .        yy_synt
2f0b0 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73  ax_error(yypPars
2f0b1 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e  er,yymajor,yymin
2f0b2 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20  orunion);.      
2f0b3 7d 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65  }.      yypParse
2f0b4 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 33 3b  r->yyerrcnt = 3;
2f0b5 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75  .      yy_destru
2f0b6 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28  ctor(yypParser,(
2f0b7 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
2f0b8 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
2f0b9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 79 79 65  );.      if( yye
2f0ba 6e 64 6f 66 69 6e 70 75 74 20 29 7b 0a 20 20 20  ndofinput ){.   
2f0bb 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f 66 61       yy_parse_fa
2f0bc 69 6c 65 64 28 79 79 70 50 61 72 73 65 72 29 3b  iled(yypParser);
2f0bd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79  .      }.      y
2f0be 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44  ymajor = YYNOCOD
2f0bf 45 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  E;.#endif.    }.
2f0c0 20 20 7d 77 68 69 6c 65 28 20 79 79 6d 61 6a 6f    }while( yymajo
2f0c1 72 21 3d 59 59 4e 4f 43 4f 44 45 20 26 26 20 79  r!=YYNOCODE && y
2f0c2 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
2f0c3 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  =0 );.  return;.
2f0c4 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
2f0c5 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e  ** End of parse.
2f0c6 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
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 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2f0ca 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 6f  ** Begin file to
2f0cb 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  kenize.c *******
2f0cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0ce 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
2f0cf 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
2f0d0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2f0d1 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2f0d2 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2f0d3 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2f0d4 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2f0d5 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2f0d6 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2f0d7 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2f0d8 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2f0d9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2f0da 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2f0db 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2f0dc 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2f0dd 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2f0de 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2f0df 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2f0e0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2f0e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f0e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20  *********.** An 
2f0e6 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53 51  tokenizer for SQ
2f0e7 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  L.**.** This fil
2f0e8 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
2f0e9 65 20 74 68 61 74 20 73 70 6c 69 74 73 20 61 6e  e that splits an
2f0ea 20 53 51 4c 20 69 6e 70 75 74 20 73 74 72 69 6e   SQL input strin
2f0eb 67 20 75 70 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64  g up into.** ind
2f0ec 69 76 69 64 75 61 6c 20 74 6f 6b 65 6e 73 20 61  ividual tokens a
2f0ed 6e 64 20 73 65 6e 64 73 20 74 68 6f 73 65 20 74  nd sends those t
2f0ee 6f 6b 65 6e 73 20 6f 6e 65 2d 62 79 2d 6f 6e 65  okens one-by-one
2f0ef 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20   over to the.** 
2f0f0 70 61 72 73 65 72 20 66 6f 72 20 61 6e 61 6c 79  parser for analy
2f0f1 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  sis..**.** $Id: 
2f0f2 74 6f 6b 65 6e 69 7a 65 2e 63 2c 76 20 31 2e 31  tokenize.c,v 1.1
2f0f3 35 36 20 32 30 30 39 2f 30 35 2f 30 31 20 32 31  56 2009/05/01 21
2f0f4 3a 31 33 3a 33 37 20 64 72 68 20 45 78 70 20 24  :13:37 drh Exp $
2f0f5 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63  .*/../*.** The c
2f0f6 68 61 72 4d 61 70 28 29 20 6d 61 63 72 6f 20 6d  harMap() macro m
2f0f7 61 70 73 20 61 6c 70 68 61 62 65 74 69 63 20 63  aps alphabetic c
2f0f8 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74  haracters into t
2f0f9 68 65 69 72 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61  heir.** lower-ca
2f0fa 73 65 20 41 53 43 49 49 20 65 71 75 69 76 61 6c  se ASCII equival
2f0fb 65 6e 74 2e 20 20 4f 6e 20 41 53 43 49 49 20 6d  ent.  On ASCII m
2f0fc 61 63 68 69 6e 65 73 2c 20 74 68 69 73 20 69 73  achines, this is
2f0fd 20 6a 75 73 74 0a 2a 2a 20 61 6e 20 75 70 70 65   just.** an uppe
2f0fe 72 2d 74 6f 2d 6c 6f 77 65 72 20 63 61 73 65 20  r-to-lower case 
2f0ff 6d 61 70 2e 20 20 4f 6e 20 45 42 43 44 49 43 20  map.  On EBCDIC 
2f100 6d 61 63 68 69 6e 65 73 20 77 65 20 61 6c 73 6f  machines we also
2f101 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 61 64 6a 75   need.** to adju
2f102 73 74 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 2e  st the encoding.
2f103 20 20 4f 6e 6c 79 20 61 6c 70 68 61 62 65 74 69    Only alphabeti
2f104 63 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64  c characters and
2f105 20 75 6e 64 65 72 73 63 6f 72 65 73 0a 2a 2a 20   underscores.** 
2f106 6e 65 65 64 20 74 6f 20 62 65 20 74 72 61 6e 73  need to be trans
2f107 6c 61 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  lated..*/.#ifdef
2f108 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20   SQLITE_ASCII.# 
2f109 64 65 66 69 6e 65 20 63 68 61 72 4d 61 70 28 58  define charMap(X
2f10a 29 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  ) sqlite3UpperTo
2f10b 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20  Lower[(unsigned 
2f10c 63 68 61 72 29 58 5d 0a 23 65 6e 64 69 66 0a 23  char)X].#endif.#
2f10d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
2f10e 44 49 43 0a 23 20 64 65 66 69 6e 65 20 63 68 61  DIC.# define cha
2f10f 72 4d 61 70 28 58 29 20 65 62 63 64 69 63 54 6f  rMap(X) ebcdicTo
2f110 41 73 63 69 69 5b 28 75 6e 73 69 67 6e 65 64 20  Ascii[(unsigned 
2f111 63 68 61 72 29 58 5d 0a 63 6f 6e 73 74 20 75 6e  char)X].const un
2f112 73 69 67 6e 65 64 20 63 68 61 72 20 65 62 63 64  signed char ebcd
2f113 69 63 54 6f 41 73 63 69 69 5b 5d 20 3d 20 7b 0a  icToAscii[] = {.
2f114 2f 2a 20 30 20 20 20 31 20 20 20 32 20 20 20 33  /* 0   1   2   3
2f115 20 20 20 34 20 20 20 35 20 20 20 36 20 20 20 37     4   5   6   7
2f116 20 20 20 38 20 20 20 39 20 20 20 41 20 20 20 42     8   9   A   B
2f117 20 20 20 43 20 20 20 44 20 20 20 45 20 20 20 46     C   D   E   F
2f118 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30   */.   0,  0,  0
2f119 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f11a 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f11b 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f11c 2c 20 20 30 2c 20 20 2f 2a 20 30 78 20 2a 2f 0a  ,  0,  /* 0x */.
2f11d 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30     0,  0,  0,  0
2f11e 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f11f 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f120 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f121 2c 20 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 30  ,  /* 1x */.   0
2f122 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f123 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f124 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f125 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
2f126 2a 20 32 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * 2x */.   0,  0
2f127 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f128 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f129 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f12a 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 33 78  ,  0,  0,  /* 3x
2f12b 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30   */.   0,  0,  0
2f12c 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f12d 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f12e 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f12f 2c 20 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a  ,  0,  /* 4x */.
2f130 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30     0,  0,  0,  0
2f131 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f132 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f133 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f134 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 30  ,  /* 5x */.   0
2f135 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f136 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f137 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f138 2c 20 39 35 2c 20 20 30 2c 20 20 30 2c 20 20 2f  , 95,  0,  0,  /
2f139 2a 20 36 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * 6x */.   0,  0
2f13a 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f13b 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f13c 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f13d 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 37 78  ,  0,  0,  /* 7x
2f13e 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20 39 38   */.   0, 97, 98
2f13f 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32  , 99,100,101,102
2f140 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20 30  ,103,104,105,  0
2f141 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f142 2c 20 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f 0a  ,  0,  /* 8x */.
2f143 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30 38     0,106,107,108
2f144 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32  ,109,110,111,112
2f145 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20 30  ,113,114,  0,  0
2f146 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f147 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 30  ,  /* 9x */.   0
2f148 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31 37  ,  0,115,116,117
2f149 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31  ,118,119,120,121
2f14a 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,122,  0,  0,  0
2f14b 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
2f14c 2a 20 41 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * Ax */.   0,  0
2f14d 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f14e 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f14f 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f150 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 42 78  ,  0,  0,  /* Bx
2f151 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20 39 38   */.   0, 97, 98
2f152 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32  , 99,100,101,102
2f153 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20 30  ,103,104,105,  0
2f154 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f155 2c 20 20 30 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a  ,  0,  /* Cx */.
2f156 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30 38     0,106,107,108
2f157 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32  ,109,110,111,112
2f158 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20 30  ,113,114,  0,  0
2f159 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f15a 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 30  ,  /* Dx */.   0
2f15b 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31 37  ,  0,115,116,117
2f15c 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31  ,118,119,120,121
2f15d 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,122,  0,  0,  0
2f15e 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
2f15f 2a 20 45 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * Ex */.   0,  0
2f160 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f161 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f162 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
2f163 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 46 78  ,  0,  0,  /* Fx
2f164 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 2f   */.};.#endif../
2f165 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2f166 4b 65 79 77 6f 72 64 43 6f 64 65 20 66 75 6e 63  KeywordCode func
2f167 74 69 6f 6e 20 6c 6f 6f 6b 73 20 75 70 20 61 6e  tion looks up an
2f168 20 69 64 65 6e 74 69 66 69 65 72 20 74 6f 20 64   identifier to d
2f169 65 74 65 72 6d 69 6e 65 20 69 66 0a 2a 2a 20 69  etermine if.** i
2f16a 74 20 69 73 20 61 20 6b 65 79 77 6f 72 64 2e 20  t is a keyword. 
2f16b 20 49 66 20 69 74 20 69 73 20 61 20 6b 65 79 77   If it is a keyw
2f16c 6f 72 64 2c 20 74 68 65 20 74 6f 6b 65 6e 20 63  ord, the token c
2f16d 6f 64 65 20 6f 66 20 74 68 61 74 20 6b 65 79 77  ode of that keyw
2f16e 6f 72 64 20 69 73 20 0a 2a 2a 20 72 65 74 75 72  ord is .** retur
2f16f 6e 65 64 2e 20 20 49 66 20 74 68 65 20 69 6e 70  ned.  If the inp
2f170 75 74 20 69 73 20 6e 6f 74 20 61 20 6b 65 79 77  ut is not a keyw
2f171 6f 72 64 2c 20 54 4b 5f 49 44 20 69 73 20 72 65  ord, TK_ID is re
2f172 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
2f173 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
2f174 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
2f175 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 62   was generated b
2f176 79 20 61 20 70 72 6f 67 72 61 6d 2c 0a 2a 2a 20  y a program,.** 
2f177 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 2c  mkkeywordhash.h,
2f178 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20   located in the 
2f179 74 6f 6f 6c 20 73 75 62 64 69 72 65 63 74 6f 72  tool subdirector
2f17a 79 20 6f 66 20 74 68 65 20 64 69 73 74 72 69 62  y of the distrib
2f17b 75 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 6f 75  ution..** The ou
2f17c 74 70 75 74 20 6f 66 20 74 68 65 20 6d 6b 6b 65  tput of the mkke
2f17d 79 77 6f 72 64 68 61 73 68 2e 63 20 70 72 6f 67  ywordhash.c prog
2f17e 72 61 6d 20 69 73 20 77 72 69 74 74 65 6e 20 69  ram is written i
2f17f 6e 74 6f 20 61 20 66 69 6c 65 0a 2a 2a 20 6e 61  nto a file.** na
2f180 6d 65 64 20 6b 65 79 77 6f 72 64 68 61 73 68 2e  med keywordhash.
2f181 68 20 61 6e 64 20 74 68 65 6e 20 69 6e 63 6c 75  h and then inclu
2f182 64 65 64 20 69 6e 74 6f 20 74 68 69 73 20 73 6f  ded into this so
2f183 75 72 63 65 20 66 69 6c 65 20 62 79 0a 2a 2a 20  urce file by.** 
2f184 74 68 65 20 23 69 6e 63 6c 75 64 65 20 62 65 6c  the #include bel
2f185 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ow..*/./********
2f186 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6b  ****** Include k
2f187 65 79 77 6f 72 64 68 61 73 68 2e 68 20 69 6e 20  eywordhash.h in 
2f188 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 6f  the middle of to
2f189 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  kenize.c *******
2f18a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2f18b 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
2f18c 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20  e keywordhash.h 
2f18d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f18e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f18f 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 20 54 68  *****/./***** Th
2f190 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
2f191 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67   automatically g
2f192 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a 2a  enerated code **
2f193 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ****.**.** The c
2f194 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
2f195 20 68 61 73 20 62 65 65 6e 20 61 75 74 6f 6d 61   has been automa
2f196 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65  tically generate
2f197 64 20 62 79 0a 2a 2a 0a 2a 2a 20 20 20 20 20 24  d by.**.**     $
2f198 48 65 61 64 65 72 3a 20 2f 73 71 6c 69 74 65 2f  Header: /sqlite/
2f199 73 71 6c 69 74 65 2f 74 6f 6f 6c 2f 6d 6b 6b 65  sqlite/tool/mkke
2f19a 79 77 6f 72 64 68 61 73 68 2e 63 2c 76 20 31 2e  ywordhash.c,v 1.
2f19b 33 37 20 32 30 30 39 2f 30 32 2f 30 31 20 30 30  37 2009/02/01 00
2f19c 3a 30 30 3a 34 36 20 64 72 68 20 45 78 70 20 24  :00:46 drh Exp $
2f19d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
2f19e 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 6d 70  in this file imp
2f19f 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69  lements a functi
2f1a0 6f 6e 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  on that determin
2f1a1 65 73 20 77 68 65 74 68 65 72 0a 2a 2a 20 6f 72  es whether.** or
2f1a2 20 6e 6f 74 20 61 20 67 69 76 65 6e 20 69 64 65   not a given ide
2f1a3 6e 74 69 66 69 65 72 20 69 73 20 72 65 61 6c 6c  ntifier is reall
2f1a4 79 20 61 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64  y an SQL keyword
2f1a5 2e 20 20 54 68 65 20 73 61 6d 65 20 74 68 69 6e  .  The same thin
2f1a6 67 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 69 6d  g.** might be im
2f1a7 70 6c 65 6d 65 6e 74 65 64 20 6d 6f 72 65 20 64  plemented more d
2f1a8 69 72 65 63 74 6c 79 20 75 73 69 6e 67 20 61 20  irectly using a 
2f1a9 68 61 6e 64 2d 77 72 69 74 74 65 6e 20 68 61 73  hand-written has
2f1aa 68 20 74 61 62 6c 65 2e 0a 2a 2a 20 42 75 74 20  h table..** But 
2f1ab 62 79 20 75 73 69 6e 67 20 74 68 69 73 20 61 75  by using this au
2f1ac 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
2f1ad 72 61 74 65 64 20 63 6f 64 65 2c 20 74 68 65 20  rated code, the 
2f1ae 73 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 64 65  size of the code
2f1af 0a 2a 2a 20 69 73 20 73 75 62 73 74 61 6e 74 69  .** is substanti
2f1b0 61 6c 6c 79 20 72 65 64 75 63 65 64 2e 20 20 54  ally reduced.  T
2f1b1 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  his is important
2f1b2 20 66 6f 72 20 65 6d 62 65 64 64 65 64 20 61 70   for embedded ap
2f1b3 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6f 6e  plications.** on
2f1b4 20 70 6c 61 74 66 6f 72 6d 73 20 77 69 74 68 20   platforms with 
2f1b5 6c 69 6d 69 74 65 64 20 6d 65 6d 6f 72 79 2e 0a  limited memory..
2f1b6 2a 2f 0a 2f 2a 20 48 61 73 68 20 73 63 6f 72 65  */./* Hash score
2f1b7 3a 20 31 37 31 20 2a 2f 0a 73 74 61 74 69 63 20  : 171 */.static 
2f1b8 69 6e 74 20 6b 65 79 77 6f 72 64 43 6f 64 65 28  int keywordCode(
2f1b9 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
2f1ba 6e 74 20 6e 29 7b 0a 20 20 2f 2a 20 7a 54 65 78  nt n){.  /* zTex
2f1bb 74 5b 5d 20 65 6e 63 6f 64 65 73 20 38 30 31 20  t[] encodes 801 
2f1bc 62 79 74 65 73 20 6f 66 20 6b 65 79 77 6f 72 64  bytes of keyword
2f1bd 73 20 69 6e 20 35 34 31 20 62 79 74 65 73 20 2a  s in 541 bytes *
2f1be 2f 0a 20 20 2f 2a 20 20 20 52 45 49 4e 44 45 58  /.  /*   REINDEX
2f1bf 45 44 45 53 43 41 50 45 41 43 48 45 43 4b 45 59  EDESCAPEACHECKEY
2f1c0 42 45 46 4f 52 45 49 47 4e 4f 52 45 47 45 58 50  BEFOREIGNOREGEXP
2f1c1 4c 41 49 4e 53 54 45 41 44 44 41 54 41 42 41 53  LAINSTEADDATABAS
2f1c2 45 4c 45 43 54 20 20 20 20 20 20 20 2a 2f 0a 20  ELECT       */. 
2f1c3 20 2f 2a 20 20 20 41 42 4c 45 46 54 48 45 4e 44   /*   ABLEFTHEND
2f1c4 45 46 45 52 52 41 42 4c 45 4c 53 45 58 43 45 50  EFERRABLELSEXCEP
2f1c5 54 52 41 4e 53 41 43 54 49 4f 4e 41 54 55 52 41  TRANSACTIONATURA
2f1c6 4c 54 45 52 41 49 53 45 58 43 4c 55 53 49 56 45  LTERAISEXCLUSIVE
2f1c7 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a           */.  /*
2f1c8 20 20 20 58 49 53 54 53 41 56 45 50 4f 49 4e 54     XISTSAVEPOINT
2f1c9 45 52 53 45 43 54 52 49 47 47 45 52 45 46 45 52  ERSECTRIGGEREFER
2f1ca 45 4e 43 45 53 43 4f 4e 53 54 52 41 49 4e 54 4f  ENCESCONSTRAINTO
2f1cb 46 46 53 45 54 45 4d 50 4f 52 41 52 59 20 20 20  FFSETEMPORARY   
2f1cc 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20        */.  /*   
2f1cd 55 4e 49 51 55 45 52 59 41 54 54 41 43 48 41 56  UNIQUERYATTACHAV
2f1ce 49 4e 47 52 4f 55 50 44 41 54 45 42 45 47 49 4e  INGROUPDATEBEGIN
2f1cf 4e 45 52 45 4c 45 41 53 45 42 45 54 57 45 45 4e  NERELEASEBETWEEN
2f1d0 4f 54 4e 55 4c 4c 49 4b 45 20 20 20 20 20 20 20  OTNULLIKE       
2f1d1 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 43 41 53     */.  /*   CAS
2f1d2 43 41 44 45 4c 45 54 45 43 41 53 45 43 4f 4c 4c  CADELETECASECOLL
2f1d3 41 54 45 43 52 45 41 54 45 43 55 52 52 45 4e 54  ATECREATECURRENT
2f1d4 5f 44 41 54 45 44 45 54 41 43 48 49 4d 4d 45 44  _DATEDETACHIMMED
2f1d5 49 41 54 45 4a 4f 49 4e 20 20 20 20 20 20 20 20  IATEJOIN        
2f1d6 2a 2f 0a 20 20 2f 2a 20 20 20 53 45 52 54 4d 41  */.  /*   SERTMA
2f1d7 54 43 48 50 4c 41 4e 41 4c 59 5a 45 50 52 41 47  TCHPLANALYZEPRAG
2f1d8 4d 41 42 4f 52 54 56 41 4c 55 45 53 56 49 52 54  MABORTVALUESVIRT
2f1d9 55 41 4c 49 4d 49 54 57 48 45 4e 57 48 45 52 45  UALIMITWHENWHERE
2f1da 4e 41 4d 45 20 20 20 20 20 20 20 20 20 2a 2f 0a  NAME         */.
2f1db 20 20 2f 2a 20 20 20 41 46 54 45 52 45 50 4c 41    /*   AFTEREPLA
2f1dc 43 45 41 4e 44 45 46 41 55 4c 54 41 55 54 4f 49  CEANDEFAULTAUTOI
2f1dd 4e 43 52 45 4d 45 4e 54 43 41 53 54 43 4f 4c 55  NCREMENTCASTCOLU
2f1de 4d 4e 43 4f 4d 4d 49 54 43 4f 4e 46 4c 49 43 54  MNCOMMITCONFLICT
2f1df 43 52 4f 53 53 20 20 20 20 20 2a 2f 0a 20 20 2f  CROSS     */.  /
2f1e0 2a 20 20 20 43 55 52 52 45 4e 54 5f 54 49 4d 45  *   CURRENT_TIME
2f1e1 53 54 41 4d 50 52 49 4d 41 52 59 44 45 46 45 52  STAMPRIMARYDEFER
2f1e2 52 45 44 49 53 54 49 4e 43 54 44 52 4f 50 46 41  REDISTINCTDROPFA
2f1e3 49 4c 46 52 4f 4d 46 55 4c 4c 47 4c 4f 42 59 49  ILFROMFULLGLOBYI
2f1e4 46 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20  F      */.  /*  
2f1e5 20 49 53 4e 55 4c 4c 4f 52 44 45 52 45 53 54 52   ISNULLORDERESTR
2f1e6 49 43 54 4f 55 54 45 52 49 47 48 54 52 4f 4c 4c  ICTOUTERIGHTROLL
2f1e7 42 41 43 4b 52 4f 57 55 4e 49 4f 4e 55 53 49 4e  BACKROWUNIONUSIN
2f1e8 47 56 41 43 55 55 4d 56 49 45 57 20 20 20 20 20  GVACUUMVIEW     
2f1e9 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 49 4e      */.  /*   IN
2f1ea 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20 20 20  ITIALLY         
2f1eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1ee 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
2f1ef 73 74 20 63 68 61 72 20 7a 54 65 78 74 5b 35 34  st char zText[54
2f1f0 30 5d 20 3d 20 7b 0a 20 20 20 20 27 52 27 2c 27  0] = {.    'R','
2f1f1 45 27 2c 27 49 27 2c 27 4e 27 2c 27 44 27 2c 27  E','I','N','D','
2f1f2 45 27 2c 27 58 27 2c 27 45 27 2c 27 44 27 2c 27  E','X','E','D','
2f1f3 45 27 2c 27 53 27 2c 27 43 27 2c 27 41 27 2c 27  E','S','C','A','
2f1f4 50 27 2c 27 45 27 2c 27 41 27 2c 27 43 27 2c 27  P','E','A','C','
2f1f5 48 27 2c 0a 20 20 20 20 27 45 27 2c 27 43 27 2c  H',.    'E','C',
2f1f6 27 4b 27 2c 27 45 27 2c 27 59 27 2c 27 42 27 2c  'K','E','Y','B',
2f1f7 27 45 27 2c 27 46 27 2c 27 4f 27 2c 27 52 27 2c  'E','F','O','R',
2f1f8 27 45 27 2c 27 49 27 2c 27 47 27 2c 27 4e 27 2c  'E','I','G','N',
2f1f9 27 4f 27 2c 27 52 27 2c 27 45 27 2c 27 47 27 2c  'O','R','E','G',
2f1fa 0a 20 20 20 20 27 45 27 2c 27 58 27 2c 27 50 27  .    'E','X','P'
2f1fb 2c 27 4c 27 2c 27 41 27 2c 27 49 27 2c 27 4e 27  ,'L','A','I','N'
2f1fc 2c 27 53 27 2c 27 54 27 2c 27 45 27 2c 27 41 27  ,'S','T','E','A'
2f1fd 2c 27 44 27 2c 27 44 27 2c 27 41 27 2c 27 54 27  ,'D','D','A','T'
2f1fe 2c 27 41 27 2c 27 42 27 2c 27 41 27 2c 0a 20 20  ,'A','B','A',.  
2f1ff 20 20 27 53 27 2c 27 45 27 2c 27 4c 27 2c 27 45    'S','E','L','E
2f200 27 2c 27 43 27 2c 27 54 27 2c 27 41 27 2c 27 42  ','C','T','A','B
2f201 27 2c 27 4c 27 2c 27 45 27 2c 27 46 27 2c 27 54  ','L','E','F','T
2f202 27 2c 27 48 27 2c 27 45 27 2c 27 4e 27 2c 27 44  ','H','E','N','D
2f203 27 2c 27 45 27 2c 27 46 27 2c 0a 20 20 20 20 27  ','E','F',.    '
2f204 45 27 2c 27 52 27 2c 27 52 27 2c 27 41 27 2c 27  E','R','R','A','
2f205 42 27 2c 27 4c 27 2c 27 45 27 2c 27 4c 27 2c 27  B','L','E','L','
2f206 53 27 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c 27  S','E','X','C','
2f207 45 27 2c 27 50 27 2c 27 54 27 2c 27 52 27 2c 27  E','P','T','R','
2f208 41 27 2c 27 4e 27 2c 0a 20 20 20 20 27 53 27 2c  A','N',.    'S',
2f209 27 41 27 2c 27 43 27 2c 27 54 27 2c 27 49 27 2c  'A','C','T','I',
2f20a 27 4f 27 2c 27 4e 27 2c 27 41 27 2c 27 54 27 2c  'O','N','A','T',
2f20b 27 55 27 2c 27 52 27 2c 27 41 27 2c 27 4c 27 2c  'U','R','A','L',
2f20c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 41 27 2c  'T','E','R','A',
2f20d 27 49 27 2c 0a 20 20 20 20 27 53 27 2c 27 45 27  'I',.    'S','E'
2f20e 2c 27 58 27 2c 27 43 27 2c 27 4c 27 2c 27 55 27  ,'X','C','L','U'
2f20f 2c 27 53 27 2c 27 49 27 2c 27 56 27 2c 27 45 27  ,'S','I','V','E'
2f210 2c 27 58 27 2c 27 49 27 2c 27 53 27 2c 27 54 27  ,'X','I','S','T'
2f211 2c 27 53 27 2c 27 41 27 2c 27 56 27 2c 27 45 27  ,'S','A','V','E'
2f212 2c 0a 20 20 20 20 27 50 27 2c 27 4f 27 2c 27 49  ,.    'P','O','I
2f213 27 2c 27 4e 27 2c 27 54 27 2c 27 45 27 2c 27 52  ','N','T','E','R
2f214 27 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c 27 54  ','S','E','C','T
2f215 27 2c 27 52 27 2c 27 49 27 2c 27 47 27 2c 27 47  ','R','I','G','G
2f216 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 0a 20  ','E','R','E',. 
2f217 20 20 20 27 46 27 2c 27 45 27 2c 27 52 27 2c 27     'F','E','R','
2f218 45 27 2c 27 4e 27 2c 27 43 27 2c 27 45 27 2c 27  E','N','C','E','
2f219 53 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27 2c 27  S','C','O','N','
2f21a 53 27 2c 27 54 27 2c 27 52 27 2c 27 41 27 2c 27  S','T','R','A','
2f21b 49 27 2c 27 4e 27 2c 27 54 27 2c 0a 20 20 20 20  I','N','T',.    
2f21c 27 4f 27 2c 27 46 27 2c 27 46 27 2c 27 53 27 2c  'O','F','F','S',
2f21d 27 45 27 2c 27 54 27 2c 27 45 27 2c 27 4d 27 2c  'E','T','E','M',
2f21e 27 50 27 2c 27 4f 27 2c 27 52 27 2c 27 41 27 2c  'P','O','R','A',
2f21f 27 52 27 2c 27 59 27 2c 27 55 27 2c 27 4e 27 2c  'R','Y','U','N',
2f220 27 49 27 2c 27 51 27 2c 0a 20 20 20 20 27 55 27  'I','Q',.    'U'
2f221 2c 27 45 27 2c 27 52 27 2c 27 59 27 2c 27 41 27  ,'E','R','Y','A'
2f222 2c 27 54 27 2c 27 54 27 2c 27 41 27 2c 27 43 27  ,'T','T','A','C'
2f223 2c 27 48 27 2c 27 41 27 2c 27 56 27 2c 27 49 27  ,'H','A','V','I'
2f224 2c 27 4e 27 2c 27 47 27 2c 27 52 27 2c 27 4f 27  ,'N','G','R','O'
2f225 2c 27 55 27 2c 0a 20 20 20 20 27 50 27 2c 27 44  ,'U',.    'P','D
2f226 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 42  ','A','T','E','B
2f227 27 2c 27 45 27 2c 27 47 27 2c 27 49 27 2c 27 4e  ','E','G','I','N
2f228 27 2c 27 4e 27 2c 27 45 27 2c 27 52 27 2c 27 45  ','N','E','R','E
2f229 27 2c 27 4c 27 2c 27 45 27 2c 27 41 27 2c 27 53  ','L','E','A','S
2f22a 27 2c 0a 20 20 20 20 27 45 27 2c 27 42 27 2c 27  ',.    'E','B','
2f22b 45 27 2c 27 54 27 2c 27 57 27 2c 27 45 27 2c 27  E','T','W','E','
2f22c 45 27 2c 27 4e 27 2c 27 4f 27 2c 27 54 27 2c 27  E','N','O','T','
2f22d 4e 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27  N','U','L','L','
2f22e 49 27 2c 27 4b 27 2c 27 45 27 2c 27 43 27 2c 0a  I','K','E','C',.
2f22f 20 20 20 20 27 41 27 2c 27 53 27 2c 27 43 27 2c      'A','S','C',
2f230 27 41 27 2c 27 44 27 2c 27 45 27 2c 27 4c 27 2c  'A','D','E','L',
2f231 27 45 27 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c  'E','T','E','C',
2f232 27 41 27 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c  'A','S','E','C',
2f233 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20 20  'O','L','L',.   
2f234 20 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 43 27   'A','T','E','C'
2f235 2c 27 52 27 2c 27 45 27 2c 27 41 27 2c 27 54 27  ,'R','E','A','T'
2f236 2c 27 45 27 2c 27 43 27 2c 27 55 27 2c 27 52 27  ,'E','C','U','R'
2f237 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27  ,'R','E','N','T'
2f238 2c 27 5f 27 2c 27 44 27 2c 0a 20 20 20 20 27 41  ,'_','D',.    'A
2f239 27 2c 27 54 27 2c 27 45 27 2c 27 44 27 2c 27 45  ','T','E','D','E
2f23a 27 2c 27 54 27 2c 27 41 27 2c 27 43 27 2c 27 48  ','T','A','C','H
2f23b 27 2c 27 49 27 2c 27 4d 27 2c 27 4d 27 2c 27 45  ','I','M','M','E
2f23c 27 2c 27 44 27 2c 27 49 27 2c 27 41 27 2c 27 54  ','D','I','A','T
2f23d 27 2c 27 45 27 2c 0a 20 20 20 20 27 4a 27 2c 27  ','E',.    'J','
2f23e 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 53 27 2c 27  O','I','N','S','
2f23f 45 27 2c 27 52 27 2c 27 54 27 2c 27 4d 27 2c 27  E','R','T','M','
2f240 41 27 2c 27 54 27 2c 27 43 27 2c 27 48 27 2c 27  A','T','C','H','
2f241 50 27 2c 27 4c 27 2c 27 41 27 2c 27 4e 27 2c 27  P','L','A','N','
2f242 41 27 2c 0a 20 20 20 20 27 4c 27 2c 27 59 27 2c  A',.    'L','Y',
2f243 27 5a 27 2c 27 45 27 2c 27 50 27 2c 27 52 27 2c  'Z','E','P','R',
2f244 27 41 27 2c 27 47 27 2c 27 4d 27 2c 27 41 27 2c  'A','G','M','A',
2f245 27 42 27 2c 27 4f 27 2c 27 52 27 2c 27 54 27 2c  'B','O','R','T',
2f246 27 56 27 2c 27 41 27 2c 27 4c 27 2c 27 55 27 2c  'V','A','L','U',
2f247 0a 20 20 20 20 27 45 27 2c 27 53 27 2c 27 56 27  .    'E','S','V'
2f248 2c 27 49 27 2c 27 52 27 2c 27 54 27 2c 27 55 27  ,'I','R','T','U'
2f249 2c 27 41 27 2c 27 4c 27 2c 27 49 27 2c 27 4d 27  ,'A','L','I','M'
2f24a 2c 27 49 27 2c 27 54 27 2c 27 57 27 2c 27 48 27  ,'I','T','W','H'
2f24b 2c 27 45 27 2c 27 4e 27 2c 27 57 27 2c 0a 20 20  ,'E','N','W',.  
2f24c 20 20 27 48 27 2c 27 45 27 2c 27 52 27 2c 27 45    'H','E','R','E
2f24d 27 2c 27 4e 27 2c 27 41 27 2c 27 4d 27 2c 27 45  ','N','A','M','E
2f24e 27 2c 27 41 27 2c 27 46 27 2c 27 54 27 2c 27 45  ','A','F','T','E
2f24f 27 2c 27 52 27 2c 27 45 27 2c 27 50 27 2c 27 4c  ','R','E','P','L
2f250 27 2c 27 41 27 2c 27 43 27 2c 0a 20 20 20 20 27  ','A','C',.    '
2f251 45 27 2c 27 41 27 2c 27 4e 27 2c 27 44 27 2c 27  E','A','N','D','
2f252 45 27 2c 27 46 27 2c 27 41 27 2c 27 55 27 2c 27  E','F','A','U','
2f253 4c 27 2c 27 54 27 2c 27 41 27 2c 27 55 27 2c 27  L','T','A','U','
2f254 54 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27  T','O','I','N','
2f255 43 27 2c 27 52 27 2c 0a 20 20 20 20 27 45 27 2c  C','R',.    'E',
2f256 27 4d 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c  'M','E','N','T',
2f257 27 43 27 2c 27 41 27 2c 27 53 27 2c 27 54 27 2c  'C','A','S','T',
2f258 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27 55 27 2c  'C','O','L','U',
2f259 27 4d 27 2c 27 4e 27 2c 27 43 27 2c 27 4f 27 2c  'M','N','C','O',
2f25a 27 4d 27 2c 0a 20 20 20 20 27 4d 27 2c 27 49 27  'M',.    'M','I'
2f25b 2c 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27  ,'T','C','O','N'
2f25c 2c 27 46 27 2c 27 4c 27 2c 27 49 27 2c 27 43 27  ,'F','L','I','C'
2f25d 2c 27 54 27 2c 27 43 27 2c 27 52 27 2c 27 4f 27  ,'T','C','R','O'
2f25e 2c 27 53 27 2c 27 53 27 2c 27 43 27 2c 27 55 27  ,'S','S','C','U'
2f25f 2c 0a 20 20 20 20 27 52 27 2c 27 52 27 2c 27 45  ,.    'R','R','E
2f260 27 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27 54  ','N','T','_','T
2f261 27 2c 27 49 27 2c 27 4d 27 2c 27 45 27 2c 27 53  ','I','M','E','S
2f262 27 2c 27 54 27 2c 27 41 27 2c 27 4d 27 2c 27 50  ','T','A','M','P
2f263 27 2c 27 52 27 2c 27 49 27 2c 27 4d 27 2c 0a 20  ','R','I','M',. 
2f264 20 20 20 27 41 27 2c 27 52 27 2c 27 59 27 2c 27     'A','R','Y','
2f265 44 27 2c 27 45 27 2c 27 46 27 2c 27 45 27 2c 27  D','E','F','E','
2f266 52 27 2c 27 52 27 2c 27 45 27 2c 27 44 27 2c 27  R','R','E','D','
2f267 49 27 2c 27 53 27 2c 27 54 27 2c 27 49 27 2c 27  I','S','T','I','
2f268 4e 27 2c 27 43 27 2c 27 54 27 2c 0a 20 20 20 20  N','C','T',.    
2f269 27 44 27 2c 27 52 27 2c 27 4f 27 2c 27 50 27 2c  'D','R','O','P',
2f26a 27 46 27 2c 27 41 27 2c 27 49 27 2c 27 4c 27 2c  'F','A','I','L',
2f26b 27 46 27 2c 27 52 27 2c 27 4f 27 2c 27 4d 27 2c  'F','R','O','M',
2f26c 27 46 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c  'F','U','L','L',
2f26d 27 47 27 2c 27 4c 27 2c 0a 20 20 20 20 27 4f 27  'G','L',.    'O'
2f26e 2c 27 42 27 2c 27 59 27 2c 27 49 27 2c 27 46 27  ,'B','Y','I','F'
2f26f 2c 27 49 27 2c 27 53 27 2c 27 4e 27 2c 27 55 27  ,'I','S','N','U'
2f270 2c 27 4c 27 2c 27 4c 27 2c 27 4f 27 2c 27 52 27  ,'L','L','O','R'
2f271 2c 27 44 27 2c 27 45 27 2c 27 52 27 2c 27 45 27  ,'D','E','R','E'
2f272 2c 27 53 27 2c 0a 20 20 20 20 27 54 27 2c 27 52  ,'S',.    'T','R
2f273 27 2c 27 49 27 2c 27 43 27 2c 27 54 27 2c 27 4f  ','I','C','T','O
2f274 27 2c 27 55 27 2c 27 54 27 2c 27 45 27 2c 27 52  ','U','T','E','R
2f275 27 2c 27 49 27 2c 27 47 27 2c 27 48 27 2c 27 54  ','I','G','H','T
2f276 27 2c 27 52 27 2c 27 4f 27 2c 27 4c 27 2c 27 4c  ','R','O','L','L
2f277 27 2c 0a 20 20 20 20 27 42 27 2c 27 41 27 2c 27  ',.    'B','A','
2f278 43 27 2c 27 4b 27 2c 27 52 27 2c 27 4f 27 2c 27  C','K','R','O','
2f279 57 27 2c 27 55 27 2c 27 4e 27 2c 27 49 27 2c 27  W','U','N','I','
2f27a 4f 27 2c 27 4e 27 2c 27 55 27 2c 27 53 27 2c 27  O','N','U','S','
2f27b 49 27 2c 27 4e 27 2c 27 47 27 2c 27 56 27 2c 0a  I','N','G','V',.
2f27c 20 20 20 20 27 41 27 2c 27 43 27 2c 27 55 27 2c      'A','C','U',
2f27d 27 55 27 2c 27 4d 27 2c 27 56 27 2c 27 49 27 2c  'U','M','V','I',
2f27e 27 45 27 2c 27 57 27 2c 27 49 27 2c 27 4e 27 2c  'E','W','I','N',
2f27f 27 49 27 2c 27 54 27 2c 27 49 27 2c 27 41 27 2c  'I','T','I','A',
2f280 27 4c 27 2c 27 4c 27 2c 27 59 27 2c 0a 20 20 7d  'L','L','Y',.  }
2f281 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
2f282 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
2f283 48 61 73 68 5b 31 32 37 5d 20 3d 20 7b 0a 20 20  Hash[127] = {.  
2f284 20 20 20 20 37 30 2c 20 20 39 39 2c 20 31 31 32      70,  99, 112
2f285 2c 20 20 36 38 2c 20 20 20 30 2c 20 20 34 33 2c  ,  68,   0,  43,
2f286 20 20 20 30 2c 20 20 20 30 2c 20 20 37 36 2c 20     0,   0,  76, 
2f287 20 20 30 2c 20 20 37 31 2c 20 20 20 30 2c 20 20    0,  71,   0,  
2f288 20 30 2c 0a 20 20 20 20 20 20 34 31 2c 20 20 31   0,.      41,  1
2f289 32 2c 20 20 37 32 2c 20 20 31 35 2c 20 20 20 30  2,  72,  15,   0
2f28a 2c 20 31 31 31 2c 20 20 37 39 2c 20 20 34 39 2c  , 111,  79,  49,
2f28b 20 31 30 36 2c 20 20 20 30 2c 20 20 31 39 2c 20   106,   0,  19, 
2f28c 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 31    0,   0,.     1
2f28d 31 36 2c 20 20 20 30 2c 20 31 31 34 2c 20 31 30  16,   0, 114, 10
2f28e 39 2c 20 20 20 30 2c 20 20 32 32 2c 20 20 38 37  9,   0,  22,  87
2f28f 2c 20 20 20 30 2c 20 20 20 39 2c 20 20 20 30 2c  ,   0,   9,   0,
2f290 20 20 20 30 2c 20 20 36 34 2c 20 20 36 35 2c 0a     0,  64,  65,.
2f291 20 20 20 20 20 20 20 30 2c 20 20 36 33 2c 20 20         0,  63,  
2f292 20 36 2c 20 20 20 30 2c 20 20 34 37 2c 20 20 38   6,   0,  47,  8
2f293 34 2c 20 20 39 36 2c 20 20 20 30 2c 20 31 31 33  4,  96,   0, 113
2f294 2c 20 20 39 35 2c 20 20 20 30 2c 20 20 20 30 2c  ,  95,   0,   0,
2f295 20 20 34 34 2c 0a 20 20 20 20 20 20 20 30 2c 20    44,.       0, 
2f296 20 39 37 2c 20 20 32 34 2c 20 20 20 30 2c 20 20   97,  24,   0,  
2f297 31 37 2c 20 20 20 30 2c 20 31 31 37 2c 20 20 34  17,   0, 117,  4
2f298 38 2c 20 20 32 33 2c 20 20 20 30 2c 20 20 20 35  8,  23,   0,   5
2f299 2c 20 31 30 34 2c 20 20 32 35 2c 0a 20 20 20 20  , 104,  25,.    
2f29a 20 20 39 30 2c 20 20 20 30 2c 20 20 20 30 2c 20    90,   0,   0, 
2f29b 31 31 39 2c 20 31 30 30 2c 20 20 35 35 2c 20 31  119, 100,  55, 1
2f29c 31 38 2c 20 20 35 32 2c 20 20 20 37 2c 20 20 35  18,  52,   7,  5
2f29d 30 2c 20 20 20 30 2c 20 20 38 35 2c 20 20 20 30  0,   0,  85,   0
2f29e 2c 0a 20 20 20 20 20 20 39 34 2c 20 20 32 36 2c  ,.      94,  26,
2f29f 20 20 20 30 2c 20 20 39 33 2c 20 20 20 30 2c 20     0,  93,   0, 
2f2a0 20 20 30 2c 20 20 20 30 2c 20 20 38 39 2c 20 20    0,   0,  89,  
2f2a1 38 36 2c 20 20 39 31 2c 20 20 38 32 2c 20 31 30  86,  91,  82, 10
2f2a2 33 2c 20 20 31 34 2c 0a 20 20 20 20 20 20 33 38  3,  14,.      38
2f2a3 2c 20 31 30 32 2c 20 20 20 30 2c 20 20 37 35 2c  , 102,   0,  75,
2f2a4 20 20 20 30 2c 20 20 31 38 2c 20 20 38 33 2c 20     0,  18,  83, 
2f2a5 31 30 35 2c 20 20 33 31 2c 20 20 20 30 2c 20 31  105,  31,   0, 1
2f2a6 31 35 2c 20 20 37 34 2c 20 31 30 37 2c 0a 20 20  15,  74, 107,.  
2f2a7 20 20 20 20 35 37 2c 20 20 34 35 2c 20 20 37 38      57,  45,  78
2f2a8 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 38 38 2c  ,   0,   0,  88,
2f2a9 20 20 33 39 2c 20 20 20 30 2c 20 31 31 30 2c 20    39,   0, 110, 
2f2aa 20 20 30 2c 20 20 33 35 2c 20 20 20 30 2c 20 20    0,  35,   0,  
2f2ab 20 30 2c 0a 20 20 20 20 20 20 32 38 2c 20 20 20   0,.      28,   
2f2ac 30 2c 20 20 38 30 2c 20 20 35 33 2c 20 20 35 38  0,  80,  53,  58
2f2ad 2c 20 20 20 30 2c 20 20 32 30 2c 20 20 35 36 2c  ,   0,  20,  56,
2f2ae 20 20 20 30 2c 20 20 35 31 2c 0a 20 20 7d 3b 0a     0,  51,.  };.
2f2af 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2f2b0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4e 65  nsigned char aNe
2f2b1 78 74 5b 31 31 39 5d 20 3d 20 7b 0a 20 20 20 20  xt[119] = {.    
2f2b2 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2f2b3 20 20 30 2c 20 20 20 34 2c 20 20 20 30 2c 20 20    0,   4,   0,  
2f2b4 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2f2b5 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2f2b6 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 32 2c  ,.       0,   2,
2f2b7 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2f2b8 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2f2b9 31 33 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  13,   0,   0,   
2f2ba 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30  0,   0,.       0
2f2bb 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2f2bc 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2f2bd 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2f2be 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20   0,   0,   0,.  
2f2bf 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30       0,   0,   0
2f2c0 2c 20 20 20 30 2c 20 20 33 32 2c 20 20 32 31 2c  ,   0,  32,  21,
2f2c1 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2f2c2 20 34 32 2c 20 20 20 33 2c 20 20 34 36 2c 20 20   42,   3,  46,  
2f2c3 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20   0,.       0,   
2f2c4 30 2c 20 20 20 30 2c 20 20 32 39 2c 20 20 20 30  0,   0,  29,   0
2f2c5 2c 20 20 20 30 2c 20 20 33 37 2c 20 20 20 30 2c  ,   0,  37,   0,
2f2c6 20 20 20 30 2c 20 20 20 30 2c 20 20 20 31 2c 20     0,   0,   1, 
2f2c7 20 36 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20   60,   0,.      
2f2c8 20 30 2c 20 20 36 31 2c 20 20 20 30 2c 20 20 34   0,  61,   0,  4
2f2c9 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2f2ca 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2f2cb 20 20 20 30 2c 20 20 35 39 2c 20 20 20 30 2c 0a     0,  59,   0,.
2f2cc 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20         0,   0,  
2f2cd 20 30 2c 20 20 33 30 2c 20 20 35 34 2c 20 20 31   0,  30,  54,  1
2f2ce 36 2c 20 20 33 33 2c 20 20 31 30 2c 20 20 20 30  6,  33,  10,   0
2f2cf 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2f2d0 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20     0,.       0, 
2f2d1 20 20 30 2c 20 20 31 31 2c 20 20 36 36 2c 20 20    0,  11,  66,  
2f2d2 37 33 2c 20 20 20 30 2c 20 20 20 38 2c 20 20 20  73,   0,   8,   
2f2d3 30 2c 20 20 39 38 2c 20 20 39 32 2c 20 20 20 30  0,  98,  92,   0
2f2d4 2c 20 31 30 31 2c 20 20 20 30 2c 0a 20 20 20 20  , 101,   0,.    
2f2d5 20 20 38 31 2c 20 20 20 30 2c 20 20 36 39 2c 20    81,   0,  69, 
2f2d6 20 20 30 2c 20 20 20 30 2c 20 31 30 38 2c 20 20    0,   0, 108,  
2f2d7 32 37 2c 20 20 33 36 2c 20 20 36 37 2c 20 20 37  27,  36,  67,  7
2f2d8 37 2c 20 20 20 30 2c 20 20 33 34 2c 20 20 36 32  7,   0,  34,  62
2f2d9 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c  ,.       0,   0,
2f2da 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  .  };.  static c
2f2db 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2f2dc 61 72 20 61 4c 65 6e 5b 31 31 39 5d 20 3d 20 7b  ar aLen[119] = {
2f2dd 0a 20 20 20 20 20 20 20 37 2c 20 20 20 37 2c 20  .       7,   7, 
2f2de 20 20 35 2c 20 20 20 34 2c 20 20 20 36 2c 20 20    5,   4,   6,  
2f2df 20 34 2c 20 20 20 35 2c 20 20 20 33 2c 20 20 20   4,   5,   3,   
2f2e0 36 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 36  6,   7,   3,   6
2f2e1 2c 20 20 20 36 2c 0a 20 20 20 20 20 20 20 37 2c  ,   6,.       7,
2f2e2 20 20 20 37 2c 20 20 20 33 2c 20 20 20 38 2c 20     7,   3,   8, 
2f2e3 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20 20    2,   6,   5,  
2f2e4 20 34 2c 20 20 20 34 2c 20 20 20 33 2c 20 20 31   4,   4,   3,  1
2f2e5 30 2c 20 20 20 34 2c 20 20 20 36 2c 0a 20 20 20  0,   4,   6,.   
2f2e6 20 20 20 31 31 2c 20 20 20 32 2c 20 20 20 37 2c     11,   2,   7,
2f2e7 20 20 20 35 2c 20 20 20 35 2c 20 20 20 39 2c 20     5,   5,   9, 
2f2e8 20 20 36 2c 20 20 20 39 2c 20 20 20 39 2c 20 20    6,   9,   9,  
2f2e9 20 37 2c 20 20 31 30 2c 20 20 31 30 2c 20 20 20   7,  10,  10,   
2f2ea 34 2c 0a 20 20 20 20 20 20 20 36 2c 20 20 20 32  4,.       6,   2
2f2eb 2c 20 20 20 33 2c 20 20 20 34 2c 20 20 20 39 2c  ,   3,   4,   9,
2f2ec 20 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20     2,   6,   5, 
2f2ed 20 20 36 2c 20 20 20 36 2c 20 20 20 35 2c 20 20    6,   6,   5,  
2f2ee 20 36 2c 20 20 20 35 2c 0a 20 20 20 20 20 20 20   6,   5,.       
2f2ef 35 2c 20 20 20 37 2c 20 20 20 37 2c 20 20 20 37  5,   7,   7,   7
2f2f0 2c 20 20 20 33 2c 20 20 20 34 2c 20 20 20 34 2c  ,   3,   4,   4,
2f2f1 20 20 20 37 2c 20 20 20 33 2c 20 20 20 36 2c 20     7,   3,   6, 
2f2f2 20 20 34 2c 20 20 20 37 2c 20 20 20 36 2c 0a 20    4,   7,   6,. 
2f2f3 20 20 20 20 20 31 32 2c 20 20 20 36 2c 20 20 20       12,   6,   
2f2f4 39 2c 20 20 20 34 2c 20 20 20 36 2c 20 20 20 35  9,   4,   6,   5
2f2f5 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20 36 2c  ,   4,   7,   6,
2f2f6 20 20 20 35 2c 20 20 20 36 2c 20 20 20 37 2c 20     5,   6,   7, 
2f2f7 20 20 35 2c 0a 20 20 20 20 20 20 20 34 2c 20 20    5,.       4,  
2f2f8 20 35 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20   5,   6,   5,   
2f2f9 37 2c 20 20 20 33 2c 20 20 20 37 2c 20 20 31 33  7,   3,   7,  13
2f2fa 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20 34 2c  ,   2,   2,   4,
2f2fb 20 20 20 36 2c 20 20 20 36 2c 0a 20 20 20 20 20     6,   6,.     
2f2fc 20 20 38 2c 20 20 20 35 2c 20 20 31 37 2c 20 20    8,   5,  17,  
2f2fd 31 32 2c 20 20 20 37 2c 20 20 20 38 2c 20 20 20  12,   7,   8,   
2f2fe 38 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20 34  8,   2,   4,   4
2f2ff 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 34 2c  ,   4,   4,   4,
2f300 0a 20 20 20 20 20 20 20 32 2c 20 20 20 32 2c 20  .       2,   2, 
2f301 20 20 36 2c 20 20 20 35 2c 20 20 20 38 2c 20 20    6,   5,   8,  
2f302 20 35 2c 20 20 20 35 2c 20 20 20 38 2c 20 20 20   5,   5,   8,   
2f303 33 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20 36  3,   5,   5,   6
2f304 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 39 2c  ,   4,.       9,
2f305 20 20 20 33 2c 0a 20 20 7d 3b 0a 20 20 73 74 61     3,.  };.  sta
2f306 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
2f307 65 64 20 73 68 6f 72 74 20 69 6e 74 20 61 4f 66  ed short int aOf
2f308 66 73 65 74 5b 31 31 39 5d 20 3d 20 7b 0a 20 20  fset[119] = {.  
2f309 20 20 20 20 20 30 2c 20 20 20 32 2c 20 20 20 32       0,   2,   2
2f30a 2c 20 20 20 38 2c 20 20 20 39 2c 20 20 31 34 2c  ,   8,   9,  14,
2f30b 20 20 31 36 2c 20 20 32 30 2c 20 20 32 33 2c 20    16,  20,  23, 
2f30c 20 32 35 2c 20 20 32 35 2c 20 20 32 39 2c 20 20   25,  25,  29,  
2f30d 33 33 2c 0a 20 20 20 20 20 20 33 36 2c 20 20 34  33,.      36,  4
2f30e 31 2c 20 20 34 36 2c 20 20 34 38 2c 20 20 35 33  1,  46,  48,  53
2f30f 2c 20 20 35 34 2c 20 20 35 39 2c 20 20 36 32 2c  ,  54,  59,  62,
2f310 20 20 36 35 2c 20 20 36 37 2c 20 20 36 39 2c 20    65,  67,  69, 
2f311 20 37 38 2c 20 20 38 31 2c 0a 20 20 20 20 20 20   78,  81,.      
2f312 38 36 2c 20 20 39 35 2c 20 20 39 36 2c 20 31 30  86,  95,  96, 10
2f313 31 2c 20 31 30 35 2c 20 31 30 39 2c 20 31 31 37  1, 105, 109, 117
2f314 2c 20 31 32 32 2c 20 31 32 38 2c 20 31 33 36 2c  , 122, 128, 136,
2f315 20 31 34 32 2c 20 31 35 32 2c 20 31 35 39 2c 0a   142, 152, 159,.
2f316 20 20 20 20 20 31 36 32 2c 20 31 36 32 2c 20 31       162, 162, 1
2f317 36 35 2c 20 31 36 37 2c 20 31 36 37 2c 20 31 37  65, 167, 167, 17
2f318 31 2c 20 31 37 36 2c 20 31 37 39 2c 20 31 38 34  1, 176, 179, 184
2f319 2c 20 31 38 39 2c 20 31 39 34 2c 20 31 39 37 2c  , 189, 194, 197,
2f31a 20 32 30 33 2c 0a 20 20 20 20 20 32 30 36 2c 20   203,.     206, 
2f31b 32 31 30 2c 20 32 31 37 2c 20 32 32 33 2c 20 32  210, 217, 223, 2
2f31c 32 33 2c 20 32 32 36 2c 20 32 32 39 2c 20 32 33  23, 226, 229, 23
2f31d 33 2c 20 32 33 34 2c 20 32 33 38 2c 20 32 34 34  3, 234, 238, 244
2f31e 2c 20 32 34 38 2c 20 32 35 35 2c 0a 20 20 20 20  , 248, 255,.    
2f31f 20 32 36 31 2c 20 32 37 33 2c 20 32 37 39 2c 20   261, 273, 279, 
2f320 32 38 38 2c 20 32 39 30 2c 20 32 39 36 2c 20 33  288, 290, 296, 3
2f321 30 31 2c 20 33 30 33 2c 20 33 31 30 2c 20 33 31  01, 303, 310, 31
2f322 35 2c 20 33 32 30 2c 20 33 32 36 2c 20 33 33 32  5, 320, 326, 332
2f323 2c 0a 20 20 20 20 20 33 33 37 2c 20 33 34 31 2c  ,.     337, 341,
2f324 20 33 34 34 2c 20 33 35 30 2c 20 33 35 34 2c 20   344, 350, 354, 
2f325 33 36 31 2c 20 33 36 33 2c 20 33 37 30 2c 20 33  361, 363, 370, 3
2f326 37 32 2c 20 33 37 34 2c 20 33 38 33 2c 20 33 38  72, 374, 383, 38
2f327 37 2c 20 33 39 33 2c 0a 20 20 20 20 20 33 39 39  7, 393,.     399
2f328 2c 20 34 30 37 2c 20 34 31 32 2c 20 34 31 32 2c  , 407, 412, 412,
2f329 20 34 32 38 2c 20 34 33 35 2c 20 34 34 32 2c 20   428, 435, 442, 
2f32a 34 34 33 2c 20 34 35 30 2c 20 34 35 34 2c 20 34  443, 450, 454, 4
2f32b 35 38 2c 20 34 36 32 2c 20 34 36 36 2c 0a 20 20  58, 462, 466,.  
2f32c 20 20 20 34 36 39 2c 20 34 37 31 2c 20 34 37 33     469, 471, 473
2f32d 2c 20 34 37 39 2c 20 34 38 33 2c 20 34 39 31 2c  , 479, 483, 491,
2f32e 20 34 39 35 2c 20 35 30 30 2c 20 35 30 38 2c 20   495, 500, 508, 
2f32f 35 31 31 2c 20 35 31 36 2c 20 35 32 31 2c 20 35  511, 516, 521, 5
2f330 32 37 2c 0a 20 20 20 20 20 35 33 31 2c 20 35 33  27,.     531, 53
2f331 36 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63  6,.  };.  static
2f332 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
2f333 63 68 61 72 20 61 43 6f 64 65 5b 31 31 39 5d 20  char aCode[119] 
2f334 3d 20 7b 0a 20 20 20 20 54 4b 5f 52 45 49 4e 44  = {.    TK_REIND
2f335 45 58 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 45  EX,    TK_INDEXE
2f336 44 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 2c 20  D,    TK_INDEX, 
2f337 20 20 20 20 20 54 4b 5f 44 45 53 43 2c 20 20 20       TK_DESC,   
2f338 20 20 20 20 54 4b 5f 45 53 43 41 50 45 2c 20 20      TK_ESCAPE,  
2f339 20 20 20 0a 20 20 20 20 54 4b 5f 45 41 43 48 2c     .    TK_EACH,
2f33a 20 20 20 20 20 20 20 54 4b 5f 43 48 45 43 4b 2c         TK_CHECK,
2f33b 20 20 20 20 20 20 54 4b 5f 4b 45 59 2c 20 20 20        TK_KEY,   
2f33c 20 20 20 20 20 54 4b 5f 42 45 46 4f 52 45 2c 20       TK_BEFORE, 
2f33d 20 20 20 20 54 4b 5f 46 4f 52 45 49 47 4e 2c 20      TK_FOREIGN, 
2f33e 20 20 20 0a 20 20 20 20 54 4b 5f 46 4f 52 2c 20     .    TK_FOR, 
2f33f 20 20 20 20 20 20 20 54 4b 5f 49 47 4e 4f 52 45         TK_IGNORE
2f340 2c 20 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57  ,     TK_LIKE_KW
2f341 2c 20 20 20 20 54 4b 5f 45 58 50 4c 41 49 4e 2c  ,    TK_EXPLAIN,
2f342 20 20 20 20 54 4b 5f 49 4e 53 54 45 41 44 2c 20      TK_INSTEAD, 
2f343 20 20 20 0a 20 20 20 20 54 4b 5f 41 44 44 2c 20     .    TK_ADD, 
2f344 20 20 20 20 20 20 20 54 4b 5f 44 41 54 41 42 41         TK_DATABA
2f345 53 45 2c 20 20 20 54 4b 5f 41 53 2c 20 20 20 20  SE,   TK_AS,    
2f346 20 20 20 20 20 54 4b 5f 53 45 4c 45 43 54 2c 20       TK_SELECT, 
2f347 20 20 20 20 54 4b 5f 54 41 42 4c 45 2c 20 20 20      TK_TABLE,   
2f348 20 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f     .    TK_JOIN_
2f349 4b 57 2c 20 20 20 20 54 4b 5f 54 48 45 4e 2c 20  KW,    TK_THEN, 
2f34a 20 20 20 20 20 20 54 4b 5f 45 4e 44 2c 20 20 20        TK_END,   
2f34b 20 20 20 20 20 54 4b 5f 44 45 46 45 52 52 41 42       TK_DEFERRAB
2f34c 4c 45 2c 20 54 4b 5f 45 4c 53 45 2c 20 20 20 20  LE, TK_ELSE,    
2f34d 20 20 20 0a 20 20 20 20 54 4b 5f 45 58 43 45 50     .    TK_EXCEP
2f34e 54 2c 20 20 20 20 20 54 4b 5f 54 52 41 4e 53 41  T,     TK_TRANSA
2f34f 43 54 49 4f 4e 2c 54 4b 5f 4f 4e 2c 20 20 20 20  CTION,TK_ON,    
2f350 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c       TK_JOIN_KW,
2f351 20 20 20 20 54 4b 5f 41 4c 54 45 52 2c 20 20 20      TK_ALTER,   
2f352 20 20 20 0a 20 20 20 20 54 4b 5f 52 41 49 53 45     .    TK_RAISE
2f353 2c 20 20 20 20 20 20 54 4b 5f 45 58 43 4c 55 53  ,      TK_EXCLUS
2f354 49 56 45 2c 20 20 54 4b 5f 45 58 49 53 54 53 2c  IVE,  TK_EXISTS,
2f355 20 20 20 20 20 54 4b 5f 53 41 56 45 50 4f 49 4e       TK_SAVEPOIN
2f356 54 2c 20 20 54 4b 5f 49 4e 54 45 52 53 45 43 54  T,  TK_INTERSECT
2f357 2c 20 20 0a 20 20 20 20 54 4b 5f 54 52 49 47 47  ,  .    TK_TRIGG
2f358 45 52 2c 20 20 20 20 54 4b 5f 52 45 46 45 52 45  ER,    TK_REFERE
2f359 4e 43 45 53 2c 20 54 4b 5f 43 4f 4e 53 54 52 41  NCES, TK_CONSTRA
2f35a 49 4e 54 2c 20 54 4b 5f 49 4e 54 4f 2c 20 20 20  INT, TK_INTO,   
2f35b 20 20 20 20 54 4b 5f 4f 46 46 53 45 54 2c 20 20      TK_OFFSET,  
2f35c 20 20 20 0a 20 20 20 20 54 4b 5f 4f 46 2c 20 20     .    TK_OF,  
2f35d 20 20 20 20 20 20 20 54 4b 5f 53 45 54 2c 20 20         TK_SET,  
2f35e 20 20 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20        TK_TEMP,  
2f35f 20 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20       TK_TEMP,   
2f360 20 20 20 20 54 4b 5f 4f 52 2c 20 20 20 20 20 20      TK_OR,      
2f361 20 20 20 0a 20 20 20 20 54 4b 5f 55 4e 49 51 55     .    TK_UNIQU
2f362 45 2c 20 20 20 20 20 54 4b 5f 51 55 45 52 59 2c  E,     TK_QUERY,
2f363 20 20 20 20 20 20 54 4b 5f 41 54 54 41 43 48 2c        TK_ATTACH,
2f364 20 20 20 20 20 54 4b 5f 48 41 56 49 4e 47 2c 20       TK_HAVING, 
2f365 20 20 20 20 54 4b 5f 47 52 4f 55 50 2c 20 20 20      TK_GROUP,   
2f366 20 20 20 0a 20 20 20 20 54 4b 5f 55 50 44 41 54     .    TK_UPDAT
2f367 45 2c 20 20 20 20 20 54 4b 5f 42 45 47 49 4e 2c  E,     TK_BEGIN,
2f368 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57        TK_JOIN_KW
2f369 2c 20 20 20 20 54 4b 5f 52 45 4c 45 41 53 45 2c  ,    TK_RELEASE,
2f36a 20 20 20 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20      TK_BETWEEN, 
2f36b 20 20 20 0a 20 20 20 20 54 4b 5f 4e 4f 54 4e 55     .    TK_NOTNU
2f36c 4c 4c 2c 20 20 20 20 54 4b 5f 4e 4f 54 2c 20 20  LL,    TK_NOT,  
2f36d 20 20 20 20 20 20 54 4b 5f 4e 55 4c 4c 2c 20 20        TK_NULL,  
2f36e 20 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c       TK_LIKE_KW,
2f36f 20 20 20 20 54 4b 5f 43 41 53 43 41 44 45 2c 20      TK_CASCADE, 
2f370 20 20 20 0a 20 20 20 20 54 4b 5f 41 53 43 2c 20     .    TK_ASC, 
2f371 20 20 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45         TK_DELETE
2f372 2c 20 20 20 20 20 54 4b 5f 43 41 53 45 2c 20 20  ,     TK_CASE,  
2f373 20 20 20 20 20 54 4b 5f 43 4f 4c 4c 41 54 45 2c       TK_COLLATE,
2f374 20 20 20 20 54 4b 5f 43 52 45 41 54 45 2c 20 20      TK_CREATE,  
2f375 20 20 20 0a 20 20 20 20 54 4b 5f 43 54 49 4d 45     .    TK_CTIME
2f376 5f 4b 57 2c 20 20 20 54 4b 5f 44 45 54 41 43 48  _KW,   TK_DETACH
2f377 2c 20 20 20 20 20 54 4b 5f 49 4d 4d 45 44 49 41  ,     TK_IMMEDIA
2f378 54 45 2c 20 20 54 4b 5f 4a 4f 49 4e 2c 20 20 20  TE,  TK_JOIN,   
2f379 20 20 20 20 54 4b 5f 49 4e 53 45 52 54 2c 20 20      TK_INSERT,  
2f37a 20 20 20 0a 20 20 20 20 54 4b 5f 4d 41 54 43 48     .    TK_MATCH
2f37b 2c 20 20 20 20 20 20 54 4b 5f 50 4c 41 4e 2c 20  ,      TK_PLAN, 
2f37c 20 20 20 20 20 20 54 4b 5f 41 4e 41 4c 59 5a 45        TK_ANALYZE
2f37d 2c 20 20 20 20 54 4b 5f 50 52 41 47 4d 41 2c 20  ,    TK_PRAGMA, 
2f37e 20 20 20 20 54 4b 5f 41 42 4f 52 54 2c 20 20 20      TK_ABORT,   
2f37f 20 20 20 0a 20 20 20 20 54 4b 5f 56 41 4c 55 45     .    TK_VALUE
2f380 53 2c 20 20 20 20 20 54 4b 5f 56 49 52 54 55 41  S,     TK_VIRTUA
2f381 4c 2c 20 20 20 20 54 4b 5f 4c 49 4d 49 54 2c 20  L,    TK_LIMIT, 
2f382 20 20 20 20 20 54 4b 5f 57 48 45 4e 2c 20 20 20       TK_WHEN,   
2f383 20 20 20 20 54 4b 5f 57 48 45 52 45 2c 20 20 20      TK_WHERE,   
2f384 20 20 20 0a 20 20 20 20 54 4b 5f 52 45 4e 41 4d     .    TK_RENAM
2f385 45 2c 20 20 20 20 20 54 4b 5f 41 46 54 45 52 2c  E,     TK_AFTER,
2f386 20 20 20 20 20 20 54 4b 5f 52 45 50 4c 41 43 45        TK_REPLACE
2f387 2c 20 20 20 20 54 4b 5f 41 4e 44 2c 20 20 20 20  ,    TK_AND,    
2f388 20 20 20 20 54 4b 5f 44 45 46 41 55 4c 54 2c 20      TK_DEFAULT, 
2f389 20 20 20 0a 20 20 20 20 54 4b 5f 41 55 54 4f 49     .    TK_AUTOI
2f38a 4e 43 52 2c 20 20 20 54 4b 5f 54 4f 2c 20 20 20  NCR,   TK_TO,   
2f38b 20 20 20 20 20 20 54 4b 5f 49 4e 2c 20 20 20 20        TK_IN,    
2f38c 20 20 20 20 20 54 4b 5f 43 41 53 54 2c 20 20 20       TK_CAST,   
2f38d 20 20 20 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c      TK_COLUMNKW,
2f38e 20 20 20 0a 20 20 20 20 54 4b 5f 43 4f 4d 4d 49     .    TK_COMMI
2f38f 54 2c 20 20 20 20 20 54 4b 5f 43 4f 4e 46 4c 49  T,     TK_CONFLI
2f390 43 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57  CT,   TK_JOIN_KW
2f391 2c 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57  ,    TK_CTIME_KW
2f392 2c 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c  ,   TK_CTIME_KW,
2f393 20 20 20 0a 20 20 20 20 54 4b 5f 50 52 49 4d 41     .    TK_PRIMA
2f394 52 59 2c 20 20 20 20 54 4b 5f 44 45 46 45 52 52  RY,    TK_DEFERR
2f395 45 44 2c 20 20 20 54 4b 5f 44 49 53 54 49 4e 43  ED,   TK_DISTINC
2f396 54 2c 20 20 20 54 4b 5f 49 53 2c 20 20 20 20 20  T,   TK_IS,     
2f397 20 20 20 20 54 4b 5f 44 52 4f 50 2c 20 20 20 20      TK_DROP,    
2f398 20 20 20 0a 20 20 20 20 54 4b 5f 46 41 49 4c 2c     .    TK_FAIL,
2f399 20 20 20 20 20 20 20 54 4b 5f 46 52 4f 4d 2c 20         TK_FROM, 
2f39a 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57        TK_JOIN_KW
2f39b 2c 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c  ,    TK_LIKE_KW,
2f39c 20 20 20 20 54 4b 5f 42 59 2c 20 20 20 20 20 20      TK_BY,      
2f39d 20 20 20 0a 20 20 20 20 54 4b 5f 49 46 2c 20 20     .    TK_IF,  
2f39e 20 20 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c         TK_ISNULL
2f39f 2c 20 20 20 20 20 54 4b 5f 4f 52 44 45 52 2c 20  ,     TK_ORDER, 
2f3a0 20 20 20 20 20 54 4b 5f 52 45 53 54 52 49 43 54       TK_RESTRICT
2f3a1 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20  ,   TK_JOIN_KW, 
2f3a2 20 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f     .    TK_JOIN_
2f3a3 4b 57 2c 20 20 20 20 54 4b 5f 52 4f 4c 4c 42 41  KW,    TK_ROLLBA
2f3a4 43 4b 2c 20 20 20 54 4b 5f 52 4f 57 2c 20 20 20  CK,   TK_ROW,   
2f3a5 20 20 20 20 20 54 4b 5f 55 4e 49 4f 4e 2c 20 20       TK_UNION,  
2f3a6 20 20 20 20 54 4b 5f 55 53 49 4e 47 2c 20 20 20      TK_USING,   
2f3a7 20 20 20 0a 20 20 20 20 54 4b 5f 56 41 43 55 55     .    TK_VACUU
2f3a8 4d 2c 20 20 20 20 20 54 4b 5f 56 49 45 57 2c 20  M,     TK_VIEW, 
2f3a9 20 20 20 20 20 20 54 4b 5f 49 4e 49 54 49 41 4c        TK_INITIAL
2f3aa 4c 59 2c 20 20 54 4b 5f 41 4c 4c 2c 20 20 20 20  LY,  TK_ALL,    
2f3ab 20 20 20 20 0a 20 20 7d 3b 0a 20 20 69 6e 74 20      .  };.  int 
2f3ac 68 2c 20 69 3b 0a 20 20 69 66 28 20 6e 3c 32 20  h, i;.  if( n<2 
2f3ad 29 20 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a  ) return TK_ID;.
2f3ae 20 20 68 20 3d 20 28 28 63 68 61 72 4d 61 70 28    h = ((charMap(
2f3af 7a 5b 30 5d 29 2a 34 29 20 5e 0a 20 20 20 20 20  z[0])*4) ^.     
2f3b0 20 28 63 68 61 72 4d 61 70 28 7a 5b 6e 2d 31 5d   (charMap(z[n-1]
2f3b1 29 2a 33 29 20 5e 0a 20 20 20 20 20 20 6e 29 20  )*3) ^.      n) 
2f3b2 25 20 31 32 37 3b 0a 20 20 66 6f 72 28 69 3d 28  % 127;.  for(i=(
2f3b3 28 69 6e 74 29 61 48 61 73 68 5b 68 5d 29 2d 31  (int)aHash[h])-1
2f3b4 3b 20 69 3e 3d 30 3b 20 69 3d 28 28 69 6e 74 29  ; i>=0; i=((int)
2f3b5 61 4e 65 78 74 5b 69 5d 29 2d 31 29 7b 0a 20 20  aNext[i])-1){.  
2f3b6 20 20 69 66 28 20 61 4c 65 6e 5b 69 5d 3d 3d 6e    if( aLen[i]==n
2f3b7 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
2f3b8 43 6d 70 28 26 7a 54 65 78 74 5b 61 4f 66 66 73  Cmp(&zText[aOffs
2f3b9 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29  et[i]],z,n)==0 )
2f3ba 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
2f3bb 28 20 69 3d 3d 30 20 29 3b 20 2f 2a 20 54 4b 5f  ( i==0 ); /* TK_
2f3bc 52 45 49 4e 44 45 58 20 2a 2f 0a 20 20 20 20 20  REINDEX */.     
2f3bd 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20   testcase( i==1 
2f3be 29 3b 20 2f 2a 20 54 4b 5f 49 4e 44 45 58 45 44  ); /* TK_INDEXED
2f3bf 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f3c0 73 65 28 20 69 3d 3d 32 20 29 3b 20 2f 2a 20 54  se( i==2 ); /* T
2f3c1 4b 5f 49 4e 44 45 58 20 2a 2f 0a 20 20 20 20 20  K_INDEX */.     
2f3c2 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 20   testcase( i==3 
2f3c3 29 3b 20 2f 2a 20 54 4b 5f 44 45 53 43 20 2a 2f  ); /* TK_DESC */
2f3c4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f3c5 20 69 3d 3d 34 20 29 3b 20 2f 2a 20 54 4b 5f 45   i==4 ); /* TK_E
2f3c6 53 43 41 50 45 20 2a 2f 0a 20 20 20 20 20 20 74  SCAPE */.      t
2f3c7 65 73 74 63 61 73 65 28 20 69 3d 3d 35 20 29 3b  estcase( i==5 );
2f3c8 20 2f 2a 20 54 4b 5f 45 41 43 48 20 2a 2f 0a 20   /* TK_EACH */. 
2f3c9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f3ca 3d 3d 36 20 29 3b 20 2f 2a 20 54 4b 5f 43 48 45  ==6 ); /* TK_CHE
2f3cb 43 4b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CK */.      test
2f3cc 63 61 73 65 28 20 69 3d 3d 37 20 29 3b 20 2f 2a  case( i==7 ); /*
2f3cd 20 54 4b 5f 4b 45 59 20 2a 2f 0a 20 20 20 20 20   TK_KEY */.     
2f3ce 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 20   testcase( i==8 
2f3cf 29 3b 20 2f 2a 20 54 4b 5f 42 45 46 4f 52 45 20  ); /* TK_BEFORE 
2f3d0 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f3d1 65 28 20 69 3d 3d 39 20 29 3b 20 2f 2a 20 54 4b  e( i==9 ); /* TK
2f3d2 5f 46 4f 52 45 49 47 4e 20 2a 2f 0a 20 20 20 20  _FOREIGN */.    
2f3d3 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
2f3d4 30 20 29 3b 20 2f 2a 20 54 4b 5f 46 4f 52 20 2a  0 ); /* TK_FOR *
2f3d5 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f3d6 28 20 69 3d 3d 31 31 20 29 3b 20 2f 2a 20 54 4b  ( i==11 ); /* TK
2f3d7 5f 49 47 4e 4f 52 45 20 2a 2f 0a 20 20 20 20 20  _IGNORE */.     
2f3d8 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 32   testcase( i==12
2f3d9 20 29 3b 20 2f 2a 20 54 4b 5f 4c 49 4b 45 5f 4b   ); /* TK_LIKE_K
2f3da 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  W */.      testc
2f3db 61 73 65 28 20 69 3d 3d 31 33 20 29 3b 20 2f 2a  ase( i==13 ); /*
2f3dc 20 54 4b 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20   TK_EXPLAIN */. 
2f3dd 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f3de 3d 3d 31 34 20 29 3b 20 2f 2a 20 54 4b 5f 49 4e  ==14 ); /* TK_IN
2f3df 53 54 45 41 44 20 2a 2f 0a 20 20 20 20 20 20 74  STEAD */.      t
2f3e0 65 73 74 63 61 73 65 28 20 69 3d 3d 31 35 20 29  estcase( i==15 )
2f3e1 3b 20 2f 2a 20 54 4b 5f 41 44 44 20 2a 2f 0a 20  ; /* TK_ADD */. 
2f3e2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f3e3 3d 3d 31 36 20 29 3b 20 2f 2a 20 54 4b 5f 44 41  ==16 ); /* TK_DA
2f3e4 54 41 42 41 53 45 20 2a 2f 0a 20 20 20 20 20 20  TABASE */.      
2f3e5 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 37 20  testcase( i==17 
2f3e6 29 3b 20 2f 2a 20 54 4b 5f 41 53 20 2a 2f 0a 20  ); /* TK_AS */. 
2f3e7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f3e8 3d 3d 31 38 20 29 3b 20 2f 2a 20 54 4b 5f 53 45  ==18 ); /* TK_SE
2f3e9 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  LECT */.      te
2f3ea 73 74 63 61 73 65 28 20 69 3d 3d 31 39 20 29 3b  stcase( i==19 );
2f3eb 20 2f 2a 20 54 4b 5f 54 41 42 4c 45 20 2a 2f 0a   /* TK_TABLE */.
2f3ec 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f3ed 69 3d 3d 32 30 20 29 3b 20 2f 2a 20 54 4b 5f 4a  i==20 ); /* TK_J
2f3ee 4f 49 4e 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  OIN_KW */.      
2f3ef 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 31 20  testcase( i==21 
2f3f0 29 3b 20 2f 2a 20 54 4b 5f 54 48 45 4e 20 2a 2f  ); /* TK_THEN */
2f3f1 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f3f2 20 69 3d 3d 32 32 20 29 3b 20 2f 2a 20 54 4b 5f   i==22 ); /* TK_
2f3f3 45 4e 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  END */.      tes
2f3f4 74 63 61 73 65 28 20 69 3d 3d 32 33 20 29 3b 20  tcase( i==23 ); 
2f3f5 2f 2a 20 54 4b 5f 44 45 46 45 52 52 41 42 4c 45  /* TK_DEFERRABLE
2f3f6 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f3f7 73 65 28 20 69 3d 3d 32 34 20 29 3b 20 2f 2a 20  se( i==24 ); /* 
2f3f8 54 4b 5f 45 4c 53 45 20 2a 2f 0a 20 20 20 20 20  TK_ELSE */.     
2f3f9 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 35   testcase( i==25
2f3fa 20 29 3b 20 2f 2a 20 54 4b 5f 45 58 43 45 50 54   ); /* TK_EXCEPT
2f3fb 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f3fc 73 65 28 20 69 3d 3d 32 36 20 29 3b 20 2f 2a 20  se( i==26 ); /* 
2f3fd 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 2a  TK_TRANSACTION *
2f3fe 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f3ff 28 20 69 3d 3d 32 37 20 29 3b 20 2f 2a 20 54 4b  ( i==27 ); /* TK
2f400 5f 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  _ON */.      tes
2f401 74 63 61 73 65 28 20 69 3d 3d 32 38 20 29 3b 20  tcase( i==28 ); 
2f402 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f  /* TK_JOIN_KW */
2f403 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f404 20 69 3d 3d 32 39 20 29 3b 20 2f 2a 20 54 4b 5f   i==29 ); /* TK_
2f405 41 4c 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74  ALTER */.      t
2f406 65 73 74 63 61 73 65 28 20 69 3d 3d 33 30 20 29  estcase( i==30 )
2f407 3b 20 2f 2a 20 54 4b 5f 52 41 49 53 45 20 2a 2f  ; /* TK_RAISE */
2f408 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f409 20 69 3d 3d 33 31 20 29 3b 20 2f 2a 20 54 4b 5f   i==31 ); /* TK_
2f40a 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 20  EXCLUSIVE */.   
2f40b 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f40c 33 32 20 29 3b 20 2f 2a 20 54 4b 5f 45 58 49 53  32 ); /* TK_EXIS
2f40d 54 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  TS */.      test
2f40e 63 61 73 65 28 20 69 3d 3d 33 33 20 29 3b 20 2f  case( i==33 ); /
2f40f 2a 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54 20 2a  * TK_SAVEPOINT *
2f410 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f411 28 20 69 3d 3d 33 34 20 29 3b 20 2f 2a 20 54 4b  ( i==34 ); /* TK
2f412 5f 49 4e 54 45 52 53 45 43 54 20 2a 2f 0a 20 20  _INTERSECT */.  
2f413 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f414 3d 33 35 20 29 3b 20 2f 2a 20 54 4b 5f 54 52 49  =35 ); /* TK_TRI
2f415 47 47 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  GGER */.      te
2f416 73 74 63 61 73 65 28 20 69 3d 3d 33 36 20 29 3b  stcase( i==36 );
2f417 20 2f 2a 20 54 4b 5f 52 45 46 45 52 45 4e 43 45   /* TK_REFERENCE
2f418 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  S */.      testc
2f419 61 73 65 28 20 69 3d 3d 33 37 20 29 3b 20 2f 2a  ase( i==37 ); /*
2f41a 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 2a   TK_CONSTRAINT *
2f41b 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f41c 28 20 69 3d 3d 33 38 20 29 3b 20 2f 2a 20 54 4b  ( i==38 ); /* TK
2f41d 5f 49 4e 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74  _INTO */.      t
2f41e 65 73 74 63 61 73 65 28 20 69 3d 3d 33 39 20 29  estcase( i==39 )
2f41f 3b 20 2f 2a 20 54 4b 5f 4f 46 46 53 45 54 20 2a  ; /* TK_OFFSET *
2f420 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f421 28 20 69 3d 3d 34 30 20 29 3b 20 2f 2a 20 54 4b  ( i==40 ); /* TK
2f422 5f 4f 46 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  _OF */.      tes
2f423 74 63 61 73 65 28 20 69 3d 3d 34 31 20 29 3b 20  tcase( i==41 ); 
2f424 2f 2a 20 54 4b 5f 53 45 54 20 2a 2f 0a 20 20 20  /* TK_SET */.   
2f425 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f426 34 32 20 29 3b 20 2f 2a 20 54 4b 5f 54 45 4d 50  42 ); /* TK_TEMP
2f427 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f428 73 65 28 20 69 3d 3d 34 33 20 29 3b 20 2f 2a 20  se( i==43 ); /* 
2f429 54 4b 5f 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20  TK_TEMP */.     
2f42a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 34   testcase( i==44
2f42b 20 29 3b 20 2f 2a 20 54 4b 5f 4f 52 20 2a 2f 0a   ); /* TK_OR */.
2f42c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f42d 69 3d 3d 34 35 20 29 3b 20 2f 2a 20 54 4b 5f 55  i==45 ); /* TK_U
2f42e 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20 20 20 74  NIQUE */.      t
2f42f 65 73 74 63 61 73 65 28 20 69 3d 3d 34 36 20 29  estcase( i==46 )
2f430 3b 20 2f 2a 20 54 4b 5f 51 55 45 52 59 20 2a 2f  ; /* TK_QUERY */
2f431 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f432 20 69 3d 3d 34 37 20 29 3b 20 2f 2a 20 54 4b 5f   i==47 ); /* TK_
2f433 41 54 54 41 43 48 20 2a 2f 0a 20 20 20 20 20 20  ATTACH */.      
2f434 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 38 20  testcase( i==48 
2f435 29 3b 20 2f 2a 20 54 4b 5f 48 41 56 49 4e 47 20  ); /* TK_HAVING 
2f436 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f437 65 28 20 69 3d 3d 34 39 20 29 3b 20 2f 2a 20 54  e( i==49 ); /* T
2f438 4b 5f 47 52 4f 55 50 20 2a 2f 0a 20 20 20 20 20  K_GROUP */.     
2f439 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 30   testcase( i==50
2f43a 20 29 3b 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45   ); /* TK_UPDATE
2f43b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f43c 73 65 28 20 69 3d 3d 35 31 20 29 3b 20 2f 2a 20  se( i==51 ); /* 
2f43d 54 4b 5f 42 45 47 49 4e 20 2a 2f 0a 20 20 20 20  TK_BEGIN */.    
2f43e 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35    testcase( i==5
2f43f 32 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f  2 ); /* TK_JOIN_
2f440 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  KW */.      test
2f441 63 61 73 65 28 20 69 3d 3d 35 33 20 29 3b 20 2f  case( i==53 ); /
2f442 2a 20 54 4b 5f 52 45 4c 45 41 53 45 20 2a 2f 0a  * TK_RELEASE */.
2f443 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f444 69 3d 3d 35 34 20 29 3b 20 2f 2a 20 54 4b 5f 42  i==54 ); /* TK_B
2f445 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20  ETWEEN */.      
2f446 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 35 20  testcase( i==55 
2f447 29 3b 20 2f 2a 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  ); /* TK_NOTNULL
2f448 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f449 73 65 28 20 69 3d 3d 35 36 20 29 3b 20 2f 2a 20  se( i==56 ); /* 
2f44a 54 4b 5f 4e 4f 54 20 2a 2f 0a 20 20 20 20 20 20  TK_NOT */.      
2f44b 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 37 20  testcase( i==57 
2f44c 29 3b 20 2f 2a 20 54 4b 5f 4e 55 4c 4c 20 2a 2f  ); /* TK_NULL */
2f44d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f44e 20 69 3d 3d 35 38 20 29 3b 20 2f 2a 20 54 4b 5f   i==58 ); /* TK_
2f44f 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20  LIKE_KW */.     
2f450 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 39   testcase( i==59
2f451 20 29 3b 20 2f 2a 20 54 4b 5f 43 41 53 43 41 44   ); /* TK_CASCAD
2f452 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
2f453 61 73 65 28 20 69 3d 3d 36 30 20 29 3b 20 2f 2a  ase( i==60 ); /*
2f454 20 54 4b 5f 41 53 43 20 2a 2f 0a 20 20 20 20 20   TK_ASC */.     
2f455 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 31   testcase( i==61
2f456 20 29 3b 20 2f 2a 20 54 4b 5f 44 45 4c 45 54 45   ); /* TK_DELETE
2f457 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f458 73 65 28 20 69 3d 3d 36 32 20 29 3b 20 2f 2a 20  se( i==62 ); /* 
2f459 54 4b 5f 43 41 53 45 20 2a 2f 0a 20 20 20 20 20  TK_CASE */.     
2f45a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 33   testcase( i==63
2f45b 20 29 3b 20 2f 2a 20 54 4b 5f 43 4f 4c 4c 41 54   ); /* TK_COLLAT
2f45c 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
2f45d 61 73 65 28 20 69 3d 3d 36 34 20 29 3b 20 2f 2a  ase( i==64 ); /*
2f45e 20 54 4b 5f 43 52 45 41 54 45 20 2a 2f 0a 20 20   TK_CREATE */.  
2f45f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f460 3d 36 35 20 29 3b 20 2f 2a 20 54 4b 5f 43 54 49  =65 ); /* TK_CTI
2f461 4d 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74  ME_KW */.      t
2f462 65 73 74 63 61 73 65 28 20 69 3d 3d 36 36 20 29  estcase( i==66 )
2f463 3b 20 2f 2a 20 54 4b 5f 44 45 54 41 43 48 20 2a  ; /* TK_DETACH *
2f464 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f465 28 20 69 3d 3d 36 37 20 29 3b 20 2f 2a 20 54 4b  ( i==67 ); /* TK
2f466 5f 49 4d 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20  _IMMEDIATE */.  
2f467 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f468 3d 36 38 20 29 3b 20 2f 2a 20 54 4b 5f 4a 4f 49  =68 ); /* TK_JOI
2f469 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  N */.      testc
2f46a 61 73 65 28 20 69 3d 3d 36 39 20 29 3b 20 2f 2a  ase( i==69 ); /*
2f46b 20 54 4b 5f 49 4e 53 45 52 54 20 2a 2f 0a 20 20   TK_INSERT */.  
2f46c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f46d 3d 37 30 20 29 3b 20 2f 2a 20 54 4b 5f 4d 41 54  =70 ); /* TK_MAT
2f46e 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CH */.      test
2f46f 63 61 73 65 28 20 69 3d 3d 37 31 20 29 3b 20 2f  case( i==71 ); /
2f470 2a 20 54 4b 5f 50 4c 41 4e 20 2a 2f 0a 20 20 20  * TK_PLAN */.   
2f471 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f472 37 32 20 29 3b 20 2f 2a 20 54 4b 5f 41 4e 41 4c  72 ); /* TK_ANAL
2f473 59 5a 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  YZE */.      tes
2f474 74 63 61 73 65 28 20 69 3d 3d 37 33 20 29 3b 20  tcase( i==73 ); 
2f475 2f 2a 20 54 4b 5f 50 52 41 47 4d 41 20 2a 2f 0a  /* TK_PRAGMA */.
2f476 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f477 69 3d 3d 37 34 20 29 3b 20 2f 2a 20 54 4b 5f 41  i==74 ); /* TK_A
2f478 42 4f 52 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  BORT */.      te
2f479 73 74 63 61 73 65 28 20 69 3d 3d 37 35 20 29 3b  stcase( i==75 );
2f47a 20 2f 2a 20 54 4b 5f 56 41 4c 55 45 53 20 2a 2f   /* TK_VALUES */
2f47b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f47c 20 69 3d 3d 37 36 20 29 3b 20 2f 2a 20 54 4b 5f   i==76 ); /* TK_
2f47d 56 49 52 54 55 41 4c 20 2a 2f 0a 20 20 20 20 20  VIRTUAL */.     
2f47e 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 37   testcase( i==77
2f47f 20 29 3b 20 2f 2a 20 54 4b 5f 4c 49 4d 49 54 20   ); /* TK_LIMIT 
2f480 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f481 65 28 20 69 3d 3d 37 38 20 29 3b 20 2f 2a 20 54  e( i==78 ); /* T
2f482 4b 5f 57 48 45 4e 20 2a 2f 0a 20 20 20 20 20 20  K_WHEN */.      
2f483 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 39 20  testcase( i==79 
2f484 29 3b 20 2f 2a 20 54 4b 5f 57 48 45 52 45 20 2a  ); /* TK_WHERE *
2f485 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f486 28 20 69 3d 3d 38 30 20 29 3b 20 2f 2a 20 54 4b  ( i==80 ); /* TK
2f487 5f 52 45 4e 41 4d 45 20 2a 2f 0a 20 20 20 20 20  _RENAME */.     
2f488 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 31   testcase( i==81
2f489 20 29 3b 20 2f 2a 20 54 4b 5f 41 46 54 45 52 20   ); /* TK_AFTER 
2f48a 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f48b 65 28 20 69 3d 3d 38 32 20 29 3b 20 2f 2a 20 54  e( i==82 ); /* T
2f48c 4b 5f 52 45 50 4c 41 43 45 20 2a 2f 0a 20 20 20  K_REPLACE */.   
2f48d 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f48e 38 33 20 29 3b 20 2f 2a 20 54 4b 5f 41 4e 44 20  83 ); /* TK_AND 
2f48f 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2f490 65 28 20 69 3d 3d 38 34 20 29 3b 20 2f 2a 20 54  e( i==84 ); /* T
2f491 4b 5f 44 45 46 41 55 4c 54 20 2a 2f 0a 20 20 20  K_DEFAULT */.   
2f492 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f493 38 35 20 29 3b 20 2f 2a 20 54 4b 5f 41 55 54 4f  85 ); /* TK_AUTO
2f494 49 4e 43 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  INCR */.      te
2f495 73 74 63 61 73 65 28 20 69 3d 3d 38 36 20 29 3b  stcase( i==86 );
2f496 20 2f 2a 20 54 4b 5f 54 4f 20 2a 2f 0a 20 20 20   /* TK_TO */.   
2f497 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f498 38 37 20 29 3b 20 2f 2a 20 54 4b 5f 49 4e 20 2a  87 ); /* TK_IN *
2f499 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f49a 28 20 69 3d 3d 38 38 20 29 3b 20 2f 2a 20 54 4b  ( i==88 ); /* TK
2f49b 5f 43 41 53 54 20 2a 2f 0a 20 20 20 20 20 20 74  _CAST */.      t
2f49c 65 73 74 63 61 73 65 28 20 69 3d 3d 38 39 20 29  estcase( i==89 )
2f49d 3b 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57  ; /* TK_COLUMNKW
2f49e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f49f 73 65 28 20 69 3d 3d 39 30 20 29 3b 20 2f 2a 20  se( i==90 ); /* 
2f4a0 54 4b 5f 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 20  TK_COMMIT */.   
2f4a1 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4a2 39 31 20 29 3b 20 2f 2a 20 54 4b 5f 43 4f 4e 46  91 ); /* TK_CONF
2f4a3 4c 49 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  LICT */.      te
2f4a4 73 74 63 61 73 65 28 20 69 3d 3d 39 32 20 29 3b  stcase( i==92 );
2f4a5 20 2f 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a   /* TK_JOIN_KW *
2f4a6 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4a7 28 20 69 3d 3d 39 33 20 29 3b 20 2f 2a 20 54 4b  ( i==93 ); /* TK
2f4a8 5f 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 20 20 20  _CTIME_KW */.   
2f4a9 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4aa 39 34 20 29 3b 20 2f 2a 20 54 4b 5f 43 54 49 4d  94 ); /* TK_CTIM
2f4ab 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 74 65  E_KW */.      te
2f4ac 73 74 63 61 73 65 28 20 69 3d 3d 39 35 20 29 3b  stcase( i==95 );
2f4ad 20 2f 2a 20 54 4b 5f 50 52 49 4d 41 52 59 20 2a   /* TK_PRIMARY *
2f4ae 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4af 28 20 69 3d 3d 39 36 20 29 3b 20 2f 2a 20 54 4b  ( i==96 ); /* TK
2f4b0 5f 44 45 46 45 52 52 45 44 20 2a 2f 0a 20 20 20  _DEFERRED */.   
2f4b1 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4b2 39 37 20 29 3b 20 2f 2a 20 54 4b 5f 44 49 53 54  97 ); /* TK_DIST
2f4b3 49 4e 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  INCT */.      te
2f4b4 73 74 63 61 73 65 28 20 69 3d 3d 39 38 20 29 3b  stcase( i==98 );
2f4b5 20 2f 2a 20 54 4b 5f 49 53 20 2a 2f 0a 20 20 20   /* TK_IS */.   
2f4b6 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
2f4b7 39 39 20 29 3b 20 2f 2a 20 54 4b 5f 44 52 4f 50  99 ); /* TK_DROP
2f4b8 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f4b9 73 65 28 20 69 3d 3d 31 30 30 20 29 3b 20 2f 2a  se( i==100 ); /*
2f4ba 20 54 4b 5f 46 41 49 4c 20 2a 2f 0a 20 20 20 20   TK_FAIL */.    
2f4bb 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
2f4bc 30 31 20 29 3b 20 2f 2a 20 54 4b 5f 46 52 4f 4d  01 ); /* TK_FROM
2f4bd 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
2f4be 73 65 28 20 69 3d 3d 31 30 32 20 29 3b 20 2f 2a  se( i==102 ); /*
2f4bf 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 20   TK_JOIN_KW */. 
2f4c0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f4c1 3d 3d 31 30 33 20 29 3b 20 2f 2a 20 54 4b 5f 4c  ==103 ); /* TK_L
2f4c2 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  IKE_KW */.      
2f4c3 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 34  testcase( i==104
2f4c4 20 29 3b 20 2f 2a 20 54 4b 5f 42 59 20 2a 2f 0a   ); /* TK_BY */.
2f4c5 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f4c6 69 3d 3d 31 30 35 20 29 3b 20 2f 2a 20 54 4b 5f  i==105 ); /* TK_
2f4c7 49 46 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IF */.      test
2f4c8 63 61 73 65 28 20 69 3d 3d 31 30 36 20 29 3b 20  case( i==106 ); 
2f4c9 2f 2a 20 54 4b 5f 49 53 4e 55 4c 4c 20 2a 2f 0a  /* TK_ISNULL */.
2f4ca 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f4cb 69 3d 3d 31 30 37 20 29 3b 20 2f 2a 20 54 4b 5f  i==107 ); /* TK_
2f4cc 4f 52 44 45 52 20 2a 2f 0a 20 20 20 20 20 20 74  ORDER */.      t
2f4cd 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 38 20  estcase( i==108 
2f4ce 29 3b 20 2f 2a 20 54 4b 5f 52 45 53 54 52 49 43  ); /* TK_RESTRIC
2f4cf 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
2f4d0 61 73 65 28 20 69 3d 3d 31 30 39 20 29 3b 20 2f  ase( i==109 ); /
2f4d1 2a 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a  * TK_JOIN_KW */.
2f4d2 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f4d3 69 3d 3d 31 31 30 20 29 3b 20 2f 2a 20 54 4b 5f  i==110 ); /* TK_
2f4d4 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 20 20 20 20 20  JOIN_KW */.     
2f4d5 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
2f4d6 31 20 29 3b 20 2f 2a 20 54 4b 5f 52 4f 4c 4c 42  1 ); /* TK_ROLLB
2f4d7 41 43 4b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ACK */.      tes
2f4d8 74 63 61 73 65 28 20 69 3d 3d 31 31 32 20 29 3b  tcase( i==112 );
2f4d9 20 2f 2a 20 54 4b 5f 52 4f 57 20 2a 2f 0a 20 20   /* TK_ROW */.  
2f4da 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
2f4db 3d 31 31 33 20 29 3b 20 2f 2a 20 54 4b 5f 55 4e  =113 ); /* TK_UN
2f4dc 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ION */.      tes
2f4dd 74 63 61 73 65 28 20 69 3d 3d 31 31 34 20 29 3b  tcase( i==114 );
2f4de 20 2f 2a 20 54 4b 5f 55 53 49 4e 47 20 2a 2f 0a   /* TK_USING */.
2f4df 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2f4e0 69 3d 3d 31 31 35 20 29 3b 20 2f 2a 20 54 4b 5f  i==115 ); /* TK_
2f4e1 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20 20 20 20  VACUUM */.      
2f4e2 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 36  testcase( i==116
2f4e3 20 29 3b 20 2f 2a 20 54 4b 5f 56 49 45 57 20 2a   ); /* TK_VIEW *
2f4e4 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
2f4e5 28 20 69 3d 3d 31 31 37 20 29 3b 20 2f 2a 20 54  ( i==117 ); /* T
2f4e6 4b 5f 49 4e 49 54 49 41 4c 4c 59 20 2a 2f 0a 20  K_INITIALLY */. 
2f4e7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2f4e8 3d 3d 31 31 38 20 29 3b 20 2f 2a 20 54 4b 5f 41  ==118 ); /* TK_A
2f4e9 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  LL */.      retu
2f4ea 72 6e 20 61 43 6f 64 65 5b 69 5d 3b 0a 20 20 20  rn aCode[i];.   
2f4eb 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2f4ec 54 4b 5f 49 44 3b 0a 7d 0a 53 51 4c 49 54 45 5f  TK_ID;.}.SQLITE_
2f4ed 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2f4ee 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63  te3KeywordCode(c
2f4ef 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2f4f0 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *z, int n){. 
2f4f1 20 72 65 74 75 72 6e 20 6b 65 79 77 6f 72 64 43   return keywordC
2f4f2 6f 64 65 28 28 63 68 61 72 2a 29 7a 2c 20 6e 29  ode((char*)z, n)
2f4f3 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
2f4f4 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6b 65 79 77  **** End of keyw
2f4f5 6f 72 64 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  ordhash.h ******
2f4f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4f8 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2f4f9 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
2f4fa 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
2f4fb 66 20 69 6e 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  f in tokenize.c 
2f4fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f4fd 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  ***/.../*.** If 
2f4fe 58 20 69 73 20 61 20 63 68 61 72 61 63 74 65 72  X is a character
2f4ff 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
2f500 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69 66 69  d in an identifi
2f501 65 72 20 74 68 65 6e 0a 2a 2a 20 49 64 43 68 61  er then.** IdCha
2f502 72 28 58 29 20 77 69 6c 6c 20 62 65 20 74 72 75  r(X) will be tru
2f503 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74  e.  Otherwise it
2f504 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a   is false..**.**
2f505 20 46 6f 72 20 41 53 43 49 49 2c 20 61 6e 79 20   For ASCII, any 
2f506 63 68 61 72 61 63 74 65 72 20 77 69 74 68 20 74  character with t
2f507 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69  he high-order bi
2f508 74 20 73 65 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f  t set is.** allo
2f509 77 65 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69  wed in an identi
2f50a 66 69 65 72 2e 20 20 46 6f 72 20 37 2d 62 69 74  fier.  For 7-bit
2f50b 20 63 68 61 72 61 63 74 65 72 73 2c 20 0a 2a 2a   characters, .**
2f50c 20 73 71 6c 69 74 65 33 49 73 49 64 43 68 61 72   sqlite3IsIdChar
2f50d 5b 58 5d 20 6d 75 73 74 20 62 65 20 31 2e 0a 2a  [X] must be 1..*
2f50e 2a 0a 2a 2a 20 46 6f 72 20 45 42 43 44 49 43 2c  *.** For EBCDIC,
2f50f 20 74 68 65 20 72 75 6c 65 73 20 61 72 65 20 6d   the rules are m
2f510 6f 72 65 20 63 6f 6d 70 6c 65 78 20 62 75 74 20  ore complex but 
2f511 68 61 76 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a  have the same.**
2f512 20 65 6e 64 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a   end result..**.
2f513 2a 2a 20 54 69 63 6b 65 74 20 23 31 30 36 36 2e  ** Ticket #1066.
2f514 20 20 74 68 65 20 53 51 4c 20 73 74 61 6e 64 61    the SQL standa
2f515 72 64 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f  rd does not allo
2f516 77 20 27 24 27 20 69 6e 20 74 68 65 0a 2a 2a 20  w '$' in the.** 
2f517 6d 69 64 64 6c 65 20 6f 66 20 69 64 65 6e 74 66  middle of identf
2f518 69 65 72 73 2e 20 20 42 75 74 20 6d 61 6e 79 20  iers.  But many 
2f519 53 51 4c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  SQL implementati
2f51a 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20 53 51 4c 69  ons do. .** SQLi
2f51b 74 65 20 77 69 6c 6c 20 61 6c 6c 6f 77 20 27 24  te will allow '$
2f51c 27 20 69 6e 20 69 64 65 6e 74 69 66 69 65 72 73  ' in identifiers
2f51d 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69   for compatibili
2f51e 74 79 2e 0a 2a 2a 20 42 75 74 20 74 68 65 20 66  ty..** But the f
2f51f 65 61 74 75 72 65 20 69 73 20 75 6e 64 6f 63 75  eature is undocu
2f520 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65  mented..*/.#ifde
2f521 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53  f SQLITE_ASCII.S
2f522 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
2f523 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33  nst char sqlite3
2f524 49 73 41 73 63 69 69 49 64 43 68 61 72 5b 5d 20  IsAsciiIdChar[] 
2f525 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
2f526 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
2f527 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
2f528 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c   xE xF */.    0,
2f529 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20   0, 0, 0, 1, 0, 
2f52a 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2f52b 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
2f52c 2a 20 32 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * 2x */.    1, 1
2f52d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f52e 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
2f52f 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
2f530 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  3x */.    0, 1, 
2f531 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f532 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f533 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78   1, 1, 1,  /* 4x
2f534 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
2f535 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f536 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
2f537 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a  , 0, 1,  /* 5x *
2f538 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
2f539 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f53a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f53b 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a  1, 1,  /* 6x */.
2f53c 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
2f53d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f53e 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
2f53f 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b   0,  /* 7x */.};
2f540 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
2f541 43 29 20 20 28 28 28 63 3d 43 29 26 30 78 38 30  C)  (((c=C)&0x80
2f542 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66 20  )!=0 || (c>0x1f 
2f543 26 26 20 73 71 6c 69 74 65 33 49 73 41 73 63 69  && sqlite3IsAsci
2f544 69 49 64 43 68 61 72 5b 63 2d 30 78 32 30 5d 29  iIdChar[c-0x20])
2f545 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ).#endif.#ifdef 
2f546 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 53 51  SQLITE_EBCDIC.SQ
2f547 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
2f548 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 49  st char sqlite3I
2f549 73 45 62 63 64 69 63 49 64 43 68 61 72 5b 5d 20  sEbcdicIdChar[] 
2f54a 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
2f54b 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
2f54c 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
2f54d 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c   xE xF */.    0,
2f54e 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
2f54f 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
2f550 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
2f551 2a 20 34 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 4x */.    0, 1
2f552 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f553 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
2f554 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
2f555 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  5x */.    0, 0, 
2f556 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f557 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
2f558 20 31 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 36 78   1, 0, 0,  /* 6x
2f559 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
2f55a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f55b 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
2f55c 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a  , 0, 0,  /* 7x *
2f55d 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
2f55e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f55f 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 0, 0, 1, 1, 
2f560 31 2c 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f 0a  1, 0,  /* 8x */.
2f561 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
2f562 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f563 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 31 2c  , 0, 0, 1, 0, 1,
2f564 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a 20 20   0,  /* 9x */.  
2f565 20 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c    1, 0, 1, 1, 1,
2f566 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f567 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 0, 1, 1, 1, 0
2f568 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20  ,  /* Ax */.    
2f569 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2f56a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2f56b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2f56c 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 30 2c   /* Bx */.    0,
2f56d 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f56e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
2f56f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
2f570 2a 20 43 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * Cx */.    0, 1
2f571 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2f572 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
2f573 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
2f574 44 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  Dx */.    0, 0, 
2f575 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2f576 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 1, 0, 1, 1,
2f577 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 45 78   1, 1, 1,  /* Ex
2f578 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
2f579 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2f57a 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
2f57b 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 46 78 20 2a  , 1, 0,  /* Fx *
2f57c 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43  /.};.#define IdC
2f57d 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29 3e  har(C)  (((c=C)>
2f57e 3d 30 78 34 32 20 26 26 20 73 71 6c 69 74 65 33  =0x42 && sqlite3
2f57f 49 73 45 62 63 64 69 63 49 64 43 68 61 72 5b 63  IsEbcdicIdChar[c
2f580 2d 30 78 34 30 5d 29 29 0a 23 65 6e 64 69 66 0a  -0x40])).#endif.
2f581 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
2f582 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
2f583 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69   token that begi
2f584 6e 73 20 61 74 20 7a 5b 30 5d 2e 20 0a 2a 2a 20  ns at z[0]. .** 
2f585 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20  Store the token 
2f586 74 79 70 65 20 69 6e 20 2a 74 6f 6b 65 6e 54 79  type in *tokenTy
2f587 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  pe before return
2f588 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
2f589 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2f58a 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74  e3GetToken(const
2f58b 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
2f58c 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e 54 79 70  z, int *tokenTyp
2f58d 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a  e){.  int i, c;.
2f58e 20 20 73 77 69 74 63 68 28 20 2a 7a 20 29 7b 0a    switch( *z ){.
2f58f 20 20 20 20 63 61 73 65 20 27 20 27 3a 20 63 61      case ' ': ca
2f590 73 65 20 27 5c 74 27 3a 20 63 61 73 65 20 27 5c  se '\t': case '\
2f591 6e 27 3a 20 63 61 73 65 20 27 5c 66 27 3a 20 63  n': case '\f': c
2f592 61 73 65 20 27 5c 72 27 3a 20 7b 0a 20 20 20 20  ase '\r': {.    
2f593 20 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74    for(i=1; sqlit
2f594 65 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b  e3Isspace(z[i]);
2f595 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74   i++){}.      *t
2f596 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50  okenType = TK_SP
2f597 41 43 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ACE;.      retur
2f598 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n i;.    }.    c
2f599 61 73 65 20 27 2d 27 3a 20 7b 0a 20 20 20 20 20  ase '-': {.     
2f59a 20 69 66 28 20 7a 5b 31 5d 3d 3d 27 2d 27 20 29   if( z[1]=='-' )
2f59b 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  {.        for(i=
2f59c 32 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26  2; (c=z[i])!=0 &
2f59d 26 20 63 21 3d 27 5c 6e 27 3b 20 69 2b 2b 29 7b  & c!='\n'; i++){
2f59e 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  }.        *token
2f59f 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b  Type = TK_SPACE;
2f5a0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2f5a1 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  i;.      }.     
2f5a2 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2f5a3 5f 4d 49 4e 55 53 3b 0a 20 20 20 20 20 20 72 65  _MINUS;.      re
2f5a4 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2f5a5 20 20 63 61 73 65 20 27 28 27 3a 20 7b 0a 20 20    case '(': {.  
2f5a6 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2f5a7 20 54 4b 5f 4c 50 3b 0a 20 20 20 20 20 20 72 65   TK_LP;.      re
2f5a8 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2f5a9 20 20 63 61 73 65 20 27 29 27 3a 20 7b 0a 20 20    case ')': {.  
2f5aa 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2f5ab 20 54 4b 5f 52 50 3b 0a 20 20 20 20 20 20 72 65   TK_RP;.      re
2f5ac 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2f5ad 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 0a 20 20    case ';': {.  
2f5ae 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2f5af 20 54 4b 5f 53 45 4d 49 3b 0a 20 20 20 20 20 20   TK_SEMI;.      
2f5b0 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
2f5b1 20 20 20 20 63 61 73 65 20 27 2b 27 3a 20 7b 0a      case '+': {.
2f5b2 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2f5b3 20 3d 20 54 4b 5f 50 4c 55 53 3b 0a 20 20 20 20   = TK_PLUS;.    
2f5b4 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2f5b5 7d 0a 20 20 20 20 63 61 73 65 20 27 2a 27 3a 20  }.    case '*': 
2f5b6 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
2f5b7 70 65 20 3d 20 54 4b 5f 53 54 41 52 3b 0a 20 20  pe = TK_STAR;.  
2f5b8 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2f5b9 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2f 27    }.    case '/'
2f5ba 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  : {.      if( z[
2f5bb 31 5d 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d 3d  1]!='*' || z[2]=
2f5bc 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  =0 ){.        *t
2f5bd 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 4c  okenType = TK_SL
2f5be 41 53 48 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ASH;.        ret
2f5bf 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
2f5c0 20 20 20 20 20 66 6f 72 28 69 3d 33 2c 20 63 3d       for(i=3, c=
2f5c1 7a 5b 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c 7c  z[2]; (c!='*' ||
2f5c2 20 7a 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20 28   z[i]!='/') && (
2f5c3 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
2f5c4 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20 29  {}.      if( c )
2f5c5 20 69 2b 2b 3b 0a 20 20 20 20 20 20 2a 74 6f 6b   i++;.      *tok
2f5c6 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43  enType = TK_SPAC
2f5c7 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  E;.      return 
2f5c8 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
2f5c9 65 20 27 25 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '%': {.      *
2f5ca 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52  tokenType = TK_R
2f5cb 45 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  EM;.      return
2f5cc 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
2f5cd 73 65 20 27 3d 27 3a 20 7b 0a 20 20 20 20 20 20  se '=': {.      
2f5ce 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2f5cf 45 51 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  EQ;.      return
2f5d0 20 31 20 2b 20 28 7a 5b 31 5d 3d 3d 27 3d 27 29   1 + (z[1]=='=')
2f5d1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
2f5d2 20 27 3c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66   '<': {.      if
2f5d3 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20  ( (c=z[1])=='=' 
2f5d4 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  ){.        *toke
2f5d5 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 45 3b 0a 20  nType = TK_LE;. 
2f5d6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
2f5d7 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
2f5d8 20 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20   c=='>' ){.     
2f5d9 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2f5da 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20 72  TK_NE;.        r
2f5db 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d  eturn 2;.      }
2f5dc 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3c 27 20  else if( c=='<' 
2f5dd 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  ){.        *toke
2f5de 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 53 48 49 46  nType = TK_LSHIF
2f5df 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  T;.        retur
2f5e0 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 2;.      }else
2f5e1 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
2f5e2 54 79 70 65 20 3d 20 54 4b 5f 4c 54 3b 0a 20 20  Type = TK_LT;.  
2f5e3 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2f5e4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2f5e5 20 20 63 61 73 65 20 27 3e 27 3a 20 7b 0a 20 20    case '>': {.  
2f5e6 20 20 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29      if( (c=z[1])
2f5e7 3d 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20  =='=' ){.       
2f5e8 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2f5e9 5f 47 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74  _GE;.        ret
2f5ea 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 2;.      }el
2f5eb 73 65 20 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b  se if( c=='>' ){
2f5ec 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
2f5ed 79 70 65 20 3d 20 54 4b 5f 52 53 48 49 46 54 3b  ype = TK_RSHIFT;
2f5ee 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2f5ef 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  2;.      }else{.
2f5f0 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2f5f1 70 65 20 3d 20 54 4b 5f 47 54 3b 0a 20 20 20 20  pe = TK_GT;.    
2f5f2 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2f5f3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2f5f4 63 61 73 65 20 27 21 27 3a 20 7b 0a 20 20 20 20  case '!': {.    
2f5f5 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 3d 27 20    if( z[1]!='=' 
2f5f6 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  ){.        *toke
2f5f7 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47  nType = TK_ILLEG
2f5f8 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  AL;.        retu
2f5f9 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 2;.      }els
2f5fa 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  e{.        *toke
2f5fb 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20  nType = TK_NE;. 
2f5fc 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
2f5fd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2f5fe 20 20 20 63 61 73 65 20 27 7c 27 3a 20 7b 0a 20     case '|': {. 
2f5ff 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27       if( z[1]!='
2f600 7c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  |' ){.        *t
2f601 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49  okenType = TK_BI
2f602 54 4f 52 3b 0a 20 20 20 20 20 20 20 20 72 65 74  TOR;.        ret
2f603 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c  urn 1;.      }el
2f604 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  se{.        *tok
2f605 65 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4e 43  enType = TK_CONC
2f606 41 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  AT;.        retu
2f607 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 2;.      }.  
2f608 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2c 27    }.    case ','
2f609 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
2f60a 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 41 3b  Type = TK_COMMA;
2f60b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2f60c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2f60d 27 26 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '&': {.      *to
2f60e 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54  kenType = TK_BIT
2f60f 41 4e 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72  AND;.      retur
2f610 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
2f611 61 73 65 20 27 7e 27 3a 20 7b 0a 20 20 20 20 20  ase '~': {.     
2f612 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2f613 5f 42 49 54 4e 4f 54 3b 0a 20 20 20 20 20 20 72  _BITNOT;.      r
2f614 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
2f615 20 20 20 63 61 73 65 20 27 60 27 3a 0a 20 20 20     case '`':.   
2f616 20 63 61 73 65 20 27 5c 27 27 3a 0a 20 20 20 20   case '\'':.    
2f617 63 61 73 65 20 27 22 27 3a 20 7b 0a 20 20 20 20  case '"': {.    
2f618 20 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b    int delim = z[
2f619 30 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  0];.      for(i=
2f61a 31 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20  1; (c=z[i])!=0; 
2f61b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  i++){.        if
2f61c 28 20 63 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20  ( c==delim ){.  
2f61d 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b          if( z[i+
2f61e 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20  1]==delim ){.   
2f61f 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20           i++;.  
2f620 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2f621 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2f622 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
2f623 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2f624 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 5c 27        if( c=='\'
2f625 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  ' ){.        *to
2f626 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 54 52  kenType = TK_STR
2f627 49 4e 47 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ING;.        ret
2f628 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d  urn i+1;.      }
2f629 65 6c 73 65 20 69 66 28 20 63 21 3d 30 20 29 7b  else if( c!=0 ){
2f62a 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
2f62b 79 70 65 20 3d 20 54 4b 5f 49 44 3b 0a 20 20 20  ype = TK_ID;.   
2f62c 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b       return i+1;
2f62d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2f62e 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2f62f 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
2f630 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b         return i;
2f631 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2f632 20 20 20 63 61 73 65 20 27 2e 27 3a 20 7b 0a 23     case '.': {.#
2f633 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2f634 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
2f635 54 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 6c  T.      if( !sql
2f636 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 31 5d  ite3Isdigit(z[1]
2f637 29 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ) ).#endif.     
2f638 20 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65   {.        *toke
2f639 6e 54 79 70 65 20 3d 20 54 4b 5f 44 4f 54 3b 0a  nType = TK_DOT;.
2f63a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
2f63b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2f63c 2f 2a 20 49 66 20 74 68 65 20 6e 65 78 74 20 63  /* If the next c
2f63d 68 61 72 61 63 74 65 72 20 69 73 20 61 20 64 69  haracter is a di
2f63e 67 69 74 2c 20 74 68 69 73 20 69 73 20 61 20 66  git, this is a f
2f63f 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 20 20  loating point.  
2f640 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 74 68      ** number th
2f641 61 74 20 62 65 67 69 6e 73 20 77 69 74 68 20 22  at begins with "
2f642 2e 22 2e 20 20 46 61 6c 6c 20 74 68 72 75 20 69  .".  Fall thru i
2f643 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
2f644 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63  e */.    }.    c
2f645 61 73 65 20 27 30 27 3a 20 63 61 73 65 20 27 31  ase '0': case '1
2f646 27 3a 20 63 61 73 65 20 27 32 27 3a 20 63 61 73  ': case '2': cas
2f647 65 20 27 33 27 3a 20 63 61 73 65 20 27 34 27 3a  e '3': case '4':
2f648 0a 20 20 20 20 63 61 73 65 20 27 35 27 3a 20 63  .    case '5': c
2f649 61 73 65 20 27 36 27 3a 20 63 61 73 65 20 27 37  ase '6': case '7
2f64a 27 3a 20 63 61 73 65 20 27 38 27 3a 20 63 61 73  ': case '8': cas
2f64b 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '9': {.      *
2f64c 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2f64d 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 66 6f  NTEGER;.      fo
2f64e 72 28 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73  r(i=0; sqlite3Is
2f64f 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b  digit(z[i]); i++
2f650 29 7b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ){}.#ifndef SQLI
2f651 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
2f652 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28  _POINT.      if(
2f653 20 7a 5b 69 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20   z[i]=='.' ){.  
2f654 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
2f655 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65     while( sqlite
2f656 33 49 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29  3Isdigit(z[i]) )
2f657 7b 20 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  { i++; }.       
2f658 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2f659 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a  _FLOAT;.      }.
2f65a 20 20 20 20 20 20 69 66 28 20 28 7a 5b 69 5d 3d        if( (z[i]=
2f65b 3d 27 65 27 20 7c 7c 20 7a 5b 69 5d 3d 3d 27 45  ='e' || z[i]=='E
2f65c 27 29 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  ') &&.          
2f65d 20 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69   ( sqlite3Isdigi
2f65e 74 28 7a 5b 69 2b 31 5d 29 20 0a 20 20 20 20 20  t(z[i+1]) .     
2f65f 20 20 20 20 20 20 20 7c 7c 20 28 28 7a 5b 69 2b         || ((z[i+
2f660 31 5d 3d 3d 27 2b 27 20 7c 7c 20 7a 5b 69 2b 31  1]=='+' || z[i+1
2f661 5d 3d 3d 27 2d 27 29 20 26 26 20 73 71 6c 69 74  ]=='-') && sqlit
2f662 65 33 49 73 64 69 67 69 74 28 7a 5b 69 2b 32 5d  e3Isdigit(z[i+2]
2f663 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 29 0a  )).           ).
2f664 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
2f665 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20   i += 2;.       
2f666 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
2f667 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20  sdigit(z[i]) ){ 
2f668 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a  i++; }.        *
2f669 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46  tokenType = TK_F
2f66a 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a 23 65  LOAT;.      }.#e
2f66b 6e 64 69 66 0a 20 20 20 20 20 20 77 68 69 6c 65  ndif.      while
2f66c 28 20 49 64 43 68 61 72 28 7a 5b 69 5d 29 20 29  ( IdChar(z[i]) )
2f66d 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
2f66e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
2f66f 4c 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a  L;.        i++;.
2f670 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
2f671 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
2f672 20 20 63 61 73 65 20 27 5b 27 3a 20 7b 0a 20 20    case '[': {.  
2f673 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 63 3d 7a      for(i=1, c=z
2f674 5b 30 5d 3b 20 63 21 3d 27 5d 27 20 26 26 20 28  [0]; c!=']' && (
2f675 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29  c=z[i])!=0; i++)
2f676 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
2f677 79 70 65 20 3d 20 63 3d 3d 27 5d 27 20 3f 20 54  ype = c==']' ? T
2f678 4b 5f 49 44 20 3a 20 54 4b 5f 49 4c 4c 45 47 41  K_ID : TK_ILLEGA
2f679 4c 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  L;.      return 
2f67a 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
2f67b 65 20 27 3f 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '?': {.      *
2f67c 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56  tokenType = TK_V
2f67d 41 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66  ARIABLE;.      f
2f67e 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49  or(i=1; sqlite3I
2f67f 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b  sdigit(z[i]); i+
2f680 2b 29 7b 7d 0a 20 20 20 20 20 20 72 65 74 75 72  +){}.      retur
2f681 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n i;.    }.    c
2f682 61 73 65 20 27 23 27 3a 20 7b 0a 20 20 20 20 20  ase '#': {.     
2f683 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65   for(i=1; sqlite
2f684 33 49 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20  3Isdigit(z[i]); 
2f685 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66 28  i++){}.      if(
2f686 20 69 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20   i>1 ){.        
2f687 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66  /* Parameters of
2f688 20 74 68 65 20 66 6f 72 6d 20 23 4e 4e 4e 20 28   the form #NNN (
2f689 77 68 65 72 65 20 4e 4e 4e 20 69 73 20 61 20 6e  where NNN is a n
2f68a 75 6d 62 65 72 29 20 61 72 65 20 75 73 65 64 0a  umber) are used.
2f68b 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 72          ** inter
2f68c 6e 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33  nally by sqlite3
2f68d 4e 65 73 74 65 64 50 61 72 73 65 2e 20 20 2a 2f  NestedParse.  */
2f68e 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
2f68f 79 70 65 20 3d 20 54 4b 5f 52 45 47 49 53 54 45  ype = TK_REGISTE
2f690 52 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  R;.        retur
2f691 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n i;.      }.   
2f692 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75     /* Fall throu
2f693 67 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74  gh into the next
2f694 20 63 61 73 65 20 69 66 20 74 68 65 20 27 23 27   case if the '#'
2f695 20 69 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77 65 64   is not followed
2f696 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 64   by.      ** a d
2f697 69 67 69 74 2e 20 54 72 79 20 74 6f 20 6d 61 74  igit. Try to mat
2f698 63 68 20 23 41 41 41 41 20 77 68 65 72 65 20 41  ch #AAAA where A
2f699 41 41 41 20 69 73 20 61 20 70 61 72 61 6d 65 74  AAA is a paramet
2f69a 65 72 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20  er name. */.    
2f69b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2f69c 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42  _OMIT_TCL_VARIAB
2f69d 4c 45 0a 20 20 20 20 63 61 73 65 20 27 24 27 3a  LE.    case '$':
2f69e 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
2f69f 20 27 40 27 3a 20 20 2f 2a 20 46 6f 72 20 63 6f   '@':  /* For co
2f6a0 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68  mpatibility with
2f6a1 20 4d 53 20 53 51 4c 20 53 65 72 76 65 72 20 2a   MS SQL Server *
2f6a2 2f 0a 20 20 20 20 63 61 73 65 20 27 3a 27 3a 20  /.    case ':': 
2f6a3 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  {.      int n = 
2f6a4 30 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  0;.      *tokenT
2f6a5 79 70 65 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c  ype = TK_VARIABL
2f6a6 45 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  E;.      for(i=1
2f6a7 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69  ; (c=z[i])!=0; i
2f6a8 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
2f6a9 20 49 64 43 68 61 72 28 63 29 20 29 7b 0a 20 20   IdChar(c) ){.  
2f6aa 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a 23 69 66          n++;.#if
2f6ab 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f6ac 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20  _TCL_VARIABLE.  
2f6ad 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2f6ae 63 3d 3d 27 28 27 20 26 26 20 6e 3e 30 20 29 7b  c=='(' && n>0 ){
2f6af 0a 20 20 20 20 20 20 20 20 20 20 64 6f 7b 0a 20  .          do{. 
2f6b0 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
2f6b1 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c 65            }while
2f6b2 28 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26  ( (c=z[i])!=0 &&
2f6b3 20 21 73 71 6c 69 74 65 33 49 73 73 70 61 63 65   !sqlite3Isspace
2f6b4 28 63 29 20 26 26 20 63 21 3d 27 29 27 20 29 3b  (c) && c!=')' );
2f6b5 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63  .          if( c
2f6b6 3d 3d 27 29 27 20 29 7b 0a 20 20 20 20 20 20 20  ==')' ){.       
2f6b7 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
2f6b8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2f6b9 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
2f6ba 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  e = TK_ILLEGAL;.
2f6bb 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2f6bc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2f6bd 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
2f6be 3d 3d 27 3a 27 20 26 26 20 7a 5b 69 2b 31 5d 3d  ==':' && z[i+1]=
2f6bf 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  =':' ){.        
2f6c0 20 20 69 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20    i++;.#endif.  
2f6c1 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2f6c2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2f6c3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2f6c4 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29        if( n==0 )
2f6c5 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2f6c6 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20  _ILLEGAL;.      
2f6c7 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
2f6c8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2f6c9 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c  MIT_BLOB_LITERAL
2f6ca 0a 20 20 20 20 63 61 73 65 20 27 78 27 3a 20 63  .    case 'x': c
2f6cb 61 73 65 20 27 58 27 3a 20 7b 0a 20 20 20 20 20  ase 'X': {.     
2f6cc 20 69 66 28 20 7a 5b 31 5d 3d 3d 27 5c 27 27 20   if( z[1]=='\'' 
2f6cd 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  ){.        *toke
2f6ce 6e 54 79 70 65 20 3d 20 54 4b 5f 42 4c 4f 42 3b  nType = TK_BLOB;
2f6cf 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32  .        for(i=2
2f6d0 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26  ; (c=z[i])!=0 &&
2f6d1 20 63 21 3d 27 5c 27 27 3b 20 69 2b 2b 29 7b 0a   c!='\''; i++){.
2f6d2 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73            if( !s
2f6d3 71 6c 69 74 65 33 49 73 78 64 69 67 69 74 28 63  qlite3Isxdigit(c
2f6d4 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
2f6d5 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2f6d6 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20  _ILLEGAL;.      
2f6d7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
2f6d8 20 20 20 20 20 20 20 20 69 66 28 20 69 25 32 20          if( i%2 
2f6d9 7c 7c 20 21 63 20 29 20 2a 74 6f 6b 65 6e 54 79  || !c ) *tokenTy
2f6da 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
2f6db 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 20 29  .        if( c )
2f6dc 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72 65   i++;.        re
2f6dd 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a  turn i;.      }.
2f6de 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69        /* Otherwi
2f6df 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20  se fall through 
2f6e0 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65  to the next case
2f6e1 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66   */.    }.#endif
2f6e2 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
2f6e3 20 20 20 20 20 20 69 66 28 20 21 49 64 43 68 61        if( !IdCha
2f6e4 72 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20  r(*z) ){.       
2f6e5 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2f6e6 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 49        for(i=1; I
2f6e7 64 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b  dChar(z[i]); i++
2f6e8 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  ){}.      *token
2f6e9 54 79 70 65 20 3d 20 6b 65 79 77 6f 72 64 43 6f  Type = keywordCo
2f6ea 64 65 28 28 63 68 61 72 2a 29 7a 2c 20 69 29 3b  de((char*)z, i);
2f6eb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
2f6ec 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f  .    }.  }.  *to
2f6ed 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c  kenType = TK_ILL
2f6ee 45 47 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20 31  EGAL;.  return 1
2f6ef 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74  ;.}../*.** Run t
2f6f0 68 65 20 70 61 72 73 65 72 20 6f 6e 20 74 68 65  he parser on the
2f6f1 20 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e   given SQL strin
2f6f2 67 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 73  g.  The parser s
2f6f3 74 72 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 70  tructure is.** p
2f6f4 61 73 73 65 64 20 69 6e 2e 20 20 41 6e 20 53 51  assed in.  An SQ
2f6f5 4c 49 54 45 5f 20 73 74 61 74 75 73 20 63 6f 64  LITE_ status cod
2f6f6 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  e is returned.  
2f6f7 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
2f6f8 72 73 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 61 6e  rs.** then an an
2f6f9 64 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64  d attempt is mad
2f6fa 65 20 74 6f 20 77 72 69 74 65 20 61 6e 20 65 72  e to write an er
2f6fb 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f  ror message into
2f6fc 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62 74 61   .** memory obta
2f6fd 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
2f6fe 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74  3_malloc() and t
2f6ff 6f 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67  o make *pzErrMsg
2f700 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a   point to that.*
2f701 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e  * error message.
2f702 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2f703 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75  TE int sqlite3Ru
2f704 6e 50 61 72 73 65 72 28 50 61 72 73 65 20 2a 70  nParser(Parse *p
2f705 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
2f706 72 20 2a 7a 53 71 6c 2c 20 63 68 61 72 20 2a 2a  r *zSql, char **
2f707 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74  pzErrMsg){.  int
2f708 20 6e 45 72 72 20 3d 20 30 3b 20 20 20 20 20 20   nErr = 0;      
2f709 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f70a 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
2f70b 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a   encountered */.
2f70c 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
2f70d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f70e 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2f70f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e  r */.  void *pEn
2f710 67 69 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20  gine;           
2f711 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 45         /* The LE
2f712 4d 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 4c 41  MON-generated LA
2f713 4c 52 28 31 29 20 70 61 72 73 65 72 20 2a 2f 0a  LR(1) parser */.
2f714 20 20 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65 3b    int tokenType;
2f715 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f716 20 20 2f 2a 20 74 79 70 65 20 6f 66 20 74 68 65    /* type of the
2f717 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20   next token */. 
2f718 20 69 6e 74 20 6c 61 73 74 54 6f 6b 65 6e 50 61   int lastTokenPa
2f719 72 73 65 64 20 3d 20 2d 31 3b 20 20 20 20 20 20  rsed = -1;      
2f71a 20 2f 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20   /* type of the 
2f71b 70 72 65 76 69 6f 75 73 20 74 6f 6b 65 6e 20 2a  previous token *
2f71c 2f 0a 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f 6f  /.  u8 enableLoo
2f71d 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20  kaside;         
2f71e 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c      /* Saved val
2f71f 75 65 20 6f 66 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ue of db->lookas
2f720 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 2a 2f 0a  ide.bEnabled */.
2f721 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2f722 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
2f723 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2f724 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
2f725 20 20 69 6e 74 20 6d 78 53 71 6c 4c 65 6e 3b 20    int mxSqlLen; 
2f726 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f727 20 20 2f 2a 20 4d 61 78 20 6c 65 6e 67 74 68 20    /* Max length 
2f728 6f 66 20 61 6e 20 53 51 4c 20 73 74 72 69 6e 67  of an SQL string
2f729 20 2a 2f 0a 0a 0a 20 20 6d 78 53 71 6c 4c 65 6e   */...  mxSqlLen
2f72a 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51   = db->aLimit[SQ
2f72b 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c  LITE_LIMIT_SQL_L
2f72c 45 4e 47 54 48 5d 3b 0a 20 20 69 66 28 20 64 62  ENGTH];.  if( db
2f72d 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d  ->activeVdbeCnt=
2f72e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75 31  =0 ){.    db->u1
2f72f 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d  .isInterrupted =
2f730 20 30 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65   0;.  }.  pParse
2f731 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  ->rc = SQLITE_OK
2f732 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69  ;.  pParse->zTai
2f733 6c 20 3d 20 70 50 61 72 73 65 2d 3e 7a 53 71 6c  l = pParse->zSql
2f734 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 20 3d 20 30   = zSql;.  i = 0
2f735 3b 0a 20 20 61 73 73 65 72 74 28 20 70 7a 45 72  ;.  assert( pzEr
2f736 72 4d 73 67 21 3d 30 20 29 3b 0a 20 20 70 45 6e  rMsg!=0 );.  pEn
2f737 67 69 6e 65 20 3d 20 73 71 6c 69 74 65 33 50 61  gine = sqlite3Pa
2f738 72 73 65 72 41 6c 6c 6f 63 28 28 76 6f 69 64 2a  rserAlloc((void*
2f739 28 2a 29 28 73 69 7a 65 5f 74 29 29 73 71 6c 69  (*)(size_t))sqli
2f73a 74 65 33 4d 61 6c 6c 6f 63 29 3b 0a 20 20 69 66  te3Malloc);.  if
2f73b 28 20 70 45 6e 67 69 6e 65 3d 3d 30 20 29 7b 0a  ( pEngine==0 ){.
2f73c 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
2f73d 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65  iled = 1;.    re
2f73e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2f73f 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  M;.  }.  assert(
2f740 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
2f741 6b 65 6e 2e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20  ken.dyn==0 );.  
2f742 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2f743 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  pNewTable==0 );.
2f744 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
2f745 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3d 3d 30  ->pNewTrigger==0
2f746 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
2f747 61 72 73 65 2d 3e 6e 56 61 72 3d 3d 30 20 29 3b  arse->nVar==0 );
2f748 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
2f749 65 2d 3e 6e 56 61 72 45 78 70 72 3d 3d 30 20 29  e->nVarExpr==0 )
2f74a 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
2f74b 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f  se->nVarExprAllo
2f74c 63 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  c==0 );.  assert
2f74d 28 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45  ( pParse->apVarE
2f74e 78 70 72 3d 3d 30 20 29 3b 0a 20 20 65 6e 61 62  xpr==0 );.  enab
2f74f 6c 65 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 64 62  leLookaside = db
2f750 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
2f751 62 6c 65 64 3b 0a 20 20 69 66 28 20 64 62 2d 3e  bled;.  if( db->
2f752 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74  lookaside.pStart
2f753 20 29 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   ) db->lookaside
2f754 2e 62 45 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 20  .bEnabled = 1;. 
2f755 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
2f756 6b 65 6e 2e 71 75 6f 74 65 64 20 3d 20 31 3b 0a  ken.quoted = 1;.
2f757 20 20 77 68 69 6c 65 28 20 21 64 62 2d 3e 6d 61    while( !db->ma
2f758 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 7a 53  llocFailed && zS
2f759 71 6c 5b 69 5d 21 3d 30 20 29 7b 0a 20 20 20 20  ql[i]!=0 ){.    
2f75a 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29 3b 0a  assert( i>=0 );.
2f75b 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4c 61 73      pParse->sLas
2f75c 74 54 6f 6b 65 6e 2e 7a 20 3d 20 28 75 38 2a 29  tToken.z = (u8*)
2f75d 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 61 73  &zSql[i];.    as
2f75e 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 73 4c  sert( pParse->sL
2f75f 61 73 74 54 6f 6b 65 6e 2e 64 79 6e 3d 3d 30 20  astToken.dyn==0 
2f760 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2f761 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
2f762 6e 2e 71 75 6f 74 65 64 20 29 3b 0a 20 20 20 20  n.quoted );.    
2f763 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b  pParse->sLastTok
2f764 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33 47 65  en.n = sqlite3Ge
2f765 74 54 6f 6b 65 6e 28 28 75 6e 73 69 67 6e 65 64  tToken((unsigned
2f766 20 63 68 61 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c   char*)&zSql[i],
2f767 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20  &tokenType);.   
2f768 20 69 20 2b 3d 20 70 50 61 72 73 65 2d 3e 73 4c   i += pParse->sL
2f769 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20  astToken.n;.    
2f76a 69 66 28 20 69 3e 6d 78 53 71 6c 4c 65 6e 20 29  if( i>mxSqlLen )
2f76b 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
2f76c 72 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42  rc = SQLITE_TOOB
2f76d 49 47 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  IG;.      break;
2f76e 0a 20 20 20 20 7d 0a 20 20 20 20 73 77 69 74 63  .    }.    switc
2f76f 68 28 20 74 6f 6b 65 6e 54 79 70 65 20 29 7b 0a  h( tokenType ){.
2f770 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 50        case TK_SP
2f771 41 43 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  ACE: {.        i
2f772 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65  f( db->u1.isInte
2f773 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20 20  rrupted ){.     
2f774 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20       pParse->rc 
2f775 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
2f776 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  PT;.          sq
2f777 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70  lite3SetString(p
2f778 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 69 6e  zErrMsg, db, "in
2f779 74 65 72 72 75 70 74 22 29 3b 0a 20 20 20 20 20  terrupt");.     
2f77a 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
2f77b 70 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20 7d  parse;.        }
2f77c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2f77d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
2f77e 73 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 3a 20 7b  se TK_ILLEGAL: {
2f77f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2f780 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 45 72  DbFree(db, *pzEr
2f781 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a  rMsg);.        *
2f782 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
2f783 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 75  e3MPrintf(db, "u
2f784 6e 72 65 63 6f 67 6e 69 7a 65 64 20 74 6f 6b 65  nrecognized toke
2f785 6e 3a 20 5c 22 25 54 5c 22 22 2c 0a 20 20 20 20  n: \"%T\"",.    
2f786 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f787 20 20 20 20 26 70 50 61 72 73 65 2d 3e 73 4c 61      &pParse->sLa
2f788 73 74 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20  stToken);.      
2f789 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
2f78a 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72    goto abort_par
2f78b 73 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  se;.      }.    
2f78c 20 20 63 61 73 65 20 54 4b 5f 53 45 4d 49 3a 20    case TK_SEMI: 
2f78d 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  {.        pParse
2f78e 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b  ->zTail = &zSql[
2f78f 69 5d 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46  i];.        /* F
2f790 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68  all thru into th
2f791 65 20 64 65 66 61 75 6c 74 20 63 61 73 65 20 2a  e default case *
2f792 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  /.      }.      
2f793 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
2f794 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
2f795 28 70 45 6e 67 69 6e 65 2c 20 74 6f 6b 65 6e 54  (pEngine, tokenT
2f796 79 70 65 2c 20 70 50 61 72 73 65 2d 3e 73 4c 61  ype, pParse->sLa
2f797 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65 29  stToken, pParse)
2f798 3b 0a 20 20 20 20 20 20 20 20 6c 61 73 74 54 6f  ;.        lastTo
2f799 6b 65 6e 50 61 72 73 65 64 20 3d 20 74 6f 6b 65  kenParsed = toke
2f79a 6e 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69  nType;.        i
2f79b 66 28 20 70 50 61 72 73 65 2d 3e 72 63 21 3d 53  f( pParse->rc!=S
2f79c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2f79d 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
2f79e 5f 70 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20  _parse;.        
2f79f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2f7a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2f7a1 20 7d 0a 61 62 6f 72 74 5f 70 61 72 73 65 3a 0a   }.abort_parse:.
2f7a2 20 20 69 66 28 20 7a 53 71 6c 5b 69 5d 3d 3d 30    if( zSql[i]==0
2f7a3 20 26 26 20 6e 45 72 72 3d 3d 30 20 26 26 20 70   && nErr==0 && p
2f7a4 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
2f7a5 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
2f7a6 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 21  lastTokenParsed!
2f7a7 3d 54 4b 5f 53 45 4d 49 20 29 7b 0a 20 20 20 20  =TK_SEMI ){.    
2f7a8 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28    sqlite3Parser(
2f7a9 70 45 6e 67 69 6e 65 2c 20 54 4b 5f 53 45 4d 49  pEngine, TK_SEMI
2f7aa 2c 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54  , pParse->sLastT
2f7ab 6f 6b 65 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20  oken, pParse);. 
2f7ac 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61       pParse->zTa
2f7ad 69 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20  il = &zSql[i];. 
2f7ae 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2f7af 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20  Parser(pEngine, 
2f7b0 30 2c 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  0, pParse->sLast
2f7b1 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65 29 3b 0a  Token, pParse);.
2f7b2 20 20 7d 0a 23 69 66 64 65 66 20 59 59 54 52 41    }.#ifdef YYTRA
2f7b3 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a  CKMAXSTACKDEPTH.
2f7b4 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
2f7b5 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
2f7b6 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 2c 0a 20  _PARSER_STACK,. 
2f7b7 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73       sqlite3Pars
2f7b8 65 72 53 74 61 63 6b 50 65 61 6b 28 70 45 6e 67  erStackPeak(pEng
2f7b9 69 6e 65 29 0a 20 20 29 3b 0a 23 65 6e 64 69 66  ine).  );.#endif
2f7ba 20 2f 2a 20 59 59 44 45 42 55 47 20 2a 2f 0a 20   /* YYDEBUG */. 
2f7bb 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
2f7bc 65 65 28 70 45 6e 67 69 6e 65 2c 20 73 71 6c 69  ee(pEngine, sqli
2f7bd 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 64 62 2d  te3_free);.  db-
2f7be 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
2f7bf 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b  led = enableLook
2f7c0 61 73 69 64 65 3b 0a 20 20 69 66 28 20 64 62 2d  aside;.  if( db-
2f7c1 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2f7c2 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
2f7c3 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
2f7c4 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65    }.  if( pParse
2f7c5 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc!=SQLITE_OK 
2f7c6 26 26 20 70 50 61 72 73 65 2d 3e 72 63 21 3d 53  && pParse->rc!=S
2f7c7 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26 20 70 50  QLITE_DONE && pP
2f7c8 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30  arse->zErrMsg==0
2f7c9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
2f7ca 65 74 53 74 72 69 6e 67 28 26 70 50 61 72 73 65  etString(&pParse
2f7cb 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
2f7cc 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53  %s", sqlite3ErrS
2f7cd 74 72 28 70 50 61 72 73 65 2d 3e 72 63 29 29 3b  tr(pParse->rc));
2f7ce 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
2f7cf 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  e->zErrMsg ){.  
2f7d0 20 20 69 66 28 20 2a 70 7a 45 72 72 4d 73 67 3d    if( *pzErrMsg=
2f7d1 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  =0 ){.      *pzE
2f7d2 72 72 4d 73 67 20 3d 20 70 50 61 72 73 65 2d 3e  rrMsg = pParse->
2f7d3 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 7d 65 6c  zErrMsg;.    }el
2f7d4 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2f7d5 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72  3DbFree(db, pPar
2f7d6 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  se->zErrMsg);.  
2f7d7 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
2f7d8 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
2f7d9 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 69   nErr++;.  }.  i
2f7da 66 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65  f( pParse->pVdbe
2f7db 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   && pParse->nErr
2f7dc 3e 30 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65  >0 && pParse->ne
2f7dd 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73  sted==0 ){.    s
2f7de 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
2f7df 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b  (pParse->pVdbe);
2f7e0 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 56 64  .    pParse->pVd
2f7e1 62 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e  be = 0;.  }.#ifn
2f7e2 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2f7e3 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69  SHARED_CACHE.  i
2f7e4 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
2f7e5 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  d==0 ){.    sqli
2f7e6 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50  te3DbFree(db, pP
2f7e7 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
2f7e8 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61  );.    pParse->a
2f7e9 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20  TableLock = 0;. 
2f7ea 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c     pParse->nTabl
2f7eb 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 23  eLock = 0;.  }.#
2f7ec 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
2f7ed 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2f7ee 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33  LTABLE.  sqlite3
2f7ef 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
2f7f0 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 29 3b 0a  e->apVtabLock);.
2f7f1 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 21 49  #endif..  if( !I
2f7f2 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
2f7f3 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
2f7f4 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56  pParse->declareV
2f7f5 74 61 62 20 66 6c 61 67 20 69 73 20 73 65 74 2c  tab flag is set,
2f7f6 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 61   do not delete a
2f7f7 6e 79 20 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a  ny table .    **
2f7f8 20 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74   structure built
2f7f9 20 75 70 20 69 6e 20 70 50 61 72 73 65 2d 3e 70   up in pParse->p
2f7fa 4e 65 77 54 61 62 6c 65 2e 20 54 68 65 20 63 61  NewTable. The ca
2f7fb 6c 6c 69 6e 67 20 63 6f 64 65 20 28 73 65 65 20  lling code (see 
2f7fc 76 74 61 62 2e 63 29 0a 20 20 20 20 2a 2a 20 77  vtab.c).    ** w
2f7fd 69 6c 6c 20 74 61 6b 65 20 72 65 73 70 6f 6e 73  ill take respons
2f7fe 69 62 69 6c 69 74 79 20 66 6f 72 20 66 72 65 65  ibility for free
2f7ff 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20 73 74  ing the Table st
2f800 72 75 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a  ructure..    */.
2f801 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2f802 65 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70  eTable(pParse->p
2f803 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 0a  NewTable);.  }..
2f804 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
2f805 72 69 67 67 65 72 28 64 62 2c 20 70 50 61 72 73  rigger(db, pPars
2f806 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 29 3b  e->pNewTrigger);
2f807 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2f808 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56  (db, pParse->apV
2f809 61 72 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74  arExpr);.  sqlit
2f80a 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61  e3DbFree(db, pPa
2f80b 72 73 65 2d 3e 61 41 6c 69 61 73 29 3b 0a 20 20  rse->aAlias);.  
2f80c 77 68 69 6c 65 28 20 70 50 61 72 73 65 2d 3e 70  while( pParse->p
2f80d 5a 6f 6d 62 69 65 54 61 62 20 29 7b 0a 20 20 20  ZombieTab ){.   
2f80e 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50 61 72   Table *p = pPar
2f80f 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a  se->pZombieTab;.
2f810 20 20 20 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d      pParse->pZom
2f811 62 69 65 54 61 62 20 3d 20 70 2d 3e 70 4e 65 78  bieTab = p->pNex
2f812 74 5a 6f 6d 62 69 65 3b 0a 20 20 20 20 73 71 6c  tZombie;.    sql
2f813 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
2f814 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 45  p);.  }.  if( nE
2f815 72 72 3e 30 20 26 26 20 28 70 50 61 72 73 65 2d  rr>0 && (pParse-
2f816 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  >rc==SQLITE_OK |
2f817 7c 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51  | pParse->rc==SQ
2f818 4c 49 54 45 5f 44 4f 4e 45 29 20 29 7b 0a 20 20  LITE_DONE) ){.  
2f819 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
2f81a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2f81b 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72 3b 0a  .  return nErr;.
2f81c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
2f81d 2a 2a 20 45 6e 64 20 6f 66 20 74 6f 6b 65 6e 69  ** End of tokeni
2f81e 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ze.c ***********
2f81f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f821 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2f822 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 63 6f  ** Begin file co
2f823 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  mplete.c *******
2f824 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f825 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f826 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
2f827 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
2f828 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2f829 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2f82a 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2f82b 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2f82c 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2f82d 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2f82e 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2f82f 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2f830 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2f831 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2f832 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2f833 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2f834 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2f835 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2f836 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2f837 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2f838 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2f839 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f83a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f83b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f83c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f83d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20  *********.** An 
2f83e 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53 51  tokenizer for SQ
2f83f 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  L.**.** This fil
2f840 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
2f841 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  e that implement
2f842 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  s the sqlite3_co
2f843 6d 70 6c 65 74 65 28 29 20 41 50 49 2e 0a 2a 2a  mplete() API..**
2f844 20 54 68 69 73 20 63 6f 64 65 20 75 73 65 64 20   This code used 
2f845 74 6f 20 62 65 20 70 61 72 74 20 6f 66 20 74 68  to be part of th
2f846 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 73 6f  e tokenizer.c so
2f847 75 72 63 65 20 66 69 6c 65 2e 20 20 42 75 74 20  urce file.  But 
2f848 62 79 0a 2a 2a 20 73 65 70 61 72 61 74 69 6e 67  by.** separating
2f849 20 69 74 20 6f 75 74 2c 20 74 68 65 20 63 6f 64   it out, the cod
2f84a 65 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  e will be automa
2f84b 74 69 63 61 6c 6c 79 20 6f 6d 69 74 74 65 64 20  tically omitted 
2f84c 66 72 6f 6d 0a 2a 2a 20 73 74 61 74 69 63 20 6c  from.** static l
2f84d 69 6e 6b 73 20 74 68 61 74 20 64 6f 20 6e 6f 74  inks that do not
2f84e 20 75 73 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 24   use it..**.** $
2f84f 49 64 3a 20 63 6f 6d 70 6c 65 74 65 2e 63 2c 76  Id: complete.c,v
2f850 20 31 2e 38 20 32 30 30 39 2f 30 34 2f 32 38 20   1.8 2009/04/28 
2f851 30 34 3a 34 36 3a 34 32 20 64 72 68 20 45 78 70  04:46:42 drh Exp
2f852 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51   $.*/.#ifndef SQ
2f853 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45  LITE_OMIT_COMPLE
2f854 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  TE../*.** This i
2f855 73 20 64 65 66 69 6e 65 64 20 69 6e 20 74 6f 6b  s defined in tok
2f856 65 6e 69 7a 65 2e 63 2e 20 20 57 65 20 6a 75 73  enize.c.  We jus
2f857 74 20 68 61 76 65 20 74 6f 20 69 6d 70 6f 72 74  t have to import
2f858 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e   the definition.
2f859 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
2f85a 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a  TE_AMALGAMATION.
2f85b 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
2f85c 43 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  CII.SQLITE_PRIVA
2f85d 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  TE const char sq
2f85e 6c 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68  lite3IsAsciiIdCh
2f85f 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64  ar[];.#define Id
2f860 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29  Char(C)  (((c=C)
2f861 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e  &0x80)!=0 || (c>
2f862 30 78 31 66 20 26 26 20 73 71 6c 69 74 65 33 49  0x1f && sqlite3I
2f863 73 41 73 63 69 69 49 64 43 68 61 72 5b 63 2d 30  sAsciiIdChar[c-0
2f864 78 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 69  x20])).#endif.#i
2f865 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
2f866 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  IC.SQLITE_PRIVAT
2f867 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c  E const char sql
2f868 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68  ite3IsEbcdicIdCh
2f869 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65 20 49 64  ar[];.#define Id
2f86a 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29  Char(C)  (((c=C)
2f86b 3e 3d 30 78 34 32 20 26 26 20 73 71 6c 69 74 65  >=0x42 && sqlite
2f86c 33 49 73 45 62 63 64 69 63 49 64 43 68 61 72 5b  3IsEbcdicIdChar[
2f86d 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e 64 69 66  c-0x40])).#endif
2f86e 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2f86f 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a  E_AMALGAMATION *
2f870 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e 20  /.../*.** Token 
2f871 74 79 70 65 73 20 75 73 65 64 20 62 79 20 74 68  types used by th
2f872 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  e sqlite3_comple
2f873 74 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 53  te() routine.  S
2f874 65 65 20 74 68 65 20 68 65 61 64 65 72 0a 2a 2a  ee the header.**
2f875 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 61   comments on tha
2f876 74 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20  t procedure for 
2f877 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
2f878 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  mation..*/.#defi
2f879 6e 65 20 74 6b 53 45 4d 49 20 20 20 20 30 0a 23  ne tkSEMI    0.#
2f87a 64 65 66 69 6e 65 20 74 6b 57 53 20 20 20 20 20  define tkWS     
2f87b 20 31 0a 23 64 65 66 69 6e 65 20 74 6b 4f 54 48   1.#define tkOTH
2f87c 45 52 20 20 20 32 0a 23 64 65 66 69 6e 65 20 74  ER   2.#define t
2f87d 6b 45 58 50 4c 41 49 4e 20 33 0a 23 64 65 66 69  kEXPLAIN 3.#defi
2f87e 6e 65 20 74 6b 43 52 45 41 54 45 20 20 34 0a 23  ne tkCREATE  4.#
2f87f 64 65 66 69 6e 65 20 74 6b 54 45 4d 50 20 20 20  define tkTEMP   
2f880 20 35 0a 23 64 65 66 69 6e 65 20 74 6b 54 52 49   5.#define tkTRI
2f881 47 47 45 52 20 36 0a 23 64 65 66 69 6e 65 20 74  GGER 6.#define t
2f882 6b 45 4e 44 20 20 20 20 20 37 0a 0a 2f 2a 0a 2a  kEND     7../*.*
2f883 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
2f884 20 74 68 65 20 67 69 76 65 6e 20 53 51 4c 20 73   the given SQL s
2f885 74 72 69 6e 67 20 65 6e 64 73 20 69 6e 20 61 20  tring ends in a 
2f886 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a  semicolon..**.**
2f887 20 53 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e   Special handlin
2f888 67 20 69 73 20 72 65 71 75 69 72 65 20 66 6f 72  g is require for
2f889 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
2f88a 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 57  statements..** W
2f88b 68 65 6e 65 76 65 72 20 74 68 65 20 43 52 45 41  henever the CREA
2f88c 54 45 20 54 52 49 47 47 45 52 20 6b 65 79 77 6f  TE TRIGGER keywo
2f88d 72 64 73 20 61 72 65 20 73 65 65 6e 2c 20 74 68  rds are seen, th
2f88e 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6d  e statement.** m
2f88f 75 73 74 20 65 6e 64 20 77 69 74 68 20 22 3b 45  ust end with ";E
2f890 4e 44 3b 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ND;"..**.** This
2f891 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2f892 75 73 65 73 20 61 20 73 74 61 74 65 20 6d 61 63  uses a state mac
2f893 68 69 6e 65 20 77 69 74 68 20 37 20 73 74 61 74  hine with 7 stat
2f894 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29 20  es:.**.**   (0) 
2f895 53 54 41 52 54 20 20 20 20 20 41 74 20 74 68 65  START     At the
2f896 20 62 65 67 69 6e 6e 69 6e 67 20 6f 72 20 65 6e   beginning or en
2f897 64 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74  d of an SQL stat
2f898 65 6d 65 6e 74 2e 20 20 54 68 69 73 20 72 6f 75  ement.  This rou
2f899 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  tine.**         
2f89a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20          returns 
2f89b 31 20 69 66 20 69 74 20 65 6e 64 73 20 69 6e 20  1 if it ends in 
2f89c 74 68 65 20 53 54 41 52 54 20 73 74 61 74 65 20  the START state 
2f89d 61 6e 64 20 30 20 69 66 20 69 74 20 65 6e 64 73  and 0 if it ends
2f89e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2f89f 20 20 20 20 69 6e 20 61 6e 79 20 6f 74 68 65 72      in any other
2f8a0 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20   state..**.**   
2f8a1 28 31 29 20 4e 4f 52 4d 41 4c 20 20 20 20 57 65  (1) NORMAL    We
2f8a2 20 61 72 65 20 69 6e 20 74 68 65 20 6d 69 64 64   are in the midd
2f8a3 6c 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20  le of statement 
2f8a4 77 68 69 63 68 20 65 6e 64 73 20 77 69 74 68 20  which ends with 
2f8a5 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 20 20 20 20  a single.**     
2f8a6 20 20 20 20 20 20 20 20 20 20 20 20 73 65 6d 69              semi
2f8a7 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28  colon..**.**   (
2f8a8 32 29 20 45 58 50 4c 41 49 4e 20 20 20 54 68 65  2) EXPLAIN   The
2f8a9 20 6b 65 79 77 6f 72 64 20 45 58 50 4c 41 49 4e   keyword EXPLAIN
2f8aa 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e 20 61   has been seen a
2f8ab 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
2f8ac 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  of .**          
2f8ad 20 20 20 20 20 20 20 61 20 73 74 61 74 65 6d 65         a stateme
2f8ae 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20  nt..**.**   (3) 
2f8af 43 52 45 41 54 45 20 20 20 20 54 68 65 20 6b 65  CREATE    The ke
2f8b0 79 77 6f 72 64 20 43 52 45 41 54 45 20 68 61 73  yword CREATE has
2f8b1 20 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74 68   been seen at th
2f8b2 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61  e beginning of a
2f8b3 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2f8b4 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 70      statement, p
2f8b5 6f 73 73 69 62 6c 79 20 70 72 65 63 65 65 64 65  ossibly preceede
2f8b6 64 20 62 79 20 45 58 50 4c 41 49 4e 20 61 6e 64  d by EXPLAIN and
2f8b7 2f 6f 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a  /or followed by.
2f8b8 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2f8b9 20 20 20 54 45 4d 50 20 6f 72 20 54 45 4d 50 4f     TEMP or TEMPO
2f8ba 52 41 52 59 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29  RARY.**.**   (4)
2f8bb 20 54 52 49 47 47 45 52 20 20 20 57 65 20 61 72   TRIGGER   We ar
2f8bc 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  e in the middle 
2f8bd 6f 66 20 61 20 74 72 69 67 67 65 72 20 64 65 66  of a trigger def
2f8be 69 6e 69 74 69 6f 6e 20 74 68 61 74 20 6d 75 73  inition that mus
2f8bf 74 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  t be.**         
2f8c0 20 20 20 20 20 20 20 20 65 6e 64 65 64 20 62 79          ended by
2f8c1 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2c 20 74 68   a semicolon, th
2f8c2 65 20 6b 65 79 77 6f 72 64 20 45 4e 44 2c 20 61  e keyword END, a
2f8c3 6e 64 20 61 6e 6f 74 68 65 72 20 73 65 6d 69 63  nd another semic
2f8c4 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 35  olon..**.**   (5
2f8c5 29 20 53 45 4d 49 20 20 20 20 20 20 57 65 27 76  ) SEMI      We'v
2f8c6 65 20 73 65 65 6e 20 74 68 65 20 66 69 72 73 74  e seen the first
2f8c7 20 73 65 6d 69 63 6f 6c 6f 6e 20 69 6e 20 74 68   semicolon in th
2f8c8 65 20 22 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f  e ";END;" that o
2f8c9 63 63 75 72 73 20 61 74 0a 2a 2a 20 20 20 20 20  ccurs at.**     
2f8ca 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
2f8cb 65 6e 64 20 6f 66 20 61 20 74 72 69 67 67 65 72  end of a trigger
2f8cc 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a   definition..**.
2f8cd 2a 2a 20 20 20 28 36 29 20 45 4e 44 20 20 20 20  **   (6) END    
2f8ce 20 20 20 57 65 27 76 65 20 73 65 65 6e 20 74 68     We've seen th
2f8cf 65 20 22 3b 45 4e 44 22 20 6f 66 20 74 68 65 20  e ";END" of the 
2f8d0 22 3b 45 4e 44 3b 22 20 74 68 61 74 20 6f 63 63  ";END;" that occ
2f8d1 75 72 73 20 61 74 20 74 68 65 20 65 6e 64 0a 2a  urs at the end.*
2f8d2 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2f8d3 20 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 64    of a trigger d
2f8d4 69 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ifinition..**.**
2f8d5 20 54 72 61 6e 73 69 74 69 6f 6e 73 20 62 65 74   Transitions bet
2f8d6 77 65 65 6e 20 73 74 61 74 65 73 20 61 62 6f 76  ween states abov
2f8d7 65 20 61 72 65 20 64 65 74 65 72 6d 69 6e 65 64  e are determined
2f8d8 20 62 79 20 74 6f 6b 65 6e 73 20 65 78 74 72 61   by tokens extra
2f8d9 63 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  cted.** from the
2f8da 20 69 6e 70 75 74 2e 20 20 54 68 65 20 66 6f 6c   input.  The fol
2f8db 6c 6f 77 69 6e 67 20 74 6f 6b 65 6e 73 20 61 72  lowing tokens ar
2f8dc 65 20 73 69 67 6e 69 66 69 63 61 6e 74 3a 0a 2a  e significant:.*
2f8dd 2a 0a 2a 2a 20 20 20 28 30 29 20 74 6b 53 45 4d  *.**   (0) tkSEM
2f8de 49 20 20 20 20 20 20 41 20 73 65 6d 69 63 6f 6c  I      A semicol
2f8df 6f 6e 2e 0a 2a 2a 20 20 20 28 31 29 20 74 6b 57  on..**   (1) tkW
2f8e0 53 20 20 20 20 20 20 20 20 57 68 69 74 65 73 70  S        Whitesp
2f8e1 61 63 65 0a 2a 2a 20 20 20 28 32 29 20 74 6b 4f  ace.**   (2) tkO
2f8e2 54 48 45 52 20 20 20 20 20 41 6e 79 20 6f 74 68  THER     Any oth
2f8e3 65 72 20 53 51 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a  er SQL token..**
2f8e4 20 20 20 28 33 29 20 74 6b 45 58 50 4c 41 49 4e     (3) tkEXPLAIN
2f8e5 20 20 20 54 68 65 20 22 65 78 70 6c 61 69 6e 22     The "explain"
2f8e6 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28   keyword..**   (
2f8e7 34 29 20 74 6b 43 52 45 41 54 45 20 20 20 20 54  4) tkCREATE    T
2f8e8 68 65 20 22 63 72 65 61 74 65 22 20 6b 65 79 77  he "create" keyw
2f8e9 6f 72 64 2e 0a 2a 2a 20 20 20 28 35 29 20 74 6b  ord..**   (5) tk
2f8ea 54 45 4d 50 20 20 20 20 20 20 54 68 65 20 22 74  TEMP      The "t
2f8eb 65 6d 70 22 20 6f 72 20 22 74 65 6d 70 6f 72 61  emp" or "tempora
2f8ec 72 79 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20  ry" keyword..** 
2f8ed 20 20 28 36 29 20 74 6b 54 52 49 47 47 45 52 20    (6) tkTRIGGER 
2f8ee 20 20 54 68 65 20 22 74 72 69 67 67 65 72 22 20    The "trigger" 
2f8ef 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 37  keyword..**   (7
2f8f0 29 20 74 6b 45 4e 44 20 20 20 20 20 20 20 54 68  ) tkEND       Th
2f8f1 65 20 22 65 6e 64 22 20 6b 65 79 77 6f 72 64 2e  e "end" keyword.
2f8f2 0a 2a 2a 0a 2a 2a 20 57 68 69 74 65 73 70 61 63  .**.** Whitespac
2f8f3 65 20 6e 65 76 65 72 20 63 61 75 73 65 73 20 61  e never causes a
2f8f4 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f   state transitio
2f8f5 6e 20 61 6e 64 20 69 73 20 61 6c 77 61 79 73 20  n and is always 
2f8f6 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  ignored..**.** I
2f8f7 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74  f we compile wit
2f8f8 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  h SQLITE_OMIT_TR
2f8f9 49 47 47 45 52 2c 20 61 6c 6c 20 6f 66 20 74 68  IGGER, all of th
2f8fa 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 6e 65  e computation ne
2f8fb 65 64 65 64 0a 2a 2a 20 74 6f 20 72 65 63 6f 67  eded.** to recog
2f8fc 6e 69 7a 65 20 74 68 65 20 65 6e 64 20 6f 66 20  nize the end of 
2f8fd 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62 65  a trigger can be
2f8fe 20 6f 6d 69 74 74 65 64 2e 20 20 41 6c 6c 20 77   omitted.  All w
2f8ff 65 20 68 61 76 65 20 74 6f 20 64 6f 0a 2a 2a 20  e have to do.** 
2f900 69 73 20 6c 6f 6f 6b 20 66 6f 72 20 61 20 73 65  is look for a se
2f901 6d 69 63 6f 6c 6f 6e 20 74 68 61 74 20 69 73 20  micolon that is 
2f902 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 6e 20 73  not part of an s
2f903 74 72 69 6e 67 20 6f 72 20 63 6f 6d 6d 65 6e 74  tring or comment
2f904 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2f905 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  int sqlite3_comp
2f906 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20  lete(const char 
2f907 2a 7a 53 71 6c 29 7b 0a 20 20 75 38 20 73 74 61  *zSql){.  u8 sta
2f908 74 65 20 3d 20 30 3b 20 20 20 2f 2a 20 43 75 72  te = 0;   /* Cur
2f909 72 65 6e 74 20 73 74 61 74 65 2c 20 75 73 69 6e  rent state, usin
2f90a 67 20 6e 75 6d 62 65 72 73 20 64 65 66 69 6e 65  g numbers define
2f90b 64 20 69 6e 20 68 65 61 64 65 72 20 63 6f 6d 6d  d in header comm
2f90c 65 6e 74 20 2a 2f 0a 20 20 75 38 20 74 6f 6b 65  ent */.  u8 toke
2f90d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75  n;       /* Valu
2f90e 65 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74 6f  e of the next to
2f90f 6b 65 6e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  ken */..#ifndef 
2f910 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2f911 47 45 52 0a 20 20 2f 2a 20 41 20 63 6f 6d 70 6c  GER.  /* A compl
2f912 65 78 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 63  ex statement mac
2f913 68 69 6e 65 20 75 73 65 64 20 74 6f 20 64 65 74  hine used to det
2f914 65 63 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61  ect the end of a
2f915 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 0a   CREATE TRIGGER.
2f916 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20    ** statement. 
2f917 20 54 68 69 73 20 69 73 20 74 68 65 20 6e 6f 72   This is the nor
2f918 6d 61 6c 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20  mal case..  */. 
2f919 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
2f91a 20 74 72 61 6e 73 5b 37 5d 5b 38 5d 20 3d 20 7b   trans[7][8] = {
2f91b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f91c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20        /* Token: 
2f91d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f91e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f91f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
2f920 2f 0a 20 20 20 20 20 2f 2a 20 53 74 61 74 65 3a  /.     /* State:
2f921 20 20 20 20 20 20 20 2a 2a 20 20 53 45 4d 49 20         **  SEMI 
2f922 20 57 53 20 20 4f 54 48 45 52 20 45 58 50 4c 41   WS  OTHER EXPLA
2f923 49 4e 20 20 43 52 45 41 54 45 20 20 54 45 4d 50  IN  CREATE  TEMP
2f924 20 20 54 52 49 47 47 45 52 20 20 45 4e 44 20 20    TRIGGER  END  
2f925 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20 20 53  */.     /* 0   S
2f926 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c  TART: */ {    0,
2f927 20 20 30 2c 20 20 20 20 20 31 2c 20 20 20 20 20    0,     1,     
2f928 20 32 2c 20 20 20 20 20 20 33 2c 20 20 20 20 31   2,      3,    1
2f929 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c 20  ,       1,   1, 
2f92a 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e   },.     /* 1  N
2f92b 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30  ORMAL: */ {    0
2f92c 2c 20 20 31 2c 20 20 20 20 20 31 2c 20 20 20 20  ,  1,     1,    
2f92d 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20    1,      1,    
2f92e 31 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31 2c  1,       1,   1,
2f92f 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 32 20 45    },.     /* 2 E
2f930 58 50 4c 41 49 4e 3a 20 2a 2f 20 7b 20 20 20 20  XPLAIN: */ {    
2f931 30 2c 20 20 32 2c 20 20 20 20 20 32 2c 20 20 20  0,  2,     2,   
2f932 20 20 20 31 2c 20 20 20 20 20 20 33 2c 20 20 20     1,      3,   
2f933 20 31 2c 20 20 20 20 20 20 20 31 2c 20 20 20 31   1,       1,   1
2f934 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 33 20  ,  },.     /* 3 
2f935 20 43 52 45 41 54 45 3a 20 2a 2f 20 7b 20 20 20   CREATE: */ {   
2f936 20 30 2c 20 20 33 2c 20 20 20 20 20 31 2c 20 20   0,  3,     1,  
2f937 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20 20      1,      1,  
2f938 20 20 33 2c 20 20 20 20 20 20 20 34 2c 20 20 20    3,       4,   
2f939 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 34  1,  },.     /* 4
2f93a 20 54 52 49 47 47 45 52 3a 20 2a 2f 20 7b 20 20   TRIGGER: */ {  
2f93b 20 20 35 2c 20 20 34 2c 20 20 20 20 20 34 2c 20    5,  4,     4, 
2f93c 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20       4,      4, 
2f93d 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20 20     4,       4,  
2f93e 20 34 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20   4,  },.     /* 
2f93f 35 20 20 20 20 53 45 4d 49 3a 20 2a 2f 20 7b 20  5    SEMI: */ { 
2f940 20 20 20 35 2c 20 20 35 2c 20 20 20 20 20 34 2c     5,  5,     4,
2f941 20 20 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c        4,      4,
2f942 20 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c 20      4,       4, 
2f943 20 20 36 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a    6,  },.     /*
2f944 20 36 20 20 20 20 20 45 4e 44 3a 20 2a 2f 20 7b   6     END: */ {
2f945 20 20 20 20 30 2c 20 20 36 2c 20 20 20 20 20 34      0,  6,     4
2f946 2c 20 20 20 20 20 20 34 2c 20 20 20 20 20 20 34  ,      4,      4
2f947 2c 20 20 20 20 34 2c 20 20 20 20 20 20 20 34 2c  ,    4,       4,
2f948 20 20 20 34 2c 20 20 7d 2c 0a 20 20 7d 3b 0a 23     4,  },.  };.#
2f949 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20 74 72 69  else.  /* If tri
2f94a 67 67 65 72 73 20 61 72 65 20 6e 6f 74 20 73 75  ggers are not su
2f94b 70 70 6f 72 65 64 20 62 79 20 74 68 69 73 20 63  ppored by this c
2f94c 6f 6d 70 69 6c 65 20 74 68 65 6e 20 74 68 65 20  ompile then the 
2f94d 73 74 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e  statement machin
2f94e 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 64  e.  ** used to d
2f94f 65 74 65 63 74 20 74 68 65 20 65 6e 64 20 6f 66  etect the end of
2f950 20 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20   a statement is 
2f951 6d 75 63 68 20 73 69 6d 70 6c 69 65 72 0a 20 20  much simplier.  
2f952 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
2f953 74 20 75 38 20 74 72 61 6e 73 5b 32 5d 5b 33 5d  t u8 trans[2][3]
2f954 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20   = {.           
2f955 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
2f956 65 6e 3a 20 20 20 20 20 20 20 20 20 20 20 2a 2f  en:           */
2f957 0a 20 20 20 20 20 2f 2a 20 53 74 61 74 65 3a 20  .     /* State: 
2f958 20 20 20 20 20 20 2a 2a 20 20 53 45 4d 49 20 20        **  SEMI  
2f959 57 53 20 20 4f 54 48 45 52 20 2a 2f 0a 20 20 20  WS  OTHER */.   
2f95a 20 20 2f 2a 20 30 20 20 20 53 54 41 52 54 3a 20    /* 0   START: 
2f95b 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30 2c 20 20  */ {    0,  0,  
2f95c 20 20 20 31 2c 20 7d 2c 0a 20 20 20 20 20 2f 2a     1, },.     /*
2f95d 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b   1  NORMAL: */ {
2f95e 20 20 20 20 30 2c 20 20 31 2c 20 20 20 20 20 31      0,  1,     1
2f95f 2c 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6e 64 69 66  , },.  };.#endif
2f960 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2f961 54 52 49 47 47 45 52 20 2a 2f 0a 0a 20 20 77 68  TRIGGER */..  wh
2f962 69 6c 65 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20  ile( *zSql ){.  
2f963 20 20 73 77 69 74 63 68 28 20 2a 7a 53 71 6c 20    switch( *zSql 
2f964 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 27 3b  ){.      case ';
2f965 27 3a 20 7b 20 20 2f 2a 20 41 20 73 65 6d 69 63  ': {  /* A semic
2f966 6f 6c 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  olon */.        
2f967 74 6f 6b 65 6e 20 3d 20 74 6b 53 45 4d 49 3b 0a  token = tkSEMI;.
2f968 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2f969 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
2f96a 65 20 27 20 27 3a 0a 20 20 20 20 20 20 63 61 73  e ' ':.      cas
2f96b 65 20 27 5c 72 27 3a 0a 20 20 20 20 20 20 63 61  e '\r':.      ca
2f96c 73 65 20 27 5c 74 27 3a 0a 20 20 20 20 20 20 63  se '\t':.      c
2f96d 61 73 65 20 27 5c 6e 27 3a 0a 20 20 20 20 20 20  ase '\n':.      
2f96e 63 61 73 65 20 27 5c 66 27 3a 20 7b 20 20 2f 2a  case '\f': {  /*
2f96f 20 57 68 69 74 65 20 73 70 61 63 65 20 69 73 20   White space is 
2f970 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20  ignored */.     
2f971 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b     token = tkWS;
2f972 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2f973 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
2f974 73 65 20 27 2f 27 3a 20 7b 20 20 20 2f 2a 20 43  se '/': {   /* C
2f975 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20  -style comments 
2f976 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  */.        if( z
2f977 53 71 6c 5b 31 5d 21 3d 27 2a 27 20 29 7b 0a 20  Sql[1]!='*' ){. 
2f978 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
2f979 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
2f97a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2f97b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 53 71     }.        zSq
2f97c 6c 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20  l += 2;.        
2f97d 77 68 69 6c 65 28 20 7a 53 71 6c 5b 30 5d 20 26  while( zSql[0] &
2f97e 26 20 28 7a 53 71 6c 5b 30 5d 21 3d 27 2a 27 20  & (zSql[0]!='*' 
2f97f 7c 7c 20 7a 53 71 6c 5b 31 5d 21 3d 27 2f 27 29  || zSql[1]!='/')
2f980 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20   ){ zSql++; }.  
2f981 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 30        if( zSql[0
2f982 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  ]==0 ) return 0;
2f983 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b  .        zSql++;
2f984 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d  .        token =
2f985 20 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62   tkWS;.        b
2f986 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2f987 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20 7b 20      case '-': { 
2f988 20 20 2f 2a 20 53 51 4c 2d 73 74 79 6c 65 20 63    /* SQL-style c
2f989 6f 6d 6d 65 6e 74 73 20 66 72 6f 6d 20 22 2d 2d  omments from "--
2f98a 22 20 74 6f 20 65 6e 64 20 6f 66 20 6c 69 6e 65  " to end of line
2f98b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
2f98c 7a 53 71 6c 5b 31 5d 21 3d 27 2d 27 20 29 7b 0a  zSql[1]!='-' ){.
2f98d 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2f98e 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20  = tkOTHER;.     
2f98f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2f990 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68      }.        wh
2f991 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a  ile( *zSql && *z
2f992 53 71 6c 21 3d 27 5c 6e 27 20 29 7b 20 7a 53 71  Sql!='\n' ){ zSq
2f993 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69  l++; }.        i
2f994 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65  f( *zSql==0 ) re
2f995 74 75 72 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 20  turn state==0;. 
2f996 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2f997 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72 65  kWS;.        bre
2f998 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2f999 20 20 63 61 73 65 20 27 5b 27 3a 20 7b 20 20 20    case '[': {   
2f99a 2f 2a 20 4d 69 63 72 6f 73 6f 66 74 2d 73 74 79  /* Microsoft-sty
2f99b 6c 65 20 69 64 65 6e 74 69 66 69 65 72 73 20 69  le identifiers i
2f99c 6e 20 5b 2e 2e 2e 5d 20 2a 2f 0a 20 20 20 20 20  n [...] */.     
2f99d 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20     zSql++;.     
2f99e 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20     while( *zSql 
2f99f 26 26 20 2a 7a 53 71 6c 21 3d 27 5d 27 20 29 7b  && *zSql!=']' ){
2f9a0 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20   zSql++; }.     
2f9a1 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20     if( *zSql==0 
2f9a2 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
2f9a3 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
2f9a4 48 45 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65  HER;.        bre
2f9a5 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2f9a6 20 20 63 61 73 65 20 27 60 27 3a 20 20 20 20 20    case '`':     
2f9a7 2f 2a 20 47 72 61 76 65 2d 61 63 63 65 6e 74 20  /* Grave-accent 
2f9a8 71 75 6f 74 65 64 20 73 79 6d 62 6f 6c 73 20 75  quoted symbols u
2f9a9 73 65 64 20 62 79 20 4d 79 53 51 4c 20 2a 2f 0a  sed by MySQL */.
2f9aa 20 20 20 20 20 20 63 61 73 65 20 27 22 27 3a 20        case '"': 
2f9ab 20 20 20 20 2f 2a 20 73 69 6e 67 6c 65 2d 20 61      /* single- a
2f9ac 6e 64 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64  nd double-quoted
2f9ad 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20   strings */.    
2f9ae 20 20 63 61 73 65 20 27 5c 27 27 3a 20 7b 0a 20    case '\'': {. 
2f9af 20 20 20 20 20 20 20 69 6e 74 20 63 20 3d 20 2a         int c = *
2f9b0 7a 53 71 6c 3b 0a 20 20 20 20 20 20 20 20 7a 53  zSql;.        zS
2f9b1 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68  ql++;.        wh
2f9b2 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a  ile( *zSql && *z
2f9b3 53 71 6c 21 3d 63 20 29 7b 20 7a 53 71 6c 2b 2b  Sql!=c ){ zSql++
2f9b4 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
2f9b5 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72  *zSql==0 ) retur
2f9b6 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b  n 0;.        tok
2f9b7 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
2f9b8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2f9b9 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75     }.      defau
2f9ba 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e  lt: {.        in
2f9bb 74 20 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28  t c;.        if(
2f9bc 20 49 64 43 68 61 72 28 28 75 38 29 2a 7a 53 71   IdChar((u8)*zSq
2f9bd 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  l) ){.          
2f9be 2f 2a 20 4b 65 79 77 6f 72 64 73 20 61 6e 64 20  /* Keywords and 
2f9bf 75 6e 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66  unquoted identif
2f9c0 69 65 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  iers */.        
2f9c1 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20 20    int nId;.     
2f9c2 20 20 20 20 20 66 6f 72 28 6e 49 64 3d 31 3b 20       for(nId=1; 
2f9c3 49 64 43 68 61 72 28 7a 53 71 6c 5b 6e 49 64 5d  IdChar(zSql[nId]
2f9c4 29 3b 20 6e 49 64 2b 2b 29 7b 7d 0a 23 69 66 64  ); nId++){}.#ifd
2f9c5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2f9c6 52 49 47 47 45 52 0a 20 20 20 20 20 20 20 20 20  RIGGER.         
2f9c7 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2f9c8 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20  ;.#else.        
2f9c9 20 20 73 77 69 74 63 68 28 20 2a 7a 53 71 6c 20    switch( *zSql 
2f9ca 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
2f9cb 61 73 65 20 27 63 27 3a 20 63 61 73 65 20 27 43  ase 'c': case 'C
2f9cc 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ': {.           
2f9cd 20 20 20 69 66 28 20 6e 49 64 3d 3d 36 20 26 26     if( nId==6 &&
2f9ce 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
2f9cf 28 7a 53 71 6c 2c 20 22 63 72 65 61 74 65 22 2c  (zSql, "create",
2f9d0 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   6)==0 ){.      
2f9d1 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2f9d2 3d 20 74 6b 43 52 45 41 54 45 3b 0a 20 20 20 20  = tkCREATE;.    
2f9d3 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
2f9d4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f9d5 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2f9d6 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2f9d7 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
2f9d8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2f9d9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
2f9da 20 63 61 73 65 20 27 74 27 3a 20 63 61 73 65 20   case 't': case 
2f9db 27 54 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  'T': {.         
2f9dc 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 37 20       if( nId==7 
2f9dd 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
2f9de 6d 70 28 7a 53 71 6c 2c 20 22 74 72 69 67 67 65  mp(zSql, "trigge
2f9df 72 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20  r", 7)==0 ){.   
2f9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
2f9e1 65 6e 20 3d 20 74 6b 54 52 49 47 47 45 52 3b 0a  en = tkTRIGGER;.
2f9e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
2f9e3 6c 73 65 20 69 66 28 20 6e 49 64 3d 3d 34 20 26  lse if( nId==4 &
2f9e4 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
2f9e5 70 28 7a 53 71 6c 2c 20 22 74 65 6d 70 22 2c 20  p(zSql, "temp", 
2f9e6 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  4)==0 ){.       
2f9e7 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
2f9e8 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20 20   tkTEMP;.       
2f9e9 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2f9ea 20 6e 49 64 3d 3d 39 20 26 26 20 73 71 6c 69 74   nId==9 && sqlit
2f9eb 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c  e3StrNICmp(zSql,
2f9ec 20 22 74 65 6d 70 6f 72 61 72 79 22 2c 20 39 29   "temporary", 9)
2f9ed 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2f9ee 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2f9ef 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20 20  kTEMP;.         
2f9f0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2f9f1 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
2f9f2 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
2f9f3 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2f9f4 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
2f9f5 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
2f9f6 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
2f9f7 20 27 65 27 3a 20 20 63 61 73 65 20 27 45 27 3a   'e':  case 'E':
2f9f8 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
2f9f9 20 69 66 28 20 6e 49 64 3d 3d 33 20 26 26 20 73   if( nId==3 && s
2f9fa 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
2f9fb 53 71 6c 2c 20 22 65 6e 64 22 2c 20 33 29 3d 3d  Sql, "end", 3)==
2f9fc 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
2f9fd 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45       token = tkE
2f9fe 4e 44 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ND;.            
2f9ff 20 20 7d 65 6c 73 65 0a 23 69 66 6e 64 65 66 20    }else.#ifndef 
2fa00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
2fa01 41 49 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20  AIN.            
2fa02 20 20 69 66 28 20 6e 49 64 3d 3d 37 20 26 26 20    if( nId==7 && 
2fa03 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2fa04 7a 53 71 6c 2c 20 22 65 78 70 6c 61 69 6e 22 2c  zSql, "explain",
2fa05 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   7)==0 ){.      
2fa06 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2fa07 3d 20 74 6b 45 58 50 4c 41 49 4e 3b 0a 20 20 20  = tkEXPLAIN;.   
2fa08 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
2fa09 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
2fa0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
2fa0b 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
2fa0c 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
2fa0d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2fa0e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2fa0f 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2fa10 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a          default:
2fa11 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
2fa12 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
2fa13 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2fa14 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
2fa15 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
2fa16 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2fa17 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a  E_OMIT_TRIGGER *
2fa18 2f 0a 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c  /.          zSql
2fa19 20 2b 3d 20 6e 49 64 2d 31 3b 0a 20 20 20 20 20   += nId-1;.     
2fa1a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2fa1b 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 6f 72 73      /* Operators
2fa1c 20 61 6e 64 20 73 70 65 63 69 61 6c 20 73 79 6d   and special sym
2fa1d 62 6f 6c 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  bols */.        
2fa1e 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
2fa1f 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  R;.        }.   
2fa20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2fa21 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 74    }.    }.    st
2fa22 61 74 65 20 3d 20 74 72 61 6e 73 5b 73 74 61 74  ate = trans[stat
2fa23 65 5d 5b 74 6f 6b 65 6e 5d 3b 0a 20 20 20 20 7a  e][token];.    z
2fa24 53 71 6c 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74  Sql++;.  }.  ret
2fa25 75 72 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 7d 0a  urn state==0;.}.
2fa26 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2fa27 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
2fa28 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2fa29 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
2fa2a 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
2fa2b 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65 73 63  e() routine desc
2fa2c 72 69 62 65 64 0a 2a 2a 20 61 62 6f 76 65 2c 20  ribed.** above, 
2fa2d 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
2fa2e 70 61 72 61 6d 65 74 65 72 20 69 73 20 72 65 71  parameter is req
2fa2f 75 69 72 65 64 20 74 6f 20 62 65 20 55 54 46 2d  uired to be UTF-
2fa30 31 36 20 65 6e 63 6f 64 65 64 2c 20 6e 6f 74 0a  16 encoded, not.
2fa31 2a 2a 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c  ** UTF-8..*/.SQL
2fa32 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2fa33 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 63  te3_complete16(c
2fa34 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 29  onst void *zSql)
2fa35 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  {.  sqlite3_valu
2fa36 65 20 2a 70 56 61 6c 3b 0a 20 20 63 68 61 72 20  e *pVal;.  char 
2fa37 63 6f 6e 73 74 20 2a 7a 53 71 6c 38 3b 0a 20 20  const *zSql8;.  
2fa38 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2fa39 4e 4f 4d 45 4d 3b 0a 0a 23 69 66 6e 64 65 66 20  NOMEM;..#ifndef 
2fa3a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
2fa3b 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69  INIT.  rc = sqli
2fa3c 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
2fa3d 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
2fa3e 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20  urn rc;.#endif. 
2fa3f 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56   pVal = sqlite3V
2fa40 61 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20 73 71  alueNew(0);.  sq
2fa41 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
2fa42 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c 2c  (pVal, -1, zSql,
2fa43 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
2fa44 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  IVE, SQLITE_STAT
2fa45 49 43 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73  IC);.  zSql8 = s
2fa46 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
2fa47 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
2fa48 38 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 38 20  8);.  if( zSql8 
2fa49 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
2fa4a 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 7a 53 71  te3_complete(zSq
2fa4b 6c 38 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  l8);.  }else{.  
2fa4c 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2fa4d 4d 45 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  MEM;.  }.  sqlit
2fa4e 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c  e3ValueFree(pVal
2fa4f 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
2fa50 74 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63  te3ApiExit(0, rc
2fa51 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
2fa52 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2fa53 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   */.#endif /* SQ
2fa54 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45  LITE_OMIT_COMPLE
2fa55 54 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  TE */../********
2fa56 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 6f  ****** End of co
2fa57 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  mplete.c *******
2fa58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa5a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2fa5b 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
2fa5c 65 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e main.c *******
2fa5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa5f 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
2fa60 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
2fa61 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2fa62 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2fa63 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2fa64 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2fa65 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2fa66 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2fa67 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2fa68 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2fa69 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2fa6a 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2fa6b 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2fa6c 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2fa6d 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2fa6e 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2fa6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2fa70 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2fa71 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2fa72 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2fa73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2fa77 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72 20 74   Main file for t
2fa78 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
2fa79 79 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73  y.  The routines
2fa7a 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a   in this file.**
2fa7b 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70   implement the p
2fa7c 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65 72 66  rogrammer interf
2fa7d 61 63 65 20 74 6f 20 74 68 65 20 6c 69 62 72 61  ace to the libra
2fa7e 72 79 2e 20 20 52 6f 75 74 69 6e 65 73 20 69 6e  ry.  Routines in
2fa7f 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 73 20  .** other files 
2fa80 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
2fa81 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 61   use by SQLite a
2fa82 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  nd should not be
2fa83 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 62 79 20  .** accessed by 
2fa84 75 73 65 72 73 20 6f 66 20 74 68 65 20 6c 69 62  users of the lib
2fa85 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  rary..**.** $Id:
2fa86 20 6d 61 69 6e 2e 63 2c 76 20 31 2e 35 34 38 20   main.c,v 1.548 
2fa87 32 30 30 39 2f 30 35 2f 30 36 20 31 39 3a 30 33  2009/05/06 19:03
2fa88 3a 31 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :14 drh Exp $.*/
2fa89 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2fa8a 45 4e 41 42 4c 45 5f 46 54 53 33 0a 2f 2a 2a 2a  ENABLE_FTS3./***
2fa8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
2fa8c 75 64 65 20 66 74 73 33 2e 68 20 69 6e 20 74 68  ude fts3.h in th
2fa8d 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e  e middle of main
2fa8e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2fa8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2fa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2fa91 6e 20 66 69 6c 65 20 66 74 73 33 2e 68 20 2a 2a  n file fts3.h **
2fa92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fa94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2fa95 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a  * 2006 Oct 10.**
2fa96 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2fa97 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2fa98 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2fa99 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2fa9a 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2fa9b 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2fa9c 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2fa9d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2fa9e 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2fa9f 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2faa0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2faa1 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2faa2 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2faa3 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2faa4 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2faa5 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2faa6 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2faa7 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2faa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faac 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  *.**.** This hea
2faad 64 65 72 20 66 69 6c 65 20 69 73 20 75 73 65 64  der file is used
2faae 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61   by programs tha
2faaf 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61  t want to link a
2fab0 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 46 54  gainst the.** FT
2fab1 53 33 20 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c  S3 library.  All
2fab2 20 69 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c   it does is decl
2fab3 61 72 65 20 74 68 65 20 73 71 6c 69 74 65 33 46  are the sqlite3F
2fab4 74 73 33 49 6e 69 74 28 29 20 69 6e 74 65 72 66  ts3Init() interf
2fab5 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65  ace..*/..#if 0.e
2fab6 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64  xtern "C" {.#end
2fab7 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c  if  /* __cpluspl
2fab8 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52  us */..SQLITE_PR
2fab9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2faba 33 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65  3Fts3Init(sqlite
2fabb 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d  3 *db);..#if 0.}
2fabc 20 20 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20    /* extern "C" 
2fabd 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f  */.#endif  /* __
2fabe 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a  cplusplus */../*
2fabf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2fac0 64 20 6f 66 20 66 74 73 33 2e 68 20 2a 2a 2a 2a  d of fts3.h ****
2fac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fac2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fac3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2fac4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
2fac5 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
2fac6 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61  e left off in ma
2fac7 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
2fac8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
2fac9 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
2faca 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a  TE_ENABLE_RTREE.
2facb 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2facc 49 6e 63 6c 75 64 65 20 72 74 72 65 65 2e 68 20  Include rtree.h 
2facd 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
2face 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
2facf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2fad0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2fad1 42 65 67 69 6e 20 66 69 6c 65 20 72 74 72 65 65  Begin file rtree
2fad2 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2fad3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fad4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2fad5 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
2fad6 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
2fad7 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2fad8 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2fad9 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2fada 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2fadb 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2fadc 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2fadd 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2fade 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2fadf 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2fae0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2fae1 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2fae2 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2fae3 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2fae4 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2fae5 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2fae6 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2fae7 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2fae8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fae9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faeb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2faec 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
2faed 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20   header file is 
2faee 75 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73  used by programs
2faef 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c 69   that want to li
2faf0 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a  nk against the.*
2faf1 2a 20 52 54 52 45 45 20 6c 69 62 72 61 72 79 2e  * RTREE library.
2faf2 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73    All it does is
2faf3 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c   declare the sql
2faf4 69 74 65 33 52 74 72 65 65 49 6e 69 74 28 29 20  ite3RtreeInit() 
2faf5 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23  interface..*/..#
2faf6 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20  if 0.extern "C" 
2faf7 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63  {.#endif  /* __c
2faf8 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c  plusplus */..SQL
2faf9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2fafa 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
2fafb 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a  (sqlite3 *db);..
2fafc 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65  #if 0.}  /* exte
2fafd 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66  rn "C" */.#endif
2fafe 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73    /* __cplusplus
2faff 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2fb00 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 74 72 65  **** End of rtre
2fb01 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
2fb02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb04 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2fb05 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
2fb06 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
2fb07 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a  f in main.c ****
2fb08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb09 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64  ***/.#endif.#ifd
2fb0a 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2fb0b 5f 49 43 55 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ICU./**********
2fb0c 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c  **** Include sql
2fb0d 69 74 65 69 63 75 2e 68 20 69 6e 20 74 68 65 20  iteicu.h in the 
2fb0e 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63  middle of main.c
2fb0f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2fb10 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2fb11 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2fb12 73 71 6c 69 74 65 69 63 75 2e 68 20 2a 2a 2a 2a  sqliteicu.h ****
2fb13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb15 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
2fb16 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65  May 26.**.** The
2fb17 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
2fb18 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
2fb19 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2fb1a 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2fb1b 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2fb1c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2fb1d 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2fb1e 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2fb1f 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2fb20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2fb21 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
2fb22 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
2fb23 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2fb24 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2fb25 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2fb26 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2fb27 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
2fb28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
2fb2d 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
2fb2e 65 20 69 73 20 75 73 65 64 20 62 79 20 70 72 6f  e is used by pro
2fb2f 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20  grams that want 
2fb30 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  to link against 
2fb31 74 68 65 0a 2a 2a 20 49 43 55 20 65 78 74 65 6e  the.** ICU exten
2fb32 73 69 6f 6e 2e 20 20 41 6c 6c 20 69 74 20 64 6f  sion.  All it do
2fb33 65 73 20 69 73 20 64 65 63 6c 61 72 65 20 74 68  es is declare th
2fb34 65 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74  e sqlite3IcuInit
2fb35 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  () interface..*/
2fb36 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22  ..#if 0.extern "
2fb37 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20  C" {.#endif  /* 
2fb38 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a  __cplusplus */..
2fb39 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2fb3a 6e 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  nt sqlite3IcuIni
2fb3b 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a  t(sqlite3 *db);.
2fb3c 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74  .#if 0.}  /* ext
2fb3d 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69  ern "C" */.#endi
2fb3e 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75  f  /* __cplusplu
2fb3f 73 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  s */.../********
2fb40 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71  ****** End of sq
2fb41 6c 69 74 65 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a  liteicu.h ******
2fb42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb44 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2fb45 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
2fb46 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
2fb47 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a  off in main.c **
2fb48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fb49 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f  *****/.#endif../
2fb4a 2a 0a 2a 2a 20 54 68 65 20 76 65 72 73 69 6f 6e  *.** The version
2fb4b 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 0a   of the library.
2fb4c 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2fb4d 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53  E_AMALGAMATION.S
2fb4e 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2fb4f 63 68 61 72 20 73 71 6c 69 74 65 33 5f 76 65 72  char sqlite3_ver
2fb50 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f  sion[] = SQLITE_
2fb51 56 45 52 53 49 4f 4e 3b 0a 23 65 6e 64 69 66 0a  VERSION;.#endif.
2fb52 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2fb53 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c   char *sqlite3_l
2fb54 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 7b  ibversion(void){
2fb55 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
2fb56 76 65 72 73 69 6f 6e 3b 20 7d 0a 53 51 4c 49 54  version; }.SQLIT
2fb57 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2fb58 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
2fb59 62 65 72 28 76 6f 69 64 29 7b 20 72 65 74 75 72  ber(void){ retur
2fb5a 6e 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  n SQLITE_VERSION
2fb5b 5f 4e 55 4d 42 45 52 3b 20 7d 0a 53 51 4c 49 54  _NUMBER; }.SQLIT
2fb5c 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2fb5d 33 5f 74 68 72 65 61 64 73 61 66 65 28 76 6f 69  3_threadsafe(voi
2fb5e 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
2fb5f 45 5f 54 48 52 45 41 44 53 41 46 45 3b 20 7d 0a  E_THREADSAFE; }.
2fb60 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2fb61 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29  LITE_OMIT_TRACE)
2fb62 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
2fb63 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43  TE_ENABLE_IOTRAC
2fb64 45 29 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  E)./*.** If the 
2fb65 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69  following functi
2fb66 6f 6e 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f  on pointer is no
2fb67 74 20 4e 55 4c 4c 20 61 6e 64 20 69 66 0a 2a 2a  t NULL and if.**
2fb68 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49   SQLITE_ENABLE_I
2fb69 4f 54 52 41 43 45 20 69 73 20 65 6e 61 62 6c 65  OTRACE is enable
2fb6a 64 2c 20 74 68 65 6e 20 6d 65 73 73 61 67 65 73  d, then messages
2fb6b 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 49   describing.** I
2fb6c 2f 4f 20 61 63 74 69 76 65 20 61 72 65 20 77 72  /O active are wr
2fb6d 69 74 74 65 6e 20 75 73 69 6e 67 20 74 68 69 73  itten using this
2fb6e 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73   function.  Thes
2fb6f 65 20 6d 65 73 73 61 67 65 73 0a 2a 2a 20 61 72  e messages.** ar
2fb70 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 64  e intended for d
2fb71 65 62 75 67 67 69 6e 67 20 61 63 74 69 76 69 74  ebugging activit
2fb72 79 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  y only..*/.SQLIT
2fb73 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28  E_PRIVATE void (
2fb74 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29  *sqlite3IoTrace)
2fb75 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e  (const char*, ..
2fb76 2e 29 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  .) = 0;.#endif..
2fb77 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c  /*.** If the fol
2fb78 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61  lowing global va
2fb79 72 69 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f  riable points to
2fb7a 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20   a string which 
2fb7b 69 73 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f  is the.** name o
2fb7c 66 20 61 20 64 69 72 65 63 74 6f 72 79 2c 20 74  f a directory, t
2fb7d 68 65 6e 20 74 68 61 74 20 64 69 72 65 63 74 6f  hen that directo
2fb7e 72 79 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  ry will be used 
2fb7f 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70  to store.** temp
2fb80 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 0a  orary files..**.
2fb81 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
2fb82 22 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f  "PRAGMA temp_sto
2fb83 72 65 5f 64 69 72 65 63 74 6f 72 79 22 20 53 51  re_directory" SQ
2fb84 4c 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51  L command..*/.SQ
2fb85 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
2fb86 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
2fb87 63 74 6f 72 79 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a  ctory = 0;../*.*
2fb88 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 53 51 4c  * Initialize SQL
2fb89 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69  ite.  .**.** Thi
2fb8a 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62  s routine must b
2fb8b 65 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e 69 74  e called to init
2fb8c 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
2fb8d 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a  y allocation,.**
2fb8e 20 56 46 53 2c 20 61 6e 64 20 6d 75 74 65 78 20   VFS, and mutex 
2fb8f 73 75 62 73 79 73 74 65 6d 73 20 70 72 69 6f 72  subsystems prior
2fb90 20 74 6f 20 64 6f 69 6e 67 20 61 6e 79 20 73 65   to doing any se
2fb91 72 69 6f 75 73 20 77 6f 72 6b 20 77 69 74 68 0a  rious work with.
2fb92 2a 2a 20 53 51 4c 69 74 65 2e 20 20 42 75 74 20  ** SQLite.  But 
2fb93 61 73 20 6c 6f 6e 67 20 61 73 20 79 6f 75 20 64  as long as you d
2fb94 6f 20 6e 6f 74 20 63 6f 6d 70 69 6c 65 20 77 69  o not compile wi
2fb95 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  th SQLITE_OMIT_A
2fb96 55 54 4f 49 4e 49 54 0a 2a 2a 20 74 68 69 73 20  UTOINIT.** this 
2fb97 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 62 65 20  routine will be 
2fb98 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 63  called automatic
2fb99 61 6c 6c 79 20 62 79 20 6b 65 79 20 72 6f 75 74  ally by key rout
2fb9a 69 6e 65 73 20 73 75 63 68 20 61 73 0a 2a 2a 20  ines such as.** 
2fb9b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 20  sqlite3_open(). 
2fb9c 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75   .**.** This rou
2fb9d 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20  tine is a no-op 
2fb9e 65 78 63 65 70 74 20 6f 6e 20 69 74 73 20 76 65  except on its ve
2fb9f 72 79 20 66 69 72 73 74 20 63 61 6c 6c 20 66 6f  ry first call fo
2fba0 72 20 74 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a  r the process,.*
2fba1 2a 20 6f 72 20 66 6f 72 20 74 68 65 20 66 69 72  * or for the fir
2fba2 73 74 20 63 61 6c 6c 20 61 66 74 65 72 20 61 20  st call after a 
2fba3 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
2fba4 73 68 75 74 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20  shutdown..**.** 
2fba5 54 68 65 20 66 69 72 73 74 20 74 68 72 65 61 64  The first thread
2fba6 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f   to call this ro
2fba7 75 74 69 6e 65 20 72 75 6e 73 20 74 68 65 20 69  utine runs the i
2fba8 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 74 6f  nitialization to
2fba9 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20  .** completion. 
2fbaa 20 49 66 20 73 75 62 73 65 71 75 65 6e 74 20 74   If subsequent t
2fbab 68 72 65 61 64 73 20 63 61 6c 6c 20 74 68 69 73  hreads call this
2fbac 20 72 6f 75 74 69 6e 65 20 62 65 66 6f 72 65 20   routine before 
2fbad 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 68 72  the first.** thr
2fbae 65 61 64 20 68 61 73 20 66 69 6e 69 73 68 65 64  ead has finished
2fbaf 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74   the initializat
2fbb0 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20 74 68 65  ion process, the
2fbb1 6e 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74  n the subsequent
2fbb2 0a 2a 2a 20 74 68 72 65 61 64 73 20 6d 75 73 74  .** threads must
2fbb3 20 62 6c 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65   block until the
2fbb4 20 66 69 72 73 74 20 74 68 72 65 61 64 20 66 69   first thread fi
2fbb5 6e 69 73 68 65 73 20 77 69 74 68 20 74 68 65 20  nishes with the 
2fbb6 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a  initialization..
2fbb7 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
2fbb8 74 68 72 65 61 64 20 6d 69 67 68 74 20 63 61 6c  thread might cal
2fbb9 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  l this routine r
2fbba 65 63 75 72 73 69 76 65 6c 79 2e 20 20 52 65 63  ecursively.  Rec
2fbbb 75 72 73 69 76 65 0a 2a 2a 20 63 61 6c 6c 73 20  ursive.** calls 
2fbbc 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
2fbbd 73 68 6f 75 6c 64 20 6e 6f 74 20 62 6c 6f 63 6b  should not block
2fbbe 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20 4f 74  , of course.  Ot
2fbbf 68 65 72 77 69 73 65 20 74 68 65 0a 2a 2a 20 69  herwise the.** i
2fbc0 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72  nitialization pr
2fbc1 6f 63 65 73 73 20 77 6f 75 6c 64 20 6e 65 76 65  ocess would neve
2fbc2 72 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a  r complete..**.*
2fbc3 2a 20 4c 65 74 20 58 20 62 65 20 74 68 65 20 66  * Let X be the f
2fbc4 69 72 73 74 20 74 68 72 65 61 64 20 74 6f 20 65  irst thread to e
2fbc5 6e 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  nter this routin
2fbc6 65 2e 20 20 4c 65 74 20 59 20 62 65 20 73 6f 6d  e.  Let Y be som
2fbc7 65 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61  e other.** threa
2fbc8 64 2e 20 20 54 68 65 6e 20 77 68 69 6c 65 20 74  d.  Then while t
2fbc9 68 65 20 69 6e 69 74 69 61 6c 20 69 6e 76 6f 63  he initial invoc
2fbca 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f  ation of this ro
2fbcb 75 74 69 6e 65 20 62 79 20 58 20 69 73 0a 2a 2a  utine by X is.**
2fbcc 20 69 6e 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20   incomplete, it 
2fbcd 69 73 20 72 65 71 75 69 72 65 64 20 74 68 61 74  is required that
2fbce 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 43 61  :.**.**    *  Ca
2fbcf 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74  lls to this rout
2fbd0 69 6e 65 20 66 72 6f 6d 20 59 20 6d 75 73 74 20  ine from Y must 
2fbd1 62 6c 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65 20  block until the 
2fbd2 6f 75 74 65 72 2d 6d 6f 73 74 0a 2a 2a 20 20 20  outer-most.**   
2fbd3 20 20 20 20 63 61 6c 6c 20 62 79 20 58 20 63 6f      call by X co
2fbd4 6d 70 6c 65 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  mpletes..**.**  
2fbd5 20 20 2a 20 20 52 65 63 75 72 73 69 76 65 20 63    *  Recursive c
2fbd6 61 6c 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75  alls to this rou
2fbd7 74 69 6e 65 20 66 72 6f 6d 20 74 68 72 65 61 64  tine from thread
2fbd8 20 58 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69   X return immedi
2fbd9 61 74 65 6c 79 0a 2a 2a 20 20 20 20 20 20 20 77  ately.**       w
2fbda 69 74 68 6f 75 74 20 62 6c 6f 63 6b 69 6e 67 2e  ithout blocking.
2fbdb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2fbdc 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  nt sqlite3_initi
2fbdd 61 6c 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 73  alize(void){.  s
2fbde 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d  qlite3_mutex *pM
2fbdf 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20  aster;          
2fbe0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2fbe1 68 65 20 6d 61 69 6e 20 73 74 61 74 69 63 20 6d  he main static m
2fbe2 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 63  utex */.  int rc
2fbe3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2fbe4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fbe5 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
2fbe6 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66   code */..#ifdef
2fbe7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
2fbe8 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2fbe9 77 73 64 5f 69 6e 69 74 28 34 30 39 36 2c 20 32  wsd_init(4096, 2
2fbea 34 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  4);.  if( rc!=SQ
2fbeb 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
2fbec 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65  eturn rc;.  }.#e
2fbed 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 53 51  ndif..  /* If SQ
2fbee 4c 69 74 65 20 69 73 20 61 6c 72 65 61 64 79 20  Lite is already 
2fbef 63 6f 6d 70 6c 65 74 65 6c 79 20 69 6e 69 74 69  completely initi
2fbf0 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20 74 68 69  alized, then thi
2fbf1 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 73  s call.  ** to s
2fbf2 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
2fbf3 65 28 29 20 73 68 6f 75 6c 64 20 62 65 20 61 20  e() should be a 
2fbf4 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 74 68 65 20  no-op.  But the 
2fbf5 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20  initialization. 
2fbf6 20 2a 2a 20 6d 75 73 74 20 62 65 20 63 6f 6d 70   ** must be comp
2fbf7 6c 65 74 65 2e 20 20 53 6f 20 69 73 49 6e 69 74  lete.  So isInit
2fbf8 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 73 65 74   must not be set
2fbf9 20 75 6e 74 69 6c 20 74 68 65 20 76 65 72 79 20   until the very 
2fbfa 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73  end.  ** of this
2fbfb 20 72 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20   routine..  */. 
2fbfc 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
2fbfd 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20  alConfig.isInit 
2fbfe 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
2fbff 4f 4b 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  OK;..  /* Make s
2fc00 75 72 65 20 74 68 65 20 6d 75 74 65 78 20 73 75  ure the mutex su
2fc01 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69  bsystem is initi
2fc02 61 6c 69 7a 65 64 2e 20 20 49 66 20 75 6e 61 62  alized.  If unab
2fc03 6c 65 20 74 6f 20 0a 20 20 2a 2a 20 69 6e 69 74  le to .  ** init
2fc04 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78  ialize the mutex
2fc05 20 73 75 62 73 79 73 74 65 6d 2c 20 72 65 74 75   subsystem, retu
2fc06 72 6e 20 65 61 72 6c 79 20 77 69 74 68 20 74 68  rn early with th
2fc07 65 20 65 72 72 6f 72 2e 0a 20 20 2a 2a 20 49 66  e error..  ** If
2fc08 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 73   the system is s
2fc09 6f 20 73 69 63 6b 20 74 68 61 74 20 77 65 20 61  o sick that we a
2fc0a 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c  re unable to all
2fc0b 6f 63 61 74 65 20 61 20 6d 75 74 65 78 2c 0a 20  ocate a mutex,. 
2fc0c 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74   ** there is not
2fc0d 20 6d 75 63 68 20 53 51 4c 69 74 65 20 69 73 20   much SQLite is 
2fc0e 67 6f 69 6e 67 20 74 6f 20 62 65 20 61 62 6c 65  going to be able
2fc0f 20 74 6f 20 64 6f 2e 0a 20 20 2a 2a 0a 20 20 2a   to do..  **.  *
2fc10 2a 20 54 68 65 20 6d 75 74 65 78 20 73 75 62 73  * The mutex subs
2fc11 79 73 74 65 6d 20 6d 75 73 74 20 74 61 6b 65 20  ystem must take 
2fc12 63 61 72 65 20 6f 66 20 73 65 72 69 61 6c 69 7a  care of serializ
2fc13 69 6e 67 20 69 74 73 20 6f 77 6e 0a 20 20 2a 2a  ing its own.  **
2fc14 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e   initialization.
2fc15 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
2fc16 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 3b  ite3MutexInit();
2fc17 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
2fc18 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 6e 69  rn rc;..  /* Ini
2fc19 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c  tialize the mall
2fc1a 6f 63 28 29 20 73 79 73 74 65 6d 20 61 6e 64 20  oc() system and 
2fc1b 74 68 65 20 72 65 63 75 72 73 69 76 65 20 70 49  the recursive pI
2fc1c 6e 69 74 4d 75 74 65 78 20 6d 75 74 65 78 2e 0a  nitMutex mutex..
2fc1d 20 20 2a 2a 20 54 68 69 73 20 6f 70 65 72 61 74    ** This operat
2fc1e 69 6f 6e 20 69 73 20 70 72 6f 74 65 63 74 65 64  ion is protected
2fc1f 20 62 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d   by the STATIC_M
2fc20 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 4e 6f  ASTER mutex.  No
2fc21 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 4d 75 74  te that.  ** Mut
2fc22 65 78 41 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c  exAlloc() is cal
2fc23 6c 65 64 20 66 6f 72 20 61 20 73 74 61 74 69 63  led for a static
2fc24 20 6d 75 74 65 78 20 70 72 69 6f 72 20 74 6f 20   mutex prior to 
2fc25 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
2fc26 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 73 75 62  .  ** malloc sub
2fc27 73 79 73 74 65 6d 20 2d 20 74 68 69 73 20 69 6d  system - this im
2fc28 70 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 61  plies that the a
2fc29 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 73  llocation of a s
2fc2a 74 61 74 69 63 0a 20 20 2a 2a 20 6d 75 74 65 78  tatic.  ** mutex
2fc2b 20 6d 75 73 74 20 6e 6f 74 20 72 65 71 75 69 72   must not requir
2fc2c 65 20 73 75 70 70 6f 72 74 20 66 72 6f 6d 20 74  e support from t
2fc2d 68 65 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73  he malloc subsys
2fc2e 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73  tem..  */.  pMas
2fc2f 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  ter = sqlite3Mut
2fc30 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
2fc31 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
2fc32 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ER);.  sqlite3_m
2fc33 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74  utex_enter(pMast
2fc34 65 72 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  er);.  if( !sqli
2fc35 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2fc36 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 29 7b 0a  isMallocInit ){.
2fc37 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2fc38 4d 61 6c 6c 6f 63 49 6e 69 74 28 29 3b 0a 20 20  MallocInit();.  
2fc39 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
2fc3a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
2fc3b 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2fc3c 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 3d 20  .isMallocInit = 
2fc3d 31 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69  1;.    if( !sqli
2fc3e 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2fc3f 70 49 6e 69 74 4d 75 74 65 78 20 29 7b 0a 20 20  pInitMutex ){.  
2fc40 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2fc41 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74  lConfig.pInitMut
2fc42 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
2fc43 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
2fc44 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a  TEX_RECURSIVE);.
2fc45 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2fc46 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
2fc47 6f 72 65 4d 75 74 65 78 20 26 26 20 21 73 71 6c  oreMutex && !sql
2fc48 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2fc49 2e 70 49 6e 69 74 4d 75 74 65 78 20 29 7b 0a 20  .pInitMutex ){. 
2fc4a 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
2fc4b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
2fc4c 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  }.    }.  }.  if
2fc4d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2fc4e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
2fc4f 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49  obalConfig.nRefI
2fc50 6e 69 74 4d 75 74 65 78 2b 2b 3b 0a 20 20 7d 0a  nitMutex++;.  }.
2fc51 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2fc52 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a  leave(pMaster);.
2fc53 0a 20 20 2f 2a 20 49 66 20 75 6e 61 62 6c 65 20  .  /* If unable 
2fc54 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  to initialize th
2fc55 65 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74  e malloc subsyst
2fc56 65 6d 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  em, then return 
2fc57 65 61 72 6c 79 2e 0a 20 20 2a 2a 20 54 68 65 72  early..  ** Ther
2fc58 65 20 69 73 20 6c 69 74 74 6c 65 20 68 6f 70 65  e is little hope
2fc59 20 6f 66 20 67 65 74 74 69 6e 67 20 53 51 4c 69   of getting SQLi
2fc5a 74 65 20 74 6f 20 72 75 6e 20 69 66 20 74 68 65  te to run if the
2fc5b 20 6d 61 6c 6c 6f 63 0a 20 20 2a 2a 20 73 75 62   malloc.  ** sub
2fc5c 73 79 73 74 65 6d 20 63 61 6e 6e 6f 74 20 62 65  system cannot be
2fc5d 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20   initialized..  
2fc5e 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  */.  if( rc!=SQL
2fc5f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
2fc60 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
2fc61 2f 2a 20 44 6f 20 74 68 65 20 72 65 73 74 20 6f  /* Do the rest o
2fc62 66 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61  f the initializa
2fc63 74 69 6f 6e 20 75 6e 64 65 72 20 74 68 65 20 72  tion under the r
2fc64 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 73  ecursive mutex s
2fc65 6f 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20 77  o.  ** that we w
2fc66 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 68  ill be able to h
2fc67 61 6e 64 6c 65 20 72 65 63 75 72 73 69 76 65 20  andle recursive 
2fc68 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20  calls into.  ** 
2fc69 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
2fc6a 7a 65 28 29 2e 20 20 54 68 65 20 72 65 63 75 72  ze().  The recur
2fc6b 73 69 76 65 20 63 61 6c 6c 73 20 6e 6f 72 6d 61  sive calls norma
2fc6c 6c 6c 79 20 63 6f 6d 65 20 74 68 72 6f 75 67 68  lly come through
2fc6d 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  .  ** sqlite3_os
2fc6e 5f 69 6e 69 74 28 29 20 77 68 65 6e 20 69 74 20  _init() when it 
2fc6f 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 5f  invokes sqlite3_
2fc70 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 2c 20  vfs_register(), 
2fc71 62 75 74 20 6f 74 68 65 72 0a 20 20 2a 2a 20 72  but other.  ** r
2fc72 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 6d  ecursive calls m
2fc73 69 67 68 74 20 61 6c 73 6f 20 62 65 20 70 6f 73  ight also be pos
2fc74 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71  sible..  */.  sq
2fc75 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
2fc76 72 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  r(sqlite3GlobalC
2fc77 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
2fc78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
2fc79 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
2fc7a 6e 69 74 3d 3d 30 20 26 26 20 73 71 6c 69 74 65  nit==0 && sqlite
2fc7b 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e  3GlobalConfig.in
2fc7c 50 72 6f 67 72 65 73 73 3d 3d 30 20 29 7b 0a 20  Progress==0 ){. 
2fc7d 20 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a     FuncDefHash *
2fc7e 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28  pHash = &GLOBAL(
2fc7f 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c  FuncDefHash, sql
2fc80 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69  ite3GlobalFuncti
2fc81 6f 6e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ons);.    sqlite
2fc82 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e  3GlobalConfig.in
2fc83 50 72 6f 67 72 65 73 73 20 3d 20 31 3b 0a 20 20  Progress = 1;.  
2fc84 20 20 6d 65 6d 73 65 74 28 70 48 61 73 68 2c 20    memset(pHash, 
2fc85 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65  0, sizeof(sqlite
2fc86 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73  3GlobalFunctions
2fc87 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  ));.    sqlite3R
2fc88 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e  egisterGlobalFun
2fc89 63 74 69 6f 6e 73 28 29 3b 0a 20 20 20 20 72 63  ctions();.    rc
2fc8a 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65   = sqlite3Pcache
2fc8b 49 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20  Initialize();.  
2fc8c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2fc8d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
2fc8e 3d 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  = sqlite3_os_ini
2fc8f 74 28 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  t();.    }.    i
2fc90 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2fc91 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2fc92 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65 74  3PCacheBufferSet
2fc93 75 70 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  up( sqlite3Globa
2fc94 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 2c 20 0a  lConfig.pPage, .
2fc95 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2fc96 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
2fc97 50 61 67 65 2c 20 73 71 6c 69 74 65 33 47 6c 6f  Page, sqlite3Glo
2fc98 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 29  balConfig.nPage)
2fc99 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ;.      sqlite3G
2fc9a 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e  lobalConfig.isIn
2fc9b 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  it = 1;.    }.  
2fc9c 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2fc9d 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73  onfig.inProgress
2fc9e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 0;.  }.  sqli
2fc9f 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
2fca0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2fca1 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b  fig.pInitMutex);
2fca2 0a 0a 20 20 2f 2a 20 47 6f 20 62 61 63 6b 20 75  ..  /* Go back u
2fca3 6e 64 65 72 20 74 68 65 20 73 74 61 74 69 63 20  nder the static 
2fca4 6d 75 74 65 78 20 61 6e 64 20 63 6c 65 61 6e 20  mutex and clean 
2fca5 75 70 20 74 68 65 20 72 65 63 75 72 73 69 76 65  up the recursive
2fca6 0a 20 20 2a 2a 20 6d 75 74 65 78 20 74 6f 20 70  .  ** mutex to p
2fca7 72 65 76 65 6e 74 20 61 20 72 65 73 6f 75 72 63  revent a resourc
2fca8 65 20 6c 65 61 6b 2e 0a 20 20 2a 2f 0a 20 20 73  e leak..  */.  s
2fca9 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2fcaa 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 73  er(pMaster);.  s
2fcab 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fcac 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78  ig.nRefInitMutex
2fcad 2d 2d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  --;.  if( sqlite
2fcae 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52  3GlobalConfig.nR
2fcaf 65 66 49 6e 69 74 4d 75 74 65 78 3c 3d 30 20 29  efInitMutex<=0 )
2fcb0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  {.    assert( sq
2fcb1 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2fcb2 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3d  g.nRefInitMutex=
2fcb3 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
2fcb4 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c  3_mutex_free(sql
2fcb5 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2fcb6 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a 20 20  .pInitMutex);.  
2fcb7 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2fcb8 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
2fcb9 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 0;.  }.  sqli
2fcba 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
2fcbb 70 4d 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a 20  pMaster);..  /* 
2fcbc 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73  The following is
2fcbd 20 6a 75 73 74 20 61 20 73 61 6e 69 74 79 20 63   just a sanity c
2fcbe 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72  heck to make sur
2fcbf 65 20 53 51 4c 69 74 65 20 68 61 73 0a 20 20 2a  e SQLite has.  *
2fcc0 2a 20 62 65 65 6e 20 63 6f 6d 70 69 6c 65 64 20  * been compiled 
2fcc1 63 6f 72 72 65 63 74 6c 79 2e 20 20 49 74 20 69  correctly.  It i
2fcc2 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 72  s important to r
2fcc3 75 6e 20 74 68 69 73 20 63 6f 64 65 2c 20 62 75  un this code, bu
2fcc4 74 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20  t.  ** we don't 
2fcc5 77 61 6e 74 20 74 6f 20 72 75 6e 20 69 74 20 74  want to run it t
2fcc6 6f 6f 20 6f 66 74 65 6e 20 61 6e 64 20 73 6f 61  oo often and soa
2fcc7 6b 20 75 70 20 43 50 55 20 63 79 63 6c 65 73 20  k up CPU cycles 
2fcc8 66 6f 72 20 6e 6f 0a 20 20 2a 2a 20 72 65 61 73  for no.  ** reas
2fcc9 6f 6e 2e 20 20 53 6f 20 77 65 20 72 75 6e 20 69  on.  So we run i
2fcca 74 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 69 6e  t once during in
2fccb 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20  itialization..  
2fccc 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
2fccd 47 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  G.#ifndef SQLITE
2fcce 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
2fccf 4f 49 4e 54 0a 20 20 2f 2a 20 54 68 69 73 20 73  OINT.  /* This s
2fcd0 65 63 74 69 6f 6e 20 6f 66 20 63 6f 64 65 27 73  ection of code's
2fcd1 20 6f 6e 6c 79 20 22 6f 75 74 70 75 74 22 20 69   only "output" i
2fcd2 73 20 76 69 61 20 61 73 73 65 72 74 28 29 20 73  s via assert() s
2fcd3 74 61 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20  tatements. */.  
2fcd4 69 66 20 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  if ( rc==SQLITE_
2fcd5 4f 4b 20 29 7b 0a 20 20 20 20 75 36 34 20 78 20  OK ){.    u64 x 
2fcd6 3d 20 28 28 28 75 36 34 29 31 29 3c 3c 36 33 29  = (((u64)1)<<63)
2fcd7 2d 31 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79  -1;.    double y
2fcd8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 73 69 7a  ;.    assert(siz
2fcd9 65 6f 66 28 78 29 3d 3d 38 29 3b 0a 20 20 20 20  eof(x)==8);.    
2fcda 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29  assert(sizeof(x)
2fcdb 3d 3d 73 69 7a 65 6f 66 28 79 29 29 3b 0a 20 20  ==sizeof(y));.  
2fcdc 20 20 6d 65 6d 63 70 79 28 26 79 2c 20 26 78 2c    memcpy(&y, &x,
2fcdd 20 38 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   8);.    assert(
2fcde 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 79 29   sqlite3IsNaN(y)
2fcdf 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23   );.  }.#endif.#
2fce0 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20  endif..  return 
2fce1 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64  rc;.}../*.** Und
2fce2 6f 20 74 68 65 20 65 66 66 65 63 74 73 20 6f 66  o the effects of
2fce3 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
2fce4 69 7a 65 28 29 2e 20 20 4d 75 73 74 20 6e 6f 74  ize().  Must not
2fce5 20 62 65 20 63 61 6c 6c 65 64 20 77 68 69 6c 65   be called while
2fce6 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6f 75  .** there are ou
2fce7 74 73 74 61 6e 64 69 6e 67 20 64 61 74 61 62 61  tstanding databa
2fce8 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f  se connections o
2fce9 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  r memory allocat
2fcea 69 6f 6e 73 20 6f 72 0a 2a 2a 20 77 68 69 6c 65  ions or.** while
2fceb 20 61 6e 79 20 70 61 72 74 20 6f 66 20 53 51 4c   any part of SQL
2fcec 69 74 65 20 69 73 20 6f 74 68 65 72 77 69 73 65  ite is otherwise
2fced 20 69 6e 20 75 73 65 20 69 6e 20 61 6e 79 20 74   in use in any t
2fcee 68 72 65 61 64 2e 20 20 54 68 69 73 0a 2a 2a 20  hread.  This.** 
2fcef 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 74  routine is not t
2fcf0 68 72 65 61 64 73 61 66 65 2e 20 20 42 75 74 20  hreadsafe.  But 
2fcf1 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 69 6e  it is safe to in
2fcf2 76 6f 6b 65 20 74 68 69 73 20 72 6f 75 74 69 6e  voke this routin
2fcf3 65 0a 2a 2a 20 6f 6e 20 77 68 65 6e 20 53 51 4c  e.** on when SQL
2fcf4 69 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 73  ite is already s
2fcf5 68 75 74 20 64 6f 77 6e 2e 20 20 49 66 20 53 51  hut down.  If SQ
2fcf6 4c 69 74 65 20 69 73 20 61 6c 72 65 61 64 79 20  Lite is already 
2fcf7 73 68 75 74 20 64 6f 77 6e 0a 2a 2a 20 77 68 65  shut down.** whe
2fcf8 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
2fcf9 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 6e 20  s invoked, then 
2fcfa 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
2fcfb 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  a harmless no-op
2fcfc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2fcfd 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74  int sqlite3_shut
2fcfe 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66  down(void){.  if
2fcff 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
2fd00 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29 7b 0a  onfig.isInit ){.
2fd01 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2fd02 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63  lConfig.isMalloc
2fd03 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 73 71  Init = 0;.    sq
2fd04 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64  lite3PcacheShutd
2fd05 6f 77 6e 28 29 3b 0a 20 20 20 20 73 71 6c 69 74  own();.    sqlit
2fd06 65 33 5f 6f 73 5f 65 6e 64 28 29 3b 0a 20 20 20  e3_os_end();.   
2fd07 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61   sqlite3_reset_a
2fd08 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 3b  uto_extension();
2fd09 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c  .    sqlite3Mall
2fd0a 6f 63 45 6e 64 28 29 3b 0a 20 20 20 20 73 71 6c  ocEnd();.    sql
2fd0b 69 74 65 33 4d 75 74 65 78 45 6e 64 28 29 3b 0a  ite3MutexEnd();.
2fd0c 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2fd0d 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 3d  lConfig.isInit =
2fd0e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
2fd0f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
2fd10 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 61 6c  *.** This API al
2fd11 6c 6f 77 73 20 61 70 70 6c 69 63 61 74 69 6f 6e  lows application
2fd12 73 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20  s to modify the 
2fd13 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61  global configura
2fd14 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 53  tion of.** the S
2fd15 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 61 74  QLite library at
2fd16 20 72 75 6e 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a   run-time..**.**
2fd17 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
2fd18 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c  ould only be cal
2fd19 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 61  led when there a
2fd1a 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e  re no outstandin
2fd1b 67 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  g.** database co
2fd1c 6e 6e 65 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d  nnections or mem
2fd1d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e  ory allocations.
2fd1e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
2fd1f 73 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73  s not.** threads
2fd20 61 66 65 2e 20 20 46 61 69 6c 75 72 65 20 74 6f  afe.  Failure to
2fd21 20 68 65 65 64 20 74 68 65 73 65 20 77 61 72 6e   heed these warn
2fd22 69 6e 67 73 20 63 61 6e 20 6c 65 61 64 20 74 6f  ings can lead to
2fd23 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 0a 2a   unpredictable.*
2fd24 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53  * behavior..*/.S
2fd25 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2fd26 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74  lite3_config(int
2fd27 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f   op, ...){.  va_
2fd28 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 72  list ap;.  int r
2fd29 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
2fd2a 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e    /* sqlite3_con
2fd2b 66 69 67 28 29 20 73 68 61 6c 6c 20 72 65 74 75  fig() shall retu
2fd2c 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
2fd2d 20 69 66 20 69 74 20 69 73 20 69 6e 76 6f 6b 65   if it is invoke
2fd2e 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68 65  d while.  ** the
2fd2f 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
2fd30 69 73 20 69 6e 20 75 73 65 2e 20 2a 2f 0a 20 20  is in use. */.  
2fd31 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
2fd32 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29  lConfig.isInit )
2fd33 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
2fd34 49 53 55 53 45 3b 0a 0a 20 20 76 61 5f 73 74 61  ISUSE;..  va_sta
2fd35 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77  rt(ap, op);.  sw
2fd36 69 74 63 68 28 20 6f 70 20 29 7b 0a 0a 20 20 20  itch( op ){..   
2fd37 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 66 69 67   /* Mutex config
2fd38 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
2fd39 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62  are only availab
2fd3a 6c 65 20 69 6e 20 61 20 74 68 72 65 61 64 73 61  le in a threadsa
2fd3b 66 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c  fe.    ** compil
2fd3c 65 2e 20 0a 20 20 20 20 2a 2f 0a 23 69 66 20 53  e. .    */.#if S
2fd3d 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2fd3e 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2fd3f 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
2fd40 52 45 41 44 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  READ: {.      /*
2fd41 20 44 69 73 61 62 6c 65 20 61 6c 6c 20 6d 75 74   Disable all mut
2fd42 65 78 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 73  exing */.      s
2fd43 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fd44 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20  ig.bCoreMutex = 
2fd45 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
2fd46 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75  GlobalConfig.bFu
2fd47 6c 6c 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20  llMutex = 0;.   
2fd48 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2fd49 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2fd4a 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45  CONFIG_MULTITHRE
2fd4b 41 44 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44  AD: {.      /* D
2fd4c 69 73 61 62 6c 65 20 6d 75 74 65 78 69 6e 67 20  isable mutexing 
2fd4d 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  of database conn
2fd4e 65 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 20  ections */.     
2fd4f 20 2f 2a 20 45 6e 61 62 6c 65 20 6d 75 74 65 78   /* Enable mutex
2fd50 69 6e 67 20 6f 66 20 63 6f 72 65 20 64 61 74 61  ing of core data
2fd51 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 20   structures */. 
2fd52 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2fd53 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75  alConfig.bCoreMu
2fd54 74 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 73  tex = 1;.      s
2fd55 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fd56 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20  ig.bFullMutex = 
2fd57 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  0;.      break;.
2fd58 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
2fd59 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52  QLITE_CONFIG_SER
2fd5a 49 41 4c 49 5a 45 44 3a 20 7b 0a 20 20 20 20 20  IALIZED: {.     
2fd5b 20 2f 2a 20 45 6e 61 62 6c 65 20 61 6c 6c 20 6d   /* Enable all m
2fd5c 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 20 20 20  utexing */.     
2fd5d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2fd5e 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20  nfig.bCoreMutex 
2fd5f 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 1;.      sqlit
2fd60 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
2fd61 46 75 6c 6c 4d 75 74 65 78 20 3d 20 31 3b 0a 20  FullMutex = 1;. 
2fd62 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2fd63 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
2fd64 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3a 20  E_CONFIG_MUTEX: 
2fd65 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69  {.      /* Speci
2fd66 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  fy an alternativ
2fd67 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
2fd68 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  tation */.      
2fd69 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2fd6a 66 69 67 2e 6d 75 74 65 78 20 3d 20 2a 76 61 5f  fig.mutex = *va_
2fd6b 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f  arg(ap, sqlite3_
2fd6c 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29 3b  mutex_methods*);
2fd6d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2fd6e 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
2fd6f 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55  ITE_CONFIG_GETMU
2fd70 54 45 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  TEX: {.      /* 
2fd71 52 65 74 72 69 65 76 65 20 74 68 65 20 63 75 72  Retrieve the cur
2fd72 72 65 6e 74 20 6d 75 74 65 78 20 69 6d 70 6c 65  rent mutex imple
2fd73 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  mentation */.   
2fd74 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73     *va_arg(ap, s
2fd75 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
2fd76 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65 33  hods*) = sqlite3
2fd77 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
2fd78 65 78 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ex;.      break;
2fd79 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a  .    }.#endif...
2fd7a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2fd7b 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3a 20 7b  CONFIG_MALLOC: {
2fd7c 0a 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66  .      /* Specif
2fd7d 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65  y an alternative
2fd7e 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e   malloc implemen
2fd7f 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  tation */.      
2fd80 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2fd81 66 69 67 2e 6d 20 3d 20 2a 76 61 5f 61 72 67 28  fig.m = *va_arg(
2fd82 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  ap, sqlite3_mem_
2fd83 6d 65 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20  methods*);.     
2fd84 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2fd85 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
2fd86 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 3a 20  NFIG_GETMALLOC: 
2fd87 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 74 72 69  {.      /* Retri
2fd88 65 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  eve the current 
2fd89 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d 65  malloc() impleme
2fd8a 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ntation */.     
2fd8b 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
2fd8c 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c  alConfig.m.xMall
2fd8d 6f 63 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 4d  oc==0 ) sqlite3M
2fd8e 65 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a  emSetDefault();.
2fd8f 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70        *va_arg(ap
2fd90 2c 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  , sqlite3_mem_me
2fd91 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65  thods*) = sqlite
2fd92 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 3b  3GlobalConfig.m;
2fd93 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2fd94 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
2fd95 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54  ITE_CONFIG_MEMST
2fd96 41 54 55 53 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  ATUS: {.      /*
2fd97 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62   Enable or disab
2fd98 6c 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73 74  le the malloc st
2fd99 61 74 75 73 20 63 6f 6c 6c 65 63 74 69 6f 6e 20  atus collection 
2fd9a 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2fd9b 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
2fd9c 6d 73 74 61 74 20 3d 20 76 61 5f 61 72 67 28 61  mstat = va_arg(a
2fd9d 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62  p, int);.      b
2fd9e 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2fd9f 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
2fda0 49 47 5f 53 43 52 41 54 43 48 3a 20 7b 0a 20 20  IG_SCRATCH: {.  
2fda1 20 20 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65      /* Designate
2fda2 20 61 20 62 75 66 66 65 72 20 66 6f 72 20 73 63   a buffer for sc
2fda3 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 73 70 61  ratch memory spa
2fda4 63 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ce */.      sqli
2fda5 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2fda6 70 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72  pScratch = va_ar
2fda7 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20  g(ap, void*);.  
2fda8 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2fda9 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
2fdaa 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  h = va_arg(ap, i
2fdab 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  nt);.      sqlit
2fdac 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
2fdad 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67  Scratch = va_arg
2fdae 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
2fdaf 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2fdb0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f    case SQLITE_CO
2fdb1 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 3a 20  NFIG_PAGECACHE: 
2fdb2 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67  {.      /* Desig
2fdb3 6e 61 74 65 20 61 20 62 75 66 66 65 72 20 66 6f  nate a buffer fo
2fdb4 72 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  r scratch memory
2fdb5 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20   space */.      
2fdb6 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2fdb7 66 69 67 2e 70 50 61 67 65 20 3d 20 76 61 5f 61  fig.pPage = va_a
2fdb8 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20  rg(ap, void*);. 
2fdb9 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2fdba 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 20  alConfig.szPage 
2fdbb 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
2fdbc 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2fdbd 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61  GlobalConfig.nPa
2fdbe 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ge = va_arg(ap, 
2fdbf 69 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  int);.      brea
2fdc0 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61  k;.    }..    ca
2fdc1 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  se SQLITE_CONFIG
2fdc2 5f 50 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20  _PCACHE: {.     
2fdc3 20 2f 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61   /* Specify an a
2fdc4 6c 74 65 72 6e 61 74 69 76 65 20 6d 61 6c 6c 6f  lternative mallo
2fdc5 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  c implementation
2fdc6 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2fdc7 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
2fdc8 61 63 68 65 20 3d 20 2a 76 61 5f 61 72 67 28 61  ache = *va_arg(a
2fdc9 70 2c 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  p, sqlite3_pcach
2fdca 65 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a 20 20 20  e_methods*);.   
2fdcb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2fdcc 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2fdcd 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48  _CONFIG_GETPCACH
2fdce 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73  E: {.      if( s
2fdcf 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2fdd0 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 3d  ig.pcache.xInit=
2fdd1 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
2fdd2 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65  lite3PCacheSetDe
2fdd3 66 61 75 6c 74 28 29 3b 0a 20 20 20 20 20 20 7d  fault();.      }
2fdd4 0a 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61  .      *va_arg(a
2fdd5 70 2c 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  p, sqlite3_pcach
2fdd6 65 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71  e_methods*) = sq
2fdd7 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2fdd8 67 2e 70 63 61 63 68 65 3b 0a 20 20 20 20 20 20  g.pcache;.      
2fdd9 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69  break;.    }..#i
2fdda 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
2fddb 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 29  _ENABLE_MEMSYS3)
2fddc 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
2fddd 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
2fdde 35 29 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  5).    case SQLI
2fddf 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 3a 20  TE_CONFIG_HEAP: 
2fde0 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67  {.      /* Desig
2fde1 6e 61 74 65 20 61 20 62 75 66 66 65 72 20 66 6f  nate a buffer fo
2fde2 72 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 73 70  r heap memory sp
2fde3 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ace */.      sql
2fde4 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2fde5 2e 70 48 65 61 70 20 3d 20 76 61 5f 61 72 67 28  .pHeap = va_arg(
2fde6 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20  ap, void*);.    
2fde7 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2fde8 6f 6e 66 69 67 2e 6e 48 65 61 70 20 3d 20 76 61  onfig.nHeap = va
2fde9 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
2fdea 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2fdeb 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 20 3d  alConfig.mnReq =
2fdec 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
2fded 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ;..      if( sql
2fdee 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2fdef 2e 70 48 65 61 70 3d 3d 30 20 29 7b 0a 20 20 20  .pHeap==0 ){.   
2fdf0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 68       /* If the h
2fdf1 65 61 70 20 70 6f 69 6e 74 65 72 20 69 73 20 4e  eap pointer is N
2fdf2 55 4c 4c 2c 20 74 68 65 6e 20 72 65 73 74 6f 72  ULL, then restor
2fdf3 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 69 6d 70  e the malloc imp
2fdf4 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 20 20 20 20  lementation.    
2fdf5 20 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 4e      ** back to N
2fdf6 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 74 6f 6f  ULL pointers too
2fdf7 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75  .  This will cau
2fdf8 73 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 74 6f  se the malloc to
2fdf9 20 67 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 62   go.        ** b
2fdfa 61 63 6b 20 74 6f 20 69 74 73 20 64 65 66 61 75  ack to its defau
2fdfb 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  lt implementatio
2fdfc 6e 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 69  n when sqlite3_i
2fdfd 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 0a 20  nitialize() is. 
2fdfe 20 20 20 20 20 20 20 2a 2a 20 72 75 6e 2e 0a 20         ** run.. 
2fdff 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2fe00 20 20 6d 65 6d 73 65 74 28 26 73 71 6c 69 74 65    memset(&sqlite
2fe01 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2c  3GlobalConfig.m,
2fe02 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74   0, sizeof(sqlit
2fe03 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
2fe04 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ));.      }else{
2fe05 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
2fe06 68 65 61 70 20 70 6f 69 6e 74 65 72 20 69 73 20  heap pointer is 
2fe07 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69  not NULL, then i
2fe08 6e 73 74 61 6c 6c 20 6f 6e 65 20 6f 66 20 74 68  nstall one of th
2fe09 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 65 6d  e.        ** mem
2fe0a 35 2e 63 2f 6d 65 6d 33 2e 63 20 6d 65 74 68 6f  5.c/mem3.c metho
2fe0b 64 73 2e 20 49 66 20 6e 65 69 74 68 65 72 20 45  ds. If neither E
2fe0c 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 6e 6f  NABLE_MEMSYS3 no
2fe0d 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 45 4e 41  r.        ** ENA
2fe0e 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64  BLE_MEMSYS5 is d
2fe0f 65 66 69 6e 65 64 2c 20 72 65 74 75 72 6e 20 61  efined, return a
2fe10 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 20 20 20  n error..       
2fe11 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20   ** the default 
2fe12 63 61 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20  case and return 
2fe13 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 20 20  an error..      
2fe14 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
2fe15 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
2fe16 33 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  3.        sqlite
2fe17 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20  3GlobalConfig.m 
2fe18 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74  = *sqlite3MemGet
2fe19 4d 65 6d 73 79 73 33 28 29 3b 0a 23 65 6e 64 69  Memsys3();.#endi
2fe1a 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
2fe1b 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 20  ENABLE_MEMSYS5. 
2fe1c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c         sqlite3Gl
2fe1d 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a  obalConfig.m = *
2fe1e 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d  sqlite3MemGetMem
2fe1f 73 79 73 35 28 29 3b 0a 23 65 6e 64 69 66 0a 20  sys5();.#endif. 
2fe20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
2fe21 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
2fe22 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
2fe23 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49  E_CONFIG_LOOKASI
2fe24 44 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  DE: {.      sqli
2fe25 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2fe26 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 76 61  szLookaside = va
2fe27 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
2fe28 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2fe29 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61 73  alConfig.nLookas
2fe2a 69 64 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  ide = va_arg(ap,
2fe2b 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65   int);.      bre
2fe2c 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64  ak;.    }..    d
2fe2d 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
2fe2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
2fe2f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  R;.      break;.
2fe30 20 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65      }.  }.  va_e
2fe31 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e  nd(ap);.  return
2fe32 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65   rc;.}../*.** Se
2fe33 74 20 75 70 20 74 68 65 20 6c 6f 6f 6b 61 73 69  t up the lookasi
2fe34 64 65 20 62 75 66 66 65 72 73 20 66 6f 72 20 61  de buffers for a
2fe35 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
2fe36 74 69 6f 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  tion..** Return 
2fe37 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
2fe38 63 65 73 73 2e 20 20 0a 2a 2a 20 49 66 20 6c 6f  cess.  .** If lo
2fe39 6f 6b 61 73 69 64 65 20 69 73 20 61 6c 72 65 61  okaside is alrea
2fe3a 64 79 20 61 63 74 69 76 65 2c 20 72 65 74 75 72  dy active, retur
2fe3b 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a  n SQLITE_BUSY..*
2fe3c 2a 0a 2a 2a 20 54 68 65 20 73 7a 20 70 61 72 61  *.** The sz para
2fe3d 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e 75 6d  meter is the num
2fe3e 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
2fe3f 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 73  each lookaside s
2fe40 6c 6f 74 2e 0a 2a 2a 20 54 68 65 20 63 6e 74 20  lot..** The cnt 
2fe41 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
2fe42 20 6e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   number of slots
2fe43 2e 20 20 49 66 20 70 53 74 61 72 74 20 69 73 20  .  If pStart is 
2fe44 4e 55 4c 4c 20 74 68 65 0a 2a 2a 20 73 70 61 63  NULL the.** spac
2fe45 65 20 66 6f 72 20 74 68 65 20 6c 6f 6f 6b 61 73  e for the lookas
2fe46 69 64 65 20 6d 65 6d 6f 72 79 20 69 73 20 6f 62  ide memory is ob
2fe47 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
2fe48 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  te3_malloc()..**
2fe49 20 49 66 20 70 53 74 61 72 74 20 69 73 20 6e 6f   If pStart is no
2fe4a 74 20 4e 55 4c 4c 20 74 68 65 6e 20 69 74 20 69  t NULL then it i
2fe4b 73 20 73 7a 2a 63 6e 74 20 62 79 74 65 73 20 6f  s sz*cnt bytes o
2fe4c 66 20 6d 65 6d 6f 72 79 20 74 6f 20 75 73 65 20  f memory to use 
2fe4d 66 6f 72 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61  for.** the looka
2fe4e 73 69 64 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  side memory..*/.
2fe4f 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 75 70  static int setup
2fe50 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 65  Lookaside(sqlite
2fe51 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 42 75  3 *db, void *pBu
2fe52 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 63  f, int sz, int c
2fe53 6e 74 29 7b 0a 20 20 76 6f 69 64 20 2a 70 53 74  nt){.  void *pSt
2fe54 61 72 74 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6c  art;.  if( db->l
2fe55 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 20 29 7b  ookaside.nOut ){
2fe56 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2fe57 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 2f  TE_BUSY;.  }.  /
2fe58 2a 20 46 72 65 65 20 61 6e 79 20 65 78 69 73 74  * Free any exist
2fe59 69 6e 67 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ing lookaside bu
2fe5a 66 66 65 72 20 66 6f 72 20 74 68 69 73 20 68 61  ffer for this ha
2fe5b 6e 64 6c 65 20 62 65 66 6f 72 65 0a 20 20 2a 2a  ndle before.  **
2fe5c 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65   allocating a ne
2fe5d 77 20 6f 6e 65 20 73 6f 20 77 65 20 64 6f 6e 27  w one so we don'
2fe5e 74 20 68 61 76 65 20 74 6f 20 68 61 76 65 20 73  t have to have s
2fe5f 70 61 63 65 20 66 6f 72 20 0a 20 20 2a 2a 20 62  pace for .  ** b
2fe60 6f 74 68 20 61 74 20 74 68 65 20 73 61 6d 65 20  oth at the same 
2fe61 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  time..  */.  if(
2fe62 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
2fe63 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20 20  Malloced ){.    
2fe64 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62 2d  sqlite3_free(db-
2fe65 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72  >lookaside.pStar
2fe66 74 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 65  t);.  }.  /* The
2fe67 20 73 69 7a 65 20 6f 66 20 61 20 6c 6f 6f 6b 61   size of a looka
2fe68 73 69 64 65 20 73 6c 6f 74 20 6e 65 65 64 73 20  side slot needs 
2fe69 74 6f 20 62 65 20 6c 61 72 67 65 72 20 74 68 61  to be larger tha
2fe6a 6e 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a  n a pointer.  **
2fe6b 20 74 6f 20 62 65 20 75 73 65 66 75 6c 2e 0a 20   to be useful.. 
2fe6c 20 2a 2f 0a 20 20 69 66 28 20 73 7a 3c 3d 28 69   */.  if( sz<=(i
2fe6d 6e 74 29 73 69 7a 65 6f 66 28 4c 6f 6f 6b 61 73  nt)sizeof(Lookas
2fe6e 69 64 65 53 6c 6f 74 2a 29 20 29 20 73 7a 20 3d  ideSlot*) ) sz =
2fe6f 20 30 3b 0a 20 20 69 66 28 20 63 6e 74 3c 30 20   0;.  if( cnt<0 
2fe70 29 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28  ) cnt = 0;.  if(
2fe71 20 73 7a 3d 3d 30 20 7c 7c 20 63 6e 74 3d 3d 30   sz==0 || cnt==0
2fe72 20 29 7b 0a 20 20 20 20 73 7a 20 3d 20 30 3b 0a   ){.    sz = 0;.
2fe73 20 20 20 20 70 53 74 61 72 74 20 3d 20 30 3b 0a      pStart = 0;.
2fe74 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 75 66    }else if( pBuf
2fe75 3d 3d 30 20 29 7b 0a 20 20 20 20 73 7a 20 3d 20  ==0 ){.    sz = 
2fe76 52 4f 55 4e 44 38 28 73 7a 29 3b 0a 20 20 20 20  ROUND8(sz);.    
2fe77 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
2fe78 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
2fe79 70 53 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33  pStart = sqlite3
2fe7a 4d 61 6c 6c 6f 63 28 20 73 7a 2a 63 6e 74 20 29  Malloc( sz*cnt )
2fe7b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64  ;.    sqlite3End
2fe7c 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
2fe7d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 7a 20    }else{.    sz 
2fe7e 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a 29  = ROUNDDOWN8(sz)
2fe7f 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20 70  ;.    pStart = p
2fe80 42 75 66 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 6c  Buf;.  }.  db->l
2fe81 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 20  ookaside.pStart 
2fe82 3d 20 70 53 74 61 72 74 3b 0a 20 20 64 62 2d 3e  = pStart;.  db->
2fe83 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20  lookaside.pFree 
2fe84 3d 20 30 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61  = 0;.  db->looka
2fe85 73 69 64 65 2e 73 7a 20 3d 20 28 75 31 36 29 73  side.sz = (u16)s
2fe86 7a 3b 0a 20 20 69 66 28 20 70 53 74 61 72 74 20  z;.  if( pStart 
2fe87 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
2fe88 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20    LookasideSlot 
2fe89 2a 70 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  *p;.    assert( 
2fe8a 73 7a 20 3e 20 28 69 6e 74 29 73 69 7a 65 6f 66  sz > (int)sizeof
2fe8b 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29  (LookasideSlot*)
2fe8c 20 29 3b 0a 20 20 20 20 70 20 3d 20 28 4c 6f 6f   );.    p = (Loo
2fe8d 6b 61 73 69 64 65 53 6c 6f 74 2a 29 70 53 74 61  kasideSlot*)pSta
2fe8e 72 74 3b 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e  rt;.    for(i=cn
2fe8f 74 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  t-1; i>=0; i--){
2fe90 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20  .      p->pNext 
2fe91 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  = db->lookaside.
2fe92 70 46 72 65 65 3b 0a 20 20 20 20 20 20 64 62 2d  pFree;.      db-
2fe93 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
2fe94 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 20 3d 20   = p;.      p = 
2fe95 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29  (LookasideSlot*)
2fe96 26 28 28 75 38 2a 29 70 29 5b 73 7a 5d 3b 0a 20  &((u8*)p)[sz];. 
2fe97 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f     }.    db->loo
2fe98 6b 61 73 69 64 65 2e 70 45 6e 64 20 3d 20 70 3b  kaside.pEnd = p;
2fe99 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  .    db->lookasi
2fe9a 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 31 3b  de.bEnabled = 1;
2fe9b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  .    db->lookasi
2fe9c 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 3d 20 70  de.bMalloced = p
2fe9d 42 75 66 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 7d  Buf==0 ?1:0;.  }
2fe9e 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6c 6f  else{.    db->lo
2fe9f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 20 3d 20 30  okaside.pEnd = 0
2fea0 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
2fea1 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30  ide.bEnabled = 0
2fea2 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
2fea3 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 3d 20  ide.bMalloced = 
2fea4 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
2fea5 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2fea6 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d  .** Return the m
2fea7 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  utex associated 
2fea8 77 69 74 68 20 61 20 64 61 74 61 62 61 73 65 20  with a database 
2fea9 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  connection..*/.S
2feaa 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
2feab 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33  3_mutex *sqlite3
2feac 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65  _db_mutex(sqlite
2fead 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e  3 *db){.  return
2feae 20 64 62 2d 3e 6d 75 74 65 78 3b 0a 7d 0a 0a 2f   db->mutex;.}../
2feaf 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69  *.** Configurati
2feb0 6f 6e 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20  on settings for 
2feb1 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 64 61  an individual da
2feb2 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2feb3 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  n.*/.SQLITE_API 
2feb4 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 63  int sqlite3_db_c
2feb5 6f 6e 66 69 67 28 73 71 6c 69 74 65 33 20 2a 64  onfig(sqlite3 *d
2feb6 62 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b  b, int op, ...){
2feb7 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
2feb8 20 69 6e 74 20 72 63 3b 0a 20 20 76 61 5f 73 74   int rc;.  va_st
2feb9 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73  art(ap, op);.  s
2feba 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
2febb 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 43   case SQLITE_DBC
2febc 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a  ONFIG_LOOKASIDE:
2febd 20 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70   {.      void *p
2febe 42 75 66 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  Buf = va_arg(ap,
2febf 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 69   void*);.      i
2fec0 6e 74 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61  nt sz = va_arg(a
2fec1 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 69  p, int);.      i
2fec2 6e 74 20 63 6e 74 20 3d 20 76 61 5f 61 72 67 28  nt cnt = va_arg(
2fec3 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
2fec4 72 63 20 3d 20 73 65 74 75 70 4c 6f 6f 6b 61 73  rc = setupLookas
2fec5 69 64 65 28 64 62 2c 20 70 42 75 66 2c 20 73 7a  ide(db, pBuf, sz
2fec6 2c 20 63 6e 74 29 3b 0a 20 20 20 20 20 20 62 72  , cnt);.      br
2fec7 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
2fec8 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
2fec9 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
2feca 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  R;.      break;.
2fecb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65      }.  }.  va_e
2fecc 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e  nd(ap);.  return
2fecd 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52   rc;.}.../*.** R
2fece 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
2fecf 65 20 62 75 66 66 65 72 20 7a 5b 30 2e 2e 6e 2d  e buffer z[0..n-
2fed0 31 5d 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20  1] contains all 
2fed1 73 70 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  spaces..*/.stati
2fed2 63 20 69 6e 74 20 61 6c 6c 53 70 61 63 65 73 28  c int allSpaces(
2fed3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
2fed4 6e 74 20 6e 29 7b 0a 20 20 77 68 69 6c 65 28 20  nt n){.  while( 
2fed5 6e 3e 30 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27  n>0 && z[n-1]=='
2fed6 20 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 72   ' ){ n--; }.  r
2fed7 65 74 75 72 6e 20 6e 3d 3d 30 3b 0a 7d 0a 0a 2f  eturn n==0;.}../
2fed8 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
2fed9 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69   default collati
2feda 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  ng function name
2fedb 64 20 22 42 49 4e 41 52 59 22 20 77 68 69 63 68  d "BINARY" which
2fedc 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 76   is always.** av
2fedd 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  ailable..**.** I
2fede 66 20 74 68 65 20 70 61 64 46 6c 61 67 20 61 72  f the padFlag ar
2fedf 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55  gument is not NU
2fee0 4c 4c 20 74 68 65 6e 20 73 70 61 63 65 20 70 61  LL then space pa
2fee1 64 64 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64  dding at the end
2fee2 0a 2a 2a 20 6f 66 20 73 74 72 69 6e 67 73 20 69  .** of strings i
2fee3 73 20 69 67 6e 6f 72 65 64 2e 20 20 54 68 69 73  s ignored.  This
2fee4 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
2fee5 52 54 52 49 4d 20 63 6f 6c 6c 61 74 69 6f 6e 2e  RTRIM collation.
2fee6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
2fee7 69 6e 43 6f 6c 6c 46 75 6e 63 28 0a 20 20 76 6f  inCollFunc(.  vo
2fee8 69 64 20 2a 70 61 64 46 6c 61 67 2c 0a 20 20 69  id *padFlag,.  i
2fee9 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20  nt nKey1, const 
2feea 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20 69  void *pKey1,.  i
2feeb 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20  nt nKey2, const 
2feec 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20  void *pKey2.){. 
2feed 20 69 6e 74 20 72 63 2c 20 6e 3b 0a 20 20 6e 20   int rc, n;.  n 
2feee 3d 20 6e 4b 65 79 31 3c 6e 4b 65 79 32 20 3f 20  = nKey1<nKey2 ? 
2feef 6e 4b 65 79 31 20 3a 20 6e 4b 65 79 32 3b 0a 20  nKey1 : nKey2;. 
2fef0 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4b 65   rc = memcmp(pKe
2fef1 79 31 2c 20 70 4b 65 79 32 2c 20 6e 29 3b 0a 20  y1, pKey2, n);. 
2fef2 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20   if( rc==0 ){.  
2fef3 20 20 69 66 28 20 70 61 64 46 6c 61 67 0a 20 20    if( padFlag.  
2fef4 20 20 20 26 26 20 61 6c 6c 53 70 61 63 65 73 28     && allSpaces(
2fef5 28 28 63 68 61 72 2a 29 70 4b 65 79 31 29 2b 6e  ((char*)pKey1)+n
2fef6 2c 20 6e 4b 65 79 31 2d 6e 29 0a 20 20 20 20 20  , nKey1-n).     
2fef7 26 26 20 61 6c 6c 53 70 61 63 65 73 28 28 28 63  && allSpaces(((c
2fef8 68 61 72 2a 29 70 4b 65 79 32 29 2b 6e 2c 20 6e  har*)pKey2)+n, n
2fef9 4b 65 79 32 2d 6e 29 0a 20 20 20 20 29 7b 0a 20  Key2-n).    ){. 
2fefa 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63       /* Leave rc
2fefb 20 75 6e 63 68 61 6e 67 65 64 20 61 74 20 30 20   unchanged at 0 
2fefc 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  */.    }else{.  
2fefd 20 20 20 20 72 63 20 3d 20 6e 4b 65 79 31 20 2d      rc = nKey1 -
2fefe 20 6e 4b 65 79 32 3b 0a 20 20 20 20 7d 0a 20 20   nKey2;.    }.  
2feff 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
2ff00 0a 0a 2f 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20  ../*.** Another 
2ff01 62 75 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69  built-in collati
2ff02 6e 67 20 73 65 71 75 65 6e 63 65 3a 20 4e 4f 43  ng sequence: NOC
2ff03 41 53 45 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ASE. .**.** This
2ff04 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2ff05 6e 63 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  nce is intended 
2ff06 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 22  to be used for "
2ff07 63 61 73 65 20 69 6e 64 65 70 65 6e 64 61 6e 74  case independant
2ff08 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 22 2e  .** comparison".
2ff09 20 53 51 4c 69 74 65 27 73 20 6b 6e 6f 77 6c 65   SQLite's knowle
2ff0a 64 67 65 20 6f 66 20 75 70 70 65 72 20 61 6e 64  dge of upper and
2ff0b 20 6c 6f 77 65 72 20 63 61 73 65 20 65 71 75 69   lower case equi
2ff0c 76 61 6c 65 6e 74 73 0a 2a 2a 20 65 78 74 65 6e  valents.** exten
2ff0d 64 73 20 6f 6e 6c 79 20 74 6f 20 74 68 65 20 32  ds only to the 2
2ff0e 36 20 63 68 61 72 61 63 74 65 72 73 20 75 73 65  6 characters use
2ff0f 64 20 69 6e 20 74 68 65 20 45 6e 67 6c 69 73 68  d in the English
2ff10 20 6c 61 6e 67 75 61 67 65 2e 0a 2a 2a 0a 2a 2a   language..**.**
2ff11 20 41 74 20 74 68 65 20 6d 6f 6d 65 6e 74 20 74   At the moment t
2ff12 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 55  here is only a U
2ff13 54 46 2d 38 20 69 6d 70 6c 65 6d 65 6e 74 61 74  TF-8 implementat
2ff14 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
2ff15 6e 74 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69  nt nocaseCollati
2ff16 6e 67 46 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a  ngFunc(.  void *
2ff17 4e 6f 74 55 73 65 64 2c 0a 20 20 69 6e 74 20 6e  NotUsed,.  int n
2ff18 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64  Key1, const void
2ff19 20 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e   *pKey1,.  int n
2ff1a 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64  Key2, const void
2ff1b 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74   *pKey2.){.  int
2ff1c 20 72 20 3d 20 73 71 6c 69 74 65 33 53 74 72 4e   r = sqlite3StrN
2ff1d 49 43 6d 70 28 0a 20 20 20 20 20 20 28 63 6f 6e  ICmp(.      (con
2ff1e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 31 2c  st char *)pKey1,
2ff1f 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
2ff20 4b 65 79 32 2c 20 28 6e 4b 65 79 31 3c 6e 4b 65  Key2, (nKey1<nKe
2ff21 79 32 29 3f 6e 4b 65 79 31 3a 6e 4b 65 79 32 29  y2)?nKey1:nKey2)
2ff22 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
2ff23 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
2ff24 20 69 66 28 20 30 3d 3d 72 20 29 7b 0a 20 20 20   if( 0==r ){.   
2ff25 20 72 20 3d 20 6e 4b 65 79 31 2d 6e 4b 65 79 32   r = nKey1-nKey2
2ff26 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
2ff27 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
2ff28 6e 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20 74  n the ROWID of t
2ff29 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69  he most recent i
2ff2a 6e 73 65 72 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nsert.*/.SQLITE_
2ff2b 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  API sqlite_int64
2ff2c 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
2ff2d 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 74  sert_rowid(sqlit
2ff2e 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72  e3 *db){.  retur
2ff2f 6e 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b  n db->lastRowid;
2ff30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2ff31 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
2ff32 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f  hanges in the mo
2ff33 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74  st recent call t
2ff34 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  o sqlite3_exec()
2ff35 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2ff36 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e  int sqlite3_chan
2ff37 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ges(sqlite3 *db)
2ff38 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e  {.  return db->n
2ff39 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Change;.}../*.**
2ff3a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
2ff3b 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 73 69  er of changes si
2ff3c 6e 63 65 20 74 68 65 20 64 61 74 61 62 61 73 65  nce the database
2ff3d 20 68 61 6e 64 6c 65 20 77 61 73 20 6f 70 65 6e   handle was open
2ff3e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
2ff3f 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f  I int sqlite3_to
2ff40 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69  tal_changes(sqli
2ff41 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75  te3 *db){.  retu
2ff42 72 6e 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61  rn db->nTotalCha
2ff43 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  nge;.}../*.** Cl
2ff44 6f 73 65 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76  ose all open sav
2ff45 65 70 6f 69 6e 74 73 2e 20 54 68 69 73 20 66 75  epoints. This fu
2ff46 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 6d 61 6e 69  nction only mani
2ff47 70 75 6c 61 74 65 73 20 66 69 65 6c 64 73 20 6f  pulates fields o
2ff48 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
2ff49 65 20 68 61 6e 64 6c 65 20 6f 62 6a 65 63 74 2c  e handle object,
2ff4a 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 63 6c 6f   it does not clo
2ff4b 73 65 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74  se any savepoint
2ff4c 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 6f 70  s that may be op
2ff4d 65 6e 0a 2a 2a 20 61 74 20 74 68 65 20 62 2d 74  en.** at the b-t
2ff4e 72 65 65 2f 70 61 67 65 72 20 6c 65 76 65 6c 2e  ree/pager level.
2ff4f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2ff50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
2ff51 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 73  loseSavepoints(s
2ff52 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 77  qlite3 *db){.  w
2ff53 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 65 70  hile( db->pSavep
2ff54 6f 69 6e 74 20 29 7b 0a 20 20 20 20 53 61 76 65  oint ){.    Save
2ff55 70 6f 69 6e 74 20 2a 70 54 6d 70 20 3d 20 64 62  point *pTmp = db
2ff56 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20  ->pSavepoint;.  
2ff57 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74    db->pSavepoint
2ff58 20 3d 20 70 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a   = pTmp->pNext;.
2ff59 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2ff5a 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20 7d  e(db, pTmp);.  }
2ff5b 0a 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e  .  db->nSavepoin
2ff5c 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 6e 53 74  t = 0;.  db->nSt
2ff5d 61 74 65 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 64  atement = 0;.  d
2ff5e 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  b->isTransaction
2ff5f 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 7d  Savepoint = 0;.}
2ff60 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e  ../*.** Close an
2ff61 20 65 78 69 73 74 69 6e 67 20 53 51 4c 69 74 65   existing SQLite
2ff62 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a 53 51 4c   database.*/.SQL
2ff63 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2ff64 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65  te3_close(sqlite
2ff65 33 20 2a 64 62 29 7b 0a 20 20 48 61 73 68 45 6c  3 *db){.  HashEl
2ff66 65 6d 20 2a 69 3b 0a 20 20 69 6e 74 20 6a 3b 0a  em *i;.  int j;.
2ff67 0a 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20  .  if( !db ){.  
2ff68 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2ff69 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73  OK;.  }.  if( !s
2ff6a 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
2ff6b 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b  kSickOrOk(db) ){
2ff6c 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2ff6d 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
2ff6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2ff6f 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2ff70 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2ff71 53 53 45 0a 20 20 7b 0a 20 20 20 20 65 78 74 65  SSE.  {.    exte
2ff72 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  rn void sqlite3S
2ff73 73 65 43 6c 65 61 6e 75 70 28 73 71 6c 69 74 65  seCleanup(sqlite
2ff74 33 2a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  3*);.    sqlite3
2ff75 53 73 65 43 6c 65 61 6e 75 70 28 64 62 29 3b 0a  SseCleanup(db);.
2ff76 20 20 7d 0a 23 65 6e 64 69 66 20 0a 0a 20 20 73    }.#endif ..  s
2ff77 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
2ff78 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
2ff79 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61  ;..  /* If a tra
2ff7a 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
2ff7b 2c 20 74 68 65 20 52 65 73 65 74 49 6e 74 65 72  , the ResetInter
2ff7c 6e 61 6c 53 63 68 65 6d 61 28 29 20 63 61 6c 6c  nalSchema() call
2ff7d 20 61 62 6f 76 65 0a 20 20 2a 2a 20 77 69 6c 6c   above.  ** will
2ff7e 20 6e 6f 74 20 68 61 76 65 20 63 61 6c 6c 65 64   not have called
2ff7f 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65 63 74   the xDisconnect
2ff80 28 29 20 6d 65 74 68 6f 64 20 6f 6e 20 61 6e 79  () method on any
2ff81 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61   virtual.  ** ta
2ff82 62 6c 65 73 20 69 6e 20 74 68 65 20 64 62 2d 3e  bles in the db->
2ff83 61 56 54 72 61 6e 73 5b 5d 20 61 72 72 61 79 2e  aVTrans[] array.
2ff84 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
2ff85 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
2ff86 63 6b 28 29 0a 20 20 2a 2a 20 63 61 6c 6c 20 77  ck().  ** call w
2ff87 69 6c 6c 20 64 6f 20 73 6f 2e 20 57 65 20 6e 65  ill do so. We ne
2ff88 65 64 20 74 6f 20 64 6f 20 74 68 69 73 20 62 65  ed to do this be
2ff89 66 6f 72 65 20 74 68 65 20 63 68 65 63 6b 20 66  fore the check f
2ff8a 6f 72 20 61 63 74 69 76 65 0a 20 20 2a 2a 20 53  or active.  ** S
2ff8b 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65  QL statements be
2ff8c 6c 6f 77 2c 20 61 73 20 74 68 65 20 76 2d 74 61  low, as the v-ta
2ff8d 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
2ff8e 6f 6e 20 6d 61 79 20 62 65 20 73 74 6f 72 69 6e  on may be storin
2ff8f 67 0a 20 20 2a 2a 20 73 6f 6d 65 20 70 72 65 70  g.  ** some prep
2ff90 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
2ff91 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f  internally..  */
2ff92 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  .  sqlite3VtabRo
2ff93 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 0a 20 20 2f  llback(db);..  /
2ff94 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61  * If there are a
2ff95 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 56  ny outstanding V
2ff96 4d 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  Ms, return SQLIT
2ff97 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 69 66 28  E_BUSY. */.  if(
2ff98 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20   db->pVdbe ){.  
2ff99 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2ff9a 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20  b, SQLITE_BUSY, 
2ff9b 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65  .        "unable
2ff9c 20 74 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f   to close due to
2ff9d 20 75 6e 66 69 6e 61 6c 69 73 65 64 20 73 74 61   unfinalised sta
2ff9e 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 73  tements");.    s
2ff9f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
2ffa0 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
2ffa1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2ffa2 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 61 73 73  _BUSY;.  }.  ass
2ffa3 65 72 74 28 20 73 71 6c 69 74 65 33 53 61 66 65  ert( sqlite3Safe
2ffa4 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
2ffa5 64 62 29 20 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d  db) );..  for(j=
2ffa6 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b  0; j<db->nDb; j+
2ffa7 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70  +){.    Btree *p
2ffa8 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e  Bt = db->aDb[j].
2ffa9 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74  pBt;.    if( pBt
2ffaa 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65   && sqlite3Btree
2ffab 49 73 49 6e 42 61 63 6b 75 70 28 70 42 74 29 20  IsInBackup(pBt) 
2ffac 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2ffad 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
2ffae 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20  _BUSY, .        
2ffaf 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 63 6c 6f    "unable to clo
2ffb0 73 65 20 64 75 65 20 74 6f 20 75 6e 66 69 6e 69  se due to unfini
2ffb1 73 68 65 64 20 62 61 63 6b 75 70 20 6f 70 65 72  shed backup oper
2ffb2 61 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 73  ation");.      s
2ffb3 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
2ffb4 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
2ffb5 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2ffb6 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20  TE_BUSY;.    }. 
2ffb7 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e   }..  /* Free an
2ffb8 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 53 61  y outstanding Sa
2ffb9 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72  vepoint structur
2ffba 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
2ffbb 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28  CloseSavepoints(
2ffbc 64 62 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b  db);..  for(j=0;
2ffbd 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29   j<db->nDb; j++)
2ffbe 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20  {.    struct Db 
2ffbf 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b  *pDb = &db->aDb[
2ffc0 6a 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d  j];.    if( pDb-
2ffc1 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 73 71  >pBt ){.      sq
2ffc2 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
2ffc3 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20  pDb->pBt);.     
2ffc4 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20   pDb->pBt = 0;. 
2ffc5 20 20 20 20 20 69 66 28 20 6a 21 3d 31 20 29 7b       if( j!=1 ){
2ffc6 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 70 53  .        pDb->pS
2ffc7 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20 20 20  chema = 0;.     
2ffc8 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73   }.    }.  }.  s
2ffc9 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
2ffca 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
2ffcb 3b 0a 0a 20 20 2f 2a 20 54 65 6c 6c 20 74 68 65  ;..  /* Tell the
2ffcc 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66 79 2e   code in notify.
2ffcd 63 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65  c that the conne
2ffce 63 74 69 6f 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20  ction no longer 
2ffcf 68 6f 6c 64 73 20 61 6e 79 0a 20 20 2a 2a 20 6c  holds any.  ** l
2ffd0 6f 63 6b 73 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ocks and does no
2ffd1 74 20 72 65 71 75 69 72 65 20 61 6e 79 20 66 75  t require any fu
2ffd2 72 74 68 65 72 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  rther unlock-not
2ffd3 69 66 79 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20  ify callbacks.. 
2ffd4 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43 6f 6e   */.  sqlite3Con
2ffd5 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 64 62  nectionClosed(db
2ffd6 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  );..  assert( db
2ffd7 2d 3e 6e 44 62 3c 3d 32 20 29 3b 0a 20 20 61 73  ->nDb<=2 );.  as
2ffd8 73 65 72 74 28 20 64 62 2d 3e 61 44 62 3d 3d 64  sert( db->aDb==d
2ffd9 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 3b 0a  b->aDbStatic );.
2ffda 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72    for(j=0; j<Arr
2ffdb 61 79 53 69 7a 65 28 64 62 2d 3e 61 46 75 6e 63  aySize(db->aFunc
2ffdc 2e 61 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 46  .a); j++){.    F
2ffdd 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 2c 20 2a  uncDef *pNext, *
2ffde 70 48 61 73 68 2c 20 2a 70 3b 0a 20 20 20 20 66  pHash, *p;.    f
2ffdf 6f 72 28 70 3d 64 62 2d 3e 61 46 75 6e 63 2e 61  or(p=db->aFunc.a
2ffe0 5b 6a 5d 3b 20 70 3b 20 70 3d 70 48 61 73 68 29  [j]; p; p=pHash)
2ffe1 7b 0a 20 20 20 20 20 20 70 48 61 73 68 20 3d 20  {.      pHash = 
2ffe2 70 2d 3e 70 48 61 73 68 3b 0a 20 20 20 20 20 20  p->pHash;.      
2ffe3 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
2ffe4 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70      pNext = p->p
2ffe5 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71  Next;.        sq
2ffe6 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ffe7 70 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20  p);.        p = 
2ffe8 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
2ffe9 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
2ffea 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
2ffeb 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b  (&db->aCollSeq);
2ffec 20 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68   i; i=sqliteHash
2ffed 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 43 6f  Next(i)){.    Co
2ffee 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28  llSeq *pColl = (
2ffef 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69 74 65  CollSeq *)sqlite
2fff0 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20  HashData(i);.   
2fff1 20 2f 2a 20 49 6e 76 6f 6b 65 20 61 6e 79 20 64   /* Invoke any d
2fff2 65 73 74 72 75 63 74 6f 72 73 20 72 65 67 69 73  estructors regis
2fff3 74 65 72 65 64 20 66 6f 72 20 63 6f 6c 6c 61 74  tered for collat
2fff4 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65  ion sequence use
2fff5 72 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 66  r data. */.    f
2fff6 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b  or(j=0; j<3; j++
2fff7 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  ){.      if( pCo
2fff8 6c 6c 5b 6a 5d 2e 78 44 65 6c 20 29 7b 0a 20 20  ll[j].xDel ){.  
2fff9 20 20 20 20 20 20 70 43 6f 6c 6c 5b 6a 5d 2e 78        pColl[j].x
2fffa 44 65 6c 28 70 43 6f 6c 6c 5b 6a 5d 2e 70 55 73  Del(pColl[j].pUs
2fffb 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
2fffc 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
2fffd 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 6c 29 3b  Free(db, pColl);
2fffe 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61  .  }.  sqlite3Ha
2ffff 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61 43 6f  shClear(&db->aCo
30000 6c 6c 53 65 71 29 3b 0a 23 69 66 6e 64 65 66 20  llSeq);.#ifndef 
30001 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
30002 55 41 4c 54 41 42 4c 45 0a 20 20 66 6f 72 28 69  UALTABLE.  for(i
30003 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
30004 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 20  (&db->aModule); 
30005 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68 4e  i; i=sqliteHashN
30006 65 78 74 28 69 29 29 7b 0a 20 20 20 20 4d 6f 64  ext(i)){.    Mod
30007 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d 6f 64  ule *pMod = (Mod
30008 75 6c 65 20 2a 29 73 71 6c 69 74 65 48 61 73 68  ule *)sqliteHash
30009 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28  Data(i);.    if(
3000a 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 20   pMod->xDestroy 
3000b 29 7b 0a 20 20 20 20 20 20 70 4d 6f 64 2d 3e 78  ){.      pMod->x
3000c 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d 3e 70 41  Destroy(pMod->pA
3000d 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ux);.    }.    s
3000e 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
3000f 20 70 4d 6f 64 29 3b 0a 20 20 7d 0a 20 20 73 71   pMod);.  }.  sq
30010 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
30011 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 0a 23 65  db->aModule);.#e
30012 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 45  ndif..  sqlite3E
30013 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
30014 4f 4b 2c 20 30 29 3b 20 2f 2a 20 44 65 61 6c 6c  OK, 0); /* Deall
30015 6f 63 61 74 65 73 20 61 6e 79 20 63 61 63 68 65  ocates any cache
30016 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67 73 2e  d error strings.
30017 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 70 45   */.  if( db->pE
30018 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rr ){.    sqlite
30019 33 56 61 6c 75 65 46 72 65 65 28 64 62 2d 3e 70  3ValueFree(db->p
3001a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Err);.  }.  sqli
3001b 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f  te3CloseExtensio
3001c 6e 73 28 64 62 29 3b 0a 0a 20 20 64 62 2d 3e 6d  ns(db);..  db->m
3001d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
3001e 47 49 43 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a  GIC_ERROR;..  /*
3001f 20 54 68 65 20 74 65 6d 70 2d 64 61 74 61 62 61   The temp-databa
30020 73 65 20 73 63 68 65 6d 61 20 69 73 20 61 6c 6c  se schema is all
30021 6f 63 61 74 65 64 20 64 69 66 66 65 72 65 6e 74  ocated different
30022 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65  ly from the othe
30023 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 6f 62  r schema.  ** ob
30024 6a 65 63 74 73 20 28 75 73 69 6e 67 20 73 71 6c  jects (using sql
30025 69 74 65 4d 61 6c 6c 6f 63 28 29 20 64 69 72 65  iteMalloc() dire
30026 63 74 6c 79 2c 20 69 6e 73 74 65 61 64 20 6f 66  ctly, instead of
30027 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
30028 65 6d 61 28 29 29 2e 0a 20 20 2a 2a 20 53 6f 20  ema())..  ** So 
30029 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66  it needs to be f
3002a 72 65 65 64 20 68 65 72 65 2e 20 54 6f 64 6f 3a  reed here. Todo:
3002b 20 57 68 79 20 6e 6f 74 20 72 6f 6c 6c 20 74 68   Why not roll th
3002c 65 20 74 65 6d 70 20 73 63 68 65 6d 61 20 69 6e  e temp schema in
3002d 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65  to.  ** the same
3002e 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
3002f 61 73 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20  as the one that 
30030 61 6c 6c 6f 63 61 74 65 73 20 74 68 65 20 64 61  allocates the da
30031 74 61 62 61 73 65 20 0a 20 20 2a 2a 20 73 74 72  tabase .  ** str
30032 75 63 74 75 72 65 3f 0a 20 20 2a 2f 0a 20 20 73  ucture?.  */.  s
30033 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
30034 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68   db->aDb[1].pSch
30035 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ema);.  sqlite3_
30036 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
30037 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 6d 61  mutex);.  db->ma
30038 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
30039 49 43 5f 43 4c 4f 53 45 44 3b 0a 20 20 73 71 6c  IC_CLOSED;.  sql
3003a 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28  ite3_mutex_free(
3003b 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
3003c 73 65 72 74 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73  sert( db->lookas
3003d 69 64 65 2e 6e 4f 75 74 3d 3d 30 20 29 3b 20 20  ide.nOut==0 );  
3003e 2f 2a 20 46 61 69 6c 73 20 6f 6e 20 61 20 6c 6f  /* Fails on a lo
3003f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 6c  okaside memory l
30040 65 61 6b 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  eak */.  if( db-
30041 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c  >lookaside.bMall
30042 6f 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  oced ){.    sqli
30043 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f  te3_free(db->loo
30044 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a  kaside.pStart);.
30045 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
30046 65 65 28 64 62 29 3b 0a 20 20 72 65 74 75 72 6e  ee(db);.  return
30047 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
30048 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c  *.** Rollback al
30049 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  l database files
3004a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
3004b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3004c 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c 69  RollbackAll(sqli
3004d 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
3004e 69 3b 0a 20 20 69 6e 74 20 69 6e 54 72 61 6e 73  i;.  int inTrans
3004f 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
30050 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
30051 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
30052 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  .  sqlite3BeginB
30053 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
30054 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
30055 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nDb; i++){.    i
30056 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  f( db->aDb[i].pB
30057 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  t ){.      if( s
30058 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
30059 72 61 6e 73 28 64 62 2d 3e 61 44 62 5b 69 5d 2e  rans(db->aDb[i].
3005a 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pBt) ){.        
3005b 69 6e 54 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20  inTrans = 1;.   
3005c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
3005d 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28  e3BtreeRollback(
3005e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b  db->aDb[i].pBt);
3005f 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
30060 5d 2e 69 6e 54 72 61 6e 73 20 3d 20 30 3b 0a 20  ].inTrans = 0;. 
30061 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
30062 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 64  e3VtabRollback(d
30063 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64  b);.  sqlite3End
30064 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
30065 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
30066 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  &SQLITE_InternCh
30067 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 73 71 6c  anges ){.    sql
30068 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
30069 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
3006a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  ;.    sqlite3Res
3006b 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
3006c 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  (db, 0);.  }..  
3006d 2f 2a 20 49 66 20 6f 6e 65 20 68 61 73 20 62 65  /* If one has be
3006e 65 6e 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 69  en configured, i
3006f 6e 76 6f 6b 65 20 74 68 65 20 72 6f 6c 6c 62 61  nvoke the rollba
30070 63 6b 2d 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b  ck-hook callback
30071 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 52   */.  if( db->xR
30072 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 20  ollbackCallback 
30073 26 26 20 28 69 6e 54 72 61 6e 73 20 7c 7c 20 21  && (inTrans || !
30074 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 29 20  db->autoCommit) 
30075 29 7b 0a 20 20 20 20 64 62 2d 3e 78 52 6f 6c 6c  ){.    db->xRoll
30076 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 64 62 2d  backCallback(db-
30077 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 29 3b 0a  >pRollbackArg);.
30078 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
30079 75 72 6e 20 61 20 73 74 61 74 69 63 20 73 74 72  urn a static str
3007a 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62  ing that describ
3007b 65 73 20 74 68 65 20 6b 69 6e 64 20 6f 66 20 65  es the kind of e
3007c 72 72 6f 72 20 73 70 65 63 69 66 69 65 64 20 69  rror specified i
3007d 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  n the.** argumen
3007e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
3007f 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
30080 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 69  *sqlite3ErrStr(i
30081 6e 74 20 72 63 29 7b 0a 20 20 73 74 61 74 69 63  nt rc){.  static
30082 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 63 6f 6e   const char* con
30083 73 74 20 61 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20  st aMsg[] = {.  
30084 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 20    /* SQLITE_OK  
30085 20 20 20 20 20 20 20 20 2a 2f 20 22 6e 6f 74 20          */ "not 
30086 61 6e 20 65 72 72 6f 72 22 2c 0a 20 20 20 20 2f  an error",.    /
30087 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20  * SQLITE_ERROR  
30088 20 20 20 20 20 2a 2f 20 22 53 51 4c 20 6c 6f 67       */ "SQL log
30089 69 63 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73  ic error or miss
3008a 69 6e 67 20 64 61 74 61 62 61 73 65 22 2c 0a 20  ing database",. 
3008b 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 49 4e 54     /* SQLITE_INT
3008c 45 52 4e 41 4c 20 20 20 20 2a 2f 20 30 2c 0a 20  ERNAL    */ 0,. 
3008d 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 50 45 52     /* SQLITE_PER
3008e 4d 20 20 20 20 20 20 20 20 2a 2f 20 22 61 63 63  M        */ "acc
3008f 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 64  ess permission d
30090 65 6e 69 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  enied",.    /* S
30091 51 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20  QLITE_ABORT     
30092 20 20 2a 2f 20 22 63 61 6c 6c 62 61 63 6b 20 72    */ "callback r
30093 65 71 75 65 73 74 65 64 20 71 75 65 72 79 20 61  equested query a
30094 62 6f 72 74 22 2c 0a 20 20 20 20 2f 2a 20 53 51  bort",.    /* SQ
30095 4c 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20  LITE_BUSY       
30096 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 69 73   */ "database is
30097 20 6c 6f 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a   locked",.    /*
30098 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20   SQLITE_LOCKED  
30099 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65      */ "database
3009a 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64   table is locked
3009b 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
3009c 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 2a 2f 20  _NOMEM       */ 
3009d 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c  "out of memory",
3009e 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52  .    /* SQLITE_R
3009f 45 41 44 4f 4e 4c 59 20 20 20 20 2a 2f 20 22 61  EADONLY    */ "a
300a0 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
300a1 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62  a readonly datab
300a2 61 73 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ase",.    /* SQL
300a3 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 20 20  ITE_INTERRUPT   
300a4 2a 2f 20 22 69 6e 74 65 72 72 75 70 74 65 64 22  */ "interrupted"
300a5 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
300a6 49 4f 45 52 52 20 20 20 20 20 20 20 2a 2f 20 22  IOERR       */ "
300a7 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22 2c  disk I/O error",
300a8 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43  .    /* SQLITE_C
300a9 4f 52 52 55 50 54 20 20 20 20 20 2a 2f 20 22 64  ORRUPT     */ "d
300aa 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61  atabase disk ima
300ab 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 22  ge is malformed"
300ac 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
300ad 4e 4f 54 46 4f 55 4e 44 20 20 20 20 2a 2f 20 30  NOTFOUND    */ 0
300ae 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
300af 46 55 4c 4c 20 20 20 20 20 20 20 20 2a 2f 20 22  FULL        */ "
300b0 64 61 74 61 62 61 73 65 20 6f 72 20 64 69 73 6b  database or disk
300b1 20 69 73 20 66 75 6c 6c 22 2c 0a 20 20 20 20 2f   is full",.    /
300b2 2a 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  * SQLITE_CANTOPE
300b3 4e 20 20 20 20 2a 2f 20 22 75 6e 61 62 6c 65 20  N    */ "unable 
300b4 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  to open database
300b5 20 66 69 6c 65 22 2c 0a 20 20 20 20 2f 2a 20 53   file",.    /* S
300b6 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20  QLITE_PROTOCOL  
300b7 20 20 2a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53    */ 0,.    /* S
300b8 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20  QLITE_EMPTY     
300b9 20 20 2a 2f 20 22 74 61 62 6c 65 20 63 6f 6e 74    */ "table cont
300ba 61 69 6e 73 20 6e 6f 20 64 61 74 61 22 2c 0a 20  ains no data",. 
300bb 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 43 48     /* SQLITE_SCH
300bc 45 4d 41 20 20 20 20 20 20 2a 2f 20 22 64 61 74  EMA      */ "dat
300bd 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73  abase schema has
300be 20 63 68 61 6e 67 65 64 22 2c 0a 20 20 20 20 2f   changed",.    /
300bf 2a 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20  * SQLITE_TOOBIG 
300c0 20 20 20 20 20 2a 2f 20 22 53 74 72 69 6e 67 20       */ "String 
300c1 6f 72 20 42 4c 4f 42 20 65 78 63 65 65 64 65 64  or BLOB exceeded
300c2 20 73 69 7a 65 20 6c 69 6d 69 74 22 2c 0a 20 20   size limit",.  
300c3 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53    /* SQLITE_CONS
300c4 54 52 41 49 4e 54 20 20 2a 2f 20 22 63 6f 6e 73  TRAINT  */ "cons
300c5 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c 0a  traint failed",.
300c6 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 49      /* SQLITE_MI
300c7 53 4d 41 54 43 48 20 20 20 20 2a 2f 20 22 64 61  SMATCH    */ "da
300c8 74 61 74 79 70 65 20 6d 69 73 6d 61 74 63 68 22  tatype mismatch"
300c9 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
300ca 4d 49 53 55 53 45 20 20 20 20 20 20 2a 2f 20 22  MISUSE      */ "
300cb 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20  library routine 
300cc 63 61 6c 6c 65 64 20 6f 75 74 20 6f 66 20 73 65  called out of se
300cd 71 75 65 6e 63 65 22 2c 0a 20 20 20 20 2f 2a 20  quence",.    /* 
300ce 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20 20  SQLITE_NOLFS    
300cf 20 20 20 2a 2f 20 22 6c 61 72 67 65 20 66 69 6c     */ "large fil
300d0 65 20 73 75 70 70 6f 72 74 20 69 73 20 64 69 73  e support is dis
300d1 61 62 6c 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  abled",.    /* S
300d2 51 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 20  QLITE_AUTH      
300d3 20 20 2a 2f 20 22 61 75 74 68 6f 72 69 7a 61 74    */ "authorizat
300d4 69 6f 6e 20 64 65 6e 69 65 64 22 2c 0a 20 20 20  ion denied",.   
300d5 20 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41   /* SQLITE_FORMA
300d6 54 20 20 20 20 20 20 2a 2f 20 22 61 75 78 69 6c  T      */ "auxil
300d7 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 6f  iary database fo
300d8 72 6d 61 74 20 65 72 72 6f 72 22 2c 0a 20 20 20  rmat error",.   
300d9 20 2f 2a 20 53 51 4c 49 54 45 5f 52 41 4e 47 45   /* SQLITE_RANGE
300da 20 20 20 20 20 20 20 2a 2f 20 22 62 69 6e 64 20         */ "bind 
300db 6f 72 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20  or column index 
300dc 6f 75 74 20 6f 66 20 72 61 6e 67 65 22 2c 0a 20  out of range",. 
300dd 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 54     /* SQLITE_NOT
300de 41 44 42 20 20 20 20 20 20 2a 2f 20 22 66 69 6c  ADB      */ "fil
300df 65 20 69 73 20 65 6e 63 72 79 70 74 65 64 20 6f  e is encrypted o
300e0 72 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61 62  r is not a datab
300e1 61 73 65 22 2c 0a 20 20 7d 3b 0a 20 20 72 63 20  ase",.  };.  rc 
300e2 26 3d 20 30 78 66 66 3b 0a 20 20 69 66 28 20 41  &= 0xff;.  if( A
300e3 4c 57 41 59 53 28 72 63 3e 3d 30 29 20 26 26 20  LWAYS(rc>=0) && 
300e4 72 63 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28  rc<(int)(sizeof(
300e5 61 4d 73 67 29 2f 73 69 7a 65 6f 66 28 61 4d 73  aMsg)/sizeof(aMs
300e6 67 5b 30 5d 29 29 20 26 26 20 61 4d 73 67 5b 72  g[0])) && aMsg[r
300e7 63 5d 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  c]!=0 ){.    ret
300e8 75 72 6e 20 61 4d 73 67 5b 72 63 5d 3b 0a 20 20  urn aMsg[rc];.  
300e9 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
300ea 6e 20 22 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f 72  n "unknown error
300eb 22 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ";.  }.}../*.** 
300ec 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
300ed 6c 65 6d 65 6e 74 73 20 61 20 62 75 73 79 20 63  lements a busy c
300ee 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 73 6c 65  allback that sle
300ef 65 70 73 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a  eps and tries.**
300f0 20 61 67 61 69 6e 20 75 6e 74 69 6c 20 61 20 74   again until a t
300f1 69 6d 65 6f 75 74 20 76 61 6c 75 65 20 69 73 20  imeout value is 
300f2 72 65 61 63 68 65 64 2e 20 20 54 68 65 20 74 69  reached.  The ti
300f3 6d 65 6f 75 74 20 76 61 6c 75 65 20 69 73 0a 2a  meout value is.*
300f4 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d  * an integer num
300f5 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f  ber of milliseco
300f6 6e 64 73 20 70 61 73 73 65 64 20 69 6e 20 61 73  nds passed in as
300f7 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72   the first.** ar
300f8 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  gument..*/.stati
300f9 63 20 69 6e 74 20 73 71 6c 69 74 65 44 65 66 61  c int sqliteDefa
300fa 75 6c 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 28  ultBusyCallback(
300fb 0a 20 76 6f 69 64 20 2a 70 74 72 2c 20 20 20 20  . void *ptr,    
300fc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
300fd 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
300fe 6e 20 2a 2f 0a 20 69 6e 74 20 63 6f 75 6e 74 20  n */. int count 
300ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
30100 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  * Number of time
30101 73 20 74 61 62 6c 65 20 68 61 73 20 62 65 65 6e  s table has been
30102 20 62 75 73 79 20 2a 2f 0a 29 7b 0a 23 69 66 20   busy */.){.#if 
30103 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 7c 7c  SQLITE_OS_WIN ||
30104 20 28 64 65 66 69 6e 65 64 28 48 41 56 45 5f 55   (defined(HAVE_U
30105 53 4c 45 45 50 29 20 26 26 20 48 41 56 45 5f 55  SLEEP) && HAVE_U
30106 53 4c 45 45 50 29 0a 20 20 73 74 61 74 69 63 20  SLEEP).  static 
30107 63 6f 6e 73 74 20 75 38 20 64 65 6c 61 79 73 5b  const u8 delays[
30108 5d 20 3d 0a 20 20 20 20 20 7b 20 31 2c 20 32 2c  ] =.     { 1, 2,
30109 20 35 2c 20 31 30 2c 20 31 35 2c 20 32 30 2c 20   5, 10, 15, 20, 
3010a 32 35 2c 20 32 35 2c 20 20 32 35 2c 20 20 35 30  25, 25,  25,  50
3010b 2c 20 20 35 30 2c 20 31 30 30 20 7d 3b 0a 20 20  ,  50, 100 };.  
3010c 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
3010d 74 6f 74 61 6c 73 5b 5d 20 3d 0a 20 20 20 20 20  totals[] =.     
3010e 7b 20 30 2c 20 31 2c 20 33 2c 20 20 38 2c 20 31  { 0, 1, 3,  8, 1
3010f 38 2c 20 33 33 2c 20 35 33 2c 20 37 38 2c 20 31  8, 33, 53, 78, 1
30110 30 33 2c 20 31 32 38 2c 20 31 37 38 2c 20 32 32  03, 128, 178, 22
30111 38 20 7d 3b 0a 23 20 64 65 66 69 6e 65 20 4e 44  8 };.# define ND
30112 45 4c 41 59 20 28 73 69 7a 65 6f 66 28 64 65 6c  ELAY (sizeof(del
30113 61 79 73 29 2f 73 69 7a 65 6f 66 28 64 65 6c 61  ays)/sizeof(dela
30114 79 73 5b 30 5d 29 29 0a 20 20 73 71 6c 69 74 65  ys[0])).  sqlite
30115 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33  3 *db = (sqlite3
30116 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69   *)ptr;.  int ti
30117 6d 65 6f 75 74 20 3d 20 64 62 2d 3e 62 75 73 79  meout = db->busy
30118 54 69 6d 65 6f 75 74 3b 0a 20 20 69 6e 74 20 64  Timeout;.  int d
30119 65 6c 61 79 2c 20 70 72 69 6f 72 3b 0a 0a 20 20  elay, prior;..  
3011a 61 73 73 65 72 74 28 20 63 6f 75 6e 74 3e 3d 30  assert( count>=0
3011b 20 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e 74 20   );.  if( count 
3011c 3c 20 4e 44 45 4c 41 59 20 29 7b 0a 20 20 20 20  < NDELAY ){.    
3011d 64 65 6c 61 79 20 3d 20 64 65 6c 61 79 73 5b 63  delay = delays[c
3011e 6f 75 6e 74 5d 3b 0a 20 20 20 20 70 72 69 6f 72  ount];.    prior
3011f 20 3d 20 74 6f 74 61 6c 73 5b 63 6f 75 6e 74 5d   = totals[count]
30120 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
30121 65 6c 61 79 20 3d 20 64 65 6c 61 79 73 5b 4e 44  elay = delays[ND
30122 45 4c 41 59 2d 31 5d 3b 0a 20 20 20 20 70 72 69  ELAY-1];.    pri
30123 6f 72 20 3d 20 74 6f 74 61 6c 73 5b 4e 44 45 4c  or = totals[NDEL
30124 41 59 2d 31 5d 20 2b 20 64 65 6c 61 79 2a 28 63  AY-1] + delay*(c
30125 6f 75 6e 74 2d 28 4e 44 45 4c 41 59 2d 31 29 29  ount-(NDELAY-1))
30126 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 72 69 6f  ;.  }.  if( prio
30127 72 20 2b 20 64 65 6c 61 79 20 3e 20 74 69 6d 65  r + delay > time
30128 6f 75 74 20 29 7b 0a 20 20 20 20 64 65 6c 61 79  out ){.    delay
30129 20 3d 20 74 69 6d 65 6f 75 74 20 2d 20 70 72 69   = timeout - pri
3012a 6f 72 3b 0a 20 20 20 20 69 66 28 20 64 65 6c 61  or;.    if( dela
3012b 79 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  y<=0 ) return 0;
3012c 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73  .  }.  sqlite3Os
3012d 53 6c 65 65 70 28 64 62 2d 3e 70 56 66 73 2c 20  Sleep(db->pVfs, 
3012e 64 65 6c 61 79 2a 31 30 30 30 29 3b 0a 20 20 72  delay*1000);.  r
3012f 65 74 75 72 6e 20 31 3b 0a 23 65 6c 73 65 0a 20  eturn 1;.#else. 
30130 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28   sqlite3 *db = (
30131 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b 0a 20  sqlite3 *)ptr;. 
30132 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20 28   int timeout = (
30133 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 29 2d  (sqlite3 *)ptr)-
30134 3e 62 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20 20  >busyTimeout;.  
30135 69 66 28 20 28 63 6f 75 6e 74 2b 31 29 2a 31 30  if( (count+1)*10
30136 30 30 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a  00 > timeout ){.
30137 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
30138 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 53 6c 65  }.  sqlite3OsSle
30139 65 70 28 64 62 2d 3e 70 56 66 73 2c 20 31 30 30  ep(db->pVfs, 100
3013a 30 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  0000);.  return 
3013b 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  1;.#endif.}../*.
3013c 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 67 69  ** Invoke the gi
3013d 76 65 6e 20 62 75 73 79 20 68 61 6e 64 6c 65 72  ven busy handler
3013e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
3013f 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77  tine is called w
30140 68 65 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e  hen an operation
30141 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 20 6c   failed with a l
30142 6f 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  ock..** If this 
30143 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
30144 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f  non-zero, the lo
30145 63 6b 20 69 73 20 72 65 74 72 69 65 64 2e 20 20  ck is retried.  
30146 49 66 20 69 74 0a 2a 2a 20 72 65 74 75 72 6e 73  If it.** returns
30147 20 30 2c 20 74 68 65 20 6f 70 65 72 61 74 69 6f   0, the operatio
30148 6e 20 61 62 6f 72 74 73 20 77 69 74 68 20 61 6e  n aborts with an
30149 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72   SQLITE_BUSY err
3014a 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
3014b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
3014c 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c  3InvokeBusyHandl
3014d 65 72 28 42 75 73 79 48 61 6e 64 6c 65 72 20 2a  er(BusyHandler *
3014e 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
3014f 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20  if( NEVER(p==0) 
30150 7c 7c 20 70 2d 3e 78 46 75 6e 63 3d 3d 30 20 7c  || p->xFunc==0 |
30151 7c 20 70 2d 3e 6e 42 75 73 79 3c 30 20 29 20 72  | p->nBusy<0 ) r
30152 65 74 75 72 6e 20 30 3b 0a 20 20 72 63 20 3d 20  eturn 0;.  rc = 
30153 70 2d 3e 78 46 75 6e 63 28 70 2d 3e 70 41 72 67  p->xFunc(p->pArg
30154 2c 20 70 2d 3e 6e 42 75 73 79 29 3b 0a 20 20 69  , p->nBusy);.  i
30155 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20  f( rc==0 ){.    
30156 70 2d 3e 6e 42 75 73 79 20 3d 20 2d 31 3b 0a 20  p->nBusy = -1;. 
30157 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6e   }else{.    p->n
30158 42 75 73 79 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  Busy++;.  }.  re
30159 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f 2a 0a  turn rc; .}../*.
3015a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3015b 73 65 74 73 20 74 68 65 20 62 75 73 79 20 63 61  sets the busy ca
3015c 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53 71  llback for an Sq
3015d 6c 69 74 65 20 64 61 74 61 62 61 73 65 20 74 6f  lite database to
3015e 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63 61   the.** given ca
3015f 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
30160 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 61  with the given a
30161 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  rgument..*/.SQLI
30162 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
30163 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
30164 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
30165 20 20 69 6e 74 20 28 2a 78 42 75 73 79 29 28 76    int (*xBusy)(v
30166 6f 69 64 2a 2c 69 6e 74 29 2c 0a 20 20 76 6f 69  oid*,int),.  voi
30167 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  d *pArg.){.  sql
30168 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
30169 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
3016a 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 78  b->busyHandler.x
3016b 46 75 6e 63 20 3d 20 78 42 75 73 79 3b 0a 20 20  Func = xBusy;.  
3016c 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e  db->busyHandler.
3016d 70 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 64  pArg = pArg;.  d
3016e 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e  b->busyHandler.n
3016f 42 75 73 79 20 3d 20 30 3b 0a 20 20 73 71 6c 69  Busy = 0;.  sqli
30170 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
30171 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
30172 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
30173 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
30174 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f  E_OMIT_PROGRESS_
30175 43 41 4c 4c 42 41 43 4b 0a 2f 2a 0a 2a 2a 20 54  CALLBACK./*.** T
30176 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
30177 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61   the progress ca
30178 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53 71  llback for an Sq
30179 6c 69 74 65 20 64 61 74 61 62 61 73 65 20 74 6f  lite database to
3017a 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63 61   the.** given ca
3017b 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
3017c 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 61  with the given a
3017d 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 70 72 6f  rgument. The pro
3017e 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 77  gress callback w
3017f 69 6c 6c 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65  ill.** be invoke
30180 64 20 65 76 65 72 79 20 6e 4f 70 73 20 6f 70 63  d every nOps opc
30181 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  odes..*/.SQLITE_
30182 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
30183 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  _progress_handle
30184 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  r(.  sqlite3 *db
30185 2c 20 0a 20 20 69 6e 74 20 6e 4f 70 73 2c 0a 20  , .  int nOps,. 
30186 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73 73   int (*xProgress
30187 29 28 76 6f 69 64 2a 29 2c 20 0a 20 20 76 6f 69  )(void*), .  voi
30188 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  d *pArg.){.  sql
30189 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
3018a 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69  (db->mutex);.  i
3018b 66 28 20 6e 4f 70 73 3e 30 20 29 7b 0a 20 20 20  f( nOps>0 ){.   
3018c 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 3d   db->xProgress =
3018d 20 78 50 72 6f 67 72 65 73 73 3b 0a 20 20 20 20   xProgress;.    
3018e 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73  db->nProgressOps
3018f 20 3d 20 6e 4f 70 73 3b 0a 20 20 20 20 64 62 2d   = nOps;.    db-
30190 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d 20  >pProgressArg = 
30191 70 41 72 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pArg;.  }else{. 
30192 20 20 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73     db->xProgress
30193 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 50   = 0;.    db->nP
30194 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a  rogressOps = 0;.
30195 20 20 20 20 64 62 2d 3e 70 50 72 6f 67 72 65 73      db->pProgres
30196 73 41 72 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  sArg = 0;.  }.  
30197 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
30198 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
30199 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
3019a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e   This routine in
3019b 73 74 61 6c 6c 73 20 61 20 64 65 66 61 75 6c 74  stalls a default
3019c 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 74 68   busy handler th
3019d 61 74 20 77 61 69 74 73 20 66 6f 72 20 74 68 65  at waits for the
3019e 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75  .** specified nu
3019f 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63  mber of millisec
301a0 6f 6e 64 73 20 62 65 66 6f 72 65 20 72 65 74 75  onds before retu
301a1 72 6e 69 6e 67 20 30 2e 0a 2a 2f 0a 53 51 4c 49  rning 0..*/.SQLI
301a2 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
301a3 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
301a4 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
301a5 20 6d 73 29 7b 0a 20 20 69 66 28 20 6d 73 3e 30   ms){.  if( ms>0
301a6 20 29 7b 0a 20 20 20 20 64 62 2d 3e 62 75 73 79   ){.    db->busy
301a7 54 69 6d 65 6f 75 74 20 3d 20 6d 73 3b 0a 20 20  Timeout = ms;.  
301a8 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68    sqlite3_busy_h
301a9 61 6e 64 6c 65 72 28 64 62 2c 20 73 71 6c 69 74  andler(db, sqlit
301aa 65 44 65 66 61 75 6c 74 42 75 73 79 43 61 6c 6c  eDefaultBusyCall
301ab 62 61 63 6b 2c 20 28 76 6f 69 64 2a 29 64 62 29  back, (void*)db)
301ac 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
301ad 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
301ae 6c 65 72 28 64 62 2c 20 30 2c 20 30 29 3b 0a 20  ler(db, 0, 0);. 
301af 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
301b0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
301b1 43 61 75 73 65 20 61 6e 79 20 70 65 6e 64 69 6e  Cause any pendin
301b2 67 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 73  g operation to s
301b3 74 6f 70 20 61 74 20 69 74 73 20 65 61 72 6c 69  top at its earli
301b4 65 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e  est opportunity.
301b5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
301b6 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  oid sqlite3_inte
301b7 72 72 75 70 74 28 73 71 6c 69 74 65 33 20 2a 64  rrupt(sqlite3 *d
301b8 62 29 7b 0a 20 20 64 62 2d 3e 75 31 2e 69 73 49  b){.  db->u1.isI
301b9 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a  nterrupted = 1;.
301ba 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
301bb 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 61 63 74  unction is exact
301bc 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 73  ly the same as s
301bd 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
301be 6e 63 74 69 6f 6e 28 29 2c 20 65 78 63 65 70 74  nction(), except
301bf 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 64  .** that it is d
301c0 65 73 69 67 6e 65 64 20 74 6f 20 62 65 20 63 61  esigned to be ca
301c1 6c 6c 65 64 20 62 79 20 69 6e 74 65 72 6e 61 6c  lled by internal
301c2 20 63 6f 64 65 2e 20 54 68 65 20 64 69 66 66 65   code. The diffe
301c3 72 65 6e 63 65 20 69 73 0a 2a 2a 20 74 68 61 74  rence is.** that
301c4 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66   if a malloc() f
301c5 61 69 6c 73 20 69 6e 20 73 71 6c 69 74 65 33 5f  ails in sqlite3_
301c6 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
301c7 29 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  ), an error code
301c8 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20  .** is returned 
301c9 61 6e 64 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  and the mallocFa
301ca 69 6c 65 64 20 66 6c 61 67 20 63 6c 65 61 72 65  iled flag cleare
301cb 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  d. .*/.SQLITE_PR
301cc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
301cd 33 43 72 65 61 74 65 46 75 6e 63 28 0a 20 20 73  3CreateFunc(.  s
301ce 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
301cf 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74  nst char *zFunct
301d0 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e  ionName,.  int n
301d1 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 0a  Arg,.  int enc,.
301d2 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74    void *pUserDat
301d3 61 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  a,.  void (*xFun
301d4 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
301d5 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
301d6 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69  value **),.  voi
301d7 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
301d8 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
301d9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
301da 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e  ),.  void (*xFin
301db 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  al)(sqlite3_cont
301dc 65 78 74 2a 29 0a 29 7b 0a 20 20 46 75 6e 63 44  ext*).){.  FuncD
301dd 65 66 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 4e 61  ef *p;.  int nNa
301de 6d 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  me;..  assert( s
301df 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
301e0 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
301e1 20 20 69 66 28 20 7a 46 75 6e 63 74 69 6f 6e 4e    if( zFunctionN
301e2 61 6d 65 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20  ame==0 ||.      
301e3 28 78 46 75 6e 63 20 26 26 20 28 78 46 69 6e 61  (xFunc && (xFina
301e4 6c 20 7c 7c 20 78 53 74 65 70 29 29 20 7c 7c 20  l || xStep)) || 
301e5 0a 20 20 20 20 20 20 28 21 78 46 75 6e 63 20 26  .      (!xFunc &
301e6 26 20 28 78 46 69 6e 61 6c 20 26 26 20 21 78 53  & (xFinal && !xS
301e7 74 65 70 29 29 20 7c 7c 0a 20 20 20 20 20 20 28  tep)) ||.      (
301e8 21 78 46 75 6e 63 20 26 26 20 28 21 78 46 69 6e  !xFunc && (!xFin
301e9 61 6c 20 26 26 20 78 53 74 65 70 29 29 20 7c 7c  al && xStep)) ||
301ea 0a 20 20 20 20 20 20 28 6e 41 72 67 3c 2d 31 20  .      (nArg<-1 
301eb 7c 7c 20 6e 41 72 67 3e 53 51 4c 49 54 45 5f 4d  || nArg>SQLITE_M
301ec 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 29  AX_FUNCTION_ARG)
301ed 20 7c 7c 0a 20 20 20 20 20 20 28 32 35 35 3c 28   ||.      (255<(
301ee 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
301ef 74 72 6c 65 6e 33 30 28 20 7a 46 75 6e 63 74 69  trlen30( zFuncti
301f0 6f 6e 4e 61 6d 65 29 29 29 20 29 7b 0a 20 20 20  onName))) ){.   
301f1 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
301f2 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20  , SQLITE_ERROR, 
301f3 22 62 61 64 20 70 61 72 61 6d 65 74 65 72 73 22  "bad parameters"
301f4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
301f5 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
301f6 20 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54    .#ifndef SQLIT
301f7 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f  E_OMIT_UTF16.  /
301f8 2a 20 49 66 20 53 51 4c 49 54 45 5f 55 54 46 31  * If SQLITE_UTF1
301f9 36 20 69 73 20 73 70 65 63 69 66 69 65 64 20 61  6 is specified a
301fa 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 74  s the encoding t
301fb 79 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20 74  ype, transform t
301fc 68 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65 20  his.  ** to one 
301fd 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  of SQLITE_UTF16L
301fe 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31  E or SQLITE_UTF1
301ff 36 42 45 20 75 73 69 6e 67 20 74 68 65 0a 20 20  6BE using the.  
30200 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  ** SQLITE_UTF16N
30201 41 54 49 56 45 20 6d 61 63 72 6f 2e 20 53 51 4c  ATIVE macro. SQL
30202 49 54 45 5f 55 54 46 31 36 20 69 73 20 6e 6f 74  ITE_UTF16 is not
30203 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79   used internally
30204 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 53  ..  **.  ** If S
30205 51 4c 49 54 45 5f 41 4e 59 20 69 73 20 73 70 65  QLITE_ANY is spe
30206 63 69 66 69 65 64 2c 20 61 64 64 20 74 68 72 65  cified, add thre
30207 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  e versions of th
30208 65 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20  e function.  ** 
30209 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
3020a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 6e  e..  */.  if( en
3020b 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20  c==SQLITE_UTF16 
3020c 29 7b 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c  ){.    enc = SQL
3020d 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b  ITE_UTF16NATIVE;
3020e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65 6e 63  .  }else if( enc
3020f 3d 3d 53 51 4c 49 54 45 5f 41 4e 59 20 29 7b 0a  ==SQLITE_ANY ){.
30210 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
30211 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  rc = sqlite3Crea
30212 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63  teFunc(db, zFunc
30213 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  tionName, nArg, 
30214 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20  SQLITE_UTF8,.   
30215 20 20 20 20 20 20 70 55 73 65 72 44 61 74 61 2c        pUserData,
30216 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78   xFunc, xStep, x
30217 46 69 6e 61 6c 29 3b 0a 20 20 20 20 69 66 28 20  Final);.    if( 
30218 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
30219 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
3021a 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
3021b 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c  , zFunctionName,
3021c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54   nArg, SQLITE_UT
3021d 46 31 36 4c 45 2c 0a 20 20 20 20 20 20 20 20 20  F16LE,.         
3021e 20 70 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e   pUserData, xFun
3021f 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c  c, xStep, xFinal
30220 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
30221 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
30222 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
30223 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6e 63  c;.    }.    enc
30224 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42   = SQLITE_UTF16B
30225 45 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 65  E;.  }.#else.  e
30226 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
30227 3b 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 2f 2a  ;.#endif.  .  /*
30228 20 43 68 65 63 6b 20 69 66 20 61 6e 20 65 78 69   Check if an exi
30229 73 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69  sting function i
3022a 73 20 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64  s being overridd
3022b 65 6e 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 49  en or deleted. I
3022c 66 20 73 6f 2c 0a 20 20 2a 2a 20 61 6e 64 20 74  f so,.  ** and t
3022d 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20  here are active 
3022e 56 4d 73 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  VMs, then return
3022f 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 49 66   SQLITE_BUSY. If
30230 20 61 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a   a function.  **
30231 20 69 73 20 62 65 69 6e 67 20 6f 76 65 72 72 69   is being overri
30232 64 64 65 6e 2f 64 65 6c 65 74 65 64 20 62 75 74  dden/deleted but
30233 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 63   there are no ac
30234 74 69 76 65 20 56 4d 73 2c 20 61 6c 6c 6f 77 20  tive VMs, allow 
30235 74 68 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69  the.  ** operati
30236 6f 6e 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 62  on to continue b
30237 75 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c  ut invalidate al
30238 6c 20 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74  l precompiled st
30239 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20  atements..  */. 
3023a 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   p = sqlite3Find
3023b 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75  Function(db, zFu
3023c 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d  nctionName, nNam
3023d 65 2c 20 6e 41 72 67 2c 20 28 75 38 29 65 6e 63  e, nArg, (u8)enc
3023e 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26  , 0);.  if( p &&
3023f 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 65 6e   p->iPrefEnc==en
30240 63 20 26 26 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41  c && p->nArg==nA
30241 72 67 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  rg ){.    if( db
30242 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20  ->activeVdbeCnt 
30243 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
30244 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
30245 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20  _BUSY, .        
30246 22 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74  "unable to delet
30247 65 2f 6d 6f 64 69 66 79 20 75 73 65 72 2d 66 75  e/modify user-fu
30248 6e 63 74 69 6f 6e 20 64 75 65 20 74 6f 20 61 63  nction due to ac
30249 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 22  tive statements"
3024a 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
3024b 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
3024c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ed );.      retu
3024d 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
3024e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3024f 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
30250 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
30251 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  (db);.    }.  }.
30252 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69  .  p = sqlite3Fi
30253 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  ndFunction(db, z
30254 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e  FunctionName, nN
30255 61 6d 65 2c 20 6e 41 72 67 2c 20 28 75 38 29 65  ame, nArg, (u8)e
30256 6e 63 2c 20 31 29 3b 0a 20 20 61 73 73 65 72 74  nc, 1);.  assert
30257 28 70 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  (p || db->malloc
30258 46 61 69 6c 65 64 29 3b 0a 20 20 69 66 28 20 21  Failed);.  if( !
30259 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  p ){.    return 
3025a 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
3025b 7d 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 30  }.  p->flags = 0
3025c 3b 0a 20 20 70 2d 3e 78 46 75 6e 63 20 3d 20 78  ;.  p->xFunc = x
3025d 46 75 6e 63 3b 0a 20 20 70 2d 3e 78 53 74 65 70  Func;.  p->xStep
3025e 20 3d 20 78 53 74 65 70 3b 0a 20 20 70 2d 3e 78   = xStep;.  p->x
3025f 46 69 6e 61 6c 69 7a 65 20 3d 20 78 46 69 6e 61  Finalize = xFina
30260 6c 3b 0a 20 20 70 2d 3e 70 55 73 65 72 44 61 74  l;.  p->pUserDat
30261 61 20 3d 20 70 55 73 65 72 44 61 74 61 3b 0a 20  a = pUserData;. 
30262 20 70 2d 3e 6e 41 72 67 20 3d 20 28 75 31 36 29   p->nArg = (u16)
30263 6e 41 72 67 3b 0a 20 20 72 65 74 75 72 6e 20 53  nArg;.  return S
30264 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
30265 2a 2a 20 43 72 65 61 74 65 20 6e 65 77 20 75 73  ** Create new us
30266 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f  er functions..*/
30267 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
30268 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
30269 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74  unction(.  sqlit
3026a 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3 *db,.  const 
3026b 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e  char *zFunctionN
3026c 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c  ame,.  int nArg,
3026d 0a 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f  .  int enc,.  vo
3026e 69 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a  id *p,.  void (*
3026f 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
30270 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
30271 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20  te3_value **),. 
30272 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
30273 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
30274 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
30275 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  e **),.  void (*
30276 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f  xFinal)(sqlite3_
30277 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69  context*).){.  i
30278 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
30279 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
3027a 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20  >mutex);.  rc = 
3027b 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
3027c 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e  c(db, zFunctionN
3027d 61 6d 65 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20  ame, nArg, enc, 
3027e 70 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c  p, xFunc, xStep,
3027f 20 78 46 69 6e 61 6c 29 3b 0a 20 20 72 63 20 3d   xFinal);.  rc =
30280 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
30281 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
30282 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
30283 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
30284 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64  urn rc;.}..#ifnd
30285 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
30286 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
30287 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
30288 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20  te_function16(. 
30289 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
3028a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e  const void *zFun
3028b 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74  ctionName,.  int
3028c 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65   nArg,.  int eTe
3028d 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70  xtRep,.  void *p
3028e 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63  ,.  void (*xFunc
3028f 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
30290 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
30291 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  alue**),.  void 
30292 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33  (*xStep)(sqlite3
30293 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
30294 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a  lite3_value**),.
30295 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29    void (*xFinal)
30296 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
30297 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  *).){.  int rc;.
30298 20 20 63 68 61 72 20 2a 7a 46 75 6e 63 38 3b 0a    char *zFunc8;.
30299 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
3029a 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
3029b 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
3029c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
3029d 0a 20 20 7a 46 75 6e 63 38 20 3d 20 73 71 6c 69  .  zFunc8 = sqli
3029e 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20  te3Utf16to8(db, 
3029f 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 2d  zFunctionName, -
302a0 31 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  1);.  rc = sqlit
302a1 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
302a2 20 7a 46 75 6e 63 38 2c 20 6e 41 72 67 2c 20 65   zFunc8, nArg, e
302a3 54 65 78 74 52 65 70 2c 20 70 2c 20 78 46 75 6e  TextRep, p, xFun
302a4 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c  c, xStep, xFinal
302a5 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
302a6 65 65 28 64 62 2c 20 7a 46 75 6e 63 38 29 3b 0a  ee(db, zFunc8);.
302a7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
302a8 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
302a9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
302aa 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
302ab 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
302ac 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44  #endif.../*.** D
302ad 65 63 6c 61 72 65 20 74 68 61 74 20 61 20 66 75  eclare that a fu
302ae 6e 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20  nction has been 
302af 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 61 20  overloaded by a 
302b0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
302b1 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63  *.** If the func
302b2 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 65 78 69  tion already exi
302b3 73 74 73 20 61 73 20 61 20 72 65 67 75 6c 61 72  sts as a regular
302b4 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e   global function
302b5 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72  , then.** this r
302b6 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
302b7 70 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74  p.  If the funct
302b8 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ion does not exi
302b9 73 74 2c 20 74 68 65 6e 20 63 72 65 61 74 65 0a  st, then create.
302ba 2a 2a 20 61 20 6e 65 77 20 6f 6e 65 20 74 68 61  ** a new one tha
302bb 74 20 61 6c 77 61 79 73 20 74 68 72 6f 77 73 20  t always throws 
302bc 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72  a run-time error
302bd 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76  .  .**.** When v
302be 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e  irtual tables in
302bf 74 65 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20  tend to provide 
302c0 61 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20 66 75  an overloaded fu
302c1 6e 63 74 69 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20  nction, they.** 
302c2 73 68 6f 75 6c 64 20 63 61 6c 6c 20 74 68 69 73  should call this
302c3 20 72 6f 75 74 69 6e 65 20 74 6f 20 6d 61 6b 65   routine to make
302c4 20 73 75 72 65 20 74 68 65 20 67 6c 6f 62 61 6c   sure the global
302c5 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73   function exists
302c6 2e 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 66 75  ..** A global fu
302c7 6e 63 74 69 6f 6e 20 6d 75 73 74 20 65 78 69 73  nction must exis
302c8 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 6e  t in order for n
302c9 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 74  ame resolution t
302ca 6f 20 77 6f 72 6b 0a 2a 2a 20 70 72 6f 70 65 72  o work.** proper
302cb 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ly..*/.SQLITE_AP
302cc 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76  I int sqlite3_ov
302cd 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
302ce 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
302cf 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
302d0 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 0a  ame,.  int nArg.
302d1 29 7b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d  ){.  int nName =
302d2 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
302d3 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72  (zName);.  int r
302d4 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
302d5 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
302d6 65 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ex);.  if( sqlit
302d7 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64  e3FindFunction(d
302d8 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  b, zName, nName,
302d9 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54   nArg, SQLITE_UT
302da 46 38 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20  F8, 0)==0 ){.   
302db 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
302dc 6e 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41  nc(db, zName, nA
302dd 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  rg, SQLITE_UTF8,
302de 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
302df 20 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65         0, sqlite
302e0 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e  3InvalidFunction
302e1 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  , 0, 0);.  }.  r
302e2 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
302e3 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  it(db, SQLITE_OK
302e4 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
302e5 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
302e6 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
302e7 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
302e8 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f  ITE_OMIT_TRACE./
302e9 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
302ea 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 2e 20  trace function. 
302eb 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74   The pArg from t
302ec 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  he previously re
302ed 67 69 73 74 65 72 65 64 20 74 72 61 63 65 0a 2a  gistered trace.*
302ee 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  * is returned.  
302ef 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 74 72  .**.** A NULL tr
302f0 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61  ace function mea
302f1 6e 73 20 74 68 61 74 20 6e 6f 20 74 72 61 63 69  ns that no traci
302f2 6e 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20  ng is executes. 
302f3 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74   A non-NULL.** t
302f4 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65  race is a pointe
302f5 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  r to a function 
302f6 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20  that is invoked 
302f7 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
302f8 65 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74 61 74  each.** SQL stat
302f9 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ement..*/.SQLITE
302fa 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
302fb 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33  e3_trace(sqlite3
302fc 20 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78 54 72   *db, void (*xTr
302fd 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ace)(void*,const
302fe 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 20 2a 70   char*), void *p
302ff 41 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f  Arg){.  void *pO
30300 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ld;.  sqlite3_mu
30301 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
30302 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64  tex);.  pOld = d
30303 62 2d 3e 70 54 72 61 63 65 41 72 67 3b 0a 20 20  b->pTraceArg;.  
30304 64 62 2d 3e 78 54 72 61 63 65 20 3d 20 78 54 72  db->xTrace = xTr
30305 61 63 65 3b 0a 20 20 64 62 2d 3e 70 54 72 61 63  ace;.  db->pTrac
30306 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  eArg = pArg;.  s
30307 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
30308 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
30309 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a   return pOld;.}.
3030a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
3030b 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f   profile functio
3030c 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f  n.  The pArg fro
3030d 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  m the previously
3030e 20 72 65 67 69 73 74 65 72 65 64 20 0a 2a 2a 20   registered .** 
3030f 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e  profile function
30310 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a   is returned.  .
30311 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70 72 6f  **.** A NULL pro
30312 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65  file function me
30313 61 6e 73 20 74 68 61 74 20 6e 6f 20 70 72 6f 66  ans that no prof
30314 69 6c 69 6e 67 20 69 73 20 65 78 65 63 75 74 65  iling is execute
30315 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a  s.  A non-NULL.*
30316 2a 20 70 72 6f 66 69 6c 65 20 69 73 20 61 20 70  * profile is a p
30317 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
30318 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76  tion that is inv
30319 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63  oked at the conc
3031a 6c 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63  lusion of.** eac
3031b 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  h SQL statement 
3031c 74 68 61 74 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a  that is run..*/.
3031d 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
3031e 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65  *sqlite3_profile
3031f 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
30320 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69  .  void (*xProfi
30321 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  le)(void*,const 
30322 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e  char*,sqlite_uin
30323 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  t64),.  void *pA
30324 72 67 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f  rg.){.  void *pO
30325 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  ld;.  sqlite3_mu
30326 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
30327 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64  tex);.  pOld = d
30328 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 3b 0a  b->pProfileArg;.
30329 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 3d    db->xProfile =
3032a 20 78 50 72 6f 66 69 6c 65 3b 0a 20 20 64 62 2d   xProfile;.  db-
3032b 3e 70 50 72 6f 66 69 6c 65 41 72 67 20 3d 20 70  >pProfileArg = p
3032c 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Arg;.  sqlite3_m
3032d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
3032e 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
3032f 70 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  pOld;.}.#endif /
30330 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  * SQLITE_OMIT_TR
30331 41 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50  ACE */../*** EXP
30332 45 52 49 4d 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a  ERIMENTAL ***.**
30333 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 66  .** Register a f
30334 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e  unction to be in
30335 76 6f 6b 65 64 20 77 68 65 6e 20 61 20 74 72 61  voked when a tra
30336 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74  nsaction comment
30337 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 76  s..** If the inv
30338 6f 6b 65 64 20 66 75 6e 63 74 69 6f 6e 20 72 65  oked function re
30339 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  turns non-zero, 
3033a 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20  then the commit 
3033b 62 65 63 6f 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c  becomes a.** rol
3033c 6c 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  lback..*/.SQLITE
3033d 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
3033e 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a  e3_commit_hook(.
3033f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
30340 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
30341 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74  ttach the hook t
30342 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
30343 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c  */.  int (*xCall
30344 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 20 2f  back)(void*),  /
30345 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  * Function to in
30346 76 6f 6b 65 20 6f 6e 20 65 61 63 68 20 63 6f 6d  voke on each com
30347 6d 69 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  mit */.  void *p
30348 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Arg             
30349 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
3034a 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  o the function *
3034b 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c  /.){.  void *pOl
3034c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
3034d 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
3034e 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62  ex);.  pOld = db
3034f 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 3b 0a 20 20  ->pCommitArg;.  
30350 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62  db->xCommitCallb
30351 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b  ack = xCallback;
30352 0a 20 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72  .  db->pCommitAr
30353 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69  g = pArg;.  sqli
30354 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
30355 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
30356 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a  turn pOld;.}../*
30357 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63  .** Register a c
30358 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e  allback to be in
30359 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20  voked each time 
3035a 61 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64  a row is updated
3035b 2c 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 6f 72  ,.** inserted or
3035c 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74   deleted using t
3035d 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e  his database con
3035e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  nection..*/.SQLI
3035f 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
30360 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b  ite3_update_hook
30361 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
30362 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30363 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b   Attach the hook
30364 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73   to this databas
30365 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43  e */.  void (*xC
30366 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69  allback)(void*,i
30367 6e 74 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c  nt,char const *,
30368 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c  char const *,sql
30369 69 74 65 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f  ite_int64),.  vo
3036a 69 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20  id *pArg        
3036b 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
3036c 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
3036d 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64  ion */.){.  void
3036e 20 2a 70 52 65 74 3b 0a 20 20 73 71 6c 69 74 65   *pRet;.  sqlite
3036f 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
30370 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 52 65 74  ->mutex);.  pRet
30371 20 3d 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72   = db->pUpdateAr
30372 67 3b 0a 20 20 64 62 2d 3e 78 55 70 64 61 74 65  g;.  db->xUpdate
30373 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c  Callback = xCall
30374 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 55 70 64  back;.  db->pUpd
30375 61 74 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  ateArg = pArg;. 
30376 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
30377 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
30378 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a  .  return pRet;.
30379 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
3037a 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  r a callback to 
3037b 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20  be invoked each 
3037c 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69  time a transacti
3037d 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20  on is rolled.** 
3037e 62 61 63 6b 20 62 79 20 74 68 69 73 20 64 61 74  back by this dat
3037f 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
30380 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
30381 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f  void *sqlite3_ro
30382 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73  llback_hook(.  s
30383 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
30384 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61           /* Atta
30385 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74  ch the hook to t
30386 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  his database */.
30387 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61    void (*xCallba
30388 63 6b 29 28 76 6f 69 64 2a 29 2c 20 2f 2a 20 43  ck)(void*), /* C
30389 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
3038a 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67   */.  void *pArg
3038b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3038c 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
3038d 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  he function */.)
3038e 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a  {.  void *pRet;.
3038f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
30390 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
30391 3b 0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e 70  ;.  pRet = db->p
30392 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 0a 20 20 64  RollbackArg;.  d
30393 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  b->xRollbackCall
30394 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
30395 3b 0a 20 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63  ;.  db->pRollbac
30396 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73  kArg = pArg;.  s
30397 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
30398 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
30399 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
3039a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
3039b 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 72 75  tion returns tru
3039c 65 20 69 66 20 6d 61 69 6e 2d 6d 65 6d 6f 72 79  e if main-memory
3039d 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
3039e 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 61 20  instead of.** a 
3039f 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 66  temporary file f
303a0 6f 72 20 74 72 61 6e 73 69 65 6e 74 20 70 61 67  or transient pag
303a1 65 72 20 66 69 6c 65 73 20 61 6e 64 20 73 74 61  er files and sta
303a2 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2e  tement journals.
303a3 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65  .** The value re
303a4 74 75 72 6e 65 64 20 64 65 70 65 6e 64 73 20 6f  turned depends o
303a5 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 64  n the value of d
303a6 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 28 72  b->temp_store (r
303a7 75 6e 74 69 6d 65 0a 2a 2a 20 70 61 72 61 6d 65  untime.** parame
303a8 74 65 72 29 20 61 6e 64 20 74 68 65 20 63 6f 6d  ter) and the com
303a9 70 69 6c 65 20 74 69 6d 65 20 76 61 6c 75 65 20  pile time value 
303aa 6f 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53  of SQLITE_TEMP_S
303ab 54 4f 52 45 2e 20 54 68 65 0a 2a 2a 20 66 6f 6c  TORE. The.** fol
303ac 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 73  lowing table des
303ad 63 72 69 62 65 73 20 74 68 65 20 72 65 6c 61 74  cribes the relat
303ae 69 6f 6e 73 68 69 70 20 62 65 74 77 65 65 6e 20  ionship between 
303af 74 68 65 73 65 20 74 77 6f 20 76 61 6c 75 65 73  these two values
303b0 0a 2a 2a 20 61 6e 64 20 74 68 69 73 20 66 75 6e  .** and this fun
303b1 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 76 61  ctions return va
303b2 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c  lue..**.**   SQL
303b3 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 20  ITE_TEMP_STORE  
303b4 20 20 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72     db->temp_stor
303b5 65 20 20 20 20 20 4c 6f 63 61 74 69 6f 6e 20 6f  e     Location o
303b6 66 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  f temporary data
303b7 62 61 73 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  base.**   ------
303b8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20  -----------     
303b9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
303ba 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
303bb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
303bc 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20  -.**   0        
303bd 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79               any
303be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303bf 66 69 6c 65 20 20 20 20 20 20 28 72 65 74 75 72  file      (retur
303c0 6e 20 30 29 0a 2a 2a 20 20 20 31 20 20 20 20 20  n 0).**   1     
303c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303c2 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
303c3 20 20 20 66 69 6c 65 20 20 20 20 20 20 28 72 65     file      (re
303c4 74 75 72 6e 20 30 29 0a 2a 2a 20 20 20 31 20 20  turn 0).**   1  
303c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303c6 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
303c7 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20        memory    
303c8 28 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20  (return 1).**   
303c9 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
303ca 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20        0         
303cb 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 20 20           file   
303cc 20 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a     (return 0).**
303cd 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
303ce 20 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20           1      
303cf 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65              file
303d0 20 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29        (return 0)
303d1 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20  .**   2         
303d2 20 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20              2   
303d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
303d4 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75 72 6e  emory    (return
303d5 20 31 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20   1).**   2      
303d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
303d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303d8 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74    memory    (ret
303d9 75 72 6e 20 31 29 0a 2a 2a 20 20 20 33 20 20 20  urn 1).**   3   
303da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303db 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
303dc 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28       memory    (
303dd 72 65 74 75 72 6e 20 31 29 0a 2a 2f 0a 53 51 4c  return 1).*/.SQL
303de 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
303df 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d  sqlite3TempInMem
303e0 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65  ory(const sqlite
303e1 33 20 2a 64 62 29 7b 0a 23 69 66 20 53 51 4c 49  3 *db){.#if SQLI
303e2 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31  TE_TEMP_STORE==1
303e3 0a 20 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e  .  return ( db->
303e4 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 3b  temp_store==2 );
303e5 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
303e6 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32  TE_TEMP_STORE==2
303e7 0a 20 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e  .  return ( db->
303e8 74 65 6d 70 5f 73 74 6f 72 65 21 3d 31 20 29 3b  temp_store!=1 );
303e9 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
303ea 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 33  TE_TEMP_STORE==3
303eb 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6c  .  return 1;.#el
303ec 73 65 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 23  se.  return 0;.#
303ed 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  endif.}../*.** T
303ee 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
303ef 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20  alled to create 
303f0 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  a connection to 
303f1 61 20 64 61 74 61 62 61 73 65 20 42 54 72 65 65  a database BTree
303f2 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 49 66 20  .** driver.  If 
303f3 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65  zFilename is the
303f4 20 6e 61 6d 65 20 6f 66 20 61 20 66 69 6c 65 2c   name of a file,
303f5 20 74 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20   then that file 
303f6 69 73 0a 2a 2a 20 6f 70 65 6e 65 64 20 61 6e 64  is.** opened and
303f7 20 75 73 65 64 2e 20 20 49 66 20 7a 46 69 6c 65   used.  If zFile
303f8 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61 67 69  name is the magi
303f9 63 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a  c name ":memory:
303fa 22 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61  " then.** the da
303fb 74 61 62 61 73 65 20 69 73 20 73 74 6f 72 65 64  tabase is stored
303fc 20 69 6e 20 6d 65 6d 6f 72 79 20 28 61 6e 64 20   in memory (and 
303fd 69 73 20 74 68 75 73 20 66 6f 72 67 6f 74 74 65  is thus forgotte
303fe 6e 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20  n as soon as.** 
303ff 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  the connection i
30400 73 20 63 6c 6f 73 65 64 2e 29 20 20 49 66 20 7a  s closed.)  If z
30401 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c  Filename is NULL
30402 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   then the databa
30403 73 65 0a 2a 2a 20 69 73 20 61 20 22 76 69 72 74  se.** is a "virt
30404 75 61 6c 22 20 64 61 74 61 62 61 73 65 20 66 6f  ual" database fo
30405 72 20 74 72 61 6e 73 69 65 6e 74 20 75 73 65 20  r transient use 
30406 6f 6e 6c 79 20 61 6e 64 20 69 73 20 64 65 6c 65  only and is dele
30407 74 65 64 20 61 73 0a 2a 2a 20 73 6f 6f 6e 20 61  ted as.** soon a
30408 73 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  s the connection
30409 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a   is closed..**.*
3040a 2a 20 41 20 76 69 72 74 75 61 6c 20 64 61 74 61  * A virtual data
3040b 62 61 73 65 20 63 61 6e 20 62 65 20 65 69 74 68  base can be eith
3040c 65 72 20 61 20 64 69 73 6b 20 66 69 6c 65 20 28  er a disk file (
3040d 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69  that is automati
3040e 63 61 6c 6c 79 0a 2a 2a 20 64 65 6c 65 74 65 64  cally.** deleted
3040f 20 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 69   when the file i
30410 73 20 63 6c 6f 73 65 64 29 20 6f 72 20 69 74 20  s closed) or it 
30411 61 6e 20 62 65 20 68 65 6c 64 20 65 6e 74 69 72  an be held entir
30412 65 6c 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a  ely in memory..*
30413 2a 20 54 68 65 20 73 71 6c 69 74 65 33 54 65 6d  * The sqlite3Tem
30414 70 49 6e 4d 65 6d 6f 72 79 28 29 20 66 75 6e 63  pInMemory() func
30415 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
30416 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 2e  determine which.
30417 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
30418 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
30419 72 65 65 46 61 63 74 6f 72 79 28 0a 20 20 63 6f  reeFactory(.  co
3041a 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  nst sqlite3 *db,
3041b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20          /* Main 
3041c 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 6f 70  database when op
3041d 65 6e 69 6e 67 20 61 75 78 20 6f 74 68 65 72 77  ening aux otherw
3041e 69 73 65 20 30 20 2a 2f 0a 20 20 63 6f 6e 73 74  ise 0 */.  const
3041f 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
30420 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
30421 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  the file contain
30422 69 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61  ing the BTree da
30423 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  tabase */.  int 
30424 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 20 20 20  omitJournal,    
30425 20 20 20 20 20 20 2f 2a 20 69 66 20 54 52 55 45        /* if TRUE
30426 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75   then do not jou
30427 72 6e 61 6c 20 74 68 69 73 20 66 69 6c 65 20 2a  rnal this file *
30428 2f 0a 20 20 69 6e 74 20 6e 43 61 63 68 65 2c 20  /.  int nCache, 
30429 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3042a 20 48 6f 77 20 6d 61 6e 79 20 70 61 67 65 73 20   How many pages 
3042b 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  in the page cach
3042c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c  e */.  int vfsFl
3042d 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
3042e 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64   /* Flags passed
3042f 20 74 68 72 6f 75 67 68 20 74 6f 20 76 66 73 4f   through to vfsO
30430 70 65 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  pen */.  Btree *
30431 2a 70 70 42 74 72 65 65 20 20 20 20 20 20 20 20  *ppBtree        
30432 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
30433 20 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63   new Btree objec
30434 74 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a  t written here *
30435 2f 0a 29 7b 0a 20 20 69 6e 74 20 62 74 46 6c 61  /.){.  int btFla
30436 67 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  gs = 0;.  int rc
30437 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73  ;.  .  assert( s
30438 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
30439 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
3043a 20 20 61 73 73 65 72 74 28 20 70 70 42 74 72 65    assert( ppBtre
3043b 65 20 21 3d 20 30 29 3b 0a 20 20 69 66 28 20 6f  e != 0);.  if( o
3043c 6d 69 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20  mitJournal ){.  
3043d 20 20 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52    btFlags |= BTR
3043e 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b  EE_OMIT_JOURNAL;
3043f 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 66  .  }.  if( db->f
30440 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4e 6f  lags & SQLITE_No
30441 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20  Readlock ){.    
30442 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45  btFlags |= BTREE
30443 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a 20 20  _NO_READLOCK;.  
30444 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
30445 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20  _OMIT_MEMORYDB. 
30446 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d   if( zFilename==
30447 30 20 26 26 20 73 71 6c 69 74 65 33 54 65 6d 70  0 && sqlite3Temp
30448 49 6e 4d 65 6d 6f 72 79 28 64 62 29 20 29 7b 0a  InMemory(db) ){.
30449 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20      zFilename = 
3044a 22 3a 6d 65 6d 6f 72 79 3a 22 3b 0a 20 20 7d 0a  ":memory:";.  }.
3044b 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 28 76  #endif..  if( (v
3044c 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45  fsFlags & SQLITE
3044d 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d  _OPEN_MAIN_DB)!=
3044e 30 20 26 26 20 28 7a 46 69 6c 65 6e 61 6d 65 3d  0 && (zFilename=
3044f 3d 30 20 7c 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65  =0 || *zFilename
30450 3d 3d 30 29 20 29 7b 0a 20 20 20 20 76 66 73 46  ==0) ){.    vfsF
30451 6c 61 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73  lags = (vfsFlags
30452 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   & ~SQLITE_OPEN_
30453 4d 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54  MAIN_DB) | SQLIT
30454 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a  E_OPEN_TEMP_DB;.
30455 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
30456 65 33 42 74 72 65 65 4f 70 65 6e 28 7a 46 69 6c  e3BtreeOpen(zFil
30457 65 6e 61 6d 65 2c 20 28 73 71 6c 69 74 65 33 20  ename, (sqlite3 
30458 2a 29 64 62 2c 20 70 70 42 74 72 65 65 2c 20 62  *)db, ppBtree, b
30459 74 46 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73  tFlags, vfsFlags
3045a 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
3045b 42 2d 54 72 65 65 20 77 61 73 20 73 75 63 63 65  B-Tree was succe
3045c 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20  ssfully opened, 
3045d 73 65 74 20 74 68 65 20 70 61 67 65 72 2d 63 61  set the pager-ca
3045e 63 68 65 20 73 69 7a 65 20 74 6f 20 74 68 65 0a  che size to the.
3045f 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c    ** default val
30460 75 65 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74  ue. Except, if t
30461 68 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65  he call to Btree
30462 4f 70 65 6e 28 29 20 72 65 74 75 72 6e 65 64 20  Open() returned 
30463 61 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6f 70  a handle.  ** op
30464 65 6e 20 6f 6e 20 61 6e 20 65 78 69 73 74 69 6e  en on an existin
30465 67 20 73 68 61 72 65 64 20 70 61 67 65 72 2d 63  g shared pager-c
30466 61 63 68 65 2c 20 64 6f 20 6e 6f 74 20 63 68 61  ache, do not cha
30467 6e 67 65 20 74 68 65 20 70 61 67 65 72 2d 63 61  nge the pager-ca
30468 63 68 65 20 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a  che .  ** size..
30469 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53    */.  if( rc==S
3046a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73  QLITE_OK && 0==s
3046b 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d  qlite3BtreeSchem
3046c 61 28 2a 70 70 42 74 72 65 65 2c 20 30 2c 20 30  a(*ppBtree, 0, 0
3046d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
3046e 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
3046f 65 28 2a 70 70 42 74 72 65 65 2c 20 6e 43 61 63  e(*ppBtree, nCac
30470 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  he);.  }.  retur
30471 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
30472 65 74 75 72 6e 20 55 54 46 2d 38 20 65 6e 63 6f  eturn UTF-8 enco
30473 64 65 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67  ded English lang
30474 75 61 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e  uage explanation
30475 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
30476 65 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f  ent.** error..*/
30477 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
30478 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
30479 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 20 2a  errmsg(sqlite3 *
3047a 64 62 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  db){.  const cha
3047b 72 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62 20  r *z;.  if( !db 
3047c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
3047d 6c 69 74 65 33 45 72 72 53 74 72 28 53 51 4c 49  lite3ErrStr(SQLI
3047e 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 0a 20  TE_NOMEM);.  }. 
3047f 20 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66   if( !sqlite3Saf
30480 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b  etyCheckSickOrOk
30481 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75  (db) ){.    retu
30482 72 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  rn sqlite3ErrStr
30483 28 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 29 3b  (SQLITE_MISUSE);
30484 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
30485 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
30486 75 74 65 78 29 3b 0a 20 20 69 66 28 20 64 62 2d  utex);.  if( db-
30487 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
30488 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  .    z = sqlite3
30489 45 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f  ErrStr(SQLITE_NO
3048a 4d 45 4d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  MEM);.  }else{. 
3048b 20 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 73 71     z = (char*)sq
3048c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
3048d 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20  (db->pErr);.    
3048e 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c  assert( !db->mal
3048f 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
30490 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20   if( z==0 ){.   
30491 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 45 72     z = sqlite3Er
30492 72 53 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65  rStr(db->errCode
30493 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
30494 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
30495 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
30496 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69   return z;.}..#i
30497 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
30498 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65  T_UTF16./*.** Re
30499 74 75 72 6e 20 55 54 46 2d 31 36 20 65 6e 63 6f  turn UTF-16 enco
3049a 64 65 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67  ded English lang
3049b 75 61 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e  uage explanation
3049c 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
3049d 65 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f  ent.** error..*/
3049e 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
3049f 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
304a0 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65 33  errmsg16(sqlite3
304a1 20 2a 64 62 29 7b 0a 20 20 73 74 61 74 69 63 20   *db){.  static 
304a2 63 6f 6e 73 74 20 75 31 36 20 6f 75 74 4f 66 4d  const u16 outOfM
304a3 65 6d 5b 5d 20 3d 20 7b 0a 20 20 20 20 27 6f 27  em[] = {.    'o'
304a4 2c 20 27 75 27 2c 20 27 74 27 2c 20 27 20 27 2c  , 'u', 't', ' ',
304a5 20 27 6f 27 2c 20 27 66 27 2c 20 27 20 27 2c 20   'o', 'f', ' ', 
304a6 27 6d 27 2c 20 27 65 27 2c 20 27 6d 27 2c 20 27  'm', 'e', 'm', '
304a7 6f 27 2c 20 27 72 27 2c 20 27 79 27 2c 20 30 0a  o', 'r', 'y', 0.
304a8 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f    };.  static co
304a9 6e 73 74 20 75 31 36 20 6d 69 73 75 73 65 5b 5d  nst u16 misuse[]
304aa 20 3d 20 7b 0a 20 20 20 20 27 6c 27 2c 20 27 69   = {.    'l', 'i
304ab 27 2c 20 27 62 27 2c 20 27 72 27 2c 20 27 61 27  ', 'b', 'r', 'a'
304ac 2c 20 27 72 27 2c 20 27 79 27 2c 20 27 20 27 2c  , 'r', 'y', ' ',
304ad 20 0a 20 20 20 20 27 72 27 2c 20 27 6f 27 2c 20   .    'r', 'o', 
304ae 27 75 27 2c 20 27 74 27 2c 20 27 69 27 2c 20 27  'u', 't', 'i', '
304af 6e 27 2c 20 27 65 27 2c 20 27 20 27 2c 20 0a 20  n', 'e', ' ', . 
304b0 20 20 20 27 63 27 2c 20 27 61 27 2c 20 27 6c 27     'c', 'a', 'l'
304b1 2c 20 27 6c 27 2c 20 27 65 27 2c 20 27 64 27 2c  , 'l', 'e', 'd',
304b2 20 27 20 27 2c 20 0a 20 20 20 20 27 6f 27 2c 20   ' ', .    'o', 
304b3 27 75 27 2c 20 27 74 27 2c 20 27 20 27 2c 20 0a  'u', 't', ' ', .
304b4 20 20 20 20 27 6f 27 2c 20 27 66 27 2c 20 27 20      'o', 'f', ' 
304b5 27 2c 20 0a 20 20 20 20 27 73 27 2c 20 27 65 27  ', .    's', 'e'
304b6 2c 20 27 71 27 2c 20 27 75 27 2c 20 27 65 27 2c  , 'q', 'u', 'e',
304b7 20 27 6e 27 2c 20 27 63 27 2c 20 27 65 27 2c 20   'n', 'c', 'e', 
304b8 30 0a 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73 74 20  0.  };..  const 
304b9 76 6f 69 64 20 2a 7a 3b 0a 20 20 69 66 28 20 21  void *z;.  if( !
304ba 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  db ){.    return
304bb 20 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d 65   (void *)outOfMe
304bc 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71  m;.  }.  if( !sq
304bd 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
304be 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a  SickOrOk(db) ){.
304bf 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64      return (void
304c0 20 2a 29 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20   *)misuse;.  }. 
304c1 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
304c2 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
304c3 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
304c4 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 7a  cFailed ){.    z
304c5 20 3d 20 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66   = (void *)outOf
304c6 4d 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Mem;.  }else{.  
304c7 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61    z = sqlite3_va
304c8 6c 75 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70  lue_text16(db->p
304c9 45 72 72 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d  Err);.    if( z=
304ca 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
304cb 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64  te3ValueSetStr(d
304cc 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 73 71 6c  b->pErr, -1, sql
304cd 69 74 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65  ite3ErrStr(db->e
304ce 72 72 43 6f 64 65 29 2c 0a 20 20 20 20 20 20 20  rrCode),.       
304cf 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c      SQLITE_UTF8,
304d0 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
304d1 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  .      z = sqlit
304d2 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
304d3 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 7d  db->pErr);.    }
304d4 0a 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63  .    /* A malloc
304d5 28 29 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c  () may have fail
304d6 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 63 61  ed within the ca
304d7 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61  ll to sqlite3_va
304d8 6c 75 65 5f 74 65 78 74 31 36 28 29 0a 20 20 20  lue_text16().   
304d9 20 2a 2a 20 61 62 6f 76 65 2e 20 49 66 20 74 68   ** above. If th
304da 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
304db 74 68 65 6e 20 74 68 65 20 64 62 2d 3e 6d 61 6c  then the db->mal
304dc 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6e  locFailed flag n
304dd 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62  eeds to.    ** b
304de 65 20 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65  e cleared before
304df 20 72 65 74 75 72 6e 69 6e 67 2e 20 44 6f 20 74   returning. Do t
304e0 68 69 73 20 64 69 72 65 63 74 6c 79 2c 20 69 6e  his directly, in
304e1 73 74 65 61 64 20 6f 66 20 76 69 61 0a 20 20 20  stead of via.   
304e2 20 2a 2a 20 73 71 6c 69 74 65 33 41 70 69 45 78   ** sqlite3ApiEx
304e3 69 74 28 29 2c 20 74 6f 20 61 76 6f 69 64 20 73  it(), to avoid s
304e4 65 74 74 69 6e 67 20 74 68 65 20 64 61 74 61 62  etting the datab
304e5 61 73 65 20 68 61 6e 64 6c 65 20 65 72 72 6f 72  ase handle error
304e6 20 6d 65 73 73 61 67 65 2e 0a 20 20 20 20 2a 2f   message..    */
304e7 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
304e8 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20  ailed = 0;.  }. 
304e9 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
304ea 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
304eb 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23  .  return z;.}.#
304ec 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
304ed 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
304ee 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
304ef 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f  most recent erro
304f0 72 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  r code generated
304f1 20 62 79 20 61 6e 20 53 51 4c 69 74 65 20 72 6f   by an SQLite ro
304f2 75 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c 20 69  utine. If NULL i
304f3 73 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 74  s.** passed to t
304f4 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 77 65  his function, we
304f5 20 61 73 73 75 6d 65 20 61 20 6d 61 6c 6c 6f 63   assume a malloc
304f6 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67  () failed during
304f7 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e   sqlite3_open().
304f8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
304f9 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nt sqlite3_errco
304fa 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  de(sqlite3 *db){
304fb 0a 20 20 69 66 28 20 64 62 20 26 26 20 21 73 71  .  if( db && !sq
304fc 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
304fd 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a  SickOrOk(db) ){.
304fe 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
304ff 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
30500 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d  if( !db || db->m
30501 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
30502 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
30503 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65  _NOMEM;.  }.  re
30504 74 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65  turn db->errCode
30505 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a   & db->errMask;.
30506 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
30507 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
30508 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65  d_errcode(sqlite
30509 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62  3 *db){.  if( db
3050a 20 26 26 20 21 73 71 6c 69 74 65 33 53 61 66 65   && !sqlite3Safe
3050b 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
3050c 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  db) ){.    retur
3050d 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
3050e 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c  .  }.  if( !db |
3050f 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
30510 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
30511 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
30512 20 7d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e   }.  return db->
30513 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  errCode;.}../*.*
30514 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63  * Create a new c
30515 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f  ollating functio
30516 6e 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 22  n for database "
30517 64 62 22 2e 20 20 54 68 65 20 6e 61 6d 65 20 69  db".  The name i
30518 73 20 7a 4e 61 6d 65 0a 2a 2a 20 61 6e 64 20 74  s zName.** and t
30519 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 65  he encoding is e
3051a 6e 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nc..*/.static in
3051b 74 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f  t createCollatio
3051c 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62  n(.  sqlite3* db
3051d 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
3051e 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65  *zName, .  int e
3051f 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74  nc, .  void* pCt
30520 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  x,.  int(*xCompa
30521 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
30522 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
30523 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f  nst void*),.  vo
30524 69 64 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  id(*xDel)(void*)
30525 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  .){.  CollSeq *p
30526 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 65 6e 63 32  Coll;.  int enc2
30527 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20  ;.  int nName;. 
30528 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69   .  assert( sqli
30529 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
3052a 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
3052b 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 55 54 46  /* If SQLITE_UTF
3052c 31 36 20 69 73 20 73 70 65 63 69 66 69 65 64 20  16 is specified 
3052d 61 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  as the encoding 
3052e 74 79 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20  type, transform 
3052f 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65  this.  ** to one
30530 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36   of SQLITE_UTF16
30531 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46  LE or SQLITE_UTF
30532 31 36 42 45 20 75 73 69 6e 67 20 74 68 65 0a 20  16BE using the. 
30533 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36   ** SQLITE_UTF16
30534 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e 20 53 51  NATIVE macro. SQ
30535 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 6e 6f  LITE_UTF16 is no
30536 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  t used internall
30537 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e 63 32 20 3d  y..  */.  enc2 =
30538 20 65 6e 63 3b 0a 20 20 74 65 73 74 63 61 73 65   enc;.  testcase
30539 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55  ( enc2==SQLITE_U
3053a 54 46 31 36 20 29 3b 0a 20 20 74 65 73 74 63 61  TF16 );.  testca
3053b 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45  se( enc2==SQLITE
3053c 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 29  _UTF16_ALIGNED )
3053d 3b 0a 20 20 69 66 28 20 65 6e 63 32 3d 3d 53 51  ;.  if( enc2==SQ
3053e 4c 49 54 45 5f 55 54 46 31 36 20 7c 7c 20 65 6e  LITE_UTF16 || en
3053f 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  c2==SQLITE_UTF16
30540 5f 41 4c 49 47 4e 45 44 20 29 7b 0a 20 20 20 20  _ALIGNED ){.    
30541 65 6e 63 32 20 3d 20 53 51 4c 49 54 45 5f 55 54  enc2 = SQLITE_UT
30542 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 0a 20  F16NATIVE;.  }. 
30543 20 69 66 28 20 65 6e 63 32 3c 53 51 4c 49 54 45   if( enc2<SQLITE
30544 5f 55 54 46 38 20 7c 7c 20 65 6e 63 32 3e 53 51  _UTF8 || enc2>SQ
30545 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 7b 0a  LITE_UTF16BE ){.
30546 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
30547 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20  E_MISUSE;.  }.. 
30548 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 69   /* Check if thi
30549 73 20 63 61 6c 6c 20 69 73 20 72 65 6d 6f 76 69  s call is removi
3054a 6e 67 20 6f 72 20 72 65 70 6c 61 63 69 6e 67 20  ng or replacing 
3054b 61 6e 20 65 78 69 73 74 69 6e 67 20 63 6f 6c 6c  an existing coll
3054c 61 74 69 6f 6e 20 0a 20 20 2a 2a 20 73 65 71 75  ation .  ** sequ
3054d 65 6e 63 65 2e 20 49 66 20 73 6f 2c 20 61 6e 64  ence. If so, and
3054e 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
3054f 65 20 56 4d 73 2c 20 72 65 74 75 72 6e 20 62 75  e VMs, return bu
30550 73 79 2e 20 49 66 20 74 68 65 72 65 0a 20 20 2a  sy. If there.  *
30551 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20  * are no active 
30552 56 4d 73 2c 20 69 6e 76 61 6c 69 64 61 74 65 20  VMs, invalidate 
30553 61 6e 79 20 70 72 65 2d 63 6f 6d 70 69 6c 65 64  any pre-compiled
30554 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a   statements..  *
30555 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  /.  nName = sqli
30556 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
30557 65 29 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71  e);.  pColl = sq
30558 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
30559 28 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a  (db, (u8)enc2, z
3055a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b  Name, nName, 0);
3055b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 26 26 20  .  if( pColl && 
3055c 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 7b 0a 20  pColl->xCmp ){. 
3055d 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76     if( db->activ
3055e 65 56 64 62 65 43 6e 74 20 29 7b 0a 20 20 20 20  eVdbeCnt ){.    
3055f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
30560 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20  b, SQLITE_BUSY, 
30561 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65  .        "unable
30562 20 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69 66   to delete/modif
30563 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  y collation sequ
30564 65 6e 63 65 20 64 75 65 20 74 6f 20 61 63 74 69  ence due to acti
30565 76 65 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b  ve statements");
30566 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
30567 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
30568 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 69  .    sqlite3Expi
30569 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d  rePreparedStatem
3056a 65 6e 74 73 28 64 62 29 3b 0a 0a 20 20 20 20 2f  ents(db);..    /
3056b 2a 20 49 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  * If collation s
3056c 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 77 61  equence pColl wa
3056d 73 20 63 72 65 61 74 65 64 20 64 69 72 65 63 74  s created direct
3056e 6c 79 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ly by a call to.
3056f 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63      ** sqlite3_c
30570 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c  reate_collation,
30571 20 61 6e 64 20 6e 6f 74 20 67 65 6e 65 72 61 74   and not generat
30572 65 64 20 62 79 20 73 79 6e 74 68 43 6f 6c 6c 53  ed by synthCollS
30573 65 71 28 29 2c 0a 20 20 20 20 2a 2a 20 74 68 65  eq(),.    ** the
30574 6e 20 61 6e 79 20 63 6f 70 69 65 73 20 6d 61 64  n any copies mad
30575 65 20 62 79 20 73 79 6e 74 68 43 6f 6c 6c 53 65  e by synthCollSe
30576 71 28 29 20 6e 65 65 64 20 74 6f 20 62 65 20 69  q() need to be i
30577 6e 76 61 6c 69 64 61 74 65 64 2e 0a 20 20 20 20  nvalidated..    
30578 2a 2a 20 41 6c 73 6f 2c 20 63 6f 6c 6c 61 74 69  ** Also, collati
30579 6f 6e 20 64 65 73 74 72 75 63 74 6f 72 20 2d 20  on destructor - 
3057a 43 6f 6c 6c 53 65 71 2e 78 44 65 6c 28 29 20 2d  CollSeq.xDel() -
3057b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6e 65   function may ne
3057c 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20  ed.    ** to be 
3057d 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2f 20 0a  called..    */ .
3057e 20 20 20 20 69 66 28 20 28 70 43 6f 6c 6c 2d 3e      if( (pColl->
3057f 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54  enc & ~SQLITE_UT
30580 46 31 36 5f 41 4c 49 47 4e 45 44 29 3d 3d 65 6e  F16_ALIGNED)==en
30581 63 32 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c  c2 ){.      Coll
30582 53 65 71 20 2a 61 43 6f 6c 6c 20 3d 20 73 71 6c  Seq *aColl = sql
30583 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62  ite3HashFind(&db
30584 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 7a 4e 61 6d  ->aCollSeq, zNam
30585 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 20  e, nName);.     
30586 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f   int j;.      fo
30587 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b 29  r(j=0; j<3; j++)
30588 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65  {.        CollSe
30589 71 20 2a 70 20 3d 20 26 61 43 6f 6c 6c 5b 6a 5d  q *p = &aColl[j]
3058a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d  ;.        if( p-
3058b 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63  >enc==pColl->enc
3058c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
3058d 28 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20  ( p->xDel ){.   
3058e 20 20 20 20 20 20 20 20 20 70 2d 3e 78 44 65 6c           p->xDel
3058f 28 70 2d 3e 70 55 73 65 72 29 3b 0a 20 20 20 20  (p->pUser);.    
30590 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
30591 20 20 70 2d 3e 78 43 6d 70 20 3d 20 30 3b 0a 20    p->xCmp = 0;. 
30592 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
30593 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 43  .    }.  }..  pC
30594 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
30595 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75 38  dCollSeq(db, (u8
30596 29 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  )enc2, zName, nN
30597 61 6d 65 2c 20 31 29 3b 0a 20 20 69 66 28 20 70  ame, 1);.  if( p
30598 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Coll ){.    pCol
30599 6c 2d 3e 78 43 6d 70 20 3d 20 78 43 6f 6d 70 61  l->xCmp = xCompa
3059a 72 65 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 70  re;.    pColl->p
3059b 55 73 65 72 20 3d 20 70 43 74 78 3b 0a 20 20 20  User = pCtx;.   
3059c 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20 78   pColl->xDel = x
3059d 44 65 6c 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e  Del;.    pColl->
3059e 65 6e 63 20 3d 20 28 75 38 29 28 65 6e 63 32 20  enc = (u8)(enc2 
3059f 7c 20 28 65 6e 63 20 26 20 53 51 4c 49 54 45 5f  | (enc & SQLITE_
305a0 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 29 3b  UTF16_ALIGNED));
305a1 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72  .  }.  sqlite3Er
305a2 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
305a3 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  K, 0);.  return 
305a4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
305a5 2a 0a 2a 2a 20 54 68 69 73 20 61 72 72 61 79 20  *.** This array 
305a6 64 65 66 69 6e 65 73 20 68 61 72 64 20 75 70 70  defines hard upp
305a7 65 72 20 62 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d  er bounds on lim
305a8 69 74 20 76 61 6c 75 65 73 2e 20 20 54 68 65 0a  it values.  The.
305a9 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 6d  ** initializer m
305aa 75 73 74 20 62 65 20 6b 65 70 74 20 69 6e 20 73  ust be kept in s
305ab 79 6e 63 20 77 69 74 68 20 74 68 65 20 53 51 4c  ync with the SQL
305ac 49 54 45 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23  ITE_LIMIT_*.** #
305ad 64 65 66 69 6e 65 73 20 69 6e 20 73 71 6c 69 74  defines in sqlit
305ae 65 33 2e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  e3.h..*/.static 
305af 63 6f 6e 73 74 20 69 6e 74 20 61 48 61 72 64 4c  const int aHardL
305b0 69 6d 69 74 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c  imit[] = {.  SQL
305b1 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a  ITE_MAX_LENGTH,.
305b2 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c    SQLITE_MAX_SQL
305b3 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54  _LENGTH,.  SQLIT
305b4 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20  E_MAX_COLUMN,.  
305b5 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
305b6 44 45 50 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f  DEPTH,.  SQLITE_
305b7 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
305b8 45 43 54 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  ECT,.  SQLITE_MA
305b9 58 5f 56 44 42 45 5f 4f 50 2c 0a 20 20 53 51 4c  X_VDBE_OP,.  SQL
305ba 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
305bb 5f 41 52 47 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  _ARG,.  SQLITE_M
305bc 41 58 5f 41 54 54 41 43 48 45 44 2c 0a 20 20 53  AX_ATTACHED,.  S
305bd 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
305be 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20  ATTERN_LENGTH,. 
305bf 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49   SQLITE_MAX_VARI
305c0 41 42 4c 45 5f 4e 55 4d 42 45 52 2c 0a 7d 3b 0a  ABLE_NUMBER,.};.
305c1 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
305c2 20 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 73   the hard limits
305c3 20 61 72 65 20 73 65 74 20 74 6f 20 72 65 61 73   are set to reas
305c4 6f 6e 61 62 6c 65 20 76 61 6c 75 65 73 0a 2a 2f  onable values.*/
305c5 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
305c6 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20 65 72 72  LENGTH<100.# err
305c7 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  or SQLITE_MAX_LE
305c8 4e 47 54 48 20 6d 75 73 74 20 62 65 20 61 74 20  NGTH must be at 
305c9 6c 65 61 73 74 20 31 30 30 0a 23 65 6e 64 69 66  least 100.#endif
305ca 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
305cb 53 51 4c 5f 4c 45 4e 47 54 48 3c 31 30 30 0a 23  SQL_LENGTH<100.#
305cc 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
305cd 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75 73  X_SQL_LENGTH mus
305ce 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 30  t be at least 10
305cf 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  0.#endif.#if SQL
305d0 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
305d1 54 48 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  TH>SQLITE_MAX_LE
305d2 4e 47 54 48 0a 23 20 65 72 72 6f 72 20 53 51 4c  NGTH.# error SQL
305d3 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
305d4 54 48 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 67  TH must not be g
305d5 72 65 61 74 65 72 20 74 68 61 6e 20 53 51 4c 49  reater than SQLI
305d6 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 65  TE_MAX_LENGTH.#e
305d7 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
305d8 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
305d9 45 43 54 3c 32 0a 23 20 65 72 72 6f 72 20 53 51  ECT<2.# error SQ
305da 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e  LITE_MAX_COMPOUN
305db 44 5f 53 45 4c 45 43 54 20 6d 75 73 74 20 62 65  D_SELECT must be
305dc 20 61 74 20 6c 65 61 73 74 20 32 0a 23 65 6e 64   at least 2.#end
305dd 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
305de 58 5f 56 44 42 45 5f 4f 50 3c 34 30 0a 23 20 65  X_VDBE_OP<40.# e
305df 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
305e0 56 44 42 45 5f 4f 50 20 6d 75 73 74 20 62 65 20  VDBE_OP must be 
305e1 61 74 20 6c 65 61 73 74 20 34 30 0a 23 65 6e 64  at least 40.#end
305e2 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
305e3 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c 30  X_FUNCTION_ARG<0
305e4 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46   || SQLITE_MAX_F
305e5 55 4e 43 54 49 4f 4e 5f 41 52 47 3e 31 30 30 30  UNCTION_ARG>1000
305e6 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
305e7 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
305e8 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
305e9 20 30 20 61 6e 64 20 31 30 30 30 0a 23 65 6e 64   0 and 1000.#end
305ea 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
305eb 58 5f 41 54 54 41 43 48 45 44 3c 30 20 7c 7c 20  X_ATTACHED<0 || 
305ec 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
305ed 48 45 44 3e 33 30 0a 23 20 65 72 72 6f 72 20 53  HED>30.# error S
305ee 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
305ef 45 44 20 6d 75 73 74 20 62 65 20 62 65 74 77 65  ED must be betwe
305f0 65 6e 20 30 20 61 6e 64 20 33 30 0a 23 65 6e 64  en 0 and 30.#end
305f1 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
305f2 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
305f3 45 4e 47 54 48 3c 31 0a 23 20 65 72 72 6f 72 20  ENGTH<1.# error 
305f4 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f  SQLITE_MAX_LIKE_
305f5 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 6d  PATTERN_LENGTH m
305f6 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20  ust be at least 
305f7 31 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  1.#endif.#if SQL
305f8 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45  ITE_MAX_VARIABLE
305f9 5f 4e 55 4d 42 45 52 3c 31 0a 23 20 65 72 72 6f  _NUMBER<1.# erro
305fa 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52  r SQLITE_MAX_VAR
305fb 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 6d 75 73  IABLE_NUMBER mus
305fc 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a  t be at least 1.
305fd 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
305fe 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32 37  E_MAX_COLUMN>327
305ff 36 37 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  67.# error SQLIT
30600 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 6d 75 73  E_MAX_COLUMN mus
30601 74 20 6e 6f 74 20 65 78 63 65 65 64 20 33 32 37  t not exceed 327
30602 36 37 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  67.#endif.../*.*
30603 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
30604 75 65 20 6f 66 20 61 20 6c 69 6d 69 74 2e 20 20  ue of a limit.  
30605 52 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76  Report the old v
30606 61 6c 75 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69  alue..** If an i
30607 6e 76 61 6c 69 64 20 6c 69 6d 69 74 20 69 6e 64  nvalid limit ind
30608 65 78 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20  ex is supplied, 
30609 72 65 70 6f 72 74 20 2d 31 2e 0a 2a 2a 20 4d 61  report -1..** Ma
3060a 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 62 75  ke no changes bu
3060b 74 20 73 74 69 6c 6c 20 72 65 70 6f 72 74 20 74  t still report t
3060c 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 69 66 20  he old value if 
3060d 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74  the.** new limit
3060e 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a   is negative..**
3060f 0a 2a 2a 20 41 20 6e 65 77 20 6c 6f 77 65 72 20  .** A new lower 
30610 6c 69 6d 69 74 20 64 6f 65 73 20 6e 6f 74 20 73  limit does not s
30611 68 72 69 6e 6b 20 65 78 69 73 74 69 6e 67 20 63  hrink existing c
30612 6f 6e 73 74 72 75 63 74 73 2e 0a 2a 2a 20 49 74  onstructs..** It
30613 20 6d 65 72 65 6c 79 20 70 72 65 76 65 6e 74 73   merely prevents
30614 20 6e 65 77 20 63 6f 6e 73 74 72 75 63 74 73 20   new constructs 
30615 74 68 61 74 20 65 78 63 65 65 64 20 74 68 65 20  that exceed the 
30616 6c 69 6d 69 74 0a 2a 2a 20 66 72 6f 6d 20 66 6f  limit.** from fo
30617 72 6d 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rming..*/.SQLITE
30618 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
30619 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  _limit(sqlite3 *
3061a 64 62 2c 20 69 6e 74 20 6c 69 6d 69 74 49 64 2c  db, int limitId,
3061b 20 69 6e 74 20 6e 65 77 4c 69 6d 69 74 29 7b 0a   int newLimit){.
3061c 20 20 69 6e 74 20 6f 6c 64 4c 69 6d 69 74 3b 0a    int oldLimit;.
3061d 20 20 69 66 28 20 6c 69 6d 69 74 49 64 3c 30 20    if( limitId<0 
3061e 7c 7c 20 6c 69 6d 69 74 49 64 3e 3d 53 51 4c 49  || limitId>=SQLI
3061f 54 45 5f 4e 5f 4c 49 4d 49 54 20 29 7b 0a 20 20  TE_N_LIMIT ){.  
30620 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d    return -1;.  }
30621 0a 20 20 6f 6c 64 4c 69 6d 69 74 20 3d 20 64 62  .  oldLimit = db
30622 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64  ->aLimit[limitId
30623 5d 3b 0a 20 20 69 66 28 20 6e 65 77 4c 69 6d 69  ];.  if( newLimi
30624 74 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t>=0 ){.    if( 
30625 6e 65 77 4c 69 6d 69 74 3e 61 48 61 72 64 4c 69  newLimit>aHardLi
30626 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 20 29 7b 0a  mit[limitId] ){.
30627 20 20 20 20 20 20 6e 65 77 4c 69 6d 69 74 20 3d        newLimit =
30628 20 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69   aHardLimit[limi
30629 74 49 64 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  tId];.    }.    
3062a 64 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74  db->aLimit[limit
3062b 49 64 5d 20 3d 20 6e 65 77 4c 69 6d 69 74 3b 0a  Id] = newLimit;.
3062c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 6c 64    }.  return old
3062d 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Limit;.}../*.** 
3062e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
3062f 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 6f 70  s the work of op
30630 65 6e 69 6e 67 20 61 20 64 61 74 61 62 61 73 65  ening a database
30631 20 6f 6e 20 62 65 68 61 6c 66 20 6f 66 0a 2a 2a   on behalf of.**
30632 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
30633 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  and sqlite3_open
30634 31 36 28 29 2e 20 54 68 65 20 64 61 74 61 62 61  16(). The databa
30635 73 65 20 66 69 6c 65 6e 61 6d 65 20 22 7a 46 69  se filename "zFi
30636 6c 65 6e 61 6d 65 22 20 20 0a 2a 2a 20 69 73 20  lename"  .** is 
30637 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 0a 2a  UTF-8 encoded..*
30638 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65  /.static int ope
30639 6e 44 61 74 61 62 61 73 65 28 0a 20 20 63 6f 6e  nDatabase(.  con
3063a 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
3063b 6d 65 2c 20 2f 2a 20 44 61 74 61 62 61 73 65 20  me, /* Database 
3063c 66 69 6c 65 6e 61 6d 65 20 55 54 46 2d 38 20 65  filename UTF-8 e
3063d 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69  ncoded */.  sqli
3063e 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20 20  te3 **ppDb,     
3063f 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72     /* OUT: Retur
30640 6e 65 64 20 64 61 74 61 62 61 73 65 20 68 61 6e  ned database han
30641 64 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  dle */.  unsigne
30642 64 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  d flags,        
30643 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 61 6c 20 66  /* Operational f
30644 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  lags */.  const 
30645 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20  char *zVfs      
30646 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
30647 56 46 53 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b  VFS to use */.){
30648 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
30649 20 20 69 6e 74 20 72 63 3b 0a 20 20 43 6f 6c 6c    int rc;.  Coll
3064a 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e  Seq *pColl;.  in
3064b 74 20 69 73 54 68 72 65 61 64 73 61 66 65 3b 0a  t isThreadsafe;.
3064c 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23 69  .  *ppDb = 0;.#i
3064d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3064e 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20  T_AUTOINIT.  rc 
3064f 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  = sqlite3_initia
30650 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63  lize();.  if( rc
30651 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65   ) return rc;.#e
30652 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69  ndif..  if( sqli
30653 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
30654 62 43 6f 72 65 4d 75 74 65 78 3d 3d 30 20 29 7b  bCoreMutex==0 ){
30655 0a 20 20 20 20 69 73 54 68 72 65 61 64 73 61 66  .    isThreadsaf
30656 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69  e = 0;.  }else i
30657 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
30658 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 29  E_OPEN_NOMUTEX )
30659 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64 73 61  {.    isThreadsa
3065a 66 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  fe = 0;.  }else 
3065b 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
3065c 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45  TE_OPEN_FULLMUTE
3065d 58 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65 61  X ){.    isThrea
3065e 64 73 61 66 65 20 3d 20 31 3b 0a 20 20 7d 65 6c  dsafe = 1;.  }el
3065f 73 65 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64  se{.    isThread
30660 73 61 66 65 20 3d 20 73 71 6c 69 74 65 33 47 6c  safe = sqlite3Gl
30661 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c  obalConfig.bFull
30662 4d 75 74 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Mutex;.  }..  /*
30663 20 52 65 6d 6f 76 65 20 68 61 72 6d 66 75 6c 20   Remove harmful 
30664 62 69 74 73 20 66 72 6f 6d 20 74 68 65 20 66 6c  bits from the fl
30665 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  ags parameter */
30666 0a 20 20 66 6c 61 67 73 20 26 3d 20 20 7e 28 20  .  flags &=  ~( 
30667 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
30668 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20  TEONCLOSE |.    
30669 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
3066a 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c  E_OPEN_MAIN_DB |
3066b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3066c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
3066d 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20 20 20  _DB | .         
3066e 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
3066f 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 7c  N_TRANSIENT_DB |
30670 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
30671 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49   SQLITE_OPEN_MAI
30672 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20  N_JOURNAL | .   
30673 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
30674 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55  TE_OPEN_TEMP_JOU
30675 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20  RNAL | .        
30676 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
30677 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 7c 20  EN_SUBJOURNAL | 
30678 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
30679 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54  SQLITE_OPEN_MAST
3067a 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 0a 20 20 20  ER_JOURNAL |.   
3067b 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
3067c 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20  TE_OPEN_NOMUTEX 
3067d 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
3067e 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c   SQLITE_OPEN_FUL
3067f 4c 4d 55 54 45 58 0a 20 20 20 20 20 20 20 20 20  LMUTEX.         
30680 20 20 20 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c      );..  /* All
30681 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  ocate the sqlite
30682 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
30683 2a 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  */.  db = sqlite
30684 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a  3MallocZero( siz
30685 65 6f 66 28 73 71 6c 69 74 65 33 29 20 29 3b 0a  eof(sqlite3) );.
30686 20 20 69 66 28 20 64 62 3d 3d 30 20 29 20 67 6f    if( db==0 ) go
30687 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20  to opendb_out;. 
30688 20 69 66 28 20 69 73 54 68 72 65 61 64 73 61 66   if( isThreadsaf
30689 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 75 74  e ){.    db->mut
3068a 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
3068b 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
3068c 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a  TEX_RECURSIVE);.
3068d 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 75 74 65      if( db->mute
3068e 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  x==0 ){.      sq
3068f 6c 69 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a  lite3_free(db);.
30690 20 20 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20        db = 0;.  
30691 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
30692 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  out;.    }.  }. 
30693 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
30694 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
30695 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d  .  db->errMask =
30696 20 30 78 66 66 3b 0a 20 20 64 62 2d 3e 70 72 69   0xff;.  db->pri
30697 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 30 3b 0a  orNewRowid = 0;.
30698 20 20 64 62 2d 3e 6e 44 62 20 3d 20 32 3b 0a 20    db->nDb = 2;. 
30699 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
3069a 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b 0a  ITE_MAGIC_BUSY;.
3069b 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e    db->aDb = db->
3069c 61 44 62 53 74 61 74 69 63 3b 0a 0a 20 20 61 73  aDbStatic;..  as
3069d 73 65 72 74 28 20 73 69 7a 65 6f 66 28 64 62 2d  sert( sizeof(db-
3069e 3e 61 4c 69 6d 69 74 29 3d 3d 73 69 7a 65 6f 66  >aLimit)==sizeof
3069f 28 61 48 61 72 64 4c 69 6d 69 74 29 20 29 3b 0a  (aHardLimit) );.
306a0 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 4c 69    memcpy(db->aLi
306a1 6d 69 74 2c 20 61 48 61 72 64 4c 69 6d 69 74 2c  mit, aHardLimit,
306a2 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c 69 6d   sizeof(db->aLim
306a3 69 74 29 29 3b 0a 20 20 64 62 2d 3e 61 75 74 6f  it));.  db->auto
306a4 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 64 62  Commit = 1;.  db
306a5 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 20 3d 20  ->nextAutovac = 
306a6 2d 31 3b 0a 20 20 64 62 2d 3e 6e 65 78 74 50 61  -1;.  db->nextPa
306a7 67 65 73 69 7a 65 20 3d 20 30 3b 0a 20 20 64 62  gesize = 0;.  db
306a8 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
306a9 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 0a  E_ShortColNames.
306aa 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  #if SQLITE_DEFAU
306ab 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3c 34  LT_FILE_FORMAT<4
306ac 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
306ad 20 20 7c 20 53 51 4c 49 54 45 5f 4c 65 67 61 63    | SQLITE_Legac
306ae 79 46 69 6c 65 46 6d 74 0a 23 65 6e 64 69 66 0a  yFileFmt.#endif.
306af 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
306b0 41 42 4c 45 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  ABLE_LOAD_EXTENS
306b1 49 4f 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20  ION.            
306b2 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c 6f       | SQLITE_Lo
306b3 61 64 45 78 74 65 6e 73 69 6f 6e 0a 23 65 6e 64  adExtension.#end
306b4 69 66 0a 20 20 20 20 20 20 3b 0a 20 20 73 71 6c  if.      ;.  sql
306b5 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 64 62  ite3HashInit(&db
306b6 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66  ->aCollSeq);.#if
306b7 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
306b8 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
306b9 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
306ba 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 0a 23  &db->aModule);.#
306bb 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 70 56 66  endif..  db->pVf
306bc 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  s = sqlite3_vfs_
306bd 66 69 6e 64 28 7a 56 66 73 29 3b 0a 20 20 69 66  find(zVfs);.  if
306be 28 20 21 64 62 2d 3e 70 56 66 73 20 29 7b 0a 20  ( !db->pVfs ){. 
306bf 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
306c0 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65  RROR;.    sqlite
306c1 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22  3Error(db, rc, "
306c2 6e 6f 20 73 75 63 68 20 76 66 73 3a 20 25 73 22  no such vfs: %s"
306c3 2c 20 7a 56 66 73 29 3b 0a 20 20 20 20 67 6f 74  , zVfs);.    got
306c4 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20  o opendb_out;.  
306c5 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  }..  /* Add the 
306c6 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f  default collatio
306c7 6e 20 73 65 71 75 65 6e 63 65 20 42 49 4e 41 52  n sequence BINAR
306c8 59 2e 20 42 49 4e 41 52 59 20 77 6f 72 6b 73 20  Y. BINARY works 
306c9 66 6f 72 20 62 6f 74 68 20 55 54 46 2d 38 0a 20  for both UTF-8. 
306ca 20 2a 2a 20 61 6e 64 20 55 54 46 2d 31 36 2c 20   ** and UTF-16, 
306cb 73 6f 20 61 64 64 20 61 20 76 65 72 73 69 6f 6e  so add a version
306cc 20 66 6f 72 20 65 61 63 68 20 74 6f 20 61 76 6f   for each to avo
306cd 69 64 20 61 6e 79 20 75 6e 6e 65 63 65 73 73 61  id any unnecessa
306ce 72 79 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69  ry.  ** conversi
306cf 6f 6e 73 2e 20 54 68 65 20 6f 6e 6c 79 20 65 72  ons. The only er
306d0 72 6f 72 20 74 68 61 74 20 63 61 6e 20 6f 63 63  ror that can occ
306d1 75 72 20 68 65 72 65 20 69 73 20 61 20 6d 61 6c  ur here is a mal
306d2 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 0a 20  loc() failure.. 
306d3 20 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c   */.  createColl
306d4 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52  ation(db, "BINAR
306d5 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  Y", SQLITE_UTF8,
306d6 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c   0, binCollFunc,
306d7 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c   0);.  createCol
306d8 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41  lation(db, "BINA
306d9 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  RY", SQLITE_UTF1
306da 36 42 45 2c 20 30 2c 20 62 69 6e 43 6f 6c 6c 46  6BE, 0, binCollF
306db 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74  unc, 0);.  creat
306dc 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22  eCollation(db, "
306dd 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45 5f  BINARY", SQLITE_
306de 55 54 46 31 36 4c 45 2c 20 30 2c 20 62 69 6e 43  UTF16LE, 0, binC
306df 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63  ollFunc, 0);.  c
306e0 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
306e1 62 2c 20 22 52 54 52 49 4d 22 2c 20 53 51 4c 49  b, "RTRIM", SQLI
306e2 54 45 5f 55 54 46 38 2c 20 28 76 6f 69 64 2a 29  TE_UTF8, (void*)
306e3 31 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20  1, binCollFunc, 
306e4 30 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  0);.  if( db->ma
306e5 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
306e6 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
306e7 74 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 44 66  t;.  }.  db->pDf
306e8 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33  ltColl = sqlite3
306e9 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
306ea 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 42 49  SQLITE_UTF8, "BI
306eb 4e 41 52 59 22 2c 20 36 2c 20 30 29 3b 0a 20 20  NARY", 6, 0);.  
306ec 61 73 73 65 72 74 28 20 64 62 2d 3e 70 44 66 6c  assert( db->pDfl
306ed 74 43 6f 6c 6c 21 3d 30 20 29 3b 0a 0a 20 20 2f  tColl!=0 );..  /
306ee 2a 20 41 6c 73 6f 20 61 64 64 20 61 20 55 54 46  * Also add a UTF
306ef 2d 38 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74  -8 case-insensit
306f0 69 76 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ive collation se
306f1 71 75 65 6e 63 65 2e 20 2a 2f 0a 20 20 63 72 65  quence. */.  cre
306f2 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
306f3 20 22 4e 4f 43 41 53 45 22 2c 20 53 51 4c 49 54   "NOCASE", SQLIT
306f4 45 5f 55 54 46 38 2c 20 30 2c 20 6e 6f 63 61 73  E_UTF8, 0, nocas
306f5 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 2c 20  eCollatingFunc, 
306f6 30 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 66 6c  0);..  /* Set fl
306f7 61 67 73 20 6f 6e 20 74 68 65 20 62 75 69 6c 74  ags on the built
306f8 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  -in collating se
306f9 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 64 62 2d  quences */.  db-
306fa 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e 74 79 70 65  >pDfltColl->type
306fb 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42   = SQLITE_COLL_B
306fc 49 4e 41 52 59 3b 0a 20 20 70 43 6f 6c 6c 20 3d  INARY;.  pColl =
306fd 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
306fe 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55  Seq(db, SQLITE_U
306ff 54 46 38 2c 20 22 4e 4f 43 41 53 45 22 2c 20 36  TF8, "NOCASE", 6
30700 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c  , 0);.  if( pCol
30701 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e  l ){.    pColl->
30702 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 43 4f  type = SQLITE_CO
30703 4c 4c 5f 4e 4f 43 41 53 45 3b 0a 20 20 7d 0a 0a  LL_NOCASE;.  }..
30704 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 62 61    /* Open the ba
30705 63 6b 65 6e 64 20 64 61 74 61 62 61 73 65 20 64  ckend database d
30706 72 69 76 65 72 20 2a 2f 0a 20 20 64 62 2d 3e 6f  river */.  db->o
30707 70 65 6e 46 6c 61 67 73 20 3d 20 66 6c 61 67 73  penFlags = flags
30708 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
30709 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c  BtreeFactory(db,
3070a 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 30 2c 20 53   zFilename, 0, S
3070b 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41  QLITE_DEFAULT_CA
3070c 43 48 45 5f 53 49 5a 45 2c 20 0a 20 20 20 20 20  CHE_SIZE, .     
3070d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3070e 20 20 20 20 20 20 66 6c 61 67 73 20 7c 20 53 51        flags | SQ
3070f 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
30710 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  B,.             
30711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64                &d
30712 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a  b->aDb[0].pBt);.
30713 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
30714 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72  _OK ){.    if( r
30715 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
30716 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 72  NOMEM ){.      r
30717 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
30718 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
30719 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
3071a 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70   0);.    goto op
3071b 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20  endb_out;.  }.  
3071c 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65  db->aDb[0].pSche
3071d 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  ma = sqlite3Sche
3071e 6d 61 47 65 74 28 64 62 2c 20 64 62 2d 3e 61 44  maGet(db, db->aD
3071f 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 64 62 2d  b[0].pBt);.  db-
30720 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20  >aDb[1].pSchema 
30721 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47  = sqlite3SchemaG
30722 65 74 28 64 62 2c 20 30 29 3b 0a 0a 0a 20 20 2f  et(db, 0);...  /
30723 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 61  * The default sa
30724 66 65 74 79 5f 6c 65 76 65 6c 20 66 6f 72 20 74  fety_level for t
30725 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
30726 20 69 73 20 27 66 75 6c 6c 27 3b 20 66 6f 72 20   is 'full'; for 
30727 74 68 65 20 74 65 6d 70 0a 20 20 2a 2a 20 64 61  the temp.  ** da
30728 74 61 62 61 73 65 20 69 74 20 69 73 20 27 4e 4f  tabase it is 'NO
30729 4e 45 27 2e 20 54 68 69 73 20 6d 61 74 63 68 65  NE'. This matche
3072a 73 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65  s the pager laye
3072b 72 20 64 65 66 61 75 6c 74 73 2e 20 20 0a 20 20  r defaults.  .  
3072c 2a 2f 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e  */.  db->aDb[0].
3072d 7a 4e 61 6d 65 20 3d 20 22 6d 61 69 6e 22 3b 0a  zName = "main";.
3072e 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 73 61 66    db->aDb[0].saf
3072f 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a 23  ety_level = 3;.#
30730 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
30731 49 54 5f 54 45 4d 50 44 42 0a 20 20 64 62 2d 3e  IT_TEMPDB.  db->
30732 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 22  aDb[1].zName = "
30733 74 65 6d 70 22 3b 0a 20 20 64 62 2d 3e 61 44 62  temp";.  db->aDb
30734 5b 31 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c  [1].safety_level
30735 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20   = 1;.#endif..  
30736 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
30737 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20  TE_MAGIC_OPEN;. 
30738 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
30739 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
3073a 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20  o opendb_out;.  
3073b 7d 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72  }..  /* Register
3073c 20 61 6c 6c 20 62 75 69 6c 74 2d 69 6e 20 66 75   all built-in fu
3073d 6e 63 74 69 6f 6e 73 2c 20 62 75 74 20 64 6f 20  nctions, but do 
3073e 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72  not attempt to r
3073f 65 61 64 20 74 68 65 0a 20 20 2a 2a 20 64 61 74  ead the.  ** dat
30740 61 62 61 73 65 20 73 63 68 65 6d 61 20 79 65 74  abase schema yet
30741 2e 20 54 68 69 73 20 69 73 20 64 65 6c 61 79 65  . This is delaye
30742 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73  d until the firs
30743 74 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62  t time the datab
30744 61 73 65 0a 20 20 2a 2a 20 69 73 20 61 63 63 65  ase.  ** is acce
30745 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ssed..  */.  sql
30746 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
30747 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 73  LITE_OK, 0);.  s
30748 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75  qlite3RegisterBu
30749 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 64  iltinFunctions(d
3074a 62 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 61  b);..  /* Load a
3074b 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69  utomatic extensi
3074c 6f 6e 73 20 2d 20 65 78 74 65 6e 73 69 6f 6e 73  ons - extensions
3074d 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 20   that have been 
3074e 72 65 67 69 73 74 65 72 65 64 0a 20 20 2a 2a 20  registered.  ** 
3074f 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  using the sqlite
30750 33 5f 61 75 74 6f 6d 61 74 69 63 5f 65 78 74 65  3_automatic_exte
30751 6e 73 69 6f 6e 28 29 20 41 50 49 2e 0a 20 20 2a  nsion() API..  *
30752 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  /.  (void)sqlite
30753 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69  3AutoLoadExtensi
30754 6f 6e 73 28 64 62 29 3b 0a 20 20 69 66 28 20 73  ons(db);.  if( s
30755 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64  qlite3_errcode(d
30756 62 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  b)!=SQLITE_OK ){
30757 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62  .    goto opendb
30758 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 23 69 66 64 65  _out;.  }..#ifde
30759 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
3075a 46 54 53 31 0a 20 20 69 66 28 20 21 64 62 2d 3e  FTS1.  if( !db->
3075b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
3075c 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73      extern int s
3075d 71 6c 69 74 65 33 46 74 73 31 49 6e 69 74 28 73  qlite3Fts1Init(s
3075e 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72 63  qlite3*);.    rc
3075f 20 3d 20 73 71 6c 69 74 65 33 46 74 73 31 49 6e   = sqlite3Fts1In
30760 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64  it(db);.  }.#end
30761 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
30762 45 5f 45 4e 41 42 4c 45 5f 46 54 53 32 0a 20 20  E_ENABLE_FTS2.  
30763 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
30764 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c  ailed && rc==SQL
30765 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65 78  ITE_OK ){.    ex
30766 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
30767 46 74 73 32 49 6e 69 74 28 73 71 6c 69 74 65 33  Fts2Init(sqlite3
30768 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  *);.    rc = sql
30769 69 74 65 33 46 74 73 32 49 6e 69 74 28 64 62 29  ite3Fts2Init(db)
3076a 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
3076b 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
3076c 4c 45 5f 46 54 53 33 0a 20 20 69 66 28 20 21 64  LE_FTS3.  if( !d
3076d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
3076e 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
3076f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
30770 69 74 65 33 46 74 73 33 49 6e 69 74 28 64 62 29  ite3Fts3Init(db)
30771 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
30772 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
30773 4c 45 5f 49 43 55 0a 20 20 69 66 28 20 21 64 62  LE_ICU.  if( !db
30774 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
30775 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
30776 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
30777 74 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b 0a  te3IcuInit(db);.
30778 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    }.#endif..#ifd
30779 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
3077a 5f 52 54 52 45 45 0a 20 20 69 66 28 20 21 64 62  _RTREE.  if( !db
3077b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
3077c 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29  & rc==SQLITE_OK)
3077d 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
3077e 65 33 52 74 72 65 65 49 6e 69 74 28 64 62 29 3b  e3RtreeInit(db);
3077f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73  .  }.#endif..  s
30780 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
30781 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 2d 44  rc, 0);..  /* -D
30782 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
30783 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 31 20 6d 61  OCKING_MODE=1 ma
30784 6b 65 73 20 45 58 43 4c 55 53 49 56 45 20 74 68  kes EXCLUSIVE th
30785 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e  e default lockin
30786 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 2d 44  g.  ** mode.  -D
30787 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
30788 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 30 20 6d 61  OCKING_MODE=0 ma
30789 6b 65 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65  ke NORMAL the de
3078a 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20  fault locking.  
3078b 2a 2a 20 6d 6f 64 65 2e 20 20 44 6f 69 6e 67 20  ** mode.  Doing 
3078c 6e 6f 74 68 69 6e 67 20 61 74 20 61 6c 6c 20 61  nothing at all a
3078d 6c 73 6f 20 6d 61 6b 65 73 20 4e 4f 52 4d 41 4c  lso makes NORMAL
3078e 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20 20   the default..  
3078f 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
30790 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47  _DEFAULT_LOCKING
30791 5f 4d 4f 44 45 0a 20 20 64 62 2d 3e 64 66 6c 74  _MODE.  db->dflt
30792 4c 6f 63 6b 4d 6f 64 65 20 3d 20 53 51 4c 49 54  LockMode = SQLIT
30793 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e  E_DEFAULT_LOCKIN
30794 47 5f 4d 4f 44 45 3b 0a 20 20 73 71 6c 69 74 65  G_MODE;.  sqlite
30795 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64  3PagerLockingMod
30796 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  e(sqlite3BtreePa
30797 67 65 72 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ger(db->aDb[0].p
30798 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Bt),.           
30799 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
3079a 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f  QLITE_DEFAULT_LO
3079b 43 4b 49 4e 47 5f 4d 4f 44 45 29 3b 0a 23 65 6e  CKING_MODE);.#en
3079c 64 69 66 0a 0a 20 20 2f 2a 20 45 6e 61 62 6c 65  dif..  /* Enable
3079d 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 2d 6d   the lookaside-m
3079e 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 20  alloc subsystem 
3079f 2a 2f 0a 20 20 73 65 74 75 70 4c 6f 6f 6b 61 73  */.  setupLookas
307a0 69 64 65 28 64 62 2c 20 30 2c 20 73 71 6c 69 74  ide(db, 0, sqlit
307a1 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
307a2 7a 4c 6f 6f 6b 61 73 69 64 65 2c 0a 20 20 20 20  zLookaside,.    
307a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307a4 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
307a5 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61 73 69  lConfig.nLookasi
307a6 64 65 29 3b 0a 0a 6f 70 65 6e 64 62 5f 6f 75 74  de);..opendb_out
307a7 3a 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20  :.  if( db ){.  
307a8 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 75    assert( db->mu
307a9 74 65 78 21 3d 30 20 7c 7c 20 69 73 54 68 72 65  tex!=0 || isThre
307aa 61 64 73 61 66 65 3d 3d 30 20 7c 7c 20 73 71 6c  adsafe==0 || sql
307ab 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
307ac 2e 62 46 75 6c 6c 4d 75 74 65 78 3d 3d 30 20 29  .bFullMutex==0 )
307ad 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
307ae 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
307af 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  tex);.  }.  rc =
307b0 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
307b1 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  (db);.  if( rc==
307b2 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
307b3 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73      sqlite3_clos
307b4 65 28 64 62 29 3b 0a 20 20 20 20 64 62 20 3d 20  e(db);.    db = 
307b5 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  0;.  }else if( r
307b6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
307b7 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20      db->magic = 
307b8 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43  SQLITE_MAGIC_SIC
307b9 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 44 62 20 3d  K;.  }.  *ppDb =
307ba 20 64 62 3b 0a 20 20 72 65 74 75 72 6e 20 73 71   db;.  return sq
307bb 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c 20  lite3ApiExit(0, 
307bc 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  rc);.}../*.** Op
307bd 65 6e 20 61 20 6e 65 77 20 64 61 74 61 62 61 73  en a new databas
307be 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c  e handle..*/.SQL
307bf 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
307c0 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73  te3_open(.  cons
307c1 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
307c2 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  e, .  sqlite3 **
307c3 70 70 44 62 20 0a 29 7b 0a 20 20 72 65 74 75 72  ppDb .){.  retur
307c4 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a  n openDatabase(z
307c5 46 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c 0a  Filename, ppDb,.
307c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307c7 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
307c8 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51  N_READWRITE | SQ
307c9 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
307ca 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  , 0);.}.SQLITE_A
307cb 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
307cc 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20  pen_v2(.  const 
307cd 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20  char *filename, 
307ce 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69    /* Database fi
307cf 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a  lename (UTF-8) *
307d0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
307d1 44 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  Db,         /* O
307d2 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61  UT: SQLite db ha
307d3 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  ndle */.  int fl
307d4 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
307d5 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20    /* Flags */.  
307d6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73  const char *zVfs
307d7 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
307d8 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f  of VFS module to
307d9 20 75 73 65 20 2a 2f 0a 29 7b 0a 20 20 72 65 74   use */.){.  ret
307da 75 72 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65  urn openDatabase
307db 28 66 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c  (filename, ppDb,
307dc 20 66 6c 61 67 73 2c 20 7a 56 66 73 29 3b 0a 7d   flags, zVfs);.}
307dd 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
307de 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
307df 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61 74  * Open a new dat
307e0 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f  abase handle..*/
307e1 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
307e2 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a  sqlite3_open16(.
307e3 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46    const void *zF
307e4 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c 69  ilename, .  sqli
307e5 74 65 33 20 2a 2a 70 70 44 62 0a 29 7b 0a 20 20  te3 **ppDb.){.  
307e6 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c  char const *zFil
307e7 65 6e 61 6d 65 38 3b 20 20 20 2f 2a 20 7a 46 69  ename8;   /* zFi
307e8 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 65 64 20 69  lename encoded i
307e9 6e 20 55 54 46 2d 38 20 69 6e 73 74 65 61 64 20  n UTF-8 instead 
307ea 6f 66 20 55 54 46 2d 31 36 20 2a 2f 0a 20 20 73  of UTF-16 */.  s
307eb 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
307ec 61 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  al;.  int rc;.. 
307ed 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e 61   assert( zFilena
307ee 6d 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  me );.  assert( 
307ef 70 70 44 62 20 29 3b 0a 20 20 2a 70 70 44 62 20  ppDb );.  *ppDb 
307f0 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 0;.#ifndef SQL
307f1 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
307f2 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  T.  rc = sqlite3
307f3 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
307f4 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
307f5 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70 56   rc;.#endif.  pV
307f6 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
307f7 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c 69 74  eNew(0);.  sqlit
307f8 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56  e3ValueSetStr(pV
307f9 61 6c 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d  al, -1, zFilenam
307fa 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  e, SQLITE_UTF16N
307fb 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54  ATIVE, SQLITE_ST
307fc 41 54 49 43 29 3b 0a 20 20 7a 46 69 6c 65 6e 61  ATIC);.  zFilena
307fd 6d 65 38 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  me8 = sqlite3Val
307fe 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
307ff 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28  ITE_UTF8);.  if(
30800 20 7a 46 69 6c 65 6e 61 6d 65 38 20 29 7b 0a 20   zFilename8 ){. 
30801 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 61 74 61     rc = openData
30802 62 61 73 65 28 7a 46 69 6c 65 6e 61 6d 65 38 2c  base(zFilename8,
30803 20 70 70 44 62 2c 0a 20 20 20 20 20 20 20 20 20   ppDb,.         
30804 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
30805 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
30806 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  TE | SQLITE_OPEN
30807 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a 20 20 20  _CREATE, 0);.   
30808 20 61 73 73 65 72 74 28 20 2a 70 70 44 62 20 7c   assert( *ppDb |
30809 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  | rc==SQLITE_NOM
3080a 45 4d 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  EM );.    if( rc
3080b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21  ==SQLITE_OK && !
3080c 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 2a 70  DbHasProperty(*p
3080d 70 44 62 2c 20 30 2c 20 44 42 5f 53 63 68 65 6d  pDb, 0, DB_Schem
3080e 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20  aLoaded) ){.    
3080f 20 20 45 4e 43 28 2a 70 70 44 62 29 20 3d 20 53    ENC(*ppDb) = S
30810 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
30811 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  E;.    }.  }else
30812 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
30813 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 73  E_NOMEM;.  }.  s
30814 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
30815 70 56 61 6c 29 3b 0a 0a 20 20 72 65 74 75 72 6e  pVal);..  return
30816 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
30817 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69 66  0, rc);.}.#endif
30818 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
30819 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  UTF16 */../*.** 
3081a 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 63  Register a new c
3081b 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
3081c 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  e with the datab
3081d 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a  ase handle db..*
3081e 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
3081f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
30820 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c  collation(.  sql
30821 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e  ite3* db, .  con
30822 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
30823 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76  .  int enc, .  v
30824 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74  oid* pCtx,.  int
30825 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
30826 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
30827 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
30828 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  *).){.  int rc;.
30829 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
3082a 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
3082b 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
3082c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
3082d 0a 20 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f  .  rc = createCo
3082e 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d  llation(db, zNam
3082f 65 2c 20 65 6e 63 2c 20 70 43 74 78 2c 20 78 43  e, enc, pCtx, xC
30830 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 72 63  ompare, 0);.  rc
30831 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
30832 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
30833 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
30834 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
30835 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
30836 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65  ** Register a ne
30837 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  w collation sequ
30838 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61  ence with the da
30839 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
3083a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3083b 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
3083c 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
3083d 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  .  sqlite3* db, 
3083e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3083f 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63  Name, .  int enc
30840 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c  , .  void* pCtx,
30841 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65  .  int(*xCompare
30842 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
30843 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
30844 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64  t void*),.  void
30845 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
30846 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
30847 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
30848 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
30849 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c  assert( !db->mal
3084a 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72  locFailed );.  r
3084b 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  c = createCollat
3084c 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 65  ion(db, zName, e
3084d 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70 61  nc, pCtx, xCompa
3084e 72 65 2c 20 78 44 65 6c 29 3b 0a 20 20 72 63 20  re, xDel);.  rc 
3084f 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
30850 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
30851 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
30852 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
30853 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
30854 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
30855 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69  UTF16./*.** Regi
30856 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61  ster a new colla
30857 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69  tion sequence wi
30858 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
30859 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51  handle db..*/.SQ
3085a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3085b 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
3085c 61 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74  ation16(.  sqlit
3085d 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74  e3* db, .  const
3085e 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 20   void *zName,.  
3085f 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64  int enc, .  void
30860 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78  * pCtx,.  int(*x
30861 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69  Compare)(void*,i
30862 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  nt,const void*,i
30863 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a  nt,const void*).
30864 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
30865 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20  LITE_OK;.  char 
30866 2a 7a 4e 61 6d 65 38 3b 0a 20 20 73 71 6c 69 74  *zName8;.  sqlit
30867 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
30868 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73  b->mutex);.  ass
30869 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  ert( !db->malloc
3086a 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 4e 61 6d  Failed );.  zNam
3086b 65 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66 31  e8 = sqlite3Utf1
3086c 36 74 6f 38 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  6to8(db, zName, 
3086d 2d 31 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65  -1);.  if( zName
3086e 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 72  8 ){.    rc = cr
3086f 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
30870 2c 20 7a 4e 61 6d 65 38 2c 20 65 6e 63 2c 20 70  , zName8, enc, p
30871 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30  Ctx, xCompare, 0
30872 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
30873 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 38 29  Free(db, zName8)
30874 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
30875 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
30876 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
30877 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
30878 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
30879 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  rc;.}.#endif /* 
3087a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
3087b 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  6 */../*.** Regi
3087c 73 74 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  ster a collation
3087d 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72   sequence factor
3087e 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20  y callback with 
3087f 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
30880 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c 61  dle.** db. Repla
30881 63 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 6c  ce any previousl
30882 79 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c 6c  y installed coll
30883 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
30884 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  actory..*/.SQLIT
30885 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
30886 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
30887 65 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ed(.  sqlite3 *d
30888 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c  b, .  void *pCol
30889 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20 20 76  lNeededArg, .  v
3088a 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64  oid(*xCollNeeded
3088b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
3088c 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
3088d 6e 73 74 20 63 68 61 72 2a 29 0a 29 7b 0a 20 20  nst char*).){.  
3088e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
3088f 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
30890 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65    db->xCollNeede
30891 64 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64 3b  d = xCollNeeded;
30892 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64  .  db->xCollNeed
30893 65 64 31 36 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  ed16 = 0;.  db->
30894 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d  pCollNeededArg =
30895 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b   pCollNeededArg;
30896 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
30897 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
30898 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
30899 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
3089a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
3089b 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  F16./*.** Regist
3089c 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  er a collation s
3089d 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79 20  equence factory 
3089e 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 74 68  callback with th
3089f 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
308a0 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c 61 63 65  e.** db. Replace
308a1 20 61 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20   any previously 
308a2 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c 6c 61 74  installed collat
308a3 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63  ion sequence fac
308a4 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tory..*/.SQLITE_
308a5 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
308a6 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
308a7 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  16(.  sqlite3 *d
308a8 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c  b, .  void *pCol
308a9 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20 20 76  lNeededArg, .  v
308aa 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64  oid(*xCollNeeded
308ab 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  16)(void*,sqlite
308ac 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
308ad 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a  const void*).){.
308ae 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
308af 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
308b0 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  ;.  db->xCollNee
308b1 64 65 64 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 78  ded = 0;.  db->x
308b2 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d 20 78  CollNeeded16 = x
308b3 43 6f 6c 6c 4e 65 65 64 65 64 31 36 3b 0a 20 20  CollNeeded16;.  
308b4 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41  db->pCollNeededA
308b5 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65 64 65 64  rg = pCollNeeded
308b6 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Arg;.  sqlite3_m
308b7 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
308b8 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
308b9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
308ba 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
308bb 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 23 69 66  IT_UTF16 */..#if
308bc 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
308bd 5f 47 4c 4f 42 41 4c 52 45 43 4f 56 45 52 0a 23  _GLOBALRECOVER.#
308be 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
308bf 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
308c0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
308c1 6e 20 69 73 20 6e 6f 77 20 61 6e 20 61 6e 61 63  n is now an anac
308c2 68 72 6f 6e 69 73 6d 2e 20 49 74 20 75 73 65 64  hronism. It used
308c3 20 74 6f 20 62 65 20 75 73 65 64 20 74 6f 20 72   to be used to r
308c4 65 63 6f 76 65 72 20 66 72 6f 6d 20 61 0a 2a 2a  ecover from a.**
308c5 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
308c6 65 2c 20 62 75 74 20 53 51 4c 69 74 65 20 6e 6f  e, but SQLite no
308c7 77 20 64 6f 65 73 20 74 68 69 73 20 61 75 74 6f  w does this auto
308c8 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2f 0a 53 51  matically..*/.SQ
308c9 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
308ca 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f  ite3_global_reco
308cb 76 65 72 28 76 6f 69 64 29 7b 0a 20 20 72 65 74  ver(void){.  ret
308cc 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
308cd 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  .#endif.#endif..
308ce 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65  /*.** Test to se
308cf 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
308d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
308d1 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 61  nnection is in a
308d2 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f 64  utocommit.** mod
308d3 65 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45 20  e.  Return TRUE 
308d4 69 66 20 69 74 20 69 73 20 61 6e 64 20 46 41 4c  if it is and FAL
308d5 53 45 20 69 66 20 6e 6f 74 2e 20 20 41 75 74 6f  SE if not.  Auto
308d6 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 6f  commit mode is o
308d7 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75 6c 74 2e  n.** by default.
308d8 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 69 73 20    Autocommit is 
308d9 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 42 45  disabled by a BE
308da 47 49 4e 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  GIN statement an
308db 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a 2a 20 62  d reenabled.** b
308dc 79 20 74 68 65 20 6e 65 78 74 20 43 4f 4d 4d 49  y the next COMMI
308dd 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a  T or ROLLBACK..*
308de 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49 53 20 49  *.******* THIS I
308df 53 20 41 4e 20 45 58 50 45 52 49 4d 45 4e 54 41  S AN EXPERIMENTA
308e0 4c 20 41 50 49 20 41 4e 44 20 49 53 20 53 55 42  L API AND IS SUB
308e1 4a 45 43 54 20 54 4f 20 43 48 41 4e 47 45 20 2a  JECT TO CHANGE *
308e2 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  *****.*/.SQLITE_
308e3 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
308e4 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73  get_autocommit(s
308e5 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72  qlite3 *db){.  r
308e6 65 74 75 72 6e 20 64 62 2d 3e 61 75 74 6f 43 6f  eturn db->autoCo
308e7 6d 6d 69 74 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  mmit;.}..#ifdef 
308e8 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
308e9 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
308ea 20 72 6f 75 74 69 6e 65 20 69 73 20 73 75 62 74   routine is subt
308eb 69 74 75 74 65 64 20 66 6f 72 20 63 6f 6e 73 74  ituted for const
308ec 61 6e 74 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  ant SQLITE_CORRU
308ed 50 54 20 69 6e 0a 2a 2a 20 64 65 62 75 67 67 69  PT in.** debuggi
308ee 6e 67 20 62 75 69 6c 64 73 2e 20 20 54 68 69 73  ng builds.  This
308ef 20 70 72 6f 76 69 64 65 73 20 61 20 77 61 79 20   provides a way 
308f0 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f  to set a breakpo
308f1 69 6e 74 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20  int for when.** 
308f2 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66 69  corruption is fi
308f3 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f  rst detected..*/
308f4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
308f5 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 72 72 75  int sqlite3Corru
308f6 70 74 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75  pt(void){.  retu
308f7 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
308f8 54 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  T;.}.#endif..#if
308f9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
308fa 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a  _DEPRECATED./*.*
308fb 2a 20 54 68 69 73 20 69 73 20 61 20 63 6f 6e 76  * This is a conv
308fc 65 6e 69 65 6e 63 65 20 72 6f 75 74 69 6e 65 20  enience routine 
308fd 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 20  that makes sure 
308fe 74 68 61 74 20 61 6c 6c 20 74 68 72 65 61 64 2d  that all thread-
308ff 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61  specific.** data
30900 20 66 6f 72 20 74 68 69 73 20 74 68 72 65 61 64   for this thread
30901 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f   has been deallo
30902 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  cated..**.** SQL
30903 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73  ite no longer us
30904 65 73 20 74 68 72 65 61 64 2d 73 70 65 63 69 66  es thread-specif
30905 69 63 20 64 61 74 61 20 73 6f 20 74 68 69 73 20  ic data so this 
30906 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 77 20 61  routine is now a
30907 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 20 49 74 20 69  .** no-op.  It i
30908 73 20 72 65 74 61 69 6e 65 64 20 66 6f 72 20 68  s retained for h
30909 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d 70 61 74  istorical compat
3090a 69 62 69 6c 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49  ibility..*/.SQLI
3090b 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
3090c 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e  te3_thread_clean
3090d 75 70 28 76 6f 69 64 29 7b 0a 7d 0a 23 65 6e 64  up(void){.}.#end
3090e 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
3090f 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f   meta informatio
30910 6e 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66  n about a specif
30911 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 64  ic column of a d
30912 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a  atabase table..*
30913 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 69 6e  * See comment in
30914 20 73 71 6c 69 74 65 33 2e 68 20 28 73 71 6c 69   sqlite3.h (sqli
30915 74 65 2e 68 2e 69 6e 29 20 66 6f 72 20 64 65 74  te.h.in) for det
30916 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ails..*/.#ifdef 
30917 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
30918 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 53 51  LUMN_METADATA.SQ
30919 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3091a 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d  ite3_table_colum
3091b 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71  n_metadata(.  sq
3091c 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
3091d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
3091e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a  nection handle *
3091f 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
30920 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20  zDbName,        
30921 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65  /* Database name
30922 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f   or NULL */.  co
30923 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
30924 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62  Name,     /* Tab
30925 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e  le name */.  con
30926 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e  st char *zColumn
30927 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75  Name,    /* Colu
30928 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61  mn name */.  cha
30929 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61  r const **pzData
3092a 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50  Type,    /* OUTP
3092b 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74  UT: Declared dat
3092c 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72  a type */.  char
3092d 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53   const **pzCollS
3092e 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55  eq,     /* OUTPU
3092f 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  T: Collation seq
30930 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  uence name */.  
30931 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20  int *pNotNull,  
30932 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
30933 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e  UTPUT: True if N
30934 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
30935 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69  nt exists */.  i
30936 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c  nt *pPrimaryKey,
30937 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
30938 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f  TPUT: True if co
30939 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20  lumn part of PK 
3093a 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69  */.  int *pAutoi
3093b 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nc              
3093c 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
3093d 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75   if column is au
3093e 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a  to-increment */.
3093f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63  ){.  int rc;.  c
30940 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30  har *zErrMsg = 0
30941 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  ;.  Table *pTab 
30942 3d 20 30 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70  = 0;.  Column *p
30943 43 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  Col = 0;.  int i
30944 43 6f 6c 3b 0a 0a 20 20 63 68 61 72 20 63 6f 6e  Col;..  char con
30945 73 74 20 2a 7a 44 61 74 61 54 79 70 65 20 3d 20  st *zDataType = 
30946 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  0;.  char const 
30947 2a 7a 43 6f 6c 6c 53 65 71 20 3d 20 30 3b 0a 20  *zCollSeq = 0;. 
30948 20 69 6e 74 20 6e 6f 74 6e 75 6c 6c 20 3d 20 30   int notnull = 0
30949 3b 0a 20 20 69 6e 74 20 70 72 69 6d 61 72 79 6b  ;.  int primaryk
3094a 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 61 75  ey = 0;.  int au
3094b 74 6f 69 6e 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a  toinc = 0;..  /*
3094c 20 45 6e 73 75 72 65 20 74 68 65 20 64 61 74 61   Ensure the data
3094d 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20  base schema has 
3094e 62 65 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 20  been loaded */. 
3094f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
30950 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
30951 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
30952 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
30953 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
30954 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63 20 3d  rAll(db);.  rc =
30955 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c   sqlite3Init(db,
30956 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69 66   &zErrMsg);.  if
30957 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20  ( SQLITE_OK!=rc 
30958 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  ){.    goto erro
30959 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  r_out;.  }..  /*
3095a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c   Locate the tabl
3095b 65 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 2a 2f  e in question */
3095c 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
3095d 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
3095e 54 61 62 6c 65 4e 61 6d 65 2c 20 7a 44 62 4e 61  TableName, zDbNa
3095f 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62  me);.  if( !pTab
30960 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   || pTab->pSelec
30961 74 20 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20  t ){.    pTab = 
30962 30 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  0;.    goto erro
30963 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  r_out;.  }..  /*
30964 20 46 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e   Find the column
30965 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f 20   for which info 
30966 69 73 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a  is requested */.
30967 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52    if( sqlite3IsR
30968 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e 4e 61 6d 65  owid(zColumnName
30969 29 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20  ) ){.    iCol = 
3096a 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20  pTab->iPKey;.   
3096b 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a   if( iCol>=0 ){.
3096c 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54        pCol = &pT
3096d 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a  ab->aCol[iCol];.
3096e 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
3096f 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
30970 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  Col<pTab->nCol; 
30971 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 70  iCol++){.      p
30972 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f  Col = &pTab->aCo
30973 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 69  l[iCol];.      i
30974 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  f( 0==sqlite3Str
30975 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  ICmp(pCol->zName
30976 2c 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29  , zColumnName) )
30977 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
30978 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
30979 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61     if( iCol==pTa
3097a 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
3097b 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20   pTab = 0;.     
3097c 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b   goto error_out;
3097d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
3097e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   The following b
3097f 6c 6f 63 6b 20 73 74 6f 72 65 73 20 74 68 65 20  lock stores the 
30980 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta information
30981 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65   that will be re
30982 74 75 72 6e 65 64 0a 20 20 2a 2a 20 74 6f 20 74  turned.  ** to t
30983 68 65 20 63 61 6c 6c 65 72 20 69 6e 20 6c 6f 63  he caller in loc
30984 61 6c 20 76 61 72 69 61 62 6c 65 73 20 7a 44 61  al variables zDa
30985 74 61 54 79 70 65 2c 20 7a 43 6f 6c 6c 53 65 71  taType, zCollSeq
30986 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70 72 69 6d 61  , notnull, prima
30987 72 79 6b 65 79 0a 20 20 2a 2a 20 61 6e 64 20 61  rykey.  ** and a
30988 75 74 6f 69 6e 63 2e 20 41 74 20 74 68 69 73 20  utoinc. At this 
30989 70 6f 69 6e 74 20 74 68 65 72 65 20 61 72 65 20  point there are 
3098a 74 77 6f 20 70 6f 73 73 69 62 69 6c 69 74 69 65  two possibilitie
3098b 73 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20  s:.  ** .  **   
3098c 20 20 31 2e 20 54 68 65 20 73 70 65 63 69 66 69    1. The specifi
3098d 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77  ed column name w
3098e 61 73 20 72 6f 77 69 64 22 2c 20 22 6f 69 64 22  as rowid", "oid"
3098f 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 20 0a 20   or "_rowid_" . 
30990 20 2a 2a 20 20 20 20 20 20 20 20 61 6e 64 20 74   **        and t
30991 68 65 72 65 20 69 73 20 6e 6f 20 65 78 70 6c 69  here is no expli
30992 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 49  citly declared I
30993 50 4b 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2a  PK column. .  **
30994 0a 20 20 2a 2a 20 20 20 20 20 32 2e 20 54 68 65  .  **     2. The
30995 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 61 20   table is not a 
30996 76 69 65 77 20 61 6e 64 20 74 68 65 20 63 6f 6c  view and the col
30997 75 6d 6e 20 6e 61 6d 65 20 69 64 65 6e 74 69 66  umn name identif
30998 69 65 64 20 61 6e 20 0a 20 20 2a 2a 20 20 20 20  ied an .  **    
30999 20 20 20 20 65 78 70 6c 69 63 69 74 6c 79 20 64      explicitly d
3099a 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20  eclared column. 
3099b 43 6f 70 79 20 6d 65 74 61 20 69 6e 66 6f 72 6d  Copy meta inform
3099c 61 74 69 6f 6e 20 66 72 6f 6d 20 2a 70 43 6f 6c  ation from *pCol
3099d 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 70 43  ..  */ .  if( pC
3099e 6f 6c 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 54  ol ){.    zDataT
3099f 79 70 65 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70  ype = pCol->zTyp
309a0 65 3b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20  e;.    zCollSeq 
309a1 3d 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 3b 0a 20  = pCol->zColl;. 
309a2 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d 20 70 43 6f     notnull = pCo
309a3 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21 3d 30 3b 0a 20  l->notNull!=0;. 
309a4 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20 20 3d     primarykey  =
309a5 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79   pCol->isPrimKey
309a6 21 3d 30 3b 0a 20 20 20 20 61 75 74 6f 69 6e 63  !=0;.    autoinc
309a7 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3d 3d   = pTab->iPKey==
309a8 69 43 6f 6c 20 26 26 20 28 70 54 61 62 2d 3e 74  iCol && (pTab->t
309a9 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
309aa 6f 69 6e 63 72 65 6d 65 6e 74 29 21 3d 30 3b 0a  oincrement)!=0;.
309ab 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61    }else{.    zDa
309ac 74 61 54 79 70 65 20 3d 20 22 49 4e 54 45 47 45  taType = "INTEGE
309ad 52 22 3b 0a 20 20 20 20 70 72 69 6d 61 72 79 6b  R";.    primaryk
309ae 65 79 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66  ey = 1;.  }.  if
309af 28 20 21 7a 43 6f 6c 6c 53 65 71 20 29 7b 0a 20  ( !zCollSeq ){. 
309b0 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d 20 22 42     zCollSeq = "B
309b1 49 4e 41 52 59 22 3b 0a 20 20 7d 0a 0a 65 72 72  INARY";.  }..err
309b2 6f 72 5f 6f 75 74 3a 0a 20 20 73 71 6c 69 74 65  or_out:.  sqlite
309b3 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
309b4 62 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69  b);.  (void)sqli
309b5 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
309b6 3b 0a 0a 20 20 2f 2a 20 57 68 65 74 68 65 72 20  ;..  /* Whether 
309b7 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  the function cal
309b8 6c 20 73 75 63 63 65 65 64 65 64 20 6f 72 20 66  l succeeded or f
309b9 61 69 6c 65 64 2c 20 73 65 74 20 74 68 65 20 6f  ailed, set the o
309ba 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73  utput parameters
309bb 0a 20 20 2a 2a 20 74 6f 20 77 68 61 74 65 76 65  .  ** to whateve
309bc 72 20 74 68 65 69 72 20 6c 6f 63 61 6c 20 63 6f  r their local co
309bd 75 6e 74 65 72 70 61 72 74 73 20 63 6f 6e 74 61  unterparts conta
309be 69 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  in. If an error 
309bf 64 69 64 20 6f 63 63 75 72 2c 0a 20 20 2a 2a 20  did occur,.  ** 
309c0 74 68 69 73 20 68 61 73 20 74 68 65 20 65 66 66  this has the eff
309c1 65 63 74 20 6f 66 20 7a 65 72 6f 69 6e 67 20 61  ect of zeroing a
309c2 6c 6c 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65  ll output parame
309c3 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ters..  */.  if(
309c4 20 70 7a 44 61 74 61 54 79 70 65 20 29 20 2a 70   pzDataType ) *p
309c5 7a 44 61 74 61 54 79 70 65 20 3d 20 7a 44 61 74  zDataType = zDat
309c6 61 54 79 70 65 3b 0a 20 20 69 66 28 20 70 7a 43  aType;.  if( pzC
309c7 6f 6c 6c 53 65 71 20 29 20 2a 70 7a 43 6f 6c 6c  ollSeq ) *pzColl
309c8 53 65 71 20 3d 20 7a 43 6f 6c 6c 53 65 71 3b 0a  Seq = zCollSeq;.
309c9 20 20 69 66 28 20 70 4e 6f 74 4e 75 6c 6c 20 29    if( pNotNull )
309ca 20 2a 70 4e 6f 74 4e 75 6c 6c 20 3d 20 6e 6f 74   *pNotNull = not
309cb 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 70 50 72 69  null;.  if( pPri
309cc 6d 61 72 79 4b 65 79 20 29 20 2a 70 50 72 69 6d  maryKey ) *pPrim
309cd 61 72 79 4b 65 79 20 3d 20 70 72 69 6d 61 72 79  aryKey = primary
309ce 6b 65 79 3b 0a 20 20 69 66 28 20 70 41 75 74 6f  key;.  if( pAuto
309cf 69 6e 63 20 29 20 2a 70 41 75 74 6f 69 6e 63 20  inc ) *pAutoinc 
309d0 3d 20 61 75 74 6f 69 6e 63 3b 0a 0a 20 20 69 66  = autoinc;..  if
309d1 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
309d2 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20  && !pTab ){.    
309d3 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
309d4 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  , zErrMsg);.    
309d5 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
309d6 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f  3MPrintf(db, "no
309d7 20 73 75 63 68 20 74 61 62 6c 65 20 63 6f 6c 75   such table colu
309d8 6d 6e 3a 20 25 73 2e 25 73 22 2c 20 7a 54 61 62  mn: %s.%s", zTab
309d9 6c 65 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  leName,.        
309da 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b 0a 20 20  zColumnName);.  
309db 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
309dc 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ROR;.  }.  sqlit
309dd 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
309de 28 7a 45 72 72 4d 73 67 3f 22 25 73 22 3a 30 29  (zErrMsg?"%s":0)
309df 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71  , zErrMsg);.  sq
309e0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
309e1 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 63 20 3d  zErrMsg);.  rc =
309e2 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
309e3 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
309e4 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
309e5 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
309e6 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
309e7 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f  ../*.** Sleep fo
309e8 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65  r a little while
309e9 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  .  Return the am
309ea 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65  ount of time sle
309eb 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  pt..*/.SQLITE_AP
309ec 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c  I int sqlite3_sl
309ed 65 65 70 28 69 6e 74 20 6d 73 29 7b 0a 20 20 73  eep(int ms){.  s
309ee 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
309ef 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 56  ;.  int rc;.  pV
309f0 66 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73  fs = sqlite3_vfs
309f1 5f 66 69 6e 64 28 30 29 3b 0a 20 20 69 66 28 20  _find(0);.  if( 
309f2 70 56 66 73 3d 3d 30 20 29 20 72 65 74 75 72 6e  pVfs==0 ) return
309f3 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66   0;..  /* This f
309f4 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 69 6e  unction works in
309f5 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2c 20 62   milliseconds, b
309f6 75 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  ut the underlyin
309f7 67 20 4f 73 53 6c 65 65 70 28 29 20 0a 20 20 2a  g OsSleep() .  *
309f8 2a 20 41 50 49 20 75 73 65 73 20 6d 69 63 72 6f  * API uses micro
309f9 73 65 63 6f 6e 64 73 2e 20 48 65 6e 63 65 20 74  seconds. Hence t
309fa 68 65 20 31 30 30 30 27 73 2e 0a 20 20 2a 2f 0a  he 1000's..  */.
309fb 20 20 72 63 20 3d 20 28 73 71 6c 69 74 65 33 4f    rc = (sqlite3O
309fc 73 53 6c 65 65 70 28 70 56 66 73 2c 20 31 30 30  sSleep(pVfs, 100
309fd 30 2a 6d 73 29 2f 31 30 30 30 29 3b 0a 20 20 72  0*ms)/1000);.  r
309fe 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
309ff 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73  ** Enable or dis
30a00 61 62 6c 65 20 74 68 65 20 65 78 74 65 6e 64 65  able the extende
30a01 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 2e 0a  d result codes..
30a02 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
30a03 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64  t sqlite3_extend
30a04 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28  ed_result_codes(
30a05 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
30a06 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69 74   onoff){.  sqlit
30a07 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
30a08 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d  b->mutex);.  db-
30a09 3e 65 72 72 4d 61 73 6b 20 3d 20 6f 6e 6f 66 66  >errMask = onoff
30a0a 20 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20   ? 0xffffffff : 
30a0b 30 78 66 66 3b 0a 20 20 73 71 6c 69 74 65 33 5f  0xff;.  sqlite3_
30a0c 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
30a0d 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
30a0e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
30a0f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20  *.** Invoke the 
30a10 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74  xFileControl met
30a11 68 6f 64 20 6f 6e 20 61 20 70 61 72 74 69 63 75  hod on a particu
30a12 6c 61 72 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  lar database..*/
30a13 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
30a14 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
30a15 74 72 6f 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  trol(sqlite3 *db
30a16 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
30a17 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76  bName, int op, v
30a18 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 69 6e  oid *pArg){.  in
30a19 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52  t rc = SQLITE_ER
30a1a 52 4f 52 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  ROR;.  int iDb;.
30a1b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
30a1c 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
30a1d 3b 0a 20 20 69 66 28 20 7a 44 62 4e 61 6d 65 3d  ;.  if( zDbName=
30a1e 3d 30 20 29 7b 0a 20 20 20 20 69 44 62 20 3d 20  =0 ){.    iDb = 
30a1f 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
30a20 66 6f 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64  for(iDb=0; iDb<d
30a21 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a  b->nDb; iDb++){.
30a22 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70        if( strcmp
30a23 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e  (db->aDb[iDb].zN
30a24 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29 3d 3d 30  ame, zDbName)==0
30a25 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
30a26 20 20 7d 0a 20 20 69 66 28 20 69 44 62 3c 64 62    }.  if( iDb<db
30a27 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 42 74 72  ->nDb ){.    Btr
30a28 65 65 20 2a 70 42 74 72 65 65 20 3d 20 64 62 2d  ee *pBtree = db-
30a29 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20  >aDb[iDb].pBt;. 
30a2a 20 20 20 69 66 28 20 70 42 74 72 65 65 20 29 7b     if( pBtree ){
30a2b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50  .      Pager *pP
30a2c 61 67 65 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  ager;.      sqli
30a2d 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 0a 20 20  te3_file *fd;.  
30a2e 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
30a2f 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20  Enter(pBtree);. 
30a30 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71       pPager = sq
30a31 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
30a32 70 42 74 72 65 65 29 3b 0a 20 20 20 20 20 20 61  pBtree);.      a
30a33 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30  ssert( pPager!=0
30a34 20 29 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 73   );.      fd = s
30a35 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28  qlite3PagerFile(
30a36 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 61  pPager);.      a
30a37 73 73 65 72 74 28 20 66 64 21 3d 30 20 29 3b 0a  ssert( fd!=0 );.
30a38 20 20 20 20 20 20 69 66 28 20 66 64 2d 3e 70 4d        if( fd->pM
30a39 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20  ethods ){.      
30a3a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
30a3b 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 66 64 2c 20  FileControl(fd, 
30a3c 6f 70 2c 20 70 41 72 67 29 3b 0a 20 20 20 20 20  op, pArg);.     
30a3d 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
30a3e 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65  BtreeLeave(pBtre
30a3f 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
30a40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
30a41 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
30a42 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20 20 0a    return rc;   .
30a43 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  }../*.** Interfa
30a44 63 65 20 74 6f 20 74 68 65 20 74 65 73 74 69 6e  ce to the testin
30a45 67 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 53 51 4c 49  g logic..*/.SQLI
30a46 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
30a47 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
30a48 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20  int op, ...){.  
30a49 69 6e 74 20 72 63 20 3d 20 30 3b 0a 23 69 66 6e  int rc = 0;.#ifn
30a4a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
30a4b 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 20 20 76  BUILTIN_TEST.  v
30a4c 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 76 61 5f  a_list ap;.  va_
30a4d 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20  start(ap, op);. 
30a4e 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 0a   switch( op ){..
30a4f 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 61      /*.    ** Sa
30a50 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ve the current s
30a51 74 61 74 65 20 6f 66 20 74 68 65 20 50 52 4e 47  tate of the PRNG
30a52 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
30a53 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
30a54 4c 5f 50 52 4e 47 5f 53 41 56 45 3a 20 7b 0a 20  L_PRNG_SAVE: {. 
30a55 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67       sqlite3Prng
30a56 53 61 76 65 53 74 61 74 65 28 29 3b 0a 20 20 20  SaveState();.   
30a57 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
30a58 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52  .    /*.    ** R
30a59 65 73 74 6f 72 65 20 74 68 65 20 73 74 61 74 65  estore the state
30a5a 20 6f 66 20 74 68 65 20 50 52 4e 47 20 74 6f 20   of the PRNG to 
30a5b 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 20 73  the last state s
30a5c 61 76 65 64 20 75 73 69 6e 67 0a 20 20 20 20 2a  aved using.    *
30a5d 2a 20 50 52 4e 47 5f 53 41 56 45 2e 20 20 49 66  * PRNG_SAVE.  If
30a5e 20 50 52 4e 47 5f 53 41 56 45 20 68 61 73 20 6e   PRNG_SAVE has n
30a5f 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e  ever before been
30a60 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 0a 20 20   called, then.  
30a61 20 20 2a 2a 20 74 68 69 73 20 76 65 72 62 20 61    ** this verb a
30a62 63 74 73 20 6c 69 6b 65 20 50 52 4e 47 5f 52 45  cts like PRNG_RE
30a63 53 45 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  SET..    */.    
30a64 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
30a65 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52  CTRL_PRNG_RESTOR
30a66 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  E: {.      sqlit
30a67 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61  e3PrngRestoreSta
30a68 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61  te();.      brea
30a69 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
30a6a 0a 20 20 20 20 2a 2a 20 52 65 73 65 74 20 74 68  .    ** Reset th
30a6b 65 20 50 52 4e 47 20 62 61 63 6b 20 74 6f 20 69  e PRNG back to i
30a6c 74 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  ts uninitialized
30a6d 20 73 74 61 74 65 2e 20 20 54 68 65 20 6e 65 78   state.  The nex
30a6e 74 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f  t call.    ** to
30a6f 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
30a70 65 73 73 28 29 20 77 69 6c 6c 20 72 65 73 65 65  ess() will resee
30a71 64 20 74 68 65 20 50 52 4e 47 20 75 73 69 6e 67  d the PRNG using
30a72 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 0a 20   a single call. 
30a73 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 78 52 61     ** to the xRa
30a74 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20  ndomness method 
30a75 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 56  of the default V
30a76 46 53 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  FS..    */.    c
30a77 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
30a78 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 3a 20  TRL_PRNG_RESET: 
30a79 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  {.      sqlite3P
30a7a 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 29 3b  rngResetState();
30a7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
30a7c 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
30a7d 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74  **  sqlite3_test
30a7e 5f 63 6f 6e 74 72 6f 6c 28 42 49 54 56 45 43 5f  _control(BITVEC_
30a7f 54 45 53 54 2c 20 73 69 7a 65 2c 20 70 72 6f 67  TEST, size, prog
30a80 72 61 6d 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ram).    **.    
30a81 2a 2a 20 52 75 6e 20 61 20 74 65 73 74 20 61 67  ** Run a test ag
30a82 61 69 6e 73 74 20 61 20 42 69 74 76 65 63 20 6f  ainst a Bitvec o
30a83 62 6a 65 63 74 20 6f 66 20 73 69 7a 65 2e 20 20  bject of size.  
30a84 54 68 65 20 70 72 6f 67 72 61 6d 20 61 72 67 75  The program argu
30a85 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20 61  ment.    ** is a
30a86 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74 65 67  n array of integ
30a87 65 72 73 20 74 68 61 74 20 64 65 66 69 6e 65 73  ers that defines
30a88 20 74 68 65 20 74 65 73 74 2e 20 20 52 65 74 75   the test.  Retu
30a89 72 6e 20 2d 31 20 6f 6e 20 61 0a 20 20 20 20 2a  rn -1 on a.    *
30a8a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  * memory allocat
30a8b 69 6f 6e 20 65 72 72 6f 72 2c 20 30 20 6f 6e 20  ion error, 0 on 
30a8c 73 75 63 63 65 73 73 2c 20 6f 72 20 6e 6f 6e 2d  success, or non-
30a8d 7a 65 72 6f 20 66 6f 72 20 61 6e 20 65 72 72 6f  zero for an erro
30a8e 72 2e 0a 20 20 20 20 2a 2a 20 53 65 65 20 74 68  r..    ** See th
30a8f 65 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42  e sqlite3BitvecB
30a90 75 69 6c 74 69 6e 54 65 73 74 28 29 20 66 6f 72  uiltinTest() for
30a91 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
30a92 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  rmation..    */.
30a93 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
30a94 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f  TESTCTRL_BITVEC_
30a95 54 45 53 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e  TEST: {.      in
30a96 74 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70  t sz = va_arg(ap
30a97 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e  , int);.      in
30a98 74 20 2a 61 50 72 6f 67 20 3d 20 76 61 5f 61 72  t *aProg = va_ar
30a99 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20  g(ap, int*);.   
30a9a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
30a9b 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74  itvecBuiltinTest
30a9c 28 73 7a 2c 20 61 50 72 6f 67 29 3b 0a 20 20 20  (sz, aProg);.   
30a9d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
30a9e 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20  .    /*.    **  
30a9f 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
30aa0 74 72 6f 6c 28 42 45 4e 49 47 4e 5f 4d 41 4c 4c  trol(BENIGN_MALL
30aa1 4f 43 5f 48 4f 4f 4b 53 2c 20 78 42 65 67 69 6e  OC_HOOKS, xBegin
30aa2 2c 20 78 45 6e 64 29 0a 20 20 20 20 2a 2a 0a 20  , xEnd).    **. 
30aa3 20 20 20 2a 2a 20 52 65 67 69 73 74 65 72 20 68     ** Register h
30aa4 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 74 6f 20  ooks to call to 
30aa5 69 6e 64 69 63 61 74 65 20 77 68 69 63 68 20 6d  indicate which m
30aa6 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 73  alloc() failures
30aa7 20 0a 20 20 20 20 2a 2a 20 61 72 65 20 62 65 6e   .    ** are ben
30aa8 69 67 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ign..    */.    
30aa9 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
30aaa 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c  CTRL_BENIGN_MALL
30aab 4f 43 5f 48 4f 4f 4b 53 3a 20 7b 0a 20 20 20 20  OC_HOOKS: {.    
30aac 20 20 74 79 70 65 64 65 66 20 76 6f 69 64 20 28    typedef void (
30aad 2a 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 28  *void_function)(
30aae 76 6f 69 64 29 3b 0a 20 20 20 20 20 20 76 6f 69  void);.      voi
30aaf 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e 69  d_function xBeni
30ab0 67 6e 42 65 67 69 6e 3b 0a 20 20 20 20 20 20 76  gnBegin;.      v
30ab1 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65  oid_function xBe
30ab2 6e 69 67 6e 45 6e 64 3b 0a 20 20 20 20 20 20 78  nignEnd;.      x
30ab3 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 76 61  BenignBegin = va
30ab4 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75  _arg(ap, void_fu
30ab5 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20 78  nction);.      x
30ab6 42 65 6e 69 67 6e 45 6e 64 20 3d 20 76 61 5f 61  BenignEnd = va_a
30ab7 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75 6e 63  rg(ap, void_func
30ab8 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c  tion);.      sql
30ab9 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  ite3BenignMalloc
30aba 48 6f 6f 6b 73 28 78 42 65 6e 69 67 6e 42 65 67  Hooks(xBenignBeg
30abb 69 6e 2c 20 78 42 65 6e 69 67 6e 45 6e 64 29 3b  in, xBenignEnd);
30abc 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
30abd 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
30abe 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74  **  sqlite3_test
30abf 5f 63 6f 6e 74 72 6f 6c 28 50 45 4e 44 49 4e 47  _control(PENDING
30ac0 5f 42 59 54 45 2c 20 75 6e 73 69 67 6e 65 64 20  _BYTE, unsigned 
30ac1 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a 20 20  int X).    **.  
30ac2 20 20 2a 2a 20 53 65 74 20 74 68 65 20 50 45 4e    ** Set the PEN
30ac3 44 49 4e 47 20 62 79 74 65 20 74 6f 20 74 68 65  DING byte to the
30ac4 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 61 72   value in the ar
30ac5 67 75 6d 65 6e 74 2c 20 69 66 20 58 3e 30 2e 0a  gument, if X>0..
30ac6 20 20 20 20 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63      ** Make no c
30ac7 68 61 6e 67 65 73 20 69 66 20 58 3d 3d 30 2e 20  hanges if X==0. 
30ac8 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75   Return the valu
30ac9 65 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67  e of the pending
30aca 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 61 73 20   byte.    ** as 
30acb 69 74 20 65 78 69 73 74 69 6e 67 20 62 65 66 6f  it existing befo
30acc 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  re this routine 
30acd 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20  was called..    
30ace 2a 2a 0a 20 20 20 20 2a 2a 20 49 4d 50 4f 52 54  **.    ** IMPORT
30acf 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74  ANT:  Changing t
30ad0 68 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65 20  he PENDING byte 
30ad1 66 72 6f 6d 20 30 78 34 30 30 30 30 30 30 30 20  from 0x40000000 
30ad2 72 65 73 75 6c 74 73 20 69 6e 0a 20 20 20 20 2a  results in.    *
30ad3 2a 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  * an incompatibl
30ad4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
30ad5 66 6f 72 6d 61 74 2e 20 20 43 68 61 6e 67 69 6e  format.  Changin
30ad6 67 20 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79  g the PENDING by
30ad7 74 65 0a 20 20 20 20 2a 2a 20 77 68 69 6c 65 20  te.    ** while 
30ad8 61 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e  any database con
30ad9 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20  nection is open 
30ada 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66  results in undef
30adb 69 6e 65 64 20 61 6e 64 0a 20 20 20 20 2a 2a 20  ined and.    ** 
30adc 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61  dileterious beha
30add 76 69 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  vior..    */.   
30ade 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
30adf 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59  TCTRL_PENDING_BY
30ae0 54 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 73 69  TE: {.      unsi
30ae1 67 6e 65 64 20 69 6e 74 20 6e 65 77 56 61 6c 20  gned int newVal 
30ae2 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 75 6e 73  = va_arg(ap, uns
30ae3 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20  igned int);.    
30ae4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 65    rc = sqlite3Pe
30ae5 6e 64 69 6e 67 42 79 74 65 3b 0a 20 20 20 20 20  ndingByte;.     
30ae6 20 69 66 28 20 6e 65 77 56 61 6c 20 29 20 73 71   if( newVal ) sq
30ae7 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65  lite3PendingByte
30ae8 20 3d 20 6e 65 77 56 61 6c 3b 0a 20 20 20 20 20   = newVal;.     
30ae9 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
30aea 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  }.  va_end(ap);.
30aeb 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
30aec 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
30aed 53 54 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  ST */.  return r
30aee 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  c;.}../*********
30aef 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 69  ***** End of mai
30af0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
30af1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30af2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30af3 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
30af4 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
30af5 20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a   notify.c ******
30af6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30af7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30af8 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39  ****/./*.** 2009
30af9 20 4d 61 72 63 68 20 33 0a 2a 2a 0a 2a 2a 20 54   March 3.**.** T
30afa 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
30afb 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
30afc 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
30afd 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
30afe 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
30aff 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
30b00 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
30b01 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
30b02 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
30b03 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
30b04 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
30b05 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
30b06 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
30b07 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
30b08 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
30b09 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
30b0a 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
30b0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30b0f 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
30b10 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
30b11 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
30b12 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
30b13 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
30b14 28 29 0a 2a 2a 20 41 50 49 20 6d 65 74 68 6f 64  ().** API method
30b15 20 61 6e 64 20 69 74 73 20 61 73 73 6f 63 69 61   and its associa
30b16 74 65 64 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  ted functionalit
30b17 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6e 6f  y..**.** $Id: no
30b18 74 69 66 79 2e 63 2c 76 20 31 2e 34 20 32 30 30  tify.c,v 1.4 200
30b19 39 2f 30 34 2f 30 37 20 32 32 3a 30 36 3a 35 37  9/04/07 22:06:57
30b1a 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
30b1b 2a 20 4f 6d 69 74 20 74 68 69 73 20 65 6e 74 69  * Omit this enti
30b1c 72 65 20 66 69 6c 65 20 69 66 20 53 51 4c 49 54  re file if SQLIT
30b1d 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f  E_ENABLE_UNLOCK_
30b1e 4e 4f 54 49 46 59 20 69 73 20 6e 6f 74 20 64 65  NOTIFY is not de
30b1f 66 69 6e 65 64 2e 20 2a 2f 0a 23 69 66 64 65 66  fined. */.#ifdef
30b20 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55   SQLITE_ENABLE_U
30b21 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 0a 2f 2a  NLOCK_NOTIFY../*
30b22 0a 2a 2a 20 50 75 62 6c 69 63 20 69 6e 74 65 72  .** Public inter
30b23 66 61 63 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 73  faces:.**.**   s
30b24 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
30b25 42 6c 6f 63 6b 65 64 28 29 0a 2a 2a 20 20 20 73  Blocked().**   s
30b26 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
30b27 55 6e 6c 6f 63 6b 65 64 28 29 0a 2a 2a 20 20 20  Unlocked().**   
30b28 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
30b29 6e 43 6c 6f 73 65 64 28 29 0a 2a 2a 20 20 20 73  nClosed().**   s
30b2a 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
30b2b 74 69 66 79 28 29 0a 2a 2f 0a 0a 23 64 65 66 69  tify().*/..#defi
30b2c 6e 65 20 61 73 73 65 72 74 4d 75 74 65 78 48 65  ne assertMutexHe
30b2d 6c 64 28 29 20 5c 0a 20 20 61 73 73 65 72 74 28  ld() \.  assert(
30b2e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
30b2f 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78  eld(sqlite3Mutex
30b30 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
30b31 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
30b32 29 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 48 65 61 64  )) )../*.** Head
30b33 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
30b34 74 20 6f 66 20 61 6c 6c 20 73 71 6c 69 74 65 33  t of all sqlite3
30b35 20 6f 62 6a 65 63 74 73 20 63 72 65 61 74 65 64   objects created
30b36 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 73 73   by this process
30b37 0a 2a 2a 20 66 6f 72 20 77 68 69 63 68 20 65 69  .** for which ei
30b38 74 68 65 72 20 73 71 6c 69 74 65 33 2e 70 42 6c  ther sqlite3.pBl
30b39 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
30b3a 20 6f 72 20 73 71 6c 69 74 65 33 2e 70 55 6e 6c   or sqlite3.pUnl
30b3b 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  ockConnection.**
30b3c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 54 68   is not NULL. Th
30b3d 69 73 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20  is variable may 
30b3e 6f 6e 6c 79 20 61 63 63 65 73 73 65 64 20 77 68  only accessed wh
30b3f 69 6c 65 20 74 68 65 20 53 54 41 54 49 43 5f 4d  ile the STATIC_M
30b40 41 53 54 45 52 0a 2a 2a 20 6d 75 74 65 78 20 69  ASTER.** mutex i
30b41 73 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69  s held..*/.stati
30b42 63 20 73 71 6c 69 74 65 33 20 2a 53 51 4c 49 54  c sqlite3 *SQLIT
30b43 45 5f 57 53 44 20 73 71 6c 69 74 65 33 42 6c 6f  E_WSD sqlite3Blo
30b44 63 6b 65 64 4c 69 73 74 20 3d 20 30 3b 0a 0a 23  ckedList = 0;..#
30b45 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
30b46 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
30b47 6e 20 69 73 20 61 20 63 6f 6d 70 6c 65 78 20 61  n is a complex a
30b48 73 73 65 72 74 28 29 20 74 68 61 74 20 76 65 72  ssert() that ver
30b49 69 66 69 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77  ifies the follow
30b4a 69 6e 67 20 0a 2a 2a 20 70 72 6f 70 65 72 74 69  ing .** properti
30b4b 65 73 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 65  es of the blocke
30b4c 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69  d connections li
30b4d 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 45  st:.**.**   1) E
30b4e 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ach entry in the
30b4f 20 6c 69 73 74 20 68 61 73 20 61 20 6e 6f 6e 2d   list has a non-
30b50 4e 55 4c 4c 20 76 61 6c 75 65 20 66 6f 72 20 65  NULL value for e
30b51 69 74 68 65 72 20 0a 2a 2a 20 20 20 20 20 20 70  ither .**      p
30b52 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
30b53 20 6f 72 20 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e   or pBlockingCon
30b54 6e 65 63 74 69 6f 6e 2c 20 6f 72 20 62 6f 74 68  nection, or both
30b55 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 41 6c 6c  ..**.**   2) All
30b56 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
30b57 6c 69 73 74 20 74 68 61 74 20 73 68 61 72 65 20  list that share 
30b58 61 20 63 6f 6d 6d 6f 6e 20 76 61 6c 75 65 20 66  a common value f
30b59 6f 72 20 0a 2a 2a 20 20 20 20 20 20 78 55 6e 6c  or .**      xUnl
30b5a 6f 63 6b 4e 6f 74 69 66 79 20 61 72 65 20 67 72  ockNotify are gr
30b5b 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 2e 0a  ouped together..
30b5c 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66 20 74 68  **.**   3) If th
30b5d 65 20 61 72 67 75 6d 65 6e 74 20 64 62 20 69 73  e argument db is
30b5e 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
30b5f 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e 74 72  none of the entr
30b60 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20  ies in the.**   
30b61 20 20 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65     blocked conne
30b62 63 74 69 6f 6e 73 20 6c 69 73 74 20 68 61 76 65  ctions list have
30b63 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69   pUnlockConnecti
30b64 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b 69 6e 67 43  on or pBlockingC
30b65 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20  onnection.**    
30b66 20 20 73 65 74 20 74 6f 20 64 62 2e 20 54 68 69    set to db. Thi
30b67 73 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 63  s is used when c
30b68 6c 6f 73 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  losing connectio
30b69 6e 20 64 62 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  n db..*/.static 
30b6a 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 50 72  void checkListPr
30b6b 6f 70 65 72 74 69 65 73 28 73 71 6c 69 74 65 33  operties(sqlite3
30b6c 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33   *db){.  sqlite3
30b6d 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c   *p;.  for(p=sql
30b6e 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b  ite3BlockedList;
30b6f 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 42 6c   p; p=p->pNextBl
30b70 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69 6e 74 20  ocked){.    int 
30b71 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20 73 71  seen = 0;.    sq
30b72 6c 69 74 65 33 20 2a 70 32 3b 0a 0a 20 20 20 20  lite3 *p2;..    
30b73 2f 2a 20 56 65 72 69 66 79 20 70 72 6f 70 65 72  /* Verify proper
30b74 74 79 20 28 31 29 20 2a 2f 0a 20 20 20 20 61 73  ty (1) */.    as
30b75 73 65 72 74 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b  sert( p->pUnlock
30b76 43 6f 6e 6e 65 63 74 69 6f 6e 20 7c 7c 20 70 2d  Connection || p-
30b77 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
30b78 74 69 6f 6e 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  tion );..    /* 
30b79 56 65 72 69 66 79 20 70 72 6f 70 65 72 74 79 20  Verify property 
30b7a 28 32 29 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70  (2) */.    for(p
30b7b 32 3d 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64  2=sqlite3Blocked
30b7c 4c 69 73 74 3b 20 70 32 21 3d 70 3b 20 70 32 3d  List; p2!=p; p2=
30b7d 70 32 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64  p2->pNextBlocked
30b7e 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 32 2d  ){.      if( p2-
30b7f 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3d 3d  >xUnlockNotify==
30b80 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  p->xUnlockNotify
30b81 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20   ) seen = 1;.   
30b82 20 20 20 61 73 73 65 72 74 28 20 70 32 2d 3e 78     assert( p2->x
30b83 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3d 3d 70 2d  UnlockNotify==p-
30b84 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 7c  >xUnlockNotify |
30b85 7c 20 21 73 65 65 6e 20 29 3b 0a 20 20 20 20 20  | !seen );.     
30b86 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c   assert( db==0 |
30b87 7c 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e  | p->pUnlockConn
30b88 65 63 74 69 6f 6e 21 3d 64 62 20 29 3b 0a 20 20  ection!=db );.  
30b89 20 20 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d      assert( db==
30b8a 30 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e  0 || p->pBlockin
30b8b 67 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64 62 20  gConnection!=db 
30b8c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23  );.    }.  }.}.#
30b8d 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 63 68  else.# define ch
30b8e 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65  eckListPropertie
30b8f 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  s(x).#endif../*.
30b90 2a 2a 20 52 65 6d 6f 76 65 20 63 6f 6e 6e 65 63  ** Remove connec
30b91 74 69 6f 6e 20 64 62 20 66 72 6f 6d 20 74 68 65  tion db from the
30b92 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
30b93 69 6f 6e 73 20 6c 69 73 74 2e 20 49 66 20 63 6f  ions list. If co
30b94 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 64 62 20 69  nnection.** db i
30b95 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
30b96 61 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69  a part of the li
30b97 73 74 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  st, this functio
30b98 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  n is a no-op..*/
30b99 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6d  .static void rem
30b9a 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69  oveFromBlockedLi
30b9b 73 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  st(sqlite3 *db){
30b9c 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 3b  .  sqlite3 **pp;
30b9d 0a 20 20 61 73 73 65 72 74 4d 75 74 65 78 48 65  .  assertMutexHe
30b9e 6c 64 28 29 3b 0a 20 20 66 6f 72 28 70 70 3d 26  ld();.  for(pp=&
30b9f 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69  sqlite3BlockedLi
30ba0 73 74 3b 20 2a 70 70 3b 20 70 70 20 3d 20 26 28  st; *pp; pp = &(
30ba1 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b  *pp)->pNextBlock
30ba2 65 64 29 7b 0a 20 20 20 20 69 66 28 20 2a 70 70  ed){.    if( *pp
30ba3 3d 3d 64 62 20 29 7b 0a 20 20 20 20 20 20 2a 70  ==db ){.      *p
30ba4 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74  p = (*pp)->pNext
30ba5 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 20 20 62  Blocked;.      b
30ba6 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
30ba7 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 6e  }../*.** Add con
30ba8 6e 65 63 74 69 6f 6e 20 64 62 20 74 6f 20 74 68  nection db to th
30ba9 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
30baa 74 69 6f 6e 73 20 6c 69 73 74 2e 20 49 74 20 69  tions list. It i
30bab 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61  s assumed.** tha
30bac 74 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65  t it is not alre
30bad 61 64 79 20 61 20 70 61 72 74 20 6f 66 20 74 68  ady a part of th
30bae 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  e list..*/.stati
30baf 63 20 76 6f 69 64 20 61 64 64 54 6f 42 6c 6f 63  c void addToBloc
30bb0 6b 65 64 4c 69 73 74 28 73 71 6c 69 74 65 33 20  kedList(sqlite3 
30bb1 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  *db){.  sqlite3 
30bb2 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 4d 75  **pp;.  assertMu
30bb3 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 66 6f 72  texHeld();.  for
30bb4 28 0a 20 20 20 20 70 70 3d 26 73 71 6c 69 74 65  (.    pp=&sqlite
30bb5 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 0a 20  3BlockedList; . 
30bb6 20 20 20 2a 70 70 20 26 26 20 28 2a 70 70 29 2d     *pp && (*pp)-
30bb7 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 21 3d  >xUnlockNotify!=
30bb8 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  db->xUnlockNotif
30bb9 79 3b 20 0a 20 20 20 20 70 70 3d 26 28 2a 70 70  y; .    pp=&(*pp
30bba 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 0a  )->pNextBlocked.
30bbb 20 20 29 3b 0a 20 20 64 62 2d 3e 70 4e 65 78 74    );.  db->pNext
30bbc 42 6c 6f 63 6b 65 64 20 3d 20 2a 70 70 3b 0a 20  Blocked = *pp;. 
30bbd 20 2a 70 70 20 3d 20 64 62 3b 0a 7d 0a 0a 2f 2a   *pp = db;.}../*
30bbe 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 53  .** Obtain the S
30bbf 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74  TATIC_MASTER mut
30bc0 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ex..*/.static vo
30bc1 69 64 20 65 6e 74 65 72 4d 75 74 65 78 28 76 6f  id enterMutex(vo
30bc2 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id){.  sqlite3_m
30bc3 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
30bc4 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
30bc5 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
30bc6 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 63 68 65  _MASTER));.  che
30bc7 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73  ckListProperties
30bc8 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  (0);.}../*.** Re
30bc9 6c 65 61 73 65 20 74 68 65 20 53 54 41 54 49 43  lease the STATIC
30bca 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 0a 2a  _MASTER mutex..*
30bcb 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
30bcc 61 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  aveMutex(void){.
30bcd 20 20 61 73 73 65 72 74 4d 75 74 65 78 48 65 6c    assertMutexHel
30bce 64 28 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74  d();.  checkList
30bcf 50 72 6f 70 65 72 74 69 65 73 28 30 29 3b 0a 20  Properties(0);. 
30bd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
30bd1 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65  eave(sqlite3Mute
30bd2 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
30bd3 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
30bd4 52 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  R));.}../*.** Re
30bd5 67 69 73 74 65 72 20 61 6e 20 75 6e 6c 6f 63 6b  gister an unlock
30bd6 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
30bd7 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
30bd8 63 61 6c 6c 65 64 20 61 66 74 65 72 20 63 6f 6e  called after con
30bd9 6e 65 63 74 69 6f 6e 20 22 64 62 22 20 68 61 73  nection "db" has
30bda 20 61 74 74 65 6d 70 74 65 64 20 73 6f 6d 65 20   attempted some 
30bdb 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 62 75 74  operation.** but
30bdc 20 68 61 73 20 72 65 63 65 69 76 65 64 20 61 6e   has received an
30bdd 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65   SQLITE_LOCKED e
30bde 72 72 6f 72 20 62 65 63 61 75 73 65 20 61 6e 6f  rror because ano
30bdf 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  ther connection.
30be0 2a 2a 20 28 63 61 6c 6c 20 69 74 20 70 4f 74 68  ** (call it pOth
30be1 65 72 29 20 69 6e 20 74 68 65 20 73 61 6d 65 20  er) in the same 
30be2 70 72 6f 63 65 73 73 20 77 61 73 20 62 75 73 79  process was busy
30be3 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
30be4 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 2e  shared.** cache.
30be5 20 20 70 4f 74 68 65 72 20 69 73 20 66 6f 75 6e    pOther is foun
30be6 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20  d by looking at 
30be7 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e  db->pBlockingCon
30be8 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  nection..**.** I
30be9 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 62 6c  f there is no bl
30bea 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
30beb 6e 2c 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  n, the callback 
30bec 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64  is invoked immed
30bed 69 61 74 65 6c 79 2c 0a 2a 2a 20 62 65 66 6f 72  iately,.** befor
30bee 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  e this routine r
30bef 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  eturns..**.** If
30bf0 20 70 4f 74 68 65 72 20 69 73 20 61 6c 72 65 61   pOther is alrea
30bf1 64 79 20 62 6c 6f 63 6b 65 64 20 6f 6e 20 64 62  dy blocked on db
30bf2 2c 20 74 68 65 6e 20 72 65 70 6f 72 74 20 53 51  , then report SQ
30bf3 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20 74 6f 20  LITE_LOCKED, to 
30bf4 69 6e 64 69 63 61 74 65 0a 2a 2a 20 61 20 64 65  indicate.** a de
30bf5 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 74  adlock..**.** Ot
30bf6 68 65 72 77 69 73 65 2c 20 6d 61 6b 65 20 61 72  herwise, make ar
30bf7 72 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 69 6e  rangements to in
30bf8 76 6f 6b 65 20 78 4e 6f 74 69 66 79 20 77 68 65  voke xNotify whe
30bf9 6e 20 70 4f 74 68 65 72 20 64 72 6f 70 73 0a 2a  n pOther drops.*
30bfa 2a 20 69 74 73 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a  * its locks..**.
30bfb 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20  ** Each call to 
30bfc 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 76 65  this routine ove
30bfd 72 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72  rrides any prior
30bfe 20 63 61 6c 6c 62 61 63 6b 73 20 72 65 67 69 73   callbacks regis
30bff 74 65 72 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  tered.** on the 
30c00 73 61 6d 65 20 22 64 62 22 2e 20 20 49 66 20 78  same "db".  If x
30c01 4e 6f 74 69 66 79 3d 3d 30 20 74 68 65 6e 20 61  Notify==0 then a
30c02 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 62 61 63  ny prior callbac
30c03 6b 73 20 61 72 65 20 69 6d 6d 65 64 69 61 74 65  ks are immediate
30c04 6c 79 0a 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 2e  ly.** cancelled.
30c05 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
30c06 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  nt sqlite3_unloc
30c07 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c 69  k_notify(.  sqli
30c08 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20  te3 *db,.  void 
30c09 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69 64 20  (*xNotify)(void 
30c0a 2a 2a 2c 20 69 6e 74 29 2c 0a 20 20 76 6f 69 64  **, int),.  void
30c0b 20 2a 70 41 72 67 0a 29 7b 0a 20 20 69 6e 74 20   *pArg.){.  int 
30c0c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
30c0d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
30c0e 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
30c0f 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28  );.  enterMutex(
30c10 29 3b 0a 0a 20 20 69 66 28 20 78 4e 6f 74 69 66  );..  if( xNotif
30c11 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 6d 6f  y==0 ){.    remo
30c12 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73  veFromBlockedLis
30c13 74 28 64 62 29 3b 0a 20 20 20 20 64 62 2d 3e 70  t(db);.    db->p
30c14 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
30c15 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 78 55   = 0;.    db->xU
30c16 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 30 3b  nlockNotify = 0;
30c17 0a 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b  .    db->pUnlock
30c18 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Arg = 0;.  }else
30c19 20 69 66 28 20 30 3d 3d 64 62 2d 3e 70 42 6c 6f   if( 0==db->pBlo
30c1a 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20  ckingConnection 
30c1b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6c  ){.    /* The bl
30c1c 6f 63 6b 69 6e 67 20 74 72 61 6e 73 61 63 74 69  ocking transacti
30c1d 6f 6e 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 63  on has been conc
30c1e 6c 75 64 65 64 2e 20 4f 72 20 74 68 65 72 65 20  luded. Or there 
30c1f 6e 65 76 65 72 20 77 61 73 20 61 20 0a 20 20 20  never was a .   
30c20 20 2a 2a 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61   ** blocking tra
30c21 6e 73 61 63 74 69 6f 6e 2e 20 49 6e 20 65 69 74  nsaction. In eit
30c22 68 65 72 20 63 61 73 65 2c 20 69 6e 76 6f 6b 65  her case, invoke
30c23 20 74 68 65 20 6e 6f 74 69 66 79 20 63 61 6c 6c   the notify call
30c24 62 61 63 6b 0a 20 20 20 20 2a 2a 20 69 6d 6d 65  back.    ** imme
30c25 64 69 61 74 65 6c 79 2e 20 0a 20 20 20 20 2a 2f  diately. .    */
30c26 0a 20 20 20 20 78 4e 6f 74 69 66 79 28 26 70 41  .    xNotify(&pA
30c27 72 67 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  rg, 1);.  }else{
30c28 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 70 3b  .    sqlite3 *p;
30c29 0a 0a 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e  ..    for(p=db->
30c2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
30c2b 69 6f 6e 3b 20 70 20 26 26 20 70 21 3d 64 62 3b  ion; p && p!=db;
30c2c 20 70 3d 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e   p=p->pUnlockCon
30c2d 6e 65 63 74 69 6f 6e 29 7b 7d 0a 20 20 20 20 69  nection){}.    i
30c2e 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 72 63  f( p ){.      rc
30c2f 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44   = SQLITE_LOCKED
30c30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
30c31 2a 20 44 65 61 64 6c 6f 63 6b 20 64 65 74 65 63  * Deadlock detec
30c32 74 65 64 2e 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  ted. */.    }els
30c33 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 70 55 6e  e{.      db->pUn
30c34 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  lockConnection =
30c35 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f   db->pBlockingCo
30c36 6e 6e 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20  nnection;.      
30c37 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  db->xUnlockNotif
30c38 79 20 3d 20 78 4e 6f 74 69 66 79 3b 0a 20 20 20  y = xNotify;.   
30c39 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 41 72     db->pUnlockAr
30c3a 67 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 20 20  g = pArg;.      
30c3b 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65  removeFromBlocke
30c3c 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20 20  dList(db);.     
30c3d 20 61 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73   addToBlockedLis
30c3e 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  t(db);.    }.  }
30c3f 0a 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29  ..  leaveMutex()
30c40 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
30c41 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
30c42 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  .  sqlite3Error(
30c43 64 62 2c 20 72 63 2c 20 28 72 63 3f 22 64 61 74  db, rc, (rc?"dat
30c44 61 62 61 73 65 20 69 73 20 64 65 61 64 6c 6f 63  abase is deadloc
30c45 6b 65 64 22 3a 30 29 29 3b 0a 20 20 73 71 6c 69  ked":0));.  sqli
30c46 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
30c47 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
30c48 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
30c49 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
30c4a 69 73 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20  is called while 
30c4b 73 74 65 70 70 69 6e 67 20 6f 72 20 70 72 65 70  stepping or prep
30c4c 61 72 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e  aring a statemen
30c4d 74 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  t .** associated
30c4e 20 77 69 74 68 20 63 6f 6e 6e 65 63 74 69 6f 6e   with connection
30c4f 20 64 62 2e 20 54 68 65 20 6f 70 65 72 61 74 69   db. The operati
30c50 6f 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53  on will return S
30c51 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 0a 2a 2a 20  QLITE_LOCKED.** 
30c52 74 6f 20 74 68 65 20 75 73 65 72 20 62 65 63 61  to the user beca
30c53 75 73 65 20 69 74 20 72 65 71 75 69 72 65 73 20  use it requires 
30c54 61 20 6c 6f 63 6b 20 74 68 61 74 20 77 69 6c 6c  a lock that will
30c55 20 6e 6f 74 20 62 65 20 61 76 61 69 6c 61 62 6c   not be availabl
30c56 65 0a 2a 2a 20 75 6e 74 69 6c 20 63 6f 6e 6e 65  e.** until conne
30c57 63 74 69 6f 6e 20 70 42 6c 6f 63 6b 65 72 20 63  ction pBlocker c
30c58 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 63 75 72  oncludes its cur
30c59 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
30c5a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
30c5b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
30c5c 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65  ConnectionBlocke
30c5d 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73  d(sqlite3 *db, s
30c5e 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 65 72  qlite3 *pBlocker
30c5f 29 7b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28  ){.  enterMutex(
30c60 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 70 42 6c  );.  if( db->pBl
30c61 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
30c62 3d 3d 30 20 26 26 20 64 62 2d 3e 70 55 6e 6c 6f  ==0 && db->pUnlo
30c63 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20  ckConnection==0 
30c64 29 7b 0a 20 20 20 20 61 64 64 54 6f 42 6c 6f 63  ){.    addToBloc
30c65 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 7d  kedList(db);.  }
30c66 0a 20 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67  .  db->pBlocking
30c67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 70 42 6c  Connection = pBl
30c68 6f 63 6b 65 72 3b 0a 20 20 6c 65 61 76 65 4d 75  ocker;.  leaveMu
30c69 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex();.}../*.** 
30c6a 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
30c6b 20 63 61 6c 6c 65 64 20 77 68 65 6e 0a 2a 2a 20   called when.** 
30c6c 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
30c6d 6f 70 65 6e 65 64 20 62 79 20 64 61 74 61 62 61  opened by databa
30c6e 73 65 20 64 62 20 68 61 73 20 6a 75 73 74 20 66  se db has just f
30c6f 69 6e 69 73 68 65 64 2e 20 4c 6f 63 6b 73 20 68  inished. Locks h
30c70 65 6c 64 20 0a 2a 2a 20 62 79 20 64 61 74 61 62  eld .** by datab
30c71 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64  ase connection d
30c72 62 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c 65  b have been rele
30c73 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ased..**.** This
30c74 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 70 73 20   function loops 
30c75 74 68 72 6f 75 67 68 20 65 61 63 68 20 65 6e 74  through each ent
30c76 72 79 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 65  ry in the blocke
30c77 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a  d connections.**
30c78 20 6c 69 73 74 20 61 6e 64 20 64 6f 65 73 20 74   list and does t
30c79 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
30c7a 0a 2a 2a 20 20 20 31 29 20 49 66 20 74 68 65 20  .**   1) If the 
30c7b 73 71 6c 69 74 65 33 2e 70 42 6c 6f 63 6b 69 6e  sqlite3.pBlockin
30c7c 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65 6d 62  gConnection memb
30c7d 65 72 20 6f 66 20 61 20 6c 69 73 74 20 65 6e 74  er of a list ent
30c7e 72 79 20 69 73 0a 2a 2a 20 20 20 20 20 20 73 65  ry is.**      se
30c7f 74 20 74 6f 20 64 62 2c 20 74 68 65 6e 20 73 65  t to db, then se
30c80 74 20 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  t pBlockingConne
30c81 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a 2a 2a 20 20  ction=0..**.**  
30c82 20 32 29 20 49 66 20 74 68 65 20 73 71 6c 69 74   2) If the sqlit
30c83 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  e3.pUnlockConnec
30c84 74 69 6f 6e 20 6d 65 6d 62 65 72 20 6f 66 20 61  tion member of a
30c85 20 6c 69 73 74 20 65 6e 74 72 79 20 69 73 0a 2a   list entry is.*
30c86 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64 62  *      set to db
30c87 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68  , then invoke th
30c88 65 20 63 6f 6e 66 69 67 75 72 65 64 20 75 6e 6c  e configured unl
30c89 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
30c8a 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ack and.**      
30c8b 73 65 74 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65  set pUnlockConne
30c8c 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a 2a 2a 20 20  ction=0..**.**  
30c8d 20 33 29 20 49 66 20 74 68 65 20 74 77 6f 20 73   3) If the two s
30c8e 74 65 70 73 20 61 62 6f 76 65 20 6d 65 61 6e 20  teps above mean 
30c8f 74 68 61 74 20 70 42 6c 6f 63 6b 69 6e 67 43 6f  that pBlockingCo
30c90 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 61 6e 64 0a  nnection==0 and.
30c91 2a 2a 20 20 20 20 20 20 70 55 6e 6c 6f 63 6b 43  **      pUnlockC
30c92 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 2c 20 72 65  onnection==0, re
30c93 6d 6f 76 65 20 74 68 65 20 65 6e 74 72 79 20 66  move the entry f
30c94 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  rom the blocked 
30c95 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20 20  connections.**  
30c96 20 20 20 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c      list..*/.SQL
30c97 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
30c98 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
30c99 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71 6c 69 74  onUnlocked(sqlit
30c9a 65 33 20 2a 64 62 29 7b 0a 20 20 76 6f 69 64 20  e3 *db){.  void 
30c9b 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29  (*xUnlockNotify)
30c9c 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 20 3d  (void **, int) =
30c9d 20 30 3b 20 2f 2a 20 55 6e 6c 6f 63 6b 2d 6e 6f   0; /* Unlock-no
30c9e 74 69 66 79 20 63 62 20 74 6f 20 69 6e 76 6f 6b  tify cb to invok
30c9f 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 20  e */.  int nArg 
30ca0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
30ca1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ca2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
30ca3 72 69 65 73 20 69 6e 20 61 41 72 67 5b 5d 20 2a  ries in aArg[] *
30ca4 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
30ca5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
30ca6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30ca7 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
30ca8 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 41  e */.  void **aA
30ca9 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
30caa 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 74    /* Arguments t
30cab 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 20 63 61 6c  o the unlock cal
30cac 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20  lback */.  void 
30cad 2a 2a 61 44 79 6e 20 3d 20 30 3b 20 20 20 20 20  **aDyn = 0;     
30cae 20 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63        /* Dynamic
30caf 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ally allocated s
30cb0 70 61 63 65 20 66 6f 72 20 61 41 72 67 5b 5d 20  pace for aArg[] 
30cb1 2a 2f 0a 20 20 76 6f 69 64 20 2a 61 53 74 61 74  */.  void *aStat
30cb2 69 63 5b 31 36 5d 3b 20 20 20 20 20 20 20 20 20  ic[16];         
30cb3 2f 2a 20 53 74 61 72 74 65 72 20 73 70 61 63 65  /* Starter space
30cb4 20 66 6f 72 20 61 41 72 67 5b 5d 2e 20 20 4e 6f   for aArg[].  No
30cb5 20 6d 61 6c 6c 6f 63 20 72 65 71 75 69 72 65 64   malloc required
30cb6 20 2a 2f 0a 0a 20 20 61 41 72 67 20 3d 20 61 53   */..  aArg = aS
30cb7 74 61 74 69 63 3b 0a 20 20 65 6e 74 65 72 4d 75  tatic;.  enterMu
30cb8 74 65 78 28 29 3b 20 20 20 20 20 20 20 20 20 2f  tex();         /
30cb9 2a 20 45 6e 74 65 72 20 53 54 41 54 49 43 5f 4d  * Enter STATIC_M
30cba 41 53 54 45 52 20 6d 75 74 65 78 20 2a 2f 0a 0a  ASTER mutex */..
30cbb 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72    /* This loop r
30cbc 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  uns once for eac
30cbd 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62  h entry in the b
30cbe 6c 6f 63 6b 65 64 2d 63 6f 6e 6e 65 63 74 69 6f  locked-connectio
30cbf 6e 73 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 66 6f  ns list. */.  fo
30cc0 72 28 70 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f  r(pp=&sqlite3Blo
30cc1 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70 3b 20 2f  ckedList; *pp; /
30cc2 2a 20 6e 6f 2d 6f 70 20 2a 2f 20 29 7b 0a 20 20  * no-op */ ){.  
30cc3 20 20 73 71 6c 69 74 65 33 20 2a 70 20 3d 20 2a    sqlite3 *p = *
30cc4 70 70 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70  pp;..    /* Step
30cc5 20 31 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70   1. */.    if( p
30cc6 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
30cc7 63 74 69 6f 6e 3d 3d 64 62 20 29 7b 0a 20 20 20  ction==db ){.   
30cc8 20 20 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43     p->pBlockingC
30cc9 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20  onnection = 0;. 
30cca 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65     }..    /* Ste
30ccb 70 20 32 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  p 2. */.    if( 
30ccc 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  p->pUnlockConnec
30ccd 74 69 6f 6e 3d 3d 64 62 20 29 7b 0a 20 20 20 20  tion==db ){.    
30cce 20 20 61 73 73 65 72 74 28 20 70 2d 3e 78 55 6e    assert( p->xUn
30ccf 6c 6f 63 6b 4e 6f 74 69 66 79 20 29 3b 0a 20 20  lockNotify );.  
30cd0 20 20 20 20 69 66 28 20 70 2d 3e 78 55 6e 6c 6f      if( p->xUnlo
30cd1 63 6b 4e 6f 74 69 66 79 21 3d 78 55 6e 6c 6f 63  ckNotify!=xUnloc
30cd2 6b 4e 6f 74 69 66 79 20 26 26 20 6e 41 72 67 21  kNotify && nArg!
30cd3 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 78 55  =0 ){.        xU
30cd4 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61 41 72 67  nlockNotify(aArg
30cd5 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 20  , nArg);.       
30cd6 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 20   nArg = 0;.     
30cd7 20 7d 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65   }..      sqlite
30cd8 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
30cd9 6f 63 28 29 3b 0a 20 20 20 20 20 20 61 73 73 65  oc();.      asse
30cda 72 74 28 20 61 41 72 67 3d 3d 61 44 79 6e 20 7c  rt( aArg==aDyn |
30cdb 7c 20 28 61 44 79 6e 3d 3d 30 20 26 26 20 61 41  | (aDyn==0 && aA
30cdc 72 67 3d 3d 61 53 74 61 74 69 63 29 20 29 3b 0a  rg==aStatic) );.
30cdd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41        assert( nA
30cde 72 67 3c 3d 28 69 6e 74 29 41 72 72 61 79 53 69  rg<=(int)ArraySi
30cdf 7a 65 28 61 53 74 61 74 69 63 29 20 7c 7c 20 61  ze(aStatic) || a
30ce0 41 72 67 3d 3d 61 44 79 6e 20 29 3b 0a 20 20 20  Arg==aDyn );.   
30ce1 20 20 20 69 66 28 20 28 21 61 44 79 6e 20 26 26     if( (!aDyn &&
30ce2 20 6e 41 72 67 3d 3d 28 69 6e 74 29 41 72 72 61   nArg==(int)Arra
30ce3 79 53 69 7a 65 28 61 53 74 61 74 69 63 29 29 0a  ySize(aStatic)).
30ce4 20 20 20 20 20 20 20 7c 7c 20 28 61 44 79 6e 20         || (aDyn 
30ce5 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74 29 28 73  && nArg==(int)(s
30ce6 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
30ce7 7a 65 28 64 62 2c 20 61 44 79 6e 29 2f 73 69 7a  ze(db, aDyn)/siz
30ce8 65 6f 66 28 76 6f 69 64 2a 29 29 29 0a 20 20 20  eof(void*))).   
30ce9 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a     ){.        /*
30cea 20 54 68 65 20 61 41 72 67 5b 5d 20 61 72 72 61   The aArg[] arra
30ceb 79 20 6e 65 65 64 73 20 74 6f 20 67 72 6f 77 2e  y needs to grow.
30cec 20 2a 2f 0a 20 20 20 20 20 20 20 20 76 6f 69 64   */.        void
30ced 20 2a 2a 70 4e 65 77 20 3d 20 28 76 6f 69 64 20   **pNew = (void 
30cee 2a 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  **)sqlite3Malloc
30cef 28 6e 41 72 67 2a 73 69 7a 65 6f 66 28 76 6f 69  (nArg*sizeof(voi
30cf0 64 20 2a 29 2a 32 29 3b 0a 20 20 20 20 20 20 20  d *)*2);.       
30cf1 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
30cf2 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e         memcpy(pN
30cf3 65 77 2c 20 61 41 72 67 2c 20 6e 41 72 67 2a 73  ew, aArg, nArg*s
30cf4 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29 29 3b 0a  izeof(void *));.
30cf5 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
30cf6 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a 20 20  3_free(aDyn);.  
30cf7 20 20 20 20 20 20 20 20 61 44 79 6e 20 3d 20 61          aDyn = a
30cf8 41 72 67 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  Arg = pNew;.    
30cf9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
30cfa 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6f 63 63       /* This occ
30cfb 75 72 73 20 77 68 65 6e 20 74 68 65 20 61 72 72  urs when the arr
30cfc 61 79 20 6f 66 20 63 6f 6e 74 65 78 74 20 70 6f  ay of context po
30cfd 69 6e 74 65 72 73 20 74 68 61 74 20 6e 65 65 64  inters that need
30cfe 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   to.          **
30cff 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68   be passed to th
30d00 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  e unlock-notify 
30d01 63 61 6c 6c 62 61 63 6b 20 69 73 20 6c 61 72 67  callback is larg
30d02 65 72 20 74 68 61 6e 20 74 68 65 0a 20 20 20 20  er than the.    
30d03 20 20 20 20 20 20 2a 2a 20 61 53 74 61 74 69 63        ** aStatic
30d04 5b 5d 20 61 72 72 61 79 20 61 6c 6c 6f 63 61 74  [] array allocat
30d05 65 64 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20  ed on the stack 
30d06 61 6e 64 20 74 68 65 20 61 74 74 65 6d 70 74 20  and the attempt 
30d07 74 6f 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  to .          **
30d08 20 61 6c 6c 6f 63 61 74 65 20 61 20 6c 61 72 67   allocate a larg
30d09 65 72 20 61 72 72 61 79 20 66 72 6f 6d 20 74 68  er array from th
30d0a 65 20 68 65 61 70 20 68 61 73 20 66 61 69 6c 65  e heap has faile
30d0b 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a  d..          **.
30d0c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69            ** Thi
30d0d 73 20 69 73 20 61 20 64 69 66 66 69 63 75 6c 74  s is a difficult
30d0e 20 73 69 74 75 61 74 69 6f 6e 20 74 6f 20 68 61   situation to ha
30d0f 6e 64 6c 65 2e 20 52 65 74 75 72 6e 69 6e 67 20  ndle. Returning 
30d10 61 6e 20 65 72 72 6f 72 0a 20 20 20 20 20 20 20  an error.       
30d11 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20 74 68     ** code to th
30d12 65 20 63 61 6c 6c 65 72 20 69 73 20 69 6e 73 75  e caller is insu
30d13 66 66 69 63 69 65 6e 74 2c 20 61 73 20 65 76 65  fficient, as eve
30d14 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20 63 6f  n if an error co
30d15 64 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  de.          ** 
30d16 69 73 20 72 65 74 75 72 6e 65 64 20 74 68 65 20  is returned the 
30d17 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 63  transaction on c
30d18 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77 69 6c  onnection db wil
30d19 6c 20 73 74 69 6c 6c 20 62 65 0a 20 20 20 20 20  l still be.     
30d1a 20 20 20 20 20 2a 2a 20 63 6c 6f 73 65 64 20 61       ** closed a
30d1b 6e 64 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  nd the unlock-no
30d1c 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 73 20 6f  tify callbacks o
30d1d 6e 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  n blocked connec
30d1e 74 69 6f 6e 73 0a 20 20 20 20 20 20 20 20 20 20  tions.          
30d1f 2a 2a 20 77 69 6c 6c 20 67 6f 20 75 6e 69 73 73  ** will go uniss
30d20 75 65 64 2e 20 54 68 69 73 20 6d 69 67 68 74 20  ued. This might 
30d21 63 61 75 73 65 20 74 68 65 20 61 70 70 6c 69 63  cause the applic
30d22 61 74 69 6f 6e 20 74 6f 20 77 61 69 74 0a 20 20  ation to wait.  
30d23 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 66          ** indef
30d24 69 6e 69 74 65 6c 79 20 66 6f 72 20 61 6e 20 75  initely for an u
30d25 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
30d26 6c 62 61 63 6b 20 74 68 61 74 20 77 69 6c 6c 20  lback that will 
30d27 6e 65 76 65 72 20 0a 20 20 20 20 20 20 20 20 20  never .         
30d28 20 2a 2a 20 61 72 72 69 76 65 2e 0a 20 20 20 20   ** arrive..    
30d29 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
30d2a 20 20 20 2a 2a 20 49 6e 73 74 65 61 64 2c 20 69     ** Instead, i
30d2b 6e 76 6f 6b 65 20 74 68 65 20 75 6e 6c 6f 63 6b  nvoke the unlock
30d2c 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
30d2d 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65 78   with the contex
30d2e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  t.          ** a
30d2f 72 72 61 79 20 61 6c 72 65 61 64 79 20 61 63 63  rray already acc
30d30 75 6d 75 6c 61 74 65 64 2e 20 57 65 20 63 61 6e  umulated. We can
30d31 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20   then clear the 
30d32 61 72 72 61 79 20 61 6e 64 0a 20 20 20 20 20 20  array and.      
30d33 20 20 20 20 2a 2a 20 62 65 67 69 6e 20 61 63 63      ** begin acc
30d34 75 6d 75 6c 61 74 69 6e 67 20 61 6e 79 20 66 75  umulating any fu
30d35 72 74 68 65 72 20 63 6f 6e 74 65 78 74 20 70 6f  rther context po
30d36 69 6e 74 65 72 73 20 77 69 74 68 6f 75 74 20 0a  inters without .
30d37 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 71            ** req
30d38 75 69 72 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d  uiring any dynam
30d39 69 63 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 54  ic allocation. T
30d3a 68 69 73 20 69 73 20 73 75 62 2d 6f 70 74 69 6d  his is sub-optim
30d3b 61 6c 20 62 65 63 61 75 73 65 0a 20 20 20 20 20  al because.     
30d3c 20 20 20 20 20 2a 2a 20 69 74 20 6d 65 61 6e 73       ** it means
30d3d 20 74 68 61 74 20 69 6e 73 74 65 61 64 20 6f 66   that instead of
30d3e 20 6f 6e 65 20 63 61 6c 6c 62 61 63 6b 20 77 69   one callback wi
30d3f 74 68 20 61 20 6c 61 72 67 65 20 61 72 72 61 79  th a large array
30d40 20 6f 66 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   of.          **
30d41 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   context pointer
30d42 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  s the applicatio
30d43 6e 20 77 69 6c 6c 20 72 65 63 65 69 76 65 20 74  n will receive t
30d44 77 6f 20 6f 72 20 6d 6f 72 65 0a 20 20 20 20 20  wo or more.     
30d45 20 20 20 20 20 2a 2a 20 63 61 6c 6c 62 61 63 6b       ** callback
30d46 73 20 77 69 74 68 20 73 6d 61 6c 6c 65 72 20 61  s with smaller a
30d47 72 72 61 79 73 20 6f 66 20 63 6f 6e 74 65 78 74  rrays of context
30d48 20 70 6f 69 6e 74 65 72 73 2c 20 77 68 69 63 68   pointers, which
30d49 20 77 69 6c 6c 0a 20 20 20 20 20 20 20 20 20 20   will.          
30d4a 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 61 70  ** reduce the ap
30d4b 70 6c 69 63 61 74 69 6f 6e 73 20 61 62 69 6c 69  plications abili
30d4c 74 79 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65  ty to prioritize
30d4d 20 6d 75 6c 74 69 70 6c 65 20 0a 20 20 20 20 20   multiple .     
30d4e 20 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69       ** connecti
30d4f 6f 6e 73 2e 20 42 75 74 20 69 74 20 69 73 20 74  ons. But it is t
30d50 68 65 20 62 65 73 74 20 74 68 61 74 20 63 61 6e  he best that can
30d51 20 62 65 20 64 6f 6e 65 20 75 6e 64 65 72 20 74   be done under t
30d52 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  he.          ** 
30d53 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 0a 20  circumstances.. 
30d54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
30d55 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74        xUnlockNot
30d56 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29 3b  ify(aArg, nArg);
30d57 0a 20 20 20 20 20 20 20 20 20 20 6e 41 72 67 20  .          nArg 
30d58 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
30d59 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
30d5a 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
30d5b 6c 6f 63 28 29 3b 0a 0a 20 20 20 20 20 20 61 41  loc();..      aA
30d5c 72 67 5b 6e 41 72 67 2b 2b 5d 20 3d 20 70 2d 3e  rg[nArg++] = p->
30d5d 70 55 6e 6c 6f 63 6b 41 72 67 3b 0a 20 20 20 20  pUnlockArg;.    
30d5e 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20    xUnlockNotify 
30d5f 3d 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69  = p->xUnlockNoti
30d60 66 79 3b 0a 20 20 20 20 20 20 70 2d 3e 70 55 6e  fy;.      p->pUn
30d61 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  lockConnection =
30d62 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 78 55 6e   0;.      p->xUn
30d63 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 30 3b 0a  lockNotify = 0;.
30d64 20 20 20 20 20 20 70 2d 3e 70 55 6e 6c 6f 63 6b        p->pUnlock
30d65 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  Arg = 0;.    }..
30d66 20 20 20 20 2f 2a 20 53 74 65 70 20 33 2e 20 2a      /* Step 3. *
30d67 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 42 6c  /.    if( p->pBl
30d68 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
30d69 3d 3d 30 20 26 26 20 70 2d 3e 70 55 6e 6c 6f 63  ==0 && p->pUnloc
30d6a 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 29  kConnection==0 )
30d6b 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76  {.      /* Remov
30d6c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 66  e connection p f
30d6d 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  rom the blocked 
30d6e 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
30d6f 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70 70 20 3d  . */.      *pp =
30d70 20 70 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64   p->pNextBlocked
30d71 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74  ;.      p->pNext
30d72 42 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20  Blocked = 0;.   
30d73 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 70   }else{.      pp
30d74 20 3d 20 26 70 2d 3e 70 4e 65 78 74 42 6c 6f 63   = &p->pNextBloc
30d75 6b 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ked;.    }.  }..
30d76 20 20 69 66 28 20 6e 41 72 67 21 3d 30 20 29 7b    if( nArg!=0 ){
30d77 0a 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69  .    xUnlockNoti
30d78 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29 3b 0a  fy(aArg, nArg);.
30d79 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
30d7a 65 65 28 61 44 79 6e 29 3b 0a 20 20 6c 65 61 76  ee(aDyn);.  leav
30d7b 65 4d 75 74 65 78 28 29 3b 20 20 20 20 20 20 20  eMutex();       
30d7c 20 20 2f 2a 20 4c 65 61 76 65 20 53 54 41 54 49    /* Leave STATI
30d7d 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 20 2a  C_MASTER mutex *
30d7e 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  /.}../*.** This 
30d7f 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  is called when t
30d80 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
30d81 65 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73  ection passed as
30d82 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20   an argument is 
30d83 0a 2a 2a 20 62 65 69 6e 67 20 63 6c 6f 73 65 64  .** being closed
30d84 2e 20 54 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  . The connection
30d85 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   is removed from
30d86 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 6c 69 73   the blocked lis
30d87 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
30d88 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
30d89 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65  3ConnectionClose
30d8a 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  d(sqlite3 *db){.
30d8b 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74    sqlite3Connect
30d8c 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62 29 3b  ionUnlocked(db);
30d8d 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b  .  enterMutex();
30d8e 0a 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f  .  removeFromBlo
30d8f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20  ckedList(db);.  
30d90 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74  checkListPropert
30d91 69 65 73 28 64 62 29 3b 0a 20 20 6c 65 61 76 65  ies(db);.  leave
30d92 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69  Mutex();.}.#endi
30d93 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
30d94 2a 2a 20 45 6e 64 20 6f 66 20 6e 6f 74 69 66 79  ** End of notify
30d95 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
30d96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30d97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30d98 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
30d99 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
30d9a 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s3.c ***********
30d9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30d9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30d9d 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63  */./*.** 2006 Oc
30d9e 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 10.**.** The a
30d9f 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
30da0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
30da1 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
30da2 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
30da3 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
30da4 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
30da5 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
30da6 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
30da7 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
30da8 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
30da9 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
30daa 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
30dab 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
30dac 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
30dad 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
30dae 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
30daf 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
30db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30db1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30db2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30db3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30db4 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
30db5 68 69 73 20 69 73 20 61 6e 20 53 51 4c 69 74 65  his is an SQLite
30db6 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
30db7 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78 74 20 73  ting full-text s
30db8 65 61 72 63 68 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  earch..*/../*.**
30db9 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
30dba 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
30dbb 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
30dbc 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
30dbd 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
30dbe 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
30dbf 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
30dc0 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
30dc1 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
30dc2 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
30dc3 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
30dc4 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
30dc5 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
30dc6 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
30dc7 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
30dc8 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
30dc9 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
30dca 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 0a 2f 2a  defined)..*/../*
30dcb 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
30dcc 73 69 64 65 72 20 65 78 70 6f 72 74 69 6e 67 20  sider exporting 
30dcd 74 68 69 73 20 63 6f 6d 6d 65 6e 74 20 74 6f 20  this comment to 
30dce 61 6e 20 48 54 4d 4c 20 66 69 6c 65 20 6f 72 20  an HTML file or 
30dcf 74 68 65 0a 2a 2a 20 77 69 6b 69 2e 0a 2a 2f 0a  the.** wiki..*/.
30dd0 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74 65 78 74  /* The full-text
30dd1 20 69 6e 64 65 78 20 69 73 20 73 74 6f 72 65 64   index is stored
30dd2 20 69 6e 20 61 20 73 65 72 69 65 73 20 6f 66 20   in a series of 
30dd3 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65 29 0a 2a  b+tree (-like).*
30dd4 2a 20 73 74 72 75 63 74 75 72 65 73 20 63 61 6c  * structures cal
30dd5 6c 65 64 20 73 65 67 6d 65 6e 74 73 20 77 68 69  led segments whi
30dd6 63 68 20 6d 61 70 20 74 65 72 6d 73 20 74 6f 20  ch map terms to 
30dd7 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a  doclists.  The.*
30dd8 2a 20 73 74 72 75 63 74 75 72 65 73 20 61 72 65  * structures are
30dd9 20 6c 69 6b 65 20 62 2b 74 72 65 65 73 20 69 6e   like b+trees in
30dda 20 6c 61 79 6f 75 74 2c 20 62 75 74 20 61 72 65   layout, but are
30ddb 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f   constructed fro
30ddc 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20  m the.** bottom 
30ddd 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c 20 66 61  up in optimal fa
30dde 73 68 69 6f 6e 20 61 6e 64 20 61 72 65 20 6e 6f  shion and are no
30ddf 74 20 75 70 64 61 74 61 62 6c 65 2e 20 20 53 69  t updatable.  Si
30de0 6e 63 65 20 74 72 65 65 73 0a 2a 2a 20 61 72 65  nce trees.** are
30de1 20 62 75 69 6c 74 20 66 72 6f 6d 20 74 68 65 20   built from the 
30de2 62 6f 74 74 6f 6d 20 75 70 2c 20 74 68 69 6e 67  bottom up, thing
30de3 73 20 77 69 6c 6c 20 62 65 20 64 65 73 63 72 69  s will be descri
30de4 62 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  bed from the.** 
30de5 62 6f 74 74 6f 6d 20 75 70 2e 0a 2a 2a 0a 2a 2a  bottom up..**.**
30de6 0a 2a 2a 2a 2a 20 56 61 72 69 6e 74 73 20 2a 2a  .**** Varints **
30de7 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20  **.** The basic 
30de8 75 6e 69 74 20 6f 66 20 65 6e 63 6f 64 69 6e 67  unit of encoding
30de9 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 2d 6c   is a variable-l
30dea 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 61  ength integer ca
30deb 6c 6c 65 64 20 61 0a 2a 2a 20 76 61 72 69 6e 74  lled a.** varint
30dec 2e 20 20 57 65 20 65 6e 63 6f 64 65 20 76 61 72  .  We encode var
30ded 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
30dee 65 67 65 72 73 20 69 6e 20 6c 69 74 74 6c 65 2d  egers in little-
30def 65 6e 64 69 61 6e 20 6f 72 64 65 72 0a 2a 2a 20  endian order.** 
30df0 75 73 69 6e 67 20 73 65 76 65 6e 20 62 69 74 73  using seven bits
30df1 20 2a 20 70 65 72 20 62 79 74 65 20 61 73 20 66   * per byte as f
30df2 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45  ollows:.**.** KE
30df3 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20  Y:.**         A 
30df4 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 20  = 0xxxxxxx    7 
30df5 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
30df6 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
30df7 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78           B = 1xx
30df8 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20  xxxxx    7 bits 
30df9 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20  of data and one 
30dfa 66 6c 61 67 20 62 69 74 0a 2a 2a 0a 2a 2a 20 20  flag bit.**.**  
30dfb 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34  7 bits - A.** 14
30dfc 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31   bits - BA.** 21
30dfd 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20 61   bits - BBA.** a
30dfe 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nd so on..**.** 
30dff 54 68 69 73 20 69 73 20 69 64 65 6e 74 69 63 61  This is identica
30e00 6c 20 74 6f 20 68 6f 77 20 73 71 6c 69 74 65 20  l to how sqlite 
30e01 65 6e 63 6f 64 65 73 20 76 61 72 69 6e 74 73 20  encodes varints 
30e02 28 73 65 65 20 75 74 69 6c 2e 63 29 2e 0a 2a 2a  (see util.c)..**
30e03 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f 63 75 6d 65 6e  .**.**** Documen
30e04 74 20 6c 69 73 74 73 20 2a 2a 2a 2a 0a 2a 2a 20  t lists ****.** 
30e05 41 20 64 6f 63 6c 69 73 74 20 28 64 6f 63 75 6d  A doclist (docum
30e06 65 6e 74 20 6c 69 73 74 29 20 68 6f 6c 64 73 20  ent list) holds 
30e07 61 20 64 6f 63 69 64 2d 73 6f 72 74 65 64 20 6c  a docid-sorted l
30e08 69 73 74 20 6f 66 20 68 69 74 73 20 66 6f 72 20  ist of hits for 
30e09 61 0a 2a 2a 20 67 69 76 65 6e 20 74 65 72 6d 2e  a.** given term.
30e0a 20 20 44 6f 63 6c 69 73 74 73 20 68 6f 6c 64 20    Doclists hold 
30e0b 64 6f 63 69 64 73 2c 20 61 6e 64 20 63 61 6e 20  docids, and can 
30e0c 6f 70 74 69 6f 6e 61 6c 6c 79 20 61 73 73 6f 63  optionally assoc
30e0d 69 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 20 70 6f  iate.** token po
30e0e 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66 66 73  sitions and offs
30e0f 65 74 73 20 77 69 74 68 20 64 6f 63 69 64 73 2e  ets with docids.
30e10 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f 53 49  .**.** A DL_POSI
30e11 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 64 6f  TIONS_OFFSETS do
30e12 63 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20  clist is stored 
30e13 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
30e14 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 76 61   array {.**   va
30e15 72 69 6e 74 20 64 6f 63 69 64 3b 0a 2a 2a 20 20  rint docid;.**  
30e16 20 61 72 72 61 79 20 7b 20 20 20 20 20 20 20 20   array {        
30e17 20 20 20 20 20 20 20 20 28 70 6f 73 69 74 69 6f          (positio
30e18 6e 20 6c 69 73 74 20 66 6f 72 20 63 6f 6c 75 6d  n list for colum
30e19 6e 20 30 29 0a 2a 2a 20 20 20 20 20 76 61 72 69  n 0).**     vari
30e1a 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20 20  nt position;    
30e1b 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65   (delta from pre
30e1c 76 69 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70  vious position p
30e1d 6c 75 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a  lus POS_BASE).**
30e1e 20 20 20 20 20 76 61 72 69 6e 74 20 73 74 61 72       varint star
30e1f 74 4f 66 66 73 65 74 3b 20 20 28 64 65 6c 74 61  tOffset;  (delta
30e20 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73   from previous s
30e21 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20  tartOffset).**  
30e22 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f 66 66     varint endOff
30e23 73 65 74 3b 20 20 20 20 28 64 65 6c 74 61 20 66  set;    (delta f
30e24 72 6f 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29  rom startOffset)
30e25 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 61 72 72  .**   }.**   arr
30e26 61 79 20 7b 0a 2a 2a 20 20 20 20 20 76 61 72 69  ay {.**     vari
30e27 6e 74 20 50 4f 53 5f 43 4f 4c 55 4d 4e 3b 20 20  nt POS_COLUMN;  
30e28 20 28 6d 61 72 6b 73 20 73 74 61 72 74 20 6f 66   (marks start of
30e29 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66   position list f
30e2a 6f 72 20 6e 65 77 20 63 6f 6c 75 6d 6e 29 0a 2a  or new column).*
30e2b 2a 20 20 20 20 20 76 61 72 69 6e 74 20 63 6f 6c  *     varint col
30e2c 75 6d 6e 3b 20 20 20 20 20 20 20 28 69 6e 64 65  umn;       (inde
30e2d 78 20 6f 66 20 6e 65 77 20 63 6f 6c 75 6d 6e 29  x of new column)
30e2e 0a 2a 2a 20 20 20 20 20 61 72 72 61 79 20 7b 0a  .**     array {.
30e2f 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e 74 20  **       varint 
30e30 70 6f 73 69 74 69 6f 6e 3b 20 20 20 28 64 65 6c  position;   (del
30e31 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73  ta from previous
30e32 20 70 6f 73 69 74 69 6f 6e 20 70 6c 75 73 20 50   position plus P
30e33 4f 53 5f 42 41 53 45 29 0a 2a 2a 20 20 20 20 20  OS_BASE).**     
30e34 20 20 76 61 72 69 6e 74 20 73 74 61 72 74 4f 66    varint startOf
30e35 66 73 65 74 3b 28 64 65 6c 74 61 20 66 72 6f 6d  fset;(delta from
30e36 20 70 72 65 76 69 6f 75 73 20 73 74 61 72 74 4f   previous startO
30e37 66 66 73 65 74 29 0a 2a 2a 20 20 20 20 20 20 20  ffset).**       
30e38 76 61 72 69 6e 74 20 65 6e 64 4f 66 66 73 65 74  varint endOffset
30e39 3b 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 73  ;  (delta from s
30e3a 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20  tartOffset).**  
30e3b 20 20 20 7d 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20     }.**   }.**  
30e3c 20 76 61 72 69 6e 74 20 50 4f 53 5f 45 4e 44 3b   varint POS_END;
30e3d 20 20 20 20 20 20 20 20 28 6d 61 72 6b 73 20 65          (marks e
30e3e 6e 64 20 6f 66 20 70 6f 73 69 74 69 6f 6e 73 20  nd of positions 
30e3f 66 6f 72 20 74 68 69 73 20 64 6f 63 75 6d 65 6e  for this documen
30e40 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65  t..** }.**.** He
30e41 72 65 2c 20 61 72 72 61 79 20 7b 20 58 20 7d 20  re, array { X } 
30e42 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f  means zero or mo
30e43 72 65 20 6f 63 63 75 72 72 65 6e 63 65 73 20 6f  re occurrences o
30e44 66 20 58 2c 20 61 64 6a 61 63 65 6e 74 20 69 6e  f X, adjacent in
30e45 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 20 20 41 20 22  .** memory.  A "
30e46 70 6f 73 69 74 69 6f 6e 22 20 69 73 20 61 6e 20  position" is an 
30e47 69 6e 64 65 78 20 6f 66 20 61 20 74 6f 6b 65 6e  index of a token
30e48 20 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 73 74   in the token st
30e49 72 65 61 6d 0a 2a 2a 20 67 65 6e 65 72 61 74 65  ream.** generate
30e4a 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a  d by the tokeniz
30e4b 65 72 2c 20 77 68 69 6c 65 20 61 6e 20 22 6f 66  er, while an "of
30e4c 66 73 65 74 22 20 69 73 20 61 20 62 79 74 65 20  fset" is a byte 
30e4d 6f 66 66 73 65 74 2c 0a 2a 2a 20 62 6f 74 68 20  offset,.** both 
30e4e 62 61 73 65 64 20 61 74 20 30 2e 20 20 4e 6f 74  based at 0.  Not
30e4f 65 20 74 68 61 74 20 50 4f 53 5f 45 4e 44 20 61  e that POS_END a
30e50 6e 64 20 50 4f 53 5f 43 4f 4c 55 4d 4e 20 6f 63  nd POS_COLUMN oc
30e51 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61  cur in the.** sa
30e52 6d 65 20 6c 6f 67 69 63 61 6c 20 70 6c 61 63 65  me logical place
30e53 20 61 73 20 74 68 65 20 70 6f 73 69 74 69 6f 6e   as the position
30e54 20 65 6c 65 6d 65 6e 74 2c 20 61 6e 64 20 61 63   element, and ac
30e55 74 20 61 73 20 73 65 6e 74 69 6e 61 6c 73 0a 2a  t as sentinals.*
30e56 2a 20 65 6e 64 69 6e 67 20 61 20 70 6f 73 69 74  * ending a posit
30e57 69 6f 6e 20 6c 69 73 74 20 61 72 72 61 79 2e 0a  ion list array..
30e58 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f 53 49 54  **.** A DL_POSIT
30e59 49 4f 4e 53 20 64 6f 63 6c 69 73 74 20 6f 6d 69  IONS doclist omi
30e5a 74 73 20 74 68 65 20 73 74 61 72 74 4f 66 66 73  ts the startOffs
30e5b 65 74 20 61 6e 64 20 65 6e 64 4f 66 66 73 65 74  et and endOffset
30e5c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  .** information.
30e5d 20 20 41 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f    A DL_DOCIDS do
30e5e 63 6c 69 73 74 20 6f 6d 69 74 73 20 62 6f 74 68  clist omits both
30e5f 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 61 6e   the position an
30e60 64 0a 2a 2a 20 6f 66 66 73 65 74 20 69 6e 66 6f  d.** offset info
30e61 72 6d 61 74 69 6f 6e 2c 20 62 65 63 6f 6d 69 6e  rmation, becomin
30e62 67 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76 61  g an array of va
30e63 72 69 6e 74 2d 65 6e 63 6f 64 65 64 20 64 6f 63  rint-encoded doc
30e64 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 2d 64 69  ids..**.** On-di
30e65 73 6b 20 64 61 74 61 20 69 73 20 73 74 6f 72 65  sk data is store
30e66 64 20 61 73 20 74 79 70 65 20 44 4c 5f 44 45 46  d as type DL_DEF
30e67 41 55 4c 54 2c 20 73 6f 20 77 65 20 64 6f 6e 27  AULT, so we don'
30e68 74 20 73 65 72 69 61 6c 69 7a 65 0a 2a 2a 20 74  t serialize.** t
30e69 68 65 20 74 79 70 65 2e 20 20 44 75 65 20 74 6f  he type.  Due to
30e6a 20 68 6f 77 20 64 65 6c 65 74 69 6f 6e 20 69 73   how deletion is
30e6b 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20   implemented in 
30e6c 74 68 65 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e  the segmentation
30e6d 0a 2a 2a 20 73 79 73 74 65 6d 2c 20 6f 6e 2d 64  .** system, on-d
30e6e 69 73 6b 20 64 6f 63 6c 69 73 74 73 20 4d 55 53  isk doclists MUS
30e6f 54 20 73 74 6f 72 65 20 61 74 20 6c 65 61 73 74  T store at least
30e70 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a   positions..**.*
30e71 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c  *.**** Segment l
30e72 65 61 66 20 6e 6f 64 65 73 20 2a 2a 2a 2a 0a 2a  eaf nodes ****.*
30e73 2a 20 53 65 67 6d 65 6e 74 20 6c 65 61 66 20 6e  * Segment leaf n
30e74 6f 64 65 73 20 73 74 6f 72 65 20 74 65 72 6d 73  odes store terms
30e75 20 61 6e 64 20 64 6f 63 6c 69 73 74 73 2c 20 6f   and doclists, o
30e76 72 64 65 72 65 64 20 62 79 20 74 65 72 6d 2e 20  rdered by term. 
30e77 20 4c 65 61 66 0a 2a 2a 20 6e 6f 64 65 73 20 61   Leaf.** nodes a
30e78 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67  re written using
30e79 20 4c 65 61 66 57 72 69 74 65 72 2c 20 61 6e 64   LeafWriter, and
30e7a 20 72 65 61 64 20 75 73 69 6e 67 20 4c 65 61 66   read using Leaf
30e7b 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a 20 69 74  Reader (to.** it
30e7c 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 20  erate through a 
30e7d 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e 6f 64 65  single leaf node
30e7e 27 73 20 64 61 74 61 29 20 61 6e 64 20 4c 65 61  's data) and Lea
30e7f 76 65 73 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a  vesReader (to.**
30e80 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
30e81 20 61 20 73 65 67 6d 65 6e 74 27 73 20 65 6e 74   a segment's ent
30e82 69 72 65 20 6c 65 61 66 20 6c 61 79 65 72 29 2e  ire leaf layer).
30e83 20 20 4c 65 61 66 20 6e 6f 64 65 73 20 68 61 76    Leaf nodes hav
30e84 65 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 61 74 3a  e.** the format:
30e85 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69 48  .**.** varint iH
30e86 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  eight;          
30e87 20 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d 20     (height from 
30e88 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77 61  leaf level, alwa
30e89 79 73 20 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20  ys 0).** varint 
30e8a 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
30e8b 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
30e8c 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 63  first term).** c
30e8d 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d  har pTerm[nTerm]
30e8e 3b 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e 74  ;          (cont
30e8f 65 6e 74 20 6f 66 20 66 69 72 73 74 20 74 65 72  ent of first ter
30e90 6d 29 0a 2a 2a 20 76 61 72 69 6e 74 20 6e 44 6f  m).** varint nDo
30e91 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  clist;          
30e92 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72    (length of ter
30e93 6d 27 73 20 61 73 73 6f 63 69 61 74 65 64 20 64  m's associated d
30e94 6f 63 6c 69 73 74 29 0a 2a 2a 20 63 68 61 72 20  oclist).** char 
30e95 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73  pDoclist[nDoclis
30e96 74 5d 3b 20 20 20 20 28 63 6f 6e 74 65 6e 74 20  t];    (content 
30e97 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 61  of doclist).** a
30e98 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20  rray {.**       
30e99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e9a 20 20 20 20 20 20 28 66 75 72 74 68 65 72 20 74        (further t
30e9b 65 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d 65  erms are delta-e
30e9c 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20 76 61 72  ncoded).**   var
30e9d 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20 20  int nPrefix;    
30e9e 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f         (length o
30e9f 66 20 70 72 65 66 69 78 20 73 68 61 72 65 64 20  f prefix shared 
30ea0 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 74 65  with previous te
30ea1 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20  rm).**   varint 
30ea2 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20  nSuffix;        
30ea3 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e     (length of un
30ea4 73 68 61 72 65 64 20 73 75 66 66 69 78 29 0a 2a  shared suffix).*
30ea5 2a 20 20 20 63 68 61 72 20 70 54 65 72 6d 53 75  *   char pTermSu
30ea6 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 28 75  ffix[nSuffix];(u
30ea7 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 20 6f  nshared suffix o
30ea8 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a 2a 20  f next term).** 
30ea9 20 20 76 61 72 69 6e 74 20 6e 44 6f 63 6c 69 73    varint nDoclis
30eaa 74 3b 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e  t;          (len
30eab 67 74 68 20 6f 66 20 74 65 72 6d 27 73 20 61 73  gth of term's as
30eac 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74  sociated doclist
30ead 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 44 6f 63  ).**   char pDoc
30eae 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b 20  list[nDoclist]; 
30eaf 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20 64 6f 63   (content of doc
30eb0 6c 69 73 74 29 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  list).** }.**.**
30eb1 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58   Here, array { X
30eb2 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72   } means zero or
30eb3 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
30eb4 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
30eb5 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a 2a   in.** memory..*
30eb6 2a 0a 2a 2a 20 4c 65 61 66 20 6e 6f 64 65 73 20  *.** Leaf nodes 
30eb7 61 72 65 20 62 72 6f 6b 65 6e 20 69 6e 74 6f 20  are broken into 
30eb8 62 6c 6f 63 6b 73 20 77 68 69 63 68 20 61 72 65  blocks which are
30eb9 20 73 74 6f 72 65 64 20 63 6f 6e 74 69 67 75 6f   stored contiguo
30eba 75 73 6c 79 20 69 6e 0a 2a 2a 20 74 68 65 20 25  usly in.** the %
30ebb 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65 20  _segments table 
30ebc 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e  in sorted order.
30ebd 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61    This means tha
30ebe 74 20 77 68 65 6e 20 74 68 65 20 65 6e 64 0a 2a  t when the end.*
30ebf 2a 20 6f 66 20 61 20 6e 6f 64 65 20 69 73 20 72  * of a node is r
30ec0 65 61 63 68 65 64 2c 20 74 68 65 20 6e 65 78 74  eached, the next
30ec1 20 74 65 72 6d 20 69 73 20 69 6e 20 74 68 65 20   term is in the 
30ec2 6e 6f 64 65 20 77 69 74 68 20 74 68 65 20 6e 65  node with the ne
30ec3 78 74 0a 2a 2a 20 67 72 65 61 74 65 72 20 6e 6f  xt.** greater no
30ec4 64 65 20 69 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  de id..**.** New
30ec5 20 64 61 74 61 20 69 73 20 73 70 69 6c 6c 65 64   data is spilled
30ec6 20 74 6f 20 61 20 6e 65 77 20 6c 65 61 66 20 6e   to a new leaf n
30ec7 6f 64 65 20 77 68 65 6e 20 74 68 65 20 63 75 72  ode when the cur
30ec8 72 65 6e 74 20 6e 6f 64 65 0a 2a 2a 20 65 78 63  rent node.** exc
30ec9 65 65 64 73 20 4c 45 41 46 5f 4d 41 58 20 62 79  eeds LEAF_MAX by
30eca 74 65 73 20 28 64 65 66 61 75 6c 74 20 32 30 34  tes (default 204
30ecb 38 29 2e 20 20 4e 65 77 20 64 61 74 61 20 77 68  8).  New data wh
30ecc 69 63 68 20 69 74 73 65 6c 66 20 69 73 0a 2a 2a  ich itself is.**
30ecd 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 54 41   larger than STA
30ece 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28 64 65 66  NDALONE_MIN (def
30ecf 61 75 6c 74 20 31 30 32 34 29 20 69 73 20 70 6c  ault 1024) is pl
30ed0 61 63 65 64 20 69 6e 20 61 20 73 74 61 6e 64 61  aced in a standa
30ed1 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64 65 20 28 61 20  lone.** node (a 
30ed2 6c 65 61 66 20 6e 6f 64 65 20 77 69 74 68 20 61  leaf node with a
30ed3 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 61 6e 64   single term and
30ed4 20 64 6f 63 6c 69 73 74 29 2e 20 20 54 68 65 20   doclist).  The 
30ed5 67 6f 61 6c 20 6f 66 0a 2a 2a 20 74 68 65 73 65  goal of.** these
30ed6 20 73 65 74 74 69 6e 67 73 20 69 73 20 74 6f 20   settings is to 
30ed7 70 61 63 6b 20 74 6f 67 65 74 68 65 72 20 67 72  pack together gr
30ed8 6f 75 70 73 20 6f 66 20 73 6d 61 6c 6c 20 64 6f  oups of small do
30ed9 63 6c 69 73 74 73 20 77 68 69 6c 65 0a 2a 2a 20  clists while.** 
30eda 6d 61 6b 69 6e 67 20 69 74 20 65 66 66 69 63 69  making it effici
30edb 65 6e 74 20 74 6f 20 64 69 72 65 63 74 6c 79 20  ent to directly 
30edc 61 63 63 65 73 73 20 6c 61 72 67 65 20 64 6f 63  access large doc
30edd 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a 2a 20 61  lists.  The.** a
30ede 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 74 68 61  ssumption is tha
30edf 74 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 73  t large doclists
30ee0 20 72 65 70 72 65 73 65 6e 74 20 74 65 72 6d 73   represent terms
30ee1 20 77 68 69 63 68 20 61 72 65 20 6d 6f 72 65 0a   which are more.
30ee2 2a 2a 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  ** likely to be 
30ee3 71 75 65 72 79 20 74 61 72 67 65 74 73 2e 0a 2a  query targets..*
30ee4 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  *.** TODO(shess)
30ee5 20 49 74 20 6d 61 79 20 62 65 20 75 73 65 66 75   It may be usefu
30ee6 6c 20 66 6f 72 20 62 6c 6f 63 6b 69 6e 67 20 64  l for blocking d
30ee7 65 63 69 73 69 6f 6e 73 20 74 6f 20 62 65 20 6d  ecisions to be m
30ee8 6f 72 65 0a 2a 2a 20 64 79 6e 61 6d 69 63 2e 20  ore.** dynamic. 
30ee9 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20 69   For instance, i
30eea 74 20 6d 61 79 20 6d 61 6b 65 20 6d 6f 72 65 20  t may make more 
30eeb 73 65 6e 73 65 20 74 6f 20 68 61 76 65 20 61 20  sense to have a 
30eec 32 2e 35 6b 20 6c 65 61 66 0a 2a 2a 20 6e 6f 64  2.5k leaf.** nod
30eed 65 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 70  e rather than sp
30eee 6c 69 74 74 69 6e 67 20 69 6e 74 6f 20 32 6b 20  litting into 2k 
30eef 61 6e 64 20 2e 35 6b 20 6e 6f 64 65 73 2e 20 20  and .5k nodes.  
30ef0 4d 79 20 69 6e 74 75 69 74 69 6f 6e 20 69 73 0a  My intuition is.
30ef1 2a 2a 20 74 68 61 74 20 74 68 69 73 20 6d 69 67  ** that this mig
30ef2 68 74 20 65 78 74 65 6e 64 20 74 68 72 6f 75 67  ht extend throug
30ef3 68 20 32 78 20 6f 72 20 34 78 20 74 68 65 20 70  h 2x or 4x the p
30ef4 61 67 65 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 0a 2a  agesize..**.**.*
30ef5 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65  *** Segment inte
30ef6 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a 2a 2a 0a  rior nodes ****.
30ef7 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65 72  ** Segment inter
30ef8 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f 72 65 20  ior nodes store 
30ef9 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20 73 75 62  blockids for sub
30efa 74 72 65 65 20 6e 6f 64 65 73 20 61 6e 64 20 74  tree nodes and t
30efb 65 72 6d 73 0a 2a 2a 20 74 6f 20 64 65 73 63 72  erms.** to descr
30efc 69 62 65 20 77 68 61 74 20 64 61 74 61 20 69 73  ibe what data is
30efd 20 73 74 6f 72 65 64 20 62 79 20 74 68 65 20 65   stored by the e
30efe 61 63 68 20 73 75 62 74 72 65 65 2e 20 20 49 6e  ach subtree.  In
30eff 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73 20  terior.** nodes 
30f00 61 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e  are written usin
30f01 67 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72  g InteriorWriter
30f02 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67  , and read using
30f03 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 52 65 61 64  .** InteriorRead
30f04 65 72 2e 20 20 49 6e 74 65 72 69 6f 72 57 72 69  er.  InteriorWri
30f05 74 65 72 73 20 61 72 65 20 63 72 65 61 74 65 64  ters are created
30f06 20 61 73 20 6e 65 65 64 65 64 20 77 68 65 6e 0a   as needed when.
30f07 2a 2a 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72  ** SegmentWriter
30f08 20 63 72 65 61 74 65 73 20 6e 65 77 20 6c 65 61   creates new lea
30f09 66 20 6e 6f 64 65 73 2c 20 6f 72 20 77 68 65 6e  f nodes, or when
30f0a 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   an interior nod
30f0b 65 0a 2a 2a 20 69 74 73 65 6c 66 20 67 72 6f 77  e.** itself grow
30f0c 73 20 74 6f 6f 20 62 69 67 20 61 6e 64 20 6d 75  s too big and mu
30f0d 73 74 20 62 65 20 73 70 6c 69 74 2e 20 20 54 68  st be split.  Th
30f0e 65 20 66 6f 72 6d 61 74 20 6f 66 20 69 6e 74 65  e format of inte
30f0f 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73 3a 0a 2a  rior.** nodes:.*
30f10 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69 48 65 69  *.** varint iHei
30f11 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 28  ght;           (
30f12 68 65 69 67 68 74 20 66 72 6f 6d 20 6c 65 61 66  height from leaf
30f13 20 6c 65 76 65 6c 2c 20 61 6c 77 61 79 73 20 3e   level, always >
30f14 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20 69 42 6c  0).** varint iBl
30f15 6f 63 6b 69 64 3b 20 20 20 20 20 20 20 20 20 20  ockid;          
30f16 28 62 6c 6f 63 6b 20 69 64 20 6f 66 20 6e 6f 64  (block id of nod
30f17 65 27 73 20 6c 65 66 74 6d 6f 73 74 20 73 75 62  e's leftmost sub
30f18 74 72 65 65 29 0a 2a 2a 20 6f 70 74 69 6f 6e 61  tree).** optiona
30f19 6c 20 7b 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20  l {.**   varint 
30f1a 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
30f1b 20 28 6c 65 6e 67 74 68 20 6f 66 20 66 69 72 73   (length of firs
30f1c 74 20 74 65 72 6d 29 0a 2a 2a 20 20 20 63 68 61  t term).**   cha
30f1d 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 3b 20  r pTerm[nTerm]; 
30f1e 20 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66       (content of
30f1f 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
30f20 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20    array {.**    
30f21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30f22 20 20 20 20 20 20 20 20 20 20 20 20 28 66 75 72              (fur
30f23 74 68 65 72 20 74 65 72 6d 73 20 61 72 65 20 64  ther terms are d
30f24 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a  elta-encoded).**
30f25 20 20 20 20 20 76 61 72 69 6e 74 20 6e 50 72 65       varint nPre
30f26 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
30f27 28 6c 65 6e 67 74 68 20 6f 66 20 73 68 61 72 65  (length of share
30f28 64 20 70 72 65 66 69 78 20 77 69 74 68 20 70 72  d prefix with pr
30f29 65 76 69 6f 75 73 20 74 65 72 6d 29 0a 2a 2a 20  evious term).** 
30f2a 20 20 20 20 76 61 72 69 6e 74 20 6e 53 75 66 66      varint nSuff
30f2b 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 28  ix;            (
30f2c 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73 68 61 72  length of unshar
30f2d 65 64 20 73 75 66 66 69 78 29 0a 2a 2a 20 20 20  ed suffix).**   
30f2e 20 20 63 68 61 72 20 70 54 65 72 6d 53 75 66 66    char pTermSuff
30f2f 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20 28 75 6e  ix[nSuffix]; (un
30f30 73 68 61 72 65 64 20 73 75 66 66 69 78 20 6f 66  shared suffix of
30f31 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a 2a 20 20   next term).**  
30f32 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65   }.** }.**.** He
30f33 72 65 2c 20 6f 70 74 69 6f 6e 61 6c 20 7b 20 58  re, optional { X
30f34 20 7d 20 6d 65 61 6e 73 20 61 6e 20 6f 70 74 69   } means an opti
30f35 6f 6e 61 6c 20 65 6c 65 6d 65 6e 74 2c 20 77 68  onal element, wh
30f36 69 6c 65 20 61 72 72 61 79 20 7b 20 58 20 7d 0a  ile array { X }.
30f37 2a 2a 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72  ** means zero or
30f38 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
30f39 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
30f3a 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a   in memory..**.*
30f3b 2a 20 41 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  * An interior no
30f3c 64 65 20 65 6e 63 6f 64 65 73 20 6e 20 74 65 72  de encodes n ter
30f3d 6d 73 20 73 65 70 61 72 61 74 69 6e 67 20 6e 2b  ms separating n+
30f3e 31 20 73 75 62 74 72 65 65 73 2e 20 20 54 68 65  1 subtrees.  The
30f3f 0a 2a 2a 20 73 75 62 74 72 65 65 20 62 6c 6f 63  .** subtree bloc
30f40 6b 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75  ks are contiguou
30f41 73 2c 20 73 6f 20 6f 6e 6c 79 20 74 68 65 20 66  s, so only the f
30f42 69 72 73 74 20 73 75 62 74 72 65 65 27 73 20 62  irst subtree's b
30f43 6c 6f 63 6b 69 64 0a 2a 2a 20 69 73 20 65 6e 63  lockid.** is enc
30f44 6f 64 65 64 2e 20 20 54 68 65 20 73 75 62 74 72  oded.  The subtr
30f45 65 65 20 61 74 20 69 42 6c 6f 63 6b 69 64 20 77  ee at iBlockid w
30f46 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 6c 6c 20  ill contain all 
30f47 74 65 72 6d 73 20 6c 65 73 73 0a 2a 2a 20 74 68  terms less.** th
30f48 61 6e 20 74 68 65 20 66 69 72 73 74 20 74 65 72  an the first ter
30f49 6d 20 65 6e 63 6f 64 65 64 20 28 6f 72 20 61 6c  m encoded (or al
30f4a 6c 20 74 65 72 6d 73 20 69 66 20 6e 6f 20 74 65  l terms if no te
30f4b 72 6d 20 69 73 20 65 6e 63 6f 64 65 64 29 2e 0a  rm is encoded)..
30f4c 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f  ** Otherwise, fo
30f4d 72 20 74 65 72 6d 73 20 67 72 65 61 74 65 72 20  r terms greater 
30f4e 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
30f4f 20 70 54 65 72 6d 5b 69 5d 20 62 75 74 20 6c 65   pTerm[i] but le
30f50 73 73 0a 2a 2a 20 74 68 61 6e 20 70 54 65 72 6d  ss.** than pTerm
30f51 5b 69 2b 31 5d 2c 20 74 68 65 20 73 75 62 74 72  [i+1], the subtr
30f52 65 65 20 66 6f 72 20 74 68 61 74 20 74 65 72 6d  ee for that term
30f53 20 77 69 6c 6c 20 62 65 20 72 6f 6f 74 65 64 20   will be rooted 
30f54 61 74 0a 2a 2a 20 69 42 6c 6f 63 6b 69 64 2b 69  at.** iBlockid+i
30f55 2e 20 20 49 6e 74 65 72 69 6f 72 20 6e 6f 64 65  .  Interior node
30f56 73 20 6f 6e 6c 79 20 73 74 6f 72 65 20 65 6e 6f  s only store eno
30f57 75 67 68 20 74 65 72 6d 20 64 61 74 61 20 74 6f  ugh term data to
30f58 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69 73 68 20  .** distinguish 
30f59 61 64 6a 61 63 65 6e 74 20 63 68 69 6c 64 72 65  adjacent childre
30f5a 6e 20 28 69 66 20 74 68 65 20 72 69 67 68 74 6d  n (if the rightm
30f5b 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ost term of the 
30f5c 6c 65 66 74 0a 2a 2a 20 63 68 69 6c 64 20 69 73  left.** child is
30f5d 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c 20 61 6e   "something", an
30f5e 64 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 20 74  d the leftmost t
30f5f 65 72 6d 20 6f 66 20 74 68 65 20 72 69 67 68 74  erm of the right
30f60 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20 22 77 69   child is.** "wi
30f61 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22 77 22 20  cked", only "w" 
30f62 69 73 20 73 74 6f 72 65 64 29 2e 0a 2a 2a 0a 2a  is stored)..**.*
30f63 2a 20 4e 65 77 20 64 61 74 61 20 69 73 20 73 70  * New data is sp
30f64 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 69  illed to a new i
30f65 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 61 74 20  nterior node at 
30f66 74 68 65 20 73 61 6d 65 20 68 65 69 67 68 74 20  the same height 
30f67 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72  when.** the curr
30f68 65 6e 74 20 6e 6f 64 65 20 65 78 63 65 65 64 73  ent node exceeds
30f69 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 62 79   INTERIOR_MAX by
30f6a 74 65 73 20 28 64 65 66 61 75 6c 74 20 32 30 34  tes (default 204
30f6b 38 29 2e 0a 2a 2a 20 49 4e 54 45 52 49 4f 52 5f  8)..** INTERIOR_
30f6c 4d 49 4e 5f 54 45 52 4d 53 20 28 64 65 66 61 75  MIN_TERMS (defau
30f6d 6c 74 20 37 29 20 6b 65 65 70 73 20 6c 61 72 67  lt 7) keeps larg
30f6e 65 20 74 65 72 6d 73 20 66 72 6f 6d 20 6d 6f 6e  e terms from mon
30f6f 6f 70 6f 6c 69 7a 69 6e 67 0a 2a 2a 20 69 6e 74  opolizing.** int
30f70 65 72 69 6f 72 20 6e 6f 64 65 73 20 61 6e 64 20  erior nodes and 
30f71 6d 61 6b 69 6e 67 20 74 68 65 20 74 72 65 65 20  making the tree 
30f72 74 6f 6f 20 73 6b 69 6e 6e 79 2e 20 20 54 68 65  too skinny.  The
30f73 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a   interior nodes.
30f74 2a 2a 20 61 74 20 61 20 67 69 76 65 6e 20 68 65  ** at a given he
30f75 69 67 68 74 20 61 72 65 20 6e 61 74 75 72 61 6c  ight are natural
30f76 6c 79 20 74 72 61 63 6b 65 64 20 62 79 20 69 6e  ly tracked by in
30f77 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61 74 0a  terior nodes at.
30f78 2a 2a 20 68 65 69 67 68 74 2b 31 2c 20 61 6e 64  ** height+1, and
30f79 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a   so on..**.**.**
30f7a 2a 2a 20 53 65 67 6d 65 6e 74 20 64 69 72 65 63  ** Segment direc
30f7b 74 6f 72 79 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  tory ****.** The
30f7c 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
30f7d 72 79 20 69 6e 20 74 61 62 6c 65 20 25 5f 73 65  ry in table %_se
30f7e 67 64 69 72 20 73 74 6f 72 65 73 20 6d 65 74 61  gdir stores meta
30f7f 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72  -information for
30f80 0a 2a 2a 20 6d 65 72 67 69 6e 67 20 61 6e 64 20  .** merging and 
30f81 64 65 6c 65 74 69 6e 67 20 73 65 67 6d 65 6e 74  deleting segment
30f82 73 2c 20 61 6e 64 20 61 6c 73 6f 20 74 68 65 20  s, and also the 
30f83 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 74 68 65  root node of the
30f84 0a 2a 2a 20 73 65 67 6d 65 6e 74 27 73 20 74 72  .** segment's tr
30f85 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f  ee..**.** The ro
30f86 6f 74 20 6e 6f 64 65 20 69 73 20 74 68 65 20 74  ot node is the t
30f87 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 73  op node of the s
30f88 65 67 6d 65 6e 74 27 73 20 74 72 65 65 20 61 66  egment's tree af
30f89 74 65 72 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  ter encoding.** 
30f8a 74 68 65 20 65 6e 74 69 72 65 20 73 65 67 6d 65  the entire segme
30f8b 6e 74 2c 20 72 65 73 74 72 69 63 74 65 64 20 74  nt, restricted t
30f8c 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74 65 73  o ROOT_MAX bytes
30f8d 20 28 64 65 66 61 75 6c 74 20 31 30 32 34 29 2e   (default 1024).
30f8e 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 62  .** This could b
30f8f 65 20 65 69 74 68 65 72 20 61 20 6c 65 61 66 20  e either a leaf 
30f90 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e 74 65 72  node or an inter
30f91 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66 20 74 68  ior node.  If th
30f92 65 20 74 6f 70 0a 2a 2a 20 6e 6f 64 65 20 72 65  e top.** node re
30f93 71 75 69 72 65 73 20 6d 6f 72 65 20 74 68 61 6e  quires more than
30f94 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74 65 73 2c   ROOT_MAX bytes,
30f95 20 69 74 20 69 73 20 66 6c 75 73 68 65 64 20 74   it is flushed t
30f96 6f 20 25 5f 73 65 67 6d 65 6e 74 73 0a 2a 2a 20  o %_segments.** 
30f97 61 6e 64 20 61 20 6e 65 77 20 72 6f 6f 74 20 69  and a new root i
30f98 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 69 73 20  nterior node is 
30f99 67 65 6e 65 72 61 74 65 64 20 28 77 68 69 63 68  generated (which
30f9a 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 66   should always f
30f9b 69 74 0a 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f  it.** within ROO
30f9c 54 5f 4d 41 58 20 62 65 63 61 75 73 65 20 69 74  T_MAX because it
30f9d 20 6f 6e 6c 79 20 6e 65 65 64 73 20 73 70 61 63   only needs spac
30f9e 65 20 66 6f 72 20 32 20 76 61 72 69 6e 74 73 2c  e for 2 varints,
30f9f 20 74 68 65 0a 2a 2a 20 68 65 69 67 68 74 20 61   the.** height a
30fa0 6e 64 20 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f  nd the blockid o
30fa1 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 72  f the previous r
30fa2 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oot)..**.** The 
30fa3 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta-information
30fa4 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 20   in the segment 
30fa5 64 69 72 65 63 74 6f 72 79 20 69 73 3a 0a 2a 2a  directory is:.**
30fa6 20 20 20 6c 65 76 65 6c 20 20 20 20 20 20 20 20     level        
30fa7 20 20 20 20 20 20 20 2d 20 73 65 67 6d 65 6e 74         - segment
30fa8 20 6c 65 76 65 6c 20 28 73 65 65 20 62 65 6c 6f   level (see belo
30fa9 77 29 0a 2a 2a 20 20 20 69 64 78 20 20 20 20 20  w).**   idx     
30faa 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 69 6e              - in
30fab 64 65 78 20 77 69 74 68 69 6e 20 6c 65 76 65 6c  dex within level
30fac 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
30fad 20 20 20 20 20 20 20 20 20 20 2d 20 28 6c 65 76            - (lev
30fae 65 6c 2c 69 64 78 20 75 6e 69 71 75 65 6c 79 20  el,idx uniquely 
30faf 69 64 65 6e 74 69 66 79 20 61 20 73 65 67 6d 65  identify a segme
30fb0 6e 74 29 0a 2a 2a 20 20 20 73 74 61 72 74 5f 62  nt).**   start_b
30fb1 6c 6f 63 6b 20 20 20 20 20 20 20 20 20 2d 20 66  lock         - f
30fb2 69 72 73 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a  irst leaf node.*
30fb3 2a 20 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62  *   leaves_end_b
30fb4 6c 6f 63 6b 20 20 20 20 2d 20 6c 61 73 74 20 6c  lock    - last l
30fb5 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20 20 65 6e  eaf node.**   en
30fb6 64 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20  d_block         
30fb7 20 20 2d 20 6c 61 73 74 20 62 6c 6f 63 6b 20 28    - last block (
30fb8 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 69  including interi
30fb9 6f 72 20 6e 6f 64 65 73 29 0a 2a 2a 20 20 20 72  or nodes).**   r
30fba 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  oot             
30fbb 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73 20 6f 66     - contents of
30fbc 20 72 6f 6f 74 20 6e 6f 64 65 0a 2a 2a 0a 2a 2a   root node.**.**
30fbd 20 49 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64   If the root nod
30fbe 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65  e is a leaf node
30fbf 2c 20 74 68 65 6e 20 73 74 61 72 74 5f 62 6c 6f  , then start_blo
30fc0 63 6b 2c 0a 2a 2a 20 6c 65 61 76 65 73 5f 65 6e  ck,.** leaves_en
30fc1 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20 65 6e 64  d_block, and end
30fc2 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c 6c 20 30  _block are all 0
30fc3 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67  ..**.**.**** Seg
30fc4 6d 65 6e 74 20 6d 65 72 67 69 6e 67 20 2a 2a 2a  ment merging ***
30fc5 2a 0a 2a 2a 20 54 6f 20 61 6d 6f 72 74 69 7a 65  *.** To amortize
30fc6 20 75 70 64 61 74 65 20 63 6f 73 74 73 2c 20 73   update costs, s
30fc7 65 67 6d 65 6e 74 73 20 61 72 65 20 67 72 6f 75  egments are grou
30fc8 70 65 64 20 69 6e 74 6f 20 6c 65 76 65 6c 73 20  ped into levels 
30fc9 61 6e 64 0a 2a 2a 20 6d 65 72 67 65 64 20 69 6e  and.** merged in
30fca 20 62 61 74 63 68 65 73 2e 20 20 45 61 63 68 20   batches.  Each 
30fcb 69 6e 63 72 65 61 73 65 20 69 6e 20 6c 65 76 65  increase in leve
30fcc 6c 20 72 65 70 72 65 73 65 6e 74 73 20 65 78 70  l represents exp
30fcd 6f 6e 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20 6d 6f  onentially.** mo
30fce 72 65 20 64 6f 63 75 6d 65 6e 74 73 2e 0a 2a 2a  re documents..**
30fcf 0a 2a 2a 20 4e 65 77 20 64 6f 63 75 6d 65 6e 74  .** New document
30fd0 73 20 28 61 63 74 75 61 6c 6c 79 2c 20 64 6f 63  s (actually, doc
30fd1 75 6d 65 6e 74 20 75 70 64 61 74 65 73 29 20 61  ument updates) a
30fd2 72 65 20 74 6f 6b 65 6e 69 7a 65 64 20 61 6e 64  re tokenized and
30fd3 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e 64 69  .** written indi
30fd4 76 69 64 75 61 6c 6c 79 20 28 75 73 69 6e 67 20  vidually (using 
30fd5 4c 65 61 66 57 72 69 74 65 72 29 20 74 6f 20 61  LeafWriter) to a
30fd6 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74   level 0 segment
30fd7 2c 20 77 69 74 68 0a 2a 2a 20 69 6e 63 72 65 6d  , with.** increm
30fd8 65 6e 74 69 6e 67 20 69 64 78 2e 20 20 57 68 65  enting idx.  Whe
30fd9 6e 20 69 64 78 20 72 65 61 63 68 65 73 20 4d 45  n idx reaches ME
30fda 52 47 45 5f 43 4f 55 4e 54 20 28 64 65 66 61 75  RGE_COUNT (defau
30fdb 6c 74 20 31 36 29 2c 20 61 6c 6c 0a 2a 2a 20 6c  lt 16), all.** l
30fdc 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 73 20  evel 0 segments 
30fdd 61 72 65 20 6d 65 72 67 65 64 20 69 6e 74 6f 20  are merged into 
30fde 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 31  a single level 1
30fdf 20 73 65 67 6d 65 6e 74 2e 20 20 4c 65 76 65 6c   segment.  Level
30fe0 20 31 0a 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74   1.** is populat
30fe1 65 64 20 6c 69 6b 65 20 6c 65 76 65 6c 20 30 2c  ed like level 0,
30fe2 20 61 6e 64 20 65 76 65 6e 74 75 61 6c 6c 79 20   and eventually 
30fe3 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6c 65 76 65  MERGE_COUNT leve
30fe4 6c 20 31 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20  l 1.** segments 
30fe5 61 72 65 20 6d 65 72 67 65 64 20 74 6f 20 61 20  are merged to a 
30fe6 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 32 20 73  single level 2 s
30fe7 65 67 6d 65 6e 74 20 28 72 65 70 72 65 73 65 6e  egment (represen
30fe8 74 69 6e 67 0a 2a 2a 20 4d 45 52 47 45 5f 43 4f  ting.** MERGE_CO
30fe9 55 4e 54 5e 32 20 75 70 64 61 74 65 73 29 2c 20  UNT^2 updates), 
30fea 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
30feb 20 41 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65   A segment merge
30fec 20 74 72 61 76 65 72 73 65 73 20 61 6c 6c 20 73   traverses all s
30fed 65 67 6d 65 6e 74 73 20 61 74 20 61 20 67 69 76  egments at a giv
30fee 65 6e 20 6c 65 76 65 6c 20 69 6e 0a 2a 2a 20 70  en level in.** p
30fef 61 72 61 6c 6c 65 6c 2c 20 70 65 72 66 6f 72 6d  arallel, perform
30ff0 69 6e 67 20 61 20 73 74 72 61 69 67 68 74 66 6f  ing a straightfo
30ff1 72 77 61 72 64 20 73 6f 72 74 65 64 20 6d 65 72  rward sorted mer
30ff2 67 65 2e 20 20 53 69 6e 63 65 20 73 65 67 6d 65  ge.  Since segme
30ff3 6e 74 0a 2a 2a 20 6c 65 61 66 20 6e 6f 64 65 73  nt.** leaf nodes
30ff4 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 20   are written in 
30ff5 74 6f 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74  to the %_segment
30ff6 73 20 74 61 62 6c 65 20 69 6e 20 6f 72 64 65 72  s table in order
30ff7 2c 20 74 68 69 73 0a 2a 2a 20 6d 65 72 67 65 20  , this.** merge 
30ff8 74 72 61 76 65 72 73 65 73 20 74 68 65 20 75 6e  traverses the un
30ff9 64 65 72 6c 79 69 6e 67 20 73 71 6c 69 74 65 20  derlying sqlite 
30ffa 64 69 73 6b 20 73 74 72 75 63 74 75 72 65 73 20  disk structures 
30ffb 65 66 66 69 63 69 65 6e 74 6c 79 2e 0a 2a 2a 20  efficiently..** 
30ffc 41 66 74 65 72 20 74 68 65 20 6d 65 72 67 65 2c  After the merge,
30ffd 20 61 6c 6c 20 73 65 67 6d 65 6e 74 20 62 6c 6f   all segment blo
30ffe 63 6b 73 20 66 72 6f 6d 20 74 68 65 20 6d 65 72  cks from the mer
30fff 67 65 64 20 6c 65 76 65 6c 20 61 72 65 0a 2a 2a  ged level are.**
31000 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   deleted..**.** 
31001 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63 6f 6e 74  MERGE_COUNT cont
31002 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e 20 77  rols how often w
31003 65 20 6d 65 72 67 65 20 73 65 67 6d 65 6e 74 73  e merge segments
31004 2e 20 20 31 36 20 73 65 65 6d 73 20 74 6f 20 62  .  16 seems to b
31005 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74 20 6f 66  e.** somewhat of
31006 20 61 20 73 77 65 65 74 20 73 70 6f 74 20 66 6f   a sweet spot fo
31007 72 20 69 6e 73 65 72 74 69 6f 6e 20 70 65 72 66  r insertion perf
31008 6f 72 6d 61 6e 63 65 2e 20 20 33 32 20 61 6e 64  ormance.  32 and
31009 20 36 34 20 73 68 6f 77 0a 2a 2a 20 76 65 72 79   64 show.** very
3100a 20 73 69 6d 69 6c 61 72 20 70 65 72 66 6f 72 6d   similar perform
3100b 61 6e 63 65 20 6e 75 6d 62 65 72 73 20 74 6f 20  ance numbers to 
3100c 31 36 20 6f 6e 20 69 6e 73 65 72 74 69 6f 6e 2c  16 on insertion,
3100d 20 74 68 6f 75 67 68 20 74 68 65 79 27 72 65 0a   though they're.
3100e 2a 2a 20 61 20 74 69 6e 79 20 62 69 74 20 73 6c  ** a tiny bit sl
3100f 6f 77 65 72 20 28 70 65 72 68 61 70 73 20 64 75  ower (perhaps du
31010 65 20 74 6f 20 6d 6f 72 65 20 6f 76 65 72 68 65  e to more overhe
31011 61 64 20 69 6e 20 6d 65 72 67 65 2d 74 69 6d 65  ad in merge-time
31012 0a 2a 2a 20 73 6f 72 74 69 6e 67 29 2e 20 20 38  .** sorting).  8
31013 20 69 73 20 61 62 6f 75 74 20 32 30 25 20 73 6c   is about 20% sl
31014 6f 77 65 72 20 74 68 61 6e 20 31 36 2c 20 34 20  ower than 16, 4 
31015 61 62 6f 75 74 20 35 30 25 20 73 6c 6f 77 65 72  about 50% slower
31016 20 74 68 61 6e 0a 2a 2a 20 31 36 2c 20 32 20 61   than.** 16, 2 a
31017 62 6f 75 74 20 36 36 25 20 73 6c 6f 77 65 72 20  bout 66% slower 
31018 74 68 61 6e 20 31 36 2e 0a 2a 2a 0a 2a 2a 20 41  than 16..**.** A
31019 74 20 71 75 65 72 79 20 74 69 6d 65 2c 20 68 69  t query time, hi
3101a 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 69  gh MERGE_COUNT i
3101b 6e 63 72 65 61 73 65 73 20 74 68 65 20 6e 75 6d  ncreases the num
3101c 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73 0a  ber of segments.
3101d 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20 74 6f  ** which need to
3101e 20 62 65 20 73 63 61 6e 6e 65 64 20 61 6e 64 20   be scanned and 
3101f 6d 65 72 67 65 64 2e 20 20 46 6f 72 20 69 6e 73  merged.  For ins
31020 74 61 6e 63 65 2c 20 77 69 74 68 20 31 30 30 6b  tance, with 100k
31021 20 64 6f 63 73 0a 2a 2a 20 69 6e 73 65 72 74 65   docs.** inserte
31022 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 45 52 47  d:.**.**    MERG
31023 45 5f 43 4f 55 4e 54 20 20 20 73 65 67 6d 65 6e  E_COUNT   segmen
31024 74 73 0a 2a 2a 20 20 20 20 20 20 20 31 36 20 20  ts.**       16  
31025 20 20 20 20 20 20 20 20 20 32 35 0a 2a 2a 20 20           25.**  
31026 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20        8         
31027 20 20 31 32 0a 2a 2a 20 20 20 20 20 20 20 20 34    12.**        4
31028 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 2a 2a             10.**
31029 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
3102a 20 20 20 20 20 36 0a 2a 2a 0a 2a 2a 20 54 68 69       6.**.** Thi
3102b 73 20 61 70 70 65 61 72 73 20 74 6f 20 68 61 76  s appears to hav
3102c 65 20 6f 6e 6c 79 20 61 20 6d 6f 64 65 72 61 74  e only a moderat
3102d 65 20 69 6d 70 61 63 74 20 6f 6e 20 71 75 65 72  e impact on quer
3102e 69 65 73 20 66 6f 72 20 76 65 72 79 0a 2a 2a 20  ies for very.** 
3102f 66 72 65 71 75 65 6e 74 20 74 65 72 6d 73 20 28  frequent terms (
31030 77 68 69 63 68 20 61 72 65 20 73 6f 6d 65 77 68  which are somewh
31031 61 74 20 64 6f 6d 69 6e 61 74 65 64 20 62 79 20  at dominated by 
31032 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 0a 2a 2a  segment merge.**
31033 20 63 6f 73 74 73 29 2c 20 61 6e 64 20 69 6e 66   costs), and inf
31034 72 65 71 75 65 6e 74 20 61 6e 64 20 6e 6f 6e 2d  requent and non-
31035 65 78 69 73 74 65 6e 74 20 74 65 72 6d 73 20 73  existent terms s
31036 74 69 6c 6c 20 73 65 65 6d 20 74 6f 20 62 65 20  till seem to be 
31037 66 61 73 74 0a 2a 2a 20 65 76 65 6e 20 77 69 74  fast.** even wit
31038 68 20 6d 61 6e 79 20 73 65 67 6d 65 6e 74 73 2e  h many segments.
31039 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73  .**.** TODO(shes
3103a 73 29 20 54 68 61 74 20 73 61 69 64 2c 20 69 74  s) That said, it
3103b 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65 20 74   would be nice t
3103c 6f 20 68 61 76 65 20 61 20 62 65 74 74 65 72 20  o have a better 
3103d 71 75 65 72 79 2d 73 69 64 65 0a 2a 2a 20 61 72  query-side.** ar
3103e 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45 52 47 45  gument for MERGE
3103f 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e 20 20 41  _COUNT of 16.  A
31040 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f 73 73 69  lso, it is possi
31041 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68 61 74 0a  ble/likely that.
31042 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  ** optimizations
31043 20 74 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20   to things like 
31044 64 6f 63 6c 69 73 74 20 6d 65 72 67 69 6e 67 20  doclist merging 
31045 77 69 6c 6c 20 73 77 69 6e 67 20 74 68 65 20 73  will swing the s
31046 77 65 65 74 0a 2a 2a 20 73 70 6f 74 20 61 72 6f  weet.** spot aro
31047 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a  und..**.**.**.**
31048 2a 2a 20 48 61 6e 64 6c 69 6e 67 20 6f 66 20 64  ** Handling of d
31049 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 75 70 64  eletions and upd
3104a 61 74 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 69 6e  ates ****.** Sin
3104b 63 65 20 77 65 27 72 65 20 75 73 69 6e 67 20 61  ce we're using a
3104c 20 73 65 67 6d 65 6e 74 65 64 20 73 74 72 75 63   segmented struc
3104d 74 75 72 65 2c 20 77 69 74 68 20 6e 6f 20 64 6f  ture, with no do
3104e 63 69 64 2d 6f 72 69 65 6e 74 65 64 0a 2a 2a 20  cid-oriented.** 
3104f 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 74  index into the t
31050 65 72 6d 20 69 6e 64 65 78 2c 20 77 65 20 63 6c  erm index, we cl
31051 65 61 72 6c 79 20 63 61 6e 6e 6f 74 20 73 69 6d  early cannot sim
31052 70 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 74  ply update the t
31053 65 72 6d 0a 2a 2a 20 69 6e 64 65 78 20 77 68 65  erm.** index whe
31054 6e 20 61 20 64 6f 63 75 6d 65 6e 74 20 69 73 20  n a document is 
31055 64 65 6c 65 74 65 64 20 6f 72 20 75 70 64 61 74  deleted or updat
31056 65 64 2e 20 20 46 6f 72 20 64 65 6c 65 74 69 6f  ed.  For deletio
31057 6e 73 2c 20 77 65 0a 2a 2a 20 77 72 69 74 65 20  ns, we.** write 
31058 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74  an empty doclist
31059 20 28 76 61 72 69 6e 74 28 64 6f 63 69 64 29 20   (varint(docid) 
3105a 76 61 72 69 6e 74 28 50 4f 53 5f 45 4e 44 29 29  varint(POS_END))
3105b 2c 20 66 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a  , for updates.**
3105c 20 77 65 20 73 69 6d 70 6c 79 20 77 72 69 74 65   we simply write
3105d 20 74 68 65 20 6e 65 77 20 64 6f 63 6c 69 73 74   the new doclist
3105e 2e 20 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 65  .  Segment merge
3105f 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c 64 65  s overwrite olde
31060 72 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 61 20  r.** data for a 
31061 70 61 72 74 69 63 75 6c 61 72 20 64 6f 63 69 64  particular docid
31062 20 77 69 74 68 20 6e 65 77 65 72 20 64 61 74 61   with newer data
31063 2c 20 73 6f 20 64 65 6c 65 74 65 73 20 6f 72 20  , so deletes or 
31064 75 70 64 61 74 65 73 0a 2a 2a 20 77 69 6c 6c 20  updates.** will 
31065 65 76 65 6e 74 75 61 6c 6c 79 20 6f 76 65 72 74  eventually overt
31066 61 6b 65 20 74 68 65 20 65 61 72 6c 69 65 72 20  ake the earlier 
31067 64 61 74 61 20 61 6e 64 20 6b 6e 6f 63 6b 20 69  data and knock i
31068 74 20 6f 75 74 2e 20 20 54 68 65 0a 2a 2a 20 71  t out.  The.** q
31069 75 65 72 79 20 6c 6f 67 69 63 20 6c 69 6b 65 77  uery logic likew
3106a 69 73 65 20 6d 65 72 67 65 73 20 64 6f 63 6c 69  ise merges docli
3106b 73 74 73 20 73 6f 20 74 68 61 74 20 6e 65 77 65  sts so that newe
3106c 72 20 64 61 74 61 20 6b 6e 6f 63 6b 73 20 6f 75  r data knocks ou
3106d 74 0a 2a 2a 20 6f 6c 64 65 72 20 64 61 74 61 2e  t.** older data.
3106e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73  .**.** TODO(shes
3106f 73 29 20 50 72 6f 76 69 64 65 20 61 20 56 41 43  s) Provide a VAC
31070 55 55 4d 20 74 79 70 65 20 6f 70 65 72 61 74 69  UUM type operati
31071 6f 6e 20 74 6f 20 63 6c 65 61 72 20 6f 75 74 20  on to clear out 
31072 61 6c 6c 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 73  all.** deletions
31073 20 61 6e 64 20 64 75 70 6c 69 63 61 74 69 6f 6e   and duplication
31074 73 2e 20 20 54 68 69 73 20 77 6f 75 6c 64 20 62  s.  This would b
31075 61 73 69 63 61 6c 6c 79 20 62 65 20 61 20 66 6f  asically be a fo
31076 72 63 65 64 20 6d 65 72 67 65 0a 2a 2a 20 69 6e  rced merge.** in
31077 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65 67 6d  to a single segm
31078 65 6e 74 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64 65  ent..*/..#if !de
31079 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
3107a 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
3107b 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
3107c 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  )..#if defined(S
3107d 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
3107e 33 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  3) && !defined(S
3107f 51 4c 49 54 45 5f 43 4f 52 45 29 0a 23 20 64 65  QLITE_CORE).# de
31080 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  fine SQLITE_CORE
31081 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 2a 2a   1.#endif.../***
31082 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
31083 75 64 65 20 66 74 73 33 5f 65 78 70 72 2e 68 20  ude fts3_expr.h 
31084 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
31085 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.c ********
31086 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
31087 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
31088 6e 20 66 69 6c 65 20 66 74 73 33 5f 65 78 70 72  n file fts3_expr
31089 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
3108a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3108b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
3108c 2a 20 32 30 30 38 20 4e 6f 76 20 32 38 0a 2a 2a  * 2008 Nov 28.**
3108d 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
3108e 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
3108f 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
31090 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
31091 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
31092 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
31093 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
31094 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
31095 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
31096 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
31097 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
31098 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
31099 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
3109a 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
3109b 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
3109c 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
3109d 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
3109e 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
3109f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310a3 2a 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  *.**.*/../******
310a4 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
310a5 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
310a6 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
310a7 6f 66 20 66 74 73 33 5f 65 78 70 72 2e 68 20 2a  of fts3_expr.h *
310a8 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
310a9 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
310aa 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ile fts3_tokeniz
310ab 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
310ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310ad 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
310ae 30 30 36 20 4a 75 6c 79 20 31 30 0a 2a 2a 0a 2a  006 July 10.**.*
310af 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
310b0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
310b1 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
310b2 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  code..**.*******
310b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
310b7 2a 2a 0a 2a 2a 20 44 65 66 69 6e 65 73 20 74 68  **.** Defines th
310b8 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
310b9 6f 6b 65 6e 69 7a 65 72 73 20 75 73 65 64 20 62  okenizers used b
310ba 79 20 66 75 6c 6c 74 65 78 74 2d 73 65 61 72 63  y fulltext-searc
310bb 68 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65  h.  There.** are
310bc 20 74 68 72 65 65 20 62 61 73 69 63 20 63 6f 6d   three basic com
310bd 70 6f 6e 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 73  ponents:.**.** s
310be 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
310bf 5f 6d 6f 64 75 6c 65 20 69 73 20 61 20 73 69 6e  _module is a sin
310c0 67 6c 65 74 6f 6e 20 64 65 66 69 6e 69 6e 67 20  gleton defining 
310c1 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  the tokenizer.**
310c2 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 74   interface funct
310c3 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 73 20 65  ions.  This is e
310c4 73 73 65 6e 74 69 61 6c 6c 79 20 74 68 65 20 63  ssentially the c
310c5 6c 61 73 73 20 73 74 72 75 63 74 75 72 65 20 66  lass structure f
310c6 6f 72 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 73  or.** tokenizers
310c7 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ..**.** sqlite3_
310c8 74 6f 6b 65 6e 69 7a 65 72 20 69 73 20 75 73 65  tokenizer is use
310c9 64 20 74 6f 20 64 65 66 69 6e 65 20 61 20 70 61  d to define a pa
310ca 72 74 69 63 75 6c 61 72 20 74 6f 6b 65 6e 69 7a  rticular tokeniz
310cb 65 72 2c 20 70 65 72 68 61 70 73 0a 2a 2a 20 69  er, perhaps.** i
310cc 6e 63 6c 75 64 69 6e 67 20 63 75 73 74 6f 6d 69  ncluding customi
310cd 7a 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  zation informati
310ce 6f 6e 20 64 65 66 69 6e 65 64 20 61 74 20 63 72  on defined at cr
310cf 65 61 74 69 6f 6e 20 74 69 6d 65 2e 0a 2a 2a 0a  eation time..**.
310d0 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
310d1 69 7a 65 72 5f 63 75 72 73 6f 72 20 69 73 20 67  izer_cursor is g
310d2 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 74 6f  enerated by a to
310d3 6b 65 6e 69 7a 65 72 20 74 6f 20 67 65 6e 65 72  kenizer to gener
310d4 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 73 20 66 72  ate.** tokens fr
310d5 6f 6d 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  om a particular 
310d6 69 6e 70 75 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65  input..*/.#ifnde
310d7 66 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  f _FTS3_TOKENIZE
310d8 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 46 54  R_H_.#define _FT
310d9 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 0a  S3_TOKENIZER_H_.
310da 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
310db 4f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 53 51  Only used for SQ
310dc 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 53 51 4c 49  LITE_OK and SQLI
310dd 54 45 5f 44 4f 4e 45 20 61 74 20 74 68 69 73 20  TE_DONE at this 
310de 74 69 6d 65 2e 0a 2a 2a 20 49 66 20 74 6f 6b 65  time..** If toke
310df 6e 69 7a 65 72 73 20 61 72 65 20 74 6f 20 62 65  nizers are to be
310e0 20 61 6c 6c 6f 77 65 64 20 74 6f 20 63 61 6c 6c   allowed to call
310e1 20 73 71 6c 69 74 65 33 5f 2a 28 29 20 66 75 6e   sqlite3_*() fun
310e2 63 74 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20  ctions, then.** 
310e3 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 61 20 77  we will need a w
310e4 61 79 20 74 6f 20 72 65 67 69 73 74 65 72 20 74  ay to register t
310e5 68 65 20 41 50 49 20 63 6f 6e 73 69 73 74 65 6e  he API consisten
310e6 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  tly..*/../*.** S
310e7 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62  tructures used b
310e8 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  y the tokenizer 
310e9 69 6e 74 65 72 66 61 63 65 2e 20 57 68 65 6e 20  interface. When 
310ea 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 0a  a new tokenizer.
310eb 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
310ec 6e 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2c  n is registered,
310ed 20 74 68 65 20 63 61 6c 6c 65 72 20 70 72 6f 76   the caller prov
310ee 69 64 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ides a pointer t
310ef 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69 74 65 33 5f  o.** an sqlite3_
310f0 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
310f1 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 6f 69 6e   containing poin
310f2 74 65 72 73 20 74 6f 20 74 68 65 20 63 61 6c 6c  ters to the call
310f3 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  back.** function
310f4 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 61  s that make up a
310f5 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
310f6 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20  ..**.** When an 
310f7 66 74 73 33 20 74 61 62 6c 65 20 69 73 20 63 72  fts3 table is cr
310f8 65 61 74 65 64 2c 20 69 74 20 70 61 73 73 65 73  eated, it passes
310f9 20 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20 70   any arguments p
310fa 61 73 73 65 64 20 74 6f 0a 2a 2a 20 74 68 65 20  assed to.** the 
310fb 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75 73 65  tokenizer clause
310fc 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56   of the CREATE V
310fd 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
310fe 74 65 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a  tement to the.**
310ff 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
31100 65 72 5f 6d 6f 64 75 6c 65 2e 78 43 72 65 61 74  er_module.xCreat
31101 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20  e() function of 
31102 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 6f  the requested to
31103 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65  kenizer.** imple
31104 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65 20 78  mentation. The x
31105 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f  Create() functio
31106 6e 20 69 6e 20 74 75 72 6e 20 72 65 74 75 72 6e  n in turn return
31107 73 20 61 6e 20 0a 2a 2a 20 73 71 6c 69 74 65 33  s an .** sqlite3
31108 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72 75 63  _tokenizer struc
31109 74 75 72 65 20 72 65 70 72 65 73 65 6e 74 69 6e  ture representin
3110a 67 20 74 68 65 20 73 70 65 63 69 66 69 63 20 74  g the specific t
3110b 6f 6b 65 6e 69 7a 65 72 20 74 6f 0a 2a 2a 20 62  okenizer to.** b
3110c 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 66  e used for the f
3110d 74 73 33 20 74 61 62 6c 65 20 28 63 75 73 74 6f  ts3 table (custo
3110e 6d 69 7a 65 64 20 62 79 20 74 68 65 20 74 6f 6b  mized by the tok
3110f 65 6e 69 7a 65 72 20 63 6c 61 75 73 65 20 61 72  enizer clause ar
31110 67 75 6d 65 6e 74 73 29 2e 0a 2a 2a 0a 2a 2a 20  guments)..**.** 
31111 54 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69  To tokenize an i
31112 6e 70 75 74 20 62 75 66 66 65 72 2c 20 74 68 65  nput buffer, the
31113 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
31114 65 72 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 28  er_module.xOpen(
31115 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73 20 63  ).** method is c
31116 61 6c 6c 65 64 2e 20 49 74 20 72 65 74 75 72 6e  alled. It return
31117 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 74 6f 6b  s an sqlite3_tok
31118 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 6f 62  enizer_cursor ob
31119 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 6d 61 79  ject.** that may
3111a 20 62 65 20 75 73 65 64 20 74 6f 20 74 6f 6b 65   be used to toke
3111b 6e 69 7a 65 20 61 20 73 70 65 63 69 66 69 63 20  nize a specific 
3111c 69 6e 70 75 74 20 62 75 66 66 65 72 20 62 61 73  input buffer bas
3111d 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 74 6f 6b  ed on.** the tok
3111e 65 6e 69 7a 61 74 69 6f 6e 20 72 75 6c 65 73 20  enization rules 
3111f 73 75 70 70 6c 69 65 64 20 62 79 20 61 20 73 70  supplied by a sp
31120 65 63 69 66 69 63 20 73 71 6c 69 74 65 33 5f 74  ecific sqlite3_t
31121 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 6f 62 6a 65  okenizer.** obje
31122 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ct..*/.typedef s
31123 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f  truct sqlite3_to
31124 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73  kenizer_module s
31125 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
31126 5f 6d 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66  _module;.typedef
31127 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
31128 74 6f 6b 65 6e 69 7a 65 72 20 73 71 6c 69 74 65  tokenizer sqlite
31129 33 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79 70  3_tokenizer;.typ
3112a 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
3112b 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
3112c 72 73 6f 72 20 73 71 6c 69 74 65 33 5f 74 6f 6b  rsor sqlite3_tok
3112d 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a  enizer_cursor;..
3112e 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74  struct sqlite3_t
3112f 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
31130 7b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 74 72  {..  /*.  ** Str
31131 75 63 74 75 72 65 20 76 65 72 73 69 6f 6e 2e 20  ucture version. 
31132 53 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  Should always be
31133 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a   set to 0..  */.
31134 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a    int iVersion;.
31135 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72 65 61 74  .  /*.  ** Creat
31136 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65  e a new tokenize
31137 72 2e 20 54 68 65 20 76 61 6c 75 65 73 20 69 6e  r. The values in
31138 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72 72 61   the argv[] arra
31139 79 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20 61  y are the.  ** a
3113a 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20  rguments passed 
3113b 74 6f 20 74 68 65 20 22 74 6f 6b 65 6e 69 7a 65  to the "tokenize
3113c 72 22 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  r" clause of the
3113d 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 0a   CREATE VIRTUAL.
3113e 20 20 2a 2a 20 54 41 42 4c 45 20 73 74 61 74 65    ** TABLE state
3113f 6d 65 6e 74 20 74 68 61 74 20 63 72 65 61 74 65  ment that create
31140 64 20 74 68 65 20 66 74 73 33 20 74 61 62 6c 65  d the fts3 table
31141 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
31142 66 0a 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f  f.  ** the follo
31143 77 69 6e 67 20 53 51 4c 20 69 73 20 65 78 65 63  wing SQL is exec
31144 75 74 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uted:.  **.  ** 
31145 20 20 43 52 45 41 54 45 20 2e 2e 20 55 53 49 4e    CREATE .. USIN
31146 47 20 66 74 73 33 28 20 2e 2e 2e 20 2c 20 74 6f  G fts3( ... , to
31147 6b 65 6e 69 7a 65 72 20 3c 74 6f 6b 65 6e 69 7a  kenizer <tokeniz
31148 65 72 2d 6e 61 6d 65 3e 20 61 72 67 31 20 61 72  er-name> arg1 ar
31149 67 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 74 68  g2).  **.  ** th
3114a 65 6e 20 61 72 67 63 20 69 73 20 73 65 74 20 74  en argc is set t
3114b 6f 20 32 2c 20 61 6e 64 20 74 68 65 20 61 72 67  o 2, and the arg
3114c 76 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61 69  v[] array contai
3114d 6e 73 20 70 6f 69 6e 74 65 72 73 0a 20 20 2a 2a  ns pointers.  **
3114e 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 73 20   to the strings 
3114f 22 61 72 67 31 22 20 61 6e 64 20 22 61 72 67 32  "arg1" and "arg2
31150 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  "..  **.  ** Thi
31151 73 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  s method should 
31152 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51  return either SQ
31153 4c 49 54 45 5f 4f 4b 20 28 30 29 2c 20 6f 72 20  LITE_OK (0), or 
31154 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
31155 0a 20 20 2a 2a 20 63 6f 64 65 2e 20 49 66 20 53  .  ** code. If S
31156 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
31157 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70 54 6f  rned, then *ppTo
31158 6b 65 6e 69 7a 65 72 20 73 68 6f 75 6c 64 20 62  kenizer should b
31159 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20 70 6f  e set.  ** to po
3115a 69 6e 74 20 61 74 20 74 68 65 20 6e 65 77 6c 79  int at the newly
3115b 20 63 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a   created tokeniz
3115c 65 72 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  er structure. Th
3115d 65 20 67 65 6e 65 72 69 63 0a 20 20 2a 2a 20 73  e generic.  ** s
3115e 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3115f 2e 70 4d 6f 64 75 6c 65 20 76 61 72 69 61 62 6c  .pModule variabl
31160 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  e should not be 
31161 69 6e 69 74 69 61 6c 69 73 65 64 20 62 79 0a 20  initialised by. 
31162 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 62 61 63   ** this callbac
31163 6b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69  k. The caller wi
31164 6c 6c 20 64 6f 20 73 6f 2e 0a 20 20 2a 2f 0a 20  ll do so..  */. 
31165 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28   int (*xCreate)(
31166 0a 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 20  .    int argc,  
31167 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31168 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
31169 20 6f 66 20 61 72 67 76 20 61 72 72 61 79 20 2a   of argv array *
3116a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  /.    const char
3116b 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20   *const*argv,   
3116c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
3116d 65 6e 69 7a 65 72 20 61 72 67 75 6d 65 6e 74 20  enizer argument 
3116e 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20 73  strings */.    s
3116f 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
31170 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 20   **ppTokenizer  
31171 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74     /* OUT: Creat
31172 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a  ed tokenizer */.
31173 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20    );..  /*.  ** 
31174 44 65 73 74 72 6f 79 20 61 6e 20 65 78 69 73 74  Destroy an exist
31175 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 54  ing tokenizer. T
31176 68 65 20 66 74 73 33 20 6d 6f 64 75 6c 65 20 63  he fts3 module c
31177 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64  alls this method
31178 0a 20 20 2a 2a 20 65 78 61 63 74 6c 79 20 6f 6e  .  ** exactly on
31179 63 65 20 66 6f 72 20 65 61 63 68 20 73 75 63 63  ce for each succ
3117a 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 78  essful call to x
3117b 43 72 65 61 74 65 28 29 2e 0a 20 20 2a 2f 0a 20  Create()..  */. 
3117c 20 69 6e 74 20 28 2a 78 44 65 73 74 72 6f 79 29   int (*xDestroy)
3117d 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
3117e 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 3b  er *pTokenizer);
3117f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72 65 61  ..  /*.  ** Crea
31180 74 65 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 63  te a tokenizer c
31181 75 72 73 6f 72 20 74 6f 20 74 6f 6b 65 6e 69 7a  ursor to tokeniz
31182 65 20 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65  e an input buffe
31183 72 2e 20 54 68 65 20 63 61 6c 6c 65 72 0a 20 20  r. The caller.  
31184 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ** is responsibl
31185 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74  e for ensuring t
31186 68 61 74 20 74 68 65 20 69 6e 70 75 74 20 62 75  hat the input bu
31187 66 66 65 72 20 72 65 6d 61 69 6e 73 20 76 61 6c  ffer remains val
31188 69 64 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 74 68  id.  ** until th
31189 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73  e cursor is clos
3118a 65 64 20 28 75 73 69 6e 67 20 74 68 65 20 78 43  ed (using the xC
3118b 6c 6f 73 65 28 29 20 6d 65 74 68 6f 64 29 2e 20  lose() method). 
3118c 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f  .  */.  int (*xO
3118d 70 65 6e 29 28 0a 20 20 20 20 73 71 6c 69 74 65  pen)(.    sqlite
3118e 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
3118f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 2f  kenizer,       /
31190 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6f 62 6a 65  * Tokenizer obje
31191 63 74 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20  ct */.    const 
31192 63 68 61 72 20 2a 70 49 6e 70 75 74 2c 20 69 6e  char *pInput, in
31193 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 2f  t nBytes,      /
31194 2a 20 49 6e 70 75 74 20 62 75 66 66 65 72 20 2a  * Input buffer *
31195 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f  /.    sqlite3_to
31196 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
31197 2a 70 70 43 75 72 73 6f 72 20 20 2f 2a 20 4f 55  *ppCursor  /* OU
31198 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65 6e  T: Created token
31199 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
3119a 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 44   );..  /*.  ** D
3119b 65 73 74 72 6f 79 20 61 6e 20 65 78 69 73 74 69  estroy an existi
3119c 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72  ng tokenizer cur
3119d 73 6f 72 2e 20 54 68 65 20 66 74 73 33 20 6d 6f  sor. The fts3 mo
3119e 64 75 6c 65 20 63 61 6c 6c 73 20 74 68 69 73 20  dule calls this 
3119f 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 65 78 61  .  ** method exa
311a0 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20 65 61  ctly once for ea
311a1 63 68 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  ch successful ca
311a2 6c 6c 20 74 6f 20 78 4f 70 65 6e 28 29 2e 0a 20  ll to xOpen().. 
311a3 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f   */.  int (*xClo
311a4 73 65 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  se)(sqlite3_toke
311a5 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
311a6 75 72 73 6f 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20  ursor);..  /*.  
311a7 2a 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20  ** Retrieve the 
311a8 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
311a9 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75  the tokenizer cu
311aa 72 73 6f 72 20 70 43 75 72 73 6f 72 2e 20 54 68  rsor pCursor. Th
311ab 69 73 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 73  is.  ** method s
311ac 68 6f 75 6c 64 20 65 69 74 68 65 72 20 72 65 74  hould either ret
311ad 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e  urn SQLITE_OK an
311ae 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 73  d set the values
311af 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 22 4f 55   of the.  ** "OU
311b0 54 22 20 76 61 72 69 61 62 6c 65 73 20 69 64 65  T" variables ide
311b1 6e 74 69 66 69 65 64 20 62 65 6c 6f 77 2c 20 6f  ntified below, o
311b2 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 74 6f  r SQLITE_DONE to
311b3 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a 20   indicate that. 
311b4 20 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74   ** the end of t
311b5 68 65 20 62 75 66 66 65 72 20 68 61 73 20 62 65  he buffer has be
311b6 65 6e 20 72 65 61 63 68 65 64 2c 20 6f 72 20 61  en reached, or a
311b7 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
311b8 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2a  ode..  **.  ** *
311b9 70 70 54 6f 6b 65 6e 20 73 68 6f 75 6c 64 20 62  ppToken should b
311ba 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61  e set to point a
311bb 74 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  t a buffer conta
311bc 69 6e 69 6e 67 20 74 68 65 20 0a 20 20 2a 2a 20  ining the .  ** 
311bd 6e 6f 72 6d 61 6c 69 7a 65 64 20 76 65 72 73 69  normalized versi
311be 6f 6e 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  on of the token 
311bf 28 69 2e 65 2e 20 61 66 74 65 72 20 61 6e 79 20  (i.e. after any 
311c0 63 61 73 65 2d 66 6f 6c 64 69 6e 67 20 61 6e 64  case-folding and
311c1 2f 6f 72 0a 20 20 2a 2a 20 73 74 65 6d 6d 69 6e  /or.  ** stemmin
311c2 67 20 68 61 73 20 62 65 65 6e 20 70 65 72 66 6f  g has been perfo
311c3 72 6d 65 64 29 2e 20 2a 70 6e 42 79 74 65 73 20  rmed). *pnBytes 
311c4 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f  should be set to
311c5 20 74 68 65 20 6c 65 6e 67 74 68 0a 20 20 2a 2a   the length.  **
311c6 20 6f 66 20 74 68 69 73 20 62 75 66 66 65 72 20   of this buffer 
311c7 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20 69 6e  in bytes. The in
311c8 70 75 74 20 74 65 78 74 20 74 68 61 74 20 67 65  put text that ge
311c9 6e 65 72 61 74 65 64 20 74 68 65 20 74 6f 6b 65  nerated the toke
311ca 6e 20 69 73 0a 20 20 2a 2a 20 69 64 65 6e 74 69  n is.  ** identi
311cb 66 69 65 64 20 62 79 20 74 68 65 20 62 79 74 65  fied by the byte
311cc 20 6f 66 66 73 65 74 73 20 72 65 74 75 72 6e 65   offsets returne
311cd 64 20 69 6e 20 2a 70 69 53 74 61 72 74 4f 66 66  d in *piStartOff
311ce 73 65 74 20 61 6e 64 0a 20 20 2a 2a 20 2a 70 69  set and.  ** *pi
311cf 45 6e 64 4f 66 66 73 65 74 2e 20 2a 70 69 53 74  EndOffset. *piSt
311d0 61 72 74 4f 66 66 73 65 74 20 73 68 6f 75 6c 64  artOffset should
311d1 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 69   be set to the i
311d2 6e 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73  ndex of the firs
311d3 74 0a 20 20 2a 2a 20 62 79 74 65 20 6f 66 20 74  t.  ** byte of t
311d4 68 65 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20  he token in the 
311d5 69 6e 70 75 74 20 62 75 66 66 65 72 2e 20 2a 70  input buffer. *p
311d6 69 45 6e 64 4f 66 66 73 65 74 20 73 68 6f 75 6c  iEndOffset shoul
311d7 64 20 62 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f  d be set.  ** to
311d8 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
311d9 65 20 66 69 72 73 74 20 62 79 74 65 20 6a 75 73  e first byte jus
311da 74 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  t past the end o
311db 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 0a 20  f the token in. 
311dc 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 62 75   ** the input bu
311dd 66 66 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ffer..  **.  ** 
311de 54 68 65 20 62 75 66 66 65 72 20 2a 70 70 54 6f  The buffer *ppTo
311df 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 70 6f  ken is set to po
311e0 69 6e 74 20 61 74 20 69 73 20 6d 61 6e 61 67 65  int at is manage
311e1 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a  d by the tokeniz
311e2 65 72 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e  er.  ** implemen
311e3 74 61 74 69 6f 6e 2e 20 49 74 20 69 73 20 6f 6e  tation. It is on
311e4 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 62  ly required to b
311e5 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68  e valid until th
311e6 65 20 6e 65 78 74 20 63 61 6c 6c 0a 20 20 2a 2a  e next call.  **
311e7 20 74 6f 20 78 4e 65 78 74 28 29 20 6f 72 20 78   to xNext() or x
311e8 43 6c 6f 73 65 28 29 2e 20 0a 20 20 2a 2f 0a 20  Close(). .  */. 
311e9 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
311ea 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e  current implemen
311eb 74 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20  tation requires 
311ec 70 49 6e 70 75 74 20 74 6f 20 62 65 0a 20 20 2a  pInput to be.  *
311ed 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  * nul-terminated
311ee 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 65  .  This should e
311ef 69 74 68 65 72 20 62 65 20 66 69 78 65 64 2c 20  ither be fixed, 
311f0 6f 72 20 70 49 6e 70 75 74 2f 6e 42 79 74 65 73  or pInput/nBytes
311f1 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  .  ** should be 
311f2 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 7a 49 6e  converted to zIn
311f3 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  put..  */.  int 
311f4 28 2a 78 4e 65 78 74 29 28 0a 20 20 20 20 73 71  (*xNext)(.    sq
311f5 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
311f6 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
311f7 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
311f8 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 63 6f  cursor */.    co
311f9 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b  nst char **ppTok
311fa 65 6e 2c 20 69 6e 74 20 2a 70 6e 42 79 74 65 73  en, int *pnBytes
311fb 2c 20 20 2f 2a 20 4f 55 54 3a 20 4e 6f 72 6d 61  ,  /* OUT: Norma
311fc 6c 69 7a 65 64 20 74 65 78 74 20 66 6f 72 20 74  lized text for t
311fd 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  oken */.    int 
311fe 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  *piStartOffset, 
311ff 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66   /* OUT: Byte of
31200 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 69 6e  fset of token in
31201 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 2a 2f   input buffer */
31202 0a 20 20 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f  .    int *piEndO
31203 66 66 73 65 74 2c 20 20 20 20 2f 2a 20 4f 55 54  ffset,    /* OUT
31204 3a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66  : Byte offset of
31205 20 65 6e 64 20 6f 66 20 74 6f 6b 65 6e 20 69 6e   end of token in
31206 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 2a 2f   input buffer */
31207 0a 20 20 20 20 69 6e 74 20 2a 70 69 50 6f 73 69  .    int *piPosi
31208 74 69 6f 6e 20 20 20 20 20 20 2f 2a 20 4f 55 54  tion      /* OUT
31209 3a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  : Number of toke
3120a 6e 73 20 72 65 74 75 72 6e 65 64 20 62 65 66 6f  ns returned befo
3120b 72 65 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20  re this one */. 
3120c 20 29 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73   );.};..struct s
3120d 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3120e 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74   {.  const sqlit
3120f 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
31210 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f  ule *pModule;  /
31211 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72  * The module for
31212 20 74 68 69 73 20 74 6f 6b 65 6e 69 7a 65 72 20   this tokenizer 
31213 2a 2f 0a 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65  */.  /* Tokenize
31214 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
31215 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79  s will typically
31216 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
31217 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 73 74  fields */.};..st
31218 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  ruct sqlite3_tok
31219 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a  enizer_cursor {.
3121a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3121b 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b  zer *pTokenizer;
3121c 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69         /* Tokeni
3121d 7a 65 72 20 66 6f 72 20 74 68 69 73 20 63 75 72  zer for this cur
3121e 73 6f 72 2e 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b  sor. */.  /* Tok
3121f 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
31220 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
31221 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
31222 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
31223 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 46 54  ;..#endif /* _FT
31224 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 20  S3_TOKENIZER_H_ 
31225 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
31226 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f  *** End of fts3_
31227 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a  tokenizer.h ****
31228 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31229 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3122a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
3122b 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
3122c 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
3122d 20 69 6e 20 66 74 73 33 5f 65 78 70 72 2e 68 20   in fts3_expr.h 
3122e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3122f 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  **/../*.** The f
31230 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69 62  ollowing describ
31231 65 73 20 74 68 65 20 73 79 6e 74 61 78 20 73 75  es the syntax su
31232 70 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 66  pported by the f
31233 74 73 33 20 4d 41 54 43 48 0a 2a 2a 20 6f 70 65  ts3 MATCH.** ope
31234 72 61 74 6f 72 20 69 6e 20 61 20 73 69 6d 69 6c  rator in a simil
31235 61 72 20 66 6f 72 6d 61 74 20 74 6f 20 74 68 61  ar format to tha
31236 74 20 75 73 65 64 20 62 79 20 74 68 65 20 6c 65  t used by the le
31237 6d 6f 6e 20 70 61 72 73 65 72 0a 2a 2a 20 67 65  mon parser.** ge
31238 6e 65 72 61 74 6f 72 2e 20 54 68 69 73 20 6d 6f  nerator. This mo
31239 64 75 6c 65 20 64 6f 65 73 20 6e 6f 74 20 75 73  dule does not us
3123a 65 20 61 63 74 75 61 6c 6c 79 20 6c 65 6d 6f 6e  e actually lemon
3123b 2c 20 69 74 20 75 73 65 73 20 61 0a 2a 2a 20 63  , it uses a.** c
3123c 75 73 74 6f 6d 20 70 61 72 73 65 72 2e 0a 2a 2a  ustom parser..**
3123d 0a 2a 2a 20 20 20 71 75 65 72 79 20 3a 3a 3d 20  .**   query ::= 
3123e 61 6e 64 65 78 70 72 20 28 4f 52 20 61 6e 64 65  andexpr (OR ande
3123f 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 61  xpr)*..**.**   a
31240 6e 64 65 78 70 72 20 3a 3a 3d 20 6e 6f 74 65 78  ndexpr ::= notex
31241 70 72 20 28 41 4e 44 3f 20 6e 6f 74 65 78 70 72  pr (AND? notexpr
31242 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e 6f 74 65  )*..**.**   note
31243 78 70 72 20 3a 3a 3d 20 6e 65 61 72 65 78 70 72  xpr ::= nearexpr
31244 20 28 4e 4f 54 20 6e 65 61 72 65 78 70 72 7c 2d   (NOT nearexpr|-
31245 54 4f 4b 45 4e 29 2a 2e 0a 2a 2a 20 20 20 6e 6f  TOKEN)*..**   no
31246 74 65 78 70 72 20 3a 3a 3d 20 4c 50 20 71 75 65  texpr ::= LP que
31247 72 79 20 52 50 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e  ry RP..**.**   n
31248 65 61 72 65 78 70 72 20 3a 3a 3d 20 70 68 72 61  earexpr ::= phra
31249 73 65 20 28 4e 45 41 52 20 64 69 73 74 61 6e 63  se (NEAR distanc
3124a 65 5f 6f 70 74 20 6e 65 61 72 65 78 70 72 29 2a  e_opt nearexpr)*
3124b 2e 0a 2a 2a 0a 2a 2a 20 20 20 64 69 73 74 61 6e  ..**.**   distan
3124c 63 65 5f 6f 70 74 20 3a 3a 3d 20 2e 0a 2a 2a 20  ce_opt ::= ..** 
3124d 20 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a    distance_opt :
3124e 3a 3d 20 2f 20 49 4e 54 45 47 45 52 2e 0a 2a 2a  := / INTEGER..**
3124f 0a 2a 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d  .**   phrase ::=
31250 20 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68 72   TOKEN..**   phr
31251 61 73 65 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 3a 54  ase ::= COLUMN:T
31252 4f 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68 72 61 73  OKEN..**   phras
31253 65 20 3a 3a 3d 20 22 54 4f 4b 45 4e 20 54 4f 4b  e ::= "TOKEN TOK
31254 45 4e 20 54 4f 4b 45 4e 2e 2e 2e 22 2e 0a 2a 2f  EN TOKEN..."..*/
31255 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
31256 20 46 74 73 33 45 78 70 72 20 46 74 73 33 45 78   Fts3Expr Fts3Ex
31257 70 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  pr;.typedef stru
31258 63 74 20 46 74 73 33 50 68 72 61 73 65 20 46 74  ct Fts3Phrase Ft
31259 73 33 50 68 72 61 73 65 3b 0a 0a 2f 2a 0a 2a 2a  s3Phrase;../*.**
3125a 20 41 20 22 70 68 72 61 73 65 22 20 69 73 20 61   A "phrase" is a
3125b 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65   sequence of one
3125c 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20   or more tokens 
3125d 74 68 61 74 20 6d 75 73 74 20 6d 61 74 63 68 20  that must match 
3125e 69 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 2e 20  in.** sequence. 
3125f 20 41 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 20   A single token 
31260 69 73 20 74 68 65 20 62 61 73 65 20 63 61 73 65  is the base case
31261 20 61 6e 64 20 74 68 65 20 6d 6f 73 74 20 63 6f   and the most co
31262 6d 6d 6f 6e 20 63 61 73 65 2e 0a 2a 2a 20 46 6f  mmon case..** Fo
31263 72 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  r a sequence of 
31264 74 6f 6b 65 6e 73 20 63 6f 6e 74 61 69 6e 65 64  tokens contained
31265 20 69 6e 20 22 2e 2e 2e 22 2c 20 6e 54 6f 6b 65   in "...", nToke
31266 6e 20 77 69 6c 6c 20 62 65 20 74 68 65 20 6e 75  n will be the nu
31267 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 6f 6b 65 6e  mber.** of token
31268 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2e  s in the string.
31269 0a 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33 50  .*/.struct Fts3P
3126a 68 72 61 73 65 20 7b 0a 20 20 69 6e 74 20 6e 54  hrase {.  int nT
3126b 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  oken;          /
3126c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  * Number of toke
3126d 6e 73 20 69 6e 20 74 68 65 20 70 68 72 61 73 65  ns in the phrase
3126e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d   */.  int iColum
3126f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  n;         /* In
31270 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 74 68  dex of column th
31271 69 73 20 70 68 72 61 73 65 20 6d 75 73 74 20 6d  is phrase must m
31272 61 74 63 68 20 2a 2f 0a 20 20 69 6e 74 20 69 73  atch */.  int is
31273 4e 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Not;           /
31274 2a 20 50 68 72 61 73 65 20 70 72 65 66 69 78 65  * Phrase prefixe
31275 64 20 62 79 20 75 6e 61 72 79 20 6e 6f 74 20 28  d by unary not (
31276 2d 29 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  -) operator */. 
31277 20 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f   struct PhraseTo
31278 6b 65 6e 20 7b 0a 20 20 20 20 63 68 61 72 20 2a  ken {.    char *
31279 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
3127a 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 74  /* Text of the t
3127b 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  oken */.    int 
3127c 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
3127d 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
3127e 79 74 65 73 20 69 6e 20 62 75 66 66 65 72 20 70  ytes in buffer p
3127f 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 20 2a  ointed to by z *
31280 2f 0a 20 20 20 20 69 6e 74 20 69 73 50 72 65 66  /.    int isPref
31281 69 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ix;         /* T
31282 72 75 65 20 69 66 20 74 6f 6b 65 6e 20 65 6e 64  rue if token end
31283 73 20 69 6e 20 77 69 74 68 20 61 20 22 2a 22 20  s in with a "*" 
31284 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 7d  character */.  }
31285 20 61 54 6f 6b 65 6e 5b 31 5d 3b 20 20 20 20 20   aToken[1];     
31286 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79      /* One entry
31287 20 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20   for each token 
31288 69 6e 20 74 68 65 20 70 68 72 61 73 65 20 2a 2f  in the phrase */
31289 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 65  .};../*.** A tre
3128a 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65 63  e of these objec
3128b 74 73 20 66 6f 72 6d 73 20 74 68 65 20 52 48 53  ts forms the RHS
3128c 20 6f 66 20 61 20 4d 41 54 43 48 20 6f 70 65 72   of a MATCH oper
3128d 61 74 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ator..*/.struct 
3128e 46 74 73 33 45 78 70 72 20 7b 0a 20 20 69 6e 74  Fts3Expr {.  int
3128f 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20   eType;         
31290 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
31291 66 20 74 68 65 20 46 54 53 51 55 45 52 59 5f 58  f the FTSQUERY_X
31292 58 58 20 76 61 6c 75 65 73 20 64 65 66 69 6e 65  XX values define
31293 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74  d below */.  int
31294 20 6e 4e 65 61 72 3b 20 20 20 20 20 20 20 20 20   nNear;         
31295 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64          /* Valid
31296 20 69 66 20 65 54 79 70 65 3d 3d 46 54 53 51 55   if eType==FTSQU
31297 45 52 59 5f 4e 45 41 52 20 2a 2f 0a 20 20 46 74  ERY_NEAR */.  Ft
31298 73 33 45 78 70 72 20 2a 70 50 61 72 65 6e 74 3b  s3Expr *pParent;
31299 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 72           /* pPar
3129a 65 6e 74 2d 3e 70 4c 65 66 74 3d 3d 74 68 69 73  ent->pLeft==this
3129b 20 6f 72 20 70 50 61 72 65 6e 74 2d 3e 70 52 69   or pParent->pRi
3129c 67 68 74 3d 3d 74 68 69 73 20 2a 2f 0a 20 20 46  ght==this */.  F
3129d 74 73 33 45 78 70 72 20 2a 70 4c 65 66 74 3b 20  ts3Expr *pLeft; 
3129e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66            /* Lef
3129f 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 46  t operand */.  F
312a0 74 73 33 45 78 70 72 20 2a 70 52 69 67 68 74 3b  ts3Expr *pRight;
312a1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67            /* Rig
312a2 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
312a3 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72  Fts3Phrase *pPhr
312a4 61 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 56 61  ase;       /* Va
312a5 6c 69 64 20 69 66 20 65 54 79 70 65 3d 3d 46 54  lid if eType==FT
312a6 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 2a 2f  SQUERY_PHRASE */
312a7 0a 7d 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  .};..SQLITE_PRIV
312a8 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
312a9 74 73 33 45 78 70 72 50 61 72 73 65 28 73 71 6c  ts3ExprParse(sql
312aa 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
312ab 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74 2c 20  , char **, int, 
312ac 69 6e 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20  int, .          
312ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
312ae 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
312af 2c 20 46 74 73 33 45 78 70 72 20 2a 2a 29 3b 0a  , Fts3Expr **);.
312b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
312b1 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 45  oid sqlite3Fts3E
312b2 78 70 72 46 72 65 65 28 46 74 73 33 45 78 70 72  xprFree(Fts3Expr
312b3 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6e 64   *);../*.** Cand
312b4 69 64 61 74 65 20 76 61 6c 75 65 73 20 66 6f 72  idate values for
312b5 20 46 74 73 33 51 75 65 72 79 2e 65 54 79 70 65   Fts3Query.eType
312b6 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  . Note that the 
312b7 6f 72 64 65 72 20 6f 66 20 74 68 65 20 66 69 72  order of the fir
312b8 73 74 0a 2a 2a 20 66 6f 75 72 20 76 61 6c 75 65  st.** four value
312b9 73 20 69 73 20 69 6e 20 6f 72 64 65 72 20 6f 66  s is in order of
312ba 20 70 72 65 63 65 64 65 6e 63 65 20 77 68 65 6e   precedence when
312bb 20 70 61 72 73 69 6e 67 20 65 78 70 72 65 73 73   parsing express
312bc 69 6f 6e 73 2e 20 46 6f 72 20 0a 2a 2a 20 65 78  ions. For .** ex
312bd 61 6d 70 6c 65 2c 20 74 68 65 20 66 6f 6c 6c 6f  ample, the follo
312be 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61  wing:.**.**   "a
312bf 20 4f 52 20 62 20 41 4e 44 20 63 20 4e 4f 54 20   OR b AND c NOT 
312c0 64 20 4e 45 41 52 20 65 22 0a 2a 2a 0a 2a 2a 20  d NEAR e".**.** 
312c1 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  is equivalent to
312c2 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20 4f 52 20  :.**.**   "a OR 
312c3 28 62 20 41 4e 44 20 28 63 20 4e 4f 54 20 28 64  (b AND (c NOT (d
312c4 20 4e 45 41 52 20 65 29 29 29 22 0a 2a 2f 0a 23   NEAR e)))".*/.#
312c5 64 65 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f  define FTSQUERY_
312c6 4e 45 41 52 20 20 20 31 0a 23 64 65 66 69 6e 65  NEAR   1.#define
312c7 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20 20   FTSQUERY_NOT   
312c8 20 32 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55   2.#define FTSQU
312c9 45 52 59 5f 41 4e 44 20 20 20 20 33 0a 23 64 65  ERY_AND    3.#de
312ca 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4f 52  fine FTSQUERY_OR
312cb 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 46       4.#define F
312cc 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 35  TSQUERY_PHRASE 5
312cd 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
312ce 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
312cf 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
312d0 46 74 73 33 45 78 70 72 49 6e 69 74 54 65 73 74  Fts3ExprInitTest
312d1 49 6e 74 65 72 66 61 63 65 28 73 71 6c 69 74 65  Interface(sqlite
312d2 33 20 2a 64 62 29 3b 0a 23 65 6e 64 69 66 0a 0a  3 *db);.#endif..
312d3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
312d4 45 6e 64 20 6f 66 20 66 74 73 33 5f 65 78 70 72  End of fts3_expr
312d5 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
312d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
312d8 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
312d9 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
312da 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
312db 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.c *********
312dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
312dd 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
312de 49 6e 63 6c 75 64 65 20 66 74 73 33 5f 68 61 73  Include fts3_has
312df 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  h.h in the middl
312e0 65 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  e of fts3.c ****
312e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
312e2 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
312e3 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
312e4 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  hash.h *********
312e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
312e7 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
312e8 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  mber 22.**.** Th
312e9 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
312ea 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
312eb 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
312ec 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
312ed 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
312ee 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
312ef 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
312f0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
312f1 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
312f2 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
312f3 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
312f4 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
312f5 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
312f6 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
312f7 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
312f8 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
312f9 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
312fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
312fe 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
312ff 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c  s the header fil
31300 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69  e for the generi
31301 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70  c hash-table imp
31302 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73  lemenation.** us
31303 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 20 20 57  ed in SQLite.  W
31304 65 27 76 65 20 6d 6f 64 69 66 69 65 64 20 69 74  e've modified it
31305 20 73 6c 69 67 68 74 6c 79 20 74 6f 20 73 65 72   slightly to ser
31306 76 65 20 61 73 20 61 20 73 74 61 6e 64 61 6c 6f  ve as a standalo
31307 6e 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  ne.** hash table
31308 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
31309 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74 65 78  for the full-tex
3130a 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c  t indexing modul
3130b 65 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66  e..**.*/.#ifndef
3130c 20 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 23   _FTS3_HASH_H_.#
3130d 64 65 66 69 6e 65 20 5f 46 54 53 33 5f 48 41 53  define _FTS3_HAS
3130e 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  H_H_../* Forward
3130f 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66   declarations of
31310 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a   structures. */.
31311 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66  typedef struct f
31312 74 73 33 48 61 73 68 20 66 74 73 33 48 61 73 68  ts3Hash fts3Hash
31313 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
31314 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 66 74   fts3HashElem ft
31315 73 33 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20  s3HashElem;../* 
31316 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20  A complete hash 
31317 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74  table is an inst
31318 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
31319 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
3131a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c  .** The internal
3131b 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
3131c 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64  ure are intended
3131d 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d   to be opaque --
3131e 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20   client.** code 
3131f 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d  should not attem
31320 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20  pt to access or 
31321 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c 64  modify the field
31322 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
31323 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e  ure.** directly.
31324 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 74    Change this st
31325 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20  ructure only by 
31326 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e  using the routin
31327 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77  es below..** How
31328 65 76 65 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68  ever, many of th
31329 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 61  e "procedures" a
3132a 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66  nd "functions" f
3132b 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64  or modifying and
3132c 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68  .** accessing th
3132d 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
3132e 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20   really macros, 
3132f 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c  so we can't real
31330 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20  ly make.** this 
31331 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 65  structure opaque
31332 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 66 74 73 33  ..*/.struct fts3
31333 48 61 73 68 20 7b 0a 20 20 63 68 61 72 20 6b 65  Hash {.  char ke
31334 79 43 6c 61 73 73 3b 20 20 20 20 20 20 20 20 20  yClass;         
31335 20 2f 2a 20 48 41 53 48 5f 49 4e 54 2c 20 5f 50   /* HASH_INT, _P
31336 4f 49 4e 54 45 52 2c 20 5f 53 54 52 49 4e 47 2c  OINTER, _STRING,
31337 20 5f 42 49 4e 41 52 59 20 2a 2f 0a 20 20 63 68   _BINARY */.  ch
31338 61 72 20 63 6f 70 79 4b 65 79 3b 20 20 20 20 20  ar copyKey;     
31339 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
3133a 20 63 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61 64   copy of key mad
3133b 65 20 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20  e on insert */. 
3133c 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20   int count;     
3133d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
3133e 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
3133f 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
31340 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66   fts3HashElem *f
31341 69 72 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  irst;    /* The 
31342 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
31343 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20   the array */.  
31344 69 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20  int htsize;     
31345 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
31346 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20  r of buckets in 
31347 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  the hash table *
31348 2f 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33  /.  struct _fts3
31349 68 74 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 74  ht {        /* t
3134a 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f  he hash table */
3134b 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20  .    int count; 
3134c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3134d 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
3134e 65 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73  es with this has
3134f 68 20 2a 2f 0a 20 20 20 20 66 74 73 33 48 61 73  h */.    fts3Has
31350 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20  hElem *chain;   
31351 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
31352 66 69 72 73 74 20 65 6e 74 72 79 20 77 69 74 68  first entry with
31353 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20   this hash */.  
31354 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61  } *ht;.};../* Ea
31355 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68  ch element in th
31356 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
31357 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
31358 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a  he following .**
31359 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c 6c   structure.  All
3135a 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 74   elements are st
3135b 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65  ored on a single
3135c 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c   doubly-linked l
3135d 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e  ist..**.** Again
3135e 2c 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  , this structure
3135f 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
31360 62 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69  be opaque, but i
31361 74 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a  t can't really.*
31362 2a 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 61  * be opaque beca
31363 75 73 65 20 69 74 20 69 73 20 75 73 65 64 20 62  use it is used b
31364 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72  y macros..*/.str
31365 75 63 74 20 66 74 73 33 48 61 73 68 45 6c 65 6d  uct fts3HashElem
31366 20 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65   {.  fts3HashEle
31367 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20  m *next, *prev; 
31368 2f 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76  /* Next and prev
31369 69 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e  ious elements in
3136a 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
3136b 76 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20  void *data;     
3136c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
3136d 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
3136e 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20  th this element 
3136f 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b  */.  void *pKey;
31370 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20   int nKey;      
31371 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65  /* Key associate
31372 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d  d with this elem
31373 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ent */.};../*.**
31374 20 54 68 65 72 65 20 61 72 65 20 32 20 64 69 66   There are 2 dif
31375 66 65 72 65 6e 74 20 6d 6f 64 65 73 20 6f 66 20  ferent modes of 
31376 6f 70 65 72 61 74 69 6f 6e 20 66 6f 72 20 61 20  operation for a 
31377 68 61 73 68 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a  hash table:.**.*
31378 2a 20 20 20 46 54 53 33 5f 48 41 53 48 5f 53 54  *   FTS3_HASH_ST
31379 52 49 4e 47 20 20 20 20 20 20 20 20 70 4b 65 79  RING        pKey
3137a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72   points to a str
3137b 69 6e 67 20 74 68 61 74 20 69 73 20 6e 4b 65 79  ing that is nKey
3137c 20 62 79 74 65 73 20 6c 6f 6e 67 0a 2a 2a 20 20   bytes long.**  
3137d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3137e 20 20 20 20 20 20 20 20 20 28 69 6e 63 6c 75 64           (includ
3137f 69 6e 67 20 74 68 65 20 6e 75 6c 6c 2d 74 65 72  ing the null-ter
31380 6d 69 6e 61 74 6f 72 2c 20 69 66 20 61 6e 79 29  minator, if any)
31381 2e 20 20 43 61 73 65 0a 2a 2a 20 20 20 20 20 20  .  Case.**      
31382 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31383 20 20 20 20 20 69 73 20 72 65 73 70 65 63 74 65       is respecte
31384 64 20 69 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 73  d in comparisons
31385 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48  ..**.**   FTS3_H
31386 41 53 48 5f 42 49 4e 41 52 59 20 20 20 20 20 20  ASH_BINARY      
31387 20 20 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f    pKey points to
31388 20 62 69 6e 61 72 79 20 64 61 74 61 20 6e 4b 65   binary data nKe
31389 79 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 0a 2a  y bytes long. .*
3138a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
3138b 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63              memc
3138c 6d 70 28 29 20 69 73 20 75 73 65 64 20 74 6f 20  mp() is used to 
3138d 63 6f 6d 70 61 72 65 20 6b 65 79 73 2e 0a 2a 2a  compare keys..**
3138e 0a 2a 2a 20 41 20 63 6f 70 79 20 6f 66 20 74 68  .** A copy of th
3138f 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66  e key is made if
31390 20 74 68 65 20 63 6f 70 79 4b 65 79 20 70 61 72   the copyKey par
31391 61 6d 65 74 65 72 20 74 6f 20 66 74 73 33 48 61  ameter to fts3Ha
31392 73 68 49 6e 69 74 20 69 73 20 31 2e 20 20 0a 2a  shInit is 1.  .*
31393 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 48  /.#define FTS3_H
31394 41 53 48 5f 53 54 52 49 4e 47 20 20 20 20 31 0a  ASH_STRING    1.
31395 23 64 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53  #define FTS3_HAS
31396 48 5f 42 49 4e 41 52 59 20 20 20 20 32 0a 0a 2f  H_BINARY    2../
31397 2a 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74  *.** Access rout
31398 69 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65  ines.  To delete
31399 2c 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20  , insert a NULL 
3139a 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
3139b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
3139c 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
3139d 6e 69 74 28 66 74 73 33 48 61 73 68 2a 2c 20 69  nit(fts3Hash*, i
3139e 6e 74 20 6b 65 79 74 79 70 65 2c 20 69 6e 74 20  nt keytype, int 
3139f 63 6f 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45  copyKey);.SQLITE
313a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
313a1 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
313a2 73 65 72 74 28 66 74 73 33 48 61 73 68 2a 2c 20  sert(fts3Hash*, 
313a3 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
313a4 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64  , int nKey, void
313a5 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45   *pData);.SQLITE
313a6 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
313a7 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
313a8 6e 64 28 63 6f 6e 73 74 20 66 74 73 33 48 61 73  nd(const fts3Has
313a9 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  h*, const void *
313aa 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b  pKey, int nKey);
313ab 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
313ac 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
313ad 48 61 73 68 43 6c 65 61 72 28 66 74 73 33 48 61  HashClear(fts3Ha
313ae 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 68 6f  sh*);../*.** Sho
313af 72 74 68 61 6e 64 20 66 6f 72 20 74 68 65 20 66  rthand for the f
313b0 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 0a 2a  unctions above.*
313b1 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
313b2 73 68 49 6e 69 74 20 20 20 73 71 6c 69 74 65 33  shInit   sqlite3
313b3 46 74 73 33 48 61 73 68 49 6e 69 74 0a 23 64 65  Fts3HashInit.#de
313b4 66 69 6e 65 20 66 74 73 33 48 61 73 68 49 6e 73  fine fts3HashIns
313b5 65 72 74 20 73 71 6c 69 74 65 33 46 74 73 33 48  ert sqlite3Fts3H
313b6 61 73 68 49 6e 73 65 72 74 0a 23 64 65 66 69 6e  ashInsert.#defin
313b7 65 20 66 74 73 33 48 61 73 68 46 69 6e 64 20 20  e fts3HashFind  
313b8 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
313b9 46 69 6e 64 0a 23 64 65 66 69 6e 65 20 66 74 73  Find.#define fts
313ba 33 48 61 73 68 43 6c 65 61 72 20 20 73 71 6c 69  3HashClear  sqli
313bb 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72  te3Fts3HashClear
313bc 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
313bd 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
313be 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  all elements of 
313bf 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  a hash table.  T
313c0 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c  he idiom is.** l
313c1 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
313c2 20 20 66 74 73 33 48 61 73 68 20 68 3b 0a 2a 2a    fts3Hash h;.**
313c3 20 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20     fts3HashElem 
313c4 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20  *p;.**   ....** 
313c5 20 20 66 6f 72 28 70 3d 66 74 73 33 48 61 73 68    for(p=fts3Hash
313c6 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d  First(&h); p; p=
313c7 66 74 73 33 48 61 73 68 4e 65 78 74 28 70 29 29  fts3HashNext(p))
313c8 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72  {.**     SomeStr
313c9 75 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20  ucture *pData = 
313ca 66 74 73 33 48 61 73 68 44 61 74 61 28 70 29 3b  fts3HashData(p);
313cb 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f  .**     // do so
313cc 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 44 61  mething with pDa
313cd 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65  ta.**   }.*/.#de
313ce 66 69 6e 65 20 66 74 73 33 48 61 73 68 46 69 72  fine fts3HashFir
313cf 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72  st(H)  ((H)->fir
313d0 73 74 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33  st).#define fts3
313d1 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 28 28  HashNext(E)   ((
313d2 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e  E)->next).#defin
313d3 65 20 66 74 73 33 48 61 73 68 44 61 74 61 28 45  e fts3HashData(E
313d4 29 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a  )   ((E)->data).
313d5 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
313d6 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e  Key(E)    ((E)->
313d7 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 66 74  pKey).#define ft
313d8 73 33 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29  s3HashKeysize(E)
313d9 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a   ((E)->nKey)../*
313da 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e  .** Number of en
313db 74 72 69 65 73 20 69 6e 20 61 20 68 61 73 68 20  tries in a hash 
313dc 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65  table.*/.#define
313dd 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28 48   fts3HashCount(H
313de 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a  )  ((H)->count).
313df 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 46 54 53 33  .#endif /* _FTS3
313e0 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a  _HASH_H_ */../**
313e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
313e2 20 6f 66 20 66 74 73 33 5f 68 61 73 68 2e 68 20   of fts3_hash.h 
313e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
313e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
313e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
313e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
313e7 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
313e8 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73   left off in fts
313e9 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.c ************
313ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66  ***********/.#if
313eb 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45  ndef SQLITE_CORE
313ec 20 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e   .  SQLITE_EXTEN
313ed 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69  SION_INIT1.#endi
313ee 66 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  f.../* TODO(shes
313ef 73 29 20 4d 41 4e 2c 20 74 68 69 73 20 74 68 69  s) MAN, this thi
313f0 6e 67 20 6e 65 65 64 73 20 73 6f 6d 65 20 72 65  ng needs some re
313f1 66 61 63 74 6f 72 69 6e 67 2e 20 20 41 74 20 6d  factoring.  At m
313f2 69 6e 69 6d 75 6d 2c 20 69 74 0a 2a 2a 20 77 6f  inimum, it.** wo
313f3 75 6c 64 20 62 65 20 6e 69 63 65 20 74 6f 20 6f  uld be nice to o
313f4 72 64 65 72 20 74 68 65 20 66 69 6c 65 20 62 65  rder the file be
313f5 74 74 65 72 2c 20 70 65 72 68 61 70 73 20 73 6f  tter, perhaps so
313f6 6d 65 74 68 69 6e 67 20 61 6c 6f 6e 67 20 74 68  mething along th
313f7 65 0a 2a 2a 20 6c 69 6e 65 73 20 6f 66 3a 0a 2a  e.** lines of:.*
313f8 2a 0a 2a 2a 20 20 2d 20 75 74 69 6c 69 74 79 20  *.**  - utility 
313f9 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20  functions.**  - 
313fa 74 61 62 6c 65 20 73 65 74 75 70 20 66 75 6e 63  table setup func
313fb 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c  tions.**  - tabl
313fc 65 20 75 70 64 61 74 65 20 66 75 6e 63 74 69 6f  e update functio
313fd 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20 71  ns.**  - table q
313fe 75 65 72 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  uery functions.*
313ff 2a 0a 2a 2a 20 50 75 74 20 74 68 65 20 71 75 65  *.** Put the que
31400 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 73  ry functions las
31401 74 20 62 65 63 61 75 73 65 20 74 68 65 79 27 72  t because they'r
31402 65 20 6c 69 6b 65 6c 79 20 74 6f 20 72 65 66 65  e likely to refe
31403 72 65 6e 63 65 0a 2a 2a 20 74 79 70 65 64 65 66  rence.** typedef
31404 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 66  s or functions f
31405 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 75 70  rom the table up
31406 64 61 74 65 20 73 65 63 74 69 6f 6e 2e 0a 2a 2f  date section..*/
31407 0a 0a 23 69 66 20 30 0a 23 20 64 65 66 69 6e 65  ..#if 0.# define
31408 20 46 54 53 54 52 41 43 45 28 41 29 20 20 70 72   FTSTRACE(A)  pr
31409 69 6e 74 66 20 41 3b 20 66 66 6c 75 73 68 28 73  intf A; fflush(s
3140a 74 64 6f 75 74 29 0a 23 65 6c 73 65 0a 23 20 64  tdout).#else.# d
3140b 65 66 69 6e 65 20 46 54 53 54 52 41 43 45 28 41  efine FTSTRACE(A
3140c 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 74 20  ).#endif../* It 
3140d 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 63  is not safe to c
3140e 61 6c 6c 20 69 73 73 70 61 63 65 28 29 2c 20 74  all isspace(), t
3140f 6f 6c 6f 77 65 72 28 29 2c 20 6f 72 20 69 73 61  olower(), or isa
31410 6c 6e 75 6d 28 29 20 6f 6e 0a 2a 2a 20 68 69 2d  lnum() on.** hi-
31411 62 69 74 2d 73 65 74 20 63 68 61 72 61 63 74 65  bit-set characte
31412 72 73 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  rs.  This is the
31413 20 73 61 6d 65 20 73 6f 6c 75 74 69 6f 6e 20 75   same solution u
31414 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 74 6f  sed in the.** to
31415 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 2f 2a 20 54  kenizer..*/./* T
31416 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 73  ODO(shess) The s
31417 6e 69 70 70 65 74 2d 67 65 6e 65 72 61 74 69 6f  nippet-generatio
31418 6e 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 62 65  n code should be
31419 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 74 6f   using the.** to
3141a 6b 65 6e 69 7a 65 72 2d 67 65 6e 65 72 61 74 65  kenizer-generate
3141b 64 20 74 6f 6b 65 6e 73 20 72 61 74 68 65 72 20  d tokens rather 
3141c 74 68 61 6e 20 64 6f 69 6e 67 20 69 74 73 20 6f  than doing its o
3141d 77 6e 20 6c 6f 63 61 6c 0a 2a 2a 20 74 6f 6b 65  wn local.** toke
3141e 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20  nization..*/./* 
3141f 54 4f 44 4f 28 73 68 65 73 73 29 20 49 73 20 5f  TODO(shess) Is _
31420 5f 69 73 61 73 63 69 69 28 29 20 61 20 70 6f 72  _isascii() a por
31421 74 61 62 6c 65 20 76 65 72 73 69 6f 6e 20 6f 66  table version of
31422 20 28 63 26 30 78 38 30 29 3d 3d 30 3f 20 2a 2f   (c&0x80)==0? */
31423 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 66 65  .static int safe
31424 5f 69 73 73 70 61 63 65 28 63 68 61 72 20 63 29  _isspace(char c)
31425 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78  {.  return (c&0x
31426 38 30 29 3d 3d 30 20 3f 20 69 73 73 70 61 63 65  80)==0 ? isspace
31427 28 63 29 20 3a 20 30 3b 0a 7d 0a 73 74 61 74 69  (c) : 0;.}.stati
31428 63 20 69 6e 74 20 73 61 66 65 5f 74 6f 6c 6f 77  c int safe_tolow
31429 65 72 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65  er(char c){.  re
3142a 74 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30  turn (c&0x80)==0
3142b 20 3f 20 74 6f 6c 6f 77 65 72 28 63 29 20 3a 20   ? tolower(c) : 
3142c 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  c;.}.static int 
3142d 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 63 68 61  safe_isalnum(cha
3142e 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  r c){.  return (
3142f 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 61  c&0x80)==0 ? isa
31430 6c 6e 75 6d 28 63 29 20 3a 20 30 3b 0a 7d 0a 0a  lnum(c) : 0;.}..
31431 74 79 70 65 64 65 66 20 65 6e 75 6d 20 44 6f 63  typedef enum Doc
31432 4c 69 73 74 54 79 70 65 20 7b 0a 20 20 44 4c 5f  ListType {.  DL_
31433 44 4f 43 49 44 53 2c 20 20 20 20 20 20 20 20 20  DOCIDS,         
31434 20 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 6f       /* docids o
31435 6e 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50 4f 53 49  nly */.  DL_POSI
31436 54 49 4f 4e 53 2c 20 20 20 20 20 20 20 20 20 20  TIONS,          
31437 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20 70 6f 73   /* docids + pos
31438 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 44 4c 5f 50  itions */.  DL_P
31439 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
3143a 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20      /* docids + 
3143b 70 6f 73 69 74 69 6f 6e 73 20 2b 20 6f 66 66 73  positions + offs
3143c 65 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c 69 73 74  ets */.} DocList
3143d 54 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20 42 79 20  Type;../*.** By 
3143e 64 65 66 61 75 6c 74 2c 20 6f 6e 6c 79 20 70 6f  default, only po
3143f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6e 6f 74 20  sitions and not 
31440 6f 66 66 73 65 74 73 20 61 72 65 20 73 74 6f 72  offsets are stor
31441 65 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73  ed in the doclis
31442 74 73 2e 0a 2a 2a 20 54 6f 20 63 68 61 6e 67 65  ts..** To change
31443 20 74 68 69 73 20 73 6f 20 74 68 61 74 20 6f 66   this so that of
31444 66 73 65 74 73 20 61 72 65 20 73 74 6f 72 65 64  fsets are stored
31445 20 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65 20 77 69   too, compile wi
31446 74 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  th.**.**        
31447 20 20 2d 44 44 4c 5f 44 45 46 41 55 4c 54 3d 44    -DDL_DEFAULT=D
31448 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
31449 45 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20 44 4c 5f  ETS.**.** If DL_
3144a 44 45 46 41 55 4c 54 20 69 73 20 73 65 74 20 74  DEFAULT is set t
3144b 6f 20 44 4c 5f 44 4f 43 49 44 53 2c 20 79 6f 75  o DL_DOCIDS, you
3144c 72 20 74 61 62 6c 65 20 63 61 6e 20 6f 6e 6c 79  r table can only
3144d 20 62 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20   be inserted.** 
3144e 69 6e 74 6f 20 28 6e 6f 20 64 65 6c 65 74 65 73  into (no deletes
3144f 20 6f 72 20 75 70 64 61 74 65 73 29 2e 0a 2a 2f   or updates)..*/
31450 0a 23 69 66 6e 64 65 66 20 44 4c 5f 44 45 46 41  .#ifndef DL_DEFA
31451 55 4c 54 0a 23 20 64 65 66 69 6e 65 20 44 4c 5f  ULT.# define DL_
31452 44 45 46 41 55 4c 54 20 44 4c 5f 50 4f 53 49 54  DEFAULT DL_POSIT
31453 49 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a 65 6e 75  IONS.#endif..enu
31454 6d 20 7b 0a 20 20 50 4f 53 5f 45 4e 44 20 3d 20  m {.  POS_END = 
31455 30 2c 20 20 20 20 20 20 20 20 2f 2a 20 65 6e 64  0,        /* end
31456 20 6f 66 20 74 68 69 73 20 70 6f 73 69 74 69 6f   of this positio
31457 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 50 4f 53 5f  n list */.  POS_
31458 43 4f 4c 55 4d 4e 2c 20 20 20 20 20 20 20 20 20  COLUMN,         
31459 2f 2a 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e  /* followed by n
3145a 65 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  ew column number
3145b 20 2a 2f 0a 20 20 50 4f 53 5f 42 41 53 45 0a 7d   */.  POS_BASE.}
3145c 3b 0a 0a 2f 2a 20 4d 45 52 47 45 5f 43 4f 55 4e  ;../* MERGE_COUN
3145d 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f  T controls how o
3145e 66 74 65 6e 20 77 65 20 6d 65 72 67 65 20 73 65  ften we merge se
3145f 67 6d 65 6e 74 73 20 28 73 65 65 20 63 6f 6d 6d  gments (see comm
31460 65 6e 74 20 61 74 0a 2a 2a 20 74 6f 70 20 6f 66  ent at.** top of
31461 20 66 69 6c 65 29 2e 0a 2a 2f 0a 23 64 65 66 69   file)..*/.#defi
31462 6e 65 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 31  ne MERGE_COUNT 1
31463 36 0a 0a 2f 2a 20 75 74 69 6c 69 74 79 20 66 75  6../* utility fu
31464 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f 2a 20 43  nctions */../* C
31465 4c 45 41 52 28 29 20 61 6e 64 20 53 43 52 41 4d  LEAR() and SCRAM
31466 42 4c 45 28 29 20 61 62 73 74 72 61 63 74 20 6d  BLE() abstract m
31467 65 6d 73 65 74 28 29 20 6f 6e 20 61 20 70 6f 69  emset() on a poi
31468 6e 74 65 72 20 74 6f 20 61 20 73 69 6e 67 6c 65  nter to a single
31469 0a 2a 2a 20 72 65 63 6f 72 64 20 74 6f 20 70 72  .** record to pr
3146a 65 76 65 6e 74 20 65 72 72 6f 72 73 20 6f 66 20  event errors of 
3146b 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  the form:.**.** 
3146c 6d 79 5f 66 75 6e 63 74 69 6f 6e 28 53 6f 6d 65  my_function(Some
3146d 54 79 70 65 20 2a 62 29 7b 0a 2a 2a 20 20 20 6d  Type *b){.**   m
3146e 65 6d 73 65 74 28 62 2c 20 27 5c 30 27 2c 20 73  emset(b, '\0', s
3146f 69 7a 65 6f 66 28 62 29 29 3b 20 20 2f 2f 20 73  izeof(b));  // s
31470 69 7a 65 6f 66 28 62 29 21 3d 73 69 7a 65 6f 66  izeof(b)!=sizeof
31471 28 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a 2f 2a 20  (*b).** }.*/./* 
31472 54 4f 44 4f 28 73 68 65 73 73 29 20 4f 62 76 69  TODO(shess) Obvi
31473 6f 75 73 20 63 61 6e 64 69 64 61 74 65 73 20 66  ous candidates f
31474 6f 72 20 61 20 68 65 61 64 65 72 20 66 69 6c 65  or a header file
31475 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4c 45  . */.#define CLE
31476 41 52 28 62 29 20 6d 65 6d 73 65 74 28 62 2c 20  AR(b) memset(b, 
31477 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28 2a 28 62  '\0', sizeof(*(b
31478 29 29 29 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  )))..#ifndef NDE
31479 42 55 47 0a 23 20 20 64 65 66 69 6e 65 20 53 43  BUG.#  define SC
3147a 52 41 4d 42 4c 45 28 62 29 20 6d 65 6d 73 65 74  RAMBLE(b) memset
3147b 28 62 2c 20 30 78 35 35 2c 20 73 69 7a 65 6f 66  (b, 0x55, sizeof
3147c 28 2a 28 62 29 29 29 0a 23 65 6c 73 65 0a 23 20  (*(b))).#else.# 
3147d 20 64 65 66 69 6e 65 20 53 43 52 41 4d 42 4c 45   define SCRAMBLE
3147e 28 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 57  (b).#endif../* W
3147f 65 20 6d 61 79 20 6e 65 65 64 20 75 70 20 74 6f  e may need up to
31480 20 56 41 52 49 4e 54 5f 4d 41 58 20 62 79 74 65   VARINT_MAX byte
31481 73 20 74 6f 20 73 74 6f 72 65 20 61 6e 20 65 6e  s to store an en
31482 63 6f 64 65 64 20 36 34 2d 62 69 74 20 69 6e 74  coded 64-bit int
31483 65 67 65 72 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  eger. */.#define
31484 20 56 41 52 49 4e 54 5f 4d 41 58 20 31 30 0a 0a   VARINT_MAX 10..
31485 2f 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69  /* Write a 64-bi
31486 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
31487 68 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d  h integer to mem
31488 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20  ory starting at 
31489 70 5b 30 5d 2e 0a 20 2a 20 54 68 65 20 6c 65 6e  p[0].. * The len
3148a 67 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74  gth of data writ
3148b 74 65 6e 20 77 69 6c 6c 20 62 65 20 62 65 74 77  ten will be betw
3148c 65 65 6e 20 31 20 61 6e 64 20 56 41 52 49 4e 54  een 1 and VARINT
3148d 5f 4d 41 58 20 62 79 74 65 73 2e 0a 20 2a 20 54  _MAX bytes.. * T
3148e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
3148f 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65  es written is re
31490 74 75 72 6e 65 64 2e 20 2a 2f 0a 73 74 61 74 69  turned. */.stati
31491 63 20 69 6e 74 20 66 74 73 33 50 75 74 56 61 72  c int fts3PutVar
31492 69 6e 74 28 63 68 61 72 20 2a 70 2c 20 73 71 6c  int(char *p, sql
31493 69 74 65 5f 69 6e 74 36 34 20 76 29 7b 0a 20 20  ite_int64 v){.  
31494 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71  unsigned char *q
31495 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
31496 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74 65  r *) p;.  sqlite
31497 5f 75 69 6e 74 36 34 20 76 75 20 3d 20 76 3b 0a  _uint64 vu = v;.
31498 20 20 64 6f 7b 0a 20 20 20 20 2a 71 2b 2b 20 3d    do{.    *q++ =
31499 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29   (unsigned char)
3149a 20 28 28 76 75 20 26 20 30 78 37 66 29 20 7c 20   ((vu & 0x7f) | 
3149b 30 78 38 30 29 3b 0a 20 20 20 20 76 75 20 3e 3e  0x80);.    vu >>
3149c 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76  = 7;.  }while( v
3149d 75 21 3d 30 20 29 3b 0a 20 20 71 5b 2d 31 5d 20  u!=0 );.  q[-1] 
3149e 26 3d 20 30 78 37 66 3b 20 20 2f 2a 20 74 75 72  &= 0x7f;  /* tur
3149f 6e 20 6f 66 66 20 68 69 67 68 20 62 69 74 20 69  n off high bit i
314a0 6e 20 66 69 6e 61 6c 20 62 79 74 65 20 2a 2f 0a  n final byte */.
314a1 20 20 61 73 73 65 72 74 28 20 71 20 2d 20 28 75    assert( q - (u
314a2 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
314a3 20 3c 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20 29   <= VARINT_MAX )
314a4 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  ;.  return (int)
314a5 20 28 71 20 2d 20 28 75 6e 73 69 67 6e 65 64 20   (q - (unsigned 
314a6 63 68 61 72 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a  char *)p);.}../*
314a7 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76   Read a 64-bit v
314a8 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
314a9 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f  nteger from memo
314aa 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ry starting at p
314ab 5b 30 5d 2e 0a 20 2a 20 52 65 74 75 72 6e 20 74  [0].. * Return t
314ac 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
314ad 65 73 20 72 65 61 64 2c 20 6f 72 20 30 20 6f 6e  es read, or 0 on
314ae 20 65 72 72 6f 72 2e 0a 20 2a 20 54 68 65 20 76   error.. * The v
314af 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69  alue is stored i
314b0 6e 20 2a 76 2e 20 2a 2f 0a 73 74 61 74 69 63 20  n *v. */.static 
314b1 69 6e 74 20 66 74 73 33 47 65 74 56 61 72 69 6e  int fts3GetVarin
314b2 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c  t(const char *p,
314b3 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 76   sqlite_int64 *v
314b4 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
314b5 6e 65 64 20 63 68 61 72 20 2a 71 20 3d 20 28 63  ned char *q = (c
314b6 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
314b7 61 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74  ar *) p;.  sqlit
314b8 65 5f 75 69 6e 74 36 34 20 78 20 3d 20 30 2c 20  e_uint64 x = 0, 
314b9 79 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20  y = 1;.  while( 
314ba 28 2a 71 20 26 20 30 78 38 30 29 20 3d 3d 20 30  (*q & 0x80) == 0
314bb 78 38 30 20 29 7b 0a 20 20 20 20 78 20 2b 3d 20  x80 ){.    x += 
314bc 79 20 2a 20 28 2a 71 2b 2b 20 26 20 30 78 37 66  y * (*q++ & 0x7f
314bd 29 3b 0a 20 20 20 20 79 20 3c 3c 3d 20 37 3b 0a  );.    y <<= 7;.
314be 20 20 20 20 69 66 28 20 71 20 2d 20 28 75 6e 73      if( q - (uns
314bf 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 20 3e  igned char *)p >
314c0 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20 29 7b 20  = VARINT_MAX ){ 
314c1 20 2f 2a 20 62 61 64 20 64 61 74 61 20 2a 2f 0a   /* bad data */.
314c2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20        assert( 0 
314c3 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
314c4 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 78  0;.    }.  }.  x
314c5 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 29 3b 0a   += y * (*q++);.
314c6 20 20 2a 76 20 3d 20 28 73 71 6c 69 74 65 5f 69    *v = (sqlite_i
314c7 6e 74 36 34 29 20 78 3b 0a 20 20 72 65 74 75 72  nt64) x;.  retur
314c8 6e 20 28 69 6e 74 29 20 28 71 20 2d 20 28 75 6e  n (int) (q - (un
314c9 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29  signed char *)p)
314ca 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
314cb 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
314cc 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c 20 69  const char *p, i
314cd 6e 74 20 2a 70 69 29 7b 0a 20 73 71 6c 69 74 65  nt *pi){. sqlite
314ce 5f 69 6e 74 36 34 20 69 3b 0a 20 69 6e 74 20 72  _int64 i;. int r
314cf 65 74 20 3d 20 66 74 73 33 47 65 74 56 61 72 69  et = fts3GetVari
314d0 6e 74 28 70 2c 20 26 69 29 3b 0a 20 2a 70 69 20  nt(p, &i);. *pi 
314d1 3d 20 28 69 6e 74 29 20 69 3b 0a 20 61 73 73 65  = (int) i;. asse
314d2 72 74 28 20 2a 70 69 3d 3d 69 20 29 3b 0a 20 72  rt( *pi==i );. r
314d3 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a  eturn ret;.}../*
314d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
314d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
314d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
314d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
314d8 2a 2a 2f 0a 2f 2a 20 44 61 74 61 42 75 66 66 65  **/./* DataBuffe
314d9 72 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c  r is used to col
314da 6c 65 63 74 20 64 61 74 61 20 69 6e 74 6f 20 61  lect data into a
314db 20 62 75 66 66 65 72 20 69 6e 20 70 69 65 63 65   buffer in piece
314dc 6d 65 61 6c 0a 2a 2a 20 66 61 73 68 69 6f 6e 2e  meal.** fashion.
314dd 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20    It implements 
314de 74 68 65 20 75 73 75 61 6c 20 64 69 73 74 69 6e  the usual distin
314df 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 61 6d  ction between am
314e0 6f 75 6e 74 20 6f 66 0a 2a 2a 20 64 61 74 61 20  ount of.** data 
314e1 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64  currently stored
314e2 20 28 6e 44 61 74 61 29 20 61 6e 64 20 62 75 66   (nData) and buf
314e3 66 65 72 20 63 61 70 61 63 69 74 79 20 28 6e 43  fer capacity (nC
314e4 61 70 61 63 69 74 79 29 2e 0a 2a 2a 0a 2a 2a 20  apacity)..**.** 
314e5 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 20 2d  dataBufferInit -
314e6 20 63 72 65 61 74 65 20 61 20 62 75 66 66 65 72   create a buffer
314e7 20 77 69 74 68 20 67 69 76 65 6e 20 69 6e 69 74   with given init
314e8 69 61 6c 20 63 61 70 61 63 69 74 79 2e 0a 2a 2a  ial capacity..**
314e9 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
314ea 20 2d 20 66 6f 72 67 65 74 20 62 75 66 66 65 72   - forget buffer
314eb 27 73 20 64 61 74 61 2c 20 72 65 74 61 69 6e 69  's data, retaini
314ec 6e 67 20 63 61 70 61 63 69 74 79 2e 0a 2a 2a 20  ng capacity..** 
314ed 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
314ee 79 20 2d 20 66 72 65 65 20 62 75 66 66 65 72 27  y - free buffer'
314ef 73 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42  s data..** dataB
314f0 75 66 66 65 72 53 77 61 70 20 2d 20 73 77 61 70  ufferSwap - swap
314f1 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f   contents of two
314f2 20 62 75 66 66 65 72 73 2e 0a 2a 2a 20 64 61 74   buffers..** dat
314f3 61 42 75 66 66 65 72 45 78 70 61 6e 64 20 2d 20  aBufferExpand - 
314f4 65 78 70 61 6e 64 20 63 61 70 61 63 69 74 79 20  expand capacity 
314f5 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20 64  without adding d
314f6 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66  ata..** dataBuff
314f7 65 72 41 70 70 65 6e 64 20 2d 20 61 70 70 65 6e  erAppend - appen
314f8 64 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42  d data..** dataB
314f9 75 66 66 65 72 41 70 70 65 6e 64 32 20 2d 20 61  ufferAppend2 - a
314fa 70 70 65 6e 64 20 74 77 6f 20 70 69 65 63 65 73  ppend two pieces
314fb 20 6f 66 20 64 61 74 61 20 61 74 20 6f 6e 63 65   of data at once
314fc 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 52  ..** dataBufferR
314fd 65 70 6c 61 63 65 20 2d 20 72 65 70 6c 61 63 65  eplace - replace
314fe 20 62 75 66 66 65 72 27 73 20 64 61 74 61 2e 0a   buffer's data..
314ff 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
31500 74 20 44 61 74 61 42 75 66 66 65 72 20 7b 0a 20  t DataBuffer {. 
31501 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20   char *pData;   
31502 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
31503 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 65 64 20 62  r to malloc'ed b
31504 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20  uffer. */.  int 
31505 6e 43 61 70 61 63 69 74 79 3b 20 20 20 20 20 20  nCapacity;      
31506 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 44 61    /* Size of pDa
31507 74 61 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20  ta buffer. */.  
31508 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20  int nData;      
31509 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20        /* End of 
3150a 64 61 74 61 20 6c 6f 61 64 65 64 20 69 6e 74 6f  data loaded into
3150b 20 70 44 61 74 61 2e 20 2a 2f 0a 7d 20 44 61 74   pData. */.} Dat
3150c 61 42 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63  aBuffer;..static
3150d 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72   void dataBuffer
3150e 49 6e 69 74 28 44 61 74 61 42 75 66 66 65 72 20  Init(DataBuffer 
3150f 2a 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e 43  *pBuffer, int nC
31510 61 70 61 63 69 74 79 29 7b 0a 20 20 61 73 73 65  apacity){.  asse
31511 72 74 28 20 6e 43 61 70 61 63 69 74 79 3e 3d 30  rt( nCapacity>=0
31512 20 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e   );.  pBuffer->n
31513 44 61 74 61 20 3d 20 30 3b 0a 20 20 70 42 75 66  Data = 0;.  pBuf
31514 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d  fer->nCapacity =
31515 20 6e 43 61 70 61 63 69 74 79 3b 0a 20 20 70 42   nCapacity;.  pB
31516 75 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20 6e  uffer->pData = n
31517 43 61 70 61 63 69 74 79 3d 3d 30 20 3f 20 4e 55  Capacity==0 ? NU
31518 4c 4c 20 3a 20 73 71 6c 69 74 65 33 5f 6d 61 6c  LL : sqlite3_mal
31519 6c 6f 63 28 6e 43 61 70 61 63 69 74 79 29 3b 0a  loc(nCapacity);.
3151a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  }.static void da
3151b 74 61 42 75 66 66 65 72 52 65 73 65 74 28 44 61  taBufferReset(Da
3151c 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65  taBuffer *pBuffe
3151d 72 29 7b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e  r){.  pBuffer->n
3151e 44 61 74 61 20 3d 20 30 3b 0a 7d 0a 73 74 61 74  Data = 0;.}.stat
3151f 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66  ic void dataBuff
31520 65 72 44 65 73 74 72 6f 79 28 44 61 74 61 42 75  erDestroy(DataBu
31521 66 66 65 72 20 2a 70 42 75 66 66 65 72 29 7b 0a  ffer *pBuffer){.
31522 20 20 69 66 28 20 70 42 75 66 66 65 72 2d 3e 70    if( pBuffer->p
31523 44 61 74 61 21 3d 4e 55 4c 4c 20 29 20 73 71 6c  Data!=NULL ) sql
31524 69 74 65 33 5f 66 72 65 65 28 70 42 75 66 66 65  ite3_free(pBuffe
31525 72 2d 3e 70 44 61 74 61 29 3b 0a 20 20 53 43 52  r->pData);.  SCR
31526 41 4d 42 4c 45 28 70 42 75 66 66 65 72 29 3b 0a  AMBLE(pBuffer);.
31527 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  }.static void da
31528 74 61 42 75 66 66 65 72 53 77 61 70 28 44 61 74  taBufferSwap(Dat
31529 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
3152a 31 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 70  1, DataBuffer *p
3152b 42 75 66 66 65 72 32 29 7b 0a 20 20 44 61 74 61  Buffer2){.  Data
3152c 42 75 66 66 65 72 20 74 6d 70 20 3d 20 2a 70 42  Buffer tmp = *pB
3152d 75 66 66 65 72 31 3b 0a 20 20 2a 70 42 75 66 66  uffer1;.  *pBuff
3152e 65 72 31 20 3d 20 2a 70 42 75 66 66 65 72 32 3b  er1 = *pBuffer2;
3152f 0a 20 20 2a 70 42 75 66 66 65 72 32 20 3d 20 74  .  *pBuffer2 = t
31530 6d 70 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  mp;.}.static voi
31531 64 20 64 61 74 61 42 75 66 66 65 72 45 78 70 61  d dataBufferExpa
31532 6e 64 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  nd(DataBuffer *p
31533 42 75 66 66 65 72 2c 20 69 6e 74 20 6e 41 64 64  Buffer, int nAdd
31534 43 61 70 61 63 69 74 79 29 7b 0a 20 20 61 73 73  Capacity){.  ass
31535 65 72 74 28 20 6e 41 64 64 43 61 70 61 63 69 74  ert( nAddCapacit
31536 79 3e 30 20 29 3b 0a 20 20 2f 2a 20 54 4f 44 4f  y>0 );.  /* TODO
31537 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72  (shess) Consider
31538 20 65 78 70 61 6e 64 69 6e 67 20 6d 6f 72 65 20   expanding more 
31539 61 67 67 72 65 73 73 69 76 65 6c 79 2e 20 20 4e  aggressively.  N
3153a 6f 74 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a  ote that the.  *
3153b 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 61 6c  * underlying mal
3153c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  loc implementati
3153d 6f 6e 20 6d 61 79 20 74 61 6b 65 20 63 61 72 65  on may take care
3153e 20 6f 66 20 73 75 63 68 20 74 68 69 6e 67 73 20   of such things 
3153f 66 6f 72 0a 20 20 2a 2a 20 75 73 20 61 6c 72 65  for.  ** us alre
31540 61 64 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ady..  */.  if( 
31541 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e  pBuffer->nData+n
31542 41 64 64 43 61 70 61 63 69 74 79 3e 70 42 75 66  AddCapacity>pBuf
31543 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 29  fer->nCapacity )
31544 7b 0a 20 20 20 20 70 42 75 66 66 65 72 2d 3e 6e  {.    pBuffer->n
31545 43 61 70 61 63 69 74 79 20 3d 20 70 42 75 66 66  Capacity = pBuff
31546 65 72 2d 3e 6e 44 61 74 61 2b 6e 41 64 64 43 61  er->nData+nAddCa
31547 70 61 63 69 74 79 3b 0a 20 20 20 20 70 42 75 66  pacity;.    pBuf
31548 66 65 72 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c  fer->pData = sql
31549 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 42 75  ite3_realloc(pBu
3154a 66 66 65 72 2d 3e 70 44 61 74 61 2c 20 70 42 75  ffer->pData, pBu
3154b 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 29  ffer->nCapacity)
3154c 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
3154d 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 41 70  oid dataBufferAp
3154e 70 65 6e 64 28 44 61 74 61 42 75 66 66 65 72 20  pend(DataBuffer 
3154f 2a 70 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20  *pBuffer,.      
31550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31551 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
31552 72 20 2a 70 53 6f 75 72 63 65 2c 20 69 6e 74 20  r *pSource, int 
31553 6e 53 6f 75 72 63 65 29 7b 0a 20 20 61 73 73 65  nSource){.  asse
31554 72 74 28 20 6e 53 6f 75 72 63 65 3e 30 20 26 26  rt( nSource>0 &&
31555 20 70 53 6f 75 72 63 65 21 3d 4e 55 4c 4c 20 29   pSource!=NULL )
31556 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 45 78  ;.  dataBufferEx
31557 70 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e 53  pand(pBuffer, nS
31558 6f 75 72 63 65 29 3b 0a 20 20 6d 65 6d 63 70 79  ource);.  memcpy
31559 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 2b  (pBuffer->pData+
3155a 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2c 20  pBuffer->nData, 
3155b 70 53 6f 75 72 63 65 2c 20 6e 53 6f 75 72 63 65  pSource, nSource
3155c 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44  );.  pBuffer->nD
3155d 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63 65 3b 0a  ata += nSource;.
3155e 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  }.static void da
3155f 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
31560 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66  DataBuffer *pBuf
31561 66 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fer,.           
31562 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31563 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
31564 53 6f 75 72 63 65 31 2c 20 69 6e 74 20 6e 53 6f  Source1, int nSo
31565 75 72 63 65 31 2c 0a 20 20 20 20 20 20 20 20 20  urce1,.         
31566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31567 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
31568 2a 70 53 6f 75 72 63 65 32 2c 20 69 6e 74 20 6e  *pSource2, int n
31569 53 6f 75 72 63 65 32 29 7b 0a 20 20 61 73 73 65  Source2){.  asse
3156a 72 74 28 20 6e 53 6f 75 72 63 65 31 3e 30 20 26  rt( nSource1>0 &
3156b 26 20 70 53 6f 75 72 63 65 31 21 3d 4e 55 4c 4c  & pSource1!=NULL
3156c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 53   );.  assert( nS
3156d 6f 75 72 63 65 32 3e 30 20 26 26 20 70 53 6f 75  ource2>0 && pSou
3156e 72 63 65 32 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20  rce2!=NULL );.  
3156f 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64  dataBufferExpand
31570 28 70 42 75 66 66 65 72 2c 20 6e 53 6f 75 72 63  (pBuffer, nSourc
31571 65 31 2b 6e 53 6f 75 72 63 65 32 29 3b 0a 20 20  e1+nSource2);.  
31572 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e  memcpy(pBuffer->
31573 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e  pData+pBuffer->n
31574 44 61 74 61 2c 20 70 53 6f 75 72 63 65 31 2c 20  Data, pSource1, 
31575 6e 53 6f 75 72 63 65 31 29 3b 0a 20 20 6d 65 6d  nSource1);.  mem
31576 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70 44 61  cpy(pBuffer->pDa
31577 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  ta+pBuffer->nDat
31578 61 2b 6e 53 6f 75 72 63 65 31 2c 20 70 53 6f 75  a+nSource1, pSou
31579 72 63 65 32 2c 20 6e 53 6f 75 72 63 65 32 29 3b  rce2, nSource2);
3157a 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  .  pBuffer->nDat
3157b 61 20 2b 3d 20 6e 53 6f 75 72 63 65 31 2b 6e 53  a += nSource1+nS
3157c 6f 75 72 63 65 32 3b 0a 7d 0a 73 74 61 74 69 63  ource2;.}.static
3157d 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72   void dataBuffer
3157e 52 65 70 6c 61 63 65 28 44 61 74 61 42 75 66 66  Replace(DataBuff
3157f 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20 20  er *pBuffer,.   
31580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31581 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
31582 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c 20   char *pSource, 
31583 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20 20  int nSource){.  
31584 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
31585 70 42 75 66 66 65 72 29 3b 0a 20 20 64 61 74 61  pBuffer);.  data
31586 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 42 75  BufferAppend(pBu
31587 66 66 65 72 2c 20 70 53 6f 75 72 63 65 2c 20 6e  ffer, pSource, n
31588 53 6f 75 72 63 65 29 3b 0a 7d 0a 0a 2f 2a 20 53  Source);.}../* S
31589 74 72 69 6e 67 42 75 66 66 65 72 20 69 73 20 61  tringBuffer is a
3158a 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64   null-terminated
3158b 20 76 65 72 73 69 6f 6e 20 6f 66 20 44 61 74 61   version of Data
3158c 42 75 66 66 65 72 2e 20 2a 2f 0a 74 79 70 65 64  Buffer. */.typed
3158d 65 66 20 73 74 72 75 63 74 20 53 74 72 69 6e 67  ef struct String
3158e 42 75 66 66 65 72 20 7b 0a 20 20 44 61 74 61 42  Buffer {.  DataB
3158f 75 66 66 65 72 20 62 3b 20 20 20 20 20 20 20 20  uffer b;        
31590 20 20 20 20 2f 2a 20 49 6e 63 6c 75 64 65 73 20      /* Includes 
31591 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e  null terminator.
31592 20 2a 2f 0a 7d 20 53 74 72 69 6e 67 42 75 66 66   */.} StringBuff
31593 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  er;..static void
31594 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65   initStringBuffe
31595 72 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a  r(StringBuffer *
31596 73 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65  sb){.  dataBuffe
31597 72 49 6e 69 74 28 26 73 62 2d 3e 62 2c 20 31 30  rInit(&sb->b, 10
31598 30 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72  0);.  dataBuffer
31599 52 65 70 6c 61 63 65 28 26 73 62 2d 3e 62 2c 20  Replace(&sb->b, 
3159a 22 22 2c 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63  "", 1);.}.static
3159b 20 69 6e 74 20 73 74 72 69 6e 67 42 75 66 66 65   int stringBuffe
3159c 72 4c 65 6e 67 74 68 28 53 74 72 69 6e 67 42 75  rLength(StringBu
3159d 66 66 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74  ffer *sb){.  ret
3159e 75 72 6e 20 73 62 2d 3e 62 2e 6e 44 61 74 61 2d  urn sb->b.nData-
3159f 31 3b 0a 7d 0a 73 74 61 74 69 63 20 63 68 61 72  1;.}.static char
315a0 20 2a 73 74 72 69 6e 67 42 75 66 66 65 72 44 61   *stringBufferDa
315a1 74 61 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  ta(StringBuffer 
315a2 2a 73 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  *sb){.  return s
315a3 62 2d 3e 62 2e 70 44 61 74 61 3b 0a 7d 0a 73 74  b->b.pData;.}.st
315a4 61 74 69 63 20 76 6f 69 64 20 73 74 72 69 6e 67  atic void string
315a5 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 53 74  BufferDestroy(St
315a6 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b  ringBuffer *sb){
315a7 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
315a8 74 72 6f 79 28 26 73 62 2d 3e 62 29 3b 0a 7d 0a  troy(&sb->b);.}.
315a9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 61 70  .static void nap
315aa 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66 66 65  pend(StringBuffe
315ab 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63 68 61  r *sb, const cha
315ac 72 20 2a 7a 46 72 6f 6d 2c 20 69 6e 74 20 6e 46  r *zFrom, int nF
315ad 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rom){.  assert( 
315ae 73 62 2d 3e 62 2e 6e 44 61 74 61 3e 30 20 29 3b  sb->b.nData>0 );
315af 0a 20 20 69 66 28 20 6e 46 72 6f 6d 3e 30 20 29  .  if( nFrom>0 )
315b0 7b 0a 20 20 20 20 73 62 2d 3e 62 2e 6e 44 61 74  {.    sb->b.nDat
315b1 61 2d 2d 3b 0a 20 20 20 20 64 61 74 61 42 75 66  a--;.    dataBuf
315b2 66 65 72 41 70 70 65 6e 64 32 28 26 73 62 2d 3e  ferAppend2(&sb->
315b3 62 2c 20 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 2c  b, zFrom, nFrom,
315b4 20 22 22 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 73   "", 1);.  }.}.s
315b5 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e  tatic void appen
315b6 64 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a  d(StringBuffer *
315b7 73 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  sb, const char *
315b8 7a 46 72 6f 6d 29 7b 0a 20 20 6e 61 70 70 65 6e  zFrom){.  nappen
315b9 64 28 73 62 2c 20 7a 46 72 6f 6d 2c 20 73 74 72  d(sb, zFrom, str
315ba 6c 65 6e 28 7a 46 72 6f 6d 29 29 3b 0a 7d 0a 0a  len(zFrom));.}..
315bb 2f 2a 20 41 70 70 65 6e 64 20 61 20 6c 69 73 74  /* Append a list
315bc 20 6f 66 20 73 74 72 69 6e 67 73 20 73 65 70 61   of strings sepa
315bd 72 61 74 65 64 20 62 79 20 63 6f 6d 6d 61 73 2e  rated by commas.
315be 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
315bf 61 70 70 65 6e 64 4c 69 73 74 28 53 74 72 69 6e  appendList(Strin
315c0 67 42 75 66 66 65 72 20 2a 73 62 2c 20 69 6e 74  gBuffer *sb, int
315c1 20 6e 53 74 72 69 6e 67 2c 20 63 68 61 72 20 2a   nString, char *
315c2 2a 61 7a 53 74 72 69 6e 67 29 7b 0a 20 20 69 6e  *azString){.  in
315c3 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
315c4 69 3c 6e 53 74 72 69 6e 67 3b 20 2b 2b 69 29 7b  i<nString; ++i){
315c5 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 20 61  .    if( i>0 ) a
315c6 70 70 65 6e 64 28 73 62 2c 20 22 2c 20 22 29 3b  ppend(sb, ", ");
315c7 0a 20 20 20 20 61 70 70 65 6e 64 28 73 62 2c 20  .    append(sb, 
315c8 61 7a 53 74 72 69 6e 67 5b 69 5d 29 3b 0a 20 20  azString[i]);.  
315c9 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  }.}..static int 
315ca 65 6e 64 73 49 6e 57 68 69 74 65 53 70 61 63 65  endsInWhiteSpace
315cb 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 70  (StringBuffer *p
315cc 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69  ){.  return stri
315cd 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 70  ngBufferLength(p
315ce 29 3e 30 20 26 26 0a 20 20 20 20 73 61 66 65 5f  )>0 &&.    safe_
315cf 69 73 73 70 61 63 65 28 73 74 72 69 6e 67 42 75  isspace(stringBu
315d0 66 66 65 72 44 61 74 61 28 70 29 5b 73 74 72 69  fferData(p)[stri
315d1 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 70  ngBufferLength(p
315d2 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f 2a 20 49 66 20  )-1]);.}../* If 
315d3 74 68 65 20 53 74 72 69 6e 67 42 75 66 66 65 72  the StringBuffer
315d4 20 65 6e 64 73 20 69 6e 20 73 6f 6d 65 74 68 69   ends in somethi
315d5 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 77 68  ng other than wh
315d6 69 74 65 20 73 70 61 63 65 2c 20 61 64 64 20 61  ite space, add a
315d7 0a 2a 2a 20 73 69 6e 67 6c 65 20 73 70 61 63 65  .** single space
315d8 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 74 68   character to th
315d9 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  e end..*/.static
315da 20 76 6f 69 64 20 61 70 70 65 6e 64 57 68 69 74   void appendWhit
315db 65 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66  eSpace(StringBuf
315dc 66 65 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 73  fer *p){.  if( s
315dd 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
315de 68 28 70 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  h(p)==0 ) return
315df 3b 0a 20 20 69 66 28 20 21 65 6e 64 73 49 6e 57  ;.  if( !endsInW
315e0 68 69 74 65 53 70 61 63 65 28 70 29 20 29 20 61  hiteSpace(p) ) a
315e1 70 70 65 6e 64 28 70 2c 20 22 20 22 29 3b 0a 7d  ppend(p, " ");.}
315e2 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 77 68 69 74  ../* Remove whit
315e3 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65  e space from the
315e4 20 65 6e 64 20 6f 66 20 74 68 65 20 53 74 72 69   end of the Stri
315e5 6e 67 42 75 66 66 65 72 20 2a 2f 0a 73 74 61 74  ngBuffer */.stat
315e6 69 63 20 76 6f 69 64 20 74 72 69 6d 57 68 69 74  ic void trimWhit
315e7 65 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66  eSpace(StringBuf
315e8 66 65 72 20 2a 70 29 7b 0a 20 20 77 68 69 6c 65  fer *p){.  while
315e9 28 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70 61  ( endsInWhiteSpa
315ea 63 65 28 70 29 20 29 7b 0a 20 20 20 20 70 2d 3e  ce(p) ){.    p->
315eb 62 2e 70 44 61 74 61 5b 2d 2d 70 2d 3e 62 2e 6e  b.pData[--p->b.n
315ec 44 61 74 61 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a  Data-1] = '\0';.
315ed 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
315ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
315ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
315f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
315f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
315f2 44 4c 52 65 61 64 65 72 20 69 73 20 75 73 65 64  DLReader is used
315f3 20 74 6f 20 72 65 61 64 20 64 6f 63 75 6d 65 6e   to read documen
315f4 74 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20  t elements from 
315f5 61 20 64 6f 63 6c 69 73 74 2e 20 20 54 68 65 0a  a doclist.  The.
315f6 2a 2a 20 63 75 72 72 65 6e 74 20 64 6f 63 69 64  ** current docid
315f7 20 69 73 20 63 61 63 68 65 64 2c 20 73 6f 20 64   is cached, so d
315f8 6c 72 44 6f 63 69 64 28 29 20 69 73 20 66 61 73  lrDocid() is fas
315f9 74 2e 20 20 44 4c 52 65 61 64 65 72 20 64 6f 65  t.  DLReader doe
315fa 73 20 6e 6f 74 0a 2a 2a 20 6f 77 6e 20 74 68 65  s not.** own the
315fb 20 64 6f 63 6c 69 73 74 20 62 75 66 66 65 72 2e   doclist buffer.
315fc 0a 2a 2a 0a 2a 2a 20 64 6c 72 41 74 45 6e 64 20  .**.** dlrAtEnd 
315fd 2d 20 74 72 75 65 20 69 66 20 74 68 65 72 65 27  - true if there'
315fe 73 20 6e 6f 20 6d 6f 72 65 20 64 61 74 61 20 74  s no more data t
315ff 6f 20 72 65 61 64 2e 0a 2a 2a 20 64 6c 72 44 6f  o read..** dlrDo
31600 63 69 64 20 2d 20 64 6f 63 69 64 20 6f 66 20 63  cid - docid of c
31601 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 2e  urrent document.
31602 0a 2a 2a 20 64 6c 72 44 6f 63 44 61 74 61 20 2d  .** dlrDocData -
31603 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66 6f   doclist data fo
31604 72 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65  r current docume
31605 6e 74 20 28 69 6e 63 6c 75 64 69 6e 67 20 64 6f  nt (including do
31606 63 69 64 29 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44  cid)..** dlrDocD
31607 61 74 61 42 79 74 65 73 20 2d 20 6c 65 6e 67 74  ataBytes - lengt
31608 68 20 6f 66 20 73 61 6d 65 2e 0a 2a 2a 20 64 6c  h of same..** dl
31609 72 41 6c 6c 44 61 74 61 42 79 74 65 73 20 2d 20  rAllDataBytes - 
3160a 6c 65 6e 67 74 68 20 6f 66 20 61 6c 6c 20 72 65  length of all re
3160b 6d 61 69 6e 69 6e 67 20 64 61 74 61 2e 0a 2a 2a  maining data..**
3160c 20 64 6c 72 50 6f 73 44 61 74 61 20 2d 20 70 6f   dlrPosData - po
3160d 73 69 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20  sition data for 
3160e 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74  current document
3160f 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61 4c  ..** dlrPosDataL
31610 65 6e 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 70  en - length of p
31611 6f 73 20 64 61 74 61 20 66 6f 72 20 63 75 72 72  os data for curr
31612 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20 28 69 6e  ent document (in
31613 63 6c 20 50 4f 53 5f 45 4e 44 29 2e 0a 2a 2a 20  cl POS_END)..** 
31614 64 6c 72 53 74 65 70 20 2d 20 73 74 65 70 20 74  dlrStep - step t
31615 6f 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65  o current docume
31616 6e 74 2e 0a 2a 2a 20 64 6c 72 49 6e 69 74 20 2d  nt..** dlrInit -
31617 20 69 6e 69 74 69 61 6c 20 66 6f 72 20 64 6f 63   initial for doc
31618 6c 69 73 74 20 6f 66 20 67 69 76 65 6e 20 74 79  list of given ty
31619 70 65 20 61 67 61 69 6e 73 74 20 67 69 76 65 6e  pe against given
3161a 20 64 61 74 61 2e 0a 2a 2a 20 64 6c 72 44 65 73   data..** dlrDes
3161b 74 72 6f 79 20 2d 20 63 6c 65 61 6e 20 75 70 2e  troy - clean up.
3161c 0a 2a 2a 0a 2a 2a 20 45 78 70 65 63 74 65 64 20  .**.** Expected 
3161d 75 73 61 67 65 20 69 73 20 73 6f 6d 65 74 68 69  usage is somethi
3161e 6e 67 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20  ng like:.**.**  
3161f 20 44 4c 52 65 61 64 65 72 20 72 65 61 64 65 72   DLReader reader
31620 3b 0a 2a 2a 20 20 20 64 6c 72 49 6e 69 74 28 26  ;.**   dlrInit(&
31621 72 65 61 64 65 72 2c 20 70 44 61 74 61 2c 20 6e  reader, pData, n
31622 44 61 74 61 29 3b 0a 2a 2a 20 20 20 77 68 69 6c  Data);.**   whil
31623 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26 72 65  e( !dlrAtEnd(&re
31624 61 64 65 72 29 20 29 7b 0a 2a 2a 20 20 20 20 20  ader) ){.**     
31625 2f 2f 20 63 61 6c 6c 73 20 74 6f 20 64 6c 72 44  // calls to dlrD
31626 6f 63 69 64 28 29 20 61 6e 64 20 6b 69 6e 2e 0a  ocid() and kin..
31627 2a 2a 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  **     dlrStep(&
31628 72 65 61 64 65 72 29 3b 0a 2a 2a 20 20 20 7d 0a  reader);.**   }.
31629 2a 2a 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28  **   dlrDestroy(
3162a 26 72 65 61 64 65 72 29 3b 0a 2a 2f 0a 74 79 70  &reader);.*/.typ
3162b 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 52 65  edef struct DLRe
3162c 61 64 65 72 20 7b 0a 20 20 44 6f 63 4c 69 73 74  ader {.  DocList
3162d 54 79 70 65 20 69 54 79 70 65 3b 0a 20 20 63 6f  Type iType;.  co
3162e 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b  nst char *pData;
3162f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20  .  int nData;.. 
31630 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
31631 6f 63 69 64 3b 0a 20 20 69 6e 74 20 6e 45 6c 65  ocid;.  int nEle
31632 6d 65 6e 74 3b 0a 7d 20 44 4c 52 65 61 64 65 72  ment;.} DLReader
31633 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c  ;..static int dl
31634 72 41 74 45 6e 64 28 44 4c 52 65 61 64 65 72 20  rAtEnd(DLReader 
31635 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
31636 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 44  ert( pReader->nD
31637 61 74 61 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75  ata>=0 );.  retu
31638 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  rn pReader->nDat
31639 61 3d 3d 30 3b 0a 7d 0a 73 74 61 74 69 63 20 73  a==0;.}.static s
3163a 71 6c 69 74 65 5f 69 6e 74 36 34 20 64 6c 72 44  qlite_int64 dlrD
3163b 6f 63 69 64 28 44 4c 52 65 61 64 65 72 20 2a 70  ocid(DLReader *p
3163c 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
3163d 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65  t( !dlrAtEnd(pRe
3163e 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
3163f 6e 20 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69  n pReader->iDoci
31640 64 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73  d;.}.static cons
31641 74 20 63 68 61 72 20 2a 64 6c 72 44 6f 63 44 61  t char *dlrDocDa
31642 74 61 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65  ta(DLReader *pRe
31643 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
31644 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64   !dlrAtEnd(pRead
31645 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  er) );.  return 
31646 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 3b 0a  pReader->pData;.
31647 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72  }.static int dlr
31648 44 6f 63 44 61 74 61 42 79 74 65 73 28 44 4c 52  DocDataBytes(DLR
31649 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
3164a 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
3164b 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
3164c 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
3164d 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a 7d 0a 73  r->nElement;.}.s
3164e 74 61 74 69 63 20 69 6e 74 20 64 6c 72 41 6c 6c  tatic int dlrAll
3164f 44 61 74 61 42 79 74 65 73 28 44 4c 52 65 61 64  DataBytes(DLRead
31650 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
31651 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e  assert( !dlrAtEn
31652 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
31653 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
31654 6e 44 61 74 61 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f  nData;.}./* TODO
31655 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72  (shess) Consider
31656 20 61 64 64 69 6e 67 20 61 20 66 69 65 6c 64 20   adding a field 
31657 74 6f 20 74 72 61 63 6b 20 69 44 6f 63 69 64 20  to track iDocid 
31658 76 61 72 69 6e 74 20 6c 65 6e 67 74 68 0a 2a 2a  varint length.**
31659 20 74 6f 20 6d 61 6b 65 20 74 68 65 73 65 20 74   to make these t
3165a 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 66 61 73  wo functions fas
3165b 74 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74  ter.  This might
3165c 20 6d 61 74 74 65 72 20 28 61 20 74 69 6e 79 20   matter (a tiny 
3165d 62 69 74 29 0a 2a 2a 20 66 6f 72 20 71 75 65 72  bit).** for quer
3165e 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ies..*/.static c
3165f 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72 50 6f  onst char *dlrPo
31660 73 44 61 74 61 28 44 4c 52 65 61 64 65 72 20 2a  sData(DLReader *
31661 70 52 65 61 64 65 72 29 7b 0a 20 20 73 71 6c 69  pReader){.  sqli
31662 74 65 5f 69 6e 74 36 34 20 69 44 75 6d 6d 79 3b  te_int64 iDummy;
31663 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 47  .  int n = fts3G
31664 65 74 56 61 72 69 6e 74 28 70 52 65 61 64 65 72  etVarint(pReader
31665 2d 3e 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79  ->pData, &iDummy
31666 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  );.  assert( !dl
31667 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
31668 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
31669 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b 0a 7d 0a  der->pData+n;.}.
3166a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72 50 6f  static int dlrPo
3166b 73 44 61 74 61 4c 65 6e 28 44 4c 52 65 61 64 65  sDataLen(DLReade
3166c 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 73  r *pReader){.  s
3166d 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75 6d  qlite_int64 iDum
3166e 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74  my;.  int n = ft
3166f 73 33 47 65 74 56 61 72 69 6e 74 28 70 52 65 61  s3GetVarint(pRea
31670 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 75  der->pData, &iDu
31671 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
31672 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
31673 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
31674 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74  Reader->nElement
31675 2d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  -n;.}.static voi
31676 64 20 64 6c 72 53 74 65 70 28 44 4c 52 65 61 64  d dlrStep(DLRead
31677 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
31678 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e  assert( !dlrAtEn
31679 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20  d(pReader) );.. 
3167a 20 2f 2a 20 53 6b 69 70 20 70 61 73 74 20 63 75   /* Skip past cu
3167b 72 72 65 6e 74 20 64 6f 63 6c 69 73 74 20 65 6c  rrent doclist el
3167c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 61 73 73 65  ement. */.  asse
3167d 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c  rt( pReader->nEl
3167e 65 6d 65 6e 74 3c 3d 70 52 65 61 64 65 72 2d 3e  ement<=pReader->
3167f 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64  nData );.  pRead
31680 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 70 52 65  er->pData += pRe
31681 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a  ader->nElement;.
31682 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
31683 20 2d 3d 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c   -= pReader->nEl
31684 65 6d 65 6e 74 3b 0a 0a 20 20 2f 2a 20 49 66 20  ement;..  /* If 
31685 74 68 65 72 65 20 69 73 20 6d 6f 72 65 20 64 61  there is more da
31686 74 61 2c 20 72 65 61 64 20 74 68 65 20 6e 65 78  ta, read the nex
31687 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e  t doclist elemen
31688 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61  t. */.  if( pRea
31689 64 65 72 2d 3e 6e 44 61 74 61 21 3d 30 20 29 7b  der->nData!=0 ){
3168a 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
3168b 34 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20  4 iDocidDelta;. 
3168c 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 2c 20 6e     int iDummy, n
3168d 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
3168e 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
3168f 20 26 69 44 6f 63 69 64 44 65 6c 74 61 29 3b 0a   &iDocidDelta);.
31690 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 44 6f      pReader->iDo
31691 63 69 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c  cid += iDocidDel
31692 74 61 3b 0a 20 20 20 20 69 66 28 20 70 52 65 61  ta;.    if( pRea
31693 64 65 72 2d 3e 69 54 79 70 65 3e 3d 44 4c 5f 50  der->iType>=DL_P
31694 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20  OSITIONS ){.    
31695 20 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65 61    assert( n<pRea
31696 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
31697 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a      while( 1 ){.
31698 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73          n += fts
31699 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
3169a 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
3169b 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20  iDummy);.       
3169c 20 61 73 73 65 72 74 28 20 6e 3c 3d 70 52 65 61   assert( n<=pRea
3169d 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
3169e 20 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79        if( iDummy
3169f 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62 72 65 61  ==POS_END ) brea
316a0 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  k;.        if( i
316a1 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f 4c 55 4d  Dummy==POS_COLUM
316a2 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  N ){.          n
316a3 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
316a4 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
316a5 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  ta+n, &iDummy);.
316a6 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
316a7 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61  ( n<pReader->nDa
316a8 74 61 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  ta );.        }e
316a9 6c 73 65 20 69 66 28 20 70 52 65 61 64 65 72 2d  lse if( pReader-
316aa 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54  >iType==DL_POSIT
316ab 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a  IONS_OFFSETS ){.
316ac 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66            n += f
316ad 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
316ae 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
316af 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20   &iDummy);.     
316b0 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65       n += fts3Ge
316b1 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
316b2 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  r->pData+n, &iDu
316b3 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  mmy);.          
316b4 61 73 73 65 72 74 28 20 6e 3c 70 52 65 61 64 65  assert( n<pReade
316b5 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  r->nData );.    
316b6 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
316b7 20 20 7d 0a 20 20 20 20 70 52 65 61 64 65 72 2d    }.    pReader-
316b8 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 6e 3b 0a 20  >nElement = n;. 
316b9 20 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64     assert( pRead
316ba 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52  er->nElement<=pR
316bb 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
316bc 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
316bd 64 20 64 6c 72 49 6e 69 74 28 44 4c 52 65 61 64  d dlrInit(DLRead
316be 65 72 20 2a 70 52 65 61 64 65 72 2c 20 44 6f 63  er *pReader, Doc
316bf 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 0a  ListType iType,.
316c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316c1 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
316c2 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
316c3 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  ){.  assert( pDa
316c4 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e 44 61 74  ta!=NULL && nDat
316c5 61 21 3d 30 20 29 3b 0a 20 20 70 52 65 61 64 65  a!=0 );.  pReade
316c6 72 2d 3e 69 54 79 70 65 20 3d 20 69 54 79 70 65  r->iType = iType
316c7 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  ;.  pReader->pDa
316c8 74 61 20 3d 20 70 44 61 74 61 3b 0a 20 20 70 52  ta = pData;.  pR
316c9 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e  eader->nData = n
316ca 44 61 74 61 3b 0a 20 20 70 52 65 61 64 65 72 2d  Data;.  pReader-
316cb 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 30 3b 0a 20  >nElement = 0;. 
316cc 20 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64   pReader->iDocid
316cd 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64   = 0;..  /* Load
316ce 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   the first eleme
316cf 6e 74 27 73 20 64 61 74 61 2e 20 20 54 68 65 72  nt's data.  Ther
316d0 65 20 6d 75 73 74 20 62 65 20 61 20 66 69 72 73  e must be a firs
316d1 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  t element. */.  
316d2 64 6c 72 53 74 65 70 28 70 52 65 61 64 65 72 29  dlrStep(pReader)
316d3 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
316d4 64 6c 72 44 65 73 74 72 6f 79 28 44 4c 52 65 61  dlrDestroy(DLRea
316d5 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
316d6 20 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65   SCRAMBLE(pReade
316d7 72 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e  r);.}..#ifndef N
316d8 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79 20  DEBUG./* Verify 
316d9 74 68 61 74 20 74 68 65 20 64 6f 63 6c 69 73 74  that the doclist
316da 20 63 61 6e 20 62 65 20 76 61 6c 69 64 6c 79 20   can be validly 
316db 64 65 63 6f 64 65 64 2e 20 20 41 6c 73 6f 20 72  decoded.  Also r
316dc 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6c 61  eturns the.** la
316dd 73 74 20 64 6f 63 69 64 20 66 6f 75 6e 64 20 62  st docid found b
316de 65 63 61 75 73 65 20 69 74 20 69 73 20 63 6f 6e  ecause it is con
316df 76 65 6e 69 65 6e 74 20 69 6e 20 6f 74 68 65 72  venient in other
316e0 20 61 73 73 65 72 74 69 6f 6e 73 20 66 6f 72 0a   assertions for.
316e1 2a 2a 20 44 4c 57 72 69 74 65 72 2e 0a 2a 2f 0a  ** DLWriter..*/.
316e2 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
316e3 69 73 74 56 61 6c 69 64 61 74 65 28 44 6f 63 4c  istValidate(DocL
316e4 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 63  istType iType, c
316e5 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
316e6 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20  , int nData,.   
316e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
316e8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
316e9 69 6e 74 36 34 20 2a 70 4c 61 73 74 44 6f 63 69  int64 *pLastDoci
316ea 64 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  d){.  sqlite_int
316eb 36 34 20 69 50 72 65 76 44 6f 63 69 64 20 3d 20  64 iPrevDocid = 
316ec 30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  0;.  assert( nDa
316ed 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>0 );.  assert
316ee 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20 20  ( pData!=0 );.  
316ef 61 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e 44  assert( pData+nD
316f0 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 20 20 77  ata>pData );.  w
316f1 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30 20 29  hile( nData!=0 )
316f2 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74  {.    sqlite_int
316f3 36 34 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a  64 iDocidDelta;.
316f4 20 20 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33      int n = fts3
316f5 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c  GetVarint(pData,
316f6 20 26 69 44 6f 63 69 64 44 65 6c 74 61 29 3b 0a   &iDocidDelta);.
316f7 20 20 20 20 69 50 72 65 76 44 6f 63 69 64 20 2b      iPrevDocid +
316f8 3d 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20  = iDocidDelta;. 
316f9 20 20 20 69 66 28 20 69 54 79 70 65 3e 44 4c 5f     if( iType>DL_
316fa 44 4f 43 49 44 53 20 29 7b 0a 20 20 20 20 20 20  DOCIDS ){.      
316fb 69 6e 74 20 69 44 75 6d 6d 79 3b 0a 20 20 20 20  int iDummy;.    
316fc 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20    while( 1 ){.  
316fd 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47        n += fts3G
316fe 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
316ff 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  +n, &iDummy);.  
31700 20 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79        if( iDummy
31701 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62 72 65 61  ==POS_END ) brea
31702 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  k;.        if( i
31703 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f 4c 55 4d  Dummy==POS_COLUM
31704 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  N ){.          n
31705 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
31706 74 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44  t32(pData+n, &iD
31707 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d  ummy);.        }
31708 65 6c 73 65 20 69 66 28 20 69 54 79 70 65 3e 44  else if( iType>D
31709 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20  L_POSITIONS ){. 
3170a 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74           n += ft
3170b 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
3170c 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b  ata+n, &iDummy);
3170d 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  .          n += 
3170e 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
3170f 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
31710 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
31711 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d       assert( n<=
31712 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 7d  nData );.      }
31713 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
31714 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20  t( n<=nData );. 
31715 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20     pData += n;. 
31716 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 20     nData -= n;. 
31717 20 7d 0a 20 20 69 66 28 20 70 4c 61 73 74 44 6f   }.  if( pLastDo
31718 63 69 64 20 29 20 2a 70 4c 61 73 74 44 6f 63 69  cid ) *pLastDoci
31719 64 20 3d 20 69 50 72 65 76 44 6f 63 69 64 3b 0a  d = iPrevDocid;.
3171a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54  }.#define ASSERT
3171b 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69  _VALID_DOCLIST(i
3171c 2c 20 70 2c 20 6e 2c 20 6f 29 20 64 6f 63 4c 69  , p, n, o) docLi
3171d 73 74 56 61 6c 69 64 61 74 65 28 69 2c 20 70 2c  stValidate(i, p,
3171e 20 6e 2c 20 6f 29 0a 23 65 6c 73 65 0a 23 64 65   n, o).#else.#de
3171f 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49  fine ASSERT_VALI
31720 44 5f 44 4f 43 4c 49 53 54 28 69 2c 20 70 2c 20  D_DOCLIST(i, p, 
31721 6e 2c 20 6f 29 20 61 73 73 65 72 74 28 20 31 20  n, o) assert( 1 
31722 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ).#endif../*****
31723 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31724 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31725 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31726 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
31727 2f 2a 20 44 4c 57 72 69 74 65 72 20 69 73 20 75  /* DLWriter is u
31728 73 65 64 20 74 6f 20 77 72 69 74 65 20 64 6f 63  sed to write doc
31729 6c 69 73 74 20 64 61 74 61 20 74 6f 20 61 20 44  list data to a D
3172a 61 74 61 42 75 66 66 65 72 2e 20 20 44 4c 57 72  ataBuffer.  DLWr
3172b 69 74 65 72 0a 2a 2a 20 61 6c 77 61 79 73 20 61  iter.** always a
3172c 70 70 65 6e 64 73 20 74 6f 20 74 68 65 20 62 75  ppends to the bu
3172d 66 66 65 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ffer and does no
3172e 74 20 6f 77 6e 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  t own it..**.** 
3172f 64 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 69 61  dlwInit - initia
31730 6c 69 7a 65 20 74 6f 20 77 72 69 74 65 20 61 20  lize to write a 
31731 67 69 76 65 6e 20 74 79 70 65 20 64 6f 63 6c 69  given type docli
31732 73 74 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a  stto a buffer..*
31733 2a 20 64 6c 77 44 65 73 74 72 6f 79 20 2d 20 63  * dlwDestroy - c
31734 6c 65 61 72 20 74 68 65 20 77 72 69 74 65 72 27  lear the writer'
31735 73 20 6d 65 6d 6f 72 79 2e 20 20 44 6f 65 73 20  s memory.  Does 
31736 6e 6f 74 20 66 72 65 65 20 62 75 66 66 65 72 2e  not free buffer.
31737 0a 2a 2a 20 64 6c 77 41 70 70 65 6e 64 20 2d 20  .** dlwAppend - 
31738 61 70 70 65 6e 64 20 72 61 77 20 64 6f 63 6c 69  append raw docli
31739 73 74 20 64 61 74 61 20 74 6f 20 62 75 66 66 65  st data to buffe
3173a 72 2e 0a 2a 2a 20 64 6c 77 43 6f 70 79 20 2d 20  r..** dlwCopy - 
3173b 63 6f 70 79 20 6e 65 78 74 20 64 6f 63 6c 69 73  copy next doclis
3173c 74 20 66 72 6f 6d 20 72 65 61 64 65 72 20 74 6f  t from reader to
3173d 20 77 72 69 74 65 72 2e 0a 2a 2a 20 64 6c 77 41   writer..** dlwA
3173e 64 64 20 2d 20 63 6f 6e 73 74 72 75 63 74 20 64  dd - construct d
3173f 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 20 61  oclist element a
31740 6e 64 20 61 70 70 65 6e 64 20 74 6f 20 62 75 66  nd append to buf
31741 66 65 72 2e 0a 2a 2a 20 20 20 20 4f 6e 6c 79 20  fer..**    Only 
31742 61 70 70 6c 79 20 64 6c 77 41 64 64 28 29 20 74  apply dlwAdd() t
31743 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c  o DL_DOCIDS docl
31744 69 73 74 73 20 28 65 6c 73 65 20 75 73 65 20 50  ists (else use P
31745 4c 57 72 69 74 65 72 29 2e 0a 2a 2f 0a 74 79 70  LWriter)..*/.typ
31746 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 57 72  edef struct DLWr
31747 69 74 65 72 20 7b 0a 20 20 44 6f 63 4c 69 73 74  iter {.  DocList
31748 54 79 70 65 20 69 54 79 70 65 3b 0a 20 20 44 61  Type iType;.  Da
31749 74 61 42 75 66 66 65 72 20 2a 62 3b 0a 20 20 73  taBuffer *b;.  s
3174a 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72 65  qlite_int64 iPre
3174b 76 44 6f 63 69 64 3b 0a 23 69 66 6e 64 65 66 20  vDocid;.#ifndef 
3174c 4e 44 45 42 55 47 0a 20 20 69 6e 74 20 68 61 73  NDEBUG.  int has
3174d 5f 69 50 72 65 76 44 6f 63 69 64 3b 0a 23 65 6e  _iPrevDocid;.#en
3174e 64 69 66 0a 7d 20 44 4c 57 72 69 74 65 72 3b 0a  dif.} DLWriter;.
3174f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 77  .static void dlw
31750 49 6e 69 74 28 44 4c 57 72 69 74 65 72 20 2a 70  Init(DLWriter *p
31751 57 72 69 74 65 72 2c 20 44 6f 63 4c 69 73 74 54  Writer, DocListT
31752 79 70 65 20 69 54 79 70 65 2c 20 44 61 74 61 42  ype iType, DataB
31753 75 66 66 65 72 20 2a 62 29 7b 0a 20 20 70 57 72  uffer *b){.  pWr
31754 69 74 65 72 2d 3e 62 20 3d 20 62 3b 0a 20 20 70  iter->b = b;.  p
31755 57 72 69 74 65 72 2d 3e 69 54 79 70 65 20 3d 20  Writer->iType = 
31756 69 54 79 70 65 3b 0a 20 20 70 57 72 69 74 65 72  iType;.  pWriter
31757 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20 30  ->iPrevDocid = 0
31758 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
31759 0a 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f  .  pWriter->has_
3175a 69 50 72 65 76 44 6f 63 69 64 20 3d 20 30 3b 0a  iPrevDocid = 0;.
3175b 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63 20  #endif.}.static 
3175c 76 6f 69 64 20 64 6c 77 44 65 73 74 72 6f 79 28  void dlwDestroy(
3175d 44 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  DLWriter *pWrite
3175e 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70  r){.  SCRAMBLE(p
3175f 57 72 69 74 65 72 29 3b 0a 7d 0a 2f 2a 20 69 46  Writer);.}./* iF
31760 69 72 73 74 44 6f 63 69 64 20 69 73 20 74 68 65  irstDocid is the
31761 20 66 69 72 73 74 20 64 6f 63 69 64 20 69 6e 20   first docid in 
31762 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 20 70  the doclist in p
31763 44 61 74 61 2e 20 20 49 74 20 69 73 0a 2a 2a 20  Data.  It is.** 
31764 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70  needed because p
31765 44 61 74 61 20 6d 61 79 20 70 6f 69 6e 74 20 77  Data may point w
31766 69 74 68 69 6e 20 61 20 6c 61 72 67 65 72 20 64  ithin a larger d
31767 6f 63 6c 69 73 74 2c 20 69 6e 20 77 68 69 63 68  oclist, in which
31768 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 66 69 72  .** case the fir
31769 73 74 20 69 74 65 6d 20 77 6f 75 6c 64 20 62 65  st item would be
3176a 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 0a   delta-encoded..
3176b 2a 2a 0a 2a 2a 20 69 4c 61 73 74 44 6f 63 69 64  **.** iLastDocid
3176c 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 64 6f   is the final do
3176d 63 69 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69  cid in the docli
3176e 73 74 20 69 6e 20 70 44 61 74 61 2e 20 20 49 74  st in pData.  It
3176f 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f   is.** needed to
31770 20 63 72 65 61 74 65 20 74 68 65 20 6e 65 77 20   create the new 
31771 69 50 72 65 76 44 6f 63 69 64 20 66 6f 72 20 66  iPrevDocid for f
31772 75 74 75 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f  uture delta-enco
31773 64 69 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f  ding.  The.** co
31774 64 65 20 63 6f 75 6c 64 20 64 65 63 6f 64 65 20  de could decode 
31775 74 68 65 20 70 61 73 73 65 64 20 64 6f 63 6c 69  the passed docli
31776 73 74 20 74 6f 20 72 65 63 72 65 61 74 65 20 69  st to recreate i
31777 4c 61 73 74 44 6f 63 69 64 2c 20 62 75 74 0a 2a  LastDocid, but.*
31778 2a 20 74 68 65 20 6f 6e 6c 79 20 63 75 72 72 65  * the only curre
31779 6e 74 20 75 73 65 72 20 28 64 6f 63 4c 69 73 74  nt user (docList
3177a 4d 65 72 67 65 29 20 61 6c 72 65 61 64 79 20 68  Merge) already h
3177b 61 73 20 64 65 63 6f 64 65 64 20 74 68 69 73 0a  as decoded this.
3177c 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  ** information..
3177d 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  */./* TODO(shess
3177e 29 20 54 68 69 73 20 68 61 73 20 62 65 63 6f 6d  ) This has becom
3177f 65 20 6a 75 73 74 20 61 20 68 65 6c 70 65 72 20  e just a helper 
31780 66 6f 72 20 64 6f 63 4c 69 73 74 4d 65 72 67 65  for docListMerge
31781 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61 20  ..** Consider a 
31782 72 65 66 61 63 74 6f 72 20 74 6f 20 6d 61 6b 65  refactor to make
31783 20 74 68 69 73 20 63 6c 65 61 6e 65 72 2e 0a 2a   this cleaner..*
31784 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c  /.static void dl
31785 77 41 70 70 65 6e 64 28 44 4c 57 72 69 74 65 72  wAppend(DLWriter
31786 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20   *pWriter,.     
31787 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31788 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
31789 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20  ta, int nData,. 
3178a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3178b 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
3178c 34 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20 73  4 iFirstDocid, s
3178d 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73  qlite_int64 iLas
3178e 74 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74  tDocid){.  sqlit
3178f 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 20 3d  e_int64 iDocid =
31790 20 30 3b 0a 20 20 63 68 61 72 20 63 5b 56 41 52   0;.  char c[VAR
31791 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20  INT_MAX];.  int 
31792 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 46 69 72 73  nFirstOld, nFirs
31793 74 4e 65 77 3b 20 20 20 20 20 2f 2a 20 4f 6c 64  tNew;     /* Old
31794 20 61 6e 64 20 6e 65 77 20 76 61 72 69 6e 74 20   and new varint 
31795 6c 65 6e 20 6f 66 20 66 69 72 73 74 20 64 6f 63  len of first doc
31796 69 64 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  id. */.#ifndef N
31797 44 45 42 55 47 0a 20 20 73 71 6c 69 74 65 5f 69  DEBUG.  sqlite_i
31798 6e 74 36 34 20 69 4c 61 73 74 44 6f 63 69 64 44  nt64 iLastDocidD
31799 65 6c 74 61 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  elta;.#endif..  
3179a 2f 2a 20 52 65 63 6f 64 65 20 74 68 65 20 69 6e  /* Recode the in
3179b 69 74 69 61 6c 20 64 6f 63 69 64 20 61 73 20 64  itial docid as d
3179c 65 6c 74 61 20 66 72 6f 6d 20 69 50 72 65 76 44  elta from iPrevD
3179d 6f 63 69 64 2e 20 2a 2f 0a 20 20 6e 46 69 72 73  ocid. */.  nFirs
3179e 74 4f 6c 64 20 3d 20 66 74 73 33 47 65 74 56 61  tOld = fts3GetVa
3179f 72 69 6e 74 28 70 44 61 74 61 2c 20 26 69 44 6f  rint(pData, &iDo
317a0 63 69 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20  cid);.  assert( 
317a1 6e 46 69 72 73 74 4f 6c 64 3c 6e 44 61 74 61 20  nFirstOld<nData 
317a2 7c 7c 20 28 6e 46 69 72 73 74 4f 6c 64 3d 3d 6e  || (nFirstOld==n
317a3 44 61 74 61 20 26 26 20 70 57 72 69 74 65 72 2d  Data && pWriter-
317a4 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44  >iType==DL_DOCID
317a5 53 29 20 29 3b 0a 20 20 6e 46 69 72 73 74 4e 65  S) );.  nFirstNe
317a6 77 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  w = fts3PutVarin
317a7 74 28 63 2c 20 69 46 69 72 73 74 44 6f 63 69 64  t(c, iFirstDocid
317a8 2d 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44  -pWriter->iPrevD
317a9 6f 63 69 64 29 3b 0a 0a 20 20 2f 2a 20 56 65 72  ocid);..  /* Ver
317aa 69 66 79 20 74 68 61 74 20 74 68 65 20 69 6e 63  ify that the inc
317ab 6f 6d 69 6e 67 20 64 6f 63 6c 69 73 74 20 69 73  oming doclist is
317ac 20 76 61 6c 69 64 20 41 4e 44 20 74 68 61 74 20   valid AND that 
317ad 69 74 20 65 6e 64 73 20 77 69 74 68 0a 20 20 2a  it ends with.  *
317ae 2a 20 74 68 65 20 65 78 70 65 63 74 65 64 20 64  * the expected d
317af 6f 63 69 64 2e 20 20 54 68 69 73 20 69 73 20 65  ocid.  This is e
317b0 73 73 65 6e 74 69 61 6c 20 62 65 63 61 75 73 65  ssential because
317b1 20 77 65 27 6c 6c 20 74 72 75 73 74 20 74 68 69   we'll trust thi
317b2 73 0a 20 20 2a 2a 20 64 6f 63 69 64 20 69 6e 20  s.  ** docid in 
317b3 66 75 74 75 72 65 20 64 65 6c 74 61 2d 65 6e 63  future delta-enc
317b4 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 41 53  oding..  */.  AS
317b5 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49  SERT_VALID_DOCLI
317b6 53 54 28 70 57 72 69 74 65 72 2d 3e 69 54 79 70  ST(pWriter->iTyp
317b7 65 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  e, pData, nData,
317b8 20 26 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74   &iLastDocidDelt
317b9 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c  a);.  assert( iL
317ba 61 73 74 44 6f 63 69 64 3d 3d 69 46 69 72 73 74  astDocid==iFirst
317bb 44 6f 63 69 64 2d 69 44 6f 63 69 64 2b 69 4c 61  Docid-iDocid+iLa
317bc 73 74 44 6f 63 69 64 44 65 6c 74 61 20 29 3b 0a  stDocidDelta );.
317bd 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 72 65 63  .  /* Append rec
317be 6f 64 65 64 20 69 6e 69 74 69 61 6c 20 64 6f 63  oded initial doc
317bf 69 64 20 61 6e 64 20 65 76 65 72 79 74 68 69 6e  id and everythin
317c0 67 20 65 6c 73 65 2e 20 20 52 65 73 74 20 6f 66  g else.  Rest of
317c1 20 64 6f 63 69 64 73 0a 20 20 2a 2a 20 73 68 6f   docids.  ** sho
317c2 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 64 65  uld have been de
317c3 6c 74 61 2d 65 6e 63 6f 64 65 64 20 66 72 6f 6d  lta-encoded from
317c4 20 70 72 65 76 69 6f 75 73 20 69 6e 69 74 69 61   previous initia
317c5 6c 20 64 6f 63 69 64 2e 0a 20 20 2a 2f 0a 20 20  l docid..  */.  
317c6 69 66 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e 44  if( nFirstOld<nD
317c7 61 74 61 20 29 7b 0a 20 20 20 20 64 61 74 61 42  ata ){.    dataB
317c8 75 66 66 65 72 41 70 70 65 6e 64 32 28 70 57 72  ufferAppend2(pWr
317c9 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 46 69 72  iter->b, c, nFir
317ca 73 74 4e 65 77 2c 0a 20 20 20 20 20 20 20 20 20  stNew,.         
317cb 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 61               pDa
317cc 74 61 2b 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 44  ta+nFirstOld, nD
317cd 61 74 61 2d 6e 46 69 72 73 74 4f 6c 64 29 3b 0a  ata-nFirstOld);.
317ce 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61 74    }else{.    dat
317cf 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57  aBufferAppend(pW
317d0 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 46 69  riter->b, c, nFi
317d1 72 73 74 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 70  rstNew);.  }.  p
317d2 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63  Writer->iPrevDoc
317d3 69 64 20 3d 20 69 4c 61 73 74 44 6f 63 69 64 3b  id = iLastDocid;
317d4 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
317d5 6c 77 43 6f 70 79 28 44 4c 57 72 69 74 65 72 20  lwCopy(DLWriter 
317d6 2a 70 57 72 69 74 65 72 2c 20 44 4c 52 65 61 64  *pWriter, DLRead
317d7 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
317d8 64 6c 77 41 70 70 65 6e 64 28 70 57 72 69 74 65  dlwAppend(pWrite
317d9 72 2c 20 64 6c 72 44 6f 63 44 61 74 61 28 70 52  r, dlrDocData(pR
317da 65 61 64 65 72 29 2c 20 64 6c 72 44 6f 63 44 61  eader), dlrDocDa
317db 74 61 42 79 74 65 73 28 70 52 65 61 64 65 72 29  taBytes(pReader)
317dc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 6c  ,.            dl
317dd 72 44 6f 63 69 64 28 70 52 65 61 64 65 72 29 2c  rDocid(pReader),
317de 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61 64 65   dlrDocid(pReade
317df 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  r));.}.static vo
317e0 69 64 20 64 6c 77 41 64 64 28 44 4c 57 72 69 74  id dlwAdd(DLWrit
317e1 65 72 20 2a 70 57 72 69 74 65 72 2c 20 73 71 6c  er *pWriter, sql
317e2 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
317e3 29 7b 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49  ){.  char c[VARI
317e4 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e  NT_MAX];.  int n
317e5 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
317e6 28 63 2c 20 69 44 6f 63 69 64 2d 70 57 72 69 74  (c, iDocid-pWrit
317e7 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b  er->iPrevDocid);
317e8 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75  ..  /* Docids mu
317e9 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20  st ascend. */.  
317ea 61 73 73 65 72 74 28 20 21 70 57 72 69 74 65 72  assert( !pWriter
317eb 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  ->has_iPrevDocid
317ec 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57 72 69 74   || iDocid>pWrit
317ed 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 29  er->iPrevDocid )
317ee 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69  ;.  assert( pWri
317ef 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44  ter->iType==DL_D
317f0 4f 43 49 44 53 20 29 3b 0a 0a 20 20 64 61 74 61  OCIDS );..  data
317f1 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72  BufferAppend(pWr
317f2 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a  iter->b, c, n);.
317f3 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76    pWriter->iPrev
317f4 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a  Docid = iDocid;.
317f5 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
317f6 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 69 50   pWriter->has_iP
317f7 72 65 76 44 6f 63 69 64 20 3d 20 31 3b 0a 23 65  revDocid = 1;.#e
317f8 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ndif.}../*******
317f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
317fd 20 50 4c 52 65 61 64 65 72 20 69 73 20 75 73 65   PLReader is use
317fe 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 66  d to read data f
317ff 72 6f 6d 20 61 20 64 6f 63 75 6d 65 6e 74 27 73  rom a document's
31800 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2e 20   position list. 
31801 20 41 73 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65   As.** the calle
31802 72 20 73 74 65 70 73 20 74 68 72 6f 75 67 68 20  r steps through 
31803 74 68 65 20 6c 69 73 74 2c 20 64 61 74 61 20 69  the list, data i
31804 73 20 63 61 63 68 65 64 20 73 6f 20 74 68 61 74  s cached so that
31805 20 76 61 72 69 6e 74 73 0a 2a 2a 20 6f 6e 6c 79   varints.** only
31806 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65 63 6f   need to be deco
31807 64 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  ded once..**.** 
31808 70 6c 72 49 6e 69 74 2c 20 70 6c 72 44 65 73 74  plrInit, plrDest
31809 72 6f 79 20 2d 20 63 72 65 61 74 65 2f 64 65 73  roy - create/des
3180a 74 72 6f 79 20 61 20 72 65 61 64 65 72 2e 0a 2a  troy a reader..*
3180b 2a 20 70 6c 72 43 6f 6c 75 6d 6e 2c 20 70 6c 72  * plrColumn, plr
3180c 50 6f 73 69 74 69 6f 6e 2c 20 70 6c 72 53 74 61  Position, plrSta
3180d 72 74 4f 66 66 73 65 74 2c 20 70 6c 72 45 6e 64  rtOffset, plrEnd
3180e 4f 66 66 73 65 74 20 2d 20 61 63 63 65 73 73 6f  Offset - accesso
3180f 72 73 0a 2a 2a 20 70 6c 72 41 74 45 6e 64 20 2d  rs.** plrAtEnd -
31810 20 61 74 20 65 6e 64 20 6f 66 20 73 74 72 65 61   at end of strea
31811 6d 2c 20 6f 6e 6c 79 20 63 61 6c 6c 20 70 6c 72  m, only call plr
31812 44 65 73 74 72 6f 79 20 6f 6e 63 65 20 74 72 75  Destroy once tru
31813 65 2e 0a 2a 2a 20 70 6c 72 53 74 65 70 20 2d 20  e..** plrStep - 
31814 73 74 65 70 20 74 6f 20 74 68 65 20 6e 65 78 74  step to the next
31815 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 74 79 70   element..*/.typ
31816 65 64 65 66 20 73 74 72 75 63 74 20 50 4c 52 65  edef struct PLRe
31817 61 64 65 72 20 7b 0a 20 20 2f 2a 20 54 68 65 73  ader {.  /* Thes
31818 65 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 6e  e refer to the n
31819 65 78 74 20 70 6f 73 69 74 69 6f 6e 27 73 20 64  ext position's d
3181a 61 74 61 2e 20 20 6e 44 61 74 61 20 77 69 6c 6c  ata.  nData will
3181b 20 72 65 61 63 68 20 30 20 77 68 65 6e 0a 20 20   reach 0 when.  
3181c 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 6c  ** reading the l
3181d 61 73 74 20 70 6f 73 69 74 69 6f 6e 2c 20 73 6f  ast position, so
3181e 20 70 6c 72 53 74 65 70 28 29 20 73 69 67 6e 61   plrStep() signa
3181f 6c 73 20 45 4f 46 20 62 79 20 73 65 74 74 69 6e  ls EOF by settin
31820 67 0a 20 20 2a 2a 20 70 44 61 74 61 20 74 6f 20  g.  ** pData to 
31821 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 63 6f 6e  NULL..  */.  con
31822 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b 0a  st char *pData;.
31823 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20 20    int nData;..  
31824 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
31825 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  e;.  int iColumn
31826 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  ;         /* the
31827 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 72 65 61   last column rea
31828 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73 69  d */.  int iPosi
31829 74 69 6f 6e 3b 20 20 20 20 20 20 20 2f 2a 20 74  tion;       /* t
3182a 68 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e  he last position
3182b 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69   read */.  int i
3182c 53 74 61 72 74 4f 66 66 73 65 74 3b 20 20 20 20  StartOffset;    
3182d 2f 2a 20 74 68 65 20 6c 61 73 74 20 73 74 61 72  /* the last star
3182e 74 20 6f 66 66 73 65 74 20 72 65 61 64 20 2a 2f  t offset read */
3182f 0a 20 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65  .  int iEndOffse
31830 74 3b 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6c  t;      /* the l
31831 61 73 74 20 65 6e 64 20 6f 66 66 73 65 74 20 72  ast end offset r
31832 65 61 64 20 2a 2f 0a 7d 20 50 4c 52 65 61 64 65  ead */.} PLReade
31833 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70  r;..static int p
31834 6c 72 41 74 45 6e 64 28 50 4c 52 65 61 64 65 72  lrAtEnd(PLReader
31835 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65   *pReader){.  re
31836 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44  turn pReader->pD
31837 61 74 61 3d 3d 4e 55 4c 4c 3b 0a 7d 0a 73 74 61  ata==NULL;.}.sta
31838 74 69 63 20 69 6e 74 20 70 6c 72 43 6f 6c 75 6d  tic int plrColum
31839 6e 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61  n(PLReader *pRea
3183a 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
3183b 21 70 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !plrAtEnd(pReade
3183c 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
3183d 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e 3b  Reader->iColumn;
3183e 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c  .}.static int pl
3183f 72 50 6f 73 69 74 69 6f 6e 28 50 4c 52 65 61 64  rPosition(PLRead
31840 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
31841 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
31842 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
31843 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
31844 69 50 6f 73 69 74 69 6f 6e 3b 0a 7d 0a 73 74 61  iPosition;.}.sta
31845 74 69 63 20 69 6e 74 20 70 6c 72 53 74 61 72 74  tic int plrStart
31846 4f 66 66 73 65 74 28 50 4c 52 65 61 64 65 72 20  Offset(PLReader 
31847 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
31848 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70  ert( !plrAtEnd(p
31849 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
3184a 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 53 74  urn pReader->iSt
3184b 61 72 74 4f 66 66 73 65 74 3b 0a 7d 0a 73 74 61  artOffset;.}.sta
3184c 74 69 63 20 69 6e 74 20 70 6c 72 45 6e 64 4f 66  tic int plrEndOf
3184d 66 73 65 74 28 50 4c 52 65 61 64 65 72 20 2a 70  fset(PLReader *p
3184e 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
3184f 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65  t( !plrAtEnd(pRe
31850 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
31851 6e 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f  n pReader->iEndO
31852 66 66 73 65 74 3b 0a 7d 0a 73 74 61 74 69 63 20  ffset;.}.static 
31853 76 6f 69 64 20 70 6c 72 53 74 65 70 28 50 4c 52  void plrStep(PLR
31854 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
31855 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 0a 20 20  .  int i, n;..  
31856 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
31857 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20  d(pReader) );.. 
31858 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44   if( pReader->nD
31859 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 70 52  ata==0 ){.    pR
3185a 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 4e  eader->pData = N
3185b 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ULL;.    return;
3185c 0a 20 20 7d 0a 0a 20 20 6e 20 3d 20 66 74 73 33  .  }..  n = fts3
3185d 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
3185e 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 29 3b  der->pData, &i);
3185f 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53 5f 43 4f  .  if( i==POS_CO
31860 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6e 20 2b 3d  LUMN ){.    n +=
31861 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
31862 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b  (pReader->pData+
31863 6e 2c 20 26 70 52 65 61 64 65 72 2d 3e 69 43 6f  n, &pReader->iCo
31864 6c 75 6d 6e 29 3b 0a 20 20 20 20 70 52 65 61 64  lumn);.    pRead
31865 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20  er->iPosition = 
31866 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  0;.    pReader->
31867 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30  iStartOffset = 0
31868 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47  ;.    n += fts3G
31869 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
3186a 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29  er->pData+n, &i)
3186b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 68 6f 75 6c  ;.  }.  /* Shoul
3186c 64 20 6e 65 76 65 72 20 73 65 65 20 61 64 6a 61  d never see adja
3186d 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 68 61 6e  cent column chan
3186e 67 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ges. */.  assert
3186f 28 20 69 21 3d 50 4f 53 5f 43 4f 4c 55 4d 4e 20  ( i!=POS_COLUMN 
31870 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53  );..  if( i==POS
31871 5f 45 4e 44 20 29 7b 0a 20 20 20 20 70 52 65 61  _END ){.    pRea
31872 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a  der->nData = 0;.
31873 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61      pReader->pDa
31874 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72  ta = NULL;.    r
31875 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 52  eturn;.  }..  pR
31876 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e  eader->iPosition
31877 20 2b 3d 20 69 2d 50 4f 53 5f 42 41 53 45 3b 0a   += i-POS_BASE;.
31878 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 69    if( pReader->i
31879 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  Type==DL_POSITIO
3187a 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20  NS_OFFSETS ){.  
3187b 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
3187c 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
3187d 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20 20  pData+n, &i);.  
3187e 20 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72    pReader->iStar
3187f 74 4f 66 66 73 65 74 20 2b 3d 20 69 3b 0a 20 20  tOffset += i;.  
31880 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
31881 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
31882 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20 20  pData+n, &i);.  
31883 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f    pReader->iEndO
31884 66 66 73 65 74 20 3d 20 70 52 65 61 64 65 72 2d  ffset = pReader-
31885 3e 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69 3b  >iStartOffset+i;
31886 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
31887 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61  <=pReader->nData
31888 20 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70   );.  pReader->p
31889 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 70 52 65  Data += n;.  pRe
3188a 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e  ader->nData -= n
3188b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
3188c 20 70 6c 72 49 6e 69 74 28 50 4c 52 65 61 64 65   plrInit(PLReade
3188d 72 20 2a 70 52 65 61 64 65 72 2c 20 44 4c 52 65  r *pReader, DLRe
3188e 61 64 65 72 20 2a 70 44 4c 52 65 61 64 65 72 29  ader *pDLReader)
3188f 7b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  {.  pReader->pDa
31890 74 61 20 3d 20 64 6c 72 50 6f 73 44 61 74 61 28  ta = dlrPosData(
31891 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20 70 52  pDLReader);.  pR
31892 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 64  eader->nData = d
31893 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28 70 44 4c  lrPosDataLen(pDL
31894 52 65 61 64 65 72 29 3b 0a 20 20 70 52 65 61 64  Reader);.  pRead
31895 65 72 2d 3e 69 54 79 70 65 20 3d 20 70 44 4c 52  er->iType = pDLR
31896 65 61 64 65 72 2d 3e 69 54 79 70 65 3b 0a 20 20  eader->iType;.  
31897 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pReader->iColumn
31898 20 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d   = 0;.  pReader-
31899 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20 30 3b 0a  >iPosition = 0;.
3189a 20 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72    pReader->iStar
3189b 74 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70  tOffset = 0;.  p
3189c 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f 66 66 73  Reader->iEndOffs
3189d 65 74 20 3d 20 30 3b 0a 20 20 70 6c 72 53 74 65  et = 0;.  plrSte
3189e 70 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74  p(pReader);.}.st
3189f 61 74 69 63 20 76 6f 69 64 20 70 6c 72 44 65 73  atic void plrDes
318a0 74 72 6f 79 28 50 4c 52 65 61 64 65 72 20 2a 70  troy(PLReader *p
318a1 52 65 61 64 65 72 29 7b 0a 20 20 53 43 52 41 4d  Reader){.  SCRAM
318a2 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a  BLE(pReader);.}.
318a3 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
318a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
318a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
318a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
318a7 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 57 72 69 74  *****/./* PLWrit
318a8 65 72 20 69 73 20 75 73 65 64 20 69 6e 20 63 6f  er is used in co
318a9 6e 73 74 72 75 63 74 69 6e 67 20 61 20 64 6f 63  nstructing a doc
318aa 75 6d 65 6e 74 27 73 20 70 6f 73 69 74 69 6f 6e  ument's position
318ab 20 6c 69 73 74 2e 20 20 41 73 20 61 0a 2a 2a 20   list.  As a.** 
318ac 63 6f 6e 76 65 6e 69 65 6e 63 65 2c 20 69 66 20  convenience, if 
318ad 69 54 79 70 65 20 69 73 20 44 4c 5f 44 4f 43 49  iType is DL_DOCI
318ae 44 53 2c 20 50 4c 57 72 69 74 65 72 20 62 65 63  DS, PLWriter bec
318af 6f 6d 65 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  omes a no-op..**
318b0 20 50 4c 57 72 69 74 65 72 20 77 72 69 74 65 73   PLWriter writes
318b1 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61 74   to the associat
318b2 65 64 20 44 4c 57 72 69 74 65 72 27 73 20 62 75  ed DLWriter's bu
318b3 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 6c 77 49  ffer..**.** plwI
318b4 6e 69 74 20 2d 20 69 6e 69 74 20 66 6f 72 20 77  nit - init for w
318b5 72 69 74 69 6e 67 20 61 20 64 6f 63 75 6d 65 6e  riting a documen
318b6 74 27 73 20 70 6f 73 6c 69 73 74 2e 0a 2a 2a 20  t's poslist..** 
318b7 70 6c 77 44 65 73 74 72 6f 79 20 2d 20 63 6c 65  plwDestroy - cle
318b8 61 72 20 61 20 77 72 69 74 65 72 2e 0a 2a 2a 20  ar a writer..** 
318b9 70 6c 77 41 64 64 20 2d 20 61 70 70 65 6e 64 20  plwAdd - append 
318ba 70 6f 73 69 74 69 6f 6e 20 61 6e 64 20 6f 66 66  position and off
318bb 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  set information.
318bc 0a 2a 2a 20 70 6c 77 43 6f 70 79 20 2d 20 63 6f  .** plwCopy - co
318bd 70 79 20 6e 65 78 74 20 70 6f 73 69 74 69 6f 6e  py next position
318be 27 73 20 64 61 74 61 20 66 72 6f 6d 20 72 65 61  's data from rea
318bf 64 65 72 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a  der to writer..*
318c0 2a 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 20 2d  * plwTerminate -
318c1 20 61 64 64 20 61 6e 79 20 6e 65 63 65 73 73 61   add any necessa
318c2 72 79 20 64 6f 63 6c 69 73 74 20 74 65 72 6d 69  ry doclist termi
318c3 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c  nator..**.** Cal
318c4 6c 69 6e 67 20 70 6c 77 41 64 64 28 29 20 61 66  ling plwAdd() af
318c5 74 65 72 20 70 6c 77 54 65 72 6d 69 6e 61 74 65  ter plwTerminate
318c6 28 29 20 6d 61 79 20 72 65 73 75 6c 74 20 69 6e  () may result in
318c7 20 61 20 63 6f 72 72 75 70 74 0a 2a 2a 20 64 6f   a corrupt.** do
318c8 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  clist..*/./* TOD
318c9 4f 28 73 68 65 73 73 29 20 55 6e 74 69 6c 20 77  O(shess) Until w
318ca 65 27 76 65 20 77 72 69 74 74 65 6e 20 74 68 65  e've written the
318cb 20 73 65 63 6f 6e 64 20 69 74 65 6d 2c 20 77 65   second item, we
318cc 20 63 61 6e 20 63 61 63 68 65 20 74 68 65 0a 2a   can cache the.*
318cd 2a 20 66 69 72 73 74 20 69 74 65 6d 27 73 20 69  * first item's i
318ce 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 54 68 65  nformation.  The
318cf 6e 20 77 65 27 64 20 68 61 76 65 20 74 68 72 65  n we'd have thre
318d0 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
318d1 2d 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 69  - initialized wi
318d2 74 68 20 64 6f 63 69 64 2c 20 6e 6f 20 70 6f 73  th docid, no pos
318d3 69 74 69 6f 6e 73 2e 0a 2a 2a 20 2d 20 64 6f 63  itions..** - doc
318d4 69 64 20 61 6e 64 20 6f 6e 65 20 70 6f 73 69 74  id and one posit
318d5 69 6f 6e 2e 0a 2a 2a 20 2d 20 64 6f 63 69 64 20  ion..** - docid 
318d6 61 6e 64 20 6d 75 6c 74 69 70 6c 65 20 70 6f 73  and multiple pos
318d7 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  itions..**.** On
318d8 6c 79 20 74 68 65 20 6c 61 73 74 20 73 74 61 74  ly the last stat
318d9 65 20 6e 65 65 64 73 20 74 6f 20 61 63 74 75 61  e needs to actua
318da 6c 6c 79 20 77 72 69 74 65 20 74 6f 20 64 6c 77  lly write to dlw
318db 2d 3e 62 2c 20 77 68 69 63 68 20 77 6f 75 6c 64  ->b, which would
318dc 0a 2a 2a 20 62 65 20 61 6e 20 69 6d 70 72 6f 76  .** be an improv
318dd 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 44 4c 43  ement in the DLC
318de 6f 6c 6c 65 63 74 6f 72 20 63 61 73 65 2e 0a 2a  ollector case..*
318df 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
318e0 20 50 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 4c   PLWriter {.  DL
318e1 57 72 69 74 65 72 20 2a 64 6c 77 3b 0a 0a 20 20  Writer *dlw;..  
318e2 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
318e3 2f 2a 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75  /* the last colu
318e4 6d 6e 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  mn written */.  
318e5 69 6e 74 20 69 50 6f 73 3b 20 20 20 20 20 20 20  int iPos;       
318e6 2f 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73 69  /* the last posi
318e7 74 69 6f 6e 20 77 72 69 74 74 65 6e 20 2a 2f 0a  tion written */.
318e8 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20    int iOffset;  
318e9 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 73 74    /* the last st
318ea 61 72 74 20 6f 66 66 73 65 74 20 77 72 69 74 74  art offset writt
318eb 65 6e 20 2a 2f 0a 7d 20 50 4c 57 72 69 74 65 72  en */.} PLWriter
318ec 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  ;../* TODO(shess
318ed 29 20 49 6e 20 74 68 65 20 63 61 73 65 20 77 68  ) In the case wh
318ee 65 72 65 20 74 68 65 20 70 61 72 65 6e 74 20 69  ere the parent i
318ef 73 20 72 65 61 64 69 6e 67 20 74 68 65 73 65 20  s reading these 
318f0 76 61 6c 75 65 73 0a 2a 2a 20 66 72 6f 6d 20 61  values.** from a
318f1 20 50 4c 52 65 61 64 65 72 2c 20 77 65 20 63 6f   PLReader, we co
318f2 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20 74 6f 20  uld optimize to 
318f3 61 20 63 6f 70 79 20 69 66 20 74 68 61 74 20 50  a copy if that P
318f4 4c 52 65 61 64 65 72 20 68 61 73 0a 2a 2a 20 74  LReader has.** t
318f5 68 65 20 73 61 6d 65 20 74 79 70 65 20 61 73 20  he same type as 
318f6 70 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  pWriter..*/.stat
318f7 69 63 20 76 6f 69 64 20 70 6c 77 41 64 64 28 50  ic void plwAdd(P
318f8 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
318f9 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69  , int iColumn, i
318fa 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20 20 20 20  nt iPos,.       
318fb 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
318fc 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 6e  iStartOffset, in
318fd 74 20 69 45 6e 64 4f 66 66 73 65 74 29 7b 0a 20  t iEndOffset){. 
318fe 20 2f 2a 20 57 6f 72 73 74 2d 63 61 73 65 20 73   /* Worst-case s
318ff 70 61 63 65 20 66 6f 72 20 50 4f 53 5f 43 4f 4c  pace for POS_COL
31900 55 4d 4e 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50  UMN, iColumn, iP
31901 6f 73 44 65 6c 74 61 2c 0a 20 20 2a 2a 20 69 53  osDelta,.  ** iS
31902 74 61 72 74 4f 66 66 73 65 74 44 65 6c 74 61 2c  tartOffsetDelta,
31903 20 61 6e 64 20 69 45 6e 64 4f 66 66 73 65 74 44   and iEndOffsetD
31904 65 6c 74 61 2e 0a 20 20 2a 2f 0a 20 20 63 68 61  elta..  */.  cha
31905 72 20 63 5b 35 2a 56 41 52 49 4e 54 5f 4d 41 58  r c[5*VARINT_MAX
31906 5d 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a  ];.  int n = 0;.
31907 0a 20 20 2f 2a 20 42 61 6e 20 70 6c 77 41 64 64  .  /* Ban plwAdd
31908 28 29 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d  () after plwTerm
31909 69 6e 61 74 65 28 29 2e 20 2a 2f 0a 20 20 61 73  inate(). */.  as
3190a 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 69  sert( pWriter->i
3190b 50 6f 73 21 3d 2d 31 20 29 3b 0a 0a 20 20 69 66  Pos!=-1 );..  if
3190c 28 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e  ( pWriter->dlw->
3190d 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53  iType==DL_DOCIDS
3190e 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66   ) return;..  if
3190f 28 20 69 43 6f 6c 75 6d 6e 21 3d 70 57 72 69 74  ( iColumn!=pWrit
31910 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  er->iColumn ){. 
31911 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56     n += fts3PutV
31912 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f 53 5f 43  arint(c+n, POS_C
31913 4f 4c 55 4d 4e 29 3b 0a 20 20 20 20 6e 20 2b 3d  OLUMN);.    n +=
31914 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
31915 2b 6e 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20  +n, iColumn);.  
31916 20 20 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75    pWriter->iColu
31917 6d 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20  mn = iColumn;.  
31918 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20    pWriter->iPos 
31919 3d 20 30 3b 0a 20 20 20 20 70 57 72 69 74 65 72  = 0;.    pWriter
3191a 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ->iOffset = 0;. 
3191b 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 50 6f   }.  assert( iPo
3191c 73 3e 3d 70 57 72 69 74 65 72 2d 3e 69 50 6f 73  s>=pWriter->iPos
3191d 20 29 3b 0a 20 20 6e 20 2b 3d 20 66 74 73 33 50   );.  n += fts3P
3191e 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f  utVarint(c+n, PO
3191f 53 5f 42 41 53 45 2b 28 69 50 6f 73 2d 70 57 72  S_BASE+(iPos-pWr
31920 69 74 65 72 2d 3e 69 50 6f 73 29 29 3b 0a 20 20  iter->iPos));.  
31921 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20  pWriter->iPos = 
31922 69 50 6f 73 3b 0a 20 20 69 66 28 20 70 57 72 69  iPos;.  if( pWri
31923 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3d  ter->dlw->iType=
31924 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46  =DL_POSITIONS_OF
31925 46 53 45 54 53 20 29 7b 0a 20 20 20 20 61 73 73  FSETS ){.    ass
31926 65 72 74 28 20 69 53 74 61 72 74 4f 66 66 73 65  ert( iStartOffse
31927 74 3e 3d 70 57 72 69 74 65 72 2d 3e 69 4f 66 66  t>=pWriter->iOff
31928 73 65 74 20 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  set );.    n += 
31929 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
3192a 6e 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74 2d  n, iStartOffset-
3192b 70 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74  pWriter->iOffset
3192c 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
3192d 69 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74  iOffset = iStart
3192e 4f 66 66 73 65 74 3b 0a 20 20 20 20 61 73 73 65  Offset;.    asse
3192f 72 74 28 20 69 45 6e 64 4f 66 66 73 65 74 3e 3d  rt( iEndOffset>=
31930 69 53 74 61 72 74 4f 66 66 73 65 74 20 29 3b 0a  iStartOffset );.
31931 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74      n += fts3Put
31932 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 45 6e 64  Varint(c+n, iEnd
31933 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66  Offset-iStartOff
31934 73 65 74 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61  set);.  }.  data
31935 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72  BufferAppend(pWr
31936 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63 2c  iter->dlw->b, c,
31937 20 6e 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f   n);.}.static vo
31938 69 64 20 70 6c 77 43 6f 70 79 28 50 4c 57 72 69  id plwCopy(PLWri
31939 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20 50 4c  ter *pWriter, PL
3193a 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
3193b 7b 0a 20 20 70 6c 77 41 64 64 28 70 57 72 69 74  {.  plwAdd(pWrit
3193c 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 52  er, plrColumn(pR
3193d 65 61 64 65 72 29 2c 20 70 6c 72 50 6f 73 69 74  eader), plrPosit
3193e 69 6f 6e 28 70 52 65 61 64 65 72 29 2c 0a 20 20  ion(pReader),.  
3193f 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f         plrStartO
31940 66 66 73 65 74 28 70 52 65 61 64 65 72 29 2c 20  ffset(pReader), 
31941 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52 65  plrEndOffset(pRe
31942 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63  ader));.}.static
31943 20 76 6f 69 64 20 70 6c 77 49 6e 69 74 28 50 4c   void plwInit(PL
31944 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
31945 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77 2c 20   DLWriter *dlw, 
31946 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
31947 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63 5b 56  cid){.  char c[V
31948 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
31949 74 20 6e 3b 0a 0a 20 20 70 57 72 69 74 65 72 2d  t n;..  pWriter-
3194a 3e 64 6c 77 20 3d 20 64 6c 77 3b 0a 0a 20 20 2f  >dlw = dlw;..  /
3194b 2a 20 44 6f 63 69 64 73 20 6d 75 73 74 20 61 73  * Docids must as
3194c 63 65 6e 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72  cend. */.  asser
3194d 74 28 20 21 70 57 72 69 74 65 72 2d 3e 64 6c 77  t( !pWriter->dlw
3194e 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  ->has_iPrevDocid
3194f 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57 72 69 74   || iDocid>pWrit
31950 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f  er->dlw->iPrevDo
31951 63 69 64 20 29 3b 0a 20 20 6e 20 3d 20 66 74 73  cid );.  n = fts
31952 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69 44  3PutVarint(c, iD
31953 6f 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 64 6c  ocid-pWriter->dl
31954 77 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b 0a  w->iPrevDocid);.
31955 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
31956 6e 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d  nd(pWriter->dlw-
31957 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20 70 57 72  >b, c, n);.  pWr
31958 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76  iter->dlw->iPrev
31959 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a  Docid = iDocid;.
3195a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
3195b 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 68   pWriter->dlw->h
3195c 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20  as_iPrevDocid = 
3195d 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 70 57 72  1;.#endif..  pWr
3195e 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  iter->iColumn = 
3195f 30 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50  0;.  pWriter->iP
31960 6f 73 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65  os = 0;.  pWrite
31961 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  r->iOffset = 0;.
31962 7d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  }./* TODO(shess)
31963 20 53 68 6f 75 6c 64 20 70 6c 77 44 65 73 74 72   Should plwDestr
31964 6f 79 28 29 20 61 6c 73 6f 20 74 65 72 6d 69 6e  oy() also termin
31965 61 74 65 20 74 68 65 20 64 6f 63 6c 69 73 74 3f  ate the doclist?
31966 20 20 42 75 74 0a 2a 2a 20 74 68 65 6e 20 70 6c    But.** then pl
31967 77 44 65 73 74 72 6f 79 28 29 20 77 6f 75 6c 64  wDestroy() would
31968 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 6a 75   no longer be ju
31969 73 74 20 61 20 64 65 73 74 72 75 63 74 6f 72 2c  st a destructor,
3196a 20 69 74 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73   it would.** als
3196b 6f 20 62 65 20 64 6f 69 6e 67 20 77 6f 72 6b 2c  o be doing work,
3196c 20 77 68 69 63 68 20 69 73 6e 27 74 20 63 6f 6e   which isn't con
3196d 73 69 73 74 65 6e 74 20 77 69 74 68 20 74 68 65  sistent with the
3196e 20 6f 76 65 72 61 6c 6c 20 69 64 69 6f 6d 2e 0a   overall idiom..
3196f 2a 2a 20 41 6e 6f 74 68 65 72 20 6f 70 74 69 6f  ** Another optio
31970 6e 20 77 6f 75 6c 64 20 62 65 20 66 6f 72 20 70  n would be for p
31971 6c 77 41 64 64 28 29 20 74 6f 20 61 6c 77 61 79  lwAdd() to alway
31972 73 20 61 70 70 65 6e 64 20 61 6e 79 20 6e 65 63  s append any nec
31973 65 73 73 61 72 79 0a 2a 2a 20 74 65 72 6d 69 6e  essary.** termin
31974 61 74 6f 72 2c 20 73 6f 20 74 68 61 74 20 74 68  ator, so that th
31975 65 20 6f 75 74 70 75 74 20 69 73 20 61 6c 77 61  e output is alwa
31976 79 73 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74  ys correct.  But
31977 20 74 68 61 74 20 77 6f 75 6c 64 0a 2a 2a 20 61   that would.** a
31978 64 64 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 77  dd incremental w
31979 6f 72 6b 20 74 6f 20 74 68 65 20 63 6f 6d 6d 6f  ork to the commo
3197a 6e 20 63 61 73 65 20 77 69 74 68 20 74 68 65 20  n case with the 
3197b 6f 6e 6c 79 20 62 65 6e 65 66 69 74 20 62 65 69  only benefit bei
3197c 6e 67 0a 2a 2a 20 41 50 49 20 65 6c 65 67 61 6e  ng.** API elegan
3197d 63 65 2e 20 20 50 75 6e 74 20 66 6f 72 20 6e 6f  ce.  Punt for no
3197e 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  w..*/.static voi
3197f 64 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 50  d plwTerminate(P
31980 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
31981 29 7b 0a 20 20 69 66 28 20 70 57 72 69 74 65 72  ){.  if( pWriter
31982 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3e 44 4c 5f  ->dlw->iType>DL_
31983 44 4f 43 49 44 53 20 29 7b 0a 20 20 20 20 63 68  DOCIDS ){.    ch
31984 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d  ar c[VARINT_MAX]
31985 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66 74  ;.    int n = ft
31986 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 50  s3PutVarint(c, P
31987 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20 64 61 74  OS_END);.    dat
31988 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57  aBufferAppend(pW
31989 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63  riter->dlw->b, c
3198a 2c 20 6e 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  , n);.  }.#ifnde
3198b 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 4d 61  f NDEBUG.  /* Ma
3198c 72 6b 20 61 73 20 74 65 72 6d 69 6e 61 74 65 64  rk as terminated
3198d 20 66 6f 72 20 61 73 73 65 72 74 20 69 6e 20 70   for assert in p
3198e 6c 77 41 64 64 28 29 2e 20 2a 2f 0a 20 20 70 57  lwAdd(). */.  pW
3198f 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20 2d 31  riter->iPos = -1
31990 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69  ;.#endif.}.stati
31991 63 20 76 6f 69 64 20 70 6c 77 44 65 73 74 72 6f  c void plwDestro
31992 79 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69  y(PLWriter *pWri
31993 74 65 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45  ter){.  SCRAMBLE
31994 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a  (pWriter);.}../*
31995 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31996 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31997 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31998 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31999 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f 6c 6c 65 63 74  **/./* DLCollect
3199a 6f 72 20 77 72 61 70 73 20 50 4c 57 72 69 74 65  or wraps PLWrite
3199b 72 20 61 6e 64 20 44 4c 57 72 69 74 65 72 20 74  r and DLWriter t
3199c 6f 20 70 72 6f 76 69 64 65 20 61 0a 2a 2a 20 64  o provide a.** d
3199d 79 6e 61 6d 69 63 61 6c 6c 79 2d 61 6c 6c 6f 63  ynamically-alloc
3199e 61 74 65 64 20 64 6f 63 6c 69 73 74 20 61 72 65  ated doclist are
3199f 61 20 74 6f 20 75 73 65 20 64 75 72 69 6e 67 20  a to use during 
319a0 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2a  tokenization..**
319a1 0a 2a 2a 20 64 6c 63 4e 65 77 20 2d 20 6d 61 6c  .** dlcNew - mal
319a2 6c 6f 63 20 75 70 20 61 6e 64 20 69 6e 69 74 69  loc up and initi
319a3 61 6c 69 7a 65 20 61 20 63 6f 6c 6c 65 63 74 6f  alize a collecto
319a4 72 2e 0a 2a 2a 20 64 6c 63 44 65 6c 65 74 65 20  r..** dlcDelete 
319a5 2d 20 64 65 73 74 72 6f 79 20 61 20 63 6f 6c 6c  - destroy a coll
319a6 65 63 74 6f 72 20 61 6e 64 20 61 6c 6c 20 63 6f  ector and all co
319a7 6e 74 61 69 6e 65 64 20 69 74 65 6d 73 2e 0a 2a  ntained items..*
319a8 2a 20 64 6c 63 41 64 64 50 6f 73 20 2d 20 61 70  * dlcAddPos - ap
319a9 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e  pend position an
319aa 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61  d offset informa
319ab 74 69 6f 6e 2e 0a 2a 2a 20 64 6c 63 41 64 64 44  tion..** dlcAddD
319ac 6f 63 6c 69 73 74 20 2d 20 61 64 64 20 74 68 65  oclist - add the
319ad 20 63 6f 6c 6c 65 63 74 65 64 20 64 6f 63 6c 69   collected docli
319ae 73 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20  st to the given 
319af 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 63 4e 65  buffer..** dlcNe
319b0 78 74 20 2d 20 74 65 72 6d 69 6e 61 74 65 20 74  xt - terminate t
319b1 68 65 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d  he current docum
319b2 65 6e 74 20 61 6e 64 20 6f 70 65 6e 20 61 6e 6f  ent and open ano
319b3 74 68 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ther..*/.typedef
319b4 20 73 74 72 75 63 74 20 44 4c 43 6f 6c 6c 65 63   struct DLCollec
319b5 74 6f 72 20 7b 0a 20 20 44 61 74 61 42 75 66 66  tor {.  DataBuff
319b6 65 72 20 62 3b 0a 20 20 44 4c 57 72 69 74 65 72  er b;.  DLWriter
319b7 20 64 6c 77 3b 0a 20 20 50 4c 57 72 69 74 65 72   dlw;.  PLWriter
319b8 20 70 6c 77 3b 0a 7d 20 44 4c 43 6f 6c 6c 65 63   plw;.} DLCollec
319b9 74 6f 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  tor;../* TODO(sh
319ba 65 73 73 29 20 54 68 69 73 20 63 6f 75 6c 64 20  ess) This could 
319bb 61 6c 73 6f 20 62 65 20 64 6f 6e 65 20 62 79 20  also be done by 
319bc 63 61 6c 6c 69 6e 67 20 70 6c 77 54 65 72 6d 69  calling plwTermi
319bd 6e 61 74 65 28 29 20 61 6e 64 0a 2a 2a 20 64 61  nate() and.** da
319be 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 29  taBufferAppend()
319bf 2e 20 20 49 20 74 72 69 65 64 20 74 68 61 74 2c  .  I tried that,
319c0 20 65 78 70 65 63 74 69 6e 67 20 6e 6f 6d 69 6e   expecting nomin
319c1 61 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a  al performance.*
319c2 2a 20 64 69 66 66 65 72 65 6e 63 65 73 2c 20 62  * differences, b
319c3 75 74 20 69 74 20 73 65 65 6d 65 64 20 74 6f 20  ut it seemed to 
319c4 70 72 65 74 74 79 20 72 65 6c 69 61 62 6c 79 20  pretty reliably 
319c5 62 65 20 77 6f 72 74 68 20 31 25 20 74 6f 20 63  be worth 1% to c
319c6 6f 64 65 0a 2a 2a 20 69 74 20 74 68 69 73 20 77  ode.** it this w
319c7 61 79 2e 20 20 49 20 73 75 73 70 65 63 74 20 69  ay.  I suspect i
319c8 74 20 69 73 20 74 68 65 20 69 6e 63 72 65 6d 65  t is the increme
319c9 6e 74 61 6c 20 6d 61 6c 6c 6f 63 20 6f 76 65 72  ntal malloc over
319ca 68 65 61 64 20 28 73 6f 6d 65 0a 2a 2a 20 70 65  head (some.** pe
319cb 72 63 65 6e 74 61 67 65 20 6f 66 20 74 68 65 20  rcentage of the 
319cc 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 29 20 63  plwTerminate() c
319cd 61 6c 6c 73 20 77 69 6c 6c 20 63 61 75 73 65 20  alls will cause 
319ce 61 20 72 65 61 6c 6c 6f 63 29 2c 20 73 6f 0a 2a  a realloc), so.*
319cf 2a 20 74 68 69 73 20 6d 69 67 68 74 20 62 65 20  * this might be 
319d0 77 6f 72 74 68 20 72 65 76 69 73 69 74 69 6e 67  worth revisiting
319d1 20 69 66 20 74 68 65 20 44 61 74 61 42 75 66 66   if the DataBuff
319d2 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  er implementatio
319d3 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 0a 2a 2f  n.** changes..*/
319d4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 63  .static void dlc
319d5 41 64 64 44 6f 63 6c 69 73 74 28 44 4c 43 6f 6c  AddDoclist(DLCol
319d6 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74  lector *pCollect
319d7 6f 72 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a  or, DataBuffer *
319d8 62 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 65  b){.  if( pColle
319d9 63 74 6f 72 2d 3e 64 6c 77 2e 69 54 79 70 65 3e  ctor->dlw.iType>
319da 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20  DL_DOCIDS ){.   
319db 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d   char c[VARINT_M
319dc 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  AX];.    int n =
319dd 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
319de 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20  , POS_END);.    
319df 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
319e0 32 28 62 2c 20 70 43 6f 6c 6c 65 63 74 6f 72 2d  2(b, pCollector-
319e1 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f 6c 6c 65  >b.pData, pColle
319e2 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61 2c 20 63  ctor->b.nData, c
319e3 2c 20 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  , n);.  }else{. 
319e4 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
319e5 65 6e 64 28 62 2c 20 70 43 6f 6c 6c 65 63 74 6f  end(b, pCollecto
319e6 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f 6c  r->b.pData, pCol
319e7 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61 29  lector->b.nData)
319e8 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
319e9 6f 69 64 20 64 6c 63 4e 65 78 74 28 44 4c 43 6f  oid dlcNext(DLCo
319ea 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63  llector *pCollec
319eb 74 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  tor, sqlite_int6
319ec 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 70 6c 77  4 iDocid){.  plw
319ed 54 65 72 6d 69 6e 61 74 65 28 26 70 43 6f 6c 6c  Terminate(&pColl
319ee 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 70  ector->plw);.  p
319ef 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c  lwDestroy(&pColl
319f0 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 70  ector->plw);.  p
319f1 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74  lwInit(&pCollect
319f2 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c 65  or->plw, &pColle
319f3 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63 69  ctor->dlw, iDoci
319f4 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  d);.}.static voi
319f5 64 20 64 6c 63 41 64 64 50 6f 73 28 44 4c 43 6f  d dlcAddPos(DLCo
319f6 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63  llector *pCollec
319f7 74 6f 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  tor, int iColumn
319f8 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20  , int iPos,.    
319f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
319fa 20 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73    int iStartOffs
319fb 65 74 2c 20 69 6e 74 20 69 45 6e 64 4f 66 66 73  et, int iEndOffs
319fc 65 74 29 7b 0a 20 20 70 6c 77 41 64 64 28 26 70  et){.  plwAdd(&p
319fd 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c 20  Collector->plw, 
319fe 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73 2c 20 69  iColumn, iPos, i
319ff 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e  StartOffset, iEn
31a00 64 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a 73 74 61  dOffset);.}..sta
31a01 74 69 63 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20  tic DLCollector 
31a02 2a 64 6c 63 4e 65 77 28 73 71 6c 69 74 65 5f 69  *dlcNew(sqlite_i
31a03 6e 74 36 34 20 69 44 6f 63 69 64 2c 20 44 6f 63  nt64 iDocid, Doc
31a04 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 29 7b  ListType iType){
31a05 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a  .  DLCollector *
31a06 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 73 71 6c  pCollector = sql
31a07 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
31a08 6f 66 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 29 29  of(DLCollector))
31a09 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ;.  dataBufferIn
31a0a 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  it(&pCollector->
31a0b 62 2c 20 30 29 3b 0a 20 20 64 6c 77 49 6e 69 74  b, 0);.  dlwInit
31a0c 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c  (&pCollector->dl
31a0d 77 2c 20 69 54 79 70 65 2c 20 26 70 43 6f 6c 6c  w, iType, &pColl
31a0e 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20 70 6c 77  ector->b);.  plw
31a0f 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72  Init(&pCollector
31a10 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c 65 63 74  ->plw, &pCollect
31a11 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63 69 64 29  or->dlw, iDocid)
31a12 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c  ;.  return pColl
31a13 65 63 74 6f 72 3b 0a 7d 0a 73 74 61 74 69 63 20  ector;.}.static 
31a14 76 6f 69 64 20 64 6c 63 44 65 6c 65 74 65 28 44  void dlcDelete(D
31a15 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c  LCollector *pCol
31a16 6c 65 63 74 6f 72 29 7b 0a 20 20 70 6c 77 44 65  lector){.  plwDe
31a17 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f  stroy(&pCollecto
31a18 72 2d 3e 70 6c 77 29 3b 0a 20 20 64 6c 77 44 65  r->plw);.  dlwDe
31a19 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f  stroy(&pCollecto
31a1a 72 2d 3e 64 6c 77 29 3b 0a 20 20 64 61 74 61 42  r->dlw);.  dataB
31a1b 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 43  ufferDestroy(&pC
31a1c 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20  ollector->b);.  
31a1d 53 43 52 41 4d 42 4c 45 28 70 43 6f 6c 6c 65 63  SCRAMBLE(pCollec
31a1e 74 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  tor);.  sqlite3_
31a1f 66 72 65 65 28 70 43 6f 6c 6c 65 63 74 6f 72 29  free(pCollector)
31a20 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f 70 79 20 74 68  ;.}.../* Copy th
31a21 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 6f  e doclist data o
31a22 66 20 69 54 79 70 65 20 69 6e 20 70 44 61 74 61  f iType in pData
31a23 2f 6e 44 61 74 61 20 69 6e 74 6f 20 2a 6f 75 74  /nData into *out
31a24 2c 20 74 72 69 6d 6d 69 6e 67 0a 2a 2a 20 75 6e  , trimming.** un
31a25 6e 65 63 65 73 73 61 72 79 20 64 61 74 61 20 61  necessary data a
31a26 73 20 77 65 20 67 6f 2e 20 20 4f 6e 6c 79 20 63  s we go.  Only c
31a27 6f 6c 75 6d 6e 73 20 6d 61 74 63 68 69 6e 67 20  olumns matching 
31a28 69 43 6f 6c 75 6d 6e 20 61 72 65 0a 2a 2a 20 63  iColumn are.** c
31a29 6f 70 69 65 64 2c 20 61 6c 6c 20 63 6f 6c 75 6d  opied, all colum
31a2a 6e 73 20 63 6f 70 69 65 64 20 69 66 20 69 43 6f  ns copied if iCo
31a2b 6c 75 6d 6e 20 69 73 20 2d 31 2e 20 20 45 6c 65  lumn is -1.  Ele
31a2c 6d 65 6e 74 73 20 77 69 74 68 20 6e 6f 0a 2a 2a  ments with no.**
31a2d 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e   matching column
31a2e 73 20 61 72 65 20 64 72 6f 70 70 65 64 2e 20 20  s are dropped.  
31a2f 54 68 65 20 6f 75 74 70 75 74 20 69 73 20 61 6e  The output is an
31a30 20 69 4f 75 74 54 79 70 65 20 64 6f 63 6c 69 73   iOutType doclis
31a31 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f 54 45 28 73 68  t..*/./* NOTE(sh
31a32 65 73 73 29 20 54 68 69 73 20 63 6f 64 65 20 69  ess) This code i
31a33 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 61 66 74  s only valid aft
31a34 65 72 20 61 6c 6c 20 64 6f 63 6c 69 73 74 73 20  er all doclists 
31a35 61 72 65 20 6d 65 72 67 65 64 2e 0a 2a 2a 20 49  are merged..** I
31a36 66 20 74 68 69 73 20 69 73 20 72 75 6e 20 62 65  f this is run be
31a37 66 6f 72 65 20 6d 65 72 67 65 73 2c 20 74 68 65  fore merges, the
31a38 6e 20 64 6f 63 6c 69 73 74 20 69 74 65 6d 73 20  n doclist items 
31a39 77 68 69 63 68 20 72 65 70 72 65 73 65 6e 74 0a  which represent.
31a3a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 77 69 6c 6c  ** deletion will
31a3b 20 62 65 20 74 72 69 6d 6d 65 64 2c 20 61 6e 64   be trimmed, and
31a3c 20 77 69 6c 6c 20 74 68 75 73 20 6e 6f 74 20 65   will thus not e
31a3d 66 66 65 63 74 20 61 20 64 65 6c 65 74 69 6f 6e  ffect a deletion
31a3e 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 6d  .** during the m
31a3f 65 72 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  erge..*/.static 
31a40 76 6f 69 64 20 64 6f 63 4c 69 73 74 54 72 69 6d  void docListTrim
31a41 28 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79  (DocListType iTy
31a42 70 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  pe, const char *
31a43 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
31a44 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
31a45 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 43            int iC
31a46 6f 6c 75 6d 6e 2c 20 44 6f 63 4c 69 73 74 54 79  olumn, DocListTy
31a47 70 65 20 69 4f 75 74 54 79 70 65 2c 20 44 61 74  pe iOutType, Dat
31a48 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20  aBuffer *out){. 
31a49 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64   DLReader dlRead
31a4a 65 72 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 64  er;.  DLWriter d
31a4b 6c 57 72 69 74 65 72 3b 0a 0a 20 20 61 73 73 65  lWriter;..  asse
31a4c 72 74 28 20 69 4f 75 74 54 79 70 65 3c 3d 69 54  rt( iOutType<=iT
31a4d 79 70 65 20 29 3b 0a 0a 20 20 64 6c 72 49 6e 69  ype );..  dlrIni
31a4e 74 28 26 64 6c 52 65 61 64 65 72 2c 20 69 54 79  t(&dlReader, iTy
31a4f 70 65 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  pe, pData, nData
31a50 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26 64 6c  );.  dlwInit(&dl
31a51 57 72 69 74 65 72 2c 20 69 4f 75 74 54 79 70 65  Writer, iOutType
31a52 2c 20 6f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65  , out);..  while
31a53 28 20 21 64 6c 72 41 74 45 6e 64 28 26 64 6c 52  ( !dlrAtEnd(&dlR
31a54 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 50 4c  eader) ){.    PL
31a55 52 65 61 64 65 72 20 70 6c 52 65 61 64 65 72 3b  Reader plReader;
31a56 0a 20 20 20 20 50 4c 57 72 69 74 65 72 20 70 6c  .    PLWriter pl
31a57 57 72 69 74 65 72 3b 0a 20 20 20 20 69 6e 74 20  Writer;.    int 
31a58 6d 61 74 63 68 20 3d 20 30 3b 0a 0a 20 20 20 20  match = 0;..    
31a59 70 6c 72 49 6e 69 74 28 26 70 6c 52 65 61 64 65  plrInit(&plReade
31a5a 72 2c 20 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a  r, &dlReader);..
31a5b 20 20 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41      while( !plrA
31a5c 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29 20  tEnd(&plReader) 
31a5d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f  ){.      if( iCo
31a5e 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70 6c 72 43  lumn==-1 || plrC
31a5f 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29  olumn(&plReader)
31a60 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ==iColumn ){.   
31a61 20 20 20 20 20 69 66 28 20 21 6d 61 74 63 68 20       if( !match 
31a62 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  ){.          plw
31a63 49 6e 69 74 28 26 70 6c 57 72 69 74 65 72 2c 20  Init(&plWriter, 
31a64 26 64 6c 57 72 69 74 65 72 2c 20 64 6c 72 44 6f  &dlWriter, dlrDo
31a65 63 69 64 28 26 64 6c 52 65 61 64 65 72 29 29 3b  cid(&dlReader));
31a66 0a 20 20 20 20 20 20 20 20 20 20 6d 61 74 63 68  .          match
31a67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
31a68 20 20 20 20 20 20 20 20 70 6c 77 41 64 64 28 26          plwAdd(&
31a69 70 6c 57 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c  plWriter, plrCol
31a6a 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 2c 20  umn(&plReader), 
31a6b 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52  plrPosition(&plR
31a6c 65 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20  eader),.        
31a6d 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f         plrStartO
31a6e 66 66 73 65 74 28 26 70 6c 52 65 61 64 65 72 29  ffset(&plReader)
31a6f 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 26  , plrEndOffset(&
31a70 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20  plReader));.    
31a71 20 20 7d 0a 20 20 20 20 20 20 70 6c 72 53 74 65    }.      plrSte
31a72 70 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 20 20  p(&plReader);.  
31a73 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 61 74 63    }.    if( matc
31a74 68 20 29 7b 0a 20 20 20 20 20 20 70 6c 77 54 65  h ){.      plwTe
31a75 72 6d 69 6e 61 74 65 28 26 70 6c 57 72 69 74 65  rminate(&plWrite
31a76 72 29 3b 0a 20 20 20 20 20 20 70 6c 77 44 65 73  r);.      plwDes
31a77 74 72 6f 79 28 26 70 6c 57 72 69 74 65 72 29 3b  troy(&plWriter);
31a78 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 6c 72 44  .    }..    plrD
31a79 65 73 74 72 6f 79 28 26 70 6c 52 65 61 64 65 72  estroy(&plReader
31a7a 29 3b 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26  );.    dlrStep(&
31a7b 64 6c 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  dlReader);.  }. 
31a7c 20 64 6c 77 44 65 73 74 72 6f 79 28 26 64 6c 57   dlwDestroy(&dlW
31a7d 72 69 74 65 72 29 3b 0a 20 20 64 6c 72 44 65 73  riter);.  dlrDes
31a7e 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72 29 3b  troy(&dlReader);
31a7f 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 62 79 20 64  .}../* Used by d
31a80 6f 63 4c 69 73 74 4d 65 72 67 65 28 29 20 74 6f  ocListMerge() to
31a81 20 6b 65 65 70 20 64 6f 63 6c 69 73 74 73 20 69   keep doclists i
31a82 6e 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 20  n the ascending 
31a83 6f 72 64 65 72 20 62 79 0a 2a 2a 20 64 6f 63 69  order by.** doci
31a84 64 2c 20 74 68 65 6e 20 61 73 63 65 6e 64 69 6e  d, then ascendin
31a85 67 20 6f 72 64 65 72 20 62 79 20 61 67 65 20 28  g order by age (
31a86 73 6f 20 74 68 65 20 6e 65 77 65 73 74 20 63 6f  so the newest co
31a87 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 74  mes first)..*/.t
31a88 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4f 72  ypedef struct Or
31a89 64 65 72 65 64 44 4c 52 65 61 64 65 72 20 7b 0a  deredDLReader {.
31a8a 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61    DLReader *pRea
31a8b 64 65 72 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28  der;..  /* TODO(
31a8c 73 68 65 73 73 29 20 49 66 20 77 65 20 61 73 73  shess) If we ass
31a8d 75 6d 65 20 74 68 61 74 20 64 6f 63 4c 69 73 74  ume that docList
31a8e 4d 65 72 67 65 20 70 52 65 61 64 65 72 73 20 69  Merge pReaders i
31a8f 73 20 6f 72 64 65 72 65 64 20 62 79 0a 20 20 2a  s ordered by.  *
31a90 2a 20 61 67 65 20 28 77 68 69 63 68 20 77 65 20  * age (which we 
31a91 64 6f 29 2c 20 74 68 65 6e 20 77 65 20 63 6f 75  do), then we cou
31a92 6c 64 20 75 73 65 20 70 52 65 61 64 65 72 20 63  ld use pReader c
31a93 6f 6d 70 61 72 69 73 6f 6e 73 20 74 6f 20 62 72  omparisons to br
31a94 65 61 6b 0a 20 20 2a 2a 20 74 69 65 73 2e 0a 20  eak.  ** ties.. 
31a95 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 0a 7d   */.  int idx;.}
31a96 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   OrderedDLReader
31a97 3b 0a 0a 2f 2a 20 4f 72 64 65 72 20 65 6f 66 20  ;../* Order eof 
31a98 74 6f 20 65 6e 64 2c 20 74 68 65 6e 20 62 79 20  to end, then by 
31a99 64 6f 63 69 64 20 61 73 63 2c 20 69 64 78 20 64  docid asc, idx d
31a9a 65 73 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  esc. */.static i
31a9b 6e 74 20 6f 72 64 65 72 65 64 44 4c 52 65 61 64  nt orderedDLRead
31a9c 65 72 43 6d 70 28 4f 72 64 65 72 65 64 44 4c 52  erCmp(OrderedDLR
31a9d 65 61 64 65 72 20 2a 72 31 2c 20 4f 72 64 65 72  eader *r1, Order
31a9e 65 64 44 4c 52 65 61 64 65 72 20 2a 72 32 29 7b  edDLReader *r2){
31a9f 0a 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28  .  if( dlrAtEnd(
31aa0 72 31 2d 3e 70 52 65 61 64 65 72 29 20 29 7b 0a  r1->pReader) ){.
31aa1 20 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64      if( dlrAtEnd
31aa2 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20  (r2->pReader) ) 
31aa3 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 42 6f  return 0;  /* Bo
31aa4 74 68 20 61 74 45 6e 64 28 29 2e 20 2a 2f 0a 20  th atEnd(). */. 
31aa5 20 20 20 72 65 74 75 72 6e 20 31 3b 20 20 20 20     return 1;    
31aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31aa7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c            /* Onl
31aa8 79 20 72 31 20 61 74 45 6e 64 28 29 2e 20 2a 2f  y r1 atEnd(). */
31aa9 0a 20 20 7d 0a 20 20 69 66 28 20 64 6c 72 41 74  .  }.  if( dlrAt
31aaa 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29  End(r2->pReader)
31aab 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 20 20 20   ) return -1;   
31aac 2f 2a 20 4f 6e 6c 79 20 72 32 20 61 74 45 6e 64  /* Only r2 atEnd
31aad 28 29 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 64 6c  (). */..  if( dl
31aae 72 44 6f 63 69 64 28 72 31 2d 3e 70 52 65 61 64  rDocid(r1->pRead
31aaf 65 72 29 3c 64 6c 72 44 6f 63 69 64 28 72 32 2d  er)<dlrDocid(r2-
31ab0 3e 70 52 65 61 64 65 72 29 20 29 20 72 65 74 75  >pReader) ) retu
31ab1 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 64 6c 72  rn -1;.  if( dlr
31ab2 44 6f 63 69 64 28 72 31 2d 3e 70 52 65 61 64 65  Docid(r1->pReade
31ab3 72 29 3e 64 6c 72 44 6f 63 69 64 28 72 32 2d 3e  r)>dlrDocid(r2->
31ab4 70 52 65 61 64 65 72 29 20 29 20 72 65 74 75 72  pReader) ) retur
31ab5 6e 20 31 3b 0a 0a 20 20 2f 2a 20 44 65 73 63 65  n 1;..  /* Desce
31ab6 6e 64 69 6e 67 20 6f 6e 20 69 64 78 2e 20 2a 2f  nding on idx. */
31ab7 0a 20 20 72 65 74 75 72 6e 20 72 32 2d 3e 69 64  .  return r2->id
31ab8 78 2d 72 31 2d 3e 69 64 78 3b 0a 7d 0a 0a 2f 2a  x-r1->idx;.}../*
31ab9 20 42 75 62 62 6c 65 20 70 5b 30 5d 20 74 6f 20   Bubble p[0] to 
31aba 61 70 70 72 6f 70 72 69 61 74 65 20 70 6c 61 63  appropriate plac
31abb 65 20 69 6e 20 70 5b 31 2e 2e 6e 2d 31 5d 2e 20  e in p[1..n-1]. 
31abc 20 41 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a   Assumes that.**
31abd 20 70 5b 31 2e 2e 6e 2d 31 5d 20 69 73 20 61 6c   p[1..n-1] is al
31abe 72 65 61 64 79 20 73 6f 72 74 65 64 2e 0a 2a 2f  ready sorted..*/
31abf 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
31ac0 49 73 20 74 68 69 73 20 66 72 65 71 75 65 6e 74  Is this frequent
31ac1 20 65 6e 6f 75 67 68 20 74 6f 20 77 61 72 72 61   enough to warra
31ac2 6e 74 20 61 20 62 69 6e 61 72 79 20 73 65 61 72  nt a binary sear
31ac3 63 68 3f 0a 2a 2a 20 42 65 66 6f 72 65 20 69 6d  ch?.** Before im
31ac4 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 61 74 2c  plementing that,
31ac5 20 69 6e 73 74 72 75 6d 65 6e 74 20 74 68 65 20   instrument the 
31ac6 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 2e 20 20  code to check.  
31ac7 49 6e 20 6d 6f 73 74 0a 2a 2a 20 63 75 72 72 65  In most.** curre
31ac8 6e 74 20 75 73 61 67 65 2c 20 49 20 65 78 70 65  nt usage, I expe
31ac9 63 74 20 74 68 61 74 20 70 5b 30 5d 20 77 69 6c  ct that p[0] wil
31aca 6c 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 70  l be less than p
31acb 5b 31 5d 20 61 20 76 65 72 79 0a 2a 2a 20 68 69  [1] a very.** hi
31acc 67 68 20 70 72 6f 70 6f 72 74 69 6f 6e 20 6f 66  gh proportion of
31acd 20 74 68 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74   the time..*/.st
31ace 61 74 69 63 20 76 6f 69 64 20 6f 72 64 65 72 65  atic void ordere
31acf 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72  dDLReaderReorder
31ad0 28 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72  (OrderedDLReader
31ad1 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 77   *p, int n){.  w
31ad2 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 6f 72 64  hile( n>1 && ord
31ad3 65 72 65 64 44 4c 52 65 61 64 65 72 43 6d 70 28  eredDLReaderCmp(
31ad4 70 2c 20 70 2b 31 29 3e 30 20 29 7b 0a 20 20 20  p, p+1)>0 ){.   
31ad5 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   OrderedDLReader
31ad6 20 74 6d 70 20 3d 20 70 5b 30 5d 3b 0a 20 20 20   tmp = p[0];.   
31ad7 20 70 5b 30 5d 20 3d 20 70 5b 31 5d 3b 0a 20 20   p[0] = p[1];.  
31ad8 20 20 70 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20    p[1] = tmp;.  
31ad9 20 20 6e 2d 2d 3b 0a 20 20 20 20 70 2b 2b 3b 0a    n--;.    p++;.
31ada 20 20 7d 0a 7d 0a 0a 2f 2a 20 47 69 76 65 6e 20    }.}../* Given 
31adb 61 6e 20 61 72 72 61 79 20 6f 66 20 64 6f 63 6c  an array of docl
31adc 69 73 74 20 72 65 61 64 65 72 73 2c 20 6d 65 72  ist readers, mer
31add 67 65 20 74 68 65 69 72 20 64 6f 63 6c 69 73 74  ge their doclist
31ade 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e 74   elements.** int
31adf 6f 20 6f 75 74 20 69 6e 20 73 6f 72 74 65 64 20  o out in sorted 
31ae0 6f 72 64 65 72 20 28 62 79 20 64 6f 63 69 64 29  order (by docid)
31ae1 2c 20 64 72 6f 70 70 69 6e 67 20 65 6c 65 6d 65  , dropping eleme
31ae2 6e 74 73 20 66 72 6f 6d 20 6f 6c 64 65 72 0a 2a  nts from older.*
31ae3 2a 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 74  * readers when t
31ae4 68 65 72 65 20 69 73 20 61 20 64 75 70 6c 69 63  here is a duplic
31ae5 61 74 65 20 64 6f 63 69 64 2e 20 20 70 52 65 61  ate docid.  pRea
31ae6 64 65 72 73 20 69 73 20 61 73 73 75 6d 65 64 20  ders is assumed 
31ae7 74 6f 20 62 65 0a 2a 2a 20 6f 72 64 65 72 65 64  to be.** ordered
31ae8 20 62 79 20 61 67 65 2c 20 6f 6c 64 65 73 74 20   by age, oldest 
31ae9 66 69 72 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  first..*/./* TOD
31aea 4f 28 73 68 65 73 73 29 20 6e 52 65 61 64 65 72  O(shess) nReader
31aeb 73 20 6d 75 73 74 20 62 65 20 3c 3d 20 4d 45 52  s must be <= MER
31aec 47 45 5f 43 4f 55 4e 54 2e 20 20 54 68 69 73 20  GE_COUNT.  This 
31aed 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a  should probably.
31aee 2a 2a 20 62 65 20 66 69 78 65 64 2e 0a 2a 2f 0a  ** be fixed..*/.
31aef 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
31af0 69 73 74 4d 65 72 67 65 28 44 61 74 61 42 75 66  istMerge(DataBuf
31af1 66 65 72 20 2a 6f 75 74 2c 0a 20 20 20 20 20 20  fer *out,.      
31af2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31af3 20 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65     DLReader *pRe
31af4 61 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64  aders, int nRead
31af5 65 72 73 29 7b 0a 20 20 4f 72 64 65 72 65 64 44  ers){.  OrderedD
31af6 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 73 5b  LReader readers[
31af7 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20  MERGE_COUNT];.  
31af8 44 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b  DLWriter writer;
31af9 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 63  .  int i, n;.  c
31afa 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 74 61 72  onst char *pStar
31afb 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 53 74  t = 0;.  int nSt
31afc 61 72 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  art = 0;.  sqlit
31afd 65 5f 69 6e 74 36 34 20 69 46 69 72 73 74 44 6f  e_int64 iFirstDo
31afe 63 69 64 20 3d 20 30 2c 20 69 4c 61 73 74 44 6f  cid = 0, iLastDo
31aff 63 69 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  cid = 0;..  asse
31b00 72 74 28 20 6e 52 65 61 64 65 72 73 3e 30 20 29  rt( nReaders>0 )
31b01 3b 0a 20 20 69 66 28 20 6e 52 65 61 64 65 72 73  ;.  if( nReaders
31b02 3d 3d 31 20 29 7b 0a 20 20 20 20 64 61 74 61 42  ==1 ){.    dataB
31b03 75 66 66 65 72 41 70 70 65 6e 64 28 6f 75 74 2c  ufferAppend(out,
31b04 20 64 6c 72 44 6f 63 44 61 74 61 28 70 52 65 61   dlrDocData(pRea
31b05 64 65 72 73 29 2c 20 64 6c 72 41 6c 6c 44 61 74  ders), dlrAllDat
31b06 61 42 79 74 65 73 28 70 52 65 61 64 65 72 73 29  aBytes(pReaders)
31b07 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
31b08 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52   }..  assert( nR
31b09 65 61 64 65 72 73 3c 3d 4d 45 52 47 45 5f 43 4f  eaders<=MERGE_CO
31b0a 55 4e 54 20 29 3b 0a 20 20 6e 20 3d 20 30 3b 0a  UNT );.  n = 0;.
31b0b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65    for(i=0; i<nRe
31b0c 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  aders; i++){.   
31b0d 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72   assert( pReader
31b0e 73 5b 69 5d 2e 69 54 79 70 65 3d 3d 70 52 65 61  s[i].iType==pRea
31b0f 64 65 72 73 5b 30 5d 2e 69 54 79 70 65 20 29 3b  ders[0].iType );
31b10 0a 20 20 20 20 72 65 61 64 65 72 73 5b 69 5d 2e  .    readers[i].
31b11 70 52 65 61 64 65 72 20 3d 20 70 52 65 61 64 65  pReader = pReade
31b12 72 73 2b 69 3b 0a 20 20 20 20 72 65 61 64 65 72  rs+i;.    reader
31b13 73 5b 69 5d 2e 69 64 78 20 3d 20 69 3b 0a 20 20  s[i].idx = i;.  
31b14 20 20 6e 20 2b 3d 20 64 6c 72 41 6c 6c 44 61 74    n += dlrAllDat
31b15 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72 73  aBytes(&pReaders
31b16 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 43  [i]);.  }.  /* C
31b17 6f 6e 73 65 72 76 61 74 69 76 65 6c 79 20 73 69  onservatively si
31b18 7a 65 20 6f 75 74 70 75 74 20 74 6f 20 73 75 6d  ze output to sum
31b19 20 6f 66 20 69 6e 70 75 74 73 2e 20 20 4f 75 74   of inputs.  Out
31b1a 70 75 74 20 73 68 6f 75 6c 64 20 65 6e 64 0a 20  put should end. 
31b1b 20 2a 2a 20 75 70 20 73 74 72 69 63 74 6c 79 20   ** up strictly 
31b1c 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e 70  smaller than inp
31b1d 75 74 2e 0a 20 20 2a 2f 0a 20 20 64 61 74 61 42  ut..  */.  dataB
31b1e 75 66 66 65 72 45 78 70 61 6e 64 28 6f 75 74 2c  ufferExpand(out,
31b1f 20 6e 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74   n);..  /* Get t
31b20 68 65 20 72 65 61 64 65 72 73 20 69 6e 74 6f 20  he readers into 
31b21 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 2a 2f  sorted order. */
31b22 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20  .  while( i-->0 
31b23 29 7b 0a 20 20 20 20 6f 72 64 65 72 65 64 44 4c  ){.    orderedDL
31b24 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 72 65  ReaderReorder(re
31b25 61 64 65 72 73 2b 69 2c 20 6e 52 65 61 64 65 72  aders+i, nReader
31b26 73 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c 77  s-i);.  }..  dlw
31b27 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70 52  Init(&writer, pR
31b28 65 61 64 65 72 73 5b 30 5d 2e 69 54 79 70 65 2c  eaders[0].iType,
31b29 20 6f 75 74 29 3b 0a 20 20 77 68 69 6c 65 28 20   out);.  while( 
31b2a 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64 65 72  !dlrAtEnd(reader
31b2b 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 20 29 7b  s[0].pReader) ){
31b2c 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
31b2d 34 20 69 44 6f 63 69 64 20 3d 20 64 6c 72 44 6f  4 iDocid = dlrDo
31b2e 63 69 64 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  cid(readers[0].p
31b2f 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a  Reader);..    /*
31b30 20 49 66 20 74 68 69 73 20 69 73 20 61 20 63 6f   If this is a co
31b31 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68  ntinuation of th
31b32 65 20 63 75 72 72 65 6e 74 20 62 75 66 66 65 72  e current buffer
31b33 20 74 6f 20 63 6f 70 79 2c 20 65 78 74 65 6e 64   to copy, extend
31b34 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 62 75 66  .    ** that buf
31b35 66 65 72 2e 20 20 6d 65 6d 63 70 79 28 29 20 73  fer.  memcpy() s
31b36 65 65 6d 73 20 74 6f 20 62 65 20 6d 6f 72 65 20  eems to be more 
31b37 65 66 66 69 63 69 65 6e 74 20 69 66 20 69 74 20  efficient if it 
31b38 68 61 73 20 61 0a 20 20 20 20 2a 2a 20 6c 6f 74  has a.    ** lot
31b39 73 20 6f 66 20 64 61 74 61 20 74 6f 20 63 6f 70  s of data to cop
31b3a 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  y..    */.    if
31b3b 28 20 64 6c 72 44 6f 63 44 61 74 61 28 72 65 61  ( dlrDocData(rea
31b3c 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29  ders[0].pReader)
31b3d 3d 3d 70 53 74 61 72 74 2b 6e 53 74 61 72 74 20  ==pStart+nStart 
31b3e 29 7b 0a 20 20 20 20 20 20 6e 53 74 61 72 74 20  ){.      nStart 
31b3f 2b 3d 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74  += dlrDocDataByt
31b40 65 73 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52  es(readers[0].pR
31b41 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 65 6c 73  eader);.    }els
31b42 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 53 74  e{.      if( pSt
31b43 61 72 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  art!=0 ){.      
31b44 20 20 64 6c 77 41 70 70 65 6e 64 28 26 77 72 69    dlwAppend(&wri
31b45 74 65 72 2c 20 70 53 74 61 72 74 2c 20 6e 53 74  ter, pStart, nSt
31b46 61 72 74 2c 20 69 46 69 72 73 74 44 6f 63 69 64  art, iFirstDocid
31b47 2c 20 69 4c 61 73 74 44 6f 63 69 64 29 3b 0a 20  , iLastDocid);. 
31b48 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53 74       }.      pSt
31b49 61 72 74 20 3d 20 64 6c 72 44 6f 63 44 61 74 61  art = dlrDocData
31b4a 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61  (readers[0].pRea
31b4b 64 65 72 29 3b 0a 20 20 20 20 20 20 6e 53 74 61  der);.      nSta
31b4c 72 74 20 3d 20 64 6c 72 44 6f 63 44 61 74 61 42  rt = dlrDocDataB
31b4d 79 74 65 73 28 72 65 61 64 65 72 73 5b 30 5d 2e  ytes(readers[0].
31b4e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20  pReader);.      
31b4f 69 46 69 72 73 74 44 6f 63 69 64 20 3d 20 69 44  iFirstDocid = iD
31b50 6f 63 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ocid;.    }.    
31b51 69 4c 61 73 74 44 6f 63 69 64 20 3d 20 69 44 6f  iLastDocid = iDo
31b52 63 69 64 3b 0a 20 20 20 20 64 6c 72 53 74 65 70  cid;.    dlrStep
31b53 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61  (readers[0].pRea
31b54 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72  der);..    /* Dr
31b55 6f 70 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 6c  op all of the ol
31b56 64 65 72 20 65 6c 65 6d 65 6e 74 73 20 77 69 74  der elements wit
31b57 68 20 74 68 65 20 73 61 6d 65 20 64 6f 63 69 64  h the same docid
31b58 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31  . */.    for(i=1
31b59 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26 26 0a  ; i<nReaders &&.
31b5a 20 20 20 20 20 20 20 20 20 20 20 20 20 21 64 6c               !dl
31b5b 72 41 74 45 6e 64 28 72 65 61 64 65 72 73 5b 69  rAtEnd(readers[i
31b5c 5d 2e 70 52 65 61 64 65 72 29 20 26 26 0a 20 20  ].pReader) &&.  
31b5d 20 20 20 20 20 20 20 20 20 20 20 64 6c 72 44 6f             dlrDo
31b5e 63 69 64 28 72 65 61 64 65 72 73 5b 69 5d 2e 70  cid(readers[i].p
31b5f 52 65 61 64 65 72 29 3d 3d 69 44 6f 63 69 64 3b  Reader)==iDocid;
31b60 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 64 6c 72   i++){.      dlr
31b61 53 74 65 70 28 72 65 61 64 65 72 73 5b 69 5d 2e  Step(readers[i].
31b62 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a  pReader);.    }.
31b63 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20  .    /* Get the 
31b64 72 65 61 64 65 72 73 20 62 61 63 6b 20 69 6e 74  readers back int
31b65 6f 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20  o order. */.    
31b66 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29 7b 0a  while( i-->0 ){.
31b67 20 20 20 20 20 20 6f 72 64 65 72 65 64 44 4c 52        orderedDLR
31b68 65 61 64 65 72 52 65 6f 72 64 65 72 28 72 65 61  eaderReorder(rea
31b69 64 65 72 73 2b 69 2c 20 6e 52 65 61 64 65 72 73  ders+i, nReaders
31b6a 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  -i);.    }.  }..
31b6b 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20 61    /* Copy over a
31b6c 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20 65 6c 65  ny remaining ele
31b6d 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28 20  ments. */.  if( 
31b6e 6e 53 74 61 72 74 3e 30 20 29 20 64 6c 77 41 70  nStart>0 ) dlwAp
31b6f 70 65 6e 64 28 26 77 72 69 74 65 72 2c 20 70 53  pend(&writer, pS
31b70 74 61 72 74 2c 20 6e 53 74 61 72 74 2c 20 69 46  tart, nStart, iF
31b71 69 72 73 74 44 6f 63 69 64 2c 20 69 4c 61 73 74  irstDocid, iLast
31b72 44 6f 63 69 64 29 3b 0a 20 20 64 6c 77 44 65 73  Docid);.  dlwDes
31b73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d  troy(&writer);.}
31b74 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e 63  ../* Helper func
31b75 74 69 6f 6e 20 66 6f 72 20 70 6f 73 4c 69 73 74  tion for posList
31b76 55 6e 69 6f 6e 28 29 2e 20 20 43 6f 6d 70 61 72  Union().  Compar
31b77 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  es the current p
31b78 6f 73 69 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65  osition.** betwe
31b79 65 6e 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68  en left and righ
31b7a 74 2c 20 72 65 74 75 72 6e 69 6e 67 20 61 73 20  t, returning as 
31b7b 73 74 61 6e 64 61 72 64 20 43 20 69 64 69 6f 6d  standard C idiom
31b7c 20 6f 66 20 3c 30 20 69 66 0a 2a 2a 20 6c 65 66   of <0 if.** lef
31b7d 74 3c 72 69 67 68 74 2c 20 3e 30 20 69 66 20 6c  t<right, >0 if l
31b7e 65 66 74 3e 72 69 67 68 74 2c 20 61 6e 64 20 30  eft>right, and 0
31b7f 20 69 66 20 6c 65 66 74 3d 3d 72 69 67 68 74 2e   if left==right.
31b80 20 20 22 45 6e 64 22 20 61 6c 77 61 79 73 0a 2a    "End" always.*
31b81 2a 20 63 6f 6d 70 61 72 65 73 20 67 72 65 61 74  * compares great
31b82 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
31b83 74 20 70 6f 73 4c 69 73 74 43 6d 70 28 50 4c 52  t posListCmp(PLR
31b84 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 50 4c  eader *pLeft, PL
31b85 52 65 61 64 65 72 20 2a 70 52 69 67 68 74 29 7b  Reader *pRight){
31b86 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74  .  assert( pLeft
31b87 2d 3e 69 54 79 70 65 3d 3d 70 52 69 67 68 74 2d  ->iType==pRight-
31b88 3e 69 54 79 70 65 20 29 3b 0a 20 20 69 66 28 20  >iType );.  if( 
31b89 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 44 4c  pLeft->iType==DL
31b8a 5f 44 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e  _DOCIDS ) return
31b8b 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c 72 41 74   0;..  if( plrAt
31b8c 45 6e 64 28 70 4c 65 66 74 29 20 29 20 72 65 74  End(pLeft) ) ret
31b8d 75 72 6e 20 70 6c 72 41 74 45 6e 64 28 70 52 69  urn plrAtEnd(pRi
31b8e 67 68 74 29 20 3f 20 30 20 3a 20 31 3b 0a 20 20  ght) ? 0 : 1;.  
31b8f 69 66 28 20 70 6c 72 41 74 45 6e 64 28 70 52 69  if( plrAtEnd(pRi
31b90 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31  ght) ) return -1
31b91 3b 0a 0a 20 20 69 66 28 20 70 6c 72 43 6f 6c 75  ;..  if( plrColu
31b92 6d 6e 28 70 4c 65 66 74 29 3c 70 6c 72 43 6f 6c  mn(pLeft)<plrCol
31b93 75 6d 6e 28 70 52 69 67 68 74 29 20 29 20 72 65  umn(pRight) ) re
31b94 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 70  turn -1;.  if( p
31b95 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3e  lrColumn(pLeft)>
31b96 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74  plrColumn(pRight
31b97 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20  ) ) return 1;.. 
31b98 20 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e   if( plrPosition
31b99 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74  (pLeft)<plrPosit
31b9a 69 6f 6e 28 70 52 69 67 68 74 29 20 29 20 72 65  ion(pRight) ) re
31b9b 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 70  turn -1;.  if( p
31b9c 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74  lrPosition(pLeft
31b9d 29 3e 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52  )>plrPosition(pR
31b9e 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31  ight) ) return 1
31b9f 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 69  ;.  if( pLeft->i
31ba0 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  Type==DL_POSITIO
31ba1 4e 53 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a  NS ) return 0;..
31ba2 20 20 69 66 28 20 70 6c 72 53 74 61 72 74 4f 66    if( plrStartOf
31ba3 66 73 65 74 28 70 4c 65 66 74 29 3c 70 6c 72 53  fset(pLeft)<plrS
31ba4 74 61 72 74 4f 66 66 73 65 74 28 70 52 69 67 68  tartOffset(pRigh
31ba5 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  t) ) return -1;.
31ba6 20 20 69 66 28 20 70 6c 72 53 74 61 72 74 4f 66    if( plrStartOf
31ba7 66 73 65 74 28 70 4c 65 66 74 29 3e 70 6c 72 53  fset(pLeft)>plrS
31ba8 74 61 72 74 4f 66 66 73 65 74 28 70 52 69 67 68  tartOffset(pRigh
31ba9 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a  t) ) return 1;..
31baa 20 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66 73    if( plrEndOffs
31bab 65 74 28 70 4c 65 66 74 29 3c 70 6c 72 45 6e 64  et(pLeft)<plrEnd
31bac 4f 66 66 73 65 74 28 70 52 69 67 68 74 29 20 29  Offset(pRight) )
31bad 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
31bae 28 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 70  ( plrEndOffset(p
31baf 4c 65 66 74 29 3e 70 6c 72 45 6e 64 4f 66 66 73  Left)>plrEndOffs
31bb0 65 74 28 70 52 69 67 68 74 29 20 29 20 72 65 74  et(pRight) ) ret
31bb1 75 72 6e 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e  urn 1;..  return
31bb2 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20   0;.}../* Write 
31bb3 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 70 6f 73  the union of pos
31bb4 69 74 69 6f 6e 20 6c 69 73 74 73 20 69 6e 20 70  ition lists in p
31bb5 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20  Left and pRight 
31bb6 74 6f 20 70 4f 75 74 2e 0a 2a 2a 20 22 55 6e 69  to pOut..** "Uni
31bb7 6f 6e 22 20 69 6e 20 74 68 69 73 20 63 61 73 65  on" in this case
31bb8 20 6d 65 61 6e 69 6e 67 20 22 41 6c 6c 20 75 6e   meaning "All un
31bb9 69 71 75 65 20 70 6f 73 69 74 69 6f 6e 20 74 75  ique position tu
31bba 70 6c 65 73 22 2e 20 20 53 68 6f 75 6c 64 0a 2a  ples".  Should.*
31bbb 2a 20 77 6f 72 6b 20 77 69 74 68 20 61 6e 79 20  * work with any 
31bbc 64 6f 63 6c 69 73 74 20 74 79 70 65 2c 20 74 68  doclist type, th
31bbd 6f 75 67 68 20 62 6f 74 68 20 69 6e 70 75 74 73  ough both inputs
31bbe 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a   and the output.
31bbf 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  ** should be the
31bc0 20 73 61 6d 65 20 74 79 70 65 2e 0a 2a 2f 0a 73   same type..*/.s
31bc1 74 61 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69  tatic void posLi
31bc2 73 74 55 6e 69 6f 6e 28 44 4c 52 65 61 64 65 72  stUnion(DLReader
31bc3 20 2a 70 4c 65 66 74 2c 20 44 4c 52 65 61 64 65   *pLeft, DLReade
31bc4 72 20 2a 70 52 69 67 68 74 2c 20 44 4c 57 72 69  r *pRight, DLWri
31bc5 74 65 72 20 2a 70 4f 75 74 29 7b 0a 20 20 50 4c  ter *pOut){.  PL
31bc6 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67  Reader left, rig
31bc7 68 74 3b 0a 20 20 50 4c 57 72 69 74 65 72 20 77  ht;.  PLWriter w
31bc8 72 69 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  riter;..  assert
31bc9 28 20 64 6c 72 44 6f 63 69 64 28 70 4c 65 66 74  ( dlrDocid(pLeft
31bca 29 3d 3d 64 6c 72 44 6f 63 69 64 28 70 52 69 67  )==dlrDocid(pRig
31bcb 68 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ht) );.  assert(
31bcc 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70   pLeft->iType==p
31bcd 52 69 67 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a  Right->iType );.
31bce 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d    assert( pLeft-
31bcf 3e 69 54 79 70 65 3d 3d 70 4f 75 74 2d 3e 69 54  >iType==pOut->iT
31bd0 79 70 65 20 29 3b 0a 0a 20 20 70 6c 72 49 6e 69  ype );..  plrIni
31bd1 74 28 26 6c 65 66 74 2c 20 70 4c 65 66 74 29 3b  t(&left, pLeft);
31bd2 0a 20 20 70 6c 72 49 6e 69 74 28 26 72 69 67 68  .  plrInit(&righ
31bd3 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 6c  t, pRight);.  pl
31bd4 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70  wInit(&writer, p
31bd5 4f 75 74 2c 20 64 6c 72 44 6f 63 69 64 28 70 4c  Out, dlrDocid(pL
31bd6 65 66 74 29 29 3b 0a 0a 20 20 77 68 69 6c 65 28  eft));..  while(
31bd7 20 21 70 6c 72 41 74 45 6e 64 28 26 6c 65 66 74   !plrAtEnd(&left
31bd8 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64 28 26  ) || !plrAtEnd(&
31bd9 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 6e  right) ){.    in
31bda 74 20 63 20 3d 20 70 6f 73 4c 69 73 74 43 6d 70  t c = posListCmp
31bdb 28 26 6c 65 66 74 2c 20 26 72 69 67 68 74 29 3b  (&left, &right);
31bdc 0a 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a  .    if( c<0 ){.
31bdd 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77        plwCopy(&w
31bde 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20  riter, &left);. 
31bdf 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c 65       plrStep(&le
31be0 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
31be1 66 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 20 20  f( c>0 ){.      
31be2 70 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c  plwCopy(&writer,
31be3 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20   &right);.      
31be4 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  plrStep(&right);
31be5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
31be6 20 20 70 6c 77 43 6f 70 79 28 26 77 72 69 74 65    plwCopy(&write
31be7 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  r, &left);.     
31be8 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b   plrStep(&left);
31be9 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26  .      plrStep(&
31bea 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20  right);.    }.  
31beb 7d 0a 0a 20 20 70 6c 77 54 65 72 6d 69 6e 61 74  }..  plwTerminat
31bec 65 28 26 77 72 69 74 65 72 29 3b 0a 20 20 70 6c  e(&writer);.  pl
31bed 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  wDestroy(&writer
31bee 29 3b 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28  );.  plrDestroy(
31bef 26 6c 65 66 74 29 3b 0a 20 20 70 6c 72 44 65 73  &left);.  plrDes
31bf0 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 7d 0a  troy(&right);.}.
31bf1 0a 2f 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e  ./* Write the un
31bf2 69 6f 6e 20 6f 66 20 64 6f 63 6c 69 73 74 73 20  ion of doclists 
31bf3 69 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  in pLeft and pRi
31bf4 67 68 74 20 74 6f 20 70 4f 75 74 2e 20 20 46 6f  ght to pOut.  Fo
31bf5 72 0a 2a 2a 20 64 6f 63 69 64 73 20 69 6e 20 63  r.** docids in c
31bf6 6f 6d 6d 6f 6e 20 62 65 74 77 65 65 6e 20 74 68  ommon between th
31bf7 65 20 69 6e 70 75 74 73 2c 20 74 68 65 20 75 6e  e inputs, the un
31bf8 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 73 69 74  ion of the posit
31bf9 69 6f 6e 0a 2a 2a 20 6c 69 73 74 73 20 69 73 20  ion.** lists is 
31bfa 77 72 69 74 74 65 6e 2e 20 20 49 6e 70 75 74 73  written.  Inputs
31bfb 20 61 6e 64 20 6f 75 74 70 75 74 73 20 61 72 65   and outputs are
31bfc 20 61 6c 77 61 79 73 20 74 79 70 65 20 44 4c 5f   always type DL_
31bfd 44 45 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74  DEFAULT..*/.stat
31bfe 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 55  ic void docListU
31bff 6e 69 6f 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68  nion(.  const ch
31c00 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e  ar *pLeft, int n
31c01 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Left,.  const ch
31c02 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20  ar *pRight, int 
31c03 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75  nRight,.  DataBu
31c04 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20  ffer *pOut      
31c05 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d  /* Write the com
31c06 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65  bined doclist he
31c07 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61  re */.){.  DLRea
31c08 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b  der left, right;
31c09 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74  .  DLWriter writ
31c0a 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74  er;..  if( nLeft
31c0b 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  ==0 ){.    if( n
31c0c 52 69 67 68 74 21 3d 30 29 20 64 61 74 61 42 75  Right!=0) dataBu
31c0d 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c  fferAppend(pOut,
31c0e 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29   pRight, nRight)
31c0f 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
31c10 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d  }.  if( nRight==
31c11 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
31c12 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20  ferAppend(pOut, 
31c13 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
31c14 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
31c15 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    dlrInit(&left,
31c16 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 4c 65   DL_DEFAULT, pLe
31c17 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c  ft, nLeft);.  dl
31c18 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c  rInit(&right, DL
31c19 5f 44 45 46 41 55 4c 54 2c 20 70 52 69 67 68 74  _DEFAULT, pRight
31c1a 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77  , nRight);.  dlw
31c1b 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c  Init(&writer, DL
31c1c 5f 44 45 46 41 55 4c 54 2c 20 70 4f 75 74 29 3b  _DEFAULT, pOut);
31c1d 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41  ..  while( !dlrA
31c1e 74 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20 21  tEnd(&left) || !
31c1f 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  dlrAtEnd(&right)
31c20 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41   ){.    if( dlrA
31c21 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a  tEnd(&right) ){.
31c22 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77        dlwCopy(&w
31c23 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20  riter, &left);. 
31c24 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
31c25 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
31c26 66 28 20 64 6c 72 41 74 45 6e 64 28 26 6c 65 66  f( dlrAtEnd(&lef
31c27 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43  t) ){.      dlwC
31c28 6f 70 79 28 26 77 72 69 74 65 72 2c 20 26 72 69  opy(&writer, &ri
31c29 67 68 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53  ght);.      dlrS
31c2a 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
31c2b 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f   }else if( dlrDo
31c2c 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f  cid(&left)<dlrDo
31c2d 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  cid(&right) ){. 
31c2e 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77 72       dlwCopy(&wr
31c2f 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20  iter, &left);.  
31c30 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
31c31 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  t);.    }else if
31c32 28 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  ( dlrDocid(&left
31c33 29 3e 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68  )>dlrDocid(&righ
31c34 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43  t) ){.      dlwC
31c35 6f 70 79 28 26 77 72 69 74 65 72 2c 20 26 72 69  opy(&writer, &ri
31c36 67 68 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53  ght);.      dlrS
31c37 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
31c38 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 6f   }else{.      po
31c39 73 4c 69 73 74 55 6e 69 6f 6e 28 26 6c 65 66 74  sListUnion(&left
31c3a 2c 20 26 72 69 67 68 74 2c 20 26 77 72 69 74 65  , &right, &write
31c3b 72 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  r);.      dlrSte
31c3c 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  p(&left);.      
31c3d 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  dlrStep(&right);
31c3e 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c  .    }.  }..  dl
31c3f 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b  rDestroy(&left);
31c40 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
31c41 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74  ight);.  dlwDest
31c42 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a  roy(&writer);.}.
31c43 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./* .** This fun
31c44 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73  ction is used as
31c45 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70   part of the imp
31c46 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70  lementation of p
31c47 68 72 61 73 65 20 61 6e 64 0a 2a 2a 20 4e 45 41  hrase and.** NEA
31c48 52 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2a 0a 2a  R matching..**.*
31c49 2a 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  * pLeft and pRig
31c4a 68 74 20 61 72 65 20 44 4c 52 65 61 64 65 72 73  ht are DLReaders
31c4b 20 70 6f 73 69 74 69 6f 6e 65 64 20 74 6f 20 74   positioned to t
31c4c 68 65 20 73 61 6d 65 20 64 6f 63 69 64 20 69 6e  he same docid in
31c4d 0a 2a 2a 20 6c 69 73 74 73 20 6f 66 20 74 79 70  .** lists of typ
31c4e 65 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 2e 20 54  e DL_POSITION. T
31c4f 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69  his function wri
31c50 74 65 73 20 61 6e 20 65 6e 74 72 79 20 74 6f 20  tes an entry to 
31c51 74 68 65 0a 2a 2a 20 44 4c 57 72 69 74 65 72 20  the.** DLWriter 
31c52 70 4f 75 74 20 66 6f 72 20 65 61 63 68 20 70 6f  pOut for each po
31c53 73 69 74 69 6f 6e 20 69 6e 20 70 52 69 67 68 74  sition in pRight
31c54 20 74 68 61 74 20 69 73 20 6c 65 73 73 20 74 68   that is less th
31c55 61 6e 0a 2a 2a 20 28 6e 4e 65 61 72 2b 31 29 20  an.** (nNear+1) 
31c56 67 72 65 61 74 65 72 20 28 62 75 74 20 6e 6f 74  greater (but not
31c57 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 73 6d 61   equal to or sma
31c58 6c 6c 65 72 29 20 74 68 61 6e 20 61 20 70 6f 73  ller) than a pos
31c59 69 74 69 6f 6e 20 0a 2a 2a 20 69 6e 20 70 4c 65  ition .** in pLe
31c5a 66 74 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  ft. For example,
31c5b 20 69 66 20 6e 4e 65 61 72 20 69 73 20 30 2c 20   if nNear is 0, 
31c5c 61 6e 64 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  and the position
31c5d 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 62  s contained.** b
31c5e 79 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  y pLeft and pRig
31c5f 68 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  ht are:.**.**   
31c60 20 70 4c 65 66 74 3a 20 20 35 20 31 30 20 31 35   pLeft:  5 10 15
31c61 20 32 30 0a 2a 2a 20 20 20 20 70 52 69 67 68 74   20.**    pRight
31c62 3a 20 36 20 20 39 20 31 37 20 32 31 0a 2a 2a 0a  : 6  9 17 21.**.
31c63 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 6f 63 69  ** then the doci
31c64 64 20 69 73 20 61 64 64 65 64 20 74 6f 20 70 4f  d is added to pO
31c65 75 74 2e 20 49 66 20 70 4f 75 74 20 69 73 20 6f  ut. If pOut is o
31c66 66 20 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49  f type DL_POSITI
31c67 4f 4e 53 2c 0a 2a 2a 20 74 68 65 6e 20 61 20 70  ONS,.** then a p
31c68 6f 73 69 74 69 6f 6e 69 64 73 20 22 36 22 20 61  ositionids "6" a
31c69 6e 64 20 22 32 31 22 20 61 72 65 20 61 6c 73 6f  nd "21" are also
31c6a 20 61 64 64 65 64 20 74 6f 20 70 4f 75 74 2e 0a   added to pOut..
31c6b 2a 2a 0a 2a 2a 20 49 66 20 62 6f 6f 6c 65 61 6e  **.** If boolean
31c6c 20 61 72 67 75 6d 65 6e 74 20 69 73 53 61 76 65   argument isSave
31c6d 4c 65 66 74 20 69 73 20 74 72 75 65 2c 20 74 68  Left is true, th
31c6e 65 6e 20 70 6f 73 69 74 69 6f 6e 69 64 73 20 61  en positionids a
31c6f 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f  re copied.** fro
31c70 6d 20 70 4c 65 66 74 20 69 6e 73 74 65 61 64 20  m pLeft instead 
31c71 6f 66 20 70 52 69 67 68 74 2e 20 49 6e 20 74 68  of pRight. In th
31c72 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c  e example above,
31c73 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 22   the positions "
31c74 35 22 0a 2a 2a 20 61 6e 64 20 22 32 30 22 20 77  5".** and "20" w
31c75 6f 75 6c 64 20 62 65 20 61 64 64 65 64 20 69 6e  ould be added in
31c76 73 74 65 61 64 20 6f 66 20 22 36 22 20 61 6e 64  stead of "6" and
31c77 20 22 32 31 22 2e 0a 2a 2f 0a 73 74 61 74 69 63   "21"..*/.static
31c78 20 76 6f 69 64 20 70 6f 73 4c 69 73 74 50 68 72   void posListPhr
31c79 61 73 65 4d 65 72 67 65 28 0a 20 20 44 4c 52 65  aseMerge(.  DLRe
31c7a 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20  ader *pLeft, .  
31c7b 44 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68 74  DLReader *pRight
31c7c 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72 2c 0a 20  ,.  int nNear,. 
31c7d 20 69 6e 74 20 69 73 53 61 76 65 4c 65 66 74 2c   int isSaveLeft,
31c7e 0a 20 20 44 4c 57 72 69 74 65 72 20 2a 70 4f 75  .  DLWriter *pOu
31c7f 74 0a 29 7b 0a 20 20 50 4c 52 65 61 64 65 72 20  t.){.  PLReader 
31c80 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 50  left, right;.  P
31c81 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
31c82 20 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b    int match = 0;
31c83 0a 0a 20 20 61 73 73 65 72 74 28 20 64 6c 72 44  ..  assert( dlrD
31c84 6f 63 69 64 28 70 4c 65 66 74 29 3d 3d 64 6c 72  ocid(pLeft)==dlr
31c85 44 6f 63 69 64 28 70 52 69 67 68 74 29 20 29 3b  Docid(pRight) );
31c86 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d  .  assert( pOut-
31c87 3e 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49 54  >iType!=DL_POSIT
31c88 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b 0a  IONS_OFFSETS );.
31c89 0a 20 20 70 6c 72 49 6e 69 74 28 26 6c 65 66 74  .  plrInit(&left
31c8a 2c 20 70 4c 65 66 74 29 3b 0a 20 20 70 6c 72 49  , pLeft);.  plrI
31c8b 6e 69 74 28 26 72 69 67 68 74 2c 20 70 52 69 67  nit(&right, pRig
31c8c 68 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21  ht);..  while( !
31c8d 70 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20  plrAtEnd(&left) 
31c8e 26 26 20 21 70 6c 72 41 74 45 6e 64 28 26 72 69  && !plrAtEnd(&ri
31c8f 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20  ght) ){.    if( 
31c90 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29  plrColumn(&left)
31c91 3c 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68  <plrColumn(&righ
31c92 74 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53  t) ){.      plrS
31c93 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  tep(&left);.    
31c94 7d 65 6c 73 65 20 69 66 28 20 70 6c 72 43 6f 6c  }else if( plrCol
31c95 75 6d 6e 28 26 6c 65 66 74 29 3e 70 6c 72 43 6f  umn(&left)>plrCo
31c96 6c 75 6d 6e 28 26 72 69 67 68 74 29 20 29 7b 0a  lumn(&right) ){.
31c97 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72        plrStep(&r
31c98 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ight);.    }else
31c99 20 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e   if( plrPosition
31c9a 28 26 6c 65 66 74 29 3e 3d 70 6c 72 50 6f 73 69  (&left)>=plrPosi
31c9b 74 69 6f 6e 28 26 72 69 67 68 74 29 20 29 7b 0a  tion(&right) ){.
31c9c 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72        plrStep(&r
31c9d 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ight);.    }else
31c9e 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 6c 72  {.      if( (plr
31c9f 50 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74 29  Position(&right)
31ca0 2d 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65  -plrPosition(&le
31ca1 66 74 29 29 3c 3d 28 6e 4e 65 61 72 2b 31 29 20  ft))<=(nNear+1) 
31ca2 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ){.        if( !
31ca3 6d 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20  match ){.       
31ca4 20 20 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74     plwInit(&writ
31ca5 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63  er, pOut, dlrDoc
31ca6 69 64 28 70 4c 65 66 74 29 29 3b 0a 20 20 20 20  id(pLeft));.    
31ca7 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 31 3b        match = 1;
31ca8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
31ca9 20 20 20 69 66 28 20 21 69 73 53 61 76 65 4c 65     if( !isSaveLe
31caa 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
31cab 70 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  plwAdd(&writer, 
31cac 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74  plrColumn(&right
31cad 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26  ), plrPosition(&
31cae 72 69 67 68 74 29 2c 20 30 2c 20 30 29 3b 0a 20  right), 0, 0);. 
31caf 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
31cb0 20 20 20 20 20 20 20 20 70 6c 77 41 64 64 28 26          plwAdd(&
31cb1 77 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d  writer, plrColum
31cb2 6e 28 26 6c 65 66 74 29 2c 20 70 6c 72 50 6f 73  n(&left), plrPos
31cb3 69 74 69 6f 6e 28 26 6c 65 66 74 29 2c 20 30 2c  ition(&left), 0,
31cb4 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
31cb5 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26         plrStep(&
31cb6 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 65  right);.      }e
31cb7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 6c 72  lse{.        plr
31cb8 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
31cb9 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
31cba 20 20 69 66 28 20 6d 61 74 63 68 20 29 7b 0a 20    if( match ){. 
31cbb 20 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28     plwTerminate(
31cbc 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 70 6c  &writer);.    pl
31cbd 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  wDestroy(&writer
31cbe 29 3b 0a 20 20 7d 0a 0a 20 20 70 6c 72 44 65 73  );.  }..  plrDes
31cbf 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70  troy(&left);.  p
31cc0 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74  lrDestroy(&right
31cc1 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  );.}../*.** Comp
31cc2 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 70  are the values p
31cc3 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65  ointed to by the
31cc4 20 50 4c 52 65 61 64 65 72 73 20 70 61 73 73 65   PLReaders passe
31cc5 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73 2e 20  d as arguments. 
31cc6 0a 2a 2a 20 52 65 74 75 72 6e 20 2d 31 20 69 66  .** Return -1 if
31cc7 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74   the value point
31cc8 65 64 20 74 6f 20 62 79 20 70 4c 65 66 74 20 69  ed to by pLeft i
31cc9 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  s considered les
31cca 73 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 76 61  s than.** the va
31ccb 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  lue pointed to b
31ccc 79 20 70 52 69 67 68 74 2c 20 2b 31 20 69 66 20  y pRight, +1 if 
31ccd 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  it is considered
31cce 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e   greater.** than
31ccf 20 69 74 2c 20 6f 72 20 30 20 69 66 20 69 74 20   it, or 0 if it 
31cd0 69 73 20 65 71 75 61 6c 2e 20 69 2e 65 2e 0a 2a  is equal. i.e..*
31cd1 2a 0a 2a 2a 20 20 20 20 20 28 2a 70 4c 65 66 74  *.**     (*pLeft
31cd2 20 2d 20 2a 70 52 69 67 68 74 29 0a 2a 2a 0a 2a   - *pRight).**.*
31cd3 2a 20 41 20 50 4c 52 65 61 64 65 72 20 74 68 61  * A PLReader tha
31cd4 74 20 69 73 20 69 6e 20 74 68 65 20 45 4f 46 20  t is in the EOF 
31cd5 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 63 6f 6e  condition is con
31cd6 73 69 64 65 72 65 64 20 67 72 65 61 74 65 72 20  sidered greater 
31cd7 74 68 61 6e 0a 2a 2a 20 61 6e 79 20 6f 74 68 65  than.** any othe
31cd8 72 2e 20 49 66 20 6e 65 69 74 68 65 72 20 61 72  r. If neither ar
31cd9 67 75 6d 65 6e 74 20 69 73 20 69 6e 20 45 4f 46  gument is in EOF
31cda 20 73 74 61 74 65 2c 20 74 68 65 20 72 65 74 75   state, the retu
31cdb 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 70  rn value of.** p
31cdc 6c 72 43 6f 6c 75 6d 6e 28 29 20 69 73 20 75 73  lrColumn() is us
31cdd 65 64 2e 20 49 66 20 74 68 65 20 70 6c 72 43 6f  ed. If the plrCo
31cde 6c 75 6d 6e 28 29 20 76 61 6c 75 65 73 20 61 72  lumn() values ar
31cdf 65 20 65 71 75 61 6c 2c 20 74 68 65 0a 2a 2a 20  e equal, the.** 
31ce0 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6f 6e  comparison is on
31ce1 20 74 68 65 20 62 61 73 69 73 20 6f 66 20 70 6c   the basis of pl
31ce2 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a  rPosition()..*/.
31ce3 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 43 6f  static int plrCo
31ce4 6d 70 61 72 65 28 50 4c 52 65 61 64 65 72 20 2a  mpare(PLReader *
31ce5 70 4c 65 66 74 2c 20 50 4c 52 65 61 64 65 72 20  pLeft, PLReader 
31ce6 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73 65  *pRight){.  asse
31ce7 72 74 28 21 70 6c 72 41 74 45 6e 64 28 70 4c 65  rt(!plrAtEnd(pLe
31ce8 66 74 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64  ft) || !plrAtEnd
31ce9 28 70 52 69 67 68 74 29 29 3b 0a 0a 20 20 69 66  (pRight));..  if
31cea 28 20 70 6c 72 41 74 45 6e 64 28 70 52 69 67 68  ( plrAtEnd(pRigh
31ceb 74 29 20 7c 7c 20 70 6c 72 41 74 45 6e 64 28 70  t) || plrAtEnd(p
31cec 4c 65 66 74 29 20 29 7b 0a 20 20 20 20 72 65 74  Left) ){.    ret
31ced 75 72 6e 20 28 70 6c 72 41 74 45 6e 64 28 70 52  urn (plrAtEnd(pR
31cee 69 67 68 74 29 20 3f 20 2d 31 20 3a 20 31 29 3b  ight) ? -1 : 1);
31cef 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72 43 6f  .  }.  if( plrCo
31cf0 6c 75 6d 6e 28 70 4c 65 66 74 29 21 3d 70 6c 72  lumn(pLeft)!=plr
31cf1 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29 20 29  Column(pRight) )
31cf2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28 70  {.    return ((p
31cf3 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c  lrColumn(pLeft)<
31cf4 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74  plrColumn(pRight
31cf5 29 29 20 3f 20 2d 31 20 3a 20 31 29 3b 0a 20 20  )) ? -1 : 1);.  
31cf6 7d 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69 74  }.  if( plrPosit
31cf7 69 6f 6e 28 70 4c 65 66 74 29 21 3d 70 6c 72 50  ion(pLeft)!=plrP
31cf8 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 20  osition(pRight) 
31cf9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28  ){.    return ((
31cfa 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66  plrPosition(pLef
31cfb 74 29 3c 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70  t)<plrPosition(p
31cfc 52 69 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31  Right)) ? -1 : 1
31cfd 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
31cfe 30 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65  0;.}../* We have
31cff 20 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 77 69   two doclists wi
31d00 74 68 20 70 6f 73 69 74 69 6f 6e 73 3a 20 20 70  th positions:  p
31d01 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e  Left and pRight.
31d02 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e   Depending.** on
31d03 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
31d04 65 20 6e 4e 65 61 72 20 70 61 72 61 6d 65 74 65  e nNear paramete
31d05 72 2c 20 70 65 72 66 6f 72 6d 20 65 69 74 68 65  r, perform eithe
31d06 72 20 61 20 70 68 72 61 73 65 0a 2a 2a 20 69 6e  r a phrase.** in
31d07 74 65 72 73 65 63 74 69 6f 6e 20 28 69 66 20 6e  tersection (if n
31d08 4e 65 61 72 3d 3d 30 29 20 6f 72 20 61 20 4e 45  Near==0) or a NE
31d09 41 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  AR intersection 
31d0a 28 69 66 20 6e 4e 65 61 72 3e 30 29 0a 2a 2a 20  (if nNear>0).** 
31d0b 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65  and write the re
31d0c 73 75 6c 74 73 20 69 6e 74 6f 20 70 4f 75 74 2e  sults into pOut.
31d0d 0a 2a 2a 0a 2a 2a 20 41 20 70 68 72 61 73 65 20  .**.** A phrase 
31d0e 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d 65 61  intersection mea
31d0f 6e 73 20 74 68 61 74 20 74 77 6f 20 64 6f 63 75  ns that two docu
31d10 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74 63 68  ments only match
31d11 0a 2a 2a 20 69 66 20 70 4c 65 66 74 2e 69 50 6f  .** if pLeft.iPo
31d12 73 2b 31 3d 3d 70 52 69 67 68 74 2e 69 50 6f 73  s+1==pRight.iPos
31d13 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 45 41 52 20 69  ..**.** A NEAR i
31d14 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d 65 61 6e  ntersection mean
31d15 73 20 74 68 61 74 20 74 77 6f 20 64 6f 63 75 6d  s that two docum
31d16 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74 63 68 20  ents only match 
31d17 69 66 20 0a 2a 2a 20 28 61 62 73 28 70 4c 65 66  if .** (abs(pLef
31d18 74 2e 69 50 6f 73 2d 70 52 69 67 68 74 2e 69 50  t.iPos-pRight.iP
31d19 6f 73 29 3c 6e 4e 65 61 72 29 2e 0a 2a 2a 0a 2a  os)<nNear)..**.*
31d1a 2a 20 49 66 20 61 20 4e 45 41 52 20 69 6e 74 65  * If a NEAR inte
31d1b 72 73 65 63 74 69 6f 6e 20 69 73 20 72 65 71 75  rsection is requ
31d1c 65 73 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20  ested, then the 
31d1d 6e 50 68 72 61 73 65 20 61 72 67 75 6d 65 6e 74  nPhrase argument
31d1e 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 70 61   should.** be pa
31d1f 73 73 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20  ssed the number 
31d20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65  of tokens in the
31d21 20 74 77 6f 20 6f 70 65 72 61 6e 64 73 20 74 6f   two operands to
31d22 20 74 68 65 20 4e 45 41 52 20 6f 70 65 72 61 74   the NEAR operat
31d23 6f 72 0a 2a 2a 20 63 6f 6d 62 69 6e 65 64 2e 20  or.** combined. 
31d24 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  For example:.**.
31d25 2a 2a 20 20 20 20 20 20 20 51 75 65 72 79 20 73  **       Query s
31d26 79 6e 74 61 78 20 20 20 20 20 20 20 20 20 20 20  yntax           
31d27 20 20 20 20 6e 50 68 72 61 73 65 0a 2a 2a 20 20      nPhrase.**  
31d28 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      ------------
31d29 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31d2a 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20  --------.**     
31d2b 20 20 22 41 20 42 20 43 22 20 4e 45 41 52 20 22    "A B C" NEAR "
31d2c 44 20 45 22 20 20 20 20 20 20 20 20 20 35 0a 2a  D E"         5.*
31d2d 2a 20 20 20 20 20 20 20 41 20 4e 45 41 52 20 42  *       A NEAR B
31d2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31d2f 20 20 20 32 0a 2a 2a 0a 2a 2a 20 69 54 79 70 65     2.**.** iType
31d30 20 63 6f 6e 74 72 6f 6c 73 20 74 68 65 20 74 79   controls the ty
31d31 70 65 20 6f 66 20 64 61 74 61 20 77 72 69 74 74  pe of data writt
31d32 65 6e 20 74 6f 20 70 4f 75 74 2e 20 20 49 66 20  en to pOut.  If 
31d33 69 54 79 70 65 20 69 73 0a 2a 2a 20 44 4c 5f 50  iType is.** DL_P
31d34 4f 53 49 54 49 4f 4e 53 2c 20 74 68 65 20 70 6f  OSITIONS, the po
31d35 73 69 74 69 6f 6e 73 20 61 72 65 20 74 68 6f 73  sitions are thos
31d36 65 20 66 72 6f 6d 20 70 52 69 67 68 74 2e 0a 2a  e from pRight..*
31d37 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f  /.static void do
31d38 63 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65  cListPhraseMerge
31d39 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
31d3a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
31d3b 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
31d3c 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
31d3d 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72 2c  ht,.  int nNear,
31d3e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30              /* 0
31d3f 20 66 6f 72 20 61 20 70 68 72 61 73 65 20 6d 65   for a phrase me
31d40 72 67 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f  rge, non-zero fo
31d41 72 20 61 20 4e 45 41 52 20 6d 65 72 67 65 20 2a  r a NEAR merge *
31d42 2f 0a 20 20 69 6e 74 20 6e 50 68 72 61 73 65 2c  /.  int nPhrase,
31d43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
31d44 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e  ber of tokens in
31d45 20 6c 65 66 74 2b 72 69 67 68 74 20 6f 70 65 72   left+right oper
31d46 61 6e 64 73 20 74 6f 20 4e 45 41 52 20 2a 2f 0a  ands to NEAR */.
31d47 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54    DocListType iT
31d48 79 70 65 2c 20 20 20 20 2f 2a 20 54 79 70 65 20  ype,    /* Type 
31d49 6f 66 20 64 6f 63 6c 69 73 74 20 74 6f 20 77 72  of doclist to wr
31d4a 69 74 65 20 74 6f 20 70 4f 75 74 20 2a 2f 0a 20  ite to pOut */. 
31d4b 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75   DataBuffer *pOu
31d4c 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  t      /* Write 
31d4d 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63  the combined doc
31d4e 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  list here */.){.
31d4f 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c    DLReader left,
31d50 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74   right;.  DLWrit
31d51 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66  er writer;..  if
31d52 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c 20 6e 52  ( nLeft==0 || nR
31d53 69 67 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  ight==0 ) return
31d54 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 54 79  ;..  assert( iTy
31d55 70 65 21 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  pe!=DL_POSITIONS
31d56 5f 4f 46 46 53 45 54 53 20 29 3b 0a 0a 20 20 64  _OFFSETS );..  d
31d57 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c  lrInit(&left, DL
31d58 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70 4c 65 66  _POSITIONS, pLef
31d59 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72  t, nLeft);.  dlr
31d5a 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f  Init(&right, DL_
31d5b 50 4f 53 49 54 49 4f 4e 53 2c 20 70 52 69 67 68  POSITIONS, pRigh
31d5c 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c  t, nRight);.  dl
31d5d 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 69  wInit(&writer, i
31d5e 54 79 70 65 2c 20 70 4f 75 74 29 3b 0a 0a 20 20  Type, pOut);..  
31d5f 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64  while( !dlrAtEnd
31d60 28 26 6c 65 66 74 29 20 26 26 20 21 64 6c 72 41  (&left) && !dlrA
31d61 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a  tEnd(&right) ){.
31d62 20 20 20 20 69 66 28 20 64 6c 72 44 6f 63 69 64      if( dlrDocid
31d63 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64  (&left)<dlrDocid
31d64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20  (&right) ){.    
31d65 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29    dlrStep(&left)
31d66 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
31d67 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
31d68 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29  <dlrDocid(&left)
31d69 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74 65   ){.      dlrSte
31d6a 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
31d6b 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
31d6c 6e 4e 65 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20  nNear==0 ){.    
31d6d 20 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73      posListPhras
31d6e 65 4d 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72  eMerge(&left, &r
31d6f 69 67 68 74 2c 20 30 2c 20 30 2c 20 26 77 72 69  ight, 0, 0, &wri
31d70 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ter);.      }els
31d71 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e{.        /* Th
31d72 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 77  is case occurs w
31d73 68 65 6e 20 74 77 6f 20 74 65 72 6d 73 20 28 73  hen two terms (s
31d74 69 6d 70 6c 65 20 74 65 72 6d 73 20 6f 72 20 70  imple terms or p
31d75 68 72 61 73 65 73 29 20 61 72 65 0a 20 20 20 20  hrases) are.    
31d76 20 20 20 20 20 2a 20 63 6f 6e 6e 65 63 74 65 64       * connected
31d77 20 62 79 20 61 20 4e 45 41 52 20 6f 70 65 72 61   by a NEAR opera
31d78 74 6f 72 2c 20 73 70 61 6e 20 28 6e 4e 65 61 72  tor, span (nNear
31d79 2b 31 29 2e 20 69 2e 65 2e 0a 20 20 20 20 20 20  +1). i.e..      
31d7a 20 20 20 2a 0a 20 20 20 20 20 20 20 20 20 2a 20     *.         * 
31d7b 20 20 20 20 27 22 74 65 72 72 69 62 6c 65 20 63      '"terrible c
31d7c 6f 6d 70 61 6e 79 22 20 4e 45 41 52 20 77 69 64  ompany" NEAR wid
31d7d 67 65 74 27 0a 20 20 20 20 20 20 20 20 20 2a 2f  get'.         */
31d7e 0a 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66  .        DataBuf
31d7f 66 65 72 20 6f 6e 65 20 3d 20 7b 30 2c 20 30 2c  fer one = {0, 0,
31d80 20 30 7d 3b 0a 20 20 20 20 20 20 20 20 44 61 74   0};.        Dat
31d81 61 42 75 66 66 65 72 20 74 77 6f 20 3d 20 7b 30  aBuffer two = {0
31d82 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20 20  , 0, 0};..      
31d83 20 20 44 4c 57 72 69 74 65 72 20 64 6c 77 72 69    DLWriter dlwri
31d84 74 65 72 32 3b 0a 20 20 20 20 20 20 20 20 44 4c  ter2;.        DL
31d85 52 65 61 64 65 72 20 64 72 31 20 3d 20 7b 30 2c  Reader dr1 = {0,
31d86 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b 20 0a 20   0, 0, 0, 0}; . 
31d87 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20         DLReader 
31d88 64 72 32 20 3d 20 7b 30 2c 20 30 2c 20 30 2c 20  dr2 = {0, 0, 0, 
31d89 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20 20 20 20  0, 0};..        
31d8a 64 6c 77 49 6e 69 74 28 26 64 6c 77 72 69 74 65  dlwInit(&dlwrite
31d8b 72 32 2c 20 69 54 79 70 65 2c 20 26 6f 6e 65 29  r2, iType, &one)
31d8c 3b 0a 20 20 20 20 20 20 20 20 70 6f 73 4c 69 73  ;.        posLis
31d8d 74 50 68 72 61 73 65 4d 65 72 67 65 28 26 72 69  tPhraseMerge(&ri
31d8e 67 68 74 2c 20 26 6c 65 66 74 2c 20 6e 4e 65 61  ght, &left, nNea
31d8f 72 2d 33 2b 6e 50 68 72 61 73 65 2c 20 31 2c 20  r-3+nPhrase, 1, 
31d90 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 20 20 20  &dlwriter2);.   
31d91 20 20 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c       dlwInit(&dl
31d92 77 72 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20  writer2, iType, 
31d93 26 74 77 6f 29 3b 0a 20 20 20 20 20 20 20 20 70  &two);.        p
31d94 6f 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67  osListPhraseMerg
31d95 65 28 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c  e(&left, &right,
31d96 20 6e 4e 65 61 72 2d 31 2c 20 30 2c 20 26 64 6c   nNear-1, 0, &dl
31d97 77 72 69 74 65 72 32 29 3b 0a 0a 20 20 20 20 20  writer2);..     
31d98 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44 61 74 61     if( one.nData
31d99 29 20 64 6c 72 49 6e 69 74 28 26 64 72 31 2c 20  ) dlrInit(&dr1, 
31d9a 69 54 79 70 65 2c 20 6f 6e 65 2e 70 44 61 74 61  iType, one.pData
31d9b 2c 20 6f 6e 65 2e 6e 44 61 74 61 29 3b 0a 20 20  , one.nData);.  
31d9c 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e 6e 44        if( two.nD
31d9d 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26 64 72  ata) dlrInit(&dr
31d9e 32 2c 20 69 54 79 70 65 2c 20 74 77 6f 2e 70 44  2, iType, two.pD
31d9f 61 74 61 2c 20 74 77 6f 2e 6e 44 61 74 61 29 3b  ata, two.nData);
31da0 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 64  ..        if( !d
31da1 6c 72 41 74 45 6e 64 28 26 64 72 31 29 20 7c 7c  lrAtEnd(&dr1) ||
31da2 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72 32 29   !dlrAtEnd(&dr2)
31da3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 4c   ){.          PL
31da4 52 65 61 64 65 72 20 70 72 31 20 3d 20 7b 30 7d  Reader pr1 = {0}
31da5 3b 0a 20 20 20 20 20 20 20 20 20 20 50 4c 52 65  ;.          PLRe
31da6 61 64 65 72 20 70 72 32 20 3d 20 7b 30 7d 3b 0a  ader pr2 = {0};.
31da7 0a 20 20 20 20 20 20 20 20 20 20 50 4c 57 72 69  .          PLWri
31da8 74 65 72 20 70 6c 77 72 69 74 65 72 3b 0a 20 20  ter plwriter;.  
31da9 20 20 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28          plwInit(
31daa 26 70 6c 77 72 69 74 65 72 2c 20 26 77 72 69 74  &plwriter, &writ
31dab 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 64 6c 72  er, dlrDocid(dlr
31dac 41 74 45 6e 64 28 26 64 72 31 29 3f 26 64 72 32  AtEnd(&dr1)?&dr2
31dad 3a 26 64 72 31 29 29 3b 0a 0a 20 20 20 20 20 20  :&dr1));..      
31dae 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44 61 74      if( one.nDat
31daf 61 20 29 20 70 6c 72 49 6e 69 74 28 26 70 72 31  a ) plrInit(&pr1
31db0 2c 20 26 64 72 31 29 3b 0a 20 20 20 20 20 20 20  , &dr1);.       
31db1 20 20 20 69 66 28 20 74 77 6f 2e 6e 44 61 74 61     if( two.nData
31db2 20 29 20 70 6c 72 49 6e 69 74 28 26 70 72 32 2c   ) plrInit(&pr2,
31db3 20 26 64 72 32 29 3b 0a 20 20 20 20 20 20 20 20   &dr2);.        
31db4 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41 74 45    while( !plrAtE
31db5 6e 64 28 26 70 72 31 29 20 7c 7c 20 21 70 6c 72  nd(&pr1) || !plr
31db6 41 74 45 6e 64 28 26 70 72 32 29 20 29 7b 0a 20  AtEnd(&pr2) ){. 
31db7 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69             int i
31db8 43 6f 6d 70 61 72 65 20 3d 20 70 6c 72 43 6f 6d  Compare = plrCom
31db9 70 61 72 65 28 26 70 72 31 2c 20 26 70 72 32 29  pare(&pr1, &pr2)
31dba 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 77  ;.            sw
31dbb 69 74 63 68 28 20 69 43 6f 6d 70 61 72 65 20 29  itch( iCompare )
31dbc 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
31dbd 63 61 73 65 20 2d 31 3a 0a 20 20 20 20 20 20 20  case -1:.       
31dbe 20 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70 79           plwCopy
31dbf 28 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72 31  (&plwriter, &pr1
31dc0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
31dc1 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 31 29     plrStep(&pr1)
31dc2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
31dc3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
31dc4 20 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20         case 1:. 
31dc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
31dc6 6c 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65 72  lwCopy(&plwriter
31dc7 2c 20 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20  , &pr2);.       
31dc8 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70           plrStep
31dc9 28 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20  (&pr2);.        
31dca 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31dcb 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73               cas
31dcc 65 20 30 3a 0a 20 20 20 20 20 20 20 20 20 20 20  e 0:.           
31dcd 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 70 6c       plwCopy(&pl
31dce 77 72 69 74 65 72 2c 20 26 70 72 31 29 3b 0a 20  writer, &pr1);. 
31dcf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
31dd0 6c 72 53 74 65 70 28 26 70 72 31 29 3b 0a 20 20  lrStep(&pr1);.  
31dd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
31dd2 72 53 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20  rStep(&pr2);.   
31dd3 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
31dd4 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
31dd5 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
31dd6 20 20 20 20 20 20 20 20 70 6c 77 54 65 72 6d 69          plwTermi
31dd7 6e 61 74 65 28 26 70 6c 77 72 69 74 65 72 29 3b  nate(&plwriter);
31dd8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
31dd9 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
31dda 74 72 6f 79 28 26 6f 6e 65 29 3b 0a 20 20 20 20  troy(&one);.    
31ddb 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
31ddc 73 74 72 6f 79 28 26 74 77 6f 29 3b 0a 20 20 20  stroy(&two);.   
31ddd 20 20 20 7d 0a 20 20 20 20 20 20 64 6c 72 53 74     }.      dlrSt
31dde 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  ep(&left);.     
31ddf 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
31de0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64  ;.    }.  }..  d
31de1 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
31de2 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ;.  dlrDestroy(&
31de3 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73  right);.  dlwDes
31de4 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d  troy(&writer);.}
31de5 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f  ../* We have two
31de6 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69   DL_DOCIDS docli
31de7 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20  sts:  pLeft and 
31de8 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65  pRight..** Write
31de9 20 74 68 65 20 69 6e 74 65 72 73 65 63 74 69 6f   the intersectio
31dea 6e 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 64  n of these two d
31deb 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 4f 75  oclists into pOu
31dec 74 20 61 73 20 61 0a 2a 2a 20 44 4c 5f 44 4f 43  t as a.** DL_DOC
31ded 49 44 53 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a  IDS doclist..*/.
31dee 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
31def 69 73 74 41 6e 64 4d 65 72 67 65 28 0a 20 20 63  istAndMerge(.  c
31df0 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74  onst char *pLeft
31df1 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63  , int nLeft,.  c
31df2 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 69 67 68  onst char *pRigh
31df3 74 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20  t, int nRight,. 
31df4 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75   DataBuffer *pOu
31df5 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  t      /* Write 
31df6 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63  the combined doc
31df7 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  list here */.){.
31df8 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c    DLReader left,
31df9 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74   right;.  DLWrit
31dfa 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66  er writer;..  if
31dfb 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c 20 6e 52  ( nLeft==0 || nR
31dfc 69 67 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  ight==0 ) return
31dfd 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65  ;..  dlrInit(&le
31dfe 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  ft, DL_DOCIDS, p
31dff 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20  Left, nLeft);.  
31e00 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20  dlrInit(&right, 
31e01 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69 67 68  DL_DOCIDS, pRigh
31e02 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c  t, nRight);.  dl
31e03 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44  wInit(&writer, D
31e04 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b  L_DOCIDS, pOut);
31e05 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41  ..  while( !dlrA
31e06 74 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20 21  tEnd(&left) && !
31e07 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  dlrAtEnd(&right)
31e08 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 44   ){.    if( dlrD
31e09 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44  ocid(&left)<dlrD
31e0a 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a  ocid(&right) ){.
31e0b 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c        dlrStep(&l
31e0c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  eft);.    }else 
31e0d 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72 69  if( dlrDocid(&ri
31e0e 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c  ght)<dlrDocid(&l
31e0f 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c  eft) ){.      dl
31e10 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20  rStep(&right);. 
31e11 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
31e12 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  dlwAdd(&writer, 
31e13 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29  dlrDocid(&left))
31e14 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
31e15 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c  &left);.      dl
31e16 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20  rStep(&right);. 
31e17 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72 44     }.  }..  dlrD
31e18 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20  estroy(&left);. 
31e19 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67   dlrDestroy(&rig
31e1a 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f  ht);.  dlwDestro
31e1b 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f  y(&writer);.}../
31e1c 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20 44 4c  * We have two DL
31e1d 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 73  _DOCIDS doclists
31e1e 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  :  pLeft and pRi
31e1f 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68  ght..** Write th
31e20 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 73 65  e union of these
31e21 20 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e   two doclists in
31e22 74 6f 20 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20  to pOut as a.** 
31e23 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73  DL_DOCIDS doclis
31e24 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
31e25 64 20 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67 65  d docListOrMerge
31e26 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
31e27 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
31e28 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
31e29 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
31e2a 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72  ht,.  DataBuffer
31e2b 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
31e2c 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
31e2d 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
31e2e 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
31e2f 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
31e30 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
31e31 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
31e32 29 7b 0a 20 20 20 20 69 66 28 20 6e 52 69 67 68  ){.    if( nRigh
31e33 74 21 3d 30 20 29 20 64 61 74 61 42 75 66 66 65  t!=0 ) dataBuffe
31e34 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70 52  rAppend(pOut, pR
31e35 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
31e36 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
31e37 20 69 66 28 20 6e 52 69 67 68 74 3d 3d 30 20 29   if( nRight==0 )
31e38 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
31e39 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70 4c 65  Append(pOut, pLe
31e3a 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 20 20  ft, nLeft);.    
31e3b 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 64  return;.  }..  d
31e3c 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c  lrInit(&left, DL
31e3d 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66 74 2c 20  _DOCIDS, pLeft, 
31e3e 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69  nLeft);.  dlrIni
31e3f 74 28 26 72 69 67 68 74 2c 20 44 4c 5f 44 4f 43  t(&right, DL_DOC
31e40 49 44 53 2c 20 70 52 69 67 68 74 2c 20 6e 52 69  IDS, pRight, nRi
31e41 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28  ght);.  dlwInit(
31e42 26 77 72 69 74 65 72 2c 20 44 4c 5f 44 4f 43 49  &writer, DL_DOCI
31e43 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68  DS, pOut);..  wh
31e44 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26  ile( !dlrAtEnd(&
31e45 6c 65 66 74 29 20 7c 7c 20 21 64 6c 72 41 74 45  left) || !dlrAtE
31e46 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20  nd(&right) ){.  
31e47 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26    if( dlrAtEnd(&
31e48 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  right) ){.      
31e49 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  dlwAdd(&writer, 
31e4a 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29  dlrDocid(&left))
31e4b 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
31e4c 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
31e4d 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26  e if( dlrAtEnd(&
31e4e 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
31e4f 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 64  lwAdd(&writer, d
31e50 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 29  lrDocid(&right))
31e51 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
31e52 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c  &right);.    }el
31e53 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
31e54 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64 28  &left)<dlrDocid(
31e55 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
31e56 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
31e57 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
31e58 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
31e59 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
31e5a 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
31e5b 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64  &right)<dlrDocid
31e5c 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  (&left) ){.     
31e5d 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
31e5e 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74   dlrDocid(&right
31e5f 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  ));.      dlrSte
31e60 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
31e61 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 6c 77 41  else{.      dlwA
31e62 64 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44  dd(&writer, dlrD
31e63 6f 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20  ocid(&left));.  
31e64 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
31e65 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  t);.      dlrSte
31e66 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
31e67 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72  .  }..  dlrDestr
31e68 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72  oy(&left);.  dlr
31e69 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
31e6a 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26 77  .  dlwDestroy(&w
31e6b 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65  riter);.}../* We
31e6c 20 68 61 76 65 20 74 77 6f 20 44 4c 5f 44 4f 43   have two DL_DOC
31e6d 49 44 53 20 64 6f 63 6c 69 73 74 73 3a 20 20 70  IDS doclists:  p
31e6e 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e  Left and pRight.
31e6f 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 70  .** Write into p
31e70 4f 75 74 20 61 73 20 44 4c 5f 44 4f 43 49 44 53  Out as DL_DOCIDS
31e71 20 64 6f 63 6c 69 73 74 20 63 6f 6e 74 61 69 6e   doclist contain
31e72 69 6e 67 20 61 6c 6c 20 64 6f 63 75 6d 65 6e 74  ing all document
31e73 73 20 74 68 61 74 0a 2a 2a 20 6f 63 63 75 72 20  s that.** occur 
31e74 69 6e 20 70 4c 65 66 74 20 62 75 74 20 6e 6f 74  in pLeft but not
31e75 20 69 6e 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 73   in pRight..*/.s
31e76 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
31e77 73 74 45 78 63 65 70 74 4d 65 72 67 65 28 0a 20  stExceptMerge(. 
31e78 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c 65   const char *pLe
31e79 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20  ft, int nLeft,. 
31e7a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 69   const char *pRi
31e7b 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c  ght, int nRight,
31e7c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70  .  DataBuffer *p
31e7d 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74  Out      /* Writ
31e7e 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64  e the combined d
31e7f 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29  oclist here */.)
31e80 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66  {.  DLReader lef
31e81 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72  t, right;.  DLWr
31e82 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20  iter writer;..  
31e83 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 29 20 72  if( nLeft==0 ) r
31e84 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e 52 69  eturn;.  if( nRi
31e85 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61  ght==0 ){.    da
31e86 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70  taBufferAppend(p
31e87 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66  Out, pLeft, nLef
31e88 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
31e89 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26    }..  dlrInit(&
31e8a 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c  left, DL_DOCIDS,
31e8b 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a   pLeft, nLeft);.
31e8c 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74    dlrInit(&right
31e8d 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69  , DL_DOCIDS, pRi
31e8e 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20  ght, nRight);.  
31e8f 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c  dlwInit(&writer,
31e90 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74   DL_DOCIDS, pOut
31e91 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c  );..  while( !dl
31e92 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 29 7b  rAtEnd(&left) ){
31e93 0a 20 20 20 20 77 68 69 6c 65 28 20 21 64 6c 72  .    while( !dlr
31e94 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 26 26  AtEnd(&right) &&
31e95 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74   dlrDocid(&right
31e96 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  )<dlrDocid(&left
31e97 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74  ) ){.      dlrSt
31e98 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
31e99 7d 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74 45  }.    if( dlrAtE
31e9a 6e 64 28 26 72 69 67 68 74 29 20 7c 7c 20 64 6c  nd(&right) || dl
31e9b 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c  rDocid(&left)<dl
31e9c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29  rDocid(&right) )
31e9d 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64 28 26  {.      dlwAdd(&
31e9e 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64  writer, dlrDocid
31e9f 28 26 6c 65 66 74 29 29 3b 0a 20 20 20 20 7d 0a  (&left));.    }.
31ea0 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
31ea1 74 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65  t);.  }..  dlrDe
31ea2 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
31ea3 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68  dlrDestroy(&righ
31ea4 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79  t);.  dlwDestroy
31ea5 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 73 74  (&writer);.}..st
31ea6 61 74 69 63 20 63 68 61 72 20 2a 73 74 72 69 6e  atic char *strin
31ea7 67 5f 64 75 70 5f 6e 28 63 6f 6e 73 74 20 63 68  g_dup_n(const ch
31ea8 61 72 20 2a 73 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *s, int n){. 
31ea9 20 63 68 61 72 20 2a 73 74 72 20 3d 20 73 71 6c   char *str = sql
31eaa 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 20 2b 20  ite3_malloc(n + 
31eab 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28 73 74 72  1);.  memcpy(str
31eac 2c 20 73 2c 20 6e 29 3b 0a 20 20 73 74 72 5b 6e  , s, n);.  str[n
31ead 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 72 65 74 75  ] = '\0';.  retu
31eae 72 6e 20 73 74 72 3b 0a 7d 0a 0a 2f 2a 20 44 75  rn str;.}../* Du
31eaf 70 6c 69 63 61 74 65 20 61 20 73 74 72 69 6e 67  plicate a string
31eb0 3b 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  ; the caller mus
31eb1 74 20 66 72 65 65 28 29 20 74 68 65 20 72 65 74  t free() the ret
31eb2 75 72 6e 65 64 20 73 74 72 69 6e 67 2e 0a 20 2a  urned string.. *
31eb3 20 28 57 65 20 64 6f 6e 27 74 20 75 73 65 20 73   (We don't use s
31eb4 74 72 64 75 70 28 29 20 73 69 6e 63 65 20 69 74  trdup() since it
31eb5 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20   is not part of 
31eb6 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c  the standard C l
31eb7 69 62 72 61 72 79 20 61 6e 64 0a 20 2a 20 6d 61  ibrary and. * ma
31eb8 79 20 6e 6f 74 20 62 65 20 61 76 61 69 6c 61 62  y not be availab
31eb9 6c 65 20 65 76 65 72 79 77 68 65 72 65 2e 29 20  le everywhere.) 
31eba 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
31ebb 73 74 72 69 6e 67 5f 64 75 70 28 63 6f 6e 73 74  string_dup(const
31ebc 20 63 68 61 72 20 2a 73 29 7b 0a 20 20 72 65 74   char *s){.  ret
31ebd 75 72 6e 20 73 74 72 69 6e 67 5f 64 75 70 5f 6e  urn string_dup_n
31ebe 28 73 2c 20 73 74 72 6c 65 6e 28 73 29 29 3b 0a  (s, strlen(s));.
31ebf 7d 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20 61 20 73  }../* Format a s
31ec0 74 72 69 6e 67 2c 20 72 65 70 6c 61 63 69 6e 67  tring, replacing
31ec1 20 65 61 63 68 20 6f 63 63 75 72 72 65 6e 63 65   each occurrence
31ec2 20 6f 66 20 74 68 65 20 25 20 63 68 61 72 61 63   of the % charac
31ec3 74 65 72 20 77 69 74 68 0a 20 2a 20 7a 44 62 2e  ter with. * zDb.
31ec4 7a 4e 61 6d 65 2e 20 20 54 68 69 73 20 6d 61 79  zName.  This may
31ec5 20 62 65 20 6d 6f 72 65 20 63 6f 6e 76 65 6e 69   be more conveni
31ec6 65 6e 74 20 74 68 61 6e 20 73 71 6c 69 74 65 5f  ent than sqlite_
31ec7 6d 70 72 69 6e 74 66 28 29 0a 20 2a 20 77 68 65  mprintf(). * whe
31ec8 6e 20 6f 6e 65 20 73 74 72 69 6e 67 20 69 73 20  n one string is 
31ec9 75 73 65 64 20 72 65 70 65 61 74 65 64 6c 79 20  used repeatedly 
31eca 69 6e 20 61 20 66 6f 72 6d 61 74 20 73 74 72 69  in a format stri
31ecb 6e 67 2e 0a 20 2a 20 54 68 65 20 63 61 6c 6c 65  ng.. * The calle
31ecc 72 20 6d 75 73 74 20 66 72 65 65 28 29 20 74 68  r must free() th
31ecd 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
31ece 67 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  g. */.static cha
31ecf 72 20 2a 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74  r *string_format
31ed0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f  (const char *zFo
31ed1 72 6d 61 74 2c 0a 20 20 20 20 20 20 20 20 20 20  rmat,.          
31ed2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ed3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
31ed4 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
31ed5 61 6d 65 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  ame){.  const ch
31ed6 61 72 20 2a 70 3b 0a 20 20 73 69 7a 65 5f 74 20  ar *p;.  size_t 
31ed7 6c 65 6e 20 3d 20 30 3b 0a 20 20 73 69 7a 65 5f  len = 0;.  size_
31ed8 74 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e 28 7a  t nDb = strlen(z
31ed9 44 62 29 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 4e  Db);.  size_t nN
31eda 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61  ame = strlen(zNa
31edb 6d 65 29 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 46  me);.  size_t nF
31edc 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20 3d 20 6e  ullTableName = n
31edd 44 62 2b 31 2b 6e 4e 61 6d 65 3b 0a 20 20 63 68  Db+1+nName;.  ch
31ede 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20 20 63 68  ar *result;.  ch
31edf 61 72 20 2a 72 3b 0a 0a 20 20 2f 2a 20 66 69 72  ar *r;..  /* fir
31ee0 73 74 20 63 6f 6d 70 75 74 65 20 6c 65 6e 67 74  st compute lengt
31ee1 68 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 66 6f  h needed */.  fo
31ee2 72 28 70 20 3d 20 7a 46 6f 72 6d 61 74 20 3b 20  r(p = zFormat ; 
31ee3 2a 70 20 3b 20 2b 2b 70 29 7b 0a 20 20 20 20 6c  *p ; ++p){.    l
31ee4 65 6e 20 2b 3d 20 28 2a 70 3d 3d 27 25 27 20 3f  en += (*p=='%' ?
31ee5 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20   nFullTableName 
31ee6 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 6c 65 6e 20  : 1);.  }.  len 
31ee7 2b 3d 20 31 3b 20 20 2f 2a 20 66 6f 72 20 6e 75  += 1;  /* for nu
31ee8 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f  ll terminator */
31ee9 0a 0a 20 20 72 20 3d 20 72 65 73 75 6c 74 20 3d  ..  r = result =
31eea 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
31eeb 6c 65 6e 29 3b 0a 20 20 66 6f 72 28 70 20 3d 20  len);.  for(p = 
31eec 7a 46 6f 72 6d 61 74 3b 20 2a 70 3b 20 2b 2b 70  zFormat; *p; ++p
31eed 29 7b 0a 20 20 20 20 69 66 28 20 2a 70 3d 3d 27  ){.    if( *p=='
31eee 25 27 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  %' ){.      memc
31eef 70 79 28 72 2c 20 7a 44 62 2c 20 6e 44 62 29 3b  py(r, zDb, nDb);
31ef0 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e 44 62 3b  .      r += nDb;
31ef1 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d 20 27 2e  .      *r++ = '.
31ef2 27 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ';.      memcpy(
31ef3 72 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  r, zName, nName)
31ef4 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e 4e 61  ;.      r += nNa
31ef5 6d 65 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b  me;.    } else {
31ef6 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d 20 2a 70  .      *r++ = *p
31ef7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 72  ;.    }.  }.  *r
31ef8 2b 2b 20 3d 20 27 5c 30 27 3b 0a 20 20 61 73 73  ++ = '\0';.  ass
31ef9 65 72 74 28 20 72 20 3d 3d 20 72 65 73 75 6c 74  ert( r == result
31efa 20 2b 20 6c 65 6e 20 29 3b 0a 20 20 72 65 74 75   + len );.  retu
31efb 72 6e 20 72 65 73 75 6c 74 3b 0a 7d 0a 0a 73 74  rn result;.}..st
31efc 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 65 78 65  atic int sql_exe
31efd 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  c(sqlite3 *db, c
31efe 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20  onst char *zDb, 
31eff 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
31f00 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
31f01 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
31f02 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a 20 20 63  r *zFormat){.  c
31f03 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64 20 3d 20  har *zCommand = 
31f04 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74 28 7a 46  string_format(zF
31f05 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a 4e 61 6d  ormat, zDb, zNam
31f06 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e);.  int rc;.  
31f07 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
31f08 73 71 6c 3a 20 25 73 5c 6e 22 2c 20 7a 43 6f 6d  sql: %s\n", zCom
31f09 6d 61 6e 64 29 29 3b 0a 20 20 72 63 20 3d 20 73  mand));.  rc = s
31f0a 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
31f0b 7a 43 6f 6d 6d 61 6e 64 2c 20 4e 55 4c 4c 2c 20  zCommand, NULL, 
31f0c 30 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73 71 6c 69  0, NULL);.  sqli
31f0d 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 6d 61 6e  te3_free(zComman
31f0e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  d);.  return rc;
31f0f 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .}..static int s
31f10 71 6c 5f 70 72 65 70 61 72 65 28 73 71 6c 69 74  ql_prepare(sqlit
31f11 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
31f12 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20 63  ar *zDb, const c
31f13 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20  har *zName,.    
31f14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f15 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
31f16 2a 2a 70 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20  **ppStmt, const 
31f17 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a  char *zFormat){.
31f18 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64    char *zCommand
31f19 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74   = string_format
31f1a 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a  (zFormat, zDb, z
31f1b 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b  Name);.  int rc;
31f1c 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
31f1d 53 33 20 70 72 65 70 61 72 65 3a 20 25 73 5c 6e  S3 prepare: %s\n
31f1e 22 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20  ", zCommand));. 
31f1f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
31f20 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 43 6f  epare_v2(db, zCo
31f21 6d 6d 61 6e 64 2c 20 2d 31 2c 20 70 70 53 74 6d  mmand, -1, ppStm
31f22 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73 71 6c 69  t, NULL);.  sqli
31f23 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 6d 61 6e  te3_free(zComman
31f24 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  d);.  return rc;
31f25 0a 7d 0a 0a 2f 2a 20 65 6e 64 20 75 74 69 6c 69  .}../* end utili
31f26 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  ty functions */.
31f27 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
31f28 72 65 6e 63 65 20 2a 2f 0a 74 79 70 65 64 65 66  rence */.typedef
31f29 20 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78 74   struct fulltext
31f2a 5f 76 74 61 62 20 66 75 6c 6c 74 65 78 74 5f 76  _vtab fulltext_v
31f2b 74 61 62 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  tab;../*.** An i
31f2c 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
31f2d 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
31f2e 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f  re keeps track o
31f2f 66 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 6d  f generated.** m
31f30 61 74 63 68 69 6e 67 2d 77 6f 72 64 20 6f 66 66  atching-word off
31f31 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  set information 
31f32 61 6e 64 20 73 6e 69 70 70 65 74 73 2e 0a 2a 2f  and snippets..*/
31f33 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
31f34 53 6e 69 70 70 65 74 20 7b 0a 20 20 69 6e 74 20  Snippet {.  int 
31f35 6e 4d 61 74 63 68 3b 20 20 20 20 20 2f 2a 20 54  nMatch;     /* T
31f36 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6d  otal number of m
31f37 61 74 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20  atches */.  int 
31f38 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 53  nAlloc;     /* S
31f39 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66  pace allocated f
31f3a 6f 72 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20  or aMatch[] */. 
31f3b 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d   struct snippetM
31f3c 61 74 63 68 20 7b 20 2f 2a 20 4f 6e 65 20 65 6e  atch { /* One en
31f3d 74 72 79 20 66 6f 72 20 65 61 63 68 20 6d 61 74  try for each mat
31f3e 63 68 69 6e 67 20 74 65 72 6d 20 2a 2f 0a 20 20  ching term */.  
31f3f 20 20 63 68 61 72 20 73 6e 53 74 61 74 75 73 3b    char snStatus;
31f40 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73         /* Status
31f41 20 66 6c 61 67 20 66 6f 72 20 75 73 65 20 77 68   flag for use wh
31f42 69 6c 65 20 63 6f 6e 73 74 72 75 63 74 69 6e 67  ile constructing
31f43 20 73 6e 69 70 70 65 74 73 20 2a 2f 0a 20 20 20   snippets */.   
31f44 20 73 68 6f 72 74 20 69 6e 74 20 69 43 6f 6c 3b   short int iCol;
31f45 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c        /* The col
31f46 75 6d 6e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  umn that contain
31f47 73 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 20  s the match */. 
31f48 20 20 20 73 68 6f 72 74 20 69 6e 74 20 69 54 65     short int iTe
31f49 72 6d 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69  rm;     /* The i
31f4a 6e 64 65 78 20 69 6e 20 51 75 65 72 79 2e 70 54  ndex in Query.pT
31f4b 65 72 6d 73 5b 5d 20 6f 66 20 74 68 65 20 6d 61  erms[] of the ma
31f4c 74 63 68 69 6e 67 20 74 65 72 6d 20 2a 2f 0a 20  tching term */. 
31f4d 20 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20     int iToken;  
31f4e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
31f4f 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63  ndex of the matc
31f50 68 69 6e 67 20 64 6f 63 75 6d 65 6e 74 20 74 6f  hing document to
31f51 6b 65 6e 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74  ken */.    short
31f52 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20   int nByte;     
31f53 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
31f54 65 73 20 69 6e 20 74 68 65 20 74 65 72 6d 20 2a  es in the term *
31f55 2f 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74  /.    int iStart
31f56 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
31f57 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20  e offset to the 
31f58 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20  first character 
31f59 6f 66 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20  of the term */. 
31f5a 20 7d 20 2a 61 4d 61 74 63 68 3b 20 20 20 20 20   } *aMatch;     
31f5b 20 2f 2a 20 50 6f 69 6e 74 73 20 74 6f 20 73 70   /* Points to sp
31f5c 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ace obtained fro
31f5d 6d 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68  m malloc */.  ch
31f5e 61 72 20 2a 7a 4f 66 66 73 65 74 3b 20 20 2f 2a  ar *zOffset;  /*
31f5f 20 54 65 78 74 20 72 65 6e 64 65 72 69 6e 67 20   Text rendering 
31f60 6f 66 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20  of aMatch[] */. 
31f61 20 69 6e 74 20 6e 4f 66 66 73 65 74 3b 20 20 20   int nOffset;   
31f62 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 4f 66 66 73   /* strlen(zOffs
31f63 65 74 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  et) */.  char *z
31f64 53 6e 69 70 70 65 74 3b 20 2f 2a 20 53 6e 69 70  Snippet; /* Snip
31f65 70 65 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  pet text */.  in
31f66 74 20 6e 53 6e 69 70 70 65 74 3b 20 20 20 2f 2a  t nSnippet;   /*
31f67 20 73 74 72 6c 65 6e 28 7a 53 6e 69 70 70 65 74   strlen(zSnippet
31f68 29 20 2a 2f 0a 7d 20 53 6e 69 70 70 65 74 3b 0a  ) */.} Snippet;.
31f69 0a 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20 51  ..typedef enum Q
31f6a 75 65 72 79 54 79 70 65 20 7b 0a 20 20 51 55 45  ueryType {.  QUE
31f6b 52 59 5f 47 45 4e 45 52 49 43 2c 20 20 20 2f 2a  RY_GENERIC,   /*
31f6c 20 74 61 62 6c 65 20 73 63 61 6e 20 2a 2f 0a 20   table scan */. 
31f6d 20 51 55 45 52 59 5f 44 4f 43 49 44 2c 20 20 20   QUERY_DOCID,   
31f6e 20 20 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79 20 64    /* lookup by d
31f6f 6f 63 69 64 20 2a 2f 0a 20 20 51 55 45 52 59 5f  ocid */.  QUERY_
31f70 46 55 4c 4c 54 45 58 54 20 20 20 2f 2a 20 51 55  FULLTEXT   /* QU
31f71 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 2b 20 5b  ERY_FULLTEXT + [
31f72 69 5d 20 69 73 20 61 20 66 75 6c 6c 2d 74 65 78  i] is a full-tex
31f73 74 20 73 65 61 72 63 68 20 66 6f 72 20 63 6f 6c  t search for col
31f74 75 6d 6e 20 69 2a 2f 0a 7d 20 51 75 65 72 79 54  umn i*/.} QueryT
31f75 79 70 65 3b 0a 0a 74 79 70 65 64 65 66 20 65 6e  ype;..typedef en
31f76 75 6d 20 66 75 6c 6c 74 65 78 74 5f 73 74 61 74  um fulltext_stat
31f77 65 6d 65 6e 74 20 7b 0a 20 20 43 4f 4e 54 45 4e  ement {.  CONTEN
31f78 54 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c 0a 20  T_INSERT_STMT,. 
31f79 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f   CONTENT_SELECT_
31f7a 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f  STMT,.  CONTENT_
31f7b 55 50 44 41 54 45 5f 53 54 4d 54 2c 0a 20 20 43  UPDATE_STMT,.  C
31f7c 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 5f 53 54  ONTENT_DELETE_ST
31f7d 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 45 58  MT,.  CONTENT_EX
31f7e 49 53 54 53 5f 53 54 4d 54 2c 0a 0a 20 20 42 4c  ISTS_STMT,..  BL
31f7f 4f 43 4b 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c  OCK_INSERT_STMT,
31f80 0a 20 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 5f  .  BLOCK_SELECT_
31f81 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 44 45  STMT,.  BLOCK_DE
31f82 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f  LETE_STMT,.  BLO
31f83 43 4b 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54  CK_DELETE_ALL_ST
31f84 4d 54 2c 0a 0a 20 20 53 45 47 44 49 52 5f 4d 41  MT,..  SEGDIR_MA
31f85 58 5f 49 4e 44 45 58 5f 53 54 4d 54 2c 0a 20 20  X_INDEX_STMT,.  
31f86 53 45 47 44 49 52 5f 53 45 54 5f 53 54 4d 54 2c  SEGDIR_SET_STMT,
31f87 0a 20 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54  .  SEGDIR_SELECT
31f88 5f 4c 45 56 45 4c 5f 53 54 4d 54 2c 0a 20 20 53  _LEVEL_STMT,.  S
31f89 45 47 44 49 52 5f 53 50 41 4e 5f 53 54 4d 54 2c  EGDIR_SPAN_STMT,
31f8a 0a 20 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45  .  SEGDIR_DELETE
31f8b 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f  _STMT,.  SEGDIR_
31f8c 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54 5f 53  SELECT_SEGMENT_S
31f8d 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 45  TMT,.  SEGDIR_SE
31f8e 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 20  LECT_ALL_STMT,. 
31f8f 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 41   SEGDIR_DELETE_A
31f90 4c 4c 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49  LL_STMT,.  SEGDI
31f91 52 5f 43 4f 55 4e 54 5f 53 54 4d 54 2c 0a 0a 20  R_COUNT_STMT,.. 
31f92 20 4d 41 58 5f 53 54 4d 54 20 20 20 20 20 20 20   MAX_STMT       
31f93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
31f94 20 41 6c 77 61 79 73 20 61 74 20 65 6e 64 21 20   Always at end! 
31f95 2a 2f 0a 7d 20 66 75 6c 6c 74 65 78 74 5f 73 74  */.} fulltext_st
31f96 61 74 65 6d 65 6e 74 3b 0a 0a 2f 2a 20 54 68 65  atement;../* The
31f97 73 65 20 6d 75 73 74 20 65 78 61 63 74 6c 79 20  se must exactly 
31f98 6d 61 74 63 68 20 74 68 65 20 65 6e 75 6d 20 61  match the enum a
31f99 62 6f 76 65 2e 20 2a 2f 0a 2f 2a 20 54 4f 44 4f  bove. */./* TODO
31f9a 28 73 68 65 73 73 29 3a 20 49 73 20 74 68 65 72  (shess): Is ther
31f9b 65 20 73 6f 6d 65 20 72 69 73 6b 20 74 68 61 74  e some risk that
31f9c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c   a statement wil
31f9d 6c 20 62 65 20 75 73 65 64 20 69 6e 20 74 77 6f  l be used in two
31f9e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 61 74 20 6f  .** cursors at o
31f9f 6e 63 65 2c 20 65 2e 67 2e 20 20 69 66 20 61 20  nce, e.g.  if a 
31fa0 71 75 65 72 79 20 6a 6f 69 6e 73 20 61 20 76 69  query joins a vi
31fa1 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20 69  rtual table to i
31fa2 74 73 65 6c 66 3f 0a 2a 2a 20 49 66 20 73 6f 20  tself?.** If so 
31fa3 70 65 72 68 61 70 73 20 77 65 20 73 68 6f 75 6c  perhaps we shoul
31fa4 64 20 6d 6f 76 65 20 73 6f 6d 65 20 6f 66 20 74  d move some of t
31fa5 68 65 73 65 20 74 6f 20 74 68 65 20 63 75 72 73  hese to the curs
31fa6 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74  or object..*/.st
31fa7 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
31fa8 2a 63 6f 6e 73 74 20 66 75 6c 6c 74 65 78 74 5f  *const fulltext_
31fa9 7a 53 74 61 74 65 6d 65 6e 74 5b 4d 41 58 5f 53  zStatement[MAX_S
31faa 54 4d 54 5d 20 3d 20 7b 0a 20 20 2f 2a 20 43 4f  TMT] = {.  /* CO
31fab 4e 54 45 4e 54 5f 49 4e 53 45 52 54 20 2a 2f 20  NTENT_INSERT */ 
31fac 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61  NULL,  /* genera
31fad 74 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74 49 6e  ted in contentIn
31fae 73 65 72 74 53 74 61 74 65 6d 65 6e 74 28 29 20  sertStatement() 
31faf 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f  */.  /* CONTENT_
31fb0 53 45 4c 45 43 54 20 2a 2f 20 4e 55 4c 4c 2c 20  SELECT */ NULL, 
31fb1 20 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e   /* generated in
31fb2 20 63 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74   contentSelectSt
31fb3 61 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f  atement() */.  /
31fb4 2a 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45  * CONTENT_UPDATE
31fb5 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65   */ NULL,  /* ge
31fb6 6e 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74 65  nerated in conte
31fb7 6e 74 55 70 64 61 74 65 53 74 61 74 65 6d 65 6e  ntUpdateStatemen
31fb8 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54  t() */.  /* CONT
31fb9 45 4e 54 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64  ENT_DELETE */ "d
31fba 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 63 6f 6e  elete from %_con
31fbb 74 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64  tent where docid
31fbc 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 43 4f 4e 54   = ?",.  /* CONT
31fbd 45 4e 54 5f 45 58 49 53 54 53 20 2a 2f 20 22 73  ENT_EXISTS */ "s
31fbe 65 6c 65 63 74 20 64 6f 63 69 64 20 66 72 6f 6d  elect docid from
31fbf 20 25 5f 63 6f 6e 74 65 6e 74 20 6c 69 6d 69 74   %_content limit
31fc0 20 31 22 2c 0a 0a 20 20 2f 2a 20 42 4c 4f 43 4b   1",..  /* BLOCK
31fc1 5f 49 4e 53 45 52 54 20 2a 2f 0a 20 20 22 69 6e  _INSERT */.  "in
31fc2 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67 6d  sert into %_segm
31fc3 65 6e 74 73 20 28 62 6c 6f 63 6b 69 64 2c 20 62  ents (blockid, b
31fc4 6c 6f 63 6b 29 20 76 61 6c 75 65 73 20 28 6e 75  lock) values (nu
31fc5 6c 6c 2c 20 3f 29 22 2c 0a 20 20 2f 2a 20 42 4c  ll, ?)",.  /* BL
31fc6 4f 43 4b 5f 53 45 4c 45 43 54 20 2a 2f 20 22 73  OCK_SELECT */ "s
31fc7 65 6c 65 63 74 20 62 6c 6f 63 6b 20 66 72 6f 6d  elect block from
31fc8 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65 72   %_segments wher
31fc9 65 20 62 6c 6f 63 6b 69 64 20 3d 20 3f 22 2c 0a  e blockid = ?",.
31fca 20 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45 4c 45 54    /* BLOCK_DELET
31fcb 45 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f  E */ "delete fro
31fcc 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65  m %_segments whe
31fcd 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65  re blockid betwe
31fce 65 6e 20 3f 20 61 6e 64 20 3f 22 2c 0a 20 20 2f  en ? and ?",.  /
31fcf 2a 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41  * BLOCK_DELETE_A
31fd0 4c 4c 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72  LL */ "delete fr
31fd1 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 22 2c 0a  om %_segments",.
31fd2 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 4d 41 58  .  /* SEGDIR_MAX
31fd3 5f 49 4e 44 45 58 20 2a 2f 20 22 73 65 6c 65 63  _INDEX */ "selec
31fd4 74 20 6d 61 78 28 69 64 78 29 20 66 72 6f 6d 20  t max(idx) from 
31fd5 25 5f 73 65 67 64 69 72 20 77 68 65 72 65 20 6c  %_segdir where l
31fd6 65 76 65 6c 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20  evel = ?",.  /* 
31fd7 53 45 47 44 49 52 5f 53 45 54 20 2a 2f 20 22 69  SEGDIR_SET */ "i
31fd8 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67  nsert into %_seg
31fd9 64 69 72 20 76 61 6c 75 65 73 20 28 3f 2c 20 3f  dir values (?, ?
31fda 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 29 22 2c 0a  , ?, ?, ?, ?)",.
31fdb 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 4c 45    /* SEGDIR_SELE
31fdc 43 54 5f 4c 45 56 45 4c 20 2a 2f 0a 20 20 22 73  CT_LEVEL */.  "s
31fdd 65 6c 65 63 74 20 73 74 61 72 74 5f 62 6c 6f 63  elect start_bloc
31fde 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c  k, leaves_end_bl
31fdf 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25  ock, root from %
31fe0 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20 77 68  _segdir ".  " wh
31fe1 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20 6f 72  ere level = ? or
31fe2 64 65 72 20 62 79 20 69 64 78 22 2c 0a 20 20 2f  der by idx",.  /
31fe3 2a 20 53 45 47 44 49 52 5f 53 50 41 4e 20 2a 2f  * SEGDIR_SPAN */
31fe4 0a 20 20 22 73 65 6c 65 63 74 20 6d 69 6e 28 73  .  "select min(s
31fe5 74 61 72 74 5f 62 6c 6f 63 6b 29 2c 20 6d 61 78  tart_block), max
31fe6 28 65 6e 64 5f 62 6c 6f 63 6b 29 20 66 72 6f 6d  (end_block) from
31fe7 20 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20   %_segdir ".  " 
31fe8 77 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20  where level = ? 
31fe9 61 6e 64 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20  and start_block 
31fea 3c 3e 20 30 22 2c 0a 20 20 2f 2a 20 53 45 47 44  <> 0",.  /* SEGD
31feb 49 52 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64 65  IR_DELETE */ "de
31fec 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 64  lete from %_segd
31fed 69 72 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d  ir where level =
31fee 20 3f 22 2c 0a 0a 20 20 2f 2a 20 4e 4f 54 45 28   ?",..  /* NOTE(
31fef 73 68 65 73 73 29 3a 20 54 68 65 20 66 69 72 73  shess): The firs
31ff0 74 20 74 68 72 65 65 20 72 65 73 75 6c 74 73 20  t three results 
31ff1 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
31ff2 20 74 77 6f 0a 20 20 2a 2a 20 73 74 61 74 65 6d   two.  ** statem
31ff3 65 6e 74 73 20 6d 75 73 74 20 6d 61 74 63 68 2e  ents must match.
31ff4 0a 20 20 2a 2f 0a 20 20 2f 2a 20 53 45 47 44 49  .  */.  /* SEGDI
31ff5 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54  R_SELECT_SEGMENT
31ff6 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74   */.  "select st
31ff7 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65  art_block, leave
31ff8 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f  s_end_block, roo
31ff9 74 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20  t from %_segdir 
31ffa 22 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76 65  ".  " where leve
31ffb 6c 20 3d 20 3f 20 61 6e 64 20 69 64 78 20 3d 20  l = ? and idx = 
31ffc 3f 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  ?",.  /* SEGDIR_
31ffd 53 45 4c 45 43 54 5f 41 4c 4c 20 2a 2f 0a 20 20  SELECT_ALL */.  
31ffe 22 73 65 6c 65 63 74 20 73 74 61 72 74 5f 62 6c  "select start_bl
31fff 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f  ock, leaves_end_
32000 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d  block, root from
32001 20 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20   %_segdir ".  " 
32002 6f 72 64 65 72 20 62 79 20 6c 65 76 65 6c 20 64  order by level d
32003 65 73 63 2c 20 69 64 78 20 61 73 63 22 2c 0a 20  esc, idx asc",. 
32004 20 2f 2a 20 53 45 47 44 49 52 5f 44 45 4c 45 54   /* SEGDIR_DELET
32005 45 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c 65 74 65  E_ALL */ "delete
32006 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 22 2c   from %_segdir",
32007 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 43 4f 55  .  /* SEGDIR_COU
32008 4e 54 20 2a 2f 20 22 73 65 6c 65 63 74 20 63 6f  NT */ "select co
32009 75 6e 74 28 2a 29 2c 20 69 66 6e 75 6c 6c 28 6d  unt(*), ifnull(m
3200a 61 78 28 6c 65 76 65 6c 29 2c 30 29 20 66 72 6f  ax(level),0) fro
3200b 6d 20 25 5f 73 65 67 64 69 72 22 2c 0a 7d 3b 0a  m %_segdir",.};.
3200c 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 6e 65 63 74  ./*.** A connect
3200d 69 6f 6e 20 74 6f 20 61 20 66 75 6c 6c 74 65 78  ion to a fulltex
3200e 74 20 69 6e 64 65 78 20 69 73 20 61 6e 20 69 6e  t index is an in
3200f 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
32010 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
32011 74 75 72 65 2e 20 20 54 68 65 20 78 43 72 65 61  ture.  The xCrea
32012 74 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20  te and xConnect 
32013 6d 65 74 68 6f 64 73 20 63 72 65 61 74 65 20 61  methods create a
32014 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
32015 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
32016 61 6e 64 20 78 44 65 73 74 72 6f 79 20 61 6e 64  and xDestroy and
32017 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 66 72 65   xDisconnect fre
32018 65 20 74 68 61 74 20 69 6e 73 74 61 6e 63 65 2e  e that instance.
32019 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 6d 65  .** All other me
3201a 74 68 6f 64 73 20 72 65 63 65 69 76 65 20 61 20  thods receive a 
3201b 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73  pointer to the s
3201c 74 72 75 63 74 75 72 65 20 61 73 20 6f 6e 65 20  tructure as one 
3201d 6f 66 20 74 68 65 69 72 0a 2a 2a 20 61 72 67 75  of their.** argu
3201e 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  ments..*/.struct
3201f 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 7b   fulltext_vtab {
32020 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
32021 62 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  base;           
32022 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73      /* Base clas
32023 73 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  s used by SQLite
32024 20 63 6f 72 65 20 2a 2f 0a 20 20 73 71 6c 69 74   core */.  sqlit
32025 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
32026 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
32027 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
32028 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ection */.  cons
32029 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
3202a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3202b 6c 6f 67 69 63 61 6c 20 64 61 74 61 62 61 73 65  logical database
3202c 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   name */.  const
3202d 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
3202e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76              /* v
3202f 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d  irtual table nam
32030 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  e */.  int nColu
32031 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
32032 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65          /* numbe
32033 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
32034 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
32035 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 75  .  char **azColu
32036 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
32037 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 61      /* column na
32038 6d 65 73 2e 20 20 6d 61 6c 6c 6f 63 65 64 20 2a  mes.  malloced *
32039 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6e  /.  char **azCon
3203a 74 65 6e 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  tentColumn;     
3203b 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e       /* column n
3203c 61 6d 65 73 20 69 6e 20 63 6f 6e 74 65 6e 74 20  ames in content 
3203d 74 61 62 6c 65 3b 20 6d 61 6c 6c 6f 63 65 64 20  table; malloced 
3203e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
3203f 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
32040 65 72 3b 20 20 20 2f 2a 20 74 6f 6b 65 6e 69 7a  er;   /* tokeniz
32041 65 72 20 66 6f 72 20 69 6e 73 65 72 74 73 20 61  er for inserts a
32042 6e 64 20 71 75 65 72 69 65 73 20 2a 2f 0a 0a 20  nd queries */.. 
32043 20 2f 2a 20 50 72 65 63 6f 6d 70 69 6c 65 64 20   /* Precompiled 
32044 73 74 61 74 65 6d 65 6e 74 73 20 77 68 69 63 68  statements which
32045 20 77 65 20 6b 65 65 70 20 61 73 20 6c 6f 6e 67   we keep as long
32046 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 69 73   as the table is
32047 0a 20 20 2a 2a 20 6f 70 65 6e 2e 0a 20 20 2a 2f  .  ** open..  */
32048 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
32049 2a 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  *pFulltextStatem
3204a 65 6e 74 73 5b 4d 41 58 5f 53 54 4d 54 5d 3b 0a  ents[MAX_STMT];.
3204b 0a 20 20 2f 2a 20 50 72 65 63 6f 6d 70 69 6c 65  .  /* Precompile
3204c 64 20 73 74 61 74 65 6d 65 6e 74 73 20 75 73 65  d statements use
3204d 64 20 66 6f 72 20 73 65 67 6d 65 6e 74 20 6d 65  d for segment me
3204e 72 67 65 73 2e 20 20 57 65 20 72 75 6e 20 61 0a  rges.  We run a.
3204f 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 73 65    ** separate se
32050 6c 65 63 74 20 61 63 72 6f 73 73 20 74 68 65 20  lect across the 
32051 6c 65 61 66 20 6c 65 76 65 6c 20 6f 66 20 65 61  leaf level of ea
32052 63 68 20 74 72 65 65 20 62 65 69 6e 67 20 6d 65  ch tree being me
32053 72 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  rged..  */.  sql
32054 69 74 65 33 5f 73 74 6d 74 20 2a 70 4c 65 61 66  ite3_stmt *pLeaf
32055 53 65 6c 65 63 74 53 74 6d 74 73 5b 4d 45 52 47  SelectStmts[MERG
32056 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 2f 2a 20 54  E_COUNT];.  /* T
32057 68 65 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65  he statement use
32058 64 20 74 6f 20 70 72 65 70 61 72 65 20 70 4c 65  d to prepare pLe
32059 61 66 53 65 6c 65 63 74 53 74 6d 74 73 2e 20 2a  afSelectStmts. *
3205a 2f 0a 23 64 65 66 69 6e 65 20 4c 45 41 46 5f 53  /.#define LEAF_S
3205b 45 4c 45 43 54 20 5c 0a 20 20 22 73 65 6c 65 63  ELECT \.  "selec
3205c 74 20 62 6c 6f 63 6b 20 66 72 6f 6d 20 25 5f 73  t block from %_s
3205d 65 67 6d 65 6e 74 73 20 77 68 65 72 65 20 62 6c  egments where bl
3205e 6f 63 6b 69 64 20 62 65 74 77 65 65 6e 20 3f 20  ockid between ? 
3205f 61 6e 64 20 3f 20 6f 72 64 65 72 20 62 79 20 62  and ? order by b
32060 6c 6f 63 6b 69 64 22 0a 0a 20 20 2f 2a 20 54 68  lockid"..  /* Th
32061 65 73 65 20 62 75 66 66 65 72 20 70 65 6e 64 69  ese buffer pendi
32062 6e 67 20 69 6e 64 65 78 20 75 70 64 61 74 65 73  ng index updates
32063 20 64 75 72 69 6e 67 20 74 72 61 6e 73 61 63 74   during transact
32064 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 50 65 6e 64  ions..  ** nPend
32065 69 6e 67 44 61 74 61 20 65 73 74 69 6d 61 74 65  ingData estimate
32066 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 69 7a  s the memory siz
32067 65 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67  e of the pending
32068 20 64 61 74 61 2e 20 20 49 74 0a 20 20 2a 2a 20   data.  It.  ** 
32069 64 6f 65 73 6e 27 74 20 69 6e 63 6c 75 64 65 20  doesn't include 
3206a 74 68 65 20 68 61 73 68 2d 62 75 63 6b 65 74 20  the hash-bucket 
3206b 6f 76 65 72 68 65 61 64 2c 20 6e 6f 72 20 61 6e  overhead, nor an
3206c 79 20 6d 61 6c 6c 6f 63 0a 20 20 2a 2a 20 6f 76  y malloc.  ** ov
3206d 65 72 68 65 61 64 2e 20 20 57 68 65 6e 20 6e 50  erhead.  When nP
3206e 65 6e 64 69 6e 67 44 61 74 61 20 65 78 63 65 65  endingData excee
3206f 64 73 20 6b 50 65 6e 64 69 6e 67 54 68 72 65 73  ds kPendingThres
32070 68 6f 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20 62  hold, the.  ** b
32071 75 66 66 65 72 20 69 73 20 66 6c 75 73 68 65 64  uffer is flushed
32072 20 65 76 65 6e 20 62 65 66 6f 72 65 20 74 68 65   even before the
32073 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6c 6f   transaction clo
32074 73 65 73 2e 0a 20 20 2a 2a 20 70 65 6e 64 69 6e  ses..  ** pendin
32075 67 54 65 72 6d 73 20 73 74 6f 72 65 73 20 74 68  gTerms stores th
32076 65 20 64 61 74 61 2c 20 61 6e 64 20 69 73 20 6f  e data, and is o
32077 6e 6c 79 20 76 61 6c 69 64 20 77 68 65 6e 20 6e  nly valid when n
32078 50 65 6e 64 69 6e 67 44 61 74 61 0a 20 20 2a 2a  PendingData.  **
32079 20 69 73 20 3e 3d 30 20 28 6e 50 65 6e 64 69 6e   is >=0 (nPendin
3207a 67 44 61 74 61 3c 30 20 6d 65 61 6e 73 20 70 65  gData<0 means pe
3207b 6e 64 69 6e 67 54 65 72 6d 73 20 68 61 73 20 6e  ndingTerms has n
3207c 6f 74 20 62 65 65 6e 0a 20 20 2a 2a 20 69 6e 69  ot been.  ** ini
3207d 74 69 61 6c 69 7a 65 64 29 2e 20 20 69 50 72 65  tialized).  iPre
3207e 76 44 6f 63 69 64 20 69 73 20 74 68 65 20 6c 61  vDocid is the la
3207f 73 74 20 64 6f 63 69 64 20 77 72 69 74 74 65 6e  st docid written
32080 2c 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 0a 20  , used to make. 
32081 20 2a 2a 20 63 65 72 74 61 69 6e 20 77 65 27 72   ** certain we'r
32082 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 20 73  e inserting in s
32083 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 20 20 2a  orted order..  *
32084 2f 0a 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e 67  /.  int nPending
32085 44 61 74 61 3b 0a 23 64 65 66 69 6e 65 20 6b 50  Data;.#define kP
32086 65 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c 64 20  endingThreshold 
32087 28 31 2a 31 30 32 34 2a 31 30 32 34 29 0a 20 20  (1*1024*1024).  
32088 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72  sqlite_int64 iPr
32089 65 76 44 6f 63 69 64 3b 0a 20 20 66 74 73 33 48  evDocid;.  fts3H
3208a 61 73 68 20 70 65 6e 64 69 6e 67 54 65 72 6d 73  ash pendingTerms
3208b 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ;.};../*.** When
3208c 20 74 68 65 20 63 6f 72 65 20 77 61 6e 74 73 20   the core wants 
3208d 74 6f 20 64 6f 20 61 20 71 75 65 72 79 2c 20 69  to do a query, i
3208e 74 20 63 72 65 61 74 65 20 61 20 63 75 72 73 6f  t create a curso
3208f 72 20 75 73 69 6e 67 20 61 0a 2a 2a 20 63 61 6c  r using a.** cal
32090 6c 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 69  l to xOpen.  Thi
32091 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  s structure is a
32092 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 61 20  n instance of a 
32093 63 75 72 73 6f 72 2e 20 20 49 74 0a 2a 2a 20 69  cursor.  It.** i
32094 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 78  s destroyed by x
32095 43 6c 6f 73 65 2e 0a 2a 2f 0a 74 79 70 65 64 65  Close..*/.typede
32096 66 20 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78  f struct fulltex
32097 74 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  t_cursor {.  sql
32098 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
32099 20 62 61 73 65 3b 20 20 20 20 20 20 20 20 2f 2a   base;        /*
3209a 20 42 61 73 65 20 63 6c 61 73 73 20 75 73 65 64   Base class used
3209b 20 62 79 20 53 51 4c 69 74 65 20 63 6f 72 65 20   by SQLite core 
3209c 2a 2f 0a 20 20 51 75 65 72 79 54 79 70 65 20 69  */.  QueryType i
3209d 43 75 72 73 6f 72 54 79 70 65 3b 20 20 20 20 20  CursorType;     
3209e 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
3209f 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
320a0 6e 66 6f 2e 69 64 78 4e 75 6d 20 2a 2f 0a 20 20  nfo.idxNum */.  
320a1 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
320a2 74 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  tmt;            
320a3 20 2f 2a 20 50 72 65 70 61 72 65 64 20 73 74 61   /* Prepared sta
320a4 74 65 6d 65 6e 74 20 69 6e 20 75 73 65 20 62 79  tement in use by
320a5 20 74 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20   the cursor */. 
320a6 20 69 6e 74 20 65 6f 66 3b 20 20 20 20 20 20 20   int eof;       
320a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
320a8 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74 20    /* True if at 
320a9 45 6e 64 20 4f 66 20 52 65 73 75 6c 74 73 20 2a  End Of Results *
320aa 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 45  /.  Fts3Expr *pE
320ab 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
320ac 20 20 20 20 20 2f 2a 20 50 61 72 73 65 64 20 4d       /* Parsed M
320ad 41 54 43 48 20 71 75 65 72 79 20 73 74 72 69 6e  ATCH query strin
320ae 67 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 73  g */.  Snippet s
320af 6e 69 70 70 65 74 3b 20 20 20 20 20 20 20 20 20  nippet;         
320b0 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65          /* Cache
320b1 64 20 73 6e 69 70 70 65 74 20 66 6f 72 20 74 68  d snippet for th
320b2 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 2a 2f  e current row */
320b3 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20  .  int iColumn; 
320b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
320b5 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 62 65      /* Column be
320b6 69 6e 67 20 73 65 61 72 63 68 65 64 20 2a 2f 0a  ing searched */.
320b7 20 20 44 61 74 61 42 75 66 66 65 72 20 72 65 73    DataBuffer res
320b8 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ult;            
320b9 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20 72 65     /* Doclist re
320ba 73 75 6c 74 73 20 66 72 6f 6d 20 66 75 6c 6c 74  sults from fullt
320bb 65 78 74 51 75 65 72 79 20 2a 2f 0a 20 20 44 4c  extQuery */.  DL
320bc 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 20 20  Reader reader;  
320bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
320be 2a 20 52 65 73 75 6c 74 20 72 65 61 64 65 72 20  * Result reader 
320bf 69 66 20 72 65 73 75 6c 74 20 6e 6f 74 20 65 6d  if result not em
320c0 70 74 79 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65 78  pty */.} fulltex
320c1 74 5f 63 75 72 73 6f 72 3b 0a 0a 73 74 61 74 69  t_cursor;..stati
320c2 63 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  c fulltext_vtab 
320c3 2a 63 75 72 73 6f 72 5f 76 74 61 62 28 66 75 6c  *cursor_vtab(ful
320c4 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 29  ltext_cursor *c)
320c5 7b 0a 20 20 72 65 74 75 72 6e 20 28 66 75 6c 6c  {.  return (full
320c6 74 65 78 74 5f 76 74 61 62 20 2a 29 20 63 2d 3e  text_vtab *) c->
320c7 62 61 73 65 2e 70 56 74 61 62 3b 0a 7d 0a 0a 73  base.pVtab;.}..s
320c8 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
320c9 74 65 33 5f 6d 6f 64 75 6c 65 20 66 74 73 33 4d  te3_module fts3M
320ca 6f 64 75 6c 65 3b 20 20 20 2f 2a 20 66 6f 72 77  odule;   /* forw
320cb 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ard declaration 
320cc 2a 2f 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20  */../* Return a 
320cd 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65  dynamically gene
320ce 72 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20  rated statement 
320cf 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20  of the form. *  
320d0 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 63   insert into %_c
320d1 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c 20 2e  ontent (docid, .
320d2 2e 2e 29 20 76 61 6c 75 65 73 20 28 3f 2c 20 2e  ..) values (?, .
320d3 2e 2e 29 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63  ..). */.static c
320d4 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65  onst char *conte
320d5 6e 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65 6e  ntInsertStatemen
320d6 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
320d7 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66  *v){.  StringBuf
320d8 66 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 69 3b  fer sb;.  int i;
320d9 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ..  initStringBu
320da 66 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70 70  ffer(&sb);.  app
320db 65 6e 64 28 26 73 62 2c 20 22 69 6e 73 65 72 74  end(&sb, "insert
320dc 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20   into %_content 
320dd 28 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20 61 70  (docid, ");.  ap
320de 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d  pendList(&sb, v-
320df 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43  >nColumn, v->azC
320e0 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20  ontentColumn);. 
320e1 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 29 20   append(&sb, ") 
320e2 76 61 6c 75 65 73 20 28 3f 22 29 3b 0a 20 20 66  values (?");.  f
320e3 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f  or(i=0; i<v->nCo
320e4 6c 75 6d 6e 3b 20 2b 2b 69 29 0a 20 20 20 20 61  lumn; ++i).    a
320e5 70 70 65 6e 64 28 26 73 62 2c 20 22 2c 20 3f 22  ppend(&sb, ", ?"
320e6 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c  );.  append(&sb,
320e7 20 22 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20   ")");.  return 
320e8 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
320e9 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74  (&sb);.}../* Ret
320ea 75 72 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c  urn a dynamicall
320eb 79 20 67 65 6e 65 72 61 74 65 64 20 73 74 61 74  y generated stat
320ec 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72  ement of the for
320ed 6d 0a 20 2a 20 20 20 73 65 6c 65 63 74 20 3c 63  m. *   select <c
320ee 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 73 3e 20  ontent columns> 
320ef 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77  from %_content w
320f0 68 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 0a 20  here docid = ?. 
320f1 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
320f2 63 68 61 72 20 2a 63 6f 6e 74 65 6e 74 53 65 6c  char *contentSel
320f3 65 63 74 53 74 61 74 65 6d 65 6e 74 28 66 75 6c  ectStatement(ful
320f4 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
320f5 20 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73    StringBuffer s
320f6 62 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42  b;.  initStringB
320f7 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70  uffer(&sb);.  ap
320f8 70 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45 43  pend(&sb, "SELEC
320f9 54 20 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69  T ");.  appendLi
320fa 73 74 28 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75  st(&sb, v->nColu
320fb 6d 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74  mn, v->azContent
320fc 43 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65 6e  Column);.  appen
320fd 64 28 26 73 62 2c 20 22 20 46 52 4f 4d 20 25 5f  d(&sb, " FROM %_
320fe 63 6f 6e 74 65 6e 74 20 57 48 45 52 45 20 64 6f  content WHERE do
320ff 63 69 64 20 3d 20 3f 22 29 3b 0a 20 20 72 65 74  cid = ?");.  ret
32100 75 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65 72  urn stringBuffer
32101 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a  Data(&sb);.}../*
32102 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d 69   Return a dynami
32103 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20  cally generated 
32104 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65  statement of the
32105 20 66 6f 72 6d 0a 20 2a 20 20 20 75 70 64 61 74   form. *   updat
32106 65 20 25 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20  e %_content set 
32107 5b 63 6f 6c 5f 30 5d 20 3d 20 3f 2c 20 5b 63 6f  [col_0] = ?, [co
32108 6c 5f 31 5d 20 3d 20 3f 2c 20 2e 2e 2e 0a 20 2a  l_1] = ?, .... *
32109 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3210a 20 20 20 20 77 68 65 72 65 20 64 6f 63 69 64 20      where docid 
3210b 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63  = ?. */.static c
3210c 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65  onst char *conte
3210d 6e 74 55 70 64 61 74 65 53 74 61 74 65 6d 65 6e  ntUpdateStatemen
3210e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
3210f 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66  *v){.  StringBuf
32110 66 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 69 3b  fer sb;.  int i;
32111 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ..  initStringBu
32112 66 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70 70  ffer(&sb);.  app
32113 65 6e 64 28 26 73 62 2c 20 22 75 70 64 61 74 65  end(&sb, "update
32114 20 25 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20 22   %_content set "
32115 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
32116 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29  v->nColumn; ++i)
32117 20 7b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29   {.    if( i>0 )
32118 7b 0a 20 20 20 20 20 20 61 70 70 65 6e 64 28 26  {.      append(&
32119 73 62 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 7d  sb, ", ");.    }
3211a 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c  .    append(&sb,
3211b 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c   v->azContentCol
3211c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 61 70 70  umn[i]);.    app
3211d 65 6e 64 28 26 73 62 2c 20 22 20 3d 20 3f 22 29  end(&sb, " = ?")
3211e 3b 0a 20 20 7d 0a 20 20 61 70 70 65 6e 64 28 26  ;.  }.  append(&
3211f 73 62 2c 20 22 20 77 68 65 72 65 20 64 6f 63 69  sb, " where doci
32120 64 20 3d 20 3f 22 29 3b 0a 20 20 72 65 74 75 72  d = ?");.  retur
32121 6e 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 61  n stringBufferDa
32122 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20 50  ta(&sb);.}../* P
32123 75 74 73 20 61 20 66 72 65 73 68 6c 79 2d 70 72  uts a freshly-pr
32124 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
32125 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 69   determined by i
32126 53 74 6d 74 20 69 6e 20 2a 70 70 53 74 6d 74 2e  Stmt in *ppStmt.
32127 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 64 69 63  .** If the indic
32128 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 68  ated statement h
32129 61 73 20 6e 65 76 65 72 20 62 65 65 6e 20 70 72  as never been pr
3212a 65 70 61 72 65 64 2c 20 69 74 20 69 73 20 70 72  epared, it is pr
3212b 65 70 61 72 65 64 0a 2a 2a 20 61 6e 64 20 63 61  epared.** and ca
3212c 63 68 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20  ched, otherwise 
3212d 74 68 65 20 63 61 63 68 65 64 20 76 65 72 73 69  the cached versi
3212e 6f 6e 20 69 73 20 72 65 73 65 74 2e 0a 2a 2f 0a  on is reset..*/.
3212f 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 67  static int sql_g
32130 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 66 75 6c  et_statement(ful
32131 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 66  ltext_vtab *v, f
32132 75 6c 6c 74 65 78 74 5f 73 74 61 74 65 6d 65 6e  ulltext_statemen
32133 74 20 69 53 74 6d 74 2c 0a 20 20 20 20 20 20 20  t iStmt,.       
32134 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32135 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74        sqlite3_st
32136 6d 74 20 2a 2a 70 70 53 74 6d 74 29 7b 0a 20 20  mt **ppStmt){.  
32137 61 73 73 65 72 74 28 20 69 53 74 6d 74 3c 4d 41  assert( iStmt<MA
32138 58 5f 53 54 4d 54 20 29 3b 0a 20 20 69 66 28 20  X_STMT );.  if( 
32139 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
3213a 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 3d 3d 4e  ements[iStmt]==N
3213b 55 4c 4c 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ULL ){.    const
3213c 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20   char *zStmt;.  
3213d 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 77    int rc;.    sw
3213e 69 74 63 68 28 20 69 53 74 6d 74 20 29 7b 0a 20  itch( iStmt ){. 
3213f 20 20 20 20 20 63 61 73 65 20 43 4f 4e 54 45 4e       case CONTEN
32140 54 5f 49 4e 53 45 52 54 5f 53 54 4d 54 3a 0a 20  T_INSERT_STMT:. 
32141 20 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63         zStmt = c
32142 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61 74  ontentInsertStat
32143 65 6d 65 6e 74 28 76 29 3b 20 62 72 65 61 6b 3b  ement(v); break;
32144 0a 20 20 20 20 20 20 63 61 73 65 20 43 4f 4e 54  .      case CONT
32145 45 4e 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54 3a  ENT_SELECT_STMT:
32146 0a 20 20 20 20 20 20 20 20 7a 53 74 6d 74 20 3d  .        zStmt =
32147 20 63 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74   contentSelectSt
32148 61 74 65 6d 65 6e 74 28 76 29 3b 20 62 72 65 61  atement(v); brea
32149 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 43 4f  k;.      case CO
3214a 4e 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d  NTENT_UPDATE_STM
3214b 54 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d 74  T:.        zStmt
3214c 20 3d 20 63 6f 6e 74 65 6e 74 55 70 64 61 74 65   = contentUpdate
3214d 53 74 61 74 65 6d 65 6e 74 28 76 29 3b 20 62 72  Statement(v); br
3214e 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75  eak;.      defau
3214f 6c 74 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d  lt:.        zStm
32150 74 20 3d 20 66 75 6c 6c 74 65 78 74 5f 7a 53 74  t = fulltext_zSt
32151 61 74 65 6d 65 6e 74 5b 69 53 74 6d 74 5d 3b 0a  atement[iStmt];.
32152 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
32153 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
32154 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
32155 6d 65 2c 20 26 76 2d 3e 70 46 75 6c 6c 74 65 78  me, &v->pFulltex
32156 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d  tStatements[iStm
32157 74 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t],.            
32158 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 53 74               zSt
32159 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 74  mt);.    if( zSt
3215a 6d 74 20 21 3d 20 66 75 6c 6c 74 65 78 74 5f 7a  mt != fulltext_z
3215b 53 74 61 74 65 6d 65 6e 74 5b 69 53 74 6d 74 5d  Statement[iStmt]
3215c 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28  ) sqlite3_free((
3215d 76 6f 69 64 20 2a 29 20 7a 53 74 6d 74 29 3b 0a  void *) zStmt);.
3215e 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
3215f 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
32160 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  c;.  } else {.  
32161 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
32162 65 33 5f 72 65 73 65 74 28 76 2d 3e 70 46 75 6c  e3_reset(v->pFul
32163 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b  ltextStatements[
32164 69 53 74 6d 74 5d 29 3b 0a 20 20 20 20 69 66 28  iStmt]);.    if(
32165 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32166 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
32167 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 76 2d 3e  .  *ppStmt = v->
32168 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65  pFulltextStateme
32169 6e 74 73 5b 69 53 74 6d 74 5d 3b 0a 20 20 72 65  nts[iStmt];.  re
3216a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3216b 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 73 71 6c 69 74  }../* Like sqlit
3216c 65 33 5f 73 74 65 70 28 29 2c 20 62 75 74 20 63  e3_step(), but c
3216d 6f 6e 76 65 72 74 20 53 51 4c 49 54 45 5f 44 4f  onvert SQLITE_DO
3216e 4e 45 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  NE to SQLITE_OK 
3216f 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f  and.** SQLITE_RO
32170 57 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f  W to SQLITE_ERRO
32171 52 2e 20 20 55 73 65 66 75 6c 20 66 6f 72 20 73  R.  Useful for s
32172 74 61 74 65 6d 65 6e 74 73 20 6c 69 6b 65 20 55  tatements like U
32173 50 44 41 54 45 2c 0a 2a 2a 20 77 68 65 72 65 20  PDATE,.** where 
32174 77 65 20 65 78 70 65 63 74 20 6e 6f 20 72 65 73  we expect no res
32175 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ults..*/.static 
32176 69 6e 74 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73  int sql_single_s
32177 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  tep(sqlite3_stmt
32178 20 2a 73 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d   *s){.  int rc =
32179 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
3217a 3b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d  ;.  return (rc==
3217b 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20 3f 20 53  SQLITE_DONE) ? S
3217c 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 3b 0a 7d  QLITE_OK : rc;.}
3217d 0a 0a 2f 2a 20 4c 69 6b 65 20 73 71 6c 5f 67 65  ../* Like sql_ge
3217e 74 5f 73 74 61 74 65 6d 65 6e 74 28 29 2c 20 62  t_statement(), b
3217f 75 74 20 66 6f 72 20 73 70 65 63 69 61 6c 20 72  ut for special r
32180 65 70 6c 69 63 61 74 65 64 20 4c 45 41 46 5f 53  eplicated LEAF_S
32181 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65  ELECT.** stateme
32182 6e 74 73 2e 20 20 69 64 78 20 2d 31 20 69 73 20  nts.  idx -1 is 
32183 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 66  a special case f
32184 6f 72 20 61 6e 20 75 6e 63 61 63 68 65 64 20 76  or an uncached v
32185 65 72 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65  ersion of.** the
32186 20 73 74 61 74 65 6d 65 6e 74 20 28 75 73 65 64   statement (used
32187 20 69 6e 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   in the optimize
32188 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29   implementation)
32189 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
3218a 73 73 29 20 57 72 69 74 65 20 76 65 72 73 69 6f  ss) Write versio
3218b 6e 20 66 6f 72 20 67 65 6e 65 72 69 63 20 73 74  n for generic st
3218c 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 68 65  atements and the
3218d 6e 20 73 68 61 72 65 0a 2a 2a 20 74 68 61 74 20  n share.** that 
3218e 62 65 74 77 65 65 6e 20 74 68 65 20 63 61 63 68  between the cach
3218f 65 64 2d 73 74 61 74 65 6d 65 6e 74 20 66 75 6e  ed-statement fun
32190 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69  ctions..*/.stati
32191 63 20 69 6e 74 20 73 71 6c 5f 67 65 74 5f 6c 65  c int sql_get_le
32192 61 66 5f 73 74 61 74 65 6d 65 6e 74 28 66 75 6c  af_statement(ful
32193 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
32194 6e 74 20 69 64 78 2c 0a 20 20 20 20 20 20 20 20  nt idx,.        
32195 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32196 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
32197 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 29  3_stmt **ppStmt)
32198 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e  {.  assert( idx>
32199 3d 2d 31 20 26 26 20 69 64 78 3c 4d 45 52 47 45  =-1 && idx<MERGE
3219a 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 69 66 28 20  _COUNT );.  if( 
3219b 69 64 78 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 72  idx==-1 ){.    r
3219c 65 74 75 72 6e 20 73 71 6c 5f 70 72 65 70 61 72  eturn sql_prepar
3219d 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c  e(v->db, v->zDb,
3219e 20 76 2d 3e 7a 4e 61 6d 65 2c 20 70 70 53 74 6d   v->zName, ppStm
3219f 74 2c 20 4c 45 41 46 5f 53 45 4c 45 43 54 29 3b  t, LEAF_SELECT);
321a0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 2d 3e  .  }else if( v->
321a1 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
321a2 5b 69 64 78 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a 20  [idx]==NULL ){. 
321a3 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f     int rc = sql_
321a4 70 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76  prepare(v->db, v
321a5 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c  ->zDb, v->zName,
321a6 20 26 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74   &v->pLeafSelect
321a7 53 74 6d 74 73 5b 69 64 78 5d 2c 0a 20 20 20 20  Stmts[idx],.    
321a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321a9 20 20 20 20 20 4c 45 41 46 5f 53 45 4c 45 43 54       LEAF_SELECT
321aa 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
321ab 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
321ac 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n rc;.  }else{. 
321ad 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
321ae 74 65 33 5f 72 65 73 65 74 28 76 2d 3e 70 4c 65  te3_reset(v->pLe
321af 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64  afSelectStmts[id
321b0 78 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  x]);.    if( rc!
321b1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
321b2 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a  urn rc;.  }..  *
321b3 70 70 53 74 6d 74 20 3d 20 76 2d 3e 70 4c 65 61  ppStmt = v->pLea
321b4 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78  fSelectStmts[idx
321b5 5d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ];.  return SQLI
321b6 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73  TE_OK;.}../* ins
321b7 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65  ert into %_conte
321b8 6e 74 20 28 64 6f 63 69 64 2c 20 2e 2e 2e 29 20  nt (docid, ...) 
321b9 76 61 6c 75 65 73 20 28 5b 64 6f 63 69 64 5d 2c  values ([docid],
321ba 20 5b 70 56 61 6c 75 65 73 5d 29 0a 2a 2a 20 49   [pValues]).** I
321bb 66 20 74 68 65 20 64 6f 63 69 64 20 63 6f 6e 74  f the docid cont
321bc 61 69 6e 73 20 53 51 4c 20 4e 55 4c 4c 2c 20 74  ains SQL NULL, t
321bd 68 65 6e 20 61 20 75 6e 69 71 75 65 20 64 6f 63  hen a unique doc
321be 69 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 67 65  id will be.** ge
321bf 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  nerated..*/.stat
321c0 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 69  ic int content_i
321c1 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f 76  nsert(fulltext_v
321c2 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f  tab *v, sqlite3_
321c3 76 61 6c 75 65 20 2a 64 6f 63 69 64 2c 0a 20 20  value *docid,.  
321c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321c5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
321c6 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 29  value **pValues)
321c7 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
321c8 20 2a 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20   *s;.  int i;.  
321c9 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  int rc = sql_get
321ca 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f  _statement(v, CO
321cb 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54 4d  NTENT_INSERT_STM
321cc 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
321cd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
321ce 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
321cf 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
321d0 6c 75 65 28 73 2c 20 31 2c 20 64 6f 63 69 64 29  lue(s, 1, docid)
321d1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
321d2 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
321d3 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  c;..  for(i=0; i
321d4 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69  <v->nColumn; ++i
321d5 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
321d6 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73  te3_bind_value(s
321d7 2c 20 32 2b 69 2c 20 70 56 61 6c 75 65 73 5b 69  , 2+i, pValues[i
321d8 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  ]);.    if( rc!=
321d9 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
321da 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65  rn rc;.  }..  re
321db 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f  turn sql_single_
321dc 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 75  step(s);.}../* u
321dd 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20  pdate %_content 
321de 73 65 74 20 63 6f 6c 30 20 3d 20 70 56 61 6c 75  set col0 = pValu
321df 65 73 5b 30 5d 2c 20 63 6f 6c 31 20 3d 20 70 56  es[0], col1 = pV
321e0 61 6c 75 65 73 5b 31 5d 2c 20 2e 2e 2e 0a 20 2a  alues[1], .... *
321e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321e2 20 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20    where docid = 
321e3 5b 69 44 6f 63 69 64 5d 20 2a 2f 0a 73 74 61 74  [iDocid] */.stat
321e4 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 75  ic int content_u
321e5 70 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f 76  pdate(fulltext_v
321e6 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f  tab *v, sqlite3_
321e7 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 2c  value **pValues,
321e8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
321e9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
321ea 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b  e_int64 iDocid){
321eb 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
321ec 2a 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  *s;.  int i;.  i
321ed 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
321ee 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e  statement(v, CON
321ef 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54  TENT_UPDATE_STMT
321f0 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
321f1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
321f2 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69  urn rc;..  for(i
321f3 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e  =0; i<v->nColumn
321f4 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 72 63 20 3d  ; ++i){.    rc =
321f5 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
321f6 6c 75 65 28 73 2c 20 31 2b 69 2c 20 70 56 61 6c  lue(s, 1+i, pVal
321f7 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  ues[i]);.    if(
321f8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
321f9 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
321fa 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
321fb 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2b  bind_int64(s, 1+
321fc 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69 44 6f 63  v->nColumn, iDoc
321fd 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
321fe 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
321ff 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
32200 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
32201 73 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  s);.}..static vo
32202 69 64 20 66 72 65 65 53 74 72 69 6e 67 41 72 72  id freeStringArr
32203 61 79 28 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20  ay(int nString, 
32204 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 53 74  const char **pSt
32205 72 69 6e 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  ring){.  int i;.
32206 0a 20 20 66 6f 72 20 28 69 3d 30 20 3b 20 69 20  .  for (i=0 ; i 
32207 3c 20 6e 53 74 72 69 6e 67 20 3b 20 2b 2b 69 29  < nString ; ++i)
32208 20 7b 0a 20 20 20 20 69 66 28 20 70 53 74 72 69   {.    if( pStri
32209 6e 67 5b 69 5d 21 3d 4e 55 4c 4c 20 29 20 73 71  ng[i]!=NULL ) sq
3220a 6c 69 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64  lite3_free((void
3220b 20 2a 29 20 70 53 74 72 69 6e 67 5b 69 5d 29 3b   *) pString[i]);
3220c 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
3220d 72 65 65 28 28 76 6f 69 64 20 2a 29 20 70 53 74  ree((void *) pSt
3220e 72 69 6e 67 29 3b 0a 7d 0a 0a 2f 2a 20 73 65 6c  ring);.}../* sel
3220f 65 63 74 20 2a 20 66 72 6f 6d 20 25 5f 63 6f 6e  ect * from %_con
32210 74 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64  tent where docid
32211 20 3d 20 5b 69 44 6f 63 69 64 5d 0a 20 2a 20 54   = [iDocid]. * T
32212 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 64  he caller must d
32213 65 6c 65 74 65 20 74 68 65 20 72 65 74 75 72 6e  elete the return
32214 65 64 20 61 72 72 61 79 20 61 6e 64 20 61 6c 6c  ed array and all
32215 20 73 74 72 69 6e 67 73 20 69 6e 20 69 74 2e 0a   strings in it..
32216 20 2a 20 6e 75 6c 6c 20 66 69 65 6c 64 73 20 77   * null fields w
32217 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 6e 20 74  ill be NULL in t
32218 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61  he returned arra
32219 79 2e 0a 20 2a 0a 20 2a 20 54 4f 44 4f 3a 20 50  y.. *. * TODO: P
3221a 65 72 68 61 70 73 20 77 65 20 73 68 6f 75 6c 64  erhaps we should
3221b 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 2f   return pointer/
3221c 6c 65 6e 67 74 68 20 73 74 72 69 6e 67 73 20 68  length strings h
3221d 65 72 65 20 66 6f 72 20 63 6f 6e 73 69 73 74 65  ere for consiste
3221e 6e 63 79 0a 20 2a 20 77 69 74 68 20 6f 74 68 65  ncy. * with othe
3221f 72 20 63 6f 64 65 20 77 68 69 63 68 20 75 73 65  r code which use
32220 73 20 70 6f 69 6e 74 65 72 2f 6c 65 6e 67 74 68  s pointer/length
32221 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
32222 63 6f 6e 74 65 6e 74 5f 73 65 6c 65 63 74 28 66  content_select(f
32223 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
32224 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
32225 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ocid,.          
32226 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32227 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 2a 70 56  const char ***pV
32228 61 6c 75 65 73 29 7b 0a 20 20 73 71 6c 69 74 65  alues){.  sqlite
32229 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 63 6f 6e  3_stmt *s;.  con
3222a 73 74 20 63 68 61 72 20 2a 2a 76 61 6c 75 65 73  st char **values
3222b 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
3222c 20 72 63 3b 0a 0a 20 20 2a 70 56 61 6c 75 65 73   rc;..  *pValues
3222d 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 72 63 20 3d   = NULL;..  rc =
3222e 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
3222f 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 53 45  nt(v, CONTENT_SE
32230 4c 45 43 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  LECT_STMT, &s);.
32231 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32232 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32233 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
32234 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31  _bind_int64(s, 1
32235 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66 28  , iDocid);.  if(
32236 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32237 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
32238 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
32239 28 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  (s);.  if( rc!=S
3223a 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
3223b 72 6e 20 72 63 3b 0a 0a 20 20 76 61 6c 75 65 73  rn rc;..  values
3223c 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
3223d 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
3223e 63 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 73  c(v->nColumn * s
3223f 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63 68 61 72  izeof(const char
32240 20 2a 29 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   *));.  for(i=0;
32241 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b   i<v->nColumn; +
32242 2b 69 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  +i){.    if( sql
32243 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
32244 28 73 2c 20 69 29 3d 3d 53 51 4c 49 54 45 5f 4e  (s, i)==SQLITE_N
32245 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 76 61 6c  ULL ){.      val
32246 75 65 73 5b 69 5d 20 3d 20 4e 55 4c 4c 3b 0a 20  ues[i] = NULL;. 
32247 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
32248 76 61 6c 75 65 73 5b 69 5d 20 3d 20 73 74 72 69  values[i] = stri
32249 6e 67 5f 64 75 70 28 28 63 68 61 72 2a 29 73 71  ng_dup((char*)sq
3224a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
3224b 74 28 73 2c 20 69 29 29 3b 0a 20 20 20 20 7d 0a  t(s, i));.    }.
3224c 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70    }..  /* We exp
3224d 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77  ect only one row
3224e 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75  .  We must execu
3224f 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74  te another sqlit
32250 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74  e3_step().   * t
32251 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69  o complete the i
32252 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77  teration; otherw
32253 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ise the table wi
32254 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64  ll remain locked
32255 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69  . */.  rc = sqli
32256 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69  te3_step(s);.  i
32257 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
32258 4e 45 20 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75  NE ){.    *pValu
32259 65 73 20 3d 20 76 61 6c 75 65 73 3b 0a 20 20 20  es = values;.   
3225a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3225b 4b 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 53 74  K;.  }..  freeSt
3225c 72 69 6e 67 41 72 72 61 79 28 76 2d 3e 6e 43 6f  ringArray(v->nCo
3225d 6c 75 6d 6e 2c 20 76 61 6c 75 65 73 29 3b 0a 20  lumn, values);. 
3225e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
3225f 2a 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f  * delete from %_
32260 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f  content where do
32261 63 69 64 20 3d 20 5b 69 44 6f 63 69 64 20 5d 20  cid = [iDocid ] 
32262 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
32263 6e 74 65 6e 74 5f 64 65 6c 65 74 65 28 66 75 6c  ntent_delete(ful
32264 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
32265 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
32266 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  id){.  sqlite3_s
32267 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63  tmt *s;.  int rc
32268 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
32269 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f  ment(v, CONTENT_
3226a 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73 29  DELETE_STMT, &s)
3226b 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
3226c 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
3226d 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
3226e 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
3226f 20 31 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 69   1, iDocid);.  i
32270 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
32271 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
32272 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67   return sql_sing
32273 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f  le_step(s);.}../
32274 2a 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45  * Returns SQLITE
32275 5f 52 4f 57 20 69 66 20 61 6e 79 20 72 6f 77 73  _ROW if any rows
32276 20 65 78 69 73 74 20 69 6e 20 25 5f 63 6f 6e 74   exist in %_cont
32277 65 6e 74 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45  ent, SQLITE_DONE
32278 20 69 66 0a 2a 2a 20 6e 6f 20 72 6f 77 73 20 65   if.** no rows e
32279 78 69 73 74 2c 20 61 6e 64 20 61 6e 79 20 65 72  xist, and any er
3227a 72 6f 72 20 69 6e 20 63 61 73 65 20 6f 66 20 66  ror in case of f
3227b 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ailure..*/.stati
3227c 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 65 78  c int content_ex
3227d 69 73 74 73 28 66 75 6c 6c 74 65 78 74 5f 76 74  ists(fulltext_vt
3227e 61 62 20 2a 76 29 7b 0a 20 20 73 71 6c 69 74 65  ab *v){.  sqlite
3227f 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
32280 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
32281 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45  atement(v, CONTE
32282 4e 54 5f 45 58 49 53 54 53 5f 53 54 4d 54 2c 20  NT_EXISTS_STMT, 
32283 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
32284 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
32285 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
32286 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
32287 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
32288 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ROW ) return rc;
32289 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74  ..  /* We expect
3228a 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20   only one row.  
3228b 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20  We must execute 
3228c 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f  another sqlite3_
3228d 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63  step().   * to c
3228e 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72  omplete the iter
3228f 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65  ation; otherwise
32290 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20   the table will 
32291 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a  remain locked. *
32292 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
32293 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
32294 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
32295 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
32296 52 4f 57 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ROW;.  if( rc==S
32297 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
32298 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
32299 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3229a 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20  ./* insert into 
3229b 25 5f 73 65 67 6d 65 6e 74 73 20 76 61 6c 75 65  %_segments value
3229c 73 20 28 5b 70 44 61 74 61 5d 29 0a 2a 2a 20 20  s ([pData]).**  
3229d 20 72 65 74 75 72 6e 73 20 61 73 73 69 67 6e 65   returns assigne
3229e 64 20 62 6c 6f 63 6b 69 64 20 69 6e 20 2a 70 69  d blockid in *pi
3229f 42 6c 6f 63 6b 69 64 0a 2a 2f 0a 73 74 61 74 69  Blockid.*/.stati
322a0 63 20 69 6e 74 20 62 6c 6f 63 6b 5f 69 6e 73 65  c int block_inse
322a1 72 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  rt(fulltext_vtab
322a2 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *v, const char 
322a3 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
322a4 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
322a5 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
322a6 65 5f 69 6e 74 36 34 20 2a 70 69 42 6c 6f 63 6b  e_int64 *piBlock
322a7 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  id){.  sqlite3_s
322a8 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63  tmt *s;.  int rc
322a9 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
322aa 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 49 4e  ment(v, BLOCK_IN
322ab 53 45 52 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  SERT_STMT, &s);.
322ac 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
322ad 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
322ae 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
322af 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 2c 20 31 2c  _bind_blob(s, 1,
322b0 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 53   pData, nData, S
322b1 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
322b2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
322b3 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
322b4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
322b5 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72  step(s);.  if( r
322b6 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c==SQLITE_ROW ) 
322b7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
322b8 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ROR;.  if( rc!=S
322b9 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74  QLITE_DONE ) ret
322ba 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 62 6c  urn rc;..  /* bl
322bb 6f 63 6b 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20  ockid column is 
322bc 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f 77  an alias for row
322bd 69 64 2e 20 2a 2f 0a 20 20 2a 70 69 42 6c 6f 63  id. */.  *piBloc
322be 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61  kid = sqlite3_la
322bf 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
322c0 76 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e  v->db);.  return
322c1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
322c2 2a 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f  * delete from %_
322c3 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 20 20 77 68  segments.**   wh
322c4 65 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77  ere blockid betw
322c5 65 65 6e 20 5b 69 53 74 61 72 74 42 6c 6f 63 6b  een [iStartBlock
322c6 69 64 5d 20 61 6e 64 20 5b 69 45 6e 64 42 6c 6f  id] and [iEndBlo
322c7 63 6b 69 64 5d 0a 2a 2a 0a 2a 2a 20 44 65 6c 65  ckid].**.** Dele
322c8 74 65 73 20 74 68 65 20 72 61 6e 67 65 20 6f 66  tes the range of
322c9 20 62 6c 6f 63 6b 73 2c 20 69 6e 63 6c 75 73 69   blocks, inclusi
322ca 76 65 2c 20 75 73 65 64 20 74 6f 20 64 65 6c 65  ve, used to dele
322cb 74 65 20 74 68 65 20 62 6c 6f 63 6b 73 0a 2a 2a  te the blocks.**
322cc 20 77 68 69 63 68 20 66 6f 72 6d 20 61 20 73 65   which form a se
322cd 67 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  gment..*/.static
322ce 20 69 6e 74 20 62 6c 6f 63 6b 5f 64 65 6c 65 74   int block_delet
322cf 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
322d0 2a 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  *v,.            
322d1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
322d2 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42  te_int64 iStartB
322d3 6c 6f 63 6b 69 64 2c 20 73 71 6c 69 74 65 5f 69  lockid, sqlite_i
322d4 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64  nt64 iEndBlockid
322d5 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
322d6 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
322d7 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
322d8 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45  nt(v, BLOCK_DELE
322d9 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  TE_STMT, &s);.  
322da 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
322db 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
322dc 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
322dd 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20  ind_int64(s, 1, 
322de 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a  iStartBlockid);.
322df 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
322e0 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
322e1 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
322e2 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 32  _bind_int64(s, 2
322e3 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a  , iEndBlockid);.
322e4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
322e5 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
322e6 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73  ..  return sql_s
322e7 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d  ingle_step(s);.}
322e8 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c  ../* Returns SQL
322e9 49 54 45 5f 52 4f 57 20 77 69 74 68 20 2a 70 69  ITE_ROW with *pi
322ea 64 78 20 73 65 74 20 74 6f 20 74 68 65 20 6d 61  dx set to the ma
322eb 78 69 6d 75 6d 20 73 65 67 6d 65 6e 74 20 69 64  ximum segment id
322ec 78 20 66 6f 75 6e 64 0a 2a 2a 20 61 74 20 69 4c  x found.** at iL
322ed 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 73 20 53  evel.  Returns S
322ee 51 4c 49 54 45 5f 44 4f 4e 45 20 69 66 20 74 68  QLITE_DONE if th
322ef 65 72 65 20 61 72 65 20 6e 6f 20 73 65 67 6d 65  ere are no segme
322f0 6e 74 73 20 61 74 0a 2a 2a 20 69 4c 65 76 65 6c  nts at.** iLevel
322f1 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74  .  Otherwise ret
322f2 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  urns an error..*
322f3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
322f4 64 69 72 5f 6d 61 78 5f 69 6e 64 65 78 28 66 75  dir_max_index(fu
322f5 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
322f6 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20  int iLevel, int 
322f7 2a 70 69 64 78 29 7b 0a 20 20 73 71 6c 69 74 65  *pidx){.  sqlite
322f8 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
322f9 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
322fa 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49  atement(v, SEGDI
322fb 52 5f 4d 41 58 5f 49 4e 44 45 58 5f 53 54 4d 54  R_MAX_INDEX_STMT
322fc 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
322fd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
322fe 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
322ff 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
32300 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a  (s, 1, iLevel);.
32301 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32302 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32303 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
32304 5f 73 74 65 70 28 73 29 3b 0a 20 20 2f 2a 20 53  _step(s);.  /* S
32305 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 67 65 74  hould always get
32306 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f   at least one ro
32307 77 20 64 75 65 20 74 6f 20 68 6f 77 20 6d 61 78  w due to how max
32308 28 29 20 77 6f 72 6b 73 2e 20 2a 2f 0a 20 20 69  () works. */.  i
32309 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
3230a 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  NE ) return SQLI
3230b 54 45 5f 44 4f 4e 45 3b 0a 20 20 69 66 28 20 72  TE_DONE;.  if( r
3230c 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
3230d 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
3230e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74   NULL means that
3230f 20 74 68 65 72 65 20 77 65 72 65 20 6e 6f 20 69   there were no i
32310 6e 70 75 74 73 20 74 6f 20 6d 61 78 28 29 2e 20  nputs to max(). 
32311 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  */.  if( SQLITE_
32312 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 63 6f  NULL==sqlite3_co
32313 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20 30 29 20  lumn_type(s, 0) 
32314 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
32315 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 20  te3_step(s);.   
32316 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
32317 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ROW ) return SQL
32318 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72  ITE_ERROR;.    r
32319 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
3231a 20 2a 70 69 64 78 20 3d 20 73 71 6c 69 74 65 33   *pidx = sqlite3
3231b 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 30  _column_int(s, 0
3231c 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65  );..  /* We expe
3231d 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e  ct only one row.
3231e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74    We must execut
3231f 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65  e another sqlite
32320 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f  3_step().   * to
32321 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74   complete the it
32322 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69  eration; otherwi
32323 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c  se the table wil
32324 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e  l remain locked.
32325 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
32326 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
32327 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
32328 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
32329 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63  _ERROR;.  if( rc
3232a 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20  !=SQLITE_DONE ) 
3232b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 65 74  return rc;.  ret
3232c 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a  urn SQLITE_ROW;.
3232d 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74  }../* insert int
3232e 6f 20 25 5f 73 65 67 64 69 72 20 76 61 6c 75 65  o %_segdir value
3232f 73 20 28 0a 2a 2a 20 20 20 5b 69 4c 65 76 65 6c  s (.**   [iLevel
32330 5d 2c 20 5b 69 64 78 5d 2c 0a 2a 2a 20 20 20 5b  ], [idx],.**   [
32331 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 5d 2c 20  iStartBlockid], 
32332 5b 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b  [iLeavesEndBlock
32333 69 64 5d 2c 20 5b 69 45 6e 64 42 6c 6f 63 6b 69  id], [iEndBlocki
32334 64 5d 2c 0a 2a 2a 20 20 20 5b 70 52 6f 6f 74 44  d],.**   [pRootD
32335 61 74 61 5d 0a 2a 2a 20 29 0a 2a 2f 0a 73 74 61  ata].** ).*/.sta
32336 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 73  tic int segdir_s
32337 65 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  et(fulltext_vtab
32338 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *v, int iLevel,
32339 20 69 6e 74 20 69 64 78 2c 0a 20 20 20 20 20 20   int idx,.      
3233a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3233b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74  sqlite_int64 iSt
3233c 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  artBlockid,.    
3233d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3233e 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
3233f 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69 64  LeavesEndBlockid
32340 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32341 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
32342 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64  nt64 iEndBlockid
32343 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32344 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
32345 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 2c 20 69  ar *pRootData, i
32346 6e 74 20 6e 52 6f 6f 74 44 61 74 61 29 7b 0a 20  nt nRootData){. 
32347 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
32348 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
32349 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
3234a 2c 20 53 45 47 44 49 52 5f 53 45 54 5f 53 54 4d  , SEGDIR_SET_STM
3234b 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
3234c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
3234d 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
3234e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
3234f 74 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b  t(s, 1, iLevel);
32350 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
32351 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
32352 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
32353 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c  3_bind_int(s, 2,
32354 20 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21   idx);.  if( rc!
32355 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
32356 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
32357 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
32358 36 34 28 73 2c 20 33 2c 20 69 53 74 61 72 74 42  64(s, 3, iStartB
32359 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
3235a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3235b 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
3235c 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
3235d 6e 74 36 34 28 73 2c 20 34 2c 20 69 4c 65 61 76  nt64(s, 4, iLeav
3235e 65 73 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20  esEndBlockid);. 
3235f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
32360 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
32361 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
32362 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 35 2c  bind_int64(s, 5,
32363 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20   iEndBlockid);. 
32364 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
32365 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
32366 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
32367 62 69 6e 64 5f 62 6c 6f 62 28 73 2c 20 36 2c 20  bind_blob(s, 6, 
32368 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74  pRootData, nRoot
32369 44 61 74 61 2c 20 53 51 4c 49 54 45 5f 53 54 41  Data, SQLITE_STA
3236a 54 49 43 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  TIC);.  if( rc!=
3236b 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
3236c 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e  rn rc;..  return
3236d 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70   sql_single_step
3236e 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 51 75 65 72 69  (s);.}../* Queri
3236f 65 73 20 25 5f 73 65 67 64 69 72 20 66 6f 72 20  es %_segdir for 
32370 74 68 65 20 62 6c 6f 63 6b 20 73 70 61 6e 20 6f  the block span o
32371 66 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 69  f the segments i
32372 6e 20 6c 65 76 65 6c 0a 2a 2a 20 69 4c 65 76 65  n level.** iLeve
32373 6c 2e 20 20 52 65 74 75 72 6e 73 20 53 51 4c 49  l.  Returns SQLI
32374 54 45 5f 44 4f 4e 45 20 69 66 20 74 68 65 72 65  TE_DONE if there
32375 20 61 72 65 20 6e 6f 20 62 6c 6f 63 6b 73 20 66   are no blocks f
32376 6f 72 20 69 4c 65 76 65 6c 2c 0a 2a 2a 20 53 51  or iLevel,.** SQ
32377 4c 49 54 45 5f 52 4f 57 20 69 66 20 74 68 65 72  LITE_ROW if ther
32378 65 20 61 72 65 20 62 6c 6f 63 6b 73 2c 20 65 6c  e are blocks, el
32379 73 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  se an error..*/.
3237a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 64 69  static int segdi
3237b 72 5f 73 70 61 6e 28 66 75 6c 6c 74 65 78 74 5f  r_span(fulltext_
3237c 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65  vtab *v, int iLe
3237d 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  vel,.           
3237e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
3237f 74 65 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72  te_int64 *piStar
32380 74 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20  tBlockid,.      
32381 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32382 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
32383 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20  iEndBlockid){.  
32384 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
32385 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
32386 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
32387 20 53 45 47 44 49 52 5f 53 50 41 4e 5f 53 54 4d   SEGDIR_SPAN_STM
32388 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
32389 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
3238a 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
3238b 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
3238c 74 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b  t(s, 1, iLevel);
3238d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3238e 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
3238f 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
32390 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28  3_step(s);.  if(
32391 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
32392 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
32393 5f 44 4f 4e 45 3b 20 20 2f 2a 20 53 68 6f 75 6c  _DONE;  /* Shoul
32394 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 2a  d never happen *
32395 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  /.  if( rc!=SQLI
32396 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
32397 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 68  rc;..  /* This h
32398 61 70 70 65 6e 73 20 69 66 20 61 6c 6c 20 73 65  appens if all se
32399 67 6d 65 6e 74 73 20 61 74 20 74 68 69 73 20 6c  gments at this l
3239a 65 76 65 6c 20 61 72 65 20 65 6e 74 69 72 65 6c  evel are entirel
3239b 79 20 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 20 20 69  y inline. */.  i
3239c 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d  f( SQLITE_NULL==
3239d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
3239e 79 70 65 28 73 2c 20 30 29 20 29 7b 0a 20 20 20  ype(s, 0) ){.   
3239f 20 2f 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e   /* We expect on
323a0 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20  ly one row.  We 
323a1 6d 75 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f  must execute ano
323a2 74 68 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65  ther sqlite3_ste
323a3 70 28 29 0a 20 20 20 20 20 2a 20 74 6f 20 63 6f  p().     * to co
323a4 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72 61  mplete the itera
323a5 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65 20  tion; otherwise 
323a6 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20 72  the table will r
323a7 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f  emain locked. */
323a8 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 73  .    int rc2 = s
323a9 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
323aa 20 20 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c      if( rc2==SQL
323ab 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e  ITE_ROW ) return
323ac 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
323ad 20 20 20 72 65 74 75 72 6e 20 72 63 32 3b 0a 20     return rc2;. 
323ae 20 7d 0a 0a 20 20 2a 70 69 53 74 61 72 74 42 6c   }..  *piStartBl
323af 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  ockid = sqlite3_
323b0 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20  column_int64(s, 
323b1 30 29 3b 0a 20 20 2a 70 69 45 6e 64 42 6c 6f 63  0);.  *piEndBloc
323b2 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  kid = sqlite3_co
323b3 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29  lumn_int64(s, 1)
323b4 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63  ;..  /* We expec
323b5 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20  t only one row. 
323b6 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65   We must execute
323b7 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33   another sqlite3
323b8 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20  _step().   * to 
323b9 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65  complete the ite
323ba 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73  ration; otherwis
323bb 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c  e the table will
323bc 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20   remain locked. 
323bd 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
323be 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28  3_step(s);.  if(
323bf 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc==SQLITE_ROW 
323c0 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
323c1 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21  ERROR;.  if( rc!
323c2 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72  =SQLITE_DONE ) r
323c3 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 65 74 75  eturn rc;.  retu
323c4 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 7d  rn SQLITE_ROW;.}
323c5 0a 0a 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20  ../* Delete the 
323c6 73 65 67 6d 65 6e 74 20 62 6c 6f 63 6b 73 20 61  segment blocks a
323c7 6e 64 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63  nd segment direc
323c8 74 6f 72 79 20 72 65 63 6f 72 64 73 20 66 6f 72  tory records for
323c9 20 61 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e 74 73   all.** segments
323ca 20 61 74 20 69 4c 65 76 65 6c 2e 0a 2a 2f 0a 73   at iLevel..*/.s
323cb 74 61 74 69 63 20 69 6e 74 20 73 65 67 64 69 72  tatic int segdir
323cc 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74  _delete(fulltext
323cd 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
323ce 65 76 65 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33  evel){.  sqlite3
323cf 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 73 71 6c 69  _stmt *s;.  sqli
323d0 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42  te_int64 iStartB
323d1 6c 6f 63 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63  lockid, iEndBloc
323d2 6b 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  kid;.  int rc = 
323d3 73 65 67 64 69 72 5f 73 70 61 6e 28 76 2c 20 69  segdir_span(v, i
323d4 4c 65 76 65 6c 2c 20 26 69 53 74 61 72 74 42 6c  Level, &iStartBl
323d5 6f 63 6b 69 64 2c 20 26 69 45 6e 64 42 6c 6f 63  ockid, &iEndBloc
323d6 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  kid);.  if( rc!=
323d7 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 63  SQLITE_ROW && rc
323d8 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20  !=SQLITE_DONE ) 
323d9 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 66  return rc;..  if
323da 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
323db 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f   ){.    rc = blo
323dc 63 6b 5f 64 65 6c 65 74 65 28 76 2c 20 69 53 74  ck_delete(v, iSt
323dd 61 72 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e 64  artBlockid, iEnd
323de 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66  Blockid);.    if
323df 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
323e0 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
323e1 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68  ..  /* Delete th
323e2 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74  e segment direct
323e3 6f 72 79 20 69 74 73 65 6c 66 2e 20 2a 2f 0a 20  ory itself. */. 
323e4 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
323e5 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49  atement(v, SEGDI
323e6 52 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26  R_DELETE_STMT, &
323e7 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
323e8 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
323e9 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
323ea 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
323eb 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20  s, 1, iLevel);. 
323ec 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
323ed 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
323ee 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
323ef 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
323f0 0a 2f 2a 20 44 65 6c 65 74 65 20 65 6e 74 69 72  ./* Delete entir
323f1 65 20 66 74 73 20 69 6e 64 65 78 2c 20 53 51 4c  e fts index, SQL
323f2 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
323f3 73 2c 20 72 65 6c 65 76 61 6e 74 20 65 72 72 6f  s, relevant erro
323f4 72 20 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72 65 2e  r on.** failure.
323f5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
323f6 65 67 64 69 72 5f 64 65 6c 65 74 65 5f 61 6c 6c  egdir_delete_all
323f7 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
323f8 76 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  v){.  sqlite3_st
323f9 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
323fa 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
323fb 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 44 45  ent(v, SEGDIR_DE
323fc 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26  LETE_ALL_STMT, &
323fd 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
323fe 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
323ff 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
32400 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b  _single_step(s);
32401 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
32402 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
32403 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65  ;..  rc = sql_ge
32404 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42  t_statement(v, B
32405 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f  LOCK_DELETE_ALL_
32406 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
32407 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
32408 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
32409 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65  eturn sql_single
3240a 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20  _step(s);.}../* 
3240b 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  Returns SQLITE_O
3240c 4b 20 77 69 74 68 20 2a 70 6e 53 65 67 6d 65 6e  K with *pnSegmen
3240d 74 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  ts set to the nu
3240e 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
3240f 69 6e 0a 2a 2a 20 25 5f 73 65 67 64 69 72 20 61  in.** %_segdir a
32410 6e 64 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20 73  nd *piMaxLevel s
32411 65 74 20 74 6f 20 74 68 65 20 68 69 67 68 65 73  et to the highes
32412 74 20 6c 65 76 65 6c 20 77 68 69 63 68 20 68 61  t level which ha
32413 73 20 61 0a 2a 2a 20 73 65 67 6d 65 6e 74 2e 20  s a.** segment. 
32414 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
32415 6e 73 20 74 68 65 20 53 51 4c 69 74 65 20 65 72  ns the SQLite er
32416 72 6f 72 20 77 68 69 63 68 20 63 61 75 73 65 64  ror which caused
32417 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61   failure..*/.sta
32418 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 63  tic int segdir_c
32419 6f 75 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  ount(fulltext_vt
3241a 61 62 20 2a 76 2c 20 69 6e 74 20 2a 70 6e 53 65  ab *v, int *pnSe
3241b 67 6d 65 6e 74 73 2c 20 69 6e 74 20 2a 70 69 4d  gments, int *piM
3241c 61 78 4c 65 76 65 6c 29 7b 0a 20 20 73 71 6c 69  axLevel){.  sqli
3241d 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
3241e 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
3241f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47  statement(v, SEG
32420 44 49 52 5f 43 4f 55 4e 54 5f 53 54 4d 54 2c 20  DIR_COUNT_STMT, 
32421 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
32422 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
32423 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
32424 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
32425 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a   /* TODO(shess):
32426 20 54 68 69 73 20 63 61 73 65 20 73 68 6f 75 6c   This case shoul
32427 64 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c  d not be possibl
32428 65 3f 20 20 53 68 6f 75 6c 64 20 73 74 72 6f 6e  e?  Should stron
32429 67 65 72 0a 20 20 2a 2a 20 6d 65 61 73 75 72 65  ger.  ** measure
3242a 73 20 62 65 20 74 61 6b 65 6e 20 69 66 20 69 74  s be taken if it
3242b 20 68 61 70 70 65 6e 73 3f 0a 20 20 2a 2f 0a 20   happens?.  */. 
3242c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3242d 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2a 70 6e 53  DONE ){.    *pnS
3242e 65 67 6d 65 6e 74 73 20 3d 20 30 3b 0a 20 20 20  egments = 0;.   
3242f 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20 3d 20 30   *piMaxLevel = 0
32430 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
32431 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
32432 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
32433 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
32434 20 2a 70 6e 53 65 67 6d 65 6e 74 73 20 3d 20 73   *pnSegments = s
32435 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
32436 74 28 73 2c 20 30 29 3b 0a 20 20 2a 70 69 4d 61  t(s, 0);.  *piMa
32437 78 4c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33  xLevel = sqlite3
32438 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 31  _column_int(s, 1
32439 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65  );..  /* We expe
3243a 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e  ct only one row.
3243b 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74    We must execut
3243c 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65  e another sqlite
3243d 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f  3_step().   * to
3243e 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74   complete the it
3243f 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69  eration; otherwi
32440 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c  se the table wil
32441 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e  l remain locked.
32442 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
32443 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
32444 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
32445 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  E ) return SQLIT
32446 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 72 63 3d 3d  E_OK;.  if( rc==
32447 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74  SQLITE_ROW ) ret
32448 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
32449 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
3244a 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  ../* TODO(shess)
3244b 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
3244c 6d 73 28 29 20 69 73 20 66 61 72 20 64 6f 77 6e  ms() is far down
3244d 20 74 68 65 20 66 69 6c 65 20 62 65 63 61 75 73   the file becaus
3244e 65 0a 2a 2a 20 77 72 69 74 65 5a 65 72 6f 53 65  e.** writeZeroSe
3244f 67 6d 65 6e 74 28 29 20 69 73 20 66 61 72 20 64  gment() is far d
32450 6f 77 6e 20 74 68 65 20 66 69 6c 65 20 62 65 63  own the file bec
32451 61 75 73 65 20 4c 65 61 66 57 72 69 74 65 72 20  ause LeafWriter 
32452 69 73 20 66 61 72 0a 2a 2a 20 64 6f 77 6e 20 74  is far.** down t
32453 68 65 20 66 69 6c 65 2e 20 20 43 6f 6e 73 69 64  he file.  Consid
32454 65 72 20 72 65 66 61 63 74 6f 72 69 6e 67 20 74  er refactoring t
32455 68 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20  he code to move 
32456 74 68 65 20 6e 6f 6e 2d 76 74 61 62 0a 2a 2a 20  the non-vtab.** 
32457 63 6f 64 65 20 61 62 6f 76 65 20 74 68 65 20 76  code above the v
32458 74 61 62 20 63 6f 64 65 20 73 6f 20 74 68 61 74  tab code so that
32459 20 77 65 20 64 6f 6e 27 74 20 6e 65 65 64 20 74   we don't need t
3245a 68 69 73 20 66 6f 72 77 61 72 64 0a 2a 2a 20 72  his forward.** r
3245b 65 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61  eference..*/.sta
3245c 74 69 63 20 69 6e 74 20 63 6c 65 61 72 50 65 6e  tic int clearPen
3245d 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65  dingTerms(fullte
3245e 78 74 5f 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a  xt_vtab *v);../*
3245f 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 6d 65 6d  .** Free the mem
32460 6f 72 79 20 75 73 65 64 20 74 6f 20 63 6f 6e 74  ory used to cont
32461 61 69 6e 20 61 20 66 75 6c 6c 74 65 78 74 5f 76  ain a fulltext_v
32462 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  tab structure..*
32463 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 75  /.static void fu
32464 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74  lltext_vtab_dest
32465 72 6f 79 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  roy(fulltext_vta
32466 62 20 2a 76 29 7b 0a 20 20 69 6e 74 20 69 53 74  b *v){.  int iSt
32467 6d 74 2c 20 69 3b 0a 0a 20 20 46 54 53 54 52 41  mt, i;..  FTSTRA
32468 43 45 28 28 22 46 54 53 33 20 44 65 73 74 72 6f  CE(("FTS3 Destro
32469 79 20 25 70 5c 6e 22 2c 20 76 29 29 3b 0a 20 20  y %p\n", v));.  
3246a 66 6f 72 28 20 69 53 74 6d 74 3d 30 3b 20 69 53  for( iStmt=0; iS
3246b 74 6d 74 3c 4d 41 58 5f 53 54 4d 54 3b 20 69 53  tmt<MAX_STMT; iS
3246c 74 6d 74 2b 2b 20 29 7b 0a 20 20 20 20 69 66 28  tmt++ ){.    if(
3246d 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61   v->pFulltextSta
3246e 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 21 3d  tements[iStmt]!=
3246f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 73 71  NULL ){.      sq
32470 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 76  lite3_finalize(v
32471 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65  ->pFulltextState
32472 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 29 3b 0a 20  ments[iStmt]);. 
32473 20 20 20 20 20 76 2d 3e 70 46 75 6c 6c 74 65 78       v->pFulltex
32474 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d  tStatements[iStm
32475 74 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d  t] = NULL;.    }
32476 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 20 69 3d 30  .  }..  for( i=0
32477 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b  ; i<MERGE_COUNT;
32478 20 69 2b 2b 20 29 7b 0a 20 20 20 20 69 66 28 20   i++ ){.    if( 
32479 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74  v->pLeafSelectSt
3247a 6d 74 73 5b 69 5d 21 3d 4e 55 4c 4c 20 29 7b 0a  mts[i]!=NULL ){.
3247b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69        sqlite3_fi
3247c 6e 61 6c 69 7a 65 28 76 2d 3e 70 4c 65 61 66 53  nalize(v->pLeafS
3247d 65 6c 65 63 74 53 74 6d 74 73 5b 69 5d 29 3b 0a  electStmts[i]);.
3247e 20 20 20 20 20 20 76 2d 3e 70 4c 65 61 66 53 65        v->pLeafSe
3247f 6c 65 63 74 53 74 6d 74 73 5b 69 5d 20 3d 20 4e  lectStmts[i] = N
32480 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ULL;.    }.  }..
32481 20 20 69 66 28 20 76 2d 3e 70 54 6f 6b 65 6e 69    if( v->pTokeni
32482 7a 65 72 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  zer!=NULL ){.   
32483 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e   v->pTokenizer->
32484 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f  pModule->xDestro
32485 79 28 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29  y(v->pTokenizer)
32486 3b 0a 20 20 20 20 76 2d 3e 70 54 6f 6b 65 6e 69  ;.    v->pTokeni
32487 7a 65 72 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a  zer = NULL;.  }.
32488 0a 20 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54  .  clearPendingT
32489 65 72 6d 73 28 76 29 3b 0a 0a 20 20 73 71 6c 69  erms(v);..  sqli
3248a 74 65 33 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f  te3_free(v->azCo
3248b 6c 75 6d 6e 29 3b 0a 20 20 66 6f 72 28 69 20 3d  lumn);.  for(i =
3248c 20 30 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75   0; i < v->nColu
3248d 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20 73  mn; ++i) {.    s
3248e 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d 3e 61  qlite3_free(v->a
3248f 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69  zContentColumn[i
32490 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ]);.  }.  sqlite
32491 33 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f 6e 74  3_free(v->azCont
32492 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71  entColumn);.  sq
32493 6c 69 74 65 33 5f 66 72 65 65 28 76 29 3b 0a 7d  lite3_free(v);.}
32494 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79  ../*.** Token ty
32495 70 65 73 20 66 6f 72 20 70 61 72 73 69 6e 67 20  pes for parsing 
32496 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  the arguments to
32497 20 78 43 6f 6e 6e 65 63 74 20 6f 72 20 78 43 72   xConnect or xCr
32498 65 61 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  eate..*/.#define
32499 20 54 4f 4b 45 4e 5f 45 4f 46 20 20 20 20 20 20   TOKEN_EOF      
3249a 20 20 20 30 20 20 20 20 2f 2a 20 45 6e 64 20 6f     0    /* End o
3249b 66 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e  f file */.#defin
3249c 65 20 54 4f 4b 45 4e 5f 53 50 41 43 45 20 20 20  e TOKEN_SPACE   
3249d 20 20 20 20 31 20 20 20 20 2f 2a 20 41 6e 79 20      1    /* Any 
3249e 6b 69 6e 64 20 6f 66 20 77 68 69 74 65 73 70 61  kind of whitespa
3249f 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f  ce */.#define TO
324a0 4b 45 4e 5f 49 44 20 20 20 20 20 20 20 20 20 20  KEN_ID          
324a1 32 20 20 20 20 2f 2a 20 41 6e 20 69 64 65 6e 74  2    /* An ident
324a2 69 66 69 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  ifier */.#define
324a3 20 54 4f 4b 45 4e 5f 53 54 52 49 4e 47 20 20 20   TOKEN_STRING   
324a4 20 20 20 33 20 20 20 20 2f 2a 20 41 20 73 74 72     3    /* A str
324a5 69 6e 67 20 6c 69 74 65 72 61 6c 20 2a 2f 0a 23  ing literal */.#
324a6 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 50 55 4e  define TOKEN_PUN
324a7 43 54 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a  CT       4    /*
324a8 20 41 20 73 69 6e 67 6c 65 20 70 75 6e 63 74 75   A single punctu
324a9 61 74 69 6f 6e 20 63 68 61 72 61 63 74 65 72 20  ation character 
324aa 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69  */../*.** If X i
324ab 73 20 61 20 63 68 61 72 61 63 74 65 72 20 74 68  s a character th
324ac 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 69  at can be used i
324ad 6e 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20  n an identifier 
324ae 74 68 65 6e 0a 2a 2a 20 66 74 73 49 64 43 68 61  then.** ftsIdCha
324af 72 28 58 29 20 77 69 6c 6c 20 62 65 20 74 72 75  r(X) will be tru
324b0 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74  e.  Otherwise it
324b1 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a   is false..**.**
324b2 20 46 6f 72 20 41 53 43 49 49 2c 20 61 6e 79 20   For ASCII, any 
324b3 63 68 61 72 61 63 74 65 72 20 77 69 74 68 20 74  character with t
324b4 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69  he high-order bi
324b5 74 20 73 65 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f  t set is.** allo
324b6 77 65 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69  wed in an identi
324b7 66 69 65 72 2e 20 20 46 6f 72 20 37 2d 62 69 74  fier.  For 7-bit
324b8 20 63 68 61 72 61 63 74 65 72 73 2c 20 0a 2a 2a   characters, .**
324b9 20 69 73 46 74 73 49 64 43 68 61 72 5b 58 5d 20   isFtsIdChar[X] 
324ba 6d 75 73 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a  must be 1..**.**
324bb 20 54 69 63 6b 65 74 20 23 31 30 36 36 2e 20 20   Ticket #1066.  
324bc 74 68 65 20 53 51 4c 20 73 74 61 6e 64 61 72 64  the SQL standard
324bd 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20   does not allow 
324be 27 24 27 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 69  '$' in the.** mi
324bf 64 64 6c 65 20 6f 66 20 69 64 65 6e 74 66 69 65  ddle of identfie
324c0 72 73 2e 20 20 42 75 74 20 6d 61 6e 79 20 53 51  rs.  But many SQ
324c1 4c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  L implementation
324c2 73 20 64 6f 2e 20 0a 2a 2a 20 53 51 4c 69 74 65  s do. .** SQLite
324c3 20 77 69 6c 6c 20 61 6c 6c 6f 77 20 27 24 27 20   will allow '$' 
324c4 69 6e 20 69 64 65 6e 74 69 66 69 65 72 73 20 66  in identifiers f
324c5 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  or compatibility
324c6 2e 0a 2a 2a 20 42 75 74 20 74 68 65 20 66 65 61  ..** But the fea
324c7 74 75 72 65 20 69 73 20 75 6e 64 6f 63 75 6d 65  ture is undocume
324c8 6e 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nted..*/.static 
324c9 63 6f 6e 73 74 20 63 68 61 72 20 69 73 46 74 73  const char isFts
324ca 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20  IdChar[] = {./* 
324cb 78 30 20 78 31 20 78 32 20 78 33 20 78 34 20 78  x0 x1 x2 x3 x4 x
324cc 35 20 78 36 20 78 37 20 78 38 20 78 39 20 78 41  5 x6 x7 x8 x9 xA
324cd 20 78 42 20 78 43 20 78 44 20 78 45 20 78 46 20   xB xC xD xE xF 
324ce 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  */.    0, 0, 0, 
324cf 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 1, 0, 0, 0, 0
324d0 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
324d1 20 30 2c 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f   0, 0,  /* 2x */
324d2 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c  .    1, 1, 1, 1,
324d3 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
324d4 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
324d5 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20  , 0,  /* 3x */. 
324d6 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
324d7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
324d8 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
324d9 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20  1,  /* 4x */.   
324da 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
324db 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
324dc 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  , 0, 0, 0, 0, 1,
324dd 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30    /* 5x */.    0
324de 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
324df 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
324e0 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20  1, 1, 1, 1, 1,  
324e1 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20  /* 6x */.    1, 
324e2 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
324e3 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
324e4 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
324e5 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e   7x */.};.#defin
324e6 65 20 66 74 73 49 64 43 68 61 72 28 43 29 20 20  e ftsIdChar(C)  
324e7 28 28 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30  (((c=C)&0x80)!=0
324e8 20 7c 7c 20 28 63 3e 30 78 31 66 20 26 26 20 69   || (c>0x1f && i
324e9 73 46 74 73 49 64 43 68 61 72 5b 63 2d 30 78 32  sFtsIdChar[c-0x2
324ea 30 5d 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  0])).../*.** Ret
324eb 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  urn the length o
324ec 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  f the token that
324ed 20 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e   begins at z[0].
324ee 20 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 74   .** Store the t
324ef 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f  oken type in *to
324f0 6b 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20 72  kenType before r
324f1 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61  eturning..*/.sta
324f2 74 69 63 20 69 6e 74 20 66 74 73 47 65 74 54 6f  tic int ftsGetTo
324f3 6b 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ken(const char *
324f4 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e 54 79 70  z, int *tokenTyp
324f5 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a  e){.  int i, c;.
324f6 20 20 73 77 69 74 63 68 28 20 2a 7a 20 29 7b 0a    switch( *z ){.
324f7 20 20 20 20 63 61 73 65 20 30 3a 20 7b 0a 20 20      case 0: {.  
324f8 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
324f9 20 54 4f 4b 45 4e 5f 45 4f 46 3b 0a 20 20 20 20   TOKEN_EOF;.    
324fa 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
324fb 7d 0a 20 20 20 20 63 61 73 65 20 27 20 27 3a 20  }.    case ' ': 
324fc 63 61 73 65 20 27 5c 74 27 3a 20 63 61 73 65 20  case '\t': case 
324fd 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c 66 27 3a  '\n': case '\f':
324fe 20 63 61 73 65 20 27 5c 72 27 3a 20 7b 0a 20 20   case '\r': {.  
324ff 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 73 61 66      for(i=1; saf
32500 65 5f 69 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b  e_isspace(z[i]);
32501 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74   i++){}.      *t
32502 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e  okenType = TOKEN
32503 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20 72 65  _SPACE;.      re
32504 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
32505 20 20 63 61 73 65 20 27 60 27 3a 0a 20 20 20 20    case '`':.    
32506 63 61 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 63  case '\'':.    c
32507 61 73 65 20 27 22 27 3a 20 7b 0a 20 20 20 20 20  ase '"': {.     
32508 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30   int delim = z[0
32509 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  ];.      for(i=1
3250a 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69  ; (c=z[i])!=0; i
3250b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
3250c 20 63 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20   c==delim ){.   
3250d 20 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31         if( z[i+1
3250e 5d 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20  ]==delim ){.    
3250f 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
32510 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
32511 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
32512 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
32513 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
32514 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32515 3d 20 54 4f 4b 45 4e 5f 53 54 52 49 4e 47 3b 0a  = TOKEN_STRING;.
32516 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 20 2b        return i +
32517 20 28 63 21 3d 30 29 3b 0a 20 20 20 20 7d 0a 20   (c!=0);.    }. 
32518 20 20 20 63 61 73 65 20 27 5b 27 3a 20 7b 0a 20     case '[': {. 
32519 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 63 3d       for(i=1, c=
3251a 7a 5b 30 5d 3b 20 63 21 3d 27 5d 27 20 26 26 20  z[0]; c!=']' && 
3251b 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b  (c=z[i])!=0; i++
3251c 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  ){}.      *token
3251d 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 49 44 3b  Type = TOKEN_ID;
3251e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
3251f 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
32520 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  lt: {.      if( 
32521 21 66 74 73 49 64 43 68 61 72 28 2a 7a 29 20 29  !ftsIdChar(*z) )
32522 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
32523 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
32524 6f 72 28 69 3d 31 3b 20 66 74 73 49 64 43 68 61  or(i=1; ftsIdCha
32525 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a  r(z[i]); i++){}.
32526 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
32527 20 3d 20 54 4f 4b 45 4e 5f 49 44 3b 0a 20 20 20   = TOKEN_ID;.   
32528 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
32529 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54   }.  }.  *tokenT
3252a 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 50 55 4e 43  ype = TOKEN_PUNC
3252b 54 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  T;.  return 1;.}
3252c 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 6f 6b 65 6e 20  ../*.** A token 
3252d 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 61  extracted from a
3252e 20 73 74 72 69 6e 67 20 69 73 20 61 6e 20 69 6e   string is an in
3252f 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
32530 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
32531 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ture..*/.typedef
32532 20 73 74 72 75 63 74 20 46 74 73 54 6f 6b 65 6e   struct FtsToken
32533 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20   {.  const char 
32534 2a 7a 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  *z;       /* Poi
32535 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e 20 74 65  nter to token te
32536 78 74 2e 20 20 4e 6f 74 20 27 5c 30 30 30 27 20  xt.  Not '\000' 
32537 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 20 20  terminated */.  
32538 73 68 6f 72 74 20 69 6e 74 20 6e 3b 20 20 20 20  short int n;    
32539 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
3253a 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74  f the token text
3253b 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 7d 20   in bytes. */.} 
3253c 46 74 73 54 6f 6b 65 6e 3b 0a 0a 2f 2a 0a 2a 2a  FtsToken;../*.**
3253d 20 47 69 76 65 6e 20 61 20 69 6e 70 75 74 20 73   Given a input s
3253e 74 72 69 6e 67 20 28 77 68 69 63 68 20 69 73 20  tring (which is 
3253f 72 65 61 6c 6c 79 20 6f 6e 65 20 6f 66 20 74 68  really one of th
32540 65 20 61 72 67 76 5b 5d 20 70 61 72 61 6d 65 74  e argv[] paramet
32541 65 72 73 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e  ers.** passed in
32542 74 6f 20 78 43 6f 6e 6e 65 63 74 20 6f 72 20 78  to xConnect or x
32543 43 72 65 61 74 65 29 20 73 70 6c 69 74 20 74 68  Create) split th
32544 65 20 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f  e string up into
32545 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20 52 65 74 75   tokens..** Retu
32546 72 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  rn an array of p
32547 6f 69 6e 74 65 72 73 20 74 6f 20 27 5c 30 30 30  ointers to '\000
32548 27 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  ' terminated str
32549 69 6e 67 73 2c 20 6f 6e 65 20 73 74 72 69 6e 67  ings, one string
3254a 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 6e 6f 6e  .** for each non
3254b 2d 77 68 69 74 65 73 70 61 63 65 20 74 6f 6b 65  -whitespace toke
3254c 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  n..**.** The ret
3254d 75 72 6e 65 64 20 61 72 72 61 79 20 69 73 20 74  urned array is t
3254e 65 72 6d 69 6e 61 74 65 64 20 62 79 20 61 20 73  erminated by a s
3254f 69 6e 67 6c 65 20 4e 55 4c 4c 20 70 6f 69 6e 74  ingle NULL point
32550 65 72 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20  er..**.** Space 
32551 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
32552 72 6e 65 64 20 61 72 72 61 79 20 69 73 20 6f 62  rned array is ob
32553 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 20 73 69  tained from a si
32554 6e 67 6c 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 61  ngle.** malloc a
32555 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65  nd should be fre
32556 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 74 68  ed by passing th
32557 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 74  e return value t
32558 6f 20 66 72 65 65 28 29 2e 0a 2a 2a 20 54 68 65  o free()..** The
32559 20 69 6e 64 69 76 69 64 75 61 6c 20 73 74 72 69   individual stri
3255a 6e 67 73 20 77 69 74 68 69 6e 20 74 68 65 20 74  ngs within the t
3255b 6f 6b 65 6e 20 6c 69 73 74 20 61 72 65 20 61 6c  oken list are al
3255c 6c 20 61 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74  l a part of.** t
3255d 68 65 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79  he single memory
3255e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20   allocation and 
3255f 77 69 6c 6c 20 61 6c 6c 20 62 65 20 66 72 65 65  will all be free
32560 64 20 61 74 20 6f 6e 63 65 2e 0a 2a 2f 0a 73 74  d at once..*/.st
32561 61 74 69 63 20 63 68 61 72 20 2a 2a 74 6f 6b 65  atic char **toke
32562 6e 69 7a 65 53 74 72 69 6e 67 28 63 6f 6e 73 74  nizeString(const
32563 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70   char *z, int *p
32564 6e 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 6e  nToken){.  int n
32565 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 46 74 73  Token = 0;.  Fts
32566 54 6f 6b 65 6e 20 2a 61 54 6f 6b 65 6e 20 3d 20  Token *aToken = 
32567 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
32568 73 74 72 6c 65 6e 28 7a 29 20 2a 20 73 69 7a 65  strlen(z) * size
32569 6f 66 28 61 54 6f 6b 65 6e 5b 30 5d 29 20 29 3b  of(aToken[0]) );
3256a 0a 20 20 69 6e 74 20 6e 20 3d 20 31 3b 0a 20 20  .  int n = 1;.  
3256b 69 6e 74 20 65 2c 20 69 3b 0a 20 20 69 6e 74 20  int e, i;.  int 
3256c 74 6f 74 61 6c 53 69 7a 65 20 3d 20 30 3b 0a 20  totalSize = 0;. 
3256d 20 63 68 61 72 20 2a 2a 61 7a 54 6f 6b 65 6e 3b   char **azToken;
3256e 0a 20 20 63 68 61 72 20 2a 7a 43 6f 70 79 3b 0a  .  char *zCopy;.
3256f 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 29 7b 0a    while( n>0 ){.
32570 20 20 20 20 6e 20 3d 20 66 74 73 47 65 74 54 6f      n = ftsGetTo
32571 6b 65 6e 28 7a 2c 20 26 65 29 3b 0a 20 20 20 20  ken(z, &e);.    
32572 69 66 28 20 65 21 3d 54 4f 4b 45 4e 5f 53 50 41  if( e!=TOKEN_SPA
32573 43 45 20 29 7b 0a 20 20 20 20 20 20 61 54 6f 6b  CE ){.      aTok
32574 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e 7a 20 3d 20 7a  en[nToken].z = z
32575 3b 0a 20 20 20 20 20 20 61 54 6f 6b 65 6e 5b 6e  ;.      aToken[n
32576 54 6f 6b 65 6e 5d 2e 6e 20 3d 20 6e 3b 0a 20 20  Token].n = n;.  
32577 20 20 20 20 6e 54 6f 6b 65 6e 2b 2b 3b 0a 20 20      nToken++;.  
32578 20 20 20 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d      totalSize +=
32579 20 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   n+1;.    }.    
3257a 7a 20 2b 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 7a  z += n;.  }.  az
3257b 54 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 2a 29  Token = (char**)
3257c 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
3257d 6e 54 6f 6b 65 6e 2a 73 69 7a 65 6f 66 28 63 68  nToken*sizeof(ch
3257e 61 72 2a 29 20 2b 20 74 6f 74 61 6c 53 69 7a 65  ar*) + totalSize
3257f 20 29 3b 0a 20 20 7a 43 6f 70 79 20 3d 20 28 63   );.  zCopy = (c
32580 68 61 72 2a 29 26 61 7a 54 6f 6b 65 6e 5b 6e 54  har*)&azToken[nT
32581 6f 6b 65 6e 5d 3b 0a 20 20 6e 54 6f 6b 65 6e 2d  oken];.  nToken-
32582 2d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  -;.  for(i=0; i<
32583 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20  nToken; i++){.  
32584 20 20 61 7a 54 6f 6b 65 6e 5b 69 5d 20 3d 20 7a    azToken[i] = z
32585 43 6f 70 79 3b 0a 20 20 20 20 6e 20 3d 20 61 54  Copy;.    n = aT
32586 6f 6b 65 6e 5b 69 5d 2e 6e 3b 0a 20 20 20 20 6d  oken[i].n;.    m
32587 65 6d 63 70 79 28 7a 43 6f 70 79 2c 20 61 54 6f  emcpy(zCopy, aTo
32588 6b 65 6e 5b 69 5d 2e 7a 2c 20 6e 29 3b 0a 20 20  ken[i].z, n);.  
32589 20 20 7a 43 6f 70 79 5b 6e 5d 20 3d 20 30 3b 0a    zCopy[n] = 0;.
3258a 20 20 20 20 7a 43 6f 70 79 20 2b 3d 20 6e 2b 31      zCopy += n+1
3258b 3b 0a 20 20 7d 0a 20 20 61 7a 54 6f 6b 65 6e 5b  ;.  }.  azToken[
3258c 6e 54 6f 6b 65 6e 5d 20 3d 20 30 3b 0a 20 20 73  nToken] = 0;.  s
3258d 71 6c 69 74 65 33 5f 66 72 65 65 28 61 54 6f 6b  qlite3_free(aTok
3258e 65 6e 29 3b 0a 20 20 2a 70 6e 54 6f 6b 65 6e 20  en);.  *pnToken 
3258f 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 72 65 74 75  = nToken;.  retu
32590 72 6e 20 61 7a 54 6f 6b 65 6e 3b 0a 7d 0a 0a 2f  rn azToken;.}../
32591 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20  *.** Convert an 
32592 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64  SQL-style quoted
32593 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e   string into a n
32594 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20  ormal string by 
32595 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20  removing.** the 
32596 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73  quote characters
32597 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f  .  The conversio
32598 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61  n is done in-pla
32599 63 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69  ce.  If the.** i
3259a 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65  nput does not be
3259b 67 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65  gin with a quote
3259c 20 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e   character, then
3259d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
3259e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
3259f 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a  ** Examples:.**.
325a0 2a 2a 20 20 20 20 20 22 61 62 63 22 20 20 20 62  **     "abc"   b
325a1 65 63 6f 6d 65 73 20 20 20 61 62 63 0a 2a 2a 20  ecomes   abc.** 
325a2 20 20 20 20 27 78 79 7a 27 20 20 20 62 65 63 6f      'xyz'   beco
325a3 6d 65 73 20 20 20 78 79 7a 0a 2a 2a 20 20 20 20  mes   xyz.**    
325a4 20 5b 70 71 72 5d 20 20 20 62 65 63 6f 6d 65 73   [pqr]   becomes
325a5 20 20 20 70 71 72 0a 2a 2a 20 20 20 20 20 60 6d     pqr.**     `m
325a6 6e 6f 60 20 20 20 62 65 63 6f 6d 65 73 20 20 20  no`   becomes   
325a7 6d 6e 6f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  mno.*/.static vo
325a8 69 64 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67  id dequoteString
325a9 28 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74  (char *z){.  int
325aa 20 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c   quote;.  int i,
325ab 20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29   j;.  if( z==0 )
325ac 20 72 65 74 75 72 6e 3b 0a 20 20 71 75 6f 74 65   return;.  quote
325ad 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63   = z[0];.  switc
325ae 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20 20  h( quote ){.    
325af 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65 61  case '\'':  brea
325b0 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a  k;.    case '"':
325b1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
325b2 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b 3b  se '`':   break;
325b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
325b4 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d  /* For MySQL com
325b5 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20  patibility */.  
325b6 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 75    case '[':   qu
325b7 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 61  ote = ']';  brea
325b8 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71  k;  /* For MS Sq
325b9 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 62  lServer compatib
325ba 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65 66  ility */.    def
325bb 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e 3b  ault:    return;
325bc 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20  .  }.  for(i=1, 
325bd 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b  j=0; z[i]; i++){
325be 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71  .    if( z[i]==q
325bf 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66  uote ){.      if
325c0 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20  ( z[i+1]==quote 
325c1 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b  ){.        z[j++
325c2 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20 20 20 20  ] = quote;.     
325c3 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65     i++;.      }e
325c4 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a  lse{.        z[j
325c5 2b 2b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ++] = 0;.       
325c6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
325c7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
325c8 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a   z[j++] = z[i];.
325c9 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
325ca 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 61 7a 49  ** The input azI
325cb 6e 20 69 73 20 61 20 4e 55 4c 4c 2d 74 65 72 6d  n is a NULL-term
325cc 69 6e 61 74 65 64 20 6c 69 73 74 20 6f 66 20 74  inated list of t
325cd 6f 6b 65 6e 73 2e 20 20 52 65 6d 6f 76 65 20 74  okens.  Remove t
325ce 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65  he first.** toke
325cf 6e 20 61 6e 64 20 61 6c 6c 20 70 75 6e 63 74 75  n and all punctu
325d0 61 74 69 6f 6e 20 74 6f 6b 65 6e 73 2e 20 20 52  ation tokens.  R
325d1 65 6d 6f 76 65 20 74 68 65 20 71 75 6f 74 65 73  emove the quotes
325d2 20 66 72 6f 6d 0a 2a 2a 20 61 72 6f 75 6e 64 20   from.** around 
325d3 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 74  string literal t
325d4 6f 6b 65 6e 73 2e 0a 2a 2a 0a 2a 2a 20 45 78 61  okens..**.** Exa
325d5 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple:.**.**     
325d6 69 6e 70 75 74 3a 20 20 20 20 20 20 74 6f 6b 65  input:      toke
325d7 6e 69 7a 65 20 63 68 69 6e 65 73 65 20 28 20 27  nize chinese ( '
325d8 73 69 6d 70 6c 69 66 65 64 27 20 2c 20 27 6d 69  simplifed' , 'mi
325d9 78 65 64 27 20 29 0a 2a 2a 20 20 20 20 20 6f 75  xed' ).**     ou
325da 74 70 75 74 3a 20 20 20 20 20 63 68 69 6e 65 73  tput:     chines
325db 65 20 73 69 6d 70 6c 69 66 65 64 20 6d 69 78 65  e simplifed mixe
325dc 64 0a 2a 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20  d.**.** Another 
325dd 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  example:.**.**  
325de 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 20 64     input:      d
325df 65 6c 69 6d 69 74 65 72 73 20 28 20 27 5b 27 20  elimiters ( '[' 
325e0 2c 20 27 5d 27 20 2c 20 27 2e 2e 2e 27 20 29 0a  , ']' , '...' ).
325e1 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20  **     output:  
325e2 20 20 20 5b 20 5d 20 2e 2e 2e 0a 2a 2f 0a 73 74     [ ] ....*/.st
325e3 61 74 69 63 20 76 6f 69 64 20 74 6f 6b 65 6e 4c  atic void tokenL
325e4 69 73 74 54 6f 49 64 4c 69 73 74 28 63 68 61 72  istToIdList(char
325e5 20 2a 2a 61 7a 49 6e 29 7b 0a 20 20 69 6e 74 20   **azIn){.  int 
325e6 69 2c 20 6a 3b 0a 20 20 69 66 28 20 61 7a 49 6e  i, j;.  if( azIn
325e7 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c   ){.    for(i=0,
325e8 20 6a 3d 2d 31 3b 20 61 7a 49 6e 5b 69 5d 3b 20   j=-1; azIn[i]; 
325e9 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
325ea 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 61 7a 49  safe_isalnum(azI
325eb 6e 5b 69 5d 5b 30 5d 29 20 7c 7c 20 61 7a 49 6e  n[i][0]) || azIn
325ec 5b 69 5d 5b 31 5d 20 29 7b 0a 20 20 20 20 20 20  [i][1] ){.      
325ed 20 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28    dequoteString(
325ee 61 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 20 20  azIn[i]);.      
325ef 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20    if( j>=0 ){.  
325f0 20 20 20 20 20 20 20 20 61 7a 49 6e 5b 6a 5d 20          azIn[j] 
325f1 3d 20 61 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 20  = azIn[i];.     
325f2 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6a 2b 2b     }.        j++
325f3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
325f4 20 20 20 20 61 7a 49 6e 5b 6a 5d 20 3d 20 30 3b      azIn[j] = 0;
325f5 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46  .  }.}.../*.** F
325f6 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 61 6c  ind the first al
325f7 70 68 61 6e 75 6d 65 72 69 63 20 74 6f 6b 65 6e  phanumeric token
325f8 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20 7a   in the string z
325f9 49 6e 2e 20 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e  In.  Null-termin
325fa 61 74 65 0a 2a 2a 20 74 68 69 73 20 74 6f 6b 65  ate.** this toke
325fb 6e 2e 20 20 52 65 6d 6f 76 65 20 61 6e 79 20 71  n.  Remove any q
325fc 75 6f 74 61 74 69 6f 6e 20 6d 61 72 6b 73 2e 20  uotation marks. 
325fd 20 41 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f   And return a po
325fe 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20  inter to.** the 
325ff 72 65 73 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69  result..*/.stati
32600 63 20 63 68 61 72 20 2a 66 69 72 73 74 54 6f 6b  c char *firstTok
32601 65 6e 28 63 68 61 72 20 2a 7a 49 6e 2c 20 63 68  en(char *zIn, ch
32602 61 72 20 2a 2a 70 7a 54 61 69 6c 29 7b 0a 20 20  ar **pzTail){.  
32603 69 6e 74 20 6e 2c 20 74 74 79 70 65 3b 0a 20 20  int n, ttype;.  
32604 77 68 69 6c 65 28 31 29 7b 0a 20 20 20 20 6e 20  while(1){.    n 
32605 3d 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 7a 49  = ftsGetToken(zI
32606 6e 2c 20 26 74 74 79 70 65 29 3b 0a 20 20 20 20  n, &ttype);.    
32607 69 66 28 20 74 74 79 70 65 3d 3d 54 4f 4b 45 4e  if( ttype==TOKEN
32608 5f 53 50 41 43 45 20 29 7b 0a 20 20 20 20 20 20  _SPACE ){.      
32609 7a 49 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 7d 65  zIn += n;.    }e
3260a 6c 73 65 20 69 66 28 20 74 74 79 70 65 3d 3d 54  lse if( ttype==T
3260b 4f 4b 45 4e 5f 45 4f 46 20 29 7b 0a 20 20 20 20  OKEN_EOF ){.    
3260c 20 20 2a 70 7a 54 61 69 6c 20 3d 20 7a 49 6e 3b    *pzTail = zIn;
3260d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
3260e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3260f 20 20 7a 49 6e 5b 6e 5d 20 3d 20 30 3b 0a 20 20    zIn[n] = 0;.  
32610 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 26 7a      *pzTail = &z
32611 49 6e 5b 31 5d 3b 0a 20 20 20 20 20 20 64 65 71  In[1];.      deq
32612 75 6f 74 65 53 74 72 69 6e 67 28 7a 49 6e 29 3b  uoteString(zIn);
32613 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 7a 49  .      return zI
32614 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  n;.    }.  }.  /
32615 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 7d 0a  *NOTREACHED*/.}.
32616 0a 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  ./* Return true 
32617 69 66 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  if....**.**   * 
32618 20 73 20 62 65 67 69 6e 73 20 77 69 74 68 20 74   s begins with t
32619 68 65 20 73 74 72 69 6e 67 20 74 2c 20 69 67 6e  he string t, ign
3261a 6f 72 69 6e 67 20 63 61 73 65 0a 2a 2a 20 20 20  oring case.**   
3261b 2a 20 20 73 20 69 73 20 6c 6f 6e 67 65 72 20 74  *  s is longer t
3261c 68 61 6e 20 74 0a 2a 2a 20 20 20 2a 20 20 54 68  han t.**   *  Th
3261d 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65  e first characte
3261e 72 20 6f 66 20 73 20 62 65 79 6f 6e 64 20 74 20  r of s beyond t 
3261f 69 73 20 6e 6f 74 20 61 20 61 6c 70 68 61 6e 75  is not a alphanu
32620 6d 65 72 69 63 0a 2a 2a 20 0a 2a 2a 20 49 67 6e  meric.** .** Ign
32621 6f 72 65 20 6c 65 61 64 69 6e 67 20 73 70 61 63  ore leading spac
32622 65 20 69 6e 20 2a 73 2e 0a 2a 2a 0a 2a 2a 20 54  e in *s..**.** T
32623 6f 20 70 75 74 20 69 74 20 61 6e 6f 74 68 65 72  o put it another
32624 20 77 61 79 2c 20 72 65 74 75 72 6e 20 74 72 75   way, return tru
32625 65 20 69 66 20 74 68 65 20 66 69 72 73 74 20 74  e if the first t
32626 6f 6b 65 6e 20 6f 66 0a 2a 2a 20 73 5b 5d 20 69  oken of.** s[] i
32627 73 20 74 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  s t[]..*/.static
32628 20 69 6e 74 20 73 74 61 72 74 73 57 69 74 68 28   int startsWith(
32629 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 2c 20 63  const char *s, c
3262a 6f 6e 73 74 20 63 68 61 72 20 2a 74 29 7b 0a 20  onst char *t){. 
3262b 20 77 68 69 6c 65 28 20 73 61 66 65 5f 69 73 73   while( safe_iss
3262c 70 61 63 65 28 2a 73 29 20 29 7b 20 73 2b 2b 3b  pace(*s) ){ s++;
3262d 20 7d 0a 20 20 77 68 69 6c 65 28 20 2a 74 20 29   }.  while( *t )
3262e 7b 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 74  {.    if( safe_t
3262f 6f 6c 6f 77 65 72 28 2a 73 2b 2b 29 21 3d 73 61  olower(*s++)!=sa
32630 66 65 5f 74 6f 6c 6f 77 65 72 28 2a 74 2b 2b 29  fe_tolower(*t++)
32631 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d   ) return 0;.  }
32632 0a 20 20 72 65 74 75 72 6e 20 2a 73 21 3d 27 5f  .  return *s!='_
32633 27 20 26 26 20 21 73 61 66 65 5f 69 73 61 6c 6e  ' && !safe_isaln
32634 75 6d 28 2a 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  um(*s);.}../*.**
32635 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
32636 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 64  this structure d
32637 65 66 69 6e 65 73 20 74 68 65 20 22 73 70 65 63  efines the "spec
32638 22 20 6f 66 20 61 0a 2a 2a 20 66 75 6c 6c 20 74  " of a.** full t
32639 65 78 74 20 69 6e 64 65 78 2e 20 20 54 68 69 73  ext index.  This
3263a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 6f   structure is po
3263b 70 75 6c 61 74 65 64 20 62 79 20 70 61 72 73 65  pulated by parse
3263c 53 70 65 63 0a 2a 2a 20 61 6e 64 20 75 73 65 20  Spec.** and use 
3263d 62 79 20 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65  by fulltextConne
3263e 63 74 20 61 6e 64 20 66 75 6c 6c 74 65 78 74 43  ct and fulltextC
3263f 72 65 61 74 65 2e 0a 2a 2f 0a 74 79 70 65 64 65  reate..*/.typede
32640 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 53 70  f struct TableSp
32641 65 63 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ec {.  const cha
32642 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20  r *zDb;         
32643 2f 2a 20 4c 6f 67 69 63 61 6c 20 64 61 74 61 62  /* Logical datab
32644 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f  ase name */.  co
32645 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
32646 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
32647 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20  f the full-text 
32648 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e  index */.  int n
32649 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
3264a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3264b 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69   columns to be i
3264c 6e 64 65 78 65 64 20 2a 2f 0a 20 20 63 68 61 72  ndexed */.  char
3264d 20 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20   **azColumn;    
3264e 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c       /* Original
3264f 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e   names of column
32650 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  s to be indexed 
32651 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  */.  char **azCo
32652 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b 20 20 2f 2a  ntentColumn;  /*
32653 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f   Column names fo
32654 72 20 25 5f 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20  r %_content */. 
32655 20 63 68 61 72 20 2a 2a 61 7a 54 6f 6b 65 6e 69   char **azTokeni
32656 7a 65 72 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d  zer;      /* Nam
32657 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 20 61  e of tokenizer a
32658 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73  nd its arguments
32659 20 2a 2f 0a 7d 20 54 61 62 6c 65 53 70 65 63 3b   */.} TableSpec;
3265a 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d 20  ../*.** Reclaim 
3265b 61 6c 6c 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  all of the memor
3265c 79 20 75 73 65 64 20 62 79 20 61 20 54 61 62 6c  y used by a Tabl
3265d 65 53 70 65 63 0a 2a 2f 0a 73 74 61 74 69 63 20  eSpec.*/.static 
3265e 76 6f 69 64 20 63 6c 65 61 72 54 61 62 6c 65 53  void clearTableS
3265f 70 65 63 28 54 61 62 6c 65 53 70 65 63 20 2a 70  pec(TableSpec *p
32660 29 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  ) {.  sqlite3_fr
32661 65 65 28 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 29 3b  ee(p->azColumn);
32662 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
32663 70 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  p->azContentColu
32664 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  mn);.  sqlite3_f
32665 72 65 65 28 70 2d 3e 61 7a 54 6f 6b 65 6e 69 7a  ree(p->azTokeniz
32666 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 61 72 73 65  er);.}../* Parse
32667 20 61 20 43 52 45 41 54 45 20 56 49 52 54 55 41   a CREATE VIRTUA
32668 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  L TABLE statemen
32669 74 2c 20 77 68 69 63 68 20 6c 6f 6f 6b 73 20 6c  t, which looks l
3266a 69 6b 65 20 74 68 69 73 3a 0a 20 2a 0a 20 2a 20  ike this:. *. * 
3266b 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
3266c 41 42 4c 45 20 65 6d 61 69 6c 0a 20 2a 20 20 20  ABLE email. *   
3266d 20 20 20 20 20 55 53 49 4e 47 20 66 74 73 33 28       USING fts3(
3266e 73 75 62 6a 65 63 74 2c 20 62 6f 64 79 2c 20 74  subject, body, t
3266f 6f 6b 65 6e 69 7a 65 20 6d 79 74 6f 6b 65 6e 69  okenize mytokeni
32670 7a 65 72 28 6d 79 61 72 67 29 29 0a 20 2a 0a 20  zer(myarg)). *. 
32671 2a 20 57 65 20 72 65 74 75 72 6e 20 70 61 72 73  * We return pars
32672 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ed information i
32673 6e 20 61 20 54 61 62 6c 65 53 70 65 63 20 73 74  n a TableSpec st
32674 72 75 63 74 75 72 65 2e 0a 20 2a 20 0a 20 2a 2f  ructure.. * . */
32675 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73  .static int pars
32676 65 53 70 65 63 28 54 61 62 6c 65 53 70 65 63 20  eSpec(TableSpec 
32677 2a 70 53 70 65 63 2c 20 69 6e 74 20 61 72 67 63  *pSpec, int argc
32678 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  , const char *co
32679 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20  nst*argv,.      
3267a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
3267b 68 61 72 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69  har**pzErr){.  i
3267c 6e 74 20 69 2c 20 6e 3b 0a 20 20 63 68 61 72 20  nt i, n;.  char 
3267d 2a 7a 2c 20 2a 7a 44 75 6d 6d 79 3b 0a 20 20 63  *z, *zDummy;.  c
3267e 68 61 72 20 2a 2a 61 7a 41 72 67 3b 0a 20 20 63  har **azArg;.  c
3267f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65  onst char *zToke
32680 6e 69 7a 65 72 20 3d 20 30 3b 20 20 20 20 2f 2a  nizer = 0;    /*
32681 20 61 72 67 76 5b 5d 20 65 6e 74 72 79 20 64 65   argv[] entry de
32682 73 63 72 69 62 69 6e 67 20 74 68 65 20 74 6f 6b  scribing the tok
32683 65 6e 69 7a 65 72 20 2a 2f 0a 0a 20 20 61 73 73  enizer */..  ass
32684 65 72 74 28 20 61 72 67 63 3e 3d 33 20 29 3b 0a  ert( argc>=3 );.
32685 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 74    /* Current int
32686 65 72 66 61 63 65 3a 0a 20 20 2a 2a 20 61 72 67  erface:.  ** arg
32687 76 5b 30 5d 20 2d 20 6d 6f 64 75 6c 65 20 6e 61  v[0] - module na
32688 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 31 5d 20  me.  ** argv[1] 
32689 2d 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a  - database name.
3268a 20 20 2a 2a 20 61 72 67 76 5b 32 5d 20 2d 20 74    ** argv[2] - t
3268b 61 62 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61  able name.  ** a
3268c 72 67 76 5b 33 2e 2e 5d 20 2d 20 63 6f 6c 75 6d  rgv[3..] - colum
3268d 6e 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 66  ns, optionally f
3268e 6f 6c 6c 6f 77 65 64 20 62 79 20 74 6f 6b 65 6e  ollowed by token
3268f 69 7a 65 72 20 73 70 65 63 69 66 69 63 61 74 69  izer specificati
32690 6f 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  on.  **         
32691 20 20 20 20 61 6e 64 20 73 6e 69 70 70 65 74 20      and snippet 
32692 64 65 6c 69 6d 69 74 65 72 73 20 73 70 65 63 69  delimiters speci
32693 66 69 63 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a  fication..  */..
32694 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79    /* Make a copy
32695 20 6f 66 20 74 68 65 20 63 6f 6d 70 6c 65 74 65   of the complete
32696 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72 61 79 20   argv[][] array 
32697 69 6e 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f  in a single allo
32698 63 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65  cation..  ** The
32699 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72 61 79 20   argv[][] array 
3269a 69 73 20 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64  is read-only and
3269b 20 74 72 61 6e 73 69 65 6e 74 2e 20 20 57 65 20   transient.  We 
3269c 63 61 6e 20 77 72 69 74 65 20 74 6f 20 74 68 65  can write to the
3269d 0a 20 20 2a 2a 20 63 6f 70 79 20 69 6e 20 6f 72  .  ** copy in or
3269e 64 65 72 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  der to modify th
3269f 69 6e 67 73 20 61 6e 64 20 74 68 65 20 63 6f 70  ings and the cop
326a0 79 20 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e  y is persistent.
326a1 0a 20 20 2a 2f 0a 20 20 43 4c 45 41 52 28 70 53  .  */.  CLEAR(pS
326a2 70 65 63 29 3b 0a 20 20 66 6f 72 28 69 3d 6e 3d  pec);.  for(i=n=
326a3 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b  0; i<argc; i++){
326a4 0a 20 20 20 20 6e 20 2b 3d 20 73 74 72 6c 65 6e  .    n += strlen
326a5 28 61 72 67 76 5b 69 5d 29 20 2b 20 31 3b 0a 20  (argv[i]) + 1;. 
326a6 20 7d 0a 20 20 61 7a 41 72 67 20 3d 20 73 71 6c   }.  azArg = sql
326a7 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
326a8 65 6f 66 28 63 68 61 72 2a 29 2a 61 72 67 63 20  eof(char*)*argc 
326a9 2b 20 6e 20 29 3b 0a 20 20 69 66 28 20 61 7a 41  + n );.  if( azA
326aa 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  rg==0 ){.    ret
326ab 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
326ac 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61  ;.  }.  z = (cha
326ad 72 2a 29 26 61 7a 41 72 67 5b 61 72 67 63 5d 3b  r*)&azArg[argc];
326ae 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72  .  for(i=0; i<ar
326af 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 7a  gc; i++){.    az
326b0 41 72 67 5b 69 5d 20 3d 20 7a 3b 0a 20 20 20 20  Arg[i] = z;.    
326b1 73 74 72 63 70 79 28 7a 2c 20 61 72 67 76 5b 69  strcpy(z, argv[i
326b2 5d 29 3b 0a 20 20 20 20 7a 20 2b 3d 20 73 74 72  ]);.    z += str
326b3 6c 65 6e 28 7a 29 2b 31 3b 0a 20 20 7d 0a 0a 20  len(z)+1;.  }.. 
326b4 20 2f 2a 20 49 64 65 6e 74 69 66 79 20 74 68 65   /* Identify the
326b5 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e   column names an
326b6 64 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  d the tokenizer 
326b7 61 6e 64 20 64 65 6c 69 6d 69 74 65 72 20 61 72  and delimiter ar
326b8 67 75 6d 65 6e 74 73 0a 20 20 2a 2a 20 69 6e 20  guments.  ** in 
326b9 74 68 65 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72  the argv[][] arr
326ba 61 79 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63  ay..  */.  pSpec
326bb 2d 3e 7a 44 62 20 3d 20 61 7a 41 72 67 5b 31 5d  ->zDb = azArg[1]
326bc 3b 0a 20 20 70 53 70 65 63 2d 3e 7a 4e 61 6d 65  ;.  pSpec->zName
326bd 20 3d 20 61 7a 41 72 67 5b 32 5d 3b 0a 20 20 70   = azArg[2];.  p
326be 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20  Spec->nColumn = 
326bf 30 3b 0a 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f  0;.  pSpec->azCo
326c0 6c 75 6d 6e 20 3d 20 61 7a 41 72 67 3b 0a 20 20  lumn = azArg;.  
326c1 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 22 74 6f  zTokenizer = "to
326c2 6b 65 6e 69 7a 65 20 73 69 6d 70 6c 65 22 3b 0a  kenize simple";.
326c3 20 20 66 6f 72 28 69 3d 33 3b 20 69 3c 61 72 67    for(i=3; i<arg
326c4 63 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69 66 28  c; ++i){.    if(
326c5 20 73 74 61 72 74 73 57 69 74 68 28 61 7a 41 72   startsWith(azAr
326c6 67 5b 69 5d 2c 22 74 6f 6b 65 6e 69 7a 65 22 29  g[i],"tokenize")
326c7 20 29 7b 0a 20 20 20 20 20 20 7a 54 6f 6b 65 6e   ){.      zToken
326c8 69 7a 65 72 20 3d 20 61 7a 41 72 67 5b 69 5d 3b  izer = azArg[i];
326c9 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
326ca 20 20 7a 20 3d 20 61 7a 41 72 67 5b 70 53 70 65    z = azArg[pSpe
326cb 63 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 66 69  c->nColumn] = fi
326cc 72 73 74 54 6f 6b 65 6e 28 61 7a 41 72 67 5b 69  rstToken(azArg[i
326cd 5d 2c 20 26 7a 44 75 6d 6d 79 29 3b 0a 20 20 20  ], &zDummy);.   
326ce 20 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d     pSpec->nColum
326cf 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n++;.    }.  }. 
326d0 20 69 66 28 20 70 53 70 65 63 2d 3e 6e 43 6f 6c   if( pSpec->nCol
326d1 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 7a  umn==0 ){.    az
326d2 41 72 67 5b 30 5d 20 3d 20 22 63 6f 6e 74 65 6e  Arg[0] = "conten
326d3 74 22 3b 0a 20 20 20 20 70 53 70 65 63 2d 3e 6e  t";.    pSpec->n
326d4 43 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 20 20 7d 0a  Column = 1;.  }.
326d5 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6f 6e 73 74  .  /*.  ** Const
326d6 72 75 63 74 20 74 68 65 20 6c 69 73 74 20 6f 66  ruct the list of
326d7 20 63 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 20   content column 
326d8 6e 61 6d 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  names..  **.  **
326d9 20 45 61 63 68 20 63 6f 6e 74 65 6e 74 20 63 6f   Each content co
326da 6c 75 6d 6e 20 6e 61 6d 65 20 77 69 6c 6c 20 62  lumn name will b
326db 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 63 4e  e of the form cN
326dc 4e 41 41 41 41 0a 20 20 2a 2a 20 77 68 65 72 65  NAAAA.  ** where
326dd 20 4e 4e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d   NN is the colum
326de 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 41 41 41  n number and AAA
326df 41 20 69 73 20 74 68 65 20 73 61 6e 69 74 69 7a  A is the sanitiz
326e0 65 64 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e  ed.  ** column n
326e1 61 6d 65 2e 20 20 22 73 61 6e 69 74 69 7a 65 64  ame.  "sanitized
326e2 22 20 6d 65 61 6e 73 20 74 68 61 74 20 73 70 65  " means that spe
326e3 63 69 61 6c 20 63 68 61 72 61 63 74 65 72 73 20  cial characters 
326e4 61 72 65 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 74  are.  ** convert
326e5 65 64 20 74 6f 20 22 5f 22 2e 20 20 54 68 65 20  ed to "_".  The 
326e6 63 4e 4e 20 70 72 65 66 69 78 20 67 75 61 72 61  cNN prefix guara
326e7 6e 74 65 65 73 20 74 68 61 74 20 61 6c 6c 20 63  ntees that all c
326e8 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6e 61 6d 65 73  olumn.  ** names
326e9 20 61 72 65 20 75 6e 69 71 75 65 2e 0a 20 20 2a   are unique..  *
326ea 2a 0a 20 20 2a 2a 20 54 68 65 20 41 41 41 41 20  *.  ** The AAAA 
326eb 73 75 66 66 69 78 20 69 73 20 6e 6f 74 20 73 74  suffix is not st
326ec 72 69 63 74 6c 79 20 6e 65 63 65 73 73 61 72 79  rictly necessary
326ed 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65  .  It is include
326ee 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 63  d.  ** for the c
326ef 6f 6e 76 65 6e 69 65 6e 63 65 20 6f 66 20 70 65  onvenience of pe
326f0 6f 70 6c 65 20 77 68 6f 20 6d 69 67 68 74 20 65  ople who might e
326f1 78 61 6d 69 6e 65 20 74 68 65 20 67 65 6e 65 72  xamine the gener
326f2 61 74 65 64 0a 20 20 2a 2a 20 25 5f 63 6f 6e 74  ated.  ** %_cont
326f3 65 6e 74 20 74 61 62 6c 65 20 61 6e 64 20 77 6f  ent table and wo
326f4 6e 64 65 72 20 77 68 61 74 20 74 68 65 20 63 6f  nder what the co
326f5 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 66  lumns are used f
326f6 6f 72 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63  or..  */.  pSpec
326f7 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
326f8 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  n = sqlite3_mall
326f9 6f 63 28 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  oc( pSpec->nColu
326fa 6d 6e 20 2a 20 73 69 7a 65 6f 66 28 63 68 61 72  mn * sizeof(char
326fb 20 2a 29 20 29 3b 0a 20 20 69 66 28 20 70 53 70   *) );.  if( pSp
326fc 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  ec->azContentCol
326fd 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6c  umn==0 ){.    cl
326fe 65 61 72 54 61 62 6c 65 53 70 65 63 28 70 53 70  earTableSpec(pSp
326ff 65 63 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ec);.    return 
32700 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
32701 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
32702 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  Spec->nColumn; i
32703 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 70  ++){.    char *p
32704 3b 0a 20 20 20 20 70 53 70 65 63 2d 3e 61 7a 43  ;.    pSpec->azC
32705 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 20  ontentColumn[i] 
32706 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
32707 66 28 22 63 25 64 25 73 22 2c 20 69 2c 20 61 7a  f("c%d%s", i, az
32708 41 72 67 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72  Arg[i]);.    for
32709 20 28 70 20 3d 20 70 53 70 65 63 2d 3e 61 7a 43   (p = pSpec->azC
3270a 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 3b  ontentColumn[i];
3270b 20 2a 70 20 3b 20 2b 2b 70 29 20 7b 0a 20 20 20   *p ; ++p) {.   
3270c 20 20 20 69 66 28 20 21 73 61 66 65 5f 69 73 61     if( !safe_isa
3270d 6c 6e 75 6d 28 2a 70 29 20 29 20 2a 70 20 3d 20  lnum(*p) ) *p = 
3270e 27 5f 27 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  '_';.    }.  }..
3270f 20 20 2f 2a 0a 20 20 2a 2a 20 50 61 72 73 65 20    /*.  ** Parse 
32710 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70  the tokenizer sp
32711 65 63 69 66 69 63 61 74 69 6f 6e 20 73 74 72 69  ecification stri
32712 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63  ng..  */.  pSpec
32713 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  ->azTokenizer = 
32714 74 6f 6b 65 6e 69 7a 65 53 74 72 69 6e 67 28 7a  tokenizeString(z
32715 54 6f 6b 65 6e 69 7a 65 72 2c 20 26 6e 29 3b 0a  Tokenizer, &n);.
32716 20 20 74 6f 6b 65 6e 4c 69 73 74 54 6f 49 64 4c    tokenListToIdL
32717 69 73 74 28 70 53 70 65 63 2d 3e 61 7a 54 6f 6b  ist(pSpec->azTok
32718 65 6e 69 7a 65 72 29 3b 0a 0a 20 20 72 65 74 75  enizer);..  retu
32719 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
3271a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
3271b 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
3271c 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65  tatement that de
3271d 73 63 72 69 62 65 73 20 74 68 65 20 73 63 68 65  scribes the sche
3271e 6d 61 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72  ma of.** the vir
3271f 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 52 65 74  tual table.  Ret
32720 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
32721 20 74 68 69 73 20 73 63 68 65 6d 61 20 73 74 72   this schema str
32722 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  ing..**.** Space
32723 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
32724 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  m sqlite3_mprint
32725 66 28 29 20 61 6e 64 20 73 68 6f 75 6c 64 20 62  f() and should b
32726 65 20 66 72 65 65 64 0a 2a 2a 20 75 73 69 6e 67  e freed.** using
32727 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e   sqlite3_free().
32728 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
32729 2a 66 75 6c 6c 74 65 78 74 53 63 68 65 6d 61 28  *fulltextSchema(
3272a 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20  .  int nColumn, 
3272b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3272c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
3272d 6c 75 6d 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  lumns */.  const
3272e 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 20 61 7a   char *const* az
3272f 43 6f 6c 75 6d 6e 2c 20 20 2f 2a 20 4c 69 73 74  Column,  /* List
32730 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20   of columns */. 
32731 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
32732 62 6c 65 4e 61 6d 65 20 20 20 20 20 20 20 20 2f  bleName        /
32733 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
32734 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ble */.){.  int 
32735 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 63 68 65  i;.  char *zSche
32736 6d 61 2c 20 2a 7a 4e 65 78 74 3b 0a 20 20 63 6f  ma, *zNext;.  co
32737 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 20 3d  nst char *zSep =
32738 20 22 28 22 3b 0a 20 20 7a 53 63 68 65 6d 61 20   "(";.  zSchema 
32739 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
3273a 66 28 22 43 52 45 41 54 45 20 54 41 42 4c 45 20  f("CREATE TABLE 
3273b 78 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  x");.  for(i=0; 
3273c 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  i<nColumn; i++){
3273d 0a 20 20 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c  .    zNext = sql
3273e 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
3273f 25 73 25 51 22 2c 20 7a 53 63 68 65 6d 61 2c 20  %s%Q", zSchema, 
32740 7a 53 65 70 2c 20 61 7a 43 6f 6c 75 6d 6e 5b 69  zSep, azColumn[i
32741 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ]);.    sqlite3_
32742 66 72 65 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20  free(zSchema);. 
32743 20 20 20 7a 53 63 68 65 6d 61 20 3d 20 7a 4e 65     zSchema = zNe
32744 78 74 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22  xt;.    zSep = "
32745 2c 22 3b 0a 20 20 7d 0a 20 20 7a 4e 65 78 74 20  ,";.  }.  zNext 
32746 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
32747 66 28 22 25 73 2c 25 51 20 48 49 44 44 45 4e 22  f("%s,%Q HIDDEN"
32748 2c 20 7a 53 63 68 65 6d 61 2c 20 7a 54 61 62 6c  , zSchema, zTabl
32749 65 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  eName);.  sqlite
3274a 33 5f 66 72 65 65 28 7a 53 63 68 65 6d 61 29 3b  3_free(zSchema);
3274b 0a 20 20 7a 53 63 68 65 6d 61 20 3d 20 7a 4e 65  .  zSchema = zNe
3274c 78 74 3b 0a 20 20 7a 4e 65 78 74 20 3d 20 73 71  xt;.  zNext = sq
3274d 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
3274e 73 2c 64 6f 63 69 64 20 48 49 44 44 45 4e 29 22  s,docid HIDDEN)"
3274f 2c 20 7a 53 63 68 65 6d 61 29 3b 0a 20 20 73 71  , zSchema);.  sq
32750 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68 65  lite3_free(zSche
32751 6d 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 4e  ma);.  return zN
32752 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75  ext;.}../*.** Bu
32753 69 6c 64 20 61 20 6e 65 77 20 73 71 6c 69 74 65  ild a new sqlite
32754 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
32755 20 74 68 61 74 20 77 69 6c 6c 20 64 65 73 63 72   that will descr
32756 69 62 65 20 74 68 65 0a 2a 2a 20 66 75 6c 6c 74  ibe the.** fullt
32757 65 78 74 20 69 6e 64 65 78 20 64 65 66 69 6e 65  ext index define
32758 64 20 62 79 20 73 70 65 63 2e 0a 2a 2f 0a 73 74  d by spec..*/.st
32759 61 74 69 63 20 69 6e 74 20 63 6f 6e 73 74 72 75  atic int constru
3275a 63 74 56 74 61 62 28 0a 20 20 73 71 6c 69 74 65  ctVtab(.  sqlite
3275b 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
3275c 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 69 74      /* The SQLit
3275d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
3275e 63 74 69 6f 6e 20 2a 2f 0a 20 20 66 74 73 33 48  ction */.  fts3H
3275f 61 73 68 20 2a 70 48 61 73 68 2c 20 20 20 20 20  ash *pHash,     
32760 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62       /* Hash tab
32761 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f  le containing to
32762 6b 65 6e 69 7a 65 72 73 20 2a 2f 0a 20 20 54 61  kenizers */.  Ta
32763 62 6c 65 53 70 65 63 20 2a 73 70 65 63 2c 20 20  bleSpec *spec,  
32764 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
32765 64 20 73 70 65 63 20 69 6e 66 6f 72 6d 61 74 69  d spec informati
32766 6f 6e 20 66 72 6f 6d 20 70 61 72 73 65 53 70 65  on from parseSpe
32767 63 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  c() */.  sqlite3
32768 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20  _vtab **ppVTab, 
32769 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
3276a 72 65 73 75 6c 74 69 6e 67 20 76 74 61 62 20 73  resulting vtab s
3276b 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f  tructure here */
3276c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 20  .  char **pzErr 
3276d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3276e 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20  Write any error 
3276f 6d 65 73 73 61 67 65 20 68 65 72 65 20 2a 2f 0a  message here */.
32770 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
32771 6e 74 20 6e 3b 0a 20 20 66 75 6c 6c 74 65 78 74  nt n;.  fulltext
32772 5f 76 74 61 62 20 2a 76 20 3d 20 30 3b 0a 20 20  _vtab *v = 0;.  
32773 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
32774 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
32775 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 63 68 61 72  m = NULL;.  char
32776 20 2a 73 63 68 65 6d 61 3b 0a 0a 20 20 63 68 61   *schema;..  cha
32777 72 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b 3b 20 20  r const *zTok;  
32778 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
32779 66 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 75  f tokenizer to u
3277a 73 65 20 66 6f 72 20 74 68 69 73 20 66 74 73 20  se for this fts 
3277b 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
3277c 54 6f 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tok;            
3277d 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
3277e 66 20 7a 54 6f 6b 2c 20 69 6e 63 6c 75 64 69 6e  f zTok, includin
3277f 67 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 6f 72  g nul terminator
32780 20 2a 2f 0a 0a 20 20 76 20 3d 20 28 66 75 6c 6c   */..  v = (full
32781 74 65 78 74 5f 76 74 61 62 20 2a 29 20 73 71 6c  text_vtab *) sql
32782 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
32783 6f 66 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  of(fulltext_vtab
32784 29 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  ));.  if( v==0 )
32785 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
32786 4f 4d 45 4d 3b 0a 20 20 43 4c 45 41 52 28 76 29  OMEM;.  CLEAR(v)
32787 3b 0a 20 20 2f 2a 20 73 71 6c 69 74 65 20 77 69  ;.  /* sqlite wi
32788 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 76 2d  ll initialize v-
32789 3e 62 61 73 65 20 2a 2f 0a 20 20 76 2d 3e 64 62  >base */.  v->db
3278a 20 3d 20 64 62 3b 0a 20 20 76 2d 3e 7a 44 62 20   = db;.  v->zDb 
3278b 3d 20 73 70 65 63 2d 3e 7a 44 62 3b 20 20 20 20  = spec->zDb;    
3278c 20 20 20 2f 2a 20 46 72 65 65 64 20 77 68 65 6e     /* Freed when
3278d 20 61 7a 43 6f 6c 75 6d 6e 20 69 73 20 66 72 65   azColumn is fre
3278e 65 64 20 2a 2f 0a 20 20 76 2d 3e 7a 4e 61 6d 65  ed */.  v->zName
3278f 20 3d 20 73 70 65 63 2d 3e 7a 4e 61 6d 65 3b 20   = spec->zName; 
32790 20 20 2f 2a 20 46 72 65 65 64 20 77 68 65 6e 20    /* Freed when 
32791 61 7a 43 6f 6c 75 6d 6e 20 69 73 20 66 72 65 65  azColumn is free
32792 64 20 2a 2f 0a 20 20 76 2d 3e 6e 43 6f 6c 75 6d  d */.  v->nColum
32793 6e 20 3d 20 73 70 65 63 2d 3e 6e 43 6f 6c 75 6d  n = spec->nColum
32794 6e 3b 0a 20 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e  n;.  v->azConten
32795 74 43 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e  tColumn = spec->
32796 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b  azContentColumn;
32797 0a 20 20 73 70 65 63 2d 3e 61 7a 43 6f 6e 74 65  .  spec->azConte
32798 6e 74 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20  ntColumn = 0;.  
32799 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 73 70  v->azColumn = sp
3279a 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 3b 0a 20 20  ec->azColumn;.  
3279b 73 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d  spec->azColumn =
3279c 20 30 3b 0a 0a 20 20 69 66 28 20 73 70 65 63 2d   0;..  if( spec-
3279d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 3d 3d 30 20  >azTokenizer==0 
3279e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3279f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
327a0 0a 20 20 7a 54 6f 6b 20 3d 20 73 70 65 63 2d 3e  .  zTok = spec->
327a1 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 30 5d 3b 20  azTokenizer[0]; 
327a2 0a 20 20 69 66 28 20 21 7a 54 6f 6b 20 29 7b 0a  .  if( !zTok ){.
327a3 20 20 20 20 7a 54 6f 6b 20 3d 20 22 73 69 6d 70      zTok = "simp
327a4 6c 65 22 3b 0a 20 20 7d 0a 20 20 6e 54 6f 6b 20  le";.  }.  nTok 
327a5 3d 20 73 74 72 6c 65 6e 28 7a 54 6f 6b 29 2b 31  = strlen(zTok)+1
327a6 3b 0a 0a 20 20 6d 20 3d 20 28 73 71 6c 69 74 65  ;..  m = (sqlite
327a7 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
327a8 6c 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33  le *)sqlite3Fts3
327a9 48 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20  HashFind(pHash, 
327aa 7a 54 6f 6b 2c 20 6e 54 6f 6b 29 3b 0a 20 20 69  zTok, nTok);.  i
327ab 66 28 20 21 6d 20 29 7b 0a 20 20 20 20 2a 70 7a  f( !m ){.    *pz
327ac 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
327ad 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74  rintf("unknown t
327ae 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20 73  okenizer: %s", s
327af 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72  pec->azTokenizer
327b0 5b 30 5d 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  [0]);.    rc = S
327b1 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
327b2 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 0a   goto err;.  }..
327b3 20 20 66 6f 72 28 6e 3d 30 3b 20 73 70 65 63 2d    for(n=0; spec-
327b4 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 6e 5d 3b  >azTokenizer[n];
327b5 20 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 6e 20   n++){}.  if( n 
327b6 29 7b 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e 78  ){.    rc = m->x
327b7 43 72 65 61 74 65 28 6e 2d 31 2c 20 28 63 6f 6e  Create(n-1, (con
327b8 73 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a 29 26  st char*const*)&
327b9 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  spec->azTokenize
327ba 72 5b 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  r[1],.          
327bb 20 20 20 20 20 20 20 20 20 20 26 76 2d 3e 70 54            &v->pT
327bc 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 7d 65 6c  okenizer);.  }el
327bd 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e  se{.    rc = m->
327be 78 43 72 65 61 74 65 28 30 2c 20 30 2c 20 26 76  xCreate(0, 0, &v
327bf 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20  ->pTokenizer);. 
327c0 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
327c1 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
327c2 72 3b 0a 20 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a  r;.  v->pTokeniz
327c3 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 6d 3b  er->pModule = m;
327c4 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 76 65 72  ..  /* TODO: ver
327c5 69 66 79 20 74 68 65 20 65 78 69 73 74 65 6e 63  ify the existenc
327c6 65 20 6f 66 20 62 61 63 6b 69 6e 67 20 74 61 62  e of backing tab
327c7 6c 65 73 20 66 6f 6f 5f 63 6f 6e 74 65 6e 74 2c  les foo_content,
327c8 20 66 6f 6f 5f 74 65 72 6d 20 2a 2f 0a 0a 20 20   foo_term */..  
327c9 73 63 68 65 6d 61 20 3d 20 66 75 6c 6c 74 65 78  schema = fulltex
327ca 74 53 63 68 65 6d 61 28 76 2d 3e 6e 43 6f 6c 75  tSchema(v->nColu
327cb 6d 6e 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a  mn, (const char*
327cc 63 6f 6e 73 74 2a 29 76 2d 3e 61 7a 43 6f 6c 75  const*)v->azColu
327cd 6d 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  mn,.            
327ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 70                sp
327cf 65 63 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 72 63  ec->zName);.  rc
327d0 20 3d 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61   = sqlite3_decla
327d1 72 65 5f 76 74 61 62 28 64 62 2c 20 73 63 68 65  re_vtab(db, sche
327d2 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ma);.  sqlite3_f
327d3 72 65 65 28 73 63 68 65 6d 61 29 3b 0a 20 20 69  ree(schema);.  i
327d4 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
327d5 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20   ) goto err;..  
327d6 6d 65 6d 73 65 74 28 76 2d 3e 70 46 75 6c 6c 74  memset(v->pFullt
327d7 65 78 74 53 74 61 74 65 6d 65 6e 74 73 2c 20 30  extStatements, 0
327d8 2c 20 73 69 7a 65 6f 66 28 76 2d 3e 70 46 75 6c  , sizeof(v->pFul
327d9 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 29  ltextStatements)
327da 29 3b 0a 0a 20 20 2f 2a 20 49 6e 64 69 63 61 74  );..  /* Indicat
327db 65 20 74 68 61 74 20 74 68 65 20 62 75 66 66 65  e that the buffe
327dc 72 20 69 73 20 6e 6f 74 20 6c 69 76 65 2e 20 2a  r is not live. *
327dd 2f 0a 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44  /.  v->nPendingD
327de 61 74 61 20 3d 20 2d 31 3b 0a 0a 20 20 2a 70 70  ata = -1;..  *pp
327df 56 54 61 62 20 3d 20 26 76 2d 3e 62 61 73 65 3b  VTab = &v->base;
327e0 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
327e1 53 33 20 43 6f 6e 6e 65 63 74 20 25 70 5c 6e 22  S3 Connect %p\n"
327e2 2c 20 76 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e  , v));..  return
327e3 20 72 63 3b 0a 0a 65 72 72 3a 0a 20 20 66 75 6c   rc;..err:.  ful
327e4 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72  ltext_vtab_destr
327e5 6f 79 28 76 29 3b 0a 20 20 72 65 74 75 72 6e 20  oy(v);.  return 
327e6 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
327e7 74 20 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65 63  t fulltextConnec
327e8 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
327e9 2c 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a  ,.  void *pAux,.
327ea 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
327eb 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72  t char *const*ar
327ec 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  gv,.  sqlite3_vt
327ed 61 62 20 2a 2a 70 70 56 54 61 62 2c 0a 20 20 63  ab **ppVTab,.  c
327ee 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20  har **pzErr.){. 
327ef 20 54 61 62 6c 65 53 70 65 63 20 73 70 65 63 3b   TableSpec spec;
327f0 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 61 72 73  .  int rc = pars
327f1 65 53 70 65 63 28 26 73 70 65 63 2c 20 61 72 67  eSpec(&spec, arg
327f2 63 2c 20 61 72 67 76 2c 20 70 7a 45 72 72 29 3b  c, argv, pzErr);
327f3 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
327f4 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
327f5 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 73 74 72  ;..  rc = constr
327f6 75 63 74 56 74 61 62 28 64 62 2c 20 28 66 74 73  uctVtab(db, (fts
327f7 33 48 61 73 68 20 2a 29 70 41 75 78 2c 20 26 73  3Hash *)pAux, &s
327f8 70 65 63 2c 20 70 70 56 54 61 62 2c 20 70 7a 45  pec, ppVTab, pzE
327f9 72 72 29 3b 0a 20 20 63 6c 65 61 72 54 61 62 6c  rr);.  clearTabl
327fa 65 53 70 65 63 28 26 73 70 65 63 29 3b 0a 20 20  eSpec(&spec);.  
327fb 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
327fc 20 54 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   The %_content t
327fd 61 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 74  able holds the t
327fe 65 78 74 20 6f 66 20 65 61 63 68 20 64 6f 63 75  ext of each docu
327ff 6d 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 74 68  ment, with.** th
32800 65 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 20 65  e docid column e
32801 78 70 6f 73 65 64 20 61 73 20 74 68 65 20 53 51  xposed as the SQ
32802 4c 69 74 65 20 72 6f 77 69 64 20 66 6f 72 20 74  Lite rowid for t
32803 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20  he table..*/./* 
32804 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73  TODO(shess) This
32805 20 63 6f 6d 6d 65 6e 74 20 6e 65 65 64 73 20 65   comment needs e
32806 6c 61 62 6f 72 61 74 69 6f 6e 20 74 6f 20 6d 61  laboration to ma
32807 74 63 68 20 74 68 65 20 75 70 64 61 74 65 64 0a  tch the updated.
32808 2a 2a 20 63 6f 64 65 2e 20 20 57 6f 72 6b 20 69  ** code.  Work i
32809 74 20 69 6e 74 6f 20 74 68 65 20 74 6f 70 2d 6f  t into the top-o
3280a 66 2d 66 69 6c 65 20 63 6f 6d 6d 65 6e 74 20 61  f-file comment a
3280b 74 20 74 68 61 74 20 74 69 6d 65 2e 0a 2a 2f 0a  t that time..*/.
3280c 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
3280d 65 78 74 43 72 65 61 74 65 28 73 71 6c 69 74 65  extCreate(sqlite
3280e 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 41 75  3 *db, void *pAu
3280f 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
32810 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
32811 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
32812 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
32813 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32814 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
32815 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62  e3_vtab **ppVTab
32816 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b  , char **pzErr){
32817 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 61 62  .  int rc;.  Tab
32818 6c 65 53 70 65 63 20 73 70 65 63 3b 0a 20 20 53  leSpec spec;.  S
32819 74 72 69 6e 67 42 75 66 66 65 72 20 73 63 68 65  tringBuffer sche
3281a 6d 61 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28  ma;.  FTSTRACE((
3281b 22 46 54 53 33 20 43 72 65 61 74 65 5c 6e 22 29  "FTS3 Create\n")
3281c 29 3b 0a 0a 20 20 72 63 20 3d 20 70 61 72 73 65  );..  rc = parse
3281d 53 70 65 63 28 26 73 70 65 63 2c 20 61 72 67 63  Spec(&spec, argc
3281e 2c 20 61 72 67 76 2c 20 70 7a 45 72 72 29 3b 0a  , argv, pzErr);.
3281f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32820 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
32821 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ..  initStringBu
32822 66 66 65 72 28 26 73 63 68 65 6d 61 29 3b 0a 20  ffer(&schema);. 
32823 20 61 70 70 65 6e 64 28 26 73 63 68 65 6d 61 2c   append(&schema,
32824 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25   "CREATE TABLE %
32825 5f 63 6f 6e 74 65 6e 74 28 22 29 3b 0a 20 20 61  _content(");.  a
32826 70 70 65 6e 64 28 26 73 63 68 65 6d 61 2c 20 22  ppend(&schema, "
32827 20 20 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20    docid INTEGER 
32828 50 52 49 4d 41 52 59 20 4b 45 59 2c 22 29 3b 0a  PRIMARY KEY,");.
32829 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73 63    appendList(&sc
3282a 68 65 6d 61 2c 20 73 70 65 63 2e 6e 43 6f 6c 75  hema, spec.nColu
3282b 6d 6e 2c 20 73 70 65 63 2e 61 7a 43 6f 6e 74 65  mn, spec.azConte
3282c 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70  ntColumn);.  app
3282d 65 6e 64 28 26 73 63 68 65 6d 61 2c 20 22 29 22  end(&schema, ")"
3282e 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78  );.  rc = sql_ex
3282f 65 63 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c  ec(db, spec.zDb,
32830 20 73 70 65 63 2e 7a 4e 61 6d 65 2c 20 73 74 72   spec.zName, str
32831 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26 73  ingBufferData(&s
32832 63 68 65 6d 61 29 29 3b 0a 20 20 73 74 72 69 6e  chema));.  strin
32833 67 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  gBufferDestroy(&
32834 73 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 72  schema);.  if( r
32835 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
32836 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d  oto out;..  rc =
32837 20 73 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70   sql_exec(db, sp
32838 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61  ec.zDb, spec.zNa
32839 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  me,.            
3283a 20 20 20 20 22 63 72 65 61 74 65 20 74 61 62 6c      "create tabl
3283b 65 20 25 5f 73 65 67 6d 65 6e 74 73 28 22 0a 20  e %_segments(". 
3283c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
3283d 20 20 62 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45    blockid INTEGE
3283e 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 22 0a  R PRIMARY KEY,".
3283f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32840 22 20 20 62 6c 6f 63 6b 20 62 6c 6f 62 22 0a 20  "  block blob". 
32841 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
32842 29 3b 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  );".            
32843 20 20 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21      );.  if( rc!
32844 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
32845 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73  o out;..  rc = s
32846 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70 65 63  ql_exec(db, spec
32847 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65  .zDb, spec.zName
32848 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
32849 20 20 22 63 72 65 61 74 65 20 74 61 62 6c 65 20    "create table 
3284a 25 5f 73 65 67 64 69 72 28 22 0a 20 20 20 20 20  %_segdir(".     
3284b 20 20 20 20 20 20 20 20 20 20 20 22 20 20 6c 65             "  le
3284c 76 65 6c 20 69 6e 74 65 67 65 72 2c 22 0a 20 20  vel integer,".  
3284d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20                " 
3284e 20 69 64 78 20 69 6e 74 65 67 65 72 2c 22 0a 20   idx integer,". 
3284f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
32850 20 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 69 6e    start_block in
32851 74 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20  teger,".        
32852 20 20 20 20 20 20 20 20 22 20 20 6c 65 61 76 65          "  leave
32853 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 69 6e 74 65  s_end_block inte
32854 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20  ger,".          
32855 20 20 20 20 20 20 22 20 20 65 6e 64 5f 62 6c 6f        "  end_blo
32856 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20  ck integer,".   
32857 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
32858 72 6f 6f 74 20 62 6c 6f 62 2c 22 0a 20 20 20 20  root blob,".    
32859 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20 70              "  p
3285a 72 69 6d 61 72 79 20 6b 65 79 28 6c 65 76 65 6c  rimary key(level
3285b 2c 20 69 64 78 29 22 0a 20 20 20 20 20 20 20 20  , idx)".        
3285c 20 20 20 20 20 20 20 20 22 29 3b 22 29 3b 0a 20          ");");. 
3285d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3285e 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a  OK ) goto out;..
3285f 20 20 72 63 20 3d 20 63 6f 6e 73 74 72 75 63 74    rc = construct
32860 56 74 61 62 28 64 62 2c 20 28 66 74 73 33 48 61  Vtab(db, (fts3Ha
32861 73 68 20 2a 29 70 41 75 78 2c 20 26 73 70 65 63  sh *)pAux, &spec
32862 2c 20 70 70 56 54 61 62 2c 20 70 7a 45 72 72 29  , ppVTab, pzErr)
32863 3b 0a 0a 6f 75 74 3a 0a 20 20 63 6c 65 61 72 54  ;..out:.  clearT
32864 61 62 6c 65 53 70 65 63 28 26 73 70 65 63 29 3b  ableSpec(&spec);
32865 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
32866 0a 2f 2a 20 44 65 63 69 64 65 20 68 6f 77 20 74  ./* Decide how t
32867 6f 20 68 61 6e 64 6c 65 20 61 6e 20 53 51 4c 20  o handle an SQL 
32868 71 75 65 72 79 2e 20 2a 2f 0a 73 74 61 74 69 63  query. */.static
32869 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 42 65 73   int fulltextBes
3286a 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 76  tIndex(sqlite3_v
3286b 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
3286c 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
3286d 70 49 6e 66 6f 29 7b 0a 20 20 66 75 6c 6c 74 65  pInfo){.  fullte
3286e 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75  xt_vtab *v = (fu
3286f 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56  lltext_vtab *)pV
32870 54 61 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Tab;.  int i;.  
32871 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
32872 42 65 73 74 49 6e 64 65 78 5c 6e 22 29 29 3b 0a  BestIndex\n"));.
32873 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49  .  for(i=0; i<pI
32874 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  nfo->nConstraint
32875 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 63 6f 6e 73  ; ++i){.    cons
32876 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  t struct sqlite3
32877 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
32878 74 20 2a 70 43 6f 6e 73 74 72 61 69 6e 74 3b 0a  t *pConstraint;.
32879 20 20 20 20 70 43 6f 6e 73 74 72 61 69 6e 74 20      pConstraint 
3287a 3d 20 26 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  = &pInfo->aConst
3287b 72 61 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66  raint[i];.    if
3287c 28 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 75  ( pConstraint->u
3287d 73 61 62 6c 65 20 29 20 7b 0a 20 20 20 20 20 20  sable ) {.      
3287e 69 66 28 20 28 70 43 6f 6e 73 74 72 61 69 6e 74  if( (pConstraint
3287f 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c  ->iColumn==-1 ||
32880 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43   pConstraint->iC
32881 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d  olumn==v->nColum
32882 6e 2b 31 29 20 26 26 0a 20 20 20 20 20 20 20 20  n+1) &&.        
32883 20 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 6f    pConstraint->o
32884 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  p==SQLITE_INDEX_
32885 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29 7b  CONSTRAINT_EQ ){
32886 0a 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  .        pInfo->
32887 69 64 78 4e 75 6d 20 3d 20 51 55 45 52 59 5f 44  idxNum = QUERY_D
32888 4f 43 49 44 3b 20 20 20 20 20 20 2f 2a 20 6c 6f  OCID;      /* lo
32889 6f 6b 75 70 20 62 79 20 64 6f 63 69 64 20 2a 2f  okup by docid */
3288a 0a 20 20 20 20 20 20 20 20 46 54 53 54 52 41 43  .        FTSTRAC
3288b 45 28 28 22 46 54 53 33 20 51 55 45 52 59 5f 44  E(("FTS3 QUERY_D
3288c 4f 43 49 44 5c 6e 22 29 29 3b 0a 20 20 20 20 20  OCID\n"));.     
3288d 20 7d 20 65 6c 73 65 20 69 66 28 20 70 43 6f 6e   } else if( pCon
3288e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e  straint->iColumn
3288f 3e 3d 30 20 26 26 20 70 43 6f 6e 73 74 72 61 69  >=0 && pConstrai
32890 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3c 3d 76 2d 3e  nt->iColumn<=v->
32891 6e 43 6f 6c 75 6d 6e 20 26 26 0a 20 20 20 20 20  nColumn &&.     
32892 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6e              pCon
32893 73 74 72 61 69 6e 74 2d 3e 6f 70 3d 3d 53 51 4c  straint->op==SQL
32894 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
32895 41 49 4e 54 5f 4d 41 54 43 48 20 29 7b 0a 20 20  AINT_MATCH ){.  
32896 20 20 20 20 20 20 2f 2a 20 66 75 6c 6c 2d 74 65        /* full-te
32897 78 74 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 20  xt search */.   
32898 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e       pInfo->idxN
32899 75 6d 20 3d 20 51 55 45 52 59 5f 46 55 4c 4c 54  um = QUERY_FULLT
3289a 45 58 54 20 2b 20 70 43 6f 6e 73 74 72 61 69 6e  EXT + pConstrain
3289b 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  t->iColumn;.    
3289c 20 20 20 20 46 54 53 54 52 41 43 45 28 28 22 46      FTSTRACE(("F
3289d 54 53 33 20 51 55 45 52 59 5f 46 55 4c 4c 54 45  TS3 QUERY_FULLTE
3289e 58 54 20 25 64 5c 6e 22 2c 20 70 43 6f 6e 73 74  XT %d\n", pConst
3289f 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 29 29  raint->iColumn))
328a0 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 63  ;.      } else c
328a1 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20  ontinue;..      
328a2 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  pInfo->aConstrai
328a3 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49  ntUsage[i].argvI
328a4 6e 64 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20  ndex = 1;.      
328a5 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  pInfo->aConstrai
328a6 6e 74 55 73 61 67 65 5b 69 5d 2e 6f 6d 69 74 20  ntUsage[i].omit 
328a7 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41  = 1;..      /* A
328a8 6e 20 61 72 62 69 74 72 61 72 79 20 76 61 6c 75  n arbitrary valu
328a9 65 20 66 6f 72 20 6e 6f 77 2e 0a 20 20 20 20 20  e for now..     
328aa 20 20 2a 20 54 4f 44 4f 3a 20 50 65 72 68 61 70    * TODO: Perhap
328ab 73 20 64 6f 63 69 64 20 6d 61 74 63 68 65 73 20  s docid matches 
328ac 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64  should be consid
328ad 65 72 65 64 20 63 68 65 61 70 65 72 20 74 68 61  ered cheaper tha
328ae 6e 0a 20 20 20 20 20 20 20 2a 20 66 75 6c 6c 2d  n.       * full-
328af 74 65 78 74 20 73 65 61 72 63 68 65 73 2e 20 2a  text searches. *
328b0 2f 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 65  /.      pInfo->e
328b1 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 31  stimatedCost = 1
328b2 2e 30 3b 20 20 20 0a 0a 20 20 20 20 20 20 72 65  .0;   ..      re
328b3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
328b4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 49 6e 66      }.  }.  pInf
328b5 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55 45 52  o->idxNum = QUER
328b6 59 5f 47 45 4e 45 52 49 43 3b 0a 20 20 72 65 74  Y_GENERIC;.  ret
328b7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
328b8 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  ..static int ful
328b9 6c 74 65 78 74 44 69 73 63 6f 6e 6e 65 63 74 28  ltextDisconnect(
328ba 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
328bb 54 61 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45  Tab){.  FTSTRACE
328bc 28 28 22 46 54 53 33 20 44 69 73 63 6f 6e 6e 65  (("FTS3 Disconne
328bd 63 74 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 29  ct %p\n", pVTab)
328be 29 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  );.  fulltext_vt
328bf 61 62 5f 64 65 73 74 72 6f 79 28 28 66 75 6c 6c  ab_destroy((full
328c0 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61  text_vtab *)pVTa
328c1 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  b);.  return SQL
328c2 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69  ITE_OK;.}..stati
328c3 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 44 65  c int fulltextDe
328c4 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76 74  stroy(sqlite3_vt
328c5 61 62 20 2a 70 56 54 61 62 29 7b 0a 20 20 66 75  ab *pVTab){.  fu
328c6 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
328c7 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20   (fulltext_vtab 
328c8 2a 29 70 56 54 61 62 3b 0a 20 20 69 6e 74 20 72  *)pVTab;.  int r
328c9 63 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28  c;..  FTSTRACE((
328ca 22 46 54 53 33 20 44 65 73 74 72 6f 79 20 25 70  "FTS3 Destroy %p
328cb 5c 6e 22 2c 20 70 56 54 61 62 29 29 3b 0a 20 20  \n", pVTab));.  
328cc 72 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 76 2d  rc = sql_exec(v-
328cd 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e  >db, v->zDb, v->
328ce 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
328cf 20 20 20 20 20 20 20 22 64 72 6f 70 20 74 61 62         "drop tab
328d0 6c 65 20 69 66 20 65 78 69 73 74 73 20 25 5f 63  le if exists %_c
328d1 6f 6e 74 65 6e 74 3b 22 0a 20 20 20 20 20 20 20  ontent;".       
328d2 20 20 20 20 20 20 20 20 20 22 64 72 6f 70 20 74           "drop t
328d3 61 62 6c 65 20 69 66 20 65 78 69 73 74 73 20 25  able if exists %
328d4 5f 73 65 67 6d 65 6e 74 73 3b 22 0a 20 20 20 20  _segments;".    
328d5 20 20 20 20 20 20 20 20 20 20 20 20 22 64 72 6f              "dro
328d6 70 20 74 61 62 6c 65 20 69 66 20 65 78 69 73 74  p table if exist
328d7 73 20 25 5f 73 65 67 64 69 72 3b 22 0a 20 20 20  s %_segdir;".   
328d8 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a               );.
328d9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
328da 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
328db 0a 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  ..  fulltext_vta
328dc 62 5f 64 65 73 74 72 6f 79 28 28 66 75 6c 6c 74  b_destroy((fullt
328dd 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62  ext_vtab *)pVTab
328de 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
328df 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63  TE_OK;.}..static
328e0 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 4f 70 65   int fulltextOpe
328e1 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  n(sqlite3_vtab *
328e2 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76  pVTab, sqlite3_v
328e3 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43  tab_cursor **ppC
328e4 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65  ursor){.  fullte
328e5 78 74 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20  xt_cursor *c;.. 
328e6 20 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63   c = (fulltext_c
328e7 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33  ursor *) sqlite3
328e8 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66  _malloc(sizeof(f
328e9 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 29 29  ulltext_cursor))
328ea 3b 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20  ;.  if( c ){.   
328eb 20 6d 65 6d 73 65 74 28 63 2c 20 30 2c 20 73 69   memset(c, 0, si
328ec 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74 5f 63 75  zeof(fulltext_cu
328ed 72 73 6f 72 29 29 3b 0a 20 20 20 20 2f 2a 20 73  rsor));.    /* s
328ee 71 6c 69 74 65 20 77 69 6c 6c 20 69 6e 69 74 69  qlite will initi
328ef 61 6c 69 7a 65 20 63 2d 3e 62 61 73 65 20 2a 2f  alize c->base */
328f0 0a 20 20 20 20 2a 70 70 43 75 72 73 6f 72 20 3d  .    *ppCursor =
328f1 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20 20 20 46   &c->base;.    F
328f2 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 4f  TSTRACE(("FTS3 O
328f3 70 65 6e 20 25 70 3a 20 25 70 5c 6e 22 2c 20 70  pen %p: %p\n", p
328f4 56 54 61 62 2c 20 63 29 29 3b 0a 20 20 20 20 72  VTab, c));.    r
328f5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
328f6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
328f7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
328f8 4d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 72 65  M;.  }.}../* Fre
328f9 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 64 79 6e  e all of the dyn
328fa 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
328fb 65 64 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62  ed memory held b
328fc 79 20 74 68 65 0a 2a 2a 20 53 6e 69 70 70 65 74  y the.** Snippet
328fd 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
328fe 73 6e 69 70 70 65 74 43 6c 65 61 72 28 53 6e 69  snippetClear(Sni
328ff 70 70 65 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69  ppet *p){.  sqli
32900 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 4d 61 74  te3_free(p->aMat
32901 63 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ch);.  sqlite3_f
32902 72 65 65 28 70 2d 3e 7a 4f 66 66 73 65 74 29 3b  ree(p->zOffset);
32903 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
32904 70 2d 3e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20  p->zSnippet);.  
32905 43 4c 45 41 52 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  CLEAR(p);.}../*.
32906 2a 2a 20 41 70 70 65 6e 64 20 61 20 73 69 6e 67  ** Append a sing
32907 6c 65 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20  le entry to the 
32908 70 2d 3e 61 4d 61 74 63 68 5b 5d 20 6c 6f 67 2e  p->aMatch[] log.
32909 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3290a 73 6e 69 70 70 65 74 41 70 70 65 6e 64 4d 61 74  snippetAppendMat
3290b 63 68 28 0a 20 20 53 6e 69 70 70 65 74 20 2a 70  ch(.  Snippet *p
3290c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3290d 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20 65 6e  /* Append the en
3290e 74 72 79 20 74 6f 20 74 68 69 73 20 73 6e 69 70  try to this snip
3290f 70 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  pet */.  int iCo
32910 6c 2c 20 69 6e 74 20 69 54 65 72 6d 2c 20 20 20  l, int iTerm,   
32911 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e     /* The column
32912 20 61 6e 64 20 71 75 65 72 79 20 74 65 72 6d 20   and query term 
32913 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 2c  */.  int iToken,
32914 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32915 2a 20 4d 61 74 63 68 69 6e 67 20 74 6f 6b 65 6e  * Matching token
32916 20 69 6e 20 64 6f 63 75 6d 65 6e 74 20 2a 2f 0a   in document */.
32917 20 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e    int iStart, in
32918 74 20 6e 42 79 74 65 20 20 20 20 20 2f 2a 20 4f  t nByte     /* O
32919 66 66 73 65 74 20 61 6e 64 20 73 69 7a 65 20 6f  ffset and size o
3291a 66 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 29  f the match */.)
3291b 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
3291c 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68  uct snippetMatch
3291d 20 2a 70 4d 61 74 63 68 3b 0a 20 20 69 66 28 20   *pMatch;.  if( 
3291e 70 2d 3e 6e 4d 61 74 63 68 2b 31 3e 3d 70 2d 3e  p->nMatch+1>=p->
3291f 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 2d  nAlloc ){.    p-
32920 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c  >nAlloc = p->nAl
32921 6c 6f 63 2a 32 20 2b 20 31 30 3b 0a 20 20 20 20  loc*2 + 10;.    
32922 70 2d 3e 61 4d 61 74 63 68 20 3d 20 73 71 6c 69  p->aMatch = sqli
32923 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2d 3e 61  te3_realloc(p->a
32924 4d 61 74 63 68 2c 20 70 2d 3e 6e 41 6c 6c 6f 63  Match, p->nAlloc
32925 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4d 61 74 63  *sizeof(p->aMatc
32926 68 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28  h[0]) );.    if(
32927 20 70 2d 3e 61 4d 61 74 63 68 3d 3d 30 20 29 7b   p->aMatch==0 ){
32928 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 61 74 63 68  .      p->nMatch
32929 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6e   = 0;.      p->n
3292a 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20  Alloc = 0;.     
3292b 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
3292c 20 7d 0a 20 20 69 20 3d 20 70 2d 3e 6e 4d 61 74   }.  i = p->nMat
3292d 63 68 2b 2b 3b 0a 20 20 70 4d 61 74 63 68 20 3d  ch++;.  pMatch =
3292e 20 26 70 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a   &p->aMatch[i];.
3292f 20 20 70 4d 61 74 63 68 2d 3e 69 43 6f 6c 20 3d    pMatch->iCol =
32930 20 69 43 6f 6c 3b 0a 20 20 70 4d 61 74 63 68 2d   iCol;.  pMatch-
32931 3e 69 54 65 72 6d 20 3d 20 69 54 65 72 6d 3b 0a  >iTerm = iTerm;.
32932 20 20 70 4d 61 74 63 68 2d 3e 69 54 6f 6b 65 6e    pMatch->iToken
32933 20 3d 20 69 54 6f 6b 65 6e 3b 0a 20 20 70 4d 61   = iToken;.  pMa
32934 74 63 68 2d 3e 69 53 74 61 72 74 20 3d 20 69 53  tch->iStart = iS
32935 74 61 72 74 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  tart;.  pMatch->
32936 6e 42 79 74 65 20 3d 20 6e 42 79 74 65 3b 0a 7d  nByte = nByte;.}
32937 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 69 6e 67 20 69  ../*.** Sizing i
32938 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
32939 68 65 20 63 69 72 63 75 6c 61 72 20 62 75 66 66  he circular buff
3293a 65 72 20 75 73 65 64 20 69 6e 20 73 6e 69 70 70  er used in snipp
3293b 65 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d  etOffsetsOfColum
3293c 6e 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  n().*/.#define F
3293d 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 20 20 20 28  TS3_ROTOR_SZ   (
3293e 33 32 29 0a 23 64 65 66 69 6e 65 20 46 54 53 33  32).#define FTS3
3293f 5f 52 4f 54 4f 52 5f 4d 41 53 4b 20 28 46 54 53  _ROTOR_MASK (FTS
32940 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 0a 0a 2f  3_ROTOR_SZ-1)../
32941 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f  *.** Function to
32942 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
32943 20 74 68 65 20 74 6f 6b 65 6e 73 20 6f 66 20 61   the tokens of a
32944 20 63 6f 6d 70 69 6c 65 64 20 65 78 70 72 65 73   compiled expres
32945 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65  sion..**.** Exce
32946 70 74 2c 20 73 6b 69 70 20 61 6c 6c 20 74 6f 6b  pt, skip all tok
32947 65 6e 73 20 6f 6e 20 74 68 65 20 72 69 67 68 74  ens on the right
32948 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20  -hand side of a 
32949 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a  NOT operator..**
3294a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
3294b 73 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74  s used to find t
3294c 6f 6b 65 6e 73 20 61 73 20 70 61 72 74 20 6f 66  okens as part of
3294d 20 73 6e 69 70 70 65 74 20 61 6e 64 20 6f 66 66   snippet and off
3294e 73 65 74 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6f  set.** generatio
3294f 6e 20 61 6e 64 20 77 65 20 64 6f 20 6e 74 20 77  n and we do nt w
32950 61 6e 74 20 73 6e 69 70 70 65 74 73 20 61 6e 64  ant snippets and
32951 20 6f 66 66 73 65 74 73 20 74 6f 20 72 65 70 6f   offsets to repo
32952 72 74 20 6d 61 74 63 68 65 73 0a 2a 2a 20 66 6f  rt matches.** fo
32953 72 20 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65 20  r tokens on the 
32954 52 48 53 20 6f 66 20 61 20 4e 4f 54 2e 0a 2a 2f  RHS of a NOT..*/
32955 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
32956 4e 65 78 74 45 78 70 72 54 6f 6b 65 6e 28 46 74  NextExprToken(Ft
32957 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c  s3Expr **ppExpr,
32958 20 69 6e 74 20 2a 70 69 54 6f 6b 65 6e 29 7b 0a   int *piToken){.
32959 20 20 46 74 73 33 45 78 70 72 20 2a 70 20 3d 20    Fts3Expr *p = 
3295a 2a 70 70 45 78 70 72 3b 0a 20 20 69 6e 74 20 69  *ppExpr;.  int i
3295b 54 6f 6b 65 6e 20 3d 20 2a 70 69 54 6f 6b 65 6e  Token = *piToken
3295c 3b 0a 20 20 69 66 28 20 69 54 6f 6b 65 6e 3c 30  ;.  if( iToken<0
3295d 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68   ){.    /* In th
3295e 69 73 20 63 61 73 65 20 74 68 65 20 65 78 70 72  is case the expr
3295f 65 73 73 69 6f 6e 20 70 20 69 73 20 74 68 65 20  ession p is the 
32960 72 6f 6f 74 20 6f 66 20 61 6e 20 65 78 70 72 65  root of an expre
32961 73 73 69 6f 6e 20 74 72 65 65 2e 0a 20 20 20 20  ssion tree..    
32962 2a 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20 66  ** Move to the f
32963 69 72 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68  irst token in th
32964 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
32965 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68  e..    */.    wh
32966 69 6c 65 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b  ile( p->pLeft ){
32967 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c  .      p = p->pL
32968 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eft;.    }.    i
32969 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c  Token = 0;.  }el
3296a 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70  se{.    assert(p
3296b 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54   && p->eType==FT
3296c 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b  SQUERY_PHRASE );
3296d 0a 20 20 20 20 69 66 28 20 69 54 6f 6b 65 6e 3c  .    if( iToken<
3296e 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  (p->pPhrase->nTo
3296f 6b 65 6e 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ken-1) ){.      
32970 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 7d 65  iToken++;.    }e
32971 6c 73 65 7b 0a 20 20 20 20 20 20 69 54 6f 6b 65  lse{.      iToke
32972 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69  n = 0;.      whi
32973 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 26  le( p->pParent &
32974 26 20 70 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 4c  & p->pParent->pL
32975 65 66 74 21 3d 70 20 29 7b 0a 20 20 20 20 20 20  eft!=p ){.      
32976 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 61    assert( p->pPa
32977 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70 20  rent->pRight==p 
32978 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70  );.        p = p
32979 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 20  ->pParent;.     
3297a 20 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e   }.      p = p->
3297b 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 69  pParent;.      i
3297c 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
3297d 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68  assert( p->pRigh
3297e 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  t!=0 );.        
3297f 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20  p = p->pRight;. 
32980 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d         while( p-
32981 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  >pLeft ){.      
32982 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74      p = p->pLeft
32983 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
32984 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
32985 20 2a 70 70 45 78 70 72 20 3d 20 70 3b 0a 20 20   *ppExpr = p;.  
32986 2a 70 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65  *piToken = iToke
32987 6e 3b 0a 20 20 72 65 74 75 72 6e 20 70 3f 31 3a  n;.  return p?1:
32988 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  0;.}../*.** Retu
32989 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 65  rn TRUE if the e
3298a 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 70  xpression node p
3298b 45 78 70 72 20 69 73 20 6c 6f 63 61 74 65 64 20  Expr is located 
3298c 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 52  beneath the.** R
3298d 48 53 20 6f 66 20 61 20 4e 4f 54 20 6f 70 65 72  HS of a NOT oper
3298e 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ator..*/.static 
3298f 69 6e 74 20 66 74 73 33 45 78 70 72 42 65 6e 65  int fts3ExprBene
32990 61 74 68 4e 6f 74 28 46 74 73 33 45 78 70 72 20  athNot(Fts3Expr 
32991 2a 70 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  *p){.  Fts3Expr 
32992 2a 70 50 61 72 65 6e 74 3b 0a 20 20 77 68 69 6c  *pParent;.  whil
32993 65 28 20 70 20 29 7b 0a 20 20 20 20 70 50 61 72  e( p ){.    pPar
32994 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74  ent = p->pParent
32995 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 65 6e  ;.    if( pParen
32996 74 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 65 54  t && pParent->eT
32997 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 4f  ype==FTSQUERY_NO
32998 54 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 52  T && pParent->pR
32999 69 67 68 74 3d 3d 70 20 29 7b 0a 20 20 20 20 20  ight==p ){.     
3299a 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
3299b 0a 20 20 20 20 70 20 3d 20 70 50 61 72 65 6e 74  .    p = pParent
3299c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
3299d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 65  ;.}../*.** Add e
3299e 6e 74 72 69 65 73 20 74 6f 20 70 53 6e 69 70 70  ntries to pSnipp
3299f 65 74 2d 3e 61 4d 61 74 63 68 5b 5d 20 66 6f 72  et->aMatch[] for
329a0 20 65 76 65 72 79 20 6d 61 74 63 68 20 74 68 61   every match tha
329a1 74 20 6f 63 63 75 72 73 20 61 67 61 69 6e 73 74  t occurs against
329a2 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 7a 44 6f  .** document zDo
329a3 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 77 68 69  c[0..nDoc-1] whi
329a4 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ch is stored in 
329a5 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 0a  column iColumn..
329a6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
329a7 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43  nippetOffsetsOfC
329a8 6f 6c 75 6d 6e 28 0a 20 20 66 75 6c 6c 74 65 78  olumn(.  fulltex
329a9 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  t_cursor *pCur, 
329aa 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
329ab 75 6c 6c 74 65 73 74 20 73 65 61 72 63 68 20 63  ulltest search c
329ac 75 72 73 6f 72 20 2a 2f 0a 20 20 53 6e 69 70 70  ursor */.  Snipp
329ad 65 74 20 2a 70 53 6e 69 70 70 65 74 2c 20 20 20  et *pSnippet,   
329ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
329af 20 53 6e 69 70 70 65 74 20 6f 62 6a 65 63 74 20   Snippet object 
329b0 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20  to be filled in 
329b1 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
329b2 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
329b3 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
329b4 66 75 6c 6c 74 65 78 74 20 74 61 62 6c 65 20 63  fulltext table c
329b5 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  olumn */.  const
329b6 20 63 68 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20   char *zDoc,    
329b7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
329b8 74 20 6f 66 20 74 68 65 20 66 75 6c 6c 74 65 78  t of the fulltex
329b9 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a  t table column *
329ba 2f 0a 20 20 69 6e 74 20 6e 44 6f 63 20 20 20 20  /.  int nDoc    
329bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329bc 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
329bd 7a 44 6f 63 20 69 6e 20 62 79 74 65 73 20 2a 2f  zDoc in bytes */
329be 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  .){.  const sqli
329bf 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
329c0 64 75 6c 65 20 2a 70 54 4d 6f 64 75 6c 65 3b 20  dule *pTModule; 
329c1 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65   /* The tokenize
329c2 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71  r module */.  sq
329c3 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
329c4 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20  *pTokenizer;    
329c5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
329c6 73 70 65 63 69 66 69 63 20 74 6f 6b 65 6e 69 7a  specific tokeniz
329c7 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  er */.  sqlite3_
329c8 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
329c9 20 2a 70 54 43 75 72 73 6f 72 3b 20 20 20 20 20   *pTCursor;     
329ca 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
329cb 63 75 72 73 6f 72 20 2a 2f 0a 20 20 66 75 6c 6c  cursor */.  full
329cc 74 65 78 74 5f 76 74 61 62 20 2a 70 56 74 61 62  text_vtab *pVtab
329cd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
329ce 20 2f 2a 20 54 68 65 20 66 75 6c 6c 20 74 65 78   /* The full tex
329cf 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  t index */.  int
329d0 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
329d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329d2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
329d3 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e  olumns in the in
329d4 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  dex */.  int i, 
329d5 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
329d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
329d7 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
329d8 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
329d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329da 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
329db 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e 73  rn code */.  uns
329dc 69 67 6e 65 64 20 69 6e 74 20 6d 61 74 63 68 2c  igned int match,
329dd 20 70 72 65 76 4d 61 74 63 68 3b 20 20 20 20 20   prevMatch;     
329de 20 20 2f 2a 20 50 68 72 61 73 65 20 73 65 61 72    /* Phrase sear
329df 63 68 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 20  ch bitmasks */. 
329e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f   const char *zTo
329e1 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ken;            
329e2 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74 6f        /* Next to
329e3 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b  ken from the tok
329e4 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 69 6e 74 20  enizer */.  int 
329e5 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  nToken;         
329e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329e7 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 54 6f 6b   /* Size of zTok
329e8 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 67  en */.  int iBeg
329e9 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f 73 3b 20  in, iEnd, iPos; 
329ea 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
329eb 4f 66 66 73 65 74 73 20 6f 66 20 62 65 67 69 6e  Offsets of begin
329ec 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 2a 2f 0a  ning and end */.
329ed 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
329ee 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 6b 65  ing variables ke
329ef 65 70 20 61 20 63 69 72 63 75 6c 61 72 20 62 75  ep a circular bu
329f0 66 66 65 72 20 6f 66 20 74 68 65 20 6c 61 73 74  ffer of the last
329f1 0a 20 20 2a 2a 20 66 65 77 20 74 6f 6b 65 6e 73  .  ** few tokens
329f2 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
329f3 6e 74 20 69 52 6f 74 6f 72 20 3d 20 30 3b 20 20  nt iRotor = 0;  
329f4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
329f5 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 74  dex of current t
329f6 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 52  oken */.  int iR
329f7 6f 74 6f 72 42 65 67 69 6e 5b 46 54 53 33 5f 52  otorBegin[FTS3_R
329f8 4f 54 4f 52 5f 53 5a 5d 3b 20 20 20 20 20 20 2f  OTOR_SZ];      /
329f9 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 66 73  * Beginning offs
329fa 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
329fb 20 69 6e 74 20 69 52 6f 74 6f 72 4c 65 6e 5b 46   int iRotorLen[F
329fc 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20  TS3_ROTOR_SZ];  
329fd 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
329fe 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 70  of token */..  p
329ff 56 74 61 62 20 3d 20 63 75 72 73 6f 72 5f 76 74  Vtab = cursor_vt
32a00 61 62 28 70 43 75 72 29 3b 0a 20 20 6e 43 6f 6c  ab(pCur);.  nCol
32a01 75 6d 6e 20 3d 20 70 56 74 61 62 2d 3e 6e 43 6f  umn = pVtab->nCo
32a02 6c 75 6d 6e 3b 0a 20 20 70 54 6f 6b 65 6e 69 7a  lumn;.  pTokeniz
32a03 65 72 20 3d 20 70 56 74 61 62 2d 3e 70 54 6f 6b  er = pVtab->pTok
32a04 65 6e 69 7a 65 72 3b 0a 20 20 70 54 4d 6f 64 75  enizer;.  pTModu
32a05 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d  le = pTokenizer-
32a06 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 72 63 20 3d  >pModule;.  rc =
32a07 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e   pTModule->xOpen
32a08 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 44 6f  (pTokenizer, zDo
32a09 63 2c 20 6e 44 6f 63 2c 20 26 70 54 43 75 72 73  c, nDoc, &pTCurs
32a0a 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  or);.  if( rc ) 
32a0b 72 65 74 75 72 6e 3b 0a 20 20 70 54 43 75 72 73  return;.  pTCurs
32a0c 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  or->pTokenizer =
32a0d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20   pTokenizer;..  
32a0e 70 72 65 76 4d 61 74 63 68 20 3d 20 30 3b 0a 20  prevMatch = 0;. 
32a0f 20 77 68 69 6c 65 28 20 21 70 54 4d 6f 64 75 6c   while( !pTModul
32a10 65 2d 3e 78 4e 65 78 74 28 70 54 43 75 72 73 6f  e->xNext(pTCurso
32a11 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f  r, &zToken, &nTo
32a12 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c 20 26 69  ken, &iBegin, &i
32a13 45 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20  End, &iPos) ){. 
32a14 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 49 74     Fts3Expr *pIt
32a15 65 72 20 3d 20 70 43 75 72 2d 3e 70 45 78 70 72  er = pCur->pExpr
32a16 3b 0a 20 20 20 20 69 6e 74 20 69 49 74 65 72 20  ;.    int iIter 
32a17 3d 20 2d 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72  = -1;.    iRotor
32a18 42 65 67 69 6e 5b 69 52 6f 74 6f 72 26 46 54 53  Begin[iRotor&FTS
32a19 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20  3_ROTOR_MASK] = 
32a1a 69 42 65 67 69 6e 3b 0a 20 20 20 20 69 52 6f 74  iBegin;.    iRot
32a1b 6f 72 4c 65 6e 5b 69 52 6f 74 6f 72 26 46 54 53  orLen[iRotor&FTS
32a1c 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20  3_ROTOR_MASK] = 
32a1d 69 45 6e 64 2d 69 42 65 67 69 6e 3b 0a 20 20 20  iEnd-iBegin;.   
32a1e 20 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20   match = 0;.    
32a1f 66 6f 72 28 69 3d 30 3b 20 69 3c 28 46 54 53 33  for(i=0; i<(FTS3
32a20 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 20 26 26 20  _ROTOR_SZ-1) && 
32a21 66 74 73 33 4e 65 78 74 45 78 70 72 54 6f 6b 65  fts3NextExprToke
32a22 6e 28 26 70 49 74 65 72 2c 20 26 69 49 74 65 72  n(&pIter, &iIter
32a23 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ); i++){.      i
32a24 6e 74 20 6e 50 68 72 61 73 65 3b 20 20 20 20 20  nt nPhrase;     
32a25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32a26 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  * Number of toke
32a27 6e 73 20 69 6e 20 63 75 72 72 65 6e 74 20 70 68  ns in current ph
32a28 72 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 73 74  rase */.      st
32a29 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e  ruct PhraseToken
32a2a 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20 2f 2a   *pToken;     /*
32a2b 20 43 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a   Current token *
32a2c 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  /.      int iCol
32a2d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
32a2e 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
32a2f 6e 20 69 6e 64 65 78 20 2a 2f 0a 0a 20 20 20 20  n index */..    
32a30 20 20 69 66 28 20 66 74 73 33 45 78 70 72 42 65    if( fts3ExprBe
32a31 6e 65 61 74 68 4e 6f 74 28 70 49 74 65 72 29 20  neathNot(pIter) 
32a32 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
32a33 20 20 6e 50 68 72 61 73 65 20 3d 20 70 49 74 65    nPhrase = pIte
32a34 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  r->pPhrase->nTok
32a35 65 6e 3b 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e  en;.      pToken
32a36 20 3d 20 26 70 49 74 65 72 2d 3e 70 50 68 72 61   = &pIter->pPhra
32a37 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 49 74 65 72  se->aToken[iIter
32a38 5d 3b 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20  ];.      iCol = 
32a39 70 49 74 65 72 2d 3e 70 50 68 72 61 73 65 2d 3e  pIter->pPhrase->
32a3a 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69  iColumn;.      i
32a3b 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  f( iCol>=0 && iC
32a3c 6f 6c 3c 6e 43 6f 6c 75 6d 6e 20 26 26 20 69 43  ol<nColumn && iC
32a3d 6f 6c 21 3d 69 43 6f 6c 75 6d 6e 20 29 20 63 6f  ol!=iColumn ) co
32a3e 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66  ntinue;.      if
32a3f 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 6e 54 6f 6b  ( pToken->n>nTok
32a40 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  en ) continue;. 
32a41 20 20 20 20 20 69 66 28 20 21 70 54 6f 6b 65 6e       if( !pToken
32a42 2d 3e 69 73 50 72 65 66 69 78 20 26 26 20 70 54  ->isPrefix && pT
32a43 6f 6b 65 6e 2d 3e 6e 3c 6e 54 6f 6b 65 6e 20 29  oken->n<nToken )
32a44 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
32a45 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d   assert( pToken-
32a46 3e 6e 3c 3d 6e 54 6f 6b 65 6e 20 29 3b 0a 20 20  >n<=nToken );.  
32a47 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70      if( memcmp(p
32a48 54 6f 6b 65 6e 2d 3e 7a 2c 20 7a 54 6f 6b 65 6e  Token->z, zToken
32a49 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 20 29 20 63  , pToken->n) ) c
32a4a 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69  ontinue;.      i
32a4b 66 28 20 69 49 74 65 72 3e 30 20 26 26 20 28 70  f( iIter>0 && (p
32a4c 72 65 76 4d 61 74 63 68 20 26 20 28 31 3c 3c 69  revMatch & (1<<i
32a4d 29 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  ))==0 ) continue
32a4e 3b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 7c 3d  ;.      match |=
32a4f 20 31 3c 3c 69 3b 0a 20 20 20 20 20 20 69 66 28   1<<i;.      if(
32a50 20 69 3d 3d 28 46 54 53 33 5f 52 4f 54 4f 52 5f   i==(FTS3_ROTOR_
32a51 53 5a 2d 32 29 20 7c 7c 20 6e 50 68 72 61 73 65  SZ-2) || nPhrase
32a52 3d 3d 69 49 74 65 72 2b 31 20 29 7b 0a 20 20 20  ==iIter+1 ){.   
32a53 20 20 20 20 20 66 6f 72 28 6a 3d 6e 50 68 72 61       for(j=nPhra
32a54 73 65 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 29  se-1; j>=0; j--)
32a55 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
32a56 6b 20 3d 20 28 69 52 6f 74 6f 72 2d 6a 29 20 26  k = (iRotor-j) &
32a57 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b   FTS3_ROTOR_MASK
32a58 3b 0a 20 20 20 20 20 20 20 20 20 20 73 6e 69 70  ;.          snip
32a59 70 65 74 41 70 70 65 6e 64 4d 61 74 63 68 28 70  petAppendMatch(p
32a5a 53 6e 69 70 70 65 74 2c 20 69 43 6f 6c 75 6d 6e  Snippet, iColumn
32a5b 2c 20 69 2d 6a 2c 20 69 50 6f 73 2d 6a 2c 0a 20  , i-j, iPos-j,. 
32a5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
32a5d 52 6f 74 6f 72 42 65 67 69 6e 5b 6b 5d 2c 20 69  RotorBegin[k], i
32a5e 52 6f 74 6f 72 4c 65 6e 5b 6b 5d 29 3b 0a 20 20  RotorLen[k]);.  
32a5f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
32a60 20 20 20 20 7d 0a 20 20 20 20 70 72 65 76 4d 61      }.    prevMa
32a61 74 63 68 20 3d 20 6d 61 74 63 68 3c 3c 31 3b 0a  tch = match<<1;.
32a62 20 20 20 20 69 52 6f 74 6f 72 2b 2b 3b 0a 20 20      iRotor++;.  
32a63 7d 0a 20 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 43  }.  pTModule->xC
32a64 6c 6f 73 65 28 70 54 43 75 72 73 6f 72 29 3b 20  lose(pTCursor); 
32a65 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76   .}../*.** Remov
32a66 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74  e entries from t
32a67 68 65 20 70 53 6e 69 70 70 65 74 20 73 74 72 75  he pSnippet stru
32a68 63 74 75 72 65 20 74 6f 20 61 63 63 6f 75 6e 74  cture to account
32a69 20 66 6f 72 20 74 68 65 20 4e 45 41 52 0a 2a 2a   for the NEAR.**
32a6a 20 6f 70 65 72 61 74 6f 72 2e 20 57 68 65 6e 20   operator. When 
32a6b 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20  this is called, 
32a6c 70 53 6e 69 70 70 65 74 20 63 6f 6e 74 61 69 6e  pSnippet contain
32a6d 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 74 6f  s the list of to
32a6e 6b 65 6e 20 0a 2a 2a 20 6f 66 66 73 65 74 73 20  ken .** offsets 
32a6f 70 72 6f 64 75 63 65 64 20 62 79 20 74 72 65 61  produced by trea
32a70 74 69 6e 67 20 61 6c 6c 20 4e 45 41 52 20 6f 70  ting all NEAR op
32a71 65 72 61 74 6f 72 73 20 61 73 20 41 4e 44 20 6f  erators as AND o
32a72 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 20 54 68 69  perators..** Thi
32a73 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76  s function remov
32a74 65 73 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74  es any entries t
32a75 68 61 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  hat should not b
32a76 65 20 70 72 65 73 65 6e 74 20 61 66 74 65 72 0a  e present after.
32a77 2a 2a 20 61 63 63 6f 75 6e 74 69 6e 67 20 66 6f  ** accounting fo
32a78 72 20 74 68 65 20 4e 45 41 52 20 72 65 73 74 72  r the NEAR restr
32a79 69 63 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d  iction. For exam
32a7a 70 6c 65 2c 20 69 66 20 74 68 65 20 71 75 65 72  ple, if the quer
32a7b 69 65 64 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20  ied.** document 
32a7c 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 41  is:.**.**     "A
32a7d 20 42 20 43 20 44 20 45 20 41 22 0a 2a 2a 0a 2a   B C D E A".**.*
32a7e 2a 20 61 6e 64 20 74 68 65 20 71 75 65 72 79 20  * and the query 
32a7f 69 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 41  is:.** .**     A
32a80 20 4e 45 41 52 2f 30 20 45 0a 2a 2a 0a 2a 2a 20   NEAR/0 E.**.** 
32a81 74 68 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66  then when this f
32a82 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
32a83 64 20 74 68 65 20 53 6e 69 70 70 65 74 20 63 6f  d the Snippet co
32a84 6e 74 61 69 6e 73 20 74 6f 6b 65 6e 20 6f 66 66  ntains token off
32a85 73 65 74 73 0a 2a 2a 20 30 2c 20 34 20 61 6e 64  sets.** 0, 4 and
32a86 20 35 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f   5. This functio
32a87 6e 20 72 65 6d 6f 76 65 73 20 74 68 65 20 22 30  n removes the "0
32a88 22 20 65 6e 74 72 79 20 28 62 65 63 61 75 73 65  " entry (because
32a89 20 74 68 65 20 66 69 72 73 74 20 41 0a 2a 2a 20   the first A.** 
32a8a 69 73 20 6e 6f 74 20 6e 65 61 72 20 65 6e 6f 75  is not near enou
32a8b 67 68 20 74 6f 20 61 6e 20 45 29 2e 0a 2a 2a 0a  gh to an E)..**.
32a8c 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e  ** When this fun
32a8d 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
32a8e 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74   the value point
32a8f 65 64 20 74 6f 20 62 79 20 70 61 72 61 6d 65 74  ed to by paramet
32a90 65 72 20 70 69 4c 65 66 74 20 69 73 0a 2a 2a 20  er piLeft is.** 
32a91 74 68 65 20 69 6e 74 65 67 65 72 20 69 64 20 6f  the integer id o
32a92 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  f the left-most 
32a93 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70  token in the exp
32a94 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65 61  ression tree hea
32a95 64 65 64 20 62 79 0a 2a 2a 20 70 45 78 70 72 2e  ded by.** pExpr.
32a96 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
32a97 6e 63 72 65 6d 65 6e 74 73 20 2a 70 69 4c 65 66  ncrements *piLef
32a98 74 20 62 79 20 74 68 65 20 74 6f 74 61 6c 20 6e  t by the total n
32a99 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 0a  umber of tokens.
32a9a 2a 2a 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  ** in the expres
32a9b 73 69 6f 6e 20 74 72 65 65 20 68 65 61 64 65 64  sion tree headed
32a9c 20 62 79 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a   by pExpr..**.**
32a9d 20 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79   Return 1 if any
32a9e 20 74 72 69 6d 6d 69 6e 67 20 6f 63 63 75 72 73   trimming occurs
32a9f 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e  .  Return 0 if n
32aa0 6f 20 74 72 69 6d 6d 69 6e 67 20 69 73 20 72 65  o trimming is re
32aa1 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  quired..*/.stati
32aa2 63 20 69 6e 74 20 74 72 69 6d 53 6e 69 70 70 65  c int trimSnippe
32aa3 74 4f 66 66 73 65 74 73 28 0a 20 20 46 74 73 33  tOffsets(.  Fts3
32aa4 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
32aa5 20 20 2f 2a 20 54 68 65 20 73 65 61 72 63 68 20    /* The search 
32aa6 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
32aa7 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
32aa8 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 73 65 74  t,    /* The set
32aa9 20 6f 66 20 73 6e 69 70 70 65 74 20 6f 66 66 73   of snippet offs
32aaa 65 74 73 20 74 6f 20 62 65 20 74 72 69 6d 6d 65  ets to be trimme
32aab 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 4c 65  d */.  int *piLe
32aac 66 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ft           /* 
32aad 49 6e 64 65 78 20 6f 66 20 6c 65 66 74 2d 6d 6f  Index of left-mo
32aae 73 74 20 74 6f 6b 65 6e 20 69 6e 20 70 45 78 70  st token in pExp
32aaf 72 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45  r */.){.  if( pE
32ab0 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 74  xpr ){.    if( t
32ab1 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74  rimSnippetOffset
32ab2 73 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  s(pExpr->pLeft, 
32ab3 70 53 6e 69 70 70 65 74 2c 20 70 69 4c 65 66 74  pSnippet, piLeft
32ab4 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
32ab5 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  n 1;.    }..    
32ab6 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65  switch( pExpr->e
32ab7 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61  Type ){.      ca
32ab8 73 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41  se FTSQUERY_PHRA
32ab9 53 45 3a 0a 20 20 20 20 20 20 20 20 2a 70 69 4c  SE:.        *piL
32aba 65 66 74 20 2b 3d 20 70 45 78 70 72 2d 3e 70 50  eft += pExpr->pP
32abb 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20  hrase->nToken;. 
32abc 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32abd 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
32abe 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20  Y_NEAR: {.      
32abf 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68    /* The right-h
32ac0 61 6e 64 2d 73 69 64 65 20 6f 66 20 61 20 4e 45  and-side of a NE
32ac1 41 52 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61  AR operator is a
32ac2 6c 77 61 79 73 20 61 20 70 68 72 61 73 65 2e 20  lways a phrase. 
32ac3 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c  The.        ** l
32ac4 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 69 73  eft-hand-side is
32ac5 20 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65   either a phrase
32ac6 20 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f   or an expressio
32ac7 6e 20 74 72 65 65 20 74 68 61 74 20 69 73 20 0a  n tree that is .
32ac8 20 20 20 20 20 20 20 20 2a 2a 20 69 74 73 65 6c          ** itsel
32ac9 66 20 68 65 61 64 65 64 20 62 79 20 61 20 4e 45  f headed by a NE
32aca 41 52 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 65  AR operator. The
32acb 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 69 74 69   following initi
32acc 61 6c 69 7a 61 74 69 6f 6e 73 0a 20 20 20 20 20  alizations.     
32acd 20 20 20 2a 2a 20 73 65 74 20 6c 6f 63 61 6c 20     ** set local 
32ace 76 61 72 69 61 62 6c 65 20 69 4c 65 66 74 20 74  variable iLeft t
32acf 6f 20 74 68 65 20 74 6f 6b 65 6e 20 6e 75 6d 62  o the token numb
32ad0 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 6d  er of the left-m
32ad1 6f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ost.        ** t
32ad2 6f 6b 65 6e 20 69 6e 20 74 68 65 20 72 69 67 68  oken in the righ
32ad3 74 2d 68 61 6e 64 20 70 68 72 61 73 65 2c 20 61  t-hand phrase, a
32ad4 6e 64 20 69 52 69 67 68 74 20 74 6f 20 74 68 65  nd iRight to the
32ad5 20 72 69 67 68 74 20 6d 6f 73 74 0a 20 20 20 20   right most.    
32ad6 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20      ** token in 
32ad7 74 68 65 20 73 61 6d 65 20 70 68 72 61 73 65 2e  the same phrase.
32ad8 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
32ad9 20 77 65 20 68 61 64 3a 0a 20 20 20 20 20 20 20   we had:.       
32ada 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   **.        **  
32adb 20 20 20 3c 63 6f 6c 3e 20 4d 41 54 43 48 20 27     <col> MATCH '
32adc 22 61 62 63 20 64 65 66 22 20 4e 45 41 52 2f 32  "abc def" NEAR/2
32add 20 22 67 68 69 20 6a 6b 6c 22 27 0a 20 20 20 20   "ghi jkl"'.    
32ade 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
32adf 2a 20 74 68 65 6e 20 69 4c 65 66 74 20 77 69 6c  * then iLeft wil
32ae0 6c 20 62 65 20 73 65 74 20 74 6f 20 32 20 28 74  l be set to 2 (t
32ae1 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 6f 66 20 67  oken number of g
32ae2 68 69 29 20 61 6e 64 20 6e 54 6f 6b 65 6e 20 77  hi) and nToken w
32ae3 69 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 62  ill.        ** b
32ae4 65 20 73 65 74 20 74 6f 20 34 2e 0a 20 20 20 20  e set to 4..    
32ae5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 46      */.        F
32ae6 74 73 33 45 78 70 72 20 2a 70 4c 65 66 74 20 3d  ts3Expr *pLeft =
32ae7 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
32ae8 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20         Fts3Expr 
32ae9 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  *pRight = pExpr-
32aea 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  >pRight;.       
32aeb 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 2a 70 69   int iLeft = *pi
32aec 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 6e  Left;.        in
32aed 74 20 6e 4e 65 61 72 20 3d 20 70 45 78 70 72 2d  t nNear = pExpr-
32aee 3e 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20 20 20  >nNear;.        
32aef 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 70 52 69  int nToken = pRi
32af0 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  ght->pPhrase->nT
32af1 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e  oken;.        in
32af2 74 20 6a 6a 2c 20 69 69 3b 0a 20 20 20 20 20 20  t jj, ii;.      
32af3 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 65 54 79    if( pLeft->eTy
32af4 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41  pe==FTSQUERY_NEA
32af5 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  R ){.          p
32af6 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 52  Left = pLeft->pR
32af7 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ight;.        }.
32af8 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
32af9 70 52 69 67 68 74 2d 3e 65 54 79 70 65 3d 3d 46  pRight->eType==F
32afa 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
32afb 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
32afc 28 20 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d  ( pLeft->eType==
32afd 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
32afe 29 3b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65  );.        nToke
32aff 6e 20 2b 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72  n += pLeft->pPhr
32b00 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 0a 20 20  ase->nToken;..  
32b01 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20        for(ii=0; 
32b02 69 69 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61  ii<pSnippet->nMa
32b03 74 63 68 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  tch; ii++){.    
32b04 20 20 20 20 20 20 73 74 72 75 63 74 20 73 6e 69        struct sni
32b05 70 70 65 74 4d 61 74 63 68 20 2a 70 20 3d 20 26  ppetMatch *p = &
32b06 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
32b07 5b 69 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  [ii];.          
32b08 69 66 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 69 4c  if( p->iTerm==iL
32b09 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  eft ){.         
32b0a 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30 3b     int isOk = 0;
32b0b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
32b0c 53 6e 69 70 70 65 74 20 69 69 20 69 73 20 61 6e  Snippet ii is an
32b0d 20 6f 63 63 75 72 65 6e 63 65 20 6f 66 20 71 75   occurence of qu
32b0e 65 72 79 20 74 65 72 6d 20 69 4c 65 66 74 20 69  ery term iLeft i
32b0f 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e 0a  n the document..
32b10 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49              ** I
32b11 74 20 6f 63 63 75 72 73 20 61 74 20 70 6f 73 69  t occurs at posi
32b12 74 69 6f 6e 20 28 70 2d 3e 69 54 6f 6b 65 6e 29  tion (p->iToken)
32b13 20 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e 74   of the document
32b14 2e 20 57 65 20 6e 6f 77 0a 20 20 20 20 20 20 20  . We now.       
32b15 20 20 20 20 20 2a 2a 20 73 65 61 72 63 68 20 66       ** search f
32b16 6f 72 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  or an instance o
32b17 66 20 74 6f 6b 65 6e 20 28 69 4c 65 66 74 2d 31  f token (iLeft-1
32b18 29 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74  ) somewhere in t
32b19 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  he .            
32b1a 2a 2a 20 72 61 6e 67 65 20 28 70 2d 3e 69 54 6f  ** range (p->iTo
32b1b 6b 65 6e 20 2d 20 6e 4e 65 61 72 29 2e 2e 2e 28  ken - nNear)...(
32b1c 70 2d 3e 69 54 6f 6b 65 6e 20 2b 20 6e 4e 65 61  p->iToken + nNea
32b1d 72 20 2b 20 6e 54 6f 6b 65 6e 29 20 77 69 74 68  r + nToken) with
32b1e 69 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  in .            
32b1f 2a 2a 20 74 68 65 20 73 65 74 20 6f 66 20 73 6e  ** the set of sn
32b20 69 70 70 65 74 4d 61 74 63 68 20 73 74 72 75 63  ippetMatch struc
32b21 74 75 72 65 73 2e 20 49 66 20 6f 6e 65 20 69 73  tures. If one is
32b22 20 66 6f 75 6e 64 2c 20 70 72 6f 63 65 65 64 2e   found, proceed.
32b23 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a   .            **
32b24 20 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62   If one cannot b
32b25 65 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 72 65  e found, then re
32b26 6d 6f 76 65 20 73 6e 69 70 70 65 74 73 20 69 69  move snippets ii
32b27 2e 2e 28 69 69 2b 4e 2d 31 29 20 0a 20 20 20 20  ..(ii+N-1) .    
32b28 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20          ** from 
32b29 74 68 65 20 6d 61 74 63 68 69 6e 67 20 73 6e 69  the matching sni
32b2a 70 70 65 74 73 2c 20 77 68 65 72 65 20 4e 20 69  ppets, where N i
32b2b 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
32b2c 74 6f 6b 65 6e 73 20 0a 20 20 20 20 20 20 20 20  tokens .        
32b2d 20 20 20 20 2a 2a 20 69 6e 20 70 68 72 61 73 65      ** in phrase
32b2e 20 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65   pRight->pPhrase
32b2f 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  ..            */
32b30 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72  .            for
32b31 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20 26  (jj=0; isOk==0 &
32b32 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e 6e  & jj<pSnippet->n
32b33 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20 20  Match; jj++){.  
32b34 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72 75              stru
32b35 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20  ct snippetMatch 
32b36 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74 2d  *p2 = &pSnippet-
32b37 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20 20  >aMatch[jj];.   
32b38 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
32b39 32 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66 74  2->iTerm==(iLeft
32b3a 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  -1) ){.         
32b3b 20 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69         if( p2->i
32b3c 54 6f 6b 65 6e 3e 3d 28 70 2d 3e 69 54 6f 6b 65  Token>=(p->iToke
32b3d 6e 2d 6e 4e 65 61 72 2d 31 29 20 0a 20 20 20 20  n-nNear-1) .    
32b3e 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
32b3f 70 32 2d 3e 69 54 6f 6b 65 6e 3c 28 70 2d 3e 69  p2->iToken<(p->i
32b40 54 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 6e 54 6f 6b  Token+nNear+nTok
32b41 65 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  en) .           
32b42 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
32b43 20 20 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d            isOk =
32b44 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
32b45 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
32b46 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
32b47 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
32b48 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20  if( !isOk ){.   
32b49 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b             int k
32b4a 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
32b4b 20 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 52   for(kk=0; kk<pR
32b4c 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  ight->pPhrase->n
32b4d 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20  Token; kk++){.  
32b4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53                pS
32b4f 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6b  nippet->aMatch[k
32b50 6b 2b 69 69 5d 2e 69 54 65 72 6d 20 3d 20 2d 32  k+ii].iTerm = -2
32b51 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
32b52 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
32b53 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
32b54 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
32b55 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
32b56 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65  ( p->iTerm==(iLe
32b57 66 74 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  ft-1) ){.       
32b58 20 20 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20       int isOk = 
32b59 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  0;.            f
32b5a 6f 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30  or(jj=0; isOk==0
32b5b 20 26 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d   && jj<pSnippet-
32b5c 3e 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a  >nMatch; jj++){.
32b5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
32b5e 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
32b5f 68 20 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65  h *p2 = &pSnippe
32b60 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20  t->aMatch[jj];. 
32b61 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
32b62 20 70 32 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65 66   p2->iTerm==iLef
32b63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
32b64 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54 6f       if( p2->iTo
32b65 6b 65 6e 3c 3d 28 70 2d 3e 69 54 6f 6b 65 6e 2b  ken<=(p->iToken+
32b66 6e 4e 65 61 72 2b 31 29 20 0a 20 20 20 20 20 20  nNear+1) .      
32b67 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 32             && p2
32b68 2d 3e 69 54 6f 6b 65 6e 3e 28 70 2d 3e 69 54 6f  ->iToken>(p->iTo
32b69 6b 65 6e 2d 6e 4e 65 61 72 2d 6e 54 6f 6b 65 6e  ken-nNear-nToken
32b6a 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ) .             
32b6b 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
32b6c 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20 31          isOk = 1
32b6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
32b6e 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
32b6f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
32b70 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  }.            if
32b71 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20 20  ( !isOk ){.     
32b72 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b           int kk;
32b73 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66  .              f
32b74 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 4c 65 66  or(kk=0; kk<pLef
32b75 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  t->pPhrase->nTok
32b76 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20  en; kk++){.     
32b77 20 20 20 20 20 20 20 20 20 20 20 70 53 6e 69 70             pSnip
32b78 70 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 2d 6b  pet->aMatch[ii-k
32b79 6b 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b 0a 20  k].iTerm = -2;. 
32b7a 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
32b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
32b7c 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20  urn 1;.         
32b7d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
32b7e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
32b7f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32b80 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  }.    }..    if(
32b81 20 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73   trimSnippetOffs
32b82 65 74 73 28 70 45 78 70 72 2d 3e 70 52 69 67 68  ets(pExpr->pRigh
32b83 74 2c 20 70 53 6e 69 70 70 65 74 2c 20 70 69 4c  t, pSnippet, piL
32b84 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  eft) ){.      re
32b85 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
32b86 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
32b87 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61  ./*.** Compute a
32b88 6c 6c 20 6f 66 66 73 65 74 73 20 66 6f 72 20 74  ll offsets for t
32b89 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f  he current row o
32b8a 66 20 74 68 65 20 71 75 65 72 79 2e 20 20 0a 2a  f the query.  .*
32b8b 2a 20 49 66 20 74 68 65 20 6f 66 66 73 65 74 73  * If the offsets
32b8c 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
32b8d 65 6e 20 63 6f 6d 70 75 74 65 64 2c 20 74 68 69  en computed, thi
32b8e 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
32b8f 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
32b90 76 6f 69 64 20 73 6e 69 70 70 65 74 41 6c 6c 4f  void snippetAllO
32b91 66 66 73 65 74 73 28 66 75 6c 6c 74 65 78 74 5f  ffsets(fulltext_
32b92 63 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 6e  cursor *p){.  in
32b93 74 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74  t nColumn;.  int
32b94 20 69 43 6f 6c 75 6d 6e 2c 20 69 3b 0a 20 20 69   iColumn, i;.  i
32b95 6e 74 20 69 46 69 72 73 74 2c 20 69 4c 61 73 74  nt iFirst, iLast
32b96 3b 0a 20 20 69 6e 74 20 69 54 65 72 6d 20 3d 20  ;.  int iTerm = 
32b97 30 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  0;.  fulltext_vt
32b98 61 62 20 2a 70 46 74 73 20 3d 20 63 75 72 73 6f  ab *pFts = curso
32b99 72 5f 76 74 61 62 28 70 29 3b 0a 0a 20 20 69 66  r_vtab(p);..  if
32b9a 28 20 70 2d 3e 73 6e 69 70 70 65 74 2e 6e 4d 61  ( p->snippet.nMa
32b9b 74 63 68 20 7c 7c 20 70 2d 3e 70 45 78 70 72 3d  tch || p->pExpr=
32b9c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
32b9d 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c 75 6d 6e 20  ;.  }.  nColumn 
32b9e 3d 20 70 46 74 73 2d 3e 6e 43 6f 6c 75 6d 6e 3b  = pFts->nColumn;
32b9f 0a 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 28 70 2d  .  iColumn = (p-
32ba0 3e 69 43 75 72 73 6f 72 54 79 70 65 20 2d 20 51  >iCursorType - Q
32ba1 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 29 3b 0a  UERY_FULLTEXT);.
32ba2 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20    if( iColumn<0 
32ba3 7c 7c 20 69 43 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c  || iColumn>=nCol
32ba4 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 6f  umn ){.    /* Lo
32ba5 6f 6b 20 66 6f 72 20 6d 61 74 63 68 65 73 20 6f  ok for matches o
32ba6 76 65 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20  ver all columns 
32ba7 6f 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74  of the full-text
32ba8 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 46   index */.    iF
32ba9 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 4c  irst = 0;.    iL
32baa 61 73 74 20 3d 20 6e 43 6f 6c 75 6d 6e 2d 31 3b  ast = nColumn-1;
32bab 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
32bac 20 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74 63 68 65   Look for matche
32bad 73 20 69 6e 20 74 68 65 20 69 43 6f 6c 75 6d 6e  s in the iColumn
32bae 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  -th column of th
32baf 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2a 2f 0a  e index only */.
32bb0 20 20 20 20 69 46 69 72 73 74 20 3d 20 69 43 6f      iFirst = iCo
32bb1 6c 75 6d 6e 3b 0a 20 20 20 20 69 4c 61 73 74 20  lumn;.    iLast 
32bb2 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20  = iColumn;.  }. 
32bb3 20 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20 69   for(i=iFirst; i
32bb4 3c 3d 69 4c 61 73 74 3b 20 69 2b 2b 29 7b 0a 20  <=iLast; i++){. 
32bb5 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
32bb6 44 6f 63 3b 0a 20 20 20 20 69 6e 74 20 6e 44 6f  Doc;.    int nDo
32bb7 63 3b 0a 20 20 20 20 7a 44 6f 63 20 3d 20 28 63  c;.    zDoc = (c
32bb8 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74  onst char*)sqlit
32bb9 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70  e3_column_text(p
32bba 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20  ->pStmt, i+1);. 
32bbb 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65     nDoc = sqlite
32bbc 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70  3_column_bytes(p
32bbd 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20  ->pStmt, i+1);. 
32bbe 20 20 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74     snippetOffset
32bbf 73 4f 66 43 6f 6c 75 6d 6e 28 70 2c 20 26 70 2d  sOfColumn(p, &p-
32bc0 3e 73 6e 69 70 70 65 74 2c 20 69 2c 20 7a 44 6f  >snippet, i, zDo
32bc1 63 2c 20 6e 44 6f 63 29 3b 0a 20 20 7d 0a 0a 20  c, nDoc);.  }.. 
32bc2 20 77 68 69 6c 65 28 20 74 72 69 6d 53 6e 69 70   while( trimSnip
32bc3 70 65 74 4f 66 66 73 65 74 73 28 70 2d 3e 70 45  petOffsets(p->pE
32bc4 78 70 72 2c 20 26 70 2d 3e 73 6e 69 70 70 65 74  xpr, &p->snippet
32bc5 2c 20 26 69 54 65 72 6d 29 20 29 7b 0a 20 20 20  , &iTerm) ){.   
32bc6 20 69 54 65 72 6d 20 3d 20 30 3b 0a 20 20 7d 0a   iTerm = 0;.  }.
32bc7 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
32bc8 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   the information
32bc9 20 69 6e 20 74 68 65 20 61 4d 61 74 63 68 5b 5d   in the aMatch[]
32bca 20 61 72 72 61 79 20 6f 66 20 74 68 65 20 73 6e   array of the sn
32bcb 69 70 70 65 74 0a 2a 2a 20 69 6e 74 6f 20 74 68  ippet.** into th
32bcc 65 20 73 74 72 69 6e 67 20 7a 4f 66 66 73 65 74  e string zOffset
32bcd 5b 30 2e 2e 6e 4f 66 66 73 65 74 2d 31 5d 2e 20  [0..nOffset-1]. 
32bce 54 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 75  This string is u
32bcf 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 72 65  sed as.** the re
32bd0 74 75 72 6e 20 6f 66 20 74 68 65 20 53 51 4c 20  turn of the SQL 
32bd1 6f 66 66 73 65 74 73 28 29 20 66 75 6e 63 74 69  offsets() functi
32bd2 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
32bd3 69 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74  id snippetOffset
32bd4 54 65 78 74 28 53 6e 69 70 70 65 74 20 2a 70 29  Text(Snippet *p)
32bd5 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
32bd6 20 63 6e 74 20 3d 20 30 3b 0a 20 20 53 74 72 69   cnt = 0;.  Stri
32bd7 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 63  ngBuffer sb;.  c
32bd8 68 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a 20  har zBuf[200];. 
32bd9 20 69 66 28 20 70 2d 3e 7a 4f 66 66 73 65 74 20   if( p->zOffset 
32bda 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 6e 69 74  ) return;.  init
32bdb 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62  StringBuffer(&sb
32bdc 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
32bdd 70 2d 3e 6e 4d 61 74 63 68 3b 20 69 2b 2b 29 7b  p->nMatch; i++){
32bde 0a 20 20 20 20 73 74 72 75 63 74 20 73 6e 69 70  .    struct snip
32bdf 70 65 74 4d 61 74 63 68 20 2a 70 4d 61 74 63 68  petMatch *pMatch
32be0 20 3d 20 26 70 2d 3e 61 4d 61 74 63 68 5b 69 5d   = &p->aMatch[i]
32be1 3b 0a 20 20 20 20 69 66 28 20 70 4d 61 74 63 68  ;.    if( pMatch
32be2 2d 3e 69 54 65 72 6d 3e 3d 30 20 29 7b 0a 20 20  ->iTerm>=0 ){.  
32be3 20 20 20 20 2f 2a 20 49 66 20 73 6e 69 70 70 65      /* If snippe
32be4 74 4d 61 74 63 68 2e 69 54 65 72 6d 20 69 73 20  tMatch.iTerm is 
32be5 6c 65 73 73 20 74 68 61 6e 20 30 2c 20 74 68 65  less than 0, the
32be6 6e 20 74 68 65 20 6d 61 74 63 68 20 77 61 73 20  n the match was 
32be7 0a 20 20 20 20 20 20 2a 2a 20 64 69 73 63 61 72  .      ** discar
32be8 64 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 70  ded as part of p
32be9 72 6f 63 65 73 73 69 6e 67 20 74 68 65 20 4e 45  rocessing the NE
32bea 41 52 20 6f 70 65 72 61 74 6f 72 20 28 73 65 65  AR operator (see
32beb 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74   the .      ** t
32bec 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74  rimSnippetOffset
32bed 73 46 6f 72 4e 65 61 72 28 29 20 66 75 6e 63 74  sForNear() funct
32bee 69 6f 6e 20 66 6f 72 20 64 65 74 61 69 6c 73 29  ion for details)
32bef 2e 20 49 67 6e 6f 72 65 20 0a 20 20 20 20 20 20  . Ignore .      
32bf0 2a 2a 20 69 74 20 69 6e 20 74 68 69 73 20 63 61  ** it in this ca
32bf1 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  se.      */.    
32bf2 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b    zBuf[0] = ' ';
32bf3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
32bf4 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
32bf5 42 75 66 29 2d 31 2c 20 26 7a 42 75 66 5b 63 6e  Buf)-1, &zBuf[cn
32bf6 74 3e 30 5d 2c 20 22 25 64 20 25 64 20 25 64 20  t>0], "%d %d %d 
32bf7 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  %d",.          p
32bf8 4d 61 74 63 68 2d 3e 69 43 6f 6c 2c 20 70 4d 61  Match->iCol, pMa
32bf9 74 63 68 2d 3e 69 54 65 72 6d 2c 20 70 4d 61 74  tch->iTerm, pMat
32bfa 63 68 2d 3e 69 53 74 61 72 74 2c 20 70 4d 61 74  ch->iStart, pMat
32bfb 63 68 2d 3e 6e 42 79 74 65 29 3b 0a 20 20 20 20  ch->nByte);.    
32bfc 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 42    append(&sb, zB
32bfd 75 66 29 3b 0a 20 20 20 20 20 20 63 6e 74 2b 2b  uf);.      cnt++
32bfe 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 2d  ;.    }.  }.  p-
32bff 3e 7a 4f 66 66 73 65 74 20 3d 20 73 74 72 69 6e  >zOffset = strin
32c00 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29  gBufferData(&sb)
32c01 3b 0a 20 20 70 2d 3e 6e 4f 66 66 73 65 74 20 3d  ;.  p->nOffset =
32c02 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e   stringBufferLen
32c03 67 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 0a  gth(&sb);.}../*.
32c04 2a 2a 20 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d  ** zDoc[0..nDoc-
32c05 31 5d 20 69 73 20 70 68 72 61 73 65 20 6f 66 20  1] is phrase of 
32c06 74 65 78 74 2e 20 20 61 4d 61 74 63 68 5b 30 2e  text.  aMatch[0.
32c07 2e 6e 4d 61 74 63 68 2d 31 5d 20 61 72 65 20 61  .nMatch-1] are a
32c08 20 73 65 74 0a 2a 2a 20 6f 66 20 6d 61 74 63 68   set.** of match
32c09 69 6e 67 20 77 6f 72 64 73 20 73 6f 6d 65 20 6f  ing words some o
32c0a 66 20 77 68 69 63 68 20 6d 69 67 68 74 20 62 65  f which might be
32c0b 20 69 6e 20 7a 44 6f 63 2e 20 20 7a 44 6f 63 20   in zDoc.  zDoc 
32c0c 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d  is column.** num
32c0d 62 65 72 20 69 43 6f 6c 2e 0a 2a 2a 0a 2a 2a 20  ber iCol..**.** 
32c0e 69 42 72 65 61 6b 20 69 73 20 73 75 67 67 65 73  iBreak is sugges
32c0f 74 65 64 20 73 70 6f 74 20 69 6e 20 7a 44 6f 63  ted spot in zDoc
32c10 20 77 68 65 72 65 20 77 65 20 63 6f 75 6c 64 20   where we could 
32c11 62 65 67 69 6e 20 6f 72 20 65 6e 64 20 61 6e 0a  begin or end an.
32c12 2a 2a 20 65 78 63 65 72 70 74 2e 20 20 52 65 74  ** excerpt.  Ret
32c13 75 72 6e 20 61 20 76 61 6c 75 65 20 73 69 6d 69  urn a value simi
32c14 6c 61 72 20 74 6f 20 69 42 72 65 61 6b 20 62 75  lar to iBreak bu
32c15 74 20 70 6f 73 73 69 62 6c 79 20 61 64 6a 75 73  t possibly adjus
32c16 74 65 64 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6c  ted.** to be a l
32c17 69 74 74 6c 65 20 6c 65 66 74 20 6f 72 20 72 69  ittle left or ri
32c18 67 68 74 20 73 6f 20 74 68 61 74 20 74 68 65 20  ght so that the 
32c19 62 72 65 61 6b 20 70 6f 69 6e 74 20 69 73 20 62  break point is b
32c1a 65 74 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  etter..*/.static
32c1b 20 69 6e 74 20 77 6f 72 64 42 6f 75 6e 64 61 72   int wordBoundar
32c1c 79 28 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 2c  y(.  int iBreak,
32c1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c1e 20 20 20 2f 2a 20 54 68 65 20 73 75 67 67 65 73     /* The sugges
32c1f 74 65 64 20 62 72 65 61 6b 20 70 6f 69 6e 74 20  ted break point 
32c20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
32c21 2a 7a 44 6f 63 2c 20 20 20 20 20 20 20 20 20 20  *zDoc,          
32c22 20 20 20 2f 2a 20 44 6f 63 75 6d 65 6e 74 20 74     /* Document t
32c23 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f  ext */.  int nDo
32c24 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
32c25 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
32c26 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 44 6f   of bytes in zDo
32c27 63 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  c[] */.  struct 
32c28 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 61 4d  snippetMatch *aM
32c29 61 74 63 68 2c 20 20 2f 2a 20 4d 61 74 63 68 69  atch,  /* Matchi
32c2a 6e 67 20 77 6f 72 64 73 20 2a 2f 0a 20 20 69 6e  ng words */.  in
32c2b 74 20 6e 4d 61 74 63 68 2c 20 20 20 20 20 20 20  t nMatch,       
32c2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
32c2d 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
32c2e 20 69 6e 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a   in aMatch[] */.
32c2f 20 20 69 6e 74 20 69 43 6f 6c 20 20 20 20 20 20    int iCol      
32c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32c31 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 75  /* The column nu
32c32 6d 62 65 72 20 66 6f 72 20 7a 44 6f 63 5b 5d 20  mber for zDoc[] 
32c33 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
32c34 20 69 66 28 20 69 42 72 65 61 6b 3c 3d 31 30 20   if( iBreak<=10 
32c35 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
32c36 0a 20 20 7d 0a 20 20 69 66 28 20 69 42 72 65 61  .  }.  if( iBrea
32c37 6b 3e 3d 6e 44 6f 63 2d 31 30 20 29 7b 0a 20 20  k>=nDoc-10 ){.  
32c38 20 20 72 65 74 75 72 6e 20 6e 44 6f 63 3b 0a 20    return nDoc;. 
32c39 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
32c3a 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68  nMatch && aMatch
32c3b 5b 69 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 3b 20 69  [i].iCol<iCol; i
32c3c 2b 2b 29 7b 7d 0a 20 20 77 68 69 6c 65 28 20 69  ++){}.  while( i
32c3d 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63  <nMatch && aMatc
32c3e 68 5b 69 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74  h[i].iStart+aMat
32c3f 63 68 5b 69 5d 2e 6e 42 79 74 65 3c 69 42 72 65  ch[i].nByte<iBre
32c40 61 6b 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 69  ak ){ i++; }.  i
32c41 66 28 20 69 3c 6e 4d 61 74 63 68 20 29 7b 0a 20  f( i<nMatch ){. 
32c42 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 69 5d     if( aMatch[i]
32c43 2e 69 53 74 61 72 74 3c 69 42 72 65 61 6b 2b 31  .iStart<iBreak+1
32c44 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
32c45 6e 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  n aMatch[i].iSta
32c46 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
32c47 28 20 69 3e 30 20 26 26 20 61 4d 61 74 63 68 5b  ( i>0 && aMatch[
32c48 69 2d 31 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74  i-1].iStart+aMat
32c49 63 68 5b 69 2d 31 5d 2e 6e 42 79 74 65 3e 3d 69  ch[i-1].nByte>=i
32c4a 42 72 65 61 6b 20 29 7b 0a 20 20 20 20 20 20 72  Break ){.      r
32c4b 65 74 75 72 6e 20 61 4d 61 74 63 68 5b 69 2d 31  eturn aMatch[i-1
32c4c 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a  ].iStart;.    }.
32c4d 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69    }.  for(i=1; i
32c4e 3c 3d 31 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  <=10; i++){.    
32c4f 69 66 28 20 73 61 66 65 5f 69 73 73 70 61 63 65  if( safe_isspace
32c50 28 7a 44 6f 63 5b 69 42 72 65 61 6b 2d 69 5d 29  (zDoc[iBreak-i])
32c51 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
32c52 20 69 42 72 65 61 6b 20 2d 20 69 20 2b 20 31 3b   iBreak - i + 1;
32c53 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
32c54 61 66 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63  afe_isspace(zDoc
32c55 5b 69 42 72 65 61 6b 2b 69 5d 29 20 29 7b 0a 20  [iBreak+i]) ){. 
32c56 20 20 20 20 20 72 65 74 75 72 6e 20 69 42 72 65       return iBre
32c57 61 6b 20 2b 20 69 20 2b 20 31 3b 0a 20 20 20 20  ak + i + 1;.    
32c58 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  }.  }.  return i
32c59 42 72 65 61 6b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a  Break;.}..../*.*
32c5a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
32c5b 20 66 6f 72 20 53 6e 69 70 70 65 74 2e 61 4d 61   for Snippet.aMa
32c5c 74 63 68 5b 5d 2e 73 6e 53 74 61 74 75 73 0a 2a  tch[].snStatus.*
32c5d 2f 0a 23 64 65 66 69 6e 65 20 53 4e 49 50 50 45  /.#define SNIPPE
32c5e 54 5f 49 47 4e 4f 52 45 20 20 30 20 20 20 2f 2a  T_IGNORE  0   /*
32c5f 20 49 74 20 69 73 20 6f 6b 20 74 6f 20 6f 6d 69   It is ok to omi
32c60 74 20 74 68 69 73 20 6d 61 74 63 68 20 66 72 6f  t this match fro
32c61 6d 20 74 68 65 20 73 6e 69 70 70 65 74 20 2a 2f  m the snippet */
32c62 0a 23 64 65 66 69 6e 65 20 53 4e 49 50 50 45 54  .#define SNIPPET
32c63 5f 44 45 53 49 52 45 44 20 31 20 20 20 2f 2a 20  _DESIRED 1   /* 
32c64 57 65 20 77 61 6e 74 20 74 6f 20 69 6e 63 6c 75  We want to inclu
32c65 64 65 20 74 68 69 73 20 6d 61 74 63 68 20 69 6e  de this match in
32c66 20 74 68 65 20 73 6e 69 70 70 65 74 20 2a 2f 0a   the snippet */.
32c67 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
32c68 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 73 6e  the text of a sn
32c69 69 70 70 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ippet..*/.static
32c6a 20 76 6f 69 64 20 73 6e 69 70 70 65 74 54 65 78   void snippetTex
32c6b 74 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  t(.  fulltext_cu
32c6c 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
32c6d 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 77   /* The cursor w
32c6e 65 20 6e 65 65 64 20 74 68 65 20 73 6e 69 70 70  e need the snipp
32c6f 65 74 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73  et for */.  cons
32c70 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 4d 61  t char *zStartMa
32c71 72 6b 2c 20 20 20 20 20 2f 2a 20 4d 61 72 6b 75  rk,     /* Marku
32c72 70 20 74 6f 20 61 70 70 65 61 72 20 62 65 66 6f  p to appear befo
32c73 72 65 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f  re each match */
32c74 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
32c75 45 6e 64 4d 61 72 6b 2c 20 20 20 20 20 20 20 2f  EndMark,       /
32c76 2a 20 4d 61 72 6b 75 70 20 74 6f 20 61 70 70 65  * Markup to appe
32c77 61 72 20 61 66 74 65 72 20 65 61 63 68 20 6d 61  ar after each ma
32c78 74 63 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  tch */.  const c
32c79 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20 20  har *zEllipsis  
32c7a 20 20 20 20 20 2f 2a 20 45 6c 6c 69 70 73 69 73       /* Ellipsis
32c7b 20 6d 61 72 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e   mark */.){.  in
32c7c 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74  t i, j;.  struct
32c7d 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 61   snippetMatch *a
32c7e 4d 61 74 63 68 3b 0a 20 20 69 6e 74 20 6e 4d 61  Match;.  int nMa
32c7f 74 63 68 3b 0a 20 20 69 6e 74 20 6e 44 65 73 69  tch;.  int nDesi
32c80 72 65 64 3b 0a 20 20 53 74 72 69 6e 67 42 75 66  red;.  StringBuf
32c81 66 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 74 61  fer sb;.  int ta
32c82 69 6c 43 6f 6c 3b 0a 20 20 69 6e 74 20 74 61 69  ilCol;.  int tai
32c83 6c 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 69  lOffset;.  int i
32c84 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 44 6f 63 3b  Col;.  int nDoc;
32c85 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
32c86 44 6f 63 3b 0a 20 20 69 6e 74 20 69 53 74 61 72  Doc;.  int iStar
32c87 74 2c 20 69 45 6e 64 3b 0a 20 20 69 6e 74 20 74  t, iEnd;.  int t
32c88 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b  ailEllipsis = 0;
32c89 0a 20 20 69 6e 74 20 69 4d 61 74 63 68 3b 0a 20  .  int iMatch;. 
32c8a 20 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   ..  sqlite3_fre
32c8b 65 28 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70  e(pCursor->snipp
32c8c 65 74 2e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20  et.zSnippet);.  
32c8d 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74  pCursor->snippet
32c8e 2e 7a 53 6e 69 70 70 65 74 20 3d 20 30 3b 0a 20  .zSnippet = 0;. 
32c8f 20 61 4d 61 74 63 68 20 3d 20 70 43 75 72 73 6f   aMatch = pCurso
32c90 72 2d 3e 73 6e 69 70 70 65 74 2e 61 4d 61 74 63  r->snippet.aMatc
32c91 68 3b 0a 20 20 6e 4d 61 74 63 68 20 3d 20 70 43  h;.  nMatch = pC
32c92 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e  ursor->snippet.n
32c93 4d 61 74 63 68 3b 0a 20 20 69 6e 69 74 53 74 72  Match;.  initStr
32c94 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a  ingBuffer(&sb);.
32c95 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d  .  for(i=0; i<nM
32c96 61 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  atch; i++){.    
32c97 61 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74  aMatch[i].snStat
32c98 75 73 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e  us = SNIPPET_IGN
32c99 4f 52 45 3b 0a 20 20 7d 0a 20 20 6e 44 65 73 69  ORE;.  }.  nDesi
32c9a 72 65 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  red = 0;.  for(i
32c9b 3d 30 3b 20 69 3c 46 54 53 33 5f 52 4f 54 4f 52  =0; i<FTS3_ROTOR
32c9c 5f 53 5a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66  _SZ; i++){.    f
32c9d 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 4d 61 74 63 68  or(j=0; j<nMatch
32c9e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
32c9f 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72  ( aMatch[j].iTer
32ca0 6d 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20  m==i ){.        
32ca1 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74  aMatch[j].snStat
32ca2 75 73 20 3d 20 53 4e 49 50 50 45 54 5f 44 45 53  us = SNIPPET_DES
32ca3 49 52 45 44 3b 0a 20 20 20 20 20 20 20 20 6e 44  IRED;.        nD
32ca4 65 73 69 72 65 64 2b 2b 3b 0a 20 20 20 20 20 20  esired++;.      
32ca5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
32ca6 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 4d  .    }.  }..  iM
32ca7 61 74 63 68 20 3d 20 30 3b 0a 20 20 74 61 69 6c  atch = 0;.  tail
32ca8 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 74 61 69 6c  Col = -1;.  tail
32ca9 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f  Offset = 0;.  fo
32caa 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20  r(i=0; i<nMatch 
32cab 26 26 20 6e 44 65 73 69 72 65 64 3e 30 3b 20 69  && nDesired>0; i
32cac 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61  ++){.    if( aMa
32cad 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73 21  tch[i].snStatus!
32cae 3d 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44  =SNIPPET_DESIRED
32caf 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
32cb0 20 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20   nDesired--;.   
32cb1 20 69 43 6f 6c 20 3d 20 61 4d 61 74 63 68 5b 69   iCol = aMatch[i
32cb2 5d 2e 69 43 6f 6c 3b 0a 20 20 20 20 7a 44 6f 63  ].iCol;.    zDoc
32cb3 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
32cb4 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
32cb5 65 78 74 28 70 43 75 72 73 6f 72 2d 3e 70 53 74  ext(pCursor->pSt
32cb6 6d 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20  mt, iCol+1);.   
32cb7 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f   nDoc = sqlite3_
32cb8 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 75  column_bytes(pCu
32cb9 72 73 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f  rsor->pStmt, iCo
32cba 6c 2b 31 29 3b 0a 20 20 20 20 69 53 74 61 72 74  l+1);.    iStart
32cbb 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74   = aMatch[i].iSt
32cbc 61 72 74 20 2d 20 34 30 3b 0a 20 20 20 20 69 53  art - 40;.    iS
32cbd 74 61 72 74 20 3d 20 77 6f 72 64 42 6f 75 6e 64  tart = wordBound
32cbe 61 72 79 28 69 53 74 61 72 74 2c 20 7a 44 6f 63  ary(iStart, zDoc
32cbf 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20  , nDoc, aMatch, 
32cc0 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20  nMatch, iCol);. 
32cc1 20 20 20 69 66 28 20 69 53 74 61 72 74 3c 3d 31     if( iStart<=1
32cc2 30 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72  0 ){.      iStar
32cc3 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  t = 0;.    }.   
32cc4 20 69 66 28 20 69 43 6f 6c 3d 3d 74 61 69 6c 43   if( iCol==tailC
32cc5 6f 6c 20 26 26 20 69 53 74 61 72 74 3c 3d 74 61  ol && iStart<=ta
32cc6 69 6c 4f 66 66 73 65 74 2b 32 30 20 29 7b 0a 20  ilOffset+20 ){. 
32cc7 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 74 61       iStart = ta
32cc8 69 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a  ilOffset;.    }.
32cc9 20 20 20 20 69 66 28 20 28 69 43 6f 6c 21 3d 74      if( (iCol!=t
32cca 61 69 6c 43 6f 6c 20 26 26 20 74 61 69 6c 43 6f  ailCol && tailCo
32ccb 6c 3e 3d 30 29 20 7c 7c 20 69 53 74 61 72 74 21  l>=0) || iStart!
32ccc 3d 74 61 69 6c 4f 66 66 73 65 74 20 29 7b 0a 20  =tailOffset ){. 
32ccd 20 20 20 20 20 74 72 69 6d 57 68 69 74 65 53 70       trimWhiteSp
32cce 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20 20  ace(&sb);.      
32ccf 61 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65  appendWhiteSpace
32cd0 28 26 73 62 29 3b 0a 20 20 20 20 20 20 61 70 70  (&sb);.      app
32cd1 65 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73  end(&sb, zEllips
32cd2 69 73 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e  is);.      appen
32cd3 64 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29  dWhiteSpace(&sb)
32cd4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64  ;.    }.    iEnd
32cd5 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74   = aMatch[i].iSt
32cd6 61 72 74 20 2b 20 61 4d 61 74 63 68 5b 69 5d 2e  art + aMatch[i].
32cd7 6e 42 79 74 65 20 2b 20 34 30 3b 0a 20 20 20 20  nByte + 40;.    
32cd8 69 45 6e 64 20 3d 20 77 6f 72 64 42 6f 75 6e 64  iEnd = wordBound
32cd9 61 72 79 28 69 45 6e 64 2c 20 7a 44 6f 63 2c 20  ary(iEnd, zDoc, 
32cda 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d  nDoc, aMatch, nM
32cdb 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20  atch, iCol);.   
32cdc 20 69 66 28 20 69 45 6e 64 3e 3d 6e 44 6f 63 2d   if( iEnd>=nDoc-
32cdd 31 30 20 29 7b 0a 20 20 20 20 20 20 69 45 6e 64  10 ){.      iEnd
32cde 20 3d 20 6e 44 6f 63 3b 0a 20 20 20 20 20 20 74   = nDoc;.      t
32cdf 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b  ailEllipsis = 0;
32ce0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
32ce1 20 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d    tailEllipsis =
32ce2 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68   1;.    }.    wh
32ce3 69 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74  ile( iMatch<nMat
32ce4 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61  ch && aMatch[iMa
32ce5 74 63 68 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 20 29  tch].iCol<iCol )
32ce6 7b 20 69 4d 61 74 63 68 2b 2b 3b 20 7d 0a 20 20  { iMatch++; }.  
32ce7 20 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c    while( iStart<
32ce8 69 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 77 68  iEnd ){.      wh
32ce9 69 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74  ile( iMatch<nMat
32cea 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61  ch && aMatch[iMa
32ceb 74 63 68 5d 2e 69 53 74 61 72 74 3c 69 53 74 61  tch].iStart<iSta
32cec 72 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  rt.             
32ced 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  && aMatch[iMatch
32cee 5d 2e 69 43 6f 6c 3c 3d 69 43 6f 6c 20 29 7b 0a  ].iCol<=iCol ){.
32cef 20 20 20 20 20 20 20 20 69 4d 61 74 63 68 2b 2b          iMatch++
32cf0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
32cf1 69 66 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63  if( iMatch<nMatc
32cf2 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74  h && aMatch[iMat
32cf3 63 68 5d 2e 69 53 74 61 72 74 3c 69 45 6e 64 0a  ch].iStart<iEnd.
32cf4 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
32cf5 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
32cf6 43 6f 6c 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20  Col==iCol ){.   
32cf7 20 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62       nappend(&sb
32cf8 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c  , &zDoc[iStart],
32cf9 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e   aMatch[iMatch].
32cfa 69 53 74 61 72 74 20 2d 20 69 53 74 61 72 74 29  iStart - iStart)
32cfb 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74  ;.        iStart
32cfc 20 3d 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68   = aMatch[iMatch
32cfd 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20  ].iStart;.      
32cfe 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 53    append(&sb, zS
32cff 74 61 72 74 4d 61 72 6b 29 3b 0a 20 20 20 20 20  tartMark);.     
32d00 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c 20     nappend(&sb, 
32d01 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20 61  &zDoc[iStart], a
32d02 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42  Match[iMatch].nB
32d03 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 61 70  yte);.        ap
32d04 70 65 6e 64 28 26 73 62 2c 20 7a 45 6e 64 4d 61  pend(&sb, zEndMa
32d05 72 6b 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74  rk);.        iSt
32d06 61 72 74 20 2b 3d 20 61 4d 61 74 63 68 5b 69 4d  art += aMatch[iM
32d07 61 74 63 68 5d 2e 6e 42 79 74 65 3b 0a 20 20 20  atch].nByte;.   
32d08 20 20 20 20 20 66 6f 72 28 6a 3d 69 4d 61 74 63       for(j=iMatc
32d09 68 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a  h+1; j<nMatch; j
32d0a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
32d0b 66 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65  f( aMatch[j].iTe
32d0c 72 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d 61 74 63  rm==aMatch[iMatc
32d0d 68 5d 2e 69 54 65 72 6d 0a 20 20 20 20 20 20 20  h].iTerm.       
32d0e 20 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68         && aMatch
32d0f 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 3d 3d 53 4e  [j].snStatus==SN
32d10 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 29 7b  IPPET_DESIRED ){
32d11 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65  .            nDe
32d12 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20 20 20 20  sired--;.       
32d13 20 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73       aMatch[j].s
32d14 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
32d15 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 20 20 20 20  T_IGNORE;.      
32d16 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
32d17 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
32d18 20 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62       nappend(&sb
32d19 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c  , &zDoc[iStart],
32d1a 20 69 45 6e 64 20 2d 20 69 53 74 61 72 74 29 3b   iEnd - iStart);
32d1b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20  .        iStart 
32d1c 3d 20 69 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  = iEnd;.      }.
32d1d 20 20 20 20 7d 0a 20 20 20 20 74 61 69 6c 43 6f      }.    tailCo
32d1e 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 74 61  l = iCol;.    ta
32d1f 69 6c 4f 66 66 73 65 74 20 3d 20 69 45 6e 64 3b  ilOffset = iEnd;
32d20 0a 20 20 7d 0a 20 20 74 72 69 6d 57 68 69 74 65  .  }.  trimWhite
32d21 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 69 66  Space(&sb);.  if
32d22 28 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 29  ( tailEllipsis )
32d23 7b 0a 20 20 20 20 61 70 70 65 6e 64 57 68 69 74  {.    appendWhit
32d24 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20  eSpace(&sb);.   
32d25 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6c   append(&sb, zEl
32d26 6c 69 70 73 69 73 29 3b 0a 20 20 7d 0a 20 20 70  lipsis);.  }.  p
32d27 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
32d28 7a 53 6e 69 70 70 65 74 20 3d 20 73 74 72 69 6e  zSnippet = strin
32d29 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29  gBufferData(&sb)
32d2a 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69  ;.  pCursor->sni
32d2b 70 70 65 74 2e 6e 53 6e 69 70 70 65 74 20 3d 20  ppet.nSnippet = 
32d2c 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67  stringBufferLeng
32d2d 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  th(&sb);.}.../*.
32d2e 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72  ** Close the cur
32d2f 73 6f 72 2e 20 20 46 6f 72 20 61 64 64 69 74 69  sor.  For additi
32d30 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
32d31 20 73 65 65 20 74 68 65 20 64 6f 63 75 6d 65 6e   see the documen
32d32 74 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 68 65  tation.** on the
32d33 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 6f   xClose method o
32d34 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
32d35 62 6c 65 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  ble interface..*
32d36 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  /.static int ful
32d37 6c 74 65 78 74 43 6c 6f 73 65 28 73 71 6c 69 74  ltextClose(sqlit
32d38 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
32d39 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c  pCursor){.  full
32d3a 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d  text_cursor *c =
32d3b 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f   (fulltext_curso
32d3c 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
32d3d 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
32d3e 43 6c 6f 73 65 20 25 70 5c 6e 22 2c 20 63 29 29  Close %p\n", c))
32d3f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  ;.  sqlite3_fina
32d40 6c 69 7a 65 28 63 2d 3e 70 53 74 6d 74 29 3b 0a  lize(c->pStmt);.
32d41 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
32d42 72 46 72 65 65 28 63 2d 3e 70 45 78 70 72 29 3b  rFree(c->pExpr);
32d43 0a 20 20 73 6e 69 70 70 65 74 43 6c 65 61 72 28  .  snippetClear(
32d44 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b 0a 20 20  &c->snippet);.  
32d45 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44  if( c->result.nD
32d46 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 64 6c  ata!=0 ){.    dl
32d47 72 44 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 61  rDestroy(&c->rea
32d48 64 65 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61  der);.  }.  data
32d49 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 63  BufferDestroy(&c
32d4a 2d 3e 72 65 73 75 6c 74 29 3b 0a 20 20 73 71 6c  ->result);.  sql
32d4b 69 74 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20  ite3_free(c);.  
32d4c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
32d4d 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
32d4e 66 75 6c 6c 74 65 78 74 4e 65 78 74 28 73 71 6c  fulltextNext(sql
32d4f 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
32d50 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75   *pCursor){.  fu
32d51 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63  lltext_cursor *c
32d52 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72   = (fulltext_cur
32d53 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
32d54 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53    int rc;..  FTS
32d55 54 52 41 43 45 28 28 22 46 54 53 33 20 4e 65 78  TRACE(("FTS3 Nex
32d56 74 20 25 70 5c 6e 22 2c 20 70 43 75 72 73 6f 72  t %p\n", pCursor
32d57 29 29 3b 0a 20 20 73 6e 69 70 70 65 74 43 6c 65  ));.  snippetCle
32d58 61 72 28 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b  ar(&c->snippet);
32d59 0a 20 20 69 66 28 20 63 2d 3e 69 43 75 72 73 6f  .  if( c->iCurso
32d5a 72 54 79 70 65 20 3c 20 51 55 45 52 59 5f 46 55  rType < QUERY_FU
32d5b 4c 4c 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a  LLTEXT ){.    /*
32d5c 20 54 4f 44 4f 28 73 68 65 73 73 29 20 48 61 6e   TODO(shess) Han
32d5d 64 6c 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  dle SQLITE_SCHEM
32d5e 41 20 41 4e 44 20 53 51 4c 49 54 45 5f 42 55 53  A AND SQLITE_BUS
32d5f 59 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  Y. */.    rc = s
32d60 71 6c 69 74 65 33 5f 73 74 65 70 28 63 2d 3e 70  qlite3_step(c->p
32d61 53 74 6d 74 29 3b 0a 20 20 20 20 73 77 69 74 63  Stmt);.    switc
32d62 68 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 63  h( rc ){.      c
32d63 61 73 65 20 53 51 4c 49 54 45 5f 52 4f 57 3a 0a  ase SQLITE_ROW:.
32d64 20 20 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d          c->eof =
32d65 20 30 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   0;.        retu
32d66 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
32d67 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
32d68 44 4f 4e 45 3a 0a 20 20 20 20 20 20 20 20 63 2d  DONE:.        c-
32d69 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  >eof = 1;.      
32d6a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
32d6b 4f 4b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c  OK;.      defaul
32d6c 74 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e 65 6f  t:.        c->eo
32d6d 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72  f = 1;.        r
32d6e 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
32d6f 20 20 7d 20 65 6c 73 65 20 7b 20 20 2f 2a 20 66    } else {  /* f
32d70 75 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 20 2a  ull-text query *
32d71 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
32d72 65 33 5f 72 65 73 65 74 28 63 2d 3e 70 53 74 6d  e3_reset(c->pStm
32d73 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  t);.    if( rc!=
32d74 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
32d75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 69 66 28 20  rn rc;..    if( 
32d76 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61 3d  c->result.nData=
32d77 3d 30 20 7c 7c 20 64 6c 72 41 74 45 6e 64 28 26  =0 || dlrAtEnd(&
32d78 63 2d 3e 72 65 61 64 65 72 29 20 29 7b 0a 20 20  c->reader) ){.  
32d79 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a      c->eof = 1;.
32d7a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
32d7b 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
32d7c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
32d7d 69 6e 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74  ind_int64(c->pSt
32d7e 6d 74 2c 20 31 2c 20 64 6c 72 44 6f 63 69 64 28  mt, 1, dlrDocid(
32d7f 26 63 2d 3e 72 65 61 64 65 72 29 29 3b 0a 20 20  &c->reader));.  
32d80 20 20 64 6c 72 53 74 65 70 28 26 63 2d 3e 72 65    dlrStep(&c->re
32d81 61 64 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ader);.    if( r
32d82 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
32d83 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 2f 2a  eturn rc;.    /*
32d84 20 54 4f 44 4f 28 73 68 65 73 73 29 20 48 61 6e   TODO(shess) Han
32d85 64 6c 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  dle SQLITE_SCHEM
32d86 41 20 41 4e 44 20 53 51 4c 49 54 45 5f 42 55 53  A AND SQLITE_BUS
32d87 59 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  Y. */.    rc = s
32d88 71 6c 69 74 65 33 5f 73 74 65 70 28 63 2d 3e 70  qlite3_step(c->p
32d89 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72  Stmt);.    if( r
32d8a 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
32d8b 20 20 20 2f 2a 20 74 68 65 20 63 61 73 65 20 77     /* the case w
32d8c 65 20 65 78 70 65 63 74 20 2a 2f 0a 20 20 20 20  e expect */.    
32d8d 20 20 63 2d 3e 65 6f 66 20 3d 20 30 3b 0a 20 20    c->eof = 0;.  
32d8e 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
32d8f 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
32d90 2f 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  /* an error occu
32d91 72 72 65 64 3b 20 61 62 6f 72 74 20 2a 2f 0a 20  rred; abort */. 
32d92 20 20 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51     return rc==SQ
32d93 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
32d94 54 45 5f 45 52 52 4f 52 20 3a 20 72 63 3b 0a 20  TE_ERROR : rc;. 
32d95 20 7d 0a 7d 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73   }.}.../* TODO(s
32d96 68 65 73 73 29 20 49 66 20 77 65 20 70 75 73 68  hess) If we push
32d97 65 64 20 4c 65 61 66 52 65 61 64 65 72 20 74 6f  ed LeafReader to
32d98 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
32d99 66 69 6c 65 2c 20 6f 72 20 74 6f 0a 2a 2a 20 61  file, or to.** a
32d9a 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20 74 65 72  nother file, ter
32d9b 6d 5f 73 65 6c 65 63 74 28 29 20 63 6f 75 6c 64  m_select() could
32d9c 20 62 65 20 70 75 73 68 65 64 20 61 62 6f 76 65   be pushed above
32d9d 0a 2a 2a 20 64 6f 63 4c 69 73 74 4f 66 54 65 72  .** docListOfTer
32d9e 6d 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  m()..*/.static i
32d9f 6e 74 20 74 65 72 6d 53 65 6c 65 63 74 28 66 75  nt termSelect(fu
32da0 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
32da1 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20  int iColumn,.   
32da2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32da3 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
32da4 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c  Term, int nTerm,
32da5 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20   int isPrefix,. 
32da6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32da7 20 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65       DocListType
32da8 20 69 54 79 70 65 2c 20 44 61 74 61 42 75 66 66   iType, DataBuff
32da9 65 72 20 2a 6f 75 74 29 3b 0a 0a 2f 2a 20 0a 2a  er *out);../* .*
32daa 2a 20 52 65 74 75 72 6e 20 61 20 44 6f 63 4c 69  * Return a DocLi
32dab 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  st corresponding
32dac 20 74 6f 20 74 68 65 20 70 68 72 61 73 65 20 2a   to the phrase *
32dad 70 50 68 72 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  pPhrase..**.** T
32dae 68 65 20 72 65 73 75 6c 74 69 6e 67 20 44 4c 5f  he resulting DL_
32daf 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20 69  DOCIDS doclist i
32db0 73 20 73 74 6f 72 65 64 20 69 6e 20 70 52 65 73  s stored in pRes
32db1 75 6c 74 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a  ult, which is.**
32db2 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 2a 2f   overwritten..*/
32db3 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 63 4c  .static int docL
32db4 69 73 74 4f 66 50 68 72 61 73 65 28 0a 20 20 66  istOfPhrase(.  f
32db5 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 54  ulltext_vtab *pT
32db6 61 62 2c 20 20 20 2f 2a 20 54 68 65 20 66 75 6c  ab,   /* The ful
32db7 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a  l text index */.
32db8 20 20 46 74 73 33 50 68 72 61 73 65 20 2a 70 50    Fts3Phrase *pP
32db9 68 72 61 73 65 2c 20 20 20 2f 2a 20 50 68 72 61  hrase,   /* Phra
32dba 73 65 20 74 6f 20 72 65 74 75 72 6e 20 61 20 64  se to return a d
32dbb 6f 63 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e  oclist correspon
32dbc 64 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 44 6f 63  ding to */.  Doc
32dbd 4c 69 73 74 54 79 70 65 20 65 4c 69 73 74 54 79  ListType eListTy
32dbe 70 65 2c 20 2f 2a 20 45 69 74 68 65 72 20 44 4c  pe, /* Either DL
32dbf 5f 44 4f 43 49 44 53 20 6f 72 20 44 4c 5f 50 4f  _DOCIDS or DL_PO
32dc0 53 49 54 49 4f 4e 53 20 2a 2f 0a 20 20 44 61 74  SITIONS */.  Dat
32dc1 61 42 75 66 66 65 72 20 2a 70 52 65 73 75 6c 74  aBuffer *pResult
32dc2 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
32dc3 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a   result here */.
32dc4 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69  ){.  int ii;.  i
32dc5 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
32dc6 4b 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20  K;.  int iCol = 
32dc7 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e  pPhrase->iColumn
32dc8 3b 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20  ;.  DocListType 
32dc9 65 54 79 70 65 20 3d 20 65 4c 69 73 74 54 79 70  eType = eListTyp
32dca 65 3b 0a 20 20 61 73 73 65 72 74 28 20 65 54 79  e;.  assert( eTy
32dcb 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  pe==DL_POSITIONS
32dcc 20 7c 7c 20 65 54 79 70 65 3d 3d 44 4c 5f 44 4f   || eType==DL_DO
32dcd 43 49 44 53 20 29 3b 0a 20 20 69 66 28 20 70 50  CIDS );.  if( pP
32dce 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3e 31 20  hrase->nToken>1 
32dcf 29 7b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 44  ){.    eType = D
32dd0 4c 5f 50 4f 53 49 54 49 4f 4e 53 3b 0a 20 20 7d  L_POSITIONS;.  }
32dd1 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65  ..  /* This code
32dd2 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65   should never be
32dd3 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66   called with buf
32dd4 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a  fered updates. *
32dd5 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  /.  assert( pTab
32dd6 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30  ->nPendingData<0
32dd7 20 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b   );..  for(ii=0;
32dd8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
32dd9 26 20 69 69 3c 70 50 68 72 61 73 65 2d 3e 6e 54  & ii<pPhrase->nT
32dda 6f 6b 65 6e 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  oken; ii++){.   
32ddb 20 44 61 74 61 42 75 66 66 65 72 20 74 6d 70 3b   DataBuffer tmp;
32ddc 0a 20 20 20 20 73 74 72 75 63 74 20 50 68 72 61  .    struct Phra
32ddd 73 65 54 6f 6b 65 6e 20 2a 70 20 3d 20 26 70 50  seToken *p = &pP
32dde 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69  hrase->aToken[ii
32ddf 5d 3b 0a 20 20 20 20 72 63 20 3d 20 74 65 72 6d  ];.    rc = term
32de0 53 65 6c 65 63 74 28 70 54 61 62 2c 20 69 43 6f  Select(pTab, iCo
32de1 6c 2c 20 70 2d 3e 7a 2c 20 70 2d 3e 6e 2c 20 70  l, p->z, p->n, p
32de2 2d 3e 69 73 50 72 65 66 69 78 2c 20 65 54 79 70  ->isPrefix, eTyp
32de3 65 2c 20 26 74 6d 70 29 3b 0a 20 20 20 20 69 66  e, &tmp);.    if
32de4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
32de5 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 69 3d  ){.      if( ii=
32de6 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  =0 ){.        *p
32de7 52 65 73 75 6c 74 20 3d 20 74 6d 70 3b 0a 20 20  Result = tmp;.  
32de8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
32de9 20 20 20 44 61 74 61 42 75 66 66 65 72 20 72 65     DataBuffer re
32dea 73 20 3d 20 2a 70 52 65 73 75 6c 74 3b 0a 20 20  s = *pResult;.  
32deb 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
32dec 49 6e 69 74 28 70 52 65 73 75 6c 74 2c 20 30 29  Init(pResult, 0)
32ded 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 69  ;.        if( ii
32dee 3d 3d 28 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  ==(pPhrase->nTok
32def 65 6e 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  en-1) ){.       
32df0 20 20 20 65 54 79 70 65 20 3d 20 65 4c 69 73 74     eType = eList
32df1 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Type;.        }.
32df2 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 50          docListP
32df3 68 72 61 73 65 4d 65 72 67 65 28 0a 20 20 20 20  hraseMerge(.    
32df4 20 20 20 20 20 20 72 65 73 2e 70 44 61 74 61 2c        res.pData,
32df5 20 72 65 73 2e 6e 44 61 74 61 2c 20 74 6d 70 2e   res.nData, tmp.
32df6 70 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74 61  pData, tmp.nData
32df7 2c 20 30 2c 20 30 2c 20 65 54 79 70 65 2c 20 70  , 0, 0, eType, p
32df8 52 65 73 75 6c 74 0a 20 20 20 20 20 20 20 20 29  Result.        )
32df9 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75  ;.        dataBu
32dfa 66 66 65 72 44 65 73 74 72 6f 79 28 26 72 65 73  fferDestroy(&res
32dfb 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  );.        dataB
32dfc 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 74 6d  ufferDestroy(&tm
32dfd 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
32dfe 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
32dff 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 76 61  rc;.}../*.** Eva
32e00 6c 75 61 74 65 20 74 68 65 20 66 75 6c 6c 2d 74  luate the full-t
32e01 65 78 74 20 65 78 70 72 65 73 73 69 6f 6e 20 70  ext expression p
32e02 45 78 70 72 20 61 67 61 69 6e 73 74 20 66 74 73  Expr against fts
32e03 33 20 74 61 62 6c 65 20 70 54 61 62 2e 20 57 72  3 table pTab. Wr
32e04 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  ite.** the resul
32e05 74 73 20 69 6e 74 6f 20 70 52 65 73 2e 0a 2a 2f  ts into pRes..*/
32e06 0a 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c  .static int eval
32e07 46 74 73 33 45 78 70 72 28 0a 20 20 66 75 6c 6c  Fts3Expr(.  full
32e08 74 65 78 74 5f 76 74 61 62 20 2a 70 54 61 62 2c  text_vtab *pTab,
32e09 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 74             /* Ft
32e0a 73 33 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  s3 Virtual table
32e0b 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 46 74 73   object */.  Fts
32e0c 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20  3Expr *pExpr,   
32e0d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
32e0e 61 72 73 65 64 20 66 74 73 33 20 65 78 70 72 65  arsed fts3 expre
32e0f 73 73 69 6f 6e 20 2a 2f 0a 20 20 44 61 74 61 42  ssion */.  DataB
32e10 75 66 66 65 72 20 2a 70 52 65 73 20 20 20 20 20  uffer *pRes     
32e11 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
32e12 3a 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20  : Write results 
32e13 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
32e14 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  n here */.){.  i
32e15 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
32e16 4b 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c  K;..  /* Initial
32e17 69 7a 65 20 74 68 65 20 6f 75 74 70 75 74 20 62  ize the output b
32e18 75 66 66 65 72 2e 20 49 66 20 74 68 69 73 20 69  uffer. If this i
32e19 73 20 61 6e 20 65 6d 70 74 79 20 71 75 65 72 79  s an empty query
32e1a 20 28 70 45 78 70 72 3d 3d 30 29 2c 20 0a 20 20   (pExpr==0), .  
32e1b 2a 2a 20 74 68 69 73 20 69 73 20 61 6c 6c 20 74  ** this is all t
32e1c 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  hat needs to be 
32e1d 64 6f 6e 65 2e 20 45 6d 70 74 79 20 71 75 65 72  done. Empty quer
32e1e 69 65 73 20 70 72 6f 64 75 63 65 20 65 6d 70 74  ies produce empt
32e1f 79 20 0a 20 20 2a 2a 20 72 65 73 75 6c 74 20 73  y .  ** result s
32e20 65 74 73 2e 0a 20 20 2a 2f 0a 20 20 64 61 74 61  ets..  */.  data
32e21 42 75 66 66 65 72 49 6e 69 74 28 70 52 65 73 2c  BufferInit(pRes,
32e22 20 30 29 3b 0a 0a 20 20 69 66 28 20 70 45 78 70   0);..  if( pExp
32e23 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78  r ){.    if( pEx
32e24 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  pr->eType==FTSQU
32e25 45 52 59 5f 50 48 52 41 53 45 20 29 7b 0a 20 20  ERY_PHRASE ){.  
32e26 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20      DocListType 
32e27 65 54 79 70 65 20 3d 20 44 4c 5f 44 4f 43 49 44  eType = DL_DOCID
32e28 53 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78  S;.      if( pEx
32e29 70 72 2d 3e 70 50 61 72 65 6e 74 20 26 26 20 70  pr->pParent && p
32e2a 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 2d 3e 65  Expr->pParent->e
32e2b 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e  Type==FTSQUERY_N
32e2c 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20 20 65  EAR ){.        e
32e2d 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49  Type = DL_POSITI
32e2e 4f 4e 53 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ONS;.      }.   
32e2f 20 20 20 72 63 20 3d 20 64 6f 63 4c 69 73 74 4f     rc = docListO
32e30 66 50 68 72 61 73 65 28 70 54 61 62 2c 20 70 45  fPhrase(pTab, pE
32e31 78 70 72 2d 3e 70 50 68 72 61 73 65 2c 20 65 54  xpr->pPhrase, eT
32e32 79 70 65 2c 20 70 52 65 73 29 3b 0a 20 20 20 20  ype, pRes);.    
32e33 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 44 61 74  }else{.      Dat
32e34 61 42 75 66 66 65 72 20 6c 68 73 3b 0a 20 20 20  aBuffer lhs;.   
32e35 20 20 20 44 61 74 61 42 75 66 66 65 72 20 72 68     DataBuffer rh
32e36 73 3b 0a 0a 20 20 20 20 20 20 64 61 74 61 42 75  s;..      dataBu
32e37 66 66 65 72 49 6e 69 74 28 26 72 68 73 2c 20 30  fferInit(&rhs, 0
32e38 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c  );.      if( SQL
32e39 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 65 76  ITE_OK==(rc = ev
32e3a 61 6c 46 74 73 33 45 78 70 72 28 70 54 61 62 2c  alFts3Expr(pTab,
32e3b 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
32e3c 6c 68 73 29 29 20 0a 20 20 20 20 20 20 20 26 26  lhs)) .       &&
32e3d 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
32e3e 3d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 70  = evalFts3Expr(p
32e3f 54 61 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  Tab, pExpr->pRig
32e40 68 74 2c 20 26 72 68 73 29 29 20 0a 20 20 20 20  ht, &rhs)) .    
32e41 20 20 29 7b 0a 20 20 20 20 20 20 20 20 73 77 69    ){.        swi
32e42 74 63 68 28 20 70 45 78 70 72 2d 3e 65 54 79 70  tch( pExpr->eTyp
32e43 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  e ){.          c
32e44 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 45 41  ase FTSQUERY_NEA
32e45 52 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20  R: {.           
32e46 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20   int nToken;.   
32e47 20 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70           Fts3Exp
32e48 72 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r *pLeft;.      
32e49 20 20 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70        DocListTyp
32e4a 65 20 65 54 79 70 65 20 3d 20 44 4c 5f 44 4f 43  e eType = DL_DOC
32e4b 49 44 53 3b 0a 20 20 20 20 20 20 20 20 20 20 20  IDS;.           
32e4c 20 69 66 28 20 70 45 78 70 72 2d 3e 70 50 61 72   if( pExpr->pPar
32e4d 65 6e 74 20 26 26 20 70 45 78 70 72 2d 3e 70 50  ent && pExpr->pP
32e4e 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54  arent->eType==FT
32e4f 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20  SQUERY_NEAR ){. 
32e50 20 20 20 20 20 20 20 20 20 20 20 20 20 65 54 79               eTy
32e51 70 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  pe = DL_POSITION
32e52 53 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  S;.            }
32e53 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 65  .            pLe
32e54 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  ft = pExpr->pLef
32e55 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 77  t;.            w
32e56 68 69 6c 65 28 20 70 4c 65 66 74 2d 3e 65 54 79  hile( pLeft->eTy
32e57 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41  pe==FTSQUERY_NEA
32e58 52 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20  R ){ .          
32e59 20 20 20 20 70 4c 65 66 74 3d 70 4c 65 66 74 2d      pLeft=pLeft-
32e5a 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  >pRight;.       
32e5b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
32e5c 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
32e5d 2d 3e 70 52 69 67 68 74 2d 3e 65 54 79 70 65 3d  ->pRight->eType=
32e5e 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45  =FTSQUERY_PHRASE
32e5f 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
32e60 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 65  assert( pLeft->e
32e61 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50  Type==FTSQUERY_P
32e62 48 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20  HRASE );.       
32e63 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 70 4c       nToken = pL
32e64 65 66 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  eft->pPhrase->nT
32e65 6f 6b 65 6e 20 2b 20 70 45 78 70 72 2d 3e 70 52  oken + pExpr->pR
32e66 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  ight->pPhrase->n
32e67 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20  Token;.         
32e68 20 20 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65     docListPhrase
32e69 4d 65 72 67 65 28 6c 68 73 2e 70 44 61 74 61 2c  Merge(lhs.pData,
32e6a 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e   lhs.nData, rhs.
32e6b 70 44 61 74 61 2c 20 72 68 73 2e 6e 44 61 74 61  pData, rhs.nData
32e6c 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
32e6d 20 20 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72 2b     pExpr->nNear+
32e6e 31 2c 20 6e 54 6f 6b 65 6e 2c 20 65 54 79 70 65  1, nToken, eType
32e6f 2c 20 70 52 65 73 0a 20 20 20 20 20 20 20 20 20  , pRes.         
32e70 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20     );.          
32e71 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
32e72 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63     }.          c
32e73 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54  ase FTSQUERY_NOT
32e74 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
32e75 64 6f 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72  docListExceptMer
32e76 67 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c 68  ge(lhs.pData, lh
32e77 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44 61  s.nData, rhs.pDa
32e78 74 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 70 52  ta, rhs.nData,pR
32e79 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  es);.           
32e7a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
32e7b 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 61    }.          ca
32e7c 73 65 20 46 54 53 51 55 45 52 59 5f 41 4e 44 3a  se FTSQUERY_AND:
32e7d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64   {.            d
32e7e 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67 65 28 6c  ocListAndMerge(l
32e7f 68 73 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44  hs.pData, lhs.nD
32e80 61 74 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20  ata, rhs.pData, 
32e81 72 68 73 2e 6e 44 61 74 61 2c 20 70 52 65 73 29  rhs.nData, pRes)
32e82 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
32e83 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
32e84 0a 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20  .          case 
32e85 46 54 53 51 55 45 52 59 5f 4f 52 3a 20 7b 0a 20  FTSQUERY_OR: {. 
32e86 20 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69             docLi
32e87 73 74 4f 72 4d 65 72 67 65 28 6c 68 73 2e 70 44  stOrMerge(lhs.pD
32e88 61 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20  ata, lhs.nData, 
32e89 72 68 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e  rhs.pData, rhs.n
32e8a 44 61 74 61 2c 20 70 52 65 73 29 3b 0a 20 20 20  Data, pRes);.   
32e8b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
32e8c 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
32e8d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
32e8e 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
32e8f 73 74 72 6f 79 28 26 6c 68 73 29 3b 0a 20 20 20  stroy(&lhs);.   
32e90 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
32e91 74 72 6f 79 28 26 72 68 73 29 3b 0a 20 20 20 20  troy(&rhs);.    
32e92 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
32e93 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73  rc;.}../* TODO(s
32e94 68 65 73 73 29 20 52 65 66 61 63 74 6f 72 20 74  hess) Refactor t
32e95 68 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76  he code to remov
32e96 65 20 74 68 69 73 20 66 6f 72 77 61 72 64 20 64  e this forward d
32e97 65 63 6c 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  ecl. */.static i
32e98 6e 74 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54  nt flushPendingT
32e99 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74  erms(fulltext_vt
32e9a 61 62 20 2a 76 29 3b 0a 0a 2f 2a 20 50 65 72 66  ab *v);../* Perf
32e9b 6f 72 6d 20 61 20 66 75 6c 6c 2d 74 65 78 74 20  orm a full-text 
32e9c 71 75 65 72 79 20 75 73 69 6e 67 20 74 68 65 20  query using the 
32e9d 73 65 61 72 63 68 20 65 78 70 72 65 73 73 69 6f  search expressio
32e9e 6e 20 69 6e 0a 2a 2a 20 7a 49 6e 70 75 74 5b 30  n in.** zInput[0
32e9f 2e 2e 6e 49 6e 70 75 74 2d 31 5d 2e 20 20 52 65  ..nInput-1].  Re
32ea0 74 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 6d  turn a list of m
32ea1 61 74 63 68 69 6e 67 20 64 6f 63 75 6d 65 6e 74  atching document
32ea2 73 0a 2a 2a 20 69 6e 20 70 52 65 73 75 6c 74 2e  s.** in pResult.
32ea3 0a 2a 2a 0a 2a 2a 20 51 75 65 72 69 65 73 20 6d  .**.** Queries m
32ea4 75 73 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e  ust match column
32ea5 20 69 43 6f 6c 75 6d 6e 2e 20 20 4f 72 20 69 66   iColumn.  Or if
32ea6 20 69 43 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c 75 6d   iColumn>=nColum
32ea7 6e 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 61 6c  n.** they are al
32ea8 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20 61  lowed to match a
32ea9 67 61 69 6e 73 74 20 61 6e 79 20 63 6f 6c 75 6d  gainst any colum
32eaa 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
32eab 20 66 75 6c 6c 74 65 78 74 51 75 65 72 79 28 0a   fulltextQuery(.
32eac 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
32ead 2a 76 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  *v,      /* The 
32eae 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20  full text index 
32eaf 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
32eb0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ,           /* M
32eb1 61 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 69  atch against thi
32eb2 73 20 63 6f 6c 75 6d 6e 20 62 79 20 64 65 66 61  s column by defa
32eb3 75 6c 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ult */.  const c
32eb4 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 20 20 20  har *zInput,    
32eb5 2f 2a 20 54 68 65 20 71 75 65 72 79 20 73 74 72  /* The query str
32eb6 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e  ing */.  int nIn
32eb7 70 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  put,            
32eb8 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
32eb9 65 73 20 69 6e 20 7a 49 6e 70 75 74 5b 5d 20 2a  es in zInput[] *
32eba 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  /.  DataBuffer *
32ebb 70 52 65 73 75 6c 74 2c 20 20 20 2f 2a 20 57 72  pResult,   /* Wr
32ebc 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 64  ite the result d
32ebd 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 20  oclist here */. 
32ebe 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78   Fts3Expr **ppEx
32ebf 70 72 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74  pr        /* Put
32ec0 20 70 61 72 73 65 64 20 71 75 65 72 79 20 73 74   parsed query st
32ec1 72 69 6e 67 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ring here */.){.
32ec2 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20    int rc;..  /* 
32ec3 54 4f 44 4f 28 73 68 65 73 73 29 20 49 6e 73 74  TODO(shess) Inst
32ec4 65 61 64 20 6f 66 20 66 6c 75 73 68 69 6e 67 20  ead of flushing 
32ec5 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 77 65  pendingTerms, we
32ec6 20 63 6f 75 6c 64 20 71 75 65 72 79 20 66 6f 72   could query for
32ec7 0a 20 20 2a 2a 20 74 68 65 20 72 65 6c 65 76 61  .  ** the releva
32ec8 6e 74 20 74 65 72 6d 20 61 6e 64 20 6d 65 72 67  nt term and merg
32ec9 65 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e  e the doclist in
32eca 74 6f 20 77 68 61 74 20 77 65 20 72 65 63 65 69  to what we recei
32ecb 76 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65  ve from.  ** the
32ecc 20 64 61 74 61 62 61 73 65 2e 20 20 57 61 69 74   database.  Wait
32ecd 20 61 6e 64 20 73 65 65 20 69 66 20 74 68 69 73   and see if this
32ece 20 69 73 20 61 20 63 6f 6d 6d 6f 6e 20 69 73 73   is a common iss
32ecf 75 65 2c 20 66 69 72 73 74 2e 0a 20 20 2a 2a 0a  ue, first..  **.
32ed0 20 20 2a 2a 20 41 20 67 6f 6f 64 20 72 65 61 73    ** A good reas
32ed1 6f 6e 20 6e 6f 74 20 74 6f 20 66 6c 75 73 68 20  on not to flush 
32ed2 69 73 20 74 6f 20 6e 6f 74 20 67 65 6e 65 72 61  is to not genera
32ed3 74 65 20 75 70 64 61 74 65 2d 72 65 6c 61 74 65  te update-relate
32ed4 64 0a 20 20 2a 2a 20 65 72 72 6f 72 20 63 6f 64  d.  ** error cod
32ed5 65 73 20 66 72 6f 6d 20 68 65 72 65 2e 0a 20 20  es from here..  
32ed6 2a 2f 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 61  */..  /* Flush a
32ed7 6e 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61  ny buffered upda
32ed8 74 65 73 20 62 65 66 6f 72 65 20 65 78 65 63 75  tes before execu
32ed9 74 69 6e 67 20 74 68 65 20 71 75 65 72 79 2e 20  ting the query. 
32eda 2a 2f 0a 20 20 72 63 20 3d 20 66 6c 75 73 68 50  */.  rc = flushP
32edb 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
32edc 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
32edd 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
32ede 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
32edf 50 61 72 73 65 20 74 68 65 20 71 75 65 72 79 20  Parse the query 
32ee0 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 4d 41  passed to the MA
32ee1 54 43 48 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f  TCH operator. */
32ee2 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46  .  rc = sqlite3F
32ee3 74 73 33 45 78 70 72 50 61 72 73 65 28 76 2d 3e  ts3ExprParse(v->
32ee4 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 0a 20 20 20  pTokenizer, .   
32ee5 20 20 20 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c 20     v->azColumn, 
32ee6 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69 43 6f 6c  v->nColumn, iCol
32ee7 75 6d 6e 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  umn, zInput, nIn
32ee8 70 75 74 2c 20 70 70 45 78 70 72 0a 20 20 29 3b  put, ppExpr.  );
32ee9 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
32eea 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  E_OK ){.    asse
32eeb 72 74 28 20 30 3d 3d 28 2a 70 70 45 78 70 72 29  rt( 0==(*ppExpr)
32eec 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   );.    return r
32eed 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  c;.  }..  return
32eee 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 76 2c   evalFts3Expr(v,
32eef 20 2a 70 70 45 78 70 72 2c 20 70 52 65 73 75 6c   *ppExpr, pResul
32ef0 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  t);.}../*.** Thi
32ef1 73 20 69 73 20 74 68 65 20 78 46 69 6c 74 65 72  s is the xFilter
32ef2 20 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 74   interface for t
32ef3 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
32ef4 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 76 69  .  See.** the vi
32ef5 72 74 75 61 6c 20 74 61 62 6c 65 20 78 46 69 6c  rtual table xFil
32ef6 74 65 72 20 6d 65 74 68 6f 64 20 64 6f 63 75 6d  ter method docum
32ef7 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64  entation for add
32ef8 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72  itional.** infor
32ef9 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  mation..**.** If
32efa 20 69 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 47   idxNum==QUERY_G
32efb 45 4e 45 52 49 43 20 74 68 65 6e 20 64 6f 20 61  ENERIC then do a
32efc 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e   full table scan
32efd 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20   against.** the 
32efe 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e  %_content table.
32eff 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d  .**.** If idxNum
32f00 3d 3d 51 55 45 52 59 5f 44 4f 43 49 44 20 74 68  ==QUERY_DOCID th
32f01 65 6e 20 64 6f 20 61 20 64 6f 63 69 64 20 6c 6f  en do a docid lo
32f02 6f 6b 75 70 20 66 6f 72 20 61 20 73 69 6e 67 6c  okup for a singl
32f03 65 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 74 68  e entry.** in th
32f04 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c  e %_content tabl
32f05 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e  e..**.** If idxN
32f06 75 6d 3e 3d 51 55 45 52 59 5f 46 55 4c 4c 54 45  um>=QUERY_FULLTE
32f07 58 54 20 74 68 65 6e 20 75 73 65 20 74 68 65 20  XT then use the 
32f08 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 2e  full text index.
32f09 20 20 54 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20    The.** column 
32f0a 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64  on the left-hand
32f0b 20 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54   side of the MAT
32f0c 43 48 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63  CH operator is c
32f0d 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20  olumn.** number 
32f0e 69 64 78 4e 75 6d 2d 51 55 45 52 59 5f 46 55 4c  idxNum-QUERY_FUL
32f0f 4c 54 45 58 54 2c 20 30 20 69 6e 64 65 78 65 64  LTEXT, 0 indexed
32f10 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 68  .  argv[0] is th
32f11 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20  e right-hand.** 
32f12 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
32f13 48 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 2f  H operator..*/./
32f14 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55 70  * TODO(shess) Up
32f15 67 72 61 64 65 20 74 68 65 20 63 75 72 73 6f 72  grade the cursor
32f16 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
32f17 61 6e 64 20 64 65 73 74 72 75 63 74 69 6f 6e 20  and destruction 
32f18 74 6f 0a 2a 2a 20 61 63 63 6f 75 6e 74 20 66 6f  to.** account fo
32f19 72 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72  r fulltextFilter
32f1a 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  () being called 
32f1b 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 6f  multiple times o
32f1c 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 75  n the.** same cu
32f1d 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 72 65  rsor.  The curre
32f1e 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 76  nt solution is v
32f1f 65 72 79 20 66 72 61 67 69 6c 65 2e 20 20 41 70  ery fragile.  Ap
32f20 70 6c 79 20 66 69 78 20 74 6f 0a 2a 2a 20 66 74  ply fix to.** ft
32f21 73 33 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  s3 as appropriat
32f22 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
32f23 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72 28   fulltextFilter(
32f24 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
32f25 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
32f26 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
32f27 6f 72 20 75 73 65 64 20 66 6f 72 20 74 68 69 73  or used for this
32f28 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20   query */.  int 
32f29 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20 63 68  idxNum, const ch
32f2a 61 72 20 2a 69 64 78 53 74 72 2c 20 20 20 2f 2a  ar *idxStr,   /*
32f2b 20 57 68 69 63 68 20 69 6e 64 65 78 69 6e 67 20   Which indexing 
32f2c 73 63 68 65 6d 65 20 74 6f 20 75 73 65 20 2a 2f  scheme to use */
32f2d 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  .  int argc, sql
32f2e 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
32f2f 76 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74  v    /* Argument
32f30 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 69  s for the indexi
32f31 6e 67 20 73 63 68 65 6d 65 20 2a 2f 0a 29 7b 0a  ng scheme */.){.
32f32 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f    fulltext_curso
32f33 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74  r *c = (fulltext
32f34 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  _cursor *) pCurs
32f35 6f 72 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  or;.  fulltext_v
32f36 74 61 62 20 2a 76 20 3d 20 63 75 72 73 6f 72 5f  tab *v = cursor_
32f37 76 74 61 62 28 63 29 3b 0a 20 20 69 6e 74 20 72  vtab(c);.  int r
32f38 63 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28  c;..  FTSTRACE((
32f39 22 46 54 53 33 20 46 69 6c 74 65 72 20 25 70 5c  "FTS3 Filter %p\
32f3a 6e 22 2c 70 43 75 72 73 6f 72 29 29 3b 0a 0a 20  n",pCursor));.. 
32f3b 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f   /* If the curso
32f3c 72 20 68 61 73 20 61 20 73 74 61 74 65 6d 65 6e  r has a statemen
32f3d 74 20 74 68 61 74 20 77 61 73 20 6e 6f 74 20 70  t that was not p
32f3e 72 65 70 61 72 65 64 20 61 63 63 6f 72 64 69 6e  repared accordin
32f3f 67 20 74 6f 0a 20 20 2a 2a 20 69 64 78 4e 75 6d  g to.  ** idxNum
32f40 2c 20 63 6c 65 61 72 20 69 74 2e 20 20 49 20 62  , clear it.  I b
32f41 65 6c 69 65 76 65 20 61 6c 6c 20 63 61 6c 6c 73  elieve all calls
32f42 20 74 6f 20 66 75 6c 6c 74 65 78 74 46 69 6c 74   to fulltextFilt
32f43 65 72 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 67  er with a.  ** g
32f44 69 76 65 6e 20 63 75 72 73 6f 72 20 77 69 6c 6c  iven cursor will
32f45 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 69   have the same i
32f46 64 78 4e 75 6d 20 2c 20 62 75 74 20 69 6e 20 74  dxNum , but in t
32f47 68 69 73 20 63 61 73 65 20 69 74 27 73 0a 20 20  his case it's.  
32f48 2a 2a 20 65 61 73 79 20 74 6f 20 62 65 20 73 61  ** easy to be sa
32f49 66 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  fe..  */.  if( c
32f4a 2d 3e 70 53 74 6d 74 20 26 26 20 63 2d 3e 69 43  ->pStmt && c->iC
32f4b 75 72 73 6f 72 54 79 70 65 21 3d 69 64 78 4e 75  ursorType!=idxNu
32f4c 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  m ){.    sqlite3
32f4d 5f 66 69 6e 61 6c 69 7a 65 28 63 2d 3e 70 53 74  _finalize(c->pSt
32f4e 6d 74 29 3b 0a 20 20 20 20 63 2d 3e 70 53 74 6d  mt);.    c->pStm
32f4f 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20  t = NULL;.  }.. 
32f50 20 2f 2a 20 47 65 74 20 61 20 66 72 65 73 68 20   /* Get a fresh 
32f51 73 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70  statement approp
32f52 72 69 61 74 65 20 74 6f 20 69 64 78 4e 75 6d 2e  riate to idxNum.
32f53 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
32f54 65 73 73 29 3a 20 41 64 64 20 61 20 70 72 65 70  ess): Add a prep
32f55 61 72 65 64 2d 73 74 61 74 65 6d 65 6e 74 20 63  ared-statement c
32f56 61 63 68 65 20 69 6e 20 74 68 65 20 76 74 20 73  ache in the vt s
32f57 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54  tructure..  ** T
32f58 68 65 20 63 61 63 68 65 20 6d 75 73 74 20 68 61  he cache must ha
32f59 6e 64 6c 65 20 6d 75 6c 74 69 70 6c 65 20 6f 70  ndle multiple op
32f5a 65 6e 20 63 75 72 73 6f 72 73 2e 20 20 45 61 73  en cursors.  Eas
32f5b 69 65 72 20 74 6f 20 63 61 63 68 65 20 74 68 65  ier to cache the
32f5c 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  .  ** statement 
32f5d 76 61 72 69 61 6e 74 73 20 61 74 20 74 68 65 20  variants at the 
32f5e 76 74 20 74 6f 20 72 65 64 75 63 65 20 6d 61 6c  vt to reduce mal
32f5f 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65  loc/realloc/free
32f60 20 68 65 72 65 2e 0a 20 20 2a 2a 20 4f 72 20 77   here..  ** Or w
32f61 65 20 63 6f 75 6c 64 20 68 61 76 65 20 61 20 53  e could have a S
32f62 74 72 69 6e 67 42 75 66 66 65 72 20 76 61 72 69  tringBuffer vari
32f63 61 6e 74 20 77 68 69 63 68 20 61 6c 6c 6f 77 65  ant which allowe
32f64 64 20 73 74 61 63 6b 0a 20 20 2a 2a 20 63 6f 6e  d stack.  ** con
32f65 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 73 6d  struction for sm
32f66 61 6c 6c 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f  all values..  */
32f67 0a 20 20 69 66 28 20 21 63 2d 3e 70 53 74 6d 74  .  if( !c->pStmt
32f68 20 29 7b 0a 20 20 20 20 53 74 72 69 6e 67 42 75   ){.    StringBu
32f69 66 66 65 72 20 73 62 3b 0a 20 20 20 20 69 6e 69  ffer sb;.    ini
32f6a 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73  tStringBuffer(&s
32f6b 62 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26  b);.    append(&
32f6c 73 62 2c 20 22 53 45 4c 45 43 54 20 64 6f 63 69  sb, "SELECT doci
32f6d 64 2c 20 22 29 3b 0a 20 20 20 20 61 70 70 65 6e  d, ");.    appen
32f6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d 3e 6e 43  dList(&sb, v->nC
32f6f 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74  olumn, v->azCont
32f70 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  entColumn);.    
32f71 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 46 52  append(&sb, " FR
32f72 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 22 29 3b 0a  OM %_content");.
32f73 20 20 20 20 69 66 28 20 69 64 78 4e 75 6d 21 3d      if( idxNum!=
32f74 51 55 45 52 59 5f 47 45 4e 45 52 49 43 20 29 20  QUERY_GENERIC ) 
32f75 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 57 48  append(&sb, " WH
32f76 45 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b  ERE docid = ?");
32f77 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 70 72  .    rc = sql_pr
32f78 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d 3e  epare(v->db, v->
32f79 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26  zDb, v->zName, &
32f7a 63 2d 3e 70 53 74 6d 74 2c 0a 20 20 20 20 20 20  c->pStmt,.      
32f7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
32f7c 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28  tringBufferData(
32f7d 26 73 62 29 29 3b 0a 20 20 20 20 73 74 72 69 6e  &sb));.    strin
32f7e 67 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  gBufferDestroy(&
32f7f 73 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  sb);.    if( rc!
32f80 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
32f81 75 72 6e 20 72 63 3b 0a 20 20 20 20 63 2d 3e 69  urn rc;.    c->i
32f82 43 75 72 73 6f 72 54 79 70 65 20 3d 20 69 64 78  CursorType = idx
32f83 4e 75 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Num;.  }else{.  
32f84 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
32f85 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 61  c->pStmt);.    a
32f86 73 73 65 72 74 28 20 63 2d 3e 69 43 75 72 73 6f  ssert( c->iCurso
32f87 72 54 79 70 65 3d 3d 69 64 78 4e 75 6d 20 29 3b  rType==idxNum );
32f88 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20  .  }..  switch( 
32f89 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20 63 61  idxNum ){.    ca
32f8a 73 65 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43  se QUERY_GENERIC
32f8b 3a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  :.      break;..
32f8c 20 20 20 20 63 61 73 65 20 51 55 45 52 59 5f 44      case QUERY_D
32f8d 4f 43 49 44 3a 0a 20 20 20 20 20 20 72 63 20 3d  OCID:.      rc =
32f8e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
32f8f 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 31 2c  t64(c->pStmt, 1,
32f90 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
32f91 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 29 3b 0a  nt64(argv[0]));.
32f92 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
32f93 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
32f94 20 72 63 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   rc;.      break
32f95 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  ;..    default: 
32f96 20 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73    /* full-text s
32f97 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 7b 0a 20  earch */.    {. 
32f98 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
32f99 69 64 78 4e 75 6d 2d 51 55 45 52 59 5f 46 55 4c  idxNum-QUERY_FUL
32f9a 4c 54 45 58 54 3b 0a 20 20 20 20 20 20 63 6f 6e  LTEXT;.      con
32f9b 73 74 20 63 68 61 72 20 2a 7a 51 75 65 72 79 20  st char *zQuery 
32f9c 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
32f9d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
32f9e 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
32f9f 20 20 20 61 73 73 65 72 74 28 20 69 64 78 4e 75     assert( idxNu
32fa0 6d 3c 3d 51 55 45 52 59 5f 46 55 4c 4c 54 45 58  m<=QUERY_FULLTEX
32fa1 54 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  T+v->nColumn);. 
32fa2 20 20 20 20 20 61 73 73 65 72 74 28 20 61 72 67       assert( arg
32fa3 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66  c==1 );.      if
32fa4 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74  ( c->result.nDat
32fa5 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  a!=0 ){.        
32fa6 2f 2a 20 54 68 69 73 20 63 61 73 65 20 68 61 70  /* This case hap
32fa7 70 65 6e 73 20 69 66 20 74 68 65 20 73 61 6d 65  pens if the same
32fa8 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20   cursor is used 
32fa9 72 65 70 65 61 74 65 64 6c 79 2e 20 2a 2f 0a 20  repeatedly. */. 
32faa 20 20 20 20 20 20 20 64 6c 72 44 65 73 74 72 6f         dlrDestro
32fab 79 28 26 63 2d 3e 72 65 61 64 65 72 29 3b 0a 20  y(&c->reader);. 
32fac 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
32fad 72 52 65 73 65 74 28 26 63 2d 3e 72 65 73 75 6c  rReset(&c->resul
32fae 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
32faf 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
32fb0 66 65 72 49 6e 69 74 28 26 63 2d 3e 72 65 73 75  ferInit(&c->resu
32fb1 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  lt, 0);.      }.
32fb2 20 20 20 20 20 20 72 63 20 3d 20 66 75 6c 6c 74        rc = fullt
32fb3 65 78 74 51 75 65 72 79 28 76 2c 20 69 43 6f 6c  extQuery(v, iCol
32fb4 2c 20 7a 51 75 65 72 79 2c 20 2d 31 2c 20 26 63  , zQuery, -1, &c
32fb5 2d 3e 72 65 73 75 6c 74 2c 20 26 63 2d 3e 70 45  ->result, &c->pE
32fb6 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  xpr);.      if( 
32fb7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
32fb8 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
32fb9 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e   if( c->result.n
32fba 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20  Data!=0 ){.     
32fbb 20 20 20 64 6c 72 49 6e 69 74 28 26 63 2d 3e 72     dlrInit(&c->r
32fbc 65 61 64 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53  eader, DL_DOCIDS
32fbd 2c 20 63 2d 3e 72 65 73 75 6c 74 2e 70 44 61 74  , c->result.pDat
32fbe 61 2c 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61  a, c->result.nDa
32fbf 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ta);.      }.   
32fc0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
32fc1 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 66 75    }..  return fu
32fc2 6c 6c 74 65 78 74 4e 65 78 74 28 70 43 75 72 73  lltextNext(pCurs
32fc3 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  or);.}../* This 
32fc4 69 73 20 74 68 65 20 78 45 6f 66 20 6d 65 74 68  is the xEof meth
32fc5 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  od of the virtua
32fc6 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 53 51  l table.  The SQ
32fc7 4c 69 74 65 20 63 6f 72 65 0a 2a 2a 20 63 61 6c  Lite core.** cal
32fc8 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
32fc9 74 6f 20 66 69 6e 64 20 6f 75 74 20 69 66 20 69  to find out if i
32fca 74 20 68 61 73 20 72 65 61 63 68 65 64 20 74 68  t has reached th
32fcb 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 61 20 71 75  e end of.** a qu
32fcc 65 72 79 27 73 20 72 65 73 75 6c 74 73 20 73 65  ery's results se
32fcd 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
32fce 20 66 75 6c 6c 74 65 78 74 45 6f 66 28 73 71 6c   fulltextEof(sql
32fcf 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
32fd0 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75   *pCursor){.  fu
32fd1 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63  lltext_cursor *c
32fd2 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72   = (fulltext_cur
32fd3 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
32fd4 20 20 72 65 74 75 72 6e 20 63 2d 3e 65 6f 66 3b    return c->eof;
32fd5 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20 74  .}../* This is t
32fd6 68 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68 6f  he xColumn metho
32fd7 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
32fd8 20 74 61 62 6c 65 2e 20 20 54 68 65 20 53 51 4c   table.  The SQL
32fd9 69 74 65 0a 2a 2a 20 63 6f 72 65 20 63 61 6c 6c  ite.** core call
32fda 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 64 75  s this method du
32fdb 72 69 6e 67 20 61 20 71 75 65 72 79 20 77 68 65  ring a query whe
32fdc 6e 20 69 74 20 6e 65 65 64 73 20 74 68 65 20 76  n it needs the v
32fdd 61 6c 75 65 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c  alue.** of a col
32fde 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20 76 69 72  umn from the vir
32fdf 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69  tual table.  Thi
32fe0 73 20 6d 65 74 68 6f 64 20 6e 65 65 64 73 20 74  s method needs t
32fe1 6f 20 75 73 65 0a 2a 2a 20 6f 6e 65 20 6f 66 20  o use.** one of 
32fe2 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
32fe3 6c 74 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  lt_*() routines 
32fe4 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 71  to store the req
32fe5 75 65 73 74 65 64 0a 2a 2a 20 76 61 6c 75 65 20  uested.** value 
32fe6 62 61 63 6b 20 69 6e 20 74 68 65 20 70 43 6f 6e  back in the pCon
32fe7 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  text..*/.static 
32fe8 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75  int fulltextColu
32fe9 6d 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  mn(sqlite3_vtab_
32fea 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
32feb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32fec 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
32fed 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  e3_context *pCon
32fee 74 65 78 74 2c 20 69 6e 74 20 69 64 78 43 6f 6c  text, int idxCol
32fef 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
32ff0 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74  rsor *c = (fullt
32ff1 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43  ext_cursor *) pC
32ff2 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c 74 65 78  ursor;.  fulltex
32ff3 74 5f 76 74 61 62 20 2a 76 20 3d 20 63 75 72 73  t_vtab *v = curs
32ff4 6f 72 5f 76 74 61 62 28 63 29 3b 0a 0a 20 20 69  or_vtab(c);..  i
32ff5 66 28 20 69 64 78 43 6f 6c 3c 76 2d 3e 6e 43 6f  f( idxCol<v->nCo
32ff6 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69  lumn ){.    sqli
32ff7 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
32ff8 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
32ff9 5f 76 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c  _value(c->pStmt,
32ffa 20 69 64 78 43 6f 6c 2b 31 29 3b 0a 20 20 20 20   idxCol+1);.    
32ffb 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
32ffc 61 6c 75 65 28 70 43 6f 6e 74 65 78 74 2c 20 70  alue(pContext, p
32ffd 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Val);.  }else if
32ffe 28 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f  ( idxCol==v->nCo
32fff 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54  lumn ){.    /* T
33000 68 65 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 20  he extra column 
33001 77 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 74 68  whose name is th
33002 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 74 61  e same as the ta
33003 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75  ble..    ** Retu
33004 72 6e 20 61 20 62 6c 6f 62 20 77 68 69 63 68 20  rn a blob which 
33005 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
33006 74 68 65 20 63 75 72 73 6f 72 0a 20 20 20 20 2a  the cursor.    *
33007 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  /.    sqlite3_re
33008 73 75 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e 74 65  sult_blob(pConte
33009 78 74 2c 20 26 63 2c 20 73 69 7a 65 6f 66 28 63  xt, &c, sizeof(c
3300a 29 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  ), SQLITE_TRANSI
3300b 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ENT);.  }else if
3300c 28 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f  ( idxCol==v->nCo
3300d 6c 75 6d 6e 2b 31 20 29 7b 0a 20 20 20 20 2f 2a  lumn+1 ){.    /*
3300e 20 54 68 65 20 64 6f 63 69 64 20 63 6f 6c 75 6d   The docid colum
3300f 6e 2c 20 77 68 69 63 68 20 69 73 20 61 6e 20 61  n, which is an a
33010 6c 69 61 73 20 66 6f 72 20 72 6f 77 69 64 2e 20  lias for rowid. 
33011 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  */.    sqlite3_v
33012 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 73 71 6c  alue *pVal = sql
33013 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
33014 65 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a  e(c->pStmt, 0);.
33015 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
33016 6c 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65 78  lt_value(pContex
33017 74 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20  t, pVal);.  }.  
33018 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
33019 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20  ;.}../* This is 
3301a 74 68 65 20 78 52 6f 77 69 64 20 6d 65 74 68 6f  the xRowid metho
3301b 64 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63  d.  The SQLite c
3301c 6f 72 65 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ore calls this r
3301d 6f 75 74 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 74  outine to.** ret
3301e 72 69 65 76 65 20 74 68 65 20 72 6f 77 69 64 20  rieve the rowid 
3301f 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
33020 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
33021 74 20 73 65 74 2e 20 20 66 74 73 33 0a 2a 2a 20  t set.  fts3.** 
33022 65 78 70 6f 73 65 73 20 25 5f 63 6f 6e 74 65 6e  exposes %_conten
33023 74 2e 64 6f 63 69 64 20 61 73 20 74 68 65 20 72  t.docid as the r
33024 6f 77 69 64 20 66 6f 72 20 74 68 65 20 76 69 72  owid for the vir
33025 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  tual table.  The
33026 0a 2a 2a 20 72 6f 77 69 64 20 73 68 6f 75 6c 64  .** rowid should
33027 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 2a   be written to *
33028 70 52 6f 77 69 64 2e 0a 2a 2f 0a 73 74 61 74 69  pRowid..*/.stati
33029 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 52 6f  c int fulltextRo
3302a 77 69 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  wid(sqlite3_vtab
3302b 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
3302c 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a  , sqlite_int64 *
3302d 70 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c 74  pRowid){.  fullt
3302e 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  ext_cursor *c = 
3302f 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72  (fulltext_cursor
33030 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 0a 20 20   *) pCursor;..  
33031 2a 70 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  *pRowid = sqlite
33032 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 63  3_column_int64(c
33033 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 72  ->pStmt, 0);.  r
33034 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33035 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 6c 6c 20 74  .}../* Add all t
33036 65 72 6d 73 20 69 6e 20 5b 7a 54 65 78 74 5d 20  erms in [zText] 
33037 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  to pendingTerms 
33038 74 61 62 6c 65 2e 20 20 49 66 20 5b 69 43 6f 6c  table.  If [iCol
33039 75 6d 6e 5d 20 3e 20 30 2c 0a 2a 2a 20 77 65 20  umn] > 0,.** we 
3303a 61 6c 73 6f 20 73 74 6f 72 65 20 70 6f 73 69 74  also store posit
3303b 69 6f 6e 73 20 61 6e 64 20 6f 66 66 73 65 74 73  ions and offsets
3303c 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62   in the hash tab
3303d 6c 65 20 75 73 69 6e 67 20 74 68 61 74 0a 2a 2a  le using that.**
3303e 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a   column number..
3303f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 75  */.static int bu
33040 69 6c 64 54 65 72 6d 73 28 66 75 6c 6c 74 65 78  ildTerms(fulltex
33041 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
33042 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a  e_int64 iDocid,.
33043 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33044 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
33045 20 2a 7a 54 65 78 74 2c 20 69 6e 74 20 69 43 6f   *zText, int iCo
33046 6c 75 6d 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33  lumn){.  sqlite3
33047 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
33048 65 6e 69 7a 65 72 20 3d 20 76 2d 3e 70 54 6f 6b  enizer = v->pTok
33049 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65  enizer;.  sqlite
3304a 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
3304b 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 63  or *pCursor;.  c
3304c 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 6f 6b 65  onst char *pToke
3304d 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 42  n;.  int nTokenB
3304e 79 74 65 73 3b 0a 20 20 69 6e 74 20 69 53 74 61  ytes;.  int iSta
3304f 72 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f 66  rtOffset, iEndOf
33050 66 73 65 74 2c 20 69 50 6f 73 69 74 69 6f 6e 3b  fset, iPosition;
33051 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63  .  int rc;..  rc
33052 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70   = pTokenizer->p
33053 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54  Module->xOpen(pT
33054 6f 6b 65 6e 69 7a 65 72 2c 20 7a 54 65 78 74 2c  okenizer, zText,
33055 20 2d 31 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a   -1, &pCursor);.
33056 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33057 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
33058 0a 0a 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f  ..  pCursor->pTo
33059 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
3305a 69 7a 65 72 3b 0a 20 20 77 68 69 6c 65 28 20 53  izer;.  while( S
3305b 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 3d 70 54  QLITE_OK==(rc=pT
3305c 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
3305d 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72  e->xNext(pCursor
3305e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3305f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33061 20 20 20 20 20 26 70 54 6f 6b 65 6e 2c 20 26 6e       &pToken, &n
33062 54 6f 6b 65 6e 42 79 74 65 73 2c 0a 20 20 20 20  TokenBytes,.    
33063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33064 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33065 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
33066 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 26 69  iStartOffset, &i
33067 45 6e 64 4f 66 66 73 65 74 2c 0a 20 20 20 20 20  EndOffset,.     
33068 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33069 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3306a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69                &i
3306b 50 6f 73 69 74 69 6f 6e 29 29 20 29 7b 0a 20 20  Position)) ){.  
3306c 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70    DLCollector *p
3306d 3b 0a 20 20 20 20 69 6e 74 20 6e 44 61 74 61 3b  ;.    int nData;
3306e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3306f 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f     /* Size of do
33070 63 6c 69 73 74 20 62 65 66 6f 72 65 20 6f 75 72  clist before our
33071 20 75 70 64 61 74 65 2e 20 2a 2f 0a 0a 20 20 20   update. */..   
33072 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 73 20 63 61   /* Positions ca
33073 6e 27 74 20 62 65 20 6e 65 67 61 74 69 76 65 3b  n't be negative;
33074 20 77 65 20 75 73 65 20 2d 31 20 61 73 20 61 20   we use -1 as a 
33075 74 65 72 6d 69 6e 61 74 6f 72 0a 20 20 20 20 20  terminator.     
33076 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 20 54  * internally.  T
33077 6f 6b 65 6e 20 63 61 6e 27 74 20 62 65 20 4e 55  oken can't be NU
33078 4c 4c 20 6f 72 20 65 6d 70 74 79 2e 20 2a 2f 0a  LL or empty. */.
33079 20 20 20 20 69 66 28 20 69 50 6f 73 69 74 69 6f      if( iPositio
3307a 6e 3c 30 20 7c 7c 20 70 54 6f 6b 65 6e 20 3d 3d  n<0 || pToken ==
3307b 20 4e 55 4c 4c 20 7c 7c 20 6e 54 6f 6b 65 6e 42   NULL || nTokenB
3307c 79 74 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20  ytes == 0 ){.   
3307d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
3307e 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61  RROR;.      brea
3307f 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 20  k;.    }..    p 
33080 3d 20 66 74 73 33 48 61 73 68 46 69 6e 64 28 26  = fts3HashFind(&
33081 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c  v->pendingTerms,
33082 20 70 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42   pToken, nTokenB
33083 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 70  ytes);.    if( p
33084 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  ==NULL ){.      
33085 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 20  nData = 0;.     
33086 20 70 20 3d 20 64 6c 63 4e 65 77 28 69 44 6f 63   p = dlcNew(iDoc
33087 69 64 2c 20 44 4c 5f 44 45 46 41 55 4c 54 29 3b  id, DL_DEFAULT);
33088 0a 20 20 20 20 20 20 66 74 73 33 48 61 73 68 49  .      fts3HashI
33089 6e 73 65 72 74 28 26 76 2d 3e 70 65 6e 64 69 6e  nsert(&v->pendin
3308a 67 54 65 72 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20  gTerms, pToken, 
3308b 6e 54 6f 6b 65 6e 42 79 74 65 73 2c 20 70 29 3b  nTokenBytes, p);
3308c 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 68  ..      /* Overh
3308d 65 61 64 20 66 6f 72 20 6f 75 72 20 68 61 73 68  ead for our hash
3308e 20 74 61 62 6c 65 20 65 6e 74 72 79 2c 20 74 68   table entry, th
3308f 65 20 6b 65 79 2c 20 61 6e 64 20 74 68 65 20 76  e key, and the v
33090 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 20 20 76  alue. */.      v
33091 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 2b  ->nPendingData +
33092 3d 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  = sizeof(struct 
33093 66 74 73 33 48 61 73 68 45 6c 65 6d 29 2b 73 69  fts3HashElem)+si
33094 7a 65 6f 66 28 2a 70 29 2b 6e 54 6f 6b 65 6e 42  zeof(*p)+nTokenB
33095 79 74 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ytes;.    }else{
33096 0a 20 20 20 20 20 20 6e 44 61 74 61 20 3d 20 70  .      nData = p
33097 2d 3e 62 2e 6e 44 61 74 61 3b 0a 20 20 20 20 20  ->b.nData;.     
33098 20 69 66 28 20 70 2d 3e 64 6c 77 2e 69 50 72 65   if( p->dlw.iPre
33099 76 44 6f 63 69 64 21 3d 69 44 6f 63 69 64 20 29  vDocid!=iDocid )
3309a 20 64 6c 63 4e 65 78 74 28 70 2c 20 69 44 6f 63   dlcNext(p, iDoc
3309b 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  id);.    }.    i
3309c 66 28 20 69 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b  f( iColumn>=0 ){
3309d 0a 20 20 20 20 20 20 64 6c 63 41 64 64 50 6f 73  .      dlcAddPos
3309e 28 70 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f  (p, iColumn, iPo
3309f 73 69 74 69 6f 6e 2c 20 69 53 74 61 72 74 4f 66  sition, iStartOf
330a0 66 73 65 74 2c 20 69 45 6e 64 4f 66 66 73 65 74  fset, iEndOffset
330a1 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
330a2 20 41 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61   Accumulate data
330a3 20 61 64 64 65 64 20 62 79 20 64 6c 63 4e 65 77   added by dlcNew
330a4 20 6f 72 20 64 6c 63 4e 65 78 74 2c 20 61 6e 64   or dlcNext, and
330a5 20 64 6c 63 41 64 64 50 6f 73 2e 20 2a 2f 0a 20   dlcAddPos. */. 
330a6 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61     v->nPendingDa
330a7 74 61 20 2b 3d 20 70 2d 3e 62 2e 6e 44 61 74 61  ta += p->b.nData
330a8 2d 6e 44 61 74 61 3b 0a 20 20 7d 0a 0a 20 20 2f  -nData;.  }..  /
330a9 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 68  * TODO(shess) Ch
330aa 65 63 6b 20 72 65 74 75 72 6e 3f 20 20 53 68 6f  eck return?  Sho
330ab 75 6c 64 20 74 68 69 73 20 62 65 20 61 62 6c 65  uld this be able
330ac 20 74 6f 20 63 61 75 73 65 20 65 72 72 6f 72 73   to cause errors
330ad 20 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 70 6f   at.  ** this po
330ae 69 6e 74 3f 20 20 41 63 74 75 61 6c 6c 79 2c 20  int?  Actually, 
330af 73 61 6d 65 20 71 75 65 73 74 69 6f 6e 20 61 62  same question ab
330b0 6f 75 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  out sqlite3_fina
330b1 6c 69 7a 65 28 29 2c 0a 20 20 2a 2a 20 74 68 6f  lize(),.  ** tho
330b2 75 67 68 20 6f 6e 65 20 63 6f 75 6c 64 20 61 72  ugh one could ar
330b3 67 75 65 20 74 68 61 74 20 66 61 69 6c 75 72 65  gue that failure
330b4 20 74 68 65 72 65 20 6d 65 61 6e 73 20 74 68 61   there means tha
330b5 74 20 74 68 65 20 64 61 74 61 20 69 73 0a 20 20  t the data is.  
330b6 2a 2a 20 6e 6f 74 20 64 75 72 61 62 6c 65 2e 20  ** not durable. 
330b7 20 2a 70 6f 6e 64 65 72 2a 0a 20 20 2a 2f 0a 20   *ponder*.  */. 
330b8 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
330b9 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75  dule->xClose(pCu
330ba 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 53 51 4c  rsor);.  if( SQL
330bb 49 54 45 5f 44 4f 4e 45 20 3d 3d 20 72 63 20 29  ITE_DONE == rc )
330bc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
330bd 4b 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  K;.  return rc;.
330be 7d 0a 0a 2f 2a 20 41 64 64 20 64 6f 63 6c 69 73  }../* Add doclis
330bf 74 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d 73  ts for all terms
330c0 20 69 6e 20 5b 70 56 61 6c 75 65 73 5d 20 74 6f   in [pValues] to
330c1 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 74 61   pendingTerms ta
330c2 62 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  ble. */.static i
330c3 6e 74 20 69 6e 73 65 72 74 54 65 72 6d 73 28 66  nt insertTerms(f
330c4 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
330c5 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
330c6 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ocid,.          
330c7 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
330c8 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
330c9 6c 75 65 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  lues){.  int i;.
330ca 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 20 3c    for(i = 0; i <
330cb 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3b 20 2b 2b   v->nColumn ; ++
330cc 69 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  i){.    char *zT
330cd 65 78 74 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  ext = (char*)sql
330ce 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
330cf 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20 20  pValues[i]);.   
330d0 20 69 6e 74 20 72 63 20 3d 20 62 75 69 6c 64 54   int rc = buildT
330d1 65 72 6d 73 28 76 2c 20 69 44 6f 63 69 64 2c 20  erms(v, iDocid, 
330d2 7a 54 65 78 74 2c 20 69 29 3b 0a 20 20 20 20 69  zText, i);.    i
330d3 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
330d4 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
330d5 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
330d6 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20  E_OK;.}../* Add 
330d7 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20 66  empty doclists f
330d8 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20  or all terms in 
330d9 74 68 65 20 67 69 76 65 6e 20 72 6f 77 27 73 20  the given row's 
330da 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 70 65  content to.** pe
330db 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73  ndingTerms..*/.s
330dc 74 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65  tatic int delete
330dd 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76  Terms(fulltext_v
330de 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69  tab *v, sqlite_i
330df 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20  nt64 iDocid){.  
330e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 56 61  const char **pVa
330e1 6c 75 65 73 3b 0a 20 20 69 6e 74 20 69 2c 20 72  lues;.  int i, r
330e2 63 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  c;..  /* TODO(sh
330e3 65 73 73 29 20 53 68 6f 75 6c 64 20 77 65 20 61  ess) Should we a
330e4 6c 6c 6f 77 20 73 75 63 68 20 74 61 62 6c 65 73  llow such tables
330e5 20 61 74 20 61 6c 6c 3f 20 2a 2f 0a 20 20 69 66   at all? */.  if
330e6 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c  ( DL_DEFAULT==DL
330e7 5f 44 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e  _DOCIDS ) return
330e8 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a   SQLITE_ERROR;..
330e9 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 73    rc = content_s
330ea 65 6c 65 63 74 28 76 2c 20 69 44 6f 63 69 64 2c  elect(v, iDocid,
330eb 20 26 70 56 61 6c 75 65 73 29 3b 0a 20 20 69 66   &pValues);.  if
330ec 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
330ed 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
330ee 66 6f 72 28 69 20 3d 20 30 20 3b 20 69 20 3c 20  for(i = 0 ; i < 
330ef 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29  v->nColumn; ++i)
330f0 20 7b 0a 20 20 20 20 72 63 20 3d 20 62 75 69 6c   {.    rc = buil
330f1 64 54 65 72 6d 73 28 76 2c 20 69 44 6f 63 69 64  dTerms(v, iDocid
330f2 2c 20 70 56 61 6c 75 65 73 5b 69 5d 2c 20 2d 31  , pValues[i], -1
330f3 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
330f4 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b  QLITE_OK ) break
330f5 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 53 74 72  ;.  }..  freeStr
330f6 69 6e 67 41 72 72 61 79 28 76 2d 3e 6e 43 6f 6c  ingArray(v->nCol
330f7 75 6d 6e 2c 20 70 56 61 6c 75 65 73 29 3b 0a 20  umn, pValues);. 
330f8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
330f9 4b 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  K;.}../* TODO(sh
330fa 65 73 73 29 20 52 65 66 61 63 74 6f 72 20 74 68  ess) Refactor th
330fb 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65  e code to remove
330fc 20 74 68 69 73 20 66 6f 72 77 61 72 64 20 64 65   this forward de
330fd 63 6c 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  cl. */.static in
330fe 74 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72  t initPendingTer
330ff 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ms(fulltext_vtab
33100 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36   *v, sqlite_int6
33101 34 20 69 44 6f 63 69 64 29 3b 0a 0a 2f 2a 20 49  4 iDocid);../* I
33102 6e 73 65 72 74 20 61 20 72 6f 77 20 69 6e 74 6f  nsert a row into
33103 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
33104 61 62 6c 65 3b 20 73 65 74 20 2a 70 69 44 6f 63  able; set *piDoc
33105 69 64 20 74 6f 20 62 65 20 74 68 65 20 49 44 20  id to be the ID 
33106 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 72 6f  of the.** new ro
33107 77 2e 20 20 41 64 64 20 64 6f 63 6c 69 73 74 73  w.  Add doclists
33108 20 66 6f 72 20 74 65 72 6d 73 20 74 6f 20 70 65   for terms to pe
33109 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73  ndingTerms..*/.s
3310a 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f  tatic int index_
3310b 69 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f  insert(fulltext_
3310c 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33  vtab *v, sqlite3
3310d 5f 76 61 6c 75 65 20 2a 70 52 65 71 75 65 73 74  _value *pRequest
3310e 44 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20  Docid,.         
3310f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
33110 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
33111 56 61 6c 75 65 73 2c 20 73 71 6c 69 74 65 5f 69  Values, sqlite_i
33112 6e 74 36 34 20 2a 70 69 44 6f 63 69 64 29 7b 0a  nt64 *piDocid){.
33113 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20    int rc;..  rc 
33114 3d 20 63 6f 6e 74 65 6e 74 5f 69 6e 73 65 72 74  = content_insert
33115 28 76 2c 20 70 52 65 71 75 65 73 74 44 6f 63 69  (v, pRequestDoci
33116 64 2c 20 70 56 61 6c 75 65 73 29 3b 20 20 2f 2a  d, pValues);  /*
33117 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20   execute an SQL 
33118 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 66 28 20  INSERT */.  if( 
33119 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3311a 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
3311b 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 20 69 73   docid column is
3311c 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f   an alias for ro
3311d 77 69 64 2e 20 2a 2f 0a 20 20 2a 70 69 44 6f 63  wid. */.  *piDoc
3311e 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73  id = sqlite3_las
3311f 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 76  t_insert_rowid(v
33120 2d 3e 64 62 29 3b 0a 20 20 72 63 20 3d 20 69 6e  ->db);.  rc = in
33121 69 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  itPendingTerms(v
33122 2c 20 2a 70 69 44 6f 63 69 64 29 3b 0a 20 20 69  , *piDocid);.  i
33123 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
33124 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
33125 20 72 65 74 75 72 6e 20 69 6e 73 65 72 74 54 65   return insertTe
33126 72 6d 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 2c  rms(v, *piDocid,
33127 20 70 56 61 6c 75 65 73 29 3b 0a 7d 0a 0a 2f 2a   pValues);.}../*
33128 20 44 65 6c 65 74 65 20 61 20 72 6f 77 20 66 72   Delete a row fr
33129 6f 6d 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  om the %_content
3312a 20 74 61 62 6c 65 3b 20 61 64 64 20 65 6d 70 74   table; add empt
3312b 79 20 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 74  y doclists for t
3312c 65 72 6d 73 0a 2a 2a 20 74 6f 20 70 65 6e 64 69  erms.** to pendi
3312d 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74  ngTerms..*/.stat
3312e 69 63 20 69 6e 74 20 69 6e 64 65 78 5f 64 65 6c  ic int index_del
3312f 65 74 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ete(fulltext_vta
33130 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
33131 36 34 20 69 52 6f 77 29 7b 0a 20 20 69 6e 74 20  64 iRow){.  int 
33132 72 63 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67  rc = initPending
33133 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a  Terms(v, iRow);.
33134 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33135 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
33136 0a 0a 20 20 72 63 20 3d 20 64 65 6c 65 74 65 54  ..  rc = deleteT
33137 65 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20  erms(v, iRow);. 
33138 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33139 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
3313a 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 65 6e  .  return conten
3313b 74 5f 64 65 6c 65 74 65 28 76 2c 20 69 52 6f 77  t_delete(v, iRow
3313c 29 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61  );  /* execute a
3313d 6e 20 53 51 4c 20 44 45 4c 45 54 45 20 2a 2f 0a  n SQL DELETE */.
3313e 7d 0a 0a 2f 2a 20 55 70 64 61 74 65 20 61 20 72  }../* Update a r
3313f 6f 77 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74  ow in the %_cont
33140 65 6e 74 20 74 61 62 6c 65 3b 20 61 64 64 20 64  ent table; add d
33141 65 6c 65 74 65 20 64 6f 63 6c 69 73 74 73 20 74  elete doclists t
33142 6f 0a 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d  o.** pendingTerm
33143 73 20 66 6f 72 20 6f 6c 64 20 74 65 72 6d 73 20  s for old terms 
33144 6e 6f 74 20 69 6e 20 74 68 65 20 6e 65 77 20 64  not in the new d
33145 61 74 61 2c 20 61 64 64 20 69 6e 73 65 72 74 20  ata, add insert 
33146 64 6f 63 6c 69 73 74 73 0a 2a 2a 20 74 6f 20 70  doclists.** to p
33147 65 6e 64 69 6e 67 54 65 72 6d 73 20 66 6f 72 20  endingTerms for 
33148 74 65 72 6d 73 20 69 6e 20 74 68 65 20 6e 65 77  terms in the new
33149 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63   data..*/.static
3314a 20 69 6e 74 20 69 6e 64 65 78 5f 75 70 64 61 74   int index_updat
3314b 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
3314c 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
3314d 20 69 52 6f 77 2c 0a 20 20 20 20 20 20 20 20 20   iRow,.         
3314e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3314f 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
33150 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e 74 20 72  Values){.  int r
33151 63 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54  c = initPendingT
33152 65 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20  erms(v, iRow);. 
33153 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33154 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
33155 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
33156 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 20  n empty doclist 
33157 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 74 68  for each term th
33158 61 74 20 70 72 65 76 69 6f 75 73 6c 79 20 61 70  at previously ap
33159 70 65 61 72 65 64 20 69 6e 20 74 68 69 73 0a 20  peared in this. 
3315a 20 20 2a 20 72 6f 77 2e 20 2a 2f 0a 20 20 72 63    * row. */.  rc
3315b 20 3d 20 64 65 6c 65 74 65 54 65 72 6d 73 28 76   = deleteTerms(v
3315c 2c 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72  , iRow);.  if( r
3315d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3315e 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
3315f 3d 20 63 6f 6e 74 65 6e 74 5f 75 70 64 61 74 65  = content_update
33160 28 76 2c 20 70 56 61 6c 75 65 73 2c 20 69 52 6f  (v, pValues, iRo
33161 77 29 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20  w);  /* execute 
33162 61 6e 20 53 51 4c 20 55 50 44 41 54 45 20 2a 2f  an SQL UPDATE */
33163 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
33164 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
33165 3b 0a 0a 20 20 2f 2a 20 4e 6f 77 20 61 64 64 20  ;..  /* Now add 
33166 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 65  positions for te
33167 72 6d 73 20 77 68 69 63 68 20 61 70 70 65 61 72  rms which appear
33168 20 69 6e 20 74 68 65 20 75 70 64 61 74 65 64 20   in the updated 
33169 72 6f 77 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  row. */.  return
3316a 20 69 6e 73 65 72 74 54 65 72 6d 73 28 76 2c 20   insertTerms(v, 
3316b 69 52 6f 77 2c 20 70 56 61 6c 75 65 73 29 3b 0a  iRow, pValues);.
3316c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
3316d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3316e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3316f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33170 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74 65  *******/./* Inte
33171 72 69 6f 72 57 72 69 74 65 72 20 69 73 20 75 73  riorWriter is us
33172 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 65  ed to collect te
33173 72 6d 73 20 61 6e 64 20 62 6c 6f 63 6b 20 72 65  rms and block re
33174 66 65 72 65 6e 63 65 73 20 69 6e 74 6f 0a 2a 2a  ferences into.**
33175 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
33176 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73 2e 20 20  in %_segments.  
33177 53 65 65 20 63 6f 6d 6d 65 6e 74 61 72 79 20 61  See commentary a
33178 74 20 74 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f  t top of file fo
33179 72 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2f 0a  r.** format..*/.
3317a 0a 2f 2a 20 48 6f 77 20 6c 61 72 67 65 20 69 6e  ./* How large in
3317b 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 63 61 6e  terior nodes can
3317c 20 67 72 6f 77 2e 20 2a 2f 0a 23 64 65 66 69 6e   grow. */.#defin
3317d 65 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 32  e INTERIOR_MAX 2
3317e 30 34 38 0a 0a 2f 2a 20 4d 69 6e 69 6d 75 6d 20  048../* Minimum 
3317f 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  number of terms 
33180 70 65 72 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  per interior nod
33181 65 20 28 65 78 63 65 70 74 20 74 68 65 20 72 6f  e (except the ro
33182 6f 74 29 2e 20 54 68 69 73 0a 2a 2a 20 70 72 65  ot). This.** pre
33183 76 65 6e 74 73 20 6c 61 72 67 65 20 74 65 72 6d  vents large term
33184 73 20 66 72 6f 6d 20 6d 61 6b 69 6e 67 20 74 68  s from making th
33185 65 20 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e  e tree too skinn
33186 79 20 2d 20 6d 75 73 74 20 62 65 20 3e 30 0a 2a  y - must be >0.*
33187 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 74 72  * so that the tr
33188 65 65 20 61 6c 77 61 79 73 20 6d 61 6b 65 73 20  ee always makes 
33189 70 72 6f 67 72 65 73 73 2e 20 20 4e 6f 74 65 20  progress.  Note 
3318a 74 68 61 74 20 74 68 65 20 6d 69 6e 20 74 72 65  that the min tre
3318b 65 0a 2a 2a 20 66 61 6e 6f 75 74 20 77 69 6c 6c  e.** fanout will
3318c 20 62 65 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e   be INTERIOR_MIN
3318d 5f 54 45 52 4d 53 2b 31 2e 0a 2a 2f 0a 23 64 65  _TERMS+1..*/.#de
3318e 66 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f 4d 49  fine INTERIOR_MI
3318f 4e 5f 54 45 52 4d 53 20 37 0a 23 69 66 20 49 4e  N_TERMS 7.#if IN
33190 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53  TERIOR_MIN_TERMS
33191 3c 31 0a 23 20 65 72 72 6f 72 20 49 4e 54 45 52  <1.# error INTER
33192 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 6d 75  IOR_MIN_TERMS mu
33193 73 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68  st be greater th
33194 61 6e 20 30 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a  an 0..#endif../*
33195 20 52 4f 4f 54 5f 4d 41 58 20 63 6f 6e 74 72 6f   ROOT_MAX contro
33196 6c 73 20 68 6f 77 20 6d 75 63 68 20 64 61 74 61  ls how much data
33197 20 69 73 20 73 74 6f 72 65 64 20 69 6e 6c 69 6e   is stored inlin
33198 65 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  e in the segment
33199 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a  .** directory..*
3319a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
3319b 20 50 75 73 68 20 52 4f 4f 54 5f 4d 41 58 20 64   Push ROOT_MAX d
3319c 6f 77 6e 20 74 6f 20 77 68 6f 65 76 65 72 20 69  own to whoever i
3319d 73 20 77 72 69 74 69 6e 67 20 74 68 69 6e 67 73  s writing things
3319e 2e 20 20 49 74 27 73 0a 2a 2a 20 6f 6e 6c 79 20  .  It's.** only 
3319f 68 65 72 65 20 73 6f 20 74 68 61 74 20 69 6e 74  here so that int
331a0 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49  eriorWriterRootI
331a1 6e 66 6f 28 29 20 61 6e 64 20 6c 65 61 66 57 72  nfo() and leafWr
331a2 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29 0a 2a  iterRootInfo().*
331a3 2a 20 63 61 6e 20 62 6f 74 68 20 73 65 65 20 69  * can both see i
331a4 74 2c 20 62 75 74 20 69 66 20 74 68 65 20 63 61  t, but if the ca
331a5 6c 6c 65 72 20 70 61 73 73 65 64 20 69 74 20 69  ller passed it i
331a6 6e 2c 20 77 65 20 77 6f 75 6c 64 6e 27 74 20 65  n, we wouldn't e
331a7 76 65 6e 0a 2a 2a 20 6e 65 65 64 20 61 20 64 65  ven.** need a de
331a8 66 69 6e 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  fine..*/.#define
331a9 20 52 4f 4f 54 5f 4d 41 58 20 31 30 32 34 0a 23   ROOT_MAX 1024.#
331aa 69 66 20 52 4f 4f 54 5f 4d 41 58 3c 56 41 52 49  if ROOT_MAX<VARI
331ab 4e 54 5f 4d 41 58 2a 32 0a 23 20 65 72 72 6f 72  NT_MAX*2.# error
331ac 20 52 4f 4f 54 5f 4d 41 58 20 6d 75 73 74 20 68   ROOT_MAX must h
331ad 61 76 65 20 65 6e 6f 75 67 68 20 73 70 61 63 65  ave enough space
331ae 20 66 6f 72 20 61 20 68 65 61 64 65 72 2e 0a 23   for a header..#
331af 65 6e 64 69 66 0a 0a 2f 2a 20 49 6e 74 65 72 69  endif../* Interi
331b0 6f 72 42 6c 6f 63 6b 20 73 74 6f 72 65 73 20 61  orBlock stores a
331b1 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66 20   linked-list of 
331b2 69 6e 74 65 72 69 6f 72 20 62 6c 6f 63 6b 73 20  interior blocks 
331b3 77 68 69 6c 65 20 61 20 6c 6f 77 65 72 0a 2a 2a  while a lower.**
331b4 20 6c 61 79 65 72 20 69 73 20 62 65 69 6e 67 20   layer is being 
331b5 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2f 0a  constructed..*/.
331b6 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49  typedef struct I
331b7 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 7b 0a 20  nteriorBlock {. 
331b8 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72 6d   DataBuffer term
331b9 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ;           /* L
331ba 65 66 74 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20  eftmost term in 
331bb 62 6c 6f 63 6b 27 73 20 73 75 62 74 72 65 65 2e  block's subtree.
331bc 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
331bd 20 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20   data;          
331be 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 64 20   /* Accumulated 
331bf 64 61 74 61 20 66 6f 72 20 74 68 65 20 62 6c 6f  data for the blo
331c0 63 6b 2e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ck. */.  struct 
331c1 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 6e  InteriorBlock *n
331c2 65 78 74 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 42  ext;.} InteriorB
331c3 6c 6f 63 6b 3b 0a 0a 73 74 61 74 69 63 20 49 6e  lock;..static In
331c4 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 69 6e 74  teriorBlock *int
331c5 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 69 6e  eriorBlockNew(in
331c6 74 20 69 48 65 69 67 68 74 2c 20 73 71 6c 69 74  t iHeight, sqlit
331c7 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64 42 6c  e_int64 iChildBl
331c8 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ock,.           
331c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
331ca 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
331cb 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
331cc 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20 49 6e 74  nt nTerm){.  Int
331cd 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63  eriorBlock *bloc
331ce 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  k = sqlite3_mall
331cf 6f 63 28 73 69 7a 65 6f 66 28 49 6e 74 65 72 69  oc(sizeof(Interi
331d0 6f 72 42 6c 6f 63 6b 29 29 3b 0a 20 20 63 68 61  orBlock));.  cha
331d1 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56  r c[VARINT_MAX+V
331d2 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
331d3 74 20 6e 3b 0a 0a 20 20 69 66 28 20 62 6c 6f 63  t n;..  if( bloc
331d4 6b 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  k ){.    memset(
331d5 62 6c 6f 63 6b 2c 20 30 2c 20 73 69 7a 65 6f 66  block, 0, sizeof
331d6 28 2a 62 6c 6f 63 6b 29 29 3b 0a 20 20 20 20 64  (*block));.    d
331d7 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 62  ataBufferInit(&b
331d8 6c 6f 63 6b 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a  lock->term, 0);.
331d9 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
331da 70 6c 61 63 65 28 26 62 6c 6f 63 6b 2d 3e 74 65  place(&block->te
331db 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  rm, pTerm, nTerm
331dc 29 3b 0a 0a 20 20 20 20 6e 20 3d 20 66 74 73 33  );..    n = fts3
331dd 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69 48 65  PutVarint(c, iHe
331de 69 67 68 74 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  ight);.    n += 
331df 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
331e0 6e 2c 20 69 43 68 69 6c 64 42 6c 6f 63 6b 29 3b  n, iChildBlock);
331e1 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49  .    dataBufferI
331e2 6e 69 74 28 26 62 6c 6f 63 6b 2d 3e 64 61 74 61  nit(&block->data
331e3 2c 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 29 3b  , INTERIOR_MAX);
331e4 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52  .    dataBufferR
331e5 65 70 6c 61 63 65 28 26 62 6c 6f 63 6b 2d 3e 64  eplace(&block->d
331e6 61 74 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a  ata, c, n);.  }.
331e7 20 20 72 65 74 75 72 6e 20 62 6c 6f 63 6b 3b 0a    return block;.
331e8 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
331e9 47 0a 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74  G./* Verify that
331ea 20 74 68 65 20 64 61 74 61 20 69 73 20 72 65 61   the data is rea
331eb 64 61 62 6c 65 20 61 73 20 61 6e 20 69 6e 74 65  dable as an inte
331ec 72 69 6f 72 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74  rior node. */.st
331ed 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69  atic void interi
331ee 6f 72 42 6c 6f 63 6b 56 61 6c 69 64 61 74 65 28  orBlockValidate(
331ef 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 70  InteriorBlock *p
331f0 42 6c 6f 63 6b 29 7b 0a 20 20 63 6f 6e 73 74 20  Block){.  const 
331f1 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 70 42  char *pData = pB
331f2 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61  lock->data.pData
331f3 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 20 3d 20  ;.  int nData = 
331f4 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61  pBlock->data.nDa
331f5 74 61 3b 0a 20 20 69 6e 74 20 6e 2c 20 69 44 75  ta;.  int n, iDu
331f6 6d 6d 79 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  mmy;.  sqlite_in
331f7 74 36 34 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20  t64 iBlockid;.. 
331f8 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 30   assert( nData>0
331f9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   );.  assert( pD
331fa 61 74 61 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ata!=0 );.  asse
331fb 72 74 28 20 70 44 61 74 61 2b 6e 44 61 74 61 3e  rt( pData+nData>
331fc 70 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 4d  pData );..  /* M
331fd 75 73 74 20 6c 65 61 64 20 77 69 74 68 20 68 65  ust lead with he
331fe 69 67 68 74 20 6f 66 20 6e 6f 64 65 20 61 73 20  ight of node as 
331ff 61 20 76 61 72 69 6e 74 28 6e 29 2c 20 6e 3e 30  a varint(n), n>0
33200 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65   */.  n = fts3Ge
33201 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
33202 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73   &iDummy);.  ass
33203 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73  ert( n>0 );.  as
33204 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29  sert( iDummy>0 )
33205 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44  ;.  assert( n<nD
33206 61 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b  ata );.  pData +
33207 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20  = n;.  nData -= 
33208 6e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 63 6f  n;..  /* Must co
33209 6e 74 61 69 6e 20 69 42 6c 6f 63 6b 69 64 2e 20  ntain iBlockid. 
3320a 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
3320b 56 61 72 69 6e 74 28 70 44 61 74 61 2c 20 26 69  Varint(pData, &i
3320c 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65  Blockid);.  asse
3320d 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( n>0 );.  ass
3320e 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b  ert( n<=nData );
3320f 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20  .  pData += n;. 
33210 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20   nData -= n;..  
33211 2f 2a 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  /* Zero or more 
33212 74 65 72 6d 73 20 6f 66 20 70 6f 73 69 74 69 76  terms of positiv
33213 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 66  e length */.  if
33214 28 20 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20  ( nData!=0 ){.  
33215 20 20 2f 2a 20 46 69 72 73 74 20 74 65 72 6d 20    /* First term 
33216 69 73 20 6e 6f 74 20 64 65 6c 74 61 2d 65 6e 63  is not delta-enc
33217 6f 64 65 64 2e 20 2a 2f 0a 20 20 20 20 6e 20 3d  oded. */.    n =
33218 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
33219 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29  (pData, &iDummy)
3321a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e  ;.    assert( n>
3321b 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
3321c 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20   iDummy>0 );.   
3321d 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d   assert( n+iDumm
3321e 79 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74  y>0);.    assert
3321f 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74  ( n+iDummy<=nDat
33220 61 20 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b  a );.    pData +
33221 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20  = n+iDummy;.    
33222 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d  nData -= n+iDumm
33223 79 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f 6c 6c 6f  y;..    /* Follo
33224 77 69 6e 67 20 74 65 72 6d 73 20 64 65 6c 74 61  wing terms delta
33225 2d 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 20  -encoded. */.   
33226 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30   while( nData!=0
33227 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 6e   ){.      /* Len
33228 67 74 68 20 6f 66 20 73 68 61 72 65 64 20 70 72  gth of shared pr
33229 65 66 69 78 2e 20 2a 2f 0a 20 20 20 20 20 20 6e  efix. */.      n
3322a 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
3322b 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
3322c 79 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  y);.      assert
3322d 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  ( n>0 );.      a
3322e 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 3d 30  ssert( iDummy>=0
3322f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
33230 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20  ( n<nData );.   
33231 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20     pData += n;. 
33232 20 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b       nData -= n;
33233 0a 0a 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74  ..      /* Lengt
33234 68 20 61 6e 64 20 64 61 74 61 20 6f 66 20 64 69  h and data of di
33235 73 74 69 6e 63 74 20 73 75 66 66 69 78 2e 20 2a  stinct suffix. *
33236 2f 0a 20 20 20 20 20 20 6e 20 3d 20 66 74 73 33  /.      n = fts3
33237 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74  GetVarint32(pDat
33238 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20  a, &iDummy);.   
33239 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
3323a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
3323b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20  iDummy>0 );.    
3323c 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
3323d 6d 79 3e 30 29 3b 0a 20 20 20 20 20 20 61 73 73  my>0);.      ass
3323e 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
3323f 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 70 44  Data );.      pD
33240 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
33241 0a 20 20 20 20 20 20 6e 44 61 74 61 20 2d 3d 20  .      nData -= 
33242 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 7d 0a  n+iDummy;.    }.
33243 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53    }.}.#define AS
33244 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52  SERT_VALID_INTER
33245 49 4f 52 5f 42 4c 4f 43 4b 28 78 29 20 69 6e 74  IOR_BLOCK(x) int
33246 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69 64 61  eriorBlockValida
33247 74 65 28 78 29 0a 23 65 6c 73 65 0a 23 64 65 66  te(x).#else.#def
33248 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44  ine ASSERT_VALID
33249 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28  _INTERIOR_BLOCK(
3324a 78 29 20 61 73 73 65 72 74 28 20 31 20 29 0a 23  x) assert( 1 ).#
3324b 65 6e 64 69 66 0a 0a 74 79 70 65 64 65 66 20 73  endif..typedef s
3324c 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72 57 72  truct InteriorWr
3324d 69 74 65 72 20 7b 0a 20 20 69 6e 74 20 69 48 65  iter {.  int iHe
3324e 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
3324f 20 20 20 20 20 20 20 20 2f 2a 20 66 72 6f 6d 20          /* from 
33250 30 20 61 74 20 6c 65 61 76 65 73 2e 20 2a 2f 0a  0 at leaves. */.
33251 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
33252 2a 66 69 72 73 74 2c 20 2a 6c 61 73 74 3b 0a 20  *first, *last;. 
33253 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72   struct Interior
33254 57 72 69 74 65 72 20 2a 70 61 72 65 6e 74 57 72  Writer *parentWr
33255 69 74 65 72 3b 0a 0a 20 20 44 61 74 61 42 75 66  iter;..  DataBuf
33256 66 65 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20  fer term;       
33257 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
33258 74 65 72 6d 20 77 72 69 74 74 65 6e 20 74 6f 20  term written to 
33259 62 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a 2f  block "last". */
3325a 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
3325b 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f  iOpeningChildBlo
3325c 63 6b 3b 20 2f 2a 20 46 69 72 73 74 20 63 68 69  ck; /* First chi
3325d 6c 64 20 62 6c 6f 63 6b 20 69 6e 20 62 6c 6f 63  ld block in bloc
3325e 6b 20 22 6c 61 73 74 22 2e 20 2a 2f 0a 23 69 66  k "last". */.#if
3325f 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 71  ndef NDEBUG.  sq
33260 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73 74  lite_int64 iLast
33261 43 68 69 6c 64 42 6c 6f 63 6b 3b 20 20 2f 2a 20  ChildBlock;  /* 
33262 66 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20  for consistency 
33263 63 68 65 63 6b 73 2e 20 2a 2f 0a 23 65 6e 64 69  checks. */.#endi
33264 66 0a 7d 20 49 6e 74 65 72 69 6f 72 57 72 69 74  f.} InteriorWrit
33265 65 72 3b 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69  er;../* Initiali
33266 7a 65 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e  ze an interior n
33267 6f 64 65 20 77 68 65 72 65 20 70 54 65 72 6d 5b  ode where pTerm[
33268 6e 54 65 72 6d 5d 20 6d 61 72 6b 73 20 74 68 65  nTerm] marks the
33269 20 6c 65 66 74 6d 6f 73 74 0a 2a 2a 20 74 65 72   leftmost.** ter
3326a 6d 20 69 6e 20 74 68 65 20 74 72 65 65 2e 20 20  m in the tree.  
3326b 69 43 68 69 6c 64 42 6c 6f 63 6b 20 69 73 20 74  iChildBlock is t
3326c 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 68 69 6c  he leftmost chil
3326d 64 20 62 6c 6f 63 6b 20 61 74 20 74 68 65 0a 2a  d block at the.*
3326e 2a 20 6e 65 78 74 20 6c 65 76 65 6c 20 64 6f 77  * next level dow
3326f 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73  n the tree..*/.s
33270 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72  tatic void inter
33271 69 6f 72 57 72 69 74 65 72 49 6e 69 74 28 69 6e  iorWriterInit(in
33272 74 20 69 48 65 69 67 68 74 2c 20 63 6f 6e 73 74  t iHeight, const
33273 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
33274 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20  t nTerm,.       
33275 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33276 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
33277 6e 74 36 34 20 69 43 68 69 6c 64 42 6c 6f 63 6b  nt64 iChildBlock
33278 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3327a 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20   InteriorWriter 
3327b 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 49 6e 74  *pWriter){.  Int
3327c 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63  eriorBlock *bloc
3327d 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 48 65  k;.  assert( iHe
3327e 69 67 68 74 3e 30 20 29 3b 0a 20 20 43 4c 45 41  ight>0 );.  CLEA
3327f 52 28 70 57 72 69 74 65 72 29 3b 0a 0a 20 20 70  R(pWriter);..  p
33280 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68 74 20  Writer->iHeight 
33281 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 70 57 72  = iHeight;.  pWr
33282 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e 67 43 68  iter->iOpeningCh
33283 69 6c 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c  ildBlock = iChil
33284 64 42 6c 6f 63 6b 3b 0a 23 69 66 6e 64 65 66 20  dBlock;.#ifndef 
33285 4e 44 45 42 55 47 0a 20 20 70 57 72 69 74 65 72  NDEBUG.  pWriter
33286 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63  ->iLastChildBloc
33287 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b 3b  k = iChildBlock;
33288 0a 23 65 6e 64 69 66 0a 20 20 62 6c 6f 63 6b 20  .#endif.  block 
33289 3d 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e  = interiorBlockN
3328a 65 77 28 69 48 65 69 67 68 74 2c 20 69 43 68 69  ew(iHeight, iChi
3328b 6c 64 42 6c 6f 63 6b 2c 20 70 54 65 72 6d 2c 20  ldBlock, pTerm, 
3328c 6e 54 65 72 6d 29 3b 0a 20 20 70 57 72 69 74 65  nTerm);.  pWrite
3328d 72 2d 3e 6c 61 73 74 20 3d 20 70 57 72 69 74 65  r->last = pWrite
3328e 72 2d 3e 66 69 72 73 74 20 3d 20 62 6c 6f 63 6b  r->first = block
3328f 3b 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44  ;.  ASSERT_VALID
33290 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28  _INTERIOR_BLOCK(
33291 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a  pWriter->last);.
33292 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
33293 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c  (&pWriter->term,
33294 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 41 70 70 65 6e   0);.}../* Appen
33295 64 20 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65  d the child node
33296 20 72 6f 6f 74 65 64 20 61 74 20 69 43 68 69 6c   rooted at iChil
33297 64 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e  dBlock to the in
33298 74 65 72 69 6f 72 20 6e 6f 64 65 2c 0a 2a 2a 20  terior node,.** 
33299 77 69 74 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d  with pTerm[nTerm
3329a 5d 20 61 73 20 74 68 65 20 6c 65 66 74 6d 6f 73  ] as the leftmos
3329b 74 20 74 65 72 6d 20 69 6e 20 69 43 68 69 6c 64  t term in iChild
3329c 42 6c 6f 63 6b 27 73 20 73 75 62 74 72 65 65 2e  Block's subtree.
3329d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3329e 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70  interiorWriterAp
3329f 70 65 6e 64 28 49 6e 74 65 72 69 6f 72 57 72 69  pend(InteriorWri
332a0 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
332a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
332a3 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
332a4 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20  , int nTerm,.   
332a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
332a7 6c 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c  lite_int64 iChil
332a8 64 42 6c 6f 63 6b 29 7b 0a 20 20 63 68 61 72 20  dBlock){.  char 
332a9 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52  c[VARINT_MAX+VAR
332aa 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20  INT_MAX];.  int 
332ab 6e 2c 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 0a  n, nPrefix = 0;.
332ac 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  .  ASSERT_VALID_
332ad 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70  INTERIOR_BLOCK(p
332ae 57 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 0a  Writer->last);..
332af 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
332b0 65 72 6d 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  erm written into
332b1 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   an interior nod
332b2 65 20 69 73 20 61 63 74 75 61 6c 6c 79 0a 20 20  e is actually.  
332b3 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ** associated wi
332b4 74 68 20 74 68 65 20 73 65 63 6f 6e 64 20 63 68  th the second ch
332b5 69 6c 64 20 61 64 64 65 64 20 28 74 68 65 20 66  ild added (the f
332b6 69 72 73 74 20 63 68 69 6c 64 20 77 61 73 20 61  irst child was a
332b7 64 64 65 64 0a 20 20 2a 2a 20 69 6e 20 69 6e 74  dded.  ** in int
332b8 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74 2c  eriorWriterInit,
332b9 20 6f 72 20 69 6e 20 74 68 65 20 69 66 20 63 6c   or in the if cl
332ba 61 75 73 65 20 61 74 20 74 68 65 20 62 6f 74 74  ause at the bott
332bb 6f 6d 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20  om of this.  ** 
332bc 66 75 6e 63 74 69 6f 6e 29 2e 20 20 54 68 61 74  function).  That
332bd 20 74 65 72 6d 20 67 65 74 73 20 65 6e 63 6f 64   term gets encod
332be 65 64 20 73 74 72 61 69 67 68 74 20 75 70 2c 20  ed straight up, 
332bf 77 69 74 68 20 6e 50 72 65 66 69 78 20 6c 65 66  with nPrefix lef
332c0 74 0a 20 20 2a 2a 20 61 74 20 30 2e 0a 20 20 2a  t.  ** at 0..  *
332c1 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  /.  if( pWriter-
332c2 3e 74 65 72 6d 2e 6e 44 61 74 61 3d 3d 30 20 29  >term.nData==0 )
332c3 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75  {.    n = fts3Pu
332c4 74 56 61 72 69 6e 74 28 63 2c 20 6e 54 65 72 6d  tVarint(c, nTerm
332c5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
332c6 77 68 69 6c 65 28 20 6e 50 72 65 66 69 78 3c 70  while( nPrefix<p
332c7 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61  Writer->term.nDa
332c8 74 61 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  ta &&.          
332c9 20 70 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 3d   pTerm[nPrefix]=
332ca 3d 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 70  =pWriter->term.p
332cb 44 61 74 61 5b 6e 50 72 65 66 69 78 5d 20 29 7b  Data[nPrefix] ){
332cc 0a 20 20 20 20 20 20 6e 50 72 65 66 69 78 2b 2b  .      nPrefix++
332cd 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 20 3d  ;.    }..    n =
332ce 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
332cf 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20  , nPrefix);.    
332d0 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
332d1 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50  nt(c+n, nTerm-nP
332d2 72 65 66 69 78 29 3b 0a 20 20 7d 0a 0a 23 69 66  refix);.  }..#if
332d3 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57  ndef NDEBUG.  pW
332d4 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c  riter->iLastChil
332d5 64 42 6c 6f 63 6b 2b 2b 3b 0a 23 65 6e 64 69 66  dBlock++;.#endif
332d6 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
332d7 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42 6c  er->iLastChildBl
332d8 6f 63 6b 3d 3d 69 43 68 69 6c 64 42 6c 6f 63 6b  ock==iChildBlock
332d9 20 29 3b 0a 0a 20 20 2f 2a 20 4f 76 65 72 66 6c   );..  /* Overfl
332da 6f 77 20 74 6f 20 61 20 6e 65 77 20 62 6c 6f 63  ow to a new bloc
332db 6b 20 69 66 20 74 68 65 20 6e 65 77 20 74 65 72  k if the new ter
332dc 6d 20 6d 61 6b 65 73 20 74 68 65 20 63 75 72 72  m makes the curr
332dd 65 6e 74 20 62 6c 6f 63 6b 0a 20 20 2a 2a 20 74  ent block.  ** t
332de 6f 6f 20 62 69 67 2c 20 61 6e 64 20 74 68 65 20  oo big, and the 
332df 63 75 72 72 65 6e 74 20 62 6c 6f 63 6b 20 61 6c  current block al
332e0 72 65 61 64 79 20 68 61 73 20 65 6e 6f 75 67 68  ready has enough
332e1 20 74 65 72 6d 73 2e 0a 20 20 2a 2f 0a 20 20 69   terms..  */.  i
332e2 66 28 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74  f( pWriter->last
332e3 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2b 6e 2b 6e  ->data.nData+n+n
332e4 54 65 72 6d 2d 6e 50 72 65 66 69 78 3e 49 4e 54  Term-nPrefix>INT
332e5 45 52 49 4f 52 5f 4d 41 58 20 26 26 0a 20 20 20  ERIOR_MAX &&.   
332e6 20 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2d 70     iChildBlock-p
332e7 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e 67  Writer->iOpening
332e8 43 68 69 6c 64 42 6c 6f 63 6b 3e 49 4e 54 45 52  ChildBlock>INTER
332e9 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 29 7b  IOR_MIN_TERMS ){
332ea 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 6c 61  .    pWriter->la
332eb 73 74 2d 3e 6e 65 78 74 20 3d 20 69 6e 74 65 72  st->next = inter
332ec 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 70 57 72 69  iorBlockNew(pWri
332ed 74 65 72 2d 3e 69 48 65 69 67 68 74 2c 20 69 43  ter->iHeight, iC
332ee 68 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20  hildBlock,.     
332ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332f1 20 20 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65        pTerm, nTe
332f2 72 6d 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72  rm);.    pWriter
332f3 2d 3e 6c 61 73 74 20 3d 20 70 57 72 69 74 65 72  ->last = pWriter
332f4 2d 3e 6c 61 73 74 2d 3e 6e 65 78 74 3b 0a 20 20  ->last->next;.  
332f5 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e    pWriter->iOpen
332f6 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20  ingChildBlock = 
332f7 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a 20 20 20  iChildBlock;.   
332f8 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
332f9 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29  (&pWriter->term)
332fa 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
332fb 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32  ataBufferAppend2
332fc 28 26 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 2d  (&pWriter->last-
332fd 3e 64 61 74 61 2c 20 63 2c 20 6e 2c 0a 20 20 20  >data, c, n,.   
332fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332ff 20 20 20 70 54 65 72 6d 2b 6e 50 72 65 66 69 78     pTerm+nPrefix
33300 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 29  , nTerm-nPrefix)
33301 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
33302 52 65 70 6c 61 63 65 28 26 70 57 72 69 74 65 72  Replace(&pWriter
33303 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e  ->term, pTerm, n
33304 54 65 72 6d 29 3b 0a 20 20 7d 0a 20 20 41 53 53  Term);.  }.  ASS
33305 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
33306 4f 52 5f 42 4c 4f 43 4b 28 70 57 72 69 74 65 72  OR_BLOCK(pWriter
33307 2d 3e 6c 61 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 46  ->last);.}../* F
33308 72 65 65 20 74 68 65 20 73 70 61 63 65 20 75 73  ree the space us
33309 65 64 20 62 79 20 70 57 72 69 74 65 72 2c 20 69  ed by pWriter, i
3330a 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6c 69 6e  ncluding the lin
3330b 6b 65 64 2d 6c 69 73 74 20 6f 66 0a 2a 2a 20 49  ked-list of.** I
3330c 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 73 2c 20 61  nteriorBlocks, a
3330d 6e 64 20 70 61 72 65 6e 74 57 72 69 74 65 72 2c  nd parentWriter,
3330e 20 69 66 20 70 72 65 73 65 6e 74 2e 0a 2a 2f 0a   if present..*/.
3330f 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
33310 69 6f 72 57 72 69 74 65 72 44 65 73 74 72 6f 79  iorWriterDestroy
33311 28 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20  (InteriorWriter 
33312 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 49 6e 74  *pWriter){.  Int
33313 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63  eriorBlock *bloc
33314 6b 20 3d 20 70 57 72 69 74 65 72 2d 3e 66 69 72  k = pWriter->fir
33315 73 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20 62 6c  st;..  while( bl
33316 6f 63 6b 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  ock!=NULL ){.   
33317 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a   InteriorBlock *
33318 62 20 3d 20 62 6c 6f 63 6b 3b 0a 20 20 20 20 62  b = block;.    b
33319 6c 6f 63 6b 20 3d 20 62 6c 6f 63 6b 2d 3e 6e 65  lock = block->ne
3331a 78 74 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  xt;.    dataBuff
3331b 65 72 44 65 73 74 72 6f 79 28 26 62 2d 3e 74 65  erDestroy(&b->te
3331c 72 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66  rm);.    dataBuf
3331d 66 65 72 44 65 73 74 72 6f 79 28 26 62 2d 3e 64  ferDestroy(&b->d
3331e 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ata);.    sqlite
3331f 33 5f 66 72 65 65 28 62 29 3b 0a 20 20 7d 0a 20  3_free(b);.  }. 
33320 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 70 61   if( pWriter->pa
33321 72 65 6e 74 57 72 69 74 65 72 21 3d 4e 55 4c 4c  rentWriter!=NULL
33322 20 29 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72   ){.    interior
33323 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 70 57  WriterDestroy(pW
33324 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
33325 74 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ter);.    sqlite
33326 33 5f 66 72 65 65 28 70 57 72 69 74 65 72 2d 3e  3_free(pWriter->
33327 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 20  parentWriter);. 
33328 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44   }.  dataBufferD
33329 65 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d  estroy(&pWriter-
3332a 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d 42  >term);.  SCRAMB
3332b 4c 45 28 70 57 72 69 74 65 72 29 3b 0a 20 20 72  LE(pWriter);.  r
3332c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3332d 0a 7d 0a 0a 2f 2a 20 49 66 20 70 57 72 69 74 65  .}../* If pWrite
3332e 72 20 63 61 6e 20 66 69 74 20 65 6e 74 69 72 65  r can fit entire
3332f 6c 79 20 69 6e 20 52 4f 4f 54 5f 4d 41 58 2c 20  ly in ROOT_MAX, 
33330 72 65 74 75 72 6e 20 69 74 20 61 73 20 74 68 65  return it as the
33331 20 72 6f 6f 74 20 69 6e 66 6f 0a 2a 2a 20 64 69   root info.** di
33332 72 65 63 74 6c 79 2c 20 6c 65 61 76 69 6e 67 20  rectly, leaving 
33333 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 75 6e  *piEndBlockid un
33334 63 68 61 6e 67 65 64 2e 20 20 4f 74 68 65 72 77  changed.  Otherw
33335 69 73 65 2c 20 66 6c 75 73 68 0a 2a 2a 20 70 57  ise, flush.** pW
33336 72 69 74 65 72 20 74 6f 20 25 5f 73 65 67 6d 65  riter to %_segme
33337 6e 74 73 2c 20 62 75 69 6c 64 69 6e 67 20 61 20  nts, building a 
33338 6e 65 77 20 6c 61 79 65 72 20 6f 66 20 69 6e 74  new layer of int
33339 65 72 69 6f 72 20 6e 6f 64 65 73 2c 20 61 6e 64  erior nodes, and
3333a 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 6c 79 20  .** recursively 
3333b 61 73 6b 20 66 6f 72 20 74 68 65 69 72 20 72 6f  ask for their ro
3333c 6f 74 20 69 6e 74 6f 2e 0a 2a 2f 0a 73 74 61 74  ot into..*/.stat
3333d 69 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 57  ic int interiorW
3333e 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 66 75  riterRootInfo(fu
3333f 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
33340 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a  InteriorWriter *
33341 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
33342 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33343 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
33344 2a 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e  **ppRootInfo, in
33345 74 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20  t *pnRootInfo,. 
33346 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33347 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33348 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
33349 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20  iEndBlockid){.  
3334a 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
3334b 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72 2d 3e  lock = pWriter->
3334c 66 69 72 73 74 3b 0a 20 20 73 71 6c 69 74 65 5f  first;.  sqlite_
3334d 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 20 3d  int64 iBlockid =
3334e 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20   0;.  int rc;.. 
3334f 20 2f 2a 20 49 66 20 77 65 20 63 61 6e 20 66 69   /* If we can fi
33350 74 20 74 68 65 20 73 65 67 6d 65 6e 74 20 69 6e  t the segment in
33351 6c 69 6e 65 20 2a 2f 0a 20 20 69 66 28 20 62 6c  line */.  if( bl
33352 6f 63 6b 3d 3d 70 57 72 69 74 65 72 2d 3e 6c 61  ock==pWriter->la
33353 73 74 20 26 26 20 62 6c 6f 63 6b 2d 3e 64 61 74  st && block->dat
33354 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d 41 58  a.nData<ROOT_MAX
33355 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f 74 49   ){.    *ppRootI
33356 6e 66 6f 20 3d 20 62 6c 6f 63 6b 2d 3e 64 61 74  nfo = block->dat
33357 61 2e 70 44 61 74 61 3b 0a 20 20 20 20 2a 70 6e  a.pData;.    *pn
33358 52 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b  RootInfo = block
33359 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20 20  ->data.nData;.  
3335a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3335b 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6c  OK;.  }..  /* Fl
3335c 75 73 68 20 74 68 65 20 66 69 72 73 74 20 62 6c  ush the first bl
3335d 6f 63 6b 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74  ock to %_segment
3335e 73 2c 20 61 6e 64 20 63 72 65 61 74 65 20 61 20  s, and create a 
3335f 6e 65 77 20 6c 65 76 65 6c 20 6f 66 0a 20 20 2a  new level of.  *
33360 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e  * interior node.
33361 0a 20 20 2a 2f 0a 20 20 41 53 53 45 52 54 5f 56  .  */.  ASSERT_V
33362 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c  ALID_INTERIOR_BL
33363 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20 20 72 63  OCK(block);.  rc
33364 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28   = block_insert(
33365 76 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70  v, block->data.p
33366 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74  Data, block->dat
33367 61 2e 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b  a.nData, &iBlock
33368 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
33369 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3336a 6e 20 72 63 3b 0a 20 20 2a 70 69 45 6e 64 42 6c  n rc;.  *piEndBl
3336b 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64  ockid = iBlockid
3336c 3b 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e 70 61  ;..  pWriter->pa
3336d 72 65 6e 74 57 72 69 74 65 72 20 3d 20 73 71 6c  rentWriter = sql
3336e 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
3336f 6f 66 28 2a 70 57 72 69 74 65 72 2d 3e 70 61 72  of(*pWriter->par
33370 65 6e 74 57 72 69 74 65 72 29 29 3b 0a 20 20 69  entWriter));.  i
33371 6e 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69  nteriorWriterIni
33372 74 28 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67  t(pWriter->iHeig
33373 68 74 2b 31 2c 0a 20 20 20 20 20 20 20 20 20 20  ht+1,.          
33374 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f 63 6b             block
33375 2d 3e 74 65 72 6d 2e 70 44 61 74 61 2c 20 62 6c  ->term.pData, bl
33376 6f 63 6b 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 2c  ock->term.nData,
33377 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33378 20 20 20 20 20 20 69 42 6c 6f 63 6b 69 64 2c 20        iBlockid, 
33379 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57  pWriter->parentW
3337a 72 69 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 46 6c  riter);..  /* Fl
3337b 75 73 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 62  ush additional b
3337c 6c 6f 63 6b 73 20 61 6e 64 20 61 70 70 65 6e 64  locks and append
3337d 20 74 6f 20 74 68 65 20 68 69 67 68 65 72 20 69   to the higher i
3337e 6e 74 65 72 69 6f 72 0a 20 20 2a 2a 20 6e 6f 64  nterior.  ** nod
3337f 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 62 6c  e..  */.  for(bl
33380 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65 78 74 3b  ock=block->next;
33381 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 3b 20 62 6c   block!=NULL; bl
33382 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65 78 74 29  ock=block->next)
33383 7b 0a 20 20 20 20 41 53 53 45 52 54 5f 56 41 4c  {.    ASSERT_VAL
33384 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43  ID_INTERIOR_BLOC
33385 4b 28 62 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63  K(block);.    rc
33386 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28   = block_insert(
33387 76 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70  v, block->data.p
33388 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74  Data, block->dat
33389 61 2e 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b  a.nData, &iBlock
3338a 69 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  id);.    if( rc!
3338b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3338c 75 72 6e 20 72 63 3b 0a 20 20 20 20 2a 70 69 45  urn rc;.    *piE
3338d 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c 6f  ndBlockid = iBlo
3338e 63 6b 69 64 3b 0a 0a 20 20 20 20 69 6e 74 65 72  ckid;..    inter
3338f 69 6f 72 57 72 69 74 65 72 41 70 70 65 6e 64 28  iorWriterAppend(
33390 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57  pWriter->parentW
33391 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
33392 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33393 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74  block->term.pDat
33394 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e  a, block->term.n
33395 44 61 74 61 2c 20 69 42 6c 6f 63 6b 69 64 29 3b  Data, iBlockid);
33396 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 61 72 65 6e  .  }..  /* Paren
33397 74 20 6e 6f 64 65 20 67 65 74 73 20 74 68 65 20  t node gets the 
33398 63 68 61 6e 63 65 20 74 6f 20 62 65 20 74 68 65  chance to be the
33399 20 72 6f 6f 74 2e 20 2a 2f 0a 20 20 72 65 74 75   root. */.  retu
3339a 72 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65  rn interiorWrite
3339b 72 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72  rRootInfo(v, pWr
3339c 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74  iter->parentWrit
3339d 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
3339e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3339f 20 20 20 20 70 70 52 6f 6f 74 49 6e 66 6f 2c 20      ppRootInfo, 
333a0 70 6e 52 6f 6f 74 49 6e 66 6f 2c 20 70 69 45 6e  pnRootInfo, piEn
333a1 64 42 6c 6f 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a  dBlockid);.}../*
333a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
333a6 0a 2f 2a 20 49 6e 74 65 72 69 6f 72 52 65 61 64  ./* InteriorRead
333a7 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  er is used to re
333a8 61 64 20 6f 66 66 20 74 68 65 20 64 61 74 61 20  ad off the data 
333a9 66 72 6f 6d 20 61 6e 20 69 6e 74 65 72 69 6f 72  from an interior
333aa 20 6e 6f 64 65 0a 2a 2a 20 28 73 65 65 20 63 6f   node.** (see co
333ab 6d 6d 65 6e 74 20 61 74 20 74 6f 70 20 6f 66 20  mment at top of 
333ac 66 69 6c 65 20 66 6f 72 20 74 68 65 20 66 6f 72  file for the for
333ad 6d 61 74 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  mat)..*/.typedef
333ae 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72   struct Interior
333af 52 65 61 64 65 72 20 7b 0a 20 20 63 6f 6e 73 74  Reader {.  const
333b0 20 63 68 61 72 20 2a 70 44 61 74 61 3b 0a 20 20   char *pData;.  
333b1 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20 20 44 61  int nData;..  Da
333b2 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20  taBuffer term;  
333b3 20 20 20 20 20 20 20 20 2f 2a 20 70 72 65 76 69          /* previ
333b4 6f 75 73 20 74 65 72 6d 2c 20 66 6f 72 20 64 65  ous term, for de
333b5 63 6f 64 69 6e 67 20 74 65 72 6d 20 64 65 6c 74  coding term delt
333b6 61 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 5f  a. */..  sqlite_
333b7 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 3b 0a  int64 iBlockid;.
333b8 7d 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72  } InteriorReader
333b9 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  ;..static void i
333ba 6e 74 65 72 69 6f 72 52 65 61 64 65 72 44 65 73  nteriorReaderDes
333bb 74 72 6f 79 28 49 6e 74 65 72 69 6f 72 52 65 61  troy(InteriorRea
333bc 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
333bd 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
333be 6f 79 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72  oy(&pReader->ter
333bf 6d 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70  m);.  SCRAMBLE(p
333c0 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 54  Reader);.}../* T
333c1 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 61  ODO(shess) The a
333c2 73 73 65 72 74 69 6f 6e 73 20 61 72 65 20 67 72  ssertions are gr
333c3 65 61 74 2c 20 62 75 74 20 77 68 61 74 20 69 66  eat, but what if
333c4 20 77 65 27 72 65 20 69 6e 20 4e 44 45 42 55 47   we're in NDEBUG
333c5 0a 2a 2a 20 61 6e 64 20 74 68 65 20 62 6c 6f 62  .** and the blob
333c6 20 69 73 20 65 6d 70 74 79 20 6f 72 20 6f 74 68   is empty or oth
333c7 65 72 77 69 73 65 20 63 6f 6e 74 61 69 6e 73 20  erwise contains 
333c8 73 75 73 70 65 63 74 20 64 61 74 61 3f 0a 2a 2f  suspect data?.*/
333c9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74  .static void int
333ca 65 72 69 6f 72 52 65 61 64 65 72 49 6e 69 74 28  eriorReaderInit(
333cb 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
333cc 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
333cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
333ce 20 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74               Int
333cf 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65  eriorReader *pRe
333d0 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c 20  ader){.  int n, 
333d1 6e 54 65 72 6d 3b 0a 0a 20 20 2f 2a 20 52 65 71  nTerm;..  /* Req
333d2 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 74 68  uire at least th
333d3 65 20 6c 65 61 64 69 6e 67 20 66 6c 61 67 20 62  e leading flag b
333d4 79 74 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  yte */.  assert(
333d5 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73   nData>0 );.  as
333d6 73 65 72 74 28 20 70 44 61 74 61 5b 30 5d 21 3d  sert( pData[0]!=
333d7 27 5c 30 27 20 29 3b 0a 0a 20 20 43 4c 45 41 52  '\0' );..  CLEAR
333d8 28 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (pReader);..  /*
333d9 20 44 65 63 6f 64 65 20 74 68 65 20 62 61 73 65   Decode the base
333da 20 62 6c 6f 63 6b 69 64 2c 20 61 6e 64 20 73 65   blockid, and se
333db 74 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  t the cursor to 
333dc 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 2e 20  the first term. 
333dd 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
333de 56 61 72 69 6e 74 28 70 44 61 74 61 2b 31 2c 20  Varint(pData+1, 
333df 26 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b  &pReader->iBlock
333e0 69 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 31  id);.  assert( 1
333e1 2b 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 70  +n<=nData );.  p
333e2 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20  Reader->pData = 
333e3 70 44 61 74 61 2b 31 2b 6e 3b 0a 20 20 70 52 65  pData+1+n;.  pRe
333e4 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44  ader->nData = nD
333e5 61 74 61 2d 28 31 2b 6e 29 3b 0a 0a 20 20 2f 2a  ata-(1+n);..  /*
333e6 20 41 20 73 69 6e 67 6c 65 2d 63 68 69 6c 64 20   A single-child 
333e7 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 28 73  interior node (s
333e8 75 63 68 20 61 73 20 77 68 65 6e 20 61 20 6c 65  uch as when a le
333e9 61 66 20 6e 6f 64 65 20 77 61 73 20 74 6f 6f 0a  af node was too.
333ea 20 20 2a 2a 20 6c 61 72 67 65 20 66 6f 72 20 74    ** large for t
333eb 68 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63  he segment direc
333ec 74 6f 72 79 29 20 77 6f 6e 27 74 20 68 61 76 65  tory) won't have
333ed 20 61 6e 79 20 74 65 72 6d 73 2e 0a 20 20 2a 2a   any terms..  **
333ee 20 4f 74 68 65 72 77 69 73 65 2c 20 64 65 63 6f   Otherwise, deco
333ef 64 65 20 74 68 65 20 66 69 72 73 74 20 74 65 72  de the first ter
333f0 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52  m..  */.  if( pR
333f1 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20  eader->nData==0 
333f2 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  ){.    dataBuffe
333f3 72 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e  rInit(&pReader->
333f4 74 65 72 6d 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  term, 0);.  }els
333f5 65 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47  e{.    n = fts3G
333f6 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
333f7 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 54 65 72  er->pData, &nTer
333f8 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  m);.    dataBuff
333f9 65 72 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d  erInit(&pReader-
333fa 3e 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20  >term, nTerm);. 
333fb 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
333fc 6c 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 74  lace(&pReader->t
333fd 65 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70 44  erm, pReader->pD
333fe 61 74 61 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20  ata+n, nTerm);. 
333ff 20 20 20 61 73 73 65 72 74 28 20 6e 2b 6e 54 65     assert( n+nTe
33400 72 6d 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61  rm<=pReader->nDa
33401 74 61 20 29 3b 0a 20 20 20 20 70 52 65 61 64 65  ta );.    pReade
33402 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 54  r->pData += n+nT
33403 65 72 6d 3b 0a 20 20 20 20 70 52 65 61 64 65 72  erm;.    pReader
33404 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 54 65  ->nData -= n+nTe
33405 72 6d 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69  rm;.  }.}..stati
33406 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65  c int interiorRe
33407 61 64 65 72 41 74 45 6e 64 28 49 6e 74 65 72 69  aderAtEnd(Interi
33408 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
33409 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  r){.  return pRe
3340a 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
3340b 3d 3d 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73  ==0;.}..static s
3340c 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 6e 74 65  qlite_int64 inte
3340d 72 69 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e  riorReaderCurren
3340e 74 42 6c 6f 63 6b 69 64 28 49 6e 74 65 72 69 6f  tBlockid(Interio
3340f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  rReader *pReader
33410 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  ){.  return pRea
33411 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 3b 0a 7d  der->iBlockid;.}
33412 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74  ..static int int
33413 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d 42  eriorReaderTermB
33414 79 74 65 73 28 49 6e 74 65 72 69 6f 72 52 65 61  ytes(InteriorRea
33415 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
33416 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69   assert( !interi
33417 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52  orReaderAtEnd(pR
33418 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
33419 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  rn pReader->term
3341a 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63  .nData;.}.static
3341b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 74   const char *int
3341c 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d 28  eriorReaderTerm(
3341d 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a  InteriorReader *
3341e 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
3341f 72 74 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61  rt( !interiorRea
33420 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  derAtEnd(pReader
33421 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  ) );.  return pR
33422 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74  eader->term.pDat
33423 61 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 66 6f  a;.}../* Step fo
33424 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65 78  rward to the nex
33425 74 20 74 65 72 6d 20 69 6e 20 74 68 65 20 6e 6f  t term in the no
33426 64 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  de. */.static vo
33427 69 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65  id interiorReade
33428 72 53 74 65 70 28 49 6e 74 65 72 69 6f 72 52 65  rStep(InteriorRe
33429 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
3342a 20 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72    assert( !inter
3342b 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70  iorReaderAtEnd(p
3342c 52 65 61 64 65 72 29 20 29 3b 0a 0a 20 20 2f 2a  Reader) );..  /*
3342d 20 49 66 20 74 68 65 20 6c 61 73 74 20 74 65 72   If the last ter
3342e 6d 20 68 61 73 20 62 65 65 6e 20 72 65 61 64 2c  m has been read,
3342f 20 73 69 67 6e 61 6c 20 65 6f 66 2c 20 65 6c 73   signal eof, els
33430 65 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 0a  e construct the.
33431 20 20 2a 2a 20 6e 65 78 74 20 74 65 72 6d 2e 0a    ** next term..
33432 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61 64    */.  if( pRead
33433 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a  er->nData==0 ){.
33434 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
33435 73 65 74 28 26 70 52 65 61 64 65 72 2d 3e 74 65  set(&pReader->te
33436 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  rm);.  }else{.  
33437 20 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78    int n, nPrefix
33438 2c 20 6e 53 75 66 66 69 78 3b 0a 0a 20 20 20 20  , nSuffix;..    
33439 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
3343a 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
3343b 74 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20  ta, &nPrefix);. 
3343c 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56     n += fts3GetV
3343d 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d  arint32(pReader-
3343e 3e 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66 66  >pData+n, &nSuff
3343f 69 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 72 75  ix);..    /* Tru
33440 6e 63 61 74 65 20 74 68 65 20 63 75 72 72 65 6e  ncate the curren
33441 74 20 74 65 72 6d 20 61 6e 64 20 61 70 70 65 6e  t term and appen
33442 64 20 73 75 66 66 69 78 20 64 61 74 61 2e 20 2a  d suffix data. *
33443 2f 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 74  /.    pReader->t
33444 65 72 6d 2e 6e 44 61 74 61 20 3d 20 6e 50 72 65  erm.nData = nPre
33445 66 69 78 3b 0a 20 20 20 20 64 61 74 61 42 75 66  fix;.    dataBuf
33446 66 65 72 41 70 70 65 6e 64 28 26 70 52 65 61 64  ferAppend(&pRead
33447 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65  er->term, pReade
33448 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 53 75 66  r->pData+n, nSuf
33449 66 69 78 29 3b 0a 0a 20 20 20 20 61 73 73 65 72  fix);..    asser
3344a 74 28 20 6e 2b 6e 53 75 66 66 69 78 3c 3d 70 52  t( n+nSuffix<=pR
3344b 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
3344c 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61      pReader->pDa
3344d 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66 69 78 3b  ta += n+nSuffix;
3344e 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 44  .    pReader->nD
3344f 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66 66 69 78  ata -= n+nSuffix
33450 3b 0a 20 20 7d 0a 20 20 70 52 65 61 64 65 72 2d  ;.  }.  pReader-
33451 3e 69 42 6c 6f 63 6b 69 64 2b 2b 3b 0a 7d 0a 0a  >iBlockid++;.}..
33452 2f 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 63  /* Compare the c
33453 75 72 72 65 6e 74 20 74 65 72 6d 20 74 6f 20 70  urrent term to p
33454 54 65 72 6d 5b 6e 54 65 72 6d 5d 2c 20 72 65 74  Term[nTerm], ret
33455 75 72 6e 69 6e 67 20 73 74 72 63 6d 70 2d 73 74  urning strcmp-st
33456 79 6c 65 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20  yle.** results. 
33457 20 49 66 20 69 73 50 72 65 66 69 78 2c 20 65 71   If isPrefix, eq
33458 75 61 6c 69 74 79 20 6d 65 61 6e 73 20 65 71 75  uality means equ
33459 61 6c 20 74 68 72 6f 75 67 68 20 6e 54 65 72 6d  al through nTerm
3345a 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
3345b 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65  c int interiorRe
3345c 61 64 65 72 54 65 72 6d 43 6d 70 28 49 6e 74 65  aderTermCmp(Inte
3345d 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61  riorReader *pRea
3345e 64 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  der,.           
3345f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33460 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
33461 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
33462 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78  rm, int isPrefix
33463 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
33464 2a 70 52 65 61 64 65 72 54 65 72 6d 20 3d 20 69  *pReaderTerm = i
33465 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72  nteriorReaderTer
33466 6d 28 70 52 65 61 64 65 72 29 3b 0a 20 20 69 6e  m(pReader);.  in
33467 74 20 6e 52 65 61 64 65 72 54 65 72 6d 20 3d 20  t nReaderTerm = 
33468 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65  interiorReaderTe
33469 72 6d 42 79 74 65 73 28 70 52 65 61 64 65 72 29  rmBytes(pReader)
3346a 3b 0a 20 20 69 6e 74 20 63 2c 20 6e 20 3d 20 6e  ;.  int c, n = n
3346b 52 65 61 64 65 72 54 65 72 6d 3c 6e 54 65 72 6d  ReaderTerm<nTerm
3346c 20 3f 20 6e 52 65 61 64 65 72 54 65 72 6d 20 3a   ? nReaderTerm :
3346d 20 6e 54 65 72 6d 3b 0a 0a 20 20 69 66 28 20 6e   nTerm;..  if( n
3346e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  ==0 ){.    if( n
3346f 52 65 61 64 65 72 54 65 72 6d 3e 30 20 29 20 72  ReaderTerm>0 ) r
33470 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 69 66  eturn -1;.    if
33471 28 20 6e 54 65 72 6d 3e 30 20 29 20 72 65 74 75  ( nTerm>0 ) retu
33472 72 6e 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e  rn 1;.    return
33473 20 30 3b 0a 20 20 7d 0a 0a 20 20 63 20 3d 20 6d   0;.  }..  c = m
33474 65 6d 63 6d 70 28 70 52 65 61 64 65 72 54 65 72  emcmp(pReaderTer
33475 6d 2c 20 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20  m, pTerm, n);.  
33476 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72  if( c!=0 ) retur
33477 6e 20 63 3b 0a 20 20 69 66 28 20 69 73 50 72 65  n c;.  if( isPre
33478 66 69 78 20 26 26 20 6e 3d 3d 6e 54 65 72 6d 20  fix && n==nTerm 
33479 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65  ) return 0;.  re
3347a 74 75 72 6e 20 6e 52 65 61 64 65 72 54 65 72 6d  turn nReaderTerm
3347b 20 2d 20 6e 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 2a   - nTerm;.}../**
3347c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3347d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3347e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3347f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
33480 2f 2a 20 4c 65 61 66 57 72 69 74 65 72 20 69 73  /* LeafWriter is
33481 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74   used to collect
33482 20 74 65 72 6d 73 20 61 6e 64 20 61 73 73 6f 63   terms and assoc
33483 69 61 74 65 64 20 64 6f 63 6c 69 73 74 20 64 61  iated doclist da
33484 74 61 0a 2a 2a 20 69 6e 74 6f 20 6c 65 61 66 20  ta.** into leaf 
33485 62 6c 6f 63 6b 73 20 69 6e 20 25 5f 73 65 67 6d  blocks in %_segm
33486 65 6e 74 73 20 28 73 65 65 20 74 6f 70 20 6f 66  ents (see top of
33487 20 66 69 6c 65 20 66 6f 72 20 66 6f 72 6d 61 74   file for format
33488 20 69 6e 66 6f 29 2e 0a 2a 2a 20 45 78 70 65 63   info)..** Expec
33489 74 65 64 20 75 73 61 67 65 20 69 73 3a 0a 2a 2a  ted usage is:.**
3348a 0a 2a 2a 20 4c 65 61 66 57 72 69 74 65 72 20 77  .** LeafWriter w
3348b 72 69 74 65 72 3b 0a 2a 2a 20 6c 65 61 66 57 72  riter;.** leafWr
3348c 69 74 65 72 49 6e 69 74 28 30 2c 20 30 2c 20 26  iterInit(0, 0, &
3348d 77 72 69 74 65 72 29 3b 0a 2a 2a 20 77 68 69 6c  writer);.** whil
3348e 65 28 20 73 6f 72 74 65 64 5f 74 65 72 6d 73 5f  e( sorted_terms_
3348f 6c 65 66 74 5f 74 6f 5f 70 72 6f 63 65 73 73 20  left_to_process 
33490 29 7b 0a 2a 2a 20 20 20 2f 2f 20 64 61 74 61 20  ){.**   // data 
33491 69 73 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  is doclist data 
33492 66 6f 72 20 74 68 61 74 20 74 65 72 6d 2e 0a 2a  for that term..*
33493 2a 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69  *   rc = leafWri
33494 74 65 72 53 74 65 70 28 76 2c 20 26 77 72 69 74  terStep(v, &writ
33495 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  er, pTerm, nTerm
33496 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b  , pData, nData);
33497 0a 2a 2a 20 20 20 69 66 28 20 72 63 21 3d 53 51  .**   if( rc!=SQ
33498 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
33499 72 72 3b 0a 2a 2a 20 7d 0a 2a 2a 20 72 63 20 3d  rr;.** }.** rc =
3349a 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c   leafWriterFinal
3349b 69 7a 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b  ize(v, &writer);
3349c 0a 2a 2a 65 72 72 3a 0a 2a 2a 20 6c 65 61 66 57  .**err:.** leafW
3349d 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72  riterDestroy(&wr
3349e 69 74 65 72 29 3b 0a 2a 2a 20 72 65 74 75 72 6e  iter);.** return
3349f 20 72 63 3b 0a 2a 2a 0a 2a 2a 20 6c 65 61 66 57   rc;.**.** leafW
334a0 72 69 74 65 72 53 74 65 70 28 29 20 6d 61 79 20  riterStep() may 
334a1 77 72 69 74 65 20 61 20 63 6f 6c 6c 65 63 74 65  write a collecte
334a2 64 20 6c 65 61 66 20 6f 75 74 20 74 6f 20 25 5f  d leaf out to %_
334a3 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 20 6c 65 61  segments..** lea
334a4 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28  fWriterFinalize(
334a5 29 20 66 69 6e 69 73 68 65 73 20 77 72 69 74 69  ) finishes writi
334a6 6e 67 20 61 6e 79 20 62 75 66 66 65 72 65 64 20  ng any buffered 
334a7 64 61 74 61 20 61 6e 64 20 73 74 6f 72 65 73 0a  data and stores.
334a8 2a 2a 20 61 20 72 6f 6f 74 20 6e 6f 64 65 20 69  ** a root node i
334a9 6e 20 25 5f 73 65 67 64 69 72 2e 20 20 6c 65 61  n %_segdir.  lea
334aa 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 29  fWriterDestroy()
334ab 20 66 72 65 65 73 20 61 6c 6c 20 62 75 66 66 65   frees all buffe
334ac 72 73 20 61 6e 64 0a 2a 2a 20 49 6e 74 65 72 69  rs and.** Interi
334ad 6f 72 57 72 69 74 65 72 73 20 61 6c 6c 6f 63 61  orWriters alloca
334ae 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 77  ted as part of w
334af 72 69 74 69 6e 67 20 74 68 69 73 20 73 65 67 6d  riting this segm
334b0 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28  ent..**.** TODO(
334b1 73 68 65 73 73 29 20 44 6f 63 75 6d 65 6e 74 20  shess) Document 
334b2 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65  leafWriterStepMe
334b3 72 67 65 28 29 2e 0a 2a 2f 0a 0a 2f 2a 20 50 75  rge()..*/../* Pu
334b4 74 20 74 65 72 6d 73 20 77 69 74 68 20 64 61 74  t terms with dat
334b5 61 20 74 68 69 73 20 62 69 67 20 69 6e 20 74 68  a this big in th
334b6 65 69 72 20 6f 77 6e 20 62 6c 6f 63 6b 2e 20 2a  eir own block. *
334b7 2f 0a 23 64 65 66 69 6e 65 20 53 54 41 4e 44 41  /.#define STANDA
334b8 4c 4f 4e 45 5f 4d 49 4e 20 31 30 32 34 0a 0a 2f  LONE_MIN 1024../
334b9 2a 20 4b 65 65 70 20 6c 65 61 66 20 62 6c 6f 63  * Keep leaf bloc
334ba 6b 73 20 62 65 6c 6f 77 20 74 68 69 73 20 73 69  ks below this si
334bb 7a 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4c  ze. */.#define L
334bc 45 41 46 5f 4d 41 58 20 32 30 34 38 0a 0a 74 79  EAF_MAX 2048..ty
334bd 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 65 61  pedef struct Lea
334be 66 57 72 69 74 65 72 20 7b 0a 20 20 69 6e 74 20  fWriter {.  int 
334bf 69 4c 65 76 65 6c 3b 0a 20 20 69 6e 74 20 69 64  iLevel;.  int id
334c0 78 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  x;.  sqlite_int6
334c1 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3b  4 iStartBlockid;
334c2 20 20 20 20 20 2f 2a 20 6e 65 65 64 65 64 20 74       /* needed t
334c3 6f 20 63 72 65 61 74 65 20 74 68 65 20 72 6f 6f  o create the roo
334c4 74 20 69 6e 66 6f 20 2a 2f 0a 20 20 73 71 6c 69  t info */.  sqli
334c5 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  te_int64 iEndBlo
334c6 63 6b 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 77  ckid;       /* w
334c7 68 65 6e 20 77 65 27 72 65 20 64 6f 6e 65 20 77  hen we're done w
334c8 72 69 74 69 6e 67 2e 20 2a 2f 0a 0a 20 20 44 61  riting. */..  Da
334c9 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20  taBuffer term;  
334ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
334cb 20 70 72 65 76 69 6f 75 73 20 65 6e 63 6f 64 65   previous encode
334cc 64 20 74 65 72 6d 20 2a 2f 0a 20 20 44 61 74 61  d term */.  Data
334cd 42 75 66 66 65 72 20 64 61 74 61 3b 20 20 20 20  Buffer data;    
334ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65              /* e
334cf 6e 63 6f 64 69 6e 67 20 62 75 66 66 65 72 20 2a  ncoding buffer *
334d0 2f 0a 0a 20 20 2f 2a 20 62 79 74 65 73 20 6f 66  /..  /* bytes of
334d1 20 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20 74   first term in t
334d2 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20  he current node 
334d3 77 68 69 63 68 20 64 69 73 74 69 6e 67 75 69 73  which distinguis
334d4 68 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 65  hes that.  ** te
334d5 72 6d 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74  rm from the last
334d6 20 74 65 72 6d 20 6f 66 20 74 68 65 20 70 72 65   term of the pre
334d7 76 69 6f 75 73 20 6e 6f 64 65 2e 0a 20 20 2a 2f  vious node..  */
334d8 0a 20 20 69 6e 74 20 6e 54 65 72 6d 44 69 73 74  .  int nTermDist
334d9 69 6e 63 74 3b 0a 0a 20 20 49 6e 74 65 72 69 6f  inct;..  Interio
334da 72 57 72 69 74 65 72 20 70 61 72 65 6e 74 57 72  rWriter parentWr
334db 69 74 65 72 3b 20 20 20 20 2f 2a 20 69 66 20 77  iter;    /* if w
334dc 65 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20  e overflow */.  
334dd 69 6e 74 20 68 61 73 5f 70 61 72 65 6e 74 3b 0a  int has_parent;.
334de 7d 20 4c 65 61 66 57 72 69 74 65 72 3b 0a 0a 73  } LeafWriter;..s
334df 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66 57  tatic void leafW
334e0 72 69 74 65 72 49 6e 69 74 28 69 6e 74 20 69 4c  riterInit(int iL
334e1 65 76 65 6c 2c 20 69 6e 74 20 69 64 78 2c 20 4c  evel, int idx, L
334e2 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74  eafWriter *pWrit
334e3 65 72 29 7b 0a 20 20 43 4c 45 41 52 28 70 57 72  er){.  CLEAR(pWr
334e4 69 74 65 72 29 3b 0a 20 20 70 57 72 69 74 65 72  iter);.  pWriter
334e5 2d 3e 69 4c 65 76 65 6c 20 3d 20 69 4c 65 76 65  ->iLevel = iLeve
334e6 6c 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 64  l;.  pWriter->id
334e7 78 20 3d 20 69 64 78 3b 0a 0a 20 20 64 61 74 61  x = idx;..  data
334e8 42 75 66 66 65 72 49 6e 69 74 28 26 70 57 72 69  BufferInit(&pWri
334e9 74 65 72 2d 3e 74 65 72 6d 2c 20 33 32 29 3b 0a  ter->term, 32);.
334ea 0a 20 20 2f 2a 20 53 74 61 72 74 20 6f 75 74 20  .  /* Start out 
334eb 77 69 74 68 20 61 20 72 65 61 73 6f 6e 61 62 6c  with a reasonabl
334ec 79 20 73 69 7a 65 64 20 62 6c 6f 63 6b 2c 20 74  y sized block, t
334ed 68 6f 75 67 68 20 69 74 20 63 61 6e 20 67 72 6f  hough it can gro
334ee 77 2e 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66  w. */.  dataBuff
334ef 65 72 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d  erInit(&pWriter-
334f0 3e 64 61 74 61 2c 20 4c 45 41 46 5f 4d 41 58 29  >data, LEAF_MAX)
334f1 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  ;.}..#ifndef NDE
334f2 42 55 47 0a 2f 2a 20 56 65 72 69 66 79 20 74 68  BUG./* Verify th
334f3 61 74 20 74 68 65 20 64 61 74 61 20 69 73 20 72  at the data is r
334f4 65 61 64 61 62 6c 65 20 61 73 20 61 20 6c 65 61  eadable as a lea
334f5 66 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74 69  f node. */.stati
334f6 63 20 76 6f 69 64 20 6c 65 61 66 4e 6f 64 65 56  c void leafNodeV
334f7 61 6c 69 64 61 74 65 28 63 6f 6e 73 74 20 63 68  alidate(const ch
334f8 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
334f9 44 61 74 61 29 7b 0a 20 20 69 6e 74 20 6e 2c 20  Data){.  int n, 
334fa 69 44 75 6d 6d 79 3b 0a 0a 20 20 69 66 28 20 6e  iDummy;..  if( n
334fb 44 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e  Data==0 ) return
334fc 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74  ;.  assert( nDat
334fd 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  a>0 );.  assert(
334fe 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 61   pData!=0 );.  a
334ff 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e 44 61  ssert( pData+nDa
33500 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20 20 2f  ta>pData );..  /
33501 2a 20 4d 75 73 74 20 6c 65 61 64 20 77 69 74 68  * Must lead with
33502 20 61 20 76 61 72 69 6e 74 28 30 29 20 2a 2f 0a   a varint(0) */.
33503 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
33504 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
33505 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28  ummy);.  assert(
33506 20 69 44 75 6d 6d 79 3d 3d 30 20 29 3b 0a 20 20   iDummy==0 );.  
33507 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20  assert( n>0 );. 
33508 20 61 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61   assert( n<nData
33509 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e   );.  pData += n
3350a 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a  ;.  nData -= n;.
3350b 0a 20 20 2f 2a 20 4c 65 61 64 69 6e 67 20 74 65  .  /* Leading te
3350c 72 6d 20 6c 65 6e 67 74 68 20 61 6e 64 20 64 61  rm length and da
3350d 74 61 20 6d 75 73 74 20 66 69 74 20 69 6e 20 62  ta must fit in b
3350e 75 66 66 65 72 2e 20 2a 2f 0a 20 20 6e 20 3d 20  uffer. */.  n = 
3350f 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
33510 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b  pData, &iDummy);
33511 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29  .  assert( n>0 )
33512 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d  ;.  assert( iDum
33513 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  my>0 );.  assert
33514 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  ( n+iDummy>0 );.
33515 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
33516 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44  my<nData );.  pD
33517 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
33518 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44  .  nData -= n+iD
33519 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64  ummy;..  /* Lead
3351a 69 6e 67 20 74 65 72 6d 27 73 20 64 6f 63 6c 69  ing term's docli
3351b 73 74 20 6c 65 6e 67 74 68 20 61 6e 64 20 64 61  st length and da
3351c 74 61 20 6d 75 73 74 20 66 69 74 2e 20 2a 2f 0a  ta must fit. */.
3351d 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
3351e 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
3351f 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28  ummy);.  assert(
33520 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74   n>0 );.  assert
33521 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20  ( iDummy>0 );.  
33522 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
33523 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
33524 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61 20  n+iDummy<=nData 
33525 29 3b 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49  );.  ASSERT_VALI
33526 44 5f 44 4f 43 4c 49 53 54 28 44 4c 5f 44 45 46  D_DOCLIST(DL_DEF
33527 41 55 4c 54 2c 20 70 44 61 74 61 2b 6e 2c 20 69  AULT, pData+n, i
33528 44 75 6d 6d 79 2c 20 4e 55 4c 4c 29 3b 0a 20 20  Dummy, NULL);.  
33529 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d  pData += n+iDumm
3352a 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b  y;.  nData -= n+
3352b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 56 65  iDummy;..  /* Ve
3352c 72 69 66 79 20 74 68 61 74 20 74 72 61 69 6c 69  rify that traili
3352d 6e 67 20 74 65 72 6d 73 20 61 6e 64 20 64 6f 63  ng terms and doc
3352e 6c 69 73 74 73 20 61 6c 73 6f 20 61 72 65 20 72  lists also are r
3352f 65 61 64 61 62 6c 65 2e 20 2a 2f 0a 20 20 77 68  eadable. */.  wh
33530 69 6c 65 28 20 6e 44 61 74 61 21 3d 30 20 29 7b  ile( nData!=0 ){
33531 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
33532 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
33533 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73  &iDummy);.    as
33534 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20  sert( n>0 );.   
33535 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e   assert( iDummy>
33536 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
33537 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20  ( n<nData );.   
33538 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 20   pData += n;.   
33539 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 20 20 20   nData -= n;.   
3353a 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
3353b 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
3353c 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74  mmy);.    assert
3353d 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ( n>0 );.    ass
3353e 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
3353f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69  .    assert( n+i
33540 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
33541 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
33542 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61  nData );.    pDa
33543 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a  ta += n+iDummy;.
33544 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69      nData -= n+i
33545 44 75 6d 6d 79 3b 0a 0a 20 20 20 20 6e 20 3d 20  Dummy;..    n = 
33546 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
33547 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b  pData, &iDummy);
33548 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30  .    assert( n>0
33549 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
3354a 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20  iDummy>0 );.    
3354b 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
3354c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
3354d 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74  ( n+iDummy<=nDat
3354e 61 20 29 3b 0a 20 20 20 20 41 53 53 45 52 54 5f  a );.    ASSERT_
3354f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44 4c  VALID_DOCLIST(DL
33550 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2b  _DEFAULT, pData+
33551 6e 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c 29  n, iDummy, NULL)
33552 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
33553 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 6e 44 61  +iDummy;.    nDa
33554 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a  ta -= n+iDummy;.
33555 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53    }.}.#define AS
33556 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f  SERT_VALID_LEAF_
33557 4e 4f 44 45 28 70 2c 20 6e 29 20 6c 65 61 66 4e  NODE(p, n) leafN
33558 6f 64 65 56 61 6c 69 64 61 74 65 28 70 2c 20 6e  odeValidate(p, n
33559 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
3355a 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41  ASSERT_VALID_LEA
3355b 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20 61 73 73  F_NODE(p, n) ass
3355c 65 72 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a  ert( 1 ).#endif.
3355d 0a 2f 2a 20 46 6c 75 73 68 20 74 68 65 20 63 75  ./* Flush the cu
3355e 72 72 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 20  rrent leaf node 
3355f 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61  to %_segments, a
33560 6e 64 20 61 64 64 69 6e 67 20 74 68 65 20 72 65  nd adding the re
33561 73 75 6c 74 69 6e 67 0a 2a 2a 20 62 6c 6f 63 6b  sulting.** block
33562 69 64 20 61 6e 64 20 74 68 65 20 73 74 61 72 74  id and the start
33563 69 6e 67 20 74 65 72 6d 20 74 6f 20 74 68 65 20  ing term to the 
33564 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 77 68  interior node wh
33565 69 63 68 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74  ich will.** cont
33566 61 69 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69  ain it..*/.stati
33567 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72  c int leafWriter
33568 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 66 75  InternalFlush(fu
33569 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
3356a 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69  LeafWriter *pWri
3356b 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ter,.           
3356c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3356d 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 61 74          int iDat
3356e 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20  a, int nData){. 
3356f 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 42   sqlite_int64 iB
33570 6c 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20 63 6f  lockid = 0;.  co
33571 6e 73 74 20 63 68 61 72 20 2a 70 53 74 61 72 74  nst char *pStart
33572 69 6e 67 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e  ingTerm;.  int n
33573 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 72 63  StartingTerm, rc
33574 2c 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20  , n;..  /* Must 
33575 68 61 76 65 20 74 68 65 20 6c 65 61 64 69 6e 67  have the leading
33576 20 76 61 72 69 6e 74 28 30 29 20 66 6c 61 67 2c   varint(0) flag,
33577 20 70 6c 75 73 20 61 74 20 6c 65 61 73 74 20 73   plus at least s
33578 6f 6d 65 0a 20 20 2a 2a 20 76 61 6c 69 64 2d 6c  ome.  ** valid-l
33579 6f 6f 6b 69 6e 67 20 64 61 74 61 2e 0a 20 20 2a  ooking data..  *
3357a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74  /.  assert( nDat
3357b 61 3e 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28  a>2 );.  assert(
3357c 20 69 44 61 74 61 3e 3d 30 20 29 3b 0a 20 20 61   iData>=0 );.  a
3357d 73 73 65 72 74 28 20 69 44 61 74 61 2b 6e 44 61  ssert( iData+nDa
3357e 74 61 3c 3d 70 57 72 69 74 65 72 2d 3e 64 61 74  ta<=pWriter->dat
3357f 61 2e 6e 44 61 74 61 20 29 3b 0a 20 20 41 53 53  a.nData );.  ASS
33580 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e  ERT_VALID_LEAF_N
33581 4f 44 45 28 70 57 72 69 74 65 72 2d 3e 64 61 74  ODE(pWriter->dat
33582 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e  a.pData+iData, n
33583 44 61 74 61 29 3b 0a 0a 20 20 72 63 20 3d 20 62  Data);..  rc = b
33584 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 76 2c 20 70  lock_insert(v, p
33585 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
33586 74 61 2b 69 44 61 74 61 2c 20 6e 44 61 74 61 2c  ta+iData, nData,
33587 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69   &iBlockid);.  i
33588 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
33589 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
3358a 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64  assert( iBlockid
3358b 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 63  !=0 );..  /* Rec
3358c 6f 6e 73 74 72 75 63 74 20 74 68 65 20 66 69 72  onstruct the fir
3358d 73 74 20 74 65 72 6d 20 69 6e 20 74 68 65 20 6c  st term in the l
3358e 65 61 66 20 66 6f 72 20 70 75 72 70 6f 73 65 73  eaf for purposes
3358f 20 6f 66 20 62 75 69 6c 64 69 6e 67 0a 20 20 2a   of building.  *
33590 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e  * the interior n
33591 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 6e 20 3d 20  ode..  */.  n = 
33592 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
33593 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44  pWriter->data.pD
33594 61 74 61 2b 69 44 61 74 61 2b 31 2c 20 26 6e 53  ata+iData+1, &nS
33595 74 61 72 74 69 6e 67 54 65 72 6d 29 3b 0a 20 20  tartingTerm);.  
33596 70 53 74 61 72 74 69 6e 67 54 65 72 6d 20 3d 20  pStartingTerm = 
33597 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44  pWriter->data.pD
33598 61 74 61 2b 69 44 61 74 61 2b 31 2b 6e 3b 0a 20  ata+iData+1+n;. 
33599 20 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72   assert( pWriter
3359a 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 69 44 61  ->data.nData>iDa
3359b 74 61 2b 31 2b 6e 2b 6e 53 74 61 72 74 69 6e 67  ta+1+n+nStarting
3359c 54 65 72 6d 20 29 3b 0a 20 20 61 73 73 65 72 74  Term );.  assert
3359d 28 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d  ( pWriter->nTerm
3359e 44 69 73 74 69 6e 63 74 3e 30 20 29 3b 0a 20 20  Distinct>0 );.  
3359f 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d  assert( pWriter-
335a0 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3c 3d  >nTermDistinct<=
335a1 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 20 29 3b  nStartingTerm );
335a2 0a 20 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  .  nStartingTerm
335a3 20 3d 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72   = pWriter->nTer
335a4 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 69 66  mDistinct;..  if
335a5 28 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70  ( pWriter->has_p
335a6 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74  arent ){.    int
335a7 65 72 69 6f 72 57 72 69 74 65 72 41 70 70 65 6e  eriorWriterAppen
335a8 64 28 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65  d(&pWriter->pare
335a9 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  ntWriter,.      
335aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335ab 20 20 20 70 53 74 61 72 74 69 6e 67 54 65 72 6d     pStartingTerm
335ac 2c 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 2c  , nStartingTerm,
335ad 20 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 7d 65   iBlockid);.  }e
335ae 6c 73 65 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f  lse{.    interio
335af 72 57 72 69 74 65 72 49 6e 69 74 28 31 2c 20 70  rWriterInit(1, p
335b0 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 6e 53  StartingTerm, nS
335b1 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 69 42 6c  tartingTerm, iBl
335b2 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
335b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
335b4 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72  Writer->parentWr
335b5 69 74 65 72 29 3b 0a 20 20 20 20 70 57 72 69 74  iter);.    pWrit
335b6 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 3d  er->has_parent =
335b7 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72   1;.  }..  /* Tr
335b8 61 63 6b 20 74 68 65 20 73 70 61 6e 20 6f 66 20  ack the span of 
335b9 74 68 69 73 20 73 65 67 6d 65 6e 74 27 73 20 6c  this segment's l
335ba 65 61 66 20 6e 6f 64 65 73 2e 20 2a 2f 0a 20 20  eaf nodes. */.  
335bb 69 66 28 20 70 57 72 69 74 65 72 2d 3e 69 45 6e  if( pWriter->iEn
335bc 64 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b 0a 20  dBlockid==0 ){. 
335bd 20 20 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64     pWriter->iEnd
335be 42 6c 6f 63 6b 69 64 20 3d 20 70 57 72 69 74 65  Blockid = pWrite
335bf 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  r->iStartBlockid
335c0 20 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a 20 20 7d   = iBlockid;.  }
335c1 65 6c 73 65 7b 0a 20 20 20 20 70 57 72 69 74 65  else{.    pWrite
335c2 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 2b 2b  r->iEndBlockid++
335c3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 42  ;.    assert( iB
335c4 6c 6f 63 6b 69 64 3d 3d 70 57 72 69 74 65 72 2d  lockid==pWriter-
335c5 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 20 29 3b 0a  >iEndBlockid );.
335c6 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
335c7 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69  LITE_OK;.}.stati
335c8 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72  c int leafWriter
335c9 46 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f 76  Flush(fulltext_v
335ca 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
335cb 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
335cc 69 6e 74 20 72 63 20 3d 20 6c 65 61 66 57 72 69  int rc = leafWri
335cd 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68  terInternalFlush
335ce 28 76 2c 20 70 57 72 69 74 65 72 2c 20 30 2c 20  (v, pWriter, 0, 
335cf 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
335d0 61 74 61 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ata);.  if( rc!=
335d1 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
335d2 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 52 65 2d  rn rc;..  /* Re-
335d3 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f  initialize the o
335d4 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f  utput buffer. */
335d5 0a 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73  .  dataBufferRes
335d6 65 74 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  et(&pWriter->dat
335d7 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  a);..  return SQ
335d8 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 46  LITE_OK;.}../* F
335d9 65 74 63 68 20 74 68 65 20 72 6f 6f 74 20 69 6e  etch the root in
335da 66 6f 20 66 6f 72 20 74 68 65 20 73 65 67 6d 65  fo for the segme
335db 6e 74 2e 20 20 49 66 20 74 68 65 20 65 6e 74 69  nt.  If the enti
335dc 72 65 20 6c 65 61 66 20 66 69 74 73 0a 2a 2a 20  re leaf fits.** 
335dd 77 69 74 68 69 6e 20 52 4f 4f 54 5f 4d 41 58 2c  within ROOT_MAX,
335de 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65   then it will be
335df 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 74   returned direct
335e0 6c 79 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74  ly, otherwise it
335e1 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66 6c 75 73  .** will be flus
335e2 68 65 64 20 61 6e 64 20 74 68 65 20 72 6f 6f 74  hed and the root
335e3 20 69 6e 66 6f 20 77 69 6c 6c 20 62 65 20 72 65   info will be re
335e4 74 75 72 6e 65 64 20 66 72 6f 6d 20 74 68 65 0a  turned from the.
335e5 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ** interior node
335e6 2e 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  .  *piEndBlockid
335e7 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 62   is set to the b
335e8 6c 6f 63 6b 69 64 20 6f 66 20 74 68 65 20 6c 61  lockid of the la
335e9 73 74 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6f  st.** interior o
335ea 72 20 6c 65 61 66 20 6e 6f 64 65 20 77 72 69 74  r leaf node writ
335eb 74 65 6e 20 74 6f 20 64 69 73 6b 20 28 30 20 69  ten to disk (0 i
335ec 66 20 6e 6f 6e 65 20 61 72 65 20 77 72 69 74 74  f none are writt
335ed 65 6e 20 61 74 0a 2a 2a 20 61 6c 6c 29 2e 0a 2a  en at.** all)..*
335ee 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
335ef 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28  fWriterRootInfo(
335f0 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
335f1 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
335f2 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
335f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335f4 20 20 20 20 20 63 68 61 72 20 2a 2a 70 70 52 6f       char **ppRo
335f5 6f 74 49 6e 66 6f 2c 20 69 6e 74 20 2a 70 6e 52  otInfo, int *pnR
335f6 6f 6f 74 49 6e 66 6f 2c 0a 20 20 20 20 20 20 20  ootInfo,.       
335f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335f8 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
335f9 74 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69  t64 *piEndBlocki
335fa 64 29 7b 0a 20 20 2f 2a 20 77 65 20 63 61 6e 20  d){.  /* we can 
335fb 66 69 74 20 74 68 65 20 73 65 67 6d 65 6e 74 20  fit the segment 
335fc 65 6e 74 69 72 65 6c 79 20 69 6e 6c 69 6e 65 20  entirely inline 
335fd 2a 2f 0a 20 20 69 66 28 20 21 70 57 72 69 74 65  */.  if( !pWrite
335fe 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 26 26  r->has_parent &&
335ff 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e   pWriter->data.n
33600 44 61 74 61 3c 52 4f 4f 54 5f 4d 41 58 20 29 7b  Data<ROOT_MAX ){
33601 0a 20 20 20 20 2a 70 70 52 6f 6f 74 49 6e 66 6f  .    *ppRootInfo
33602 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
33603 2e 70 44 61 74 61 3b 0a 20 20 20 20 2a 70 6e 52  .pData;.    *pnR
33604 6f 6f 74 49 6e 66 6f 20 3d 20 70 57 72 69 74 65  ootInfo = pWrite
33605 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20  r->data.nData;. 
33606 20 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64     *piEndBlockid
33607 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
33608 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
33609 0a 20 20 2f 2a 20 46 6c 75 73 68 20 72 65 6d 61  .  /* Flush rema
3360a 69 6e 69 6e 67 20 6c 65 61 66 20 64 61 74 61 2e  ining leaf data.
3360b 20 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65   */.  if( pWrite
3360c 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 30 20  r->data.nData>0 
3360d 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
3360e 6c 65 61 66 57 72 69 74 65 72 46 6c 75 73 68 28  leafWriterFlush(
3360f 76 2c 20 70 57 72 69 74 65 72 29 3b 0a 20 20 20  v, pWriter);.   
33610 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33611 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
33612 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 6d 75 73    }..  /* We mus
33613 74 20 68 61 76 65 20 66 6c 75 73 68 65 64 20 61  t have flushed a
33614 20 6c 65 61 66 20 61 74 20 73 6f 6d 65 20 70 6f   leaf at some po
33615 69 6e 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  int. */.  assert
33616 28 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70  ( pWriter->has_p
33617 61 72 65 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 54  arent );..  /* T
33618 65 6e 61 74 69 76 65 6c 79 20 73 65 74 20 74 68  enatively set th
33619 65 20 65 6e 64 20 6c 65 61 66 20 62 6c 6f 63 6b  e end leaf block
3361a 69 64 20 61 73 20 74 68 65 20 65 6e 64 20 62 6c  id as the end bl
3361b 6f 63 6b 69 64 2e 20 20 49 66 20 74 68 65 0a 20  ockid.  If the. 
3361c 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   ** interior nod
3361d 65 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65  e can be returne
3361e 64 20 69 6e 6c 69 6e 65 2c 20 74 68 69 73 20 77  d inline, this w
3361f 69 6c 6c 20 62 65 20 74 68 65 20 66 69 6e 61 6c  ill be the final
33620 0a 20 20 2a 2a 20 62 6c 6f 63 6b 69 64 2c 20 6f  .  ** blockid, o
33621 74 68 65 72 77 69 73 65 20 69 74 20 77 69 6c 6c  therwise it will
33622 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20   be overwritten 
33623 62 79 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72  by.  ** interior
33624 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29  WriterRootInfo()
33625 2e 0a 20 20 2a 2f 0a 20 20 2a 70 69 45 6e 64 42  ..  */.  *piEndB
33626 6c 6f 63 6b 69 64 20 3d 20 70 57 72 69 74 65 72  lockid = pWriter
33627 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 0a  ->iEndBlockid;..
33628 20 20 72 65 74 75 72 6e 20 69 6e 74 65 72 69 6f    return interio
33629 72 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28  rWriterRootInfo(
3362a 76 2c 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72  v, &pWriter->par
3362b 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20  entWriter,.     
3362c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3362d 20 20 20 20 20 20 20 20 20 20 20 70 70 52 6f 6f             ppRoo
3362e 74 49 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66  tInfo, pnRootInf
3362f 6f 2c 20 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29  o, piEndBlockid)
33630 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6c 6c 65 63 74 20  ;.}../* Collect 
33631 74 68 65 20 72 6f 6f 74 49 6e 66 6f 20 64 61 74  the rootInfo dat
33632 61 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69  a and store it i
33633 6e 74 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 20  nto the segment 
33634 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 20 54 68  directory..** Th
33635 69 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63  is has the effec
33636 74 20 6f 66 20 66 6c 75 73 68 69 6e 67 20 74 68  t of flushing th
33637 65 20 73 65 67 6d 65 6e 74 27 73 20 6c 65 61 66  e segment's leaf
33638 20 64 61 74 61 20 74 6f 0a 2a 2a 20 25 5f 73 65   data to.** %_se
33639 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f  gments, and also
3363a 20 66 6c 75 73 68 69 6e 67 20 61 6e 79 20 69 6e   flushing any in
3363b 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 74 6f 20  terior nodes to 
3363c 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  %_segments..*/.s
3363d 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72  tatic int leafWr
3363e 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 66 75 6c  iterFinalize(ful
3363f 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c  ltext_vtab *v, L
33640 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74  eafWriter *pWrit
33641 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  er){.  sqlite_in
33642 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b  t64 iEndBlockid;
33643 0a 20 20 63 68 61 72 20 2a 70 52 6f 6f 74 49 6e  .  char *pRootIn
33644 66 6f 3b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 52  fo;.  int rc, nR
33645 6f 6f 74 49 6e 66 6f 3b 0a 0a 20 20 72 63 20 3d  ootInfo;..  rc =
33646 20 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49   leafWriterRootI
33647 6e 66 6f 28 76 2c 20 70 57 72 69 74 65 72 2c 20  nfo(v, pWriter, 
33648 26 70 52 6f 6f 74 49 6e 66 6f 2c 20 26 6e 52 6f  &pRootInfo, &nRo
33649 6f 74 49 6e 66 6f 2c 20 26 69 45 6e 64 42 6c 6f  otInfo, &iEndBlo
3364a 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ckid);.  if( rc!
3364b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3364c 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f  urn rc;..  /* Do
3364d 6e 27 74 20 62 6f 74 68 65 72 20 73 74 6f 72 69  n't bother stori
3364e 6e 67 20 61 6e 20 65 6e 74 69 72 65 6c 79 20 65  ng an entirely e
3364f 6d 70 74 79 20 73 65 67 6d 65 6e 74 2e 20 2a 2f  mpty segment. */
33650 0a 20 20 69 66 28 20 69 45 6e 64 42 6c 6f 63 6b  .  if( iEndBlock
33651 69 64 3d 3d 30 20 26 26 20 6e 52 6f 6f 74 49 6e  id==0 && nRootIn
33652 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53  fo==0 ) return S
33653 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 65 74  QLITE_OK;..  ret
33654 75 72 6e 20 73 65 67 64 69 72 5f 73 65 74 28 76  urn segdir_set(v
33655 2c 20 70 57 72 69 74 65 72 2d 3e 69 4c 65 76 65  , pWriter->iLeve
33656 6c 2c 20 70 57 72 69 74 65 72 2d 3e 69 64 78 2c  l, pWriter->idx,
33657 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33658 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 53       pWriter->iS
33659 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 70 57 72  tartBlockid, pWr
3365a 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
3365b 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
3365c 20 20 20 20 20 20 20 69 45 6e 64 42 6c 6f 63 6b         iEndBlock
3365d 69 64 2c 20 70 52 6f 6f 74 49 6e 66 6f 2c 20 6e  id, pRootInfo, n
3365e 52 6f 6f 74 49 6e 66 6f 29 3b 0a 7d 0a 0a 73 74  RootInfo);.}..st
3365f 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66 57 72  atic void leafWr
33660 69 74 65 72 44 65 73 74 72 6f 79 28 4c 65 61 66  iterDestroy(Leaf
33661 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
33662 7b 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  {.  if( pWriter-
33663 3e 68 61 73 5f 70 61 72 65 6e 74 20 29 20 69 6e  >has_parent ) in
33664 74 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74  teriorWriterDest
33665 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e 70 61  roy(&pWriter->pa
33666 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 64  rentWriter);.  d
33667 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
33668 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29  (&pWriter->term)
33669 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
3366a 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e  stroy(&pWriter->
3366b 64 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20 45 6e 63  data);.}../* Enc
3366c 6f 64 65 20 61 20 74 65 72 6d 20 69 6e 74 6f 20  ode a term into 
3366d 74 68 65 20 6c 65 61 66 57 72 69 74 65 72 2c 20  the leafWriter, 
3366e 64 65 6c 74 61 2d 65 6e 63 6f 64 69 6e 67 20 61  delta-encoding a
3366f 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a  s appropriate..*
33670 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 6c 65  * Returns the le
33671 6e 67 74 68 20 6f 66 20 74 68 65 20 6e 65 77 20  ngth of the new 
33672 74 65 72 6d 20 77 68 69 63 68 20 64 69 73 74 69  term which disti
33673 6e 67 75 69 73 68 65 73 20 69 74 20 66 72 6f 6d  nguishes it from
33674 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73   the.** previous
33675 20 74 65 72 6d 2c 20 77 68 69 63 68 20 63 61 6e   term, which can
33676 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 20   be used to set 
33677 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 77 68  nTermDistinct wh
33678 65 6e 20 61 20 6e 6f 64 65 0a 2a 2a 20 62 6f 75  en a node.** bou
33679 6e 64 61 72 79 20 69 73 20 63 72 6f 73 73 65 64  ndary is crossed
3367a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3367b 6c 65 61 66 57 72 69 74 65 72 45 6e 63 6f 64 65  leafWriterEncode
3367c 54 65 72 6d 28 4c 65 61 66 57 72 69 74 65 72 20  Term(LeafWriter 
3367d 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
3367e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3367f 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
33680 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
33681 20 6e 54 65 72 6d 29 7b 0a 20 20 63 68 61 72 20   nTerm){.  char 
33682 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52  c[VARINT_MAX+VAR
33683 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20  INT_MAX];.  int 
33684 6e 2c 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 0a  n, nPrefix = 0;.
33685 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d  .  assert( nTerm
33686 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e  >0 );.  while( n
33687 50 72 65 66 69 78 3c 70 57 72 69 74 65 72 2d 3e  Prefix<pWriter->
33688 74 65 72 6d 2e 6e 44 61 74 61 20 26 26 0a 20 20  term.nData &&.  
33689 20 20 20 20 20 20 20 70 54 65 72 6d 5b 6e 50 72         pTerm[nPr
3368a 65 66 69 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e  efix]==pWriter->
3368b 74 65 72 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66  term.pData[nPref
3368c 69 78 5d 20 29 7b 0a 20 20 20 20 6e 50 72 65 66  ix] ){.    nPref
3368d 69 78 2b 2b 3b 0a 20 20 20 20 2f 2a 20 46 61 69  ix++;.    /* Fai
3368e 6c 69 6e 67 20 74 68 69 73 20 69 6d 70 6c 69 65  ling this implie
3368f 73 20 74 68 61 74 20 74 68 65 20 74 65 72 6d 73  s that the terms
33690 20 77 65 72 65 6e 27 74 20 69 6e 20 6f 72 64 65   weren't in orde
33691 72 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  r. */.    assert
33692 28 20 6e 50 72 65 66 69 78 3c 6e 54 65 72 6d 20  ( nPrefix<nTerm 
33693 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 57  );.  }..  if( pW
33694 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
33695 61 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 45  a==0 ){.    /* E
33696 6e 63 6f 64 65 20 74 68 65 20 6e 6f 64 65 20 68  ncode the node h
33697 65 61 64 65 72 20 61 6e 64 20 6c 65 61 64 69 6e  eader and leadin
33698 67 20 74 65 72 6d 20 61 73 3a 0a 20 20 20 20 2a  g term as:.    *
33699 2a 20 20 76 61 72 69 6e 74 28 30 29 0a 20 20 20  *  varint(0).   
3369a 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e 54 65 72   **  varint(nTer
3369b 6d 29 0a 20 20 20 20 2a 2a 20 20 63 68 61 72 20  m).    **  char 
3369c 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 0a 20 20 20  pTerm[nTerm].   
3369d 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33   */.    n = fts3
3369e 50 75 74 56 61 72 69 6e 74 28 63 2c 20 27 5c 30  PutVarint(c, '\0
3369f 27 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  ');.    n += fts
336a0 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20  3PutVarint(c+n, 
336a1 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61  nTerm);.    data
336a2 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 26 70  BufferAppend2(&p
336a3 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c  Writer->data, c,
336a4 20 6e 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d   n, pTerm, nTerm
336a5 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
336a6 2f 2a 20 44 65 6c 74 61 2d 65 6e 63 6f 64 65 20  /* Delta-encode 
336a7 74 68 65 20 74 65 72 6d 20 61 73 3a 0a 20 20 20  the term as:.   
336a8 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e 50 72 65   **  varint(nPre
336a9 66 69 78 29 0a 20 20 20 20 2a 2a 20 20 76 61 72  fix).    **  var
336aa 69 6e 74 28 6e 53 75 66 66 69 78 29 0a 20 20 20  int(nSuffix).   
336ab 20 2a 2a 20 20 63 68 61 72 20 70 54 65 72 6d 53   **  char pTermS
336ac 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 0a 20  uffix[nSuffix]. 
336ad 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74     */.    n = ft
336ae 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 6e  s3PutVarint(c, n
336af 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 20 2b  Prefix);.    n +
336b0 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
336b1 63 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66  c+n, nTerm-nPref
336b2 69 78 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66  ix);.    dataBuf
336b3 66 65 72 41 70 70 65 6e 64 32 28 26 70 57 72 69  ferAppend2(&pWri
336b4 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 2c  ter->data, c, n,
336b5 20 70 54 65 72 6d 2b 6e 50 72 65 66 69 78 2c 20   pTerm+nPrefix, 
336b6 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a  nTerm-nPrefix);.
336b7 20 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72    }.  dataBuffer
336b8 52 65 70 6c 61 63 65 28 26 70 57 72 69 74 65 72  Replace(&pWriter
336b9 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e  ->term, pTerm, n
336ba 54 65 72 6d 29 3b 0a 0a 20 20 72 65 74 75 72 6e  Term);..  return
336bb 20 6e 50 72 65 66 69 78 2b 31 3b 0a 7d 0a 0a 2f   nPrefix+1;.}../
336bc 2a 20 55 73 65 64 20 74 6f 20 61 76 6f 69 64 20  * Used to avoid 
336bd 61 20 6d 65 6d 6d 6f 76 65 20 77 68 65 6e 20 61  a memmove when a
336be 20 6c 61 72 67 65 20 61 6d 6f 75 6e 74 20 6f 66   large amount of
336bf 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 69 73   doclist data is
336c0 20 69 6e 0a 2a 2a 20 74 68 65 20 62 75 66 66 65   in.** the buffe
336c1 72 2e 20 20 54 68 69 73 20 63 6f 6e 73 74 72 75  r.  This constru
336c2 63 74 73 20 61 20 6e 6f 64 65 20 61 6e 64 20 74  cts a node and t
336c3 65 72 6d 20 68 65 61 64 65 72 20 62 65 66 6f 72  erm header befor
336c4 65 0a 2a 2a 20 69 44 6f 63 6c 69 73 74 44 61 74  e.** iDoclistDat
336c5 61 20 61 6e 64 20 66 6c 75 73 68 65 73 20 74 68  a and flushes th
336c6 65 20 72 65 73 75 6c 74 69 6e 67 20 63 6f 6d 70  e resulting comp
336c7 6c 65 74 65 20 6e 6f 64 65 20 75 73 69 6e 67 0a  lete node using.
336c8 2a 2a 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74  ** leafWriterInt
336c9 65 72 6e 61 6c 46 6c 75 73 68 28 29 2e 0a 2a 2f  ernalFlush()..*/
336ca 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
336cb 57 72 69 74 65 72 49 6e 6c 69 6e 65 46 6c 75 73  WriterInlineFlus
336cc 68 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  h(fulltext_vtab 
336cd 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  *v, LeafWriter *
336ce 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
336cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336d0 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
336d1 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
336d2 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20   nTerm,.        
336d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336d4 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 6f           int iDo
336d5 63 6c 69 73 74 44 61 74 61 29 7b 0a 20 20 63 68  clistData){.  ch
336d6 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
336d7 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
336d8 6e 74 20 69 44 61 74 61 2c 20 6e 20 3d 20 66 74  nt iData, n = ft
336d9 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 30  s3PutVarint(c, 0
336da 29 3b 0a 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  );.  n += fts3Pu
336db 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65  tVarint(c+n, nTe
336dc 72 6d 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65  rm);..  /* There
336dd 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62   should always b
336de 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 68  e room for the h
336df 65 61 64 65 72 2e 20 20 45 76 65 6e 20 69 66 20  eader.  Even if 
336e0 70 54 65 72 6d 20 73 68 61 72 65 64 0a 20 20 2a  pTerm shared.  *
336e1 2a 20 61 20 73 75 62 73 74 61 6e 74 69 61 6c 20  * a substantial 
336e2 70 72 65 66 69 78 20 77 69 74 68 20 74 68 65 20  prefix with the 
336e3 70 72 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 74  previous term, t
336e4 68 65 20 65 6e 74 69 72 65 20 70 72 65 66 69 78  he entire prefix
336e5 0a 20 20 2a 2a 20 63 6f 75 6c 64 20 62 65 20 63  .  ** could be c
336e6 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20  onstructed from 
336e7 65 61 72 6c 69 65 72 20 64 61 74 61 20 69 6e 20  earlier data in 
336e8 74 68 65 20 64 6f 63 6c 69 73 74 2c 20 73 6f 20  the doclist, so 
336e9 74 68 65 72 65 0a 20 20 2a 2a 20 73 68 6f 75 6c  there.  ** shoul
336ea 64 20 62 65 20 72 6f 6f 6d 2e 0a 20 20 2a 2f 0a  d be room..  */.
336eb 20 20 61 73 73 65 72 74 28 20 69 44 6f 63 6c 69    assert( iDocli
336ec 73 74 44 61 74 61 3e 3d 6e 2b 6e 54 65 72 6d 20  stData>=n+nTerm 
336ed 29 3b 0a 0a 20 20 69 44 61 74 61 20 3d 20 69 44  );..  iData = iD
336ee 6f 63 6c 69 73 74 44 61 74 61 2d 28 6e 2b 6e 54  oclistData-(n+nT
336ef 65 72 6d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  erm);.  memcpy(p
336f0 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
336f1 74 61 2b 69 44 61 74 61 2c 20 63 2c 20 6e 29 3b  ta+iData, c, n);
336f2 0a 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65  .  memcpy(pWrite
336f3 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44  r->data.pData+iD
336f4 61 74 61 2b 6e 2c 20 70 54 65 72 6d 2c 20 6e 54  ata+n, pTerm, nT
336f5 65 72 6d 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  erm);..  return 
336f6 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e  leafWriterIntern
336f7 61 6c 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74  alFlush(v, pWrit
336f8 65 72 2c 20 69 44 61 74 61 2c 20 70 57 72 69 74  er, iData, pWrit
336f9 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69  er->data.nData-i
336fa 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20 50 75 73  Data);.}../* Pus
336fb 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61  h pTerm[nTerm] a
336fc 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 64 6f  long with the do
336fd 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20 74 68  clist data to th
336fe 65 20 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 0a  e leaf layer of.
336ff 2a 2a 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a  ** %_segments..*
33700 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
33701 66 57 72 69 74 65 72 53 74 65 70 4d 65 72 67 65  fWriterStepMerge
33702 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
33703 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70  v, LeafWriter *p
33704 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
33705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33706 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
33707 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
33708 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  erm,.           
33709 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3370a 20 20 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52      DLReader *pR
3370b 65 61 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61  eaders, int nRea
3370c 64 65 72 73 29 7b 0a 20 20 63 68 61 72 20 63 5b  ders){.  char c[
3370d 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e  VARINT_MAX+VARIN
3370e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 69 54  T_MAX];.  int iT
3370f 65 72 6d 44 61 74 61 20 3d 20 70 57 72 69 74 65  ermData = pWrite
33710 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 69  r->data.nData, i
33711 44 6f 63 6c 69 73 74 44 61 74 61 3b 0a 20 20 69  DoclistData;.  i
33712 6e 74 20 69 2c 20 6e 44 61 74 61 2c 20 6e 2c 20  nt i, nData, n, 
33713 6e 41 63 74 75 61 6c 44 61 74 61 2c 20 6e 41 63  nActualData, nAc
33714 74 75 61 6c 2c 20 72 63 2c 20 6e 54 65 72 6d 44  tual, rc, nTermD
33715 69 73 74 69 6e 63 74 3b 0a 0a 20 20 41 53 53 45  istinct;..  ASSE
33716 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f  RT_VALID_LEAF_NO
33717 44 45 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  DE(pWriter->data
33718 2e 70 44 61 74 61 2c 20 70 57 72 69 74 65 72 2d  .pData, pWriter-
33719 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b 0a 20 20  >data.nData);.  
3371a 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d 20  nTermDistinct = 
3371b 6c 65 61 66 57 72 69 74 65 72 45 6e 63 6f 64 65  leafWriterEncode
3371c 54 65 72 6d 28 70 57 72 69 74 65 72 2c 20 70 54  Term(pWriter, pT
3371d 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20  erm, nTerm);..  
3371e 2f 2a 20 52 65 6d 65 6d 62 65 72 20 6e 54 65 72  /* Remember nTer
3371f 6d 44 69 73 74 69 6e 63 74 20 69 66 20 6f 70 65  mDistinct if ope
33720 6e 69 6e 67 20 61 20 6e 65 77 20 6e 6f 64 65 2e  ning a new node.
33721 20 2a 2f 0a 20 20 69 66 28 20 69 54 65 72 6d 44   */.  if( iTermD
33722 61 74 61 3d 3d 30 20 29 20 70 57 72 69 74 65 72  ata==0 ) pWriter
33723 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20  ->nTermDistinct 
33724 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b  = nTermDistinct;
33725 0a 0a 20 20 69 44 6f 63 6c 69 73 74 44 61 74 61  ..  iDoclistData
33726 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
33727 2e 6e 44 61 74 61 3b 0a 0a 20 20 2f 2a 20 45 73  .nData;..  /* Es
33728 74 69 6d 61 74 65 20 74 68 65 20 6c 65 6e 67 74  timate the lengt
33729 68 20 6f 66 20 74 68 65 20 6d 65 72 67 65 64 20  h of the merged 
3372a 64 6f 63 6c 69 73 74 20 73 6f 20 77 65 20 63 61  doclist so we ca
3372b 6e 20 6c 65 61 76 65 20 73 70 61 63 65 0a 20 20  n leave space.  
3372c 2a 2a 20 74 6f 20 65 6e 63 6f 64 65 20 69 74 2e  ** to encode it.
3372d 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c  .  */.  for(i=0,
3372e 20 6e 44 61 74 61 3d 30 3b 20 69 3c 6e 52 65 61   nData=0; i<nRea
3372f 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ders; i++){.    
33730 6e 44 61 74 61 20 2b 3d 20 64 6c 72 41 6c 6c 44  nData += dlrAllD
33731 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65  ataBytes(&pReade
33732 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6e 20  rs[i]);.  }.  n 
33733 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
33734 63 2c 20 6e 44 61 74 61 29 3b 0a 20 20 64 61 74  c, nData);.  dat
33735 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 70  aBufferAppend(&p
33736 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c  Writer->data, c,
33737 20 6e 29 3b 0a 0a 20 20 64 6f 63 4c 69 73 74 4d   n);..  docListM
33738 65 72 67 65 28 26 70 57 72 69 74 65 72 2d 3e 64  erge(&pWriter->d
33739 61 74 61 2c 20 70 52 65 61 64 65 72 73 2c 20 6e  ata, pReaders, n
3373a 52 65 61 64 65 72 73 29 3b 0a 20 20 41 53 53 45  Readers);.  ASSE
3373b 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54  RT_VALID_DOCLIST
3373c 28 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20  (DL_DEFAULT,.   
3373d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3373e 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
3373f 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74  a.pData+iDoclist
33740 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20  Data+n,.        
33741 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
33742 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
33743 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61 2d  ta-iDoclistData-
33744 6e 2c 20 4e 55 4c 4c 29 3b 0a 0a 20 20 2f 2a 20  n, NULL);..  /* 
33745 54 68 65 20 61 63 74 75 61 6c 20 61 6d 6f 75 6e  The actual amoun
33746 74 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61 74  t of doclist dat
33747 61 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20  a at this point 
33748 63 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 65 72  could be smaller
33749 0a 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 6c  .  ** than the l
3374a 65 6e 67 74 68 20 77 65 20 65 6e 63 6f 64 65 64  ength we encoded
3374b 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  .  Additionally,
3374c 20 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69   the space requi
3374d 72 65 64 20 74 6f 0a 20 20 2a 2a 20 65 6e 63 6f  red to.  ** enco
3374e 64 65 20 74 68 69 73 20 6c 65 6e 67 74 68 20 63  de this length c
3374f 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 65 72 2e  ould be smaller.
33750 20 20 46 6f 72 20 73 6d 61 6c 6c 20 64 6f 63 6c    For small docl
33751 69 73 74 73 2c 20 74 68 69 73 20 69 73 0a 20 20  ists, this is.  
33752 2a 2a 20 6e 6f 74 20 61 20 62 69 67 20 64 65 61  ** not a big dea
33753 6c 2c 20 77 65 20 63 61 6e 20 6a 75 73 74 20 75  l, we can just u
33754 73 65 20 6d 65 6d 6d 6f 76 65 28 29 20 74 6f 20  se memmove() to 
33755 61 64 6a 75 73 74 20 74 68 69 6e 67 73 2e 0a 20  adjust things.. 
33756 20 2a 2f 0a 20 20 6e 41 63 74 75 61 6c 44 61 74   */.  nActualDat
33757 61 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74  a = pWriter->dat
33758 61 2e 6e 44 61 74 61 2d 28 69 44 6f 63 6c 69 73  a.nData-(iDoclis
33759 74 44 61 74 61 2b 6e 29 3b 0a 20 20 6e 41 63 74  tData+n);.  nAct
3375a 75 61 6c 20 3d 20 66 74 73 33 50 75 74 56 61 72  ual = fts3PutVar
3375b 69 6e 74 28 63 2c 20 6e 41 63 74 75 61 6c 44 61  int(c, nActualDa
3375c 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  ta);.  assert( n
3375d 41 63 74 75 61 6c 44 61 74 61 3c 3d 6e 44 61 74  ActualData<=nDat
3375e 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  a );.  assert( n
3375f 41 63 74 75 61 6c 3c 3d 6e 20 29 3b 0a 0a 20 20  Actual<=n );..  
33760 2f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 64 6f  /* If the new do
33761 63 6c 69 73 74 20 69 73 20 62 69 67 20 65 6e 6f  clist is big eno
33762 75 67 68 20 66 6f 72 20 66 6f 72 63 65 20 61 20  ugh for force a 
33763 73 74 61 6e 64 61 6c 6f 6e 65 20 6c 65 61 66 0a  standalone leaf.
33764 20 20 2a 2a 20 6e 6f 64 65 2c 20 77 65 20 63 61    ** node, we ca
33765 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6c  n immediately fl
33766 75 73 68 20 69 74 20 69 6e 6c 69 6e 65 20 77 69  ush it inline wi
33767 74 68 6f 75 74 20 64 6f 69 6e 67 20 74 68 65 0a  thout doing the.
33768 20 20 2a 2a 20 6d 65 6d 6d 6f 76 65 28 29 2e 0a    ** memmove()..
33769 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73    */.  /* TODO(s
3376a 68 65 73 73 29 20 54 68 69 73 20 74 65 73 74 20  hess) This test 
3376b 6d 61 74 63 68 65 73 20 6c 65 61 66 57 72 69 74  matches leafWrit
3376c 65 72 53 74 65 70 28 29 2c 20 77 68 69 63 68 20  erStep(), which 
3376d 64 6f 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 74  does this.  ** t
3376e 65 73 74 20 62 65 66 6f 72 65 20 69 74 20 6b 6e  est before it kn
3376f 6f 77 73 20 74 68 65 20 63 6f 73 74 20 74 6f 20  ows the cost to 
33770 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65 20 74 68  varint-encode th
33771 65 20 74 65 72 6d 20 61 6e 64 0a 20 20 2a 2a 20  e term and.  ** 
33772 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68 73 2e  doclist lengths.
33773 20 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 2c    At some point,
33774 20 63 68 61 6e 67 65 20 74 6f 0a 20 20 2a 2a 20   change to.  ** 
33775 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
33776 61 74 61 2d 69 54 65 72 6d 44 61 74 61 3e 53 54  ata-iTermData>ST
33777 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 2e 0a 20 20  ANDALONE_MIN..  
33778 2a 2f 0a 20 20 69 66 28 20 6e 54 65 72 6d 2b 6e  */.  if( nTerm+n
33779 41 63 74 75 61 6c 44 61 74 61 3e 53 54 41 4e 44  ActualData>STAND
3377a 41 4c 4f 4e 45 5f 4d 49 4e 20 29 7b 0a 20 20 20  ALONE_MIN ){.   
3377b 20 2f 2a 20 50 75 73 68 20 6c 65 61 66 20 6e 6f   /* Push leaf no
3377c 64 65 20 66 72 6f 6d 20 62 65 66 6f 72 65 20 74  de from before t
3377d 68 69 73 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 20  his term. */.   
3377e 20 69 66 28 20 69 54 65 72 6d 44 61 74 61 3e 30   if( iTermData>0
3377f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c   ){.      rc = l
33780 65 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61  eafWriterInterna
33781 6c 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65  lFlush(v, pWrite
33782 72 2c 20 30 2c 20 69 54 65 72 6d 44 61 74 61 29  r, 0, iTermData)
33783 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
33784 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
33785 72 6e 20 72 63 3b 0a 0a 20 20 20 20 20 20 70 57  rn rc;..      pW
33786 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
33787 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
33788 69 6e 63 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  inct;.    }..   
33789 20 2f 2a 20 46 69 78 20 74 68 65 20 65 6e 63 6f   /* Fix the enco
3378a 64 65 64 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67  ded doclist leng
3378b 74 68 2e 20 2a 2f 0a 20 20 20 20 69 44 6f 63 6c  th. */.    iDocl
3378c 69 73 74 44 61 74 61 20 2b 3d 20 6e 20 2d 20 6e  istData += n - n
3378d 41 63 74 75 61 6c 3b 0a 20 20 20 20 6d 65 6d 63  Actual;.    memc
3378e 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  py(pWriter->data
3378f 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
33790 61 74 61 2c 20 63 2c 20 6e 41 63 74 75 61 6c 29  ata, c, nActual)
33791 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 74  ;..    /* Push t
33792 68 65 20 73 74 61 6e 64 61 6c 6f 6e 65 20 6c 65  he standalone le
33793 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20  af node. */.    
33794 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49  rc = leafWriterI
33795 6e 6c 69 6e 65 46 6c 75 73 68 28 76 2c 20 70 57  nlineFlush(v, pW
33796 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  riter, pTerm, nT
33797 65 72 6d 2c 20 69 44 6f 63 6c 69 73 74 44 61 74  erm, iDoclistDat
33798 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  a);.    if( rc!=
33799 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
3379a 72 6e 20 72 63 3b 0a 0a 20 20 20 20 2f 2a 20 4c  rn rc;..    /* L
3379b 65 61 76 65 20 74 68 65 20 6e 6f 64 65 20 65 6d  eave the node em
3379c 70 74 79 2e 20 2a 2f 0a 20 20 20 20 64 61 74 61  pty. */.    data
3379d 42 75 66 66 65 72 52 65 73 65 74 28 26 70 57 72  BufferReset(&pWr
3379e 69 74 65 72 2d 3e 64 61 74 61 29 3b 0a 0a 20 20  iter->data);..  
3379f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
337a0 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70  ..  /* At this p
337a1 6f 69 6e 74 2c 20 77 65 20 6b 6e 6f 77 20 74 68  oint, we know th
337a2 61 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20 77  at the doclist w
337a3 61 73 20 73 6d 61 6c 6c 2c 20 73 6f 20 64 6f 20  as small, so do 
337a4 74 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76 65  the.  ** memmove
337a5 20 69 66 20 69 6e 64 69 63 61 74 65 64 2e 0a 20   if indicated.. 
337a6 20 2a 2f 0a 20 20 69 66 28 20 6e 41 63 74 75 61   */.  if( nActua
337a7 6c 3c 6e 20 29 7b 0a 20 20 20 20 6d 65 6d 6d 6f  l<n ){.    memmo
337a8 76 65 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  ve(pWriter->data
337a9 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
337aa 61 74 61 2b 6e 41 63 74 75 61 6c 2c 0a 20 20 20  ata+nActual,.   
337ab 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
337ac 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f  ->data.pData+iDo
337ad 63 6c 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20  clistData+n,.   
337ae 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
337af 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44  ->data.nData-(iD
337b0 6f 63 6c 69 73 74 44 61 74 61 2b 6e 29 29 3b 0a  oclistData+n));.
337b1 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
337b2 61 2e 6e 44 61 74 61 20 2d 3d 20 6e 2d 6e 41 63  a.nData -= n-nAc
337b3 74 75 61 6c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tual;.  }..  /* 
337b4 52 65 70 6c 61 63 65 20 77 72 69 74 74 65 6e 20  Replace written 
337b5 6c 65 6e 67 74 68 20 77 69 74 68 20 61 63 74 75  length with actu
337b6 61 6c 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a 20 20  al length. */.  
337b7 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e  memcpy(pWriter->
337b8 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c  data.pData+iDocl
337b9 69 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63 74  istData, c, nAct
337ba 75 61 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  ual);..  /* If t
337bb 68 65 20 6e 6f 64 65 20 69 73 20 74 6f 6f 20 6c  he node is too l
337bc 61 72 67 65 2c 20 62 72 65 61 6b 20 74 68 69 6e  arge, break thin
337bd 67 73 20 75 70 2e 20 2a 2f 0a 20 20 2f 2a 20 54  gs up. */.  /* T
337be 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
337bf 74 65 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61  test matches lea
337c0 66 57 72 69 74 65 72 53 74 65 70 28 29 2c 20 77  fWriterStep(), w
337c1 68 69 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20  hich does this. 
337c2 20 2a 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20   ** test before 
337c3 69 74 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73  it knows the cos
337c4 74 20 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f  t to varint-enco
337c5 64 65 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a  de the term and.
337c6 20 20 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e    ** doclist len
337c7 67 74 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70  gths.  At some p
337c8 6f 69 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a  oint, change to.
337c9 20 20 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61    ** pWriter->da
337ca 74 61 2e 6e 44 61 74 61 3e 4c 45 41 46 5f 4d 41  ta.nData>LEAF_MA
337cb 58 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 54  X..  */.  if( iT
337cc 65 72 6d 44 61 74 61 2b 6e 54 65 72 6d 2b 6e 41  ermData+nTerm+nA
337cd 63 74 75 61 6c 44 61 74 61 3e 4c 45 41 46 5f 4d  ctualData>LEAF_M
337ce 41 58 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6c 75  AX ){.    /* Flu
337cf 73 68 20 6f 75 74 20 74 68 65 20 6c 65 61 64 69  sh out the leadi
337d0 6e 67 20 64 61 74 61 20 61 73 20 61 20 6e 6f 64  ng data as a nod
337d1 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6c 65  e */.    rc = le
337d2 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c  afWriterInternal
337d3 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72  Flush(v, pWriter
337d4 2c 20 30 2c 20 69 54 65 72 6d 44 61 74 61 29 3b  , 0, iTermData);
337d5 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
337d6 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
337d7 72 63 3b 0a 0a 20 20 20 20 70 57 72 69 74 65 72  rc;..    pWriter
337d8 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20  ->nTermDistinct 
337d9 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b  = nTermDistinct;
337da 0a 0a 20 20 20 20 2f 2a 20 52 65 62 75 69 6c 64  ..    /* Rebuild
337db 20 68 65 61 64 65 72 20 75 73 69 6e 67 20 74 68   header using th
337dc 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 2a  e current term *
337dd 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75  /.    n = fts3Pu
337de 74 56 61 72 69 6e 74 28 70 57 72 69 74 65 72 2d  tVarint(pWriter-
337df 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 30 29 3b  >data.pData, 0);
337e0 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
337e1 74 56 61 72 69 6e 74 28 70 57 72 69 74 65 72 2d  tVarint(pWriter-
337e2 3e 64 61 74 61 2e 70 44 61 74 61 2b 6e 2c 20 6e  >data.pData+n, n
337e3 54 65 72 6d 29 3b 0a 20 20 20 20 6d 65 6d 63 70  Term);.    memcp
337e4 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  y(pWriter->data.
337e5 70 44 61 74 61 2b 6e 2c 20 70 54 65 72 6d 2c 20  pData+n, pTerm, 
337e6 6e 54 65 72 6d 29 3b 0a 20 20 20 20 6e 20 2b 3d  nTerm);.    n +=
337e7 20 6e 54 65 72 6d 3b 0a 0a 20 20 20 20 2f 2a 20   nTerm;..    /* 
337e8 54 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77  There should alw
337e9 61 79 73 20 62 65 20 72 6f 6f 6d 2c 20 62 65 63  ays be room, bec
337ea 61 75 73 65 20 74 68 65 20 70 72 65 76 69 6f 75  ause the previou
337eb 73 20 65 6e 63 6f 64 69 6e 67 0a 20 20 20 20 2a  s encoding.    *
337ec 2a 20 69 6e 63 6c 75 64 65 64 20 61 6c 6c 20 64  * included all d
337ed 61 74 61 20 6e 65 63 65 73 73 61 72 79 20 74 6f  ata necessary to
337ee 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 74   construct the t
337ef 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  erm..    */.    
337f0 61 73 73 65 72 74 28 20 6e 3c 69 44 6f 63 6c 69  assert( n<iDocli
337f1 73 74 44 61 74 61 20 29 3b 0a 20 20 20 20 2f 2a  stData );.    /*
337f2 20 53 6f 20 6c 6f 6e 67 20 61 73 20 53 54 41 4e   So long as STAN
337f3 44 41 4c 4f 4e 45 5f 4d 49 4e 20 69 73 20 68 61  DALONE_MIN is ha
337f4 6c 66 20 6f 72 20 6c 65 73 73 20 6f 66 20 4c 45  lf or less of LE
337f5 41 46 5f 4d 41 58 2c 20 74 68 65 0a 20 20 20 20  AF_MAX, the.    
337f6 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d  ** following mem
337f7 63 70 79 28 29 20 69 73 20 73 61 66 65 20 28 61  cpy() is safe (a
337f8 73 20 6f 70 70 6f 73 65 64 20 74 6f 20 6e 65 65  s opposed to nee
337f9 64 69 6e 67 20 61 20 6d 65 6d 6d 6f 76 65 29 2e  ding a memmove).
337fa 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
337fb 72 74 28 20 32 2a 53 54 41 4e 44 41 4c 4f 4e 45  rt( 2*STANDALONE
337fc 5f 4d 49 4e 3c 3d 4c 45 41 46 5f 4d 41 58 20 29  _MIN<=LEAF_MAX )
337fd 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b  ;.    assert( n+
337fe 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
337ff 61 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61  ata-iDoclistData
33800 3c 69 44 6f 63 6c 69 73 74 44 61 74 61 20 29 3b  <iDoclistData );
33801 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 72 69  .    memcpy(pWri
33802 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
33803 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 57  n,.           pW
33804 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
33805 61 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 0a  a+iDoclistData,.
33806 20 20 20 20 20 20 20 20 20 20 20 70 57 72 69 74             pWrit
33807 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69  er->data.nData-i
33808 44 6f 63 6c 69 73 74 44 61 74 61 29 3b 0a 20 20  DoclistData);.  
33809 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e    pWriter->data.
3380a 6e 44 61 74 61 20 2d 3d 20 69 44 6f 63 6c 69 73  nData -= iDoclis
3380b 74 44 61 74 61 2d 6e 3b 0a 20 20 7d 0a 20 20 41  tData-n;.  }.  A
3380c 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46  SSERT_VALID_LEAF
3380d 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d 3e 64  _NODE(pWriter->d
3380e 61 74 61 2e 70 44 61 74 61 2c 20 70 57 72 69 74  ata.pData, pWrit
3380f 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b  er->data.nData);
33810 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
33811 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 50 75 73 68  E_OK;.}../* Push
33812 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61 6c   pTerm[nTerm] al
33813 6f 6e 67 20 77 69 74 68 20 74 68 65 20 64 6f 63  ong with the doc
33814 6c 69 73 74 20 64 61 74 61 20 74 6f 20 74 68 65  list data to the
33815 20 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 0a 2a   leaf layer of.*
33816 2a 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f  * %_segments..*/
33817 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
33818 52 65 76 69 73 65 20 77 72 69 74 65 5a 65 72 6f  Revise writeZero
33819 53 65 67 6d 65 6e 74 28 29 20 73 6f 20 74 68 61  Segment() so tha
3381a 74 20 64 6f 63 6c 69 73 74 73 20 61 72 65 0a 2a  t doclists are.*
3381b 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 20 64 69  * constructed di
3381c 72 65 63 74 6c 79 20 69 6e 20 70 57 72 69 74 65  rectly in pWrite
3381d 72 2d 3e 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74  r->data..*/.stat
3381e 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65  ic int leafWrite
3381f 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76  rStep(fulltext_v
33820 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
33821 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
33822 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33823 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
33824 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
33825 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  erm,.           
33826 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
33827 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
33828 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20  , int nData){.  
33829 69 6e 74 20 72 63 3b 0a 20 20 44 4c 52 65 61 64  int rc;.  DLRead
3382a 65 72 20 72 65 61 64 65 72 3b 0a 0a 20 20 64 6c  er reader;..  dl
3382b 72 49 6e 69 74 28 26 72 65 61 64 65 72 2c 20 44  rInit(&reader, D
3382c 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61  L_DEFAULT, pData
3382d 2c 20 6e 44 61 74 61 29 3b 0a 20 20 72 63 20 3d  , nData);.  rc =
3382e 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d   leafWriterStepM
3382f 65 72 67 65 28 76 2c 20 70 57 72 69 74 65 72 2c  erge(v, pWriter,
33830 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 26   pTerm, nTerm, &
33831 72 65 61 64 65 72 2c 20 31 29 3b 0a 20 20 64 6c  reader, 1);.  dl
33832 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72  rDestroy(&reader
33833 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
33834 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}.../**********
33835 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33836 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33837 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33838 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66 52  ******/./* LeafR
33839 65 61 64 65 72 20 69 73 20 75 73 65 64 20 74 6f  eader is used to
3383a 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6e   iterate over an
3383b 20 69 6e 64 69 76 69 64 75 61 6c 20 6c 65 61 66   individual leaf
3383c 20 6e 6f 64 65 2e 20 2a 2f 0a 74 79 70 65 64 65   node. */.typede
3383d 66 20 73 74 72 75 63 74 20 4c 65 61 66 52 65 61  f struct LeafRea
3383e 64 65 72 20 7b 0a 20 20 44 61 74 61 42 75 66 66  der {.  DataBuff
3383f 65 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20  er term;        
33840 20 20 2f 2a 20 63 6f 70 79 20 6f 66 20 63 75 72    /* copy of cur
33841 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 0a 20  rent term. */.. 
33842 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
33843 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 64 61  ta;        /* da
33844 74 61 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74  ta for current t
33845 65 72 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  erm. */.  int nD
33846 61 74 61 3b 0a 7d 20 4c 65 61 66 52 65 61 64 65  ata;.} LeafReade
33847 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
33848 6c 65 61 66 52 65 61 64 65 72 44 65 73 74 72 6f  leafReaderDestro
33849 79 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  y(LeafReader *pR
3384a 65 61 64 65 72 29 7b 0a 20 20 64 61 74 61 42 75  eader){.  dataBu
3384b 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 52 65  fferDestroy(&pRe
3384c 61 64 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53  ader->term);.  S
3384d 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29  CRAMBLE(pReader)
3384e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
3384f 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28  leafReaderAtEnd(
33850 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
33851 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  der){.  return p
33852 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3c 3d 30  Reader->nData<=0
33853 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74  ;.}../* Access t
33854 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e  he current term.
33855 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c   */.static int l
33856 65 61 66 52 65 61 64 65 72 54 65 72 6d 42 79 74  eafReaderTermByt
33857 65 73 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70  es(LeafReader *p
33858 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
33859 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
3385a 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20  nData;.}.static 
3385b 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65 61 66  const char *leaf
3385c 52 65 61 64 65 72 54 65 72 6d 28 4c 65 61 66 52  ReaderTerm(LeafR
3385d 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
3385e 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64  .  assert( pRead
3385f 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30  er->term.nData>0
33860 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
33861 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61  ader->term.pData
33862 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74  ;.}../* Access t
33863 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  he doclist data 
33864 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
33865 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20  term. */.static 
33866 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 44 61  int leafReaderDa
33867 74 61 42 79 74 65 73 28 4c 65 61 66 52 65 61 64  taBytes(LeafRead
33868 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
33869 69 6e 74 20 6e 44 61 74 61 3b 0a 20 20 61 73 73  int nData;.  ass
3386a 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 74 65  ert( pReader->te
3386b 72 6d 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20  rm.nData>0 );.  
3386c 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
3386d 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
3386e 26 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75 72  &nData);.  retur
3386f 6e 20 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69  n nData;.}.stati
33870 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65  c const char *le
33871 61 66 52 65 61 64 65 72 44 61 74 61 28 4c 65 61  afReaderData(Lea
33872 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
33873 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e 44 61 74  ){.  int n, nDat
33874 61 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 65  a;.  assert( pRe
33875 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
33876 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 66 74 73 33  >0 );.  n = fts3
33877 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
33878 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44 61  der->pData, &nDa
33879 74 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  ta);.  return pR
3387a 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b 0a  eader->pData+n;.
3387b 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  }..static void l
3387c 65 61 66 52 65 61 64 65 72 49 6e 69 74 28 63 6f  eafReaderInit(co
3387d 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
3387e 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20   int nData,.    
3387f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33880 20 20 20 20 20 20 20 4c 65 61 66 52 65 61 64 65         LeafReade
33881 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69  r *pReader){.  i
33882 6e 74 20 6e 54 65 72 6d 2c 20 6e 3b 0a 0a 20 20  nt nTerm, n;..  
33883 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20  assert( nData>0 
33884 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  );.  assert( pDa
33885 74 61 5b 30 5d 3d 3d 27 5c 30 27 20 29 3b 0a 0a  ta[0]=='\0' );..
33886 20 20 43 4c 45 41 52 28 70 52 65 61 64 65 72 29    CLEAR(pReader)
33887 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ;..  /* Read the
33888 20 66 69 72 73 74 20 74 65 72 6d 2c 20 73 6b 69   first term, ski
33889 70 70 69 6e 67 20 74 68 65 20 68 65 61 64 65 72  pping the header
3388a 20 62 79 74 65 2e 20 2a 2f 0a 20 20 6e 20 3d 20   byte. */.  n = 
3388b 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
3388c 70 44 61 74 61 2b 31 2c 20 26 6e 54 65 72 6d 29  pData+1, &nTerm)
3388d 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ;.  dataBufferIn
3388e 69 74 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72  it(&pReader->ter
3388f 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 64 61 74  m, nTerm);.  dat
33890 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26  aBufferReplace(&
33891 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70  pReader->term, p
33892 44 61 74 61 2b 31 2b 6e 2c 20 6e 54 65 72 6d 29  Data+1+n, nTerm)
33893 3b 0a 0a 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e  ;..  /* Position
33894 20 61 66 74 65 72 20 74 68 65 20 66 69 72 73 74   after the first
33895 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 61 73 73 65   term. */.  asse
33896 72 74 28 20 31 2b 6e 2b 6e 54 65 72 6d 3c 6e 44  rt( 1+n+nTerm<nD
33897 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72  ata );.  pReader
33898 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2b  ->pData = pData+
33899 31 2b 6e 2b 6e 54 65 72 6d 3b 0a 20 20 70 52 65  1+n+nTerm;.  pRe
3389a 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44  ader->nData = nD
3389b 61 74 61 2d 31 2d 6e 2d 6e 54 65 72 6d 3b 0a 7d  ata-1-n-nTerm;.}
3389c 0a 0a 2f 2a 20 53 74 65 70 20 74 68 65 20 72 65  ../* Step the re
3389d 61 64 65 72 20 66 6f 72 77 61 72 64 20 74 6f 20  ader forward to 
3389e 74 68 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a  the next term. *
3389f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
338a0 61 66 52 65 61 64 65 72 53 74 65 70 28 4c 65 61  afReaderStep(Lea
338a1 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
338a2 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e 44 61 74  ){.  int n, nDat
338a3 61 2c 20 6e 50 72 65 66 69 78 2c 20 6e 53 75 66  a, nPrefix, nSuf
338a4 66 69 78 3b 0a 20 20 61 73 73 65 72 74 28 20 21  fix;.  assert( !
338a5 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28  leafReaderAtEnd(
338a6 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20 20 2f  pReader) );..  /
338a7 2a 20 53 6b 69 70 20 70 72 65 76 69 6f 75 73 20  * Skip previous 
338a8 65 6e 74 72 79 27 73 20 64 61 74 61 20 62 6c 6f  entry's data blo
338a9 63 6b 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73  ck. */.  n = fts
338aa 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
338ab 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44  ader->pData, &nD
338ac 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ata);.  assert( 
338ad 6e 2b 6e 44 61 74 61 3c 3d 70 52 65 61 64 65 72  n+nData<=pReader
338ae 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65  ->nData );.  pRe
338af 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e  ader->pData += n
338b0 2b 6e 44 61 74 61 3b 0a 20 20 70 52 65 61 64 65  +nData;.  pReade
338b1 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 44  r->nData -= n+nD
338b2 61 74 61 3b 0a 0a 20 20 69 66 28 20 21 6c 65 61  ata;..  if( !lea
338b3 66 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65  fReaderAtEnd(pRe
338b4 61 64 65 72 29 20 29 7b 0a 20 20 20 20 2f 2a 20  ader) ){.    /* 
338b5 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 6e 65  Construct the ne
338b6 77 20 74 65 72 6d 20 75 73 69 6e 67 20 61 20 70  w term using a p
338b7 72 65 66 69 78 20 66 72 6f 6d 20 74 68 65 20 6f  refix from the o
338b8 6c 64 20 74 65 72 6d 20 70 6c 75 73 20 61 0a 20  ld term plus a. 
338b9 20 20 20 2a 2a 20 73 75 66 66 69 78 20 66 72 6f     ** suffix fro
338ba 6d 20 74 68 65 20 6c 65 61 66 20 64 61 74 61 2e  m the leaf data.
338bb 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20  .    */.    n = 
338bc 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
338bd 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
338be 26 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e  &nPrefix);.    n
338bf 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
338c0 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
338c1 74 61 2b 6e 2c 20 26 6e 53 75 66 66 69 78 29 3b  ta+n, &nSuffix);
338c2 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 6e  .    assert( n+n
338c3 53 75 66 66 69 78 3c 70 52 65 61 64 65 72 2d 3e  Suffix<pReader->
338c4 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 52 65  nData );.    pRe
338c5 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
338c6 20 3d 20 6e 50 72 65 66 69 78 3b 0a 20 20 20 20   = nPrefix;.    
338c7 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
338c8 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c  (&pReader->term,
338c9 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b   pReader->pData+
338ca 6e 2c 20 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20  n, nSuffix);..  
338cb 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
338cc 20 2b 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a 20   += n+nSuffix;. 
338cd 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74     pReader->nDat
338ce 61 20 2d 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a  a -= n+nSuffix;.
338cf 20 20 7d 0a 7d 0a 0a 2f 2a 20 73 74 72 63 6d 70    }.}../* strcmp
338d0 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f  -style compariso
338d1 6e 20 6f 66 20 70 52 65 61 64 65 72 27 73 20 63  n of pReader's c
338d2 75 72 72 65 6e 74 20 74 65 72 6d 20 61 67 61 69  urrent term agai
338d3 6e 73 74 20 70 54 65 72 6d 2e 0a 2a 2a 20 49 66  nst pTerm..** If
338d4 20 69 73 50 72 65 66 69 78 2c 20 65 71 75 61 6c   isPrefix, equal
338d5 69 74 79 20 6d 65 61 6e 73 20 65 71 75 61 6c 20  ity means equal 
338d6 74 68 72 6f 75 67 68 20 6e 54 65 72 6d 20 62 79  through nTerm by
338d7 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tes..*/.static i
338d8 6e 74 20 6c 65 61 66 52 65 61 64 65 72 54 65 72  nt leafReaderTer
338d9 6d 43 6d 70 28 4c 65 61 66 52 65 61 64 65 72 20  mCmp(LeafReader 
338da 2a 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20  *pReader,.      
338db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
338dc 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
338dd 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
338de 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
338df 78 29 7b 0a 20 20 69 6e 74 20 63 2c 20 6e 20 3d  x){.  int c, n =
338e0 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e   pReader->term.n
338e1 44 61 74 61 3c 6e 54 65 72 6d 20 3f 20 70 52 65  Data<nTerm ? pRe
338e2 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
338e3 20 3a 20 6e 54 65 72 6d 3b 0a 20 20 69 66 28 20   : nTerm;.  if( 
338e4 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  n==0 ){.    if( 
338e5 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
338e6 61 74 61 3e 30 20 29 20 72 65 74 75 72 6e 20 2d  ata>0 ) return -
338e7 31 3b 0a 20 20 20 20 69 66 28 6e 54 65 72 6d 3e  1;.    if(nTerm>
338e8 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
338e9 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
338ea 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 70 52  .  c = memcmp(pR
338eb 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74  eader->term.pDat
338ec 61 2c 20 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20  a, pTerm, n);.  
338ed 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72  if( c!=0 ) retur
338ee 6e 20 63 3b 0a 20 20 69 66 28 20 69 73 50 72 65  n c;.  if( isPre
338ef 66 69 78 20 26 26 20 6e 3d 3d 6e 54 65 72 6d 20  fix && n==nTerm 
338f0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65  ) return 0;.  re
338f1 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65  turn pReader->te
338f2 72 6d 2e 6e 44 61 74 61 20 2d 20 6e 54 65 72 6d  rm.nData - nTerm
338f3 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}.../*********
338f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338f7 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 76  *******/./* Leav
338f8 65 73 52 65 61 64 65 72 20 77 72 61 70 73 20 4c  esReader wraps L
338f9 65 61 66 52 65 61 64 65 72 20 74 6f 20 61 6c 6c  eafReader to all
338fa 6f 77 20 69 74 65 72 61 74 69 6e 67 20 6f 76 65  ow iterating ove
338fb 72 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20  r the entire.** 
338fc 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 20 74 68  leaf layer of th
338fd 65 20 74 72 65 65 2e 0a 2a 2f 0a 74 79 70 65 64  e tree..*/.typed
338fe 65 66 20 73 74 72 75 63 74 20 4c 65 61 76 65 73  ef struct Leaves
338ff 52 65 61 64 65 72 20 7b 0a 20 20 69 6e 74 20 69  Reader {.  int i
33900 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
33901 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 69       /* Index wi
33902 74 68 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  thin the segment
33903 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 33 5f  . */..  sqlite3_
33904 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20  stmt *pStmt;    
33905 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 77    /* Statement w
33906 65 27 72 65 20 73 74 72 65 61 6d 69 6e 67 20 6c  e're streaming l
33907 65 61 76 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20  eaves from. */. 
33908 20 69 6e 74 20 65 6f 66 3b 20 20 20 20 20 20 20   int eof;       
33909 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 77 65             /* we
3390a 27 76 65 20 73 65 65 6e 20 53 51 4c 49 54 45 5f  've seen SQLITE_
3390b 44 4f 4e 45 20 66 72 6f 6d 20 70 53 74 6d 74 2e  DONE from pStmt.
3390c 20 2a 2f 0a 0a 20 20 4c 65 61 66 52 65 61 64 65   */..  LeafReade
3390d 72 20 6c 65 61 66 52 65 61 64 65 72 3b 20 20 20  r leafReader;   
3390e 20 2f 2a 20 72 65 61 64 65 72 20 66 6f 72 20 74   /* reader for t
3390f 68 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 2e  he current leaf.
33910 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
33911 20 72 6f 6f 74 44 61 74 61 3b 20 20 20 20 20 20   rootData;      
33912 2f 2a 20 72 6f 6f 74 20 64 61 74 61 20 66 6f 72  /* root data for
33913 20 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 7d 20 4c 65   inline. */.} Le
33914 61 76 65 73 52 65 61 64 65 72 3b 0a 0a 2f 2a 20  avesReader;../* 
33915 41 63 63 65 73 73 20 74 68 65 20 63 75 72 72 65  Access the curre
33916 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74  nt term. */.stat
33917 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
33918 64 65 72 54 65 72 6d 42 79 74 65 73 28 4c 65 61  derTermBytes(Lea
33919 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
3391a 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
3391b 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a  pReader->eof );.
3391c 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61    return leafRea
3391d 64 65 72 54 65 72 6d 42 79 74 65 73 28 26 70 52  derTermBytes(&pR
3391e 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65  eader->leafReade
3391f 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e  r);.}.static con
33920 73 74 20 63 68 61 72 20 2a 6c 65 61 76 65 73 52  st char *leavesR
33921 65 61 64 65 72 54 65 72 6d 28 4c 65 61 76 65 73  eaderTerm(Leaves
33922 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
33923 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 52 65  {.  assert( !pRe
33924 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72  ader->eof );.  r
33925 65 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72  eturn leafReader
33926 54 65 72 6d 28 26 70 52 65 61 64 65 72 2d 3e 6c  Term(&pReader->l
33927 65 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f  eafReader);.}../
33928 2a 20 41 63 63 65 73 73 20 74 68 65 20 64 6f 63  * Access the doc
33929 6c 69 73 74 20 64 61 74 61 20 66 6f 72 20 74 68  list data for th
3392a 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20  e current term. 
3392b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  */.static int le
3392c 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
3392d 74 65 73 28 4c 65 61 76 65 73 52 65 61 64 65 72  tes(LeavesReader
3392e 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
3392f 73 65 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e  sert( !pReader->
33930 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  eof );.  return 
33931 6c 65 61 66 52 65 61 64 65 72 44 61 74 61 42 79  leafReaderDataBy
33932 74 65 73 28 26 70 52 65 61 64 65 72 2d 3e 6c 65  tes(&pReader->le
33933 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61  afReader);.}.sta
33934 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
33935 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61  leavesReaderData
33936 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70  (LeavesReader *p
33937 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
33938 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66  t( !pReader->eof
33939 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61   );.  return lea
3393a 66 52 65 61 64 65 72 44 61 74 61 28 26 70 52 65  fReaderData(&pRe
3393b 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
3393c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  );.}..static int
3393d 20 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45   leavesReaderAtE
3393e 6e 64 28 4c 65 61 76 65 73 52 65 61 64 65 72 20  nd(LeavesReader 
3393f 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
33940 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 65 6f 66  urn pReader->eof
33941 3b 0a 7d 0a 0a 2f 2a 20 6c 6f 61 64 53 65 67 6d  ;.}../* loadSegm
33942 65 6e 74 4c 65 61 76 65 73 28 29 20 6d 61 79 20  entLeaves() may 
33943 6e 6f 74 20 72 65 61 64 20 61 6c 6c 20 74 68 65  not read all the
33944 20 77 61 79 20 74 6f 20 53 51 4c 49 54 45 5f 44   way to SQLITE_D
33945 4f 4e 45 2c 20 74 68 75 73 0a 2a 2a 20 6c 65 61  ONE, thus.** lea
33946 76 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65  ving the stateme
33947 6e 74 20 68 61 6e 64 6c 65 20 6f 70 65 6e 2c 20  nt handle open, 
33948 77 68 69 63 68 20 6c 6f 63 6b 73 20 74 68 65 20  which locks the 
33949 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  table..*/./* TOD
3394a 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 22 73  O(shess) This "s
3394b 6f 6c 75 74 69 6f 6e 22 20 69 73 20 6e 6f 74 20  olution" is not 
3394c 73 61 74 69 73 66 61 63 74 6f 72 79 2e 20 20 52  satisfactory.  R
3394d 65 61 6c 6c 79 2c 20 74 68 65 72 65 0a 2a 2a 20  eally, there.** 
3394e 73 68 6f 75 6c 64 20 62 65 20 63 68 65 63 6b 2d  should be check-
3394f 69 6e 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  in function for 
33950 61 6c 6c 20 73 74 61 74 65 6d 65 6e 74 20 68 61  all statement ha
33951 6e 64 6c 65 73 20 77 68 69 63 68 0a 2a 2a 20 61  ndles which.** a
33952 72 72 61 6e 67 65 73 20 74 6f 20 63 61 6c 6c 20  rranges to call 
33953 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2e  sqlite3_reset().
33954 20 20 54 68 69 73 20 6d 6f 73 74 20 6c 69 6b 65    This most like
33955 6c 79 20 77 69 6c 6c 20 72 65 71 75 69 72 65 0a  ly will require.
33956 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20  ** modification 
33957 74 6f 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 20  to control flow 
33958 61 6c 6c 20 6f 76 65 72 20 74 68 65 20 70 6c 61  all over the pla
33959 63 65 2c 20 74 68 6f 75 67 68 2c 20 73 6f 20 66  ce, though, so f
3395a 6f 72 20 6e 6f 77 0a 2a 2a 20 6a 75 73 74 20 70  or now.** just p
3395b 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  unt..**.** Note 
3395c 74 68 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  the the current 
3395d 73 79 73 74 65 6d 20 61 73 73 75 6d 65 73 20 74  system assumes t
3395e 68 61 74 20 73 65 67 6d 65 6e 74 20 6d 65 72 67  hat segment merg
3395f 65 73 20 77 69 6c 6c 20 72 75 6e 20 74 6f 0a 2a  es will run to.*
33960 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 2c 20 77 68  * completion, wh
33961 69 63 68 20 69 73 20 77 68 79 20 74 68 69 73 20  ich is why this 
33962 70 61 72 74 69 63 75 6c 61 72 20 70 72 6f 62 61  particular proba
33963 62 6c 79 20 68 61 73 6e 27 74 20 61 72 69 73 65  bly hasn't arise
33964 6e 20 69 6e 0a 2a 2a 20 74 68 69 73 20 63 61 73  n in.** this cas
33965 65 2e 20 20 50 72 6f 62 61 62 6c 79 20 61 20 62  e.  Probably a b
33966 72 69 74 74 6c 65 20 61 73 73 75 6d 70 74 69 6f  rittle assumptio
33967 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
33968 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73   leavesReaderRes
33969 65 74 28 4c 65 61 76 65 73 52 65 61 64 65 72 20  et(LeavesReader 
3396a 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
3396b 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65  urn sqlite3_rese
3396c 74 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  t(pReader->pStmt
3396d 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
3396e 64 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65  d leavesReaderDe
3396f 73 74 72 6f 79 28 4c 65 61 76 65 73 52 65 61 64  stroy(LeavesRead
33970 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
33971 2f 2a 20 49 66 20 69 64 78 20 69 73 20 2d 31 2c  /* If idx is -1,
33972 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 27 72   that means we'r
33973 65 20 75 73 69 6e 67 20 61 20 6e 6f 6e 2d 63 61  e using a non-ca
33974 63 68 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 20  ched statement. 
33975 20 2a 2a 20 68 61 6e 64 6c 65 20 69 6e 20 74 68   ** handle in th
33976 65 20 6f 70 74 69 6d 69 7a 65 28 29 20 63 61 73  e optimize() cas
33977 65 2c 20 73 6f 20 77 65 20 6e 65 65 64 20 74 6f  e, so we need to
33978 20 72 65 6c 65 61 73 65 20 69 74 2e 0a 20 20 2a   release it..  *
33979 2f 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d  /.  if( pReader-
3397a 3e 70 53 74 6d 74 21 3d 4e 55 4c 4c 20 26 26 20  >pStmt!=NULL && 
3397b 70 52 65 61 64 65 72 2d 3e 69 64 78 3d 3d 2d 31  pReader->idx==-1
3397c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
3397d 66 69 6e 61 6c 69 7a 65 28 70 52 65 61 64 65 72  finalize(pReader
3397e 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20  ->pStmt);.  }.  
3397f 6c 65 61 66 52 65 61 64 65 72 44 65 73 74 72 6f  leafReaderDestro
33980 79 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  y(&pReader->leaf
33981 52 65 61 64 65 72 29 3b 0a 20 20 64 61 74 61 42  Reader);.  dataB
33982 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 52  ufferDestroy(&pR
33983 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 29  eader->rootData)
33984 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  ;.  SCRAMBLE(pRe
33985 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69  ader);.}../* Ini
33986 74 69 61 6c 69 7a 65 20 70 52 65 61 64 65 72 20  tialize pReader 
33987 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 72  with the given r
33988 6f 6f 74 20 64 61 74 61 20 28 69 66 20 69 53 74  oot data (if iSt
33989 61 72 74 42 6c 6f 63 6b 69 64 3d 3d 30 0a 2a 2a  artBlockid==0.**
3398a 20 74 68 65 20 6c 65 61 66 20 64 61 74 61 20 77   the leaf data w
3398b 61 73 20 65 6e 74 69 72 65 6c 79 20 63 6f 6e 74  as entirely cont
3398c 61 69 6e 65 64 20 69 6e 20 74 68 65 20 72 6f 6f  ained in the roo
3398d 74 29 2c 20 6f 72 20 66 72 6f 6d 20 74 68 65 0a  t), or from the.
3398e 2a 2a 20 73 74 72 65 61 6d 20 6f 66 20 62 6c 6f  ** stream of blo
3398f 63 6b 73 20 62 65 74 77 65 65 6e 20 69 53 74 61  cks between iSta
33990 72 74 42 6c 6f 63 6b 69 64 20 61 6e 64 20 69 45  rtBlockid and iE
33991 6e 64 42 6c 6f 63 6b 69 64 2c 20 69 6e 63 6c 75  ndBlockid, inclu
33992 73 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sive..*/.static 
33993 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72  int leavesReader
33994 49 6e 69 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  Init(fulltext_vt
33995 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20  ab *v,.         
33996 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33997 20 20 20 69 6e 74 20 69 64 78 2c 0a 20 20 20 20     int idx,.    
33998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33999 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
3399a 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b  nt64 iStartBlock
3399b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  id,.            
3399c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3399d 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
3399e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20  dBlockid,.      
3399f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
339a0 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
339a1 20 2a 70 52 6f 6f 74 44 61 74 61 2c 20 69 6e 74   *pRootData, int
339a2 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20   nRootData,.    
339a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
339a4 20 20 20 20 20 20 20 20 4c 65 61 76 65 73 52 65          LeavesRe
339a5 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
339a6 20 20 43 4c 45 41 52 28 70 52 65 61 64 65 72 29    CLEAR(pReader)
339a7 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 64 78  ;.  pReader->idx
339a8 20 3d 20 69 64 78 3b 0a 0a 20 20 64 61 74 61 42   = idx;..  dataB
339a9 75 66 66 65 72 49 6e 69 74 28 26 70 52 65 61 64  ufferInit(&pRead
339aa 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2c 20 30 29  er->rootData, 0)
339ab 3b 0a 20 20 69 66 28 20 69 53 74 61 72 74 42 6c  ;.  if( iStartBl
339ac 6f 63 6b 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20  ockid==0 ){.    
339ad 2f 2a 20 45 6e 74 69 72 65 20 6c 65 61 66 20 6c  /* Entire leaf l
339ae 65 76 65 6c 20 66 69 74 20 69 6e 20 72 6f 6f 74  evel fit in root
339af 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 64 61   data. */.    da
339b0 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
339b1 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61  &pReader->rootDa
339b2 74 61 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e  ta, pRootData, n
339b3 52 6f 6f 74 44 61 74 61 29 3b 0a 20 20 20 20 6c  RootData);.    l
339b4 65 61 66 52 65 61 64 65 72 49 6e 69 74 28 70 52  eafReaderInit(pR
339b5 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e  eader->rootData.
339b6 70 44 61 74 61 2c 20 70 52 65 61 64 65 72 2d 3e  pData, pReader->
339b7 72 6f 6f 74 44 61 74 61 2e 6e 44 61 74 61 2c 0a  rootData.nData,.
339b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
339b9 20 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65 61     &pReader->lea
339ba 66 52 65 61 64 65 72 29 3b 0a 20 20 7d 65 6c 73  fReader);.  }els
339bb 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  e{.    sqlite3_s
339bc 74 6d 74 20 2a 73 3b 0a 20 20 20 20 69 6e 74 20  tmt *s;.    int 
339bd 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 6c 65 61  rc = sql_get_lea
339be 66 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 69  f_statement(v, i
339bf 64 78 2c 20 26 73 29 3b 0a 20 20 20 20 69 66 28  dx, &s);.    if(
339c0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
339c1 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
339c2 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
339c3 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69  nd_int64(s, 1, i
339c4 53 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20  StartBlockid);. 
339c5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
339c6 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
339c7 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
339c8 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
339c9 2c 20 32 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64  , 2, iEndBlockid
339ca 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
339cb 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
339cc 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20  n rc;..    rc = 
339cd 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
339ce 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
339cf 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
339d0 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d    pReader->eof =
339d1 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
339d2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
339d3 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  }.    if( rc!=SQ
339d4 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
339d5 6e 20 72 63 3b 0a 0a 20 20 20 20 70 52 65 61 64  n rc;..    pRead
339d6 65 72 2d 3e 70 53 74 6d 74 20 3d 20 73 3b 0a 20  er->pStmt = s;. 
339d7 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69     leafReaderIni
339d8 74 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t(sqlite3_column
339d9 5f 62 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70  _blob(pReader->p
339da 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20  Stmt, 0),.      
339db 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
339dc 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
339dd 73 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  s(pReader->pStmt
339de 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0),.          
339df 20 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65           &pReade
339e0 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
339e1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
339e2 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 53 74  ITE_OK;.}../* St
339e3 65 70 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  ep the current l
339e4 65 61 66 20 66 6f 72 77 61 72 64 20 74 6f 20 74  eaf forward to t
339e5 68 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 20 49  he next term.  I
339e6 66 20 77 65 20 72 65 61 63 68 20 74 68 65 0a 2a  f we reach the.*
339e7 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 63 75 72  * end of the cur
339e8 72 65 6e 74 20 6c 65 61 66 2c 20 73 74 65 70 20  rent leaf, step 
339e9 66 6f 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e  forward to the n
339ea 65 78 74 20 6c 65 61 66 20 62 6c 6f 63 6b 2e 0a  ext leaf block..
339eb 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  */.static int le
339ec 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28 66  avesReaderStep(f
339ed 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
339ee 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70   LeavesReader *p
339ef 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
339f0 74 28 20 21 6c 65 61 76 65 73 52 65 61 64 65 72  t( !leavesReader
339f1 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29  AtEnd(pReader) )
339f2 3b 0a 20 20 6c 65 61 66 52 65 61 64 65 72 53 74  ;.  leafReaderSt
339f3 65 70 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61  ep(&pReader->lea
339f4 66 52 65 61 64 65 72 29 3b 0a 0a 20 20 69 66 28  fReader);..  if(
339f5 20 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64   leafReaderAtEnd
339f6 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
339f7 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 69 6e  eader) ){.    in
339f8 74 20 72 63 3b 0a 20 20 20 20 69 66 28 20 70 52  t rc;.    if( pR
339f9 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e  eader->rootData.
339fa 70 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 70  pData ){.      p
339fb 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d 20 31 3b  Reader->eof = 1;
339fc 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
339fd 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
339fe 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
339ff 73 74 65 70 28 70 52 65 61 64 65 72 2d 3e 70 53  step(pReader->pS
33a00 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  tmt);.    if( rc
33a01 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  !=SQLITE_ROW ){.
33a02 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65        pReader->e
33a03 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65  of = 1;.      re
33a04 74 75 72 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f  turn rc==SQLITE_
33a05 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  DONE ? SQLITE_OK
33a06 20 3a 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20   : rc;.    }.   
33a07 20 6c 65 61 66 52 65 61 64 65 72 44 65 73 74 72   leafReaderDestr
33a08 6f 79 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61  oy(&pReader->lea
33a09 66 52 65 61 64 65 72 29 3b 0a 20 20 20 20 6c 65  fReader);.    le
33a0a 61 66 52 65 61 64 65 72 49 6e 69 74 28 73 71 6c  afReaderInit(sql
33a0b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
33a0c 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c  (pReader->pStmt,
33a0d 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0),.           
33a0e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
33a0f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 52 65  column_bytes(pRe
33a10 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c  ader->pStmt, 0),
33a11 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33a12 20 20 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65      &pReader->le
33a13 61 66 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  afReader);.  }. 
33a14 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
33a15 4b 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 4c  K;.}../* Order L
33a16 65 61 76 65 73 52 65 61 64 65 72 73 20 62 79 20  eavesReaders by 
33a17 74 68 65 69 72 20 74 65 72 6d 2c 20 69 67 6e 6f  their term, igno
33a18 72 69 6e 67 20 69 64 78 2e 20 20 52 65 61 64 65  ring idx.  Reade
33a19 72 73 20 61 74 20 65 6f 66 0a 2a 2a 20 61 6c 77  rs at eof.** alw
33a1a 61 79 73 20 73 6f 72 74 20 74 6f 20 74 68 65 20  ays sort to the 
33a1b 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  end..*/.static i
33a1c 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  nt leavesReaderT
33a1d 65 72 6d 43 6d 70 28 4c 65 61 76 65 73 52 65 61  ermCmp(LeavesRea
33a1e 64 65 72 20 2a 6c 72 31 2c 20 4c 65 61 76 65 73  der *lr1, Leaves
33a1f 52 65 61 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20  Reader *lr2){.  
33a20 69 66 28 20 6c 65 61 76 65 73 52 65 61 64 65 72  if( leavesReader
33a21 41 74 45 6e 64 28 6c 72 31 29 20 29 7b 0a 20 20  AtEnd(lr1) ){.  
33a22 20 20 69 66 28 20 6c 65 61 76 65 73 52 65 61 64    if( leavesRead
33a23 65 72 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72  erAtEnd(lr2) ) r
33a24 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 72 65 74  eturn 0;.    ret
33a25 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 1;.  }.  if(
33a26 20 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45   leavesReaderAtE
33a27 6e 64 28 6c 72 32 29 20 29 20 72 65 74 75 72 6e  nd(lr2) ) return
33a28 20 2d 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6c   -1;..  return l
33a29 65 61 66 52 65 61 64 65 72 54 65 72 6d 43 6d 70  eafReaderTermCmp
33a2a 28 26 6c 72 31 2d 3e 6c 65 61 66 52 65 61 64 65  (&lr1->leafReade
33a2b 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
33a2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 65                le
33a2d 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 6c  avesReaderTerm(l
33a2e 72 32 29 2c 20 6c 65 61 76 65 73 52 65 61 64 65  r2), leavesReade
33a2f 72 54 65 72 6d 42 79 74 65 73 28 6c 72 32 29 2c  rTermBytes(lr2),
33a30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33a31 20 20 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a              0);.
33a32 7d 0a 0a 2f 2a 20 53 69 6d 69 6c 61 72 20 74 6f  }../* Similar to
33a33 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
33a34 6d 43 6d 70 28 29 2c 20 77 69 74 68 20 61 64 64  mCmp(), with add
33a35 69 74 69 6f 6e 61 6c 20 6f 72 64 65 72 69 6e 67  itional ordering
33a36 20 62 79 20 69 64 78 0a 2a 2a 20 73 6f 20 74 68   by idx.** so th
33a37 61 74 20 6f 6c 64 65 72 20 73 65 67 6d 65 6e 74  at older segment
33a38 73 20 73 6f 72 74 20 62 65 66 6f 72 65 20 6e 65  s sort before ne
33a39 77 65 72 20 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f  wer segments..*/
33a3a 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
33a3b 65 73 52 65 61 64 65 72 43 6d 70 28 4c 65 61 76  esReaderCmp(Leav
33a3c 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c  esReader *lr1, L
33a3d 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32  eavesReader *lr2
33a3e 29 7b 0a 20 20 69 6e 74 20 63 20 3d 20 6c 65 61  ){.  int c = lea
33a3f 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
33a40 28 6c 72 31 2c 20 6c 72 32 29 3b 0a 20 20 69 66  (lr1, lr2);.  if
33a41 28 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20  ( c!=0 ) return 
33a42 63 3b 0a 20 20 72 65 74 75 72 6e 20 6c 72 31 2d  c;.  return lr1-
33a43 3e 69 64 78 2d 6c 72 32 2d 3e 69 64 78 3b 0a 7d  >idx-lr2->idx;.}
33a44 0a 0a 2f 2a 20 41 73 73 75 6d 65 20 74 68 61 74  ../* Assume that
33a45 20 70 4c 72 5b 31 5d 2e 2e 70 4c 72 5b 6e 4c 72   pLr[1]..pLr[nLr
33a46 5d 20 61 72 65 20 73 6f 72 74 65 64 2e 20 20 42  ] are sorted.  B
33a47 75 62 62 6c 65 20 70 4c 72 5b 30 5d 20 69 6e 74  ubble pLr[0] int
33a48 6f 20 69 74 73 0a 2a 2a 20 73 6f 72 74 65 64 20  o its.** sorted 
33a49 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  position..*/.sta
33a4a 74 69 63 20 76 6f 69 64 20 6c 65 61 76 65 73 52  tic void leavesR
33a4b 65 61 64 65 72 52 65 6f 72 64 65 72 28 4c 65 61  eaderReorder(Lea
33a4c 76 65 73 52 65 61 64 65 72 20 2a 70 4c 72 2c 20  vesReader *pLr, 
33a4d 69 6e 74 20 6e 4c 72 29 7b 0a 20 20 77 68 69 6c  int nLr){.  whil
33a4e 65 28 20 6e 4c 72 3e 31 20 26 26 20 6c 65 61 76  e( nLr>1 && leav
33a4f 65 73 52 65 61 64 65 72 43 6d 70 28 70 4c 72 2c  esReaderCmp(pLr,
33a50 20 70 4c 72 2b 31 29 3e 30 20 29 7b 0a 20 20 20   pLr+1)>0 ){.   
33a51 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 74 6d   LeavesReader tm
33a52 70 20 3d 20 70 4c 72 5b 30 5d 3b 0a 20 20 20 20  p = pLr[0];.    
33a53 70 4c 72 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d 3b  pLr[0] = pLr[1];
33a54 0a 20 20 20 20 70 4c 72 5b 31 5d 20 3d 20 74 6d  .    pLr[1] = tm
33a55 70 3b 0a 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20 20  p;.    nLr--;.  
33a56 20 20 70 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a    pLr++;.  }.}..
33a57 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 73 20 70  /* Initializes p
33a58 52 65 61 64 65 72 73 20 77 69 74 68 20 74 68 65  Readers with the
33a59 20 73 65 67 6d 65 6e 74 73 20 66 72 6f 6d 20 6c   segments from l
33a5a 65 76 65 6c 20 69 4c 65 76 65 6c 2c 20 72 65 74  evel iLevel, ret
33a5b 75 72 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 6e 75  urning.** the nu
33a5c 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73  mber of segments
33a5d 20 69 6e 20 2a 70 69 52 65 61 64 65 72 73 2e 20   in *piReaders. 
33a5e 20 4c 65 61 76 65 73 20 70 52 65 61 64 65 72 73   Leaves pReaders
33a5f 20 69 6e 20 73 6f 72 74 65 64 0a 2a 2a 20 6f 72   in sorted.** or
33a60 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  der..*/.static i
33a61 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 73  nt leavesReaders
33a62 49 6e 69 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  Init(fulltext_vt
33a63 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65  ab *v, int iLeve
33a64 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
33a65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a66 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
33a67 65 61 64 65 72 73 2c 20 69 6e 74 20 2a 70 69 52  eaders, int *piR
33a68 65 61 64 65 72 73 29 7b 0a 20 20 73 71 6c 69 74  eaders){.  sqlit
33a69 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e  e3_stmt *s;.  in
33a6a 74 20 69 2c 20 72 63 20 3d 20 73 71 6c 5f 67 65  t i, rc = sql_ge
33a6b 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53  t_statement(v, S
33a6c 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 4c 45 56  EGDIR_SELECT_LEV
33a6d 45 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  EL_STMT, &s);.  
33a6e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
33a6f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
33a70 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
33a71 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c  ind_int(s, 1, iL
33a72 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21  evel);.  if( rc!
33a73 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
33a74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 20 3d 20 30  urn rc;..  i = 0
33a75 3b 0a 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d  ;.  while( (rc =
33a76 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
33a77 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
33a78 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
33a79 34 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74  4 iStart = sqlit
33a7a 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
33a7b 73 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  s, 0);.    sqlit
33a7c 65 5f 69 6e 74 36 34 20 69 45 6e 64 20 3d 20 73  e_int64 iEnd = s
33a7d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
33a7e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20 63  t64(s, 1);.    c
33a7f 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74  onst char *pRoot
33a80 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63  Data = sqlite3_c
33a81 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29  olumn_blob(s, 2)
33a82 3b 0a 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 44  ;.    int nRootD
33a83 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ata = sqlite3_co
33a84 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29  lumn_bytes(s, 2)
33a85 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ;..    assert( i
33a86 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a  <MERGE_COUNT );.
33a87 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52      rc = leavesR
33a88 65 61 64 65 72 49 6e 69 74 28 76 2c 20 69 2c 20  eaderInit(v, i, 
33a89 69 53 74 61 72 74 2c 20 69 45 6e 64 2c 20 70 52  iStart, iEnd, pR
33a8a 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61  ootData, nRootDa
33a8b 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
33a8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
33a8d 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 20  Readers[i]);.   
33a8e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
33a8f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20  OK ) break;..   
33a90 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20   i++;.  }.  if( 
33a91 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
33a92 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d  ){.    while( i-
33a93 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20 6c 65 61  ->0 ){.      lea
33a94 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
33a95 28 26 70 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a  (&pReaders[i]);.
33a96 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
33a97 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 52   rc;.  }..  *piR
33a98 65 61 64 65 72 73 20 3d 20 69 3b 0a 0a 20 20 2f  eaders = i;..  /
33a99 2a 20 4c 65 61 76 65 20 6f 75 72 20 72 65 73 75  * Leave our resu
33a9a 6c 74 73 20 73 6f 72 74 65 64 20 62 79 20 74 65  lts sorted by te
33a9b 72 6d 2c 20 74 68 65 6e 20 61 67 65 2e 20 2a 2f  rm, then age. */
33a9c 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 29 7b  .  while( i-- ){
33a9d 0a 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65  .    leavesReade
33a9e 72 52 65 6f 72 64 65 72 28 70 52 65 61 64 65 72  rReorder(pReader
33a9f 73 2b 69 2c 20 2a 70 69 52 65 61 64 65 72 73 2d  s+i, *piReaders-
33aa0 69 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  i);.  }.  return
33aa1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
33aa2 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69 73 74 73  * Merge doclists
33aa3 20 66 72 6f 6d 20 70 52 65 61 64 65 72 73 5b 6e   from pReaders[n
33aa4 52 65 61 64 65 72 73 5d 20 69 6e 74 6f 20 61 20  Readers] into a 
33aa5 73 69 6e 67 6c 65 20 64 6f 63 6c 69 73 74 2c 20  single doclist, 
33aa6 77 68 69 63 68 0a 2a 2a 20 69 73 20 77 72 69 74  which.** is writ
33aa7 74 65 6e 20 74 6f 20 70 57 72 69 74 65 72 2e 20  ten to pWriter. 
33aa8 20 41 73 73 75 6d 65 73 20 70 52 65 61 64 65 72   Assumes pReader
33aa9 73 20 69 73 20 6f 72 64 65 72 65 64 20 6f 6c 64  s is ordered old
33aaa 65 73 74 20 74 6f 0a 2a 2a 20 6e 65 77 65 73 74  est to.** newest
33aab 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
33aac 73 73 29 20 43 6f 6e 73 69 64 65 72 20 70 75 74  ss) Consider put
33aad 74 69 6e 67 20 74 68 69 73 20 69 6e 6c 69 6e 65  ting this inline
33aae 20 69 6e 20 73 65 67 6d 65 6e 74 4d 65 72 67 65   in segmentMerge
33aaf 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  (). */.static in
33ab0 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 73 4d  t leavesReadersM
33ab1 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74  erge(fulltext_vt
33ab2 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20  ab *v,.         
33ab3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ab4 20 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65       LeavesReade
33ab5 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
33ab6 20 6e 52 65 61 64 65 72 73 2c 0a 20 20 20 20 20   nReaders,.     
33ab7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ab8 20 20 20 20 20 20 20 20 20 4c 65 61 66 57 72 69           LeafWri
33ab9 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20  ter *pWriter){. 
33aba 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64   DLReader dlRead
33abb 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d  ers[MERGE_COUNT]
33abc 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
33abd 70 54 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65  pTerm = leavesRe
33abe 61 64 65 72 54 65 72 6d 28 70 52 65 61 64 65 72  aderTerm(pReader
33abf 73 29 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 54 65  s);.  int i, nTe
33ac0 72 6d 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rm = leavesReade
33ac1 72 54 65 72 6d 42 79 74 65 73 28 70 52 65 61 64  rTermBytes(pRead
33ac2 65 72 73 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  ers);..  assert(
33ac3 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52 47 45   nReaders<=MERGE
33ac4 5f 43 4f 55 4e 54 20 29 3b 0a 0a 20 20 66 6f 72  _COUNT );..  for
33ac5 28 69 3d 30 3b 20 69 3c 6e 52 65 61 64 65 72 73  (i=0; i<nReaders
33ac6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 64 6c 72 49  ; i++){.    dlrI
33ac7 6e 69 74 28 26 64 6c 52 65 61 64 65 72 73 5b 69  nit(&dlReaders[i
33ac8 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20  ], DL_DEFAULT,. 
33ac9 20 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65             leave
33aca 73 52 65 61 64 65 72 44 61 74 61 28 70 52 65 61  sReaderData(pRea
33acb 64 65 72 73 2b 69 29 2c 0a 20 20 20 20 20 20 20  ders+i),.       
33acc 20 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65       leavesReade
33acd 72 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64  rDataBytes(pRead
33ace 65 72 73 2b 69 29 29 3b 0a 20 20 7d 0a 0a 20 20  ers+i));.  }..  
33acf 72 65 74 75 72 6e 20 6c 65 61 66 57 72 69 74 65  return leafWrite
33ad0 72 53 74 65 70 4d 65 72 67 65 28 76 2c 20 70 57  rStepMerge(v, pW
33ad1 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  riter, pTerm, nT
33ad2 65 72 6d 2c 20 64 6c 52 65 61 64 65 72 73 2c 20  erm, dlReaders, 
33ad3 6e 52 65 61 64 65 72 73 29 3b 0a 7d 0a 0a 2f 2a  nReaders);.}../*
33ad4 20 46 6f 72 77 61 72 64 20 72 65 66 20 64 75 65   Forward ref due
33ad5 20 74 6f 20 6d 75 74 75 61 6c 20 72 65 63 75 72   to mutual recur
33ad6 73 69 6f 6e 20 77 69 74 68 20 73 65 67 64 69 72  sion with segdir
33ad7 4e 65 78 74 49 6e 64 65 78 28 29 2e 20 2a 2f 0a  NextIndex(). */.
33ad8 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 6d 65  static int segme
33ad9 6e 74 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74  ntMerge(fulltext
33ada 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
33adb 65 76 65 6c 29 3b 0a 0a 2f 2a 20 50 75 74 20 74  evel);../* Put t
33adc 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c  he next availabl
33add 65 20 69 6e 64 65 78 20 61 74 20 69 4c 65 76 65  e index at iLeve
33ade 6c 20 69 6e 74 6f 20 2a 70 69 64 78 2e 20 20 49  l into *pidx.  I
33adf 66 20 69 4c 65 76 65 6c 0a 2a 2a 20 61 6c 72 65  f iLevel.** alre
33ae0 61 64 79 20 68 61 73 20 4d 45 52 47 45 5f 43 4f  ady has MERGE_CO
33ae1 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20 74 68  UNT segments, th
33ae2 65 79 20 61 72 65 20 6d 65 72 67 65 64 20 74 6f  ey are merged to
33ae3 20 61 20 68 69 67 68 65 72 0a 2a 2a 20 6c 65 76   a higher.** lev
33ae4 65 6c 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 2e  el to make room.
33ae5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
33ae6 65 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 66  egdirNextIndex(f
33ae7 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
33ae8 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74   int iLevel, int
33ae9 20 2a 70 69 64 78 29 7b 0a 20 20 69 6e 74 20 72   *pidx){.  int r
33aea 63 20 3d 20 73 65 67 64 69 72 5f 6d 61 78 5f 69  c = segdir_max_i
33aeb 6e 64 65 78 28 76 2c 20 69 4c 65 76 65 6c 2c 20  ndex(v, iLevel, 
33aec 70 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 3d  pidx);.  if( rc=
33aed 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 20  =SQLITE_DONE ){ 
33aee 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33aef 4e 6f 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69  No segments at i
33af0 4c 65 76 65 6c 2e 20 2a 2f 0a 20 20 20 20 2a 70  Level. */.    *p
33af1 69 64 78 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  idx = 0;.  }else
33af2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
33af3 52 4f 57 20 29 7b 0a 20 20 20 20 69 66 28 20 2a  ROW ){.    if( *
33af4 70 69 64 78 3d 3d 28 4d 45 52 47 45 5f 43 4f 55  pidx==(MERGE_COU
33af5 4e 54 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72  NT-1) ){.      r
33af6 63 20 3d 20 73 65 67 6d 65 6e 74 4d 65 72 67 65  c = segmentMerge
33af7 28 76 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 20  (v, iLevel);.   
33af8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
33af9 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
33afa 3b 0a 20 20 20 20 20 20 2a 70 69 64 78 20 3d 20  ;.      *pidx = 
33afb 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
33afc 20 20 20 20 28 2a 70 69 64 78 29 2b 2b 3b 0a 20      (*pidx)++;. 
33afd 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
33afe 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
33aff 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
33b00 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65  _OK;.}../* Merge
33b01 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67   MERGE_COUNT seg
33b02 6d 65 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 20  ments at iLevel 
33b03 69 6e 74 6f 20 61 20 6e 65 77 20 73 65 67 6d 65  into a new segme
33b04 6e 74 20 61 74 0a 2a 2a 20 69 4c 65 76 65 6c 2b  nt at.** iLevel+
33b05 31 2e 20 20 49 66 20 69 4c 65 76 65 6c 2b 31 20  1.  If iLevel+1 
33b06 69 73 20 61 6c 72 65 61 64 79 20 66 75 6c 6c 20  is already full 
33b07 6f 66 20 73 65 67 6d 65 6e 74 73 2c 20 74 68 6f  of segments, tho
33b08 73 65 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 6d 65  se will be.** me
33b09 72 67 65 64 20 74 6f 20 6d 61 6b 65 20 72 6f 6f  rged to make roo
33b0a 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m..*/.static int
33b0b 20 73 65 67 6d 65 6e 74 4d 65 72 67 65 28 66 75   segmentMerge(fu
33b0c 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
33b0d 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20 4c  int iLevel){.  L
33b0e 65 61 66 57 72 69 74 65 72 20 77 72 69 74 65 72  eafWriter writer
33b0f 3b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65 72  ;.  LeavesReader
33b10 20 6c 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54   lrs[MERGE_COUNT
33b11 5d 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c 20  ];.  int i, rc, 
33b12 69 64 78 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44  idx = 0;..  /* D
33b13 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78  etermine the nex
33b14 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 67 6d  t available segm
33b15 65 6e 74 20 69 6e 64 65 78 20 61 74 20 74 68 65  ent index at the
33b16 20 6e 65 78 74 20 6c 65 76 65 6c 2c 0a 20 20 2a   next level,.  *
33b17 2a 20 6d 65 72 67 69 6e 67 20 61 73 20 6e 65 63  * merging as nec
33b18 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 72  essary..  */.  r
33b19 63 20 3d 20 73 65 67 64 69 72 4e 65 78 74 49 6e  c = segdirNextIn
33b1a 64 65 78 28 76 2c 20 69 4c 65 76 65 6c 2b 31 2c  dex(v, iLevel+1,
33b1b 20 26 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63   &idx);.  if( rc
33b1c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
33b1d 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54  turn rc;..  /* T
33b1e 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
33b1f 61 73 73 75 6d 65 73 20 74 68 61 74 20 77 65 27  assumes that we'
33b20 6c 6c 20 61 6c 77 61 79 73 20 73 65 65 20 65 78  ll always see ex
33b21 61 63 74 6c 79 0a 20 20 2a 2a 20 4d 45 52 47 45  actly.  ** MERGE
33b22 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 20  _COUNT segments 
33b23 74 6f 20 6d 65 72 67 65 20 61 74 20 61 20 67 69  to merge at a gi
33b24 76 65 6e 20 6c 65 76 65 6c 2e 20 20 54 68 61 74  ven level.  That
33b25 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 62 72   will be.  ** br
33b26 6f 6b 65 6e 20 69 66 20 77 65 20 61 6c 6c 6f 77  oken if we allow
33b27 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20 74   the developer t
33b28 6f 20 72 65 71 75 65 73 74 20 70 72 65 65 6d 70  o request preemp
33b29 74 69 76 65 20 6f 72 0a 20 20 2a 2a 20 64 65 66  tive or.  ** def
33b2a 65 72 72 65 64 20 6d 65 72 67 69 6e 67 2e 0a 20  erred merging.. 
33b2b 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6c 72   */.  memset(&lr
33b2c 73 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28  s, '\0', sizeof(
33b2d 6c 72 73 29 29 3b 0a 20 20 72 63 20 3d 20 6c 65  lrs));.  rc = le
33b2e 61 76 65 73 52 65 61 64 65 72 73 49 6e 69 74 28  avesReadersInit(
33b2f 76 2c 20 69 4c 65 76 65 6c 2c 20 6c 72 73 2c 20  v, iLevel, lrs, 
33b30 26 69 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &i);.  if( rc!=S
33b31 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
33b32 6e 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  n rc;.  assert( 
33b33 69 3d 3d 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29  i==MERGE_COUNT )
33b34 3b 0a 0a 20 20 6c 65 61 66 57 72 69 74 65 72 49  ;..  leafWriterI
33b35 6e 69 74 28 69 4c 65 76 65 6c 2b 31 2c 20 69 64  nit(iLevel+1, id
33b36 78 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 20  x, &writer);..  
33b37 2f 2a 20 53 69 6e 63 65 20 6c 65 61 76 65 73 52  /* Since leavesR
33b38 65 61 64 65 72 52 65 6f 72 64 65 72 28 29 20 70  eaderReorder() p
33b39 75 73 68 65 73 20 72 65 61 64 65 72 73 20 61 74  ushes readers at
33b3a 20 65 6f 66 20 74 6f 20 74 68 65 20 65 6e 64 2c   eof to the end,
33b3b 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 66  .  ** when the f
33b3c 69 72 73 74 20 72 65 61 64 65 72 20 69 73 20 65  irst reader is e
33b3d 6d 70 74 79 2c 20 61 6c 6c 20 77 69 6c 6c 20 62  mpty, all will b
33b3e 65 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20  e empty..  */.  
33b3f 77 68 69 6c 65 28 20 21 6c 65 61 76 65 73 52 65  while( !leavesRe
33b40 61 64 65 72 41 74 45 6e 64 28 6c 72 73 29 20 29  aderAtEnd(lrs) )
33b41 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  {.    /* Figure 
33b42 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 61  out how many rea
33b43 64 65 72 73 20 73 68 61 72 65 20 74 68 65 69 72  ders share their
33b44 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a 2f 0a 20   next term. */. 
33b45 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4d 45     for(i=1; i<ME
33b46 52 47 45 5f 43 4f 55 4e 54 20 26 26 20 21 6c 65  RGE_COUNT && !le
33b47 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28  avesReaderAtEnd(
33b48 6c 72 73 2b 69 29 3b 20 69 2b 2b 29 7b 0a 20 20  lrs+i); i++){.  
33b49 20 20 20 20 69 66 28 20 30 21 3d 6c 65 61 76 65      if( 0!=leave
33b4a 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 6c  sReaderTermCmp(l
33b4b 72 73 2c 20 6c 72 73 2b 69 29 20 29 20 62 72 65  rs, lrs+i) ) bre
33b4c 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72  ak;.    }..    r
33b4d 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72  c = leavesReader
33b4e 73 4d 65 72 67 65 28 76 2c 20 6c 72 73 2c 20 69  sMerge(v, lrs, i
33b4f 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20  , &writer);.    
33b50 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
33b51 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20  K ) goto err;.. 
33b52 20 20 20 2f 2a 20 53 74 65 70 20 66 6f 72 77 61     /* Step forwa
33b53 72 64 20 74 68 6f 73 65 20 74 68 61 74 20 77 65  rd those that we
33b54 72 65 20 6d 65 72 67 65 64 2e 20 2a 2f 0a 20 20  re merged. */.  
33b55 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29    while( i-->0 )
33b56 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65 61  {.      rc = lea
33b57 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c  vesReaderStep(v,
33b58 20 6c 72 73 2b 69 29 3b 0a 20 20 20 20 20 20 69   lrs+i);.      i
33b59 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
33b5a 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20   ) goto err;..  
33b5b 20 20 20 20 2f 2a 20 52 65 6f 72 64 65 72 20 62      /* Reorder b
33b5c 79 20 74 65 72 6d 2c 20 74 68 65 6e 20 62 79 20  y term, then by 
33b5d 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 6c 65  age. */.      le
33b5e 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
33b5f 72 28 6c 72 73 2b 69 2c 20 4d 45 52 47 45 5f 43  r(lrs+i, MERGE_C
33b60 4f 55 4e 54 2d 69 29 3b 0a 20 20 20 20 7d 0a 20  OUNT-i);.    }. 
33b61 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   }..  for(i=0; i
33b62 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b  <MERGE_COUNT; i+
33b63 2b 29 7b 0a 20 20 20 20 6c 65 61 76 65 73 52 65  +){.    leavesRe
33b64 61 64 65 72 44 65 73 74 72 6f 79 28 26 6c 72 73  aderDestroy(&lrs
33b65 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  [i]);.  }..  rc 
33b66 3d 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61  = leafWriterFina
33b67 6c 69 7a 65 28 76 2c 20 26 77 72 69 74 65 72 29  lize(v, &writer)
33b68 3b 0a 20 20 6c 65 61 66 57 72 69 74 65 72 44 65  ;.  leafWriterDe
33b69 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
33b6a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33b6b 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
33b6c 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68  ..  /* Delete th
33b6d 65 20 6d 65 72 67 65 64 20 73 65 67 6d 65 6e 74  e merged segment
33b6e 20 64 61 74 61 2e 20 2a 2f 0a 20 20 72 65 74 75   data. */.  retu
33b6f 72 6e 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65  rn segdir_delete
33b70 28 76 2c 20 69 4c 65 76 65 6c 29 3b 0a 0a 20 65  (v, iLevel);.. e
33b71 72 72 3a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rr:.  for(i=0; i
33b72 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b  <MERGE_COUNT; i+
33b73 2b 29 7b 0a 20 20 20 20 6c 65 61 76 65 73 52 65  +){.    leavesRe
33b74 61 64 65 72 44 65 73 74 72 6f 79 28 26 6c 72 73  aderDestroy(&lrs
33b75 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6c 65 61 66  [i]);.  }.  leaf
33b76 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 77  WriterDestroy(&w
33b77 72 69 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  riter);.  return
33b78 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 75 6d   rc;.}../* Accum
33b79 75 6c 61 74 65 20 74 68 65 20 75 6e 69 6f 6e 20  ulate the union 
33b7a 6f 66 20 2a 61 63 63 20 61 6e 64 20 2a 70 44 61  of *acc and *pDa
33b7b 74 61 20 69 6e 74 6f 20 2a 61 63 63 2e 20 2a 2f  ta into *acc. */
33b7c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
33b7d 4c 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e  ListAccumulateUn
33b7e 69 6f 6e 28 44 61 74 61 42 75 66 66 65 72 20 2a  ion(DataBuffer *
33b7f 61 63 63 2c 0a 20 20 20 20 20 20 20 20 20 20 20  acc,.           
33b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b81 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
33b82 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
33b83 44 61 74 61 29 20 7b 0a 20 20 44 61 74 61 42 75  Data) {.  DataBu
33b84 66 66 65 72 20 74 6d 70 20 3d 20 2a 61 63 63 3b  ffer tmp = *acc;
33b85 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  .  dataBufferIni
33b86 74 28 61 63 63 2c 20 74 6d 70 2e 6e 44 61 74 61  t(acc, tmp.nData
33b87 2b 6e 44 61 74 61 29 3b 0a 20 20 64 6f 63 4c 69  +nData);.  docLi
33b88 73 74 55 6e 69 6f 6e 28 74 6d 70 2e 70 44 61 74  stUnion(tmp.pDat
33b89 61 2c 20 74 6d 70 2e 6e 44 61 74 61 2c 20 70 44  a, tmp.nData, pD
33b8a 61 74 61 2c 20 6e 44 61 74 61 2c 20 61 63 63 29  ata, nData, acc)
33b8b 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
33b8c 73 74 72 6f 79 28 26 74 6d 70 29 3b 0a 7d 0a 0a  stroy(&tmp);.}..
33b8d 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
33b8e 74 20 6d 69 67 68 74 20 62 65 20 69 6e 74 65 72  t might be inter
33b8f 65 73 74 69 6e 67 20 74 6f 20 65 78 70 6c 6f 72  esting to explor
33b90 65 20 64 69 66 66 65 72 65 6e 74 20 6d 65 72 67  e different merg
33b91 65 0a 2a 2a 20 73 74 72 61 74 65 67 69 65 73 2c  e.** strategies,
33b92 20 68 65 72 65 2e 20 20 46 6f 72 20 69 6e 73 74   here.  For inst
33b93 61 6e 63 65 2c 20 73 69 6e 63 65 20 74 68 69 73  ance, since this
33b94 20 69 73 20 61 20 73 6f 72 74 65 64 20 6d 65 72   is a sorted mer
33b95 67 65 2c 20 77 65 0a 2a 2a 20 63 6f 75 6c 64 20  ge, we.** could 
33b96 65 61 73 69 6c 79 20 6d 65 72 67 65 20 6d 61 6e  easily merge man
33b97 79 20 64 6f 63 6c 69 73 74 73 20 69 6e 20 70 61  y doclists in pa
33b98 72 61 6c 6c 65 6c 2e 20 20 57 69 74 68 20 73 6f  rallel.  With so
33b99 6d 65 0a 2a 2a 20 63 6f 6d 70 72 65 68 65 6e 73  me.** comprehens
33b9a 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 6f 72 61  ion of the stora
33b9b 67 65 20 66 6f 72 6d 61 74 2c 20 77 65 20 63 6f  ge format, we co
33b9c 75 6c 64 20 6d 65 72 67 65 20 61 6c 6c 20 6f 66  uld merge all of
33b9d 20 74 68 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 73   the.** doclists
33b9e 20 77 69 74 68 69 6e 20 61 20 6c 65 61 66 20 6e   within a leaf n
33b9f 6f 64 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f  ode directly fro
33ba0 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 27  m the leaf node'
33ba1 73 20 73 74 6f 72 61 67 65 2e 0a 2a 2a 20 49 74  s storage..** It
33ba2 20 6d 61 79 20 62 65 20 77 6f 72 74 68 77 68 69   may be worthwhi
33ba3 6c 65 20 74 6f 20 6d 65 72 67 65 20 73 6d 61 6c  le to merge smal
33ba4 6c 65 72 20 64 6f 63 6c 69 73 74 73 20 62 65 66  ler doclists bef
33ba5 6f 72 65 20 6c 61 72 67 65 72 0a 2a 2a 20 64 6f  ore larger.** do
33ba6 63 6c 69 73 74 73 2c 20 73 69 6e 63 65 20 74 68  clists, since th
33ba7 65 79 20 63 61 6e 20 62 65 20 74 72 61 76 65 72  ey can be traver
33ba8 73 65 64 20 6d 6f 72 65 20 71 75 69 63 6b 6c 79  sed more quickly
33ba9 20 2d 20 62 75 74 20 74 68 65 0a 2a 2a 20 72 65   - but the.** re
33baa 73 75 6c 74 73 20 6d 61 79 20 68 61 76 65 20 6c  sults may have l
33bab 65 73 73 20 6f 76 65 72 6c 61 70 2c 20 6d 61 6b  ess overlap, mak
33bac 69 6e 67 20 74 68 65 6d 20 6d 6f 72 65 20 65 78  ing them more ex
33bad 70 65 6e 73 69 76 65 20 69 6e 20 61 0a 2a 2a 20  pensive in a.** 
33bae 64 69 66 66 65 72 65 6e 74 20 77 61 79 2e 0a 2a  different way..*
33baf 2f 0a 0a 2f 2a 20 53 63 61 6e 20 70 52 65 61 64  /../* Scan pRead
33bb0 65 72 20 66 6f 72 20 70 54 65 72 6d 2f 6e 54 65  er for pTerm/nTe
33bb1 72 6d 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68  rm, and merge th
33bb2 65 20 74 65 72 6d 27 73 20 64 6f 63 6c 69 73 74  e term's doclist
33bb3 20 6f 76 65 72 0a 2a 2a 20 2a 6f 75 74 20 28 61   over.** *out (a
33bb4 6e 79 20 64 6f 63 6c 69 73 74 73 20 77 69 74 68  ny doclists with
33bb5 20 64 75 70 6c 69 63 61 74 65 20 64 6f 63 69 64   duplicate docid
33bb6 73 20 6f 76 65 72 77 72 69 74 65 20 74 68 6f 73  s overwrite thos
33bb7 65 20 69 6e 20 2a 6f 75 74 29 2e 0a 2a 2a 20 49  e in *out)..** I
33bb8 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e  nternal function
33bb9 20 66 6f 72 20 6c 6f 61 64 53 65 67 6d 65 6e 74   for loadSegment
33bba 4c 65 61 66 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Leaf()..*/.stati
33bbb 63 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e  c int loadSegmen
33bbc 74 4c 65 61 76 65 73 49 6e 74 28 66 75 6c 6c 74  tLeavesInt(fullt
33bbd 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
33bbe 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
33bbf 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
33bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33bc1 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
33bc2 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
33bc3 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a  , int isPrefix,.
33bc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33bc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33bc6 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29  DataBuffer *out)
33bc7 7b 0a 20 20 2f 2a 20 64 6f 63 6c 69 73 74 20 64  {.  /* doclist d
33bc8 61 74 61 20 69 73 20 61 63 63 75 6d 75 6c 61 74  ata is accumulat
33bc9 65 64 20 69 6e 74 6f 20 70 42 75 66 66 65 72 73  ed into pBuffers
33bca 20 73 69 6d 69 6c 61 72 20 74 6f 20 68 6f 77 20   similar to how 
33bcb 6f 6e 65 20 64 6f 65 73 0a 20 20 2a 2a 20 69 6e  one does.  ** in
33bcc 63 72 65 6d 65 6e 74 20 69 6e 20 62 69 6e 61 72  crement in binar
33bcd 79 20 61 72 69 74 68 6d 65 74 69 63 2e 20 20 49  y arithmetic.  I
33bce 66 20 69 6e 64 65 78 20 30 20 69 73 20 65 6d 70  f index 0 is emp
33bcf 74 79 2c 20 74 68 65 20 64 61 74 61 20 69 73 0a  ty, the data is.
33bd0 20 20 2a 2a 20 73 74 6f 72 65 64 20 74 68 65 72    ** stored ther
33bd1 65 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  e.  If there is 
33bd2 64 61 74 61 20 74 68 65 72 65 2c 20 69 74 20 69  data there, it i
33bd3 73 20 6d 65 72 67 65 64 20 61 6e 64 20 74 68 65  s merged and the
33bd4 0a 20 20 2a 2a 20 72 65 73 75 6c 74 73 20 63 61  .  ** results ca
33bd5 72 72 69 65 64 20 69 6e 74 6f 20 70 6f 73 69 74  rried into posit
33bd6 69 6f 6e 20 31 2c 20 77 69 74 68 20 66 75 72 74  ion 1, with furt
33bd7 68 65 72 20 6d 65 72 67 65 2d 61 6e 64 2d 63 61  her merge-and-ca
33bd8 72 72 79 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 61  rry.  ** until a
33bd9 6e 20 65 6d 70 74 79 20 70 6f 73 69 74 69 6f 6e  n empty position
33bda 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a   is found..  */.
33bdb 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 42    DataBuffer *pB
33bdc 75 66 66 65 72 73 20 3d 20 4e 55 4c 4c 3b 0a 20  uffers = NULL;. 
33bdd 20 69 6e 74 20 6e 42 75 66 66 65 72 73 20 3d 20   int nBuffers = 
33bde 30 2c 20 6e 4d 61 78 42 75 66 66 65 72 73 20 3d  0, nMaxBuffers =
33bdf 20 30 2c 20 72 63 3b 0a 0a 20 20 61 73 73 65 72   0, rc;..  asser
33be0 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0a 0a 20  t( nTerm>0 );.. 
33be1 20 66 6f 72 28 72 63 3d 53 51 4c 49 54 45 5f 4f   for(rc=SQLITE_O
33be2 4b 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  K; rc==SQLITE_OK
33be3 20 26 26 20 21 6c 65 61 76 65 73 52 65 61 64 65   && !leavesReade
33be4 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 3b  rAtEnd(pReader);
33be5 0a 20 20 20 20 20 20 72 63 3d 6c 65 61 76 65 73  .      rc=leaves
33be6 52 65 61 64 65 72 53 74 65 70 28 76 2c 20 70 52  ReaderStep(v, pR
33be7 65 61 64 65 72 29 29 7b 0a 20 20 20 20 2f 2a 20  eader)){.    /* 
33be8 54 4f 44 4f 28 73 68 65 73 73 29 20 52 65 61 6c  TODO(shess) Real
33be9 6c 79 20 77 61 6e 74 20 6c 65 61 76 65 73 52 65  ly want leavesRe
33bea 61 64 65 72 54 65 72 6d 43 6d 70 28 29 2c 20 62  aderTermCmp(), b
33beb 75 74 20 74 68 61 74 20 6e 61 6d 65 20 69 73 0a  ut that name is.
33bec 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 74      ** already t
33bed 61 6b 65 6e 20 74 6f 20 63 6f 6d 70 61 72 65 20  aken to compare 
33bee 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 77 6f  the terms of two
33bef 20 4c 65 61 76 65 73 52 65 61 64 65 72 73 2e 20   LeavesReaders. 
33bf0 20 54 68 69 6e 6b 0a 20 20 20 20 2a 2a 20 6f 6e   Think.    ** on
33bf1 20 61 20 62 65 74 74 65 72 20 6e 61 6d 65 2e 20   a better name. 
33bf2 20 5b 4d 65 61 6e 77 68 69 6c 65 2c 20 62 72 65   [Meanwhile, bre
33bf3 61 6b 20 65 6e 63 61 70 73 75 6c 61 74 69 6f 6e  ak encapsulation
33bf4 20 72 61 74 68 65 72 20 74 68 61 6e 0a 20 20 20   rather than.   
33bf5 20 2a 2a 20 75 73 65 20 61 20 63 6f 6e 66 75 73   ** use a confus
33bf6 69 6e 67 20 6e 61 6d 65 2e 5d 0a 20 20 20 20 2a  ing name.].    *
33bf7 2f 0a 20 20 20 20 69 6e 74 20 63 20 3d 20 6c 65  /.    int c = le
33bf8 61 66 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  afReaderTermCmp(
33bf9 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65  &pReader->leafRe
33bfa 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  ader, pTerm, nTe
33bfb 72 6d 2c 20 69 73 50 72 65 66 69 78 29 3b 0a 20  rm, isPrefix);. 
33bfc 20 20 20 69 66 28 20 63 3e 30 20 29 20 62 72 65     if( c>0 ) bre
33bfd 61 6b 3b 20 20 20 20 20 20 2f 2a 20 50 61 73 74  ak;      /* Past
33bfe 20 61 6e 79 20 70 6f 73 73 69 62 6c 65 20 6d 61   any possible ma
33bff 74 63 68 65 73 2e 20 2a 2f 0a 20 20 20 20 69 66  tches. */.    if
33c00 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
33c01 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
33c02 61 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72  a = leavesReader
33c03 44 61 74 61 28 70 52 65 61 64 65 72 29 3b 0a 20  Data(pReader);. 
33c04 20 20 20 20 20 69 6e 74 20 69 42 75 66 66 65 72       int iBuffer
33c05 2c 20 6e 44 61 74 61 20 3d 20 6c 65 61 76 65 73  , nData = leaves
33c06 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28  ReaderDataBytes(
33c07 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 20  pReader);..     
33c08 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72   /* Find the fir
33c09 73 74 20 65 6d 70 74 79 20 62 75 66 66 65 72 2e  st empty buffer.
33c0a 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 69 42   */.      for(iB
33c0b 75 66 66 65 72 3d 30 3b 20 69 42 75 66 66 65 72  uffer=0; iBuffer
33c0c 3c 6e 42 75 66 66 65 72 73 3b 20 2b 2b 69 42 75  <nBuffers; ++iBu
33c0d 66 66 65 72 29 7b 0a 20 20 20 20 20 20 20 20 69  ffer){.        i
33c0e 66 28 20 30 3d 3d 70 42 75 66 66 65 72 73 5b 69  f( 0==pBuffers[i
33c0f 42 75 66 66 65 72 5d 2e 6e 44 61 74 61 20 29 20  Buffer].nData ) 
33c10 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a  break;.      }..
33c11 20 20 20 20 20 20 2f 2a 20 4f 75 74 20 6f 66 20        /* Out of 
33c12 62 75 66 66 65 72 73 2c 20 61 64 64 20 61 6e 20  buffers, add an 
33c13 65 6d 70 74 79 20 6f 6e 65 2e 20 2a 2f 0a 20 20  empty one. */.  
33c14 20 20 20 20 69 66 28 20 69 42 75 66 66 65 72 3d      if( iBuffer=
33c15 3d 6e 42 75 66 66 65 72 73 20 29 7b 0a 20 20 20  =nBuffers ){.   
33c16 20 20 20 20 20 69 66 28 20 6e 42 75 66 66 65 72       if( nBuffer
33c17 73 3d 3d 6e 4d 61 78 42 75 66 66 65 72 73 20 29  s==nMaxBuffers )
33c18 7b 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 61  {.          Data
33c19 42 75 66 66 65 72 20 2a 70 3b 0a 20 20 20 20 20  Buffer *p;.     
33c1a 20 20 20 20 20 6e 4d 61 78 42 75 66 66 65 72 73       nMaxBuffers
33c1b 20 2b 3d 20 32 30 3b 0a 0a 20 20 20 20 20 20 20   += 20;..       
33c1c 20 20 20 2f 2a 20 4d 61 6e 75 61 6c 20 72 65 61     /* Manual rea
33c1d 6c 6c 6f 63 20 73 6f 20 77 65 20 63 61 6e 20 68  lloc so we can h
33c1e 61 6e 64 6c 65 20 4e 55 4c 4c 20 61 70 70 72 6f  andle NULL appro
33c1f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20  priately. */.   
33c20 20 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74         p = sqlit
33c21 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4d 61 78 42 75  e3_malloc(nMaxBu
33c22 66 66 65 72 73 2a 73 69 7a 65 6f 66 28 2a 70 42  ffers*sizeof(*pB
33c23 75 66 66 65 72 73 29 29 3b 0a 20 20 20 20 20 20  uffers));.      
33c24 20 20 20 20 69 66 28 20 70 3d 3d 4e 55 4c 4c 20      if( p==NULL 
33c25 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
33c26 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
33c27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
33c28 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
33c29 0a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
33c2a 6e 42 75 66 66 65 72 73 3e 30 20 29 7b 0a 20 20  nBuffers>0 ){.  
33c2b 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
33c2c 28 70 42 75 66 66 65 72 73 21 3d 4e 55 4c 4c 29  (pBuffers!=NULL)
33c2d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  ;.            me
33c2e 6d 63 70 79 28 70 2c 20 70 42 75 66 66 65 72 73  mcpy(p, pBuffers
33c2f 2c 20 6e 42 75 66 66 65 72 73 2a 73 69 7a 65 6f  , nBuffers*sizeo
33c30 66 28 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20  f(*pBuffers));. 
33c31 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
33c32 65 33 5f 66 72 65 65 28 70 42 75 66 66 65 72 73  e3_free(pBuffers
33c33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
33c34 20 20 20 20 20 20 20 20 20 70 42 75 66 66 65 72           pBuffer
33c35 73 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d  s = p;.        }
33c36 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
33c37 66 65 72 49 6e 69 74 28 26 28 70 42 75 66 66 65  ferInit(&(pBuffe
33c38 72 73 5b 6e 42 75 66 66 65 72 73 5d 29 2c 20 30  rs[nBuffers]), 0
33c39 29 3b 0a 20 20 20 20 20 20 20 20 6e 42 75 66 66  );.        nBuff
33c3a 65 72 73 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a  ers++;.      }..
33c3b 20 20 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73        /* At this
33c3c 20 70 6f 69 6e 74 2c 20 6d 75 73 74 20 68 61 76   point, must hav
33c3d 65 20 61 6e 20 65 6d 70 74 79 20 61 74 20 69 42  e an empty at iB
33c3e 75 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20  uffer. */.      
33c3f 61 73 73 65 72 74 28 69 42 75 66 66 65 72 3c 6e  assert(iBuffer<n
33c40 42 75 66 66 65 72 73 20 26 26 20 70 42 75 66 66  Buffers && pBuff
33c41 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61  ers[iBuffer].nDa
33c42 74 61 3d 3d 30 29 3b 0a 0a 20 20 20 20 20 20 2f  ta==0);..      /
33c43 2a 20 49 66 20 65 6d 70 74 79 20 77 61 73 20 66  * If empty was f
33c44 69 72 73 74 20 62 75 66 66 65 72 2c 20 6e 6f 20  irst buffer, no 
33c45 6e 65 65 64 20 66 6f 72 20 6d 65 72 67 65 20 6c  need for merge l
33c46 6f 67 69 63 2e 20 2a 2f 0a 20 20 20 20 20 20 69  ogic. */.      i
33c47 66 28 20 69 42 75 66 66 65 72 3d 3d 30 20 29 7b  f( iBuffer==0 ){
33c48 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
33c49 66 65 72 52 65 70 6c 61 63 65 28 26 28 70 42 75  ferReplace(&(pBu
33c4a 66 66 65 72 73 5b 30 5d 29 2c 20 70 44 61 74 61  ffers[0]), pData
33c4b 2c 20 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  , nData);.      
33c4c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
33c4d 2a 20 70 41 63 63 20 69 73 20 74 68 65 20 65 6d  * pAcc is the em
33c4e 70 74 79 20 62 75 66 66 65 72 20 74 68 65 20 6d  pty buffer the m
33c4f 65 72 67 65 64 20 64 61 74 61 20 77 69 6c 6c 20  erged data will 
33c50 65 6e 64 20 75 70 20 69 6e 2e 20 2a 2f 0a 20 20  end up in. */.  
33c51 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
33c52 20 2a 70 41 63 63 20 3d 20 26 28 70 42 75 66 66   *pAcc = &(pBuff
33c53 65 72 73 5b 69 42 75 66 66 65 72 5d 29 3b 0a 20  ers[iBuffer]);. 
33c54 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65         DataBuffe
33c55 72 20 2a 70 20 3d 20 26 28 70 42 75 66 66 65 72  r *p = &(pBuffer
33c56 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20 20 20  s[0]);..        
33c57 2f 2a 20 48 61 6e 64 6c 65 20 70 6f 73 69 74 69  /* Handle positi
33c58 6f 6e 20 30 20 73 70 65 63 69 61 6c 6c 79 20 74  on 0 specially t
33c59 6f 20 61 76 6f 69 64 20 6e 65 65 64 20 74 6f 20  o avoid need to 
33c5a 70 72 69 6d 65 20 70 41 63 63 0a 20 20 20 20 20  prime pAcc.     
33c5b 20 20 20 2a 2a 20 77 69 74 68 20 70 44 61 74 61     ** with pData
33c5c 2f 6e 44 61 74 61 2e 0a 20 20 20 20 20 20 20 20  /nData..        
33c5d 2a 2f 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  */.        dataB
33c5e 75 66 66 65 72 53 77 61 70 28 70 2c 20 70 41 63  ufferSwap(p, pAc
33c5f 63 29 3b 0a 20 20 20 20 20 20 20 20 64 6f 63 4c  c);.        docL
33c60 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69  istAccumulateUni
33c61 6f 6e 28 70 41 63 63 2c 20 70 44 61 74 61 2c 20  on(pAcc, pData, 
33c62 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 20  nData);..       
33c63 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72   /* Accumulate r
33c64 65 6d 61 69 6e 69 6e 67 20 64 6f 63 6c 69 73 74  emaining doclist
33c65 73 20 69 6e 74 6f 20 70 41 63 63 2e 20 2a 2f 0a  s into pAcc. */.
33c66 20 20 20 20 20 20 20 20 66 6f 72 28 2b 2b 70 3b          for(++p;
33c67 20 70 3c 70 41 63 63 3b 20 2b 2b 70 29 7b 0a 20   p<pAcc; ++p){. 
33c68 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74           docList
33c69 41 63 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28  AccumulateUnion(
33c6a 70 41 63 63 2c 20 70 2d 3e 70 44 61 74 61 2c 20  pAcc, p->pData, 
33c6b 70 2d 3e 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20  p->nData);..    
33c6c 20 20 20 20 20 20 2f 2a 20 64 61 74 61 42 75 66        /* dataBuf
33c6d 66 65 72 52 65 73 65 74 28 29 20 63 6f 75 6c 64  ferReset() could
33c6e 20 61 6c 6c 6f 77 20 61 20 6c 61 72 67 65 20 64   allow a large d
33c6f 6f 63 6c 69 73 74 20 74 6f 20 62 6c 6f 77 20 75  oclist to blow u
33c70 70 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f  p.          ** o
33c71 75 72 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72  ur memory requir
33c72 65 6d 65 6e 74 73 2e 0a 20 20 20 20 20 20 20 20  ements..        
33c73 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69    */.          i
33c74 66 28 20 70 2d 3e 6e 43 61 70 61 63 69 74 79 3c  f( p->nCapacity<
33c75 31 30 32 34 20 29 7b 0a 20 20 20 20 20 20 20 20  1024 ){.        
33c76 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
33c77 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
33c78 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
33c79 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44       dataBufferD
33c7a 65 73 74 72 6f 79 28 70 29 3b 0a 20 20 20 20 20  estroy(p);.     
33c7b 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
33c7c 72 49 6e 69 74 28 70 2c 20 30 29 3b 0a 20 20 20  rInit(p, 0);.   
33c7d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
33c7e 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
33c7f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 6e 69 6f 6e  .  }..  /* Union
33c80 20 61 6c 6c 20 74 68 65 20 64 6f 63 6c 69 73 74   all the doclist
33c81 73 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20  s together into 
33c82 2a 6f 75 74 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f  *out. */.  /* TO
33c83 44 4f 28 73 68 65 73 73 29 20 57 68 61 74 20 69  DO(shess) What i
33c84 66 20 2a 6f 75 74 20 69 73 20 62 69 67 3f 20 20  f *out is big?  
33c85 53 69 67 68 2e 20 2a 2f 0a 20 20 69 66 28 20 72  Sigh. */.  if( r
33c86 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
33c87 6e 42 75 66 66 65 72 73 3e 30 20 29 7b 0a 20 20  nBuffers>0 ){.  
33c88 20 20 69 6e 74 20 69 42 75 66 66 65 72 3b 0a 20    int iBuffer;. 
33c89 20 20 20 66 6f 72 28 69 42 75 66 66 65 72 3d 30     for(iBuffer=0
33c8a 3b 20 69 42 75 66 66 65 72 3c 6e 42 75 66 66 65  ; iBuffer<nBuffe
33c8b 72 73 3b 20 2b 2b 69 42 75 66 66 65 72 29 7b 0a  rs; ++iBuffer){.
33c8c 20 20 20 20 20 20 69 66 28 20 70 42 75 66 66 65        if( pBuffe
33c8d 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74  rs[iBuffer].nDat
33c8e 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  a>0 ){.        i
33c8f 66 28 20 6f 75 74 2d 3e 6e 44 61 74 61 3d 3d 30  f( out->nData==0
33c90 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 61   ){.          da
33c91 74 61 42 75 66 66 65 72 53 77 61 70 28 6f 75 74  taBufferSwap(out
33c92 2c 20 26 28 70 42 75 66 66 65 72 73 5b 69 42 75  , &(pBuffers[iBu
33c93 66 66 65 72 5d 29 29 3b 0a 20 20 20 20 20 20 20  ffer]));.       
33c94 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
33c95 20 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c    docListAccumul
33c96 61 74 65 55 6e 69 6f 6e 28 6f 75 74 2c 20 70 42  ateUnion(out, pB
33c97 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e  uffers[iBuffer].
33c98 70 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  pData,.         
33c99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c9a 20 20 20 20 20 20 20 20 70 42 75 66 66 65 72 73          pBuffers
33c9b 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74 61 29  [iBuffer].nData)
33c9c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
33c9d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
33c9e 20 77 68 69 6c 65 28 20 6e 42 75 66 66 65 72 73   while( nBuffers
33c9f 2d 2d 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75  -- ){.    dataBu
33ca0 66 66 65 72 44 65 73 74 72 6f 79 28 26 28 70 42  fferDestroy(&(pB
33ca1 75 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73 5d  uffers[nBuffers]
33ca2 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 42  ));.  }.  if( pB
33ca3 75 66 66 65 72 73 21 3d 4e 55 4c 4c 20 29 20 73  uffers!=NULL ) s
33ca4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75 66  qlite3_free(pBuf
33ca5 66 65 72 73 29 3b 0a 0a 20 20 72 65 74 75 72 6e  fers);..  return
33ca6 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61 6c 6c 20   rc;.}../* Call 
33ca7 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65  loadSegmentLeave
33ca8 73 49 6e 74 28 29 20 77 69 74 68 20 70 44 61 74  sInt() with pDat
33ca9 61 2f 6e 44 61 74 61 20 61 73 20 69 6e 70 75 74  a/nData as input
33caa 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
33cab 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 66 28  loadSegmentLeaf(
33cac 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
33cad 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
33cae 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
33caf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33cb0 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
33cb1 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
33cb2 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
33cb3 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
33cb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33cb5 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75    DataBuffer *ou
33cb6 74 29 7b 0a 20 20 4c 65 61 76 65 73 52 65 61 64  t){.  LeavesRead
33cb7 65 72 20 72 65 61 64 65 72 3b 0a 20 20 69 6e 74  er reader;.  int
33cb8 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
33cb9 6e 44 61 74 61 3e 31 20 29 3b 0a 20 20 61 73 73  nData>1 );.  ass
33cba 65 72 74 28 20 2a 70 44 61 74 61 3d 3d 27 5c 30  ert( *pData=='\0
33cbb 27 20 29 3b 0a 20 20 72 63 20 3d 20 6c 65 61 76  ' );.  rc = leav
33cbc 65 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c 20  esReaderInit(v, 
33cbd 30 2c 20 30 2c 20 30 2c 20 70 44 61 74 61 2c 20  0, 0, 0, pData, 
33cbe 6e 44 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b  nData, &reader);
33cbf 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
33cc0 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
33cc1 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64 53 65  ;..  rc = loadSe
33cc2 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 76  gmentLeavesInt(v
33cc3 2c 20 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d  , &reader, pTerm
33cc4 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
33cc5 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61 76 65  x, out);.  leave
33cc6 73 52 65 61 64 65 72 52 65 73 65 74 28 26 72 65  sReaderReset(&re
33cc7 61 64 65 72 29 3b 0a 20 20 6c 65 61 76 65 73 52  ader);.  leavesR
33cc8 65 61 64 65 72 44 65 73 74 72 6f 79 28 26 72 65  eaderDestroy(&re
33cc9 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  ader);.  return 
33cca 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61 6c 6c 20 6c  rc;.}../* Call l
33ccb 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
33ccc 49 6e 74 28 29 20 77 69 74 68 20 74 68 65 20 6c  Int() with the l
33ccd 65 61 66 20 6e 6f 64 65 73 20 66 72 6f 6d 20 69  eaf nodes from i
33cce 53 74 61 72 74 4c 65 61 66 20 74 6f 0a 2a 2a 20  StartLeaf to.** 
33ccf 69 45 6e 64 4c 65 61 66 20 28 69 6e 63 6c 75 73  iEndLeaf (inclus
33cd0 69 76 65 29 20 61 73 20 69 6e 70 75 74 2c 20 61  ive) as input, a
33cd1 6e 64 20 6d 65 72 67 65 20 74 68 65 20 72 65 73  nd merge the res
33cd2 75 6c 74 69 6e 67 20 64 6f 63 6c 69 73 74 20 69  ulting doclist i
33cd3 6e 74 6f 0a 2a 2a 20 6f 75 74 2e 0a 2a 2f 0a 73  nto.** out..*/.s
33cd4 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65  tatic int loadSe
33cd5 67 6d 65 6e 74 4c 65 61 76 65 73 28 66 75 6c 6c  gmentLeaves(full
33cd6 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20  text_vtab *v,.  
33cd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33cd8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
33cd9 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 4c 65  e_int64 iStartLe
33cda 61 66 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  af, sqlite_int64
33cdb 20 69 45 6e 64 4c 65 61 66 2c 0a 20 20 20 20 20   iEndLeaf,.     
33cdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33cdd 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
33cde 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
33cdf 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
33ce0 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
33ce1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ce2 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74   DataBuffer *out
33ce3 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4c  ){.  int rc;.  L
33ce4 65 61 76 65 73 52 65 61 64 65 72 20 72 65 61 64  eavesReader read
33ce5 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  er;..  assert( i
33ce6 53 74 61 72 74 4c 65 61 66 3c 3d 69 45 6e 64 4c  StartLeaf<=iEndL
33ce7 65 61 66 20 29 3b 0a 20 20 72 63 20 3d 20 6c 65  eaf );.  rc = le
33ce8 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76  avesReaderInit(v
33ce9 2c 20 30 2c 20 69 53 74 61 72 74 4c 65 61 66 2c  , 0, iStartLeaf,
33cea 20 69 45 6e 64 4c 65 61 66 2c 20 4e 55 4c 4c 2c   iEndLeaf, NULL,
33ceb 20 30 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20   0, &reader);.  
33cec 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
33ced 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
33cee 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65    rc = loadSegme
33cef 6e 74 4c 65 61 76 65 73 49 6e 74 28 76 2c 20 26  ntLeavesInt(v, &
33cf0 72 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e  reader, pTerm, n
33cf1 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20  Term, isPrefix, 
33cf2 6f 75 74 29 3b 0a 20 20 6c 65 61 76 65 73 52 65  out);.  leavesRe
33cf3 61 64 65 72 52 65 73 65 74 28 26 72 65 61 64 65  aderReset(&reade
33cf4 72 29 3b 0a 20 20 6c 65 61 76 65 73 52 65 61 64  r);.  leavesRead
33cf5 65 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65  erDestroy(&reade
33cf6 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
33cf7 0a 7d 0a 0a 2f 2a 20 54 61 6b 69 6e 67 20 70 44  .}../* Taking pD
33cf8 61 74 61 2f 6e 44 61 74 61 20 61 73 20 61 6e 20  ata/nData as an 
33cf9 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 20 66  interior node, f
33cfa 69 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 65  ind the sequence
33cfb 20 6f 66 20 63 68 69 6c 64 0a 2a 2a 20 6e 6f 64   of child.** nod
33cfc 65 73 20 77 68 69 63 68 20 63 6f 75 6c 64 20 69  es which could i
33cfd 6e 63 6c 75 64 65 20 70 54 65 72 6d 2f 6e 54 65  nclude pTerm/nTe
33cfe 72 6d 2f 69 73 50 72 65 66 69 78 2e 20 20 4e 6f  rm/isPrefix.  No
33cff 74 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 69  te that the.** i
33d00 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 74 65 72  nterior node ter
33d01 6d 73 20 6c 6f 67 69 63 61 6c 6c 79 20 63 6f 6d  ms logically com
33d02 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 62 6c  e between the bl
33d03 6f 63 6b 73 2c 20 73 6f 20 74 68 65 72 65 20 69  ocks, so there i
33d04 73 0a 2a 2a 20 6f 6e 65 20 6d 6f 72 65 20 62 6c  s.** one more bl
33d05 6f 63 6b 69 64 20 74 68 61 6e 20 74 68 65 72 65  ockid than there
33d06 20 61 72 65 20 74 65 72 6d 73 20 28 74 68 61 74   are terms (that
33d07 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20   block contains 
33d08 74 65 72 6d 73 20 3e 3d 0a 2a 2a 20 74 68 65 20  terms >=.** the 
33d09 6c 61 73 74 20 69 6e 74 65 72 69 6f 72 2d 6e 6f  last interior-no
33d0a 64 65 20 74 65 72 6d 29 2e 0a 2a 2f 0a 2f 2a 20  de term)..*/./* 
33d0b 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20  TODO(shess) The 
33d0c 63 61 6c 6c 69 6e 67 20 63 6f 64 65 20 6d 61 79  calling code may
33d0d 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68   already know th
33d0e 61 74 20 74 68 65 20 65 6e 64 20 63 68 69 6c 64  at the end child
33d0f 20 69 73 0a 2a 2a 20 6e 6f 74 20 77 6f 72 74 68   is.** not worth
33d10 20 63 61 6c 63 75 6c 61 74 69 6e 67 2c 20 62 65   calculating, be
33d11 63 61 75 73 65 20 74 68 65 20 65 6e 64 20 6d 61  cause the end ma
33d12 79 20 62 65 20 69 6e 20 61 20 6c 61 74 65 72 20  y be in a later 
33d13 73 69 62 6c 69 6e 67 0a 2a 2a 20 6e 6f 64 65 2e  sibling.** node.
33d14 20 20 43 6f 6e 73 69 64 65 72 20 77 68 65 74 68    Consider wheth
33d15 65 72 20 62 72 65 61 6b 69 6e 67 20 73 79 6d 6d  er breaking symm
33d16 65 74 72 79 20 69 73 20 77 6f 72 74 68 77 68 69  etry is worthwhi
33d17 6c 65 2e 20 20 49 20 73 75 73 70 65 63 74 0a 2a  le.  I suspect.*
33d18 2a 20 69 74 20 69 73 20 6e 6f 74 20 77 6f 72 74  * it is not wort
33d19 68 77 68 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  hwhile..*/.stati
33d1a 63 20 76 6f 69 64 20 67 65 74 43 68 69 6c 64 72  c void getChildr
33d1b 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 63 6f 6e  enContaining(con
33d1c 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
33d1d 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
33d1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d1f 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
33d20 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
33d21 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
33d22 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
33d23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d24 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
33d25 65 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72 74  e_int64 *piStart
33d26 43 68 69 6c 64 2c 0a 20 20 20 20 20 20 20 20 20  Child,.         
33d27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d28 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
33d29 69 6e 74 36 34 20 2a 70 69 45 6e 64 43 68 69 6c  int64 *piEndChil
33d2a 64 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 52 65  d){.  InteriorRe
33d2b 61 64 65 72 20 72 65 61 64 65 72 3b 0a 0a 20 20  ader reader;..  
33d2c 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 31 20  assert( nData>1 
33d2d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70 44  );.  assert( *pD
33d2e 61 74 61 21 3d 27 5c 30 27 20 29 3b 0a 20 20 69  ata!='\0' );.  i
33d2f 6e 74 65 72 69 6f 72 52 65 61 64 65 72 49 6e 69  nteriorReaderIni
33d30 74 28 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  t(pData, nData, 
33d31 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20  &reader);..  /* 
33d32 53 63 61 6e 20 66 6f 72 20 74 68 65 20 66 69 72  Scan for the fir
33d33 73 74 20 63 68 69 6c 64 20 77 68 69 63 68 20 63  st child which c
33d34 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 20 70 54 65  ould contain pTe
33d35 72 6d 2f 6e 54 65 72 6d 2e 20 2a 2f 0a 20 20 77  rm/nTerm. */.  w
33d36 68 69 6c 65 28 20 21 69 6e 74 65 72 69 6f 72 52  hile( !interiorR
33d37 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64  eaderAtEnd(&read
33d38 65 72 29 20 29 7b 0a 20 20 20 20 69 66 28 20 69  er) ){.    if( i
33d39 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72  nteriorReaderTer
33d3a 6d 43 6d 70 28 26 72 65 61 64 65 72 2c 20 70 54  mCmp(&reader, pT
33d3b 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 29 3e 30  erm, nTerm, 0)>0
33d3c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 6e   ) break;.    in
33d3d 74 65 72 69 6f 72 52 65 61 64 65 72 53 74 65 70  teriorReaderStep
33d3e 28 26 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  (&reader);.  }. 
33d3f 20 2a 70 69 53 74 61 72 74 43 68 69 6c 64 20 3d   *piStartChild =
33d40 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
33d41 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72  urrentBlockid(&r
33d42 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 4b 65  eader);..  /* Ke
33d43 65 70 20 73 63 61 6e 6e 69 6e 67 20 74 6f 20 66  ep scanning to f
33d44 69 6e 64 20 61 20 74 65 72 6d 20 67 72 65 61 74  ind a term great
33d45 65 72 20 74 68 61 6e 20 6f 75 72 20 74 65 72 6d  er than our term
33d46 2c 20 75 73 69 6e 67 20 70 72 65 66 69 78 0a 20  , using prefix. 
33d47 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69   ** comparison i
33d48 66 20 69 6e 64 69 63 61 74 65 64 2e 20 20 49 66  f indicated.  If
33d49 20 69 73 50 72 65 66 69 78 20 69 73 20 66 61 6c   isPrefix is fal
33d4a 73 65 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65  se, this will be
33d4b 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 62   the.  ** same b
33d4c 6c 6f 63 6b 69 64 20 61 73 20 74 68 65 20 73 74  lockid as the st
33d4d 61 72 74 69 6e 67 20 62 6c 6f 63 6b 2e 0a 20 20  arting block..  
33d4e 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21 69 6e 74  */.  while( !int
33d4f 65 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64  eriorReaderAtEnd
33d50 28 26 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20  (&reader) ){.   
33d51 20 69 66 28 20 69 6e 74 65 72 69 6f 72 52 65 61   if( interiorRea
33d52 64 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64  derTermCmp(&read
33d53 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  er, pTerm, nTerm
33d54 2c 20 69 73 50 72 65 66 69 78 29 3e 30 20 29 20  , isPrefix)>0 ) 
33d55 62 72 65 61 6b 3b 0a 20 20 20 20 69 6e 74 65 72  break;.    inter
33d56 69 6f 72 52 65 61 64 65 72 53 74 65 70 28 26 72  iorReaderStep(&r
33d57 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20 2a 70  eader);.  }.  *p
33d58 69 45 6e 64 43 68 69 6c 64 20 3d 20 69 6e 74 65  iEndChild = inte
33d59 72 69 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e  riorReaderCurren
33d5a 74 42 6c 6f 63 6b 69 64 28 26 72 65 61 64 65 72  tBlockid(&reader
33d5b 29 3b 0a 0a 20 20 69 6e 74 65 72 69 6f 72 52 65  );..  interiorRe
33d5c 61 64 65 72 44 65 73 74 72 6f 79 28 26 72 65 61  aderDestroy(&rea
33d5d 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 43 68 69 6c  der);..  /* Chil
33d5e 64 72 65 6e 20 6d 75 73 74 20 61 73 63 65 6e 64  dren must ascend
33d5f 2c 20 61 6e 64 20 69 66 20 21 70 72 65 66 69 78  , and if !prefix
33d60 2c 20 62 6f 74 68 20 6d 75 73 74 20 62 65 20 74  , both must be t
33d61 68 65 20 73 61 6d 65 2e 20 2a 2f 0a 20 20 61 73  he same. */.  as
33d62 73 65 72 74 28 20 2a 70 69 45 6e 64 43 68 69 6c  sert( *piEndChil
33d63 64 3e 3d 2a 70 69 53 74 61 72 74 43 68 69 6c 64  d>=*piStartChild
33d64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73   );.  assert( is
33d65 50 72 65 66 69 78 20 7c 7c 20 2a 70 69 53 74 61  Prefix || *piSta
33d66 72 74 43 68 69 6c 64 3d 3d 2a 70 69 45 6e 64 43  rtChild==*piEndC
33d67 68 69 6c 64 20 29 3b 0a 7d 0a 0a 2f 2a 20 52 65  hild );.}../* Re
33d68 61 64 20 62 6c 6f 63 6b 20 61 74 20 69 42 6c 6f  ad block at iBlo
33d69 63 6b 69 64 20 61 6e 64 20 70 61 73 73 20 69 74  ckid and pass it
33d6a 20 77 69 74 68 20 6f 74 68 65 72 20 70 61 72 61   with other para
33d6b 6d 73 20 74 6f 0a 2a 2a 20 67 65 74 43 68 69 6c  ms to.** getChil
33d6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 29  drenContaining()
33d6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
33d6e 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c 64 72  loadAndGetChildr
33d6f 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 0a 20 20  enContaining(.  
33d70 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
33d71 2c 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ,.  sqlite_int64
33d72 20 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 63 6f 6e   iBlockid,.  con
33d73 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
33d74 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
33d75 73 50 72 65 66 69 78 2c 0a 20 20 73 71 6c 69 74  sPrefix,.  sqlit
33d76 65 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72 74  e_int64 *piStart
33d77 43 68 69 6c 64 2c 20 73 71 6c 69 74 65 5f 69 6e  Child, sqlite_in
33d78 74 36 34 20 2a 70 69 45 6e 64 43 68 69 6c 64 0a  t64 *piEndChild.
33d79 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
33d7a 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69  t *s = NULL;.  i
33d7b 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
33d7c 28 20 69 42 6c 6f 63 6b 69 64 21 3d 30 20 29 3b  ( iBlockid!=0 );
33d7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d  .  assert( pTerm
33d7e 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65  !=NULL );.  asse
33d7f 72 74 28 20 6e 54 65 72 6d 21 3d 30 20 29 3b 20  rt( nTerm!=0 ); 
33d80 20 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73         /* TODO(s
33d81 68 65 73 73 29 20 57 68 79 20 6e 6f 74 20 61 6c  hess) Why not al
33d82 6c 6f 77 20 74 68 69 73 3f 20 2a 2f 0a 20 20 61  low this? */.  a
33d83 73 73 65 72 74 28 20 70 69 53 74 61 72 74 43 68  ssert( piStartCh
33d84 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61  ild!=NULL );.  a
33d85 73 73 65 72 74 28 20 70 69 45 6e 64 43 68 69 6c  ssert( piEndChil
33d86 64 21 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 72 63  d!=NULL );..  rc
33d87 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
33d88 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 53 45  ment(v, BLOCK_SE
33d89 4c 45 43 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  LECT_STMT, &s);.
33d8a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33d8b 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
33d8c 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
33d8d 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31  _bind_int64(s, 1
33d8e 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69  , iBlockid);.  i
33d8f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
33d90 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
33d91 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
33d92 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d  ep(s);.  if( rc=
33d93 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72  =SQLITE_DONE ) r
33d94 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
33d95 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  OR;.  if( rc!=SQ
33d96 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
33d97 6e 20 72 63 3b 0a 0a 20 20 67 65 74 43 68 69 6c  n rc;..  getChil
33d98 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 73  drenContaining(s
33d99 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
33d9a 6f 62 28 73 2c 20 30 29 2c 20 73 71 6c 69 74 65  ob(s, 0), sqlite
33d9b 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73  3_column_bytes(s
33d9c 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0),.          
33d9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
33d9e 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
33d9f 65 66 69 78 2c 20 70 69 53 74 61 72 74 43 68 69  efix, piStartChi
33da0 6c 64 2c 20 70 69 45 6e 64 43 68 69 6c 64 29 3b  ld, piEndChild);
33da1 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74  ..  /* We expect
33da2 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20   only one row.  
33da3 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20  We must execute 
33da4 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f  another sqlite3_
33da5 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63  step().   * to c
33da6 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72  omplete the iter
33da7 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65  ation; otherwise
33da8 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20   the table will 
33da9 72 65 6d 61 69 6e 0a 20 20 20 2a 20 6c 6f 63 6b  remain.   * lock
33daa 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ed. */.  rc = sq
33dab 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
33dac 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
33dad 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ROW ) return SQL
33dae 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28  ITE_ERROR;.  if(
33daf 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
33db0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
33db1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
33db2 4b 3b 0a 7d 0a 0a 2f 2a 20 54 72 61 76 65 72 73  K;.}../* Travers
33db3 65 20 74 68 65 20 74 72 65 65 20 72 65 70 72 65  e the tree repre
33db4 73 65 6e 74 65 64 20 62 79 20 70 44 61 74 61 5b  sented by pData[
33db5 6e 44 61 74 61 5d 20 6c 6f 6f 6b 69 6e 67 20 66  nData] looking f
33db6 6f 72 0a 2a 2a 20 70 54 65 72 6d 5b 6e 54 65 72  or.** pTerm[nTer
33db7 6d 5d 2c 20 70 6c 61 63 69 6e 67 20 69 74 73 20  m], placing its 
33db8 64 6f 63 6c 69 73 74 20 69 6e 74 6f 20 2a 6f 75  doclist into *ou
33db9 74 2e 20 20 54 68 69 73 20 69 73 20 69 6e 74 65  t.  This is inte
33dba 72 6e 61 6c 20 74 6f 0a 2a 2a 20 6c 6f 61 64 53  rnal to.** loadS
33dbb 65 67 6d 65 6e 74 28 29 20 74 6f 20 6d 61 6b 65  egment() to make
33dbc 20 65 72 72 6f 72 2d 68 61 6e 64 6c 69 6e 67 20   error-handling 
33dbd 63 6c 65 61 6e 65 72 2e 0a 2a 2f 0a 73 74 61 74  cleaner..*/.stat
33dbe 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65  ic int loadSegme
33dbf 6e 74 49 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76  ntInt(fulltext_v
33dc0 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68  tab *v, const ch
33dc1 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
33dc2 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
33dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33dc4 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 65  sqlite_int64 iLe
33dc5 61 76 65 73 45 6e 64 2c 0a 20 20 20 20 20 20 20  avesEnd,.       
33dc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33dc7 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
33dc8 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c  Term, int nTerm,
33dc9 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20   int isPrefix,. 
33dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33dcb 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66           DataBuf
33dcc 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20  fer *out){.  /* 
33dcd 53 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65  Special case whe
33dce 72 65 20 72 6f 6f 74 20 69 73 20 61 20 6c 65 61  re root is a lea
33dcf 66 2e 20 2a 2f 0a 20 20 69 66 28 20 2a 70 44 61  f. */.  if( *pDa
33dd0 74 61 3d 3d 27 5c 30 27 20 29 7b 0a 20 20 20 20  ta=='\0' ){.    
33dd1 72 65 74 75 72 6e 20 6c 6f 61 64 53 65 67 6d 65  return loadSegme
33dd2 6e 74 4c 65 61 66 28 76 2c 20 70 44 61 74 61 2c  ntLeaf(v, pData,
33dd3 20 6e 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e   nData, pTerm, n
33dd4 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20  Term, isPrefix, 
33dd5 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  out);.  }else{. 
33dd6 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73     int rc;.    s
33dd7 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61  qlite_int64 iSta
33dd8 72 74 43 68 69 6c 64 2c 20 69 45 6e 64 43 68 69  rtChild, iEndChi
33dd9 6c 64 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63  ld;..    /* Proc
33dda 65 73 73 20 70 44 61 74 61 20 61 73 20 61 6e 20  ess pData as an 
33ddb 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 20 74  interior node, t
33ddc 68 65 6e 20 6c 6f 6f 70 20 64 6f 77 6e 20 74 68  hen loop down th
33ddd 65 20 74 72 65 65 0a 20 20 20 20 2a 2a 20 75 6e  e tree.    ** un
33dde 74 69 6c 20 77 65 20 66 69 6e 64 20 74 68 65 20  til we find the 
33ddf 73 65 74 20 6f 66 20 6c 65 61 66 20 6e 6f 64 65  set of leaf node
33de0 73 20 74 6f 20 73 63 61 6e 20 66 6f 72 20 74 68  s to scan for th
33de1 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20  e term..    */. 
33de2 20 20 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f     getChildrenCo
33de3 6e 74 61 69 6e 69 6e 67 28 70 44 61 74 61 2c 20  ntaining(pData, 
33de4 6e 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e 54  nData, pTerm, nT
33de5 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 0a 20  erm, isPrefix,. 
33de6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33de7 20 20 20 20 20 20 20 20 20 26 69 53 74 61 72 74           &iStart
33de8 43 68 69 6c 64 2c 20 26 69 45 6e 64 43 68 69 6c  Child, &iEndChil
33de9 64 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69  d);.    while( i
33dea 53 74 61 72 74 43 68 69 6c 64 3e 69 4c 65 61 76  StartChild>iLeav
33deb 65 73 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 73  esEnd ){.      s
33dec 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4e 65 78  qlite_int64 iNex
33ded 74 53 74 61 72 74 2c 20 69 4e 65 78 74 45 6e 64  tStart, iNextEnd
33dee 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 61  ;.      rc = loa
33def 64 41 6e 64 47 65 74 43 68 69 6c 64 72 65 6e 43  dAndGetChildrenC
33df0 6f 6e 74 61 69 6e 69 6e 67 28 76 2c 20 69 53 74  ontaining(v, iSt
33df1 61 72 74 43 68 69 6c 64 2c 20 70 54 65 72 6d 2c  artChild, pTerm,
33df2 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
33df3 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33df4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33df5 20 20 20 20 20 20 20 20 20 20 26 69 4e 65 78 74            &iNext
33df6 53 74 61 72 74 2c 20 26 69 4e 65 78 74 45 6e 64  Start, &iNextEnd
33df7 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
33df8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
33df9 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 20 20 2f  urn rc;..      /
33dfa 2a 20 49 66 20 77 65 27 76 65 20 62 72 61 6e 63  * If we've branc
33dfb 68 65 64 2c 20 66 6f 6c 6c 6f 77 20 74 68 65 20  hed, follow the 
33dfc 65 6e 64 20 62 72 61 6e 63 68 2c 20 74 6f 6f 2e  end branch, too.
33dfd 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 53   */.      if( iS
33dfe 74 61 72 74 43 68 69 6c 64 21 3d 69 45 6e 64 43  tartChild!=iEndC
33dff 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20  hild ){.        
33e00 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75  sqlite_int64 iDu
33e01 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 72 63 20  mmy;.        rc 
33e02 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c  = loadAndGetChil
33e03 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76  drenContaining(v
33e04 2c 20 69 45 6e 64 43 68 69 6c 64 2c 20 70 54 65  , iEndChild, pTe
33e05 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
33e06 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fix,.           
33e07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
33e09 69 44 75 6d 6d 79 2c 20 26 69 4e 65 78 74 45 6e  iDummy, &iNextEn
33e0a 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
33e0b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
33e0c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
33e0d 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74   }..      assert
33e0e 28 20 69 4e 65 78 74 53 74 61 72 74 3c 3d 69 4e  ( iNextStart<=iN
33e0f 65 78 74 45 6e 64 20 29 3b 0a 20 20 20 20 20 20  extEnd );.      
33e10 69 53 74 61 72 74 43 68 69 6c 64 20 3d 20 69 4e  iStartChild = iN
33e11 65 78 74 53 74 61 72 74 3b 0a 20 20 20 20 20 20  extStart;.      
33e12 69 45 6e 64 43 68 69 6c 64 20 3d 20 69 4e 65 78  iEndChild = iNex
33e13 74 45 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  tEnd;.    }.    
33e14 61 73 73 65 72 74 28 20 69 53 74 61 72 74 43 68  assert( iStartCh
33e15 69 6c 64 3c 3d 69 4c 65 61 76 65 73 45 6e 64 20  ild<=iLeavesEnd 
33e16 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
33e17 45 6e 64 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65  EndChild<=iLeave
33e18 73 45 6e 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  sEnd );..    /* 
33e19 53 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65  Scan through the
33e1a 20 6c 65 61 66 20 73 65 67 6d 65 6e 74 73 20 66   leaf segments f
33e1b 6f 72 20 64 6f 63 6c 69 73 74 73 2e 20 2a 2f 0a  or doclists. */.
33e1c 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64 53      return loadS
33e1d 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 76 2c 20  egmentLeaves(v, 
33e1e 69 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45 6e  iStartChild, iEn
33e1f 64 43 68 69 6c 64 2c 0a 20 20 20 20 20 20 20 20  dChild,.        
33e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e21 20 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72       pTerm, nTer
33e22 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 6f 75 74  m, isPrefix, out
33e23 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 43 61 6c  );.  }.}../* Cal
33e24 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74  l loadSegmentInt
33e25 28 29 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 68  () to collect th
33e26 65 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 70 54  e doclist for pT
33e27 65 72 6d 2f 6e 54 65 72 6d 2c 20 74 68 65 6e 0a  erm/nTerm, then.
33e28 2a 2a 20 6d 65 72 67 65 20 69 74 73 20 64 6f 63  ** merge its doc
33e29 6c 69 73 74 20 6f 76 65 72 20 2a 6f 75 74 20 28  list over *out (
33e2a 61 6e 79 20 64 75 70 6c 69 63 61 74 65 20 64 6f  any duplicate do
33e2b 63 6c 69 73 74 73 20 72 65 61 64 20 66 72 6f 6d  clists read from
33e2c 20 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 20   the.** segment 
33e2d 72 6f 6f 74 65 64 20 61 74 20 70 44 61 74 61 20  rooted at pData 
33e2e 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74  will overwrite t
33e2f 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e 0a 2a  hose in *out)..*
33e30 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
33e31 20 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67 69   Consider changi
33e32 6e 67 20 74 68 69 73 20 74 6f 20 64 65 74 65 72  ng this to deter
33e33 6d 69 6e 65 20 74 68 65 20 64 65 70 74 68 20 6f  mine the depth o
33e34 66 20 74 68 65 0a 2a 2a 20 6c 65 61 76 65 73 20  f the.** leaves 
33e35 75 73 69 6e 67 20 65 69 74 68 65 72 20 74 68 65  using either the
33e36 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
33e37 73 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s of interior no
33e38 64 65 73 20 28 77 68 65 6e 0a 2a 2a 20 3d 3d 31  des (when.** ==1
33e39 2c 20 77 65 27 72 65 20 6f 6e 65 20 6c 65 76 65  , we're one leve
33e3a 6c 20 61 62 6f 76 65 20 74 68 65 20 6c 65 61 76  l above the leav
33e3b 65 73 29 2c 20 6f 72 20 74 68 65 20 66 69 72 73  es), or the firs
33e3c 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 0a 2a  t character of.*
33e3d 2a 20 74 68 65 20 72 6f 6f 74 20 28 77 68 69 63  * the root (whic
33e3e 68 20 77 69 6c 6c 20 64 65 73 63 72 69 62 65 20  h will describe 
33e3f 74 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 68  the height of th
33e40 65 20 74 72 65 65 20 64 69 72 65 63 74 6c 79 29  e tree directly)
33e41 2e 0a 2a 2a 20 45 69 74 68 65 72 20 66 65 65 6c  ..** Either feel
33e42 73 20 73 6f 6d 65 77 68 61 74 20 74 72 69 63 6b  s somewhat trick
33e43 79 20 74 6f 20 6d 65 2e 0a 2a 2f 0a 2f 2a 20 54  y to me..*/./* T
33e44 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 63  ODO(shess) The c
33e45 75 72 72 65 6e 74 20 6d 65 72 67 65 20 69 73 20  urrent merge is 
33e46 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 73 6c 6f  likely to be slo
33e47 77 20 66 6f 72 20 6c 61 72 67 65 0a 2a 2a 20 64  w for large.** d
33e48 6f 63 6c 69 73 74 73 20 28 74 68 6f 75 67 68 20  oclists (though 
33e49 69 74 20 73 68 6f 75 6c 64 20 70 72 6f 63 65 73  it should proces
33e4a 73 20 66 72 6f 6d 20 6e 65 77 65 73 74 2f 73 6d  s from newest/sm
33e4b 61 6c 6c 65 73 74 20 74 6f 0a 2a 2a 20 6f 6c 64  allest to.** old
33e4c 65 73 74 2f 6c 61 72 67 65 73 74 2c 20 73 6f 20  est/largest, so 
33e4d 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68  it may not be th
33e4e 61 74 20 62 61 64 29 2e 20 20 49 74 20 6d 69 67  at bad).  It mig
33e4f 68 74 20 62 65 20 75 73 65 66 75 6c 20 74 6f 0a  ht be useful to.
33e50 2a 2a 20 6d 6f 64 69 66 79 20 74 68 69 6e 67 73  ** modify things
33e51 20 74 6f 20 61 6c 6c 6f 77 20 66 6f 72 20 4e 2d   to allow for N-
33e52 77 61 79 20 6d 65 72 67 69 6e 67 2e 20 20 54 68  way merging.  Th
33e53 69 73 20 63 6f 75 6c 64 20 65 69 74 68 65 72 20  is could either 
33e54 62 65 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 73  be.** within a s
33e55 65 67 6d 65 6e 74 2c 20 77 69 74 68 20 70 61 69  egment, with pai
33e56 72 77 69 73 65 20 6d 65 72 67 65 73 20 61 63 72  rwise merges acr
33e57 6f 73 73 20 73 65 67 6d 65 6e 74 73 2c 20 6f 72  oss segments, or
33e58 20 61 63 72 6f 73 73 0a 2a 2a 20 61 6c 6c 20 73   across.** all s
33e59 65 67 6d 65 6e 74 73 20 61 74 20 6f 6e 63 65 2e  egments at once.
33e5a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
33e5b 6f 61 64 53 65 67 6d 65 6e 74 28 66 75 6c 6c 74  oadSegment(fullt
33e5c 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e  ext_vtab *v, con
33e5d 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
33e5e 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
33e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e60 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
33e61 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20 20 20 20  LeavesEnd,.     
33e62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e63 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
33e64 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
33e65 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
33e66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e67 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
33e68 2a 6f 75 74 29 7b 0a 20 20 44 61 74 61 42 75 66  *out){.  DataBuf
33e69 66 65 72 20 72 65 73 75 6c 74 3b 0a 20 20 69 6e  fer result;.  in
33e6a 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
33e6b 20 6e 44 61 74 61 3e 31 20 29 3b 0a 0a 20 20 2f   nData>1 );..  /
33e6c 2a 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f 75  * This code shou
33e6d 6c 64 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c  ld never be call
33e6e 65 64 20 77 69 74 68 20 62 75 66 66 65 72 65 64  ed with buffered
33e6f 20 75 70 64 61 74 65 73 2e 20 2a 2f 0a 20 20 61   updates. */.  a
33e70 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69  ssert( v->nPendi
33e71 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20 20 64  ngData<0 );..  d
33e72 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 72  ataBufferInit(&r
33e73 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 72 63 20  esult, 0);.  rc 
33e74 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74  = loadSegmentInt
33e75 28 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  (v, pData, nData
33e76 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20  , iLeavesEnd,.  
33e77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e78 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d      pTerm, nTerm
33e79 2c 20 69 73 50 72 65 66 69 78 2c 20 26 72 65 73  , isPrefix, &res
33e7a 75 6c 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ult);.  if( rc==
33e7b 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73  SQLITE_OK && res
33e7c 75 6c 74 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20  ult.nData>0 ){. 
33e7d 20 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61 74     if( out->nDat
33e7e 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 44 61  a==0 ){.      Da
33e7f 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20 2a  taBuffer tmp = *
33e80 6f 75 74 3b 0a 20 20 20 20 20 20 2a 6f 75 74 20  out;.      *out 
33e81 3d 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 20 20  = result;.      
33e82 72 65 73 75 6c 74 20 3d 20 74 6d 70 3b 0a 20 20  result = tmp;.  
33e83 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 44    }else{.      D
33e84 61 74 61 42 75 66 66 65 72 20 6d 65 72 67 65 64  ataBuffer merged
33e85 3b 0a 20 20 20 20 20 20 44 4c 52 65 61 64 65 72  ;.      DLReader
33e86 20 72 65 61 64 65 72 73 5b 32 5d 3b 0a 0a 20 20   readers[2];..  
33e87 20 20 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61      dlrInit(&rea
33e88 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46 41  ders[0], DL_DEFA
33e89 55 4c 54 2c 20 6f 75 74 2d 3e 70 44 61 74 61 2c  ULT, out->pData,
33e8a 20 6f 75 74 2d 3e 6e 44 61 74 61 29 3b 0a 20 20   out->nData);.  
33e8b 20 20 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61      dlrInit(&rea
33e8c 64 65 72 73 5b 31 5d 2c 20 44 4c 5f 44 45 46 41  ders[1], DL_DEFA
33e8d 55 4c 54 2c 20 72 65 73 75 6c 74 2e 70 44 61 74  ULT, result.pDat
33e8e 61 2c 20 72 65 73 75 6c 74 2e 6e 44 61 74 61 29  a, result.nData)
33e8f 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
33e90 65 72 49 6e 69 74 28 26 6d 65 72 67 65 64 2c 20  erInit(&merged, 
33e91 6f 75 74 2d 3e 6e 44 61 74 61 2b 72 65 73 75 6c  out->nData+resul
33e92 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  t.nData);.      
33e93 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26 6d 65  docListMerge(&me
33e94 72 67 65 64 2c 20 72 65 61 64 65 72 73 2c 20 32  rged, readers, 2
33e95 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  );.      dataBuf
33e96 66 65 72 44 65 73 74 72 6f 79 28 6f 75 74 29 3b  ferDestroy(out);
33e97 0a 20 20 20 20 20 20 2a 6f 75 74 20 3d 20 6d 65  .      *out = me
33e98 72 67 65 64 3b 0a 20 20 20 20 20 20 64 6c 72 44  rged;.      dlrD
33e99 65 73 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b  estroy(&readers[
33e9a 30 5d 29 3b 0a 20 20 20 20 20 20 64 6c 72 44 65  0]);.      dlrDe
33e9b 73 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b 31  stroy(&readers[1
33e9c 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ]);.    }.  }.  
33e9d 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
33e9e 79 28 26 72 65 73 75 6c 74 29 3b 0a 20 20 72 65  y(&result);.  re
33e9f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 53  turn rc;.}../* S
33ea0 63 61 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  can the database
33ea1 20 61 6e 64 20 6d 65 72 67 65 20 74 6f 67 65 74   and merge toget
33ea2 68 65 72 20 74 68 65 20 70 6f 73 74 69 6e 67 20  her the posting 
33ea3 6c 69 73 74 73 20 66 6f 72 20 74 68 65 20 74 65  lists for the te
33ea4 72 6d 0a 2a 2a 20 69 6e 74 6f 20 2a 6f 75 74 2e  rm.** into *out.
33ea5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
33ea6 65 72 6d 53 65 6c 65 63 74 28 0a 20 20 66 75 6c  ermSelect(.  ful
33ea7 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 0a  ltext_vtab *v, .
33ea8 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20    int iColumn,. 
33ea9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
33eaa 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20  rm, int nTerm,  
33eab 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
33eac 72 6d 20 74 6f 20 71 75 65 72 79 20 66 6f 72 20  rm to query for 
33ead 2a 2f 0a 20 20 69 6e 74 20 69 73 50 72 65 66 69  */.  int isPrefi
33eae 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
33eaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33eb0 2a 20 54 72 75 65 20 66 6f 72 20 61 20 70 72 65  * True for a pre
33eb1 66 69 78 20 73 65 61 72 63 68 20 2a 2f 0a 20 20  fix search */.  
33eb2 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
33eb3 65 2c 20 0a 20 20 44 61 74 61 42 75 66 66 65 72  e, .  DataBuffer
33eb4 20 2a 6f 75 74 20 20 20 20 20 20 20 20 20 20 20   *out           
33eb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33eb6 2f 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74 73  /* Write results
33eb7 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 61   here */.){.  Da
33eb8 74 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74  taBuffer doclist
33eb9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
33eba 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   *s;.  int rc = 
33ebb 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
33ebc 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45  t(v, SEGDIR_SELE
33ebd 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29  CT_ALL_STMT, &s)
33ebe 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
33ebf 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
33ec0 63 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f  c;..  /* This co
33ec1 64 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  de should never 
33ec2 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62  be called with b
33ec3 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73 2e  uffered updates.
33ec4 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d   */.  assert( v-
33ec5 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20  >nPendingData<0 
33ec6 29 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72  );..  dataBuffer
33ec7 49 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 30  Init(&doclist, 0
33ec8 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  );.  dataBufferI
33ec9 6e 69 74 28 6f 75 74 2c 20 30 29 3b 0a 0a 20 20  nit(out, 0);..  
33eca 2f 2a 20 54 72 61 76 65 72 73 65 20 74 68 65 20  /* Traverse the 
33ecb 73 65 67 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c  segments from ol
33ecc 64 65 73 74 20 74 6f 20 6e 65 77 65 73 74 20 73  dest to newest s
33ecd 6f 20 74 68 61 74 20 6e 65 77 65 72 20 64 6f 63  o that newer doc
33ece 6c 69 73 74 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e  list.  ** elemen
33ecf 74 73 20 66 6f 72 20 67 69 76 65 6e 20 64 6f 63  ts for given doc
33ed0 69 64 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c  ids overwrite ol
33ed1 64 65 72 20 65 6c 65 6d 65 6e 74 73 2e 0a 20 20  der elements..  
33ed2 2a 2f 0a 20 20 77 68 69 6c 65 28 20 28 72 63 20  */.  while( (rc 
33ed3 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
33ed4 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  ))==SQLITE_ROW )
33ed5 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
33ed6 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65   *pData = sqlite
33ed7 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c  3_column_blob(s,
33ed8 20 32 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69   2);.    const i
33ed9 6e 74 20 6e 44 61 74 61 20 3d 20 73 71 6c 69 74  nt nData = sqlit
33eda 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
33edb 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f 6e 73 74  s, 2);.    const
33edc 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c   sqlite_int64 iL
33edd 65 61 76 65 73 45 6e 64 20 3d 20 73 71 6c 69 74  eavesEnd = sqlit
33ede 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
33edf 73 2c 20 31 29 3b 0a 20 20 20 20 72 63 20 3d 20  s, 1);.    rc = 
33ee0 6c 6f 61 64 53 65 67 6d 65 6e 74 28 76 2c 20 70  loadSegment(v, p
33ee1 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65  Data, nData, iLe
33ee2 61 76 65 73 45 6e 64 2c 20 70 54 65 72 6d 2c 20  avesEnd, pTerm, 
33ee3 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c  nTerm, isPrefix,
33ee4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33ee5 20 20 20 20 20 20 26 64 6f 63 6c 69 73 74 29 3b        &doclist);
33ee6 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
33ee7 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
33ee8 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  r;.  }.  if( rc=
33ee9 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
33eea 20 20 20 20 69 66 28 20 64 6f 63 6c 69 73 74 2e      if( doclist.
33eeb 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20  nData!=0 ){.    
33eec 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
33eed 20 54 68 65 20 6f 6c 64 20 74 65 72 6d 5f 73 65   The old term_se
33eee 6c 65 63 74 5f 61 6c 6c 28 29 20 63 6f 64 65 20  lect_all() code 
33eef 61 70 70 6c 69 65 64 20 74 68 65 20 63 6f 6c 75  applied the colu
33ef0 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 72 65 73 74  mn.      ** rest
33ef1 72 69 63 74 20 61 73 20 77 65 20 6d 65 72 67 65  rict as we merge
33ef2 64 20 73 65 67 6d 65 6e 74 73 2c 20 6c 65 61 64  d segments, lead
33ef3 69 6e 67 20 74 6f 20 73 6d 61 6c 6c 65 72 20 62  ing to smaller b
33ef4 75 66 66 65 72 73 2e 0a 20 20 20 20 20 20 2a 2a  uffers..      **
33ef5 20 54 68 69 73 20 69 73 20 70 72 6f 62 61 62 6c   This is probabl
33ef6 79 20 77 6f 72 74 68 77 68 69 6c 65 20 74 6f 20  y worthwhile to 
33ef7 62 72 69 6e 67 20 62 61 63 6b 2c 20 6f 6e 63 65  bring back, once
33ef8 20 74 68 65 20 6e 65 77 20 73 74 6f 72 61 67 65   the new storage
33ef9 0a 20 20 20 20 20 20 2a 2a 20 73 79 73 74 65 6d  .      ** system
33efa 20 69 73 20 63 68 65 63 6b 65 64 20 69 6e 2e 0a   is checked in..
33efb 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
33efc 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e  f( iColumn==v->n
33efd 43 6f 6c 75 6d 6e 29 20 69 43 6f 6c 75 6d 6e 20  Column) iColumn 
33efe 3d 20 2d 31 3b 0a 20 20 20 20 20 20 64 6f 63 4c  = -1;.      docL
33eff 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41 55  istTrim(DL_DEFAU
33f00 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74  LT, doclist.pDat
33f01 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61  a, doclist.nData
33f02 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33f03 20 20 20 20 69 43 6f 6c 75 6d 6e 2c 20 69 54 79      iColumn, iTy
33f04 70 65 2c 20 6f 75 74 29 3b 0a 20 20 20 20 7d 0a  pe, out);.    }.
33f05 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
33f06 4f 4b 3b 0a 20 20 7d 0a 0a 20 65 72 72 3a 0a 20  OK;.  }.. err:. 
33f07 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
33f08 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20  oy(&doclist);.  
33f09 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
33f0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
33f0e 0a 2f 2a 20 55 73 65 64 20 74 6f 20 68 6f 6c 64  ./* Used to hold
33f0f 20 68 61 73 68 74 61 62 6c 65 20 64 61 74 61 20   hashtable data 
33f10 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a  for sorting. */.
33f11 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54  typedef struct T
33f12 65 72 6d 44 61 74 61 20 7b 0a 20 20 63 6f 6e 73  ermData {.  cons
33f13 74 20 63 68 61 72 20 2a 70 54 65 72 6d 3b 0a 20  t char *pTerm;. 
33f14 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 44 4c   int nTerm;.  DL
33f15 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c  Collector *pColl
33f16 65 63 74 6f 72 3b 0a 7d 20 54 65 72 6d 44 61 74  ector;.} TermDat
33f17 61 3b 0a 0a 2f 2a 20 4f 72 64 65 72 73 20 54 65  a;../* Orders Te
33f18 72 6d 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20  rmData elements 
33f19 69 6e 20 73 74 72 63 6d 70 20 66 61 73 68 69 6f  in strcmp fashio
33f1a 6e 20 28 20 3c 30 20 66 6f 72 20 6c 65 73 73 2d  n ( <0 for less-
33f1b 74 68 61 6e 2c 20 30 0a 2a 2a 20 66 6f 72 20 65  than, 0.** for e
33f1c 71 75 61 6c 2c 20 3e 30 20 66 6f 72 20 67 72 65  qual, >0 for gre
33f1d 61 74 65 72 2d 74 68 61 6e 29 2e 0a 2a 2f 0a 73  ater-than)..*/.s
33f1e 74 61 74 69 63 20 69 6e 74 20 74 65 72 6d 44 61  tatic int termDa
33f1f 74 61 43 6d 70 28 63 6f 6e 73 74 20 76 6f 69 64  taCmp(const void
33f20 20 2a 61 76 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *av, const void
33f21 20 2a 62 76 29 7b 0a 20 20 63 6f 6e 73 74 20 54   *bv){.  const T
33f22 65 72 6d 44 61 74 61 20 2a 61 20 3d 20 28 63 6f  ermData *a = (co
33f23 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29 61  nst TermData *)a
33f24 76 3b 0a 20 20 63 6f 6e 73 74 20 54 65 72 6d 44  v;.  const TermD
33f25 61 74 61 20 2a 62 20 3d 20 28 63 6f 6e 73 74 20  ata *b = (const 
33f26 54 65 72 6d 44 61 74 61 20 2a 29 62 76 3b 0a 20  TermData *)bv;. 
33f27 20 69 6e 74 20 6e 20 3d 20 61 2d 3e 6e 54 65 72   int n = a->nTer
33f28 6d 3c 62 2d 3e 6e 54 65 72 6d 20 3f 20 61 2d 3e  m<b->nTerm ? a->
33f29 6e 54 65 72 6d 20 3a 20 62 2d 3e 6e 54 65 72 6d  nTerm : b->nTerm
33f2a 3b 0a 20 20 69 6e 74 20 63 20 3d 20 6d 65 6d 63  ;.  int c = memc
33f2b 6d 70 28 61 2d 3e 70 54 65 72 6d 2c 20 62 2d 3e  mp(a->pTerm, b->
33f2c 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20 69 66 28  pTerm, n);.  if(
33f2d 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 63   c!=0 ) return c
33f2e 3b 0a 20 20 72 65 74 75 72 6e 20 61 2d 3e 6e 54  ;.  return a->nT
33f2f 65 72 6d 2d 62 2d 3e 6e 54 65 72 6d 3b 0a 7d 0a  erm-b->nTerm;.}.
33f30 0a 2f 2a 20 4f 72 64 65 72 20 70 54 65 72 6d 73  ./* Order pTerms
33f31 20 64 61 74 61 20 62 79 20 74 65 72 6d 2c 20 74   data by term, t
33f32 68 65 6e 20 77 72 69 74 65 20 61 20 6e 65 77 20  hen write a new 
33f33 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 20  level 0 segment 
33f34 75 73 69 6e 67 0a 2a 2a 20 4c 65 61 66 57 72 69  using.** LeafWri
33f35 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ter..*/.static i
33f36 6e 74 20 77 72 69 74 65 5a 65 72 6f 53 65 67 6d  nt writeZeroSegm
33f37 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ent(fulltext_vta
33f38 62 20 2a 76 2c 20 66 74 73 33 48 61 73 68 20 2a  b *v, fts3Hash *
33f39 70 54 65 72 6d 73 29 7b 0a 20 20 66 74 73 33 48  pTerms){.  fts3H
33f3a 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 69 6e  ashElem *e;.  in
33f3b 74 20 69 64 78 2c 20 72 63 2c 20 69 2c 20 6e 3b  t idx, rc, i, n;
33f3c 0a 20 20 54 65 72 6d 44 61 74 61 20 2a 70 44 61  .  TermData *pDa
33f3d 74 61 3b 0a 20 20 4c 65 61 66 57 72 69 74 65 72  ta;.  LeafWriter
33f3e 20 77 72 69 74 65 72 3b 0a 20 20 44 61 74 61 42   writer;.  DataB
33f3f 75 66 66 65 72 20 64 6c 3b 0a 0a 20 20 2f 2a 20  uffer dl;..  /* 
33f40 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65  Determine the ne
33f41 78 74 20 69 6e 64 65 78 20 61 74 20 6c 65 76 65  xt index at leve
33f42 6c 20 30 2c 20 6d 65 72 67 69 6e 67 20 61 73 20  l 0, merging as 
33f43 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f 0a 20 20  necessary. */.  
33f44 72 63 20 3d 20 73 65 67 64 69 72 4e 65 78 74 49  rc = segdirNextI
33f45 6e 64 65 78 28 76 2c 20 30 2c 20 26 69 64 78 29  ndex(v, 0, &idx)
33f46 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
33f47 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
33f48 63 3b 0a 0a 20 20 6e 20 3d 20 66 74 73 33 48 61  c;..  n = fts3Ha
33f49 73 68 43 6f 75 6e 74 28 70 54 65 72 6d 73 29 3b  shCount(pTerms);
33f4a 0a 20 20 70 44 61 74 61 20 3d 20 73 71 6c 69 74  .  pData = sqlit
33f4b 65 33 5f 6d 61 6c 6c 6f 63 28 6e 2a 73 69 7a 65  e3_malloc(n*size
33f4c 6f 66 28 54 65 72 6d 44 61 74 61 29 29 3b 0a 0a  of(TermData));..
33f4d 20 20 66 6f 72 28 69 20 3d 20 30 2c 20 65 20 3d    for(i = 0, e =
33f4e 20 66 74 73 33 48 61 73 68 46 69 72 73 74 28 70   fts3HashFirst(p
33f4f 54 65 72 6d 73 29 3b 20 65 3b 20 69 2b 2b 2c 20  Terms); e; i++, 
33f50 65 20 3d 20 66 74 73 33 48 61 73 68 4e 65 78 74  e = fts3HashNext
33f51 28 65 29 29 7b 0a 20 20 20 20 61 73 73 65 72 74  (e)){.    assert
33f52 28 20 69 3c 6e 20 29 3b 0a 20 20 20 20 70 44 61  ( i<n );.    pDa
33f53 74 61 5b 69 5d 2e 70 54 65 72 6d 20 3d 20 66 74  ta[i].pTerm = ft
33f54 73 33 48 61 73 68 4b 65 79 28 65 29 3b 0a 20 20  s3HashKey(e);.  
33f55 20 20 70 44 61 74 61 5b 69 5d 2e 6e 54 65 72 6d    pData[i].nTerm
33f56 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 73 69   = fts3HashKeysi
33f57 7a 65 28 65 29 3b 0a 20 20 20 20 70 44 61 74 61  ze(e);.    pData
33f58 5b 69 5d 2e 70 43 6f 6c 6c 65 63 74 6f 72 20 3d  [i].pCollector =
33f59 20 66 74 73 33 48 61 73 68 44 61 74 61 28 65 29   fts3HashData(e)
33f5a 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
33f5b 69 3d 3d 6e 20 29 3b 0a 0a 20 20 2f 2a 20 54 4f  i==n );..  /* TO
33f5c 44 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64  DO(shess) Should
33f5d 20 77 65 20 61 6c 6c 6f 77 20 75 73 65 72 2d 64   we allow user-d
33f5e 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  efined collation
33f5f 20 73 65 71 75 65 6e 63 65 73 2c 0a 20 20 2a 2a   sequences,.  **
33f60 20 68 65 72 65 3f 20 20 49 20 74 68 69 6e 6b 20   here?  I think 
33f61 77 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 68 61  we only need tha
33f62 74 20 6f 6e 63 65 20 77 65 20 73 75 70 70 6f 72  t once we suppor
33f63 74 20 70 72 65 66 69 78 20 73 65 61 72 63 68 65  t prefix searche
33f64 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 3e  s..  */.  if( n>
33f65 31 20 29 20 71 73 6f 72 74 28 70 44 61 74 61 2c  1 ) qsort(pData,
33f66 20 6e 2c 20 73 69 7a 65 6f 66 28 2a 70 44 61 74   n, sizeof(*pDat
33f67 61 29 2c 20 74 65 72 6d 44 61 74 61 43 6d 70 29  a), termDataCmp)
33f68 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  ;..  /* TODO(she
33f69 73 73 29 20 52 65 66 61 63 74 6f 72 20 73 6f 20  ss) Refactor so 
33f6a 74 68 61 74 20 77 65 20 63 61 6e 20 77 72 69 74  that we can writ
33f6b 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68  e directly to th
33f6c 65 20 73 65 67 6d 65 6e 74 0a 20 20 2a 2a 20 44  e segment.  ** D
33f6d 61 74 61 42 75 66 66 65 72 2c 20 61 73 20 68 61  ataBuffer, as ha
33f6e 70 70 65 6e 73 20 66 6f 72 20 73 65 67 6d 65 6e  ppens for segmen
33f6f 74 20 6d 65 72 67 65 73 2e 0a 20 20 2a 2f 0a 20  t merges..  */. 
33f70 20 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28   leafWriterInit(
33f71 30 2c 20 69 64 78 2c 20 26 77 72 69 74 65 72 29  0, idx, &writer)
33f72 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ;.  dataBufferIn
33f73 69 74 28 26 64 6c 2c 20 30 29 3b 0a 20 20 66 6f  it(&dl, 0);.  fo
33f74 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29  r(i=0; i<n; i++)
33f75 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
33f76 52 65 73 65 74 28 26 64 6c 29 3b 0a 20 20 20 20  Reset(&dl);.    
33f77 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 28 70 44  dlcAddDoclist(pD
33f78 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63 74 6f  ata[i].pCollecto
33f79 72 2c 20 26 64 6c 29 3b 0a 20 20 20 20 72 63 20  r, &dl);.    rc 
33f7a 3d 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  = leafWriterStep
33f7b 28 76 2c 20 26 77 72 69 74 65 72 2c 0a 20 20 20  (v, &writer,.   
33f7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f7d 20 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 70 54       pData[i].pT
33f7e 65 72 6d 2c 20 70 44 61 74 61 5b 69 5d 2e 6e 54  erm, pData[i].nT
33f7f 65 72 6d 2c 20 64 6c 2e 70 44 61 74 61 2c 20 64  erm, dl.pData, d
33f80 6c 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 69 66  l.nData);.    if
33f81 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
33f82 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d 0a  ) goto err;.  }.
33f83 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
33f84 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72  rFinalize(v, &wr
33f85 69 74 65 72 29 3b 0a 0a 20 65 72 72 3a 0a 20 20  iter);.. err:.  
33f86 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
33f87 79 28 26 64 6c 29 3b 0a 20 20 73 71 6c 69 74 65  y(&dl);.  sqlite
33f88 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a 20  3_free(pData);. 
33f89 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72   leafWriterDestr
33f8a 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72  oy(&writer);.  r
33f8b 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
33f8c 49 66 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  If pendingTerms 
33f8d 68 61 73 20 64 61 74 61 2c 20 66 72 65 65 20 69  has data, free i
33f8e 74 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t. */.static int
33f8f 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
33f90 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ms(fulltext_vtab
33f91 20 2a 76 29 7b 0a 20 20 69 66 28 20 76 2d 3e 6e   *v){.  if( v->n
33f92 50 65 6e 64 69 6e 67 44 61 74 61 3e 3d 30 20 29  PendingData>=0 )
33f93 7b 0a 20 20 20 20 66 74 73 33 48 61 73 68 45 6c  {.    fts3HashEl
33f94 65 6d 20 2a 65 3b 0a 20 20 20 20 66 6f 72 28 65  em *e;.    for(e
33f95 3d 66 74 73 33 48 61 73 68 46 69 72 73 74 28 26  =fts3HashFirst(&
33f96 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29  v->pendingTerms)
33f97 3b 20 65 3b 20 65 3d 66 74 73 33 48 61 73 68 4e  ; e; e=fts3HashN
33f98 65 78 74 28 65 29 29 7b 0a 20 20 20 20 20 20 64  ext(e)){.      d
33f99 6c 63 44 65 6c 65 74 65 28 66 74 73 33 48 61 73  lcDelete(fts3Has
33f9a 68 44 61 74 61 28 65 29 29 3b 0a 20 20 20 20 7d  hData(e));.    }
33f9b 0a 20 20 20 20 66 74 73 33 48 61 73 68 43 6c 65  .    fts3HashCle
33f9c 61 72 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65  ar(&v->pendingTe
33f9d 72 6d 73 29 3b 0a 20 20 20 20 76 2d 3e 6e 50 65  rms);.    v->nPe
33f9e 6e 64 69 6e 67 44 61 74 61 20 3d 20 2d 31 3b 0a  ndingData = -1;.
33f9f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
33fa0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66  ITE_OK;.}../* If
33fa1 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 68 61   pendingTerms ha
33fa2 73 20 64 61 74 61 2c 20 66 6c 75 73 68 20 69 74  s data, flush it
33fa3 20 74 6f 20 61 20 6c 65 76 65 6c 2d 7a 65 72 6f   to a level-zero
33fa4 20 73 65 67 6d 65 6e 74 2c 20 61 6e 64 0a 2a 2a   segment, and.**
33fa5 20 66 72 65 65 20 69 74 2e 0a 2a 2f 0a 73 74 61   free it..*/.sta
33fa6 74 69 63 20 69 6e 74 20 66 6c 75 73 68 50 65 6e  tic int flushPen
33fa7 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65  dingTerms(fullte
33fa8 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69  xt_vtab *v){.  i
33fa9 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  f( v->nPendingDa
33faa 74 61 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ta>=0 ){.    int
33fab 20 72 63 20 3d 20 77 72 69 74 65 5a 65 72 6f 53   rc = writeZeroS
33fac 65 67 6d 65 6e 74 28 76 2c 20 26 76 2d 3e 70 65  egment(v, &v->pe
33fad 6e 64 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20 20  ndingTerms);.   
33fae 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
33faf 4f 4b 20 29 20 63 6c 65 61 72 50 65 6e 64 69 6e  OK ) clearPendin
33fb0 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20 72  gTerms(v);.    r
33fb1 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
33fb2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
33fb3 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69  ;.}../* If pendi
33fb4 6e 67 54 65 72 6d 73 20 69 73 20 22 74 6f 6f 20  ngTerms is "too 
33fb5 62 69 67 22 2c 20 6f 72 20 64 6f 63 69 64 20 69  big", or docid i
33fb6 73 20 6f 75 74 20 6f 66 20 6f 72 64 65 72 2c 20  s out of order, 
33fb7 66 6c 75 73 68 20 69 74 2e 0a 2a 2a 20 52 65 67  flush it..** Reg
33fb8 61 72 64 6c 65 73 73 2c 20 62 65 20 63 65 72 74  ardless, be cert
33fb9 61 69 6e 20 74 68 61 74 20 70 65 6e 64 69 6e 67  ain that pending
33fba 54 65 72 6d 73 20 69 73 20 69 6e 69 74 69 61 6c  Terms is initial
33fbb 69 7a 65 64 20 66 6f 72 20 75 73 65 2e 0a 2a 2f  ized for use..*/
33fbc 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 69 74  .static int init
33fbd 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c  PendingTerms(ful
33fbe 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
33fbf 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
33fc0 69 64 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 28 73  id){.  /* TODO(s
33fc1 68 65 73 73 29 20 45 78 70 6c 6f 72 65 20 77 68  hess) Explore wh
33fc2 65 74 68 65 72 20 70 61 72 74 69 61 6c 6c 79 20  ether partially 
33fc3 66 6c 75 73 68 69 6e 67 20 74 68 65 20 62 75 66  flushing the buf
33fc4 66 65 72 20 6f 6e 0a 20 20 2a 2a 20 66 6f 72 63  fer on.  ** forc
33fc5 65 64 2d 66 6c 75 73 68 20 77 6f 75 6c 64 20 70  ed-flush would p
33fc6 72 6f 76 69 64 65 20 62 65 74 74 65 72 20 70 65  rovide better pe
33fc7 72 66 6f 72 6d 61 6e 63 65 2e 20 20 49 20 73 75  rformance.  I su
33fc8 73 70 65 63 74 20 74 68 61 74 20 69 66 0a 20 20  spect that if.  
33fc9 2a 2a 20 77 65 20 6f 72 64 65 72 65 64 20 74 68  ** we ordered th
33fca 65 20 64 6f 63 6c 69 73 74 73 20 62 79 20 73 69  e doclists by si
33fcb 7a 65 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74  ze and flushed t
33fcc 68 65 20 6c 61 72 67 65 73 74 20 75 6e 74 69 6c  he largest until
33fcd 20 74 68 65 0a 20 20 2a 2a 20 62 75 66 66 65 72   the.  ** buffer
33fce 20 77 61 73 20 68 61 6c 66 20 65 6d 70 74 79 2c   was half empty,
33fcf 20 74 68 61 74 20 77 6f 75 6c 64 20 6c 65 74 20   that would let 
33fd0 74 68 65 20 6c 65 73 73 20 66 72 65 71 75 65 6e  the less frequen
33fd1 74 20 74 65 72 6d 73 0a 20 20 2a 2a 20 67 65 6e  t terms.  ** gen
33fd2 65 72 61 74 65 20 6c 6f 6e 67 65 72 20 64 6f 63  erate longer doc
33fd3 6c 69 73 74 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  lists..  */.  if
33fd4 28 20 69 44 6f 63 69 64 3c 3d 76 2d 3e 69 50 72  ( iDocid<=v->iPr
33fd5 65 76 44 6f 63 69 64 20 7c 7c 20 76 2d 3e 6e 50  evDocid || v->nP
33fd6 65 6e 64 69 6e 67 44 61 74 61 3e 6b 50 65 6e 64  endingData>kPend
33fd7 69 6e 67 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a  ingThreshold ){.
33fd8 20 20 20 20 69 6e 74 20 72 63 20 3d 20 66 6c 75      int rc = flu
33fd9 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  shPendingTerms(v
33fda 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
33fdb 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
33fdc 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20  n rc;.  }.  if( 
33fdd 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  v->nPendingData<
33fde 30 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73  0 ){.    fts3Has
33fdf 68 49 6e 69 74 28 26 76 2d 3e 70 65 6e 64 69 6e  hInit(&v->pendin
33fe0 67 54 65 72 6d 73 2c 20 46 54 53 33 5f 48 41 53  gTerms, FTS3_HAS
33fe1 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20  H_STRING, 1);.  
33fe2 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74    v->nPendingDat
33fe3 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 76 2d 3e  a = 0;.  }.  v->
33fe4 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f  iPrevDocid = iDo
33fe5 63 69 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  cid;.  return SQ
33fe6 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54  LITE_OK;.}../* T
33fe7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  his function imp
33fe8 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 55 70 64  lements the xUpd
33fe9 61 74 65 20 63 61 6c 6c 62 61 63 6b 3b 20 69 74  ate callback; it
33fea 20 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65   is the top-leve
33feb 6c 20 65 6e 74 72 79 0a 20 2a 20 70 6f 69 6e 74  l entry. * point
33fec 20 66 6f 72 20 69 6e 73 65 72 74 69 6e 67 2c 20   for inserting, 
33fed 64 65 6c 65 74 69 6e 67 20 6f 72 20 75 70 64 61  deleting or upda
33fee 74 69 6e 67 20 61 20 72 6f 77 20 69 6e 20 61 20  ting a row in a 
33fef 66 75 6c 6c 2d 74 65 78 74 20 74 61 62 6c 65 2e  full-text table.
33ff0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
33ff1 75 6c 6c 74 65 78 74 55 70 64 61 74 65 28 73 71  ulltextUpdate(sq
33ff2 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
33ff3 62 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c  b, int nArg, sql
33ff4 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70 41  ite3_value **ppA
33ff5 72 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rg,.            
33ff6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
33ff7 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77  lite_int64 *pRow
33ff8 69 64 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  id){.  fulltext_
33ff9 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74  vtab *v = (fullt
33ffa 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56 74 61  ext_vtab *) pVta
33ffb 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  b;.  int rc;..  
33ffc 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
33ffd 55 70 64 61 74 65 20 25 70 5c 6e 22 2c 20 70 56  Update %p\n", pV
33ffe 74 61 62 29 29 3b 0a 0a 20 20 69 66 28 20 6e 41  tab));..  if( nA
33fff 72 67 3c 32 20 29 7b 0a 20 20 20 20 72 63 20 3d  rg<2 ){.    rc =
34000 20 69 6e 64 65 78 5f 64 65 6c 65 74 65 28 76 2c   index_delete(v,
34001 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
34002 6e 74 36 34 28 70 70 41 72 67 5b 30 5d 29 29 3b  nt64(ppArg[0]));
34003 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
34004 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
34005 2f 2a 20 49 66 20 77 65 20 6a 75 73 74 20 64 65  /* If we just de
34006 6c 65 74 65 64 20 74 68 65 20 6c 61 73 74 20 72  leted the last r
34007 6f 77 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  ow in the table,
34008 20 63 6c 65 61 72 20 6f 75 74 20 74 68 65 0a 20   clear out the. 
34009 20 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 64 61       ** index da
3400a 74 61 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ta..      */.   
3400b 20 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f     rc = content_
3400c 65 78 69 73 74 73 28 76 29 3b 0a 20 20 20 20 20  exists(v);.     
3400d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3400e 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ROW ){.        r
3400f 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
34010 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72       }else if( r
34011 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
34012 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 65  {.        /* Cle
34013 61 72 20 74 68 65 20 70 65 6e 64 69 6e 67 20 74  ar the pending t
34014 65 72 6d 73 20 73 6f 20 77 65 20 64 6f 6e 27 74  erms so we don't
34015 20 66 6c 75 73 68 20 61 20 75 73 65 6c 65 73 73   flush a useless
34016 20 6c 65 76 65 6c 2d 30 0a 20 20 20 20 20 20 20   level-0.       
34017 20 2a 2a 20 73 65 67 6d 65 6e 74 20 77 68 65 6e   ** segment when
34018 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
34019 20 63 6c 6f 73 65 73 2e 0a 20 20 20 20 20 20 20   closes..       
3401a 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d   */.        rc =
3401b 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
3401c 6d 73 28 76 29 3b 0a 20 20 20 20 20 20 20 20 69  ms(v);.        i
3401d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3401e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
3401f 20 3d 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65   = segdir_delete
34020 5f 61 6c 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  _all(v);.       
34021 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
34022 0a 20 20 7d 20 65 6c 73 65 20 69 66 28 20 73 71  .  } else if( sq
34023 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
34024 28 70 70 41 72 67 5b 30 5d 29 20 21 3d 20 53 51  (ppArg[0]) != SQ
34025 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20  LITE_NULL ){.   
34026 20 2f 2a 20 41 6e 20 75 70 64 61 74 65 3a 0a 20   /* An update:. 
34027 20 20 20 20 2a 20 70 70 41 72 67 5b 30 5d 20 3d      * ppArg[0] =
34028 20 6f 6c 64 20 72 6f 77 69 64 0a 20 20 20 20 20   old rowid.     
34029 2a 20 70 70 41 72 67 5b 31 5d 20 3d 20 6e 65 77  * ppArg[1] = new
3402a 20 72 6f 77 69 64 0a 20 20 20 20 20 2a 20 70 70   rowid.     * pp
3402b 41 72 67 5b 32 2e 2e 32 2b 76 2d 3e 6e 43 6f 6c  Arg[2..2+v->nCol
3402c 75 6d 6e 2d 31 5d 20 3d 20 76 61 6c 75 65 73 0a  umn-1] = values.
3402d 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2b 76       * ppArg[2+v
3402e 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 76 61 6c  ->nColumn] = val
3402f 75 65 20 66 6f 72 20 6d 61 67 69 63 20 63 6f 6c  ue for magic col
34030 75 6d 6e 20 28 77 65 20 69 67 6e 6f 72 65 20 74  umn (we ignore t
34031 68 69 73 29 0a 20 20 20 20 20 2a 20 70 70 41 72  his).     * ppAr
34032 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31  g[2+v->nColumn+1
34033 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 64 6f  ] = value for do
34034 63 69 64 0a 20 20 20 20 20 2a 2f 0a 20 20 20 20  cid.     */.    
34035 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77  sqlite_int64 row
34036 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  id = sqlite3_val
34037 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b 30  ue_int64(ppArg[0
34038 5d 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ]);.    if( sqli
34039 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70  te3_value_type(p
3403a 70 41 72 67 5b 31 5d 29 20 21 3d 20 53 51 4c 49  pArg[1]) != SQLI
3403b 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c 0a 20 20  TE_INTEGER ||.  
3403c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
3403d 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b  lue_int64(ppArg[
3403e 31 5d 29 20 21 3d 20 72 6f 77 69 64 20 29 7b 0a  1]) != rowid ){.
3403f 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
34040 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 77 65 20  E_ERROR;  /* we 
34041 64 6f 6e 27 74 20 61 6c 6c 6f 77 20 63 68 61 6e  don't allow chan
34042 67 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 2a  ging the rowid *
34043 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  /.    }else if( 
34044 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
34045 70 65 28 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43  pe(ppArg[2+v->nC
34046 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20 53 51 4c  olumn+1]) != SQL
34047 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c 0a 20  ITE_INTEGER ||. 
34048 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
34049 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
3404a 28 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c  (ppArg[2+v->nCol
3404b 75 6d 6e 2b 31 5d 29 20 21 3d 20 72 6f 77 69 64  umn+1]) != rowid
3404c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
3404d 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a  QLITE_ERROR;  /*
3404e 20 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77 20   we don't allow 
3404f 63 68 61 6e 67 69 6e 67 20 74 68 65 20 64 6f 63  changing the doc
34050 69 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b  id */.    }else{
34051 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
34052 41 72 67 3d 3d 32 2b 76 2d 3e 6e 43 6f 6c 75 6d  Arg==2+v->nColum
34053 6e 2b 32 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  n+2);.      rc =
34054 20 69 6e 64 65 78 5f 75 70 64 61 74 65 28 76 2c   index_update(v,
34055 20 72 6f 77 69 64 2c 20 26 70 70 41 72 67 5b 32   rowid, &ppArg[2
34056 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c  ]);.    }.  } el
34057 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 69  se {.    /* An i
34058 6e 73 65 72 74 3a 0a 20 20 20 20 20 2a 20 70 70  nsert:.     * pp
34059 41 72 67 5b 31 5d 20 3d 20 72 65 71 75 65 73 74  Arg[1] = request
3405a 65 64 20 72 6f 77 69 64 0a 20 20 20 20 20 2a 20  ed rowid.     * 
3405b 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d 3e 6e 43  ppArg[2..2+v->nC
3405c 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61 6c 75 65  olumn-1] = value
3405d 73 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32  s.     * ppArg[2
3405e 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 76  +v->nColumn] = v
3405f 61 6c 75 65 20 66 6f 72 20 6d 61 67 69 63 20 63  alue for magic c
34060 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e 6f 72 65  olumn (we ignore
34061 20 74 68 69 73 29 0a 20 20 20 20 20 2a 20 70 70   this).     * pp
34062 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e  Arg[2+v->nColumn
34063 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20  +1] = value for 
34064 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f 0a 20 20  docid.     */.  
34065 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
34066 2a 70 52 65 71 75 65 73 74 44 6f 63 69 64 20 3d  *pRequestDocid =
34067 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
34068 75 6d 6e 2b 31 5d 3b 0a 20 20 20 20 61 73 73 65  umn+1];.    asse
34069 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e  rt( nArg==2+v->n
3406a 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 69  Column+2);.    i
3406b 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 21  f( SQLITE_NULL !
3406c 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3406d 74 79 70 65 28 70 52 65 71 75 65 73 74 44 6f 63  type(pRequestDoc
3406e 69 64 29 20 26 26 0a 20 20 20 20 20 20 20 20 53  id) &&.        S
3406f 51 4c 49 54 45 5f 4e 55 4c 4c 20 21 3d 20 73 71  QLITE_NULL != sq
34070 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
34071 28 70 70 41 72 67 5b 31 5d 29 20 29 7b 0a 20 20  (ppArg[1]) ){.  
34072 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
34073 73 29 20 43 6f 6e 73 69 64 65 72 20 61 6c 6c 6f  s) Consider allo
34074 77 69 6e 67 20 74 68 69 73 20 74 6f 20 77 6f 72  wing this to wor
34075 6b 20 69 66 20 74 68 65 20 76 61 6c 75 65 73 20  k if the values 
34076 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 69 64 65  are.      ** ide
34077 6e 74 69 63 61 6c 2e 20 20 49 27 6d 20 69 6e 63  ntical.  I'm inc
34078 6c 69 6e 65 64 20 74 6f 20 64 69 73 63 6f 75 72  lined to discour
34079 61 67 65 20 74 68 61 74 20 75 73 61 67 65 2c 20  age that usage, 
3407a 74 68 6f 75 67 68 2c 0a 20 20 20 20 20 20 2a 2a  though,.      **
3407b 20 67 69 76 65 6e 20 74 68 61 74 20 62 6f 74 68   given that both
3407c 20 72 6f 77 69 64 20 61 6e 64 20 64 6f 63 69 64   rowid and docid
3407d 20 61 72 65 20 73 70 65 63 69 61 6c 20 63 6f 6c   are special col
3407e 75 6d 6e 73 2e 20 20 42 65 74 74 65 72 0a 20 20  umns.  Better.  
3407f 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20      ** would be 
34080 74 6f 20 64 65 66 69 6e 65 20 6f 6e 65 20 6f 72  to define one or
34081 20 74 68 65 20 6f 74 68 65 72 20 61 73 20 74 68   the other as th
34082 65 20 64 65 66 61 75 6c 74 20 77 69 6e 6e 65 72  e default winner
34083 2c 0a 20 20 20 20 20 20 2a 2a 20 62 75 74 20 73  ,.      ** but s
34084 68 6f 75 6c 64 20 69 74 20 62 65 20 66 74 73 33  hould it be fts3
34085 2d 63 65 6e 74 72 69 63 20 28 64 6f 63 69 64 29  -centric (docid)
34086 20 6f 72 20 53 51 4c 69 74 65 2d 63 65 6e 74 72   or SQLite-centr
34087 69 63 0a 20 20 20 20 20 20 2a 2a 20 28 72 6f 77  ic.      ** (row
34088 69 64 29 3f 0a 20 20 20 20 20 20 2a 2f 0a 20 20  id)?.      */.  
34089 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3408a 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65  ERROR;.    }else
3408b 7b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  {.      if( SQLI
3408c 54 45 5f 4e 55 4c 4c 20 3d 3d 20 73 71 6c 69 74  TE_NULL == sqlit
3408d 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 52  e3_value_type(pR
3408e 65 71 75 65 73 74 44 6f 63 69 64 29 20 29 7b 0a  equestDocid) ){.
3408f 20 20 20 20 20 20 20 20 70 52 65 71 75 65 73 74          pRequest
34090 44 6f 63 69 64 20 3d 20 70 70 41 72 67 5b 31 5d  Docid = ppArg[1]
34091 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
34092 72 63 20 3d 20 69 6e 64 65 78 5f 69 6e 73 65 72  rc = index_inser
34093 74 28 76 2c 20 70 52 65 71 75 65 73 74 44 6f 63  t(v, pRequestDoc
34094 69 64 2c 20 26 70 70 41 72 67 5b 32 5d 2c 20 70  id, &ppArg[2], p
34095 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Rowid);.    }.  
34096 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
34097 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  }..static int fu
34098 6c 6c 74 65 78 74 53 79 6e 63 28 73 71 6c 69 74  lltextSync(sqlit
34099 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b  e3_vtab *pVtab){
3409a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
3409b 53 33 20 78 53 79 6e 63 28 29 5c 6e 22 29 29 3b  S3 xSync()\n"));
3409c 0a 20 20 72 65 74 75 72 6e 20 66 6c 75 73 68 50  .  return flushP
3409d 65 6e 64 69 6e 67 54 65 72 6d 73 28 28 66 75 6c  endingTerms((ful
3409e 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56 74  ltext_vtab *)pVt
3409f 61 62 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  ab);.}..static i
340a0 6e 74 20 66 75 6c 6c 74 65 78 74 42 65 67 69 6e  nt fulltextBegin
340a1 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
340a2 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78  Vtab){.  fulltex
340a3 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c  t_vtab *v = (ful
340a4 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56  ltext_vtab *) pV
340a5 74 61 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28  tab;.  FTSTRACE(
340a6 28 22 46 54 53 33 20 78 42 65 67 69 6e 28 29 5c  ("FTS3 xBegin()\
340a7 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 41 6e 79 20  n"));..  /* Any 
340a8 62 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73  buffered updates
340a9 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65   should have bee
340aa 6e 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65  n cleared by the
340ab 20 70 72 65 76 69 6f 75 73 0a 20 20 2a 2a 20 74   previous.  ** t
340ac 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f  ransaction..  */
340ad 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50  .  assert( v->nP
340ae 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a  endingData<0 );.
340af 20 20 72 65 74 75 72 6e 20 63 6c 65 61 72 50 65    return clearPe
340b0 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 7d  ndingTerms(v);.}
340b1 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  ..static int ful
340b2 6c 74 65 78 74 43 6f 6d 6d 69 74 28 73 71 6c 69  ltextCommit(sqli
340b3 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
340b4 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  {.  fulltext_vta
340b5 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74  b *v = (fulltext
340b6 5f 76 74 61 62 20 2a 29 20 70 56 74 61 62 3b 0a  _vtab *) pVtab;.
340b7 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
340b8 33 20 78 43 6f 6d 6d 69 74 28 29 5c 6e 22 29 29  3 xCommit()\n"))
340b9 3b 0a 0a 20 20 2f 2a 20 42 75 66 66 65 72 65 64  ;..  /* Buffered
340ba 20 75 70 64 61 74 65 73 20 73 68 6f 75 6c 64 20   updates should 
340bb 68 61 76 65 20 62 65 65 6e 20 63 6c 65 61 72 65  have been cleare
340bc 64 20 62 79 20 66 75 6c 6c 74 65 78 74 53 79 6e  d by fulltextSyn
340bd 63 28 29 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  c(). */.  assert
340be 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74  ( v->nPendingDat
340bf 61 3c 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  a<0 );.  return 
340c0 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d  clearPendingTerm
340c1 73 28 76 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  s(v);.}..static 
340c2 69 6e 74 20 66 75 6c 6c 74 65 78 74 52 6f 6c 6c  int fulltextRoll
340c3 62 61 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61  back(sqlite3_vta
340c4 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 46 54 53  b *pVtab){.  FTS
340c5 54 52 41 43 45 28 28 22 46 54 53 33 20 78 52 6f  TRACE(("FTS3 xRo
340c6 6c 6c 62 61 63 6b 28 29 5c 6e 22 29 29 3b 0a 20  llback()\n"));. 
340c7 20 72 65 74 75 72 6e 20 63 6c 65 61 72 50 65 6e   return clearPen
340c8 64 69 6e 67 54 65 72 6d 73 28 28 66 75 6c 6c 74  dingTerms((fullt
340c9 65 78 74 5f 76 74 61 62 20 2a 29 70 56 74 61 62  ext_vtab *)pVtab
340ca 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
340cb 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
340cc 65 20 73 6e 69 70 70 65 74 28 29 20 66 75 6e 63  e snippet() func
340cd 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f  tion for FTS3.*/
340ce 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69  .static void sni
340cf 70 70 65 74 46 75 6e 63 28 0a 20 20 73 71 6c 69  ppetFunc(.  sqli
340d0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
340d1 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
340d2 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
340d3 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66  ue **argv.){.  f
340d4 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
340d5 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61  pCursor;.  if( a
340d6 72 67 63 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a  rgc<1 ) return;.
340d7 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
340d8 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
340d9 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c  )!=SQLITE_BLOB |
340da 7c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  |.      sqlite3_
340db 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
340dc 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75  [0])!=sizeof(pCu
340dd 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c  rsor) ){.    sql
340de 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
340df 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c  r(pContext, "ill
340e0 65 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d  egal first argum
340e1 65 6e 74 20 74 6f 20 68 74 6d 6c 5f 73 6e 69 70  ent to html_snip
340e2 70 65 74 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73  pet",-1);.  }els
340e3 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e{.    const cha
340e4 72 20 2a 7a 53 74 61 72 74 20 3d 20 22 3c 62 3e  r *zStart = "<b>
340e5 22 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ";.    const cha
340e6 72 20 2a 7a 45 6e 64 20 3d 20 22 3c 2f 62 3e 22  r *zEnd = "</b>"
340e7 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
340e8 20 2a 7a 45 6c 6c 69 70 73 69 73 20 3d 20 22 3c   *zEllipsis = "<
340e9 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b 0a 20 20 20 20  b>...</b>";.    
340ea 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c  memcpy(&pCursor,
340eb 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
340ec 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69  lob(argv[0]), si
340ed 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a  zeof(pCursor));.
340ee 20 20 20 20 69 66 28 20 61 72 67 63 3e 3d 32 20      if( argc>=2 
340ef 29 7b 0a 20 20 20 20 20 20 7a 53 74 61 72 74 20  ){.      zStart 
340f0 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73  = (const char*)s
340f1 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
340f2 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  t(argv[1]);.    
340f3 20 20 69 66 28 20 61 72 67 63 3e 3d 33 20 29 7b    if( argc>=3 ){
340f4 0a 20 20 20 20 20 20 20 20 7a 45 6e 64 20 3d 20  .        zEnd = 
340f5 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c  (const char*)sql
340f6 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
340f7 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20  argv[2]);.      
340f8 20 20 69 66 28 20 61 72 67 63 3e 3d 34 20 29 7b    if( argc>=4 ){
340f9 0a 20 20 20 20 20 20 20 20 20 20 7a 45 6c 6c 69  .          zElli
340fa 70 73 69 73 20 3d 20 28 63 6f 6e 73 74 20 63 68  psis = (const ch
340fb 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
340fc 65 5f 74 65 78 74 28 61 72 67 76 5b 33 5d 29 3b  e_text(argv[3]);
340fd 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
340fe 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 6e 69   }.    }.    sni
340ff 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73 28 70  ppetAllOffsets(p
34100 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 6e 69  Cursor);.    sni
34101 70 70 65 74 54 65 78 74 28 70 43 75 72 73 6f 72  ppetText(pCursor
34102 2c 20 7a 53 74 61 72 74 2c 20 7a 45 6e 64 2c 20  , zStart, zEnd, 
34103 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20 20 20  zEllipsis);.    
34104 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
34105 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 70 43  ext(pContext, pC
34106 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a  ursor->snippet.z
34107 53 6e 69 70 70 65 74 2c 0a 20 20 20 20 20 20 20  Snippet,.       
34108 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34109 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65   pCursor->snippe
3410a 74 2e 6e 53 6e 69 70 70 65 74 2c 20 53 51 4c 49  t.nSnippet, SQLI
3410b 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  TE_STATIC);.  }.
3410c 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
3410d 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f  ntation of the o
3410e 66 66 73 65 74 73 28 29 20 66 75 6e 63 74 69 6f  ffsets() functio
3410f 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f 0a 73 74  n for FTS3.*/.st
34110 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65  atic void snippe
34111 74 4f 66 66 73 65 74 73 46 75 6e 63 28 0a 20 20  tOffsetsFunc(.  
34112 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
34113 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74  *pContext,.  int
34114 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
34115 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
34116 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
34117 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69  or *pCursor;.  i
34118 66 28 20 61 72 67 63 3c 31 20 29 20 72 65 74 75  f( argc<1 ) retu
34119 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rn;.  if( sqlite
3411a 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
3411b 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c  v[0])!=SQLITE_BL
3411c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69  OB ||.      sqli
3411d 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
3411e 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66  argv[0])!=sizeof
3411f 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20  (pCursor) ){.   
34120 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
34121 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  error(pContext, 
34122 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61  "illegal first a
34123 72 67 75 6d 65 6e 74 20 74 6f 20 6f 66 66 73 65  rgument to offse
34124 74 73 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65  ts",-1);.  }else
34125 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43  {.    memcpy(&pC
34126 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76  ursor, sqlite3_v
34127 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
34128 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73  ]), sizeof(pCurs
34129 6f 72 29 29 3b 0a 20 20 20 20 73 6e 69 70 70 65  or));.    snippe
3412a 74 41 6c 6c 4f 66 66 73 65 74 73 28 70 43 75 72  tAllOffsets(pCur
3412b 73 6f 72 29 3b 0a 20 20 20 20 73 6e 69 70 70 65  sor);.    snippe
3412c 74 4f 66 66 73 65 74 54 65 78 74 28 26 70 43 75  tOffsetText(&pCu
3412d 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 29 3b 0a  rsor->snippet);.
3412e 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3412f 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74  lt_text(pContext
34130 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
34131 20 20 20 20 20 20 20 20 20 20 70 43 75 72 73 6f            pCurso
34132 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 4f 66 66 73  r->snippet.zOffs
34133 65 74 2c 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69  et, pCursor->sni
34134 70 70 65 74 2e 6e 4f 66 66 73 65 74 2c 0a 20 20  ppet.nOffset,.  
34135 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34136 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 54 41        SQLITE_STA
34137 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  TIC);.  }.}../* 
34138 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
34139 69 73 20 6e 65 61 72 6c 79 20 69 64 65 6e 74 69  is nearly identi
3413a 63 61 6c 20 74 6f 20 4c 65 61 76 65 73 52 65 61  cal to LeavesRea
3413b 64 65 72 2c 20 65 78 63 65 70 74 20 74 68 61 74  der, except that
3413c 0a 2a 2a 20 77 68 65 72 65 20 4c 65 61 76 65 73  .** where Leaves
3413d 52 65 61 64 65 72 20 69 73 20 67 65 61 72 65 64  Reader is geared
3413e 20 74 6f 77 61 72 64 73 20 74 68 65 20 6d 65 72   towards the mer
3413f 67 69 6e 67 20 6f 66 20 63 6f 6d 70 6c 65 74 65  ging of complete
34140 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 6c 65 76 65  .** segment leve
34141 6c 73 20 28 77 69 74 68 20 65 78 61 63 74 6c 79  ls (with exactly
34142 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67   MERGE_COUNT seg
34143 6d 65 6e 74 73 29 2c 20 4f 70 74 4c 65 61 76 65  ments), OptLeave
34144 73 52 65 61 64 65 72 0a 2a 2a 20 69 73 20 67 65  sReader.** is ge
34145 61 72 65 64 20 74 6f 77 61 72 64 73 20 69 6d 70  ared towards imp
34146 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
34147 68 65 20 6f 70 74 69 6d 69 7a 65 28 29 20 66 75  he optimize() fu
34148 6e 63 74 69 6f 6e 2c 20 61 6e 64 0a 2a 2a 20 63  nction, and.** c
34149 61 6e 20 6d 65 72 67 65 20 61 6c 6c 20 73 65 67  an merge all seg
3414a 6d 65 6e 74 73 20 73 69 6d 75 6c 74 61 6e 65 6f  ments simultaneo
3414b 75 73 6c 79 2e 20 20 54 68 69 73 20 76 65 72 73  usly.  This vers
3414c 69 6f 6e 20 6d 61 79 20 62 65 0a 2a 2a 20 73 6f  ion may be.** so
3414d 6d 65 77 68 61 74 20 6c 65 73 73 20 65 66 66 69  mewhat less effi
3414e 63 69 65 6e 74 20 74 68 61 6e 20 4c 65 61 76 65  cient than Leave
3414f 73 52 65 61 64 65 72 20 62 65 63 61 75 73 65 20  sReader because 
34150 69 74 20 6d 65 72 67 65 73 20 69 6e 74 6f 20 61  it merges into a
34151 6e 0a 2a 2a 20 61 63 63 75 6d 75 6c 61 74 6f 72  n.** accumulator
34152 20 72 61 74 68 65 72 20 74 68 61 6e 20 64 6f 69   rather than doi
34153 6e 67 20 61 6e 20 4e 2d 77 61 79 20 6d 65 72 67  ng an N-way merg
34154 65 2c 20 62 75 74 20 73 69 6e 63 65 20 73 65 67  e, but since seg
34155 6d 65 6e 74 0a 2a 2a 20 73 69 7a 65 20 67 72 6f  ment.** size gro
34156 77 73 20 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79  ws exponentially
34157 20 28 73 6f 20 73 65 67 6d 65 6e 74 20 63 6f 75   (so segment cou
34158 6e 74 20 6c 6f 67 72 69 74 68 6d 69 63 61 6c 6c  nt logrithmicall
34159 79 29 20 74 68 69 73 20 69 73 0a 2a 2a 20 70 72  y) this is.** pr
3415a 6f 62 61 62 6c 79 20 6e 6f 74 20 61 6e 20 69 6d  obably not an im
3415b 6d 65 64 69 61 74 65 20 70 72 6f 62 6c 65 6d 2e  mediate problem.
3415c 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
3415d 73 29 3a 20 50 72 6f 76 65 20 74 68 61 74 20 61  s): Prove that a
3415e 73 73 65 72 74 69 6f 6e 2c 20 6f 72 20 65 78 74  ssertion, or ext
3415f 65 6e 64 20 74 68 65 20 6d 65 72 67 65 20 63 6f  end the merge co
34160 64 65 20 74 6f 0a 2a 2a 20 6d 65 72 67 65 20 74  de to.** merge t
34161 72 65 65 20 66 61 73 68 69 6f 6e 20 28 6c 69 6b  ree fashion (lik
34162 65 20 74 68 65 20 70 72 65 66 69 78 2d 73 65 61  e the prefix-sea
34163 72 63 68 69 6e 67 20 63 6f 64 65 20 64 6f 65 73  rching code does
34164 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  )..*/./* TODO(sh
34165 65 73 73 29 3a 20 4f 70 74 4c 65 61 76 65 73 52  ess): OptLeavesR
34166 65 61 64 65 72 20 61 6e 64 20 4c 65 61 76 65 73  eader and Leaves
34167 52 65 61 64 65 72 20 63 6f 75 6c 64 20 70 72 6f  Reader could pro
34168 62 61 62 6c 79 20 62 65 0a 2a 2a 20 6d 65 72 67  bably be.** merg
34169 65 64 20 77 69 74 68 20 6c 69 74 74 6c 65 20 6f  ed with little o
3416a 72 20 6e 6f 20 6c 6f 73 73 20 6f 66 20 70 65 72  r no loss of per
3416b 66 6f 72 6d 61 6e 63 65 20 66 6f 72 20 4c 65 61  formance for Lea
3416c 76 65 73 52 65 61 64 65 72 2e 20 20 54 68 65 0a  vesReader.  The.
3416d 2a 2a 20 6d 65 72 67 65 64 20 63 6f 64 65 20 77  ** merged code w
3416e 6f 75 6c 64 20 6e 65 65 64 20 74 6f 20 68 61 6e  ould need to han
3416f 64 6c 65 20 3e 4d 45 52 47 45 5f 43 4f 55 4e 54  dle >MERGE_COUNT
34170 20 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20 77   segments, and w
34171 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 6e 65 65  ould.** also nee
34172 64 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20  d to be able to 
34173 6f 70 74 69 6f 6e 61 6c 6c 79 20 6f 70 74 69 6d  optionally optim
34174 69 7a 65 20 61 77 61 79 20 64 65 6c 65 74 65 73  ize away deletes
34175 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
34176 75 63 74 20 4f 70 74 4c 65 61 76 65 73 52 65 61  uct OptLeavesRea
34177 64 65 72 20 7b 0a 20 20 2f 2a 20 53 65 67 6d 65  der {.  /* Segme
34178 6e 74 20 6e 75 6d 62 65 72 2c 20 74 6f 20 6f 72  nt number, to or
34179 64 65 72 20 72 65 61 64 65 72 73 20 62 79 20 61  der readers by a
3417a 67 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 67  ge. */.  int seg
3417b 6d 65 6e 74 3b 0a 20 20 4c 65 61 76 65 73 52 65  ment;.  LeavesRe
3417c 61 64 65 72 20 72 65 61 64 65 72 3b 0a 7d 20 4f  ader reader;.} O
3417d 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 3b 0a  ptLeavesReader;.
3417e 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c  .static int optL
3417f 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64  eavesReaderAtEnd
34180 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  (OptLeavesReader
34181 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65   *pReader){.  re
34182 74 75 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65  turn leavesReade
34183 72 41 74 45 6e 64 28 26 70 52 65 61 64 65 72 2d  rAtEnd(&pReader-
34184 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74  >reader);.}.stat
34185 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73  ic int optLeaves
34186 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
34187 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
34188 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
34189 75 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72  urn leavesReader
3418a 54 65 72 6d 42 79 74 65 73 28 26 70 52 65 61 64  TermBytes(&pRead
3418b 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73  er->reader);.}.s
3418c 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
3418d 20 2a 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65   *optLeavesReade
3418e 72 44 61 74 61 28 4f 70 74 4c 65 61 76 65 73 52  rData(OptLeavesR
3418f 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
34190 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73  .  return leaves
34191 52 65 61 64 65 72 44 61 74 61 28 26 70 52 65 61  ReaderData(&pRea
34192 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a  der->reader);.}.
34193 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c 65  static int optLe
34194 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
34195 74 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65 61  tes(OptLeavesRea
34196 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
34197 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65   return leavesRe
34198 61 64 65 72 44 61 74 61 42 79 74 65 73 28 26 70  aderDataBytes(&p
34199 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b  Reader->reader);
3419a 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .}.static const 
3419b 63 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73 52  char *optLeavesR
3419c 65 61 64 65 72 54 65 72 6d 28 4f 70 74 4c 65 61  eaderTerm(OptLea
3419d 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
3419e 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65  er){.  return le
3419f 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 26  avesReaderTerm(&
341a0 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29  pReader->reader)
341a1 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  ;.}.static int o
341a2 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 53 74  ptLeavesReaderSt
341a3 65 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ep(fulltext_vtab
341a4 20 2a 76 2c 20 4f 70 74 4c 65 61 76 65 73 52 65   *v, OptLeavesRe
341a5 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
341a6 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52    return leavesR
341a7 65 61 64 65 72 53 74 65 70 28 76 2c 20 26 70 52  eaderStep(v, &pR
341a8 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a  eader->reader);.
341a9 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74  }.static int opt
341aa 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  LeavesReaderTerm
341ab 43 6d 70 28 4f 70 74 4c 65 61 76 65 73 52 65 61  Cmp(OptLeavesRea
341ac 64 65 72 20 2a 6c 72 31 2c 20 4f 70 74 4c 65 61  der *lr1, OptLea
341ad 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32 29 7b  vesReader *lr2){
341ae 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73  .  return leaves
341af 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 6c  ReaderTermCmp(&l
341b0 72 31 2d 3e 72 65 61 64 65 72 2c 20 26 6c 72 32  r1->reader, &lr2
341b1 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 2f 2a 20  ->reader);.}./* 
341b2 4f 72 64 65 72 20 62 79 20 74 65 72 6d 20 61 73  Order by term as
341b3 63 65 6e 64 69 6e 67 2c 20 73 65 67 6d 65 6e 74  cending, segment
341b4 20 61 73 63 65 6e 64 69 6e 67 20 28 6f 6c 64 65   ascending (olde
341b5 73 74 20 74 6f 20 6e 65 77 65 73 74 29 2c 20 77  st to newest), w
341b6 69 74 68 0a 2a 2a 20 65 78 68 61 75 73 74 65 64  ith.** exhausted
341b7 20 72 65 61 64 65 72 73 20 74 6f 20 74 68 65 20   readers to the 
341b8 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  end..*/.static i
341b9 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64  nt optLeavesRead
341ba 65 72 43 6d 70 28 4f 70 74 4c 65 61 76 65 73 52  erCmp(OptLeavesR
341bb 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f 70 74 4c  eader *lr1, OptL
341bc 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32  eavesReader *lr2
341bd 29 7b 0a 20 20 69 6e 74 20 63 20 3d 20 6f 70 74  ){.  int c = opt
341be 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  LeavesReaderTerm
341bf 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b 0a 20  Cmp(lr1, lr2);. 
341c0 20 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75   if( c!=0 ) retu
341c1 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20 6c  rn c;.  return l
341c2 72 31 2d 3e 73 65 67 6d 65 6e 74 2d 6c 72 32 2d  r1->segment-lr2-
341c3 3e 73 65 67 6d 65 6e 74 3b 0a 7d 0a 2f 2a 20 42  >segment;.}./* B
341c4 75 62 62 6c 65 20 70 4c 72 5b 30 5d 20 74 6f 20  ubble pLr[0] to 
341c5 61 70 70 72 6f 70 72 69 61 74 65 20 70 6c 61 63  appropriate plac
341c6 65 20 69 6e 20 70 4c 72 5b 31 2e 2e 6e 4c 72 2d  e in pLr[1..nLr-
341c7 31 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68 61  1].  Assumes tha
341c8 74 0a 2a 2a 20 70 4c 72 5b 31 2e 2e 6e 4c 72 2d  t.** pLr[1..nLr-
341c9 31 5d 20 69 73 20 61 6c 72 65 61 64 79 20 73 6f  1] is already so
341ca 72 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rted..*/.static 
341cb 76 6f 69 64 20 6f 70 74 4c 65 61 76 65 73 52 65  void optLeavesRe
341cc 61 64 65 72 52 65 6f 72 64 65 72 28 4f 70 74 4c  aderReorder(OptL
341cd 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 4c 72  eavesReader *pLr
341ce 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20 20 77 68  , int nLr){.  wh
341cf 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26 20 6f 70  ile( nLr>1 && op
341d0 74 4c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70  tLeavesReaderCmp
341d1 28 70 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29  (pLr, pLr+1)>0 )
341d2 7b 0a 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52  {.    OptLeavesR
341d3 65 61 64 65 72 20 74 6d 70 20 3d 20 70 4c 72 5b  eader tmp = pLr[
341d4 30 5d 3b 0a 20 20 20 20 70 4c 72 5b 30 5d 20 3d  0];.    pLr[0] =
341d5 20 70 4c 72 5b 31 5d 3b 0a 20 20 20 20 70 4c 72   pLr[1];.    pLr
341d6 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20 20 20 6e  [1] = tmp;.    n
341d7 4c 72 2d 2d 3b 0a 20 20 20 20 70 4c 72 2b 2b 3b  Lr--;.    pLr++;
341d8 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 6f 70 74 69 6d  .  }.}../* optim
341d9 69 7a 65 28 29 20 68 65 6c 70 65 72 20 66 75 6e  ize() helper fun
341da 63 74 69 6f 6e 2e 20 20 50 75 74 20 74 68 65 20  ction.  Put the 
341db 72 65 61 64 65 72 73 20 69 6e 20 6f 72 64 65 72  readers in order
341dc 20 61 6e 64 20 69 74 65 72 61 74 65 0a 2a 2a 20   and iterate.** 
341dd 74 68 72 6f 75 67 68 20 74 68 65 6d 2c 20 6d 65  through them, me
341de 72 67 69 6e 67 20 64 6f 63 6c 69 73 74 73 20 66  rging doclists f
341df 6f 72 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d  or matching term
341e0 73 20 69 6e 74 6f 20 70 57 72 69 74 65 72 2e 0a  s into pWriter..
341e1 2a 2a 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54  ** Returns SQLIT
341e2 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c  E_OK on success,
341e3 20 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 65   or the SQLite e
341e4 72 72 6f 72 20 63 6f 64 65 20 77 68 69 63 68 0a  rror code which.
341e5 2a 2a 20 70 72 65 76 65 6e 74 65 64 20 73 75 63  ** prevented suc
341e6 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cess..*/.static 
341e7 69 6e 74 20 6f 70 74 69 6d 69 7a 65 49 6e 74 65  int optimizeInte
341e8 72 6e 61 6c 28 66 75 6c 6c 74 65 78 74 5f 76 74  rnal(fulltext_vt
341e9 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20  ab *v,.         
341ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
341eb 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64     OptLeavesRead
341ec 65 72 20 2a 72 65 61 64 65 72 73 2c 20 69 6e 74  er *readers, int
341ed 20 6e 52 65 61 64 65 72 73 2c 0a 20 20 20 20 20   nReaders,.     
341ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
341ef 20 20 20 20 20 20 20 4c 65 61 66 57 72 69 74 65         LeafWrite
341f0 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69  r *pWriter){.  i
341f1 6e 74 20 69 2c 20 72 63 20 3d 20 53 51 4c 49 54  nt i, rc = SQLIT
341f2 45 5f 4f 4b 3b 0a 20 20 44 61 74 61 42 75 66 66  E_OK;.  DataBuff
341f3 65 72 20 64 6f 63 6c 69 73 74 2c 20 6d 65 72 67  er doclist, merg
341f4 65 64 2c 20 74 6d 70 3b 0a 0a 20 20 2f 2a 20 4f  ed, tmp;..  /* O
341f5 72 64 65 72 20 74 68 65 20 72 65 61 64 65 72 73  rder the readers
341f6 2e 20 2a 2f 0a 20 20 69 20 3d 20 6e 52 65 61 64  . */.  i = nRead
341f7 65 72 73 3b 0a 20 20 77 68 69 6c 65 28 20 69 2d  ers;.  while( i-
341f8 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 6f 70 74  - > 0 ){.    opt
341f9 4c 65 61 76 65 73 52 65 61 64 65 72 52 65 6f 72  LeavesReaderReor
341fa 64 65 72 28 26 72 65 61 64 65 72 73 5b 69 5d 2c  der(&readers[i],
341fb 20 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20   nReaders-i);.  
341fc 7d 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  }..  dataBufferI
341fd 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 4c 45  nit(&doclist, LE
341fe 41 46 5f 4d 41 58 29 3b 0a 20 20 64 61 74 61 42  AF_MAX);.  dataB
341ff 75 66 66 65 72 49 6e 69 74 28 26 6d 65 72 67 65  ufferInit(&merge
34200 64 2c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 0a 20  d, LEAF_MAX);.. 
34201 20 2f 2a 20 45 78 68 61 75 73 74 65 64 20 72 65   /* Exhausted re
34202 61 64 65 72 73 20 62 75 62 62 6c 65 20 74 6f 20  aders bubble to 
34203 74 68 65 20 65 6e 64 2c 20 73 6f 20 77 68 65 6e  the end, so when
34204 20 74 68 65 20 66 69 72 73 74 20 72 65 61 64 65   the first reade
34205 72 20 69 73 0a 20 20 2a 2a 20 61 74 20 65 6f 66  r is.  ** at eof
34206 2c 20 61 6c 6c 20 61 72 65 20 61 74 20 65 6f 66  , all are at eof
34207 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ..  */.  while( 
34208 21 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  !optLeavesReader
34209 41 74 45 6e 64 28 26 72 65 61 64 65 72 73 5b 30  AtEnd(&readers[0
3420a 5d 29 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 46 69  ]) ){..    /* Fi
3420b 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
3420c 79 20 72 65 61 64 65 72 73 20 73 68 61 72 65 20  y readers share 
3420d 74 68 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a  the next term. *
3420e 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  /.    for(i=1; i
3420f 3c 6e 52 65 61 64 65 72 73 20 26 26 20 21 6f 70  <nReaders && !op
34210 74 4c 65 61 76 65 73 52 65 61 64 65 72 41 74 45  tLeavesReaderAtE
34211 6e 64 28 26 72 65 61 64 65 72 73 5b 69 5d 29 3b  nd(&readers[i]);
34212 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
34213 20 30 21 3d 6f 70 74 4c 65 61 76 65 73 52 65 61   0!=optLeavesRea
34214 64 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64  derTermCmp(&read
34215 65 72 73 5b 30 5d 2c 20 26 72 65 61 64 65 72 73  ers[0], &readers
34216 5b 69 5d 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  [i]) ) break;.  
34217 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 70 65 63    }..    /* Spec
34218 69 61 6c 2d 63 61 73 65 20 66 6f 72 20 6e 6f 20  ial-case for no 
34219 6d 65 72 67 65 2e 20 2a 2f 0a 20 20 20 20 69 66  merge. */.    if
3421a 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ( i==1 ){.      
3421b 2f 2a 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e  /* Trim deletion
3421c 73 20 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69  s from the docli
3421d 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74  st. */.      dat
3421e 61 42 75 66 66 65 72 52 65 73 65 74 28 26 6d 65  aBufferReset(&me
3421f 72 67 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63  rged);.      doc
34220 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41  ListTrim(DL_DEFA
34221 55 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ULT,.           
34222 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73         optLeaves
34223 52 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64  ReaderData(&read
34224 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20  ers[0]),.       
34225 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65             optLe
34226 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
34227 74 65 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29  tes(&readers[0])
34228 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
34229 20 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41 55      -1, DL_DEFAU
3422a 4c 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20 20  LT, &merged);.  
3422b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 44    }else{.      D
3422c 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64 65 72  LReader dlReader
3422d 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a  s[MERGE_COUNT];.
3422e 20 20 20 20 20 20 69 6e 74 20 69 52 65 61 64 65        int iReade
3422f 72 2c 20 6e 52 65 61 64 65 72 73 3b 0a 0a 20 20  r, nReaders;..  
34230 20 20 20 20 2f 2a 20 50 72 69 6d 65 20 74 68 65      /* Prime the
34231 20 70 69 70 65 6c 69 6e 65 20 77 69 74 68 20 74   pipeline with t
34232 68 65 20 66 69 72 73 74 20 72 65 61 64 65 72 27  he first reader'
34233 73 20 64 6f 63 6c 69 73 74 2e 20 20 41 66 74 65  s doclist.  Afte
34234 72 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 65 20 70  r.      ** one p
34235 61 73 73 20 69 6e 64 65 78 20 30 20 77 69 6c 6c  ass index 0 will
34236 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 61   reference the a
34237 63 63 75 6d 75 6c 61 74 65 64 20 64 6f 63 6c 69  ccumulated docli
34238 73 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  st..      */.   
34239 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65     dlrInit(&dlRe
3423a 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46  aders[0], DL_DEF
3423b 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20  AULT,.          
3423c 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
3423d 64 65 72 44 61 74 61 28 26 72 65 61 64 65 72 73  derData(&readers
3423e 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  [0]),.          
3423f 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
34240 64 65 72 44 61 74 61 42 79 74 65 73 28 26 72 65  derDataBytes(&re
34241 61 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20 20  aders[0]));.    
34242 20 20 69 52 65 61 64 65 72 20 3d 20 31 3b 0a 0a    iReader = 1;..
34243 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 52        assert( iR
34244 65 61 64 65 72 3c 69 20 29 3b 20 20 2f 2a 20 4d  eader<i );  /* M
34245 75 73 74 20 65 78 65 63 75 74 65 20 74 68 65 20  ust execute the 
34246 6c 6f 6f 70 20 61 74 20 6c 65 61 73 74 20 6f 6e  loop at least on
34247 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  ce. */.      whi
34248 6c 65 28 20 69 52 65 61 64 65 72 3c 69 20 29 7b  le( iReader<i ){
34249 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 72 67  .        /* Merg
3424a 65 20 31 36 20 69 6e 70 75 74 73 20 70 65 72 20  e 16 inputs per 
3424b 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20  pass. */.       
3424c 20 66 6f 72 28 20 6e 52 65 61 64 65 72 73 3d 31   for( nReaders=1
3424d 3b 20 69 52 65 61 64 65 72 3c 69 20 26 26 20 6e  ; iReader<i && n
3424e 52 65 61 64 65 72 73 3c 4d 45 52 47 45 5f 43 4f  Readers<MERGE_CO
3424f 55 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20  UNT;.           
34250 20 20 69 52 65 61 64 65 72 2b 2b 2c 20 6e 52 65    iReader++, nRe
34251 61 64 65 72 73 2b 2b 20 29 7b 0a 20 20 20 20 20  aders++ ){.     
34252 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c       dlrInit(&dl
34253 52 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73  Readers[nReaders
34254 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20  ], DL_DEFAULT,. 
34255 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34256 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
34257 44 61 74 61 28 26 72 65 61 64 65 72 73 5b 69 52  Data(&readers[iR
34258 65 61 64 65 72 5d 29 2c 0a 20 20 20 20 20 20 20  eader]),.       
34259 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65             optLe
3425a 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
3425b 74 65 73 28 26 72 65 61 64 65 72 73 5b 69 52 65  tes(&readers[iRe
3425c 61 64 65 72 5d 29 29 3b 0a 20 20 20 20 20 20 20  ader]));.       
3425d 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d   }..        /* M
3425e 65 72 67 65 20 64 6f 63 6c 69 73 74 73 20 61 6e  erge doclists an
3425f 64 20 73 77 61 70 20 72 65 73 75 6c 74 20 69 6e  d swap result in
34260 74 6f 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e 20  to accumulator. 
34261 2a 2f 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  */.        dataB
34262 75 66 66 65 72 52 65 73 65 74 28 26 6d 65 72 67  ufferReset(&merg
34263 65 64 29 3b 0a 20 20 20 20 20 20 20 20 64 6f 63  ed);.        doc
34264 4c 69 73 74 4d 65 72 67 65 28 26 6d 65 72 67 65  ListMerge(&merge
34265 64 2c 20 64 6c 52 65 61 64 65 72 73 2c 20 6e 52  d, dlReaders, nR
34266 65 61 64 65 72 73 29 3b 0a 20 20 20 20 20 20 20  eaders);.       
34267 20 74 6d 70 20 3d 20 6d 65 72 67 65 64 3b 0a 20   tmp = merged;. 
34268 20 20 20 20 20 20 20 6d 65 72 67 65 64 20 3d 20         merged = 
34269 64 6f 63 6c 69 73 74 3b 0a 20 20 20 20 20 20 20  doclist;.       
3426a 20 64 6f 63 6c 69 73 74 20 3d 20 74 6d 70 3b 0a   doclist = tmp;.
3426b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
3426c 6e 52 65 61 64 65 72 73 2d 2d 20 3e 20 30 20 29  nReaders-- > 0 )
3426d 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6c 72 44  {.          dlrD
3426e 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72  estroy(&dlReader
3426f 73 5b 6e 52 65 61 64 65 72 73 5d 29 3b 0a 20 20  s[nReaders]);.  
34270 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
34271 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 64 20   /* Accumulated 
34272 64 6f 63 6c 69 73 74 20 74 6f 20 72 65 61 64 65  doclist to reade
34273 72 20 30 20 66 6f 72 20 6e 65 78 74 20 70 61 73  r 0 for next pas
34274 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 6c  s. */.        dl
34275 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72 73  rInit(&dlReaders
34276 5b 30 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c  [0], DL_DEFAULT,
34277 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c 20   doclist.pData, 
34278 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29 3b 0a  doclist.nData);.
34279 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
3427a 2a 20 44 65 73 74 72 6f 79 20 72 65 61 64 65 72  * Destroy reader
3427b 20 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 69   that was left i
3427c 6e 20 74 68 65 20 70 69 70 65 6c 69 6e 65 2e 20  n the pipeline. 
3427d 2a 2f 0a 20 20 20 20 20 20 64 6c 72 44 65 73 74  */.      dlrDest
3427e 72 6f 79 28 26 64 6c 52 65 61 64 65 72 73 5b 30  roy(&dlReaders[0
3427f 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 72  ]);..      /* Tr
34280 69 6d 20 64 65 6c 65 74 69 6f 6e 73 20 66 72 6f  im deletions fro
34281 6d 20 74 68 65 20 64 6f 63 6c 69 73 74 2e 20 2a  m the doclist. *
34282 2f 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  /.      dataBuff
34283 65 72 52 65 73 65 74 28 26 6d 65 72 67 65 64 29  erReset(&merged)
34284 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69 73 74 54  ;.      docListT
34285 72 69 6d 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20  rim(DL_DEFAULT, 
34286 64 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c 20 64  doclist.pData, d
34287 6f 63 6c 69 73 74 2e 6e 44 61 74 61 2c 0a 20 20  oclist.nData,.  
34288 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34289 2d 31 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  -1, DL_DEFAULT, 
3428a 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 7d 0a  &merged);.    }.
3428b 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 61 73  .    /* Only pas
3428c 73 20 64 6f 63 6c 69 73 74 73 20 77 69 74 68 20  s doclists with 
3428d 68 69 74 73 20 28 73 6b 69 70 20 69 66 20 61 6c  hits (skip if al
3428e 6c 20 68 69 74 73 20 64 65 6c 65 74 65 64 29 2e  l hits deleted).
3428f 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 72 67   */.    if( merg
34290 65 64 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20  ed.nData>0 ){.  
34291 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69      rc = leafWri
34292 74 65 72 53 74 65 70 28 76 2c 20 70 57 72 69 74  terStep(v, pWrit
34293 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
34294 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
34295 74 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72  tLeavesReaderTer
34296 6d 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a  m(&readers[0]),.
34297 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34298 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61            optLea
34299 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74  vesReaderTermByt
3429a 65 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c  es(&readers[0]),
3429b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3429c 20 20 20 20 20 20 20 20 20 20 20 6d 65 72 67 65             merge
3429d 64 2e 70 44 61 74 61 2c 20 6d 65 72 67 65 64 2e  d.pData, merged.
3429e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 69 66  nData);.      if
3429f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
342a0 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 20 20  ) goto err;.    
342a1 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20 6d  }..    /* Step m
342a2 65 72 67 65 64 20 72 65 61 64 65 72 73 20 74 6f  erged readers to
342a3 20 6e 65 78 74 20 74 65 72 6d 20 61 6e 64 20 72   next term and r
342a4 65 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 77  eorder. */.    w
342a5 68 69 6c 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b  hile( i-- > 0 ){
342a6 0a 20 20 20 20 20 20 72 63 20 3d 20 6f 70 74 4c  .      rc = optL
342a7 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28  eavesReaderStep(
342a8 76 2c 20 26 72 65 61 64 65 72 73 5b 69 5d 29 3b  v, &readers[i]);
342a9 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
342aa 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
342ab 65 72 72 3b 0a 0a 20 20 20 20 20 20 6f 70 74 4c  err;..      optL
342ac 65 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64  eavesReaderReord
342ad 65 72 28 26 72 65 61 64 65 72 73 5b 69 5d 2c 20  er(&readers[i], 
342ae 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 20  nReaders-i);.   
342af 20 7d 0a 20 20 7d 0a 0a 20 65 72 72 3a 0a 20 20   }.  }.. err:.  
342b0 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
342b1 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 64  y(&doclist);.  d
342b2 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
342b3 28 26 6d 65 72 67 65 64 29 3b 0a 20 20 72 65 74  (&merged);.  ret
342b4 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 49 6d  urn rc;.}../* Im
342b5 70 6c 65 6d 65 6e 74 20 6f 70 74 69 6d 69 7a 65  plement optimize
342b6 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  () function for 
342b7 46 54 53 33 2e 20 20 6f 70 74 69 6d 69 7a 65 28  FTS3.  optimize(
342b8 74 29 20 6d 65 72 67 65 73 20 61 6c 6c 0a 2a 2a  t) merges all.**
342b9 20 73 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65   segments in the
342ba 20 66 74 73 20 69 6e 64 65 78 20 69 6e 74 6f 20   fts index into 
342bb 61 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74  a single segment
342bc 2e 20 20 27 74 27 20 69 73 20 74 68 65 20 6d 61  .  't' is the ma
342bd 67 69 63 0a 2a 2a 20 74 61 62 6c 65 2d 6e 61 6d  gic.** table-nam
342be 65 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74  ed column..*/.st
342bf 61 74 69 63 20 76 6f 69 64 20 6f 70 74 69 6d 69  atic void optimi
342c0 7a 65 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63  zeFunc(sqlite3_c
342c1 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74  ontext *pContext
342c2 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
342c3 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61             int a
342c4 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
342c5 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 66 75  ue **argv){.  fu
342c6 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
342c7 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61 72  Cursor;.  if( ar
342c8 67 63 3e 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  gc>1 ){.    sqli
342c9 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
342ca 28 70 43 6f 6e 74 65 78 74 2c 20 22 65 78 63 65  (pContext, "exce
342cb 73 73 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ss arguments to 
342cc 6f 70 74 69 6d 69 7a 65 28 29 22 2c 2d 31 29 3b  optimize()",-1);
342cd 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
342ce 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
342cf 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45  argv[0])!=SQLITE
342d0 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20  _BLOB ||.       
342d1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
342d2 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
342d3 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  )!=sizeof(pCurso
342d4 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  r) ){.    sqlite
342d5 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
342d6 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c 65 67 61  Context, "illega
342d7 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  l first argument
342d8 20 74 6f 20 6f 70 74 69 6d 69 7a 65 22 2c 2d 31   to optimize",-1
342d9 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
342da 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
342db 3b 0a 20 20 20 20 69 6e 74 20 69 2c 20 72 63 2c  ;.    int i, rc,
342dc 20 69 4d 61 78 4c 65 76 65 6c 3b 0a 20 20 20 20   iMaxLevel;.    
342dd 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
342de 2a 72 65 61 64 65 72 73 3b 0a 20 20 20 20 69 6e  *readers;.    in
342df 74 20 6e 52 65 61 64 65 72 73 3b 0a 20 20 20 20  t nReaders;.    
342e0 4c 65 61 66 57 72 69 74 65 72 20 77 72 69 74 65  LeafWriter write
342e1 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  r;.    sqlite3_s
342e2 74 6d 74 20 2a 73 3b 0a 0a 20 20 20 20 6d 65 6d  tmt *s;..    mem
342e3 63 70 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71  cpy(&pCursor, sq
342e4 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
342e5 28 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f  (argv[0]), sizeo
342e6 66 28 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20  f(pCursor));.   
342e7 20 76 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62   v = cursor_vtab
342e8 28 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20  (pCursor);..    
342e9 2f 2a 20 46 6c 75 73 68 20 61 6e 79 20 62 75 66  /* Flush any buf
342ea 66 65 72 65 64 20 75 70 64 61 74 65 73 20 62 65  fered updates be
342eb 66 6f 72 65 20 6f 70 74 69 6d 69 7a 69 6e 67 2e  fore optimizing.
342ec 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 66 6c 75   */.    rc = flu
342ed 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  shPendingTerms(v
342ee 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
342ef 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
342f0 65 72 72 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73  err;..    rc = s
342f1 65 67 64 69 72 5f 63 6f 75 6e 74 28 76 2c 20 26  egdir_count(v, &
342f2 6e 52 65 61 64 65 72 73 2c 20 26 69 4d 61 78 4c  nReaders, &iMaxL
342f3 65 76 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  evel);.    if( r
342f4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
342f5 6f 74 6f 20 65 72 72 3b 0a 20 20 20 20 69 66 28  oto err;.    if(
342f6 20 6e 52 65 61 64 65 72 73 3d 3d 30 20 7c 7c 20   nReaders==0 || 
342f7 6e 52 65 61 64 65 72 73 3d 3d 31 20 29 7b 0a 20  nReaders==1 ){. 
342f8 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
342f9 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
342fa 74 2c 20 22 49 6e 64 65 78 20 61 6c 72 65 61 64  t, "Index alread
342fb 79 20 6f 70 74 69 6d 61 6c 22 2c 20 2d 31 2c 0a  y optimal", -1,.
342fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
342fd 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
342fe 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
342ff 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20  return;.    }.. 
34300 20 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f     rc = sql_get_
34301 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47  statement(v, SEG
34302 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53  DIR_SELECT_ALL_S
34303 54 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 69 66  TMT, &s);.    if
34304 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34305 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20  ) goto err;..   
34306 20 72 65 61 64 65 72 73 20 3d 20 73 71 6c 69 74   readers = sqlit
34307 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65 61 64 65  e3_malloc(nReade
34308 72 73 2a 73 69 7a 65 6f 66 28 72 65 61 64 65 72  rs*sizeof(reader
34309 73 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20  s[0]));.    if( 
3430a 72 65 61 64 65 72 73 3d 3d 4e 55 4c 4c 20 29 20  readers==NULL ) 
3430b 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 2f  goto err;..    /
3430c 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 72  * Note that ther
3430d 65 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20 62  e will already b
3430e 65 20 61 20 73 65 67 6d 65 6e 74 20 61 74 20 74  e a segment at t
3430f 68 69 73 20 70 6f 73 69 74 69 6f 6e 0a 20 20 20  his position.   
34310 20 2a 2a 20 75 6e 74 69 6c 20 77 65 20 63 61 6c   ** until we cal
34311 6c 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65 28  l segdir_delete(
34312 29 20 6f 6e 20 69 4d 61 78 4c 65 76 65 6c 2e 0a  ) on iMaxLevel..
34313 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 61 66 57      */.    leafW
34314 72 69 74 65 72 49 6e 69 74 28 69 4d 61 78 4c 65  riterInit(iMaxLe
34315 76 65 6c 2c 20 30 2c 20 26 77 72 69 74 65 72 29  vel, 0, &writer)
34316 3b 0a 0a 20 20 20 20 69 20 3d 20 30 3b 0a 20 20  ;..    i = 0;.  
34317 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73    while( (rc = s
34318 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29 3d  qlite3_step(s))=
34319 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
3431a 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
3431b 34 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74  4 iStart = sqlit
3431c 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
3431d 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  s, 0);.      sql
3431e 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 20 3d  ite_int64 iEnd =
3431f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
34320 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20  int64(s, 1);.   
34321 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
34322 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74  RootData = sqlit
34323 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73  e3_column_blob(s
34324 2c 20 32 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  , 2);.      int 
34325 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69  nRootData = sqli
34326 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
34327 28 73 2c 20 32 29 3b 0a 0a 20 20 20 20 20 20 61  (s, 2);..      a
34328 73 73 65 72 74 28 20 69 3c 6e 52 65 61 64 65 72  ssert( i<nReader
34329 73 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  s );.      rc = 
3432a 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74  leavesReaderInit
3432b 28 76 2c 20 2d 31 2c 20 69 53 74 61 72 74 2c 20  (v, -1, iStart, 
3432c 69 45 6e 64 2c 20 70 52 6f 6f 74 44 61 74 61 2c  iEnd, pRootData,
3432d 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20   nRootData,.    
3432e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3432f 20 20 20 20 20 20 20 20 26 72 65 61 64 65 72 73          &readers
34330 5b 69 5d 2e 72 65 61 64 65 72 29 3b 0a 20 20 20  [i].reader);.   
34331 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
34332 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a 20  E_OK ) break;.. 
34333 20 20 20 20 20 72 65 61 64 65 72 73 5b 69 5d 2e       readers[i].
34334 73 65 67 6d 65 6e 74 20 3d 20 69 3b 0a 20 20 20  segment = i;.   
34335 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20     i++;.    }.. 
34336 20 20 20 2f 2a 20 49 66 20 77 65 20 6d 61 6e 61     /* If we mana
34337 67 65 64 20 74 6f 20 73 75 63 63 65 73 73 66 75  ged to successfu
34338 6c 6c 79 20 72 65 61 64 20 74 68 65 6d 20 61 6c  lly read them al
34339 6c 2c 20 6f 70 74 69 6d 69 7a 65 20 74 68 65 6d  l, optimize them
3433a 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d  . */.    if( rc=
3433b 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
3433c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3d        assert( i=
3433d 3d 6e 52 65 61 64 65 72 73 20 29 3b 0a 20 20 20  =nReaders );.   
3433e 20 20 20 72 63 20 3d 20 6f 70 74 69 6d 69 7a 65     rc = optimize
3433f 49 6e 74 65 72 6e 61 6c 28 76 2c 20 72 65 61 64  Internal(v, read
34340 65 72 73 2c 20 6e 52 65 61 64 65 72 73 2c 20 26  ers, nReaders, &
34341 77 72 69 74 65 72 29 3b 0a 20 20 20 20 7d 0a 0a  writer);.    }..
34342 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e      while( i-- >
34343 20 30 20 29 7b 0a 20 20 20 20 20 20 6c 65 61 76   0 ){.      leav
34344 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
34345 26 72 65 61 64 65 72 73 5b 69 5d 2e 72 65 61 64  &readers[i].read
34346 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  er);.    }.    s
34347 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65 61 64  qlite3_free(read
34348 65 72 73 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  ers);..    /* If
34349 20 77 65 27 76 65 20 73 75 63 63 65 73 73 66 75   we've successfu
3434a 6c 6c 79 20 67 6f 74 74 65 6e 20 74 6f 20 68 65  lly gotten to he
3434b 72 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6f  re, delete the o
3434c 6c 64 20 73 65 67 6d 65 6e 74 73 0a 20 20 20 20  ld segments.    
3434d 2a 2a 20 61 6e 64 20 66 6c 75 73 68 20 74 68 65  ** and flush the
3434e 20 69 6e 74 65 72 69 6f 72 20 73 74 72 75 63 74   interior struct
3434f 75 72 65 20 6f 66 20 74 68 65 20 6e 65 77 20 73  ure of the new s
34350 65 67 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20  egment..    */. 
34351 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
34352 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 66 6f  E_OK ){.      fo
34353 72 28 20 69 3d 30 3b 20 69 3c 3d 69 4d 61 78 4c  r( i=0; i<=iMaxL
34354 65 76 65 6c 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  evel; i++ ){.   
34355 20 20 20 20 20 72 63 20 3d 20 73 65 67 64 69 72       rc = segdir
34356 5f 64 65 6c 65 74 65 28 76 2c 20 69 29 3b 0a 20  _delete(v, i);. 
34357 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
34358 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b  QLITE_OK ) break
34359 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
3435a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3435b 4f 4b 20 29 20 72 63 20 3d 20 6c 65 61 66 57 72  OK ) rc = leafWr
3435c 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20  iterFinalize(v, 
3435d 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 7d 0a  &writer);.    }.
3435e 0a 20 20 20 20 6c 65 61 66 57 72 69 74 65 72 44  .    leafWriterD
3435f 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
34360 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ..    if( rc!=SQ
34361 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
34362 72 72 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33  rr;..    sqlite3
34363 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
34364 6e 74 65 78 74 2c 20 22 49 6e 64 65 78 20 6f 70  ntext, "Index op
34365 74 69 6d 69 7a 65 64 22 2c 20 2d 31 2c 20 53 51  timized", -1, SQ
34366 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
34367 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 2f    return;..    /
34368 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 45  * TODO(shess): E
34369 72 72 6f 72 2d 68 61 6e 64 6c 69 6e 67 20 6e 65  rror-handling ne
3436a 65 64 73 20 74 6f 20 62 65 20 69 6d 70 72 6f 76  eds to be improv
3436b 65 64 20 61 6c 6f 6e 67 20 74 68 65 0a 20 20 20  ed along the.   
3436c 20 2a 2a 20 6c 69 6e 65 73 20 6f 66 20 74 68 65   ** lines of the
3436d 20 64 75 6d 70 5f 20 66 75 6e 63 74 69 6f 6e 73   dump_ functions
3436e 2e 0a 20 20 20 20 2a 2f 0a 20 65 72 72 3a 0a 20  ..    */. err:. 
3436f 20 20 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20     {.      char 
34370 62 75 66 5b 35 31 32 5d 3b 0a 20 20 20 20 20 20  buf[512];.      
34371 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
34372 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75  (sizeof(buf), bu
34373 66 2c 20 22 45 72 72 6f 72 20 69 6e 20 6f 70 74  f, "Error in opt
34374 69 6d 69 7a 65 3a 20 25 73 22 2c 0a 20 20 20 20  imize: %s",.    
34375 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34376 20 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73     sqlite3_errms
34377 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  g(sqlite3_contex
34378 74 5f 64 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e  t_db_handle(pCon
34379 74 65 78 74 29 29 29 3b 0a 20 20 20 20 20 20 73  text)));.      s
3437a 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
3437b 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 62 75  ror(pContext, bu
3437c 66 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20  f, -1);.    }.  
3437d 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  }.}..#ifdef SQLI
3437e 54 45 5f 54 45 53 54 0a 2f 2a 20 47 65 6e 65 72  TE_TEST./* Gener
3437f 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6f 66 20  ate an error of 
34380 74 68 65 20 66 6f 72 6d 20 22 3c 70 72 65 66 69  the form "<prefi
34381 78 3e 3a 20 3c 6d 73 67 3e 22 2e 20 20 49 66 20  x>: <msg>".  If 
34382 6d 73 67 20 69 73 20 4e 55 4c 4c 2c 0a 2a 2a 20  msg is NULL,.** 
34383 70 75 6c 6c 20 74 68 65 20 65 72 72 6f 72 20 66  pull the error f
34384 72 6f 6d 20 74 68 65 20 63 6f 6e 74 65 78 74 27  rom the context'
34385 73 20 64 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  s db handle..*/.
34386 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65  static void gene
34387 72 61 74 65 45 72 72 6f 72 28 73 71 6c 69 74 65  rateError(sqlite
34388 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
34389 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
3438a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
3438b 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 65 66 69  onst char *prefi
3438c 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6d  x, const char *m
3438d 73 67 29 7b 0a 20 20 63 68 61 72 20 62 75 66 5b  sg){.  char buf[
3438e 35 31 32 5d 3b 0a 20 20 69 66 28 20 6d 73 67 3d  512];.  if( msg=
3438f 3d 4e 55 4c 4c 20 29 20 6d 73 67 20 3d 20 73 71  =NULL ) msg = sq
34390 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c  lite3_errmsg(sql
34391 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
34392 68 61 6e 64 6c 65 28 70 43 6f 6e 74 65 78 74 29  handle(pContext)
34393 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  );.  sqlite3_snp
34394 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
34395 29 2c 20 62 75 66 2c 20 22 25 73 3a 20 25 73 22  ), buf, "%s: %s"
34396 2c 20 70 72 65 66 69 78 2c 20 6d 73 67 29 3b 0a  , prefix, msg);.
34397 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
34398 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  _error(pContext,
34399 20 62 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a   buf, -1);.}../*
3439a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
3439b 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 68 65 20   to collect the 
3439c 73 65 74 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  set of terms in 
3439d 74 68 65 20 73 65 67 6d 65 6e 74 20 69 6e 74 6f  the segment into
3439e 0a 2a 2a 20 70 54 65 72 6d 73 2e 20 20 54 68 65  .** pTerms.  The
3439f 20 73 65 67 6d 65 6e 74 20 69 73 20 64 65 66 69   segment is defi
343a0 6e 65 64 20 62 79 20 74 68 65 20 6c 65 61 66 20  ned by the leaf 
343a1 6e 6f 64 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a  nodes between.**
343a2 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61   iStartBlockid a
343a3 6e 64 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20  nd iEndBlockid, 
343a4 69 6e 63 6c 75 73 69 76 65 2c 20 6f 72 20 62 79  inclusive, or by
343a5 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
343a6 0a 2a 2a 20 70 52 6f 6f 74 44 61 74 61 20 69 66  .** pRootData if
343a7 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 69   iStartBlockid i
343a8 73 20 30 20 28 69 6e 20 77 68 69 63 68 20 63 61  s 0 (in which ca
343a9 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 73 65  se the entire se
343aa 67 6d 65 6e 74 0a 2a 2a 20 66 69 74 20 69 6e 20  gment.** fit in 
343ab 61 20 6c 65 61 66 29 2e 0a 2a 2f 0a 73 74 61 74  a leaf)..*/.stat
343ac 69 63 20 69 6e 74 20 63 6f 6c 6c 65 63 74 53 65  ic int collectSe
343ad 67 6d 65 6e 74 54 65 72 6d 73 28 66 75 6c 6c 74  gmentTerms(fullt
343ae 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
343af 69 74 65 33 5f 73 74 6d 74 20 2a 73 2c 0a 20 20  ite3_stmt *s,.  
343b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
343b1 20 20 20 20 20 20 20 20 20 20 20 20 20 66 74 73               fts
343b2 33 48 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a  3Hash *pTerms){.
343b3 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69    const sqlite_i
343b4 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b  nt64 iStartBlock
343b5 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  id = sqlite3_col
343b6 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b  umn_int64(s, 0);
343b7 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f  .  const sqlite_
343b8 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69  int64 iEndBlocki
343b9 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
343ba 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
343bb 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
343bc 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65  ootData = sqlite
343bd 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c  3_column_blob(s,
343be 20 32 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74   2);.  const int
343bf 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c   nRootData = sql
343c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
343c1 73 28 73 2c 20 32 29 3b 0a 20 20 4c 65 61 76 65  s(s, 2);.  Leave
343c2 73 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  sReader reader;.
343c3 20 20 69 6e 74 20 72 63 20 3d 20 6c 65 61 76 65    int rc = leave
343c4 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c 20 30  sReaderInit(v, 0
343c5 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  , iStartBlockid,
343c6 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20   iEndBlockid,.  
343c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
343c8 20 20 20 20 20 20 20 20 20 20 70 52 6f 6f 74 44            pRootD
343c9 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 20  ata, nRootData, 
343ca 26 72 65 61 64 65 72 29 3b 0a 20 20 69 66 28 20  &reader);.  if( 
343cb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
343cc 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68  return rc;..  wh
343cd 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
343ce 4f 4b 20 26 26 20 21 6c 65 61 76 65 73 52 65 61  OK && !leavesRea
343cf 64 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72  derAtEnd(&reader
343d0 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ) ){.    const c
343d1 68 61 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61  har *pTerm = lea
343d2 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 26 72  vesReaderTerm(&r
343d3 65 61 64 65 72 29 3b 0a 20 20 20 20 63 6f 6e 73  eader);.    cons
343d4 74 20 69 6e 74 20 6e 54 65 72 6d 20 3d 20 6c 65  t int nTerm = le
343d5 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79  avesReaderTermBy
343d6 74 65 73 28 26 72 65 61 64 65 72 29 3b 0a 20 20  tes(&reader);.  
343d7 20 20 76 6f 69 64 20 2a 6f 6c 64 56 61 6c 75 65    void *oldValue
343d8 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61   = sqlite3Fts3Ha
343d9 73 68 46 69 6e 64 28 70 54 65 72 6d 73 2c 20 70  shFind(pTerms, p
343da 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  Term, nTerm);.  
343db 20 20 76 6f 69 64 20 2a 6e 65 77 56 61 6c 75 65    void *newValue
343dc 20 3d 20 28 76 6f 69 64 20 2a 29 28 28 63 68 61   = (void *)((cha
343dd 72 20 2a 29 6f 6c 64 56 61 6c 75 65 2b 31 29 3b  r *)oldValue+1);
343de 0a 0a 20 20 20 20 2f 2a 20 46 72 6f 6d 20 74 68  ..    /* From th
343df 65 20 63 6f 6d 6d 65 6e 74 20 62 65 66 6f 72 65  e comment before
343e0 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
343e1 49 6e 73 65 72 74 20 69 6e 20 66 74 73 33 5f 68  Insert in fts3_h
343e2 61 73 68 2e 63 2c 0a 20 20 20 20 2a 2a 20 74 68  ash.c,.    ** th
343e3 65 20 64 61 74 61 20 76 61 6c 75 65 20 70 61 73  e data value pas
343e4 73 65 64 20 69 73 20 72 65 74 75 72 6e 65 64 20  sed is returned 
343e5 69 6e 20 63 61 73 65 20 6f 66 20 6d 61 6c 6c 6f  in case of mallo
343e6 63 20 66 61 69 6c 75 72 65 2e 0a 20 20 20 20 2a  c failure..    *
343e7 2f 0a 20 20 20 20 69 66 28 20 6e 65 77 56 61 6c  /.    if( newVal
343e8 75 65 3d 3d 73 71 6c 69 74 65 33 46 74 73 33 48  ue==sqlite3Fts3H
343e9 61 73 68 49 6e 73 65 72 74 28 70 54 65 72 6d 73  ashInsert(pTerms
343ea 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
343eb 6e 65 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20  newValue) ){.   
343ec 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
343ed 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  OMEM;.    }else{
343ee 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65 61 76  .      rc = leav
343ef 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c 20  esReaderStep(v, 
343f0 26 72 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a  &reader);.    }.
343f1 20 20 7d 0a 0a 20 20 6c 65 61 76 65 73 52 65 61    }..  leavesRea
343f2 64 65 72 44 65 73 74 72 6f 79 28 26 72 65 61 64  derDestroy(&read
343f3 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  er);.  return rc
343f4 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66  ;.}../* Helper f
343f5 75 6e 63 74 69 6f 6e 20 74 6f 20 62 75 69 6c 64  unction to build
343f6 20 74 68 65 20 72 65 73 75 6c 74 20 73 74 72 69   the result stri
343f7 6e 67 20 66 6f 72 20 64 75 6d 70 5f 74 65 72 6d  ng for dump_term
343f8 73 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  s(). */.static i
343f9 6e 74 20 67 65 6e 65 72 61 74 65 54 65 72 6d 73  nt generateTerms
343fa 52 65 73 75 6c 74 28 73 71 6c 69 74 65 33 5f 63  Result(sqlite3_c
343fb 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74  ontext *pContext
343fc 2c 20 66 74 73 33 48 61 73 68 20 2a 70 54 65 72  , fts3Hash *pTer
343fd 6d 73 29 7b 0a 20 20 69 6e 74 20 69 54 65 72 6d  ms){.  int iTerm
343fe 2c 20 6e 54 65 72 6d 73 2c 20 6e 52 65 73 75 6c  , nTerms, nResul
343ff 74 42 79 74 65 73 2c 20 69 42 79 74 65 3b 0a 20  tBytes, iByte;. 
34400 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20   char *result;. 
34401 20 54 65 72 6d 44 61 74 61 20 2a 70 44 61 74 61   TermData *pData
34402 3b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d  ;.  fts3HashElem
34403 20 2a 65 3b 0a 0a 20 20 2f 2a 20 49 74 65 72 61   *e;..  /* Itera
34404 74 65 20 70 54 65 72 6d 73 20 74 6f 20 67 65 6e  te pTerms to gen
34405 65 72 61 74 65 20 61 6e 20 61 72 72 61 79 20 6f  erate an array o
34406 66 20 74 65 72 6d 73 20 69 6e 20 70 44 61 74 61  f terms in pData
34407 20 66 6f 72 0a 20 20 2a 2a 20 73 6f 72 74 69 6e   for.  ** sortin
34408 67 2e 0a 20 20 2a 2f 0a 20 20 6e 54 65 72 6d 73  g..  */.  nTerms
34409 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74   = fts3HashCount
3440a 28 70 54 65 72 6d 73 29 3b 0a 20 20 61 73 73 65  (pTerms);.  asse
3440b 72 74 28 20 6e 54 65 72 6d 73 3e 30 20 29 3b 0a  rt( nTerms>0 );.
3440c 20 20 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65    pData = sqlite
3440d 33 5f 6d 61 6c 6c 6f 63 28 6e 54 65 72 6d 73 2a  3_malloc(nTerms*
3440e 73 69 7a 65 6f 66 28 54 65 72 6d 44 61 74 61 29  sizeof(TermData)
3440f 29 3b 0a 20 20 69 66 28 20 70 44 61 74 61 3d 3d  );.  if( pData==
34410 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
34411 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 6e  LITE_NOMEM;..  n
34412 52 65 73 75 6c 74 42 79 74 65 73 20 3d 20 30 3b  ResultBytes = 0;
34413 0a 20 20 66 6f 72 28 69 54 65 72 6d 20 3d 20 30  .  for(iTerm = 0
34414 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 46 69  , e = fts3HashFi
34415 72 73 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20  rst(pTerms); e; 
34416 69 54 65 72 6d 2b 2b 2c 20 65 20 3d 20 66 74 73  iTerm++, e = fts
34417 33 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20  3HashNext(e)){. 
34418 20 20 20 6e 52 65 73 75 6c 74 42 79 74 65 73 20     nResultBytes 
34419 2b 3d 20 66 74 73 33 48 61 73 68 4b 65 79 73 69  += fts3HashKeysi
3441a 7a 65 28 65 29 2b 31 3b 20 20 20 2f 2a 20 54 65  ze(e)+1;   /* Te
3441b 72 6d 20 70 6c 75 73 20 74 72 61 69 6c 69 6e 67  rm plus trailing
3441c 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 61 73   space */.    as
3441d 73 65 72 74 28 20 69 54 65 72 6d 3c 6e 54 65 72  sert( iTerm<nTer
3441e 6d 73 20 29 3b 0a 20 20 20 20 70 44 61 74 61 5b  ms );.    pData[
3441f 69 54 65 72 6d 5d 2e 70 54 65 72 6d 20 3d 20 66  iTerm].pTerm = f
34420 74 73 33 48 61 73 68 4b 65 79 28 65 29 3b 0a 20  ts3HashKey(e);. 
34421 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e     pData[iTerm].
34422 6e 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68  nTerm = fts3Hash
34423 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20  Keysize(e);.    
34424 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70 43 6f  pData[iTerm].pCo
34425 6c 6c 65 63 74 6f 72 20 3d 20 66 74 73 33 48 61  llector = fts3Ha
34426 73 68 44 61 74 61 28 65 29 3b 20 20 2f 2a 20 75  shData(e);  /* u
34427 6e 75 73 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61  nused */.  }.  a
34428 73 73 65 72 74 28 20 69 54 65 72 6d 3d 3d 6e 54  ssert( iTerm==nT
34429 65 72 6d 73 20 29 3b 0a 0a 20 20 61 73 73 65 72  erms );..  asser
3442a 74 28 20 6e 52 65 73 75 6c 74 42 79 74 65 73 3e  t( nResultBytes>
3442b 30 20 29 3b 20 20 20 2f 2a 20 6e 54 65 72 6d 73  0 );   /* nTerms
3442c 3e 30 2c 20 6e 52 65 73 75 6c 74 73 42 79 74 65  >0, nResultsByte
3442d 73 20 6d 75 73 74 20 62 65 2c 20 74 6f 6f 2e 20  s must be, too. 
3442e 2a 2f 0a 20 20 72 65 73 75 6c 74 20 3d 20 73 71  */.  result = sq
3442f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65  lite3_malloc(nRe
34430 73 75 6c 74 42 79 74 65 73 29 3b 0a 20 20 69 66  sultBytes);.  if
34431 28 20 72 65 73 75 6c 74 3d 3d 4e 55 4c 4c 20 29  ( result==NULL )
34432 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
34433 65 65 28 70 44 61 74 61 29 3b 0a 20 20 20 20 72  ee(pData);.    r
34434 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
34435 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e  EM;.  }..  if( n
34436 54 65 72 6d 73 3e 31 20 29 20 71 73 6f 72 74 28  Terms>1 ) qsort(
34437 70 44 61 74 61 2c 20 6e 54 65 72 6d 73 2c 20 73  pData, nTerms, s
34438 69 7a 65 6f 66 28 2a 70 44 61 74 61 29 2c 20 74  izeof(*pData), t
34439 65 72 6d 44 61 74 61 43 6d 70 29 3b 0a 0a 20 20  ermDataCmp);..  
3443a 2f 2a 20 52 65 61 64 20 74 68 65 20 74 65 72 6d  /* Read the term
3443b 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 75  s in order to bu
3443c 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74 2e 20  ild the result. 
3443d 2a 2f 0a 20 20 69 42 79 74 65 20 3d 20 30 3b 0a  */.  iByte = 0;.
3443e 20 20 66 6f 72 28 69 54 65 72 6d 3d 30 3b 20 69    for(iTerm=0; i
3443f 54 65 72 6d 3c 6e 54 65 72 6d 73 3b 20 2b 2b 69  Term<nTerms; ++i
34440 54 65 72 6d 29 7b 0a 20 20 20 20 6d 65 6d 63 70  Term){.    memcp
34441 79 28 72 65 73 75 6c 74 2b 69 42 79 74 65 2c 20  y(result+iByte, 
34442 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70 54 65  pData[iTerm].pTe
34443 72 6d 2c 20 70 44 61 74 61 5b 69 54 65 72 6d 5d  rm, pData[iTerm]
34444 2e 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69 42 79  .nTerm);.    iBy
34445 74 65 20 2b 3d 20 70 44 61 74 61 5b 69 54 65 72  te += pData[iTer
34446 6d 5d 2e 6e 54 65 72 6d 3b 0a 20 20 20 20 72 65  m].nTerm;.    re
34447 73 75 6c 74 5b 69 42 79 74 65 2b 2b 5d 20 3d 20  sult[iByte++] = 
34448 27 20 27 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ' ';.  }.  asser
34449 74 28 20 69 42 79 74 65 3d 3d 6e 52 65 73 75 6c  t( iByte==nResul
3444a 74 42 79 74 65 73 20 29 3b 0a 20 20 61 73 73 65  tBytes );.  asse
3444b 72 74 28 20 72 65 73 75 6c 74 5b 6e 52 65 73 75  rt( result[nResu
3444c 6c 74 42 79 74 65 73 2d 31 5d 3d 3d 27 20 27 20  ltBytes-1]==' ' 
3444d 29 3b 0a 20 20 72 65 73 75 6c 74 5b 6e 52 65 73  );.  result[nRes
3444e 75 6c 74 42 79 74 65 73 2d 31 5d 20 3d 20 27 5c  ultBytes-1] = '\
3444f 30 27 3b 0a 0a 20 20 2f 2a 20 50 61 73 73 65 73  0';..  /* Passes
34450 20 61 77 61 79 20 6f 77 6e 65 72 73 68 69 70 20   away ownership 
34451 6f 66 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20  of result. */.  
34452 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
34453 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 72 65  ext(pContext, re
34454 73 75 6c 74 2c 20 6e 52 65 73 75 6c 74 42 79 74  sult, nResultByt
34455 65 73 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72  es-1, sqlite3_fr
34456 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ee);.  sqlite3_f
34457 72 65 65 28 70 44 61 74 61 29 3b 0a 20 20 72 65  ree(pData);.  re
34458 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
34459 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 73  }../* Implements
3445a 20 64 75 6d 70 5f 74 65 72 6d 73 28 29 20 66 6f   dump_terms() fo
3445b 72 20 75 73 65 20 69 6e 20 69 6e 73 70 65 63 74  r use in inspect
3445c 69 6e 67 20 74 68 65 20 66 74 73 33 20 69 6e 64  ing the fts3 ind
3445d 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 65 73 74 73  ex from.** tests
3445e 2e 20 20 54 45 58 54 20 72 65 73 75 6c 74 20 63  .  TEXT result c
3445f 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72  ontaining the or
34460 64 65 72 65 64 20 6c 69 73 74 20 6f 66 20 74 65  dered list of te
34461 72 6d 73 20 6a 6f 69 6e 65 64 20 62 79 0a 2a 2a  rms joined by.**
34462 20 73 70 61 63 65 73 2e 20 20 64 75 6d 70 5f 74   spaces.  dump_t
34463 65 72 6d 73 28 74 2c 20 6c 65 76 65 6c 2c 20 69  erms(t, level, i
34464 64 78 29 20 64 75 6d 70 73 20 74 68 65 20 74 65  dx) dumps the te
34465 72 6d 73 20 66 6f 72 20 74 68 65 20 73 65 67 6d  rms for the segm
34466 65 6e 74 0a 2a 2a 20 73 70 65 63 69 66 69 65 64  ent.** specified
34467 20 62 79 20 6c 65 76 65 6c 2c 20 69 64 78 20 28   by level, idx (
34468 69 6e 20 25 5f 73 65 67 64 69 72 29 2c 20 77 68  in %_segdir), wh
34469 69 6c 65 20 64 75 6d 70 5f 74 65 72 6d 73 28 74  ile dump_terms(t
3446a 29 20 64 75 6d 70 73 0a 2a 2a 20 61 6c 6c 20 74  ) dumps.** all t
3446b 65 72 6d 73 20 69 6e 20 74 68 65 20 69 6e 64 65  erms in the inde
3446c 78 2e 20 20 49 6e 20 62 6f 74 68 20 63 61 73 65  x.  In both case
3446d 73 20 74 20 69 73 20 74 68 65 20 66 74 73 20 74  s t is the fts t
3446e 61 62 6c 65 27 73 20 6d 61 67 69 63 0a 2a 2a 20  able's magic.** 
3446f 74 61 62 6c 65 2d 6e 61 6d 65 64 20 63 6f 6c 75  table-named colu
34470 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  mn..*/.static vo
34471 69 64 20 64 75 6d 70 54 65 72 6d 73 46 75 6e 63  id dumpTermsFunc
34472 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
34473 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20  ext *pContext,. 
34474 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
34475 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
34476 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
34477 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20  rsor *pCursor;. 
34478 20 69 66 28 20 61 72 67 63 21 3d 33 20 26 26 20   if( argc!=3 && 
34479 61 72 67 63 21 3d 31 20 29 7b 0a 20 20 20 20 67  argc!=1 ){.    g
3447a 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
3447b 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72  ntext, "dump_ter
3447c 6d 73 22 2c 20 22 69 6e 63 6f 72 72 65 63 74 20  ms", "incorrect 
3447d 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20 20 7d  arguments");.  }
3447e 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
3447f 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
34480 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
34481 42 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20  B ||.           
34482 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
34483 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73  ytes(argv[0])!=s
34484 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29  izeof(pCursor) )
34485 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 45 72  {.    generateEr
34486 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64  ror(pContext, "d
34487 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 69 6c 6c  ump_terms", "ill
34488 65 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d  egal first argum
34489 65 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ent");.  }else{.
3448a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61      fulltext_vta
3448b 62 20 2a 76 3b 0a 20 20 20 20 66 74 73 33 48 61  b *v;.    fts3Ha
3448c 73 68 20 74 65 72 6d 73 3b 0a 20 20 20 20 73 71  sh terms;.    sq
3448d 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 20 3d 20  lite3_stmt *s = 
3448e 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 63  NULL;.    int rc
3448f 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70  ;..    memcpy(&p
34490 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f  Cursor, sqlite3_
34491 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
34492 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72  0]), sizeof(pCur
34493 73 6f 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63  sor));.    v = c
34494 75 72 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73  ursor_vtab(pCurs
34495 6f 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  or);..    /* If 
34496 70 61 73 73 65 64 20 6f 6e 6c 79 20 74 68 65 20  passed only the 
34497 63 75 72 73 6f 72 20 63 6f 6c 75 6d 6e 2c 20 67  cursor column, g
34498 65 74 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 2e  et all segments.
34499 20 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20    Otherwise.    
3449a 2a 2a 20 67 65 74 20 74 68 65 20 73 65 67 6d 65  ** get the segme
3449b 6e 74 20 64 65 73 63 72 69 62 65 64 20 62 79 20  nt described by 
3449c 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
3449d 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 20 20  o arguments..   
3449e 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63   */.    if( argc
3449f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==1 ){.      rc 
344a0 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
344a1 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45  ent(v, SEGDIR_SE
344a2 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26  LECT_ALL_STMT, &
344a3 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  s);.    }else{. 
344a4 20 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 67 65       rc = sql_ge
344a5 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53  t_statement(v, S
344a6 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47  EGDIR_SELECT_SEG
344a7 4d 45 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  MENT_STMT, &s);.
344a8 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
344a9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
344aa 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
344ab 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 73  bind_int(s, 1, s
344ac 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
344ad 28 61 72 67 76 5b 31 5d 29 29 3b 0a 20 20 20 20  (argv[1]));.    
344ae 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
344af 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
344b0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
344b1 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c 20 73  bind_int(s, 2, s
344b2 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
344b3 28 61 72 67 76 5b 32 5d 29 29 3b 0a 20 20 20 20  (argv[2]));.    
344b4 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
344b5 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21    }..    if( rc!
344b6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
344b7 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
344b8 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
344b9 70 5f 74 65 72 6d 73 22 2c 20 4e 55 4c 4c 29 3b  p_terms", NULL);
344ba 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
344bb 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6c     }..    /* Col
344bc 6c 65 63 74 20 74 68 65 20 74 65 72 6d 73 20 66  lect the terms f
344bd 6f 72 20 65 61 63 68 20 73 65 67 6d 65 6e 74 2e  or each segment.
344be 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 46   */.    sqlite3F
344bf 74 73 33 48 61 73 68 49 6e 69 74 28 26 74 65 72  ts3HashInit(&ter
344c0 6d 73 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54  ms, FTS3_HASH_ST
344c1 52 49 4e 47 2c 20 31 29 3b 0a 20 20 20 20 77 68  RING, 1);.    wh
344c2 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74  ile( (rc = sqlit
344c3 65 33 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c  e3_step(s))==SQL
344c4 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
344c5 20 72 63 20 3d 20 63 6f 6c 6c 65 63 74 53 65 67   rc = collectSeg
344c6 6d 65 6e 74 54 65 72 6d 73 28 76 2c 20 73 2c 20  mentTerms(v, s, 
344c7 26 74 65 72 6d 73 29 3b 0a 20 20 20 20 20 20 69  &terms);.      i
344c8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
344c9 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
344ca 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
344cb 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
344cc 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
344cd 73 29 3b 0a 20 20 20 20 20 20 67 65 6e 65 72 61  s);.      genera
344ce 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
344cf 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20  , "dump_terms", 
344d0 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 65 6c 73 65  NULL);.    }else
344d1 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e  {.      const in
344d2 74 20 6e 54 65 72 6d 73 20 3d 20 66 74 73 33 48  t nTerms = fts3H
344d3 61 73 68 43 6f 75 6e 74 28 26 74 65 72 6d 73 29  ashCount(&terms)
344d4 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 54 65 72  ;.      if( nTer
344d5 6d 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ms>0 ){.        
344d6 72 63 20 3d 20 67 65 6e 65 72 61 74 65 54 65 72  rc = generateTer
344d7 6d 73 52 65 73 75 6c 74 28 70 43 6f 6e 74 65 78  msResult(pContex
344d8 74 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20 20 20  t, &terms);.    
344d9 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
344da 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
344db 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45 72        generateEr
344dc 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64  ror(pContext, "d
344dd 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 6f 75 74  ump_terms", "out
344de 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20   of memory");.  
344df 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
344e0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
344e1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
344e2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
344e3 7d 65 6c 73 65 20 69 66 28 20 61 72 67 63 3d 3d  }else if( argc==
344e4 33 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  3 ){.        /* 
344e5 54 68 65 20 73 70 65 63 69 66 69 63 20 73 65 67  The specific seg
344e6 6d 65 6e 74 20 61 73 6b 65 64 20 66 6f 72 20 63  ment asked for c
344e7 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e  ould not be foun
344e8 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 67 65  d. */.        ge
344e9 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
344ea 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d  text, "dump_term
344eb 73 22 2c 20 22 73 65 67 6d 65 6e 74 20 6e 6f 74  s", "segment not
344ec 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20 20 20 20   found");.      
344ed 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
344ee 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73 20 66 6f  * No segments fo
344ef 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  und. */.        
344f0 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
344f1 49 74 20 73 68 6f 75 6c 64 20 62 65 20 69 6d 70  It should be imp
344f2 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68  ossible to reach
344f3 20 74 68 69 73 2e 20 20 54 68 69 73 0a 20 20 20   this.  This.   
344f4 20 20 20 20 20 2a 2a 20 63 61 73 65 20 63 61 6e       ** case can
344f5 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 66 6f 72   only happen for
344f6 20 61 6e 20 65 6d 70 74 79 20 74 61 62 6c 65 2c   an empty table,
344f7 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 0a 20   in which case. 
344f8 20 20 20 20 20 20 20 2a 2a 20 53 51 4c 69 74 65         ** SQLite
344f9 20 68 61 73 20 6e 6f 20 72 6f 77 73 20 74 6f 20   has no rows to 
344fa 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69  call this functi
344fb 6f 6e 20 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a  on on..        *
344fc 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
344fd 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 70 43  3_result_null(pC
344fe 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  ontext);.      }
344ff 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
34500 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28  e3Fts3HashClear(
34501 26 74 65 72 6d 73 29 3b 0a 20 20 7d 0a 7d 0a 0a  &terms);.  }.}..
34502 2f 2a 20 45 78 70 61 6e 64 20 74 68 65 20 44 4c  /* Expand the DL
34503 5f 44 45 46 41 55 4c 54 20 64 6f 63 6c 69 73 74  _DEFAULT doclist
34504 20 69 6e 20 70 44 61 74 61 20 69 6e 74 6f 20 61   in pData into a
34505 20 74 65 78 74 20 72 65 73 75 6c 74 20 69 6e 0a   text result in.
34506 2a 2a 20 70 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a  ** pContext..*/.
34507 73 74 61 74 69 63 20 76 6f 69 64 20 63 72 65 61  static void crea
34508 74 65 44 6f 63 6c 69 73 74 52 65 73 75 6c 74 28  teDoclistResult(
34509 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3450a 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20  *pContext,.     
3450b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3450c 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
3450d 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
3450e 74 20 6e 44 61 74 61 29 7b 0a 20 20 44 61 74 61  t nData){.  Data
3450f 42 75 66 66 65 72 20 64 75 6d 70 3b 0a 20 20 44  Buffer dump;.  D
34510 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64 65 72  LReader dlReader
34511 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  ;..  assert( pDa
34512 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e 44 61 74  ta!=NULL && nDat
34513 61 3e 30 20 29 3b 0a 0a 20 20 64 61 74 61 42 75  a>0 );..  dataBu
34514 66 66 65 72 49 6e 69 74 28 26 64 75 6d 70 2c 20  fferInit(&dump, 
34515 30 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 64  0);.  dlrInit(&d
34516 6c 52 65 61 64 65 72 2c 20 44 4c 5f 44 45 46 41  lReader, DL_DEFA
34517 55 4c 54 2c 20 70 44 61 74 61 2c 20 6e 44 61 74  ULT, pData, nDat
34518 61 29 3b 0a 20 20 66 6f 72 28 20 3b 20 21 64 6c  a);.  for( ; !dl
34519 72 41 74 45 6e 64 28 26 64 6c 52 65 61 64 65 72  rAtEnd(&dlReader
3451a 29 3b 20 64 6c 72 53 74 65 70 28 26 64 6c 52 65  ); dlrStep(&dlRe
3451b 61 64 65 72 29 20 29 7b 0a 20 20 20 20 63 68 61  ader) ){.    cha
3451c 72 20 62 75 66 5b 32 35 36 5d 3b 0a 20 20 20 20  r buf[256];.    
3451d 50 4c 52 65 61 64 65 72 20 70 6c 52 65 61 64 65  PLReader plReade
3451e 72 3b 0a 0a 20 20 20 20 70 6c 72 49 6e 69 74 28  r;..    plrInit(
3451f 26 70 6c 52 65 61 64 65 72 2c 20 26 64 6c 52 65  &plReader, &dlRe
34520 61 64 65 72 29 3b 0a 20 20 20 20 69 66 28 20 44  ader);.    if( D
34521 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 44 4f  L_DEFAULT==DL_DO
34522 43 49 44 53 20 7c 7c 20 70 6c 72 41 74 45 6e 64  CIDS || plrAtEnd
34523 28 26 70 6c 52 65 61 64 65 72 29 20 29 7b 0a 20  (&plReader) ){. 
34524 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
34525 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
34526 29 2c 20 62 75 66 2c 20 22 5b 25 6c 6c 64 5d 20  ), buf, "[%lld] 
34527 22 2c 20 64 6c 72 44 6f 63 69 64 28 26 64 6c 52  ", dlrDocid(&dlR
34528 65 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20 64  eader));.      d
34529 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
3452a 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72 6c  &dump, buf, strl
3452b 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 7d 65  en(buf));.    }e
3452c 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  lse{.      int i
3452d 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43 6f 6c 75  Column = plrColu
3452e 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 0a  mn(&plReader);..
3452f 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
34530 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75  printf(sizeof(bu
34531 66 29 2c 20 62 75 66 2c 20 22 5b 25 6c 6c 64 20  f), buf, "[%lld 
34532 25 64 5b 22 2c 0a 20 20 20 20 20 20 20 20 20 20  %d[",.          
34533 20 20 20 20 20 20 20 20 20 20 20 20 20 64 6c 72               dlr
34534 44 6f 63 69 64 28 26 64 6c 52 65 61 64 65 72 29  Docid(&dlReader)
34535 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , iColumn);.    
34536 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
34537 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73  nd(&dump, buf, s
34538 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 0a 20 20  trlen(buf));..  
34539 20 20 20 20 66 6f 72 28 20 3b 20 21 70 6c 72 41      for( ; !plrA
3453a 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29 3b  tEnd(&plReader);
3453b 20 70 6c 72 53 74 65 70 28 26 70 6c 52 65 61 64   plrStep(&plRead
3453c 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  er) ){.        i
3453d 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c  f( plrColumn(&pl
3453e 52 65 61 64 65 72 29 21 3d 69 43 6f 6c 75 6d 6e  Reader)!=iColumn
3453f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 43   ){.          iC
34540 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43 6f 6c 75 6d  olumn = plrColum
34541 6e 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 20 20  n(&plReader);.  
34542 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
34543 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
34544 62 75 66 29 2c 20 62 75 66 2c 20 22 5d 20 25 64  buf), buf, "] %d
34545 5b 22 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20  [", iColumn);.  
34546 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
34547 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a  dump.nData>0 );.
34548 20 20 20 20 20 20 20 20 20 20 64 75 6d 70 2e 6e            dump.n
34549 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20 20 20  Data--;         
3454a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
3454b 76 65 72 77 72 69 74 65 20 74 72 61 69 6c 69 6e  verwrite trailin
3454c 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20  g space. */.    
3454d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 75        assert( du
3454e 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70 2e 6e 44  mp.pData[dump.nD
3454f 61 74 61 5d 3d 3d 27 20 27 29 3b 0a 20 20 20 20  ata]==' ');.    
34550 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
34551 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75  Append(&dump, bu
34552 66 2c 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b  f, strlen(buf));
34553 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
34554 20 20 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c     if( DL_DEFAUL
34555 54 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f  T==DL_POSITIONS_
34556 4f 46 46 53 45 54 53 20 29 7b 0a 20 20 20 20 20  OFFSETS ){.     
34557 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
34558 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
34559 29 2c 20 62 75 66 2c 20 22 25 64 2c 25 64 2c 25  ), buf, "%d,%d,%
3455a 64 20 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  d ",.           
3455b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3455c 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52  plrPosition(&plR
3455d 65 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20  eader),.        
3455e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3455f 20 20 20 70 6c 72 53 74 61 72 74 4f 66 66 73 65     plrStartOffse
34560 74 28 26 70 6c 52 65 61 64 65 72 29 2c 20 70 6c  t(&plReader), pl
34561 72 45 6e 64 4f 66 66 73 65 74 28 26 70 6c 52 65  rEndOffset(&plRe
34562 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20  ader));.        
34563 7d 65 6c 73 65 20 69 66 28 20 44 4c 5f 44 45 46  }else if( DL_DEF
34564 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  AULT==DL_POSITIO
34565 4e 53 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  NS ){.          
34566 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
34567 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75  (sizeof(buf), bu
34568 66 2c 20 22 25 64 20 22 2c 20 70 6c 72 50 6f 73  f, "%d ", plrPos
34569 69 74 69 6f 6e 28 26 70 6c 52 65 61 64 65 72 29  ition(&plReader)
3456a 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
3456b 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
3456c 72 74 28 20 4e 55 4c 4c 3d 3d 22 55 6e 68 61 6e  rt( NULL=="Unhan
3456d 64 6c 65 64 20 44 4c 5f 44 45 46 41 55 4c 54 20  dled DL_DEFAULT 
3456e 76 61 6c 75 65 22 29 3b 0a 20 20 20 20 20 20 20  value");.       
3456f 20 7d 0a 20 20 20 20 20 20 20 20 64 61 74 61 42   }.        dataB
34570 75 66 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d  ufferAppend(&dum
34571 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62  p, buf, strlen(b
34572 75 66 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  uf));.      }.  
34573 20 20 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26      plrDestroy(&
34574 70 6c 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20  plReader);..    
34575 20 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e    assert( dump.n
34576 44 61 74 61 3e 30 20 29 3b 0a 20 20 20 20 20 20  Data>0 );.      
34577 64 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20  dump.nData--;   
34578 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34579 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74    /* Overwrite t
3457a 72 61 69 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a  railing space. *
3457b 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
3457c 64 75 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70 2e  dump.pData[dump.
3457d 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b 0a 20 20  nData]==' ');.  
3457e 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
3457f 70 65 6e 64 28 26 64 75 6d 70 2c 20 22 5d 5d 20  pend(&dump, "]] 
34580 22 2c 20 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ", 3);.    }.  }
34581 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64  .  dlrDestroy(&d
34582 6c 52 65 61 64 65 72 29 3b 0a 0a 20 20 61 73 73  lReader);..  ass
34583 65 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e  ert( dump.nData>
34584 30 20 29 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74  0 );.  dump.nDat
34585 61 2d 2d 3b 20 20 20 20 20 20 20 20 20 20 20 20  a--;            
34586 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72           /* Over
34587 77 72 69 74 65 20 74 72 61 69 6c 69 6e 67 20 73  write trailing s
34588 70 61 63 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72  pace. */.  asser
34589 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75  t( dump.pData[du
3458a 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b  mp.nData]==' ');
3458b 0a 20 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75  .  dump.pData[du
3458c 6d 70 2e 6e 44 61 74 61 5d 20 3d 20 27 5c 30 27  mp.nData] = '\0'
3458d 3b 0a 20 20 61 73 73 65 72 74 28 20 64 75 6d 70  ;.  assert( dump
3458e 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 0a 20 20 2f  .nData>0 );..  /
3458f 2a 20 50 61 73 73 65 73 20 6f 77 6e 65 72 73 68  * Passes ownersh
34590 69 70 20 6f 66 20 64 75 6d 70 27 73 20 62 75 66  ip of dump's buf
34591 66 65 72 20 74 6f 20 70 43 6f 6e 74 65 78 74 2e  fer to pContext.
34592 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65   */.  sqlite3_re
34593 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65  sult_text(pConte
34594 78 74 2c 20 64 75 6d 70 2e 70 44 61 74 61 2c 20  xt, dump.pData, 
34595 64 75 6d 70 2e 6e 44 61 74 61 2c 20 73 71 6c 69  dump.nData, sqli
34596 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 64 75 6d  te3_free);.  dum
34597 70 2e 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a  p.pData = NULL;.
34598 20 20 64 75 6d 70 2e 6e 44 61 74 61 20 3d 20 64    dump.nData = d
34599 75 6d 70 2e 6e 43 61 70 61 63 69 74 79 20 3d 20  ump.nCapacity = 
3459a 30 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65  0;.}../* Impleme
3459b 6e 74 73 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74  nts dump_doclist
3459c 28 29 20 66 6f 72 20 75 73 65 20 69 6e 20 69 6e  () for use in in
3459d 73 70 65 63 74 69 6e 67 20 74 68 65 20 66 74 73  specting the fts
3459e 33 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20  3 index from.** 
3459f 74 65 73 74 73 2e 20 20 54 45 58 54 20 72 65 73  tests.  TEXT res
345a0 75 6c 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  ult containing a
345a1 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
345a2 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  tation of the.**
345a3 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 68 65   doclist for the
345a4 20 69 6e 64 69 63 61 74 65 64 20 74 65 72 6d 2e   indicated term.
345a5 20 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74 28 74    dump_doclist(t
345a6 2c 20 74 65 72 6d 2c 20 6c 65 76 65 6c 2c 20 69  , term, level, i
345a7 64 78 29 0a 2a 2a 20 64 75 6d 70 73 20 74 68 65  dx).** dumps the
345a8 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 65 72   doclist for ter
345a9 6d 20 66 72 6f 6d 20 74 68 65 20 73 65 67 6d 65  m from the segme
345aa 6e 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20  nt specified by 
345ab 6c 65 76 65 6c 2c 20 69 64 78 0a 2a 2a 20 28 69  level, idx.** (i
345ac 6e 20 25 5f 73 65 67 64 69 72 29 2c 20 77 68 69  n %_segdir), whi
345ad 6c 65 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74 28  le dump_doclist(
345ae 74 2c 20 74 65 72 6d 29 20 64 75 6d 70 73 20 74  t, term) dumps t
345af 68 65 20 6c 6f 67 69 63 61 6c 0a 2a 2a 20 64 6f  he logical.** do
345b0 63 6c 69 73 74 20 66 6f 72 20 74 68 65 20 74 65  clist for the te
345b1 72 6d 20 61 63 72 6f 73 73 20 61 6c 6c 20 73 65  rm across all se
345b2 67 6d 65 6e 74 73 2e 20 20 54 68 65 20 70 65 72  gments.  The per
345b3 2d 73 65 67 6d 65 6e 74 20 64 6f 63 6c 69 73 74  -segment doclist
345b4 0a 2a 2a 20 63 61 6e 20 63 6f 6e 74 61 69 6e 20  .** can contain 
345b5 64 65 6c 65 74 69 6f 6e 73 2c 20 77 68 69 6c 65  deletions, while
345b6 20 74 68 65 20 66 75 6c 6c 2d 69 6e 64 65 78 20   the full-index 
345b7 64 6f 63 6c 69 73 74 20 77 69 6c 6c 20 6e 6f 74  doclist will not
345b8 0a 2a 2a 20 28 64 65 6c 65 74 69 6f 6e 73 20 61  .** (deletions a
345b9 72 65 20 6f 6d 69 74 74 65 64 29 2e 0a 2a 2a 0a  re omitted)..**.
345ba 2a 2a 20 52 65 73 75 6c 74 20 66 6f 72 6d 61 74  ** Result format
345bb 73 20 64 69 66 66 65 72 20 77 69 74 68 20 74 68  s differ with th
345bc 65 20 73 65 74 74 69 6e 67 20 6f 66 20 44 4c 5f  e setting of DL_
345bd 44 45 46 41 55 4c 54 53 2e 20 20 45 78 61 6d 70  DEFAULTS.  Examp
345be 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 44 4c 5f 44 4f  les:.**.** DL_DO
345bf 43 49 44 53 3a 20 5b 31 5d 20 5b 33 5d 20 5b 37  CIDS: [1] [3] [7
345c0 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  ].** DL_POSITION
345c1 53 3a 20 5b 31 20 30 5b 30 20 34 5d 20 31 5b 31  S: [1 0[0 4] 1[1
345c2 37 5d 5d 20 5b 33 20 31 5b 35 5d 5d 0a 2a 2a 20  7]] [3 1[5]].** 
345c3 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46  DL_POSITIONS_OFF
345c4 53 45 54 53 3a 20 5b 31 20 30 5b 30 2c 30 2c 33  SETS: [1 0[0,0,3
345c5 20 34 2c 32 33 2c 32 36 5d 20 31 5b 31 37 2c 31   4,23,26] 1[17,1
345c6 30 32 2c 31 30 35 5d 5d 20 5b 33 20 31 5b 35 2c  02,105]] [3 1[5,
345c7 32 30 2c 32 33 5d 5d 0a 2a 2a 0a 2a 2a 20 49 6e  20,23]].**.** In
345c8 20 65 61 63 68 20 63 61 73 65 20 74 68 65 20 6e   each case the n
345c9 75 6d 62 65 72 20 61 66 74 65 72 20 74 68 65 20  umber after the 
345ca 6f 75 74 65 72 20 27 5b 27 20 69 73 20 74 68 65  outer '[' is the
345cb 20 64 6f 63 69 64 2e 20 20 49 6e 20 74 68 65 0a   docid.  In the.
345cc 2a 2a 20 6c 61 74 74 65 72 20 74 77 6f 20 63 61  ** latter two ca
345cd 73 65 73 2c 20 74 68 65 20 6e 75 6d 62 65 72 20  ses, the number 
345ce 62 65 66 6f 72 65 20 74 68 65 20 69 6e 6e 65 72  before the inner
345cf 20 27 5b 27 20 69 73 20 74 68 65 20 63 6f 6c 75   '[' is the colu
345d0 6d 6e 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  mn.** associated
345d1 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 73   with the values
345d2 20 77 69 74 68 69 6e 2e 20 20 46 6f 72 20 44 4c   within.  For DL
345d3 5f 50 4f 53 49 54 49 4f 4e 53 20 74 68 65 20 6e  _POSITIONS the n
345d4 75 6d 62 65 72 73 0a 2a 2a 20 77 69 74 68 69 6e  umbers.** within
345d5 20 61 72 65 20 74 68 65 20 70 6f 73 69 74 69 6f   are the positio
345d6 6e 73 2c 20 66 6f 72 20 44 4c 5f 50 4f 53 49 54  ns, for DL_POSIT
345d7 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 74 68 65  IONS_OFFSETS the
345d8 79 20 61 72 65 20 74 68 65 0a 2a 2a 20 70 6f 73  y are the.** pos
345d9 69 74 69 6f 6e 2c 20 74 68 65 20 73 74 61 72 74  ition, the start
345da 20 6f 66 66 73 65 74 2c 20 61 6e 64 20 74 68 65   offset, and the
345db 20 65 6e 64 20 6f 66 66 73 65 74 2e 0a 2a 2f 0a   end offset..*/.
345dc 73 74 61 74 69 63 20 76 6f 69 64 20 64 75 6d 70  static void dump
345dd 44 6f 63 6c 69 73 74 46 75 6e 63 28 0a 20 20 73  DoclistFunc(.  s
345de 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
345df 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  pContext,.  int 
345e0 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
345e1 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
345e2 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
345e3 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20  *pCursor;.  if( 
345e4 61 72 67 63 21 3d 32 20 26 26 20 61 72 67 63 21  argc!=2 && argc!
345e5 3d 34 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61  =4 ){.    genera
345e6 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
345e7 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22  , "dump_doclist"
345e8 2c 20 22 69 6e 63 6f 72 72 65 63 74 20 61 72 67  , "incorrect arg
345e9 75 6d 65 6e 74 73 22 29 3b 0a 20 20 7d 65 6c 73  uments");.  }els
345ea 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61  e if( sqlite3_va
345eb 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
345ec 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c  )!=SQLITE_BLOB |
345ed 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  |.            sq
345ee 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
345ef 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65  s(argv[0])!=size
345f0 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20  of(pCursor) ){. 
345f1 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
345f2 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
345f3 5f 64 6f 63 6c 69 73 74 22 2c 20 22 69 6c 6c 65  _doclist", "ille
345f4 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65  gal first argume
345f5 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  nt");.  }else if
345f6 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
345f7 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3d 3d 4e  text(argv[1])==N
345f8 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  ULL ||.         
345f9 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
345fa 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 5b 30  _text(argv[1])[0
345fb 5d 3d 3d 27 5c 30 27 20 29 7b 0a 20 20 20 20 67  ]=='\0' ){.    g
345fc 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
345fd 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63  ntext, "dump_doc
345fe 6c 69 73 74 22 2c 20 22 65 6d 70 74 79 20 73 65  list", "empty se
345ff 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 22 29 3b  cond argument");
34600 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f  .  }else{.    co
34601 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20  nst char *pTerm 
34602 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
34603 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
34604 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  xt(argv[1]);.   
34605 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d   const int nTerm
34606 20 3d 20 73 74 72 6c 65 6e 28 70 54 65 72 6d 29   = strlen(pTerm)
34607 3b 0a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76  ;.    fulltext_v
34608 74 61 62 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20  tab *v;.    int 
34609 72 63 3b 0a 20 20 20 20 44 61 74 61 42 75 66 66  rc;.    DataBuff
3460a 65 72 20 64 6f 63 6c 69 73 74 3b 0a 0a 20 20 20  er doclist;..   
3460b 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72   memcpy(&pCursor
3460c 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
3460d 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73  blob(argv[0]), s
3460e 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b  izeof(pCursor));
3460f 0a 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72 5f  .    v = cursor_
34610 76 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a 0a  vtab(pCursor);..
34611 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
34612 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 30 29 3b  it(&doclist, 0);
34613 0a 0a 20 20 20 20 2f 2a 20 74 65 72 6d 53 65 6c  ..    /* termSel
34614 65 63 74 28 29 20 79 69 65 6c 64 73 20 74 68 65  ect() yields the
34615 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20 64 6f   same logical do
34616 63 6c 69 73 74 20 74 68 61 74 20 71 75 65 72 69  clist that queri
34617 65 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 72 75  es are.    ** ru
34618 6e 20 61 67 61 69 6e 73 74 2e 0a 20 20 20 20 2a  n against..    *
34619 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63 3d 3d  /.    if( argc==
3461a 32 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  2 ){.      rc = 
3461b 74 65 72 6d 53 65 6c 65 63 74 28 76 2c 20 76 2d  termSelect(v, v-
3461c 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 54 65 72 6d 2c  >nColumn, pTerm,
3461d 20 6e 54 65 72 6d 2c 20 30 2c 20 44 4c 5f 44 45   nTerm, 0, DL_DE
3461e 46 41 55 4c 54 2c 20 26 64 6f 63 6c 69 73 74 29  FAULT, &doclist)
3461f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
34620 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
34621 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 20 20  *s = NULL;..    
34622 20 20 2f 2a 20 47 65 74 20 6f 75 72 20 73 70 65    /* Get our spe
34623 63 69 66 69 63 20 73 65 67 6d 65 6e 74 27 73 20  cific segment's 
34624 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 2a 2f 0a  information. */.
34625 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 67        rc = sql_g
34626 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
34627 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45  SEGDIR_SELECT_SE
34628 47 4d 45 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b  GMENT_STMT, &s);
34629 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
3462a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3462b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3462c 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20  _bind_int(s, 1, 
3462d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
3462e 74 28 61 72 67 76 5b 32 5d 29 29 3b 0a 20 20 20  t(argv[2]));.   
3462f 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
34630 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
34631 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
34632 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c 20  _bind_int(s, 2, 
34633 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
34634 74 28 61 72 67 76 5b 33 5d 29 29 3b 0a 20 20 20  t(argv[3]));.   
34635 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
34636 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
34637 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
34638 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
34639 73 74 65 70 28 73 29 3b 0a 0a 20 20 20 20 20 20  step(s);..      
3463a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3463b 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20  _DONE ){.       
3463c 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
3463d 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a  troy(&doclist);.
3463e 20 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61            genera
3463f 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
34640 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22  , "dump_doclist"
34641 2c 20 22 73 65 67 6d 65 6e 74 20 6e 6f 74 20 66  , "segment not f
34642 6f 75 6e 64 22 29 3b 0a 20 20 20 20 20 20 20 20  ound");.        
34643 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
34644 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20    }..        /* 
34645 46 6f 75 6e 64 20 61 20 73 65 67 6d 65 6e 74 2c  Found a segment,
34646 20 6c 6f 61 64 20 69 74 20 69 6e 74 6f 20 64 6f   load it into do
34647 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20  clist. */.      
34648 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
34649 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20  _ROW ){.        
3464a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69    const sqlite_i
3464b 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 20  nt64 iLeavesEnd 
3464c 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
3464d 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20  _int64(s, 1);.  
3464e 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
3464f 61 72 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69  ar *pData = sqli
34650 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
34651 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20  s, 2);.         
34652 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 61 74 61   const int nData
34653 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
34654 6e 5f 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 0a  n_bytes(s, 2);..
34655 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 6f 61            /* loa
34656 64 53 65 67 6d 65 6e 74 28 29 20 69 73 20 75 73  dSegment() is us
34657 65 64 20 62 79 20 74 65 72 6d 53 65 6c 65 63 74  ed by termSelect
34658 28 29 20 74 6f 20 6c 6f 61 64 20 65 61 63 68 0a  () to load each.
34659 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 65 67            ** seg
3465a 6d 65 6e 74 27 73 20 64 61 74 61 2e 0a 20 20 20  ment's data..   
3465b 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
3465c 20 20 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67      rc = loadSeg
3465d 6d 65 6e 74 28 76 2c 20 70 44 61 74 61 2c 20 6e  ment(v, pData, n
3465e 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e 64  Data, iLeavesEnd
3465f 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
34660 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
34661 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64                &d
34662 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  oclist);.       
34663 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
34664 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
34665 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
34666 5f 73 74 65 70 28 73 29 3b 0a 0a 20 20 20 20 20  _step(s);..     
34667 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64         /* Should
34668 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74   not have more t
34669 68 61 6e 20 6f 6e 65 20 6d 61 74 63 68 69 6e 67  han one matching
3466a 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20   segment. */.   
3466b 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
3466c 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
3466d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
3466e 6c 69 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a  lite3_reset(s);.
3466f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
34670 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
34671 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20  &doclist);.     
34672 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74           generat
34673 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
34674 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c   "dump_doclist",
34675 20 22 69 6e 76 61 6c 69 64 20 73 65 67 64 69 72   "invalid segdir
34676 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ");.            
34677 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
34678 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
34679 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3467a 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  OK;.          }.
3467b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3467c 7d 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  }..      sqlite3
3467d 5f 72 65 73 65 74 28 73 29 3b 0a 20 20 20 20 7d  _reset(s);.    }
3467e 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
3467f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
34680 20 69 66 28 20 64 6f 63 6c 69 73 74 2e 6e 44 61   if( doclist.nDa
34681 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ta>0 ){.        
34682 63 72 65 61 74 65 44 6f 63 6c 69 73 74 52 65 73  createDoclistRes
34683 75 6c 74 28 70 43 6f 6e 74 65 78 74 2c 20 64 6f  ult(pContext, do
34684 63 6c 69 73 74 2e 70 44 61 74 61 2c 20 64 6f 63  clist.pData, doc
34685 6c 69 73 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20  list.nData);.   
34686 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
34687 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
34688 3a 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65  : This can happe
34689 6e 20 69 66 20 74 68 65 20 74 65 72 6d 20 69 73  n if the term is
3468a 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20 6f 72   not present, or
3468b 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 61  .        ** if a
3468c 6c 6c 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20  ll instances of 
3468d 74 68 65 20 74 65 72 6d 20 68 61 76 65 20 62 65  the term have be
3468e 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74  en deleted and t
3468f 68 69 73 20 69 73 0a 20 20 20 20 20 20 20 20 2a  his is.        *
34690 2a 20 61 6e 20 61 6c 6c 2d 69 6e 64 65 78 20 64  * an all-index d
34691 75 6d 70 2e 20 20 49 74 20 6d 61 79 20 62 65 20  ump.  It may be 
34692 69 6e 74 65 72 65 73 74 69 6e 67 20 74 6f 20 64  interesting to d
34693 69 73 74 69 6e 67 75 69 73 68 0a 20 20 20 20 20  istinguish.     
34694 20 20 20 2a 2a 20 74 68 65 73 65 20 63 61 73 65     ** these case
34695 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
34696 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
34697 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65  sult_text(pConte
34698 78 74 2c 20 22 22 2c 20 30 2c 20 53 51 4c 49 54  xt, "", 0, SQLIT
34699 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
3469a 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
3469b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
3469c 4d 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 48 61  M ){.      /* Ha
3469d 6e 64 6c 65 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f  ndle out-of-memo
3469e 72 79 20 63 61 73 65 73 20 73 70 65 63 69 61 6c  ry cases special
3469f 6c 79 20 62 65 63 61 75 73 65 20 69 66 20 74 68  ly because if th
346a0 65 79 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20  ey are.      ** 
346a1 67 65 6e 65 72 61 74 65 64 20 69 6e 20 66 74 73  generated in fts
346a2 33 20 63 6f 64 65 20 74 68 65 79 20 6d 61 79 20  3 code they may 
346a3 6e 6f 74 20 62 65 20 72 65 66 6c 65 63 74 65 64  not be reflected
346a4 20 69 6e 20 74 68 65 20 64 62 0a 20 20 20 20 20   in the db.     
346a5 20 2a 2a 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20   ** handle..    
346a6 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 54 4f    */.      /* TO
346a7 44 4f 28 73 68 65 73 73 29 3a 20 48 61 6e 64 6c  DO(shess): Handl
346a8 65 20 74 68 69 73 20 6d 6f 72 65 20 63 6f 6d 70  e this more comp
346a9 72 65 68 65 6e 73 69 76 65 6c 79 2e 0a 20 20 20  rehensively..   
346aa 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 45 72 72     ** sqlite3Err
346ab 53 74 72 28 29 20 68 61 73 20 77 68 61 74 20 49  Str() has what I
346ac 20 6e 65 65 64 2c 20 62 75 74 20 69 73 20 69 6e   need, but is in
346ad 74 65 72 6e 61 6c 2e 0a 20 20 20 20 20 20 2a 2f  ternal..      */
346ae 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45  .      generateE
346af 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
346b0 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22  dump_doclist", "
346b1 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b  out of memory");
346b2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
346b3 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28    generateError(
346b4 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f  pContext, "dump_
346b5 64 6f 63 6c 69 73 74 22 2c 20 4e 55 4c 4c 29 3b  doclist", NULL);
346b6 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 61 74 61  .    }..    data
346b7 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 64  BufferDestroy(&d
346b8 6f 63 6c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 23  oclist);.  }.}.#
346b9 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
346ba 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d  s routine implem
346bb 65 6e 74 73 20 74 68 65 20 78 46 69 6e 64 46 75  ents the xFindFu
346bc 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 20 66 6f  nction method fo
346bd 72 20 74 68 65 20 46 54 53 33 0a 2a 2a 20 76 69  r the FTS3.** vi
346be 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a  rtual table..*/.
346bf 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
346c0 65 78 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  extFindFunction(
346c1 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
346c2 2a 70 56 74 61 62 2c 0a 20 20 69 6e 74 20 6e 41  *pVtab,.  int nA
346c3 72 67 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  rg,.  const char
346c4 20 2a 7a 4e 61 6d 65 2c 0a 20 20 76 6f 69 64 20   *zName,.  void 
346c5 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c 69 74  (**pxFunc)(sqlit
346c6 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
346c7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
346c8 2c 0a 20 20 76 6f 69 64 20 2a 2a 70 70 41 72 67  ,.  void **ppArg
346c9 0a 29 7b 0a 20 20 69 66 28 20 73 74 72 63 6d 70  .){.  if( strcmp
346ca 28 7a 4e 61 6d 65 2c 22 73 6e 69 70 70 65 74 22  (zName,"snippet"
346cb 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78 46  )==0 ){.    *pxF
346cc 75 6e 63 20 3d 20 73 6e 69 70 70 65 74 46 75 6e  unc = snippetFun
346cd 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  c;.    return 1;
346ce 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72  .  }else if( str
346cf 63 6d 70 28 7a 4e 61 6d 65 2c 22 6f 66 66 73 65  cmp(zName,"offse
346d0 74 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  ts")==0 ){.    *
346d1 70 78 46 75 6e 63 20 3d 20 73 6e 69 70 70 65 74  pxFunc = snippet
346d2 4f 66 66 73 65 74 73 46 75 6e 63 3b 0a 20 20 20  OffsetsFunc;.   
346d3 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
346d4 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e  se if( strcmp(zN
346d5 61 6d 65 2c 22 6f 70 74 69 6d 69 7a 65 22 29 3d  ame,"optimize")=
346d6 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78 46 75 6e  =0 ){.    *pxFun
346d7 63 20 3d 20 6f 70 74 69 6d 69 7a 65 46 75 6e 63  c = optimizeFunc
346d8 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
346d9 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
346da 53 54 0a 20 20 20 20 2f 2a 20 4e 4f 54 45 28 73  ST.    /* NOTE(s
346db 68 65 73 73 29 3a 20 54 68 65 73 65 20 66 75 6e  hess): These fun
346dc 63 74 69 6f 6e 73 20 61 72 65 20 70 72 65 73 65  ctions are prese
346dd 6e 74 20 6f 6e 6c 79 20 66 6f 72 20 74 65 73 74  nt only for test
346de 69 6e 67 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f  ing.    ** purpo
346df 73 65 73 2e 20 20 4e 6f 20 70 61 72 74 69 63 75  ses.  No particu
346e0 6c 61 72 20 65 66 66 6f 72 74 20 69 73 20 6d 61  lar effort is ma
346e1 64 65 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 74  de to optimize t
346e2 68 65 69 72 0a 20 20 20 20 2a 2a 20 65 78 65 63  heir.    ** exec
346e3 75 74 69 6f 6e 20 6f 72 20 68 6f 77 20 74 68 65  ution or how the
346e4 79 20 62 75 69 6c 64 20 74 68 65 69 72 20 72 65  y build their re
346e5 73 75 6c 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  sults..    */.  
346e6 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70  }else if( strcmp
346e7 28 7a 4e 61 6d 65 2c 22 64 75 6d 70 5f 74 65 72  (zName,"dump_ter
346e8 6d 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  ms")==0 ){.    /
346e9 2a 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72  * fprintf(stderr
346ea 2c 20 22 46 6f 75 6e 64 20 64 75 6d 70 5f 74 65  , "Found dump_te
346eb 72 6d 73 5c 6e 22 29 3b 20 2a 2f 0a 20 20 20 20  rms\n"); */.    
346ec 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d 70 54 65  *pxFunc = dumpTe
346ed 72 6d 73 46 75 6e 63 3b 0a 20 20 20 20 72 65 74  rmsFunc;.    ret
346ee 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 1;.  }else i
346ef 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c  f( strcmp(zName,
346f0 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 29 3d  "dump_doclist")=
346f1 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 70 72  =0 ){.    /* fpr
346f2 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 46 6f  intf(stderr, "Fo
346f3 75 6e 64 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74  und dump_doclist
346f4 5c 6e 22 29 3b 20 2a 2f 0a 20 20 20 20 2a 70 78  \n"); */.    *px
346f5 46 75 6e 63 20 3d 20 64 75 6d 70 44 6f 63 6c 69  Func = dumpDocli
346f6 73 74 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75  stFunc;.    retu
346f7 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  rn 1;.#endif.  }
346f8 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
346f9 2f 2a 0a 2a 2a 20 52 65 6e 61 6d 65 20 61 6e 20  /*.** Rename an 
346fa 66 74 73 33 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  fts3 table..*/.s
346fb 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
346fc 78 74 52 65 6e 61 6d 65 28 0a 20 20 73 71 6c 69  xtRename(.  sqli
346fd 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c  te3_vtab *pVtab,
346fe 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
346ff 4e 61 6d 65 0a 29 7b 0a 20 20 66 75 6c 6c 74 65  Name.){.  fullte
34700 78 74 5f 76 74 61 62 20 2a 70 20 3d 20 28 66 75  xt_vtab *p = (fu
34701 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56  lltext_vtab *)pV
34702 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  tab;.  int rc = 
34703 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
34704 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c  char *zSql = sql
34705 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
34706 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
34707 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 20  Q.'%q_content'  
34708 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 63 6f  RENAME TO '%q_co
34709 6e 74 65 6e 74 27 3b 22 0a 20 20 20 20 22 41 4c  ntent';".    "AL
3470a 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71  TER TABLE %Q.'%q
3470b 5f 73 65 67 6d 65 6e 74 73 27 20 52 45 4e 41 4d  _segments' RENAM
3470c 45 20 54 4f 20 27 25 71 5f 73 65 67 6d 65 6e 74  E TO '%q_segment
3470d 73 27 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20  s';".    "ALTER 
3470e 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73 65 67  TABLE %Q.'%q_seg
3470f 64 69 72 27 20 20 20 52 45 4e 41 4d 45 20 54 4f  dir'   RENAME TO
34710 20 27 25 71 5f 73 65 67 64 69 72 27 3b 22 0a 20   '%q_segdir';". 
34711 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e     , p->zDb, p->
34712 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20 20  zName, zName .  
34713 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a    , p->zDb, p->z
34714 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20 20 20  Name, zName .   
34715 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e   , p->zDb, p->zN
34716 61 6d 65 2c 20 7a 4e 61 6d 65 0a 20 20 29 3b 0a  ame, zName.  );.
34717 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
34718 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
34719 78 65 63 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c  xec(p->db, zSql,
3471a 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73   0, 0, 0);.    s
3471b 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c  qlite3_free(zSql
3471c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
3471d 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f  rc;.}..static co
3471e 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
3471f 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65 20 3d 20  le fts3Module = 
34720 7b 0a 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20  {.  /* iVersion 
34721 20 20 20 20 20 2a 2f 20 30 2c 0a 20 20 2f 2a 20       */ 0,.  /* 
34722 78 43 72 65 61 74 65 20 20 20 20 20 20 20 2a 2f  xCreate       */
34723 20 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65 2c   fulltextCreate,
34724 0a 20 20 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 20  .  /* xConnect  
34725 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43      */ fulltextC
34726 6f 6e 6e 65 63 74 2c 0a 20 20 2f 2a 20 78 42 65  onnect,.  /* xBe
34727 73 74 49 6e 64 65 78 20 20 20 20 2a 2f 20 66 75  stIndex    */ fu
34728 6c 6c 74 65 78 74 42 65 73 74 49 6e 64 65 78 2c  lltextBestIndex,
34729 0a 20 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63  .  /* xDisconnec
3472a 74 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 44  t   */ fulltextD
3472b 69 73 63 6f 6e 6e 65 63 74 2c 0a 20 20 2f 2a 20  isconnect,.  /* 
3472c 78 44 65 73 74 72 6f 79 20 20 20 20 20 20 2a 2f  xDestroy      */
3472d 20 66 75 6c 6c 74 65 78 74 44 65 73 74 72 6f 79   fulltextDestroy
3472e 2c 0a 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20  ,.  /* xOpen    
3472f 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
34730 4f 70 65 6e 2c 0a 20 20 2f 2a 20 78 43 6c 6f 73  Open,.  /* xClos
34731 65 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c  e        */ full
34732 74 65 78 74 43 6c 6f 73 65 2c 0a 20 20 2f 2a 20  textClose,.  /* 
34733 78 46 69 6c 74 65 72 20 20 20 20 20 20 20 2a 2f  xFilter       */
34734 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72 2c   fulltextFilter,
34735 0a 20 20 2f 2a 20 78 4e 65 78 74 20 20 20 20 20  .  /* xNext     
34736 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 4e      */ fulltextN
34737 65 78 74 2c 0a 20 20 2f 2a 20 78 45 6f 66 20 20  ext,.  /* xEof  
34738 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74          */ fullt
34739 65 78 74 45 6f 66 2c 0a 20 20 2f 2a 20 78 43 6f  extEof,.  /* xCo
3473a 6c 75 6d 6e 20 20 20 20 20 20 20 2a 2f 20 66 75  lumn       */ fu
3473b 6c 6c 74 65 78 74 43 6f 6c 75 6d 6e 2c 0a 20 20  lltextColumn,.  
3473c 2f 2a 20 78 52 6f 77 69 64 20 20 20 20 20 20 20  /* xRowid       
3473d 20 2a 2f 20 66 75 6c 6c 74 65 78 74 52 6f 77 69   */ fulltextRowi
3473e 64 2c 0a 20 20 2f 2a 20 78 55 70 64 61 74 65 20  d,.  /* xUpdate 
3473f 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
34740 74 55 70 64 61 74 65 2c 0a 20 20 2f 2a 20 78 42  tUpdate,.  /* xB
34741 65 67 69 6e 20 20 20 20 20 20 20 20 2a 2f 20 66  egin        */ f
34742 75 6c 6c 74 65 78 74 42 65 67 69 6e 2c 0a 20 20  ulltextBegin,.  
34743 2f 2a 20 78 53 79 6e 63 20 20 20 20 20 20 20 20  /* xSync        
34744 20 2a 2f 20 66 75 6c 6c 74 65 78 74 53 79 6e 63   */ fulltextSync
34745 2c 0a 20 20 2f 2a 20 78 43 6f 6d 6d 69 74 20 20  ,.  /* xCommit  
34746 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
34747 43 6f 6d 6d 69 74 2c 0a 20 20 2f 2a 20 78 52 6f  Commit,.  /* xRo
34748 6c 6c 62 61 63 6b 20 20 20 20 20 2a 2f 20 66 75  llback     */ fu
34749 6c 6c 74 65 78 74 52 6f 6c 6c 62 61 63 6b 2c 0a  lltextRollback,.
3474a 20 20 2f 2a 20 78 46 69 6e 64 46 75 6e 63 74 69    /* xFindFuncti
3474b 6f 6e 20 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69  on */ fulltextFi
3474c 6e 64 46 75 6e 63 74 69 6f 6e 2c 0a 20 20 2f 2a  ndFunction,.  /*
3474d 20 78 52 65 6e 61 6d 65 20 2a 2f 20 20 20 20 20   xRename */     
3474e 20 20 66 75 6c 6c 74 65 78 74 52 65 6e 61 6d 65    fulltextRename
3474f 2c 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  ,.};..static voi
34750 64 20 68 61 73 68 44 65 73 74 72 6f 79 28 76 6f  d hashDestroy(vo
34751 69 64 20 2a 70 29 7b 0a 20 20 66 74 73 33 48 61  id *p){.  fts3Ha
34752 73 68 20 2a 70 48 61 73 68 20 3d 20 28 66 74 73  sh *pHash = (fts
34753 33 48 61 73 68 20 2a 29 70 3b 0a 20 20 73 71 6c  3Hash *)p;.  sql
34754 69 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61  ite3Fts3HashClea
34755 72 28 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69  r(pHash);.  sqli
34756 74 65 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b  te3_free(pHash);
34757 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 74  .}../*.** The ft
34758 73 33 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65  s3 built-in toke
34759 6e 69 7a 65 72 73 20 2d 20 22 73 69 6d 70 6c 65  nizers - "simple
3475a 22 20 61 6e 64 20 22 70 6f 72 74 65 72 22 20 2d  " and "porter" -
3475b 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   are implemented
3475c 0a 2a 2a 20 69 6e 20 66 69 6c 65 73 20 66 74 73  .** in files fts
3475d 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 61  3_tokenizer1.c a
3475e 6e 64 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63  nd fts3_porter.c
3475f 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54   respectively. T
34760 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
34761 74 77 6f 20 66 6f 72 77 61 72 64 20 64 65 63 6c  two forward decl
34762 61 72 61 74 69 6f 6e 73 20 61 72 65 20 66 6f 72  arations are for
34763 20 66 75 6e 63 74 69 6f 6e 73 20 64 65 63 6c 61   functions decla
34764 72 65 64 20 69 6e 20 74 68 65 73 65 20 66 69 6c  red in these fil
34765 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 72 65  es.** used to re
34766 74 72 69 65 76 65 20 74 68 65 20 72 65 73 70 65  trieve the respe
34767 63 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61  ctive implementa
34768 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c  tions..**.** Cal
34769 6c 69 6e 67 20 73 71 6c 69 74 65 33 46 74 73 33  ling sqlite3Fts3
3476a 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d  SimpleTokenizerM
3476b 6f 64 75 6c 65 28 29 20 73 65 74 73 20 74 68 65  odule() sets the
3476c 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a   value pointed.*
3476d 2a 20 74 6f 20 62 79 20 74 68 65 20 61 72 67 75  * to by the argu
3476e 6d 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20 61 20  ment to point a 
3476f 74 68 65 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b  the "simple" tok
34770 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
34771 61 74 69 6f 6e 2e 0a 2a 2a 20 46 75 6e 63 74 69  ation..** Functi
34772 6f 6e 20 2e 2e 2e 50 6f 72 74 65 72 54 6f 6b 65  on ...PorterToke
34773 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73 65  nizerModule() se
34774 74 73 20 2a 70 4d 6f 64 75 6c 65 20 74 6f 20 70  ts *pModule to p
34775 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70  oint to the.** p
34776 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65 72 2f  orter tokenizer/
34777 73 74 65 6d 6d 65 72 20 69 6d 70 6c 65 6d 65 6e  stemmer implemen
34778 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  tation..*/.SQLIT
34779 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
3477a 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65  qlite3Fts3Simple
3477b 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
3477c 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3477d 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a  r_module const**
3477e 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c 49 54  ppModule);.SQLIT
3477f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
34780 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72  qlite3Fts3Porter
34781 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
34782 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34783 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a  r_module const**
34784 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c 49 54  ppModule);.SQLIT
34785 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
34786 71 6c 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b  qlite3Fts3IcuTok
34787 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c  enizerModule(sql
34788 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
34789 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d  odule const**ppM
3478a 6f 64 75 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f  odule);..SQLITE_
3478b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
3478c 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68 54  te3Fts3InitHashT
3478d 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 2c 20  able(sqlite3 *, 
3478e 66 74 73 33 48 61 73 68 20 2a 2c 20 63 6f 6e 73  fts3Hash *, cons
3478f 74 20 63 68 61 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a  t char *);../*.*
34790 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 74 68 65  * Initialise the
34791 20 66 74 73 33 20 65 78 74 65 6e 73 69 6f 6e 2e   fts3 extension.
34792 20 49 66 20 74 68 69 73 20 65 78 74 65 6e 73 69   If this extensi
34793 6f 6e 20 69 73 20 62 75 69 6c 74 20 61 73 20 70  on is built as p
34794 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71  art.** of the sq
34795 6c 69 74 65 20 6c 69 62 72 61 72 79 2c 20 74 68  lite library, th
34796 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
34797 20 69 73 20 63 61 6c 6c 65 64 20 64 69 72 65 63   is called direc
34798 74 6c 79 20 62 79 0a 2a 2a 20 53 51 4c 69 74 65  tly by.** SQLite
34799 2e 20 49 66 20 66 74 73 33 20 69 73 20 62 75 69  . If fts3 is bui
3479a 6c 74 20 61 73 20 61 20 64 79 6e 61 6d 69 63 61  lt as a dynamica
3479b 6c 6c 79 20 6c 6f 61 64 61 62 6c 65 20 65 78 74  lly loadable ext
3479c 65 6e 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20  ension, this.** 
3479d 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
3479e 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ed by the sqlite
3479f 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
347a0 28 29 20 65 6e 74 72 79 20 70 6f 69 6e 74 2e 0a  () entry point..
347a1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
347a2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
347a3 33 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  3Init(sqlite3 *d
347a4 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  b){.  int rc = S
347a5 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 66 74 73 33  QLITE_OK;.  fts3
347a6 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 30 3b  Hash *pHash = 0;
347a7 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
347a8 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
347a9 65 20 2a 70 53 69 6d 70 6c 65 20 3d 20 30 3b 0a  e *pSimple = 0;.
347aa 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
347ab 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
347ac 20 2a 70 50 6f 72 74 65 72 20 3d 20 30 3b 0a 20   *pPorter = 0;. 
347ad 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
347ae 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
347af 2a 70 49 63 75 20 3d 20 30 3b 0a 0a 20 20 73 71  *pIcu = 0;..  sq
347b0 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54  lite3Fts3SimpleT
347b1 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26  okenizerModule(&
347b2 70 53 69 6d 70 6c 65 29 3b 0a 20 20 73 71 6c 69  pSimple);.  sqli
347b3 74 65 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b  te3Fts3PorterTok
347b4 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 50  enizerModule(&pP
347b5 6f 72 74 65 72 29 3b 0a 23 69 66 64 65 66 20 53  orter);.#ifdef S
347b6 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
347b7 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 49 63  .  sqlite3Fts3Ic
347b8 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
347b9 28 26 70 49 63 75 29 3b 0a 23 65 6e 64 69 66 0a  (&pIcu);.#endif.
347ba 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
347bb 6e 64 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68  nd initialise th
347bc 65 20 68 61 73 68 2d 74 61 62 6c 65 20 75 73 65  e hash-table use
347bd 64 20 74 6f 20 73 74 6f 72 65 20 74 6f 6b 65 6e  d to store token
347be 69 7a 65 72 73 2e 20 2a 2f 0a 20 20 70 48 61 73  izers. */.  pHas
347bf 68 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  h = sqlite3_mall
347c0 6f 63 28 73 69 7a 65 6f 66 28 66 74 73 33 48 61  oc(sizeof(fts3Ha
347c1 73 68 29 29 3b 0a 20 20 69 66 28 20 21 70 48 61  sh));.  if( !pHa
347c2 73 68 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  sh ){.    rc = S
347c3 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
347c4 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
347c5 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 70 48  3Fts3HashInit(pH
347c6 61 73 68 2c 20 46 54 53 33 5f 48 41 53 48 5f 53  ash, FTS3_HASH_S
347c7 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20 7d 0a 0a  TRING, 1);.  }..
347c8 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 62 75    /* Load the bu
347c9 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a 65 72  ilt-in tokenizer
347ca 73 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20  s into the hash 
347cb 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 72  table */.  if( r
347cc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
347cd 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
347ce 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48  ts3HashInsert(pH
347cf 61 73 68 2c 20 22 73 69 6d 70 6c 65 22 2c 20 37  ash, "simple", 7
347d0 2c 20 28 76 6f 69 64 20 2a 29 70 53 69 6d 70 6c  , (void *)pSimpl
347d1 65 29 0a 20 20 20 20 20 7c 7c 20 73 71 6c 69 74  e).     || sqlit
347d2 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
347d3 28 70 48 61 73 68 2c 20 22 70 6f 72 74 65 72 22  (pHash, "porter"
347d4 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70 50 6f  , 7, (void *)pPo
347d5 72 74 65 72 29 20 0a 20 20 20 20 20 7c 7c 20 28  rter) .     || (
347d6 70 49 63 75 20 26 26 20 73 71 6c 69 74 65 33 46  pIcu && sqlite3F
347d7 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48  ts3HashInsert(pH
347d8 61 73 68 2c 20 22 69 63 75 22 2c 20 34 2c 20 28  ash, "icu", 4, (
347d9 76 6f 69 64 20 2a 29 70 49 63 75 29 29 0a 20 20  void *)pIcu)).  
347da 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20    ){.      rc = 
347db 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
347dc 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20    }.  }..#ifdef 
347dd 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71  SQLITE_TEST.  sq
347de 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69  lite3Fts3ExprIni
347df 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 64  tTestInterface(d
347e0 62 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  b);.#endif..  /*
347e1 20 43 72 65 61 74 65 20 74 68 65 20 76 69 72 74   Create the virt
347e2 75 61 6c 20 74 61 62 6c 65 20 77 72 61 70 70 65  ual table wrappe
347e3 72 20 61 72 6f 75 6e 64 20 74 68 65 20 68 61 73  r around the has
347e4 68 2d 74 61 62 6c 65 20 61 6e 64 20 6f 76 65 72  h-table and over
347e5 6c 6f 61 64 20 0a 20 20 2a 2a 20 74 68 65 20 74  load .  ** the t
347e6 77 6f 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69  wo scalar functi
347e7 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73 20  ons. If this is 
347e8 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 67 69  successful, regi
347e9 73 74 65 72 20 74 68 65 0a 20 20 2a 2a 20 6d 6f  ster the.  ** mo
347ea 64 75 6c 65 20 77 69 74 68 20 73 71 6c 69 74 65  dule with sqlite
347eb 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c  ..  */.  if( SQL
347ec 49 54 45 5f 4f 4b 3d 3d 72 63 20 0a 20 20 20 26  ITE_OK==rc .   &
347ed 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  & SQLITE_OK==(rc
347ee 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e   = sqlite3Fts3In
347ef 69 74 48 61 73 68 54 61 62 6c 65 28 64 62 2c 20  itHashTable(db, 
347f0 70 48 61 73 68 2c 20 22 66 74 73 33 5f 74 6f 6b  pHash, "fts3_tok
347f1 65 6e 69 7a 65 72 22 29 29 0a 20 20 20 26 26 20  enizer")).   && 
347f2 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
347f3 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
347f4 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22  d_function(db, "
347f5 73 6e 69 70 70 65 74 22 2c 20 2d 31 29 29 0a 20  snippet", -1)). 
347f6 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
347f7 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76  (rc = sqlite3_ov
347f8 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
347f9 64 62 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 2d  db, "offsets", -
347fa 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45  1)).   && SQLITE
347fb 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
347fc 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
347fd 74 69 6f 6e 28 64 62 2c 20 22 6f 70 74 69 6d 69  tion(db, "optimi
347fe 7a 65 22 2c 20 2d 31 29 29 0a 23 69 66 64 65 66  ze", -1)).#ifdef
347ff 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
34800 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
34801 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  c = sqlite3_over
34802 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62  load_function(db
34803 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20  , "dump_terms", 
34804 2d 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54  -1)).   && SQLIT
34805 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
34806 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
34807 63 74 69 6f 6e 28 64 62 2c 20 22 64 75 6d 70 5f  ction(db, "dump_
34808 64 6f 63 6c 69 73 74 22 2c 20 2d 31 29 29 0a 23  doclist", -1)).#
34809 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20 72  endif.  ){.    r
3480a 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63 72  eturn sqlite3_cr
3480b 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a  eate_module_v2(.
3480c 20 20 20 20 20 20 20 20 64 62 2c 20 22 66 74 73          db, "fts
3480d 33 22 2c 20 26 66 74 73 33 4d 6f 64 75 6c 65 2c  3", &fts3Module,
3480e 20 28 76 6f 69 64 20 2a 29 70 48 61 73 68 2c 20   (void *)pHash, 
3480f 68 61 73 68 44 65 73 74 72 6f 79 0a 20 20 20 20  hashDestroy.    
34810 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20  );.  }..  /* An 
34811 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
34812 65 64 2e 20 44 65 6c 65 74 65 20 74 68 65 20 68  ed. Delete the h
34813 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65  ash table and re
34814 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63  turn the error c
34815 6f 64 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ode. */.  assert
34816 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34817 29 3b 0a 20 20 69 66 28 20 70 48 61 73 68 20 29  );.  if( pHash )
34818 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
34819 33 48 61 73 68 43 6c 65 61 72 28 70 48 61 73 68  3HashClear(pHash
3481a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
3481b 72 65 65 28 70 48 61 73 68 29 3b 0a 20 20 7d 0a  ree(pHash);.  }.
3481c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3481d 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52 45  #if !SQLITE_CORE
3481e 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3481f 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
34820 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  n_init(.  sqlite
34821 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a  3 *db, .  char *
34822 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e  *pzErrMsg,.  con
34823 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
34824 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b  outines *pApi.){
34825 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
34826 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a  ION_INIT2(pApi).
34827 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
34828 46 74 73 33 49 6e 69 74 28 64 62 29 3b 0a 7d 0a  Fts3Init(db);.}.
34829 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
3482a 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
3482b 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3482c 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3482d 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
3482e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3482f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f fts3.c *******
34830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34831 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34832 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
34833 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
34834 20 66 69 6c 65 20 66 74 73 33 5f 65 78 70 72 2e   file fts3_expr.
34835 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
34836 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34837 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
34838 20 32 30 30 38 20 4e 6f 76 20 32 38 0a 2a 2a 0a   2008 Nov 28.**.
34839 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
3483a 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
3483b 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
3483c 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
3483d 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
3483e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
3483f 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
34840 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
34841 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
34842 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
34843 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
34844 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
34845 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
34846 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
34847 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
34848 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
34849 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
3484a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
3484b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3484c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3484d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3484e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3484f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  .**.** This modu
34850 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
34851 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
34852 20 61 20 70 61 72 73 65 72 20 66 6f 72 20 66 74   a parser for ft
34853 73 33 20 71 75 65 72 79 20 73 74 72 69 6e 67 73  s3 query strings
34854 0a 2a 2a 20 28 74 68 65 20 72 69 67 68 74 2d 68  .** (the right-h
34855 61 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  and argument to 
34856 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74  the MATCH operat
34857 6f 72 29 2e 20 42 65 63 61 75 73 65 20 74 68 65  or). Because the
34858 20 73 75 70 70 6f 72 74 65 64 20 0a 2a 2a 20 73   supported .** s
34859 79 6e 74 61 78 20 69 73 20 72 65 6c 61 74 69 76  yntax is relativ
3485a 65 6c 79 20 73 69 6d 70 6c 65 2c 20 74 68 65 20  ely simple, the 
3485b 77 68 6f 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 2f  whole tokenizer/
3485c 70 61 72 73 65 72 20 73 79 73 74 65 6d 20 69 73  parser system is
3485d 0a 2a 2a 20 68 61 6e 64 2d 63 6f 64 65 64 2e 20  .** hand-coded. 
3485e 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72  The public inter
3485f 66 61 63 65 20 74 6f 20 74 68 69 73 20 6d 6f 64  face to this mod
34860 75 6c 65 20 69 73 20 64 65 63 6c 61 72 65 64 20  ule is declared 
34861 69 6e 20 73 6f 75 72 63 65 0a 2a 2a 20 63 6f 64  in source.** cod
34862 65 20 66 69 6c 65 20 22 66 74 73 33 5f 65 78 70  e file "fts3_exp
34863 72 2e 68 22 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  r.h"..*/.#if !de
34864 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
34865 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
34866 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
34867 29 0a 0a 2f 2a 0a 2a 2a 20 42 79 20 64 65 66 61  )../*.** By defa
34868 75 6c 74 2c 20 74 68 69 73 20 6d 6f 64 75 6c 65  ult, this module
34869 20 70 61 72 73 65 73 20 74 68 65 20 6c 65 67 61   parses the lega
3486a 63 79 20 73 79 6e 74 61 78 20 74 68 61 74 20 68  cy syntax that h
3486b 61 73 20 62 65 65 6e 20 0a 2a 2a 20 74 72 61 64  as been .** trad
3486c 69 74 69 6f 6e 61 6c 6c 79 20 75 73 65 64 20 62  itionally used b
3486d 79 20 66 74 73 33 2e 20 4f 72 2c 20 69 66 20 53  y fts3. Or, if S
3486e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
3486f 33 5f 50 41 52 45 4e 54 48 45 53 49 53 0a 2a 2a  3_PARENTHESIS.**
34870 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
34871 6e 20 69 74 20 75 73 65 73 20 74 68 65 20 6e 65  n it uses the ne
34872 77 20 73 79 6e 74 61 78 2e 20 54 68 65 20 64 69  w syntax. The di
34873 66 66 65 72 65 6e 63 65 73 20 62 65 74 77 65 65  fferences betwee
34874 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 61 6e 64  n.** the new and
34875 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 65   the old syntaxe
34876 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 61 29  s are:.**.**  a)
34877 20 54 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20   The new syntax 
34878 73 75 70 70 6f 72 74 73 20 70 61 72 65 6e 74 68  supports parenth
34879 65 73 69 73 2e 20 54 68 65 20 6f 6c 64 20 64 6f  esis. The old do
3487a 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 20 62  es not..**.**  b
3487b 29 20 54 68 65 20 6e 65 77 20 73 79 6e 74 61 78  ) The new syntax
3487c 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 41 4e   supports the AN
3487d 44 20 61 6e 64 20 4e 4f 54 20 6f 70 65 72 61 74  D and NOT operat
3487e 6f 72 73 2e 20 54 68 65 20 6f 6c 64 20 64 6f 65  ors. The old doe
3487f 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 20 63 29  s not..**.**  c)
34880 20 54 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 20   The old syntax 
34881 73 75 70 70 6f 72 74 73 20 74 68 65 20 22 2d 22  supports the "-"
34882 20 74 6f 6b 65 6e 20 71 75 61 6c 69 66 69 65 72   token qualifier
34883 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 0a 2a  . This is not .*
34884 2a 20 20 20 20 20 73 75 70 70 6f 72 74 65 64 20  *     supported 
34885 62 79 20 74 68 65 20 6e 65 77 20 73 79 6e 74 61  by the new synta
34886 78 20 28 69 74 20 69 73 20 72 65 70 6c 61 63 65  x (it is replace
34887 64 20 62 79 20 74 68 65 20 4e 4f 54 20 6f 70 65  d by the NOT ope
34888 72 61 74 6f 72 29 2e 0a 2a 2a 0a 2a 2a 20 20 64  rator)..**.**  d
34889 29 20 57 68 65 6e 20 75 73 69 6e 67 20 74 68 65  ) When using the
3488a 20 6f 6c 64 20 73 79 6e 74 61 78 2c 20 74 68 65   old syntax, the
3488b 20 4f 52 20 6f 70 65 72 61 74 6f 72 20 68 61 73   OR operator has
3488c 20 61 20 67 72 65 61 74 65 72 20 70 72 65 63 65   a greater prece
3488d 64 65 6e 63 65 0a 2a 2a 20 20 20 20 20 74 68 61  dence.**     tha
3488e 6e 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e  n an implicit AN
3488f 44 2e 20 57 68 65 6e 20 75 73 69 6e 67 20 74 68  D. When using th
34890 65 20 6e 65 77 2c 20 62 6f 74 68 20 69 6d 70 6c  e new, both impl
34891 69 63 69 74 79 20 61 6e 64 20 65 78 70 6c 69 63  icity and explic
34892 69 74 0a 2a 2a 20 20 20 20 20 41 4e 44 20 6f 70  it.**     AND op
34893 65 72 61 74 6f 72 73 20 68 61 76 65 20 61 20 68  erators have a h
34894 69 67 68 65 72 20 70 72 65 63 65 64 65 6e 63 65  igher precedence
34895 20 74 68 61 6e 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20   than OR..**.** 
34896 49 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  If compiled with
34897 20 53 51 4c 49 54 45 5f 54 45 53 54 20 64 65 66   SQLITE_TEST def
34898 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20  ined, then this 
34899 6d 6f 64 75 6c 65 20 65 78 70 6f 72 74 73 20 74  module exports t
3489a 68 65 0a 2a 2a 20 73 79 6d 62 6f 6c 20 22 69 6e  he.** symbol "in
3489b 74 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65  t sqlite3_fts3_e
3489c 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
3489d 73 22 2e 20 53 65 74 74 69 6e 67 20 74 68 69 73  s". Setting this
3489e 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 74 6f 20   variable.** to 
3489f 7a 65 72 6f 20 63 61 75 73 65 73 20 74 68 65 20  zero causes the 
348a0 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 74 68  module to use th
348a1 65 20 6f 6c 64 20 73 79 6e 74 61 78 2e 20 49 66  e old syntax. If
348a2 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 0a 2a   it is set to .*
348a3 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 20 6e  * non-zero the n
348a4 65 77 20 73 79 6e 74 61 78 20 69 73 20 61 63 74  ew syntax is act
348a5 69 76 61 74 65 64 2e 20 54 68 69 73 20 69 73 20  ivated. This is 
348a6 73 6f 20 62 6f 74 68 20 73 79 6e 74 61 78 65 73  so both syntaxes
348a7 20 63 61 6e 0a 2a 2a 20 62 65 20 74 65 73 74 65   can.** be teste
348a8 64 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65  d using a single
348a9 20 62 75 69 6c 64 20 6f 66 20 74 65 73 74 66 69   build of testfi
348aa 78 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  xture..*/.#ifdef
348ab 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
348ac 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
348ad 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
348ae 70 61 72 65 6e 74 68 65 73 65 73 20 3d 20 30 3b  parentheses = 0;
348af 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53  .#else.# ifdef S
348b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
348b1 33 5f 50 41 52 45 4e 54 48 45 53 49 53 20 0a 23  3_PARENTHESIS .#
348b2 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
348b3 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
348b4 65 6e 74 68 65 73 65 73 20 31 0a 23 20 65 6c 73  entheses 1.# els
348b5 65 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69  e.#  define sqli
348b6 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
348b7 70 61 72 65 6e 74 68 65 73 65 73 20 30 0a 23 20  parentheses 0.# 
348b8 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
348b9 0a 2a 2a 20 44 65 66 61 75 6c 74 20 73 70 61 6e  .** Default span
348ba 20 66 6f 72 20 4e 45 41 52 20 6f 70 65 72 61 74   for NEAR operat
348bb 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ors..*/.#define 
348bc 53 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46 41  SQLITE_FTS3_DEFA
348bd 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 20 31  ULT_NEAR_PARAM 1
348be 30 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  0...typedef stru
348bf 63 74 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20  ct ParseContext 
348c0 50 61 72 73 65 43 6f 6e 74 65 78 74 3b 0a 73 74  ParseContext;.st
348c1 72 75 63 74 20 50 61 72 73 65 43 6f 6e 74 65 78  ruct ParseContex
348c2 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  t {.  sqlite3_to
348c3 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
348c4 7a 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 6f 6b  zer;      /* Tok
348c5 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f  enizer module */
348c6 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
348c7 61 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  azCol;          
348c8 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
348c9 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  of column names 
348ca 66 6f 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a  for fts3 table *
348cb 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  /.  int nCol;   
348cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348cd 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
348ce 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
348cf 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  azCol[] */.  int
348d0 20 69 44 65 66 61 75 6c 74 43 6f 6c 3b 20 20 20   iDefaultCol;   
348d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348d2 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75   /* Default colu
348d3 6d 6e 20 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20  mn to query */. 
348d4 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
348d5 20 2a 70 43 74 78 3b 20 20 20 20 20 20 20 20 20   *pCtx;         
348d6 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72       /* Write er
348d7 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65  ror message here
348d8 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 73 74 3b   */.  int nNest;
348d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
348db 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 62 72  ber of nested br
348dc 61 63 6b 65 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ackets */.};../*
348dd 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
348de 6e 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20  n is equivalent 
348df 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  to the standard 
348e0 69 73 73 70 61 63 65 28 29 20 66 75 6e 63 74 69  isspace() functi
348e1 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  on. .**.** The s
348e2 74 61 6e 64 61 72 64 20 69 73 73 70 61 63 65 28  tandard isspace(
348e3 29 20 63 61 6e 20 62 65 20 61 77 6b 77 61 72 64  ) can be awkward
348e4 20 74 6f 20 75 73 65 20 73 61 66 65 6c 79 2c 20   to use safely, 
348e5 62 65 63 61 75 73 65 20 61 6c 74 68 6f 75 67 68  because although
348e6 20 69 74 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65   it.** is define
348e7 64 20 74 6f 20 61 63 63 65 70 74 20 61 6e 20 61  d to accept an a
348e8 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20  rgument of type 
348e9 69 6e 74 2c 20 69 74 73 20 62 65 68 61 76 69 6f  int, its behavio
348ea 75 72 20 77 68 65 6e 20 70 61 73 73 65 64 0a 2a  ur when passed.*
348eb 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  * an integer tha
348ec 74 20 66 61 6c 6c 73 20 6f 75 74 73 69 64 65 20  t falls outside 
348ed 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20  of the range of 
348ee 74 68 65 20 75 6e 73 69 67 6e 65 64 20 63 68 61  the unsigned cha
348ef 72 20 74 79 70 65 0a 2a 2a 20 69 73 20 75 6e 64  r type.** is und
348f0 65 66 69 6e 65 64 20 28 61 6e 64 20 73 6f 6d 65  efined (and some
348f1 74 69 6d 65 73 2c 20 22 75 6e 64 65 66 69 6e 65  times, "undefine
348f2 64 22 20 6d 65 61 6e 73 20 73 65 67 66 61 75 6c  d" means segfaul
348f3 74 29 2e 20 54 68 69 73 20 77 72 61 70 70 65 72  t). This wrapper
348f4 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74  .** is defined t
348f5 6f 20 61 63 63 65 70 74 20 61 6e 20 61 72 67 75  o accept an argu
348f6 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 63 68 61  ment of type cha
348f7 72 2c 20 61 6e 64 20 61 6c 77 61 79 73 20 72 65  r, and always re
348f8 74 75 72 6e 73 20 30 20 66 6f 72 0a 2a 2a 20 61  turns 0 for.** a
348f9 6e 79 20 76 61 6c 75 65 73 20 74 68 61 74 20 66  ny values that f
348fa 61 6c 6c 20 6f 75 74 73 69 64 65 20 6f 66 20 74  all outside of t
348fb 68 65 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20  he range of the 
348fc 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 79  unsigned char ty
348fd 70 65 20 28 69 2e 65 2e 0a 2a 2a 20 6e 65 67 61  pe (i.e..** nega
348fe 74 69 76 65 20 76 61 6c 75 65 73 29 2e 0a 2a 2f  tive values)..*/
348ff 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
34900 69 73 73 70 61 63 65 28 63 68 61 72 20 63 29 7b  isspace(char c){
34901 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38  .  return (c&0x8
34902 30 29 3d 3d 30 20 3f 20 69 73 73 70 61 63 65 28  0)==0 ? isspace(
34903 63 29 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  c) : 0;.}../*.**
34904 20 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 78   Extract the nex
34905 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 62 75 66  t token from buf
34906 66 65 72 20 7a 20 28 6c 65 6e 67 74 68 20 6e 29  fer z (length n)
34907 20 75 73 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e   using the token
34908 69 7a 65 72 0a 2a 2a 20 61 6e 64 20 6f 74 68 65  izer.** and othe
34909 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 63  r information (c
3490a 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 65 74 63 2e  olumn names etc.
3490b 29 20 69 6e 20 70 50 61 72 73 65 2e 20 43 72 65  ) in pParse. Cre
3490c 61 74 65 20 61 6e 20 46 74 73 33 45 78 70 72 0a  ate an Fts3Expr.
3490d 2a 2a 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  ** structure of 
3490e 74 79 70 65 20 46 54 53 51 55 45 52 59 5f 50 48  type FTSQUERY_PH
3490f 52 41 53 45 20 63 6f 6e 74 61 69 6e 69 6e 67 20  RASE containing 
34910 61 20 70 68 72 61 73 65 20 63 6f 6e 73 69 73 74  a phrase consist
34911 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73  ing of this.** s
34912 69 6e 67 6c 65 20 74 6f 6b 65 6e 20 61 6e 64 20  ingle token and 
34913 73 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20 70  set *ppExpr to p
34914 6f 69 6e 74 20 74 6f 20 69 74 2e 20 49 66 20 74  oint to it. If t
34915 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75  he end of the bu
34916 66 66 65 72 20 69 73 0a 2a 2a 20 72 65 61 63 68  ffer is.** reach
34917 65 64 20 62 65 66 6f 72 65 20 61 20 74 6f 6b 65  ed before a toke
34918 6e 20 69 73 20 66 6f 75 6e 64 2c 20 73 65 74 20  n is found, set 
34919 2a 70 70 45 78 70 72 20 74 6f 20 7a 65 72 6f 2e  *ppExpr to zero.
3491a 20 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 72 65   It is the.** re
3491b 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
3491c 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65 76  the caller to ev
3491d 65 6e 74 75 61 6c 6c 79 20 64 65 61 6c 6c 6f 63  entually dealloc
3491e 61 74 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  ate the allocate
3491f 64 20 0a 2a 2a 20 46 74 73 33 45 78 70 72 20 73  d .** Fts3Expr s
34920 74 72 75 63 74 75 72 65 20 28 69 66 20 61 6e 79  tructure (if any
34921 29 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20  ) by passing it 
34922 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  to sqlite3_free(
34923 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  )..**.** Return 
34924 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63  SQLITE_OK if suc
34925 63 65 73 73 66 75 6c 2c 20 6f 72 20 53 51 4c 49  cessful, or SQLI
34926 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65  TE_NOMEM if a me
34927 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  mory allocation.
34928 2a 2a 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  ** fails..*/.sta
34929 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74 54  tic int getNextT
3492a 6f 6b 65 6e 28 0a 20 20 50 61 72 73 65 43 6f 6e  oken(.  ParseCon
3492b 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20 20  text *pParse,   
3492c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3492d 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20 70 61  /* fts3 query pa
3492e 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
3492f 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20   int iCol,      
34930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34931 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
34932 65 20 66 6f 72 20 46 74 73 33 50 68 72 61 73 65  e for Fts3Phrase
34933 2e 69 43 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f  .iColumn */.  co
34934 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
34935 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
34936 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73        /* Input s
34937 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45  tring */.  Fts3E
34938 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20  xpr **ppExpr,   
34939 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3493a 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65     /* OUT: expre
3493b 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  ssion */.  int *
3493c 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20 20 20 20  pnConsumed      
3493d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3493e 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
3493f 72 20 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75  r of bytes consu
34940 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  med */.){.  sqli
34941 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
34942 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50 61 72  Tokenizer = pPar
34943 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  se->pTokenizer;.
34944 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
34945 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
34946 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b   *pModule = pTok
34947 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b  enizer->pModule;
34948 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
34949 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3494a 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a  ursor *pCursor;.
3494b 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 65 74    Fts3Expr *pRet
3494c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 43 6f 6e   = 0;.  int nCon
3494d 73 75 6d 65 64 20 3d 20 30 3b 0a 0a 20 20 72 63  sumed = 0;..  rc
3494e 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65   = pModule->xOpe
3494f 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 2c  n(pTokenizer, z,
34950 20 6e 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a 20   n, &pCursor);. 
34951 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
34952 4f 4b 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  OK ){.    const 
34953 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20  char *zToken;.  
34954 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69 53    int nToken, iS
34955 74 61 72 74 2c 20 69 45 6e 64 2c 20 69 50 6f 73  tart, iEnd, iPos
34956 69 74 69 6f 6e 3b 0a 20 20 20 20 69 6e 74 20 6e  ition;.    int n
34957 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  Byte;           
34958 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34959 20 20 20 20 2f 2a 20 74 6f 74 61 6c 20 73 70 61      /* total spa
3495a 63 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a  ce to allocate *
3495b 2f 0a 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e  /..    pCursor->
3495c 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
3495d 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20 72 63 20  kenizer;.    rc 
3495e 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74  = pModule->xNext
3495f 28 70 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65  (pCursor, &zToke
34960 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74  n, &nToken, &iSt
34961 61 72 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f  art, &iEnd, &iPo
34962 73 69 74 69 6f 6e 29 3b 0a 0a 20 20 20 20 69 66  sition);..    if
34963 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
34964 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d  ){.      nByte =
34965 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72   sizeof(Fts3Expr
34966 29 20 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 50  ) + sizeof(Fts3P
34967 68 72 61 73 65 29 20 2b 20 6e 54 6f 6b 65 6e 3b  hrase) + nToken;
34968 0a 20 20 20 20 20 20 70 52 65 74 20 3d 20 28 46  .      pRet = (F
34969 74 73 33 45 78 70 72 20 2a 29 73 71 6c 69 74 65  ts3Expr *)sqlite
3496a 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b  3_malloc(nByte);
3496b 0a 20 20 20 20 20 20 69 66 28 20 21 70 52 65 74  .      if( !pRet
3496c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
3496d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3496e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3496f 20 20 20 20 6d 65 6d 73 65 74 28 70 52 65 74 2c      memset(pRet,
34970 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20   0, nByte);.    
34971 20 20 20 20 70 52 65 74 2d 3e 65 54 79 70 65 20      pRet->eType 
34972 3d 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  = FTSQUERY_PHRAS
34973 45 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  E;.        pRet-
34974 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74 73 33  >pPhrase = (Fts3
34975 50 68 72 61 73 65 20 2a 29 26 70 52 65 74 5b 31  Phrase *)&pRet[1
34976 5d 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  ];.        pRet-
34977 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
34978 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 52   = 1;.        pR
34979 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f  et->pPhrase->iCo
3497a 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lumn = iCol;.   
3497b 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61       pRet->pPhra
3497c 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 6e 20  se->aToken[0].n 
3497d 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  = nToken;.      
3497e 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d    pRet->pPhrase-
3497f 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a 20 3d 20 28  >aToken[0].z = (
34980 63 68 61 72 20 2a 29 26 70 52 65 74 2d 3e 70 50  char *)&pRet->pP
34981 68 72 61 73 65 5b 31 5d 3b 0a 20 20 20 20 20 20  hrase[1];.      
34982 20 20 6d 65 6d 63 70 79 28 70 52 65 74 2d 3e 70    memcpy(pRet->p
34983 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30  Phrase->aToken[0
34984 5d 2e 7a 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f  ].z, zToken, nTo
34985 6b 65 6e 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  ken);..        i
34986 66 28 20 69 45 6e 64 3c 6e 20 26 26 20 7a 5b 69  f( iEnd<n && z[i
34987 45 6e 64 5d 3d 3d 27 2a 27 20 29 7b 0a 20 20 20  End]=='*' ){.   
34988 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
34989 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e  rase->aToken[0].
3498a 69 73 50 72 65 66 69 78 20 3d 20 31 3b 0a 20 20  isPrefix = 1;.  
3498b 20 20 20 20 20 20 20 20 69 45 6e 64 2b 2b 3b 0a          iEnd++;.
3498c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3498d 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 66    if( !sqlite3_f
3498e 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
3498f 74 68 65 73 65 73 20 26 26 20 69 53 74 61 72 74  theses && iStart
34990 3e 30 20 26 26 20 7a 5b 69 53 74 61 72 74 2d 31  >0 && z[iStart-1
34991 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20  ]=='-' ){.      
34992 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73      pRet->pPhras
34993 65 2d 3e 69 73 4e 6f 74 20 3d 20 31 3b 0a 20 20  e->isNot = 1;.  
34994 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
34995 20 20 20 20 20 20 6e 43 6f 6e 73 75 6d 65 64 20        nConsumed 
34996 3d 20 69 45 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20  = iEnd;.    }.. 
34997 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
34998 73 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  se(pCursor);.  }
34999 0a 20 20 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65  .  .  *pnConsume
3499a 64 20 3d 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a 20  d = nConsumed;. 
3499b 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b   *ppExpr = pRet;
3499c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3499d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 6c 61 72 67 65 20  ../*.** Enlarge 
3499e 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
3499f 69 6f 6e 2e 20 20 49 66 20 61 6e 20 6f 75 74 2d  ion.  If an out-
349a0 6f 66 2d 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  of-memory alloca
349a1 74 69 6f 6e 20 6f 63 63 75 72 73 2c 0a 2a 2a 20  tion occurs,.** 
349a2 74 68 65 6e 20 66 72 65 65 20 74 68 65 20 6f 6c  then free the ol
349a3 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f  d allocation..*/
349a4 0a 76 6f 69 64 20 2a 66 74 73 33 52 65 61 6c 6c  .void *fts3Reall
349a5 6f 63 4f 72 46 72 65 65 28 76 6f 69 64 20 2a 70  ocOrFree(void *p
349a6 4f 72 69 67 2c 20 69 6e 74 20 6e 4e 65 77 29 7b  Orig, int nNew){
349a7 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 20 3d 20  .  void *pRet = 
349a8 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
349a9 70 4f 72 69 67 2c 20 6e 4e 65 77 29 3b 0a 20 20  pOrig, nNew);.  
349aa 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20  if( !pRet ){.   
349ab 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f   sqlite3_free(pO
349ac 72 69 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rig);.  }.  retu
349ad 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pRet;.}../*.*
349ae 2a 20 42 75 66 66 65 72 20 7a 49 6e 70 75 74 2c  * Buffer zInput,
349af 20 6c 65 6e 67 74 68 20 6e 49 6e 70 75 74 2c 20   length nInput, 
349b0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e  contains the con
349b1 74 65 6e 74 73 20 6f 66 20 61 20 71 75 6f 74 65  tents of a quote
349b2 64 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74  d string.** that
349b3 20 61 70 70 65 61 72 65 64 20 61 73 20 70 61 72   appeared as par
349b4 74 20 6f 66 20 61 6e 20 66 74 73 33 20 71 75 65  t of an fts3 que
349b5 72 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  ry expression. N
349b6 65 69 74 68 65 72 20 71 75 6f 74 65 20 63 68 61  either quote cha
349b7 72 61 63 74 65 72 0a 2a 2a 20 69 73 20 69 6e 63  racter.** is inc
349b8 6c 75 64 65 64 20 69 6e 20 74 68 65 20 62 75 66  luded in the buf
349b9 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69  fer. This functi
349ba 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 74  on attempts to t
349bb 6f 6b 65 6e 69 7a 65 20 74 68 65 20 65 6e 74 69  okenize the enti
349bc 72 65 0a 2a 2a 20 69 6e 70 75 74 20 62 75 66 66  re.** input buff
349bd 65 72 20 61 6e 64 20 63 72 65 61 74 65 20 61 6e  er and create an
349be 20 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74   Fts3Expr struct
349bf 75 72 65 20 6f 66 20 74 79 70 65 20 46 54 53 51  ure of type FTSQ
349c0 55 45 52 59 5f 50 48 52 41 53 45 20 0a 2a 2a 20  UERY_PHRASE .** 
349c1 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
349c2 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  esults..**.** If
349c3 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
349c4 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
349c5 65 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73  ed and *ppExpr s
349c6 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  et to point at t
349c7 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  he.** allocated 
349c8 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
349c9 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 65  re. Otherwise, e
349ca 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ither SQLITE_NOM
349cb 45 4d 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  EM (out of memor
349cc 79 0a 2a 2a 20 65 72 72 6f 72 29 20 6f 72 20 53  y.** error) or S
349cd 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 74 6f 6b  QLITE_ERROR (tok
349ce 65 6e 69 7a 61 74 69 6f 6e 20 65 72 72 6f 72 29  enization error)
349cf 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
349d0 20 2a 70 70 45 78 70 72 20 73 65 74 0a 2a 2a 20   *ppExpr set.** 
349d1 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  to 0..*/.static 
349d2 69 6e 74 20 67 65 74 4e 65 78 74 53 74 72 69 6e  int getNextStrin
349d3 67 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  g(.  ParseContex
349d4 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
349d5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
349d6 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
349d7 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
349d8 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
349d9 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20  , int nInput,   
349da 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73        /* Input s
349db 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45  tring */.  Fts3E
349dc 78 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20  xpr **ppExpr    
349dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
349de 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65     /* OUT: expre
349df 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71  ssion */.){.  sq
349e0 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
349e1 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50  *pTokenizer = pP
349e2 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  arse->pTokenizer
349e3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ;.  sqlite3_toke
349e4 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
349e5 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54  st *pModule = pT
349e6 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
349e7 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46  e;.  int rc;.  F
349e8 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  ts3Expr *p = 0;.
349e9 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
349ea 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
349eb 73 6f 72 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  sor = 0;.  char 
349ec 2a 7a 54 65 6d 70 20 3d 20 30 3b 0a 20 20 69 6e  *zTemp = 0;.  in
349ed 74 20 6e 54 65 6d 70 20 3d 20 30 3b 0a 0a 20 20  t nTemp = 0;..  
349ee 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f  rc = pModule->xO
349ef 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  pen(pTokenizer, 
349f0 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  zInput, nInput, 
349f1 26 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28  &pCursor);.  if(
349f2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
349f3 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20  {.    int ii;.  
349f4 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65    pCursor->pToke
349f5 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
349f6 65 72 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30  er;.    for(ii=0
349f7 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  ; rc==SQLITE_OK;
349f8 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f   ii++){.      co
349f9 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e  nst char *zToken
349fa 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b  ;.      int nTok
349fb 65 6e 2c 20 69 42 65 67 69 6e 2c 20 69 45 6e 64  en, iBegin, iEnd
349fc 2c 20 69 50 6f 73 3b 0a 20 20 20 20 20 20 72 63  , iPos;.      rc
349fd 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78   = pModule->xNex
349fe 74 28 70 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b  t(pCursor, &zTok
349ff 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 42  en, &nToken, &iB
34a00 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50  egin, &iEnd, &iP
34a01 6f 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  os);.      if( r
34a02 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
34a03 20 20 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74          int nByt
34a04 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73 33 45  e = sizeof(Fts3E
34a05 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28 46 74  xpr) + sizeof(Ft
34a06 73 33 50 68 72 61 73 65 29 3b 0a 20 20 20 20 20  s3Phrase);.     
34a07 20 20 20 70 20 3d 20 66 74 73 33 52 65 61 6c 6c     p = fts3Reall
34a08 6f 63 4f 72 46 72 65 65 28 70 2c 20 6e 42 79 74  ocOrFree(p, nByt
34a09 65 2b 69 69 2a 73 69 7a 65 6f 66 28 73 74 72 75  e+ii*sizeof(stru
34a0a 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e 29 29  ct PhraseToken))
34a0b 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 20  ;.        zTemp 
34a0c 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46  = fts3ReallocOrF
34a0d 72 65 65 28 7a 54 65 6d 70 2c 20 6e 54 65 6d 70  ree(zTemp, nTemp
34a0e 20 2b 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20   + nToken);.    
34a0f 20 20 20 20 69 66 28 20 21 70 20 7c 7c 20 21 7a      if( !p || !z
34a10 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20  Temp ){.        
34a11 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
34a12 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
34a13 20 69 66 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20   if( ii==0 ){.  
34a14 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
34a15 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  , 0, nByte);.   
34a16 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73         p->pPhras
34a17 65 20 3d 20 28 46 74 73 33 50 68 72 61 73 65 20  e = (Fts3Phrase 
34a18 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 20 20 20  *)&p[1];.       
34a19 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50   }.        p->pP
34a1a 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72  hrase = (Fts3Phr
34a1b 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  ase *)&p[1];.   
34a1c 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d       p->pPhrase-
34a1d 3e 6e 54 6f 6b 65 6e 20 3d 20 69 69 2b 31 3b 0a  >nToken = ii+1;.
34a1e 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61          p->pPhra
34a1f 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 2e 6e  se->aToken[ii].n
34a20 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20   = nToken;.     
34a21 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70     memcpy(&zTemp
34a22 5b 6e 54 65 6d 70 5d 2c 20 7a 54 6f 6b 65 6e 2c  [nTemp], zToken,
34a23 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20   nToken);.      
34a24 20 20 6e 54 65 6d 70 20 2b 3d 20 6e 54 6f 6b 65    nTemp += nToke
34a25 6e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  n;.        if( i
34a26 45 6e 64 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49  End<nInput && zI
34a27 6e 70 75 74 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20  nput[iEnd]=='*' 
34a28 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ){.          p->
34a29 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
34a2a 69 69 5d 2e 69 73 50 72 65 66 69 78 20 3d 20 31  ii].isPrefix = 1
34a2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
34a2c 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50  .          p->pP
34a2d 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69  hrase->aToken[ii
34a2e 5d 2e 69 73 50 72 65 66 69 78 20 3d 20 30 3b 0a  ].isPrefix = 0;.
34a2f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
34a30 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 6f  }.    }..    pMo
34a31 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75  dule->xClose(pCu
34a32 72 73 6f 72 29 3b 0a 20 20 20 20 70 43 75 72 73  rsor);.    pCurs
34a33 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69  or = 0;.  }..  i
34a34 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
34a35 4e 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 6a  NE ){.    int jj
34a36 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77  ;.    char *zNew
34a37 3b 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d  ;.    int nNew =
34a38 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74   0;.    int nByt
34a39 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73 33 45  e = sizeof(Fts3E
34a3a 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28 46 74  xpr) + sizeof(Ft
34a3b 73 33 50 68 72 61 73 65 29 3b 0a 20 20 20 20 6e  s3Phrase);.    n
34a3c 42 79 74 65 20 2b 3d 20 28 70 3f 28 70 2d 3e 70  Byte += (p?(p->p
34a3d 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 31  Phrase->nToken-1
34a3e 29 3a 30 29 20 2a 20 73 69 7a 65 6f 66 28 73 74  ):0) * sizeof(st
34a3f 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e  ruct PhraseToken
34a40 29 3b 0a 20 20 20 20 70 20 3d 20 66 74 73 33 52  );.    p = fts3R
34a41 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2c 20  eallocOrFree(p, 
34a42 6e 42 79 74 65 20 2b 20 6e 54 65 6d 70 29 3b 0a  nByte + nTemp);.
34a43 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20      if( !p ){.  
34a44 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
34a45 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a  .    }.    if( z
34a46 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 7a 4e  Temp ){.      zN
34a47 65 77 20 3d 20 26 28 28 28 63 68 61 72 20 2a 29  ew = &(((char *)
34a48 70 29 5b 6e 42 79 74 65 5d 29 3b 0a 20 20 20 20  p)[nByte]);.    
34a49 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a    memcpy(zNew, z
34a4a 54 65 6d 70 2c 20 6e 54 65 6d 70 29 3b 0a 20 20  Temp, nTemp);.  
34a4b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
34a4c 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74  emset(p, 0, nByt
34a4d 65 2b 6e 54 65 6d 70 29 3b 0a 20 20 20 20 7d 0a  e+nTemp);.    }.
34a4e 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 20 3d      p->pPhrase =
34a4f 20 28 46 74 73 33 50 68 72 61 73 65 20 2a 29 26   (Fts3Phrase *)&
34a50 70 5b 31 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 6a  p[1];.    for(jj
34a51 3d 30 3b 20 6a 6a 3c 70 2d 3e 70 50 68 72 61 73  =0; jj<p->pPhras
34a52 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 6a 6a 2b 2b 29  e->nToken; jj++)
34a53 7b 0a 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61  {.      p->pPhra
34a54 73 65 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 7a  se->aToken[jj].z
34a55 20 3d 20 26 7a 4e 65 77 5b 6e 4e 65 77 5d 3b 0a   = &zNew[nNew];.
34a56 20 20 20 20 20 20 6e 4e 65 77 20 2b 3d 20 70 2d        nNew += p-
34a57 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  >pPhrase->aToken
34a58 5b 6a 6a 5d 2e 6e 3b 0a 20 20 20 20 7d 0a 20 20  [jj].n;.    }.  
34a59 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
34a5a 54 65 6d 70 29 3b 0a 20 20 20 20 70 2d 3e 65 54  Temp);.    p->eT
34a5b 79 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f 50  ype = FTSQUERY_P
34a5c 48 52 41 53 45 3b 0a 20 20 20 20 70 2d 3e 70 50  HRASE;.    p->pP
34a5d 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  hrase->iColumn =
34a5e 20 70 50 61 72 73 65 2d 3e 69 44 65 66 61 75 6c   pParse->iDefaul
34a5f 74 43 6f 6c 3b 0a 20 20 20 20 72 63 20 3d 20 53  tCol;.    rc = S
34a60 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
34a61 20 2a 70 70 45 78 70 72 20 3d 20 70 3b 0a 20 20   *ppExpr = p;.  
34a62 72 65 74 75 72 6e 20 72 63 3b 0a 6e 6f 5f 6d 65  return rc;.no_me
34a63 6d 3a 0a 0a 20 20 69 66 28 20 70 43 75 72 73 6f  m:..  if( pCurso
34a64 72 20 29 7b 0a 20 20 20 20 70 4d 6f 64 75 6c 65  r ){.    pModule
34a65 2d 3e 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72  ->xClose(pCursor
34a66 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
34a67 5f 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20  _free(zTemp);.  
34a68 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
34a69 0a 20 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a  .  *ppExpr = 0;.
34a6a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
34a6b 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  NOMEM;.}../*.** 
34a6c 46 75 6e 63 74 69 6f 6e 20 67 65 74 4e 65 78 74  Function getNext
34a6d 4e 6f 64 65 28 29 2c 20 77 68 69 63 68 20 69 73  Node(), which is
34a6e 20 63 61 6c 6c 65 64 20 62 79 20 66 74 73 33 45   called by fts3E
34a6f 78 70 72 50 61 72 73 65 28 29 2c 20 6d 61 79 20  xprParse(), may 
34a70 69 74 73 65 6c 66 0a 2a 2a 20 63 61 6c 6c 20 66  itself.** call f
34a71 74 73 33 45 78 70 72 50 61 72 73 65 28 29 2e 20  ts3ExprParse(). 
34a72 53 6f 20 74 68 69 73 20 66 6f 72 77 61 72 64 20  So this forward 
34a73 64 65 63 6c 61 72 61 74 69 6f 6e 20 69 73 20 72  declaration is r
34a74 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  equired..*/.stat
34a75 69 63 20 69 6e 74 20 66 74 73 33 45 78 70 72 50  ic int fts3ExprP
34a76 61 72 73 65 28 50 61 72 73 65 43 6f 6e 74 65 78  arse(ParseContex
34a77 74 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  t *, const char 
34a78 2a 2c 20 69 6e 74 2c 20 46 74 73 33 45 78 70 72  *, int, Fts3Expr
34a79 20 2a 2a 2c 20 69 6e 74 20 2a 29 3b 0a 0a 2f 2a   **, int *);../*
34a7a 0a 2a 2a 20 54 68 65 20 6f 75 74 70 75 74 20 76  .** The output v
34a7b 61 72 69 61 62 6c 65 20 2a 70 70 45 78 70 72 20  ariable *ppExpr 
34a7c 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  is populated wit
34a7d 68 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 46  h an allocated F
34a7e 74 73 33 45 78 70 72 20 0a 2a 2a 20 73 74 72 75  ts3Expr .** stru
34a7f 63 74 75 72 65 2c 20 6f 72 20 73 65 74 20 74 6f  cture, or set to
34a80 20 30 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66   0 if the end of
34a81 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65   the input buffe
34a82 72 20 69 73 20 72 65 61 63 68 65 64 2e 0a 2a 2a  r is reached..**
34a83 0a 2a 2a 20 52 65 74 75 72 6e 73 20 61 6e 20 53  .** Returns an S
34a84 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
34a85 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65  . SQLITE_OK if e
34a86 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2c  verything works,
34a87 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a   SQLITE_NOMEM.**
34a88 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   if a malloc fai
34a89 6c 75 72 65 20 6f 63 63 75 72 73 2c 20 6f 72 20  lure occurs, or 
34a8a 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20  SQLITE_ERROR if 
34a8b 61 20 70 61 72 73 65 20 65 72 72 6f 72 20 69 73  a parse error is
34a8c 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a   encountered..**
34a8d 20 49 66 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   If SQLITE_ERROR
34a8e 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 70 43   is returned, pC
34a8f 6f 6e 74 65 78 74 20 69 73 20 70 6f 70 75 6c 61  ontext is popula
34a90 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ted with an erro
34a91 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73 74  r message..*/.st
34a92 61 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74  atic int getNext
34a93 4e 6f 64 65 28 0a 20 20 50 61 72 73 65 43 6f 6e  Node(.  ParseCon
34a94 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20 20  text *pParse,   
34a95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34a96 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20 70 61  /* fts3 query pa
34a97 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
34a98 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
34a99 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20  int n,          
34a9a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
34a9b 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74  t string */.  Ft
34a9c 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c  s3Expr **ppExpr,
34a9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34a9e 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78        /* OUT: ex
34a9f 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e  pression */.  in
34aa0 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20  t *pnConsumed   
34aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34aa2 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
34aa3 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f  mber of bytes co
34aa4 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 73  nsumed */.){.  s
34aa5 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
34aa6 63 74 20 46 74 73 33 4b 65 79 77 6f 72 64 20 7b  ct Fts3Keyword {
34aa7 0a 20 20 20 20 63 68 61 72 20 7a 5b 34 5d 3b 20  .    char z[4]; 
34aa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34aa9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65             /* Ke
34aaa 79 77 6f 72 64 20 74 65 78 74 20 2a 2f 0a 20 20  yword text */.  
34aab 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
34aac 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
34aad 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
34aae 68 20 6f 66 20 74 68 65 20 6b 65 79 77 6f 72 64  h of the keyword
34aaf 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64   */.    unsigned
34ab0 20 63 68 61 72 20 70 61 72 65 6e 4f 6e 6c 79 3b   char parenOnly;
34ab1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34ab2 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 6e 20 70   Only valid in p
34ab3 61 72 65 6e 20 6d 6f 64 65 20 2a 2f 0a 20 20 20  aren mode */.   
34ab4 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65   unsigned char e
34ab5 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  Type;           
34ab6 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72         /* Keywor
34ab7 64 20 63 6f 64 65 20 2a 2f 0a 20 20 7d 20 61 4b  d code */.  } aK
34ab8 65 79 77 6f 72 64 5b 5d 20 3d 20 7b 0a 20 20 20  eyword[] = {.   
34ab9 20 7b 20 22 4f 52 22 20 2c 20 20 32 2c 20 30 2c   { "OR" ,  2, 0,
34aba 20 46 54 53 51 55 45 52 59 5f 4f 52 20 20 20 7d   FTSQUERY_OR   }
34abb 2c 0a 20 20 20 20 7b 20 22 41 4e 44 22 2c 20 20  ,.    { "AND",  
34abc 33 2c 20 31 2c 20 46 54 53 51 55 45 52 59 5f 41  3, 1, FTSQUERY_A
34abd 4e 44 20 20 7d 2c 0a 20 20 20 20 7b 20 22 4e 4f  ND  },.    { "NO
34abe 54 22 2c 20 20 33 2c 20 31 2c 20 46 54 53 51 55  T",  3, 1, FTSQU
34abf 45 52 59 5f 4e 4f 54 20 20 7d 2c 0a 20 20 20 20  ERY_NOT  },.    
34ac0 7b 20 22 4e 45 41 52 22 2c 20 34 2c 20 30 2c 20  { "NEAR", 4, 0, 
34ac1 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 7d 0a  FTSQUERY_NEAR }.
34ac2 20 20 7d 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20    };.  int ii;. 
34ac3 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74   int iCol;.  int
34ac4 20 69 43 6f 6c 4c 65 6e 3b 0a 20 20 69 6e 74 20   iColLen;.  int 
34ac5 72 63 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a  rc;.  Fts3Expr *
34ac6 70 52 65 74 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e  pRet = 0;..  con
34ac7 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 20  st char *zInput 
34ac8 3d 20 7a 3b 0a 20 20 69 6e 74 20 6e 49 6e 70 75  = z;.  int nInpu
34ac9 74 20 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 53 6b 69  t = n;..  /* Ski
34aca 70 20 6f 76 65 72 20 61 6e 79 20 77 68 69 74 65  p over any white
34acb 73 70 61 63 65 20 62 65 66 6f 72 65 20 63 68 65  space before che
34acc 63 6b 69 6e 67 20 66 6f 72 20 61 20 6b 65 79 77  cking for a keyw
34acd 6f 72 64 2c 20 61 6e 20 6f 70 65 6e 20 6f 72 0a  ord, an open or.
34ace 20 20 2a 2a 20 63 6c 6f 73 65 20 62 72 61 63 6b    ** close brack
34acf 65 74 2c 20 6f 72 20 61 20 71 75 6f 74 65 64 20  et, or a quoted 
34ad0 73 74 72 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20  string. .  */.  
34ad1 77 68 69 6c 65 28 20 6e 49 6e 70 75 74 3e 30 20  while( nInput>0 
34ad2 26 26 20 66 74 73 33 69 73 73 70 61 63 65 28 2a  && fts3isspace(*
34ad3 7a 49 6e 70 75 74 29 20 29 7b 0a 20 20 20 20 6e  zInput) ){.    n
34ad4 49 6e 70 75 74 2d 2d 3b 0a 20 20 20 20 7a 49 6e  Input--;.    zIn
34ad5 70 75 74 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28  put++;.  }.  if(
34ad6 20 6e 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20   nInput==0 ){.  
34ad7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
34ad8 44 4f 4e 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  DONE;.  }..  /* 
34ad9 53 65 65 20 69 66 20 77 65 20 61 72 65 20 64 65  See if we are de
34ada 61 6c 69 6e 67 20 77 69 74 68 20 61 20 6b 65 79  aling with a key
34adb 77 6f 72 64 2e 20 2a 2f 0a 20 20 66 6f 72 28 69  word. */.  for(i
34adc 69 3d 30 3b 20 69 69 3c 28 69 6e 74 29 28 73 69  i=0; ii<(int)(si
34add 7a 65 6f 66 28 61 4b 65 79 77 6f 72 64 29 2f 73  zeof(aKeyword)/s
34ade 69 7a 65 6f 66 28 73 74 72 75 63 74 20 46 74 73  izeof(struct Fts
34adf 33 4b 65 79 77 6f 72 64 29 29 3b 20 69 69 2b 2b  3Keyword)); ii++
34ae0 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72  ){.    const str
34ae1 75 63 74 20 46 74 73 33 4b 65 79 77 6f 72 64 20  uct Fts3Keyword 
34ae2 2a 70 4b 65 79 20 3d 20 26 61 4b 65 79 77 6f 72  *pKey = &aKeywor
34ae3 64 5b 69 69 5d 3b 0a 0a 20 20 20 20 69 66 28 20  d[ii];..    if( 
34ae4 28 70 4b 65 79 2d 3e 70 61 72 65 6e 4f 6e 6c 79  (pKey->parenOnly
34ae5 20 26 20 7e 73 71 6c 69 74 65 33 5f 66 74 73 33   & ~sqlite3_fts3
34ae6 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65  _enable_parenthe
34ae7 73 65 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ses)!=0 ){.     
34ae8 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
34ae9 0a 0a 20 20 20 20 69 66 28 20 6e 49 6e 70 75 74  ..    if( nInput
34aea 3e 3d 70 4b 65 79 2d 3e 6e 20 26 26 20 30 3d 3d  >=pKey->n && 0==
34aeb 6d 65 6d 63 6d 70 28 7a 49 6e 70 75 74 2c 20 70  memcmp(zInput, p
34aec 4b 65 79 2d 3e 7a 2c 20 70 4b 65 79 2d 3e 6e 29  Key->z, pKey->n)
34aed 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e   ){.      int nN
34aee 65 61 72 20 3d 20 53 51 4c 49 54 45 5f 46 54 53  ear = SQLITE_FTS
34aef 33 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50  3_DEFAULT_NEAR_P
34af0 41 52 41 4d 3b 0a 20 20 20 20 20 20 69 6e 74 20  ARAM;.      int 
34af1 6e 4b 65 79 20 3d 20 70 4b 65 79 2d 3e 6e 3b 0a  nKey = pKey->n;.
34af2 20 20 20 20 20 20 63 68 61 72 20 63 4e 65 78 74        char cNext
34af3 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ;..      /* If t
34af4 68 69 73 20 69 73 20 61 20 22 4e 45 41 52 22 20  his is a "NEAR" 
34af5 6b 65 79 77 6f 72 64 2c 20 63 68 65 63 6b 20 66  keyword, check f
34af6 6f 72 20 61 6e 20 65 78 70 6c 69 63 69 74 20 6e  or an explicit n
34af7 65 61 72 6e 65 73 73 2e 20 2a 2f 0a 20 20 20 20  earness. */.    
34af8 20 20 69 66 28 20 70 4b 65 79 2d 3e 65 54 79 70    if( pKey->eTyp
34af9 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
34afa 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
34afb 72 74 28 20 6e 4b 65 79 3d 3d 34 20 29 3b 0a 20  rt( nKey==4 );. 
34afc 20 20 20 20 20 20 20 69 66 28 20 7a 49 6e 70 75         if( zInpu
34afd 74 5b 34 5d 3d 3d 27 2f 27 20 26 26 20 7a 49 6e  t[4]=='/' && zIn
34afe 70 75 74 5b 35 5d 3e 3d 27 30 27 20 26 26 20 7a  put[5]>='0' && z
34aff 49 6e 70 75 74 5b 35 5d 3c 3d 27 39 27 20 29 7b  Input[5]<='9' ){
34b00 0a 20 20 20 20 20 20 20 20 20 20 6e 4e 65 61 72  .          nNear
34b01 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
34b02 66 6f 72 28 6e 4b 65 79 3d 35 3b 20 7a 49 6e 70  for(nKey=5; zInp
34b03 75 74 5b 6e 4b 65 79 5d 3e 3d 27 30 27 20 26 26  ut[nKey]>='0' &&
34b04 20 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3c 3d 27   zInput[nKey]<='
34b05 39 27 3b 20 6e 4b 65 79 2b 2b 29 7b 0a 20 20 20  9'; nKey++){.   
34b06 20 20 20 20 20 20 20 20 20 6e 4e 65 61 72 20 3d           nNear =
34b07 20 6e 4e 65 61 72 20 2a 20 31 30 20 2b 20 28 7a   nNear * 10 + (z
34b08 49 6e 70 75 74 5b 6e 4b 65 79 5d 20 2d 20 27 30  Input[nKey] - '0
34b09 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ');.          }.
34b0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
34b0b 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74 20 74  }..      /* At t
34b0c 68 69 73 20 70 6f 69 6e 74 20 74 68 69 73 20 69  his point this i
34b0d 73 20 70 72 6f 62 61 62 6c 79 20 61 20 6b 65 79  s probably a key
34b0e 77 6f 72 64 2e 20 42 75 74 20 66 6f 72 20 74 68  word. But for th
34b0f 61 74 20 74 6f 20 62 65 20 74 72 75 65 2c 0a 20  at to be true,. 
34b10 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78 74       ** the next
34b11 20 62 79 74 65 20 6d 75 73 74 20 63 6f 6e 74 61   byte must conta
34b12 69 6e 20 65 69 74 68 65 72 20 77 68 69 74 65 73  in either whites
34b13 70 61 63 65 2c 20 61 6e 20 6f 70 65 6e 20 6f 72  pace, an open or
34b14 20 63 6c 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20   close.      ** 
34b15 70 61 72 65 6e 74 68 65 73 69 73 2c 20 61 20 71  parenthesis, a q
34b16 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c 20  uote character, 
34b17 6f 72 20 45 4f 46 2e 20 0a 20 20 20 20 20 20 2a  or EOF. .      *
34b18 2f 0a 20 20 20 20 20 20 63 4e 65 78 74 20 3d 20  /.      cNext = 
34b19 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3b 0a 20 20  zInput[nKey];.  
34b1a 20 20 20 20 69 66 28 20 66 74 73 33 69 73 73 70      if( fts3issp
34b1b 61 63 65 28 63 4e 65 78 74 29 20 0a 20 20 20 20  ace(cNext) .    
34b1c 20 20 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 22 27     || cNext=='"'
34b1d 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 28 27 20 7c   || cNext=='(' |
34b1e 7c 20 63 4e 65 78 74 3d 3d 27 29 27 20 7c 7c 20  | cNext==')' || 
34b1f 63 4e 65 78 74 3d 3d 30 0a 20 20 20 20 20 20 29  cNext==0.      )
34b20 7b 0a 20 20 20 20 20 20 20 20 70 52 65 74 20 3d  {.        pRet =
34b21 20 28 46 74 73 33 45 78 70 72 20 2a 29 73 71 6c   (Fts3Expr *)sql
34b22 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
34b23 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
34b24 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 52         memset(pR
34b25 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74  et, 0, sizeof(Ft
34b26 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20  s3Expr));.      
34b27 20 20 70 52 65 74 2d 3e 65 54 79 70 65 20 3d 20    pRet->eType = 
34b28 70 4b 65 79 2d 3e 65 54 79 70 65 3b 0a 20 20 20  pKey->eType;.   
34b29 20 20 20 20 20 70 52 65 74 2d 3e 6e 4e 65 61 72       pRet->nNear
34b2a 20 3d 20 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20   = nNear;.      
34b2b 20 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74    *ppExpr = pRet
34b2c 3b 0a 20 20 20 20 20 20 20 20 2a 70 6e 43 6f 6e  ;.        *pnCon
34b2d 73 75 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20  sumed = (zInput 
34b2e 2d 20 7a 29 20 2b 20 6e 4b 65 79 3b 0a 20 20 20  - z) + nKey;.   
34b2f 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
34b30 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 0a  TE_OK;.      }..
34b31 20 20 20 20 20 20 2f 2a 20 54 75 72 6e 73 20 6f        /* Turns o
34b32 75 74 20 74 68 61 74 20 77 61 73 6e 27 74 20 61  ut that wasn't a
34b33 20 6b 65 79 77 6f 72 64 20 61 66 74 65 72 20 61   keyword after a
34b34 6c 6c 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73  ll. This happens
34b35 20 69 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a   if the.      **
34b36 20 75 73 65 72 20 68 61 73 20 73 75 70 70 6c 69   user has suppli
34b37 65 64 20 61 20 74 6f 6b 65 6e 20 73 75 63 68 20  ed a token such 
34b38 61 73 20 22 4f 52 61 63 6c 65 22 2e 20 43 6f 6e  as "ORacle". Con
34b39 74 69 6e 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  tinue..      */.
34b3a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
34b3b 43 68 65 63 6b 20 66 6f 72 20 61 6e 20 6f 70 65  Check for an ope
34b3c 6e 20 62 72 61 63 6b 65 74 2e 20 2a 2f 0a 20 20  n bracket. */.  
34b3d 69 66 28 20 73 71 6c 69 74 65 33 5f 66 74 73 33  if( sqlite3_fts3
34b3e 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65  _enable_parenthe
34b3f 73 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20 2a  ses ){.    if( *
34b40 7a 49 6e 70 75 74 3d 3d 27 28 27 20 29 7b 0a 20  zInput=='(' ){. 
34b41 20 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 75 6d       int nConsum
34b42 65 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 63  ed;.      int rc
34b43 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
34b44 6e 4e 65 73 74 2b 2b 3b 0a 20 20 20 20 20 20 72  nNest++;.      r
34b45 63 20 3d 20 66 74 73 33 45 78 70 72 50 61 72 73  c = fts3ExprPars
34b46 65 28 70 50 61 72 73 65 2c 20 26 7a 49 6e 70 75  e(pParse, &zInpu
34b47 74 5b 31 5d 2c 20 6e 49 6e 70 75 74 2d 31 2c 20  t[1], nInput-1, 
34b48 70 70 45 78 70 72 2c 20 26 6e 43 6f 6e 73 75 6d  ppExpr, &nConsum
34b49 65 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ed);.      if( r
34b4a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
34b4b 21 2a 70 70 45 78 70 72 20 29 7b 0a 20 20 20 20  !*ppExpr ){.    
34b4c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
34b4d 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  DONE;.      }.  
34b4e 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20      *pnConsumed 
34b4f 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b  = (zInput - z) +
34b50 20 31 20 2b 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a   1 + nConsumed;.
34b51 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
34b52 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
34b53 20 43 68 65 63 6b 20 66 6f 72 20 61 20 63 6c 6f   Check for a clo
34b54 73 65 20 62 72 61 63 6b 65 74 2e 20 2a 2f 0a 20  se bracket. */. 
34b55 20 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d     if( *zInput==
34b56 27 29 27 20 29 7b 0a 20 20 20 20 20 20 70 50 61  ')' ){.      pPa
34b57 72 73 65 2d 3e 6e 4e 65 73 74 2d 2d 3b 0a 20 20  rse->nNest--;.  
34b58 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20      *pnConsumed 
34b59 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b  = (zInput - z) +
34b5a 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
34b5b 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
34b5c 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65    }.  }..  /* Se
34b5d 65 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c  e if we are deal
34b5e 69 6e 67 20 77 69 74 68 20 61 20 71 75 6f 74 65  ing with a quote
34b5f 64 20 70 68 72 61 73 65 2e 20 49 66 20 74 68 69  d phrase. If thi
34b60 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  s is the case, t
34b61 68 65 6e 0a 20 20 2a 2a 20 73 65 61 72 63 68 20  hen.  ** search 
34b62 66 6f 72 20 74 68 65 20 63 6c 6f 73 69 6e 67 20  for the closing 
34b63 71 75 6f 74 65 20 61 6e 64 20 70 61 73 73 20 74  quote and pass t
34b64 68 65 20 77 68 6f 6c 65 20 73 74 72 69 6e 67 20  he whole string 
34b65 74 6f 20 67 65 74 4e 65 78 74 53 74 72 69 6e 67  to getNextString
34b66 28 29 0a 20 20 2a 2a 20 66 6f 72 20 70 72 6f 63  ().  ** for proc
34b67 65 73 73 69 6e 67 2e 20 54 68 69 73 20 69 73 20  essing. This is 
34b68 65 61 73 79 20 74 6f 20 64 6f 2c 20 61 73 20 66  easy to do, as f
34b69 74 73 33 20 68 61 73 20 6e 6f 20 73 79 6e 74 61  ts3 has no synta
34b6a 78 20 66 6f 72 20 65 73 63 61 70 69 6e 67 0a 20  x for escaping. 
34b6b 20 2a 2a 20 61 20 71 75 6f 74 65 20 63 68 61 72   ** a quote char
34b6c 61 63 74 65 72 20 65 6d 62 65 64 64 65 64 20 69  acter embedded i
34b6d 6e 20 61 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f  n a string..  */
34b6e 0a 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d  .  if( *zInput==
34b6f 27 22 27 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  '"' ){.    for(i
34b70 69 3d 31 3b 20 69 69 3c 6e 49 6e 70 75 74 20 26  i=1; ii<nInput &
34b71 26 20 7a 49 6e 70 75 74 5b 69 69 5d 21 3d 27 22  & zInput[ii]!='"
34b72 27 3b 20 69 69 2b 2b 29 3b 0a 20 20 20 20 2a 70  '; ii++);.    *p
34b73 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e  nConsumed = (zIn
34b74 70 75 74 20 2d 20 7a 29 20 2b 20 69 69 20 2b 20  put - z) + ii + 
34b75 31 3b 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 6e  1;.    if( ii==n
34b76 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 20 20 72  Input ){.      r
34b77 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
34b78 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  OR;.    }.    re
34b79 74 75 72 6e 20 67 65 74 4e 65 78 74 53 74 72 69  turn getNextStri
34b7a 6e 67 28 70 50 61 72 73 65 2c 20 26 7a 49 6e 70  ng(pParse, &zInp
34b7b 75 74 5b 31 5d 2c 20 69 69 2d 31 2c 20 70 70 45  ut[1], ii-1, ppE
34b7c 78 70 72 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  xpr);.  }...  /*
34b7d 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77   If control flow
34b7e 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c  s to this point,
34b7f 20 74 68 69 73 20 6d 75 73 74 20 62 65 20 61 20   this must be a 
34b80 72 65 67 75 6c 61 72 20 74 6f 6b 65 6e 2c 20 6f  regular token, o
34b81 72 20 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 20  r .  ** the end 
34b82 6f 66 20 74 68 65 20 69 6e 70 75 74 2e 20 52 65  of the input. Re
34b83 61 64 20 61 20 72 65 67 75 6c 61 72 20 74 6f 6b  ad a regular tok
34b84 65 6e 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  en using the sql
34b85 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 20  ite3_tokenizer. 
34b86 20 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 42   ** interface. B
34b87 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20  efore doing so, 
34b88 66 69 67 75 72 65 20 6f 75 74 20 69 66 20 74 68  figure out if th
34b89 65 72 65 20 69 73 20 61 6e 20 65 78 70 6c 69 63  ere is an explic
34b8a 69 74 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73  it.  ** column s
34b8b 70 65 63 69 66 69 65 72 20 66 6f 72 20 74 68 65  pecifier for the
34b8c 20 74 6f 6b 65 6e 2e 20 0a 20 20 2a 2a 0a 20 20   token. .  **.  
34b8d 2a 2a 20 54 4f 44 4f 3a 20 53 74 72 61 6e 67 65  ** TODO: Strange
34b8e 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f  ly, it is not po
34b8f 73 73 69 62 6c 65 20 74 6f 20 61 73 73 6f 63 69  ssible to associ
34b90 61 74 65 20 61 20 63 6f 6c 75 6d 6e 20 73 70 65  ate a column spe
34b91 63 69 66 69 65 72 0a 20 20 2a 2a 20 77 69 74 68  cifier.  ** with
34b92 20 61 20 71 75 6f 74 65 64 20 70 68 72 61 73 65   a quoted phrase
34b93 2c 20 6f 6e 6c 79 20 77 69 74 68 20 61 20 73 69  , only with a si
34b94 6e 67 6c 65 20 74 6f 6b 65 6e 2e 20 4e 6f 74 20  ngle token. Not 
34b95 73 75 72 65 20 69 66 20 74 68 69 73 20 77 61 73  sure if this was
34b96 0a 20 20 2a 2a 20 61 6e 20 69 6d 70 6c 65 6d 65  .  ** an impleme
34b97 6e 74 61 74 69 6f 6e 20 61 72 74 69 66 61 63 74  ntation artifact
34b98 20 6f 72 20 61 6e 20 69 6e 74 65 6e 74 69 6f 6e   or an intention
34b99 61 6c 20 64 65 63 69 73 69 6f 6e 20 77 68 65 6e  al decision when
34b9a 20 66 74 73 33 20 77 61 73 0a 20 20 2a 2a 20 66   fts3 was.  ** f
34b9b 69 72 73 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64  irst implemented
34b9c 2e 20 57 68 69 63 68 65 76 65 72 20 69 74 20 77  . Whichever it w
34b9d 61 73 2c 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  as, this module 
34b9e 64 75 70 6c 69 63 61 74 65 73 20 74 68 65 20 0a  duplicates the .
34b9f 20 20 2a 2a 20 6c 69 6d 69 74 61 74 69 6f 6e 2e    ** limitation.
34ba0 0a 20 20 2a 2f 0a 20 20 69 43 6f 6c 20 3d 20 70  .  */.  iCol = p
34ba1 50 61 72 73 65 2d 3e 69 44 65 66 61 75 6c 74 43  Parse->iDefaultC
34ba2 6f 6c 3b 0a 20 20 69 43 6f 6c 4c 65 6e 20 3d 20  ol;.  iColLen = 
34ba3 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  0;.  for(ii=0; i
34ba4 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 3b 20  i<pParse->nCol; 
34ba5 69 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ii++){.    const
34ba6 20 63 68 61 72 20 2a 7a 53 74 72 20 3d 20 70 50   char *zStr = pP
34ba7 61 72 73 65 2d 3e 61 7a 43 6f 6c 5b 69 69 5d 3b  arse->azCol[ii];
34ba8 0a 20 20 20 20 69 6e 74 20 6e 53 74 72 20 3d 20  .    int nStr = 
34ba9 73 74 72 6c 65 6e 28 7a 53 74 72 29 3b 0a 20 20  strlen(zStr);.  
34baa 20 20 69 66 28 20 6e 49 6e 70 75 74 3e 6e 53 74    if( nInput>nSt
34bab 72 20 26 26 20 7a 49 6e 70 75 74 5b 6e 53 74 72  r && zInput[nStr
34bac 5d 3d 3d 27 3a 27 20 26 26 20 6d 65 6d 63 6d 70  ]==':' && memcmp
34bad 28 7a 53 74 72 2c 20 7a 49 6e 70 75 74 2c 20 6e  (zStr, zInput, n
34bae 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Str)==0 ){.     
34baf 20 69 43 6f 6c 20 3d 20 69 69 3b 0a 20 20 20 20   iCol = ii;.    
34bb0 20 20 69 43 6f 6c 4c 65 6e 20 3d 20 28 28 7a 49    iColLen = ((zI
34bb1 6e 70 75 74 20 2d 20 7a 29 20 2b 20 6e 53 74 72  nput - z) + nStr
34bb2 20 2b 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65   + 1);.      bre
34bb3 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
34bb4 72 63 20 3d 20 67 65 74 4e 65 78 74 54 6f 6b 65  rc = getNextToke
34bb5 6e 28 70 50 61 72 73 65 2c 20 69 43 6f 6c 2c 20  n(pParse, iCol, 
34bb6 26 7a 5b 69 43 6f 6c 4c 65 6e 5d 2c 20 6e 2d 69  &z[iColLen], n-i
34bb7 43 6f 6c 4c 65 6e 2c 20 70 70 45 78 70 72 2c 20  ColLen, ppExpr, 
34bb8 70 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 2a  pnConsumed);.  *
34bb9 70 6e 43 6f 6e 73 75 6d 65 64 20 2b 3d 20 69 43  pnConsumed += iC
34bba 6f 6c 4c 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20  olLen;.  return 
34bbb 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
34bbc 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
34bbd 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
34bbe 72 65 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20  re for a binary 
34bbf 6f 70 65 72 61 74 6f 72 20 28 61 6e 79 20 74 79  operator (any ty
34bc0 70 65 0a 2a 2a 20 65 78 63 65 70 74 20 61 6e 20  pe.** except an 
34bc1 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 29  FTSQUERY_PHRASE)
34bc2 2e 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65  . Return an inte
34bc3 67 65 72 20 76 61 6c 75 65 20 72 65 70 72 65 73  ger value repres
34bc4 65 6e 74 69 6e 67 20 74 68 65 0a 2a 2a 20 70 72  enting the.** pr
34bc5 65 63 65 64 65 6e 63 65 20 6f 66 20 74 68 65 20  ecedence of the 
34bc6 6f 70 65 72 61 74 6f 72 2e 20 4c 6f 77 65 72 20  operator. Lower 
34bc7 76 61 6c 75 65 73 20 68 61 76 65 20 61 20 68 69  values have a hi
34bc8 67 68 65 72 20 70 72 65 63 65 64 65 6e 63 65 20  gher precedence 
34bc9 28 69 2e 65 2e 0a 2a 2a 20 67 72 6f 75 70 20 6d  (i.e..** group m
34bca 6f 72 65 20 74 69 67 68 74 6c 79 29 2e 20 46 6f  ore tightly). Fo
34bcb 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e 20 74 68  r example, in th
34bcc 65 20 43 20 6c 61 6e 67 75 61 67 65 2c 20 74 68  e C language, th
34bcd 65 20 3d 3d 20 6f 70 65 72 61 74 6f 72 0a 2a 2a  e == operator.**
34bce 20 67 72 6f 75 70 73 20 6d 6f 72 65 20 74 69 67   groups more tig
34bcf 68 74 6c 79 20 74 68 61 6e 20 7c 7c 2c 20 61 6e  htly than ||, an
34bd0 64 20 77 6f 75 6c 64 20 74 68 65 72 65 66 6f 72  d would therefor
34bd1 65 20 68 61 76 65 20 61 20 68 69 67 68 65 72 20  e have a higher 
34bd2 70 72 65 63 65 64 65 6e 63 65 2e 0a 2a 2a 0a 2a  precedence..**.*
34bd3 2a 20 57 68 65 6e 20 75 73 69 6e 67 20 74 68 65  * When using the
34bd4 20 6e 65 77 20 66 74 73 33 20 71 75 65 72 79 20   new fts3 query 
34bd5 73 79 6e 74 61 78 20 28 77 68 65 6e 20 53 51 4c  syntax (when SQL
34bd6 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f  ITE_ENABLE_FTS3_
34bd7 50 41 52 45 4e 54 48 45 53 49 53 0a 2a 2a 20 69  PARENTHESIS.** i
34bd8 73 20 64 65 66 69 6e 65 64 29 2c 20 74 68 65 20  s defined), the 
34bd9 6f 72 64 65 72 20 6f 66 20 74 68 65 20 6f 70 65  order of the ope
34bda 72 61 74 6f 72 73 20 69 6e 20 70 72 65 63 65 64  rators in preced
34bdb 65 6e 63 65 20 66 72 6f 6d 20 68 69 67 68 65 73  ence from highes
34bdc 74 20 74 6f 0a 2a 2a 20 6c 6f 77 65 73 74 20 69  t to.** lowest i
34bdd 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4e 45 41 52 0a  s:.**.**   NEAR.
34bde 2a 2a 20 20 20 4e 4f 54 0a 2a 2a 20 20 20 41 4e  **   NOT.**   AN
34bdf 44 20 28 69 6e 63 6c 75 64 69 6e 67 20 69 6d 70  D (including imp
34be0 6c 69 63 69 74 20 41 4e 44 73 29 0a 2a 2a 20 20  licit ANDs).**  
34be1 20 4f 52 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74   OR.**.** Note t
34be2 68 61 74 20 77 68 65 6e 20 75 73 69 6e 67 20 74  hat when using t
34be3 68 65 20 6f 6c 64 20 71 75 65 72 79 20 73 79 6e  he old query syn
34be4 74 61 78 2c 20 74 68 65 20 4f 52 20 6f 70 65 72  tax, the OR oper
34be5 61 74 6f 72 20 68 61 73 20 61 20 68 69 67 68 65  ator has a highe
34be6 72 0a 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20  r.** precedence 
34be7 74 68 61 6e 20 74 68 65 20 41 4e 44 20 6f 70 65  than the AND ope
34be8 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  rator..*/.static
34be9 20 69 6e 74 20 6f 70 50 72 65 63 65 64 65 6e 63   int opPrecedenc
34bea 65 28 46 74 73 33 45 78 70 72 20 2a 70 29 7b 0a  e(Fts3Expr *p){.
34beb 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79    assert( p->eTy
34bec 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe!=FTSQUERY_PHR
34bed 41 53 45 20 29 3b 0a 20 20 69 66 28 20 73 71 6c  ASE );.  if( sql
34bee 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65  ite3_fts3_enable
34bef 5f 70 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0a  _parentheses ){.
34bf0 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 65 54      return p->eT
34bf1 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ype;.  }else if(
34bf2 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55   p->eType==FTSQU
34bf3 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20  ERY_NEAR ){.    
34bf4 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73  return 1;.  }els
34bf5 65 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d  e if( p->eType==
34bf6 46 54 53 51 55 45 52 59 5f 4f 52 20 29 7b 0a 20  FTSQUERY_OR ){. 
34bf7 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d     return 2;.  }
34bf8 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 54  .  assert( p->eT
34bf9 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 41 4e  ype==FTSQUERY_AN
34bfa 44 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 33 3b  D );.  return 3;
34bfb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65  .}../*.** Argume
34bfc 6e 74 20 70 70 48 65 61 64 20 63 6f 6e 74 61 69  nt ppHead contai
34bfd 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
34bfe 74 68 65 20 63 75 72 72 65 6e 74 20 68 65 61 64  the current head
34bff 20 6f 66 20 61 20 71 75 65 72 79 20 0a 2a 2a 20   of a query .** 
34c00 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
34c01 62 65 69 6e 67 20 70 61 72 73 65 64 2e 20 70 50  being parsed. pP
34c02 72 65 76 20 69 73 20 74 68 65 20 65 78 70 72 65  rev is the expre
34c03 73 73 69 6f 6e 20 6e 6f 64 65 20 6d 6f 73 74 20  ssion node most 
34c04 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 69 6e 73 65  recently.** inse
34c05 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72  rted into the tr
34c06 65 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ee. This functio
34c07 6e 20 61 64 64 73 20 70 4e 65 77 2c 20 77 68 69  n adds pNew, whi
34c08 63 68 20 69 73 20 61 6c 77 61 79 73 20 61 20 62  ch is always a b
34c09 69 6e 61 72 79 0a 2a 2a 20 6f 70 65 72 61 74 6f  inary.** operato
34c0a 72 20 6e 6f 64 65 2c 20 69 6e 74 6f 20 74 68 65  r node, into the
34c0b 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
34c0c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65   based on the re
34c0d 6c 61 74 69 76 65 20 70 72 65 63 65 64 65 6e 63  lative precedenc
34c0e 65 0a 2a 2a 20 6f 66 20 70 4e 65 77 20 61 6e 64  e.** of pNew and
34c0f 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 6e 6f   the existing no
34c10 64 65 73 20 6f 66 20 74 68 65 20 74 72 65 65 2e  des of the tree.
34c11 20 54 68 69 73 20 6d 61 79 20 72 65 73 75 6c 74   This may result
34c12 20 69 6e 20 74 68 65 20 68 65 61 64 0a 2a 2a 20   in the head.** 
34c13 6f 66 20 74 68 65 20 74 72 65 65 20 63 68 61 6e  of the tree chan
34c14 67 69 6e 67 2c 20 69 6e 20 77 68 69 63 68 20 63  ging, in which c
34c15 61 73 65 20 2a 70 70 48 65 61 64 20 69 73 20 73  ase *ppHead is s
34c16 65 74 20 74 6f 20 74 68 65 20 6e 65 77 20 72 6f  et to the new ro
34c17 6f 74 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  ot node..*/.stat
34c18 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 42 69  ic void insertBi
34c19 6e 61 72 79 4f 70 65 72 61 74 6f 72 28 0a 20 20  naryOperator(.  
34c1a 46 74 73 33 45 78 70 72 20 2a 2a 70 70 48 65 61  Fts3Expr **ppHea
34c1b 64 2c 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  d,       /* Poin
34c1c 74 65 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20  ter to the root 
34c1d 6e 6f 64 65 20 6f 66 20 61 20 74 72 65 65 20 2a  node of a tree *
34c1e 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 50  /.  Fts3Expr *pP
34c1f 72 65 76 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rev,         /* 
34c20 4e 6f 64 65 20 6d 6f 73 74 20 72 65 63 65 6e 74  Node most recent
34c21 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ly inserted into
34c22 20 74 68 65 20 74 72 65 65 20 2a 2f 0a 20 20 46   the tree */.  F
34c23 74 73 33 45 78 70 72 20 2a 70 4e 65 77 20 20 20  ts3Expr *pNew   
34c24 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 62          /* New b
34c25 69 6e 61 72 79 20 6e 6f 64 65 20 74 6f 20 69 6e  inary node to in
34c26 73 65 72 74 20 69 6e 74 6f 20 65 78 70 72 65 73  sert into expres
34c27 73 69 6f 6e 20 74 72 65 65 20 2a 2f 0a 29 7b 0a  sion tree */.){.
34c28 20 20 46 74 73 33 45 78 70 72 20 2a 70 53 70 6c    Fts3Expr *pSpl
34c29 69 74 20 3d 20 70 50 72 65 76 3b 0a 20 20 77 68  it = pPrev;.  wh
34c2a 69 6c 65 28 20 70 53 70 6c 69 74 2d 3e 70 50 61  ile( pSplit->pPa
34c2b 72 65 6e 74 20 26 26 20 6f 70 50 72 65 63 65 64  rent && opPreced
34c2c 65 6e 63 65 28 70 53 70 6c 69 74 2d 3e 70 50 61  ence(pSplit->pPa
34c2d 72 65 6e 74 29 3c 3d 6f 70 50 72 65 63 65 64 65  rent)<=opPrecede
34c2e 6e 63 65 28 70 4e 65 77 29 20 29 7b 0a 20 20 20  nce(pNew) ){.   
34c2f 20 70 53 70 6c 69 74 20 3d 20 70 53 70 6c 69 74   pSplit = pSplit
34c30 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 0a  ->pParent;.  }..
34c31 20 20 69 66 28 20 70 53 70 6c 69 74 2d 3e 70 50    if( pSplit->pP
34c32 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 61 73 73  arent ){.    ass
34c33 65 72 74 28 20 70 53 70 6c 69 74 2d 3e 70 50 61  ert( pSplit->pPa
34c34 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70 53  rent->pRight==pS
34c35 70 6c 69 74 20 29 3b 0a 20 20 20 20 70 53 70 6c  plit );.    pSpl
34c36 69 74 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69  it->pParent->pRi
34c37 67 68 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ght = pNew;.    
34c38 70 4e 65 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20  pNew->pParent = 
34c39 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b  pSplit->pParent;
34c3a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
34c3b 70 48 65 61 64 20 3d 20 70 4e 65 77 3b 0a 20 20  pHead = pNew;.  
34c3c 7d 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20  }.  pNew->pLeft 
34c3d 3d 20 70 53 70 6c 69 74 3b 0a 20 20 70 53 70 6c  = pSplit;.  pSpl
34c3e 69 74 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e  it->pParent = pN
34c3f 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  ew;.}../*.** Par
34c40 73 65 20 74 68 65 20 66 74 73 33 20 71 75 65 72  se the fts3 quer
34c41 79 20 65 78 70 72 65 73 73 69 6f 6e 20 66 6f 75  y expression fou
34c42 6e 64 20 69 6e 20 62 75 66 66 65 72 20 7a 2c 20  nd in buffer z, 
34c43 6c 65 6e 67 74 68 20 6e 2e 20 54 68 69 73 20 66  length n. This f
34c44 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72  unction.** retur
34c45 6e 73 20 65 69 74 68 65 72 20 77 68 65 6e 20 74  ns either when t
34c46 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75  he end of the bu
34c47 66 66 65 72 20 69 73 20 72 65 61 63 68 65 64 20  ffer is reached 
34c48 6f 72 20 61 6e 20 75 6e 6d 61 74 63 68 65 64 20  or an unmatched 
34c49 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 62 72 61 63  .** closing brac
34c4a 6b 65 74 20 2d 20 27 29 27 20 2d 20 69 73 20 65  ket - ')' - is e
34c4b 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a  ncountered..**.*
34c4c 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
34c4d 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
34c4e 74 75 72 6e 65 64 2c 20 2a 70 70 45 78 70 72 20  turned, *ppExpr 
34c4f 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
34c50 74 6f 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 64  to the.** parsed
34c51 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 65 78 70   form of the exp
34c52 72 65 73 73 69 6f 6e 20 61 6e 64 20 2a 70 6e 43  ression and *pnC
34c53 6f 6e 73 75 6d 65 64 20 69 73 20 73 65 74 20 74  onsumed is set t
34c54 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  o the number of.
34c55 2a 2a 20 62 79 74 65 73 20 72 65 61 64 20 66 72  ** bytes read fr
34c56 6f 6d 20 62 75 66 66 65 72 20 7a 2e 20 4f 74 68  om buffer z. Oth
34c57 65 72 77 69 73 65 2c 20 2a 70 70 45 78 70 72 20  erwise, *ppExpr 
34c58 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20  is set to 0 and 
34c59 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20  SQLITE_NOMEM.** 
34c5a 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65  (out of memory e
34c5b 72 72 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f  rror) or SQLITE_
34c5c 45 52 52 4f 52 20 28 70 61 72 73 65 20 65 72 72  ERROR (parse err
34c5d 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65 64 2e  or) is returned.
34c5e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
34c5f 74 73 33 45 78 70 72 50 61 72 73 65 28 0a 20 20  ts3ExprParse(.  
34c60 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50  ParseContext *pP
34c61 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
34c62 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20          /* fts3 
34c63 71 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74  query parse cont
34c64 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
34c65 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20  har *z, int n,  
34c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c67 20 2f 2a 20 54 65 78 74 20 6f 66 20 4d 41 54 43   /* Text of MATC
34c68 48 20 71 75 65 72 79 20 2a 2f 0a 20 20 46 74 73  H query */.  Fts
34c69 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20  3Expr **ppExpr, 
34c6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c6b 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72       /* OUT: Par
34c6c 73 65 64 20 71 75 65 72 79 20 73 74 72 75 63 74  sed query struct
34c6d 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ure */.  int *pn
34c6e 43 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20  Consumed        
34c6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c70 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
34c71 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65  of bytes consume
34c72 64 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78  d */.){.  Fts3Ex
34c73 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20  pr *pRet = 0;.  
34c74 46 74 73 33 45 78 70 72 20 2a 70 50 72 65 76 20  Fts3Expr *pPrev 
34c75 3d 20 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20  = 0;.  Fts3Expr 
34c76 2a 70 4e 6f 74 42 72 61 6e 63 68 20 3d 20 30 3b  *pNotBranch = 0;
34c77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
34c78 2a 20 4f 6e 6c 79 20 75 73 65 64 20 69 6e 20 6c  * Only used in l
34c79 65 67 61 63 79 20 70 61 72 73 65 20 6d 6f 64 65  egacy parse mode
34c7a 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 20 3d 20   */.  int nIn = 
34c7b 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  n;.  const char 
34c7c 2a 7a 49 6e 20 3d 20 7a 3b 0a 20 20 69 6e 74 20  *zIn = z;.  int 
34c7d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
34c7e 20 20 69 6e 74 20 69 73 52 65 71 75 69 72 65 50    int isRequireP
34c7f 68 72 61 73 65 20 3d 20 31 3b 0a 0a 20 20 77 68  hrase = 1;..  wh
34c80 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
34c81 4f 4b 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78  OK ){.    Fts3Ex
34c82 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20 20 20 69  pr *p = 0;.    i
34c83 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20  nt nByte = 0;.  
34c84 20 20 72 63 20 3d 20 67 65 74 4e 65 78 74 4e 6f    rc = getNextNo
34c85 64 65 28 70 50 61 72 73 65 2c 20 7a 49 6e 2c 20  de(pParse, zIn, 
34c86 6e 49 6e 2c 20 26 70 2c 20 26 6e 42 79 74 65 29  nIn, &p, &nByte)
34c87 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
34c88 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
34c89 20 69 6e 74 20 69 73 50 68 72 61 73 65 3b 0a 0a   int isPhrase;..
34c8a 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
34c8b 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
34c8c 61 72 65 6e 74 68 65 73 65 73 20 0a 20 20 20 20  arentheses .    
34c8d 20 20 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d     && p->eType==
34c8e 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
34c8f 26 26 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69  && p->pPhrase->i
34c90 73 4e 6f 74 20 0a 20 20 20 20 20 20 29 7b 0a 20  sNot .      ){. 
34c91 20 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65         /* Create
34c92 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 4e 4f 54   an implicit NOT
34c93 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20   operator. */.  
34c94 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
34c95 70 4e 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  pNot = sqlite3_m
34c96 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73  alloc(sizeof(Fts
34c97 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20  3Expr));.       
34c98 20 69 66 28 20 21 70 4e 6f 74 20 29 7b 0a 20 20   if( !pNot ){.  
34c99 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46          sqlite3F
34c9a 74 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a  ts3ExprFree(p);.
34c9b 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
34c9c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
34c9d 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72         goto expr
34c9e 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20  parse_out;.     
34c9f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 65 6d     }.        mem
34ca0 73 65 74 28 70 4e 6f 74 2c 20 30 2c 20 73 69 7a  set(pNot, 0, siz
34ca1 65 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a  eof(Fts3Expr));.
34ca2 20 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e 65 54          pNot->eT
34ca3 79 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f 4e  ype = FTSQUERY_N
34ca4 4f 54 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 74  OT;.        pNot
34ca5 2d 3e 70 52 69 67 68 74 20 3d 20 70 3b 0a 20 20  ->pRight = p;.  
34ca6 20 20 20 20 20 20 69 66 28 20 70 4e 6f 74 42 72        if( pNotBr
34ca7 61 6e 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20  anch ){.        
34ca8 20 20 70 4e 6f 74 42 72 61 6e 63 68 2d 3e 70 4c    pNotBranch->pL
34ca9 65 66 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  eft = p;.       
34caa 20 20 20 70 4e 6f 74 2d 3e 70 52 69 67 68 74 20     pNot->pRight 
34cab 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20  = pNotBranch;.  
34cac 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
34cad 70 4e 6f 74 42 72 61 6e 63 68 20 3d 20 70 4e 6f  pNotBranch = pNo
34cae 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  t;.      }else{.
34caf 20 20 20 20 20 20 20 20 69 6e 74 20 65 54 79 70          int eTyp
34cb0 65 20 3d 20 70 2d 3e 65 54 79 70 65 3b 0a 20 20  e = p->eType;.  
34cb1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54        assert( eT
34cb2 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48  ype!=FTSQUERY_PH
34cb3 52 41 53 45 20 7c 7c 20 21 70 2d 3e 70 50 68 72  RASE || !p->pPhr
34cb4 61 73 65 2d 3e 69 73 4e 6f 74 20 29 3b 0a 20 20  ase->isNot );.  
34cb5 20 20 20 20 20 20 69 73 50 68 72 61 73 65 20 3d        isPhrase =
34cb6 20 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52   (eType==FTSQUER
34cb7 59 5f 50 48 52 41 53 45 20 7c 7c 20 70 2d 3e 70  Y_PHRASE || p->p
34cb8 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20  Left);..        
34cb9 2f 2a 20 54 68 65 20 69 73 52 65 71 75 69 72 65  /* The isRequire
34cba 50 68 72 61 73 65 20 76 61 72 69 61 62 6c 65 20  Phrase variable 
34cbb 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
34cbc 66 20 61 20 70 68 72 61 73 65 20 6f 72 0a 20 20  f a phrase or.  
34cbd 20 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72        ** an expr
34cbe 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 65 64  ession contained
34cbf 20 69 6e 20 70 61 72 65 6e 74 68 65 73 69 73 20   in parenthesis 
34cc0 69 73 20 72 65 71 75 69 72 65 64 2e 20 49 66 20  is required. If 
34cc1 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 69 6e  a.        ** bin
34cc2 61 72 79 20 6f 70 65 72 61 74 6f 72 20 28 41 4e  ary operator (AN
34cc3 44 2c 20 4f 52 2c 20 4e 4f 54 20 6f 72 20 4e 45  D, OR, NOT or NE
34cc4 41 52 29 20 69 73 20 65 6e 63 6f 75 6e 74 65 64  AR) is encounted
34cc5 20 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a   when.        **
34cc6 20 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65   isRequirePhrase
34cc7 20 69 73 20 73 65 74 2c 20 74 68 69 73 20 69 73   is set, this is
34cc8 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e   a syntax error.
34cc9 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
34cca 20 20 20 20 69 66 28 20 21 69 73 50 68 72 61 73      if( !isPhras
34ccb 65 20 26 26 20 69 73 52 65 71 75 69 72 65 50 68  e && isRequirePh
34ccc 72 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20  rase ){.        
34ccd 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
34cce 72 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  rFree(p);.      
34ccf 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
34cd0 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20  ERROR;.         
34cd1 20 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f   goto exprparse_
34cd2 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  out;.        }. 
34cd3 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73   .        if( is
34cd4 50 68 72 61 73 65 20 26 26 20 21 69 73 52 65 71  Phrase && !isReq
34cd5 75 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20  uirePhrase ){.  
34cd6 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72          /* Inser
34cd7 74 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e  t an implicit AN
34cd8 44 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20  D operator. */. 
34cd9 20 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70           Fts3Exp
34cda 72 20 2a 70 41 6e 64 3b 0a 20 20 20 20 20 20 20  r *pAnd;.       
34cdb 20 20 20 61 73 73 65 72 74 28 20 70 52 65 74 20     assert( pRet 
34cdc 26 26 20 70 50 72 65 76 20 29 3b 0a 20 20 20 20  && pPrev );.    
34cdd 20 20 20 20 20 20 70 41 6e 64 20 3d 20 73 71 6c        pAnd = sql
34cde 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
34cdf 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
34ce0 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70 41           if( !pA
34ce1 6e 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nd ){.          
34ce2 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
34ce3 72 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  rFree(p);.      
34ce4 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
34ce5 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20  E_NOMEM;.       
34ce6 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61       goto exprpa
34ce7 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  rse_out;.       
34ce8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d     }.          m
34ce9 65 6d 73 65 74 28 70 41 6e 64 2c 20 30 2c 20 73  emset(pAnd, 0, s
34cea 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 29  izeof(Fts3Expr))
34ceb 3b 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64  ;.          pAnd
34cec 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55 45  ->eType = FTSQUE
34ced 52 59 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20 20  RY_AND;.        
34cee 20 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70    insertBinaryOp
34cef 65 72 61 74 6f 72 28 26 70 52 65 74 2c 20 70 50  erator(&pRet, pP
34cf0 72 65 76 2c 20 70 41 6e 64 29 3b 0a 20 20 20 20  rev, pAnd);.    
34cf1 20 20 20 20 20 20 70 50 72 65 76 20 3d 20 70 41        pPrev = pA
34cf2 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  nd;.        }.. 
34cf3 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 74         /* This t
34cf4 65 73 74 20 63 61 74 63 68 65 73 20 61 74 74 65  est catches atte
34cf5 6d 70 74 73 20 74 6f 20 6d 61 6b 65 20 65 69 74  mpts to make eit
34cf6 68 65 72 20 6f 70 65 72 61 6e 64 20 6f 66 20 61  her operand of a
34cf7 20 4e 45 41 52 0a 20 20 20 20 20 20 20 20 2a 2a   NEAR.        **
34cf8 20 6f 70 65 72 61 74 6f 72 20 73 6f 6d 65 74 68   operator someth
34cf9 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61  ing other than a
34cfa 20 70 68 72 61 73 65 2e 20 46 6f 72 20 65 78 61   phrase. For exa
34cfb 6d 70 6c 65 2c 20 65 69 74 68 65 72 20 6f 66 0a  mple, either of.
34cfc 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66          ** the f
34cfd 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 20 20  ollowing:.      
34cfe 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
34cff 20 20 20 28 62 72 61 63 6b 65 74 65 64 20 65 78     (bracketed ex
34d00 70 72 65 73 73 69 6f 6e 29 20 4e 45 41 52 20 70  pression) NEAR p
34d01 68 72 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  hrase.        **
34d02 20 20 20 20 70 68 72 61 73 65 20 4e 45 41 52 20      phrase NEAR 
34d03 28 62 72 61 63 6b 65 74 65 64 20 65 78 70 72 65  (bracketed expre
34d04 73 73 69 6f 6e 29 0a 20 20 20 20 20 20 20 20 2a  ssion).        *
34d05 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 52 65 74  *.        ** Ret
34d06 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 20  urn an error in 
34d07 65 69 74 68 65 72 20 63 61 73 65 2e 0a 20 20 20  either case..   
34d08 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
34d09 69 66 28 20 70 50 72 65 76 20 26 26 20 28 0a 20  if( pPrev && (. 
34d0a 20 20 20 20 20 20 20 20 20 20 20 28 65 54 79 70             (eTyp
34d0b 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
34d0c 20 26 26 20 21 69 73 50 68 72 61 73 65 20 26 26   && !isPhrase &&
34d0d 20 70 50 72 65 76 2d 3e 65 54 79 70 65 21 3d 46   pPrev->eType!=F
34d0e 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 29 0a  TSQUERY_PHRASE).
34d0f 20 20 20 20 20 20 20 20 20 7c 7c 20 28 65 54 79           || (eTy
34d10 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe!=FTSQUERY_PHR
34d11 41 53 45 20 26 26 20 69 73 50 68 72 61 73 65 20  ASE && isPhrase 
34d12 26 26 20 70 50 72 65 76 2d 3e 65 54 79 70 65 3d  && pPrev->eType=
34d13 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 29 0a  =FTSQUERY_NEAR).
34d14 20 20 20 20 20 20 20 20 29 29 7b 0a 20 20 20 20          )){.    
34d15 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73        sqlite3Fts
34d16 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20 20  3ExprFree(p);.  
34d17 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
34d18 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
34d19 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61       goto exprpa
34d1a 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  rse_out;.       
34d1b 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 69 66   }.  .        if
34d1c 28 20 69 73 50 68 72 61 73 65 20 29 7b 0a 20 20  ( isPhrase ){.  
34d1d 20 20 20 20 20 20 20 20 69 66 28 20 70 52 65 74          if( pRet
34d1e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
34d1f 61 73 73 65 72 74 28 20 70 50 72 65 76 20 26 26  assert( pPrev &&
34d20 20 70 50 72 65 76 2d 3e 70 4c 65 66 74 20 26 26   pPrev->pLeft &&
34d21 20 70 50 72 65 76 2d 3e 70 52 69 67 68 74 3d 3d   pPrev->pRight==
34d22 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  0 );.           
34d23 20 70 50 72 65 76 2d 3e 70 52 69 67 68 74 20 3d   pPrev->pRight =
34d24 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   p;.            
34d25 70 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 72  p->pParent = pPr
34d26 65 76 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ev;.          }e
34d27 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
34d28 20 70 52 65 74 20 3d 20 70 3b 0a 20 20 20 20 20   pRet = p;.     
34d29 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
34d2a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
34d2b 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65 72  insertBinaryOper
34d2c 61 74 6f 72 28 26 70 52 65 74 2c 20 70 50 72 65  ator(&pRet, pPre
34d2d 76 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 7d  v, p);.        }
34d2e 0a 20 20 20 20 20 20 20 20 69 73 52 65 71 75 69  .        isRequi
34d2f 72 65 50 68 72 61 73 65 20 3d 20 21 69 73 50 68  rePhrase = !isPh
34d30 72 61 73 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rase;.      }.  
34d31 20 20 20 20 61 73 73 65 72 74 28 20 6e 42 79 74      assert( nByt
34d32 65 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  e>0 );.    }.   
34d33 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
34d34 49 54 45 5f 4f 4b 20 7c 7c 20 28 6e 42 79 74 65  ITE_OK || (nByte
34d35 3e 30 20 26 26 20 6e 42 79 74 65 3c 3d 6e 49 6e  >0 && nByte<=nIn
34d36 29 20 29 3b 0a 20 20 20 20 6e 49 6e 20 2d 3d 20  ) );.    nIn -= 
34d37 6e 42 79 74 65 3b 0a 20 20 20 20 7a 49 6e 20 2b  nByte;.    zIn +
34d38 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 50 72  = nByte;.    pPr
34d39 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 69  ev = p;.  }..  i
34d3a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
34d3b 4e 45 20 26 26 20 70 52 65 74 20 26 26 20 69 73  NE && pRet && is
34d3c 52 65 71 75 69 72 65 50 68 72 61 73 65 20 29 7b  RequirePhrase ){
34d3d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
34d3e 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69  _ERROR;.  }..  i
34d3f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
34d40 4e 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  NE ){.    rc = S
34d41 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 66  QLITE_OK;.    if
34d42 28 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33 5f  ( !sqlite3_fts3_
34d43 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73  enable_parenthes
34d44 65 73 20 26 26 20 70 4e 6f 74 42 72 61 6e 63 68  es && pNotBranch
34d45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70   ){.      if( !p
34d46 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  Ret ){.        r
34d47 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
34d48 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
34d49 20 20 20 20 20 20 20 70 4e 6f 74 42 72 61 6e 63         pNotBranc
34d4a 68 2d 3e 70 4c 65 66 74 20 3d 20 70 52 65 74 3b  h->pLeft = pRet;
34d4b 0a 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20  .        pRet = 
34d4c 70 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20  pNotBranch;.    
34d4d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
34d4e 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 6e 20  *pnConsumed = n 
34d4f 2d 20 6e 49 6e 3b 0a 0a 65 78 70 72 70 61 72 73  - nIn;..exprpars
34d50 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21  e_out:.  if( rc!
34d51 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
34d52 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
34d53 72 46 72 65 65 28 70 52 65 74 29 3b 0a 20 20 20  rFree(pRet);.   
34d54 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
34d55 46 72 65 65 28 70 4e 6f 74 42 72 61 6e 63 68 29  Free(pNotBranch)
34d56 3b 0a 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a  ;.    pRet = 0;.
34d57 20 20 7d 0a 20 20 2a 70 70 45 78 70 72 20 3d 20    }.  *ppExpr = 
34d58 70 52 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 72  pRet;.  return r
34d59 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61  c;.}../*.** Para
34d5a 6d 65 74 65 72 73 20 7a 20 61 6e 64 20 6e 20 63  meters z and n c
34d5b 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72  ontain a pointer
34d5c 20 74 6f 20 61 6e 64 20 6c 65 6e 67 74 68 20 6f   to and length o
34d5d 66 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  f a buffer conta
34d5e 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 66 74 73 33  ining.** an fts3
34d5f 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
34d60 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
34d61 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
34d62 74 74 65 6d 70 74 73 20 74 6f 20 70 61 72 73 65  ttempts to parse
34d63 20 74 68 65 0a 2a 2a 20 71 75 65 72 79 20 65 78   the.** query ex
34d64 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 63 72 65  pression and cre
34d65 61 74 65 20 61 20 74 72 65 65 20 6f 66 20 46 74  ate a tree of Ft
34d66 73 33 45 78 70 72 20 73 74 72 75 63 74 75 72 65  s3Expr structure
34d67 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  s representing t
34d68 68 65 0a 2a 2a 20 70 61 72 73 65 64 20 65 78 70  he.** parsed exp
34d69 72 65 73 73 69 6f 6e 2e 20 49 66 20 73 75 63 63  ression. If succ
34d6a 65 73 73 66 75 6c 2c 20 2a 70 70 45 78 70 72 20  essful, *ppExpr 
34d6b 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
34d6c 74 6f 20 74 68 65 20 68 65 61 64 0a 2a 2a 20 6f  to the head.** o
34d6d 66 20 74 68 65 20 70 61 72 73 65 64 20 65 78 70  f the parsed exp
34d6e 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64  ression tree and
34d6f 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
34d70 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72  turned. If an er
34d71 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 65  ror.** occurs, e
34d72 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ither SQLITE_NOM
34d73 45 4d 20 28 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72  EM (out-of-memor
34d74 79 20 65 72 72 6f 72 29 20 6f 72 20 53 51 4c 49  y error) or SQLI
34d75 54 45 5f 45 52 52 4f 52 20 28 70 61 72 73 65 0a  TE_ERROR (parse.
34d76 2a 2a 20 65 72 72 6f 72 29 20 69 73 20 72 65 74  ** error) is ret
34d77 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45 78 70  urned and *ppExp
34d78 72 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a  r is set to 0..*
34d79 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65  *.** If paramete
34d7a 72 20 6e 20 69 73 20 61 20 6e 65 67 61 74 69 76  r n is a negativ
34d7b 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 6e 20 7a  e number, then z
34d7c 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 70   is assumed to p
34d7d 6f 69 6e 74 20 74 6f 20 61 0a 2a 2a 20 6e 75 6c  oint to a.** nul
34d7e 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
34d7f 6e 67 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74  ng and the lengt
34d80 68 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  h is determined 
34d81 75 73 69 6e 67 20 73 74 72 6c 65 6e 28 29 2e 0a  using strlen()..
34d82 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
34d83 70 61 72 61 6d 65 74 65 72 2c 20 70 54 6f 6b 65  parameter, pToke
34d84 6e 69 7a 65 72 2c 20 69 73 20 70 61 73 73 65 64  nizer, is passed
34d85 20 74 68 65 20 66 74 73 33 20 74 6f 6b 65 6e 69   the fts3 tokeni
34d86 7a 65 72 20 6d 6f 64 75 6c 65 20 74 6f 0a 2a 2a  zer module to.**
34d87 20 75 73 65 20 74 6f 20 6e 6f 72 6d 61 6c 69 7a   use to normaliz
34d88 65 20 71 75 65 72 79 20 74 6f 6b 65 6e 73 20 77  e query tokens w
34d89 68 69 6c 65 20 70 61 72 73 69 6e 67 20 74 68 65  hile parsing the
34d8a 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 54 68 65   expression. The
34d8b 20 61 7a 43 6f 6c 5b 5d 0a 2a 2a 20 61 72 72 61   azCol[].** arra
34d8c 79 2c 20 77 68 69 63 68 20 69 73 20 61 73 73 75  y, which is assu
34d8d 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 6e  med to contain n
34d8e 43 6f 6c 20 65 6e 74 72 69 65 73 2c 20 73 68 6f  Col entries, sho
34d8f 75 6c 64 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  uld contain the 
34d90 6e 61 6d 65 73 0a 2a 2a 20 6f 66 20 65 61 63 68  names.** of each
34d91 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 74   column in the t
34d92 61 72 67 65 74 20 66 74 73 33 20 74 61 62 6c 65  arget fts3 table
34d93 2c 20 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20  , in order from 
34d94 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2e 20 0a  left to right. .
34d95 2a 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ** Column names 
34d96 6d 75 73 74 20 62 65 20 6e 75 6c 2d 74 65 72 6d  must be nul-term
34d97 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 2e 0a  inated strings..
34d98 2a 2a 0a 2a 2a 20 54 68 65 20 69 44 65 66 61 75  **.** The iDefau
34d99 6c 74 43 6f 6c 20 70 61 72 61 6d 65 74 65 72 20  ltCol parameter 
34d9a 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
34d9b 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
34d9c 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a  e table column.*
34d9d 2a 20 74 68 61 74 20 61 70 70 65 61 72 73 20 6f  * that appears o
34d9e 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 2d  n the left-hand-
34d9f 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
34da0 48 20 6f 70 65 72 61 74 6f 72 20 28 74 68 65 20  H operator (the 
34da1 64 65 66 61 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d  default.** colum
34da2 6e 20 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e  n to match again
34da3 73 74 20 66 6f 72 20 74 6f 6b 65 6e 73 20 66 6f  st for tokens fo
34da4 72 20 77 68 69 63 68 20 61 20 63 6f 6c 75 6d 6e  r which a column
34da5 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 65 78 70   name is not exp
34da6 6c 69 63 69 74 6c 79 0a 2a 2a 20 73 70 65 63 69  licitly.** speci
34da7 66 69 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  fied as part of 
34da8 74 68 65 20 71 75 65 72 79 20 73 74 72 69 6e 67  the query string
34da9 29 2c 20 6f 72 20 2d 31 20 69 66 20 74 6f 6b 65  ), or -1 if toke
34daa 6e 73 20 6d 61 79 20 62 79 20 64 65 66 61 75 6c  ns may by defaul
34dab 74 0a 2a 2a 20 6d 61 74 63 68 20 61 6e 79 20 74  t.** match any t
34dac 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  able column..*/.
34dad 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
34dae 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  nt sqlite3Fts3Ex
34daf 70 72 50 61 72 73 65 28 0a 20 20 73 71 6c 69 74  prParse(.  sqlit
34db0 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
34db1 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 2f  okenizer,      /
34db2 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  * Tokenizer modu
34db3 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
34db4 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  zCol,           
34db5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
34db6 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e  rray of column n
34db7 61 6d 65 73 20 66 6f 72 20 66 74 73 33 20 74 61  ames for fts3 ta
34db8 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
34db9 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
34dba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34dbb 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
34dbc 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a  s in azCol[] */.
34dbd 20 20 69 6e 74 20 69 44 65 66 61 75 6c 74 43 6f    int iDefaultCo
34dbe 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
34dbf 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
34dc0 20 63 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79   column to query
34dc1 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
34dc2 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20   *z, int n,     
34dc3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
34dc4 74 20 6f 66 20 4d 41 54 43 48 20 71 75 65 72 79  t of MATCH query
34dc5 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
34dc6 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20 20  *ppExpr         
34dc7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
34dc8 3a 20 50 61 72 73 65 64 20 71 75 65 72 79 20 73  : Parsed query s
34dc9 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20  tructure */.){. 
34dca 20 69 6e 74 20 6e 50 61 72 73 65 64 3b 0a 20 20   int nParsed;.  
34dcb 69 6e 74 20 72 63 3b 0a 20 20 50 61 72 73 65 43  int rc;.  ParseC
34dcc 6f 6e 74 65 78 74 20 73 50 61 72 73 65 3b 0a 20  ontext sParse;. 
34dcd 20 73 50 61 72 73 65 2e 70 54 6f 6b 65 6e 69 7a   sParse.pTokeniz
34dce 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b  er = pTokenizer;
34dcf 0a 20 20 73 50 61 72 73 65 2e 61 7a 43 6f 6c 20  .  sParse.azCol 
34dd0 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  = (const char **
34dd1 29 61 7a 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65  )azCol;.  sParse
34dd2 2e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20  .nCol = nCol;.  
34dd3 73 50 61 72 73 65 2e 69 44 65 66 61 75 6c 74 43  sParse.iDefaultC
34dd4 6f 6c 20 3d 20 69 44 65 66 61 75 6c 74 43 6f 6c  ol = iDefaultCol
34dd5 3b 0a 20 20 73 50 61 72 73 65 2e 6e 4e 65 73 74  ;.  sParse.nNest
34dd6 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 3d 3d 30   = 0;.  if( z==0
34dd7 20 29 7b 0a 20 20 20 20 2a 70 70 45 78 70 72 20   ){.    *ppExpr 
34dd8 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
34dd9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
34dda 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20   if( n<0 ){.    
34ddb 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  n = strlen(z);. 
34ddc 20 7d 0a 20 20 72 63 20 3d 20 66 74 73 33 45 78   }.  rc = fts3Ex
34ddd 70 72 50 61 72 73 65 28 26 73 50 61 72 73 65 2c  prParse(&sParse,
34dde 20 7a 2c 20 6e 2c 20 70 70 45 78 70 72 2c 20 26   z, n, ppExpr, &
34ddf 6e 50 61 72 73 65 64 29 3b 0a 0a 20 20 2f 2a 20  nParsed);..  /* 
34de0 43 68 65 63 6b 20 66 6f 72 20 6d 69 73 6d 61 74  Check for mismat
34de1 63 68 65 64 20 70 61 72 65 6e 74 68 65 73 69 73  ched parenthesis
34de2 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
34de3 4c 49 54 45 5f 4f 4b 20 26 26 20 73 50 61 72 73  LITE_OK && sPars
34de4 65 2e 6e 4e 65 73 74 20 29 7b 0a 20 20 20 20 72  e.nNest ){.    r
34de5 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
34de6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ;.    sqlite3Fts
34de7 33 45 78 70 72 46 72 65 65 28 2a 70 70 45 78 70  3ExprFree(*ppExp
34de8 72 29 3b 0a 20 20 20 20 2a 70 70 45 78 70 72 20  r);.    *ppExpr 
34de9 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  = 0;.  }..  retu
34dea 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
34deb 46 72 65 65 20 61 20 70 61 72 73 65 64 20 66 74  Free a parsed ft
34dec 73 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73  s3 query express
34ded 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  ion allocated by
34dee 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
34def 50 61 72 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49  Parse()..*/.SQLI
34df0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
34df1 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
34df2 72 65 65 28 46 74 73 33 45 78 70 72 20 2a 70 29  ree(Fts3Expr *p)
34df3 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
34df4 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
34df5 46 72 65 65 28 70 2d 3e 70 4c 65 66 74 29 3b 0a  Free(p->pLeft);.
34df6 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 45      sqlite3Fts3E
34df7 78 70 72 46 72 65 65 28 70 2d 3e 70 52 69 67 68  xprFree(p->pRigh
34df8 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
34df9 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  free(p);.  }.}..
34dfa 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
34dfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34dfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34dfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34dfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
34dff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 45  ***********.** E
34e04 76 65 72 79 74 68 69 6e 67 20 61 66 74 65 72 20  verything after 
34e05 74 68 69 73 20 70 6f 69 6e 74 20 69 73 20 6a 75  this point is ju
34e06 73 74 20 74 65 73 74 20 63 6f 64 65 2e 0a 2a 2f  st test code..*/
34e07 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
34e08 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e  TEST.../*.** Fun
34e09 63 74 69 6f 6e 20 74 6f 20 71 75 65 72 79 20 74  ction to query t
34e0a 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20 6f 66  he hash-table of
34e0b 20 74 6f 6b 65 6e 69 7a 65 72 73 20 28 73 65 65   tokenizers (see
34e0c 20 52 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65   README.tokenize
34e0d 72 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  rs)..*/.static i
34e0e 6e 74 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65  nt queryTestToke
34e0f 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33  nizer(.  sqlite3
34e10 20 2a 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63   *db, .  const c
34e11 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20  har *zName,  .  
34e12 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
34e13 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
34e14 2a 70 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  *pp.){.  int rc;
34e15 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
34e16 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20  *pStmt;.  const 
34e17 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53  char zSql[] = "S
34e18 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e  ELECT fts3_token
34e19 69 7a 65 72 28 3f 29 22 3b 0a 0a 20 20 2a 70 70  izer(?)";..  *pp
34e1a 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c   = 0;.  rc = sql
34e1b 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
34e1c 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
34e1d 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  Stmt, 0);.  if( 
34e1e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
34e1f 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
34e20 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62    }..  sqlite3_b
34e21 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c 20  ind_text(pStmt, 
34e22 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51  1, zName, -1, SQ
34e23 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
34e24 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  if( SQLITE_ROW==
34e25 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
34e26 6d 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 73  mt) ){.    if( s
34e27 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
34e28 70 65 28 70 53 74 6d 74 2c 20 30 29 3d 3d 53 51  pe(pStmt, 0)==SQ
34e29 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20  LITE_BLOB ){.   
34e2a 20 20 20 6d 65 6d 63 70 79 28 70 70 2c 20 73 71     memcpy(pp, sq
34e2b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
34e2c 62 28 70 53 74 6d 74 2c 20 30 29 2c 20 73 69 7a  b(pStmt, 0), siz
34e2d 65 6f 66 28 2a 70 70 29 29 3b 0a 20 20 20 20 7d  eof(*pp));.    }
34e2e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73  .  }..  return s
34e2f 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
34e30 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pStmt);.}../*.**
34e31 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
34e32 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 65  s part of the te
34e33 73 74 20 69 6e 74 65 72 66 61 63 65 20 66 6f 72  st interface for
34e34 20 74 68 65 20 71 75 65 72 79 20 70 61 72 73 65   the query parse
34e35 72 2e 20 49 74 0a 2a 2a 20 77 72 69 74 65 73 20  r. It.** writes 
34e36 61 20 74 65 78 74 20 72 65 70 72 65 73 65 6e 74  a text represent
34e37 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65  ation of the que
34e38 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  ry expression pE
34e39 78 70 72 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20  xpr into the.** 
34e3a 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
34e3b 6f 20 62 79 20 61 72 67 75 6d 65 6e 74 20 7a 42  o by argument zB
34e3c 75 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65  uf. It is assume
34e3d 64 20 74 68 61 74 20 7a 42 75 66 20 69 73 20 6c  d that zBuf is l
34e3e 61 72 67 65 20 0a 2a 2a 20 65 6e 6f 75 67 68 20  arge .** enough 
34e3f 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 71  to store the req
34e40 75 69 72 65 64 20 74 65 78 74 20 72 65 70 72 65  uired text repre
34e41 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  sentation..*/.st
34e42 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 54 6f  atic void exprTo
34e43 53 74 72 69 6e 67 28 46 74 73 33 45 78 70 72 20  String(Fts3Expr 
34e44 2a 70 45 78 70 72 2c 20 63 68 61 72 20 2a 7a 42  *pExpr, char *zB
34e45 75 66 29 7b 0a 20 20 73 77 69 74 63 68 28 20 70  uf){.  switch( p
34e46 45 78 70 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20  Expr->eType ){. 
34e47 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
34e48 5f 50 48 52 41 53 45 3a 20 7b 0a 20 20 20 20 20  _PHRASE: {.     
34e49 20 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68   Fts3Phrase *pPh
34e4a 72 61 73 65 20 3d 20 70 45 78 70 72 2d 3e 70 50  rase = pExpr->pP
34e4b 68 72 61 73 65 3b 0a 20 20 20 20 20 20 69 6e 74  hrase;.      int
34e4c 20 69 3b 0a 20 20 20 20 20 20 7a 42 75 66 20 2b   i;.      zBuf +
34e4d 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20  = sprintf(zBuf, 
34e4e 22 50 48 52 41 53 45 20 25 64 20 25 64 22 2c 20  "PHRASE %d %d", 
34e4f 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e  pPhrase->iColumn
34e50 2c 20 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74  , pPhrase->isNot
34e51 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
34e52 3b 20 69 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f  ; i<pPhrase->nTo
34e53 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ken; i++){.     
34e54 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e     zBuf += sprin
34e55 74 66 28 7a 42 75 66 2c 22 20 25 2e 2a 73 22 2c  tf(zBuf," %.*s",
34e56 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
34e57 69 5d 2e 6e 2c 70 50 68 72 61 73 65 2d 3e 61 54  i].n,pPhrase->aT
34e58 6f 6b 65 6e 5b 69 5d 2e 7a 29 3b 0a 20 20 20 20  oken[i].z);.    
34e59 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69      zBuf += spri
34e5a 6e 74 66 28 7a 42 75 66 2c 22 25 73 22 2c 20 28  ntf(zBuf,"%s", (
34e5b 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
34e5c 69 5d 2e 69 73 50 72 65 66 69 78 3f 22 2b 22 3a  i].isPrefix?"+":
34e5d 22 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ""));.      }.  
34e5e 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
34e5f 7d 0a 0a 20 20 20 20 63 61 73 65 20 46 54 53 51  }..    case FTSQ
34e60 55 45 52 59 5f 4e 45 41 52 3a 0a 20 20 20 20 20  UERY_NEAR:.     
34e61 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
34e62 28 7a 42 75 66 2c 20 22 4e 45 41 52 2f 25 64 20  (zBuf, "NEAR/%d 
34e63 22 2c 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72 29  ", pExpr->nNear)
34e64 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
34e65 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
34e66 5f 4e 4f 54 3a 0a 20 20 20 20 20 20 7a 42 75 66  _NOT:.      zBuf
34e67 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
34e68 2c 20 22 4e 4f 54 20 22 29 3b 0a 20 20 20 20 20  , "NOT ");.     
34e69 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
34e6a 20 46 54 53 51 55 45 52 59 5f 41 4e 44 3a 0a 20   FTSQUERY_AND:. 
34e6b 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72       zBuf += spr
34e6c 69 6e 74 66 28 7a 42 75 66 2c 20 22 41 4e 44 20  intf(zBuf, "AND 
34e6d 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ");.      break;
34e6e 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45  .    case FTSQUE
34e6f 52 59 5f 4f 52 3a 0a 20 20 20 20 20 20 7a 42 75  RY_OR:.      zBu
34e70 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
34e71 66 2c 20 22 4f 52 20 22 29 3b 0a 20 20 20 20 20  f, "OR ");.     
34e72 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 7a   break;.  }..  z
34e73 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a  Buf += sprintf(z
34e74 42 75 66 2c 20 22 7b 22 29 3b 0a 20 20 65 78 70  Buf, "{");.  exp
34e75 72 54 6f 53 74 72 69 6e 67 28 70 45 78 70 72 2d  rToString(pExpr-
34e76 3e 70 4c 65 66 74 2c 20 7a 42 75 66 29 3b 0a 20  >pLeft, zBuf);. 
34e77 20 7a 42 75 66 20 2b 3d 20 73 74 72 6c 65 6e 28   zBuf += strlen(
34e78 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d  zBuf);.  zBuf +=
34e79 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
34e7a 7d 20 22 29 3b 0a 0a 20 20 7a 42 75 66 20 2b 3d  } ");..  zBuf +=
34e7b 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
34e7c 7b 22 29 3b 0a 20 20 65 78 70 72 54 6f 53 74 72  {");.  exprToStr
34e7d 69 6e 67 28 70 45 78 70 72 2d 3e 70 52 69 67 68  ing(pExpr->pRigh
34e7e 74 2c 20 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66  t, zBuf);.  zBuf
34e7f 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29   += strlen(zBuf)
34e80 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69  ;.  zBuf += spri
34e81 6e 74 66 28 7a 42 75 66 2c 20 22 7d 22 29 3b 0a  ntf(zBuf, "}");.
34e82 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
34e83 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
34e84 69 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61 72 20  ion of a scalar 
34e85 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  SQL function use
34e86 64 20 74 6f 20 74 65 73 74 20 74 68 65 20 0a 2a  d to test the .*
34e87 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 72  * expression par
34e88 73 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62  ser. It should b
34e89 65 20 63 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c  e called as foll
34e8a 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73  ows:.**.**   fts
34e8b 33 5f 65 78 70 72 74 65 73 74 28 3c 74 6f 6b 65  3_exprtest(<toke
34e8c 6e 69 7a 65 72 3e 2c 20 3c 65 78 70 72 3e 2c 20  nizer>, <expr>, 
34e8d 3c 63 6f 6c 75 6d 6e 20 31 3e 2c 20 2e 2e 2e 29  <column 1>, ...)
34e8e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ;.**.** The firs
34e8f 74 20 61 72 67 75 6d 65 6e 74 2c 20 3c 74 6f 6b  t argument, <tok
34e90 65 6e 69 7a 65 72 3e 2c 20 69 73 20 74 68 65 20  enizer>, is the 
34e91 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 74 73 33  name of the fts3
34e92 20 74 6f 6b 65 6e 69 7a 65 72 20 75 73 65 64 0a   tokenizer used.
34e93 2a 2a 20 74 6f 20 70 61 72 73 65 20 74 68 65 20  ** to parse the 
34e94 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  query expression
34e95 20 28 73 65 65 20 52 45 41 44 4d 45 2e 74 6f 6b   (see README.tok
34e96 65 6e 69 7a 65 72 73 29 2e 20 54 68 65 20 73 65  enizers). The se
34e97 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
34e98 20 69 73 20 74 68 65 20 71 75 65 72 79 20 65 78   is the query ex
34e99 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61 72 73  pression to pars
34e9a 65 2e 20 45 61 63 68 20 73 75 62 73 65 71 75 65  e. Each subseque
34e9b 6e 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nt argument is t
34e9c 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20  he name.** of a 
34e9d 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 74  column of the ft
34e9e 73 33 20 74 61 62 6c 65 20 74 68 61 74 20 74 68  s3 table that th
34e9f 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  e query expressi
34ea0 6f 6e 20 6d 61 79 20 72 65 66 65 72 20 74 6f 2e  on may refer to.
34ea1 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a  .** For example:
34ea2 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
34ea3 66 74 73 33 5f 65 78 70 72 74 65 73 74 28 27 73  fts3_exprtest('s
34ea4 69 6d 70 6c 65 27 2c 20 27 42 69 6c 6c 20 63 6f  imple', 'Bill co
34ea5 6c 32 3a 42 6c 6f 67 67 73 27 2c 20 27 63 6f 6c  l2:Bloggs', 'col
34ea6 31 27 2c 20 27 63 6f 6c 32 27 29 3b 0a 2a 2f 0a  1', 'col2');.*/.
34ea7 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
34ea8 45 78 70 72 54 65 73 74 28 0a 20 20 73 71 6c 69  ExprTest(.  sqli
34ea9 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
34eaa 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
34eab 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
34eac 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71  e **argv.){.  sq
34ead 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
34eae 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d  module const *pM
34eaf 6f 64 75 6c 65 20 3d 20 30 3b 0a 20 20 73 71 6c  odule = 0;.  sql
34eb0 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
34eb1 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0a  pTokenizer = 0;.
34eb2 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72    int rc;.  char
34eb3 20 2a 2a 61 7a 43 6f 6c 20 3d 20 30 3b 0a 20 20   **azCol = 0;.  
34eb4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78 70  const char *zExp
34eb5 72 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b 0a  r;.  int nExpr;.
34eb6 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e    int nCol;.  in
34eb7 74 20 69 69 3b 0a 20 20 46 74 73 33 45 78 70 72  t ii;.  Fts3Expr
34eb8 20 2a 70 45 78 70 72 3b 0a 20 20 73 71 6c 69 74   *pExpr;.  sqlit
34eb9 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
34eba 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
34ebb 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20  le(context);..  
34ebc 69 66 28 20 61 72 67 63 3c 33 20 29 7b 0a 20 20  if( argc<3 ){.  
34ebd 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
34ebe 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
34ebf 0a 20 20 20 20 20 20 20 20 22 55 73 61 67 65 3a  .        "Usage:
34ec0 20 66 74 73 33 5f 65 78 70 72 74 65 73 74 28 74   fts3_exprtest(t
34ec1 6f 6b 65 6e 69 7a 65 72 2c 20 65 78 70 72 2c 20  okenizer, expr, 
34ec2 63 6f 6c 31 2c 20 2e 2e 2e 22 2c 20 2d 31 0a 20  col1, ...", -1. 
34ec3 20 20 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e     );.    return
34ec4 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 71 75  ;.  }..  rc = qu
34ec5 65 72 79 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72  eryTestTokenizer
34ec6 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  (db,.           
34ec7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
34ec8 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
34ec9 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
34eca 61 72 67 76 5b 30 5d 29 2c 20 26 70 4d 6f 64 75  argv[0]), &pModu
34ecb 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  le);.  if( rc==S
34ecc 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
34ecd 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
34ece 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
34ecf 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f  ntext);.    goto
34ed0 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20   exprtest_out;. 
34ed1 20 7d 65 6c 73 65 20 69 66 28 20 21 70 4d 6f 64   }else if( !pMod
34ed2 75 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ule ){.    sqlit
34ed3 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
34ed4 63 6f 6e 74 65 78 74 2c 20 22 4e 6f 20 73 75 63  context, "No suc
34ed5 68 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  h tokenizer modu
34ed6 6c 65 22 2c 20 2d 31 29 3b 0a 20 20 20 20 67 6f  le", -1);.    go
34ed7 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b  to exprtest_out;
34ed8 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 70 4d 6f  .  }..  rc = pMo
34ed9 64 75 6c 65 2d 3e 78 43 72 65 61 74 65 28 30 2c  dule->xCreate(0,
34eda 20 30 2c 20 26 70 54 6f 6b 65 6e 69 7a 65 72 29   0, &pTokenizer)
34edb 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
34edc 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
34edd 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
34ede 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
34edf 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73  E_NOMEM ){.    s
34ee0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
34ee1 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78  ror_nomem(contex
34ee2 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70  t);.    goto exp
34ee3 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 20  rtest_out;.  }. 
34ee4 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
34ee5 64 75 6c 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a  dule = pModule;.
34ee6 0a 20 20 7a 45 78 70 72 20 3d 20 28 63 6f 6e 73  .  zExpr = (cons
34ee7 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
34ee8 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
34ee9 5b 31 5d 29 3b 0a 20 20 6e 45 78 70 72 20 3d 20  [1]);.  nExpr = 
34eea 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
34eeb 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  tes(argv[1]);.  
34eec 6e 43 6f 6c 20 3d 20 61 72 67 63 2d 32 3b 0a 20  nCol = argc-2;. 
34eed 20 61 7a 43 6f 6c 20 3d 20 28 63 68 61 72 20 2a   azCol = (char *
34eee 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
34eef 28 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 68 61  (nCol*sizeof(cha
34ef0 72 20 2a 29 29 3b 0a 20 20 69 66 28 20 21 61 7a  r *));.  if( !az
34ef1 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Col ){.    sqlit
34ef2 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
34ef3 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
34ef4 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73      goto exprtes
34ef5 74 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 66 6f 72  t_out;.  }.  for
34ef6 28 69 69 3d 30 3b 20 69 69 3c 6e 43 6f 6c 3b 20  (ii=0; ii<nCol; 
34ef7 69 69 2b 2b 29 7b 0a 20 20 20 20 61 7a 43 6f 6c  ii++){.    azCol
34ef8 5b 69 69 5d 20 3d 20 28 63 68 61 72 20 2a 29 73  [ii] = (char *)s
34ef9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
34efa 74 28 61 72 67 76 5b 69 69 2b 32 5d 29 3b 0a 20  t(argv[ii+2]);. 
34efb 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
34efc 65 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28  e3Fts3ExprParse(
34efd 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e 69 7a 65  .      pTokenize
34efe 72 2c 20 61 7a 43 6f 6c 2c 20 6e 43 6f 6c 2c 20  r, azCol, nCol, 
34eff 6e 43 6f 6c 2c 20 7a 45 78 70 72 2c 20 6e 45 78  nCol, zExpr, nEx
34f00 70 72 2c 20 26 70 45 78 70 72 0a 20 20 29 3b 0a  pr, &pExpr.  );.
34f01 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
34f02 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71  _NOMEM ){.    sq
34f03 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
34f04 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74  or_nomem(context
34f05 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72  );.    goto expr
34f06 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73  test_out;.  }els
34f07 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45  e if( rc==SQLITE
34f08 5f 4f 4b 20 29 7b 0a 20 20 20 20 63 68 61 72 20  _OK ){.    char 
34f09 7a 42 75 66 5b 34 30 39 36 5d 3b 0a 20 20 20 20  zBuf[4096];.    
34f0a 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45 78  exprToString(pEx
34f0b 70 72 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 73  pr, zBuf);.    s
34f0c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
34f0d 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66  xt(context, zBuf
34f0e 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41  , -1, SQLITE_TRA
34f0f 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c  NSIENT);.    sql
34f10 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
34f11 28 70 45 78 70 72 29 3b 0a 20 20 7d 65 6c 73 65  (pExpr);.  }else
34f12 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
34f13 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
34f14 78 74 2c 20 22 45 72 72 6f 72 20 70 61 72 73 69  xt, "Error parsi
34f15 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 22 2c 20  ng expression", 
34f16 2d 31 29 3b 0a 20 20 7d 0a 0a 65 78 70 72 74 65  -1);.  }..exprte
34f17 73 74 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 4d  st_out:.  if( pM
34f18 6f 64 75 6c 65 20 26 26 20 70 54 6f 6b 65 6e 69  odule && pTokeni
34f19 7a 65 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  zer ){.    rc = 
34f1a 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f  pModule->xDestro
34f1b 79 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20  y(pTokenizer);. 
34f1c 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
34f1d 65 28 61 7a 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a  e(azCol);.}../*.
34f1e 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20  ** Register the 
34f1f 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  query expression
34f20 20 70 61 72 73 65 72 20 74 65 73 74 20 66 75 6e   parser test fun
34f21 63 74 69 6f 6e 20 66 74 73 33 5f 65 78 70 72 74  ction fts3_exprt
34f22 65 73 74 28 29 20 0a 2a 2a 20 77 69 74 68 20 64  est() .** with d
34f23 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
34f24 6f 6e 20 64 62 2e 20 0a 2a 2f 0a 53 51 4c 49 54  on db. .*/.SQLIT
34f25 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
34f26 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e  qlite3Fts3ExprIn
34f27 69 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28  itTestInterface(
34f28 73 71 6c 69 74 65 33 2a 20 64 62 29 7b 0a 20 20  sqlite3* db){.  
34f29 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
34f2a 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 64  unction(.      d
34f2b 62 2c 20 22 66 74 73 33 5f 65 78 70 72 74 65 73  b, "fts3_exprtes
34f2c 74 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55  t", -1, SQLITE_U
34f2d 54 46 38 2c 20 30 2c 20 66 74 73 33 45 78 70 72  TF8, 0, fts3Expr
34f2e 54 65 73 74 2c 20 30 2c 20 30 0a 20 20 29 3b 0a  Test, 0, 0.  );.
34f2f 7d 0a 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  }..#endif.#endif
34f30 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
34f31 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
34f32 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
34f33 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a  LE_FTS3) */../**
34f34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
34f35 20 6f 66 20 66 74 73 33 5f 65 78 70 72 2e 63 20   of fts3_expr.c 
34f36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
34f39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
34f3a 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 68 61 73  in file fts3_has
34f3b 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
34f3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
34f3e 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
34f3f 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 22.**.** The a
34f40 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
34f41 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
34f42 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
34f43 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
34f44 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
34f45 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
34f46 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
34f47 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
34f48 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
34f49 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
34f4a 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
34f4b 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
34f4c 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
34f4d 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
34f4e 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
34f4f 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
34f50 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
34f51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f55 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ***.** This is t
34f56 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
34f57 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73  n of generic has
34f58 68 2d 74 61 62 6c 65 73 20 75 73 65 64 20 69 6e  h-tables used in
34f59 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 57 65 27 76   SQLite..** We'v
34f5a 65 20 6d 6f 64 69 66 69 65 64 20 69 74 20 73 6c  e modified it sl
34f5b 69 67 68 74 6c 79 20 74 6f 20 73 65 72 76 65 20  ightly to serve 
34f5c 61 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20  as a standalone 
34f5d 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 69 6d  hash table.** im
34f5e 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
34f5f 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69   the full-text i
34f60 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a  ndexing module..
34f61 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
34f62 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
34f63 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
34f64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a   if:.**.**     *
34f65 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
34f66 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
34f67 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  as an extension.
34f68 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68 69  **       (in whi
34f69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43  ch case SQLITE_C
34f6a 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  ORE is not defin
34f6b 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20  ed), or.**.**   
34f6c 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
34f6d 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
34f6e 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65  lt into the core
34f6f 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c   of.**       SQL
34f70 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61  ite (in which ca
34f71 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  se SQLITE_ENABLE
34f72 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64  _FTS3 is defined
34f73 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  )..*/.#if !defin
34f74 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
34f75 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
34f76 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a  E_ENABLE_FTS3)..
34f77 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 61  ../*.** Malloc a
34f78 6e 64 20 46 72 65 65 20 66 75 6e 63 74 69 6f 6e  nd Free function
34f79 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s.*/.static void
34f7a 20 2a 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63   *fts3HashMalloc
34f7b 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20  (int n){.  void 
34f7c 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  *p = sqlite3_mal
34f7d 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20  loc(n);.  if( p 
34f7e 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c  ){.    memset(p,
34f7f 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65   0, n);.  }.  re
34f80 74 75 72 6e 20 70 3b 0a 7d 0a 73 74 61 74 69 63  turn p;.}.static
34f81 20 76 6f 69 64 20 66 74 73 33 48 61 73 68 46 72   void fts3HashFr
34f82 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73  ee(void *p){.  s
34f83 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
34f84 7d 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20  }../* Turn bulk 
34f85 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61  memory into a ha
34f86 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20  sh table object 
34f87 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  by initializing 
34f88 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66  the.** fields of
34f89 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74   the Hash struct
34f8a 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77  ure..**.** "pNew
34f8b 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  " is a pointer t
34f8c 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
34f8d 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69   that is to be i
34f8e 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6b  nitialized..** k
34f8f 65 79 43 6c 61 73 73 20 69 73 20 6f 6e 65 20 6f  eyClass is one o
34f90 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20  f the constants 
34f91 0a 2a 2a 20 46 54 53 33 5f 48 41 53 48 5f 42 49  .** FTS3_HASH_BI
34f92 4e 41 52 59 20 6f 72 20 46 54 53 33 5f 48 41 53  NARY or FTS3_HAS
34f93 48 5f 53 54 52 49 4e 47 2e 20 20 54 68 65 20 76  H_STRING.  The v
34f94 61 6c 75 65 20 6f 66 20 6b 65 79 43 6c 61 73 73  alue of keyClass
34f95 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 73 20   .** determines 
34f96 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6b 65 79  what kind of key
34f97 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
34f98 77 69 6c 6c 20 75 73 65 2e 20 20 22 63 6f 70 79  will use.  "copy
34f99 4b 65 79 22 20 69 73 0a 2a 2a 20 74 72 75 65 20  Key" is.** true 
34f9a 69 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  if the hash tabl
34f9b 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74  e should make it
34f9c 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f  s own private co
34f9d 70 79 20 6f 66 20 6b 65 79 73 20 61 6e 64 0a 2a  py of keys and.*
34f9e 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 73 68  * false if it sh
34f9f 6f 75 6c 64 20 6a 75 73 74 20 75 73 65 20 74 68  ould just use th
34fa0 65 20 73 75 70 70 6c 69 65 64 20 70 6f 69 6e 74  e supplied point
34fa1 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
34fa2 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
34fa3 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 66  e3Fts3HashInit(f
34fa4 74 73 33 48 61 73 68 20 2a 70 4e 65 77 2c 20 69  ts3Hash *pNew, i
34fa5 6e 74 20 6b 65 79 43 6c 61 73 73 2c 20 69 6e 74  nt keyClass, int
34fa6 20 63 6f 70 79 4b 65 79 29 7b 0a 20 20 61 73 73   copyKey){.  ass
34fa7 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a  ert( pNew!=0 );.
34fa8 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61    assert( keyCla
34fa9 73 73 3e 3d 46 54 53 33 5f 48 41 53 48 5f 53 54  ss>=FTS3_HASH_ST
34faa 52 49 4e 47 20 26 26 20 6b 65 79 43 6c 61 73 73  RING && keyClass
34fab 3c 3d 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41  <=FTS3_HASH_BINA
34fac 52 59 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 6b 65  RY );.  pNew->ke
34fad 79 43 6c 61 73 73 20 3d 20 6b 65 79 43 6c 61 73  yClass = keyClas
34fae 73 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 4b  s;.  pNew->copyK
34faf 65 79 20 3d 20 63 6f 70 79 4b 65 79 3b 0a 20 20  ey = copyKey;.  
34fb0 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20 30 3b  pNew->first = 0;
34fb1 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d  .  pNew->count =
34fb2 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 73 69   0;.  pNew->htsi
34fb3 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  ze = 0;.  pNew->
34fb4 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65  ht = 0;.}../* Re
34fb5 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69 65 73  move all entries
34fb6 20 66 72 6f 6d 20 61 20 68 61 73 68 20 74 61 62   from a hash tab
34fb7 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c  le.  Reclaim all
34fb8 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 6c 6c   memory..** Call
34fb9 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
34fba 20 64 65 6c 65 74 65 20 61 20 68 61 73 68 20 74   delete a hash t
34fbb 61 62 6c 65 20 6f 72 20 74 6f 20 72 65 73 65 74  able or to reset
34fbc 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a   a hash table.**
34fbd 20 74 6f 20 74 68 65 20 65 6d 70 74 79 20 73 74   to the empty st
34fbe 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ate..*/.SQLITE_P
34fbf 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
34fc0 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72  te3Fts3HashClear
34fc1 28 66 74 73 33 48 61 73 68 20 2a 70 48 29 7b 0a  (fts3Hash *pH){.
34fc2 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
34fc3 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  elem;         /*
34fc4 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
34fc5 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f  r all elements o
34fc6 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a  f the table */..
34fc7 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20    assert( pH!=0 
34fc8 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e  );.  elem = pH->
34fc9 66 69 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72  first;.  pH->fir
34fca 73 74 20 3d 20 30 3b 0a 20 20 66 74 73 33 48 61  st = 0;.  fts3Ha
34fcb 73 68 46 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a  shFree(pH->ht);.
34fcc 20 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20    pH->ht = 0;.  
34fcd 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a  pH->htsize = 0;.
34fce 20 20 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b    while( elem ){
34fcf 0a 20 20 20 20 66 74 73 33 48 61 73 68 45 6c 65  .    fts3HashEle
34fd0 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65  m *next_elem = e
34fd1 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69  lem->next;.    i
34fd2 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26  f( pH->copyKey &
34fd3 26 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a  & elem->pKey ){.
34fd4 20 20 20 20 20 20 66 74 73 33 48 61 73 68 46 72        fts3HashFr
34fd5 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a  ee(elem->pKey);.
34fd6 20 20 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61      }.    fts3Ha
34fd7 73 68 46 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20  shFree(elem);.  
34fd8 20 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c    elem = next_el
34fd9 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f  em;.  }.  pH->co
34fda 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  unt = 0;.}../*.*
34fdb 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61  * Hash and compa
34fdc 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20  rison functions 
34fdd 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73  when the mode is
34fde 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e   FTS3_HASH_STRIN
34fdf 47 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  G.*/.static int 
34fe0 66 74 73 33 53 74 72 48 61 73 68 28 63 6f 6e 73  fts3StrHash(cons
34fe1 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
34fe2 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e 73 74  t nKey){.  const
34fe3 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73   char *z = (cons
34fe4 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20  t char *)pKey;. 
34fe5 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 69 66   int h = 0;.  if
34fe6 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b 65 79  ( nKey<=0 ) nKey
34fe7 20 3d 20 28 69 6e 74 29 20 73 74 72 6c 65 6e 28   = (int) strlen(
34fe8 7a 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65  z);.  while( nKe
34fe9 79 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 20  y > 0  ){.    h 
34fea 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a  = (h<<3) ^ h ^ *
34feb 7a 2b 2b 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b  z++;.    nKey--;
34fec 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20  .  }.  return h 
34fed 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a  & 0x7fffffff;.}.
34fee 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
34fef 74 72 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  trCompare(const 
34ff0 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74  void *pKey1, int
34ff1 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20   n1, const void 
34ff2 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b  *pKey2, int n2){
34ff3 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29 20  .  if( n1!=n2 ) 
34ff4 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75  return 1;.  retu
34ff5 72 6e 20 73 74 72 6e 63 6d 70 28 28 63 6f 6e 73  rn strncmp((cons
34ff6 74 20 63 68 61 72 2a 29 70 4b 65 79 31 2c 28 63  t char*)pKey1,(c
34ff7 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79 32  onst char*)pKey2
34ff8 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48  ,n1);.}../*.** H
34ff9 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69 73  ash and comparis
34ffa 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65  on functions whe
34ffb 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20 46 54  n the mode is FT
34ffc 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 0a 2a  S3_HASH_BINARY.*
34ffd 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
34ffe 33 42 69 6e 48 61 73 68 28 63 6f 6e 73 74 20 76  3BinHash(const v
34fff 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
35000 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 20 3d 20  Key){.  int h = 
35001 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  0;.  const char 
35002 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  *z = (const char
35003 20 2a 29 70 4b 65 79 3b 0a 20 20 77 68 69 6c 65   *)pKey;.  while
35004 28 20 6e 4b 65 79 2d 2d 20 3e 20 30 20 29 7b 0a  ( nKey-- > 0 ){.
35005 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e      h = (h<<3) ^
35006 20 68 20 5e 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d   h ^ *(z++);.  }
35007 0a 20 20 72 65 74 75 72 6e 20 68 20 26 20 30 78  .  return h & 0x
35008 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74  7fffffff;.}.stat
35009 69 63 20 69 6e 74 20 66 74 73 33 42 69 6e 43 6f  ic int fts3BinCo
3500a 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64  mpare(const void
3500b 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c   *pKey1, int n1,
3500c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
3500d 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69  y2, int n2){.  i
3500e 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75  f( n1!=n2 ) retu
3500f 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 6d  rn 1;.  return m
35010 65 6d 63 6d 70 28 70 4b 65 79 31 2c 70 4b 65 79  emcmp(pKey1,pKey
35011 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  2,n1);.}../*.** 
35012 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
35013 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69   to the appropri
35014 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  ate hash functio
35015 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20  n given the key 
35016 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  class..**.** The
35017 20 43 20 73 79 6e 74 61 78 20 69 6e 20 74 68 69   C syntax in thi
35018 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  s function defin
35019 69 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 6e 66  ition may be unf
3501a 61 6d 69 6c 61 72 20 74 6f 20 73 6f 6d 65 20 0a  amilar to some .
3501b 2a 2a 20 70 72 6f 67 72 61 6d 6d 65 72 73 2c 20  ** programmers, 
3501c 73 6f 20 77 65 20 70 72 6f 76 69 64 65 20 74 68  so we provide th
3501d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 64 64 69  e following addi
3501e 74 69 6f 6e 61 6c 20 65 78 70 6c 61 6e 61 74 69  tional explanati
3501f 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61  on:.**.** The na
35020 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  me of the functi
35021 6f 6e 20 69 73 20 22 66 74 73 48 61 73 68 46 75  on is "ftsHashFu
35022 6e 63 74 69 6f 6e 22 2e 20 20 54 68 65 20 66 75  nction".  The fu
35023 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 0a 2a  nction takes a.*
35024 2a 20 73 69 6e 67 6c 65 20 70 61 72 61 6d 65 74  * single paramet
35025 65 72 20 22 6b 65 79 43 6c 61 73 73 22 2e 20 20  er "keyClass".  
35026 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
35027 20 6f 66 20 66 74 73 48 61 73 68 46 75 6e 63 74   of ftsHashFunct
35028 69 6f 6e 28 29 0a 2a 2a 20 69 73 20 61 20 70 6f  ion().** is a po
35029 69 6e 74 65 72 20 74 6f 20 61 6e 6f 74 68 65 72  inter to another
3502a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 70 65 63   function.  Spec
3502b 69 66 69 63 61 6c 6c 79 2c 20 74 68 65 20 72 65  ifically, the re
3502c 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66  turn value.** of
3502d 20 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e   ftsHashFunction
3502e 28 29 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  () is a pointer 
3502f 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68  to a function th
35030 61 74 20 74 61 6b 65 73 20 74 77 6f 20 70 61 72  at takes two par
35031 61 6d 65 74 65 72 73 0a 2a 2a 20 77 69 74 68 20  ameters.** with 
35032 74 79 70 65 73 20 22 63 6f 6e 73 74 20 76 6f 69  types "const voi
35033 64 2a 22 20 61 6e 64 20 22 69 6e 74 22 20 61 6e  d*" and "int" an
35034 64 20 72 65 74 75 72 6e 73 20 61 6e 20 22 69 6e  d returns an "in
35035 74 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  t"..*/.static in
35036 74 20 28 2a 66 74 73 48 61 73 68 46 75 6e 63 74  t (*ftsHashFunct
35037 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73 73  ion(int keyClass
35038 29 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  ))(const void*,i
35039 6e 74 29 7b 0a 20 20 69 66 28 20 6b 65 79 43 6c  nt){.  if( keyCl
3503a 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 53  ass==FTS3_HASH_S
3503b 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 72 65 74  TRING ){.    ret
3503c 75 72 6e 20 26 66 74 73 33 53 74 72 48 61 73 68  urn &fts3StrHash
3503d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
3503e 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d  ssert( keyClass=
3503f 3d 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52  =FTS3_HASH_BINAR
35040 59 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  Y );.    return 
35041 26 66 74 73 33 42 69 6e 48 61 73 68 3b 0a 20 20  &fts3BinHash;.  
35042 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
35043 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
35044 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 68  he appropriate h
35045 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76  ash function giv
35046 65 6e 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73  en the key class
35047 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 68 65 6c 70  ..**.** For help
35048 20 69 6e 20 69 6e 74 65 72 70 72 65 74 65 64 20   in interpreted 
35049 74 68 65 20 6f 62 73 63 75 72 65 20 43 20 63 6f  the obscure C co
3504a 64 65 20 69 6e 20 74 68 65 20 66 75 6e 63 74 69  de in the functi
3504b 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 0a 2a  on definition,.*
3504c 2a 20 73 65 65 20 74 68 65 20 68 65 61 64 65 72  * see the header
3504d 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20   comment on the 
3504e 70 72 65 76 69 6f 75 73 20 66 75 6e 63 74 69 6f  previous functio
3504f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
35050 20 28 2a 66 74 73 43 6f 6d 70 61 72 65 46 75 6e   (*ftsCompareFun
35051 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61  ction(int keyCla
35052 73 73 29 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  ss))(const void*
35053 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
35054 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20 6b 65 79  ,int){.  if( key
35055 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48  Class==FTS3_HASH
35056 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 72  _STRING ){.    r
35057 65 74 75 72 6e 20 26 66 74 73 33 53 74 72 43 6f  eturn &fts3StrCo
35058 6d 70 61 72 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  mpare;.  }else{.
35059 20 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43      assert( keyC
3505a 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f  lass==FTS3_HASH_
3505b 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65  BINARY );.    re
3505c 74 75 72 6e 20 26 66 74 73 33 42 69 6e 43 6f 6d  turn &fts3BinCom
3505d 70 61 72 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  pare;.  }.}../* 
3505e 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e 74 20  Link an element 
3505f 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  into the hash ta
35060 62 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ble.*/.static vo
35061 69 64 20 66 74 73 33 48 61 73 68 49 6e 73 65 72  id fts3HashInser
35062 74 45 6c 65 6d 65 6e 74 28 0a 20 20 66 74 73 33  tElement(.  fts3
35063 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20  Hash *pH,       
35064 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70       /* The comp
35065 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20  lete hash table 
35066 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73  */.  struct _fts
35067 33 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 2f 2a  3ht *pEntry,  /*
35068 20 54 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20   The entry into 
35069 77 68 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e  which pNew is in
3506a 73 65 72 74 65 64 20 2a 2f 0a 20 20 66 74 73 33  serted */.  fts3
3506b 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20  HashElem *pNew  
3506c 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d       /* The elem
3506d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ent to be insert
3506e 65 64 20 2a 2f 0a 29 7b 0a 20 20 66 74 73 33 48  ed */.){.  fts3H
3506f 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20  ashElem *pHead; 
35070 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65      /* First ele
35071 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20  ment already in 
35072 70 45 6e 74 72 79 20 2a 2f 0a 20 20 70 48 65 61  pEntry */.  pHea
35073 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  d = pEntry->chai
35074 6e 3b 0a 20 20 69 66 28 20 70 48 65 61 64 20 29  n;.  if( pHead )
35075 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74  {.    pNew->next
35076 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e   = pHead;.    pN
35077 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64  ew->prev = pHead
35078 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20  ->prev;.    if( 
35079 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70  pHead->prev ){ p
3507a 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74  Head->prev->next
3507b 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65   = pNew; }.    e
3507c 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
3507d 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e  { pH->first = pN
3507e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d  ew; }.    pHead-
3507f 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20  >prev = pNew;.  
35080 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d  }else{.    pNew-
35081 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73  >next = pH->firs
35082 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66  t;.    if( pH->f
35083 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73  irst ){ pH->firs
35084 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20  t->prev = pNew; 
35085 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76  }.    pNew->prev
35086 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69   = 0;.    pH->fi
35087 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  rst = pNew;.  }.
35088 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b    pEntry->count+
35089 2b 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 63 68 61  +;.  pEntry->cha
3508a 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 0a 2f  in = pNew;.}.../
3508b 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68 61 73  * Resize the has
3508c 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20  h table so that 
3508d 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e 65 77  it cantains "new
3508e 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73 2e 0a  _size" buckets..
3508f 2a 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20 6d 75  ** "new_size" mu
35090 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66  st be a power of
35091 20 32 2e 20 20 54 68 65 20 68 61 73 68 20 74 61   2.  The hash ta
35092 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c 20 0a  ble might fail .
35093 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20  ** to resize if 
35094 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 66  sqliteMalloc() f
35095 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ails..*/.static 
35096 76 6f 69 64 20 66 74 73 33 52 65 68 61 73 68 28  void fts3Rehash(
35097 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20 69 6e  fts3Hash *pH, in
35098 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73  t new_size){.  s
35099 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 6e  truct _fts3ht *n
3509a 65 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20  ew_ht;          
3509b 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 73 68 20  /* The new hash 
3509c 74 61 62 6c 65 20 2a 2f 0a 20 20 66 74 73 33 48  table */.  fts3H
3509d 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a  ashElem *elem, *
3509e 6e 65 78 74 5f 65 6c 65 6d 3b 20 20 2f 2a 20 46  next_elem;  /* F
3509f 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
350a0 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74  existing element
350a1 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61  s */.  int (*xHa
350a2 73 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  sh)(const void*,
350a3 69 6e 74 29 3b 20 20 20 2f 2a 20 54 68 65 20 68  int);   /* The h
350a4 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ash function */.
350a5 0a 20 20 61 73 73 65 72 74 28 20 28 6e 65 77 5f  .  assert( (new_
350a6 73 69 7a 65 20 26 20 28 6e 65 77 5f 73 69 7a 65  size & (new_size
350a7 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 6e 65 77  -1))==0 );.  new
350a8 5f 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f 66  _ht = (struct _f
350a9 74 73 33 68 74 20 2a 29 66 74 73 33 48 61 73 68  ts3ht *)fts3Hash
350aa 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65  Malloc( new_size
350ab 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  *sizeof(struct _
350ac 66 74 73 33 68 74 29 20 29 3b 0a 20 20 69 66 28  fts3ht) );.  if(
350ad 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20 72 65 74   new_ht==0 ) ret
350ae 75 72 6e 3b 0a 20 20 66 74 73 33 48 61 73 68 46  urn;.  fts3HashF
350af 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70  ree(pH->ht);.  p
350b0 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a  H->ht = new_ht;.
350b1 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e    pH->htsize = n
350b2 65 77 5f 73 69 7a 65 3b 0a 20 20 78 48 61 73 68  ew_size;.  xHash
350b3 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74 69   = ftsHashFuncti
350b4 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29  on(pH->keyClass)
350b5 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d  ;.  for(elem=pH-
350b6 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73  >first, pH->firs
350b7 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20  t=0; elem; elem 
350b8 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20  = next_elem){.  
350b9 20 20 69 6e 74 20 68 20 3d 20 28 2a 78 48 61 73    int h = (*xHas
350ba 68 29 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65  h)(elem->pKey, e
350bb 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26 20 28 6e 65  lem->nKey) & (ne
350bc 77 5f 73 69 7a 65 2d 31 29 3b 0a 20 20 20 20 6e  w_size-1);.    n
350bd 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d  ext_elem = elem-
350be 3e 6e 65 78 74 3b 0a 20 20 20 20 66 74 73 33 48  >next;.    fts3H
350bf 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74  ashInsertElement
350c0 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c  (pH, &new_ht[h],
350c1 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   elem);.  }.}../
350c2 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
350c3 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73  (for internal us
350c4 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20  e only) locates 
350c5 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e  an element in an
350c6 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74  .** hash table t
350c7 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20  hat matches the 
350c8 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20  given key.  The 
350c9 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65  hash for this ke
350ca 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  y has.** already
350cb 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61   been computed a
350cc 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 73 20  nd is passed as 
350cd 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
350ce 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66 74 73  r..*/.static fts
350cf 33 48 61 73 68 45 6c 65 6d 20 2a 66 74 73 33 46  3HashElem *fts3F
350d0 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61 73 68  indElementByHash
350d1 28 0a 20 20 63 6f 6e 73 74 20 66 74 73 33 48 61  (.  const fts3Ha
350d2 73 68 20 2a 70 48 2c 20 2f 2a 20 54 68 65 20 70  sh *pH, /* The p
350d3 48 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64  H to be searched
350d4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
350d5 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65   *pKey,   /* The
350d6 20 6b 65 79 20 77 65 20 61 72 65 20 73 65 61 72   key we are sear
350d7 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 69  ching for */.  i
350d8 6e 74 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 68  nt nKey,.  int h
350d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
350da 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74  * The hash for t
350db 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20  his key. */.){. 
350dc 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65   fts3HashElem *e
350dd 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  lem;            
350de 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
350df 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
350e0 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63   list */.  int c
350e1 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
350e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
350e3 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
350e4 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a  left to test */.
350e5 20 20 69 6e 74 20 28 2a 78 43 6f 6d 70 61 72 65    int (*xCompare
350e6 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
350e7 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
350e8 74 29 3b 20 20 2f 2a 20 63 6f 6d 70 61 72 69 73  t);  /* comparis
350e9 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a  on function */..
350ea 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a    if( pH->ht ){.
350eb 20 20 20 20 73 74 72 75 63 74 20 5f 66 74 73 33      struct _fts3
350ec 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48  ht *pEntry = &pH
350ed 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65  ->ht[h];.    ele
350ee 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  m = pEntry->chai
350ef 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70  n;.    count = p
350f0 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20  Entry->count;.  
350f1 20 20 78 43 6f 6d 70 61 72 65 20 3d 20 66 74 73    xCompare = fts
350f2 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28  CompareFunction(
350f3 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20  pH->keyClass);. 
350f4 20 20 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d     while( count-
350f5 2d 20 26 26 20 65 6c 65 6d 20 29 7b 0a 20 20 20  - && elem ){.   
350f6 20 20 20 69 66 28 20 28 2a 78 43 6f 6d 70 61 72     if( (*xCompar
350f7 65 29 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c  e)(elem->pKey,el
350f8 65 6d 2d 3e 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b  em->nKey,pKey,nK
350f9 65 79 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20  ey)==0 ){ .     
350fa 20 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a     return elem;.
350fb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 6c        }.      el
350fc 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  em = elem->next;
350fd 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
350fe 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d  urn 0;.}../* Rem
350ff 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74  ove a single ent
35100 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  ry from the hash
35101 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70   table given a p
35102 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a  ointer to that.*
35103 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20  * element and a 
35104 68 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d  hash on the elem
35105 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74  ent's key..*/.st
35106 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 52 65  atic void fts3Re
35107 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79 48 61 73  moveElementByHas
35108 68 28 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  h(.  fts3Hash *p
35109 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  H,         /* Th
3510a 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e 67 20  e pH containing 
3510b 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 66 74 73 33  "elem" */.  fts3
3510c 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20  HashElem* elem, 
3510d 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74    /* The element
3510e 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66   to be removed f
3510f 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20  rom the pH */.  
35110 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20  int h           
35111 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61        /* Hash va
35112 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d  lue for the elem
35113 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75  ent */.){.  stru
35114 63 74 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74  ct _fts3ht *pEnt
35115 72 79 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e  ry;.  if( elem->
35116 70 72 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d  prev ){.    elem
35117 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65  ->prev->next = e
35118 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65  lem->next; .  }e
35119 6c 73 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72  lse{.    pH->fir
3511a 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  st = elem->next;
3511b 0a 20 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d  .  }.  if( elem-
3511c 3e 6e 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65  >next ){.    ele
3511d 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20  m->next->prev = 
3511e 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a  elem->prev;.  }.
3511f 20 20 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e    pEntry = &pH->
35120 68 74 5b 68 5d 3b 0a 20 20 69 66 28 20 70 45 6e  ht[h];.  if( pEn
35121 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d  try->chain==elem
35122 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e   ){.    pEntry->
35123 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65  chain = elem->ne
35124 78 74 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79  xt;.  }.  pEntry
35125 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28  ->count--;.  if(
35126 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d   pEntry->count<=
35127 30 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d  0 ){.    pEntry-
35128 3e 63 68 61 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a  >chain = 0;.  }.
35129 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65    if( pH->copyKe
3512a 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79 20  y && elem->pKey 
3512b 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73 68 46  ){.    fts3HashF
3512c 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b  ree(elem->pKey);
3512d 0a 20 20 7d 0a 20 20 66 74 73 33 48 61 73 68 46  .  }.  fts3HashF
3512e 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70  ree( elem );.  p
3512f 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66  H->count--;.  if
35130 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29  ( pH->count<=0 )
35131 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48  {.    assert( pH
35132 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20  ->first==0 );.  
35133 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f    assert( pH->co
35134 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 66 74  unt==0 );.    ft
35135 73 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b  s3HashClear(pH);
35136 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d  .  }.}../* Attem
35137 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20  pt to locate an 
35138 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68  element of the h
35139 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74  ash table pH wit
3513a 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20  h a key.** that 
3513b 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65  matches pKey,nKe
3513c 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 64  y.  Return the d
3513d 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65  ata for this ele
3513e 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a  ment if it is.**
3513f 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20   found, or NULL 
35140 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
35141 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  atch..*/.SQLITE_
35142 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
35143 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e  lite3Fts3HashFin
35144 64 28 63 6f 6e 73 74 20 66 74 73 33 48 61 73 68  d(const fts3Hash
35145 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *pH, const void
35146 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
35147 29 7b 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20  ){.  int h;     
35148 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
35149 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a   hash on key */.
3514a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
3514b 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20  elem;    /* The 
3514c 65 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74  element that mat
3514d 63 68 65 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  ches key */.  in
3514e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74  t (*xHash)(const
3514f 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a   void*,int);  /*
35150 20 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69   The hash functi
35151 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 3d  on */..  if( pH=
35152 3d 30 20 7c 7c 20 70 48 2d 3e 68 74 3d 3d 30 20  =0 || pH->ht==0 
35153 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 78 48  ) return 0;.  xH
35154 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e  ash = ftsHashFun
35155 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61  ction(pH->keyCla
35156 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78  ss);.  assert( x
35157 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 20 3d  Hash!=0 );.  h =
35158 20 28 2a 78 48 61 73 68 29 28 70 4b 65 79 2c 6e   (*xHash)(pKey,n
35159 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Key);.  assert( 
3515a 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70  (pH->htsize & (p
3515b 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30  H->htsize-1))==0
3515c 20 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 74 73   );.  elem = fts
3515d 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61  3FindElementByHa
3515e 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c  sh(pH,pKey,nKey,
3515f 20 68 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65   h & (pH->htsize
35160 2d 31 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 65  -1));.  return e
35161 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61  lem ? elem->data
35162 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65   : 0;.}../* Inse
35163 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e  rt an element in
35164 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
35165 65 20 70 48 2e 20 20 54 68 65 20 6b 65 79 20 69  e pH.  The key i
35166 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61  s pKey,nKey.** a
35167 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 20 22  nd the data is "
35168 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  data"..**.** If 
35169 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69 73 74  no element exist
3516a 73 20 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e  s with a matchin
3516b 67 20 6b 65 79 2c 20 74 68 65 6e 20 61 20 6e 65  g key, then a ne
3516c 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20  w.** element is 
3516d 63 72 65 61 74 65 64 2e 20 20 41 20 63 6f 70 79  created.  A copy
3516e 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 6d   of the key is m
3516f 61 64 65 20 69 66 20 74 68 65 20 63 6f 70 79 4b  ade if the copyK
35170 65 79 0a 2a 2a 20 66 6c 61 67 20 69 73 20 73 65  ey.** flag is se
35171 74 2e 20 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  t.  NULL is retu
35172 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rned..**.** If a
35173 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 61  nother element a
35174 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 77 69  lready exists wi
35175 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79 2c  th the same key,
35176 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77   then the.** new
35177 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 20 74   data replaces t
35178 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 20  he old data and 
35179 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20  the old data is 
3517a 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65  returned..** The
3517b 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 69   key is not copi
3517c 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 61  ed in this insta
3517d 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f  nce.  If a mallo
3517e 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a  c fails, then.**
3517f 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 73   the new data is
35180 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
35181 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
35182 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  unchanged..**.**
35183 20 49 66 20 74 68 65 20 22 64 61 74 61 22 20 70   If the "data" p
35184 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73  arameter to this
35185 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c   function is NUL
35186 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65  L, then the.** e
35187 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e  lement correspon
35188 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 73  ding to "key" is
35189 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
3518a 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f  e hash table..*/
3518b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3518c 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73  void *sqlite3Fts
3518d 33 48 61 73 68 49 6e 73 65 72 74 28 0a 20 20 66  3HashInsert(.  f
3518e 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20 20 20  ts3Hash *pH,    
3518f 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20      /* The hash 
35190 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20  table to insert 
35191 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  into */.  const 
35192 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 20 2f  void *pKey,    /
35193 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69  * The key */.  i
35194 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20  nt nKey,        
35195 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
35196 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65   bytes in the ke
35197 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74  y */.  void *dat
35198 61 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  a           /* T
35199 68 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20  he data */.){.  
3519a 69 6e 74 20 68 72 61 77 3b 20 20 20 20 20 20 20  int hraw;       
3519b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 61 77            /* Raw
3519c 20 68 61 73 68 20 76 61 6c 75 65 20 6f 66 20 74   hash value of t
3519d 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  he key */.  int 
3519e 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
3519f 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73        /* the has
351a0 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64  h of the key mod
351a1 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73  ulo hash table s
351a2 69 7a 65 20 2a 2f 0a 20 20 66 74 73 33 48 61 73  ize */.  fts3Has
351a3 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
351a4 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f     /* Used to lo
351a5 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d  op thru the elem
351a6 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 66 74  ent list */.  ft
351a7 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f  s3HashElem *new_
351a8 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65  elem;   /* New e
351a9 6c 65 6d 65 6e 74 20 61 64 64 65 64 20 74 6f 20  lement added to 
351aa 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74 20  the pH */.  int 
351ab 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76  (*xHash)(const v
351ac 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54  oid*,int);  /* T
351ad 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  he hash function
351ae 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
351af 48 21 3d 30 20 29 3b 0a 20 20 78 48 61 73 68 20  H!=0 );.  xHash 
351b0 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f  = ftsHashFunctio
351b1 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b  n(pH->keyClass);
351b2 0a 20 20 61 73 73 65 72 74 28 20 78 48 61 73 68  .  assert( xHash
351b3 21 3d 30 20 29 3b 0a 20 20 68 72 61 77 20 3d 20  !=0 );.  hraw = 
351b4 28 2a 78 48 61 73 68 29 28 70 4b 65 79 2c 20 6e  (*xHash)(pKey, n
351b5 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Key);.  assert( 
351b6 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70  (pH->htsize & (p
351b7 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30  H->htsize-1))==0
351b8 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 26   );.  h = hraw &
351b9 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b   (pH->htsize-1);
351ba 0a 20 20 65 6c 65 6d 20 3d 20 66 74 73 33 46 69  .  elem = fts3Fi
351bb 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28  ndElementByHash(
351bc 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b  pH,pKey,nKey,h);
351bd 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20  .  if( elem ){. 
351be 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74     void *old_dat
351bf 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a  a = elem->data;.
351c0 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20      if( data==0 
351c1 29 7b 0a 20 20 20 20 20 20 66 74 73 33 52 65 6d  ){.      fts3Rem
351c2 6f 76 65 45 6c 65 6d 65 6e 74 42 79 48 61 73 68  oveElementByHash
351c3 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20  (pH,elem,h);.   
351c4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c   }else{.      el
351c5 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b  em->data = data;
351c6 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
351c7 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a  n old_data;.  }.
351c8 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 20    if( data==0 ) 
351c9 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
351ca 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b  pH->htsize==0 ){
351cb 0a 20 20 20 20 66 74 73 33 52 65 68 61 73 68 28  .    fts3Rehash(
351cc 70 48 2c 38 29 3b 0a 20 20 20 20 69 66 28 20 70  pH,8);.    if( p
351cd 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a  H->htsize==0 ){.
351ce 20 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e 74 20        pH->count 
351cf 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 0;.      retur
351d0 6e 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20  n data;.    }.  
351d1 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28  }.  new_elem = (
351d2 66 74 73 33 48 61 73 68 45 6c 65 6d 2a 29 66 74  fts3HashElem*)ft
351d3 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 73 69  s3HashMalloc( si
351d4 7a 65 6f 66 28 66 74 73 33 48 61 73 68 45 6c 65  zeof(fts3HashEle
351d5 6d 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f  m) );.  if( new_
351d6 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e  elem==0 ) return
351d7 20 64 61 74 61 3b 0a 20 20 69 66 28 20 70 48 2d   data;.  if( pH-
351d8 3e 63 6f 70 79 4b 65 79 20 26 26 20 70 4b 65 79  >copyKey && pKey
351d9 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 77 5f 65  !=0 ){.    new_e
351da 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 66 74 73 33  lem->pKey = fts3
351db 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 4b 65 79  HashMalloc( nKey
351dc 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 77 5f   );.    if( new_
351dd 65 6c 65 6d 2d 3e 70 4b 65 79 3d 3d 30 20 29 7b  elem->pKey==0 ){
351de 0a 20 20 20 20 20 20 66 74 73 33 48 61 73 68 46  .      fts3HashF
351df 72 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20  ree(new_elem);. 
351e0 20 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61       return data
351e1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63  ;.    }.    memc
351e2 70 79 28 28 76 6f 69 64 2a 29 6e 65 77 5f 65 6c  py((void*)new_el
351e3 65 6d 2d 3e 70 4b 65 79 2c 20 70 4b 65 79 2c 20  em->pKey, pKey, 
351e4 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nKey);.  }else{.
351e5 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b      new_elem->pK
351e6 65 79 20 3d 20 28 76 6f 69 64 2a 29 70 4b 65 79  ey = (void*)pKey
351e7 3b 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d  ;.  }.  new_elem
351e8 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a 20  ->nKey = nKey;. 
351e9 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20   pH->count++;.  
351ea 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20  if( pH->count > 
351eb 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20  pH->htsize ){.  
351ec 20 20 66 74 73 33 52 65 68 61 73 68 28 70 48 2c    fts3Rehash(pH,
351ed 70 48 2d 3e 68 74 73 69 7a 65 2a 32 29 3b 0a 20  pH->htsize*2);. 
351ee 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 48 2d   }.  assert( pH-
351ef 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 61  >htsize>0 );.  a
351f0 73 73 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69  ssert( (pH->htsi
351f1 7a 65 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65  ze & (pH->htsize
351f2 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d  -1))==0 );.  h =
351f3 20 68 72 61 77 20 26 20 28 70 48 2d 3e 68 74 73   hraw & (pH->hts
351f4 69 7a 65 2d 31 29 3b 0a 20 20 66 74 73 33 48 61  ize-1);.  fts3Ha
351f5 73 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28  shInsertElement(
351f6 70 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20  pH, &pH->ht[h], 
351f7 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 6e 65 77  new_elem);.  new
351f8 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61  _elem->data = da
351f9 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  ta;.  return 0;.
351fa 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
351fb 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
351fc 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
351fd 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
351fe 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
351ff 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
35200 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a  3_hash.c *******
35201 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35202 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35203 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
35204 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
35205 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63 20 2a   fts3_porter.c *
35206 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35207 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35208 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
35209 20 53 65 70 74 65 6d 62 65 72 20 33 30 0a 2a 2a   September 30.**
3520a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
3520b 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
3520c 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
3520d 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
3520e 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
3520f 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
35210 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
35211 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
35212 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
35213 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
35214 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
35215 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
35216 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
35217 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
35218 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
35219 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
3521a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
3521b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
3521c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3521d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3521e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3521f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
35220 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
35221 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 2d  f the full-text-
35222 73 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a 65 72  search tokenizer
35223 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
35224 0a 2a 2a 20 61 20 50 6f 72 74 65 72 20 73 74 65  .** a Porter ste
35225 6d 6d 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  mmer..*/../*.** 
35226 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
35227 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f   file is only co
35228 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a  mpiled if:.**.**
35229 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20       * The FTS3 
3522a 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20  module is being 
3522b 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65  built as an exte
3522c 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28  nsion.**       (
3522d 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51  in which case SQ
3522e 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74  LITE_CORE is not
3522f 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a   defined), or.**
35230 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54  .**     * The FT
35231 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69  S3 module is bei
35232 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  ng built into th
35233 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20  e core of.**    
35234 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68     SQLite (in wh
35235 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f  ich case SQLITE_
35236 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64  ENABLE_FTS3 is d
35237 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20  efined)..*/.#if 
35238 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
35239 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
3523a 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
3523b 54 53 33 29 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 43  TS3)...../*.** C
3523c 6c 61 73 73 20 64 65 72 69 76 65 64 20 66 72 6f  lass derived fro
3523d 6d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  m sqlite3_tokeni
3523e 7a 65 72 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  zer.*/.typedef s
3523f 74 72 75 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b  truct porter_tok
35240 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74  enizer {.  sqlit
35241 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73  e3_tokenizer bas
35242 65 3b 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20  e;      /* Base 
35243 63 6c 61 73 73 20 2a 2f 0a 7d 20 70 6f 72 74 65  class */.} porte
35244 72 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 2f 2a  r_tokenizer;../*
35245 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72 69 76 65  .** Class derive
35246 64 20 66 72 6f 6d 20 73 71 6c 69 74 33 5f 74 6f  d from sqlit3_to
35247 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 0a 2a  kenizer_cursor.*
35248 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
35249 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
3524a 72 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  r_cursor {.  sql
3524b 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3524c 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 63 6f  ursor base;.  co
3524d 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
3524e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e  ;          /* in
3524f 70 75 74 20 77 65 20 61 72 65 20 74 6f 6b 65 6e  put we are token
35250 69 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e  izing */.  int n
35251 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20  Input;          
35252 20 20 20 20 20 20 20 20 2f 2a 20 73 69 7a 65 20          /* size 
35253 6f 66 20 74 68 65 20 69 6e 70 75 74 20 2a 2f 0a  of the input */.
35254 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20    int iOffset;  
35255 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35256 2a 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69  * current positi
35257 6f 6e 20 69 6e 20 7a 49 6e 70 75 74 20 2a 2f 0a  on in zInput */.
35258 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20 20    int iToken;   
35259 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3525a 2a 20 69 6e 64 65 78 20 6f 66 20 6e 65 78 74 20  * index of next 
3525b 74 6f 6b 65 6e 20 74 6f 20 62 65 20 72 65 74 75  token to be retu
3525c 72 6e 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  rned */.  char *
3525d 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  zToken;         
3525e 20 20 20 20 20 20 20 2f 2a 20 73 74 6f 72 61 67         /* storag
3525f 65 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74 6f  e for current to
35260 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  ken */.  int nAl
35261 6c 6f 63 61 74 65 64 3b 20 20 20 20 20 20 20 20  located;        
35262 20 20 20 20 20 20 2f 2a 20 73 70 61 63 65 20 61        /* space a
35263 6c 6c 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f 6b  llocated to zTok
35264 65 6e 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20 70  en buffer */.} p
35265 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
35266 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f 72  cursor;.../* For
35267 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
35268 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
35269 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3526a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72 74 65 72  er_module porter
3526b 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b  TokenizerModule;
3526c 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  .../*.** Create 
3526d 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20  a new tokenizer 
3526e 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61  instance..*/.sta
3526f 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 43 72  tic int porterCr
35270 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63  eate(.  int argc
35271 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63  , const char * c
35272 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 73 71  onst *argv,.  sq
35273 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
35274 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b  **ppTokenizer.){
35275 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .  porter_tokeni
35276 7a 65 72 20 2a 74 3b 0a 20 20 74 20 3d 20 28 70  zer *t;.  t = (p
35277 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20  orter_tokenizer 
35278 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
35279 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20  c(sizeof(*t));. 
3527a 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29 20 72   if( t==NULL ) r
3527b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
3527c 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74 2c 20  EM;.  memset(t, 
3527d 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a  0, sizeof(*t));.
3527e 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d    *ppTokenizer =
3527f 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74   &t->base;.  ret
35280 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
35281 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20  ../*.** Destroy 
35282 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73  a tokenizer.*/.s
35283 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72  tatic int porter
35284 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f  Destroy(sqlite3_
35285 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
35286 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c 69 74 65  nizer){.  sqlite
35287 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e 69 7a 65  3_free(pTokenize
35288 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  r);.  return SQL
35289 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3528a 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67 69   Prepare to begi
3528b 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70  n tokenizing a p
3528c 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e 67  articular string
3528d 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20  .  The input.** 
3528e 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b  string to be tok
3528f 65 6e 69 7a 65 64 20 69 73 20 7a 49 6e 70 75 74  enized is zInput
35290 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d 2e 20 20  [0..nInput-1].  
35291 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64  A cursor.** used
35292 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c   to incrementall
35293 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20  y tokenize this 
35294 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e  string is return
35295 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72  ed in .** *ppCur
35296 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  sor..*/.static i
35297 6e 74 20 70 6f 72 74 65 72 4f 70 65 6e 28 0a 20  nt porterOpen(. 
35298 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
35299 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er *pTokenizer, 
3529a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
3529b 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f  okenizer */.  co
3529c 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
3529d 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20  , int nInput,   
3529e 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 74       /* String t
3529f 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 2a  o be tokenized *
352a0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
352a1 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70  nizer_cursor **p
352a2 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f 55  pCursor    /* OU
352a3 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20  T: Tokenization 
352a4 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 70  cursor */.){.  p
352a5 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
352a6 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20 63 20  cursor *c;..  c 
352a7 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  = (porter_tokeni
352a8 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 73 71  zer_cursor *) sq
352a9 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
352aa 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69 66 28 20  eof(*c));.  if( 
352ab 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  c==NULL ) return
352ac 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a   SQLITE_NOMEM;..
352ad 20 20 63 2d 3e 7a 49 6e 70 75 74 20 3d 20 7a 49    c->zInput = zI
352ae 6e 70 75 74 3b 0a 20 20 69 66 28 20 7a 49 6e 70  nput;.  if( zInp
352af 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 63 2d 3e  ut==0 ){.    c->
352b0 6e 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20 7d 65  nInput = 0;.  }e
352b1 6c 73 65 20 69 66 28 20 6e 49 6e 70 75 74 3c 30  lse if( nInput<0
352b2 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75   ){.    c->nInpu
352b3 74 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  t = (int)strlen(
352b4 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 65 6c 73 65  zInput);.  }else
352b5 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74 20  {.    c->nInput 
352b6 3d 20 6e 49 6e 70 75 74 3b 0a 20 20 7d 0a 20 20  = nInput;.  }.  
352b7 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 20  c->iOffset = 0; 
352b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
352b9 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65 6e 69 7a  /* start tokeniz
352ba 69 6e 67 20 61 74 20 74 68 65 20 62 65 67 69 6e  ing at the begin
352bb 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e 69 54 6f  ning */.  c->iTo
352bc 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d 3e 7a 54  ken = 0;.  c->zT
352bd 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  oken = NULL;    
352be 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f             /* no
352bf 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
352c0 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63 2d 3e 6e  , yet. */.  c->n
352c1 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b 0a 0a  Allocated = 0;..
352c2 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26 63    *ppCursor = &c
352c3 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e  ->base;.  return
352c4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
352c5 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b  *.** Close a tok
352c6 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
352c7 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
352c8 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
352c9 2a 2a 20 70 6f 72 74 65 72 4f 70 65 6e 28 29 20  ** porterOpen() 
352ca 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  above..*/.static
352cb 20 69 6e 74 20 70 6f 72 74 65 72 43 6c 6f 73 65   int porterClose
352cc 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
352cd 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
352ce 6f 72 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f  or){.  porter_to
352cf 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
352d0 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65  c = (porter_toke
352d1 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20  nizer_cursor *) 
352d2 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74  pCursor;.  sqlit
352d3 65 33 5f 66 72 65 65 28 63 2d 3e 7a 54 6f 6b 65  e3_free(c->zToke
352d4 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  n);.  sqlite3_fr
352d5 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(c);.  return 
352d6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a  SQLITE_OK;.}./*.
352d7 2a 2a 20 56 6f 77 65 6c 20 6f 72 20 63 6f 6e 73  ** Vowel or cons
352d8 6f 6e 61 6e 74 0a 2a 2f 0a 73 74 61 74 69 63 20  onant.*/.static 
352d9 63 6f 6e 73 74 20 63 68 61 72 20 63 54 79 70 65  const char cType
352da 5b 5d 20 3d 20 7b 0a 20 20 20 30 2c 20 31 2c 20  [] = {.   0, 1, 
352db 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
352dc 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
352dd 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
352de 31 2c 20 31 2c 20 30 2c 0a 20 20 20 31 2c 20 31  1, 1, 0,.   1, 1
352df 2c 20 31 2c 20 32 2c 20 31 0a 7d 3b 0a 0a 2f 2a  , 1, 2, 1.};../*
352e0 0a 2a 2a 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  .** isConsonant(
352e1 29 20 61 6e 64 20 69 73 56 6f 77 65 6c 28 29 20  ) and isVowel() 
352e2 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65  determine if the
352e3 69 72 20 66 69 72 73 74 20 63 68 61 72 61 63 74  ir first charact
352e4 65 72 20 69 6e 0a 2a 2a 20 74 68 65 20 73 74 72  er in.** the str
352e5 69 6e 67 20 74 68 65 79 20 70 6f 69 6e 74 20 74  ing they point t
352e6 6f 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74  o is a consonant
352e7 20 6f 72 20 61 20 76 6f 77 65 6c 2c 20 61 63 63   or a vowel, acc
352e8 6f 72 64 69 6e 67 0a 2a 2a 20 74 6f 20 50 6f 72  ording.** to Por
352e9 74 65 72 20 72 75 6c 73 2e 20 20 0a 2a 2a 0a 2a  ter ruls.  .**.*
352ea 2a 20 41 20 63 6f 6e 73 6f 6e 61 74 65 20 69 73  * A consonate is
352eb 20 61 6e 79 20 6c 65 74 74 65 72 20 6f 74 68 65   any letter othe
352ec 72 20 74 68 61 6e 20 27 61 27 2c 20 27 65 27 2c  r than 'a', 'e',
352ed 20 27 69 27 2c 20 27 6f 27 2c 20 6f 72 20 27 75   'i', 'o', or 'u
352ee 27 2e 0a 2a 2a 20 27 59 27 20 69 73 20 61 20 63  '..** 'Y' is a c
352ef 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73 20  onsonant unless 
352f0 69 74 20 66 6f 6c 6c 6f 77 73 20 61 6e 6f 74 68  it follows anoth
352f1 65 72 20 63 6f 6e 73 6f 6e 61 6e 74 2c 0a 2a 2a  er consonant,.**
352f2 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 69   in which case i
352f3 74 20 69 73 20 61 20 76 6f 77 65 6c 2e 0a 2a 2a  t is a vowel..**
352f4 0a 2a 2a 20 49 6e 20 74 68 65 73 65 20 72 6f 75  .** In these rou
352f5 74 69 6e 65 2c 20 74 68 65 20 6c 65 74 74 65 72  tine, the letter
352f6 73 20 61 72 65 20 69 6e 20 72 65 76 65 72 73 65  s are in reverse
352f7 20 6f 72 64 65 72 2e 20 20 53 6f 20 74 68 65 20   order.  So the 
352f8 27 79 27 20 72 75 6c 65 0a 2a 2a 20 69 73 20 74  'y' rule.** is t
352f9 68 61 74 20 27 79 27 20 69 73 20 61 20 63 6f 6e  hat 'y' is a con
352fa 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73 20 69 74  sonant unless it
352fb 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20   is followed by 
352fc 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 73 6f  another.** conso
352fd 6e 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nent..*/.static 
352fe 69 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e 73  int isVowel(cons
352ff 74 20 63 68 61 72 2a 29 3b 0a 73 74 61 74 69 63  t char*);.static
35300 20 69 6e 74 20 69 73 43 6f 6e 73 6f 6e 61 6e 74   int isConsonant
35301 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
35302 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68 61 72  .  int j;.  char
35303 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28 20 78   x = *z;.  if( x
35304 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
35305 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27 61 27    assert( x>='a'
35306 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a 20 20   && x<='z' );.  
35307 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61 27 5d  j = cType[x-'a']
35308 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20 72 65  ;.  if( j<2 ) re
35309 74 75 72 6e 20 6a 3b 0a 20 20 72 65 74 75 72 6e  turn j;.  return
3530a 20 7a 5b 31 5d 3d 3d 30 20 7c 7c 20 69 73 56 6f   z[1]==0 || isVo
3530b 77 65 6c 28 7a 20 2b 20 31 29 3b 0a 7d 0a 73 74  wel(z + 1);.}.st
3530c 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77 65 6c  atic int isVowel
3530d 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
3530e 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68 61 72  .  int j;.  char
3530f 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28 20 78   x = *z;.  if( x
35310 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
35311 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27 61 27    assert( x>='a'
35312 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a 20 20   && x<='z' );.  
35313 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61 27 5d  j = cType[x-'a']
35314 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20 72 65  ;.  if( j<2 ) re
35315 74 75 72 6e 20 31 2d 6a 3b 0a 20 20 72 65 74 75  turn 1-j;.  retu
35316 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  rn isConsonant(z
35317 20 2b 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   + 1);.}../*.** 
35318 4c 65 74 20 61 6e 79 20 73 65 71 75 65 6e 63 65  Let any sequence
35319 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   of one or more 
3531a 76 6f 77 65 6c 73 20 62 65 20 72 65 70 72 65 73  vowels be repres
3531b 65 6e 74 65 64 20 62 79 20 56 20 61 6e 64 20 6c  ented by V and l
3531c 65 74 0a 2a 2a 20 43 20 62 65 20 73 65 71 75 65  et.** C be seque
3531d 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f  nce of one or mo
3531e 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 73 2e 20 20  re consonants.  
3531f 54 68 65 6e 20 65 76 65 72 79 20 77 6f 72 64 20  Then every word 
35320 63 61 6e 20 62 65 0a 2a 2a 20 72 65 70 72 65 73  can be.** repres
35321 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a 2a 20  ented as:.**.** 
35322 20 20 20 20 20 20 20 20 20 20 5b 43 5d 20 28 56            [C] (V
35323 43 29 7b 6d 7d 20 5b 56 5d 0a 2a 2a 0a 2a 2a 20  C){m} [V].**.** 
35324 49 6e 20 70 72 6f 73 65 3a 20 20 41 20 77 6f 72  In prose:  A wor
35325 64 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c  d is an optional
35326 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f   consonant follo
35327 77 65 64 20 62 79 20 7a 65 72 6f 20 6f 72 0a 2a  wed by zero or.*
35328 2a 20 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 61 6e  * vowel-consonan
35329 74 20 70 61 69 72 73 20 66 6f 6c 6c 6f 77 65 64  t pairs followed
3532a 20 62 79 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20   by an optional 
3532b 76 6f 77 65 6c 2e 20 20 22 6d 22 20 69 73 20 74  vowel.  "m" is t
3532c 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
3532d 76 6f 77 65 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20  vowel consonant 
3532e 70 61 69 72 73 2e 20 20 54 68 69 73 20 72 6f 75  pairs.  This rou
3532f 74 69 6e 65 20 63 6f 6d 70 75 74 65 73 20 74 68  tine computes th
35330 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 6d 20  e value.** of m 
35331 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 69 20  for the first i 
35332 62 79 74 65 73 20 6f 66 20 61 20 77 6f 72 64 2e  bytes of a word.
35333 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  .**.** Return tr
35334 75 65 20 69 66 20 74 68 65 20 6d 2d 76 61 6c 75  ue if the m-valu
35335 65 20 66 6f 72 20 7a 20 69 73 20 31 20 6f 72 20  e for z is 1 or 
35336 6d 6f 72 65 2e 20 20 49 6e 20 6f 74 68 65 72 20  more.  In other 
35337 77 6f 72 64 73 2c 0a 2a 2a 20 72 65 74 75 72 6e  words,.** return
35338 20 74 72 75 65 20 69 66 20 7a 20 63 6f 6e 74 61   true if z conta
35339 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  ins at least one
3533a 20 76 6f 77 65 6c 20 74 68 61 74 20 69 73 20 66   vowel that is f
3533b 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 61 20  ollowed.** by a 
3533c 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a  consonant..**.**
3533d 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65   In this routine
3533e 20 7a 5b 5d 20 69 73 20 69 6e 20 72 65 76 65 72   z[] is in rever
3533f 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20 77 65  se order.  So we
35340 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b   are really look
35341 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 6e 20 69 6e  ing.** for an in
35342 73 74 61 6e 63 65 20 6f 66 20 6f 66 20 61 20 63  stance of of a c
35343 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65  onsonant followe
35344 64 20 62 79 20 61 20 76 6f 77 65 6c 2e 0a 2a 2f  d by a vowel..*/
35345 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 67 74  .static int m_gt
35346 5f 30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  _0(const char *z
35347 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  ){.  while( isVo
35348 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
35349 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
3534a 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
3534b 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
3534c 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74   ){ z++; }.  ret
3534d 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a  urn *z!=0;.}../*
3534e 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f 76 65   Like mgt0 above
3534f 20 65 78 63 65 70 74 20 77 65 20 61 72 65 20 6c   except we are l
35350 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76 61 6c  ooking for a val
35351 75 65 20 6f 66 20 6d 20 77 68 69 63 68 20 69 73  ue of m which is
35352 0a 2a 2a 20 65 78 61 63 74 6c 79 20 31 0a 2a 2f  .** exactly 1.*/
35353 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 65 71  .static int m_eq
35354 5f 31 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  _1(const char *z
35355 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  ){.  while( isVo
35356 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
35357 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
35358 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
35359 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
3535a 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
3535b 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
3535c 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  0;.  while( isVo
3535d 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
3535e 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
3535f 65 74 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c 65  eturn 1;.  while
35360 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
35361 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74   ){ z++; }.  ret
35362 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a  urn *z==0;.}../*
35363 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f 76 65   Like mgt0 above
35364 20 65 78 63 65 70 74 20 77 65 20 61 72 65 20 6c   except we are l
35365 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76 61 6c  ooking for a val
35366 75 65 20 6f 66 20 6d 3e 31 20 69 6e 73 74 65 61  ue of m>1 instea
35367 64 0a 2a 2a 20 6f 72 20 6d 3e 30 0a 2a 2f 0a 73  d.** or m>0.*/.s
35368 74 61 74 69 63 20 69 6e 74 20 6d 5f 67 74 5f 31  tatic int m_gt_1
35369 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
3536a 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65  .  while( isVowe
3536b 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20  l(z) ){ z++; }. 
3536c 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74   if( *z==0 ) ret
3536d 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  urn 0;.  while( 
3536e 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29  isConsonant(z) )
3536f 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a  { z++; }.  if( *
35370 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  z==0 ) return 0;
35371 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65  .  while( isVowe
35372 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20  l(z) ){ z++; }. 
35373 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74   if( *z==0 ) ret
35374 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  urn 0;.  while( 
35375 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29  isConsonant(z) )
35376 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72  { z++; }.  retur
35377 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  n *z!=0;.}../*.*
35378 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
35379 20 74 68 65 72 65 20 69 73 20 61 20 76 6f 77 65   there is a vowe
3537a 6c 20 61 6e 79 77 68 65 72 65 20 77 69 74 68 69  l anywhere withi
3537b 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 0a 2a 2f 0a 73  n z[0..n-1].*/.s
3537c 74 61 74 69 63 20 69 6e 74 20 68 61 73 56 6f 77  tatic int hasVow
3537d 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  el(const char *z
3537e 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  ){.  while( isCo
3537f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
35380 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
35381 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  !=0;.}../*.** Re
35382 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
35383 20 77 6f 72 64 20 65 6e 64 73 20 69 6e 20 61 20   word ends in a 
35384 64 6f 75 62 6c 65 20 63 6f 6e 73 6f 6e 61 6e 74  double consonant
35385 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 78 74  ..**.** The text
35386 20 69 73 20 72 65 76 65 72 73 65 64 20 68 65 72   is reversed her
35387 65 2e 20 53 6f 20 77 65 20 61 72 65 20 72 65 61  e. So we are rea
35388 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 0a 2a  lly looking at.*
35389 2a 20 74 68 65 20 66 69 72 73 74 20 74 77 6f 20  * the first two 
3538a 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 7a 5b  characters of z[
3538b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ]..*/.static int
3538c 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e 61 6e 74   doubleConsonant
3538d 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
3538e 0a 20 20 72 65 74 75 72 6e 20 69 73 43 6f 6e 73  .  return isCons
3538f 6f 6e 61 6e 74 28 7a 29 20 26 26 20 7a 5b 30 5d  onant(z) && z[0]
35390 3d 3d 7a 5b 31 5d 20 26 26 20 69 73 43 6f 6e 73  ==z[1] && isCons
35391 6f 6e 61 6e 74 28 7a 2b 31 29 3b 0a 7d 0a 0a 2f  onant(z+1);.}../
35392 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
35393 20 69 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64   if the word end
35394 73 20 77 69 74 68 20 74 68 72 65 65 20 6c 65 74  s with three let
35395 74 65 72 73 20 77 68 69 63 68 0a 2a 2a 20 61 72  ters which.** ar
35396 65 20 63 6f 6e 73 6f 6e 61 6e 74 2d 76 6f 77 65  e consonant-vowe
35397 6c 2d 63 6f 6e 73 6f 6e 65 6e 74 20 61 6e 64 20  l-consonent and 
35398 77 68 65 72 65 20 74 68 65 20 66 69 6e 61 6c 20  where the final 
35399 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2a 20 69 73 20  consonant.** is 
3539a 6e 6f 74 20 27 77 27 2c 20 27 78 27 2c 20 6f 72  not 'w', 'x', or
3539b 20 27 79 27 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   'y'..**.** The 
3539c 77 6f 72 64 20 69 73 20 72 65 76 65 72 73 65 64  word is reversed
3539d 20 68 65 72 65 2e 20 20 53 6f 20 77 65 20 61 72   here.  So we ar
3539e 65 20 72 65 61 6c 6c 79 20 63 68 65 63 6b 69 6e  e really checkin
3539f 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74  g the.** first t
353a0 68 72 65 65 20 6c 65 74 74 65 72 73 20 61 6e 64  hree letters and
353a1 20 74 68 65 20 66 69 72 73 74 20 6f 6e 65 20 63   the first one c
353a2 61 6e 6e 6f 74 20 62 65 20 69 6e 20 5b 77 78 79  annot be in [wxy
353a3 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ]..*/.static int
353a4 20 73 74 61 72 5f 6f 68 28 63 6f 6e 73 74 20 63   star_oh(const c
353a5 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72  har *z){.  retur
353a6 6e 0a 20 20 20 20 7a 5b 30 5d 21 3d 30 20 26 26  n.    z[0]!=0 &&
353a7 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20   isConsonant(z) 
353a8 26 26 0a 20 20 20 20 7a 5b 30 5d 21 3d 27 77 27  &&.    z[0]!='w'
353a9 20 26 26 20 7a 5b 30 5d 21 3d 27 78 27 20 26 26   && z[0]!='x' &&
353aa 20 7a 5b 30 5d 21 3d 27 79 27 20 26 26 0a 20 20   z[0]!='y' &&.  
353ab 20 20 7a 5b 31 5d 21 3d 30 20 26 26 20 69 73 56    z[1]!=0 && isV
353ac 6f 77 65 6c 28 7a 2b 31 29 20 26 26 0a 20 20 20  owel(z+1) &&.   
353ad 20 7a 5b 32 5d 21 3d 30 20 26 26 20 69 73 43 6f   z[2]!=0 && isCo
353ae 6e 73 6f 6e 61 6e 74 28 7a 2b 32 29 3b 0a 7d 0a  nsonant(z+2);.}.
353af 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 6f  ./*.** If the wo
353b0 72 64 20 65 6e 64 73 20 77 69 74 68 20 7a 46 72  rd ends with zFr
353b1 6f 6d 20 61 6e 64 20 78 43 6f 6e 64 28 29 20 69  om and xCond() i
353b2 73 20 74 72 75 65 20 66 6f 72 20 74 68 65 20 73  s true for the s
353b3 74 65 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 77 6f  tem.** of the wo
353b4 72 64 20 74 68 61 74 20 70 72 65 63 65 65 64 73  rd that preceeds
353b5 20 74 68 65 20 7a 46 72 6f 6d 20 65 6e 64 69 6e   the zFrom endin
353b6 67 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20 74  g, then change t
353b7 68 65 20 0a 2a 2a 20 65 6e 64 69 6e 67 20 74 6f  he .** ending to
353b8 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   zTo..**.** The 
353b9 69 6e 70 75 74 20 77 6f 72 64 20 2a 70 7a 20 61  input word *pz a
353ba 6e 64 20 7a 46 72 6f 6d 20 61 72 65 20 62 6f 74  nd zFrom are bot
353bb 68 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  h in reverse ord
353bc 65 72 2e 20 20 7a 54 6f 0a 2a 2a 20 69 73 20 69  er.  zTo.** is i
353bd 6e 20 6e 6f 72 6d 61 6c 20 6f 72 64 65 72 2e 20  n normal order. 
353be 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  .**.** Return TR
353bf 55 45 20 69 66 20 7a 46 72 6f 6d 20 6d 61 74 63  UE if zFrom matc
353c0 68 65 73 2e 20 20 52 65 74 75 72 6e 20 46 41 4c  hes.  Return FAL
353c1 53 45 20 69 66 20 7a 46 72 6f 6d 20 64 6f 65 73  SE if zFrom does
353c2 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 2e 20 20   not.** match.  
353c3 4e 6f 74 20 74 68 61 74 20 54 52 55 45 20 69 73  Not that TRUE is
353c4 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e 20 69   returned even i
353c5 66 20 78 43 6f 6e 64 28 29 20 66 61 69 6c 73 20  f xCond() fails 
353c6 61 6e 64 0a 2a 2a 20 6e 6f 20 73 75 62 73 74 69  and.** no substi
353c7 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a  tution occurs..*
353c8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 65  /.static int ste
353c9 6d 28 0a 20 20 63 68 61 72 20 2a 2a 70 7a 2c 20  m(.  char **pz, 
353ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
353cb 68 65 20 77 6f 72 64 20 62 65 69 6e 67 20 73 74  he word being st
353cc 65 6d 6d 65 64 20 28 52 65 76 65 72 73 65 64 29  emmed (Reversed)
353cd 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
353ce 20 2a 7a 46 72 6f 6d 2c 20 20 20 20 20 2f 2a 20   *zFrom,     /* 
353cf 49 66 20 74 68 65 20 65 6e 64 69 6e 67 20 6d 61  If the ending ma
353d0 74 63 68 65 73 20 74 68 69 73 2e 2e 2e 20 28 52  tches this... (R
353d1 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20 63 6f  eversed) */.  co
353d2 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 2c 20 20  nst char *zTo,  
353d3 20 20 20 20 20 2f 2a 20 2e 2e 2e 20 63 68 61 6e       /* ... chan
353d4 67 65 20 74 68 65 20 65 6e 64 69 6e 67 20 74 6f  ge the ending to
353d5 20 74 68 69 73 20 28 6e 6f 74 20 72 65 76 65 72   this (not rever
353d6 73 65 64 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  sed) */.  int (*
353d7 78 43 6f 6e 64 29 28 63 6f 6e 73 74 20 63 68 61  xCond)(const cha
353d8 72 2a 29 20 20 20 2f 2a 20 43 6f 6e 64 69 74 69  r*)   /* Conditi
353d9 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62 65 20  on that must be 
353da 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 68 61  true */.){.  cha
353db 72 20 2a 7a 20 3d 20 2a 70 7a 3b 0a 20 20 77 68  r *z = *pz;.  wh
353dc 69 6c 65 28 20 2a 7a 46 72 6f 6d 20 26 26 20 2a  ile( *zFrom && *
353dd 7a 46 72 6f 6d 3d 3d 2a 7a 20 29 7b 20 7a 2b 2b  zFrom==*z ){ z++
353de 3b 20 7a 46 72 6f 6d 2b 2b 3b 20 7d 0a 20 20 69  ; zFrom++; }.  i
353df 66 28 20 2a 7a 46 72 6f 6d 21 3d 30 20 29 20 72  f( *zFrom!=0 ) r
353e0 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 78  eturn 0;.  if( x
353e1 43 6f 6e 64 20 26 26 20 21 78 43 6f 6e 64 28 7a  Cond && !xCond(z
353e2 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
353e3 77 68 69 6c 65 28 20 2a 7a 54 6f 20 29 7b 0a 20  while( *zTo ){. 
353e4 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 2a 28 7a 54     *(--z) = *(zT
353e5 6f 2b 2b 29 3b 0a 20 20 7d 0a 20 20 2a 70 7a 20  o++);.  }.  *pz 
353e6 3d 20 7a 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  = z;.  return 1;
353e7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
353e8 73 20 74 68 65 20 66 61 6c 6c 62 61 63 6b 20 73  s the fallback s
353e9 74 65 6d 6d 65 72 20 75 73 65 64 20 77 68 65 6e  temmer used when
353ea 20 74 68 65 20 70 6f 72 74 65 72 20 73 74 65 6d   the porter stem
353eb 6d 65 72 20 69 73 0a 2a 2a 20 69 6e 61 70 70 72  mer is.** inappr
353ec 6f 70 72 69 61 74 65 2e 20 20 54 68 65 20 69 6e  opriate.  The in
353ed 70 75 74 20 77 6f 72 64 20 69 73 20 63 6f 70 69  put word is copi
353ee 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70  ed into the outp
353ef 75 74 20 77 69 74 68 0a 2a 2a 20 55 53 2d 41 53  ut with.** US-AS
353f0 43 49 49 20 63 61 73 65 20 66 6f 6c 64 69 6e 67  CII case folding
353f1 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  .  If the input 
353f2 77 6f 72 64 20 69 73 20 74 6f 6f 20 6c 6f 6e 67  word is too long
353f3 20 28 6d 6f 72 65 0a 2a 2a 20 74 68 61 6e 20 32   (more.** than 2
353f4 30 20 62 79 74 65 73 20 69 66 20 69 74 20 63 6f  0 bytes if it co
353f5 6e 74 61 69 6e 73 20 6e 6f 20 64 69 67 69 74 73  ntains no digits
353f6 20 6f 72 20 6d 6f 72 65 20 74 68 61 6e 20 36 20   or more than 6 
353f7 62 79 74 65 73 20 69 66 0a 2a 2a 20 69 74 20 63  bytes if.** it c
353f8 6f 6e 74 61 69 6e 73 20 64 69 67 69 74 73 29 20  ontains digits) 
353f9 74 68 65 6e 20 77 6f 72 64 20 69 73 20 74 72 75  then word is tru
353fa 6e 63 61 74 65 64 20 74 6f 20 32 30 20 6f 72 20  ncated to 20 or 
353fb 36 20 62 79 74 65 73 0a 2a 2a 20 62 79 20 74 61  6 bytes.** by ta
353fc 6b 69 6e 67 20 31 30 20 6f 72 20 33 20 62 79 74  king 10 or 3 byt
353fd 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  es from the begi
353fe 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 2e 0a 2a  nning and end..*
353ff 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
35400 70 79 5f 73 74 65 6d 6d 65 72 28 63 6f 6e 73 74  py_stemmer(const
35401 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20   char *zIn, int 
35402 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f 75 74 2c  nIn, char *zOut,
35403 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20   int *pnOut){.  
35404 69 6e 74 20 69 2c 20 6d 78 2c 20 6a 3b 0a 20 20  int i, mx, j;.  
35405 69 6e 74 20 68 61 73 44 69 67 69 74 20 3d 20 30  int hasDigit = 0
35406 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
35407 49 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  In; i++){.    in
35408 74 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20  t c = zIn[i];.  
35409 20 20 69 66 28 20 63 3e 3d 27 41 27 20 26 26 20    if( c>='A' && 
3540a 63 3c 3d 27 5a 27 20 29 7b 0a 20 20 20 20 20 20  c<='Z' ){.      
3540b 7a 4f 75 74 5b 69 5d 20 3d 20 63 20 2d 20 27 41  zOut[i] = c - 'A
3540c 27 20 2b 20 27 61 27 3b 0a 20 20 20 20 7d 65 6c  ' + 'a';.    }el
3540d 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3e  se{.      if( c>
3540e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29  ='0' && c<='9' )
3540f 20 68 61 73 44 69 67 69 74 20 3d 20 31 3b 0a 20   hasDigit = 1;. 
35410 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63       zOut[i] = c
35411 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 78  ;.    }.  }.  mx
35412 20 3d 20 68 61 73 44 69 67 69 74 20 3f 20 33 20   = hasDigit ? 3 
35413 3a 20 31 30 3b 0a 20 20 69 66 28 20 6e 49 6e 3e  : 10;.  if( nIn>
35414 6d 78 2a 32 20 29 7b 0a 20 20 20 20 66 6f 72 28  mx*2 ){.    for(
35415 6a 3d 6d 78 2c 20 69 3d 6e 49 6e 2d 6d 78 3b 20  j=mx, i=nIn-mx; 
35416 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2b 2b 29  i<nIn; i++, j++)
35417 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 5d 20  {.      zOut[j] 
35418 3d 20 7a 4f 75 74 5b 69 5d 3b 0a 20 20 20 20 7d  = zOut[i];.    }
35419 0a 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 7d 0a  .    i = j;.  }.
3541a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20    zOut[i] = 0;. 
3541b 20 2a 70 6e 4f 75 74 20 3d 20 69 3b 0a 7d 0a 0a   *pnOut = i;.}..
3541c 0a 2f 2a 0a 2a 2a 20 53 74 65 6d 20 74 68 65 20  ./*.** Stem the 
3541d 69 6e 70 75 74 20 77 6f 72 64 20 7a 49 6e 5b 30  input word zIn[0
3541e 2e 2e 6e 49 6e 2d 31 5d 2e 20 20 53 74 6f 72 65  ..nIn-1].  Store
3541f 20 74 68 65 20 6f 75 74 70 75 74 20 69 6e 20 7a   the output in z
35420 4f 75 74 2e 0a 2a 2a 20 7a 4f 75 74 20 69 73 20  Out..** zOut is 
35421 61 74 20 6c 65 61 73 74 20 62 69 67 20 65 6e 6f  at least big eno
35422 75 67 68 20 74 6f 20 68 6f 6c 64 20 6e 49 6e 20  ugh to hold nIn 
35423 62 79 74 65 73 2e 20 20 57 72 69 74 65 20 74 68  bytes.  Write th
35424 65 20 61 63 74 75 61 6c 0a 2a 2a 20 73 69 7a 65  e actual.** size
35425 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 77   of the output w
35426 6f 72 64 20 28 65 78 63 6c 75 73 69 76 65 20 6f  ord (exclusive o
35427 66 20 74 68 65 20 27 5c 30 27 20 74 65 72 6d 69  f the '\0' termi
35428 6e 61 74 6f 72 29 20 69 6e 74 6f 20 2a 70 6e 4f  nator) into *pnO
35429 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 75 70  ut..**.** Any up
3542a 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 74  per-case charact
3542b 65 72 73 20 69 6e 20 74 68 65 20 55 53 2d 41 53  ers in the US-AS
3542c 43 49 49 20 63 68 61 72 61 63 74 65 72 20 73 65  CII character se
3542d 74 20 28 5b 41 2d 5a 5d 29 0a 2a 2a 20 61 72 65  t ([A-Z]).** are
3542e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 6c 6f   converted to lo
3542f 77 65 72 20 63 61 73 65 2e 20 20 55 70 70 65 72  wer case.  Upper
35430 2d 63 61 73 65 20 55 54 46 20 63 68 61 72 61 63  -case UTF charac
35431 74 65 72 73 20 61 72 65 0a 2a 2a 20 75 6e 63 68  ters are.** unch
35432 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 6f 72  anged..**.** Wor
35433 64 73 20 74 68 61 74 20 61 72 65 20 6c 6f 6e 67  ds that are long
35434 65 72 20 74 68 61 6e 20 61 62 6f 75 74 20 32 30  er than about 20
35435 20 62 79 74 65 73 20 61 72 65 20 73 74 65 6d 6d   bytes are stemm
35436 65 64 20 62 79 20 72 65 74 61 69 6e 69 6e 67 0a  ed by retaining.
35437 2a 2a 20 61 20 66 65 77 20 62 79 74 65 73 20 66  ** a few bytes f
35438 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
35439 67 20 61 6e 64 20 74 68 65 20 65 6e 64 20 6f 66  g and the end of
3543a 20 74 68 65 20 77 6f 72 64 2e 20 20 49 66 20 74   the word.  If t
3543b 68 65 0a 2a 2a 20 77 6f 72 64 20 63 6f 6e 74 61  he.** word conta
3543c 69 6e 73 20 64 69 67 69 74 73 2c 20 33 20 62 79  ins digits, 3 by
3543d 74 65 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72  tes are taken fr
3543e 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  om the beginning
3543f 20 61 6e 64 0a 2a 2a 20 33 20 62 79 74 65 73 20   and.** 3 bytes 
35440 66 72 6f 6d 20 74 68 65 20 65 6e 64 2e 20 20 46  from the end.  F
35441 6f 72 20 6c 6f 6e 67 20 77 6f 72 64 73 20 77 69  or long words wi
35442 74 68 6f 75 74 20 64 69 67 69 74 73 2c 20 31 30  thout digits, 10
35443 20 62 79 74 65 73 0a 2a 2a 20 61 72 65 20 74 61   bytes.** are ta
35444 6b 65 6e 20 66 72 6f 6d 20 65 61 63 68 20 65 6e  ken from each en
35445 64 2e 20 20 55 53 2d 41 53 43 49 49 20 63 61 73  d.  US-ASCII cas
35446 65 20 66 6f 6c 64 69 6e 67 20 73 74 69 6c 6c 20  e folding still 
35447 61 70 70 6c 69 65 73 2e 0a 2a 2a 20 0a 2a 2a 20  applies..** .** 
35448 49 66 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72  If the input wor
35449 64 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 20 64  d contains not d
3544a 69 67 69 74 73 20 62 75 74 20 64 6f 65 73 20 63  igits but does c
3544b 68 61 72 61 63 74 65 72 73 20 6e 6f 74 20 0a 2a  haracters not .*
3544c 2a 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 74 68  * in [a-zA-Z] th
3544d 65 6e 20 6e 6f 20 73 74 65 6d 6d 69 6e 67 20 69  en no stemming i
3544e 73 20 61 74 74 65 6d 70 74 65 64 20 61 6e 64 20  s attempted and 
3544f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73  this routine jus
35450 74 20 0a 2a 2a 20 63 6f 70 69 65 73 20 74 68 65  t .** copies the
35451 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68 65 20   input into the 
35452 69 6e 70 75 74 20 69 6e 74 6f 20 74 68 65 20 6f  input into the o
35453 75 74 70 75 74 20 77 69 74 68 20 55 53 2d 41 53  utput with US-AS
35454 43 49 49 0a 2a 2a 20 63 61 73 65 20 66 6f 6c 64  CII.** case fold
35455 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 74 65 6d 6d  ing..**.** Stemm
35456 69 6e 67 20 6e 65 76 65 72 20 69 6e 63 72 65 61  ing never increa
35457 73 65 73 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  ses the length o
35458 66 20 74 68 65 20 77 6f 72 64 2e 20 20 53 6f 20  f the word.  So 
35459 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 63  there is.** no c
3545a 68 61 6e 63 65 20 6f 66 20 6f 76 65 72 66 6c 6f  hance of overflo
3545b 77 69 6e 67 20 74 68 65 20 7a 4f 75 74 20 62 75  wing the zOut bu
3545c 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ffer..*/.static 
3545d 76 6f 69 64 20 70 6f 72 74 65 72 5f 73 74 65 6d  void porter_stem
3545e 6d 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mer(const char *
3545f 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20 63 68  zIn, int nIn, ch
35460 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a 70  ar *zOut, int *p
35461 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20 69 2c 20  nOut){.  int i, 
35462 6a 2c 20 63 3b 0a 20 20 63 68 61 72 20 7a 52 65  j, c;.  char zRe
35463 76 65 72 73 65 5b 32 38 5d 3b 0a 20 20 63 68 61  verse[28];.  cha
35464 72 20 2a 7a 2c 20 2a 7a 32 3b 0a 20 20 69 66 28  r *z, *z2;.  if(
35465 20 6e 49 6e 3c 33 20 7c 7c 20 6e 49 6e 3e 3d 73   nIn<3 || nIn>=s
35466 69 7a 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d  izeof(zReverse)-
35467 37 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  7 ){.    /* The 
35468 77 6f 72 64 20 69 73 20 74 6f 6f 20 62 69 67 20  word is too big 
35469 6f 72 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72  or too small for
3546a 20 74 68 65 20 70 6f 72 74 65 72 20 73 74 65 6d   the porter stem
3546b 6d 65 72 2e 0a 20 20 20 20 2a 2a 20 46 61 6c 6c  mer..    ** Fall
3546c 62 61 63 6b 20 74 6f 20 74 68 65 20 63 6f 70 79  back to the copy
3546d 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20 20 20   stemmer */.    
3546e 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a 49 6e  copy_stemmer(zIn
3546f 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f  , nIn, zOut, pnO
35470 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ut);.    return;
35471 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 2c 20  .  }.  for(i=0, 
35472 6a 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73  j=sizeof(zRevers
35473 65 29 2d 36 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b  e)-6; i<nIn; i++
35474 2c 20 6a 2d 2d 29 7b 0a 20 20 20 20 63 20 3d 20  , j--){.    c = 
35475 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20  zIn[i];.    if( 
35476 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27 5a 27  c>='A' && c<='Z'
35477 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 76 65 72   ){.      zRever
35478 73 65 5b 6a 5d 20 3d 20 63 20 2b 20 27 61 27 20  se[j] = c + 'a' 
35479 2d 20 27 41 27 3b 0a 20 20 20 20 7d 65 6c 73 65  - 'A';.    }else
3547a 20 69 66 28 20 63 3e 3d 27 61 27 20 26 26 20 63   if( c>='a' && c
3547b 3c 3d 27 7a 27 20 29 7b 0a 20 20 20 20 20 20 7a  <='z' ){.      z
3547c 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63 3b 0a  Reverse[j] = c;.
3547d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3547e 20 2f 2a 20 54 68 65 20 75 73 65 20 6f 66 20 61   /* The use of a
3547f 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69   character not i
35480 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 6d 65 61 6e 73  n [a-zA-Z] means
35481 20 74 68 61 74 20 77 65 20 66 61 6c 6c 62 61 63   that we fallbac
35482 6b 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68  k.      ** to th
35483 65 20 63 6f 70 79 20 73 74 65 6d 6d 65 72 20 2a  e copy stemmer *
35484 2f 0a 20 20 20 20 20 20 63 6f 70 79 5f 73 74 65  /.      copy_ste
35485 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a  mmer(zIn, nIn, z
35486 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20 20 20  Out, pnOut);.   
35487 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
35488 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 7a  .  }.  memset(&z
35489 52 65 76 65 72 73 65 5b 73 69 7a 65 6f 66 28 7a  Reverse[sizeof(z
3548a 52 65 76 65 72 73 65 29 2d 35 5d 2c 20 30 2c 20  Reverse)-5], 0, 
3548b 35 29 3b 0a 20 20 7a 20 3d 20 26 7a 52 65 76 65  5);.  z = &zReve
3548c 72 73 65 5b 6a 2b 31 5d 3b 0a 0a 0a 20 20 2f 2a  rse[j+1];...  /*
3548d 20 53 74 65 70 20 31 61 20 2a 2f 0a 20 20 69 66   Step 1a */.  if
3548e 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 29 7b 0a 20  ( z[0]=='s' ){. 
3548f 20 20 20 69 66 28 0a 20 20 20 20 20 21 73 74 65     if(.     !ste
35490 6d 28 26 7a 2c 20 22 73 65 73 73 22 2c 20 22 73  m(&z, "sess", "s
35491 73 22 2c 20 30 29 20 26 26 0a 20 20 20 20 20 21  s", 0) &&.     !
35492 73 74 65 6d 28 26 7a 2c 20 22 73 65 69 22 2c 20  stem(&z, "sei", 
35493 22 69 22 2c 20 30 29 20 20 26 26 0a 20 20 20 20  "i", 0)  &&.    
35494 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 73 22 2c   !stem(&z, "ss",
35495 20 22 73 73 22 2c 20 30 29 0a 20 20 20 20 29 7b   "ss", 0).    ){
35496 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
35497 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  }.  }..  /* Step
35498 20 31 62 20 2a 2f 20 20 0a 20 20 7a 32 20 3d 20   1b */  .  z2 = 
35499 7a 3b 0a 20 20 69 66 28 20 73 74 65 6d 28 26 7a  z;.  if( stem(&z
3549a 2c 20 22 64 65 65 22 2c 20 22 65 65 22 2c 20 6d  , "dee", "ee", m
3549b 5f 67 74 5f 30 29 20 29 7b 0a 20 20 20 20 2f 2a  _gt_0) ){.    /*
3549c 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68   Do nothing.  Th
3549d 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20 69  e work was all i
3549e 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20 20  n the test */.  
3549f 7d 65 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20  }else if( .     
354a0 28 73 74 65 6d 28 26 7a 2c 20 22 67 6e 69 22 2c  (stem(&z, "gni",
354a1 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29 20 7c   "", hasVowel) |
354a2 7c 20 73 74 65 6d 28 26 7a 2c 20 22 64 65 22 2c  | stem(&z, "de",
354a3 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29 29 0a   "", hasVowel)).
354a4 20 20 20 20 20 20 26 26 20 7a 21 3d 7a 32 0a 20        && z!=z2. 
354a5 20 29 7b 0a 20 20 20 20 20 69 66 28 20 73 74 65   ){.     if( ste
354a6 6d 28 26 7a 2c 20 22 74 61 22 2c 20 22 61 74 65  m(&z, "ta", "ate
354a7 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20 20  ", 0) ||.       
354a8 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 62 22 2c    stem(&z, "lb",
354a9 20 22 62 6c 65 22 2c 20 30 29 20 7c 7c 0a 20 20   "ble", 0) ||.  
354aa 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
354ab 22 7a 69 22 2c 20 22 69 7a 65 22 2c 20 30 29 20  "zi", "ize", 0) 
354ac 29 7b 0a 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  ){.       /* Do 
354ad 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f  nothing.  The wo
354ae 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68  rk was all in th
354af 65 20 74 65 73 74 20 2a 2f 0a 20 20 20 20 20 7d  e test */.     }
354b0 65 6c 73 65 20 69 66 28 20 64 6f 75 62 6c 65 43  else if( doubleC
354b1 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20 28  onsonant(z) && (
354b2 2a 7a 21 3d 27 6c 27 20 26 26 20 2a 7a 21 3d 27  *z!='l' && *z!='
354b3 73 27 20 26 26 20 2a 7a 21 3d 27 7a 27 29 20 29  s' && *z!='z') )
354b4 7b 0a 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  {.       z++;.  
354b5 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f 65     }else if( m_e
354b6 71 5f 31 28 7a 29 20 26 26 20 73 74 61 72 5f 6f  q_1(z) && star_o
354b7 68 28 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 2a  h(z) ){.       *
354b8 28 2d 2d 7a 29 20 3d 20 27 65 27 3b 0a 20 20 20  (--z) = 'e';.   
354b9 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74    }.  }..  /* St
354ba 65 70 20 31 63 20 2a 2f 0a 20 20 69 66 28 20 7a  ep 1c */.  if( z
354bb 5b 30 5d 3d 3d 27 79 27 20 26 26 20 68 61 73 56  [0]=='y' && hasV
354bc 6f 77 65 6c 28 7a 2b 31 29 20 29 7b 0a 20 20 20  owel(z+1) ){.   
354bd 20 7a 5b 30 5d 20 3d 20 27 69 27 3b 0a 20 20 7d   z[0] = 'i';.  }
354be 0a 0a 20 20 2f 2a 20 53 74 65 70 20 32 20 2a 2f  ..  /* Step 2 */
354bf 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31 5d 20  .  switch( z[1] 
354c0 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27 3a 0a  ){.   case 'a':.
354c1 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
354c2 61 6e 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20  anoita", "ate", 
354c3 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
354c4 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69 74  stem(&z, "lanoit
354c5 22 2c 20 22 74 69 6f 6e 22 2c 20 6d 5f 67 74 5f  ", "tion", m_gt_
354c6 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
354c7 20 20 20 63 61 73 65 20 27 63 27 3a 0a 20 20 20     case 'c':.   
354c8 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 65    stem(&z, "icne
354c9 22 2c 20 22 65 6e 63 65 22 2c 20 6d 5f 67 74 5f  ", "ence", m_gt_
354ca 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
354cb 26 7a 2c 20 22 69 63 6e 61 22 2c 20 22 61 6e 63  &z, "icna", "anc
354cc 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  e", m_gt_0);.   
354cd 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
354ce 20 27 65 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   'e':.     stem(
354cf 26 7a 2c 20 22 72 65 7a 69 22 2c 20 22 69 7a 65  &z, "rezi", "ize
354d0 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
354d1 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
354d2 27 67 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'g':.     stem(&
354d3 7a 2c 20 22 69 67 6f 6c 22 2c 20 22 6c 6f 67 22  z, "igol", "log"
354d4 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
354d5 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
354d6 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  l':.     stem(&z
354d7 2c 20 22 69 6c 62 22 2c 20 22 62 6c 65 22 2c 20  , "ilb", "ble", 
354d8 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
354d9 73 74 65 6d 28 26 7a 2c 20 22 69 6c 6c 61 22 2c  stem(&z, "illa",
354da 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c   "al", m_gt_0) |
354db 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
354dc 22 69 6c 74 6e 65 22 2c 20 22 65 6e 74 22 2c 20  "iltne", "ent", 
354dd 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
354de 73 74 65 6d 28 26 7a 2c 20 22 69 6c 65 22 2c 20  stem(&z, "ile", 
354df 22 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  "e", m_gt_0) ||.
354e0 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
354e1 6c 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f  lsuo", "ous", m_
354e2 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
354e3 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27 3a 0a  k;.   case 'o':.
354e4 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e       stem(&z, "n
354e5 6f 69 74 61 7a 69 22 2c 20 22 69 7a 65 22 2c 20  oitazi", "ize", 
354e6 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
354e7 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61 22  stem(&z, "noita"
354e8 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ate", m_gt_0)
354e9 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
354ea 2c 20 22 72 6f 74 61 22 2c 20 22 61 74 65 22 2c  , "rota", "ate",
354eb 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
354ec 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 73  reak;.   case 's
354ed 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
354ee 20 22 6d 73 69 6c 61 22 2c 20 22 61 6c 22 2c 20   "msila", "al", 
354ef 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
354f0 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 65 76  stem(&z, "ssenev
354f1 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f  i", "ive", m_gt_
354f2 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
354f3 26 7a 2c 20 22 73 73 65 6e 6c 75 66 22 2c 20 22  &z, "ssenluf", "
354f4 66 75 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c  ful", m_gt_0) ||
354f5 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
354f6 73 73 65 6e 73 75 6f 22 2c 20 22 6f 75 73 22 2c  ssensuo", "ous",
354f7 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
354f8 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 74  reak;.   case 't
354f9 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
354fa 20 22 69 74 69 6c 61 22 2c 20 22 61 6c 22 2c 20   "itila", "al", 
354fb 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
354fc 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 76 69 22  stem(&z, "itivi"
354fd 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ive", m_gt_0)
354fe 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
354ff 2c 20 22 69 74 69 6c 69 62 22 2c 20 22 62 6c 65  , "itilib", "ble
35500 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
35501 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
35502 2a 20 53 74 65 70 20 33 20 2a 2f 0a 20 20 73 77  * Step 3 */.  sw
35503 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 20 20  itch( z[0] ){.  
35504 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20   case 'e':.     
35505 73 74 65 6d 28 26 7a 2c 20 22 65 74 61 63 69 22  stem(&z, "etaci"
35506 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20  , "ic", m_gt_0) 
35507 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
35508 20 22 65 76 69 74 61 22 2c 20 22 22 2c 20 6d 5f   "evita", "", m_
35509 67 74 5f 30 29 20 20 20 7c 7c 0a 20 20 20 20 20  gt_0)   ||.     
3550a 73 74 65 6d 28 26 7a 2c 20 22 65 7a 69 6c 61 22  stem(&z, "ezila"
3550b 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 3b  , "al", m_gt_0);
3550c 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
3550d 63 61 73 65 20 27 69 27 3a 0a 20 20 20 20 20 73  case 'i':.     s
3550e 74 65 6d 28 26 7a 2c 20 22 69 74 69 63 69 22 2c  tem(&z, "itici",
3550f 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a   "ic", m_gt_0);.
35510 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
35511 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74  ase 'l':.     st
35512 65 6d 28 26 7a 2c 20 22 6c 61 63 69 22 2c 20 22  em(&z, "laci", "
35513 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ic", m_gt_0) ||.
35514 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
35515 75 66 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29  uf", "", m_gt_0)
35516 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
35517 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20   case 's':.     
35518 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 22 2c  stem(&z, "ssen",
35519 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20   "", m_gt_0);.  
3551a 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
3551b 20 2f 2a 20 53 74 65 70 20 34 20 2a 2f 0a 20 20   /* Step 4 */.  
3551c 73 77 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b 0a  switch( z[1] ){.
3551d 20 20 20 63 61 73 65 20 27 61 27 3a 0a 20 20 20     case 'a':.   
3551e 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6c 27 20    if( z[0]=='l' 
3551f 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29  && m_gt_1(z+2) )
35520 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b  {.       z += 2;
35521 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
35522 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27 3a  ak;.   case 'c':
35523 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d  .     if( z[0]==
35524 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6e 27  'e' && z[2]=='n'
35525 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c   && (z[3]=='a' |
35526 7c 20 7a 5b 33 5d 3d 3d 27 65 27 29 20 20 26 26  | z[3]=='e')  &&
35527 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 20 29 7b   m_gt_1(z+4)  ){
35528 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b 0a  .       z += 4;.
35529 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61       }.     brea
3552a 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  k;.   case 'e':.
3552b 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
3552c 72 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32  r' && m_gt_1(z+2
3552d 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d  ) ){.       z +=
3552e 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20   2;.     }.     
3552f 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
35530 69 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  i':.     if( z[0
35531 5d 3d 3d 27 63 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='c' && m_gt_1
35532 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+2) ){.       
35533 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20  z += 2;.     }. 
35534 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
35535 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 69 66 28  se 'l':.     if(
35536 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b   z[0]=='e' && z[
35537 32 5d 3d 3d 27 62 27 20 26 26 20 28 7a 5b 33 5d  2]=='b' && (z[3]
35538 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27  =='a' || z[3]=='
35539 69 27 29 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  i') && m_gt_1(z+
3553a 34 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  4) ){.       z +
3553b 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 4;.     }.    
3553c 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
3553d 27 6e 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'n':.     if( z[
3553e 30 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20 20  0]=='t' ){.     
3553f 20 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 61 27 20    if( z[2]=='a' 
35540 29 7b 0a 20 20 20 20 20 20 20 20 20 69 66 28 20  ){.         if( 
35541 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20  m_gt_1(z+3) ){. 
35542 20 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 33            z += 3
35543 3b 0a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ;.         }.   
35544 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b      }else if( z[
35545 32 5d 3d 3d 27 65 27 20 29 7b 0a 20 20 20 20 20  2]=='e' ){.     
35546 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e      stem(&z, "tn
35547 65 6d 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31  eme", "", m_gt_1
35548 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74  ) ||.         st
35549 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 22 2c 20 22  em(&z, "tnem", "
3554a 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20  ", m_gt_1) ||.  
3554b 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
3554c 22 74 6e 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "tne", "", m_gt_
3554d 31 29 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20  1);.       }.   
3554e 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
3554f 20 20 20 63 61 73 65 20 27 6f 27 3a 0a 20 20 20     case 'o':.   
35550 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 75 27 20    if( z[0]=='u' 
35551 29 7b 0a 20 20 20 20 20 20 20 69 66 28 20 6d 5f  ){.       if( m_
35552 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20  gt_1(z+2) ){.   
35553 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20        z += 2;.  
35554 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 65 6c 73       }.     }els
35555 65 20 69 66 28 20 7a 5b 33 5d 3d 3d 27 73 27 20  e if( z[3]=='s' 
35556 7c 7c 20 7a 5b 33 5d 3d 3d 27 74 27 20 29 7b 0a  || z[3]=='t' ){.
35557 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
35558 22 6e 6f 69 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "noi", "", m_gt_
35559 31 29 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20  1);.     }.     
3555a 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
3555b 73 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  s':.     if( z[0
3555c 5d 3d 3d 27 6d 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='m' && z[2]==
3555d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  'i' && m_gt_1(z+
3555e 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  3) ){.       z +
3555f 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 3;.     }.    
35560 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
35561 27 74 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  't':.     stem(&
35562 7a 2c 20 22 65 74 61 22 2c 20 22 22 2c 20 6d 5f  z, "eta", "", m_
35563 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_1) ||.     st
35564 65 6d 28 26 7a 2c 20 22 69 74 69 22 2c 20 22 22  em(&z, "iti", ""
35565 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20  , m_gt_1);.     
35566 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
35567 75 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  u':.     if( z[0
35568 5d 3d 3d 27 73 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='s' && z[2]==
35569 27 6f 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  'o' && m_gt_1(z+
3556a 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  3) ){.       z +
3556b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 3;.     }.    
3556c 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
3556d 27 76 27 3a 0a 20 20 20 63 61 73 65 20 27 7a 27  'v':.   case 'z'
3556e 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
3556f 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 69  ='e' && z[2]=='i
35570 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29  ' && m_gt_1(z+3)
35571 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20   ){.       z += 
35572 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62  3;.     }.     b
35573 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
35574 53 74 65 70 20 35 61 20 2a 2f 0a 20 20 69 66 28  Step 5a */.  if(
35575 20 7a 5b 30 5d 3d 3d 27 65 27 20 29 7b 0a 20 20   z[0]=='e' ){.  
35576 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 31    if( m_gt_1(z+1
35577 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  ) ){.      z++;.
35578 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f      }else if( m_
35579 65 71 5f 31 28 7a 2b 31 29 20 26 26 20 21 73 74  eq_1(z+1) && !st
3557a 61 72 5f 6f 68 28 7a 2b 31 29 20 29 7b 0a 20 20  ar_oh(z+1) ){.  
3557b 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20      z++;.    }. 
3557c 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 62   }..  /* Step 5b
3557d 20 2a 2f 0a 20 20 69 66 28 20 6d 5f 67 74 5f 31   */.  if( m_gt_1
3557e 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 27 6c 27  (z) && z[0]=='l'
3557f 20 26 26 20 7a 5b 31 5d 3d 3d 27 6c 27 20 29 7b   && z[1]=='l' ){
35580 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 0a 20  .    z++;.  }.. 
35581 20 2f 2a 20 7a 5b 5d 20 69 73 20 6e 6f 77 20 74   /* z[] is now t
35582 68 65 20 73 74 65 6d 6d 65 64 20 77 6f 72 64 20  he stemmed word 
35583 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
35584 2e 20 20 46 6c 69 70 20 69 74 20 62 61 63 6b 0a  .  Flip it back.
35585 20 20 2a 2a 20 61 72 6f 75 6e 64 20 69 6e 74 6f    ** around into
35586 20 66 6f 72 77 61 72 64 20 6f 72 64 65 72 20 61   forward order a
35587 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a  nd return..  */.
35588 20 20 2a 70 6e 4f 75 74 20 3d 20 69 20 3d 20 73    *pnOut = i = s
35589 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 7a 4f 75 74  trlen(z);.  zOut
3558a 5b 69 5d 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  [i] = 0;.  while
3558b 28 20 2a 7a 20 29 7b 0a 20 20 20 20 7a 4f 75 74  ( *z ){.    zOut
3558c 5b 2d 2d 69 5d 20 3d 20 2a 28 7a 2b 2b 29 3b 0a  [--i] = *(z++);.
3558d 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
3558e 72 61 63 74 65 72 73 20 74 68 61 74 20 63 61 6e  racters that can
3558f 20 62 65 20 70 61 72 74 20 6f 66 20 61 20 74 6f   be part of a to
35590 6b 65 6e 2e 20 20 57 65 20 61 73 73 75 6d 65 20  ken.  We assume 
35591 61 6e 79 20 63 68 61 72 61 63 74 65 72 0a 2a 2a  any character.**
35592 20 77 68 6f 73 65 20 76 61 6c 75 65 20 69 73 20   whose value is 
35593 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 78 38  greater than 0x8
35594 30 20 28 61 6e 79 20 55 54 46 20 63 68 61 72 61  0 (any UTF chara
35595 63 74 65 72 29 20 63 61 6e 20 62 65 0a 2a 2a 20  cter) can be.** 
35596 70 61 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e  part of a token.
35597 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
35598 2c 20 64 65 6c 69 6d 69 74 65 72 73 20 61 6c 6c  , delimiters all
35599 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a 20 76 61   must have.** va
3559a 6c 75 65 73 20 6f 66 20 30 78 37 66 20 6f 72 20  lues of 0x7f or 
3559b 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  lower..*/.static
3559c 20 63 6f 6e 73 74 20 63 68 61 72 20 70 6f 72 74   const char port
3559d 65 72 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f  erIdChar[] = {./
3559e 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34  * x0 x1 x2 x3 x4
3559f 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20   x5 x6 x7 x8 x9 
355a0 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78  xA xB xC xD xE x
355a1 46 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31  F */.    1, 1, 1
355a2 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
355a3 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
355a4 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78 20  0, 0, 0,  /* 3x 
355a5 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20  */.    0, 1, 1, 
355a6 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
355a7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
355a8 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a 2f   1, 1,  /* 4x */
355a9 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c  .    1, 1, 1, 1,
355aa 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
355ab 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 1, 0, 0, 0, 0
355ac 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20  , 1,  /* 5x */. 
355ad 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
355ae 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
355af 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
355b0 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  1,  /* 6x */.   
355b1 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
355b2 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
355b3 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
355b4 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64    /* 7x */.};.#d
355b5 65 66 69 6e 65 20 69 73 44 65 6c 69 6d 28 43 29  efine isDelim(C)
355b6 20 28 28 28 63 68 3d 43 29 26 30 78 38 30 29 3d   (((ch=C)&0x80)=
355b7 3d 30 20 26 26 20 28 63 68 3c 30 78 33 30 20 7c  =0 && (ch<0x30 |
355b8 7c 20 21 70 6f 72 74 65 72 49 64 43 68 61 72 5b  | !porterIdChar[
355b9 63 68 2d 30 78 33 30 5d 29 29 0a 0a 2f 2a 0a 2a  ch-0x30]))../*.*
355ba 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e 65  * Extract the ne
355bb 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20  xt token from a 
355bc 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72  tokenization cur
355bd 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72  sor.  The cursor
355be 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 62 65   must.** have be
355bf 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70  en opened by a p
355c0 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 70 6f 72  rior call to por
355c1 74 65 72 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74  terOpen()..*/.st
355c2 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 4e  atic int porterN
355c3 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  ext(.  sqlite3_t
355c4 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
355c5 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75  *pCursor,  /* Cu
355c6 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79  rsor returned by
355c7 20 70 6f 72 74 65 72 4f 70 65 6e 20 2a 2f 0a 20   porterOpen */. 
355c8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
355c9 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20  Token,          
355ca 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 7a       /* OUT: *pz
355cb 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74 6f 6b  Token is the tok
355cc 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  en text */.  int
355cd 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20 20 20   *pnBytes,      
355ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355cf 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
355d0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f 6b 65  of bytes in toke
355d1 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74  n */.  int *piSt
355d2 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20 20 20  artOffset,      
355d3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
355d4 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73  T: Starting offs
355d5 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
355d6 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65   int *piEndOffse
355d7 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
355d8 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64       /* OUT: End
355d9 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f  ing offset of to
355da 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
355db 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20  Position        
355dc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
355dd 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e  OUT: Position in
355de 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a  teger of token *
355df 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f  /.){.  porter_to
355e0 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
355e1 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65  c = (porter_toke
355e2 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20  nizer_cursor *) 
355e3 70 43 75 72 73 6f 72 3b 0a 20 20 63 6f 6e 73 74  pCursor;.  const
355e4 20 63 68 61 72 20 2a 7a 20 3d 20 63 2d 3e 7a 49   char *z = c->zI
355e5 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20  nput;..  while( 
355e6 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 49  c->iOffset<c->nI
355e7 6e 70 75 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  nput ){.    int 
355e8 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 63 68  iStartOffset, ch
355e9 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 70  ;..    /* Scan p
355ea 61 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63 68  ast delimiter ch
355eb 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 20 20  aracters */.    
355ec 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65  while( c->iOffse
355ed 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26 26 20 69  t<c->nInput && i
355ee 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66  sDelim(z[c->iOff
355ef 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63  set]) ){.      c
355f0 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20  ->iOffset++;.   
355f1 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 75 6e 74   }..    /* Count
355f2 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65 72 20 63   non-delimiter c
355f3 68 61 72 61 63 74 65 72 73 2e 20 2a 2f 0a 20 20  haracters. */.  
355f4 20 20 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d    iStartOffset =
355f5 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20   c->iOffset;.   
355f6 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73   while( c->iOffs
355f7 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26 26 20  et<c->nInput && 
355f8 21 69 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f  !isDelim(z[c->iO
355f9 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20  ffset]) ){.     
355fa 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20   c->iOffset++;. 
355fb 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 63 2d     }..    if( c-
355fc 3e 69 4f 66 66 73 65 74 3e 69 53 74 61 72 74 4f  >iOffset>iStartO
355fd 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 69  ffset ){.      i
355fe 6e 74 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73 65  nt n = c->iOffse
355ff 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  t-iStartOffset;.
35600 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e 6e        if( n>c->n
35601 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a 20 20 20  Allocated ){.   
35602 20 20 20 20 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74       c->nAllocat
35603 65 64 20 3d 20 6e 2b 32 30 3b 0a 20 20 20 20 20  ed = n+20;.     
35604 20 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 73     c->zToken = s
35605 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 63  qlite3_realloc(c
35606 2d 3e 7a 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 41 6c  ->zToken, c->nAl
35607 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20 20 20 20  located);.      
35608 20 20 69 66 28 20 63 2d 3e 7a 54 6f 6b 65 6e 3d    if( c->zToken=
35609 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53  =NULL ) return S
3560a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
3560b 20 20 20 7d 0a 20 20 20 20 20 20 70 6f 72 74 65     }.      porte
3560c 72 5f 73 74 65 6d 6d 65 72 28 26 7a 5b 69 53 74  r_stemmer(&z[iSt
3560d 61 72 74 4f 66 66 73 65 74 5d 2c 20 6e 2c 20 63  artOffset], n, c
3560e 2d 3e 7a 54 6f 6b 65 6e 2c 20 70 6e 42 79 74 65  ->zToken, pnByte
3560f 73 29 3b 0a 20 20 20 20 20 20 2a 70 7a 54 6f 6b  s);.      *pzTok
35610 65 6e 20 3d 20 63 2d 3e 7a 54 6f 6b 65 6e 3b 0a  en = c->zToken;.
35611 20 20 20 20 20 20 2a 70 69 53 74 61 72 74 4f 66        *piStartOf
35612 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f 66 66  fset = iStartOff
35613 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69 45 6e  set;.      *piEn
35614 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66  dOffset = c->iOf
35615 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69 50  fset;.      *piP
35616 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e 69 54 6f  osition = c->iTo
35617 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  ken++;.      ret
35618 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
35619 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
3561a 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 7d  n SQLITE_DONE;.}
3561b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20  ../*.** The set 
3561c 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  of routines that
3561d 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70   implement the p
3561e 6f 72 74 65 72 2d 73 74 65 6d 6d 65 72 20 74 6f  orter-stemmer to
3561f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69  kenizer.*/.stati
35620 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
35621 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
35622 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72   porterTokenizer
35623 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a  Module = {.  0,.
35624 20 20 70 6f 72 74 65 72 43 72 65 61 74 65 2c 0a    porterCreate,.
35625 20 20 70 6f 72 74 65 72 44 65 73 74 72 6f 79 2c    porterDestroy,
35626 0a 20 20 70 6f 72 74 65 72 4f 70 65 6e 2c 0a 20  .  porterOpen,. 
35627 20 70 6f 72 74 65 72 43 6c 6f 73 65 2c 0a 20 20   porterClose,.  
35628 70 6f 72 74 65 72 4e 65 78 74 2c 0a 7d 3b 0a 0a  porterNext,.};..
35629 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
3562a 20 6e 65 77 20 70 6f 72 74 65 72 20 74 6f 6b 65   new porter toke
3562b 6e 69 7a 65 72 2e 20 20 52 65 74 75 72 6e 20 61  nizer.  Return a
3562c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
3562d 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72  new.** tokenizer
3562e 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f   in *ppModule.*/
3562f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
35630 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
35631 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d  PorterTokenizerM
35632 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  odule(.  sqlite3
35633 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
35634 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c  e const**ppModul
35635 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65  e.){.  *ppModule
35636 20 3d 20 26 70 6f 72 74 65 72 54 6f 6b 65 6e 69   = &porterTokeni
35637 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65  zerModule;.}..#e
35638 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
35639 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
3563a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3563b 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
3563c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3563d 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 70 6f 72   End of fts3_por
3563e 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ter.c **********
3563f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
35641 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
35642 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
35643 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a 2a  _tokenizer.c ***
35644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
35646 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a 75 6e 65  ./*.** 2007 June
35647 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
35648 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
35649 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
3564a 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
3564b 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
3564c 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
3564d 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
3564e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
3564f 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
35650 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
35651 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
35652 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
35653 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
35654 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
35655 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
35656 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
35657 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
35658 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
35659 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3565a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3565b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3565c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3565d 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
3565e 69 73 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e  is is part of an
3565f 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 69   SQLite module i
35660 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66 75 6c 6c  mplementing full
35661 2d 74 65 78 74 20 73 65 61 72 63 68 2e 0a 2a 2a  -text search..**
35662 20 54 68 69 73 20 70 61 72 74 69 63 75 6c 61 72   This particular
35663 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73   file implements
35664 20 74 68 65 20 67 65 6e 65 72 69 63 20 74 6f 6b   the generic tok
35665 65 6e 69 7a 65 72 20 69 6e 74 65 72 66 61 63 65  enizer interface
35666 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
35667 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
35668 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c  e is only compil
35669 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed if:.**.**    
3566a 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
3566b 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
3566c 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f  t as an extensio
3566d 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77  n.**       (in w
3566e 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
3566f 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66  _CORE is not def
35670 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  ined), or.**.** 
35671 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
35672 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
35673 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f  uilt into the co
35674 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53  re of.**       S
35675 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20  QLite (in which 
35676 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  case SQLITE_ENAB
35677 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e  LE_FTS3 is defin
35678 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ed)..*/.#if !def
35679 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
3567a 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
3567b 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
3567c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
3567d 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f 45  _CORE.  SQLITE_E
3567e 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23  XTENSION_INIT1.#
3567f 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d  endif.../*.** Im
35680 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
35681 74 68 65 20 53 51 4c 20 73 63 61 6c 61 72 20 66  the SQL scalar f
35682 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 63 63 65  unction for acce
35683 73 73 69 6e 67 20 74 68 65 20 75 6e 64 65 72 6c  ssing the underl
35684 79 69 6e 67 20 0a 2a 2a 20 68 61 73 68 20 74 61  ying .** hash ta
35685 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ble. This functi
35686 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64  on may be called
35687 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
35688 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e  **   SELECT <fun
35689 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79  ction-name>(<key
3568a 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a 20 20 20 53 45  -name>);.**   SE
3568b 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  LECT <function-n
3568c 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c  ame>(<key-name>,
3568d 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a 2a 0a   <pointer>);.**.
3568e 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63 74 69  ** where <functi
3568f 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65 20  on-name> is the 
35690 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73 20 74  name passed as t
35691 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
35692 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c  nt.** to the sql
35693 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68  ite3Fts3InitHash
35694 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e  Table() function
35695 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f 6b   (e.g. 'fts3_tok
35696 65 6e 69 7a 65 72 27 29 2e 0a 2a 2a 0a 2a 2a 20  enizer')..**.** 
35697 49 66 20 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e  If the <pointer>
35698 20 61 72 67 75 6d 65 6e 74 20 69 73 20 73 70 65   argument is spe
35699 63 69 66 69 65 64 2c 20 69 74 20 6d 75 73 74 20  cified, it must 
3569a 62 65 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 0a  be a blob value.
3569b 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20  ** containing a 
3569c 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 73 74  pointer to be st
3569d 6f 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68  ored as the hash
3569e 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64   data correspond
3569f 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74  ing.** to the st
356a0 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 2e  ring <key-name>.
356a1 20 49 66 20 3c 70 6f 69 6e 74 65 72 3e 20 69 73   If <pointer> is
356a2 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 2c 20   not specified, 
356a3 74 68 65 6e 0a 2a 2a 20 74 68 65 20 73 74 72 69  then.** the stri
356a4 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20 6d 75  ng <key-name> mu
356a5 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  st already exist
356a6 20 69 6e 20 74 68 65 20 68 61 73 20 74 61 62 6c   in the has tabl
356a7 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a  e. Otherwise,.**
356a8 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
356a9 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  urned..**.** Whe
356aa 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
356ab 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67 75 6d 65  <pointer> argume
356ac 6e 74 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  nt is specified,
356ad 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
356ae 6e 65 64 0a 2a 2a 20 69 73 20 61 20 62 6c 6f 62  ned.** is a blob
356af 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
356b0 70 6f 69 6e 74 65 72 20 73 74 6f 72 65 64 20 61  pointer stored a
356b1 73 20 74 68 65 20 68 61 73 68 20 64 61 74 61 20  s the hash data 
356b2 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
356b3 20 74 6f 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d   to string <key-
356b4 6e 61 6d 65 3e 20 28 61 66 74 65 72 20 74 68 65  name> (after the
356b5 20 68 61 73 68 2d 74 61 62 6c 65 20 69 73 20 75   hash-table is u
356b6 70 64 61 74 65 64 2c 20 69 66 20 61 70 70 6c 69  pdated, if appli
356b7 63 61 62 6c 65 29 2e 0a 2a 2f 0a 73 74 61 74 69  cable)..*/.stati
356b8 63 20 76 6f 69 64 20 73 63 61 6c 61 72 46 75 6e  c void scalarFun
356b9 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
356ba 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
356bb 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
356bc 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
356bd 76 0a 29 7b 0a 20 20 66 74 73 33 48 61 73 68 20  v.){.  fts3Hash 
356be 2a 70 48 61 73 68 3b 0a 20 20 76 6f 69 64 20 2a  *pHash;.  void *
356bf 70 50 74 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  pPtr = 0;.  cons
356c0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
356c1 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e  *zName;.  int nN
356c2 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ame;..  assert( 
356c3 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d  argc==1 || argc=
356c4 3d 32 20 29 3b 0a 0a 20 20 70 48 61 73 68 20 3d  =2 );..  pHash =
356c5 20 28 66 74 73 33 48 61 73 68 20 2a 29 73 71 6c   (fts3Hash *)sql
356c6 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63  ite3_user_data(c
356c7 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 4e 61 6d  ontext);..  zNam
356c8 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
356c9 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
356ca 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  nName = sqlit
356cb 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
356cc 72 67 76 5b 30 5d 29 2b 31 3b 0a 0a 20 20 69 66  rgv[0])+1;..  if
356cd 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20  ( argc==2 ){.   
356ce 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20 20 20   void *pOld;.   
356cf 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33   int n = sqlite3
356d0 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
356d1 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e  v[1]);.    if( n
356d2 21 3d 73 69 7a 65 6f 66 28 70 50 74 72 29 20 29  !=sizeof(pPtr) )
356d3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
356d4 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
356d5 74 65 78 74 2c 20 22 61 72 67 75 6d 65 6e 74 20  text, "argument 
356d6 74 79 70 65 20 6d 69 73 6d 61 74 63 68 22 2c 20  type mismatch", 
356d7 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  -1);.      retur
356d8 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 74  n;.    }.    pPt
356d9 72 20 3d 20 2a 28 76 6f 69 64 20 2a 2a 29 73 71  r = *(void **)sq
356da 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
356db 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 70  (argv[1]);.    p
356dc 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 46 74 73  Old = sqlite3Fts
356dd 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73  3HashInsert(pHas
356de 68 2c 20 28 76 6f 69 64 20 2a 29 7a 4e 61 6d 65  h, (void *)zName
356df 2c 20 6e 4e 61 6d 65 2c 20 70 50 74 72 29 3b 0a  , nName, pPtr);.
356e0 20 20 20 20 69 66 28 20 70 4f 6c 64 3d 3d 70 50      if( pOld==pP
356e1 74 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  tr ){.      sqli
356e2 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
356e3 28 63 6f 6e 74 65 78 74 2c 20 22 6f 75 74 20 6f  (context, "out o
356e4 66 20 6d 65 6d 6f 72 79 22 2c 20 2d 31 29 3b 0a  f memory", -1);.
356e5 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
356e6 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
356e7 20 70 50 74 72 20 3d 20 73 71 6c 69 74 65 33 46   pPtr = sqlite3F
356e8 74 73 33 48 61 73 68 46 69 6e 64 28 70 48 61 73  ts3HashFind(pHas
356e9 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
356ea 3b 0a 20 20 20 20 69 66 28 20 21 70 50 74 72 20  ;.    if( !pPtr 
356eb 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
356ec 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
356ed 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74  rintf("unknown t
356ee 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a  okenizer: %s", z
356ef 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  Name);.      sql
356f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
356f1 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c  r(context, zErr,
356f2 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   -1);.      sqli
356f3 74 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a  te3_free(zErr);.
356f4 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
356f5 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74    }.  }..  sqlit
356f6 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63  e3_result_blob(c
356f7 6f 6e 74 65 78 74 2c 20 28 76 6f 69 64 20 2a 29  ontext, (void *)
356f8 26 70 50 74 72 2c 20 73 69 7a 65 6f 66 28 70 50  &pPtr, sizeof(pP
356f9 74 72 29 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  tr), SQLITE_TRAN
356fa 53 49 45 4e 54 29 3b 0a 7d 0a 0a 23 69 66 64 65  SIENT);.}..#ifde
356fb 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 0a 0a  f SQLITE_TEST...
356fc 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
356fd 74 69 6f 6e 20 6f 66 20 61 20 73 70 65 63 69 61  tion of a specia
356fe 6c 20 53 51 4c 20 73 63 61 6c 61 72 20 66 75 6e  l SQL scalar fun
356ff 63 74 69 6f 6e 20 66 6f 72 20 74 65 73 74 69 6e  ction for testin
35700 67 20 74 6f 6b 65 6e 69 7a 65 72 73 20 0a 2a 2a  g tokenizers .**
35701 20 64 65 73 69 67 6e 65 64 20 74 6f 20 62 65 20   designed to be 
35702 75 73 65 64 20 69 6e 20 63 6f 6e 63 65 72 74 20  used in concert 
35703 77 69 74 68 20 74 68 65 20 54 63 6c 20 74 65 73  with the Tcl tes
35704 74 69 6e 67 20 66 72 61 6d 65 77 6f 72 6b 2e 20  ting framework. 
35705 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  This.** function
35706 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
35707 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e  with two argumen
35708 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  ts:.**.**   SELE
35709 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d  CT <function-nam
3570a 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c  e>(<key-name>, <
3570b 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e 29 3b 0a  input-string>);.
3570c 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e  **   SELECT <fun
3570d 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79  ction-name>(<key
3570e 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74 65 72  -name>, <pointer
3570f 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20  >);.**.** where 
35710 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20  <function-name> 
35711 69 73 20 74 68 65 20 6e 61 6d 65 20 70 61 73 73  is the name pass
35712 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
35713 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
35714 74 68 65 20 73 71 6c 69 74 65 33 46 74 73 33 49  the sqlite3Fts3I
35715 6e 69 74 48 61 73 68 54 61 62 6c 65 28 29 20 66  nitHashTable() f
35716 75 6e 63 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66  unction (e.g. 'f
35717 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 0a  ts3_tokenizer').
35718 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20  ** concatenated 
35719 77 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 20  with the string 
3571a 27 5f 74 65 73 74 27 20 28 65 2e 67 2e 20 27 66  '_test' (e.g. 'f
3571b 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65  ts3_tokenizer_te
3571c 73 74 27 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st')..**.** The 
3571d 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
3571e 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6d 61  a string that ma
3571f 79 20 62 65 20 69 6e 74 65 72 70 72 65 74 65 64  y be interpreted
35720 20 61 73 20 61 20 54 63 6c 0a 2a 2a 20 6c 69 73   as a Tcl.** lis
35721 74 2e 20 46 6f 72 20 65 61 63 68 20 74 6f 6b 65  t. For each toke
35722 6e 20 69 6e 20 74 68 65 20 3c 69 6e 70 75 74 2d  n in the <input-
35723 73 74 72 69 6e 67 3e 2c 20 74 68 72 65 65 20 65  string>, three e
35724 6c 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 61  lements are.** a
35725 64 64 65 64 20 74 6f 20 74 68 65 20 72 65 74 75  dded to the retu
35726 72 6e 65 64 20 6c 69 73 74 2e 20 54 68 65 20 66  rned list. The f
35727 69 72 73 74 20 69 73 20 74 68 65 20 74 6f 6b 65  irst is the toke
35728 6e 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20  n position, the 
35729 0a 2a 2a 20 73 65 63 6f 6e 64 20 69 73 20 74 68  .** second is th
3572a 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 28 66 6f  e token text (fo
3572b 6c 64 65 64 2c 20 73 74 65 6d 6d 65 64 2c 20 65  lded, stemmed, e
3572c 74 63 2e 29 20 61 6e 64 20 74 68 65 20 74 68 69  tc.) and the thi
3572d 72 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 75 62  rd is the.** sub
3572e 73 74 72 69 6e 67 20 6f 66 20 3c 69 6e 70 75 74  string of <input
3572f 2d 73 74 72 69 6e 67 3e 20 61 73 73 6f 63 69 61  -string> associa
35730 74 65 64 20 77 69 74 68 20 74 68 65 20 74 6f 6b  ted with the tok
35731 65 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  en. For example,
35732 20 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 62   .** using the b
35733 75 69 6c 74 2d 69 6e 20 22 73 69 6d 70 6c 65 22  uilt-in "simple"
35734 20 74 6f 6b 65 6e 69 7a 65 72 3a 0a 2a 2a 0a 2a   tokenizer:.**.*
35735 2a 20 20 20 53 45 4c 45 43 54 20 66 74 73 5f 74  *   SELECT fts_t
35736 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74 28 27 73  okenizer_test('s
35737 69 6d 70 6c 65 27 2c 20 27 49 20 64 6f 6e 27 74  imple', 'I don't
35738 20 73 65 65 20 68 6f 77 27 29 3b 0a 2a 2a 0a 2a   see how');.**.*
35739 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74 68  * will return th
3573a 65 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  e string:.**.** 
3573b 20 20 22 7b 30 20 69 20 49 20 31 20 64 6f 6e 74    "{0 i I 1 dont
3573c 20 64 6f 6e 27 74 20 32 20 73 65 65 20 73 65 65   don't 2 see see
3573d 20 33 20 68 6f 77 20 68 6f 77 7d 22 0a 2a 2a 20   3 how how}".** 
3573e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69    .*/.static voi
3573f 64 20 74 65 73 74 46 75 6e 63 28 0a 20 20 73 71  d testFunc(.  sq
35740 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
35741 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
35742 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
35743 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
35744 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 3b  fts3Hash *pHash;
35745 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
35746 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 3b 0a  izer_module *p;.
35747 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
35748 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20  zer *pTokenizer 
35749 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
3574a 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
3574b 2a 70 43 73 72 20 3d 20 30 3b 0a 0a 20 20 63 6f  *pCsr = 0;..  co
3574c 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d  nst char *zErr =
3574d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61   0;..  const cha
3574e 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20  r *zName;.  int 
3574f 6e 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63  nName;.  const c
35750 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 69  har *zInput;.  i
35751 6e 74 20 6e 49 6e 70 75 74 3b 0a 0a 20 20 63 6f  nt nInput;..  co
35752 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 20 3d  nst char *zArg =
35753 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61   0;..  const cha
35754 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74  r *zToken;.  int
35755 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 69   nToken;.  int i
35756 53 74 61 72 74 3b 0a 20 20 69 6e 74 20 69 45 6e  Start;.  int iEn
35757 64 3b 0a 20 20 69 6e 74 20 69 50 6f 73 3b 0a 0a  d;.  int iPos;..
35758 20 20 54 63 6c 5f 4f 62 6a 20 2a 70 52 65 74 3b    Tcl_Obj *pRet;
35759 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
3575a 3d 3d 32 20 7c 7c 20 61 72 67 63 3d 3d 33 20 29  ==2 || argc==3 )
3575b 3b 0a 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c  ;..  nName = sql
3575c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
3575d 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61  (argv[0]);.  zNa
3575e 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  me = (const char
3575f 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
35760 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
35761 20 20 6e 49 6e 70 75 74 20 3d 20 73 71 6c 69 74    nInput = sqlit
35762 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
35763 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 20 20  rgv[argc-1]);.  
35764 7a 49 6e 70 75 74 20 3d 20 28 63 6f 6e 73 74 20  zInput = (const 
35765 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
35766 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 61  alue_text(argv[a
35767 72 67 63 2d 31 5d 29 3b 0a 0a 20 20 69 66 28 20  rgc-1]);..  if( 
35768 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 7a  argc==3 ){.    z
35769 41 72 67 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  Arg = (const cha
3576a 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
3576b 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
3576c 0a 20 20 7d 0a 0a 20 20 70 48 61 73 68 20 3d 20  .  }..  pHash = 
3576d 28 66 74 73 33 48 61 73 68 20 2a 29 73 71 6c 69  (fts3Hash *)sqli
3576e 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f  te3_user_data(co
3576f 6e 74 65 78 74 29 3b 0a 20 20 70 20 3d 20 28 73  ntext);.  p = (s
35770 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
35771 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  _module *)sqlite
35772 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48  3Fts3HashFind(pH
35773 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ash, zName, nNam
35774 65 2b 31 29 3b 0a 0a 20 20 69 66 28 20 21 70 20  e+1);..  if( !p 
35775 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72  ){.    char *zEr
35776 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  r = sqlite3_mpri
35777 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b  ntf("unknown tok
35778 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a 4e 61  enizer: %s", zNa
35779 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  me);.    sqlite3
3577a 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
3577b 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
3577c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
3577d 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 72 65  ee(zErr);.    re
3577e 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 52 65  turn;.  }..  pRe
3577f 74 20 3d 20 54 63 6c 5f 4e 65 77 4f 62 6a 28 29  t = Tcl_NewObj()
35780 3b 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43  ;.  Tcl_IncrRefC
35781 6f 75 6e 74 28 70 52 65 74 29 3b 0a 0a 20 20 69  ount(pRet);..  i
35782 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d  f( SQLITE_OK!=p-
35783 3e 78 43 72 65 61 74 65 28 7a 41 72 67 20 3f 20  >xCreate(zArg ? 
35784 31 20 3a 20 30 2c 20 26 7a 41 72 67 2c 20 26 70  1 : 0, &zArg, &p
35785 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20 20  Tokenizer) ){.  
35786 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20    zErr = "error 
35787 69 6e 20 78 43 72 65 61 74 65 28 29 22 3b 0a 20  in xCreate()";. 
35788 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a     goto finish;.
35789 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72    }.  pTokenizer
3578a 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 3b 0a 20  ->pModule = p;. 
3578b 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
3578c 70 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  p->xOpen(pTokeni
3578d 7a 65 72 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  zer, zInput, nIn
3578e 70 75 74 2c 20 26 70 43 73 72 29 20 29 7b 0a 20  put, &pCsr) ){. 
3578f 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72     zErr = "error
35790 20 69 6e 20 78 4f 70 65 6e 28 29 22 3b 0a 20 20   in xOpen()";.  
35791 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20    goto finish;. 
35792 20 7d 0a 20 20 70 43 73 72 2d 3e 70 54 6f 6b 65   }.  pCsr->pToke
35793 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
35794 65 72 3b 0a 0a 20 20 77 68 69 6c 65 28 20 53 51  er;..  while( SQ
35795 4c 49 54 45 5f 4f 4b 3d 3d 70 2d 3e 78 4e 65 78  LITE_OK==p->xNex
35796 74 28 70 43 73 72 2c 20 26 7a 54 6f 6b 65 6e 2c  t(pCsr, &zToken,
35797 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72   &nToken, &iStar
35798 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29  t, &iEnd, &iPos)
35799 20 29 7b 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74   ){.    Tcl_List
3579a 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74  ObjAppendElement
3579b 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65  (0, pRet, Tcl_Ne
3579c 77 49 6e 74 4f 62 6a 28 69 50 6f 73 29 29 3b 0a  wIntObj(iPos));.
3579d 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41      Tcl_ListObjA
3579e 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20  ppendElement(0, 
3579f 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 53 74 72  pRet, Tcl_NewStr
357a0 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e  ingObj(zToken, n
357a1 54 6f 6b 65 6e 29 29 3b 0a 20 20 20 20 7a 54 6f  Token));.    zTo
357a2 6b 65 6e 20 3d 20 26 7a 49 6e 70 75 74 5b 69 53  ken = &zInput[iS
357a3 74 61 72 74 5d 3b 0a 20 20 20 20 6e 54 6f 6b 65  tart];.    nToke
357a4 6e 20 3d 20 69 45 6e 64 2d 69 53 74 61 72 74 3b  n = iEnd-iStart;
357a5 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a  .    Tcl_ListObj
357a6 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c  AppendElement(0,
357a7 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 53 74   pRet, Tcl_NewSt
357a8 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20  ringObj(zToken, 
357a9 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20 7d 0a 0a 20  nToken));.  }.. 
357aa 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
357ab 70 2d 3e 78 43 6c 6f 73 65 28 70 43 73 72 29 20  p->xClose(pCsr) 
357ac 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65  ){.    zErr = "e
357ad 72 72 6f 72 20 69 6e 20 78 43 6c 6f 73 65 28 29  rror in xClose()
357ae 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69  ";.    goto fini
357af 73 68 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51  sh;.  }.  if( SQ
357b0 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 44 65 73  LITE_OK!=p->xDes
357b1 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65 72 29  troy(pTokenizer)
357b2 20 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22   ){.    zErr = "
357b3 65 72 72 6f 72 20 69 6e 20 78 44 65 73 74 72 6f  error in xDestro
357b4 79 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66  y()";.    goto f
357b5 69 6e 69 73 68 3b 0a 20 20 7d 0a 0a 66 69 6e 69  inish;.  }..fini
357b6 73 68 3a 0a 20 20 69 66 28 20 7a 45 72 72 20 29  sh:.  if( zErr )
357b7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
357b8 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
357b9 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20  xt, zErr, -1);. 
357ba 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
357bb 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
357bc 63 6f 6e 74 65 78 74 2c 20 54 63 6c 5f 47 65 74  context, Tcl_Get
357bd 53 74 72 69 6e 67 28 70 52 65 74 29 2c 20 2d 31  String(pRet), -1
357be 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
357bf 4e 54 29 3b 0a 20 20 7d 0a 20 20 54 63 6c 5f 44  NT);.  }.  Tcl_D
357c0 65 63 72 52 65 66 43 6f 75 6e 74 28 70 52 65 74  ecrRefCount(pRet
357c1 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a 69 6e 74  );.}..static.int
357c2 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a   registerTokeniz
357c3 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  er(.  sqlite3 *d
357c4 62 2c 20 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  b, .  char *zNam
357c5 65 2c 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  e, .  const sqli
357c6 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
357c7 64 75 6c 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74  dule *p.){.  int
357c8 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73   rc;.  sqlite3_s
357c9 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63 6f  tmt *pStmt;.  co
357ca 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20  nst char zSql[] 
357cb 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f 74  = "SELECT fts3_t
357cc 6f 6b 65 6e 69 7a 65 72 28 3f 2c 20 3f 29 22 3b  okenizer(?, ?)";
357cd 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
357ce 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
357cf 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
357d0 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
357d1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
357d2 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
357d3 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
357d4 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a  text(pStmt, 1, z
357d5 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Name, -1, SQLITE
357d6 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69  _STATIC);.  sqli
357d7 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53  te3_bind_blob(pS
357d8 74 6d 74 2c 20 32 2c 20 26 70 2c 20 73 69 7a 65  tmt, 2, &p, size
357d9 6f 66 28 70 29 2c 20 53 51 4c 49 54 45 5f 53 54  of(p), SQLITE_ST
357da 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33  ATIC);.  sqlite3
357db 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a 0a 20  _step(pStmt);.. 
357dc 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
357dd 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
357de 0a 7d 0a 0a 73 74 61 74 69 63 0a 69 6e 74 20 71  .}..static.int q
357df 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 0a 20  ueryTokenizer(. 
357e0 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
357e1 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 0a   char *zName,  .
357e2 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
357e3 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
357e4 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e 74 20 72   **pp.){.  int r
357e5 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  c;.  sqlite3_stm
357e6 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73  t *pStmt;.  cons
357e7 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20  t char zSql[] = 
357e8 22 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b  "SELECT fts3_tok
357e9 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a 20 20 2a  enizer(?)";..  *
357ea 70 70 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73  pp = 0;.  rc = s
357eb 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
357ec 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20  2(db, zSql, -1, 
357ed 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66  &pStmt, 0);.  if
357ee 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
357ef 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
357f0 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
357f1 5f 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74  _bind_text(pStmt
357f2 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20  , 1, zName, -1, 
357f3 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
357f4 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57    if( SQLITE_ROW
357f5 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  ==sqlite3_step(p
357f6 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 69 66 28  Stmt) ){.    if(
357f7 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
357f8 74 79 70 65 28 70 53 74 6d 74 2c 20 30 29 3d 3d  type(pStmt, 0)==
357f9 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20  SQLITE_BLOB ){. 
357fa 20 20 20 20 20 6d 65 6d 63 70 79 28 70 70 2c 20       memcpy(pp, 
357fb 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
357fc 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 2c 20 73  lob(pStmt, 0), s
357fd 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a 20 20 20  izeof(*pp));.   
357fe 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
357ff 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
35800 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 53 51 4c  e(pStmt);.}..SQL
35801 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
35802 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70   sqlite3Fts3Simp
35803 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
35804 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
35805 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
35806 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 2f 2a  **ppModule);../*
35807 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
35808 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61 72  on of the scalar
35809 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f 74   function fts3_t
3580a 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61  okenizer_interna
3580b 6c 5f 74 65 73 74 28 29 2e 0a 2a 2a 20 54 68 69  l_test()..** Thi
3580c 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
3580d 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 6f  ed for testing o
3580e 6e 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 69  nly, it is not i
3580f 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 0a 2a  ncluded in the.*
35810 2a 20 62 75 69 6c 64 20 75 6e 6c 65 73 73 20 53  * build unless S
35811 51 4c 49 54 45 5f 54 45 53 54 20 69 73 20 64 65  QLITE_TEST is de
35812 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  fined..**.** The
35813 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73   purpose of this
35814 20 69 73 20 74 6f 20 74 65 73 74 20 74 68 61 74   is to test that
35815 20 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69   the fts3_tokeni
35816 7a 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a  zer() function.*
35817 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73  * can be used as
35818 20 64 65 73 69 67 6e 65 64 20 62 79 20 74 68 65   designed by the
35819 20 43 2d 63 6f 64 65 20 69 6e 20 74 68 65 20 71   C-code in the q
3581a 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 20 61 6e  ueryTokenizer an
3581b 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 54 6f 6b  d.** registerTok
3581c 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74 69 6f  enizer() functio
3581d 6e 73 20 61 62 6f 76 65 2e 20 54 68 65 73 65 20  ns above. These 
3581e 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  two functions ar
3581f 65 20 72 65 70 65 61 74 65 64 0a 2a 2a 20 69 6e  e repeated.** in
35820 20 74 68 65 20 52 45 41 44 4d 45 2e 74 6f 6b 65   the README.toke
35821 6e 69 7a 65 72 20 66 69 6c 65 20 61 73 20 61 6e  nizer file as an
35822 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20 69 74 20   example, so it 
35823 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 0a  is important to.
35824 2a 2a 20 74 65 73 74 20 74 68 65 6d 2e 0a 2a 2a  ** test them..**
35825 0a 2a 2a 20 54 6f 20 72 75 6e 20 74 68 65 20 74  .** To run the t
35826 65 73 74 73 2c 20 65 76 61 6c 75 61 74 65 20 74  ests, evaluate t
35827 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  he fts3_tokenize
35828 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28  r_internal_test(
35829 29 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63  ) scalar.** func
3582a 74 69 6f 6e 20 77 69 74 68 20 6e 6f 20 61 72 67  tion with no arg
3582b 75 6d 65 6e 74 73 2e 20 41 6e 20 61 73 73 65 72  uments. An asser
3582c 74 28 29 20 77 69 6c 6c 20 66 61 69 6c 20 69 66  t() will fail if
3582d 20 61 20 70 72 6f 62 6c 65 6d 20 69 73 0a 2a 2a   a problem is.**
3582e 20 64 65 74 65 63 74 65 64 2e 20 69 2e 65 2e 3a   detected. i.e.:
3582f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
35830 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  T fts3_tokenizer
35831 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28 29  _internal_test()
35832 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ;.**.*/.static v
35833 6f 69 64 20 69 6e 74 54 65 73 74 46 75 6e 63 28  oid intTestFunc(
35834 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
35835 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
35836 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
35837 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
35838 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63  ){.  int rc;.  c
35839 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
3583a 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
3583b 31 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  1;.  const sqlit
3583c 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3583d 75 6c 65 20 2a 70 32 3b 0a 20 20 73 71 6c 69 74  ule *p2;.  sqlit
3583e 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65  e3 *db = (sqlite
3583f 33 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72  3 *)sqlite3_user
35840 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a  _data(context);.
35841 0a 20 20 2f 2a 20 54 65 73 74 20 74 68 65 20 71  .  /* Test the q
35842 75 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  uery function */
35843 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 69  .  sqlite3Fts3Si
35844 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
35845 75 6c 65 28 26 70 31 29 3b 0a 20 20 72 63 20 3d  ule(&p1);.  rc =
35846 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28   queryTokenizer(
35847 64 62 2c 20 22 73 69 6d 70 6c 65 22 2c 20 26 70  db, "simple", &p
35848 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  2);.  assert( rc
35849 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
3584a 20 61 73 73 65 72 74 28 20 70 31 3d 3d 70 32 20   assert( p1==p2 
3584b 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54  );.  rc = queryT
3584c 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f  okenizer(db, "no
3584d 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20  suchtokenizer", 
3584e 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20  &p2);.  assert( 
3584f 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52  rc==SQLITE_ERROR
35850 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 32   );.  assert( p2
35851 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
35852 20 30 3d 3d 73 74 72 63 6d 70 28 73 71 6c 69 74   0==strcmp(sqlit
35853 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 22  e3_errmsg(db), "
35854 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65  unknown tokenize
35855 72 3a 20 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a  r: nosuchtokeniz
35856 65 72 22 29 20 29 3b 0a 0a 20 20 2f 2a 20 54 65  er") );..  /* Te
35857 73 74 20 74 68 65 20 73 74 6f 72 61 67 65 20 66  st the storage f
35858 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20  unction */.  rc 
35859 3d 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69  = registerTokeni
3585a 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74  zer(db, "nosucht
3585b 6f 6b 65 6e 69 7a 65 72 22 2c 20 70 31 29 3b 0a  okenizer", p1);.
3585c 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
3585d 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20  LITE_OK );.  rc 
3585e 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72  = queryTokenizer
3585f 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65  (db, "nosuchtoke
35860 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a 20 20  nizer", &p2);.  
35861 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
35862 54 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73 65 72  TE_OK );.  asser
35863 74 28 20 70 32 3d 3d 70 31 20 29 3b 0a 0a 20 20  t( p2==p1 );..  
35864 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
35865 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 6f 6b  ext(context, "ok
35866 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54  ", -1, SQLITE_ST
35867 41 54 49 43 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  ATIC);.}..#endif
35868 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 53  ../*.** Set up S
35869 51 4c 20 6f 62 6a 65 63 74 73 20 69 6e 20 64 61  QL objects in da
3586a 74 61 62 61 73 65 20 64 62 20 75 73 65 64 20 74  tabase db used t
3586b 6f 20 61 63 63 65 73 73 20 74 68 65 20 63 6f 6e  o access the con
3586c 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20  tents of.** the 
3586d 68 61 73 68 20 74 61 62 6c 65 20 70 6f 69 6e 74  hash table point
3586e 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e  ed to by argumen
3586f 74 20 70 48 61 73 68 2e 20 54 68 65 20 68 61 73  t pHash. The has
35870 68 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a 20  h table must.** 
35871 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 73 65 64  been initialised
35872 20 74 6f 20 75 73 65 20 73 74 72 69 6e 67 20 6b   to use string k
35873 65 79 73 2c 20 61 6e 64 20 74 6f 20 74 61 6b 65  eys, and to take
35874 20 61 20 70 72 69 76 61 74 65 20 63 6f 70 79 20   a private copy 
35875 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65 79 20 77  .** of the key w
35876 68 65 6e 20 61 20 76 61 6c 75 65 20 69 73 20 69  hen a value is i
35877 6e 73 65 72 74 65 64 2e 20 69 2e 65 2e 20 62 79  nserted. i.e. by
35878 20 61 20 63 61 6c 6c 20 73 69 6d 69 6c 61 72 20   a call similar 
35879 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c  to:.**.**    sql
3587a 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
3587b 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48 41 53  (pHash, FTS3_HAS
3587c 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 2a 2a  H_STRING, 1);.**
3587d 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
3587e 6e 20 61 64 64 73 20 61 20 73 63 61 6c 61 72 20  n adds a scalar 
3587f 66 75 6e 63 74 69 6f 6e 20 28 73 65 65 20 68 65  function (see he
35880 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62 6f  ader comment abo
35881 76 65 0a 2a 2a 20 73 63 61 6c 61 72 46 75 6e 63  ve.** scalarFunc
35882 28 29 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  () in this file 
35883 66 6f 72 20 64 65 74 61 69 6c 73 29 20 61 6e 64  for details) and
35884 2c 20 69 66 20 45 4e 41 42 4c 45 5f 54 41 42 4c  , if ENABLE_TABL
35885 45 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20  E is.** defined 
35886 61 74 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 74  at compilation t
35887 69 6d 65 2c 20 61 20 74 65 6d 70 6f 72 61 72 79  ime, a temporary
35888 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 28   virtual table (
35889 73 65 65 20 68 65 61 64 65 72 20 0a 2a 2a 20 63  see header .** c
3588a 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 73 74 72  omment above str
3588b 75 63 74 20 48 61 73 68 54 61 62 6c 65 56 74 61  uct HashTableVta
3588c 62 29 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  b) to the databa
3588d 73 65 20 73 63 68 65 6d 61 2e 20 42 6f 74 68 20  se schema. Both 
3588e 0a 2a 2a 20 70 72 6f 76 69 64 65 20 72 65 61 64  .** provide read
3588f 2f 77 72 69 74 65 20 61 63 63 65 73 73 20 74 6f  /write access to
35890 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
35891 20 2a 70 48 61 73 68 2e 0a 2a 2a 0a 2a 2a 20 54   *pHash..**.** T
35892 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
35893 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
35894 6f 6e 2c 20 7a 4e 61 6d 65 2c 20 69 73 20 75 73  on, zName, is us
35895 65 64 20 61 73 20 74 68 65 20 6e 61 6d 65 0a 2a  ed as the name.*
35896 2a 20 6f 66 20 62 6f 74 68 20 74 68 65 20 73 63  * of both the sc
35897 61 6c 61 72 20 61 6e 64 2c 20 69 66 20 63 72 65  alar and, if cre
35898 61 74 65 64 2c 20 74 68 65 20 76 69 72 74 75 61  ated, the virtua
35899 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  l table..*/.SQLI
3589a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3589b 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
3589c 73 68 54 61 62 6c 65 28 0a 20 20 73 71 6c 69 74  shTable(.  sqlit
3589d 65 33 20 2a 64 62 2c 20 0a 20 20 66 74 73 33 48  e3 *db, .  fts3H
3589e 61 73 68 20 2a 70 48 61 73 68 2c 20 0a 20 20 63  ash *pHash, .  c
3589f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
358a0 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
358a1 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 76 6f 69 64  QLITE_OK;.  void
358a2 20 2a 70 20 3d 20 28 76 6f 69 64 20 2a 29 70 48   *p = (void *)pH
358a3 61 73 68 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74  ash;.  const int
358a4 20 61 6e 79 20 3d 20 53 51 4c 49 54 45 5f 41 4e   any = SQLITE_AN
358a5 59 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 73 74  Y;.  char *zTest
358a6 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 54   = 0;.  char *zT
358a7 65 73 74 32 20 3d 20 30 3b 0a 0a 23 69 66 64 65  est2 = 0;..#ifde
358a8 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
358a9 76 6f 69 64 20 2a 70 64 62 20 3d 20 28 76 6f 69  void *pdb = (voi
358aa 64 20 2a 29 64 62 3b 0a 20 20 7a 54 65 73 74 20  d *)db;.  zTest 
358ab 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
358ac 66 28 22 25 73 5f 74 65 73 74 22 2c 20 7a 4e 61  f("%s_test", zNa
358ad 6d 65 29 3b 0a 20 20 7a 54 65 73 74 32 20 3d 20  me);.  zTest2 = 
358ae 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
358af 22 25 73 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73  "%s_internal_tes
358b0 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66  t", zName);.  if
358b1 28 20 21 7a 54 65 73 74 20 7c 7c 20 21 7a 54 65  ( !zTest || !zTe
358b2 73 74 32 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  st2 ){.    rc = 
358b3 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
358b4 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  }.#endif..  if( 
358b5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20  rc!=SQLITE_OK.  
358b6 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65   || (rc = sqlite
358b7 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
358b8 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 31 2c 20  n(db, zName, 1, 
358b9 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72 46 75  any, p, scalarFu
358ba 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c  nc, 0, 0)).   ||
358bb 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
358bc 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
358bd 62 2c 20 7a 4e 61 6d 65 2c 20 32 2c 20 61 6e 79  b, zName, 2, any
358be 2c 20 70 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c  , p, scalarFunc,
358bf 20 30 2c 20 30 29 29 0a 23 69 66 64 65 66 20 53   0, 0)).#ifdef S
358c0 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 7c 7c  QLITE_TEST.   ||
358c1 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
358c2 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
358c3 62 2c 20 7a 54 65 73 74 2c 20 32 2c 20 61 6e 79  b, zTest, 2, any
358c4 2c 20 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30  , p, testFunc, 0
358c5 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20  , 0)).   || (rc 
358c6 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
358c7 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54  _function(db, zT
358c8 65 73 74 2c 20 33 2c 20 61 6e 79 2c 20 70 2c 20  est, 3, any, p, 
358c9 74 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29  testFunc, 0, 0))
358ca 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c  .   || (rc = sql
358cb 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
358cc 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 32 2c  tion(db, zTest2,
358cd 20 30 2c 20 61 6e 79 2c 20 70 64 62 2c 20 69 6e   0, any, pdb, in
358ce 74 54 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29  tTestFunc, 0, 0)
358cf 29 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 0a 20  ).#endif.  );.. 
358d0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
358d1 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  est);.  sqlite3_
358d2 66 72 65 65 28 7a 54 65 73 74 32 29 3b 0a 20 20  free(zTest2);.  
358d3 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65  return rc;.}..#e
358d4 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
358d5 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
358d6 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
358d7 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
358d8 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
358d9 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b   End of fts3_tok
358da 65 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  enizer.c *******
358db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
358dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
358dd 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
358de 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
358df 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a  _tokenizer1.c **
358e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
358e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
358e2 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20  ./*.** 2006 Oct 
358e3 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  10.**.** The aut
358e4 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
358e5 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
358e6 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
358e7 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
358e8 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
358e9 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
358ea 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
358eb 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
358ec 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
358ed 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
358ee 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
358ef 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
358f0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
358f1 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
358f2 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
358f3 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
358f4 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
358f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
358f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
358f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
358f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
358f9 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70  ******.**.** Imp
358fa 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
358fb 68 65 20 22 73 69 6d 70 6c 65 22 20 66 75 6c 6c  he "simple" full
358fc 2d 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b  -text-search tok
358fd 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  enizer..*/../*.*
358fe 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
358ff 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
35900 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a  compiled if:.**.
35901 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
35902 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
35903 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78  g built as an ex
35904 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  tension.**      
35905 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
35906 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e  SQLITE_CORE is n
35907 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a  ot defined), or.
35908 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
35909 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
3590a 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20  eing built into 
3590b 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20  the core of.**  
3590c 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20       SQLite (in 
3590d 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
3590e 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73  E_ENABLE_FTS3 is
3590f 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69   defined)..*/.#i
35910 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
35911 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
35912 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
35913 5f 46 54 53 33 29 0a 0a 0a 0a 0a 74 79 70 65 64  _FTS3).....typed
35914 65 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65  ef struct simple
35915 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73  _tokenizer {.  s
35916 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
35917 20 62 61 73 65 3b 0a 20 20 63 68 61 72 20 64 65   base;.  char de
35918 6c 69 6d 5b 31 32 38 5d 3b 20 20 20 20 20 20 20  lim[128];       
35919 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 20 41 53        /* flag AS
3591a 43 49 49 20 64 65 6c 69 6d 69 74 65 72 73 20 2a  CII delimiters *
3591b 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  /.} simple_token
3591c 69 7a 65 72 3b 0a 0a 74 79 70 65 64 65 66 20 73  izer;..typedef s
3591d 74 72 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b  truct simple_tok
3591e 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a  enizer_cursor {.
3591f 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
35920 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  zer_cursor base;
35921 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
35922 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20  Input;          
35923 2f 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20  /* input we are 
35924 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20  tokenizing */.  
35925 69 6e 74 20 6e 42 79 74 65 73 3b 20 20 20 20 20  int nBytes;     
35926 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35927 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75  size of the inpu
35928 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  t */.  int iOffs
35929 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  et;             
3592a 20 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70      /* current p
3592b 6f 73 69 74 69 6f 6e 20 69 6e 20 70 49 6e 70 75  osition in pInpu
3592c 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65  t */.  int iToke
3592d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
3592e 20 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20      /* index of 
3592f 6e 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65  next token to be
35930 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63   returned */.  c
35931 68 61 72 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20  har *pToken;    
35932 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
35933 74 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65  torage for curre
35934 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  nt token */.  in
35935 74 20 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65  t nTokenAllocate
35936 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70  d;         /* sp
35937 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f  ace allocated to
35938 20 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a   zToken buffer *
35939 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  /.} simple_token
3593a 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f  izer_cursor;.../
3593b 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
3593c 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20  ation */.static 
3593d 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
3593e 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73  kenizer_module s
3593f 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
35940 64 75 6c 65 3b 0a 0a 73 74 61 74 69 63 20 69 6e  dule;..static in
35941 74 20 73 69 6d 70 6c 65 44 65 6c 69 6d 28 73 69  t simpleDelim(si
35942 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  mple_tokenizer *
35943 74 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  t, unsigned char
35944 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 3c   c){.  return c<
35945 30 78 38 30 20 26 26 20 74 2d 3e 64 65 6c 69 6d  0x80 && t->delim
35946 5b 63 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  [c];.}../*.** Cr
35947 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e  eate a new token
35948 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a  izer instance..*
35949 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d  /.static int sim
3594a 70 6c 65 43 72 65 61 74 65 28 0a 20 20 69 6e 74  pleCreate(.  int
3594b 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
3594c 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
3594d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
3594e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a  izer **ppTokeniz
3594f 65 72 0a 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74  er.){.  simple_t
35950 6f 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a 0a 20 20  okenizer *t;..  
35951 74 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65  t = (simple_toke
35952 6e 69 7a 65 72 20 2a 29 20 73 71 6c 69 74 65 33  nizer *) sqlite3
35953 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
35954 74 29 29 3b 0a 20 20 69 66 28 20 74 3d 3d 4e 55  t));.  if( t==NU
35955 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  LL ) return SQLI
35956 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73  TE_NOMEM;.  mems
35957 65 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28  et(t, 0, sizeof(
35958 2a 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f  *t));..  /* TODO
35959 28 73 68 65 73 73 29 20 44 65 6c 69 6d 69 74 65  (shess) Delimite
3595a 72 73 20 6e 65 65 64 20 74 6f 20 72 65 6d 61 69  rs need to remai
3595b 6e 20 74 68 65 20 73 61 6d 65 20 66 72 6f 6d 20  n the same from 
3595c 72 75 6e 20 74 6f 20 72 75 6e 2c 0a 20 20 2a 2a  run to run,.  **
3595d 20 65 6c 73 65 20 77 65 20 6e 65 65 64 20 74 6f   else we need to
3595e 20 72 65 69 6e 64 65 78 2e 20 20 4f 6e 65 20 73   reindex.  One s
3595f 6f 6c 75 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65  olution would be
35960 20 61 20 6d 65 74 61 2d 74 61 62 6c 65 20 74 6f   a meta-table to
35961 0a 20 20 2a 2a 20 74 72 61 63 6b 20 73 75 63 68  .  ** track such
35962 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20   information in 
35963 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 68  the database, th
35964 65 6e 20 77 65 27 64 20 6f 6e 6c 79 20 77 61 6e  en we'd only wan
35965 74 20 74 68 69 73 0a 20 20 2a 2a 20 69 6e 66 6f  t this.  ** info
35966 72 6d 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69  rmation on the i
35967 6e 69 74 69 61 6c 20 63 72 65 61 74 65 2e 0a 20  nitial create.. 
35968 20 2a 2f 0a 20 20 69 66 28 20 61 72 67 63 3e 31   */.  if( argc>1
35969 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e   ){.    int i, n
3596a 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 31   = strlen(argv[1
3596b 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ]);.    for(i=0;
3596c 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<n; i++){.    
3596d 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
3596e 63 68 20 3d 20 61 72 67 76 5b 31 5d 5b 69 5d 3b  ch = argv[1][i];
3596f 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 65 78 70  .      /* We exp
35970 6c 69 63 69 74 6c 79 20 64 6f 6e 27 74 20 73 75  licitly don't su
35971 70 70 6f 72 74 20 55 54 46 2d 38 20 64 65 6c 69  pport UTF-8 deli
35972 6d 69 74 65 72 73 20 66 6f 72 20 6e 6f 77 2e 20  miters for now. 
35973 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 63 68 3e  */.      if( ch>
35974 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20 20 20  =0x80 ){.       
35975 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 74 29   sqlite3_free(t)
35976 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
35977 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
35978 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 2d 3e       }.      t->
35979 64 65 6c 69 6d 5b 63 68 5d 20 3d 20 31 3b 0a 20  delim[ch] = 1;. 
3597a 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a     }.  } else {.
3597b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d      /* Mark non-
3597c 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 41 53 43  alphanumeric ASC
3597d 49 49 20 63 68 61 72 61 63 74 65 72 73 20 61 73  II characters as
3597e 20 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f 0a 20   delimiters */. 
3597f 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
35980 72 28 69 3d 31 3b 20 69 3c 30 78 38 30 3b 20 69  r(i=1; i<0x80; i
35981 2b 2b 29 7b 0a 20 20 20 20 20 20 74 2d 3e 64 65  ++){.      t->de
35982 6c 69 6d 5b 69 5d 20 3d 20 21 69 73 61 6c 6e 75  lim[i] = !isalnu
35983 6d 28 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  m(i);.    }.  }.
35984 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20  .  *ppTokenizer 
35985 3d 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20 72 65  = &t->base;.  re
35986 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
35987 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79  }../*.** Destroy
35988 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a   a tokenizer.*/.
35989 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c  static int simpl
3598a 65 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  eDestroy(sqlite3
3598b 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
3598c 65 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c 69 74  enizer){.  sqlit
3598d 65 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e 69 7a  e3_free(pTokeniz
3598e 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  er);.  return SQ
3598f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
35990 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67  * Prepare to beg
35991 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20  in tokenizing a 
35992 70 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e  particular strin
35993 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a  g.  The input.**
35994 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f   string to be to
35995 6b 65 6e 69 7a 65 64 20 69 73 20 70 49 6e 70 75  kenized is pInpu
35996 74 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e 20  t[0..nBytes-1]. 
35997 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65   A cursor.** use
35998 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c  d to incremental
35999 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73  ly tokenize this
3599a 20 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72   string is retur
3599b 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75  ned in .** *ppCu
3599c 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rsor..*/.static 
3599d 69 6e 74 20 73 69 6d 70 6c 65 4f 70 65 6e 28 0a  int simpleOpen(.
3599e 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3599f 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c  zer *pTokenizer,
359a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
359a1 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63  tokenizer */.  c
359a2 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75  onst char *pInpu
359a3 74 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  t, int nBytes,  
359a4 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
359a5 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
359a6 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
359a7 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a  enizer_cursor **
359a8 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f  ppCursor    /* O
359a9 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e  UT: Tokenization
359aa 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20   cursor */.){.  
359ab 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
359ac 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20 63  _cursor *c;..  c
359ad 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e   = (simple_token
359ae 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 73  izer_cursor *) s
359af 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
359b0 7a 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69 66 28  zeof(*c));.  if(
359b1 20 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72   c==NULL ) retur
359b2 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
359b3 0a 20 20 63 2d 3e 70 49 6e 70 75 74 20 3d 20 70  .  c->pInput = p
359b4 49 6e 70 75 74 3b 0a 20 20 69 66 28 20 70 49 6e  Input;.  if( pIn
359b5 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 63 2d  put==0 ){.    c-
359b6 3e 6e 42 79 74 65 73 20 3d 20 30 3b 0a 20 20 7d  >nBytes = 0;.  }
359b7 65 6c 73 65 20 69 66 28 20 6e 42 79 74 65 73 3c  else if( nBytes<
359b8 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74  0 ){.    c->nByt
359b9 65 73 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e  es = (int)strlen
359ba 28 70 49 6e 70 75 74 29 3b 0a 20 20 7d 65 6c 73  (pInput);.  }els
359bb 65 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65 73  e{.    c->nBytes
359bc 20 3d 20 6e 42 79 74 65 73 3b 0a 20 20 7d 0a 20   = nBytes;.  }. 
359bd 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b   c->iOffset = 0;
359be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359bf 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65 6e 69   /* start tokeni
359c0 7a 69 6e 67 20 61 74 20 74 68 65 20 62 65 67 69  zing at the begi
359c1 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e 69 54  nning */.  c->iT
359c2 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d 3e 70  oken = 0;.  c->p
359c3 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20 20 20  Token = NULL;   
359c4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
359c5 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  o space allocate
359c6 64 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63 2d 3e  d, yet. */.  c->
359c7 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20  nTokenAllocated 
359c8 3d 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f  = 0;..  *ppCurso
359c9 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20  r = &c->base;.  
359ca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
359cb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
359cc 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
359cd 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c  cursor previousl
359ce 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61  y opened by a ca
359cf 6c 6c 20 74 6f 0a 2a 2a 20 73 69 6d 70 6c 65 4f  ll to.** simpleO
359d0 70 65 6e 28 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a  pen() above..*/.
359d1 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c  static int simpl
359d2 65 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 74  eClose(sqlite3_t
359d3 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
359d4 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 73 69 6d  *pCursor){.  sim
359d5 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ple_tokenizer_cu
359d6 72 73 6f 72 20 2a 63 20 3d 20 28 73 69 6d 70 6c  rsor *c = (simpl
359d7 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  e_tokenizer_curs
359d8 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20  or *) pCursor;. 
359d9 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 2d   sqlite3_free(c-
359da 3e 70 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69  >pToken);.  sqli
359db 74 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20 72  te3_free(c);.  r
359dc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
359dd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  .}../*.** Extrac
359de 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  t the next token
359df 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61   from a tokeniza
359e0 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20 54 68  tion cursor.  Th
359e1 65 20 63 75 72 73 6f 72 20 6d 75 73 74 0a 2a 2a  e cursor must.**
359e2 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65   have been opene
359e3 64 20 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c  d by a prior cal
359e4 6c 20 74 6f 20 73 69 6d 70 6c 65 4f 70 65 6e 28  l to simpleOpen(
359e5 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
359e6 20 73 69 6d 70 6c 65 4e 65 78 74 28 0a 20 20 73   simpleNext(.  s
359e7 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
359e8 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
359e9 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74  ,  /* Cursor ret
359ea 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f  urned by simpleO
359eb 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  pen */.  const c
359ec 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20  har **ppToken,  
359ed 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
359ee 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69 73  OUT: *ppToken is
359ef 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
359f0 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65  */.  int *pnByte
359f1 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
359f2 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
359f3 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
359f4 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   in token */.  i
359f5 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65  nt *piStartOffse
359f6 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
359f7 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74     /* OUT: Start
359f8 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f  ing offset of to
359f9 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
359fa 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20 20  EndOffset,      
359fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
359fc 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73  OUT: Ending offs
359fd 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
359fe 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e   int *piPosition
359ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a00 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73       /* OUT: Pos
35a01 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66  ition integer of
35a02 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 73   token */.){.  s
35a03 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
35a04 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 73 69 6d  cursor *c = (sim
35a05 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ple_tokenizer_cu
35a06 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b  rsor *) pCursor;
35a07 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .  simple_tokeni
35a08 7a 65 72 20 2a 74 20 3d 20 28 73 69 6d 70 6c 65  zer *t = (simple
35a09 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 70 43  _tokenizer *) pC
35a0a 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65  ursor->pTokenize
35a0b 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  r;.  unsigned ch
35a0c 61 72 20 2a 70 20 3d 20 28 75 6e 73 69 67 6e 65  ar *p = (unsigne
35a0d 64 20 63 68 61 72 20 2a 29 63 2d 3e 70 49 6e 70  d char *)c->pInp
35a0e 75 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20 63 2d  ut;..  while( c-
35a0f 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74  >iOffset<c->nByt
35a10 65 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 53  es ){.    int iS
35a11 74 61 72 74 4f 66 66 73 65 74 3b 0a 0a 20 20 20  tartOffset;..   
35a12 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20 64 65   /* Scan past de
35a13 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74 65  limiter characte
35a14 72 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  rs */.    while(
35a15 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e   c->iOffset<c->n
35a16 42 79 74 65 73 20 26 26 20 73 69 6d 70 6c 65 44  Bytes && simpleD
35a17 65 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66  elim(t, p[c->iOf
35a18 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20  fset]) ){.      
35a19 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20  c->iOffset++;.  
35a1a 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 75 6e    }..    /* Coun
35a1b 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65 72 20  t non-delimiter 
35a1c 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f 0a 20  characters. */. 
35a1d 20 20 20 69 53 74 61 72 74 4f 66 66 73 65 74 20     iStartOffset 
35a1e 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20  = c->iOffset;.  
35a1f 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66    while( c->iOff
35a20 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 26 26  set<c->nBytes &&
35a21 20 21 73 69 6d 70 6c 65 44 65 6c 69 6d 28 74 2c   !simpleDelim(t,
35a22 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20   p[c->iOffset]) 
35a23 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66  ){.      c->iOff
35a24 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  set++;.    }..  
35a25 20 20 69 66 28 20 63 2d 3e 69 4f 66 66 73 65 74    if( c->iOffset
35a26 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20 29 7b  >iStartOffset ){
35a27 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6e 20  .      int i, n 
35a28 3d 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53 74  = c->iOffset-iSt
35a29 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  artOffset;.     
35a2a 20 69 66 28 20 6e 3e 63 2d 3e 6e 54 6f 6b 65 6e   if( n>c->nToken
35a2b 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a 20 20 20  Allocated ){.   
35a2c 20 20 20 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c       c->nTokenAl
35a2d 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a  located = n+20;.
35a2e 20 20 20 20 20 20 20 20 63 2d 3e 70 54 6f 6b 65          c->pToke
35a2f 6e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  n = sqlite3_real
35a30 6c 6f 63 28 63 2d 3e 70 54 6f 6b 65 6e 2c 20 63  loc(c->pToken, c
35a31 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65  ->nTokenAllocate
35a32 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
35a33 63 2d 3e 70 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20  c->pToken==NULL 
35a34 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
35a35 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
35a36 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
35a37 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
35a38 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
35a39 54 68 69 73 20 6e 65 65 64 73 20 65 78 70 61 6e  This needs expan
35a3a 73 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65 20 55  sion to handle U
35a3b 54 46 2d 38 0a 20 20 20 20 20 20 20 20 2a 2a 20  TF-8.        ** 
35a3c 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 69  case-insensitivi
35a3d 74 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ty..        */. 
35a3e 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20         unsigned 
35a3f 63 68 61 72 20 63 68 20 3d 20 70 5b 69 53 74 61  char ch = p[iSta
35a40 72 74 4f 66 66 73 65 74 2b 69 5d 3b 0a 20 20 20  rtOffset+i];.   
35a41 20 20 20 20 20 63 2d 3e 70 54 6f 6b 65 6e 5b 69       c->pToken[i
35a42 5d 20 3d 20 63 68 3c 30 78 38 30 20 3f 20 74 6f  ] = ch<0x80 ? to
35a43 6c 6f 77 65 72 28 63 68 29 20 3a 20 63 68 3b 0a  lower(ch) : ch;.
35a44 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70        }.      *p
35a45 70 54 6f 6b 65 6e 20 3d 20 63 2d 3e 70 54 6f 6b  pToken = c->pTok
35a46 65 6e 3b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74  en;.      *pnByt
35a47 65 73 20 3d 20 6e 3b 0a 20 20 20 20 20 20 2a 70  es = n;.      *p
35a48 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 69  iStartOffset = i
35a49 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20  StartOffset;.   
35a4a 20 20 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20     *piEndOffset 
35a4b 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20  = c->iOffset;.  
35a4c 20 20 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20      *piPosition 
35a4d 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a  = c->iToken++;..
35a4e 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
35a4f 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
35a50 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
35a51 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  E_DONE;.}../*.**
35a52 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f 75 74   The set of rout
35a53 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ines that implem
35a54 65 6e 74 20 74 68 65 20 73 69 6d 70 6c 65 20 74  ent the simple t
35a55 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74  okenizer.*/.stat
35a56 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
35a57 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
35a58 65 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65  e simpleTokenize
35a59 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c  rModule = {.  0,
35a5a 0a 20 20 73 69 6d 70 6c 65 43 72 65 61 74 65 2c  .  simpleCreate,
35a5b 0a 20 20 73 69 6d 70 6c 65 44 65 73 74 72 6f 79  .  simpleDestroy
35a5c 2c 0a 20 20 73 69 6d 70 6c 65 4f 70 65 6e 2c 0a  ,.  simpleOpen,.
35a5d 20 20 73 69 6d 70 6c 65 43 6c 6f 73 65 2c 0a 20    simpleClose,. 
35a5e 20 73 69 6d 70 6c 65 4e 65 78 74 2c 0a 7d 3b 0a   simpleNext,.};.
35a5f 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
35a60 61 20 6e 65 77 20 73 69 6d 70 6c 65 20 74 6f 6b  a new simple tok
35a61 65 6e 69 7a 65 72 2e 20 20 52 65 74 75 72 6e 20  enizer.  Return 
35a62 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
35a63 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65   new.** tokenize
35a64 72 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a  r in *ppModule.*
35a65 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
35a66 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
35a67 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  3SimpleTokenizer
35a68 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  Module(.  sqlite
35a69 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
35a6a 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75  le const**ppModu
35a6b 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c  le.){.  *ppModul
35a6c 65 20 3d 20 26 73 69 6d 70 6c 65 54 6f 6b 65 6e  e = &simpleToken
35a6d 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23  izerModule;.}..#
35a6e 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
35a6f 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
35a70 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
35a71 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
35a72 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
35a73 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f  * End of fts3_to
35a74 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a  kenizer1.c *****
35a75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a77 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
35a78 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 74 72  * Begin file rtr
35a79 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ee.c ***********
35a7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a7c 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
35a7d 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
35a7e 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
35a7f 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
35a80 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
35a81 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
35a82 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
35a83 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
35a84 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
35a85 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
35a86 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
35a87 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
35a88 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
35a89 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
35a8a 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
35a8b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
35a8c 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
35a8d 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
35a8e 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
35a8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a93 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
35a94 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
35a95 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e  ode for implemen
35a96 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 72  tations of the r
35a97 2d 74 72 65 65 20 61 6e 64 20 72 2a 2d 74 72 65  -tree and r*-tre
35a98 65 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 73 20  e.** algorithms 
35a99 70 61 63 6b 61 67 65 64 20 61 73 20 61 6e 20 53  packaged as an S
35a9a 51 4c 69 74 65 20 76 69 72 74 75 61 6c 20 74 61  QLite virtual ta
35a9b 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a  ble module..**.*
35a9c 2a 20 24 49 64 3a 20 72 74 72 65 65 2e 63 2c 76  * $Id: rtree.c,v
35a9d 20 31 2e 31 32 20 32 30 30 38 2f 31 32 2f 32 32   1.12 2008/12/22
35a9e 20 31 35 3a 30 34 3a 33 32 20 64 61 6e 69 65 6c   15:04:32 daniel
35a9f 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
35aa0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
35aa1 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
35aa2 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
35aa3 4c 45 5f 52 54 52 45 45 29 0a 0a 2f 2a 0a 2a 2a  LE_RTREE)../*.**
35aa4 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
35aa5 69 6e 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74  ins an implement
35aa6 61 74 69 6f 6e 20 6f 66 20 61 20 63 6f 75 70 6c  ation of a coupl
35aa7 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 76  e of different v
35aa8 61 72 69 61 6e 74 73 0a 2a 2a 20 6f 66 20 74 68  ariants.** of th
35aa9 65 20 72 2d 74 72 65 65 20 61 6c 67 6f 72 69 74  e r-tree algorit
35aaa 68 6d 2e 20 53 65 65 20 74 68 65 20 52 45 41 44  hm. See the READ
35aab 4d 45 20 66 69 6c 65 20 66 6f 72 20 66 75 72 74  ME file for furt
35aac 68 65 72 20 64 65 74 61 69 6c 73 2e 20 54 68 65  her details. The
35aad 20 0a 2a 2a 20 73 61 6d 65 20 64 61 74 61 2d 73   .** same data-s
35aae 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
35aaf 20 66 6f 72 20 61 6c 6c 2c 20 62 75 74 20 74 68   for all, but th
35ab0 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 66 6f 72  e algorithms for
35ab1 20 69 6e 73 65 72 74 20 61 6e 64 0a 2a 2a 20 64   insert and.** d
35ab2 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 73  elete operations
35ab3 20 76 61 72 79 2e 20 54 68 65 20 76 61 72 69 61   vary. The varia
35ab4 6e 74 73 20 75 73 65 64 20 61 72 65 20 73 65 6c  nts used are sel
35ab5 65 63 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  ected at compile
35ab6 20 74 69 6d 65 20 0a 2a 2a 20 62 79 20 64 65 66   time .** by def
35ab7 69 6e 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77  ining the follow
35ab8 69 6e 67 20 73 79 6d 62 6f 6c 73 3a 0a 2a 2f 0a  ing symbols:.*/.
35ab9 0a 2f 2a 20 45 69 74 68 65 72 2c 20 62 6f 74 68  ./* Either, both
35aba 20 6f 72 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   or none of the 
35abb 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 79 20 62 65  following may be
35abc 20 73 65 74 20 74 6f 20 61 63 74 69 76 61 74 65   set to activate
35abd 20 0a 2a 2a 20 72 2a 74 72 65 65 20 76 61 72 69   .** r*tree vari
35abe 61 6e 74 20 61 6c 67 6f 72 69 74 68 6d 73 2e 0a  ant algorithms..
35abf 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41  */.#define VARIA
35ac0 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f  NT_RSTARTREE_CHO
35ac1 4f 53 45 53 55 42 54 52 45 45 20 30 0a 23 64 65  OSESUBTREE 0.#de
35ac2 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54  fine VARIANT_RST
35ac3 41 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 20  ARTREE_REINSERT 
35ac4 20 20 20 20 20 31 0a 0a 2f 2a 20 0a 2a 2a 20 45       1../* .** E
35ac5 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68  xactly one of th
35ac6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 75 73 74  e following must
35ac7 20 62 65 20 73 65 74 20 74 6f 20 31 2e 0a 2a 2f   be set to 1..*/
35ac8 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54  .#define VARIANT
35ac9 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54  _GUTTMAN_QUADRAT
35aca 49 43 5f 53 50 4c 49 54 20 30 0a 23 64 65 66 69  IC_SPLIT 0.#defi
35acb 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  ne VARIANT_GUTTM
35acc 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20  AN_LINEAR_SPLIT 
35acd 20 20 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52     0.#define VAR
35ace 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53  IANT_RSTARTREE_S
35acf 50 4c 49 54 20 20 20 20 20 20 20 20 20 31 0a 0a  PLIT         1..
35ad0 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f  #define VARIANT_
35ad1 47 55 54 54 4d 41 4e 5f 53 50 4c 49 54 20 5c 0a  GUTTMAN_SPLIT \.
35ad2 20 20 20 20 20 20 20 20 28 56 41 52 49 41 4e 54          (VARIANT
35ad3 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f  _GUTTMAN_LINEAR_
35ad4 53 50 4c 49 54 7c 7c 56 41 52 49 41 4e 54 5f 47  SPLIT||VARIANT_G
35ad5 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
35ad6 5f 53 50 4c 49 54 29 0a 0a 23 69 66 20 56 41 52  _SPLIT)..#if VAR
35ad7 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
35ad8 44 52 41 54 49 43 5f 53 50 4c 49 54 0a 20 20 23  DRATIC_SPLIT.  #
35ad9 64 65 66 69 6e 65 20 50 69 63 6b 4e 65 78 74 20  define PickNext 
35ada 51 75 61 64 72 61 74 69 63 50 69 63 6b 4e 65 78  QuadraticPickNex
35adb 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  t.  #define Pick
35adc 53 65 65 64 73 20 51 75 61 64 72 61 74 69 63 50  Seeds QuadraticP
35add 69 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69  ickSeeds.  #defi
35ade 6e 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73  ne AssignCells s
35adf 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a  plitNodeGuttman.
35ae0 23 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41  #endif.#if VARIA
35ae1 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41  NT_GUTTMAN_LINEA
35ae2 52 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e  R_SPLIT.  #defin
35ae3 65 20 50 69 63 6b 4e 65 78 74 20 4c 69 6e 65 61  e PickNext Linea
35ae4 72 50 69 63 6b 4e 65 78 74 0a 20 20 23 64 65 66  rPickNext.  #def
35ae5 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20 4c 69  ine PickSeeds Li
35ae6 6e 65 61 72 50 69 63 6b 53 65 65 64 73 0a 20 20  nearPickSeeds.  
35ae7 23 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65  #define AssignCe
35ae8 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65 47 75 74  lls splitNodeGut
35ae9 74 6d 61 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20  tman.#endif.#if 
35aea 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
35aeb 45 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e  E_SPLIT.  #defin
35aec 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70  e AssignCells sp
35aed 6c 69 74 4e 6f 64 65 53 74 61 72 74 72 65 65 0a  litNodeStartree.
35aee 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66  #endif...#ifndef
35aef 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53   SQLITE_CORE.  S
35af0 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f  QLITE_EXTENSION_
35af1 49 4e 49 54 31 0a 23 65 6c 73 65 0a 23 65 6e 64  INIT1.#else.#end
35af2 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  if...#ifndef SQL
35af3 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
35af4 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 33  .typedef sqlite3
35af5 5f 69 6e 74 36 34 20 69 36 34 3b 0a 74 79 70 65  _int64 i64;.type
35af6 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61  def unsigned cha
35af7 72 20 75 38 3b 0a 74 79 70 65 64 65 66 20 75 6e  r u8;.typedef un
35af8 73 69 67 6e 65 64 20 69 6e 74 20 75 33 32 3b 0a  signed int u32;.
35af9 23 65 6e 64 69 66 0a 0a 74 79 70 65 64 65 66 20  #endif..typedef 
35afa 73 74 72 75 63 74 20 52 74 72 65 65 20 52 74 72  struct Rtree Rtr
35afb 65 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ee;.typedef stru
35afc 63 74 20 52 74 72 65 65 43 75 72 73 6f 72 20 52  ct RtreeCursor R
35afd 74 72 65 65 43 75 72 73 6f 72 3b 0a 74 79 70 65  treeCursor;.type
35afe 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65  def struct Rtree
35aff 4e 6f 64 65 20 52 74 72 65 65 4e 6f 64 65 3b 0a  Node RtreeNode;.
35b00 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52  typedef struct R
35b01 74 72 65 65 43 65 6c 6c 20 52 74 72 65 65 43 65  treeCell RtreeCe
35b02 6c 6c 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ll;.typedef stru
35b03 63 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  ct RtreeConstrai
35b04 6e 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  nt RtreeConstrai
35b05 6e 74 3b 0a 74 79 70 65 64 65 66 20 75 6e 69 6f  nt;.typedef unio
35b06 6e 20 52 74 72 65 65 43 6f 6f 72 64 20 52 74 72  n RtreeCoord Rtr
35b07 65 65 43 6f 6f 72 64 3b 0a 0a 2f 2a 20 54 68 65  eeCoord;../* The
35b08 20 72 74 72 65 65 20 6d 61 79 20 68 61 76 65 20   rtree may have 
35b09 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 52 54  between 1 and RT
35b0a 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f  REE_MAX_DIMENSIO
35b0b 4e 53 20 64 69 6d 65 6e 73 69 6f 6e 73 2e 20 2a  NS dimensions. *
35b0c 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
35b0d 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 35  MAX_DIMENSIONS 5
35b0e 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 68 61 73  ../* Size of has
35b0f 68 20 74 61 62 6c 65 20 52 74 72 65 65 2e 61 48  h table Rtree.aH
35b10 61 73 68 2e 20 54 68 69 73 20 68 61 73 68 20 74  ash. This hash t
35b11 61 62 6c 65 20 69 73 20 6e 6f 74 20 65 78 70 65  able is not expe
35b12 63 74 65 64 20 74 6f 0a 2a 2a 20 65 76 65 72 20  cted to.** ever 
35b13 63 6f 6e 74 61 69 6e 20 76 65 72 79 20 6d 61 6e  contain very man
35b14 79 20 65 6e 74 72 69 65 73 2c 20 73 6f 20 61 20  y entries, so a 
35b15 66 69 78 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  fixed number of 
35b16 62 75 63 6b 65 74 73 20 69 73 20 0a 2a 2a 20 75  buckets is .** u
35b17 73 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  sed..*/.#define 
35b18 48 41 53 48 53 49 5a 45 20 31 32 38 0a 0a 2f 2a  HASHSIZE 128../*
35b19 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 76 69   .** An rtree vi
35b1a 72 74 75 61 6c 2d 74 61 62 6c 65 20 6f 62 6a 65  rtual-table obje
35b1b 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  ct..*/.struct Rt
35b1c 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ree {.  sqlite3_
35b1d 76 74 61 62 20 62 61 73 65 3b 0a 20 20 73 71 6c  vtab base;.  sql
35b1e 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
35b1f 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 73 74           /* Host
35b20 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
35b21 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4e  tion */.  int iN
35b22 6f 64 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  odeSize;        
35b23 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 69 6e        /* Size in
35b24 20 62 79 74 65 73 20 6f 66 20 65 61 63 68 20 6e   bytes of each n
35b25 6f 64 65 20 69 6e 20 74 68 65 20 6e 6f 64 65 20  ode in the node 
35b26 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
35b27 44 69 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Dim;            
35b28 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
35b29 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 73 20 2a   of dimensions *
35b2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 50 65  /.  int nBytesPe
35b2b 72 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  rCell;          
35b2c 2f 2a 20 42 79 74 65 73 20 63 6f 6e 73 75 6d 65  /* Bytes consume
35b2d 64 20 70 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20  d per cell */.  
35b2e 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20 20  int iDepth;     
35b2f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
35b30 75 72 72 65 6e 74 20 64 65 70 74 68 20 6f 66 20  urrent depth of 
35b31 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75 63  the r-tree struc
35b32 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ture */.  char *
35b33 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  zDb;            
35b34 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
35b35 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
35b36 6e 69 6e 67 20 72 2d 74 72 65 65 20 74 61 62 6c  ning r-tree tabl
35b37 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  e */.  char *zNa
35b38 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
35b39 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 72 2d     /* Name of r-
35b3a 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 20 0a 20  tree table */ . 
35b3b 20 52 74 72 65 65 4e 6f 64 65 20 2a 61 48 61 73   RtreeNode *aHas
35b3c 68 5b 48 41 53 48 53 49 5a 45 5d 3b 20 2f 2a 20  h[HASHSIZE]; /* 
35b3d 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 69 6e  Hash table of in
35b3e 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65 73 2e 20 2a  -memory nodes. *
35b3f 2f 20 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20  / .  int nBusy; 
35b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b41 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62   /* Current numb
35b42 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 74  er of users of t
35b43 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
35b44 0a 0a 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6e  ..  /* List of n
35b45 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 64 75 72  odes removed dur
35b46 69 6e 67 20 61 20 43 6f 6e 64 65 6e 73 65 54 72  ing a CondenseTr
35b47 65 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 4c 69  ee operation. Li
35b48 73 74 20 69 73 0a 20 20 2a 2a 20 6c 69 6e 6b 65  st is.  ** linke
35b49 64 20 74 6f 67 65 74 68 65 72 20 76 69 61 20 74  d together via t
35b4a 68 65 20 70 6f 69 6e 74 65 72 20 6e 6f 72 6d 61  he pointer norma
35b4b 6c 6c 79 20 75 73 65 64 20 66 6f 72 20 68 61 73  lly used for has
35b4c 68 20 63 68 61 69 6e 73 20 2d 0a 20 20 2a 2a 20  h chains -.  ** 
35b4d 52 74 72 65 65 4e 6f 64 65 2e 70 4e 65 78 74 2e  RtreeNode.pNext.
35b4e 20 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65   RtreeNode.iNode
35b4f 20 73 74 6f 72 65 73 20 74 68 65 20 64 65 70 74   stores the dept
35b50 68 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65  h of the sub-tre
35b51 65 20 0a 20 20 2a 2a 20 68 65 61 64 65 64 20 62  e .  ** headed b
35b52 79 20 74 68 65 20 6e 6f 64 65 20 28 6c 65 61 66  y the node (leaf
35b53 20 6e 6f 64 65 73 20 68 61 76 65 20 52 74 72 65   nodes have Rtre
35b54 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2e  eNode.iNode==0).
35b55 0a 20 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  .  */.  RtreeNod
35b56 65 20 2a 70 44 65 6c 65 74 65 64 3b 0a 20 20 69  e *pDeleted;.  i
35b57 6e 74 20 69 52 65 69 6e 73 65 72 74 48 65 69 67  nt iReinsertHeig
35b58 68 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 48 65  ht;        /* He
35b59 69 67 68 74 20 6f 66 20 73 75 62 2d 74 72 65 65  ight of sub-tree
35b5a 73 20 52 65 69 6e 73 65 72 74 28 29 20 68 61 73  s Reinsert() has
35b5b 20 72 75 6e 20 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a   run on */..  /*
35b5c 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72   Statements to r
35b5d 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65  ead/write/delete
35b5e 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78   a record from x
35b5f 78 78 5f 6e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  xx_node */.  sql
35b60 69 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64  ite3_stmt *pRead
35b61 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Node;.  sqlite3_
35b62 73 74 6d 74 20 2a 70 57 72 69 74 65 4e 6f 64 65  stmt *pWriteNode
35b63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
35b64 20 2a 70 44 65 6c 65 74 65 4e 6f 64 65 3b 0a 0a   *pDeleteNode;..
35b65 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73 20    /* Statements 
35b66 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64 65  to read/write/de
35b67 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66 72  lete a record fr
35b68 6f 6d 20 78 78 78 5f 72 6f 77 69 64 20 2a 2f 0a  om xxx_rowid */.
35b69 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
35b6a 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20 73 71  pReadRowid;.  sq
35b6b 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69  lite3_stmt *pWri
35b6c 74 65 52 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74  teRowid;.  sqlit
35b6d 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65  e3_stmt *pDelete
35b6e 52 6f 77 69 64 3b 0a 0a 20 20 2f 2a 20 53 74 61  Rowid;..  /* Sta
35b6f 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f  tements to read/
35b70 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72  write/delete a r
35b71 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 70  ecord from xxx_p
35b72 61 72 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74  arent */.  sqlit
35b73 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 50 61  e3_stmt *pReadPa
35b74 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f  rent;.  sqlite3_
35b75 73 74 6d 74 20 2a 70 57 72 69 74 65 50 61 72 65  stmt *pWritePare
35b76 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  nt;.  sqlite3_st
35b77 6d 74 20 2a 70 44 65 6c 65 74 65 50 61 72 65 6e  mt *pDeleteParen
35b78 74 3b 0a 0a 20 20 69 6e 74 20 65 43 6f 6f 72 64  t;..  int eCoord
35b79 54 79 70 65 3b 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73  Type;.};../* Pos
35b7a 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72  sible values for
35b7b 20 65 43 6f 6f 72 64 54 79 70 65 3a 20 2a 2f 0a   eCoordType: */.
35b7c 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 43 4f  #define RTREE_CO
35b7d 4f 52 44 5f 52 45 41 4c 33 32 20 30 0a 23 64 65  ORD_REAL32 0.#de
35b7e 66 69 6e 65 20 52 54 52 45 45 5f 43 4f 4f 52 44  fine RTREE_COORD
35b7f 5f 49 4e 54 33 32 20 20 31 0a 0a 2f 2a 0a 2a 2a  _INT32  1../*.**
35b80 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 6e 75 6d   The minimum num
35b81 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 61 6c 6c  ber of cells all
35b82 6f 77 65 64 20 66 6f 72 20 61 20 6e 6f 64 65 20  owed for a node 
35b83 69 73 20 61 20 74 68 69 72 64 20 6f 66 20 74 68  is a third of th
35b84 65 20 0a 2a 2a 20 6d 61 78 69 6d 75 6d 2e 20 49  e .** maximum. I
35b85 6e 20 47 75 74 6d 61 6e 27 73 20 6e 6f 74 61 74  n Gutman's notat
35b86 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6d  ion:.**.**     m
35b87 20 3d 20 4d 2f 33 0a 2a 2a 0a 2a 2a 20 49 66 20   = M/3.**.** If 
35b88 61 6e 20 52 2a 2d 74 72 65 65 20 22 52 65 69 6e  an R*-tree "Rein
35b89 73 65 72 74 22 20 6f 70 65 72 61 74 69 6f 6e 20  sert" operation 
35b8a 69 73 20 72 65 71 75 69 72 65 64 2c 20 74 68 65  is required, the
35b8b 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 0a   same number of.
35b8c 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 72 65 6d  ** cells are rem
35b8d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 76  oved from the ov
35b8e 65 72 66 75 6c 6c 20 6e 6f 64 65 20 61 6e 64 20  erfull node and 
35b8f 72 65 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  reinserted into 
35b90 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 23 64 65  the tree..*/.#de
35b91 66 69 6e 65 20 52 54 52 45 45 5f 4d 49 4e 43 45  fine RTREE_MINCE
35b92 4c 4c 53 28 70 29 20 28 28 28 28 70 29 2d 3e 69  LLS(p) ((((p)->i
35b93 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 28 70 29 2d  NodeSize-4)/(p)-
35b94 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 29 2f  >nBytesPerCell)/
35b95 33 29 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  3).#define RTREE
35b96 5f 52 45 49 4e 53 45 52 54 28 70 29 20 52 54 52  _REINSERT(p) RTR
35b97 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 29 0a 23  EE_MINCELLS(p).#
35b98 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4d 41 58  define RTREE_MAX
35b99 43 45 4c 4c 53 20 35 31 0a 0a 2f 2a 20 0a 2a 2a  CELLS 51../* .**
35b9a 20 41 6e 20 72 74 72 65 65 20 63 75 72 73 6f 72   An rtree cursor
35b9b 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75   object..*/.stru
35b9c 63 74 20 52 74 72 65 65 43 75 72 73 6f 72 20 7b  ct RtreeCursor {
35b9d 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
35b9e 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 52  cursor base;.  R
35b9f 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b  treeNode *pNode;
35ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ba1 20 2f 2a 20 4e 6f 64 65 20 63 75 72 73 6f 72 20   /* Node cursor 
35ba2 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
35ba3 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 69 6e  nting at */.  in
35ba4 74 20 69 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  t iCell;        
35ba5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ba6 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72  /* Index of curr
35ba7 65 6e 74 20 63 65 6c 6c 20 69 6e 20 70 4e 6f 64  ent cell in pNod
35ba8 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 72 61  e */.  int iStra
35ba9 74 65 67 79 3b 20 20 20 20 20 20 20 20 20 20 20  tegy;           
35baa 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
35bab 20 6f 66 20 69 64 78 4e 75 6d 20 73 65 61 72 63   of idxNum searc
35bac 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  h parameter */. 
35bad 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74   int nConstraint
35bae 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35baf 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
35bb0 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73  entries in aCons
35bb1 74 72 61 69 6e 74 20 2a 2f 0a 20 20 52 74 72 65  traint */.  Rtre
35bb2 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 61 43 6f  eConstraint *aCo
35bb3 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 2f 2a  nstraint;     /*
35bb4 20 53 65 61 72 63 68 20 63 6f 6e 73 74 72 61 69   Search constrai
35bb5 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a 0a 75 6e 69 6f  nts. */.};..unio
35bb6 6e 20 52 74 72 65 65 43 6f 6f 72 64 20 7b 0a 20  n RtreeCoord {. 
35bb7 20 66 6c 6f 61 74 20 66 3b 0a 20 20 69 6e 74 20   float f;.  int 
35bb8 69 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  i;.};../*.** The
35bb9 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
35bba 52 74 72 65 65 43 6f 6f 72 64 2e 20 52 65 74 75  RtreeCoord. Retu
35bbb 72 6e 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  rn the value sto
35bbc 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52  red within the R
35bbd 74 72 65 65 43 6f 6f 72 64 0a 2a 2a 20 66 6f 72  treeCoord.** for
35bbe 6d 61 74 74 65 64 20 61 73 20 61 20 64 6f 75 62  matted as a doub
35bbf 6c 65 2e 20 54 68 69 73 20 6d 61 63 72 6f 20 61  le. This macro a
35bc0 73 73 75 6d 65 73 20 74 68 61 74 20 6c 6f 63 61  ssumes that loca
35bc1 6c 20 76 61 72 69 61 62 6c 65 20 70 52 74 72 65  l variable pRtre
35bc2 65 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74  e points.** to t
35bc3 68 65 20 52 74 72 65 65 20 73 74 72 75 63 74 75  he Rtree structu
35bc4 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  re associated wi
35bc5 74 68 20 74 68 65 20 52 74 72 65 65 43 6f 6f 72  th the RtreeCoor
35bc6 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 43  d..*/.#define DC
35bc7 4f 4f 52 44 28 63 6f 6f 72 64 29 20 28 20 20 20  OORD(coord) (   
35bc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bc9 20 20 20 20 20 20 20 20 5c 0a 20 20 28 70 52 74          \.  (pRt
35bca 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
35bcb 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41  =RTREE_COORD_REA
35bcc 4c 33 32 29 20 3f 20 20 20 20 20 20 5c 0a 20 20  L32) ?      \.  
35bcd 20 20 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64    ((double)coord
35bce 2e 66 29 20 3a 20 20 20 20 20 20 20 20 20 20 20  .f) :           
35bcf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bd0 5c 0a 20 20 20 20 28 28 64 6f 75 62 6c 65 29 63  \.    ((double)c
35bd1 6f 6f 72 64 2e 69 29 20 20 20 20 20 20 20 20 20  oord.i)         
35bd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bd3 20 20 20 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a 20 41      \.)../*.** A
35bd4 20 73 65 61 72 63 68 20 63 6f 6e 73 74 72 61 69   search constrai
35bd5 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  nt..*/.struct Rt
35bd6 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 7b 0a  reeConstraint {.
35bd7 20 20 69 6e 74 20 69 43 6f 6f 72 64 3b 20 20 20    int iCoord;   
35bd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bd9 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
35bda 63 6f 6e 73 74 72 61 69 6e 65 64 20 63 6f 6f 72  constrained coor
35bdb 64 69 6e 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20  dinate */.  int 
35bdc 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
35bdd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35bde 20 43 6f 6e 73 74 72 61 69 6e 69 6e 67 20 6f 70   Constraining op
35bdf 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 64 6f 75  eration */.  dou
35be0 62 6c 65 20 72 56 61 6c 75 65 3b 20 20 20 20 20  ble rValue;     
35be1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35be2 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c  * Constraint val
35be3 75 65 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 50 6f  ue. */.};../* Po
35be4 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
35be5 72 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e  r RtreeConstrain
35be6 74 2e 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20  t.op */.#define 
35be7 52 54 52 45 45 5f 45 51 20 30 78 34 31 0a 23 64  RTREE_EQ 0x41.#d
35be8 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 45 20 30  efine RTREE_LE 0
35be9 78 34 32 0a 23 64 65 66 69 6e 65 20 52 54 52 45  x42.#define RTRE
35bea 45 5f 4c 54 20 30 78 34 33 0a 23 64 65 66 69 6e  E_LT 0x43.#defin
35beb 65 20 52 54 52 45 45 5f 47 45 20 30 78 34 34 0a  e RTREE_GE 0x44.
35bec 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 47 54  #define RTREE_GT
35bed 20 30 78 34 35 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e   0x45../* .** An
35bee 20 72 74 72 65 65 20 73 74 72 75 63 74 75 72 65   rtree structure
35bef 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 44 61 74   node..**.** Dat
35bf0 61 20 66 6f 72 6d 61 74 20 28 52 74 72 65 65 4e  a format (RtreeN
35bf1 6f 64 65 2e 7a 44 61 74 61 29 3a 0a 2a 2a 0a 2a  ode.zData):.**.*
35bf2 2a 20 20 20 31 2e 20 49 66 20 74 68 65 20 6e 6f  *   1. If the no
35bf3 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6e  de is the root n
35bf4 6f 64 65 20 28 6e 6f 64 65 20 31 29 2c 20 74 68  ode (node 1), th
35bf5 65 6e 20 74 68 65 20 66 69 72 73 74 20 32 20 62  en the first 2 b
35bf6 79 74 65 73 0a 2a 2a 20 20 20 20 20 20 6f 66 20  ytes.**      of 
35bf7 74 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e  the node contain
35bf8 20 74 68 65 20 74 72 65 65 20 64 65 70 74 68 20   the tree depth 
35bf9 61 73 20 61 20 62 69 67 2d 65 6e 64 69 61 6e 20  as a big-endian 
35bfa 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 20 20 20 20  integer..**     
35bfb 20 46 6f 72 20 6e 6f 6e 2d 72 6f 6f 74 20 6e 6f   For non-root no
35bfc 64 65 73 2c 20 74 68 65 20 66 69 72 73 74 20 32  des, the first 2
35bfd 20 62 79 74 65 73 20 61 72 65 20 6c 65 66 74 20   bytes are left 
35bfe 75 6e 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  unused..**.**   
35bff 32 2e 20 54 68 65 20 6e 65 78 74 20 32 20 62 79  2. The next 2 by
35c00 74 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  tes contain the 
35c01 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
35c02 73 20 63 75 72 72 65 6e 74 6c 79 20 0a 2a 2a 20  s currently .** 
35c03 20 20 20 20 20 73 74 6f 72 65 64 20 69 6e 20 74       stored in t
35c04 68 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 20  he node..**.**  
35c05 20 33 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65   3. The remainde
35c06 72 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 63 6f  r of the node co
35c07 6e 74 61 69 6e 73 20 74 68 65 20 6e 6f 64 65 20  ntains the node 
35c08 65 6e 74 72 69 65 73 2e 20 45 61 63 68 20 65 6e  entries. Each en
35c09 74 72 79 0a 2a 2a 20 20 20 20 20 20 63 6f 6e 73  try.**      cons
35c0a 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65  ists of a single
35c0b 20 38 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20   8-byte integer 
35c0c 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 20 65  followed by an e
35c0d 76 65 6e 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20  ven number.**   
35c0e 20 20 20 6f 66 20 34 2d 62 79 74 65 20 63 6f 6f     of 4-byte coo
35c0f 72 64 69 6e 61 74 65 73 2e 20 46 6f 72 20 6c 65  rdinates. For le
35c10 61 66 20 6e 6f 64 65 73 20 74 68 65 20 69 6e 74  af nodes the int
35c11 65 67 65 72 20 69 73 20 74 68 65 20 72 6f 77 69  eger is the rowi
35c12 64 0a 2a 2a 20 20 20 20 20 20 6f 66 20 61 20 72  d.**      of a r
35c13 65 63 6f 72 64 2e 20 46 6f 72 20 69 6e 74 65 72  ecord. For inter
35c14 6e 61 6c 20 6e 6f 64 65 73 20 69 74 20 69 73 20  nal nodes it is 
35c15 74 68 65 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20  the node number 
35c16 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20 63 68 69  of a.**      chi
35c17 6c 64 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 72 75  ld page..*/.stru
35c18 63 74 20 52 74 72 65 65 4e 6f 64 65 20 7b 0a 20  ct RtreeNode {. 
35c19 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
35c1a 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ent;            
35c1b 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6e 6f 64     /* Parent nod
35c1c 65 20 2a 2f 0a 20 20 69 36 34 20 69 4e 6f 64 65  e */.  i64 iNode
35c1d 3b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20  ;.  int nRef;.  
35c1e 69 6e 74 20 69 73 44 69 72 74 79 3b 0a 20 20 75  int isDirty;.  u
35c1f 38 20 2a 7a 44 61 74 61 3b 0a 20 20 52 74 72 65  8 *zData;.  Rtre
35c20 65 4e 6f 64 65 20 2a 70 4e 65 78 74 3b 20 20 20  eNode *pNext;   
35c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35c22 20 4e 65 78 74 20 6e 6f 64 65 20 69 6e 20 74 68   Next node in th
35c23 69 73 20 68 61 73 68 20 63 68 61 69 6e 20 2a 2f  is hash chain */
35c24 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 4e 43 45 4c  .};.#define NCEL
35c25 4c 28 70 4e 6f 64 65 29 20 72 65 61 64 49 6e 74  L(pNode) readInt
35c26 31 36 28 26 28 70 4e 6f 64 65 29 2d 3e 7a 44 61  16(&(pNode)->zDa
35c27 74 61 5b 32 5d 29 0a 0a 2f 2a 20 0a 2a 2a 20 53  ta[2])../* .** S
35c28 74 72 75 63 74 75 72 65 20 74 6f 20 73 74 6f 72  tructure to stor
35c29 65 20 61 20 64 65 73 65 72 69 61 6c 69 7a 65 64  e a deserialized
35c2a 20 72 74 72 65 65 20 72 65 63 6f 72 64 2e 0a 2a   rtree record..*
35c2b 2f 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 65  /.struct RtreeCe
35c2c 6c 6c 20 7b 0a 20 20 69 36 34 20 69 52 6f 77 69  ll {.  i64 iRowi
35c2d 64 3b 0a 20 20 52 74 72 65 65 43 6f 6f 72 64 20  d;.  RtreeCoord 
35c2e 61 43 6f 6f 72 64 5b 52 54 52 45 45 5f 4d 41 58  aCoord[RTREE_MAX
35c2f 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 32 5d 3b 0a  _DIMENSIONS*2];.
35c30 7d 3b 0a 0a 23 69 66 6e 64 65 66 20 4d 41 58 0a  };..#ifndef MAX.
35c31 23 20 64 65 66 69 6e 65 20 4d 41 58 28 78 2c 79  # define MAX(x,y
35c32 29 20 28 28 78 29 20 3c 20 28 79 29 20 3f 20 28  ) ((x) < (y) ? (
35c33 79 29 20 3a 20 28 78 29 29 0a 23 65 6e 64 69 66  y) : (x)).#endif
35c34 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64  .#ifndef MIN.# d
35c35 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28  efine MIN(x,y) (
35c36 28 78 29 20 3e 20 28 79 29 20 3f 20 28 79 29 20  (x) > (y) ? (y) 
35c37 3a 20 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f  : (x)).#endif../
35c38 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74  *.** Functions t
35c39 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 61 20  o deserialize a 
35c3a 31 36 20 62 69 74 20 69 6e 74 65 67 65 72 2c 20  16 bit integer, 
35c3b 33 32 20 62 69 74 20 72 65 61 6c 20 6e 75 6d 62  32 bit real numb
35c3c 65 72 20 61 6e 64 0a 2a 2a 20 36 34 20 62 69 74  er and.** 64 bit
35c3d 20 69 6e 74 65 67 65 72 2e 20 54 68 65 20 64 65   integer. The de
35c3e 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65  serialized value
35c3f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
35c40 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
35c41 49 6e 74 31 36 28 75 38 20 2a 70 29 7b 0a 20 20  Int16(u8 *p){.  
35c42 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 38 29  return (p[0]<<8)
35c43 20 2b 20 70 5b 31 5d 3b 0a 7d 0a 73 74 61 74 69   + p[1];.}.stati
35c44 63 20 76 6f 69 64 20 72 65 61 64 43 6f 6f 72 64  c void readCoord
35c45 28 75 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f  (u8 *p, RtreeCoo
35c46 72 64 20 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75  rd *pCoord){.  u
35c47 33 32 20 69 20 3d 20 28 0a 20 20 20 20 28 28 28  32 i = (.    (((
35c48 75 33 32 29 70 5b 30 5d 29 20 3c 3c 20 32 34 29  u32)p[0]) << 24)
35c49 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
35c4a 5b 31 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20  [1]) << 16) + . 
35c4b 20 20 20 28 28 28 75 33 32 29 70 5b 32 5d 29 20     (((u32)p[2]) 
35c4c 3c 3c 20 20 38 29 20 2b 20 0a 20 20 20 20 28 28  <<  8) + .    ((
35c4d 28 75 33 32 29 70 5b 33 5d 29 20 3c 3c 20 20 30  (u32)p[3]) <<  0
35c4e 29 0a 20 20 29 3b 0a 20 20 2a 28 75 33 32 20 2a  ).  );.  *(u32 *
35c4f 29 70 43 6f 6f 72 64 20 3d 20 69 3b 0a 7d 0a 73  )pCoord = i;.}.s
35c50 74 61 74 69 63 20 69 36 34 20 72 65 61 64 49 6e  tatic i64 readIn
35c51 74 36 34 28 75 38 20 2a 70 29 7b 0a 20 20 72 65  t64(u8 *p){.  re
35c52 74 75 72 6e 20 28 0a 20 20 20 20 28 28 28 69 36  turn (.    (((i6
35c53 34 29 70 5b 30 5d 29 20 3c 3c 20 35 36 29 20 2b  4)p[0]) << 56) +
35c54 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 31   .    (((i64)p[1
35c55 5d 29 20 3c 3c 20 34 38 29 20 2b 20 0a 20 20 20  ]) << 48) + .   
35c56 20 28 28 28 69 36 34 29 70 5b 32 5d 29 20 3c 3c   (((i64)p[2]) <<
35c57 20 34 30 29 20 2b 20 0a 20 20 20 20 28 28 28 69   40) + .    (((i
35c58 36 34 29 70 5b 33 5d 29 20 3c 3c 20 33 32 29 20  64)p[3]) << 32) 
35c59 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
35c5a 34 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20  4]) << 24) + .  
35c5b 20 20 28 28 28 69 36 34 29 70 5b 35 5d 29 20 3c    (((i64)p[5]) <
35c5c 3c 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28  < 16) + .    (((
35c5d 69 36 34 29 70 5b 36 5d 29 20 3c 3c 20 20 38 29  i64)p[6]) <<  8)
35c5e 20 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70   + .    (((i64)p
35c5f 5b 37 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b  [7]) <<  0).  );
35c60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  .}../*.** Functi
35c61 6f 6e 73 20 74 6f 20 73 65 72 69 61 6c 69 7a 65  ons to serialize
35c62 20 61 20 31 36 20 62 69 74 20 69 6e 74 65 67 65   a 16 bit intege
35c63 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c 20 6e  r, 32 bit real n
35c64 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36 34 20  umber and.** 64 
35c65 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54 68 65  bit integer. The
35c66 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
35c67 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
35c68 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 0a 2a   bytes written.*
35c69 2a 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e  * to the argumen
35c6a 74 20 62 75 66 66 65 72 20 28 61 6c 77 61 79 73  t buffer (always
35c6b 20 32 2c 20 34 20 61 6e 64 20 38 20 72 65 73 70   2, 4 and 8 resp
35c6c 65 63 74 69 76 65 6c 79 29 2e 0a 2a 2f 0a 73 74  ectively)..*/.st
35c6d 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e  atic int writeIn
35c6e 74 31 36 28 75 38 20 2a 70 2c 20 69 6e 74 20 69  t16(u8 *p, int i
35c6f 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e  ){.  p[0] = (i>>
35c70 20 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d   8)&0xFF;.  p[1]
35c71 20 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b   = (i>> 0)&0xFF;
35c72 0a 20 20 72 65 74 75 72 6e 20 32 3b 0a 7d 0a 73  .  return 2;.}.s
35c73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 43  tatic int writeC
35c74 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72 65  oord(u8 *p, Rtre
35c75 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29 7b  eCoord *pCoord){
35c76 0a 20 20 75 33 32 20 69 3b 0a 20 20 61 73 73 65  .  u32 i;.  asse
35c77 72 74 28 20 73 69 7a 65 6f 66 28 52 74 72 65 65  rt( sizeof(Rtree
35c78 43 6f 6f 72 64 29 3d 3d 34 20 29 3b 0a 20 20 61  Coord)==4 );.  a
35c79 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33  ssert( sizeof(u3
35c7a 32 29 3d 3d 34 20 29 3b 0a 20 20 69 20 3d 20 2a  2)==4 );.  i = *
35c7b 28 75 33 32 20 2a 29 70 43 6f 6f 72 64 3b 0a 20  (u32 *)pCoord;. 
35c7c 20 70 5b 30 5d 20 3d 20 28 69 3e 3e 32 34 29 26   p[0] = (i>>24)&
35c7d 30 78 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28  0xFF;.  p[1] = (
35c7e 69 3e 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70  i>>16)&0xFF;.  p
35c7f 5b 32 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [2] = (i>> 8)&0x
35c80 46 46 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 69 3e  FF;.  p[3] = (i>
35c81 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
35c82 75 72 6e 20 34 3b 0a 7d 0a 73 74 61 74 69 63 20  urn 4;.}.static 
35c83 69 6e 74 20 77 72 69 74 65 49 6e 74 36 34 28 75  int writeInt64(u
35c84 38 20 2a 70 2c 20 69 36 34 20 69 29 7b 0a 20 20  8 *p, i64 i){.  
35c85 70 5b 30 5d 20 3d 20 28 69 3e 3e 35 36 29 26 30  p[0] = (i>>56)&0
35c86 78 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69  xFF;.  p[1] = (i
35c87 3e 3e 34 38 29 26 30 78 46 46 3b 0a 20 20 70 5b  >>48)&0xFF;.  p[
35c88 32 5d 20 3d 20 28 69 3e 3e 34 30 29 26 30 78 46  2] = (i>>40)&0xF
35c89 46 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 69 3e 3e  F;.  p[3] = (i>>
35c8a 33 32 29 26 30 78 46 46 3b 0a 20 20 70 5b 34 5d  32)&0xFF;.  p[4]
35c8b 20 3d 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b   = (i>>24)&0xFF;
35c8c 0a 20 20 70 5b 35 5d 20 3d 20 28 69 3e 3e 31 36  .  p[5] = (i>>16
35c8d 29 26 30 78 46 46 3b 0a 20 20 70 5b 36 5d 20 3d  )&0xFF;.  p[6] =
35c8e 20 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20   (i>> 8)&0xFF;. 
35c8f 20 70 5b 37 5d 20 3d 20 28 69 3e 3e 20 30 29 26   p[7] = (i>> 0)&
35c90 30 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 38  0xFF;.  return 8
35c91 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65  ;.}../*.** Incre
35c92 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e  ment the referen
35c93 63 65 20 63 6f 75 6e 74 20 6f 66 20 6e 6f 64 65  ce count of node
35c94 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f   p..*/.static vo
35c95 69 64 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65  id nodeReference
35c96 28 52 74 72 65 65 4e 6f 64 65 20 2a 70 29 7b 0a  (RtreeNode *p){.
35c97 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70    if( p ){.    p
35c98 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 7d 0a  ->nRef++;.  }.}.
35c99 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
35c9a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6e 6f 64 65   content of node
35c9b 20 70 20 28 73 65 74 20 61 6c 6c 20 62 79 74 65   p (set all byte
35c9c 73 20 74 6f 20 30 78 30 30 29 2e 0a 2a 2f 0a 73  s to 0x00)..*/.s
35c9d 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 5a  tatic void nodeZ
35c9e 65 72 6f 28 52 74 72 65 65 20 2a 70 52 74 72 65  ero(Rtree *pRtre
35c9f 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 29  e, RtreeNode *p)
35ca0 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
35ca1 20 6d 65 6d 73 65 74 28 26 70 2d 3e 7a 44 61 74   memset(&p->zDat
35ca2 61 5b 32 5d 2c 20 30 2c 20 70 52 74 72 65 65 2d  a[2], 0, pRtree-
35ca3 3e 69 4e 6f 64 65 53 69 7a 65 2d 32 29 3b 0a 20  >iNodeSize-2);. 
35ca4 20 20 20 70 2d 3e 69 73 44 69 72 74 79 20 3d 20     p->isDirty = 
35ca5 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
35ca6 47 69 76 65 6e 20 61 20 6e 6f 64 65 20 6e 75 6d  Given a node num
35ca7 62 65 72 20 69 4e 6f 64 65 2c 20 72 65 74 75 72  ber iNode, retur
35ca8 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
35ca9 69 6e 67 20 6b 65 79 20 74 6f 20 75 73 65 0a 2a  ing key to use.*
35caa 2a 20 69 6e 20 74 68 65 20 52 74 72 65 65 2e 61  * in the Rtree.a
35cab 48 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  Hash table..*/.s
35cac 74 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 48 61  tatic int nodeHa
35cad 73 68 28 69 36 34 20 69 4e 6f 64 65 29 7b 0a 20  sh(i64 iNode){. 
35cae 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20 28 69   return (.    (i
35caf 4e 6f 64 65 3e 3e 35 36 29 20 5e 20 28 69 4e 6f  Node>>56) ^ (iNo
35cb0 64 65 3e 3e 34 38 29 20 5e 20 28 69 4e 6f 64 65  de>>48) ^ (iNode
35cb1 3e 3e 34 30 29 20 5e 20 28 69 4e 6f 64 65 3e 3e  >>40) ^ (iNode>>
35cb2 33 32 29 20 5e 20 0a 20 20 20 20 28 69 4e 6f 64  32) ^ .    (iNod
35cb3 65 3e 3e 32 34 29 20 5e 20 28 69 4e 6f 64 65 3e  e>>24) ^ (iNode>
35cb4 3e 31 36 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 20  >16) ^ (iNode>> 
35cb5 38 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 20 30 29  8) ^ (iNode>> 0)
35cb6 0a 20 20 29 20 25 20 48 41 53 48 53 49 5a 45 3b  .  ) % HASHSIZE;
35cb7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68  .}../*.** Search
35cb8 20 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74   the node hash t
35cb9 61 62 6c 65 20 66 6f 72 20 6e 6f 64 65 20 69 4e  able for node iN
35cba 6f 64 65 2e 20 49 66 20 66 6f 75 6e 64 2c 20 72  ode. If found, r
35cbb 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
35cbc 2a 2a 20 74 6f 20 69 74 2e 20 4f 74 68 65 72 77  ** to it. Otherw
35cbd 69 73 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a  ise, return 0..*
35cbe 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f  /.static RtreeNo
35cbf 64 65 20 2a 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  de *nodeHashLook
35cc0 75 70 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  up(Rtree *pRtree
35cc1 2c 20 69 36 34 20 69 4e 6f 64 65 29 7b 0a 20 20  , i64 iNode){.  
35cc2 52 74 72 65 65 4e 6f 64 65 20 2a 70 3b 0a 20 20  RtreeNode *p;.  
35cc3 61 73 73 65 72 74 28 20 69 4e 6f 64 65 21 3d 30  assert( iNode!=0
35cc4 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 52 74 72   );.  for(p=pRtr
35cc5 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61  ee->aHash[nodeHa
35cc6 73 68 28 69 4e 6f 64 65 29 5d 3b 20 70 20 26 26  sh(iNode)]; p &&
35cc7 20 70 2d 3e 69 4e 6f 64 65 21 3d 69 4e 6f 64 65   p->iNode!=iNode
35cc8 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 3b 0a 20  ; p=p->pNext);. 
35cc9 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
35cca 0a 2a 2a 20 41 64 64 20 6e 6f 64 65 20 70 4e 6f  .** Add node pNo
35ccb 64 65 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 68  de to the node h
35ccc 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ash table..*/.st
35ccd 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 48 61  atic void nodeHa
35cce 73 68 49 6e 73 65 72 74 28 52 74 72 65 65 20 2a  shInsert(Rtree *
35ccf 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
35cd0 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 66 28  e *pNode){.  if(
35cd1 20 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e   pNode ){.    in
35cd2 74 20 69 48 61 73 68 3b 0a 20 20 20 20 61 73 73  t iHash;.    ass
35cd3 65 72 74 28 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  ert( pNode->pNex
35cd4 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69 48 61 73  t==0 );.    iHas
35cd5 68 20 3d 20 6e 6f 64 65 48 61 73 68 28 70 4e 6f  h = nodeHash(pNo
35cd6 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
35cd7 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70  pNode->pNext = p
35cd8 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61  Rtree->aHash[iHa
35cd9 73 68 5d 3b 0a 20 20 20 20 70 52 74 72 65 65 2d  sh];.    pRtree-
35cda 3e 61 48 61 73 68 5b 69 48 61 73 68 5d 20 3d 20  >aHash[iHash] = 
35cdb 70 4e 6f 64 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  pNode;.  }.}../*
35cdc 0a 2a 2a 20 52 65 6d 6f 76 65 20 6e 6f 64 65 20  .** Remove node 
35cdd 70 4e 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 6e  pNode from the n
35cde 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ode hash table..
35cdf 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
35ce0 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28 52 74  odeHashDelete(Rt
35ce1 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
35ce2 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
35ce3 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70 70    RtreeNode **pp
35ce4 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69  ;.  if( pNode->i
35ce5 4e 6f 64 65 21 3d 30 20 29 7b 0a 20 20 20 20 70  Node!=0 ){.    p
35ce6 70 20 3d 20 26 70 52 74 72 65 65 2d 3e 61 48 61  p = &pRtree->aHa
35ce7 73 68 5b 6e 6f 64 65 48 61 73 68 28 70 4e 6f 64  sh[nodeHash(pNod
35ce8 65 2d 3e 69 4e 6f 64 65 29 5d 3b 0a 20 20 20 20  e->iNode)];.    
35ce9 66 6f 72 28 20 3b 20 28 2a 70 70 29 21 3d 70 4e  for( ; (*pp)!=pN
35cea 6f 64 65 3b 20 70 70 20 3d 20 26 28 2a 70 70 29  ode; pp = &(*pp)
35ceb 2d 3e 70 4e 65 78 74 29 7b 20 61 73 73 65 72 74  ->pNext){ assert
35cec 28 2a 70 70 29 3b 20 7d 0a 20 20 20 20 2a 70 70  (*pp); }.    *pp
35ced 20 3d 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 3b   = pNode->pNext;
35cee 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  .    pNode->pNex
35cef 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  t = 0;.  }.}../*
35cf0 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64  .** Allocate and
35cf1 20 72 65 74 75 72 6e 20 6e 65 77 20 72 2d 74 72   return new r-tr
35cf2 65 65 20 6e 6f 64 65 2e 20 49 6e 69 74 69 61 6c  ee node. Initial
35cf3 6c 79 2c 20 28 52 74 72 65 65 4e 6f 64 65 2e 69  ly, (RtreeNode.i
35cf4 4e 6f 64 65 3d 3d 30 29 2c 0a 2a 2a 20 69 6e 64  Node==0),.** ind
35cf5 69 63 61 74 69 6e 67 20 74 68 61 74 20 6e 6f 64  icating that nod
35cf6 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  e has not yet be
35cf7 65 6e 20 61 73 73 69 67 6e 65 64 20 61 20 6e 6f  en assigned a no
35cf8 64 65 20 6e 75 6d 62 65 72 2e 20 49 74 20 69 73  de number. It is
35cf9 0a 2a 2a 20 61 73 73 69 67 6e 65 64 20 61 20 6e  .** assigned a n
35cfa 6f 64 65 20 6e 75 6d 62 65 72 20 77 68 65 6e 20  ode number when 
35cfb 6e 6f 64 65 57 72 69 74 65 28 29 20 69 73 20 63  nodeWrite() is c
35cfc 61 6c 6c 65 64 20 74 6f 20 77 72 69 74 65 20 74  alled to write t
35cfd 68 65 0a 2a 2a 20 6e 6f 64 65 20 63 6f 6e 74 65  he.** node conte
35cfe 6e 74 73 20 6f 75 74 20 74 6f 20 74 68 65 20 64  nts out to the d
35cff 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74  atabase..*/.stat
35d00 69 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f  ic RtreeNode *no
35d01 64 65 4e 65 77 28 52 74 72 65 65 20 2a 70 52 74  deNew(Rtree *pRt
35d02 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a  ree, RtreeNode *
35d03 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 7a 65 72  pParent, int zer
35d04 6f 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  o){.  RtreeNode 
35d05 2a 70 4e 6f 64 65 3b 0a 20 20 70 4e 6f 64 65 20  *pNode;.  pNode 
35d06 3d 20 28 52 74 72 65 65 4e 6f 64 65 20 2a 29 73  = (RtreeNode *)s
35d07 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
35d08 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 20  zeof(RtreeNode) 
35d09 2b 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  + pRtree->iNodeS
35d0a 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 4e 6f 64  ize);.  if( pNod
35d0b 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  e ){.    memset(
35d0c 70 4e 6f 64 65 2c 20 30 2c 20 73 69 7a 65 6f 66  pNode, 0, sizeof
35d0d 28 52 74 72 65 65 4e 6f 64 65 29 20 2b 20 28 7a  (RtreeNode) + (z
35d0e 65 72 6f 3f 70 52 74 72 65 65 2d 3e 69 4e 6f 64  ero?pRtree->iNod
35d0f 65 53 69 7a 65 3a 30 29 29 3b 0a 20 20 20 20 70  eSize:0));.    p
35d10 4e 6f 64 65 2d 3e 7a 44 61 74 61 20 3d 20 28 75  Node->zData = (u
35d11 38 20 2a 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a 20  8 *)&pNode[1];. 
35d12 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 20 3d     pNode->nRef =
35d13 20 31 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70   1;.    pNode->p
35d14 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74  Parent = pParent
35d15 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44  ;.    pNode->isD
35d16 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 6e 6f  irty = 1;.    no
35d17 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61 72  deReference(pPar
35d18 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ent);.  }.  retu
35d19 72 6e 20 70 4e 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a  rn pNode;.}../*.
35d1a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 72 65 66 65  ** Obtain a refe
35d1b 72 65 6e 63 65 20 74 6f 20 61 6e 20 72 2d 74 72  rence to an r-tr
35d1c 65 65 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  ee node..*/.stat
35d1d 69 63 20 69 6e 74 0a 6e 6f 64 65 41 63 71 75 69  ic int.nodeAcqui
35d1e 72 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  re(.  Rtree *pRt
35d1f 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ree,            
35d20 20 2f 2a 20 52 2d 74 72 65 65 20 73 74 72 75 63   /* R-tree struc
35d21 74 75 72 65 20 2a 2f 0a 20 20 69 36 34 20 69 4e  ture */.  i64 iN
35d22 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ode,            
35d23 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6e 75 6d       /* Node num
35d24 62 65 72 20 74 6f 20 6c 6f 61 64 20 2a 2f 0a 20  ber to load */. 
35d25 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
35d26 65 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 45  ent,        /* E
35d27 69 74 68 65 72 20 74 68 65 20 70 61 72 65 6e 74  ither the parent
35d28 20 6e 6f 64 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f   node or NULL */
35d29 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  .  RtreeNode **p
35d2a 70 4e 6f 64 65 20 20 20 20 20 20 20 20 20 2f 2a  pNode         /*
35d2b 20 4f 55 54 3a 20 41 63 71 75 69 72 65 64 20 6e   OUT: Acquired n
35d2c 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ode */.){.  int 
35d2d 72 63 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  rc;.  RtreeNode 
35d2e 2a 70 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20 43 68  *pNode;..  /* Ch
35d2f 65 63 6b 20 69 66 20 74 68 65 20 72 65 71 75 65  eck if the reque
35d30 73 74 65 64 20 6e 6f 64 65 20 69 73 20 61 6c 72  sted node is alr
35d31 65 61 64 79 20 69 6e 20 74 68 65 20 68 61 73 68  eady in the hash
35d32 20 74 61 62 6c 65 2e 20 49 66 20 73 6f 2c 0a 20   table. If so,. 
35d33 20 2a 2a 20 69 6e 63 72 65 61 73 65 20 69 74 73   ** increase its
35d34 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
35d35 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 2e 0a   and return it..
35d36 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 4e 6f 64    */.  if( (pNod
35d37 65 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  e = nodeHashLook
35d38 75 70 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65  up(pRtree, iNode
35d39 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  )) ){.    assert
35d3a 28 20 21 70 50 61 72 65 6e 74 20 7c 7c 20 21 70  ( !pParent || !p
35d3b 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 7c 7c  Node->pParent ||
35d3c 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3d   pNode->pParent=
35d3d 3d 70 50 61 72 65 6e 74 20 29 3b 0a 20 20 20 20  =pParent );.    
35d3e 69 66 28 20 70 50 61 72 65 6e 74 20 26 26 20 21  if( pParent && !
35d3f 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 29  pNode->pParent )
35d40 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65  {.      nodeRefe
35d41 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
35d42 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e 70 50 61        pNode->pPa
35d43 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74 3b 0a  rent = pParent;.
35d44 20 20 20 20 7d 0a 20 20 20 20 70 4e 6f 64 65 2d      }.    pNode-
35d45 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 2a 70 70  >nRef++;.    *pp
35d46 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  Node = pNode;.  
35d47 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
35d48 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4e 6f 64 65  OK;.  }..  pNode
35d49 20 3d 20 28 52 74 72 65 65 4e 6f 64 65 20 2a 29   = (RtreeNode *)
35d4a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
35d4b 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29  izeof(RtreeNode)
35d4c 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65   + pRtree->iNode
35d4d 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 70 4e  Size);.  if( !pN
35d4e 6f 64 65 20 29 7b 0a 20 20 20 20 2a 70 70 4e 6f  ode ){.    *ppNo
35d4f 64 65 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  de = 0;.    retu
35d50 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
35d51 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 70 50  .  }.  pNode->pP
35d52 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74 3b  arent = pParent;
35d53 0a 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 20  .  pNode->zData 
35d54 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b 31  = (u8 *)&pNode[1
35d55 5d 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  ];.  pNode->nRef
35d56 20 3d 20 31 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69   = 1;.  pNode->i
35d57 4e 6f 64 65 20 3d 20 69 4e 6f 64 65 3b 0a 20 20  Node = iNode;.  
35d58 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
35d59 20 30 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 4e 65   0;.  pNode->pNe
35d5a 78 74 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74  xt = 0;..  sqlit
35d5b 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
35d5c 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 2c  tree->pReadNode,
35d5d 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 72 63   1, iNode);.  rc
35d5e 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
35d5f 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
35d60 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
35d61 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
35d62 63 6f 6e 73 74 20 75 38 20 2a 7a 42 6c 6f 62 20  const u8 *zBlob 
35d63 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
35d64 5f 62 6c 6f 62 28 70 52 74 72 65 65 2d 3e 70 52  _blob(pRtree->pR
35d65 65 61 64 4e 6f 64 65 2c 20 30 29 3b 0a 20 20 20  eadNode, 0);.   
35d66 20 6d 65 6d 63 70 79 28 70 4e 6f 64 65 2d 3e 7a   memcpy(pNode->z
35d67 44 61 74 61 2c 20 7a 42 6c 6f 62 2c 20 70 52 74  Data, zBlob, pRt
35d68 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
35d69 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
35d6a 63 65 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d  ce(pParent);.  }
35d6b 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
35d6c 33 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a 20  3_free(pNode);. 
35d6d 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20     pNode = 0;.  
35d6e 7d 0a 0a 20 20 2a 70 70 4e 6f 64 65 20 3d 20 70  }..  *ppNode = p
35d6f 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Node;.  rc = sql
35d70 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
35d71 65 2d 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 0a  e->pReadNode);..
35d72 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
35d73 5f 4f 4b 20 26 26 20 69 4e 6f 64 65 3d 3d 31 20  _OK && iNode==1 
35d74 29 7b 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69  ){.    pRtree->i
35d75 44 65 70 74 68 20 3d 20 72 65 61 64 49 6e 74 31  Depth = readInt1
35d76 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 29 3b  6(pNode->zData);
35d77 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
35d78 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26  (rc==SQLITE_OK &
35d79 26 20 70 4e 6f 64 65 29 20 7c 7c 20 28 70 4e 6f  & pNode) || (pNo
35d7a 64 65 3d 3d 30 20 26 26 20 72 63 21 3d 53 51 4c  de==0 && rc!=SQL
35d7b 49 54 45 5f 4f 4b 29 20 29 3b 0a 20 20 6e 6f 64  ITE_OK) );.  nod
35d7c 65 48 61 73 68 49 6e 73 65 72 74 28 70 52 74 72  eHashInsert(pRtr
35d7d 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 72  ee, pNode);..  r
35d7e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
35d7f 2a 2a 20 4f 76 65 72 77 72 69 74 65 20 63 65 6c  ** Overwrite cel
35d80 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20  l iCell of node 
35d81 70 4e 6f 64 65 20 77 69 74 68 20 74 68 65 20 63  pNode with the c
35d82 6f 6e 74 65 6e 74 73 20 6f 66 20 70 43 65 6c 6c  ontents of pCell
35d83 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
35d84 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65   nodeOverwriteCe
35d85 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ll(.  Rtree *pRt
35d86 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64  ree, .  RtreeNod
35d87 65 20 2a 70 4e 6f 64 65 2c 20 20 0a 20 20 52 74  e *pNode,  .  Rt
35d88 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20  reeCell *pCell, 
35d89 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a 29 7b 0a  .  int iCell.){.
35d8a 20 20 69 6e 74 20 69 69 3b 0a 20 20 75 38 20 2a    int ii;.  u8 *
35d8b 70 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  p = &pNode->zDat
35d8c 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  a[4 + pRtree->nB
35d8d 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
35d8e 6c 5d 3b 0a 20 20 70 20 2b 3d 20 77 72 69 74 65  l];.  p += write
35d8f 49 6e 74 36 34 28 70 2c 20 70 43 65 6c 6c 2d 3e  Int64(p, pCell->
35d90 69 52 6f 77 69 64 29 3b 0a 20 20 66 6f 72 28 69  iRowid);.  for(i
35d91 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d  i=0; ii<(pRtree-
35d92 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 2b 29 7b  >nDim*2); ii++){
35d93 0a 20 20 20 20 70 20 2b 3d 20 77 72 69 74 65 43  .    p += writeC
35d94 6f 6f 72 64 28 70 2c 20 26 70 43 65 6c 6c 2d 3e  oord(p, &pCell->
35d95 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d  aCoord[ii]);.  }
35d96 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74  .  pNode->isDirt
35d97 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  y = 1;.}../*.** 
35d98 52 65 6d 6f 76 65 20 63 65 6c 6c 20 74 68 65 20  Remove cell the 
35d99 63 65 6c 6c 20 77 69 74 68 20 69 6e 64 65 78 20  cell with index 
35d9a 69 43 65 6c 6c 20 66 72 6f 6d 20 6e 6f 64 65 20  iCell from node 
35d9b 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  pNode..*/.static
35d9c 20 76 6f 69 64 20 6e 6f 64 65 44 65 6c 65 74 65   void nodeDelete
35d9d 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52 74 72  Cell(Rtree *pRtr
35d9e 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
35d9f 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29  Node, int iCell)
35da0 7b 0a 20 20 75 38 20 2a 70 44 73 74 20 3d 20 26  {.  u8 *pDst = &
35da1 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b  pNode->zData[4 +
35da2 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
35da3 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20  erCell*iCell];. 
35da4 20 75 38 20 2a 70 53 72 63 20 3d 20 26 70 44 73   u8 *pSrc = &pDs
35da5 74 5b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73  t[pRtree->nBytes
35da6 50 65 72 43 65 6c 6c 5d 3b 0a 20 20 69 6e 74 20  PerCell];.  int 
35da7 6e 42 79 74 65 20 3d 20 28 4e 43 45 4c 4c 28 70  nByte = (NCELL(p
35da8 4e 6f 64 65 29 20 2d 20 69 43 65 6c 6c 20 2d 20  Node) - iCell - 
35da9 31 29 20 2a 20 70 52 74 72 65 65 2d 3e 6e 42 79  1) * pRtree->nBy
35daa 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6d 65  tesPerCell;.  me
35dab 6d 6d 6f 76 65 28 70 44 73 74 2c 20 70 53 72 63  mmove(pDst, pSrc
35dac 2c 20 6e 42 79 74 65 29 3b 0a 20 20 77 72 69 74  , nByte);.  writ
35dad 65 49 6e 74 31 36 28 26 70 4e 6f 64 65 2d 3e 7a  eInt16(&pNode->z
35dae 44 61 74 61 5b 32 5d 2c 20 4e 43 45 4c 4c 28 70  Data[2], NCELL(p
35daf 4e 6f 64 65 29 2d 31 29 3b 0a 20 20 70 4e 6f 64  Node)-1);.  pNod
35db0 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a  e->isDirty = 1;.
35db1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
35db2 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
35db3 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e 74 6f 20  cell pCell into 
35db4 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20 74  node pNode. If t
35db5 68 65 20 69 6e 73 65 72 74 0a 2a 2a 20 69 73 20  he insert.** is 
35db6 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75  successful, retu
35db7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  rn SQLITE_OK..**
35db8 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
35db9 6e 6f 74 20 65 6e 6f 75 67 68 20 66 72 65 65 20  not enough free 
35dba 73 70 61 63 65 20 69 6e 20 70 4e 6f 64 65 2c 20  space in pNode, 
35dbb 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
35dbc 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  LL..*/.static in
35dbd 74 0a 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c  t.nodeInsertCell
35dbe 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
35dbf 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e, .  RtreeNode 
35dc0 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74 72 65 65  *pNode, .  Rtree
35dc1 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 0a 29 7b 0a  Cell *pCell .){.
35dc2 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20    int nCell;    
35dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dc4 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65  /* Current numbe
35dc5 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 70 4e  r of cells in pN
35dc6 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61  ode */.  int nMa
35dc7 78 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  xCell;          
35dc8 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
35dc9 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  m number of cell
35dca 73 20 66 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a 0a  s for pNode */..
35dcb 20 20 6e 4d 61 78 43 65 6c 6c 20 3d 20 28 70 52    nMaxCell = (pR
35dcc 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d  tree->iNodeSize-
35dcd 34 29 2f 70 52 74 72 65 65 2d 3e 6e 42 79 74 65  4)/pRtree->nByte
35dce 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6e 43 65 6c  sPerCell;.  nCel
35dcf 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
35dd0 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 43 65 6c  ;..  assert(nCel
35dd1 6c 3c 3d 6e 4d 61 78 43 65 6c 6c 29 3b 0a 0a 20  l<=nMaxCell);.. 
35dd2 20 69 66 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43   if( nCell<nMaxC
35dd3 65 6c 6c 20 29 7b 0a 20 20 20 20 6e 6f 64 65 4f  ell ){.    nodeO
35dd4 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74  verwriteCell(pRt
35dd5 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
35dd6 6c 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 20 20 77  l, nCell);.    w
35dd7 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f 64 65  riteInt16(&pNode
35dd8 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 6e 43 65 6c  ->zData[2], nCel
35dd9 6c 2b 31 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  l+1);.    pNode-
35dda 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20  >isDirty = 1;.  
35ddb 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 6e 43 65  }..  return (nCe
35ddc 6c 6c 3d 3d 6e 4d 61 78 43 65 6c 6c 29 3b 0a 7d  ll==nMaxCell);.}
35ddd 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ../*.** If the n
35dde 6f 64 65 20 69 73 20 64 69 72 74 79 2c 20 77 72  ode is dirty, wr
35ddf 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 74 68  ite it out to th
35de0 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73  e database..*/.s
35de1 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65 57 72  tatic int.nodeWr
35de2 69 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65  ite(Rtree *pRtre
35de3 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  e, RtreeNode *pN
35de4 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ode){.  int rc =
35de5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
35de6 28 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79  ( pNode->isDirty
35de7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
35de8 73 74 6d 74 20 2a 70 20 3d 20 70 52 74 72 65 65  stmt *p = pRtree
35de9 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20  ->pWriteNode;.  
35dea 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
35deb 64 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  de ){.      sqli
35dec 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
35ded 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  , 1, pNode->iNod
35dee 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
35def 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e       sqlite3_bin
35df0 64 5f 6e 75 6c 6c 28 70 2c 20 31 29 3b 0a 20 20  d_null(p, 1);.  
35df1 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
35df2 62 69 6e 64 5f 62 6c 6f 62 28 70 2c 20 32 2c 20  bind_blob(p, 2, 
35df3 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 70 52  pNode->zData, pR
35df4 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2c  tree->iNodeSize,
35df5 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
35df6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65  .    sqlite3_ste
35df7 70 28 70 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  p(p);.    pNode-
35df8 3e 69 73 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  >isDirty = 0;.  
35df9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
35dfa 65 73 65 74 28 70 29 3b 0a 20 20 20 20 69 66 28  eset(p);.    if(
35dfb 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 30   pNode->iNode==0
35dfc 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
35dfd 4b 20 29 7b 0a 20 20 20 20 20 20 70 4e 6f 64 65  K ){.      pNode
35dfe 2d 3e 69 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65  ->iNode = sqlite
35dff 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
35e00 77 69 64 28 70 52 74 72 65 65 2d 3e 64 62 29 3b  wid(pRtree->db);
35e01 0a 20 20 20 20 20 20 6e 6f 64 65 48 61 73 68 49  .      nodeHashI
35e02 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
35e03 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ode);.    }.  }.
35e04 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
35e05 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20  /*.** Release a 
35e06 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 6e  reference to a n
35e07 6f 64 65 2e 20 49 66 20 74 68 65 20 6e 6f 64 65  ode. If the node
35e08 20 69 73 20 64 69 72 74 79 20 61 6e 64 20 74 68   is dirty and th
35e09 65 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 63  e reference.** c
35e0a 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f 20 7a 65  ount drops to ze
35e0b 72 6f 2c 20 74 68 65 20 6e 6f 64 65 20 64 61 74  ro, the node dat
35e0c 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  a is written to 
35e0d 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  the database..*/
35e0e 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
35e0f 52 65 6c 65 61 73 65 28 52 74 72 65 65 20 2a 70  Release(Rtree *p
35e10 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
35e11 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20   *pNode){.  int 
35e12 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
35e13 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
35e14 20 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65     assert( pNode
35e15 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20  ->nRef>0 );.    
35e16 70 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  pNode->nRef--;. 
35e17 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 6e 52     if( pNode->nR
35e18 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  ef==0 ){.      i
35e19 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d  f( pNode->iNode=
35e1a 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52  =1 ){.        pR
35e1b 74 72 65 65 2d 3e 69 44 65 70 74 68 20 3d 20 2d  tree->iDepth = -
35e1c 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
35e1d 20 69 66 28 20 70 4e 6f 64 65 2d 3e 70 50 61 72   if( pNode->pPar
35e1e 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ent ){.        r
35e1f 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
35e20 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2d 3e 70  pRtree, pNode->p
35e21 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 7d  Parent);.      }
35e22 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
35e23 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
35e24 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 57 72 69      rc = nodeWri
35e25 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
35e26 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
35e27 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28   nodeHashDelete(
35e28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
35e29 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
35e2a 65 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d  ee(pNode);.    }
35e2b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
35e2c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
35e2d 6e 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74  n the 64-bit int
35e2e 65 67 65 72 20 76 61 6c 75 65 20 61 73 73 6f 63  eger value assoc
35e2f 69 61 74 65 64 20 77 69 74 68 20 63 65 6c 6c 20  iated with cell 
35e30 69 43 65 6c 6c 20 6f 66 0a 2a 2a 20 6e 6f 64 65  iCell of.** node
35e31 20 70 4e 6f 64 65 2e 20 49 66 20 70 4e 6f 64 65   pNode. If pNode
35e32 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c   is a leaf node,
35e33 20 74 68 69 73 20 69 73 20 61 20 72 6f 77 69 64   this is a rowid
35e34 2e 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 61 6e  . If it is.** an
35e35 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2c 20   internal node, 
35e36 74 68 65 6e 20 74 68 65 20 36 34 2d 62 69 74 20  then the 64-bit 
35e37 69 6e 74 65 67 65 72 20 69 73 20 61 20 63 68 69  integer is a chi
35e38 6c 64 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a  ld page number..
35e39 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6e 6f  */.static i64 no
35e3a 64 65 47 65 74 52 6f 77 69 64 28 0a 20 20 52 74  deGetRowid(.  Rt
35e3b 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
35e3c 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
35e3d 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a 29  , .  int iCell.)
35e3e 7b 0a 20 20 61 73 73 65 72 74 28 20 69 43 65 6c  {.  assert( iCel
35e3f 6c 3c 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 29  l<NCELL(pNode) )
35e40 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 61 64 49  ;.  return readI
35e41 6e 74 36 34 28 26 70 4e 6f 64 65 2d 3e 7a 44 61  nt64(&pNode->zDa
35e42 74 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e  ta[4 + pRtree->n
35e43 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65  BytesPerCell*iCe
35e44 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ll]);.}../*.** R
35e45 65 74 75 72 6e 20 63 6f 6f 72 64 69 6e 61 74 65  eturn coordinate
35e46 20 69 43 6f 6f 72 64 20 66 72 6f 6d 20 63 65 6c   iCoord from cel
35e47 6c 20 69 43 65 6c 6c 20 69 6e 20 6e 6f 64 65 20  l iCell in node 
35e48 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  pNode..*/.static
35e49 20 76 6f 69 64 20 6e 6f 64 65 47 65 74 43 6f 6f   void nodeGetCoo
35e4a 72 64 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  rd(.  Rtree *pRt
35e4b 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64  ree, .  RtreeNod
35e4c 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74  e *pNode, .  int
35e4d 20 69 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69 43   iCell,.  int iC
35e4e 6f 6f 72 64 2c 0a 20 20 52 74 72 65 65 43 6f 6f  oord,.  RtreeCoo
35e4f 72 64 20 2a 70 43 6f 6f 72 64 20 20 20 20 20 20  rd *pCoord      
35e50 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f       /* Space to
35e51 20 77 72 69 74 65 20 72 65 73 75 6c 74 20 74 6f   write result to
35e52 20 2a 2f 0a 29 7b 0a 20 20 72 65 61 64 43 6f 6f   */.){.  readCoo
35e53 72 64 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61  rd(&pNode->zData
35e54 5b 31 32 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  [12 + pRtree->nB
35e55 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
35e56 6c 20 2b 20 34 2a 69 43 6f 6f 72 64 5d 2c 20 70  l + 4*iCoord], p
35e57 43 6f 6f 72 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Coord);.}../*.**
35e58 20 44 65 73 65 72 69 61 6c 69 7a 65 20 63 65 6c   Deserialize cel
35e59 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20  l iCell of node 
35e5a 70 4e 6f 64 65 2e 20 50 6f 70 75 6c 61 74 65 20  pNode. Populate 
35e5b 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70 6f  the structure po
35e5c 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70  inted.** to by p
35e5d 43 65 6c 6c 20 77 69 74 68 20 74 68 65 20 72 65  Cell with the re
35e5e 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  sults..*/.static
35e5f 20 76 6f 69 64 20 6e 6f 64 65 47 65 74 43 65 6c   void nodeGetCel
35e60 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  l(.  Rtree *pRtr
35e61 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65  ee, .  RtreeNode
35e62 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20   *pNode, .  int 
35e63 69 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 43 65  iCell,.  RtreeCe
35e64 6c 6c 20 2a 70 43 65 6c 6c 0a 29 7b 0a 20 20 69  ll *pCell.){.  i
35e65 6e 74 20 69 69 3b 0a 20 20 70 43 65 6c 6c 2d 3e  nt ii;.  pCell->
35e66 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74  iRowid = nodeGet
35e67 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 4e  Rowid(pRtree, pN
35e68 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 66  ode, iCell);.  f
35e69 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72  or(ii=0; ii<pRtr
35e6a 65 65 2d 3e 6e 44 69 6d 2a 32 3b 20 69 69 2b 2b  ee->nDim*2; ii++
35e6b 29 7b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 6f  ){.    nodeGetCo
35e6c 6f 72 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ord(pRtree, pNod
35e6d 65 2c 20 69 43 65 6c 6c 2c 20 69 69 2c 20 26 70  e, iCell, ii, &p
35e6e 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d  Cell->aCoord[ii]
35e6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 46 6f  );.  }.}.../* Fo
35e70 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
35e71 6e 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69  n for the functi
35e72 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 74 68 65  on that does the
35e73 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 74 68 65 20   work of.** the 
35e74 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35e75 64 75 6c 65 20 78 43 72 65 61 74 65 28 29 20 61  dule xCreate() a
35e76 6e 64 20 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65  nd xConnect() me
35e77 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  thods..*/.static
35e78 20 69 6e 74 20 72 74 72 65 65 49 6e 69 74 28 0a   int rtreeInit(.
35e79 20 20 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69    sqlite3 *, voi
35e7a 64 20 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  d *, int, const 
35e7b 63 68 61 72 20 2a 63 6f 6e 73 74 2a 2c 20 73 71  char *const*, sq
35e7c 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 2c 20 63  lite3_vtab **, c
35e7d 68 61 72 20 2a 2a 2c 20 69 6e 74 0a 29 3b 0a 0a  har **, int.);..
35e7e 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  /* .** Rtree vir
35e7f 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
35e80 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64  e xCreate method
35e81 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35e82 72 74 72 65 65 43 72 65 61 74 65 28 0a 20 20 73  rtreeCreate(.  s
35e83 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f  qlite3 *db,.  vo
35e84 69 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20  id *pAux,.  int 
35e85 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
35e86 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20   *const*argv,.  
35e87 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
35e88 70 56 74 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a  pVtab,.  char **
35e89 70 7a 45 72 72 0a 29 7b 0a 20 20 72 65 74 75 72  pzErr.){.  retur
35e8a 6e 20 72 74 72 65 65 49 6e 69 74 28 64 62 2c 20  n rtreeInit(db, 
35e8b 70 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76  pAux, argc, argv
35e8c 2c 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72 2c  , ppVtab, pzErr,
35e8d 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52   1);.}../* .** R
35e8e 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
35e8f 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6f 6e 6e 65  le module xConne
35e90 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74  ct method..*/.st
35e91 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 43 6f  atic int rtreeCo
35e92 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33  nnect(.  sqlite3
35e93 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41   *db,.  void *pA
35e94 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  ux,.  int argc, 
35e95 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
35e96 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65  t*argv,.  sqlite
35e97 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
35e98 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a  .  char **pzErr.
35e99 29 7b 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65  ){.  return rtre
35e9a 65 49 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20  eInit(db, pAux, 
35e9b 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74  argc, argv, ppVt
35e9c 61 62 2c 20 70 7a 45 72 72 2c 20 30 29 3b 0a 7d  ab, pzErr, 0);.}
35e9d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e  ../*.** Incremen
35e9e 74 20 74 68 65 20 72 2d 74 72 65 65 20 72 65 66  t the r-tree ref
35e9f 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 0a 2a 2f  erence count..*/
35ea0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 74 72  .static void rtr
35ea1 65 65 52 65 66 65 72 65 6e 63 65 28 52 74 72 65  eeReference(Rtre
35ea2 65 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 70 52  e *pRtree){.  pR
35ea3 74 72 65 65 2d 3e 6e 42 75 73 79 2b 2b 3b 0a 7d  tree->nBusy++;.}
35ea4 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ../*.** Decremen
35ea5 74 20 74 68 65 20 72 2d 74 72 65 65 20 72 65 66  t the r-tree ref
35ea6 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 57 68  erence count. Wh
35ea7 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  en the reference
35ea8 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 0a 2a   count reaches.*
35ea9 2a 20 7a 65 72 6f 20 74 68 65 20 73 74 72 75 63  * zero the struc
35eaa 74 75 72 65 20 69 73 20 64 65 6c 65 74 65 64 2e  ture is deleted.
35eab 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
35eac 72 74 72 65 65 52 65 6c 65 61 73 65 28 52 74 72  rtreeRelease(Rtr
35ead 65 65 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 70  ee *pRtree){.  p
35eae 52 74 72 65 65 2d 3e 6e 42 75 73 79 2d 2d 3b 0a  Rtree->nBusy--;.
35eaf 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e 6e 42    if( pRtree->nB
35eb0 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  usy==0 ){.    sq
35eb1 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
35eb2 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65  Rtree->pReadNode
35eb3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
35eb4 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
35eb5 70 57 72 69 74 65 4e 6f 64 65 29 3b 0a 20 20 20  pWriteNode);.   
35eb6 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
35eb7 65 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  e(pRtree->pDelet
35eb8 65 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  eNode);.    sqli
35eb9 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74  te3_finalize(pRt
35eba 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29  ree->pReadRowid)
35ebb 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
35ebc 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
35ebd 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 20  WriteRowid);.   
35ebe 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
35ebf 65 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  e(pRtree->pDelet
35ec0 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  eRowid);.    sql
35ec1 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
35ec2 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
35ec3 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
35ec4 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d  finalize(pRtree-
35ec5 3e 70 57 72 69 74 65 50 61 72 65 6e 74 29 3b 0a  >pWriteParent);.
35ec6 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
35ec7 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
35ec8 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 20  leteParent);.   
35ec9 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 52   sqlite3_free(pR
35eca 74 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  tree);.  }.}../*
35ecb 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75   .** Rtree virtu
35ecc 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20  al table module 
35ecd 78 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 74 68  xDisconnect meth
35ece 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
35ecf 74 20 72 74 72 65 65 44 69 73 63 6f 6e 6e 65 63  t rtreeDisconnec
35ed0 74 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  t(sqlite3_vtab *
35ed1 70 56 74 61 62 29 7b 0a 20 20 72 74 72 65 65 52  pVtab){.  rtreeR
35ed2 65 6c 65 61 73 65 28 28 52 74 72 65 65 20 2a 29  elease((Rtree *)
35ed3 70 56 74 61 62 29 3b 0a 20 20 72 65 74 75 72 6e  pVtab);.  return
35ed4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
35ed5 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
35ed6 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
35ed7 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64   xDestroy method
35ed8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35ed9 72 74 72 65 65 44 65 73 74 72 6f 79 28 73 71 6c  rtreeDestroy(sql
35eda 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
35edb 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
35edc 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56  ee = (Rtree *)pV
35edd 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  tab;.  int rc;. 
35ede 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65 20 3d   char *zCreate =
35edf 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
35ee0 28 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c  (.    "DROP TABL
35ee1 45 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27  E '%q'.'%q_node'
35ee2 3b 22 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42  ;".    "DROP TAB
35ee3 4c 45 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69  LE '%q'.'%q_rowi
35ee4 64 27 3b 22 0a 20 20 20 20 22 44 52 4f 50 20 54  d';".    "DROP T
35ee5 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 70 61  ABLE '%q'.'%q_pa
35ee6 72 65 6e 74 27 3b 22 2c 0a 20 20 20 20 70 52 74  rent';",.    pRt
35ee7 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65  ree->zDb, pRtree
35ee8 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 70 52  ->zName, .    pR
35ee9 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65  tree->zDb, pRtre
35eea 65 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 70 52  e->zName,.    pR
35eeb 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65  tree->zDb, pRtre
35eec 65 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b 0a 20 20  e->zName.  );.  
35eed 69 66 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a  if( !zCreate ){.
35eee 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
35eef 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NOMEM;.  }else{.
35ef0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
35ef1 5f 65 78 65 63 28 70 52 74 72 65 65 2d 3e 64 62  _exec(pRtree->db
35ef2 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c 20 30 2c  , zCreate, 0, 0,
35ef3 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
35ef4 5f 66 72 65 65 28 7a 43 72 65 61 74 65 29 3b 0a  _free(zCreate);.
35ef5 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
35ef6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
35ef7 74 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72  treeRelease(pRtr
35ef8 65 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ee);.  }..  retu
35ef9 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  rn rc;.}../* .**
35efa 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
35efb 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 4f 70 65  able module xOpe
35efc 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  n method..*/.sta
35efd 74 69 63 20 69 6e 74 20 72 74 72 65 65 4f 70 65  tic int rtreeOpe
35efe 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  n(sqlite3_vtab *
35eff 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76  pVTab, sqlite3_v
35f00 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43  tab_cursor **ppC
35f01 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74 20 72 63  ursor){.  int rc
35f02 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
35f03 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
35f04 70 43 73 72 3b 0a 0a 20 20 70 43 73 72 20 3d 20  pCsr;..  pCsr = 
35f05 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 73  (RtreeCursor *)s
35f06 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
35f07 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73 6f 72  zeof(RtreeCursor
35f08 29 29 3b 0a 20 20 69 66 28 20 70 43 73 72 20 29  ));.  if( pCsr )
35f09 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 43 73  {.    memset(pCs
35f0a 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72  r, 0, sizeof(Rtr
35f0b 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20  eeCursor));.    
35f0c 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
35f0d 20 3d 20 70 56 54 61 62 3b 0a 20 20 20 20 72 63   = pVTab;.    rc
35f0e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
35f0f 7d 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20  }.  *ppCursor = 
35f10 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
35f11 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a 0a 20 20  rsor *)pCsr;..  
35f12 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
35f13 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75   .** Rtree virtu
35f14 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20  al table module 
35f15 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 2e 0a 2a  xClose method..*
35f16 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
35f17 65 65 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  eeClose(sqlite3_
35f18 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72  vtab_cursor *cur
35f19 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
35f1a 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 28 63  ee = (Rtree *)(c
35f1b 75 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20 69 6e  ur->pVtab);.  in
35f1c 74 20 72 63 3b 0a 20 20 52 74 72 65 65 43 75 72  t rc;.  RtreeCur
35f1d 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72  sor *pCsr = (Rtr
35f1e 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b 0a  eeCursor *)cur;.
35f1f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
35f20 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  Csr->aConstraint
35f21 29 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 52 65  );.  rc = nodeRe
35f22 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43  lease(pRtree, pC
35f23 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 73 71  sr->pNode);.  sq
35f24 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 29  lite3_free(pCsr)
35f25 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
35f26 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20 76 69  ../*.** Rtree vi
35f27 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
35f28 6c 65 20 78 45 6f 66 20 6d 65 74 68 6f 64 2e 0a  le xEof method..
35f29 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e  **.** Return non
35f2a 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 63 75 72  -zero if the cur
35f2b 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 63 75 72  sor does not cur
35f2c 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 20  rently point to 
35f2d 61 20 76 61 6c 69 64 20 0a 2a 2a 20 72 65 63 6f  a valid .** reco
35f2e 72 64 20 28 69 2e 65 20 69 66 20 74 68 65 20 73  rd (i.e if the s
35f2f 63 61 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64  can has finished
35f30 29 2c 20 6f 72 20 7a 65 72 6f 20 6f 74 68 65 72  ), or zero other
35f31 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
35f32 69 6e 74 20 72 74 72 65 65 45 6f 66 28 73 71 6c  int rtreeEof(sql
35f33 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
35f34 20 2a 63 75 72 29 7b 0a 20 20 52 74 72 65 65 43   *cur){.  RtreeC
35f35 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52  ursor *pCsr = (R
35f36 74 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72  treeCursor *)cur
35f37 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 43 73 72  ;.  return (pCsr
35f38 2d 3e 70 4e 6f 64 65 3d 3d 30 29 3b 0a 7d 0a 0a  ->pNode==0);.}..
35f39 2f 2a 20 0a 2a 2a 20 43 75 72 73 6f 72 20 70 43  /* .** Cursor pC
35f3a 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
35f3b 70 6f 69 6e 74 73 20 74 6f 20 61 20 63 65 6c 6c  points to a cell
35f3c 20 69 6e 20 61 20 6e 6f 6e 2d 6c 65 61 66 20 70   in a non-leaf p
35f3d 61 67 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74  age..** Return t
35f3e 72 75 65 20 69 66 20 74 68 65 20 73 75 62 2d 74  rue if the sub-t
35f3f 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
35f40 65 20 63 65 6c 6c 20 69 73 20 66 69 6c 74 65 72  e cell is filter
35f41 65 64 0a 2a 2a 20 28 65 78 63 6c 75 64 65 64 29  ed.** (excluded)
35f42 20 62 79 20 74 68 65 20 63 6f 6e 73 74 72 61 69   by the constrai
35f43 6e 74 73 20 69 6e 20 74 68 65 20 70 43 75 72 73  nts in the pCurs
35f44 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  or->aConstraint[
35f45 5d 20 0a 2a 2a 20 61 72 72 61 79 2c 20 6f 72 20  ] .** array, or 
35f46 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e  false otherwise.
35f47 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
35f48 65 73 74 52 74 72 65 65 43 65 6c 6c 28 52 74 72  estRtreeCell(Rtr
35f49 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
35f4a 65 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  eCursor *pCursor
35f4b 29 7b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63  ){.  RtreeCell c
35f4c 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  ell;.  int ii;. 
35f4d 20 69 6e 74 20 62 52 65 73 20 3d 20 30 3b 0a 0a   int bRes = 0;..
35f4e 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
35f4f 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70  tree, pCursor->p
35f50 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69  Node, pCursor->i
35f51 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
35f52 66 6f 72 28 69 69 3d 30 3b 20 62 52 65 73 3d 3d  for(ii=0; bRes==
35f53 30 20 26 26 20 69 69 3c 70 43 75 72 73 6f 72 2d  0 && ii<pCursor-
35f54 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69  >nConstraint; ii
35f55 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f  ++){.    RtreeCo
35f56 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70  nstraint *p = &p
35f57 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61  Cursor->aConstra
35f58 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75  int[ii];.    dou
35f59 62 6c 65 20 63 65 6c 6c 5f 6d 69 6e 20 3d 20 44  ble cell_min = D
35f5a 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72  COORD(cell.aCoor
35f5b 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29  d[(p->iCoord>>1)
35f5c 2a 32 5d 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65  *2]);.    double
35f5d 20 63 65 6c 6c 5f 6d 61 78 20 3d 20 44 43 4f 4f   cell_max = DCOO
35f5e 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28  RD(cell.aCoord[(
35f5f 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29 2a 32 2b  p->iCoord>>1)*2+
35f60 31 5d 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  1]);..    assert
35f61 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45  (p->op==RTREE_LE
35f62 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
35f63 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LT || p->op==RT
35f64 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20 20 20  REE_GE .        
35f65 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
35f66 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  GT || p->op==RTR
35f67 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 0a 20 20  EE_EQ.    );..  
35f68 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20    switch( p->op 
35f69 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54  ){.      case RT
35f6a 52 45 45 5f 4c 45 3a 20 63 61 73 65 20 52 54 52  REE_LE: case RTR
35f6b 45 45 5f 4c 54 3a 20 62 52 65 73 20 3d 20 70 2d  EE_LT: bRes = p-
35f6c 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e  >rValue<cell_min
35f6d 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63  ; break;.      c
35f6e 61 73 65 20 52 54 52 45 45 5f 47 45 3a 20 63 61  ase RTREE_GE: ca
35f6f 73 65 20 52 54 52 45 45 5f 47 54 3a 20 62 52 65  se RTREE_GT: bRe
35f70 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65 3e 63 65  s = p->rValue>ce
35f71 6c 6c 5f 6d 61 78 3b 20 62 72 65 61 6b 3b 0a 20  ll_max; break;. 
35f72 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
35f73 45 51 3a 20 0a 20 20 20 20 20 20 20 20 62 52 65  EQ: .        bRe
35f74 73 20 3d 20 28 70 2d 3e 72 56 61 6c 75 65 3e 63  s = (p->rValue>c
35f75 65 6c 6c 5f 6d 61 78 20 7c 7c 20 70 2d 3e 72 56  ell_max || p->rV
35f76 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e 29 3b 0a  alue<cell_min);.
35f77 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
35f78 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
35f79 72 6e 20 62 52 65 73 3b 0a 7d 0a 0a 2f 2a 20 0a  rn bRes;.}../* .
35f7a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
35f7b 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
35f7c 63 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20 63  cursor pCursor c
35f7d 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20  urrently points 
35f7e 74 6f 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20 66  to.** would be f
35f7f 69 6c 74 65 72 65 64 20 28 65 78 63 6c 75 64 65  iltered (exclude
35f80 64 29 20 62 79 20 74 68 65 20 63 6f 6e 73 74 72  d) by the constr
35f81 61 69 6e 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a  aints in the .**
35f82 20 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74   pCursor->aConst
35f83 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20 6f  raint[] array, o
35f84 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73  r false otherwis
35f85 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  e..**.** This fu
35f86 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  nction assumes t
35f87 68 61 74 20 74 68 65 20 63 65 6c 6c 20 69 73 20  hat the cell is 
35f88 70 61 72 74 20 6f 66 20 61 20 6c 65 61 66 20 6e  part of a leaf n
35f89 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ode..*/.static i
35f8a 6e 74 20 74 65 73 74 52 74 72 65 65 45 6e 74 72  nt testRtreeEntr
35f8b 79 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  y(Rtree *pRtree,
35f8c 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
35f8d 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43  ursor){.  RtreeC
35f8e 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20  ell cell;.  int 
35f8f 69 69 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65  ii;..  nodeGetCe
35f90 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  ll(pRtree, pCurs
35f91 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73  or->pNode, pCurs
35f92 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  or->iCell, &cell
35f93 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
35f94 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73  i<pCursor->nCons
35f95 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  traint; ii++){. 
35f96 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69     RtreeConstrai
35f97 6e 74 20 2a 70 20 3d 20 26 70 43 75 72 73 6f 72  nt *p = &pCursor
35f98 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69  ->aConstraint[ii
35f99 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 6f  ];.    double co
35f9a 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c  ord = DCOORD(cel
35f9b 6c 2e 61 43 6f 6f 72 64 5b 70 2d 3e 69 43 6f 6f  l.aCoord[p->iCoo
35f9c 72 64 5d 29 3b 0a 20 20 20 20 69 6e 74 20 72 65  rd]);.    int re
35f9d 73 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 2d  s;.    assert(p-
35f9e 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45 20 7c 7c  >op==RTREE_LE ||
35f9f 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 54   p->op==RTREE_LT
35fa0 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
35fa1 5f 47 45 20 0a 20 20 20 20 20 20 20 20 7c 7c 20  _GE .        || 
35fa2 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 54 20  p->op==RTREE_GT 
35fa3 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
35fa4 45 51 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 77  EQ.    );.    sw
35fa5 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20  itch( p->op ){. 
35fa6 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
35fa7 4c 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64  LE: res = (coord
35fa8 3c 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72  <=p->rValue); br
35fa9 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
35faa 52 54 52 45 45 5f 4c 54 3a 20 72 65 73 20 3d 20  RTREE_LT: res = 
35fab 28 63 6f 6f 72 64 3c 70 2d 3e 72 56 61 6c 75 65  (coord<p->rValue
35fac 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  );  break;.     
35fad 20 63 61 73 65 20 52 54 52 45 45 5f 47 45 3a 20   case RTREE_GE: 
35fae 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e 3d 70 2d  res = (coord>=p-
35faf 3e 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b  >rValue); break;
35fb0 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45  .      case RTRE
35fb1 45 5f 47 54 3a 20 72 65 73 20 3d 20 28 63 6f 6f  E_GT: res = (coo
35fb2 72 64 3e 70 2d 3e 72 56 61 6c 75 65 29 3b 20 20  rd>p->rValue);  
35fb3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
35fb4 65 20 52 54 52 45 45 5f 45 51 3a 20 72 65 73 20  e RTREE_EQ: res 
35fb5 3d 20 28 63 6f 6f 72 64 3d 3d 70 2d 3e 72 56 61  = (coord==p->rVa
35fb6 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20  lue); break;.   
35fb7 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 72 65 73   }..    if( !res
35fb8 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d   ) return 1;.  }
35fb9 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ..  return 0;.}.
35fba 0a 2f 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 70 43  ./*.** Cursor pC
35fbb 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
35fbc 70 6f 69 6e 74 73 20 61 74 20 61 20 6e 6f 64 65  points at a node
35fbd 20 74 68 61 74 20 68 65 61 64 73 20 61 20 73 75   that heads a su
35fbe 62 2d 74 72 65 65 20 6f 66 0a 2a 2a 20 68 65 69  b-tree of.** hei
35fbf 67 68 74 20 69 48 65 69 67 68 74 20 28 69 66 20  ght iHeight (if 
35fc0 69 48 65 69 67 68 74 3d 3d 30 2c 20 74 68 65 6e  iHeight==0, then
35fc1 20 74 68 65 20 6e 6f 64 65 20 69 73 20 61 20 6c   the node is a l
35fc2 65 61 66 29 2e 20 44 65 73 63 65 6e 64 0a 2a 2a  eaf). Descend.**
35fc3 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
35fc4 20 6c 65 66 74 2d 6d 6f 73 74 20 63 65 6c 6c 20   left-most cell 
35fc5 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20  of the sub-tree 
35fc6 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65  that matches the
35fc7 20 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20   .** configured 
35fc8 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2f 0a  constraints..*/.
35fc9 73 74 61 74 69 63 20 69 6e 74 20 64 65 73 63 65  static int desce
35fca 6e 64 54 6f 43 65 6c 6c 28 0a 20 20 52 74 72 65  ndToCell(.  Rtre
35fcb 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74  e *pRtree, .  Rt
35fcc 72 65 65 43 75 72 73 6f 72 20 2a 70 43 75 72 73  reeCursor *pCurs
35fcd 6f 72 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67  or, .  int iHeig
35fce 68 74 2c 0a 20 20 69 6e 74 20 2a 70 45 6f 66 20  ht,.  int *pEof 
35fcf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35fd0 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74  /* OUT: Set to t
35fd1 72 75 65 20 69 66 20 63 61 6e 6e 6f 74 20 64 65  rue if cannot de
35fd2 73 63 65 6e 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e  scend */.){.  in
35fd3 74 20 69 73 45 6f 66 3b 0a 20 20 69 6e 74 20 72  t isEof;.  int r
35fd4 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 52  c;.  int ii;.  R
35fd5 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64  treeNode *pChild
35fd6 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
35fd7 34 20 69 52 6f 77 69 64 3b 0a 0a 20 20 52 74 72  4 iRowid;..  Rtr
35fd8 65 65 4e 6f 64 65 20 2a 70 53 61 76 65 64 4e 6f  eeNode *pSavedNo
35fd9 64 65 20 3d 20 70 43 75 72 73 6f 72 2d 3e 70 4e  de = pCursor->pN
35fda 6f 64 65 3b 0a 20 20 69 6e 74 20 69 53 61 76 65  ode;.  int iSave
35fdb 64 43 65 6c 6c 20 3d 20 70 43 75 72 73 6f 72 2d  dCell = pCursor-
35fdc 3e 69 43 65 6c 6c 3b 0a 0a 20 20 61 73 73 65 72  >iCell;..  asser
35fdd 74 28 20 69 48 65 69 67 68 74 3e 3d 30 20 29 3b  t( iHeight>=0 );
35fde 0a 0a 20 20 69 66 28 20 69 48 65 69 67 68 74 3d  ..  if( iHeight=
35fdf 3d 30 20 29 7b 0a 20 20 20 20 69 73 45 6f 66 20  =0 ){.    isEof 
35fe0 3d 20 74 65 73 74 52 74 72 65 65 45 6e 74 72 79  = testRtreeEntry
35fe1 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
35fe2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
35fe3 69 73 45 6f 66 20 3d 20 74 65 73 74 52 74 72 65  isEof = testRtre
35fe4 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  eCell(pRtree, pC
35fe5 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66  ursor);.  }.  if
35fe6 28 20 69 73 45 6f 66 20 7c 7c 20 69 48 65 69 67  ( isEof || iHeig
35fe7 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 45  ht==0 ){.    *pE
35fe8 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20 20 20  of = isEof;.    
35fe9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
35fea 3b 0a 20 20 7d 0a 0a 20 20 69 52 6f 77 69 64 20  ;.  }..  iRowid 
35feb 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  = nodeGetRowid(p
35fec 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e  Rtree, pCursor->
35fed 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e  pNode, pCursor->
35fee 69 43 65 6c 6c 29 3b 0a 20 20 72 63 20 3d 20 6e  iCell);.  rc = n
35fef 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65  odeAcquire(pRtre
35ff0 65 2c 20 69 52 6f 77 69 64 2c 20 70 43 75 72 73  e, iRowid, pCurs
35ff1 6f 72 2d 3e 70 4e 6f 64 65 2c 20 26 70 43 68 69  or->pNode, &pChi
35ff2 6c 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ld);.  if( rc!=S
35ff3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
35ff4 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
35ff5 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
35ff6 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70  tree, pCursor->p
35ff7 4e 6f 64 65 29 3b 0a 20 20 70 43 75 72 73 6f 72  Node);.  pCursor
35ff8 2d 3e 70 4e 6f 64 65 20 3d 20 70 43 68 69 6c 64  ->pNode = pChild
35ff9 3b 0a 20 20 69 73 45 6f 66 20 3d 20 31 3b 0a 20  ;.  isEof = 1;. 
35ffa 20 66 6f 72 28 69 69 3d 30 3b 20 69 73 45 6f 66   for(ii=0; isEof
35ffb 20 26 26 20 69 69 3c 4e 43 45 4c 4c 28 70 43 68   && ii<NCELL(pCh
35ffc 69 6c 64 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  ild); ii++){.   
35ffd 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 20   pCursor->iCell 
35ffe 3d 20 69 69 3b 0a 20 20 20 20 72 63 20 3d 20 64  = ii;.    rc = d
35fff 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52 74  escendToCell(pRt
36000 72 65 65 2c 20 70 43 75 72 73 6f 72 2c 20 69 48  ree, pCursor, iH
36001 65 69 67 68 74 2d 31 2c 20 26 69 73 45 6f 66 29  eight-1, &isEof)
36002 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
36003 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
36004 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
36005 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 45  }.  }..  if( isE
36006 6f 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  of ){.    assert
36007 28 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65  ( pCursor->pNode
36008 3d 3d 70 43 68 69 6c 64 20 29 3b 0a 20 20 20 20  ==pChild );.    
36009 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 53  nodeReference(pS
3600a 61 76 65 64 4e 6f 64 65 29 3b 0a 20 20 20 20 6e  avedNode);.    n
3600b 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
3600c 65 2c 20 70 43 68 69 6c 64 29 3b 0a 20 20 20 20  e, pChild);.    
3600d 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d  pCursor->pNode =
3600e 20 70 53 61 76 65 64 4e 6f 64 65 3b 0a 20 20 20   pSavedNode;.   
3600f 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 20   pCursor->iCell 
36010 3d 20 69 53 61 76 65 64 43 65 6c 6c 3b 0a 20 20  = iSavedCell;.  
36011 7d 0a 0a 20 20 2a 70 45 6f 66 20 3d 20 69 73 45  }..  *pEof = isE
36012 6f 66 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  of;.  return SQL
36013 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
36014 20 4f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c   One of the cell
36015 73 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 20  s in node pNode 
36016 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
36017 20 68 61 76 65 20 61 20 36 34 2d 62 69 74 20 0a   have a 64-bit .
36018 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  ** integer value
36019 20 65 71 75 61 6c 20 74 6f 20 69 52 6f 77 69 64   equal to iRowid
3601a 2e 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  . Return the ind
3601b 65 78 20 6f 66 20 74 68 69 73 20 63 65 6c 6c 2e  ex of this cell.
3601c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
3601d 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 52 74  odeRowidIndex(Rt
3601e 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3601f 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69  eeNode *pNode, i
36020 36 34 20 69 52 6f 77 69 64 29 7b 0a 20 20 69 6e  64 iRowid){.  in
36021 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30  t ii;.  for(ii=0
36022 3b 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  ; nodeGetRowid(p
36023 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69  Rtree, pNode, ii
36024 29 21 3d 69 52 6f 77 69 64 3b 20 69 69 2b 2b 29  )!=iRowid; ii++)
36025 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 69  {.    assert( ii
36026 3c 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d 31  <(NCELL(pNode)-1
36027 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) );.  }.  retur
36028 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n ii;.}../*.** R
36029 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
3602a 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
3602b 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72  aining a pointer
3602c 20 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 0a 2a   to node pNode.*
3602d 2a 20 69 6e 20 69 74 73 20 70 61 72 65 6e 74 2e  * in its parent.
3602e 20 49 66 20 70 4e 6f 64 65 20 69 73 20 74 68 65   If pNode is the
3602f 20 72 6f 6f 74 20 6e 6f 64 65 2c 20 72 65 74 75   root node, retu
36030 72 6e 20 2d 31 2e 0a 2a 2f 0a 73 74 61 74 69 63  rn -1..*/.static
36031 20 69 6e 74 20 6e 6f 64 65 50 61 72 65 6e 74 49   int nodeParentI
36032 6e 64 65 78 28 52 74 72 65 65 20 2a 70 52 74 72  ndex(Rtree *pRtr
36033 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
36034 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f  Node){.  RtreeNo
36035 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e  de *pParent = pN
36036 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  ode->pParent;.  
36037 69 66 28 20 70 50 61 72 65 6e 74 20 29 7b 0a 20  if( pParent ){. 
36038 20 20 20 72 65 74 75 72 6e 20 6e 6f 64 65 52 6f     return nodeRo
36039 77 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c  widIndex(pRtree,
3603a 20 70 50 61 72 65 6e 74 2c 20 70 4e 6f 64 65 2d   pParent, pNode-
3603b 3e 69 4e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72  >iNode);.  }.  r
3603c 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 20  eturn -1;.}../* 
3603d 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
3603e 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
3603f 4e 65 78 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  Next method..*/.
36040 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
36041 4e 65 78 74 28 73 71 6c 69 74 65 33 5f 76 74 61  Next(sqlite3_vta
36042 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
36043 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 20  ursor){.  Rtree 
36044 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
36045 20 2a 29 28 70 56 74 61 62 43 75 72 73 6f 72 2d   *)(pVtabCursor-
36046 3e 70 56 74 61 62 29 3b 0a 20 20 52 74 72 65 65  >pVtab);.  Rtree
36047 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
36048 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56  RtreeCursor *)pV
36049 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74  tabCursor;.  int
3604a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
3604b 0a 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 69 53  ..  if( pCsr->iS
3604c 74 72 61 74 65 67 79 3d 3d 31 20 29 7b 0a 20 20  trategy==1 ){.  
3604d 20 20 2f 2a 20 54 68 69 73 20 22 73 63 61 6e 22    /* This "scan"
3604e 20 69 73 20 61 20 64 69 72 65 63 74 20 6c 6f 6f   is a direct loo
3604f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20 54 68  kup by rowid. Th
36050 65 72 65 20 69 73 20 6e 6f 20 6e 65 78 74 20 65  ere is no next e
36051 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 6e 6f 64  ntry. */.    nod
36052 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
36053 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20   pCsr->pNode);. 
36054 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d     pCsr->pNode =
36055 20 30 3b 0a 20 20 7d 0a 0a 20 20 65 6c 73 65 20   0;.  }..  else 
36056 69 66 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20  if( pCsr->pNode 
36057 29 7b 0a 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74  ){.    /* Move t
36058 6f 20 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79  o the next entry
36059 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68   that matches th
3605a 65 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e  e configured con
3605b 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a 20 20 20  straints. */.   
3605c 20 69 6e 74 20 69 48 65 69 67 68 74 20 3d 20 30   int iHeight = 0
3605d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 73  ;.    while( pCs
3605e 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20  r->pNode ){.    
3605f 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
36060 64 65 20 3d 20 70 43 73 72 2d 3e 70 4e 6f 64 65  de = pCsr->pNode
36061 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 65 6c  ;.      int nCel
36062 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
36063 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 43 73 72  ;.      for(pCsr
36064 2d 3e 69 43 65 6c 6c 2b 2b 3b 20 70 43 73 72 2d  ->iCell++; pCsr-
36065 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20 70 43  >iCell<nCell; pC
36066 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20  sr->iCell++){.  
36067 20 20 20 20 20 20 69 6e 74 20 69 73 45 6f 66 3b        int isEof;
36068 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 64 65  .        rc = de
36069 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52 74 72  scendToCell(pRtr
3606a 65 65 2c 20 70 43 73 72 2c 20 69 48 65 69 67 68  ee, pCsr, iHeigh
3606b 74 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20  t, &isEof);.    
3606c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
3606d 54 45 5f 4f 4b 20 7c 7c 20 21 69 73 45 6f 66 20  TE_OK || !isEof 
3606e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
3606f 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
36070 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
36071 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4e  pCsr->pNode = pN
36072 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  ode->pParent;.  
36073 20 20 20 20 70 43 73 72 2d 3e 69 43 65 6c 6c 20      pCsr->iCell 
36074 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65  = nodeParentInde
36075 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  x(pRtree, pNode)
36076 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65  ;.      nodeRefe
36077 72 65 6e 63 65 28 70 43 73 72 2d 3e 70 4e 6f 64  rence(pCsr->pNod
36078 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65  e);.      nodeRe
36079 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e  lease(pRtree, pN
3607a 6f 64 65 29 3b 0a 20 20 20 20 20 20 69 48 65 69  ode);.      iHei
3607b 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ght++;.    }.  }
3607c 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
3607d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
3607e 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
3607f 75 6c 65 20 78 52 6f 77 69 64 20 6d 65 74 68 6f  ule xRowid metho
36080 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
36081 20 72 74 72 65 65 52 6f 77 69 64 28 73 71 6c 69   rtreeRowid(sqli
36082 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
36083 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20 73 71  *pVtabCursor, sq
36084 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77  lite_int64 *pRow
36085 69 64 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52  id){.  Rtree *pR
36086 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29  tree = (Rtree *)
36087 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
36088 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  ab;.  RtreeCurso
36089 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65  r *pCsr = (Rtree
3608a 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75  Cursor *)pVtabCu
3608b 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  rsor;..  assert(
3608c 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20  pCsr->pNode);.  
3608d 2a 70 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65  *pRowid = nodeGe
3608e 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
3608f 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72  Csr->pNode, pCsr
36090 2d 3e 69 43 65 6c 6c 29 3b 0a 0a 20 20 72 65 74  ->iCell);..  ret
36091 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
36092 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
36093 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
36094 75 6c 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68  ule xColumn meth
36095 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
36096 74 20 72 74 72 65 65 43 6f 6c 75 6d 6e 28 73 71  t rtreeColumn(sq
36097 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
36098 72 20 2a 63 75 72 2c 20 73 71 6c 69 74 65 33 5f  r *cur, sqlite3_
36099 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e  context *ctx, in
3609a 74 20 69 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  t i){.  Rtree *p
3609b 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
3609c 29 63 75 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52  )cur->pVtab;.  R
3609d 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72  treeCursor *pCsr
3609e 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
3609f 2a 29 63 75 72 3b 0a 0a 20 20 69 66 28 20 69 3d  *)cur;..  if( i=
360a0 3d 30 20 29 7b 0a 20 20 20 20 69 36 34 20 69 52  =0 ){.    i64 iR
360a1 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
360a2 77 69 64 28 70 52 74 72 65 65 2c 20 70 43 73 72  wid(pRtree, pCsr
360a3 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69  ->pNode, pCsr->i
360a4 43 65 6c 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  Cell);.    sqlit
360a5 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
360a6 63 74 78 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20  ctx, iRowid);.  
360a7 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74 72 65 65  }else{.    Rtree
360a8 43 6f 6f 72 64 20 63 3b 0a 20 20 20 20 6e 6f 64  Coord c;.    nod
360a9 65 47 65 74 43 6f 6f 72 64 28 70 52 74 72 65 65  eGetCoord(pRtree
360aa 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70  , pCsr->pNode, p
360ab 43 73 72 2d 3e 69 43 65 6c 6c 2c 20 69 2d 31 2c  Csr->iCell, i-1,
360ac 20 26 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52   &c);.    if( pR
360ad 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65  tree->eCoordType
360ae 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  ==RTREE_COORD_RE
360af 41 4c 33 32 20 29 7b 0a 20 20 20 20 20 20 73 71  AL32 ){.      sq
360b0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
360b1 62 6c 65 28 63 74 78 2c 20 63 2e 66 29 3b 0a 20  ble(ctx, c.f);. 
360b2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
360b3 61 73 73 65 72 74 28 20 70 52 74 72 65 65 2d 3e  assert( pRtree->
360b4 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
360b5 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 20 29 3b  E_COORD_INT32 );
360b6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
360b7 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20 63  esult_int(ctx, c
360b8 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  .i);.    }.  }..
360b9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
360ba 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 55 73  OK;.}../* .** Us
360bb 65 20 6e 6f 64 65 41 63 71 75 69 72 65 28 29 20  e nodeAcquire() 
360bc 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 65  to obtain the le
360bd 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69  af node containi
360be 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 77 69  ng the record wi
360bf 74 68 20 0a 2a 2a 20 72 6f 77 69 64 20 69 52 6f  th .** rowid iRo
360c0 77 69 64 2e 20 49 66 20 73 75 63 63 65 73 73 66  wid. If successf
360c1 75 6c 2c 20 73 65 74 20 2a 70 70 4c 65 61 66 20  ul, set *ppLeaf 
360c2 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
360c3 6e 6f 64 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75  node and.** retu
360c4 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66  rn SQLITE_OK. If
360c5 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63   there is no suc
360c6 68 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  h record in the 
360c7 74 61 62 6c 65 2c 20 73 65 74 0a 2a 2a 20 2a 70  table, set.** *p
360c8 70 4c 65 61 66 20 74 6f 20 30 20 61 6e 64 20 72  pLeaf to 0 and r
360c9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
360ca 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
360cb 75 72 73 2c 20 73 65 74 20 2a 70 70 4c 65 61 66  urs, set *ppLeaf
360cc 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 61 6e 64 20  .** to zero and 
360cd 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65  return an SQLite
360ce 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
360cf 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64 4c  static int findL
360d0 65 61 66 4e 6f 64 65 28 52 74 72 65 65 20 2a 70  eafNode(Rtree *p
360d1 52 74 72 65 65 2c 20 69 36 34 20 69 52 6f 77 69  Rtree, i64 iRowi
360d2 64 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  d, RtreeNode **p
360d3 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20 72 63  pLeaf){.  int rc
360d4 3b 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20 30 3b  ;.  *ppLeaf = 0;
360d5 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
360d6 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
360d7 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 69 52 6f  eadRowid, 1, iRo
360d8 77 69 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  wid);.  if( sqli
360d9 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d  te3_step(pRtree-
360da 3e 70 52 65 61 64 52 6f 77 69 64 29 3d 3d 53 51  >pReadRowid)==SQ
360db 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
360dc 69 36 34 20 69 4e 6f 64 65 20 3d 20 73 71 6c 69  i64 iNode = sqli
360dd 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
360de 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
360df 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 72 63 20  wid, 0);.    rc 
360e0 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
360e1 74 72 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20  tree, iNode, 0, 
360e2 70 70 4c 65 61 66 29 3b 0a 20 20 20 20 73 71 6c  ppLeaf);.    sql
360e3 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
360e4 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
360e5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
360e6 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
360e7 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
360e8 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  id);.  }.  retur
360e9 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 20 0a 2a 2a  n rc;.}.../* .**
360ea 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
360eb 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 46 69 6c  able module xFil
360ec 74 65 72 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ter method..*/.s
360ed 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 46  tatic int rtreeF
360ee 69 6c 74 65 72 28 0a 20 20 73 71 6c 69 74 65 33  ilter(.  sqlite3
360ef 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
360f0 74 61 62 43 75 72 73 6f 72 2c 20 0a 20 20 69 6e  tabCursor, .  in
360f1 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20  t idxNum, const 
360f2 63 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20 20  char *idxStr,.  
360f3 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
360f4 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
360f5 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  {.  Rtree *pRtre
360f6 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74  e = (Rtree *)pVt
360f7 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
360f8 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
360f9 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72  pCsr = (RtreeCur
360fa 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72 73 6f  sor *)pVtabCurso
360fb 72 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  r;..  RtreeNode 
360fc 2a 70 52 6f 6f 74 20 3d 20 30 3b 0a 20 20 69 6e  *pRoot = 0;.  in
360fd 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t ii;.  int rc =
360fe 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72   SQLITE_OK;..  r
360ff 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 70 52  treeReference(pR
36100 74 72 65 65 29 3b 0a 0a 20 20 73 71 6c 69 74 65  tree);..  sqlite
36101 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61 43 6f  3_free(pCsr->aCo
36102 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 70 43 73  nstraint);.  pCs
36103 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d  r->aConstraint =
36104 20 30 3b 0a 20 20 70 43 73 72 2d 3e 69 53 74 72   0;.  pCsr->iStr
36105 61 74 65 67 79 20 3d 20 69 64 78 4e 75 6d 3b 0a  ategy = idxNum;.
36106 0a 20 20 69 66 28 20 69 64 78 4e 75 6d 3d 3d 31  .  if( idxNum==1
36107 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70 65 63 69   ){.    /* Speci
36108 61 6c 20 63 61 73 65 20 2d 20 6c 6f 6f 6b 75 70  al case - lookup
36109 20 62 79 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20   by rowid. */.  
3610a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65    RtreeNode *pLe
3610b 61 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  af;        /* Le
3610c 61 66 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  af on which the 
3610d 72 65 71 75 69 72 65 64 20 63 65 6c 6c 20 72 65  required cell re
3610e 73 69 64 65 73 20 2a 2f 0a 20 20 20 20 69 36 34  sides */.    i64
3610f 20 69 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65   iRowid = sqlite
36110 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
36111 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 63 20 3d  gv[0]);.    rc =
36112 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 70 52   findLeafNode(pR
36113 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 26 70  tree, iRowid, &p
36114 4c 65 61 66 29 3b 0a 20 20 20 20 70 43 73 72 2d  Leaf);.    pCsr-
36115 3e 70 4e 6f 64 65 20 3d 20 70 4c 65 61 66 3b 20  >pNode = pLeaf; 
36116 0a 20 20 20 20 69 66 28 20 70 4c 65 61 66 20 26  .    if( pLeaf &
36117 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
36118 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69  ){.      pCsr->i
36119 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77 69 64  Cell = nodeRowid
3611a 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4c  Index(pRtree, pL
3611b 65 61 66 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20  eaf, iRowid);.  
3611c 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
3611d 20 2f 2a 20 4e 6f 72 6d 61 6c 20 63 61 73 65 20   /* Normal case 
3611e 2d 20 72 2d 74 72 65 65 20 73 63 61 6e 2e 20 53  - r-tree scan. S
3611f 65 74 20 75 70 20 74 68 65 20 52 74 72 65 65 43  et up the RtreeC
36120 75 72 73 6f 72 2e 61 43 6f 6e 73 74 72 61 69 6e  ursor.aConstrain
36121 74 20 61 72 72 61 79 20 0a 20 20 20 20 2a 2a 20  t array .    ** 
36122 77 69 74 68 20 74 68 65 20 63 6f 6e 66 69 67 75  with the configu
36123 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  red constraints.
36124 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28   .    */.    if(
36125 20 61 72 67 63 3e 30 20 29 7b 0a 20 20 20 20 20   argc>0 ){.     
36126 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69   pCsr->aConstrai
36127 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  nt = sqlite3_mal
36128 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
36129 43 6f 6e 73 74 72 61 69 6e 74 29 2a 61 72 67 63  Constraint)*argc
3612a 29 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 6e  );.      pCsr->n
3612b 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 61 72 67  Constraint = arg
3612c 63 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43  c;.      if( !pC
3612d 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20  sr->aConstraint 
3612e 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
3612f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
36130 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36131 20 20 20 61 73 73 65 72 74 28 20 28 69 64 78 53     assert( (idxS
36132 74 72 3d 3d 30 20 26 26 20 61 72 67 63 3d 3d 30  tr==0 && argc==0
36133 29 20 7c 7c 20 73 74 72 6c 65 6e 28 69 64 78 53  ) || strlen(idxS
36134 74 72 29 3d 3d 61 72 67 63 2a 32 20 29 3b 0a 20  tr)==argc*2 );. 
36135 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b         for(ii=0;
36136 20 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29 7b   ii<argc; ii++){
36137 0a 20 20 20 20 20 20 20 20 20 20 52 74 72 65 65  .          Rtree
36138 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d 20  Constraint *p = 
36139 26 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  &pCsr->aConstrai
3613a 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 20 20 20 20  nt[ii];.        
3613b 20 20 70 2d 3e 6f 70 20 3d 20 69 64 78 53 74 72    p->op = idxStr
3613c 5b 69 69 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20  [ii*2];.        
3613d 20 20 70 2d 3e 69 43 6f 6f 72 64 20 3d 20 69 64    p->iCoord = id
3613e 78 53 74 72 5b 69 69 2a 32 2b 31 5d 2d 27 61 27  xStr[ii*2+1]-'a'
3613f 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72  ;.          p->r
36140 56 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  Value = sqlite3_
36141 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67  value_double(arg
36142 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20 20 20  v[ii]);.        
36143 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
36144 20 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53    .    if( rc==S
36145 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
36146 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20    pCsr->pNode = 
36147 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  0;.      rc = no
36148 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
36149 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b  , 1, 0, &pRoot);
3614a 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
3614b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3614c 20 20 20 20 20 20 69 6e 74 20 69 73 45 6f 66 20        int isEof 
3614d 3d 20 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  = 1;.      int n
3614e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 52 6f  Cell = NCELL(pRo
3614f 6f 74 29 3b 0a 20 20 20 20 20 20 70 43 73 72 2d  ot);.      pCsr-
36150 3e 70 4e 6f 64 65 20 3d 20 70 52 6f 6f 74 3b 0a  >pNode = pRoot;.
36151 20 20 20 20 20 20 66 6f 72 28 70 43 73 72 2d 3e        for(pCsr->
36152 69 43 65 6c 6c 3d 30 3b 20 72 63 3d 3d 53 51 4c  iCell=0; rc==SQL
36153 49 54 45 5f 4f 4b 20 26 26 20 70 43 73 72 2d 3e  ITE_OK && pCsr->
36154 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20 70 43 73  iCell<nCell; pCs
36155 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20  r->iCell++){.   
36156 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73       assert( pCs
36157 72 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20  r->pNode==pRoot 
36158 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
36159 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52  descendToCell(pR
3615a 74 72 65 65 2c 20 70 43 73 72 2c 20 70 52 74 72  tree, pCsr, pRtr
3615b 65 65 2d 3e 69 44 65 70 74 68 2c 20 26 69 73 45  ee->iDepth, &isE
3615c 6f 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  of);.        if(
3615d 20 21 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 20   !isEof ){.     
3615e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3615f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
36160 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
36161 54 45 5f 4f 4b 20 26 26 20 69 73 45 6f 66 20 29  TE_OK && isEof )
36162 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
36163 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70  ( pCsr->pNode==p
36164 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20  Root );.        
36165 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
36166 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20  ee, pRoot);.    
36167 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20      pCsr->pNode 
36168 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
36169 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
3616a 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 70 43 73  QLITE_OK || !pCs
3616b 72 2d 3e 70 4e 6f 64 65 20 7c 7c 20 70 43 73 72  r->pNode || pCsr
3616c 2d 3e 69 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 43  ->iCell<NCELL(pC
3616d 73 72 2d 3e 70 4e 6f 64 65 29 20 29 3b 0a 20 20  sr->pNode) );.  
3616e 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 74 72 65 65    }.  }..  rtree
3616f 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b  Release(pRtree);
36170 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
36171 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  ./*.** Rtree vir
36172 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
36173 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74  e xBestIndex met
36174 68 6f 64 2e 20 54 68 65 72 65 20 61 72 65 20 74  hod. There are t
36175 68 72 65 65 0a 2a 2a 20 74 61 62 6c 65 20 73 63  hree.** table sc
36176 61 6e 20 73 74 72 61 74 65 67 69 65 73 20 74 6f  an strategies to
36177 20 63 68 6f 6f 73 65 20 66 72 6f 6d 20 28 69 6e   choose from (in
36178 20 6f 72 64 65 72 20 66 72 6f 6d 20 6d 6f 73 74   order from most
36179 20 74 6f 20 0a 2a 2a 20 6c 65 61 73 74 20 64 65   to .** least de
3617a 73 69 72 61 62 6c 65 29 3a 0a 2a 2a 0a 2a 2a 20  sirable):.**.** 
3617b 20 20 69 64 78 4e 75 6d 20 20 20 20 20 69 64 78    idxNum     idx
3617c 53 74 72 20 20 20 20 20 20 20 20 53 74 72 61 74  Str        Strat
3617d 65 67 79 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  egy.**   -------
3617e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3617f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
36180 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20  ---------.**    
36181 20 31 20 20 20 20 20 20 20 20 55 6e 75 73 65 64   1        Unused
36182 20 20 20 20 20 20 20 20 44 69 72 65 63 74 20 6c          Direct l
36183 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 0a  ookup by rowid..
36184 2a 2a 20 20 20 20 20 32 20 20 20 20 20 20 20 20  **     2        
36185 53 65 65 20 62 65 6c 6f 77 20 20 20 20 20 52 2d  See below     R-
36186 74 72 65 65 20 71 75 65 72 79 2e 0a 2a 2a 20 20  tree query..**  
36187 20 20 20 33 20 20 20 20 20 20 20 20 55 6e 75 73     3        Unus
36188 65 64 20 20 20 20 20 20 20 20 46 75 6c 6c 20 74  ed        Full t
36189 61 62 6c 65 20 73 63 61 6e 2e 0a 2a 2a 20 20 20  able scan..**   
3618a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3618b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3618c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3618d 0a 2a 2a 0a 2a 2a 20 49 66 20 73 74 72 61 74 65  .**.** If strate
3618e 67 79 20 31 20 6f 72 20 33 20 69 73 20 75 73 65  gy 1 or 3 is use
3618f 64 2c 20 74 68 65 6e 20 69 64 78 53 74 72 20 69  d, then idxStr i
36190 73 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c  s not meaningful
36191 2e 20 49 66 20 73 74 72 61 74 65 67 79 0a 2a 2a  . If strategy.**
36192 20 32 20 69 73 20 75 73 65 64 2c 20 69 64 78 53   2 is used, idxS
36193 74 72 20 69 73 20 66 6f 72 6d 61 74 74 65 64 20  tr is formatted 
36194 74 6f 20 63 6f 6e 74 61 69 6e 20 32 20 62 79 74  to contain 2 byt
36195 65 73 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20  es for each .** 
36196 63 6f 6e 73 74 72 61 69 6e 74 20 75 73 65 64 2e  constraint used.
36197 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 62   The first two b
36198 79 74 65 73 20 6f 66 20 69 64 78 53 74 72 20 63  ytes of idxStr c
36199 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 0a 2a 2a  orrespond to .**
3619a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
3619b 69 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  in sqlite3_index
3619c 5f 69 6e 66 6f 2e 61 43 6f 6e 73 74 72 61 69 6e  _info.aConstrain
3619d 74 55 73 61 67 65 5b 5d 20 77 69 74 68 0a 2a 2a  tUsage[] with.**
3619e 20 28 61 72 67 76 49 6e 64 65 78 3d 3d 31 29 20   (argvIndex==1) 
3619f 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  etc..**.** The f
361a0 69 72 73 74 20 6f 66 20 65 61 63 68 20 70 61 69  irst of each pai
361a1 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 69 64  r of bytes in id
361a2 78 53 74 72 20 69 64 65 6e 74 69 66 69 65 73 20  xStr identifies 
361a3 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  the constraint.*
361a4 2a 20 6f 70 65 72 61 74 6f 72 20 61 73 20 66 6f  * operator as fo
361a5 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f  llows:.**.**   O
361a6 70 65 72 61 74 6f 72 20 20 20 20 42 79 74 65 20  perator    Byte 
361a7 56 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d  Value.**   -----
361a8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
361a9 2d 0a 2a 2a 20 20 20 20 20 20 3d 20 20 20 20 20  -.**      =     
361aa 20 20 20 30 78 34 31 20 28 27 41 27 29 0a 2a 2a     0x41 ('A').**
361ab 20 20 20 20 20 3c 3d 20 20 20 20 20 20 20 20 30       <=        0
361ac 78 34 32 20 28 27 42 27 29 0a 2a 2a 20 20 20 20  x42 ('B').**    
361ad 20 20 3c 20 20 20 20 20 20 20 20 30 78 34 33 20    <        0x43 
361ae 28 27 43 27 29 0a 2a 2a 20 20 20 20 20 3e 3d 20  ('C').**     >= 
361af 20 20 20 20 20 20 20 30 78 34 34 20 28 27 44 27         0x44 ('D'
361b0 29 0a 2a 2a 20 20 20 20 20 20 3e 20 20 20 20 20  ).**      >     
361b1 20 20 20 30 78 34 35 20 28 27 45 27 29 0a 2a 2a     0x45 ('E').**
361b2 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
361b3 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
361b4 54 68 65 20 73 65 63 6f 6e 64 20 6f 66 20 65 61  The second of ea
361b5 63 68 20 70 61 69 72 20 6f 66 20 62 79 74 65 73  ch pair of bytes
361b6 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
361b7 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f 6c 75 6d  coordinate colum
361b8 6e 0a 2a 2a 20 74 6f 20 77 68 69 63 68 20 74 68  n.** to which th
361b9 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 70 70  e constraint app
361ba 6c 69 65 73 2e 20 54 68 65 20 6c 65 66 74 6d 6f  lies. The leftmo
361bb 73 74 20 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f  st coordinate co
361bc 6c 75 6d 6e 0a 2a 2a 20 69 73 20 27 61 27 2c 20  lumn.** is 'a', 
361bd 74 68 65 20 73 65 63 6f 6e 64 20 66 72 6f 6d 20  the second from 
361be 74 68 65 20 6c 65 66 74 20 27 62 27 20 65 74 63  the left 'b' etc
361bf 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
361c0 72 74 72 65 65 42 65 73 74 49 6e 64 65 78 28 73  rtreeBestIndex(s
361c1 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 74 61 62  qlite3_vtab *tab
361c2 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  , sqlite3_index_
361c3 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 29 7b  info *pIdxInfo){
361c4 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
361c5 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 69 2c  TE_OK;.  int ii,
361c6 20 63 43 6f 6c 3b 0a 0a 20 20 69 6e 74 20 69 49   cCol;..  int iI
361c7 64 78 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a  dx = 0;.  char z
361c8 49 64 78 53 74 72 5b 52 54 52 45 45 5f 4d 41 58  IdxStr[RTREE_MAX
361c9 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 38 2b 31 5d  _DIMENSIONS*8+1]
361ca 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 49 64 78 53  ;.  memset(zIdxS
361cb 74 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 7a 49  tr, 0, sizeof(zI
361cc 64 78 53 74 72 29 29 3b 0a 0a 20 20 61 73 73 65  dxStr));..  asse
361cd 72 74 28 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64  rt( pIdxInfo->id
361ce 78 53 74 72 3d 3d 30 20 29 3b 0a 20 20 66 6f 72  xStr==0 );.  for
361cf 28 69 69 3d 30 3b 20 69 69 3c 70 49 64 78 49 6e  (ii=0; ii<pIdxIn
361d0 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  fo->nConstraint;
361d1 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75   ii++){.    stru
361d2 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
361d3 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d  _constraint *p =
361d4 20 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e   &pIdxInfo->aCon
361d5 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 0a 20 20  straint[ii];..  
361d6 20 20 69 66 28 20 70 2d 3e 75 73 61 62 6c 65 20    if( p->usable 
361d7 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30  && p->iColumn==0
361d8 20 26 26 20 70 2d 3e 6f 70 3d 3d 53 51 4c 49 54   && p->op==SQLIT
361d9 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
361da 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 2f  NT_EQ ){.      /
361db 2a 20 57 65 20 68 61 76 65 20 61 6e 20 65 71 75  * We have an equ
361dc 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
361dd 20 6f 6e 20 74 68 65 20 72 6f 77 69 64 2e 20 55   on the rowid. U
361de 73 65 20 73 74 72 61 74 65 67 79 20 31 2e 20 2a  se strategy 1. *
361df 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  /.      int jj;.
361e0 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20        for(jj=0; 
361e1 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b 29 7b 0a 20 20  jj<ii; jj++){.  
361e2 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
361e3 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
361e4 5b 6a 6a 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d  [jj].argvIndex =
361e5 20 30 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78   0;.        pIdx
361e6 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
361e7 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20  tUsage[jj].omit 
361e8 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
361e9 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78     pIdxInfo->idx
361ea 4e 75 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  Num = 1;.      p
361eb 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
361ec 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 61 72  aintUsage[ii].ar
361ed 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20 20 20  gvIndex = 1;.   
361ee 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f     pIdxInfo->aCo
361ef 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a  nstraintUsage[jj
361f0 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20  ].omit = 1;..   
361f1 20 20 20 2f 2a 20 54 68 69 73 20 73 74 72 61 74     /* This strat
361f2 65 67 79 20 69 6e 76 6f 6c 76 65 73 20 61 20 74  egy involves a t
361f3 77 6f 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 73  wo rowid lookups
361f4 20 6f 6e 20 61 6e 20 42 2d 54 72 65 65 20 73 74   on an B-Tree st
361f5 72 75 63 74 75 72 65 73 0a 20 20 20 20 20 20 2a  ructures.      *
361f6 2a 20 61 6e 64 20 74 68 65 6e 20 61 20 6c 69 6e  * and then a lin
361f7 65 61 72 20 73 65 61 72 63 68 20 6f 66 20 61 6e  ear search of an
361f8 20 52 2d 54 72 65 65 20 6e 6f 64 65 2e 20 54 68   R-Tree node. Th
361f9 69 73 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 20  is should be .  
361fa 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65      ** considere
361fb 64 20 61 6c 6d 6f 73 74 20 61 73 20 71 75 69 63  d almost as quic
361fc 6b 20 61 73 20 61 20 64 69 72 65 63 74 20 72 6f  k as a direct ro
361fd 77 69 64 20 6c 6f 6f 6b 75 70 20 28 66 6f 72 20  wid lookup (for 
361fe 77 68 69 63 68 20 0a 20 20 20 20 20 20 2a 2a 20  which .      ** 
361ff 73 71 6c 69 74 65 20 75 73 65 73 20 61 6e 20 69  sqlite uses an i
36200 6e 74 65 72 6e 61 6c 20 63 6f 73 74 20 6f 66 20  nternal cost of 
36201 30 2e 30 29 2e 0a 20 20 20 20 20 20 2a 2f 20 0a  0.0)..      */ .
36202 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
36203 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20  estimatedCost = 
36204 31 30 2e 30 3b 0a 20 20 20 20 20 20 72 65 74 75  10.0;.      retu
36205 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
36206 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e    }..    if( p->
36207 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f  usable && p->iCo
36208 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20  lumn>0 ){.      
36209 75 38 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20 20  u8 op = 0;.     
3620a 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
3620b 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53  {.        case S
3620c 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
3620d 54 52 41 49 4e 54 5f 45 51 3a 20 6f 70 20 3d 20  TRAINT_EQ: op = 
3620e 52 54 52 45 45 5f 45 51 3b 20 62 72 65 61 6b 3b  RTREE_EQ; break;
3620f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51  .        case SQ
36210 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
36211 52 41 49 4e 54 5f 47 54 3a 20 6f 70 20 3d 20 52  RAINT_GT: op = R
36212 54 52 45 45 5f 47 54 3b 20 62 72 65 61 6b 3b 0a  TREE_GT; break;.
36213 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c          case SQL
36214 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
36215 41 49 4e 54 5f 4c 45 3a 20 6f 70 20 3d 20 52 54  AINT_LE: op = RT
36216 52 45 45 5f 4c 45 3b 20 62 72 65 61 6b 3b 0a 20  REE_LE; break;. 
36217 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49         case SQLI
36218 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
36219 49 4e 54 5f 4c 54 3a 20 6f 70 20 3d 20 52 54 52  INT_LT: op = RTR
3621a 45 45 5f 4c 54 3b 20 62 72 65 61 6b 3b 0a 20 20  EE_LT; break;.  
3621b 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
3621c 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
3621d 4e 54 5f 47 45 3a 20 6f 70 20 3d 20 52 54 52 45  NT_GE: op = RTRE
3621e 45 5f 47 45 3b 20 62 72 65 61 6b 3b 0a 20 20 20  E_GE; break;.   
3621f 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f     }.      if( o
36220 70 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  p ){.        /* 
36221 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 70  Make sure this p
36222 61 72 74 69 63 75 6c 61 72 20 63 6f 6e 73 74 72  articular constr
36223 61 69 6e 74 20 68 61 73 20 6e 6f 74 20 62 65 65  aint has not bee
36224 6e 20 75 73 65 64 20 62 65 66 6f 72 65 2e 0a 20  n used before.. 
36225 20 20 20 20 20 20 20 2a 2a 20 49 66 20 69 74 20         ** If it 
36226 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 65  has been used be
36227 66 6f 72 65 2c 20 69 67 6e 6f 72 65 20 69 74 2e  fore, ignore it.
36228 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
36229 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c      ** A <= or <
3622a 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 20   can be used if 
3622b 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f 72  there is a prior
3622c 20 3e 3d 20 6f 72 20 3e 2e 0a 20 20 20 20 20 20   >= or >..      
3622d 20 20 2a 2a 20 41 20 3e 3d 20 6f 72 20 3e 20 63    ** A >= or > c
3622e 61 6e 20 62 65 20 75 73 65 64 20 69 66 20 74 68  an be used if th
3622f 65 72 65 20 69 73 20 61 20 70 72 69 6f 72 20 3c  ere is a prior <
36230 20 6f 72 20 3c 3d 2e 0a 20 20 20 20 20 20 20 20   or <=..        
36231 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c 20 69 73 20  ** A <= or < is 
36232 64 69 73 71 75 61 6c 69 66 69 65 64 20 69 66 20  disqualified if 
36233 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f 72  there is a prior
36234 20 3c 3d 2c 20 3c 2c 20 6f 72 20 3d 3d 2e 0a 20   <=, <, or ==.. 
36235 20 20 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f         ** A >= o
36236 72 20 3e 20 69 73 20 64 69 73 71 75 61 6c 69 66  r > is disqualif
36237 69 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20  ied if there is 
36238 61 20 70 72 69 6f 72 20 3e 3d 2c 20 3e 2c 20 6f  a prior >=, >, o
36239 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20 20 2a 2a  r ==..        **
3623a 20 41 20 3d 3d 20 69 73 20 64 69 73 71 75 61 6c   A == is disqual
3623b 69 66 65 64 20 69 66 20 74 68 65 72 65 20 69 73  ifed if there is
3623c 20 61 6e 79 20 70 72 69 6f 72 20 63 6f 6e 73 74   any prior const
3623d 72 61 69 6e 74 2e 0a 20 20 20 20 20 20 20 20 2a  raint..        *
3623e 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 2c  /.        int j,
3623f 20 6f 70 6d 73 6b 3b 0a 20 20 20 20 20 20 20 20   opmsk;.        
36240 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
36241 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6d 70 61  igned char compa
36242 74 69 62 6c 65 5b 5d 20 3d 20 7b 20 30 2c 20 30  tible[] = { 0, 0
36243 2c 20 31 2c 20 31 2c 20 32 2c 20 32 20 7d 3b 0a  , 1, 1, 2, 2 };.
36244 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
36245 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45  compatible[RTREE
36246 5f 45 51 20 26 20 37 5d 3d 3d 30 20 29 3b 0a 20  _EQ & 7]==0 );. 
36247 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63         assert( c
36248 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f  ompatible[RTREE_
36249 4c 54 20 26 20 37 5d 3d 3d 31 20 29 3b 0a 20 20  LT & 7]==1 );.  
3624a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63 6f        assert( co
3624b 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 4c  mpatible[RTREE_L
3624c 45 20 26 20 37 5d 3d 3d 31 20 29 3b 0a 20 20 20  E & 7]==1 );.   
3624d 20 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6d       assert( com
3624e 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 47 54  patible[RTREE_GT
3624f 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20 20 20   & 7]==2 );.    
36250 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6d 70      assert( comp
36251 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 47 45 20  atible[RTREE_GE 
36252 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20 20 20 20  & 7]==2 );.     
36253 20 20 20 63 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f     cCol = p->iCo
36254 6c 75 6d 6e 20 2d 20 31 20 2b 20 27 61 27 3b 0a  lumn - 1 + 'a';.
36255 20 20 20 20 20 20 20 20 6f 70 6d 73 6b 20 3d 20          opmsk = 
36256 63 6f 6d 70 61 74 69 62 6c 65 5b 6f 70 20 26 20  compatible[op & 
36257 37 5d 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  7];.        for(
36258 6a 3d 30 3b 20 6a 3c 69 49 64 78 3b 20 6a 2b 3d  j=0; j<iIdx; j+=
36259 32 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  2){.          if
3625a 28 20 7a 49 64 78 53 74 72 5b 6a 2b 31 5d 3d 3d  ( zIdxStr[j+1]==
3625b 63 43 6f 6c 20 26 26 20 28 63 6f 6d 70 61 74 69  cCol && (compati
3625c 62 6c 65 5b 7a 49 64 78 53 74 72 5b 6a 5d 20 26  ble[zIdxStr[j] &
3625d 20 37 5d 20 26 20 6f 70 6d 73 6b 29 21 3d 30 20   7] & opmsk)!=0 
3625e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f  ){.            o
3625f 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  p = 0;.         
36260 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
36261 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
36262 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
36263 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20  ( op ){.        
36264 61 73 73 65 72 74 28 20 69 49 64 78 3c 73 69 7a  assert( iIdx<siz
36265 65 6f 66 28 7a 49 64 78 53 74 72 29 2d 31 20 29  eof(zIdxStr)-1 )
36266 3b 0a 20 20 20 20 20 20 20 20 7a 49 64 78 53 74  ;.        zIdxSt
36267 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 6f 70 3b 0a  r[iIdx++] = op;.
36268 20 20 20 20 20 20 20 20 7a 49 64 78 53 74 72 5b          zIdxStr[
36269 69 49 64 78 2b 2b 5d 20 3d 20 63 43 6f 6c 3b 0a  iIdx++] = cCol;.
3626a 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f          pIdxInfo
3626b 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
3626c 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e 64 65 78  ge[ii].argvIndex
3626d 20 3d 20 28 69 49 64 78 2f 32 29 3b 0a 20 20 20   = (iIdx/2);.   
3626e 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61       pIdxInfo->a
3626f 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
36270 69 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 20 20  ii].omit = 1;.  
36271 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
36272 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  .  pIdxInfo->idx
36273 4e 75 6d 20 3d 20 32 3b 0a 20 20 70 49 64 78 49  Num = 2;.  pIdxI
36274 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
36275 64 78 53 74 72 20 3d 20 31 3b 0a 20 20 69 66 28  dxStr = 1;.  if(
36276 20 69 49 64 78 3e 30 20 26 26 20 30 3d 3d 28 70   iIdx>0 && 0==(p
36277 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20  IdxInfo->idxStr 
36278 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
36279 66 28 22 25 73 22 2c 20 7a 49 64 78 53 74 72 29  f("%s", zIdxStr)
3627a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
3627b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
3627c 7d 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 78  }.  assert( iIdx
3627d 3e 3d 30 20 29 3b 0a 20 20 70 49 64 78 49 6e 66  >=0 );.  pIdxInf
3627e 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
3627f 20 3d 20 28 32 30 30 30 30 30 30 2e 30 20 2f 20   = (2000000.0 / 
36280 28 64 6f 75 62 6c 65 29 28 69 49 64 78 20 2b 20  (double)(iIdx + 
36281 31 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  1));.  return rc
36282 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
36283 6e 20 74 68 65 20 4e 2d 64 69 6d 65 6e 73 69 6f  n the N-dimensio
36284 6e 61 6c 20 76 6f 6c 75 6d 6e 20 6f 66 20 74 68  nal volumn of th
36285 65 20 63 65 6c 6c 20 73 74 6f 72 65 64 20 69 6e  e cell stored in
36286 20 2a 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66   *p..*/.static f
36287 6c 6f 61 74 20 63 65 6c 6c 41 72 65 61 28 52 74  loat cellArea(Rt
36288 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
36289 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c  eeCell *p){.  fl
3628a 6f 61 74 20 61 72 65 61 20 3d 20 31 2e 30 3b 0a  oat area = 1.0;.
3628b 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28    int ii;.  for(
3628c 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65  ii=0; ii<(pRtree
3628d 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32  ->nDim*2); ii+=2
3628e 29 7b 0a 20 20 20 20 61 72 65 61 20 3d 20 61 72  ){.    area = ar
3628f 65 61 20 2a 20 28 44 43 4f 4f 52 44 28 70 2d 3e  ea * (DCOORD(p->
36290 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20  aCoord[ii+1]) - 
36291 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
36292 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65  [ii]));.  }.  re
36293 74 75 72 6e 20 61 72 65 61 3b 0a 7d 0a 0a 2f 2a  turn area;.}../*
36294 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d  .** Return the m
36295 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 6f 66 20  argin length of 
36296 63 65 6c 6c 20 70 2e 20 54 68 65 20 6d 61 72 67  cell p. The marg
36297 69 6e 20 6c 65 6e 67 74 68 20 69 73 20 74 68 65  in length is the
36298 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 6f   sum.** of the o
36299 62 6a 65 63 74 73 20 73 69 7a 65 20 69 6e 20 65  bjects size in e
3629a 61 63 68 20 64 69 6d 65 6e 73 69 6f 6e 2e 0a 2a  ach dimension..*
3629b 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  /.static float c
3629c 65 6c 6c 4d 61 72 67 69 6e 28 52 74 72 65 65 20  ellMargin(Rtree 
3629d 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 43 65  *pRtree, RtreeCe
3629e 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c 6f 61 74 20  ll *p){.  float 
3629f 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20  margin = 0.0;.  
362a0 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69  int ii;.  for(ii
362a1 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e  =0; ii<(pRtree->
362a2 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b  nDim*2); ii+=2){
362a3 0a 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20 28  .    margin += (
362a4 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
362a5 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44  [ii+1]) - DCOORD
362a6 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29  (p->aCoord[ii]))
362a7 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d  ;.  }.  return m
362a8 61 72 67 69 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  argin;.}../*.** 
362a9 53 74 6f 72 65 20 74 68 65 20 75 6e 69 6f 6e 20  Store the union 
362aa 6f 66 20 63 65 6c 6c 73 20 70 31 20 61 6e 64 20  of cells p1 and 
362ab 70 32 20 69 6e 20 70 31 2e 0a 2a 2f 0a 73 74 61  p2 in p1..*/.sta
362ac 74 69 63 20 76 6f 69 64 20 63 65 6c 6c 55 6e 69  tic void cellUni
362ad 6f 6e 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  on(Rtree *pRtree
362ae 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c  , RtreeCell *p1,
362af 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b   RtreeCell *p2){
362b0 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 66 28  .  int ii;.  if(
362b1 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
362b2 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
362b3 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 66  _REAL32 ){.    f
362b4 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
362b5 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
362b6 2b 3d 32 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e  +=2){.      p1->
362b7 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 4d  aCoord[ii].f = M
362b8 49 4e 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69  IN(p1->aCoord[ii
362b9 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b  ].f, p2->aCoord[
362ba 69 69 5d 2e 66 29 3b 0a 20 20 20 20 20 20 70 31  ii].f);.      p1
362bb 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66  ->aCoord[ii+1].f
362bc 20 3d 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72   = MAX(p1->aCoor
362bd 64 5b 69 69 2b 31 5d 2e 66 2c 20 70 32 2d 3e 61  d[ii+1].f, p2->a
362be 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 29 3b 0a  Coord[ii+1].f);.
362bf 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
362c0 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
362c1 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29  (pRtree->nDim*2)
362c2 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20  ; ii+=2){.      
362c3 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  p1->aCoord[ii].i
362c4 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f 72   = MIN(p1->aCoor
362c5 64 5b 69 69 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f  d[ii].i, p2->aCo
362c6 6f 72 64 5b 69 69 5d 2e 69 29 3b 0a 20 20 20 20  ord[ii].i);.    
362c7 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b    p1->aCoord[ii+
362c8 31 5d 2e 69 20 3d 20 4d 41 58 28 70 31 2d 3e 61  1].i = MAX(p1->a
362c9 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 2c 20 70  Coord[ii+1].i, p
362ca 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e  2->aCoord[ii+1].
362cb 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  i);.    }.  }.}.
362cc 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
362cd 75 65 20 69 66 20 74 68 65 20 61 72 65 61 20 63  ue if the area c
362ce 6f 76 65 72 65 64 20 62 79 20 70 32 20 69 73 20  overed by p2 is 
362cf 61 20 73 75 62 73 65 74 20 6f 66 20 74 68 65 20  a subset of the 
362d0 61 72 65 61 20 63 6f 76 65 72 65 64 0a 2a 2a 20  area covered.** 
362d1 62 79 20 70 31 2e 20 46 61 6c 73 65 20 6f 74 68  by p1. False oth
362d2 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  erwise..*/.stati
362d3 63 20 69 6e 74 20 63 65 6c 6c 43 6f 6e 74 61 69  c int cellContai
362d4 6e 73 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ns(Rtree *pRtree
362d5 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c  , RtreeCell *p1,
362d6 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b   RtreeCell *p2){
362d7 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
362d8 20 69 73 49 6e 74 20 3d 20 28 70 52 74 72 65 65   isInt = (pRtree
362d9 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
362da 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 29  REE_COORD_INT32)
362db 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
362dc 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
362dd 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 52  ); ii+=2){.    R
362de 74 72 65 65 43 6f 6f 72 64 20 2a 61 31 20 3d 20  treeCoord *a1 = 
362df 26 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b  &p1->aCoord[ii];
362e0 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20  .    RtreeCoord 
362e1 2a 61 32 20 3d 20 26 70 32 2d 3e 61 43 6f 6f 72  *a2 = &p2->aCoor
362e2 64 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 28  d[ii];.    if( (
362e3 21 69 73 49 6e 74 20 26 26 20 28 61 32 5b 30 5d  !isInt && (a2[0]
362e4 2e 66 3c 61 31 5b 30 5d 2e 66 20 7c 7c 20 61 32  .f<a1[0].f || a2
362e5 5b 31 5d 2e 66 3e 61 31 5b 31 5d 2e 66 29 29 20  [1].f>a1[1].f)) 
362e6 0a 20 20 20 20 20 7c 7c 20 28 20 69 73 49 6e 74  .     || ( isInt
362e7 20 26 26 20 28 61 32 5b 30 5d 2e 69 3c 61 31 5b   && (a2[0].i<a1[
362e8 30 5d 2e 69 20 7c 7c 20 61 32 5b 31 5d 2e 69 3e  0].i || a2[1].i>
362e9 61 31 5b 31 5d 2e 69 29 29 20 0a 20 20 20 20 29  a1[1].i)) .    )
362ea 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
362eb 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
362ec 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
362ed 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
362ee 6e 74 20 63 65 6c 6c 20 70 20 77 6f 75 6c 64 20  nt cell p would 
362ef 67 72 6f 77 20 62 79 20 69 66 20 69 74 20 77 65  grow by if it we
362f0 72 65 20 75 6e 69 6f 6e 65 64 20 77 69 74 68 20  re unioned with 
362f1 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  pCell..*/.static
362f2 20 66 6c 6f 61 74 20 63 65 6c 6c 47 72 6f 77 74   float cellGrowt
362f3 68 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  h(Rtree *pRtree,
362f4 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 52   RtreeCell *p, R
362f5 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 29  treeCell *pCell)
362f6 7b 0a 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a  {.  float area;.
362f7 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
362f8 3b 0a 20 20 6d 65 6d 63 70 79 28 26 63 65 6c 6c  ;.  memcpy(&cell
362f9 2c 20 70 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  , p, sizeof(Rtre
362fa 65 43 65 6c 6c 29 29 3b 0a 20 20 61 72 65 61 20  eCell));.  area 
362fb 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65  = cellArea(pRtre
362fc 65 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 63 65 6c  e, &cell);.  cel
362fd 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
362fe 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  cell, pCell);.  
362ff 72 65 74 75 72 6e 20 28 63 65 6c 6c 41 72 65 61  return (cellArea
36300 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29 2d  (pRtree, &cell)-
36301 61 72 65 61 29 3b 0a 7d 0a 0a 23 69 66 20 56 41  area);.}..#if VA
36302 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
36303 43 48 4f 4f 53 45 53 55 42 54 52 45 45 20 7c 7c  CHOOSESUBTREE ||
36304 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
36305 45 45 5f 53 50 4c 49 54 0a 73 74 61 74 69 63 20  EE_SPLIT.static 
36306 66 6c 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61  float cellOverla
36307 70 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  p(.  Rtree *pRtr
36308 65 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  ee, .  RtreeCell
36309 20 2a 70 2c 20 0a 20 20 52 74 72 65 65 43 65 6c   *p, .  RtreeCel
3630a 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *aCell, .  int
3630b 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69   nCell, .  int i
3630c 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 69 6e 74  Exclude.){.  int
3630d 20 69 69 3b 0a 20 20 66 6c 6f 61 74 20 6f 76 65   ii;.  float ove
3630e 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20 66 6f  rlap = 0.0;.  fo
3630f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
36310 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ; ii++){.    if(
36311 20 69 69 21 3d 69 45 78 63 6c 75 64 65 20 29 7b   ii!=iExclude ){
36312 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20  .      int jj;. 
36313 20 20 20 20 20 66 6c 6f 61 74 20 6f 20 3d 20 31       float o = 1
36314 2e 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a  .0;.      for(jj
36315 3d 30 3b 20 6a 6a 3c 28 70 52 74 72 65 65 2d 3e  =0; jj<(pRtree->
36316 6e 44 69 6d 2a 32 29 3b 20 6a 6a 2b 3d 32 29 7b  nDim*2); jj+=2){
36317 0a 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20  .        double 
36318 78 31 3b 0a 20 20 20 20 20 20 20 20 64 6f 75 62  x1;.        doub
36319 6c 65 20 78 32 3b 0a 0a 20 20 20 20 20 20 20 20  le x2;..        
3631a 78 31 20 3d 20 4d 41 58 28 44 43 4f 4f 52 44 28  x1 = MAX(DCOORD(
3631b 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 2c 20  p->aCoord[jj]), 
3631c 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d  DCOORD(aCell[ii]
3631d 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 29 3b 0a 20  .aCoord[jj]));. 
3631e 20 20 20 20 20 20 20 78 32 20 3d 20 4d 49 4e 28         x2 = MIN(
3631f 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
36320 5b 6a 6a 2b 31 5d 29 2c 20 44 43 4f 4f 52 44 28  [jj+1]), DCOORD(
36321 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64  aCell[ii].aCoord
36322 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a 20 20 20 20 20  [jj+1]));..     
36323 20 20 20 69 66 28 20 78 32 3c 78 31 20 29 7b 0a     if( x2<x1 ){.
36324 20 20 20 20 20 20 20 20 20 20 6f 20 3d 20 30 2e            o = 0.
36325 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  0;.          bre
36326 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
36327 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 20 3d  e{.          o =
36328 20 6f 20 2a 20 28 78 32 2d 78 31 29 3b 0a 20 20   o * (x2-x1);.  
36329 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
3632a 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 2b 3d        overlap +=
3632b 20 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   o;.    }.  }.  
3632c 72 65 74 75 72 6e 20 6f 76 65 72 6c 61 70 3b 0a  return overlap;.
3632d 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41  }.#endif..#if VA
3632e 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
3632f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 73 74  CHOOSESUBTREE.st
36330 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f  atic float cellO
36331 76 65 72 6c 61 70 45 6e 6c 61 72 67 65 6d 65 6e  verlapEnlargemen
36332 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
36333 65 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  ee, .  RtreeCell
36334 20 2a 70 2c 20 0a 20 20 52 74 72 65 65 43 65 6c   *p, .  RtreeCel
36335 6c 20 2a 70 49 6e 73 65 72 74 2c 20 0a 20 20 52  l *pInsert, .  R
36336 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c  treeCell *aCell,
36337 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a   .  int nCell, .
36338 20 20 69 6e 74 20 69 45 78 63 6c 75 64 65 0a 29    int iExclude.)
36339 7b 0a 20 20 66 6c 6f 61 74 20 62 65 66 6f 72 65  {.  float before
3633a 3b 0a 20 20 66 6c 6f 61 74 20 61 66 74 65 72 3b  ;.  float after;
3633b 0a 20 20 62 65 66 6f 72 65 20 3d 20 63 65 6c 6c  .  before = cell
3633c 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20  Overlap(pRtree, 
3633d 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  p, aCell, nCell,
3633e 20 69 45 78 63 6c 75 64 65 29 3b 0a 20 20 63 65   iExclude);.  ce
3633f 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20  llUnion(pRtree, 
36340 70 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20 61  p, pInsert);.  a
36341 66 74 65 72 20 3d 20 63 65 6c 6c 4f 76 65 72 6c  fter = cellOverl
36342 61 70 28 70 52 74 72 65 65 2c 20 70 2c 20 61 43  ap(pRtree, p, aC
36343 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69 45 78 63  ell, nCell, iExc
36344 6c 75 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  lude);.  return 
36345 61 66 74 65 72 2d 62 65 66 6f 72 65 3b 0a 7d 0a  after-before;.}.
36346 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54  #endif.../*.** T
36347 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  his function imp
36348 6c 65 6d 65 6e 74 73 20 74 68 65 20 43 68 6f 6f  lements the Choo
36349 73 65 4c 65 61 66 20 61 6c 67 6f 72 69 74 68 6d  seLeaf algorithm
3634a 20 66 72 6f 6d 20 47 75 74 6d 61 6e 5b 38 34 5d   from Gutman[84]
3634b 2e 0a 2a 2a 20 43 68 6f 6f 73 65 53 75 62 54 72  ..** ChooseSubTr
3634c 65 65 20 69 6e 20 72 2a 74 72 65 65 20 74 65 72  ee in r*tree ter
3634d 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a 2f 0a 73 74 61  minology..*/.sta
3634e 74 69 63 20 69 6e 74 20 43 68 6f 6f 73 65 4c 65  tic int ChooseLe
3634f 61 66 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  af(.  Rtree *pRt
36350 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ree,            
36351 20 20 20 2f 2a 20 52 74 72 65 65 20 74 61 62 6c     /* Rtree tabl
36352 65 20 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c  e */.  RtreeCell
36353 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20   *pCell,        
36354 20 20 20 20 2f 2a 20 43 65 6c 6c 20 74 6f 20 69      /* Cell to i
36355 6e 73 65 72 74 20 69 6e 74 6f 20 72 74 72 65 65  nsert into rtree
36356 20 2a 2f 0a 20 20 69 6e 74 20 69 48 65 69 67 68   */.  int iHeigh
36357 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
36358 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20     /* Height of 
36359 73 75 62 2d 74 72 65 65 20 72 6f 6f 74 65 64 20  sub-tree rooted 
3635a 61 74 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 52 74  at pCell */.  Rt
3635b 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66  reeNode **ppLeaf
3635c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3635d 54 3a 20 53 65 6c 65 63 74 65 64 20 6c 65 61 66  T: Selected leaf
3635e 20 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   page */.){.  in
3635f 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  t rc;.  int ii;.
36360 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
36361 64 65 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41  de;.  rc = nodeA
36362 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 31  cquire(pRtree, 1
36363 2c 20 30 2c 20 26 70 4e 6f 64 65 29 3b 0a 0a 20  , 0, &pNode);.. 
36364 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53   for(ii=0; rc==S
36365 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28  QLITE_OK && ii<(
36366 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 69  pRtree->iDepth-i
36367 48 65 69 67 68 74 29 3b 20 69 69 2b 2b 29 7b 0a  Height); ii++){.
36368 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 3b 0a 20      int iCell;. 
36369 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
3636a 20 69 42 65 73 74 3b 0a 0a 20 20 20 20 66 6c 6f   iBest;..    flo
3636b 61 74 20 66 4d 69 6e 47 72 6f 77 74 68 3b 0a 20  at fMinGrowth;. 
3636c 20 20 20 66 6c 6f 61 74 20 66 4d 69 6e 41 72 65     float fMinAre
3636d 61 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69  a;.    float fMi
3636e 6e 4f 76 65 72 6c 61 70 3b 0a 0a 20 20 20 20 69  nOverlap;..    i
3636f 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  nt nCell = NCELL
36370 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74 72  (pNode);.    Rtr
36371 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20  eeCell cell;.   
36372 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69   RtreeNode *pChi
36373 6c 64 3b 0a 0a 20 20 20 20 52 74 72 65 65 43 65  ld;..    RtreeCe
36374 6c 6c 20 2a 61 43 65 6c 6c 20 3d 20 30 3b 0a 0a  ll *aCell = 0;..
36375 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
36376 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54  RTREE_CHOOSESUBT
36377 52 45 45 0a 20 20 20 20 69 66 28 20 69 69 3d 3d  REE.    if( ii==
36378 28 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d  (pRtree->iDepth-
36379 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  1) ){.      int 
3637a 6a 6a 3b 0a 20 20 20 20 20 20 61 43 65 6c 6c 20  jj;.      aCell 
3637b 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
3637c 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c  (sizeof(RtreeCel
3637d 6c 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 20 20 20  l)*nCell);.     
3637e 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20   if( !aCell ){. 
3637f 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
36380 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
36381 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
36382 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20  tree, pNode);.  
36383 20 20 20 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b        pNode = 0;
36384 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
36385 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
36386 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43   for(jj=0; jj<nC
36387 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  ell; jj++){.    
36388 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
36389 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 6a  pRtree, pNode, j
3638a 6a 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a  j, &aCell[jj]);.
3638b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
3638c 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53 65 6c  ndif..    /* Sel
3638d 65 63 74 20 74 68 65 20 63 68 69 6c 64 20 6e 6f  ect the child no
3638e 64 65 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65  de which will be
3638f 20 65 6e 6c 61 72 67 65 64 20 74 68 65 20 6c 65   enlarged the le
36390 61 73 74 20 69 66 20 70 43 65 6c 6c 0a 20 20 20  ast if pCell.   
36391 20 2a 2a 20 69 73 20 69 6e 73 65 72 74 65 64 20   ** is inserted 
36392 69 6e 74 6f 20 69 74 2e 20 52 65 73 6f 6c 76 65  into it. Resolve
36393 20 74 69 65 73 20 62 79 20 63 68 6f 6f 73 69 6e   ties by choosin
36394 67 20 74 68 65 20 65 6e 74 72 79 20 77 69 74 68  g the entry with
36395 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 6d 61 6c  .    ** the smal
36396 6c 65 73 74 20 61 72 65 61 2e 0a 20 20 20 20 2a  lest area..    *
36397 2f 0a 20 20 20 20 66 6f 72 28 69 43 65 6c 6c 3d  /.    for(iCell=
36398 30 3b 20 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20  0; iCell<nCell; 
36399 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20  iCell++){.      
3639a 66 6c 6f 61 74 20 67 72 6f 77 74 68 3b 0a 20 20  float growth;.  
3639b 20 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a      float area;.
3639c 20 20 20 20 20 20 66 6c 6f 61 74 20 6f 76 65 72        float over
3639d 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20  lap = 0.0;.     
3639e 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
3639f 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c  ree, pNode, iCel
363a0 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 20  l, &cell);.     
363a1 20 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c 47 72   growth = cellGr
363a2 6f 77 74 68 28 70 52 74 72 65 65 2c 20 26 63 65  owth(pRtree, &ce
363a3 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  ll, pCell);.    
363a4 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72 65    area = cellAre
363a5 61 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29  a(pRtree, &cell)
363a6 3b 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53  ;.#if VARIANT_RS
363a7 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55  TARTREE_CHOOSESU
363a8 42 54 52 45 45 0a 20 20 20 20 20 20 69 66 28 20  BTREE.      if( 
363a9 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44 65  ii==(pRtree->iDe
363aa 70 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  pth-1) ){.      
363ab 20 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c 6c    overlap = cell
363ac 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67 65 6d 65  OverlapEnlargeme
363ad 6e 74 28 70 52 74 72 65 65 2c 26 63 65 6c 6c 2c  nt(pRtree,&cell,
363ae 70 43 65 6c 6c 2c 61 43 65 6c 6c 2c 6e 43 65 6c  pCell,aCell,nCel
363af 6c 2c 69 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20  l,iCell);.      
363b0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
363b1 66 28 20 28 69 43 65 6c 6c 3d 3d 30 29 20 0a 20  f( (iCell==0) . 
363b2 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61        || (overla
363b3 70 3c 66 4d 69 6e 4f 76 65 72 6c 61 70 29 20 0a  p<fMinOverlap) .
363b4 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c         || (overl
363b5 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20  ap==fMinOverlap 
363b6 26 26 20 67 72 6f 77 74 68 3c 66 4d 69 6e 47 72  && growth<fMinGr
363b7 6f 77 74 68 29 0a 20 20 20 20 20 20 20 7c 7c 20  owth).       || 
363b8 28 6f 76 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76  (overlap==fMinOv
363b9 65 72 6c 61 70 20 26 26 20 67 72 6f 77 74 68 3d  erlap && growth=
363ba 3d 66 4d 69 6e 47 72 6f 77 74 68 20 26 26 20 61  =fMinGrowth && a
363bb 72 65 61 3c 66 4d 69 6e 41 72 65 61 29 0a 20 20  rea<fMinArea).  
363bc 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66      ){.        f
363bd 4d 69 6e 4f 76 65 72 6c 61 70 20 3d 20 6f 76 65  MinOverlap = ove
363be 72 6c 61 70 3b 0a 20 20 20 20 20 20 20 20 66 4d  rlap;.        fM
363bf 69 6e 47 72 6f 77 74 68 20 3d 20 67 72 6f 77 74  inGrowth = growt
363c0 68 3b 0a 20 20 20 20 20 20 20 20 66 4d 69 6e 41  h;.        fMinA
363c1 72 65 61 20 3d 20 61 72 65 61 3b 0a 20 20 20 20  rea = area;.    
363c2 20 20 20 20 69 42 65 73 74 20 3d 20 63 65 6c 6c      iBest = cell
363c3 2e 69 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 7d  .iRowid;.      }
363c4 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
363c5 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b  te3_free(aCell);
363c6 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63  .    rc = nodeAc
363c7 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69 42  quire(pRtree, iB
363c8 65 73 74 2c 20 70 4e 6f 64 65 2c 20 26 70 43 68  est, pNode, &pCh
363c9 69 6c 64 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ild);.    nodeRe
363ca 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e  lease(pRtree, pN
363cb 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 20  ode);.    pNode 
363cc 3d 20 70 43 68 69 6c 64 3b 0a 20 20 7d 0a 0a 20  = pChild;.  }.. 
363cd 20 2a 70 70 4c 65 61 66 20 3d 20 70 4e 6f 64 65   *ppLeaf = pNode
363ce 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
363cf 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 65 6c 6c 20 77  ../*.** A cell w
363d0 69 74 68 20 74 68 65 20 73 61 6d 65 20 63 6f 6e  ith the same con
363d1 74 65 6e 74 20 61 73 20 70 43 65 6c 6c 20 68 61  tent as pCell ha
363d2 73 20 6a 75 73 74 20 62 65 65 6e 20 69 6e 73 65  s just been inse
363d3 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  rted into.** the
363d4 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 54 68 69   node pNode. Thi
363d5 73 20 66 75 6e 63 74 69 6f 6e 20 75 70 64 61 74  s function updat
363d6 65 73 20 74 68 65 20 62 6f 75 6e 64 69 6e 67 20  es the bounding 
363d7 62 6f 78 20 63 65 6c 6c 73 20 69 6e 0a 2a 2a 20  box cells in.** 
363d8 61 6c 6c 20 61 6e 63 65 73 74 6f 72 20 65 6c 65  all ancestor ele
363d9 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
363da 20 76 6f 69 64 20 41 64 6a 75 73 74 54 72 65 65   void AdjustTree
363db 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
363dc 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
363dd 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65 20 74        /* Rtree t
363de 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65 65 4e  able */.  RtreeN
363df 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20 20 20 20  ode *pNode,     
363e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
363e1 64 6a 75 73 74 20 61 6e 63 65 73 74 72 79 20 6f  djust ancestry o
363e2 66 20 74 68 69 73 20 6e 6f 64 65 2e 20 2a 2f 0a  f this node. */.
363e3 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65    RtreeCell *pCe
363e4 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
363e5 20 20 20 20 2f 2a 20 54 68 69 73 20 63 65 6c 6c      /* This cell
363e6 20 77 61 73 20 6a 75 73 74 20 69 6e 73 65 72 74   was just insert
363e7 65 64 20 2a 2f 0a 29 7b 0a 20 20 52 74 72 65 65  ed */.){.  Rtree
363e8 4e 6f 64 65 20 2a 70 20 3d 20 70 4e 6f 64 65 3b  Node *p = pNode;
363e9 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 61  .  while( p->pPa
363ea 72 65 6e 74 20 29 7b 0a 20 20 20 20 52 74 72 65  rent ){.    Rtre
363eb 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
363ec 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65  RtreeNode *pPare
363ed 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b  nt = p->pParent;
363ee 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 20 3d  .    int iCell =
363ef 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78   nodeParentIndex
363f0 28 70 52 74 72 65 65 2c 20 70 29 3b 0a 0a 20 20  (pRtree, p);..  
363f1 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
363f2 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 69  tree, pParent, i
363f3 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
363f4 20 20 69 66 28 20 21 63 65 6c 6c 43 6f 6e 74 61    if( !cellConta
363f5 69 6e 73 28 70 52 74 72 65 65 2c 20 26 63 65 6c  ins(pRtree, &cel
363f6 6c 2c 20 70 43 65 6c 6c 29 20 29 7b 0a 20 20 20  l, pCell) ){.   
363f7 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74     cellUnion(pRt
363f8 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c  ree, &cell, pCel
363f9 6c 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 4f 76  l);.      nodeOv
363fa 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74 72  erwriteCell(pRtr
363fb 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 63 65  ee, pParent, &ce
363fc 6c 6c 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20 20  ll, iCell);.    
363fd 7d 0a 20 0a 20 20 20 20 70 20 3d 20 70 50 61 72  }. .    p = pPar
363fe 65 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ent;.  }.}../*.*
363ff 2a 20 57 72 69 74 65 20 6d 61 70 70 69 6e 67 20  * Write mapping 
36400 28 69 52 6f 77 69 64 2d 3e 69 4e 6f 64 65 29 20  (iRowid->iNode) 
36401 74 6f 20 74 68 65 20 3c 72 74 72 65 65 3e 5f 72  to the <rtree>_r
36402 6f 77 69 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  owid table..*/.s
36403 74 61 74 69 63 20 69 6e 74 20 72 6f 77 69 64 57  tatic int rowidW
36404 72 69 74 65 28 52 74 72 65 65 20 2a 70 52 74 72  rite(Rtree *pRtr
36405 65 65 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ee, sqlite3_int6
36406 34 20 69 52 6f 77 69 64 2c 20 73 71 6c 69 74 65  4 iRowid, sqlite
36407 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65 29 7b 0a  3_int64 iNode){.
36408 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
36409 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72  nt64(pRtree->pWr
3640a 69 74 65 52 6f 77 69 64 2c 20 31 2c 20 69 52 6f  iteRowid, 1, iRo
3640b 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  wid);.  sqlite3_
3640c 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
3640d 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20  e->pWriteRowid, 
3640e 32 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c  2, iNode);.  sql
3640f 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
36410 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
36411 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
36412 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
36413 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 7d 0a 0a  WriteRowid);.}..
36414 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70 70  /*.** Write mapp
36415 69 6e 67 20 28 69 4e 6f 64 65 2d 3e 69 50 61 72  ing (iNode->iPar
36416 29 20 74 6f 20 74 68 65 20 3c 72 74 72 65 65 3e  ) to the <rtree>
36417 5f 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 0a 2a  _parent table..*
36418 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
36419 65 6e 74 57 72 69 74 65 28 52 74 72 65 65 20 2a  entWrite(Rtree *
3641a 70 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33 5f  pRtree, sqlite3_
3641b 69 6e 74 36 34 20 69 4e 6f 64 65 2c 20 73 71 6c  int64 iNode, sql
3641c 69 74 65 33 5f 69 6e 74 36 34 20 69 50 61 72 29  ite3_int64 iPar)
3641d 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  {.  sqlite3_bind
3641e 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
3641f 57 72 69 74 65 50 61 72 65 6e 74 2c 20 31 2c 20  WriteParent, 1, 
36420 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65  iNode);.  sqlite
36421 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
36422 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e  ree->pWriteParen
36423 74 2c 20 32 2c 20 69 50 61 72 29 3b 0a 20 20 73  t, 2, iPar);.  s
36424 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
36425 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
36426 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
36427 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
36428 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74 29 3b  ->pWriteParent);
36429 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .}..static int r
3642a 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 52  treeInsertCell(R
3642b 74 72 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64  tree *, RtreeNod
3642c 65 20 2a 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  e *, RtreeCell *
3642d 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20 56 41 52  , int);..#if VAR
3642e 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
3642f 45 41 52 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20  EAR_SPLIT./*.** 
36430 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
36431 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76 61 72  f the linear var
36432 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
36433 4e 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  Next() function 
36434 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
36435 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52  84]..*/.static R
36436 74 72 65 65 43 65 6c 6c 20 2a 4c 69 6e 65 61 72  treeCell *Linear
36437 50 69 63 6b 4e 65 78 74 28 0a 20 20 52 74 72 65  PickNext(.  Rtre
36438 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
36439 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a  eeCell *aCell, .
3643a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20    int nCell, .  
3643b 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c 65 66 74  RtreeCell *pLeft
3643c 42 6f 78 2c 20 0a 20 20 52 74 72 65 65 43 65 6c  Box, .  RtreeCel
3643d 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c 0a 20 20  l *pRightBox,.  
3643e 69 6e 74 20 2a 61 69 55 73 65 64 0a 29 7b 0a 20  int *aiUsed.){. 
3643f 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
36440 69 3d 30 3b 20 61 69 55 73 65 64 5b 69 69 5d 3b  i=0; aiUsed[ii];
36441 20 69 69 2b 2b 29 3b 0a 20 20 61 69 55 73 65 64   ii++);.  aiUsed
36442 5b 69 69 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75  [ii] = 1;.  retu
36443 72 6e 20 26 61 43 65 6c 6c 5b 69 69 5d 3b 0a 7d  rn &aCell[ii];.}
36444 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
36445 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  tation of the li
36446 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f 66 20  near variant of 
36447 74 68 65 20 50 69 63 6b 53 65 65 64 73 28 29 20  the PickSeeds() 
36448 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
36449 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
3644a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 4c 69 6e  .static void Lin
3644b 65 61 72 50 69 63 6b 53 65 65 64 73 28 0a 20 20  earPickSeeds(.  
3644c 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20  Rtree *pRtree,. 
3644d 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
3644e 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c  l, .  int nCell,
3644f 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66 74 53   .  int *piLeftS
36450 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a 70 69 52  eed, .  int *piR
36451 69 67 68 74 53 65 65 64 0a 29 7b 0a 20 20 69 6e  ightSeed.){.  in
36452 74 20 69 3b 0a 20 20 69 6e 74 20 69 4c 65 66 74  t i;.  int iLeft
36453 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Seed = 0;.  int 
36454 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a  iRightSeed = 1;.
36455 20 20 66 6c 6f 61 74 20 6d 61 78 4e 6f 72 6d 61    float maxNorma
36456 6c 49 6e 6e 65 72 57 69 64 74 68 20 3d 20 30 2e  lInnerWidth = 0.
36457 30 3b 0a 0a 20 20 2f 2a 20 50 69 63 6b 20 74 77  0;..  /* Pick tw
36458 6f 20 22 73 65 65 64 22 20 63 65 6c 6c 73 20 66  o "seed" cells f
36459 72 6f 6d 20 74 68 65 20 61 72 72 61 79 20 6f 66  rom the array of
3645a 20 63 65 6c 6c 73 2e 20 54 68 65 20 61 6c 67 6f   cells. The algo
3645b 72 69 74 68 6d 20 75 73 65 64 0a 20 20 2a 2a 20  rithm used.  ** 
3645c 68 65 72 65 20 69 73 20 74 68 65 20 4c 69 6e 65  here is the Line
3645d 61 72 50 69 63 6b 53 65 65 64 73 20 61 6c 67 6f  arPickSeeds algo
3645e 72 69 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61  rithm from Gutma
3645f 6e 5b 31 39 38 34 5d 2e 20 54 68 65 20 0a 20 20  n[1984]. The .  
36460 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68  ** indices of th
36461 65 20 74 77 6f 20 73 65 65 64 20 63 65 6c 6c 73  e two seed cells
36462 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 61 72   in the array ar
36463 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 63 61  e stored in loca
36464 6c 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73  l.  ** variables
36465 20 69 4c 65 66 74 53 65 65 6b 20 61 6e 64 20 69   iLeftSeek and i
36466 52 69 67 68 74 53 65 65 64 2e 0a 20 20 2a 2f 0a  RightSeed..  */.
36467 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 52 74    for(i=0; i<pRt
36468 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 2b 2b 29 7b  ree->nDim; i++){
36469 0a 20 20 20 20 66 6c 6f 61 74 20 78 31 20 3d 20  .    float x1 = 
3646a 61 43 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64 5b  aCell[0].aCoord[
3646b 69 2a 32 5d 3b 0a 20 20 20 20 66 6c 6f 61 74 20  i*2];.    float 
3646c 78 32 20 3d 20 61 43 65 6c 6c 5b 30 5d 2e 61 43  x2 = aCell[0].aC
3646d 6f 6f 72 64 5b 69 2a 32 2b 31 5d 3b 0a 20 20 20  oord[i*2+1];.   
3646e 20 66 6c 6f 61 74 20 78 33 20 3d 20 78 31 3b 0a   float x3 = x1;.
3646f 20 20 20 20 66 6c 6f 61 74 20 78 34 20 3d 20 78      float x4 = x
36470 32 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a  2;.    int jj;..
36471 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 4c 65 66      int iCellLef
36472 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 69  t = 0;.    int i
36473 43 65 6c 6c 52 69 67 68 74 20 3d 20 30 3b 0a 0a  CellRight = 0;..
36474 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a      for(jj=1; jj
36475 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20  <nCell; jj++){. 
36476 20 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20       float left 
36477 3d 20 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f  = aCell[jj].aCoo
36478 72 64 5b 69 2a 32 5d 3b 0a 20 20 20 20 20 20 66  rd[i*2];.      f
36479 6c 6f 61 74 20 72 69 67 68 74 20 3d 20 61 43 65  loat right = aCe
3647a 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a  ll[jj].aCoord[i*
3647b 32 2b 31 5d 3b 0a 0a 20 20 20 20 20 20 69 66 28  2+1];..      if(
3647c 20 6c 65 66 74 3c 78 31 20 29 20 78 31 20 3d 20   left<x1 ) x1 = 
3647d 6c 65 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20  left;.      if( 
3647e 72 69 67 68 74 3e 78 34 20 29 20 78 34 20 3d 20  right>x4 ) x4 = 
3647f 72 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28  right;.      if(
36480 20 6c 65 66 74 3e 78 33 20 29 7b 0a 20 20 20 20   left>x3 ){.    
36481 20 20 20 20 78 33 20 3d 20 6c 65 66 74 3b 0a 20      x3 = left;. 
36482 20 20 20 20 20 20 20 69 43 65 6c 6c 52 69 67 68         iCellRigh
36483 74 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a  t = jj;.      }.
36484 20 20 20 20 20 20 69 66 28 20 72 69 67 68 74 3c        if( right<
36485 78 32 20 29 7b 0a 20 20 20 20 20 20 20 20 78 32  x2 ){.        x2
36486 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20   = right;.      
36487 20 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 6a 6a    iCellLeft = jj
36488 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
36489 0a 20 20 20 20 69 66 28 20 78 34 21 3d 78 31 20  .    if( x4!=x1 
3648a 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6e  ){.      float n
3648b 6f 72 6d 61 6c 77 69 64 74 68 20 3d 20 28 78 33  ormalwidth = (x3
3648c 20 2d 20 78 32 29 20 2f 20 28 78 34 20 2d 20 78   - x2) / (x4 - x
3648d 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f  1);.      if( no
3648e 72 6d 61 6c 77 69 64 74 68 3e 6d 61 78 4e 6f 72  rmalwidth>maxNor
3648f 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68 20 29 7b  malInnerWidth ){
36490 0a 20 20 20 20 20 20 20 20 69 4c 65 66 74 53 65  .        iLeftSe
36491 65 64 20 3d 20 69 43 65 6c 6c 4c 65 66 74 3b 0a  ed = iCellLeft;.
36492 20 20 20 20 20 20 20 20 69 52 69 67 68 74 53 65          iRightSe
36493 65 64 20 3d 20 69 43 65 6c 6c 52 69 67 68 74 3b  ed = iCellRight;
36494 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
36495 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65 65   }..  *piLeftSee
36496 64 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a 20  d = iLeftSeed;. 
36497 20 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d 20   *piRightSeed = 
36498 69 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23 65  iRightSeed;.}.#e
36499 6e 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54 5f  ndif /* VARIANT_
3649a 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53  GUTTMAN_LINEAR_S
3649b 50 4c 49 54 20 2a 2f 0a 0a 23 69 66 20 56 41 52  PLIT */..#if VAR
3649c 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
3649d 44 52 41 54 49 43 5f 53 50 4c 49 54 0a 2f 2a 0a  DRATIC_SPLIT./*.
3649e 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
3649f 6e 20 6f 66 20 74 68 65 20 71 75 61 64 72 61 74  n of the quadrat
364a0 69 63 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68  ic variant of th
364a1 65 20 50 69 63 6b 4e 65 78 74 28 29 20 66 75 6e  e PickNext() fun
364a2 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75  ction from.** Gu
364a3 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74  ttman[84]..*/.st
364a4 61 74 69 63 20 52 74 72 65 65 43 65 6c 6c 20 2a  atic RtreeCell *
364a5 51 75 61 64 72 61 74 69 63 50 69 63 6b 4e 65 78  QuadraticPickNex
364a6 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
364a7 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
364a8 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
364a9 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43 65  Cell, .  RtreeCe
364aa 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a 20  ll *pLeftBox, . 
364ab 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69 67   RtreeCell *pRig
364ac 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61 69  htBox,.  int *ai
364ad 55 73 65 64 0a 29 7b 0a 20 20 23 64 65 66 69 6e  Used.){.  #defin
364ae 65 20 46 41 42 53 28 61 29 20 28 28 61 29 3c 30  e FABS(a) ((a)<0
364af 2e 30 3f 2d 31 2e 30 2a 28 61 29 3a 28 61 29 29  .0?-1.0*(a):(a))
364b0 0a 0a 20 20 69 6e 74 20 69 53 65 6c 65 63 74 20  ..  int iSelect 
364b1 3d 20 2d 31 3b 0a 20 20 66 6c 6f 61 74 20 66 44  = -1;.  float fD
364b2 69 66 66 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  iff;.  int ii;. 
364b3 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43   for(ii=0; ii<nC
364b4 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ell; ii++){.    
364b5 69 66 28 20 61 69 55 73 65 64 5b 69 69 5d 3d 3d  if( aiUsed[ii]==
364b6 30 20 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74  0 ){.      float
364b7 20 6c 65 66 74 20 3d 20 63 65 6c 6c 47 72 6f 77   left = cellGrow
364b8 74 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  th(pRtree, pLeft
364b9 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29  Box, &aCell[ii])
364ba 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69  ;.      float ri
364bb 67 68 74 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68  ght = cellGrowth
364bc 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 42 6f  (pRtree, pLeftBo
364bd 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a  x, &aCell[ii]);.
364be 20 20 20 20 20 20 66 6c 6f 61 74 20 64 69 66 66        float diff
364bf 20 3d 20 46 41 42 53 28 72 69 67 68 74 2d 6c 65   = FABS(right-le
364c0 66 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  ft);.      if( i
364c1 53 65 6c 65 63 74 3c 30 20 7c 7c 20 64 69 66 66  Select<0 || diff
364c2 3e 66 44 69 66 66 20 29 7b 0a 20 20 20 20 20 20  >fDiff ){.      
364c3 20 20 66 44 69 66 66 20 3d 20 64 69 66 66 3b 0a    fDiff = diff;.
364c4 20 20 20 20 20 20 20 20 69 53 65 6c 65 63 74 20          iSelect 
364c5 3d 20 69 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = ii;.      }.  
364c6 20 20 7d 0a 20 20 7d 0a 20 20 61 69 55 73 65 64    }.  }.  aiUsed
364c7 5b 69 53 65 6c 65 63 74 5d 20 3d 20 31 3b 0a 20  [iSelect] = 1;. 
364c8 20 72 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69   return &aCell[i
364c9 53 65 6c 65 63 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  Select];.}../*.*
364ca 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
364cb 20 6f 66 20 74 68 65 20 71 75 61 64 72 61 74 69   of the quadrati
364cc 63 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65  c variant of the
364cd 20 50 69 63 6b 53 65 65 64 73 28 29 20 66 75 6e   PickSeeds() fun
364ce 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75  ction from.** Gu
364cf 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74  ttman[84]..*/.st
364d0 61 74 69 63 20 76 6f 69 64 20 51 75 61 64 72 61  atic void Quadra
364d1 74 69 63 50 69 63 6b 53 65 65 64 73 28 0a 20 20  ticPickSeeds(.  
364d2 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20  Rtree *pRtree,. 
364d3 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
364d4 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c  l, .  int nCell,
364d5 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66 74 53   .  int *piLeftS
364d6 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a 70 69 52  eed, .  int *piR
364d7 69 67 68 74 53 65 65 64 0a 29 7b 0a 20 20 69 6e  ightSeed.){.  in
364d8 74 20 69 69 3b 0a 20 20 69 6e 74 20 6a 6a 3b 0a  t ii;.  int jj;.
364d9 0a 20 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64  .  int iLeftSeed
364da 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67   = 0;.  int iRig
364db 68 74 53 65 65 64 20 3d 20 31 3b 0a 20 20 66 6c  htSeed = 1;.  fl
364dc 6f 61 74 20 66 57 61 73 74 65 20 3d 20 30 2e 30  oat fWaste = 0.0
364dd 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
364de 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
364df 20 20 20 20 66 6f 72 28 6a 6a 3d 69 69 2b 31 3b      for(jj=ii+1;
364e0 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29   jj<nCell; jj++)
364e1 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69  {.      float ri
364e2 67 68 74 20 3d 20 63 65 6c 6c 41 72 65 61 28 70  ght = cellArea(p
364e3 52 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b 6a 6a  Rtree, &aCell[jj
364e4 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  ]);.      float 
364e5 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c 47 72 6f  growth = cellGro
364e6 77 74 68 28 70 52 74 72 65 65 2c 20 26 61 43 65  wth(pRtree, &aCe
364e7 6c 6c 5b 69 69 5d 2c 20 26 61 43 65 6c 6c 5b 6a  ll[ii], &aCell[j
364e8 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  j]);.      float
364e9 20 77 61 73 74 65 20 3d 20 67 72 6f 77 74 68 20   waste = growth 
364ea 2d 20 72 69 67 68 74 3b 0a 0a 20 20 20 20 20 20  - right;..      
364eb 69 66 28 20 77 61 73 74 65 3e 66 57 61 73 74 65  if( waste>fWaste
364ec 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 65 66   ){.        iLef
364ed 74 53 65 65 64 20 3d 20 69 69 3b 0a 20 20 20 20  tSeed = ii;.    
364ee 20 20 20 20 69 52 69 67 68 74 53 65 65 64 20 3d      iRightSeed =
364ef 20 6a 6a 3b 0a 20 20 20 20 20 20 20 20 66 57 61   jj;.        fWa
364f0 73 74 65 20 3d 20 77 61 73 74 65 3b 0a 20 20 20  ste = waste;.   
364f1 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
364f2 20 20 2a 70 69 4c 65 66 74 53 65 65 64 20 3d 20    *piLeftSeed = 
364f3 69 4c 65 66 74 53 65 65 64 3b 0a 20 20 2a 70 69  iLeftSeed;.  *pi
364f4 52 69 67 68 74 53 65 65 64 20 3d 20 69 52 69 67  RightSeed = iRig
364f5 68 74 53 65 65 64 3b 0a 7d 0a 23 65 6e 64 69 66  htSeed;.}.#endif
364f6 20 2f 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54   /* VARIANT_GUTT
364f7 4d 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50  MAN_QUADRATIC_SP
364f8 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 72  LIT */../*.** Ar
364f9 67 75 6d 65 6e 74 73 20 61 49 64 78 2c 20 61 44  guments aIdx, aD
364fa 69 73 74 61 6e 63 65 20 61 6e 64 20 61 53 70 61  istance and aSpa
364fb 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20  re all point to 
364fc 61 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0a 2a  arrays of size.*
364fd 2a 20 6e 49 64 78 2e 20 54 68 65 20 61 49 64 78  * nIdx. The aIdx
364fe 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
364ff 74 68 65 20 73 65 74 20 6f 66 20 69 6e 74 65 67  the set of integ
36500 65 72 73 20 66 72 6f 6d 20 30 20 74 6f 20 0a 2a  ers from 0 to .*
36501 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e 20 6e 6f  * (nIdx-1) in no
36502 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
36503 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
36504 20 73 6f 72 74 73 20 74 68 65 20 76 61 6c 75 65   sorts the value
36505 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20 61 63 63  s.** in aIdx acc
36506 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 69 6e  ording to the in
36507 64 65 78 65 64 20 76 61 6c 75 65 73 20 69 6e 20  dexed values in 
36508 61 44 69 73 74 61 6e 63 65 2e 20 46 6f 72 0a 2a  aDistance. For.*
36509 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d  * example, assum
3650a 69 6e 67 20 74 68 65 20 69 6e 70 75 74 73 3a 0a  ing the inputs:.
3650b 2a 2a 0a 2a 2a 20 20 20 61 49 64 78 20 20 20 20  **.**   aIdx    
3650c 20 20 3d 20 7b 20 30 2c 20 20 20 31 2c 20 20 20    = { 0,   1,   
3650d 32 2c 20 20 20 33 20 7d 0a 2a 2a 20 20 20 61 44  2,   3 }.**   aD
3650e 69 73 74 61 6e 63 65 20 3d 20 7b 20 35 2e 30 2c  istance = { 5.0,
3650f 20 32 2e 30 2c 20 37 2e 30 2c 20 36 2e 30 20 7d   2.0, 7.0, 6.0 }
36510 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  .**.** this func
36511 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 61 49  tion sets the aI
36512 64 78 20 61 72 72 61 79 20 74 6f 20 63 6f 6e 74  dx array to cont
36513 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64  ain:.**.**   aId
36514 78 20 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20  x      = { 0,   
36515 31 2c 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a  1,   2,   3 }.**
36516 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20 61  .** The aSpare a
36517 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73 20  rray is used as 
36518 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69 6e  temporary workin
36519 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0a 2a  g space by the.*
3651a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72 69  * sorting algori
3651b 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  thm..*/.static v
3651c 6f 69 64 20 53 6f 72 74 42 79 44 69 73 74 61 6e  oid SortByDistan
3651d 63 65 28 0a 20 20 69 6e 74 20 2a 61 49 64 78 2c  ce(.  int *aIdx,
3651e 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c 20 0a 20   .  int nIdx, . 
3651f 20 66 6c 6f 61 74 20 2a 61 44 69 73 74 61 6e 63   float *aDistanc
36520 65 2c 20 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  e, .  int *aSpar
36521 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49 64 78 3e  e.){.  if( nIdx>
36522 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4c 65  1 ){.    int iLe
36523 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  ft = 0;.    int 
36524 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 20  iRight = 0;..   
36525 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20 6e 49 64   int nLeft = nId
36526 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20 6e 52 69  x/2;.    int nRi
36527 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66 74  ght = nIdx-nLeft
36528 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66 74  ;.    int *aLeft
36529 20 3d 20 61 49 64 78 3b 0a 20 20 20 20 69 6e 74   = aIdx;.    int
3652a 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64 78   *aRight = &aIdx
3652b 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20 20 53 6f  [nLeft];..    So
3652c 72 74 42 79 44 69 73 74 61 6e 63 65 28 61 4c 65  rtByDistance(aLe
3652d 66 74 2c 20 6e 4c 65 66 74 2c 20 61 44 69 73 74  ft, nLeft, aDist
3652e 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a 20  ance, aSpare);. 
3652f 20 20 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63     SortByDistanc
36530 65 28 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74  e(aRight, nRight
36531 2c 20 61 44 69 73 74 61 6e 63 65 2c 20 61 53 70  , aDistance, aSp
36532 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  are);..    memcp
36533 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66 74 2c  y(aSpare, aLeft,
36534 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65   sizeof(int)*nLe
36535 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d  ft);.    aLeft =
36536 20 61 53 70 61 72 65 3b 0a 0a 20 20 20 20 77 68   aSpare;..    wh
36537 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66 74  ile( iLeft<nLeft
36538 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67 68   || iRight<nRigh
36539 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  t ){.      if( i
3653a 4c 65 66 74 3d 3d 6e 4c 65 66 74 20 29 7b 0a 20  Left==nLeft ){. 
3653b 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66         aIdx[iLef
3653c 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52 69 67  t+iRight] = aRig
3653d 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20 20  ht[iRight];.    
3653e 20 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20      iRight++;.  
3653f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 52      }else if( iR
36540 69 67 68 74 3d 3d 6e 52 69 67 68 74 20 29 7b 0a  ight==nRight ){.
36541 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65          aIdx[iLe
36542 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 4c 65  ft+iRight] = aLe
36543 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20  ft[iLeft];.     
36544 20 20 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20     iLeft++;.    
36545 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
36546 20 66 6c 6f 61 74 20 66 4c 65 66 74 20 3d 20 61   float fLeft = a
36547 44 69 73 74 61 6e 63 65 5b 61 4c 65 66 74 5b 69  Distance[aLeft[i
36548 4c 65 66 74 5d 5d 3b 0a 20 20 20 20 20 20 20 20  Left]];.        
36549 66 6c 6f 61 74 20 66 52 69 67 68 74 20 3d 20 61  float fRight = a
3654a 44 69 73 74 61 6e 63 65 5b 61 52 69 67 68 74 5b  Distance[aRight[
3654b 69 52 69 67 68 74 5d 5d 3b 0a 20 20 20 20 20 20  iRight]];.      
3654c 20 20 69 66 28 20 66 4c 65 66 74 3c 66 52 69 67    if( fLeft<fRig
3654d 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ht ){.          
3654e 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68  aIdx[iLeft+iRigh
3654f 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74  t] = aLeft[iLeft
36550 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 4c 65  ];.          iLe
36551 66 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65  ft++;.        }e
36552 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61  lse{.          a
36553 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
36554 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68  ] = aRight[iRigh
36555 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 52  t];.          iR
36556 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ight++;.        
36557 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
36558 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 43 68  .#if 0.    /* Ch
36559 65 63 6b 20 74 68 61 74 20 74 68 65 20 73 6f 72  eck that the sor
3655a 74 20 77 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20  t worked */.    
3655b 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  {.      int jj;.
3655c 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20        for(jj=1; 
3655d 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a  jj<nIdx; jj++){.
3655e 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 6c 65          float le
3655f 66 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61  ft = aDistance[a
36560 49 64 78 5b 6a 6a 2d 31 5d 5d 3b 0a 20 20 20 20  Idx[jj-1]];.    
36561 20 20 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20      float right 
36562 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 49 64 78  = aDistance[aIdx
36563 5b 6a 6a 5d 5d 3b 0a 20 20 20 20 20 20 20 20 61  [jj]];.        a
36564 73 73 65 72 74 28 20 6c 65 66 74 3c 3d 72 69 67  ssert( left<=rig
36565 68 74 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ht );.      }.  
36566 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
36567 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
36568 73 20 61 49 64 78 2c 20 61 43 65 6c 6c 20 61 6e  s aIdx, aCell an
36569 64 20 61 53 70 61 72 65 20 61 6c 6c 20 70 6f 69  d aSpare all poi
3656a 6e 74 20 74 6f 20 61 72 72 61 79 73 20 6f 66 20  nt to arrays of 
3656b 73 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68  size.** nIdx. Th
3656c 65 20 61 49 64 78 20 61 72 72 61 79 20 63 6f 6e  e aIdx array con
3656d 74 61 69 6e 73 20 74 68 65 20 73 65 74 20 6f 66  tains the set of
3656e 20 69 6e 74 65 67 65 72 73 20 66 72 6f 6d 20 30   integers from 0
3656f 20 74 6f 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29   to .** (nIdx-1)
36570 20 69 6e 20 6e 6f 20 70 61 72 74 69 63 75 6c 61   in no particula
36571 72 20 6f 72 64 65 72 2e 20 54 68 69 73 20 66 75  r order. This fu
36572 6e 63 74 69 6f 6e 20 73 6f 72 74 73 20 74 68 65  nction sorts the
36573 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49   values.** in aI
36574 64 78 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  dx according to 
36575 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20 6f  dimension iDim o
36576 66 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 61  f the cells in a
36577 43 65 6c 6c 2e 20 54 68 65 0a 2a 2a 20 6d 69 6e  Cell. The.** min
36578 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 64 69  imum value of di
36579 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20 69 73 20  mension iDim is 
3657a 63 6f 6e 73 69 64 65 72 65 64 20 66 69 72 73 74  considered first
3657b 2c 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d  , the.** maximum
3657c 20 75 73 65 64 20 74 6f 20 62 72 65 61 6b 20 74   used to break t
3657d 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ies..**.** The a
3657e 53 70 61 72 65 20 61 72 72 61 79 20 69 73 20 75  Spare array is u
3657f 73 65 64 20 61 73 20 74 65 6d 70 6f 72 61 72 79  sed as temporary
36580 20 77 6f 72 6b 69 6e 67 20 73 70 61 63 65 20 62   working space b
36581 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74 69 6e 67  y the.** sorting
36582 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73   algorithm..*/.s
36583 74 61 74 69 63 20 76 6f 69 64 20 53 6f 72 74 42  tatic void SortB
36584 79 44 69 6d 65 6e 73 69 6f 6e 28 0a 20 20 52 74  yDimension(.  Rt
36585 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 69  ree *pRtree,.  i
36586 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e 74  nt *aIdx, .  int
36587 20 6e 49 64 78 2c 20 0a 20 20 69 6e 74 20 69 44   nIdx, .  int iD
36588 69 6d 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  im, .  RtreeCell
36589 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *aCell, .  int 
3658a 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20 69 66 28  *aSpare.){.  if(
3658b 20 6e 49 64 78 3e 31 20 29 7b 0a 0a 20 20 20 20   nIdx>1 ){..    
3658c 69 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b 0a 20  int iLeft = 0;. 
3658d 20 20 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20     int iRight = 
3658e 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66  0;..    int nLef
3658f 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20 20 20 20  t = nIdx/2;.    
36590 69 6e 74 20 6e 52 69 67 68 74 20 3d 20 6e 49 64  int nRight = nId
36591 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74  x-nLeft;.    int
36592 20 2a 61 4c 65 66 74 20 3d 20 61 49 64 78 3b 0a   *aLeft = aIdx;.
36593 20 20 20 20 69 6e 74 20 2a 61 52 69 67 68 74 20      int *aRight 
36594 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a  = &aIdx[nLeft];.
36595 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65 6e  .    SortByDimen
36596 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 4c 65  sion(pRtree, aLe
36597 66 74 2c 20 6e 4c 65 66 74 2c 20 69 44 69 6d 2c  ft, nLeft, iDim,
36598 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29 3b   aCell, aSpare);
36599 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65 6e  .    SortByDimen
3659a 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 52 69  sion(pRtree, aRi
3659b 67 68 74 2c 20 6e 52 69 67 68 74 2c 20 69 44 69  ght, nRight, iDi
3659c 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65  m, aCell, aSpare
3659d 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 61  );..    memcpy(a
3659e 53 70 61 72 65 2c 20 61 4c 65 66 74 2c 20 73 69  Spare, aLeft, si
3659f 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65 66 74 29  zeof(int)*nLeft)
365a0 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d 20 61 53  ;.    aLeft = aS
365a1 70 61 72 65 3b 0a 20 20 20 20 77 68 69 6c 65 28  pare;.    while(
365a2 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20   iLeft<nLeft || 
365a3 69 52 69 67 68 74 3c 6e 52 69 67 68 74 20 29 7b  iRight<nRight ){
365a4 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 6c  .      double xl
365a5 65 66 74 31 20 3d 20 44 43 4f 4f 52 44 28 61 43  eft1 = DCOORD(aC
365a6 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d  ell[aLeft[iLeft]
365a7 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d  ].aCoord[iDim*2]
365a8 29 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  );.      double 
365a9 78 6c 65 66 74 32 20 3d 20 44 43 4f 4f 52 44 28  xleft2 = DCOORD(
365aa 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65 66  aCell[aLeft[iLef
365ab 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a  t]].aCoord[iDim*
365ac 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75  2+1]);.      dou
365ad 62 6c 65 20 78 72 69 67 68 74 31 20 3d 20 44 43  ble xright1 = DC
365ae 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68  OORD(aCell[aRigh
365af 74 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72  t[iRight]].aCoor
365b0 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20  d[iDim*2]);.    
365b1 20 20 64 6f 75 62 6c 65 20 78 72 69 67 68 74 32    double xright2
365b2 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
365b3 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e  aRight[iRight]].
365b4 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
365b5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 4c  );.      if( (iL
365b6 65 66 74 21 3d 6e 4c 65 66 74 29 20 26 26 20 28  eft!=nLeft) && (
365b7 28 69 52 69 67 68 74 3d 3d 6e 52 69 67 68 74 29  (iRight==nRight)
365b8 0a 20 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66  .       || (xlef
365b9 74 31 3c 78 72 69 67 68 74 31 29 0a 20 20 20 20  t1<xright1).    
365ba 20 20 20 7c 7c 20 28 78 6c 65 66 74 31 3d 3d 78     || (xleft1==x
365bb 72 69 67 68 74 31 20 26 26 20 78 6c 65 66 74 32  right1 && xleft2
365bc 3c 78 72 69 67 68 74 32 29 0a 20 20 20 20 20 20  <xright2).      
365bd 29 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78  )){.        aIdx
365be 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d  [iLeft+iRight] =
365bf 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20   aLeft[iLeft];. 
365c0 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a         iLeft++;.
365c1 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
365c2 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
365c3 69 52 69 67 68 74 5d 20 3d 20 61 52 69 67 68 74  iRight] = aRight
365c4 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20 20 20 20  [iRight];.      
365c5 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20 20    iRight++;.    
365c6 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30    }.    }..#if 0
365c7 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
365c8 61 74 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b  at the sort work
365c9 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20  ed */.    {.    
365ca 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
365cb 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64  for(jj=1; jj<nId
365cc 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  x; jj++){.      
365cd 20 20 66 6c 6f 61 74 20 78 6c 65 66 74 31 20 3d    float xleft1 =
365ce 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31   aCell[aIdx[jj-1
365cf 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
365d0 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ];.        float
365d1 20 78 6c 65 66 74 32 20 3d 20 61 43 65 6c 6c 5b   xleft2 = aCell[
365d2 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f 6f  aIdx[jj-1]].aCoo
365d3 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b 0a 20 20  rd[iDim*2+1];.  
365d4 20 20 20 20 20 20 66 6c 6f 61 74 20 78 72 69 67        float xrig
365d5 68 74 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78  ht1 = aCell[aIdx
365d6 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  [jj]].aCoord[iDi
365d7 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  m*2];.        fl
365d8 6f 61 74 20 78 72 69 67 68 74 32 20 3d 20 61 43  oat xright2 = aC
365d9 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43  ell[aIdx[jj]].aC
365da 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b 0a  oord[iDim*2+1];.
365db 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
365dc 78 6c 65 66 74 31 3c 3d 78 72 69 67 68 74 31 20  xleft1<=xright1 
365dd 26 26 20 28 78 6c 65 66 74 31 3c 78 72 69 67 68  && (xleft1<xrigh
365de 74 31 20 7c 7c 20 78 6c 65 66 74 32 3c 3d 78 72  t1 || xleft2<=xr
365df 69 67 68 74 32 29 20 29 3b 0a 20 20 20 20 20 20  ight2) );.      
365e0 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
365e1 20 7d 0a 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e   }.}..#if VARIAN
365e2 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
365e3 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  T./*.** Implemen
365e4 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 52 2a  tation of the R*
365e5 2d 74 72 65 65 20 76 61 72 69 61 6e 74 20 6f 66  -tree variant of
365e6 20 53 70 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20   SplitNode from 
365e7 42 65 63 6b 6d 61 6e 5b 31 39 39 30 5d 2e 0a 2a  Beckman[1990]..*
365e8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 70 6c  /.static int spl
365e9 69 74 4e 6f 64 65 53 74 61 72 74 72 65 65 28 0a  itNodeStartree(.
365ea 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
365eb 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43  .  RtreeCell *aC
365ec 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ell,.  int nCell
365ed 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ,.  RtreeNode *p
365ee 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 4e 6f 64  Left,.  RtreeNod
365ef 65 20 2a 70 52 69 67 68 74 2c 0a 20 20 52 74 72  e *pRight,.  Rtr
365f0 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 4c 65 66  eeCell *pBboxLef
365f1 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  t,.  RtreeCell *
365f2 70 42 62 6f 78 52 69 67 68 74 0a 29 7b 0a 20 20  pBboxRight.){.  
365f3 69 6e 74 20 2a 2a 61 61 53 6f 72 74 65 64 3b 0a  int **aaSorted;.
365f4 20 20 69 6e 74 20 2a 61 53 70 61 72 65 3b 0a 20    int *aSpare;. 
365f5 20 69 6e 74 20 69 69 3b 0a 0a 20 20 69 6e 74 20   int ii;..  int 
365f6 69 42 65 73 74 44 69 6d 3b 0a 20 20 69 6e 74 20  iBestDim;.  int 
365f7 69 42 65 73 74 53 70 6c 69 74 3b 0a 20 20 66 6c  iBestSplit;.  fl
365f8 6f 61 74 20 66 42 65 73 74 4d 61 72 67 69 6e 3b  oat fBestMargin;
365f9 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  ..  int nByte = 
365fa 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2b 31 29  (pRtree->nDim+1)
365fb 2a 28 73 69 7a 65 6f 66 28 69 6e 74 2a 29 2b 6e  *(sizeof(int*)+n
365fc 43 65 6c 6c 2a 73 69 7a 65 6f 66 28 69 6e 74 29  Cell*sizeof(int)
365fd 29 3b 0a 0a 20 20 61 61 53 6f 72 74 65 64 20 3d  );..  aaSorted =
365fe 20 28 69 6e 74 20 2a 2a 29 73 71 6c 69 74 65 33   (int **)sqlite3
365ff 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a  _malloc(nByte);.
36600 20 20 69 66 28 20 21 61 61 53 6f 72 74 65 64 20    if( !aaSorted 
36601 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
36602 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
36603 0a 20 20 61 53 70 61 72 65 20 3d 20 26 28 28 69  .  aSpare = &((i
36604 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64 5b 70  nt *)&aaSorted[p
36605 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b 70 52  Rtree->nDim])[pR
36606 74 72 65 65 2d 3e 6e 44 69 6d 2a 6e 43 65 6c 6c  tree->nDim*nCell
36607 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 61 53 6f  ];.  memset(aaSo
36608 72 74 65 64 2c 20 30 2c 20 6e 42 79 74 65 29 3b  rted, 0, nByte);
36609 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
3660a 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 69  pRtree->nDim; ii
3660b 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b  ++){.    int jj;
3660c 0a 20 20 20 20 61 61 53 6f 72 74 65 64 5b 69 69  .    aaSorted[ii
3660d 5d 20 3d 20 26 28 28 69 6e 74 20 2a 29 26 61 61  ] = &((int *)&aa
3660e 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d 3e 6e  Sorted[pRtree->n
3660f 44 69 6d 5d 29 5b 69 69 2a 6e 43 65 6c 6c 5d 3b  Dim])[ii*nCell];
36610 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a  .    for(jj=0; j
36611 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a  j<nCell; jj++){.
36612 20 20 20 20 20 20 61 61 53 6f 72 74 65 64 5b 69        aaSorted[i
36613 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a 3b 0a 20 20 20  i][jj] = jj;.   
36614 20 7d 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d   }.    SortByDim
36615 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61  ension(pRtree, a
36616 61 53 6f 72 74 65 64 5b 69 69 5d 2c 20 6e 43 65  aSorted[ii], nCe
36617 6c 6c 2c 20 69 69 2c 20 61 43 65 6c 6c 2c 20 61  ll, ii, aCell, a
36618 53 70 61 72 65 29 3b 0a 20 20 7d 0a 0a 20 20 66  Spare);.  }..  f
36619 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72  or(ii=0; ii<pRtr
3661a 65 65 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b  ee->nDim; ii++){
3661b 0a 20 20 20 20 66 6c 6f 61 74 20 6d 61 72 67 69  .    float margi
3661c 6e 20 3d 20 30 2e 30 3b 0a 20 20 20 20 66 6c 6f  n = 0.0;.    flo
3661d 61 74 20 66 42 65 73 74 4f 76 65 72 6c 61 70 3b  at fBestOverlap;
3661e 0a 20 20 20 20 66 6c 6f 61 74 20 66 42 65 73 74  .    float fBest
3661f 41 72 65 61 3b 0a 20 20 20 20 69 6e 74 20 69 42  Area;.    int iB
36620 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74  estLeft;.    int
36621 20 6e 4c 65 66 74 3b 0a 0a 20 20 20 20 66 6f 72   nLeft;..    for
36622 28 0a 20 20 20 20 20 20 6e 4c 65 66 74 3d 52 54  (.      nLeft=RT
36623 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
36624 72 65 65 29 3b 20 0a 20 20 20 20 20 20 6e 4c 65  ree); .      nLe
36625 66 74 3c 3d 28 6e 43 65 6c 6c 2d 52 54 52 45 45  ft<=(nCell-RTREE
36626 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
36627 29 29 3b 20 0a 20 20 20 20 20 20 6e 4c 65 66 74  )); .      nLeft
36628 2b 2b 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  ++.    ){.      
36629 52 74 72 65 65 43 65 6c 6c 20 6c 65 66 74 3b 0a  RtreeCell left;.
3662a 20 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20        RtreeCell 
3662b 72 69 67 68 74 3b 0a 20 20 20 20 20 20 69 6e 74  right;.      int
3662c 20 6b 6b 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74   kk;.      float
3662d 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20   overlap;.      
3662e 66 6c 6f 61 74 20 61 72 65 61 3b 0a 0a 20 20 20  float area;..   
3662f 20 20 20 6d 65 6d 63 70 79 28 26 6c 65 66 74 2c     memcpy(&left,
36630 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64   &aCell[aaSorted
36631 5b 69 69 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66  [ii][0]], sizeof
36632 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
36633 20 20 20 20 6d 65 6d 63 70 79 28 26 72 69 67 68      memcpy(&righ
36634 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74  t, &aCell[aaSort
36635 65 64 5b 69 69 5d 5b 6e 43 65 6c 6c 2d 31 5d 5d  ed[ii][nCell-1]]
36636 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65  , sizeof(RtreeCe
36637 6c 6c 29 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  ll));.      for(
36638 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e 43 65 6c 6c 2d  kk=1; kk<(nCell-
36639 31 29 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20  1); kk++){.     
3663a 20 20 20 69 66 28 20 6b 6b 3c 6e 4c 65 66 74 20     if( kk<nLeft 
3663b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 65 6c  ){.          cel
3663c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
3663d 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  left, &aCell[aaS
3663e 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29 3b  orted[ii][kk]]);
3663f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
36640 20 20 20 20 20 20 20 20 20 20 63 65 6c 6c 55 6e            cellUn
36641 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 72 69 67  ion(pRtree, &rig
36642 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  ht, &aCell[aaSor
36643 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20  ted[ii][kk]]);. 
36644 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
36645 0a 20 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d  .      margin +=
36646 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72   cellMargin(pRtr
36647 65 65 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20 20  ee, &left);.    
36648 20 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c    margin += cell
36649 4d 61 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26  Margin(pRtree, &
3664a 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 6f 76  right);.      ov
3664b 65 72 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72  erlap = cellOver
3664c 6c 61 70 28 70 52 74 72 65 65 2c 20 26 6c 65 66  lap(pRtree, &lef
3664d 74 2c 20 26 72 69 67 68 74 2c 20 31 2c 20 2d 31  t, &right, 1, -1
3664e 29 3b 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20  );.      area = 
3664f 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
36650 20 26 6c 65 66 74 29 20 2b 20 63 65 6c 6c 41 72   &left) + cellAr
36651 65 61 28 70 52 74 72 65 65 2c 20 26 72 69 67 68  ea(pRtree, &righ
36652 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 6e  t);.      if( (n
36653 4c 65 66 74 3d 3d 52 54 52 45 45 5f 4d 49 4e 43  Left==RTREE_MINC
36654 45 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20 20  ELLS(pRtree)).  
36655 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70       || (overlap
36656 3c 66 42 65 73 74 4f 76 65 72 6c 61 70 29 0a 20  <fBestOverlap). 
36657 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61        || (overla
36658 70 3d 3d 66 42 65 73 74 4f 76 65 72 6c 61 70 20  p==fBestOverlap 
36659 26 26 20 61 72 65 61 3c 66 42 65 73 74 41 72 65  && area<fBestAre
3665a 61 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  a).      ){.    
3665b 20 20 20 20 69 42 65 73 74 4c 65 66 74 20 3d 20      iBestLeft = 
3665c 6e 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 66  nLeft;.        f
3665d 42 65 73 74 4f 76 65 72 6c 61 70 20 3d 20 6f 76  BestOverlap = ov
3665e 65 72 6c 61 70 3b 0a 20 20 20 20 20 20 20 20 66  erlap;.        f
3665f 42 65 73 74 41 72 65 61 20 3d 20 61 72 65 61 3b  BestArea = area;
36660 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
36661 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20 7c 7c      if( ii==0 ||
36662 20 6d 61 72 67 69 6e 3c 66 42 65 73 74 4d 61 72   margin<fBestMar
36663 67 69 6e 20 29 7b 0a 20 20 20 20 20 20 69 42 65  gin ){.      iBe
36664 73 74 44 69 6d 20 3d 20 69 69 3b 0a 20 20 20 20  stDim = ii;.    
36665 20 20 66 42 65 73 74 4d 61 72 67 69 6e 20 3d 20    fBestMargin = 
36666 6d 61 72 67 69 6e 3b 0a 20 20 20 20 20 20 69 42  margin;.      iB
36667 65 73 74 53 70 6c 69 74 20 3d 20 69 42 65 73 74  estSplit = iBest
36668 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Left;.    }.  }.
36669 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c  .  memcpy(pBboxL
3666a 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f  eft, &aCell[aaSo
3666b 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b 30  rted[iBestDim][0
3666c 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ]], sizeof(Rtree
3666d 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79  Cell));.  memcpy
3666e 28 70 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43  (pBboxRight, &aC
3666f 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42 65  ell[aaSorted[iBe
36670 73 74 44 69 6d 5d 5b 69 42 65 73 74 53 70 6c 69  stDim][iBestSpli
36671 74 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  t]], sizeof(Rtre
36672 65 43 65 6c 6c 29 29 3b 0a 20 20 66 6f 72 28 69  eCell));.  for(i
36673 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
36674 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  i++){.    RtreeN
36675 6f 64 65 20 2a 70 54 61 72 67 65 74 20 3d 20 28  ode *pTarget = (
36676 69 69 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70  ii<iBestSplit)?p
36677 4c 65 66 74 3a 70 52 69 67 68 74 3b 0a 20 20 20  Left:pRight;.   
36678 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f   RtreeCell *pBbo
36679 78 20 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c  x = (ii<iBestSpl
3667a 69 74 29 3f 70 42 62 6f 78 4c 65 66 74 3a 70 42  it)?pBboxLeft:pB
3667b 62 6f 78 52 69 67 68 74 3b 0a 20 20 20 20 52 74  boxRight;.    Rt
3667c 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 3d  reeCell *pCell =
3667d 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64   &aCell[aaSorted
3667e 5b 69 42 65 73 74 44 69 6d 5d 5b 69 69 5d 5d 3b  [iBestDim][ii]];
3667f 0a 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43  .    nodeInsertC
36680 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 54 61 72  ell(pRtree, pTar
36681 67 65 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  get, pCell);.   
36682 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
36683 65 2c 20 70 42 62 6f 78 2c 20 70 43 65 6c 6c 29  e, pBbox, pCell)
36684 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
36685 5f 66 72 65 65 28 61 61 53 6f 72 74 65 64 29 3b  _free(aaSorted);
36686 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
36687 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  _OK;.}.#endif..#
36688 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  if VARIANT_GUTTM
36689 41 4e 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49  AN_SPLIT./*.** I
3668a 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
3668b 20 74 68 65 20 72 65 67 75 6c 61 72 20 52 2d 74   the regular R-t
3668c 72 65 65 20 53 70 6c 69 74 4e 6f 64 65 20 66 72  ree SplitNode fr
3668d 6f 6d 20 47 75 74 74 6d 61 6e 5b 31 39 38 34 5d  om Guttman[1984]
3668e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3668f 73 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e  splitNodeGuttman
36690 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
36691 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e,.  RtreeCell *
36692 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43 65  aCell,.  int nCe
36693 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ll,.  RtreeNode 
36694 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 4e  *pLeft,.  RtreeN
36695 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a 20 20 52  ode *pRight,.  R
36696 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 4c  treeCell *pBboxL
36697 65 66 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  eft,.  RtreeCell
36698 20 2a 70 42 62 6f 78 52 69 67 68 74 0a 29 7b 0a   *pBboxRight.){.
36699 20 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20    int iLeftSeed 
3669a 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68  = 0;.  int iRigh
3669b 74 53 65 65 64 20 3d 20 31 3b 0a 20 20 69 6e 74  tSeed = 1;.  int
3669c 20 2a 61 69 55 73 65 64 3b 0a 20 20 69 6e 74 20   *aiUsed;.  int 
3669d 69 3b 0a 0a 20 20 61 69 55 73 65 64 20 3d 20 73  i;..  aiUsed = s
3669e 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
3669f 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29  zeof(int)*nCell)
366a0 3b 0a 20 20 6d 65 6d 73 65 74 28 61 69 55 73 65  ;.  memset(aiUse
366a1 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e 74  d, 0, sizeof(int
366a2 29 2a 6e 43 65 6c 6c 29 3b 0a 0a 20 20 50 69 63  )*nCell);..  Pic
366a3 6b 53 65 65 64 73 28 70 52 74 72 65 65 2c 20 61  kSeeds(pRtree, a
366a4 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 26 69 4c  Cell, nCell, &iL
366a5 65 66 74 53 65 65 64 2c 20 26 69 52 69 67 68 74  eftSeed, &iRight
366a6 53 65 65 64 29 3b 0a 0a 20 20 6d 65 6d 63 70 79  Seed);..  memcpy
366a7 28 70 42 62 6f 78 4c 65 66 74 2c 20 26 61 43 65  (pBboxLeft, &aCe
366a8 6c 6c 5b 69 4c 65 66 74 53 65 65 64 5d 2c 20 73  ll[iLeftSeed], s
366a9 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
366aa 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f  );.  memcpy(pBbo
366ab 78 52 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 69  xRight, &aCell[i
366ac 52 69 67 68 74 53 65 65 64 5d 2c 20 73 69 7a 65  RightSeed], size
366ad 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a  of(RtreeCell));.
366ae 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c    nodeInsertCell
366af 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20  (pRtree, pLeft, 
366b0 26 61 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65 64  &aCell[iLeftSeed
366b1 5d 29 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72 74  ]);.  nodeInsert
366b2 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69  Cell(pRtree, pRi
366b3 67 68 74 2c 20 26 61 43 65 6c 6c 5b 69 52 69 67  ght, &aCell[iRig
366b4 68 74 53 65 65 64 5d 29 3b 0a 20 20 61 69 55 73  htSeed]);.  aiUs
366b5 65 64 5b 69 4c 65 66 74 53 65 65 64 5d 20 3d 20  ed[iLeftSeed] = 
366b6 31 3b 0a 20 20 61 69 55 73 65 64 5b 69 52 69 67  1;.  aiUsed[iRig
366b7 68 74 53 65 65 64 5d 20 3d 20 31 3b 0a 0a 20 20  htSeed] = 1;..  
366b8 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d 32 3b 20 69  for(i=nCell-2; i
366b9 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 52 74  >0; i--){.    Rt
366ba 72 65 65 43 65 6c 6c 20 2a 70 4e 65 78 74 3b 0a  reeCell *pNext;.
366bb 20 20 20 20 70 4e 65 78 74 20 3d 20 50 69 63 6b      pNext = Pick
366bc 4e 65 78 74 28 70 52 74 72 65 65 2c 20 61 43 65  Next(pRtree, aCe
366bd 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70 42 62 6f 78  ll, nCell, pBbox
366be 4c 65 66 74 2c 20 70 42 62 6f 78 52 69 67 68 74  Left, pBboxRight
366bf 2c 20 61 69 55 73 65 64 29 3b 0a 20 20 20 20 66  , aiUsed);.    f
366c0 6c 6f 61 74 20 64 69 66 66 20 3d 20 20 0a 20 20  loat diff =  .  
366c1 20 20 20 20 63 65 6c 6c 47 72 6f 77 74 68 28 70      cellGrowth(p
366c2 52 74 72 65 65 2c 20 70 42 62 6f 78 4c 65 66 74  Rtree, pBboxLeft
366c3 2c 20 70 4e 65 78 74 29 20 2d 20 0a 20 20 20 20  , pNext) - .    
366c4 20 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74    cellGrowth(pRt
366c5 72 65 65 2c 20 70 42 62 6f 78 52 69 67 68 74 2c  ree, pBboxRight,
366c6 20 70 4e 65 78 74 29 0a 20 20 20 20 3b 0a 20 20   pNext).    ;.  
366c7 20 20 69 66 28 20 28 52 54 52 45 45 5f 4d 49 4e    if( (RTREE_MIN
366c8 43 45 4c 4c 53 28 70 52 74 72 65 65 29 2d 4e 43  CELLS(pRtree)-NC
366c9 45 4c 4c 28 70 52 69 67 68 74 29 3d 3d 69 29 0a  ELL(pRight)==i).
366ca 20 20 20 20 20 7c 7c 20 28 64 69 66 66 3e 30 2e       || (diff>0.
366cb 30 20 26 26 20 28 52 54 52 45 45 5f 4d 49 4e 43  0 && (RTREE_MINC
366cc 45 4c 4c 53 28 70 52 74 72 65 65 29 2d 4e 43 45  ELLS(pRtree)-NCE
366cd 4c 4c 28 70 4c 65 66 74 29 21 3d 69 29 29 0a 20  LL(pLeft)!=i)). 
366ce 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65     ){.      node
366cf 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65  InsertCell(pRtre
366d0 65 2c 20 70 52 69 67 68 74 2c 20 70 4e 65 78 74  e, pRight, pNext
366d1 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e 69  );.      cellUni
366d2 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78  on(pRtree, pBbox
366d3 52 69 67 68 74 2c 20 70 4e 65 78 74 29 3b 0a 20  Right, pNext);. 
366d4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
366d5 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
366d6 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20 70 4e  Rtree, pLeft, pN
366d7 65 78 74 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c  ext);.      cell
366d8 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42  Union(pRtree, pB
366d9 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b  boxLeft, pNext);
366da 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71  .    }.  }..  sq
366db 6c 69 74 65 33 5f 66 72 65 65 28 61 69 55 73 65  lite3_free(aiUse
366dc 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d);.  return SQL
366dd 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
366de 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 75 70 64  ..static int upd
366df 61 74 65 4d 61 70 70 69 6e 67 28 0a 20 20 52 74  ateMapping(.  Rt
366e0 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
366e1 69 36 34 20 69 52 6f 77 69 64 2c 20 0a 20 20 52  i64 iRowid, .  R
366e2 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
366e3 20 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a   .  int iHeight.
366e4 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 74 4d  ){.  int (*xSetM
366e5 61 70 70 69 6e 67 29 28 52 74 72 65 65 20 2a 2c  apping)(Rtree *,
366e6 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20   sqlite3_int64, 
366e7 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a  sqlite3_int64);.
366e8 20 20 78 53 65 74 4d 61 70 70 69 6e 67 20 3d 20    xSetMapping = 
366e9 28 28 69 48 65 69 67 68 74 3d 3d 30 29 3f 72 6f  ((iHeight==0)?ro
366ea 77 69 64 57 72 69 74 65 3a 70 61 72 65 6e 74 57  widWrite:parentW
366eb 72 69 74 65 29 3b 0a 20 20 69 66 28 20 69 48 65  rite);.  if( iHe
366ec 69 67 68 74 3e 30 20 29 7b 0a 20 20 20 20 52 74  ight>0 ){.    Rt
366ed 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64 20  reeNode *pChild 
366ee 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b 75 70  = nodeHashLookup
366ef 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 29  (pRtree, iRowid)
366f0 3b 0a 20 20 20 20 69 66 28 20 70 43 68 69 6c 64  ;.    if( pChild
366f1 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65   ){.      nodeRe
366f2 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43  lease(pRtree, pC
366f3 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  hild->pParent);.
366f4 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65        nodeRefere
366f5 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  nce(pNode);.    
366f6 20 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e    pChild->pParen
366f7 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 7d  t = pNode;.    }
366f8 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 78 53  .  }.  return xS
366f9 65 74 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65  etMapping(pRtree
366fa 2c 20 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d  , iRowid, pNode-
366fb 3e 69 4e 6f 64 65 29 3b 0a 7d 0a 0a 73 74 61 74  >iNode);.}..stat
366fc 69 63 20 69 6e 74 20 53 70 6c 69 74 4e 6f 64 65  ic int SplitNode
366fd 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
366fe 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  e,.  RtreeNode *
366ff 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65 65 43 65  pNode,.  RtreeCe
36700 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e 74  ll *pCell,.  int
36701 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e   iHeight.){.  in
36702 74 20 69 3b 0a 20 20 69 6e 74 20 6e 65 77 43 65  t i;.  int newCe
36703 6c 6c 49 73 52 69 67 68 74 20 3d 20 30 3b 0a 0a  llIsRight = 0;..
36704 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
36705 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65 6c  E_OK;.  int nCel
36706 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
36707 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ;.  RtreeCell *a
36708 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 2a 61 69 55  Cell;.  int *aiU
36709 73 65 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64  sed;..  RtreeNod
3670a 65 20 2a 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20  e *pLeft = 0;.  
3670b 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 69 67 68  RtreeNode *pRigh
3670c 74 20 3d 20 30 3b 0a 0a 20 20 52 74 72 65 65 43  t = 0;..  RtreeC
3670d 65 6c 6c 20 6c 65 66 74 62 62 6f 78 3b 0a 20 20  ell leftbbox;.  
3670e 52 74 72 65 65 43 65 6c 6c 20 72 69 67 68 74 62  RtreeCell rightb
3670f 62 6f 78 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  box;..  /* Alloc
36710 61 74 65 20 61 6e 20 61 72 72 61 79 20 61 6e 64  ate an array and
36711 20 70 6f 70 75 6c 61 74 65 20 69 74 20 77 69 74   populate it wit
36712 68 20 61 20 63 6f 70 79 20 6f 66 20 70 43 65 6c  h a copy of pCel
36713 6c 20 61 6e 64 20 0a 20 20 2a 2a 20 61 6c 6c 20  l and .  ** all 
36714 63 65 6c 6c 73 20 66 72 6f 6d 20 6e 6f 64 65 20  cells from node 
36715 70 4c 65 66 74 2e 20 54 68 65 6e 20 7a 65 72 6f  pLeft. Then zero
36716 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6e 6f   the original no
36717 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 43 65 6c 6c  de..  */.  aCell
36718 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
36719 63 28 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43  c((sizeof(RtreeC
3671a 65 6c 6c 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29  ell)+sizeof(int)
3671b 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b 0a 20 20  )*(nCell+1));.  
3671c 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20 20  if( !aCell ){.  
3671d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
3671e 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 73 70  MEM;.    goto sp
3671f 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d  litnode_out;.  }
36720 0a 20 20 61 69 55 73 65 64 20 3d 20 28 69 6e 74  .  aiUsed = (int
36721 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 2b   *)&aCell[nCell+
36722 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 69 55  1];.  memset(aiU
36723 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69  sed, 0, sizeof(i
36724 6e 74 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b 0a  nt)*(nCell+1));.
36725 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65    for(i=0; i<nCe
36726 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f  ll; i++){.    no
36727 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
36728 2c 20 70 4e 6f 64 65 2c 20 69 2c 20 26 61 43 65  , pNode, i, &aCe
36729 6c 6c 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6e 6f  ll[i]);.  }.  no
3672a 64 65 5a 65 72 6f 28 70 52 74 72 65 65 2c 20 70  deZero(pRtree, p
3672b 4e 6f 64 65 29 3b 0a 20 20 6d 65 6d 63 70 79 28  Node);.  memcpy(
3672c 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 70  &aCell[nCell], p
3672d 43 65 6c 6c 2c 20 73 69 7a 65 6f 66 28 52 74 72  Cell, sizeof(Rtr
3672e 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6e 43 65 6c  eeCell));.  nCel
3672f 6c 2b 2b 3b 0a 0a 20 20 69 66 28 20 70 4e 6f 64  l++;..  if( pNod
36730 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20  e->iNode==1 ){. 
36731 20 20 20 70 52 69 67 68 74 20 3d 20 6e 6f 64 65     pRight = node
36732 4e 65 77 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  New(pRtree, pNod
36733 65 2c 20 31 29 3b 0a 20 20 20 20 70 4c 65 66 74  e, 1);.    pLeft
36734 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65   = nodeNew(pRtre
36735 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20 20  e, pNode, 1);.  
36736 20 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68    pRtree->iDepth
36737 2b 2b 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69  ++;.    pNode->i
36738 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20  sDirty = 1;.    
36739 77 72 69 74 65 49 6e 74 31 36 28 70 4e 6f 64 65  writeInt16(pNode
3673a 2d 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d  ->zData, pRtree-
3673b 3e 69 44 65 70 74 68 29 3b 0a 20 20 7d 65 6c 73  >iDepth);.  }els
3673c 65 7b 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70  e{.    pLeft = p
3673d 4e 6f 64 65 3b 0a 20 20 20 20 70 52 69 67 68 74  Node;.    pRight
3673e 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65   = nodeNew(pRtre
3673f 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e  e, pLeft->pParen
36740 74 2c 20 31 29 3b 0a 20 20 20 20 6e 6f 64 65 52  t, 1);.    nodeR
36741 65 66 65 72 65 6e 63 65 28 70 4c 65 66 74 29 3b  eference(pLeft);
36742 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 4c 65  .  }..  if( !pLe
36743 66 74 20 7c 7c 20 21 70 52 69 67 68 74 20 29 7b  ft || !pRight ){
36744 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
36745 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f  _NOMEM;.    goto
36746 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
36747 20 20 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 70 4c    }..  memset(pL
36748 65 66 74 2d 3e 7a 44 61 74 61 2c 20 30 2c 20 70  eft->zData, 0, p
36749 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65  Rtree->iNodeSize
3674a 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 52 69 67  );.  memset(pRig
3674b 68 74 2d 3e 7a 44 61 74 61 2c 20 30 2c 20 70 52  ht->zData, 0, pR
3674c 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29  tree->iNodeSize)
3674d 3b 0a 0a 20 20 72 63 20 3d 20 41 73 73 69 67 6e  ;..  rc = Assign
3674e 43 65 6c 6c 73 28 70 52 74 72 65 65 2c 20 61 43  Cells(pRtree, aC
3674f 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70 4c 65 66  ell, nCell, pLef
36750 74 2c 20 70 52 69 67 68 74 2c 20 26 6c 65 66 74  t, pRight, &left
36751 62 62 6f 78 2c 20 26 72 69 67 68 74 62 62 6f 78  bbox, &rightbbox
36752 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36753 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f  ITE_OK ){.    go
36754 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74  to splitnode_out
36755 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75  ;.  }..  /* Ensu
36756 72 65 20 62 6f 74 68 20 63 68 69 6c 64 20 6e 6f  re both child no
36757 64 65 73 20 68 61 76 65 20 6e 6f 64 65 20 6e 75  des have node nu
36758 6d 62 65 72 73 20 61 73 73 69 67 6e 65 64 20 74  mbers assigned t
36759 6f 20 74 68 65 6d 2e 20 2a 2f 0a 20 20 69 66 28  o them. */.  if(
3675a 20 28 30 3d 3d 70 52 69 67 68 74 2d 3e 69 4e 6f   (0==pRight->iNo
3675b 64 65 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21  de && SQLITE_OK!
3675c 3d 28 72 63 20 3d 20 6e 6f 64 65 57 72 69 74 65  =(rc = nodeWrite
3675d 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74 29  (pRtree, pRight)
3675e 29 29 0a 20 20 20 7c 7c 20 28 30 3d 3d 70 4c 65  )).   || (0==pLe
3675f 66 74 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51 4c  ft->iNode && SQL
36760 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f  ITE_OK!=(rc = no
36761 64 65 57 72 69 74 65 28 70 52 74 72 65 65 2c 20  deWrite(pRtree, 
36762 70 4c 65 66 74 29 29 29 0a 20 20 29 7b 0a 20 20  pLeft))).  ){.  
36763 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65    goto splitnode
36764 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 69 67  _out;.  }..  rig
36765 68 74 62 62 6f 78 2e 69 52 6f 77 69 64 20 3d 20  htbbox.iRowid = 
36766 70 52 69 67 68 74 2d 3e 69 4e 6f 64 65 3b 0a 20  pRight->iNode;. 
36767 20 6c 65 66 74 62 62 6f 78 2e 69 52 6f 77 69 64   leftbbox.iRowid
36768 20 3d 20 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 3b   = pLeft->iNode;
36769 0a 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69  ..  if( pNode->i
3676a 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 72  Node==1 ){.    r
3676b 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43  c = rtreeInsertC
3676c 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66  ell(pRtree, pLef
3676d 74 2d 3e 70 50 61 72 65 6e 74 2c 20 26 6c 65 66  t->pParent, &lef
3676e 74 62 62 6f 78 2c 20 69 48 65 69 67 68 74 2b 31  tbbox, iHeight+1
3676f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
36770 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
36771 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65    goto splitnode
36772 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  _out;.    }.  }e
36773 6c 73 65 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f  lse{.    RtreeNo
36774 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4c  de *pParent = pL
36775 65 66 74 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  eft->pParent;.  
36776 20 20 69 6e 74 20 69 43 65 6c 6c 20 3d 20 6e 6f    int iCell = no
36777 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52  deParentIndex(pR
36778 74 72 65 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20  tree, pLeft);.  
36779 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43    nodeOverwriteC
3677a 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72  ell(pRtree, pPar
3677b 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20  ent, &leftbbox, 
3677c 69 43 65 6c 6c 29 3b 0a 20 20 20 20 41 64 6a 75  iCell);.    Adju
3677d 73 74 54 72 65 65 28 70 52 74 72 65 65 2c 20 70  stTree(pRtree, p
3677e 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f  Parent, &leftbbo
3677f 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 72  x);.  }.  if( (r
36780 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43  c = rtreeInsertC
36781 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67  ell(pRtree, pRig
36782 68 74 2d 3e 70 50 61 72 65 6e 74 2c 20 26 72 69  ht->pParent, &ri
36783 67 68 74 62 62 6f 78 2c 20 69 48 65 69 67 68 74  ghtbbox, iHeight
36784 2b 31 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  +1)) ){.    goto
36785 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
36786 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20    }..  for(i=0; 
36787 69 3c 4e 43 45 4c 4c 28 70 52 69 67 68 74 29 3b  i<NCELL(pRight);
36788 20 69 2b 2b 29 7b 0a 20 20 20 20 69 36 34 20 69   i++){.    i64 i
36789 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52  Rowid = nodeGetR
3678a 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 52 69  owid(pRtree, pRi
3678b 67 68 74 2c 20 69 29 3b 0a 20 20 20 20 72 63 20  ght, i);.    rc 
3678c 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  = updateMapping(
3678d 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20  pRtree, iRowid, 
3678e 70 52 69 67 68 74 2c 20 69 48 65 69 67 68 74 29  pRight, iHeight)
3678f 3b 0a 20 20 20 20 69 66 28 20 69 52 6f 77 69 64  ;.    if( iRowid
36790 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 20  ==pCell->iRowid 
36791 29 7b 0a 20 20 20 20 20 20 6e 65 77 43 65 6c 6c  ){.      newCell
36792 49 73 52 69 67 68 74 20 3d 20 31 3b 0a 20 20 20  IsRight = 1;.   
36793 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
36794 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
36795 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65    goto splitnode
36796 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  _out;.    }.  }.
36797 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
36798 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 66 6f 72  de==1 ){.    for
36799 28 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 4c  (i=0; i<NCELL(pL
3679a 65 66 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  eft); i++){.    
3679b 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20 6e    i64 iRowid = n
3679c 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72  odeGetRowid(pRtr
3679d 65 65 2c 20 70 4c 65 66 74 2c 20 69 29 3b 0a 20  ee, pLeft, i);. 
3679e 20 20 20 20 20 72 63 20 3d 20 75 70 64 61 74 65       rc = update
3679f 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20  Mapping(pRtree, 
367a0 69 52 6f 77 69 64 2c 20 70 4c 65 66 74 2c 20 69  iRowid, pLeft, i
367a1 48 65 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69  Height);.      i
367a2 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
367a3 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
367a4 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
367a5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
367a6 7d 65 6c 73 65 20 69 66 28 20 6e 65 77 43 65 6c  }else if( newCel
367a7 6c 49 73 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20  lIsRight==0 ){. 
367a8 20 20 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61     rc = updateMa
367a9 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20 70 43  pping(pRtree, pC
367aa 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4c 65  ell->iRowid, pLe
367ab 66 74 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  ft, iHeight);.  
367ac 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
367ad 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
367ae 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
367af 52 74 72 65 65 2c 20 70 52 69 67 68 74 29 3b 0a  Rtree, pRight);.
367b0 20 20 20 20 70 52 69 67 68 74 20 3d 20 30 3b 0a      pRight = 0;.
367b1 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
367b2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
367b3 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
367b4 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 3b 0a  pRtree, pLeft);.
367b5 20 20 20 20 70 4c 65 66 74 20 3d 20 30 3b 0a 20      pLeft = 0;. 
367b6 20 7d 0a 0a 73 70 6c 69 74 6e 6f 64 65 5f 6f 75   }..splitnode_ou
367b7 74 3a 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65  t:.  nodeRelease
367b8 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74 29  (pRtree, pRight)
367b9 3b 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  ;.  nodeRelease(
367ba 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 3b 0a  pRtree, pLeft);.
367bb 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
367bc 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20  Cell);.  return 
367bd 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
367be 74 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74 28  t fixLeafParent(
367bf 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
367c0 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 29  treeNode *pLeaf)
367c1 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
367c2 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4c  ITE_OK;.  if( pL
367c3 65 61 66 2d 3e 69 4e 6f 64 65 21 3d 31 20 26 26  eaf->iNode!=1 &&
367c4 20 70 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 3d   pLeaf->pParent=
367c5 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
367c6 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
367c7 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74  ree->pReadParent
367c8 2c 20 31 2c 20 70 4c 65 61 66 2d 3e 69 4e 6f 64  , 1, pLeaf->iNod
367c9 65 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  e);.    if( sqli
367ca 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d  te3_step(pRtree-
367cb 3e 70 52 65 61 64 50 61 72 65 6e 74 29 3d 3d 53  >pReadParent)==S
367cc 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
367cd 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d 20 73     i64 iNode = s
367ce 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
367cf 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61  t64(pRtree->pRea
367d0 64 50 61 72 65 6e 74 2c 20 30 29 3b 0a 20 20 20  dParent, 0);.   
367d1 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75     rc = nodeAcqu
367d2 69 72 65 28 70 52 74 72 65 65 2c 20 69 4e 6f 64  ire(pRtree, iNod
367d3 65 2c 20 30 2c 20 26 70 4c 65 61 66 2d 3e 70 50  e, 0, &pLeaf->pP
367d4 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 65 6c 73  arent);.    }els
367d5 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  e{.      rc = SQ
367d6 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
367d7 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  }.    sqlite3_re
367d8 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61  set(pRtree->pRea
367d9 64 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66  dParent);.    if
367da 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
367db 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69  ){.      rc = fi
367dc 78 4c 65 61 66 50 61 72 65 6e 74 28 70 52 74 72  xLeafParent(pRtr
367dd 65 65 2c 20 70 4c 65 61 66 2d 3e 70 50 61 72 65  ee, pLeaf->pPare
367de 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  nt);.    }.  }. 
367df 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
367e0 74 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65  tatic int delete
367e1 43 65 6c 6c 28 52 74 72 65 65 20 2a 2c 20 52 74  Cell(Rtree *, Rt
367e2 72 65 65 4e 6f 64 65 20 2a 2c 20 69 6e 74 2c 20  reeNode *, int, 
367e3 69 6e 74 29 3b 0a 0a 73 74 61 74 69 63 20 69 6e  int);..static in
367e4 74 20 72 65 6d 6f 76 65 4e 6f 64 65 28 52 74 72  t removeNode(Rtr
367e5 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
367e6 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e  eNode *pNode, in
367e7 74 20 69 48 65 69 67 68 74 29 7b 0a 20 20 69 6e  t iHeight){.  in
367e8 74 20 72 63 3b 0a 20 20 52 74 72 65 65 4e 6f 64  t rc;.  RtreeNod
367e9 65 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 69 6e  e *pParent;.  in
367ea 74 20 69 43 65 6c 6c 3b 0a 0a 20 20 61 73 73 65  t iCell;..  asse
367eb 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d  rt( pNode->nRef=
367ec 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 6d 6f  =1 );..  /* Remo
367ed 76 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20  ve the entry in 
367ee 74 68 65 20 70 61 72 65 6e 74 20 63 65 6c 6c 2e  the parent cell.
367ef 20 2a 2f 0a 20 20 69 43 65 6c 6c 20 3d 20 6e 6f   */.  iCell = no
367f0 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52  deParentIndex(pR
367f1 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20  tree, pNode);.  
367f2 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d  pParent = pNode-
367f3 3e 70 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f 64  >pParent;.  pNod
367f4 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 30 3b 0a  e->pParent = 0;.
367f5 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
367f6 3d 28 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c  =(rc = deleteCel
367f7 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  l(pRtree, pParen
367f8 74 2c 20 69 43 65 6c 6c 2c 20 69 48 65 69 67 68  t, iCell, iHeigh
367f9 74 2b 31 29 29 20 0a 20 20 20 7c 7c 20 53 51 4c  t+1)) .   || SQL
367fa 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f  ITE_OK!=(rc = no
367fb 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
367fc 2c 20 70 50 61 72 65 6e 74 29 29 0a 20 20 29 7b  , pParent)).  ){
367fd 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
367fe 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65    }..  /* Remove
367ff 20 74 68 65 20 78 78 78 5f 6e 6f 64 65 20 65 6e   the xxx_node en
36800 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  try. */.  sqlite
36801 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
36802 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65  ree->pDeleteNode
36803 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  , 1, pNode->iNod
36804 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  e);.  sqlite3_st
36805 65 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  ep(pRtree->pDele
36806 74 65 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 53  teNode);.  if( S
36807 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
36808 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
36809 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64  tree->pDeleteNod
3680a 65 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  e)) ){.    retur
3680b 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
3680c 52 65 6d 6f 76 65 20 74 68 65 20 78 78 78 5f 70  Remove the xxx_p
3680d 61 72 65 6e 74 20 65 6e 74 72 79 2e 20 2a 2f 0a  arent entry. */.
3680e 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
3680f 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44 65  nt64(pRtree->pDe
36810 6c 65 74 65 50 61 72 65 6e 74 2c 20 31 2c 20 70  leteParent, 1, p
36811 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
36812 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74  sqlite3_step(pRt
36813 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65  ree->pDeletePare
36814 6e 74 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  nt);.  if( SQLIT
36815 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
36816 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
36817 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29  ->pDeleteParent)
36818 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
36819 72 63 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  rc;.  }.  .  /* 
3681a 52 65 6d 6f 76 65 20 74 68 65 20 6e 6f 64 65 20  Remove the node 
3681b 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  from the in-memo
3681c 72 79 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e  ry hash table an
3681d 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 0a 20  d link it into. 
3681e 20 2a 2a 20 74 68 65 20 52 74 72 65 65 2e 70 44   ** the Rtree.pD
3681f 65 6c 65 74 65 64 20 6c 69 73 74 2e 20 49 74 73  eleted list. Its
36820 20 63 6f 6e 74 65 6e 74 73 20 77 69 6c 6c 20 62   contents will b
36821 65 20 72 65 2d 69 6e 73 65 72 74 65 64 20 6c 61  e re-inserted la
36822 74 65 72 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 6e  ter on..  */.  n
36823 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28 70 52  odeHashDelete(pR
36824 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20  tree, pNode);.  
36825 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 69  pNode->iNode = i
36826 48 65 69 67 68 74 3b 0a 20 20 70 4e 6f 64 65 2d  Height;.  pNode-
36827 3e 70 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d  >pNext = pRtree-
36828 3e 70 44 65 6c 65 74 65 64 3b 0a 20 20 70 4e 6f  >pDeleted;.  pNo
36829 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 70 52  de->nRef++;.  pR
3682a 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d  tree->pDeleted =
3682b 20 70 4e 6f 64 65 3b 0a 0a 20 20 72 65 74 75 72   pNode;..  retur
3682c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
3682d 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 78 42  static void fixB
3682e 6f 75 6e 64 69 6e 67 42 6f 78 28 52 74 72 65 65  oundingBox(Rtree
3682f 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
36830 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52  ode *pNode){.  R
36831 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e  treeNode *pParen
36832 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65  t = pNode->pPare
36833 6e 74 3b 0a 20 20 69 66 28 20 70 50 61 72 65 6e  nt;.  if( pParen
36834 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b  t ){.    int ii;
36835 20 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20   .    int nCell 
36836 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a  = NCELL(pNode);.
36837 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 62 6f      RtreeCell bo
36838 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x;              
36839 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3683a 20 42 6f 75 6e 64 69 6e 67 20 62 6f 78 20 66 6f   Bounding box fo
3683b 72 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 20 20 6e  r pNode */.    n
3683c 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65  odeGetCell(pRtre
3683d 65 2c 20 70 4e 6f 64 65 2c 20 30 2c 20 26 62 6f  e, pNode, 0, &bo
3683e 78 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 31  x);.    for(ii=1
3683f 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
36840 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65 43 65  ){.      RtreeCe
36841 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 20 20 6e  ll cell;.      n
36842 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65  odeGetCell(pRtre
36843 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 63  e, pNode, ii, &c
36844 65 6c 6c 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c  ell);.      cell
36845 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 62  Union(pRtree, &b
36846 6f 78 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20  ox, &cell);.    
36847 7d 0a 20 20 20 20 62 6f 78 2e 69 52 6f 77 69 64  }.    box.iRowid
36848 20 3d 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3b   = pNode->iNode;
36849 0a 20 20 20 20 69 69 20 3d 20 6e 6f 64 65 50 61  .    ii = nodePa
3684a 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65 65  rentIndex(pRtree
3684b 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 6e 6f  , pNode);.    no
3684c 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28  deOverwriteCell(
3684d 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c  pRtree, pParent,
3684e 20 26 62 6f 78 2c 20 69 69 29 3b 0a 20 20 20 20   &box, ii);.    
3684f 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70  fixBoundingBox(p
36850 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 29 3b  Rtree, pParent);
36851 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  .  }.}../*.** De
36852 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20 61 74  lete the cell at
36853 20 69 6e 64 65 78 20 69 43 65 6c 6c 20 6f 66 20   index iCell of 
36854 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 41 66 74 65  node pNode. Afte
36855 72 20 72 65 6d 6f 76 69 6e 67 20 74 68 65 0a 2a  r removing the.*
36856 2a 20 63 65 6c 6c 2c 20 61 64 6a 75 73 74 20 74  * cell, adjust t
36857 68 65 20 72 2d 74 72 65 65 20 64 61 74 61 20 73  he r-tree data s
36858 74 72 75 63 74 75 72 65 20 69 66 20 72 65 71 75  tructure if requ
36859 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ired..*/.static 
3685a 69 6e 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52  int deleteCell(R
3685b 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
3685c 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20  reeNode *pNode, 
3685d 69 6e 74 20 69 43 65 6c 6c 2c 20 69 6e 74 20 69  int iCell, int i
3685e 48 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72  Height){.  int r
3685f 63 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45  c;..  if( SQLITE
36860 5f 4f 4b 21 3d 28 72 63 20 3d 20 66 69 78 4c 65  _OK!=(rc = fixLe
36861 61 66 50 61 72 65 6e 74 28 70 52 74 72 65 65 2c  afParent(pRtree,
36862 20 70 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20   pNode)) ){.    
36863 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
36864 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
36865 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 6e 6f  cell from the no
36866 64 65 2e 20 54 68 69 73 20 63 61 6c 6c 20 6a 75  de. This call ju
36867 73 74 20 6d 6f 76 65 73 20 62 79 74 65 73 20 61  st moves bytes a
36868 72 6f 75 6e 64 0a 20 20 2a 2a 20 74 68 65 20 69  round.  ** the i
36869 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65 20 69 6d  n-memory node im
3686a 61 67 65 2c 20 73 6f 20 69 74 20 63 61 6e 6e 6f  age, so it canno
3686b 74 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 6e  t fail..  */.  n
3686c 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 70 52  odeDeleteCell(pR
3686d 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65  tree, pNode, iCe
3686e 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ll);..  /* If th
3686f 65 20 6e 6f 64 65 20 69 73 20 6e 6f 74 20 74 68  e node is not th
36870 65 20 74 72 65 65 20 72 6f 6f 74 20 61 6e 64 20  e tree root and 
36871 6e 6f 77 20 68 61 73 20 6c 65 73 73 20 74 68 61  now has less tha
36872 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20  n the minimum.  
36873 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c  ** number of cel
36874 6c 73 2c 20 72 65 6d 6f 76 65 20 69 74 20 66 72  ls, remove it fr
36875 6f 6d 20 74 68 65 20 74 72 65 65 2e 20 4f 74 68  om the tree. Oth
36876 65 72 77 69 73 65 2c 20 75 70 64 61 74 65 20 74  erwise, update t
36877 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 20 69 6e 20  he.  ** cell in 
36878 74 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20  the parent node 
36879 73 6f 20 74 68 61 74 20 69 74 20 74 69 67 68 74  so that it tight
3687a 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  ly contains the 
3687b 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 64  updated.  ** nod
3687c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4e  e..  */.  if( pN
3687d 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d 31 20 29 7b  ode->iNode!=1 ){
3687e 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
3687f 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d  pParent = pNode-
36880 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 69 66  >pParent;.    if
36881 28 20 28 70 50 61 72 65 6e 74 2d 3e 69 4e 6f 64  ( (pParent->iNod
36882 65 21 3d 31 20 7c 7c 20 4e 43 45 4c 4c 28 70 50  e!=1 || NCELL(pP
36883 61 72 65 6e 74 29 21 3d 31 29 20 0a 20 20 20 20  arent)!=1) .    
36884 20 26 26 20 28 4e 43 45 4c 4c 28 70 4e 6f 64 65   && (NCELL(pNode
36885 29 3c 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53  )<RTREE_MINCELLS
36886 28 70 52 74 72 65 65 29 29 0a 20 20 20 20 29 7b  (pRtree)).    ){
36887 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 6d 6f  .      rc = remo
36888 76 65 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70  veNode(pRtree, p
36889 4e 6f 64 65 2c 20 69 48 65 69 67 68 74 29 3b 0a  Node, iHeight);.
3688a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3688b 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28   fixBoundingBox(
3688c 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
3688d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
3688e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69  urn rc;.}..stati
3688f 63 20 69 6e 74 20 52 65 69 6e 73 65 72 74 28 0a  c int Reinsert(.
36890 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
36891 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70   .  RtreeNode *p
36892 4e 6f 64 65 2c 20 0a 20 20 52 74 72 65 65 43 65  Node, .  RtreeCe
36893 6c 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *pCell, .  in
36894 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69  t iHeight.){.  i
36895 6e 74 20 2a 61 4f 72 64 65 72 3b 0a 20 20 69 6e  nt *aOrder;.  in
36896 74 20 2a 61 53 70 61 72 65 3b 0a 20 20 52 74 72  t *aSpare;.  Rtr
36897 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20  eeCell *aCell;. 
36898 20 66 6c 6f 61 74 20 2a 61 44 69 73 74 61 6e 63   float *aDistanc
36899 65 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a  e;.  int nCell;.
3689a 20 20 66 6c 6f 61 74 20 61 43 65 6e 74 65 72 43    float aCenterC
3689b 6f 6f 72 64 5b 52 54 52 45 45 5f 4d 41 58 5f 44  oord[RTREE_MAX_D
3689c 49 4d 45 4e 53 49 4f 4e 53 5d 3b 0a 20 20 69 6e  IMENSIONS];.  in
3689d 74 20 69 44 69 6d 3b 0a 20 20 69 6e 74 20 69 69  t iDim;.  int ii
3689e 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
3689f 49 54 45 5f 4f 4b 3b 0a 0a 20 20 6d 65 6d 73 65  ITE_OK;..  memse
368a0 74 28 61 43 65 6e 74 65 72 43 6f 6f 72 64 2c 20  t(aCenterCoord, 
368a1 30 2c 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74 29  0, sizeof(float)
368a2 2a 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  *RTREE_MAX_DIMEN
368a3 53 49 4f 4e 53 29 3b 0a 0a 20 20 6e 43 65 6c 6c  SIONS);..  nCell
368a4 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2b   = NCELL(pNode)+
368a5 31 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  1;..  /* Allocat
368a6 65 20 74 68 65 20 62 75 66 66 65 72 73 20 75 73  e the buffers us
368a7 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 61  ed by this opera
368a8 74 69 6f 6e 2e 20 54 68 65 20 61 6c 6c 6f 63 61  tion. The alloca
368a9 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 6c  tion is.  ** rel
368aa 69 6e 71 75 69 73 68 65 64 20 62 65 66 6f 72 65  inquished before
368ab 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
368ac 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61  eturns..  */.  a
368ad 43 65 6c 6c 20 3d 20 28 52 74 72 65 65 43 65 6c  Cell = (RtreeCel
368ae 6c 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  l *)sqlite3_mall
368af 6f 63 28 6e 43 65 6c 6c 20 2a 20 28 0a 20 20 20  oc(nCell * (.   
368b0 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c   sizeof(RtreeCel
368b1 6c 29 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20  l) +         /* 
368b2 61 43 65 6c 6c 20 61 72 72 61 79 20 2a 2f 0a 20  aCell array */. 
368b3 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 20     sizeof(int)  
368b4 20 20 20 20 20 2b 20 20 20 20 20 20 20 20 20 2f       +         /
368b5 2a 20 61 4f 72 64 65 72 20 61 72 72 61 79 20 2a  * aOrder array *
368b6 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74  /.    sizeof(int
368b7 29 20 20 20 20 20 20 20 2b 20 20 20 20 20 20 20  )       +       
368b8 20 20 2f 2a 20 61 53 70 61 72 65 20 61 72 72 61    /* aSpare arra
368b9 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28  y */.    sizeof(
368ba 66 6c 6f 61 74 29 20 20 20 20 20 20 20 20 20 20  float)          
368bb 20 20 20 20 20 2f 2a 20 61 44 69 73 74 61 6e 63       /* aDistanc
368bc 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 29 29 3b  e array */.  ));
368bd 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b  .  if( !aCell ){
368be 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
368bf 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
368c0 61 4f 72 64 65 72 20 20 20 20 3d 20 28 69 6e 74  aOrder    = (int
368c1 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 5d   *)&aCell[nCell]
368c2 3b 0a 20 20 61 53 70 61 72 65 20 20 20 20 3d 20  ;.  aSpare    = 
368c3 28 69 6e 74 20 2a 29 26 61 4f 72 64 65 72 5b 6e  (int *)&aOrder[n
368c4 43 65 6c 6c 5d 3b 0a 20 20 61 44 69 73 74 61 6e  Cell];.  aDistan
368c5 63 65 20 3d 20 28 66 6c 6f 61 74 20 2a 29 26 61  ce = (float *)&a
368c6 53 70 61 72 65 5b 6e 43 65 6c 6c 5d 3b 0a 0a 20  Spare[nCell];.. 
368c7 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43   for(ii=0; ii<nC
368c8 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ell; ii++){.    
368c9 69 66 28 20 69 69 3d 3d 28 6e 43 65 6c 6c 2d 31  if( ii==(nCell-1
368ca 29 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  ) ){.      memcp
368cb 79 28 26 61 43 65 6c 6c 5b 69 69 5d 2c 20 70 43  y(&aCell[ii], pC
368cc 65 6c 6c 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  ell, sizeof(Rtre
368cd 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20 7d 65 6c  eCell));.    }el
368ce 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65 47 65  se{.      nodeGe
368cf 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e  tCell(pRtree, pN
368d0 6f 64 65 2c 20 69 69 2c 20 26 61 43 65 6c 6c 5b  ode, ii, &aCell[
368d1 69 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ii]);.    }.    
368d2 61 4f 72 64 65 72 5b 69 69 5d 20 3d 20 69 69 3b  aOrder[ii] = ii;
368d3 0a 20 20 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b  .    for(iDim=0;
368d4 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44   iDim<pRtree->nD
368d5 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20 20  im; iDim++){.   
368d6 20 20 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b     aCenterCoord[
368d7 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f 52 44 28  iDim] += DCOORD(
368d8 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64  aCell[ii].aCoord
368d9 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20  [iDim*2]);.     
368da 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44   aCenterCoord[iD
368db 69 6d 5d 20 2b 3d 20 44 43 4f 4f 52 44 28 61 43  im] += DCOORD(aC
368dc 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69  ell[ii].aCoord[i
368dd 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 7d  Dim*2+1]);.    }
368de 0a 20 20 7d 0a 20 20 66 6f 72 28 69 44 69 6d 3d  .  }.  for(iDim=
368df 30 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e  0; iDim<pRtree->
368e0 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20  nDim; iDim++){. 
368e1 20 20 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b     aCenterCoord[
368e2 69 44 69 6d 5d 20 3d 20 61 43 65 6e 74 65 72 43  iDim] = aCenterC
368e3 6f 6f 72 64 5b 69 44 69 6d 5d 2f 28 28 66 6c 6f  oord[iDim]/((flo
368e4 61 74 29 6e 43 65 6c 6c 2a 32 2e 30 29 3b 0a 20  at)nCell*2.0);. 
368e5 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   }..  for(ii=0; 
368e6 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
368e7 0a 20 20 20 20 61 44 69 73 74 61 6e 63 65 5b 69  .    aDistance[i
368e8 69 5d 20 3d 20 30 2e 30 3b 0a 20 20 20 20 66 6f  i] = 0.0;.    fo
368e9 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70  r(iDim=0; iDim<p
368ea 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69  Rtree->nDim; iDi
368eb 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61  m++){.      floa
368ec 74 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52 44  t coord = DCOORD
368ed 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
368ee 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 20 2d 20 0a  d[iDim*2+1]) - .
368ef 20 20 20 20 20 20 20 20 20 20 44 43 4f 4f 52 44            DCOORD
368f0 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
368f1 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20  d[iDim*2]);.    
368f2 20 20 61 44 69 73 74 61 6e 63 65 5b 69 69 5d 20    aDistance[ii] 
368f3 2b 3d 20 28 63 6f 6f 72 64 2d 61 43 65 6e 74 65  += (coord-aCente
368f4 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29 2a 28 63  rCoord[iDim])*(c
368f5 6f 6f 72 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72  oord-aCenterCoor
368f6 64 5b 69 44 69 6d 5d 29 3b 0a 20 20 20 20 7d 0a  d[iDim]);.    }.
368f7 20 20 7d 0a 0a 20 20 53 6f 72 74 42 79 44 69 73    }..  SortByDis
368f8 74 61 6e 63 65 28 61 4f 72 64 65 72 2c 20 6e 43  tance(aOrder, nC
368f9 65 6c 6c 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  ell, aDistance, 
368fa 61 53 70 61 72 65 29 3b 0a 20 20 6e 6f 64 65 5a  aSpare);.  nodeZ
368fb 65 72 6f 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ero(pRtree, pNod
368fc 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b  e);..  for(ii=0;
368fd 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
368fe 26 20 69 69 3c 28 6e 43 65 6c 6c 2d 28 52 54 52  & ii<(nCell-(RTR
368ff 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72  EE_MINCELLS(pRtr
36900 65 65 29 2b 31 29 29 3b 20 69 69 2b 2b 29 7b 0a  ee)+1)); ii++){.
36901 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70      RtreeCell *p
36902 20 3d 20 26 61 43 65 6c 6c 5b 61 4f 72 64 65 72   = &aCell[aOrder
36903 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49  [ii]];.    nodeI
36904 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
36905 2c 20 70 4e 6f 64 65 2c 20 70 29 3b 0a 20 20 20  , pNode, p);.   
36906 20 69 66 28 20 70 2d 3e 69 52 6f 77 69 64 3d 3d   if( p->iRowid==
36907 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 29 7b  pCell->iRowid ){
36908 0a 20 20 20 20 20 20 69 66 28 20 69 48 65 69 67  .      if( iHeig
36909 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ht==0 ){.       
3690a 20 72 63 20 3d 20 72 6f 77 69 64 57 72 69 74 65   rc = rowidWrite
3690b 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77  (pRtree, p->iRow
3690c 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  id, pNode->iNode
3690d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
3690e 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 72          rc = par
3690f 65 6e 74 57 72 69 74 65 28 70 52 74 72 65 65 2c  entWrite(pRtree,
36910 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64   p->iRowid, pNod
36911 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e->iNode);.     
36912 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69   }.    }.  }.  i
36913 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36914 20 29 7b 0a 20 20 20 20 66 69 78 42 6f 75 6e 64   ){.    fixBound
36915 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70  ingBox(pRtree, p
36916 4e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72  Node);.  }.  for
36917 28 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  (; rc==SQLITE_OK
36918 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69   && ii<nCell; ii
36919 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 46 69 6e 64  ++){.    /* Find
3691a 20 61 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72 65   a node to store
3691b 20 74 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20 70   this cell in. p
3691c 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72 72  Node->iNode curr
3691d 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a 20  ently contains. 
3691e 20 20 20 2a 2a 20 74 68 65 20 68 65 69 67 68 74     ** the height
3691f 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65 65   of the sub-tree
36920 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20 63   headed by the c
36921 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ell..    */.    
36922 52 74 72 65 65 4e 6f 64 65 20 2a 70 49 6e 73 65  RtreeNode *pInse
36923 72 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c  rt;.    RtreeCel
36924 6c 20 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61 4f  l *p = &aCell[aO
36925 72 64 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20 72  rder[ii]];.    r
36926 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28 70  c = ChooseLeaf(p
36927 52 74 72 65 65 2c 20 70 2c 20 69 48 65 69 67 68  Rtree, p, iHeigh
36928 74 2c 20 26 70 49 6e 73 65 72 74 29 3b 0a 20 20  t, &pInsert);.  
36929 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3692a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  _OK ){.      int
3692b 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d   rc2;.      rc =
3692c 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c   rtreeInsertCell
3692d 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74  (pRtree, pInsert
3692e 2c 20 70 2c 20 69 48 65 69 67 68 74 29 3b 0a 20  , p, iHeight);. 
3692f 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52       rc2 = nodeR
36930 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
36931 49 6e 73 65 72 74 29 3b 0a 20 20 20 20 20 20 69  Insert);.      i
36932 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36933 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
36934 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20   rc2;.      }.  
36935 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74    }.  }..  sqlit
36936 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b 0a  e3_free(aCell);.
36937 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
36938 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 63 65 6c  /*.** Insert cel
36939 6c 20 70 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64  l pCell into nod
3693a 65 20 70 4e 6f 64 65 2e 20 4e 6f 64 65 20 70 4e  e pNode. Node pN
3693b 6f 64 65 20 69 73 20 74 68 65 20 68 65 61 64 20  ode is the head 
3693c 6f 66 20 61 20 0a 2a 2a 20 73 75 62 74 72 65 65  of a .** subtree
3693d 20 69 48 65 69 67 68 74 20 68 69 67 68 20 28 6c   iHeight high (l
3693e 65 61 66 20 6e 6f 64 65 73 20 68 61 76 65 20 69  eaf nodes have i
3693f 48 65 69 67 68 74 3d 3d 30 29 2e 0a 2a 2f 0a 73  Height==0)..*/.s
36940 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49  tatic int rtreeI
36941 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 52 74 72  nsertCell(.  Rtr
36942 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74  ee *pRtree,.  Rt
36943 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a  reeNode *pNode,.
36944 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65    RtreeCell *pCe
36945 6c 6c 2c 0a 20 20 69 6e 74 20 69 48 65 69 67 68  ll,.  int iHeigh
36946 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t.){.  int rc = 
36947 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
36948 20 69 48 65 69 67 68 74 3e 30 20 29 7b 0a 20 20   iHeight>0 ){.  
36949 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
3694a 69 6c 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f  ild = nodeHashLo
3694b 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 70 43 65  okup(pRtree, pCe
3694c 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20 20  ll->iRowid);.   
3694d 20 69 66 28 20 70 43 68 69 6c 64 20 29 7b 0a 20   if( pChild ){. 
3694e 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65       nodeRelease
3694f 28 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2d  (pRtree, pChild-
36950 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20  >pParent);.     
36951 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70   nodeReference(p
36952 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 70 43 68  Node);.      pCh
36953 69 6c 64 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70  ild->pParent = p
36954 4e 6f 64 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Node;.    }.  }.
36955 20 20 69 66 28 20 6e 6f 64 65 49 6e 73 65 72 74    if( nodeInsert
36956 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f  Cell(pRtree, pNo
36957 64 65 2c 20 70 43 65 6c 6c 29 20 29 7b 0a 23 69  de, pCell) ){.#i
36958 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54  f VARIANT_RSTART
36959 52 45 45 5f 52 45 49 4e 53 45 52 54 0a 20 20 20  REE_REINSERT.   
3695a 20 69 66 28 20 69 48 65 69 67 68 74 3c 3d 70 52   if( iHeight<=pR
3695b 74 72 65 65 2d 3e 69 52 65 69 6e 73 65 72 74 48  tree->iReinsertH
3695c 65 69 67 68 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e  eight || pNode->
3695d 69 4e 6f 64 65 3d 3d 31 29 7b 0a 20 20 20 20 20  iNode==1){.     
3695e 20 72 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28   rc = SplitNode(
3695f 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
36960 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a  Cell, iHeight);.
36961 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36962 20 70 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65   pRtree->iReinse
36963 72 74 48 65 69 67 68 74 20 3d 20 69 48 65 69 67  rtHeight = iHeig
36964 68 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 52  ht;.      rc = R
36965 65 69 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20  einsert(pRtree, 
36966 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48  pNode, pCell, iH
36967 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 23 65  eight);.    }.#e
36968 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 53 70 6c  lse.    rc = Spl
36969 69 74 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70  itNode(pRtree, p
3696a 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65  Node, pCell, iHe
3696b 69 67 68 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20  ight);.#endif.  
3696c 7d 65 6c 73 65 7b 0a 20 20 20 20 41 64 6a 75 73  }else{.    Adjus
3696d 74 54 72 65 65 28 70 52 74 72 65 65 2c 20 70 4e  tTree(pRtree, pN
3696e 6f 64 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  ode, pCell);.   
3696f 20 69 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20   if( iHeight==0 
36970 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 6f  ){.      rc = ro
36971 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65 2c  widWrite(pRtree,
36972 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20   pCell->iRowid, 
36973 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20  pNode->iNode);. 
36974 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
36975 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69 74 65  rc = parentWrite
36976 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e  (pRtree, pCell->
36977 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69  iRowid, pNode->i
36978 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Node);.    }.  }
36979 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3697a 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 69 6e  .static int rein
3697b 73 65 72 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28  sertNodeContent(
3697c 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
3697d 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29  treeNode *pNode)
3697e 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e  {.  int ii;.  in
3697f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
36980 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20  ;.  int nCell = 
36981 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20  NCELL(pNode);.. 
36982 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53   for(ii=0; rc==S
36983 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e  QLITE_OK && ii<n
36984 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  Cell; ii++){.   
36985 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 49 6e 73   RtreeNode *pIns
36986 65 72 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65  ert;.    RtreeCe
36987 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 6e 6f 64  ll cell;.    nod
36988 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  eGetCell(pRtree,
36989 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c   pNode, ii, &cel
3698a 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64  l);..    /* Find
3698b 20 61 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72 65   a node to store
3698c 20 74 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20 70   this cell in. p
3698d 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72 72  Node->iNode curr
3698e 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a 20  ently contains. 
3698f 20 20 20 2a 2a 20 74 68 65 20 68 65 69 67 68 74     ** the height
36990 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65 65   of the sub-tree
36991 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20 63   headed by the c
36992 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ell..    */.    
36993 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28  rc = ChooseLeaf(
36994 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70  pRtree, &cell, p
36995 4e 6f 64 65 2d 3e 69 4e 6f 64 65 2c 20 26 70 49  Node->iNode, &pI
36996 6e 73 65 72 74 29 3b 0a 20 20 20 20 69 66 28 20  nsert);.    if( 
36997 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
36998 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a  .      int rc2;.
36999 20 20 20 20 20 20 72 63 20 3d 20 72 74 72 65 65        rc = rtree
3699a 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65  InsertCell(pRtre
3699b 65 2c 20 70 49 6e 73 65 72 74 2c 20 26 63 65 6c  e, pInsert, &cel
3699c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29  l, pNode->iNode)
3699d 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f  ;.      rc2 = no
3699e 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
3699f 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20  , pInsert);.    
369a0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
369a1 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
369a2 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d  c = rc2;.      }
369a3 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
369a4 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
369a5 20 53 65 6c 65 63 74 20 61 20 63 75 72 72 65 6e   Select a curren
369a6 74 6c 79 20 75 6e 75 73 65 64 20 72 6f 77 69 64  tly unused rowid
369a7 20 66 6f 72 20 61 20 6e 65 77 20 72 2d 74 72 65   for a new r-tre
369a8 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 61  e record..*/.sta
369a9 74 69 63 20 69 6e 74 20 6e 65 77 52 6f 77 69 64  tic int newRowid
369aa 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
369ab 69 36 34 20 2a 70 69 52 6f 77 69 64 29 7b 0a 20  i64 *piRowid){. 
369ac 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
369ad 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 52 74  e3_bind_null(pRt
369ae 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
369af 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  , 1);.  sqlite3_
369b0 62 69 6e 64 5f 6e 75 6c 6c 28 70 52 74 72 65 65  bind_null(pRtree
369b1 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20 32  ->pWriteRowid, 2
369b2 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65  );.  sqlite3_ste
369b3 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  p(pRtree->pWrite
369b4 52 6f 77 69 64 29 3b 0a 20 20 72 63 20 3d 20 73  Rowid);.  rc = s
369b5 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
369b6 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
369b7 29 3b 0a 20 20 2a 70 69 52 6f 77 69 64 20 3d 20  );.  *piRowid = 
369b8 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
369b9 65 72 74 5f 72 6f 77 69 64 28 70 52 74 72 65 65  ert_rowid(pRtree
369ba 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20  ->db);.  return 
369bb 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e  rc;.}..#ifndef N
369bc 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74  DEBUG.static int
369bd 20 68 61 73 68 49 73 45 6d 70 74 79 28 52 74 72   hashIsEmpty(Rtr
369be 65 65 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 69  ee *pRtree){.  i
369bf 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d  nt ii;.  for(ii=
369c0 30 3b 20 69 69 3c 48 41 53 48 53 49 5a 45 3b 20  0; ii<HASHSIZE; 
369c1 69 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72  ii++){.    asser
369c2 74 28 20 21 70 52 74 72 65 65 2d 3e 61 48 61 73  t( !pRtree->aHas
369c3 68 5b 69 69 5d 20 29 3b 0a 20 20 7d 0a 20 20 72  h[ii] );.  }.  r
369c4 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
369c5 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 55 70  f../*.** The xUp
369c6 64 61 74 65 20 6d 65 74 68 6f 64 20 66 6f 72 20  date method for 
369c7 72 74 72 65 65 20 6d 6f 64 75 6c 65 20 76 69 72  rtree module vir
369c8 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a  tual tables..*/.
369c9 69 6e 74 20 72 74 72 65 65 55 70 64 61 74 65 28  int rtreeUpdate(
369ca 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
369cb 2a 70 56 74 61 62 2c 20 0a 20 20 69 6e 74 20 6e  *pVtab, .  int n
369cc 44 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 33  Data, .  sqlite3
369cd 5f 76 61 6c 75 65 20 2a 2a 61 7a 44 61 74 61 2c  _value **azData,
369ce 20 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34   .  sqlite_int64
369cf 20 2a 70 52 6f 77 69 64 0a 29 7b 0a 20 20 52 74   *pRowid.){.  Rt
369d0 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
369d1 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20  tree *)pVtab;.  
369d2 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
369d3 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65 66 65  OK;..  rtreeRefe
369d4 72 65 6e 63 65 28 70 52 74 72 65 65 29 3b 0a 0a  rence(pRtree);..
369d5 20 20 61 73 73 65 72 74 28 6e 44 61 74 61 3e 3d    assert(nData>=
369d6 31 29 3b 0a 20 20 61 73 73 65 72 74 28 68 61 73  1);.  assert(has
369d7 68 49 73 45 6d 70 74 79 28 70 52 74 72 65 65 29  hIsEmpty(pRtree)
369d8 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 7a 44 61  );..  /* If azDa
369d9 74 61 5b 30 5d 20 69 73 20 6e 6f 74 20 61 6e 20  ta[0] is not an 
369da 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20  SQL NULL value, 
369db 69 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20  it is the rowid 
369dc 6f 66 20 61 0a 20 20 2a 2a 20 72 65 63 6f 72 64  of a.  ** record
369dd 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20   to delete from 
369de 74 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65  the r-tree table
369df 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
369e0 62 6c 6f 63 6b 20 64 6f 65 73 0a 20 20 2a 2a 20  block does.  ** 
369e1 6a 75 73 74 20 74 68 61 74 2e 0a 20 20 2a 2f 0a  just that..  */.
369e2 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
369e3 6c 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b  lue_type(azData[
369e4 30 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])!=SQLITE_NULL
369e5 20 29 7b 0a 20 20 20 20 69 36 34 20 69 44 65 6c   ){.    i64 iDel
369e6 65 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ete;            
369e7 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64      /* The rowid
369e8 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
369e9 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65    RtreeNode *pLe
369ea 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  af;           /*
369eb 20 4c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61   Leaf node conta
369ec 69 6e 69 6e 67 20 72 65 63 6f 72 64 20 69 44 65  ining record iDe
369ed 6c 65 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  lete */.    int 
369ee 69 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  iCell;          
369ef 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
369f0 20 6f 66 20 69 44 65 6c 65 74 65 20 63 65 6c 6c   of iDelete cell
369f1 20 69 6e 20 70 4c 65 61 66 20 2a 2f 0a 20 20 20   in pLeaf */.   
369f2 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f   RtreeNode *pRoo
369f3 74 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69  t;..    /* Obtai
369f4 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  n a reference to
369f5 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 74   the root node t
369f6 6f 20 69 6e 69 74 69 61 6c 69 73 65 20 52 74 72  o initialise Rtr
369f7 65 65 2e 69 44 65 70 74 68 20 2a 2f 0a 20 20 20  ee.iDepth */.   
369f8 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72   rc = nodeAcquir
369f9 65 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20  e(pRtree, 1, 0, 
369fa 26 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 2f 2a  &pRoot);..    /*
369fb 20 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65   Obtain a refere
369fc 6e 63 65 20 74 6f 20 74 68 65 20 6c 65 61 66 20  nce to the leaf 
369fd 6e 6f 64 65 20 74 68 61 74 20 63 6f 6e 74 61 69  node that contai
369fe 6e 73 20 74 68 65 20 65 6e 74 72 79 20 0a 20 20  ns the entry .  
369ff 20 20 2a 2a 20 61 62 6f 75 74 20 74 6f 20 62 65    ** about to be
36a00 20 64 65 6c 65 74 65 64 2e 20 0a 20 20 20 20 2a   deleted. .    *
36a01 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
36a02 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
36a03 20 69 44 65 6c 65 74 65 20 3d 20 73 71 6c 69 74   iDelete = sqlit
36a04 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61  e3_value_int64(a
36a05 7a 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20  zData[0]);.     
36a06 20 72 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f   rc = findLeafNo
36a07 64 65 28 70 52 74 72 65 65 2c 20 69 44 65 6c 65  de(pRtree, iDele
36a08 74 65 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20  te, &pLeaf);.   
36a09 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74   }..    /* Delet
36a0a 65 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 71 75  e the cell in qu
36a0b 65 73 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  estion from the 
36a0c 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20  leaf node. */.  
36a0d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
36a0e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  _OK ){.      int
36a0f 20 72 63 32 3b 0a 20 20 20 20 20 20 69 43 65 6c   rc2;.      iCel
36a10 6c 20 3d 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  l = nodeRowidInd
36a11 65 78 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  ex(pRtree, pLeaf
36a12 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20 20 20 20  , iDelete);.    
36a13 20 20 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c    rc = deleteCel
36a14 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c  l(pRtree, pLeaf,
36a15 20 69 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20   iCell, 0);.    
36a16 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65    rc2 = nodeRele
36a17 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 61  ase(pRtree, pLea
36a18 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  f);.      if( rc
36a19 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
36a1a 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
36a1b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
36a1c 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68      /* Delete th
36a1d 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
36a1e 65 6e 74 72 79 20 69 6e 20 74 68 65 20 3c 72 74  entry in the <rt
36a1f 72 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65  ree>_rowid table
36a20 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d  . */.    if( rc=
36a21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
36a22 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
36a23 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
36a24 44 65 6c 65 74 65 52 6f 77 69 64 2c 20 31 2c 20  DeleteRowid, 1, 
36a25 69 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20  iDelete);.      
36a26 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74  sqlite3_step(pRt
36a27 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69  ree->pDeleteRowi
36a28 64 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  d);.      rc = s
36a29 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
36a2a 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69  ree->pDeleteRowi
36a2b 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  d);.    }..    /
36a2c 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72  * Check if the r
36a2d 6f 6f 74 20 6e 6f 64 65 20 6e 6f 77 20 68 61 73  oot node now has
36a2e 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 69   exactly one chi
36a2f 6c 64 2e 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76  ld. If so, remov
36a30 65 0a 20 20 20 20 2a 2a 20 69 74 2c 20 73 63 68  e.    ** it, sch
36a31 65 64 75 6c 65 20 74 68 65 20 63 6f 6e 74 65 6e  edule the conten
36a32 74 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  ts of the child 
36a33 66 6f 72 20 72 65 69 6e 73 65 72 74 69 6f 6e 20  for reinsertion 
36a34 61 6e 64 20 0a 20 20 20 20 2a 2a 20 72 65 64 75  and .    ** redu
36a35 63 65 20 74 68 65 20 74 72 65 65 20 68 65 69 67  ce the tree heig
36a36 68 74 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a  ht by one..    *
36a37 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73  *.    ** This is
36a38 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 63   equivalent to c
36a39 6f 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  opying the conte
36a3a 6e 74 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64  nts of the child
36a3b 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65   into.    ** the
36a3c 20 72 6f 6f 74 20 6e 6f 64 65 20 28 74 68 65 20   root node (the 
36a3d 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 47  operation that G
36a3e 75 74 6d 61 6e 27 73 20 70 61 70 65 72 20 73 61  utman's paper sa
36a3f 79 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 0a 20  ys to perform . 
36a40 20 20 20 2a 2a 20 69 6e 20 74 68 69 73 20 73 63     ** in this sc
36a41 65 6e 61 72 69 6f 29 2e 0a 20 20 20 20 2a 2f 0a  enario)..    */.
36a42 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
36a43 54 45 5f 4f 4b 20 26 26 20 70 52 74 72 65 65 2d  TE_OK && pRtree-
36a44 3e 69 44 65 70 74 68 3e 30 20 29 7b 0a 20 20 20  >iDepth>0 ){.   
36a45 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
36a46 45 5f 4f 4b 20 26 26 20 4e 43 45 4c 4c 28 70 52  E_OK && NCELL(pR
36a47 6f 6f 74 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20  oot)==1 ){.     
36a48 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43     RtreeNode *pC
36a49 68 69 6c 64 3b 0a 20 20 20 20 20 20 20 20 69 36  hild;.        i6
36a4a 34 20 69 43 68 69 6c 64 20 3d 20 6e 6f 64 65 47  4 iChild = nodeG
36a4b 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
36a4c 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20  pRoot, 0);.     
36a4d 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75     rc = nodeAcqu
36a4e 69 72 65 28 70 52 74 72 65 65 2c 20 69 43 68 69  ire(pRtree, iChi
36a4f 6c 64 2c 20 70 52 6f 6f 74 2c 20 26 70 43 68 69  ld, pRoot, &pChi
36a50 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ld);.        if(
36a51 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
36a52 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
36a53 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72   removeNode(pRtr
36a54 65 65 2c 20 70 43 68 69 6c 64 2c 20 70 52 74 72  ee, pChild, pRtr
36a55 65 65 2d 3e 69 44 65 70 74 68 2d 31 29 3b 0a 20  ee->iDepth-1);. 
36a56 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
36a57 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
36a58 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
36a59 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 2d  pRtree->iDepth--
36a5a 3b 0a 20 20 20 20 20 20 20 20 20 20 77 72 69 74  ;.          writ
36a5b 65 49 6e 74 31 36 28 70 52 6f 6f 74 2d 3e 7a 44  eInt16(pRoot->zD
36a5c 61 74 61 2c 20 70 52 74 72 65 65 2d 3e 69 44 65  ata, pRtree->iDe
36a5d 70 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pth);.          
36a5e 70 52 6f 6f 74 2d 3e 69 73 44 69 72 74 79 20 3d  pRoot->isDirty =
36a5f 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
36a60 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
36a61 20 2f 2a 20 52 65 2d 69 6e 73 65 72 74 20 74 68   /* Re-insert th
36a62 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e  e contents of an
36a63 79 20 75 6e 64 65 72 66 75 6c 6c 20 6e 6f 64 65  y underfull node
36a64 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  s removed from t
36a65 68 65 20 74 72 65 65 2e 20 2a 2f 0a 20 20 20 20  he tree. */.    
36a66 66 6f 72 28 70 4c 65 61 66 3d 70 52 74 72 65 65  for(pLeaf=pRtree
36a67 2d 3e 70 44 65 6c 65 74 65 64 3b 20 70 4c 65 61  ->pDeleted; pLea
36a68 66 3b 20 70 4c 65 61 66 3d 70 52 74 72 65 65 2d  f; pLeaf=pRtree-
36a69 3e 70 44 65 6c 65 74 65 64 29 7b 0a 20 20 20 20  >pDeleted){.    
36a6a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
36a6b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
36a6c 63 20 3d 20 72 65 69 6e 73 65 72 74 4e 6f 64 65  c = reinsertNode
36a6d 43 6f 6e 74 65 6e 74 28 70 52 74 72 65 65 2c 20  Content(pRtree, 
36a6e 70 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 7d 0a  pLeaf);.      }.
36a6f 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e 70 44        pRtree->pD
36a70 65 6c 65 74 65 64 20 3d 20 70 4c 65 61 66 2d 3e  eleted = pLeaf->
36a71 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c  pNext;.      sql
36a72 69 74 65 33 5f 66 72 65 65 28 70 4c 65 61 66 29  ite3_free(pLeaf)
36a73 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
36a74 52 65 6c 65 61 73 65 20 74 68 65 20 72 65 66 65  Release the refe
36a75 72 65 6e 63 65 20 74 6f 20 74 68 65 20 72 6f 6f  rence to the roo
36a76 74 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 69  t node. */.    i
36a77 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36a78 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e   ){.      rc = n
36a79 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
36a7a 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  e, pRoot);.    }
36a7b 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65  else{.      node
36a7c 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
36a7d 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pRoot);.    }.  
36a7e 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  }..  /* If the a
36a7f 7a 44 61 74 61 5b 5d 20 61 72 72 61 79 20 63 6f  zData[] array co
36a80 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e  ntains more than
36a81 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c 20 65 6c   one element, el
36a82 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 28 61 7a 44  ements.  ** (azD
36a83 61 74 61 5b 32 5d 2e 2e 61 7a 44 61 74 61 5b 61  ata[2]..azData[a
36a84 72 67 63 2d 31 5d 29 20 63 6f 6e 74 61 69 6e 20  rgc-1]) contain 
36a85 61 20 6e 65 77 20 72 65 63 6f 72 64 20 74 6f 20  a new record to 
36a86 69 6e 73 65 72 74 20 69 6e 74 6f 0a 20 20 2a 2a  insert into.  **
36a87 20 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75   the r-tree stru
36a88 63 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  cture..  */.  if
36a89 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
36a8a 26 26 20 6e 44 61 74 61 3e 31 20 29 7b 0a 20 20  && nData>1 ){.  
36a8b 20 20 2f 2a 20 49 6e 73 65 72 74 20 61 20 6e 65    /* Insert a ne
36a8c 77 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68  w record into th
36a8d 65 20 72 2d 74 72 65 65 20 2a 2f 0a 20 20 20 20  e r-tree */.    
36a8e 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a  RtreeCell cell;.
36a8f 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20      int ii;.    
36a90 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66  RtreeNode *pLeaf
36a91 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61  ;..    /* Popula
36a92 74 65 20 74 68 65 20 63 65 6c 6c 2e 61 43 6f 6f  te the cell.aCoo
36a93 72 64 5b 5d 20 61 72 72 61 79 2e 20 54 68 65 20  rd[] array. The 
36a94 66 69 72 73 74 20 63 6f 6f 72 64 69 6e 61 74 65  first coordinate
36a95 20 69 73 20 61 7a 44 61 74 61 5b 33 5d 2e 20 2a   is azData[3]. *
36a96 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 44  /.    assert( nD
36a97 61 74 61 3d 3d 28 70 52 74 72 65 65 2d 3e 6e 44  ata==(pRtree->nD
36a98 69 6d 2a 32 20 2b 20 33 29 20 29 3b 0a 20 20 20  im*2 + 3) );.   
36a99 20 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f   if( pRtree->eCo
36a9a 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43  ordType==RTREE_C
36a9b 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20  OORD_REAL32 ){. 
36a9c 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
36a9d 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  i<(pRtree->nDim*
36a9e 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20  2); ii+=2){.    
36a9f 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b      cell.aCoord[
36aa0 69 69 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73  ii].f = (float)s
36aa1 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
36aa2 62 6c 65 28 61 7a 44 61 74 61 5b 69 69 2b 33 5d  ble(azData[ii+3]
36aa3 29 3b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e  );.        cell.
36aa4 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 3d  aCoord[ii+1].f =
36aa5 20 28 66 6c 6f 61 74 29 73 71 6c 69 74 65 33 5f   (float)sqlite3_
36aa6 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 7a 44  value_double(azD
36aa7 61 74 61 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20  ata[ii+4]);.    
36aa8 20 20 20 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f      if( cell.aCo
36aa9 6f 72 64 5b 69 69 5d 2e 66 3e 63 65 6c 6c 2e 61  ord[ii].f>cell.a
36aaa 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 29 7b  Coord[ii+1].f ){
36aab 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
36aac 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
36aad 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  T;.          got
36aae 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20  o constraint;.  
36aaf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
36ab0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36ab1 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70   for(ii=0; ii<(p
36ab2 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20  Rtree->nDim*2); 
36ab3 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20  ii+=2){.        
36ab4 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e  cell.aCoord[ii].
36ab5 69 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  i = sqlite3_valu
36ab6 65 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b  e_int(azData[ii+
36ab7 33 5d 29 3b 0a 20 20 20 20 20 20 20 20 63 65 6c  3]);.        cel
36ab8 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69  l.aCoord[ii+1].i
36ab9 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
36aba 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 34  _int(azData[ii+4
36abb 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]);.        if( 
36abc 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e  cell.aCoord[ii].
36abd 69 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69  i>cell.aCoord[ii
36abe 2b 31 5d 2e 69 20 29 7b 0a 20 20 20 20 20 20 20  +1].i ){.       
36abf 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
36ac0 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20  ONSTRAINT;.     
36ac1 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72       goto constr
36ac2 61 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  aint;.        }.
36ac3 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
36ac4 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
36ac5 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
36ac6 65 20 6e 65 77 20 72 6f 77 2e 20 2a 2f 0a 20 20  e new row. */.  
36ac7 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
36ac8 6c 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b  lue_type(azData[
36ac9 32 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  2])==SQLITE_NULL
36aca 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e   ){.      rc = n
36acb 65 77 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  ewRowid(pRtree, 
36acc 26 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20  &cell.iRowid);. 
36acd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
36ace 63 65 6c 6c 2e 69 52 6f 77 69 64 20 3d 20 73 71  cell.iRowid = sq
36acf 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
36ad0 34 28 61 7a 44 61 74 61 5b 32 5d 29 3b 0a 20 20  4(azData[2]);.  
36ad1 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
36ad2 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
36ad3 52 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 63 65  ReadRowid, 1, ce
36ad4 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20  ll.iRowid);.    
36ad5 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57    if( SQLITE_ROW
36ad6 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  ==sqlite3_step(p
36ad7 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69  Rtree->pReadRowi
36ad8 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  d) ){.        sq
36ad9 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
36ada 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b  ee->pReadRowid);
36adb 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
36adc 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b  LITE_CONSTRAINT;
36add 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f  .        goto co
36ade 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20  nstraint;.      
36adf 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  }.      rc = sql
36ae0 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
36ae1 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
36ae2 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72      }..    if( r
36ae3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
36ae4 20 20 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73        rc = Choos
36ae5 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20 26 63  eLeaf(pRtree, &c
36ae6 65 6c 6c 2c 20 30 2c 20 26 70 4c 65 61 66 29 3b  ell, 0, &pLeaf);
36ae7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
36ae8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
36ae9 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20        int rc2;. 
36aea 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52 65       pRtree->iRe
36aeb 69 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20 2d  insertHeight = -
36aec 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74  1;.      rc = rt
36aed 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  reeInsertCell(pR
36aee 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 26 63 65  tree, pLeaf, &ce
36aef 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63  ll, 0);.      rc
36af0 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  2 = nodeRelease(
36af1 70 52 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a  pRtree, pLeaf);.
36af2 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
36af3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
36af4 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20     rc = rc2;.   
36af5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
36af6 63 6f 6e 73 74 72 61 69 6e 74 3a 0a 20 20 72 74  constraint:.  rt
36af7 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  reeRelease(pRtre
36af8 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
36af9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 52  .}../*.** The xR
36afa 65 6e 61 6d 65 20 6d 65 74 68 6f 64 20 66 6f 72  ename method for
36afb 20 72 74 72 65 65 20 6d 6f 64 75 6c 65 20 76 69   rtree module vi
36afc 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f  rtual tables..*/
36afd 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
36afe 65 52 65 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  eRename(sqlite3_
36aff 76 74 61 62 20 2a 70 56 74 61 62 2c 20 63 6f 6e  vtab *pVtab, con
36b00 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 4e 61 6d  st char *zNewNam
36b01 65 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74  e){.  Rtree *pRt
36b02 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70  ree = (Rtree *)p
36b03 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d  Vtab;.  int rc =
36b04 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
36b05 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71   char *zSql = sq
36b06 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20  lite3_mprintf(. 
36b07 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20     "ALTER TABLE 
36b08 25 51 2e 27 25 71 5f 6e 6f 64 65 27 20 20 20 52  %Q.'%q_node'   R
36b09 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 6e 6f  ENAME TO \"%w_no
36b0a 64 65 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45  de\";".    "ALTE
36b0b 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 70  R TABLE %Q.'%q_p
36b0c 61 72 65 6e 74 27 20 52 45 4e 41 4d 45 20 54 4f  arent' RENAME TO
36b0d 20 5c 22 25 77 5f 70 61 72 65 6e 74 5c 22 3b 22   \"%w_parent\";"
36b0e 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c  .    "ALTER TABL
36b0f 45 20 25 51 2e 27 25 71 5f 72 6f 77 69 64 27 20  E %Q.'%q_rowid' 
36b10 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f   RENAME TO \"%w_
36b11 72 6f 77 69 64 5c 22 3b 22 0a 20 20 20 20 2c 20  rowid\";".    , 
36b12 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74  pRtree->zDb, pRt
36b13 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77  ree->zName, zNew
36b14 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74 72  Name .    , pRtr
36b15 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d  ee->zDb, pRtree-
36b16 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65  >zName, zNewName
36b17 20 0a 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e   .    , pRtree->
36b18 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
36b19 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 0a 20 20 29  me, zNewName.  )
36b1a 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a  ;.  if( zSql ){.
36b1b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
36b1c 5f 65 78 65 63 28 70 52 74 72 65 65 2d 3e 64 62  _exec(pRtree->db
36b1d 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29  , zSql, 0, 0, 0)
36b1e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
36b1f 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20  ee(zSql);.  }.  
36b20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74  return rc;.}..st
36b21 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 6f 64  atic sqlite3_mod
36b22 75 6c 65 20 72 74 72 65 65 4d 6f 64 75 6c 65 20  ule rtreeModule 
36b23 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20 20 20 20  = {.  0,        
36b24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b25 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
36b26 20 20 72 74 72 65 65 43 72 65 61 74 65 2c 20 20    rtreeCreate,  
36b27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36b28 20 78 43 72 65 61 74 65 20 2d 20 63 72 65 61 74   xCreate - creat
36b29 65 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72  e a table */.  r
36b2a 74 72 65 65 43 6f 6e 6e 65 63 74 2c 20 20 20 20  treeConnect,    
36b2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
36b2c 6f 6e 6e 65 63 74 20 2d 20 63 6f 6e 6e 65 63 74  onnect - connect
36b2d 20 74 6f 20 61 6e 20 65 78 69 73 74 69 6e 67 20   to an existing 
36b2e 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65  table */.  rtree
36b2f 42 65 73 74 49 6e 64 65 78 2c 20 20 20 20 20 20  BestIndex,      
36b30 20 20 20 20 20 20 20 2f 2a 20 78 42 65 73 74 49         /* xBestI
36b31 6e 64 65 78 20 2d 20 44 65 74 65 72 6d 69 6e 65  ndex - Determine
36b32 20 73 65 61 72 63 68 20 73 74 72 61 74 65 67 79   search strategy
36b33 20 2a 2f 0a 20 20 72 74 72 65 65 44 69 73 63 6f   */.  rtreeDisco
36b34 6e 6e 65 63 74 2c 20 20 20 20 20 20 20 20 20 20  nnect,          
36b35 20 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74    /* xDisconnect
36b36 20 2d 20 44 69 73 63 6f 6e 6e 65 63 74 20 66 72   - Disconnect fr
36b37 6f 6d 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20  om a table */.  
36b38 72 74 72 65 65 44 65 73 74 72 6f 79 2c 20 20 20  rtreeDestroy,   
36b39 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
36b3a 44 65 73 74 72 6f 79 20 2d 20 44 72 6f 70 20 61  Destroy - Drop a
36b3b 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65   table */.  rtre
36b3c 65 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  eOpen,          
36b3d 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e          /* xOpen
36b3e 20 2d 20 6f 70 65 6e 20 61 20 63 75 72 73 6f 72   - open a cursor
36b3f 20 2a 2f 0a 20 20 72 74 72 65 65 43 6c 6f 73 65   */.  rtreeClose
36b40 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36b41 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2d 20 63 6c    /* xClose - cl
36b42 6f 73 65 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a  ose a cursor */.
36b43 20 20 72 74 72 65 65 46 69 6c 74 65 72 2c 20 20    rtreeFilter,  
36b44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36b45 20 78 46 69 6c 74 65 72 20 2d 20 63 6f 6e 66 69   xFilter - confi
36b46 67 75 72 65 20 73 63 61 6e 20 63 6f 6e 73 74 72  gure scan constr
36b47 61 69 6e 74 73 20 2a 2f 0a 20 20 72 74 72 65 65  aints */.  rtree
36b48 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20  Next,           
36b49 20 20 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20         /* xNext 
36b4a 2d 20 61 64 76 61 6e 63 65 20 61 20 63 75 72 73  - advance a curs
36b4b 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65 45 6f 66  or */.  rtreeEof
36b4c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36b4d 20 20 20 20 2f 2a 20 78 45 6f 66 20 2a 2f 0a 20      /* xEof */. 
36b4e 20 72 74 72 65 65 43 6f 6c 75 6d 6e 2c 20 20 20   rtreeColumn,   
36b4f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36b50 78 43 6f 6c 75 6d 6e 20 2d 20 72 65 61 64 20 64  xColumn - read d
36b51 61 74 61 20 2a 2f 0a 20 20 72 74 72 65 65 52 6f  ata */.  rtreeRo
36b52 77 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  wid,            
36b53 20 20 20 20 20 2f 2a 20 78 52 6f 77 69 64 20 2d       /* xRowid -
36b54 20 72 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20   read data */.  
36b55 72 74 72 65 65 55 70 64 61 74 65 2c 20 20 20 20  rtreeUpdate,    
36b56 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
36b57 55 70 64 61 74 65 20 2d 20 77 72 69 74 65 20 64  Update - write d
36b58 61 74 61 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ata */.  0,     
36b59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b5a 20 20 20 20 20 2f 2a 20 78 42 65 67 69 6e 20 2d       /* xBegin -
36b5b 20 62 65 67 69 6e 20 74 72 61 6e 73 61 63 74 69   begin transacti
36b5c 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  on */.  0,      
36b5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b5e 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2d 20 73      /* xSync - s
36b5f 79 6e 63 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ync transaction 
36b60 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
36b61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b62 20 2f 2a 20 78 43 6f 6d 6d 69 74 20 2d 20 63 6f   /* xCommit - co
36b63 6d 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  mmit transaction
36b64 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
36b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b66 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 2d    /* xRollback -
36b67 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61   rollback transa
36b68 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20  ction */.  0,   
36b69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b6a 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 64 46         /* xFindF
36b6b 75 6e 63 74 69 6f 6e 20 2d 20 66 75 6e 63 74 69  unction - functi
36b6c 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 20 2a  on overloading *
36b6d 2f 0a 20 20 72 74 72 65 65 52 65 6e 61 6d 65 20  /.  rtreeRename 
36b6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b6f 2f 2a 20 78 52 65 6e 61 6d 65 20 2d 20 72 65 6e  /* xRename - ren
36b70 61 6d 65 20 74 68 65 20 74 61 62 6c 65 20 2a 2f  ame the table */
36b71 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  .};..static int 
36b72 72 74 72 65 65 53 71 6c 49 6e 69 74 28 0a 20 20  rtreeSqlInit(.  
36b73 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
36b74 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
36b75 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
36b76 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b, .  const char
36b77 20 2a 7a 50 72 65 66 69 78 2c 20 0a 20 20 69 6e   *zPrefix, .  in
36b78 74 20 69 73 43 72 65 61 74 65 0a 29 7b 0a 20 20  t isCreate.){.  
36b79 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
36b7a 4f 4b 3b 0a 0a 20 20 23 64 65 66 69 6e 65 20 4e  OK;..  #define N
36b7b 5f 53 54 41 54 45 4d 45 4e 54 20 39 0a 20 20 73  _STATEMENT 9.  s
36b7c 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
36b7d 20 2a 61 7a 53 71 6c 5b 4e 5f 53 54 41 54 45 4d   *azSql[N_STATEM
36b7e 45 4e 54 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a 20  ENT] = {.    /* 
36b7f 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74  Read and write t
36b80 68 65 20 78 78 78 5f 6e 6f 64 65 20 74 61 62 6c  he xxx_node tabl
36b81 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43 54  e */.    "SELECT
36b82 20 64 61 74 61 20 46 52 4f 4d 20 27 25 71 27 2e   data FROM '%q'.
36b83 27 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20  '%q_node' WHERE 
36b84 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20  nodeno = :1",.  
36b85 20 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50    "INSERT OR REP
36b86 4c 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27  LACE INTO '%q'.'
36b87 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53 28  %q_node' VALUES(
36b88 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44  :1, :2)",.    "D
36b89 45 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e  ELETE FROM '%q'.
36b8a 27 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20  '%q_node' WHERE 
36b8b 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 0a 20  nodeno = :1",.. 
36b8c 20 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77     /* Read and w
36b8d 72 69 74 65 20 74 68 65 20 78 78 78 5f 72 6f 77  rite the xxx_row
36b8e 69 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  id table */.    
36b8f 22 53 45 4c 45 43 54 20 6e 6f 64 65 6e 6f 20 46  "SELECT nodeno F
36b90 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f 77  ROM '%q'.'%q_row
36b91 69 64 27 20 57 48 45 52 45 20 72 6f 77 69 64 20  id' WHERE rowid 
36b92 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45  = :1",.    "INSE
36b93 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e  RT OR REPLACE IN
36b94 54 4f 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69  TO '%q'.'%q_rowi
36b95 64 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a 32  d' VALUES(:1, :2
36b96 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45 20  )",.    "DELETE 
36b97 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f  FROM '%q'.'%q_ro
36b98 77 69 64 27 20 57 48 45 52 45 20 72 6f 77 69 64  wid' WHERE rowid
36b99 20 3d 20 3a 31 22 2c 0a 0a 20 20 20 20 2f 2a 20   = :1",..    /* 
36b9a 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74  Read and write t
36b9b 68 65 20 78 78 78 5f 70 61 72 65 6e 74 20 74 61  he xxx_parent ta
36b9c 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45  ble */.    "SELE
36b9d 43 54 20 70 61 72 65 6e 74 6e 6f 64 65 20 46 52  CT parentnode FR
36b9e 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65  OM '%q'.'%q_pare
36b9f 6e 74 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f  nt' WHERE nodeno
36ba0 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53   = :1",.    "INS
36ba1 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49  ERT OR REPLACE I
36ba2 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 70 61 72  NTO '%q'.'%q_par
36ba3 65 6e 74 27 20 56 41 4c 55 45 53 28 3a 31 2c 20  ent' VALUES(:1, 
36ba4 3a 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54  :2)",.    "DELET
36ba5 45 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f  E FROM '%q'.'%q_
36ba6 70 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e 6f  parent' WHERE no
36ba7 64 65 6e 6f 20 3d 20 3a 31 22 0a 20 20 7d 3b 0a  deno = :1".  };.
36ba8 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
36ba9 2a 61 70 70 53 74 6d 74 5b 4e 5f 53 54 41 54 45  *appStmt[N_STATE
36baa 4d 45 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a  MENT];.  int i;.
36bab 0a 20 20 70 52 74 72 65 65 2d 3e 64 62 20 3d 20  .  pRtree->db = 
36bac 64 62 3b 0a 0a 20 20 69 66 28 20 69 73 43 72 65  db;..  if( isCre
36bad 61 74 65 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ate ){.    char 
36bae 2a 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74  *zCreate = sqlit
36baf 65 33 5f 6d 70 72 69 6e 74 66 28 0a 22 43 52 45  e3_mprintf(."CRE
36bb0 41 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22  ATE TABLE \"%w\"
36bb1 2e 5c 22 25 77 5f 6e 6f 64 65 5c 22 28 6e 6f 64  .\"%w_node\"(nod
36bb2 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d  eno INTEGER PRIM
36bb3 41 52 59 20 4b 45 59 2c 20 64 61 74 61 20 42 4c  ARY KEY, data BL
36bb4 4f 42 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41  OB);"."CREATE TA
36bb5 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f  BLE \"%w\".\"%w_
36bb6 72 6f 77 69 64 5c 22 28 72 6f 77 69 64 20 49 4e  rowid\"(rowid IN
36bb7 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
36bb8 59 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45  Y, nodeno INTEGE
36bb9 52 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41 42  R);"."CREATE TAB
36bba 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 70  LE \"%w\".\"%w_p
36bbb 61 72 65 6e 74 5c 22 28 6e 6f 64 65 6e 6f 20 49  arent\"(nodeno I
36bbc 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
36bbd 45 59 2c 20 70 61 72 65 6e 74 6e 6f 64 65 20 49  EY, parentnode I
36bbe 4e 54 45 47 45 52 29 3b 22 0a 22 49 4e 53 45 52  NTEGER);"."INSER
36bbf 54 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f  T INTO '%q'.'%q_
36bc0 6e 6f 64 65 27 20 56 41 4c 55 45 53 28 31 2c 20  node' VALUES(1, 
36bc1 7a 65 72 6f 62 6c 6f 62 28 25 64 29 29 22 2c 0a  zeroblob(%d))",.
36bc2 20 20 20 20 20 20 7a 44 62 2c 20 7a 50 72 65 66        zDb, zPref
36bc3 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78  ix, zDb, zPrefix
36bc4 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20  , zDb, zPrefix, 
36bc5 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 70 52  zDb, zPrefix, pR
36bc6 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 0a  tree->iNodeSize.
36bc7 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21      );.    if( !
36bc8 7a 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 20  zCreate ){.     
36bc9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
36bca 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
36bcb 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
36bcc 63 28 64 62 2c 20 7a 43 72 65 61 74 65 2c 20 30  c(db, zCreate, 0
36bcd 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
36bce 69 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61 74  ite3_free(zCreat
36bcf 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
36bd0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
36bd1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
36bd2 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 70 70 53 74    }.  }..  appSt
36bd3 6d 74 5b 30 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[0] = &pRtree-
36bd4 3e 70 52 65 61 64 4e 6f 64 65 3b 0a 20 20 61 70  >pReadNode;.  ap
36bd5 70 53 74 6d 74 5b 31 5d 20 3d 20 26 70 52 74 72  pStmt[1] = &pRtr
36bd6 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b 0a  ee->pWriteNode;.
36bd7 20 20 61 70 70 53 74 6d 74 5b 32 5d 20 3d 20 26    appStmt[2] = &
36bd8 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e  pRtree->pDeleteN
36bd9 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 33  ode;.  appStmt[3
36bda 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65  ] = &pRtree->pRe
36bdb 61 64 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74  adRowid;.  appSt
36bdc 6d 74 5b 34 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[4] = &pRtree-
36bdd 3e 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20  >pWriteRowid;.  
36bde 61 70 70 53 74 6d 74 5b 35 5d 20 3d 20 26 70 52  appStmt[5] = &pR
36bdf 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77  tree->pDeleteRow
36be0 69 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 36 5d  id;.  appStmt[6]
36be1 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61   = &pRtree->pRea
36be2 64 50 61 72 65 6e 74 3b 0a 20 20 61 70 70 53 74  dParent;.  appSt
36be3 6d 74 5b 37 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[7] = &pRtree-
36be4 3e 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20  >pWriteParent;. 
36be5 20 61 70 70 53 74 6d 74 5b 38 5d 20 3d 20 26 70   appStmt[8] = &p
36be6 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
36be7 72 65 6e 74 3b 0a 0a 20 20 66 6f 72 28 69 3d 30  rent;..  for(i=0
36be8 3b 20 69 3c 4e 5f 53 54 41 54 45 4d 45 4e 54 20  ; i<N_STATEMENT 
36be9 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
36bea 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72  ; i++){.    char
36beb 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33   *zSql = sqlite3
36bec 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b 69  _mprintf(azSql[i
36bed 5d 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 29  ], zDb, zPrefix)
36bee 3b 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29  ;.    if( zSql )
36bef 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
36bf0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
36bf1 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 61 70  db, zSql, -1, ap
36bf2 70 53 74 6d 74 5b 69 5d 2c 20 30 29 3b 20 0a 20  pStmt[i], 0); . 
36bf3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
36bf4 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
36bf5 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  M;.    }.    sql
36bf6 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b  ite3_free(zSql);
36bf7 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
36bf8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
36bf9 20 72 6f 75 74 69 6e 65 20 71 75 65 72 69 65 73   routine queries
36bfa 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
36bfb 20 64 62 20 66 6f 72 20 74 68 65 20 70 61 67 65   db for the page
36bfc 2d 73 69 7a 65 20 75 73 65 64 20 62 79 0a 2a 2a  -size used by.**
36bfd 20 64 61 74 61 62 61 73 65 20 7a 44 62 2e 20 49   database zDb. I
36bfe 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68  f successful, th
36bff 65 20 70 61 67 65 2d 73 69 7a 65 20 69 6e 20 62  e page-size in b
36c00 79 74 65 73 20 69 73 20 77 72 69 74 74 65 6e 20  ytes is written 
36c01 74 6f 0a 2a 2a 20 2a 70 69 50 61 67 65 53 69 7a  to.** *piPageSiz
36c02 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  e and SQLITE_OK 
36c03 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
36c04 69 73 65 2c 20 61 6e 64 20 61 6e 20 53 51 4c 69  ise, and an SQLi
36c05 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64  te error .** cod
36c06 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
36c07 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
36c08 50 61 67 65 53 69 7a 65 28 73 71 6c 69 74 65 33  PageSize(sqlite3
36c09 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
36c0a 20 2a 7a 44 62 2c 20 69 6e 74 20 2a 70 69 50 61   *zDb, int *piPa
36c0b 67 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  geSize){.  int r
36c0c 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
36c0d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  ;.  char *zSql;.
36c0e 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
36c0f 70 53 74 6d 74 20 3d 20 30 3b 0a 0a 20 20 7a 53  pStmt = 0;..  zS
36c10 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
36c11 69 6e 74 66 28 22 50 52 41 47 4d 41 20 25 51 2e  intf("PRAGMA %Q.
36c12 70 61 67 65 5f 73 69 7a 65 22 2c 20 7a 44 62 29  page_size", zDb)
36c13 3b 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b  ;.  if( !zSql ){
36c14 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
36c15 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
36c16 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
36c17 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71  epare_v2(db, zSq
36c18 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30  l, -1, &pStmt, 0
36c19 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
36c1a 65 28 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72  e(zSql);.  if( r
36c1b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
36c1c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
36c1d 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45   }..  if( SQLITE
36c1e 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74  _ROW==sqlite3_st
36c1f 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20  ep(pStmt) ){.   
36c20 20 2a 70 69 50 61 67 65 53 69 7a 65 20 3d 20 73   *piPageSize = s
36c21 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
36c22 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 7d  t(pStmt, 0);.  }
36c23 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
36c24 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
36c25 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69  );.}../* .** Thi
36c26 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68  s function is th
36c27 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
36c28 20 6f 66 20 62 6f 74 68 20 74 68 65 20 78 43 6f   of both the xCo
36c29 6e 6e 65 63 74 20 61 6e 64 20 78 43 72 65 61 74  nnect and xCreat
36c2a 65 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 6f 66 20  e.** methods of 
36c2b 74 68 65 20 72 2d 74 72 65 65 20 76 69 72 74 75  the r-tree virtu
36c2c 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
36c2d 20 20 61 72 67 76 5b 30 5d 20 20 20 2d 3e 20 6d    argv[0]   -> m
36c2e 6f 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  odule name.**   
36c2f 61 72 67 76 5b 31 5d 20 20 20 2d 3e 20 64 61 74  argv[1]   -> dat
36c30 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  abase name.**   
36c31 61 72 67 76 5b 32 5d 20 20 20 2d 3e 20 74 61 62  argv[2]   -> tab
36c32 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67  le name.**   arg
36c33 76 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f 6c 75 6d 6e  v[...] -> column
36c34 20 6e 61 6d 65 73 2e 2e 2e 0a 2a 2f 0a 73 74 61   names....*/.sta
36c35 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 69  tic int rtreeIni
36c36 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
36c37 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36c38 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
36c39 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
36c3a 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  */.  void *pAux,
36c3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c3c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
36c3d 6f 66 20 74 68 65 20 52 54 52 45 45 5f 43 4f 4f  of the RTREE_COO
36c3e 52 44 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a  RD_* constants *
36c3f 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  /.  int argc, co
36c40 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
36c41 61 72 67 76 2c 20 20 20 2f 2a 20 50 61 72 61 6d  argv,   /* Param
36c42 65 74 65 72 73 20 74 6f 20 43 52 45 41 54 45 20  eters to CREATE 
36c43 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
36c44 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  */.  sqlite3_vta
36c45 62 20 2a 2a 70 70 56 74 61 62 2c 20 20 20 20 20  b **ppVtab,     
36c46 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
36c47 20 4e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62   New virtual tab
36c48 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  le */.  char **p
36c49 7a 45 72 72 2c 20 20 20 20 20 20 20 20 20 20 20  zErr,           
36c4a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
36c4b 55 54 3a 20 45 72 72 6f 72 20 6d 65 73 73 61 67  UT: Error messag
36c4c 65 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 69  e, if any */.  i
36c4d 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20  nt isCreate     
36c4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c4f 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 78     /* True for x
36c50 43 72 65 61 74 65 2c 20 66 61 6c 73 65 20 66 6f  Create, false fo
36c51 72 20 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b  r xConnect */.){
36c52 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
36c53 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 50 61  TE_OK;.  int iPa
36c54 67 65 53 69 7a 65 20 3d 20 30 3b 0a 20 20 52 74  geSize = 0;.  Rt
36c55 72 65 65 20 2a 70 52 74 72 65 65 3b 0a 20 20 69  ree *pRtree;.  i
36c56 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20  nt nDb;         
36c57 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
36c58 66 20 73 74 72 69 6e 67 20 61 72 67 76 5b 31 5d  f string argv[1]
36c59 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b   */.  int nName;
36c5a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
36c5b 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20  ength of string 
36c5c 61 72 67 76 5b 32 5d 20 2a 2f 0a 20 20 69 6e 74  argv[2] */.  int
36c5d 20 65 43 6f 6f 72 64 54 79 70 65 20 3d 20 28 69   eCoordType = (i
36c5e 6e 74 29 70 41 75 78 3b 0a 0a 20 20 63 6f 6e 73  nt)pAux;..  cons
36c5f 74 20 63 68 61 72 20 2a 61 45 72 72 4d 73 67 5b  t char *aErrMsg[
36c60 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20  ] = {.    0,    
36c61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c64 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 22 57 72 6f  /* 0 */.    "Wro
36c65 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  ng number of col
36c66 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65  umns for an rtre
36c67 65 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20  e table",       
36c68 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 22 54    /* 1 */.    "T
36c69 6f 6f 20 66 65 77 20 63 6f 6c 75 6d 6e 73 20 66  oo few columns f
36c6a 6f 72 20 61 6e 20 72 74 72 65 65 20 74 61 62 6c  or an rtree tabl
36c6b 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
36c6c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20      /* 2 */.    
36c6d 22 54 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e  "Too many column
36c6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65 20 74  s for an rtree t
36c6f 61 62 6c 65 22 20 20 20 20 20 20 20 20 20 20 20  able"           
36c70 20 20 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20        /* 3 */.  
36c71 7d 3b 0a 0a 20 20 69 6e 74 20 69 45 72 72 20 3d  };..  int iErr =
36c72 20 28 61 72 67 63 3c 36 29 20 3f 20 32 20 3a 20   (argc<6) ? 2 : 
36c73 61 72 67 63 3e 28 52 54 52 45 45 5f 4d 41 58 5f  argc>(RTREE_MAX_
36c74 44 49 4d 45 4e 53 49 4f 4e 53 2a 32 2b 34 29 20  DIMENSIONS*2+4) 
36c75 3f 20 33 20 3a 20 61 72 67 63 25 32 3b 0a 20 20  ? 3 : argc%2;.  
36c76 69 66 28 20 61 45 72 72 4d 73 67 5b 69 45 72 72  if( aErrMsg[iErr
36c77 5d 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20  ] ){.    *pzErr 
36c78 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
36c79 66 28 22 25 73 22 2c 20 61 45 72 72 4d 73 67 5b  f("%s", aErrMsg[
36c7a 69 45 72 72 5d 29 3b 0a 20 20 20 20 72 65 74 75  iErr]);.    retu
36c7b 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
36c7c 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74  .  }..  rc = get
36c7d 50 61 67 65 53 69 7a 65 28 64 62 2c 20 61 72 67  PageSize(db, arg
36c7e 76 5b 31 5d 2c 20 26 69 50 61 67 65 53 69 7a 65  v[1], &iPageSize
36c7f 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36c80 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
36c81 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
36c82 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
36c83 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72  sqlite3_vtab str
36c84 75 63 74 75 72 65 20 2a 2f 0a 20 20 6e 44 62 20  ucture */.  nDb 
36c85 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d  = strlen(argv[1]
36c86 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72  );.  nName = str
36c87 6c 65 6e 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  len(argv[2]);.  
36c88 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
36c89 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
36c8a 28 73 69 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e  (sizeof(Rtree)+n
36c8b 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 69  Db+nName+2);.  i
36c8c 66 28 20 21 70 52 74 72 65 65 20 29 7b 0a 20 20  f( !pRtree ){.  
36c8d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
36c8e 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
36c8f 73 65 74 28 70 52 74 72 65 65 2c 20 30 2c 20 73  set(pRtree, 0, s
36c90 69 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44 62  izeof(Rtree)+nDb
36c91 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 70 52 74  +nName+2);.  pRt
36c92 72 65 65 2d 3e 6e 42 75 73 79 20 3d 20 31 3b 0a  ree->nBusy = 1;.
36c93 20 20 70 52 74 72 65 65 2d 3e 62 61 73 65 2e 70    pRtree->base.p
36c94 4d 6f 64 75 6c 65 20 3d 20 26 72 74 72 65 65 4d  Module = &rtreeM
36c95 6f 64 75 6c 65 3b 0a 20 20 70 52 74 72 65 65 2d  odule;.  pRtree-
36c96 3e 7a 44 62 20 3d 20 28 63 68 61 72 20 2a 29 26  >zDb = (char *)&
36c97 70 52 74 72 65 65 5b 31 5d 3b 0a 20 20 70 52 74  pRtree[1];.  pRt
36c98 72 65 65 2d 3e 7a 4e 61 6d 65 20 3d 20 26 70 52  ree->zName = &pR
36c99 74 72 65 65 2d 3e 7a 44 62 5b 6e 44 62 2b 31 5d  tree->zDb[nDb+1]
36c9a 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ;.  pRtree->nDim
36c9b 20 3d 20 28 61 72 67 63 2d 34 29 2f 32 3b 0a 20   = (argc-4)/2;. 
36c9c 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
36c9d 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20 70 52 74  erCell = 8 + pRt
36c9e 72 65 65 2d 3e 6e 44 69 6d 2a 34 2a 32 3b 0a 20  ree->nDim*4*2;. 
36c9f 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
36ca0 79 70 65 20 3d 20 65 43 6f 6f 72 64 54 79 70 65  ype = eCoordType
36ca1 3b 0a 20 20 6d 65 6d 63 70 79 28 70 52 74 72 65  ;.  memcpy(pRtre
36ca2 65 2d 3e 7a 44 62 2c 20 61 72 67 76 5b 31 5d 2c  e->zDb, argv[1],
36ca3 20 6e 44 62 29 3b 0a 20 20 6d 65 6d 63 70 79 28   nDb);.  memcpy(
36ca4 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 61  pRtree->zName, a
36ca5 72 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65 29 3b 0a  rgv[2], nName);.
36ca6 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
36ca7 20 74 68 65 20 6e 6f 64 65 20 73 69 7a 65 20 74   the node size t
36ca8 6f 20 75 73 65 2e 20 42 79 20 64 65 66 61 75 6c  o use. By defaul
36ca9 74 2c 20 75 73 65 20 36 34 20 62 79 74 65 73 20  t, use 64 bytes 
36caa 6c 65 73 73 20 74 68 61 6e 0a 20 20 2a 2a 20 74  less than.  ** t
36cab 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
36cac 2d 73 69 7a 65 2e 20 54 68 69 73 20 65 6e 73 75  -size. This ensu
36cad 72 65 73 20 74 68 61 74 20 65 61 63 68 20 6e 6f  res that each no
36cae 64 65 20 69 73 20 73 74 6f 72 65 64 20 6f 6e 0a  de is stored on.
36caf 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64 61    ** a single da
36cb0 74 61 62 61 73 65 20 70 61 67 65 2e 0a 20 20 2a  tabase page..  *
36cb1 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61  *.  ** If the da
36cb2 74 61 62 61 73 64 20 70 61 67 65 2d 73 69 7a 65  tabasd page-size
36cb3 20 69 73 20 73 6f 20 6c 61 72 67 65 20 74 68 61   is so large tha
36cb4 74 20 6d 6f 72 65 20 74 68 61 6e 20 52 54 52 45  t more than RTRE
36cb5 45 5f 4d 41 58 43 45 4c 4c 53 0a 20 20 2a 2a 20  E_MAXCELLS.  ** 
36cb6 65 6e 74 72 69 65 73 20 77 6f 75 6c 64 20 66 69  entries would fi
36cb7 74 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6e 6f  t in a single no
36cb8 64 65 2c 20 75 73 65 20 61 20 73 6d 61 6c 6c 65  de, use a smalle
36cb9 72 20 6e 6f 64 65 2d 73 69 7a 65 2e 0a 20 20 2a  r node-size..  *
36cba 2f 0a 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64  /.  pRtree->iNod
36cbb 65 53 69 7a 65 20 3d 20 69 50 61 67 65 53 69 7a  eSize = iPageSiz
36cbc 65 2d 36 34 3b 0a 20 20 69 66 28 20 28 34 2b 70  e-64;.  if( (4+p
36cbd 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
36cbe 43 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58 43 45  Cell*RTREE_MAXCE
36cbf 4c 4c 53 29 3c 70 52 74 72 65 65 2d 3e 69 4e 6f  LLS)<pRtree->iNo
36cc0 64 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 70 52  deSize ){.    pR
36cc1 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20  tree->iNodeSize 
36cc2 3d 20 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74  = 4+pRtree->nByt
36cc3 65 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f  esPerCell*RTREE_
36cc4 4d 41 58 43 45 4c 4c 53 3b 0a 20 20 7d 0a 0a 20  MAXCELLS;.  }.. 
36cc5 20 2f 2a 20 43 72 65 61 74 65 2f 43 6f 6e 6e 65   /* Create/Conne
36cc6 63 74 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c  ct to the underl
36cc7 79 69 6e 67 20 72 65 6c 61 74 69 6f 6e 61 6c 20  ying relational 
36cc8 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
36cc9 20 49 66 0a 20 20 2a 2a 20 74 68 61 74 20 69 73   If.  ** that is
36cca 20 73 75 63 63 65 73 73 66 75 6c 2c 20 63 61 6c   successful, cal
36ccb 6c 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  l sqlite3_declar
36ccc 65 5f 76 74 61 62 28 29 20 74 6f 20 63 6f 6e 66  e_vtab() to conf
36ccd 69 67 75 72 65 0a 20 20 2a 2a 20 74 68 65 20 72  igure.  ** the r
36cce 2d 74 72 65 65 20 74 61 62 6c 65 20 73 63 68 65  -tree table sche
36ccf 6d 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ma..  */.  if( (
36cd0 72 63 20 3d 20 72 74 72 65 65 53 71 6c 49 6e 69  rc = rtreeSqlIni
36cd1 74 28 70 52 74 72 65 65 2c 20 64 62 2c 20 61 72  t(pRtree, db, ar
36cd2 67 76 5b 31 5d 2c 20 61 72 67 76 5b 32 5d 2c 20  gv[1], argv[2], 
36cd3 69 73 43 72 65 61 74 65 29 29 20 29 7b 0a 20 20  isCreate)) ){.  
36cd4 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
36cd5 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
36cd6 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
36cd7 64 62 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  db));.  }else{. 
36cd8 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20     char *zSql = 
36cd9 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
36cda 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 28  "CREATE TABLE x(
36cdb 25 73 22 2c 20 61 72 67 76 5b 33 5d 29 3b 0a 20  %s", argv[3]);. 
36cdc 20 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20     char *zTmp;. 
36cdd 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66     int ii;.    f
36cde 6f 72 28 69 69 3d 34 3b 20 7a 53 71 6c 20 26 26  or(ii=4; zSql &&
36cdf 20 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29 7b   ii<argc; ii++){
36ce0 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53  .      zTmp = zS
36ce1 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d  ql;.      zSql =
36ce2 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
36ce3 28 22 25 73 2c 20 25 73 22 2c 20 7a 54 6d 70 2c  ("%s, %s", zTmp,
36ce4 20 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20 20   argv[ii]);.    
36ce5 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
36ce6 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Tmp);.    }.    
36ce7 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20  if( zSql ){.    
36ce8 20 20 7a 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20    zTmp = zSql;. 
36ce9 20 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69       zSql = sqli
36cea 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 29  te3_mprintf("%s)
36ceb 3b 22 2c 20 7a 54 6d 70 29 3b 0a 20 20 20 20 20  ;", zTmp);.     
36cec 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
36ced 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  mp);.    }.    i
36cee 66 28 20 21 7a 53 71 6c 20 7c 7c 20 73 71 6c 69  f( !zSql || sqli
36cef 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
36cf0 28 64 62 2c 20 7a 53 71 6c 29 20 29 7b 0a 20 20  (db, zSql) ){.  
36cf1 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
36cf2 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
36cf3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53   sqlite3_free(zS
36cf4 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ql);.  }..  if( 
36cf5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
36cf6 0a 20 20 20 20 2a 70 70 56 74 61 62 20 3d 20 28  .    *ppVtab = (
36cf7 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 70  sqlite3_vtab *)p
36cf8 52 74 72 65 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Rtree;.  }else{.
36cf9 20 20 20 20 72 74 72 65 65 52 65 6c 65 61 73 65      rtreeRelease
36cfa 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20  (pRtree);.  }.  
36cfb 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
36cfc 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
36cfd 69 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61 72 20  ion of a scalar 
36cfe 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65  function that de
36cff 63 6f 64 65 73 20 72 2d 74 72 65 65 20 6e 6f 64  codes r-tree nod
36d00 65 73 20 74 6f 0a 2a 2a 20 68 75 6d 61 6e 20 72  es to.** human r
36d01 65 61 64 61 62 6c 65 20 73 74 72 69 6e 67 73 2e  eadable strings.
36d02 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
36d03 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20  d for debugging 
36d04 61 6e 64 20 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a  and analysis..**
36d05 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61 72 20 66  .** The scalar f
36d06 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 74 77  unction takes tw
36d07 6f 20 61 72 67 75 6d 65 6e 74 73 2c 20 61 20 62  o arguments, a b
36d08 6c 6f 62 20 6f 66 20 64 61 74 61 20 63 6f 6e 74  lob of data cont
36d09 61 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 72 2d 74  aining.** an r-t
36d0a 72 65 65 20 6e 6f 64 65 2c 20 61 6e 64 20 74 68  ree node, and th
36d0b 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 6d 65  e number of dime
36d0c 6e 73 69 6f 6e 73 20 74 68 65 20 72 2d 74 72 65  nsions the r-tre
36d0d 65 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 46 6f  e indexes..** Fo
36d0e 72 20 61 20 74 77 6f 2d 64 69 6d 65 6e 73 69 6f  r a two-dimensio
36d0f 6e 61 6c 20 72 2d 74 72 65 65 20 73 74 72 75 63  nal r-tree struc
36d10 74 75 72 65 20 63 61 6c 6c 65 64 20 22 72 74 22  ture called "rt"
36d11 2c 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65  , to deserialize
36d12 0a 2a 2a 20 61 6c 6c 20 6e 6f 64 65 73 2c 20 61  .** all nodes, a
36d13 20 73 74 61 74 65 6d 65 6e 74 20 6c 69 6b 65 3a   statement like:
36d14 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
36d15 72 74 72 65 65 6e 6f 64 65 28 32 2c 20 64 61 74  rtreenode(2, dat
36d16 61 29 20 46 52 4f 4d 20 72 74 5f 6e 6f 64 65 3b  a) FROM rt_node;
36d17 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 75 6d 61 6e  .**.** The human
36d18 20 72 65 61 64 61 62 6c 65 20 73 74 72 69 6e 67   readable string
36d19 20 74 61 6b 65 73 20 74 68 65 20 66 6f 72 6d 20   takes the form 
36d1a 6f 66 20 61 20 54 63 6c 20 6c 69 73 74 20 77 69  of a Tcl list wi
36d1b 74 68 20 6f 6e 65 0a 2a 2a 20 65 6e 74 72 79 20  th one.** entry 
36d1c 66 6f 72 20 65 61 63 68 20 63 65 6c 6c 20 69 6e  for each cell in
36d1d 20 74 68 65 20 72 2d 74 72 65 65 20 6e 6f 64 65   the r-tree node
36d1e 2e 20 45 61 63 68 20 65 6e 74 72 79 20 69 73 20  . Each entry is 
36d1f 69 74 73 65 6c 66 20 61 0a 2a 2a 20 6c 69 73 74  itself a.** list
36d20 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  , containing the
36d21 20 38 2d 62 79 74 65 20 72 6f 77 69 64 2f 70 61   8-byte rowid/pa
36d22 67 65 6e 6f 20 66 6f 6c 6c 6f 77 65 64 20 62 79  geno followed by
36d23 20 74 68 65 20 0a 2a 2a 20 3c 6e 75 6d 2d 64 69   the .** <num-di
36d24 6d 65 6e 73 69 6f 6e 3e 2a 32 20 63 6f 6f 72 64  mension>*2 coord
36d25 69 6e 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  inates..*/.stati
36d26 63 20 76 6f 69 64 20 72 74 72 65 65 6e 6f 64 65  c void rtreenode
36d27 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
36d28 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72 67 2c   *ctx, int nArg,
36d29 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
36d2a 2a 61 70 41 72 67 29 7b 0a 20 20 63 68 61 72 20  *apArg){.  char 
36d2b 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20 52 74  *zText = 0;.  Rt
36d2c 72 65 65 4e 6f 64 65 20 6e 6f 64 65 3b 0a 20 20  reeNode node;.  
36d2d 52 74 72 65 65 20 74 72 65 65 3b 0a 20 20 69 6e  Rtree tree;.  in
36d2e 74 20 69 69 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  t ii;..  memset(
36d2f 26 6e 6f 64 65 2c 20 30 2c 20 73 69 7a 65 6f 66  &node, 0, sizeof
36d30 28 52 74 72 65 65 4e 6f 64 65 29 29 3b 0a 20 20  (RtreeNode));.  
36d31 6d 65 6d 73 65 74 28 26 74 72 65 65 2c 20 30 2c  memset(&tree, 0,
36d32 20 73 69 7a 65 6f 66 28 52 74 72 65 65 29 29 3b   sizeof(Rtree));
36d33 0a 20 20 74 72 65 65 2e 6e 44 69 6d 20 3d 20 73  .  tree.nDim = s
36d34 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
36d35 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 74 72  (apArg[0]);.  tr
36d36 65 65 2e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  ee.nBytesPerCell
36d37 20 3d 20 38 20 2b 20 38 20 2a 20 74 72 65 65 2e   = 8 + 8 * tree.
36d38 6e 44 69 6d 3b 0a 20 20 6e 6f 64 65 2e 7a 44 61  nDim;.  node.zDa
36d39 74 61 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  ta = (u8 *)sqlit
36d3a 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 70  e3_value_blob(ap
36d3b 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 66 6f 72 28  Arg[1]);..  for(
36d3c 69 69 3d 30 3b 20 69 69 3c 4e 43 45 4c 4c 28 26  ii=0; ii<NCELL(&
36d3d 6e 6f 64 65 29 3b 20 69 69 2b 2b 29 7b 0a 20 20  node); ii++){.  
36d3e 20 20 63 68 61 72 20 7a 43 65 6c 6c 5b 35 31 32    char zCell[512
36d3f 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c  ];.    int nCell
36d40 20 3d 20 30 3b 0a 20 20 20 20 52 74 72 65 65 43   = 0;.    RtreeC
36d41 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 69 6e  ell cell;.    in
36d42 74 20 6a 6a 3b 0a 0a 20 20 20 20 6e 6f 64 65 47  t jj;..    nodeG
36d43 65 74 43 65 6c 6c 28 26 74 72 65 65 2c 20 26 6e  etCell(&tree, &n
36d44 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b  ode, ii, &cell);
36d45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
36d46 72 69 6e 74 66 28 35 31 32 2d 6e 43 65 6c 6c 2c  rintf(512-nCell,
36d47 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 25  &zCell[nCell],"%
36d48 64 22 2c 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29  d", cell.iRowid)
36d49 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 73 74  ;.    nCell = st
36d4a 72 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a 20 20 20  rlen(zCell);.   
36d4b 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 74 72   for(jj=0; jj<tr
36d4c 65 65 2e 6e 44 69 6d 2a 32 3b 20 6a 6a 2b 2b 29  ee.nDim*2; jj++)
36d4d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
36d4e 73 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e 43 65  snprintf(512-nCe
36d4f 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  ll,&zCell[nCell]
36d50 2c 22 20 25 66 22 2c 28 64 6f 75 62 6c 65 29 63  ," %f",(double)c
36d51 65 6c 6c 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 2e 66  ell.aCoord[jj].f
36d52 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d  );.      nCell =
36d53 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a   strlen(zCell);.
36d54 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a      }..    if( z
36d55 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 63 68  Text ){.      ch
36d56 61 72 20 2a 7a 54 65 78 74 4e 65 77 20 3d 20 73  ar *zTextNew = s
36d57 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
36d58 25 73 20 7b 25 73 7d 22 2c 20 7a 54 65 78 74 2c  %s {%s}", zText,
36d59 20 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 73   zCell);.      s
36d5a 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 78  qlite3_free(zTex
36d5b 74 29 3b 0a 20 20 20 20 20 20 7a 54 65 78 74 20  t);.      zText 
36d5c 3d 20 7a 54 65 78 74 4e 65 77 3b 0a 20 20 20 20  = zTextNew;.    
36d5d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 54 65  }else{.      zTe
36d5e 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  xt = sqlite3_mpr
36d5f 69 6e 74 66 28 22 7b 25 73 7d 22 2c 20 7a 43 65  intf("{%s}", zCe
36d60 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ll);.    }.  }. 
36d61 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75   .  sqlite3_resu
36d62 6c 74 5f 74 65 78 74 28 63 74 78 2c 20 7a 54 65  lt_text(ctx, zTe
36d63 78 74 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f  xt, -1, sqlite3_
36d64 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  free);.}..static
36d65 20 76 6f 69 64 20 72 74 72 65 65 64 65 70 74 68   void rtreedepth
36d66 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
36d67 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72 67 2c   *ctx, int nArg,
36d68 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
36d69 2a 61 70 41 72 67 29 7b 0a 20 20 69 66 28 20 73  *apArg){.  if( s
36d6a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
36d6b 65 28 61 70 41 72 67 5b 30 5d 29 21 3d 53 51 4c  e(apArg[0])!=SQL
36d6c 49 54 45 5f 42 4c 4f 42 20 0a 20 20 20 7c 7c 20  ITE_BLOB .   || 
36d6d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
36d6e 74 65 73 28 61 70 41 72 67 5b 30 5d 29 3c 32 0a  tes(apArg[0])<2.
36d6f 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33    ){.    sqlite3
36d70 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 74  _result_error(ct
36d71 78 2c 20 22 49 6e 76 61 6c 69 64 20 61 72 67 75  x, "Invalid argu
36d72 6d 65 6e 74 20 74 6f 20 72 74 72 65 65 64 65 70  ment to rtreedep
36d73 74 68 28 29 22 2c 20 2d 31 29 3b 20 0a 20 20 7d  th()", -1); .  }
36d74 65 6c 73 65 7b 0a 20 20 20 20 75 38 20 2a 7a 42  else{.    u8 *zB
36d75 6c 6f 62 20 3d 20 28 75 38 20 2a 29 73 71 6c 69  lob = (u8 *)sqli
36d76 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
36d77 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 73 71  pArg[0]);.    sq
36d78 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
36d79 28 63 74 78 2c 20 72 65 61 64 49 6e 74 31 36 28  (ctx, readInt16(
36d7a 7a 42 6c 6f 62 29 29 3b 0a 20 20 7d 0a 7d 0a 0a  zBlob));.  }.}..
36d7b 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74  /*.** Register t
36d7c 68 65 20 72 2d 74 72 65 65 20 6d 6f 64 75 6c 65  he r-tree module
36d7d 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20 68   with database h
36d7e 61 6e 64 6c 65 20 64 62 2e 20 54 68 69 73 20 63  andle db. This c
36d7f 72 65 61 74 65 73 20 74 68 65 0a 2a 2a 20 76 69  reates the.** vi
36d80 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
36d81 6c 65 20 22 72 74 72 65 65 22 20 61 6e 64 20 74  le "rtree" and t
36d82 68 65 20 64 65 62 75 67 67 69 6e 67 2f 61 6e 61  he debugging/ana
36d83 6c 79 73 69 73 20 73 63 61 6c 61 72 20 0a 2a 2a  lysis scalar .**
36d84 20 66 75 6e 63 74 69 6f 6e 20 22 72 74 72 65 65   function "rtree
36d85 6e 6f 64 65 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45  node"..*/.SQLITE
36d86 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
36d87 69 74 65 33 52 74 72 65 65 49 6e 69 74 28 73 71  ite3RtreeInit(sq
36d88 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
36d89 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
36d8a 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
36d8b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
36d8c 74 20 75 74 66 38 20 3d 20 53 51 4c 49 54 45 5f  t utf8 = SQLITE_
36d8d 55 54 46 38 3b 0a 20 20 20 20 72 63 20 3d 20 73  UTF8;.    rc = s
36d8e 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
36d8f 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72 65  nction(db, "rtre
36d90 65 6e 6f 64 65 22 2c 20 32 2c 20 75 74 66 38 2c  enode", 2, utf8,
36d91 20 30 2c 20 72 74 72 65 65 6e 6f 64 65 2c 20 30   0, rtreenode, 0
36d92 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 0);.  }.  if( 
36d93 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
36d94 0a 20 20 20 20 69 6e 74 20 75 74 66 38 20 3d 20  .    int utf8 = 
36d95 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20  SQLITE_UTF8;.   
36d96 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
36d97 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62  eate_function(db
36d98 2c 20 22 72 74 72 65 65 64 65 70 74 68 22 2c 20  , "rtreedepth", 
36d99 31 2c 20 75 74 66 38 2c 20 30 2c 72 74 72 65 65  1, utf8, 0,rtree
36d9a 64 65 70 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20  depth, 0, 0);.  
36d9b 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
36d9c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76 6f 69  TE_OK ){.    voi
36d9d 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29 52  d *c = (void *)R
36d9e 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33  TREE_COORD_REAL3
36d9f 32 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  2;.    rc = sqli
36da0 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
36da1 65 5f 76 32 28 64 62 2c 20 22 72 74 72 65 65 22  e_v2(db, "rtree"
36da2 2c 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 2c 20  , &rtreeModule, 
36da3 63 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  c, 0);.  }.  if(
36da4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
36da5 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63 20 3d 20  {.    void *c = 
36da6 28 76 6f 69 64 20 2a 29 52 54 52 45 45 5f 43 4f  (void *)RTREE_CO
36da7 4f 52 44 5f 49 4e 54 33 32 3b 0a 20 20 20 20 72  ORD_INT32;.    r
36da8 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61  c = sqlite3_crea
36da9 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64 62 2c  te_module_v2(db,
36daa 20 22 72 74 72 65 65 5f 69 33 32 22 2c 20 26 72   "rtree_i32", &r
36dab 74 72 65 65 4d 6f 64 75 6c 65 2c 20 63 2c 20 30  treeModule, c, 0
36dac 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
36dad 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c   rc;.}..#if !SQL
36dae 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f  ITE_CORE.SQLITE_
36daf 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
36db0 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a  extension_init(.
36db1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
36db2 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
36db3 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ,.  const sqlite
36db4 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a  3_api_routines *
36db5 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45  pApi.){.  SQLITE
36db6 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32  _EXTENSION_INIT2
36db7 28 70 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20  (pApi).  return 
36db8 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
36db9 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  (db);.}.#endif..
36dba 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
36dbb 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72  ******* End of r
36dbc 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tree.c *********
36dbd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dbe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dbf 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
36dc0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
36dc1 6c 65 20 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a  le icu.c *******
36dc2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dc4 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
36dc5 30 37 20 4d 61 79 20 36 0a 2a 2a 0a 2a 2a 20 54  07 May 6.**.** T
36dc6 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
36dc7 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
36dc8 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
36dc9 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
36dca 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
36dcb 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
36dcc 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
36dcd 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
36dce 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
36dcf 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
36dd0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
36dd1 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
36dd2 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
36dd3 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
36dd4 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
36dd5 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
36dd6 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
36dd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36dda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ddb 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  *******.** $Id: 
36ddc 69 63 75 2e 63 2c 76 20 31 2e 37 20 32 30 30 37  icu.c,v 1.7 2007
36ddd 2f 31 32 2f 31 33 20 32 31 3a 35 34 3a 31 31 20  /12/13 21:54:11 
36dde 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20  drh Exp $.**.** 
36ddf 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  This file implem
36de0 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 72 61 74  ents an integrat
36de1 69 6f 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20  ion between the 
36de2 49 43 55 20 6c 69 62 72 61 72 79 20 0a 2a 2a 20  ICU library .** 
36de3 28 22 49 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 20  ("International 
36de4 43 6f 6d 70 6f 6e 65 6e 74 73 20 66 6f 72 20 55  Components for U
36de5 6e 69 63 6f 64 65 22 2c 20 61 6e 20 6f 70 65 6e  nicode", an open
36de6 2d 73 6f 75 72 63 65 20 6c 69 62 72 61 72 79 20  -source library 
36de7 0a 2a 2a 20 66 6f 72 20 68 61 6e 64 6c 69 6e 67  .** for handling
36de8 20 75 6e 69 63 6f 64 65 20 64 61 74 61 29 20 61   unicode data) a
36de9 6e 64 20 53 51 4c 69 74 65 2e 20 54 68 65 20 69  nd SQLite. The i
36dea 6e 74 65 67 72 61 74 69 6f 6e 20 75 73 65 73 20  ntegration uses 
36deb 0a 2a 2a 20 49 43 55 20 74 6f 20 70 72 6f 76 69  .** ICU to provi
36dec 64 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  de the following
36ded 20 74 6f 20 53 51 4c 69 74 65 3a 0a 2a 2a 0a 2a   to SQLite:.**.*
36dee 2a 20 20 20 2a 20 41 6e 20 69 6d 70 6c 65 6d 65  *   * An impleme
36def 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53  ntation of the S
36df0 51 4c 20 72 65 67 65 78 70 28 29 20 66 75 6e 63  QL regexp() func
36df1 74 69 6f 6e 20 28 61 6e 64 20 68 65 6e 63 65 20  tion (and hence 
36df2 52 45 47 45 58 50 0a 2a 2a 20 20 20 20 20 6f 70  REGEXP.**     op
36df3 65 72 61 74 6f 72 29 20 75 73 69 6e 67 20 74 68  erator) using th
36df4 65 20 49 43 55 20 75 72 65 67 65 78 5f 58 58 28  e ICU uregex_XX(
36df5 29 20 41 50 49 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  ) APIs..**.**   
36df6 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
36df7 73 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61  s of the SQL sca
36df8 6c 61 72 20 75 70 70 65 72 28 29 20 61 6e 64 20  lar upper() and 
36df9 6c 6f 77 65 72 28 29 20 66 75 6e 63 74 69 6f 6e  lower() function
36dfa 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 63 61 73  s.**     for cas
36dfb 65 20 6d 61 70 70 69 6e 67 2e 0a 2a 2a 0a 2a 2a  e mapping..**.**
36dfc 20 20 20 2a 20 49 6e 74 65 67 72 61 74 69 6f 6e     * Integration
36dfd 20 6f 66 20 49 43 55 20 61 6e 64 20 53 51 4c 69   of ICU and SQLi
36dfe 74 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  te collation seq
36dff 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a  ences..**.**   *
36e00 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   An implementati
36e01 6f 6e 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f  on of the LIKE o
36e02 70 65 72 61 74 6f 72 20 74 68 61 74 20 75 73 65  perator that use
36e03 73 20 49 43 55 20 74 6f 20 0a 2a 2a 20 20 20 20  s ICU to .**    
36e04 20 70 72 6f 76 69 64 65 20 63 61 73 65 2d 69 6e   provide case-in
36e05 64 65 70 65 6e 64 65 6e 74 20 6d 61 74 63 68 69  dependent matchi
36e06 6e 67 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66  ng..*/..#if !def
36e07 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
36e08 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
36e09 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 29 0a  ITE_ENABLE_ICU).
36e0a 0a 2f 2a 20 49 6e 63 6c 75 64 65 20 49 43 55 20  ./* Include ICU 
36e0b 68 65 61 64 65 72 73 20 2a 2f 0a 23 69 6e 63 6c  headers */.#incl
36e0c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 74 79  ude <unicode/uty
36e0d 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  pes.h>.#include 
36e0e 3c 75 6e 69 63 6f 64 65 2f 75 72 65 67 65 78 2e  <unicode/uregex.
36e0f 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
36e10 63 6f 64 65 2f 75 73 74 72 69 6e 67 2e 68 3e 0a  code/ustring.h>.
36e11 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
36e12 65 2f 75 63 6f 6c 2e 68 3e 0a 0a 0a 23 69 66 6e  e/ucol.h>...#ifn
36e13 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a  def SQLITE_CORE.
36e14 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
36e15 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65 0a 23  ON_INIT1.#else.#
36e16 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78  endif../*.** Max
36e17 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69 6e 20  imum length (in 
36e18 62 79 74 65 73 29 20 6f 66 20 74 68 65 20 70 61  bytes) of the pa
36e19 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b 45 20  ttern in a LIKE 
36e1a 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65 72 61  or GLOB.** opera
36e1b 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  tor..*/.#ifndef 
36e1c 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f  SQLITE_MAX_LIKE_
36e1d 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0a 23  PATTERN_LENGTH.#
36e1e 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
36e1f 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f  AX_LIKE_PATTERN_
36e20 4c 45 4e 47 54 48 20 35 30 30 30 30 0a 23 65 6e  LENGTH 50000.#en
36e21 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 73 69  dif../*.** Versi
36e22 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 72  on of sqlite3_fr
36e23 65 65 28 29 20 74 68 61 74 20 69 73 20 61 6c 77  ee() that is alw
36e24 61 79 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20  ays a function, 
36e25 6e 65 76 65 72 20 61 20 6d 61 63 72 6f 2e 0a 2a  never a macro..*
36e26 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 78 46  /.static void xF
36e27 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ree(void *p){.  
36e28 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
36e29 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72  .}../*.** Compar
36e2a 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69  e two UTF-8 stri
36e2b 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79  ngs for equality
36e2c 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74   where the first
36e2d 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 20   string is.** a 
36e2e 22 4c 49 4b 45 22 20 65 78 70 72 65 73 73 69 6f  "LIKE" expressio
36e2f 6e 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 28  n. Return true (
36e30 31 29 20 69 66 20 74 68 65 79 20 61 72 65 20 74  1) if they are t
36e31 68 65 20 73 61 6d 65 20 61 6e 64 20 0a 2a 2a 20  he same and .** 
36e32 66 61 6c 73 65 20 28 30 29 20 69 66 20 74 68 65  false (0) if the
36e33 79 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e  y are different.
36e34 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
36e35 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28 0a 20  cuLikeCompare(. 
36e36 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74 20 2a   const uint8_t *
36e37 7a 50 61 74 74 65 72 6e 2c 20 20 20 2f 2a 20 4c  zPattern,   /* L
36e38 49 4b 45 20 70 61 74 74 65 72 6e 20 2a 2f 0a 20  IKE pattern */. 
36e39 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74 20 2a   const uint8_t *
36e3a 7a 53 74 72 69 6e 67 2c 20 20 20 20 2f 2a 20 54  zString,    /* T
36e3b 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20  he UTF-8 string 
36e3c 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e  to compare again
36e3d 73 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 55 43  st */.  const UC
36e3e 68 61 72 33 32 20 75 45 73 63 20 20 20 20 20 20  har32 uEsc      
36e3f 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65     /* The escape
36e40 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 29 7b   character */.){
36e41 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
36e42 69 6e 74 20 4d 41 54 43 48 5f 4f 4e 45 20 3d 20  int MATCH_ONE = 
36e43 28 55 43 68 61 72 33 32 29 27 5f 27 3b 0a 20 20  (UChar32)'_';.  
36e44 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74  static const int
36e45 20 4d 41 54 43 48 5f 41 4c 4c 20 3d 20 28 55 43   MATCH_ALL = (UC
36e46 68 61 72 33 32 29 27 25 27 3b 0a 0a 20 20 69 6e  har32)'%';..  in
36e47 74 20 69 50 61 74 74 65 72 6e 20 3d 20 30 3b 20  t iPattern = 0; 
36e48 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
36e49 20 62 79 74 65 20 69 6e 64 65 78 20 69 6e 20 7a   byte index in z
36e4a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74  Pattern */.  int
36e4b 20 69 53 74 72 69 6e 67 20 3d 20 30 3b 20 20 20   iString = 0;   
36e4c 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
36e4d 62 79 74 65 20 69 6e 64 65 78 20 69 6e 20 7a 53  byte index in zS
36e4e 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 69 6e 74 20  tring */..  int 
36e4f 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 20  prevEscape = 0; 
36e50 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
36e51 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 61 72  he previous char
36e52 61 63 74 65 72 20 77 61 73 20 75 45 73 63 20 2a  acter was uEsc *
36e53 2f 0a 0a 20 20 77 68 69 6c 65 28 20 7a 50 61 74  /..  while( zPat
36e54 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 21 3d  tern[iPattern]!=
36e55 30 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 52 65 61  0 ){..    /* Rea
36e56 64 20 28 61 6e 64 20 63 6f 6e 73 75 6d 65 29 20  d (and consume) 
36e57 74 68 65 20 6e 65 78 74 20 63 68 61 72 61 63 74  the next charact
36e58 65 72 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75  er from the inpu
36e59 74 20 70 61 74 74 65 72 6e 2e 20 2a 2f 0a 20 20  t pattern. */.  
36e5a 20 20 55 43 68 61 72 33 32 20 75 50 61 74 74 65    UChar32 uPatte
36e5b 72 6e 3b 0a 20 20 20 20 55 38 5f 4e 45 58 54 5f  rn;.    U8_NEXT_
36e5c 55 4e 53 41 46 45 28 7a 50 61 74 74 65 72 6e 2c  UNSAFE(zPattern,
36e5d 20 69 50 61 74 74 65 72 6e 2c 20 75 50 61 74 74   iPattern, uPatt
36e5e 65 72 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ern);.    assert
36e5f 28 75 50 61 74 74 65 72 6e 21 3d 30 29 3b 0a 0a  (uPattern!=0);..
36e60 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65      /* There are
36e61 20 6e 6f 77 20 34 20 70 6f 73 73 69 62 69 6c 69   now 4 possibili
36e62 74 69 65 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  ties:.    **.   
36e63 20 2a 2a 20 20 20 20 20 31 2e 20 75 50 61 74 74   **     1. uPatt
36e64 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61  ern is an unesca
36e65 70 65 64 20 6d 61 74 63 68 2d 61 6c 6c 20 63 68  ped match-all ch
36e66 61 72 61 63 74 65 72 20 22 25 22 2c 0a 20 20 20  aracter "%",.   
36e67 20 2a 2a 20 20 20 20 20 32 2e 20 75 50 61 74 74   **     2. uPatt
36e68 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61  ern is an unesca
36e69 70 65 64 20 6d 61 74 63 68 2d 6f 6e 65 20 63 68  ped match-one ch
36e6a 61 72 61 63 74 65 72 20 22 5f 22 2c 0a 20 20 20  aracter "_",.   
36e6b 20 2a 2a 20 20 20 20 20 33 2e 20 75 50 61 74 74   **     3. uPatt
36e6c 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61  ern is an unesca
36e6d 70 65 64 20 65 73 63 61 70 65 20 63 68 61 72 61  ped escape chara
36e6e 63 74 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20  cter, or.    ** 
36e6f 20 20 20 20 34 2e 20 75 50 61 74 74 65 72 6e 20      4. uPattern 
36e70 69 73 20 74 6f 20 62 65 20 68 61 6e 64 6c 65 64  is to be handled
36e71 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20   as an ordinary 
36e72 63 68 61 72 61 63 74 65 72 0a 20 20 20 20 2a 2f  character.    */
36e73 0a 20 20 20 20 69 66 28 20 21 70 72 65 76 45 73  .    if( !prevEs
36e74 63 61 70 65 20 26 26 20 75 50 61 74 74 65 72 6e  cape && uPattern
36e75 3d 3d 4d 41 54 43 48 5f 41 4c 4c 20 29 7b 0a 20  ==MATCH_ALL ){. 
36e76 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31 2e 20       /* Case 1. 
36e77 2a 2f 0a 20 20 20 20 20 20 75 69 6e 74 38 5f 74  */.      uint8_t
36e78 20 63 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 6b   c;..      /* Sk
36e79 69 70 20 61 6e 79 20 4d 41 54 43 48 5f 41 4c 4c  ip any MATCH_ALL
36e7a 20 6f 72 20 4d 41 54 43 48 5f 4f 4e 45 20 63 68   or MATCH_ONE ch
36e7b 61 72 61 63 74 65 72 73 20 74 68 61 74 20 66 6f  aracters that fo
36e7c 6c 6c 6f 77 20 61 0a 20 20 20 20 20 20 2a 2a 20  llow a.      ** 
36e7d 4d 41 54 43 48 5f 41 4c 4c 2e 20 46 6f 72 20 65  MATCH_ALL. For e
36e7e 61 63 68 20 4d 41 54 43 48 5f 4f 4e 45 2c 20 73  ach MATCH_ONE, s
36e7f 6b 69 70 20 6f 6e 65 20 63 68 61 72 61 63 74 65  kip one characte
36e80 72 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20  r in the .      
36e81 2a 2a 20 74 65 73 74 20 73 74 72 69 6e 67 2e 0a  ** test string..
36e82 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 77        */.      w
36e83 68 69 6c 65 28 20 28 63 3d 7a 50 61 74 74 65 72  hile( (c=zPatter
36e84 6e 5b 69 50 61 74 74 65 72 6e 5d 29 20 3d 3d 20  n[iPattern]) == 
36e85 4d 41 54 43 48 5f 41 4c 4c 20 7c 7c 20 63 20 3d  MATCH_ALL || c =
36e86 3d 20 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20  = MATCH_ONE ){. 
36e87 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 4d 41         if( c==MA
36e88 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20 20 20 20  TCH_ONE ){.     
36e89 20 20 20 20 20 69 66 28 20 7a 53 74 72 69 6e 67       if( zString
36e8a 5b 69 53 74 72 69 6e 67 5d 3d 3d 30 20 29 20 72  [iString]==0 ) r
36e8b 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20  eturn 0;.       
36e8c 20 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41     U8_FWD_1_UNSA
36e8d 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72  FE(zString, iStr
36e8e 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ing);.        }.
36e8f 20 20 20 20 20 20 20 20 69 50 61 74 74 65 72 6e          iPattern
36e90 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ++;.      }..   
36e91 20 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e 5b     if( zPattern[
36e92 69 50 61 74 74 65 72 6e 5d 3d 3d 30 20 29 20 72  iPattern]==0 ) r
36e93 65 74 75 72 6e 20 31 3b 0a 0a 20 20 20 20 20 20  eturn 1;..      
36e94 77 68 69 6c 65 28 20 7a 53 74 72 69 6e 67 5b 69  while( zString[i
36e95 53 74 72 69 6e 67 5d 20 29 7b 0a 20 20 20 20 20  String] ){.     
36e96 20 20 20 69 66 28 20 69 63 75 4c 69 6b 65 43 6f     if( icuLikeCo
36e97 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e 5b  mpare(&zPattern[
36e98 69 50 61 74 74 65 72 6e 5d 2c 20 26 7a 53 74 72  iPattern], &zStr
36e99 69 6e 67 5b 69 53 74 72 69 6e 67 5d 2c 20 75 45  ing[iString], uE
36e9a 73 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  sc) ){.         
36e9b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
36e9c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 55 38 5f     }.        U8_
36e9d 46 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74  FWD_1_UNSAFE(zSt
36e9e 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a  ring, iString);.
36e9f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
36ea0 74 75 72 6e 20 30 3b 0a 0a 20 20 20 20 7d 65 6c  turn 0;..    }el
36ea1 73 65 20 69 66 28 20 21 70 72 65 76 45 73 63 61  se if( !prevEsca
36ea2 70 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d 3d  pe && uPattern==
36ea3 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20 20  MATCH_ONE ){.   
36ea4 20 20 20 2f 2a 20 43 61 73 65 20 32 2e 20 2a 2f     /* Case 2. */
36ea5 0a 20 20 20 20 20 20 69 66 28 20 7a 53 74 72 69  .      if( zStri
36ea6 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30 20 29  ng[iString]==0 )
36ea7 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
36ea8 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46 45   U8_FWD_1_UNSAFE
36ea9 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69 6e  (zString, iStrin
36eaa 67 29 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 20 69  g);..    }else i
36eab 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26  f( !prevEscape &
36eac 26 20 75 50 61 74 74 65 72 6e 3d 3d 75 45 73 63  & uPattern==uEsc
36ead 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65  ){.      /* Case
36eae 20 33 2e 20 2a 2f 0a 20 20 20 20 20 20 70 72 65   3. */.      pre
36eaf 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 0a 20 20  vEscape = 1;..  
36eb0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
36eb1 2a 20 43 61 73 65 20 34 2e 20 2a 2f 0a 20 20 20  * Case 4. */.   
36eb2 20 20 20 55 43 68 61 72 33 32 20 75 53 74 72 69     UChar32 uStri
36eb3 6e 67 3b 0a 20 20 20 20 20 20 55 38 5f 4e 45 58  ng;.      U8_NEX
36eb4 54 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67  T_UNSAFE(zString
36eb5 2c 20 69 53 74 72 69 6e 67 2c 20 75 53 74 72 69  , iString, uStri
36eb6 6e 67 29 3b 0a 20 20 20 20 20 20 75 53 74 72 69  ng);.      uStri
36eb7 6e 67 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28  ng = u_foldCase(
36eb8 75 53 74 72 69 6e 67 2c 20 55 5f 46 4f 4c 44 5f  uString, U_FOLD_
36eb9 43 41 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a 20  CASE_DEFAULT);. 
36eba 20 20 20 20 20 75 50 61 74 74 65 72 6e 20 3d 20       uPattern = 
36ebb 75 5f 66 6f 6c 64 43 61 73 65 28 75 50 61 74 74  u_foldCase(uPatt
36ebc 65 72 6e 2c 20 55 5f 46 4f 4c 44 5f 43 41 53 45  ern, U_FOLD_CASE
36ebd 5f 44 45 46 41 55 4c 54 29 3b 0a 20 20 20 20 20  _DEFAULT);.     
36ebe 20 69 66 28 20 75 53 74 72 69 6e 67 21 3d 75 50   if( uString!=uP
36ebf 61 74 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20  attern ){.      
36ec0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
36ec1 20 20 7d 0a 20 20 20 20 20 20 70 72 65 76 45 73    }.      prevEs
36ec2 63 61 70 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  cape = 0;.    }.
36ec3 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 7a 53    }..  return zS
36ec4 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d  tring[iString]==
36ec5 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  0;.}../*.** Impl
36ec6 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
36ec7 65 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 6e  e like() SQL fun
36ec8 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e  ction.  This fun
36ec9 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73  ction implements
36eca 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e  .** the build-in
36ecb 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20   LIKE operator. 
36ecc 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
36ecd 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
36ece 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70 61  ion is the.** pa
36ecf 74 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 65  ttern and the se
36ed0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
36ed1 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 6f   the string.  So
36ed2 2c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  , the SQL statem
36ed3 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ents:.**.**     
36ed4 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a    A LIKE B.**.**
36ed5 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
36ed6 61 73 20 6c 69 6b 65 28 42 2c 20 41 29 2e 20 49  as like(B, A). I
36ed7 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 73  f there is an es
36ed8 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20 45  cape character E
36ed9 2c 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41  , .**.**       A
36eda 20 4c 49 4b 45 20 42 20 45 53 43 41 50 45 20 45   LIKE B ESCAPE E
36edb 0a 2a 2a 0a 2a 2a 20 69 73 20 6d 61 70 70 65 64  .**.** is mapped
36edc 20 74 6f 20 6c 69 6b 65 28 42 2c 20 41 2c 20 45   to like(B, A, E
36edd 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
36ede 64 20 69 63 75 4c 69 6b 65 46 75 6e 63 28 0a 20  d icuLikeFunc(. 
36edf 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
36ee0 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e   *context, .  in
36ee1 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74  t argc, .  sqlit
36ee2 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
36ee3 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
36ee4 6e 65 64 20 63 68 61 72 20 2a 7a 41 20 3d 20 73  ned char *zA = s
36ee5 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
36ee6 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f  t(argv[0]);.  co
36ee7 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
36ee8 72 20 2a 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f  r *zB = sqlite3_
36ee9 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
36eea 31 5d 29 3b 0a 20 20 55 43 68 61 72 33 32 20 75  1]);.  UChar32 u
36eeb 45 73 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c  Esc = 0;..  /* L
36eec 69 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20  imit the length 
36eed 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47  of the LIKE or G
36eee 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61  LOB pattern to a
36eef 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20  void problems.  
36ef0 2a 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72  ** of deep recur
36ef1 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68  sion and N*N beh
36ef2 61 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e  avior in pattern
36ef3 43 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a  Compare()..  */.
36ef4 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
36ef5 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
36ef6 5d 29 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  ])>SQLITE_MAX_LI
36ef7 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
36ef8 48 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  H ){.    sqlite3
36ef9 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
36efa 6e 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20  ntext, "LIKE or 
36efb 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f  GLOB pattern too
36efc 20 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a   complex", -1);.
36efd 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
36efe 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20  ..  if( argc==3 
36eff 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73  ){.    /* The es
36f00 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20 73  cape character s
36f01 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69  tring must consi
36f02 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55  st of a single U
36f03 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a  TF-8 character..
36f04 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65      ** Otherwise
36f05 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
36f06 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  r..    */.    in
36f07 74 20 6e 45 3d 20 73 71 6c 69 74 65 33 5f 76 61  t nE= sqlite3_va
36f08 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32  lue_bytes(argv[2
36f09 5d 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e  ]);.    const un
36f0a 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 20  signed char *zE 
36f0b 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
36f0c 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  text(argv[2]);. 
36f0d 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20     int i = 0;.  
36f0e 20 20 69 66 28 20 7a 45 3d 3d 30 20 29 20 72 65    if( zE==0 ) re
36f0f 74 75 72 6e 3b 0a 20 20 20 20 55 38 5f 4e 45 58  turn;.    U8_NEX
36f10 54 28 7a 45 2c 20 69 2c 20 6e 45 2c 20 75 45 73  T(zE, i, nE, uEs
36f11 63 29 3b 0a 20 20 20 20 69 66 28 20 69 21 3d 6e  c);.    if( i!=n
36f12 45 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  E){.      sqlite
36f13 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
36f14 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20  ontext, .       
36f15 20 20 20 22 45 53 43 41 50 45 20 65 78 70 72 65     "ESCAPE expre
36f16 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 20  ssion must be a 
36f17 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72  single character
36f18 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65  ", -1);.      re
36f19 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
36f1a 0a 20 20 69 66 28 20 7a 41 20 26 26 20 7a 42 20  .  if( zA && zB 
36f1b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
36f1c 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78  esult_int(contex
36f1d 74 2c 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72  t, icuLikeCompar
36f1e 65 28 7a 41 2c 20 7a 42 2c 20 75 45 73 63 29 29  e(zA, zB, uEsc))
36f1f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
36f20 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
36f21 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49  called when an I
36f22 43 55 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c  CU function call
36f23 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a  ed from within.*
36f24 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  * the implementa
36f25 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 73  tion of an SQL s
36f26 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 72  calar function r
36f27 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e  eturns an error.
36f28 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61  .**.** The scala
36f29 72 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65  r function conte
36f2a 78 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  xt passed as the
36f2b 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
36f2c 69 73 20 0a 2a 2a 20 6c 6f 61 64 65 64 20 77 69  is .** loaded wi
36f2d 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
36f2e 61 67 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65  age based on the
36f2f 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61   following two a
36f30 72 67 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  rgs..*/.static v
36f31 6f 69 64 20 69 63 75 46 75 6e 63 74 69 6f 6e 45  oid icuFunctionE
36f32 72 72 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 5f  rror(.  sqlite3_
36f33 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20  context *pCtx,  
36f34 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 73       /* SQLite s
36f35 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 63  calar function c
36f36 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73  ontext */.  cons
36f37 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
36f38 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
36f39 20 6f 66 20 49 43 55 20 66 75 6e 63 74 69 6f 6e   of ICU function
36f3a 20 74 68 61 74 20 66 61 69 6c 65 64 20 2a 2f 0a   that failed */.
36f3b 20 20 55 45 72 72 6f 72 43 6f 64 65 20 65 20 20    UErrorCode e  
36f3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36f3d 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74  * Error code ret
36f3e 75 72 6e 65 64 20 62 79 20 49 43 55 20 66 75 6e  urned by ICU fun
36f3f 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 63 68  ction */.){.  ch
36f40 61 72 20 7a 42 75 66 5b 31 32 38 5d 3b 0a 20 20  ar zBuf[128];.  
36f41 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
36f42 28 31 32 38 2c 20 7a 42 75 66 2c 20 22 49 43 55  (128, zBuf, "ICU
36f43 20 65 72 72 6f 72 3a 20 25 73 28 29 3a 20 25 73   error: %s(): %s
36f44 22 2c 20 7a 4e 61 6d 65 2c 20 75 5f 65 72 72 6f  ", zName, u_erro
36f45 72 4e 61 6d 65 28 65 29 29 3b 0a 20 20 7a 42 75  rName(e));.  zBu
36f46 66 5b 31 32 37 5d 20 3d 20 27 5c 30 27 3b 0a 20  f[127] = '\0';. 
36f47 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
36f48 65 72 72 6f 72 28 70 43 74 78 2c 20 7a 42 75 66  error(pCtx, zBuf
36f49 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  , -1);.}../*.** 
36f4a 46 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65  Function to dele
36f4b 74 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67 65  te compiled rege
36f4c 78 70 20 6f 62 6a 65 63 74 73 2e 20 52 65 67 69  xp objects. Regi
36f4d 73 74 65 72 65 64 20 61 73 0a 2a 2a 20 61 20 64  stered as.** a d
36f4e 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
36f4f 6f 6e 20 77 69 74 68 20 73 71 6c 69 74 65 33 5f  on with sqlite3_
36f50 73 65 74 5f 61 75 78 64 61 74 61 28 29 2e 0a 2a  set_auxdata()..*
36f51 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
36f52 75 52 65 67 65 78 70 44 65 6c 65 74 65 28 76 6f  uRegexpDelete(vo
36f53 69 64 20 2a 70 29 7b 0a 20 20 55 52 65 67 75 6c  id *p){.  URegul
36f54 61 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 70 45  arExpression *pE
36f55 78 70 72 20 3d 20 28 55 52 65 67 75 6c 61 72 45  xpr = (URegularE
36f56 78 70 72 65 73 73 69 6f 6e 20 2a 29 70 3b 0a 20  xpression *)p;. 
36f57 20 75 72 65 67 65 78 5f 63 6c 6f 73 65 28 70 45   uregex_close(pE
36f58 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  xpr);.}../*.** I
36f59 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
36f5a 20 53 51 4c 69 74 65 20 52 45 47 45 58 50 20 6f   SQLite REGEXP o
36f5b 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 63  perator. This sc
36f5c 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 61  alar function ta
36f5d 6b 65 73 0a 2a 2a 20 74 77 6f 20 61 72 67 75 6d  kes.** two argum
36f5e 65 6e 74 73 2e 20 54 68 65 20 66 69 72 73 74 20  ents. The first 
36f5f 69 73 20 61 20 72 65 67 75 6c 61 72 20 65 78 70  is a regular exp
36f60 72 65 73 73 69 6f 6e 20 70 61 74 74 65 72 6e 20  ression pattern 
36f61 74 6f 20 63 6f 6d 70 69 6c 65 0a 2a 2a 20 74 68  to compile.** th
36f62 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 73 74  e second is a st
36f63 72 69 6e 67 20 74 6f 20 6d 61 74 63 68 20 61 67  ring to match ag
36f64 61 69 6e 73 74 20 74 68 61 74 20 70 61 74 74 65  ainst that patte
36f65 72 6e 2e 20 49 66 20 65 69 74 68 65 72 20 0a 2a  rn. If either .*
36f66 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e  * argument is an
36f67 20 53 51 4c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   SQL NULL, then 
36f68 4e 55 4c 4c 20 49 73 20 72 65 74 75 72 6e 65 64  NULL Is returned
36f69 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  . Otherwise, the
36f6a 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 31 20   result.** is 1 
36f6b 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 6d 61  if the string ma
36f6c 74 63 68 65 73 20 74 68 65 20 70 61 74 74 65 72  tches the patter
36f6d 6e 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73  n, or 0 otherwis
36f6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  e..**.** SQLite 
36f6f 6d 61 70 73 20 74 68 65 20 72 65 67 65 78 70 28  maps the regexp(
36f70 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68  ) function to th
36f71 65 20 72 65 67 65 78 70 28 29 20 6f 70 65 72 61  e regexp() opera
36f72 74 6f 72 20 73 75 63 68 0a 2a 2a 20 74 68 61 74  tor such.** that
36f73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
36f74 77 6f 20 61 72 65 20 65 71 75 69 76 61 6c 65 6e  wo are equivalen
36f75 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 7a 53 74  t:.**.**     zSt
36f76 72 69 6e 67 20 52 45 47 45 58 50 20 7a 50 61 74  ring REGEXP zPat
36f77 74 65 72 6e 0a 2a 2a 20 20 20 20 20 72 65 67 65  tern.**     rege
36f78 78 70 28 7a 50 61 74 74 65 72 6e 2c 20 7a 53 74  xp(zPattern, zSt
36f79 72 69 6e 67 29 0a 2a 2a 0a 2a 2a 20 55 73 65 73  ring).**.** Uses
36f7a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 49   the following I
36f7b 43 55 20 72 65 67 65 78 70 20 41 50 49 73 3a 0a  CU regexp APIs:.
36f7c 2a 2a 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78  **.**     uregex
36f7d 5f 6f 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 75  _open().**     u
36f7e 72 65 67 65 78 5f 6d 61 74 63 68 65 73 28 29 0a  regex_matches().
36f7f 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f 63 6c  **     uregex_cl
36f80 6f 73 65 28 29 0a 2a 2f 0a 73 74 61 74 69 63 20  ose().*/.static 
36f81 76 6f 69 64 20 69 63 75 52 65 67 65 78 70 46 75  void icuRegexpFu
36f82 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nc(sqlite3_conte
36f83 78 74 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c  xt *p, int nArg,
36f84 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
36f85 2a 61 70 41 72 67 29 7b 0a 20 20 55 45 72 72 6f  *apArg){.  UErro
36f86 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55  rCode status = U
36f87 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 55  _ZERO_ERROR;.  U
36f88 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f  RegularExpressio
36f89 6e 20 2a 70 45 78 70 72 3b 0a 20 20 55 42 6f 6f  n *pExpr;.  UBoo
36f8a 6c 20 72 65 73 3b 0a 20 20 63 6f 6e 73 74 20 55  l res;.  const U
36f8b 43 68 61 72 20 2a 7a 53 74 72 69 6e 67 20 3d 20  Char *zString = 
36f8c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
36f8d 78 74 31 36 28 61 70 41 72 67 5b 31 5d 29 3b 0a  xt16(apArg[1]);.
36f8e 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6c 65 66  .  /* If the lef
36f8f 74 20 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74  t hand side of t
36f90 68 65 20 72 65 67 65 78 70 20 6f 70 65 72 61 74  he regexp operat
36f91 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 0a 20 20 2a  or is NULL, .  *
36f92 2a 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  * then the resul
36f93 74 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20  t is also NULL. 
36f94 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 53 74  .  */.  if( !zSt
36f95 72 69 6e 67 20 29 7b 0a 20 20 20 20 72 65 74 75  ring ){.    retu
36f96 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 45 78 70 72  rn;.  }..  pExpr
36f97 20 3d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61   = sqlite3_get_a
36f98 75 78 64 61 74 61 28 70 2c 20 30 29 3b 0a 20 20  uxdata(p, 0);.  
36f99 69 66 28 20 21 70 45 78 70 72 20 29 7b 0a 20 20  if( !pExpr ){.  
36f9a 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a    const UChar *z
36f9b 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65  Pattern = sqlite
36f9c 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61  3_value_text16(a
36f9d 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66  pArg[0]);.    if
36f9e 28 20 21 7a 50 61 74 74 65 72 6e 20 29 7b 0a 20  ( !zPattern ){. 
36f9f 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
36fa0 20 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 75   }.    pExpr = u
36fa1 72 65 67 65 78 5f 6f 70 65 6e 28 7a 50 61 74 74  regex_open(zPatt
36fa2 65 72 6e 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 26  ern, -1, 0, 0, &
36fa3 73 74 61 74 75 73 29 3b 0a 0a 20 20 20 20 69 66  status);..    if
36fa4 28 20 55 5f 53 55 43 43 45 53 53 28 73 74 61 74  ( U_SUCCESS(stat
36fa5 75 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  us) ){.      sql
36fa6 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
36fa7 28 70 2c 20 30 2c 20 70 45 78 70 72 2c 20 69 63  (p, 0, pExpr, ic
36fa8 75 52 65 67 65 78 70 44 65 6c 65 74 65 29 3b 0a  uRegexpDelete);.
36fa9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36faa 20 61 73 73 65 72 74 28 21 70 45 78 70 72 29 3b   assert(!pExpr);
36fab 0a 20 20 20 20 20 20 69 63 75 46 75 6e 63 74 69  .      icuFuncti
36fac 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65 67  onError(p, "ureg
36fad 65 78 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73  ex_open", status
36fae 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  );.      return;
36faf 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
36fb0 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 74   Configure the t
36fb1 65 78 74 20 74 68 61 74 20 74 68 65 20 72 65 67  ext that the reg
36fb2 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20  ular expression 
36fb3 6f 70 65 72 61 74 65 73 20 6f 6e 2e 20 2a 2f 0a  operates on. */.
36fb4 20 20 75 72 65 67 65 78 5f 73 65 74 54 65 78 74    uregex_setText
36fb5 28 70 45 78 70 72 2c 20 7a 53 74 72 69 6e 67 2c  (pExpr, zString,
36fb6 20 2d 31 2c 20 26 73 74 61 74 75 73 29 3b 0a 20   -1, &status);. 
36fb7 20 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28   if( !U_SUCCESS(
36fb8 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69  status) ){.    i
36fb9 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28  cuFunctionError(
36fba 70 2c 20 22 75 72 65 67 65 78 5f 73 65 74 54 65  p, "uregex_setTe
36fbb 78 74 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20  xt", status);.  
36fbc 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
36fbd 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 68 65 20   /* Attempt the 
36fbe 6d 61 74 63 68 20 2a 2f 0a 20 20 72 65 73 20 3d  match */.  res =
36fbf 20 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73 28   uregex_matches(
36fc0 70 45 78 70 72 2c 20 30 2c 20 26 73 74 61 74 75  pExpr, 0, &statu
36fc1 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43  s);.  if( !U_SUC
36fc2 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
36fc3 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45      icuFunctionE
36fc4 72 72 6f 72 28 70 2c 20 22 75 72 65 67 65 78 5f  rror(p, "uregex_
36fc5 6d 61 74 63 68 65 73 22 2c 20 73 74 61 74 75 73  matches", status
36fc6 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
36fc7 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
36fc8 20 74 65 78 74 20 74 68 61 74 20 74 68 65 20 72   text that the r
36fc9 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
36fca 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e 20 74 6f  n operates on to
36fcb 20 61 20 4e 55 4c 4c 0a 20 20 2a 2a 20 70 6f 69   a NULL.  ** poi
36fcc 6e 74 65 72 2e 20 54 68 69 73 20 69 73 20 6e 6f  nter. This is no
36fcd 74 20 72 65 61 6c 6c 79 20 6e 65 63 65 73 73 61  t really necessa
36fce 72 79 2c 20 62 75 74 20 69 74 20 69 73 20 74 69  ry, but it is ti
36fcf 64 69 65 72 20 74 68 61 6e 20 0a 20 20 2a 2a 20  dier than .  ** 
36fd0 6c 65 61 76 69 6e 67 20 74 68 65 20 72 65 67 75  leaving the regu
36fd1 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  lar expression o
36fd2 62 6a 65 63 74 20 63 6f 6e 66 69 67 75 72 65 64  bject configured
36fd3 20 77 69 74 68 20 61 6e 20 69 6e 76 61 6c 69 64   with an invalid
36fd4 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 61 66  .  ** pointer af
36fd5 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ter this functio
36fd6 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a  n returns..  */.
36fd7 20 20 75 72 65 67 65 78 5f 73 65 74 54 65 78 74    uregex_setText
36fd8 28 70 45 78 70 72 2c 20 30 2c 20 30 2c 20 26 73  (pExpr, 0, 0, &s
36fd9 74 61 74 75 73 29 3b 0a 0a 20 20 2f 2a 20 52 65  tatus);..  /* Re
36fda 74 75 72 6e 20 31 20 6f 72 20 30 2e 20 2a 2f 0a  turn 1 or 0. */.
36fdb 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
36fdc 5f 69 6e 74 28 70 2c 20 72 65 73 20 3f 20 31 20  _int(p, res ? 1 
36fdd 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  : 0);.}../*.** I
36fde 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
36fdf 66 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  f scalar functio
36fe0 6e 73 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70  ns for case mapp
36fe1 69 6e 67 20 2d 20 75 70 70 65 72 28 29 20 61 6e  ing - upper() an
36fe2 64 20 0a 2a 2a 20 6c 6f 77 65 72 28 29 2e 20 46  d .** lower(). F
36fe3 75 6e 63 74 69 6f 6e 20 75 70 70 65 72 28 29 20  unction upper() 
36fe4 63 6f 6e 76 65 72 74 73 20 69 74 73 20 69 6e 70  converts its inp
36fe5 75 74 20 74 6f 20 75 70 70 65 72 2d 63 61 73 65  ut to upper-case
36fe6 20 28 41 42 43 29 2e 0a 2a 2a 20 46 75 6e 63 74   (ABC)..** Funct
36fe7 69 6f 6e 20 6c 6f 77 65 72 28 29 20 63 6f 6e 76  ion lower() conv
36fe8 65 72 74 73 20 74 6f 20 6c 6f 77 65 72 2d 63 61  erts to lower-ca
36fe9 73 65 20 28 61 62 63 29 2e 0a 2a 2a 0a 2a 2a 20  se (abc)..**.** 
36fea 49 43 55 20 70 72 6f 76 69 64 65 73 20 74 77 6f  ICU provides two
36feb 20 74 79 70 65 73 20 6f 66 20 63 61 73 65 20 6d   types of case m
36fec 61 70 70 69 6e 67 2c 20 22 67 65 6e 65 72 61 6c  apping, "general
36fed 22 20 63 61 73 65 20 6d 61 70 70 69 6e 67 20 61  " case mapping a
36fee 6e 64 0a 2a 2a 20 22 6c 61 6e 67 75 61 67 65 20  nd.** "language 
36fef 73 70 65 63 69 66 69 63 22 2e 20 52 65 66 65 72  specific". Refer
36ff0 20 74 6f 20 49 43 55 20 64 6f 63 75 6d 65 6e 74   to ICU document
36ff1 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 64 69  ation for the di
36ff2 66 66 65 72 65 6e 63 65 73 0a 2a 2a 20 62 65 74  fferences.** bet
36ff3 77 65 65 6e 20 74 68 65 20 74 77 6f 2e 0a 2a 2a  ween the two..**
36ff4 0a 2a 2a 20 54 6f 20 75 74 69 6c 69 73 65 20 22  .** To utilise "
36ff5 67 65 6e 65 72 61 6c 22 20 63 61 73 65 20 6d 61  general" case ma
36ff6 70 70 69 6e 67 2c 20 74 68 65 20 75 70 70 65 72  pping, the upper
36ff7 28 29 20 6f 72 20 6c 6f 77 65 72 28 29 20 73 63  () or lower() sc
36ff8 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74 69 6f  alar .** functio
36ff9 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 77  ns are invoked w
36ffa 69 74 68 20 6f 6e 65 20 61 72 67 75 6d 65 6e 74  ith one argument
36ffb 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75 70 70 65  :.**.**     uppe
36ffc 72 28 27 41 42 43 27 29 20 2d 3e 20 27 61 62 63  r('ABC') -> 'abc
36ffd 27 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27  '.**     lower('
36ffe 61 62 63 27 29 20 2d 3e 20 27 41 42 43 27 0a 2a  abc') -> 'ABC'.*
36fff 2a 0a 2a 2a 20 54 6f 20 61 63 63 65 73 73 20 49  *.** To access I
37000 43 55 20 22 6c 61 6e 67 75 61 67 65 20 73 70 65  CU "language spe
37001 63 69 66 69 63 22 20 63 61 73 65 20 6d 61 70 70  cific" case mapp
37002 69 6e 67 2c 20 75 70 70 65 72 28 29 20 6f 72 20  ing, upper() or 
37003 6c 6f 77 65 72 28 29 0a 2a 2a 20 73 68 6f 75 6c  lower().** shoul
37004 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74  d be invoked wit
37005 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e  h two arguments.
37006 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
37007 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d 65  ment is the name
37008 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 6f 63 61 6c  .** of the local
37009 65 20 74 6f 20 75 73 65 2e 20 50 61 73 73 69 6e  e to use. Passin
3700a 67 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e  g an empty strin
3700b 67 20 28 22 22 29 20 6f 72 20 53 51 4c 20 4e 55  g ("") or SQL NU
3700c 4c 4c 20 76 61 6c 75 65 0a 2a 2a 20 61 73 20 74  LL value.** as t
3700d 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
3700e 6e 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  nt is the same a
3700f 73 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 31  s invoking the 1
37010 20 61 72 67 75 6d 65 6e 74 20 76 65 72 73 69 6f   argument versio
37011 6e 0a 2a 2a 20 6f 66 20 75 70 70 65 72 28 29 20  n.** of upper() 
37012 6f 72 20 6c 6f 77 65 72 28 29 2e 0a 2a 2a 0a 2a  or lower()..**.*
37013 2a 20 20 20 20 20 6c 6f 77 65 72 28 27 49 27 2c  *     lower('I',
37014 20 27 65 6e 5f 75 73 27 29 20 2d 3e 20 27 69 27   'en_us') -> 'i'
37015 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27 49  .**     lower('I
37016 27 2c 20 27 74 72 5f 74 72 27 29 20 2d 3e 20 27  ', 'tr_tr') -> '
37017 c4 b1 27 20 28 73 6d 61 6c 6c 20 64 6f 74 6c 65  ..' (small dotle
37018 73 73 20 69 29 0a 2a 2a 0a 2a 2a 20 68 74 74 70  ss i).**.** http
37019 3a 2f 2f 77 77 77 2e 69 63 75 2d 70 72 6f 6a 65  ://www.icu-proje
3701a 63 74 2e 6f 72 67 2f 75 73 65 72 67 75 69 64 65  ct.org/userguide
3701b 2f 70 6f 73 69 78 2e 68 74 6d 6c 23 63 61 73 65  /posix.html#case
3701c 5f 6d 61 70 70 69 6e 67 73 0a 2a 2f 0a 73 74 61  _mappings.*/.sta
3701d 74 69 63 20 76 6f 69 64 20 69 63 75 43 61 73 65  tic void icuCase
3701e 46 75 6e 63 31 36 28 73 71 6c 69 74 65 33 5f 63  Func16(sqlite3_c
3701f 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e  ontext *p, int n
37020 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  Arg, sqlite3_val
37021 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 63  ue **apArg){.  c
37022 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a 49 6e 70  onst UChar *zInp
37023 75 74 3b 0a 20 20 55 43 68 61 72 20 2a 7a 4f 75  ut;.  UChar *zOu
37024 74 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49 6e 70  tput;.  int nInp
37025 75 74 3b 0a 20 20 69 6e 74 20 6e 4f 75 74 70 75  ut;.  int nOutpu
37026 74 3b 0a 0a 20 20 55 45 72 72 6f 72 43 6f 64 65  t;..  UErrorCode
37027 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f   status = U_ZERO
37028 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73 74 20  _ERROR;.  const 
37029 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 20 3d 20  char *zLocale = 
3702a 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 41 72  0;..  assert(nAr
3702b 67 3d 3d 31 20 7c 7c 20 6e 41 72 67 3d 3d 32 29  g==1 || nArg==2)
3702c 3b 0a 20 20 69 66 28 20 6e 41 72 67 3d 3d 32 20  ;.  if( nArg==2 
3702d 29 7b 0a 20 20 20 20 7a 4c 6f 63 61 6c 65 20 3d  ){.    zLocale =
3702e 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
3702f 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
37030 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a 20 20 7d  t(apArg[1]);.  }
37031 0a 0a 20 20 7a 49 6e 70 75 74 20 3d 20 73 71 6c  ..  zInput = sql
37032 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
37033 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 69  6(apArg[0]);.  i
37034 66 28 20 21 7a 49 6e 70 75 74 20 29 7b 0a 20 20  f( !zInput ){.  
37035 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
37036 6e 49 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33  nInput = sqlite3
37037 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 61  _value_bytes16(a
37038 70 41 72 67 5b 30 5d 29 3b 0a 0a 20 20 6e 4f 75  pArg[0]);..  nOu
37039 74 70 75 74 20 3d 20 6e 49 6e 70 75 74 20 2a 20  tput = nInput * 
3703a 32 20 2b 20 32 3b 0a 20 20 7a 4f 75 74 70 75 74  2 + 2;.  zOutput
3703b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
3703c 63 28 6e 4f 75 74 70 75 74 29 3b 0a 20 20 69 66  c(nOutput);.  if
3703d 28 20 21 7a 4f 75 74 70 75 74 20 29 7b 0a 20 20  ( !zOutput ){.  
3703e 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
3703f 20 69 66 28 20 73 71 6c 69 74 65 33 5f 75 73 65   if( sqlite3_use
37040 72 5f 64 61 74 61 28 70 29 20 29 7b 0a 20 20 20  r_data(p) ){.   
37041 20 75 5f 73 74 72 54 6f 55 70 70 65 72 28 7a 4f   u_strToUpper(zO
37042 75 74 70 75 74 2c 20 6e 4f 75 74 70 75 74 2f 32  utput, nOutput/2
37043 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74  , zInput, nInput
37044 2f 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20 26 73 74  /2, zLocale, &st
37045 61 74 75 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  atus);.  }else{.
37046 20 20 20 20 75 5f 73 74 72 54 6f 4c 6f 77 65 72      u_strToLower
37047 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74 70 75  (zOutput, nOutpu
37048 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  t/2, zInput, nIn
37049 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20  put/2, zLocale, 
3704a 26 73 74 61 74 75 73 29 3b 0a 20 20 7d 0a 0a 20  &status);.  }.. 
3704b 20 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28   if( !U_SUCCESS(
3704c 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69  status) ){.    i
3704d 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28  cuFunctionError(
3704e 70 2c 20 22 75 5f 73 74 72 54 6f 4c 6f 77 65 72  p, "u_strToLower
3704f 28 29 2f 75 5f 73 74 72 54 6f 55 70 70 65 72 22  ()/u_strToUpper"
37050 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20 72  , status);.    r
37051 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 71  eturn;.  }..  sq
37052 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
37053 74 31 36 28 70 2c 20 7a 4f 75 74 70 75 74 2c 20  t16(p, zOutput, 
37054 2d 31 2c 20 78 46 72 65 65 29 3b 0a 7d 0a 0a 2f  -1, xFree);.}../
37055 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73  *.** Collation s
37056 65 71 75 65 6e 63 65 20 64 65 73 74 72 75 63 74  equence destruct
37057 6f 72 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65  or function. The
37058 20 70 43 74 78 20 61 72 67 75 6d 65 6e 74 20 70   pCtx argument p
37059 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 61 20 55 43  oints to.** a UC
3705a 6f 6c 6c 61 74 6f 72 20 73 74 72 75 63 74 75 72  ollator structur
3705b 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  e previously all
3705c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 75 63 6f  ocated using uco
3705d 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61  l_open()..*/.sta
3705e 74 69 63 20 76 6f 69 64 20 69 63 75 43 6f 6c 6c  tic void icuColl
3705f 61 74 69 6f 6e 44 65 6c 28 76 6f 69 64 20 2a 70  ationDel(void *p
37060 43 74 78 29 7b 0a 20 20 55 43 6f 6c 6c 61 74 6f  Ctx){.  UCollato
37061 72 20 2a 70 20 3d 20 28 55 43 6f 6c 6c 61 74 6f  r *p = (UCollato
37062 72 20 2a 29 70 43 74 78 3b 0a 20 20 75 63 6f 6c  r *)pCtx;.  ucol
37063 5f 63 6c 6f 73 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  _close(p);.}../*
37064 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65  .** Collation se
37065 71 75 65 6e 63 65 20 63 6f 6d 70 61 72 69 73 6f  quence compariso
37066 6e 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  n function. The 
37067 70 43 74 78 20 61 72 67 75 6d 65 6e 74 20 70 6f  pCtx argument po
37068 69 6e 74 73 20 74 6f 0a 2a 2a 20 61 20 55 43 6f  ints to.** a UCo
37069 6c 6c 61 74 6f 72 20 73 74 72 75 63 74 75 72 65  llator structure
3706a 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
3706b 63 61 74 65 64 20 75 73 69 6e 67 20 75 63 6f 6c  cated using ucol
3706c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74  _open()..*/.stat
3706d 69 63 20 69 6e 74 20 69 63 75 43 6f 6c 6c 61 74  ic int icuCollat
3706e 69 6f 6e 43 6f 6c 6c 28 0a 20 20 76 6f 69 64 20  ionColl(.  void 
3706f 2a 70 43 74 78 2c 0a 20 20 69 6e 74 20 6e 4c 65  *pCtx,.  int nLe
37070 66 74 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  ft,.  const void
37071 20 2a 7a 4c 65 66 74 2c 0a 20 20 69 6e 74 20 6e   *zLeft,.  int n
37072 52 69 67 68 74 2c 0a 20 20 63 6f 6e 73 74 20 76  Right,.  const v
37073 6f 69 64 20 2a 7a 52 69 67 68 74 0a 29 7b 0a 20  oid *zRight.){. 
37074 20 55 43 6f 6c 6c 61 74 69 6f 6e 52 65 73 75 6c   UCollationResul
37075 74 20 72 65 73 3b 0a 20 20 55 43 6f 6c 6c 61 74  t res;.  UCollat
37076 6f 72 20 2a 70 20 3d 20 28 55 43 6f 6c 6c 61 74  or *p = (UCollat
37077 6f 72 20 2a 29 70 43 74 78 3b 0a 20 20 72 65 73  or *)pCtx;.  res
37078 20 3d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28   = ucol_strcoll(
37079 70 2c 20 28 55 43 68 61 72 20 2a 29 7a 4c 65 66  p, (UChar *)zLef
3707a 74 2c 20 6e 4c 65 66 74 2f 32 2c 20 28 55 43 68  t, nLeft/2, (UCh
3707b 61 72 20 2a 29 7a 52 69 67 68 74 2c 20 6e 52 69  ar *)zRight, nRi
3707c 67 68 74 2f 32 29 3b 0a 20 20 73 77 69 74 63 68  ght/2);.  switch
3707d 28 20 72 65 73 20 29 7b 0a 20 20 20 20 63 61 73  ( res ){.    cas
3707e 65 20 55 43 4f 4c 5f 4c 45 53 53 3a 20 20 20 20  e UCOL_LESS:    
3707f 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 63  return -1;.    c
37080 61 73 65 20 55 43 4f 4c 5f 47 52 45 41 54 45 52  ase UCOL_GREATER
37081 3a 20 72 65 74 75 72 6e 20 2b 31 3b 0a 20 20 20  : return +1;.   
37082 20 63 61 73 65 20 55 43 4f 4c 5f 45 51 55 41 4c   case UCOL_EQUAL
37083 3a 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  :   return 0;.  
37084 7d 0a 20 20 61 73 73 65 72 74 28 21 22 55 6e 65  }.  assert(!"Une
37085 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 20 76  xpected return v
37086 61 6c 75 65 20 66 72 6f 6d 20 75 63 6f 6c 5f 73  alue from ucol_s
37087 74 72 63 6f 6c 6c 28 29 22 29 3b 0a 20 20 72 65  trcoll()");.  re
37088 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
37089 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
3708a 6f 66 20 74 68 65 20 73 63 61 6c 61 72 20 66 75  of the scalar fu
3708b 6e 63 74 69 6f 6e 20 69 63 75 5f 6c 6f 61 64 5f  nction icu_load_
3708c 63 6f 6c 6c 61 74 69 6f 6e 28 29 2e 0a 2a 2a 0a  collation()..**.
3708d 2a 2a 20 54 68 69 73 20 73 63 61 6c 61 72 20 66  ** This scalar f
3708e 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
3708f 74 6f 20 61 64 64 20 49 43 55 20 63 6f 6c 6c 61  to add ICU colla
37090 74 69 6f 6e 20 62 61 73 65 64 20 63 6f 6c 6c 61  tion based colla
37091 74 69 6f 6e 20 0a 2a 2a 20 74 79 70 65 73 20 74  tion .** types t
37092 6f 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61  o an SQLite data
37093 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
37094 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20   It is intended 
37095 74 6f 20 62 65 20 63 61 6c 6c 65 64 0a 2a 2a 20  to be called.** 
37096 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
37097 2a 20 20 20 20 20 53 45 4c 45 43 54 20 69 63 75  *     SELECT icu
37098 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28  _load_collation(
37099 3c 6c 6f 63 61 6c 65 3e 2c 20 3c 63 6f 6c 6c 61  <locale>, <colla
3709a 74 69 6f 6e 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a 0a  tion-name>);.**.
3709b 2a 2a 20 57 68 65 72 65 20 3c 6c 6f 63 61 6c 65  ** Where <locale
3709c 3e 20 69 73 20 61 20 73 74 72 69 6e 67 20 63 6f  > is a string co
3709d 6e 74 61 69 6e 69 6e 67 20 61 6e 20 49 43 55 20  ntaining an ICU 
3709e 6c 6f 63 61 6c 65 20 69 64 65 6e 74 69 66 69 65  locale identifie
3709f 72 20 28 69 2e 65 2e 0a 2a 2a 20 22 65 6e 5f 41  r (i.e..** "en_A
370a0 55 22 2c 20 22 74 72 5f 54 52 22 20 65 74 63 2e  U", "tr_TR" etc.
370a1 29 20 61 6e 64 20 3c 63 6f 6c 6c 61 74 69 6f 6e  ) and <collation
370a2 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65 20 6e 61  -name> is the na
370a3 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c  me of the.** col
370a4 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
370a5 74 6f 20 63 72 65 61 74 65 2e 0a 2a 2f 0a 73 74  to create..*/.st
370a6 61 74 69 63 20 76 6f 69 64 20 69 63 75 4c 6f 61  atic void icuLoa
370a7 64 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71  dCollation(.  sq
370a8 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
370a9 2c 20 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 0a  , .  int nArg, .
370aa 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
370ab 2a 2a 61 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c  **apArg.){.  sql
370ac 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69  ite3 *db = (sqli
370ad 74 65 33 20 2a 29 73 71 6c 69 74 65 33 5f 75 73  te3 *)sqlite3_us
370ae 65 72 5f 64 61 74 61 28 70 29 3b 0a 20 20 55 45  er_data(p);.  UE
370af 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20  rrorCode status 
370b0 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a  = U_ZERO_ERROR;.
370b1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c    const char *zL
370b2 6f 63 61 6c 65 3b 20 20 20 20 20 20 2f 2a 20 4c  ocale;      /* L
370b3 6f 63 61 6c 65 20 69 64 65 6e 74 69 66 69 65 72  ocale identifier
370b4 20 2d 20 28 65 67 2e 20 22 6a 70 5f 4a 50 22 29   - (eg. "jp_JP")
370b5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
370b6 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
370b7 2f 2a 20 53 51 4c 20 43 6f 6c 6c 61 74 69 6f 6e  /* SQL Collation
370b8 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 28   sequence name (
370b9 65 67 2e 20 22 6a 61 70 61 6e 65 73 65 22 29 20  eg. "japanese") 
370ba 2a 2f 0a 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a  */.  UCollator *
370bb 70 55 43 6f 6c 6c 61 74 6f 72 3b 20 20 20 20 2f  pUCollator;    /
370bc 2a 20 49 43 55 20 6c 69 62 72 61 72 79 20 63 6f  * ICU library co
370bd 6c 6c 61 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a  llation object *
370be 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
370bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
370c0 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   Return code fro
370c1 6d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  m sqlite3_create
370c2 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 78 28 29 20 2a  _collation_x() *
370c3 2f 0a 0a 20 20 61 73 73 65 72 74 28 6e 41 72 67  /..  assert(nArg
370c4 3d 3d 32 29 3b 0a 20 20 7a 4c 6f 63 61 6c 65 20  ==2);.  zLocale 
370c5 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
370c6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
370c7 78 74 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20  xt(apArg[0]);.  
370c8 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63  zName = (const c
370c9 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
370ca 6c 75 65 5f 74 65 78 74 28 61 70 41 72 67 5b 31  lue_text(apArg[1
370cb 5d 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4c 6f 63  ]);..  if( !zLoc
370cc 61 6c 65 20 7c 7c 20 21 7a 4e 61 6d 65 20 29 7b  ale || !zName ){
370cd 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
370ce 0a 0a 20 20 70 55 43 6f 6c 6c 61 74 6f 72 20 3d  ..  pUCollator =
370cf 20 75 63 6f 6c 5f 6f 70 65 6e 28 7a 4c 6f 63 61   ucol_open(zLoca
370d0 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20  le, &status);.  
370d1 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73  if( !U_SUCCESS(s
370d2 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63  tatus) ){.    ic
370d3 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70  uFunctionError(p
370d4 2c 20 22 75 63 6f 6c 5f 6f 70 65 6e 22 2c 20 73  , "ucol_open", s
370d5 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75  tatus);.    retu
370d6 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rn;.  }.  assert
370d7 28 70 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c  (p);..  rc = sql
370d8 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
370d9 61 74 69 6f 6e 5f 76 32 28 64 62 2c 20 7a 4e 61  ation_v2(db, zNa
370da 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  me, SQLITE_UTF16
370db 2c 20 28 76 6f 69 64 20 2a 29 70 55 43 6f 6c 6c  , (void *)pUColl
370dc 61 74 6f 72 2c 20 0a 20 20 20 20 20 20 69 63 75  ator, .      icu
370dd 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c 2c 20 69  CollationColl, i
370de 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65 6c 0a 20  cuCollationDel. 
370df 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
370e0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75  LITE_OK ){.    u
370e1 63 6f 6c 5f 63 6c 6f 73 65 28 70 55 43 6f 6c 6c  col_close(pUColl
370e2 61 74 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  ator);.    sqlit
370e3 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
370e4 70 2c 20 22 45 72 72 6f 72 20 72 65 67 69 73 74  p, "Error regist
370e5 65 72 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20  ering collation 
370e6 66 75 6e 63 74 69 6f 6e 22 2c 20 2d 31 29 3b 0a  function", -1);.
370e7 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67    }.}../*.** Reg
370e8 69 73 74 65 72 20 74 68 65 20 49 43 55 20 65 78  ister the ICU ex
370e9 74 65 6e 73 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  tension function
370ea 73 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20  s with database 
370eb 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  db..*/.SQLITE_PR
370ec 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
370ed 33 49 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33  3IcuInit(sqlite3
370ee 20 2a 64 62 29 7b 0a 20 20 73 74 72 75 63 74 20   *db){.  struct 
370ef 49 63 75 53 63 61 6c 61 72 20 7b 0a 20 20 20 20  IcuScalar {.    
370f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
370f1 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
370f2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e            /* Fun
370f3 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
370f4 20 20 69 6e 74 20 6e 41 72 67 3b 20 20 20 20 20    int nArg;     
370f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370f6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
370f7 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
370f8 74 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 6e  ts */.    int en
370f9 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
370fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370fb 20 20 20 20 2f 2a 20 4f 70 74 69 6d 61 6c 20 74      /* Optimal t
370fc 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a  ext encoding */.
370fd 20 20 20 20 76 6f 69 64 20 2a 70 43 6f 6e 74 65      void *pConte
370fe 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
370ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37100 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
37101 74 61 28 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ta() context */.
37102 20 20 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63      void (*xFunc
37103 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
37104 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
37105 61 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20 73 63 61  alue**);.  } sca
37106 6c 61 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b  lars[] = {.    {
37107 22 72 65 67 65 78 70 22 2c 2d 31 2c 20 53 51 4c  "regexp",-1, SQL
37108 49 54 45 5f 41 4e 59 2c 20 20 20 20 20 20 20 20  ITE_ANY,        
37109 20 20 30 2c 20 69 63 75 52 65 67 65 78 70 46 75    0, icuRegexpFu
3710a 6e 63 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f 77 65  nc},..    {"lowe
3710b 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55  r",  1, SQLITE_U
3710c 54 46 31 36 2c 20 20 20 20 20 20 20 20 30 2c 20  TF16,        0, 
3710d 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
3710e 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 32      {"lower",  2
3710f 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20  , SQLITE_UTF16, 
37110 20 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73         0, icuCas
37111 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22  eFunc16},.    {"
37112 75 70 70 65 72 22 2c 20 20 31 2c 20 53 51 4c 49  upper",  1, SQLI
37113 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64 2a  TE_UTF16, (void*
37114 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31  )1, icuCaseFunc1
37115 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22  6},.    {"upper"
37116 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46  ,  2, SQLITE_UTF
37117 31 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63  16, (void*)1, ic
37118 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 0a 20  uCaseFunc16},.. 
37119 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31 2c     {"lower",  1,
3711a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
3711b 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65        0, icuCase
3711c 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 6c  Func16},.    {"l
3711d 6f 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54  ower",  2, SQLIT
3711e 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20  E_UTF8,         
3711f 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
37120 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c  },.    {"upper",
37121 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38    1, SQLITE_UTF8
37122 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75  ,  (void*)1, icu
37123 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
37124 20 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20 53   {"upper",  2, S
37125 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f  QLITE_UTF8,  (vo
37126 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
37127 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c 69  nc16},..    {"li
37128 6b 65 22 2c 20 20 20 32 2c 20 53 51 4c 49 54 45  ke",   2, SQLITE
37129 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30  _UTF8,         0
3712a 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d 2c 0a  , icuLikeFunc},.
3712b 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20 33      {"like",   3
3712c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
3712d 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c 69 6b         0, icuLik
3712e 65 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b 22 69  eFunc},..    {"i
3712f 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f  cu_load_collatio
37130 6e 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55  n",  2, SQLITE_U
37131 54 46 38 2c 20 28 76 6f 69 64 2a 29 64 62 2c 20  TF8, (void*)db, 
37132 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e  icuLoadCollation
37133 7d 2c 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 72  },.  };..  int r
37134 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
37135 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 28 69   int i;..  for(i
37136 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
37137 4b 20 26 26 20 69 3c 28 73 69 7a 65 6f 66 28 73  K && i<(sizeof(s
37138 63 61 6c 61 72 73 29 2f 73 69 7a 65 6f 66 28 73  calars)/sizeof(s
37139 74 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 29  truct IcuScalar)
3713a 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72  ); i++){.    str
3713b 75 63 74 20 49 63 75 53 63 61 6c 61 72 20 2a 70  uct IcuScalar *p
3713c 20 3d 20 26 73 63 61 6c 61 72 73 5b 69 5d 3b 0a   = &scalars[i];.
3713d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3713e 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
3713f 28 0a 20 20 20 20 20 20 20 20 64 62 2c 20 70 2d  (.        db, p-
37140 3e 7a 4e 61 6d 65 2c 20 70 2d 3e 6e 41 72 67 2c  >zName, p->nArg,
37141 20 70 2d 3e 65 6e 63 2c 20 70 2d 3e 70 43 6f 6e   p->enc, p->pCon
37142 74 65 78 74 2c 20 70 2d 3e 78 46 75 6e 63 2c 20  text, p->xFunc, 
37143 30 2c 20 30 0a 20 20 20 20 29 3b 0a 20 20 7d 0a  0, 0.    );.  }.
37144 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
37145 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52  .#if !SQLITE_COR
37146 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  E.SQLITE_API int
37147 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69   sqlite3_extensi
37148 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74  on_init(.  sqlit
37149 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20  e3 *db, .  char 
3714a 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f  **pzErrMsg,.  co
3714b 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
3714c 72 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29  routines *pApi.)
3714d 7b 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  {.  SQLITE_EXTEN
3714e 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29  SION_INIT2(pApi)
3714f 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
37150 33 49 63 75 49 6e 69 74 28 64 62 29 3b 0a 7d 0a  3IcuInit(db);.}.
37151 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a  #endif..#endif..
37152 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
37153 45 6e 64 20 6f 66 20 69 63 75 2e 63 20 2a 2a 2a  End of icu.c ***
37154 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37155 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37156 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
37157 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
37158 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
37159 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  icu.c **********
3715a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3715b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
3715c 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a 75 6e 65 20  /*.** 2007 June 
3715d 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  22.**.** The aut
3715e 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
3715f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
37160 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
37161 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
37162 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
37163 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
37164 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
37165 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
37166 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
37167 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
37168 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
37169 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
3716a 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
3716b 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
3716c 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
3716d 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
3716e 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
3716f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37171 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37172 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37173 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69  *.** This file i
37174 6d 70 6c 65 6d 65 6e 74 73 20 61 20 74 6f 6b 65  mplements a toke
37175 6e 69 7a 65 72 20 66 6f 72 20 66 74 73 33 20 62  nizer for fts3 b
37176 61 73 65 64 20 6f 6e 20 74 68 65 20 49 43 55 20  ased on the ICU 
37177 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20  library..** .** 
37178 24 49 64 3a 20 66 74 73 33 5f 69 63 75 2e 63 2c  $Id: fts3_icu.c,
37179 76 20 31 2e 33 20 32 30 30 38 2f 30 39 2f 30 31  v 1.3 2008/09/01
3717a 20 31 38 3a 33 34 3a 32 30 20 64 61 6e 69 65 6c   18:34:20 daniel
3717b 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
3717c 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
3717d 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
3717e 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
3717f 4c 45 5f 46 54 53 33 29 0a 23 69 66 64 65 66 20  LE_FTS3).#ifdef 
37180 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43  SQLITE_ENABLE_IC
37181 55 0a 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e  U...#include <un
37182 69 63 6f 64 65 2f 75 62 72 6b 2e 68 3e 0a 23 69  icode/ubrk.h>.#i
37183 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f  nclude <unicode/
37184 75 74 66 31 36 2e 68 3e 0a 0a 74 79 70 65 64 65  utf16.h>..typede
37185 66 20 73 74 72 75 63 74 20 49 63 75 54 6f 6b 65  f struct IcuToke
37186 6e 69 7a 65 72 20 49 63 75 54 6f 6b 65 6e 69 7a  nizer IcuTokeniz
37187 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  er;.typedef stru
37188 63 74 20 49 63 75 43 75 72 73 6f 72 20 49 63 75  ct IcuCursor Icu
37189 43 75 72 73 6f 72 3b 0a 0a 73 74 72 75 63 74 20  Cursor;..struct 
3718a 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20  IcuTokenizer {. 
3718b 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3718c 65 72 20 62 61 73 65 3b 0a 20 20 63 68 61 72 20  er base;.  char 
3718d 2a 7a 4c 6f 63 61 6c 65 3b 0a 7d 3b 0a 0a 73 74  *zLocale;.};..st
3718e 72 75 63 74 20 49 63 75 43 75 72 73 6f 72 20 7b  ruct IcuCursor {
3718f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
37190 69 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65  izer_cursor base
37191 3b 0a 0a 20 20 55 42 72 65 61 6b 49 74 65 72 61  ;..  UBreakItera
37192 74 6f 72 20 2a 70 49 74 65 72 3b 20 20 20 20 20  tor *pIter;     
37193 20 2f 2a 20 49 43 55 20 62 72 65 61 6b 2d 69 74   /* ICU break-it
37194 65 72 61 74 6f 72 20 6f 62 6a 65 63 74 20 2a 2f  erator object */
37195 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 20 20 20  .  int nChar;   
37196 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37197 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 43 68 61  * Number of UCha
37198 72 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 49  r elements in pI
37199 6e 70 75 74 20 2a 2f 0a 20 20 55 43 68 61 72 20  nput */.  UChar 
3719a 2a 61 43 68 61 72 3b 20 20 20 20 20 20 20 20 20  *aChar;         
3719b 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
3719c 20 69 6e 70 75 74 20 75 73 69 6e 67 20 75 74 66   input using utf
3719d 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a  -16 encoding */.
3719e 20 20 69 6e 74 20 2a 61 4f 66 66 73 65 74 3b 20    int *aOffset; 
3719f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
371a0 20 4f 66 66 73 65 74 73 20 6f 66 20 65 61 63 68   Offsets of each
371a1 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 75 74   character in ut
371a2 66 2d 38 20 69 6e 70 75 74 20 2a 2f 0a 0a 20 20  f-8 input */..  
371a3 69 6e 74 20 6e 42 75 66 66 65 72 3b 0a 20 20 63  int nBuffer;.  c
371a4 68 61 72 20 2a 7a 42 75 66 66 65 72 3b 0a 0a 20  har *zBuffer;.. 
371a5 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 0a 7d 3b 0a   int iToken;.};.
371a6 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
371a7 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e  new tokenizer in
371a8 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69  stance..*/.stati
371a9 63 20 69 6e 74 20 69 63 75 43 72 65 61 74 65 28  c int icuCreate(
371aa 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20  .  int argc,    
371ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371ac 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
371ad 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
371ae 61 72 67 76 5b 5d 20 2a 2f 0a 20 20 63 6f 6e 73  argv[] */.  cons
371af 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a  t char * const *
371b0 61 72 67 76 2c 20 20 20 20 20 20 20 20 20 20 20  argv,           
371b1 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 63 72   /* Tokenizer cr
371b2 65 61 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73  eation arguments
371b3 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f   */.  sqlite3_to
371b4 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65  kenizer **ppToke
371b5 6e 69 7a 65 72 20 20 20 20 20 20 2f 2a 20 4f 55  nizer      /* OU
371b6 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65 6e  T: Created token
371b7 69 7a 65 72 20 2a 2f 0a 29 7b 0a 20 20 49 63 75  izer */.){.  Icu
371b8 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 3b 0a 20 20  Tokenizer *p;.  
371b9 69 6e 74 20 6e 20 3d 20 30 3b 0a 0a 20 20 69 66  int n = 0;..  if
371ba 28 20 61 72 67 63 3e 30 20 29 7b 0a 20 20 20 20  ( argc>0 ){.    
371bb 6e 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b  n = strlen(argv[
371bc 30 5d 29 2b 31 3b 0a 20 20 7d 0a 20 20 70 20 3d  0])+1;.  }.  p =
371bd 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a   (IcuTokenizer *
371be 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
371bf 73 69 7a 65 6f 66 28 49 63 75 54 6f 6b 65 6e 69  sizeof(IcuTokeni
371c0 7a 65 72 29 2b 6e 29 3b 0a 20 20 69 66 28 20 21  zer)+n);.  if( !
371c1 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  p ){.    return 
371c2 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
371c3 7d 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  }.  memset(p, 0,
371c4 20 73 69 7a 65 6f 66 28 49 63 75 54 6f 6b 65 6e   sizeof(IcuToken
371c5 69 7a 65 72 29 29 3b 0a 0a 20 20 69 66 28 20 6e  izer));..  if( n
371c6 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 4c 6f 63 61   ){.    p->zLoca
371c7 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 5b  le = (char *)&p[
371c8 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  1];.    memcpy(p
371c9 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 61 72 67 76 5b  ->zLocale, argv[
371ca 30 5d 2c 20 6e 29 3b 0a 20 20 7d 0a 0a 20 20 2a  0], n);.  }..  *
371cb 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 28 73  ppTokenizer = (s
371cc 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
371cd 20 2a 29 70 3b 0a 0a 20 20 72 65 74 75 72 6e 20   *)p;..  return 
371ce 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
371cf 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f  .** Destroy a to
371d0 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69  kenizer.*/.stati
371d1 63 20 69 6e 74 20 69 63 75 44 65 73 74 72 6f 79  c int icuDestroy
371d2 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
371d3 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b  er *pTokenizer){
371d4 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  .  IcuTokenizer 
371d5 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a  *p = (IcuTokeniz
371d6 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65 72 3b  er *)pTokenizer;
371d7 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
371d8 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  p);.  return SQL
371d9 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
371da 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67 69   Prepare to begi
371db 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70  n tokenizing a p
371dc 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e 67  articular string
371dd 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20  .  The input.** 
371de 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b  string to be tok
371df 65 6e 69 7a 65 64 20 69 73 20 70 49 6e 70 75 74  enized is pInput
371e0 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e 20 20  [0..nBytes-1].  
371e1 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64  A cursor.** used
371e2 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c   to incrementall
371e3 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20  y tokenize this 
371e4 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e  string is return
371e5 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72  ed in .** *ppCur
371e6 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  sor..*/.static i
371e7 6e 74 20 69 63 75 4f 70 65 6e 28 0a 20 20 73 71  nt icuOpen(.  sq
371e8 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
371e9 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20  *pTokenizer,    
371ea 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65       /* The toke
371eb 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  nizer */.  const
371ec 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 20   char *zInput,  
371ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371ee 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e    /* Input strin
371ef 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75  g */.  int nInpu
371f0 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
371f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
371f2 20 4c 65 6e 67 74 68 20 6f 66 20 7a 49 6e 70 75   Length of zInpu
371f3 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  t in bytes */.  
371f4 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
371f5 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72  r_cursor **ppCur
371f6 73 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54  sor    /* OUT: T
371f7 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
371f8 6f 72 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 54 6f  or */.){.  IcuTo
371f9 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28 49 63  kenizer *p = (Ic
371fa 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54 6f  uTokenizer *)pTo
371fb 6b 65 6e 69 7a 65 72 3b 0a 20 20 49 63 75 43 75  kenizer;.  IcuCu
371fc 72 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20 20 63  rsor *pCsr;..  c
371fd 6f 6e 73 74 20 69 6e 74 33 32 5f 74 20 6f 70 74  onst int32_t opt
371fe 20 3d 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f 44   = U_FOLD_CASE_D
371ff 45 46 41 55 4c 54 3b 0a 20 20 55 45 72 72 6f 72  EFAULT;.  UError
37200 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f  Code status = U_
37201 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 69 6e  ZERO_ERROR;.  in
37202 74 20 6e 43 68 61 72 3b 0a 0a 20 20 55 43 68 61  t nChar;..  UCha
37203 72 33 32 20 63 3b 0a 20 20 69 6e 74 20 69 49 6e  r32 c;.  int iIn
37204 70 75 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  put = 0;.  int i
37205 4f 75 74 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43  Out = 0;..  *ppC
37206 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20 69 66  ursor = 0;..  if
37207 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a 20 20  ( nInput<0 ){.  
37208 20 20 6e 49 6e 70 75 74 20 3d 20 73 74 72 6c 65    nInput = strle
37209 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 0a 20  n(zInput);.  }. 
3720a 20 6e 43 68 61 72 20 3d 20 6e 49 6e 70 75 74 2b   nChar = nInput+
3720b 31 3b 0a 20 20 70 43 73 72 20 3d 20 28 49 63 75  1;.  pCsr = (Icu
3720c 43 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33  Cursor *)sqlite3
3720d 5f 6d 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 73  _malloc(.      s
3720e 69 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72 29  izeof(IcuCursor)
3720f 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   +              
37210 20 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 20 2a    /* IcuCursor *
37211 2f 0a 20 20 20 20 20 20 6e 43 68 61 72 20 2a 20  /.      nChar * 
37212 73 69 7a 65 6f 66 28 55 43 68 61 72 29 20 2b 20  sizeof(UChar) + 
37213 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 63             /* Ic
37214 75 43 75 72 73 6f 72 2e 61 43 68 61 72 5b 5d 20  uCursor.aChar[] 
37215 2a 2f 0a 20 20 20 20 20 20 28 6e 43 68 61 72 2b  */.      (nChar+
37216 31 29 20 2a 20 73 69 7a 65 6f 66 28 69 6e 74 29  1) * sizeof(int)
37217 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
37218 63 75 43 75 72 73 6f 72 2e 61 4f 66 66 73 65 74  cuCursor.aOffset
37219 5b 5d 20 2a 2f 0a 20 20 29 3b 0a 20 20 69 66 28  [] */.  );.  if(
3721a 20 21 70 43 73 72 20 29 7b 0a 20 20 20 20 72 65   !pCsr ){.    re
3721b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
3721c 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  M;.  }.  memset(
3721d 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pCsr, 0, sizeof(
3721e 49 63 75 43 75 72 73 6f 72 29 29 3b 0a 20 20 70  IcuCursor));.  p
3721f 43 73 72 2d 3e 61 43 68 61 72 20 3d 20 28 55 43  Csr->aChar = (UC
37220 68 61 72 20 2a 29 26 70 43 73 72 5b 31 5d 3b 0a  har *)&pCsr[1];.
37221 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 20    pCsr->aOffset 
37222 3d 20 28 69 6e 74 20 2a 29 26 70 43 73 72 2d 3e  = (int *)&pCsr->
37223 61 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 0a 20  aChar[nChar];.. 
37224 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69   pCsr->aOffset[i
37225 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74 3b 0a 20  Out] = iInput;. 
37226 20 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75 74 2c   U8_NEXT(zInput,
37227 20 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c   iInput, nInput,
37228 20 63 29 3b 20 0a 20 20 77 68 69 6c 65 28 20 63   c); .  while( c
37229 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73  >0 ){.    int is
3722a 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 63  Error = 0;.    c
3722b 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 63 2c   = u_foldCase(c,
3722c 20 6f 70 74 29 3b 0a 20 20 20 20 55 31 36 5f 41   opt);.    U16_A
3722d 50 50 45 4e 44 28 70 43 73 72 2d 3e 61 43 68 61  PPEND(pCsr->aCha
3722e 72 2c 20 69 4f 75 74 2c 20 6e 43 68 61 72 2c 20  r, iOut, nChar, 
3722f 63 2c 20 69 73 45 72 72 6f 72 29 3b 0a 20 20 20  c, isError);.   
37230 20 69 66 28 20 69 73 45 72 72 6f 72 20 29 7b 0a   if( isError ){.
37231 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
37232 65 65 28 70 43 73 72 29 3b 0a 20 20 20 20 20 20  ee(pCsr);.      
37233 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
37234 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ROR;.    }.    p
37235 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 4f 75  Csr->aOffset[iOu
37236 74 5d 20 3d 20 69 49 6e 70 75 74 3b 0a 0a 20 20  t] = iInput;..  
37237 20 20 69 66 28 20 69 49 6e 70 75 74 3c 6e 49 6e    if( iInput<nIn
37238 70 75 74 20 29 7b 0a 20 20 20 20 20 20 55 38 5f  put ){.      U8_
37239 4e 45 58 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e  NEXT(zInput, iIn
3723a 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b  put, nInput, c);
3723b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3723c 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20    c = 0;.    }. 
3723d 20 7d 0a 0a 20 20 70 43 73 72 2d 3e 70 49 74 65   }..  pCsr->pIte
3723e 72 20 3d 20 75 62 72 6b 5f 6f 70 65 6e 28 55 42  r = ubrk_open(UB
3723f 52 4b 5f 57 4f 52 44 2c 20 70 2d 3e 7a 4c 6f 63  RK_WORD, p->zLoc
37240 61 6c 65 2c 20 70 43 73 72 2d 3e 61 43 68 61 72  ale, pCsr->aChar
37241 2c 20 69 4f 75 74 2c 20 26 73 74 61 74 75 73 29  , iOut, &status)
37242 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45  ;.  if( !U_SUCCE
37243 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20  SS(status) ){.  
37244 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
37245 43 73 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Csr);.    return
37246 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
37247 20 7d 0a 20 20 70 43 73 72 2d 3e 6e 43 68 61 72   }.  pCsr->nChar
37248 20 3d 20 69 4f 75 74 3b 0a 0a 20 20 75 62 72 6b   = iOut;..  ubrk
37249 5f 66 69 72 73 74 28 70 43 73 72 2d 3e 70 49 74  _first(pCsr->pIt
3724a 65 72 29 3b 0a 20 20 2a 70 70 43 75 72 73 6f 72  er);.  *ppCursor
3724b 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65   = (sqlite3_toke
3724c 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 70  nizer_cursor *)p
3724d 43 73 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Csr;.  return SQ
3724e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
3724f 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69  * Close a tokeni
37250 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72  zation cursor pr
37251 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20  eviously opened 
37252 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 69 63 75  by a call to icu
37253 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Open()..*/.stati
37254 63 20 69 6e 74 20 69 63 75 43 6c 6f 73 65 28 73  c int icuClose(s
37255 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
37256 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
37257 29 7b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a  ){.  IcuCursor *
37258 70 43 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f  pCsr = (IcuCurso
37259 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a 20 20 75  r *)pCursor;.  u
3725a 62 72 6b 5f 63 6c 6f 73 65 28 70 43 73 72 2d 3e  brk_close(pCsr->
3725b 70 49 74 65 72 29 3b 0a 20 20 73 71 6c 69 74 65  pIter);.  sqlite
3725c 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 7a 42 75  3_free(pCsr->zBu
3725d 66 66 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ffer);.  sqlite3
3725e 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72  _free(pCsr);.  r
3725f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
37260 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  .}../*.** Extrac
37261 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  t the next token
37262 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61   from a tokeniza
37263 74 69 6f 6e 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a  tion cursor..*/.
37264 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 4e 65  static int icuNe
37265 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  xt(.  sqlite3_to
37266 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
37267 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72  pCursor,  /* Cur
37268 73 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20  sor returned by 
37269 73 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20  simpleOpen */.  
3726a 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54  const char **ppT
3726b 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  oken,           
3726c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 70 54      /* OUT: *ppT
3726d 6f 6b 65 6e 20 69 73 20 74 68 65 20 74 6f 6b 65  oken is the toke
3726e 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  n text */.  int 
3726f 2a 70 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20  *pnBytes,       
37270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37271 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f  /* OUT: Number o
37272 66 20 62 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e  f bytes in token
37273 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74 61   */.  int *piSta
37274 72 74 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20  rtOffset,       
37275 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
37276 3a 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65  : Starting offse
37277 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  t of token */.  
37278 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74  int *piEndOffset
37279 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3727a 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69      /* OUT: Endi
3727b 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  ng offset of tok
3727c 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50  en */.  int *piP
3727d 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20  osition         
3727e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
3727f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74  UT: Position int
37280 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  eger of token */
37281 0a 29 7b 0a 20 20 49 63 75 43 75 72 73 6f 72 20  .){.  IcuCursor 
37282 2a 70 43 73 72 20 3d 20 28 49 63 75 43 75 72 73  *pCsr = (IcuCurs
37283 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a 0a 20  or *)pCursor;.. 
37284 20 69 6e 74 20 69 53 74 61 72 74 20 3d 20 30 3b   int iStart = 0;
37285 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 30 3b  .  int iEnd = 0;
37286 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 30  .  int nByte = 0
37287 3b 0a 0a 20 20 77 68 69 6c 65 28 20 69 53 74 61  ;..  while( iSta
37288 72 74 3d 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20  rt==iEnd ){.    
37289 55 43 68 61 72 33 32 20 63 3b 0a 0a 20 20 20 20  UChar32 c;..    
3728a 69 53 74 61 72 74 20 3d 20 75 62 72 6b 5f 63 75  iStart = ubrk_cu
3728b 72 72 65 6e 74 28 70 43 73 72 2d 3e 70 49 74 65  rrent(pCsr->pIte
3728c 72 29 3b 0a 20 20 20 20 69 45 6e 64 20 3d 20 75  r);.    iEnd = u
3728d 62 72 6b 5f 6e 65 78 74 28 70 43 73 72 2d 3e 70  brk_next(pCsr->p
3728e 49 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 69  Iter);.    if( i
3728f 45 6e 64 3d 3d 55 42 52 4b 5f 44 4f 4e 45 20 29  End==UBRK_DONE )
37290 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
37291 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
37292 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53  }..    while( iS
37293 74 61 72 74 3c 69 45 6e 64 20 29 7b 0a 20 20 20  tart<iEnd ){.   
37294 20 20 20 69 6e 74 20 69 57 68 69 74 65 20 3d 20     int iWhite = 
37295 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20 55 38  iStart;.      U8
37296 5f 4e 45 58 54 28 70 43 73 72 2d 3e 61 43 68 61  _NEXT(pCsr->aCha
37297 72 2c 20 69 57 68 69 74 65 2c 20 70 43 73 72 2d  r, iWhite, pCsr-
37298 3e 6e 43 68 61 72 2c 20 63 29 3b 0a 20 20 20 20  >nChar, c);.    
37299 20 20 69 66 28 20 75 5f 69 73 73 70 61 63 65 28    if( u_isspace(
3729a 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 53  c) ){.        iS
3729b 74 61 72 74 20 3d 20 69 57 68 69 74 65 3b 0a 20  tart = iWhite;. 
3729c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3729d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3729e 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73   }.    }.    ass
3729f 65 72 74 28 69 53 74 61 72 74 3c 3d 69 45 6e 64  ert(iStart<=iEnd
372a0 29 3b 0a 20 20 7d 0a 0a 20 20 64 6f 20 7b 0a 20  );.  }..  do {. 
372a1 20 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74     UErrorCode st
372a2 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52  atus = U_ZERO_ER
372a3 52 4f 52 3b 0a 20 20 20 20 69 66 28 20 6e 42 79  ROR;.    if( nBy
372a4 74 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72  te ){.      char
372a5 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   *zNew = sqlite3
372a6 5f 72 65 61 6c 6c 6f 63 28 70 43 73 72 2d 3e 7a  _realloc(pCsr->z
372a7 42 75 66 66 65 72 2c 20 6e 42 79 74 65 29 3b 0a  Buffer, nByte);.
372a8 20 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77 20        if( !zNew 
372a9 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
372aa 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
372ab 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
372ac 73 72 2d 3e 7a 42 75 66 66 65 72 20 3d 20 7a 4e  sr->zBuffer = zN
372ad 65 77 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e  ew;.      pCsr->
372ae 6e 42 75 66 66 65 72 20 3d 20 6e 42 79 74 65 3b  nBuffer = nByte;
372af 0a 20 20 20 20 7d 0a 0a 20 20 20 20 75 5f 73 74  .    }..    u_st
372b0 72 54 6f 55 54 46 38 28 0a 20 20 20 20 20 20 20  rToUTF8(.       
372b1 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 2c 20   pCsr->zBuffer, 
372b2 70 43 73 72 2d 3e 6e 42 75 66 66 65 72 2c 20 26  pCsr->nBuffer, &
372b3 6e 42 79 74 65 2c 20 20 20 20 2f 2a 20 4f 75 74  nByte,    /* Out
372b4 70 75 74 20 76 61 72 73 20 2a 2f 0a 20 20 20 20  put vars */.    
372b5 20 20 20 20 26 70 43 73 72 2d 3e 61 43 68 61 72      &pCsr->aChar
372b6 5b 69 53 74 61 72 74 5d 2c 20 69 45 6e 64 2d 69  [iStart], iEnd-i
372b7 53 74 61 72 74 2c 20 20 20 20 20 20 20 2f 2a 20  Start,       /* 
372b8 49 6e 70 75 74 20 76 61 72 73 20 2a 2f 0a 20 20  Input vars */.  
372b9 20 20 20 20 20 20 26 73 74 61 74 75 73 20 20 20        &status   
372ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
372bc 2a 20 4f 75 74 70 75 74 20 73 75 63 63 65 73 73  * Output success
372bd 2f 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 20 20  /failure */.    
372be 29 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 42  );.  } while( nB
372bf 79 74 65 3e 70 43 73 72 2d 3e 6e 42 75 66 66 65  yte>pCsr->nBuffe
372c0 72 20 29 3b 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  r );..  *ppToken
372c1 20 3d 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72   = pCsr->zBuffer
372c2 3b 0a 20 20 2a 70 6e 42 79 74 65 73 20 3d 20 6e  ;.  *pnBytes = n
372c3 42 79 74 65 3b 0a 20 20 2a 70 69 53 74 61 72 74  Byte;.  *piStart
372c4 4f 66 66 73 65 74 20 3d 20 70 43 73 72 2d 3e 61  Offset = pCsr->a
372c5 4f 66 66 73 65 74 5b 69 53 74 61 72 74 5d 3b 0a  Offset[iStart];.
372c6 20 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d    *piEndOffset =
372c7 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69   pCsr->aOffset[i
372c8 45 6e 64 5d 3b 0a 20 20 2a 70 69 50 6f 73 69 74  End];.  *piPosit
372c9 69 6f 6e 20 3d 20 70 43 73 72 2d 3e 69 54 6f 6b  ion = pCsr->iTok
372ca 65 6e 2b 2b 3b 0a 0a 20 20 72 65 74 75 72 6e 20  en++;..  return 
372cb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
372cc 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72  .** The set of r
372cd 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70  outines that imp
372ce 6c 65 6d 65 6e 74 20 74 68 65 20 73 69 6d 70 6c  lement the simpl
372cf 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73  e tokenizer.*/.s
372d0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
372d1 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
372d2 64 75 6c 65 20 69 63 75 54 6f 6b 65 6e 69 7a 65  dule icuTokenize
372d3 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c  rModule = {.  0,
372d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372d5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56             /* iV
372d6 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 69 63 75 43  ersion */.  icuC
372d7 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  reate,          
372d8 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65           /* xCre
372d9 61 74 65 20 20 2a 2f 0a 20 20 69 63 75 44 65 73  ate  */.  icuDes
372da 74 72 6f 79 2c 20 20 20 20 20 20 20 20 20 20 20  troy,           
372db 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74         /* xCreat
372dc 65 20 20 2a 2f 0a 20 20 69 63 75 4f 70 65 6e 2c  e  */.  icuOpen,
372dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372de 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20       /* xOpen   
372df 20 2a 2f 0a 20 20 69 63 75 43 6c 6f 73 65 2c 20   */.  icuClose, 
372e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372e1 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 20 20 2a     /* xClose   *
372e2 2f 0a 20 20 69 63 75 4e 65 78 74 2c 20 20 20 20  /.  icuNext,    
372e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372e4 20 2f 2a 20 78 4e 65 78 74 20 20 20 20 2a 2f 0a   /* xNext    */.
372e5 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70  };../*.** Set *p
372e6 70 4d 6f 64 75 6c 65 20 74 6f 20 70 6f 69 6e 74  pModule to point
372e7 20 61 74 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   at the implemen
372e8 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 43  tation of the IC
372e9 55 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a  U tokenizer..*/.
372ea 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
372eb 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 49  oid sqlite3Fts3I
372ec 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  cuTokenizerModul
372ed 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  e(.  sqlite3_tok
372ee 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
372ef 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b  nst**ppModule.){
372f0 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26  .  *ppModule = &
372f1 69 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  icuTokenizerModu
372f2 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  le;.}..#endif /*
372f3 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
372f4 45 4e 41 42 4c 45 5f 49 43 55 29 20 2a 2f 0a 23  ENABLE_ICU) */.#
372f5 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
372f6 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
372f7 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
372f8 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
372f9 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
372fa 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 69 63  * End of fts3_ic
372fb 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  u.c ************
372fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372fe 2f 0a                                            /.